]> err.no Git - linux-2.6/blobdiff - fs/sysfs/dir.c
Merge branch 'drm-patches' of git://git.kernel.org/pub/scm/linux/kernel/git/airlied...
[linux-2.6] / fs / sysfs / dir.c
index 9ff04491e3ea59766f220508581d3cd01e4ac988..c4342a0199727b19fe832c4eaff371c0c8e1b9ee 100644 (file)
 #include "sysfs.h"
 
 DECLARE_RWSEM(sysfs_rename_sem);
+spinlock_t sysfs_lock = SPIN_LOCK_UNLOCKED;
 
 static void sysfs_d_iput(struct dentry * dentry, struct inode * inode)
 {
        struct sysfs_dirent * sd = dentry->d_fsdata;
 
        if (sd) {
-               BUG_ON(sd->s_dentry != dentry);
-               sd->s_dentry = NULL;
+               /* sd->s_dentry is protected with sysfs_lock.  This
+                * allows sysfs_drop_dentry() to dereference it.
+                */
+               spin_lock(&sysfs_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_lock);
                sysfs_put(sd);
        }
        iput(inode);
@@ -30,28 +42,51 @@ static struct dentry_operations sysfs_dentry_ops = {
        .d_iput         = sysfs_d_iput,
 };
 
+static unsigned int sysfs_inode_counter;
+ino_t sysfs_get_inum(void)
+{
+       if (unlikely(sysfs_inode_counter < 3))
+               sysfs_inode_counter = 3;
+       return sysfs_inode_counter++;
+}
+
 /*
  * Allocates a new sysfs_dirent and links it to the parent sysfs_dirent
  */
-static struct sysfs_dirent * sysfs_new_dirent(struct sysfs_dirent * parent_sd,
-                                               void * element)
+static struct sysfs_dirent * __sysfs_new_dirent(void * element)
 {
        struct sysfs_dirent * sd;
 
-       sd = kmem_cache_alloc(sysfs_dir_cachep, GFP_KERNEL);
+       sd = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL);
        if (!sd)
                return NULL;
 
-       memset(sd, 0, sizeof(*sd));
+       sd->s_ino = sysfs_get_inum();
        atomic_set(&sd->s_count, 1);
        atomic_set(&sd->s_event, 1);
        INIT_LIST_HEAD(&sd->s_children);
-       list_add(&sd->s_sibling, &parent_sd->s_children);
+       INIT_LIST_HEAD(&sd->s_sibling);
        sd->s_element = element;
 
        return sd;
 }
 
+static void __sysfs_list_dirent(struct sysfs_dirent *parent_sd,
+                             struct sysfs_dirent *sd)
+{
+       if (sd)
+               list_add(&sd->s_sibling, &parent_sd->s_children);
+}
+
+static struct sysfs_dirent * sysfs_new_dirent(struct sysfs_dirent *parent_sd,
+                                               void * element)
+{
+       struct sysfs_dirent *sd;
+       sd = __sysfs_new_dirent(element);
+       __sysfs_list_dirent(parent_sd, sd);
+       return sd;
+}
+
 /*
  *
  * Return -EEXIST if there is already a sysfs element with the same name for
@@ -78,14 +113,14 @@ int sysfs_dirent_exist(struct sysfs_dirent *parent_sd,
 }
 
 
-int sysfs_make_dirent(struct sysfs_dirent * parent_sd, struct dentry * dentry,
-                       void * element, umode_t mode, int type)
+static struct sysfs_dirent *
+__sysfs_make_dirent(struct dentry *dentry, void *element, mode_t mode, int type)
 {
        struct sysfs_dirent * sd;
 
-       sd = sysfs_new_dirent(parent_sd, element);
+       sd = __sysfs_new_dirent(element);
        if (!sd)
-               return -ENOMEM;
+               goto out;
 
        sd->s_mode = mode;
        sd->s_type = type;
@@ -95,7 +130,19 @@ int sysfs_make_dirent(struct sysfs_dirent * parent_sd, struct dentry * dentry,
                dentry->d_op = &sysfs_dentry_ops;
        }
 
-       return 0;
+out:
+       return sd;
+}
+
+int sysfs_make_dirent(struct sysfs_dirent * parent_sd, struct dentry * dentry,
+                       void * element, umode_t mode, int type)
+{
+       struct sysfs_dirent *sd;
+
+       sd = __sysfs_make_dirent(dentry, element, mode, type);
+       __sysfs_list_dirent(parent_sd, sd);
+
+       return sd ? 0 : -ENOMEM;
 }
 
 static int init_dir(struct inode * inode)
@@ -166,11 +213,11 @@ int sysfs_create_subdir(struct kobject * k, const char * n, struct dentry ** d)
 
 /**
  *     sysfs_create_dir - create a directory for an object.
- *     @parent:        parent parent object.
  *     @kobj:          object we're creating directory for. 
+ *     @shadow_parent: parent parent object.
  */
 
