[PATCH v2 11/25] fs: Make page_mkwrite_check_truncate thp-aware

Matthew Wilcox
2020-02-12T04:20:06Z

From: "Matthew Wilcox (Oracle)" <willy@infradead.org>

If the page is compound, check the appropriate indices and return the
appropriate sizes.

Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
---
include/linux/pagemap.h | 11 ++++++-----
1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h
index aa925295347c..2ec33aabdbf6 100644
--- a/include/linux/pagemap.h
+++ b/include/linux/pagemap.h
@@ -732,17 +732,18 @@ static inline int page_mkwrite_check_truncate(struct page *page,
struct inode *inode)
{
loff_t size = i_size_read(inode);
- pgoff_t index = size >> PAGE_SHIFT;
- int offset = offset_in_page(size);
+ pgoff_t first_index = size >> PAGE_SHIFT;
+ pgoff_t last_index = first_index + hpage_nr_pages(page) - 1;
+ unsigned long offset = offset_in_this_page(page, size);

if (page->mapping != inode->i_mapping)
return -EFAULT;

/* page is wholly inside EOF */
- if (page->index < index)
- return PAGE_SIZE;
+ if (page->index < first_index)
+ return thp_size(page);
/* page is wholly past EOF */
- if (page->index > index || !offset)
+ if (page->index > last_index || !offset)
return -EFAULT;
/* page is partially inside EOF */
return offset;
--
2.25.0