Commit 3db11b2e authored by Zach Brown's avatar Zach Brown Committed by Al Viro

btrfs: add .copy_file_range file operation

This rearranges the existing COPY_RANGE ioctl implementation so that the
.copy_file_range file operation can call the core loop that copies file
data extent items.

The extent copying loop is lifted up into its own function.  It retains
the core btrfs error checks that should be shared.
Signed-off-by: default avatarZach Brown <zab@redhat.com>
[Anna Schumaker: Make flags an unsigned int,
                 Check for COPY_FR_REFLINK]
Signed-off-by: default avatarAnna Schumaker <Anna.Schumaker@Netapp.com>
Reviewed-by: default avatarJosef Bacik <jbacik@fb.com>
Reviewed-by: default avatarDavid Sterba <dsterba@suse.com>
Reviewed-by: default avatarChristoph Hellwig <hch@lst.de>
Signed-off-by: default avatarAl Viro <viro@zeniv.linux.org.uk>
parent cb4c4e80
...@@ -4055,6 +4055,9 @@ int btrfs_dirty_pages(struct btrfs_root *root, struct inode *inode, ...@@ -4055,6 +4055,9 @@ int btrfs_dirty_pages(struct btrfs_root *root, struct inode *inode,
loff_t pos, size_t write_bytes, loff_t pos, size_t write_bytes,
struct extent_state **cached); struct extent_state **cached);
int btrfs_fdatawrite_range(struct inode *inode, loff_t start, loff_t end); int btrfs_fdatawrite_range(struct inode *inode, loff_t start, loff_t end);
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);
/* tree-defrag.c */ /* tree-defrag.c */
int btrfs_defrag_leaves(struct btrfs_trans_handle *trans, int btrfs_defrag_leaves(struct btrfs_trans_handle *trans,
......
...@@ -2924,6 +2924,7 @@ const struct file_operations btrfs_file_operations = { ...@@ -2924,6 +2924,7 @@ const struct file_operations btrfs_file_operations = {
#ifdef CONFIG_COMPAT #ifdef CONFIG_COMPAT
.compat_ioctl = btrfs_ioctl, .compat_ioctl = btrfs_ioctl,
#endif #endif
.copy_file_range = btrfs_copy_file_range,
}; };
void btrfs_auto_defrag_exit(void) void btrfs_auto_defrag_exit(void)
......
...@@ -3779,17 +3779,16 @@ static int btrfs_clone(struct inode *src, struct inode *inode, ...@@ -3779,17 +3779,16 @@ static int btrfs_clone(struct inode *src, struct inode *inode,
return ret; return ret;
} }
static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd, static noinline int btrfs_clone_files(struct file *file, struct file *file_src,
u64 off, u64 olen, u64 destoff) u64 off, u64 olen, u64 destoff)
{ {
struct inode *inode = file_inode(file); struct inode *inode = file_inode(file);
struct inode *src = file_inode(file_src);
struct btrfs_root *root = BTRFS_I(inode)->root; struct btrfs_root *root = BTRFS_I(inode)->root;
struct fd src_file;
struct inode *src;
int ret; int ret;
u64 len = olen; u64 len = olen;
u64 bs = root->fs_info->sb->s_blocksize; u64 bs = root->fs_info->sb->s_blocksize;
int same_inode = 0; int same_inode = src == inode;
/* /*
* TODO: * TODO:
...@@ -3802,49 +3801,20 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd, ...@@ -3802,49 +3801,20 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
* be either compressed or non-compressed. * 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)) if (btrfs_root_readonly(root))
return -EROFS; return -EROFS;
ret = mnt_want_write_file(file); if (file_src->f_path.mnt != file->f_path.mnt ||
if (ret) src->i_sb != inode->i_sb)
return ret; return -EXDEV;
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;
/* don't make the dst file partly checksummed */ /* don't make the dst file partly checksummed */
if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) != if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
(BTRFS_I(inode)->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)) if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
goto out_fput; return -EISDIR;
ret = -EXDEV;
if (src->i_sb != inode->i_sb)
goto out_fput;
if (!same_inode) { if (!same_inode) {
btrfs_double_inode_lock(src, inode); btrfs_double_inode_lock(src, inode);
...@@ -3921,6 +3891,49 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd, ...@@ -3921,6 +3891,49 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
btrfs_double_inode_unlock(src, inode); btrfs_double_inode_unlock(src, inode);
else else
mutex_unlock(&src->i_mutex); mutex_unlock(&src->i_mutex);
return ret;
}
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)
{
ssize_t ret;
ret = btrfs_clone_files(file_out, file_in, pos_in, len, pos_out);
if (ret == 0)
ret = len;
return ret;
}
static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
u64 off, u64 olen, u64 destoff)
{
struct fd src_file;
int ret;
/* the destination must be opened for writing */
if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
return -EINVAL;
ret = mnt_want_write_file(file);
if (ret)
return ret;
src_file = fdget(srcfd);
if (!src_file.file) {
ret = -EBADF;
goto out_drop_write;
}
/* the src must be open for reading */
if (!(src_file.file->f_mode & FMODE_READ)) {
ret = -EINVAL;
goto out_fput;
}
ret = btrfs_clone_files(file, src_file.file, off, olen, destoff);
out_fput: out_fput:
fdput(src_file); fdput(src_file);
out_drop_write: out_drop_write:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment