Skip to content

Commit 054d9c8

Browse files
zhangyi089tytso
authored andcommitted
jbd2: cleanup load_superblock()
Rename load_superblock() to journal_load_superblock(), move getting and reading superblock from journal_init_common() and journal_get_superblock() to this function, and also rename journal_get_superblock() to journal_check_superblock(), make it a pure check helper to check superblock validity from disk. Signed-off-by: Zhang Yi <[email protected]> Reviewed-by: Jan Kara <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Theodore Ts'o <[email protected]>
1 parent 18dad50 commit 054d9c8

File tree

1 file changed

+35
-50
lines changed

1 file changed

+35
-50
lines changed

fs/jbd2/journal.c

Lines changed: 35 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,53 +1341,37 @@ static void journal_fail_superblock(journal_t *journal)
13411341
}
13421342

13431343
/*
1344-
* Read the superblock for a given journal, performing initial
1344+
* Check the superblock for a given journal, performing initial
13451345
* validation of the format.
13461346
*/
1347-
static int journal_get_superblock(journal_t *journal)
1347+
static int journal_check_superblock(journal_t *journal)
13481348
{
1349-
struct buffer_head *bh;
1350-
journal_superblock_t *sb;
1351-
int err;
1352-
1353-
bh = journal->j_sb_buffer;
1354-
1355-
J_ASSERT(bh != NULL);
1356-
1357-
err = bh_read(bh, 0);
1358-
if (err < 0) {
1359-
printk(KERN_ERR
1360-
"JBD2: IO error reading journal superblock\n");
1361-
goto out;
1362-
}
1363-
1364-
sb = journal->j_superblock;
1365-
1366-
err = -EINVAL;
1349+
journal_superblock_t *sb = journal->j_superblock;
1350+
int err = -EINVAL;
13671351

13681352
if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
13691353
sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
13701354
printk(KERN_WARNING "JBD2: no valid journal superblock found\n");
1371-
goto out;
1355+
return err;
13721356
}
13731357

13741358
if (be32_to_cpu(sb->s_header.h_blocktype) != JBD2_SUPERBLOCK_V1 &&
13751359
be32_to_cpu(sb->s_header.h_blocktype) != JBD2_SUPERBLOCK_V2) {
13761360
printk(KERN_WARNING "JBD2: unrecognised superblock format ID\n");
1377-
goto out;
1361+
return err;
13781362
}
13791363

13801364
if (be32_to_cpu(sb->s_maxlen) > journal->j_total_len) {
13811365
printk(KERN_WARNING "JBD2: journal file too short\n");
1382-
goto out;
1366+
return err;
13831367
}
13841368

13851369
if (be32_to_cpu(sb->s_first) == 0 ||
13861370
be32_to_cpu(sb->s_first) >= journal->j_total_len) {
13871371
printk(KERN_WARNING
13881372
"JBD2: Invalid start block of journal: %u\n",
13891373
be32_to_cpu(sb->s_first));
1390-
goto out;
1374+
return err;
13911375
}
13921376

13931377
/*
@@ -1402,51 +1386,48 @@ static int journal_get_superblock(journal_t *journal)
14021386
(sb->s_feature_incompat &
14031387
~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) {
14041388
printk(KERN_WARNING "JBD2: Unrecognised features on journal\n");
1405-
goto out;
1389+
return err;
14061390
}
14071391

14081392
if (jbd2_has_feature_csum2(journal) &&
14091393
jbd2_has_feature_csum3(journal)) {
14101394
/* Can't have checksum v2 and v3 at the same time! */
14111395
printk(KERN_ERR "JBD2: Can't enable checksumming v2 and v3 "
14121396
"at the same time!\n");
1413-
goto out;
1397+
return err;
14141398
}
14151399

14161400
if (jbd2_journal_has_csum_v2or3_feature(journal) &&
14171401
jbd2_has_feature_checksum(journal)) {
14181402
/* Can't have checksum v1 and v2 on at the same time! */
14191403
printk(KERN_ERR "JBD2: Can't enable checksumming v1 and v2/3 "
14201404
"at the same time!\n");
1421-
goto out;
1405+
return err;
14221406
}
14231407

