mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Baolin Liu <liubaolin12138@163.com>
To: linkinjeon@kernel.org, hyc.lee@gmail.com
Cc: ntfs@lists.linux.dev, linux-kernel@vger.kernel.org,
	Baolin Liu <liubaolin@kylinos.cn>
Subject: [PATCH v1 08/13] ntfs: keep space for MFT growth during delayed allocation
Date: Thu,  3 Dec 2026 12:03:47 +0800	[thread overview]
Message-ID: <20261203040352.1161120-9-liubaolin12138@163.com> (raw)
In-Reply-To: <20261203040352.1161120-1-liubaolin12138@163.com>

From: Baolin Liu <liubaolin@kylinos.cn>

Delayed allocation reserves data clusters but leaves no space for the
MFT or attribute-list growth needed to describe them. Near ENOSPC,
metadata allocation can therefore fail during writeback after buffered
writes have succeeded. Separated writes on a nearly full volume
reproduce the resulting fsync failure and data loss without large folios.

Keep 2% of the volume, capped at 4096 clusters, out of new data allocations
and delayed-allocation reservations. Allow MFT, MFT bitmap and
attribute-list allocations to use this reserve while respecting
outstanding data reservations. Preserve their existing placement policy.

Exclude the reserve from f_bavail and document that data writes cannot
use it, including writes by root. This is an in-memory allocation policy,
separate from MFT zone placement, with the same sizing policy as ext4's
metadata reserve.

Fixes: b041ca562526 ("ntfs: update iomap and address space operations")
Signed-off-by: Baolin Liu <liubaolin@kylinos.cn>
---
 Documentation/filesystems/ntfs.rst | 15 +++++++++++++++
 fs/ntfs/attrib.c                   | 11 ++++++-----
 fs/ntfs/attrlist.c                 |  2 +-
 fs/ntfs/file.c                     |  3 ++-
 fs/ntfs/lcnalloc.c                 | 15 ++++++++++-----
 fs/ntfs/lcnalloc.h                 | 13 ++++++++++++-
 fs/ntfs/mft.c                      |  7 ++++---
 fs/ntfs/super.c                    | 18 +++++++++++++++---
 fs/ntfs/volume.h                   |  2 ++
 9 files changed, 67 insertions(+), 19 deletions(-)

diff --git a/Documentation/filesystems/ntfs.rst b/Documentation/filesystems/ntfs.rst
index 635301117807..f4c1a281f76d 100644
--- a/Documentation/filesystems/ntfs.rst
+++ b/Documentation/filesystems/ntfs.rst
@@ -11,6 +11,7 @@ The Linux NTFS filesystem driver
    - Utilities support
    - Linux file attributes
    - Temporary files
+   - Space accounting
    - Supported mount options
 
 
@@ -78,6 +79,20 @@ Named stream ioctls cannot create or open streams on an unpublished
 temporary file. They can be used after the file has been linked.
 
 
+Space accounting
+================
+
+The driver keeps up to 2% of the volume, capped at 4096 clusters, available
+for growth of the MFT and attribute lists. These structures may need more
+space when delayed writes are converted into allocated extents. New file
+data allocations cannot use this reserve, including writes by root.
+
+``statfs(2)`` excludes delayed allocation reservations from ``f_bfree``
+and also excludes the metadata reserve from ``f_bavail``. The reserve is
+an allocation policy, not an on-disk allocation; it is independent of the
+placement preference controlled by ``mft_zone_multiplier``.
+
+
 Supported mount options
 =======================
 
