Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit fce205e9 authored by Linus Torvalds's avatar Linus Torvalds
Browse files

Merge branch 'work.copy_file_range' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs

Pull vfs copy_file_range updates from Al Viro:
 "Several series around copy_file_range/CLONE"

* 'work.copy_file_range' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs:
  btrfs: use new dedupe data function pointer
  vfs: hoist the btrfs deduplication ioctl to the vfs
  vfs: wire up compat ioctl for CLONE/CLONE_RANGE
  cifs: avoid unused variable and label
  nfsd: implement the NFSv4.2 CLONE operation
  nfsd: Pass filehandle to nfs4_preprocess_stateid_op()
  vfs: pull btrfs clone API to vfs layer
  locks: new locks_mandatory_area calling convention
  vfs: Add vfs_copy_file_range() support for pagecache copies
  btrfs: add .copy_file_range file operation
  x86: add sys_copy_file_range to syscall tables
  vfs: add copy_file_range syscall and vfs helper
parents 065019a3 2b3909f8
Loading
Loading
Loading
Loading
+1 −0
Original line number Original line Diff line number Diff line
@@ -383,3 +383,4 @@
374	i386	userfaultfd		sys_userfaultfd
374	i386	userfaultfd		sys_userfaultfd
375	i386	membarrier		sys_membarrier
375	i386	membarrier		sys_membarrier
376	i386	mlock2			sys_mlock2
376	i386	mlock2			sys_mlock2
377	i386	copy_file_range		sys_copy_file_range
+1 −0
Original line number Original line Diff line number Diff line
@@ -332,6 +332,7 @@
323	common	userfaultfd		sys_userfaultfd
323	common	userfaultfd		sys_userfaultfd
324	common	membarrier		sys_membarrier
324	common	membarrier		sys_membarrier
325	common	mlock2			sys_mlock2
325	common	mlock2			sys_mlock2
326	common	copy_file_range		sys_copy_file_range


#
#
# x32-specific system call numbers start at 512 to avoid cache impact
# x32-specific system call numbers start at 512 to avoid cache impact
+7 −1
Original line number Original line Diff line number Diff line
@@ -4024,7 +4024,8 @@ void btrfs_get_block_group_info(struct list_head *groups_list,
				struct btrfs_ioctl_space_info *space);
				struct btrfs_ioctl_space_info *space);
void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
			       struct btrfs_ioctl_balance_args *bargs);
			       struct btrfs_ioctl_balance_args *bargs);

ssize_t btrfs_dedupe_file_range(struct file *src_file, u64 loff, u64 olen,
			   struct file *dst_file, u64 dst_loff);


/* file.c */
/* file.c */
int btrfs_auto_defrag_init(void);
int btrfs_auto_defrag_init(void);
@@ -4055,6 +4056,11 @@ 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);
int btrfs_clone_file_range(struct file *file_in, loff_t pos_in,
			   struct file *file_out, loff_t pos_out, u64 len);


/* tree-defrag.c */
/* tree-defrag.c */
int btrfs_defrag_leaves(struct btrfs_trans_handle *trans,
int btrfs_defrag_leaves(struct btrfs_trans_handle *trans,
+3 −0
Original line number Original line Diff line number Diff line
@@ -2934,6 +2934,9 @@ 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,
	.clone_file_range = btrfs_clone_file_range,
	.dedupe_file_range = btrfs_dedupe_file_range,
};
};


