Skip to contentSkip to conversation

jbd2: Fix two annoyances in jbd2

PATCH Applied Jan Kara 4 participants 6 messages Feb 6, 2025 – Mar 18, 2025

Subsystems (2)
lore
Share & tools
lore

Jan Kara

author
#0 lore raw

cover jbd2: Fix two annoyances in jbd2

Hello,

filesystem fuzzing of ocfs2 has revealed some issues in jbd2 where suitably corrupted fs can trigger issues in jbd2 - most notably a complaint about sb->s_sequence being 0 (which is actually correct after recent changes) and also attempt to replay the journal after it has been wiped. Fix them.

Honza

author
#1 lore raw

PATCH 1/2 jbd2: Remove wrong sb->s_sequence check

Journal emptiness is not determined by sb->s_sequence == 0 but rather by sb->s_start == 0 (which is set a few lines above). Furthermore 0 is a valid transaction ID so the check can spuriously trigger. Remove the invalid WARN_ON.

---
 fs/jbd2/journal.c | 1 -
 1 file changed, 1 deletion(-)
fs/jbd2/journal.c +0 -1
index 7e49d912b091..354c9f691df3 100644
@@ -1879,7 +1879,6 @@ int jbd2_journal_update_sb_log_tail(journal_t *journal, tid_t tail_tid,
 
 	/* Log is no longer empty */
 	write_lock(&journal->j_state_lock);
-	WARN_ON(!sb->s_sequence);
 	journal->j_flags &= ~JBD2_FLUSHED;
 	write_unlock(&journal->j_state_lock);
 
Signature · 1 line
2.43.0
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] jbd2: Remove wrong sb->s_sequence check

Quoted 7 lines · Jan Kara

Journal emptiness is not determined by sb->s_sequence == 0 but rather by sb->s_start == 0 (which is set a few lines above). Furthermore 0 is a valid transaction ID so the check can spuriously trigger. Remove the invalid WARN_ON.

This is indeed subtle, it looks good to me.

Quoted 16 lines
---
 fs/jbd2/journal.c | 1 -
 1 file changed, 1 deletion(-)
fs/jbd2/journal.c +0 -1
index 7e49d912b091..354c9f691df3 100644
@@ -1879,7 +1879,6 @@ int jbd2_journal_update_sb_log_tail(journal_t *journal, tid_t tail_tid,
 
 	/* Log is no longer empty */
 	write_lock(&journal->j_state_lock);
-	WARN_ON(!sb->s_sequence);
 	journal->j_flags &= ~JBD2_FLUSHED;
 	write_unlock(&journal->j_state_lock);
 
author
#3 lore raw

PATCH 2/2 jbd2: Do not try to recover wiped journal

In reply to Jan Kara

If a journal is wiped, we will set journal->j_tail to 0. However if 'write' argument is not set (as it happens for read-only device or for ocfs2), the on-disk superblock is not updated accordingly and thus jbd2_journal_recover() cat try to recover the wiped journal. Fix the check in jbd2_journal_recover() to use journal->j_tail for checking empty journal instead.

---
 fs/jbd2/recovery.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
fs/jbd2/recovery.c +6 -5
index 9192be7c19d8..23502f1a67c1 100644
@@ -287,19 +287,20 @@ static int fc_do_one_pass(journal_t *journal,
 int jbd2_journal_recover(journal_t *journal)
 {
 	int			err, err2;
-	journal_superblock_t *	sb;
-
 	struct recovery_info	info;
 
 	memset(&info, 0, sizeof(info));
-	sb = journal->j_superblock;
 
 	/*
 	 * The journal superblock's s_start field (the current log head)
 	 * is always zero if, and only if, the journal was cleanly
-	 * unmounted.
+	 * unmounted. We use its in-memory version j_tail here because
+	 * jbd2_journal_wipe() could have updated it without updating journal
+	 * superblock.
 	 */
-	if (!sb->s_start) {
+	if (!journal->j_tail) {
+		journal_superblock_t *sb = journal->j_superblock;
+
 		jbd2_debug(1, "No recovery required, last transaction %d, head block %u\n",
 			  be32_to_cpu(sb->s_sequence), be32_to_cpu(sb->s_head));
 		journal->j_transaction_sequence = be32_to_cpu(sb->s_sequence) + 1;
Signature · 1 line
2.43.0
Reviewed-by on v1 2/2
#4 lore raw

Re: [PATCH 2/2] jbd2: Do not try to recover wiped journal

Quoted 8 lines · Jan Kara

If a journal is wiped, we will set journal->j_tail to 0. However if 'write' argument is not set (as it happens for read-only device or for ocfs2), the on-disk superblock is not updated accordingly and thus jbd2_journal_recover() cat try to recover the wiped journal. Fix the check in jbd2_journal_recover() to use journal->j_tail for checking empty journal instead.

Ha, right, it looks good to me.

Quoted 34 lines
---
 fs/jbd2/recovery.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
fs/jbd2/recovery.c +6 -5
index 9192be7c19d8..23502f1a67c1 100644
@@ -287,19 +287,20 @@ static int fc_do_one_pass(journal_t *journal,
 int jbd2_journal_recover(journal_t *journal)
 {
 	int			err, err2;
-	journal_superblock_t *	sb;
-
 	struct recovery_info	info;
 
 	memset(&info, 0, sizeof(info));
-	sb = journal->j_superblock;
 
 	/*
 	 * The journal superblock's s_start field (the current log head)
 	 * is always zero if, and only if, the journal was cleanly
-	 * unmounted.
+	 * unmounted. We use its in-memory version j_tail here because
+	 * jbd2_journal_wipe() could have updated it without updating journal
+	 * superblock.
 	 */
-	if (!sb->s_start) {
+	if (!journal->j_tail) {
+		journal_superblock_t *sb = journal->j_superblock;
+
 		jbd2_debug(1, "No recovery required, last transaction %d, head block %u\n",
 			  be32_to_cpu(sb->s_sequence), be32_to_cpu(sb->s_head));
 		journal->j_transaction_sequence = be32_to_cpu(sb->s_sequence) + 1;
maintainer Applied
#5 lore raw
In reply to Jan Kara
Quoted 8 lines · Jan Kara

filesystem fuzzing of ocfs2 has revealed some issues in jbd2 where suitably corrupted fs can trigger issues in jbd2 - most notably a complaint about sb->s_sequence being 0 (which is actually correct after recent changes) and also attempt to replay the journal after it has been wiped. Fix them.

Honza

[...]

Applied, thanks!

[1/2] jbd2: Remove wrong sb->s_sequence check

      commit: e6eff39dd0fe4190c6146069cc16d160e71d1148
[2/2] jbd2: Do not try to recover wiped journal
      commit: a662f3c03b754e1f97a2781fa242e95bdb139798

Best regards,

Signature · 1 line
Theodore Ts'o <[email protected]>