* [PATCH v3 1/6] ntfs: mark the record dirtied by a resident write at iomap begin
2026-11-30 3:07 [PATCH v3 0/6] ntfs: synchronize the in-memory size handling Hongling Zeng
@ 2026-11-30 3:07 ` Hongling Zeng
2026-11-30 3:49 ` liubaolin
2026-11-30 3:07 ` [PATCH v3 2/6] ntfs: roll back the size update when the attribute lookup fails Hongling Zeng
` (4 subsequent siblings)
5 siblings, 1 reply; 13+ messages in thread
From: Hongling Zeng @ 2026-11-30 3:07 UTC (permalink / raw)
To: linkinjeon, hyc.lee, liubaolin12138
Cc: ntfs, linux-kernel, Hongling Zeng, Baolin Liu
A buffered write to a resident attribute is copied straight into the
mft record found by the search in ntfs_write_iomap_begin_resident().
With an attribute list that record can be an extent record, while
ntfs_write_iomap_end_resident() only marks the base inode dirty, so
the record holding the copied data is never written out and the write
is lost on a clean unmount.
Mark the record the lookup found at begin time, while the search
context still points at it. This is safe against a concurrent
writeback of the record: extent records are only written out from the
write_inode path of the base inode, which takes the same mrec_lock
the write path holds until iomap_end.
Keep marking the base record in iomap_end. The base record also
holds the standard information, which the write's timestamp update
modifies and which is likewise only written out when the record is
marked dirty.
Fixes: b041ca562526 ("ntfs: update iomap and address space operations")
Signed-off-by: Hongling Zeng <zenghongling@kylinos.cn>
Reviewed-by: Baolin Liu <liubaolin@kylinos.cn>
---
fs/ntfs/iomap.c | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/fs/ntfs/iomap.c b/fs/ntfs/iomap.c
index ce916707f69b..8933f432aa68 100644
--- a/fs/ntfs/iomap.c
+++ b/fs/ntfs/iomap.c
@@ -682,6 +682,15 @@ static int ntfs_write_iomap_begin_resident(struct inode *inode, loff_t offset,
goto out;
}
+ /*
+ * The record holding a resident attribute can be an extent record.
+ * Mark the record the lookup found dirty here, while the search
+ * context still points at it. The writeout of the record cannot
+ * overtake the inline copy: it is serialized by the mrec_lock held
+ * until iomap_end.
+ */
+ mark_mft_record_dirty(ctx->ntfs_ino);
+
a = ctx->attr;
/* The total length of the attribute value. */
attr_len = le32_to_cpu(a->data.resident.value_length);
--
2.25.1
^ permalink raw reply [flat|nested] 13+ messages in thread* Re: [PATCH v3 1/6] ntfs: mark the record dirtied by a resident write at iomap begin
2026-11-30 3:07 ` [PATCH v3 1/6] ntfs: mark the record dirtied by a resident write at iomap begin Hongling Zeng
@ 2026-11-30 3:49 ` liubaolin
0 siblings, 0 replies; 13+ messages in thread
From: liubaolin @ 2026-11-30 3:49 UTC (permalink / raw)
To: Hongling Zeng, linkinjeon, hyc.lee; +Cc: ntfs, linux-kernel, Baolin Liu
在 2026/11/30 11:07, Hongling Zeng 写道:
> A buffered write to a resident attribute is copied straight into the
> mft record found by the search in ntfs_write_iomap_begin_resident().
> With an attribute list that record can be an extent record, while
> ntfs_write_iomap_end_resident() only marks the base inode dirty, so
> the record holding the copied data is never written out and the write
> is lost on a clean unmount.
>
> Mark the record the lookup found at begin time, while the search
> context still points at it. This is safe against a concurrent
> writeback of the record: extent records are only written out from the
> write_inode path of the base inode, which takes the same mrec_lock
> the write path holds until iomap_end.
>
> Keep marking the base record in iomap_end. The base record also
> holds the standard information, which the write's timestamp update
> modifies and which is likewise only written out when the record is
> marked dirty.
>
> Fixes: b041ca562526 ("ntfs: update iomap and address space operations")
> Signed-off-by: Hongling Zeng <zenghongling@kylinos.cn>
> Reviewed-by: Baolin Liu <liubaolin@kylinos.cn>
> ---
> fs/ntfs/iomap.c | 9 +++++++++
> 1 file changed, 9 insertions(+)
>
> diff --git a/fs/ntfs/iomap.c b/fs/ntfs/iomap.c
> index ce916707f69b..8933f432aa68 100644
> --- a/fs/ntfs/iomap.c
> +++ b/fs/ntfs/iomap.c
> @@ -682,6 +682,15 @@ static int ntfs_write_iomap_begin_resident(struct inode *inode, loff_t offset,
> goto out;
> }
>
> + /*
> + * The record holding a resident attribute can be an extent record.
> + * Mark the record the lookup found dirty here, while the search
> + * context still points at it. The writeout of the record cannot
> + * overtake the inline copy: it is serialized by the mrec_lock held
> + * until iomap_end.
> + */
> + mark_mft_record_dirty(ctx->ntfs_ino);
> +
> a = ctx->attr;
> /* The total length of the attribute value. */
> attr_len = le32_to_cpu(a->data.resident.value_length);
Looks good to me.
Reviewed-by: Baolin Liu <liubaolin@kylinos.cn>
^ permalink raw reply [flat|nested] 13+ messages in thread
* [PATCH v3 2/6] ntfs: roll back the size update when the attribute lookup fails
2026-11-30 3:07 [PATCH v3 0/6] ntfs: synchronize the in-memory size handling Hongling Zeng
2026-11-30 3:07 ` [PATCH v3 1/6] ntfs: mark the record dirtied by a resident write at iomap begin Hongling Zeng
@ 2026-11-30 3:07 ` Hongling Zeng
2026-11-30 3:49 ` liubaolin
2026-11-30 3:07 ` [PATCH v3 3/6] ntfs: read initialized_size under size_lock in the iomap paths Hongling Zeng
` (3 subsequent siblings)
5 siblings, 1 reply; 13+ messages in thread
From: Hongling Zeng @ 2026-11-30 3:07 UTC (permalink / raw)
To: linkinjeon, hyc.lee, liubaolin12138
Cc: ntfs, linux-kernel, Hongling Zeng, stable
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@vger.kernel.org
Signed-off-by: Hongling Zeng <zenghongling@kylinos.cn>
---
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
^ permalink raw reply [flat|nested] 13+ messages in thread* Re: [PATCH v3 2/6] ntfs: roll back the size update when the attribute lookup fails
2026-11-30 3:07 ` [PATCH v3 2/6] ntfs: roll back the size update when the attribute lookup fails Hongling Zeng
@ 2026-11-30 3:49 ` liubaolin
0 siblings, 0 replies; 13+ messages in thread
From: liubaolin @ 2026-11-30 3:49 UTC (permalink / raw)
To: Hongling Zeng, linkinjeon, hyc.lee; +Cc: ntfs, linux-kernel, stable
在 2026/11/30 11:07, 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 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@vger.kernel.org
> Signed-off-by: Hongling Zeng <zenghongling@kylinos.cn>
> ---
> 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;
Looks good to me.
Reviewed-by: Baolin Liu <liubaolin@kylinos.cn>
^ permalink raw reply [flat|nested] 13+ messages in thread
* [PATCH v3 3/6] ntfs: read initialized_size under size_lock in the iomap paths
2026-11-30 3:07 [PATCH v3 0/6] ntfs: synchronize the in-memory size handling Hongling Zeng
2026-11-30 3:07 ` [PATCH v3 1/6] ntfs: mark the record dirtied by a resident write at iomap begin Hongling Zeng
2026-11-30 3:07 ` [PATCH v3 2/6] ntfs: roll back the size update when the attribute lookup fails Hongling Zeng
@ 2026-11-30 3:07 ` Hongling Zeng
2026-11-30 3:49 ` liubaolin
2026-11-30 3:07 ` [PATCH v3 4/6] ntfs: update the in-memory sizes under the size lock Hongling Zeng
` (2 subsequent siblings)
5 siblings, 1 reply; 13+ messages in thread
From: Hongling Zeng @ 2026-11-30 3:07 UTC (permalink / raw)
To: linkinjeon, hyc.lee, liubaolin12138
Cc: ntfs, linux-kernel, Hongling Zeng, stable
The iomap read paths read ni->initialized_size without size_lock.
ntfs_read_iomap_begin_non_resident() reads it twice in the length
adjustment condition and once more in round_up(), so a concurrent
update in between lets the condition pass with one value while the
length is computed from another, which can yield a length matching
neither boundary.
Read initialized_size once under size_lock in the resident clamp,
the non-resident unwritten mapping, the length adjustment,
ntfs_iomap_put_folio_non_resident() and the bio completion path
ntfs_iomap_read_end_io(), so each decision is made on one value.
In ntfs_read_iomap_begin_non_resident() the unwritten mapping
decision and the length adjustment share that snapshot, so the type
and the length of a mapped extent cannot be decided against different
initialized_size boundaries.
The buffered write and direct I/O paths read initialized_size
unlocked too, when deciding whether the head or the tail of a newly
allocated cluster has to be zeroed first. The mmap write fault path
can extend initialized_size while holding neither the inode lock nor
the MFT record lock, so snapshot the field under size_lock there as
well.
Fixes: b041ca562526 ("ntfs: update iomap and address space operations")
Reported-by: Hyunchul Lee <hyc.lee@gmail.com>
Cc: stable@vger.kernel.org
Signed-off-by: Hongling Zeng <zenghongling@kylinos.cn>
---
fs/ntfs/aops.c | 3 +++
fs/ntfs/iomap.c | 39 +++++++++++++++++++++++++++++++--------
2 files changed, 34 insertions(+), 8 deletions(-)
diff --git a/fs/ntfs/aops.c b/fs/ntfs/aops.c
index d0d1a8402bc4..82d3ebbc0972 100644
--- a/fs/ntfs/aops.c
+++ b/fs/ntfs/aops.c
@@ -24,10 +24,13 @@ static void ntfs_iomap_read_end_io(struct bio *bio)
bio_for_each_folio_all(iter, bio) {
struct folio *folio = iter.folio;
struct ntfs_inode *ni = NTFS_I(folio->mapping->host);
+ unsigned long flags;
s64 init_size;
loff_t pos = folio_pos(folio);
+ read_lock_irqsave(&ni->size_lock, flags);
init_size = ni->initialized_size;
+ read_unlock_irqrestore(&ni->size_lock, flags);
if (pos + iter.offset < init_size &&
pos + iter.offset + iter.length > init_size)
folio_zero_segment(folio, offset_in_folio(folio, init_size),
diff --git a/fs/ntfs/iomap.c b/fs/ntfs/iomap.c
index 8933f432aa68..a9c04fb964ba 100644
--- a/fs/ntfs/iomap.c
+++ b/fs/ntfs/iomap.c
@@ -17,11 +17,14 @@ static void ntfs_iomap_put_folio_non_resident(struct inode *inode, loff_t pos,
{
struct ntfs_inode *ni = NTFS_I(inode);
unsigned long sector_size = 1UL << inode->i_blkbits;
+ unsigned long flags;
loff_t start_down, end_up, init;
start_down = round_down(pos, sector_size);
end_up = (pos + len - 1) | (sector_size - 1);
+ read_lock_irqsave(&ni->size_lock, flags);
init = ni->initialized_size;
+ read_unlock_irqrestore(&ni->size_lock, flags);
if (init >= start_down && init <= end_up) {
if (init < pos) {
@@ -88,6 +91,7 @@ static int ntfs_read_iomap_begin_resident(struct inode *inode, loff_t offset, lo
loff_t i_size;
u32 attr_len;
int err = 0;
+ unsigned long irqflags;
char *kattr;
base_ni = ntfs_base_inode(ni);
@@ -106,8 +110,10 @@ static int ntfs_read_iomap_begin_resident(struct inode *inode, loff_t offset, lo
goto out;
attr_len = le32_to_cpu(ctx->attr->data.resident.value_length);
+ read_lock_irqsave(&ni->size_lock, irqflags);
if (unlikely(attr_len > ni->initialized_size))
attr_len = ni->initialized_size;
+ read_unlock_irqrestore(&ni->size_lock, irqflags);
i_size = i_size_read(inode);
if (unlikely(attr_len > i_size)) {
@@ -200,6 +206,8 @@ static int ntfs_read_iomap_begin_non_resident(struct inode *inode, loff_t offset
struct ntfs_volume *vol = ni->vol;
loff_t vcn_ofs;
loff_t rl_length;
+ loff_t initialized_size;
+ unsigned long irqflags;
vcn = ntfs_bytes_to_cluster(vol, offset);
vcn_ofs = ntfs_bytes_to_cluster_off(vol, offset);
@@ -231,7 +239,10 @@ static int ntfs_read_iomap_begin_non_resident(struct inode *inode, loff_t offset
iomap->type = IOMAP_HOLE;
iomap->addr = IOMAP_NULL_ADDR;
} else {
- if (need_unwritten && offset >= ni->initialized_size)
+ read_lock_irqsave(&ni->size_lock, irqflags);
+ initialized_size = ni->initialized_size;
+ read_unlock_irqrestore(&ni->size_lock, irqflags);
+ if (need_unwritten && offset >= initialized_size)
iomap->type = IOMAP_UNWRITTEN;
else
iomap->type = IOMAP_MAPPED;
@@ -256,9 +267,9 @@ static int ntfs_read_iomap_begin_non_resident(struct inode *inode, loff_t offset
if (!(flags & IOMAP_ZERO) &&
iomap->type == IOMAP_MAPPED &&
- iomap->offset < ni->initialized_size &&
- iomap->offset + iomap->length > ni->initialized_size) {
- iomap->length = round_up(ni->initialized_size, 1 << inode->i_blkbits) -
+ iomap->offset < initialized_size &&
+ iomap->offset + iomap->length > initialized_size) {
+ iomap->length = round_up(initialized_size, 1 << inode->i_blkbits) -
iomap->offset;
}
iomap->flags |= IOMAP_F_MERGED;
@@ -383,6 +394,7 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_
struct runlist_element *rl, *rlc;
bool is_retry = false;
int err = 0, map_err = 0;
+ unsigned long flags;
s64 vcn, lcn;
s64 max_clu_count =
ntfs_bytes_to_cluster(vol, round_up(length, vol->cluster_size));
@@ -511,9 +523,14 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_
iomap->flags = IOMAP_F_NEW;
if (lcn <= LCN_HOLE) {
loff_t end = offset + length;
+ loff_t initialized_size;
+
+ read_lock_irqsave(&ni->size_lock, flags);
+ initialized_size = ni->initialized_size;
+ read_unlock_irqrestore(&ni->size_lock, flags);
if (vcn_ofs || ((vol->cluster_size > iomap->length) &&
- end < ni->initialized_size)) {
+ end < initialized_size)) {
loff_t z_start, z_end;
z_start = vcn << vol->cluster_size_bits;
@@ -527,7 +544,7 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_
if ((!err || err == -EPERM) &&
max_clu_count > 1 &&
(iomap->length & vol->cluster_size_mask &&
- end < ni->initialized_size)) {
+ end < initialized_size)) {
loff_t z_start, z_end;
z_start = (vcn + max_clu_count - 1) <<
@@ -581,6 +598,7 @@ static int ntfs_write_da_iomap_begin_non_resident(struct inode *inode,
s64 vcn, start_lcn, lcn_count;
bool balloc = false, update_mp;
int err;
+ unsigned long irqflags;
s64 max_clu_count =
ntfs_bytes_to_cluster(vol, round_up(length, vol->cluster_size));
@@ -622,16 +640,21 @@ static int ntfs_write_da_iomap_begin_non_resident(struct inode *inode,
if (flags & IOMAP_DIRECT ||
ntfs_iomap_flags & NTFS_IOMAP_FLAGS_MKWRITE) {
loff_t end = offset + length;
+ loff_t initialized_size;
+
+ read_lock_irqsave(&ni->size_lock, irqflags);
+ initialized_size = ni->initialized_size;
+ read_unlock_irqrestore(&ni->size_lock, irqflags);
if (vcn_ofs || ((vol->cluster_size > iomap->length) &&
- end < ni->initialized_size))
+ end < initialized_size))
err = ntfs_dio_zero_range(inode,
start_lcn <<
vol->cluster_size_bits,
vol->cluster_size);
if (!err && lcn_count > 1 &&
(iomap->length & vol->cluster_size_mask &&
- end < ni->initialized_size))
+ end < initialized_size))
err = ntfs_dio_zero_range(inode,
(start_lcn + lcn_count - 1) <<
vol->cluster_size_bits,
--
2.25.1
^ permalink raw reply [flat|nested] 13+ messages in thread* Re: [PATCH v3 3/6] ntfs: read initialized_size under size_lock in the iomap paths
2026-11-30 3:07 ` [PATCH v3 3/6] ntfs: read initialized_size under size_lock in the iomap paths Hongling Zeng
@ 2026-11-30 3:49 ` liubaolin
0 siblings, 0 replies; 13+ messages in thread
From: liubaolin @ 2026-11-30 3:49 UTC (permalink / raw)
To: Hongling Zeng, linkinjeon, hyc.lee; +Cc: ntfs, linux-kernel, stable
在 2026/11/30 11:07, Hongling Zeng 写道:
> The iomap read paths read ni->initialized_size without size_lock.
> ntfs_read_iomap_begin_non_resident() reads it twice in the length
> adjustment condition and once more in round_up(), so a concurrent
> update in between lets the condition pass with one value while the
> length is computed from another, which can yield a length matching
> neither boundary.
>
> Read initialized_size once under size_lock in the resident clamp,
> the non-resident unwritten mapping, the length adjustment,
> ntfs_iomap_put_folio_non_resident() and the bio completion path
> ntfs_iomap_read_end_io(), so each decision is made on one value.
> In ntfs_read_iomap_begin_non_resident() the unwritten mapping
> decision and the length adjustment share that snapshot, so the type
> and the length of a mapped extent cannot be decided against different
> initialized_size boundaries.
>
> The buffered write and direct I/O paths read initialized_size
> unlocked too, when deciding whether the head or the tail of a newly
> allocated cluster has to be zeroed first. The mmap write fault path
> can extend initialized_size while holding neither the inode lock nor
> the MFT record lock, so snapshot the field under size_lock there as
> well.
>
> Fixes: b041ca562526 ("ntfs: update iomap and address space operations")
> Reported-by: Hyunchul Lee <hyc.lee@gmail.com>
> Cc: stable@vger.kernel.org
> Signed-off-by: Hongling Zeng <zenghongling@kylinos.cn>
> ---
> fs/ntfs/aops.c | 3 +++
> fs/ntfs/iomap.c | 39 +++++++++++++++++++++++++++++++--------
> 2 files changed, 34 insertions(+), 8 deletions(-)
>
> diff --git a/fs/ntfs/aops.c b/fs/ntfs/aops.c
> index d0d1a8402bc4..82d3ebbc0972 100644
> --- a/fs/ntfs/aops.c
> +++ b/fs/ntfs/aops.c
> @@ -24,10 +24,13 @@ static void ntfs_iomap_read_end_io(struct bio *bio)
> bio_for_each_folio_all(iter, bio) {
> struct folio *folio = iter.folio;
> struct ntfs_inode *ni = NTFS_I(folio->mapping->host);
> + unsigned long flags;
> s64 init_size;
> loff_t pos = folio_pos(folio);
>
> + read_lock_irqsave(&ni->size_lock, flags);
> init_size = ni->initialized_size;
> + read_unlock_irqrestore(&ni->size_lock, flags);
> if (pos + iter.offset < init_size &&
> pos + iter.offset + iter.length > init_size)
> folio_zero_segment(folio, offset_in_folio(folio, init_size),
> diff --git a/fs/ntfs/iomap.c b/fs/ntfs/iomap.c
> index 8933f432aa68..a9c04fb964ba 100644
> --- a/fs/ntfs/iomap.c
> +++ b/fs/ntfs/iomap.c
> @@ -17,11 +17,14 @@ static void ntfs_iomap_put_folio_non_resident(struct inode *inode, loff_t pos,
> {
> struct ntfs_inode *ni = NTFS_I(inode);
> unsigned long sector_size = 1UL << inode->i_blkbits;
> + unsigned long flags;
> loff_t start_down, end_up, init;
>
> start_down = round_down(pos, sector_size);
> end_up = (pos + len - 1) | (sector_size - 1);
> + read_lock_irqsave(&ni->size_lock, flags);
> init = ni->initialized_size;
> + read_unlock_irqrestore(&ni->size_lock, flags);
>
> if (init >= start_down && init <= end_up) {
> if (init < pos) {
> @@ -88,6 +91,7 @@ static int ntfs_read_iomap_begin_resident(struct inode *inode, loff_t offset, lo
> loff_t i_size;
> u32 attr_len;
> int err = 0;
> + unsigned long irqflags;
> char *kattr;
>
> base_ni = ntfs_base_inode(ni);
> @@ -106,8 +110,10 @@ static int ntfs_read_iomap_begin_resident(struct inode *inode, loff_t offset, lo
> goto out;
>
> attr_len = le32_to_cpu(ctx->attr->data.resident.value_length);
> + read_lock_irqsave(&ni->size_lock, irqflags);
> if (unlikely(attr_len > ni->initialized_size))
> attr_len = ni->initialized_size;
> + read_unlock_irqrestore(&ni->size_lock, irqflags);
> i_size = i_size_read(inode);
>
> if (unlikely(attr_len > i_size)) {
> @@ -200,6 +206,8 @@ static int ntfs_read_iomap_begin_non_resident(struct inode *inode, loff_t offset
> struct ntfs_volume *vol = ni->vol;
> loff_t vcn_ofs;
> loff_t rl_length;
> + loff_t initialized_size;
> + unsigned long irqflags;
>
> vcn = ntfs_bytes_to_cluster(vol, offset);
> vcn_ofs = ntfs_bytes_to_cluster_off(vol, offset);
> @@ -231,7 +239,10 @@ static int ntfs_read_iomap_begin_non_resident(struct inode *inode, loff_t offset
> iomap->type = IOMAP_HOLE;
> iomap->addr = IOMAP_NULL_ADDR;
> } else {
> - if (need_unwritten && offset >= ni->initialized_size)
> + read_lock_irqsave(&ni->size_lock, irqflags);
> + initialized_size = ni->initialized_size;
> + read_unlock_irqrestore(&ni->size_lock, irqflags);
> + if (need_unwritten && offset >= initialized_size)
> iomap->type = IOMAP_UNWRITTEN;
> else
> iomap->type = IOMAP_MAPPED;
> @@ -256,9 +267,9 @@ static int ntfs_read_iomap_begin_non_resident(struct inode *inode, loff_t offset
>
> if (!(flags & IOMAP_ZERO) &&
> iomap->type == IOMAP_MAPPED &&
> - iomap->offset < ni->initialized_size &&
> - iomap->offset + iomap->length > ni->initialized_size) {
> - iomap->length = round_up(ni->initialized_size, 1 << inode->i_blkbits) -
> + iomap->offset < initialized_size &&
> + iomap->offset + iomap->length > initialized_size) {
> + iomap->length = round_up(initialized_size, 1 << inode->i_blkbits) -
> iomap->offset;
> }
> iomap->flags |= IOMAP_F_MERGED;
> @@ -383,6 +394,7 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_
> struct runlist_element *rl, *rlc;
> bool is_retry = false;
> int err = 0, map_err = 0;
> + unsigned long flags;
> s64 vcn, lcn;
> s64 max_clu_count =
> ntfs_bytes_to_cluster(vol, round_up(length, vol->cluster_size));
> @@ -511,9 +523,14 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_
> iomap->flags = IOMAP_F_NEW;
> if (lcn <= LCN_HOLE) {
> loff_t end = offset + length;
> + loff_t initialized_size;
> +
> + read_lock_irqsave(&ni->size_lock, flags);
> + initialized_size = ni->initialized_size;
> + read_unlock_irqrestore(&ni->size_lock, flags);
>
> if (vcn_ofs || ((vol->cluster_size > iomap->length) &&
> - end < ni->initialized_size)) {
> + end < initialized_size)) {
> loff_t z_start, z_end;
>
> z_start = vcn << vol->cluster_size_bits;
> @@ -527,7 +544,7 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_
> if ((!err || err == -EPERM) &&
> max_clu_count > 1 &&
> (iomap->length & vol->cluster_size_mask &&
> - end < ni->initialized_size)) {
> + end < initialized_size)) {
> loff_t z_start, z_end;
>
> z_start = (vcn + max_clu_count - 1) <<
> @@ -581,6 +598,7 @@ static int ntfs_write_da_iomap_begin_non_resident(struct inode *inode,
> s64 vcn, start_lcn, lcn_count;
> bool balloc = false, update_mp;
> int err;
> + unsigned long irqflags;
> s64 max_clu_count =
> ntfs_bytes_to_cluster(vol, round_up(length, vol->cluster_size));
>
> @@ -622,16 +640,21 @@ static int ntfs_write_da_iomap_begin_non_resident(struct inode *inode,
> if (flags & IOMAP_DIRECT ||
> ntfs_iomap_flags & NTFS_IOMAP_FLAGS_MKWRITE) {
> loff_t end = offset + length;
> + loff_t initialized_size;
> +
> + read_lock_irqsave(&ni->size_lock, irqflags);
> + initialized_size = ni->initialized_size;
> + read_unlock_irqrestore(&ni->size_lock, irqflags);
>
> if (vcn_ofs || ((vol->cluster_size > iomap->length) &&
> - end < ni->initialized_size))
> + end < initialized_size))
> err = ntfs_dio_zero_range(inode,
> start_lcn <<
> vol->cluster_size_bits,
> vol->cluster_size);
> if (!err && lcn_count > 1 &&
> (iomap->length & vol->cluster_size_mask &&
> - end < ni->initialized_size))
> + end < initialized_size))
> err = ntfs_dio_zero_range(inode,
> (start_lcn + lcn_count - 1) <<
> vol->cluster_size_bits,
Looks good to me.
Reviewed-by: Baolin Liu <liubaolin@kylinos.cn>
^ permalink raw reply [flat|nested] 13+ messages in thread
* [PATCH v3 4/6] ntfs: update the in-memory sizes under the size lock
2026-11-30 3:07 [PATCH v3 0/6] ntfs: synchronize the in-memory size handling Hongling Zeng
` (2 preceding siblings ...)
2026-11-30 3:07 ` [PATCH v3 3/6] ntfs: read initialized_size under size_lock in the iomap paths Hongling Zeng
@ 2026-11-30 3:07 ` Hongling Zeng
2026-11-30 3:49 ` liubaolin
2026-11-30 3:07 ` [PATCH v3 5/6] ntfs: serialize the mmap write fault initialized size update Hongling Zeng
2026-11-30 3:07 ` [PATCH v3 6/6] ntfs: read initialized_size under size_lock in the mmap prepare path Hongling Zeng
5 siblings, 1 reply; 13+ messages in thread
From: Hongling Zeng @ 2026-11-30 3:07 UTC (permalink / raw)
To: linkinjeon, hyc.lee, liubaolin12138
Cc: ntfs, linux-kernel, Hongling Zeng, Baolin Liu
The runtime mutation paths update data_size, initialized_size and
allocated_size without size_lock, racing with the readers that use
the lock to obtain a coherent snapshot.
Protect the shrink and expand tails, make-resident and resident-resize
paths, insert/collapse range, KEEP_SIZE rollback, internal initialized
size updates, preallocation trimming, the index root reparent, and
the compressed size recalculation. Group assignments that form one
logical size-state update into the same critical section where
possible.
The inode load, creation, and eviction paths are unchanged because the
inode is not concurrently visible there. Existing inode and MFT locks
serialize writers; size_lock synchronizes publication of the resulting
state to readers.
All affected mutation paths are already serialized against one another by
the inode lock or the relevant MFT record lock; size_lock is added only to
synchronize publication to readers.
LOCKING PROTOCOL:
- size_lock protects: data_size, initialized_size, allocated_size,
itype.compressed.size (allocated size for compressed files)
- itype.compressed.block_size, block_size_bits, block_clusters are NOT
protected by size_lock (reader and writer access patterns confirmed)
Signed-off-by: Hongling Zeng <zenghongling@kylinos.cn>
Reviewed-by: Baolin Liu <liubaolin@kylinos.cn>
---
fs/ntfs/attrib.c | 48 +++++++++++++++++++++++++++++++++++++++++++-----
fs/ntfs/file.c | 3 +++
fs/ntfs/index.c | 5 +++++
fs/ntfs/inode.c | 42 ++++++++++++++++++++++++++++++------------
4 files changed, 81 insertions(+), 17 deletions(-)
diff --git a/fs/ntfs/attrib.c b/fs/ntfs/attrib.c
index 6ee6b6bcac1d..081ce3e829f2 100644
--- a/fs/ntfs/attrib.c
+++ b/fs/ntfs/attrib.c
@@ -2436,6 +2436,7 @@ int ntfs_attr_set_initialized_size(struct ntfs_inode *ni, loff_t new_size)
struct ntfs_attr_search_ctx *ctx;
struct ntfs_inode *base_ni = ntfs_base_inode(ni);
int err = 0;
+ unsigned long flags;
if (!NInoNonResident(ni))
return -EINVAL;
@@ -2450,7 +2451,9 @@ int ntfs_attr_set_initialized_size(struct ntfs_inode *ni, loff_t new_size)
goto out_ctx;
ctx->attr->data.non_resident.initialized_size = cpu_to_le64(new_size);
+ write_lock_irqsave(&ni->size_lock, flags);
ni->initialized_size = new_size;
+ write_unlock_irqrestore(&ni->size_lock, flags);
mark_mft_record_dirty(ctx->ntfs_ino);
out_ctx:
ntfs_attr_put_search_ctx(ctx);
@@ -3729,6 +3732,7 @@ static int ntfs_attr_update_meta(struct attr_record *a, struct ntfs_inode *ni,
struct ntfs_inode *locked_ni, bool defer_attrlist)
{
int sparse, err = 0;
+ unsigned long flags;
struct ntfs_inode *base_ni;
struct super_block *sb = ni->vol->sb;
@@ -3834,7 +3838,9 @@ static int ntfs_attr_update_meta(struct attr_record *a, struct ntfs_inode *ni,
goto out;
}
+ write_lock_irqsave(&ni->size_lock, flags);
ni->itype.compressed.size = new_compr_size;
+ write_unlock_irqrestore(&ni->size_lock, flags);
a->data.non_resident.compressed_size = cpu_to_le64(new_compr_size);
}
@@ -4271,6 +4277,7 @@ static int ntfs_attr_make_resident(struct ntfs_inode *ni, struct ntfs_attr_searc
struct attr_record *a = ctx->attr;
int name_ofs, val_ofs, err;
s64 arec_size, freed;
+ unsigned long flags;
ntfs_debug("Entering for inode 0x%llx, attr 0x%x.\n",
(unsigned long long)ni->mft_no, ni->type);
@@ -4376,8 +4383,10 @@ static int ntfs_attr_make_resident(struct ntfs_inode *ni, struct ntfs_attr_searc
ni->flags &= ~FILE_ATTR_SPARSE_FILE;
NInoClearEncrypted(ni);
ni->flags &= ~FILE_ATTR_ENCRYPTED;
+ write_lock_irqsave(&ni->size_lock, flags);
ni->initialized_size = ni->data_size;
ni->allocated_size = ni->itype.compressed.size = (ni->data_size + 7) & ~7;
+ write_unlock_irqrestore(&ni->size_lock, flags);
ni->itype.compressed.block_size = 0;
ni->itype.compressed.block_size_bits = ni->itype.compressed.block_clusters = 0;
return 0;
@@ -4401,6 +4410,8 @@ static int ntfs_non_resident_attr_shrink(struct ntfs_inode *ni,
s64 first_free_vcn;
s64 nr_freed_clusters;
int err;
+ unsigned long flags;
+ bool update_initialized;
struct ntfs_inode *base_ni;
bool runlist_locked = locked_ni == ni;
@@ -4493,12 +4504,16 @@ static int ntfs_non_resident_attr_shrink(struct ntfs_inode *ni,
}
/* Prepare to mapping pairs update. */
+ write_lock_irqsave(&ni->size_lock, flags);
ni->allocated_size = ntfs_cluster_to_bytes(vol, first_free_vcn);
+ write_unlock_irqrestore(&ni->size_lock, flags);
if (NInoSparse(ni) || NInoCompressed(ni)) {
if (nr_freed_clusters) {
+ write_lock_irqsave(&ni->size_lock, flags);
ni->itype.compressed.size -=
ntfs_cluster_to_bytes(vol, nr_freed_clusters);
+ write_unlock_irqrestore(&ni->size_lock, flags);
VFS_I(base_ni)->i_blocks = ni->itype.compressed.size >> 9;
}
} else
@@ -4531,12 +4546,15 @@ static int ntfs_non_resident_attr_shrink(struct ntfs_inode *ni,
}
/* Update data and initialized size. */
+ write_lock_irqsave(&ni->size_lock, flags);
ni->data_size = newsize;
- ctx->attr->data.non_resident.data_size = cpu_to_le64(newsize);
- if (newsize < ni->initialized_size) {
+ update_initialized = newsize < ni->initialized_size;
+ if (update_initialized)
ni->initialized_size = newsize;
+ write_unlock_irqrestore(&ni->size_lock, flags);
+ ctx->attr->data.non_resident.data_size = cpu_to_le64(newsize);
+ if (update_initialized)
ctx->attr->data.non_resident.initialized_size = cpu_to_le64(newsize);
- }
/*
* Drop any page-cache folios that now lie beyond the shrunk
@@ -4808,7 +4826,9 @@ static int ntfs_non_resident_attr_expand(struct ntfs_inode *ni, const s64 newsiz
}
/* Prepare to mapping pairs update. */
+ write_lock_irqsave(&ni->size_lock, flags);
ni->allocated_size = ntfs_cluster_to_bytes(vol, first_free_vcn);
+ write_unlock_irqrestore(&ni->size_lock, flags);
if (ni->type == AT_ATTRIBUTE_LIST && !runlist_locked) {
/*
* Making room for the list's mapping pairs can resize
@@ -4854,7 +4874,9 @@ static int ntfs_non_resident_attr_expand(struct ntfs_inode *ni, const s64 newsiz
}
/* Update data size. */
+ write_lock_irqsave(&ni->size_lock, flags);
ni->data_size = newsize;
+ write_unlock_irqrestore(&ni->size_lock, flags);
ctx->attr->data.non_resident.data_size = cpu_to_le64(newsize);
/* Update data size in the index. */
if (ni->type == AT_DATA && ni->name == AT_UNNAMED)
@@ -4900,7 +4922,9 @@ static int ntfs_non_resident_attr_expand(struct ntfs_inode *ni, const s64 newsiz
ntfs_error(sb, "Failed to restore old mapping pairs");
if (NInoSparse(ni) || NInoCompressed(ni)) {
- ni->itype.compressed.size = org_compressed_size;
+ write_lock_irqsave(&ni->size_lock, flags);
+ ni->itype.compressed.size = org_compressed_size;
+ write_unlock_irqrestore(&ni->size_lock, flags);
VFS_I(base_ni)->i_blocks = ni->itype.compressed.size >> 9;
} else
VFS_I(base_ni)->i_blocks = ni->allocated_size >> 9;
@@ -4936,6 +4960,7 @@ static int ntfs_resident_attr_resize(struct ntfs_inode *attr_ni, const s64 newsi
struct ntfs_volume *vol = attr_ni->vol;
struct super_block *sb = vol->sb;
int err = -EIO;
+ unsigned long flags;
struct ntfs_inode *base_ni, *ext_ni = NULL;
attr_resize_again:
@@ -4984,10 +5009,12 @@ static int ntfs_resident_attr_resize(struct ntfs_inode *attr_ni, const s64 newsi
newsize);
if (!err) {
/* Update attribute size everywhere. */
+ write_lock_irqsave(&attr_ni->size_lock, flags);
attr_ni->data_size = attr_ni->initialized_size = newsize;
attr_ni->allocated_size = (newsize + 7) & ~7;
if (NInoCompressed(attr_ni) || NInoSparse(attr_ni))
attr_ni->itype.compressed.size = attr_ni->allocated_size;
+ write_unlock_irqrestore(&attr_ni->size_lock, flags);
if (attr_ni->type == AT_DATA && attr_ni->name == AT_UNNAMED)
NInoSetFileNameDirty(attr_ni);
goto resize_done;
@@ -5789,6 +5816,7 @@ int ntfs_non_resident_attr_insert_range(struct ntfs_inode *ni, s64 start_vcn, s6
struct ntfs_attr_search_ctx *ctx;
int ret;
size_t new_rl_count;
+ unsigned long flags;
if (ni->type != AT_DATA)
return -EOPNOTSUPP;
@@ -5830,10 +5858,12 @@ int ntfs_non_resident_attr_insert_range(struct ntfs_inode *ni, s64 start_vcn, s6
ni->runlist.rl = rl;
ni->runlist.count = new_rl_count;
+ write_lock_irqsave(&ni->size_lock, flags);
ni->allocated_size += ntfs_cluster_to_bytes(vol, len);
ni->data_size += ntfs_cluster_to_bytes(vol, len);
if (ntfs_cluster_to_bytes(vol, start_vcn) < ni->initialized_size)
ni->initialized_size += ntfs_cluster_to_bytes(vol, len);
+ write_unlock_irqrestore(&ni->size_lock, flags);
ret = ntfs_attr_update_mapping_pairs_locked(ni, 0, ni);
up_write(&ni->runlist.lock);
if (ret)
@@ -5871,6 +5901,8 @@ int ntfs_non_resident_attr_collapse_range(struct ntfs_inode *ni, s64 start_vcn,
int dst_cnt;
int ret;
size_t new_rl_cnt;
+ unsigned long flags;
+ s64 allocated_size;
if (ni->type != AT_DATA)
return -EOPNOTSUPP;
@@ -5904,6 +5936,7 @@ int ntfs_non_resident_attr_collapse_range(struct ntfs_inode *ni, s64 start_vcn,
ni->runlist.rl = rl;
ni->runlist.count = new_rl_cnt;
+ write_lock_irqsave(&ni->size_lock, flags);
ni->allocated_size -= ntfs_cluster_to_bytes(vol, len);
if (ni->data_size > ntfs_cluster_to_bytes(vol, start_vcn)) {
if (ni->data_size > ntfs_cluster_to_bytes(vol, (start_vcn + len)))
@@ -5918,8 +5951,10 @@ int ntfs_non_resident_attr_collapse_range(struct ntfs_inode *ni, s64 start_vcn,
else
ni->initialized_size = ntfs_cluster_to_bytes(vol, start_vcn);
}
+ allocated_size = ni->allocated_size;
+ write_unlock_irqrestore(&ni->size_lock, flags);
- if (ni->allocated_size > 0) {
+ if (allocated_size > 0) {
ret = ntfs_attr_update_mapping_pairs_locked(ni, 0, ni);
if (ret) {
up_write(&ni->runlist.lock);
@@ -6029,6 +6064,7 @@ int ntfs_attr_fallocate(struct ntfs_inode *ni, loff_t start, loff_t byte_len, bo
int err = 0;
struct runlist_element *rl;
bool balloc;
+ unsigned long flags;
if (ni->type != AT_DATA)
return -EINVAL;
@@ -6076,7 +6112,9 @@ int ntfs_attr_fallocate(struct ntfs_inode *ni, loff_t start, loff_t byte_len, bo
err = -EIO;
goto out_unmap;
}
+ write_lock_irqsave(&ni->size_lock, flags);
ni->data_size = old_data_size;
+ write_unlock_irqrestore(&ni->size_lock, flags);
i_size_write(VFS_I(ni), old_data_size);
if (NInoNonResident(ni))
ctx->attr->data.non_resident.data_size =
diff --git a/fs/ntfs/file.c b/fs/ntfs/file.c
index 612498b45fbb..956aad92cc72 100644
--- a/fs/ntfs/file.c
+++ b/fs/ntfs/file.c
@@ -93,6 +93,7 @@ static int ntfs_trim_prealloc(struct inode *vi)
s64 vcn_ds, vcn_tr;
ssize_t rc;
int err = 0;
+ unsigned long flags;
inode_lock(vi);
mutex_lock(&mrec_ni->mrec_lock);
@@ -119,7 +120,9 @@ static int ntfs_trim_prealloc(struct inode *vi)
ni->runlist.rl = NULL;
ntfs_error(vol->sb, "Preallocated block rollback failed");
} else {
+ write_lock_irqsave(&ni->size_lock, flags);
ni->allocated_size = ntfs_cluster_to_bytes(vol, vcn_tr);
+ write_unlock_irqrestore(&ni->size_lock, flags);
err = ntfs_attr_update_mapping_pairs_locked(
ni, 0, ni);
if (err)
diff --git a/fs/ntfs/index.c b/fs/ntfs/index.c
index f80cd42ed0d1..39e669b390e9 100644
--- a/fs/ntfs/index.c
+++ b/fs/ntfs/index.c
@@ -1376,6 +1376,7 @@ static int ntfs_ir_reparent(struct ntfs_index_context *icx)
u32 old_value_length;
int ix_root_size;
int ret = 0;
+ unsigned long flags;
ntfs_debug("Entering\n");
@@ -1431,9 +1432,11 @@ static int ntfs_ir_reparent(struct ntfs_index_context *icx)
if (ret)
goto resize_failed;
+ write_lock_irqsave(&icx->idx_ni->size_lock, flags);
icx->idx_ni->data_size = ix_root_size;
icx->idx_ni->initialized_size = ix_root_size;
icx->idx_ni->allocated_size = (ix_root_size + 7) & ~7;
+ write_unlock_irqrestore(&icx->idx_ni->size_lock, flags);
i_size_write(VFS_I(icx->idx_ni), ix_root_size);
}
@@ -1453,9 +1456,11 @@ static int ntfs_ir_reparent(struct ntfs_index_context *icx)
if (ret)
goto resize_failed;
+ write_lock_irqsave(&icx->idx_ni->size_lock, flags);
icx->idx_ni->data_size = ix_root_size;
icx->idx_ni->initialized_size = ix_root_size;
icx->idx_ni->allocated_size = (ix_root_size + 7) & ~7;
+ write_unlock_irqrestore(&icx->idx_ni->size_lock, flags);
i_size_write(VFS_I(icx->idx_ni), ix_root_size);
}
ntfs_ie_set_vcn(ie, new_ib_vcn);
diff --git a/fs/ntfs/inode.c b/fs/ntfs/inode.c
index 1fd3e5a72076..8bf504147517 100644
--- a/fs/ntfs/inode.c
+++ b/fs/ntfs/inode.c
@@ -3856,18 +3856,22 @@ static inline int ntfs_enlarge_attribute(struct inode *vi, s64 pos, s64 count,
struct super_block *sb = vi->i_sb;
unsigned long flags;
s64 old_data_size;
+ s64 new_initialized_size = pos + count;
+ s64 initialized_size;
+ s64 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
+ * Snapshot the old sizes under size_lock: data_size is the
+ * rollback target, initialized_size is compared below, the other
+ * sizes are updated by ntfs_attr_truncate() itself
*/
read_lock_irqsave(&ni->size_lock, flags);
old_data_size = ni->data_size;
+ initialized_size = ni->initialized_size;
read_unlock_irqrestore(&ni->size_lock, flags);
- if (pos + count <= ni->initialized_size)
+ if (new_initialized_size <= initialized_size)
return 0;
if (NInoEncrypted(ni) && NInoNonResident(ni))
@@ -3876,7 +3880,7 @@ static inline int ntfs_enlarge_attribute(struct inode *vi, s64 pos, s64 count,
if (NInoCompressed(ni))
return -EOPNOTSUPP;
- if (pos + count > old_data_size) {
+ if (new_initialized_size > old_data_size) {
ret = ntfs_attr_truncate(ni, pos + count);
if (ret) {
ntfs_debug("Failed to truncate attribute");
@@ -3942,25 +3946,39 @@ static inline int ntfs_enlarge_attribute(struct inode *vi, s64 pos, s64 count,
}
}
+ /* ntfs_attr_truncate() may have updated data_size, re-read it */
+ read_lock_irqsave(&ni->size_lock, flags);
+ data_size = ni->data_size;
+ read_unlock_irqrestore(&ni->size_lock, flags);
+
if (!NInoNonResident(ni)) {
- if (likely(i_size_read(vi) < ni->data_size))
- i_size_write(vi, ni->data_size);
+ if (likely(i_size_read(vi) < data_size))
+ i_size_write(vi, data_size);
return 0;
}
/* Named streams initialize gaps after dropping the MFT record lock. */
if (ntfs_inode_is_named_stream(ni)) {
- if (i_size_read(vi) < ni->data_size)
- i_size_write(vi, ni->data_size);
+ if (i_size_read(vi) < data_size)
+ i_size_write(vi, data_size);
return 0;
}
- if (pos + count > ni->initialized_size) {
+ /* Re-read to avoid race after possible truncate */
+ read_lock_irqsave(&ni->size_lock, flags);
+ initialized_size = ni->initialized_size;
+ read_unlock_irqrestore(&ni->size_lock, flags);
+
+ if (new_initialized_size > initialized_size) {
ctx->attr->data.non_resident.initialized_size = cpu_to_le64(pos + count);
mark_mft_record_dirty(ctx->ntfs_ino);
+
+ write_lock_irqsave(&ni->size_lock, flags);
ni->initialized_size = pos + count;
- if (i_size_read(vi) < ni->initialized_size)
- i_size_write(vi, ni->initialized_size);
+ write_unlock_irqrestore(&ni->size_lock, flags);
+
+ if (i_size_read(vi) < new_initialized_size)
+ i_size_write(vi, new_initialized_size);
}
return 0;
}
--
2.25.1
^ permalink raw reply [flat|nested] 13+ messages in thread* Re: [PATCH v3 4/6] ntfs: update the in-memory sizes under the size lock
2026-11-30 3:07 ` [PATCH v3 4/6] ntfs: update the in-memory sizes under the size lock Hongling Zeng
@ 2026-11-30 3:49 ` liubaolin
0 siblings, 0 replies; 13+ messages in thread
From: liubaolin @ 2026-11-30 3:49 UTC (permalink / raw)
To: Hongling Zeng, linkinjeon, hyc.lee; +Cc: ntfs, linux-kernel, Baolin Liu
在 2026/11/30 11:07, Hongling Zeng 写道:
> The runtime mutation paths update data_size, initialized_size and
> allocated_size without size_lock, racing with the readers that use
> the lock to obtain a coherent snapshot.
>
> Protect the shrink and expand tails, make-resident and resident-resize
> paths, insert/collapse range, KEEP_SIZE rollback, internal initialized
> size updates, preallocation trimming, the index root reparent, and
> the compressed size recalculation. Group assignments that form one
> logical size-state update into the same critical section where
> possible.
>
> The inode load, creation, and eviction paths are unchanged because the
> inode is not concurrently visible there. Existing inode and MFT locks
> serialize writers; size_lock synchronizes publication of the resulting
> state to readers.
>
> All affected mutation paths are already serialized against one another by
> the inode lock or the relevant MFT record lock; size_lock is added only to
> synchronize publication to readers.
>
> LOCKING PROTOCOL:
> - size_lock protects: data_size, initialized_size, allocated_size,
> itype.compressed.size (allocated size for compressed files)
> - itype.compressed.block_size, block_size_bits, block_clusters are NOT
> protected by size_lock (reader and writer access patterns confirmed)
>
> Signed-off-by: Hongling Zeng <zenghongling@kylinos.cn>
> Reviewed-by: Baolin Liu <liubaolin@kylinos.cn>
> ---
> fs/ntfs/attrib.c | 48 +++++++++++++++++++++++++++++++++++++++++++-----
> fs/ntfs/file.c | 3 +++
> fs/ntfs/index.c | 5 +++++
> fs/ntfs/inode.c | 42 ++++++++++++++++++++++++++++++------------
> 4 files changed, 81 insertions(+), 17 deletions(-)
>
> diff --git a/fs/ntfs/attrib.c b/fs/ntfs/attrib.c
> index 6ee6b6bcac1d..081ce3e829f2 100644
> --- a/fs/ntfs/attrib.c
> +++ b/fs/ntfs/attrib.c
> @@ -2436,6 +2436,7 @@ int ntfs_attr_set_initialized_size(struct ntfs_inode *ni, loff_t new_size)
> struct ntfs_attr_search_ctx *ctx;
> struct ntfs_inode *base_ni = ntfs_base_inode(ni);
> int err = 0;
> + unsigned long flags;
>
> if (!NInoNonResident(ni))
> return -EINVAL;
> @@ -2450,7 +2451,9 @@ int ntfs_attr_set_initialized_size(struct ntfs_inode *ni, loff_t new_size)
> goto out_ctx;
>
> ctx->attr->data.non_resident.initialized_size = cpu_to_le64(new_size);
> + write_lock_irqsave(&ni->size_lock, flags);
> ni->initialized_size = new_size;
> + write_unlock_irqrestore(&ni->size_lock, flags);
> mark_mft_record_dirty(ctx->ntfs_ino);
> out_ctx:
> ntfs_attr_put_search_ctx(ctx);
> @@ -3729,6 +3732,7 @@ static int ntfs_attr_update_meta(struct attr_record *a, struct ntfs_inode *ni,
> struct ntfs_inode *locked_ni, bool defer_attrlist)
> {
> int sparse, err = 0;
> + unsigned long flags;
> struct ntfs_inode *base_ni;
> struct super_block *sb = ni->vol->sb;
>
> @@ -3834,7 +3838,9 @@ static int ntfs_attr_update_meta(struct attr_record *a, struct ntfs_inode *ni,
> goto out;
> }
>
> + write_lock_irqsave(&ni->size_lock, flags);
> ni->itype.compressed.size = new_compr_size;
> + write_unlock_irqrestore(&ni->size_lock, flags);
> a->data.non_resident.compressed_size = cpu_to_le64(new_compr_size);
> }
>
> @@ -4271,6 +4277,7 @@ static int ntfs_attr_make_resident(struct ntfs_inode *ni, struct ntfs_attr_searc
> struct attr_record *a = ctx->attr;
> int name_ofs, val_ofs, err;
> s64 arec_size, freed;
> + unsigned long flags;
>
> ntfs_debug("Entering for inode 0x%llx, attr 0x%x.\n",
> (unsigned long long)ni->mft_no, ni->type);
> @@ -4376,8 +4383,10 @@ static int ntfs_attr_make_resident(struct ntfs_inode *ni, struct ntfs_attr_searc
> ni->flags &= ~FILE_ATTR_SPARSE_FILE;
> NInoClearEncrypted(ni);
> ni->flags &= ~FILE_ATTR_ENCRYPTED;
> + write_lock_irqsave(&ni->size_lock, flags);
> ni->initialized_size = ni->data_size;
> ni->allocated_size = ni->itype.compressed.size = (ni->data_size + 7) & ~7;
> + write_unlock_irqrestore(&ni->size_lock, flags);
> ni->itype.compressed.block_size = 0;
> ni->itype.compressed.block_size_bits = ni->itype.compressed.block_clusters = 0;
> return 0;
> @@ -4401,6 +4410,8 @@ static int ntfs_non_resident_attr_shrink(struct ntfs_inode *ni,
> s64 first_free_vcn;
> s64 nr_freed_clusters;
> int err;
> + unsigned long flags;
> + bool update_initialized;
> struct ntfs_inode *base_ni;
> bool runlist_locked = locked_ni == ni;
>
> @@ -4493,12 +4504,16 @@ static int ntfs_non_resident_attr_shrink(struct ntfs_inode *ni,
> }
>
> /* Prepare to mapping pairs update. */
> + write_lock_irqsave(&ni->size_lock, flags);
> ni->allocated_size = ntfs_cluster_to_bytes(vol, first_free_vcn);
> + write_unlock_irqrestore(&ni->size_lock, flags);
>
> if (NInoSparse(ni) || NInoCompressed(ni)) {
> if (nr_freed_clusters) {
> + write_lock_irqsave(&ni->size_lock, flags);
> ni->itype.compressed.size -=
> ntfs_cluster_to_bytes(vol, nr_freed_clusters);
> + write_unlock_irqrestore(&ni->size_lock, flags);
> VFS_I(base_ni)->i_blocks = ni->itype.compressed.size >> 9;
> }
> } else
> @@ -4531,12 +4546,15 @@ static int ntfs_non_resident_attr_shrink(struct ntfs_inode *ni,
> }
>
> /* Update data and initialized size. */
> + write_lock_irqsave(&ni->size_lock, flags);
> ni->data_size = newsize;
> - ctx->attr->data.non_resident.data_size = cpu_to_le64(newsize);
> - if (newsize < ni->initialized_size) {
> + update_initialized = newsize < ni->initialized_size;
> + if (update_initialized)
> ni->initialized_size = newsize;
> + write_unlock_irqrestore(&ni->size_lock, flags);
> + ctx->attr->data.non_resident.data_size = cpu_to_le64(newsize);
> + if (update_initialized)
> ctx->attr->data.non_resident.initialized_size = cpu_to_le64(newsize);
> - }
>
> /*
> * Drop any page-cache folios that now lie beyond the shrunk
> @@ -4808,7 +4826,9 @@ static int ntfs_non_resident_attr_expand(struct ntfs_inode *ni, const s64 newsiz
> }
>
> /* Prepare to mapping pairs update. */
> + write_lock_irqsave(&ni->size_lock, flags);
> ni->allocated_size = ntfs_cluster_to_bytes(vol, first_free_vcn);
> + write_unlock_irqrestore(&ni->size_lock, flags);
> if (ni->type == AT_ATTRIBUTE_LIST && !runlist_locked) {
> /*
> * Making room for the list's mapping pairs can resize
> @@ -4854,7 +4874,9 @@ static int ntfs_non_resident_attr_expand(struct ntfs_inode *ni, const s64 newsiz
> }
>
> /* Update data size. */
> + write_lock_irqsave(&ni->size_lock, flags);
> ni->data_size = newsize;
> + write_unlock_irqrestore(&ni->size_lock, flags);
> ctx->attr->data.non_resident.data_size = cpu_to_le64(newsize);
> /* Update data size in the index. */
> if (ni->type == AT_DATA && ni->name == AT_UNNAMED)
> @@ -4900,7 +4922,9 @@ static int ntfs_non_resident_attr_expand(struct ntfs_inode *ni, const s64 newsiz
> ntfs_error(sb, "Failed to restore old mapping pairs");
>
> if (NInoSparse(ni) || NInoCompressed(ni)) {
> - ni->itype.compressed.size = org_compressed_size;
> + write_lock_irqsave(&ni->size_lock, flags);
> + ni->itype.compressed.size = org_compressed_size;
> + write_unlock_irqrestore(&ni->size_lock, flags);
> VFS_I(base_ni)->i_blocks = ni->itype.compressed.size >> 9;
> } else
> VFS_I(base_ni)->i_blocks = ni->allocated_size >> 9;
> @@ -4936,6 +4960,7 @@ static int ntfs_resident_attr_resize(struct ntfs_inode *attr_ni, const s64 newsi
> struct ntfs_volume *vol = attr_ni->vol;
> struct super_block *sb = vol->sb;
> int err = -EIO;
> + unsigned long flags;
> struct ntfs_inode *base_ni, *ext_ni = NULL;
>
> attr_resize_again:
> @@ -4984,10 +5009,12 @@ static int ntfs_resident_attr_resize(struct ntfs_inode *attr_ni, const s64 newsi
> newsize);
> if (!err) {
> /* Update attribute size everywhere. */
> + write_lock_irqsave(&attr_ni->size_lock, flags);
> attr_ni->data_size = attr_ni->initialized_size = newsize;
> attr_ni->allocated_size = (newsize + 7) & ~7;
> if (NInoCompressed(attr_ni) || NInoSparse(attr_ni))
> attr_ni->itype.compressed.size = attr_ni->allocated_size;
> + write_unlock_irqrestore(&attr_ni->size_lock, flags);
> if (attr_ni->type == AT_DATA && attr_ni->name == AT_UNNAMED)
> NInoSetFileNameDirty(attr_ni);
> goto resize_done;
> @@ -5789,6 +5816,7 @@ int ntfs_non_resident_attr_insert_range(struct ntfs_inode *ni, s64 start_vcn, s6
> struct ntfs_attr_search_ctx *ctx;
> int ret;
> size_t new_rl_count;
> + unsigned long flags;
>
> if (ni->type != AT_DATA)
> return -EOPNOTSUPP;
> @@ -5830,10 +5858,12 @@ int ntfs_non_resident_attr_insert_range(struct ntfs_inode *ni, s64 start_vcn, s6
> ni->runlist.rl = rl;
> ni->runlist.count = new_rl_count;
>
> + write_lock_irqsave(&ni->size_lock, flags);
> ni->allocated_size += ntfs_cluster_to_bytes(vol, len);
> ni->data_size += ntfs_cluster_to_bytes(vol, len);
> if (ntfs_cluster_to_bytes(vol, start_vcn) < ni->initialized_size)
> ni->initialized_size += ntfs_cluster_to_bytes(vol, len);
> + write_unlock_irqrestore(&ni->size_lock, flags);
> ret = ntfs_attr_update_mapping_pairs_locked(ni, 0, ni);
> up_write(&ni->runlist.lock);
> if (ret)
> @@ -5871,6 +5901,8 @@ int ntfs_non_resident_attr_collapse_range(struct ntfs_inode *ni, s64 start_vcn,
> int dst_cnt;
> int ret;
> size_t new_rl_cnt;
> + unsigned long flags;
> + s64 allocated_size;
>
> if (ni->type != AT_DATA)
> return -EOPNOTSUPP;
> @@ -5904,6 +5936,7 @@ int ntfs_non_resident_attr_collapse_range(struct ntfs_inode *ni, s64 start_vcn,
> ni->runlist.rl = rl;
> ni->runlist.count = new_rl_cnt;
>
> + write_lock_irqsave(&ni->size_lock, flags);
> ni->allocated_size -= ntfs_cluster_to_bytes(vol, len);
> if (ni->data_size > ntfs_cluster_to_bytes(vol, start_vcn)) {
> if (ni->data_size > ntfs_cluster_to_bytes(vol, (start_vcn + len)))
> @@ -5918,8 +5951,10 @@ int ntfs_non_resident_attr_collapse_range(struct ntfs_inode *ni, s64 start_vcn,
> else
> ni->initialized_size = ntfs_cluster_to_bytes(vol, start_vcn);
> }
> + allocated_size = ni->allocated_size;
> + write_unlock_irqrestore(&ni->size_lock, flags);
>
> - if (ni->allocated_size > 0) {
> + if (allocated_size > 0) {
> ret = ntfs_attr_update_mapping_pairs_locked(ni, 0, ni);
> if (ret) {
> up_write(&ni->runlist.lock);
> @@ -6029,6 +6064,7 @@ int ntfs_attr_fallocate(struct ntfs_inode *ni, loff_t start, loff_t byte_len, bo
> int err = 0;
> struct runlist_element *rl;
> bool balloc;
> + unsigned long flags;
>
> if (ni->type != AT_DATA)
> return -EINVAL;
> @@ -6076,7 +6112,9 @@ int ntfs_attr_fallocate(struct ntfs_inode *ni, loff_t start, loff_t byte_len, bo
> err = -EIO;
> goto out_unmap;
> }
> + write_lock_irqsave(&ni->size_lock, flags);
> ni->data_size = old_data_size;
> + write_unlock_irqrestore(&ni->size_lock, flags);
> i_size_write(VFS_I(ni), old_data_size);
> if (NInoNonResident(ni))
> ctx->attr->data.non_resident.data_size =
> diff --git a/fs/ntfs/file.c b/fs/ntfs/file.c
> index 612498b45fbb..956aad92cc72 100644
> --- a/fs/ntfs/file.c
> +++ b/fs/ntfs/file.c
> @@ -93,6 +93,7 @@ static int ntfs_trim_prealloc(struct inode *vi)
> s64 vcn_ds, vcn_tr;
> ssize_t rc;
> int err = 0;
> + unsigned long flags;
>
> inode_lock(vi);
> mutex_lock(&mrec_ni->mrec_lock);
> @@ -119,7 +120,9 @@ static int ntfs_trim_prealloc(struct inode *vi)
> ni->runlist.rl = NULL;
> ntfs_error(vol->sb, "Preallocated block rollback failed");
> } else {
> + write_lock_irqsave(&ni->size_lock, flags);
> ni->allocated_size = ntfs_cluster_to_bytes(vol, vcn_tr);
> + write_unlock_irqrestore(&ni->size_lock, flags);
> err = ntfs_attr_update_mapping_pairs_locked(
> ni, 0, ni);
> if (err)
> diff --git a/fs/ntfs/index.c b/fs/ntfs/index.c
> index f80cd42ed0d1..39e669b390e9 100644
> --- a/fs/ntfs/index.c
> +++ b/fs/ntfs/index.c
> @@ -1376,6 +1376,7 @@ static int ntfs_ir_reparent(struct ntfs_index_context *icx)
> u32 old_value_length;
> int ix_root_size;
> int ret = 0;
> + unsigned long flags;
>
> ntfs_debug("Entering\n");
>
> @@ -1431,9 +1432,11 @@ static int ntfs_ir_reparent(struct ntfs_index_context *icx)
> if (ret)
> goto resize_failed;
>
> + write_lock_irqsave(&icx->idx_ni->size_lock, flags);
> icx->idx_ni->data_size = ix_root_size;
> icx->idx_ni->initialized_size = ix_root_size;
> icx->idx_ni->allocated_size = (ix_root_size + 7) & ~7;
> + write_unlock_irqrestore(&icx->idx_ni->size_lock, flags);
> i_size_write(VFS_I(icx->idx_ni), ix_root_size);
> }
>
> @@ -1453,9 +1456,11 @@ static int ntfs_ir_reparent(struct ntfs_index_context *icx)
> if (ret)
> goto resize_failed;
>
> + write_lock_irqsave(&icx->idx_ni->size_lock, flags);
> icx->idx_ni->data_size = ix_root_size;
> icx->idx_ni->initialized_size = ix_root_size;
> icx->idx_ni->allocated_size = (ix_root_size + 7) & ~7;
> + write_unlock_irqrestore(&icx->idx_ni->size_lock, flags);
> i_size_write(VFS_I(icx->idx_ni), ix_root_size);
> }
> ntfs_ie_set_vcn(ie, new_ib_vcn);
> diff --git a/fs/ntfs/inode.c b/fs/ntfs/inode.c
> index 1fd3e5a72076..8bf504147517 100644
> --- a/fs/ntfs/inode.c
> +++ b/fs/ntfs/inode.c
> @@ -3856,18 +3856,22 @@ static inline int ntfs_enlarge_attribute(struct inode *vi, s64 pos, s64 count,
> struct super_block *sb = vi->i_sb;
> unsigned long flags;
> s64 old_data_size;
> + s64 new_initialized_size = pos + count;
> + s64 initialized_size;
> + s64 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
> + * Snapshot the old sizes under size_lock: data_size is the
> + * rollback target, initialized_size is compared below, the other
> + * sizes are updated by ntfs_attr_truncate() itself
> */
> read_lock_irqsave(&ni->size_lock, flags);
> old_data_size = ni->data_size;
> + initialized_size = ni->initialized_size;
> read_unlock_irqrestore(&ni->size_lock, flags);
>
> - if (pos + count <= ni->initialized_size)
> + if (new_initialized_size <= initialized_size)
> return 0;
>
> if (NInoEncrypted(ni) && NInoNonResident(ni))
> @@ -3876,7 +3880,7 @@ static inline int ntfs_enlarge_attribute(struct inode *vi, s64 pos, s64 count,
> if (NInoCompressed(ni))
> return -EOPNOTSUPP;
>
> - if (pos + count > old_data_size) {
> + if (new_initialized_size > old_data_size) {
> ret = ntfs_attr_truncate(ni, pos + count);
> if (ret) {
> ntfs_debug("Failed to truncate attribute");
> @@ -3942,25 +3946,39 @@ static inline int ntfs_enlarge_attribute(struct inode *vi, s64 pos, s64 count,
> }
> }
>
> + /* ntfs_attr_truncate() may have updated data_size, re-read it */
> + read_lock_irqsave(&ni->size_lock, flags);
> + data_size = ni->data_size;
> + read_unlock_irqrestore(&ni->size_lock, flags);
> +
> if (!NInoNonResident(ni)) {
> - if (likely(i_size_read(vi) < ni->data_size))
> - i_size_write(vi, ni->data_size);
> + if (likely(i_size_read(vi) < data_size))
> + i_size_write(vi, data_size);
> return 0;
> }
>
> /* Named streams initialize gaps after dropping the MFT record lock. */
> if (ntfs_inode_is_named_stream(ni)) {
> - if (i_size_read(vi) < ni->data_size)
> - i_size_write(vi, ni->data_size);
> + if (i_size_read(vi) < data_size)
> + i_size_write(vi, data_size);
> return 0;
> }
>
> - if (pos + count > ni->initialized_size) {
> + /* Re-read to avoid race after possible truncate */
> + read_lock_irqsave(&ni->size_lock, flags);
> + initialized_size = ni->initialized_size;
> + read_unlock_irqrestore(&ni->size_lock, flags);
> +
> + if (new_initialized_size > initialized_size) {
> ctx->attr->data.non_resident.initialized_size = cpu_to_le64(pos + count);
> mark_mft_record_dirty(ctx->ntfs_ino);
> +
> + write_lock_irqsave(&ni->size_lock, flags);
> ni->initialized_size = pos + count;
> - if (i_size_read(vi) < ni->initialized_size)
> - i_size_write(vi, ni->initialized_size);
> + write_unlock_irqrestore(&ni->size_lock, flags);
> +
> + if (i_size_read(vi) < new_initialized_size)
> + i_size_write(vi, new_initialized_size);
> }
> return 0;
> }
Looks good to me.
Reviewed-by: Baolin Liu <liubaolin@kylinos.cn>
^ permalink raw reply [flat|nested] 13+ messages in thread
* [PATCH v3 5/6] ntfs: serialize the mmap write fault initialized size update
2026-11-30 3:07 [PATCH v3 0/6] ntfs: synchronize the in-memory size handling Hongling Zeng
` (3 preceding siblings ...)
2026-11-30 3:07 ` [PATCH v3 4/6] ntfs: update the in-memory sizes under the size lock Hongling Zeng
@ 2026-11-30 3:07 ` Hongling Zeng
2026-11-30 3:49 ` liubaolin
2026-11-30 3:07 ` [PATCH v3 6/6] ntfs: read initialized_size under size_lock in the mmap prepare path Hongling Zeng
5 siblings, 1 reply; 13+ messages in thread
From: Hongling Zeng @ 2026-11-30 3:07 UTC (permalink / raw)
To: linkinjeon, hyc.lee, liubaolin12138
Cc: ntfs, linux-kernel, Hongling Zeng, stable, Baolin Liu
The mmap write fault path checks whether the faulting range extends
past ni->initialized_size with an unlocked read and then calls
ntfs_attr_set_initialized_size(), which rewrites the field and the
MFT record with its argument unconditionally. The call is made
without the MFT record lock that every other caller of the function
holds, and a buffered write or a second fault extending the
initialized size between the read and the rewrite can make the
rewrite shrink the field again, which exposes initialized data as
uninitialized.
Hold the MFT record lock across the check and the update and read
initialized_size under size_lock inside the critical section, so the
decision is made on the value that is current for the record and the
field can no longer regress through the fault path.
Fixes: b041ca562526 ("ntfs: update iomap and address space operations")
Cc: stable@vger.kernel.org
Signed-off-by: Hongling Zeng <zenghongling@kylinos.cn>
Reviewed-by: Baolin Liu <liubaolin@kylinos.cn>
---
fs/ntfs/attrib.c | 1 +
fs/ntfs/iomap.c | 15 +++++++++++----
2 files changed, 12 insertions(+), 4 deletions(-)
diff --git a/fs/ntfs/attrib.c b/fs/ntfs/attrib.c
index 081ce3e829f2..56df3049a386 100644
--- a/fs/ntfs/attrib.c
+++ b/fs/ntfs/attrib.c
@@ -2431,6 +2431,7 @@ int ntfs_attr_set(struct ntfs_inode *ni, s64 ofs, s64 cnt, const u8 val)
return ret;
}
+/* The caller must hold the base inode's mft record lock. */
int ntfs_attr_set_initialized_size(struct ntfs_inode *ni, loff_t new_size)
{
struct ntfs_attr_search_ctx *ctx;
diff --git a/fs/ntfs/iomap.c b/fs/ntfs/iomap.c
index a9c04fb964ba..f191bd5c444e 100644
--- a/fs/ntfs/iomap.c
+++ b/fs/ntfs/iomap.c
@@ -669,10 +669,17 @@ static int ntfs_write_da_iomap_begin_non_resident(struct inode *inode,
return err;
}
- if (ntfs_iomap_flags & NTFS_IOMAP_FLAGS_MKWRITE &&
- iomap->offset + iomap->length > ni->initialized_size) {
- err = ntfs_attr_set_initialized_size(ni,
- iomap->offset + iomap->length);
+ if (ntfs_iomap_flags & NTFS_IOMAP_FLAGS_MKWRITE) {
+ loff_t initialized_size;
+
+ mutex_lock(&mrec_ni->mrec_lock);
+ read_lock_irqsave(&ni->size_lock, irqflags);
+ initialized_size = ni->initialized_size;
+ read_unlock_irqrestore(&ni->size_lock, irqflags);
+ if (iomap->offset + iomap->length > initialized_size)
+ err = ntfs_attr_set_initialized_size(ni,
+ iomap->offset + iomap->length);
+ mutex_unlock(&mrec_ni->mrec_lock);
}
return err;
--
2.25.1
^ permalink raw reply [flat|nested] 13+ messages in thread* Re: [PATCH v3 5/6] ntfs: serialize the mmap write fault initialized size update
2026-11-30 3:07 ` [PATCH v3 5/6] ntfs: serialize the mmap write fault initialized size update Hongling Zeng
@ 2026-11-30 3:49 ` liubaolin
0 siblings, 0 replies; 13+ messages in thread
From: liubaolin @ 2026-11-30 3:49 UTC (permalink / raw)
To: Hongling Zeng, linkinjeon, hyc.lee; +Cc: ntfs, linux-kernel, stable, Baolin Liu
在 2026/11/30 11:07, Hongling Zeng 写道:
> The mmap write fault path checks whether the faulting range extends
> past ni->initialized_size with an unlocked read and then calls
> ntfs_attr_set_initialized_size(), which rewrites the field and the
> MFT record with its argument unconditionally. The call is made
> without the MFT record lock that every other caller of the function
> holds, and a buffered write or a second fault extending the
> initialized size between the read and the rewrite can make the
> rewrite shrink the field again, which exposes initialized data as
> uninitialized.
>
> Hold the MFT record lock across the check and the update and read
> initialized_size under size_lock inside the critical section, so the
> decision is made on the value that is current for the record and the
> field can no longer regress through the fault path.
>
> Fixes: b041ca562526 ("ntfs: update iomap and address space operations")
> Cc: stable@vger.kernel.org
> Signed-off-by: Hongling Zeng <zenghongling@kylinos.cn>
> Reviewed-by: Baolin Liu <liubaolin@kylinos.cn>
> ---
> fs/ntfs/attrib.c | 1 +
> fs/ntfs/iomap.c | 15 +++++++++++----
> 2 files changed, 12 insertions(+), 4 deletions(-)
>
> diff --git a/fs/ntfs/attrib.c b/fs/ntfs/attrib.c
> index 081ce3e829f2..56df3049a386 100644
> --- a/fs/ntfs/attrib.c
> +++ b/fs/ntfs/attrib.c
> @@ -2431,6 +2431,7 @@ int ntfs_attr_set(struct ntfs_inode *ni, s64 ofs, s64 cnt, const u8 val)
> return ret;
> }
>
> +/* The caller must hold the base inode's mft record lock. */
> int ntfs_attr_set_initialized_size(struct ntfs_inode *ni, loff_t new_size)
> {
> struct ntfs_attr_search_ctx *ctx;
> diff --git a/fs/ntfs/iomap.c b/fs/ntfs/iomap.c
> index a9c04fb964ba..f191bd5c444e 100644
> --- a/fs/ntfs/iomap.c
> +++ b/fs/ntfs/iomap.c
> @@ -669,10 +669,17 @@ static int ntfs_write_da_iomap_begin_non_resident(struct inode *inode,
> return err;
> }
>
> - if (ntfs_iomap_flags & NTFS_IOMAP_FLAGS_MKWRITE &&
> - iomap->offset + iomap->length > ni->initialized_size) {
> - err = ntfs_attr_set_initialized_size(ni,
> - iomap->offset + iomap->length);
> + if (ntfs_iomap_flags & NTFS_IOMAP_FLAGS_MKWRITE) {
> + loff_t initialized_size;
> +
> + mutex_lock(&mrec_ni->mrec_lock);
> + read_lock_irqsave(&ni->size_lock, irqflags);
> + initialized_size = ni->initialized_size;
> + read_unlock_irqrestore(&ni->size_lock, irqflags);
> + if (iomap->offset + iomap->length > initialized_size)
> + err = ntfs_attr_set_initialized_size(ni,
> + iomap->offset + iomap->length);
> + mutex_unlock(&mrec_ni->mrec_lock);
> }
>
> return err;
Looks good to me.
Reviewed-by: Baolin Liu <liubaolin@kylinos.cn>
^ permalink raw reply [flat|nested] 13+ messages in thread
* [PATCH v3 6/6] ntfs: read initialized_size under size_lock in the mmap prepare path
2026-11-30 3:07 [PATCH v3 0/6] ntfs: synchronize the in-memory size handling Hongling Zeng
` (4 preceding siblings ...)
2026-11-30 3:07 ` [PATCH v3 5/6] ntfs: serialize the mmap write fault initialized size update Hongling Zeng
@ 2026-11-30 3:07 ` Hongling Zeng
2026-11-30 3:50 ` liubaolin
5 siblings, 1 reply; 13+ messages in thread
From: Hongling Zeng @ 2026-11-30 3:07 UTC (permalink / raw)
To: linkinjeon, hyc.lee, liubaolin12138
Cc: ntfs, linux-kernel, Hongling Zeng, stable, Baolin Liu
ntfs_file_mmap_prepare() reads ni->initialized_size without the
size lock when deciding whether a shared writable mapping needs
its initialized size extended before the faults start. The field
is published under size_lock, so the read is a data race on the
protocol the other readers follow: a stale low value causes a
redundant call that __ntfs_extend_initialized_size() re-checks
under the lock, and a stale high value skips the extension and
leaves it to the write faults.
Take the snapshot under size_lock like the other readers.
Fixes: 9c87959601e8 ("ntfs: update file operations")
Cc: stable@vger.kernel.org
Signed-off-by: Hongling Zeng <zenghongling@kylinos.cn>
Reviewed-by: Baolin Liu <liubaolin@kylinos.cn>
---
fs/ntfs/file.c | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)
diff --git a/fs/ntfs/file.c b/fs/ntfs/file.c
index 956aad92cc72..d6b2fd186b92 100644
--- a/fs/ntfs/file.c
+++ b/fs/ntfs/file.c
@@ -1048,14 +1048,20 @@ int ntfs_file_mmap_prepare(struct vm_area_desc *desc)
if (vma_desc_test_all(desc, VMA_SHARED_BIT, VMA_MAYWRITE_BIT)) {
struct inode *inode = file_inode(file);
+ struct ntfs_inode *ni = NTFS_I(inode);
loff_t from, to;
+ loff_t initialized_size;
+ unsigned long flags;
int err;
from = ((loff_t)desc->pgoff << PAGE_SHIFT);
to = min_t(loff_t, i_size_read(inode),
from + desc->end - desc->start);
- if (NTFS_I(inode)->initialized_size < to) {
+ read_lock_irqsave(&ni->size_lock, flags);
+ initialized_size = ni->initialized_size;
+ read_unlock_irqrestore(&ni->size_lock, flags);
+ if (initialized_size < to) {
err = ntfs_extend_initialized_size(inode, to, to);
if (err)
return err;
--
2.25.1
^ permalink raw reply [flat|nested] 13+ messages in thread* Re: [PATCH v3 6/6] ntfs: read initialized_size under size_lock in the mmap prepare path
2026-11-30 3:07 ` [PATCH v3 6/6] ntfs: read initialized_size under size_lock in the mmap prepare path Hongling Zeng
@ 2026-11-30 3:50 ` liubaolin
0 siblings, 0 replies; 13+ messages in thread
From: liubaolin @ 2026-11-30 3:50 UTC (permalink / raw)
To: Hongling Zeng, linkinjeon, hyc.lee; +Cc: ntfs, linux-kernel, stable, Baolin Liu
在 2026/11/30 11:07, Hongling Zeng 写道:
> ntfs_file_mmap_prepare() reads ni->initialized_size without the
> size lock when deciding whether a shared writable mapping needs
> its initialized size extended before the faults start. The field
> is published under size_lock, so the read is a data race on the
> protocol the other readers follow: a stale low value causes a
> redundant call that __ntfs_extend_initialized_size() re-checks
> under the lock, and a stale high value skips the extension and
> leaves it to the write faults.
>
> Take the snapshot under size_lock like the other readers.
>
> Fixes: 9c87959601e8 ("ntfs: update file operations")
> Cc: stable@vger.kernel.org
> Signed-off-by: Hongling Zeng <zenghongling@kylinos.cn>
> Reviewed-by: Baolin Liu <liubaolin@kylinos.cn>
> ---
> fs/ntfs/file.c | 8 +++++++-
> 1 file changed, 7 insertions(+), 1 deletion(-)
>
> diff --git a/fs/ntfs/file.c b/fs/ntfs/file.c
> index 956aad92cc72..d6b2fd186b92 100644
> --- a/fs/ntfs/file.c
> +++ b/fs/ntfs/file.c
> @@ -1048,14 +1048,20 @@ int ntfs_file_mmap_prepare(struct vm_area_desc *desc)
>
> if (vma_desc_test_all(desc, VMA_SHARED_BIT, VMA_MAYWRITE_BIT)) {
> struct inode *inode = file_inode(file);
> + struct ntfs_inode *ni = NTFS_I(inode);
> loff_t from, to;
> + loff_t initialized_size;
> + unsigned long flags;
> int err;
>
> from = ((loff_t)desc->pgoff << PAGE_SHIFT);
> to = min_t(loff_t, i_size_read(inode),
> from + desc->end - desc->start);
>
> - if (NTFS_I(inode)->initialized_size < to) {
> + read_lock_irqsave(&ni->size_lock, flags);
> + initialized_size = ni->initialized_size;
> + read_unlock_irqrestore(&ni->size_lock, flags);
> + if (initialized_size < to) {
> err = ntfs_extend_initialized_size(inode, to, to);
> if (err)
> return err;
Looks good to me.
Reviewed-by: Baolin Liu <liubaolin@kylinos.cn>
^ permalink raw reply [flat|nested] 13+ messages in thread