mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
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 05/11] ntfs: keep delalloc reservations when a cluster allocation fails
Date: Tue, 01 Dec 2026 18:42:40 +0100	[thread overview]
Message-ID: <20261201-b4-ntfs-delalloc-fixes-v4-5-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>

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
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



  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 ` Ernest Borowski via B4 Relay [this message]
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 ` [PATCH v4 11/11] ntfs: revalidate a buffered write's mapping after its clusters were allocated Ernest Borowski via B4 Relay

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-5-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®