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

From: Hongling Zeng

Date: Mon Nov 30 2026 - 20:33:18 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 the lookup fails, the write
fails but the attribute stays extended.

Undo the truncation before returning the error. A resident resize
shrinks the value in place without sleeping on a folio, so it is
safe under mrec_lock. A non-resident shrink has to drop the folios
beyond the old EOF, which cannot happen under mrec_lock: writeback
locks a folio and waits for mrec_lock in
ntfs_write_iomap_begin_non_resident(). Defer it to
ntfs_inode_attr_pwrite(), which releases mrec_lock and follows
ntfs_setattr_size(): truncate_setsize() outside the lock, then
__ntfs_attr_truncate_vfs() under it, after re-checking that no
concurrent write took over the extension. If the rollback fails,
mark the volume and sync i_size with data_size.

Only the named streams can drop the folios in the deferred rollback:
the stream ioctl holds the mapping invalidate lock across the write.
The internal attributes reach it under the base mrec_lock their
callers hold, and the folio locks would invert against it, since the
writeback and the resident readers take mrec_lock with the folio
locked. Their readers and writers serialize on the base mrec_lock,
their mappings stay kernel only, and the failed write dirtied no
folio, so the folios beyond the old EOF are left in place and i_size
is synced by hand.

Recompute the modified flag after the deferred rollback, so a failed
write the rollback restored completely does not update the base file
times of a named stream.

Fixes: af0db57d4293 ("ntfs: update inode operations")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Hongling Zeng <zenghongling@xxxxxxxxxx>
Reviewed-by: Baolin Liu <liubaolin@xxxxxxxxxx>
---
fs/ntfs/inode.c | 132 ++++++++++++++++++++++++++++++++++++++++++++++--
1 file changed, 128 insertions(+), 4 deletions(-)

diff --git a/fs/ntfs/inode.c b/fs/ntfs/inode.c
index 3aac1d7d310f..f317a4ec8eb6 100644
--- a/fs/ntfs/inode.c
+++ b/fs/ntfs/inode.c
@@ -3849,12 +3849,24 @@ s64 ntfs_inode_attr_pread(struct inode *vi, s64 pos, s64 count, u8 *buf)
}

static inline int ntfs_enlarge_attribute(struct inode *vi, s64 pos, s64 count,
- struct ntfs_attr_search_ctx *ctx)
+ struct ntfs_attr_search_ctx *ctx,
+ bool *deferred_rollback)
{
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;

@@ -3864,7 +3876,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");
@@ -3876,7 +3888,52 @@ 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;
+
+ /*
+ * Undo the truncation: a resident resize never
+ * sleeps on a folio under mrec_lock, even the
+ * conversion to non-resident uses FGP_NOWAIT.
+ */
+ if (NInoNonResident(ni)) {
+ /*
+ * A non-resident shrink has to drop the
+ * folios beyond the old EOF, which cannot
+ * happen under mrec_lock: writeback locks
+ * a folio and waits for mrec_lock. Let the
+ * caller roll back with the lock released.
+ */
+ *deferred_rollback = true;
+ ntfs_error(sb, "Failed to look up attr %#x",
+ ni->type);
+ return ret;
+ }
+ rollback_err = ntfs_attr_truncate(ni, old_data_size);
+ if (rollback_err) {
+ s64 current_data_size;
+
+ /*
+ * The sizes may be inconsistent now.
+ * Mark the volume and sync i_size with
+ * 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: %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;
}
}
@@ -4096,6 +4153,61 @@ static s64 __ntfs_inode_non_resident_attr_pwrite(struct inode *vi,
return ret ? ret : written;
}

+/*
+ * ntfs_inode_rollback_enlarge - undo the size update that
+ * ntfs_enlarge_attribute() deferred
+ *
+ * truncate_setsize() drops the folios beyond the old EOF, then
+ * __ntfs_attr_truncate_vfs() shrinks the attribute under mrec_lock.
+ * Only the stream ioctl holds the invalidate lock this needs: the
+ * internal attributes run under the base mrec_lock their callers
+ * hold, which the folio locks would invert against, so they keep
+ * their folios and sync i_size by hand - their readers and writers
+ * serialize on the base mrec_lock.
+ */
+static void ntfs_inode_rollback_enlarge(struct inode *vi,
+ struct ntfs_inode *mrec_ni, s64 new_data_size,
+ s64 old_data_size)
+{
+ struct ntfs_inode *ni = NTFS_I(vi);
+ bool stream = ntfs_inode_is_named_stream(ni);
+ unsigned long flags;
+ s64 current_data_size;
+ int rollback_err = 0;
+
+ if (stream)
+ truncate_setsize(vi, old_data_size);
+
+ mutex_lock(&mrec_ni->mrec_lock);
+ read_lock_irqsave(&ni->size_lock, flags);
+ current_data_size = ni->data_size;
+ read_unlock_irqrestore(&ni->size_lock, flags);
+ if (current_data_size == new_data_size)
+ rollback_err = __ntfs_attr_truncate_vfs(ni, old_data_size,
+ old_data_size);
+ mutex_unlock(&mrec_ni->mrec_lock);
+ if (rollback_err) {
+ /*
+ * The sizes may be inconsistent now.
+ * Mark the volume and sync i_size with
+ * 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(vi->i_sb, "Failed to roll back the size update: %d",
+ rollback_err);
+ } else if (current_data_size != new_data_size) {
+ /* A concurrent write took over the extension. */
+ i_size_write(vi, current_data_size);
+ } else if (!stream) {
+ /* No folio was dropped. */
+ i_size_write(vi, old_data_size);
+ }
+}
+
s64 ntfs_inode_attr_pwrite(struct inode *vi, s64 pos, s64 count, u8 *buf, bool sync)
{
struct ntfs_inode *ni = NTFS_I(vi);
@@ -4104,8 +4216,10 @@ s64 ntfs_inode_attr_pwrite(struct inode *vi, s64 pos, s64 count, u8 *buf, bool s
base_ni : ni;
struct ntfs_attr_search_ctx *ctx;
loff_t old_data_size, old_init_size;
+ unsigned long flags;
s64 ret;
bool modified = false;
+ bool deferred_rollback = false;

WARN_ON(!NInoAttr(ni));

@@ -4152,7 +4266,8 @@ s64 ntfs_inode_attr_pwrite(struct inode *vi, s64 pos, s64 count, u8 *buf, bool s

old_data_size = ni->data_size;
old_init_size = ni->initialized_size;
- ret = ntfs_enlarge_attribute(vi, pos, count, ctx);
+ ret = ntfs_enlarge_attribute(vi, pos, count, ctx,
+ &deferred_rollback);
modified = ni->data_size != old_data_size ||
ni->initialized_size != old_init_size;
if (ret)
@@ -4195,6 +4310,15 @@ s64 ntfs_inode_attr_pwrite(struct inode *vi, s64 pos, s64 count, u8 *buf, bool s
ntfs_attr_put_search_ctx(ctx);
out_unlock:
mutex_unlock(&mrec_ni->mrec_lock);
+ if (deferred_rollback) {
+ ntfs_inode_rollback_enlarge(vi, mrec_ni, pos + count,
+ old_data_size);
+ /* The rollback may have restored the sizes. */
+ read_lock_irqsave(&ni->size_lock, flags);
+ modified = ni->data_size != old_data_size ||
+ ni->initialized_size != old_init_size;
+ read_unlock_irqrestore(&ni->size_lock, flags);
+ }
if (modified && ntfs_inode_is_named_stream(ni))
ntfs_stream_update_base_time(base_ni);
return ret;
--
2.25.1