[PATCH v4 11/12] ntfs: flush internal attributes before unlinking a file
From: Baolin Liu
Date: Sun Nov 15 2026 - 08:28:26 EST
From: Baolin Liu <liubaolin@xxxxxxxxxx>
Removing a file's last name clears internal attribute link counts and
can discard dirty folios while the file is still open. A later
fgetxattr() can return stale data; rename-overwrite has the same
problem.
Flush cached internal attributes before removing names. Release MFT
locks for writeback, then reacquire and rescan to catch concurrent
conversions. Return writeback errors before namespace changes. Preserve
named-stream lifetime handling.
Also make internal attribute writes and conversions synchronous once the
base inode has no links, covering the interval before cleanup clears the
attribute inode's own link count.
Fixes: af0db57d4293 ("ntfs: update inode operations")
Signed-off-by: Baolin Liu <liubaolin@xxxxxxxxxx>
---
New in v4. Fixes the preexisting dirty-attribute loss on ordinary unlink
also identified in the review.
Reproduced stale EA reads after unlink and rename-overwrite before the
fix. Tested EA preservation with 512-byte/4 KiB/8 KiB clusters, rmdir,
directory replacement, hard links, post-unlink EA updates and reclamation.
Injected EA block-write EIO returned before names or link counts changed;
rewrite/retry passed. Concurrent EA reads/writes, fsync and unlink/rename
passed with four CPUs, NTFS debug and lockdep.
fs/ntfs/attrib.c | 2 +-
fs/ntfs/inode.c | 6 +++--
fs/ntfs/namei.c | 67 +++++++++++++++++++++++++++++++++++++++++++++---
3 files changed, 68 insertions(+), 7 deletions(-)
diff --git a/fs/ntfs/attrib.c b/fs/ntfs/attrib.c
index af044ea895fc..29eae1eb3d02 100644
--- a/fs/ntfs/attrib.c
+++ b/fs/ntfs/attrib.c
@@ -2064,7 +2064,7 @@ static int __ntfs_attr_make_non_resident(struct ntfs_inode *ni, const u32 data_s
base_ni = ni;
else
base_ni = ni->ext.base_ntfs_ino;
- sync = NInoAttr(ni) && !vi->i_nlink &&
+ sync = NInoAttr(ni) && (!vi->i_nlink || !VFS_I(base_ni)->i_nlink) &&
!ntfs_inode_is_named_stream(ni);
m = map_mft_record(base_ni);
if (IS_ERR(m)) {
diff --git a/fs/ntfs/inode.c b/fs/ntfs/inode.c
index 4a4d7d36ad34..fb2f08265cc1 100644
--- a/fs/ntfs/inode.c
+++ b/fs/ntfs/inode.c
@@ -4093,9 +4093,11 @@ s64 ntfs_inode_attr_pwrite(struct inode *vi, s64 pos, s64 count, u8 *buf, bool s
/*
* A zero-link attribute inode is evicted at the final iput(), even
* if its base file has since been linked. Write its data before
- * eviction can discard the dirty pages.
+ * eviction can discard the dirty pages. Also cover the interval after
+ * unlink clears the base inode's link count but before attribute cleanup.
*/
- if (!vi->i_nlink && !ntfs_inode_is_named_stream(ni))
+ if ((!vi->i_nlink || !VFS_I(base_ni)->i_nlink) &&
+ !ntfs_inode_is_named_stream(ni))
sync = true;
if (ntfs_inode_is_named_stream(ni)) {
diff --git a/fs/ntfs/namei.c b/fs/ntfs/namei.c
index 3eb2d57bef1b..1b4d1e02e183 100644
--- a/fs/ntfs/namei.c
+++ b/fs/ntfs/namei.c
@@ -911,6 +911,58 @@ static int ntfs_test_inode_attr(struct inode *vi, void *data)
return 0;
}
+static int ntfs_match_dirty_attr(struct inode *vi, u64 mft_no, void *data)
+{
+ struct ntfs_inode *ni = NTFS_I(vi);
+ int found = 0;
+
+ if (ni->mft_no != mft_no || !NInoAttr(ni) ||
+ ntfs_inode_is_named_stream(ni))
+ return 0;
+
+ spin_lock(&vi->i_lock);
+ if (!(inode_state_read(vi) & (I_NEW | I_CREATING | I_FREEING | I_WILL_FREE)) &&
+ (mapping_tagged(vi->i_mapping, PAGECACHE_TAG_DIRTY) ||
+ mapping_tagged(vi->i_mapping, PAGECACHE_TAG_WRITEBACK))) {
+ __iget(vi);
+ found = 1;
+ }
+ spin_unlock(&vi->i_lock);
+ return found;
+}
+
+/*
+ * Return with the target's mrec_lock held and its internal attributes clean.
+ * Rename also needs the source locked first. Drop both locks for writeback,
+ * then rescan under the locks to catch concurrent attribute conversions.
+ * Directory mrec_locks must not be held by the caller.
+ */
+static int ntfs_lock_unlink_inodes(struct ntfs_inode *ni,
+ struct ntfs_inode *source_ni)
+{
+ struct inode *attr_vi;
+ int err;
+
+ for (;;) {
+ if (source_ni)
+ mutex_lock_nested(&source_ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL);
+ mutex_lock_nested(&ni->mrec_lock, source_ni ?
+ NTFS_INODE_MUTEX_NORMAL_2 : NTFS_INODE_MUTEX_NORMAL);
+ attr_vi = find_inode_nowait(VFS_I(ni)->i_sb, ni->mft_no,
+ ntfs_match_dirty_attr, NULL);
+ if (!attr_vi)
+ return 0;
+ mutex_unlock(&ni->mrec_lock);
+ if (source_ni)
+ mutex_unlock(&source_ni->mrec_lock);
+
+ err = filemap_write_and_wait(attr_vi->i_mapping);
+ iput(attr_vi);
+ if (err)
+ return err;
+ }
+}
+
static void ntfs_cleanup_deleted_inode(struct ntfs_inode *ni)
{
struct inode *attr_vi;
@@ -961,7 +1013,9 @@ static int ntfs_delete(struct ntfs_inode *ni, struct ntfs_inode *dir_ni,
ntfs_debug("Entering.\n");
if (need_lock == true) {
- mutex_lock_nested(&ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL);
+ err = ntfs_lock_unlink_inodes(ni, NULL);
+ if (err)
+ return err;
mutex_lock_nested(&dir_ni->mrec_lock, NTFS_INODE_MUTEX_PARENT);
}
@@ -1454,9 +1508,13 @@ static int ntfs_rename(const struct mnt_idmap *idmap, struct inode *old_dir,
ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY);
- mutex_lock_nested(&old_ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL);
- if (new_ni)
- mutex_lock_nested(&new_ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL_2);
+ if (new_ni) {
+ err = ntfs_lock_unlink_inodes(new_ni, old_ni);
+ if (err)
+ goto out_names;
+ } else {
+ mutex_lock_nested(&old_ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL);
+ }
if (old_dir == new_dir) {
mutex_lock_nested(&old_dir_ni->mrec_lock, NTFS_INODE_MUTEX_PARENT);
@@ -1540,6 +1598,7 @@ static int ntfs_rename(const struct mnt_idmap *idmap, struct inode *old_dir,
mutex_unlock(&old_ni->mrec_lock);
if (new_ni)
ntfs_cleanup_deleted_inode(new_ni);
+out_names:
if (uname_new)
kmem_cache_free(ntfs_name_cache, uname_new);
if (uname_old)