[PATCH v6 15/37] ntfs: report iomap metadata corruption
From: Xuewen Wang
Date: Wed Nov 18 2026 - 04:09:05 EST
From: Dandan Wu <wudandan@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: Dandan Wu <wudandan@xxxxxxxxxx>
---
Changes in v6:
Keep the reporting changes here; buffered-read error propagation is
now a preceding patch. Adapt to the current iomap callback interface.
Validation:
- Compiled and linked the NTFS module object with W=1 at this stage.
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 ce916707f69b..438837318522 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;
}
if (rl_length && length > rl_length - vcn_ofs)
@@ -375,6 +381,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;
@@ -397,7 +404,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;
@@ -409,9 +416,18 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_
/* 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);
@@ -430,19 +446,24 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_
* 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;
@@ -456,7 +477,7 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_
err = max_clu_count;
up_write(&ni->runlist.lock);
mutex_unlock(&mrec_ni->mrec_lock);
- return err;
+ goto out;
}
}
@@ -485,11 +506,16 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_
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;
@@ -544,7 +570,7 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_
err = 0;
if (err) {
ntfs_release_dirty_clusters(vol, max_clu_count);
- return err;
+ goto out;
}
}
} else {
@@ -561,7 +587,11 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_
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)
@@ -677,8 +707,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