btrfs-progs: print-tree: print the checksum of header without tailing zeros

For the default CRC32C checksum, print-tree now prints tons of
unnecessary padding zeros:

  btrfs-progs v5.17
  chunk tree
  leaf 22036480 items 7 free space 15430 generation 6 owner CHUNK_TREE
  leaf 22036480 flags 0x1(WRITTEN) backref revision 1
  checksum stored 0ac1b9fa00000000000000000000000000000000000000000000000000000000
  checksum calced 0ac1b9fa00000000000000000000000000000000000000000000000000000000
  fs uuid 3d95b7e3-3ab6-4927-af56-c58aa634342e

This is caused by commit 1bb6fb896d ("btrfs-progs: btrfstune:
experimental, new option to switch csums"), and it looks like most
distros just enable EXPERIMENTAL features by default.
(Which is a good thing to provide much better coverage).

So here we just limit the csum print to the utilized csum size.

Now the output looks like:

  btrfs-progs v5.17
  chunk tree
  leaf 22036480 items 4 free space 15781 generation 6 owner CHUNK_TREE
  leaf 22036480 flags 0x1(WRITTEN) backref revision 1
  checksum stored 676b812f
  checksum calced 676b812f
  fs uuid d11f8799-b6dc-415d-b1ed-cebe6da5f0b7

Fixes: 1bb6fb896d ("btrfs-progs: btrfstune: experimental, new option to switch csums")
Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
Qu Wenruo 2022-05-10 14:03:18 +08:00 committed by David Sterba
parent e278e0755f
commit 50a5dfde6d

View File

@ -1224,7 +1224,7 @@ static void print_header_info(struct extent_buffer *eb, unsigned int mode)
u8 backref_rev;
char csum_str[2 * BTRFS_CSUM_SIZE + strlen(" csum 0x") + 1];
int i;
int csum_size;
int csum_size = fs_info->csum_size;
flags = btrfs_header_flags(eb) & ~BTRFS_BACKREF_REV_MASK;
backref_rev = btrfs_header_flags(eb) >> BTRFS_BACKREF_REV_SHIFT;
@ -1249,7 +1249,6 @@ static void print_header_info(struct extent_buffer *eb, unsigned int mode)
char *tmp = csum_str;
u8 *csum = (u8 *)(eb->data + offsetof(struct btrfs_header, csum));
csum_size = fs_info->csum_size;
strcpy(csum_str, " csum 0x");
tmp = csum_str + strlen(csum_str);
for (i = 0; i < csum_size; i++) {
@ -1268,7 +1267,7 @@ static void print_header_info(struct extent_buffer *eb, unsigned int mode)
#ifdef EXPERIMENTAL
printf("checksum stored ");
for (i = 0; i < BTRFS_CSUM_SIZE; i++)
for (i = 0; i < csum_size; i++)
printf("%02hhx", (int)(eb->data[i]));
printf("\n");
memset(csum, 0, sizeof(csum));
@ -1276,7 +1275,7 @@ static void print_header_info(struct extent_buffer *eb, unsigned int mode)
(u8 *)eb->data + BTRFS_CSUM_SIZE,
csum, fs_info->nodesize - BTRFS_CSUM_SIZE);
printf("checksum calced ");
for (i = 0; i < BTRFS_CSUM_SIZE; i++)
for (i = 0; i < csum_size; i++)
printf("%02hhx", (int)(csum[i]));
printf("\n");
#endif