[PATCH v5 10/13] ntfs: zero the rest of a new cluster after a short buffered write

From: Ernest Borowski via B4 Relay

Date: Fri Dec 04 2026 - 13:41:00 EST


From: Ernest Borowski <ernest.borowski.git@xxxxxxxxx>

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@xxxxxxxxxxxxxxx
Suggested-by: Hyunchul Lee <hyc.lee@xxxxxxxxx>
Signed-off-by: Ernest Borowski <ernest.borowski.git@xxxxxxxxx>
---
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