]> pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - fs/sysfs/dir.c
Merge branch 'linux-2.6'
[linux-2.6-omap-h63xx.git] / fs / sysfs / dir.c
index 1c3dc5d01ccde7121bb01ed79dc134ba2594e4cd..f281cc6584b0bca1f14fab02d464e027f31aabe0 100644 (file)
@@ -1,5 +1,13 @@
 /*
- * dir.c - Operations for sysfs directories.
+ * fs/sysfs/dir.c - sysfs core and dir operation 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.
  */
 
 #undef DEBUG
@@ -16,9 +24,9 @@
 
 DEFINE_MUTEX(sysfs_mutex);
 DEFINE_MUTEX(sysfs_rename_mutex);
-spinlock_t sysfs_assoc_lock = SPIN_LOCK_UNLOCKED;
+DEFINE_SPINLOCK(sysfs_assoc_lock);
 
-static spinlock_t sysfs_ino_lock = SPIN_LOCK_UNLOCKED;
+static DEFINE_SPINLOCK(sysfs_ino_lock);
 static DEFINE_IDA(sysfs_ino_ida);
 
 /**
@@ -26,7 +34,7 @@ static DEFINE_IDA(sysfs_ino_ida);
  *     @sd: sysfs_dirent of interest
  *
  *     Link @sd into its sibling list which starts from
- *     sd->s_parent->s_children.
+ *     sd->s_parent->s_dir.children.
  *
  *     Locking:
  *     mutex_lock(sysfs_mutex)
@@ -40,9 +48,9 @@ static void sysfs_link_sibling(struct sysfs_dirent *sd)
 
        /* Store directory entries in order by ino.  This allows
         * readdir to properly restart without having to add a
-        * cursor into the s_children list.
+        * cursor into the s_dir.children list.
         */