14241408
/* Load the checksum driver */
14251409
if (jbd2_journal_has_csum_v2or3_feature(journal)) {
14261410
if (sb->s_checksum_type != JBD2_CRC32C_CHKSUM) {
14271411
printk(KERN_ERR "JBD2: Unknown checksum type\n");
1428-
goto out;
1412+
return err;
14291413
}
14301414

14311415
journal->j_chksum_driver = crypto_alloc_shash("crc32c", 0, 0);
14321416
if (IS_ERR(journal->j_chksum_driver)) {
14331417
printk(KERN_ERR "JBD2: Cannot load crc32c driver.\n");
14341418
err = PTR_ERR(journal->j_chksum_driver);
14351419
journal->j_chksum_driver = NULL;
1436-
goto out;
1420+
return err;
14371421
}
14381422
/* Check superblock checksum */
14391423
if (sb->s_checksum != jbd2_superblock_csum(journal, sb)) {
14401424
printk(KERN_ERR "JBD2: journal checksum error\n");
14411425
err = -EFSBADCRC;
1442-
goto out;
1426+
return err;
14431427
}
14441428
}
1445-
return 0;
14461429

1447-
out:
1448-
journal_fail_superblock(journal);
1449-
return err;
1430+
return 0;
14501431
}
14511432

14521433
static int journal_revoke_records_per_block(journal_t *journal)
@@ -1468,17 +1449,31 @@ static int journal_revoke_records_per_block(journal_t *journal)
14681449
* Load the on-disk journal superblock and read the key fields into the
14691450
* journal_t.
14701451
*/
1471-
static int load_superblock(journal_t *journal)
1452+
static int journal_load_superblock(journal_t *journal)
14721453
{
14731454
int err;
1455+
struct buffer_head *bh;
14741456
journal_superblock_t *sb;
14751457
int num_fc_blocks;
14761458

1477-
err = journal_get_superblock(journal);
1478-
if (err)
1479-
return err;
1459+
bh = getblk_unmovable(journal->j_dev, journal->j_blk_offset,
1460+
journal->j_blocksize);
1461+
if (bh)
1462+
err = bh_read(bh, 0);
1463+
if (!bh || err < 0) {
1464+
pr_err("%s: Cannot read journal superblock\n", __func__);
1465+
brelse(bh);
1466+
return -EIO;
1467+
}
14801468

1481-
sb = journal->j_superblock;
1469+
journal->j_sb_buffer = bh;
1470+
sb = (journal_superblock_t *)bh->b_data;
1471+
journal->j_superblock = sb;
1472+
err = journal_check_superblock(journal);
1473+
if (err) {
1474+
journal_fail_superblock(journal);
1475+
return err;
1476+
}
14821477

14831478
journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
14841479
journal->j_tail = be32_to_cpu(sb->s_start);
@@ -1524,7 +1519,6 @@ static journal_t *journal_init_common(struct block_device *bdev,
15241519
static struct lock_class_key jbd2_trans_commit_key;
15251520
journal_t *journal;
15261521
int err;
1527-
struct buffer_head *bh;
15281522
int n;
15291523

15301524
journal = kzalloc(sizeof(*journal), GFP_KERNEL);
@@ -1577,16 +1571,7 @@ static journal_t *journal_init_common(struct block_device *bdev,
15771571
if (!journal->j_wbuf)
15781572
goto err_cleanup;
15791573

1580-
bh = getblk_unmovable(journal->j_dev, start, journal->j_blocksize);
1581-
if (!bh) {
1582-
pr_err("%s: Cannot get buffer for journal superblock\n",
1583-
__func__);
1584-
goto err_cleanup;
1585-
}
1586-
journal->j_sb_buffer = bh;
1587-
journal->j_superblock = (journal_superblock_t *)bh->b_data;
1588-
1589-
err = load_superblock(journal);
1574+
err = journal_load_superblock(journal);
15901575
if (err)
15911576
goto err_cleanup;
15921577

0 commit comments

Comments
 (0)