tunnels: fix netns vs proto registration ordering
[safe/jmp/linux-2.6] / fs / configfs / dir.c
index e48b539..8e48b52 100644 (file)
 #include <linux/mount.h>
 #include <linux/module.h>
 #include <linux/slab.h>
+#include <linux/err.h>
 
 #include <linux/configfs.h>
 #include "configfs_internal.h"
 
 DECLARE_RWSEM(configfs_rename_sem);
+/*
+ * Protects mutations of configfs_dirent linkage together with proper i_mutex
+ * Also protects mutations of symlinks linkage to target configfs_dirent
+ * Mutators of configfs_dirent linkage must *both* have the proper inode locked
+ * and configfs_dirent_lock locked, in that order.
+ * This allows one to safely traverse configfs_dirent trees and symlinks without
+ * having to lock inodes.
+ *
+ * Protects setting of CONFIGFS_USET_DROPPING: checking the flag
+ * unlocked is not reliable unless in detach_groups() called from
+ * rmdir()/unregister() and from configfs_attach_group()
+ */
+DEFINE_SPINLOCK(configfs_dirent_lock);
 
 static void configfs_d_iput(struct dentry * dentry,
                            struct inode * inode)
@@ -58,46 +72,165 @@ static int configfs_d_delete(struct dentry *dentry)
        return 1;
 }
 
