Skip to content

Commit 6abfe10

Browse files
Ye Bintytso
authored andcommitted
jbd2: fix the inconsistency between checksum and data in memory for journal sb
Copying the file system while it is mounted as read-only results in a mount failure: [~]# mkfs.ext4 -F /dev/sdc [~]# mount /dev/sdc -o ro /mnt/test [~]# dd if=/dev/sdc of=/dev/sda bs=1M [~]# mount /dev/sda /mnt/test1 [ 1094.849826] JBD2: journal checksum error [ 1094.850927] EXT4-fs (sda): Could not load journal inode mount: mount /dev/sda on /mnt/test1 failed: Bad message The process described above is just an abstracted way I came up with to reproduce the issue. In the actual scenario, the file system was mounted read-only and then copied while it was still mounted. It was found that the mount operation failed. The user intended to verify the data or use it as a backup, and this action was performed during a version upgrade. Above issue may happen as follows: ext4_fill_super set_journal_csum_feature_set(sb) if (ext4_has_metadata_csum(sb)) incompat = JBD2_FEATURE_INCOMPAT_CSUM_V3; if (test_opt(sb, JOURNAL_CHECKSUM) jbd2_journal_set_features(sbi->s_journal, compat, 0, incompat); lock_buffer(journal->j_sb_buffer); sb->s_feature_incompat |= cpu_to_be32(incompat); //The data in the journal sb was modified, but the checksum was not updated, so the data remaining in memory has a mismatch between the data and the checksum. unlock_buffer(journal->j_sb_buffer); In this case, the journal sb copied over is in a state where the checksum and data are inconsistent, so mounting fails. To solve the above issue, update the checksum in memory after modifying the journal sb. Fixes: 4fd5ea4 ("jbd2: checksum journal superblock") Signed-off-by: Ye Bin <yebin10@huawei.com> Reviewed-by: Baokun Li <libaokun1@huawei.com> Reviewed-by: Darrick J. Wong <djwong@kernel.org> Reviewed-by: Jan Kara <jack@suse.cz> Message-ID: <20251103010123.3753631-1-yebin@huaweicloud.com> Signed-off-by: Theodore Ts'o <tytso@mit.edu> Cc: stable@kernel.org
1 parent 3db63d2 commit 6abfe10

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

fs/jbd2/journal.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2354,6 +2354,12 @@ int jbd2_journal_set_features(journal_t *journal, unsigned long compat,
23542354
sb->s_feature_compat |= cpu_to_be32(compat);
23552355
sb->s_feature_ro_compat |= cpu_to_be32(ro);
23562356
sb->s_feature_incompat |= cpu_to_be32(incompat);
2357+
/*
2358+
* Update the checksum now so that it is valid even for read-only
2359+
* filesystems where jbd2_write_superblock() doesn't get called.
2360+
*/
2361+
if (jbd2_journal_has_csum_v2or3(journal))
2362+
sb->s_checksum = jbd2_superblock_csum(sb);
23572363
unlock_buffer(journal->j_sb_buffer);
23582364
jbd2_journal_init_transaction_limits(journal);
23592365

@@ -2383,9 +2389,17 @@ void jbd2_journal_clear_features(journal_t *journal, unsigned long compat,
23832389

23842390
sb = journal->j_superblock;
23852391

2392+
lock_buffer(journal->j_sb_buffer);
23862393
sb->s_feature_compat &= ~cpu_to_be32(compat);
23872394
sb->s_feature_ro_compat &= ~cpu_to_be32(ro);
23882395
sb->s_feature_incompat &= ~cpu_to_be32(incompat);
2396+
/*
2397+
* Update the checksum now so that it is valid even for read-only
2398+
* filesystems where jbd2_write_superblock() doesn't get called.
2399+
*/
2400+
if (jbd2_journal_has_csum_v2or3(journal))
2401+
sb->s_checksum = jbd2_superblock_csum(sb);
2402+
unlock_buffer(journal->j_sb_buffer);
23892403
jbd2_journal_init_transaction_limits(journal);
23902404
}
23912405
EXPORT_SYMBOL(jbd2_journal_clear_features);

0 commit comments

Comments
 (0)