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 print_file_extent_item(struct extent_buffer *eb,
289 struct btrfs_item *item,
291 struct btrfs_file_extent_item *fi)
293 int extent_type = btrfs_file_extent_type(eb, fi);
295 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
296 printf("\t\tinline extent data size %u "
297 "ram %u compress %d\n",
298 btrfs_file_extent_inline_item_len(eb, item),
299 btrfs_file_extent_inline_len(eb, slot, fi),
300 btrfs_file_extent_compression(eb, fi));
303 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
304 printf("\t\tprealloc 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\tprealloc data offset %llu nr %llu\n",
308 (unsigned long long)btrfs_file_extent_offset(eb, fi),
309 (unsigned long long)btrfs_file_extent_num_bytes(eb, fi));
312 printf("\t\textent data disk byte %llu nr %llu\n",
313 (unsigned long long)btrfs_file_extent_disk_bytenr(eb, fi),
314 (unsigned long long)btrfs_file_extent_disk_num_bytes(eb, fi));
315 printf("\t\textent data offset %llu nr %llu ram %llu\n",
316 (unsigned long long)btrfs_file_extent_offset(eb, fi),
317 (unsigned long long)btrfs_file_extent_num_bytes(eb, fi),
318 (unsigned long long)btrfs_file_extent_ram_bytes(eb, fi));
319 printf("\t\textent compression %d\n",
320 btrfs_file_extent_compression(eb, fi));
323 /* Caller should ensure sizeof(*ret) >= 16("DATA|TREE_BLOCK") */
324 static void extent_flags_to_str(u64 flags, char *ret)
328 if (flags & BTRFS_EXTENT_FLAG_DATA) {
332 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
337 strcat(ret, "TREE_BLOCK");
339 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
341 strcat(ret, "FULL_BACKREF");
345 void print_extent_item(struct extent_buffer *eb, int slot, int metadata)
347 struct btrfs_extent_item *ei;
348 struct btrfs_extent_inline_ref *iref;
349 struct btrfs_extent_data_ref *dref;
350 struct btrfs_shared_data_ref *sref;
351 struct btrfs_disk_key key;
355 u32 item_size = btrfs_item_size_nr(eb, slot);
358 char flags_str[32] = {0};
360 if (item_size < sizeof(*ei)) {
361 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
362 struct btrfs_extent_item_v0 *ei0;
363 BUG_ON(item_size != sizeof(*ei0));
364 ei0 = btrfs_item_ptr(eb, slot, struct btrfs_extent_item_v0);
365 printf("\t\textent refs %u\n",
366 btrfs_extent_refs_v0(eb, ei0));
373 ei = btrfs_item_ptr(eb, slot, struct btrfs_extent_item);
374 flags = btrfs_extent_flags(eb, ei);
375 extent_flags_to_str(flags, flags_str);
377 printf("\t\textent refs %llu gen %llu flags %s\n",
378 (unsigned long long)btrfs_extent_refs(eb, ei),
379 (unsigned long long)btrfs_extent_generation(eb, ei),
382 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !metadata) {
383 struct btrfs_tree_block_info *info;
384 info = (struct btrfs_tree_block_info *)(ei + 1);
385 btrfs_tree_block_key(eb, info, &key);
386 printf("\t\ttree block ");
387 btrfs_print_key(&key);
388 printf(" level %d\n", btrfs_tree_block_level(eb, info));
389 iref = (struct btrfs_extent_inline_ref *)(info + 1);
390 } else if (metadata) {
391 struct btrfs_key tmp;
393 btrfs_item_key_to_cpu(eb, &tmp, slot);
394 printf("\t\ttree block skinny level %d\n", (int)tmp.offset);
395 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
397 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
400 ptr = (unsigned long)iref;
401 end = (unsigned long)ei + item_size;
403 iref = (struct btrfs_extent_inline_ref *)ptr;
404 type = btrfs_extent_inline_ref_type(eb, iref);
405 offset = btrfs_extent_inline_ref_offset(eb, iref);
407 case BTRFS_TREE_BLOCK_REF_KEY:
408 printf("\t\ttree block backref root %llu\n",
409 (unsigned long long)offset);
411 case BTRFS_SHARED_BLOCK_REF_KEY:
412 printf("\t\tshared block backref parent %llu\n",
413 (unsigned long long)offset);
415 case BTRFS_EXTENT_DATA_REF_KEY:
416 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
417 printf("\t\textent data backref root %llu "
418 "objectid %llu offset %llu count %u\n",
419 (unsigned long long)btrfs_extent_data_ref_root(eb, dref),
420 (unsigned long long)btrfs_extent_data_ref_objectid(eb, dref),
421 (unsigned long long)btrfs_extent_data_ref_offset(eb, dref),
422 btrfs_extent_data_ref_count(eb, dref));
424 case BTRFS_SHARED_DATA_REF_KEY:
425 sref = (struct btrfs_shared_data_ref *)(iref + 1);
426 printf("\t\tshared data backref parent %llu count %u\n",
427 (unsigned long long)offset,
428 btrfs_shared_data_ref_count(eb, sref));
433 ptr += btrfs_extent_inline_ref_size(type);
438 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
439 static void print_extent_ref_v0(struct extent_buffer *eb, int slot)
441 struct btrfs_extent_ref_v0 *ref0;
443 ref0 = btrfs_item_ptr(eb, slot, struct btrfs_extent_ref_v0);
444 printf("\t\textent back ref root %llu gen %llu "
445 "owner %llu num_refs %lu\n",
446 (unsigned long long)btrfs_ref_root_v0(eb, ref0),
447 (unsigned long long)btrfs_ref_generation_v0(eb, ref0),
448 (unsigned long long)btrfs_ref_objectid_v0(eb, ref0),
449 (unsigned long)btrfs_ref_count_v0(eb, ref0));
453 static void print_root_ref(struct extent_buffer *leaf, int slot, char *tag)
455 struct btrfs_root_ref *ref;
456 char namebuf[BTRFS_NAME_LEN];
459 ref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
460 namelen = btrfs_root_ref_name_len(leaf, ref);
461 read_extent_buffer(leaf, namebuf, (unsigned long)(ref + 1), namelen);
462 printf("\t\troot %s key dirid %llu sequence %llu name %.*s\n", tag,
463 (unsigned long long)btrfs_root_ref_dirid(leaf, ref),
464 (unsigned long long)btrfs_root_ref_sequence(leaf, ref),
468 static int count_bytes(void *buf, int len, char b)
472 for (i = 0; i < len; i++) {
473 if (((char*)buf)[i] == b)
479 static void print_root(struct extent_buffer *leaf, int slot)
481 struct btrfs_root_item *ri;
482 struct btrfs_root_item root_item;
484 char uuid_str[BTRFS_UUID_UNPARSED_SIZE];
486 ri = btrfs_item_ptr(leaf, slot, struct btrfs_root_item);
487 len = btrfs_item_size_nr(leaf, slot);
489 memset(&root_item, 0, sizeof(root_item));
490 read_extent_buffer(leaf, &root_item, (unsigned long)ri, len);
492 printf("\t\troot data bytenr %llu level %d dirid %llu refs %u gen %llu lastsnap %llu\n",
493 (unsigned long long)btrfs_root_bytenr(&root_item),
494 btrfs_root_level(&root_item),
495 (unsigned long long)btrfs_root_dirid(&root_item),
496 btrfs_root_refs(&root_item),
497 (unsigned long long)btrfs_root_generation(&root_item),
498 (unsigned long long)btrfs_root_last_snapshot(&root_item));
500 if (root_item.generation == root_item.generation_v2) {
501 uuid_unparse(root_item.uuid, uuid_str);
502 printf("\t\tuuid %s\n", uuid_str);
503 if (count_bytes(root_item.parent_uuid, BTRFS_UUID_SIZE, 0) != BTRFS_UUID_SIZE) {
504 uuid_unparse(root_item.parent_uuid, uuid_str);
505 printf("\t\tparent_uuid %s\n", uuid_str);
507 if (count_bytes(root_item.received_uuid, BTRFS_UUID_SIZE, 0) != BTRFS_UUID_SIZE) {
508 uuid_unparse(root_item.received_uuid, uuid_str);
509 printf("\t\treceived_uuid %s\n", uuid_str);
511 if (root_item.ctransid) {
512 printf("\t\tctransid %llu otransid %llu stransid %llu rtransid %llu\n",
513 btrfs_root_ctransid(&root_item),
514 btrfs_root_otransid(&root_item),
515 btrfs_root_stransid(&root_item),
516 btrfs_root_rtransid(&root_item));
519 if (btrfs_root_refs(&root_item) == 0) {
520 struct btrfs_key drop_key;
521 btrfs_disk_key_to_cpu(&drop_key,
522 &root_item.drop_progress);
524 btrfs_print_key(&root_item.drop_progress);
525 printf(" level %d\n", root_item.drop_level);
529 static void print_free_space_header(struct extent_buffer *leaf, int slot)
531 struct btrfs_free_space_header *header;
532 struct btrfs_disk_key location;
534 header = btrfs_item_ptr(leaf, slot, struct btrfs_free_space_header);
535 btrfs_free_space_key(leaf, header, &location);
536 printf("\t\tlocation ");
537 btrfs_print_key(&location);
539 printf("\t\tcache generation %llu entries %llu bitmaps %llu\n",
540 (unsigned long long)btrfs_free_space_generation(leaf, header),
541 (unsigned long long)btrfs_free_space_entries(leaf, header),
542 (unsigned long long)btrfs_free_space_bitmaps(leaf, header));
545 static void print_key_type(u64 objectid, u8 type)
547 if (type == 0 && objectid == BTRFS_FREE_SPACE_OBJECTID) {
553 case BTRFS_INODE_ITEM_KEY:
554 printf("INODE_ITEM");
556 case BTRFS_INODE_REF_KEY:
559 case BTRFS_INODE_EXTREF_KEY:
560 printf("INODE_EXTREF");
562 case BTRFS_DIR_ITEM_KEY:
565 case BTRFS_DIR_INDEX_KEY:
568 case BTRFS_DIR_LOG_ITEM_KEY:
569 printf("DIR_LOG_ITEM");
571 case BTRFS_DIR_LOG_INDEX_KEY:
572 printf("DIR_LOG_INDEX");
574 case BTRFS_XATTR_ITEM_KEY:
575 printf("XATTR_ITEM");
577 case BTRFS_ORPHAN_ITEM_KEY:
578 printf("ORPHAN_ITEM");
580 case BTRFS_ROOT_ITEM_KEY:
583 case BTRFS_ROOT_REF_KEY:
586 case BTRFS_ROOT_BACKREF_KEY:
587 printf("ROOT_BACKREF");
589 case BTRFS_EXTENT_ITEM_KEY:
590 printf("EXTENT_ITEM");
592 case BTRFS_METADATA_ITEM_KEY:
593 printf("METADATA_ITEM");
595 case BTRFS_TREE_BLOCK_REF_KEY:
596 printf("TREE_BLOCK_REF");
598 case BTRFS_SHARED_BLOCK_REF_KEY:
599 printf("SHARED_BLOCK_REF");
601 case BTRFS_EXTENT_DATA_REF_KEY:
602 printf("EXTENT_DATA_REF");
604 case BTRFS_SHARED_DATA_REF_KEY:
605 printf("SHARED_DATA_REF");
607 case BTRFS_EXTENT_REF_V0_KEY:
608 printf("EXTENT_REF_V0");
610 case BTRFS_CSUM_ITEM_KEY:
613 case BTRFS_EXTENT_CSUM_KEY:
614 printf("EXTENT_CSUM");
616 case BTRFS_EXTENT_DATA_KEY:
617 printf("EXTENT_DATA");
619 case BTRFS_BLOCK_GROUP_ITEM_KEY:
620 printf("BLOCK_GROUP_ITEM");
622 case BTRFS_FREE_SPACE_INFO_KEY:
623 printf("FREE_SPACE_INFO");
625 case BTRFS_FREE_SPACE_EXTENT_KEY:
626 printf("FREE_SPACE_EXTENT");
628 case BTRFS_FREE_SPACE_BITMAP_KEY:
629 printf("FREE_SPACE_BITMAP");
631 case BTRFS_CHUNK_ITEM_KEY:
632 printf("CHUNK_ITEM");
634 case BTRFS_DEV_ITEM_KEY:
637 case BTRFS_DEV_EXTENT_KEY:
638 printf("DEV_EXTENT");
640 case BTRFS_BALANCE_ITEM_KEY:
641 printf("BALANCE_ITEM");
643 case BTRFS_DEV_REPLACE_KEY:
644 printf("DEV_REPLACE");
646 case BTRFS_STRING_ITEM_KEY:
647 printf("STRING_ITEM");
649 case BTRFS_QGROUP_STATUS_KEY:
650 printf("QGROUP_STATUS");
652 case BTRFS_QGROUP_RELATION_KEY:
653 printf("QGROUP_RELATION");
655 case BTRFS_QGROUP_INFO_KEY:
656 printf("QGROUP_INFO");
658 case BTRFS_QGROUP_LIMIT_KEY:
659 printf("QGROUP_LIMIT");
661 case BTRFS_DEV_STATS_KEY:
664 case BTRFS_UUID_KEY_SUBVOL:
665 printf("UUID_KEY_SUBVOL");
667 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
668 printf("UUID_KEY_RECEIVED_SUBVOL");
671 printf("UNKNOWN.%d", type);
675 static void print_objectid(u64 objectid, u8 type)
678 case BTRFS_DEV_EXTENT_KEY:
679 printf("%llu", (unsigned long long)objectid); /* device id */
681 case BTRFS_QGROUP_RELATION_KEY:
682 printf("%llu/%llu", btrfs_qgroup_level(objectid),
683 btrfs_qgroup_subvid(objectid));
685 case BTRFS_UUID_KEY_SUBVOL:
686 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
687 printf("0x%016llx", (unsigned long long)objectid);
692 case BTRFS_ROOT_TREE_OBJECTID:
693 if (type == BTRFS_DEV_ITEM_KEY)
698 case BTRFS_EXTENT_TREE_OBJECTID:
699 printf("EXTENT_TREE");
701 case BTRFS_CHUNK_TREE_OBJECTID:
702 printf("CHUNK_TREE");
704 case BTRFS_DEV_TREE_OBJECTID:
707 case BTRFS_FS_TREE_OBJECTID:
710 case BTRFS_ROOT_TREE_DIR_OBJECTID:
711 printf("ROOT_TREE_DIR");
713 case BTRFS_CSUM_TREE_OBJECTID:
716 case BTRFS_BALANCE_OBJECTID:
719 case BTRFS_ORPHAN_OBJECTID:
722 case BTRFS_TREE_LOG_OBJECTID:
725 case BTRFS_TREE_LOG_FIXUP_OBJECTID:
728 case BTRFS_TREE_RELOC_OBJECTID:
729 printf("TREE_RELOC");
731 case BTRFS_DATA_RELOC_TREE_OBJECTID:
732 printf("DATA_RELOC_TREE");
734 case BTRFS_EXTENT_CSUM_OBJECTID:
735 printf("EXTENT_CSUM");
737 case BTRFS_FREE_SPACE_OBJECTID:
738 printf("FREE_SPACE");
740 case BTRFS_FREE_INO_OBJECTID:
743 case BTRFS_QUOTA_TREE_OBJECTID:
744 printf("QUOTA_TREE");
746 case BTRFS_UUID_TREE_OBJECTID:
749 case BTRFS_FREE_SPACE_TREE_OBJECTID:
750 printf("FREE_SPACE_TREE");
752 case BTRFS_MULTIPLE_OBJECTIDS:
758 case BTRFS_FIRST_CHUNK_TREE_OBJECTID:
759 if (type == BTRFS_CHUNK_ITEM_KEY) {
760 printf("FIRST_CHUNK_TREE");
765 printf("%llu", (unsigned long long)objectid);
769 void btrfs_print_key(struct btrfs_disk_key *disk_key)
771 u64 objectid = btrfs_disk_key_objectid(disk_key);
772 u8 type = btrfs_disk_key_type(disk_key);
773 u64 offset = btrfs_disk_key_offset(disk_key);
776 print_objectid(objectid, type);
778 print_key_type(objectid, type);
780 case BTRFS_QGROUP_RELATION_KEY:
781 case BTRFS_QGROUP_INFO_KEY:
782 case BTRFS_QGROUP_LIMIT_KEY:
783 printf(" %llu/%llu)", btrfs_qgroup_level(offset),
784 btrfs_qgroup_subvid(offset));
786 case BTRFS_UUID_KEY_SUBVOL:
787 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
788 printf(" 0x%016llx)", (unsigned long long)offset);
791 if (offset == (u64)-1)
794 printf(" %llu)", (unsigned long long)offset);
799 static void print_uuid_item(struct extent_buffer *l, unsigned long offset,
802 if (item_size & (sizeof(u64) - 1)) {
803 printf("btrfs: uuid item with illegal size %lu!\n",
804 (unsigned long)item_size);
810 read_extent_buffer(l, &subvol_id, offset, sizeof(u64));
811 printf("\t\tsubvol_id %llu\n",
812 (unsigned long long)le64_to_cpu(subvol_id));
813 item_size -= sizeof(u64);
814 offset += sizeof(u64);
818 void btrfs_print_leaf(struct btrfs_root *root, struct extent_buffer *l)
822 struct btrfs_item *item;
823 struct btrfs_dir_item *di;
824 struct btrfs_inode_item *ii;
825 struct btrfs_file_extent_item *fi;
826 struct btrfs_block_group_item *bi;
827 struct btrfs_extent_data_ref *dref;
828 struct btrfs_shared_data_ref *sref;
829 struct btrfs_inode_ref *iref;
830 struct btrfs_inode_extref *iref2;
831 struct btrfs_dev_extent *dev_extent;
832 struct btrfs_disk_key disk_key;
833 struct btrfs_block_group_item bg_item;
834 struct btrfs_free_space_info *free_info;
835 struct btrfs_dir_log_item *dlog;
836 struct btrfs_qgroup_info_item *qg_info;
837 struct btrfs_qgroup_limit_item *qg_limit;
838 struct btrfs_qgroup_status_item *qg_status;
839 u32 nr = btrfs_header_nritems(l);
844 printf("leaf %llu items %d free space %d generation %llu owner %llu\n",
845 (unsigned long long)btrfs_header_bytenr(l), nr,
846 btrfs_leaf_free_space(root, l),
847 (unsigned long long)btrfs_header_generation(l),
848 (unsigned long long)btrfs_header_owner(l));
851 for (i = 0 ; i < nr ; i++) {
852 item = btrfs_item_nr(i);
853 btrfs_item_key(l, &disk_key, i);
854 objectid = btrfs_disk_key_objectid(&disk_key);
855 type = btrfs_disk_key_type(&disk_key);
856 printf("\titem %d ", i);
857 btrfs_print_key(&disk_key);
858 printf(" itemoff %d itemsize %d\n",
859 btrfs_item_offset(l, item),
860 btrfs_item_size(l, item));
862 if (type == 0 && objectid == BTRFS_FREE_SPACE_OBJECTID)
863 print_free_space_header(l, i);
866 case BTRFS_INODE_ITEM_KEY:
867 ii = btrfs_item_ptr(l, i, struct btrfs_inode_item);
868 printf("\t\tinode generation %llu transid %llu size %llu nbytes %llu\n"
869 "\t\tblock group %llu mode %o links %u uid %u gid %u\n"
870 "\t\trdev %llu flags 0x%llx\n",
871 (unsigned long long)btrfs_inode_generation(l, ii),
872 (unsigned long long)btrfs_inode_transid(l, ii),
873 (unsigned long long)btrfs_inode_size(l, ii),
874 (unsigned long long)btrfs_inode_nbytes(l, ii),
875 (unsigned long long)btrfs_inode_block_group(l,ii),
876 btrfs_inode_mode(l, ii),
877 btrfs_inode_nlink(l, ii),
878 btrfs_inode_uid(l, ii),
879 btrfs_inode_gid(l, ii),
880 (unsigned long long)btrfs_inode_rdev(l,ii),
881 (unsigned long long)btrfs_inode_flags(l,ii));
883 case BTRFS_INODE_REF_KEY:
884 iref = btrfs_item_ptr(l, i, struct btrfs_inode_ref);
885 print_inode_ref_item(l, item, iref);
887 case BTRFS_INODE_EXTREF_KEY:
888 iref2 = btrfs_item_ptr(l, i, struct btrfs_inode_extref);
889 print_inode_extref_item(l, item, iref2);
891 case BTRFS_DIR_ITEM_KEY:
892 case BTRFS_DIR_INDEX_KEY:
893 case BTRFS_XATTR_ITEM_KEY:
894 di = btrfs_item_ptr(l, i, struct btrfs_dir_item);
895 print_dir_item(l, item, di);
897 case BTRFS_DIR_LOG_INDEX_KEY:
898 case BTRFS_DIR_LOG_ITEM_KEY:
899 dlog = btrfs_item_ptr(l, i, struct btrfs_dir_log_item);
900 printf("\t\tdir log end %Lu\n",
901 (unsigned long long)btrfs_dir_log_end(l, dlog));
903 case BTRFS_ORPHAN_ITEM_KEY:
904 printf("\t\torphan item\n");
906 case BTRFS_ROOT_ITEM_KEY:
909 case BTRFS_ROOT_REF_KEY:
910 print_root_ref(l, i, "ref");
912 case BTRFS_ROOT_BACKREF_KEY:
913 print_root_ref(l, i, "backref");
915 case BTRFS_EXTENT_ITEM_KEY:
916 print_extent_item(l, i, 0);
918 case BTRFS_METADATA_ITEM_KEY:
919 print_extent_item(l, i, 1);
921 case BTRFS_TREE_BLOCK_REF_KEY:
922 printf("\t\ttree block backref\n");
924 case BTRFS_SHARED_BLOCK_REF_KEY:
925 printf("\t\tshared block backref\n");
927 case BTRFS_EXTENT_DATA_REF_KEY:
928 dref = btrfs_item_ptr(l, i, struct btrfs_extent_data_ref);
929 printf("\t\textent data backref root %llu "
930 "objectid %llu offset %llu count %u\n",
931 (unsigned long long)btrfs_extent_data_ref_root(l, dref),
932 (unsigned long long)btrfs_extent_data_ref_objectid(l, dref),
933 (unsigned long long)btrfs_extent_data_ref_offset(l, dref),
934 btrfs_extent_data_ref_count(l, dref));
936 case BTRFS_SHARED_DATA_REF_KEY:
937 sref = btrfs_item_ptr(l, i, struct btrfs_shared_data_ref);
938 printf("\t\tshared data backref count %u\n",
939 btrfs_shared_data_ref_count(l, sref));
941 case BTRFS_EXTENT_REF_V0_KEY:
942 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
943 print_extent_ref_v0(l, i);
948 case BTRFS_CSUM_ITEM_KEY:
949 printf("\t\tcsum item\n");
951 case BTRFS_EXTENT_CSUM_KEY:
952 printf("\t\textent csum item\n");
954 case BTRFS_EXTENT_DATA_KEY:
955 fi = btrfs_item_ptr(l, i,
956 struct btrfs_file_extent_item);
957 print_file_extent_item(l, item, i, fi);
959 case BTRFS_BLOCK_GROUP_ITEM_KEY:
960 bi = btrfs_item_ptr(l, i,
961 struct btrfs_block_group_item);
962 read_extent_buffer(l, &bg_item, (unsigned long)bi,
964 memset(flags_str, 0, sizeof(flags_str));
965 bg_flags_to_str(btrfs_block_group_flags(&bg_item),
967 printf("\t\tblock group used %llu chunk_objectid %llu flags %s\n",
968 (unsigned long long)btrfs_block_group_used(&bg_item),
969 (unsigned long long)btrfs_block_group_chunk_objectid(&bg_item),
972 case BTRFS_FREE_SPACE_INFO_KEY:
973 free_info = btrfs_item_ptr(l, i, struct btrfs_free_space_info);
974 printf("\t\tfree space info extent count %u flags %u\n",
975 (unsigned)btrfs_free_space_extent_count(l, free_info),
976 (unsigned)btrfs_free_space_flags(l, free_info));
978 case BTRFS_FREE_SPACE_EXTENT_KEY:
979 printf("\t\tfree space extent\n");
981 case BTRFS_FREE_SPACE_BITMAP_KEY:
982 printf("\t\tfree space bitmap\n");
984 case BTRFS_CHUNK_ITEM_KEY:
985 print_chunk(l, btrfs_item_ptr(l, i, struct btrfs_chunk));
987 case BTRFS_DEV_ITEM_KEY:
988 print_dev_item(l, btrfs_item_ptr(l, i,
989 struct btrfs_dev_item));
991 case BTRFS_DEV_EXTENT_KEY:
992 dev_extent = btrfs_item_ptr(l, i,
993 struct btrfs_dev_extent);
994 printf("\t\tdev extent chunk_tree %llu\n"
995 "\t\tchunk objectid %llu chunk offset %llu "
998 btrfs_dev_extent_chunk_tree(l, dev_extent),
1000 btrfs_dev_extent_chunk_objectid(l, dev_extent),
1001 (unsigned long long)
1002 btrfs_dev_extent_chunk_offset(l, dev_extent),
1003 (unsigned long long)
1004 btrfs_dev_extent_length(l, dev_extent));
1006 case BTRFS_QGROUP_STATUS_KEY:
1007 qg_status = btrfs_item_ptr(l, i,
1008 struct btrfs_qgroup_status_item);
1009 memset(flags_str, 0, sizeof(flags_str));
1010 qgroup_flags_to_str(btrfs_qgroup_status_flags(l, qg_status),
1012 printf("\t\tversion %llu generation %llu flags %s "
1014 (unsigned long long)
1015 btrfs_qgroup_status_version(l, qg_status),
1016 (unsigned long long)
1017 btrfs_qgroup_status_generation(l, qg_status),
1019 (unsigned long long)
1020 btrfs_qgroup_status_scan(l, qg_status));
1022 case BTRFS_QGROUP_RELATION_KEY:
1024 case BTRFS_QGROUP_INFO_KEY:
1025 qg_info = btrfs_item_ptr(l, i,
1026 struct btrfs_qgroup_info_item);
1027 printf("\t\tgeneration %llu\n"
1028 "\t\treferenced %llu referenced compressed %llu\n"
1029 "\t\texclusive %llu exclusive compressed %llu\n",
1030 (unsigned long long)
1031 btrfs_qgroup_info_generation(l, qg_info),
1032 (unsigned long long)
1033 btrfs_qgroup_info_referenced(l, qg_info),
1034 (unsigned long long)
1035 btrfs_qgroup_info_referenced_compressed(l,
1037 (unsigned long long)
1038 btrfs_qgroup_info_exclusive(l, qg_info),
1039 (unsigned long long)
1040 btrfs_qgroup_info_exclusive_compressed(l,
1043 case BTRFS_QGROUP_LIMIT_KEY:
1044 qg_limit = btrfs_item_ptr(l, i,
1045 struct btrfs_qgroup_limit_item);
1046 printf("\t\tflags %llx\n"
1047 "\t\tmax referenced %lld max exclusive %lld\n"
1048 "\t\trsv referenced %lld rsv exclusive %lld\n",
1049 (unsigned long long)
1050 btrfs_qgroup_limit_flags(l, qg_limit),
1052 btrfs_qgroup_limit_max_referenced(l, qg_limit),
1054 btrfs_qgroup_limit_max_exclusive(l, qg_limit),
1056 btrfs_qgroup_limit_rsv_referenced(l, qg_limit),
1058 btrfs_qgroup_limit_rsv_exclusive(l, qg_limit));
1060 case BTRFS_UUID_KEY_SUBVOL:
1061 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
1062 print_uuid_item(l, btrfs_item_ptr_offset(l, i),
1063 btrfs_item_size_nr(l, i));
1065 case BTRFS_STRING_ITEM_KEY:
1066 /* dirty, but it's simple */
1067 str = l->data + btrfs_item_ptr_offset(l, i);
1068 printf("\t\titem data %.*s\n", btrfs_item_size(l, item), str);
1070 case BTRFS_DEV_STATS_KEY:
1071 printf("\t\tdevice stats\n");
1078 void btrfs_print_tree(struct btrfs_root *root, struct extent_buffer *eb, int follow)
1083 struct btrfs_disk_key disk_key;
1084 struct btrfs_key key;
1088 nr = btrfs_header_nritems(eb);
1089 if (btrfs_is_leaf(eb)) {
1090 btrfs_print_leaf(root, eb);
1093 printf("node %llu level %d items %d free %u generation %llu owner %llu\n",
1094 (unsigned long long)eb->start,
1095 btrfs_header_level(eb), nr,
1096 (u32)BTRFS_NODEPTRS_PER_BLOCK(root) - nr,
1097 (unsigned long long)btrfs_header_generation(eb),
1098 (unsigned long long)btrfs_header_owner(eb));
1101 size = btrfs_level_size(root, btrfs_header_level(eb) - 1);
1102 for (i = 0; i < nr; i++) {
1103 u64 blocknr = btrfs_node_blockptr(eb, i);
1104 btrfs_node_key(eb, &disk_key, i);
1105 btrfs_disk_key_to_cpu(&key, &disk_key);
1107 btrfs_print_key(&disk_key);
1108 printf(" block %llu (%llu) gen %llu\n",
1109 (unsigned long long)blocknr,
1110 (unsigned long long)blocknr / size,
1111 (unsigned long long)btrfs_node_ptr_generation(eb, i));
1117 for (i = 0; i < nr; i++) {
1118 struct extent_buffer *next = read_tree_block(root,
1119 btrfs_node_blockptr(eb, i),
1121 btrfs_node_ptr_generation(eb, i));
1122 if (!extent_buffer_uptodate(next)) {
1123 fprintf(stderr, "failed to read %llu in tree %llu\n",
1124 (unsigned long long)btrfs_node_blockptr(eb, i),
1125 (unsigned long long)btrfs_header_owner(eb));
1128 if (btrfs_is_leaf(next) &&
1129 btrfs_header_level(eb) != 1)
1131 if (btrfs_header_level(next) !=
1132 btrfs_header_level(eb) - 1)
1134 btrfs_print_tree(root, next, 1);
1135 free_extent_buffer(next);