-static struct dentry_operations configfs_dentry_ops = {
+static const struct dentry_operations configfs_dentry_ops = {
        .d_iput         = configfs_d_iput,
        /* simple_delete_dentry() isn't exported */
        .d_delete       = configfs_d_delete,
 };
 
+#ifdef CONFIG_LOCKDEP
+
+/*
+ * Helpers to make lockdep happy with our recursive locking of default groups'
+ * inodes (see configfs_attach_group() and configfs_detach_group()).
+ * We put default groups i_mutexes in separate classes according to their depth
+ * from the youngest non-default group ancestor.
+ *
+ * For a non-default group A having default groups A/B, A/C, and A/C/D, default
+ * groups A/B and A/C will have their inode's mutex in class
+ * default_group_class[0], and default group A/C/D will be in
+ * default_group_class[1].
+ *
+ * The lock classes are declared and assigned in inode.c, according to the
+ * s_depth value.
+ * The s_depth value is initialized to -1, adjusted to >= 0 when attaching
+ * default groups, and reset to -1 when all default groups are attached. During
+ * attachment, if configfs_create() sees s_depth > 0, the lock class of the new
+ * inode's mutex is set to default_group_class[s_depth - 1].
+ */
+
+static void configfs_init_dirent_depth(struct configfs_dirent *sd)
+{
+       sd->s_depth = -1;
+}
+
+static void configfs_set_dir_dirent_depth(struct configfs_dirent *parent_sd,
+                                         struct configfs_dirent *sd)
+{
+       int parent_depth = parent_sd->s_depth;
+
+       if (parent_depth >= 0)
+               sd->s_depth = parent_depth + 1;
+}
+
+static void
+configfs_adjust_dir_dirent_depth_before_populate(struct configfs_dirent *sd)
+{
+       /*
+        * item's i_mutex class is already setup, so s_depth is now only
+        * used to set new sub-directories s_depth, which is always done
+        * with item's i_mutex locked.
+        */
+       /*
+        *  sd->s_depth == -1 iff we are a non default group.
+        *  else (we are a default group) sd->s_depth > 0 (see
+        *  create_dir()).
+        */
+       if (sd->s_depth == -1)
+               /*
+                * We are a non default group and we are going to create
+                * default groups.
+                */
+               sd->s_depth = 0;
+}
+
+static void
+configfs_adjust_dir_dirent_depth_after_populate(struct configfs_dirent *sd)
+{
+       /* We will not create default groups anymore. */
+       sd->s_depth = -1;
+}
+
+#else /* CONFIG_LOCKDEP */
+
+static void configfs_init_dirent_depth(struct configfs_dirent *sd)
+{
+}
+
+static void configfs_set_dir_dirent_depth(struct configfs_dirent *parent_sd,
+                                         struct configfs_dirent *sd)
+{
+}
+
+static void
+configfs_adjust_dir_dirent_depth_before_populate(struct configfs_dirent *sd)
+{
+}
+
+static void
+configfs_adjust_dir_dirent_depth_after_populate(struct configfs_dirent *sd)
+{
+}
+
+#endif /* CONFIG_LOCKDEP */
+
 /*
  * Allocates a new configfs_dirent and links it to the parent configfs_dirent
  */
-static struct configfs_dirent *configfs_new_dirent(struct configfs_dirent * parent_sd,
-                                               void * element)
+static struct configfs_dirent *configfs_new_dirent(struct configfs_dirent *parent_sd,
+                                                  void *element, int type)
 {
        struct configfs_dirent * sd;
 
-       sd = kmalloc(sizeof(*sd), GFP_KERNEL);
+       sd = kmem_cache_zalloc(configfs_dir_cachep, GFP_KERNEL);
        if (!sd)
-               return NULL;
+               return ERR_PTR(-ENOMEM);
 
-       memset(sd, 0, sizeof(*sd));
        atomic_set(&sd->s_count, 1);
        INIT_LIST_HEAD(&sd->s_links);
        INIT_LIST_HEAD(&sd->s_children);
-       list_add(&sd->s_sibling, &parent_sd->s_children);
        sd->s_element = element;
+       sd->s_type = type;
+       configfs_init_dirent_depth(sd);
+       spin_lock(&configfs_dirent_lock);
+       if (parent_sd->s_type & CONFIGFS_USET_DROPPING) {
+               spin_unlock(&configfs_dirent_lock);
+               kmem_cache_free(configfs_dir_cachep, sd);
+               return ERR_PTR(-ENOENT);
+       }
+       list_add(&sd->s_sibling, &parent_sd->s_children);
+       spin_unlock(&configfs_dirent_lock);
 
        return sd;
 }
 
+/*
+ *
+ * Return -EEXIST if there is already a configfs element with the same
+ * name for the same parent.
+ *
+ * called with parent inode's i_mutex held
+ */
+static int configfs_dirent_exists(struct configfs_dirent *parent_sd,
+                                 const unsigned char *new)
+{
+       struct configfs_dirent * sd;
+
+       list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
+               if (sd->s_element) {
+                       const unsigned char *existing = configfs_get_name(sd);
+                       if (strcmp(existing, new))
+                               continue;
+                       else
+                               return -EEXIST;
+               }
+       }
+
+       return 0;
+}
+
+
 int configfs_make_dirent(struct configfs_dirent * parent_sd,
                         struct dentry * dentry, void * element,
                         umode_t mode, int type)
 {
        struct configfs_dirent * sd;
 
-       sd = configfs_new_dirent(parent_sd, element);
-       if (!sd)
-               return -ENOMEM;
+       sd = configfs_new_dirent(parent_sd, element, type);
+       if (IS_ERR(sd))
+               return PTR_ERR(sd);
 
        sd->s_mode = mode;
-       sd->s_type = type;
        sd->s_dentry = dentry;
        if (dentry) {
                dentry->d_fsdata = configfs_get(sd);
@@ -113,11 +246,11 @@ static int init_dir(struct inode * inode)
        inode->i_fop = &configfs_dir_operations;
 
        /* directory inodes start off with i_nlink == 2 (for "." entry) */
-       inode->i_nlink++;
+       inc_nlink(inode);
        return 0;
 }
 
-static int init_file(struct inode * inode)
+static int configfs_init_file(struct inode * inode)
 {
        inode->i_size = PAGE_SIZE;
        inode->i_fop = &configfs_file_operations;
@@ -136,13 +269,24 @@ static int create_dir(struct config_item * k, struct dentry * p,
        int error;
        umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
 
-       error = configfs_create(d, mode, init_dir);
-       if (!error) {
+       error = configfs_dirent_exists(p->d_fsdata, d->d_name.name);
+       if (!error)
                error = configfs_make_dirent(p->d_fsdata, d, k, mode,
-                                          CONFIGFS_DIR);
+                                            CONFIGFS_DIR | CONFIGFS_USET_CREATING);
+       if (!error) {
+               configfs_set_dir_dirent_depth(p->d_fsdata, d->d_fsdata);
+               error = configfs_create(d, mode, init_dir);
                if (!error) {
-                       p->d_inode->i_nlink++;
+                       inc_nlink(p->d_inode);
                        (d)->d_op = &configfs_dentry_ops;
+               } else {
+                       struct configfs_dirent *sd = d->d_fsdata;
+                       if (sd) {
+                               spin_lock(&configfs_dirent_lock);
+                               list_del_init(&sd->s_sibling);
+                               spin_unlock(&configfs_dirent_lock);
+                               configfs_put(sd);
+                       }
                }
        }
        return error;
@@ -153,6 +297,9 @@ static int create_dir(struct config_item * k, struct dentry * p,
  *     configfs_create_dir - create a directory for an config_item.
  *     @item:          config_itemwe're creating directory for.
  *     @dentry:        config_item's dentry.
+ *
+ *     Note: user-created entries won't be allowed under this new directory
+ *     until it is validated by configfs_dir_set_ready()
  */
 
 static int configfs_create_dir(struct config_item * item, struct dentry *dentry)
@@ -175,6 +322,44 @@ static int configfs_create_dir(struct config_item * item, struct dentry *dentry)
        return error;
 }
 
+/*
+ * Allow userspace to create new entries under a new directory created with
+ * configfs_create_dir(), and under all of its chidlren directories recursively.
+ * @sd         configfs_dirent of the new directory to validate
+ *
+ * Caller must hold configfs_dirent_lock.
+ */
+static void configfs_dir_set_ready(struct configfs_dirent *sd)
+{
+       struct configfs_dirent *child_sd;
+
+       sd->s_type &= ~CONFIGFS_USET_CREATING;
+       list_for_each_entry(child_sd, &sd->s_children, s_sibling)
+               if (child_sd->s_type & CONFIGFS_USET_CREATING)
+                       configfs_dir_set_ready(child_sd);
+}
+
+/*
+ * Check that a directory does not belong to a directory hierarchy being
+ * attached and not validated yet.
+ * @sd         configfs_dirent of the directory to check
+ *
+ * @return     non-zero iff the directory was validated
+ *
+ * Note: takes configfs_dirent_lock, so the result may change from false to true
+ * in two consecutive calls, but never from true to false.
+ */
+int configfs_dirent_is_ready(struct configfs_dirent *sd)
+{
+       int ret;
+
+       spin_lock(&configfs_dirent_lock);
+       ret = !(sd->s_type & CONFIGFS_USET_CREATING);
+       spin_unlock(&configfs_dirent_lock);
+
+       return ret;
+}
+
 int configfs_create_link(struct configfs_symlink *sl,
                         struct dentry *parent,
                         struct dentry *dentry)
@@ -182,12 +367,21 @@ int configfs_create_link(struct configfs_symlink *sl,
        int err = 0;
        umode_t mode = S_IFLNK | S_IRWXUGO;
 
-       err = configfs_create(dentry, mode, init_symlink);
+       err = configfs_make_dirent(parent->d_fsdata, dentry, sl, mode,
+                                  CONFIGFS_ITEM_LINK);
        if (!err) {
-               err = configfs_make_dirent(parent->d_fsdata, dentry, sl,
-                                        mode, CONFIGFS_ITEM_LINK);
+               err = configfs_create(dentry, mode, init_symlink);
                if (!err)
                        dentry->d_op = &configfs_dentry_ops;
+               else {
+                       struct configfs_dirent *sd = dentry->d_fsdata;
+                       if (sd) {
+                               spin_lock(&configfs_dirent_lock);
+                               list_del_init(&sd->s_sibling);
+                               spin_unlock(&configfs_dirent_lock);
+                               configfs_put(sd);
+                       }
+               }
        }
        return err;
 }
@@ -198,7 +392,9 @@ static void remove_dir(struct dentry * d)
        struct configfs_dirent * sd;
 
        sd = d->d_fsdata;
-       list_del_init(&sd->s_sibling);
+       spin_lock(&configfs_dirent_lock);
+       list_del_init(&sd->s_sibling);
+       spin_unlock(&configfs_dirent_lock);
        configfs_put(sd);
        if (d->d_inode)
                simple_rmdir(parent->d_inode,d);
@@ -216,6 +412,8 @@ static void remove_dir(struct dentry * d)
  * 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 configfs_rmdir() below, instead of calling separately.
+ *
+ * Caller holds the mutex of the item's inode
  */
 
 static void configfs_remove_dir(struct config_item * item)
@@ -241,13 +439,16 @@ static int configfs_attach_attr(struct configfs_dirent * sd, struct dentry * den
        struct configfs_attribute * attr = sd->s_element;
        int error;
 
-       error = configfs_create(dentry, (attr->ca_mode & S_IALLUGO) | S_IFREG, init_file);
-       if (error)
+       dentry->d_fsdata = configfs_get(sd);
+       sd->s_dentry = dentry;
+       error = configfs_create(dentry, (attr->ca_mode & S_IALLUGO) | S_IFREG,
+                               configfs_init_file);
+       if (error) {
+               configfs_put(sd);
                return error;
+       }
 
        dentry->d_op = &configfs_dentry_ops;
-       dentry->d_fsdata = configfs_get(sd);
-       sd->s_dentry = dentry;
        d_rehash(dentry);
 
        return 0;
@@ -260,7 +461,19 @@ static struct dentry * configfs_lookup(struct inode *dir,
        struct configfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
        struct configfs_dirent * sd;
        int found = 0;
-       int err = 0;
+       int err;
+
+       /*
+        * Fake invisibility if dir belongs to a group/default groups hierarchy
+        * being attached
+        *
+        * This forbids userspace to read/write attributes of items which may
+        * not complete their initialization, since the dentries of the
+        * attributes won't be instantiated.
+        */
+       err = -ENOENT;
+       if (!configfs_dirent_is_ready(parent_sd))
+               goto out;
 
        list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
                if (sd->s_type & CONFIGFS_NOT_PINNED) {
@@ -283,39 +496,51 @@ static struct dentry * configfs_lookup(struct inode *dir,
                return simple_lookup(dir, dentry, nd);
        }
 
+out:
        return ERR_PTR(err);
 }
 
 /*
  * Only subdirectories count here.  Files (CONFIGFS_NOT_PINNED) are
- * attributes and are removed by rmdir().  We recurse, taking i_sem
- * on all children that are candidates for default detach.  If the
- * result is clean, then configfs_detach_group() will handle dropping
- * i_sem.  If there is an error, the caller will clean up the i_sem
- * holders via configfs_detach_rollback().
+ * attributes and are removed by rmdir().  We recurse, setting
+ * CONFIGFS_USET_DROPPING on all children that are candidates for
+ * default detach.
+ * If there is an error, the caller will reset the flags via
+ * configfs_detach_rollback().
  */
-static int configfs_detach_prep(struct dentry *dentry)
+static int configfs_detach_prep(struct dentry *dentry, struct mutex **wait_mutex)
 {
        struct configfs_dirent *parent_sd = dentry->d_fsdata;
        struct configfs_dirent *sd;
        int ret;
 
+       /* Mark that we're trying to drop the group */
+       parent_sd->s_type |= CONFIGFS_USET_DROPPING;
+
        ret = -EBUSY;
        if (!list_empty(&parent_sd->s_links))
                goto out;
 
        ret = 0;
        list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
-               if (sd->s_type & CONFIGFS_NOT_PINNED)
+               if (!sd->s_element ||
+                   (sd->s_type & CONFIGFS_NOT_PINNED))
                        continue;
                if (sd->s_type & CONFIGFS_USET_DEFAULT) {
-                       down(&sd->s_dentry->d_inode->i_sem);
-                       /* Mark that we've taken i_sem */
-                       sd->s_type |= CONFIGFS_USET_DROPPING;
+                       /* Abort if racing with mkdir() */
+                       if (sd->s_type & CONFIGFS_USET_IN_MKDIR) {
+                               if (wait_mutex)
+                                       *wait_mutex = &sd->s_dentry->d_inode->i_mutex;
+                               return -EAGAIN;
+                       }
 
-                       ret = configfs_detach_prep(sd->s_dentry);
+                       /*
+                        * Yup, recursive.  If there's a problem, blame
+                        * deep nesting of default_groups
+                        */
+                       ret = configfs_detach_prep(sd->s_dentry, wait_mutex);
                        if (!ret)
-                               continue;
+                               continue;
                } else
                        ret = -ENOTEMPTY;
 
@@ -327,7 +552,7 @@ out:
 }
 
 /*
- * Walk the tree, dropping i_sem wherever CONFIGFS_USET_DROPPING is
+ * Walk the tree, resetting CONFIGFS_USET_DROPPING wherever it was
  * set.
  */
 static void configfs_detach_rollback(struct dentry *dentry)
@@ -335,16 +560,11 @@ static void configfs_detach_rollback(struct dentry *dentry)
        struct configfs_dirent *parent_sd = dentry->d_fsdata;
        struct configfs_dirent *sd;
 
-       list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
-               if (sd->s_type & CONFIGFS_USET_DEFAULT) {
-                       configfs_detach_rollback(sd->s_dentry);
+       parent_sd->s_type &= ~CONFIGFS_USET_DROPPING;
 
-                       if (sd->s_type & CONFIGFS_USET_DROPPING) {
-                               sd->s_type &= ~CONFIGFS_USET_DROPPING;
-                               up(&sd->s_dentry->d_inode->i_sem);
-                       }
-               }
-       }
+       list_for_each_entry(sd, &parent_sd->s_children, s_sibling)
+               if (sd->s_type & CONFIGFS_USET_DEFAULT)
+                       configfs_detach_rollback(sd->s_dentry);
 }
 
 static void detach_attrs(struct config_item * item)
@@ -363,7 +583,9 @@ static void detach_attrs(struct config_item * item)
        list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
                if (!sd->s_element || !(sd->s_type & CONFIGFS_NOT_PINNED))
                        continue;
+               spin_lock(&configfs_dirent_lock);
                list_del_init(&sd->s_sibling);
+               spin_unlock(&configfs_dirent_lock);
                configfs_drop_dentry(sd, dentry);
                configfs_put(sd);
        }
@@ -419,16 +641,12 @@ static void detach_groups(struct config_group *group)
 
                child = sd->s_dentry;
 
+               mutex_lock(&child->d_inode->i_mutex);
+
                configfs_detach_group(sd->s_element);
                child->d_inode->i_flags |= S_DEAD;
 
-               /*
-                * From rmdir/unregister, a configfs_detach_prep() pass
-                * has taken our i_sem for us.  Drop it.
-                * From mkdir/register cleanup, there is no sem held.
-                */
-               if (sd->s_type & CONFIGFS_USET_DROPPING)
-                       up(&child->d_inode->i_sem);
+               mutex_unlock(&child->d_inode->i_mutex);
 
                d_delete(child);
                dput(child);
@@ -485,40 +703,27 @@ static int create_default_group(struct config_group *parent_group,
 static int populate_groups(struct config_group *group)
 {
        struct config_group *new_group;
-       struct dentry *dentry = group->cg_item.ci_dentry;
        int ret = 0;
        int i;
 
-       if (group && group->default_groups) {
-               /* FYI, we're faking mkdir here
-                * I'm not sure we need this semaphore, as we're called
-                * from our parent's mkdir.  That holds our parent's
-                * i_sem, so afaik lookup cannot continue through our
-                * parent to find us, let alone mess with our tree.
-                * That said, taking our i_sem is closer to mkdir
-                * emulation, and shouldn't hurt. */
-               down(&dentry->d_inode->i_sem);
-
+       if (group->default_groups) {
                for (i = 0; group->default_groups[i]; i++) {
                        new_group = group->default_groups[i];
 
                        ret = create_default_group(group, new_group);
-                       if (ret)
+                       if (ret) {
+                               detach_groups(group);
                                break;
+                       }
                }
-
-               up(&dentry->d_inode->i_sem);
        }
 
-       if (ret)
-               detach_groups(group);
-
        return ret;
 }
 
 /*
  * All of link_obj/unlink_obj/link_group/unlink_group require that
- * subsys->su_sem is held.
+ * subsys->su_mutex is held.
  */
 
 static void unlink_obj(struct config_item *item)
@@ -531,20 +736,34 @@ static void unlink_obj(struct config_item *item)
 
                item->ci_group = NULL;
                item->ci_parent = NULL;
+
+               /* Drop the reference for ci_entry */
                config_item_put(item);
 
+               /* Drop the reference for ci_parent */
                config_group_put(group);
        }
 }
 
 static void link_obj(struct config_item *parent_item, struct config_item *item)
 {
-       /* Parent seems redundant with group, but it makes certain
-        * traversals much nicer. */
+       /*
+        * Parent seems redundant with group, but it makes certain
+        * traversals much nicer.
+        */
        item->ci_parent = parent_item;
+
+       /*
+        * We hold a reference on the parent for the child's ci_parent
+        * link.
+        */
        item->ci_group = config_group_get(to_config_group(parent_item));
        list_add_tail(&item->ci_entry, &item->ci_group->cg_children);
 
+       /*
+        * We hold a reference on the child for ci_entry on the parent's
+        * cg_children
+        */
        config_item_get(item);
 }
 
@@ -613,7 +832,15 @@ static int configfs_attach_item(struct config_item *parent_item,
        if (!ret) {
                ret = populate_attrs(item);
                if (ret) {
+                       /*
+                        * We are going to remove an inode and its dentry but
+                        * the VFS may already have hit and used them. Thus,
+                        * we must lock them as rmdir() would.
+                        */
+                       mutex_lock(&dentry->d_inode->i_mutex);
                        configfs_remove_dir(item);
+                       dentry->d_inode->i_flags |= S_DEAD;
+                       mutex_unlock(&dentry->d_inode->i_mutex);
                        d_delete(dentry);
                }
        }
@@ -621,6 +848,7 @@ static int configfs_attach_item(struct config_item *parent_item,
        return ret;
 }
 
+/* Caller holds the mutex of the item's inode */
 static void configfs_detach_item(struct config_item *item)
 {
        detach_attrs(item);
@@ -639,16 +867,32 @@ static int configfs_attach_group(struct config_item *parent_item,
                sd = dentry->d_fsdata;
                sd->s_type |= CONFIGFS_USET_DIR;
 
+               /*
+                * FYI, we're faking mkdir in populate_groups()
+                * We must lock the group's inode to avoid races with the VFS
+                * which can already hit the inode and try to add/remove entries
+                * under it.
+                *
+                * We must also lock the inode to remove it safely in case of
+                * error, as rmdir() would.
+                */
+               mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_CHILD);
+               configfs_adjust_dir_dirent_depth_before_populate(sd);
                ret = populate_groups(to_config_group(item));
                if (ret) {
                        configfs_detach_item(item);
-                       d_delete(dentry);
+                       dentry->d_inode->i_flags |= S_DEAD;
                }
+               configfs_adjust_dir_dirent_depth_after_populate(sd);
+               mutex_unlock(&dentry->d_inode->i_mutex);
+               if (ret)
+                       d_delete(dentry);
        }
 
        return ret;
 }
 
+/* Caller holds the mutex of the group's inode */
 static void configfs_detach_group(struct config_item *item)
 {
        detach_groups(to_config_group(item));
@@ -656,6 +900,28 @@ static void configfs_detach_group(struct config_item *item)
 }
 
 /*
+ * After the item has been detached from the filesystem view, we are
+ * ready to tear it out of the hierarchy.  Notify the client before
+ * we do that so they can perform any cleanup that requires
+ * navigating the hierarchy.  A client does not need to provide this
+ * callback.  The subsystem semaphore MUST be held by the caller, and
+ * references must be valid for both items.  It also assumes the
+ * caller has validated ci_type.
+ */
+static void client_disconnect_notify(struct config_item *parent_item,
+                                    struct config_item *item)
+{
+       struct config_item_type *type;
+
+       type = parent_item->ci_type;
+       BUG_ON(!type);
+
+       if (type->ct_group_ops && type->ct_group_ops->disconnect_notify)
+               type->ct_group_ops->disconnect_notify(to_config_group(parent_item),
+                                                     item);
+}
+
+/*
  * Drop the initial reference from make_item()/make_group()
  * This function assumes that reference is held on item
  * and that item holds a valid reference to the parent.  Also, it
@@ -669,33 +935,273 @@ static void client_drop_item(struct config_item *parent_item,
        type = parent_item->ci_type;
        BUG_ON(!type);
 
+       /*
+        * If ->drop_item() exists, it is responsible for the
+        * config_item_put().
+        */
        if (type->ct_group_ops && type->ct_group_ops->drop_item)
                type->ct_group_ops->drop_item(to_config_group(parent_item),
-                                               item);
+                                             item);
        else
                config_item_put(item);
 }
 
