Add scan of the btrfs log tree to btrfs-debug-tree
[btrfs-progs-unstable/devel.git] / print-tree.c
blob5eb8bfe91b8ac6499fb08d309974f3035dc1fe51
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 <uuid/uuid.h>
22 #include "kerncompat.h"
23 #include "radix-tree.h"
24 #include "ctree.h"
25 #include "disk-io.h"
27 static int print_dir_item(struct extent_buffer *eb, struct btrfs_item *item,
28 struct btrfs_dir_item *di)
30 u32 total;
31 u32 cur = 0;
32 u32 len;
33 u32 name_len;
34 u32 data_len;
35 char namebuf[BTRFS_NAME_LEN];
36 struct btrfs_disk_key location;
38 total = btrfs_item_size(eb, item);
39 while(cur < total) {
40 btrfs_dir_item_key(eb, di, &location);
41 printf("\t\tdir index %llu type %u\n",
42 (unsigned long long)btrfs_disk_key_objectid(&location),
43 btrfs_dir_type(eb, di));
44 name_len = btrfs_dir_name_len(eb, di);
45 data_len = btrfs_dir_data_len(eb, di);
46 len = (name_len <= sizeof(namebuf))? name_len: sizeof(namebuf);
47 read_extent_buffer(eb, namebuf, (unsigned long)(di + 1), len);
48 printf("\t\tnamelen %u datalen %u name: %.*s\n",
49 name_len, data_len, len, namebuf);
50 len = sizeof(*di) + name_len + data_len;
51 di = (struct btrfs_dir_item *)((char *)di + len);
52 cur += len;
54 return 0;
57 static int print_inode_ref_item(struct extent_buffer *eb, struct btrfs_item *item,
58 struct btrfs_inode_ref *ref)
60 u32 total;
61 u32 cur = 0;
62 u32 len;
63 u32 name_len;
64 u64 index;
65 char namebuf[BTRFS_NAME_LEN];
66 total = btrfs_item_size(eb, item);
67 while(cur < total) {
68 name_len = btrfs_inode_ref_name_len(eb, ref);
69 index = btrfs_inode_ref_index(eb, ref);
70 len = (name_len <= sizeof(namebuf))? name_len: sizeof(namebuf);
71 read_extent_buffer(eb, namebuf, (unsigned long)(ref + 1), len);
72 printf("\t\tinode ref index %llu namelen %u name: %.*s\n",
73 index, name_len, len, namebuf);
74 len = sizeof(*ref) + name_len;
75 ref = (struct btrfs_inode_ref *)((char *)ref + len);
76 cur += len;
78 return 0;
81 static void print_chunk(struct extent_buffer *eb, struct btrfs_chunk *chunk)
83 int num_stripes = btrfs_chunk_num_stripes(eb, chunk);
84 int i;
85 printf("\t\tchunk length %llu owner %llu type %llu num_stripes %d\n",
86 (unsigned long long)btrfs_chunk_length(eb, chunk),
87 (unsigned long long)btrfs_chunk_owner(eb, chunk),
88 (unsigned long long)btrfs_chunk_type(eb, chunk),
89 num_stripes);
90 for (i = 0 ; i < num_stripes ; i++) {
91 printf("\t\t\tstripe %d devid %llu offset %llu\n", i,
92 (unsigned long long)btrfs_stripe_devid_nr(eb, chunk, i),
93 (unsigned long long)btrfs_stripe_offset_nr(eb, chunk, i));
96 static void print_dev_item(struct extent_buffer *eb,
97 struct btrfs_dev_item *dev_item)
99 printf("\t\tdev item devid %llu "
100 "total_bytes %llu bytes used %Lu\n",
101 (unsigned long long)btrfs_device_id(eb, dev_item),
102 (unsigned long long)btrfs_device_total_bytes(eb, dev_item),
103 (unsigned long long)btrfs_device_bytes_used(eb, dev_item));
106 static void print_uuids(struct extent_buffer *eb)
108 char fs_uuid[37];
109 char chunk_uuid[37];
110 u8 disk_uuid[BTRFS_UUID_SIZE];
112 read_extent_buffer(eb, disk_uuid, (unsigned long)btrfs_header_fsid(eb),
113 BTRFS_FSID_SIZE);
115 fs_uuid[36] = '\0';
116 uuid_unparse(disk_uuid, fs_uuid);
118 read_extent_buffer(eb, disk_uuid,
119 (unsigned long)btrfs_header_chunk_tree_uuid(eb),
120 BTRFS_UUID_SIZE);
122 chunk_uuid[36] = '\0';
123 uuid_unparse(disk_uuid, chunk_uuid);
124 printf("fs uuid %s\nchunk uuid %s\n", fs_uuid, chunk_uuid);
127 static void print_file_extent_item(struct extent_buffer *eb,
128 struct btrfs_item *item,
129 struct btrfs_file_extent_item *fi)
131 int extent_type = btrfs_file_extent_type(eb, fi);
133 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
134 printf("\t\tinline extent data size %u "
135 "ram %u compress %d\n",
136 btrfs_file_extent_inline_item_len(eb, item),
137 btrfs_file_extent_inline_len(eb, fi),
138 btrfs_file_extent_compression(eb, fi));
139 return;
141 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
142 printf("\t\tprealloc data disk byte %llu nr %llu\n",
143 (unsigned long long)btrfs_file_extent_disk_bytenr(eb, fi),
144 (unsigned long long)btrfs_file_extent_disk_num_bytes(eb, fi));
145 printf("\t\tprealloc data offset %llu nr %llu\n",
146 (unsigned long long)btrfs_file_extent_offset(eb, fi),
147 (unsigned long long)btrfs_file_extent_num_bytes(eb, fi));
148 return;
150 printf("\t\textent data disk byte %llu nr %llu\n",
151 (unsigned long long)btrfs_file_extent_disk_bytenr(eb, fi),
152 (unsigned long long)btrfs_file_extent_disk_num_bytes(eb, fi));
153 printf("\t\textent data offset %llu nr %llu ram %llu\n",
154 (unsigned long long)btrfs_file_extent_offset(eb, fi),
155 (unsigned long long)btrfs_file_extent_num_bytes(eb, fi),
156 (unsigned long long)btrfs_file_extent_ram_bytes(eb, fi));
157 printf("\t\textent compression %d\n",
158 btrfs_file_extent_compression(eb, fi));
162 static void print_root_ref(struct extent_buffer *leaf, int slot, char *tag)
164 struct btrfs_root_ref *ref;
165 char namebuf[BTRFS_NAME_LEN];
166 int namelen;
168 ref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
169 namelen = btrfs_root_ref_name_len(leaf, ref);
170 read_extent_buffer(leaf, namebuf, (unsigned long)(ref + 1), namelen);
171 printf("\t\troot %s key dirid %llu sequence %llu name %.*s\n", tag,
172 (unsigned long long)btrfs_root_ref_dirid(leaf, ref),
173 (unsigned long long)btrfs_root_ref_sequence(leaf, ref),
174 namelen, namebuf);
177 static void print_key_type(u8 type)
179 switch (type) {
180 case BTRFS_INODE_ITEM_KEY:
181 printf("INODE_ITEM");
182 break;
183 case BTRFS_INODE_REF_KEY:
184 printf("INODE_REF");
185 break;
186 case BTRFS_DIR_ITEM_KEY:
187 printf("DIR_ITEM");
188 break;
189 case BTRFS_DIR_INDEX_KEY:
190 printf("DIR_INDEX");
191 break;
192 case BTRFS_DIR_LOG_ITEM_KEY:
193 printf("DIR_LOG_ITEM");
194 break;
195 case BTRFS_DIR_LOG_INDEX_KEY:
196 printf("DIR_LOG_INDEX");
197 break;
198 case BTRFS_XATTR_ITEM_KEY:
199 printf("XATTR_ITEM");
200 break;
201 case BTRFS_ORPHAN_ITEM_KEY:
202 printf("ORPHAN_ITEM");
203 break;
204 case BTRFS_ROOT_ITEM_KEY:
205 printf("ROOT_ITEM");
206 break;
207 case BTRFS_ROOT_REF_KEY:
208 printf("ROOT_REF");
209 break;
210 case BTRFS_ROOT_BACKREF_KEY:
211 printf("ROOT_BACKREF");
212 break;
213 case BTRFS_EXTENT_ITEM_KEY:
214 printf("EXTENT_ITEM");
215 break;
216 case BTRFS_EXTENT_REF_KEY:
217 printf("EXTENT_REF");
218 break;
219 case BTRFS_CSUM_ITEM_KEY:
220 printf("CSUM_ITEM");
221 break;
222 case BTRFS_EXTENT_CSUM_KEY:
223 printf("EXTENT_CSUM");
224 break;
225 case BTRFS_EXTENT_DATA_KEY:
226 printf("EXTENT_DATA");
227 break;
228 case BTRFS_BLOCK_GROUP_ITEM_KEY:
229 printf("GROUP_ITEM");
230 break;
231 case BTRFS_CHUNK_ITEM_KEY:
232 printf("CHUNK_ITEM");
233 break;
234 case BTRFS_DEV_ITEM_KEY:
235 printf("DEV_ITEM");
236 break;
237 case BTRFS_DEV_EXTENT_KEY:
238 printf("DEV_EXTENT");
239 break;
240 case BTRFS_STRING_ITEM_KEY:
241 printf("STRING_ITEM");
242 break;
243 default:
244 printf("UNKNOWN");
248 void btrfs_print_leaf(struct btrfs_root *root, struct extent_buffer *l)
250 int i;
251 char *str;
252 struct btrfs_item *item;
253 struct btrfs_extent_item *ei;
254 struct btrfs_root_item *ri;
255 struct btrfs_dir_item *di;
256 struct btrfs_inode_item *ii;
257 struct btrfs_file_extent_item *fi;
258 struct btrfs_csum_item *ci;
259 struct btrfs_block_group_item *bi;
260 struct btrfs_extent_ref *ref;
261 struct btrfs_inode_ref *iref;
262 struct btrfs_dev_extent *dev_extent;
263 struct btrfs_disk_key disk_key;
264 struct btrfs_root_item root_item;
265 struct btrfs_block_group_item bg_item;
266 struct btrfs_dir_log_item *dlog;
267 u32 nr = btrfs_header_nritems(l);
268 u32 type;
270 printf("leaf %llu ptrs %d free space %d generation %llu owner %llu\n",
271 (unsigned long long)btrfs_header_bytenr(l), nr,
272 btrfs_leaf_free_space(root, l),
273 (unsigned long long)btrfs_header_generation(l),
274 (unsigned long long)btrfs_header_owner(l));
275 print_uuids(l);
276 fflush(stdout);
277 for (i = 0 ; i < nr ; i++) {
278 item = btrfs_item_nr(l, i);
279 btrfs_item_key(l, &disk_key, i);
280 type = btrfs_disk_key_type(&disk_key);
281 printf("\titem %d key (%llu ",
283 (unsigned long long)btrfs_disk_key_objectid(&disk_key));
284 print_key_type(type);
285 printf(" %llu) itemoff %d itemsize %d\n",
286 (unsigned long long)btrfs_disk_key_offset(&disk_key),
287 btrfs_item_offset(l, item),
288 btrfs_item_size(l, item));
289 switch (type) {
290 case BTRFS_INODE_ITEM_KEY:
291 ii = btrfs_item_ptr(l, i, struct btrfs_inode_item);
292 printf("\t\tinode generation %llu size %llu block group %llu mode %o links %u\n",
293 (unsigned long long)btrfs_inode_generation(l, ii),
294 (unsigned long long)btrfs_inode_size(l, ii),
295 (unsigned long long)btrfs_inode_block_group(l,ii),
296 btrfs_inode_mode(l, ii),
297 btrfs_inode_nlink(l, ii));
298 break;
299 case BTRFS_INODE_REF_KEY:
300 iref = btrfs_item_ptr(l, i, struct btrfs_inode_ref);
301 print_inode_ref_item(l, item, iref);
302 break;
303 case BTRFS_DIR_ITEM_KEY:
304 case BTRFS_DIR_INDEX_KEY:
305 case BTRFS_XATTR_ITEM_KEY:
306 di = btrfs_item_ptr(l, i, struct btrfs_dir_item);
307 print_dir_item(l, item, di);
308 break;
309 case BTRFS_DIR_LOG_INDEX_KEY:
310 case BTRFS_DIR_LOG_ITEM_KEY:
311 dlog = btrfs_item_ptr(l, i, struct btrfs_dir_log_item);
312 printf("\t\tdir log end %Lu\n",
313 btrfs_dir_log_end(l, dlog));
314 break;
315 case BTRFS_ORPHAN_ITEM_KEY:
316 printf("\t\torphan item\n");
317 break;
318 case BTRFS_ROOT_ITEM_KEY:
319 ri = btrfs_item_ptr(l, i, struct btrfs_root_item);
320 read_extent_buffer(l, &root_item, (unsigned long)ri, sizeof(root_item));
321 printf("\t\troot data bytenr %llu level %d dirid %llu refs %u\n",
322 (unsigned long long)btrfs_root_bytenr(&root_item),
323 btrfs_root_level(&root_item),
324 (unsigned long long)btrfs_root_dirid(&root_item),
325 btrfs_root_refs(&root_item));
326 if (1 || btrfs_root_refs(&root_item) == 0) {
327 struct btrfs_key drop_key;
328 btrfs_disk_key_to_cpu(&drop_key,
329 &root_item.drop_progress);
330 printf("\t\tdrop key %Lu %x %Lu level %d\n",
331 (unsigned long long)drop_key.objectid,
332 drop_key.type,
333 (unsigned long long)drop_key.offset,
334 root_item.drop_level);
336 break;
337 case BTRFS_ROOT_REF_KEY:
338 print_root_ref(l, i, "ref");
339 break;
340 case BTRFS_ROOT_BACKREF_KEY:
341 print_root_ref(l, i, "backref");
342 break;
343 case BTRFS_EXTENT_ITEM_KEY:
344 ei = btrfs_item_ptr(l, i, struct btrfs_extent_item);
345 printf("\t\textent data refs %u\n",
346 btrfs_extent_refs(l, ei));
347 break;
348 case BTRFS_EXTENT_REF_KEY:
349 ref = btrfs_item_ptr(l, i, struct btrfs_extent_ref);
350 printf("\t\textent back ref root %llu gen %llu "
351 "owner %llu num_refs %lu\n",
352 (unsigned long long)btrfs_ref_root(l, ref),
353 (unsigned long long)btrfs_ref_generation(l, ref),
354 (unsigned long long)btrfs_ref_objectid(l, ref),
355 (unsigned long)btrfs_ref_num_refs(l, ref));
356 break;
357 case BTRFS_CSUM_ITEM_KEY:
358 ci = btrfs_item_ptr(l, i, struct btrfs_csum_item);
359 printf("\t\tcsum item\n");
360 break;
361 case BTRFS_EXTENT_CSUM_KEY:
362 ci = btrfs_item_ptr(l, i, struct btrfs_csum_item);
363 printf("\t\textent csum item\n");
364 break;
365 case BTRFS_EXTENT_DATA_KEY:
366 fi = btrfs_item_ptr(l, i,
367 struct btrfs_file_extent_item);
368 print_file_extent_item(l, item, fi);
369 break;
370 case BTRFS_BLOCK_GROUP_ITEM_KEY:
371 bi = btrfs_item_ptr(l, i,
372 struct btrfs_block_group_item);
373 read_extent_buffer(l, &bg_item, (unsigned long)bi,
374 sizeof(bg_item));
375 printf("\t\tblock group used %llu flags %llu\n",
376 (unsigned long long)btrfs_block_group_used(&bg_item),
377 (unsigned long long)btrfs_block_group_flags(&bg_item));
378 break;
379 case BTRFS_CHUNK_ITEM_KEY:
380 print_chunk(l, btrfs_item_ptr(l, i, struct btrfs_chunk));
381 break;
382 case BTRFS_DEV_ITEM_KEY:
383 print_dev_item(l, btrfs_item_ptr(l, i,
384 struct btrfs_dev_item));
385 break;
386 case BTRFS_DEV_EXTENT_KEY:
387 dev_extent = btrfs_item_ptr(l, i,
388 struct btrfs_dev_extent);
389 printf("\t\tdev extent chunk_tree %llu\n"
390 "\t\tchunk objectid %llu chunk offset %llu "
391 "length %llu\n",
392 (unsigned long long)
393 btrfs_dev_extent_chunk_tree(l, dev_extent),
394 (unsigned long long)
395 btrfs_dev_extent_chunk_objectid(l, dev_extent),
396 (unsigned long long)
397 btrfs_dev_extent_chunk_offset(l, dev_extent),
398 (unsigned long long)
399 btrfs_dev_extent_length(l, dev_extent));
400 break;
401 case BTRFS_STRING_ITEM_KEY:
402 /* dirty, but it's simple */
403 str = l->data + btrfs_item_ptr_offset(l, i);
404 printf("\t\titem data %.*s\n", btrfs_item_size(l, item), str);
405 break;
407 fflush(stdout);
411 void btrfs_print_tree(struct btrfs_root *root, struct extent_buffer *eb)
413 int i;
414 u32 nr;
415 u32 size;
416 struct btrfs_key key;
418 if (!eb)
419 return;
420 nr = btrfs_header_nritems(eb);
421 if (btrfs_is_leaf(eb)) {
422 btrfs_print_leaf(root, eb);
423 return;
425 printf("node %llu level %d ptrs %d free %u generation %llu owner %llu\n",
426 (unsigned long long)eb->start,
427 btrfs_header_level(eb), nr,
428 (u32)BTRFS_NODEPTRS_PER_BLOCK(root) - nr,
429 (unsigned long long)btrfs_header_generation(eb),
430 (unsigned long long)btrfs_header_owner(eb));
431 print_uuids(eb);
432 fflush(stdout);
433 size = btrfs_level_size(root, btrfs_header_level(eb) - 1);
434 for (i = 0; i < nr; i++) {
435 u64 blocknr = btrfs_node_blockptr(eb, i);
436 btrfs_node_key_to_cpu(eb, &key, i);
437 printf("\tkey %d (%llu ",
439 (unsigned long long)key.objectid);
440 print_key_type(key.type);
441 printf(" %llu) block %llu (%llu) gen %llu\n",
442 (unsigned long long)key.offset,
443 (unsigned long long)blocknr,
444 (unsigned long long)blocknr / size,
445 (unsigned long long)btrfs_node_ptr_generation(eb, i));
446 fflush(stdout);
448 for (i = 0; i < nr; i++) {
449 struct extent_buffer *next = read_tree_block(root,
450 btrfs_node_blockptr(eb, i),
451 size,
452 btrfs_node_ptr_generation(eb, i));
453 if (btrfs_is_leaf(next) &&
454 btrfs_header_level(eb) != 1)
455 BUG();
456 if (btrfs_header_level(next) !=
457 btrfs_header_level(eb) - 1)
458 BUG();
459 btrfs_print_tree(root, next);
460 free_extent_buffer(next);