* [PATCH v2 1/2] jbd2: add a metadata validation hook to journal recovery
2026-11-19 12:25 [PATCH v2 0/2] ext4: reject impossible group free cluster counts Zhao Gongyi
@ 2026-11-19 12:26 ` Zhao Gongyi
2026-11-19 12:34 ` sashiko-bot
2026-11-19 12:26 ` [PATCH v2 2/2] ext4: reject impossible group free cluster counts Zhao Gongyi
1 sibling, 1 reply; 5+ messages in thread
From: Zhao Gongyi @ 2026-11-19 12:26 UTC (permalink / raw)
To: linux-ext4
Cc: Zhao Gongyi, linux-kernel, tytso, adilger.kernel, libaokun, jack,
ojaswin, ritesh.list, yi.zhang
Filesystems can need to validate metadata that block replay has changed
before fast commit replay consumes it. They can also need to check
metadata that fast commit replay updates before completing recovery.
Provide an optional callback after block replay, including when the
journal is empty, and after fast commit replay. Propagate validation
errors before resetting the journal. Keep the existing replay flow for
callers of jbd2_journal_load().
Signed-off-by: Zhao Gongyi <zhaogongyi@bytedance.com>
---
fs/jbd2/journal.c | 22 ++++++++++++++++------
fs/jbd2/recovery.c | 38 +++++++++++++++++++++++++++-----------
include/linux/jbd2.h | 7 ++++++-
3 files changed, 49 insertions(+), 18 deletions(-)
diff --git a/fs/jbd2/journal.c b/fs/jbd2/journal.c
index eb8e944..383606a 100644
--- a/fs/jbd2/journal.c
+++ b/fs/jbd2/journal.c
@@ -2079,20 +2079,24 @@ void jbd2_journal_update_sb_errno(journal_t *journal)
EXPORT_SYMBOL(jbd2_journal_update_sb_errno);
/**
- * jbd2_journal_load() - Read journal from disk.
+ * jbd2_journal_load_with_callback() - Read journal from disk.
* @journal: Journal to act on.
+ * @callback: Optional validation callback for recovery.
+ * @data: Private data for the replay callback.
*
- * Given a journal_t structure which tells us which disk blocks contain
- * a journal, read the journal from disk to initialise the in-memory
- * structures.
+ * Read the journal from disk and initialise the in-memory structures.
+ * Call @callback with after_fast_commit false after block replay,
+ * including for an empty journal, and with true after fast commit
+ * replay. A callback error stops recovery before the journal is reset.
*/
-int jbd2_journal_load(journal_t *journal)
+int jbd2_journal_load_with_callback(journal_t *journal,
+ jbd2_replay_callback_t callback, void *data)
{
int err;
/* Let the recovery code check whether it needs to recover any
* data from the journal. */
- err = jbd2_journal_recover(journal);
+ err = jbd2_journal_recover(journal, callback, data);
if (err) {
pr_warn("JBD2: journal recovery failed\n");
return err;
@@ -2125,6 +2129,12 @@ int jbd2_journal_load(journal_t *journal)
write_unlock(&journal->j_state_lock);
return 0;
}
+EXPORT_SYMBOL(jbd2_journal_load_with_callback);
+
+int jbd2_journal_load(journal_t *journal)
+{
+ return jbd2_journal_load_with_callback(journal, NULL, NULL);
+}
/**
* jbd2_journal_destroy() - Release a journal_t structure.
diff --git a/fs/jbd2/recovery.c b/fs/jbd2/recovery.c
index cac8c2c..be4fd27 100644
--- a/fs/jbd2/recovery.c
+++ b/fs/jbd2/recovery.c
@@ -37,8 +37,9 @@ struct recovery_info
int nr_revoke_hits;
};
-static int do_one_pass(journal_t *journal,
- struct recovery_info *info, enum passtype pass);
+static int do_one_pass(journal_t *journal, struct recovery_info *info,
+ enum passtype pass, jbd2_replay_callback_t callback,
+ void *data);
static int scan_revoke_records(journal_t *, enum passtype, struct buffer_head *,
tid_t, struct recovery_info *);
@@ -270,6 +271,8 @@ static int fc_do_one_pass(journal_t *journal,
/**
* jbd2_journal_recover - recovers a on-disk journal
* @journal: the journal to recover
+ * @callback: optional check after each replay stage
+ * @data: private data for the replay callback
*
* The primary function for recovering the log contents when mounting a
* journaled device.
@@ -279,7 +282,8 @@ static int fc_do_one_pass(journal_t *journal,
* blocks. In the third and final pass, we replay any un-revoked blocks
* in the log.
*/
-int jbd2_journal_recover(journal_t *journal)
+int jbd2_journal_recover(journal_t *journal,
+ jbd2_replay_callback_t callback, void *data)
{
int err, err2;
struct recovery_info info;
@@ -300,14 +304,14 @@ int jbd2_journal_recover(journal_t *journal)
be32_to_cpu(sb->s_sequence), be32_to_cpu(sb->s_head));
journal->j_transaction_sequence = be32_to_cpu(sb->s_sequence) + 1;
journal->j_head = be32_to_cpu(sb->s_head);
- return 0;
+ return callback ? callback(journal, data, false) : 0;
}
- err = do_one_pass(journal, &info, PASS_SCAN);
+ err = do_one_pass(journal, &info, PASS_SCAN, NULL, NULL);
if (!err)
- err = do_one_pass(journal, &info, PASS_REVOKE);
+ err = do_one_pass(journal, &info, PASS_REVOKE, NULL, NULL);
if (!err)
- err = do_one_pass(journal, &info, PASS_REPLAY);
+ err = do_one_pass(journal, &info, PASS_REPLAY, callback, data);
jbd2_debug(1, "JBD2: recovery, exit status %d, "
"recovered transactions %u to %u\n",
@@ -365,7 +369,7 @@ int jbd2_journal_skip_recovery(journal_t *journal)
memset (&info, 0, sizeof(info));
- err = do_one_pass(journal, &info, PASS_SCAN);
+ err = do_one_pass(journal, &info, PASS_SCAN, NULL, NULL);
if (err) {
printk(KERN_ERR "JBD2: error %d scanning journal\n", err);
@@ -585,8 +589,9 @@ skip_write:
return ret;
}
-static int do_one_pass(journal_t *journal,
- struct recovery_info *info, enum passtype pass)
+static int do_one_pass(journal_t *journal, struct recovery_info *info,
+ enum passtype pass, jbd2_replay_callback_t callback,
+ void *data)
{
unsigned int first_commit_ID, next_commit_ID;
unsigned long next_log_block, head_block;
@@ -936,10 +941,21 @@ chksum_ok:
}
}
- if (jbd2_has_feature_fast_commit(journal) && pass != PASS_REVOKE) {
+ /* Only PASS_REPLAY can carry a validation callback. */
+ if (callback) {
+ if (success)
+ return success;
+ err = callback(journal, data, false);
+ if (err)
+ return err;
+ }
+
+ if (jbd2_has_feature_fast_commit(journal) && pass != PASS_REVOKE) {
err = fc_do_one_pass(journal, info, pass);
if (err)
success = err;
+ else if (callback)
+ success = callback(journal, data, true);
}
return success;
diff --git a/include/linux/jbd2.h b/include/linux/jbd2.h
index 69f1236..28358c6 100644
--- a/include/linux/jbd2.h
+++ b/include/linux/jbd2.h
@@ -1565,9 +1565,14 @@ extern int jbd2_journal_set_features
(journal_t *, unsigned long, unsigned long, unsigned long);
extern void jbd2_journal_clear_features
(journal_t *, unsigned long, unsigned long, unsigned long);
+typedef int (*jbd2_replay_callback_t)(journal_t *journal, void *data,
+ bool after_fast_commit);
extern int jbd2_journal_load (journal_t *journal);
+int jbd2_journal_load_with_callback(journal_t *journal,
+ jbd2_replay_callback_t callback, void *data);
extern int jbd2_journal_destroy (journal_t *);
-extern int jbd2_journal_recover (journal_t *journal);
+int jbd2_journal_recover(journal_t *journal,
+ jbd2_replay_callback_t callback, void *data);
extern int jbd2_journal_wipe (journal_t *, int);
extern int jbd2_journal_skip_recovery (journal_t *);
extern void jbd2_journal_update_sb_errno(journal_t *);
^ permalink raw reply related [flat|nested] 5+ messages in thread* [PATCH v2 2/2] ext4: reject impossible group free cluster counts
2026-11-19 12:25 [PATCH v2 0/2] ext4: reject impossible group free cluster counts Zhao Gongyi
2026-11-19 12:26 ` [PATCH v2 1/2] jbd2: add a metadata validation hook to journal recovery Zhao Gongyi
@ 2026-11-19 12:26 ` Zhao Gongyi
2026-11-19 13:02 ` sashiko-bot
1 sibling, 1 reply; 5+ messages in thread
From: Zhao Gongyi @ 2026-11-19 12:26 UTC (permalink / raw)
To: linux-ext4
Cc: Zhao Gongyi, linux-kernel, tytso, adilger.kernel, libaokun, jack,
ojaswin, ritesh.list, yi.zhang
ext4_count_free_clusters() sums the free-cluster counts from the group
descriptors when initializing the mount's free-space counters. A corrupt
bigalloc filesystem can report 201327130 free clusters in a group that
holds only 32. Reject counts above the group's capacity, using the same
calculation as the block bitmap, including for short final groups and
read-only mounts. Read both halves of the count under the group lock so
updates from deferred EA inode eviction cannot yield a mixed value.
Journal replay can replace both the group descriptors and the
superblock used to interpret them. For mounts that need recovery,
validate the descriptor for the internal journal inode before loading
it. Validate the recovered superblock geometry and all descriptors
after replaying journal blocks. Reload the descriptor table if replay
of an online resize changed the group count or descriptor layout.
Use the JBD2 recovery callback to validate recovered metadata before
fast commit replay consumes it. Check the descriptors again after
fast commit replay modifies them. Both checks run before JBD2 resets
the journal and before ext4 initializes its free-space counters.
Link: https://lore.kernel.org/linux-ext4/20261102042428.65204-1-zhaogongyi@bytedance.com/
Signed-off-by: Zhao Gongyi <zhaogongyi@bytedance.com>
---
fs/ext4/balloc.c | 10 +-
fs/ext4/ext4.h | 2 +
fs/ext4/super.c | 294 +++++++++++++++++++++++++++++++++++++----------
3 files changed, 238 insertions(+), 68 deletions(-)
diff --git a/fs/ext4/balloc.c b/fs/ext4/balloc.c
index 778fe87..0f36630 100644
--- a/fs/ext4/balloc.c
+++ b/fs/ext4/balloc.c
@@ -159,8 +159,8 @@ static unsigned ext4_num_overhead_clusters(struct super_block *sb,
return num_clusters;
}
-static unsigned int num_clusters_in_group(struct super_block *sb,
- ext4_group_t block_group)
+unsigned int ext4_num_clusters_in_group(struct super_block *sb,
+ ext4_group_t block_group)
{
unsigned int blocks;
@@ -228,7 +228,7 @@ static int ext4_init_block_bitmap(struct super_block *sb,
* the blocksize * 8 ( which is the size of bitmap ), set rest
* of the block bitmap to 1
*/
- ext4_mark_bitmap_end(num_clusters_in_group(sb, block_group),
+ ext4_mark_bitmap_end(ext4_num_clusters_in_group(sb, block_group),
sb->s_blocksize * 8, bh->b_data);
return 0;
}
@@ -240,7 +240,7 @@ unsigned ext4_free_clusters_after_init(struct super_block *sb,
ext4_group_t block_group,
struct ext4_group_desc *gdp)
{
- return num_clusters_in_group(sb, block_group) -
+ return ext4_num_clusters_in_group(sb, block_group) -
ext4_num_overhead_clusters(sb, block_group, gdp);
}
@@ -313,7 +313,7 @@ static ext4_fsblk_t ext4_valid_block_bitmap_padding(struct super_block *sb,
{
ext4_grpblk_t next_zero_bit;
unsigned long bitmap_size = sb->s_blocksize * 8;
- unsigned int offset = num_clusters_in_group(sb, block_group);
+ unsigned int offset = ext4_num_clusters_in_group(sb, block_group);
if (bitmap_size <= offset)
return 0;
diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
index aa0f230..598528f 100644
--- a/fs/ext4/ext4.h
+++ b/fs/ext4/ext4.h
@@ -3480,6 +3480,8 @@ extern ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb,
struct ext4_group_desc *bg);
extern ext4_fsblk_t ext4_inode_table(struct super_block *sb,
struct ext4_group_desc *bg);
+unsigned int ext4_num_clusters_in_group(struct super_block *sb,
+ ext4_group_t block_group);
extern __u32 ext4_free_group_clusters(struct super_block *sb,
struct ext4_group_desc *bg);
extern __u32 ext4_free_inodes_count(struct super_block *sb,
diff --git a/fs/ext4/super.c b/fs/ext4/super.c
index 359f9d4..68c33a9 100644
--- a/fs/ext4/super.c
+++ b/fs/ext4/super.c
@@ -66,7 +66,8 @@ static DEFINE_MUTEX(ext4_li_mtx);
static struct ratelimit_state ext4_mount_msg_ratelimit;
static int ext4_load_journal(struct super_block *, struct ext4_super_block *,
- unsigned long journal_devnum);
+ unsigned long journal_devnum,
+ ext4_group_t *first_not_zeroed);
static int ext4_show_options(struct seq_file *seq, struct dentry *root);
static void ext4_update_super(struct super_block *sb);
static int ext4_commit_super(struct super_block *sb);
@@ -3366,7 +3367,9 @@ void ext4_group_desc_csum_set(struct super_block *sb, __u32 block_group,
/* Called at mount-time, super-block is locked */
static int ext4_check_descriptors(struct super_block *sb,
ext4_fsblk_t sb_block,
- ext4_group_t *first_not_zeroed)
+ unsigned int journal_inum,
+ ext4_group_t *first_not_zeroed,
+ bool readonly)
{
struct ext4_sb_info *sbi = EXT4_SB(sb);
ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
@@ -3376,15 +3379,27 @@ static int ext4_check_descriptors(struct super_block *sb,
ext4_fsblk_t inode_bitmap;
ext4_fsblk_t inode_table;
int flexbg_flag = 0;
- ext4_group_t i, grp = sbi->s_groups_count;
+ ext4_group_t i = 0, end = sbi->s_groups_count, grp = end;
if (ext4_has_feature_flex_bg(sb))
flexbg_flag = 1;
+ /* Before recovery, check the group needed to load the journal inode. */
+ if (journal_inum) {
+ if (journal_inum < EXT4_ROOT_INO ||
+ journal_inum > le32_to_cpu(sbi->s_es->s_inodes_count))
+ return 0;
+ i = (journal_inum - 1) / EXT4_INODES_PER_GROUP(sb);
+ end = i + 1;
+ if (!flexbg_flag)
+ first_block = ext4_group_first_block_no(sb, i);
+ }
+
ext4_debug("Checking group descriptors");
- for (i = 0; i < sbi->s_groups_count; i++) {
+ for (; i < end; i++) {
struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
+ unsigned int free_clusters = 0;
if (i == sbi->s_groups_count - 1 || flexbg_flag)
last_block = ext4_blocks_count(sbi->s_es) - 1;
@@ -3401,7 +3416,7 @@ static int ext4_check_descriptors(struct super_block *sb,
ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
"Block bitmap for group %u overlaps "
"superblock", i);
- if (!sb_rdonly(sb))
+ if (!readonly)
return 0;
}
if (block_bitmap >= sb_block + 1 &&
@@ -3409,7 +3424,7 @@ static int ext4_check_descriptors(struct super_block *sb,
ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
"Block bitmap for group %u overlaps "
"block group descriptors", i);
- if (!sb_rdonly(sb))
+ if (!readonly)
return 0;
}
if (block_bitmap < first_block || block_bitmap > last_block) {
@@ -3423,7 +3438,7 @@ static int ext4_check_descriptors(struct super_block *sb,
ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
"Inode bitmap for group %u overlaps "
"superblock", i);
- if (!sb_rdonly(sb))
+ if (!readonly)
return 0;
}
if (inode_bitmap >= sb_block + 1 &&
@@ -3431,7 +3446,7 @@ static int ext4_check_descriptors(struct super_block *sb,
ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
"Inode bitmap for group %u overlaps "
"block group descriptors", i);
- if (!sb_rdonly(sb))
+ if (!readonly)
return 0;
}
if (inode_bitmap < first_block || inode_bitmap > last_block) {
@@ -3445,7 +3460,7 @@ static int ext4_check_descriptors(struct super_block *sb,
ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
"Inode table for group %u overlaps "
"superblock", i);
- if (!sb_rdonly(sb))
+ if (!readonly)
return 0;
}
if (inode_table >= sb_block + 1 &&
@@ -3453,11 +3468,12 @@ static int ext4_check_descriptors(struct super_block *sb,
ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
"Inode table for group %u overlaps "
"block group descriptors", i);
- if (!sb_rdonly(sb))
+ if (!readonly)
return 0;
}
if (inode_table < first_block ||
- inode_table + sbi->s_itb_per_group - 1 > last_block) {
+ inode_table > last_block ||
+ sbi->s_itb_per_group - 1 > last_block - inode_table) {
ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
"Inode table for group %u not in group "
"(block %llu)!", i, inode_table);
@@ -3469,16 +3485,30 @@ static int ext4_check_descriptors(struct super_block *sb,
"Checksum for group %u failed (%u!=%u)",
i, le16_to_cpu(ext4_group_desc_csum(sb, i,
gdp)), le16_to_cpu(gdp->bg_checksum));
- if (!sb_rdonly(sb)) {
+ if (!readonly) {
ext4_unlock_group(sb, i);
return 0;
}
}
+ /* Sample both halves of the count under the group lock. */
+ if (!journal_inum)
+ free_clusters = ext4_free_group_clusters(sb, gdp);
ext4_unlock_group(sb, i);
+
+ if (!journal_inum) {
+ unsigned int capacity = ext4_num_clusters_in_group(sb, i);
+
+ if (free_clusters > capacity) {
+ ext4_msg(sb, KERN_ERR,
+ "group %u has invalid free clusters count %u > %u",
+ i, free_clusters, capacity);
+ return 0;
+ }
+ }
if (!flexbg_flag)
first_block += EXT4_BLOCKS_PER_GROUP(sb);
}
- if (NULL != first_not_zeroed)
+ if (first_not_zeroed)
*first_not_zeroed = grp;
return 1;
}
@@ -4875,7 +4905,8 @@ static int ext4_check_feature_compatibility(struct super_block *sb,
}
static int ext4_check_geometry(struct super_block *sb,
- struct ext4_super_block *es)
+ struct ext4_super_block *es,
+ ext4_group_t *groups_count)
{
struct ext4_sb_info *sbi = EXT4_SB(sb);
__u64 blocks_count;
@@ -4939,88 +4970,193 @@ static int ext4_check_geometry(struct super_block *sb,
EXT4_BLOCKS_PER_GROUP(sb));
return -EINVAL;
}
- sbi->s_groups_count = blocks_count;
- sbi->s_blockfile_groups = min(sbi->s_groups_count,
- (EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb)));
- if (((u64)sbi->s_groups_count * sbi->s_inodes_per_group) !=
+ if (blocks_count * sbi->s_inodes_per_group !=
le32_to_cpu(es->s_inodes_count)) {
ext4_msg(sb, KERN_ERR, "inodes count not valid: %u vs %llu",
le32_to_cpu(es->s_inodes_count),
- ((u64)sbi->s_groups_count * sbi->s_inodes_per_group));
+ blocks_count * sbi->s_inodes_per_group);
return -EINVAL;
}
+ *groups_count = blocks_count;
return 0;
}
-static int ext4_group_desc_init(struct super_block *sb,
- struct ext4_super_block *es,
+/* Mount and recovery load the table before publishing the superblock. */
+static int ext4_load_group_desc(struct super_block *sb,
ext4_fsblk_t logical_sb_block,
- ext4_group_t *first_not_zeroed)
+ ext4_group_t groups_count)
{
struct ext4_sb_info *sbi = EXT4_SB(sb);
+ struct ext4_super_block *es = sbi->s_es;
+ struct buffer_head **group_desc;
unsigned int db_count;
ext4_fsblk_t block;
- int i;
+ int err, i;
- db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
- EXT4_DESC_PER_BLOCK(sb);
- if (ext4_has_feature_meta_bg(sb)) {
- if (le32_to_cpu(es->s_first_meta_bg) > db_count) {
- ext4_msg(sb, KERN_WARNING,
- "first meta block group too large: %u "
- "(group descriptor block count %u)",
- le32_to_cpu(es->s_first_meta_bg), db_count);
- return -EINVAL;
- }
- }
- rcu_assign_pointer(sbi->s_group_desc,
- kvmalloc_objs(struct buffer_head *, db_count));
- if (sbi->s_group_desc == NULL) {
- ext4_msg(sb, KERN_ERR, "not enough memory");
- return -ENOMEM;
+ db_count = DIV_ROUND_UP(groups_count, EXT4_DESC_PER_BLOCK(sb));
+ if (ext4_has_feature_meta_bg(sb) &&
+ le32_to_cpu(es->s_first_meta_bg) > db_count) {
+ ext4_msg(sb, KERN_ERR,
+ "first meta block group too large: %u (group descriptor block count %u)",
+ le32_to_cpu(es->s_first_meta_bg), db_count);
+ return -EINVAL;
}
- bgl_lock_init(sbi->s_blockgroup_lock);
+ group_desc = kvmalloc_objs(struct buffer_head *, db_count);
+ if (!group_desc)
+ return -ENOMEM;
- /* Pre-read the descriptors into the buffer cache */
+ /* Pre-read the descriptors into the buffer cache. */
for (i = 0; i < db_count; i++) {
block = descriptor_loc(sb, logical_sb_block, i);
+ if (block >= ext4_blocks_count(es)) {
+ ext4_msg(sb, KERN_ERR,
+ "group descriptor block %d outside filesystem", i);
+ kvfree(group_desc);
+ return -EFSCORRUPTED;
+ }
ext4_sb_breadahead_unmovable(sb, block);
}
for (i = 0; i < db_count; i++) {
- struct buffer_head *bh;
-
block = descriptor_loc(sb, logical_sb_block, i);
- bh = ext4_sb_bread_unmovable(sb, block);
- if (IS_ERR(bh)) {
+ group_desc[i] = ext4_sb_bread_unmovable(sb, block);
+ if (IS_ERR(group_desc[i])) {
ext4_msg(sb, KERN_ERR,
- "can't read group descriptor %d", i);
- sbi->s_gdb_count = i;
- return PTR_ERR(bh);
+ "can't read group descriptor %d", i);
+ err = PTR_ERR(group_desc[i]);
+ goto out_free;
}
- rcu_read_lock();
- rcu_dereference(sbi->s_group_desc)[i] = bh;
- rcu_read_unlock();
}
+
+ ext4_group_desc_free(sbi);
+ rcu_assign_pointer(sbi->s_group_desc, group_desc);
sbi->s_gdb_count = db_count;
- if (!ext4_check_descriptors(sb, logical_sb_block, first_not_zeroed)) {
- ext4_msg(sb, KERN_ERR, "group descriptors corrupted!");
+ sbi->s_groups_count = groups_count;
+ sbi->s_blockfile_groups = min(groups_count,
+ EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb));
+ return 0;
+
+out_free:
+ while (i--)
+ brelse(group_desc[i]);
+ kvfree(group_desc);
+ return err;
+}
+
+struct ext4_replay_callback_data {
+ struct ext4_super_block super;
+ ext4_group_t *first_not_zeroed;
+ bool readonly;
+};
+
+static int ext4_validate_replayed_metadata(journal_t *journal, void *data,
+ bool after_fast_commit)
+{
+ struct ext4_replay_callback_data *replay = data;
+ 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 *old = &replay->super;
+ ext4_fsblk_t sb_block = sbi->s_sbh->b_blocknr;
+ ext4_group_t groups_count;
+ unsigned int inode_size, first_ino;
+ u32 incompat, ro_compat;
+ int err;
+
+ /* Fast commits only change descriptor contents, such as counts. */
+ if (after_fast_commit) {
+ if (!(sbi->s_mount_state & EXT4_FC_REPLAY))
+ return 0;
+ goto check_descriptors;
+ }
+
+ incompat = le32_to_cpu(es->s_feature_incompat ^ old->s_feature_incompat);
+ ro_compat = le32_to_cpu(es->s_feature_ro_compat ^ old->s_feature_ro_compat);
+ if (es->s_rev_level == cpu_to_le32(EXT4_GOOD_OLD_REV)) {
+ inode_size = EXT4_GOOD_OLD_INODE_SIZE;
+ first_ino = EXT4_GOOD_OLD_FIRST_INO;
+ } else {
+ inode_size = le16_to_cpu(es->s_inode_size);
+ first_ino = le32_to_cpu(es->s_first_ino);
+ }
+
+ /* Online resize can grow the filesystem without changing group units. */
+ if (es->s_magic != old->s_magic ||
+ es->s_log_block_size != old->s_log_block_size ||
+ es->s_log_cluster_size != old->s_log_cluster_size ||
+ es->s_blocks_per_group != old->s_blocks_per_group ||
+ es->s_clusters_per_group != old->s_clusters_per_group ||
+ es->s_inodes_per_group != old->s_inodes_per_group ||
+ es->s_first_data_block != old->s_first_data_block ||
+ inode_size != sbi->s_inode_size || first_ino != sbi->s_first_ino ||
+ es->s_desc_size != old->s_desc_size ||
+ es->s_log_groups_per_flex != old->s_log_groups_per_flex ||
+ ext4_blocks_count(es) < ext4_blocks_count(old) ||
+ (incompat & (EXT4_FEATURE_INCOMPAT_64BIT |
+ EXT4_FEATURE_INCOMPAT_FLEX_BG)) ||
+ (ro_compat & (EXT4_FEATURE_RO_COMPAT_BIGALLOC |
+ EXT4_FEATURE_RO_COMPAT_METADATA_CSUM |
+ EXT4_FEATURE_RO_COMPAT_GDT_CSUM |
+ EXT4_FEATURE_RO_COMPAT_READONLY))) {
+ ext4_msg(sb, KERN_ERR, "invalid filesystem geometry after journal replay");
return -EFSCORRUPTED;
}
+ /* An unreplayed superblock can contain mount-time hash flags. */
+ if (memcmp(es, old, sizeof(*es)) &&
+ (!ext4_verify_csum_type(sb, es) ||
+ !ext4_superblock_csum_verify(sb, es))) {
+ ext4_msg(sb, KERN_ERR, "invalid superblock checksum after journal replay");
+ return -EFSBADCRC;
+ }
+
+ if (ext4_has_feature_metadata_csum(sb) &&
+ sbi->s_csum_seed != (ext4_has_feature_csum_seed(sb) ?
+ le32_to_cpu(es->s_checksum_seed) :
+ ext4_chksum(~0, es->s_uuid, sizeof(es->s_uuid)))) {
+ ext4_msg(sb, KERN_ERR, "metadata checksum seed changed during journal replay");
+ return -EFSCORRUPTED;
+ }
+
+ if (!ext4_feature_set_ok(sb, replay->readonly))
+ return -EINVAL;
+
+ err = ext4_check_geometry(sb, es, &groups_count);
+ if (err)
+ return err;
+
+ if (groups_count != sbi->s_groups_count ||
+ es->s_first_meta_bg != old->s_first_meta_bg ||
+ (incompat & EXT4_FEATURE_INCOMPAT_META_BG) ||
+ (ro_compat & EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER) ||
+ ((es->s_feature_compat ^ old->s_feature_compat) &
+ cpu_to_le32(EXT4_FEATURE_COMPAT_SPARSE_SUPER2)) ||
+ memcmp(es->s_backup_bgs, old->s_backup_bgs, sizeof(es->s_backup_bgs))) {
+ err = ext4_load_group_desc(sb, sb_block, groups_count);
+ if (err)
+ return err;
+ }
+
+ super_set_uuid(sb, es->s_uuid, sizeof(es->s_uuid));
+
+check_descriptors:
+ if (!ext4_check_descriptors(sb, sb_block, 0, replay->first_not_zeroed,
+ replay->readonly))
+ return -EFSCORRUPTED;
return 0;
}
static int ext4_load_and_init_journal(struct super_block *sb,
struct ext4_super_block *es,
- struct ext4_fs_context *ctx)
+ struct ext4_fs_context *ctx,
+ ext4_group_t *first_not_zeroed)
{
struct ext4_sb_info *sbi = EXT4_SB(sb);
int err;
- err = ext4_load_journal(sb, es, ctx->journal_devnum);
+ err = ext4_load_journal(sb, es, ctx->journal_devnum, first_not_zeroed);
if (err)
return err;
@@ -5406,7 +5542,7 @@ static int __ext4_fill_super(struct fs_context *fc, struct super_block *sb)
struct inode *root;
int needs_recovery;
int err;
- ext4_group_t first_not_zeroed;
+ ext4_group_t first_not_zeroed, groups_count;
struct ext4_fs_context *ctx = fc->fs_private;
int silent = fc->sb_flags & SB_SILENT;
@@ -5496,7 +5632,7 @@ static int __ext4_fill_super(struct fs_context *fc, struct super_block *sb)
if (err)
goto failed_mount;
- err = ext4_check_geometry(sb, es);
+ err = ext4_check_geometry(sb, es, &groups_count);
if (err)
goto failed_mount;
@@ -5505,10 +5641,19 @@ static int __ext4_fill_super(struct fs_context *fc, struct super_block *sb)
mutex_init(&sbi->s_error_notify_mutex);
INIT_WORK(&sbi->s_sb_upd_work, update_super_work);
- err = ext4_group_desc_init(sb, es, logical_sb_block, &first_not_zeroed);
+ bgl_lock_init(sbi->s_blockgroup_lock);
+ err = ext4_load_group_desc(sb, logical_sb_block, groups_count);
if (err)
goto failed_mount3;
+ if ((test_opt(sb, NOLOAD) || !ext4_has_feature_journal(sb) ||
+ !ext4_has_feature_journal_needs_recovery(sb)) &&
+ !ext4_check_descriptors(sb, logical_sb_block, 0, &first_not_zeroed,
+ sb_rdonly(sb))) {
+ err = -EFSCORRUPTED;
+ goto failed_mount3;
+ }
+
err = ext4_es_register_shrinker(sbi);
if (err)
goto failed_mount3;
@@ -5574,7 +5719,7 @@ static int __ext4_fill_super(struct fs_context *fc, struct super_block *sb)
* root first: it may be modified in the journal!
*/
if (!test_opt(sb, NOLOAD) && ext4_has_feature_journal(sb)) {
- err = ext4_load_and_init_journal(sb, es, ctx);
+ err = ext4_load_and_init_journal(sb, es, ctx, &first_not_zeroed);
if (err)
goto failed_mount3a;
if (bdev_read_only(sb->s_bdev))
@@ -6169,10 +6314,15 @@ out_bdev:
static int ext4_load_journal(struct super_block *sb,
struct ext4_super_block *es,
- unsigned long journal_devnum)
+ unsigned long journal_devnum,
+ ext4_group_t *first_not_zeroed)
{
+ struct ext4_sb_info *sbi = EXT4_SB(sb);
+ struct ext4_replay_callback_data *replay = NULL;
+ jbd2_replay_callback_t callback = NULL;
journal_t *journal;
unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
+ bool recover = ext4_has_feature_journal_needs_recovery(sb);
dev_t journal_dev;
int err = 0;
int really_read_only;
@@ -6197,6 +6347,10 @@ static int ext4_load_journal(struct super_block *sb,
}
if (journal_inum) {
+ if (recover &&
+ !ext4_check_descriptors(sb, sbi->s_sbh->b_blocknr,
+ journal_inum, NULL, sb_rdonly(sb)))
+ return -EFSCORRUPTED;
journal = ext4_open_inode_journal(sb, journal_inum);
if (IS_ERR(journal))
return PTR_ERR(journal);
@@ -6221,7 +6375,7 @@ static int ext4_load_journal(struct super_block *sb,
* crash? For recovery, we need to check in advance whether we
* can get read-write access to the device.
*/
- if (ext4_has_feature_journal_needs_recovery(sb)) {
+ if (recover) {
if (sb_rdonly(sb)) {
ext4_msg(sb, KERN_INFO, "INFO: recovery "
"required on readonly filesystem");
@@ -6241,8 +6395,19 @@ static int ext4_load_journal(struct super_block *sb,
if (!(journal->j_flags & JBD2_BARRIER))
ext4_msg(sb, KERN_INFO, "barriers disabled");
- if (!ext4_has_feature_journal_needs_recovery(sb))
+ if (recover) {
+ replay = kmalloc_obj(*replay);
+ if (!replay) {
+ err = -ENOMEM;
+ goto err_out;
+ }
+ replay->super = *es;
+ replay->first_not_zeroed = first_not_zeroed;
+ replay->readonly = sb_rdonly(sb);
+ callback = ext4_validate_replayed_metadata;
+ } else {
err = jbd2_journal_wipe(journal, !really_read_only);
+ }
if (!err) {
char *save = kmalloc(EXT4_S_ERR_LEN, GFP_KERNEL);
__le16 orig_state;
@@ -6261,9 +6426,11 @@ static int ext4_load_journal(struct super_block *sb,
*/
if (enable_write)
sb->s_flags &= ~SB_RDONLY;
- err = jbd2_journal_load(journal);
+ err = jbd2_journal_load_with_callback(journal, callback, replay);
if (enable_write)
sb->s_flags |= SB_RDONLY;
+ kfree(replay);
+
if (save && memcmp(((char *) es) + EXT4_S_ERR_START,
save, EXT4_S_ERR_LEN)) {
memcpy(((char *) es) + EXT4_S_ERR_START,
@@ -6285,6 +6452,7 @@ static int ext4_load_journal(struct super_block *sb,
}
if (err) {
+ ext4_fc_replay_cleanup(sb);
ext4_msg(sb, KERN_ERR, "error loading journal");
goto err_out;
}
^ permalink raw reply related [flat|nested] 5+ messages in thread