From patchwork Sat Oct 24 23:17:10 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peng Tao X-Patchwork-Id: 7480411 Return-Path: X-Original-To: patchwork-linux-btrfs@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 A6214BEEA4 for ; Sat, 24 Oct 2015 15:29:34 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id AFE47206EF for ; Sat, 24 Oct 2015 15:29:33 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id B1C01207E9 for ; Sat, 24 Oct 2015 15:29:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752507AbbJXP3Z (ORCPT ); Sat, 24 Oct 2015 11:29:25 -0400 Received: from mail-pa0-f44.google.com ([209.85.220.44]:36185 "EHLO mail-pa0-f44.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752569AbbJXP3J (ORCPT ); Sat, 24 Oct 2015 11:29:09 -0400 Received: by pacfv9 with SMTP id fv9so151044496pac.3 for ; Sat, 24 Oct 2015 08:29:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=primarydata_com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=wlTkKJX3feL1aAWguiKiJ+bozbUtjwTUISqW4irsS/c=; b=OJoxQXlZAn3M1HnLnUHeXIfC1uXVWVLbelx3wfq5RFkUEBR7nPCPKicdHLzsMh98ZE HVBblsIzusvi4UaPQ271F6Vi6x+kuIdEvOjJaF3wLARjJIwNUenscAeMjT6UpaNkeggr VSwvBMfN+bYplFsrTWRClsJ893vpqM3EgvM2M8awx/TOiz35H40BHhrzPH+SgkRh3g6f x8J0OKcq4PbdeabgUCzF2iDLqOrXQkek5p/k4hyU9jM4Q76yDzEuvaQksc09s2cwQ8ky aPjlFpAq7UpUq9W0KvmiPXvttmYOJat9Re3vg8ueXnmOkbyZgXz+iufVHUOGqHXyhyL7 y9Yg== 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=wlTkKJX3feL1aAWguiKiJ+bozbUtjwTUISqW4irsS/c=; b=PmxV/peyb5Yn51bQE/EOqE1jfKL9HQ7LqBJojgW/ykh654Q0udk3wWGGj/dDjogmrV R9PwX0F10LpQQmUt6yRJASeW+rIqSWAOO2PzfnMJCvx33sQs42Khi+sIOXgeWYH9Gljb Yc9prktbREU9f7G5mX2tLUVxvhKPVfCiZRQGope/aW2KeURirmOQUsP79Sr3q0lvNWwY SVJ182/qhRwEtVeychafXE9rpzelwi2HXBrlp1sgQEHX0TqVjiduKtEyR2BHoUi5QLef /dRJR8wNowTfvUw9glAXmqPAQ45HoxbUpM4YV8G84HIoeZ2ZiKAzo+uGk6hHozJKBnpx ic1w== X-Gm-Message-State: ALoCoQmPEYzKCjEVnLxE1CDJaegpG9HL9yLtVxnB6cVCkQCjHNyTzksmhFgh/BKTnVLP9pMyNM0O X-Received: by 10.68.221.133 with SMTP id qe5mr11902828pbc.93.1445700548627; Sat, 24 Oct 2015 08:29:08 -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 nj1sm24764918pbc.2.2015.10.24.08.29.01 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 24 Oct 2015 08:29:08 -0700 (PDT) From: Peng Tao To: linux-fsdevel@vger.kernel.org Cc: Trond Myklebust , Anna Schumaker , Christoph Hellwig , Zach Brown , Darren Hart , Jeff Layton , bfields@fieldses.org, "Darrick J. Wong" , viro@zeniv.linux.org.uk, linux-nfs@vger.kernel.org, linux-btrfs@vger.kernel.org, linux-cifs@vger.kernel.org, Steve French , Peng Tao Subject: [PATCH 3/9] nfs42: add .copy_file_range file operation Date: Sun, 25 Oct 2015 07:17:10 +0800 Message-Id: <1445728636-10109-4-git-send-email-tao.peng@primarydata.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1445728636-10109-1-git-send-email-tao.peng@primarydata.com> References: <1445728636-10109-1-git-send-email-tao.peng@primarydata.com> Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Spam-Status: No, score=-4.8 required=5.0 tests=BAYES_00, DATE_IN_FUTURE_06_12, DKIM_SIGNED, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, T_DKIM_INVALID, UNPARSEABLE_RELAY autolearn=ham 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 Signed-off-by: Peng Tao --- fs/nfs/nfs4file.c | 95 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 52 insertions(+), 43 deletions(-) diff --git a/fs/nfs/nfs4file.c b/fs/nfs/nfs4file.c index 4aa5719..f1924d8 100644 --- a/fs/nfs/nfs4file.c +++ b/fs/nfs/nfs4file.c @@ -194,15 +194,60 @@ static long nfs42_fallocate(struct file *filep, int mode, loff_t offset, loff_t return nfs42_proc_allocate(filep, offset, len); } +static ssize_t nfs42_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 inode *src_inode = file_inode(file_in); + struct inode *dst_inode = file_inode(file_out); + struct nfs_server *server = NFS_SERVER(src_inode); + unsigned int bs = server->clone_blksize; + int ret; + + /* check alignment w.r.t. clone_blksize */ + ret = -EINVAL; + if (bs) { + if (!IS_ALIGNED(pos_in, bs) || !IS_ALIGNED(pos_out, bs) || + (!IS_ALIGNED(len, bs) && + i_size_read(src_inode) != (pos_in + len))) + goto out; + } + + /* XXX: do we lock at all? what if server needs CB_RECALL_LAYOUT? */ + lock_two_nondirectories(src_inode, dst_inode); + + /* flush all pending writes on both src and dst so that server + * has the latest data + */ + ret = nfs_sync_inode(src_inode); + if (ret) + goto out_unlock; + ret = nfs_sync_inode(dst_inode); + if (ret) + goto out_unlock; + + ret = nfs42_proc_clone(file_in, file_out, pos_in, pos_out, len); + + /* truncate inode page cache of the dst range so that future + * reads can fetch new data from server + */ + if (!ret) + truncate_inode_pages_range(&dst_inode->i_data, pos_out, + pos_out + len - 1); + +out_unlock: + unlock_two_nondirectories(src_inode, dst_inode); +out: + return ret < 0 ? ret : len; +} + static noinline long nfs42_ioctl_clone(struct file *dst_file, unsigned long srcfd, u64 src_off, u64 dst_off, u64 count) { struct inode *dst_inode = file_inode(dst_file); - struct nfs_server *server = NFS_SERVER(dst_inode); struct fd src_file; struct inode *src_inode; - unsigned int bs = server->clone_blksize; int ret; /* dst file must be opened for writing */ @@ -240,48 +285,11 @@ nfs42_ioctl_clone(struct file *dst_file, unsigned long srcfd, src_inode->i_sb != dst_inode->i_sb) goto out_fput; - /* check alignment w.r.t. clone_blksize */ - ret = -EINVAL; - if (bs) { - if (!IS_ALIGNED(src_off, bs) || !IS_ALIGNED(dst_off, bs)) - goto out_fput; - if (!IS_ALIGNED(count, bs) && i_size_read(src_inode) != (src_off + count)) - goto out_fput; - } - - /* XXX: do we lock at all? what if server needs CB_RECALL_LAYOUT? */ - if (dst_inode < src_inode) { - mutex_lock_nested(&dst_inode->i_mutex, I_MUTEX_PARENT); - mutex_lock_nested(&src_inode->i_mutex, I_MUTEX_CHILD); - } else { - mutex_lock_nested(&src_inode->i_mutex, I_MUTEX_PARENT); - mutex_lock_nested(&dst_inode->i_mutex, I_MUTEX_CHILD); - } - - /* flush all pending writes on both src and dst so that server - * has the latest data */ - ret = nfs_sync_inode(src_inode); - if (ret) - goto out_unlock; - ret = nfs_sync_inode(dst_inode); - if (ret) - goto out_unlock; - - ret = nfs42_proc_clone(src_file.file, dst_file, src_off, dst_off, count); - - /* truncate inode page cache of the dst range so that future reads can fetch - * new data from server */ - if (!ret) - truncate_inode_pages_range(&dst_inode->i_data, dst_off, dst_off + count - 1); + ret = nfs42_copy_file_range(src_file.file, src_off, dst_file, + dst_off, count, 0); + if (ret > 0) + ret = 0; -out_unlock: - if (dst_inode < src_inode) { - mutex_unlock(&src_inode->i_mutex); - mutex_unlock(&dst_inode->i_mutex); - } else { - mutex_unlock(&dst_inode->i_mutex); - mutex_unlock(&src_inode->i_mutex); - } out_fput: fdput(src_file); out_drop_write: @@ -344,6 +352,7 @@ const struct file_operations nfs4_file_operations = { .splice_write = iter_file_splice_write, #ifdef CONFIG_NFS_V4_2 .fallocate = nfs42_fallocate, + .copy_file_range = nfs42_copy_file_range, #endif /* CONFIG_NFS_V4_2 */ .check_flags = nfs_check_flags, .setlease = simple_nosetlease,