Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C4E111F560B; Mon, 5 May 2025 09:18:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746436740; cv=none; b=MfFrXObe3c0jgKnni2czBGz2vlN1csbNA5SAkSey+AyWT4jRZX6nYUou1W5oEHfN9hDztV+1F2jB60+GPR75CE3X2HQS4XE6fTlr6Ng3nyvUZKNVxPHeuEcWO943M2cgCRVYYdBoH2Z28IjoVbZbRLWkSDLLYTLXaLGQibYXnxs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746436740; c=relaxed/simple; bh=pWpPAHPeIiiXKyqIHInvUH7mXokIvEPwOCHRL6podng=; h=Subject:To:Cc:From:Date:In-Reply-To:Message-ID:MIME-Version: Content-Type; b=fy8JzLYjBQoynceKy7mjgjnthC6N1vUvryZhjkqvqFgXZNqj+DB1VYUyhr8tv9P1/J4AdC6Tjc5TRkZEUBIkufC0sSCcMA2mOdUl/RC4Q6wZ0CEOC+BlluT3TBS0RMsiYGE83RM3dUzB0ksNuLCEMi3Vqq4CHFGHPqVqs5hpAho= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=Mv+WlwIf; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="Mv+WlwIf" Received: by smtp.kernel.org (Postfix) with ESMTPSA id D4713C4CEE4; Mon, 5 May 2025 09:18:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1746436739; bh=pWpPAHPeIiiXKyqIHInvUH7mXokIvEPwOCHRL6podng=; h=Subject:To:Cc:From:Date:In-Reply-To:From; b=Mv+WlwIfgmd886QMYsApJhf+ogYs1uyzJtHOdoQgdRFly7lzuB4womAg7oSLVIDSt B7Vkz8a8J3YZ4/LoqfFBgIwxmXw/jnq+lJIyKlAl1Hg6a3QsnnMVRn0wV7Ki9wvjoQ 9YPkf9LsA+KvGvNOw1J5ZxKBBrXLttUfBVAFapkM= Subject: Patch "xfs: convert delayed extents to unwritten when zeroing post eof blocks" has been added to the 6.1-stable tree To: catherine.hoang@oracle.com,chandan.babu@oracle.com,chandanbabu@kernel.org,david@fromorbit.com,djwong@kernel.org,gregkh@linuxfoundation.org,hch@lst.de,leah.rumancik@gmail.com,xfs-stable@lists.linux.dev,yi.zhang@huawei.com Cc: From: Date: Mon, 05 May 2025 11:18:24 +0200 In-Reply-To: <20250430212704.2905795-12-leah.rumancik@gmail.com> Message-ID: <2025050524-budget-banana-fcd5@gregkh> Precedence: bulk X-Mailing-List: xfs-stable@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=ANSI_X3.4-1968 Content-Transfer-Encoding: 8bit X-stable: commit X-Patchwork-Hint: ignore This is a note to let you know that I've just added the patch titled xfs: convert delayed extents to unwritten when zeroing post eof blocks to the 6.1-stable tree which can be found at: http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary The filename of the patch is: xfs-convert-delayed-extents-to-unwritten-when-zeroing-post-eof-blocks.patch and it can be found in the queue-6.1 subdirectory. If you, or anyone else, feels it should not be added to the stable tree, please let know about it. From stable+bounces-139238-greg=kroah.com@vger.kernel.org Wed Apr 30 23:32:16 2025 From: Leah Rumancik Date: Wed, 30 Apr 2025 14:26:58 -0700 Subject: xfs: convert delayed extents to unwritten when zeroing post eof blocks To: stable@vger.kernel.org Cc: xfs-stable@lists.linux.dev, chandan.babu@oracle.com, catherine.hoang@oracle.com, djwong@kernel.org, Zhang Yi , Dave Chinner , Christoph Hellwig , Chandan Babu R , Leah Rumancik Message-ID: <20250430212704.2905795-12-leah.rumancik@gmail.com> From: Zhang Yi [ Upstream commit 5ce5674187c345dc31534d2024c09ad8ef29b7ba ] Current clone operation could be non-atomic if the destination of a file is beyond EOF, user could get a file with corrupted (zeroed) data on crash. The problem is about preallocations. If you write some data into a file: [A...B) and XFS decides to preallocate some post-eof blocks, then it can create a delayed allocation reservation: [A.........D) The writeback path tries to convert delayed extents to real ones by allocating blocks. If there aren't enough contiguous free space, we can end up with two extents, the first real and the second still delalloc: [A....C)[C.D) After that, both the in-memory and the on-disk file sizes are still B. If we clone into the range [E...F) from another file: [A....C)[C.D) [E...F) then xfs_reflink_zero_posteof() calls iomap_zero_range() to zero out the range [B, E) beyond EOF and flush it. Since [C, D) is still a delalloc extent, its pagecache will be zeroed and both the in-memory and on-disk size will be updated to D after flushing but before cloning. This is wrong, because the user can see the size change and read the zeroes while the clone operation is ongoing. We need to keep the in-memory and on-disk size before the clone operation starts, so instead of writing zeroes through the page cache for delayed ranges beyond EOF, we convert these ranges to unwritten and invalidate any cached data over that range beyond EOF. Suggested-by: Dave Chinner Signed-off-by: Zhang Yi Reviewed-by: "Darrick J. Wong" Reviewed-by: Christoph Hellwig Signed-off-by: Chandan Babu R Signed-off-by: Leah Rumancik Acked-by: "Darrick J. Wong" Signed-off-by: Greg Kroah-Hartman --- fs/xfs/xfs_iomap.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) --- a/fs/xfs/xfs_iomap.c +++ b/fs/xfs/xfs_iomap.c @@ -997,6 +997,24 @@ xfs_buffered_write_iomap_begin( } /* + * For zeroing, trim a delalloc extent that extends beyond the EOF + * block. If it starts beyond the EOF block, convert it to an + * unwritten extent. + */ + if ((flags & IOMAP_ZERO) && imap.br_startoff <= offset_fsb && + isnullstartblock(imap.br_startblock)) { + xfs_fileoff_t eof_fsb = XFS_B_TO_FSB(mp, XFS_ISIZE(ip)); + + if (offset_fsb >= eof_fsb) + goto convert_delay; + if (end_fsb > eof_fsb) { + end_fsb = eof_fsb; + xfs_trim_extent(&imap, offset_fsb, + end_fsb - offset_fsb); + } + } + + /* * Search the COW fork extent list even if we did not find a data fork * extent. This serves two purposes: first this implements the * speculative preallocation using cowextsize, so that we also unshare @@ -1138,6 +1156,17 @@ found_imap: xfs_iunlock(ip, lockmode); return xfs_bmbt_to_iomap(ip, iomap, &imap, flags, 0, seq); +convert_delay: + xfs_iunlock(ip, lockmode); + truncate_pagecache(inode, offset); + error = xfs_bmapi_convert_delalloc(ip, XFS_DATA_FORK, offset, + iomap, NULL); + if (error) + return error; + + trace_xfs_iomap_alloc(ip, offset, count, XFS_DATA_FORK, &imap); + return 0; + found_cow: seq = xfs_iomap_inode_sequence(ip, 0); if (imap.br_startoff <= offset_fsb) { Patches currently in stable-queue which might be from leah.rumancik@gmail.com are queue-6.1/xfs-revert-commit-44af6c7e59b12.patch queue-6.1/xfs-make-the-seq-argument-to-xfs_bmapi_convert_delalloc-optional.patch queue-6.1/xfs-check-opcode-and-iovec-count-match-in-xlog_recover_attri_commit_pass2.patch queue-6.1/xfs-allow-symlinks-with-short-remote-targets.patch queue-6.1/xfs-match-lock-mode-in-xfs_buffered_write_iomap_begin.patch queue-6.1/xfs-require-xfs_sb_feat_incompat_log_xattrs-for-attr-log-intent-item-recovery.patch queue-6.1/xfs-allow-unlinked-symlinks-and-dirs-with-zero-size.patch queue-6.1/xfs-restrict-when-we-try-to-align-cow-fork-delalloc-to-cowextsz-hints.patch queue-6.1/xfs-fix-xfs_bmap_add_extent_delay_real-for-partial-conversions.patch queue-6.1/xfs-validate-recovered-name-buffers-when-recovering-xattr-items.patch queue-6.1/xfs-make-xfs_bmapi_convert_delalloc-to-allocate-the-target-offset.patch queue-6.1/xfs-convert-delayed-extents-to-unwritten-when-zeroing-post-eof-blocks.patch queue-6.1/xfs-fix-freeing-speculative-preallocations-for-preallocated-files.patch queue-6.1/xfs-make-sure-sb_fdblocks-is-non-negative.patch queue-6.1/xfs-remove-a-racy-if_bytes-check-in-xfs_reflink_end_cow_extent.patch queue-6.1/xfs-fix-error-returns-from-xfs_bmapi_write.patch