use node list for roots
[btrfs-progs-unstable.git] / print-tree.c
blobb7018eaef3c7d60a37e2c41168c465304069d6bb
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 static int print_dir_item(struct btrfs_item *item,
9 struct btrfs_dir_item *di)
11 u32 total;
12 u32 cur = 0;
13 u32 len;
14 total = btrfs_item_size(item);
15 while(cur < total) {
16 printf("\t\tdir index %Lu flags %u type %u\n",
17 btrfs_disk_key_objectid(&di->location),
18 btrfs_dir_flags(di),
19 btrfs_dir_type(di));
20 printf("\t\tname %.*s\n",
21 btrfs_dir_name_len(di),(char *)(di + 1));
22 len = sizeof(*di) + btrfs_dir_name_len(di);
23 di = (struct btrfs_dir_item *)((char *)di + len);
24 cur += len;
26 return 0;
28 void btrfs_print_leaf(struct btrfs_root *root, struct btrfs_leaf *l)
30 int i;
31 u32 nr = btrfs_header_nritems(&l->header);
32 struct btrfs_item *item;
33 struct btrfs_extent_item *ei;
34 struct btrfs_root_item *ri;
35 struct btrfs_dir_item *di;
36 struct btrfs_device_item *devi;
37 struct btrfs_inode_item *ii;
38 struct btrfs_file_extent_item *fi;
39 struct btrfs_csum_item *ci;
40 char *p;
41 u32 type;
43 printf("leaf %Lu ptrs %d free space %d generation %Lu owner %Lu\n",
44 btrfs_header_blocknr(&l->header), nr,
45 btrfs_leaf_free_space(root, l),
46 btrfs_header_generation(&l->header),
47 btrfs_header_owner(&l->header));
48 fflush(stdout);
49 for (i = 0 ; i < nr ; i++) {
50 item = l->items + i;
51 type = btrfs_disk_key_type(&item->key);
52 printf("\titem %d key (%Lu %x %Lu) itemoff %d itemsize %d\n",
54 btrfs_disk_key_objectid(&item->key),
55 btrfs_disk_key_flags(&item->key),
56 btrfs_disk_key_offset(&item->key),
57 btrfs_item_offset(item),
58 btrfs_item_size(item));
59 switch (type) {
60 case BTRFS_INODE_ITEM_KEY:
61 ii = btrfs_item_ptr(l, i, struct btrfs_inode_item);
62 printf("\t\tinode generation %Lu size %Lu mode %o\n",
63 btrfs_inode_generation(ii),
64 btrfs_inode_size(ii),
65 btrfs_inode_mode(ii));
66 break;
67 case BTRFS_INLINE_DATA_KEY:
68 p = btrfs_item_ptr(l, i, char);
69 printf("\t\tinline data %.*s\n", 10, p);
70 break;
71 case BTRFS_DIR_ITEM_KEY:
72 di = btrfs_item_ptr(l, i, struct btrfs_dir_item);
73 print_dir_item(l->items + i, di);
74 break;
75 case BTRFS_DIR_INDEX_KEY:
76 di = btrfs_item_ptr(l, i, struct btrfs_dir_item);
77 print_dir_item(l->items + i, di);
78 break;
79 case BTRFS_ROOT_ITEM_KEY:
80 ri = btrfs_item_ptr(l, i, struct btrfs_root_item);
81 printf("\t\troot data blocknr %Lu dirid %Lu refs %u\n",
82 btrfs_root_blocknr(ri),
83 btrfs_root_dirid(ri),
84 btrfs_root_refs(ri));
85 break;
86 case BTRFS_EXTENT_ITEM_KEY:
87 ei = btrfs_item_ptr(l, i, struct btrfs_extent_item);
88 printf("\t\textent data refs %u owner %Lu\n",
89 btrfs_extent_refs(ei),
90 btrfs_extent_owner(ei));
91 break;
92 case BTRFS_CSUM_ITEM_KEY:
93 ci = btrfs_item_ptr(l, i,
94 struct btrfs_csum_item);
95 printf("\t\tcsum item\n");
96 break;
97 case BTRFS_EXTENT_DATA_KEY:
98 fi = btrfs_item_ptr(l, i,
99 struct btrfs_file_extent_item);
100 if (btrfs_file_extent_type(fi) ==
101 BTRFS_FILE_EXTENT_INLINE) {
102 printf("\t\tinline extent data size %u\n",
103 btrfs_file_extent_inline_len(l->items + i));
104 break;
106 printf("\t\textent data disk block %Lu nr %Lu\n",
107 btrfs_file_extent_disk_blocknr(fi),
108 btrfs_file_extent_disk_num_blocks(fi));
109 printf("\t\textent data offset %Lu nr %Lu\n",
110 btrfs_file_extent_offset(fi),
111 btrfs_file_extent_num_blocks(fi));
112 break;
113 case BTRFS_DEV_ITEM_KEY:
114 devi = btrfs_item_ptr(l, i, struct btrfs_device_item);
115 printf("\t\tdev id %Lu namelen %u name %.*s\n",
116 btrfs_device_id(devi),
117 btrfs_device_pathlen(devi),
118 btrfs_device_pathlen(devi),
119 (char *)(devi + 1));
120 break;
121 case BTRFS_STRING_ITEM_KEY:
122 printf("\t\titem data %.*s\n", btrfs_item_size(item),
123 btrfs_leaf_data(l) + btrfs_item_offset(item));
124 break;
126 fflush(stdout);
129 void btrfs_print_tree(struct btrfs_root *root, struct btrfs_buffer *t)
131 int i;
132 u32 nr;
133 struct btrfs_node *c;
135 if (!t)
136 return;
137 c = &t->node;
138 nr = btrfs_header_nritems(&c->header);
139 if (btrfs_is_leaf(c)) {
140 btrfs_print_leaf(root, (struct btrfs_leaf *)c);
141 return;
143 printf("node %Lu level %d ptrs %d free %u generation %Lu owner %Lu\n",
144 t->blocknr,
145 btrfs_header_level(&c->header), nr,
146 (u32)BTRFS_NODEPTRS_PER_BLOCK(root) - nr,
147 btrfs_header_generation(&c->header),
148 btrfs_header_owner(&c->header));
149 fflush(stdout);
150 for (i = 0; i < nr; i++) {
151 printf("\tkey %d (%Lu %x %Lu) block %Lu\n",
153 c->ptrs[i].key.objectid,
154 c->ptrs[i].key.flags,
155 c->ptrs[i].key.offset,
156 btrfs_node_blockptr(c, i));
157 fflush(stdout);
159 for (i = 0; i < nr; i++) {
160 struct btrfs_buffer *next_buf = read_tree_block(root,
161 btrfs_node_blockptr(c, i));
162 struct btrfs_node *next = &next_buf->node;
163 if (btrfs_is_leaf(next) &&
164 btrfs_header_level(&c->header) != 1)
165 BUG();
166 if (btrfs_header_level(&next->header) !=
167 btrfs_header_level(&c->header) - 1)
168 BUG();
169 btrfs_print_tree(root, next_buf);
170 btrfs_block_release(root, next_buf);