[PATCH v6 03/37] ntfs: handle source record removal failure during attribute moves

From: Xuewen Wang

Date: Wed Nov 18 2026 - 04:08:03 EST


From: Xuewen Wang <wangxuewen@xxxxxxxxxx>

ntfs_attr_record_move_to() ignores failure to remove the source attribute
after copying it. It can update the attribute-list reference and return
success with both copies present.

Check source removal before changing the list entry. On failure, attempt
to remove the destination copy, dirty and unmap its record, and leave the
list pointing to the source. Record both failures with the existing volume
error bit and log messages after releasing the destination search context.

Shrinking a record to zero needs no extra space, so failure indicates
inconsistent record accounting. Return EFSCORRUPTED even if cleanup fails.

Fixes: 495e90fa3348 ("ntfs: update attrib operations")

Reviewed-by: Baolin Liu <liubaolin@xxxxxxxxxx>
Signed-off-by: Xuewen Wang <wangxuewen@xxxxxxxxxx>
---
Changes in v6:
Split v5 patch 11: use only the existing volume error bit and diagnostics.
The final patch adds FAN_FS_ERROR reporting at these sites.

Validation:
- Compiled and linked the NTFS module object with W=1 at this stage.
- No undefined references to ntfs_report_* or fserror_report_*.
- Eight controlled attribute-move cases passed for this stage,
including source removal and independent destination cleanup failure.

fs/ntfs/attrib.c | 22 ++++++++++++++++++++--
1 file changed, 20 insertions(+), 2 deletions(-)

diff --git a/fs/ntfs/attrib.c b/fs/ntfs/attrib.c
index 72e0c94c878e..ad2691c3457d 100644
--- a/fs/ntfs/attrib.c
+++ b/fs/ntfs/attrib.c
@@ -3466,6 +3466,7 @@ int ntfs_attr_record_move_to(struct ntfs_attr_search_ctx *ctx, struct ntfs_inode
struct ntfs_attr_search_ctx *nctx;
struct attr_record *a;
int err;
+ int remove_err = 0, cleanup_err = 0;
u32 ic;
struct mft_record *ni_mrec;
struct super_block *sb;
@@ -3520,7 +3521,7 @@ int ntfs_attr_record_move_to(struct ntfs_attr_search_ctx *ctx, struct ntfs_inode
/* Make space and move attribute. */
ni_mrec = map_mft_record(ni);
if (IS_ERR(ni_mrec)) {
- err = -EIO;
+ err = PTR_ERR(ni_mrec);
goto put_err_out;
}

@@ -3535,7 +3536,16 @@ int ntfs_attr_record_move_to(struct ntfs_attr_search_ctx *ctx, struct ntfs_inode
nctx->attr->instance = nctx->mrec->next_attr_instance;
nctx->mrec->next_attr_instance =
cpu_to_le16((le16_to_cpu(nctx->mrec->next_attr_instance) + 1) & 0xffff);
- ntfs_attr_record_resize(ctx->mrec, a, 0);
+ err = ntfs_attr_record_resize(ctx->mrec, a, 0);
+ if (err) {
+ err = -EFSCORRUPTED;
+ remove_err = err;
+ if (ntfs_attr_record_resize(ni_mrec, nctx->attr, 0))
+ cleanup_err = -EFSCORRUPTED;
+ mark_mft_record_dirty(ni);
+ unmap_mft_record(ni);
+ goto put_err_out;
+ }
mark_mft_record_dirty(ctx->ntfs_ino);
mark_mft_record_dirty(ni);

@@ -3546,6 +3556,14 @@ int ntfs_attr_record_move_to(struct ntfs_attr_search_ctx *ctx, struct ntfs_inode
unmap_mft_record(ni);
put_err_out:
ntfs_attr_put_search_ctx(nctx);
+ if (remove_err) {
+ NVolSetErrors(ni->vol);
+ ntfs_error(sb, "Failed to remove source attribute record");
+ }
+ if (cleanup_err) {
+ NVolSetErrors(ni->vol);
+ ntfs_error(sb, "Failed to remove destination attribute copy");
+ }
return err;
}

--
2.51.0