From: Chao Yu <chao@kernel.org>
To: jaegeuk@kernel.org
Cc: linux-f2fs-devel@lists.sourceforge.net,
linux-kernel@vger.kernel.org, Chao Yu <chao@kernel.org>,
stable@vger.kernel.org, Daeho Jeong <daehojeong@google.com>
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 [thread overview]
Message-ID: <20261201110842.1825797-11-chao@kernel.org> (raw)
In-Reply-To: <20261201110842.1825797-7-chao@kernel.org>
From: Chao Yu <chao@kernel.org>
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 <daehojeong@google.com>
Signed-off-by: Chao Yu <chao@kernel.org>
---
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
next prev parent reply other threads:[~2026-12-01 11:08 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-01 11:08 [PATCH 1/6] f2fs: fix to clear SBI_IS_CLOSE in __f2fs_remount() Chao Yu
2026-12-01 11:08 ` [PATCH 2/6] f2fs: quota: fix to recheck quota flush after taking node_change in checkpoint Chao Yu
2026-12-01 11:08 ` [PATCH 3/6] f2fs: cache: fix to submit cached DATA bio when waiting writeback of meta cache Chao Yu
2026-12-01 11:08 ` [PATCH 4/6] f2fs: atomic: fix to keep committed data of atomic file in f2fs_write_failed() Chao Yu
2026-12-01 11:08 ` Chao Yu [this message]
2026-12-01 11:08 ` [PATCH 6/6] f2fs: atomic: fix to disallow shrinking atomic file via truncate Chao Yu
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=20261201110842.1825797-11-chao@kernel.org \
--to=chao@kernel.org \
--cc=daehojeong@google.com \
--cc=jaegeuk@kernel.org \
--cc=linux-f2fs-devel@lists.sourceforge.net \
--cc=linux-kernel@vger.kernel.org \
--cc=stable@vger.kernel.org \
/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®