-int sysfs_create_dir(struct kobject * kobj)
+int sysfs_create_dir(struct kobject * kobj, struct dentry *shadow_parent)
 {
        struct dentry * dentry = NULL;
        struct dentry * parent;
@@ -178,7 +225,9 @@ int sysfs_create_dir(struct kobject * kobj)
 
        BUG_ON(!kobj);
 
-       if (kobj->parent)
+       if (shadow_parent)
+               parent = shadow_parent;
+       else if (kobj->parent)
                parent = kobj->parent->dentry;
        else if (sysfs_mount && sysfs_mount->mnt_sb)
                parent = sysfs_mount->mnt_sb->s_root;
@@ -210,7 +259,10 @@ static int sysfs_attach_attr(struct sysfs_dirent * sd, struct dentry * dentry)
         }
 
        dentry->d_fsdata = sysfs_get(sd);
+       /* protect sd->s_dentry against sysfs_d_iput */
+       spin_lock(&sysfs_lock);
        sd->s_dentry = dentry;
+       spin_unlock(&sysfs_lock);
        error = sysfs_create(dentry, (attr->mode & S_IALLUGO) | S_IFREG, init);
        if (error) {
                sysfs_put(sd);
@@ -232,7 +284,10 @@ static int sysfs_attach_link(struct sysfs_dirent * sd, struct dentry * dentry)
        int err = 0;
 
        dentry->d_fsdata = sysfs_get(sd);
+       /* protect sd->s_dentry against sysfs_d_iput */
+       spin_lock(&sysfs_lock);
        sd->s_dentry = dentry;
+       spin_unlock(&sysfs_lock);
        err = sysfs_create(dentry, S_IFLNK|S_IRWXUGO, init_symlink);
        if (!err) {
                dentry->d_op = &sysfs_dentry_ops;
@@ -268,7 +323,7 @@ static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
        return ERR_PTR(err);
 }
 
-struct inode_operations sysfs_dir_inode_operations = {
+const struct inode_operations sysfs_dir_inode_operations = {
        .lookup         = sysfs_lookup,
        .setattr        = sysfs_setattr,
 };
@@ -299,21 +354,12 @@ void sysfs_remove_subdir(struct dentry * d)
 }
 
 
-/**
- *     sysfs_remove_dir - remove an object's directory.
- *     @kobj:  object. 
- *
- *     The only thing special about this is that we remove any files in 
- *     the directory before we remove the directory, and we've inlined
- *     what used to be sysfs_rmdir() below, instead of calling separately.
- */
-
-void sysfs_remove_dir(struct kobject * kobj)
+static void __sysfs_remove_dir(struct dentry *dentry)
 {
-       struct dentry * dentry = dget(kobj->dentry);
        struct sysfs_dirent * parent_sd;
        struct sysfs_dirent * sd, * tmp;
 
+       dget(dentry);
        if (!dentry)
                return;
 
@@ -334,32 +380,60 @@ void sysfs_remove_dir(struct kobject * kobj)
         * Drop reference from dget() on entrance.
         */
        dput(dentry);
+}
+
+/**
+ *     sysfs_remove_dir - remove an object's directory.
+ *     @kobj:  object.
+ *
+ *     The only thing special about this is that we remove any files in
+ *     the directory before we remove the directory, and we've inlined
+ *     what used to be sysfs_rmdir() below, instead of calling separately.
+ */
+
+void sysfs_remove_dir(struct kobject * kobj)
+{
+       __sysfs_remove_dir(kobj->dentry);
        kobj->dentry = NULL;
 }
 
-int sysfs_rename_dir(struct kobject * kobj, const char *new_name)
+int sysfs_rename_dir(struct kobject * kobj, struct dentry *new_parent,
+                    const char *new_name)
 {
        int error = 0;
-       struct dentry * new_dentry, * parent;
+       struct dentry * new_dentry;
 
-       if (!strcmp(kobject_name(kobj), new_name))
-               return -EINVAL;
-
-       if (!kobj->parent)
-               return -EINVAL;
+       if (!new_parent)
+               return -EFAULT;
 
        down_write(&sysfs_rename_sem);
-       parent = kobj->parent->dentry;
-
-       mutex_lock(&parent->d_inode->i_mutex);
+       mutex_lock(&new_parent->d_inode->i_mutex);
 
-       new_dentry = lookup_one_len(new_name, parent, strlen(new_name));
+       new_dentry = lookup_one_len(new_name, new_parent, strlen(new_name));
        if (!IS_ERR(new_dentry)) {
-               if (!new_dentry->d_inode) {
+               /* By allowing two different directories with the
+                * same d_parent we allow this routine to move
+                * between different shadows of the same directory
+                */
+               if (kobj->dentry->d_parent->d_inode != new_parent->d_inode)
+                       return -EINVAL;
+               else if (new_dentry->d_parent->d_inode != new_parent->d_inode)
+                       error = -EINVAL;
+               else if (new_dentry == kobj->dentry)
+                       error = -EINVAL;
+               else if (!new_dentry->d_inode) {
                        error = kobject_set_name(kobj, "%s", new_name);
                        if (!error) {
+                               struct sysfs_dirent *sd, *parent_sd;
+
                                d_add(new_dentry, NULL);
                                d_move(kobj->dentry, new_dentry);
+
+                               sd = kobj->dentry->d_fsdata;
+                               parent_sd = new_parent->d_fsdata;
+
+                               list_del_init(&sd->s_sibling);
+                               list_add(&sd->s_sibling, &parent_sd->s_children);
                        }
                        else
                                d_drop(new_dentry);
@@ -367,7 +441,7 @@ int sysfs_rename_dir(struct kobject * kobj, const char *new_name)
                        error = -EEXIST;
                dput(new_dentry);
        }
-       mutex_unlock(&parent->d_inode->i_mutex);
+       mutex_unlock(&new_parent->d_inode->i_mutex);
        up_write(&sysfs_rename_sem);
 
        return error;
@@ -384,6 +458,8 @@ int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent)
        new_parent_dentry = new_parent ?
                new_parent->dentry : sysfs_mount->mnt_sb->s_root;
 
+       if (old_parent_dentry->d_inode == new_parent_dentry->d_inode)
+               return 0;       /* nothing to move */
 again:
        mutex_lock(&old_parent_dentry->d_inode->i_mutex);
        if (!mutex_trylock(&new_parent_dentry->d_inode->i_mutex)) {
@@ -460,7 +536,7 @@ static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
 
        switch (i) {
                case 0:
-                       ino = dentry->d_inode->i_ino;
+                       ino = parent_sd->s_ino;
                        if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
                                break;
                        filp->f_pos++;
@@ -489,10 +565,7 @@ static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
 
                                name = sysfs_get_name(next);
                                len = strlen(name);
-                               if (next->s_dentry)
-                                       ino = next->s_dentry->d_inode->i_ino;
-                               else
-                                       ino = iunique(sysfs_sb, 2);
+                               ino = next->s_ino;
 
                                if (filldir(dirent, name, len, filp->f_pos, ino,
                                                 dt_type(next)) < 0)
@@ -546,6 +619,95 @@ static loff_t sysfs_dir_lseek(struct file * file, loff_t offset, int origin)
        return offset;
 }
 
+
+/**
+ *     sysfs_make_shadowed_dir - Setup so a directory can be shadowed
+ *     @kobj:  object we're creating shadow of.
+ */
+
+int sysfs_make_shadowed_dir(struct kobject *kobj,
+       void * (*follow_link)(struct dentry *, struct nameidata *))
+{
+       struct inode *inode;
+       struct inode_operations *i_op;
+
+       inode = kobj->dentry->d_inode;
+       if (inode->i_op != &sysfs_dir_inode_operations)
+               return -EINVAL;
+
+       i_op = kmalloc(sizeof(*i_op), GFP_KERNEL);
+       if (!i_op)
+               return -ENOMEM;
+
+       memcpy(i_op, &sysfs_dir_inode_operations, sizeof(*i_op));
+       i_op->follow_link = follow_link;
+
+       /* Locking of inode->i_op?
+        * Since setting i_op is a single word write and they
+        * are atomic we should be ok here.
+        */
+       inode->i_op = i_op;
+       return 0;
+}
+
+/**
+ *     sysfs_create_shadow_dir - create a shadow directory for an object.
+ *     @kobj:  object we're creating directory for.
+ *
+ *     sysfs_make_shadowed_dir must already have been called on this
+ *     directory.
+ */
+
+struct dentry *sysfs_create_shadow_dir(struct kobject *kobj)
+{
+       struct sysfs_dirent *sd;
+       struct dentry *parent, *dir, *shadow;
+       struct inode *inode;
+
+       dir = kobj->dentry;
+       inode = dir->d_inode;
+       parent = dir->d_parent;
+       shadow = ERR_PTR(-EINVAL);
+       if (!sysfs_is_shadowed_inode(inode))
+               goto out;
+
+       shadow = d_alloc(parent, &dir->d_name);
+       if (!shadow)
+               goto nomem;
+
+       sd = __sysfs_make_dirent(shadow, kobj, inode->i_mode, SYSFS_DIR);
+       if (!sd)
+               goto nomem;
+
+       d_instantiate(shadow, igrab(inode));
+       inc_nlink(inode);
+       inc_nlink(parent->d_inode);
+       shadow->d_op = &sysfs_dentry_ops;
+
+       dget(shadow);           /* Extra count - pin the dentry in core */
+
+out:
+       return shadow;
+nomem:
+       dput(shadow);
+       shadow = ERR_PTR(-ENOMEM);
+       goto out;
+}
+
+/**
+ *     sysfs_remove_shadow_dir - remove an object's directory.
+ *     @shadow: dentry of shadow directory
+ *
+ *     The only thing special about this is that we remove any files in
+ *     the directory before we remove the directory, and we've inlined
+ *     what used to be sysfs_rmdir() below, instead of calling separately.
+ */
+
+void sysfs_remove_shadow_dir(struct dentry *shadow)
+{
+       __sysfs_remove_dir(shadow);
+}
+
 const struct file_operations sysfs_dir_operations = {
        .open           = sysfs_dir_open,
        .release        = sysfs_dir_close,