[PATCH v7 22/31] ntfs: report namespace and file recovery errors

From: Xuewen Wang

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


From: Xuewen Wang <wangxuewen@xxxxxxxxxx>

Report stale directory references, missing FILE_NAME counterparts and
missing index entries during deletion. Return EFSCORRUPTED from the
affected failing lookups; ordinary missing names remain valid results.

Report independent cleanup failures during create, link, rename, file
size restoration and preallocation trimming. Use directory attribution
where appropriate and volume reports during failed creation.
Only negative reparse-index cleanup results are errors; a positive
returned data length means cleanup succeeded.

Report missing required parents and attributes during file fsync while
retaining its best-effort lookup handling.

Include failures to clean up EA, reparse and attribute-list metadata
after creation fails. Report I/O errors while flushing internal attributes
before unlink, after releasing the inode locks and attribute reference.

Keep the upstream short-write recovery implementation. Report failed size
restoration after releasing the invalidate lock, without repeating
corruption already reported by the size setters or truncate path.

Reviewed-by: Baolin Liu <liubaolin@xxxxxxxxxx>
Signed-off-by: Xuewen Wang <wangxuewen@xxxxxxxxxx>
---
Changes in v7:
Adapt creation cleanup for the new EA, reparse and attribute-list
paths. Add volume reports after unlocking for these cleanup failures.
Report EIO from internal-attribute writeback before unlink.
Adapt EA creation cleanup to the maintainer-merged tri-state lookup;
keep errno diagnostics and report after cleanup and unlocking.
Move size-restoration reports into the upstream short-write cleanup path.
Report after unlocking and avoid repeating lower corruption reports. Keep
notifications for resource and I/O failures during recovery.

fs/ntfs/file.c | 25 ++++++++-
fs/ntfs/namei.c | 143 +++++++++++++++++++++++++++++++++---------------
2 files changed, 122 insertions(+), 46 deletions(-)

diff --git a/fs/ntfs/file.c b/fs/ntfs/file.c
index 31ff35176e05..f659af0b4b6e 100644
--- a/fs/ntfs/file.c
+++ b/fs/ntfs/file.c
@@ -86,6 +86,7 @@ int ntfs_file_open(struct inode *vi, struct file *filp)
*/
static int ntfs_trim_prealloc(struct inode *vi)
{
+ int report_err = 0;
struct ntfs_inode *ni = NTFS_I(vi);
struct ntfs_inode *mrec_ni = ntfs_base_inode(ni);
struct ntfs_volume *vol = ni->vol;
@@ -118,14 +119,17 @@ static int ntfs_trim_prealloc(struct inode *vi)
if (err) {
kvfree(ni->runlist.rl);
ni->runlist.rl = NULL;
+ report_err = err;
ntfs_error(vol->sb, "Preallocated block rollback failed");
} else {
ni->allocated_size = ntfs_cluster_to_bytes(vol, vcn_tr);
err = ntfs_attr_update_mapping_pairs_locked(
ni, 0, ni);
- if (err)
+ if (err) {
+ report_err = err;
ntfs_error(vol->sb,
"Failed to rollback mapping pairs for prealloc");
+ }
}
}

@@ -134,6 +138,8 @@ out_unlock:
mutex_unlock(&mrec_ni->mrec_lock);
inode_unlock(vi);

+ if (report_err)
+ ntfs_report_file_metadata_error(vi, report_err);
return err;
}

