[PATCH] configfs: Fix failing symlink() making rmdir() fail
authorLouis Rilling <louis.rilling@kerlabs.com>
Fri, 20 Jun 2008 12:09:22 +0000 (14:09 +0200)
committerMark Fasheh <mfasheh@suse.com>
Thu, 31 Jul 2008 23:21:13 +0000 (16:21 -0700)
On a similar pattern as mkdir() vs rmdir(), a failing symlink() may make rmdir()
fail for the symlink's parent and the symlink's target as well.

failing symlink() making target's rmdir() fail:

process 1: process 2:
symlink("A/S" -> "B")
  allow_link()
  create_link()
    attach to "B" links list
rmdir("B")
  detach_prep("B")
    error because of new link
    configfs_create_link("A", "S")
      error (eg -ENOMEM)

failing symlink() making parent's rmdir() fail:

process 1: process 2:
symlink("A/D/S" -> "B")
  allow_link()
  create_link()
    attach to "B" links list
    configfs_create_link("A/D", "S")
      make_dirent("A/D", "S")
rmdir("A")
  detach_prep("A")
    detach_prep("A/D")
      error because of "S"
      create("S")
        error (eg -ENOMEM)

We cannot use the same solution as for mkdir() vs rmdir(), since rmdir() on the
target cannot wait on the i_mutex of the new symlink's parent without risking a
deadlock (with other symlink() or sys_rename()). Instead we define a global
mutex protecting all configfs symlinks attachment, so that rmdir() can avoid the
races above.

Signed-off-by: Louis Rilling <louis.rilling@kerlabs.com>
Signed-off-by: Joel Becker <joel.becker@oracle.com>
Signed-off-by: Mark Fasheh <mfasheh@suse.com>
fs/configfs/configfs_internal.h
fs/configfs/dir.c
fs/configfs/symlink.c

index da015c1..5f61b26 100644 (file)
@@ -51,6 +51,7 @@ struct configfs_dirent {
 #define CONFIGFS_USET_IN_MKDIR 0x0200
 #define CONFIGFS_NOT_PINNED    (CONFIGFS_ITEM_ATTR)
 
+extern struct mutex configfs_symlink_mutex;
 extern spinlock_t configfs_dirent_lock;
 
 extern struct vfsmount * configfs_mount;
index cb5ea44..4e228c8 100644 (file)
@@ -1207,6 +1207,11 @@ static int configfs_rmdir(struct inode *dir, struct dentry *dentry)
                return -EINVAL;
        }
 
+       /*
+        * Ensure that no racing symlink() will make detach_prep() fail while
+        * the new link is temporarily attached
+        */
+       mutex_lock(&configfs_symlink_mutex);
        spin_lock(&configfs_dirent_lock);
        do {
                struct mutex *wait_mutex;
@@ -1215,6 +1220,7 @@ static int configfs_rmdir(struct inode *dir, struct dentry *dentry)
                if (ret) {
                        configfs_detach_rollback(dentry);
                        spin_unlock(&configfs_dirent_lock);
+                       mutex_unlock(&configfs_symlink_mutex);
                        if (ret != -EAGAIN) {
                                config_item_put(parent_item);
                                return ret;
@@ -1224,10 +1230,12 @@ static int configfs_rmdir(struct inode *dir, struct dentry *dentry)
                        mutex_lock(wait_mutex);
                        mutex_unlock(wait_mutex);
 
+                       mutex_lock(&configfs_symlink_mutex);
                        spin_lock(&configfs_dirent_lock);
                }
        } while (ret == -EAGAIN);
        spin_unlock(&configfs_dirent_lock);
+       mutex_unlock(&configfs_symlink_mutex);
 
        /* Get a working ref for the duration of this function */
        item = configfs_get_config_item(dentry);
@@ -1517,11 +1525,13 @@ void configfs_unregister_subsystem(struct configfs_subsystem *subsys)
        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;
        mutex_unlock(&dentry->d_inode->i_mutex);
index c12801a..61a886d 100644 (file)
@@ -31,6 +31,9 @@
 #include <linux/configfs.h>
 #include "configfs_internal.h"
 
+/* Protects attachments of new symlinks */
+DEFINE_MUTEX(configfs_symlink_mutex);
+
 static int item_depth(struct config_item * item)
 {
        struct config_item * p = item;
@@ -147,7 +150,9 @@ int configfs_symlink(struct inode *dir, struct dentry *dentry, const char *symna
 
        ret = type->ct_item_ops->allow_link(parent_item, target_item);
        if (!ret) {
+               mutex_lock(&configfs_symlink_mutex);
                ret = create_link(parent_item, target_item, dentry);
+               mutex_unlock(&configfs_symlink_mutex);
                if (ret && type->ct_item_ops->drop_link)
                        type->ct_item_ops->drop_link(parent_item,
                                                     target_item);