Skip to contentSkip to conversation

fs/ntfs3: allocate only the written range of a hole on writeback

PATCH New Zhan Xusheng 1 participant 2 messages Nov 20

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 fs/ntfs3: allocate only the written range of a hole on writeback

From: Zhan Xusheng <[email protected]>

ntfs_writeback_range() maps from the folio to the end of the file with one ntfs_iomap_begin() call, and for a sparse file that call allocates the holes it maps. A page stored through a shared mapping reaches writeback without a delayed allocation, so writing it back allocates every cluster from that page to the end of the file. Only the page is written, and ntfs_filemap_close() later raises the valid size over the rest.

On a volume with 4K clusters, map a 4096-byte sparse file MAP_SHARED for 100 MiB, grow it to 100 MiB with ftruncate() and store 8 bytes at 4096:
97876 KiB are allocated instead of 8 KiB. With a 4 MiB file and the store at 2M, the 2093056 bytes from 2M + 4K to the end of the file read back what their clusters held before.

Map only the range being written back for a sparse file.

Fixes 099ef9ab9203 ("fs/ntfs3: implement iomap-based file operations")

---
Tested in a VM on 7.4-rc3 (20eef29b7c84), 4K and 64K clusters, with and without delalloc, with the free space filled with a known pattern first. The numbers in both changelogs are from 7.4-rc3 without the series.

For a sparse file, writeback now calls ntfs_iomap_begin() for each dirty range instead of once per extent.

 fs/ntfs3/inode.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
fs/ntfs3/inode.c +5 -1
index 45f894d182dd..535a2b0804af 100644
@@ -1181,7 +1181,11 @@ static ssize_t ntfs_writeback_range(struct iomap_writepage_ctx *wpc,
 		struct ntfs_inode *ni = ntfs_i(inode);
 		struct ntfs_sb_info *sbi = ntfs_sb(inode->i_sb);
 		loff_t i_size_up = ntfs_up_cluster(sbi, inode->i_size);
-		loff_t len_max = i_size_up - offset;
+		/*
+		 * Map up to EOF in one go, except across the holes of a sparse
+		 * file: iomap_begin() allocates what it maps there.
+		 */
+		loff_t len_max = is_sparsed(ni) ? len : i_size_up - offset;
 
 		err = ni->file.run_da.count ? ni_allocate_da_blocks(ni) : 0;
 

base-commit: 20eef29b7c8465ac98d7dd6d6f2ac07a8d8056ca

Signature · 1 line
2.43.0
#1 lore raw

PATCH 2/2 fs/ntfs3: zero new clusters that a write only partly covers

From: Zhan Xusheng <[email protected]>

When ntfs_iomap_begin() allocates clusters in a hole, the part of a new cluster that the request does not write keeps the old disk contents, and reads below the valid size return it. The iomap conversion asked attr_data_get_block() to zero such clusters only for writes through the page cache, and commit 10d7c95af043 ("fs/ntfs3: add delayed-allocation (delalloc) support") stopped asking for it at all. Only the delalloc path still zeroes them.

On a volume with 64K clusters, write 4K at 0 and at 960K into a sparse file, then 4K at 512K: 61440 bytes of the file read back old disk contents, both with O_DIRECT and with a buffered write on a volume mounted without delalloc.

Zero the new clusters when the request does not cover them.

Fixes 099ef9ab9203 ("fs/ntfs3: implement iomap-based file operations")
Fixes 10d7c95af043 ("fs/ntfs3: add delayed-allocation (delalloc) support")

---
With 4K clusters a page covers a whole cluster, so only 64K clusters show it in these tests. With both patches, fsx with mmap on 4K and 64K cluster volumes and on sparse and compressed files gives the same results as before.

 fs/ntfs3/inode.c | 4 ++++
 1 file changed, 4 insertions(+)
fs/ntfs3/inode.c +4 -0
index 535a2b0804af..9c2ded9567f7 100644
@@ -982,6 +982,10 @@ static int ntfs_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
 	 */
 	no_da = flags == (IOMAP_DIRECT | IOMAP_WRITE) || srcmap == WB_NO_DA;
 
+	/* The request does not write all of a new cluster: zero it first. */
+	if (rw && ((offset | endbyte) & sbi->cluster_mask))
+		zero = true;
+
 	err = attr_data_get_block(ni, vcn, clen_max, &lcn, &clen,
 				  rw ? &new_clst : NULL, zero, &res, no_da);
 
Signature · 1 line
2.43.0