@@ -342,8 +348,13 @@ int ntfs_file_fsync(struct file *filp, loff_t start, loff_t end,
continue;

parent_vi = ntfs_iget(vi->i_sb, MREF_LE(fn->parent_directory));
- if (IS_ERR(parent_vi))
+ if (IS_ERR(parent_vi)) {
+ if (PTR_ERR(parent_vi) == -ENOENT &&
+ !NInoBeingDeleted(ni))
+ ntfs_report_file_metadata_error(vi,
+ -EFSCORRUPTED);
continue;
+ }
mutex_lock_nested(&NTFS_I(parent_vi)->mrec_lock, NTFS_INODE_MUTEX_NORMAL);
ia_vi = ntfs_index_iget(parent_vi, I30, 4);
mutex_unlock(&NTFS_I(parent_vi)->mrec_lock);
@@ -374,6 +385,11 @@ int ntfs_file_fsync(struct file *filp, loff_t start, loff_t end,
attr_vi = ntfs_attr_iget(vi, ctx->attr->type,
name, ctx->attr->name_length);
if (IS_ERR(attr_vi)) {
+ if (PTR_ERR(attr_vi) == -ENOENT) {
+ ntfs_report_file_metadata_error(vi,
+ -EFSCORRUPTED);
+ attr_vi = ERR_PTR(-EFSCORRUPTED);
+ }
kfree(attr);
continue;
}
@@ -1047,6 +1063,11 @@ static void ntfs_write_rollback(struct kiocb *iocb, ssize_t written,
}

filemap_invalidate_unlock(vi->i_mapping);
+ /* The size setters and truncate path already report corruption. */
+ if (init_err && init_err != -EFSCORRUPTED)
+ ntfs_report_file_metadata_error(vi, init_err);
+ if (data_err && data_err != -EFSCORRUPTED)
+ ntfs_report_file_metadata_error(vi, data_err);
}

ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
diff --git a/fs/ntfs/namei.c b/fs/ntfs/namei.c
index ca2d3baefcfd..f54c3d0ba2b1 100644
--- a/fs/ntfs/namei.c
+++ b/fs/ntfs/namei.c
@@ -236,14 +236,21 @@ static struct dentry *ntfs_lookup(struct inode *dir_ino, struct dentry *dent,
goto handle_name;
}
ntfs_error(vol->sb,
- "Found stale reference to inode 0x%lx (reference sequence number = 0x%x, inode sequence number = 0x%x), returning -EIO. Run chkdsk.",
+ "Found stale reference to inode 0x%lx (reference sequence number = 0x%x, inode sequence number = 0x%x), returning -EFSCORRUPTED. Run chkdsk.",
dent_ino, MSEQNO(mref),
NTFS_I(dent_inode)->seq_no);
+ ntfs_report_file_metadata_error(dir_ino, -EFSCORRUPTED);
iput(dent_inode);
- dent_inode = ERR_PTR(-EIO);
- } else
+ dent_inode = ERR_PTR(-EFSCORRUPTED);
+ } else {
+ if (PTR_ERR(dent_inode) == -ENOENT) {
+ ntfs_report_file_metadata_error(dir_ino,
+ -EFSCORRUPTED);
+ dent_inode = ERR_PTR(-EFSCORRUPTED);
+ }
ntfs_error(vol->sb, "ntfs_iget(0x%lx) failed with error code %li.",
dent_ino, PTR_ERR(dent_inode));
+ }
kfree(name);
/* Return the error code. */
return ERR_CAST(dent_inode);
@@ -303,8 +310,11 @@ handle_name:
if (unlikely(err)) {
ntfs_error(vol->sb,
"Inode corrupt: No WIN32 namespace counterpart to DOS file name. Run chkdsk.");
- if (err == -ENOENT)
- err = -EIO;
+ if (err == -ENOENT) {
+ err = -EFSCORRUPTED;
+ ntfs_report_file_metadata_error(dent_inode,
+ -EFSCORRUPTED);
+ }
goto err_out;
}
/* Consistency checks. */
@@ -339,7 +349,8 @@ handle_name:

