[PATCH v7 28/31] ntfs: report named stream metadata and recovery errors

From: Xuewen Wang

Date: Fri Dec 04 2026 - 02:53:29 EST


From: Xuewen Wang <wangxuewen@xxxxxxxxxx>

Report missing or stale MFT references after a successful base-name
lookup as EFSCORRUPTED only when the caller holds the directory i_rwsem.
Pass that locking state through the helper and its callers. Unlocked
revalidation can race with unlink or record reuse and must not report
those races as corruption.

Also report missing required stream attributes and cleanup failures.
For rollback failures that trigger shutdown, report the cleanup errno
and retain the EIO operation result. Preserve the existing ENOMEM
exception, and keep ordinary missing streams silent.

Report non-root directory self-references from locked stream lookups as
corruption. Keep the root self-entry absent and unlocked revalidation
silent.

Reviewed-by: Baolin Liu <liubaolin@xxxxxxxxxx>
Signed-off-by: Xuewen Wang <wangxuewen@xxxxxxxxxx>
---
Changes in v7:
Remove unreachable signal exceptions from metadata-only rollback.
Preserve ENOMEM handling and interruptible named-stream payload I/O.
Preserve the upstream self-reference guard. Report confirmed non-root
self-references from locked lookups, while keeping the root exception and
unlocked revalidation silent.

Validation:
- QEMU: a non-root directory self-reference returns EUCLEAN and reports
once; the root self-entry returns ENOENT without an event.

fs/ntfs/named_stream.c | 56 +++++++++++++++++++++++++++++++-----------
fs/ntfs/stream.h | 2 +-
2 files changed, 42 insertions(+), 16 deletions(-)