+#ifdef DEBUG
+static void configfs_dump_one(struct configfs_dirent *sd, int level)
+{
+       printk(KERN_INFO "%*s\"%s\":\n", level, " ", configfs_get_name(sd));
+
+#define type_print(_type) if (sd->s_type & _type) printk(KERN_INFO "%*s %s\n", level, " ", #_type);
+       type_print(CONFIGFS_ROOT);
+       type_print(CONFIGFS_DIR);
+       type_print(CONFIGFS_ITEM_ATTR);
+       type_print(CONFIGFS_ITEM_LINK);
+       type_print(CONFIGFS_USET_DIR);
+       type_print(CONFIGFS_USET_DEFAULT);
+       type_print(CONFIGFS_USET_DROPPING);
+#undef type_print
+}
 
-static int configfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
+static int configfs_dump(struct configfs_dirent *sd, int level)
+{
+       struct configfs_dirent *child_sd;
+       int ret = 0;
+
+       configfs_dump_one(sd, level);
+
+       if (!(sd->s_type & (CONFIGFS_DIR|CONFIGFS_ROOT)))
+               return 0;
+
+       list_for_each_entry(child_sd, &sd->s_children, s_sibling) {
+               ret = configfs_dump(child_sd, level + 2);
+               if (ret)
+                       break;
+       }
+
+       return ret;
+}
+#endif
+
+
+/*
+ * configfs_depend_item() and configfs_undepend_item()
+ *
+ * WARNING: Do not call these from a configfs callback!
+ *
+ * This describes these functions and their helpers.
+ *
+ * Allow another kernel system to depend on a config_item.  If this
+ * happens, the item cannot go away until the dependant can live without
+ * it.  The idea is to give client modules as simple an interface as
+ * possible.  When a system asks them to depend on an item, they just
+ * call configfs_depend_item().  If the item is live and the client
+ * driver is in good shape, we'll happily do the work for them.
+ *
+ * Why is the locking complex?  Because configfs uses the VFS to handle
+ * all locking, but this function is called outside the normal
+ * VFS->configfs path.  So it must take VFS locks to prevent the
+ * VFS->configfs stuff (configfs_mkdir(), configfs_rmdir(), etc).  This is
+ * why you can't call these functions underneath configfs callbacks.
+ *
+ * Note, btw, that this can be called at *any* time, even when a configfs
+ * subsystem isn't registered, or when configfs is loading or unloading.
+ * Just like configfs_register_subsystem().  So we take the same
+ * precautions.  We pin the filesystem.  We lock configfs_dirent_lock.
+ * If we can find the target item in the
+ * configfs tree, it must be part of the subsystem tree as well, so we
+ * do not need the subsystem semaphore.  Holding configfs_dirent_lock helps
+ * locking out mkdir() and rmdir(), who might be racing us.
+ */
+
+/*
+ * configfs_depend_prep()
+ *
+ * Only subdirectories count here.  Files (CONFIGFS_NOT_PINNED) are
+ * attributes.  This is similar but not the same to configfs_detach_prep().
+ * Note that configfs_detach_prep() expects the parent to be locked when it
+ * is called, but we lock the parent *inside* configfs_depend_prep().  We
+ * do that so we can unlock it if we find nothing.
+ *
+ * Here we do a depth-first search of the dentry hierarchy looking for
+ * our object.
+ * We deliberately ignore items tagged as dropping since they are virtually
+ * dead, as well as items in the middle of attachment since they virtually
+ * do not exist yet. This completes the locking out of racing mkdir() and
+ * rmdir().
+ * Note: subdirectories in the middle of attachment start with s_type =
+ * CONFIGFS_DIR|CONFIGFS_USET_CREATING set by create_dir().  When
+ * CONFIGFS_USET_CREATING is set, we ignore the item.  The actual set of
+ * s_type is in configfs_new_dirent(), which has configfs_dirent_lock.
+ *
+ * If the target is not found, -ENOENT is bubbled up.
+ *
+ * This adds a requirement that all config_items be unique!
+ *
+ * This is recursive.  There isn't
+ * much on the stack, though, so folks that need this function - be careful
+ * about your stack!  Patches will be accepted to make it iterative.
+ */
+static int configfs_depend_prep(struct dentry *origin,
+                               struct config_item *target)
+{
+       struct configfs_dirent *child_sd, *sd = origin->d_fsdata;
+       int ret = 0;
+
+       BUG_ON(!origin || !sd);
+
+       if (sd->s_element == target)  /* Boo-yah */
+               goto out;
+
+       list_for_each_entry(child_sd, &sd->s_children, s_sibling) {
+               if ((child_sd->s_type & CONFIGFS_DIR) &&
+                   !(child_sd->s_type & CONFIGFS_USET_DROPPING) &&
+                   !(child_sd->s_type & CONFIGFS_USET_CREATING)) {
+                       ret = configfs_depend_prep(child_sd->s_dentry,
+                                                  target);
+                       if (!ret)
+                               goto out;  /* Child path boo-yah */
+               }
+       }
+
+       /* We looped all our children and didn't find target */
+       ret = -ENOENT;
+
+out:
+       return ret;
+}
+
+int configfs_depend_item(struct configfs_subsystem *subsys,
+                        struct config_item *target)
 {
        int ret;
-       struct config_group *group;
-       struct config_item *item;
+       struct configfs_dirent *p, *root_sd, *subsys_sd = NULL;
+       struct config_item *s_item = &subsys->su_group.cg_item;
+
+       /*
+        * Pin the configfs filesystem.  This means we can safely access
+        * the root of the configfs filesystem.
+        */
+       ret = configfs_pin_fs();
+       if (ret)
+               return ret;
+
+       /*
+        * Next, lock the root directory.  We're going to check that the
+        * subsystem is really registered, and so we need to lock out
+        * configfs_[un]register_subsystem().
+        */
+       mutex_lock(&configfs_sb->s_root->d_inode->i_mutex);
+
+       root_sd = configfs_sb->s_root->d_fsdata;
+
+       list_for_each_entry(p, &root_sd->s_children, s_sibling) {
+               if (p->s_type & CONFIGFS_DIR) {
+                       if (p->s_element == s_item) {
+                               subsys_sd = p;
+                               break;
+                       }
+               }
+       }
+
+       if (!subsys_sd) {
+               ret = -ENOENT;
+               goto out_unlock_fs;
+       }
+
+       /* Ok, now we can trust subsys/s_item */
+
+       spin_lock(&configfs_dirent_lock);
+       /* Scan the tree, return 0 if found */
+       ret = configfs_depend_prep(subsys_sd->s_dentry, target);
+       if (ret)
+               goto out_unlock_dirent_lock;
+
+       /*
+        * We are sure that the item is not about to be removed by rmdir(), and
+        * not in the middle of attachment by mkdir().
+        */
+       p = target->ci_dentry->d_fsdata;
+       p->s_dependent_count += 1;
+
+out_unlock_dirent_lock:
+       spin_unlock(&configfs_dirent_lock);
+out_unlock_fs:
+       mutex_unlock(&configfs_sb->s_root->d_inode->i_mutex);
+
+       /*
+        * If we succeeded, the fs is pinned via other methods.  If not,
+        * we're done with it anyway.  So release_fs() is always right.
+        */
+       configfs_release_fs();
+
+       return ret;
+}
+EXPORT_SYMBOL(configfs_depend_item);
+
+/*
+ * Release the dependent linkage.  This is much simpler than
+ * configfs_depend_item() because we know that that the client driver is
+ * pinned, thus the subsystem is pinned, and therefore configfs is pinned.
+ */
+void configfs_undepend_item(struct configfs_subsystem *subsys,
+                           struct config_item *target)
+{
+       struct configfs_dirent *sd;
+
+       /*
+        * Since we can trust everything is pinned, we just need
+        * configfs_dirent_lock.
+        */
+       spin_lock(&configfs_dirent_lock);
+
+       sd = target->ci_dentry->d_fsdata;
+       BUG_ON(sd->s_dependent_count < 1);
+
+       sd->s_dependent_count -= 1;
+
+       /*
+        * After this unlock, we cannot trust the item to stay alive!
+        * DO NOT REFERENCE item after this unlock.
+        */
+       spin_unlock(&configfs_dirent_lock);
+}
+EXPORT_SYMBOL(configfs_undepend_item);
+
+static int configfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
+{
+       int ret = 0;
+       int module_got = 0;
+       struct config_group *group = NULL;
+       struct config_item *item = NULL;
        struct config_item *parent_item;
        struct configfs_subsystem *subsys;
        struct configfs_dirent *sd;
        struct config_item_type *type;
-       struct module *owner;
+       struct module *subsys_owner = NULL, *new_item_owner = NULL;
        char *name;
 
-       if (dentry->d_parent == configfs_sb->s_root)
-               return -EPERM;
+       if (dentry->d_parent == configfs_sb->s_root) {
+               ret = -EPERM;
+               goto out;
+       }
 
        sd = dentry->d_parent->d_fsdata;
-       if (!(sd->s_type & CONFIGFS_USET_DIR))
-               return -EPERM;
 
+       /*
+        * Fake invisibility if dir belongs to a group/default groups hierarchy
+        * being attached
+        */
+       if (!configfs_dirent_is_ready(sd)) {
+               ret = -ENOENT;
+               goto out;
+       }
+
+       if (!(sd->s_type & CONFIGFS_USET_DIR)) {
+               ret = -EPERM;
+               goto out;
+       }
+
+       /* Get a working ref for the duration of this function */
        parent_item = configfs_get_config_item(dentry->d_parent);
        type = parent_item->ci_type;
        subsys = to_config_group(parent_item)->cg_subsys;
@@ -704,69 +1210,140 @@ static int configfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
        if (!type || !type->ct_group_ops ||
            (!type->ct_group_ops->make_group &&
             !type->ct_group_ops->make_item)) {
-               config_item_put(parent_item);
-               return -EPERM;  /* What lack-of-mkdir returns */
+               ret = -EPERM;  /* Lack-of-mkdir returns -EPERM */
+               goto out_put;
+       }
+
+       /*
+        * The subsystem may belong to a different module than the item
+        * being created.  We don't want to safely pin the new item but
+        * fail to pin the subsystem it sits under.
+        */
+       if (!subsys->su_group.cg_item.ci_type) {
+               ret = -EINVAL;
+               goto out_put;
+       }
+       subsys_owner = subsys->su_group.cg_item.ci_type->ct_owner;
+       if (!try_module_get(subsys_owner)) {
+               ret = -EINVAL;
+               goto out_put;
        }
 
        name = kmalloc(dentry->d_name.len + 1, GFP_KERNEL);
        if (!name) {
-               config_item_put(parent_item);
-               return -ENOMEM;
+               ret = -ENOMEM;
+               goto out_subsys_put;
        }
+
        snprintf(name, dentry->d_name.len + 1, "%s", dentry->d_name.name);
 
-       down(&subsys->su_sem);
-       group = NULL;
-       item = NULL;
+       mutex_lock(&subsys->su_mutex);
        if (type->ct_group_ops->make_group) {
                group = type->ct_group_ops->make_group(to_config_group(parent_item), name);
-               if (group) {
+               if (!group)
+                       group = ERR_PTR(-ENOMEM);
+               if (!IS_ERR(group)) {
                        link_group(to_config_group(parent_item), group);
                        item = &group->cg_item;
-               }
+               } else
+                       ret = PTR_ERR(group);
        } else {
                item = type->ct_group_ops->make_item(to_config_group(parent_item), name);
-               if (item)
+               if (!item)
+                       item = ERR_PTR(-ENOMEM);
+               if (!IS_ERR(item))
                        link_obj(parent_item, item);
+               else
+                       ret = PTR_ERR(item);
        }
-       up(&subsys->su_sem);
+       mutex_unlock(&subsys->su_mutex);
 
        kfree(name);
-       if (!item) {
-               config_item_put(parent_item);
-               return -ENOMEM;
+       if (ret) {
+               /*
+                * If ret != 0, then link_obj() was never called.
+                * There are no extra references to clean up.
+                */
+               goto out_subsys_put;
        }
 
-       ret = -EINVAL;
+       /*
+        * link_obj() has been called (via link_group() for groups).
+        * From here on out, errors must clean that up.
+        */
+
        type = item->ci_type;
-       if (type) {
-               owner = type->ct_owner;
-               if (try_module_get(owner)) {
-                       if (group) {
-                               ret = configfs_attach_group(parent_item,
-                                                           item,
-                                                           dentry);
-                       } else {
-                               ret = configfs_attach_item(parent_item,
-                                                          item,
-                                                          dentry);
-                       }
+       if (!type) {
+               ret = -EINVAL;
+               goto out_unlink;
+       }
 
-                       if (ret) {
-                               down(&subsys->su_sem);
-                               if (group)
-                                       unlink_group(group);
-                               else
-                                       unlink_obj(item);
-                               client_drop_item(parent_item, item);
-                               up(&subsys->su_sem);
+       new_item_owner = type->ct_owner;
+       if (!try_module_get(new_item_owner)) {
+               ret = -EINVAL;
+               goto out_unlink;
+       }
 
-                               config_item_put(parent_item);
-                               module_put(owner);
-                       }
-               }
+       /*
+        * I hate doing it this way, but if there is
+        * an error,  module_put() probably should
+        * happen after any cleanup.
+        */
+       module_got = 1;
+
+       /*
+        * Make racing rmdir() fail if it did not tag parent with
+        * CONFIGFS_USET_DROPPING
+        * Note: if CONFIGFS_USET_DROPPING is already set, attach_group() will
+        * fail and let rmdir() terminate correctly
+        */
+       spin_lock(&configfs_dirent_lock);
+       /* This will make configfs_detach_prep() fail */
+       sd->s_type |= CONFIGFS_USET_IN_MKDIR;
+       spin_unlock(&configfs_dirent_lock);
+
+       if (group)
+               ret = configfs_attach_group(parent_item, item, dentry);
+       else
+               ret = configfs_attach_item(parent_item, item, dentry);
+
+       spin_lock(&configfs_dirent_lock);
+       sd->s_type &= ~CONFIGFS_USET_IN_MKDIR;
+       if (!ret)
+               configfs_dir_set_ready(dentry->d_fsdata);
+       spin_unlock(&configfs_dirent_lock);
+
+out_unlink:
+       if (ret) {
+               /* Tear down everything we built up */
+               mutex_lock(&subsys->su_mutex);
+
+               client_disconnect_notify(parent_item, item);
+               if (group)
+                       unlink_group(group);
+               else
+                       unlink_obj(item);
+               client_drop_item(parent_item, item);
+
+               mutex_unlock(&subsys->su_mutex);
+
+               if (module_got)
+                       module_put(new_item_owner);
        }
 
+out_subsys_put:
+       if (ret)
+               module_put(subsys_owner);
+
+out_put:
+       /*
+        * link_obj()/link_group() took a reference from child->parent,
+        * so the parent is safely pinned.  We can drop our working
+        * reference.
+        */
+       config_item_put(parent_item);
+
+out:
        return ret;
 }
 
@@ -776,7 +1353,7 @@ static int configfs_rmdir(struct inode *dir, struct dentry *dentry)
        struct config_item *item;
        struct configfs_subsystem *subsys;
        struct configfs_dirent *sd;
-       struct module *owner = NULL;
+       struct module *subsys_owner = NULL, *dead_item_owner = NULL;
        int ret;
 
        if (dentry->d_parent == configfs_sb->s_root)
@@ -786,6 +1363,7 @@ static int configfs_rmdir(struct inode *dir, struct dentry *dentry)
        if (sd->s_type & CONFIGFS_USET_DEFAULT)
                return -EPERM;
 
+       /* Get a working ref until we have the child */
        parent_item = configfs_get_config_item(dentry->d_parent);
        subsys = to_config_group(parent_item)->cg_subsys;
        BUG_ON(!subsys);
@@ -795,50 +1373,87 @@ static int configfs_rmdir(struct inode *dir, struct dentry *dentry)
                return -EINVAL;
        }
 
-       ret = configfs_detach_prep(dentry);
-       if (ret) {
-               configfs_detach_rollback(dentry);
-               config_item_put(parent_item);
-               return ret;
-       }
+       /* configfs_mkdir() shouldn't have allowed this */
+       BUG_ON(!subsys->su_group.cg_item.ci_type);
+       subsys_owner = subsys->su_group.cg_item.ci_type->ct_owner;
+
+       /*
+        * Ensure that no racing symlink() will make detach_prep() fail while
+        * the new link is temporarily attached
+        */
+       do {
+               struct mutex *wait_mutex;
+
+               mutex_lock(&configfs_symlink_mutex);
+               spin_lock(&configfs_dirent_lock);
+               /*
+                * Here's where we check for dependents.  We're protected by
+                * configfs_dirent_lock.
+                * If no dependent, atomically tag the item as dropping.
+                */
+               ret = sd->s_dependent_count ? -EBUSY : 0;
+               if (!ret) {
+                       ret = configfs_detach_prep(dentry, &wait_mutex);
+                       if (ret)
+                               configfs_detach_rollback(dentry);
+               }
+               spin_unlock(&configfs_dirent_lock);
+               mutex_unlock(&configfs_symlink_mutex);
 
+               if (ret) {
+                       if (ret != -EAGAIN) {
+                               config_item_put(parent_item);
+                               return ret;
+                       }
+
+                       /* Wait until the racing operation terminates */
+                       mutex_lock(wait_mutex);
+                       mutex_unlock(wait_mutex);
+               }
+       } while (ret == -EAGAIN);
+
+       /* Get a working ref for the duration of this function */
        item = configfs_get_config_item(dentry);
 
        /* Drop reference from above, item already holds one. */
        config_item_put(parent_item);
 
        if (item->ci_type)
-               owner = item->ci_type->ct_owner;
+               dead_item_owner = item->ci_type->ct_owner;
 
        if (sd->s_type & CONFIGFS_USET_DIR) {
                configfs_detach_group(item);
 
-               down(&subsys->su_sem);
+               mutex_lock(&subsys->su_mutex);
+               client_disconnect_notify(parent_item, item);
                unlink_group(to_config_group(item));
        } else {
                configfs_detach_item(item);
 
-               down(&subsys->su_sem);
+               mutex_lock(&subsys->su_mutex);
+               client_disconnect_notify(parent_item, item);
                unlink_obj(item);
        }
 
        client_drop_item(parent_item, item);
-       up(&subsys->su_sem);
+       mutex_unlock(&subsys->su_mutex);
 
        /* Drop our reference from above */
        config_item_put(item);
 
-       module_put(owner);
+       module_put(dead_item_owner);
+       module_put(subsys_owner);
 
        return 0;
 }
 
