Skip to content

Commit 51bce6c

Browse files
Johannes Thumshirnkdave
authored andcommitted
btrfs: Simplify btrfs_check_super_csum() and get rid of size assumptions
Now that we have already checked for a valid checksum type before calling btrfs_check_super_csum(), it can be simplified even further. While at it get rid of the implicit size assumption of the resulting checksum as well. This is a preparation for changing all checksum functionality to use the crypto layer later. Reviewed-by: Nikolay Borisov <[email protected]> Signed-off-by: Johannes Thumshirn <[email protected]> Reviewed-by: David Sterba <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent 8dc3f22 commit 51bce6c

File tree

1 file changed

+16
-26
lines changed

1 file changed

+16
-26
lines changed

fs/btrfs/disk-io.c

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -371,30 +371,20 @@ static int btrfs_check_super_csum(struct btrfs_fs_info *fs_info,
371371
{
372372
struct btrfs_super_block *disk_sb =
373373
(struct btrfs_super_block *)raw_disk_sb;
374-
u16 csum_type = btrfs_super_csum_type(disk_sb);
375-
376-
if (!btrfs_supported_super_csum(csum_type)) {
377-
btrfs_err(fs_info, "unsupported checksum algorithm %u",
378-
csum_type);
379-
return 1;
380-
}
381-
382-
if (csum_type == BTRFS_CSUM_TYPE_CRC32) {
383-
u32 crc = ~(u32)0;
384-
char result[sizeof(crc)];
374+
u32 crc = ~(u32)0;
375+
char result[BTRFS_CSUM_SIZE];
385376

386-
/*
387-
* The super_block structure does not span the whole
388-
* BTRFS_SUPER_INFO_SIZE range, we expect that the unused space
389-
* is filled with zeros and is included in the checksum.
390-
*/
391-
crc = btrfs_csum_data(raw_disk_sb + BTRFS_CSUM_SIZE,
392-
crc, BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
393-
btrfs_csum_final(crc, result);
377+
/*
378+
* The super_block structure does not span the whole
379+
* BTRFS_SUPER_INFO_SIZE range, we expect that the unused space is
380+
* filled with zeros and is included in the checksum.
381+
*/
382+
crc = btrfs_csum_data(raw_disk_sb + BTRFS_CSUM_SIZE,
383+
crc, BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
384+
btrfs_csum_final(crc, result);
394385

395-
if (memcmp(raw_disk_sb, result, sizeof(result)))
396-
return 1;
397-
}
386+
if (memcmp(disk_sb->csum, result, btrfs_super_csum_size(disk_sb)))
387+
return 1;
398388

399389
return 0;
400390
}
@@ -2611,6 +2601,7 @@ int open_ctree(struct super_block *sb,
26112601
u32 stripesize;
26122602
u64 generation;
26132603
u64 features;
2604+
u16 csum_type;
26142605
struct btrfs_key location;
26152606
struct buffer_head *bh;
26162607
struct btrfs_super_block *disk_super;
@@ -2820,11 +2811,10 @@ int open_ctree(struct super_block *sb,
28202811
* Verify the type first, if that or the the checksum value are
28212812
* corrupted, we'll find out
28222813
*/
2823-
if (!btrfs_supported_super_csum(btrfs_super_csum_type(
2824-
(struct btrfs_super_block *) bh->b_data))) {
2814+
csum_type = btrfs_super_csum_type((struct btrfs_super_block *)bh->b_data);
2815+
if (!btrfs_supported_super_csum(csum_type)) {
28252816
btrfs_err(fs_info, "unsupported checksum algorithm: %u",
2826-
btrfs_super_csum_type((struct btrfs_super_block *)
2827-
bh->b_data));
2817+
csum_type);
28282818
err = -EINVAL;
28292819
brelse(bh);
28302820
goto fail_alloc;

0 commit comments

Comments
 (0)