Unionfs: implement lockdep classes
authorErez Zadok <ezk@cs.sunysb.edu>
Thu, 10 Jan 2008 12:10:25 +0000 (07:10 -0500)
committerRachita Kothiyal <rachita@dewey.fsl.cs.sunysb.edu>
Thu, 1 May 2008 23:03:32 +0000 (19:03 -0400)
Lockdep fixes.  Support locking order/classes (e.g., parent -> child ->
whiteout).  Remove locking from create_parents: it's enough to just dget the
dentries in question.  Move parent locking to from lookup_backend to caller,
unionfs_lookup.

Signed-off-by: Erez Zadok <ezk@cs.sunysb.edu>
18 files changed:
fs/unionfs/commonfops.c
fs/unionfs/copyup.c
fs/unionfs/dentry.c
fs/unionfs/dirfops.c
fs/unionfs/export.c
fs/unionfs/fanout.h
fs/unionfs/file.c
fs/unionfs/inode.c
fs/unionfs/lookup.c
fs/unionfs/main.c
fs/unionfs/mmap.c
fs/unionfs/odf.c
fs/unionfs/rename.c
fs/unionfs/subr.c
fs/unionfs/super.c
fs/unionfs/union.h
fs/unionfs/unlink.c
fs/unionfs/xattr.c

index 26573f300606ca8763ac1a3d77182682ac60a957..55f016ee102a1f74e8c7755cd39cda2d7a2e7760 100644 (file)
@@ -378,7 +378,7 @@ int unionfs_file_revalidate(struct file *file, bool willwrite)
        int err = 0;
 
        dentry = file->f_path.dentry;
-       unionfs_lock_dentry(dentry);
+       unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
        sb = dentry->d_sb;
 
        /*
@@ -601,7 +601,7 @@ int unionfs_open(struct inode *inode, struct file *file)
        int bindex = 0, bstart = 0, bend = 0;
        int size;
 
-       unionfs_read_lock(inode->i_sb);
+       unionfs_read_lock(inode->i_sb, UNIONFS_SMUTEX_PARENT);
 
        file->private_data =
                kzalloc(sizeof(struct unionfs_file_info), GFP_KERNEL);
@@ -638,7 +638,7 @@ int unionfs_open(struct inode *inode, struct file *file)
                err = -ESTALE;
                goto out;
        }
-       unionfs_lock_dentry(dentry);
+       unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
 
        bstart = fbstart(file) = dbstart(dentry);
        bend = fbend(file) = dbend(dentry);
@@ -701,7 +701,7 @@ int unionfs_file_release(struct inode *inode, struct file *file)
        int bindex, bstart, bend;
        int fgen, err = 0;
 
-       unionfs_read_lock(sb);
+       unionfs_read_lock(sb, UNIONFS_SMUTEX_PARENT);
        /*
         * Yes, we have to revalidate this file even if it's being released.
         * This is important for open-but-unlinked files, as well as mmap
@@ -719,7 +719,7 @@ int unionfs_file_release(struct inode *inode, struct file *file)
        bstart = fbstart(file);
        bend = fbend(file);
 
-       unionfs_lock_dentry(dentry);
+       unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
        for (bindex = bstart; bindex <= bend; bindex++) {
                lower_file = unionfs_lower_file_idx(file, bindex);
 
@@ -788,7 +788,7 @@ static int unionfs_ioctl_queryfile(struct file *file, unsigned int cmd,
        struct vfsmount *mnt;
 
        dentry = file->f_path.dentry;
-       unionfs_lock_dentry(dentry);
+       unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
        orig_bstart = dbstart(dentry);
        orig_bend = dbend(dentry);
        err = unionfs_partial_lookup(dentry);
@@ -838,7 +838,7 @@ long unionfs_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 {
        long err;
 
-       unionfs_read_lock(file->f_path.dentry->d_sb);
+       unionfs_read_lock(file->f_path.dentry->d_sb, UNIONFS_SMUTEX_PARENT);
 
        err = unionfs_file_revalidate(file, true);
        if (unlikely(err))
@@ -877,7 +877,7 @@ int unionfs_flush(struct file *file, fl_owner_t id)
        struct dentry *dentry = file->f_path.dentry;
        int bindex, bstart, bend;
 
-       unionfs_read_lock(dentry->d_sb);
+       unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
 
        err = unionfs_file_revalidate(file, true);
        if (unlikely(err))
index 33c2d33c2d12b764a8c1bb1546ae74691334b1d6..3d211576cf240a15702dd5b820aa9fbe6942bdc6 100644 (file)
@@ -723,7 +723,7 @@ struct dentry *create_parents(struct inode *dir, struct dentry *dentry,
 
                /* find the parent directory dentry in unionfs */
                parent_dentry = child_dentry->d_parent;
