]> git.hungrycats.org Git - linux/commitdiff
btrfs: fix clone / extent-same deadlocks
authorMark Fasheh <mfasheh@suse.de>
Tue, 23 Jun 2015 21:28:38 +0000 (14:28 -0700)
committerZygo Blaxell <zblaxell@serenity.furryterror.org>
Wed, 1 Jul 2015 00:00:29 +0000 (20:00 -0400)
Clone and extent same lock their source and target inodes in opposite order.
In addition to this, the range locking in clone doesn't take ordering into
account. Fix this by having clone use the same locking helpers as
btrfs-extent-same.

In addition, I do a small cleanup of the locking helpers, removing a case
(both inodes being the same) which was poorly accounted for and never
actually used by the callers.

Signed-off-by: Mark Fasheh <mfasheh@suse.de>
Reviewed-by: David Sterba <dsterba@suse.cz>
(cherry picked from commit 476fc6db1e1c33647a63c05af6e0d91ac93c1326)

fs/btrfs/ioctl.c

index b89958466bfcb4958bfcc781b25d16a0ef3d40f7..8d6887d2bf4dfeab799e4f497371e224a5606446 100644 (file)
@@ -2831,8 +2831,7 @@ static void btrfs_double_inode_lock(struct inode *inode1, struct inode *inode2)
                swap(inode1, inode2);
 
        mutex_lock_nested(&inode1->i_mutex, I_MUTEX_PARENT);
-       if (inode1 != inode2)
-               mutex_lock_nested(&inode2->i_mutex, I_MUTEX_CHILD);
+       mutex_lock_nested(&inode2->i_mutex, I_MUTEX_CHILD);
 }
 
 static void btrfs_double_extent_unlock(struct inode *inode1, u64 loff1,
@@ -2850,8 +2849,7 @@ static void btrfs_double_extent_lock(struct inode *inode1, u64 loff1,
                swap(loff1, loff2);
        }
        lock_extent_range(inode1, loff1, len);
-       if (inode1 != inode2)
-               lock_extent_range(inode2, loff2, len);
+       lock_extent_range(inode2, loff2, len);
 }
 
 struct cmp_pages {
@@ -3713,13 +3711,7 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
                goto out_fput;
 
        if (!same_inode) {
-               if (inode < src) {
-                       mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
-                       mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
-               } else {
-                       mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
-                       mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
-               }
+               btrfs_double_inode_lock(src, inode);
        } else {
                mutex_lock(&src->i_mutex);
        }
@@ -3769,8 +3761,7 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
 
                lock_extent_range(src, lock_start, lock_len);
        } else {
-               lock_extent_range(src, off, len);
-               lock_extent_range(inode, destoff, len);
+               btrfs_double_extent_lock(src, off, inode, destoff, len);
        }
 
        ret = btrfs_clone(src, inode, off, olen, len, destoff);
@@ -3781,9 +3772,7 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
 
                unlock_extent(&BTRFS_I(src)->io_tree, lock_start, lock_end);
        } else {
-               unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
-               unlock_extent(&BTRFS_I(inode)->io_tree, destoff,
-                             destoff + len - 1);
+               btrfs_double_extent_unlock(src, off, inode, destoff, len);
        }
        /*
         * Truncate page cache pages so that future reads will see the cloned
@@ -3792,17 +3781,10 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
        truncate_inode_pages_range(&inode->i_data, destoff,
                                   PAGE_CACHE_ALIGN(destoff + len) - 1);
 out_unlock:
-       if (!same_inode) {
-               if (inode < src) {
-                       mutex_unlock(&src->i_mutex);
-                       mutex_unlock(&inode->i_mutex);
-               } else {
-                       mutex_unlock(&inode->i_mutex);
-                       mutex_unlock(&src->i_mutex);
-               }
-       } else {
+       if (!same_inode)
+               btrfs_double_inode_unlock(src, inode);
+       else
                mutex_unlock(&src->i_mutex);
-       }
 out_fput:
        fdput(src_file);
 out_drop_write: