Skip to content

Commit

Permalink
netfs: fix test for whether we can skip read when writing beyond EOF
Browse files Browse the repository at this point in the history
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.

Add a new helper function that corrects and clarifies the logic of
when we can skip reads, and have it only zero out the part of the page
that won't have data copied in for the write.

Finally, don't set the page Uptodate after zeroing. It's not up to date
since the write data won't have been copied in yet.

[DH made the following changes:

 - Prefixed the new function with "netfs_".

 - Don't call zero_user_segments() for a full-page write.

 - Altered the beyond-last-page check to avoid a DIV instruction and got
   rid of then-redundant zero-length file check.
]

Fixes: e1b1240 ("netfs: Add write_begin helper")
Reported-by: Andrew W Elble <[email protected]>
Signed-off-by: Jeff Layton <[email protected]>
Signed-off-by: David Howells <[email protected]>
Reviewed-by: Matthew Wilcox (Oracle) <[email protected]>
cc: [email protected]
Link: https://lore.kernel.org/r/[email protected]/
Link: https://lore.kernel.org/r/162367683365.460125.4467036947364047314.stgit@warthog.procyon.org.uk/ # v1
Link: https://lore.kernel.org/r/162391826758.1173366.11794946719301590013.stgit@warthog.procyon.org.uk/ # v2
  • Loading branch information
jtlayton authored and dhowells committed Jun 21, 2021
1 parent 66e9c6a commit 827a746
Showing 1 changed file with 36 additions and 13 deletions.
49 changes: 36 additions & 13 deletions fs/netfs/read_helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -1011,20 +1011,50 @@ int netfs_readpage(struct file *file,
}
EXPORT_SYMBOL(netfs_readpage);

static void netfs_clear_thp(struct page *page)
/**
* netfs_skip_page_read - prep a page for writing without reading first
* @page: page being prepared
* @pos: starting position for the write
* @len: length of write
*
* In some cases, write_begin doesn't need to read at all:
* - full page write
* - 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 any of these criteria are met, then zero out the unwritten parts
* of the page and return true. Otherwise, return false.
*/
static bool netfs_skip_page_read(struct page *page, loff_t pos, size_t len)
{
unsigned int i;
struct inode *inode = page->mapping->host;
loff_t i_size = i_size_read(inode);
size_t offset = offset_in_thp(page, pos);

/* Full page write */
if (offset == 0 && len >= thp_size(page))
return true;

/* pos beyond last page in the file */
if (pos - offset >= i_size)
goto zero_out;

/* Write that covers from the start of the page to EOF or beyond */
if (offset == 0 && (pos + len) >= i_size)
goto zero_out;

for (i = 0; i < thp_nr_pages(page); i++)
clear_highpage(page + i);
return false;
zero_out:
zero_user_segments(page, 0, offset, offset + len, thp_size(page));
return true;
}

/**
* netfs_write_begin - Helper to prepare for writing
* @file: The file to read from
* @mapping: The mapping to read from
* @pos: File position at which the write will begin
* @len: The length of the write in this page
* @len: The length of the write (may extend beyond the end of the page chosen)
* @flags: AOP_* flags
* @_page: Where to put the resultant page
* @_fsdata: Place for the netfs to store a cookie
Expand Down Expand Up @@ -1061,8 +1091,6 @@ int netfs_write_begin(struct file *file, struct address_space *mapping,
struct inode *inode = file_inode(file);
unsigned int debug_index = 0;
pgoff_t index = pos >> PAGE_SHIFT;
int pos_in_page = pos & ~PAGE_MASK;
loff_t size;
int ret;

DEFINE_READAHEAD(ractl, file, NULL, mapping, index);
Expand Down Expand Up @@ -1090,13 +1118,8 @@ int netfs_write_begin(struct file *file, struct address_space *mapping,
* within the cache granule containing the EOF, in which case we need
* to preload the granule.
*/
size = i_size_read(inode);
if (!ops->is_cache_enabled(inode) &&
((pos_in_page == 0 && len == thp_size(page)) ||
(pos >= size) ||
(pos_in_page == 0 && (pos + len) >= size))) {
netfs_clear_thp(page);
SetPageUptodate(page);
netfs_skip_page_read(page, pos, len)) {
netfs_stat(&netfs_n_rh_write_zskip);
goto have_page_no_wait;
}
Expand Down

0 comments on commit 827a746

Please sign in to comment.