endian fixes, makefile fixes
[btrfs-progs-unstable.git] / print-tree.c
blob35399cb3033e932fa7a0851277bac8fd97a77cd5
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 struct btrfs_block_group_item *bi;
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 block group %Lu mode %o\n",
63 btrfs_inode_generation(ii),
64 btrfs_inode_size(ii),
65 btrfs_inode_block_group(ii),
66 btrfs_inode_mode(ii));
67 break;
68 case BTRFS_DIR_ITEM_KEY:
69 di = btrfs_item_ptr(l, i, struct btrfs_dir_item);
70 print_dir_item(l->items + i, di);
71 break;
72 case BTRFS_DIR_INDEX_KEY:
73 di = btrfs_item_ptr(l, i, struct btrfs_dir_item);
74 print_dir_item(l->items + i, di);
75 break;
76 case BTRFS_ROOT_ITEM_KEY:
77 ri = btrfs_item_ptr(l, i, struct btrfs_root_item);
78 printf("\t\troot data blocknr %Lu dirid %Lu refs %u\n",
79 btrfs_root_blocknr(ri),
80 btrfs_root_dirid(ri),
81 btrfs_root_refs(ri));
82 break;
83 case BTRFS_EXTENT_ITEM_KEY:
84 ei = btrfs_item_ptr(l, i, struct btrfs_extent_item);
85 printf("\t\textent data refs %u owner %Lu\n",
86 btrfs_extent_refs(ei),
87 btrfs_extent_owner(ei));
88 break;
89 case BTRFS_CSUM_ITEM_KEY:
90 ci = btrfs_item_ptr(l, i,
91 struct btrfs_csum_item);
92 printf("\t\tcsum item\n");
93 break;
94 case BTRFS_EXTENT_DATA_KEY:
95 fi = btrfs_item_ptr(l, i,
96 struct btrfs_file_extent_item);
97 if (btrfs_file_extent_type(fi) ==
98 BTRFS_FILE_EXTENT_INLINE) {
99 printf("\t\tinline extent data size %u\n",
100 btrfs_file_extent_inline_len(l->items + i));
101 break;
103 printf("\t\textent data disk block %Lu nr %Lu\n",
104 btrfs_file_extent_disk_blocknr(fi),
105 btrfs_file_extent_disk_num_blocks(fi));
106 printf("\t\textent data offset %Lu nr %Lu\n",
107 btrfs_file_extent_offset(fi),
108 btrfs_file_extent_num_blocks(fi));
109 break;
110 case BTRFS_BLOCK_GROUP_ITEM_KEY:
111 bi = btrfs_item_ptr(l, i,
112 struct btrfs_block_group_item);
113 printf("\t\tblock group used %Lu flags %x\n",
114 btrfs_block_group_used(bi), bi->flags);
115 break;
116 case BTRFS_DEV_ITEM_KEY:
117 devi = btrfs_item_ptr(l, i, struct btrfs_device_item);
118 printf("\t\tdev id %Lu namelen %u name %.*s\n",
119 btrfs_device_id(devi),
120 btrfs_device_pathlen(devi),
121 btrfs_device_pathlen(devi),
122 (char *)(devi + 1));
123 break;
124 case BTRFS_STRING_ITEM_KEY:
125 printf("\t\titem data %.*s\n", btrfs_item_size(item),
126 btrfs_leaf_data(l) + btrfs_item_offset(item));
127 break;
129 fflush(stdout);
132 void btrfs_print_tree(struct btrfs_root *root, struct btrfs_buffer *t)
134 int i;
135 u32 nr;
136 struct btrfs_node *c;
138 if (!t)
139 return;
140 c = &t->node;
141 nr = btrfs_header_nritems(&c->header);
142 if (btrfs_is_leaf(c)) {
143 btrfs_print_leaf(root, (struct btrfs_leaf *)c);
144 return;
146 printf("node %Lu level %d ptrs %d free %u generation %Lu owner %Lu\n",
147 t->blocknr,
148 btrfs_header_level(&c->header), nr,
149 (u32)BTRFS_NODEPTRS_PER_BLOCK(root) - nr,
150 btrfs_header_generation(&c->header),
151 btrfs_header_owner(&c->header));
152 fflush(stdout);
153 for (i = 0; i < nr; i++) {
154 printf("\tkey %d (%Lu %x %Lu) block %Lu\n",
156 c->ptrs[i].key.objectid,
157 c->ptrs[i].key.flags,
158 c->ptrs[i].key.offset,
159 btrfs_node_blockptr(c, i));
160 fflush(stdout);
162 for (i = 0; i < nr; i++) {
163 struct btrfs_buffer *next_buf = read_tree_block(root,
164 btrfs_node_blockptr(c, i));
165 struct btrfs_node *next = &next_buf->node;
166 if (btrfs_is_leaf(next) &&
167 btrfs_header_level(&c->header) != 1)
168 BUG();
169 if (btrfs_header_level(&next->header) !=
170 btrfs_header_level(&c->header) - 1)
171 BUG();
172 btrfs_print_tree(root, next_buf);
173 btrfs_block_release(root, next_buf);