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

From: Hongling Zeng

Date: Tue Dec 01 2026 - 06:13:54 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 the
size update under it, after re-checking that no concurrent write
took over the extension. Roll the sizes back with
__ntfs_attr_truncate_vfs() and ntfs_attr_set_data_size(): only the
clusters the write allocated are freed, and the allocation that
predates the write is preserved. 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")
Suggested-by: Hyunchul Lee <hyc.lee@xxxxxxxxx>
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Hongling Zeng <zenghongling@xxxxxxxxxx>
Reviewed-by: Baolin Liu <liubaolin@xxxxxxxxxx>
---
fs/ntfs/inode.c | 109 +++++++++++++++++++++++++++++++++++++++++++++---
1 file changed, 104 insertions(+), 5 deletions(-)

diff --git a/fs/ntfs/inode.c b/fs/ntfs/inode.c
index 3aac1d7d310f..48182e0f9da8 100644
--- a/fs/ntfs/inode.c
+++ b/fs/ntfs/inode.c
@@ -3849,12 +3849,20 @@ 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 rollback target under size_lock. */
+ 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 +3872,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 +3884,37 @@ 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;
+
+ /* A resident resize never sleeps on a folio under mrec_lock. */
+ if (NInoNonResident(ni)) {
+ /*
+ * The shrink would drop folios under mrec_lock:
+ * let the caller roll back once the lock is out.
+ */
+ *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. */
+ 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 {
+ ntfs_error(sb, "Failed to look up attr %#x",
+ ni->type);
+ }
return ret;
}
}
@@ -4096,6 +4134,54 @@ static s64 __ntfs_inode_non_resident_attr_pwrite(struct inode *vi,
return ret ? ret : written;
}

+/* Undo the deferred size update like ntfs_write_rollback() does. */
+static void ntfs_inode_rollback_enlarge(struct inode *vi,
+ struct ntfs_inode *mrec_ni, s64 new_data_size,
+ s64 old_data_size, s64 old_alloc_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) {
+ /* Free only what the write allocated. */
+ s64 keep = max(old_data_size, old_alloc_size);
+
+ if (ni->allocated_size > keep)
+ rollback_err = __ntfs_attr_truncate_vfs(ni, keep,
+ keep);
+ if (!rollback_err && ni->data_size != old_data_size)
+ rollback_err = ntfs_attr_set_data_size(ni,
+ old_data_size);
+ }
+ mutex_unlock(&mrec_ni->mrec_lock);
+ if (rollback_err) {
+ /* The sizes may be inconsistent now. */
+ 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);
@@ -4103,9 +4189,11 @@ s64 ntfs_inode_attr_pwrite(struct inode *vi, s64 pos, s64 count, u8 *buf, bool s
struct ntfs_inode *mrec_ni = ntfs_inode_is_named_stream(ni) ?
base_ni : ni;
struct ntfs_attr_search_ctx *ctx;
- loff_t old_data_size, old_init_size;
+ loff_t old_data_size, old_init_size, old_alloc_size;
+ unsigned long flags;
s64 ret;
bool modified = false;
+ bool deferred_rollback = false;

WARN_ON(!NInoAttr(ni));

@@ -4152,7 +4240,9 @@ 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);
+ old_alloc_size = ni->allocated_size;
+ 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 +4285,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, old_alloc_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