linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Al Viro <viro@zeniv.linux.org.uk>
To: Nicolas Boichat <drinkcat@chromium.org>
Cc: "Darrick J . Wong" <djwong@kernel.org>,
	Dave Chinner <david@fromorbit.com>,
	Luis Lozano <llozano@chromium.org>,
	Ian Lance Taylor <iant@google.com>,
	Amir Goldstein <amir73il@gmail.com>,
	"Darrick J. Wong" <darrick.wong@oracle.com>,
	Dave Chinner <dchinner@redhat.com>,
	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
Date: Tue, 26 Jan 2021 23:50:54 +0000	[thread overview]
Message-ID: <20210126235054.GN740243@zeniv-ca> (raw)
In-Reply-To: <20210126135012.1.If45b7cdc3ff707bc1efa17f5366057d60603c45f@changeid>

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.

      parent reply	other threads:[~2021-01-27  4:46 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-26  5:50 [PATCH] fs: generic_copy_file_checks: Do not adjust count based on file size Nicolas Boichat
2021-01-26 23:38 ` Dave Chinner
2021-01-28  0:46   ` Nicolas Boichat
2021-01-28  5:57     ` Darrick J. Wong
2021-02-12  4:48       ` Nicolas Boichat
2021-01-26 23:50 ` Al Viro [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210126235054.GN740243@zeniv-ca \
    --to=viro@zeniv.linux.org.uk \
    --cc=amir73il@gmail.com \
    --cc=darrick.wong@oracle.com \
    --cc=david@fromorbit.com \
    --cc=dchinner@redhat.com \
    --cc=djwong@kernel.org \
    --cc=drinkcat@chromium.org \
    --cc=iant@google.com \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=llozano@chromium.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).