-       for (pos = &parent_sd->s_children; *pos; pos = &(*pos)->s_sibling) {
+       for (pos = &parent_sd->s_dir.children; *pos; pos = &(*pos)->s_sibling) {
                if (sd->s_ino < (*pos)->s_ino)
                        break;
        }
@@ -55,7 +63,7 @@ static void sysfs_link_sibling(struct sysfs_dirent *sd)
  *     @sd: sysfs_dirent of interest
  *
  *     Unlink @sd from its sibling list which starts from
- *     sd->s_parent->s_children.
+ *     sd->s_parent->s_dir.children.
  *
  *     Locking:
  *     mutex_lock(sysfs_mutex)
@@ -64,7 +72,8 @@ static void sysfs_unlink_sibling(struct sysfs_dirent *sd)
 {
        struct sysfs_dirent **pos;
 
-       for (pos = &sd->s_parent->s_children; *pos; pos = &(*pos)->s_sibling) {
+       for (pos = &sd->s_parent->s_dir.children; *pos;
+            pos = &(*pos)->s_sibling) {
                if (*pos == sd) {
                        *pos = sd->s_sibling;
                        sd->s_sibling = NULL;
@@ -103,8 +112,7 @@ struct dentry *sysfs_get_dentry(struct sysfs_dirent *sd)
                /* look it up */
                parent = dentry;
                mutex_lock(&parent->d_inode->i_mutex);
-               dentry = lookup_one_len_kern(cur->s_name, parent,
-                                            strlen(cur->s_name));
+               dentry = lookup_one_noperm(cur->s_name, parent);
                mutex_unlock(&parent->d_inode->i_mutex);
                dput(parent);
 
@@ -124,7 +132,7 @@ struct dentry *sysfs_get_dentry(struct sysfs_dirent *sd)
  *     RETURNS:
  *     Pointer to @sd on success, NULL on failure.
  */
-struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd)
+static struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd)
 {
        if (unlikely(!sd))
                return NULL;
@@ -153,7 +161,7 @@ struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd)
  *     Put an active reference to @sd.  This function is noop if @sd
  *     is NULL.
  */
-void sysfs_put_active(struct sysfs_dirent *sd)
+static void sysfs_put_active(struct sysfs_dirent *sd)
 {
        struct completion *cmpl;
        int v;
@@ -273,7 +281,7 @@ void release_sysfs_dirent(struct sysfs_dirent * sd)
        parent_sd = sd->s_parent;
 
        if (sysfs_type(sd) == SYSFS_KOBJ_LINK)
-               sysfs_put(sd->s_elem.symlink.target_sd);
+               sysfs_put(sd->s_symlink.target_sd);
        if (sysfs_type(sd) & SYSFS_COPY_NAME)
                kfree(sd->s_name);
        kfree(sd->s_iattr);
@@ -289,22 +297,7 @@ static void sysfs_d_iput(struct dentry * dentry, struct inode * inode)
 {
        struct sysfs_dirent * sd = dentry->d_fsdata;
 
-       if (sd) {
-               /* sd->s_dentry is protected with sysfs_assoc_lock.
-                * This allows sysfs_drop_dentry() to dereference it.
-                */
-               spin_lock(&sysfs_assoc_lock);
-
-               /* The dentry might have been deleted or another
-                * lookup could have happened updating sd->s_dentry to
-                * point the new dentry.  Ignore if it isn't pointing
-                * to this dentry.
-                */
-               if (sd->s_dentry == dentry)
-                       sd->s_dentry = NULL;
-               spin_unlock(&sysfs_assoc_lock);
-               sysfs_put(sd);
-       }
+       sysfs_put(sd);
        iput(inode);
 }
 
@@ -332,7 +325,6 @@ struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type)
 
        atomic_set(&sd->s_count, 1);
        atomic_set(&sd->s_active, 0);
-       atomic_set(&sd->s_event, 1);
 
        sd->s_name = name;
        sd->s_mode = mode;
@@ -347,30 +339,6 @@ struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type)
        return NULL;
 }
 
-/**
- *     sysfs_attach_dentry - associate sysfs_dirent with dentry
- *     @sd: target sysfs_dirent
- *     @dentry: dentry to associate
- *
- *     Associate @sd with @dentry.  This is protected by
- *     sysfs_assoc_lock to avoid race with sysfs_d_iput().
- *
- *     LOCKING:
- *     mutex_lock(sysfs_mutex)
- */
-static void sysfs_attach_dentry(struct sysfs_dirent *sd, struct dentry *dentry)
-{
-       dentry->d_op = &sysfs_dentry_ops;
-       dentry->d_fsdata = sysfs_get(sd);
-
-       /* protect sd->s_dentry against sysfs_d_iput */
-       spin_lock(&sysfs_assoc_lock);
-       sd->s_dentry = dentry;
-       spin_unlock(&sysfs_assoc_lock);
-
-       d_rehash(dentry);
-}
-
 static int sysfs_ilookup_test(struct inode *inode, void *arg)
 {
        struct sysfs_dirent *sd = arg;
@@ -434,10 +402,8 @@ void sysfs_addrm_start(struct sysfs_addrm_cxt *acxt,
  *     @sd: sysfs_dirent to be added
  *
  *     Get @acxt->parent_sd and set sd->s_parent to it and increment
- *     nlink of parent inode if @sd is a directory.  @sd is NOT
- *     linked into the children list of the parent.  The caller
- *     should invoke sysfs_link_sibling() after this function
- *     completes if @sd needs to be on the children list.
+ *     nlink of parent inode if @sd is a directory and link into the
+ *     children list of the parent.
  *
  *     This function should be called between calls to
  *     sysfs_addrm_start() and sysfs_addrm_finish() and should be
@@ -452,8 +418,12 @@ void sysfs_addrm_start(struct sysfs_addrm_cxt *acxt,
  */
 int sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
 {
-       if (sysfs_find_dirent(acxt->parent_sd, sd->s_name))
+       if (sysfs_find_dirent(acxt->parent_sd, sd->s_name)) {
+               printk(KERN_WARNING "sysfs: duplicate filename '%s' "
+                      "can not be created\n", sd->s_name);
+               WARN_ON(1);
                return -EEXIST;
+       }
 
        sd->s_parent = sysfs_get(acxt->parent_sd);
 
@@ -473,9 +443,7 @@ int sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
  *     @sd: sysfs_dirent to be added
  *
  *     Mark @sd removed and drop nlink of parent inode if @sd is a
- *     directory.  @sd is NOT unlinked from the children list of the
- *     parent.  The caller is repsonsible for removing @sd from the
- *     children list before calling this function.
+ *     directory.  @sd is unlinked from the children list.
  *
  *     This function should be called between calls to
  *     sysfs_addrm_start() and sysfs_addrm_finish() and should be
@@ -609,7 +577,7 @@ struct sysfs_dirent *sysfs_find_dirent(struct sysfs_dirent *parent_sd,
 {
        struct sysfs_dirent *sd;
 
-       for (sd = parent_sd->s_children; sd; sd = sd->s_sibling)
+       for (sd = parent_sd->s_dir.children; sd; sd = sd->s_sibling)
                if (!strcmp(sd->s_name, name))
                        return sd;
        return NULL;
@@ -654,7 +622,7 @@ static int create_dir(struct kobject *kobj, struct sysfs_dirent *parent_sd,
        sd = sysfs_new_dirent(name, mode, SYSFS_DIR);
        if (!sd)
                return -ENOMEM;
-       sd->s_elem.dir.kobj = kobj;
+       sd->s_dir.kobj = kobj;
 
        /* link in */
        sysfs_addrm_start(&acxt, parent_sd);
@@ -710,8 +678,10 @@ static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
        sd = sysfs_find_dirent(parent_sd, dentry->d_name.name);
 
        /* no such entry */
-       if (!sd)
+       if (!sd) {
+               ret = ERR_PTR(-ENOENT);
                goto out_unlock;
+       }
 
        /* attach dentry and inode */
        inode = sysfs_get_inode(sd);
@@ -720,8 +690,11 @@ static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
                goto out_unlock;
        }
 
+       /* instantiate and hash dentry */
+       dentry->d_op = &sysfs_dentry_ops;
+       dentry->d_fsdata = sysfs_get(sd);
        d_instantiate(dentry, inode);
-       sysfs_attach_dentry(sd, dentry);
+       d_rehash(dentry);
 
  out_unlock:
        mutex_unlock(&sysfs_mutex);
@@ -758,7 +731,7 @@ static void __sysfs_remove_dir(struct sysfs_dirent *dir_sd)
 
        pr_debug("sysfs %s: removing dir\n", dir_sd->s_name);
        sysfs_addrm_start(&acxt, dir_sd);
-       pos = &dir_sd->s_children;
+       pos = &dir_sd->s_dir.children;
        while (*pos) {
                struct sysfs_dirent *sd = *pos;
 
@@ -794,7 +767,7 @@ void sysfs_remove_dir(struct kobject * kobj)
 
 int sysfs_rename_dir(struct kobject * kobj, const char *new_name)
 {
-       struct sysfs_dirent *sd;
+       struct sysfs_dirent *sd = kobj->sd;
        struct dentry *parent = NULL;
        struct dentry *old_dentry = NULL, *new_dentry = NULL;
        const char *dup_name = NULL;
@@ -802,63 +775,58 @@ int sysfs_rename_dir(struct kobject * kobj, const char *new_name)
 
        mutex_lock(&sysfs_rename_mutex);
 
+       error = 0;
+       if (strcmp(sd->s_name, new_name) == 0)
+               goto out;       /* nothing to rename */
+
        /* get the original dentry */
-       sd = kobj->sd;
        old_dentry = sysfs_get_dentry(sd);
        if (IS_ERR(old_dentry)) {
                error = PTR_ERR(old_dentry);
-               goto out_dput;
+               old_dentry = NULL;
+               goto out;
        }
 
        parent = old_dentry->d_parent;
 
        /* lock parent and get dentry for new name */
        mutex_lock(&parent->d_inode->i_mutex);
+       mutex_lock(&sysfs_mutex);
 
-       new_dentry = lookup_one_len(new_name, parent, strlen(new_name));
-       if (IS_ERR(new_dentry)) {
-               error = PTR_ERR(new_dentry);
-               goto out_unlock;
-       }
-
-       error = -EINVAL;
-       if (old_dentry == new_dentry)
+       error = -EEXIST;
+       if (sysfs_find_dirent(sd->s_parent, new_name))
                goto out_unlock;
 
-       error = -EEXIST;
-       if (new_dentry->d_inode)
+       error = -ENOMEM;
+       new_dentry = d_alloc_name(parent, new_name);
+       if (!new_dentry)
                goto out_unlock;
 
        /* rename kobject and sysfs_dirent */
        error = -ENOMEM;
        new_name = dup_name = kstrdup(new_name, GFP_KERNEL);
        if (!new_name)
-               goto out_drop;
+               goto out_unlock;
 
        error = kobject_set_name(kobj, "%s", new_name);
        if (error)
-               goto out_drop;
+               goto out_unlock;
 
-       mutex_lock(&sysfs_mutex);
        dup_name = sd->s_name;
        sd->s_name = new_name;
-       mutex_unlock(&sysfs_mutex);
 
        /* rename */
        d_add(new_dentry, NULL);
-       d_move(sd->s_dentry, new_dentry);
+       d_move(old_dentry, new_dentry);
 
        error = 0;
-       goto out_unlock;
-
- out_drop:
-       d_drop(new_dentry);
  out_unlock:
+       mutex_unlock(&sysfs_mutex);
        mutex_unlock(&parent->d_inode->i_mutex);
- out_dput:
        kfree(dup_name);
        dput(old_dentry);
        dput(new_dentry);
+ out:
        mutex_unlock(&sysfs_rename_mutex);
        return error;
 }
@@ -875,56 +843,59 @@ int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent_kobj)
        BUG_ON(!sd->s_parent);
        new_parent_sd = new_parent_kobj->sd ? new_parent_kobj->sd : &sysfs_root;
 
+       error = 0;
+       if (sd->s_parent == new_parent_sd)
+               goto out;       /* nothing to move */
+
        /* get dentries */
        old_dentry = sysfs_get_dentry(sd);
        if (IS_ERR(old_dentry)) {
                error = PTR_ERR(old_dentry);
-               goto out_dput;
+               old_dentry = NULL;
+               goto out;
        }
-       old_parent = sd->s_parent->s_dentry;
+       old_parent = old_dentry->d_parent;
 
        new_parent = sysfs_get_dentry(new_parent_sd);
        if (IS_ERR(new_parent)) {
                error = PTR_ERR(new_parent);
-               goto out_dput;
+               new_parent = NULL;
+               goto out;
        }
 
-       if (old_parent->d_inode == new_parent->d_inode) {
-               error = 0;
-               goto out_dput;  /* nothing to move */
-       }
 again:
        mutex_lock(&old_parent->d_inode->i_mutex);
        if (!mutex_trylock(&new_parent->d_inode->i_mutex)) {
                mutex_unlock(&old_parent->d_inode->i_mutex);
                goto again;
        }
+       mutex_lock(&sysfs_mutex);
+
+       error = -EEXIST;
+       if (sysfs_find_dirent(new_parent_sd, sd->s_name))
+               goto out_unlock;
 
-       new_dentry = lookup_one_len(kobject_name(kobj), new_parent, strlen(kobject_name(kobj)));
-       if (IS_ERR(new_dentry)) {
-               error = PTR_ERR(new_dentry);
+       error = -ENOMEM;
+       new_dentry = d_alloc_name(new_parent, sd->s_name);
+       if (!new_dentry)
                goto out_unlock;
-       } else
-               error = 0;
+
+       error = 0;
        d_add(new_dentry, NULL);
-       d_move(sd->s_dentry, new_dentry);
-       dput(new_dentry);
+       d_move(old_dentry, new_dentry);
 
        /* Remove from old parent's list and insert into new parent's list. */
-       mutex_lock(&sysfs_mutex);
-
        sysfs_unlink_sibling(sd);
        sysfs_get(new_parent_sd);
        sysfs_put(sd->s_parent);
        sd->s_parent = new_parent_sd;
        sysfs_link_sibling(sd);
 
-       mutex_unlock(&sysfs_mutex);
-
  out_unlock:
+       mutex_unlock(&sysfs_mutex);
        mutex_unlock(&new_parent->d_inode->i_mutex);
        mutex_unlock(&old_parent->d_inode->i_mutex);
- out_dput:
+ out:
        dput(new_parent);
        dput(old_dentry);
        dput(new_dentry);
@@ -962,7 +933,7 @@ static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
                mutex_lock(&sysfs_mutex);
 
                /* Skip the dentries we have already reported */
-               pos = parent_sd->s_children;
+               pos = parent_sd->s_dir.children;
                while (pos && (filp->f_pos > pos->s_ino))
                        pos = pos->s_sibling;