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.
21 #include <uuid/uuid.h>
22 #include "kerncompat.h"
23 #include "radix-tree.h"
26 #include "print-tree.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);
36 case BTRFS_FT_REG_FILE:
54 case BTRFS_FT_SYMLINK:
65 static int print_dir_item(struct extent_buffer *eb, struct btrfs_item *item,
66 struct btrfs_dir_item *di)
73 char namebuf[BTRFS_NAME_LEN];
74 struct btrfs_disk_key location;
76 total = btrfs_item_size(eb, item);
78 btrfs_dir_item_key(eb, di, &location);
79 printf("\t\tlocation ");
80 btrfs_print_key(&location);
82 print_dir_item_type(eb, di);
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);
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);
103 static int print_inode_extref_item(struct extent_buffer *eb,
104 struct btrfs_item *item,
105 struct btrfs_inode_extref *extref)
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 "
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);
139 static int print_inode_ref_item(struct extent_buffer *eb, struct btrfs_item *item,
140 struct btrfs_inode_ref *ref)
147 char namebuf[BTRFS_NAME_LEN];
148 total = btrfs_item_size(eb, item);
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);
163 /* Caller should ensure sizeof(*ret)>=21 "DATA|METADATA|RAID10" */
164 static void bg_flags_to_str(u64 flags, char *ret)
168 if (flags & BTRFS_BLOCK_GROUP_DATA) {
172 if (flags & BTRFS_BLOCK_GROUP_METADATA) {
175 strcat(ret, "METADATA");
177 if (flags & BTRFS_BLOCK_GROUP_SYSTEM) {
180 strcat(ret, "SYSTEM");
182 switch (flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
183 case BTRFS_BLOCK_GROUP_RAID0:
184 strcat(ret, "|RAID0");
186 case BTRFS_BLOCK_GROUP_RAID1:
187 strcat(ret, "|RAID1");
189 case BTRFS_BLOCK_GROUP_DUP:
192 case BTRFS_BLOCK_GROUP_RAID10:
193 strcat(ret, "|RAID10");
195 case BTRFS_BLOCK_GROUP_RAID5:
196 strcat(ret, "|RAID5");
198 case BTRFS_BLOCK_GROUP_RAID6:
199 strcat(ret, "|RAID6");
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)
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);
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 unsigned char dev_uuid[BTRFS_UUID_SIZE];
235 char str_dev_uuid[BTRFS_UUID_UNPARSED_SIZE];
237 read_extent_buffer(eb, dev_uuid,
238 (unsigned long)btrfs_stripe_dev_uuid_nr(chunk, i),
240 uuid_unparse(dev_uuid, str_dev_uuid);
241 printf("\t\t\tstripe %d devid %llu offset %llu\n", i,
242 (unsigned long long)btrfs_stripe_devid_nr(eb, chunk, i),
243 (unsigned long long)btrfs_stripe_offset_nr(eb, chunk, i));
244 printf("\t\t\tdev uuid: %s\n", str_dev_uuid);
248 static void print_dev_item(struct extent_buffer *eb,
249 struct btrfs_dev_item *dev_item)
251 char disk_uuid_c[BTRFS_UUID_UNPARSED_SIZE];
252 u8 disk_uuid[BTRFS_UUID_SIZE];
254 read_extent_buffer(eb, disk_uuid,
255 (unsigned long)btrfs_device_uuid(dev_item),
257 uuid_unparse(disk_uuid, disk_uuid_c);
258 printf("\t\tdev item devid %llu "
259 "total_bytes %llu bytes used %Lu\n"
261 (unsigned long long)btrfs_device_id(eb, dev_item),
262 (unsigned long long)btrfs_device_total_bytes(eb, dev_item),
263 (unsigned long long)btrfs_device_bytes_used(eb, dev_item),
267 static void print_uuids(struct extent_buffer *eb)
269 char fs_uuid[BTRFS_UUID_UNPARSED_SIZE];
270 char chunk_uuid[BTRFS_UUID_UNPARSED_SIZE];
271 u8 disk_uuid[BTRFS_UUID_SIZE];
273 read_extent_buffer(eb, disk_uuid, btrfs_header_fsid(),
276 fs_uuid[BTRFS_UUID_UNPARSED_SIZE - 1] = '\0';
277 uuid_unparse(disk_uuid, fs_uuid);
279 read_extent_buffer(eb, disk_uuid,
280 btrfs_header_chunk_tree_uuid(eb),
283 chunk_uuid[BTRFS_UUID_UNPARSED_SIZE - 1] = '\0';
284 uuid_unparse(disk_uuid, chunk_uuid);
285 printf("fs uuid %s\nchunk uuid %s\n", fs_uuid, chunk_uuid);
288 static void compress_type_to_str(u8 compress_type, char *ret)
290 switch (compress_type) {
291 case BTRFS_COMPRESS_NONE:
294 case BTRFS_COMPRESS_ZLIB:
297 case BTRFS_COMPRESS_LZO:
301 sprintf(ret, "UNKNOWN.%d", compress_type);
305 static void print_file_extent_item(struct extent_buffer *eb,
306 struct btrfs_item *item,
308 struct btrfs_file_extent_item *fi)
310 int extent_type = btrfs_file_extent_type(eb, fi);
311 char compress_str[16];
313 compress_type_to_str(btrfs_file_extent_compression(eb, fi),
316 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
317 printf("\t\tinline extent data size %u "
318 "ram %u compress(%s)\n",
319 btrfs_file_extent_inline_item_len(eb, item),
320 btrfs_file_extent_inline_len(eb, slot, fi),
324 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
325 printf("\t\tprealloc data disk byte %llu nr %llu\n",
326 (unsigned long long)btrfs_file_extent_disk_bytenr(eb, fi),
327 (unsigned long long)btrfs_file_extent_disk_num_bytes(eb, fi));
328 printf("\t\tprealloc data offset %llu nr %llu\n",
329 (unsigned long long)btrfs_file_extent_offset(eb, fi),
330 (unsigned long long)btrfs_file_extent_num_bytes(eb, fi));
333 printf("\t\textent data disk byte %llu nr %llu\n",
334 (unsigned long long)btrfs_file_extent_disk_bytenr(eb, fi),
335 (unsigned long long)btrfs_file_extent_disk_num_bytes(eb, fi));
336 printf("\t\textent data offset %llu nr %llu ram %llu\n",
337 (unsigned long long)btrfs_file_extent_offset(eb, fi),
338 (unsigned long long)btrfs_file_extent_num_bytes(eb, fi),
339 (unsigned long long)btrfs_file_extent_ram_bytes(eb, fi));
340 printf("\t\textent compression(%s)\n", compress_str);
343 /* Caller should ensure sizeof(*ret) >= 16("DATA|TREE_BLOCK") */
344 static void extent_flags_to_str(u64 flags, char *ret)
348 if (flags & BTRFS_EXTENT_FLAG_DATA) {
352 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
357 strcat(ret, "TREE_BLOCK");
359 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
361 strcat(ret, "FULL_BACKREF");
365 void print_extent_item(struct extent_buffer *eb, int slot, int metadata)
367 struct btrfs_extent_item *ei;
368 struct btrfs_extent_inline_ref *iref;
369 struct btrfs_extent_data_ref *dref;
370 struct btrfs_shared_data_ref *sref;
371 struct btrfs_disk_key key;
375 u32 item_size = btrfs_item_size_nr(eb, slot);
378 char flags_str[32] = {0};
380 if (item_size < sizeof(*ei)) {
381 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
382 struct btrfs_extent_item_v0 *ei0;
383 BUG_ON(item_size != sizeof(*ei0));
384 ei0 = btrfs_item_ptr(eb, slot, struct btrfs_extent_item_v0);
385 printf("\t\textent refs %u\n",
386 btrfs_extent_refs_v0(eb, ei0));
393 ei = btrfs_item_ptr(eb, slot, struct btrfs_extent_item);
394 flags = btrfs_extent_flags(eb, ei);
395 extent_flags_to_str(flags, flags_str);
397 printf("\t\textent refs %llu gen %llu flags %s\n",
398 (unsigned long long)btrfs_extent_refs(eb, ei),
399 (unsigned long long)btrfs_extent_generation(eb, ei),
402 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !metadata) {
403 struct btrfs_tree_block_info *info;
404 info = (struct btrfs_tree_block_info *)(ei + 1);
405 btrfs_tree_block_key(eb, info, &key);
406 printf("\t\ttree block ");
407 btrfs_print_key(&key);
408 printf(" level %d\n", btrfs_tree_block_level(eb, info));
409 iref = (struct btrfs_extent_inline_ref *)(info + 1);
410 } else if (metadata) {
411 struct btrfs_key tmp;
413 btrfs_item_key_to_cpu(eb, &tmp, slot);
414 printf("\t\ttree block skinny level %d\n", (int)tmp.offset);
415 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
417 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
420 ptr = (unsigned long)iref;
421 end = (unsigned long)ei + item_size;
423 iref = (struct btrfs_extent_inline_ref *)ptr;
424 type = btrfs_extent_inline_ref_type(eb, iref);
425 offset = btrfs_extent_inline_ref_offset(eb, iref);
427 case BTRFS_TREE_BLOCK_REF_KEY:
428 printf("\t\ttree block backref root %llu\n",
429 (unsigned long long)offset);
431 case BTRFS_SHARED_BLOCK_REF_KEY:
432 printf("\t\tshared block backref parent %llu\n",
433 (unsigned long long)offset);
435 case BTRFS_EXTENT_DATA_REF_KEY:
436 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
437 printf("\t\textent data backref root %llu "
438 "objectid %llu offset %llu count %u\n",
439 (unsigned long long)btrfs_extent_data_ref_root(eb, dref),
440 (unsigned long long)btrfs_extent_data_ref_objectid(eb, dref),
441 (unsigned long long)btrfs_extent_data_ref_offset(eb, dref),
442 btrfs_extent_data_ref_count(eb, dref));
444 case BTRFS_SHARED_DATA_REF_KEY:
445 sref = (struct btrfs_shared_data_ref *)(iref + 1);
446 printf("\t\tshared data backref parent %llu count %u\n",
447 (unsigned long long)offset,
448 btrfs_shared_data_ref_count(eb, sref));
453 ptr += btrfs_extent_inline_ref_size(type);
458 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
459 static void print_extent_ref_v0(struct extent_buffer *eb, int slot)
461 struct btrfs_extent_ref_v0 *ref0;
463 ref0 = btrfs_item_ptr(eb, slot, struct btrfs_extent_ref_v0);
464 printf("\t\textent back ref root %llu gen %llu "
465 "owner %llu num_refs %lu\n",
466 (unsigned long long)btrfs_ref_root_v0(eb, ref0),
467 (unsigned long long)btrfs_ref_generation_v0(eb, ref0),
468 (unsigned long long)btrfs_ref_objectid_v0(eb, ref0),
469 (unsigned long)btrfs_ref_count_v0(eb, ref0));
473 static void print_root_ref(struct extent_buffer *leaf, int slot, char *tag)
475 struct btrfs_root_ref *ref;
476 char namebuf[BTRFS_NAME_LEN];
479 ref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
480 namelen = btrfs_root_ref_name_len(leaf, ref);
481 read_extent_buffer(leaf, namebuf, (unsigned long)(ref + 1), namelen);
482 printf("\t\troot %s key dirid %llu sequence %llu name %.*s\n", tag,
483 (unsigned long long)btrfs_root_ref_dirid(leaf, ref),
484 (unsigned long long)btrfs_root_ref_sequence(leaf, ref),
488 static int count_bytes(void *buf, int len, char b)
492 for (i = 0; i < len; i++) {
493 if (((char*)buf)[i] == b)
499 static void print_root(struct extent_buffer *leaf, int slot)
501 struct btrfs_root_item *ri;
502 struct btrfs_root_item root_item;
504 char uuid_str[BTRFS_UUID_UNPARSED_SIZE];
506 ri = btrfs_item_ptr(leaf, slot, struct btrfs_root_item);
507 len = btrfs_item_size_nr(leaf, slot);
509 memset(&root_item, 0, sizeof(root_item));
510 read_extent_buffer(leaf, &root_item, (unsigned long)ri, len);
512 printf("\t\troot data bytenr %llu level %d dirid %llu refs %u gen %llu lastsnap %llu\n",
513 (unsigned long long)btrfs_root_bytenr(&root_item),
514 btrfs_root_level(&root_item),
515 (unsigned long long)btrfs_root_dirid(&root_item),
516 btrfs_root_refs(&root_item),
517 (unsigned long long)btrfs_root_generation(&root_item),
518 (unsigned long long)btrfs_root_last_snapshot(&root_item));
520 if (root_item.generation == root_item.generation_v2) {
521 uuid_unparse(root_item.uuid, uuid_str);
522 printf("\t\tuuid %s\n", uuid_str);
523 if (count_bytes(root_item.parent_uuid, BTRFS_UUID_SIZE, 0) != BTRFS_UUID_SIZE) {
524 uuid_unparse(root_item.parent_uuid, uuid_str);
525 printf("\t\tparent_uuid %s\n", uuid_str);
527 if (count_bytes(root_item.received_uuid, BTRFS_UUID_SIZE, 0) != BTRFS_UUID_SIZE) {
528 uuid_unparse(root_item.received_uuid, uuid_str);
529 printf("\t\treceived_uuid %s\n", uuid_str);
531 if (root_item.ctransid) {
532 printf("\t\tctransid %llu otransid %llu stransid %llu rtransid %llu\n",
533 btrfs_root_ctransid(&root_item),
534 btrfs_root_otransid(&root_item),
535 btrfs_root_stransid(&root_item),
536 btrfs_root_rtransid(&root_item));
539 if (btrfs_root_refs(&root_item) == 0) {
540 struct btrfs_key drop_key;
541 btrfs_disk_key_to_cpu(&drop_key,
542 &root_item.drop_progress);
544 btrfs_print_key(&root_item.drop_progress);
545 printf(" level %d\n", root_item.drop_level);
549 static void print_free_space_header(struct extent_buffer *leaf, int slot)
551 struct btrfs_free_space_header *header;
552 struct btrfs_disk_key location;
554 header = btrfs_item_ptr(leaf, slot, struct btrfs_free_space_header);
555 btrfs_free_space_key(leaf, header, &location);
556 printf("\t\tlocation ");
557 btrfs_print_key(&location);
559 printf("\t\tcache generation %llu entries %llu bitmaps %llu\n",
560 (unsigned long long)btrfs_free_space_generation(leaf, header),
561 (unsigned long long)btrfs_free_space_entries(leaf, header),
562 (unsigned long long)btrfs_free_space_bitmaps(leaf, header));
565 static void print_key_type(u64 objectid, u8 type)
567 if (type == 0 && objectid == BTRFS_FREE_SPACE_OBJECTID) {
573 case BTRFS_INODE_ITEM_KEY:
574 printf("INODE_ITEM");
576 case BTRFS_INODE_REF_KEY:
579 case BTRFS_INODE_EXTREF_KEY:
580 printf("INODE_EXTREF");
582 case BTRFS_DIR_ITEM_KEY:
585 case BTRFS_DIR_INDEX_KEY:
588 case BTRFS_DIR_LOG_ITEM_KEY:
589 printf("DIR_LOG_ITEM");
591 case BTRFS_DIR_LOG_INDEX_KEY:
592 printf("DIR_LOG_INDEX");
594 case BTRFS_XATTR_ITEM_KEY:
595 printf("XATTR_ITEM");
597 case BTRFS_ORPHAN_ITEM_KEY:
598 printf("ORPHAN_ITEM");
600 case BTRFS_ROOT_ITEM_KEY:
603 case BTRFS_ROOT_REF_KEY:
606 case BTRFS_ROOT_BACKREF_KEY:
607 printf("ROOT_BACKREF");
609 case BTRFS_EXTENT_ITEM_KEY:
610 printf("EXTENT_ITEM");
612 case BTRFS_METADATA_ITEM_KEY:
613 printf("METADATA_ITEM");
615 case BTRFS_TREE_BLOCK_REF_KEY:
616 printf("TREE_BLOCK_REF");
618 case BTRFS_SHARED_BLOCK_REF_KEY:
619 printf("SHARED_BLOCK_REF");
621 case BTRFS_EXTENT_DATA_REF_KEY:
622 printf("EXTENT_DATA_REF");
624 case BTRFS_SHARED_DATA_REF_KEY:
625 printf("SHARED_DATA_REF");
627 case BTRFS_EXTENT_REF_V0_KEY:
628 printf("EXTENT_REF_V0");
630 case BTRFS_CSUM_ITEM_KEY:
633 case BTRFS_EXTENT_CSUM_KEY:
634 printf("EXTENT_CSUM");
636 case BTRFS_EXTENT_DATA_KEY:
637 printf("EXTENT_DATA");
639 case BTRFS_BLOCK_GROUP_ITEM_KEY:
640 printf("BLOCK_GROUP_ITEM");
642 case BTRFS_FREE_SPACE_INFO_KEY:
643 printf("FREE_SPACE_INFO");
645 case BTRFS_FREE_SPACE_EXTENT_KEY:
646 printf("FREE_SPACE_EXTENT");
648 case BTRFS_FREE_SPACE_BITMAP_KEY:
649 printf("FREE_SPACE_BITMAP");
651 case BTRFS_CHUNK_ITEM_KEY:
652 printf("CHUNK_ITEM");
654 case BTRFS_DEV_ITEM_KEY:
657 case BTRFS_DEV_EXTENT_KEY:
658 printf("DEV_EXTENT");
660 case BTRFS_BALANCE_ITEM_KEY:
661 printf("BALANCE_ITEM");
663 case BTRFS_DEV_REPLACE_KEY:
664 printf("DEV_REPLACE");
666 case BTRFS_STRING_ITEM_KEY:
667 printf("STRING_ITEM");
669 case BTRFS_QGROUP_STATUS_KEY:
670 printf("QGROUP_STATUS");
672 case BTRFS_QGROUP_RELATION_KEY:
673 printf("QGROUP_RELATION");
675 case BTRFS_QGROUP_INFO_KEY:
676 printf("QGROUP_INFO");
678 case BTRFS_QGROUP_LIMIT_KEY:
679 printf("QGROUP_LIMIT");
681 case BTRFS_DEV_STATS_KEY:
684 case BTRFS_UUID_KEY_SUBVOL:
685 printf("UUID_KEY_SUBVOL");
687 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
688 printf("UUID_KEY_RECEIVED_SUBVOL");
691 printf("UNKNOWN.%d", type);
695 static void print_objectid(u64 objectid, u8 type)
698 case BTRFS_DEV_EXTENT_KEY:
699 printf("%llu", (unsigned long long)objectid); /* device id */
701 case BTRFS_QGROUP_RELATION_KEY:
702 printf("%llu/%llu", btrfs_qgroup_level(objectid),
703 btrfs_qgroup_subvid(objectid));
705 case BTRFS_UUID_KEY_SUBVOL:
706 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
707 printf("0x%016llx", (unsigned long long)objectid);
712 case BTRFS_ROOT_TREE_OBJECTID:
713 if (type == BTRFS_DEV_ITEM_KEY)
718 case BTRFS_EXTENT_TREE_OBJECTID:
719 printf("EXTENT_TREE");
721 case BTRFS_CHUNK_TREE_OBJECTID:
722 printf("CHUNK_TREE");
724 case BTRFS_DEV_TREE_OBJECTID:
727 case BTRFS_FS_TREE_OBJECTID:
730 case BTRFS_ROOT_TREE_DIR_OBJECTID:
731 printf("ROOT_TREE_DIR");
733 case BTRFS_CSUM_TREE_OBJECTID:
736 case BTRFS_BALANCE_OBJECTID:
739 case BTRFS_ORPHAN_OBJECTID:
742 case BTRFS_TREE_LOG_OBJECTID:
745 case BTRFS_TREE_LOG_FIXUP_OBJECTID:
748 case BTRFS_TREE_RELOC_OBJECTID:
749 printf("TREE_RELOC");
751 case BTRFS_DATA_RELOC_TREE_OBJECTID:
752 printf("DATA_RELOC_TREE");
754 case BTRFS_EXTENT_CSUM_OBJECTID:
755 printf("EXTENT_CSUM");
757 case BTRFS_FREE_SPACE_OBJECTID:
758 printf("FREE_SPACE");
760 case BTRFS_FREE_INO_OBJECTID:
763 case BTRFS_QUOTA_TREE_OBJECTID:
764 printf("QUOTA_TREE");
766 case BTRFS_UUID_TREE_OBJECTID:
769 case BTRFS_FREE_SPACE_TREE_OBJECTID:
770 printf("FREE_SPACE_TREE");
772 case BTRFS_MULTIPLE_OBJECTIDS:
778 case BTRFS_FIRST_CHUNK_TREE_OBJECTID:
779 if (type == BTRFS_CHUNK_ITEM_KEY) {
780 printf("FIRST_CHUNK_TREE");
785 printf("%llu", (unsigned long long)objectid);
789 void btrfs_print_key(struct btrfs_disk_key *disk_key)
791 u64 objectid = btrfs_disk_key_objectid(disk_key);
792 u8 type = btrfs_disk_key_type(disk_key);
793 u64 offset = btrfs_disk_key_offset(disk_key);
796 print_objectid(objectid, type);
798 print_key_type(objectid, type);
800 case BTRFS_QGROUP_RELATION_KEY:
801 case BTRFS_QGROUP_INFO_KEY:
802 case BTRFS_QGROUP_LIMIT_KEY:
803 printf(" %llu/%llu)", btrfs_qgroup_level(offset),
804 btrfs_qgroup_subvid(offset));
806 case BTRFS_UUID_KEY_SUBVOL:
807 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
808 printf(" 0x%016llx)", (unsigned long long)offset);
811 if (offset == (u64)-1)
814 printf(" %llu)", (unsigned long long)offset);
819 static void print_uuid_item(struct extent_buffer *l, unsigned long offset,
822 if (item_size & (sizeof(u64) - 1)) {
823 printf("btrfs: uuid item with illegal size %lu!\n",
824 (unsigned long)item_size);
830 read_extent_buffer(l, &subvol_id, offset, sizeof(u64));
831 printf("\t\tsubvol_id %llu\n",
832 (unsigned long long)le64_to_cpu(subvol_id));
833 item_size -= sizeof(u64);
834 offset += sizeof(u64);
838 void btrfs_print_leaf(struct btrfs_root *root, struct extent_buffer *l)
842 struct btrfs_item *item;
843 struct btrfs_dir_item *di;
844 struct btrfs_inode_item *ii;
845 struct btrfs_file_extent_item *fi;
846 struct btrfs_block_group_item *bi;
847 struct btrfs_extent_data_ref *dref;
848 struct btrfs_shared_data_ref *sref;
849 struct btrfs_inode_ref *iref;
850 struct btrfs_inode_extref *iref2;
851 struct btrfs_dev_extent *dev_extent;
852 struct btrfs_disk_key disk_key;
853 struct btrfs_block_group_item bg_item;
854 struct btrfs_free_space_info *free_info;
855 struct btrfs_dir_log_item *dlog;
856 struct btrfs_qgroup_info_item *qg_info;
857 struct btrfs_qgroup_limit_item *qg_limit;
858 struct btrfs_qgroup_status_item *qg_status;
859 u32 nr = btrfs_header_nritems(l);
864 printf("leaf %llu items %d free space %d generation %llu owner %llu\n",
865 (unsigned long long)btrfs_header_bytenr(l), nr,
866 btrfs_leaf_free_space(root, l),
867 (unsigned long long)btrfs_header_generation(l),
868 (unsigned long long)btrfs_header_owner(l));
871 for (i = 0 ; i < nr ; i++) {
872 item = btrfs_item_nr(i);
873 btrfs_item_key(l, &disk_key, i);
874 objectid = btrfs_disk_key_objectid(&disk_key);
875 type = btrfs_disk_key_type(&disk_key);
876 printf("\titem %d ", i);
877 btrfs_print_key(&disk_key);
878 printf(" itemoff %d itemsize %d\n",
879 btrfs_item_offset(l, item),
880 btrfs_item_size(l, item));
882 if (type == 0 && objectid == BTRFS_FREE_SPACE_OBJECTID)
883 print_free_space_header(l, i);
886 case BTRFS_INODE_ITEM_KEY:
887 ii = btrfs_item_ptr(l, i, struct btrfs_inode_item);
888 printf("\t\tinode generation %llu transid %llu size %llu nbytes %llu\n"
889 "\t\tblock group %llu mode %o links %u uid %u gid %u\n"
890 "\t\trdev %llu flags 0x%llx\n",
891 (unsigned long long)btrfs_inode_generation(l, ii),
892 (unsigned long long)btrfs_inode_transid(l, ii),
893 (unsigned long long)btrfs_inode_size(l, ii),
894 (unsigned long long)btrfs_inode_nbytes(l, ii),
895 (unsigned long long)btrfs_inode_block_group(l,ii),
896 btrfs_inode_mode(l, ii),
897 btrfs_inode_nlink(l, ii),
898 btrfs_inode_uid(l, ii),
899 btrfs_inode_gid(l, ii),
900 (unsigned long long)btrfs_inode_rdev(l,ii),
901 (unsigned long long)btrfs_inode_flags(l,ii));
903 case BTRFS_INODE_REF_KEY:
904 iref = btrfs_item_ptr(l, i, struct btrfs_inode_ref);
905 print_inode_ref_item(l, item, iref);
907 case BTRFS_INODE_EXTREF_KEY:
908 iref2 = btrfs_item_ptr(l, i, struct btrfs_inode_extref);
909 print_inode_extref_item(l, item, iref2);
911 case BTRFS_DIR_ITEM_KEY:
912 case BTRFS_DIR_INDEX_KEY:
913 case BTRFS_XATTR_ITEM_KEY:
914 di = btrfs_item_ptr(l, i, struct btrfs_dir_item);
915 print_dir_item(l, item, di);
917 case BTRFS_DIR_LOG_INDEX_KEY:
918 case BTRFS_DIR_LOG_ITEM_KEY:
919 dlog = btrfs_item_ptr(l, i, struct btrfs_dir_log_item);
920 printf("\t\tdir log end %Lu\n",
921 (unsigned long long)btrfs_dir_log_end(l, dlog));
923 case BTRFS_ORPHAN_ITEM_KEY:
924 printf("\t\torphan item\n");
926 case BTRFS_ROOT_ITEM_KEY:
929 case BTRFS_ROOT_REF_KEY:
930 print_root_ref(l, i, "ref");
932 case BTRFS_ROOT_BACKREF_KEY:
933 print_root_ref(l, i, "backref");
935 case BTRFS_EXTENT_ITEM_KEY:
936 print_extent_item(l, i, 0);
938 case BTRFS_METADATA_ITEM_KEY:
939 print_extent_item(l, i, 1);
941 case BTRFS_TREE_BLOCK_REF_KEY:
942 printf("\t\ttree block backref\n");
944 case BTRFS_SHARED_BLOCK_REF_KEY:
945 printf("\t\tshared block backref\n");
947 case BTRFS_EXTENT_DATA_REF_KEY:
948 dref = btrfs_item_ptr(l, i, struct btrfs_extent_data_ref);
949 printf("\t\textent data backref root %llu "
950 "objectid %llu offset %llu count %u\n",
951 (unsigned long long)btrfs_extent_data_ref_root(l, dref),
952 (unsigned long long)btrfs_extent_data_ref_objectid(l, dref),
953 (unsigned long long)btrfs_extent_data_ref_offset(l, dref),
954 btrfs_extent_data_ref_count(l, dref));
956 case BTRFS_SHARED_DATA_REF_KEY:
957 sref = btrfs_item_ptr(l, i, struct btrfs_shared_data_ref);
958 printf("\t\tshared data backref count %u\n",
959 btrfs_shared_data_ref_count(l, sref));
961 case BTRFS_EXTENT_REF_V0_KEY:
962 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
963 print_extent_ref_v0(l, i);
968 case BTRFS_CSUM_ITEM_KEY:
969 printf("\t\tcsum item\n");
971 case BTRFS_EXTENT_CSUM_KEY:
972 printf("\t\textent csum item\n");
974 case BTRFS_EXTENT_DATA_KEY:
975 fi = btrfs_item_ptr(l, i,
976 struct btrfs_file_extent_item);
977 print_file_extent_item(l, item, i, fi);
979 case BTRFS_BLOCK_GROUP_ITEM_KEY:
980 bi = btrfs_item_ptr(l, i,
981 struct btrfs_block_group_item);
982 read_extent_buffer(l, &bg_item, (unsigned long)bi,
984 memset(flags_str, 0, sizeof(flags_str));
985 bg_flags_to_str(btrfs_block_group_flags(&bg_item),
987 printf("\t\tblock group used %llu chunk_objectid %llu flags %s\n",
988 (unsigned long long)btrfs_block_group_used(&bg_item),
989 (unsigned long long)btrfs_block_group_chunk_objectid(&bg_item),
992 case BTRFS_FREE_SPACE_INFO_KEY:
993 free_info = btrfs_item_ptr(l, i, struct btrfs_free_space_info);
994 printf("\t\tfree space info extent count %u flags %u\n",
995 (unsigned)btrfs_free_space_extent_count(l, free_info),
996 (unsigned)btrfs_free_space_flags(l, free_info));
998 case BTRFS_FREE_SPACE_EXTENT_KEY:
999 printf("\t\tfree space extent\n");
1001 case BTRFS_FREE_SPACE_BITMAP_KEY:
1002 printf("\t\tfree space bitmap\n");
1004 case BTRFS_CHUNK_ITEM_KEY:
1005 print_chunk(l, btrfs_item_ptr(l, i, struct btrfs_chunk));
1007 case BTRFS_DEV_ITEM_KEY:
1008 print_dev_item(l, btrfs_item_ptr(l, i,
1009 struct btrfs_dev_item));
1011 case BTRFS_DEV_EXTENT_KEY:
1012 dev_extent = btrfs_item_ptr(l, i,
1013 struct btrfs_dev_extent);
1014 printf("\t\tdev extent chunk_tree %llu\n"
1015 "\t\tchunk objectid %llu chunk offset %llu "
1017 (unsigned long long)
1018 btrfs_dev_extent_chunk_tree(l, dev_extent),
1019 (unsigned long long)
1020 btrfs_dev_extent_chunk_objectid(l, dev_extent),
1021 (unsigned long long)
1022 btrfs_dev_extent_chunk_offset(l, dev_extent),
1023 (unsigned long long)
1024 btrfs_dev_extent_length(l, dev_extent));
1026 case BTRFS_QGROUP_STATUS_KEY:
1027 qg_status = btrfs_item_ptr(l, i,
1028 struct btrfs_qgroup_status_item);
1029 memset(flags_str, 0, sizeof(flags_str));
1030 qgroup_flags_to_str(btrfs_qgroup_status_flags(l, qg_status),
1032 printf("\t\tversion %llu generation %llu flags %s "
1034 (unsigned long long)
1035 btrfs_qgroup_status_version(l, qg_status),
1036 (unsigned long long)
1037 btrfs_qgroup_status_generation(l, qg_status),
1039 (unsigned long long)
1040 btrfs_qgroup_status_scan(l, qg_status));
1042 case BTRFS_QGROUP_RELATION_KEY:
1044 case BTRFS_QGROUP_INFO_KEY:
1045 qg_info = btrfs_item_ptr(l, i,
1046 struct btrfs_qgroup_info_item);
1047 printf("\t\tgeneration %llu\n"
1048 "\t\treferenced %llu referenced compressed %llu\n"
1049 "\t\texclusive %llu exclusive compressed %llu\n",
1050 (unsigned long long)
1051 btrfs_qgroup_info_generation(l, qg_info),
1052 (unsigned long long)
1053 btrfs_qgroup_info_referenced(l, qg_info),
1054 (unsigned long long)
1055 btrfs_qgroup_info_referenced_compressed(l,
1057 (unsigned long long)
1058 btrfs_qgroup_info_exclusive(l, qg_info),
1059 (unsigned long long)
1060 btrfs_qgroup_info_exclusive_compressed(l,
1063 case BTRFS_QGROUP_LIMIT_KEY:
1064 qg_limit = btrfs_item_ptr(l, i,
1065 struct btrfs_qgroup_limit_item);
1066 printf("\t\tflags %llx\n"
1067 "\t\tmax referenced %lld max exclusive %lld\n"
1068 "\t\trsv referenced %lld rsv exclusive %lld\n",
1069 (unsigned long long)
1070 btrfs_qgroup_limit_flags(l, qg_limit),
1072 btrfs_qgroup_limit_max_referenced(l, qg_limit),
1074 btrfs_qgroup_limit_max_exclusive(l, qg_limit),
1076 btrfs_qgroup_limit_rsv_referenced(l, qg_limit),
1078 btrfs_qgroup_limit_rsv_exclusive(l, qg_limit));
1080 case BTRFS_UUID_KEY_SUBVOL:
1081 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
1082 print_uuid_item(l, btrfs_item_ptr_offset(l, i),
1083 btrfs_item_size_nr(l, i));
1085 case BTRFS_STRING_ITEM_KEY:
1086 /* dirty, but it's simple */
1087 str = l->data + btrfs_item_ptr_offset(l, i);
1088 printf("\t\titem data %.*s\n", btrfs_item_size(l, item), str);
1090 case BTRFS_DEV_STATS_KEY:
1091 printf("\t\tdevice stats\n");
1098 void btrfs_print_tree(struct btrfs_root *root, struct extent_buffer *eb, int follow)
1103 struct btrfs_disk_key disk_key;
1104 struct btrfs_key key;
1108 nr = btrfs_header_nritems(eb);
1109 if (btrfs_is_leaf(eb)) {
1110 btrfs_print_leaf(root, eb);
1113 printf("node %llu level %d items %d free %u generation %llu owner %llu\n",
1114 (unsigned long long)eb->start,
1115 btrfs_header_level(eb), nr,
1116 (u32)BTRFS_NODEPTRS_PER_BLOCK(root) - nr,
1117 (unsigned long long)btrfs_header_generation(eb),
1118 (unsigned long long)btrfs_header_owner(eb));
1121 size = root->nodesize;
1122 for (i = 0; i < nr; i++) {
1123 u64 blocknr = btrfs_node_blockptr(eb, i);
1124 btrfs_node_key(eb, &disk_key, i);
1125 btrfs_disk_key_to_cpu(&key, &disk_key);
1127 btrfs_print_key(&disk_key);
1128 printf(" block %llu (%llu) gen %llu\n",
1129 (unsigned long long)blocknr,
1130 (unsigned long long)blocknr / size,
1131 (unsigned long long)btrfs_node_ptr_generation(eb, i));
1137 for (i = 0; i < nr; i++) {
1138 struct extent_buffer *next = read_tree_block(root,
1139 btrfs_node_blockptr(eb, i),
1141 btrfs_node_ptr_generation(eb, i));
1142 if (!extent_buffer_uptodate(next)) {
1143 fprintf(stderr, "failed to read %llu in tree %llu\n",
1144 (unsigned long long)btrfs_node_blockptr(eb, i),
1145 (unsigned long long)btrfs_header_owner(eb));
1148 if (btrfs_is_leaf(next) &&
1149 btrfs_header_level(eb) != 1)
1151 if (btrfs_header_level(next) !=
1152 btrfs_header_level(eb) - 1)
1154 btrfs_print_tree(root, next, 1);
1155 free_extent_buffer(next);