All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] jbd2: stop fast commit replay at the end of the fast commit area
@ 2026-11-21  3:03 Tomás Senart
  2026-11-21  3:13 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Tomás Senart @ 2026-11-21  3:03 UTC (permalink / raw)
  To: Theodore Ts'o, Jan Kara
  Cc: Harshad Shirwadkar, linux-ext4, linux-kernel, stable

fc_do_one_pass() scans the fast commit area with

	while (next_fc_block <= journal->j_fc_last)

but j_fc_last is one past the last fast commit block. It is set to
s_maxlen when the journal is loaded, j_fc_first is j_last + 1, and
jbd2_fc_get_buf() hands out blocks only while
j_fc_first + j_fc_off < j_fc_last. So block j_fc_last is never written,
and it is also past the end of the journal, because j_total_len is at
most s_maxlen.

The scan normally stops before that block: the replay callback returns
JBD2_FC_REPLAY_STOP at the first block that does not hold a fast commit
of the transaction being replayed. When every block of the area holds
one, nothing stops the scan. It then reads block j_fc_last, jread()
fails with -EFSCORRUPTED, and journal recovery fails:

  JBD2: corrupted journal superblock
  JBD2: journal recovery failed
  EXT4-fs (ubda): error loading journal

The mount then fails with -EUCLEAN until e2fsck replays the journal.

The area becomes exactly full in normal use. Each fsync() that ext4 can
fast commit uses at least one block, and only a full commit empties the
area. A crash after the fsync() that uses the last block, and before
the next full commit, leaves a journal that the kernel cannot recover.

Stop the scan at j_fc_last, the first block past the area.

Reproduced on mainline 7.4.0-rc3 under User-Mode Linux: mount with
commit=3600, append one byte and fsync() until the fast commit area is
full, then EXT4_IOC_SHUTDOWN with EXT4_GOING_FLAGS_NOLOGFLUSH and kill
the VM without unmounting. With 1 KiB blocks (mke2fs -O fast_commit
-J size=8, 63 usable fast commit blocks) and with 4 KiB blocks (default
sizing on a 64 MiB image, 15 usable blocks), the mount fails as above
when all fast commit blocks are in use, and recovery completes when one
block is still free. With this patch the failing images mount, and all
fast commits are replayed.

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")
Cc: stable@vger.kernel.org
Signed-off-by: Tomás Senart <ts@perfloop.ai>
---
Tested on torvalds master 65e4fd942f72a9dba0e4e257290c4fa1aa756854,
under User-Mode Linux only, with one journal size per block size. Not
tested on real hardware, in QEMU, or with the larger fast commit areas
that mke2fs makes for bigger filesystems. e2fsck 1.47.2 -fy recovers
the failing image, so the journal on disk is intact; only kernel
recovery fails.

Reproducer (needs a User-Mode Linux kernel with ext4, ubd and hostfs
built in; set UML= to its path):

