Skip to contentSkip to conversation

fsck: fix stale "bad orphan inode" after fsck runs

PATCH New Baokun Li 1 participant 6 messages Jul 14

lore
Share & tools
lore

Baokun Li

author
#0 lore raw

cover fsck: fix stale "bad orphan inode" after fsck runs

systemd-fsck invokes the fsck wrapper with options (-l, -C <fd>) that the e2fsprogs wrapper mis-parses and leaks to e2fsck, where they get eaten as a bad_blocks_file. e2fsck then aborts after orphan cleanup but before flushing the superblock, leaving s_last_orphan stale on disk and the kernel reporting "bad orphan inode" on next mount.

This series fixes both wrapper parsing bugs (consume -l; parse a separate "-C <fd>"), and, as defense-in-depth, flushes the superblock right after orphan cleanup so an early abort can no longer corrupt the on-disk orphan state. Two regression tests are added. See the individual patches for details.

Comments and review are welcome.

Thanks,
Baokun

Baokun Li (5):
fsck: consume -l option instead of passing to e2fsck
fsck: fix -C fd option parsing when fd is a separate argument
e2fsck: flush superblock immediately after orphan cleanup
tests: add fsck option-parsing regression test
tests: add orphan-cleanup flush regression test

 e2fsck/super.c                      | 11 ++++++
 misc/fsck.c                         |  5 ++-
 tests/f_fsck_opt_parse/expect.1     |  2 +
 tests/f_fsck_opt_parse/name         |  1 +
 tests/f_fsck_opt_parse/script       | 61 +++++++++++++++++++++++++++++
 tests/f_orphan_flush_abort/expect.1 |  7 ++++
 tests/f_orphan_flush_abort/name     |  1 +
 tests/f_orphan_flush_abort/script   | 61 +++++++++++++++++++++++++++++
 tests/test_config                   |  2 +
 9 files changed, 150 insertions(+), 1 deletion(-)
 create mode 100644 tests/f_fsck_opt_parse/expect.1
 create mode 100644 tests/f_fsck_opt_parse/name
 create mode 100644 tests/f_fsck_opt_parse/script
 create mode 100644 tests/f_orphan_flush_abort/expect.1
 create mode 100644 tests/f_orphan_flush_abort/name
 create mode 100644 tests/f_orphan_flush_abort/script
Signature · 1 line
2.43.7
author
#1 lore raw

PATCH 1/5 fsck: consume -l option instead of passing to e2fsck

