btrfs: add common checksum type validation
authorJohannes Thumshirn <jthumshirn@suse.de>
Mon, 3 Jun 2019 14:58:53 +0000 (16:58 +0200)
committerDavid Sterba <dsterba@suse.com>
Mon, 1 Jul 2019 11:35:01 +0000 (13:35 +0200)
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 <nborisov@suse.com>
Signed-off-by: Johannes Thumshirn <jthumshirn@suse.de>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/disk-io.c

index fe14b971440f18559666fca359a37da2fd964e0d..3f3bb70ca437aef354de6b6af39e585ccc326dd3 100644 (file)
@@ -352,6 +352,16 @@ out:
        return ret;
 }
 
+static bool btrfs_supported_super_csum(u16 csum_type)
+{
+       switch (csum_type) {
+       case BTRFS_CSUM_TYPE_CRC32:
+               return true;
+       default:
+               return false;
+       }
+}
+
 /*
  * Return 0 if the superblock checksum type matches the checksum value of that
  * algorithm. Pass the raw disk superblock data.
@@ -362,7 +372,12 @@ static int btrfs_check_super_csum(struct btrfs_fs_info *fs_info,
        struct btrfs_super_block *disk_sb =
                (struct btrfs_super_block *)raw_disk_sb;
        u16 csum_type = btrfs_super_csum_type(disk_sb);
-       int ret = 0;
+
+       if (!btrfs_supported_super_csum(csum_type)) {
+               btrfs_err(fs_info, "unsupported checksum algorithm %u",
+                         csum_type);
+               return 1;
+       }
 
        if (csum_type == BTRFS_CSUM_TYPE_CRC32) {
                u32 crc = ~(u32)0;
@@ -378,16 +393,10 @@ static int btrfs_check_super_csum(struct btrfs_fs_info *fs_info,
                btrfs_csum_final(crc, result);
 
                if (memcmp(raw_disk_sb, result, sizeof(result)))
-                       ret = 1;
+                       return 1;
        }
 
-       if (csum_type >= ARRAY_SIZE(btrfs_csum_sizes)) {
-               btrfs_err(fs_info, "unsupported checksum algorithm %u",
-                               csum_type);
-               ret = 1;
-       }
-
-       return ret;
+       return 0;
 }
 
 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,
        ret = validate_super(fs_info, sb, -1);
        if (ret < 0)
                goto out;
-       if (btrfs_super_csum_type(sb) != BTRFS_CSUM_TYPE_CRC32) {
+       if (!btrfs_supported_super_csum(btrfs_super_csum_type(sb))) {
                ret = -EUCLEAN;
                btrfs_err(fs_info, "invalid csum type, has %u want %u",
                          btrfs_super_csum_type(sb), BTRFS_CSUM_TYPE_CRC32);