The fast-commit area ends at j_fc_last, an exclusive block limit, but fc_do_one_pass() includes that block. When a valid log fills the area, e2fsck reads beyond the journal after the final TAIL, reports journal corruption, and leaves acknowledged file data unrecovered. Stop scanning when the next block reaches j_fc_last. On the exact-base 15-Tail image, e2fsck reports corruption and leaves /boundary.bin at its initial 4 KiB. With this change, e2fsck recovers all 65,536 bytes and a read-only full-filesystem check passes. Add a regression test with a valid TAIL in the final usable block. Check its fixture writes so a setup failure cannot bypass the final-block scan. This contribution was developed with AI assistance. Fixes: 7ed2b5d0f5a3 ("e2fsck: port fc changes from kernel's recovery.c to e2fsck") Signed-off-by: Wesley Atwell --- e2fsck/recovery.c | 2 +- tests/j_fc_last_tail_replay/expect | 2 + tests/j_fc_last_tail_replay/name | 1 + tests/j_fc_last_tail_replay/script | 178 +++++++++++++++++++++++++++++ 4 files changed, 182 insertions(+), 1 deletion(-) create mode 100644 tests/j_fc_last_tail_replay/expect create mode 100644 tests/j_fc_last_tail_replay/name create mode 100644 tests/j_fc_last_tail_replay/script diff --git a/e2fsck/recovery.c b/e2fsck/recovery.c index 3c5dd01..6e515c5 100644 --- a/e2fsck/recovery.c +++ b/e2fsck/recovery.c @@ -244,7 +244,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) { jbd_debug(3, "Fast commit replay: next block %ld\n", next_fc_block); err = jread(&bh, journal, next_fc_block); diff --git a/tests/j_fc_last_tail_replay/expect b/tests/j_fc_last_tail_replay/expect new file mode 100644 index 0000000..2927f5c --- /dev/null +++ b/tests/j_fc_last_tail_replay/expect @@ -0,0 +1,2 @@ +final fast-commit block journal replay status: 0 +final fast-commit block full postcheck status: 0 diff --git a/tests/j_fc_last_tail_replay/name b/tests/j_fc_last_tail_replay/name new file mode 100644 index 0000000..85b3d58 --- /dev/null +++ b/tests/j_fc_last_tail_replay/name @@ -0,0 +1 @@ +e2fsck replays a fast commit ending at the final FC block diff --git a/tests/j_fc_last_tail_replay/script b/tests/j_fc_last_tail_replay/script new file mode 100644 index 0000000..03d8aca --- /dev/null +++ b/tests/j_fc_last_tail_replay/script @@ -0,0 +1,178 @@ +#!/bin/sh + +test_description="e2fsck reads the final fast-commit block" + +if ! test -x $DEBUGFS_EXE; then + echo "$test_name: $test_description: skipped (no debugfs)" + return 0 +fi + +SKIP_GUNZIP=true +OUT=$test_name.log +EXP=$test_dir/expect +replay_out=$TMPFILE.replay +check_out=$TMPFILE.check +status= + +cleanup() +{ + rm -f "$replay_out" "$check_out" "$OUT.new" +} + +fail() +{ + echo "$1" >> "$OUT.new" + if [ -s "$replay_out" ]; then + cat "$replay_out" >> "$OUT.new" + fi + sed -f $cmd_dir/filter.sed -e "s;$TMPFILE;test.img;" "$OUT.new" > "$OUT" + echo "$test_name: $test_description: failed" + diff $DIFF_OPTS "$EXP" "$OUT" > "$test_name.failed" + cleanup + return 1 +} + +: > "$OUT.new" +$MKE2FS -q -F -o Linux -t ext4 -b 4096 \ + -O fast_commit,^metadata_csum \ + -J size=4,fast_commit_size=64 \ + -E lazy_itable_init=0,lazy_journal_init=0 "$TMPFILE" 32768 \ + >/dev/null 2>&1 +status=$? +if [ "$status" -ne 0 ]; then + fail "mke2fs failed with $status" + return 1 +fi + +journal_blocks=$($DUMPE2FS -h "$TMPFILE" 2>/dev/null | + sed -n 's/^Total journal blocks:[[:space:]]*//p') +fc_length=$($DUMPE2FS -h "$TMPFILE" 2>/dev/null | + sed -n 's/^Fast commit length:[[:space:]]*//p') +if [ "$journal_blocks" != 1040 ] || [ "$fc_length" != 16 ]; then + fail "unexpected journal or fast-commit size" + return 1 +fi + +jsb=$($DEBUGFS -R "bmap <8> 0" "$TMPFILE" 2>/dev/null) +fc_first=$($DEBUGFS -R "bmap <8> 1025" "$TMPFILE" 2>/dev/null) +fc_last=$($DEBUGFS -R "bmap <8> 1039" "$TMPFILE" 2>/dev/null) +if [ -z "$jsb" ] || [ -z "$fc_first" ] || [ -z "$fc_last" ]; then + fail "could not locate the final fast-commit block" + return 1 +fi + +# The 4 KiB journal has 15 usable FC blocks, ending at logical block 1039. +logical=1025 +while [ "$logical" -le 1039 ]; do + pblk=$($DEBUGFS -R "bmap <8> $logical" "$TMPFILE" 2>/dev/null) + if [ -z "$pblk" ] || [ "$pblk" -eq 0 ]; then + fail "fast-commit block mapping is missing" + return 1 + fi + $DD if=/dev/zero of="$TMPFILE" bs=4096 seek="$pblk" count=1 \ + conv=notrunc >/dev/null 2>&1 + status=$? + if [ "$status" -ne 0 ]; then + fail "could not clear a fast-commit block" + return 1 + fi + logical=$((logical + 1)) +done + +# Initialize an empty transaction and enable fast-commit replay. +printf '\000\000\000\001\000\000\000\001' | + $DD of="$TMPFILE" bs=1 seek=$((jsb * 4096 + 24)) conv=notrunc \ + >/dev/null 2>&1 +status=$? +if [ "$status" -ne 0 ]; then + fail "could not initialize journal sequence" + return 1 +fi +printf '\000\000\000\040' | + $DD of="$TMPFILE" bs=1 seek=$((jsb * 4096 + 40)) conv=notrunc \ + >/dev/null 2>&1 +status=$? +if [ "$status" -ne 0 ]; then + fail "could not enable journal fast-commit replay" + return 1 +fi + +# Start with HEAD, then recognized PAD tags fill all blocks through the +# final TAIL at offset 1039. The CRC covers every tag and the TAIL TID. +printf '\011\000\010\000\000\000\000\000\001\000\000\000\007\000\360\017' | + $DD of="$TMPFILE" bs=1 seek=$((fc_first * 4096)) conv=notrunc \ + >/dev/null 2>&1 +status=$? +if [ "$status" -ne 0 ]; then + fail "could not write the fast-commit HEAD tag" + return 1 +fi +logical=1026 +while [ "$logical" -lt 1039 ]; do + pblk=$($DEBUGFS -R "bmap <8> $logical" "$TMPFILE" 2>/dev/null) + printf '\007\000\374\017' | + $DD of="$TMPFILE" bs=1 seek=$((pblk * 4096)) conv=notrunc \ + >/dev/null 2>&1 + status=$? + if [ "$status" -ne 0 ]; then + fail "could not write an intermediate PAD tag" + return 1 + fi + logical=$((logical + 1)) +done +printf '\007\000\360\017' | + $DD of="$TMPFILE" bs=1 seek=$((fc_last * 4096)) conv=notrunc \ + >/dev/null 2>&1 +status=$? +if [ "$status" -ne 0 ]; then + fail "could not write the final PAD tag" + return 1 +fi +printf '\010\000\010\000\001\000\000\000\235\026\013\323' | + $DD of="$TMPFILE" bs=1 seek=$((fc_last * 4096 + 4084)) conv=notrunc \ + >/dev/null 2>&1 +status=$? +if [ "$status" -ne 0 ]; then + fail "could not write the final TAIL tag" + return 1 +fi +$DEBUGFS -w -R "feature needs_recovery" "$TMPFILE" >/dev/null 2>&1 +status=$? +if [ "$status" -ne 0 ]; then + fail "could not mark the journal for recovery" + return 1 +fi + +$FSCK -y -E journal_only -N test_filesys "$TMPFILE" \ + > "$replay_out" 2>&1 +status=$? +echo "final fast-commit block journal replay status: $status" >> "$OUT.new" +if [ "$status" -ne 0 ] || + grep -q "JBD2: corrupted journal superblock" "$replay_out"; then + fail "e2fsck did not stop at the final fast-commit block" + return 1 +fi + +$FSCK -f -n -N test_filesys "$TMPFILE" > "$check_out" 2>&1 +status=$? +echo "final fast-commit block full postcheck status: $status" >> "$OUT.new" +if [ "$status" -ne 0 ]; then + cat "$check_out" >> "$OUT.new" + fail "final fast-commit replay left an inconsistent filesystem" + return 1 +fi + +sed -f $cmd_dir/filter.sed -e "s;$TMPFILE;test.img;" "$OUT.new" > "$OUT" +cmp -s "$OUT" "$EXP" +status=$? +if [ "$status" = 0 ]; then + echo "$test_name: $test_description: ok" + touch $test_name.ok +else + echo "$test_name: $test_description: failed" + diff $DIFF_OPTS "$EXP" "$OUT" > "$test_name.failed" +fi + +cleanup +unset OUT EXP replay_out check_out status jsb fc_first fc_last +unset journal_blocks fc_length logical pblk SKIP_GUNZIP base-commit: 76b5934a82daa43cd54125c7f31c66139c49bd3c -- 2.56.1