#!/bin/bash
# Repro: ext4 fast-commit (FC) area filled to its last usable block,
then power loss -> next mount fails journal recovery.
# usage: repro_ext4_fc_full.sh <N fsyncs> [mkfs args]     e.g.  ...
255   or  ... 254
#   env UML = path to a User-Mode Linux kernel (ARCH=um, built-in ext4
+ ubd + hostfs; kernel 65e4fd942f72a9dba0e4e257290c4fa1aa756854).
# 1 KiB blocks: mke2fs -J size=8 gives s_num_fc_blks=64 => 63 usable
FC blocks. After the first few fsyncs fall back to full commits,
# N=254 leaves 62 FC blocks in use (control, recovers), N=255 leaves
63 (area full, mount fails). The exact N depends on the
# initial full-commit fallbacks; check with: debugfs -R logdump
<image>   (count "tag TAIL" for the last tid).
set -eu
export PATH=$PATH:/sbin:/usr/sbin
N=${1:?N}; MKFS_ARGS=${2:--b 1024 -O fast_commit -J size=8}
UML=${UML:?path to UML kernel}
W=$(mktemp -d); IMG=$W/disk.img
# --- helpers (fsync loop, ext4 shutdown ioctl without log flush) ---
cat > $W/fsyncfill.c <<'C'
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
int main(int c, char **v) { int n = atoi(v[2]), fd = open(v[1],
O_CREAT|O_RDWR|O_APPEND, 0644);
  for (int i = 0; i < n; i++) { if (write(fd, "x", 1) != 1 ||
fsync(fd)) { perror("w/fsync"); return 1; } } return 0; }
C
cat > $W/shutdown.c <<'C'
#include <stdio.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/types.h>
int main(int c, char **v) { __u32 f = 2; /* EXT4_GOING_FLAGS_NOLOGFLUSH */
  int fd = open(v[1], O_RDONLY); if (fd < 0 || ioctl(fd, _IOR('X',
125, __u32), &f)) { perror("shutdown"); return 1; } return 0; }
C
gcc -O2 -o $W/fsyncfill $W/fsyncfill.c; gcc -O2 -o $W/shutdown $W/shutdown.c
# --- guest init: runs as PID 1 inside UML ---
cat > $W/init.sh <<'S'
#!/bin/bash
export PATH=/sbin:/usr/sbin:/bin:/usr/bin
mount -t proc none /proc; mount -t tmpfs none /dev; mknod /dev/ubd0 b 98 0
for a in $(cat /proc/cmdline); do case $a in mode=*) MODE=${a#mode=};;
n=*) N=${a#n=};; w=*) W=${a#w=};; esac; done
mount -t tmpfs none /mnt; mkdir -p /mnt/out /mnt/t; mount -t hostfs
none /mnt/out -o hostfs=$W
{
if [ $MODE = fill ]; then
  mount -t ext4 -o commit=3600 /dev/ubd0 /mnt/t          #
commit=3600: no timer-driven full commit while we fill the FC area
  echo hello > /mnt/t/f; sync                              # full
commit: FC area starts empty for the next transaction
  $W/fsyncfill /mnt/t/f $N                                 # N x
(append 1 byte; fsync) = fast commits
  $W/shutdown /mnt/t                                       #
EXT4_IOC_SHUTDOWN, NOLOGFLUSH: no journal flush
else
  mount -t ext4 /dev/ubd0 /mnt/t; echo "mount exit status: $?"; wc -c /mnt/t/f
fi
dmesg | grep -E "JBD2|EXT4-fs"
} > /mnt/out/log.$MODE 2>&1
touch /mnt/out/done.$MODE; sleep 3600
S
chmod +x $W/init.sh
vm() {  # vm <image> <mode>; waits for the guest to finish, then
SIGKILLs UML (= power loss, no unmount)
  rm -f $W/done.$2
  $UML ubd0=$1 mem=512m root=/dev/root rootfstype=hostfs rootflags=/
ro init=$W/init.sh mode=$2 n=$N w=$W con=null ssl=null con0=null,fd:2
>$W/uml.$2 2>&1 &
  local p=$!; for i in $(seq 120); do [ -e $W/done.$2 ] && break;
sleep 1; done; kill -9 $p; wait $p 2>/dev/null || true; }
truncate -s 64M $IMG
mke2fs -q -F -t ext4 $MKFS_ARGS $IMG                 # default
journal/FC sizing unless overridden
vm $IMG fill                                          # fill the FC
area, shutdown without log flush, kill the VM
cp $IMG $W/crashed.img
echo "--- journal / FC log on the crashed image"; dumpe2fs -h
$W/crashed.img 2>&1 | grep -E "Block size|Total journal
blocks|needs_recovery" | cut -c1-70
debugfs -R logdump $W/crashed.img 2>/dev/null | grep -E "commit
block|tag (HEAD|TAIL)" | awk '/commit block/{c++} /tag TAIL/{t[$NF]++}
END{print c" full commits; FC TAIL tags per tid:"; for(k in t) print "
 tid",k":",t[k]}'
vm $IMG mount                                         # remount:
journal recovery runs here
echo "--- guest log (mount):"; cat $W/log.mount; echo "images/logs kept in $W"


 fs/jbd2/recovery.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

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.47.0

^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-21  3:13 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-21  3:03 [PATCH] jbd2: stop fast commit replay at the end of the fast commit area Tomás Senart
2026-11-21  3:13 ` sashiko-bot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.