eio_err_out:
ntfs_error(vol->sb, "Illegal file name attribute. Run chkdsk.");
- err = -EIO;
+ err = -EFSCORRUPTED;
+ ntfs_report_file_metadata_error(dent_inode, -EFSCORRUPTED);
err_out:
if (ctx)
ntfs_attr_put_search_ctx(ctx);
@@ -424,6 +435,7 @@ static struct ntfs_inode *__ntfs_create(const struct mnt_idmap *idmap, struct in
struct ntfs_inode *ni;
bool rollback_data = false, rollback_sd = false, rollback_reparse = false;
bool rollback_ea = false;
+ int ea_err = 0, eai_err = 0, rp_err = 0, al_err = 0;
struct file_name_attr *fn = NULL;
struct standard_information *si = NULL;
int err = 0, fn_len, si_len;
@@ -532,7 +544,7 @@ static struct ntfs_inode *__ntfs_create(const struct mnt_idmap *idmap, struct in
if (IS_ERR(dni_mrec)) {
ntfs_error(dir_ni->vol->sb, "failed to map mft record for file 0x%llx.\n",
dir_ni->mft_no);
- err = -EIO;
+ err = PTR_ERR(dni_mrec);
goto err_out;
}
parent_mft_ref = MK_LE_MREF(dir_ni->mft_no,
@@ -745,25 +757,22 @@ done:

err_out:
if (rollback_ea) {
- int rollback_err;
-
- rollback_err = ntfs_attr_exist(ni, AT_EA, AT_UNNAMED, 0);
- if (rollback_err > 0)
- rollback_err = ntfs_attr_remove(ni, AT_EA, AT_UNNAMED, 0,
- &rollback_ea_vi);
- if (rollback_err < 0)
+ ea_err = ntfs_attr_exist(ni, AT_EA, AT_UNNAMED, 0);
+ if (ea_err > 0)
+ ea_err = ntfs_attr_remove(ni, AT_EA, AT_UNNAMED, 0,
+ &rollback_ea_vi);
+ if (ea_err < 0)
ntfs_error(sb, "Failed to roll back EA attribute: %d",
- rollback_err);
-
- rollback_err = ntfs_attr_exist(ni, AT_EA_INFORMATION,
- AT_UNNAMED, 0);
- if (rollback_err > 0)
- rollback_err = ntfs_attr_remove(ni, AT_EA_INFORMATION,
- AT_UNNAMED, 0,
- &rollback_eai_vi);
- if (rollback_err < 0)
+ ea_err);
+
+ eai_err = ntfs_attr_exist(ni, AT_EA_INFORMATION,
+ AT_UNNAMED, 0);
+ if (eai_err > 0)
+ eai_err = ntfs_attr_remove(ni, AT_EA_INFORMATION,
+ AT_UNNAMED, 0, &rollback_eai_vi);
+ if (eai_err < 0)
ntfs_error(sb, "Failed to roll back EA_INFORMATION attribute: %d",
- rollback_err);
+ eai_err);
}
if (rollback_sd) {
int rollback_err;
@@ -771,9 +780,11 @@ err_out:
rollback_err = ntfs_attr_remove(ni,
AT_SECURITY_DESCRIPTOR, AT_UNNAMED, 0,
&rollback_sd_vi);
- if (rollback_err)
+ if (rollback_err) {
+ ntfs_report_metadata_error(ni->vol, rollback_err);
ntfs_error(sb,
"Failed to roll back security descriptor.\n");
+ }
}

if (rollback_data) {
@@ -781,21 +792,30 @@ err_out:

rollback_err = ntfs_attr_remove(ni, AT_DATA, AT_UNNAMED,
0, &rollback_data_vi);
- if (rollback_err)
+ if (rollback_err) {
+ ntfs_report_metadata_error(ni->vol, rollback_err);
ntfs_error(sb, "Failed to roll back DATA attribute.\n");
+ }
}

if (rollback_reparse) {
- if (ntfs_delete_reparse_index(ni) < 0)
+ int rollback_err = ntfs_delete_reparse_index(ni);
+
+ if (rollback_err < 0) {
+ ntfs_report_metadata_error(ni->vol, rollback_err);
ntfs_error(sb, "Failed to roll back reparse index entry.");
- if (ntfs_attr_remove(ni, AT_REPARSE_POINT, AT_UNNAMED, 0,
- &rollback_rp_vi))
+ }
+ rp_err = ntfs_attr_remove(ni, AT_REPARSE_POINT, AT_UNNAMED, 0,
+ &rollback_rp_vi);
+ if (rp_err)
ntfs_error(sb, "Failed to roll back REPARSE_POINT attribute.");
}
- if (NInoAttrList(ni) &&
- ntfs_attr_remove(ni, AT_ATTRIBUTE_LIST, AT_UNNAMED, 0,
- &rollback_al_vi))
- ntfs_error(sb, "Failed to roll back ATTRIBUTE_LIST attribute.");
+ if (NInoAttrList(ni)) {
+ al_err = ntfs_attr_remove(ni, AT_ATTRIBUTE_LIST, AT_UNNAMED, 0,
+ &rollback_al_vi);
+ if (al_err)
+ ntfs_error(sb, "Failed to roll back ATTRIBUTE_LIST attribute.");
+ }
/*
* Free extent MFT records (should not exist any with current
* ntfs_create implementation, but for any case if something will be
@@ -805,14 +825,22 @@ err_out:
int err2;

err2 = ntfs_mft_record_free(ni->vol, *(ni->ext.extent_ntfs_inos));
- if (err2)
+ if (err2) {
+ ntfs_report_metadata_error(ni->vol, err2);
ntfs_error(sb,
"Failed to free extent MFT record. Leaving inconsistent metadata.\n");
+ }
ntfs_inode_close(*(ni->ext.extent_ntfs_inos));
}
- if (ntfs_mft_record_free(ni->vol, ni))
- ntfs_error(sb,
- "Failed to free MFT record. Leaving inconsistent metadata. Run chkdsk.\n");
+ {
+ int free_err = ntfs_mft_record_free(ni->vol, ni);
+
+ if (free_err) {
+ ntfs_report_metadata_error(ni->vol, free_err);
+ ntfs_error(sb,
+ "Failed to free MFT record. Leaving inconsistent metadata. Run chkdsk.\n");
+ }
+ }
unmap_mft_record(ni);
kfree(fn);
kfree(si);
@@ -820,6 +848,16 @@ err_out:
mutex_unlock(&dir_ni->mrec_lock);
mutex_unlock(&ni->mrec_lock);

+ /* These failures leave creation cleanup incomplete. */
+ if (ea_err < 0)
+ ntfs_report_metadata_error(vol, ea_err);
+ if (eai_err < 0)
+ ntfs_report_metadata_error(vol, eai_err);
+ if (rp_err)
+ ntfs_report_metadata_error(vol, rp_err);
+ if (al_err)
+ ntfs_report_metadata_error(vol, al_err);
+
/* The MFT record has already been freed above. */
set_nlink(vi, 1);
if (rollback_ea_vi)
@@ -1012,6 +1050,8 @@ static int ntfs_lock_unlink_inodes(struct ntfs_inode *ni,

err = filemap_write_and_wait(attr_vi->i_mapping);
iput(attr_vi);
+ if (err == -EIO)
+ ntfs_report_file_metadata_error(VFS_I(ni), err);
if (err)
return err;
}
@@ -1157,6 +1197,11 @@ search:
ntfs_attr_reinit_search_ctx(actx);
goto search;
}
+ if (err == -ENOENT) {
+ err = -EFSCORRUPTED;
+ ntfs_report_file_metadata_error(VFS_I(ni),
+ -EFSCORRUPTED);
+ }
goto err_out;
}

