On 10.05.19 г. 14:15 ч., Johannes Thumshirn wrote:
> 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.
>
> Signed-off-by: Johannes Thumshirn <jthumshirn@xxxxxxx>
Reviewed-by: Nikolay Borisov <nborisov@xxxxxxxx>
> ---
> fs/btrfs/disk-io.c | 24 +++++++++++++++++-------
> 1 file changed, 17 insertions(+), 7 deletions(-)
>
> diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
> index 663efce22d98..ab13282d91d2 100644
> --- a/fs/btrfs/disk-io.c
> +++ b/fs/btrfs/disk-io.c
> @@ -356,6 +356,16 @@ static int verify_parent_transid(struct extent_io_tree *io_tree,
> return ret;
> }
>
> +static bool btrfs_supported_super_csum(struct btrfs_super_block *sb)
> +{
> + switch (btrfs_super_csum_type(sb)) {
> + 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.
> @@ -368,6 +378,12 @@ static int btrfs_check_super_csum(struct btrfs_fs_info *fs_info,
> u16 csum_type = btrfs_super_csum_type(disk_sb);
> int ret = 0;
>
> + if (!btrfs_supported_super_csum(disk_sb)) {
> + btrfs_err(fs_info, "unsupported checksum algorithm %u",
> + csum_type);
> + ret = 1;
> + }
> +
> if (csum_type == BTRFS_CSUM_TYPE_CRC32) {
> u32 crc = ~(u32)0;
> char result[sizeof(crc)];
> @@ -385,12 +401,6 @@ static int btrfs_check_super_csum(struct btrfs_fs_info *fs_info,
> ret = 1;
> }
>
> - if (csum_type >= ARRAY_SIZE(btrfs_csum_sizes)) {
> - btrfs_err(fs_info, "unsupported checksum algorithm %u",
> - csum_type);
> - ret = 1;
> - }
> -
> return ret;
> }
>
> @@ -2577,7 +2587,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(sb)) {
> ret = -EUCLEAN;
> btrfs_err(fs_info, "invalid csum type, has %u want %u",
> btrfs_super_csum_type(sb), BTRFS_CSUM_TYPE_CRC32);
>