void btrfs_auto_defrag_exit(void)
void btrfs_auto_defrag_exit(void)
+36 −150
Original line number Original line Diff line number Diff line
@@ -2962,7 +2962,7 @@ static int btrfs_cmp_data(struct inode *src, u64 loff, struct inode *dst,
		flush_dcache_page(dst_page);
		flush_dcache_page(dst_page);


		if (memcmp(addr, dst_addr, cmp_len))
		if (memcmp(addr, dst_addr, cmp_len))
			ret = BTRFS_SAME_DATA_DIFFERS;
			ret = -EBADE;


		kunmap_atomic(addr);
		kunmap_atomic(addr);
		kunmap_atomic(dst_addr);
		kunmap_atomic(dst_addr);
@@ -3098,53 +3098,16 @@ static int btrfs_extent_same(struct inode *src, u64 loff, u64 olen,


#define BTRFS_MAX_DEDUPE_LEN	(16 * 1024 * 1024)
#define BTRFS_MAX_DEDUPE_LEN	(16 * 1024 * 1024)


static long btrfs_ioctl_file_extent_same(struct file *file,
ssize_t btrfs_dedupe_file_range(struct file *src_file, u64 loff, u64 olen,
			struct btrfs_ioctl_same_args __user *argp)
				struct file *dst_file, u64 dst_loff)
{
{
	struct btrfs_ioctl_same_args *same = NULL;
	struct inode *src = file_inode(src_file);
	struct btrfs_ioctl_same_extent_info *info;
	struct inode *dst = file_inode(dst_file);
	struct inode *src = file_inode(file);
	u64 off;
	u64 len;
	int i;
	int ret;
	unsigned long size;
	u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize;
	u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize;
	bool is_admin = capable(CAP_SYS_ADMIN);
	ssize_t res;
	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]);
	if (olen > BTRFS_MAX_DEDUPE_LEN)

		olen = BTRFS_MAX_DEDUPE_LEN;
	same = memdup_user(argp, size);

	if (IS_ERR(same)) {
		ret = PTR_ERR(same);
		same = NULL;
		goto out;
	}

	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 (WARN_ON_ONCE(bs < PAGE_CACHE_SIZE)) {
	if (WARN_ON_ONCE(bs < PAGE_CACHE_SIZE)) {
		/*
		/*
@@ -3152,58 +3115,13 @@ static long btrfs_ioctl_file_extent_same(struct file *file,
		 * result, btrfs_cmp_data() won't correctly handle
		 * result, btrfs_cmp_data() won't correctly handle
		 * this situation without an update.
		 * this situation without an update.
		 */
		 */
		ret = -EINVAL;
		return -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);
	}
	}


	ret = copy_to_user(argp, same, size);
	res = btrfs_extent_same(src, loff, olen, dst, dst_loff);
	if (ret)
	if (res)
		ret = -EFAULT;
		return res;

	return olen;
out:
	mnt_drop_write_file(file);
	kfree(same);
	return ret;
}
}


static int clone_finish_inode_update(struct btrfs_trans_handle *trans,
static int clone_finish_inode_update(struct btrfs_trans_handle *trans,
@@ -3779,17 +3697,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 +3719,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,21 +3809,25 @@ 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);
out_fput:
	fdput(src_file);
out_drop_write:
	mnt_drop_write_file(file);
	return ret;
	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)))
	ret = btrfs_clone_files(file_out, file_in, pos_in, len, pos_out);
		return -EFAULT;
	if (ret == 0)
	return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
		ret = len;
				 args.src_length, args.dest_offset);
	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);
}
}


/*
/*
@@ -5485,10 +5377,6 @@ long btrfs_ioctl(struct file *file, unsigned int
		return btrfs_ioctl_dev_info(root, argp);
		return btrfs_ioctl_dev_info(root, argp);
	case BTRFS_IOC_BALANCE:
	case BTRFS_IOC_BALANCE:
		return btrfs_ioctl_balance(file, NULL);
		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:
	case BTRFS_IOC_TRANS_START:
		return btrfs_ioctl_trans_start(file);
		return btrfs_ioctl_trans_start(file);
	case BTRFS_IOC_TRANS_END:
	case BTRFS_IOC_TRANS_END:
@@ -5566,8 +5454,6 @@ long btrfs_ioctl(struct file *file, unsigned int
		return btrfs_ioctl_get_fslabel(file, argp);
		return btrfs_ioctl_get_fslabel(file, argp);
	case BTRFS_IOC_SET_FSLABEL:
	case BTRFS_IOC_SET_FSLABEL:
		return btrfs_ioctl_set_fslabel(file, argp);
		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:
	case BTRFS_IOC_GET_SUPPORTED_FEATURES:
		return btrfs_ioctl_get_supported_features(file, argp);
		return btrfs_ioctl_get_supported_features(file, argp);
	case BTRFS_IOC_GET_FEATURES:
	case BTRFS_IOC_GET_FEATURES:
Loading