[PATCH 2/6] ntfs: roll back the size update when the attribute lookup fails
From: Hongling Zeng
Date: Wed Nov 25 2026 - 02:36:32 EST
ntfs_enlarge_attribute() truncates the attribute to the end of the
write before looking it up again, because the truncation can move the
attribute to a different mft record. If that lookup fails, the error
is returned but the attribute stays at the extended size, so a failed
write extends the file.
Undo the truncation before returning the error. If the rollback fails
as well, mark the volume as having errors and synchronize i_size with
the current data_size instead of the old snapshot.
Fixes: af0db57d4293 ("ntfs: update inode operations")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Hongling Zeng <zenghongling@xxxxxxxxxx>
---
fs/ntfs/inode.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++--
1 file changed, 46 insertions(+), 2 deletions(-)
diff --git a/fs/ntfs/inode.c b/fs/ntfs/inode.c
index 4fbdcd459652..7d7bb741455e 100644
--- a/fs/ntfs/inode.c
+++ b/fs/ntfs/inode.c
@@ -3842,8 +3842,19 @@ static inline int ntfs_enlarge_attribute(struct inode *vi, s64 pos, s64 count,
{
struct ntfs_inode *ni = NTFS_I(vi);
struct super_block *sb = vi->i_sb;
+ unsigned long flags;
+ s64 old_data_size;
int ret;
+ /*
+ * Snapshot the old data_size under size_lock: it is the rollback
+ * target, the other sizes are updated by ntfs_attr_truncate()
+ * itself
+ */
+ read_lock_irqsave(&ni->size_lock, flags);
+ old_data_size = ni->data_size;
+ read_unlock_irqrestore(&ni->size_lock, flags);
+
if (pos + count <= ni->initialized_size)
return 0;
@@ -3853,7 +3864,7 @@ static inline int ntfs_enlarge_attribute(struct inode *vi, s64 pos, s64 count,
if (NInoCompressed(ni))
return -EOPNOTSUPP;
- if (pos + count > ni->data_size) {
+ if (pos + count > old_data_size) {
ret = ntfs_attr_truncate(ni, pos + count);
if (ret) {
ntfs_debug("Failed to truncate attribute");
@@ -3865,7 +3876,40 @@ static inline int ntfs_enlarge_attribute(struct inode *vi, s64 pos, s64 count,
CASE_SENSITIVE,
0, NULL, 0, ctx);
if (ret) {
- ntfs_error(sb, "Failed to look up attr %#x", ni->type);
+ int rollback_err;
+
+ /*
+ * The truncation already happened, so undo it
+ * before returning the error
+ */
+ rollback_err = ntfs_attr_truncate(ni, old_data_size);
+ if (rollback_err) {
+ s64 current_data_size;
+
+ /*
+ * Rollback failed, the sizes may be
+ * inconsistent. Mark the volume as having
+ * errors and sync i_size with the current
+ * data_size
+ */
+ read_lock_irqsave(&ni->size_lock, flags);
+ current_data_size = ni->data_size;
+ read_unlock_irqrestore(&ni->size_lock, flags);
+
+ i_size_write(vi, current_data_size);
+ NVolSetErrors(ni->vol);
+ ntfs_error(sb,
+ "Failed to look up attr %#x and "
+ "roll back the size update: %d",
+ ni->type, rollback_err);
+ } else {
+ /*
+ * Rollback succeeded, the sizes were updated
+ * by ntfs_attr_truncate() itself
+ */
+ ntfs_error(sb, "Failed to look up attr %#x",
+ ni->type);
+ }
return ret;
}
}
--
2.25.1