[PATCH v6 09/37] ntfs: serialize export parent reconstruction
From: Xuewen Wang
Date: Wed Nov 18 2026 - 04:08:55 EST
From: Xuewen Wang <wangxuewen@xxxxxxxxxx>
Exportfs calls ntfs_get_parent() without holding the child's inode lock.
FILE_NAME lookup and parent loading can therefore race with rename or
rmdir, despite the function's existing locking comment.
Hold the child's mrec_lock across the lookup and parent load. Release it
on every error path and before d_obtain_alias().
Reviewed-by: Baolin Liu <liubaolin@xxxxxxxxxx>
Signed-off-by: Xuewen Wang <wangxuewen@xxxxxxxxxx>
---
Changes in v6:
Extract export-parent locking from v5 patch 24.
Validation:
- Compiled and linked the NTFS module object with W=1 at this stage.
- No undefined references to ntfs_report_* or fserror_report_*.
fs/ntfs/namei.c | 25 +++++++++++++++++++------
1 file changed, 19 insertions(+), 6 deletions(-)
diff --git a/fs/ntfs/namei.c b/fs/ntfs/namei.c
index d2d9fe732014..fb1946b9e7e3 100644
--- a/fs/ntfs/namei.c
+++ b/fs/ntfs/namei.c
@@ -1690,7 +1690,8 @@ const struct inode_operations ntfs_dir_inode_ops = {
* fs/exportfs/expfs.c::find_exported_dentry() which in turn is called from the
* default ->decode_fh() which is export_decode_fh() in the same file.
*
- * Note: ntfs_get_parent() is called with @d_inode(child_dent)->i_mutex down.
+ * Serialize the file name lookup and parent load against rename and rmdir
+ * with the child's mrec_lock; exportfs does not hold the child's inode lock.
*
* Return the dentry of the parent directory on success or the error code on
* error (IS_ERR() is true).
@@ -1698,6 +1699,7 @@ const struct inode_operations ntfs_dir_inode_ops = {
static struct dentry *ntfs_get_parent(struct dentry *child_dent)
{
struct inode *vi = d_inode(child_dent);
+ struct inode *parent;
struct ntfs_inode *ni = NTFS_I(vi);
struct mft_record *mrec;
struct ntfs_attr_search_ctx *ctx;
@@ -1707,15 +1709,19 @@ static struct dentry *ntfs_get_parent(struct dentry *child_dent)
int err;
ntfs_debug("Entering for inode 0x%llx.", ni->mft_no);
+ mutex_lock(&ni->mrec_lock);
/* Get the mft record of the inode belonging to the child dentry. */
mrec = map_mft_record(ni);
- if (IS_ERR(mrec))
- return ERR_CAST(mrec);
+ if (IS_ERR(mrec)) {
+ err = PTR_ERR(mrec);
+ goto unlock_err;
+ }
/* Find the first file name attribute in the mft record. */
ctx = ntfs_attr_get_search_ctx(ni, mrec);
if (IS_ERR(ctx)) {
unmap_mft_record(ni);
- return ERR_PTR(PTR_ERR(ctx));
+ err = PTR_ERR(ctx);
+ goto unlock_err;
}
try_next:
err = ntfs_attr_lookup(AT_FILE_NAME, NULL, 0, CASE_SENSITIVE, 0, NULL,
@@ -1727,7 +1733,7 @@ static struct dentry *ntfs_get_parent(struct dentry *child_dent)
ntfs_error(vi->i_sb,
"Inode 0x%llx does not have a file name attribute. Run chkdsk.",
ni->mft_no);
- return ERR_PTR(err);
+ goto unlock_err;
}
attr = ctx->attr;
if (unlikely(attr->non_resident))
@@ -1743,7 +1749,14 @@ static struct dentry *ntfs_get_parent(struct dentry *child_dent)
ntfs_attr_put_search_ctx(ctx);
unmap_mft_record(ni);
- return d_obtain_alias(ntfs_iget(vi->i_sb, parent_ino));
+ parent = ntfs_iget(vi->i_sb, parent_ino);
+ goto out;
+
+unlock_err:
+ parent = ERR_PTR(err);
+out:
+ mutex_unlock(&ni->mrec_lock);
+ return d_obtain_alias(parent);
}
static struct inode *ntfs_nfs_get_inode(struct super_block *sb,
--
2.51.0