btrfs-progs: dump-super: detect invalid checksum type
authorDavid Sterba <dsterba@suse.com>
Thu, 1 Sep 2016 17:54:04 +0000 (19:54 +0200)
committerDavid Sterba <dsterba@suse.com>
Mon, 5 Sep 2016 08:05:38 +0000 (10:05 +0200)
The helper btrfs_super_csum_size unconditionally bugs on a bogus value,
the dump-super is supposed to dump even corrupted superblocks so this
must not happen here. Group the checsum values in the output and be more
robust agains garbage values.

Signed-off-by: David Sterba <dsterba@suse.com>
cmds-inspect-dump-super.c

index 38e99ec..aab5075 100644 (file)
@@ -287,11 +287,29 @@ static void dump_superblock(struct btrfs_super_block *sb, int full)
        int i;
        char *s, buf[BTRFS_UUID_UNPARSED_SIZE];
        u8 *p;
+       u32 csum_size;
+       u16 csum_type;
+
+       csum_type = btrfs_super_csum_type(sb);
+       csum_size = BTRFS_CSUM_SIZE;
+       printf("csum_type\t\t%hu (", csum_type);
+       if (csum_type >= ARRAY_SIZE(btrfs_csum_sizes)) {
+               printf("INVALID");
+       } else {
+               if (csum_type == BTRFS_CSUM_TYPE_CRC32) {
+                       printf("crc32c");
+                       csum_size = btrfs_csum_sizes[csum_type];
+               } else {
+                       printf("unknown");
+               }
+       }
+       printf(")\n");
+       printf("csum_size\t\t%llu\n", (unsigned long long)csum_size);
 
        printf("csum\t\t\t0x");
-       for (i = 0, p = sb->csum; i < btrfs_super_csum_size(sb); i++)
+       for (i = 0, p = sb->csum; i < csum_size; i++)
                printf("%02x", p[i]);
-       if (check_csum_sblock(sb, btrfs_super_csum_size(sb)))
+       if (check_csum_sblock(sb, csum_size))
                printf(" [match]");
        else
                printf(" [DON'T MATCH]");
@@ -363,10 +381,6 @@ static void dump_superblock(struct btrfs_super_block *sb, int full)
        printf("incompat_flags\t\t0x%llx\n",
               (unsigned long long)btrfs_super_incompat_flags(sb));
        print_readable_incompat_flag(btrfs_super_incompat_flags(sb));
-       printf("csum_type\t\t%llu\n",
-              (unsigned long long)btrfs_super_csum_type(sb));
-       printf("csum_size\t\t%llu\n",
-              (unsigned long long)btrfs_super_csum_size(sb));
        printf("cache_generation\t%llu\n",
               (unsigned long long)btrfs_super_cache_generation(sb));
        printf("uuid_tree_generation\t%llu\n",