Merge branch 'for-linus-4.5' of git://git.kernel.org/pub/scm/linux/kernel/git/mason...
authorLinus Torvalds <torvalds@linux-foundation.org>
Fri, 12 Feb 2016 17:21:28 +0000 (09:21 -0800)
committerLinus Torvalds <torvalds@linux-foundation.org>
Fri, 12 Feb 2016 17:21:28 +0000 (09:21 -0800)
Pull btrfs fixes from Chris Mason:
 "This has a few fixes from Filipe, along with a readdir fix from Dave
  that we've been testing for some time"

* 'for-linus-4.5' of git://git.kernel.org/pub/scm/linux/kernel/git/mason/linux-btrfs:
  btrfs: properly set the termination value of ctx->pos in readdir
  Btrfs: fix hang on extent buffer lock caused by the inode_paths ioctl
  Btrfs: remove no longer used function extent_read_full_page_nolock()
  Btrfs: fix page reading in extent_same ioctl leading to csum errors
  Btrfs: fix invalid page accesses in extent_same (dedup) ioctl

1  2 
fs/btrfs/inode.c
fs/btrfs/ioctl.c

diff --combined fs/btrfs/inode.c
@@@ -3546,10 -3546,10 +3546,10 @@@ static noinline int acls_after_inode_it
        int scanned = 0;
  
        if (!xattr_access) {
 -              xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS,
 -                                      strlen(POSIX_ACL_XATTR_ACCESS));
 -              xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT,
 -                                      strlen(POSIX_ACL_XATTR_DEFAULT));
 +              xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
 +                                      strlen(XATTR_NAME_POSIX_ACL_ACCESS));
 +              xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
 +                                      strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
        }
  
        slot++;
@@@ -3770,7 -3770,6 +3770,7 @@@ cache_acl
                break;
        case S_IFLNK:
                inode->i_op = &btrfs_symlink_inode_operations;
 +              inode_nohighmem(inode);
                inode->i_mapping->a_ops = &btrfs_symlink_aops;
                break;
        default:
