From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796123336; b=CvEa/6yZdX68tfxopddk8aETLMBVbDUxmZbAjY6GcvyYZEZjpdXCiRzJbw82UG4j+fAb eXdQovUNBwfBmsj7FQp4/0SgAH3mJnzpBw22M1BCAV9TU3nT1r7/DNz3pHNmWscWyf3Fo wu0g4fSn+LdwVAAHtzsNOEG/tQXIgetX5PSmvbyOoH7KvzTWV5c2/7yK22DHCqVcUkMEN K03Fgqs5KclrPGFuay8IrWN26CELQaK5zEC7dCQp62KfHy1Q+qGgWDBuSAyTmmFH7s60k GtWxD+gaZA8NtZMlFrYE7lQjscIq3+V/XSZxZdPqQ7nWf8WCn1qUUsBll/RLn1Q9Kkw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796123336; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=p9h3JZWpFOjH1n1WM3ajTQs/gVvR8y+D9wzd97wq6zw=; b=hzB7p4Ou+ZQ9gWy8/+mTRFkuHzEqotPCJzFC3CV2uX56L/M+tL8ni80XWqmzew7E8Oh+ xN8dYL3pavpj6PAv0ZywqqShWaBFeg++ncpz4wYSemgXDhkAqRl8crpkBDlsPAivNYQeC YLdSFRpwtt0q5l76Ts9ii+CFpgAO5qzpg5cS220AX8+ssQEXvPSoY0qdtFCuHIWBLXaom BWoHXPrV7/b6T5HyDvJhru5AWEd85TvkzdVyhPlLMV91BXVmGzRzku6nbIUZA9jq4VhYj KCxmmVpP0cM/oDmiSsZDU3Iip08QXdIaH7n774CuK2mpj7VB/eHhdkYUhIMhL4aD4Jw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=G+5abK7Z; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 68C8A606A33A; Tue, 1 Dec 2026 11:08:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=G+5abK7Z Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0278D1F00898; Tue, 1 Dec 2026 11:08:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796123336; bh=p9h3JZWpFOjH1n1WM3ajTQs/gVvR8y+D9wzd97wq6zw=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=G+5abK7ZchxoX5eroo6K9yHeAC79nBCMo7MGDDq762QHAJdr+vi78hccav2upoV7p c4+DF+PgSOTb6Sqqg0IEA9Dlg4NM7FQb1ghhlL9nEN6tbBIsvy5niHM82YKXe06hrP y2hI9DNScFc+VQr39RwV2dyQPxCzuRmVJzAit5QfRnGu2KcVzxKdQEMLzZHCcWAH4g LfhOjc6bQHz7oHlt974mOzF4Hhvk8qbbJcsZjAwJ1EqR6Eghm3YpAym+uJyzwGNLTP HwAKuvBPC31mef71OvnQzTV3jM7jvLLYjP7WIqga+IX1VtXNu9Rd9OKg7ffvVLGCCF bNQcVTLkqrgXQ== From: Chao Yu To: jaegeuk@kernel.org Cc: linux-f2fs-devel@lists.sourceforge.net, linux-kernel@vger.kernel.org, Chao Yu , stable@vger.kernel.org, Daeho Jeong Subject: [PATCH 5/6] f2fs: atomic: fix to avoid preallocating blocks in original inode for atomic file Date: Tue, 1 Dec 2026 11:08:47 +0000 Message-ID: <20261201110842.1825797-11-chao@kernel.org> X-Mailer: git-send-email 2.56.1.839.g22a8290db9-goog In-Reply-To: <20261201110842.1825797-7-chao@kernel.org> References: <20261201110842.1825797-7-chao@kernel.org> X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Chao Yu f2fs_file_write_iter() preallocates blocks in the original inode via f2fs_preallocate_blocks(), and calls f2fs_truncate() to free blocks beyond i_size if the write fails or is short. However, blocks of an atomic write are reserved in the COW inode in ->write_begin(), so preallocating blocks in the original inode is useless, and causes two bugs: 1. In atomic replace mode, i_size of the original inode is reset to zero, so f2fs_truncate() after a failed write frees committed data, even w/ commit "f2fs: atomic: fix to keep committed data of atomic file in f2fs_write_failed()", e.g. w/ 4KB blocks: write 512KB, fsync committed blocks 0..127 f2fs_ioc_start_atomic_write(truncate=true) original_i_size = 512KB, i_size = 0 pwrite(0, 100000) written to COW inode pwrite(600000, 8192) f2fs_file_write_iter f2fs_preallocate_blocks preallocated > 0 f2fs_buffered_write_iter f2fs_write_begin -ENOMEM (FAULT_PAGE_GET) f2fs_write_failed keeps committed data f2fs_truncate(inode) f2fs_truncate_blocks(inode, 100000) frees committed blocks 25..127 f2fs_ioc_abort_atomic_write i_size = original_i_size (512KB) After the abort, 103 of 128 blocks of the original file are zeroes. 2. In non-replace mode, blocks preallocated by a write beyond EOF are left in the original inode after the atomic write is aborted: write 64KB, fsync ioctl(F2FS_IOC_START_ATOMIC_WRITE) pwrite(65536, 32768) ioctl(F2FS_IOC_ABORT_ATOMIC_WRITE) st_blocks of the file grows from 128 to 192, w/ 4KB or 16KB blocks. Fix it by skipping f2fs_preallocate_blocks() for atomic files, then the original data is intact in 1., and st_blocks stays 128 in 2. Fixes: 3db1de0e582c ("f2fs: change the current atomic write way") Fixes: 41e8f85a75fc ("f2fs: introduce F2FS_IOC_START_ATOMIC_REPLACE") Cc: stable@vger.kernel.org Cc: Daeho Jeong Signed-off-by: Chao Yu --- fs/f2fs/file.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c index 10c198117f57..5ad772a41f9b 100644 --- a/fs/f2fs/file.c +++ b/fs/f2fs/file.c @@ -5747,6 +5747,10 @@ static int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *iter, int flag; int ret; + /* atomic write reserves blocks in COW inode in ->write_begin() */ + if (f2fs_is_atomic_file(inode)) + return 0; + /* If it will be an out-of-place direct write, don't bother. */ if (dio && f2fs_lfs_mode(sbi)) return 0; -- 2.49.0 From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from lists.sourceforge.net (lists.sourceforge.net [216.105.38.7]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 01CBB606A31A for ; Tue, 1 Dec 2026 11:09:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=lists.sourceforge.net header.i=@lists.sourceforge.net header.a=rsa-sha256 header.s=beta header.b=kSLQ7sNt; dkim=fail reason="signature verification failed" (1024-bit key) header.d=sourceforge.net header.i=@sourceforge.net header.a=rsa-sha256 header.s=x header.b=X3OZZclw; dkim=fail reason="signature verification failed" (1024-bit key) header.d=sf.net header.i=@sf.net header.a=rsa-sha256 header.s=x header.b=SxLfwYX2; dkim=neutral header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=G+5abK7Z DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.sourceforge.net; s=beta; h=Content-Transfer-Encoding:Content-Type:Cc: Reply-To:From:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:Subject:MIME-Version:References:In-Reply-To: Message-ID:Date:To:Sender:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Unsubscribe-Post:List-Owner; bh=De7lp+2BknkVYZIT/0jZODE1scz6Uw1IMbTnm02R7Bk=; b=kSLQ7sNt4hPsfDQ9kt/lvxx3mZ Bs229blbnCLdfnv49DoFadvvgHqdN9vFCSr7N2DGH2ggozHxKhhAZI+Z968caoAfhb2t7Z5McRaDb 4xLxkLNhGFajTJhm4B5nsYXjGYHAxQ/G3NVqshSODYk3JYOFwDodQ22U5+vDUzykrqRA=; Received: from [127.0.0.1] (helo=sfs-ml-2.v29.lw.sourceforge.com) by sfs-ml-2.v29.lw.sourceforge.com with esmtp (Exim 4.100.1) (envelope-from ) id 1xYLjb-000000006I7-34Tg; Tue, 01 Dec 2026 11:09:19 +0000 Received: from [172.30.29.66] (helo=mx.sourceforge.net) by sfs-ml-2.v29.lw.sourceforge.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.100.1) (envelope-from ) id 1xYLjL-000000006Hh-29X8 for linux-f2fs-devel@lists.sourceforge.net; Tue, 01 Dec 2026 11:09:03 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=Content-Transfer-Encoding:MIME-Version:References: In-Reply-To:Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Unsubscribe-Post:List-Subscribe:List-Post:List-Owner:List-Archive; bh=p9h3JZWpFOjH1n1WM3ajTQs/gVvR8y+D9wzd97wq6zw=; b=X3OZZclwTgJ1uG7DbkUnKNRyOS 3ZF6E+r9FbGEPzStzDSywqPbcGAujd+AjI+R67kq6HsxxUX58DbZtK3KTlvEaGUVMgqQSE1UxDqia XkXWgi4EkFh3hKTPvXUJWYVQPuMdpdKidVMoMf5LOX788Xc6lBlTDsuOh3bhnfYmKDZM=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-ID: Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Unsubscribe-Post: List-Subscribe:List-Post:List-Owner:List-Archive; bh=p9h3JZWpFOjH1n1WM3ajTQs/gVvR8y+D9wzd97wq6zw=; b=SxLfwYX2BpHCo6gG4NLs5+Hc2y qaOlNH8UUhKKbcSoBJc3qfZRAxc5sEFV/eHNJTutZyuJsKoPfyYMIWhkgl7HUcr6kigLhn+YfqyKR 5kmxCUYl3oQ9o0Pi40yTiuG38BXoeFaEuA/U12Ww9KXgHvc+Fis3YdixF59mM74yRCu4=; Received: from tor.source.kernel.org ([172.105.4.254]) by sfi-mx-2.v28.lw.sourceforge.com with esmtps (TLS1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.100.1) id 1xYLjJ-000000005YU-3DgT for linux-f2fs-devel@lists.sourceforge.net; Tue, 01 Dec 2026 11:09:03 +0000 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by tor.source.kernel.org (Postfix) with ESMTP id 2D6D76249A; Tue, 1 Dec 2026 11:08:56 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0278D1F00898; Tue, 1 Dec 2026 11:08:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796123336; bh=p9h3JZWpFOjH1n1WM3ajTQs/gVvR8y+D9wzd97wq6zw=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=G+5abK7ZchxoX5eroo6K9yHeAC79nBCMo7MGDDq762QHAJdr+vi78hccav2upoV7p c4+DF+PgSOTb6Sqqg0IEA9Dlg4NM7FQb1ghhlL9nEN6tbBIsvy5niHM82YKXe06hrP y2hI9DNScFc+VQr39RwV2dyQPxCzuRmVJzAit5QfRnGu2KcVzxKdQEMLzZHCcWAH4g LfhOjc6bQHz7oHlt974mOzF4Hhvk8qbbJcsZjAwJ1EqR6Eghm3YpAym+uJyzwGNLTP HwAKuvBPC31mef71OvnQzTV3jM7jvLLYjP7WIqga+IX1VtXNu9Rd9OKg7ffvVLGCCF bNQcVTLkqrgXQ== To: jaegeuk@kernel.org Date: Tue, 1 Dec 2026 11:08:47 +0000 Message-ID: <20261201110842.1825797-11-chao@kernel.org> X-Mailer: git-send-email 2.56.1.839.g22a8290db9-goog In-Reply-To: <20261201110842.1825797-7-chao@kernel.org> References: <20261201110842.1825797-7-chao@kernel.org> X-Archived-By: MIME-Version: 1.0 X-Spam-Score: -0.2 (/) X-Headers-End: 1xYLjJ-000000005YU-3DgT Subject: [f2fs-dev] [PATCH 5/6] f2fs: atomic: fix to avoid preallocating blocks in original inode for atomic file X-BeenThere: linux-f2fs-devel@lists.sourceforge.net X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Archive: List-Post: List-Help: List-Subscribe: , From: Chao Yu via Linux-f2fs-devel Reply-To: Chao Yu Cc: Daeho Jeong , linux-kernel@vger.kernel.org, stable@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-19.1 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS,SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 From: Chao Yu f2fs_file_write_iter() preallocates blocks in the original inode via f2fs_preallocate_blocks(), and calls f2fs_truncate() to free blocks beyond i_size if the write fails or is short. However, blocks of an atomic write are reserved in the COW inode in ->write_begin(), so preallocating blocks in the original inode is useless, and causes two bugs: 1. In atomic replace mode, i_size of the original inode is reset to zero, so f2fs_truncate() after a failed write frees committed data, even w/ commit "f2fs: atomic: fix to keep committed data of atomic file in f2fs_write_failed()", e.g. w/ 4KB blocks: write 512KB, fsync committed blocks 0..127 f2fs_ioc_start_atomic_write(truncate=true) original_i_size = 512KB, i_size = 0 pwrite(0, 100000) written to COW inode pwrite(600000, 8192) f2fs_file_write_iter f2fs_preallocate_blocks preallocated > 0 f2fs_buffered_write_iter f2fs_write_begin -ENOMEM (FAULT_PAGE_GET) f2fs_write_failed keeps committed data f2fs_truncate(inode) f2fs_truncate_blocks(inode, 100000) frees committed blocks 25..127 f2fs_ioc_abort_atomic_write i_size = original_i_size (512KB) After the abort, 103 of 128 blocks of the original file are zeroes. 2. In non-replace mode, blocks preallocated by a write beyond EOF are left in the original inode after the atomic write is aborted: write 64KB, fsync ioctl(F2FS_IOC_START_ATOMIC_WRITE) pwrite(65536, 32768) ioctl(F2FS_IOC_ABORT_ATOMIC_WRITE) st_blocks of the file grows from 128 to 192, w/ 4KB or 16KB blocks. Fix it by skipping f2fs_preallocate_blocks() for atomic files, then the original data is intact in 1., and st_blocks stays 128 in 2. Fixes: 3db1de0e582c ("f2fs: change the current atomic write way") Fixes: 41e8f85a75fc ("f2fs: introduce F2FS_IOC_START_ATOMIC_REPLACE") Cc: stable@vger.kernel.org Cc: Daeho Jeong Signed-off-by: Chao Yu --- fs/f2fs/file.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c index 10c198117f57..5ad772a41f9b 100644 --- a/fs/f2fs/file.c +++ b/fs/f2fs/file.c @@ -5747,6 +5747,10 @@ static int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *iter, int flag; int ret; + /* atomic write reserves blocks in COW inode in ->write_begin() */ + if (f2fs_is_atomic_file(inode)) + return 0; + /* If it will be an out-of-place direct write, don't bother. */ if (dio && f2fs_lfs_mode(sbi)) return 0; -- 2.49.0 _______________________________________________ Linux-f2fs-devel mailing list Linux-f2fs-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel