Btrfs: fix deadlock between clone/dedupe and rename
[sfrench/cifs-2.6.git] / fs / btrfs / ioctl.c
index 9c8e1734429c7649721eeadd3dca941ff2ee490c..ec2d8919e7fb0ee63c28bcfd241d0e8a54ed05a3 100644 (file)
@@ -1642,7 +1642,7 @@ static noinline int btrfs_ioctl_resize(struct file *file,
                btrfs_info(fs_info, "resizing devid %llu", devid);
        }
 
-       device = btrfs_find_device(fs_info, devid, NULL, NULL);
+       device = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, true);
        if (!device) {
                btrfs_info(fs_info, "resizer unable to find device %llu",
                           devid);
@@ -3178,7 +3178,8 @@ static long btrfs_ioctl_dev_info(struct btrfs_fs_info *fs_info,
                s_uuid = di_args->uuid;
 
        rcu_read_lock();
-       dev = btrfs_find_device(fs_info, di_args->devid, s_uuid, NULL);
+       dev = btrfs_find_device(fs_info->fs_devices, di_args->devid, s_uuid,
+                               NULL, true);
 
        if (!dev) {
                ret = -ENODEV;
@@ -3206,21 +3207,6 @@ out:
        return ret;
 }
 
-static void btrfs_double_inode_unlock(struct inode *inode1, struct inode *inode2)
-{
-       inode_unlock(inode1);
-       inode_unlock(inode2);
-}
-
-static void btrfs_double_inode_lock(struct inode *inode1, struct inode *inode2)
-{
-       if (inode1 < inode2)
-               swap(inode1, inode2);
-
-       inode_lock_nested(inode1, I_MUTEX_PARENT);
-       inode_lock_nested(inode2, I_MUTEX_CHILD);
-}
-
 static void btrfs_double_extent_unlock(struct inode *inode1, u64 loff1,
                                       struct inode *inode2, u64 loff2, u64 len)
 {
@@ -3241,32 +3227,17 @@ static void btrfs_double_extent_lock(struct inode *inode1, u64 loff1,
        lock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
 }
 
-static int btrfs_extent_same_range(struct inode *src, u64 loff, u64 olen,
+static int btrfs_extent_same_range(struct inode *src, u64 loff, u64 len,
                                   struct inode *dst, u64 dst_loff)
 {
-       u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize;
        int ret;
-       u64 len = olen;
-
-       if (loff + len == src->i_size)
-               len = ALIGN(src->i_size, bs) - loff;
-       /*
-        * For same inode case we don't want our length pushed out past i_size
-        * as comparing that data range makes no sense.
-        *
-        * This effectively means we require aligned extents for the single
-        * inode case, whereas the other cases allow an unaligned length so long
-        * as it ends at i_size.
-        */
-       if (dst == src && len != olen)
-               return -EINVAL;
 
        /*
         * Lock destination range to serialize with concurrent readpages() and
         * source range to serialize with relocation.
         */
        btrfs_double_extent_lock(src, loff, dst, dst_loff, len);
-       ret = btrfs_clone(src, dst, loff, olen, len, dst_loff, 1);
+       ret = btrfs_clone(src, dst, loff, len, len, dst_loff, 1);
        btrfs_double_extent_unlock(src, loff, dst, dst_loff, len);
 
        return ret;
@@ -3278,21 +3249,10 @@ static int btrfs_extent_same(struct inode *src, u64 loff, u64 olen,
                             struct inode *dst, u64 dst_loff)
 {
        int ret;
-       int num_pages = PAGE_ALIGN(BTRFS_MAX_DEDUPE_LEN) >> PAGE_SHIFT;
        u64 i, tail_len, chunk_count;
 
-       /* don't make the dst file partly checksummed */
-       if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
-           (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM))
-               return -EINVAL;
-
-       if (IS_SWAPFILE(src) || IS_SWAPFILE(dst))
-               return -ETXTBSY;
-
        tail_len = olen % BTRFS_MAX_DEDUPE_LEN;
        chunk_count = div_u64(olen, BTRFS_MAX_DEDUPE_LEN);
-       if (chunk_count == 0)
-               num_pages = PAGE_ALIGN(tail_len) >> PAGE_SHIFT;
 
        for (i = 0; i < chunk_count; i++) {
                ret = btrfs_extent_same_range(src, loff, BTRFS_MAX_DEDUPE_LEN,
@@ -3908,14 +3868,6 @@ static noinline int btrfs_clone_files(struct file *file, struct file *file_src,
         *   be either compressed or non-compressed.
         */
 
-       /* don't make the dst file partly checksummed */
-       if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
-           (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
-               return -EINVAL;
-
-       if (IS_SWAPFILE(src) || IS_SWAPFILE(inode))
-               return -ETXTBSY;
-
        /*
         * VFS's generic_remap_file_range_prep() protects us from cloning the
         * eof block into the middle of a file, which would result in corruption
@@ -3989,7 +3941,14 @@ static int btrfs_remap_file_range_prep(struct file *file_in, loff_t pos_in,
        if (same_inode)
                inode_lock(inode_in);
        else
-               btrfs_double_inode_lock(inode_in, inode_out);
+               lock_two_nondirectories(inode_in, inode_out);
+
+       /* don't make the dst file partly checksummed */
+       if ((BTRFS_I(inode_in)->flags & BTRFS_INODE_NODATASUM) !=
+           (BTRFS_I(inode_out)->flags & BTRFS_INODE_NODATASUM)) {
+               ret = -EINVAL;
+               goto out_unlock;
+       }
 
        /*
         * Now that the inodes are locked, we need to start writeback ourselves
@@ -4039,7 +3998,7 @@ static int btrfs_remap_file_range_prep(struct file *file_in, loff_t pos_in,
        if (same_inode)
                inode_unlock(inode_in);
        else
-               btrfs_double_inode_unlock(inode_in, inode_out);
+               unlock_two_nondirectories(inode_in, inode_out);
 
        return ret;
 }
@@ -4069,7 +4028,7 @@ loff_t btrfs_remap_file_range(struct file *src_file, loff_t off,
        if (same_inode)
                inode_unlock(src_inode);
        else
-               btrfs_double_inode_unlock(src_inode, dst_inode);
+               unlock_two_nondirectories(src_inode, dst_inode);
 
        return ret < 0 ? ret : len;
 }
@@ -4381,7 +4340,7 @@ static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
                              &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
                              0);
 
-       if (copy_to_user(arg, sa, sizeof(*sa)))
+       if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
                ret = -EFAULT;
 
        if (!(sa->flags & BTRFS_SCRUB_READONLY))
@@ -4414,7 +4373,7 @@ static long btrfs_ioctl_scrub_progress(struct btrfs_fs_info *fs_info,
 
        ret = btrfs_scrub_progress(fs_info, sa->devid, &sa->progress);
 
-       if (copy_to_user(arg, sa, sizeof(*sa)))
+       if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
                ret = -EFAULT;
 
        kfree(sa);
@@ -4438,7 +4397,7 @@ static long btrfs_ioctl_get_dev_stats(struct btrfs_fs_info *fs_info,
 
        ret = btrfs_get_dev_stats(fs_info, sa);
 
-       if (copy_to_user(arg, sa, sizeof(*sa)))
+       if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
                ret = -EFAULT;
 
        kfree(sa);
@@ -4484,7 +4443,7 @@ static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info,
                break;
        }
 
-       if (copy_to_user(arg, p, sizeof(*p)))
+       if ((ret == 0 || ret == -ECANCELED) && copy_to_user(arg, p, sizeof(*p)))
                ret = -EFAULT;
 out:
        kfree(p);
@@ -4790,7 +4749,7 @@ do_balance:
        ret = btrfs_balance(fs_info, bctl, bargs);
        bctl = NULL;
 
-       if (arg) {
+       if ((ret == 0 || ret == -ECANCELED) && arg) {
                if (copy_to_user(arg, bargs, sizeof(*bargs)))
                        ret = -EFAULT;
        }