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\ttransid %llu data_len %u name_len %u\n",
89 btrfs_dir_transid(eb, di),
91 printf("\t\tname: %.*s\n", len, namebuf);
93 len = (data_len <= sizeof(namebuf))? data_len: sizeof(namebuf);
94 read_extent_buffer(eb, namebuf,
95 (unsigned long)(di + 1) + name_len, len);
96 printf("\t\tdata %.*s\n", len, namebuf);
98 len = sizeof(*di) + name_len + data_len;
99 di = (struct btrfs_dir_item *)((char *)di + len);
105 static int print_inode_extref_item(struct extent_buffer *eb,
106 struct btrfs_item *item,
107 struct btrfs_inode_extref *extref)
115 char namebuf[BTRFS_NAME_LEN];
117 total = btrfs_item_size(eb, item);
119 while (cur < total) {
120 index = btrfs_inode_extref_index(eb, extref);
121 name_len = btrfs_inode_extref_name_len(eb, extref);
122 parent_objid = btrfs_inode_extref_parent(eb, extref);
124 len = (name_len <= sizeof(namebuf))? name_len: sizeof(namebuf);
126 read_extent_buffer(eb, namebuf, (unsigned long)(extref->name), len);
128 printf("\t\tinode extref index %llu parent %llu namelen %u "
130 (unsigned long long)index,
131 (unsigned long long)parent_objid,
132 name_len, len, namebuf);
134 len = sizeof(*extref) + name_len;
135 extref = (struct btrfs_inode_extref *)((char *)extref + len);
141 static int print_inode_ref_item(struct extent_buffer *eb, struct btrfs_item *item,
142 struct btrfs_inode_ref *ref)
149 char namebuf[BTRFS_NAME_LEN];
150 total = btrfs_item_size(eb, item);
152 name_len = btrfs_inode_ref_name_len(eb, ref);
153 index = btrfs_inode_ref_index(eb, ref);
154 len = (name_len <= sizeof(namebuf))? name_len: sizeof(namebuf);
155 read_extent_buffer(eb, namebuf, (unsigned long)(ref + 1), len);
156 printf("\t\tinode ref index %llu namelen %u name: %.*s\n",
157 (unsigned long long)index, name_len, len, namebuf);
158 len = sizeof(*ref) + name_len;
159 ref = (struct btrfs_inode_ref *)((char *)ref + len);
165 /* Caller should ensure sizeof(*ret)>=21 "DATA|METADATA|RAID10" */
166 static void bg_flags_to_str(u64 flags, char *ret)
170 if (flags & BTRFS_BLOCK_GROUP_DATA) {
174 if (flags & BTRFS_BLOCK_GROUP_METADATA) {
177 strcat(ret, "METADATA");
179 if (flags & BTRFS_BLOCK_GROUP_SYSTEM) {
182 strcat(ret, "SYSTEM");
184 switch (flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
185 case BTRFS_BLOCK_GROUP_RAID0:
186 strcat(ret, "|RAID0");
188 case BTRFS_BLOCK_GROUP_RAID1:
189 strcat(ret, "|RAID1");
191 case BTRFS_BLOCK_GROUP_DUP:
194 case BTRFS_BLOCK_GROUP_RAID10:
195 strcat(ret, "|RAID10");
197 case BTRFS_BLOCK_GROUP_RAID5:
198 strcat(ret, "|RAID5");
200 case BTRFS_BLOCK_GROUP_RAID6:
201 strcat(ret, "|RAID6");
208 /* Caller should ensure sizeof(*ret)>= 26 "OFF|SCANNING|INCONSISTENT" */
209 static void qgroup_flags_to_str(u64 flags, char *ret)
211 if (flags & BTRFS_QGROUP_STATUS_FLAG_ON)
216 if (flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
217 strcat(ret, "|SCANNING");
218 if (flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT)
219 strcat(ret, "|INCONSISTENT");
222 void print_chunk(struct extent_buffer *eb, struct btrfs_chunk *chunk)
224 int num_stripes = btrfs_chunk_num_stripes(eb, chunk);
226 char chunk_flags_str[32] = {0};
228 bg_flags_to_str(btrfs_chunk_type(eb, chunk), chunk_flags_str);
229 printf("\t\tlength %llu owner %llu stripe_len %llu type %s\n",
230 (unsigned long long)btrfs_chunk_length(eb, chunk),
231 (unsigned long long)btrfs_chunk_owner(eb, chunk),
232 (unsigned long long)btrfs_chunk_stripe_len(eb, chunk),
234 printf("\t\tio_align %u io_width %u sector_size %u\n",
235 btrfs_chunk_io_align(eb, chunk),
236 btrfs_chunk_io_width(eb, chunk),
237 btrfs_chunk_sector_size(eb, chunk));
238 printf("\t\tnum_stripes %hu sub_stripes %hu\n", num_stripes,
239 btrfs_chunk_sub_stripes(eb, chunk));
240 for (i = 0 ; i < num_stripes ; i++) {
241 unsigned char dev_uuid[BTRFS_UUID_SIZE];
242 char str_dev_uuid[BTRFS_UUID_UNPARSED_SIZE];
244 read_extent_buffer(eb, dev_uuid,
245 (unsigned long)btrfs_stripe_dev_uuid_nr(chunk, i),
247 uuid_unparse(dev_uuid, str_dev_uuid);
248 printf("\t\t\tstripe %d devid %llu offset %llu\n", i,
249 (unsigned long long)btrfs_stripe_devid_nr(eb, chunk, i),
250 (unsigned long long)btrfs_stripe_offset_nr(eb, chunk, i));
251 printf("\t\t\tdev uuid: %s\n", str_dev_uuid);
255 static void print_dev_item(struct extent_buffer *eb,
256 struct btrfs_dev_item *dev_item)
258 char uuid_str[BTRFS_UUID_UNPARSED_SIZE];
259 char fsid_str[BTRFS_UUID_UNPARSED_SIZE];
260 u8 uuid[BTRFS_UUID_SIZE];
261 u8 fsid[BTRFS_UUID_SIZE];
263 read_extent_buffer(eb, uuid,
264 (unsigned long)btrfs_device_uuid(dev_item),
266 uuid_unparse(uuid, uuid_str);
267 read_extent_buffer(eb, fsid,
268 (unsigned long)btrfs_device_fsid(dev_item),
270 uuid_unparse(fsid, fsid_str);
271 printf("\t\tdevid %llu total_bytes %llu bytes_used %Lu\n"
272 "\t\tio_align %u io_width %u sector_size %u type %llu\n"
273 "\t\tgeneration %llu start_offset %llu dev_group %u\n"
274 "\t\tseek_speed %hhu bandwidth %hhu\n"
277 (unsigned long long)btrfs_device_id(eb, dev_item),
278 (unsigned long long)btrfs_device_total_bytes(eb, dev_item),
279 (unsigned long long)btrfs_device_bytes_used(eb, dev_item),
280 btrfs_device_io_align(eb, dev_item),
281 btrfs_device_io_width(eb, dev_item),
282 btrfs_device_sector_size(eb, dev_item),
283 (unsigned long long)btrfs_device_type(eb, dev_item),
284 (unsigned long long)btrfs_device_generation(eb, dev_item),
285 (unsigned long long)btrfs_device_start_offset(eb, dev_item),
286 btrfs_device_group(eb, dev_item),
287 btrfs_device_seek_speed(eb, dev_item),
288 btrfs_device_bandwidth(eb, dev_item),
292 static void print_uuids(struct extent_buffer *eb)
294 char fs_uuid[BTRFS_UUID_UNPARSED_SIZE];
295 char chunk_uuid[BTRFS_UUID_UNPARSED_SIZE];
296 u8 disk_uuid[BTRFS_UUID_SIZE];
298 read_extent_buffer(eb, disk_uuid, btrfs_header_fsid(),
301 fs_uuid[BTRFS_UUID_UNPARSED_SIZE - 1] = '\0';
302 uuid_unparse(disk_uuid, fs_uuid);
304 read_extent_buffer(eb, disk_uuid,
305 btrfs_header_chunk_tree_uuid(eb),
308 chunk_uuid[BTRFS_UUID_UNPARSED_SIZE - 1] = '\0';
309 uuid_unparse(disk_uuid, chunk_uuid);
310 printf("fs uuid %s\nchunk uuid %s\n", fs_uuid, chunk_uuid);
313 static void compress_type_to_str(u8 compress_type, char *ret)
315 switch (compress_type) {
316 case BTRFS_COMPRESS_NONE:
319 case BTRFS_COMPRESS_ZLIB:
322 case BTRFS_COMPRESS_LZO:
326 sprintf(ret, "UNKNOWN.%d", compress_type);
330 static const char* file_extent_type_to_str(u8 type)
333 case BTRFS_FILE_EXTENT_INLINE: return "inline";
334 case BTRFS_FILE_EXTENT_PREALLOC: return "prealloc";
335 case BTRFS_FILE_EXTENT_REG: return "regular";
336 default: return "unknown";
340 static void print_file_extent_item(struct extent_buffer *eb,
341 struct btrfs_item *item,
343 struct btrfs_file_extent_item *fi)
345 int extent_type = btrfs_file_extent_type(eb, fi);
346 char compress_str[16];
348 compress_type_to_str(btrfs_file_extent_compression(eb, fi),
351 printf("\t\tgeneration %llu type %hhu (%s)\n",
352 btrfs_file_extent_generation(eb, fi),
353 extent_type, file_extent_type_to_str(extent_type));
355 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
356 printf("\t\tinline extent data size %u ram %u compress(%s)\n",
357 btrfs_file_extent_inline_item_len(eb, item),
358 btrfs_file_extent_inline_len(eb, slot, fi),
362 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
363 printf("\t\tprealloc data disk byte %llu nr %llu\n",
364 (unsigned long long)btrfs_file_extent_disk_bytenr(eb, fi),
365 (unsigned long long)btrfs_file_extent_disk_num_bytes(eb, fi));
366 printf("\t\tprealloc data offset %llu nr %llu\n",
367 (unsigned long long)btrfs_file_extent_offset(eb, fi),
368 (unsigned long long)btrfs_file_extent_num_bytes(eb, fi));
371 printf("\t\textent data disk byte %llu nr %llu\n",
372 (unsigned long long)btrfs_file_extent_disk_bytenr(eb, fi),
373 (unsigned long long)btrfs_file_extent_disk_num_bytes(eb, fi));
374 printf("\t\textent data offset %llu nr %llu ram %llu\n",
375 (unsigned long long)btrfs_file_extent_offset(eb, fi),
376 (unsigned long long)btrfs_file_extent_num_bytes(eb, fi),
377 (unsigned long long)btrfs_file_extent_ram_bytes(eb, fi));
378 printf("\t\textent compression(%s)\n", compress_str);
381 /* Caller should ensure sizeof(*ret) >= 16("DATA|TREE_BLOCK") */
382 static void extent_flags_to_str(u64 flags, char *ret)
386 if (flags & BTRFS_EXTENT_FLAG_DATA) {
390 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
395 strcat(ret, "TREE_BLOCK");
397 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
399 strcat(ret, "FULL_BACKREF");
403 void print_extent_item(struct extent_buffer *eb, int slot, int metadata)
405 struct btrfs_extent_item *ei;
406 struct btrfs_extent_inline_ref *iref;
407 struct btrfs_extent_data_ref *dref;
408 struct btrfs_shared_data_ref *sref;
409 struct btrfs_disk_key key;
413 u32 item_size = btrfs_item_size_nr(eb, slot);
416 char flags_str[32] = {0};
418 if (item_size < sizeof(*ei)) {
419 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
420 struct btrfs_extent_item_v0 *ei0;
421 BUG_ON(item_size != sizeof(*ei0));
422 ei0 = btrfs_item_ptr(eb, slot, struct btrfs_extent_item_v0);
423 printf("\t\textent refs %u\n",
424 btrfs_extent_refs_v0(eb, ei0));
431 ei = btrfs_item_ptr(eb, slot, struct btrfs_extent_item);
432 flags = btrfs_extent_flags(eb, ei);
433 extent_flags_to_str(flags, flags_str);
435 printf("\t\textent refs %llu gen %llu flags %s\n",
436 (unsigned long long)btrfs_extent_refs(eb, ei),
437 (unsigned long long)btrfs_extent_generation(eb, ei),
440 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !metadata) {
441 struct btrfs_tree_block_info *info;
442 info = (struct btrfs_tree_block_info *)(ei + 1);
443 btrfs_tree_block_key(eb, info, &key);
444 printf("\t\ttree block ");
445 btrfs_print_key(&key);
446 printf(" level %d\n", btrfs_tree_block_level(eb, info));
447 iref = (struct btrfs_extent_inline_ref *)(info + 1);
448 } else if (metadata) {
449 struct btrfs_key tmp;
451 btrfs_item_key_to_cpu(eb, &tmp, slot);
452 printf("\t\ttree block skinny level %d\n", (int)tmp.offset);
453 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
455 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
458 ptr = (unsigned long)iref;
459 end = (unsigned long)ei + item_size;
461 iref = (struct btrfs_extent_inline_ref *)ptr;
462 type = btrfs_extent_inline_ref_type(eb, iref);
463 offset = btrfs_extent_inline_ref_offset(eb, iref);
465 case BTRFS_TREE_BLOCK_REF_KEY:
466 printf("\t\ttree block backref root %llu\n",
467 (unsigned long long)offset);
469 case BTRFS_SHARED_BLOCK_REF_KEY:
470 printf("\t\tshared block backref parent %llu\n",
471 (unsigned long long)offset);
473 case BTRFS_EXTENT_DATA_REF_KEY:
474 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
475 printf("\t\textent data backref root %llu "
476 "objectid %llu offset %llu count %u\n",
477 (unsigned long long)btrfs_extent_data_ref_root(eb, dref),
478 (unsigned long long)btrfs_extent_data_ref_objectid(eb, dref),
479 (unsigned long long)btrfs_extent_data_ref_offset(eb, dref),
480 btrfs_extent_data_ref_count(eb, dref));
482 case BTRFS_SHARED_DATA_REF_KEY:
483 sref = (struct btrfs_shared_data_ref *)(iref + 1);
484 printf("\t\tshared data backref parent %llu count %u\n",
485 (unsigned long long)offset,
486 btrfs_shared_data_ref_count(eb, sref));
491 ptr += btrfs_extent_inline_ref_size(type);
496 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
497 static void print_extent_ref_v0(struct extent_buffer *eb, int slot)
499 struct btrfs_extent_ref_v0 *ref0;
501 ref0 = btrfs_item_ptr(eb, slot, struct btrfs_extent_ref_v0);
502 printf("\t\textent back ref root %llu gen %llu "
503 "owner %llu num_refs %lu\n",
504 (unsigned long long)btrfs_ref_root_v0(eb, ref0),
505 (unsigned long long)btrfs_ref_generation_v0(eb, ref0),
506 (unsigned long long)btrfs_ref_objectid_v0(eb, ref0),
507 (unsigned long)btrfs_ref_count_v0(eb, ref0));
511 static void print_root_ref(struct extent_buffer *leaf, int slot, char *tag)
513 struct btrfs_root_ref *ref;
514 char namebuf[BTRFS_NAME_LEN];
517 ref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
518 namelen = btrfs_root_ref_name_len(leaf, ref);
519 read_extent_buffer(leaf, namebuf, (unsigned long)(ref + 1), namelen);
520 printf("\t\troot %s key dirid %llu sequence %llu name %.*s\n", tag,
521 (unsigned long long)btrfs_root_ref_dirid(leaf, ref),
522 (unsigned long long)btrfs_root_ref_sequence(leaf, ref),
526 static int count_bytes(void *buf, int len, char b)
530 for (i = 0; i < len; i++) {
531 if (((char*)buf)[i] == b)
538 * Caller must ensure sizeof(*ret) >= 7 "RDONLY"
540 static void root_flags_to_str(u64 flags, char *ret)
542 if (flags & BTRFS_ROOT_SUBVOL_RDONLY)
543 strcat(ret, "RDONLY");
548 static void print_root(struct extent_buffer *leaf, int slot)
550 struct btrfs_root_item *ri;
551 struct btrfs_root_item root_item;
553 char uuid_str[BTRFS_UUID_UNPARSED_SIZE];
554 char flags_str[32] = {0};
555 struct btrfs_key drop_key;
557 ri = btrfs_item_ptr(leaf, slot, struct btrfs_root_item);
558 len = btrfs_item_size_nr(leaf, slot);
560 memset(&root_item, 0, sizeof(root_item));
561 read_extent_buffer(leaf, &root_item, (unsigned long)ri, len);
562 root_flags_to_str(btrfs_root_flags(&root_item), flags_str);
564 printf("\t\tgeneration %llu root_dirid %llu bytenr %llu level %hhu refs %u\n",
565 (unsigned long long)btrfs_root_generation(&root_item),
566 (unsigned long long)btrfs_root_dirid(&root_item),
567 (unsigned long long)btrfs_root_bytenr(&root_item),
568 btrfs_root_level(&root_item),
569 btrfs_root_refs(&root_item));
570 printf("\t\tlastsnap %llu byte_limit %llu bytes_used %llu flags 0x%llx(%s)\n",
571 (unsigned long long)btrfs_root_last_snapshot(&root_item),
572 (unsigned long long)btrfs_root_limit(&root_item),
573 (unsigned long long)btrfs_root_used(&root_item),
574 (unsigned long long)btrfs_root_flags(&root_item),
577 if (root_item.generation == root_item.generation_v2) {
578 uuid_unparse(root_item.uuid, uuid_str);
579 printf("\t\tuuid %s\n", uuid_str);
580 if (count_bytes(root_item.parent_uuid, BTRFS_UUID_SIZE, 0) != BTRFS_UUID_SIZE) {
581 uuid_unparse(root_item.parent_uuid, uuid_str);
582 printf("\t\tparent_uuid %s\n", uuid_str);
584 if (count_bytes(root_item.received_uuid, BTRFS_UUID_SIZE, 0) != BTRFS_UUID_SIZE) {
585 uuid_unparse(root_item.received_uuid, uuid_str);
586 printf("\t\treceived_uuid %s\n", uuid_str);
588 if (root_item.ctransid) {
589 printf("\t\tctransid %llu otransid %llu stransid %llu rtransid %llu\n",
590 btrfs_root_ctransid(&root_item),
591 btrfs_root_otransid(&root_item),
592 btrfs_root_stransid(&root_item),
593 btrfs_root_rtransid(&root_item));
597 btrfs_disk_key_to_cpu(&drop_key, &root_item.drop_progress);
599 btrfs_print_key(&root_item.drop_progress);
600 printf(" level %hhu\n", root_item.drop_level);
603 static void print_free_space_header(struct extent_buffer *leaf, int slot)
605 struct btrfs_free_space_header *header;
606 struct btrfs_disk_key location;
608 header = btrfs_item_ptr(leaf, slot, struct btrfs_free_space_header);
609 btrfs_free_space_key(leaf, header, &location);
610 printf("\t\tlocation ");
611 btrfs_print_key(&location);
613 printf("\t\tcache generation %llu entries %llu bitmaps %llu\n",
614 (unsigned long long)btrfs_free_space_generation(leaf, header),
615 (unsigned long long)btrfs_free_space_entries(leaf, header),
616 (unsigned long long)btrfs_free_space_bitmaps(leaf, header));
619 void print_key_type(FILE *stream, u64 objectid, u8 type)
621 if (type == 0 && objectid == BTRFS_FREE_SPACE_OBJECTID) {
622 fprintf(stream, "UNTYPED");
627 case BTRFS_INODE_ITEM_KEY:
628 fprintf(stream, "INODE_ITEM");
630 case BTRFS_INODE_REF_KEY:
631 fprintf(stream, "INODE_REF");
633 case BTRFS_INODE_EXTREF_KEY:
634 fprintf(stream, "INODE_EXTREF");
636 case BTRFS_DIR_ITEM_KEY:
637 fprintf(stream, "DIR_ITEM");
639 case BTRFS_DIR_INDEX_KEY:
640 fprintf(stream, "DIR_INDEX");
642 case BTRFS_DIR_LOG_ITEM_KEY:
643 fprintf(stream, "DIR_LOG_ITEM");
645 case BTRFS_DIR_LOG_INDEX_KEY:
646 fprintf(stream, "DIR_LOG_INDEX");
648 case BTRFS_XATTR_ITEM_KEY:
649 fprintf(stream, "XATTR_ITEM");
651 case BTRFS_ORPHAN_ITEM_KEY:
652 fprintf(stream, "ORPHAN_ITEM");
654 case BTRFS_ROOT_ITEM_KEY:
655 fprintf(stream, "ROOT_ITEM");
657 case BTRFS_ROOT_REF_KEY:
658 fprintf(stream, "ROOT_REF");
660 case BTRFS_ROOT_BACKREF_KEY:
661 fprintf(stream, "ROOT_BACKREF");
663 case BTRFS_EXTENT_ITEM_KEY:
664 fprintf(stream, "EXTENT_ITEM");
666 case BTRFS_METADATA_ITEM_KEY:
667 fprintf(stream, "METADATA_ITEM");
669 case BTRFS_TREE_BLOCK_REF_KEY:
670 fprintf(stream, "TREE_BLOCK_REF");
672 case BTRFS_SHARED_BLOCK_REF_KEY:
673 fprintf(stream, "SHARED_BLOCK_REF");
675 case BTRFS_EXTENT_DATA_REF_KEY:
676 fprintf(stream, "EXTENT_DATA_REF");
678 case BTRFS_SHARED_DATA_REF_KEY:
679 fprintf(stream, "SHARED_DATA_REF");
681 case BTRFS_EXTENT_REF_V0_KEY:
682 fprintf(stream, "EXTENT_REF_V0");
684 case BTRFS_CSUM_ITEM_KEY:
685 fprintf(stream, "CSUM_ITEM");
687 case BTRFS_EXTENT_CSUM_KEY:
688 fprintf(stream, "EXTENT_CSUM");
690 case BTRFS_EXTENT_DATA_KEY:
691 fprintf(stream, "EXTENT_DATA");
693 case BTRFS_BLOCK_GROUP_ITEM_KEY:
694 fprintf(stream, "BLOCK_GROUP_ITEM");
696 case BTRFS_FREE_SPACE_INFO_KEY:
697 fprintf(stream, "FREE_SPACE_INFO");
699 case BTRFS_FREE_SPACE_EXTENT_KEY:
700 fprintf(stream, "FREE_SPACE_EXTENT");
702 case BTRFS_FREE_SPACE_BITMAP_KEY:
703 fprintf(stream, "FREE_SPACE_BITMAP");
705 case BTRFS_CHUNK_ITEM_KEY:
706 fprintf(stream, "CHUNK_ITEM");
708 case BTRFS_DEV_ITEM_KEY:
709 fprintf(stream, "DEV_ITEM");
711 case BTRFS_DEV_EXTENT_KEY:
712 fprintf(stream, "DEV_EXTENT");
714 case BTRFS_BALANCE_ITEM_KEY:
715 fprintf(stream, "BALANCE_ITEM");
717 case BTRFS_DEV_REPLACE_KEY:
718 fprintf(stream, "DEV_REPLACE");
720 case BTRFS_STRING_ITEM_KEY:
721 fprintf(stream, "STRING_ITEM");
723 case BTRFS_QGROUP_STATUS_KEY:
724 fprintf(stream, "QGROUP_STATUS");
726 case BTRFS_QGROUP_RELATION_KEY:
727 fprintf(stream, "QGROUP_RELATION");
729 case BTRFS_QGROUP_INFO_KEY:
730 fprintf(stream, "QGROUP_INFO");
732 case BTRFS_QGROUP_LIMIT_KEY:
733 fprintf(stream, "QGROUP_LIMIT");
735 case BTRFS_DEV_STATS_KEY:
736 fprintf(stream, "DEV_STATS");
738 case BTRFS_UUID_KEY_SUBVOL:
739 fprintf(stream, "UUID_KEY_SUBVOL");
741 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
742 fprintf(stream, "UUID_KEY_RECEIVED_SUBVOL");
745 fprintf(stream, "UNKNOWN.%d", type);
749 void print_objectid(FILE *stream, u64 objectid, u8 type)
752 case BTRFS_DEV_EXTENT_KEY:
754 fprintf(stream, "%llu", (unsigned long long)objectid);
756 case BTRFS_QGROUP_RELATION_KEY:
757 fprintf(stream, "%llu/%llu", btrfs_qgroup_level(objectid),
758 btrfs_qgroup_subvid(objectid));
760 case BTRFS_UUID_KEY_SUBVOL:
761 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
762 fprintf(stream, "0x%016llx", (unsigned long long)objectid);
767 case BTRFS_ROOT_TREE_OBJECTID:
768 if (type == BTRFS_DEV_ITEM_KEY)
769 fprintf(stream, "DEV_ITEMS");
771 fprintf(stream, "ROOT_TREE");
773 case BTRFS_EXTENT_TREE_OBJECTID:
774 fprintf(stream, "EXTENT_TREE");
776 case BTRFS_CHUNK_TREE_OBJECTID:
777 fprintf(stream, "CHUNK_TREE");
779 case BTRFS_DEV_TREE_OBJECTID:
780 fprintf(stream, "DEV_TREE");
782 case BTRFS_FS_TREE_OBJECTID:
783 fprintf(stream, "FS_TREE");
785 case BTRFS_ROOT_TREE_DIR_OBJECTID:
786 fprintf(stream, "ROOT_TREE_DIR");
788 case BTRFS_CSUM_TREE_OBJECTID:
789 fprintf(stream, "CSUM_TREE");
791 case BTRFS_BALANCE_OBJECTID:
792 fprintf(stream, "BALANCE");
794 case BTRFS_ORPHAN_OBJECTID:
795 fprintf(stream, "ORPHAN");
797 case BTRFS_TREE_LOG_OBJECTID:
798 fprintf(stream, "TREE_LOG");
800 case BTRFS_TREE_LOG_FIXUP_OBJECTID:
801 fprintf(stream, "LOG_FIXUP");
803 case BTRFS_TREE_RELOC_OBJECTID:
804 fprintf(stream, "TREE_RELOC");
806 case BTRFS_DATA_RELOC_TREE_OBJECTID:
807 fprintf(stream, "DATA_RELOC_TREE");
809 case BTRFS_EXTENT_CSUM_OBJECTID:
810 fprintf(stream, "EXTENT_CSUM");
812 case BTRFS_FREE_SPACE_OBJECTID:
813 fprintf(stream, "FREE_SPACE");
815 case BTRFS_FREE_INO_OBJECTID:
816 fprintf(stream, "FREE_INO");
818 case BTRFS_QUOTA_TREE_OBJECTID:
819 fprintf(stream, "QUOTA_TREE");
821 case BTRFS_UUID_TREE_OBJECTID:
822 fprintf(stream, "UUID_TREE");
824 case BTRFS_FREE_SPACE_TREE_OBJECTID:
825 fprintf(stream, "FREE_SPACE_TREE");
827 case BTRFS_MULTIPLE_OBJECTIDS:
828 fprintf(stream, "MULTIPLE");
831 fprintf(stream, "-1");
833 case BTRFS_FIRST_CHUNK_TREE_OBJECTID:
834 if (type == BTRFS_CHUNK_ITEM_KEY) {
835 fprintf(stream, "FIRST_CHUNK_TREE");
840 fprintf(stream, "%llu", (unsigned long long)objectid);
844 void btrfs_print_key(struct btrfs_disk_key *disk_key)
846 u64 objectid = btrfs_disk_key_objectid(disk_key);
847 u8 type = btrfs_disk_key_type(disk_key);
848 u64 offset = btrfs_disk_key_offset(disk_key);
851 print_objectid(stdout, objectid, type);
853 print_key_type(stdout, objectid, type);
855 case BTRFS_QGROUP_RELATION_KEY:
856 case BTRFS_QGROUP_INFO_KEY:
857 case BTRFS_QGROUP_LIMIT_KEY:
858 printf(" %llu/%llu)", btrfs_qgroup_level(offset),
859 btrfs_qgroup_subvid(offset));
861 case BTRFS_UUID_KEY_SUBVOL:
862 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
863 printf(" 0x%016llx)", (unsigned long long)offset);
866 if (offset == (u64)-1)
869 printf(" %llu)", (unsigned long long)offset);
874 static void print_uuid_item(struct extent_buffer *l, unsigned long offset,
877 if (item_size & (sizeof(u64) - 1)) {
878 printf("btrfs: uuid item with illegal size %lu!\n",
879 (unsigned long)item_size);
885 read_extent_buffer(l, &subvol_id, offset, sizeof(u64));
886 printf("\t\tsubvol_id %llu\n",
887 (unsigned long long)le64_to_cpu(subvol_id));
888 item_size -= sizeof(u64);
889 offset += sizeof(u64);
893 /* Btrfs inode flag stringification helper */
894 #define STRCAT_ONE_INODE_FLAG(flags, name, empty, dst) ({ \
895 if (flags & BTRFS_INODE_##name) { \
898 strcat(dst, #name); \
904 * Caller should ensure sizeof(*ret) >= 102: all charactors plus '|' of
905 * BTRFS_INODE_* flags
907 static void inode_flags_to_str(u64 flags, char *ret)
911 STRCAT_ONE_INODE_FLAG(flags, NODATASUM, empty, ret);
912 STRCAT_ONE_INODE_FLAG(flags, NODATACOW, empty, ret);
913 STRCAT_ONE_INODE_FLAG(flags, READONLY, empty, ret);
914 STRCAT_ONE_INODE_FLAG(flags, NOCOMPRESS, empty, ret);
915 STRCAT_ONE_INODE_FLAG(flags, PREALLOC, empty, ret);
916 STRCAT_ONE_INODE_FLAG(flags, SYNC, empty, ret);
917 STRCAT_ONE_INODE_FLAG(flags, IMMUTABLE, empty, ret);
918 STRCAT_ONE_INODE_FLAG(flags, APPEND, empty, ret);
919 STRCAT_ONE_INODE_FLAG(flags, NODUMP, empty, ret);
920 STRCAT_ONE_INODE_FLAG(flags, NOATIME, empty, ret);
921 STRCAT_ONE_INODE_FLAG(flags, DIRSYNC, empty, ret);
922 STRCAT_ONE_INODE_FLAG(flags, COMPRESS, empty, ret);
927 static void print_timespec(struct extent_buffer *eb,
928 struct btrfs_timespec *timespec, const char *prefix,
937 tmp_u64 = btrfs_timespec_sec(eb, timespec);
938 tmp_u32 = btrfs_timespec_nsec(eb, timespec);
940 localtime_r(&tmp_time, &tm);
941 strftime(timestamp, sizeof(timestamp),
942 "%Y-%m-%d %H:%M:%S", &tm);
943 printf("%s%llu.%u (%s)%s", prefix, (unsigned long long)tmp_u64, tmp_u32,
947 static void print_inode_item(struct extent_buffer *eb, struct btrfs_item *item,
948 struct btrfs_inode_item *ii)
952 memset(flags_str, 0, sizeof(flags_str));
953 inode_flags_to_str(btrfs_inode_flags(eb, ii), flags_str);
954 printf("\t\tinode generation %llu transid %llu size %llu nbytes %llu\n"
955 "\t\tblock group %llu mode %o links %u uid %u gid %u rdev %llu\n"
956 "\t\tsequence %llu flags 0x%llx(%s)\n",
957 (unsigned long long)btrfs_inode_generation(eb, ii),
958 (unsigned long long)btrfs_inode_transid(eb, ii),
959 (unsigned long long)btrfs_inode_size(eb, ii),
960 (unsigned long long)btrfs_inode_nbytes(eb, ii),
961 (unsigned long long)btrfs_inode_block_group(eb,ii),
962 btrfs_inode_mode(eb, ii),
963 btrfs_inode_nlink(eb, ii),
964 btrfs_inode_uid(eb, ii),
965 btrfs_inode_gid(eb, ii),
966 (unsigned long long)btrfs_inode_rdev(eb,ii),
967 (unsigned long long)btrfs_inode_flags(eb,ii),
968 (unsigned long long)btrfs_inode_sequence(eb, ii),
970 print_timespec(eb, btrfs_inode_atime(ii), "\t\tatime ", "\n");
971 print_timespec(eb, btrfs_inode_ctime(ii), "\t\tctime ", "\n");
972 print_timespec(eb, btrfs_inode_mtime(ii), "\t\tmtime ", "\n");
973 print_timespec(eb, btrfs_inode_otime(ii), "\t\totime ", "\n");
976 void btrfs_print_leaf(struct btrfs_root *root, struct extent_buffer *l)
980 struct btrfs_item *item;
981 struct btrfs_dir_item *di;
982 struct btrfs_inode_item *ii;
983 struct btrfs_file_extent_item *fi;
984 struct btrfs_block_group_item *bi;
985 struct btrfs_extent_data_ref *dref;
986 struct btrfs_shared_data_ref *sref;
987 struct btrfs_inode_ref *iref;
988 struct btrfs_inode_extref *iref2;
989 struct btrfs_dev_extent *dev_extent;
990 struct btrfs_disk_key disk_key;
991 struct btrfs_block_group_item bg_item;
992 struct btrfs_free_space_info *free_info;
993 struct btrfs_dir_log_item *dlog;
994 struct btrfs_qgroup_info_item *qg_info;
995 struct btrfs_qgroup_limit_item *qg_limit;
996 struct btrfs_qgroup_status_item *qg_status;
997 u32 nr = btrfs_header_nritems(l);
1000 char flags_str[256];
1001 char uuid_str[BTRFS_UUID_UNPARSED_SIZE];
1002 u8 uuid[BTRFS_UUID_SIZE];
1004 printf("leaf %llu items %d free space %d generation %llu owner %llu\n",
1005 (unsigned long long)btrfs_header_bytenr(l), nr,
1006 btrfs_leaf_free_space(root, l),
1007 (unsigned long long)btrfs_header_generation(l),
1008 (unsigned long long)btrfs_header_owner(l));
1011 for (i = 0 ; i < nr ; i++) {
1012 item = btrfs_item_nr(i);
1013 btrfs_item_key(l, &disk_key, i);
1014 objectid = btrfs_disk_key_objectid(&disk_key);
1015 type = btrfs_disk_key_type(&disk_key);
1016 printf("\titem %d ", i);
1017 btrfs_print_key(&disk_key);
1018 printf(" itemoff %d itemsize %d\n",
1019 btrfs_item_offset(l, item),
1020 btrfs_item_size(l, item));
1022 if (type == 0 && objectid == BTRFS_FREE_SPACE_OBJECTID)
1023 print_free_space_header(l, i);
1026 case BTRFS_INODE_ITEM_KEY:
1027 ii = btrfs_item_ptr(l, i, struct btrfs_inode_item);
1028 print_inode_item(l, item, ii);
1030 case BTRFS_INODE_REF_KEY:
1031 iref = btrfs_item_ptr(l, i, struct btrfs_inode_ref);
1032 print_inode_ref_item(l, item, iref);
1034 case BTRFS_INODE_EXTREF_KEY:
1035 iref2 = btrfs_item_ptr(l, i, struct btrfs_inode_extref);
1036 print_inode_extref_item(l, item, iref2);
1038 case BTRFS_DIR_ITEM_KEY:
1039 case BTRFS_DIR_INDEX_KEY:
1040 case BTRFS_XATTR_ITEM_KEY:
1041 di = btrfs_item_ptr(l, i, struct btrfs_dir_item);
1042 print_dir_item(l, item, di);
1044 case BTRFS_DIR_LOG_INDEX_KEY:
1045 case BTRFS_DIR_LOG_ITEM_KEY:
1046 dlog = btrfs_item_ptr(l, i, struct btrfs_dir_log_item);
1047 printf("\t\tdir log end %Lu\n",
1048 (unsigned long long)btrfs_dir_log_end(l, dlog));
1050 case BTRFS_ORPHAN_ITEM_KEY:
1051 printf("\t\torphan item\n");
1053 case BTRFS_ROOT_ITEM_KEY:
1056 case BTRFS_ROOT_REF_KEY:
1057 print_root_ref(l, i, "ref");
1059 case BTRFS_ROOT_BACKREF_KEY:
1060 print_root_ref(l, i, "backref");
1062 case BTRFS_EXTENT_ITEM_KEY:
1063 print_extent_item(l, i, 0);
1065 case BTRFS_METADATA_ITEM_KEY:
1066 print_extent_item(l, i, 1);
1068 case BTRFS_TREE_BLOCK_REF_KEY:
1069 printf("\t\ttree block backref\n");
1071 case BTRFS_SHARED_BLOCK_REF_KEY:
1072 printf("\t\tshared block backref\n");
1074 case BTRFS_EXTENT_DATA_REF_KEY:
1075 dref = btrfs_item_ptr(l, i, struct btrfs_extent_data_ref);
1076 printf("\t\textent data backref root %llu "
1077 "objectid %llu offset %llu count %u\n",
1078 (unsigned long long)btrfs_extent_data_ref_root(l, dref),
1079 (unsigned long long)btrfs_extent_data_ref_objectid(l, dref),
1080 (unsigned long long)btrfs_extent_data_ref_offset(l, dref),
1081 btrfs_extent_data_ref_count(l, dref));
1083 case BTRFS_SHARED_DATA_REF_KEY:
1084 sref = btrfs_item_ptr(l, i, struct btrfs_shared_data_ref);
1085 printf("\t\tshared data backref count %u\n",
1086 btrfs_shared_data_ref_count(l, sref));
1088 case BTRFS_EXTENT_REF_V0_KEY:
1089 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1090 print_extent_ref_v0(l, i);
1095 case BTRFS_CSUM_ITEM_KEY:
1096 printf("\t\tcsum item\n");
1098 case BTRFS_EXTENT_CSUM_KEY:
1099 printf("\t\textent csum item\n");
1101 case BTRFS_EXTENT_DATA_KEY:
1102 fi = btrfs_item_ptr(l, i,
1103 struct btrfs_file_extent_item);
1104 print_file_extent_item(l, item, i, fi);
1106 case BTRFS_BLOCK_GROUP_ITEM_KEY:
1107 bi = btrfs_item_ptr(l, i,
1108 struct btrfs_block_group_item);
1109 read_extent_buffer(l, &bg_item, (unsigned long)bi,
1111 memset(flags_str, 0, sizeof(flags_str));
1112 bg_flags_to_str(btrfs_block_group_flags(&bg_item),
1114 printf("\t\tblock group used %llu chunk_objectid %llu flags %s\n",
1115 (unsigned long long)btrfs_block_group_used(&bg_item),
1116 (unsigned long long)btrfs_block_group_chunk_objectid(&bg_item),
1119 case BTRFS_FREE_SPACE_INFO_KEY:
1120 free_info = btrfs_item_ptr(l, i, struct btrfs_free_space_info);
1121 printf("\t\tfree space info extent count %u flags %u\n",
1122 (unsigned)btrfs_free_space_extent_count(l, free_info),
1123 (unsigned)btrfs_free_space_flags(l, free_info));
1125 case BTRFS_FREE_SPACE_EXTENT_KEY:
1126 printf("\t\tfree space extent\n");
1128 case BTRFS_FREE_SPACE_BITMAP_KEY:
1129 printf("\t\tfree space bitmap\n");
1131 case BTRFS_CHUNK_ITEM_KEY:
1132 print_chunk(l, btrfs_item_ptr(l, i, struct btrfs_chunk));
1134 case BTRFS_DEV_ITEM_KEY:
1135 print_dev_item(l, btrfs_item_ptr(l, i,
1136 struct btrfs_dev_item));
1138 case BTRFS_DEV_EXTENT_KEY:
1139 dev_extent = btrfs_item_ptr(l, i,
1140 struct btrfs_dev_extent);
1141 read_extent_buffer(l, uuid,
1142 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(dev_extent),
1144 uuid_unparse(uuid, uuid_str);
1145 printf("\t\tdev extent chunk_tree %llu\n"
1146 "\t\tchunk objectid %llu chunk offset %llu "
1148 "\t\tchunk_tree_uuid %s\n",
1149 (unsigned long long)
1150 btrfs_dev_extent_chunk_tree(l, dev_extent),
1151 (unsigned long long)
1152 btrfs_dev_extent_chunk_objectid(l, dev_extent),
1153 (unsigned long long)
1154 btrfs_dev_extent_chunk_offset(l, dev_extent),
1155 (unsigned long long)
1156 btrfs_dev_extent_length(l, dev_extent),
1159 case BTRFS_QGROUP_STATUS_KEY:
1160 qg_status = btrfs_item_ptr(l, i,
1161 struct btrfs_qgroup_status_item);
1162 memset(flags_str, 0, sizeof(flags_str));
1163 qgroup_flags_to_str(btrfs_qgroup_status_flags(l, qg_status),
1165 printf("\t\tversion %llu generation %llu flags %s "
1167 (unsigned long long)
1168 btrfs_qgroup_status_version(l, qg_status),
1169 (unsigned long long)
1170 btrfs_qgroup_status_generation(l, qg_status),
1172 (unsigned long long)
1173 btrfs_qgroup_status_rescan(l, qg_status));
1175 case BTRFS_QGROUP_RELATION_KEY:
1177 case BTRFS_QGROUP_INFO_KEY:
1178 qg_info = btrfs_item_ptr(l, i,
1179 struct btrfs_qgroup_info_item);
1180 printf("\t\tgeneration %llu\n"
1181 "\t\treferenced %llu referenced compressed %llu\n"
1182 "\t\texclusive %llu exclusive compressed %llu\n",
1183 (unsigned long long)
1184 btrfs_qgroup_info_generation(l, qg_info),
1185 (unsigned long long)
1186 btrfs_qgroup_info_referenced(l, qg_info),
1187 (unsigned long long)
1188 btrfs_qgroup_info_referenced_compressed(l,
1190 (unsigned long long)
1191 btrfs_qgroup_info_exclusive(l, qg_info),
1192 (unsigned long long)
1193 btrfs_qgroup_info_exclusive_compressed(l,
1196 case BTRFS_QGROUP_LIMIT_KEY:
1197 qg_limit = btrfs_item_ptr(l, i,
1198 struct btrfs_qgroup_limit_item);
1199 printf("\t\tflags %llx\n"
1200 "\t\tmax referenced %lld max exclusive %lld\n"
1201 "\t\trsv referenced %lld rsv exclusive %lld\n",
1202 (unsigned long long)
1203 btrfs_qgroup_limit_flags(l, qg_limit),
1205 btrfs_qgroup_limit_max_referenced(l, qg_limit),
1207 btrfs_qgroup_limit_max_exclusive(l, qg_limit),
1209 btrfs_qgroup_limit_rsv_referenced(l, qg_limit),
1211 btrfs_qgroup_limit_rsv_exclusive(l, qg_limit));
1213 case BTRFS_UUID_KEY_SUBVOL:
1214 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
1215 print_uuid_item(l, btrfs_item_ptr_offset(l, i),
1216 btrfs_item_size_nr(l, i));
1218 case BTRFS_STRING_ITEM_KEY:
1219 /* dirty, but it's simple */
1220 str = l->data + btrfs_item_ptr_offset(l, i);
1221 printf("\t\titem data %.*s\n", btrfs_item_size(l, item), str);
1223 case BTRFS_DEV_STATS_KEY:
1224 printf("\t\tdevice stats\n");
1231 void btrfs_print_tree(struct btrfs_root *root, struct extent_buffer *eb, int follow)
1236 struct btrfs_disk_key disk_key;
1237 struct btrfs_key key;
1238 struct extent_buffer *next;
1242 nr = btrfs_header_nritems(eb);
1243 if (btrfs_is_leaf(eb)) {
1244 btrfs_print_leaf(root, eb);
1247 printf("node %llu level %d items %d free %u generation %llu owner %llu\n",
1248 (unsigned long long)eb->start,
1249 btrfs_header_level(eb), nr,
1250 (u32)BTRFS_NODEPTRS_PER_BLOCK(root) - nr,
1251 (unsigned long long)btrfs_header_generation(eb),
1252 (unsigned long long)btrfs_header_owner(eb));
1255 size = root->nodesize;
1256 for (i = 0; i < nr; i++) {
1257 u64 blocknr = btrfs_node_blockptr(eb, i);
1258 btrfs_node_key(eb, &disk_key, i);
1259 btrfs_disk_key_to_cpu(&key, &disk_key);
1261 btrfs_print_key(&disk_key);
1262 printf(" block %llu (%llu) gen %llu\n",
1263 (unsigned long long)blocknr,
1264 (unsigned long long)blocknr / size,
1265 (unsigned long long)btrfs_node_ptr_generation(eb, i));
1271 for (i = 0; i < nr; i++) {
1272 next = read_tree_block(root, btrfs_node_blockptr(eb, i), size,
1273 btrfs_node_ptr_generation(eb, i));
1274 if (!extent_buffer_uptodate(next)) {
1275 fprintf(stderr, "failed to read %llu in tree %llu\n",
1276 (unsigned long long)btrfs_node_blockptr(eb, i),
1277 (unsigned long long)btrfs_header_owner(eb));
1280 if (btrfs_is_leaf(next) && btrfs_header_level(eb) != 1) {
1282 "eb corrupted: item %d eb level %d next level %d, skipping the rest",
1283 i, btrfs_header_level(next),
1284 btrfs_header_level(eb));
1287 if (btrfs_header_level(next) != btrfs_header_level(eb) - 1) {
1289 "eb corrupted: item %d eb level %d next level %d, skipping the rest",
1290 i, btrfs_header_level(next),
1291 btrfs_header_level(eb));
1294 btrfs_print_tree(root, next, 1);
1295 free_extent_buffer(next);
1301 free_extent_buffer(next);