Skip to content

Commit e7e16f4

Browse files
Johannes Thumshirnkdave
authored andcommitted
btrfs: add common checksum type validation
Currently btrfs is only supporting CRC32C as checksumming algorithm. As this is about to change provide a function to validate the checksum type in the superblock against all possible algorithms. This makes adding new algorithms easier as there are fewer places to adjust when adding new algorithms. 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 7ebc7e5 commit e7e16f4

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

fs/btrfs/disk-io.c

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,16 @@ static int verify_parent_transid(struct extent_io_tree *io_tree,
352352
return ret;
353353
}
354354

355+
static bool btrfs_supported_super_csum(u16 csum_type)
356+
{
357+
switch (csum_type) {
358+
case BTRFS_CSUM_TYPE_CRC32:
359+
return true;
360+
default:
361+
return false;
362+
}
363+
}
364+
355365
/*
356366
* Return 0 if the superblock checksum type matches the checksum value of that
357367
* algorithm. Pass the raw disk superblock data.
@@ -362,7 +372,12 @@ static int btrfs_check_super_csum(struct btrfs_fs_info *fs_info,
362372
struct btrfs_super_block *disk_sb =
363373
(struct btrfs_super_block *)raw_disk_sb;
364374
u16 csum_type = btrfs_super_csum_type(disk_sb);
365-
int ret = 0;
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+
}
366381

367382
if (csum_type == BTRFS_CSUM_TYPE_CRC32) {
368383
u32 crc = ~(u32)0;
@@ -378,16 +393,10 @@ static int btrfs_check_super_csum(struct btrfs_fs_info *fs_info,
378393
btrfs_csum_final(crc, result);
379394

380395
if (memcmp(raw_disk_sb, result, sizeof(result)))
381-
ret = 1;
396+
return 1;
382397
}
383398

384-
if (csum_type >= ARRAY_SIZE(btrfs_csum_sizes)) {
385-
btrfs_err(fs_info, "unsupported checksum algorithm %u",
386-
csum_type);
387-
ret = 1;
388-
}
389-
390-
return ret;
399+
return 0;
391400
}
392401

393402
int btrfs_verify_level_key(struct extent_buffer *eb, int level,
@@ -2572,7 +2581,7 @@ static int btrfs_validate_write_super(struct btrfs_fs_info *fs_info,
25722581
ret = validate_super(fs_info, sb, -1);
25732582
if (ret < 0)
25742583
goto out;
2575-
if (btrfs_super_csum_type(sb) != BTRFS_CSUM_TYPE_CRC32) {
2584+
if (!btrfs_supported_super_csum(btrfs_super_csum_type(sb))) {
25762585
ret = -EUCLEAN;
25772586
btrfs_err(fs_info, "invalid csum type, has %u want %u",
25782587
btrfs_super_csum_type(sb), BTRFS_CSUM_TYPE_CRC32);

0 commit comments

Comments
 (0)