-struct inode_operations configfs_dir_inode_operations = {
+const struct inode_operations configfs_dir_inode_operations = {
        .mkdir          = configfs_mkdir,
        .rmdir          = configfs_rmdir,
        .symlink        = configfs_symlink,
        .unlink         = configfs_unlink,
        .lookup         = configfs_lookup,
+       .setattr        = configfs_setattr,
 };
 
 #if 0
@@ -856,11 +1471,11 @@ int configfs_rename_dir(struct config_item * item, const char *new_name)
        down_write(&configfs_rename_sem);
        parent = item->parent->dentry;
 
-       down(&parent->d_inode->i_sem);
+       mutex_lock(&parent->d_inode->i_mutex);
 
        new_dentry = lookup_one_len(new_name, parent, strlen(new_name));
        if (!IS_ERR(new_dentry)) {
-               if (!new_dentry->d_inode) {
+               if (!new_dentry->d_inode) {
                        error = config_item_set_name(item, "%s", new_name);
                        if (!error) {
                                d_add(new_dentry, NULL);
@@ -872,7 +1487,7 @@ int configfs_rename_dir(struct config_item * item, const char *new_name)
                        error = -EEXIST;
                dput(new_dentry);
        }
-       up(&parent->d_inode->i_sem);
+       mutex_unlock(&parent->d_inode->i_mutex);
        up_write(&configfs_rename_sem);
 
        return error;
@@ -881,25 +1496,38 @@ int configfs_rename_dir(struct config_item * item, const char *new_name)
 
 static int configfs_dir_open(struct inode *inode, struct file *file)
 {
-       struct dentry * dentry = file->f_dentry;
+       struct dentry * dentry = file->f_path.dentry;
        struct configfs_dirent * parent_sd = dentry->d_fsdata;
+       int err;
 
-       down(&dentry->d_inode->i_sem);
-       file->private_data = configfs_new_dirent(parent_sd, NULL);
-       up(&dentry->d_inode->i_sem);
-
-       return file->private_data ? 0 : -ENOMEM;
+       mutex_lock(&dentry->d_inode->i_mutex);
+       /*
+        * Fake invisibility if dir belongs to a group/default groups hierarchy
+        * being attached
+        */
+       err = -ENOENT;
+       if (configfs_dirent_is_ready(parent_sd)) {
+               file->private_data = configfs_new_dirent(parent_sd, NULL, 0);
+               if (IS_ERR(file->private_data))
+                       err = PTR_ERR(file->private_data);
+               else
+                       err = 0;
+       }
+       mutex_unlock(&dentry->d_inode->i_mutex);
 
+       return err;
 }
 
 static int configfs_dir_close(struct inode *inode, struct file *file)
 {
-       struct dentry * dentry = file->f_dentry;
+       struct dentry * dentry = file->f_path.dentry;
        struct configfs_dirent * cursor = file->private_data;
 
-       down(&dentry->d_inode->i_sem);
+       mutex_lock(&dentry->d_inode->i_mutex);
+       spin_lock(&configfs_dirent_lock);
        list_del_init(&cursor->s_sibling);
-       up(&dentry->d_inode->i_sem);
+       spin_unlock(&configfs_dirent_lock);
+       mutex_unlock(&dentry->d_inode->i_mutex);
 
        release_configfs_dirent(cursor);
 
@@ -914,7 +1542,7 @@ static inline unsigned char dt_type(struct configfs_dirent *sd)
 
 static int configfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
 {
-       struct dentry *dentry = filp->f_dentry;
+       struct dentry *dentry = filp->f_path.dentry;
        struct configfs_dirent * parent_sd = dentry->d_fsdata;
        struct configfs_dirent *cursor = filp->private_data;
        struct list_head *p, *q = &cursor->s_sibling;
@@ -938,8 +1566,9 @@ static int configfs_readdir(struct file * filp, void * dirent, filldir_t filldir
                        /* fallthrough */
                default:
                        if (filp->f_pos == 2) {
-                               list_del(q);
-                               list_add(q, &parent_sd->s_children);
+                               spin_lock(&configfs_dirent_lock);
+                               list_move(q, &parent_sd->s_children);
+                               spin_unlock(&configfs_dirent_lock);
                        }
                        for (p=q->next; p!= &parent_sd->s_children; p=p->next) {
                                struct configfs_dirent *next;
@@ -962,8 +1591,9 @@ static int configfs_readdir(struct file * filp, void * dirent, filldir_t filldir
                                                 dt_type(next)) < 0)
                                        return 0;
 
-                               list_del(q);
-                               list_add(q, p);
+                               spin_lock(&configfs_dirent_lock);
+                               list_move(q, p);
+                               spin_unlock(&configfs_dirent_lock);
                                p = q;
                                filp->f_pos++;
                        }
@@ -973,9 +1603,9 @@ static int configfs_readdir(struct file * filp, void * dirent, filldir_t filldir
 
 static loff_t configfs_dir_lseek(struct file * file, loff_t offset, int origin)
 {
-       struct dentry * dentry = file->f_dentry;
+       struct dentry * dentry = file->f_path.dentry;
 
-       down(&dentry->d_inode->i_sem);
+       mutex_lock(&dentry->d_inode->i_mutex);
        switch (origin) {
                case 1:
                        offset += file->f_pos;
@@ -983,7 +1613,7 @@ static loff_t configfs_dir_lseek(struct file * file, loff_t offset, int origin)
                        if (offset >= 0)
                                break;
                default:
-                       up(&file->f_dentry->d_inode->i_sem);
+                       mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
                        return -EINVAL;
        }
        if (offset != file->f_pos) {
@@ -994,6 +1624,7 @@ static loff_t configfs_dir_lseek(struct file * file, loff_t offset, int origin)
                        struct list_head *p;
                        loff_t n = file->f_pos - 2;
 
+                       spin_lock(&configfs_dirent_lock);
                        list_del(&cursor->s_sibling);
                        p = sd->s_children.next;
                        while (n && p != &sd->s_children) {
@@ -1005,13 +1636,14 @@ static loff_t configfs_dir_lseek(struct file * file, loff_t offset, int origin)
                                p = p->next;
                        }
                        list_add_tail(&cursor->s_sibling, p);
+                       spin_unlock(&configfs_dirent_lock);
                }
        }
-       up(&dentry->d_inode->i_sem);
+       mutex_unlock(&dentry->d_inode->i_mutex);
        return offset;
 }
 
-struct file_operations configfs_dir_operations = {
+const struct file_operations configfs_dir_operations = {
        .open           = configfs_dir_open,
        .release        = configfs_dir_close,
        .llseek         = configfs_dir_lseek,
@@ -1037,7 +1669,8 @@ int configfs_register_subsystem(struct configfs_subsystem *subsys)
        sd = configfs_sb->s_root->d_fsdata;
        link_group(to_config_group(sd->s_element), group);
 
-       down(&configfs_sb->s_root->d_inode->i_sem);
+       mutex_lock_nested(&configfs_sb->s_root->d_inode->i_mutex,
+                       I_MUTEX_PARENT);
 
        name.name = group->cg_item.ci_name;
        name.len = strlen(name.name);
@@ -1045,25 +1678,26 @@ int configfs_register_subsystem(struct configfs_subsystem *subsys)
 
        err = -ENOMEM;
        dentry = d_alloc(configfs_sb->s_root, &name);
-       if (!dentry)
-               goto out_release;
-
-       d_add(dentry, NULL);
+       if (dentry) {
+               d_add(dentry, NULL);
 
-       err = configfs_attach_group(sd->s_element, &group->cg_item,
-                                   dentry);
-       if (!err)
-               dentry = NULL;
-       else
-               d_delete(dentry);
+               err = configfs_attach_group(sd->s_element, &group->cg_item,
+                                           dentry);
+               if (err) {
+                       d_delete(dentry);
+                       dput(dentry);
+               } else {
+                       spin_lock(&configfs_dirent_lock);
+                       configfs_dir_set_ready(dentry->d_fsdata);
+                       spin_unlock(&configfs_dirent_lock);
+               }
+       }
 
-       up(&configfs_sb->s_root->d_inode->i_sem);
+       mutex_unlock(&configfs_sb->s_root->d_inode->i_mutex);
 
-       if (dentry) {
-           dput(dentry);
-out_release:
-           unlink_group(group);
-           configfs_release_fs();
+       if (err) {
+               unlink_group(group);
+               configfs_release_fs();
        }
 
        return err;
@@ -1079,18 +1713,23 @@ void configfs_unregister_subsystem(struct configfs_subsystem *subsys)
                return;
        }
 
-       down(&configfs_sb->s_root->d_inode->i_sem);
-       down(&dentry->d_inode->i_sem);
-       if (configfs_detach_prep(dentry)) {
+       mutex_lock_nested(&configfs_sb->s_root->d_inode->i_mutex,
+                         I_MUTEX_PARENT);
+       mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_CHILD);
+       mutex_lock(&configfs_symlink_mutex);
+       spin_lock(&configfs_dirent_lock);
+       if (configfs_detach_prep(dentry, NULL)) {
                printk(KERN_ERR "configfs: Tried to unregister non-empty subsystem!\n");
        }
+       spin_unlock(&configfs_dirent_lock);
+       mutex_unlock(&configfs_symlink_mutex);
        configfs_detach_group(&group->cg_item);
        dentry->d_inode->i_flags |= S_DEAD;
-       up(&dentry->d_inode->i_sem);
+       mutex_unlock(&dentry->d_inode->i_mutex);
 
        d_delete(dentry);
 
-       up(&configfs_sb->s_root->d_inode->i_sem);
+       mutex_unlock(&configfs_sb->s_root->d_inode->i_mutex);
 
        dput(dentry);