[PATCH 07/12] jbd2: add fast_commit space check

Zhang Yi
2023-07-04T13:44:15Z

From: Zhang Yi <yi.zhang@huawei.com>

If JBD2_FEATURE_INCOMPAT_FAST_COMMIT bit is set, it means the journal
have fast commit records need to recover, so the fast commit size
should not be zero, and also the leftover normal journal size should
never less than JBD2_MIN_JOURNAL_BLOCKS. Add a check into the
journal_check_superblock() and drop the pointless branch when
initializing in-memory fastcommit parameters.

Signed-off-by: Zhang Yi <yi.zhang@huawei.com>
---
fs/jbd2/journal.c | 18 ++++++++++++++----
1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/fs/jbd2/journal.c b/fs/jbd2/journal.c
index efdb8db3c06e..210b532a3673 100644
--- a/fs/jbd2/journal.c
+++ b/fs/jbd2/journal.c
@@ -1392,6 +1392,18 @@ static int journal_check_superblock(journal_t *journal)
return err;
}

+ if (jbd2_has_feature_fast_commit(journal)) {
+ int num_fc_blks = be32_to_cpu(sb->s_num_fc_blks);
+
+ if (!num_fc_blks ||
+ (be32_to_cpu(sb->s_maxlen) - num_fc_blks <
+ JBD2_MIN_JOURNAL_BLOCKS)) {
+ printk(KERN_ERR "JBD2: Invalid fast commit size %d\n",
+ num_fc_blks);
+ return err;
+ }
+ }
+
if (jbd2_has_feature_csum2(journal) &&
jbd2_has_feature_csum3(journal)) {
/* Can't have checksum v2 and v3 at the same time! */
@@ -1460,7 +1472,6 @@ static int journal_load_superblock(journal_t *journal)
int err;
struct buffer_head *bh;
journal_superblock_t *sb;
- int num_fc_blocks;

bh = getblk_unmovable(journal->j_dev, journal->j_blk_offset,
journal->j_blocksize);
@@ -1498,9 +1509,8 @@ static int journal_load_superblock(journal_t *journal)

if (jbd2_has_feature_fast_commit(journal)) {
journal->j_fc_last = be32_to_cpu(sb->s_maxlen);
- num_fc_blocks = jbd2_journal_get_num_fc_blks(sb);
- if (journal->j_last - num_fc_blocks >= JBD2_MIN_JOURNAL_BLOCKS)
- journal->j_last = journal->j_fc_last - num_fc_blocks;
+ journal->j_last = journal->j_fc_last -
+ be32_to_cpu(sb->s_num_fc_blks);
journal->j_fc_first = journal->j_last + 1;
journal->j_fc_off = 0;
}
--
2.39.2