Received: by 2002:a5d:9c59:0:0:0:0:0 with SMTP id 25csp612959iof; Mon, 6 Jun 2022 09:22:50 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyVmGr0cUIXNFYYjR7On4efAPrvMT5JE3AWmx7yycbqcojVRaSMXwAwMTjxZc+hJXj+s/W+ X-Received: by 2002:a05:6a00:1902:b0:4f7:8813:b2cb with SMTP id y2-20020a056a00190200b004f78813b2cbmr25014984pfi.54.1654532570612; Mon, 06 Jun 2022 09:22:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654532570; cv=none; d=google.com; s=arc-20160816; b=Hj1v6TE/m+LqYr+t4WgmsitLJwOI/z/ol8UgOTfUR+QviK0tCuqhneOD1upSYziSYX 4mzEmbNWlIfY0USWuRJWqKXnz3GX2rZ3g4+ISAcETd+NVIEs9ITef/VtSnQBPxHqfZkq V4/XYbL9IxngN1ptdQ6KAWaP5UifrrEbPS8sq35nFckw/pO7QEvyfFYJ1dLf2oUS06UC vqWJRMOw2sSMAfTBhAo66L+MvZVa5wU4fe3piiBWOTgPZtGrLaxVE9LG0Sf7zVpbd5CV E0BRnD1GJX6uifZgsLaINqiJh27snKVDGcNH2DNlr0DAFge9gCK54/I5aGHcXbXtZL0O 77vw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:in-reply-to:date:references:subject:cc:to:from :dkim-signature:dkim-signature; bh=rl//JYrcZmwHLwJZh49b7uombIHcrj8h7hQNgtZaQFY=; b=an9Gp2guqDAEA/QWEKzk5+ZpcxtOohoDUoSbcgeSELl21lLyXmBS0NPuW7tIKe472t ukCgVtvoJ1VfC+H/f7PUEPwLaMS3wfEWmR/h9EMheYl1C/HSh0PXiV71p9WBEF5iHRrI bK31NA2xXb+7XazA9yFaKouKfPQlroC2SDK9eqM1+1kDgf3+ZuqWy3y+7rENpbQqCb3R xPK8rIehm2I5VgtQH9LF28SYhGP80TWr4Zdkmy+fb+Sbr8pOgZ6SmS3P+jA63XaPnRvq nP31FmRI8/Stb4kXK5uGcsMs4ZzqbDxcpTBYsoiymz5Zb3dWZ/ILGeS9UhN9PN2PNysV Nf9g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=U8IQx0W5; dkim=neutral (no key) header.i=@suse.de; spf=pass (google.com: domain of linux-nfs-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-nfs-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id d24-20020a170902aa9800b0015cd18f032fsi19939076plr.155.2022.06.06.09.22.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Jun 2022 09:22:50 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-nfs-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=U8IQx0W5; dkim=neutral (no key) header.i=@suse.de; spf=pass (google.com: domain of linux-nfs-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-nfs-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 669ED1A04AC; Mon, 6 Jun 2022 09:06:16 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241502AbiFFQGP (ORCPT + 99 others); Mon, 6 Jun 2022 12:06:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44514 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241569AbiFFQGO (ORCPT ); Mon, 6 Jun 2022 12:06:14 -0400 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5D4E219FD59; Mon, 6 Jun 2022 09:06:12 -0700 (PDT) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id F34B91F9F1; Mon, 6 Jun 2022 16:06:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1654531571; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=rl//JYrcZmwHLwJZh49b7uombIHcrj8h7hQNgtZaQFY=; b=U8IQx0W53xJpXcGNyjyPUygD70HMHletCF96etaOIOcjNR2LlMkUgyii6BjETRphbdKOZ4 uy+lZ94myf0+Bg6+HgtKpepGGXy0deHBAO3MG7MOtvs70Rp63rqSa8pjoLkm9zWErj+Vzg A0EHubFmRlw/9risk0CaWE06iyFqMsI= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1654531571; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=rl//JYrcZmwHLwJZh49b7uombIHcrj8h7hQNgtZaQFY=; b=CIqitOzRRkzlpwJgRgACUQbLw5qkEN1htP7YM+fGmeVzuOxUuCy9aa4kC8GUjbQg632QE9 cLYzSUzcnUDETgAw== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 2D76813A5F; Mon, 6 Jun 2022 16:06:10 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id EVNiCPIlnmKKAgAAMHmgww (envelope-from ); Mon, 06 Jun 2022 16:06:10 +0000 Received: from localhost (brahms.olymp [local]) by brahms.olymp (OpenSMTPD) with ESMTPA id a5d56dd6; Mon, 6 Jun 2022 16:06:51 +0000 (UTC) From: =?utf-8?Q?Lu=C3=ADs_Henriques?= To: Amir Goldstein Cc: Steve French , Namjae Jeon , Olga Kornievskaia , Luis Henriques , Al Viro , Dave Chinner , "Darrick J . Wong" , He Zhe , Paul Gortmaker , linux-fsdevel@vger.kernel.org, linux-nfs@vger.kernel.org, linux-cifs@vger.kernel.org, Nicolas Boichat , kernel test robot Subject: Re: [PATCH v14] vfs: fix copy_file_range() regression in cross-fs copies References: <20220606134608.684131-1-amir73il@gmail.com> Date: Mon, 06 Jun 2022 17:06:51 +0100 In-Reply-To: <20220606134608.684131-1-amir73il@gmail.com> (Amir Goldstein's message of "Mon, 6 Jun 2022 16:46:08 +0300") Message-ID: <87leu9ai90.fsf@brahms.olymp> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-nfs@vger.kernel.org Amir Goldstein writes: > From: Luis Henriques > > A regression has been reported by Nicolas Boichat, found while using the > copy_file_range syscall to copy a tracefs file. Before commit > 5dae222a5ff0 ("vfs: allow copy_file_range to copy across devices") the > kernel would return -EXDEV to userspace when trying to copy a file across > different filesystems. After this commit, the syscall doesn't fail anymo= re > and instead returns zero (zero bytes copied), as this file's content is > generated on-the-fly and thus reports a size of zero. > > Another regression has been reported by He Zhe - the assertion of > WARN_ON_ONCE(ret =3D=3D -EOPNOTSUPP) can be triggered from userspace when > copying from a sysfs file whose read operation may return -EOPNOTSUPP. > > Since we do not have test coverage for copy_file_range() between any > two types of filesystems, the best way to avoid these sort of issues > in the future is for the kernel to be more picky about filesystems that > are allowed to do copy_file_range(). > > This patch restores some cross-filesystem copy restrictions that existed > prior to commit 5dae222a5ff0 ("vfs: allow copy_file_range to copy across > devices"), namely, cross-sb copy is not allowed for filesystems that do > not implement ->copy_file_range(). > > Filesystems that do implement ->copy_file_range() have full control of > the result - if this method returns an error, the error is returned to > the user. Before this change this was only true for fs that did not > implement the ->remap_file_range() operation (i.e. nfsv3). > > Filesystems that implement only ->remap_file_range() (i.e. xfs) may still > fall-back to the generic_copy_file_range() implementation when the copy > is within the same sb, but filesystem cannot handle the reuqested copied > range. This helps the kernel can maintain a more consistent story about > which filesystems support copy_file_range(). > > nfsd and ksmbd servers are modified to fall-back to the > generic_copy_file_range() implementation in case vfs_copy_file_range() > fails with -EOPNOTSUPP or -EXDEV, which preserves behavior of > server-side-copy. > > fall-back to generic_copy_file_range() is not implemented for the smb > operation FSCTL_DUPLICATE_EXTENTS_TO_FILE, which is arguably a correct > change of behavior. > > Fixes: 5dae222a5ff0 ("vfs: allow copy_file_range to copy across devices") > Link: https://lore.kernel.org/linux-fsdevel/20210212044405.4120619-1-drin= kcat@chromium.org/ > Link: https://lore.kernel.org/linux-fsdevel/CANMq1KDZuxir2LM5jOTm0xx+BnvW= =3DZmpsG47CyHFJwnw7zSX6Q@mail.gmail.com/ > Link: https://lore.kernel.org/linux-fsdevel/20210126135012.1.If45b7cdc3ff= 707bc1efa17f5366057d60603c45f@changeid/ > Link: https://lore.kernel.org/linux-fsdevel/20210630161320.29006-1-lhenri= ques@suse.de/ > Reported-by: Nicolas Boichat > Reported-by: kernel test robot > Signed-off-by: Luis Henriques > Fixes: 64bf5ff58dff ("vfs: no fallback for ->copy_file_range") > Link: https://lore.kernel.org/linux-fsdevel/20f17f64-88cb-4e80-07c1-85cb9= 6c83619@windriver.com/ > Reported-by: He Zhe > Signed-off-by: Amir Goldstein > --- > > Hi Steve, Namje, > > I was going to ping Al about this patch when I remembered that we have > another kernel file server that supports server side copy and needs to > be adjusted. I also realized that v13 wrongly (?) falls back to > generic_copy_file_range() in nfs/smb client code. > > It would be great if you could review my ksmbd change and run the fstests > as below in your test setup. > > I tested knfsd with kvm-xfstests: > $ kvm-xfstests -c nfs -g copy_range > ... > nfs/loopback: 11 tests, 2 skipped, 28 seconds > generic/430 Pass 3s > generic/431 Pass 4s > generic/432 Pass 3s > generic/433 Pass 4s > generic/434 Pass 3s > generic/553 Skipped 1s > generic/564 Pass 3s > generic/565 Pass 4s > generic/629 Skipped 1s > Totals: 9 tests, 2 skipped, 0 failures, 0 errors, 26s > > These tests were run when local server fs is ext4 (no clone support) > and when local server fs is xfs (clone support, but not cross-sb clone), > which is relevant for cross-fs copy test generic/565. > > It would be great if someone could add smb config support to kvm-xfstests > following the existing nfs/loopback config as reference. > > I rather make this change to copy_file_range() syscall and nfsd/ksmbd > with a single patch, so I will be waiting for your review/test. > > Olga, > > It would be great if you can verify my test results and also test this > patch with nfsd server-side-copy across different combination of exported > fs. It would also be great if you can ack that the behavior change of > "no fall back to generic_copy_file_range() in nfs42 client" is desired. > > Luis, > > I did not added your Tested-by and RVB from v13, because the patch had > changed. Note that you are still the author of the patch, as I felt there > is still more code in the patch from v12 than code that I have changed. > If you would like me to change that let me know. I'm OK either way, I'll just ask you to keep me on the CC list please. I haven't looked at this rev yet, I'll try to do so tomorrow, and run some tests against cephfs. Cheers, --=20 Lu=C3=ADs > > Thanks, > Amir. > > Changes since v13 [1]: > - Rebased and tested over 5.19-rc1 > - Never fallback from ->copy_file_range() to generic_copy_file_range() > - Added fallback to generic_copy_file_range() in ksmbd > - Typo fixes in commit message and comments > > [1] https://lore.kernel.org/linux-fsdevel/20220520082111.2066400-1-amir73= il@gmail.com/ > > fs/ksmbd/smb2pdu.c | 16 ++++++++-- > fs/ksmbd/vfs.c | 4 +++ > fs/nfsd/vfs.c | 8 ++++- > fs/read_write.c | 77 ++++++++++++++++++++++++++-------------------- > 4 files changed, 68 insertions(+), 37 deletions(-) > > diff --git a/fs/ksmbd/smb2pdu.c b/fs/ksmbd/smb2pdu.c > index e6f4ccc12f49..17f42f5b02fe 100644 > --- a/fs/ksmbd/smb2pdu.c > +++ b/fs/ksmbd/smb2pdu.c > @@ -7806,14 +7806,24 @@ int smb2_ioctl(struct ksmbd_work *work) > src_off =3D le64_to_cpu(dup_ext->SourceFileOffset); > dst_off =3D le64_to_cpu(dup_ext->TargetFileOffset); > length =3D le64_to_cpu(dup_ext->ByteCount); > - cloned =3D vfs_clone_file_range(fp_in->filp, src_off, fp_out->filp, > - dst_off, length, 0); > + /* > + * XXX: It is not clear if FSCTL_DUPLICATE_EXTENTS_TO_FILE > + * should fall back to vfs_copy_file_range(). This could be > + * beneficial when re-exporting nfs/smb mount, but note that > + * this can result in partial copy that returns an error status. > + * If/when FSCTL_DUPLICATE_EXTENTS_TO_FILE_EX is implemented, > + * fall back to vfs_copy_file_range(), should be avoided when > + * the flag DUPLICATE_EXTENTS_DATA_EX_SOURCE_ATOMIC is set. > + */ > + cloned =3D vfs_clone_file_range(fp_in->filp, src_off, > + fp_out->filp, dst_off, length, 0); > if (cloned =3D=3D -EXDEV || cloned =3D=3D -EOPNOTSUPP) { > ret =3D -EOPNOTSUPP; > goto dup_ext_out; > } else if (cloned !=3D length) { > cloned =3D vfs_copy_file_range(fp_in->filp, src_off, > - fp_out->filp, dst_off, length, 0); > + fp_out->filp, dst_off, > + length, 0); > if (cloned !=3D length) { > if (cloned < 0) > ret =3D cloned; > diff --git a/fs/ksmbd/vfs.c b/fs/ksmbd/vfs.c > index dcdd07c6efff..8d57347231ce 100644 > --- a/fs/ksmbd/vfs.c > +++ b/fs/ksmbd/vfs.c > @@ -1777,6 +1777,10 @@ int ksmbd_vfs_copy_file_ranges(struct ksmbd_work *= work, >=20=20 > ret =3D vfs_copy_file_range(src_fp->filp, src_off, > dst_fp->filp, dst_off, len, 0); > + if (ret =3D=3D -EOPNOTSUPP || ret =3D=3D -EXDEV) > + ret =3D generic_copy_file_range(src_fp->filp, src_off, > + dst_fp->filp, dst_off, > + len, 0); > if (ret < 0) > return ret; >=20=20 > diff --git a/fs/nfsd/vfs.c b/fs/nfsd/vfs.c > index 840e3af63a6f..b764213bcc55 100644 > --- a/fs/nfsd/vfs.c > +++ b/fs/nfsd/vfs.c > @@ -577,6 +577,7 @@ __be32 nfsd4_clone_file_range(struct svc_rqst *rqstp, > ssize_t nfsd_copy_file_range(struct file *src, u64 src_pos, struct file = *dst, > u64 dst_pos, u64 count) > { > + ssize_t ret; >=20=20 > /* > * Limit copy to 4MB to prevent indefinitely blocking an nfsd > @@ -587,7 +588,12 @@ ssize_t nfsd_copy_file_range(struct file *src, u64 s= rc_pos, struct file *dst, > * limit like this and pipeline multiple COPY requests. > */ > count =3D min_t(u64, count, 1 << 22); > - return vfs_copy_file_range(src, src_pos, dst, dst_pos, count, 0); > + ret =3D vfs_copy_file_range(src, src_pos, dst, dst_pos, count, 0); > + > + if (ret =3D=3D -EOPNOTSUPP || ret =3D=3D -EXDEV) > + ret =3D generic_copy_file_range(src, src_pos, dst, dst_pos, > + count, 0); > + return ret; > } >=20=20 > __be32 nfsd4_vfs_fallocate(struct svc_rqst *rqstp, struct svc_fh *fhp, > diff --git a/fs/read_write.c b/fs/read_write.c > index b1b1cdfee9d3..f7bcca1bf0e2 100644 > --- a/fs/read_write.c > +++ b/fs/read_write.c > @@ -1397,28 +1397,6 @@ ssize_t generic_copy_file_range(struct file *file_= in, loff_t pos_in, > } > EXPORT_SYMBOL(generic_copy_file_range); >=20=20 > -static ssize_t do_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) > -{ > - /* > - * Although we now allow filesystems to handle cross sb copy, passing > - * a file of the wrong filesystem type to filesystem driver can result > - * in an attempt to dereference the wrong type of ->private_data, so > - * avoid doing that until we really have a good reason. NFS defines > - * several different file_system_type structures, but they all end up > - * using the same ->copy_file_range() function pointer. > - */ > - if (file_out->f_op->copy_file_range && > - file_out->f_op->copy_file_range =3D=3D file_in->f_op->copy_file_ran= ge) > - return file_out->f_op->copy_file_range(file_in, pos_in, > - file_out, pos_out, > - len, flags); > - > - return generic_copy_file_range(file_in, pos_in, file_out, pos_out, len, > - flags); > -} > - > /* > * Performs necessary checks before doing a file copy > * > @@ -1440,6 +1418,27 @@ static int generic_copy_file_checks(struct file *f= ile_in, loff_t pos_in, > if (ret) > return ret; >=20=20 > + /* > + * Although we now allow filesystems to handle cross sb copy, passing > + * a file of the wrong filesystem type to filesystem driver can result > + * in an attempt to dereference the wrong type of ->private_data, so > + * avoid doing that until we really have a good reason. > + * > + * nfs and cifs define several different file_system_type structures > + * and several different sets of file_operations, but they all end up > + * using the same ->copy_file_range() function pointer. > + */ > + if (file_out->f_op->copy_file_range) { > + if (file_in->f_op->copy_file_range !=3D > + file_out->f_op->copy_file_range) > + return -EXDEV; > + } else if (file_in->f_op->remap_file_range) { > + if (file_inode(file_in)->i_sb !=3D file_inode(file_out)->i_sb) > + return -EXDEV; > + } else { > + return -EOPNOTSUPP; > + } > + > /* Don't touch certain kinds of inodes */ > if (IS_IMMUTABLE(inode_out)) > return -EPERM; > @@ -1505,26 +1504,38 @@ ssize_t vfs_copy_file_range(struct file *file_in,= loff_t pos_in, > file_start_write(file_out); >=20=20 > /* > - * Try cloning first, this is supported by more file systems, and > - * more efficient if both clone and copy are supported (e.g. NFS). > + * Cloning is supported by more file systems, so we implement copy on > + * same sb using clone, but for filesystems where both clone and copy > + * are supported (e.g. nfs,cifs), we only call the copy method. > */ > + if (file_out->f_op->copy_file_range) { > + ret =3D file_out->f_op->copy_file_range(file_in, pos_in, > + file_out, pos_out, > + len, flags); > + goto done; > + } > + > if (file_in->f_op->remap_file_range && > file_inode(file_in)->i_sb =3D=3D file_inode(file_out)->i_sb) { > - loff_t cloned; > - > - cloned =3D file_in->f_op->remap_file_range(file_in, pos_in, > + ret =3D file_in->f_op->remap_file_range(file_in, pos_in, > file_out, pos_out, > min_t(loff_t, MAX_RW_COUNT, len), > REMAP_FILE_CAN_SHORTEN); > - if (cloned > 0) { > - ret =3D cloned; > + if (ret > 0) > goto done; > - } > } >=20=20 > - ret =3D do_copy_file_range(file_in, pos_in, file_out, pos_out, len, > - flags); > - WARN_ON_ONCE(ret =3D=3D -EOPNOTSUPP); > + /* > + * We can get here if filesystem supports clone but rejected the clone > + * request (e.g. because it was not block aligned). > + * In that case, fall back to kernel copy so we are able to maintain a > + * consistent story about which filesystems support copy_file_range() > + * and which filesystems do not, that will allow userspace tools to > + * make consistent desicions w.r.t using copy_file_range(). > + */ > + ret =3D generic_copy_file_range(file_in, pos_in, file_out, pos_out, len, > + flags); > + > done: > if (ret > 0) { > fsnotify_access(file_in); > --=20 > > 2.25.1 >