[PATCH v2 2/6] ext4: introudce helper for jounral recover handle

From: Ye Bin
Date: Thu Feb 09 2023 - 21:57:15 EST


From: Ye Bin <yebin10@xxxxxxxxxx>

Now, ext4 file system only need to handle super block when do
recover journal.

Signed-off-by: Ye Bin <yebin10@xxxxxxxxxx>
---
fs/ext4/ext4_jbd2.c | 65 +++++++++++++++++++++++++++++++++++++++++++++
fs/ext4/ext4_jbd2.h | 2 ++
fs/ext4/super.c | 1 +
3 files changed, 68 insertions(+)

diff --git a/fs/ext4/ext4_jbd2.c b/fs/ext4/ext4_jbd2.c
index 77f318ec8abb..af03035606e1 100644
--- a/fs/ext4/ext4_jbd2.c
+++ b/fs/ext4/ext4_jbd2.c
@@ -395,3 +395,68 @@ int __ext4_handle_dirty_metadata(const char *where, unsigned int line,
}
return err;
}
+
+static void ext4_replay_end_callback(struct journal_s *journal)
+{
+ kfree(journal->j_replay_private_data);
+ journal->j_replay_private_data = NULL;
+ journal->j_replay_callback = NULL;
+ journal->j_replay_end_callback = NULL;
+}
+
+static int ext4_replay_callback(struct journal_s *journal,
+ struct buffer_head *bh)
+{
+ struct super_block *sb = journal->j_private;
+ struct ext4_sb_info *sbi = EXT4_SB(sb);
+ struct ext4_super_block *es = sbi->s_es;
+ struct ext4_super_block *nes;
+ unsigned long offset;
+
+ if (likely(sbi->s_sbh != bh))
+ return 0;
+
+ offset = (void*)es - (void*)sbi->s_sbh->b_data;
+ nes = (struct ext4_super_block*)(bh->b_data + offset);
+ /*
+ * If super block has error flag in journal record, there isn't need to
+ * cover error information, as in this case is errors=continue mode,
+ * error handle submit super block through journal.
+ */
+ if (le16_to_cpu(nes->s_state) & EXT4_ERROR_FS)
+ return 0;
+
+ memcpy(((char *)es) + EXT4_S_ERR_START,
+ journal->j_replay_private_data, EXT4_S_ERR_LEN);
+ if (sbi->s_mount_state & EXT4_ERROR_FS)
+ es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
+
+ return 0;
+}
+
+static int ext4_replay_prepare_callback(struct journal_s *journal)
+{
+ struct super_block *sb = journal->j_private;
+ struct ext4_sb_info *sbi = EXT4_SB(sb);
+ char *private;
+ struct ext4_super_block *es = sbi->s_es;
+
+ if (!(sbi->s_mount_state & EXT4_ERROR_FS))
+ return 0;
+
+ private = kmalloc(EXT4_S_ERR_LEN, GFP_KERNEL);
+ if (!private)
+ return -ENOMEM;
+ memcpy(private, ((char *)es) + EXT4_S_ERR_START, EXT4_S_ERR_LEN);
+
+ journal->j_replay_private_data = private;
+ journal->j_replay_callback = ext4_replay_callback;
+ journal->j_replay_end_callback = ext4_replay_end_callback;
+
+ return 0;
+}
+
+void ext4_init_replay(journal_t *journal)
+{
+ journal->j_replay_prepare_callback = ext4_replay_prepare_callback;
+}
diff --git a/fs/ext4/ext4_jbd2.h b/fs/ext4/ext4_jbd2.h
index 0c77697d5e90..8dcc7ef5028c 100644
--- a/fs/ext4/ext4_jbd2.h
+++ b/fs/ext4/ext4_jbd2.h
@@ -513,4 +513,6 @@ static inline int ext4_should_dioread_nolock(struct inode *inode)
return 1;
}

+void ext4_init_replay(journal_t *journal);
+
#endif /* _EXT4_JBD2_H */
diff --git a/fs/ext4/super.c b/fs/ext4/super.c
index dc3907dff13a..ea0fea04907c 100644
--- a/fs/ext4/super.c
+++ b/fs/ext4/super.c
@@ -5677,6 +5677,7 @@ static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
journal->j_commit_interval = sbi->s_commit_interval;
journal->j_min_batch_time = sbi->s_min_batch_time;
journal->j_max_batch_time = sbi->s_max_batch_time;
+ ext4_init_replay(journal);
ext4_fc_init(sb, journal);

write_lock(&journal->j_state_lock);
--
2.31.1