From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=117.135.210.3 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796010590; b=mCWaK6KzAWfLV2kBGTMSqZEC4wBGgmFOLUs/G2vzRezCCWLY7EmCuXJyUeSt9iOHmYyA dqGaT2WS1BIlzkAmnKn9B4Mdw0pRJebfjJXpSAqTesn0h6/6ZBMWJc27p38mmlv5qVL5p 6P12kfxWrO7h2Ts1EZMRrC6GB5F/R3m2zOlLd9PTV1PlYU27rBtNNShrOIxwZ6ORB6jRc 5UayG+eyhBK+tMRGmQ+aq/BtiUy/x/MM07cfkOnnibcv93KS14jTOI3Fk+5EhThM8ExVf iL01ARsouGZyLIX+mWAlbzA+z0Ys/F00xG0d+IguSpsnYf/QBuixFcjRb25zNam8O+w== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796010590; h=DMARC-Filter:DKIM-Signature:Received:Message-ID:Date:MIME-Version: User-Agent:Subject:To:Cc:References:Content-Language:From:In-Reply-To: Content-Type:Content-Transfer-Encoding:X-CM-TRANSID: X-Coremail-Antispam:X-Originating-IP:X-CM-SenderInfo; bh=KtcGe1eZzGXgxJgD7as4IqVUhQ30ksSuPOg17LbGFYk=; b=ZyPWDPSJ05RHfGozj6kgW8eOB8c9xzdA3AjdkzVx4iW/hknsxyePI62hVVvMaQIKpgA2 gFXrICx3LieClHOjaxbIoXaS6xhwuqG75qzkf8wD1DnstU/W+ANlWzvAbfBeVsNrwlt8p EnYdAH39bkxfppoFAEYJBVlO5WH6qLJy1Tif3BGSKoL8r2P7gk8X9RaJnh/qKh+V+35Vz hpzFRPtK6GmCa40OUyA+9vegboXXvMo2F53yCjlRdgt9YZtJAbOdk24JuRmAl4gZ9kccO w21//uiSKcCgEGoR4LiudmCRraBwELKME2FXfbeKmMgE0Cs4MnMwLZqRua7IwZwYdow== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=163.com; spf=pass smtp.mailfrom=163.com; dkim=pass header.d=163.com header.i=@163.com header.a=rsa-sha256 header.s=s110527 header.b=VCLxhg9G; arc=none smtp.remote-ip=117.135.210.3 Received: from m16.mail.163.com (m16.mail.163.com [117.135.210.3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2FAE3606A329; Mon, 30 Nov 2026 03:49:48 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 2FAE3606A329 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=163.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.a=rsa-sha256 header.s=s110527 header.b=VCLxhg9G DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=Message-ID:Date:MIME-Version:Subject:To:From: Content-Type; bh=KtcGe1eZzGXgxJgD7as4IqVUhQ30ksSuPOg17LbGFYk=; b=VCLxhg9GxyjVg9wAxPNaDgnQioL9dxgve1jbeB7nxQmSXImW2kdwotRdQlF18/ UnSRVLISAQfhvmH5Wy7qNJ9dkmDyyclBnVo+F+r8qY6mQIsShlQnLESkKFdJscIG vsDdSwFJwivddTWkPyHHnRUWwTrbnY2+zm63LQHE830Bc= Received: from [IPV6:2409:8900:6ae:3295:bdf5:7149:7d26:d2ca] (unknown []) by gzga-smtp-mtada-g1-4 (Coremail) with SMTP id _____wD3f18+8gxrPqntKw--.22515S2; Mon, 30 Nov 2026 11:49:19 +0800 (CST) Message-ID: Date: Mon, 30 Nov 2026 11:49:18 +0800 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v3 2/6] ntfs: roll back the size update when the attribute lookup fails To: Hongling Zeng , linkinjeon@kernel.org, hyc.lee@gmail.com Cc: ntfs@lists.linux.dev, linux-kernel@vger.kernel.org, stable@vger.kernel.org References: <20261130030708.59798-1-zenghongling@kylinos.cn> <20261130030708.59798-3-zenghongling@kylinos.cn> Content-Language: en-US From: liubaolin In-Reply-To: <20261130030708.59798-3-zenghongling@kylinos.cn> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-CM-TRANSID:_____wD3f18+8gxrPqntKw--.22515S2 X-Coremail-Antispam: 1Uf129KBjvJXoW3Xry5KrWDtrW8WFWDCry3urg_yoW3XrWrpr y7CF1ay3yDZ3s7Grn7K3WYqw1Y93yv9r4UuFy2q3WfAa4DGw1IqF1rKrWxKa92yr95trs0 qF4qgrZ7Cr1qvFJanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07jeGQhUUUUU= X-CM-SenderInfo: xolxutxrol0iasrtmqqrwthudrp/xtbC6h+pdGsM8j-vpgAA3L 在 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 > --- > 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