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

From: Zhan Xusheng

Date: Fri Nov 20 2026 - 08:36:14 EST


From: Zhan Xusheng <zhanxusheng@xxxxxxxxxx>

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")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Zhan Xusheng <zhanxusheng@xxxxxxxxxx>
---
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(-)

diff --git a/fs/ntfs3/inode.c b/fs/ntfs3/inode.c
index 45f894d182dd..535a2b0804af 100644
--- a/fs/ntfs3/inode.c
+++ b/fs/ntfs3/inode.c
@@ -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
--
2.43.0