generations on headers
[btrfs-progs-unstable.git] / print-tree.c
blob5259b6e9fd318de37330f13cc12ae4c5948f21ef
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"
8 void btrfs_print_leaf(struct btrfs_root *root, struct btrfs_leaf *l)
10 int i;
11 u32 nr = btrfs_header_nritems(&l->header);
12 struct btrfs_item *item;
13 struct btrfs_extent_item *ei;
14 struct btrfs_root_item *ri;
15 struct btrfs_dir_item *di;
16 struct btrfs_inode_map_item *mi;
17 struct btrfs_inode_item *ii;
18 u32 type;
20 printf("leaf %Lu ptrs %d free space %d parent %Lu generation %Lu\n",
21 btrfs_header_blocknr(&l->header), nr,
22 btrfs_leaf_free_space(root, l),
23 btrfs_header_parentid(&l->header),
24 btrfs_header_generation(&l->header));
25 fflush(stdout);
26 for (i = 0 ; i < nr ; i++) {
27 item = l->items + i;
28 type = btrfs_disk_key_type(&item->key);
29 printf("\titem %d key (%Lu %u %Lu) itemoff %d itemsize %d\n",
31 btrfs_disk_key_objectid(&item->key),
32 btrfs_disk_key_flags(&item->key),
33 btrfs_disk_key_offset(&item->key),
34 btrfs_item_offset(item),
35 btrfs_item_size(item));
36 switch (type) {
37 case BTRFS_INODE_ITEM_KEY:
38 ii = btrfs_item_ptr(l, i, struct btrfs_inode_item);
39 printf("\t\tinode generation %Lu size %Lu mode %o\n",
40 btrfs_inode_generation(ii),
41 btrfs_inode_size(ii),
42 btrfs_inode_mode(ii));
43 break;
44 case BTRFS_DIR_ITEM_KEY:
45 di = btrfs_item_ptr(l, i, struct btrfs_dir_item);
46 printf("\t\tdir oid %Lu flags %u type %u\n",
47 btrfs_dir_objectid(di),
48 btrfs_dir_flags(di),
49 btrfs_dir_type(di));
50 printf("\t\tname %.*s\n",
51 btrfs_dir_name_len(di),(char *)(di + 1));
52 break;
53 case BTRFS_ROOT_ITEM_KEY:
54 ri = btrfs_item_ptr(l, i, struct btrfs_root_item);
55 printf("\t\troot data blocknr %Lu refs %u\n",
56 btrfs_root_blocknr(ri), btrfs_root_refs(ri));
57 break;
58 case BTRFS_EXTENT_ITEM_KEY:
59 ei = btrfs_item_ptr(l, i, struct btrfs_extent_item);
60 printf("\t\textent data refs %u owner %Lu\n",
61 btrfs_extent_refs(ei), btrfs_extent_owner(ei));
62 break;
63 case BTRFS_INODE_MAP_ITEM_KEY:
64 mi = btrfs_item_ptr(l, i, struct btrfs_inode_map_item);
65 printf("\t\tinode map key %Lu %u %Lu\n",
66 btrfs_disk_key_objectid(&mi->key),
67 btrfs_disk_key_flags(&mi->key),
68 btrfs_disk_key_offset(&mi->key));
69 break;
70 case BTRFS_STRING_ITEM_KEY:
71 printf("\t\titem data %.*s\n", btrfs_item_size(item),
72 btrfs_leaf_data(l) + btrfs_item_offset(item));
73 break;
75 fflush(stdout);
78 void btrfs_print_tree(struct btrfs_root *root, struct btrfs_buffer *t)
80 int i;
81 u32 nr;
82 struct btrfs_node *c;
84 if (!t)
85 return;
86 c = &t->node;
87 nr = btrfs_header_nritems(&c->header);
88 if (btrfs_is_leaf(c)) {
89 btrfs_print_leaf(root, (struct btrfs_leaf *)c);
90 return;
92 printf("node %Lu level %d ptrs %d free %u parent %Lu generation %Lu\n",
93 t->blocknr,
94 btrfs_header_level(&c->header), nr,
95 (u32)BTRFS_NODEPTRS_PER_BLOCK(root) - nr,
96 btrfs_header_parentid(&c->header),
97 btrfs_header_generation(&c->header));
98 fflush(stdout);
99 for (i = 0; i < nr; i++) {
100 printf("\tkey %d (%Lu %u %Lu) block %Lu\n",
102 c->ptrs[i].key.objectid,
103 c->ptrs[i].key.flags,
104 c->ptrs[i].key.offset,
105 btrfs_node_blockptr(c, i));
106 fflush(stdout);
108 for (i = 0; i < nr; i++) {
109 struct btrfs_buffer *next_buf = read_tree_block(root,
110 btrfs_node_blockptr(c, i));
111 struct btrfs_node *next = &next_buf->node;
112 if (btrfs_is_leaf(next) &&
113 btrfs_header_level(&c->header) != 1)
114 BUG();
115 if (btrfs_header_level(&next->header) !=
116 btrfs_header_level(&c->header) - 1)
117 BUG();
118 btrfs_print_tree(root, next_buf);
119 btrfs_block_release(root, next_buf);