[PATCH v3 2/6] ntfs: roll back the size update when the attribute lookup fails
From: Hongling Zeng
Date: Sun Nov 29 2026 - 22:07:47 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.
Fixes: af0db57d4293 ("ntfs: update inode operations")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Hongling Zeng <zenghongling@xxxxxxxxxx>
---
fs/ntfs/inode.c | 129 ++++++++++++++++++++++++++++++++++++++++++++++--
1 file changed, 125 insertions(+), 4 deletions(-)
diff --git a/fs/ntfs/inode.c b/fs/ntfs/inode.c
index 3aac1d7d310f..1fd3e5a72076 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,56 @@ 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. A resident resize
+ * shrinks the value in place, and the conversion
+ * to non-resident uses FGP_NOWAIT, so it never
+ * sleeps on a folio under mrec_lock.
+ */
+ if (NInoNonResident(ni)) {
+ /*
+ * Dropping the folios beyond the old EOF
+ * cannot happen under mrec_lock: writeback
+ * locks a folio and waits for mrec_lock in
+ * ntfs_write_iomap_begin_non_resident().
+ * 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 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;
}
}
@@ -4096,6 +4157,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
+ *
+ * Like ntfs_setattr_size(): truncate_setsize() drops the folios
+ * beyond the old EOF with mrec_lock released, then
+ * __ntfs_attr_truncate_vfs() shrinks the attribute under mrec_lock.
+ * The stream ioctl already holds the invalidate lock, so only the
+ * internal attributes take it here.
+ */
+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)
+ filemap_invalidate_lock(vi->i_mapping);
+
+ 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);
+ }
+
+ if (!stream)
+ filemap_invalidate_unlock(vi->i_mapping);
+}
+
s64 ntfs_inode_attr_pwrite(struct inode *vi, s64 pos, s64 count, u8 *buf, bool sync)
{
struct ntfs_inode *ni = NTFS_I(vi);
@@ -4106,6 +4222,7 @@ s64 ntfs_inode_attr_pwrite(struct inode *vi, s64 pos, s64 count, u8 *buf, bool s
loff_t old_data_size, old_init_size;
s64 ret;
bool modified = false;
+ bool deferred_rollback = false;
WARN_ON(!NInoAttr(ni));
@@ -4152,7 +4269,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 +4313,9 @@ 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);
if (modified && ntfs_inode_is_named_stream(ni))
ntfs_stream_update_base_time(base_ni);
return ret;
--
2.25.1