mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 1/2] fs/ntfs3: allocate only the written range of a hole on writeback
@ 2026-11-20 13:36 Zhan Xusheng
  2026-11-20 13:36 ` [PATCH 2/2] fs/ntfs3: zero new clusters that a write only partly covers Zhan Xusheng
  0 siblings, 1 reply; 2+ messages in thread
From: Zhan Xusheng @ 2026-11-20 13:36 UTC (permalink / raw)
  To: almaz.alexandrovich; +Cc: ntfs3, linux-kernel, stable, zhanxusheng

From: Zhan Xusheng <zhanxusheng@xiaomi.com>

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@vger.kernel.org
Signed-off-by: Zhan Xusheng <zhanxusheng@xiaomi.com>
---
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


^ permalink raw reply	[flat|nested] 2+ messages in thread

* [PATCH 2/2] fs/ntfs3: zero new clusters that a write only partly covers
  2026-11-20 13:36 [PATCH 1/2] fs/ntfs3: allocate only the written range of a hole on writeback Zhan Xusheng
@ 2026-11-20 13:36 ` Zhan Xusheng
  0 siblings, 0 replies; 2+ messages in thread
From: Zhan Xusheng @ 2026-11-20 13:36 UTC (permalink / raw)
  To: almaz.alexandrovich; +Cc: ntfs3, linux-kernel, stable, zhanxusheng

From: Zhan Xusheng <zhanxusheng@xiaomi.com>

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")
Cc: stable@vger.kernel.org
Signed-off-by: Zhan Xusheng <zhanxusheng@xiaomi.com>
---
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(+)

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


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-20 13:36 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-20 13:36 [PATCH 1/2] fs/ntfs3: allocate only the written range of a hole on writeback Zhan Xusheng
2026-11-20 13:36 ` [PATCH 2/2] fs/ntfs3: zero new clusters that a write only partly covers Zhan Xusheng

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®