[PATCH v7 09/31] ntfs: report iomap metadata corruption

From: Xuewen Wang

Date: Fri Dec 04 2026 - 02:52:01 EST


From: Xuewen Wang <wangxuewen@xxxxxxxxxx>

The iomap paths detect missing required attributes and invalid runs
without reporting them. Report these conditions as EFSCORRUPTED while
preserving allocation and underlying mapping errors.

Keep sparse holes, ordinary EOF and partial-I/O semantics distinct from
corruption. The preceding read_folio fix preserves synchronous errors.

Reviewed-by: Baolin Liu <liubaolin@xxxxxxxxxx>
Signed-off-by: Xuewen Wang <wangxuewen@xxxxxxxxxx>
---
Changes in v7:
Rebase onto ntfs-next 660cb7022966; retain the v6 logical change.
Preserve the upstream hole mapping at and beyond the runlist end.

fs/ntfs/iomap.c | 56 ++++++++++++++++++++++++++++++++++++++-----------
1 file changed, 44 insertions(+), 12 deletions(-)

diff --git a/fs/ntfs/iomap.c b/fs/ntfs/iomap.c
index 8124c5f62700..dfdac7d5a5a4 100644
--- a/fs/ntfs/iomap.c
+++ b/fs/ntfs/iomap.c
@@ -102,8 +102,13 @@ static int ntfs_read_iomap_begin_resident(struct inode *inode, loff_t offset, lo

err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
CASE_SENSITIVE, 0, NULL, 0, ctx);
- if (unlikely(err))
+ if (unlikely(err)) {
+ if (err == -ENOENT) {
+ err = -EFSCORRUPTED;
+ ntfs_report_file_metadata_error(inode, -EFSCORRUPTED);
+ }
goto out;
+ }

attr_len = le32_to_cpu(ctx->attr->data.resident.value_length);
if (unlikely(attr_len > ni->initialized_size))
@@ -241,11 +246,12 @@ static int ntfs_read_iomap_begin_non_resident(struct inode *inode, loff_t offset
rl_length = ntfs_cluster_to_bytes(vol, rl->length - (vcn - rl->vcn));

if (rl_length == 0 && rl->lcn > LCN_DELALLOC) {
+ ntfs_report_file_metadata_error(inode, -EFSCORRUPTED);
ntfs_error(inode->i_sb,
"runlist(vcn : %lld, length : %lld, lcn : %lld) is corrupted\n",
rl->vcn, rl->length, rl->lcn);
up_write(&ni->runlist.lock);
- return -EIO;
+ return -EFSCORRUPTED;
}

/*
@@ -380,6 +386,7 @@ static int ntfs_zero_range(struct inode *inode, loff_t offset, loff_t length)
static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_t offset,
loff_t length, struct iomap *iomap)
{
+ int report_err = 0;
struct ntfs_inode *ni = NTFS_I(inode);
struct ntfs_inode *mrec_ni = ntfs_inode_is_named_stream(ni) ?
ntfs_base_inode(ni) : ni;
@@ -402,7 +409,7 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_
err = ntfs_map_runlist(ni, vcn);
if (err) {
mutex_unlock(&mrec_ni->mrec_lock);
- return -ENOENT;
+ goto out;
}
down_read(&ni->runlist.lock);
rl = ni->runlist.rl;
@@ -414,9 +421,18 @@ remap_rl:
/* Seek to element containing target vcn. */
rl = __ntfs_attr_find_vcn_nolock(&ni->runlist, vcn);
if (IS_ERR(rl)) {
+ unsigned long irqflags;
+ s64 allocated_size;
+
+ read_lock_irqsave(&ni->size_lock, irqflags);
+ allocated_size = ni->allocated_size;
+ read_unlock_irqrestore(&ni->size_lock, irqflags);
+ if (offset < allocated_size)
+ report_err = -EFSCORRUPTED;
up_write(&ni->runlist.lock);
mutex_unlock(&mrec_ni->mrec_lock);
- return -EIO;
+ err = offset < allocated_size ? -EFSCORRUPTED : -EIO;
+ goto out;
}
lcn = ntfs_rl_vcn_to_lcn(rl, vcn);

@@ -435,19 +451,24 @@ remap_rl:
* extent over clusters that are allocated on disk but unknown to us.
*/
if (lcn == LCN_RL_NOT_MAPPED) {
+ if (!map_err || map_err == -ENOENT)
+ report_err = -EFSCORRUPTED;
up_write(&ni->runlist.lock);
mutex_unlock(&mrec_ni->mrec_lock);
- return map_err == -ENOMEM ? -ENOMEM : -EIO;
+ err = !map_err || map_err == -ENOENT ? -EFSCORRUPTED : map_err;
+ goto out;
}

max_clu_count = min(max_clu_count, rl->length - (vcn - rl->vcn));
if (max_clu_count == 0) {
+ report_err = -EFSCORRUPTED;
ntfs_error(inode->i_sb,
"runlist(vcn : %lld, length : %lld) is corrupted\n",
rl->vcn, rl->length);
up_write(&ni->runlist.lock);
mutex_unlock(&mrec_ni->mrec_lock);
- return -EIO;
+ err = -EFSCORRUPTED;
+ goto out;
}

iomap->bdev = inode->i_sb->s_bdev;
@@ -461,7 +482,7 @@ remap_rl:
err = max_clu_count;
up_write(&ni->runlist.lock);
mutex_unlock(&mrec_ni->mrec_lock);
- return err;
+ goto out;
}
}

@@ -490,11 +511,16 @@ remap_rl:
rl = ntfs_runlists_merge(&ni->runlist, rlc, 0,
&new_rl_count);
if (IS_ERR(rl)) {
+ err = PTR_ERR(rl);
+ if (err != -ENOMEM) {
+ err = -EFSCORRUPTED;
+ report_err = err;
+ }
ntfs_error(vol->sb, "Failed to merge runlists");
up_write(&ni->runlist.lock);
mutex_unlock(&mrec_ni->mrec_lock);
kfree(rlc);
- return PTR_ERR(rl);
+ goto out;
}

ni->runlist.rl = rl;
@@ -549,7 +575,7 @@ remap_rl:
err = 0;
if (err) {
ntfs_release_dirty_clusters(vol, max_clu_count);
- return err;
+ goto out;
}
}
} else {
@@ -566,7 +592,11 @@ remap_rl:
iomap->length = length;
}

- return 0;
+ err = 0;
+out:
+ if (report_err)
+ ntfs_report_file_metadata_error(inode, report_err);
+ return err;
}

#define NTFS_IOMAP_FLAGS_BEGIN BIT(1)
@@ -682,8 +712,10 @@ static int ntfs_write_iomap_begin_resident(struct inode *inode, loff_t offset,
err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
CASE_SENSITIVE, 0, NULL, 0, ctx);
if (err) {
- if (err == -ENOENT)
- err = -EIO;
+ if (err == -ENOENT) {
+ err = -EFSCORRUPTED;
+ ntfs_report_file_metadata_error(inode, -EFSCORRUPTED);
+ }
goto out;
}

--
2.51.0