NFS: Convert nfs_write_begin/end to use folios

Add a helper nfs_folio_grab_cache_write_begin() that can call
__filemap_get_folio() directly with the appropriate parameters.

Since write_begin()/write_end() take struct page arguments, just pass
the folio->page back for now.

Signed-off-by: Trond Myklebust <trond.myklebust@hammerspace.com>
Signed-off-by: Anna Schumaker <Anna.Schumaker@Netapp.com>
This commit is contained in:
Trond Myklebust 2023-01-19 16:33:45 -05:00 committed by Anna Schumaker
parent 4cbf76948c
commit 54d99381b7

View file

@ -276,27 +276,28 @@ EXPORT_SYMBOL_GPL(nfs_file_fsync);
* and that the new data won't completely replace the old data in * and that the new data won't completely replace the old data in
* that range of the file. * that range of the file.
*/ */
static bool nfs_full_page_write(struct page *page, loff_t pos, unsigned int len) static bool nfs_folio_is_full_write(struct folio *folio, loff_t pos,
unsigned int len)
{ {
unsigned int pglen = nfs_page_length(page); unsigned int pglen = nfs_folio_length(folio);
unsigned int offset = pos & (PAGE_SIZE - 1); unsigned int offset = offset_in_folio(folio, pos);
unsigned int end = offset + len; unsigned int end = offset + len;
return !pglen || (end >= pglen && !offset); return !pglen || (end >= pglen && !offset);
} }
static bool nfs_want_read_modify_write(struct file *file, struct page *page, static bool nfs_want_read_modify_write(struct file *file, struct folio *folio,
loff_t pos, unsigned int len) loff_t pos, unsigned int len)
{ {
/* /*
* Up-to-date pages, those with ongoing or full-page write * Up-to-date pages, those with ongoing or full-page write
* don't need read/modify/write * don't need read/modify/write
*/ */
if (PageUptodate(page) || PagePrivate(page) || if (folio_test_uptodate(folio) || folio_test_private(folio) ||
nfs_full_page_write(page, pos, len)) nfs_folio_is_full_write(folio, pos, len))
return false; return false;
if (pnfs_ld_read_whole_page(file->f_mapping->host)) if (pnfs_ld_read_whole_page(file_inode(file)))
return true; return true;
/* Open for reading too? */ /* Open for reading too? */
if (file->f_mode & FMODE_READ) if (file->f_mode & FMODE_READ)
@ -304,6 +305,15 @@ static bool nfs_want_read_modify_write(struct file *file, struct page *page,
return false; return false;
} }
static struct folio *
nfs_folio_grab_cache_write_begin(struct address_space *mapping, pgoff_t index)
{
unsigned fgp_flags = FGP_LOCK | FGP_WRITE | FGP_CREAT | FGP_STABLE;
return __filemap_get_folio(mapping, index, fgp_flags,
mapping_gfp_mask(mapping));
}
/* /*
* This does the "real" work of the write. We must allocate and lock the * This does the "real" work of the write. We must allocate and lock the
* page to be sent back to the generic routine, which then copies the * page to be sent back to the generic routine, which then copies the
@ -313,34 +323,31 @@ static bool nfs_want_read_modify_write(struct file *file, struct page *page,
* increment the page use counts until he is done with the page. * increment the page use counts until he is done with the page.
*/ */
static int nfs_write_begin(struct file *file, struct address_space *mapping, static int nfs_write_begin(struct file *file, struct address_space *mapping,
loff_t pos, unsigned len, loff_t pos, unsigned len, struct page **pagep,
struct page **pagep, void **fsdata) void **fsdata)
{ {
int ret;
pgoff_t index = pos >> PAGE_SHIFT;
struct page *page;
struct folio *folio; struct folio *folio;
int once_thru = 0; int once_thru = 0;
int ret;
dfprintk(PAGECACHE, "NFS: write_begin(%pD2(%lu), %u@%lld)\n", dfprintk(PAGECACHE, "NFS: write_begin(%pD2(%lu), %u@%lld)\n",
file, mapping->host->i_ino, len, (long long) pos); file, mapping->host->i_ino, len, (long long) pos);
start: start:
page = grab_cache_page_write_begin(mapping, index); folio = nfs_folio_grab_cache_write_begin(mapping, pos >> PAGE_SHIFT);
if (!page) if (!folio)
return -ENOMEM; return -ENOMEM;
*pagep = page; *pagep = &folio->page;
folio = page_folio(page);
ret = nfs_flush_incompatible(file, folio); ret = nfs_flush_incompatible(file, folio);
if (ret) { if (ret) {
unlock_page(page); folio_unlock(folio);
put_page(page); folio_put(folio);
} else if (!once_thru && } else if (!once_thru &&
nfs_want_read_modify_write(file, page, pos, len)) { nfs_want_read_modify_write(file, folio, pos, len)) {
once_thru = 1; once_thru = 1;
ret = nfs_read_folio(file, folio); ret = nfs_read_folio(file, folio);
put_page(page); folio_put(folio);
if (!ret) if (!ret)
goto start; goto start;
} }
@ -348,12 +355,12 @@ static int nfs_write_begin(struct file *file, struct address_space *mapping,
} }
static int nfs_write_end(struct file *file, struct address_space *mapping, static int nfs_write_end(struct file *file, struct address_space *mapping,
loff_t pos, unsigned len, unsigned copied, loff_t pos, unsigned len, unsigned copied,
struct page *page, void *fsdata) struct page *page, void *fsdata)
{ {
unsigned offset = pos & (PAGE_SIZE - 1);
struct nfs_open_context *ctx = nfs_file_open_context(file); struct nfs_open_context *ctx = nfs_file_open_context(file);
struct folio *folio = page_folio(page); struct folio *folio = page_folio(page);
unsigned offset = offset_in_folio(folio, pos);
int status; int status;
dfprintk(PAGECACHE, "NFS: write_end(%pD2(%lu), %u@%lld)\n", dfprintk(PAGECACHE, "NFS: write_end(%pD2(%lu), %u@%lld)\n",
@ -363,26 +370,26 @@ static int nfs_write_end(struct file *file, struct address_space *mapping,
* Zero any uninitialised parts of the page, and then mark the page * Zero any uninitialised parts of the page, and then mark the page
* as up to date if it turns out that we're extending the file. * as up to date if it turns out that we're extending the file.
*/ */
if (!PageUptodate(page)) { if (!folio_test_uptodate(folio)) {
unsigned pglen = nfs_page_length(page); size_t fsize = folio_size(folio);
unsigned pglen = nfs_folio_length(folio);
unsigned end = offset + copied; unsigned end = offset + copied;
if (pglen == 0) { if (pglen == 0) {
zero_user_segments(page, 0, offset, folio_zero_segments(folio, 0, offset, end, fsize);
end, PAGE_SIZE); folio_mark_uptodate(folio);
SetPageUptodate(page);
} else if (end >= pglen) { } else if (end >= pglen) {
zero_user_segment(page, end, PAGE_SIZE); folio_zero_segment(folio, end, fsize);
if (offset == 0) if (offset == 0)
SetPageUptodate(page); folio_mark_uptodate(folio);
} else } else
zero_user_segment(page, pglen, PAGE_SIZE); folio_zero_segment(folio, pglen, fsize);
} }
status = nfs_update_folio(file, folio, offset, copied); status = nfs_update_folio(file, folio, offset, copied);
unlock_page(page); folio_unlock(folio);
put_page(page); folio_put(folio);
if (status < 0) if (status < 0)
return status; return status;