From: Zhang Yi <[email protected]>
v1->v2:
- Fix the changelog in patch 1 and 2.
- Simplify the comments for local functions in patch 6.
- Remove the incorrect zero fast_commit blocks check in patch 7.
- Fix a UAF problem in patch 11.
Hello,
This patch set cleanup the journal load and initialization process
(discussed and suggested by Ted in [1]). Firstly, move reading of the
journal superblock from jbd2_journal_load() and jbd2_journal_wipe()
early to journal_init_common(), and completely drop the kludgy call of
journal_get_superblock() in jbd2_journal_check_used_features(). Then
cleanup the ext4_get_journal() and ext4_get_dev_journal(), making their
initialization process and error handling process more clear, and return
proper errno if some bad happens. Finally rename those two functions to
jbd2_open_{dev,inode}_journal. This patch set has passed
'kvm-xfstests -g auto'.
[1] https://lore.kernel.org/linux-ext4/[email protected]/
Thanks,
Yi.
Zhang Yi (12):
jbd2: move load_superblock() dependent functions
jbd2: move load_superblock() into journal_init_common()
jbd2: don't load superblock in jbd2_journal_check_used_features()
jbd2: checking valid features early in journal_get_superblock()
jbd2: open code jbd2_verify_csum_type() helper
jbd2: cleanup load_superblock()
jbd2: add fast_commit space check
jbd2: cleanup journal_init_common()
jbd2: drop useless error tag in jbd2_journal_wipe()
jbd2: jbd2_journal_init_{dev,inode} return proper error return value
ext4: cleanup ext4_get_dev_journal() and ext4_get_journal()
ext4: ext4_get_{dev}_journal return proper error value
fs/ext4/super.c | 154 ++++++++-------
fs/jbd2/journal.c | 465 +++++++++++++++++++++------------------------
fs/ocfs2/journal.c | 8 +-
3 files changed, 299 insertions(+), 328 deletions(-)
--
2.34.3
From: Zhang Yi <[email protected]>
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]>
---
fs/jbd2/journal.c | 85 +++++++++++++++++++----------------------------
1 file changed, 35 insertions(+), 50 deletions(-)
diff --git a/fs/jbd2/journal.c b/fs/jbd2/journal.c
index 46ab47b4439e..a8d17070073b 100644
--- a/fs/jbd2/journal.c
+++ b/fs/jbd2/journal.c
@@ -1341,45 +1341,29 @@ static void journal_fail_superblock(journal_t *journal)
}
/*
- * Read the superblock for a given journal, performing initial
+ * Check the superblock for a given journal, performing initial
* validation of the format.
*/
-static int journal_get_superblock(journal_t *journal)
+static int journal_check_superblock(journal_t *journal)
{
- struct buffer_head *bh;
- journal_superblock_t *sb;
- int err;
-
- bh = journal->j_sb_buffer;
-
- J_ASSERT(bh != NULL);
-
- err = bh_read(bh, 0);
- if (err < 0) {
- printk(KERN_ERR
- "JBD2: IO error reading journal superblock\n");
- goto out;
- }
-
- sb = journal->j_superblock;
-
- err = -EINVAL;
+ journal_superblock_t *sb = journal->j_superblock;
+ int err = -EINVAL;
if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
printk(KERN_WARNING "JBD2: no valid journal superblock found\n");
- goto out;
+ return err;
}
if (be32_to_cpu(sb->s_header.h_blocktype) != JBD2_SUPERBLOCK_V1 &&
be32_to_cpu(sb->s_header.h_blocktype) != JBD2_SUPERBLOCK_V2) {
printk(KERN_WARNING "JBD2: unrecognised superblock format ID\n");
- goto out;
+ return err;
}
if (be32_to_cpu(sb->s_maxlen) > journal->j_total_len) {
printk(KERN_WARNING "JBD2: journal file too short\n");
- goto out;
+ return err;
}
if (be32_to_cpu(sb->s_first) == 0 ||
@@ -1387,7 +1371,7 @@ static int journal_get_superblock(journal_t *journal)
printk(KERN_WARNING
"JBD2: Invalid start block of journal: %u\n",
be32_to_cpu(sb->s_first));
- goto out;
+ return err;
}
/*
@@ -1402,7 +1386,7 @@ static int journal_get_superblock(journal_t *journal)
(sb->s_feature_incompat &
~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) {
printk(KERN_WARNING "JBD2: Unrecognised features on journal\n");
- goto out;
+ return err;
}
if (jbd2_has_feature_csum2(journal) &&
@@ -1410,7 +1394,7 @@ static int journal_get_superblock(journal_t *journal)
/* Can't have checksum v2 and v3 at the same time! */
printk(KERN_ERR "JBD2: Can't enable checksumming v2 and v3 "
"at the same time!\n");
- goto out;
+ return err;
}
if (jbd2_journal_has_csum_v2or3_feature(journal) &&
@@ -1418,14 +1402,14 @@ static int journal_get_superblock(journal_t *journal)
/* Can't have checksum v1 and v2 on at the same time! */
printk(KERN_ERR "JBD2: Can't enable checksumming v1 and v2/3 "
"at the same time!\n");
- goto out;
+ return err;
}
/* Load the checksum driver */
if (jbd2_journal_has_csum_v2or3_feature(journal)) {
if (sb->s_checksum_type != JBD2_CRC32C_CHKSUM) {
printk(KERN_ERR "JBD2: Unknown checksum type\n");
- goto out;
+ return err;
}
journal->j_chksum_driver = crypto_alloc_shash("crc32c", 0, 0);
@@ -1433,20 +1417,17 @@ static int journal_get_superblock(journal_t *journal)
printk(KERN_ERR "JBD2: Cannot load crc32c driver.\n");
err = PTR_ERR(journal->j_chksum_driver);
journal->j_chksum_driver = NULL;
- goto out;
+ return err;
}
/* Check superblock checksum */
if (sb->s_checksum != jbd2_superblock_csum(journal, sb)) {
printk(KERN_ERR "JBD2: journal checksum error\n");
err = -EFSBADCRC;
- goto out;
+ return err;
}
}
- return 0;
-out:
- journal_fail_superblock(journal);
- return err;
+ return 0;
}
static int journal_revoke_records_per_block(journal_t *journal)
@@ -1468,17 +1449,31 @@ static int journal_revoke_records_per_block(journal_t *journal)
* Load the on-disk journal superblock and read the key fields into the
* journal_t.
*/
-static int load_superblock(journal_t *journal)
+static int journal_load_superblock(journal_t *journal)
{
int err;
+ struct buffer_head *bh;
journal_superblock_t *sb;
int num_fc_blocks;
- err = journal_get_superblock(journal);
- if (err)
- return err;
+ bh = getblk_unmovable(journal->j_dev, journal->j_blk_offset,
+ journal->j_blocksize);
+ if (bh)
+ err = bh_read(bh, 0);
+ if (!bh || err < 0) {
+ pr_err("%s: Cannot read journal superblock\n", __func__);
+ brelse(bh);
+ return -EIO;
+ }
- sb = journal->j_superblock;
+ journal->j_sb_buffer = bh;
+ sb = (journal_superblock_t *)bh->b_data;
+ journal->j_superblock = sb;
+ err = journal_check_superblock(journal);
+ if (err) {
+ journal_fail_superblock(journal);
+ return err;
+ }
journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
journal->j_tail = be32_to_cpu(sb->s_start);
@@ -1524,7 +1519,6 @@ static journal_t *journal_init_common(struct block_device *bdev,
static struct lock_class_key jbd2_trans_commit_key;
journal_t *journal;
int err;
- struct buffer_head *bh;
int n;
journal = kzalloc(sizeof(*journal), GFP_KERNEL);
@@ -1577,16 +1571,7 @@ static journal_t *journal_init_common(struct block_device *bdev,
if (!journal->j_wbuf)
goto err_cleanup;
- bh = getblk_unmovable(journal->j_dev, start, journal->j_blocksize);
- if (!bh) {
- pr_err("%s: Cannot get buffer for journal superblock\n",
- __func__);
- goto err_cleanup;
- }
- journal->j_sb_buffer = bh;
- journal->j_superblock = (journal_superblock_t *)bh->b_data;
-
- err = load_superblock(journal);
+ err = journal_load_superblock(journal);
if (err)
goto err_cleanup;
--
2.34.3
From: Zhang Yi <[email protected]>
Move load_superblock() declaration and the functions it calls before
journal_init_common(). This is a preparation for moving a call to
load_superblock() from jbd2_journal_load() and jbd2_journal_wipe() to
journal_init_common(). No functional changes.
Signed-off-by: Zhang Yi <[email protected]>
Reviewed-by: Jan Kara <[email protected]>
---
fs/jbd2/journal.c | 337 +++++++++++++++++++++++-----------------------
1 file changed, 168 insertions(+), 169 deletions(-)
diff --git a/fs/jbd2/journal.c b/fs/jbd2/journal.c
index fbce16fedaa4..48c44c7fccf4 100644
--- a/fs/jbd2/journal.c
+++ b/fs/jbd2/journal.c
@@ -1336,6 +1336,174 @@ static unsigned long jbd2_journal_shrink_count(struct shrinker *shrink,
return count;
}
+/*
+ * If the journal init or create aborts, we need to mark the journal
+ * superblock as being NULL to prevent the journal destroy from writing
+ * back a bogus superblock.
+ */
+static void journal_fail_superblock(journal_t *journal)
+{
+ struct buffer_head *bh = journal->j_sb_buffer;
+ brelse(bh);
+ journal->j_sb_buffer = NULL;
+}
+
+/*
+ * Read the superblock for a given journal, performing initial
+ * validation of the format.
+ */
+static int journal_get_superblock(journal_t *journal)
+{
+ struct buffer_head *bh;
+ journal_superblock_t *sb;
+ int err;
+
+ bh = journal->j_sb_buffer;
+
+ J_ASSERT(bh != NULL);
+ if (buffer_verified(bh))
+ return 0;
+
+ err = bh_read(bh, 0);
+ if (err < 0) {
+ printk(KERN_ERR
+ "JBD2: IO error reading journal superblock\n");
+ goto out;
+ }
+
+ sb = journal->j_superblock;
+
+ err = -EINVAL;
+
+ if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
+ sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
+ printk(KERN_WARNING "JBD2: no valid journal superblock found\n");
+ goto out;
+ }
+
+ if (be32_to_cpu(sb->s_header.h_blocktype) != JBD2_SUPERBLOCK_V1 &&
+ be32_to_cpu(sb->s_header.h_blocktype) != JBD2_SUPERBLOCK_V2) {
+ printk(KERN_WARNING "JBD2: unrecognised superblock format ID\n");
+ goto out;
+ }
+
+ if (be32_to_cpu(sb->s_maxlen) > journal->j_total_len) {
+ printk(KERN_WARNING "JBD2: journal file too short\n");
+ goto out;
+ }
+
+ if (be32_to_cpu(sb->s_first) == 0 ||
+ be32_to_cpu(sb->s_first) >= journal->j_total_len) {
+ printk(KERN_WARNING
+ "JBD2: Invalid start block of journal: %u\n",
+ be32_to_cpu(sb->s_first));
+ goto out;
+ }
+
+ if (jbd2_has_feature_csum2(journal) &&
+ jbd2_has_feature_csum3(journal)) {
+ /* Can't have checksum v2 and v3 at the same time! */
+ printk(KERN_ERR "JBD2: Can't enable checksumming v2 and v3 "
+ "at the same time!\n");
+ goto out;
+ }
+
+ if (jbd2_journal_has_csum_v2or3_feature(journal) &&
+ jbd2_has_feature_checksum(journal)) {
+ /* Can't have checksum v1 and v2 on at the same time! */
+ printk(KERN_ERR "JBD2: Can't enable checksumming v1 and v2/3 "
+ "at the same time!\n");
+ goto out;
+ }
+
+ if (!jbd2_verify_csum_type(journal, sb)) {
+ printk(KERN_ERR "JBD2: Unknown checksum type\n");
+ goto out;
+ }
+
+ /* Load the checksum driver */
+ if (jbd2_journal_has_csum_v2or3_feature(journal)) {
+ journal->j_chksum_driver = crypto_alloc_shash("crc32c", 0, 0);
+ if (IS_ERR(journal->j_chksum_driver)) {
+ printk(KERN_ERR "JBD2: Cannot load crc32c driver.\n");
+ err = PTR_ERR(journal->j_chksum_driver);
+ journal->j_chksum_driver = NULL;
+ goto out;
+ }
+ /* Check superblock checksum */
+ if (sb->s_checksum != jbd2_superblock_csum(journal, sb)) {
+ printk(KERN_ERR "JBD2: journal checksum error\n");
+ err = -EFSBADCRC;
+ goto out;
+ }
+ }
+ set_buffer_verified(bh);
+ return 0;
+
+out:
+ journal_fail_superblock(journal);
+ return err;
+}
+
+static int journal_revoke_records_per_block(journal_t *journal)
+{
+ int record_size;
+ int space = journal->j_blocksize - sizeof(jbd2_journal_revoke_header_t);
+
+ if (jbd2_has_feature_64bit(journal))
+ record_size = 8;
+ else
+ record_size = 4;
+
+ if (jbd2_journal_has_csum_v2or3(journal))
+ space -= sizeof(struct jbd2_journal_block_tail);
+ return space / record_size;
+}
+
+/*
+ * Load the on-disk journal superblock and read the key fields into the
+ * journal_t.
+ */
+static int load_superblock(journal_t *journal)
+{
+ int err;
+ journal_superblock_t *sb;
+ int num_fc_blocks;
+
+ err = journal_get_superblock(journal);
+ if (err)
+ return err;
+
+ sb = journal->j_superblock;
+
+ journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
+ journal->j_tail = be32_to_cpu(sb->s_start);
+ journal->j_first = be32_to_cpu(sb->s_first);
+ journal->j_errno = be32_to_cpu(sb->s_errno);
+ journal->j_last = be32_to_cpu(sb->s_maxlen);
+
+ if (be32_to_cpu(sb->s_maxlen) < journal->j_total_len)
+ journal->j_total_len = be32_to_cpu(sb->s_maxlen);
+ /* Precompute checksum seed for all metadata */
+ if (jbd2_journal_has_csum_v2or3(journal))
+ journal->j_csum_seed = jbd2_chksum(journal, ~0, sb->s_uuid,
+ sizeof(sb->s_uuid));
+ journal->j_revoke_records_per_block =
+ journal_revoke_records_per_block(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_fc_first = journal->j_last + 1;
+ journal->j_fc_off = 0;
+ }
+
+ return 0;
+}
+
+
/*
* Management for journal control blocks: functions to create and
* destroy journal_t structures, and to initialise and read existing
@@ -1521,18 +1689,6 @@ journal_t *jbd2_journal_init_inode(struct inode *inode)
return journal;
}
-/*
- * If the journal init or create aborts, we need to mark the journal
- * superblock as being NULL to prevent the journal destroy from writing
- * back a bogus superblock.
- */
-static void journal_fail_superblock(journal_t *journal)
-{
- struct buffer_head *bh = journal->j_sb_buffer;
- brelse(bh);
- journal->j_sb_buffer = NULL;
-}
-
/*
* Given a journal_t structure, initialise the various fields for
* startup of a new journaling session. We use this both when creating
@@ -1889,163 +2045,6 @@ void jbd2_journal_update_sb_errno(journal_t *journal)
}
EXPORT_SYMBOL(jbd2_journal_update_sb_errno);
-static int journal_revoke_records_per_block(journal_t *journal)
-{
- int record_size;
- int space = journal->j_blocksize - sizeof(jbd2_journal_revoke_header_t);
-
- if (jbd2_has_feature_64bit(journal))
- record_size = 8;
- else
- record_size = 4;
-
- if (jbd2_journal_has_csum_v2or3(journal))
- space -= sizeof(struct jbd2_journal_block_tail);
- return space / record_size;
-}
-
-/*
- * Read the superblock for a given journal, performing initial
- * validation of the format.
- */
-static int journal_get_superblock(journal_t *journal)
-{
- struct buffer_head *bh;
- journal_superblock_t *sb;
- int err;
-
- bh = journal->j_sb_buffer;
-
- J_ASSERT(bh != NULL);
- if (buffer_verified(bh))
- return 0;
-
- err = bh_read(bh, 0);
- if (err < 0) {
- printk(KERN_ERR
- "JBD2: IO error reading journal superblock\n");
- goto out;
- }
-
- sb = journal->j_superblock;
-
- err = -EINVAL;
-
- if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
- sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
- printk(KERN_WARNING "JBD2: no valid journal superblock found\n");
- goto out;
- }
-
- if (be32_to_cpu(sb->s_header.h_blocktype) != JBD2_SUPERBLOCK_V1 &&
- be32_to_cpu(sb->s_header.h_blocktype) != JBD2_SUPERBLOCK_V2) {
- printk(KERN_WARNING "JBD2: unrecognised superblock format ID\n");
- goto out;
- }
-
- if (be32_to_cpu(sb->s_maxlen) > journal->j_total_len) {
- printk(KERN_WARNING "JBD2: journal file too short\n");
- goto out;
- }
-
- if (be32_to_cpu(sb->s_first) == 0 ||
- be32_to_cpu(sb->s_first) >= journal->j_total_len) {
- printk(KERN_WARNING
- "JBD2: Invalid start block of journal: %u\n",
- be32_to_cpu(sb->s_first));
- goto out;
- }
-
- if (jbd2_has_feature_csum2(journal) &&
- jbd2_has_feature_csum3(journal)) {
- /* Can't have checksum v2 and v3 at the same time! */
- printk(KERN_ERR "JBD2: Can't enable checksumming v2 and v3 "
- "at the same time!\n");
- goto out;
- }
-
- if (jbd2_journal_has_csum_v2or3_feature(journal) &&
- jbd2_has_feature_checksum(journal)) {
- /* Can't have checksum v1 and v2 on at the same time! */
- printk(KERN_ERR "JBD2: Can't enable checksumming v1 and v2/3 "
- "at the same time!\n");
- goto out;
- }
-
- if (!jbd2_verify_csum_type(journal, sb)) {
- printk(KERN_ERR "JBD2: Unknown checksum type\n");
- goto out;
- }
-
- /* Load the checksum driver */
- if (jbd2_journal_has_csum_v2or3_feature(journal)) {
- journal->j_chksum_driver = crypto_alloc_shash("crc32c", 0, 0);
- if (IS_ERR(journal->j_chksum_driver)) {
- printk(KERN_ERR "JBD2: Cannot load crc32c driver.\n");
- err = PTR_ERR(journal->j_chksum_driver);
- journal->j_chksum_driver = NULL;
- goto out;
- }
- /* Check superblock checksum */
- if (sb->s_checksum != jbd2_superblock_csum(journal, sb)) {
- printk(KERN_ERR "JBD2: journal checksum error\n");
- err = -EFSBADCRC;
- goto out;
- }
- }
- set_buffer_verified(bh);
- return 0;
-
-out:
- journal_fail_superblock(journal);
- return err;
-}
-
-/*
- * Load the on-disk journal superblock and read the key fields into the
- * journal_t.
- */
-
-static int load_superblock(journal_t *journal)
-{
- int err;
- journal_superblock_t *sb;
- int num_fc_blocks;
-
- err = journal_get_superblock(journal);
- if (err)
- return err;
-
- sb = journal->j_superblock;
-
- journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
- journal->j_tail = be32_to_cpu(sb->s_start);
- journal->j_first = be32_to_cpu(sb->s_first);
- journal->j_errno = be32_to_cpu(sb->s_errno);
- journal->j_last = be32_to_cpu(sb->s_maxlen);
-
- if (be32_to_cpu(sb->s_maxlen) < journal->j_total_len)
- journal->j_total_len = be32_to_cpu(sb->s_maxlen);
- /* Precompute checksum seed for all metadata */
- if (jbd2_journal_has_csum_v2or3(journal))
- journal->j_csum_seed = jbd2_chksum(journal, ~0, sb->s_uuid,
- sizeof(sb->s_uuid));
- journal->j_revoke_records_per_block =
- journal_revoke_records_per_block(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_fc_first = journal->j_last + 1;
- journal->j_fc_off = 0;
- }
-
- return 0;
-}
-
-
/**
* jbd2_journal_load() - Read journal from disk.
* @journal: Journal to act on.
--
2.34.3
From: Zhang Yi <[email protected]>
jbd2_verify_csum_type() helper check checksum type in the superblock for
v2 or v3 checksum feature, it always return true if these features are
not enabled, and it has only one user, so open code it is more clear.
Signed-off-by: Zhang Yi <[email protected]>
Reviewed-by: Jan Kara <[email protected]>
---
fs/jbd2/journal.c | 18 +++++-------------
1 file changed, 5 insertions(+), 13 deletions(-)
diff --git a/fs/jbd2/journal.c b/fs/jbd2/journal.c
index d84f26b08315..46ab47b4439e 100644
--- a/fs/jbd2/journal.c
+++ b/fs/jbd2/journal.c
@@ -115,14 +115,6 @@ void __jbd2_debug(int level, const char *file, const char *func,
#endif
/* Checksumming functions */
-static int jbd2_verify_csum_type(journal_t *j, journal_superblock_t *sb)
-{
- if (!jbd2_journal_has_csum_v2or3_feature(j))
- return 1;
-
- return sb->s_checksum_type == JBD2_CRC32C_CHKSUM;
-}
-
static __be32 jbd2_superblock_csum(journal_t *j, journal_superblock_t *sb)
{
__u32 csum;
@@ -1429,13 +1421,13 @@ static int journal_get_superblock(journal_t *journal)
goto out;
}
- if (!jbd2_verify_csum_type(journal, sb)) {
- printk(KERN_ERR "JBD2: Unknown checksum type\n");
- goto out;
- }
-
/* Load the checksum driver */
if (jbd2_journal_has_csum_v2or3_feature(journal)) {
+ if (sb->s_checksum_type != JBD2_CRC32C_CHKSUM) {
+ printk(KERN_ERR "JBD2: Unknown checksum type\n");
+ goto out;
+ }
+
journal->j_chksum_driver = crypto_alloc_shash("crc32c", 0, 0);
if (IS_ERR(journal->j_chksum_driver)) {
printk(KERN_ERR "JBD2: Cannot load crc32c driver.\n");
--
2.34.3
From: Zhang Yi <[email protected]>
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 too large, and the leftover normal journal size should
never less than JBD2_MIN_JOURNAL_BLOCKS. If it happens, the
journal->j_last is likely to be wrong and will probably lead to
incorrect journal recovery. So add a check into the
journal_check_superblock(), and drop the pointless check when
initializing the fastcommit parameters.
Signed-off-by: Zhang Yi <[email protected]>
---
fs/jbd2/journal.c | 15 +++++++++++----
1 file changed, 11 insertions(+), 4 deletions(-)
diff --git a/fs/jbd2/journal.c b/fs/jbd2/journal.c
index a8d17070073b..5e2206e7a5ba 100644
--- a/fs/jbd2/journal.c
+++ b/fs/jbd2/journal.c
@@ -1347,6 +1347,7 @@ static void journal_fail_superblock(journal_t *journal)
static int journal_check_superblock(journal_t *journal)
{
journal_superblock_t *sb = journal->j_superblock;
+ int num_fc_blks;
int err = -EINVAL;
if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
@@ -1389,6 +1390,14 @@ static int journal_check_superblock(journal_t *journal)
return err;
}
+ num_fc_blks = jbd2_has_feature_fast_commit(journal) ?
+ jbd2_journal_get_num_fc_blks(sb) : 0;
+ if (be32_to_cpu(sb->s_maxlen) < JBD2_MIN_JOURNAL_BLOCKS + num_fc_blks) {
+ printk(KERN_ERR "JBD2: journal file too short %u,%d\n",
+ be32_to_cpu(sb->s_maxlen), 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! */
@@ -1454,7 +1463,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);
@@ -1492,9 +1500,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 -
+ jbd2_journal_get_num_fc_blks(sb);
journal->j_fc_first = journal->j_last + 1;
journal->j_fc_off = 0;
}
--
2.34.3
From: Zhang Yi <[email protected]>
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]>
---
fs/ext4/super.c | 109 ++++++++++++++++++++++--------------------------
1 file changed, 49 insertions(+), 60 deletions(-)
diff --git a/fs/ext4/super.c b/fs/ext4/super.c
index ce2e02b139af..2f71076f678a 100644
--- a/fs/ext4/super.c
+++ b/fs/ext4/super.c
@@ -1105,26 +1105,6 @@ static const struct blk_holder_ops ext4_holder_ops = {
.mark_dead = ext4_bdev_mark_dead,
};
-/*
- * Open the external journal device
- */
-static struct block_device *ext4_blkdev_get(dev_t dev, struct super_block *sb)
-{
- struct block_device *bdev;
-
- bdev = blkdev_get_by_dev(dev, BLK_OPEN_READ | BLK_OPEN_WRITE, sb,
- &ext4_holder_ops);
- if (IS_ERR(bdev))
- goto fail;
- return bdev;
-
-fail:
- ext4_msg(sb, KERN_ERR,
- "failed to open journal device unknown-block(%u,%u) %ld",
- MAJOR(dev), MINOR(dev), PTR_ERR(bdev));
- return NULL;
-}
-
/*
* Release the journal device
*/
@@ -5780,14 +5760,14 @@ static struct inode *ext4_get_journal_inode(struct super_block *sb,
ext4_msg(sb, KERN_ERR, "journal inode is deleted");
return NULL;
}
-
- ext4_debug("Journal inode found at %p: %lld bytes\n",
- journal_inode, journal_inode->i_size);
if (!S_ISREG(journal_inode->i_mode) || IS_ENCRYPTED(journal_inode)) {
ext4_msg(sb, KERN_ERR, "invalid journal inode");
iput(journal_inode);
return NULL;
}
+
+ ext4_debug("Journal inode found at %p: %lld bytes\n",
+ journal_inode, journal_inode->i_size);
return journal_inode;
}
@@ -5819,9 +5799,6 @@ static journal_t *ext4_get_journal(struct super_block *sb,
struct inode *journal_inode;
journal_t *journal;
- if (WARN_ON_ONCE(!ext4_has_feature_journal(sb)))
- return NULL;
-
journal_inode = ext4_get_journal_inode(sb, journal_inum);
if (!journal_inode)
return NULL;
@@ -5838,25 +5815,25 @@ static journal_t *ext4_get_journal(struct super_block *sb,
return journal;
}
-static journal_t *ext4_get_dev_journal(struct super_block *sb,
- dev_t j_dev)
+static struct block_device *ext4_get_journal_dev(struct super_block *sb,
+ dev_t j_dev, ext4_fsblk_t *j_start,
+ ext4_fsblk_t *j_len)
{
struct buffer_head *bh;
- journal_t *journal;
- ext4_fsblk_t start;
- ext4_fsblk_t len;
+ struct block_device *bdev;
int hblock, blocksize;
ext4_fsblk_t sb_block;
unsigned long offset;
struct ext4_super_block *es;
- struct block_device *bdev;
- if (WARN_ON_ONCE(!ext4_has_feature_journal(sb)))
- return NULL;
-
- bdev = ext4_blkdev_get(j_dev, sb);
- if (bdev == NULL)
+ bdev = blkdev_get_by_dev(j_dev, BLK_OPEN_READ | BLK_OPEN_WRITE, sb,
+ &ext4_holder_ops);
+ if (IS_ERR(bdev)) {
+ ext4_msg(sb, KERN_ERR,
+ "failed to open journal device unknown-block(%u,%u) %ld",
+ MAJOR(j_dev), MINOR(j_dev), PTR_ERR(bdev));
return NULL;
+ }
blocksize = sb->s_blocksize;
hblock = bdev_logical_block_size(bdev);
@@ -5869,7 +5846,8 @@ static journal_t *ext4_get_dev_journal(struct super_block *sb,
sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
offset = EXT4_MIN_BLOCK_SIZE % blocksize;
set_blocksize(bdev, blocksize);
- if (!(bh = __bread(bdev, sb_block, blocksize))) {
+ bh = __bread(bdev, sb_block, blocksize);
+ if (!bh) {
ext4_msg(sb, KERN_ERR, "couldn't read superblock of "
"external journal");
goto out_bdev;
@@ -5879,56 +5857,67 @@ static journal_t *ext4_get_dev_journal(struct super_block *sb,
if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
!(le32_to_cpu(es->s_feature_incompat) &
EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
- ext4_msg(sb, KERN_ERR, "external journal has "
- "bad superblock");
- brelse(bh);
- goto out_bdev;
+ ext4_msg(sb, KERN_ERR, "external journal has bad superblock");
+ goto out_bh;
}
if ((le32_to_cpu(es->s_feature_ro_compat) &
EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) &&
es->s_checksum != ext4_superblock_csum(sb, es)) {
- ext4_msg(sb, KERN_ERR, "external journal has "
- "corrupt superblock");
- brelse(bh);
- goto out_bdev;
+ ext4_msg(sb, KERN_ERR, "external journal has corrupt superblock");
+ goto out_bh;
}
if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
ext4_msg(sb, KERN_ERR, "journal UUID does not match");
- brelse(bh);
- goto out_bdev;
+ goto out_bh;
}
- len = ext4_blocks_count(es);
- start = sb_block + 1;
- brelse(bh); /* we're done with the superblock */
+ *j_start = sb_block + 1;
+ *j_len = ext4_blocks_count(es);
+ brelse(bh);
+ return bdev;
+
+out_bh:
+ brelse(bh);
+out_bdev:
+ blkdev_put(bdev, sb);
+ return NULL;
+}
+
+static journal_t *ext4_get_dev_journal(struct super_block *sb,
+ dev_t j_dev)
+{
+ journal_t *journal;
+ ext4_fsblk_t j_start;
+ ext4_fsblk_t j_len;
+ struct block_device *journal_bdev;
+
+ journal_bdev = ext4_get_journal_dev(sb, j_dev, &j_start, &j_len);
+ if (!journal_bdev)
+ return NULL;
- journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
- start, len, blocksize);
+ journal = jbd2_journal_init_dev(journal_bdev, sb->s_bdev, j_start,
+ j_len, sb->s_blocksize);
if (IS_ERR(journal)) {
ext4_msg(sb, KERN_ERR, "failed to create device journal");
goto out_bdev;
}
- journal->j_private = sb;
- if (ext4_read_bh_lock(journal->j_sb_buffer, REQ_META | REQ_PRIO, true)) {
- ext4_msg(sb, KERN_ERR, "I/O error on journal device");
- goto out_journal;
- }
if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
ext4_msg(sb, KERN_ERR, "External journal has more than one "
"user (unsupported) - %d",
be32_to_cpu(journal->j_superblock->s_nr_users));
goto out_journal;
}
- EXT4_SB(sb)->s_journal_bdev = bdev;
+ journal->j_private = sb;
+ EXT4_SB(sb)->s_journal_bdev = journal_bdev;
ext4_init_journal_params(sb, journal);
return journal;
out_journal:
jbd2_journal_destroy(journal);
out_bdev:
- blkdev_put(bdev, sb);
+ blkdev_put(journal_bdev, sb);
return NULL;
}
--
2.34.3
From: Zhang Yi <[email protected]>
ext4_get_journal() and ext4_get_dev_journal() return NULL if they failed
to init journal, making them return proper error value instead, also
rename them to ext4_open_{inode,dev}_journal().
Signed-off-by: Zhang Yi <[email protected]>
Reviewed-by: Jan Kara <[email protected]>
---
fs/ext4/super.c | 51 +++++++++++++++++++++++++++++--------------------
1 file changed, 30 insertions(+), 21 deletions(-)
diff --git a/fs/ext4/super.c b/fs/ext4/super.c
index 2f71076f678a..797c5456e9bc 100644
--- a/fs/ext4/super.c
+++ b/fs/ext4/super.c
@@ -5752,18 +5752,18 @@ static struct inode *ext4_get_journal_inode(struct super_block *sb,
journal_inode = ext4_iget(sb, journal_inum, EXT4_IGET_SPECIAL);
if (IS_ERR(journal_inode)) {
ext4_msg(sb, KERN_ERR, "no journal found");
- return NULL;
+ return ERR_CAST(journal_inode);
}
if (!journal_inode->i_nlink) {
make_bad_inode(journal_inode);
iput(journal_inode);
ext4_msg(sb, KERN_ERR, "journal inode is deleted");
- return NULL;
+ return ERR_PTR(-EFSCORRUPTED);
}
if (!S_ISREG(journal_inode->i_mode) || IS_ENCRYPTED(journal_inode)) {
ext4_msg(sb, KERN_ERR, "invalid journal inode");
iput(journal_inode);
- return NULL;
+ return ERR_PTR(-EFSCORRUPTED);
}
ext4_debug("Journal inode found at %p: %lld bytes\n",
@@ -5793,21 +5793,21 @@ static int ext4_journal_bmap(journal_t *journal, sector_t *block)
return 0;
}
-static journal_t *ext4_get_journal(struct super_block *sb,
- unsigned int journal_inum)
+static journal_t *ext4_open_inode_journal(struct super_block *sb,
+ unsigned int journal_inum)
{
struct inode *journal_inode;
journal_t *journal;
journal_inode = ext4_get_journal_inode(sb, journal_inum);
- if (!journal_inode)
- return NULL;
+ if (IS_ERR(journal_inode))
+ return ERR_CAST(journal_inode);
journal = jbd2_journal_init_inode(journal_inode);
if (IS_ERR(journal)) {
ext4_msg(sb, KERN_ERR, "Could not load journal inode");
iput(journal_inode);
- return NULL;
+ return ERR_CAST(journal);
}
journal->j_private = sb;
journal->j_bmap = ext4_journal_bmap;
@@ -5825,6 +5825,7 @@ static struct block_device *ext4_get_journal_dev(struct super_block *sb,
ext4_fsblk_t sb_block;
unsigned long offset;
struct ext4_super_block *es;
+ int errno;
bdev = blkdev_get_by_dev(j_dev, BLK_OPEN_READ | BLK_OPEN_WRITE, sb,
&ext4_holder_ops);
@@ -5832,7 +5833,7 @@ static struct block_device *ext4_get_journal_dev(struct super_block *sb,
ext4_msg(sb, KERN_ERR,
"failed to open journal device unknown-block(%u,%u) %ld",
MAJOR(j_dev), MINOR(j_dev), PTR_ERR(bdev));
- return NULL;
+ return ERR_CAST(bdev);
}
blocksize = sb->s_blocksize;
@@ -5840,6 +5841,7 @@ static struct block_device *ext4_get_journal_dev(struct super_block *sb,
if (blocksize < hblock) {
ext4_msg(sb, KERN_ERR,
"blocksize too small for journal device");
+ errno = -EINVAL;
goto out_bdev;
}
@@ -5850,6 +5852,7 @@ static struct block_device *ext4_get_journal_dev(struct super_block *sb,
if (!bh) {
ext4_msg(sb, KERN_ERR, "couldn't read superblock of "
"external journal");
+ errno = -EINVAL;
goto out_bdev;
}
@@ -5858,6 +5861,7 @@ static struct block_device *ext4_get_journal_dev(struct super_block *sb,
!(le32_to_cpu(es->s_feature_incompat) &
EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
ext4_msg(sb, KERN_ERR, "external journal has bad superblock");
+ errno = -EFSCORRUPTED;
goto out_bh;
}
@@ -5865,11 +5869,13 @@ static struct block_device *ext4_get_journal_dev(struct super_block *sb,
EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) &&
es->s_checksum != ext4_superblock_csum(sb, es)) {
ext4_msg(sb, KERN_ERR, "external journal has corrupt superblock");
+ errno = -EFSCORRUPTED;
goto out_bh;
}
if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
ext4_msg(sb, KERN_ERR, "journal UUID does not match");
+ errno = -EFSCORRUPTED;
goto out_bh;
}
@@ -5882,31 +5888,34 @@ static struct block_device *ext4_get_journal_dev(struct super_block *sb,
brelse(bh);
out_bdev:
blkdev_put(bdev, sb);
- return NULL;
+ return ERR_PTR(errno);
}
-static journal_t *ext4_get_dev_journal(struct super_block *sb,
- dev_t j_dev)
+static journal_t *ext4_open_dev_journal(struct super_block *sb,
+ dev_t j_dev)
{
journal_t *journal;
ext4_fsblk_t j_start;
ext4_fsblk_t j_len;
struct block_device *journal_bdev;
+ int errno = 0;
journal_bdev = ext4_get_journal_dev(sb, j_dev, &j_start, &j_len);
- if (!journal_bdev)
- return NULL;
+ if (IS_ERR(journal_bdev))
+ return ERR_CAST(journal_bdev);
journal = jbd2_journal_init_dev(journal_bdev, sb->s_bdev, j_start,
j_len, sb->s_blocksize);
if (IS_ERR(journal)) {
ext4_msg(sb, KERN_ERR, "failed to create device journal");
+ errno = PTR_ERR(journal);
goto out_bdev;
}
if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
ext4_msg(sb, KERN_ERR, "External journal has more than one "
"user (unsupported) - %d",
be32_to_cpu(journal->j_superblock->s_nr_users));
+ errno = -EINVAL;
goto out_journal;
}
journal->j_private = sb;
@@ -5918,7 +5927,7 @@ static journal_t *ext4_get_dev_journal(struct super_block *sb,
jbd2_journal_destroy(journal);
out_bdev:
blkdev_put(journal_bdev, sb);
- return NULL;
+ return ERR_PTR(errno);
}
static int ext4_load_journal(struct super_block *sb,
@@ -5950,13 +5959,13 @@ static int ext4_load_journal(struct super_block *sb,
}
if (journal_inum) {
- journal = ext4_get_journal(sb, journal_inum);
- if (!journal)
- return -EINVAL;
+ journal = ext4_open_inode_journal(sb, journal_inum);
+ if (IS_ERR(journal))
+ return PTR_ERR(journal);
} else {
- journal = ext4_get_dev_journal(sb, journal_dev);
- if (!journal)
- return -EINVAL;
+ journal = ext4_open_dev_journal(sb, journal_dev);
+ if (IS_ERR(journal))
+ return PTR_ERR(journal);
}
journal_dev_ro = bdev_read_only(journal->j_dev);
--
2.34.3
On Thu 10-08-23 16:54:12, Zhang Yi wrote:
> From: Zhang Yi <[email protected]>
>
> 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 too large, and the leftover normal journal size should
> never less than JBD2_MIN_JOURNAL_BLOCKS. If it happens, the
> journal->j_last is likely to be wrong and will probably lead to
> incorrect journal recovery. So add a check into the
> journal_check_superblock(), and drop the pointless check when
> initializing the fastcommit parameters.
>
> Signed-off-by: Zhang Yi <[email protected]>
Just one small note below. With that fixed feel free to add:
Reviewed-by: Jan Kara <[email protected]>
> @@ -1389,6 +1390,14 @@ static int journal_check_superblock(journal_t *journal)
> return err;
> }
>
> + num_fc_blks = jbd2_has_feature_fast_commit(journal) ?
> + jbd2_journal_get_num_fc_blks(sb) : 0;
> + if (be32_to_cpu(sb->s_maxlen) < JBD2_MIN_JOURNAL_BLOCKS + num_fc_blks) {
To avoid possible overflow of the right hand side, we should probably do
the check like:
if (be32_to_cpu(sb->s_maxlen) < JBD2_MIN_JOURNAL_BLOCKS ||
be32_to_cpu(sb->s_maxlen) - JBD2_MIN_JOURNAL_BLOCKS < num_fc_blks) {
...
}
Honza
--
Jan Kara <[email protected]>
SUSE Labs, CR
On Thu 10-08-23 16:54:16, Zhang Yi wrote:
> From: Zhang Yi <[email protected]>
>
> 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]>
Just one nit: We have ext4_get_dev_journal() function and now you create
ext4_get_journal_dev() helper. These are confusingly similar names. So I
suggest you rename the new helper to something like
ext4_get_journal_blkdev() to make the difference clearer. Otherwise feel
free to add:
Reviewed-by: Jan Kara <[email protected]>
Honza
> ---
> fs/ext4/super.c | 109 ++++++++++++++++++++++--------------------------
> 1 file changed, 49 insertions(+), 60 deletions(-)
>
> diff --git a/fs/ext4/super.c b/fs/ext4/super.c
> index ce2e02b139af..2f71076f678a 100644
> --- a/fs/ext4/super.c
> +++ b/fs/ext4/super.c
> @@ -1105,26 +1105,6 @@ static const struct blk_holder_ops ext4_holder_ops = {
> .mark_dead = ext4_bdev_mark_dead,
> };
>
> -/*
> - * Open the external journal device
> - */
> -static struct block_device *ext4_blkdev_get(dev_t dev, struct super_block *sb)
> -{
> - struct block_device *bdev;
> -
> - bdev = blkdev_get_by_dev(dev, BLK_OPEN_READ | BLK_OPEN_WRITE, sb,
> - &ext4_holder_ops);
> - if (IS_ERR(bdev))
> - goto fail;
> - return bdev;
> -
> -fail:
> - ext4_msg(sb, KERN_ERR,
> - "failed to open journal device unknown-block(%u,%u) %ld",
> - MAJOR(dev), MINOR(dev), PTR_ERR(bdev));
> - return NULL;
> -}
> -
> /*
> * Release the journal device
> */
> @@ -5780,14 +5760,14 @@ static struct inode *ext4_get_journal_inode(struct super_block *sb,
> ext4_msg(sb, KERN_ERR, "journal inode is deleted");
> return NULL;
> }
> -
> - ext4_debug("Journal inode found at %p: %lld bytes\n",
> - journal_inode, journal_inode->i_size);
> if (!S_ISREG(journal_inode->i_mode) || IS_ENCRYPTED(journal_inode)) {
> ext4_msg(sb, KERN_ERR, "invalid journal inode");
> iput(journal_inode);
> return NULL;
> }
> +
> + ext4_debug("Journal inode found at %p: %lld bytes\n",
> + journal_inode, journal_inode->i_size);
> return journal_inode;
> }
>
> @@ -5819,9 +5799,6 @@ static journal_t *ext4_get_journal(struct super_block *sb,
> struct inode *journal_inode;
> journal_t *journal;
>
> - if (WARN_ON_ONCE(!ext4_has_feature_journal(sb)))
> - return NULL;
> -
> journal_inode = ext4_get_journal_inode(sb, journal_inum);
> if (!journal_inode)
> return NULL;
> @@ -5838,25 +5815,25 @@ static journal_t *ext4_get_journal(struct super_block *sb,
> return journal;
> }
>
> -static journal_t *ext4_get_dev_journal(struct super_block *sb,
> - dev_t j_dev)
> +static struct block_device *ext4_get_journal_dev(struct super_block *sb,
> + dev_t j_dev, ext4_fsblk_t *j_start,
> + ext4_fsblk_t *j_len)
> {
> struct buffer_head *bh;
> - journal_t *journal;
> - ext4_fsblk_t start;
> - ext4_fsblk_t len;
> + struct block_device *bdev;
> int hblock, blocksize;
> ext4_fsblk_t sb_block;
> unsigned long offset;
> struct ext4_super_block *es;
> - struct block_device *bdev;
>
> - if (WARN_ON_ONCE(!ext4_has_feature_journal(sb)))
> - return NULL;
> -
> - bdev = ext4_blkdev_get(j_dev, sb);
> - if (bdev == NULL)
> + bdev = blkdev_get_by_dev(j_dev, BLK_OPEN_READ | BLK_OPEN_WRITE, sb,
> + &ext4_holder_ops);
> + if (IS_ERR(bdev)) {
> + ext4_msg(sb, KERN_ERR,
> + "failed to open journal device unknown-block(%u,%u) %ld",
> + MAJOR(j_dev), MINOR(j_dev), PTR_ERR(bdev));
> return NULL;
> + }
>
> blocksize = sb->s_blocksize;
> hblock = bdev_logical_block_size(bdev);
> @@ -5869,7 +5846,8 @@ static journal_t *ext4_get_dev_journal(struct super_block *sb,
> sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
> offset = EXT4_MIN_BLOCK_SIZE % blocksize;
> set_blocksize(bdev, blocksize);
> - if (!(bh = __bread(bdev, sb_block, blocksize))) {
> + bh = __bread(bdev, sb_block, blocksize);
> + if (!bh) {
> ext4_msg(sb, KERN_ERR, "couldn't read superblock of "
> "external journal");
> goto out_bdev;
> @@ -5879,56 +5857,67 @@ static journal_t *ext4_get_dev_journal(struct super_block *sb,
> if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
> !(le32_to_cpu(es->s_feature_incompat) &
> EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
> - ext4_msg(sb, KERN_ERR, "external journal has "
> - "bad superblock");
> - brelse(bh);
> - goto out_bdev;
> + ext4_msg(sb, KERN_ERR, "external journal has bad superblock");
> + goto out_bh;
> }
>
> if ((le32_to_cpu(es->s_feature_ro_compat) &
> EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) &&
> es->s_checksum != ext4_superblock_csum(sb, es)) {
> - ext4_msg(sb, KERN_ERR, "external journal has "
> - "corrupt superblock");
> - brelse(bh);
> - goto out_bdev;
> + ext4_msg(sb, KERN_ERR, "external journal has corrupt superblock");
> + goto out_bh;
> }
>
> if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
> ext4_msg(sb, KERN_ERR, "journal UUID does not match");
> - brelse(bh);
> - goto out_bdev;
> + goto out_bh;
> }
>
> - len = ext4_blocks_count(es);
> - start = sb_block + 1;
> - brelse(bh); /* we're done with the superblock */
> + *j_start = sb_block + 1;
> + *j_len = ext4_blocks_count(es);
> + brelse(bh);
> + return bdev;
> +
> +out_bh:
> + brelse(bh);
> +out_bdev:
> + blkdev_put(bdev, sb);
> + return NULL;
> +}
> +
> +static journal_t *ext4_get_dev_journal(struct super_block *sb,
> + dev_t j_dev)
> +{
> + journal_t *journal;
> + ext4_fsblk_t j_start;
> + ext4_fsblk_t j_len;
> + struct block_device *journal_bdev;
> +
> + journal_bdev = ext4_get_journal_dev(sb, j_dev, &j_start, &j_len);
> + if (!journal_bdev)
> + return NULL;
>
> - journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
> - start, len, blocksize);
> + journal = jbd2_journal_init_dev(journal_bdev, sb->s_bdev, j_start,
> + j_len, sb->s_blocksize);
> if (IS_ERR(journal)) {
> ext4_msg(sb, KERN_ERR, "failed to create device journal");
> goto out_bdev;
> }
> - journal->j_private = sb;
> - if (ext4_read_bh_lock(journal->j_sb_buffer, REQ_META | REQ_PRIO, true)) {
> - ext4_msg(sb, KERN_ERR, "I/O error on journal device");
> - goto out_journal;
> - }
> if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
> ext4_msg(sb, KERN_ERR, "External journal has more than one "
> "user (unsupported) - %d",
> be32_to_cpu(journal->j_superblock->s_nr_users));
> goto out_journal;
> }
> - EXT4_SB(sb)->s_journal_bdev = bdev;
> + journal->j_private = sb;
> + EXT4_SB(sb)->s_journal_bdev = journal_bdev;
> ext4_init_journal_params(sb, journal);
> return journal;
>
> out_journal:
> jbd2_journal_destroy(journal);
> out_bdev:
> - blkdev_put(bdev, sb);
> + blkdev_put(journal_bdev, sb);
> return NULL;
> }
>
> --
> 2.34.3
>
--
Jan Kara <[email protected]>
SUSE Labs, CR
On Thu 10-08-23 16:54:11, Zhang Yi wrote:
> From: Zhang Yi <[email protected]>
>
> 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]>
Looks good. Feel free to add:
Reviewed-by: Jan Kara <[email protected]>
Honza
> ---
> fs/jbd2/journal.c | 85 +++++++++++++++++++----------------------------
> 1 file changed, 35 insertions(+), 50 deletions(-)
>
> diff --git a/fs/jbd2/journal.c b/fs/jbd2/journal.c
> index 46ab47b4439e..a8d17070073b 100644
> --- a/fs/jbd2/journal.c
> +++ b/fs/jbd2/journal.c
> @@ -1341,45 +1341,29 @@ static void journal_fail_superblock(journal_t *journal)
> }
>
> /*
> - * Read the superblock for a given journal, performing initial
> + * Check the superblock for a given journal, performing initial
> * validation of the format.
> */
> -static int journal_get_superblock(journal_t *journal)
> +static int journal_check_superblock(journal_t *journal)
> {
> - struct buffer_head *bh;
> - journal_superblock_t *sb;
> - int err;
> -
> - bh = journal->j_sb_buffer;
> -
> - J_ASSERT(bh != NULL);
> -
> - err = bh_read(bh, 0);
> - if (err < 0) {
> - printk(KERN_ERR
> - "JBD2: IO error reading journal superblock\n");
> - goto out;
> - }
> -
> - sb = journal->j_superblock;
> -
> - err = -EINVAL;
> + journal_superblock_t *sb = journal->j_superblock;
> + int err = -EINVAL;
>
> if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
> sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
> printk(KERN_WARNING "JBD2: no valid journal superblock found\n");
> - goto out;
> + return err;
> }
>
> if (be32_to_cpu(sb->s_header.h_blocktype) != JBD2_SUPERBLOCK_V1 &&
> be32_to_cpu(sb->s_header.h_blocktype) != JBD2_SUPERBLOCK_V2) {
> printk(KERN_WARNING "JBD2: unrecognised superblock format ID\n");
> - goto out;
> + return err;
> }
>
> if (be32_to_cpu(sb->s_maxlen) > journal->j_total_len) {
> printk(KERN_WARNING "JBD2: journal file too short\n");
> - goto out;
> + return err;
> }
>
> if (be32_to_cpu(sb->s_first) == 0 ||
> @@ -1387,7 +1371,7 @@ static int journal_get_superblock(journal_t *journal)
> printk(KERN_WARNING
> "JBD2: Invalid start block of journal: %u\n",
> be32_to_cpu(sb->s_first));
> - goto out;
> + return err;
> }
>
> /*
> @@ -1402,7 +1386,7 @@ static int journal_get_superblock(journal_t *journal)
> (sb->s_feature_incompat &
> ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) {
> printk(KERN_WARNING "JBD2: Unrecognised features on journal\n");
> - goto out;
> + return err;
> }
>
> if (jbd2_has_feature_csum2(journal) &&
> @@ -1410,7 +1394,7 @@ static int journal_get_superblock(journal_t *journal)
> /* Can't have checksum v2 and v3 at the same time! */
> printk(KERN_ERR "JBD2: Can't enable checksumming v2 and v3 "
> "at the same time!\n");
> - goto out;
> + return err;
> }
>
> if (jbd2_journal_has_csum_v2or3_feature(journal) &&
> @@ -1418,14 +1402,14 @@ static int journal_get_superblock(journal_t *journal)
> /* Can't have checksum v1 and v2 on at the same time! */
> printk(KERN_ERR "JBD2: Can't enable checksumming v1 and v2/3 "
> "at the same time!\n");
> - goto out;
> + return err;
> }
>
> /* Load the checksum driver */
> if (jbd2_journal_has_csum_v2or3_feature(journal)) {
> if (sb->s_checksum_type != JBD2_CRC32C_CHKSUM) {
> printk(KERN_ERR "JBD2: Unknown checksum type\n");
> - goto out;
> + return err;
> }
>
> journal->j_chksum_driver = crypto_alloc_shash("crc32c", 0, 0);
> @@ -1433,20 +1417,17 @@ static int journal_get_superblock(journal_t *journal)
> printk(KERN_ERR "JBD2: Cannot load crc32c driver.\n");
> err = PTR_ERR(journal->j_chksum_driver);
> journal->j_chksum_driver = NULL;
> - goto out;
> + return err;
> }
> /* Check superblock checksum */
> if (sb->s_checksum != jbd2_superblock_csum(journal, sb)) {
> printk(KERN_ERR "JBD2: journal checksum error\n");
> err = -EFSBADCRC;
> - goto out;
> + return err;
> }
> }
> - return 0;
>
> -out:
> - journal_fail_superblock(journal);
> - return err;
> + return 0;
> }
>
> static int journal_revoke_records_per_block(journal_t *journal)
> @@ -1468,17 +1449,31 @@ static int journal_revoke_records_per_block(journal_t *journal)
> * Load the on-disk journal superblock and read the key fields into the
> * journal_t.
> */
> -static int load_superblock(journal_t *journal)
> +static int journal_load_superblock(journal_t *journal)
> {
> int err;
> + struct buffer_head *bh;
> journal_superblock_t *sb;
> int num_fc_blocks;
>
> - err = journal_get_superblock(journal);
> - if (err)
> - return err;
> + bh = getblk_unmovable(journal->j_dev, journal->j_blk_offset,
> + journal->j_blocksize);
> + if (bh)
> + err = bh_read(bh, 0);
> + if (!bh || err < 0) {
> + pr_err("%s: Cannot read journal superblock\n", __func__);
> + brelse(bh);
> + return -EIO;
> + }
>
> - sb = journal->j_superblock;
> + journal->j_sb_buffer = bh;
> + sb = (journal_superblock_t *)bh->b_data;
> + journal->j_superblock = sb;
> + err = journal_check_superblock(journal);
> + if (err) {
> + journal_fail_superblock(journal);
> + return err;
> + }
>
> journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
> journal->j_tail = be32_to_cpu(sb->s_start);
> @@ -1524,7 +1519,6 @@ static journal_t *journal_init_common(struct block_device *bdev,
> static struct lock_class_key jbd2_trans_commit_key;
> journal_t *journal;
> int err;
> - struct buffer_head *bh;
> int n;
>
> journal = kzalloc(sizeof(*journal), GFP_KERNEL);
> @@ -1577,16 +1571,7 @@ static journal_t *journal_init_common(struct block_device *bdev,
> if (!journal->j_wbuf)
> goto err_cleanup;
>
> - bh = getblk_unmovable(journal->j_dev, start, journal->j_blocksize);
> - if (!bh) {
> - pr_err("%s: Cannot get buffer for journal superblock\n",
> - __func__);
> - goto err_cleanup;
> - }
> - journal->j_sb_buffer = bh;
> - journal->j_superblock = (journal_superblock_t *)bh->b_data;
> -
> - err = load_superblock(journal);
> + err = journal_load_superblock(journal);
> if (err)
> goto err_cleanup;
>
> --
> 2.34.3
>
--
Jan Kara <[email protected]>
SUSE Labs, CR