btrfs-progs: tests: add mkfs tests
[btrfs-progs-unstable/devel.git] / print-tree.c
blob7ddf400dbfe8224db7f5e43d51692bdfd0048585
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"
26 #include "print-tree.h"
27 #include "utils.h"
30 static void print_dir_item_type(struct extent_buffer *eb,
31 struct btrfs_dir_item *di)
33 u8 type = btrfs_dir_type(eb, di);
35 switch (type) {
36 case BTRFS_FT_REG_FILE:
37 printf("FILE");
38 break;
39 case BTRFS_FT_DIR:
40 printf("DIR");
41 break;
42 case BTRFS_FT_CHRDEV:
43 printf("CHRDEV");
44 break;
45 case BTRFS_FT_BLKDEV:
46 printf("BLKDEV");
47 break;
48 case BTRFS_FT_FIFO:
49 printf("FIFO");
50 break;
51 case BTRFS_FT_SOCK:
52 printf("SOCK");
53 break;
54 case BTRFS_FT_SYMLINK:
55 printf("SYMLINK");
56 break;
57 case BTRFS_FT_XATTR:
58 printf("XATTR");
59 break;
60 default:
61 printf("%u", type);
65 static int print_dir_item(struct extent_buffer *eb, struct btrfs_item *item,
66 struct btrfs_dir_item *di)
68 u32 total;
69 u32 cur = 0;
70 u32 len;
71 u32 name_len;
72 u32 data_len;
73 char namebuf[BTRFS_NAME_LEN];
74 struct btrfs_disk_key location;
76 total = btrfs_item_size(eb, item);
77 while(cur < total) {
78 btrfs_dir_item_key(eb, di, &location);
79 printf("\t\tlocation ");
80 btrfs_print_key(&location);
81 printf(" type ");
82 print_dir_item_type(eb, di);
83 printf("\n");
84 name_len = btrfs_dir_name_len(eb, di);
85 data_len = btrfs_dir_data_len(eb, di);
86 len = (name_len <= sizeof(namebuf))? name_len: sizeof(namebuf);
87 read_extent_buffer(eb, namebuf, (unsigned long)(di + 1), len);
88 printf("\t\tnamelen %u datalen %u name: %.*s\n",
89 name_len, data_len, len, namebuf);
90 if (data_len) {
91 len = (data_len <= sizeof(namebuf))? data_len: sizeof(namebuf);
92 read_extent_buffer(eb, namebuf,
93 (unsigned long)(di + 1) + name_len, len);
94 printf("\t\tdata %.*s\n", len, namebuf);
96 len = sizeof(*di) + name_len + data_len;
97 di = (struct btrfs_dir_item *)((char *)di + len);
98 cur += len;
100 return 0;
103 static int print_inode_extref_item(struct extent_buffer *eb,
104 struct btrfs_item *item,
105 struct btrfs_inode_extref *extref)
107 u32 total;
108 u32 cur = 0;
109 u32 len;
110 u32 name_len = 0;
111 u64 index = 0;
112 u64 parent_objid;
113 char namebuf[BTRFS_NAME_LEN];
115 total = btrfs_item_size(eb, item);
117 while (cur < total) {
118 index = btrfs_inode_extref_index(eb, extref);
119 name_len = btrfs_inode_extref_name_len(eb, extref);
120 parent_objid = btrfs_inode_extref_parent(eb, extref);
122 len = (name_len <= sizeof(namebuf))? name_len: sizeof(namebuf);
124 read_extent_buffer(eb, namebuf, (unsigned long)(extref->name), len);
126 printf("\t\tinode extref index %llu parent %llu namelen %u "
127 "name: %.*s\n",
128 (unsigned long long)index,
129 (unsigned long long)parent_objid,
130 name_len, len, namebuf);
132 len = sizeof(*extref) + name_len;
133 extref = (struct btrfs_inode_extref *)((char *)extref + len);
134 cur += len;
136 return 0;
139 static int print_inode_ref_item(struct extent_buffer *eb, struct btrfs_item *item,
140 struct btrfs_inode_ref *ref)
142 u32 total;
143 u32 cur = 0;
144 u32 len;
145 u32 name_len;
146 u64 index;
147 char namebuf[BTRFS_NAME_LEN];
148 total = btrfs_item_size(eb, item);
149 while(cur < total) {
150 name_len = btrfs_inode_ref_name_len(eb, ref);
151 index = btrfs_inode_ref_index(eb, ref);
152 len = (name_len <= sizeof(namebuf))? name_len: sizeof(namebuf);
153 read_extent_buffer(eb, namebuf, (unsigned long)(ref + 1), len);
154 printf("\t\tinode ref index %llu namelen %u name: %.*s\n",
155 (unsigned long long)index, name_len, len, namebuf);
156 len = sizeof(*ref) + name_len;
157 ref = (struct btrfs_inode_ref *)((char *)ref + len);
158 cur += len;
160 return 0;
163 /* Caller should ensure sizeof(*ret)>=21 "DATA|METADATA|RAID10" */
164 static void bg_flags_to_str(u64 flags, char *ret)
166 int empty = 1;
168 if (flags & BTRFS_BLOCK_GROUP_DATA) {
169 empty = 0;
170 strcpy(ret, "DATA");
172 if (flags & BTRFS_BLOCK_GROUP_METADATA) {
173 if (!empty)
174 strcat(ret, "|");
175 strcat(ret, "METADATA");
177 if (flags & BTRFS_BLOCK_GROUP_SYSTEM) {
178 if (!empty)
179 strcat(ret, "|");
180 strcat(ret, "SYSTEM");
182 switch (flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
183 case BTRFS_BLOCK_GROUP_RAID0:
184 strcat(ret, "|RAID0");
185 break;
186 case BTRFS_BLOCK_GROUP_RAID1:
187 strcat(ret, "|RAID1");
188 break;
189 case BTRFS_BLOCK_GROUP_DUP:
190 strcat(ret, "|DUP");
191 break;
192 case BTRFS_BLOCK_GROUP_RAID10:
193 strcat(ret, "|RAID10");
194 break;
195 case BTRFS_BLOCK_GROUP_RAID5:
196 strcat(ret, "|RAID5");
197 break;
198 case BTRFS_BLOCK_GROUP_RAID6:
199 strcat(ret, "|RAID6");
200 break;
201 default:
202 break;
206 /* Caller should ensure sizeof(*ret)>= 26 "OFF|SCANNING|INCONSISTENT" */
207 static void qgroup_flags_to_str(u64 flags, char *ret)
209 if (flags & BTRFS_QGROUP_STATUS_FLAG_ON)
210 strcpy(ret, "ON");
211 else
212 strcpy(ret, "OFF");
214 if (flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
215 strcat(ret, "|SCANNING");
216 if (flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT)
217 strcat(ret, "|INCONSISTENT");
220 void print_chunk(struct extent_buffer *eb, struct btrfs_chunk *chunk)
222 int num_stripes = btrfs_chunk_num_stripes(eb, chunk);
223 int i;
224 char chunk_flags_str[32] = {0};
226 bg_flags_to_str(btrfs_chunk_type(eb, chunk), chunk_flags_str);
227 printf("\t\tchunk length %llu owner %llu stripe_len %llu\n",
228 (unsigned long long)btrfs_chunk_length(eb, chunk),
229 (unsigned long long)btrfs_chunk_owner(eb, chunk),
230 (unsigned long long)btrfs_chunk_stripe_len(eb, chunk));
231 printf("\t\ttype %s num_stripes %d\n",
232 chunk_flags_str, num_stripes);
233 for (i = 0 ; i < num_stripes ; i++) {
234 printf("\t\t\tstripe %d devid %llu offset %llu\n", i,
235 (unsigned long long)btrfs_stripe_devid_nr(eb, chunk, i),
236 (unsigned long long)btrfs_stripe_offset_nr(eb, chunk, i));
240 static void print_dev_item(struct extent_buffer *eb,
241 struct btrfs_dev_item *dev_item)
243 char disk_uuid_c[BTRFS_UUID_UNPARSED_SIZE];
244 u8 disk_uuid[BTRFS_UUID_SIZE];
246 read_extent_buffer(eb, disk_uuid,
247 (unsigned long)btrfs_device_uuid(dev_item),
248 BTRFS_UUID_SIZE);
249 uuid_unparse(disk_uuid, disk_uuid_c);
250 printf("\t\tdev item devid %llu "
251 "total_bytes %llu bytes used %Lu\n"
252 "\t\tdev uuid %s\n",
253 (unsigned long long)btrfs_device_id(eb, dev_item),
254 (unsigned long long)btrfs_device_total_bytes(eb, dev_item),
255 (unsigned long long)btrfs_device_bytes_used(eb, dev_item),
256 disk_uuid_c);
259 static void print_uuids(struct extent_buffer *eb)
261 char fs_uuid[BTRFS_UUID_UNPARSED_SIZE];
262 char chunk_uuid[BTRFS_UUID_UNPARSED_SIZE];
263 u8 disk_uuid[BTRFS_UUID_SIZE];
265 read_extent_buffer(eb, disk_uuid, btrfs_header_fsid(),
266 BTRFS_FSID_SIZE);
268 fs_uuid[BTRFS_UUID_UNPARSED_SIZE - 1] = '\0';
269 uuid_unparse(disk_uuid, fs_uuid);
271 read_extent_buffer(eb, disk_uuid,
272 btrfs_header_chunk_tree_uuid(eb),
273 BTRFS_UUID_SIZE);
275 chunk_uuid[BTRFS_UUID_UNPARSED_SIZE - 1] = '\0';
276 uuid_unparse(disk_uuid, chunk_uuid);
277 printf("fs uuid %s\nchunk uuid %s\n", fs_uuid, chunk_uuid);
280 static void print_file_extent_item(struct extent_buffer *eb,
281 struct btrfs_item *item,
282 int slot,
283 struct btrfs_file_extent_item *fi)
285 int extent_type = btrfs_file_extent_type(eb, fi);
287 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
288 printf("\t\tinline extent data size %u "
289 "ram %u compress %d\n",
290 btrfs_file_extent_inline_item_len(eb, item),
291 btrfs_file_extent_inline_len(eb, slot, fi),
292 btrfs_file_extent_compression(eb, fi));
293 return;
295 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
296 printf("\t\tprealloc data disk byte %llu nr %llu\n",
297 (unsigned long long)btrfs_file_extent_disk_bytenr(eb, fi),
298 (unsigned long long)btrfs_file_extent_disk_num_bytes(eb, fi));
299 printf("\t\tprealloc data offset %llu nr %llu\n",
300 (unsigned long long)btrfs_file_extent_offset(eb, fi),
301 (unsigned long long)btrfs_file_extent_num_bytes(eb, fi));
302 return;
304 printf("\t\textent data disk byte %llu nr %llu\n",
305 (unsigned long long)btrfs_file_extent_disk_bytenr(eb, fi),
306 (unsigned long long)btrfs_file_extent_disk_num_bytes(eb, fi));
307 printf("\t\textent data offset %llu nr %llu ram %llu\n",
308 (unsigned long long)btrfs_file_extent_offset(eb, fi),
309 (unsigned long long)btrfs_file_extent_num_bytes(eb, fi),
310 (unsigned long long)btrfs_file_extent_ram_bytes(eb, fi));
311 printf("\t\textent compression %d\n",
312 btrfs_file_extent_compression(eb, fi));
315 /* Caller should ensure sizeof(*ret) >= 16("DATA|TREE_BLOCK") */
316 static void extent_flags_to_str(u64 flags, char *ret)
318 int empty = 1;
320 if (flags & BTRFS_EXTENT_FLAG_DATA) {
321 empty = 0;
322 strcpy(ret, "DATA");
324 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
325 if (!empty) {
326 empty = 0;
327 strcat(ret, "|");
329 strcat(ret, "TREE_BLOCK");
331 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
332 strcat(ret, "|");
333 strcat(ret, "FULL_BACKREF");
337 void print_extent_item(struct extent_buffer *eb, int slot, int metadata)
339 struct btrfs_extent_item *ei;
340 struct btrfs_extent_inline_ref *iref;
341 struct btrfs_extent_data_ref *dref;
342 struct btrfs_shared_data_ref *sref;
343 struct btrfs_disk_key key;
344 unsigned long end;
345 unsigned long ptr;
346 int type;
347 u32 item_size = btrfs_item_size_nr(eb, slot);
348 u64 flags;
349 u64 offset;
350 char flags_str[32] = {0};
352 if (item_size < sizeof(*ei)) {
353 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
354 struct btrfs_extent_item_v0 *ei0;
355 BUG_ON(item_size != sizeof(*ei0));
356 ei0 = btrfs_item_ptr(eb, slot, struct btrfs_extent_item_v0);
357 printf("\t\textent refs %u\n",
358 btrfs_extent_refs_v0(eb, ei0));
359 return;
360 #else
361 BUG();
362 #endif
365 ei = btrfs_item_ptr(eb, slot, struct btrfs_extent_item);
366 flags = btrfs_extent_flags(eb, ei);
367 extent_flags_to_str(flags, flags_str);
369 printf("\t\textent refs %llu gen %llu flags %s\n",
370 (unsigned long long)btrfs_extent_refs(eb, ei),
371 (unsigned long long)btrfs_extent_generation(eb, ei),
372 flags_str);
374 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !metadata) {
375 struct btrfs_tree_block_info *info;
376 info = (struct btrfs_tree_block_info *)(ei + 1);
377 btrfs_tree_block_key(eb, info, &key);
378 printf("\t\ttree block ");
379 btrfs_print_key(&key);
380 printf(" level %d\n", btrfs_tree_block_level(eb, info));
381 iref = (struct btrfs_extent_inline_ref *)(info + 1);
382 } else if (metadata) {
383 struct btrfs_key tmp;
385 btrfs_item_key_to_cpu(eb, &tmp, slot);
386 printf("\t\ttree block skinny level %d\n", (int)tmp.offset);
387 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
388 } else{
389 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
392 ptr = (unsigned long)iref;
393 end = (unsigned long)ei + item_size;
394 while (ptr < end) {
395 iref = (struct btrfs_extent_inline_ref *)ptr;
396 type = btrfs_extent_inline_ref_type(eb, iref);
397 offset = btrfs_extent_inline_ref_offset(eb, iref);
398 switch (type) {
399 case BTRFS_TREE_BLOCK_REF_KEY:
400 printf("\t\ttree block backref root %llu\n",
401 (unsigned long long)offset);
402 break;
403 case BTRFS_SHARED_BLOCK_REF_KEY:
404 printf("\t\tshared block backref parent %llu\n",
405 (unsigned long long)offset);
406 break;
407 case BTRFS_EXTENT_DATA_REF_KEY:
408 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
409 printf("\t\textent data backref root %llu "
410 "objectid %llu offset %llu count %u\n",
411 (unsigned long long)btrfs_extent_data_ref_root(eb, dref),
412 (unsigned long long)btrfs_extent_data_ref_objectid(eb, dref),
413 (unsigned long long)btrfs_extent_data_ref_offset(eb, dref),
414 btrfs_extent_data_ref_count(eb, dref));
415 break;
416 case BTRFS_SHARED_DATA_REF_KEY:
417 sref = (struct btrfs_shared_data_ref *)(iref + 1);
418 printf("\t\tshared data backref parent %llu count %u\n",
419 (unsigned long long)offset,
420 btrfs_shared_data_ref_count(eb, sref));
421 break;
422 default:
423 return;
425 ptr += btrfs_extent_inline_ref_size(type);
427 WARN_ON(ptr > end);
430 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
431 static void print_extent_ref_v0(struct extent_buffer *eb, int slot)
433 struct btrfs_extent_ref_v0 *ref0;
435 ref0 = btrfs_item_ptr(eb, slot, struct btrfs_extent_ref_v0);
436 printf("\t\textent back ref root %llu gen %llu "
437 "owner %llu num_refs %lu\n",
438 (unsigned long long)btrfs_ref_root_v0(eb, ref0),
439 (unsigned long long)btrfs_ref_generation_v0(eb, ref0),
440 (unsigned long long)btrfs_ref_objectid_v0(eb, ref0),
441 (unsigned long)btrfs_ref_count_v0(eb, ref0));
443 #endif
445 static void print_root_ref(struct extent_buffer *leaf, int slot, char *tag)
447 struct btrfs_root_ref *ref;
448 char namebuf[BTRFS_NAME_LEN];
449 int namelen;
451 ref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
452 namelen = btrfs_root_ref_name_len(leaf, ref);
453 read_extent_buffer(leaf, namebuf, (unsigned long)(ref + 1), namelen);
454 printf("\t\troot %s key dirid %llu sequence %llu name %.*s\n", tag,
455 (unsigned long long)btrfs_root_ref_dirid(leaf, ref),
456 (unsigned long long)btrfs_root_ref_sequence(leaf, ref),
457 namelen, namebuf);
460 static int count_bytes(void *buf, int len, char b)
462 int cnt = 0;
463 int i;
464 for (i = 0; i < len; i++) {
465 if (((char*)buf)[i] == b)
466 cnt++;
468 return cnt;
471 static void print_root(struct extent_buffer *leaf, int slot)
473 struct btrfs_root_item *ri;
474 struct btrfs_root_item root_item;
475 int len;
476 char uuid_str[BTRFS_UUID_UNPARSED_SIZE];
478 ri = btrfs_item_ptr(leaf, slot, struct btrfs_root_item);
479 len = btrfs_item_size_nr(leaf, slot);
481 memset(&root_item, 0, sizeof(root_item));
482 read_extent_buffer(leaf, &root_item, (unsigned long)ri, len);
484 printf("\t\troot data bytenr %llu level %d dirid %llu refs %u gen %llu lastsnap %llu\n",
485 (unsigned long long)btrfs_root_bytenr(&root_item),
486 btrfs_root_level(&root_item),
487 (unsigned long long)btrfs_root_dirid(&root_item),
488 btrfs_root_refs(&root_item),
489 (unsigned long long)btrfs_root_generation(&root_item),
490 (unsigned long long)btrfs_root_last_snapshot(&root_item));
492 if (root_item.generation == root_item.generation_v2) {
493 uuid_unparse(root_item.uuid, uuid_str);
494 printf("\t\tuuid %s\n", uuid_str);
495 if (count_bytes(root_item.parent_uuid, BTRFS_UUID_SIZE, 0) != BTRFS_UUID_SIZE) {
496 uuid_unparse(root_item.parent_uuid, uuid_str);
497 printf("\t\tparent_uuid %s\n", uuid_str);
499 if (count_bytes(root_item.received_uuid, BTRFS_UUID_SIZE, 0) != BTRFS_UUID_SIZE) {
500 uuid_unparse(root_item.received_uuid, uuid_str);
501 printf("\t\treceived_uuid %s\n", uuid_str);
503 if (root_item.ctransid) {
504 printf("\t\tctransid %llu otransid %llu stransid %llu rtransid %llu\n",
505 btrfs_root_ctransid(&root_item),
506 btrfs_root_otransid(&root_item),
507 btrfs_root_stransid(&root_item),
508 btrfs_root_rtransid(&root_item));
511 if (btrfs_root_refs(&root_item) == 0) {
512 struct btrfs_key drop_key;
513 btrfs_disk_key_to_cpu(&drop_key,
514 &root_item.drop_progress);
515 printf("\t\tdrop ");
516 btrfs_print_key(&root_item.drop_progress);
517 printf(" level %d\n", root_item.drop_level);
521 static void print_free_space_header(struct extent_buffer *leaf, int slot)
523 struct btrfs_free_space_header *header;
524 struct btrfs_disk_key location;
526 header = btrfs_item_ptr(leaf, slot, struct btrfs_free_space_header);
527 btrfs_free_space_key(leaf, header, &location);
528 printf("\t\tlocation ");
529 btrfs_print_key(&location);
530 printf("\n");
531 printf("\t\tcache generation %llu entries %llu bitmaps %llu\n",
532 (unsigned long long)btrfs_free_space_generation(leaf, header),
533 (unsigned long long)btrfs_free_space_entries(leaf, header),
534 (unsigned long long)btrfs_free_space_bitmaps(leaf, header));
537 static void print_key_type(u64 objectid, u8 type)
539 if (type == 0 && objectid == BTRFS_FREE_SPACE_OBJECTID) {
540 printf("UNTYPED");
541 return;
544 switch (type) {
545 case BTRFS_INODE_ITEM_KEY:
546 printf("INODE_ITEM");
547 break;
548 case BTRFS_INODE_REF_KEY:
549 printf("INODE_REF");
550 break;
551 case BTRFS_INODE_EXTREF_KEY:
552 printf("INODE_EXTREF");
553 break;
554 case BTRFS_DIR_ITEM_KEY:
555 printf("DIR_ITEM");
556 break;
557 case BTRFS_DIR_INDEX_KEY:
558 printf("DIR_INDEX");
559 break;
560 case BTRFS_DIR_LOG_ITEM_KEY:
561 printf("DIR_LOG_ITEM");
562 break;
563 case BTRFS_DIR_LOG_INDEX_KEY:
564 printf("DIR_LOG_INDEX");
565 break;
566 case BTRFS_XATTR_ITEM_KEY:
567 printf("XATTR_ITEM");
568 break;
569 case BTRFS_ORPHAN_ITEM_KEY:
570 printf("ORPHAN_ITEM");
571 break;
572 case BTRFS_ROOT_ITEM_KEY:
573 printf("ROOT_ITEM");
574 break;
575 case BTRFS_ROOT_REF_KEY:
576 printf("ROOT_REF");
577 break;
578 case BTRFS_ROOT_BACKREF_KEY:
579 printf("ROOT_BACKREF");
580 break;
581 case BTRFS_EXTENT_ITEM_KEY:
582 printf("EXTENT_ITEM");
583 break;
584 case BTRFS_METADATA_ITEM_KEY:
585 printf("METADATA_ITEM");
586 break;
587 case BTRFS_TREE_BLOCK_REF_KEY:
588 printf("TREE_BLOCK_REF");
589 break;
590 case BTRFS_SHARED_BLOCK_REF_KEY:
591 printf("SHARED_BLOCK_REF");
592 break;
593 case BTRFS_EXTENT_DATA_REF_KEY:
594 printf("EXTENT_DATA_REF");
595 break;
596 case BTRFS_SHARED_DATA_REF_KEY:
597 printf("SHARED_DATA_REF");
598 break;
599 case BTRFS_EXTENT_REF_V0_KEY:
600 printf("EXTENT_REF_V0");
601 break;
602 case BTRFS_CSUM_ITEM_KEY:
603 printf("CSUM_ITEM");
604 break;
605 case BTRFS_EXTENT_CSUM_KEY:
606 printf("EXTENT_CSUM");
607 break;
608 case BTRFS_EXTENT_DATA_KEY:
609 printf("EXTENT_DATA");
610 break;
611 case BTRFS_BLOCK_GROUP_ITEM_KEY:
612 printf("BLOCK_GROUP_ITEM");
613 break;
614 case BTRFS_CHUNK_ITEM_KEY:
615 printf("CHUNK_ITEM");
616 break;
617 case BTRFS_DEV_ITEM_KEY:
618 printf("DEV_ITEM");
619 break;
620 case BTRFS_DEV_EXTENT_KEY:
621 printf("DEV_EXTENT");
622 break;
623 case BTRFS_BALANCE_ITEM_KEY:
624 printf("BALANCE_ITEM");
625 break;
626 case BTRFS_DEV_REPLACE_KEY:
627 printf("DEV_REPLACE");
628 break;
629 case BTRFS_STRING_ITEM_KEY:
630 printf("STRING_ITEM");
631 break;
632 case BTRFS_QGROUP_STATUS_KEY:
633 printf("QGROUP_STATUS");
634 break;
635 case BTRFS_QGROUP_RELATION_KEY:
636 printf("QGROUP_RELATION");
637 break;
638 case BTRFS_QGROUP_INFO_KEY:
639 printf("QGROUP_INFO");
640 break;
641 case BTRFS_QGROUP_LIMIT_KEY:
642 printf("QGROUP_LIMIT");
643 break;
644 case BTRFS_DEV_STATS_KEY:
645 printf("DEV_STATS");
646 break;
647 case BTRFS_UUID_KEY_SUBVOL:
648 printf("UUID_KEY_SUBVOL");
649 break;
650 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
651 printf("UUID_KEY_RECEIVED_SUBVOL");
652 break;
653 default:
654 printf("UNKNOWN.%d", type);
658 static void print_objectid(u64 objectid, u8 type)
660 switch (type) {
661 case BTRFS_DEV_EXTENT_KEY:
662 printf("%llu", (unsigned long long)objectid); /* device id */
663 return;
664 case BTRFS_QGROUP_RELATION_KEY:
665 printf("%llu/%llu", btrfs_qgroup_level(objectid),
666 btrfs_qgroup_subvid(objectid));
667 return;
668 case BTRFS_UUID_KEY_SUBVOL:
669 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
670 printf("0x%016llx", (unsigned long long)objectid);
671 return;
674 switch (objectid) {
675 case BTRFS_ROOT_TREE_OBJECTID:
676 if (type == BTRFS_DEV_ITEM_KEY)
677 printf("DEV_ITEMS");
678 else
679 printf("ROOT_TREE");
680 break;
681 case BTRFS_EXTENT_TREE_OBJECTID:
682 printf("EXTENT_TREE");
683 break;
684 case BTRFS_CHUNK_TREE_OBJECTID:
685 printf("CHUNK_TREE");
686 break;
687 case BTRFS_DEV_TREE_OBJECTID:
688 printf("DEV_TREE");
689 break;
690 case BTRFS_FS_TREE_OBJECTID:
691 printf("FS_TREE");
692 break;
693 case BTRFS_ROOT_TREE_DIR_OBJECTID:
694 printf("ROOT_TREE_DIR");
695 break;
696 case BTRFS_CSUM_TREE_OBJECTID:
697 printf("CSUM_TREE");
698 break;
699 case BTRFS_BALANCE_OBJECTID:
700 printf("BALANCE");
701 break;
702 case BTRFS_ORPHAN_OBJECTID:
703 printf("ORPHAN");
704 break;
705 case BTRFS_TREE_LOG_OBJECTID:
706 printf("TREE_LOG");
707 break;
708 case BTRFS_TREE_LOG_FIXUP_OBJECTID:
709 printf("LOG_FIXUP");
710 break;
711 case BTRFS_TREE_RELOC_OBJECTID:
712 printf("TREE_RELOC");
713 break;
714 case BTRFS_DATA_RELOC_TREE_OBJECTID:
715 printf("DATA_RELOC_TREE");
716 break;
717 case BTRFS_EXTENT_CSUM_OBJECTID:
718 printf("EXTENT_CSUM");
719 break;
720 case BTRFS_FREE_SPACE_OBJECTID:
721 printf("FREE_SPACE");
722 break;
723 case BTRFS_FREE_INO_OBJECTID:
724 printf("FREE_INO");
725 break;
726 case BTRFS_QUOTA_TREE_OBJECTID:
727 printf("QUOTA_TREE");
728 break;
729 case BTRFS_UUID_TREE_OBJECTID:
730 printf("UUID_TREE");
731 break;
732 case BTRFS_MULTIPLE_OBJECTIDS:
733 printf("MULTIPLE");
734 break;
735 case (u64)-1:
736 printf("-1");
737 break;
738 case BTRFS_FIRST_CHUNK_TREE_OBJECTID:
739 if (type == BTRFS_CHUNK_ITEM_KEY) {
740 printf("FIRST_CHUNK_TREE");
741 break;
743 /* fall-thru */
744 default:
745 printf("%llu", (unsigned long long)objectid);
749 void btrfs_print_key(struct btrfs_disk_key *disk_key)
751 u64 objectid = btrfs_disk_key_objectid(disk_key);
752 u8 type = btrfs_disk_key_type(disk_key);
753 u64 offset = btrfs_disk_key_offset(disk_key);
755 printf("key (");
756 print_objectid(objectid, type);
757 printf(" ");
758 print_key_type(objectid, type);
759 switch (type) {
760 case BTRFS_QGROUP_RELATION_KEY:
761 case BTRFS_QGROUP_INFO_KEY:
762 case BTRFS_QGROUP_LIMIT_KEY:
763 printf(" %llu/%llu)", btrfs_qgroup_level(offset),
764 btrfs_qgroup_subvid(offset));
765 break;
766 case BTRFS_UUID_KEY_SUBVOL:
767 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
768 printf(" 0x%016llx)", (unsigned long long)offset);
769 break;
770 default:
771 if (offset == (u64)-1)
772 printf(" -1)");
773 else
774 printf(" %llu)", (unsigned long long)offset);
775 break;
779 static void print_uuid_item(struct extent_buffer *l, unsigned long offset,
780 u32 item_size)
782 if (item_size & (sizeof(u64) - 1)) {
783 printf("btrfs: uuid item with illegal size %lu!\n",
784 (unsigned long)item_size);
785 return;
787 while (item_size) {
788 __le64 subvol_id;
790 read_extent_buffer(l, &subvol_id, offset, sizeof(u64));
791 printf("\t\tsubvol_id %llu\n",
792 (unsigned long long)le64_to_cpu(subvol_id));
793 item_size -= sizeof(u64);
794 offset += sizeof(u64);
798 void btrfs_print_leaf(struct btrfs_root *root, struct extent_buffer *l)
800 int i;
801 char *str;
802 struct btrfs_item *item;
803 struct btrfs_dir_item *di;
804 struct btrfs_inode_item *ii;
805 struct btrfs_file_extent_item *fi;
806 struct btrfs_block_group_item *bi;
807 struct btrfs_extent_data_ref *dref;
808 struct btrfs_shared_data_ref *sref;
809 struct btrfs_inode_ref *iref;
810 struct btrfs_inode_extref *iref2;
811 struct btrfs_dev_extent *dev_extent;
812 struct btrfs_disk_key disk_key;
813 struct btrfs_block_group_item bg_item;
814 struct btrfs_dir_log_item *dlog;
815 struct btrfs_qgroup_info_item *qg_info;
816 struct btrfs_qgroup_limit_item *qg_limit;
817 struct btrfs_qgroup_status_item *qg_status;
818 u32 nr = btrfs_header_nritems(l);
819 u64 objectid;
820 u32 type;
821 char flags_str[32];
823 printf("leaf %llu items %d free space %d generation %llu owner %llu\n",
824 (unsigned long long)btrfs_header_bytenr(l), nr,
825 btrfs_leaf_free_space(root, l),
826 (unsigned long long)btrfs_header_generation(l),
827 (unsigned long long)btrfs_header_owner(l));
828 print_uuids(l);
829 fflush(stdout);
830 for (i = 0 ; i < nr ; i++) {
831 item = btrfs_item_nr(i);
832 btrfs_item_key(l, &disk_key, i);
833 objectid = btrfs_disk_key_objectid(&disk_key);
834 type = btrfs_disk_key_type(&disk_key);
835 printf("\titem %d ", i);
836 btrfs_print_key(&disk_key);
837 printf(" itemoff %d itemsize %d\n",
838 btrfs_item_offset(l, item),
839 btrfs_item_size(l, item));
841 if (type == 0 && objectid == BTRFS_FREE_SPACE_OBJECTID)
842 print_free_space_header(l, i);
844 switch (type) {
845 case BTRFS_INODE_ITEM_KEY:
846 ii = btrfs_item_ptr(l, i, struct btrfs_inode_item);
847 printf("\t\tinode generation %llu transid %llu size %llu nbytes %llu\n"
848 "\t\tblock group %llu mode %o links %u uid %u gid %u\n"
849 "\t\trdev %llu flags 0x%llx\n",
850 (unsigned long long)btrfs_inode_generation(l, ii),
851 (unsigned long long)btrfs_inode_transid(l, ii),
852 (unsigned long long)btrfs_inode_size(l, ii),
853 (unsigned long long)btrfs_inode_nbytes(l, ii),
854 (unsigned long long)btrfs_inode_block_group(l,ii),
855 btrfs_inode_mode(l, ii),
856 btrfs_inode_nlink(l, ii),
857 btrfs_inode_uid(l, ii),
858 btrfs_inode_gid(l, ii),
859 (unsigned long long)btrfs_inode_rdev(l,ii),
860 (unsigned long long)btrfs_inode_flags(l,ii));
861 break;
862 case BTRFS_INODE_REF_KEY:
863 iref = btrfs_item_ptr(l, i, struct btrfs_inode_ref);
864 print_inode_ref_item(l, item, iref);
865 break;
866 case BTRFS_INODE_EXTREF_KEY:
867 iref2 = btrfs_item_ptr(l, i, struct btrfs_inode_extref);
868 print_inode_extref_item(l, item, iref2);
869 break;
870 case BTRFS_DIR_ITEM_KEY:
871 case BTRFS_DIR_INDEX_KEY:
872 case BTRFS_XATTR_ITEM_KEY:
873 di = btrfs_item_ptr(l, i, struct btrfs_dir_item);
874 print_dir_item(l, item, di);
875 break;
876 case BTRFS_DIR_LOG_INDEX_KEY:
877 case BTRFS_DIR_LOG_ITEM_KEY:
878 dlog = btrfs_item_ptr(l, i, struct btrfs_dir_log_item);
879 printf("\t\tdir log end %Lu\n",
880 (unsigned long long)btrfs_dir_log_end(l, dlog));
881 break;
882 case BTRFS_ORPHAN_ITEM_KEY:
883 printf("\t\torphan item\n");
884 break;
885 case BTRFS_ROOT_ITEM_KEY:
886 print_root(l, i);
887 break;
888 case BTRFS_ROOT_REF_KEY:
889 print_root_ref(l, i, "ref");
890 break;
891 case BTRFS_ROOT_BACKREF_KEY:
892 print_root_ref(l, i, "backref");
893 break;
894 case BTRFS_EXTENT_ITEM_KEY:
895 print_extent_item(l, i, 0);
896 break;
897 case BTRFS_METADATA_ITEM_KEY:
898 print_extent_item(l, i, 1);
899 break;
900 case BTRFS_TREE_BLOCK_REF_KEY:
901 printf("\t\ttree block backref\n");
902 break;
903 case BTRFS_SHARED_BLOCK_REF_KEY:
904 printf("\t\tshared block backref\n");
905 break;
906 case BTRFS_EXTENT_DATA_REF_KEY:
907 dref = btrfs_item_ptr(l, i, struct btrfs_extent_data_ref);
908 printf("\t\textent data backref root %llu "
909 "objectid %llu offset %llu count %u\n",
910 (unsigned long long)btrfs_extent_data_ref_root(l, dref),
911 (unsigned long long)btrfs_extent_data_ref_objectid(l, dref),
912 (unsigned long long)btrfs_extent_data_ref_offset(l, dref),
913 btrfs_extent_data_ref_count(l, dref));
914 break;
915 case BTRFS_SHARED_DATA_REF_KEY:
916 sref = btrfs_item_ptr(l, i, struct btrfs_shared_data_ref);
917 printf("\t\tshared data backref count %u\n",
918 btrfs_shared_data_ref_count(l, sref));
919 break;
920 case BTRFS_EXTENT_REF_V0_KEY:
921 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
922 print_extent_ref_v0(l, i);
923 #else
924 BUG();
925 #endif
926 break;
927 case BTRFS_CSUM_ITEM_KEY:
928 printf("\t\tcsum item\n");
929 break;
930 case BTRFS_EXTENT_CSUM_KEY:
931 printf("\t\textent csum item\n");
932 break;
933 case BTRFS_EXTENT_DATA_KEY:
934 fi = btrfs_item_ptr(l, i,
935 struct btrfs_file_extent_item);
936 print_file_extent_item(l, item, i, fi);
937 break;
938 case BTRFS_BLOCK_GROUP_ITEM_KEY:
939 bi = btrfs_item_ptr(l, i,
940 struct btrfs_block_group_item);
941 read_extent_buffer(l, &bg_item, (unsigned long)bi,
942 sizeof(bg_item));
943 memset(flags_str, 0, sizeof(flags_str));
944 bg_flags_to_str(btrfs_block_group_flags(&bg_item),
945 flags_str);
946 printf("\t\tblock group used %llu chunk_objectid %llu flags %s\n",
947 (unsigned long long)btrfs_block_group_used(&bg_item),
948 (unsigned long long)btrfs_block_group_chunk_objectid(&bg_item),
949 flags_str);
950 break;
951 case BTRFS_CHUNK_ITEM_KEY:
952 print_chunk(l, btrfs_item_ptr(l, i, struct btrfs_chunk));
953 break;
954 case BTRFS_DEV_ITEM_KEY:
955 print_dev_item(l, btrfs_item_ptr(l, i,
956 struct btrfs_dev_item));
957 break;
958 case BTRFS_DEV_EXTENT_KEY:
959 dev_extent = btrfs_item_ptr(l, i,
960 struct btrfs_dev_extent);
961 printf("\t\tdev extent chunk_tree %llu\n"
962 "\t\tchunk objectid %llu chunk offset %llu "
963 "length %llu\n",
964 (unsigned long long)
965 btrfs_dev_extent_chunk_tree(l, dev_extent),
966 (unsigned long long)
967 btrfs_dev_extent_chunk_objectid(l, dev_extent),
968 (unsigned long long)
969 btrfs_dev_extent_chunk_offset(l, dev_extent),
970 (unsigned long long)
971 btrfs_dev_extent_length(l, dev_extent));
972 break;
973 case BTRFS_QGROUP_STATUS_KEY:
974 qg_status = btrfs_item_ptr(l, i,
975 struct btrfs_qgroup_status_item);
976 memset(flags_str, 0, sizeof(flags_str));
977 qgroup_flags_to_str(btrfs_qgroup_status_flags(l, qg_status),
978 flags_str);
979 printf("\t\tversion %llu generation %llu flags %s "
980 "scan %lld\n",
981 (unsigned long long)
982 btrfs_qgroup_status_version(l, qg_status),
983 (unsigned long long)
984 btrfs_qgroup_status_generation(l, qg_status),
985 flags_str,
986 (unsigned long long)
987 btrfs_qgroup_status_scan(l, qg_status));
988 break;
989 case BTRFS_QGROUP_RELATION_KEY:
990 break;
991 case BTRFS_QGROUP_INFO_KEY:
992 qg_info = btrfs_item_ptr(l, i,
993 struct btrfs_qgroup_info_item);
994 printf("\t\tgeneration %llu\n"
995 "\t\treferenced %llu referenced compressed %llu\n"
996 "\t\texclusive %llu exclusive compressed %llu\n",
997 (unsigned long long)
998 btrfs_qgroup_info_generation(l, qg_info),
999 (unsigned long long)
1000 btrfs_qgroup_info_referenced(l, qg_info),
1001 (unsigned long long)
1002 btrfs_qgroup_info_referenced_compressed(l,
1003 qg_info),
1004 (unsigned long long)
1005 btrfs_qgroup_info_exclusive(l, qg_info),
1006 (unsigned long long)
1007 btrfs_qgroup_info_exclusive_compressed(l,
1008 qg_info));
1009 break;
1010 case BTRFS_QGROUP_LIMIT_KEY:
1011 qg_limit = btrfs_item_ptr(l, i,
1012 struct btrfs_qgroup_limit_item);
1013 printf("\t\tflags %llx\n"
1014 "\t\tmax referenced %lld max exclusive %lld\n"
1015 "\t\trsv referenced %lld rsv exclusive %lld\n",
1016 (unsigned long long)
1017 btrfs_qgroup_limit_flags(l, qg_limit),
1018 (long long)
1019 btrfs_qgroup_limit_max_referenced(l, qg_limit),
1020 (long long)
1021 btrfs_qgroup_limit_max_exclusive(l, qg_limit),
1022 (long long)
1023 btrfs_qgroup_limit_rsv_referenced(l, qg_limit),
1024 (long long)
1025 btrfs_qgroup_limit_rsv_exclusive(l, qg_limit));
1026 break;
1027 case BTRFS_UUID_KEY_SUBVOL:
1028 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
1029 print_uuid_item(l, btrfs_item_ptr_offset(l, i),
1030 btrfs_item_size_nr(l, i));
1031 break;
1032 case BTRFS_STRING_ITEM_KEY:
1033 /* dirty, but it's simple */
1034 str = l->data + btrfs_item_ptr_offset(l, i);
1035 printf("\t\titem data %.*s\n", btrfs_item_size(l, item), str);
1036 break;
1037 case BTRFS_DEV_STATS_KEY:
1038 printf("\t\tdevice stats\n");
1039 break;
1041 fflush(stdout);
1045 void btrfs_print_tree(struct btrfs_root *root, struct extent_buffer *eb, int follow)
1047 int i;
1048 u32 nr;
1049 u32 size;
1050 struct btrfs_disk_key disk_key;
1051 struct btrfs_key key;
1053 if (!eb)
1054 return;
1055 nr = btrfs_header_nritems(eb);
1056 if (btrfs_is_leaf(eb)) {
1057 btrfs_print_leaf(root, eb);
1058 return;
1060 printf("node %llu level %d items %d free %u generation %llu owner %llu\n",
1061 (unsigned long long)eb->start,
1062 btrfs_header_level(eb), nr,
1063 (u32)BTRFS_NODEPTRS_PER_BLOCK(root) - nr,
1064 (unsigned long long)btrfs_header_generation(eb),
1065 (unsigned long long)btrfs_header_owner(eb));
1066 print_uuids(eb);
1067 fflush(stdout);
1068 size = btrfs_level_size(root, btrfs_header_level(eb) - 1);
1069 for (i = 0; i < nr; i++) {
1070 u64 blocknr = btrfs_node_blockptr(eb, i);
1071 btrfs_node_key(eb, &disk_key, i);
1072 btrfs_disk_key_to_cpu(&key, &disk_key);
1073 printf("\t");
1074 btrfs_print_key(&disk_key);
1075 printf(" block %llu (%llu) gen %llu\n",
1076 (unsigned long long)blocknr,
1077 (unsigned long long)blocknr / size,
1078 (unsigned long long)btrfs_node_ptr_generation(eb, i));
1079 fflush(stdout);
1081 if (!follow)
1082 return;
1084 for (i = 0; i < nr; i++) {
1085 struct extent_buffer *next = read_tree_block(root,
1086 btrfs_node_blockptr(eb, i),
1087 size,
1088 btrfs_node_ptr_generation(eb, i));
1089 if (!extent_buffer_uptodate(next)) {
1090 fprintf(stderr, "failed to read %llu in tree %llu\n",
1091 (unsigned long long)btrfs_node_blockptr(eb, i),
1092 (unsigned long long)btrfs_header_owner(eb));
1093 continue;
1095 if (btrfs_is_leaf(next) &&
1096 btrfs_header_level(eb) != 1)
1097 BUG();
1098 if (btrfs_header_level(next) !=
1099 btrfs_header_level(eb) - 1)
1100 BUG();
1101 btrfs_print_tree(root, next, 1);
1102 free_extent_buffer(next);