[PATCH v2] f2fs: stop checkpoint on pinned segment allocation failure
From: Zhijun Li
Date: Thu Nov 26 2026 - 20:12:49 EST
From: Zhijun Li <lizhijun3@xxxxxxxxxx>
f2fs_allocate_data_block() updates SIT before rotating the current
segment, since SSR needs the latest valid block information:
update_sit_entry(sbi, *new_blkaddr, 1);
update_sit_entry(sbi, old_blkaddr, -1);
If the rotation fails, it jumps to out_err without reverting the SIT
update. This is fine as long as checkpoint has been stopped by then,
because the stale SIT will never be persisted. do_write_page() asserts
this with f2fs_bug_on(!CP_ERROR_FLAG), and every failure in the
rotation path satisfies it: get_new_segment() stops checkpoint on
-ENOSPC and -EFSCORRUPTED, and change_curseg() only fails after
f2fs_get_meta_cache_retry() has stopped checkpoint.
Pinned allocation is the exception. Since
commit 48ea8b200414 ("f2fs: fix to avoid panic once fallocation fails for pinfile"),
get_new_segment() does not stop checkpoint for pinned allocation, and
since commit c966d29e01bb ("f2fs: support resizable tail section and unify pinned allocation"),
it may also return -EAGAIN for it.
That is intended for f2fs_allocate_pinning_section(), which fails
before any SIT update. But when a CURSEG_COLD_DATA_PINNED segment
becomes full in the middle of a section, f2fs_allocate_data_block()
calls new_curseg() after SIT is updated. If it fails, fallocate()
returns an error and the filesystem keeps running, with a block marked
valid in SIT that no node refers to, while __allocate_data_block() has
already reverted valid_block_count. The next checkpoint persists the
SIT, and the next mount fails in build_sit_entries():
F2FS-fs (vda): SIT is corrupted data# 513 3 vs 515
F2FS-fs (vda): Failed to initialize F2FS segment manager (-117)
fsck.f2fs also reports "SIT valid block bitmap checking [Fail]".
It can be reproduced with CONFIG_F2FS_FAULT_INJECTION:
mkfs.f2fs -f -s 4 /dev/vda
mount -t f2fs /dev/vda /mnt
touch /mnt/pin; f2fs_io pinfile set /mnt/pin
echo 0x100000 > /sys/fs/f2fs/vda/inject_type # FAULT_NO_SEGMENT
echo 2 > /sys/fs/f2fs/vda/inject_rate # fail the 2nd segment
fallocate -l 64M /mnt/pin # fails with ENOSPC
echo 0 > /sys/fs/f2fs/vda/inject_rate
umount /mnt
mount -t f2fs /dev/vda /mnt # fails with EUCLEAN
Fix it by stopping checkpoint when the rotation fails after SIT has
been updated. The early -ENOSPC return for curseg->segno == NULL_SEGNO
is not affected: it happens before any SIT update and is the expected
path once a pinned section is used up. Pinned allocation failures in
f2fs_allocate_pinning_section() still don't stop checkpoint.
Fixes: 48ea8b200414 ("f2fs: fix to avoid panic once fallocation fails for pinfile")
Co-developed-by: Chen Zhangqi <chenzhangqi@xxxxxxxxxx>
Signed-off-by: Chen Zhangqi <chenzhangqi@xxxxxxxxxx>
Signed-off-by: Zhijun Li <lizhijun3@xxxxxxxxxx>
---
v2:
- Drop the SIT rollback. v1 claimed that change_curseg() may fail with
-ENOMEM without stopping checkpoint, which is wrong: the summary read
retries on allocation failure, and f2fs_get_meta_cache_retry() stops
checkpoint on any error. The only gap is pinned allocation.
- Stop checkpoint instead of partially reverting SIT, which also left
ckpt_valid_map, discard_map, mtime and curseg state behind. This
matches the existing assumption in do_write_page().
- Fix the Fixes tag, drop Cc: stable.
- Add a reproducer. Tested in QEMU on top of f2fs/dev b61a029304bb with
and without this patch: 200 rounds of pinned fallocate with
FAULT_NO_SEGMENT at random rates (1..12) and sizes. Without the patch,
138 rounds left an image that failed to mount; with it, the same 138
rounds stopped checkpoint and the images passed fsck and mounted.
Rounds that failed in f2fs_allocate_pinning_section() did not stop
checkpoint either way. No lockdep/KASAN report with the patch.
Alternatively, we could check that the next segment can be allocated
before updating SIT for the pinned curseg, so that fallocate() fails
without shutting down the filesystem. I'm fine with either way.
v1: https://lore.kernel.org/linux-f2fs-devel/20261022120833.1698722-1-lizhijun3@xxxxxxxxxx/
fs/f2fs/segment.c | 12 +++++++++++-
1 file changed, 11 insertions(+), 1 deletion(-)
diff --git a/fs/f2fs/segment.c b/fs/f2fs/segment.c
index 066b7cf813d7..5499f20d6428 100644
--- a/fs/f2fs/segment.c
+++ b/fs/f2fs/segment.c
@@ -4051,8 +4051,18 @@ int f2fs_allocate_data_block(struct f2fs_sb_info *sbi,
stat_inc_seg_type(sbi, curseg);
}
- if (ret)
+ if (ret) {
+ /*
+ * SIT has already been updated above, stop checkpoint
+ * so that the inconsistent SIT won't be persisted.
+ * Most failures have stopped checkpoint already,
+ * except pinned allocation failure.
+ */
+ if (!f2fs_cp_error(sbi))
+ f2fs_stop_checkpoint(sbi, false,
+ STOP_CP_REASON_NO_SEGMENT);
goto out_err;
+ }
}
skip_new_segment:
--
2.43.0