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: linux-kernel@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