xref: /openbmc/linux/fs/btrfs/print-tree.c (revision 41903fe6)
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "kerncompat.h"
4 #include "radix-tree.h"
5 #include "ctree.h"
6 #include "disk-io.h"
7 
8 void print_leaf(struct leaf *l)
9 {
10 	int i;
11 	int nr = l->header.nritems;
12 	struct item *item;
13 	struct extent_item *ei;
14 	printf("leaf %Lu total ptrs %d free space %d\n", l->header.blocknr, nr,
15 	       leaf_free_space(l));
16 	fflush(stdout);
17 	for (i = 0 ; i < nr ; i++) {
18 		item = l->items + i;
19 		printf("\titem %d key (%Lu %u %Lu) itemoff %d itemsize %d\n",
20 			i,
21 			item->key.objectid, item->key.flags, item->key.offset,
22 			item->offset, item->size);
23 		fflush(stdout);
24 		printf("\t\titem data %.*s\n", item->size, l->data+item->offset);
25 		ei = (struct extent_item *)(l->data + item->offset);
26 		printf("\t\textent data %u %Lu\n", ei->refs, ei->owner);
27 		fflush(stdout);
28 	}
29 }
30 void print_tree(struct ctree_root *root, struct tree_buffer *t)
31 {
32 	int i;
33 	int nr;
34 	struct node *c;
35 
36 	if (!t)
37 		return;
38 	c = &t->node;
39 	nr = c->header.nritems;
40 	if (c->header.blocknr != t->blocknr)
41 		BUG();
42 	if (is_leaf(c->header.flags)) {
43 		print_leaf((struct leaf *)c);
44 		return;
45 	}
46 	printf("node %Lu level %d total ptrs %d free spc %u\n", t->blocknr,
47 	        node_level(c->header.flags), c->header.nritems,
48 		NODEPTRS_PER_BLOCK - c->header.nritems);
49 	fflush(stdout);
50 	for (i = 0; i < nr; i++) {
51 		printf("\tkey %d (%Lu %u %Lu) block %Lu\n",
52 		       i,
53 		       c->keys[i].objectid, c->keys[i].flags, c->keys[i].offset,
54 		       c->blockptrs[i]);
55 		fflush(stdout);
56 	}
57 	for (i = 0; i < nr; i++) {
58 		struct tree_buffer *next_buf = read_tree_block(root,
59 							    c->blockptrs[i]);
60 		struct node *next = &next_buf->node;
61 		if (is_leaf(next->header.flags) &&
62 		    node_level(c->header.flags) != 1)
63 			BUG();
64 		if (node_level(next->header.flags) !=
65 			node_level(c->header.flags) - 1)
66 			BUG();
67 		print_tree(root, next_buf);
68 		tree_block_release(root, next_buf);
69 	}
70 
71 }
72 
73