From patchwork Tue Aug 25 15:33:40 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peng Tao X-Patchwork-Id: 7071591 Return-Path: X-Original-To: patchwork-linux-nfs@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 7727CC05AD for ; Tue, 25 Aug 2015 15:35:17 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 6C705204EA for ; Tue, 25 Aug 2015 15:35:16 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 3D89F20898 for ; Tue, 25 Aug 2015 15:35:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932608AbbHYPfH (ORCPT ); Tue, 25 Aug 2015 11:35:07 -0400 Received: from mail-pa0-f42.google.com ([209.85.220.42]:34129 "EHLO mail-pa0-f42.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751924AbbHYPfE (ORCPT ); Tue, 25 Aug 2015 11:35:04 -0400 Received: by pabzx8 with SMTP id zx8so36942529pab.1 for ; Tue, 25 Aug 2015 08:35:03 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=OJTuJbyt/8inObAbBAjjxezjDHtfo06hyOPnmDDlKfQ=; b=MD6gkKV11hMadQ1HGYtd7jqe0NjthocNfUa+EcIzfFSYBgFYvHPa0gffvp/zWGeC/c uS2Jz30HEbNxpdixvYrW8bmeThTbJ/yF6Bg6o9ldXrvnljgiGxF4P75C8Vrm3BPSgSAX e9vRIf3i2/2h8r2MnTk9wdG1sBIwa0nC9uKupWxOETTtlaGxW78CFHm+hMSgyT7VQJKM BZjrv0Lf5utDSI9blKERlP0LXHxpUD6qMpduFuGkdkzIg35KuLDAjo7T7cCq0thGbgp4 dmZvf2ZOkqdDoYNvlgaO/498QNk2KxqfzogPvb3gVmSVNEE+0cnm8oOD3z1pVHqaWKUN IMuw== X-Gm-Message-State: ALoCoQkHJOCtwjv6VeNC5DRDx+z2GMzerEVTUOZEIPztkQLAGaik7SitEc7FXREqc3Gqg1gpFzgk X-Received: by 10.66.241.130 with SMTP id wi2mr57734632pac.130.1440516902985; Tue, 25 Aug 2015 08:35:02 -0700 (PDT) Received: from lear.localdomain (ec2-54-65-164-9.ap-northeast-1.compute.amazonaws.com. [54.65.164.9]) by smtp.gmail.com with ESMTPSA id hu13sm21462120pdb.72.2015.08.25.08.34.58 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 25 Aug 2015 08:35:02 -0700 (PDT) From: Peng Tao To: linux-nfs@vger.kernel.org Cc: Trond Myklebust , Anna Schumaker , Christoph Hellwig , Zach Brown , Darren Hart , bfields@fieldses.org, Jeff Layton , Peng Tao , linux-btrfs@vger.kernel.org, linux-fsdevel@vger.kernel.org Subject: [PATCH RFC 02/11] vfs/btrfs: add .clone_range file operation Date: Tue, 25 Aug 2015 23:33:40 +0800 Message-Id: <1440516829-116041-3-git-send-email-tao.peng@primarydata.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1440516829-116041-1-git-send-email-tao.peng@primarydata.com> References: <1440516829-116041-1-git-send-email-tao.peng@primarydata.com> Sender: linux-nfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-nfs@vger.kernel.org X-Spam-Status: No, score=-8.2 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Also add vfs callers in do_vfs_ioctl(). Now btrfs CLONE ioctl goes through vfs_file_clone_range(). Cc: linux-btrfs@vger.kernel.org Cc: linux-fsdevel@vger.kernel.org Signed-off-by: Peng Tao --- fs/btrfs/ctree.h | 2 ++ fs/btrfs/file.c | 1 + fs/btrfs/ioctl.c | 68 +++++----------------------------------------- fs/ioctl.c | 6 ++++ include/uapi/linux/btrfs.h | 10 ------- 5 files changed, 16 insertions(+), 71 deletions(-) diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h index aac314e..af3e224 100644 --- a/fs/btrfs/ctree.h +++ b/fs/btrfs/ctree.h @@ -3969,6 +3969,8 @@ void btrfs_get_block_group_info(struct list_head *groups_list, struct btrfs_ioctl_space_info *space); void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock, struct btrfs_ioctl_balance_args *bargs); +int btrfs_file_clone_range(struct file *src_file, struct file *dst_file, + loff_t off, size_t olen, loff_t destoff); /* file.c */ diff --git a/fs/btrfs/file.c b/fs/btrfs/file.c index b823fac..c9170fd 100644 --- a/fs/btrfs/file.c +++ b/fs/btrfs/file.c @@ -2813,6 +2813,7 @@ const struct file_operations btrfs_file_operations = { .fsync = btrfs_sync_file, .fallocate = btrfs_fallocate, .unlocked_ioctl = btrfs_ioctl, + .clone_range = btrfs_file_clone_range, #ifdef CONFIG_COMPAT .compat_ioctl = btrfs_ioctl, #endif diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c index 0770c91..3d026c8 100644 --- a/fs/btrfs/ioctl.c +++ b/fs/btrfs/ioctl.c @@ -3719,13 +3719,12 @@ out: return ret; } -static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd, - u64 off, u64 olen, u64 destoff) +int btrfs_file_clone_range(struct file *src_file, struct file *dst_file, + loff_t off, size_t olen, loff_t destoff) { - struct inode *inode = file_inode(file); + struct inode *inode = file_inode(dst_file); struct btrfs_root *root = BTRFS_I(inode)->root; - struct fd src_file; - struct inode *src; + struct inode *src = file_inode(src_file); int ret; u64 len = olen; u64 bs = root->fs_info->sb->s_blocksize; @@ -3742,49 +3741,16 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd, * 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; - /* 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; - - ret = -EISDIR; - if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode)) - goto out_fput; + return -EINVAL; - ret = -EXDEV; - if (src->i_sb != inode->i_sb) - goto out_fput; + if (src == inode) + same_inode = 1; if (!same_inode) { if (inode < src) { @@ -3800,8 +3766,6 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd, /* determine range to clone */ ret = -EINVAL; - if (off + len > src->i_size || off + len < off) - goto out_unlock; if (len == 0) olen = len = src->i_size - off; /* if we extend to eof, continue to block boundary */ @@ -3877,23 +3841,9 @@ out_unlock: } else { mutex_unlock(&src->i_mutex); } -out_fput: - fdput(src_file); -out_drop_write: - mnt_drop_write_file(file); return ret; } -static long btrfs_ioctl_clone_range(struct file *file, void __user *argp) -{ - struct btrfs_ioctl_clone_range_args args; - - 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); -} - /* * there are many ways the trans_start and trans_end ioctls can lead * to deadlocks. They should only be used by applications that @@ -5433,10 +5383,6 @@ long btrfs_ioctl(struct file *file, unsigned int 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: diff --git a/fs/ioctl.c b/fs/ioctl.c index 726c5d7..41c6080 100644 --- a/fs/ioctl.c +++ b/fs/ioctl.c @@ -624,6 +624,12 @@ int do_vfs_ioctl(struct file *filp, unsigned int fd, unsigned int cmd, case FIGETBSZ: return put_user(inode->i_sb->s_blocksize, argp); + case FICLONE: + return ioctl_file_clone(filp, arg, 0, 0, 0); + + case FICLONERANGE: + return ioctl_file_clone_range(filp, argp); + default: if (S_ISREG(inode->i_mode)) error = file_ioctl(filp, cmd, arg); diff --git a/include/uapi/linux/btrfs.h b/include/uapi/linux/btrfs.h index b6dec05..c3a23d3 100644 --- a/include/uapi/linux/btrfs.h +++ b/include/uapi/linux/btrfs.h @@ -316,12 +316,6 @@ struct btrfs_ioctl_search_args_v2 { __u64 buf[0]; /* out - found items */ }; -struct btrfs_ioctl_clone_range_args { - __s64 src_fd; - __u64 src_offset, src_length; - __u64 dest_offset; -}; - /* flags for the defrag range ioctl */ #define BTRFS_DEFRAG_RANGE_COMPRESS 1 #define BTRFS_DEFRAG_RANGE_START_IO 2 @@ -548,7 +542,6 @@ static inline char *btrfs_err_str(enum btrfs_err_code err_code) #define BTRFS_IOC_TRANS_END _IO(BTRFS_IOCTL_MAGIC, 7) #define BTRFS_IOC_SYNC _IO(BTRFS_IOCTL_MAGIC, 8) -#define BTRFS_IOC_CLONE _IOW(BTRFS_IOCTL_MAGIC, 9, int) #define BTRFS_IOC_ADD_DEV _IOW(BTRFS_IOCTL_MAGIC, 10, \ struct btrfs_ioctl_vol_args) #define BTRFS_IOC_RM_DEV _IOW(BTRFS_IOCTL_MAGIC, 11, \ @@ -556,9 +549,6 @@ static inline char *btrfs_err_str(enum btrfs_err_code err_code) #define BTRFS_IOC_BALANCE _IOW(BTRFS_IOCTL_MAGIC, 12, \ struct btrfs_ioctl_vol_args) -#define BTRFS_IOC_CLONE_RANGE _IOW(BTRFS_IOCTL_MAGIC, 13, \ - struct btrfs_ioctl_clone_range_args) - #define BTRFS_IOC_SUBVOL_CREATE _IOW(BTRFS_IOCTL_MAGIC, 14, \ struct btrfs_ioctl_vol_args) #define BTRFS_IOC_SNAP_DESTROY _IOW(BTRFS_IOCTL_MAGIC, 15, \