diff --git a/fs/ntfs/named_stream.c b/fs/ntfs/named_stream.c
index 8d5c8a32741d..bf6a3e235899 100644
--- a/fs/ntfs/named_stream.c
+++ b/fs/ntfs/named_stream.c
@@ -280,6 +280,7 @@ int ntfs_stream_path_check(struct ntfs_volume *vol,
* @dir_ino: directory containing the base file or directory
* @uname: UTF-16LE base name
* @uname_len: Length of @uname in UTF-16 code units
+ * @dir_locked: caller holds the directory i_rwsem against namespace changes
*
* Resolve the directory entry and verify its MFT reference before returning
* the inode.
@@ -287,8 +288,9 @@ int ntfs_stream_path_check(struct ntfs_volume *vol,
* Return: Referenced inode on success, or ERR_PTR() on failure.
*/
struct inode *ntfs_stream_lookup_inode_by_name(struct inode *dir_ino,
- __le16 *uname, int uname_len)
+ __le16 *uname, int uname_len, bool dir_locked)
{
+ int report_err = 0;
struct ntfs_volume *vol = NTFS_SB(dir_ino->i_sb);
struct ntfs_name *name = NULL;
struct inode *inode;
@@ -304,19 +306,35 @@ struct inode *ntfs_stream_lookup_inode_by_name(struct inode *dir_ino,
return ERR_PTR(MREF_ERR(mref));

inode = ntfs_iget(vol->sb, MREF(mref));
- if (IS_ERR(inode))
- return inode;
+ if (IS_ERR(inode)) {
+ if (dir_locked && PTR_ERR(inode) == -ENOENT) {
+ report_err = -EFSCORRUPTED;
+ inode = ERR_PTR(-EFSCORRUPTED);
+ }
+ goto out;
+ }
if (MSEQNO(mref) != NTFS_I(inode)->seq_no &&
MREF(mref) != FILE_MFT) {
+ /* Unlocked dentry revalidation can race with unlink and reuse. */
+ if (dir_locked)
+ report_err = -EFSCORRUPTED;
iput(inode);
- return ERR_PTR(-EIO);
+ inode = ERR_PTR(dir_locked ? -EFSCORRUPTED : -EIO);
+ goto out;
}
if (inode == dir_ino) {
iput(inode);
- if (NTFS_I(dir_ino)->mft_no == FILE_root)
- return ERR_PTR(-ENOENT);
- return ERR_PTR(-EIO);
+ if (NTFS_I(dir_ino)->mft_no == FILE_root) {
+ inode = ERR_PTR(-ENOENT);
+ } else {
+ if (dir_locked)
+ report_err = -EFSCORRUPTED;
+ inode = ERR_PTR(dir_locked ? -EFSCORRUPTED : -EIO);
+ }
}
+out:
+ if (report_err)
+ ntfs_report_file_metadata_error(dir_ino, report_err);
return inode;
}

@@ -367,7 +385,7 @@ static int ntfs_stream_d_revalidate(struct inode *dir,
ni->vol->upcase_len))
goto out;

- base_vi = ntfs_stream_lookup_inode_by_name(dir, base_name, base_len);
+ base_vi = ntfs_stream_lookup_inode_by_name(dir, base_name, base_len, false);
if (IS_ERR(base_vi))
goto out;
if (NTFS_I(base_vi) != ni->ext.base_ntfs_ino) {
@@ -475,7 +493,7 @@ struct dentry *ntfs_lookup_stream(struct inode *dir_ino,
}

base_vi = ntfs_stream_lookup_inode_by_name(dir_ino, base_uname,
- base_len);
+ base_len, true);
kmem_cache_free(ntfs_name_cache, base_uname);
if (IS_ERR(base_vi)) {
err = PTR_ERR(base_vi);
@@ -598,7 +616,7 @@ struct inode *ntfs_create_named_stream(const struct mnt_idmap *idmap,
bool stream_created = false;
int err, rollback_err;

- base_vi = ntfs_stream_lookup_inode_by_name(dir, base_name, base_len);
+ base_vi = ntfs_stream_lookup_inode_by_name(dir, base_name, base_len, true);
if (IS_ERR(base_vi))
return base_vi;
if (!S_ISREG(base_vi->i_mode) && !S_ISDIR(base_vi->i_mode)) {
@@ -669,18 +687,21 @@ struct inode *ntfs_create_named_stream(const struct mnt_idmap *idmap,
else
err = PTR_ERR(stream_vi);
if (err) {
+ if (err == -ENOENT) {
+ err = -EFSCORRUPTED;
+ ntfs_report_file_metadata_error(base_vi, -EFSCORRUPTED);
+ }
if (stream_created) {
rollback_err = ntfs_attr_remove(base_ni, AT_DATA,
stream_name, stream_len, &rollback_vi);
if (rollback_err) {
ntfs_error(base_ni->vol->sb,
"Failed to roll back named stream creation.\n");
- if (rollback_err == -ENOMEM ||
- rollback_err == -EINTR ||
- rollback_err == -ERESTARTSYS) {
+ if (rollback_err == -ENOMEM) {
err = rollback_err;
} else {
- NVolSetErrors(base_ni->vol);
+ ntfs_report_file_metadata_error(base_vi,
+ rollback_err);
NVolSetShutdown(base_ni->vol);
err = -EIO;
}
@@ -732,7 +753,7 @@ int ntfs_unlink_named_stream(struct inode *dir, struct dentry *dentry)
if (!path_result)
return -EINVAL;

- base_vi = ntfs_stream_lookup_inode_by_name(dir, base_name, base_len);
+ base_vi = ntfs_stream_lookup_inode_by_name(dir, base_name, base_len, true);
if (IS_ERR(base_vi)) {
err = PTR_ERR(base_vi);
goto out_free;
@@ -1078,6 +1099,7 @@ static int ntfs_stream_finish_remove(struct inode *attr_vi)
remove_inode_hash(attr_vi);
} else {
NInoSetBeingDeleted(attr_ni);
+ ntfs_report_file_metadata_error(VFS_I(ni), err);
remove_inode_hash(attr_vi);
}

@@ -1315,6 +1337,10 @@ static int ntfs_stream_ioctl_get_inode(struct inode *base_vi, __le16 *sname,
attr_vi = ntfs_attr_iget(base_vi, AT_DATA, sname, sname_len);
if (IS_ERR(attr_vi)) {
err = PTR_ERR(attr_vi);
+ if (err == -ENOENT) {
+ err = -EFSCORRUPTED;
+ ntfs_report_file_metadata_error(base_vi, -EFSCORRUPTED);
+ }
attr_vi = NULL;
goto out_unlock;
}
diff --git a/fs/ntfs/stream.h b/fs/ntfs/stream.h
index 18b20e7d7ff4..2df6422d102f 100644
--- a/fs/ntfs/stream.h
+++ b/fs/ntfs/stream.h
@@ -29,7 +29,7 @@ int ntfs_stream_path_names(struct ntfs_volume *vol,
__le16 **stream_name, int *stream_len);

struct inode *ntfs_stream_lookup_inode_by_name(struct inode *dir,
- __le16 *name, int name_len);
+ __le16 *name, int name_len, bool dir_locked);
struct dentry *ntfs_lookup_stream(struct inode *dir, struct dentry *dentry,
const struct ntfs_stream_path *path);
struct inode *ntfs_create_named_stream(const struct mnt_idmap *idmap,
--
2.51.0