@@ -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;
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(-)