mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: liubaolin <liubaolin12138@163.com>
To: Hongling Zeng <zenghongling@kylinos.cn>,
	linkinjeon@kernel.org, hyc.lee@gmail.com
Cc: ntfs@lists.linux.dev, linux-kernel@vger.kernel.org,
	stable@vger.kernel.org
Subject: Re: [PATCH v3 2/6] ntfs: roll back the size update when the attribute lookup fails
Date: Mon, 30 Nov 2026 11:49:18 +0800	[thread overview]
Message-ID: <a4e85321-9713-4a31-b536-0122d06d2200@163.com> (raw)
In-Reply-To: <20261130030708.59798-3-zenghongling@kylinos.cn>



在 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>


  reply	other threads:[~2026-11-30  3:49 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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:49   ` liubaolin
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 [this message]
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
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
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
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=a4e85321-9713-4a31-b536-0122d06d2200@163.com \
    --to=liubaolin12138@163.com \
    --cc=hyc.lee@gmail.com \
    --cc=linkinjeon@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ntfs@lists.linux.dev \
    --cc=stable@vger.kernel.org \
    --cc=zenghongling@kylinos.cn \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®