-               unionfs_lock_dentry(parent_dentry);
+               dget(parent_dentry);
 
                /* find out the lower_parent_dentry in the given branch */
                lower_parent_dentry =
@@ -758,7 +758,7 @@ struct dentry *create_parents(struct inode *dir, struct dentry *dentry,
 begin:
        /* get lower parent dir in the current branch */
        lower_parent_dentry = unionfs_lower_dentry_idx(parent_dentry, bindex);
-       unionfs_unlock_dentry(parent_dentry);
+       dput(parent_dentry);
 
        /* init the values to lookup */
        childname = child_dentry->d_name.name;
@@ -849,7 +849,7 @@ out:
        /* cleanup any leftover locks from the do/while loop above */
        if (IS_ERR(lower_dentry))
                while (count)
-                       unionfs_unlock_dentry(path[count--]);
+                       dput(path[count--]);
        kfree(path);
        return lower_dentry;
 }
index 03f2cb8121c5c98b6f0f37a01711c758efd444d1..b3120637d16b99c1f40df7f85d355ce3a402c5cd 100644 (file)
@@ -369,7 +369,7 @@ bool __unionfs_d_revalidate_chain(struct dentry *dentry, struct nameidata *nd,
         * to child order.
         */
        for (i = 0; i < chain_len; i++) {
-               unionfs_lock_dentry(chain[i]);
+               unionfs_lock_dentry(chain[i], UNIONFS_DMUTEX_REVAL+i);
                saved_bstart = dbstart(chain[i]);
                saved_bend = dbend(chain[i]);
                sbgen = atomic_read(&UNIONFS_SB(dentry->d_sb)->generation);
@@ -443,9 +443,9 @@ static int unionfs_d_revalidate(struct dentry *dentry, struct nameidata *nd)
 {
        int err;
 
-       unionfs_read_lock(dentry->d_sb);
+       unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
 
-       unionfs_lock_dentry(dentry);
+       unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
        err = __unionfs_d_revalidate_chain(dentry, nd, false);
        if (likely(err > 0)) { /* true==1: dentry is valid */
                unionfs_check_dentry(dentry);
@@ -466,7 +466,7 @@ static void unionfs_d_release(struct dentry *dentry)
 {
        int bindex, bstart, bend;
 
-       unionfs_read_lock(dentry->d_sb);
+       unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
 
        unionfs_check_dentry(dentry);
        /* this could be a negative dentry, so check first */
index 2ac01d364b075f0c679a458a9ed80a782d4a9ef7..7f6ff5fec57bc0da7e14d9a279a9a82113a622e8 100644 (file)
@@ -32,7 +32,7 @@ static int unionfs_readdir(struct file *file, void *dirent, filldir_t filldir)
        int namelen;
        unsigned int d_type;
 
-       unionfs_read_lock(file->f_path.dentry->d_sb);
+       unionfs_read_lock(file->f_path.dentry->d_sb, UNIONFS_SMUTEX_PARENT);
 
        err = unionfs_file_revalidate(file, false);
        if (unlikely(err))
@@ -56,7 +56,7 @@ static int unionfs_readdir(struct file *file, void *dirent, filldir_t filldir)
                 * odf cache dir calls partial lookup which expects a locked
                 * dentry
                 */
-               unionfs_lock_dentry(file->f_path.dentry);
+               unionfs_lock_dentry(file->f_path.dentry, UNIONFS_DMUTEX_CHILD);
                err = odf_cache_dir(file->f_path.dentry, odf_cache,
                                    &file->f_path.dentry->d_inode->i_mtime);
                unionfs_unlock_dentry(file->f_path.dentry);
@@ -136,7 +136,7 @@ static loff_t unionfs_dir_llseek(struct file *file, loff_t offset, int origin)
 {
        loff_t err;
 
-       unionfs_read_lock(file->f_path.dentry->d_sb);
+       unionfs_read_lock(file->f_path.dentry->d_sb, UNIONFS_SMUTEX_PARENT);
 
        err = unionfs_file_revalidate(file, false);
        if (unlikely(err))
index c904753003e08dd1962b58eae16e9f2497446b45..fd82a33c9c33fbc77a010bd4e577161a34b07ec6 100644 (file)
@@ -253,7 +253,7 @@ static struct dentry *unionfs_export_iget(struct super_block *sb,
        }
 
        if (!UNIONFS_D(result))
-               err = new_dentry_private_data(result);
+               err = new_dentry_private_data(result, UNIONFS_DMUTEX_CHILD);
        if (err) {
                printk(KERN_INFO "unionfs_export_iget: Not enough memory to "
                                        "allocate dentry's private data\n");
index 23272389bf5ba0ee6e0a254c0a6082e982174141..590cd9ea4c3a8d56c05ed245653fb4be9fe40657 100644 (file)
@@ -277,10 +277,20 @@ static inline struct vfsmount *unionfs_lower_mnt(const struct dentry *dent)
 }
 
 /* Macros for locking a dentry. */
-static inline void unionfs_lock_dentry(struct dentry *d)
+enum unionfs_dentry_lock_class {
+       UNIONFS_DMUTEX_NORMAL,
+       UNIONFS_DMUTEX_ROOT,
+       UNIONFS_DMUTEX_PARENT,
+       UNIONFS_DMUTEX_CHILD,
+       UNIONFS_DMUTEX_WHITEOUT,
+       UNIONFS_DMUTEX_REVAL,   /* for file/dentry revalidate */
+};
+
+static inline void unionfs_lock_dentry(struct dentry *d,
+                                      unsigned int subclass)
 {
        BUG_ON(!d);
-       mutex_lock(&UNIONFS_D(d)->lock);
+       mutex_lock_nested(&UNIONFS_D(d)->lock, subclass);
 }
 
 static inline void unionfs_unlock_dentry(struct dentry *d)
index 94784c3ce78ba65ffdd61b04f529eefbcde0930b..b63204285e9b6c4978fc3c5ab8ec4da7849c6ccb 100644 (file)
@@ -30,7 +30,7 @@ static int unionfs_mmap(struct file *file, struct vm_area_struct *vma)
        bool willwrite;
        struct file *lower_file;
 
-       unionfs_read_lock(file->f_path.dentry->d_sb);
+       unionfs_read_lock(file->f_path.dentry->d_sb, UNIONFS_SMUTEX_PARENT);
 
        /* This might be deferred to mmap's writepage */
        willwrite = ((vma->vm_flags | VM_SHARED | VM_WRITE) == vma->vm_flags);
@@ -80,7 +80,7 @@ int unionfs_fsync(struct file *file, struct dentry *dentry, int datasync)
        struct inode *lower_inode, *inode;
        int err = -EINVAL;
 
-       unionfs_read_lock(file->f_path.dentry->d_sb);
+       unionfs_read_lock(file->f_path.dentry->d_sb, UNIONFS_SMUTEX_PARENT);
        err = unionfs_file_revalidate(file, true);
        if (unlikely(err))
                goto out;
@@ -128,7 +128,7 @@ int unionfs_fasync(int fd, struct file *file, int flag)
        struct inode *lower_inode, *inode;
        int err = 0;
 
-       unionfs_read_lock(file->f_path.dentry->d_sb);
+       unionfs_read_lock(file->f_path.dentry->d_sb, UNIONFS_SMUTEX_PARENT);
        err = unionfs_file_revalidate(file, true);
        if (unlikely(err))
                goto out;
index ac907565949107dc823b8a7064972b1d768a0776..12754a1d1d046992731350c981583a523436cd33 100644 (file)
@@ -28,8 +28,8 @@ static int unionfs_create(struct inode *parent, struct dentry *dentry,
        int valid = 0;
        struct nameidata lower_nd;
 
-       unionfs_read_lock(dentry->d_sb);
-       unionfs_lock_dentry(dentry);
+       unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
+       unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
 
        valid = __unionfs_d_revalidate_chain(dentry, nd, false);
        /*
@@ -144,7 +144,9 @@ static struct dentry *unionfs_lookup(struct inode *parent,
        struct path path_save;
        struct dentry *ret;
 
-       unionfs_read_lock(dentry->d_sb);
+       unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
+       if (dentry != dentry->d_parent)
+               unionfs_lock_dentry(dentry->d_parent, UNIONFS_DMUTEX_PARENT);
 
        /* save the dentry & vfsmnt from namei */
        if (nd) {
@@ -176,6 +178,9 @@ static struct dentry *unionfs_lookup(struct inode *parent,
        unionfs_check_nd(nd);
        if (!IS_ERR(ret))
                unionfs_unlock_dentry(dentry);
+
+       if (dentry != dentry->d_parent)
+               unionfs_unlock_dentry(dentry->d_parent);
        unionfs_read_unlock(dentry->d_sb);
 
        return ret;
@@ -189,7 +194,7 @@ static int unionfs_link(struct dentry *old_dentry, struct inode *dir,
        struct dentry *lower_new_dentry = NULL;
        struct dentry *lower_dir_dentry = NULL;
 
-       unionfs_read_lock(old_dentry->d_sb);
+       unionfs_read_lock(old_dentry->d_sb, UNIONFS_SMUTEX_CHILD);
        unionfs_double_lock_dentry(new_dentry, old_dentry);
 
        if (unlikely(!__unionfs_d_revalidate_chain(old_dentry, NULL, false))) {
@@ -342,8 +347,8 @@ static int unionfs_symlink(struct inode *dir, struct dentry *dentry,
        umode_t mode;
        int bstart;
 
-       unionfs_read_lock(dentry->d_sb);
-       unionfs_lock_dentry(dentry);
+       unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
+       unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
 
        if (unlikely(dentry->d_inode &&
                     !__unionfs_d_revalidate_chain(dentry, NULL, false))) {
@@ -440,8 +445,8 @@ static int unionfs_mkdir(struct inode *parent, struct dentry *dentry, int mode)
        char *name = NULL;
        int i, bend, bindex;
 
-       unionfs_read_lock(dentry->d_sb);
-       unionfs_lock_dentry(dentry);
+       unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
+       unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
 
        if (unlikely(dentry->d_inode &&
                     !__unionfs_d_revalidate_chain(dentry, NULL, false))) {
@@ -550,8 +555,8 @@ static int unionfs_mknod(struct inode *dir, struct dentry *dentry, int mode,
        struct dentry *lower_parent_dentry = NULL;
        int bstart;
 
-       unionfs_read_lock(dentry->d_sb);
-       unionfs_lock_dentry(dentry);
+       unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
+       unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
 
        if (unlikely(dentry->d_inode &&
                     !__unionfs_d_revalidate_chain(dentry, NULL, false))) {
@@ -640,8 +645,8 @@ static int unionfs_readlink(struct dentry *dentry, char __user *buf,
        int err;
        struct dentry *lower_dentry;
 
-       unionfs_read_lock(dentry->d_sb);
-       unionfs_lock_dentry(dentry);
+       unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
+       unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
 
        if (unlikely(!__unionfs_d_revalidate_chain(dentry, NULL, false))) {
                err = -ESTALE;
@@ -718,9 +723,9 @@ out:
 static void unionfs_put_link(struct dentry *dentry, struct nameidata *nd,
                             void *cookie)
 {
-       unionfs_read_lock(dentry->d_sb);
+       unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
 
-       unionfs_lock_dentry(dentry);
+       unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
        if (unlikely(!__unionfs_d_revalidate_chain(dentry, nd, false)))
                printk(KERN_ERR
                       "unionfs: put_link failed to revalidate dentry\n");
@@ -828,8 +833,8 @@ static int unionfs_setattr(struct dentry *dentry, struct iattr *ia)
        int bstart, bend, bindex;
        loff_t size;
 
-       unionfs_read_lock(dentry->d_sb);
-       unionfs_lock_dentry(dentry);
+       unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
+       unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
 
        if (unlikely(!__unionfs_d_revalidate_chain(dentry, NULL, false))) {
                err = -ESTALE;
index 117adc75f6d2a0b8904a4100108542e29d98052b..b5a390f8c5b22f728931fbf1742e04f28c1eb015 100644 (file)
@@ -47,7 +47,6 @@ struct dentry *unionfs_lookup_backend(struct dentry *dentry,
        struct dentry *first_dentry = NULL;
        struct dentry *first_lower_dentry = NULL;
        struct vfsmount *first_lower_mnt = NULL;
-       int locked_parent = 0;
        const char *name;
        int namelen;
        struct nameidata new_nd;
@@ -67,7 +66,7 @@ struct dentry *unionfs_lookup_backend(struct dentry *dentry,
        case INTERPOSE_PARTIAL:
                break;
        case INTERPOSE_LOOKUP:
-               err = new_dentry_private_data(dentry);
+               err = new_dentry_private_data(dentry, UNIONFS_DMUTEX_CHILD);
                if (unlikely(err))
                        goto out;
                break;
@@ -84,10 +83,7 @@ struct dentry *unionfs_lookup_backend(struct dentry *dentry,
 
        parent_dentry = dget_parent(dentry);
        /* We never partial lookup the root directory. */
-       if (parent_dentry != dentry) {
-               unionfs_lock_dentry(parent_dentry);
-               locked_parent = 1;
-       } else {
+       if (parent_dentry == dentry) {
                dput(parent_dentry);
                parent_dentry = NULL;
                goto out;
@@ -369,8 +365,6 @@ out:
                        UNIONFS_I(dentry->d_inode)->stale = 1;
                }
        }
-       if (locked_parent)
-               unionfs_unlock_dentry(parent_dentry);
        dput(parent_dentry);
        if (err && (lookupmode == INTERPOSE_LOOKUP))
                unionfs_unlock_dentry(dentry);
@@ -384,6 +378,7 @@ out:
 
 /*
  * This is a utility function that fills in a unionfs dentry.
+ * Caller must lock this dentry with unionfs_lock_dentry.
  *
  * Returns: 0 (ok), or -ERRNO if an error occurred.
  */
@@ -485,7 +480,7 @@ static int realloc_dentry_private_data(struct dentry *dentry)
 }
 
 /* allocate new dentry private data */
-int new_dentry_private_data(struct dentry *dentry)
+int new_dentry_private_data(struct dentry *dentry, int subclass)
 {
        struct unionfs_dentry_info *info = UNIONFS_D(dentry);
 
@@ -496,7 +491,7 @@ int new_dentry_private_data(struct dentry *dentry)
                return -ENOMEM;
 
        mutex_init(&info->lock);
-       mutex_lock(&info->lock);
+       mutex_lock_nested(&info->lock, subclass);
 
        info->lower_paths = NULL;
 
index e0ebca8cd68757bbbc8e8e7eb9a8b4dda5d554c9..beda8afe3c335fb8d00fb1a00831ede03243cbf1 100644 (file)
@@ -751,7 +751,7 @@ static int unionfs_read_super(struct super_block *sb, void *raw_data,
 
        /* link the upper and lower dentries */
        sb->s_root->d_fsdata = NULL;
-       err = new_dentry_private_data(sb->s_root);
+       err = new_dentry_private_data(sb->s_root, UNIONFS_DMUTEX_ROOT);
        if (unlikely(err))
                goto out_freedpd;
 
index aad2137e18f37978bc01d4d0bec3e1e79509343c..a0e654bef667eefa167bc1c1300fe43fa0bba196 100644 (file)
@@ -152,7 +152,7 @@ static int unionfs_readpage(struct file *file, struct page *page)
        char *page_data = NULL;
        mode_t orig_mode;
 
-       unionfs_read_lock(file->f_path.dentry->d_sb);
+       unionfs_read_lock(file->f_path.dentry->d_sb, UNIONFS_SMUTEX_PARENT);
        err = unionfs_file_revalidate(file, false);
        if (unlikely(err))
                goto out;
@@ -245,7 +245,7 @@ static int unionfs_commit_write(struct file *file, struct page *page,
 
        BUG_ON(file == NULL);
 
-       unionfs_read_lock(file->f_path.dentry->d_sb);
+       unionfs_read_lock(file->f_path.dentry->d_sb, UNIONFS_SMUTEX_PARENT);
        err = unionfs_file_revalidate(file, true);
        if (unlikely(err))
                goto out;
index 7873df77a1eadd1ebf097435b16a1a194beeced4..c161ec8b193282640c68c5ee6bcdd3a1d7126826 100644 (file)
@@ -898,7 +898,7 @@ skip:
                lower_dentry = create_parents(dentry->d_parent->d_inode,
                                              dentry, dentry->d_name.name,
                                              branch_dst);
-               unionfs_lock_dentry(dentry->d_parent);
+               unionfs_lock_dentry(dentry->d_parent, UNIONFS_DMUTEX_PARENT);
 
                if (!lower_dentry || IS_ERR(lower_dentry)) {
                        if (IS_ERR(lower_dentry))
index e0eacf13005c2814d37e4dd50878f02b5fa46a3d..0b3d94df4b450c343e278c2e715045660c5c7b63 100644 (file)
@@ -348,7 +348,7 @@ int unionfs_rename(struct inode *old_dir, struct dentry *old_dentry,
        int err = 0;
        struct dentry *lower;
 
-       unionfs_read_lock(old_dentry->d_sb);
+       unionfs_read_lock(old_dentry->d_sb, UNIONFS_SMUTEX_CHILD);
        unionfs_double_lock_dentry(old_dentry, new_dentry);
 
        if (unlikely(!__unionfs_d_revalidate_chain(old_dentry, NULL, false))) {
index f48c5ec9ab1ab31a99e0ff6e3ced90588fc2f78c..c864f6696246575adb6e52bb8a2091abac01c77b 100644 (file)
@@ -30,7 +30,7 @@ int unionfs_refresh_lower_dentry(struct dentry *dentry, int bindex)
 
        verify_locked(dentry);
 
-       unionfs_lock_dentry(dentry->d_parent);
+       unionfs_lock_dentry(dentry->d_parent, UNIONFS_DMUTEX_CHILD);
        lower_parent = unionfs_lower_dentry_idx(dentry->d_parent, bindex);
        unionfs_unlock_dentry(dentry->d_parent);
 
index baa2e81b05d27d26eb573f885a894a319aa01f67..cb2618f6a14e4f65677aa5915bb0dcfb4dbbd433 100644 (file)
@@ -134,8 +134,8 @@ static int unionfs_statfs(struct dentry *dentry, struct kstatfs *buf)
 
        sb = dentry->d_sb;
 
-       unionfs_read_lock(sb);
-       unionfs_lock_dentry(dentry);
+       unionfs_read_lock(sb, UNIONFS_SMUTEX_CHILD);
+       unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
 
        if (unlikely(!__unionfs_d_revalidate_chain(dentry, NULL, false))) {
                err = -ESTALE;
@@ -948,7 +948,7 @@ static void unionfs_umount_begin(struct vfsmount *mnt, int flags)
 
        sb = mnt->mnt_sb;
 
-       unionfs_read_lock(sb);
+       unionfs_read_lock(sb, UNIONFS_SMUTEX_CHILD);
 
        bstart = sbstart(sb);
        bend = sbend(sb);
@@ -973,9 +973,9 @@ static int unionfs_show_options(struct seq_file *m, struct vfsmount *mnt)
        int bindex, bstart, bend;
        int perms;
 
-       unionfs_read_lock(sb);
+       unionfs_read_lock(sb, UNIONFS_SMUTEX_CHILD);
 
-       unionfs_lock_dentry(sb->s_root);
+       unionfs_lock_dentry(sb->s_root, UNIONFS_DMUTEX_CHILD);
 
        tmp_page = (char *) __get_free_page(GFP_KERNEL);
        if (unlikely(!tmp_page)) {
index e862a29acb4204c2f68404c71cbec093a92af4ca..cfdb36c7b0f345b4d2b54d11cb751bf56151297f 100644 (file)
@@ -267,12 +267,18 @@ extern struct filldir_node *find_filldir_node(
 #define MAXRDCOOKIE (0xfff)
 #define DIREOF (0xffffff)
 
-static inline void unionfs_read_lock(struct super_block *sb)
+/* Macros for locking a super_block. */
+enum unionfs_super_lock_class {
+       UNIONFS_SMUTEX_NORMAL,
+       UNIONFS_SMUTEX_PARENT,  /* when locking on behalf of file */
+       UNIONFS_SMUTEX_CHILD,   /* when locking on behalf of dentry */
+};
+static inline void unionfs_read_lock(struct super_block *sb, int subclass)
 {
        if (UNIONFS_SB(sb)->write_lock_owner &&
            UNIONFS_SB(sb)->write_lock_owner == current->pid)
                return;
-       down_read(&UNIONFS_SB(sb)->rwsem);
+       down_read_nested(&UNIONFS_SB(sb)->rwsem, subclass);
 }
 static inline void unionfs_read_unlock(struct super_block *sb)
 {
@@ -295,16 +301,17 @@ static inline void unionfs_write_unlock(struct super_block *sb)
 static inline void unionfs_double_lock_dentry(struct dentry *d1,
                                              struct dentry *d2)
 {
-       if (d2 < d1) {
-               struct dentry *tmp = d1;
-               d1 = d2;
-               d2 = tmp;
+       BUG_ON(d1 == d2);
+       if (d1 < d2) {
+               unionfs_lock_dentry(d1, UNIONFS_DMUTEX_PARENT);
+               unionfs_lock_dentry(d2, UNIONFS_DMUTEX_CHILD);
+       } else {
+               unionfs_lock_dentry(d2, UNIONFS_DMUTEX_PARENT);
+               unionfs_lock_dentry(d1, UNIONFS_DMUTEX_CHILD);
        }
-       unionfs_lock_dentry(d1);
-       unionfs_lock_dentry(d2);
 }
 
-extern int new_dentry_private_data(struct dentry *dentry);
+extern int new_dentry_private_data(struct dentry *dentry, int subclass);
 extern void free_dentry_private_data(struct dentry *dentry);
 extern void update_bstart(struct dentry *dentry);
 extern int init_lower_nd(struct nameidata *nd, unsigned int flags);
@@ -477,15 +484,18 @@ static inline int is_robranch(const struct dentry *dentry)
 extern int check_branch(struct nameidata *nd);
 extern int parse_branch_mode(const char *name, int *perms);
 
-/*
- * These two functions are here because it is kind of daft to copy and paste
- * the contents of the two functions to 32+ places in unionfs
- */
+/* locking helpers */
 static inline struct dentry *lock_parent(struct dentry *dentry)
+{
+       struct dentry *dir = dget(dentry->d_parent);
+       mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
+       return dir;
+}
+static inline struct dentry *lock_parent_wh(struct dentry *dentry)
 {
        struct dentry *dir = dget(dentry->d_parent);
 
-       mutex_lock(&dir->d_inode->i_mutex);
+       mutex_lock_nested(&dir->d_inode->i_mutex, UNIONFS_DMUTEX_WHITEOUT);
        return dir;
 }
 
index b00971109df0564e6b8f720d1d852f7f4421570a..f16d32cf553fb57be9144d1550b57b248f9eb3f3 100644 (file)
@@ -102,8 +102,8 @@ int unionfs_unlink(struct inode *dir, struct dentry *dentry)
 {
        int err = 0;
 
-       unionfs_read_lock(dentry->d_sb);
-       unionfs_lock_dentry(dentry);
+       unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
+       unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
 
        if (unlikely(!__unionfs_d_revalidate_chain(dentry, NULL, false))) {
                err = -ESTALE;
@@ -223,8 +223,8 @@ int unionfs_rmdir(struct inode *dir, struct dentry *dentry)
        struct unionfs_dir_state *namelist = NULL;
        int dstart, dend;
 
-       unionfs_read_lock(dentry->d_sb);
-       unionfs_lock_dentry(dentry);
+       unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
+       unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
 
        dstart = dbstart(dentry);
        dend = dbend(dentry);
index 00c6d0dc0e833471335057fef52bce42972c5a6b..8001c651b9f511f21b9fba53ddfd33c19a7823fd 100644 (file)
@@ -45,8 +45,8 @@ ssize_t unionfs_getxattr(struct dentry *dentry, const char *name, void *value,
        struct dentry *lower_dentry = NULL;
        int err = -EOPNOTSUPP;
 
-       unionfs_read_lock(dentry->d_sb);
-       unionfs_lock_dentry(dentry);
+       unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
+       unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
 
        if (unlikely(!__unionfs_d_revalidate_chain(dentry, NULL, false))) {
                err = -ESTALE;
@@ -74,8 +74,8 @@ int unionfs_setxattr(struct dentry *dentry, const char *name,
        struct dentry *lower_dentry = NULL;
        int err = -EOPNOTSUPP;
 
-       unionfs_read_lock(dentry->d_sb);
-       unionfs_lock_dentry(dentry);
+       unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
+       unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
 
        if (unlikely(!__unionfs_d_revalidate_chain(dentry, NULL, false))) {
                err = -ESTALE;
@@ -103,8 +103,8 @@ int unionfs_removexattr(struct dentry *dentry, const char *name)
        struct dentry *lower_dentry = NULL;
        int err = -EOPNOTSUPP;
 
-       unionfs_read_lock(dentry->d_sb);
-       unionfs_lock_dentry(dentry);
+       unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
+       unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
 
        if (unlikely(!__unionfs_d_revalidate_chain(dentry, NULL, false))) {
                err = -ESTALE;
@@ -132,8 +132,8 @@ ssize_t unionfs_listxattr(struct dentry *dentry, char *list, size_t size)
        int err = -EOPNOTSUPP;
        char *encoded_list = NULL;
 
-       unionfs_read_lock(dentry->d_sb);
-       unionfs_lock_dentry(dentry);
+       unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
+       unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
 
        if (unlikely(!__unionfs_d_revalidate_chain(dentry, NULL, false))) {
                err = -ESTALE;