[PATCH RESEND 5/9] f2fs: truncate blocks allocated past EOF by a failed direct write
From: Jan Prusakowski
Date: Thu Nov 12 2026 - 10:10:19 EST
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().
The extending direct write is always synchronous (IOMAP_DIO_FORCE_WAIT),
so the result is known before f2fs_file_write_iter() returns.
Fixes: d4dd19ec1ea0 ("f2fs: do not expose unwritten blocks to user by DIO")
Assisted-by: LLM
Signed-off-by: Jan Prusakowski <jprusakowski@xxxxxxxxxx>
---
fs/f2fs/data.c | 10 +++++++++-
fs/f2fs/file.c | 8 ++++++--
2 files changed, 15 insertions(+), 3 deletions(-)
diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c
index 4ae61138a003..e978b6b18c26 100644
--- a/fs/f2fs/data.c
+++ b/fs/f2fs/data.c
@@ -2171,7 +2171,15 @@ int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map, int flag)
err = __allocate_data_block(&dn, map->m_seg_type);
if (err)
goto sync_out;
- if (flag == F2FS_GET_BLOCK_PRE_DIO)
+ /*
+ * Blocks allocated past EOF must be truncated if the
+ * write fails. Don't do it for KEEP_SIZE files, where
+ * truncation would also drop the fallocated blocks.
+ */
+ if (flag == F2FS_GET_BLOCK_PRE_DIO ||
+ (flag == F2FS_GET_BLOCK_DIO &&
+ !file_keep_isize(inode) &&
+ pgofs >= F2FS_BLK_ALIGN(sbi, i_size_read(inode))))
file_need_truncate(inode);
set_inode_flag(inode, FI_APPEND_WRITE);
break;
diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
index b67782043396..3fcea85c36a2 100644
--- a/fs/f2fs/file.c
+++ b/fs/f2fs/file.c
@@ -5896,8 +5896,12 @@ static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
}
}
- /* Don't leave any preallocated blocks around past i_size. */
- if (preallocated && i_size_read(inode) < target_size) {
+ /*
+ * Don't leave any preallocated blocks, or blocks allocated by a failed
+ * direct write, around past i_size.
+ */
+ if ((preallocated || file_should_truncate(inode)) &&
+ i_size_read(inode) < target_size) {
f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
filemap_invalidate_lock(inode->i_mapping);
if (!f2fs_truncate(inode))
--
2.56.0.567.gdb80ae8cd5-goog