Recow all roots at the end of mkfs
[btrfs-progs-unstable.git] / print-tree.c
blob6a4d0f11b295ef5504dd96d4b7850bfdf210177d
1 /*
2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include "kerncompat.h"
22 #include "radix-tree.h"
23 #include "ctree.h"
24 #include "disk-io.h"
26 static int print_dir_item(struct extent_buffer *eb, struct btrfs_item *item,
27 struct btrfs_dir_item *di)
29 u32 total;
30 u32 cur = 0;
31 u32 len;
32 u32 name_len;
33 u32 data_len;
34 char namebuf[BTRFS_NAME_LEN];
35 struct btrfs_disk_key location;
37 total = btrfs_item_size(eb, item);
38 while(cur < total) {
39 btrfs_dir_item_key(eb, di, &location);
40 printf("\t\tdir index %llu type %u\n",
41 (unsigned long long)btrfs_disk_key_objectid(&location),
42 btrfs_dir_type(eb, di));
43 name_len = btrfs_dir_name_len(eb, di);
44 data_len = btrfs_dir_data_len(eb, di);
45 len = (name_len <= sizeof(namebuf))? name_len: sizeof(namebuf);
46 read_extent_buffer(eb, namebuf, (unsigned long)(di + 1), len);
47 printf("\t\tnamelen %u datalen %u name: %.*s\n",
48 name_len, data_len, len, namebuf);
49 len = sizeof(*di) + name_len + data_len;
50 di = (struct btrfs_dir_item *)((char *)di + len);
51 cur += len;
53 return 0;
56 static int print_inode_ref_item(struct extent_buffer *eb, struct btrfs_item *item,
57 struct btrfs_inode_ref *ref)
59 u32 total;
60 u32 cur = 0;
61 u32 len;
62 u32 name_len;
63 char namebuf[BTRFS_NAME_LEN];
64 total = btrfs_item_size(eb, item);
65 while(cur < total) {
66 name_len = btrfs_inode_ref_name_len(eb, ref);
67 len = (name_len <= sizeof(namebuf))? name_len: sizeof(namebuf);
68 read_extent_buffer(eb, namebuf, (unsigned long)(ref + 1), len);
69 printf("\t\tinode ref namelen %u name: %.*s\n",
70 name_len, len, namebuf);
71 len = sizeof(*ref) + name_len;
72 ref = (struct btrfs_inode_ref *)((char *)ref + len);
73 cur += len;
75 return 0;
78 static void print_chunk(struct extent_buffer *eb, struct btrfs_chunk *chunk)
80 int num_stripes = btrfs_chunk_num_stripes(eb, chunk);
81 int i;
82 printf("\t\tchunk owner %llu type %llu num_stripes %d\n",
83 (unsigned long long)btrfs_chunk_owner(eb, chunk),
84 (unsigned long long)btrfs_chunk_type(eb, chunk),
85 num_stripes);
86 for (i = 0 ; i < num_stripes ; i++) {
87 printf("\t\t\tstripe %d devid %llu offset %llu\n", i,
88 (unsigned long long)btrfs_stripe_devid_nr(eb, chunk, i),
89 (unsigned long long)btrfs_stripe_offset_nr(eb, chunk, i));
92 static void print_dev_item(struct extent_buffer *eb,
93 struct btrfs_dev_item *dev_item)
95 printf("\t\tdev item devid %llu "
96 "total_bytes %llu bytes used %Lu\n",
97 (unsigned long long)btrfs_device_id(eb, dev_item),
98 (unsigned long long)btrfs_device_total_bytes(eb, dev_item),
99 (unsigned long long)btrfs_device_bytes_used(eb, dev_item));
101 void btrfs_print_leaf(struct btrfs_root *root, struct extent_buffer *l)
103 int i;
104 char *str;
105 struct btrfs_item *item;
106 struct btrfs_extent_item *ei;
107 struct btrfs_root_item *ri;
108 struct btrfs_dir_item *di;
109 struct btrfs_inode_item *ii;
110 struct btrfs_file_extent_item *fi;
111 struct btrfs_csum_item *ci;
112 struct btrfs_block_group_item *bi;
113 struct btrfs_extent_ref *ref;
114 struct btrfs_inode_ref *iref;
115 struct btrfs_dev_extent *dev_extent;
116 struct btrfs_disk_key disk_key;
117 struct btrfs_root_item root_item;
118 struct btrfs_block_group_item bg_item;
119 u32 nr = btrfs_header_nritems(l);
120 u32 type;
122 printf("leaf %llu ptrs %d free space %d generation %llu owner %llu\n",
123 (unsigned long long)btrfs_header_bytenr(l), nr,
124 btrfs_leaf_free_space(root, l),
125 (unsigned long long)btrfs_header_generation(l),
126 (unsigned long long)btrfs_header_owner(l));
127 fflush(stdout);
128 for (i = 0 ; i < nr ; i++) {
129 item = btrfs_item_nr(l, i);
130 btrfs_item_key(l, &disk_key, i);
131 type = btrfs_disk_key_type(&disk_key);
132 printf("\titem %d key (%llu %x %llu) itemoff %d itemsize %d\n",
134 (unsigned long long)btrfs_disk_key_objectid(&disk_key),
135 btrfs_disk_key_type(&disk_key),
136 (unsigned long long)btrfs_disk_key_offset(&disk_key),
137 btrfs_item_offset(l, item),
138 btrfs_item_size(l, item));
139 switch (type) {
140 case BTRFS_INODE_ITEM_KEY:
141 ii = btrfs_item_ptr(l, i, struct btrfs_inode_item);
142 printf("\t\tinode generation %llu size %llu block group %llu mode %o links %u\n",
143 (unsigned long long)btrfs_inode_generation(l, ii),
144 (unsigned long long)btrfs_inode_size(l, ii),
145 (unsigned long long)btrfs_inode_block_group(l,ii),
146 btrfs_inode_mode(l, ii),
147 btrfs_inode_nlink(l, ii));
148 break;
149 case BTRFS_INODE_REF_KEY:
150 iref = btrfs_item_ptr(l, i, struct btrfs_inode_ref);
151 print_inode_ref_item(l, item, iref);
152 break;
153 case BTRFS_DIR_ITEM_KEY:
154 case BTRFS_DIR_INDEX_KEY:
155 case BTRFS_XATTR_ITEM_KEY:
156 di = btrfs_item_ptr(l, i, struct btrfs_dir_item);
157 print_dir_item(l, item, di);
158 break;
159 case BTRFS_ROOT_ITEM_KEY:
160 ri = btrfs_item_ptr(l, i, struct btrfs_root_item);
161 read_extent_buffer(l, &root_item, (unsigned long)ri, sizeof(root_item));
162 printf("\t\troot data bytenr %llu level %d dirid %llu refs %u\n",
163 (unsigned long long)btrfs_root_bytenr(&root_item),
164 btrfs_root_level(&root_item),
165 (unsigned long long)btrfs_root_dirid(&root_item),
166 btrfs_root_refs(&root_item));
167 if (1 || btrfs_root_refs(&root_item) == 0) {
168 struct btrfs_key drop_key;
169 btrfs_disk_key_to_cpu(&drop_key,
170 &root_item.drop_progress);
171 printf("\t\tdrop key %Lu %x %Lu level %d\n",
172 (unsigned long long)drop_key.objectid,
173 drop_key.type,
174 (unsigned long long)drop_key.offset,
175 root_item.drop_level);
177 break;
178 case BTRFS_EXTENT_ITEM_KEY:
179 ei = btrfs_item_ptr(l, i, struct btrfs_extent_item);
180 printf("\t\textent data refs %u\n",
181 btrfs_extent_refs(l, ei));
182 break;
183 case BTRFS_EXTENT_REF_KEY:
184 ref = btrfs_item_ptr(l, i, struct btrfs_extent_ref);
185 printf("\t\textent back ref root %llu gen %llu "
186 "owner %llu offset %llu\n",
187 (unsigned long long)btrfs_ref_root(l, ref),
188 (unsigned long long)btrfs_ref_generation(l, ref),
189 (unsigned long long)btrfs_ref_objectid(l, ref),
190 (unsigned long long)btrfs_ref_offset(l, ref));
191 break;
192 case BTRFS_CSUM_ITEM_KEY:
193 ci = btrfs_item_ptr(l, i, struct btrfs_csum_item);
194 printf("\t\tcsum item\n");
195 break;
196 case BTRFS_EXTENT_DATA_KEY:
197 fi = btrfs_item_ptr(l, i,
198 struct btrfs_file_extent_item);
199 if (btrfs_file_extent_type(l, fi) ==
200 BTRFS_FILE_EXTENT_INLINE) {
201 printf("\t\tinline extent data size %u\n",
202 btrfs_file_extent_inline_len(l, item));
203 break;
205 printf("\t\textent data disk byte %llu nr %llu\n",
206 (unsigned long long)btrfs_file_extent_disk_bytenr(l, fi),
207 (unsigned long long)btrfs_file_extent_disk_num_bytes(l, fi));
208 printf("\t\textent data offset %llu nr %llu\n",
209 (unsigned long long)btrfs_file_extent_offset(l, fi),
210 (unsigned long long)btrfs_file_extent_num_bytes(l, fi));
211 break;
212 case BTRFS_BLOCK_GROUP_ITEM_KEY:
213 bi = btrfs_item_ptr(l, i,
214 struct btrfs_block_group_item);
215 read_extent_buffer(l, &bg_item, (unsigned long)bi,
216 sizeof(bg_item));
217 printf("\t\tblock group used %llu flags %llx\n",
218 (unsigned long long)btrfs_block_group_used(&bg_item),
219 (unsigned long long)btrfs_block_group_flags(&bg_item));
220 break;
221 case BTRFS_CHUNK_ITEM_KEY:
222 print_chunk(l, btrfs_item_ptr(l, i, struct btrfs_chunk));
223 break;
224 case BTRFS_DEV_ITEM_KEY:
225 print_dev_item(l, btrfs_item_ptr(l, i,
226 struct btrfs_dev_item));
227 break;
228 case BTRFS_DEV_EXTENT_KEY:
229 dev_extent = btrfs_item_ptr(l, i,
230 struct btrfs_dev_extent);
231 printf("\t\tdev extent owner %llu length %llu\n",
232 (unsigned long long)btrfs_dev_extent_owner(l, dev_extent),
233 (unsigned long long)btrfs_dev_extent_length(l, dev_extent));
234 break;
235 case BTRFS_STRING_ITEM_KEY:
236 /* dirty, but it's simple */
237 str = l->data + btrfs_item_ptr_offset(l, i);
238 printf("\t\titem data %.*s\n", btrfs_item_size(l, item), str);
239 break;
241 fflush(stdout);
244 void btrfs_print_tree(struct btrfs_root *root, struct extent_buffer *eb)
246 int i;
247 u32 nr;
248 u32 size;
249 struct btrfs_key key;
251 if (!eb)
252 return;
253 nr = btrfs_header_nritems(eb);
254 if (btrfs_is_leaf(eb)) {
255 btrfs_print_leaf(root, eb);
256 return;
258 printf("node %llu level %d ptrs %d free %u generation %llu owner %llu\n",
259 (unsigned long long)eb->start,
260 btrfs_header_level(eb), nr,
261 (u32)BTRFS_NODEPTRS_PER_BLOCK(root) - nr,
262 (unsigned long long)btrfs_header_generation(eb),
263 (unsigned long long)btrfs_header_owner(eb));
264 fflush(stdout);
265 size = btrfs_level_size(root, btrfs_header_level(eb) - 1);
266 for (i = 0; i < nr; i++) {
267 u64 blocknr = btrfs_node_blockptr(eb, i);
268 btrfs_node_key_to_cpu(eb, &key, i);
269 printf("\tkey %d (%llu %x %llu) block %llu (%llu) gen %llu\n",
271 (unsigned long long)key.objectid,
272 key.type,
273 (unsigned long long)key.offset,
274 (unsigned long long)blocknr,
275 (unsigned long long)blocknr / size,
276 (unsigned long long)btrfs_node_ptr_generation(eb, i));
277 fflush(stdout);
279 for (i = 0; i < nr; i++) {
280 struct extent_buffer *next = read_tree_block(root,
281 btrfs_node_blockptr(eb, i),
282 size);
283 if (btrfs_is_leaf(next) &&
284 btrfs_header_level(eb) != 1)
285 BUG();
286 if (btrfs_header_level(next) !=
287 btrfs_header_level(eb) - 1)
288 BUG();
289 btrfs_print_tree(root, next);
290 free_extent_buffer(next);