From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=117.135.210.5 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796270846; b=A1D62kR3VQ6Glg9cuOKOQojMqZDBl96wIzMiqZbwZQIkXRIb7XbbhsvoKh8rqCy/sL2/ FUdTYvO0K4gd7/cykvXwb0am+mi71cprazhYmNkHiVnGKaxv3pWNrfJ8x+U7/WeQJdjcE vtvdxvsP3GaRsBKbkHfSau5cMdzF+8rEbGLxoP6TTCy7iapKBnQXaqo68elPYOaGMBWBX D8nUOZNK2pNq0bEPhzOTT7eqKJgwKESEK5CkhC7y+1mhvCTNk3ZicamavUYIc9evP+nc6 OXFL/4EQVVP+gjmTAJsZ18FTW+nHAmkCqnCyalVKRc1x7vgSsF5y3WDA7fiOGmQY6nw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796270846; h=DMARC-Filter:DKIM-Signature:Received:From:To:Cc:Subject:Date: Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-CM-TRANSID:X-Coremail-Antispam: X-Originating-IP:X-CM-SenderInfo; bh=dgJ6auLBfJ3B3UnX157Azav4WznGGZqjK6n0tBOfbRs=; b=GoWh52lefdZzu+6EAc9RbwalZUJyiuRrhQUHrBEJ9xULXbclD+AFNSKV2yS+5KMP0WRK G0YPHLDwtWRcGgbXE0YRHYq4apwidW/Wbd8Hg/wQmj7mNvbN5WqMxrrZ1YUM90ulfy2cD ie7kfFc2AuPGgcXNz0g3hLEbXFqeZDMjEWENPih3OAYz1JpOsw4j/83OuZ0NbhP376qSH nFpYWwtZCaHLnNjk/SLHWJIJ16QtdYG6UE9FOilJn+2ETgTprFwejdbZ926Hmk2z8F/9r CxwonGJpZTJLp3VoITT7KwRdhNQj8YlCYNbyKJ8B9OswKgRxQq/vgF9aw+9juhVhpPA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=163.com; spf=pass smtp.mailfrom=163.com; dkim=pass header.d=163.com header.i=@163.com header.a=rsa-sha256 header.s=s110527 header.b=P+wbgk7e; arc=none smtp.remote-ip=117.135.210.5 Received: from m16.mail.163.com (m16.mail.163.com [117.135.210.5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A1A6B606A322 for ; Thu, 3 Dec 2026 04:07:25 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org A1A6B606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=163.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.a=rsa-sha256 header.s=s110527 header.b=P+wbgk7e DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:To:Subject:Date:Message-ID:MIME-Version; bh=dg J6auLBfJ3B3UnX157Azav4WznGGZqjK6n0tBOfbRs=; b=P+wbgk7e8qc9gyO0pE 4tFJu+UFGTPl2ODOBsYPK1F1F8axvlQz/79811C3i+lLWM02uVfQzFCuByn64S/0 3aVAPETX1QCmG/WgfDog876k3r+qj4nSIBIfQmjCCw/ZjNaCaWAjLlScb93NR/+R 9pqzafYjmBszXVv/NyZrmQdbo= Received: from liubaolin-ROG-Strix-G713PV-G713PV (unknown []) by gzga-smtp-mtada-g0-2 (Coremail) with SMTP id _____wD33yfs6hBrK9w8MA--.17535S2; Thu, 03 Dec 2026 12:07:09 +0800 (CST) From: Baolin Liu To: linkinjeon@kernel.org, hyc.lee@gmail.com Cc: ntfs@lists.linux.dev, linux-kernel@vger.kernel.org, Baolin Liu Subject: [PATCH v1 08/13] ntfs: keep space for MFT growth during delayed allocation Date: Thu, 3 Dec 2026 12:03:47 +0800 Message-ID: <20261203040352.1161120-9-liubaolin12138@163.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261203040352.1161120-1-liubaolin12138@163.com> References: <20261203040352.1161120-1-liubaolin12138@163.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID:_____wD33yfs6hBrK9w8MA--.17535S2 X-Coremail-Antispam: 1Uf129KBjvAXoWfGF1DZw45XrWfXF1kGw1xXwb_yoW8JrWDXo WS9a1Uuw1qgryjyw4vk3ykta98uFWkKws5JrsxGryDCFyjvF1Dua17twnIgFy3Ka1rKFnr tryUJa95AFZavFy7n29KB7ZKAUJUUUU8529EdanIXcx71UUUUU7v73VFW2AGmfu7bjvjm3 AaLaJ3UbIYCTnIWIevJa73UjIFyTuYvjxU2l4EDUUUU X-CM-SenderInfo: xolxutxrol0iasrtmqqrwthudrp/xtbC6g3Un2sQ6u0YhwAA3P From: Baolin Liu 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 --- 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 #include #include +#include #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;