Skip to content

Commit bc74e6a

Browse files
zhangyi089tytso
authored andcommitted
ext4: cleanup ext4_get_dev_journal() and ext4_get_journal()
Factor out a new helper form ext4_get_dev_journal() to get external journal bdev and check validation of this device, drop ext4_blkdev_get() helper, and also remove duplicate check of journal feature. It makes ext4_get_dev_journal() more clear than before. 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 8e6cf5f commit bc74e6a

File tree

1 file changed

+49
-60
lines changed

1 file changed

+49
-60
lines changed

fs/ext4/super.c

Lines changed: 49 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,26 +1105,6 @@ static const struct blk_holder_ops ext4_holder_ops = {
11051105
.mark_dead = ext4_bdev_mark_dead,
11061106
};
11071107

1108-
/*
1109-
* Open the external journal device
1110-
*/
1111-
static struct block_device *ext4_blkdev_get(dev_t dev, struct super_block *sb)
1112-
{
1113-
struct block_device *bdev;
1114-
1115-
bdev = blkdev_get_by_dev(dev, BLK_OPEN_READ | BLK_OPEN_WRITE, sb,
1116-
&ext4_holder_ops);
1117-
if (IS_ERR(bdev))
1118-
goto fail;
1119-
return bdev;
1120-
1121-
fail:
1122-
ext4_msg(sb, KERN_ERR,
1123-
"failed to open journal device unknown-block(%u,%u) %ld",
1124-
MAJOR(dev), MINOR(dev), PTR_ERR(bdev));
1125-
return NULL;
1126-
}
1127-
11281108
/*
11291109
* Release the journal device
11301110
*/
@@ -5768,14 +5748,14 @@ static struct inode *ext4_get_journal_inode(struct super_block *sb,
57685748
ext4_msg(sb, KERN_ERR, "journal inode is deleted");
57695749
return NULL;
57705750
}
5771-
5772-
ext4_debug("Journal inode found at %p: %lld bytes\n",
5773-
journal_inode, journal_inode->i_size);
57745751
if (!S_ISREG(journal_inode->i_mode) || IS_ENCRYPTED(journal_inode)) {
57755752
ext4_msg(sb, KERN_ERR, "invalid journal inode");
57765753
iput(journal_inode);
57775754
return NULL;
57785755
}
5756+
5757+
ext4_debug("Journal inode found at %p: %lld bytes\n",
5758+
journal_inode, journal_inode->i_size);
57795759
return journal_inode;
57805760
}
57815761

