Skip to content

Commit 9f2121d

Browse files
adam900710kdave
authored andcommitted
btrfs-progs: print-tree: enhance btrfs_print_leaf() to handle NULL fs_info
For mkfs and convert, we need to create a temporary fs without an fs_info. This makes debugging much harder that we cannot use btrfs_print_leaf() to print the temporary tree blocks. There are only two things causing problems for btrfs_print_leaf() if eb->fs_info is NULL: - print_header_info() Which needs to grab the checksum type from eb->fs_info. This can be avoided by completely skipping checksum output if eb->fs_info is NULL. - btrfs_leaf_free_space() Which have two BUG_ON()s checking eb->fs_info, and finally calling BTRFS_LEAF_DATA_SIZE(). Which can be avoided by removing the two BUG_ON()s, and use __BTRFS_LEAF_DATA_SIZE(eb->len) to grab the same leaf data size. Thankfully all call sites inside mkfs and convert are setting eb->len to nodesize correctly. - __btrfs_print_leaf() Which calls BTRFS_LEAF_DATA_SIZE(eb->fs_info). Can be avoided by the same method above. With those changes, we can call btrfs_print_leaf() inside a debugger for temporary fses created by mkfs and convert. Signed-off-by: Qu Wenruo <wqu@suse.com> Signed-off-by: David Sterba <dsterba@suse.com>
1 parent 7ce22a2 commit 9f2121d

File tree

2 files changed

+19
-20
lines changed

2 files changed

+19
-20
lines changed

kernel-shared/ctree.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1887,9 +1887,7 @@ int btrfs_leaf_free_space(const struct extent_buffer *leaf)
18871887
u32 leaf_data_size;
18881888
int ret;
18891889

1890-
BUG_ON(!leaf->fs_info);
1891-
BUG_ON(leaf->fs_info->nodesize != leaf->len);
1892-
leaf_data_size = BTRFS_LEAF_DATA_SIZE(leaf->fs_info);
1890+
leaf_data_size = __BTRFS_LEAF_DATA_SIZE(leaf->len);
18931891
ret = leaf_data_size - leaf_space_used(leaf, 0 ,nritems);
18941892
if (ret < 0) {
18951893
printk("leaf free space ret %d, leaf data size %u, used %d nritems %d\n",

kernel-shared/print-tree.c

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,8 +1344,7 @@ static void print_header_info(struct extent_buffer *eb, unsigned int mode)
13441344
u32 nr;
13451345
u8 backref_rev;
13461346
char csum_str[2 * BTRFS_CSUM_SIZE + 8 /* strlen(" csum 0x") */ + 1];
1347-
int i;
1348-
int csum_size = fs_info->csum_size;
1347+
const int csum_size = (fs_info ? fs_info->csum_size : 0);
13491348

13501349
flags = btrfs_header_flags(eb) & ~BTRFS_BACKREF_REV_MASK;
13511350
backref_rev = btrfs_header_flags(eb) >> BTRFS_BACKREF_REV_SHIFT;
@@ -1372,7 +1371,7 @@ static void print_header_info(struct extent_buffer *eb, unsigned int mode)
13721371

13731372
strcpy(csum_str, " csum 0x");
13741373
tmp = csum_str + strlen(csum_str);
1375-
for (i = 0; i < csum_size; i++) {
1374+
for (int i = 0; i < csum_size; i++) {
13761375
sprintf(tmp, "%02x", tree_csum[i]);
13771376
tmp++;
13781377
tmp++;
@@ -1387,18 +1386,20 @@ static void print_header_info(struct extent_buffer *eb, unsigned int mode)
13871386
csum_str);
13881387

13891388
#if EXPERIMENTAL
1390-
printf("checksum stored ");
1391-
for (i = 0; i < csum_size; i++)
1392-
printf("%02hhx", (int)(eb->data[i]));
1393-
printf("\n");
1394-
memset(csum, 0, sizeof(csum));
1395-
btrfs_csum_data(btrfs_super_csum_type(fs_info->super_copy),
1396-
(u8 *)eb->data + BTRFS_CSUM_SIZE,
1397-
csum, fs_info->nodesize - BTRFS_CSUM_SIZE);
1398-
printf("checksum calced ");
1399-
for (i = 0; i < csum_size; i++)
1400-
printf("%02hhx", (int)(csum[i]));
1401-
printf("\n");
1389+
if (fs_info) {
1390+
printf("checksum stored ");
1391+
for (int i = 0; i < csum_size; i++)
1392+
printf("%02hhx", (int)(eb->data[i]));
1393+
printf("\n");
1394+
memset(csum, 0, sizeof(csum));
1395+
btrfs_csum_data(btrfs_super_csum_type(fs_info->super_copy),
1396+
(u8 *)eb->data + BTRFS_CSUM_SIZE,
1397+
csum, fs_info->nodesize - BTRFS_CSUM_SIZE);
1398+
printf("checksum calced ");
1399+
for (int i = 0; i < csum_size; i++)
1400+
printf("%02hhx", (int)(csum[i]));
1401+
printf("\n");
1402+
}
14021403
#endif
14031404

14041405
print_uuids(eb);
@@ -1478,10 +1479,10 @@ static void print_dev_replace_item(struct extent_buffer *eb, struct btrfs_dev_re
14781479
void __btrfs_print_leaf(struct extent_buffer *eb, unsigned int mode)
14791480
{
14801481
struct btrfs_disk_key disk_key;
1481-
u32 leaf_data_size = BTRFS_LEAF_DATA_SIZE(eb->fs_info);
1482+
u32 leaf_data_size = __BTRFS_LEAF_DATA_SIZE(eb->len);
14821483
u32 i;
14831484
u32 nr;
1484-
const bool print_csum_items = (mode & BTRFS_PRINT_TREE_CSUM_ITEMS);
1485+
const bool print_csum_items = (mode & BTRFS_PRINT_TREE_CSUM_ITEMS) && eb->fs_info;
14851486

14861487
print_header_info(eb, mode);
14871488
nr = btrfs_header_nritems(eb);

0 commit comments

Comments
 (0)