[PATCH RESEND 3/9] f2fs: keep old block address when out-of-place write fails to allocate

From: Jan Prusakowski

Date: Thu Nov 12 2026 - 09:57:13 EST


If f2fs_allocate_data_block() fails, do_write_block() ends writeback of
the folio without submitting it and f2fs_allocate_data_block() sets
fio->new_blkaddr to NULL_ADDR. f2fs_outplace_write_data() then still
calls f2fs_update_data_blkaddr() with that address, which replaces the
old (or reserved NEW_ADDR) block address in the dnode with NULL_ADDR.
The block stays accounted in i_blocks and valid_block_count, so the
in-memory metadata becomes inconsistent and the old data becomes
unreachable.

Only update the dnode when the write was actually submitted.

Allocation only fails after the checkpoint has been stopped with
CP_ERROR_FLAG, so this does not reach the disk, but it keeps the
in-memory state consistent for later reads and for code that rolls back
failed writes.

Fixes: 7d009e048d7c ("f2fs: fix to handle segment allocation failure correctly")
Assisted-by: LLM
Signed-off-by: Jan Prusakowski <jprusakowski@xxxxxxxxxx>
---
fs/f2fs/segment.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/fs/f2fs/segment.c b/fs/f2fs/segment.c
index a5f9de723a70..a26cde2dc60e 100644
--- a/fs/f2fs/segment.c
+++ b/fs/f2fs/segment.c
@@ -4257,7 +4257,12 @@ void f2fs_outplace_write_data(struct dnode_of_data *dn,
f2fs_update_age_extent_cache(dn);
set_summary(&sum, dn->nid, dn->ofs_in_node, fio->version);
do_write_block(&sum, fio);
- f2fs_update_data_blkaddr(dn, fio->new_blkaddr);
+ /*
+ * If block allocation failed, fio->new_blkaddr is NULL_ADDR and the
+ * folio was not submitted, so keep the old block address in the dnode.
+ */
+ if (likely(fio->submitted))
+ f2fs_update_data_blkaddr(dn, fio->new_blkaddr);

f2fs_update_iostat(sbi, dn->inode, fio->io_type,
F2FS_BLKSIZE(sbi));
--
2.56.0.567.gdb80ae8cd5-goog