* [PATCH] f2fs: fix to roll back SIT update on segment allocation failure
@ 2026-10-22 12:08 lizhijun3
2026-11-27 1:12 ` [PATCH v2] f2fs: stop checkpoint on pinned " Zhijun Li
0 siblings, 1 reply; 2+ messages in thread
From: lizhijun3 @ 2026-10-22 12:08 UTC (permalink / raw)
To: jaegeuk, chao
Cc: chenzhangqi, linux-f2fs-devel, linux-kernel, stable, lizhijun3
In f2fs_allocate_data_block(), SIT is updated before segment rotation
since SSR needs the latest valid block information:
update_sit_entry(sbi, *new_blkaddr, 1);
update_sit_entry(sbi, old_blkaddr, -1);
If the subsequent segment rotation (new_curseg(), change_curseg() or
get_atssr_segment()) fails, it jumps to out_err and clears *new_blkaddr,
however, it does not roll back the two SIT updates, so that SIT may
record that:
- the new block is valid even if no data was written to it;
- the old block is free even if a node still refers to it.
Not every failure stops the checkpoint. For example, change_curseg()
returns PTR_ERR() if f2fs_get_sum_folio() fails (e.g. -ENOMEM), and this
path does not call f2fs_stop_checkpoint(), so the filesystem keeps
running. A later checkpoint can then persist the inconsistent SIT, and
the old block may be reallocated while being referenced, causing data
corruption. The same stale SIT state can also be exposed when pinned
allocation returns -EAGAIN and the caller retries the allocation.
Fix it by tracking whether SIT was updated and rolling back the two
update_sit_entry() calls in the error path.
Fixes: 7d009e048d7c ("f2fs: fix to handle segment allocation failure correctly")
Cc: stable@vger.kernel.org
Co-developed-by: Chen Zhangqi <chenzhangqi@xiaomi.com>
Signed-off-by: Chen Zhangqi <chenzhangqi@xiaomi.com>
Signed-off-by: lizhijun3 <lizhijun3@xiaomi.com>
---
fs/f2fs/segment.c | 10 ++++++++++
1 file changed, 10 insertions(+)
diff --git a/fs/f2fs/segment.c b/fs/f2fs/segment.c
index 63b712d3d599..033a3b8bf426 100644
--- a/fs/f2fs/segment.c
+++ b/fs/f2fs/segment.c
@@ -3908,6 +3908,7 @@ int f2fs_allocate_data_block(struct f2fs_sb_info *sbi, struct folio *folio,
bool from_gc = (type == CURSEG_ALL_DATA_ATGC);
struct seg_entry *se = NULL;
bool segment_full = false;
+ bool sit_updated = false;
int ret = 0;
f2fs_down_read(&SM_I(sbi)->curseg_lock);
@@ -3958,6 +3959,7 @@ int f2fs_allocate_data_block(struct f2fs_sb_info *sbi, struct folio *folio,
*/
update_sit_entry(sbi, *new_blkaddr, 1);
update_sit_entry(sbi, old_blkaddr, -1);
+ sit_updated = true;
/*
* If the current segment is full, flush it out and replace it with a
@@ -4027,6 +4029,14 @@ int f2fs_allocate_data_block(struct f2fs_sb_info *sbi, struct folio *folio,
return 0;
out_err:
+ /*
+ * SIT is updated before segment rotation for SSR. Roll it back if
+ * segment rotation fails.
+ */
+ if (sit_updated) {
+ update_sit_entry(sbi, *new_blkaddr, -1);
+ update_sit_entry(sbi, old_blkaddr, 1);
+ }
*new_blkaddr = NULL_ADDR;
up_write(&sit_i->sentry_lock);
mutex_unlock(&curseg->curseg_mutex);
^ permalink raw reply [flat|nested] 2+ messages in thread* [PATCH v2] f2fs: stop checkpoint on pinned segment allocation failure
2026-10-22 12:08 [PATCH] f2fs: fix to roll back SIT update on segment allocation failure lizhijun3
@ 2026-11-27 1:12 ` Zhijun Li
0 siblings, 0 replies; 2+ messages in thread
From: Zhijun Li @ 2026-11-27 1:12 UTC (permalink / raw)
To: Jaegeuk Kim, Chao Yu
Cc: linux-f2fs-devel, linux-kernel, Chen Zhangqi, Zhijun Li
From: Zhijun Li <lizhijun3@xiaomi.com>
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@xiaomi.com>
Signed-off-by: Chen Zhangqi <chenzhangqi@xiaomi.com>
Signed-off-by: Zhijun Li <lizhijun3@xiaomi.com>
---
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@xiaomi.com/
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
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-27 1:12 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-10-22 12:08 [PATCH] f2fs: fix to roll back SIT update on segment allocation failure lizhijun3
2026-11-27 1:12 ` [PATCH v2] f2fs: stop checkpoint on pinned " Zhijun Li
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®