Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp462357pxb; Wed, 27 Jan 2021 12:03:29 -0800 (PST) X-Google-Smtp-Source: ABdhPJzbWLDDngiv5T7utHCdeJBH09E4ZoCiX14HWgYWaJz++4MPwodXzs/zjM8qIHSXGuf63As7 X-Received: by 2002:a17:906:68d0:: with SMTP id y16mr8498568ejr.128.1611777809000; Wed, 27 Jan 2021 12:03:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1611777808; cv=none; d=google.com; s=arc-20160816; b=JtN7gp61wb0hcfkI4WOKVqwFukFy5IibZTvqDI1AH7KEUhIBuSaPXajvWT1vj1VP5n mjyDwBpZzpcQSxDg2jC92Fy7okC8S/pQNXe3rs1Xs59wa/68WkIAoxIGtcFPg2KdJhd4 TbjHTk5zL+ciZ5+Yt9c96VojX7vei3aAS9HMKBqdFuHVU0fblXjD1UpeM4D0DR54hIdh gbbgAwx8VA1bZkojmZWkoQUd9M/cxW2+7ECL9Xkc25G44Z2vZwu58ro0psNW9y1ZOQg9 LPux6rUSOiLXp/oHhysEAFdNio+IaLozF3blViuZCtVb+aaycz3789M6Xyl0UcwO3XIv OUXw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date; bh=ssi1dVqYoeusowoWTVz7ofiBYIhOYjOngkxNXh0M5WI=; b=T73apbncNu30fMeuCgj9p+XgLTUQVAli3/NP/dpHBACZNzJvpaQ21dBaXlQO0qltzH t5nnsqIzwc4XeuDfd1Wl3+bgjhMjtgmheKBzukaR1GCpYUMinW8UxfHjRyUb3ucnKJar ttQVywMHevU7/w43kdXuWRxeDDcAjg1P87iZgveAdPMzwo4OvM8yu1JZI5tozRrYESDY EnCIxJXzPLCOEnN9j4OaO6VZXT00cGCVyoZrjwgkLtSAYnjGQsjDZ9KyfW8PJ38kCung IWmMD+/qacjB8xhQP9A+lus2VGO/YRvRSayraZ7UIScb6RJZxq3K9vMJaTicRTsWLykN Zexw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id i11si1587254edy.517.2021.01.27.12.03.03; Wed, 27 Jan 2021 12:03:28 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237186AbhA0Dsp (ORCPT + 99 others); Tue, 26 Jan 2021 22:48:45 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57752 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389627AbhA0AG0 (ORCPT ); Tue, 26 Jan 2021 19:06:26 -0500 Received: from zeniv-ca.linux.org.uk (zeniv-ca.linux.org.uk [IPv6:2607:5300:60:148a::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BD2F9C0698C6; Tue, 26 Jan 2021 15:51:15 -0800 (PST) Received: from viro by zeniv-ca.linux.org.uk with local (Exim 4.94 #2 (Red Hat Linux)) id 1l4Y6o-006RKi-QP; Tue, 26 Jan 2021 23:50:55 +0000 Date: Tue, 26 Jan 2021 23:50:54 +0000 From: Al Viro To: Nicolas Boichat Cc: "Darrick J . Wong" , Dave Chinner , Luis Lozano , Ian Lance Taylor , Amir Goldstein , "Darrick J. Wong" , Dave Chinner , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] fs: generic_copy_file_checks: Do not adjust count based on file size Message-ID: <20210126235054.GN740243@zeniv-ca> References: <20210126135012.1.If45b7cdc3ff707bc1efa17f5366057d60603c45f@changeid> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210126135012.1.If45b7cdc3ff707bc1efa17f5366057d60603c45f@changeid> Sender: Al Viro Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Jan 26, 2021 at 01:50:22PM +0800, Nicolas Boichat wrote: > copy_file_range (which calls generic_copy_file_checks) uses the > inode file size to adjust the copy count parameter. This breaks > with special filesystems like procfs/sysfs, where the file size > appears to be zero, but content is actually returned when a read > operation is performed. > > This commit ignores the source file size, and makes copy_file_range > match the end of file behaviour documented in POSIX's "read", > where 0 is returned to mark EOF. This would allow "cp" and other > standard tools to make use of copy_file_range with the exact same > behaviour as they had in the past. Proper fix is _not_ to use copy_file_range(2) in cp(1) - it's really not universal and its implementation will *NOT* do the right thing for most of procfs files. Sequential read(2) (or splice(2), for that matter) will give you consistent output; copy_file_range(2) will not. What copy_file_range() does is splice from input to internal pipe + splice from that internal pipe to output, done in a loop. HOWEVER, a short write to output will discard the contents of the internal pipe and pretend we had a short _read_ instead. With ->f_pos updated accordingly, so that on the next call we'd start from the place right after everything we'd copied. However, that will result in implicit seek on the next call of seq_read(), with no promise whatsoever that you won't end up with consistent records in your copy. Short copy on read(2) will leave the rest of the record in buffer, so the next read(2) will pick that first. IOW, don't expect copy_file_range() to produce usable copies for those. Doesn't work. splice to explicit pipe + splice from that pipe (without flushing the sucker) works, and if you really want zero-copy you could use that. Again, use of copy_file_range(2) in cp(1) is a userland bug. Don't do it.