@@ -5807,9 +5787,6 @@ static journal_t *ext4_get_journal(struct super_block *sb,
58075787
struct inode *journal_inode;
58085788
journal_t *journal;
58095789

5810-
if (WARN_ON_ONCE(!ext4_has_feature_journal(sb)))
5811-
return NULL;
5812-
58135790
journal_inode = ext4_get_journal_inode(sb, journal_inum);
58145791
if (!journal_inode)
58155792
return NULL;
@@ -5826,25 +5803,25 @@ static journal_t *ext4_get_journal(struct super_block *sb,
58265803
return journal;
58275804
}
58285805

5829-
static journal_t *ext4_get_dev_journal(struct super_block *sb,
5830-
dev_t j_dev)
5806+
static struct block_device *ext4_get_journal_blkdev(struct super_block *sb,
5807+
dev_t j_dev, ext4_fsblk_t *j_start,
5808+
ext4_fsblk_t *j_len)
58315809
{
58325810
struct buffer_head *bh;
5833-
journal_t *journal;
5834-
ext4_fsblk_t start;
5835-
ext4_fsblk_t len;
5811+
struct block_device *bdev;
58365812
int hblock, blocksize;
58375813
ext4_fsblk_t sb_block;
58385814
unsigned long offset;
58395815
struct ext4_super_block *es;
5840-
struct block_device *bdev;
58415816

5842-
if (WARN_ON_ONCE(!ext4_has_feature_journal(sb)))
5843-
return NULL;
5844-
5845-
bdev = ext4_blkdev_get(j_dev, sb);
5846-
if (bdev == NULL)
5817+
bdev = blkdev_get_by_dev(j_dev, BLK_OPEN_READ | BLK_OPEN_WRITE, sb,
5818+
&ext4_holder_ops);
5819+
if (IS_ERR(bdev)) {
5820+
ext4_msg(sb, KERN_ERR,
5821+
"failed to open journal device unknown-block(%u,%u) %ld",
5822+
MAJOR(j_dev), MINOR(j_dev), PTR_ERR(bdev));
58475823
return NULL;
5824+
}
58485825

58495826
blocksize = sb->s_blocksize;
58505827
hblock = bdev_logical_block_size(bdev);
@@ -5857,7 +5834,8 @@ static journal_t *ext4_get_dev_journal(struct super_block *sb,
58575834
sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
58585835
offset = EXT4_MIN_BLOCK_SIZE % blocksize;
58595836
set_blocksize(bdev, blocksize);
5860-
if (!(bh = __bread(bdev, sb_block, blocksize))) {
5837+
bh = __bread(bdev, sb_block, blocksize);
5838+
if (!bh) {
58615839
ext4_msg(sb, KERN_ERR, "couldn't read superblock of "
58625840
"external journal");
58635841
goto out_bdev;
@@ -5867,56 +5845,67 @@ static journal_t *ext4_get_dev_journal(struct super_block *sb,
58675845
if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
58685846
!(le32_to_cpu(es->s_feature_incompat) &
58695847
EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
5870-
ext4_msg(sb, KERN_ERR, "external journal has "
5871-
"bad superblock");
5872-
brelse(bh);
5873-
goto out_bdev;
5848+
ext4_msg(sb, KERN_ERR, "external journal has bad superblock");
5849+
goto out_bh;
58745850
}
58755851

58765852
if ((le32_to_cpu(es->s_feature_ro_compat) &
58775853
EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) &&
58785854
es->s_checksum != ext4_superblock_csum(sb, es)) {
5879-
ext4_msg(sb, KERN_ERR, "external journal has "
5880-
"corrupt superblock");
5881-
brelse(bh);
5882-
goto out_bdev;
5855+
ext4_msg(sb, KERN_ERR, "external journal has corrupt superblock");
5856+
goto out_bh;
58835857
}
58845858

58855859
if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
58865860
ext4_msg(sb, KERN_ERR, "journal UUID does not match");
5887-
brelse(bh);
5888-
goto out_bdev;
5861+
goto out_bh;
58895862
}
58905863

5891-
len = ext4_blocks_count(es);
5892-
start = sb_block + 1;
5893-
brelse(bh); /* we're done with the superblock */
5864+
*j_start = sb_block + 1;
5865+
*j_len = ext4_blocks_count(es);
5866+
brelse(bh);
5867+
return bdev;
5868+
5869+
out_bh:
5870+
brelse(bh);
5871+
out_bdev:
5872+
blkdev_put(bdev, sb);
5873+
return NULL;
5874+
}
5875+
5876+
static journal_t *ext4_get_dev_journal(struct super_block *sb,
5877+
dev_t j_dev)
5878+
{
5879+
journal_t *journal;
5880+
ext4_fsblk_t j_start;
5881+
ext4_fsblk_t j_len;
5882+
struct block_device *journal_bdev;
5883+
5884+
journal_bdev = ext4_get_journal_blkdev(sb, j_dev, &j_start, &j_len);
5885+
if (!journal_bdev)
5886+
return NULL;
58945887

5895-
journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
5896-
start, len, blocksize);
5888+
journal = jbd2_journal_init_dev(journal_bdev, sb->s_bdev, j_start,
5889+
j_len, sb->s_blocksize);
58975890
if (IS_ERR(journal)) {
58985891
ext4_msg(sb, KERN_ERR, "failed to create device journal");
58995892
goto out_bdev;
59005893
}
5901-
journal->j_private = sb;
5902-
if (ext4_read_bh_lock(journal->j_sb_buffer, REQ_META | REQ_PRIO, true)) {
5903-
ext4_msg(sb, KERN_ERR, "I/O error on journal device");
5904-
goto out_journal;
5905-
}
59065894
if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
59075895
ext4_msg(sb, KERN_ERR, "External journal has more than one "
59085896
"user (unsupported) - %d",
59095897
be32_to_cpu(journal->j_superblock->s_nr_users));
59105898
goto out_journal;
59115899
}
5912-
EXT4_SB(sb)->s_journal_bdev = bdev;
5900+
journal->j_private = sb;
5901+
EXT4_SB(sb)->s_journal_bdev = journal_bdev;
59135902
ext4_init_journal_params(sb, journal);
59145903
return journal;
59155904

59165905
out_journal:
59175906
jbd2_journal_destroy(journal);
59185907
out_bdev:
5919-
blkdev_put(bdev, sb);
5908+
blkdev_put(journal_bdev, sb);
59205909
return NULL;
59215910
}
59225911

0 commit comments

Comments
 (0)