@@ -1165,8 +1210,14 @@ search:
goto err_out;

err = ntfs_index_remove(dir_ni, fn, le32_to_cpu(actx->attr->data.resident.value_length));
- if (err)
+ if (err) {
+ if (err == -ENOENT) {
+ err = -EFSCORRUPTED;
+ ntfs_report_file_metadata_error(VFS_I(dir_ni),
+ -EFSCORRUPTED);
+ }
goto err_out;
+ }

err = ntfs_attr_record_rm(actx);
if (err)
@@ -1379,7 +1430,7 @@ static int __ntfs_link(struct ntfs_inode *ni, struct ntfs_inode *dir_ni,
struct super_block *sb;
struct inode *vi = VFS_I(ni);
struct file_name_attr *fn = NULL;
- int fn_len, err = 0;
+ int fn_len, rollback_err, err = 0;
struct mft_record *dir_mrec = NULL, *ni_mrec = NULL;

ntfs_debug("Entering.\n");
@@ -1390,7 +1441,7 @@ static int __ntfs_link(struct ntfs_inode *ni, struct ntfs_inode *dir_ni,

ni_mrec = map_mft_record(ni);
if (IS_ERR(ni_mrec)) {
- err = -EIO;
+ err = PTR_ERR(ni_mrec);
goto err_out;
}

@@ -1410,7 +1461,7 @@ static int __ntfs_link(struct ntfs_inode *ni, struct ntfs_inode *dir_ni,

dir_mrec = map_mft_record(dir_ni);
if (IS_ERR(dir_mrec)) {
- err = -EIO;
+ err = PTR_ERR(dir_mrec);
goto err_out;
}

@@ -1470,7 +1521,8 @@ static int __ntfs_link(struct ntfs_inode *ni, struct ntfs_inode *dir_ni,
if (err) {
ntfs_error(sb, "Failed to add FILE_NAME attribute.\n");
/* Try to remove just added attribute from index. */
- if (ntfs_index_remove(dir_ni, fn, fn_len))
+ rollback_err = ntfs_index_remove(dir_ni, fn, fn_len);
+ if (rollback_err)
goto rollback_failed;
goto err_out;
}
@@ -1488,6 +1540,7 @@ static int __ntfs_link(struct ntfs_inode *ni, struct ntfs_inode *dir_ni,

return 0;
rollback_failed:
+ ntfs_report_file_metadata_error(VFS_I(dir_ni), rollback_err);
ntfs_error(sb, "Rollback failed. Leaving inconsistent metadata.\n");
err_out:
kfree(fn);
@@ -1595,7 +1648,7 @@ static int ntfs_rename(const struct mnt_idmap *idmap, struct inode *old_dir,

ni_mrec = map_mft_record(NTFS_I(new_inode));
if (IS_ERR(ni_mrec)) {
- err = -EIO;
+ err = PTR_ERR(ni_mrec);
goto err_out;
}
err = ntfs_check_empty_dir(NTFS_I(new_inode), ni_mrec);
@@ -1621,9 +1674,11 @@ static int ntfs_rename(const struct mnt_idmap *idmap, struct inode *old_dir,
ntfs_error(sb, "Failed to delete old ntfs inode(%llu) in old dir, err : %d\n",
old_ni->mft_no, err);
err2 = ntfs_delete(old_ni, new_dir_ni, uname_new, new_name_len, false);
- if (err2)
+ if (err2) {
+ ntfs_report_file_metadata_error(VFS_I(new_dir_ni), err2);
ntfs_error(sb, "Failed to delete old ntfs inode in new dir, err : %d\n",
err2);
+ }
goto err_out;
}

--
2.51.0