Merge branch 'linux-2.6' into merge
[powerpc.git] / fs / sysfs / symlink.c
index cbd95a4..3eac20c 100644 (file)
@@ -1,5 +1,13 @@
 /*
- * symlink.c - operations for sysfs symlinks.
+ * fs/sysfs/symlink.c - sysfs symlink implementation
+ *
+ * Copyright (c) 2001-3 Patrick Mochel
+ * Copyright (c) 2007 SUSE Linux Products GmbH
+ * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
+ *
+ * This file is released under the GPLv2.
+ *
+ * Please see Documentation/filesystems/sysfs.txt for more information.
  */
 
 #include <linux/fs.h>
@@ -7,7 +15,7 @@
 #include <linux/module.h>
 #include <linux/kobject.h>
 #include <linux/namei.h>
-#include <asm/semaphore.h>
+#include <linux/mutex.h>
 
 #include "sysfs.h"
 
@@ -44,20 +52,6 @@ static void fill_object_path(struct sysfs_dirent *sd, char *buffer, int length)
        }
 }
 
-static int sysfs_add_link(struct sysfs_dirent * parent_sd, const char * name,
-                         struct sysfs_dirent * target_sd)
-{
-       struct sysfs_dirent * sd;
-
-       sd = sysfs_new_dirent(name, S_IFLNK|S_IRWXUGO, SYSFS_KOBJ_LINK);
-       if (!sd)
-               return -ENOMEM;
-
-       sd->s_elem.symlink.target_sd = target_sd;
-       sysfs_attach_dirent(sd, parent_sd, NULL);
-       return 0;
-}
-
 /**
  *     sysfs_create_link - create symlink between two objects.
  *     @kobj:  object whose directory we're creating the link in.
@@ -68,18 +62,20 @@ int sysfs_create_link(struct kobject * kobj, struct kobject * target, const char
 {
        struct sysfs_dirent *parent_sd = NULL;
        struct sysfs_dirent *target_sd = NULL;
-       int error = -EEXIST;
+       struct sysfs_dirent *sd = NULL;
+       struct sysfs_addrm_cxt acxt;
+       int error;
 
        BUG_ON(!name);
 
-       if (!kobj) {
-               if (sysfs_mount && sysfs_mount->mnt_sb)
-                       parent_sd = sysfs_mount->mnt_sb->s_root->d_fsdata;
-       } else
+       if (!kobj)
+               parent_sd = &sysfs_root;
+       else
                parent_sd = kobj->sd;
 
+       error = -EFAULT;
        if (!parent_sd)
-               return -EFAULT;
+               goto out_put;
 
        /* target->sd can go away beneath us but is protected with
         * sysfs_assoc_lock.  Fetch target_sd from it.
@@ -89,17 +85,30 @@ int sysfs_create_link(struct kobject * kobj, struct kobject * target, const char
                target_sd = sysfs_get(target->sd);
        spin_unlock(&sysfs_assoc_lock);
 
+       error = -ENOENT;
        if (!target_sd)
-               return -ENOENT;
+               goto out_put;
+
+       error = -ENOMEM;
+       sd = sysfs_new_dirent(name, S_IFLNK|S_IRWXUGO, SYSFS_KOBJ_LINK);
+       if (!sd)
+               goto out_put;
 
-       mutex_lock(&parent_sd->s_dentry->d_inode->i_mutex);
-       if (!sysfs_find_dirent(parent_sd, name))
-               error = sysfs_add_link(parent_sd, name, target_sd);
-       mutex_unlock(&parent_sd->s_dentry->d_inode->i_mutex);
+       sd->s_symlink.target_sd = target_sd;
+       target_sd = NULL;       /* reference is now owned by the symlink */
+
+       sysfs_addrm_start(&acxt, parent_sd);
+       error = sysfs_add_one(&acxt, sd);
+       sysfs_addrm_finish(&acxt);
 
        if (error)
-               sysfs_put(target_sd);
+               goto out_put;
+
+       return 0;
 
+ out_put:
+       sysfs_put(target_sd);
+       sysfs_put(sd);
        return error;
 }
 
@@ -141,12 +150,12 @@ static int sysfs_getlink(struct dentry *dentry, char * path)
 {
        struct sysfs_dirent *sd = dentry->d_fsdata;
        struct sysfs_dirent *parent_sd = sd->s_parent;
-       struct sysfs_dirent *target_sd = sd->s_elem.symlink.target_sd;
+       struct sysfs_dirent *target_sd = sd->s_symlink.target_sd;
        int error;
 
-       down_read(&sysfs_rename_sem);
+       mutex_lock(&sysfs_mutex);
        error = sysfs_get_target_path(parent_sd, target_sd, path);
-       up_read(&sysfs_rename_sem);
+       mutex_unlock(&sysfs_mutex);
 
        return error;
 }