[PATCH v5 12/13] ntfs: zero a write fault's new cluster before the runlist shows it
From: Ernest Borowski via B4 Relay
Date: Fri Dec 04 2026 - 13:41:03 EST
From: Ernest Borowski <ernest.borowski.git@xxxxxxxxx>
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@xxxxxxxxxxxxxxx
Signed-off-by: Ernest Borowski <ernest.borowski.git@xxxxxxxxx>
---
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