btrfs-progs: misc-test: use raid1 for data to enable mount with -o degraded
[btrfs-progs-unstable/devel.git] / print-tree.c
blob3c585e31f1fc09b47f688130f2ad06867e3d9d45
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);
34 static const char* dir_item_str[] = {
35 [BTRFS_FT_REG_FILE] = "FILE",
36 [BTRFS_FT_DIR] = "DIR",
37 [BTRFS_FT_CHRDEV] = "CHRDEV",
38 [BTRFS_FT_BLKDEV] = "BLKDEV",
39 [BTRFS_FT_FIFO] = "FIFO",
40 [BTRFS_FT_SOCK] = "SOCK",
41 [BTRFS_FT_SYMLINK] = "SYMLINK",
42 [BTRFS_FT_XATTR] = "XATTR"
45 if (type < ARRAY_SIZE(dir_item_str) && dir_item_str[type])
46 printf("%s", dir_item_str[type]);
47 else
48 printf("DIR_ITEM.%u", type);
51 static void print_dir_item(struct extent_buffer *eb, u32 size,
52 struct btrfs_dir_item *di)
54 u32 cur = 0;
55 u32 len;
56 u32 name_len;
57 u32 data_len;
58 char namebuf[BTRFS_NAME_LEN];
59 struct btrfs_disk_key location;
61 while (cur < size) {
62 btrfs_dir_item_key(eb, di, &location);
63 printf("\t\tlocation ");
64 btrfs_print_key(&location);
65 printf(" type ");
66 print_dir_item_type(eb, di);
67 printf("\n");
68 name_len = btrfs_dir_name_len(eb, di);
69 data_len = btrfs_dir_data_len(eb, di);
70 len = (name_len <= sizeof(namebuf))? name_len: sizeof(namebuf);
71 read_extent_buffer(eb, namebuf, (unsigned long)(di + 1), len);
72 printf("\t\ttransid %llu data_len %u name_len %u\n",
73 btrfs_dir_transid(eb, di),
74 data_len, name_len);
75 printf("\t\tname: %.*s\n", len, namebuf);
76 if (data_len) {
77 len = (data_len <= sizeof(namebuf))? data_len: sizeof(namebuf);
78 read_extent_buffer(eb, namebuf,
79 (unsigned long)(di + 1) + name_len, len);
80 printf("\t\tdata %.*s\n", len, namebuf);
82 len = sizeof(*di) + name_len + data_len;
83 di = (struct btrfs_dir_item *)((char *)di + len);
84 cur += len;
88 static void print_inode_extref_item(struct extent_buffer *eb, u32 size,
89 struct btrfs_inode_extref *extref)
91 u32 cur = 0;
92 u32 len;
93 u32 name_len = 0;
94 u64 index = 0;
95 u64 parent_objid;
96 char namebuf[BTRFS_NAME_LEN];
98 while (cur < size) {
99 index = btrfs_inode_extref_index(eb, extref);
100 name_len = btrfs_inode_extref_name_len(eb, extref);
101 parent_objid = btrfs_inode_extref_parent(eb, extref);
103 len = (name_len <= sizeof(namebuf))? name_len: sizeof(namebuf);
105 read_extent_buffer(eb, namebuf, (unsigned long)(extref->name), len);
107 printf("\t\tindex %llu parent %llu namelen %u name: %.*s\n",
108 (unsigned long long)index,
109 (unsigned long long)parent_objid,
110 name_len, len, namebuf);
112 len = sizeof(*extref) + name_len;
113 extref = (struct btrfs_inode_extref *)((char *)extref + len);
114 cur += len;
118 static void print_inode_ref_item(struct extent_buffer *eb, u32 size,
119 struct btrfs_inode_ref *ref)
121 u32 cur = 0;
122 u32 len;
123 u32 name_len;
124 u64 index;
125 char namebuf[BTRFS_NAME_LEN];
127 while (cur < size) {
128 name_len = btrfs_inode_ref_name_len(eb, ref);
129 index = btrfs_inode_ref_index(eb, ref);
130 len = (name_len <= sizeof(namebuf))? name_len: sizeof(namebuf);
131 read_extent_buffer(eb, namebuf, (unsigned long)(ref + 1), len);
132 printf("\t\tindex %llu namelen %u name: %.*s\n",
133 (unsigned long long)index, name_len, len, namebuf);
134 len = sizeof(*ref) + name_len;
135 ref = (struct btrfs_inode_ref *)((char *)ref + len);
136 cur += len;
140 /* Caller should ensure sizeof(*ret)>=21 "DATA|METADATA|RAID10" */
141 static void bg_flags_to_str(u64 flags, char *ret)
143 int empty = 1;
145 if (flags & BTRFS_BLOCK_GROUP_DATA) {
146 empty = 0;
147 strcpy(ret, "DATA");
149 if (flags & BTRFS_BLOCK_GROUP_METADATA) {
150 if (!empty)
151 strcat(ret, "|");
152 strcat(ret, "METADATA");
154 if (flags & BTRFS_BLOCK_GROUP_SYSTEM) {
155 if (!empty)
156 strcat(ret, "|");
157 strcat(ret, "SYSTEM");
159 switch (flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
160 case BTRFS_BLOCK_GROUP_RAID0:
161 strcat(ret, "|RAID0");
162 break;
163 case BTRFS_BLOCK_GROUP_RAID1:
164 strcat(ret, "|RAID1");
165 break;
166 case BTRFS_BLOCK_GROUP_DUP:
167 strcat(ret, "|DUP");
168 break;
169 case BTRFS_BLOCK_GROUP_RAID10:
170 strcat(ret, "|RAID10");
171 break;
172 case BTRFS_BLOCK_GROUP_RAID5:
173 strcat(ret, "|RAID5");
174 break;
175 case BTRFS_BLOCK_GROUP_RAID6:
176 strcat(ret, "|RAID6");
177 break;
178 default:
179 break;
183 /* Caller should ensure sizeof(*ret)>= 26 "OFF|SCANNING|INCONSISTENT" */
184 static void qgroup_flags_to_str(u64 flags, char *ret)
186 if (flags & BTRFS_QGROUP_STATUS_FLAG_ON)
187 strcpy(ret, "ON");
188 else
189 strcpy(ret, "OFF");
191 if (flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
192 strcat(ret, "|SCANNING");
193 if (flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT)
194 strcat(ret, "|INCONSISTENT");
197 void print_chunk_item(struct extent_buffer *eb, struct btrfs_chunk *chunk)
199 u16 num_stripes = btrfs_chunk_num_stripes(eb, chunk);
200 int i;
201 u32 chunk_item_size;
202 char chunk_flags_str[32] = {0};
204 /* The chunk must contain at least one stripe */
205 if (num_stripes < 1) {
206 printf("invalid num_stripes: %u\n", num_stripes);
207 return;
210 chunk_item_size = btrfs_chunk_item_size(num_stripes);
212 if ((unsigned long)chunk + chunk_item_size > eb->len) {
213 printf("\t\tchunk item invalid\n");
214 return;
217 bg_flags_to_str(btrfs_chunk_type(eb, chunk), chunk_flags_str);
218 printf("\t\tlength %llu owner %llu stripe_len %llu type %s\n",
219 (unsigned long long)btrfs_chunk_length(eb, chunk),
220 (unsigned long long)btrfs_chunk_owner(eb, chunk),
221 (unsigned long long)btrfs_chunk_stripe_len(eb, chunk),
222 chunk_flags_str);
223 printf("\t\tio_align %u io_width %u sector_size %u\n",
224 btrfs_chunk_io_align(eb, chunk),
225 btrfs_chunk_io_width(eb, chunk),
226 btrfs_chunk_sector_size(eb, chunk));
227 printf("\t\tnum_stripes %hu sub_stripes %hu\n", num_stripes,
228 btrfs_chunk_sub_stripes(eb, chunk));
229 for (i = 0 ; i < num_stripes ; i++) {
230 unsigned char dev_uuid[BTRFS_UUID_SIZE];
231 char str_dev_uuid[BTRFS_UUID_UNPARSED_SIZE];
232 u64 uuid_offset;
233 u64 stripe_offset;
235 uuid_offset = (unsigned long)btrfs_stripe_dev_uuid_nr(chunk, i);
236 stripe_offset = (unsigned long)btrfs_stripe_nr(chunk, i);
238 if (uuid_offset < stripe_offset ||
239 (uuid_offset + BTRFS_UUID_SIZE) >
240 (stripe_offset + sizeof(struct btrfs_stripe))) {
241 printf("\t\t\tstripe %d invalid\n", i);
242 break;
245 read_extent_buffer(eb, dev_uuid,
246 uuid_offset,
247 BTRFS_UUID_SIZE);
248 uuid_unparse(dev_uuid, str_dev_uuid);
249 printf("\t\t\tstripe %d devid %llu offset %llu\n", i,
250 (unsigned long long)btrfs_stripe_devid_nr(eb, chunk, i),
251 (unsigned long long)btrfs_stripe_offset_nr(eb, chunk, i));
252 printf("\t\t\tdev_uuid %s\n", str_dev_uuid);
256 static void print_dev_item(struct extent_buffer *eb,
257 struct btrfs_dev_item *dev_item)
259 char uuid_str[BTRFS_UUID_UNPARSED_SIZE];
260 char fsid_str[BTRFS_UUID_UNPARSED_SIZE];
261 u8 uuid[BTRFS_UUID_SIZE];
262 u8 fsid[BTRFS_UUID_SIZE];
264 read_extent_buffer(eb, uuid,
265 (unsigned long)btrfs_device_uuid(dev_item),
266 BTRFS_UUID_SIZE);
267 uuid_unparse(uuid, uuid_str);
268 read_extent_buffer(eb, fsid,
269 (unsigned long)btrfs_device_fsid(dev_item),
270 BTRFS_UUID_SIZE);
271 uuid_unparse(fsid, fsid_str);
272 printf("\t\tdevid %llu total_bytes %llu bytes_used %Lu\n"
273 "\t\tio_align %u io_width %u sector_size %u type %llu\n"
274 "\t\tgeneration %llu start_offset %llu dev_group %u\n"
275 "\t\tseek_speed %hhu bandwidth %hhu\n"
276 "\t\tuuid %s\n"
277 "\t\tfsid %s\n",
278 (unsigned long long)btrfs_device_id(eb, dev_item),
279 (unsigned long long)btrfs_device_total_bytes(eb, dev_item),
280 (unsigned long long)btrfs_device_bytes_used(eb, dev_item),
281 btrfs_device_io_align(eb, dev_item),
282 btrfs_device_io_width(eb, dev_item),
283 btrfs_device_sector_size(eb, dev_item),
284 (unsigned long long)btrfs_device_type(eb, dev_item),
285 (unsigned long long)btrfs_device_generation(eb, dev_item),
286 (unsigned long long)btrfs_device_start_offset(eb, dev_item),
287 btrfs_device_group(eb, dev_item),
288 btrfs_device_seek_speed(eb, dev_item),
289 btrfs_device_bandwidth(eb, dev_item),
290 uuid_str, fsid_str);
293 static void print_uuids(struct extent_buffer *eb)
295 char fs_uuid[BTRFS_UUID_UNPARSED_SIZE];
296 char chunk_uuid[BTRFS_UUID_UNPARSED_SIZE];
297 u8 disk_uuid[BTRFS_UUID_SIZE];
299 read_extent_buffer(eb, disk_uuid, btrfs_header_fsid(),
300 BTRFS_FSID_SIZE);
302 fs_uuid[BTRFS_UUID_UNPARSED_SIZE - 1] = '\0';
303 uuid_unparse(disk_uuid, fs_uuid);
305 read_extent_buffer(eb, disk_uuid,
306 btrfs_header_chunk_tree_uuid(eb),
307 BTRFS_UUID_SIZE);
309 chunk_uuid[BTRFS_UUID_UNPARSED_SIZE - 1] = '\0';
310 uuid_unparse(disk_uuid, chunk_uuid);
311 printf("fs uuid %s\nchunk uuid %s\n", fs_uuid, chunk_uuid);
314 static void compress_type_to_str(u8 compress_type, char *ret)
316 switch (compress_type) {
317 case BTRFS_COMPRESS_NONE:
318 strcpy(ret, "none");
319 break;
320 case BTRFS_COMPRESS_ZLIB:
321 strcpy(ret, "zlib");
322 break;
323 case BTRFS_COMPRESS_LZO:
324 strcpy(ret, "lzo");
325 break;
326 case BTRFS_COMPRESS_ZSTD:
327 strcpy(ret, "zstd");
328 break;
329 default:
330 sprintf(ret, "UNKNOWN.%d", compress_type);
334 static const char* file_extent_type_to_str(u8 type)
336 switch (type) {
337 case BTRFS_FILE_EXTENT_INLINE: return "inline";
338 case BTRFS_FILE_EXTENT_PREALLOC: return "prealloc";
339 case BTRFS_FILE_EXTENT_REG: return "regular";
340 default: return "unknown";
344 static void print_file_extent_item(struct extent_buffer *eb,
345 struct btrfs_item *item,
346 int slot,
347 struct btrfs_file_extent_item *fi)
349 unsigned char extent_type = btrfs_file_extent_type(eb, fi);
350 char compress_str[16];
352 compress_type_to_str(btrfs_file_extent_compression(eb, fi),
353 compress_str);
355 printf("\t\tgeneration %llu type %hhu (%s)\n",
356 btrfs_file_extent_generation(eb, fi),
357 extent_type, file_extent_type_to_str(extent_type));
359 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
360 printf("\t\tinline extent data size %u ram_bytes %u compression %hhu (%s)\n",
361 btrfs_file_extent_inline_item_len(eb, item),
362 btrfs_file_extent_inline_len(eb, slot, fi),
363 btrfs_file_extent_compression(eb, fi),
364 compress_str);
365 return;
367 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
368 printf("\t\tprealloc data disk byte %llu nr %llu\n",
369 (unsigned long long)btrfs_file_extent_disk_bytenr(eb, fi),
370 (unsigned long long)btrfs_file_extent_disk_num_bytes(eb, fi));
371 printf("\t\tprealloc data offset %llu nr %llu\n",
372 (unsigned long long)btrfs_file_extent_offset(eb, fi),
373 (unsigned long long)btrfs_file_extent_num_bytes(eb, fi));
374 return;
376 printf("\t\textent data disk byte %llu nr %llu\n",
377 (unsigned long long)btrfs_file_extent_disk_bytenr(eb, fi),
378 (unsigned long long)btrfs_file_extent_disk_num_bytes(eb, fi));
379 printf("\t\textent data offset %llu nr %llu ram %llu\n",
380 (unsigned long long)btrfs_file_extent_offset(eb, fi),
381 (unsigned long long)btrfs_file_extent_num_bytes(eb, fi),
382 (unsigned long long)btrfs_file_extent_ram_bytes(eb, fi));
383 printf("\t\textent compression %hhu (%s)\n",
384 btrfs_file_extent_compression(eb, fi),
385 compress_str);
388 /* Caller should ensure sizeof(*ret) >= 16("DATA|TREE_BLOCK") */
389 static void extent_flags_to_str(u64 flags, char *ret)
391 int empty = 1;
393 if (flags & BTRFS_EXTENT_FLAG_DATA) {
394 empty = 0;
395 strcpy(ret, "DATA");
397 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
398 if (!empty) {
399 empty = 0;
400 strcat(ret, "|");
402 strcat(ret, "TREE_BLOCK");
404 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
405 strcat(ret, "|");
406 strcat(ret, "FULL_BACKREF");
410 void print_extent_item(struct extent_buffer *eb, int slot, int metadata)
412 struct btrfs_extent_item *ei;
413 struct btrfs_extent_inline_ref *iref;
414 struct btrfs_extent_data_ref *dref;
415 struct btrfs_shared_data_ref *sref;
416 struct btrfs_disk_key key;
417 unsigned long end;
418 unsigned long ptr;
419 int type;
420 u32 item_size = btrfs_item_size_nr(eb, slot);
421 u64 flags;
422 u64 offset;
423 char flags_str[32] = {0};
425 if (item_size < sizeof(*ei)) {
426 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
427 struct btrfs_extent_item_v0 *ei0;
428 BUG_ON(item_size != sizeof(*ei0));
429 ei0 = btrfs_item_ptr(eb, slot, struct btrfs_extent_item_v0);
430 printf("\t\trefs %u\n",
431 btrfs_extent_refs_v0(eb, ei0));
432 return;
433 #else
434 BUG();
435 #endif
438 ei = btrfs_item_ptr(eb, slot, struct btrfs_extent_item);
439 flags = btrfs_extent_flags(eb, ei);
440 extent_flags_to_str(flags, flags_str);
442 printf("\t\trefs %llu gen %llu flags %s\n",
443 (unsigned long long)btrfs_extent_refs(eb, ei),
444 (unsigned long long)btrfs_extent_generation(eb, ei),
445 flags_str);
447 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !metadata) {
448 struct btrfs_tree_block_info *info;
449 info = (struct btrfs_tree_block_info *)(ei + 1);
450 btrfs_tree_block_key(eb, info, &key);
451 printf("\t\ttree block ");
452 btrfs_print_key(&key);
453 printf(" level %d\n", btrfs_tree_block_level(eb, info));
454 iref = (struct btrfs_extent_inline_ref *)(info + 1);
455 } else if (metadata) {
456 struct btrfs_key tmp;
458 btrfs_item_key_to_cpu(eb, &tmp, slot);
459 printf("\t\ttree block skinny level %d\n", (int)tmp.offset);
460 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
461 } else{
462 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
465 ptr = (unsigned long)iref;
466 end = (unsigned long)ei + item_size;
467 while (ptr < end) {
468 iref = (struct btrfs_extent_inline_ref *)ptr;
469 type = btrfs_extent_inline_ref_type(eb, iref);
470 offset = btrfs_extent_inline_ref_offset(eb, iref);
471 switch (type) {
472 case BTRFS_TREE_BLOCK_REF_KEY:
473 printf("\t\ttree block backref root %llu\n",
474 (unsigned long long)offset);
475 break;
476 case BTRFS_SHARED_BLOCK_REF_KEY:
477 printf("\t\tshared block backref parent %llu\n",
478 (unsigned long long)offset);
479 break;
480 case BTRFS_EXTENT_DATA_REF_KEY:
481 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
482 printf("\t\textent data backref root %llu "
483 "objectid %llu offset %llu count %u\n",
484 (unsigned long long)btrfs_extent_data_ref_root(eb, dref),
485 (unsigned long long)btrfs_extent_data_ref_objectid(eb, dref),
486 (unsigned long long)btrfs_extent_data_ref_offset(eb, dref),
487 btrfs_extent_data_ref_count(eb, dref));
488 break;
489 case BTRFS_SHARED_DATA_REF_KEY:
490 sref = (struct btrfs_shared_data_ref *)(iref + 1);
491 printf("\t\tshared data backref parent %llu count %u\n",
492 (unsigned long long)offset,
493 btrfs_shared_data_ref_count(eb, sref));
494 break;
495 default:
496 return;
498 ptr += btrfs_extent_inline_ref_size(type);
500 WARN_ON(ptr > end);
503 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
504 static void print_extent_ref_v0(struct extent_buffer *eb, int slot)
506 struct btrfs_extent_ref_v0 *ref0;
508 ref0 = btrfs_item_ptr(eb, slot, struct btrfs_extent_ref_v0);
509 printf("\t\textent back ref root %llu gen %llu "
510 "owner %llu num_refs %lu\n",
511 (unsigned long long)btrfs_ref_root_v0(eb, ref0),
512 (unsigned long long)btrfs_ref_generation_v0(eb, ref0),
513 (unsigned long long)btrfs_ref_objectid_v0(eb, ref0),
514 (unsigned long)btrfs_ref_count_v0(eb, ref0));
516 #endif
518 static void print_root_ref(struct extent_buffer *leaf, int slot, const char *tag)
520 struct btrfs_root_ref *ref;
521 char namebuf[BTRFS_NAME_LEN];
522 int namelen;
524 ref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
525 namelen = btrfs_root_ref_name_len(leaf, ref);
526 read_extent_buffer(leaf, namebuf, (unsigned long)(ref + 1), namelen);
527 printf("\t\troot %s key dirid %llu sequence %llu name %.*s\n", tag,
528 (unsigned long long)btrfs_root_ref_dirid(leaf, ref),
529 (unsigned long long)btrfs_root_ref_sequence(leaf, ref),
530 namelen, namebuf);
533 static int empty_uuid(const u8 *uuid)
535 int i;
537 for (i = 0; i < BTRFS_UUID_SIZE; i++)
538 if (uuid[i])
539 return 0;
540 return 1;
544 * Caller must ensure sizeof(*ret) >= 7 "RDONLY"
546 static void root_flags_to_str(u64 flags, char *ret)
548 if (flags & BTRFS_ROOT_SUBVOL_RDONLY)
549 strcat(ret, "RDONLY");
550 else
551 strcat(ret, "none");
554 static void print_root_item(struct extent_buffer *leaf, int slot)
556 struct btrfs_root_item *ri;
557 struct btrfs_root_item root_item;
558 int len;
559 char uuid_str[BTRFS_UUID_UNPARSED_SIZE];
560 char flags_str[32] = {0};
561 struct btrfs_key drop_key;
563 ri = btrfs_item_ptr(leaf, slot, struct btrfs_root_item);
564 len = btrfs_item_size_nr(leaf, slot);
566 memset(&root_item, 0, sizeof(root_item));
567 read_extent_buffer(leaf, &root_item, (unsigned long)ri, len);
568 root_flags_to_str(btrfs_root_flags(&root_item), flags_str);
570 printf("\t\tgeneration %llu root_dirid %llu bytenr %llu level %hhu refs %u\n",
571 (unsigned long long)btrfs_root_generation(&root_item),
572 (unsigned long long)btrfs_root_dirid(&root_item),
573 (unsigned long long)btrfs_root_bytenr(&root_item),
574 btrfs_root_level(&root_item),
575 btrfs_root_refs(&root_item));
576 printf("\t\tlastsnap %llu byte_limit %llu bytes_used %llu flags 0x%llx(%s)\n",
577 (unsigned long long)btrfs_root_last_snapshot(&root_item),
578 (unsigned long long)btrfs_root_limit(&root_item),
579 (unsigned long long)btrfs_root_used(&root_item),
580 (unsigned long long)btrfs_root_flags(&root_item),
581 flags_str);
583 if (root_item.generation == root_item.generation_v2) {
584 uuid_unparse(root_item.uuid, uuid_str);
585 printf("\t\tuuid %s\n", uuid_str);
586 if (!empty_uuid(root_item.parent_uuid)) {
587 uuid_unparse(root_item.parent_uuid, uuid_str);
588 printf("\t\tparent_uuid %s\n", uuid_str);
590 if (!empty_uuid(root_item.received_uuid)) {
591 uuid_unparse(root_item.received_uuid, uuid_str);
592 printf("\t\treceived_uuid %s\n", uuid_str);
594 if (root_item.ctransid) {
595 printf("\t\tctransid %llu otransid %llu stransid %llu rtransid %llu\n",
596 btrfs_root_ctransid(&root_item),
597 btrfs_root_otransid(&root_item),
598 btrfs_root_stransid(&root_item),
599 btrfs_root_rtransid(&root_item));
603 btrfs_disk_key_to_cpu(&drop_key, &root_item.drop_progress);
604 printf("\t\tdrop ");
605 btrfs_print_key(&root_item.drop_progress);
606 printf(" level %hhu\n", root_item.drop_level);
609 static void print_free_space_header(struct extent_buffer *leaf, int slot)
611 struct btrfs_free_space_header *header;
612 struct btrfs_disk_key location;
614 header = btrfs_item_ptr(leaf, slot, struct btrfs_free_space_header);
615 btrfs_free_space_key(leaf, header, &location);
616 printf("\t\tlocation ");
617 btrfs_print_key(&location);
618 printf("\n");
619 printf("\t\tcache generation %llu entries %llu bitmaps %llu\n",
620 (unsigned long long)btrfs_free_space_generation(leaf, header),
621 (unsigned long long)btrfs_free_space_entries(leaf, header),
622 (unsigned long long)btrfs_free_space_bitmaps(leaf, header));
625 void print_key_type(FILE *stream, u64 objectid, u8 type)
627 static const char* key_to_str[256] = {
628 [BTRFS_INODE_ITEM_KEY] = "INODE_ITEM",
629 [BTRFS_INODE_REF_KEY] = "INODE_REF",
630 [BTRFS_INODE_EXTREF_KEY] = "INODE_EXTREF",
631 [BTRFS_DIR_ITEM_KEY] = "DIR_ITEM",
632 [BTRFS_DIR_INDEX_KEY] = "DIR_INDEX",
633 [BTRFS_DIR_LOG_ITEM_KEY] = "DIR_LOG_ITEM",
634 [BTRFS_DIR_LOG_INDEX_KEY] = "DIR_LOG_INDEX",
635 [BTRFS_XATTR_ITEM_KEY] = "XATTR_ITEM",
636 [BTRFS_ORPHAN_ITEM_KEY] = "ORPHAN_ITEM",
637 [BTRFS_ROOT_ITEM_KEY] = "ROOT_ITEM",
638 [BTRFS_ROOT_REF_KEY] = "ROOT_REF",
639 [BTRFS_ROOT_BACKREF_KEY] = "ROOT_BACKREF",
640 [BTRFS_EXTENT_ITEM_KEY] = "EXTENT_ITEM",
641 [BTRFS_METADATA_ITEM_KEY] = "METADATA_ITEM",
642 [BTRFS_TREE_BLOCK_REF_KEY] = "TREE_BLOCK_REF",
643 [BTRFS_SHARED_BLOCK_REF_KEY] = "SHARED_BLOCK_REF",
644 [BTRFS_EXTENT_DATA_REF_KEY] = "EXTENT_DATA_REF",
645 [BTRFS_SHARED_DATA_REF_KEY] = "SHARED_DATA_REF",
646 [BTRFS_EXTENT_REF_V0_KEY] = "EXTENT_REF_V0",
647 [BTRFS_CSUM_ITEM_KEY] = "CSUM_ITEM",
648 [BTRFS_EXTENT_CSUM_KEY] = "EXTENT_CSUM",
649 [BTRFS_EXTENT_DATA_KEY] = "EXTENT_DATA",
650 [BTRFS_BLOCK_GROUP_ITEM_KEY] = "BLOCK_GROUP_ITEM",
651 [BTRFS_FREE_SPACE_INFO_KEY] = "FREE_SPACE_INFO",
652 [BTRFS_FREE_SPACE_EXTENT_KEY] = "FREE_SPACE_EXTENT",
653 [BTRFS_FREE_SPACE_BITMAP_KEY] = "FREE_SPACE_BITMAP",
654 [BTRFS_CHUNK_ITEM_KEY] = "CHUNK_ITEM",
655 [BTRFS_DEV_ITEM_KEY] = "DEV_ITEM",
656 [BTRFS_DEV_EXTENT_KEY] = "DEV_EXTENT",
657 [BTRFS_TEMPORARY_ITEM_KEY] = "TEMPORARY_ITEM",
658 [BTRFS_DEV_REPLACE_KEY] = "DEV_REPLACE",
659 [BTRFS_STRING_ITEM_KEY] = "STRING_ITEM",
660 [BTRFS_QGROUP_STATUS_KEY] = "QGROUP_STATUS",
661 [BTRFS_QGROUP_RELATION_KEY] = "QGROUP_RELATION",
662 [BTRFS_QGROUP_INFO_KEY] = "QGROUP_INFO",
663 [BTRFS_QGROUP_LIMIT_KEY] = "QGROUP_LIMIT",
664 [BTRFS_PERSISTENT_ITEM_KEY] = "PERSISTENT_ITEM",
665 [BTRFS_UUID_KEY_SUBVOL] = "UUID_KEY_SUBVOL",
666 [BTRFS_UUID_KEY_RECEIVED_SUBVOL] = "UUID_KEY_RECEIVED_SUBVOL",
669 if (type == 0 && objectid == BTRFS_FREE_SPACE_OBJECTID) {
670 fprintf(stream, "UNTYPED");
671 return;
675 if (key_to_str[type])
676 fputs(key_to_str[type], stream);
677 else
678 fprintf(stream, "UNKNOWN.%d", type);
681 void print_objectid(FILE *stream, u64 objectid, u8 type)
683 switch (type) {
684 case BTRFS_DEV_EXTENT_KEY:
685 /* device id */
686 fprintf(stream, "%llu", (unsigned long long)objectid);
687 return;
688 case BTRFS_QGROUP_RELATION_KEY:
689 fprintf(stream, "%llu/%llu", btrfs_qgroup_level(objectid),
690 btrfs_qgroup_subvid(objectid));
691 return;
692 case BTRFS_UUID_KEY_SUBVOL:
693 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
694 fprintf(stream, "0x%016llx", (unsigned long long)objectid);
695 return;
698 switch (objectid) {
699 case BTRFS_ROOT_TREE_OBJECTID:
700 if (type == BTRFS_DEV_ITEM_KEY)
701 fprintf(stream, "DEV_ITEMS");
702 else
703 fprintf(stream, "ROOT_TREE");
704 break;
705 case BTRFS_EXTENT_TREE_OBJECTID:
706 fprintf(stream, "EXTENT_TREE");
707 break;
708 case BTRFS_CHUNK_TREE_OBJECTID:
709 fprintf(stream, "CHUNK_TREE");
710 break;
711 case BTRFS_DEV_TREE_OBJECTID:
712 fprintf(stream, "DEV_TREE");
713 break;
714 case BTRFS_FS_TREE_OBJECTID:
715 fprintf(stream, "FS_TREE");
716 break;
717 case BTRFS_ROOT_TREE_DIR_OBJECTID:
718 fprintf(stream, "ROOT_TREE_DIR");
719 break;
720 case BTRFS_CSUM_TREE_OBJECTID:
721 fprintf(stream, "CSUM_TREE");
722 break;
723 case BTRFS_BALANCE_OBJECTID:
724 fprintf(stream, "BALANCE");
725 break;
726 case BTRFS_ORPHAN_OBJECTID:
727 fprintf(stream, "ORPHAN");
728 break;
729 case BTRFS_TREE_LOG_OBJECTID:
730 fprintf(stream, "TREE_LOG");
731 break;
732 case BTRFS_TREE_LOG_FIXUP_OBJECTID:
733 fprintf(stream, "LOG_FIXUP");
734 break;
735 case BTRFS_TREE_RELOC_OBJECTID:
736 fprintf(stream, "TREE_RELOC");
737 break;
738 case BTRFS_DATA_RELOC_TREE_OBJECTID:
739 fprintf(stream, "DATA_RELOC_TREE");
740 break;
741 case BTRFS_EXTENT_CSUM_OBJECTID:
742 fprintf(stream, "EXTENT_CSUM");
743 break;
744 case BTRFS_FREE_SPACE_OBJECTID:
745 fprintf(stream, "FREE_SPACE");
746 break;
747 case BTRFS_FREE_INO_OBJECTID:
748 fprintf(stream, "FREE_INO");
749 break;
750 case BTRFS_QUOTA_TREE_OBJECTID:
751 fprintf(stream, "QUOTA_TREE");
752 break;
753 case BTRFS_UUID_TREE_OBJECTID:
754 fprintf(stream, "UUID_TREE");
755 break;
756 case BTRFS_FREE_SPACE_TREE_OBJECTID:
757 fprintf(stream, "FREE_SPACE_TREE");
758 break;
759 case BTRFS_MULTIPLE_OBJECTIDS:
760 fprintf(stream, "MULTIPLE");
761 break;
762 case (u64)-1:
763 fprintf(stream, "-1");
764 break;
765 case BTRFS_FIRST_CHUNK_TREE_OBJECTID:
766 if (type == BTRFS_CHUNK_ITEM_KEY) {
767 fprintf(stream, "FIRST_CHUNK_TREE");
768 break;
770 /* fall-thru */
771 default:
772 fprintf(stream, "%llu", (unsigned long long)objectid);
776 void btrfs_print_key(struct btrfs_disk_key *disk_key)
778 u64 objectid = btrfs_disk_key_objectid(disk_key);
779 u8 type = btrfs_disk_key_type(disk_key);
780 u64 offset = btrfs_disk_key_offset(disk_key);
782 printf("key (");
783 print_objectid(stdout, objectid, type);
784 printf(" ");
785 print_key_type(stdout, objectid, type);
786 switch (type) {
787 case BTRFS_QGROUP_RELATION_KEY:
788 case BTRFS_QGROUP_INFO_KEY:
789 case BTRFS_QGROUP_LIMIT_KEY:
790 printf(" %llu/%llu)", btrfs_qgroup_level(offset),
791 btrfs_qgroup_subvid(offset));
792 break;
793 case BTRFS_UUID_KEY_SUBVOL:
794 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
795 printf(" 0x%016llx)", (unsigned long long)offset);
796 break;
797 default:
798 if (offset == (u64)-1)
799 printf(" -1)");
800 else
801 printf(" %llu)", (unsigned long long)offset);
802 break;
806 static void print_uuid_item(struct extent_buffer *l, unsigned long offset,
807 u32 item_size)
809 if (item_size & (sizeof(u64) - 1)) {
810 printf("btrfs: uuid item with illegal size %lu!\n",
811 (unsigned long)item_size);
812 return;
814 while (item_size) {
815 __le64 subvol_id;
817 read_extent_buffer(l, &subvol_id, offset, sizeof(u64));
818 printf("\t\tsubvol_id %llu\n",
819 (unsigned long long)le64_to_cpu(subvol_id));
820 item_size -= sizeof(u64);
821 offset += sizeof(u64);
825 /* Btrfs inode flag stringification helper */
826 #define STRCAT_ONE_INODE_FLAG(flags, name, empty, dst) ({ \
827 if (flags & BTRFS_INODE_##name) { \
828 if (!empty) \
829 strcat(dst, "|"); \
830 strcat(dst, #name); \
831 empty = 0; \
836 * Caller should ensure sizeof(*ret) >= 102: all charactors plus '|' of
837 * BTRFS_INODE_* flags
839 static void inode_flags_to_str(u64 flags, char *ret)
841 int empty = 1;
843 STRCAT_ONE_INODE_FLAG(flags, NODATASUM, empty, ret);
844 STRCAT_ONE_INODE_FLAG(flags, NODATACOW, empty, ret);
845 STRCAT_ONE_INODE_FLAG(flags, READONLY, empty, ret);
846 STRCAT_ONE_INODE_FLAG(flags, NOCOMPRESS, empty, ret);
847 STRCAT_ONE_INODE_FLAG(flags, PREALLOC, empty, ret);
848 STRCAT_ONE_INODE_FLAG(flags, SYNC, empty, ret);
849 STRCAT_ONE_INODE_FLAG(flags, IMMUTABLE, empty, ret);
850 STRCAT_ONE_INODE_FLAG(flags, APPEND, empty, ret);
851 STRCAT_ONE_INODE_FLAG(flags, NODUMP, empty, ret);
852 STRCAT_ONE_INODE_FLAG(flags, NOATIME, empty, ret);
853 STRCAT_ONE_INODE_FLAG(flags, DIRSYNC, empty, ret);
854 STRCAT_ONE_INODE_FLAG(flags, COMPRESS, empty, ret);
855 if (empty)
856 strcat(ret, "none");
859 static void print_timespec(struct extent_buffer *eb,
860 struct btrfs_timespec *timespec, const char *prefix,
861 const char *suffix)
863 struct tm tm;
864 u64 tmp_u64;
865 u32 tmp_u32;
866 time_t tmp_time;
867 char timestamp[256];
869 tmp_u64 = btrfs_timespec_sec(eb, timespec);
870 tmp_u32 = btrfs_timespec_nsec(eb, timespec);
871 tmp_time = tmp_u64;
872 localtime_r(&tmp_time, &tm);
873 strftime(timestamp, sizeof(timestamp),
874 "%Y-%m-%d %H:%M:%S", &tm);
875 printf("%s%llu.%u (%s)%s", prefix, (unsigned long long)tmp_u64, tmp_u32,
876 timestamp, suffix);
879 static void print_inode_item(struct extent_buffer *eb,
880 struct btrfs_inode_item *ii)
882 char flags_str[256];
884 memset(flags_str, 0, sizeof(flags_str));
885 inode_flags_to_str(btrfs_inode_flags(eb, ii), flags_str);
886 printf("\t\tgeneration %llu transid %llu size %llu nbytes %llu\n"
887 "\t\tblock group %llu mode %o links %u uid %u gid %u rdev %llu\n"
888 "\t\tsequence %llu flags 0x%llx(%s)\n",
889 (unsigned long long)btrfs_inode_generation(eb, ii),
890 (unsigned long long)btrfs_inode_transid(eb, ii),
891 (unsigned long long)btrfs_inode_size(eb, ii),
892 (unsigned long long)btrfs_inode_nbytes(eb, ii),
893 (unsigned long long)btrfs_inode_block_group(eb,ii),
894 btrfs_inode_mode(eb, ii),
895 btrfs_inode_nlink(eb, ii),
896 btrfs_inode_uid(eb, ii),
897 btrfs_inode_gid(eb, ii),
898 (unsigned long long)btrfs_inode_rdev(eb,ii),
899 (unsigned long long)btrfs_inode_flags(eb,ii),
900 (unsigned long long)btrfs_inode_sequence(eb, ii),
901 flags_str);
902 print_timespec(eb, btrfs_inode_atime(ii), "\t\tatime ", "\n");
903 print_timespec(eb, btrfs_inode_ctime(ii), "\t\tctime ", "\n");
904 print_timespec(eb, btrfs_inode_mtime(ii), "\t\tmtime ", "\n");
905 print_timespec(eb, btrfs_inode_otime(ii), "\t\totime ", "\n");
908 static void print_disk_balance_args(struct btrfs_disk_balance_args *ba)
910 printf("\t\tprofiles %llu devid %llu target %llu flags %llu\n",
911 (unsigned long long)le64_to_cpu(ba->profiles),
912 (unsigned long long)le64_to_cpu(ba->devid),
913 (unsigned long long)le64_to_cpu(ba->target),
914 (unsigned long long)le64_to_cpu(ba->flags));
915 printf("\t\tusage_min %u usage_max %u pstart %llu pend %llu\n",
916 le32_to_cpu(ba->usage_min),
917 le32_to_cpu(ba->usage_max),
918 (unsigned long long)le64_to_cpu(ba->pstart),
919 (unsigned long long)le64_to_cpu(ba->pend));
920 printf("\t\tvstart %llu vend %llu limit_min %u limit_max %u\n",
921 (unsigned long long)le64_to_cpu(ba->vstart),
922 (unsigned long long)le64_to_cpu(ba->vend),
923 le32_to_cpu(ba->limit_min),
924 le32_to_cpu(ba->limit_max));
925 printf("\t\tstripes_min %u stripes_max %u\n",
926 le32_to_cpu(ba->stripes_min),
927 le32_to_cpu(ba->stripes_max));
930 static void print_balance_item(struct extent_buffer *eb,
931 struct btrfs_balance_item *bi)
933 printf("\t\tbalance status flags %llu\n",
934 btrfs_balance_item_flags(eb, bi));
936 printf("\t\tDATA\n");
937 print_disk_balance_args(btrfs_balance_item_data(eb, bi));
938 printf("\t\tMETADATA\n");
939 print_disk_balance_args(btrfs_balance_item_meta(eb, bi));
940 printf("\t\tSYSTEM\n");
941 print_disk_balance_args(btrfs_balance_item_sys(eb, bi));
944 static void print_dev_stats(struct extent_buffer *eb,
945 struct btrfs_dev_stats_item *stats, u32 size)
947 int i;
948 u32 known = BTRFS_DEV_STAT_VALUES_MAX * sizeof(__le64);
949 __le64 *values = btrfs_dev_stats_values(eb, stats);
951 printf("\t\tdevice stats\n");
952 printf("\t\twrite_errs %llu read_errs %llu flush_errs %llu corruption_errs %llu generation %llu\n",
953 (unsigned long long)le64_to_cpu(values[BTRFS_DEV_STAT_WRITE_ERRS]),
954 (unsigned long long)le64_to_cpu(values[BTRFS_DEV_STAT_READ_ERRS]),
955 (unsigned long long)le64_to_cpu(values[BTRFS_DEV_STAT_FLUSH_ERRS]),
956 (unsigned long long)le64_to_cpu(values[BTRFS_DEV_STAT_CORRUPTION_ERRS]),
957 (unsigned long long)le64_to_cpu(values[BTRFS_DEV_STAT_GENERATION_ERRS]));
959 if (known < size) {
960 printf("\t\tunknown stats item bytes %u", size - known);
961 for (i = BTRFS_DEV_STAT_VALUES_MAX; i * sizeof(__le64) < size; i++) {
962 printf("\t\tunknown item %u offset %zu value %llu\n",
963 i, i * sizeof(__le64),
964 (unsigned long long)le64_to_cpu(values[i]));
969 static void print_block_group_item(struct extent_buffer *eb,
970 struct btrfs_block_group_item *bgi)
972 struct btrfs_block_group_item bg_item;
973 char flags_str[256];
975 read_extent_buffer(eb, &bg_item, (unsigned long)bgi, sizeof(bg_item));
976 memset(flags_str, 0, sizeof(flags_str));
977 bg_flags_to_str(btrfs_block_group_flags(&bg_item), flags_str);
978 printf("\t\tblock group used %llu chunk_objectid %llu flags %s\n",
979 (unsigned long long)btrfs_block_group_used(&bg_item),
980 (unsigned long long)btrfs_block_group_chunk_objectid(&bg_item),
981 flags_str);
984 static void print_extent_data_ref(struct extent_buffer *eb, int slot)
986 struct btrfs_extent_data_ref *dref;
988 dref = btrfs_item_ptr(eb, slot, struct btrfs_extent_data_ref);
989 printf("\t\textent data backref root %llu "
990 "objectid %llu offset %llu count %u\n",
991 (unsigned long long)btrfs_extent_data_ref_root(eb, dref),
992 (unsigned long long)btrfs_extent_data_ref_objectid(eb, dref),
993 (unsigned long long)btrfs_extent_data_ref_offset(eb, dref),
994 btrfs_extent_data_ref_count(eb, dref));
997 static void print_shared_data_ref(struct extent_buffer *eb, int slot)
999 struct btrfs_shared_data_ref *sref;
1001 sref = btrfs_item_ptr(eb, slot, struct btrfs_shared_data_ref);
1002 printf("\t\tshared data backref count %u\n",
1003 btrfs_shared_data_ref_count(eb, sref));
1006 static void print_free_space_info(struct extent_buffer *eb, int slot)
1008 struct btrfs_free_space_info *free_info;
1010 free_info = btrfs_item_ptr(eb, slot, struct btrfs_free_space_info);
1011 printf("\t\tfree space info extent count %u flags %u\n",
1012 (unsigned)btrfs_free_space_extent_count(eb, free_info),
1013 (unsigned)btrfs_free_space_flags(eb, free_info));
1016 static void print_dev_extent(struct extent_buffer *eb, int slot)
1018 struct btrfs_dev_extent *dev_extent;
1019 u8 uuid[BTRFS_UUID_SIZE];
1020 char uuid_str[BTRFS_UUID_UNPARSED_SIZE];
1022 dev_extent = btrfs_item_ptr(eb, slot, struct btrfs_dev_extent);
1023 read_extent_buffer(eb, uuid,
1024 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(dev_extent),
1025 BTRFS_UUID_SIZE);
1026 uuid_unparse(uuid, uuid_str);
1027 printf("\t\tdev extent chunk_tree %llu\n"
1028 "\t\tchunk_objectid %llu chunk_offset %llu "
1029 "length %llu\n"
1030 "\t\tchunk_tree_uuid %s\n",
1031 (unsigned long long)btrfs_dev_extent_chunk_tree(eb, dev_extent),
1032 (unsigned long long)btrfs_dev_extent_chunk_objectid(eb, dev_extent),
1033 (unsigned long long)btrfs_dev_extent_chunk_offset(eb, dev_extent),
1034 (unsigned long long)btrfs_dev_extent_length(eb, dev_extent),
1035 uuid_str);
1038 static void print_qgroup_status(struct extent_buffer *eb, int slot)
1040 struct btrfs_qgroup_status_item *qg_status;
1041 char flags_str[256];
1043 qg_status = btrfs_item_ptr(eb, slot, struct btrfs_qgroup_status_item);
1044 memset(flags_str, 0, sizeof(flags_str));
1045 qgroup_flags_to_str(btrfs_qgroup_status_flags(eb, qg_status),
1046 flags_str);
1047 printf("\t\tversion %llu generation %llu flags %s scan %lld\n",
1048 (unsigned long long)btrfs_qgroup_status_version(eb, qg_status),
1049 (unsigned long long)btrfs_qgroup_status_generation(eb, qg_status),
1050 flags_str,
1051 (unsigned long long)btrfs_qgroup_status_rescan(eb, qg_status));
1054 static void print_qgroup_info(struct extent_buffer *eb, int slot)
1056 struct btrfs_qgroup_info_item *qg_info;
1058 qg_info = btrfs_item_ptr(eb, slot, struct btrfs_qgroup_info_item);
1059 printf("\t\tgeneration %llu\n"
1060 "\t\treferenced %llu referenced_compressed %llu\n"
1061 "\t\texclusive %llu exclusive_compressed %llu\n",
1062 (unsigned long long)btrfs_qgroup_info_generation(eb, qg_info),
1063 (unsigned long long)btrfs_qgroup_info_referenced(eb, qg_info),
1064 (unsigned long long)btrfs_qgroup_info_referenced_compressed(eb,
1065 qg_info),
1066 (unsigned long long)btrfs_qgroup_info_exclusive(eb, qg_info),
1067 (unsigned long long)btrfs_qgroup_info_exclusive_compressed(eb,
1068 qg_info));
1071 static void print_qgroup_limit(struct extent_buffer *eb, int slot)
1073 struct btrfs_qgroup_limit_item *qg_limit;
1075 qg_limit = btrfs_item_ptr(eb, slot, struct btrfs_qgroup_limit_item);
1076 printf("\t\tflags %llx\n"
1077 "\t\tmax_referenced %lld max_exclusive %lld\n"
1078 "\t\trsv_referenced %lld rsv_exclusive %lld\n",
1079 (unsigned long long)btrfs_qgroup_limit_flags(eb, qg_limit),
1080 (long long)btrfs_qgroup_limit_max_referenced(eb, qg_limit),
1081 (long long)btrfs_qgroup_limit_max_exclusive(eb, qg_limit),
1082 (long long)btrfs_qgroup_limit_rsv_referenced(eb, qg_limit),
1083 (long long)btrfs_qgroup_limit_rsv_exclusive(eb, qg_limit));
1086 static void print_persistent_item(struct extent_buffer *eb, void *ptr,
1087 u32 item_size, u64 objectid, u64 offset)
1089 printf("\t\tpersistent item objectid ");
1090 print_objectid(stdout, objectid, BTRFS_PERSISTENT_ITEM_KEY);
1091 printf(" offset %llu\n", (unsigned long long)offset);
1092 switch (objectid) {
1093 case BTRFS_DEV_STATS_OBJECTID:
1094 print_dev_stats(eb, ptr, item_size);
1095 break;
1096 default:
1097 printf("\t\tunknown persistent item objectid %llu\n", objectid);
1101 static void print_temporary_item(struct extent_buffer *eb, void *ptr,
1102 u64 objectid, u64 offset)
1104 printf("\t\ttemporary item objectid ");
1105 print_objectid(stdout, objectid, BTRFS_TEMPORARY_ITEM_KEY);
1106 printf(" offset %llu\n", (unsigned long long)offset);
1107 switch (objectid) {
1108 case BTRFS_BALANCE_OBJECTID:
1109 print_balance_item(eb, ptr);
1110 break;
1111 default:
1112 printf("\t\tunknown temporary item objectid %llu\n", objectid);
1116 static void print_extent_csum(struct extent_buffer *eb,
1117 struct btrfs_fs_info *fs_info, u32 item_size, u64 start)
1119 u32 size;
1121 size = (item_size / btrfs_super_csum_size(fs_info->super_copy)) *
1122 fs_info->sectorsize;
1123 printf("\t\trange start %llu end %llu length %u\n",
1124 (unsigned long long)start,
1125 (unsigned long long)start + size, size);
1128 /* Caller must ensure sizeof(*ret) >= 14 "WRITTEN|RELOC" */
1129 static void header_flags_to_str(u64 flags, char *ret)
1131 int empty = 1;
1133 if (flags & BTRFS_HEADER_FLAG_WRITTEN) {
1134 empty = 0;
1135 strcpy(ret, "WRITTEN");
1137 if (flags & BTRFS_HEADER_FLAG_RELOC) {
1138 if (!empty)
1139 strcat(ret, "|");
1140 strcat(ret, "RELOC");
1144 void btrfs_print_leaf(struct btrfs_root *root, struct extent_buffer *eb)
1146 struct btrfs_item *item;
1147 struct btrfs_disk_key disk_key;
1148 char flags_str[128];
1149 u32 i;
1150 u32 nr;
1151 u64 flags;
1152 u8 backref_rev;
1154 flags = btrfs_header_flags(eb) & ~BTRFS_BACKREF_REV_MASK;
1155 backref_rev = btrfs_header_flags(eb) >> BTRFS_BACKREF_REV_SHIFT;
1156 header_flags_to_str(flags, flags_str);
1157 nr = btrfs_header_nritems(eb);
1159 printf("leaf %llu items %d free space %d generation %llu owner %llu\n",
1160 (unsigned long long)btrfs_header_bytenr(eb), nr,
1161 btrfs_leaf_free_space(root, eb),
1162 (unsigned long long)btrfs_header_generation(eb),
1163 (unsigned long long)btrfs_header_owner(eb));
1164 printf("leaf %llu flags 0x%llx(%s) backref revision %d\n",
1165 btrfs_header_bytenr(eb), flags, flags_str, backref_rev);
1166 print_uuids(eb);
1167 fflush(stdout);
1169 for (i = 0; i < nr; i++) {
1170 u32 item_size;
1171 void *ptr;
1172 u64 objectid;
1173 u32 type;
1174 u64 offset;
1176 item = btrfs_item_nr(i);
1177 item_size = btrfs_item_size(eb, item);
1178 /* Untyped extraction of slot from btrfs_item_ptr */
1179 ptr = btrfs_item_ptr(eb, i, void*);
1181 btrfs_item_key(eb, &disk_key, i);
1182 objectid = btrfs_disk_key_objectid(&disk_key);
1183 type = btrfs_disk_key_type(&disk_key);
1184 offset = btrfs_disk_key_offset(&disk_key);
1186 printf("\titem %d ", i);
1187 btrfs_print_key(&disk_key);
1188 printf(" itemoff %d itemsize %d\n",
1189 btrfs_item_offset(eb, item),
1190 btrfs_item_size(eb, item));
1192 if (type == 0 && objectid == BTRFS_FREE_SPACE_OBJECTID)
1193 print_free_space_header(eb, i);
1195 switch (type) {
1196 case BTRFS_INODE_ITEM_KEY:
1197 print_inode_item(eb, ptr);
1198 break;
1199 case BTRFS_INODE_REF_KEY:
1200 print_inode_ref_item(eb, item_size, ptr);
1201 break;
1202 case BTRFS_INODE_EXTREF_KEY:
1203 print_inode_extref_item(eb, item_size, ptr);
1204 break;
1205 case BTRFS_DIR_ITEM_KEY:
1206 case BTRFS_DIR_INDEX_KEY:
1207 case BTRFS_XATTR_ITEM_KEY:
1208 print_dir_item(eb, item_size, ptr);
1209 break;
1210 case BTRFS_DIR_LOG_INDEX_KEY:
1211 case BTRFS_DIR_LOG_ITEM_KEY: {
1212 struct btrfs_dir_log_item *dlog;
1214 dlog = btrfs_item_ptr(eb, i, struct btrfs_dir_log_item);
1215 printf("\t\tdir log end %Lu\n",
1216 (unsigned long long)btrfs_dir_log_end(eb, dlog));
1217 break;
1219 case BTRFS_ORPHAN_ITEM_KEY:
1220 printf("\t\torphan item\n");
1221 break;
1222 case BTRFS_ROOT_ITEM_KEY:
1223 print_root_item(eb, i);
1224 break;
1225 case BTRFS_ROOT_REF_KEY:
1226 print_root_ref(eb, i, "ref");
1227 break;
1228 case BTRFS_ROOT_BACKREF_KEY:
1229 print_root_ref(eb, i, "backref");
1230 break;
1231 case BTRFS_EXTENT_ITEM_KEY:
1232 print_extent_item(eb, i, 0);
1233 break;
1234 case BTRFS_METADATA_ITEM_KEY:
1235 print_extent_item(eb, i, 1);
1236 break;
1237 case BTRFS_TREE_BLOCK_REF_KEY:
1238 printf("\t\ttree block backref\n");
1239 break;
1240 case BTRFS_SHARED_BLOCK_REF_KEY:
1241 printf("\t\tshared block backref\n");
1242 break;
1243 case BTRFS_EXTENT_DATA_REF_KEY:
1244 print_extent_data_ref(eb, i);
1245 break;
1246 case BTRFS_SHARED_DATA_REF_KEY:
1247 print_shared_data_ref(eb, i);
1248 break;
1249 case BTRFS_EXTENT_REF_V0_KEY:
1250 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1251 print_extent_ref_v0(eb, i);
1252 #else
1253 BUG();
1254 #endif
1255 break;
1256 case BTRFS_CSUM_ITEM_KEY:
1257 printf("\t\tcsum item\n");
1258 break;
1259 case BTRFS_EXTENT_CSUM_KEY:
1260 print_extent_csum(eb, root->fs_info, item_size,
1261 offset);
1262 break;
1263 case BTRFS_EXTENT_DATA_KEY:
1264 print_file_extent_item(eb, item, i, ptr);
1265 break;
1266 case BTRFS_BLOCK_GROUP_ITEM_KEY:
1267 print_block_group_item(eb, ptr);
1268 break;
1269 case BTRFS_FREE_SPACE_INFO_KEY:
1270 print_free_space_info(eb, i);
1271 break;
1272 case BTRFS_FREE_SPACE_EXTENT_KEY:
1273 printf("\t\tfree space extent\n");
1274 break;
1275 case BTRFS_FREE_SPACE_BITMAP_KEY:
1276 printf("\t\tfree space bitmap\n");
1277 break;
1278 case BTRFS_CHUNK_ITEM_KEY:
1279 print_chunk_item(eb, ptr);
1280 break;
1281 case BTRFS_DEV_ITEM_KEY:
1282 print_dev_item(eb, ptr);
1283 break;
1284 case BTRFS_DEV_EXTENT_KEY:
1285 print_dev_extent(eb, i);
1286 break;
1287 case BTRFS_QGROUP_STATUS_KEY:
1288 print_qgroup_status(eb, i);
1289 break;
1290 case BTRFS_QGROUP_RELATION_KEY:
1291 break;
1292 case BTRFS_QGROUP_INFO_KEY:
1293 print_qgroup_info(eb, i);
1294 break;
1295 case BTRFS_QGROUP_LIMIT_KEY:
1296 print_qgroup_limit(eb, i);
1297 break;
1298 case BTRFS_UUID_KEY_SUBVOL:
1299 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
1300 print_uuid_item(eb, btrfs_item_ptr_offset(eb, i),
1301 btrfs_item_size_nr(eb, i));
1302 break;
1303 case BTRFS_STRING_ITEM_KEY: {
1304 const char *str = eb->data + btrfs_item_ptr_offset(eb, i);
1306 printf("\t\titem data %.*s\n", item_size, str);
1307 break;
1309 case BTRFS_PERSISTENT_ITEM_KEY:
1310 print_persistent_item(eb, ptr, item_size, objectid,
1311 offset);
1312 break;
1313 case BTRFS_TEMPORARY_ITEM_KEY:
1314 print_temporary_item(eb, ptr, objectid, offset);
1315 break;
1317 fflush(stdout);
1321 void btrfs_print_tree(struct btrfs_root *root, struct extent_buffer *eb, int follow)
1323 u32 i;
1324 u32 nr;
1325 struct btrfs_disk_key disk_key;
1326 struct btrfs_key key;
1327 struct extent_buffer *next;
1329 if (!eb)
1330 return;
1331 nr = btrfs_header_nritems(eb);
1332 if (btrfs_is_leaf(eb)) {
1333 btrfs_print_leaf(root, eb);
1334 return;
1336 printf("node %llu level %d items %d free %u generation %llu owner %llu\n",
1337 (unsigned long long)eb->start,
1338 btrfs_header_level(eb), nr,
1339 (u32)BTRFS_NODEPTRS_PER_BLOCK(root) - nr,
1340 (unsigned long long)btrfs_header_generation(eb),
1341 (unsigned long long)btrfs_header_owner(eb));
1342 print_uuids(eb);
1343 fflush(stdout);
1344 for (i = 0; i < nr; i++) {
1345 u64 blocknr = btrfs_node_blockptr(eb, i);
1346 btrfs_node_key(eb, &disk_key, i);
1347 btrfs_disk_key_to_cpu(&key, &disk_key);
1348 printf("\t");
1349 btrfs_print_key(&disk_key);
1350 printf(" block %llu (%llu) gen %llu\n",
1351 (unsigned long long)blocknr,
1352 (unsigned long long)blocknr / root->fs_info->nodesize,
1353 (unsigned long long)btrfs_node_ptr_generation(eb, i));
1354 fflush(stdout);
1356 if (!follow)
1357 return;
1359 for (i = 0; i < nr; i++) {
1360 next = read_tree_block(root->fs_info,
1361 btrfs_node_blockptr(eb, i),
1362 btrfs_node_ptr_generation(eb, i));
1363 if (!extent_buffer_uptodate(next)) {
1364 fprintf(stderr, "failed to read %llu in tree %llu\n",
1365 (unsigned long long)btrfs_node_blockptr(eb, i),
1366 (unsigned long long)btrfs_header_owner(eb));
1367 continue;
1369 if (btrfs_is_leaf(next) && btrfs_header_level(eb) != 1) {
1370 warning(
1371 "eb corrupted: item %d eb level %d next level %d, skipping the rest",
1372 i, btrfs_header_level(next),
1373 btrfs_header_level(eb));
1374 goto out;
1376 if (btrfs_header_level(next) != btrfs_header_level(eb) - 1) {
1377 warning(
1378 "eb corrupted: item %d eb level %d next level %d, skipping the rest",
1379 i, btrfs_header_level(next),
1380 btrfs_header_level(eb));
1381 goto out;
1383 btrfs_print_tree(root, next, 1);
1384 free_extent_buffer(next);
1387 return;
1389 out:
1390 free_extent_buffer(next);