jbd2_journal_load() sets j_fc_last to s_maxlen, which is the first block past the end of the journal, and jbd2_fc_get_buf() never hands out a block at or beyond j_fc_last. The recovery loop in fc_do_one_pass() nevertheless runs while next_fc_block <= j_fc_last. If ordinary fsync use has filled every usable fast commit block before the next full commit, recovery reads one block past the journal and jread() fails with -EFSCORRUPTED ("JBD2: corrupted journal superblock"). The filesystem then fails to mount after a crash until e2fsck is run. Stop the loop at j_fc_last. That alone is not enough. ext4_fc_replay() calls ext4_fc_set_bitmaps_and_counters() only when it is called again after fc_replay_num_tags has reached zero. A TAIL tag always ends its block, so the call that handles the last tag returns JBD2_FC_REPLAY_CONTINUE and the finishing call used to come from the next block. When the last fast commit block is the last block of the journal there is no next call, ext4_fc_replay_inode() has already cleared the inode's blocks in the bitmap and e2fsck reports a block bitmap difference. Do the finishing step in the same call that consumes the last tag, and drop the in-loop copy, which would otherwise run it twice. Reproduced in User-Mode Linux with 1 KiB blocks (mke2fs -J size=8, 63 usable fast commit blocks) and 4 KiB blocks (default journal, 15 usable blocks): with the fast commit area exactly full the unpatched kernel fails the mount, and with one block fewer it recovers. With this patch mount succeeds, the file has all fast committed data, and e2fsck -fn exits 0 with no differences after recovery, for the full, one-block-short and three-fast-commit cases at both block sizes. This bug was found with Perfloop, an automated code analysis tool. The fix, this message and the reproducer were written with AI assistance and reviewed by me. Fixes: 5b849b5f96b4 ("jbd2: fast commit recovery path") Fixes: 8016e29f4362 ("ext4: fast commit recovery path") Cc: stable@vger.kernel.org Signed-off-by: Tomás Senart --- v2: - Please do not apply v1. v1 only changed the loop bound. As the Sashiko review pointed out, a full fast commit area then mounts but skips ext4_fc_set_bitmaps_and_counters(), and e2fsck -fn reports "Block bitmap differences: +2113" (4 KiB) and "+32769" (1 KiB) after recovery. ext4_fc_replay() now finishes the replay in the call that consumes the last tag. - The reproducer now runs e2fsck -fn after recovery. Tested on torvalds master 65e4fd942f72a9dba0e4e257290c4fa1aa756854, under User-Mode Linux only, with one journal size per block size, and only with INODE fast commit tags (no create, link or range tags). fs/ext4/fast_commit.c | 9 +++++---- fs/jbd2/recovery.c | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/fs/ext4/fast_commit.c b/fs/ext4/fast_commit.c index 0cac890cf370..5d9b95817ac6 100644 --- a/fs/ext4/fast_commit.c +++ b/fs/ext4/fast_commit.c @@ -2608,11 +2608,8 @@ static int ext4_fc_replay(journal_t *journal, struct buffer_head *bh, ext4_fc_get_tl(&tl, cur); val = cur + EXT4_FC_TAG_BASE_LEN; - if (state->fc_replay_num_tags == 0) { - ret = JBD2_FC_REPLAY_STOP; - ext4_fc_set_bitmaps_and_counters(sb); + if (state->fc_replay_num_tags == 0) break; - } ext4_debug("Replay phase, tag:%s\n", tag2str(tl.fc_tag)); state->fc_replay_num_tags--; @@ -2656,6 +2653,10 @@ static int ext4_fc_replay(journal_t *journal, struct buffer_head *bh, break; ret = JBD2_FC_REPLAY_CONTINUE; } + if (ret >= 0 && state->fc_replay_num_tags == 0) { + ext4_fc_set_bitmaps_and_counters(sb); + ret = JBD2_FC_REPLAY_STOP; + } return ret; } diff --git a/fs/jbd2/recovery.c b/fs/jbd2/recovery.c index cac8c2cd4a92..a59de33a2a4a 100644 --- a/fs/jbd2/recovery.c +++ b/fs/jbd2/recovery.c @@ -242,7 +242,7 @@ static int fc_do_one_pass(journal_t *journal, if (!journal->j_fc_replay_callback) return 0; - while (next_fc_block <= journal->j_fc_last) { + while (next_fc_block < journal->j_fc_last) { jbd2_debug(3, "Fast commit replay: next block %ld\n", next_fc_block); err = jread(&bh, journal, next_fc_block); -- 2.55.0