@@@ -5717,6 -5716,7 +5717,7 @@@ static int btrfs_real_readdir(struct fi
        char *name_ptr;
        int name_len;
        int is_curr = 0;        /* ctx->pos points to the current index? */
+       bool emitted;
  
        /* FIXME, use a real flag for deciding about the key type */
        if (root->fs_info->tree_root == root)
        if (ret < 0)
                goto err;
  
+       emitted = false;
        while (1) {
                leaf = path->nodes[0];
                slot = path->slots[0];
@@@ -5824,6 -5825,7 +5826,7 @@@ skip
  
                        if (over)
                                goto nopos;
+                       emitted = true;
                        di_len = btrfs_dir_name_len(leaf, di) +
                                 btrfs_dir_data_len(leaf, di) + sizeof(*di);
                        di_cur += di_len;
@@@ -5836,11 -5838,20 +5839,20 @@@ next
        if (key_type == BTRFS_DIR_INDEX_KEY) {
                if (is_curr)
                        ctx->pos++;
-               ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
+               ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list, &emitted);
                if (ret)
                        goto nopos;
        }
  
+       /*
+        * If we haven't emitted any dir entry, we must not touch ctx->pos as
+        * it was was set to the termination value in previous call. We assume
+        * that "." and ".." were emitted if we reach this point and set the
+        * termination value as well for an empty directory.
+        */
+       if (ctx->pos > 2 && !emitted)
+               goto nopos;
        /* Reached end of directory/root. Bump pos past the last item. */
        ctx->pos++;
  
@@@ -8467,7 -8478,7 +8479,7 @@@ static ssize_t btrfs_direct_IO(struct k
                 * not unlock the i_mutex at this case.
                 */
                if (offset + count <= inode->i_size) {
 -                      mutex_unlock(&inode->i_mutex);
 +                      inode_unlock(inode);
                        relock = true;
                }
                ret = btrfs_delalloc_reserve_space(inode, offset, count);
@@@ -8524,7 -8535,7 +8536,7 @@@ out
        if (wakeup)
                inode_dio_end(inode);
        if (relock)
 -              mutex_lock(&inode->i_mutex);
 +              inode_lock(inode);
  
        return ret;
  }
@@@ -9192,8 -9203,7 +9204,8 @@@ int btrfs_init_cachep(void
  {
        btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
                        sizeof(struct btrfs_inode), 0,
 -                      SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
 +                      SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
 +                      init_once);
        if (!btrfs_inode_cachep)
                goto fail;
  
@@@ -9724,7 -9734,6 +9736,7 @@@ static int btrfs_symlink(struct inode *
        btrfs_free_path(path);
  
        inode->i_op = &btrfs_symlink_inode_operations;
 +      inode_nohighmem(inode);
        inode->i_mapping->a_ops = &btrfs_symlink_aops;
        inode_set_bytes(inode, name_len);
        btrfs_i_size_write(inode, name_len);
@@@ -10021,7 -10030,7 +10033,7 @@@ static const struct inode_operations bt
        .setattr        = btrfs_setattr,
        .mknod          = btrfs_mknod,
        .setxattr       = btrfs_setxattr,
 -      .getxattr       = btrfs_getxattr,
 +      .getxattr       = generic_getxattr,
        .listxattr      = btrfs_listxattr,
        .removexattr    = btrfs_removexattr,
        .permission     = btrfs_permission,
@@@ -10098,7 -10107,7 +10110,7 @@@ static const struct inode_operations bt
        .getattr        = btrfs_getattr,
        .setattr        = btrfs_setattr,
        .setxattr       = btrfs_setxattr,
 -      .getxattr       = btrfs_getxattr,
 +      .getxattr       = generic_getxattr,
        .listxattr      = btrfs_listxattr,
        .removexattr    = btrfs_removexattr,
        .permission     = btrfs_permission,
@@@ -10112,7 -10121,7 +10124,7 @@@ static const struct inode_operations bt
        .setattr        = btrfs_setattr,
        .permission     = btrfs_permission,
        .setxattr       = btrfs_setxattr,
 -      .getxattr       = btrfs_getxattr,
 +      .getxattr       = generic_getxattr,
        .listxattr      = btrfs_listxattr,
        .removexattr    = btrfs_removexattr,
        .get_acl        = btrfs_get_acl,
  };
  static const struct inode_operations btrfs_symlink_inode_operations = {
        .readlink       = generic_readlink,
 -      .follow_link    = page_follow_link_light,
 -      .put_link       = page_put_link,
 +      .get_link       = page_get_link,
        .getattr        = btrfs_getattr,
        .setattr        = btrfs_setattr,
        .permission     = btrfs_permission,
        .setxattr       = btrfs_setxattr,
 -      .getxattr       = btrfs_getxattr,
 +      .getxattr       = generic_getxattr,
        .listxattr      = btrfs_listxattr,
        .removexattr    = btrfs_removexattr,
        .update_time    = btrfs_update_time,
diff --combined fs/btrfs/ioctl.c
@@@ -240,7 -240,7 +240,7 @@@ static int btrfs_ioctl_setflags(struct 
        if (ret)
                return ret;
  
 -      mutex_lock(&inode->i_mutex);
 +      inode_lock(inode);
  
        ip_oldflags = ip->flags;
        i_oldflags = inode->i_flags;
        }
  
   out_unlock:
 -      mutex_unlock(&inode->i_mutex);
 +      inode_unlock(inode);
        mnt_drop_write_file(file);
        return ret;
  }
@@@ -881,7 -881,7 +881,7 @@@ out_up_read
  out_dput:
        dput(dentry);
  out_unlock:
 -      mutex_unlock(&dir->i_mutex);
 +      inode_unlock(dir);
        return error;
  }
  
@@@ -1393,18 -1393,18 +1393,18 @@@ int btrfs_defrag_file(struct inode *ino
                        ra_index += cluster;
                }
  
 -              mutex_lock(&inode->i_mutex);
 +              inode_lock(inode);
                if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
                        BTRFS_I(inode)->force_compress = compress_type;
                ret = cluster_pages_for_defrag(inode, pages, i, cluster);
                if (ret < 0) {
 -                      mutex_unlock(&inode->i_mutex);
 +                      inode_unlock(inode);
                        goto out_ra;
                }
  
                defrag_count += ret;
                balance_dirty_pages_ratelimited(inode->i_mapping);
 -              mutex_unlock(&inode->i_mutex);
 +              inode_unlock(inode);
  
                if (newer_than) {
                        if (newer_off == (u64)-1)
  
  out_ra:
        if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
 -              mutex_lock(&inode->i_mutex);
 +              inode_lock(inode);
                BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
 -              mutex_unlock(&inode->i_mutex);
 +              inode_unlock(inode);
        }
        if (!file)
                kfree(ra);
@@@ -2430,7 -2430,7 +2430,7 @@@ static noinline int btrfs_ioctl_snap_de
                goto out_dput;
        }
  
 -      mutex_lock(&inode->i_mutex);
 +      inode_lock(inode);
  
        /*
         * Don't allow to delete a subvolume with send in progress. This is
@@@ -2543,7 -2543,7 +2543,7 @@@ out_up_write
                spin_unlock(&dest->root_item_lock);
        }
  out_unlock_inode:
 -      mutex_unlock(&inode->i_mutex);
 +      inode_unlock(inode);
        if (!err) {
                d_invalidate(dentry);
                btrfs_invalidate_inodes(dest);
  out_dput:
        dput(dentry);
  out_unlock_dir:
 -      mutex_unlock(&dir->i_mutex);
 +      inode_unlock(dir);
  out_drop_write:
        mnt_drop_write_file(file);
  out:
  static struct page *extent_same_get_page(struct inode *inode, pgoff_t index)
  {
        struct page *page;
-       struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
  
        page = grab_cache_page(inode->i_mapping, index);
        if (!page)
-               return NULL;
+               return ERR_PTR(-ENOMEM);
  
        if (!PageUptodate(page)) {
-               if (extent_read_full_page_nolock(tree, page, btrfs_get_extent,
-                                                0))
-                       return NULL;
+               int ret;
+               ret = btrfs_readpage(NULL, page);
+               if (ret)
+                       return ERR_PTR(ret);
                lock_page(page);
                if (!PageUptodate(page)) {
                        unlock_page(page);
                        page_cache_release(page);
-                       return NULL;
+                       return ERR_PTR(-EIO);
+               }
+               if (page->mapping != inode->i_mapping) {
+                       unlock_page(page);
+                       page_cache_release(page);
+                       return ERR_PTR(-EAGAIN);
                }
        }
-       unlock_page(page);
  
        return page;
  }
@@@ -2823,17 -2828,31 +2828,31 @@@ static int gather_extent_pages(struct i
        pgoff_t index = off >> PAGE_CACHE_SHIFT;
  
        for (i = 0; i < num_pages; i++) {
+ again:
                pages[i] = extent_same_get_page(inode, index + i);
-               if (!pages[i])
-                       return -ENOMEM;
+               if (IS_ERR(pages[i])) {
+                       int err = PTR_ERR(pages[i]);
+                       if (err == -EAGAIN)
+                               goto again;
+                       pages[i] = NULL;
+                       return err;
+               }
        }
        return 0;
  }
  
- static inline void lock_extent_range(struct inode *inode, u64 off, u64 len)
+ static int lock_extent_range(struct inode *inode, u64 off, u64 len,
+                            bool retry_range_locking)
  {
-       /* do any pending delalloc/csum calc on src, one way or
-          another, and lock file content */
+       /*
+        * Do any pending delalloc/csum calculations on inode, one way or
+        * another, and lock file content.
+        * The locking order is:
+        *
+        *   1) pages
+        *   2) range in the inode's io tree
+        */
        while (1) {
                struct btrfs_ordered_extent *ordered;
                lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
                unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
                if (ordered)
                        btrfs_put_ordered_extent(ordered);
+               if (!retry_range_locking)
+                       return -EAGAIN;
                btrfs_wait_ordered_range(inode, off, len);
        }
