diff mbox series

[RFC,1/2] xfs: lift truncate iomap zeroing into a new helper

Message ID 20221028130411.977076-2-bfoster@redhat.com (mailing list archive)
State Deferred, archived
Headers show
Series xfs: optimize truncate cache flushing | expand

Commit Message

Brian Foster Oct. 28, 2022, 1:04 p.m. UTC
Create a new helper to improve readability of subsequent changes in
this area of code. No functional change is intended, but while here,
also change the end offset of the flush to use newsize - 1. This off
by one should have no impact on cases where the flush is relevant
because zeroing only occurs when the size is non-block aligned.

Signed-off-by: Brian Foster <bfoster@redhat.com>
---
 fs/xfs/xfs_iops.c | 52 +++++++++++++++++++++++++++++++----------------
 1 file changed, 34 insertions(+), 18 deletions(-)
diff mbox series

Patch

diff --git a/fs/xfs/xfs_iops.c b/fs/xfs/xfs_iops.c
index 2e10e1c66ad6..d31e64db243f 100644
--- a/fs/xfs/xfs_iops.c
+++ b/fs/xfs/xfs_iops.c
@@ -770,6 +770,39 @@  xfs_setattr_nonsize(
 	return error;
 }
 
+/*
+ * Do the appropriate zeroing for a particular truncate operation. This zeroes
+ * the range of blocks between oldsize and newsize on a truncate up or otherwise
+ * partially zeroes the post-EOF portion of the EOF block at newsize.
+ */
+static int
+xfs_truncate_zeroing(
+	struct xfs_inode	*ip,
+	xfs_off_t		oldsize,
+	xfs_off_t		newsize,
+	bool			*did_zeroing)
+{
+	int			error;
+
+	if (newsize > oldsize) {
+		trace_xfs_zero_eof(ip, oldsize, newsize - oldsize);
+		return xfs_zero_range(ip, oldsize, newsize - oldsize,
+				did_zeroing);
+	}
+
+	/*
+	 * iomap won't detect a dirty page over an unwritten block (or a cow
+	 * block over a hole) and subsequently skips zeroing the newly post-EOF
+	 * portion of the page. Flush the new EOF to convert the block before
+	 * the pagecache truncate.
+	 */
+	error = filemap_write_and_wait_range(VFS_I(ip)->i_mapping, newsize - 1,
+					     newsize - 1);
+	if (error)
+		return error;
+	return xfs_truncate_page(ip, newsize, did_zeroing);
+}
+
 /*
  * Truncate file.  Must have write permission and not be a directory.
  *
@@ -835,24 +868,7 @@  xfs_setattr_size(
 	 * extension, or zeroing out the rest of the block on a downward
 	 * truncate.
 	 */
-	if (newsize > oldsize) {
-		trace_xfs_zero_eof(ip, oldsize, newsize - oldsize);
-		error = xfs_zero_range(ip, oldsize, newsize - oldsize,
-				&did_zeroing);
-	} else {
-		/*
-		 * iomap won't detect a dirty page over an unwritten block (or a
-		 * cow block over a hole) and subsequently skips zeroing the
-		 * newly post-EOF portion of the page. Flush the new EOF to
-		 * convert the block before the pagecache truncate.
-		 */
-		error = filemap_write_and_wait_range(inode->i_mapping, newsize,
-						     newsize);
-		if (error)
-			return error;
-		error = xfs_truncate_page(ip, newsize, &did_zeroing);
-	}
-
+	error = xfs_truncate_zeroing(ip, oldsize, newsize, &did_zeroing);
 	if (error)
 		return error;