Skip to contentSkip to conversation

xfs: fix PAGE_MASK usage in xfs_free_file_space

PATCH In review Darrick J. Wong 3 participants 3 messages Nov 28, 2018 XFS filesystem

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xfs: fix PAGE_MASK usage in xfs_free_file_space

From: Darrick J. Wong <[email protected]>

In commit e53c4b598, I *tried* to teach xfs to force writeback when we fzero/fpunch right up to EOF so that if EOF is in the middle of a page, the post-EOF part of the page gets zeroed before we return to userspace. Unfortunately, I missed the part where PAGE_MASK is ~(PAGE_SIZE - 1), which means that we totally fail to zero if we're fpunching and EOF is within the first page. Worse yet, the same PAGE_MASK thinko plagues the filemap_write_and_wait_range call, so we'd initiate writeback of the entire file, which (mostly) masked the thinko.

Drop the tricky PAGE_MASK and replace it with correct usage of PAGE_SIZE and the proper rounding macros.

Fixes e53c4b598 ("xfs: ensure post-EOF zeroing happens after zeroing part of a file")
---
 fs/xfs/xfs_bmap_util.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/xfs/xfs_bmap_util.c +2 -2
index 404e581f1ea1..1ee8c5539fa4 100644
@@ -1126,9 +1126,9 @@ xfs_free_file_space(
 	 * page could be mmap'd and iomap_zero_range doesn't do that for us.
 	 * Writeback of the eof page will do this, albeit clumsily.
 	 */
-	if (offset + len >= XFS_ISIZE(ip) && ((offset + len) & PAGE_MASK)) {
+	if (offset + len >= XFS_ISIZE(ip) && offset_in_page(offset + len) > 0) {
 		error = filemap_write_and_wait_range(VFS_I(ip)->i_mapping,
-				(offset + len) & ~PAGE_MASK, LLONG_MAX);
+				round_down(offset + len, PAGE_SIZE), LLONG_MAX);
 	}
 
 	return error;
Reviewed-by on v1 1/1
#1 lore raw
Quoted 25 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

In commit e53c4b598, I *tried* to teach xfs to force writeback when we fzero/fpunch right up to EOF so that if EOF is in the middle of a page, the post-EOF part of the page gets zeroed before we return to userspace. Unfortunately, I missed the part where PAGE_MASK is ~(PAGE_SIZE - 1), which means that we totally fail to zero if we're fpunching and EOF is within the first page. Worse yet, the same PAGE_MASK thinko plagues the filemap_write_and_wait_range call, so we'd initiate writeback of the entire file, which (mostly) masked the thinko.

Drop the tricky PAGE_MASK and replace it with correct usage of PAGE_SIZE and the proper rounding macros.

Fixes e53c4b598 ("xfs: ensure post-EOF zeroing happens after zeroing part of a file")
---
 fs/xfs/xfs_bmap_util.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/xfs/xfs_bmap_util.c +0 -0
index 404e581f1ea1..1ee8c5539fa4 100644

fs/xfs/xfs_bmap_util.c xfs_free_file_space( · line 1131

Quoted 8 lines
fs/xfs/xfs_bmap_util.c +2 -2
@@ -1126,9 +1126,9 @@ xfs_free_file_space(
 	 * page could be mmap'd and iomap_zero_range doesn't do that for us.
 	 * Writeback of the eof page will do this, albeit clumsily.
 	 */
-	if (offset + len >= XFS_ISIZE(ip) && ((offset + len) & PAGE_MASK)) {
+	if (offset + len >= XFS_ISIZE(ip) && offset_in_page(offset + len) > 0) {
 		error = filemap_write_and_wait_range(VFS_I(ip)->i_mapping,
-				(offset + len) & ~PAGE_MASK, LLONG_MAX);
+				round_down(offset + len, PAGE_SIZE), LLONG_MAX);

Looks fine. I think I tripped over this in my bs > ps series and changed it to round_down(..., max(bs, ps)), but that's not necessary here...

Signature · 2 lines
Dave Chinner
[email protected]
Reviewed-by on v1 1/1
#2 lore raw
In reply to Darrick J. Wong
Quoted 11 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

In commit e53c4b598, I *tried* to teach xfs to force writeback when we fzero/fpunch right up to EOF so that if EOF is in the middle of a page, the post-EOF part of the page gets zeroed before we return to userspace. Unfortunately, I missed the part where PAGE_MASK is ~(PAGE_SIZE - 1), which means that we totally fail to zero if we're fpunching and EOF is within the first page. Worse yet, the same PAGE_MASK thinko plagues the filemap_write_and_wait_range call, so we'd initiate writeback of the entire file, which (mostly) masked the thinko.

Quoted 5 lines

Drop the tricky PAGE_MASK and replace it with correct usage of PAGE_SIZE and the proper rounding macros.

Fixes e53c4b598 ("xfs: ensure post-EOF zeroing happens after zeroing part of a file")

Thanks. This also happens to be much more readable as well: