* [PATCH v5 01/13] iomap: punch out a run of clean blocks in one call
2026-12-04 18:40 [PATCH v5 00/13] ntfs: delayed allocation accounting and stale data fixes, part 1 Ernest Borowski via B4 Relay
@ 2026-12-04 18:40 ` Ernest Borowski via B4 Relay
2026-12-04 18:40 ` [PATCH v5 02/13] iomap: add a helper to mark a range of a folio dirty Ernest Borowski via B4 Relay
` (11 subsequent siblings)
12 siblings, 0 replies; 15+ messages in thread
From: Ernest Borowski via B4 Relay @ 2026-12-04 18:40 UTC (permalink / raw)
To: Namjae Jeon, Hyunchul Lee, Baolin Liu
Cc: Christian Brauner, Christoph Hellwig, Darrick J. Wong, ntfs,
linux-fsdevel, linux-xfs, linux-kernel, Zhan Xusheng, stable,
Ernest Borowski
From: Ernest Borowski <ernest.borowski.git@proton.me>
iomap_write_delalloc_ifs_punch() calls ->punch() once for every clean
block of a dirty folio. A file system that reserves delayed allocation
space in units larger than a block can release a unit only when one call
covers it whole. ntfs reserves whole clusters and uses 512 byte blocks,
so with 1 KiB clusters a buffered write into a hole that fails after
zeroing the first cluster of a 4 KiB folio leaves the other three
clusters of that folio reserved, and writeback later allocates them.
Pass each range of contiguous clean blocks to ->punch() in one call.
The clean blocks at the start of a dirty folio join the range that ends
there, and the clean blocks at the end of the part of the folio being
scanned are left to the next punch, which also covers what follows
them: the next folio, the gap to the next cached data, or the range past
the end of the scan. Otherwise a clean 1 KiB cluster that straddles the
end of the scan, which stops at the block that holds EOF, still goes to
->punch() in two parts and stays reserved. The blocks left over are
punched after their folio is unlocked, as the range past the end of the
scan always was. XFS and ext4 punch any range they are given, so for
them this only means fewer calls.
"ntfs: punch out the delalloc reservation of a failed buffered write"
later in this series needs this, so it goes to stable with it.
Cc: stable@vger.kernel.org
Signed-off-by: Ernest Borowski <ernest.borowski.git@proton.me>
---
fs/iomap/buffered-io.c | 82 +++++++++++++++++++++++++-------------------------
1 file changed, 41 insertions(+), 41 deletions(-)
diff --git a/fs/iomap/buffered-io.c b/fs/iomap/buffered-io.c
index 25915c0f5e..2b9bcff7ce 100644
--- a/fs/iomap/buffered-io.c
+++ b/fs/iomap/buffered-io.c
@@ -1350,58 +1350,58 @@ int iomap_fsverity_write(struct file *file, loff_t pos, size_t length,
}
EXPORT_SYMBOL_GPL(iomap_fsverity_write);
-static void iomap_write_delalloc_ifs_punch(struct inode *inode,
- struct folio *folio, loff_t start_byte, loff_t end_byte,
- struct iomap *iomap, iomap_punch_t punch)
-{
- unsigned int first_blk, last_blk;
- loff_t last_byte;
- u8 blkbits = inode->i_blkbits;
- struct iomap_folio_state *ifs;
-
- /*
- * When we have per-block dirty tracking, there can be
- * blocks within a folio which are marked uptodate
- * but not dirty. In that case it is necessary to punch
- * out such blocks to avoid leaking any delalloc blocks.
- */
- ifs = folio->private;
- if (!ifs)
- return;
-
- last_byte = min_t(loff_t, end_byte - 1, folio_next_pos(folio) - 1);
- first_blk = offset_in_folio(folio, start_byte) >> blkbits;
- last_blk = offset_in_folio(folio, last_byte) >> blkbits;
- while ((first_blk = ifs_next_clean_block(folio, first_blk, last_blk))
- <= last_blk) {
- punch(inode, folio_pos(folio) + (first_blk << blkbits),
- 1 << blkbits, iomap);
- first_blk++;
- }
-}
-
static void iomap_write_delalloc_punch(struct inode *inode, struct folio *folio,
loff_t *punch_start_byte, loff_t start_byte, loff_t end_byte,
struct iomap *iomap, iomap_punch_t punch)
{
+ loff_t range_end = min_t(loff_t, end_byte, folio_next_pos(folio));
+ unsigned int first_blk, last_blk, blk;
+ u8 blkbits = inode->i_blkbits;
+
if (!folio_test_dirty(folio))
return;
- /* if dirty, punch up to offset */
- if (start_byte > *punch_start_byte) {
- punch(inode, *punch_start_byte, start_byte - *punch_start_byte,
- iomap);
+ /* Without per-block state the whole folio is dirty. */
+ if (!folio->private) {
+ /* if dirty, punch up to offset */
+ if (start_byte > *punch_start_byte)
+ punch(inode, *punch_start_byte,
+ start_byte - *punch_start_byte, iomap);
+ /*
+ * Make sure the next punch start is correctly bound to
+ * the end of this data range, not the end of the folio.
+ */
+ *punch_start_byte = range_end;
+ return;
}
- /* Punch non-dirty blocks within folio */
- iomap_write_delalloc_ifs_punch(inode, folio, start_byte, end_byte,
- iomap, punch);
-
/*
- * Make sure the next punch start is correctly bound to
- * the end of this data range, not the end of the folio.
+ * When we have per-block dirty tracking, there can be blocks within a
+ * folio which are marked uptodate but not dirty. In that case it is
+ * necessary to punch out such blocks to avoid leaking any delalloc
+ * blocks.
+ *
+ * A file system that reserves space in units larger than a block can
+ * only release a unit that one ->punch() call covers whole, so pass
+ * every clean range in one call: the clean blocks before the first
+ * dirty block join the range that ends at start_byte, and clean blocks
+ * at the end of the range are not punched here but left to the next
+ * punch, which also covers what follows them. The next punch start
+ * stays bound to the end of this data range, not the end of the folio.
*/
- *punch_start_byte = min_t(loff_t, end_byte, folio_next_pos(folio));
+ first_blk = offset_in_folio(folio, start_byte) >> blkbits;
+ last_blk = offset_in_folio(folio, range_end - 1) >> blkbits;
+ while ((blk = ifs_next_dirty_block(folio, first_blk, last_blk)) <=
+ last_blk) {
+ loff_t pos = folio_pos(folio) + ((loff_t)blk << blkbits);
+
+ if (pos > *punch_start_byte)
+ punch(inode, *punch_start_byte,
+ pos - *punch_start_byte, iomap);
+ first_blk = ifs_next_clean_block(folio, blk + 1, last_blk);
+ *punch_start_byte = min_t(loff_t, range_end, folio_pos(folio) +
+ ((loff_t)first_blk << blkbits));
+ }
}
/*
--
2.56.1
^ permalink raw reply [flat|nested] 15+ messages in thread* [PATCH v5 02/13] iomap: add a helper to mark a range of a folio dirty
2026-12-04 18:40 [PATCH v5 00/13] ntfs: delayed allocation accounting and stale data fixes, part 1 Ernest Borowski via B4 Relay
2026-12-04 18:40 ` [PATCH v5 01/13] iomap: punch out a run of clean blocks in one call Ernest Borowski via B4 Relay
@ 2026-12-04 18:40 ` Ernest Borowski via B4 Relay
2026-12-04 18:40 ` [PATCH v5 03/13] ntfs: release delalloc reservations cut off by truncate Ernest Borowski via B4 Relay
` (10 subsequent siblings)
12 siblings, 0 replies; 15+ messages in thread
From: Ernest Borowski via B4 Relay @ 2026-12-04 18:40 UTC (permalink / raw)
To: Namjae Jeon, Hyunchul Lee, Baolin Liu
Cc: Christian Brauner, Christoph Hellwig, Darrick J. Wong, ntfs,
linux-fsdevel, linux-xfs, linux-kernel, Zhan Xusheng, stable,
Baolin Liu, Ernest Borowski
From: Ernest Borowski <ernest.borowski.git@proton.me>
A filesystem that allocates in units larger than a block may need
writeback to write blocks that a buffered write did not copy into. ntfs
reserves whole clusters for delayed allocation, and after a short copy
into a cluster that was a hole, writeback must also write the rest of
that cluster, or the cluster shows its old contents on the disk there.
folio_mark_dirty() marks every block of the folio dirty, so the delalloc
release that follows no longer finds the clean blocks of the clusters
the write did not reach and keeps them reserved: with 1 KiB and 2 KiB
clusters, writeback then allocates a cluster that holds no data.
Add iomap_dirty_folio_range(), which marks the folio and only the blocks
covering a byte range dirty. It and iomap_dirty_folio() share one
internal helper; iomap_dirty_folio() keeps passing the mapping it is
given, as ->dirty_folio may be called without the folio lock.
"ntfs: zero the rest of a new cluster after a short buffered write"
later in this series needs this, so it goes to stable with it.
Cc: stable@vger.kernel.org
Reviewed-by: Baolin Liu <liubaolin@kylinos.cn>
Signed-off-by: Ernest Borowski <ernest.borowski.git@proton.me>
---
fs/iomap/buffered-io.c | 32 ++++++++++++++++++++++++++------
include/linux/iomap.h | 1 +
2 files changed, 27 insertions(+), 6 deletions(-)
diff --git a/fs/iomap/buffered-io.c b/fs/iomap/buffered-io.c
index 2b9bcff7ce..d505668360 100644
--- a/fs/iomap/buffered-io.c
+++ b/fs/iomap/buffered-io.c
@@ -847,15 +847,35 @@ void iomap_invalidate_folio(struct folio *folio, size_t offset, size_t len)
}
EXPORT_SYMBOL_GPL(iomap_invalidate_folio);
-bool iomap_dirty_folio(struct address_space *mapping, struct folio *folio)
+static bool __iomap_dirty_folio_range(struct address_space *mapping,
+ struct folio *folio, size_t off, size_t len)
{
- struct inode *inode = mapping->host;
- size_t len = folio_size(folio);
-
- ifs_alloc(inode, folio, 0);
- iomap_set_range_dirty(folio, 0, len);
+ ifs_alloc(mapping->host, folio, 0);
+ iomap_set_range_dirty(folio, off, len);
return filemap_dirty_folio(mapping, folio);
}
+
+/**
+ * iomap_dirty_folio_range - mark a range of a folio dirty
+ * @folio: the locked, uptodate folio
+ * @off: byte offset of the range in @folio
+ * @len: length of the range in bytes
+ *
+ * Mark the blocks that cover [@off, @off + @len) and @folio dirty, but none
+ * of the other blocks of @folio, so that writeback writes only those blocks.
+ *
+ * Return: true if @folio was clean before, as filemap_dirty_folio() does.
+ */
+bool iomap_dirty_folio_range(struct folio *folio, size_t off, size_t len)
+{
+ return __iomap_dirty_folio_range(folio->mapping, folio, off, len);
+}
+EXPORT_SYMBOL_GPL(iomap_dirty_folio_range);
+
+bool iomap_dirty_folio(struct address_space *mapping, struct folio *folio)
+{
+ return __iomap_dirty_folio_range(mapping, folio, 0, folio_size(folio));
+}
EXPORT_SYMBOL_GPL(iomap_dirty_folio);
static void
diff --git a/include/linux/iomap.h b/include/linux/iomap.h
index 78a1b7603a..f80da4d8ff 100644
--- a/include/linux/iomap.h
+++ b/include/linux/iomap.h
@@ -445,6 +445,7 @@ struct folio *iomap_get_folio(struct iomap_iter *iter, loff_t pos, size_t len);
bool iomap_release_folio(struct folio *folio, gfp_t gfp_flags);
void iomap_invalidate_folio(struct folio *folio, size_t offset, size_t len);
bool iomap_dirty_folio(struct address_space *mapping, struct folio *folio);
+bool iomap_dirty_folio_range(struct folio *folio, size_t off, size_t len);
void iomap_folio_mark_uptodate(struct folio *folio);
int iomap_file_unshare(struct inode *inode, loff_t pos, loff_t len,
iomap_iter_next_fn next,
--
2.56.1
^ permalink raw reply [flat|nested] 15+ messages in thread* [PATCH v5 03/13] ntfs: release delalloc reservations cut off by truncate
2026-12-04 18:40 [PATCH v5 00/13] ntfs: delayed allocation accounting and stale data fixes, part 1 Ernest Borowski via B4 Relay
2026-12-04 18:40 ` [PATCH v5 01/13] iomap: punch out a run of clean blocks in one call Ernest Borowski via B4 Relay
2026-12-04 18:40 ` [PATCH v5 02/13] iomap: add a helper to mark a range of a folio dirty Ernest Borowski via B4 Relay
@ 2026-12-04 18:40 ` Ernest Borowski via B4 Relay
2026-12-04 18:40 ` [PATCH v5 04/13] ntfs: do not consume delalloc reservations when filling holes Ernest Borowski via B4 Relay
` (9 subsequent siblings)
12 siblings, 0 replies; 15+ messages in thread
From: Ernest Borowski via B4 Relay @ 2026-12-04 18:40 UTC (permalink / raw)
To: Namjae Jeon, Hyunchul Lee, Baolin Liu
Cc: Christian Brauner, Christoph Hellwig, Darrick J. Wong, ntfs,
linux-fsdevel, linux-xfs, linux-kernel, Zhan Xusheng, stable,
Baolin Liu, Ernest Borowski
From: Ernest Borowski <ernest.borowski.git@proton.me>
A buffered write into a hole turns the hole into an LCN_DELALLOC run and
reserves its clusters in vol->dirty_clusters
(ntfs_write_simple_iomap_begin_non_resident()). Writeback hands the
reservation back when ntfs_cluster_alloc() allocates the run.
A truncate that cuts off delalloc runs before writeback drops their
dirty folios, frees the clusters of the attribute with
ntfs_cluster_free(), which skips everything that is not a real cluster,
and removes the runs with ntfs_rl_truncate_nolock(). Nothing gives the
reservation back, so the free space reported by statfs() and used by
ntfs_available_clusters_count() shrinks by that many clusters until the
volume is unmounted. $Bitmap is not affected.
Workloads that rewrite files (open(O_TRUNC) + write) or truncate freshly
written data lose free space quickly: a test that writes, truncates and
punches holes in six 400 KiB files on a 128 MiB volume sees statfs()
free space drop from 79 MB to zero in 80 seconds, after which writes
fail with ENOSPC or come back short. A 1 MiB buffered write followed by
ftruncate(fd, 0) and sync leaves 256 (4 KiB) clusters reserved.
Release the reservation of the delalloc runs that the shrink cuts off,
before the runlist is truncated.
Fixes: 495e90fa3348 ("ntfs: update attrib operations")
Cc: stable@vger.kernel.org
Reviewed-by: Baolin Liu <liubaolin@kylinos.cn>
Signed-off-by: Ernest Borowski <ernest.borowski.git@proton.me>
---
fs/ntfs/attrib.c | 33 +++++++++++++++++++++++++++++++++
1 file changed, 33 insertions(+)
diff --git a/fs/ntfs/attrib.c b/fs/ntfs/attrib.c
index c192333be8..3f19fd2ec7 100644
--- a/fs/ntfs/attrib.c
+++ b/fs/ntfs/attrib.c
@@ -4420,6 +4420,37 @@ static int ntfs_attr_make_resident(struct ntfs_inode *ni, struct ntfs_attr_searc
return 0;
}
+/*
+ * ntfs_release_delalloc_from - give back the reservation of delalloc runs
+ * @ni: non-resident ntfs attribute whose runlist is about to be truncated
+ * @start_vcn: first vcn that is cut off
+ *
+ * Delayed allocation reserves vol->dirty_clusters for every LCN_DELALLOC
+ * cluster when the buffered write is done and only gives it back when
+ * writeback allocates the clusters. Runs cut off by a truncate never reach
+ * writeback (their dirty folios are dropped from the page cache), so their
+ * reservation has to be released here or it is lost until unmount.
+ *
+ * The runlist must be mapped completely and locked for writing.
+ */
+static void ntfs_release_delalloc_from(struct ntfs_inode *ni, s64 start_vcn)
+{
+ struct runlist_element *rl = ni->runlist.rl;
+ s64 nr = 0;
+
+ lockdep_assert_held_write(&ni->runlist.lock);
+
+ for (; rl && rl->length; rl++) {
+ if (rl->lcn != LCN_DELALLOC || rl->vcn + rl->length <= start_vcn)
+ continue;
+ nr += rl->vcn + rl->length - max(rl->vcn, start_vcn);
+ }
+ if (!nr)
+ return;
+ ntfs_release_dirty_clusters(ni->vol, nr);
+ ni->i_dealloc_clusters -= min_t(s64, nr, ni->i_dealloc_clusters);
+}
+
/*
* ntfs_non_resident_attr_shrink - shrink a non-resident, open ntfs attribute
* @ni: non-resident ntfs attribute to shrink
@@ -4516,6 +4547,8 @@ static int ntfs_non_resident_attr_shrink(struct ntfs_inode *ni,
}
ntfs_attr_put_search_ctx(ctx);
+ ntfs_release_delalloc_from(ni, first_free_vcn);
+
/* Truncate the runlist itself. */
if (ntfs_rl_truncate_nolock(vol, &ni->runlist, first_free_vcn)) {
/*
--
2.56.1
^ permalink raw reply [flat|nested] 15+ messages in thread* [PATCH v5 04/13] ntfs: do not consume delalloc reservations when filling holes
2026-12-04 18:40 [PATCH v5 00/13] ntfs: delayed allocation accounting and stale data fixes, part 1 Ernest Borowski via B4 Relay
` (2 preceding siblings ...)
2026-12-04 18:40 ` [PATCH v5 03/13] ntfs: release delalloc reservations cut off by truncate Ernest Borowski via B4 Relay
@ 2026-12-04 18:40 ` Ernest Borowski via B4 Relay
2026-12-04 18:40 ` [PATCH v5 05/13] ntfs: keep delalloc reservations when a cluster allocation fails Ernest Borowski via B4 Relay
` (8 subsequent siblings)
12 siblings, 0 replies; 15+ messages in thread
From: Ernest Borowski via B4 Relay @ 2026-12-04 18:40 UTC (permalink / raw)
To: Namjae Jeon, Hyunchul Lee, Baolin Liu
Cc: Christian Brauner, Christoph Hellwig, Darrick J. Wong, ntfs,
linux-fsdevel, linux-xfs, linux-kernel, Zhan Xusheng, stable,
Baolin Liu, Ernest Borowski
From: Ernest Borowski <ernest.borowski.git@proton.me>
ntfs_attr_map_cluster() always calls ntfs_cluster_alloc() with
is_dealloc set, which makes the allocator skip the dirty_clusters check
and subtract the allocated clusters from vol->dirty_clusters. That is
right for writeback converting an LCN_DELALLOC run, whose clusters were
reserved by the buffered write, but ntfs_attr_map_cluster() also fills
plain holes for fallocate(), direct I/O and page_mkwrite(), which never
reserved anything. Those allocations release reservations that belong
to dirty data of other files, and can use clusters that are promised to
that data, so writeback of the delayed data can later run out of space
although every earlier check succeeded.
Reproducer: write 1 MiB to file A without syncing (statfs() free space
drops by 256 clusters), then fallocate() 1 MiB on file B: free space
does not drop at all.
Only treat the allocation as consuming a reservation when the run being
filled is a delalloc run. Filling a hole now fails with ENOSPC when the
remaining free clusters are all reserved for delayed data, so a write
fault into a hole of an mmap()ed file gets SIGBUS in that case, as on
other filesystems with delayed allocation.
Fixes: 495e90fa3348 ("ntfs: update attrib operations")
Cc: stable@vger.kernel.org
Reviewed-by: Baolin Liu <liubaolin@kylinos.cn>
Signed-off-by: Ernest Borowski <ernest.borowski.git@proton.me>
---
fs/ntfs/attrib.c | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)
diff --git a/fs/ntfs/attrib.c b/fs/ntfs/attrib.c
index 3f19fd2ec7..31ee811458 100644
--- a/fs/ntfs/attrib.c
+++ b/fs/ntfs/attrib.c
@@ -5557,8 +5557,14 @@ int ntfs_attr_map_cluster(struct ntfs_inode *ni, s64 vcn_start, s64 *lcn_start,
}
}
+ /*
+ * Only a delayed-allocation run holds a reservation in
+ * vol->dirty_clusters for the allocator to consume. Holes filled by
+ * fallocate(), direct I/O or page_mkwrite() have none and must leave
+ * the reservations of other writers alone.
+ */
rlc = ntfs_cluster_alloc(vol, vcn, clu_count, lcn_seek_from, DATA_ZONE,
- false, true, true);
+ false, true, lcn == LCN_DELALLOC);
if (IS_ERR(rlc)) {
err = PTR_ERR(rlc);
goto out;
--
2.56.1
^ permalink raw reply [flat|nested] 15+ messages in thread* [PATCH v5 05/13] ntfs: keep delalloc reservations when a cluster allocation fails
2026-12-04 18:40 [PATCH v5 00/13] ntfs: delayed allocation accounting and stale data fixes, part 1 Ernest Borowski via B4 Relay
` (3 preceding siblings ...)
2026-12-04 18:40 ` [PATCH v5 04/13] ntfs: do not consume delalloc reservations when filling holes Ernest Borowski via B4 Relay
@ 2026-12-04 18:40 ` Ernest Borowski via B4 Relay
2026-12-04 18:40 ` [PATCH v5 06/13] ntfs: release the delalloc reservations of an evicted inode Ernest Borowski via B4 Relay
` (7 subsequent siblings)
12 siblings, 0 replies; 15+ messages in thread
From: Ernest Borowski via B4 Relay @ 2026-12-04 18:40 UTC (permalink / raw)
To: Namjae Jeon, Hyunchul Lee, Baolin Liu
Cc: Christian Brauner, Christoph Hellwig, Darrick J. Wong, ntfs,
linux-fsdevel, linux-xfs, linux-kernel, Zhan Xusheng, stable,
Baolin Liu, Ernest Borowski
From: Ernest Borowski <ernest.borowski.git@proton.me>
ntfs_cluster_alloc() sets vol->dirty_clusters to 0 when it fails after
the free space check, which in practice means -ENOMEM or -EIO while it
scans the bitmap. vol->dirty_clusters is the sum of the reservations
of every delalloc run on the volume, and those runs are still there, so
the reset makes statfs() and the next allocations treat the reserved
clusters as free. Buffered writes then reserve the same clusters a
second time, and writeback of the delayed data later fails with ENOSPC
and loses it.
ntfs_attr_map_cluster() has the converse problem on its own error
paths: once the allocator succeeded for a delalloc run it has taken
that run's reservation, but when the runlist copy, the runlist merge or
the mapping pairs update fails afterwards, the clusters are freed and
the run stays delalloc without a reservation.
Reproducer (fault injection): write 4 MiB to file A without syncing,
then fsync another file with /proc/thread-self/fail-nth set so that the
kvzalloc() in ntfs_cluster_alloc() fails. statfs() free space jumps
back up by 1025 of the 1028 clusters A holds (4 KiB clusters). Writing a
file of the size statfs() then reports and syncing loses about 4 MiB of
that file's data; on 512 byte, 4 KiB and 64 KiB clusters alike. Fault
sites after the allocation (the runlist copy in ntfs_attr_map_cluster())
raise the free space by the clusters of the failed request.
Leave vol->dirty_clusters alone when the allocation fails, and hold the
reservation again when ntfs_attr_map_cluster() puts a delalloc run back
after the allocator consumed it, before the clusters are freed, so that
no other writer can reserve them in between. If freeing them fails too,
the run keeps the clusters and the hold is dropped again. The error
path after a failed mapping pairs update now returns the error without
also reporting the freed clusters as allocated.
Fixes: 11ccc9107dc4 ("ntfs: update runlist handling and cluster allocator")
Cc: stable@vger.kernel.org
Reviewed-by: Baolin Liu <liubaolin@kylinos.cn>
Signed-off-by: Ernest Borowski <ernest.borowski.git@proton.me>
---
fs/ntfs/attrib.c | 24 +++++++++++++++++++++++-
fs/ntfs/lcnalloc.c | 1 -
2 files changed, 23 insertions(+), 2 deletions(-)
diff --git a/fs/ntfs/attrib.c b/fs/ntfs/attrib.c
index 31ee811458..3944e32764 100644
--- a/fs/ntfs/attrib.c
+++ b/fs/ntfs/attrib.c
@@ -5465,6 +5465,19 @@ int ntfs_attr_truncate(struct ntfs_inode *ni, const s64 newsize)
HOLES_NO : HOLES_OK);
}
+/*
+ * The allocator has taken the reservation of a delalloc run whose clusters
+ * are about to be freed again, the run staying delalloc: hold the
+ * reservation again first, so that no other writer can reserve the freed
+ * clusters in between.
+ */
+static void ntfs_map_cluster_rehold(struct ntfs_volume *vol, bool was_delalloc,
+ s64 clu_count)
+{
+ if (was_delalloc)
+ ntfs_hold_dirty_clusters(vol, clu_count);
+}
+
int ntfs_attr_map_cluster(struct ntfs_inode *ni, s64 vcn_start, s64 *lcn_start,
s64 *lcn_count, s64 max_clu_count, bool *balloc, bool update_mp,
bool skip_holes)
@@ -5476,6 +5489,7 @@ int ntfs_attr_map_cluster(struct ntfs_inode *ni, s64 vcn_start, s64 *lcn_start,
struct runlist_element *old_rl = NULL;
s64 vcn = vcn_start, lcn, clu_count;
s64 lcn_seek_from = -1;
+ bool was_delalloc;
int err = 0;
size_t new_rl_count, old_rl_count;
@@ -5563,8 +5577,9 @@ int ntfs_attr_map_cluster(struct ntfs_inode *ni, s64 vcn_start, s64 *lcn_start,
* fallocate(), direct I/O or page_mkwrite() have none and must leave
* the reservations of other writers alone.
*/
+ was_delalloc = lcn == LCN_DELALLOC;
rlc = ntfs_cluster_alloc(vol, vcn, clu_count, lcn_seek_from, DATA_ZONE,
- false, true, lcn == LCN_DELALLOC);
+ false, true, was_delalloc);
if (IS_ERR(rlc)) {
err = PTR_ERR(rlc);
goto out;
@@ -5578,6 +5593,7 @@ int ntfs_attr_map_cluster(struct ntfs_inode *ni, s64 vcn_start, s64 *lcn_start,
old_rl_count * sizeof(*old_rl), GFP_NOFS);
if (!old_rl) {
err = -ENOMEM;
+ ntfs_map_cluster_rehold(vol, was_delalloc, clu_count);
if (ntfs_cluster_free_from_rl(vol, rlc)) {
ntfs_error(vol->sb,
"Failed to free cluster allocation after runlist backup failure.");
@@ -5591,6 +5607,7 @@ int ntfs_attr_map_cluster(struct ntfs_inode *ni, s64 vcn_start, s64 *lcn_start,
if (IS_ERR(rl)) {
ntfs_error(vol->sb, "Failed to merge runlists");
err = PTR_ERR(rl);
+ ntfs_map_cluster_rehold(vol, was_delalloc, clu_count);
if (ntfs_cluster_free_from_rl(vol, rlc))
ntfs_error(vol->sb, "Failed to free hot clusters.");
kvfree(rlc);
@@ -5613,11 +5630,15 @@ int ntfs_attr_map_cluster(struct ntfs_inode *ni, s64 vcn_start, s64 *lcn_start,
if (err) {
s64 freed;
+ ntfs_map_cluster_rehold(vol, was_delalloc, clu_count);
freed = ntfs_cluster_free(ni, vcn, clu_count, ctx);
if (freed < 0 || freed != clu_count) {
ntfs_error(vol->sb,
"Failed to free cluster allocation. Leaving inconsistent metadata.\n");
NVolSetErrors(vol);
+ /* The run keeps its clusters: drop the hold again. */
+ if (was_delalloc)
+ ntfs_release_dirty_clusters(vol, clu_count);
goto out;
}
@@ -5635,6 +5656,7 @@ int ntfs_attr_map_cluster(struct ntfs_inode *ni, s64 vcn_start, s64 *lcn_start,
"Failed to restore mapping pairs after allocation rollback.\n");
NVolSetErrors(vol);
}
+ goto out;
}
} else {
VFS_I(ni)->i_blocks += clu_count << (vol->cluster_size_bits - 9);
diff --git a/fs/ntfs/lcnalloc.c b/fs/ntfs/lcnalloc.c
index 7e00016f42..2fbc24e4c6 100644
--- a/fs/ntfs/lcnalloc.c
+++ b/fs/ntfs/lcnalloc.c
@@ -799,7 +799,6 @@ switch_to_data1_zone: search_zone = 2;
} else if (err == -ENOSPC)
ntfs_debug("No space left at all, err = -ENOSPC, first free lcn = 0x%llx.",
vol->data1_zone_pos);
- atomic64_set(&vol->dirty_clusters, 0);
out_restore:
up_write(&vol->lcnbmp_lock);
--
2.56.1
^ permalink raw reply [flat|nested] 15+ messages in thread* [PATCH v5 06/13] ntfs: release the delalloc reservations of an evicted inode
2026-12-04 18:40 [PATCH v5 00/13] ntfs: delayed allocation accounting and stale data fixes, part 1 Ernest Borowski via B4 Relay
` (4 preceding siblings ...)
2026-12-04 18:40 ` [PATCH v5 05/13] ntfs: keep delalloc reservations when a cluster allocation fails Ernest Borowski via B4 Relay
@ 2026-12-04 18:40 ` Ernest Borowski via B4 Relay
2026-12-04 18:40 ` [PATCH v5 07/13] ntfs: do not shift the cluster number of a delalloc run on split Ernest Borowski via B4 Relay
` (6 subsequent siblings)
12 siblings, 0 replies; 15+ messages in thread
From: Ernest Borowski via B4 Relay @ 2026-12-04 18:40 UTC (permalink / raw)
To: Namjae Jeon, Hyunchul Lee, Baolin Liu
Cc: Christian Brauner, Christoph Hellwig, Darrick J. Wong, ntfs,
linux-fsdevel, linux-xfs, linux-kernel, Zhan Xusheng, stable,
Baolin Liu, Ernest Borowski
From: Ernest Borowski <ernest.borowski.git@proton.me>
When an inode is evicted with LCN_DELALLOC runs left in its runlist,
truncate_inode_pages_final() drops their dirty folios and the clusters
reserved for them in vol->dirty_clusters are never given back. Only the
deletion of a base inode released anything, using the per-inode
i_dealloc_clusters counter. This leaks free space until unmount when a
named stream with delayed data is removed (ntfs_stream_finish_remove()
truncates the page cache and ntfs_attr_rm() frees only real clusters),
when a file is unlinked while one of its named streams has delayed data
(ntfs_cleanup_deleted_inode() drops the stream inode, whose reservation
is not covered by the base inode's counter), and when an inode that
keeps a delalloc run no dirty folio covers (see the next patches) is
evicted.
Reproducer: mount with streams_interface=windows, write 1 MiB to f:s,
rm f:s (or rm f), sync: statfs() reports 256 (4 KiB) clusters less free
space than after a remount.
Release the reservations of whatever delalloc runs are left in the
runlist when any ntfs inode is evicted, and drop the counter-based
release from the deletion path, which would release them twice.
Fixes: b041ca562526 ("ntfs: update iomap and address space operations")
Cc: stable@vger.kernel.org
Reviewed-by: Baolin Liu <liubaolin@kylinos.cn>
Signed-off-by: Ernest Borowski <ernest.borowski.git@proton.me>
---
fs/ntfs/attrib.c | 12 +++++++-----
fs/ntfs/attrib.h | 1 +
fs/ntfs/inode.c | 12 +++++++++++-
3 files changed, 19 insertions(+), 6 deletions(-)
diff --git a/fs/ntfs/attrib.c b/fs/ntfs/attrib.c
index 3944e32764..71fc4ca006 100644
--- a/fs/ntfs/attrib.c
+++ b/fs/ntfs/attrib.c
@@ -4427,13 +4427,15 @@ static int ntfs_attr_make_resident(struct ntfs_inode *ni, struct ntfs_attr_searc
*
* Delayed allocation reserves vol->dirty_clusters for every LCN_DELALLOC
* cluster when the buffered write is done and only gives it back when
- * writeback allocates the clusters. Runs cut off by a truncate never reach
- * writeback (their dirty folios are dropped from the page cache), so their
- * reservation has to be released here or it is lost until unmount.
+ * writeback allocates the clusters. Runs cut off by a truncate, or left in
+ * the runlist of an inode that is evicted, never reach writeback (their dirty
+ * folios are dropped from the page cache), so their reservation has to be
+ * released here or it is lost until unmount.
*
- * The runlist must be mapped completely and locked for writing.
+ * The runlist must be locked for writing. It need not be mapped completely:
+ * delalloc runs only exist in fragments of the runlist that are mapped.
*/
-static void ntfs_release_delalloc_from(struct ntfs_inode *ni, s64 start_vcn)
+void ntfs_release_delalloc_from(struct ntfs_inode *ni, s64 start_vcn)
{
struct runlist_element *rl = ni->runlist.rl;
s64 nr = 0;
diff --git a/fs/ntfs/attrib.h b/fs/ntfs/attrib.h
index 0415f817dc..8d72e9180c 100644
--- a/fs/ntfs/attrib.h
+++ b/fs/ntfs/attrib.h
@@ -110,6 +110,7 @@ int ntfs_attr_fallocate(struct ntfs_inode *ni, loff_t start, loff_t byte_len, bo
int ntfs_non_resident_attr_insert_range(struct ntfs_inode *ni, s64 start_vcn, s64 len);
int ntfs_non_resident_attr_collapse_range(struct ntfs_inode *ni, s64 start_vcn, s64 len);
int ntfs_non_resident_attr_punch_hole(struct ntfs_inode *ni, s64 start_vcn, s64 len);
+void ntfs_release_delalloc_from(struct ntfs_inode *ni, s64 start_vcn);
int __ntfs_attr_truncate_vfs(struct ntfs_inode *ni, const s64 newsize,
const s64 i_size);
int ntfs_attr_expand(struct ntfs_inode *ni, const s64 newsize, const s64 prealloc_size);
diff --git a/fs/ntfs/inode.c b/fs/ntfs/inode.c
index 3aac1d7d31..651427c31f 100644
--- a/fs/ntfs/inode.c
+++ b/fs/ntfs/inode.c
@@ -430,7 +430,6 @@ static int ntfs_non_resident_dealloc_clusters(struct ntfs_inode *ni)
}
}
- ntfs_release_dirty_clusters(ni->vol, ni->i_dealloc_clusters);
ntfs_attr_put_search_ctx(actx);
return err;
}
@@ -2470,6 +2469,17 @@ void ntfs_evict_big_inode(struct inode *vi)
truncate_inode_pages_final(&vi->i_data);
+ /*
+ * The dirty folios dropped above never reach writeback. Give back
+ * the reservations of the delalloc runs they had, for named streams
+ * and attribute inodes as well as for base inodes.
+ */
+ if (NInoNonResident(ni) && ni->runlist.rl) {
+ down_write(&ni->runlist.lock);
+ ntfs_release_delalloc_from(ni, 0);
+ up_write(&ni->runlist.lock);
+ }
+
if (!vi->i_nlink) {
if (!NInoAttr(ni)) {
/* Never called with extent inodes */
--
2.56.1
^ permalink raw reply [flat|nested] 15+ messages in thread* [PATCH v5 07/13] ntfs: do not shift the cluster number of a delalloc run on split
2026-12-04 18:40 [PATCH v5 00/13] ntfs: delayed allocation accounting and stale data fixes, part 1 Ernest Borowski via B4 Relay
` (5 preceding siblings ...)
2026-12-04 18:40 ` [PATCH v5 06/13] ntfs: release the delalloc reservations of an evicted inode Ernest Borowski via B4 Relay
@ 2026-12-04 18:40 ` Ernest Borowski via B4 Relay
2026-12-04 18:40 ` [PATCH v5 08/13] ntfs: punch out the delalloc reservation of a failed buffered write Ernest Borowski via B4 Relay
` (5 subsequent siblings)
12 siblings, 0 replies; 15+ messages in thread
From: Ernest Borowski via B4 Relay @ 2026-12-04 18:40 UTC (permalink / raw)
To: Namjae Jeon, Hyunchul Lee, Baolin Liu
Cc: Christian Brauner, Christoph Hellwig, Darrick J. Wong, ntfs,
linux-fsdevel, linux-xfs, linux-kernel, Zhan Xusheng, stable,
Baolin Liu, Ernest Borowski
From: Ernest Borowski <ernest.borowski.git@proton.me>
ntfs_rl_punch_hole() and ntfs_rl_collapse_range() advance the lcn of a
run they split with "if (lcn > LCN_HOLE) lcn += n". LCN_DELALLOC (-1)
is greater than LCN_HOLE (-2), so splitting a delalloc run turns its
tail into a run of real clusters starting at n - 1.
ntfs_rl_insert_range() advances it unconditionally; its callers insert
holes, so only a hole split for a non-hole source and a split delalloc
run are affected. ntfs_rle_lcn_contiguous() has the same problem and
can merge a delalloc run with a following real run.
The punched-out part then goes to ntfs_cluster_free_from_rl(), which
frees clusters that belong to other files or to metadata, and the part
left in the runlist maps the file onto them. Delalloc runs normally do
not survive the writeback that fallocate() does first, but a buffered
write that fails after ->iomap_begin() leaves one without dirty folios
(fixed by the next patch); FALLOC_FL_PUNCH_HOLE or COLLAPSE_RANGE
through it then corrupts the volume (ntfsck: "Found 1 inconsistent
bitmap block(s)").
Only shift the lcn of runs that have real clusters.
Fixes: 11ccc9107dc4 ("ntfs: update runlist handling and cluster allocator")
Cc: stable@vger.kernel.org
Reviewed-by: Baolin Liu <liubaolin@kylinos.cn>
Signed-off-by: Ernest Borowski <ernest.borowski.git@proton.me>
---
fs/ntfs/runlist.c | 13 +++++++------
1 file changed, 7 insertions(+), 6 deletions(-)
diff --git a/fs/ntfs/runlist.c b/fs/ntfs/runlist.c
index 76ba6ab72b..1e87db35ce 100644
--- a/fs/ntfs/runlist.c
+++ b/fs/ntfs/runlist.c
@@ -1770,7 +1770,7 @@ s64 ntfs_rl_get_compressed_size(struct ntfs_volume *vol, struct runlist_element
static inline bool ntfs_rle_lcn_contiguous(struct runlist_element *left_rle,
struct runlist_element *right_rle)
{
- if (left_rle->lcn > LCN_HOLE &&
+ if (left_rle->lcn >= 0 &&
left_rle->lcn + left_rle->length == right_rle->lcn)
return true;
else if (left_rle->lcn == LCN_HOLE && right_rle->lcn == LCN_HOLE)
@@ -1832,7 +1832,8 @@ struct runlist_element *ntfs_rl_insert_range(struct runlist_element *dst_rl, int
i_rl[0].vcn = start_vcn;
i_rl[0].length -= dst_rl_split.length;
- i_rl[0].lcn += dst_rl_split.length;
+ if (i_rl[0].lcn >= 0)
+ i_rl[0].lcn += dst_rl_split.length;
} else {
struct runlist_element *dst_rle, *src_rle;
merge_src_rle:
@@ -1988,7 +1989,7 @@ struct runlist_element *ntfs_rl_punch_hole(struct runlist_element *dst_rl, int d
(*punch_rl)[0].vcn = start_vcn;
(*punch_rl)[0].length -= first_cnt;
- if ((*punch_rl)[0].lcn > LCN_HOLE)
+ if ((*punch_rl)[0].lcn >= 0)
(*punch_rl)[0].lcn += first_cnt;
}
@@ -2008,7 +2009,7 @@ struct runlist_element *ntfs_rl_punch_hole(struct runlist_element *dst_rl, int d
new_rl[new_1st_cnt + 1].vcn = end_vcn + 1;
new_rl[new_1st_cnt + 1].length -= first_cnt;
- if (new_rl[new_1st_cnt + 1].lcn > LCN_HOLE)
+ if (new_rl[new_1st_cnt + 1].lcn >= 0)
new_rl[new_1st_cnt + 1].lcn += first_cnt;
if (one_split_3)
@@ -2136,7 +2137,7 @@ struct runlist_element *ntfs_rl_collapse_range(struct runlist_element *dst_rl, i
(*punch_rl)[0].vcn = start_vcn;
(*punch_rl)[0].length -= first_cnt;
- if ((*punch_rl)[0].lcn > LCN_HOLE)
+ if ((*punch_rl)[0].lcn >= 0)
(*punch_rl)[0].lcn += first_cnt;
}
@@ -2150,7 +2151,7 @@ struct runlist_element *ntfs_rl_collapse_range(struct runlist_element *dst_rl, i
new_rl[new_1st_cnt].vcn = end_vcn + 1;
new_rl[new_1st_cnt].length -= first_cnt;
- if (new_rl[new_1st_cnt].lcn > LCN_HOLE)
+ if (new_rl[new_1st_cnt].lcn >= 0)
new_rl[new_1st_cnt].lcn += first_cnt;
if (one_split_3)
--
2.56.1
^ permalink raw reply [flat|nested] 15+ messages in thread* [PATCH v5 08/13] ntfs: punch out the delalloc reservation of a failed buffered write
2026-12-04 18:40 [PATCH v5 00/13] ntfs: delayed allocation accounting and stale data fixes, part 1 Ernest Borowski via B4 Relay
` (6 preceding siblings ...)
2026-12-04 18:40 ` [PATCH v5 07/13] ntfs: do not shift the cluster number of a delalloc run on split Ernest Borowski via B4 Relay
@ 2026-12-04 18:40 ` Ernest Borowski via B4 Relay
2026-12-04 18:40 ` [PATCH v5 09/13] ntfs: zero new delalloc clusters even when writeback allocates them Ernest Borowski via B4 Relay
` (4 subsequent siblings)
12 siblings, 0 replies; 15+ messages in thread
From: Ernest Borowski via B4 Relay @ 2026-12-04 18:40 UTC (permalink / raw)
To: Namjae Jeon, Hyunchul Lee, Baolin Liu
Cc: Christian Brauner, Christoph Hellwig, Darrick J. Wong, ntfs,
linux-fsdevel, linux-xfs, linux-kernel, Zhan Xusheng, stable,
Ernest Borowski
From: Ernest Borowski <ernest.borowski.git@proton.me>
ntfs_write_simple_iomap_begin_non_resident() turns the holes a buffered
write covers into LCN_DELALLOC runs and reserves their clusters before
any data is copied. If the copy then fails or comes up short (EFAULT
from the user buffer, a fatal signal), the clusters that no dirty folio
covers stay delalloc. Writeback never allocates them, so the
reservation is kept until the inode is evicted, and the stale runs
survive the writeback that fallocate() relies on before it edits the
runlist.
Reproducer: a pwrite() of 512 KiB from an unmapped address into a hole
of a 1 MiB sparse file fails with EFAULT and leaves 128 (4 KiB) clusters
reserved after sync. A FALLOC_FL_PUNCH_HOLE or COLLAPSE_RANGE through
the range then hits the run splitting bug fixed by the previous patch.
Do what XFS does: in ->iomap_end() of a delalloc mapping that was not
written completely, let iomap_write_delalloc_release() find the ranges
without dirty data and turn the clusters entirely inside them back into
holes, releasing their reservation. Clusters that still hold dirty data
stay delalloc, including the partial first and last clusters of an
unaligned write that ->iomap_begin() zeroed in the page cache; those are
written back as zeros, as before. Delalloc runs are holes on disk, so
the mapping pairs do not change.
The error path of ->iomap_begin() itself, taken when zeroing the edges
of the new run fails, only subtracted the reservation and left the run
and i_dealloc_clusters in place, so a later truncate or eviction would
release it a second time. Use the same release there.
A failed write that moved the file size is then rolled back by
ntfs_write_rollback(), which drops the folios past the old size and
truncates the attribute back to the old allocation, so a delalloc
cluster that ->iomap_end() kept for its dirty folio is released by that
truncate. A FALLOC_FL_KEEP_SIZE preallocation past a hole keeps the
allocation, so a zeroed edge cluster in that hole stayed reserved. Punch
out the delalloc clusters of the write that the rollback leaves wholly
past the new size and below what the truncate keeps.
Fixes: b041ca562526 ("ntfs: update iomap and address space operations")
Cc: stable@vger.kernel.org
Signed-off-by: Ernest Borowski <ernest.borowski.git@proton.me>
---
fs/ntfs/attrib.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++
fs/ntfs/attrib.h | 1 +
fs/ntfs/file.c | 29 +++++++++++++++++++++++
fs/ntfs/iomap.c | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-
4 files changed, 163 insertions(+), 1 deletion(-)
diff --git a/fs/ntfs/attrib.c b/fs/ntfs/attrib.c
index 71fc4ca006..3de589ca0b 100644
--- a/fs/ntfs/attrib.c
+++ b/fs/ntfs/attrib.c
@@ -4453,6 +4453,69 @@ void ntfs_release_delalloc_from(struct ntfs_inode *ni, s64 start_vcn)
ni->i_dealloc_clusters -= min_t(s64, nr, ni->i_dealloc_clusters);
}
+/*
+ * ntfs_attr_punch_delalloc - turn delalloc runs back into holes
+ * @ni: non-resident ntfs attribute
+ * @start_vcn: first vcn of the range
+ * @len: number of clusters in the range
+ *
+ * Turn the LCN_DELALLOC parts of [@start_vcn, @start_vcn + @len) into holes
+ * and give back their reservation. Used when a buffered write that reserved
+ * them failed before it dirtied any folio over them, so that writeback will
+ * never allocate them. Runs that are not delalloc (anymore) are left alone.
+ * Delalloc runs are stored as holes on disk, so the mapping pairs need no
+ * update.
+ *
+ * The caller holds the mrec_lock of the base inode and the runlist lock for
+ * writing.
+ */
+void ntfs_attr_punch_delalloc(struct ntfs_inode *ni, s64 start_vcn, s64 len)
+{
+ struct runlist_element *rl, *punch_rl;
+ s64 vcn = start_vcn, end_vcn = start_vcn + len, run_end;
+ size_t new_rl_count;
+ int rl_count, err;
+
+ lockdep_assert_held_write(&ni->runlist.lock);
+
+ err = ntfs_attr_map_whole_runlist(ni);
+ if (err) {
+ ntfs_error(ni->vol->sb,
+ "Failed to map the runlist to drop delalloc runs: %d", err);
+ return;
+ }
+
+ while (vcn < end_vcn) {
+ rl = ntfs_rl_find_vcn_nolock(ni->runlist.rl, vcn);
+ if (!rl || !rl->length)
+ break;
+ run_end = min(rl->vcn + rl->length, end_vcn);
+ if (rl->lcn != LCN_DELALLOC) {
+ vcn = run_end;
+ continue;
+ }
+
+ for (rl = ni->runlist.rl, rl_count = 0; rl->length; rl++)
+ rl_count++;
+ rl = ntfs_rl_punch_hole(ni->runlist.rl, rl_count + 1, vcn,
+ run_end - vcn, &punch_rl, &new_rl_count);
+ if (IS_ERR(rl)) {
+ ntfs_error(ni->vol->sb,
+ "Failed to drop delalloc run at vcn 0x%llx: %ld",
+ vcn, PTR_ERR(rl));
+ break;
+ }
+ kvfree(punch_rl);
+ ni->runlist.rl = rl;
+ ni->runlist.count = new_rl_count;
+
+ ntfs_release_dirty_clusters(ni->vol, run_end - vcn);
+ ni->i_dealloc_clusters -= min_t(s64, run_end - vcn,
+ ni->i_dealloc_clusters);
+ vcn = run_end;
+ }
+}
+
/*
* ntfs_non_resident_attr_shrink - shrink a non-resident, open ntfs attribute
* @ni: non-resident ntfs attribute to shrink
diff --git a/fs/ntfs/attrib.h b/fs/ntfs/attrib.h
index 8d72e9180c..dd073f9fd7 100644
--- a/fs/ntfs/attrib.h
+++ b/fs/ntfs/attrib.h
@@ -111,6 +111,7 @@ int ntfs_non_resident_attr_insert_range(struct ntfs_inode *ni, s64 start_vcn, s6
int ntfs_non_resident_attr_collapse_range(struct ntfs_inode *ni, s64 start_vcn, s64 len);
int ntfs_non_resident_attr_punch_hole(struct ntfs_inode *ni, s64 start_vcn, s64 len);
void ntfs_release_delalloc_from(struct ntfs_inode *ni, s64 start_vcn);
+void ntfs_attr_punch_delalloc(struct ntfs_inode *ni, s64 start_vcn, s64 len);
int __ntfs_attr_truncate_vfs(struct ntfs_inode *ni, const s64 newsize,
const s64 i_size);
int ntfs_attr_expand(struct ntfs_inode *ni, const s64 newsize, const s64 prealloc_size);
diff --git a/fs/ntfs/file.c b/fs/ntfs/file.c
index 31ff35176e..a66b615ddd 100644
--- a/fs/ntfs/file.c
+++ b/fs/ntfs/file.c
@@ -923,6 +923,33 @@ static int ntfs_zero_range_unkillable(struct inode *inode, loff_t pos,
return zw.err;
}
+/*
+ * ->iomap_end() punches out the delalloc clusters of a failed write that no
+ * dirty folio covers, and keeps those that do, such as a cluster that
+ * ->iomap_begin() zeroed in part. The rollback then drops such a folio along
+ * with the size, and its cluster, wholly past the new size, would stay
+ * reserved: writeback never allocates it. __ntfs_attr_truncate_vfs()
+ * releases what lies past @keep, so punch out the clusters of [@pos, @end)
+ * between the new size and @keep, where a FALLOC_FL_KEEP_SIZE preallocation
+ * keeps the attribute allocated. The caller holds the invalidate lock and
+ * the base inode's mrec_lock, and has truncated the page cache to @new_size.
+ */
+static void ntfs_write_punch_past_eof(struct ntfs_inode *ni, loff_t pos,
+ loff_t end, loff_t new_size, loff_t keep)
+{
+ struct ntfs_volume *vol = ni->vol;
+ loff_t start = max(round_up(new_size, vol->cluster_size),
+ round_down(pos, vol->cluster_size));
+ loff_t stop = min(keep, round_up(end, vol->cluster_size));
+
+ if (start >= stop)
+ return;
+ down_write(&ni->runlist.lock);
+ ntfs_attr_punch_delalloc(ni, ntfs_bytes_to_cluster(vol, start),
+ ntfs_bytes_to_cluster(vol, stop - start));
+ up_write(&ni->runlist.lock);
+}
+
/*
* Both sizes were moved to the end of the write before the copy, so a short
* write, like a failed one, must take back what it did not write. @written
@@ -1023,6 +1050,8 @@ static void ntfs_write_rollback(struct kiocb *iocb, ssize_t written,
loff_t keep = max(new_data_size,
old_alloc_size);
+ ntfs_write_punch_past_eof(ni, pos, end, new_data_size,
+ keep);
if (ni->allocated_size > keep)
data_err = __ntfs_attr_truncate_vfs(ni,
keep, keep);
diff --git a/fs/ntfs/iomap.c b/fs/ntfs/iomap.c
index 8124c5f627..dbf1ee9e4a 100644
--- a/fs/ntfs/iomap.c
+++ b/fs/ntfs/iomap.c
@@ -377,6 +377,10 @@ static int ntfs_zero_range(struct inode *inode, loff_t offset, loff_t length)
NULL);
}
+static void ntfs_write_delalloc_release(struct inode *inode, loff_t pos,
+ loff_t length, ssize_t written,
+ unsigned int flags, struct iomap *iomap);
+
static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_t offset,
loff_t length, struct iomap *iomap)
{
@@ -548,7 +552,13 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_
if (err == -EPERM)
err = 0;
if (err) {
- ntfs_release_dirty_clusters(vol, max_clu_count);
+ /*
+ * Nothing will be copied: drop the delalloc
+ * clusters that no zeroed folio covers.
+ */
+ ntfs_write_delalloc_release(inode, offset,
+ iomap->length, 0,
+ IOMAP_WRITE, iomap);
return err;
}
}
@@ -767,6 +777,61 @@ static int ntfs_write_iomap_end_resident(struct inode *inode, loff_t pos,
return written;
}
+static void ntfs_write_delalloc_punch(struct inode *inode, loff_t offset,
+ loff_t length, struct iomap *iomap)
+{
+ struct ntfs_inode *ni = NTFS_I(inode);
+ struct ntfs_inode *mrec_ni = ntfs_inode_is_named_stream(ni) ?
+ ntfs_base_inode(ni) : ni;
+ struct ntfs_volume *vol = ni->vol;
+ /* Clusters that still hold some dirty data stay delalloc. */
+ loff_t start = round_up(offset, vol->cluster_size);
+ loff_t end = round_down(offset + length, vol->cluster_size);
+
+ if (start >= end)
+ return;
+
+ mutex_lock(&mrec_ni->mrec_lock);
+ down_write(&ni->runlist.lock);
+ ntfs_attr_punch_delalloc(ni, ntfs_bytes_to_cluster(vol, start),
+ ntfs_bytes_to_cluster(vol, end - start));
+ up_write(&ni->runlist.lock);
+ mutex_unlock(&mrec_ni->mrec_lock);
+}
+
+/*
+ * A buffered write that failed or copied less than it mapped leaves delalloc
+ * clusters that no dirty folio covers: writeback never allocates them, so
+ * their reservation would stay until the inode is evicted, and the stale
+ * delalloc runs would outlive the flush done by fallocate() and friends.
+ * Punch them out again, keeping every cluster that has dirty data in the
+ * page cache.
+ */
+static void ntfs_write_delalloc_release(struct inode *inode, loff_t pos,
+ loff_t length, ssize_t written,
+ unsigned int flags, struct iomap *iomap)
+{
+ struct ntfs_volume *vol = NTFS_I(inode)->vol;
+ loff_t start_byte, end_byte;
+
+ /*
+ * Delalloc is reserved per cluster, which can be larger than the
+ * block size iomap_last_written_block() would round to.
+ */
+ if (written)
+ start_byte = round_up(pos + written, vol->cluster_size);
+ else
+ start_byte = round_down(pos, vol->cluster_size);
+ end_byte = round_up(pos + length, vol->cluster_size);
+ if (start_byte >= end_byte)
+ return;
+
+ filemap_invalidate_lock(inode->i_mapping);
+ iomap_write_delalloc_release(inode, start_byte, end_byte, flags, iomap,
+ ntfs_write_delalloc_punch);
+ filemap_invalidate_unlock(inode->i_mapping);
+}
+
static int ntfs_write_iomap_end(struct inode *inode, loff_t pos, loff_t length,
ssize_t written, unsigned int flags,
struct iomap *iomap)
@@ -774,6 +839,10 @@ static int ntfs_write_iomap_end(struct inode *inode, loff_t pos, loff_t length,
if (iomap->type == IOMAP_INLINE)
return ntfs_write_iomap_end_resident(inode, pos, length,
written, flags, iomap);
+ if (iomap->type == IOMAP_DELALLOC && iomap->flags & IOMAP_F_NEW &&
+ !(flags & IOMAP_FAULT) && written < length)
+ ntfs_write_delalloc_release(inode, pos, length, written, flags,
+ iomap);
return written;
}
--
2.56.1
^ permalink raw reply [flat|nested] 15+ messages in thread* [PATCH v5 09/13] ntfs: zero new delalloc clusters even when writeback allocates them
2026-12-04 18:40 [PATCH v5 00/13] ntfs: delayed allocation accounting and stale data fixes, part 1 Ernest Borowski via B4 Relay
` (7 preceding siblings ...)
2026-12-04 18:40 ` [PATCH v5 08/13] ntfs: punch out the delalloc reservation of a failed buffered write Ernest Borowski via B4 Relay
@ 2026-12-04 18:40 ` Ernest Borowski via B4 Relay
2026-12-04 18:40 ` [PATCH v5 10/13] ntfs: zero the rest of a new cluster after a short buffered write Ernest Borowski via B4 Relay
` (3 subsequent siblings)
12 siblings, 0 replies; 15+ messages in thread
From: Ernest Borowski via B4 Relay @ 2026-12-04 18:40 UTC (permalink / raw)
To: Namjae Jeon, Hyunchul Lee, Baolin Liu
Cc: Christian Brauner, Christoph Hellwig, Darrick J. Wong, ntfs,
linux-fsdevel, linux-xfs, linux-kernel, Zhan Xusheng, stable,
Baolin Liu, Ernest Borowski
From: Ernest Borowski <ernest.borowski.git@proton.me>
When a buffered write starts or ends inside a cluster that is a hole,
ntfs_write_simple_iomap_begin_non_resident() turns the hole into an
LCN_DELALLOC run and then zeroes the part of the first and last cluster
that the write does not cover, in the page cache, so that writeback
writes whole clusters. initialized_size already covers that part, and
the clusters writeback allocates are not zeroed on disk.
The zeroing runs after mrec_lock and the runlist lock are dropped, and
its ->iomap_valid() check, ntfs_iomap_valid(), calls the mapping stale
as soon as the run is no longer delalloc. Writeback that runs meanwhile
(the background flusher, or FIEMAP_FLAG_SYNC and sync_file_range(),
which do not take the inode lock) picks up the folios that were zeroed
first, and ntfs_attr_map_cluster() allocates the whole delalloc run for
them. iomap_zero_range() then stops with -EPERM from
ntfs_zero_read_iomap_end(), which the caller ignores. The rest of the
cluster is neither in the page cache nor on disk, so reads return what
the reallocated clusters held before: data of deleted or truncated files.
Reproducer: fill a file, fsync, truncate it to 0 and back up, then write
small pieces at unaligned offsets into the holes while other threads
call FIEMAP with FIEMAP_FLAG_SYNC. With 4 KiB and 64 KiB clusters the
old contents show up within seconds, also without the FIEMAP threads.
The validity check was there to keep the zeroing from overwriting data
that an mmap write put into the new cluster in that window. Hold the
invalidate lock across ->iomap_begin() of a buffered write instead,
which keeps page faults out from the moment the hole becomes delalloc
until it is zeroed, and zero with the plain seek mapping, which zeroes
delalloc and mapped ranges alike, so that writeback allocating the run
no longer matters. The error path of ->iomap_begin() now runs with the
invalidate lock held.
Fixes: b041ca562526 ("ntfs: update iomap and address space operations")
Cc: stable@vger.kernel.org
Reviewed-by: Baolin Liu <liubaolin@kylinos.cn>
Signed-off-by: Ernest Borowski <ernest.borowski.git@proton.me>
---
fs/ntfs/iomap.c | 127 ++++++++++++++++++++++++--------------------------------
1 file changed, 55 insertions(+), 72 deletions(-)
diff --git a/fs/ntfs/iomap.c b/fs/ntfs/iomap.c
index dbf1ee9e4a..112319e19e 100644
--- a/fs/ntfs/iomap.c
+++ b/fs/ntfs/iomap.c
@@ -302,39 +302,6 @@ static int ntfs_read_iomap_end(struct inode *inode, loff_t pos, loff_t length,
DEFINE_IOMAP_ITER_NEXT_END(ntfs_read_iomap_next, ntfs_read_iomap_begin,
ntfs_read_iomap_end);
-/*
- * Check that the cached iomap still matches the NTFS runlist before
- * iomap_zero_range() is called. if the runlist changes while iomap is
- * iterating a cached iomap, iomap_zero_range() may overwrite folios
- * that have been already written with valid data.
- */
-static bool ntfs_iomap_valid(struct inode *inode, const struct iomap *iomap)
-{
- struct ntfs_inode *ni = NTFS_I(inode);
- struct runlist_element *rl;
- s64 vcn, lcn;
-
- if (!NInoNonResident(ni))
- return false;
-
- vcn = iomap->offset >> ni->vol->cluster_size_bits;
-
- down_read(&ni->runlist.lock);
- rl = __ntfs_attr_find_vcn_nolock(&ni->runlist, vcn);
- if (IS_ERR(rl)) {
- up_read(&ni->runlist.lock);
- return false;
- }
- lcn = ntfs_rl_vcn_to_lcn(rl, vcn);
- up_read(&ni->runlist.lock);
- return lcn == LCN_DELALLOC;
-}
-
-static const struct iomap_write_ops ntfs_zero_iomap_folio_ops = {
- .put_folio = ntfs_iomap_put_folio,
- .iomap_valid = ntfs_iomap_valid,
-};
-
static int ntfs_seek_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
unsigned int flags, struct iomap *iomap, struct iomap *srcmap)
{
@@ -342,17 +309,6 @@ static int ntfs_seek_iomap_begin(struct inode *inode, loff_t offset, loff_t leng
srcmap, false, false);
}
-static int ntfs_zero_read_iomap_end(struct inode *inode, loff_t pos, loff_t length,
- ssize_t written, unsigned int flags, struct iomap *iomap)
-{
- if ((flags & IOMAP_ZERO) && (iomap->flags & IOMAP_F_STALE))
- return -EPERM;
- return written;
-}
-
-static DEFINE_IOMAP_ITER_NEXT_END(ntfs_zero_read_iomap_next,
- ntfs_seek_iomap_begin, ntfs_zero_read_iomap_end);
-
DEFINE_IOMAP_ITER_NEXT(ntfs_seek_iomap_next, ntfs_seek_iomap_begin);
int ntfs_dio_zero_range(struct inode *inode, loff_t offset, loff_t length)
@@ -367,19 +323,28 @@ int ntfs_dio_zero_range(struct inode *inode, loff_t offset, loff_t length)
BLKDEV_ZERO_NOUNMAP);
}
+/*
+ * Zero the part of a new delalloc cluster that the buffered write does not
+ * cover, in the page cache, so that writeback writes the whole cluster.
+ * Writeback can allocate the delalloc run while this runs (the folios
+ * zeroed first are dirty), so the zeroing must not depend on the mapping
+ * staying delalloc: iomap_zero_range() zeroes delalloc and mapped ranges
+ * alike. The caller holds the invalidate lock, which keeps page faults
+ * from putting data into the cluster meanwhile.
+ */
static int ntfs_zero_range(struct inode *inode, loff_t offset, loff_t length)
{
- return iomap_zero_range(inode,
- offset, length,
- NULL,
- ntfs_zero_read_iomap_next,
- &ntfs_zero_iomap_folio_ops,
+ lockdep_assert_held_write(&inode->i_mapping->invalidate_lock);
+
+ return iomap_zero_range(inode, offset, length, NULL,
+ ntfs_seek_iomap_next, &ntfs_iomap_folio_ops,
NULL);
}
-static void ntfs_write_delalloc_release(struct inode *inode, loff_t pos,
- loff_t length, ssize_t written,
- unsigned int flags, struct iomap *iomap);
+static void __ntfs_write_delalloc_release(struct inode *inode, loff_t pos,
+ loff_t length, ssize_t written,
+ unsigned int flags,
+ struct iomap *iomap);
static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_t offset,
loff_t length, struct iomap *iomap)
@@ -533,8 +498,7 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_
z_start,
z_end - z_start);
}
- if ((!err || err == -EPERM) &&
- max_clu_count > 1 &&
+ if (!err && max_clu_count > 1 &&
(iomap->length & vol->cluster_size_mask &&
end < ni->initialized_size)) {
loff_t z_start, z_end;
@@ -549,16 +513,15 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_
z_end - z_start);
}
- if (err == -EPERM)
- err = 0;
if (err) {
/*
* Nothing will be copied: drop the delalloc
* clusters that no zeroed folio covers.
*/
- ntfs_write_delalloc_release(inode, offset,
- iomap->length, 0,
- IOMAP_WRITE, iomap);
+ __ntfs_write_delalloc_release(inode, offset,
+ iomap->length, 0,
+ IOMAP_WRITE,
+ iomap);
return err;
}
}
@@ -726,16 +689,27 @@ static int ntfs_write_iomap_begin_non_resident(struct inode *inode, loff_t offse
struct ntfs_inode *mrec_ni = ntfs_inode_is_named_stream(ni) ?
ntfs_base_inode(ni) : ni;
- mutex_lock(&mrec_ni->mrec_lock);
+ if (ntfs_iomap_flags & NTFS_IOMAP_FLAGS_BEGIN) {
+ int err;
- if (ntfs_iomap_flags & NTFS_IOMAP_FLAGS_BEGIN)
- return ntfs_write_simple_iomap_begin_non_resident(inode, offset,
- length, iomap);
- else
- return ntfs_write_da_iomap_begin_non_resident(inode,
- offset, length,
- flags, iomap,
- ntfs_iomap_flags);
+ /*
+ * A hole becomes delalloc here and the rest of its partial
+ * clusters is zeroed after the locks below are dropped: keep
+ * page faults, which could write into those clusters, out
+ * until then.
+ */
+ filemap_invalidate_lock(inode->i_mapping);
+ mutex_lock(&mrec_ni->mrec_lock);
+ err = ntfs_write_simple_iomap_begin_non_resident(inode, offset,
+ length, iomap);
+ filemap_invalidate_unlock(inode->i_mapping);
+ return err;
+ }
+
+ mutex_lock(&mrec_ni->mrec_lock);
+ return ntfs_write_da_iomap_begin_non_resident(inode, offset, length,
+ flags, iomap,
+ ntfs_iomap_flags);
}
static int __ntfs_write_iomap_begin(struct inode *inode, loff_t offset,
@@ -807,9 +781,10 @@ static void ntfs_write_delalloc_punch(struct inode *inode, loff_t offset,
* Punch them out again, keeping every cluster that has dirty data in the
* page cache.
*/
-static void ntfs_write_delalloc_release(struct inode *inode, loff_t pos,
- loff_t length, ssize_t written,
- unsigned int flags, struct iomap *iomap)
+static void __ntfs_write_delalloc_release(struct inode *inode, loff_t pos,
+ loff_t length, ssize_t written,
+ unsigned int flags,
+ struct iomap *iomap)
{
struct ntfs_volume *vol = NTFS_I(inode)->vol;
loff_t start_byte, end_byte;
@@ -826,9 +801,17 @@ static void ntfs_write_delalloc_release(struct inode *inode, loff_t pos,
if (start_byte >= end_byte)
return;
- filemap_invalidate_lock(inode->i_mapping);
iomap_write_delalloc_release(inode, start_byte, end_byte, flags, iomap,
ntfs_write_delalloc_punch);
+}
+
+static void ntfs_write_delalloc_release(struct inode *inode, loff_t pos,
+ loff_t length, ssize_t written,
+ unsigned int flags, struct iomap *iomap)
+{
+ filemap_invalidate_lock(inode->i_mapping);
+ __ntfs_write_delalloc_release(inode, pos, length, written, flags,
+ iomap);
filemap_invalidate_unlock(inode->i_mapping);
}
--
2.56.1
^ permalink raw reply [flat|nested] 15+ messages in thread* [PATCH v5 10/13] ntfs: zero the rest of a new cluster after a short buffered write
2026-12-04 18:40 [PATCH v5 00/13] ntfs: delayed allocation accounting and stale data fixes, part 1 Ernest Borowski via B4 Relay
` (8 preceding siblings ...)
2026-12-04 18:40 ` [PATCH v5 09/13] ntfs: zero new delalloc clusters even when writeback allocates them Ernest Borowski via B4 Relay
@ 2026-12-04 18:40 ` Ernest Borowski via B4 Relay
2026-12-04 18:40 ` [PATCH v5 11/13] ntfs: revalidate a buffered write's mapping after its clusters were allocated Ernest Borowski via B4 Relay
` (2 subsequent siblings)
12 siblings, 0 replies; 15+ messages in thread
From: Ernest Borowski via B4 Relay @ 2026-12-04 18:40 UTC (permalink / raw)
To: Namjae Jeon, Hyunchul Lee, Baolin Liu
Cc: Christian Brauner, Christoph Hellwig, Darrick J. Wong, ntfs,
linux-fsdevel, linux-xfs, linux-kernel, Zhan Xusheng, stable,
Ernest Borowski
From: Ernest Borowski <ernest.borowski.git@proton.me>
ntfs_write_simple_iomap_begin_non_resident() turns the holes a buffered
write covers into LCN_DELALLOC runs, and zeroes in the page cache only
the parts of the first and last cluster that the write will not cover.
When the copy from user space stops inside a cluster (the source buffer
runs into an unmapped page, or a fatal signal), only the bytes copied
are dirty. Writeback allocates the whole cluster and writes the dirty
blocks, and the rest of the cluster keeps what the reused clusters held
before. Below initialized_size, for example a write into a hole of a
sparse file, reads return that old data.
Reproducer: fill a file, fsync, truncate it to 0 and back up, write the
last byte, then pwrite() 2.5 clusters at a cluster boundary from a
buffer whose readable part ends 5096 bytes in: the write returns 5096,
and after fsync and drop_caches the rest of the second cluster (4 KiB
clusters) or of the first (64 KiB clusters) reads back the old file
contents.
Mark the mappings that ->iomap_begin() made out of a hole, and on a
short copy make writeback write the rest of the cluster the copy stopped
in, before the unused delalloc clusters after it are punched out. This
runs under the invalidate lock. While the cluster is still delalloc,
only the write and its zeroing of the cluster's edges have put data
there (a write fault would have allocated it), so zero the rest of the
cluster: iomap_zero_range() dirties exactly the blocks it zeroes. Once
a write fault or writeback has allocated the cluster during the copy,
an mmap write's data may be on the disk only, and zeroing would
overwrite it. Then mark the blocks of the range in folios in the page
cache dirty as they are, with iomap_dirty_folio_range(), and leave the
rest (the last two patches of this series make both zero a partly
written cluster on the disk before mapping it, and until then writeback
that allocates the cluster while the copy is stalled leaves the rest of
it unzeroed). Either way the other blocks of those folios stay clean,
so the punch still releases the clusters the write did not reach. When
the writer has a fatal signal pending, iomap_zero_range() refuses to run
in its context (-EINTR), so the zeroing goes through
ntfs_zero_range_unkillable(), which the short write fix added to file.c
for the same case and which zeroes from a worker then. It is no longer
static, and it now checks with lockdep that its caller holds the
invalidate lock, as both callers do.
If the zeroing fails, the write still returns the bytes it copied, so
the error is also recorded in the mapping with mapping_set_error(), and
a later fsync() reports it.
Fixes: b041ca562526 ("ntfs: update iomap and address space operations")
Cc: stable@vger.kernel.org
Suggested-by: Hyunchul Lee <hyc.lee@gmail.com>
Signed-off-by: Ernest Borowski <ernest.borowski.git@proton.me>
---
fs/ntfs/file.c | 5 ++--
fs/ntfs/iomap.c | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
fs/ntfs/iomap.h | 1 +
3 files changed, 94 insertions(+), 2 deletions(-)
diff --git a/fs/ntfs/file.c b/fs/ntfs/file.c
index a66b615ddd..036f4deac9 100644
--- a/fs/ntfs/file.c
+++ b/fs/ntfs/file.c
@@ -901,12 +901,13 @@ static void ntfs_zero_workfn(struct work_struct *work)
* still be zeroed: zero from a worker then, which has no signal to see. The
* caller holds the invalidate lock, which keeps page faults out meanwhile.
*/
-static int ntfs_zero_range_unkillable(struct inode *inode, loff_t pos,
- loff_t len)
+int ntfs_zero_range_unkillable(struct inode *inode, loff_t pos, loff_t len)
{
struct ntfs_zero_work zw = { .inode = inode, .pos = pos, .len = len };
int err;
+ lockdep_assert_held_write(&inode->i_mapping->invalidate_lock);
+
if (!fatal_signal_pending(current)) {
err = iomap_zero_range(inode, pos, len, NULL,
ntfs_seek_iomap_next,
diff --git a/fs/ntfs/iomap.c b/fs/ntfs/iomap.c
index 112319e19e..820a89c640 100644
--- a/fs/ntfs/iomap.c
+++ b/fs/ntfs/iomap.c
@@ -12,6 +12,9 @@
#include "ntfs.h"
#include "iomap.h"
+/* iomap->flags: ->iomap_begin() turned a hole into this delalloc mapping */
+#define NTFS_IOMAP_F_WAS_HOLE IOMAP_F_PRIVATE
+
static void ntfs_iomap_put_folio_non_resident(struct inode *inode, loff_t pos,
unsigned int len, struct folio *folio)
{
@@ -486,6 +489,9 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_
if (lcn <= LCN_HOLE) {
loff_t end = offset + length;
+ /* The whole mapping was a hole before this write. */
+ iomap->flags |= NTFS_IOMAP_F_WAS_HOLE;
+
if (vcn_ofs || ((vol->cluster_size > iomap->length) &&
end < ni->initialized_size)) {
loff_t z_start, z_end;
@@ -805,11 +811,95 @@ static void __ntfs_write_delalloc_release(struct inode *inode, loff_t pos,
ntfs_write_delalloc_punch);
}
+/* True if the cluster at byte @pos is still a delalloc run. */
+static bool ntfs_pos_is_delalloc(struct inode *inode, loff_t pos)
+{
+ struct ntfs_inode *ni = NTFS_I(inode);
+ s64 vcn = ntfs_bytes_to_cluster(ni->vol, pos);
+ struct runlist_element *rl;
+ s64 lcn;
+
+ down_read(&ni->runlist.lock);
+ rl = __ntfs_attr_find_vcn_nolock(&ni->runlist, vcn);
+ lcn = IS_ERR(rl) ? LCN_EINVAL : ntfs_rl_vcn_to_lcn(rl, vcn);
+ up_read(&ni->runlist.lock);
+ return lcn == LCN_DELALLOC;
+}
+
+/*
+ * Make writeback write all of [start, end), the rest of a cluster that was a
+ * hole and that a short buffered write filled only in part. While the
+ * cluster is still delalloc, nothing but the write and its zeroing of the
+ * cluster's edges has put data there (a write fault would have allocated
+ * it), so zero the range: iomap_zero_range() dirties just the blocks it
+ * zeroes. Once a write fault or writeback has allocated the cluster, what
+ * an mmap write put there since may be on the disk only, and zeroing would
+ * overwrite it: then the blocks of the range in folios in the page cache are
+ * marked dirty as they are. Either way the other clean blocks of a folio
+ * stay clean: they may be holes, or delalloc clusters the write did not
+ * reach, which the caller punches out. The caller holds the invalidate lock.
+ */
+static int ntfs_dirty_cluster_tail(struct inode *inode, loff_t start,
+ loff_t end)
+{
+ struct folio *folio;
+ loff_t next;
+
+ lockdep_assert_held_write(&inode->i_mapping->invalidate_lock);
+ /* [start, end) is part of a single cluster */
+ if (ntfs_pos_is_delalloc(inode, start))
+ return ntfs_zero_range_unkillable(inode, start, end - start);
+
+ for (; start < end; start = next) {
+ folio = filemap_lock_folio(inode->i_mapping,
+ start >> PAGE_SHIFT);
+ if (IS_ERR(folio)) {
+ next = min_t(loff_t, round_up(start + 1, PAGE_SIZE),
+ end);
+ continue;
+ }
+ next = min_t(loff_t, folio_pos(folio) + folio_size(folio),
+ end);
+ if (folio_test_uptodate(folio))
+ iomap_dirty_folio_range(folio,
+ offset_in_folio(folio, start),
+ next - start);
+ folio_unlock(folio);
+ folio_put(folio);
+ }
+ return 0;
+}
+
static void ntfs_write_delalloc_release(struct inode *inode, loff_t pos,
loff_t length, ssize_t written,
unsigned int flags, struct iomap *iomap)
{
+ struct ntfs_volume *vol = NTFS_I(inode)->vol;
+
filemap_invalidate_lock(inode->i_mapping);
+ /*
+ * A copy that stopped inside a cluster which was a hole leaves the
+ * rest of that cluster neither written nor zeroed, but writeback
+ * allocates the whole cluster and writes only the dirty blocks, so
+ * the old contents of the cluster would show through.
+ */
+ if (written && iomap->flags & NTFS_IOMAP_F_WAS_HOLE) {
+ loff_t start = pos + written;
+ loff_t end = min_t(loff_t, round_up(start, vol->cluster_size),
+ i_size_read(inode));
+ int err = 0;
+
+ if (end > start)
+ err = ntfs_dirty_cluster_tail(inode, start, end);
+ if (err) {
+ ntfs_error(vol->sb,
+ "Failed to zero [%lld, %lld) of inode 0x%llx after a short write: %d",
+ start, end, NTFS_I(inode)->mft_no, err);
+ NVolSetErrors(vol);
+ /* the write returns the bytes copied; let fsync() see it */
+ mapping_set_error(inode->i_mapping, err);
+ }
+ }
__ntfs_write_delalloc_release(inode, pos, length, written, flags,
iomap);
filemap_invalidate_unlock(inode->i_mapping);
diff --git a/fs/ntfs/iomap.h b/fs/ntfs/iomap.h
index fec8b907c4..25f50744fd 100644
--- a/fs/ntfs/iomap.h
+++ b/fs/ntfs/iomap.h
@@ -20,4 +20,5 @@ DECLARE_IOMAP_ITER_NEXT(ntfs_dio_iomap_next);
extern const struct iomap_writeback_ops ntfs_writeback_ops;
extern const struct iomap_write_ops ntfs_iomap_folio_ops;
extern int ntfs_dio_zero_range(struct inode *inode, loff_t offset, loff_t length);
+int ntfs_zero_range_unkillable(struct inode *inode, loff_t pos, loff_t len);
#endif /* _LINUX_NTFS_IOMAP_H */
--
2.56.1
^ permalink raw reply [flat|nested] 15+ messages in thread* [PATCH v5 11/13] ntfs: revalidate a buffered write's mapping after its clusters were allocated
2026-12-04 18:40 [PATCH v5 00/13] ntfs: delayed allocation accounting and stale data fixes, part 1 Ernest Borowski via B4 Relay
` (9 preceding siblings ...)
2026-12-04 18:40 ` [PATCH v5 10/13] ntfs: zero the rest of a new cluster after a short buffered write Ernest Borowski via B4 Relay
@ 2026-12-04 18:40 ` Ernest Borowski via B4 Relay
2026-12-04 18:40 ` [PATCH v5 12/13] ntfs: zero a write fault's new cluster before the runlist shows it Ernest Borowski via B4 Relay
2026-12-04 18:40 ` [PATCH v5 13/13] ntfs: allocate delalloc clusters at writeback only for dirty folios Ernest Borowski via B4 Relay
12 siblings, 0 replies; 15+ messages in thread
From: Ernest Borowski via B4 Relay @ 2026-12-04 18:40 UTC (permalink / raw)
To: Namjae Jeon, Hyunchul Lee, Baolin Liu
Cc: Christian Brauner, Christoph Hellwig, Darrick J. Wong, ntfs,
linux-fsdevel, linux-xfs, linux-kernel, Zhan Xusheng, stable,
Baolin Liu, Ernest Borowski
From: Ernest Borowski <ernest.borowski.git@proton.me>
A buffered write turns a hole into an LCN_DELALLOC run in
->iomap_begin() and copies into the page cache afterwards, with only
the inode lock held. A write fault or writeback does not take that
lock and can allocate the clusters in the meantime. If the folio the
write has not reached yet is then written back and reclaimed,
__iomap_write_begin() still sees IOMAP_DELALLOC and zeroes the parts of
the block that the write does not cover instead of reading them, and
data that a write through a shared mapping put there is lost.
Reproducer: block the copy of a write into a hole on a userfaultfd page
before its second page, store a byte through a shared mapping next to
where the write ends, sync_file_range() the file and drop the page with
POSIX_FADV_DONTNEED, then let the copy go on. The byte reads back as
zero.
Give the delalloc mapping of a buffered write a validity cookie.
ntfs_attr_map_cluster() bumps a per-inode count whenever it allocates
clusters, and ->iomap_valid() calls the mapping stale when the count
moved and its clusters are no longer all delalloc. ->iomap_end() then
releases what was not copied, as for a short write, and the rest of the
write is mapped again.
Fixes: b041ca562526 ("ntfs: update iomap and address space operations")
Cc: stable@vger.kernel.org
Reviewed-by: Baolin Liu <liubaolin@kylinos.cn>
Signed-off-by: Ernest Borowski <ernest.borowski.git@proton.me>
---
fs/ntfs/attrib.c | 2 ++
fs/ntfs/inode.c | 1 +
fs/ntfs/inode.h | 3 +++
fs/ntfs/iomap.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 54 insertions(+)
diff --git a/fs/ntfs/attrib.c b/fs/ntfs/attrib.c
index 3de589ca0b..a980198046 100644
--- a/fs/ntfs/attrib.c
+++ b/fs/ntfs/attrib.c
@@ -5680,6 +5680,8 @@ int ntfs_attr_map_cluster(struct ntfs_inode *ni, s64 vcn_start, s64 *lcn_start,
}
ni->runlist.rl = rl;
ni->runlist.count = new_rl_count;
+ /* Delalloc mappings of buffered writes in flight go stale. */
+ WRITE_ONCE(ni->alloc_seq, ni->alloc_seq + 1);
if (!update_mp) {
u64 free = atomic64_read(&vol->free_clusters) * 100;
diff --git a/fs/ntfs/inode.c b/fs/ntfs/inode.c
index 651427c31f..ba00c09e1b 100644
--- a/fs/ntfs/inode.c
+++ b/fs/ntfs/inode.c
@@ -557,6 +557,7 @@ void __ntfs_init_inode(struct super_block *sb, struct ntfs_inode *ni)
ni->target = NULL;
ni->linuxflags = 0;
ni->i_dealloc_clusters = 0;
+ ni->alloc_seq = 0;
}
/*
diff --git a/fs/ntfs/inode.h b/fs/ntfs/inode.h
index 8c1929bebe..9ccaf55682 100644
--- a/fs/ntfs/inode.h
+++ b/fs/ntfs/inode.h
@@ -94,6 +94,8 @@ enum ntfs_inode_mutex_lock_class {
* record. For fake inodes, the real (base) inode to which the attribute
* belongs.
* @i_dealloc_clusters: delayed allocated clusters.
+ * @alloc_seq: bumped under the runlist lock whenever clusters are allocated
+ * for a hole or delalloc run; a buffered write's ->iomap_valid() uses it.
* @target: symlink buffer.
* @linuxflags: Linux-specific file attributes loaded from the $LINUXFLAGS EA.
*/
@@ -144,6 +146,7 @@ struct ntfs_inode {
struct ntfs_inode *base_ntfs_ino;
} ext;
unsigned int i_dealloc_clusters;
+ unsigned int alloc_seq;
__le32 reparse_tag;
__le32 reparse_flags;
char *target;
diff --git a/fs/ntfs/iomap.c b/fs/ntfs/iomap.c
index 820a89c640..82ef13e7bb 100644
--- a/fs/ntfs/iomap.c
+++ b/fs/ntfs/iomap.c
@@ -79,8 +79,55 @@ static void ntfs_iomap_put_folio(struct inode *inode, loff_t pos,
folio_put(folio);
}
+/* Never 0: a mapping with no cookie is not checked. */
+static u64 ntfs_alloc_cookie(struct ntfs_inode *ni)
+{
+ return (u64)READ_ONCE(ni->alloc_seq) + 1;
+}
+
+/*
+ * A buffered write maps a hole or delalloc run as delalloc and copies into
+ * the page cache afterwards, without the locks a write fault or writeback
+ * takes to allocate the clusters. If one of them allocates them meanwhile
+ * and the folio is written back and reclaimed, __iomap_write_begin() would
+ * zero the parts of a block that the write does not cover instead of
+ * reading what a write through a mapping put there. Every allocation
+ * bumps ni->alloc_seq; when it moved since the mapping was made, the
+ * mapping is still good only if its clusters are all still delalloc.
+ * A stale mapping ends like a short write (->iomap_end() releases what was
+ * not copied) and the rest of the write is mapped again.
+ */
+static bool ntfs_iomap_valid(struct inode *inode, const struct iomap *iomap)
+{
+ struct ntfs_inode *ni = NTFS_I(inode);
+ struct ntfs_volume *vol = ni->vol;
+ struct runlist_element *rl;
+ s64 vcn, end;
+ bool valid = true;
+
+ if (!iomap->validity_cookie ||
+ iomap->validity_cookie == ntfs_alloc_cookie(ni))
+ return true;
+
+ vcn = ntfs_bytes_to_cluster(vol, iomap->offset);
+ end = ntfs_bytes_to_cluster(vol, round_up(iomap->offset + iomap->length,
+ vol->cluster_size));
+ down_read(&ni->runlist.lock);
+ rl = __ntfs_attr_find_vcn_nolock(&ni->runlist, vcn);
+ if (IS_ERR(rl))
+ valid = false;
+ for (; valid && vcn < end; rl++) {
+ if (!rl->length || rl->lcn != LCN_DELALLOC)
+ valid = false;
+ vcn = rl->vcn + rl->length;
+ }
+ up_read(&ni->runlist.lock);
+ return valid;
+}
+
const struct iomap_write_ops ntfs_iomap_folio_ops = {
.put_folio = ntfs_iomap_put_folio,
+ .iomap_valid = ntfs_iomap_valid,
};
static int ntfs_read_iomap_begin_resident(struct inode *inode, loff_t offset, loff_t length,
@@ -473,6 +520,7 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_
ni->runlist.count = new_rl_count;
ni->i_dealloc_clusters += max_clu_count;
}
+ iomap->validity_cookie = ntfs_alloc_cookie(ni);
up_write(&ni->runlist.lock);
mutex_unlock(&mrec_ni->mrec_lock);
--
2.56.1
^ permalink raw reply [flat|nested] 15+ messages in thread* [PATCH v5 12/13] ntfs: zero a write fault's new cluster before the runlist shows it
2026-12-04 18:40 [PATCH v5 00/13] ntfs: delayed allocation accounting and stale data fixes, part 1 Ernest Borowski via B4 Relay
` (10 preceding siblings ...)
2026-12-04 18:40 ` [PATCH v5 11/13] ntfs: revalidate a buffered write's mapping after its clusters were allocated Ernest Borowski via B4 Relay
@ 2026-12-04 18:40 ` Ernest Borowski via B4 Relay
2026-12-04 18:40 ` [PATCH v5 13/13] ntfs: allocate delalloc clusters at writeback only for dirty folios Ernest Borowski via B4 Relay
12 siblings, 0 replies; 15+ messages in thread
From: Ernest Borowski via B4 Relay @ 2026-12-04 18:40 UTC (permalink / raw)
To: Namjae Jeon, Hyunchul Lee, Baolin Liu
Cc: Christian Brauner, Christoph Hellwig, Darrick J. Wong, ntfs,
linux-fsdevel, linux-xfs, linux-kernel, Zhan Xusheng, stable,
Ernest Borowski
From: Ernest Borowski <ernest.borowski.git@proton.me>
A write fault into a hole of a file mapped with mmap() allocates
clusters in ntfs_write_da_iomap_begin_non_resident(). It calls
ntfs_attr_map_cluster(), which puts the new cluster numbers into the
runlist and drops the runlist lock and mrec_lock. Only after that does
ntfs_write_da_iomap_begin_non_resident() zero the clusters on disk with
ntfs_dio_zero_range(). The faulting folio is in the page cache, but
when a cluster is larger than the folio, the rest of the cluster is not.
Until the zeroing is done, a read of that rest through read(), another
mapping or splice() finds the new cluster in the runlist and reads its
old contents, such as data of a deleted file. The page cache keeps that
stale copy after the zeroing, so the old data stays visible until those
folios are dropped.
Reproducer: on a volume with 64 KiB clusters, fill the free space with a
file of a marker pattern and delete it. Create a sparse file and store
single bytes into its holes through a shared mapping. Meanwhile, other
processes drop the range being faulted in from the page cache with
fadvise(DONTNEED) and read it through new mappings and splice(). The
marker shows up within seconds.
When the faulting range does not cover whole clusters, zero the new
clusters in ntfs_attr_map_cluster() before they are merged into the
runlist. Only direct I/O still zeroes after the mapping.
Fixes: b041ca562526 ("ntfs: update iomap and address space operations")
Cc: stable@vger.kernel.org
Signed-off-by: Ernest Borowski <ernest.borowski.git@proton.me>
---
fs/ntfs/attrib.c | 28 +++++++++++++++++++++++++---
fs/ntfs/attrib.h | 3 ++-
fs/ntfs/iomap.c | 14 ++++++++++----
3 files changed, 37 insertions(+), 8 deletions(-)
diff --git a/fs/ntfs/attrib.c b/fs/ntfs/attrib.c
index a980198046..6da614598d 100644
--- a/fs/ntfs/attrib.c
+++ b/fs/ntfs/attrib.c
@@ -5545,7 +5545,7 @@ static void ntfs_map_cluster_rehold(struct ntfs_volume *vol, bool was_delalloc,
int ntfs_attr_map_cluster(struct ntfs_inode *ni, s64 vcn_start, s64 *lcn_start,
s64 *lcn_count, s64 max_clu_count, bool *balloc, bool update_mp,
- bool skip_holes)
+ bool skip_holes, bool zero_new)
{
struct ntfs_inode *base_ni = ntfs_base_inode(ni);
struct ntfs_volume *vol = ni->vol;
@@ -5653,6 +5653,26 @@ int ntfs_attr_map_cluster(struct ntfs_inode *ni, s64 vcn_start, s64 *lcn_start,
WARN_ON(rlc->vcn != vcn);
lcn = rlc->lcn;
clu_count = rlc->length;
+ /*
+ * Zero the clusters before the runlist shows them: from then on
+ * readers map them, and they must not see what the clusters held
+ * before.
+ */
+ if (zero_new) {
+ err = ntfs_dio_zero_range(VFS_I(ni),
+ ntfs_cluster_to_bytes(vol, lcn),
+ ntfs_cluster_to_bytes(vol, clu_count));
+ if (err) {
+ ntfs_map_cluster_rehold(vol, was_delalloc, clu_count);
+ if (ntfs_cluster_free_from_rl(vol, rlc)) {
+ ntfs_error(vol->sb,
+ "Failed to free clusters that could not be zeroed.");
+ NVolSetErrors(vol);
+ }
+ kvfree(rlc);
+ goto out;
+ }
+ }
old_rl_count = ni->runlist.count;
old_rl = kmemdup(ni->runlist.rl,
old_rl_count * sizeof(*old_rl), GFP_NOFS);
@@ -6301,7 +6321,8 @@ int ntfs_attr_fallocate(struct ntfs_inode *ni, loff_t start, loff_t byte_len, bo
NTFS_INODE_MUTEX_NORMAL);
down_write(&ni->runlist.lock);
err = ntfs_attr_map_cluster(ni, vcn, &lcn, &alloc_cnt,
- try_alloc_cnt, &balloc, false, false);
+ try_alloc_cnt, &balloc, false, false,
+ false);
up_write(&ni->runlist.lock);
mutex_unlock(&base_ni->mrec_lock);
if (err)
@@ -6334,7 +6355,8 @@ int ntfs_attr_fallocate(struct ntfs_inode *ni, loff_t start, loff_t byte_len, bo
mutex_lock_nested(&base_ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL);
down_write(&ni->runlist.lock);
err = ntfs_attr_map_cluster(ni, vcn, &lcn, &alloc_cnt,
- try_alloc_cnt, &balloc, false, false);
+ try_alloc_cnt, &balloc, false, false,
+ false);
up_write(&ni->runlist.lock);
mutex_unlock(&base_ni->mrec_lock);
if (err || fatal_signal_pending(current))
diff --git a/fs/ntfs/attrib.h b/fs/ntfs/attrib.h
index dd073f9fd7..ca3481cb78 100644
--- a/fs/ntfs/attrib.h
+++ b/fs/ntfs/attrib.h
@@ -94,7 +94,8 @@ int ntfs_attr_size_bounds_check(const struct ntfs_volume *vol,
int ntfs_attr_can_be_resident(const struct ntfs_volume *vol,
const __le32 type);
int ntfs_attr_map_cluster(struct ntfs_inode *ni, s64 vcn_start, s64 *lcn_start,
- s64 *lcn_count, s64 max_clu_count, bool *balloc, bool update_mp, bool skip_holes);
+ s64 *lcn_count, s64 max_clu_count, bool *balloc, bool update_mp,
+ bool skip_holes, bool zero_new);
int ntfs_attr_record_resize(struct mft_record *m, struct attr_record *a, u32 new_size);
int ntfs_resident_attr_value_resize(struct mft_record *m, struct attr_record *a,
const u32 new_size);
diff --git a/fs/ntfs/iomap.c b/fs/ntfs/iomap.c
index 82ef13e7bb..270bd41fa5 100644
--- a/fs/ntfs/iomap.c
+++ b/fs/ntfs/iomap.c
@@ -611,7 +611,7 @@ static int ntfs_write_da_iomap_begin_non_resident(struct inode *inode,
struct ntfs_volume *vol = ni->vol;
loff_t vcn_ofs, rl_length;
s64 vcn, start_lcn, lcn_count;
- bool balloc = false, update_mp;
+ bool balloc = false, update_mp, zero_new;
int err;
s64 max_clu_count =
ntfs_bytes_to_cluster(vol, round_up(length, vol->cluster_size));
@@ -621,10 +621,17 @@ static int ntfs_write_da_iomap_begin_non_resident(struct inode *inode,
update_mp = ntfs_iomap_flags & (NTFS_IOMAP_FLAGS_DIO | NTFS_IOMAP_FLAGS_MKWRITE) ||
NInoAttr(ni) || ni->mft_no < FILE_first_user;
+ /*
+ * A write fault into a hole dirties only the faulting folio: the rest
+ * of a cluster that the folio does not cover has to be zeroed on disk
+ * before other readers can map the cluster.
+ */
+ zero_new = ntfs_iomap_flags & NTFS_IOMAP_FLAGS_MKWRITE &&
+ (offset | length) & vol->cluster_size_mask;
down_write(&ni->runlist.lock);
err = ntfs_attr_map_cluster(ni, vcn, &start_lcn, &lcn_count,
max_clu_count, &balloc, update_mp,
- ntfs_iomap_flags & NTFS_IOMAP_FLAGS_WRITEBACK);
+ ntfs_iomap_flags & NTFS_IOMAP_FLAGS_WRITEBACK, zero_new);
up_write(&ni->runlist.lock);
mutex_unlock(&mrec_ni->mrec_lock);
if (err) {
@@ -651,8 +658,7 @@ static int ntfs_write_da_iomap_begin_non_resident(struct inode *inode,
iomap->addr = ntfs_cluster_to_bytes(vol, start_lcn) + vcn_ofs;
if (balloc == true) {
- if (flags & IOMAP_DIRECT ||
- ntfs_iomap_flags & NTFS_IOMAP_FLAGS_MKWRITE) {
+ if (flags & IOMAP_DIRECT) {
loff_t end = offset + length;
if (vcn_ofs || ((vol->cluster_size > iomap->length) &&
--
2.56.1
^ permalink raw reply [flat|nested] 15+ messages in thread* [PATCH v5 13/13] ntfs: allocate delalloc clusters at writeback only for dirty folios
2026-12-04 18:40 [PATCH v5 00/13] ntfs: delayed allocation accounting and stale data fixes, part 1 Ernest Borowski via B4 Relay
` (11 preceding siblings ...)
2026-12-04 18:40 ` [PATCH v5 12/13] ntfs: zero a write fault's new cluster before the runlist shows it Ernest Borowski via B4 Relay
@ 2026-12-04 18:40 ` Ernest Borowski via B4 Relay
2026-12-05 13:42 ` liubaolin
12 siblings, 1 reply; 15+ messages in thread
From: Ernest Borowski via B4 Relay @ 2026-12-04 18:40 UTC (permalink / raw)
To: Namjae Jeon, Hyunchul Lee, Baolin Liu
Cc: Christian Brauner, Christoph Hellwig, Darrick J. Wong, ntfs,
linux-fsdevel, linux-xfs, linux-kernel, Zhan Xusheng, stable,
Ernest Borowski
From: Ernest Borowski <ernest.borowski.git@proton.me>
A buffered write into a hole maps its whole range as one delalloc run in
->iomap_begin() and then copies the data folio by folio. Writeback that
runs meanwhile (the flusher, sync_file_range(), fadvise(DONTNEED),
FIEMAP_FLAG_SYNC) asks ntfs_write_da_iomap_begin_non_resident() for a
mapping from the first dirty folio to the end of the allocation, so
ntfs_attr_map_cluster() allocates the whole run, also the clusters whose
data has not been copied yet. Readers that do not take the inode lock
(page faults, splice()) then map those clusters, find no folio in the
page cache, and read what the reused clusters held before, data of
deleted files. The stale folios stay in the page cache.
Reproducer: fill the free space with a marker pattern and delete it,
create a sparse file, and fill its holes with buffered writes of 1 to 8
clusters while other processes call fadvise(DONTNEED) on the range being
written and read it through new mappings and splice(). With 4 KiB
clusters the marker shows up within seconds, with 64 KiB clusters too.
Ask for a mapping only up to the end of the dirty folios that follow the
one being written back. Clusters larger than a page are allocated
whole, so with those also stop before a cluster that is only partly
dirty, and when the folio's own cluster has folios below
initialized_size that are not dirty, zero it on disk before it is
mapped, as for a write fault. In the usual case every folio of a
delalloc cluster is dirty, since the buffered write zeroes the parts of
its edge clusters it does not cover, so that zeroing only happens while
a copy into the cluster is still going on. The scan for dirty folios
runs for every new mapping, so it stops at the end of the run being
mapped and after 16 MiB: scanning to the end of the allocation made
fsync of a fully dirty 1 GiB file of 4000 extents take 98 to 172 s
instead of 0.36 to 0.63 s; with the bound it takes 0.29 to 0.57 s (QEMU,
RAM-backed loop device, 3 runs each).
With clusters smaller than a page, a dirty folio can hold clean blocks
that the write in progress has mapped as delalloc but not copied into
yet: an earlier write dirtied part of the folio, and this write maps the
rest. The first blocks of the dirty folios that follow can be such
blocks too. The inode lock allows only one buffered write at a time, so
the write records its uncopied range in ntfs_inode; size_lock protects
the two fields. ->iomap_begin() sets the range while it holds the
runlist lock, before writeback can see the new delalloc clusters.
->put_folio() moves the start of the range past each copied folio, and
->iomap_end() clears the range after the clusters the write did not
reach are punched out. Writeback reads the range while it holds the
runlist lock, and stops the mapping where the range starts; the mapping
still covers the dirty blocks that iomap passes in. Mapping only the
dirty blocks of each folio would also close the race, but then fsync of
a new 256 MiB file with 512-byte clusters took 1.31 to 1.56 s, compared
to 0.20 to 0.30 s with 4 KiB clusters in the same run. With the
recorded range it takes 0.31 to 0.50 s, compared to 0.29 to 0.36 s with
4 KiB clusters (QEMU, RAM-backed loop device, 5 runs each).
Fixes: b041ca562526 ("ntfs: update iomap and address space operations")
Cc: stable@vger.kernel.org
Signed-off-by: Ernest Borowski <ernest.borowski.git@proton.me>
---
fs/ntfs/inode.c | 2 +
fs/ntfs/inode.h | 6 ++
fs/ntfs/iomap.c | 176 ++++++++++++++++++++++++++++++++++++++++++++++++++++----
3 files changed, 173 insertions(+), 11 deletions(-)
diff --git a/fs/ntfs/inode.c b/fs/ntfs/inode.c
index ba00c09e1b..121f6fa718 100644
--- a/fs/ntfs/inode.c
+++ b/fs/ntfs/inode.c
@@ -558,6 +558,8 @@ void __ntfs_init_inode(struct super_block *sb, struct ntfs_inode *ni)
ni->linuxflags = 0;
ni->i_dealloc_clusters = 0;
ni->alloc_seq = 0;
+ ni->uncopied_start = 0;
+ ni->uncopied_end = 0;
}
/*
diff --git a/fs/ntfs/inode.h b/fs/ntfs/inode.h
index 9ccaf55682..79e7301de5 100644
--- a/fs/ntfs/inode.h
+++ b/fs/ntfs/inode.h
@@ -96,6 +96,10 @@ enum ntfs_inode_mutex_lock_class {
* @i_dealloc_clusters: delayed allocated clusters.
* @alloc_seq: bumped under the runlist lock whenever clusters are allocated
* for a hole or delalloc run; a buffered write's ->iomap_valid() uses it.
+ * @uncopied_start: start of the part of the delalloc mapping of the buffered
+ * write in progress that the write has not copied into the page cache yet;
+ * writeback does not allocate it. Guarded by @size_lock.
+ * @uncopied_end: end of that part; none when not above @uncopied_start.
* @target: symlink buffer.
* @linuxflags: Linux-specific file attributes loaded from the $LINUXFLAGS EA.
*/
@@ -147,6 +151,8 @@ struct ntfs_inode {
} ext;
unsigned int i_dealloc_clusters;
unsigned int alloc_seq;
+ loff_t uncopied_start;
+ loff_t uncopied_end;
__le32 reparse_tag;
__le32 reparse_flags;
char *target;
diff --git a/fs/ntfs/iomap.c b/fs/ntfs/iomap.c
index 270bd41fa5..4fb82927b4 100644
--- a/fs/ntfs/iomap.c
+++ b/fs/ntfs/iomap.c
@@ -5,6 +5,8 @@
* Copyright (c) 2025 LG Electronics Co., Ltd.
*/
+#include <linux/sizes.h>
+#include <linux/workqueue.h>
#include <linux/writeback.h>
#include "attrib.h"
@@ -15,12 +17,29 @@
/* iomap->flags: ->iomap_begin() turned a hole into this delalloc mapping */
#define NTFS_IOMAP_F_WAS_HOLE IOMAP_F_PRIVATE
+/*
+ * A buffered write (the inode lock allows one at a time) maps its range as
+ * delalloc before it copies the data folio by folio. Record the part of the
+ * mapping it has not copied yet: writeback must not allocate it, see
+ * ntfs_writeback_run_end().
+ */
+static void ntfs_set_uncopied(struct ntfs_inode *ni, loff_t start, loff_t end)
+{
+ unsigned long flags;
+
+ write_lock_irqsave(&ni->size_lock, flags);
+ ni->uncopied_start = start;
+ ni->uncopied_end = end;
+ write_unlock_irqrestore(&ni->size_lock, flags);
+}
+
static void ntfs_iomap_put_folio_non_resident(struct inode *inode, loff_t pos,
unsigned int len, struct folio *folio)
{
struct ntfs_inode *ni = NTFS_I(inode);
unsigned long sector_size = 1UL << inode->i_blkbits;
loff_t start_down, end_up, init;
+ unsigned long flags;
start_down = round_down(pos, sector_size);
end_up = (pos + len - 1) | (sector_size - 1);
@@ -61,6 +80,14 @@ static void ntfs_iomap_put_folio_non_resident(struct inode *inode, loff_t pos,
offset2,
folio_size(folio));
}
+ /* [pos, pos + len) is copied, or zeroed, and dirty */
+ if (len) {
+ write_lock_irqsave(&ni->size_lock, flags);
+ if (ni->uncopied_start < ni->uncopied_end &&
+ pos <= ni->uncopied_start && ni->uncopied_start < pos + len)
+ ni->uncopied_start = pos + len;
+ write_unlock_irqrestore(&ni->size_lock, flags);
+ }
folio_unlock(folio);
folio_put(folio);
}
@@ -521,17 +548,19 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_
ni->i_dealloc_clusters += max_clu_count;
}
iomap->validity_cookie = ntfs_alloc_cookie(ni);
- up_write(&ni->runlist.lock);
- mutex_unlock(&mrec_ni->mrec_lock);
-
- if (lcn < LCN_DELALLOC)
- ntfs_hold_dirty_clusters(vol, max_clu_count);
rl_length = ntfs_cluster_to_bytes(vol, max_clu_count);
if (length > rl_length - vcn_ofs)
iomap->length = rl_length - vcn_ofs;
else
iomap->length = length;
+ /* before writeback can see the new delalloc clusters */
+ ntfs_set_uncopied(ni, offset, offset + iomap->length);
+ up_write(&ni->runlist.lock);
+ mutex_unlock(&mrec_ni->mrec_lock);
+
+ if (lcn < LCN_DELALLOC)
+ ntfs_hold_dirty_clusters(vol, max_clu_count);
iomap->flags = IOMAP_F_NEW;
if (lcn <= LCN_HOLE) {
@@ -576,6 +605,7 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_
iomap->length, 0,
IOMAP_WRITE,
iomap);
+ ntfs_set_uncopied(ni, 0, 0);
return err;
}
}
@@ -600,6 +630,8 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_
#define NTFS_IOMAP_FLAGS_DIO BIT(2)
#define NTFS_IOMAP_FLAGS_MKWRITE BIT(3)
#define NTFS_IOMAP_FLAGS_WRITEBACK BIT(4)
+/* Zero the clusters ->iomap_begin() allocates before mapping them. */
+#define NTFS_IOMAP_FLAGS_ZERO_NEW BIT(5)
static int ntfs_write_da_iomap_begin_non_resident(struct inode *inode,
loff_t offset, loff_t length, unsigned int flags,
@@ -626,8 +658,9 @@ static int ntfs_write_da_iomap_begin_non_resident(struct inode *inode,
* of a cluster that the folio does not cover has to be zeroed on disk
* before other readers can map the cluster.
*/
- zero_new = ntfs_iomap_flags & NTFS_IOMAP_FLAGS_MKWRITE &&
- (offset | length) & vol->cluster_size_mask;
+ zero_new = (ntfs_iomap_flags & NTFS_IOMAP_FLAGS_MKWRITE &&
+ (offset | length) & vol->cluster_size_mask) ||
+ ntfs_iomap_flags & NTFS_IOMAP_FLAGS_ZERO_NEW;
down_write(&ni->runlist.lock);
err = ntfs_attr_map_cluster(ni, vcn, &start_lcn, &lcn_count,
max_clu_count, &balloc, update_mp,
@@ -970,6 +1003,9 @@ static int ntfs_write_iomap_end(struct inode *inode, loff_t pos, loff_t length,
!(flags & IOMAP_FAULT) && written < length)
ntfs_write_delalloc_release(inode, pos, length, written, flags,
iomap);
+ /* only a buffered write maps delalloc; what it did not copy is gone */
+ if (iomap->type == IOMAP_DELALLOC)
+ ntfs_set_uncopied(NTFS_I(inode), 0, 0);
return written;
}
@@ -998,17 +1034,135 @@ static int ntfs_dio_iomap_begin(struct inode *inode, loff_t offset,
DEFINE_IOMAP_ITER_NEXT_END(ntfs_dio_iomap_next, ntfs_dio_iomap_begin,
ntfs_write_iomap_end);
+/*
+ * Longest mapping writeback asks for at a time. With the end of the run
+ * (ntfs_writeback_run_end()) it bounds the scan of ntfs_dirty_range_end(),
+ * which runs for every new mapping: unbounded, a fully dirty file of many
+ * extents is scanned once per extent. It is the largest cluster size
+ * (4096 sectors of 4 KiB), so a mapping can always cover a whole cluster.
+ */
+#define NTFS_WRITEBACK_MAP_MAX SZ_16M
+
+/*
+ * Return where the mapping for the dirty blocks [@offset, @offset + @len) of
+ * @folio can end, at most @end: at the end of the run holding @offset, so the
+ * scan for dirty folios need not go further either. With clusters smaller
+ * than a folio, the clean blocks of a dirty folio, and the leading ones of
+ * the dirty folios that follow, can be delalloc clusters that the buffered
+ * write in progress has not copied into yet: stop where they start, but
+ * always cover the blocks iomap passed in. Larger clusters are zeroed or
+ * copied whole before writeback can see them dirty.
+ */
+static loff_t ntfs_writeback_run_end(struct inode *inode, struct folio *folio,
+ loff_t offset, unsigned int len,
+ loff_t end)
+{
+ struct ntfs_inode *ni = NTFS_I(inode);
+ struct ntfs_volume *vol = ni->vol;
+ struct runlist_element *rl;
+ unsigned long flags;
+
+ down_read(&ni->runlist.lock);
+ rl = __ntfs_attr_find_vcn_nolock(&ni->runlist,
+ ntfs_bytes_to_cluster(vol, offset));
+ if (!IS_ERR(rl) && rl->length) {
+ end = min_t(loff_t, end,
+ ntfs_cluster_to_bytes(vol, rl->vcn + rl->length));
+ /*
+ * Read under the runlist lock: the write records the range
+ * before it makes the clusters delalloc, and forgets it only
+ * after it punched out what it did not copy.
+ */
+ if (rl->lcn == LCN_DELALLOC &&
+ vol->cluster_size < folio_size(folio)) {
+ read_lock_irqsave(&ni->size_lock, flags);
+ if (ni->uncopied_start < ni->uncopied_end &&
+ offset < ni->uncopied_end)
+ end = min(end, max(round_down(ni->uncopied_start,
+ vol->cluster_size),
+ offset + len));
+ read_unlock_irqrestore(&ni->size_lock, flags);
+ }
+ }
+ up_read(&ni->runlist.lock);
+ return end;
+}
+
+/*
+ * Return where the run of dirty folios that follows @folio ends, at most
+ * @end. A buffered write maps its whole range as delalloc before it copies
+ * the data folio by folio, so writeback that runs meanwhile must not allocate
+ * the clusters past the dirty folios: readers that do not take the inode
+ * lock (page faults, splice) would map the new clusters and read what they
+ * held before.
+ */
+static loff_t ntfs_dirty_range_end(struct address_space *mapping,
+ struct folio *folio, loff_t end)
+{
+ pgoff_t index = folio_next_index(folio);
+ pgoff_t last = DIV_ROUND_UP(end, PAGE_SIZE);
+
+ while (index < last &&
+ xa_get_mark(&mapping->i_pages, index, PAGECACHE_TAG_DIRTY))
+ index++;
+ return min_t(loff_t, (loff_t)index << PAGE_SHIFT, end);
+}
+
+/*
+ * With clusters larger than a page, a cluster can be only partly dirty while
+ * a buffered write is still copying into it. Return where the mapping for
+ * the folio at @offset should end so that writeback does not allocate a
+ * partly dirty cluster other than the folio's own, and set @zero when the
+ * folio's own cluster has folios below initialized_size that are not dirty:
+ * it then has to be zeroed on disk before it is mapped.
+ */
+static loff_t ntfs_writeback_cluster_end(struct inode *inode,
+ struct folio *folio, loff_t offset,
+ loff_t end, bool *zero)
+{
+ struct ntfs_inode *ni = NTFS_I(inode);
+ u32 cs = ni->vol->cluster_size;
+ loff_t cstart = round_down(offset, cs);
+ loff_t cend = cstart + cs;
+ loff_t init = round_up(ni->initialized_size, PAGE_SIZE);
+ pgoff_t index;
+
+ *zero = false;
+ if (cs <= PAGE_SIZE)
+ return end;
+ if (end < min(cend, init))
+ *zero = true;
+ for (index = cstart >> PAGE_SHIFT; !*zero && index < folio->index &&
+ ((loff_t)index << PAGE_SHIFT) < init; index++)
+ if (!xa_get_mark(&inode->i_mapping->i_pages, index,
+ PAGECACHE_TAG_DIRTY))
+ *zero = true;
+ if (*zero || end < cend)
+ return min(end, cend);
+ return round_down(end, cs);
+}
+
static ssize_t ntfs_writeback_range(struct iomap_writepage_ctx *wpc,
struct folio *folio, u64 offset, unsigned int len, u64 end_pos)
{
if (offset < wpc->iomap.offset ||
offset >= wpc->iomap.offset + wpc->iomap.length) {
+ struct inode *inode = wpc->inode;
+ loff_t end = min_t(loff_t, NTFS_I(inode)->allocated_size,
+ offset + NTFS_WRITEBACK_MAP_MAX);
+ int ntfs_iomap_flags = NTFS_IOMAP_FLAGS_WRITEBACK;
+ bool zero;
int error;
- error = __ntfs_write_iomap_begin(wpc->inode, offset,
- NTFS_I(wpc->inode)->allocated_size - offset,
- IOMAP_WRITE, &wpc->iomap,
- NTFS_IOMAP_FLAGS_WRITEBACK);
+ end = ntfs_writeback_run_end(inode, folio, offset, len, end);
+ end = ntfs_dirty_range_end(inode->i_mapping, folio, end);
+ end = ntfs_writeback_cluster_end(inode, folio, offset, end,
+ &zero);
+ if (zero)
+ ntfs_iomap_flags |= NTFS_IOMAP_FLAGS_ZERO_NEW;
+ error = __ntfs_write_iomap_begin(inode, offset, end - offset,
+ IOMAP_WRITE, &wpc->iomap,
+ ntfs_iomap_flags);
if (error)
return error;
}
--
2.56.1
^ permalink raw reply [flat|nested] 15+ messages in thread* Re: [PATCH v5 13/13] ntfs: allocate delalloc clusters at writeback only for dirty folios
2026-12-04 18:40 ` [PATCH v5 13/13] ntfs: allocate delalloc clusters at writeback only for dirty folios Ernest Borowski via B4 Relay
@ 2026-12-05 13:42 ` liubaolin
0 siblings, 0 replies; 15+ messages in thread
From: liubaolin @ 2026-12-05 13:42 UTC (permalink / raw)
To: ernest.borowski.git, Namjae Jeon, Hyunchul Lee
Cc: Christian Brauner, Christoph Hellwig, Darrick J. Wong, ntfs,
linux-fsdevel, linux-xfs, linux-kernel, Zhan Xusheng, stable
在 2026/12/5 02:40, Ernest Borowski via B4 Relay 写道:
> From: Ernest Borowski <ernest.borowski.git@proton.me>
>
> A buffered write into a hole maps its whole range as one delalloc run in
> ->iomap_begin() and then copies the data folio by folio. Writeback that
> runs meanwhile (the flusher, sync_file_range(), fadvise(DONTNEED),
> FIEMAP_FLAG_SYNC) asks ntfs_write_da_iomap_begin_non_resident() for a
> mapping from the first dirty folio to the end of the allocation, so
> ntfs_attr_map_cluster() allocates the whole run, also the clusters whose
> data has not been copied yet. Readers that do not take the inode lock
> (page faults, splice()) then map those clusters, find no folio in the
> page cache, and read what the reused clusters held before, data of
> deleted files. The stale folios stay in the page cache.
>
> Reproducer: fill the free space with a marker pattern and delete it,
> create a sparse file, and fill its holes with buffered writes of 1 to 8
> clusters while other processes call fadvise(DONTNEED) on the range being
> written and read it through new mappings and splice(). With 4 KiB
> clusters the marker shows up within seconds, with 64 KiB clusters too.
>
> Ask for a mapping only up to the end of the dirty folios that follow the
> one being written back. Clusters larger than a page are allocated
> whole, so with those also stop before a cluster that is only partly
> dirty, and when the folio's own cluster has folios below
> initialized_size that are not dirty, zero it on disk before it is
> mapped, as for a write fault. In the usual case every folio of a
> delalloc cluster is dirty, since the buffered write zeroes the parts of
> its edge clusters it does not cover, so that zeroing only happens while
> a copy into the cluster is still going on. The scan for dirty folios
> runs for every new mapping, so it stops at the end of the run being
> mapped and after 16 MiB: scanning to the end of the allocation made
> fsync of a fully dirty 1 GiB file of 4000 extents take 98 to 172 s
> instead of 0.36 to 0.63 s; with the bound it takes 0.29 to 0.57 s (QEMU,
> RAM-backed loop device, 3 runs each).
>
> With clusters smaller than a page, a dirty folio can hold clean blocks
> that the write in progress has mapped as delalloc but not copied into
> yet: an earlier write dirtied part of the folio, and this write maps the
> rest. The first blocks of the dirty folios that follow can be such
> blocks too. The inode lock allows only one buffered write at a time, so
> the write records its uncopied range in ntfs_inode; size_lock protects
> the two fields. ->iomap_begin() sets the range while it holds the
> runlist lock, before writeback can see the new delalloc clusters.
> ->put_folio() moves the start of the range past each copied folio, and
> ->iomap_end() clears the range after the clusters the write did not
> reach are punched out. Writeback reads the range while it holds the
> runlist lock, and stops the mapping where the range starts; the mapping
> still covers the dirty blocks that iomap passes in. Mapping only the
> dirty blocks of each folio would also close the race, but then fsync of
> a new 256 MiB file with 512-byte clusters took 1.31 to 1.56 s, compared
> to 0.20 to 0.30 s with 4 KiB clusters in the same run. With the
> recorded range it takes 0.31 to 0.50 s, compared to 0.29 to 0.36 s with
> 4 KiB clusters (QEMU, RAM-backed loop device, 5 runs each).
>
> Fixes: b041ca562526 ("ntfs: update iomap and address space operations")
> Cc: stable@vger.kernel.org
> Signed-off-by: Ernest Borowski <ernest.borowski.git@proton.me>
> ---
> fs/ntfs/inode.c | 2 +
> fs/ntfs/inode.h | 6 ++
> fs/ntfs/iomap.c | 176 ++++++++++++++++++++++++++++++++++++++++++++++++++++----
> 3 files changed, 173 insertions(+), 11 deletions(-)
>
> diff --git a/fs/ntfs/inode.c b/fs/ntfs/inode.c
> index ba00c09e1b..121f6fa718 100644
> --- a/fs/ntfs/inode.c
> +++ b/fs/ntfs/inode.c
> @@ -558,6 +558,8 @@ void __ntfs_init_inode(struct super_block *sb, struct ntfs_inode *ni)
> ni->linuxflags = 0;
> ni->i_dealloc_clusters = 0;
> ni->alloc_seq = 0;
> + ni->uncopied_start = 0;
> + ni->uncopied_end = 0;
> }
>
> /*
> diff --git a/fs/ntfs/inode.h b/fs/ntfs/inode.h
> index 9ccaf55682..79e7301de5 100644
> --- a/fs/ntfs/inode.h
> +++ b/fs/ntfs/inode.h
> @@ -96,6 +96,10 @@ enum ntfs_inode_mutex_lock_class {
> * @i_dealloc_clusters: delayed allocated clusters.
> * @alloc_seq: bumped under the runlist lock whenever clusters are allocated
> * for a hole or delalloc run; a buffered write's ->iomap_valid() uses it.
> + * @uncopied_start: start of the part of the delalloc mapping of the buffered
> + * write in progress that the write has not copied into the page cache yet;
> + * writeback does not allocate it. Guarded by @size_lock.
> + * @uncopied_end: end of that part; none when not above @uncopied_start.
> * @target: symlink buffer.
> * @linuxflags: Linux-specific file attributes loaded from the $LINUXFLAGS EA.
> */
> @@ -147,6 +151,8 @@ struct ntfs_inode {
> } ext;
> unsigned int i_dealloc_clusters;
> unsigned int alloc_seq;
> + loff_t uncopied_start;
> + loff_t uncopied_end;
> __le32 reparse_tag;
> __le32 reparse_flags;
> char *target;
> diff --git a/fs/ntfs/iomap.c b/fs/ntfs/iomap.c
> index 270bd41fa5..4fb82927b4 100644
> --- a/fs/ntfs/iomap.c
> +++ b/fs/ntfs/iomap.c
> @@ -5,6 +5,8 @@
> * Copyright (c) 2025 LG Electronics Co., Ltd.
> */
>
> +#include <linux/sizes.h>
> +#include <linux/workqueue.h>
> #include <linux/writeback.h>
>
> #include "attrib.h"
> @@ -15,12 +17,29 @@
> /* iomap->flags: ->iomap_begin() turned a hole into this delalloc mapping */
> #define NTFS_IOMAP_F_WAS_HOLE IOMAP_F_PRIVATE
>
> +/*
> + * A buffered write (the inode lock allows one at a time) maps its range as
> + * delalloc before it copies the data folio by folio. Record the part of the
> + * mapping it has not copied yet: writeback must not allocate it, see
> + * ntfs_writeback_run_end().
> + */
> +static void ntfs_set_uncopied(struct ntfs_inode *ni, loff_t start, loff_t end)
> +{
> + unsigned long flags;
> +
> + write_lock_irqsave(&ni->size_lock, flags);
> + ni->uncopied_start = start;
> + ni->uncopied_end = end;
> + write_unlock_irqrestore(&ni->size_lock, flags);
> +}
> +
> static void ntfs_iomap_put_folio_non_resident(struct inode *inode, loff_t pos,
> unsigned int len, struct folio *folio)
> {
> struct ntfs_inode *ni = NTFS_I(inode);
> unsigned long sector_size = 1UL << inode->i_blkbits;
> loff_t start_down, end_up, init;
> + unsigned long flags;
>
> start_down = round_down(pos, sector_size);
> end_up = (pos + len - 1) | (sector_size - 1);
> @@ -61,6 +80,14 @@ static void ntfs_iomap_put_folio_non_resident(struct inode *inode, loff_t pos,
> offset2,
> folio_size(folio));
> }
> + /* [pos, pos + len) is copied, or zeroed, and dirty */
> + if (len) {
> + write_lock_irqsave(&ni->size_lock, flags);
> + if (ni->uncopied_start < ni->uncopied_end &&
> + pos <= ni->uncopied_start && ni->uncopied_start < pos + len)
> + ni->uncopied_start = pos + len;
> + write_unlock_irqrestore(&ni->size_lock, flags);
> + }
> folio_unlock(folio);
> folio_put(folio);
> }
> @@ -521,17 +548,19 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_
> ni->i_dealloc_clusters += max_clu_count;
> }
> iomap->validity_cookie = ntfs_alloc_cookie(ni);
> - up_write(&ni->runlist.lock);
> - mutex_unlock(&mrec_ni->mrec_lock);
> -
> - if (lcn < LCN_DELALLOC)
> - ntfs_hold_dirty_clusters(vol, max_clu_count);
>
> rl_length = ntfs_cluster_to_bytes(vol, max_clu_count);
> if (length > rl_length - vcn_ofs)
> iomap->length = rl_length - vcn_ofs;
> else
> iomap->length = length;
> + /* before writeback can see the new delalloc clusters */
> + ntfs_set_uncopied(ni, offset, offset + iomap->length);
> + up_write(&ni->runlist.lock);
> + mutex_unlock(&mrec_ni->mrec_lock);
> +
> + if (lcn < LCN_DELALLOC)
> + ntfs_hold_dirty_clusters(vol, max_clu_count);
>
> iomap->flags = IOMAP_F_NEW;
> if (lcn <= LCN_HOLE) {
> @@ -576,6 +605,7 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_
> iomap->length, 0,
> IOMAP_WRITE,
> iomap);
> + ntfs_set_uncopied(ni, 0, 0);
> return err;
> }
> }
> @@ -600,6 +630,8 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_
> #define NTFS_IOMAP_FLAGS_DIO BIT(2)
> #define NTFS_IOMAP_FLAGS_MKWRITE BIT(3)
> #define NTFS_IOMAP_FLAGS_WRITEBACK BIT(4)
> +/* Zero the clusters ->iomap_begin() allocates before mapping them. */
> +#define NTFS_IOMAP_FLAGS_ZERO_NEW BIT(5)
>
> static int ntfs_write_da_iomap_begin_non_resident(struct inode *inode,
> loff_t offset, loff_t length, unsigned int flags,
> @@ -626,8 +658,9 @@ static int ntfs_write_da_iomap_begin_non_resident(struct inode *inode,
> * of a cluster that the folio does not cover has to be zeroed on disk
> * before other readers can map the cluster.
> */
> - zero_new = ntfs_iomap_flags & NTFS_IOMAP_FLAGS_MKWRITE &&
> - (offset | length) & vol->cluster_size_mask;
> + zero_new = (ntfs_iomap_flags & NTFS_IOMAP_FLAGS_MKWRITE &&
> + (offset | length) & vol->cluster_size_mask) ||
> + ntfs_iomap_flags & NTFS_IOMAP_FLAGS_ZERO_NEW;
> down_write(&ni->runlist.lock);
> err = ntfs_attr_map_cluster(ni, vcn, &start_lcn, &lcn_count,
> max_clu_count, &balloc, update_mp,
> @@ -970,6 +1003,9 @@ static int ntfs_write_iomap_end(struct inode *inode, loff_t pos, loff_t length,
> !(flags & IOMAP_FAULT) && written < length)
> ntfs_write_delalloc_release(inode, pos, length, written, flags,
> iomap);
> + /* only a buffered write maps delalloc; what it did not copy is gone */
> + if (iomap->type == IOMAP_DELALLOC)
> + ntfs_set_uncopied(NTFS_I(inode), 0, 0);
> return written;
> }
>
> @@ -998,17 +1034,135 @@ static int ntfs_dio_iomap_begin(struct inode *inode, loff_t offset,
> DEFINE_IOMAP_ITER_NEXT_END(ntfs_dio_iomap_next, ntfs_dio_iomap_begin,
> ntfs_write_iomap_end);
>
> +/*
> + * Longest mapping writeback asks for at a time. With the end of the run
> + * (ntfs_writeback_run_end()) it bounds the scan of ntfs_dirty_range_end(),
> + * which runs for every new mapping: unbounded, a fully dirty file of many
> + * extents is scanned once per extent. It is the largest cluster size
> + * (4096 sectors of 4 KiB), so a mapping can always cover a whole cluster.
> + */
> +#define NTFS_WRITEBACK_MAP_MAX SZ_16M
> +
> +/*
> + * Return where the mapping for the dirty blocks [@offset, @offset + @len) of
> + * @folio can end, at most @end: at the end of the run holding @offset, so the
> + * scan for dirty folios need not go further either. With clusters smaller
> + * than a folio, the clean blocks of a dirty folio, and the leading ones of
> + * the dirty folios that follow, can be delalloc clusters that the buffered
> + * write in progress has not copied into yet: stop where they start, but
> + * always cover the blocks iomap passed in. Larger clusters are zeroed or
> + * copied whole before writeback can see them dirty.
> + */
> +static loff_t ntfs_writeback_run_end(struct inode *inode, struct folio *folio,
> + loff_t offset, unsigned int len,
> + loff_t end)
> +{
> + struct ntfs_inode *ni = NTFS_I(inode);
> + struct ntfs_volume *vol = ni->vol;
> + struct runlist_element *rl;
> + unsigned long flags;
> +
> + down_read(&ni->runlist.lock);
> + rl = __ntfs_attr_find_vcn_nolock(&ni->runlist,
> + ntfs_bytes_to_cluster(vol, offset));
> + if (!IS_ERR(rl) && rl->length) {
> + end = min_t(loff_t, end,
> + ntfs_cluster_to_bytes(vol, rl->vcn + rl->length));
> + /*
> + * Read under the runlist lock: the write records the range
> + * before it makes the clusters delalloc, and forgets it only
> + * after it punched out what it did not copy.
> + */
> + if (rl->lcn == LCN_DELALLOC &&
> + vol->cluster_size < folio_size(folio)) {
> + read_lock_irqsave(&ni->size_lock, flags);
> + if (ni->uncopied_start < ni->uncopied_end &&
> + offset < ni->uncopied_end)
> + end = min(end, max(round_down(ni->uncopied_start,
> + vol->cluster_size),
> + offset + len));
> + read_unlock_irqrestore(&ni->size_lock, flags);
> + }
> + }
> + up_read(&ni->runlist.lock);
> + return end;
> +}
> +
> +/*
> + * Return where the run of dirty folios that follows @folio ends, at most
> + * @end. A buffered write maps its whole range as delalloc before it copies
> + * the data folio by folio, so writeback that runs meanwhile must not allocate
> + * the clusters past the dirty folios: readers that do not take the inode
> + * lock (page faults, splice) would map the new clusters and read what they
> + * held before.
> + */
> +static loff_t ntfs_dirty_range_end(struct address_space *mapping,
> + struct folio *folio, loff_t end)
> +{
> + pgoff_t index = folio_next_index(folio);
> + pgoff_t last = DIV_ROUND_UP(end, PAGE_SIZE);
> +
> + while (index < last &&
> + xa_get_mark(&mapping->i_pages, index, PAGECACHE_TAG_DIRTY))
> + index++;
> + return min_t(loff_t, (loff_t)index << PAGE_SHIFT, end);
> +}
> +
> +/*
> + * With clusters larger than a page, a cluster can be only partly dirty while
> + * a buffered write is still copying into it. Return where the mapping for
> + * the folio at @offset should end so that writeback does not allocate a
> + * partly dirty cluster other than the folio's own, and set @zero when the
> + * folio's own cluster has folios below initialized_size that are not dirty:
> + * it then has to be zeroed on disk before it is mapped.
> + */
> +static loff_t ntfs_writeback_cluster_end(struct inode *inode,
> + struct folio *folio, loff_t offset,
> + loff_t end, bool *zero)
> +{
> + struct ntfs_inode *ni = NTFS_I(inode);
> + u32 cs = ni->vol->cluster_size;
> + loff_t cstart = round_down(offset, cs);
> + loff_t cend = cstart + cs;
> + loff_t init = round_up(ni->initialized_size, PAGE_SIZE);
> + pgoff_t index;
> +
Hi Ernest,
Thanks for the series. I have a question about 13:
> + *zero = false;
> + if (cs <= PAGE_SIZE)
> + return end;
So for clusters up to a page, writeback never zeroes the cluster
before publishing its mapping.
Is there a case where a dirty folio still has clean blocks below
initialized_size here?
The case I'm thinking of is a short copy.
The rest of the cluster is then left to patch 10, which runs in
->iomap_end(), after the folio has been unlocked again.
Could writeback get to the folio before that and allocate the whole
cluster, writing only the dirty blocks?
Concretely, with 4 KiB clusters and 512-byte blocks: a 4096-byte
write at a cluster boundary into a hole, with the whole range below
initialized_size, and the copy stops after 512 bytes (the source buffer
is readable for the first 512 bytes only, the rest is inaccessible).
The folio had been read before, so it is uptodate and the short copy is
accepted, and one block is dirty.
If writeback then maps the whole cluster and writes those 512 bytes, and
the folio is dropped after that writeback has completed but before
->iomap_end() (DONTNEED, reclaim),wouldn't ntfs_dirty_cluster_tail()
take the branch for a mapped cluster,find no folio and just return?
The sizes don't change in that case, so the rollback wouldn't help
either, and the tail of the cluster would keep its old contents.
Could you help check whether this can actually happen?
If it can, maybe the zeroing could be decided for clusters up to a
page as well, from the dirty blocks of the cluster instead of whole
folios.
The zeroing you added to ntfs_attr_map_cluster() in patch 12 seems
like a good place for that. Just a thought.
Thanks,
Baolin.
> + if (end < min(cend, init))
> + *zero = true;
> + for (index = cstart >> PAGE_SHIFT; !*zero && index < folio->index &&
> + ((loff_t)index << PAGE_SHIFT) < init; index++)
> + if (!xa_get_mark(&inode->i_mapping->i_pages, index,
> + PAGECACHE_TAG_DIRTY))
> + *zero = true;
> + if (*zero || end < cend)
> + return min(end, cend);
> + return round_down(end, cs);
> +}
> +
> static ssize_t ntfs_writeback_range(struct iomap_writepage_ctx *wpc,
> struct folio *folio, u64 offset, unsigned int len, u64 end_pos)
> {
> if (offset < wpc->iomap.offset ||
> offset >= wpc->iomap.offset + wpc->iomap.length) {
> + struct inode *inode = wpc->inode;
> + loff_t end = min_t(loff_t, NTFS_I(inode)->allocated_size,
> + offset + NTFS_WRITEBACK_MAP_MAX);
> + int ntfs_iomap_flags = NTFS_IOMAP_FLAGS_WRITEBACK;
> + bool zero;
> int error;
>
> - error = __ntfs_write_iomap_begin(wpc->inode, offset,
> - NTFS_I(wpc->inode)->allocated_size - offset,
> - IOMAP_WRITE, &wpc->iomap,
> - NTFS_IOMAP_FLAGS_WRITEBACK);
> + end = ntfs_writeback_run_end(inode, folio, offset, len, end);
> + end = ntfs_dirty_range_end(inode->i_mapping, folio, end);
> + end = ntfs_writeback_cluster_end(inode, folio, offset, end,
> + &zero);
> + if (zero)
> + ntfs_iomap_flags |= NTFS_IOMAP_FLAGS_ZERO_NEW;
> + error = __ntfs_write_iomap_begin(inode, offset, end - offset,
> + IOMAP_WRITE, &wpc->iomap,
> + ntfs_iomap_flags);
> if (error)
> return error;
> }
>
^ permalink raw reply [flat|nested] 15+ messages in thread