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)
500 * Caller must ensure sizeof(*ret) >= 7 "RDONLY"
502 static void root_flags_to_str(u64 flags, char *ret)
504 if (flags & BTRFS_ROOT_SUBVOL_RDONLY)
505 strcat(ret, "RDONLY");
510 static void print_root(struct extent_buffer *leaf, int slot)
512 struct btrfs_root_item *ri;
513 struct btrfs_root_item root_item;
515 char uuid_str[BTRFS_UUID_UNPARSED_SIZE];
516 char flags_str[32] = {0};
518 ri = btrfs_item_ptr(leaf, slot, struct btrfs_root_item);
519 len = btrfs_item_size_nr(leaf, slot);
521 memset(&root_item, 0, sizeof(root_item));
522 read_extent_buffer(leaf, &root_item, (unsigned long)ri, len);
523 root_flags_to_str(btrfs_root_flags(&root_item), flags_str);
525 printf("\t\troot data bytenr %llu level %d dirid %llu refs %u gen %llu lastsnap %llu\n",
526 (unsigned long long)btrfs_root_bytenr(&root_item),
527 btrfs_root_level(&root_item),
528 (unsigned long long)btrfs_root_dirid(&root_item),
529 btrfs_root_refs(&root_item),
530 (unsigned long long)btrfs_root_generation(&root_item),
531 (unsigned long long)btrfs_root_last_snapshot(&root_item));
532 printf("\t\tflags 0x%llx(%s)\n", btrfs_root_flags(&root_item),
535 if (root_item.generation == root_item.generation_v2) {
536 uuid_unparse(root_item.uuid, uuid_str);
537 printf("\t\tuuid %s\n", uuid_str);
538 if (count_bytes(root_item.parent_uuid, BTRFS_UUID_SIZE, 0) != BTRFS_UUID_SIZE) {
539 uuid_unparse(root_item.parent_uuid, uuid_str);
540 printf("\t\tparent_uuid %s\n", uuid_str);
542 if (count_bytes(root_item.received_uuid, BTRFS_UUID_SIZE, 0) != BTRFS_UUID_SIZE) {
543 uuid_unparse(root_item.received_uuid, uuid_str);
544 printf("\t\treceived_uuid %s\n", uuid_str);
546 if (root_item.ctransid) {
547 printf("\t\tctransid %llu otransid %llu stransid %llu rtransid %llu\n",
548 btrfs_root_ctransid(&root_item),
549 btrfs_root_otransid(&root_item),
550 btrfs_root_stransid(&root_item),
551 btrfs_root_rtransid(&root_item));
554 if (btrfs_root_refs(&root_item) == 0) {
555 struct btrfs_key drop_key;
556 btrfs_disk_key_to_cpu(&drop_key,
557 &root_item.drop_progress);
559 btrfs_print_key(&root_item.drop_progress);
560 printf(" level %d\n", root_item.drop_level);
564 static void print_free_space_header(struct extent_buffer *leaf, int slot)
566 struct btrfs_free_space_header *header;
567 struct btrfs_disk_key location;
569 header = btrfs_item_ptr(leaf, slot, struct btrfs_free_space_header);
570 btrfs_free_space_key(leaf, header, &location);
571 printf("\t\tlocation ");
572 btrfs_print_key(&location);
574 printf("\t\tcache generation %llu entries %llu bitmaps %llu\n",
575 (unsigned long long)btrfs_free_space_generation(leaf, header),
576 (unsigned long long)btrfs_free_space_entries(leaf, header),
577 (unsigned long long)btrfs_free_space_bitmaps(leaf, header));
580 static void print_key_type(u64 objectid, u8 type)
582 if (type == 0 && objectid == BTRFS_FREE_SPACE_OBJECTID) {
588 case BTRFS_INODE_ITEM_KEY:
589 printf("INODE_ITEM");
591 case BTRFS_INODE_REF_KEY:
594 case BTRFS_INODE_EXTREF_KEY:
595 printf("INODE_EXTREF");
597 case BTRFS_DIR_ITEM_KEY:
600 case BTRFS_DIR_INDEX_KEY:
603 case BTRFS_DIR_LOG_ITEM_KEY:
604 printf("DIR_LOG_ITEM");
606 case BTRFS_DIR_LOG_INDEX_KEY:
607 printf("DIR_LOG_INDEX");
609 case BTRFS_XATTR_ITEM_KEY:
610 printf("XATTR_ITEM");
612 case BTRFS_ORPHAN_ITEM_KEY:
613 printf("ORPHAN_ITEM");
615 case BTRFS_ROOT_ITEM_KEY:
618 case BTRFS_ROOT_REF_KEY:
621 case BTRFS_ROOT_BACKREF_KEY:
622 printf("ROOT_BACKREF");
624 case BTRFS_EXTENT_ITEM_KEY:
625 printf("EXTENT_ITEM");
627 case BTRFS_METADATA_ITEM_KEY:
628 printf("METADATA_ITEM");
630 case BTRFS_TREE_BLOCK_REF_KEY:
631 printf("TREE_BLOCK_REF");
633 case BTRFS_SHARED_BLOCK_REF_KEY:
634 printf("SHARED_BLOCK_REF");
636 case BTRFS_EXTENT_DATA_REF_KEY:
637 printf("EXTENT_DATA_REF");
639 case BTRFS_SHARED_DATA_REF_KEY:
640 printf("SHARED_DATA_REF");
642 case BTRFS_EXTENT_REF_V0_KEY:
643 printf("EXTENT_REF_V0");
645 case BTRFS_CSUM_ITEM_KEY:
648 case BTRFS_EXTENT_CSUM_KEY:
649 printf("EXTENT_CSUM");
651 case BTRFS_EXTENT_DATA_KEY:
652 printf("EXTENT_DATA");
654 case BTRFS_BLOCK_GROUP_ITEM_KEY:
655 printf("BLOCK_GROUP_ITEM");
657 case BTRFS_FREE_SPACE_INFO_KEY:
658 printf("FREE_SPACE_INFO");
660 case BTRFS_FREE_SPACE_EXTENT_KEY:
661 printf("FREE_SPACE_EXTENT");
663 case BTRFS_FREE_SPACE_BITMAP_KEY:
664 printf("FREE_SPACE_BITMAP");
666 case BTRFS_CHUNK_ITEM_KEY:
667 printf("CHUNK_ITEM");
669 case BTRFS_DEV_ITEM_KEY:
672 case BTRFS_DEV_EXTENT_KEY:
673 printf("DEV_EXTENT");
675 case BTRFS_BALANCE_ITEM_KEY:
676 printf("BALANCE_ITEM");
678 case BTRFS_DEV_REPLACE_KEY:
679 printf("DEV_REPLACE");
681 case BTRFS_STRING_ITEM_KEY:
682 printf("STRING_ITEM");
684 case BTRFS_QGROUP_STATUS_KEY:
685 printf("QGROUP_STATUS");
687 case BTRFS_QGROUP_RELATION_KEY:
688 printf("QGROUP_RELATION");
690 case BTRFS_QGROUP_INFO_KEY:
691 printf("QGROUP_INFO");
693 case BTRFS_QGROUP_LIMIT_KEY:
694 printf("QGROUP_LIMIT");
696 case BTRFS_DEV_STATS_KEY:
699 case BTRFS_UUID_KEY_SUBVOL:
700 printf("UUID_KEY_SUBVOL");
702 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
703 printf("UUID_KEY_RECEIVED_SUBVOL");
706 printf("UNKNOWN.%d", type);
710 static void print_objectid(u64 objectid, u8 type)
713 case BTRFS_DEV_EXTENT_KEY:
714 printf("%llu", (unsigned long long)objectid); /* device id */
716 case BTRFS_QGROUP_RELATION_KEY:
717 printf("%llu/%llu", btrfs_qgroup_level(objectid),
718 btrfs_qgroup_subvid(objectid));
720 case BTRFS_UUID_KEY_SUBVOL:
721 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
722 printf("0x%016llx", (unsigned long long)objectid);
727 case BTRFS_ROOT_TREE_OBJECTID:
728 if (type == BTRFS_DEV_ITEM_KEY)
733 case BTRFS_EXTENT_TREE_OBJECTID:
734 printf("EXTENT_TREE");
736 case BTRFS_CHUNK_TREE_OBJECTID:
737 printf("CHUNK_TREE");
739 case BTRFS_DEV_TREE_OBJECTID:
742 case BTRFS_FS_TREE_OBJECTID:
745 case BTRFS_ROOT_TREE_DIR_OBJECTID:
746 printf("ROOT_TREE_DIR");
748 case BTRFS_CSUM_TREE_OBJECTID:
751 case BTRFS_BALANCE_OBJECTID:
754 case BTRFS_ORPHAN_OBJECTID:
757 case BTRFS_TREE_LOG_OBJECTID:
760 case BTRFS_TREE_LOG_FIXUP_OBJECTID:
763 case BTRFS_TREE_RELOC_OBJECTID:
764 printf("TREE_RELOC");
766 case BTRFS_DATA_RELOC_TREE_OBJECTID:
767 printf("DATA_RELOC_TREE");
769 case BTRFS_EXTENT_CSUM_OBJECTID:
770 printf("EXTENT_CSUM");
772 case BTRFS_FREE_SPACE_OBJECTID:
773 printf("FREE_SPACE");
775 case BTRFS_FREE_INO_OBJECTID:
778 case BTRFS_QUOTA_TREE_OBJECTID:
779 printf("QUOTA_TREE");
781 case BTRFS_UUID_TREE_OBJECTID:
784 case BTRFS_FREE_SPACE_TREE_OBJECTID:
785 printf("FREE_SPACE_TREE");
787 case BTRFS_MULTIPLE_OBJECTIDS:
793 case BTRFS_FIRST_CHUNK_TREE_OBJECTID:
794 if (type == BTRFS_CHUNK_ITEM_KEY) {
795 printf("FIRST_CHUNK_TREE");
800 printf("%llu", (unsigned long long)objectid);
804 void btrfs_print_key(struct btrfs_disk_key *disk_key)
806 u64 objectid = btrfs_disk_key_objectid(disk_key);
807 u8 type = btrfs_disk_key_type(disk_key);
808 u64 offset = btrfs_disk_key_offset(disk_key);
811 print_objectid(objectid, type);
813 print_key_type(objectid, type);
815 case BTRFS_QGROUP_RELATION_KEY:
816 case BTRFS_QGROUP_INFO_KEY:
817 case BTRFS_QGROUP_LIMIT_KEY:
818 printf(" %llu/%llu)", btrfs_qgroup_level(offset),
819 btrfs_qgroup_subvid(offset));
821 case BTRFS_UUID_KEY_SUBVOL:
822 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
823 printf(" 0x%016llx)", (unsigned long long)offset);
826 if (offset == (u64)-1)
829 printf(" %llu)", (unsigned long long)offset);
834 static void print_uuid_item(struct extent_buffer *l, unsigned long offset,
837 if (item_size & (sizeof(u64) - 1)) {
838 printf("btrfs: uuid item with illegal size %lu!\n",
839 (unsigned long)item_size);
845 read_extent_buffer(l, &subvol_id, offset, sizeof(u64));
846 printf("\t\tsubvol_id %llu\n",
847 (unsigned long long)le64_to_cpu(subvol_id));
848 item_size -= sizeof(u64);
849 offset += sizeof(u64);
853 /* Caller should ensure sizeof(*ret) >= 29 "NODATASUM|NODATACOW|READONLY" */
854 static void inode_flags_to_str(u64 flags, char *ret)
858 if (flags & BTRFS_INODE_NODATASUM) {
860 strcpy(ret, "NODATASUM");
862 if (flags & BTRFS_INODE_NODATACOW) {
867 strcat(ret, "NODATACOW");
869 if (flags & BTRFS_INODE_READONLY) {
874 strcat(ret, "READONLY");
880 void btrfs_print_leaf(struct btrfs_root *root, struct extent_buffer *l)
884 struct btrfs_item *item;
885 struct btrfs_dir_item *di;
886 struct btrfs_inode_item *ii;
887 struct btrfs_file_extent_item *fi;
888 struct btrfs_block_group_item *bi;
889 struct btrfs_extent_data_ref *dref;
890 struct btrfs_shared_data_ref *sref;
891 struct btrfs_inode_ref *iref;
892 struct btrfs_inode_extref *iref2;
893 struct btrfs_dev_extent *dev_extent;
894 struct btrfs_disk_key disk_key;
895 struct btrfs_block_group_item bg_item;
896 struct btrfs_free_space_info *free_info;
897 struct btrfs_dir_log_item *dlog;
898 struct btrfs_qgroup_info_item *qg_info;
899 struct btrfs_qgroup_limit_item *qg_limit;
900 struct btrfs_qgroup_status_item *qg_status;
901 u32 nr = btrfs_header_nritems(l);
906 printf("leaf %llu items %d free space %d generation %llu owner %llu\n",
907 (unsigned long long)btrfs_header_bytenr(l), nr,
908 btrfs_leaf_free_space(root, l),
909 (unsigned long long)btrfs_header_generation(l),
910 (unsigned long long)btrfs_header_owner(l));
913 for (i = 0 ; i < nr ; i++) {
914 item = btrfs_item_nr(i);
915 btrfs_item_key(l, &disk_key, i);
916 objectid = btrfs_disk_key_objectid(&disk_key);
917 type = btrfs_disk_key_type(&disk_key);
918 printf("\titem %d ", i);
919 btrfs_print_key(&disk_key);
920 printf(" itemoff %d itemsize %d\n",
921 btrfs_item_offset(l, item),
922 btrfs_item_size(l, item));
924 if (type == 0 && objectid == BTRFS_FREE_SPACE_OBJECTID)
925 print_free_space_header(l, i);
928 case BTRFS_INODE_ITEM_KEY:
929 memset(flags_str, 0, sizeof(flags_str));
930 ii = btrfs_item_ptr(l, i, struct btrfs_inode_item);
931 inode_flags_to_str(btrfs_inode_flags(l, ii), flags_str);
932 printf("\t\tinode generation %llu transid %llu size %llu nbytes %llu\n"
933 "\t\tblock group %llu mode %o links %u uid %u gid %u\n"
934 "\t\trdev %llu flags 0x%llx(%s)\n",
935 (unsigned long long)btrfs_inode_generation(l, ii),
936 (unsigned long long)btrfs_inode_transid(l, ii),
937 (unsigned long long)btrfs_inode_size(l, ii),
938 (unsigned long long)btrfs_inode_nbytes(l, ii),
939 (unsigned long long)btrfs_inode_block_group(l,ii),
940 btrfs_inode_mode(l, ii),
941 btrfs_inode_nlink(l, ii),
942 btrfs_inode_uid(l, ii),
943 btrfs_inode_gid(l, ii),
944 (unsigned long long)btrfs_inode_rdev(l,ii),
945 (unsigned long long)btrfs_inode_flags(l,ii),
948 case BTRFS_INODE_REF_KEY:
949 iref = btrfs_item_ptr(l, i, struct btrfs_inode_ref);
950 print_inode_ref_item(l, item, iref);
952 case BTRFS_INODE_EXTREF_KEY:
953 iref2 = btrfs_item_ptr(l, i, struct btrfs_inode_extref);
954 print_inode_extref_item(l, item, iref2);
956 case BTRFS_DIR_ITEM_KEY:
957 case BTRFS_DIR_INDEX_KEY:
958 case BTRFS_XATTR_ITEM_KEY:
959 di = btrfs_item_ptr(l, i, struct btrfs_dir_item);
960 print_dir_item(l, item, di);
962 case BTRFS_DIR_LOG_INDEX_KEY:
963 case BTRFS_DIR_LOG_ITEM_KEY:
964 dlog = btrfs_item_ptr(l, i, struct btrfs_dir_log_item);
965 printf("\t\tdir log end %Lu\n",
966 (unsigned long long)btrfs_dir_log_end(l, dlog));
968 case BTRFS_ORPHAN_ITEM_KEY:
969 printf("\t\torphan item\n");
971 case BTRFS_ROOT_ITEM_KEY:
974 case BTRFS_ROOT_REF_KEY:
975 print_root_ref(l, i, "ref");
977 case BTRFS_ROOT_BACKREF_KEY:
978 print_root_ref(l, i, "backref");
980 case BTRFS_EXTENT_ITEM_KEY:
981 print_extent_item(l, i, 0);
983 case BTRFS_METADATA_ITEM_KEY:
984 print_extent_item(l, i, 1);
986 case BTRFS_TREE_BLOCK_REF_KEY:
987 printf("\t\ttree block backref\n");
989 case BTRFS_SHARED_BLOCK_REF_KEY:
990 printf("\t\tshared block backref\n");
992 case BTRFS_EXTENT_DATA_REF_KEY:
993 dref = btrfs_item_ptr(l, i, struct btrfs_extent_data_ref);
994 printf("\t\textent data backref root %llu "
995 "objectid %llu offset %llu count %u\n",
996 (unsigned long long)btrfs_extent_data_ref_root(l, dref),
997 (unsigned long long)btrfs_extent_data_ref_objectid(l, dref),
998 (unsigned long long)btrfs_extent_data_ref_offset(l, dref),
999 btrfs_extent_data_ref_count(l, dref));
1001 case BTRFS_SHARED_DATA_REF_KEY:
1002 sref = btrfs_item_ptr(l, i, struct btrfs_shared_data_ref);
1003 printf("\t\tshared data backref count %u\n",
1004 btrfs_shared_data_ref_count(l, sref));
1006 case BTRFS_EXTENT_REF_V0_KEY:
1007 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1008 print_extent_ref_v0(l, i);
1013 case BTRFS_CSUM_ITEM_KEY:
1014 printf("\t\tcsum item\n");
1016 case BTRFS_EXTENT_CSUM_KEY:
1017 printf("\t\textent csum item\n");
1019 case BTRFS_EXTENT_DATA_KEY:
1020 fi = btrfs_item_ptr(l, i,
1021 struct btrfs_file_extent_item);
1022 print_file_extent_item(l, item, i, fi);
1024 case BTRFS_BLOCK_GROUP_ITEM_KEY:
1025 bi = btrfs_item_ptr(l, i,
1026 struct btrfs_block_group_item);
1027 read_extent_buffer(l, &bg_item, (unsigned long)bi,
1029 memset(flags_str, 0, sizeof(flags_str));
1030 bg_flags_to_str(btrfs_block_group_flags(&bg_item),
1032 printf("\t\tblock group used %llu chunk_objectid %llu flags %s\n",
1033 (unsigned long long)btrfs_block_group_used(&bg_item),
1034 (unsigned long long)btrfs_block_group_chunk_objectid(&bg_item),
1037 case BTRFS_FREE_SPACE_INFO_KEY:
1038 free_info = btrfs_item_ptr(l, i, struct btrfs_free_space_info);
1039 printf("\t\tfree space info extent count %u flags %u\n",
1040 (unsigned)btrfs_free_space_extent_count(l, free_info),
1041 (unsigned)btrfs_free_space_flags(l, free_info));
1043 case BTRFS_FREE_SPACE_EXTENT_KEY:
1044 printf("\t\tfree space extent\n");
1046 case BTRFS_FREE_SPACE_BITMAP_KEY:
1047 printf("\t\tfree space bitmap\n");
1049 case BTRFS_CHUNK_ITEM_KEY:
1050 print_chunk(l, btrfs_item_ptr(l, i, struct btrfs_chunk));
1052 case BTRFS_DEV_ITEM_KEY:
1053 print_dev_item(l, btrfs_item_ptr(l, i,
1054 struct btrfs_dev_item));
1056 case BTRFS_DEV_EXTENT_KEY:
1057 dev_extent = btrfs_item_ptr(l, i,
1058 struct btrfs_dev_extent);
1059 printf("\t\tdev extent chunk_tree %llu\n"
1060 "\t\tchunk objectid %llu chunk offset %llu "
1062 (unsigned long long)
1063 btrfs_dev_extent_chunk_tree(l, dev_extent),
1064 (unsigned long long)
1065 btrfs_dev_extent_chunk_objectid(l, dev_extent),
1066 (unsigned long long)
1067 btrfs_dev_extent_chunk_offset(l, dev_extent),
1068 (unsigned long long)
1069 btrfs_dev_extent_length(l, dev_extent));
1071 case BTRFS_QGROUP_STATUS_KEY:
1072 qg_status = btrfs_item_ptr(l, i,
1073 struct btrfs_qgroup_status_item);
1074 memset(flags_str, 0, sizeof(flags_str));
1075 qgroup_flags_to_str(btrfs_qgroup_status_flags(l, qg_status),
1077 printf("\t\tversion %llu generation %llu flags %s "
1079 (unsigned long long)
1080 btrfs_qgroup_status_version(l, qg_status),
1081 (unsigned long long)
1082 btrfs_qgroup_status_generation(l, qg_status),
1084 (unsigned long long)
1085 btrfs_qgroup_status_rescan(l, qg_status));
1087 case BTRFS_QGROUP_RELATION_KEY:
1089 case BTRFS_QGROUP_INFO_KEY:
1090 qg_info = btrfs_item_ptr(l, i,
1091 struct btrfs_qgroup_info_item);
1092 printf("\t\tgeneration %llu\n"
1093 "\t\treferenced %llu referenced compressed %llu\n"
1094 "\t\texclusive %llu exclusive compressed %llu\n",
1095 (unsigned long long)
1096 btrfs_qgroup_info_generation(l, qg_info),
1097 (unsigned long long)
1098 btrfs_qgroup_info_referenced(l, qg_info),
1099 (unsigned long long)
1100 btrfs_qgroup_info_referenced_compressed(l,
1102 (unsigned long long)
1103 btrfs_qgroup_info_exclusive(l, qg_info),
1104 (unsigned long long)
1105 btrfs_qgroup_info_exclusive_compressed(l,
1108 case BTRFS_QGROUP_LIMIT_KEY:
1109 qg_limit = btrfs_item_ptr(l, i,
1110 struct btrfs_qgroup_limit_item);
1111 printf("\t\tflags %llx\n"
1112 "\t\tmax referenced %lld max exclusive %lld\n"
1113 "\t\trsv referenced %lld rsv exclusive %lld\n",
1114 (unsigned long long)
1115 btrfs_qgroup_limit_flags(l, qg_limit),
1117 btrfs_qgroup_limit_max_referenced(l, qg_limit),
1119 btrfs_qgroup_limit_max_exclusive(l, qg_limit),
1121 btrfs_qgroup_limit_rsv_referenced(l, qg_limit),
1123 btrfs_qgroup_limit_rsv_exclusive(l, qg_limit));
1125 case BTRFS_UUID_KEY_SUBVOL:
1126 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
1127 print_uuid_item(l, btrfs_item_ptr_offset(l, i),
1128 btrfs_item_size_nr(l, i));
1130 case BTRFS_STRING_ITEM_KEY:
1131 /* dirty, but it's simple */
1132 str = l->data + btrfs_item_ptr_offset(l, i);
1133 printf("\t\titem data %.*s\n", btrfs_item_size(l, item), str);
1135 case BTRFS_DEV_STATS_KEY:
1136 printf("\t\tdevice stats\n");
1143 void btrfs_print_tree(struct btrfs_root *root, struct extent_buffer *eb, int follow)
1148 struct btrfs_disk_key disk_key;
1149 struct btrfs_key key;
1153 nr = btrfs_header_nritems(eb);
1154 if (btrfs_is_leaf(eb)) {
1155 btrfs_print_leaf(root, eb);
1158 printf("node %llu level %d items %d free %u generation %llu owner %llu\n",
1159 (unsigned long long)eb->start,
1160 btrfs_header_level(eb), nr,
1161 (u32)BTRFS_NODEPTRS_PER_BLOCK(root) - nr,
1162 (unsigned long long)btrfs_header_generation(eb),
1163 (unsigned long long)btrfs_header_owner(eb));
1166 size = root->nodesize;
1167 for (i = 0; i < nr; i++) {
1168 u64 blocknr = btrfs_node_blockptr(eb, i);
1169 btrfs_node_key(eb, &disk_key, i);
1170 btrfs_disk_key_to_cpu(&key, &disk_key);
1172 btrfs_print_key(&disk_key);
1173 printf(" block %llu (%llu) gen %llu\n",
1174 (unsigned long long)blocknr,
1175 (unsigned long long)blocknr / size,
1176 (unsigned long long)btrfs_node_ptr_generation(eb, i));
1182 for (i = 0; i < nr; i++) {
1183 struct extent_buffer *next = read_tree_block(root,
1184 btrfs_node_blockptr(eb, i),
1186 btrfs_node_ptr_generation(eb, i));
1187 if (!extent_buffer_uptodate(next)) {
1188 fprintf(stderr, "failed to read %llu in tree %llu\n",
1189 (unsigned long long)btrfs_node_blockptr(eb, i),
1190 (unsigned long long)btrfs_header_owner(eb));
1193 if (btrfs_is_leaf(next) &&
1194 btrfs_header_level(eb) != 1)
1196 if (btrfs_header_level(next) !=
1197 btrfs_header_level(eb) - 1)
1199 btrfs_print_tree(root, next, 1);
1200 free_extent_buffer(next);