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 void print_chunk(struct extent_buffer *eb, struct btrfs_chunk *chunk)
208 int num_stripes = btrfs_chunk_num_stripes(eb, chunk);
210 char chunk_flags_str[32] = {0};
212 bg_flags_to_str(btrfs_chunk_type(eb, chunk), chunk_flags_str);
213 printf("\t\tchunk length %llu owner %llu type %s num_stripes %d\n",
214 (unsigned long long)btrfs_chunk_length(eb, chunk),
215 (unsigned long long)btrfs_chunk_owner(eb, chunk),
216 chunk_flags_str, num_stripes);
217 for (i = 0 ; i < num_stripes ; i++) {
218 printf("\t\t\tstripe %d devid %llu offset %llu\n", i,
219 (unsigned long long)btrfs_stripe_devid_nr(eb, chunk, i),
220 (unsigned long long)btrfs_stripe_offset_nr(eb, chunk, i));
224 static void print_dev_item(struct extent_buffer *eb,
225 struct btrfs_dev_item *dev_item)
227 char disk_uuid_c[BTRFS_UUID_UNPARSED_SIZE];
228 u8 disk_uuid[BTRFS_UUID_SIZE];
230 read_extent_buffer(eb, disk_uuid,
231 (unsigned long)btrfs_device_uuid(dev_item),
233 uuid_unparse(disk_uuid, disk_uuid_c);
234 printf("\t\tdev item devid %llu "
235 "total_bytes %llu bytes used %Lu\n"
237 (unsigned long long)btrfs_device_id(eb, dev_item),
238 (unsigned long long)btrfs_device_total_bytes(eb, dev_item),
239 (unsigned long long)btrfs_device_bytes_used(eb, dev_item),
243 static void print_uuids(struct extent_buffer *eb)
245 char fs_uuid[BTRFS_UUID_UNPARSED_SIZE];
246 char chunk_uuid[BTRFS_UUID_UNPARSED_SIZE];
247 u8 disk_uuid[BTRFS_UUID_SIZE];
249 read_extent_buffer(eb, disk_uuid, btrfs_header_fsid(),
252 fs_uuid[BTRFS_UUID_UNPARSED_SIZE - 1] = '\0';
253 uuid_unparse(disk_uuid, fs_uuid);
255 read_extent_buffer(eb, disk_uuid,
256 btrfs_header_chunk_tree_uuid(eb),
259 chunk_uuid[BTRFS_UUID_UNPARSED_SIZE - 1] = '\0';
260 uuid_unparse(disk_uuid, chunk_uuid);
261 printf("fs uuid %s\nchunk uuid %s\n", fs_uuid, chunk_uuid);
264 static void print_file_extent_item(struct extent_buffer *eb,
265 struct btrfs_item *item,
267 struct btrfs_file_extent_item *fi)
269 int extent_type = btrfs_file_extent_type(eb, fi);
271 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
272 printf("\t\tinline extent data size %u "
273 "ram %u compress %d\n",
274 btrfs_file_extent_inline_item_len(eb, item),
275 btrfs_file_extent_inline_len(eb, slot, fi),
276 btrfs_file_extent_compression(eb, fi));
279 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
280 printf("\t\tprealloc data disk byte %llu nr %llu\n",
281 (unsigned long long)btrfs_file_extent_disk_bytenr(eb, fi),
282 (unsigned long long)btrfs_file_extent_disk_num_bytes(eb, fi));
283 printf("\t\tprealloc data offset %llu nr %llu\n",
284 (unsigned long long)btrfs_file_extent_offset(eb, fi),
285 (unsigned long long)btrfs_file_extent_num_bytes(eb, fi));
288 printf("\t\textent data disk byte %llu nr %llu\n",
289 (unsigned long long)btrfs_file_extent_disk_bytenr(eb, fi),
290 (unsigned long long)btrfs_file_extent_disk_num_bytes(eb, fi));
291 printf("\t\textent data offset %llu nr %llu ram %llu\n",
292 (unsigned long long)btrfs_file_extent_offset(eb, fi),
293 (unsigned long long)btrfs_file_extent_num_bytes(eb, fi),
294 (unsigned long long)btrfs_file_extent_ram_bytes(eb, fi));
295 printf("\t\textent compression %d\n",
296 btrfs_file_extent_compression(eb, fi));
299 /* Caller should ensure sizeof(*ret) >= 16("DATA|TREE_BLOCK") */
300 static void extent_flags_to_str(u64 flags, char *ret)
304 if (flags & BTRFS_EXTENT_FLAG_DATA) {
308 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
313 strcat(ret, "TREE_BLOCK");
317 void print_extent_item(struct extent_buffer *eb, int slot, int metadata)
319 struct btrfs_extent_item *ei;
320 struct btrfs_extent_inline_ref *iref;
321 struct btrfs_extent_data_ref *dref;
322 struct btrfs_shared_data_ref *sref;
323 struct btrfs_disk_key key;
327 u32 item_size = btrfs_item_size_nr(eb, slot);
330 char flags_str[32] = {0};
332 if (item_size < sizeof(*ei)) {
333 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
334 struct btrfs_extent_item_v0 *ei0;
335 BUG_ON(item_size != sizeof(*ei0));
336 ei0 = btrfs_item_ptr(eb, slot, struct btrfs_extent_item_v0);
337 printf("\t\textent refs %u\n",
338 btrfs_extent_refs_v0(eb, ei0));
345 ei = btrfs_item_ptr(eb, slot, struct btrfs_extent_item);
346 flags = btrfs_extent_flags(eb, ei);
347 extent_flags_to_str(flags, flags_str);
349 printf("\t\textent refs %llu gen %llu flags %s\n",
350 (unsigned long long)btrfs_extent_refs(eb, ei),
351 (unsigned long long)btrfs_extent_generation(eb, ei),
354 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !metadata) {
355 struct btrfs_tree_block_info *info;
356 info = (struct btrfs_tree_block_info *)(ei + 1);
357 btrfs_tree_block_key(eb, info, &key);
358 printf("\t\ttree block ");
359 btrfs_print_key(&key);
360 printf(" level %d\n", btrfs_tree_block_level(eb, info));
361 iref = (struct btrfs_extent_inline_ref *)(info + 1);
362 } else if (metadata) {
363 struct btrfs_key tmp;
365 btrfs_item_key_to_cpu(eb, &tmp, slot);
366 printf("\t\ttree block skinny level %d\n", (int)tmp.offset);
367 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
369 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
372 ptr = (unsigned long)iref;
373 end = (unsigned long)ei + item_size;
375 iref = (struct btrfs_extent_inline_ref *)ptr;
376 type = btrfs_extent_inline_ref_type(eb, iref);
377 offset = btrfs_extent_inline_ref_offset(eb, iref);
379 case BTRFS_TREE_BLOCK_REF_KEY:
380 printf("\t\ttree block backref root %llu\n",
381 (unsigned long long)offset);
383 case BTRFS_SHARED_BLOCK_REF_KEY:
384 printf("\t\tshared block backref parent %llu\n",
385 (unsigned long long)offset);
387 case BTRFS_EXTENT_DATA_REF_KEY:
388 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
389 printf("\t\textent data backref root %llu "
390 "objectid %llu offset %llu count %u\n",
391 (unsigned long long)btrfs_extent_data_ref_root(eb, dref),
392 (unsigned long long)btrfs_extent_data_ref_objectid(eb, dref),
393 (unsigned long long)btrfs_extent_data_ref_offset(eb, dref),
394 btrfs_extent_data_ref_count(eb, dref));
396 case BTRFS_SHARED_DATA_REF_KEY:
397 sref = (struct btrfs_shared_data_ref *)(iref + 1);
398 printf("\t\tshared data backref parent %llu count %u\n",
399 (unsigned long long)offset,
400 btrfs_shared_data_ref_count(eb, sref));
405 ptr += btrfs_extent_inline_ref_size(type);
410 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
411 static void print_extent_ref_v0(struct extent_buffer *eb, int slot)
413 struct btrfs_extent_ref_v0 *ref0;
415 ref0 = btrfs_item_ptr(eb, slot, struct btrfs_extent_ref_v0);
416 printf("\t\textent back ref root %llu gen %llu "
417 "owner %llu num_refs %lu\n",
418 (unsigned long long)btrfs_ref_root_v0(eb, ref0),
419 (unsigned long long)btrfs_ref_generation_v0(eb, ref0),
420 (unsigned long long)btrfs_ref_objectid_v0(eb, ref0),
421 (unsigned long)btrfs_ref_count_v0(eb, ref0));
425 static void print_root_ref(struct extent_buffer *leaf, int slot, char *tag)
427 struct btrfs_root_ref *ref;
428 char namebuf[BTRFS_NAME_LEN];
431 ref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
432 namelen = btrfs_root_ref_name_len(leaf, ref);
433 read_extent_buffer(leaf, namebuf, (unsigned long)(ref + 1), namelen);
434 printf("\t\troot %s key dirid %llu sequence %llu name %.*s\n", tag,
435 (unsigned long long)btrfs_root_ref_dirid(leaf, ref),
436 (unsigned long long)btrfs_root_ref_sequence(leaf, ref),
440 static int count_bytes(void *buf, int len, char b)
444 for (i = 0; i < len; i++) {
445 if (((char*)buf)[i] == b)
451 static void print_root(struct extent_buffer *leaf, int slot)
453 struct btrfs_root_item *ri;
454 struct btrfs_root_item root_item;
456 char uuid_str[BTRFS_UUID_UNPARSED_SIZE];
458 ri = btrfs_item_ptr(leaf, slot, struct btrfs_root_item);
459 len = btrfs_item_size_nr(leaf, slot);
461 memset(&root_item, 0, sizeof(root_item));
462 read_extent_buffer(leaf, &root_item, (unsigned long)ri, len);
464 printf("\t\troot data bytenr %llu level %d dirid %llu refs %u gen %llu\n",
465 (unsigned long long)btrfs_root_bytenr(&root_item),
466 btrfs_root_level(&root_item),
467 (unsigned long long)btrfs_root_dirid(&root_item),
468 btrfs_root_refs(&root_item),
469 (unsigned long long)btrfs_root_generation(&root_item));
471 if (root_item.generation == root_item.generation_v2) {
472 uuid_unparse(root_item.uuid, uuid_str);
473 printf("\t\tuuid %s\n", uuid_str);
474 if (count_bytes(root_item.parent_uuid, BTRFS_UUID_SIZE, 0) != BTRFS_UUID_SIZE) {
475 uuid_unparse(root_item.parent_uuid, uuid_str);
476 printf("\t\tparent_uuid %s\n", uuid_str);
478 if (count_bytes(root_item.received_uuid, BTRFS_UUID_SIZE, 0) != BTRFS_UUID_SIZE) {
479 uuid_unparse(root_item.received_uuid, uuid_str);
480 printf("\t\treceived_uuid %s\n", uuid_str);
482 if (root_item.ctransid) {
483 printf("\t\tctransid %llu otransid %llu stransid %llu rtransid %llu\n",
484 btrfs_root_ctransid(&root_item),
485 btrfs_root_otransid(&root_item),
486 btrfs_root_stransid(&root_item),
487 btrfs_root_rtransid(&root_item));
490 if (btrfs_root_refs(&root_item) == 0) {
491 struct btrfs_key drop_key;
492 btrfs_disk_key_to_cpu(&drop_key,
493 &root_item.drop_progress);
495 btrfs_print_key(&root_item.drop_progress);
496 printf(" level %d\n", root_item.drop_level);
500 static void print_free_space_header(struct extent_buffer *leaf, int slot)
502 struct btrfs_free_space_header *header;
503 struct btrfs_disk_key location;
505 header = btrfs_item_ptr(leaf, slot, struct btrfs_free_space_header);
506 btrfs_free_space_key(leaf, header, &location);
507 printf("\t\tlocation ");
508 btrfs_print_key(&location);
510 printf("\t\tcache generation %llu entries %llu bitmaps %llu\n",
511 (unsigned long long)btrfs_free_space_generation(leaf, header),
512 (unsigned long long)btrfs_free_space_entries(leaf, header),
513 (unsigned long long)btrfs_free_space_bitmaps(leaf, header));
516 static void print_key_type(u64 objectid, u8 type)
518 if (type == 0 && objectid == BTRFS_FREE_SPACE_OBJECTID) {
524 case BTRFS_INODE_ITEM_KEY:
525 printf("INODE_ITEM");
527 case BTRFS_INODE_REF_KEY:
530 case BTRFS_INODE_EXTREF_KEY:
531 printf("INODE_EXTREF");
533 case BTRFS_DIR_ITEM_KEY:
536 case BTRFS_DIR_INDEX_KEY:
539 case BTRFS_DIR_LOG_ITEM_KEY:
540 printf("DIR_LOG_ITEM");
542 case BTRFS_DIR_LOG_INDEX_KEY:
543 printf("DIR_LOG_INDEX");
545 case BTRFS_XATTR_ITEM_KEY:
546 printf("XATTR_ITEM");
548 case BTRFS_ORPHAN_ITEM_KEY:
549 printf("ORPHAN_ITEM");
551 case BTRFS_ROOT_ITEM_KEY:
554 case BTRFS_ROOT_REF_KEY:
557 case BTRFS_ROOT_BACKREF_KEY:
558 printf("ROOT_BACKREF");
560 case BTRFS_EXTENT_ITEM_KEY:
561 printf("EXTENT_ITEM");
563 case BTRFS_METADATA_ITEM_KEY:
564 printf("METADATA_ITEM");
566 case BTRFS_TREE_BLOCK_REF_KEY:
567 printf("TREE_BLOCK_REF");
569 case BTRFS_SHARED_BLOCK_REF_KEY:
570 printf("SHARED_BLOCK_REF");
572 case BTRFS_EXTENT_DATA_REF_KEY:
573 printf("EXTENT_DATA_REF");
575 case BTRFS_SHARED_DATA_REF_KEY:
576 printf("SHARED_DATA_REF");
578 case BTRFS_EXTENT_REF_V0_KEY:
579 printf("EXTENT_REF_V0");
581 case BTRFS_CSUM_ITEM_KEY:
584 case BTRFS_EXTENT_CSUM_KEY:
585 printf("EXTENT_CSUM");
587 case BTRFS_EXTENT_DATA_KEY:
588 printf("EXTENT_DATA");
590 case BTRFS_BLOCK_GROUP_ITEM_KEY:
591 printf("BLOCK_GROUP_ITEM");
593 case BTRFS_CHUNK_ITEM_KEY:
594 printf("CHUNK_ITEM");
596 case BTRFS_DEV_ITEM_KEY:
599 case BTRFS_DEV_EXTENT_KEY:
600 printf("DEV_EXTENT");
602 case BTRFS_BALANCE_ITEM_KEY:
603 printf("BALANCE_ITEM");
605 case BTRFS_DEV_REPLACE_KEY:
606 printf("DEV_REPLACE_ITEM");
608 case BTRFS_STRING_ITEM_KEY:
609 printf("STRING_ITEM");
611 case BTRFS_QGROUP_STATUS_KEY:
612 printf("BTRFS_STATUS_KEY");
614 case BTRFS_QGROUP_RELATION_KEY:
615 printf("BTRFS_QGROUP_RELATION_KEY");
617 case BTRFS_QGROUP_INFO_KEY:
618 printf("BTRFS_QGROUP_INFO_KEY");
620 case BTRFS_QGROUP_LIMIT_KEY:
621 printf("BTRFS_QGROUP_LIMIT_KEY");
623 case BTRFS_DEV_STATS_KEY:
624 printf("DEV_STATS_ITEM");
626 case BTRFS_UUID_KEY_SUBVOL:
627 printf("BTRFS_UUID_KEY_SUBVOL");
629 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
630 printf("BTRFS_UUID_KEY_RECEIVED_SUBVOL");
633 printf("UNKNOWN.%d", type);
637 static void print_objectid(u64 objectid, u8 type)
640 case BTRFS_DEV_EXTENT_KEY:
641 printf("%llu", (unsigned long long)objectid); /* device id */
643 case BTRFS_QGROUP_RELATION_KEY:
644 printf("%llu/%llu", objectid >> 48,
645 objectid & ((1ll << 48) - 1));
647 case BTRFS_UUID_KEY_SUBVOL:
648 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
649 printf("0x%016llx", (unsigned long long)objectid);
654 case BTRFS_ROOT_TREE_OBJECTID:
655 if (type == BTRFS_DEV_ITEM_KEY)
660 case BTRFS_EXTENT_TREE_OBJECTID:
661 printf("EXTENT_TREE");
663 case BTRFS_CHUNK_TREE_OBJECTID:
664 printf("CHUNK_TREE");
666 case BTRFS_DEV_TREE_OBJECTID:
669 case BTRFS_FS_TREE_OBJECTID:
672 case BTRFS_ROOT_TREE_DIR_OBJECTID:
673 printf("ROOT_TREE_DIR");
675 case BTRFS_CSUM_TREE_OBJECTID:
678 case BTRFS_BALANCE_OBJECTID:
681 case BTRFS_ORPHAN_OBJECTID:
684 case BTRFS_TREE_LOG_OBJECTID:
687 case BTRFS_TREE_LOG_FIXUP_OBJECTID:
690 case BTRFS_TREE_RELOC_OBJECTID:
691 printf("TREE_RELOC");
693 case BTRFS_DATA_RELOC_TREE_OBJECTID:
694 printf("DATA_RELOC_TREE");
696 case BTRFS_EXTENT_CSUM_OBJECTID:
697 printf("EXTENT_CSUM");
699 case BTRFS_FREE_SPACE_OBJECTID:
700 printf("FREE_SPACE");
702 case BTRFS_FREE_INO_OBJECTID:
705 case BTRFS_QUOTA_TREE_OBJECTID:
706 printf("QUOTA_TREE");
708 case BTRFS_UUID_TREE_OBJECTID:
711 case BTRFS_MULTIPLE_OBJECTIDS:
717 case BTRFS_FIRST_CHUNK_TREE_OBJECTID:
718 if (type == BTRFS_CHUNK_ITEM_KEY) {
719 printf("FIRST_CHUNK_TREE");
724 printf("%llu", (unsigned long long)objectid);
728 void btrfs_print_key(struct btrfs_disk_key *disk_key)
730 u64 objectid = btrfs_disk_key_objectid(disk_key);
731 u8 type = btrfs_disk_key_type(disk_key);
732 u64 offset = btrfs_disk_key_offset(disk_key);
735 print_objectid(objectid, type);
737 print_key_type(objectid, type);
739 case BTRFS_QGROUP_RELATION_KEY:
740 case BTRFS_QGROUP_INFO_KEY:
741 case BTRFS_QGROUP_LIMIT_KEY:
742 printf(" %llu/%llu)", (unsigned long long)(offset >> 48),
743 (unsigned long long)(offset & ((1ll << 48) - 1)));
745 case BTRFS_UUID_KEY_SUBVOL:
746 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
747 printf(" 0x%016llx)", (unsigned long long)offset);
750 if (offset == (u64)-1)
753 printf(" %llu)", (unsigned long long)offset);
758 static void print_uuid_item(struct extent_buffer *l, unsigned long offset,
761 if (item_size & (sizeof(u64) - 1)) {
762 printf("btrfs: uuid item with illegal size %lu!\n",
763 (unsigned long)item_size);
769 read_extent_buffer(l, &subvol_id, offset, sizeof(u64));
770 printf("\t\tsubvol_id %llu\n",
771 (unsigned long long)le64_to_cpu(subvol_id));
772 item_size -= sizeof(u64);
773 offset += sizeof(u64);
777 void btrfs_print_leaf(struct btrfs_root *root, struct extent_buffer *l)
781 struct btrfs_item *item;
782 struct btrfs_dir_item *di;
783 struct btrfs_inode_item *ii;
784 struct btrfs_file_extent_item *fi;
785 struct btrfs_block_group_item *bi;
786 struct btrfs_extent_data_ref *dref;
787 struct btrfs_shared_data_ref *sref;
788 struct btrfs_inode_ref *iref;
789 struct btrfs_inode_extref *iref2;
790 struct btrfs_dev_extent *dev_extent;
791 struct btrfs_disk_key disk_key;
792 struct btrfs_block_group_item bg_item;
793 struct btrfs_dir_log_item *dlog;
794 struct btrfs_qgroup_info_item *qg_info;
795 struct btrfs_qgroup_limit_item *qg_limit;
796 struct btrfs_qgroup_status_item *qg_status;
797 u32 nr = btrfs_header_nritems(l);
800 char bg_flags_str[32];
802 printf("leaf %llu items %d free space %d generation %llu owner %llu\n",
803 (unsigned long long)btrfs_header_bytenr(l), nr,
804 btrfs_leaf_free_space(root, l),
805 (unsigned long long)btrfs_header_generation(l),
806 (unsigned long long)btrfs_header_owner(l));
809 for (i = 0 ; i < nr ; i++) {
810 item = btrfs_item_nr(i);
811 btrfs_item_key(l, &disk_key, i);
812 objectid = btrfs_disk_key_objectid(&disk_key);
813 type = btrfs_disk_key_type(&disk_key);
814 printf("\titem %d ", i);
815 btrfs_print_key(&disk_key);
816 printf(" itemoff %d itemsize %d\n",
817 btrfs_item_offset(l, item),
818 btrfs_item_size(l, item));
820 if (type == 0 && objectid == BTRFS_FREE_SPACE_OBJECTID)
821 print_free_space_header(l, i);
824 case BTRFS_INODE_ITEM_KEY:
825 ii = btrfs_item_ptr(l, i, struct btrfs_inode_item);
826 printf("\t\tinode generation %llu transid %llu size %llu block group %llu mode %o links %u\n",
827 (unsigned long long)btrfs_inode_generation(l, ii),
828 (unsigned long long)btrfs_inode_transid(l, ii),
829 (unsigned long long)btrfs_inode_size(l, ii),
830 (unsigned long long)btrfs_inode_block_group(l,ii),
831 btrfs_inode_mode(l, ii),
832 btrfs_inode_nlink(l, ii));
834 case BTRFS_INODE_REF_KEY:
835 iref = btrfs_item_ptr(l, i, struct btrfs_inode_ref);
836 print_inode_ref_item(l, item, iref);
838 case BTRFS_INODE_EXTREF_KEY:
839 iref2 = btrfs_item_ptr(l, i, struct btrfs_inode_extref);
840 print_inode_extref_item(l, item, iref2);
842 case BTRFS_DIR_ITEM_KEY:
843 case BTRFS_DIR_INDEX_KEY:
844 case BTRFS_XATTR_ITEM_KEY:
845 di = btrfs_item_ptr(l, i, struct btrfs_dir_item);
846 print_dir_item(l, item, di);
848 case BTRFS_DIR_LOG_INDEX_KEY:
849 case BTRFS_DIR_LOG_ITEM_KEY:
850 dlog = btrfs_item_ptr(l, i, struct btrfs_dir_log_item);
851 printf("\t\tdir log end %Lu\n",
852 (unsigned long long)btrfs_dir_log_end(l, dlog));
854 case BTRFS_ORPHAN_ITEM_KEY:
855 printf("\t\torphan item\n");
857 case BTRFS_ROOT_ITEM_KEY:
860 case BTRFS_ROOT_REF_KEY:
861 print_root_ref(l, i, "ref");
863 case BTRFS_ROOT_BACKREF_KEY:
864 print_root_ref(l, i, "backref");
866 case BTRFS_EXTENT_ITEM_KEY:
867 print_extent_item(l, i, 0);
869 case BTRFS_METADATA_ITEM_KEY:
870 print_extent_item(l, i, 1);
872 case BTRFS_TREE_BLOCK_REF_KEY:
873 printf("\t\ttree block backref\n");
875 case BTRFS_SHARED_BLOCK_REF_KEY:
876 printf("\t\tshared block backref\n");
878 case BTRFS_EXTENT_DATA_REF_KEY:
879 dref = btrfs_item_ptr(l, i, struct btrfs_extent_data_ref);
880 printf("\t\textent data backref root %llu "
881 "objectid %llu offset %llu count %u\n",
882 (unsigned long long)btrfs_extent_data_ref_root(l, dref),
883 (unsigned long long)btrfs_extent_data_ref_objectid(l, dref),
884 (unsigned long long)btrfs_extent_data_ref_offset(l, dref),
885 btrfs_extent_data_ref_count(l, dref));
887 case BTRFS_SHARED_DATA_REF_KEY:
888 sref = btrfs_item_ptr(l, i, struct btrfs_shared_data_ref);
889 printf("\t\tshared data backref count %u\n",
890 btrfs_shared_data_ref_count(l, sref));
892 case BTRFS_EXTENT_REF_V0_KEY:
893 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
894 print_extent_ref_v0(l, i);
899 case BTRFS_CSUM_ITEM_KEY:
900 printf("\t\tcsum item\n");
902 case BTRFS_EXTENT_CSUM_KEY:
903 printf("\t\textent csum item\n");
905 case BTRFS_EXTENT_DATA_KEY:
906 fi = btrfs_item_ptr(l, i,
907 struct btrfs_file_extent_item);
908 print_file_extent_item(l, item, i, fi);
910 case BTRFS_BLOCK_GROUP_ITEM_KEY:
911 bi = btrfs_item_ptr(l, i,
912 struct btrfs_block_group_item);
913 read_extent_buffer(l, &bg_item, (unsigned long)bi,
915 memset(bg_flags_str, 0, sizeof(bg_flags_str));
916 bg_flags_to_str(btrfs_block_group_flags(&bg_item),
918 printf("\t\tblock group used %llu chunk_objectid %llu flags %s\n",
919 (unsigned long long)btrfs_block_group_used(&bg_item),
920 (unsigned long long)btrfs_block_group_chunk_objectid(&bg_item),
923 case BTRFS_CHUNK_ITEM_KEY:
924 print_chunk(l, btrfs_item_ptr(l, i, struct btrfs_chunk));
926 case BTRFS_DEV_ITEM_KEY:
927 print_dev_item(l, btrfs_item_ptr(l, i,
928 struct btrfs_dev_item));
930 case BTRFS_DEV_EXTENT_KEY:
931 dev_extent = btrfs_item_ptr(l, i,
932 struct btrfs_dev_extent);
933 printf("\t\tdev extent chunk_tree %llu\n"
934 "\t\tchunk objectid %llu chunk offset %llu "
937 btrfs_dev_extent_chunk_tree(l, dev_extent),
939 btrfs_dev_extent_chunk_objectid(l, dev_extent),
941 btrfs_dev_extent_chunk_offset(l, dev_extent),
943 btrfs_dev_extent_length(l, dev_extent));
945 case BTRFS_QGROUP_STATUS_KEY:
946 qg_status = btrfs_item_ptr(l, i,
947 struct btrfs_qgroup_status_item);
948 printf("\t\tversion %llu generation %llu flags %#llx "
951 btrfs_qgroup_status_version(l, qg_status),
953 btrfs_qgroup_status_generation(l, qg_status),
955 btrfs_qgroup_status_flags(l, qg_status),
957 btrfs_qgroup_status_scan(l, qg_status));
959 case BTRFS_QGROUP_RELATION_KEY:
961 case BTRFS_QGROUP_INFO_KEY:
962 qg_info = btrfs_item_ptr(l, i,
963 struct btrfs_qgroup_info_item);
964 printf("\t\tgeneration %llu\n"
965 "\t\treferenced %llu referenced compressed %llu\n"
966 "\t\texclusive %llu exclusive compressed %llu\n",
968 btrfs_qgroup_info_generation(l, qg_info),
970 btrfs_qgroup_info_referenced(l, qg_info),
972 btrfs_qgroup_info_referenced_compressed(l,
975 btrfs_qgroup_info_exclusive(l, qg_info),
977 btrfs_qgroup_info_exclusive_compressed(l,
980 case BTRFS_QGROUP_LIMIT_KEY:
981 qg_limit = btrfs_item_ptr(l, i,
982 struct btrfs_qgroup_limit_item);
983 printf("\t\tflags %llx\n"
984 "\t\tmax referenced %lld max exclusive %lld\n"
985 "\t\trsv referenced %lld rsv exclusive %lld\n",
987 btrfs_qgroup_limit_flags(l, qg_limit),
989 btrfs_qgroup_limit_max_referenced(l, qg_limit),
991 btrfs_qgroup_limit_max_exclusive(l, qg_limit),
993 btrfs_qgroup_limit_rsv_referenced(l, qg_limit),
995 btrfs_qgroup_limit_rsv_exclusive(l, qg_limit));
997 case BTRFS_UUID_KEY_SUBVOL:
998 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
999 print_uuid_item(l, btrfs_item_ptr_offset(l, i),
1000 btrfs_item_size_nr(l, i));
1002 case BTRFS_STRING_ITEM_KEY:
1003 /* dirty, but it's simple */
1004 str = l->data + btrfs_item_ptr_offset(l, i);
1005 printf("\t\titem data %.*s\n", btrfs_item_size(l, item), str);
1007 case BTRFS_DEV_STATS_KEY:
1008 printf("\t\tdevice stats\n");
1015 void btrfs_print_tree(struct btrfs_root *root, struct extent_buffer *eb, int follow)
1020 struct btrfs_disk_key disk_key;
1021 struct btrfs_key key;
1025 nr = btrfs_header_nritems(eb);
1026 if (btrfs_is_leaf(eb)) {
1027 btrfs_print_leaf(root, eb);
1030 printf("node %llu level %d items %d free %u generation %llu owner %llu\n",
1031 (unsigned long long)eb->start,
1032 btrfs_header_level(eb), nr,
1033 (u32)BTRFS_NODEPTRS_PER_BLOCK(root) - nr,
1034 (unsigned long long)btrfs_header_generation(eb),
1035 (unsigned long long)btrfs_header_owner(eb));
1038 size = btrfs_level_size(root, btrfs_header_level(eb) - 1);
1039 for (i = 0; i < nr; i++) {
1040 u64 blocknr = btrfs_node_blockptr(eb, i);
1041 btrfs_node_key(eb, &disk_key, i);
1042 btrfs_disk_key_to_cpu(&key, &disk_key);
1044 btrfs_print_key(&disk_key);
1045 printf(" block %llu (%llu) gen %llu\n",
1046 (unsigned long long)blocknr,
1047 (unsigned long long)blocknr / size,
1048 (unsigned long long)btrfs_node_ptr_generation(eb, i));
1054 for (i = 0; i < nr; i++) {
1055 struct extent_buffer *next = read_tree_block(root,
1056 btrfs_node_blockptr(eb, i),
1058 btrfs_node_ptr_generation(eb, i));
1060 fprintf(stderr, "failed to read %llu in tree %llu\n",
1061 (unsigned long long)btrfs_node_blockptr(eb, i),
1062 (unsigned long long)btrfs_header_owner(eb));
1065 if (btrfs_is_leaf(next) &&
1066 btrfs_header_level(eb) != 1)
1068 if (btrfs_header_level(next) !=
1069 btrfs_header_level(eb) - 1)
1071 btrfs_print_tree(root, next, 1);
1072 free_extent_buffer(next);