[PATCH] jbd2: stop fast commit replay at the end of the fast commit area

Tomás Senart posted 1 patch 3 days, 9 hours ago
fs/jbd2/recovery.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH] jbd2: stop fast commit replay at the end of the fast commit area
Posted by Tomás Senart 3 days, 9 hours ago
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
[PATCH v2] jbd2: fix fast commit recovery failure when the FC area is full
Posted by Tomás Senart 3 days, 8 hours ago
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 <ts@perfloop.ai>
---
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
Re: [PATCH v2] jbd2: fix fast commit recovery failure when the FC area is full
Posted by Jan Kara 1 day, 2 hours ago
On Sat 21-11-26 04:14:05, Tomás Senart wrote:
> 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 <ts@perfloop.ai>

Looks good. Feel free to add:

Reviewed-by: Jan Kara <jack@suse.cz>

								Honza

> ---
> 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
-- 
Jan Kara <jack@suse.com>
SUSE Labs, CR