diff --git a/fs/ntfs/attrib.c b/fs/ntfs/attrib.c
index ea3a477c0061..4dda083598c6 100644
--- a/fs/ntfs/attrib.c
+++ b/fs/ntfs/attrib.c
@@ -2132,7 +2132,7 @@ static int __ntfs_attr_make_non_resident(struct ntfs_inode *ni, const u32 data_s
 		/* Start by allocating clusters to hold the attribute value. */
 		rl = ntfs_cluster_alloc(vol, 0,
 				ntfs_bytes_to_cluster(vol, new_size),
-				-1, DATA_ZONE, true, false, false);
+				-1, ntfs_attr_alloc_zone(ni), true, false, false);
 		if (IS_ERR(rl)) {
 			err = PTR_ERR(rl);
 			ntfs_debug("Failed to allocate cluster%s, error code %i.",
@@ -4754,7 +4754,7 @@ static int ntfs_non_resident_attr_expand(struct ntfs_inode *ni, const s64 newsiz
 					ntfs_bytes_to_cluster(vol, ni->allocated_size),
 					first_free_vcn -
 					ntfs_bytes_to_cluster(vol, ni->allocated_size),
-					lcn_seek_from, DATA_ZONE, false,
+					lcn_seek_from, ntfs_attr_alloc_zone(ni), false,
 					ni->type == AT_ATTRIBUTE_LIST, false);
 			if (IS_ERR(rl)) {
 				ntfs_debug("Cluster allocation failed (%lld)",
@@ -4783,7 +4783,7 @@ static int ntfs_non_resident_attr_expand(struct ntfs_inode *ni, const s64 newsiz
 						first_free_vcn -
 						ntfs_bytes_to_cluster(vol,
 								ni->allocated_size),
-						lcn_seek_from, DATA_ZONE, false,
+						lcn_seek_from, ntfs_attr_alloc_zone(ni), false,
 						false, false);
 				if (IS_ERR(rl)) {
 					err = PTR_ERR(rl);
@@ -5487,8 +5487,9 @@ int ntfs_attr_map_cluster(struct ntfs_inode *ni, s64 vcn_start, s64 *lcn_start,
 		}
 	}
 
-	rlc = ntfs_cluster_alloc(vol, vcn, clu_count, lcn_seek_from, DATA_ZONE,
-			false, true, lcn == LCN_DELALLOC);
+	rlc = ntfs_cluster_alloc(vol, vcn, clu_count, lcn_seek_from,
+				 ntfs_attr_alloc_zone(ni), false, true,
+				 lcn == LCN_DELALLOC);
 	if (IS_ERR(rlc)) {
 		err = PTR_ERR(rlc);
 		goto out;
diff --git a/fs/ntfs/attrlist.c b/fs/ntfs/attrlist.c
index 7e7440c5f2ba..8a52343f0a7e 100644
--- a/fs/ntfs/attrlist.c
+++ b/fs/ntfs/attrlist.c
@@ -114,7 +114,7 @@ static int ntfs_attrlist_repack(struct inode *attr_vi,
 	}
 
 	/* A single run keeps the mapping pairs at the minimum size. */
-	new_rl = ntfs_cluster_alloc(vol, 0, nr_clusters, -1, DATA_ZONE,
+	new_rl = ntfs_cluster_alloc(vol, 0, nr_clusters, -1, METADATA_ZONE,
 				    true, true, false);
 	if (IS_ERR(new_rl)) {
 		err = PTR_ERR(new_rl);
diff --git a/fs/ntfs/file.c b/fs/ntfs/file.c
index 7d93a520566e..4a837c599c82 100644
--- a/fs/ntfs/file.c
+++ b/fs/ntfs/file.c
@@ -1261,7 +1261,8 @@ static int ntfs_allocate_range(struct ntfs_inode *ni, int mode, loff_t offset,
 		need_space = end_vcn - start_vcn;
 	if (need_space > 0 &&
 	    need_space > (atomic64_read(&vol->free_clusters) -
-			  atomic64_read(&vol->dirty_clusters))) {
+			  atomic64_read(&vol->dirty_clusters) -
+			  vol->mft_reserved_clusters)) {
 		err = -ENOSPC;
 		goto out;
 	}
diff --git a/fs/ntfs/lcnalloc.c b/fs/ntfs/lcnalloc.c
index a4fe5077dd7f..f27223a7bb03 100644
--- a/fs/ntfs/lcnalloc.c
+++ b/fs/ntfs/lcnalloc.c
@@ -122,16 +122,18 @@ static s64 max_empty_bit_range(unsigned char *buf, int size)
  * @start_vcn:		vcn of the first allocated cluster
  * @count:		number of clusters to allocate
  * @start_lcn:		starting lcn at which to allocate the clusters or -1 if none
- * @zone:		zone from which to allocate (MFT_ZONE or DATA_ZONE)
+ * @zone:		allocation policy (MFT_ZONE, DATA_ZONE or METADATA_ZONE)
  * @is_extension:	if true, the caller is extending an attribute
  * @is_contig:		if true, require contiguous allocation
- * @is_dealloc:		if true, the allocation is for deallocation purposes
+ * @is_dealloc:		if true, consume an existing delayed allocation reservation
  *
  * Allocate @count clusters preferably starting at cluster @start_lcn or at the
  * current allocator position if @start_lcn is -1, on the mounted ntfs volume
  * @vol. @zone is either DATA_ZONE for allocation of normal clusters or
  * MFT_ZONE for allocation of clusters for the master file table, i.e. the
- * $MFT/$DATA attribute.
+ * $MFT/$DATA attribute. METADATA_ZONE uses DATA_ZONE placement but may use
+ * the metadata reserve, as may MFT_ZONE. Neither may consume outstanding
+ * delayed allocation reservations.
  *
  * @start_vcn specifies the vcn of the first allocated cluster.  This makes
  * merging the resulting runlist with the old runlist easier.
@@ -222,8 +224,11 @@ struct runlist_element *ntfs_cluster_alloc(struct ntfs_volume *vol, const s64 st
 	/* Take the lcnbmp lock for writing. */
 	down_write(&vol->lcnbmp_lock);
 	free_clusters = atomic64_read(&vol->free_clusters);
-	if (is_dealloc == false)
+	if (!is_dealloc) {
 		free_clusters -= atomic64_read(&vol->dirty_clusters);
+		if (zone == DATA_ZONE)
+			free_clusters -= vol->mft_reserved_clusters;
+	}
 
 	if (free_clusters < count) {
 		err = -ENOSPC;
@@ -252,7 +257,7 @@ struct runlist_element *ntfs_cluster_alloc(struct ntfs_volume *vol, const s64 st
 	zone_start = start_lcn;
 
 	if (zone_start < 0) {
-		if (zone == DATA_ZONE)
+		if (zone != MFT_ZONE)
 			zone_start = vol->data1_zone_pos;
 		else
 			zone_start = vol->mft_zone_pos;
diff --git a/fs/ntfs/lcnalloc.h b/fs/ntfs/lcnalloc.h
index d9ac622e4658..8ed1427e3458 100644
--- a/fs/ntfs/lcnalloc.h
+++ b/fs/ntfs/lcnalloc.h
@@ -18,15 +18,26 @@
  * FIRST_ZONE		For sanity checking.
  * MFT_ZONE		Allocate from $MFT zone.
  * DATA_ZONE		Allocate from $DATA zone.
+ * METADATA_ZONE		Use $DATA placement, allowing the metadata reserve.
  * LAST_ZONE		For sanity checking.
  */
 enum {
 	FIRST_ZONE	= 0,
 	MFT_ZONE	= 0,
 	DATA_ZONE	= 1,
-	LAST_ZONE	= 1,
+	METADATA_ZONE	= 2,
+	LAST_ZONE	= 2,
 };
 
+static inline int ntfs_attr_alloc_zone(const struct ntfs_inode *ni)
+{
+	if (ni->mft_no == FILE_MFT)
+		return ni->type == AT_DATA ? MFT_ZONE : METADATA_ZONE;
+	if (ni->type == AT_ATTRIBUTE_LIST)
+		return METADATA_ZONE;
+	return DATA_ZONE;
+}
+
 struct runlist_element *ntfs_cluster_alloc(struct ntfs_volume *vol,
 		const s64 start_vcn, const s64 count, const s64 start_lcn,
 		const int zone,
diff --git a/fs/ntfs/mft.c b/fs/ntfs/mft.c
index 34fccfaa11c5..7b935a1f89e3 100644
--- a/fs/ntfs/mft.c
+++ b/fs/ntfs/mft.c
@@ -1410,7 +1410,8 @@ static int ntfs_mft_bitmap_extend_allocation_nolock(struct ntfs_volume *vol)
 	 */
 	if (*b != 0xff && !(*b & tb) &&
 	    vol->lcn_empty_bits_per_page[ll >> PAGE_SHIFT] &&
-	    ntfs_available_clusters_count(vol, 1) > 0) {
+	    atomic64_read(&vol->free_clusters) >
+			atomic64_read(&vol->dirty_clusters)) {
 		/* Next cluster is free, allocate it. */
 		*b |= tb;
 		folio_mark_dirty(folio);
@@ -1432,8 +1433,8 @@ static int ntfs_mft_bitmap_extend_allocation_nolock(struct ntfs_volume *vol)
 		up_write(&vol->lcnbmp_lock);
 alloc_cluster:
 		/* Allocate a cluster from the DATA_ZONE. */
-		rl2 = ntfs_cluster_alloc(vol, rl[1].vcn, 1, lcn, DATA_ZONE,
-				true, false, false);
+		rl2 = ntfs_cluster_alloc(vol, rl[1].vcn, 1, lcn, METADATA_ZONE,
+					 true, false, false);
 		if (IS_ERR(rl2)) {
 			up_write(&mftbmp_ni->runlist.lock);
 			ntfs_error(vol->sb,
diff --git a/fs/ntfs/super.c b/fs/ntfs/super.c
index 39ff56cecf62..d9f3ffa66104 100644
--- a/fs/ntfs/super.c
+++ b/fs/ntfs/super.c
@@ -14,6 +14,7 @@
 #include <linux/sched/mm.h>
 #include <linux/fs_context.h>
 #include <linux/fs_parser.h>
+#include <linux/math64.h>
 
 #include "sysctl.h"
 #include "logfile.h"
@@ -1034,6 +1035,16 @@ static void ntfs_setup_allocators(struct ntfs_volume *vol)
 {
 	s64 mft_zone_size, mft_lcn;
 
+	/*
+	 * Keep a bounded pool for the metadata needed to describe new runs.
+	 * Delayed allocation reserves data before its mapping pairs are built;
+	 * allowing data to consume all free clusters can strand those writes.
+	 * Like ext4's metadata reserve, use 2% of the volume, capped at 4096
+	 * clusters. This is separate from the MFT zone's placement preference.
+	 */
+	vol->mft_reserved_clusters = min_t(s64, div_s64(vol->nr_clusters, 50),
+					   4096);
+
 	ntfs_debug("vol->mft_zone_multiplier = 0x%x",
 			vol->mft_zone_multiplier);
 	/* Determine the size of the MFT zone. */
@@ -2423,7 +2434,7 @@ s64 ntfs_available_clusters_count(struct ntfs_volume *vol, s64 nr_clusters)
 		wait_event(vol->free_waitq, NVolFreeClusterKnown(vol));
 
 	free_clusters = atomic64_read(&vol->free_clusters) -
-		atomic64_read(&vol->dirty_clusters);
+		atomic64_read(&vol->dirty_clusters) - vol->mft_reserved_clusters;
 	if (free_clusters <= 0)
 		return -ENOSPC;
 	else if (free_clusters < nr_clusters)
@@ -2557,8 +2568,9 @@ static int ntfs_statfs(struct dentry *dentry, struct kstatfs *sfs)
 	if (size < 0LL)
 		size = 0LL;
 
-	/* Free blocks avail to non-superuser, same as above on NTFS. */
-	sfs->f_bavail = sfs->f_bfree = size;
+	sfs->f_bfree = size;
+	/* The metadata reserve is unavailable to file data, even for root. */
+	sfs->f_bavail = max_t(s64, size - vol->mft_reserved_clusters, 0);
 
 	/* Number of inodes in filesystem (at this point in time). */
 	read_lock_irqsave(&mft_ni->size_lock, flags);
diff --git a/fs/ntfs/volume.h b/fs/ntfs/volume.h
index ed5c0ccb9cd9..284c3d81aa57 100644
--- a/fs/ntfs/volume.h
+++ b/fs/ntfs/volume.h
@@ -89,6 +89,7 @@
  * @bad_clusters: Number of clusters recorded in $BadClus:$Bad.
  * @free_mft_records: Track the free mft records.
  * @dirty_clusters: Number of clusters that are dirty.
+ * @mft_reserved_clusters: Space kept for MFT and attribute list growth.
  * @sparse_compression_unit: Size of compression/sparse unit in clusters.
  * @lcn_empty_bits_per_page: Number of empty bits per page in the LCN bitmap.
  * @precalc_work: Work structure for background pre-calculation tasks.
@@ -163,6 +164,7 @@ struct ntfs_volume {
 	atomic64_t free_clusters;
 	atomic64_t free_mft_records;
 	atomic64_t dirty_clusters;
+	s64 mft_reserved_clusters;
 	u8 sparse_compression_unit;
 	unsigned int *lcn_empty_bits_per_page;
 	struct work_struct precalc_work;


  parent reply	other threads:[~2026-12-03  4:07 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03  4:03 [PATCH v1 00/13] ntfs: add large folio support for ordinary files Baolin Liu
2026-12-03  4:03 ` [PATCH v1 01/13] ntfs: limit writeback allocation to the dirty range Baolin Liu
2026-12-03  4:03 ` [PATCH v1 02/13] ntfs: read compression units without the block device page cache Baolin Liu
2026-12-03  4:03 ` [PATCH v1 03/13] ntfs: snapshot resident data for direct reads Baolin Liu
2026-12-03  4:03 ` [PATCH v1 04/13] ntfs: allow vmalloc for large runlist backups Baolin Liu
2026-12-03  4:03 ` [PATCH v1 05/13] ntfs: preserve delalloc reservations when allocating holes Baolin Liu
2026-12-03  4:03 ` [PATCH v1 06/13] ntfs: serialize delalloc reservations with cluster allocation Baolin Liu
2026-12-03  4:03 ` [PATCH v1 07/13] ntfs: respect space reservations when writing compressed data Baolin Liu
2026-12-03  4:03 ` Baolin Liu [this message]
2026-12-03  4:03 ` [PATCH v1 09/13] ntfs: serialize compression changes with file access Baolin Liu
2026-12-03  4:03 ` [PATCH v1 10/13] ntfs: complete compressed reads at folio granularity Baolin Liu
2026-12-03  4:03 ` [PATCH v1 11/13] ntfs: stop compressed reads at EOF within a base page Baolin Liu
2026-12-03  4:03 ` [PATCH v1 12/13] ntfs: preserve large folios during compressed writes Baolin Liu
2026-12-03  4:03 ` [PATCH v1 13/13] ntfs: enable large folios for ordinary files Baolin Liu
2026-12-03  4:23 ` [PATCH v1 00/13] ntfs: add large folio support " liubaolin
2026-12-03  5:30   ` Namjae Jeon
2026-12-03 10:10     ` liubaolin

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=20261203040352.1161120-9-liubaolin12138@163.com \
    --to=liubaolin12138@163.com \
    --cc=hyc.lee@gmail.com \
    --cc=linkinjeon@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=liubaolin@kylinos.cn \
    --cc=ntfs@lists.linux.dev \
    /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®