diff mbox series

ceph: fix write_begin optimization when write is beyond EOF

Message ID 20210611195904.160416-1-jlayton@kernel.org
State New
Headers show
Series ceph: fix write_begin optimization when write is beyond EOF | expand

Commit Message

Jeff Layton June 11, 2021, 7:59 p.m. UTC
It's not sufficient to skip reading when the pos is beyond the EOF.
There may be data at the head of the page that we need to fill in
before the write. Only elide the read if the pos is beyond the last page
in the file.

Cc: <stable@vger.kernel.org> # v5.10 .. v5.12
Fixes: 1cc1699070bd ("ceph: fold ceph_update_writeable_page into ceph_write_begin")
Reported-by: Andrew W Elble <aweits@rit.edu>
Signed-off-by: Jeff Layton <jlayton@kernel.org>
---
 fs/ceph/addr.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Note to stable maintainers: This is needed in v5.10.z - v5.12.z. In
v5.13, we've moved to using the new netfs_read_helper code so this isn't
necessary there.

I also now have a simple testcase for this that I'll submit to xfstests
early next week.

Comments

Matthew Wilcox June 11, 2021, 8:48 p.m. UTC | #1
On Fri, Jun 11, 2021 at 03:59:04PM -0400, Jeff Layton wrote:
>  		if ((pos_in_page == 0 && len == PAGE_SIZE) ||
> -		    (pos >= i_size_read(inode)) ||
> +		    (index > (i_size_read(inode) / PAGE_SIZE)) ||

I think that wants to be ((i_size_read(inode) - 1) / PAGE_SIZE)

If your file is 4096 bytes long, that means bytes 0-4095 contain data.
Except that i_size can be 0, so ...

		if ((offset == 0 && len == PAGE_SIZE) || i_size == 0 ||
		    (index > (i_size - 1) / PAGE_SIZE) ||
		    (offset == 0 && pos + len >= i_size))
  			zero_user_segments(page, 0, pos_in_page,
  					   pos_in_page + len, PAGE_SIZE);
Jeff Layton June 11, 2021, 10:20 p.m. UTC | #2
On Fri, 2021-06-11 at 21:48 +0100, Matthew Wilcox wrote:
> On Fri, Jun 11, 2021 at 03:59:04PM -0400, Jeff Layton wrote:
> >  		if ((pos_in_page == 0 && len == PAGE_SIZE) ||
> > -		    (pos >= i_size_read(inode)) ||
> > +		    (index > (i_size_read(inode) / PAGE_SIZE)) ||
> 
> I think that wants to be ((i_size_read(inode) - 1) / PAGE_SIZE)
> 
> If your file is 4096 bytes long, that means bytes 0-4095 contain data.
> Except that i_size can be 0, so ...
> 
> 		if ((offset == 0 && len == PAGE_SIZE) || i_size == 0 ||
> 		    (index > (i_size - 1) / PAGE_SIZE) ||
> 		    (offset == 0 && pos + len >= i_size))
>   			zero_user_segments(page, 0, pos_in_page,
>   					   pos_in_page + len, PAGE_SIZE);
> 

Oh, right -- I'll fix that and send a v2. Sorry for the noise!
diff mbox series

Patch

diff --git a/fs/ceph/addr.c b/fs/ceph/addr.c
index 26e66436f005..e636fb8275e1 100644
--- a/fs/ceph/addr.c
+++ b/fs/ceph/addr.c
@@ -1353,11 +1353,11 @@  static int ceph_write_begin(struct file *file, struct address_space *mapping,
 		/*
 		 * In some cases we don't need to read at all:
 		 * - full page write
-		 * - write that lies completely beyond EOF
+		 * - write that lies in a page that is completely beyond EOF
 		 * - write that covers the the page from start to EOF or beyond it
 		 */
 		if ((pos_in_page == 0 && len == PAGE_SIZE) ||
-		    (pos >= i_size_read(inode)) ||
+		    (index > (i_size_read(inode) / PAGE_SIZE)) ||
 		    (pos_in_page == 0 && (pos + len) >= i_size_read(inode))) {
 			zero_user_segments(page, 0, pos_in_page,
 					   pos_in_page + len, PAGE_SIZE);