ntfs: fix spurious EIO and ENOSPC on nearly full volumes
v2 cover ntfs: fix spurious EIO and ENOSPC on nearly full volumes
These fix three errors returned while free space is left. Patch 1 fixes file creation failing with EIO on a nearly full volume; it now goes on until the volume is full and then fails with ENOSPC. Patch 2 fixes the cluster allocator missing free clusters when its start hint lies at or past the end of the volume, which empties the MFT zone, and patch 3 removes the fallocate() check that then fails every later call. Together the two bugs make fallocate() refuse space that is free until the next mount. Reproducers, run with KASAN and lockdep:
https://github.com/matthiasgoergens/linux/tree/reproducer/2026-10-01-ntfs-nearfull-v2
Changes in v2: in patch 2, a contiguous request whose run from the hint stops at the end of a bitmap buffer or of the zone now gets that run back, instead of a second, non-adjacent run from the zone position, as Baolin Liu suggested. That second run also happens on ntfs-next without this series, and fallocate() over a hole then leaves part of the range reading the data of a deleted file; patch 2 now fixes that as well. Rebased on ntfs-next, with Baolin's Reviewed-by on patches 1 and 3.
v1: https://lore.kernel.org/all/[email protected]/
Matthias Goergens (3):
ntfs: set the attribute list size before reserving space for it
ntfs: restart the zone search when the allocation hint fails
ntfs: do not refuse fallocate() when the MFT zone is empty
fs/ntfs/attrlist.c | 5 +++-- fs/ntfs/file.c | 3 --- fs/ntfs/lcnalloc.c | 25 ++++++++++++++++++++++--- 3 files changed, 25 insertions(+), 8 deletions(-)
base-commit: 708f9d56cacae21aeee98d16bcdd50a66edc04a0
Signature · 1 line
2.56.0
PATCH v2 1/3 ntfs: set the attribute list size before reserving space for it
ntfs_attrlist_update_locked() resizes the attribute list and then, for $MFT, tries to reserve the maximum list size. When that allocation fails with -ENOSPC it falls back to ntfs_attrlist_repack(), which reads the list through ntfs_inode_attr_pread(). The read stops at i_size, but i_size is only updated after the reserve, so when the list has just grown the repack gets a short read and returns -EIO. Only -ENOSPC from the reserve is ignored, so the -EIO fails the $MFT extension and with it the file creation that needed a new mft record.
On a 32 MiB volume with 512-byte clusters whose $MFT has a non-resident attribute list, 591 of 1500 creations of empty files succeed and the rest fail with EIO while 742 KiB is still free:
ntfs: (device vda): ntfs_attrlist_update_locked(): Failed to reserve attribute list space
ntfs: Failed add attr entry to attrlist
ntfs: MP update failed
ntfs: (device vda): ntfs_mft_record_alloc(): Failed to extend mft data allocation.
Update i_size as soon as the list has been resized. File creation then goes on until the volume is full and fails with ENOSPC.
---
The volume is frag.img from
https://github.com/matthiasgoergens/linux/tree/reproducer/2026-09-26-ntfs-mft-runlist
(mft-bootstrap/frag.img.xz); creating empty files on it until one fails is enough. Tested with KASAN and lockdep: after the patch the files written before the volume filled read back intact after a remount, and ntfsfix and ntfsresize --info find nothing wrong with the image.
--- fs/ntfs/attrlist.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
fs/ntfs/attrlist.c +3 -2
@@ -256,6 +256,9 @@ int ntfs_attrlist_update_locked(struct ntfs_inode *base_ni,return err; } +/* ntfs_attrlist_repack() below reads the list up to i_size. */ +i_size_write (attr_vi, base_ni->attr_list_size); +/* * Reserve the maximum legal list size while the MFT metadata area is * still easy to allocate contiguously. This prevents a later list entry
@@ -283,8 +286,6 @@ int ntfs_attrlist_update_locked(struct ntfs_inode *base_ni, } } -i_size_write (attr_vi, base_ni->attr_list_size); -if (NInoNonResident (attr_ni) && !NInoAttrListNonResident (base_ni))NInoSetAttrListNonResident (base_ni);
Signature · 1 line
2.56.0
PATCH v2 2/3 ntfs: restart the zone search when the allocation hint fails
In reply to Matthias GoergensWhen ntfs_cluster_alloc() is given a start_lcn, it first tries the clusters from there on. If that does not satisfy the request, it moves on to the zone's current position, but keeps the pass, the zone_end and the has_guess state it had. That loses free clusters in two ways. If the search had already moved on to pass 2, whose range ends at the hint, it scans only from the zone position to the hint and misses every free cluster between the start of the zone and the zone position. If no cluster had been tested yet, has_guess is still set, so the cluster at the zone position is tried as if it had been the hint, and when that cluster is in use the rest of the bitmap buffer is skipped.
Both happen when the hint lies at or past the end of the volume, which ntfs_attr_map_cluster() produces when it extrapolates from the last allocated run across a hole. The allocator then finds nothing, shrinks the MFT zone to nothing trying to satisfy the request, and fails with -ENOSPC.
To reproduce on a 128 MiB volume with 4 KiB clusters, extend two files in turn by one cluster at a time, alternating between fallocate(), write() past EOF and truncate() up, so that the runs of each file are separated by holes. When the volume is full, truncate both files back to 1 MiB and start again. During the second round a one-cluster fallocate() fails with ENOSPC while 40 MiB is free.
Start the zone over as if no hint had been given. A contiguous request can get there with the run from the hint already allocated, when that run reached the end of a bitmap buffer or of the zone. Return that run instead, as when a cluster in use ends it; going on at the zone position gives the request a second run that its caller does not expect. That could happen before this patch too, whenever the cluster at the zone position was free. ntfs_attr_map_cluster() reports and zeroes only the first run, so after a fallocate() of 20 clusters over a hole, with the hint 4 clusters before the end of the volume, 16 of them read back the data of a deleted file.
---
v2: for a contiguous request, return the run from the hint instead of going on at the zone position (Baolin Liu). This also fixes the stale data after fallocate() described in the last paragraph.
--- fs/ntfs/lcnalloc.c | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-)
fs/ntfs/lcnalloc.c +22 -3
@@ -506,13 +506,32 @@ struct runlist_element *ntfs_cluster_alloc(struct ntfs_volume *vol, const s64 st }if (!used_zone_pos) { +/* +* The run at @start_lcn reached the end of the buffer +* or the zone. A contiguous request gets that run +* alone, not a second one from the zone position. +*/ +if (is_contig && rlpos) + goto out; +/* +* Leaving @start_lcn for the zone position starts the +* zone over as if no hint had been given, even if the +* search had already reached pass 2, whose range ends +* at @start_lcn. +*/ used_zone_pos =1 ; -if (search_zone ==1 ) + has_guess =0 ; + pass =1 ; +if (search_zone ==1 ) { zone_start = vol->mft_zone_pos; -else if (search_zone ==2 ) + zone_end = vol->mft_zone_end; + }else if (search_zone ==2 ) { zone_start = vol->data1_zone_pos; -else + zone_end = vol->nr_clusters; + }else { zone_start = vol->data2_zone_pos; + zone_end = vol->mft_zone_start; + }if (!zone_start || zone_start == vol->mft_zone_start || zone_start == vol->mft_zone_end)
Signature · 1 line
2.56.0
PATCH v2 3/3 ntfs: do not refuse fallocate() when the MFT zone is empty
In reply to Matthias Goergensntfs_fallocate() fails with -ENOSPC whenever the MFT zone has shrunk to nothing. The cluster allocator gives the MFT zone up when it cannot find free clusters elsewhere, and nothing restores it before the next mount, so after one such failure every fallocate() on the volume fails with ENOSPC, punching holes included, however much space is freed. The allocator failure fixed by the previous patch was enough to get there:
afterwards a one-cluster fallocate() failed with ENOSPC while 123 MiB of the 128 MiB volume were free.
write() and truncate() do not look at the MFT zone, and ntfs_allocate_range() already fails with -ENOSPC when too few clusters are free. Remove the check.
--- fs/ntfs/file.c | 3 --- 1 file changed, 3 deletions(-)
fs/ntfs/file.c +0 -3
@@ -1163,9 +1163,6 @@ static long ntfs_fallocate(struct file *file, int mode, loff_t offset, loff_t leif (!NVolFreeClusterKnown (vol))wait_event (vol->free_waitq,NVolFreeClusterKnown (vol)); -if ((ni->vol->mft_zone_end - ni->vol->mft_zone_start) ==0 ) -return -ENOSPC ; -if (NInoNonResident (ni) && !NInoFullyMapped (ni)) {down_write (&ni->runlist.lock); err =ntfs_attr_map_whole_runlist (ni);
Signature · 1 line
2.56.0
The whole series look good to me.
Signature · 2 lines
Thanks, Hyunchul
Namjae Jeon
maintainerRe: [PATCH v2 2/3] ntfs: restart the zone search when the allocation hint fails
In reply to Matthias GoergensQuoted 5 lines
fs/ntfs/lcnalloc.c +0
-0
fs/ntfs/lcnalloc.c struct runlist_element *ntfs_cluster_alloc(struct ntfs_volume *vol, const s64 st · line 515
Quoted 10 lines
fs/ntfs/lcnalloc.c +7
-0@@ -506,13 +506,32 @@ struct runlist_element *ntfs_cluster_alloc(struct ntfs_volume *vol, const s64 st }if (!used_zone_pos) { +/* +* The run at @start_lcn reached the end of the buffer +* or the zone. A contiguous request gets that run +* alone, not a second one from the zone position. +*/ +if (is_contig && rlpos) + goto out;
Could is_contig request still return noncontiguous runs after used_zone_pos becomes 1? If the fallback run reaches a bitmap-page boundary, goto next_bmp_pos can skip a fully used page and allow another run from a later page. Since ntfs_attr_map_cluster() merges all returned runs but reports only the first, could this leave the second run unzeroed during fallocate()? Should we check adjacency before setting the bitmap bit and return the existing short run when contiguity breaks?
Thanks!