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.

Signed-off-by: Johannes Thumshirn <jthumsh...@suse.de>
Reviewed-by: Nikolay Borisov <nbori...@suse.com>

---
Changes to v1:
- Check for disk_sb->csum instead of raw buffer (Nikolay)
---
 fs/btrfs/disk-io.c | 43 +++++++++++++++++--------------------------
 1 file changed, 17 insertions(+), 26 deletions(-)

diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
index b11091612074..a2b29b875ea4 100644
--- a/fs/btrfs/disk-io.c
+++ b/fs/btrfs/disk-io.c
@@ -375,30 +375,20 @@ 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);
-
-       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;
-               char result[sizeof(crc)];
+       u32 crc = ~(u32)0;
+       char result[BTRFS_CSUM_SIZE];
 
-               /*
-                * The super_block structure does not span the whole
-                * BTRFS_SUPER_INFO_SIZE range, we expect that the unused space
-                * is filled with zeros and is included in the checksum.
-                */
-               crc = btrfs_csum_data(raw_disk_sb + BTRFS_CSUM_SIZE,
-                               crc, BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
-               btrfs_csum_final(crc, result);
+       /*
+        * The super_block structure does not span the whole
+        * BTRFS_SUPER_INFO_SIZE range, we expect that the unused space
+        * is filled with zeros and is included in the checksum.
+        */
+       crc = btrfs_csum_data(raw_disk_sb + BTRFS_CSUM_SIZE,
+                             crc, BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
+       btrfs_csum_final(crc, result);
 
-               if (memcmp(raw_disk_sb, result, sizeof(result)))
-                       return 1;
-       }
+       if (memcmp(disk_sb->csum, result, btrfs_super_csum_size(disk_sb)))
+               return 1;
 
        return 0;
 }
@@ -2616,6 +2606,7 @@ int open_ctree(struct super_block *sb,
        u32 stripesize;
        u64 generation;
        u64 features;
+       u16 csum_type;
        struct btrfs_key location;
        struct buffer_head *bh;
        struct btrfs_super_block *disk_super;
@@ -2821,11 +2812,11 @@ int open_ctree(struct super_block *sb,
                goto fail_alloc;
        }
 
-       if (!btrfs_supported_super_csum(btrfs_super_csum_type(
-                                (struct btrfs_super_block *) bh->b_data))) {
+       csum_type = btrfs_super_csum_type((struct btrfs_super_block *)
+                                         bh->b_data);
+       if (!btrfs_supported_super_csum(csum_type)) {
                btrfs_err(fs_info, "unsupported checksum algorithm: %u",
-                         btrfs_super_csum_type((struct btrfs_super_block *)
-                                               bh->b_data));
+                         csum_type);
                err = -EINVAL;
                brelse(bh);
                goto fail_alloc;
-- 
2.16.4

Reply via email to