The -l (lockdisk) option was introduced by util-linux in commit dd0bd943f9 ("fsck: add support for whole-disk locking (-l option)", 2010-10-26, first released in v2.19). systemd-fsck passes -l to /sbin/fsck for parallel fsck support since commit 87c3e6c ("fsck:
pass -l to fsck").

Currently the e2fsprogs fsck wrapper (misc/fsck.c, installed via make install) does not recognize -l and passes it through to e2fsck. Since e2fsck's getopt string declares "l:" (requires argument), it consumes the next argument as the bad_blocks_file parameter. This triggers a chain of failures:

systemd-fsck -> fsck -a -T -l -M -C6 /dev/sda

    -> wrapper consumes -a,-T,-M,-C6, adds -C<fd>, passes -l
      -> fsck.ext4 -a -l -C<fd> /dev/sda
        -> e2fsck getopt: -l consumes -C<fd> as bad_blocks_file
          -> journal recovery
          -> orphan cleanup (orphan inodes processed)
          -> fopen("-C<fd>", "r") fails -> E2F_FLAG_ABORT
            -> superblock not flushed (s_last_orphan still non-zero)
              -> kernel: "bad orphan inode" on next mount

This is because the e2fsprogs wrapper lacks a case for -l in its option parsing, unlike the util-linux wrapper which handles it correctly (lockdisk = 1).

Fix by adding case 'l': break; to consume the option in the wrapper, matching util-linux's behavior. Users can still pass -l to e2fsck via the "--" separator (e.g., "fsck -- -l badblocks_file /dev/sda") or by invoking e2fsck directly.

---
 misc/fsck.c | 3 +++
 1 file changed, 3 insertions(+)
misc/fsck.c +3 -0
index 44577590bca5..cb935e1b4b71 100644
@@ -1212,6 +1212,9 @@ static void PRS(int argc, char *argv[])
 			case 'M':
 				ignore_mounted++;
 				break;
+			case 'l':
+				/* lockdisk option from util-linux fsck, consumed by wrapper */
+				break;
 			case 'P':
 				parallel_root++;
 				break;
Signature · 1 line
2.43.7
author
#2 lore raw

PATCH 2/5 fsck: fix -C fd option parsing when fd is a separate argument

In reply to Baokun Li

The e2fsprogs fsck wrapper (misc/fsck.c, installed via make install) supports the -C option for progress reporting. The fd can be passed either attached (-C6) or separated (-C 6).

Currently the wrapper handles "-C6" correctly, but fails to handle "-C 6" properly:

  # fsck -h | head -n1
  fsck 1.47.4 (6-Mar-2025)
  # fsck -C6 -N /dev/sda
  [/sbin/fsck.ext4 (1) -- /dev/sda] fsck.ext4 -C6 /dev/sda
  # fsck -C 6 -N /dev/sda
  [/sbin/fsck.ext4 (1) -- /dev/sda] fsck.ext4 6 -C0 /dev/sda

When "-C 6" is passed, it is parsed as "-C0" with "6" leaked as a stray argument. This is because in PRS(), the separate-argument branch uses argv[i] (which is "-C") instead of argv[i+1] (which is "6") to parse the fd value. string_to_int("-C") returns -1, so progress_fd stays at 0 and the "6" is never consumed. Later the wrapper generates "-C0" from progress_fd=0 and the stray "6" is passed before the device.

Fix by using argv[i+1] to correctly parse the separate fd argument.

Note: this is the e2fsprogs fsck wrapper (misc/fsck.c), not the util-linux fsck wrapper (disk-utils/fsck.c) which handles this correctly.

---
 misc/fsck.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
misc/fsck.c +1 -1
index cb935e1b4b71..318ecfcb4102 100644
@@ -1188,7 +1188,7 @@ static void PRS(int argc, char *argv[])
 						goto next_arg;
 				} else if (argc > i + 1 &&
 					   argv[i + 1][0] != '-') {
-					progress_fd = string_to_int(argv[i]);
+					progress_fd = string_to_int(argv[i+1]);
 					if (progress_fd < 0)
 						progress_fd = 0;
 					else {
Signature · 1 line
2.43.7
author
#3 lore raw

PATCH 3/5 e2fsck: flush superblock immediately after orphan cleanup

In reply to Baokun Li

When e2fsck aborts after processing orphan inodes but before the final superblock flush, the kernel reports "bad orphan inode" errors on next mount. This happens when e2fsck encounters fatal errors (e.g., bad_blocks_file open failures) after orphan cleanup completes.

The root cause is a timing issue in release_orphan_inodes(). For each orphan it rewrites the inode (updating i_dtime from the next-orphan pointer to a timestamp, and for a delete also clearing the inode bitmap and freeing blocks) and zeroes s_last_orphan in memory. The inode writes go through the io cache and may be written back to disk at any point as the cache fills during later passes, but the superblock update (s_last_orphan = 0) is only marked dirty and is not written until the end of the fsck run. If e2fsck aborts in between, the on-disk inode has already been rewritten while the on-disk superblock still points to it via the stale s_last_orphan.

When the kernel mounts such a filesystem, it follows the stale s_last_orphan pointer and finds an inode whose i_dtime field now contains a timestamp instead of a valid inode number. The kernel interprets this as an invalid next-orphan pointer and reports the error.

Fix this by flushing the superblock and bitmaps via ext2fs_flush() (which also flushes the io cache) right after a successful orphan cleanup, keeping on-disk state consistent regardless of what happens later. We only flush on success; if release_orphan_inodes() fails, the filesystem is already marked as needing a full fsck.

---
 e2fsck/super.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
e2fsck/super.c +11 -0
index 22af1b72684d..e8e4239fc037 100644
@@ -570,6 +570,17 @@ static int release_orphan_inodes(e2fsck_t ctx)
 
 	ext2fs_free_mem(&block_buf);
 	pctx.errcode = e2fsck_write_all_quotas(ctx);
+	if (pctx.errcode)
+		goto err;
+	/*
+	 * Orphan cleanup is a consistency point: force the superblock and
+	 * bitmaps to disk now.  Otherwise a later abnormal exit could leave
+	 * a stale s_last_orphan on disk while the orphan inodes it points to
+	 * have already been written back, which the kernel rejects as a
+	 * "bad orphan inode".  On error, fall through so the caller marks
+	 * the filesystem as needing a full fsck.
+	 */
+	pctx.errcode = ext2fs_flush(fs);
 	if (pctx.errcode)
 		goto err;
 	return 0;
Signature · 1 line
2.43.7
author
#4 lore raw

PATCH 4/5 tests: add fsck option-parsing regression test

In reply to Baokun Li

Add f_fsck_opt_parse, which runs the fsck wrapper in dry-run mode (-N) with -V so it echoes the exact argv it builds for the checker, and asserts the wrapper:

- consumes the util-linux "-l" (lockdisk) option instead of leaking it to e2fsck, where -l takes a bad-blocks-file argument and would swallow the following option; and
- parses "-C 2" (progress fd as a separate argument) into "-C2" instead of dropping it and leaking "2" as a stray device name.

---
 tests/f_fsck_opt_parse/expect.1 |  2 ++
 tests/f_fsck_opt_parse/name     |  1 +
 tests/f_fsck_opt_parse/script   | 61 +++++++++++++++++++++++++++++++++
 tests/test_config               |  2 ++
 4 files changed, 66 insertions(+)
 create mode 100644 tests/f_fsck_opt_parse/expect.1
 create mode 100644 tests/f_fsck_opt_parse/name
 create mode 100644 tests/f_fsck_opt_parse/script
tests/f_fsck_opt_parse/expect.1 new file +2 -0
new file mode 100644
index 000000000000..10b716cd16a2
@@ -0,0 +1,2 @@
+[fsck.ext4 (1) -- test.img] fsck.ext4 -f -C2 test.img 
+Exit status is 0
tests/f_fsck_opt_parse/name new file +1 -0
new file mode 100644
index 000000000000..37135f686288
@@ -0,0 +1 @@
+fsck wrapper consumes -l and parses "-C fd" as a separate argument
tests/f_fsck_opt_parse/script new file +61 -0

61 lines not on this page · raw message

tests/test_config +2 -0
index 9dc762ce5a88..cb2654d59266 100644
@@ -13,6 +13,8 @@ E2IMAGE="$USE_VALGRIND ../misc/e2image"
 E2IMAGE_EXE="../misc/e2image"
 DEBUGFS="$USE_VALGRIND ../debugfs/debugfs"
 DEBUGFS_EXE="../debugfs/debugfs"
+FSCK_WRAPPER="$USE_VALGRIND ../misc/fsck"
+FSCK_WRAPPER_EXE="../misc/fsck"
 TEST_BITS="test_data.tmp"
 RESIZE2FS_EXE="../resize/resize2fs"
 RESIZE2FS="$USE_VALGRIND $RESIZE2FS_EXE"
Signature · 1 line
2.43.7
author
#5 lore raw

PATCH 5/5 tests: add orphan-cleanup flush regression test

In reply to Baokun Li

Add f_orphan_flush_abort, which drives e2fsck directly:

1. Build an ext4 image whose inode 12 is an orphan (links_count 0, s_last_orphan = 12).
2. Run e2fsck with a nonexistent bad-blocks file so that orphan cleanup runs first and then read_bad_blocks_file aborts the run via exit() before the normal superblock write-back.
3. Check that s_last_orphan is 0 on disk afterwards.

Without the flush after orphan cleanup, s_last_orphan stays stale on disk (still 12) while the orphan inode has already been written back, which the kernel rejects as a "bad orphan inode" on next mount.

---
 tests/f_orphan_flush_abort/expect.1 |  7 ++++
 tests/f_orphan_flush_abort/name     |  1 +
 tests/f_orphan_flush_abort/script   | 61 +++++++++++++++++++++++++++++
 3 files changed, 69 insertions(+)
 create mode 100644 tests/f_orphan_flush_abort/expect.1
 create mode 100644 tests/f_orphan_flush_abort/name
 create mode 100644 tests/f_orphan_flush_abort/script
tests/f_orphan_flush_abort/expect.1 new file +7 -0
new file mode 100644
index 000000000000..0a6d09f16e36
@@ -0,0 +1,7 @@
+Clearing orphaned inode 12 (uid=0, gid=0, mode=0100644, size=8192)
+read_bad_blocks_file: No such file or directory while trying to open test.img
+
+test.img: ***** FILE SYSTEM WAS MODIFIED *****
+Exit status is 9
+Orphan before e2fsck: 12
+Orphan after e2fsck: 0 (cleared)
tests/f_orphan_flush_abort/name new file +1 -0
new file mode 100644
index 000000000000..c9292546c6dd
@@ -0,0 +1 @@
+orphan cleanup superblock flush survives a later abort
tests/f_orphan_flush_abort/script new file +61 -0

61 lines not on this page · raw message

Signature · 1 line
2.43.7