From: Ernest Borowski via B4 Relay <devnull+ernest.borowski.git.proton.me@kernel.org>
To: Namjae Jeon <linkinjeon@kernel.org>,
Hyunchul Lee <hyc.lee@gmail.com>,
Baolin Liu <liubaolin12138@163.com>
Cc: Christian Brauner <brauner@kernel.org>,
Christoph Hellwig <hch@lst.de>,
"Darrick J. Wong" <djwong@kernel.org>,
ntfs@lists.linux.dev, linux-fsdevel@vger.kernel.org,
linux-xfs@vger.kernel.org, linux-kernel@vger.kernel.org,
Zhan Xusheng <zhanxusheng1024@gmail.com>,
stable@vger.kernel.org,
Ernest Borowski <ernest.borowski.git@proton.me>
Subject: [PATCH v4 11/11] ntfs: revalidate a buffered write's mapping after its clusters were allocated
Date: Tue, 01 Dec 2026 18:42:46 +0100 [thread overview]
Message-ID: <20261201-b4-ntfs-delalloc-fixes-v4-11-e5dd6f77016a@proton.me> (raw)
In-Reply-To: <20261201-b4-ntfs-delalloc-fixes-v4-0-e5dd6f77016a@proton.me>
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
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 2ee08fa738..a5727968fa 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,
@@ -468,6 +515,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
prev parent reply other threads:[~2026-12-01 17:43 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-01 17:42 [PATCH v4 00/11] ntfs: delayed allocation accounting and stale data fixes, part 1 Ernest Borowski via B4 Relay
2026-12-01 17:42 ` [PATCH v4 01/11] iomap: punch out a run of clean blocks in one call Ernest Borowski via B4 Relay
2026-12-01 17:42 ` [PATCH v4 02/11] iomap: add a helper to mark a range of a folio dirty Ernest Borowski via B4 Relay
2026-12-01 17:42 ` [PATCH v4 03/11] ntfs: release delalloc reservations cut off by truncate Ernest Borowski via B4 Relay
2026-12-01 17:42 ` [PATCH v4 04/11] ntfs: do not consume delalloc reservations when filling holes Ernest Borowski via B4 Relay
2026-12-01 17:42 ` [PATCH v4 05/11] ntfs: keep delalloc reservations when a cluster allocation fails Ernest Borowski via B4 Relay
2026-12-01 17:42 ` [PATCH v4 06/11] ntfs: release the delalloc reservations of an evicted inode Ernest Borowski via B4 Relay
2026-12-01 17:42 ` [PATCH v4 07/11] ntfs: do not shift the cluster number of a delalloc run on split Ernest Borowski via B4 Relay
2026-12-01 17:42 ` [PATCH v4 08/11] ntfs: punch out the delalloc reservation of a failed buffered write Ernest Borowski via B4 Relay
2026-12-01 17:42 ` [PATCH v4 09/11] ntfs: zero new delalloc clusters even when writeback allocates them Ernest Borowski via B4 Relay
2026-12-01 17:42 ` [PATCH v4 10/11] ntfs: zero the rest of a new cluster after a short buffered write Ernest Borowski via B4 Relay
2026-12-01 17:42 ` Ernest Borowski via B4 Relay [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261201-b4-ntfs-delalloc-fixes-v4-11-e5dd6f77016a@proton.me \
--to=devnull+ernest.borowski.git.proton.me@kernel.org \
--cc=brauner@kernel.org \
--cc=djwong@kernel.org \
--cc=ernest.borowski.git@proton.me \
--cc=hch@lst.de \
--cc=hyc.lee@gmail.com \
--cc=linkinjeon@kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-xfs@vger.kernel.org \
--cc=liubaolin12138@163.com \
--cc=ntfs@lists.linux.dev \
--cc=stable@vger.kernel.org \
--cc=zhanxusheng1024@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®