Re: [PATCH RESEND 5/9] f2fs: truncate blocks allocated past EOF by a failed direct write
From: Christoph Hellwig
Date: Fri Nov 20 2026 - 11:08:26 EST
On Thu, Nov 12, 2026 at 02:33:51PM +0000, Jan Prusakowski wrote:
> When a direct write extends a file, f2fs_map_blocks() allocates the
> blocks past EOF and maps them into the dnode before the bio is
> submitted. If the write then fails, i_size is not updated, but the
> blocks stay mapped past EOF with whatever the disk contained before.
> A later extending write or truncate can make them visible.
>
> f2fs_preallocate_blocks() already handles this for blocks it
> preallocates with F2FS_GET_BLOCK_PRE_DIO: it marks the inode with
> file_need_truncate() and f2fs_file_write_iter() truncates everything
> past i_size after a short write. But the preallocation is skipped in
> LFS mode and when the user buffer can't be faulted in, and then the
> blocks are allocated by F2FS_GET_BLOCK_DIO from f2fs_iomap_begin() and
> nothing truncates them.
>
> Mark the inode with file_need_truncate() also when F2FS_GET_BLOCK_DIO
> allocates a block past EOF, and truncate after the write if the flag is
> set and the file is shorter than intended. Skip KEEP_SIZE files, where
> the truncation would also drop the blocks reserved by fallocate().
None of this fixes the issue in case the system crashes between these
ѕteps. f2fs really needs to grow unwritten extent support or find some
other way to not add the mapping until I/O completion, otherwise this
remains broken and exploitable.