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 03/11] ntfs: release delalloc reservations cut off by truncate
Date: Tue, 01 Dec 2026 18:42:38 +0100	[thread overview]
Message-ID: <20261201-b4-ntfs-delalloc-fixes-v4-3-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 into a hole turns the hole into an LCN_DELALLOC run and
reserves its clusters in vol->dirty_clusters
(ntfs_write_simple_iomap_begin_non_resident()).  Writeback hands the
reservation back when ntfs_cluster_alloc() allocates the run.

A truncate that cuts off delalloc runs before writeback drops their
dirty folios, frees the clusters of the attribute with
ntfs_cluster_free(), which skips everything that is not a real cluster,
and removes the runs with ntfs_rl_truncate_nolock().  Nothing gives the
reservation back, so the free space reported by statfs() and used by
ntfs_available_clusters_count() shrinks by that many clusters until the
volume is unmounted.  $Bitmap is not affected.

Workloads that rewrite files (open(O_TRUNC) + write) or truncate freshly
written data lose free space quickly: a test that writes, truncates and
punches holes in six 400 KiB files on a 128 MiB volume sees statfs()
free space drop from 79 MB to zero in 80 seconds, after which writes
fail with ENOSPC or come back short.  A 1 MiB buffered write followed by
ftruncate(fd, 0) and sync leaves 256 (4 KiB) clusters reserved.

Release the reservation of the delalloc runs that the shrink cuts off,
before the runlist is truncated.

Fixes: 495e90fa3348 ("ntfs: update attrib operations")
Cc: stable@vger.kernel.org
Signed-off-by: Ernest Borowski <ernest.borowski.git@proton.me>
---
 fs/ntfs/attrib.c | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)

diff --git a/fs/ntfs/attrib.c b/fs/ntfs/attrib.c
index c192333be8..3f19fd2ec7 100644
--- a/fs/ntfs/attrib.c
+++ b/fs/ntfs/attrib.c
@@ -4420,6 +4420,37 @@ static int ntfs_attr_make_resident(struct ntfs_inode *ni, struct ntfs_attr_searc
 	return 0;
 }
 
+/*
+ * ntfs_release_delalloc_from - give back the reservation of delalloc runs
+ * @ni:		non-resident ntfs attribute whose runlist is about to be truncated
+ * @start_vcn:	first vcn that is cut off
+ *
+ * Delayed allocation reserves vol->dirty_clusters for every LCN_DELALLOC
+ * cluster when the buffered write is done and only gives it back when
+ * writeback allocates the clusters.  Runs cut off by a truncate never reach
+ * writeback (their dirty folios are dropped from the page cache), so their
+ * reservation has to be released here or it is lost until unmount.
+ *
+ * The runlist must be mapped completely and locked for writing.
+ */
+static void ntfs_release_delalloc_from(struct ntfs_inode *ni, s64 start_vcn)
+{
+	struct runlist_element *rl = ni->runlist.rl;
+	s64 nr = 0;
+
+	lockdep_assert_held_write(&ni->runlist.lock);
+
+	for (; rl && rl->length; rl++) {
+		if (rl->lcn != LCN_DELALLOC || rl->vcn + rl->length <= start_vcn)
+			continue;
+		nr += rl->vcn + rl->length - max(rl->vcn, start_vcn);
+	}
+	if (!nr)
+		return;
+	ntfs_release_dirty_clusters(ni->vol, nr);
+	ni->i_dealloc_clusters -= min_t(s64, nr, ni->i_dealloc_clusters);
+}
+
 /*
  * ntfs_non_resident_attr_shrink - shrink a non-resident, open ntfs attribute
  * @ni:		non-resident ntfs attribute to shrink
@@ -4516,6 +4547,8 @@ static int ntfs_non_resident_attr_shrink(struct ntfs_inode *ni,
 		}
 		ntfs_attr_put_search_ctx(ctx);
 
+		ntfs_release_delalloc_from(ni, first_free_vcn);
+
 		/* Truncate the runlist itself. */
 		if (ntfs_rl_truncate_nolock(vol, &ni->runlist, first_free_vcn)) {
 			/*

-- 
2.56.1



  parent reply	other threads:[~2026-12-01 17:42 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 ` Ernest Borowski via B4 Relay [this message]
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 ` [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-3-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®