+       return 0;
  }
  
  static void btrfs_double_inode_unlock(struct inode *inode1, struct inode *inode2)
  {
 -      mutex_unlock(&inode1->i_mutex);
 -      mutex_unlock(&inode2->i_mutex);
 +      inode_unlock(inode1);
 +      inode_unlock(inode2);
  }
  
  static void btrfs_double_inode_lock(struct inode *inode1, struct inode *inode2)
        if (inode1 < inode2)
                swap(inode1, inode2);
  
 -      mutex_lock_nested(&inode1->i_mutex, I_MUTEX_PARENT);
 -      mutex_lock_nested(&inode2->i_mutex, I_MUTEX_CHILD);
 +      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,
        unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
  }
  
- static void btrfs_double_extent_lock(struct inode *inode1, u64 loff1,
-                                    struct inode *inode2, u64 loff2, u64 len)
+ static int btrfs_double_extent_lock(struct inode *inode1, u64 loff1,
+                                   struct inode *inode2, u64 loff2, u64 len,
+                                   bool retry_range_locking)
  {
+       int ret;
        if (inode1 < inode2) {
                swap(inode1, inode2);
                swap(loff1, loff2);
        }
-       lock_extent_range(inode1, loff1, len);
-       lock_extent_range(inode2, loff2, len);
+       ret = lock_extent_range(inode1, loff1, len, retry_range_locking);
+       if (ret)
+               return ret;
+       ret = lock_extent_range(inode2, loff2, len, retry_range_locking);
+       if (ret)
+               unlock_extent(&BTRFS_I(inode1)->io_tree, loff1,
+                             loff1 + len - 1);
+       return ret;
  }
  
  struct cmp_pages {
@@@ -2901,11 -2932,15 +2932,15 @@@ static void btrfs_cmp_data_free(struct 
  
        for (i = 0; i < cmp->num_pages; i++) {
                pg = cmp->src_pages[i];
-               if (pg)
+               if (pg) {
+                       unlock_page(pg);
                        page_cache_release(pg);
+               }
                pg = cmp->dst_pages[i];
-               if (pg)
+               if (pg) {
+                       unlock_page(pg);
                        page_cache_release(pg);
+               }
        }
        kfree(cmp->src_pages);
        kfree(cmp->dst_pages);
@@@ -2966,6 -3001,8 +3001,8 @@@ static int btrfs_cmp_data(struct inode 
  
                src_page = cmp->src_pages[i];
                dst_page = cmp->dst_pages[i];
+               ASSERT(PageLocked(src_page));
+               ASSERT(PageLocked(dst_page));
  
                addr = kmap_atomic(src_page);
                dst_addr = kmap_atomic(dst_page);
                flush_dcache_page(dst_page);
  
                if (memcmp(addr, dst_addr, cmp_len))
 -                      ret = BTRFS_SAME_DATA_DIFFERS;
 +                      ret = -EBADE;
  
                kunmap_atomic(addr);
                kunmap_atomic(dst_addr);
@@@ -3026,7 -3063,7 +3063,7 @@@ static int btrfs_extent_same(struct ino
                return 0;
  
        if (same_inode) {
 -              mutex_lock(&src->i_mutex);
 +              inode_lock(src);
  
                ret = extent_same_check_offsets(src, loff, &len, olen);
                if (ret)
                goto out_unlock;
        }
  
+ again:
        ret = btrfs_cmp_data_prepare(src, loff, dst, dst_loff, olen, &cmp);
        if (ret)
                goto out_unlock;
  
        if (same_inode)
-               lock_extent_range(src, same_lock_start, same_lock_len);
+               ret = lock_extent_range(src, same_lock_start, same_lock_len,
+                                       false);
        else
-               btrfs_double_extent_lock(src, loff, dst, dst_loff, len);
+               ret = btrfs_double_extent_lock(src, loff, dst, dst_loff, len,
+                                              false);
+       /*
+        * If one of the inodes has dirty pages in the respective range or
+        * ordered extents, we need to flush dellaloc and wait for all ordered
+        * extents in the range. We must unlock the pages and the ranges in the
+        * io trees to avoid deadlocks when flushing delalloc (requires locking
+        * pages) and when waiting for ordered extents to complete (they require
+        * range locking).
+        */
+       if (ret == -EAGAIN) {
+               /*
+                * Ranges in the io trees already unlocked. Now unlock all
+                * pages before waiting for all IO to complete.
+                */
+               btrfs_cmp_data_free(&cmp);
+               if (same_inode) {
+                       btrfs_wait_ordered_range(src, same_lock_start,
+                                                same_lock_len);
+               } else {
+                       btrfs_wait_ordered_range(src, loff, len);
+                       btrfs_wait_ordered_range(dst, dst_loff, len);
+               }
+               goto again;
+       }
+       ASSERT(ret == 0);
+       if (WARN_ON(ret)) {
+               /* ranges in the io trees already unlocked */
+               btrfs_cmp_data_free(&cmp);
+               return ret;
+       }
  
        /* pass original length for comparison so we stay within i_size */
        ret = btrfs_cmp_data(src, loff, dst, dst_loff, olen, &cmp);
        btrfs_cmp_data_free(&cmp);
  out_unlock:
        if (same_inode)
 -              mutex_unlock(&src->i_mutex);
 +              inode_unlock(src);
        else
                btrfs_double_inode_unlock(src, dst);
  
  
  #define BTRFS_MAX_DEDUPE_LEN  SZ_16M
  
 -static long btrfs_ioctl_file_extent_same(struct file *file,
 -                      struct btrfs_ioctl_same_args __user *argp)
 +ssize_t btrfs_dedupe_file_range(struct file *src_file, u64 loff, u64 olen,
 +                              struct file *dst_file, u64 dst_loff)
  {
 -      struct btrfs_ioctl_same_args *same = NULL;
 -      struct btrfs_ioctl_same_extent_info *info;
 -      struct inode *src = file_inode(file);
 -      u64 off;
 -      u64 len;
 -      int i;
 -      int ret;
 -      unsigned long size;
 +      struct inode *src = file_inode(src_file);
 +      struct inode *dst = file_inode(dst_file);
        u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize;
 -      bool is_admin = capable(CAP_SYS_ADMIN);
 -      u16 count;
 -
 -      if (!(file->f_mode & FMODE_READ))
 -              return -EINVAL;
 -
 -      ret = mnt_want_write_file(file);
 -      if (ret)
 -              return ret;
 -
 -      if (get_user(count, &argp->dest_count)) {
 -              ret = -EFAULT;
 -              goto out;
 -      }
 -
 -      size = offsetof(struct btrfs_ioctl_same_args __user, info[count]);
 -
 -      same = memdup_user(argp, size);
 -
 -      if (IS_ERR(same)) {
 -              ret = PTR_ERR(same);
 -              same = NULL;
 -              goto out;
 -      }
 +      ssize_t res;
  
 -      off = same->logical_offset;
 -      len = same->length;
 -
 -      /*
 -       * Limit the total length we will dedupe for each operation.
 -       * This is intended to bound the total time spent in this
 -       * ioctl to something sane.
 -       */
 -      if (len > BTRFS_MAX_DEDUPE_LEN)
 -              len = BTRFS_MAX_DEDUPE_LEN;
 +      if (olen > BTRFS_MAX_DEDUPE_LEN)
 +              olen = BTRFS_MAX_DEDUPE_LEN;
  
        if (WARN_ON_ONCE(bs < PAGE_CACHE_SIZE)) {
                /*
                 * result, btrfs_cmp_data() won't correctly handle
                 * this situation without an update.
                 */
 -              ret = -EINVAL;
 -              goto out;
 -      }
 -
 -      ret = -EISDIR;
 -      if (S_ISDIR(src->i_mode))
 -              goto out;
 -
 -      ret = -EACCES;
 -      if (!S_ISREG(src->i_mode))
 -              goto out;
 -
 -      /* pre-format output fields to sane values */
 -      for (i = 0; i < count; i++) {
 -              same->info[i].bytes_deduped = 0ULL;
 -              same->info[i].status = 0;
 -      }
 -
 -      for (i = 0, info = same->info; i < count; i++, info++) {
 -              struct inode *dst;
 -              struct fd dst_file = fdget(info->fd);
 -              if (!dst_file.file) {
 -                      info->status = -EBADF;
 -                      continue;
 -              }
 -              dst = file_inode(dst_file.file);
 -
 -              if (!(is_admin || (dst_file.file->f_mode & FMODE_WRITE))) {
 -                      info->status = -EINVAL;
 -              } else if (file->f_path.mnt != dst_file.file->f_path.mnt) {
 -                      info->status = -EXDEV;
 -              } else if (S_ISDIR(dst->i_mode)) {
 -                      info->status = -EISDIR;
 -              } else if (!S_ISREG(dst->i_mode)) {
 -                      info->status = -EACCES;
 -              } else {
 -                      info->status = btrfs_extent_same(src, off, len, dst,
 -                                                      info->logical_offset);
 -                      if (info->status == 0)
 -                              info->bytes_deduped += len;
 -              }
 -              fdput(dst_file);
 +              return -EINVAL;
        }
  
 -      ret = copy_to_user(argp, same, size);
 -      if (ret)
 -              ret = -EFAULT;
 -
 -out:
 -      mnt_drop_write_file(file);
 -      kfree(same);
 -      return ret;
 +      res = btrfs_extent_same(src, loff, olen, dst, dst_loff);
 +      if (res)
 +              return res;
 +      return olen;
  }
  
  static int clone_finish_inode_update(struct btrfs_trans_handle *trans,
        return ret;
  }
  
 -static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
 -                                     u64 off, u64 olen, u64 destoff)
 +static noinline int btrfs_clone_files(struct file *file, struct file *file_src,
 +                                      u64 off, u64 olen, u64 destoff)
  {
        struct inode *inode = file_inode(file);
 +      struct inode *src = file_inode(file_src);
        struct btrfs_root *root = BTRFS_I(inode)->root;
 -      struct fd src_file;
 -      struct inode *src;
        int ret;
        u64 len = olen;
        u64 bs = root->fs_info->sb->s_blocksize;
 -      int same_inode = 0;
 +      int same_inode = src == inode;
  
        /*
         * TODO:
         *   be either compressed or non-compressed.
         */
  
 -      /* the destination must be opened for writing */
 -      if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
 -              return -EINVAL;
 -
        if (btrfs_root_readonly(root))
                return -EROFS;
  
 -      ret = mnt_want_write_file(file);
 -      if (ret)
 -              return ret;
 -
 -      src_file = fdget(srcfd);
 -      if (!src_file.file) {
 -              ret = -EBADF;
 -              goto out_drop_write;
 -      }
 -
 -      ret = -EXDEV;
 -      if (src_file.file->f_path.mnt != file->f_path.mnt)
 -              goto out_fput;
 -
 -      src = file_inode(src_file.file);
 -
 -      ret = -EINVAL;
 -      if (src == inode)
 -              same_inode = 1;
 -
 -      /* the src must be open for reading */
 -      if (!(src_file.file->f_mode & FMODE_READ))
 -              goto out_fput;
 +      if (file_src->f_path.mnt != file->f_path.mnt ||
 +          src->i_sb != inode->i_sb)
 +              return -EXDEV;
  
        /* don't make the dst file partly checksummed */
        if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
            (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
 -              goto out_fput;
 +              return -EINVAL;
  
 -      ret = -EISDIR;
        if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
 -              goto out_fput;
 -
 -      ret = -EXDEV;
 -      if (src->i_sb != inode->i_sb)
 -              goto out_fput;
 +              return -EISDIR;
  
        if (!same_inode) {
                btrfs_double_inode_lock(src, inode);
        } else {
 -              mutex_lock(&src->i_mutex);
 +              inode_lock(src);
        }
  
        /* determine range to clone */
                u64 lock_start = min_t(u64, off, destoff);
                u64 lock_len = max_t(u64, off, destoff) + len - lock_start;
  
-               lock_extent_range(src, lock_start, lock_len);
+               ret = lock_extent_range(src, lock_start, lock_len, true);
        } else {
-               btrfs_double_extent_lock(src, off, inode, destoff, len);
+               ret = btrfs_double_extent_lock(src, off, inode, destoff, len,
+                                              true);
+       }
+       ASSERT(ret == 0);
+       if (WARN_ON(ret)) {
+               /* ranges in the io trees already unlocked */
+               goto out_unlock;
        }
  
        ret = btrfs_clone(src, inode, off, olen, len, destoff, 0);
@@@ -3820,26 -4007,22 +3895,26 @@@ out_unlock
        if (!same_inode)
                btrfs_double_inode_unlock(src, inode);
        else
 -              mutex_unlock(&src->i_mutex);
 -out_fput:
 -      fdput(src_file);
 -out_drop_write:
 -      mnt_drop_write_file(file);
 +              inode_unlock(src);
        return ret;
  }
  
 -static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
 +ssize_t btrfs_copy_file_range(struct file *file_in, loff_t pos_in,
 +                            struct file *file_out, loff_t pos_out,
 +                            size_t len, unsigned int flags)
  {
 -      struct btrfs_ioctl_clone_range_args args;
 +      ssize_t ret;
  
 -      if (copy_from_user(&args, argp, sizeof(args)))
 -              return -EFAULT;
 -      return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
 -                               args.src_length, args.dest_offset);
 +      ret = btrfs_clone_files(file_out, file_in, pos_in, len, pos_out);
 +      if (ret == 0)
 +              ret = len;
 +      return ret;
 +}
 +
 +int btrfs_clone_file_range(struct file *src_file, loff_t off,
 +              struct file *dst_file, loff_t destoff, u64 len)
 +{
 +      return btrfs_clone_files(dst_file, src_file, off, len, destoff);
  }
  
  /*
@@@ -5389,6 -5572,10 +5464,6 @@@ long btrfs_ioctl(struct file *file, uns
                return btrfs_ioctl_dev_info(root, argp);
        case BTRFS_IOC_BALANCE:
                return btrfs_ioctl_balance(file, NULL);
 -      case BTRFS_IOC_CLONE:
 -              return btrfs_ioctl_clone(file, arg, 0, 0, 0);
 -      case BTRFS_IOC_CLONE_RANGE:
 -              return btrfs_ioctl_clone_range(file, argp);
        case BTRFS_IOC_TRANS_START:
                return btrfs_ioctl_trans_start(file);
        case BTRFS_IOC_TRANS_END:
                return btrfs_ioctl_get_fslabel(file, argp);
        case BTRFS_IOC_SET_FSLABEL:
                return btrfs_ioctl_set_fslabel(file, argp);
 -      case BTRFS_IOC_FILE_EXTENT_SAME:
 -              return btrfs_ioctl_file_extent_same(file, argp);
        case BTRFS_IOC_GET_SUPPORTED_FEATURES:
                return btrfs_ioctl_get_supported_features(file, argp);
        case BTRFS_IOC_GET_FEATURES: