[PATCH v1 06/13] ntfs: serialize delalloc reservations with cluster allocation

From: Baolin Liu

Date: Wed Dec 02 2026 - 23:06:46 EST


From: Baolin Liu <liubaolin@xxxxxxxxxx>

The cluster allocator reads free_clusters before taking lcnbmp_lock, so
competing allocators can both pass the space check using the same free
space. The buffered iomap path also checks space and records its
reservation separately. Both races can let direct writes consume space
promised to buffered writes, causing ENOSPC and data loss at writeback.

Read free_clusters under lcnbmp_lock and hold that lock while checking
and taking a delayed-allocation reservation. Wait for the initial
free-space scan before locking, and release the reservation if runlist
allocation or merging fails.

Fixes: 11ccc9107dc4 ("ntfs: update runlist handling and cluster allocator")
Fixes: b041ca562526 ("ntfs: update iomap and address space operations")
Signed-off-by: Baolin Liu <liubaolin@xxxxxxxxxx>
---
fs/ntfs/iomap.c | 13 ++++++++++---
fs/ntfs/lcnalloc.c | 3 +--
2 files changed, 11 insertions(+), 5 deletions(-)

diff --git a/fs/ntfs/iomap.c b/fs/ntfs/iomap.c
index 8d4dc339b436..b4c9e854676e 100644
--- a/fs/ntfs/iomap.c
+++ b/fs/ntfs/iomap.c
@@ -466,8 +466,16 @@ remap_rl:

if (lcn <= LCN_DELALLOC) {
if (lcn < LCN_DELALLOC) {
+ if (!NVolFreeClusterKnown(vol))
+ wait_event(vol->free_waitq,
+ NVolFreeClusterKnown(vol));
+ /* Reserve space before a competing allocator can take it. */
+ down_write(&vol->lcnbmp_lock);
max_clu_count =
ntfs_available_clusters_count(vol, max_clu_count);
+ if (max_clu_count > 0)
+ ntfs_hold_dirty_clusters(vol, max_clu_count);
+ up_write(&vol->lcnbmp_lock);
if (max_clu_count < 0) {
err = max_clu_count;
up_write(&ni->runlist.lock);
@@ -485,6 +493,7 @@ remap_rl:
rlc = kmalloc(sizeof(struct runlist_element) * 2,
GFP_NOFS);
if (!rlc) {
+ ntfs_release_dirty_clusters(vol, max_clu_count);
up_write(&ni->runlist.lock);
mutex_unlock(&mrec_ni->mrec_lock);
return -ENOMEM;
@@ -502,6 +511,7 @@ remap_rl:
&new_rl_count);
if (IS_ERR(rl)) {
ntfs_error(vol->sb, "Failed to merge runlists");
+ ntfs_release_dirty_clusters(vol, max_clu_count);
up_write(&ni->runlist.lock);
mutex_unlock(&mrec_ni->mrec_lock);
kfree(rlc);
@@ -515,9 +525,6 @@ remap_rl:
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;
diff --git a/fs/ntfs/lcnalloc.c b/fs/ntfs/lcnalloc.c
index 7e00016f42ad..a4fe5077dd7f 100644
--- a/fs/ntfs/lcnalloc.c
+++ b/fs/ntfs/lcnalloc.c
@@ -219,10 +219,9 @@ struct runlist_element *ntfs_cluster_alloc(struct ntfs_volume *vol, const s64 st

if (!NVolFreeClusterKnown(vol))
wait_event(vol->free_waitq, NVolFreeClusterKnown(vol));
- free_clusters = atomic64_read(&vol->free_clusters);
-
/* Take the lcnbmp lock for writing. */
down_write(&vol->lcnbmp_lock);
+ free_clusters = atomic64_read(&vol->free_clusters);
if (is_dealloc == false)
free_clusters -= atomic64_read(&vol->dirty_clusters);