Re: [f2fs-dev] [PATCH 5/6] f2fs: atomic: fix to avoid preallocating blocks in original inode for atomic file
From: Daeho Jeong
Date: Thu Dec 03 2026 - 13:08:09 EST
On Tue, Dec 1, 2026 at 3:10 AM Chao Yu via Linux-f2fs-devel
<linux-f2fs-devel@xxxxxxxxxxxxxxxxxxxxx> wrote:
>
> From: Chao Yu <chao@xxxxxxxxxx>
>
> 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@xxxxxxxxxxxxxxx
> Cc: Daeho Jeong <daehojeong@xxxxxxxxxx>
> Signed-off-by: Chao Yu <chao@xxxxxxxxxx>
> ---
> 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
>
>
Reviewed-by: Daeho Jeong <daehojeong@xxxxxxxxxx>
Thanks,
>
> _______________________________________________
> Linux-f2fs-devel mailing list
> Linux-f2fs-devel@xxxxxxxxxxxxxxxxxxxxx
> https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel