btrfs-progs: replace leafsize with nodesize
[btrfs-progs-unstable/devel.git] / print-tree.c
blobd0f37a50602edbf0b7a2b9ef5c6d2f75f98b18c7
1 /*
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.
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <uuid/uuid.h>
22 #include "kerncompat.h"
23 #include "radix-tree.h"
24 #include "ctree.h"
25 #include "disk-io.h"
26 #include "print-tree.h"
27 #include "utils.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);
35 switch (type) {
36 case BTRFS_FT_REG_FILE:
37 printf("FILE");
38 break;
39 case BTRFS_FT_DIR:
40 printf("DIR");
41 break;
42 case BTRFS_FT_CHRDEV:
43 printf("CHRDEV");
44 break;
45 case BTRFS_FT_BLKDEV:
46 printf("BLKDEV");
47 break;
48 case BTRFS_FT_FIFO:
49 printf("FIFO");
50 break;
51 case BTRFS_FT_SOCK:
52 printf("SOCK");
53 break;
54 case BTRFS_FT_SYMLINK:
55 printf("SYMLINK");
56 break;
57 case BTRFS_FT_XATTR:
58 printf("XATTR");
59 break;
60 default:
61 printf("%u", type);
65 static int print_dir_item(struct extent_buffer *eb, struct btrfs_item *item,
66 struct btrfs_dir_item *di)
68 u32 total;
69 u32 cur = 0;
70 u32 len;
71 u32 name_len;
72 u32 data_len;
73 char namebuf[BTRFS_NAME_LEN];
74 struct btrfs_disk_key location;
76 total = btrfs_item_size(eb, item);
77 while(cur < total) {
78 btrfs_dir_item_key(eb, di, &location);
79 printf("\t\tlocation ");
80 btrfs_print_key(&location);
81 printf(" type ");
82 print_dir_item_type(eb, di);
83 printf("\n");
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);
90 if (data_len) {
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);
98 cur += len;
100 return 0;
103 static int print_inode_extref_item(struct extent_buffer *eb,
104 struct btrfs_item *item,
105 struct btrfs_inode_extref *extref)
107 u32 total;
108 u32 cur = 0;
109 u32 len;
110 u32 name_len = 0;
111 u64 index = 0;
112 u64 parent_objid;
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 "
127 "name: %.*s\n",
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);
134 cur += len;
136 return 0;
139 static int print_inode_ref_item(struct extent_buffer *eb, struct btrfs_item *item,
140 struct btrfs_inode_ref *ref)
142 u32 total;
143 u32 cur = 0;
144 u32 len;
145 u32 name_len;
146 u64 index;
147 char namebuf[BTRFS_NAME_LEN];
148 total = btrfs_item_size(eb, item);
149 while(cur < total) {
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);
158 cur += len;
160 return 0;
163 /* Caller should ensure sizeof(*ret)>=21 "DATA|METADATA|RAID10" */
164 static void bg_flags_to_str(u64 flags, char *ret)
166 int empty = 1;
168 if (flags & BTRFS_BLOCK_GROUP_DATA) {
169 empty = 0;
170 strcpy(ret, "DATA");
172 if (flags & BTRFS_BLOCK_GROUP_METADATA) {
173 if (!empty)
174 strcat(ret, "|");
175 strcat(ret, "METADATA");
177 if (flags & BTRFS_BLOCK_GROUP_SYSTEM) {
178 if (!empty)
179 strcat(ret, "|");
180 strcat(ret, "SYSTEM");
182 switch (flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
183 case BTRFS_BLOCK_GROUP_RAID0:
184 strcat(ret, "|RAID0");
185 break;
186 case BTRFS_BLOCK_GROUP_RAID1:
187 strcat(ret, "|RAID1");
188 break;
189 case BTRFS_BLOCK_GROUP_DUP:
190 strcat(ret, "|DUP");
191 break;
192 case BTRFS_BLOCK_GROUP_RAID10:
193 strcat(ret, "|RAID10");
194 break;
195 case BTRFS_BLOCK_GROUP_RAID5:
196 strcat(ret, "|RAID5");
197 break;
198 case BTRFS_BLOCK_GROUP_RAID6:
199 strcat(ret, "|RAID6");
200 break;
201 default:
202 break;
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)
210 strcpy(ret, "ON");
211 else
212 strcpy(ret, "OFF");
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);
223 int i;
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),
239 BTRFS_UUID_SIZE);
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),
256 BTRFS_UUID_SIZE);
257 uuid_unparse(disk_uuid, disk_uuid_c);
258 printf("\t\tdev item devid %llu "
259 "total_bytes %llu bytes used %Lu\n"
260 "\t\tdev uuid %s\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),
264 disk_uuid_c);
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(),
274 BTRFS_FSID_SIZE);
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),
281 BTRFS_UUID_SIZE);
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:
292 strcpy(ret, "none");
293 break;
294 case BTRFS_COMPRESS_ZLIB:
295 strcpy(ret, "zlib");
296 break;
297 case BTRFS_COMPRESS_LZO:
298 strcpy(ret, "lzo");
299 break;
300 default:
301 sprintf(ret, "UNKNOWN.%d", compress_type);
305 static void print_file_extent_item(struct extent_buffer *eb,
306 struct btrfs_item *item,
307 int slot,
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),
314 compress_str);
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),
321 compress_str);
322 return;
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));
331 return;
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)
346 int empty = 1;
348 if (flags & BTRFS_EXTENT_FLAG_DATA) {
349 empty = 0;
350 strcpy(ret, "DATA");
352 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
353 if (!empty) {
354 empty = 0;
355 strcat(ret, "|");
357 strcat(ret, "TREE_BLOCK");
359 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
360 strcat(ret, "|");
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;
372 unsigned long end;
373 unsigned long ptr;
374 int type;
375 u32 item_size = btrfs_item_size_nr(eb, slot);
376 u64 flags;
377 u64 offset;
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));
387 return;
388 #else
389 BUG();
390 #endif
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),
400 flags_str);
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);
416 } else{
417 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
420 ptr = (unsigned long)iref;
421 end = (unsigned long)ei + item_size;
422 while (ptr < end) {
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);
426 switch (type) {
427 case BTRFS_TREE_BLOCK_REF_KEY:
428 printf("\t\ttree block backref root %llu\n",
429 (unsigned long long)offset);
430 break;
431 case BTRFS_SHARED_BLOCK_REF_KEY:
432 printf("\t\tshared block backref parent %llu\n",
433 (unsigned long long)offset);
434 break;
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));
443 break;
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));
449 break;
450 default:
451 return;
453 ptr += btrfs_extent_inline_ref_size(type);
455 WARN_ON(ptr > end);
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));
471 #endif
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];
477 int namelen;
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),
485 namelen, namebuf);
488 static int count_bytes(void *buf, int len, char b)
490 int cnt = 0;
491 int i;
492 for (i = 0; i < len; i++) {
493 if (((char*)buf)[i] == b)
494 cnt++;
496 return cnt;
499 static void print_root(struct extent_buffer *leaf, int slot)
501 struct btrfs_root_item *ri;
502 struct btrfs_root_item root_item;
503 int len;
504 char uuid_str[BTRFS_UUID_UNPARSED_SIZE];
506 ri = btrfs_item_ptr(leaf, slot, struct btrfs_root_item);
507 len = btrfs_item_size_nr(leaf, slot);
509 memset(&root_item, 0, sizeof(root_item));
510 read_extent_buffer(leaf, &root_item, (unsigned long)ri, len);
512 printf("\t\troot data bytenr %llu level %d dirid %llu refs %u gen %llu lastsnap %llu\n",
513 (unsigned long long)btrfs_root_bytenr(&root_item),
514 btrfs_root_level(&root_item),
515 (unsigned long long)btrfs_root_dirid(&root_item),
516 btrfs_root_refs(&root_item),
517 (unsigned long long)btrfs_root_generation(&root_item),
518 (unsigned long long)btrfs_root_last_snapshot(&root_item));
520 if (root_item.generation == root_item.generation_v2) {
521 uuid_unparse(root_item.uuid, uuid_str);
522 printf("\t\tuuid %s\n", uuid_str);
523 if (count_bytes(root_item.parent_uuid, BTRFS_UUID_SIZE, 0) != BTRFS_UUID_SIZE) {
524 uuid_unparse(root_item.parent_uuid, uuid_str);
525 printf("\t\tparent_uuid %s\n", uuid_str);
527 if (count_bytes(root_item.received_uuid, BTRFS_UUID_SIZE, 0) != BTRFS_UUID_SIZE) {
528 uuid_unparse(root_item.received_uuid, uuid_str);
529 printf("\t\treceived_uuid %s\n", uuid_str);
531 if (root_item.ctransid) {
532 printf("\t\tctransid %llu otransid %llu stransid %llu rtransid %llu\n",
533 btrfs_root_ctransid(&root_item),
534 btrfs_root_otransid(&root_item),
535 btrfs_root_stransid(&root_item),
536 btrfs_root_rtransid(&root_item));
539 if (btrfs_root_refs(&root_item) == 0) {
540 struct btrfs_key drop_key;
541 btrfs_disk_key_to_cpu(&drop_key,
542 &root_item.drop_progress);
543 printf("\t\tdrop ");
544 btrfs_print_key(&root_item.drop_progress);
545 printf(" level %d\n", root_item.drop_level);
549 static void print_free_space_header(struct extent_buffer *leaf, int slot)
551 struct btrfs_free_space_header *header;
552 struct btrfs_disk_key location;
554 header = btrfs_item_ptr(leaf, slot, struct btrfs_free_space_header);
555 btrfs_free_space_key(leaf, header, &location);
556 printf("\t\tlocation ");
557 btrfs_print_key(&location);
558 printf("\n");
559 printf("\t\tcache generation %llu entries %llu bitmaps %llu\n",
560 (unsigned long long)btrfs_free_space_generation(leaf, header),
561 (unsigned long long)btrfs_free_space_entries(leaf, header),
562 (unsigned long long)btrfs_free_space_bitmaps(leaf, header));
565 static void print_key_type(u64 objectid, u8 type)
567 if (type == 0 && objectid == BTRFS_FREE_SPACE_OBJECTID) {
568 printf("UNTYPED");
569 return;
572 switch (type) {
573 case BTRFS_INODE_ITEM_KEY:
574 printf("INODE_ITEM");
575 break;
576 case BTRFS_INODE_REF_KEY:
577 printf("INODE_REF");
578 break;
579 case BTRFS_INODE_EXTREF_KEY:
580 printf("INODE_EXTREF");
581 break;
582 case BTRFS_DIR_ITEM_KEY:
583 printf("DIR_ITEM");
584 break;
585 case BTRFS_DIR_INDEX_KEY:
586 printf("DIR_INDEX");
587 break;
588 case BTRFS_DIR_LOG_ITEM_KEY:
589 printf("DIR_LOG_ITEM");
590 break;
591 case BTRFS_DIR_LOG_INDEX_KEY:
592 printf("DIR_LOG_INDEX");
593 break;
594 case BTRFS_XATTR_ITEM_KEY:
595 printf("XATTR_ITEM");
596 break;
597 case BTRFS_ORPHAN_ITEM_KEY:
598 printf("ORPHAN_ITEM");
599 break;
600 case BTRFS_ROOT_ITEM_KEY:
601 printf("ROOT_ITEM");
602 break;
603 case BTRFS_ROOT_REF_KEY:
604 printf("ROOT_REF");
605 break;
606 case BTRFS_ROOT_BACKREF_KEY:
607 printf("ROOT_BACKREF");
608 break;
609 case BTRFS_EXTENT_ITEM_KEY:
610 printf("EXTENT_ITEM");
611 break;
612 case BTRFS_METADATA_ITEM_KEY:
613 printf("METADATA_ITEM");
614 break;
615 case BTRFS_TREE_BLOCK_REF_KEY:
616 printf("TREE_BLOCK_REF");
617 break;
618 case BTRFS_SHARED_BLOCK_REF_KEY:
619 printf("SHARED_BLOCK_REF");
620 break;
621 case BTRFS_EXTENT_DATA_REF_KEY:
622 printf("EXTENT_DATA_REF");
623 break;
624 case BTRFS_SHARED_DATA_REF_KEY:
625 printf("SHARED_DATA_REF");
626 break;
627 case BTRFS_EXTENT_REF_V0_KEY:
628 printf("EXTENT_REF_V0");
629 break;
630 case BTRFS_CSUM_ITEM_KEY:
631 printf("CSUM_ITEM");
632 break;
633 case BTRFS_EXTENT_CSUM_KEY:
634 printf("EXTENT_CSUM");
635 break;
636 case BTRFS_EXTENT_DATA_KEY:
637 printf("EXTENT_DATA");
638 break;
639 case BTRFS_BLOCK_GROUP_ITEM_KEY:
640 printf("BLOCK_GROUP_ITEM");
641 break;
642 case BTRFS_FREE_SPACE_INFO_KEY:
643 printf("FREE_SPACE_INFO");
644 break;
645 case BTRFS_FREE_SPACE_EXTENT_KEY:
646 printf("FREE_SPACE_EXTENT");
647 break;
648 case BTRFS_FREE_SPACE_BITMAP_KEY:
649 printf("FREE_SPACE_BITMAP");
650 break;
651 case BTRFS_CHUNK_ITEM_KEY:
652 printf("CHUNK_ITEM");
653 break;
654 case BTRFS_DEV_ITEM_KEY:
655 printf("DEV_ITEM");
656 break;
657 case BTRFS_DEV_EXTENT_KEY:
658 printf("DEV_EXTENT");
659 break;
660 case BTRFS_BALANCE_ITEM_KEY:
661 printf("BALANCE_ITEM");
662 break;
663 case BTRFS_DEV_REPLACE_KEY:
664 printf("DEV_REPLACE");
665 break;
666 case BTRFS_STRING_ITEM_KEY:
667 printf("STRING_ITEM");
668 break;
669 case BTRFS_QGROUP_STATUS_KEY:
670 printf("QGROUP_STATUS");
671 break;
672 case BTRFS_QGROUP_RELATION_KEY:
673 printf("QGROUP_RELATION");
674 break;
675 case BTRFS_QGROUP_INFO_KEY:
676 printf("QGROUP_INFO");
677 break;
678 case BTRFS_QGROUP_LIMIT_KEY:
679 printf("QGROUP_LIMIT");
680 break;
681 case BTRFS_DEV_STATS_KEY:
682 printf("DEV_STATS");
683 break;
684 case BTRFS_UUID_KEY_SUBVOL:
685 printf("UUID_KEY_SUBVOL");
686 break;
687 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
688 printf("UUID_KEY_RECEIVED_SUBVOL");
689 break;
690 default:
691 printf("UNKNOWN.%d", type);
695 static void print_objectid(u64 objectid, u8 type)
697 switch (type) {
698 case BTRFS_DEV_EXTENT_KEY:
699 printf("%llu", (unsigned long long)objectid); /* device id */
700 return;
701 case BTRFS_QGROUP_RELATION_KEY:
702 printf("%llu/%llu", btrfs_qgroup_level(objectid),
703 btrfs_qgroup_subvid(objectid));
704 return;
705 case BTRFS_UUID_KEY_SUBVOL:
706 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
707 printf("0x%016llx", (unsigned long long)objectid);
708 return;
711 switch (objectid) {
712 case BTRFS_ROOT_TREE_OBJECTID:
713 if (type == BTRFS_DEV_ITEM_KEY)
714 printf("DEV_ITEMS");
715 else
716 printf("ROOT_TREE");
717 break;
718 case BTRFS_EXTENT_TREE_OBJECTID:
719 printf("EXTENT_TREE");
720 break;
721 case BTRFS_CHUNK_TREE_OBJECTID:
722 printf("CHUNK_TREE");
723 break;
724 case BTRFS_DEV_TREE_OBJECTID:
725 printf("DEV_TREE");
726 break;
727 case BTRFS_FS_TREE_OBJECTID:
728 printf("FS_TREE");
729 break;
730 case BTRFS_ROOT_TREE_DIR_OBJECTID:
731 printf("ROOT_TREE_DIR");
732 break;
733 case BTRFS_CSUM_TREE_OBJECTID:
734 printf("CSUM_TREE");
735 break;
736 case BTRFS_BALANCE_OBJECTID:
737 printf("BALANCE");
738 break;
739 case BTRFS_ORPHAN_OBJECTID:
740 printf("ORPHAN");
741 break;
742 case BTRFS_TREE_LOG_OBJECTID:
743 printf("TREE_LOG");
744 break;
745 case BTRFS_TREE_LOG_FIXUP_OBJECTID:
746 printf("LOG_FIXUP");
747 break;
748 case BTRFS_TREE_RELOC_OBJECTID:
749 printf("TREE_RELOC");
750 break;
751 case BTRFS_DATA_RELOC_TREE_OBJECTID:
752 printf("DATA_RELOC_TREE");
753 break;
754 case BTRFS_EXTENT_CSUM_OBJECTID:
755 printf("EXTENT_CSUM");
756 break;
757 case BTRFS_FREE_SPACE_OBJECTID:
758 printf("FREE_SPACE");
759 break;
760 case BTRFS_FREE_INO_OBJECTID:
761 printf("FREE_INO");
762 break;
763 case BTRFS_QUOTA_TREE_OBJECTID:
764 printf("QUOTA_TREE");
765 break;
766 case BTRFS_UUID_TREE_OBJECTID:
767 printf("UUID_TREE");
768 break;
769 case BTRFS_FREE_SPACE_TREE_OBJECTID:
770 printf("FREE_SPACE_TREE");
771 break;
772 case BTRFS_MULTIPLE_OBJECTIDS:
773 printf("MULTIPLE");
774 break;
775 case (u64)-1:
776 printf("-1");
777 break;
778 case BTRFS_FIRST_CHUNK_TREE_OBJECTID:
779 if (type == BTRFS_CHUNK_ITEM_KEY) {
780 printf("FIRST_CHUNK_TREE");
781 break;
783 /* fall-thru */
784 default:
785 printf("%llu", (unsigned long long)objectid);
789 void btrfs_print_key(struct btrfs_disk_key *disk_key)
791 u64 objectid = btrfs_disk_key_objectid(disk_key);
792 u8 type = btrfs_disk_key_type(disk_key);
793 u64 offset = btrfs_disk_key_offset(disk_key);
795 printf("key (");
796 print_objectid(objectid, type);
797 printf(" ");
798 print_key_type(objectid, type);
799 switch (type) {
800 case BTRFS_QGROUP_RELATION_KEY:
801 case BTRFS_QGROUP_INFO_KEY:
802 case BTRFS_QGROUP_LIMIT_KEY:
803 printf(" %llu/%llu)", btrfs_qgroup_level(offset),
804 btrfs_qgroup_subvid(offset));
805 break;
806 case BTRFS_UUID_KEY_SUBVOL:
807 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
808 printf(" 0x%016llx)", (unsigned long long)offset);
809 break;
810 default:
811 if (offset == (u64)-1)
812 printf(" -1)");
813 else
814 printf(" %llu)", (unsigned long long)offset);
815 break;
819 static void print_uuid_item(struct extent_buffer *l, unsigned long offset,
820 u32 item_size)
822 if (item_size & (sizeof(u64) - 1)) {
823 printf("btrfs: uuid item with illegal size %lu!\n",
824 (unsigned long)item_size);
825 return;
827 while (item_size) {
828 __le64 subvol_id;
830 read_extent_buffer(l, &subvol_id, offset, sizeof(u64));
831 printf("\t\tsubvol_id %llu\n",
832 (unsigned long long)le64_to_cpu(subvol_id));
833 item_size -= sizeof(u64);
834 offset += sizeof(u64);
838 void btrfs_print_leaf(struct btrfs_root *root, struct extent_buffer *l)
840 int i;
841 char *str;
842 struct btrfs_item *item;
843 struct btrfs_dir_item *di;
844 struct btrfs_inode_item *ii;
845 struct btrfs_file_extent_item *fi;
846 struct btrfs_block_group_item *bi;
847 struct btrfs_extent_data_ref *dref;
848 struct btrfs_shared_data_ref *sref;
849 struct btrfs_inode_ref *iref;
850 struct btrfs_inode_extref *iref2;
851 struct btrfs_dev_extent *dev_extent;
852 struct btrfs_disk_key disk_key;
853 struct btrfs_block_group_item bg_item;
854 struct btrfs_free_space_info *free_info;
855 struct btrfs_dir_log_item *dlog;
856 struct btrfs_qgroup_info_item *qg_info;
857 struct btrfs_qgroup_limit_item *qg_limit;
858 struct btrfs_qgroup_status_item *qg_status;
859 u32 nr = btrfs_header_nritems(l);
860 u64 objectid;
861 u32 type;
862 char flags_str[32];
864 printf("leaf %llu items %d free space %d generation %llu owner %llu\n",
865 (unsigned long long)btrfs_header_bytenr(l), nr,
866 btrfs_leaf_free_space(root, l),
867 (unsigned long long)btrfs_header_generation(l),
868 (unsigned long long)btrfs_header_owner(l));
869 print_uuids(l);
870 fflush(stdout);
871 for (i = 0 ; i < nr ; i++) {
872 item = btrfs_item_nr(i);
873 btrfs_item_key(l, &disk_key, i);
874 objectid = btrfs_disk_key_objectid(&disk_key);
875 type = btrfs_disk_key_type(&disk_key);
876 printf("\titem %d ", i);
877 btrfs_print_key(&disk_key);
878 printf(" itemoff %d itemsize %d\n",
879 btrfs_item_offset(l, item),
880 btrfs_item_size(l, item));
882 if (type == 0 && objectid == BTRFS_FREE_SPACE_OBJECTID)
883 print_free_space_header(l, i);
885 switch (type) {
886 case BTRFS_INODE_ITEM_KEY:
887 ii = btrfs_item_ptr(l, i, struct btrfs_inode_item);
888 printf("\t\tinode generation %llu transid %llu size %llu nbytes %llu\n"
889 "\t\tblock group %llu mode %o links %u uid %u gid %u\n"
890 "\t\trdev %llu flags 0x%llx\n",
891 (unsigned long long)btrfs_inode_generation(l, ii),
892 (unsigned long long)btrfs_inode_transid(l, ii),
893 (unsigned long long)btrfs_inode_size(l, ii),
894 (unsigned long long)btrfs_inode_nbytes(l, ii),
895 (unsigned long long)btrfs_inode_block_group(l,ii),
896 btrfs_inode_mode(l, ii),
897 btrfs_inode_nlink(l, ii),
898 btrfs_inode_uid(l, ii),
899 btrfs_inode_gid(l, ii),
900 (unsigned long long)btrfs_inode_rdev(l,ii),
901 (unsigned long long)btrfs_inode_flags(l,ii));
902 break;
903 case BTRFS_INODE_REF_KEY:
904 iref = btrfs_item_ptr(l, i, struct btrfs_inode_ref);
905 print_inode_ref_item(l, item, iref);
906 break;
907 case BTRFS_INODE_EXTREF_KEY:
908 iref2 = btrfs_item_ptr(l, i, struct btrfs_inode_extref);
909 print_inode_extref_item(l, item, iref2);
910 break;
911 case BTRFS_DIR_ITEM_KEY:
912 case BTRFS_DIR_INDEX_KEY:
913 case BTRFS_XATTR_ITEM_KEY:
914 di = btrfs_item_ptr(l, i, struct btrfs_dir_item);
915 print_dir_item(l, item, di);
916 break;
917 case BTRFS_DIR_LOG_INDEX_KEY:
918 case BTRFS_DIR_LOG_ITEM_KEY:
919 dlog = btrfs_item_ptr(l, i, struct btrfs_dir_log_item);
920 printf("\t\tdir log end %Lu\n",
921 (unsigned long long)btrfs_dir_log_end(l, dlog));
922 break;
923 case BTRFS_ORPHAN_ITEM_KEY:
924 printf("\t\torphan item\n");
925 break;
926 case BTRFS_ROOT_ITEM_KEY:
927 print_root(l, i);
928 break;
929 case BTRFS_ROOT_REF_KEY:
930 print_root_ref(l, i, "ref");
931 break;
932 case BTRFS_ROOT_BACKREF_KEY:
933 print_root_ref(l, i, "backref");
934 break;
935 case BTRFS_EXTENT_ITEM_KEY:
936 print_extent_item(l, i, 0);
937 break;
938 case BTRFS_METADATA_ITEM_KEY:
939 print_extent_item(l, i, 1);
940 break;
941 case BTRFS_TREE_BLOCK_REF_KEY:
942 printf("\t\ttree block backref\n");
943 break;
944 case BTRFS_SHARED_BLOCK_REF_KEY:
945 printf("\t\tshared block backref\n");
946 break;
947 case BTRFS_EXTENT_DATA_REF_KEY:
948 dref = btrfs_item_ptr(l, i, struct btrfs_extent_data_ref);
949 printf("\t\textent data backref root %llu "
950 "objectid %llu offset %llu count %u\n",
951 (unsigned long long)btrfs_extent_data_ref_root(l, dref),
952 (unsigned long long)btrfs_extent_data_ref_objectid(l, dref),
953 (unsigned long long)btrfs_extent_data_ref_offset(l, dref),
954 btrfs_extent_data_ref_count(l, dref));
955 break;
956 case BTRFS_SHARED_DATA_REF_KEY:
957 sref = btrfs_item_ptr(l, i, struct btrfs_shared_data_ref);
958 printf("\t\tshared data backref count %u\n",
959 btrfs_shared_data_ref_count(l, sref));
960 break;
961 case BTRFS_EXTENT_REF_V0_KEY:
962 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
963 print_extent_ref_v0(l, i);
964 #else
965 BUG();
966 #endif
967 break;
968 case BTRFS_CSUM_ITEM_KEY:
969 printf("\t\tcsum item\n");
970 break;
971 case BTRFS_EXTENT_CSUM_KEY:
972 printf("\t\textent csum item\n");
973 break;
974 case BTRFS_EXTENT_DATA_KEY:
975 fi = btrfs_item_ptr(l, i,
976 struct btrfs_file_extent_item);
977 print_file_extent_item(l, item, i, fi);
978 break;
979 case BTRFS_BLOCK_GROUP_ITEM_KEY:
980 bi = btrfs_item_ptr(l, i,
981 struct btrfs_block_group_item);
982 read_extent_buffer(l, &bg_item, (unsigned long)bi,
983 sizeof(bg_item));
984 memset(flags_str, 0, sizeof(flags_str));
985 bg_flags_to_str(btrfs_block_group_flags(&bg_item),
986 flags_str);
987 printf("\t\tblock group used %llu chunk_objectid %llu flags %s\n",
988 (unsigned long long)btrfs_block_group_used(&bg_item),
989 (unsigned long long)btrfs_block_group_chunk_objectid(&bg_item),
990 flags_str);
991 break;
992 case BTRFS_FREE_SPACE_INFO_KEY:
993 free_info = btrfs_item_ptr(l, i, struct btrfs_free_space_info);
994 printf("\t\tfree space info extent count %u flags %u\n",
995 (unsigned)btrfs_free_space_extent_count(l, free_info),
996 (unsigned)btrfs_free_space_flags(l, free_info));
997 break;
998 case BTRFS_FREE_SPACE_EXTENT_KEY:
999 printf("\t\tfree space extent\n");
1000 break;
1001 case BTRFS_FREE_SPACE_BITMAP_KEY:
1002 printf("\t\tfree space bitmap\n");
1003 break;
1004 case BTRFS_CHUNK_ITEM_KEY:
1005 print_chunk(l, btrfs_item_ptr(l, i, struct btrfs_chunk));
1006 break;
1007 case BTRFS_DEV_ITEM_KEY:
1008 print_dev_item(l, btrfs_item_ptr(l, i,
1009 struct btrfs_dev_item));
1010 break;
1011 case BTRFS_DEV_EXTENT_KEY:
1012 dev_extent = btrfs_item_ptr(l, i,
1013 struct btrfs_dev_extent);
1014 printf("\t\tdev extent chunk_tree %llu\n"
1015 "\t\tchunk objectid %llu chunk offset %llu "
1016 "length %llu\n",
1017 (unsigned long long)
1018 btrfs_dev_extent_chunk_tree(l, dev_extent),
1019 (unsigned long long)
1020 btrfs_dev_extent_chunk_objectid(l, dev_extent),
1021 (unsigned long long)
1022 btrfs_dev_extent_chunk_offset(l, dev_extent),
1023 (unsigned long long)
1024 btrfs_dev_extent_length(l, dev_extent));
1025 break;
1026 case BTRFS_QGROUP_STATUS_KEY:
1027 qg_status = btrfs_item_ptr(l, i,
1028 struct btrfs_qgroup_status_item);
1029 memset(flags_str, 0, sizeof(flags_str));
1030 qgroup_flags_to_str(btrfs_qgroup_status_flags(l, qg_status),
1031 flags_str);
1032 printf("\t\tversion %llu generation %llu flags %s "
1033 "scan %lld\n",
1034 (unsigned long long)
1035 btrfs_qgroup_status_version(l, qg_status),
1036 (unsigned long long)
1037 btrfs_qgroup_status_generation(l, qg_status),
1038 flags_str,
1039 (unsigned long long)
1040 btrfs_qgroup_status_scan(l, qg_status));
1041 break;
1042 case BTRFS_QGROUP_RELATION_KEY:
1043 break;
1044 case BTRFS_QGROUP_INFO_KEY:
1045 qg_info = btrfs_item_ptr(l, i,
1046 struct btrfs_qgroup_info_item);
1047 printf("\t\tgeneration %llu\n"
1048 "\t\treferenced %llu referenced compressed %llu\n"
1049 "\t\texclusive %llu exclusive compressed %llu\n",
1050 (unsigned long long)
1051 btrfs_qgroup_info_generation(l, qg_info),
1052 (unsigned long long)
1053 btrfs_qgroup_info_referenced(l, qg_info),
1054 (unsigned long long)
1055 btrfs_qgroup_info_referenced_compressed(l,
1056 qg_info),
1057 (unsigned long long)
1058 btrfs_qgroup_info_exclusive(l, qg_info),
1059 (unsigned long long)
1060 btrfs_qgroup_info_exclusive_compressed(l,
1061 qg_info));
1062 break;
1063 case BTRFS_QGROUP_LIMIT_KEY:
1064 qg_limit = btrfs_item_ptr(l, i,
1065 struct btrfs_qgroup_limit_item);
1066 printf("\t\tflags %llx\n"
1067 "\t\tmax referenced %lld max exclusive %lld\n"
1068 "\t\trsv referenced %lld rsv exclusive %lld\n",
1069 (unsigned long long)
1070 btrfs_qgroup_limit_flags(l, qg_limit),
1071 (long long)
1072 btrfs_qgroup_limit_max_referenced(l, qg_limit),
1073 (long long)
1074 btrfs_qgroup_limit_max_exclusive(l, qg_limit),
1075 (long long)
1076 btrfs_qgroup_limit_rsv_referenced(l, qg_limit),
1077 (long long)
1078 btrfs_qgroup_limit_rsv_exclusive(l, qg_limit));
1079 break;
1080 case BTRFS_UUID_KEY_SUBVOL:
1081 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
1082 print_uuid_item(l, btrfs_item_ptr_offset(l, i),
1083 btrfs_item_size_nr(l, i));
1084 break;
1085 case BTRFS_STRING_ITEM_KEY:
1086 /* dirty, but it's simple */
1087 str = l->data + btrfs_item_ptr_offset(l, i);
1088 printf("\t\titem data %.*s\n", btrfs_item_size(l, item), str);
1089 break;
1090 case BTRFS_DEV_STATS_KEY:
1091 printf("\t\tdevice stats\n");
1092 break;
1094 fflush(stdout);
1098 void btrfs_print_tree(struct btrfs_root *root, struct extent_buffer *eb, int follow)
1100 int i;
1101 u32 nr;
1102 u32 size;
1103 struct btrfs_disk_key disk_key;
1104 struct btrfs_key key;
1106 if (!eb)
1107 return;
1108 nr = btrfs_header_nritems(eb);
1109 if (btrfs_is_leaf(eb)) {
1110 btrfs_print_leaf(root, eb);
1111 return;
1113 printf("node %llu level %d items %d free %u generation %llu owner %llu\n",
1114 (unsigned long long)eb->start,
1115 btrfs_header_level(eb), nr,
1116 (u32)BTRFS_NODEPTRS_PER_BLOCK(root) - nr,
1117 (unsigned long long)btrfs_header_generation(eb),
1118 (unsigned long long)btrfs_header_owner(eb));
1119 print_uuids(eb);
1120 fflush(stdout);
1121 size = btrfs_level_size(root, btrfs_header_level(eb) - 1);
1122 for (i = 0; i < nr; i++) {
1123 u64 blocknr = btrfs_node_blockptr(eb, i);
1124 btrfs_node_key(eb, &disk_key, i);
1125 btrfs_disk_key_to_cpu(&key, &disk_key);
1126 printf("\t");
1127 btrfs_print_key(&disk_key);
1128 printf(" block %llu (%llu) gen %llu\n",
1129 (unsigned long long)blocknr,
1130 (unsigned long long)blocknr / size,
1131 (unsigned long long)btrfs_node_ptr_generation(eb, i));
1132 fflush(stdout);
1134 if (!follow)
1135 return;
1137 for (i = 0; i < nr; i++) {
1138 struct extent_buffer *next = read_tree_block(root,
1139 btrfs_node_blockptr(eb, i),
1140 size,
1141 btrfs_node_ptr_generation(eb, i));
1142 if (!extent_buffer_uptodate(next)) {
1143 fprintf(stderr, "failed to read %llu in tree %llu\n",
1144 (unsigned long long)btrfs_node_blockptr(eb, i),
1145 (unsigned long long)btrfs_header_owner(eb));
1146 continue;
1148 if (btrfs_is_leaf(next) &&
1149 btrfs_header_level(eb) != 1)
1150 BUG();
1151 if (btrfs_header_level(next) !=
1152 btrfs_header_level(eb) - 1)
1153 BUG();
1154 btrfs_print_tree(root, next, 1);
1155 free_extent_buffer(next);