[PATCH 10/14] iomap: Handle THPs when writing to pages

Matthew Wilcox (Oracle)
2020-10-14T09:21:05Z

If we come across a THP that is not uptodate when writing to the page
cache, this must be due to a readahead error, so behave the same way as
readpage and split it. Make sure to flush the right page after completing
the write. We still only copy up to a page boundary, so there's no need
to flush multiple pages at this time.

Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
---
fs/iomap/buffered-io.c | 20 +++++++++++++++++---
1 file changed, 17 insertions(+), 3 deletions(-)

diff --git a/fs/iomap/buffered-io.c b/fs/iomap/buffered-io.c
index 397795db3ce5..0a1fe7d1a27c 100644
--- a/fs/iomap/buffered-io.c
+++ b/fs/iomap/buffered-io.c
@@ -682,12 +682,19 @@ static ssize_t iomap_write_begin(struct inode *inode, loff_t pos, loff_t len,
return status;
}

+retry:
page = grab_cache_page_write_begin(inode->i_mapping, pos >> PAGE_SHIFT,
AOP_FLAG_NOFS);
if (!page) {
status = -ENOMEM;
goto out_no_page;
}
+ if (PageTransCompound(page) && !PageUptodate(page)) {
+ if (iomap_split_page(inode, page) == AOP_TRUNCATED_PAGE) {
+ put_page(page);
+ goto retry;
+ }
+ }
page = thp_head(page);
offset = offset_in_thp(page, pos);
if (len > thp_size(page) - offset)
@@ -724,6 +731,7 @@ iomap_set_page_dirty(struct page *page)
struct address_space *mapping = page_mapping(page);
int newly_dirty;

+ VM_BUG_ON_PGFLAGS(PageTail(page), page);
if (unlikely(!mapping))
return !TestSetPageDirty(page);

@@ -746,7 +754,9 @@ EXPORT_SYMBOL_GPL(iomap_set_page_dirty);
static size_t __iomap_write_end(struct inode *inode, loff_t pos, size_t len,
size_t copied, struct page *page)
{
- flush_dcache_page(page);
+ size_t offset = offset_in_thp(page, pos);
+
+ flush_dcache_page(page + offset / PAGE_SIZE);

/*
* The blocks that were entirely written will now be uptodate, so we
@@ -761,7 +771,7 @@ static size_t __iomap_write_end(struct inode *inode, loff_t pos, size_t len,
*/
if (unlikely(copied < len && !PageUptodate(page)))
return 0;
- iomap_set_range_uptodate(page, offset_in_page(pos), len);
+ iomap_set_range_uptodate(page, offset, len);
iomap_set_page_dirty(page);
return copied;
}
@@ -837,6 +847,10 @@ iomap_write_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
unsigned long bytes; /* Bytes to write to page */
size_t copied; /* Bytes copied from user */

+ /*
+ * XXX: We don't know what size page we'll find in the
+ * page cache, so only copy up to a regular page boundary.
+ */
offset = offset_in_page(pos);
bytes = min_t(unsigned long, PAGE_SIZE - offset,
iov_iter_count(i));
@@ -867,7 +881,7 @@ iomap_write_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
offset = offset_in_thp(page, pos);

if (mapping_writably_mapped(inode->i_mapping))
- flush_dcache_page(page);
+ flush_dcache_page(page + offset / PAGE_SIZE);

copied = iov_iter_copy_from_user_atomic(page, i, offset, bytes);

--
2.28.0