Re: [PATCH 2/6] ntfs: roll back the size update when the attribute lookup fails

From: liubaolin

Date: Wed Nov 25 2026 - 09:06:58 EST




在 2026/11/25 15:35, Hongling Zeng 写道:
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);

Hi Hongling,
Thanks for sending this patch series.

Could this rollback deadlock with writeback?
ntfs_inode_attr_pwrite() holds mrec_ni->mrec_lock when calling
ntfs_enlarge_attribute(). For a non-resident attribute, the rollback
can reach ntfs_non_resident_attr_shrink() and truncate_inode_pages(),
which may wait for a folio lock.
Meanwhile, iomap writeback can hold that folio lock and wait for the
same mrec_lock in ntfs_write_iomap_begin_non_resident():
rollback: mrec_lock -> folio lock
writeback: folio lock -> mrec_lock

The folio containing the old EOF may already be dirty from an earlier
write, so this does not require the failed write to have copied any
data.
Could we avoid waiting for folios while holding mrec_lock, without
allowing concurrent writes to interfere with the rollback?

Thanks,
Baolin.


+ 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;
}
}