Btrfs progs v4.17.1
[btrfs-progs-unstable/devel.git] / print-tree.c
bloba09ecfbb28f08cf48d4f3452d4f999db32bcd209
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 %llu compression %hhu (%s)\n",
361 btrfs_file_extent_inline_item_len(eb, item),
362 btrfs_file_extent_ram_bytes(eb, 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 ");
474 print_objectid(stdout, offset, 0);
475 printf("\n");
476 break;
477 case BTRFS_SHARED_BLOCK_REF_KEY:
478 printf("\t\tshared block backref parent %llu\n",
479 (unsigned long long)offset);
480 break;
481 case BTRFS_EXTENT_DATA_REF_KEY:
482 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
483 printf("\t\textent data backref root ");
484 print_objectid(stdout,
485 (unsigned long long)btrfs_extent_data_ref_root(eb, dref), 0);
486 printf(" objectid %llu offset %lld count %u\n",
487 (unsigned long long)btrfs_extent_data_ref_objectid(eb, dref),
488 btrfs_extent_data_ref_offset(eb, dref),
489 btrfs_extent_data_ref_count(eb, dref));
490 break;
491 case BTRFS_SHARED_DATA_REF_KEY:
492 sref = (struct btrfs_shared_data_ref *)(iref + 1);
493 printf("\t\tshared data backref parent %llu count %u\n",
494 (unsigned long long)offset,
495 btrfs_shared_data_ref_count(eb, sref));
496 break;
497 default:
498 return;
500 ptr += btrfs_extent_inline_ref_size(type);
502 WARN_ON(ptr > end);
505 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
506 static void print_extent_ref_v0(struct extent_buffer *eb, int slot)
508 struct btrfs_extent_ref_v0 *ref0;
510 ref0 = btrfs_item_ptr(eb, slot, struct btrfs_extent_ref_v0);
511 printf("\t\textent back ref root %llu gen %llu "
512 "owner %llu num_refs %lu\n",
513 (unsigned long long)btrfs_ref_root_v0(eb, ref0),
514 (unsigned long long)btrfs_ref_generation_v0(eb, ref0),
515 (unsigned long long)btrfs_ref_objectid_v0(eb, ref0),
516 (unsigned long)btrfs_ref_count_v0(eb, ref0));
518 #endif
520 static void print_root_ref(struct extent_buffer *leaf, int slot, const char *tag)
522 struct btrfs_root_ref *ref;
523 char namebuf[BTRFS_NAME_LEN];
524 int namelen;
526 ref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
527 namelen = btrfs_root_ref_name_len(leaf, ref);
528 read_extent_buffer(leaf, namebuf, (unsigned long)(ref + 1), namelen);
529 printf("\t\troot %s key dirid %llu sequence %llu name %.*s\n", tag,
530 (unsigned long long)btrfs_root_ref_dirid(leaf, ref),
531 (unsigned long long)btrfs_root_ref_sequence(leaf, ref),
532 namelen, namebuf);
535 static int empty_uuid(const u8 *uuid)
537 int i;
539 for (i = 0; i < BTRFS_UUID_SIZE; i++)
540 if (uuid[i])
541 return 0;
542 return 1;
546 * Caller must ensure sizeof(*ret) >= 7 "RDONLY"
548 static void root_flags_to_str(u64 flags, char *ret)
550 if (flags & BTRFS_ROOT_SUBVOL_RDONLY)
551 strcat(ret, "RDONLY");
552 else
553 strcat(ret, "none");
556 static void print_timespec(struct extent_buffer *eb,
557 struct btrfs_timespec *timespec, const char *prefix,
558 const char *suffix)
560 struct tm tm;
561 u64 tmp_u64;
562 u32 tmp_u32;
563 time_t tmp_time;
564 char timestamp[256];
566 tmp_u64 = btrfs_timespec_sec(eb, timespec);
567 tmp_u32 = btrfs_timespec_nsec(eb, timespec);
568 tmp_time = tmp_u64;
569 localtime_r(&tmp_time, &tm);
570 strftime(timestamp, sizeof(timestamp),
571 "%Y-%m-%d %H:%M:%S", &tm);
572 printf("%s%llu.%u (%s)%s", prefix, (unsigned long long)tmp_u64, tmp_u32,
573 timestamp, suffix);
576 static void print_root_item(struct extent_buffer *leaf, int slot)
578 struct btrfs_root_item *ri;
579 struct btrfs_root_item root_item;
580 int len;
581 char uuid_str[BTRFS_UUID_UNPARSED_SIZE];
582 char flags_str[32] = {0};
583 struct btrfs_key drop_key;
585 ri = btrfs_item_ptr(leaf, slot, struct btrfs_root_item);
586 len = btrfs_item_size_nr(leaf, slot);
588 memset(&root_item, 0, sizeof(root_item));
589 read_extent_buffer(leaf, &root_item, (unsigned long)ri, len);
590 root_flags_to_str(btrfs_root_flags(&root_item), flags_str);
592 printf("\t\tgeneration %llu root_dirid %llu bytenr %llu level %hhu refs %u\n",
593 (unsigned long long)btrfs_root_generation(&root_item),
594 (unsigned long long)btrfs_root_dirid(&root_item),
595 (unsigned long long)btrfs_root_bytenr(&root_item),
596 btrfs_root_level(&root_item),
597 btrfs_root_refs(&root_item));
598 printf("\t\tlastsnap %llu byte_limit %llu bytes_used %llu flags 0x%llx(%s)\n",
599 (unsigned long long)btrfs_root_last_snapshot(&root_item),
600 (unsigned long long)btrfs_root_limit(&root_item),
601 (unsigned long long)btrfs_root_used(&root_item),
602 (unsigned long long)btrfs_root_flags(&root_item),
603 flags_str);
605 if (root_item.generation == root_item.generation_v2) {
606 uuid_unparse(root_item.uuid, uuid_str);
607 printf("\t\tuuid %s\n", uuid_str);
608 if (!empty_uuid(root_item.parent_uuid)) {
609 uuid_unparse(root_item.parent_uuid, uuid_str);
610 printf("\t\tparent_uuid %s\n", uuid_str);
612 if (!empty_uuid(root_item.received_uuid)) {
613 uuid_unparse(root_item.received_uuid, uuid_str);
614 printf("\t\treceived_uuid %s\n", uuid_str);
616 if (root_item.ctransid) {
617 printf("\t\tctransid %llu otransid %llu stransid %llu rtransid %llu\n",
618 btrfs_root_ctransid(&root_item),
619 btrfs_root_otransid(&root_item),
620 btrfs_root_stransid(&root_item),
621 btrfs_root_rtransid(&root_item));
623 if (btrfs_timespec_sec(leaf, btrfs_root_ctime(ri)))
624 print_timespec(leaf, btrfs_root_ctime(ri),
625 "\t\tctime ", "\n");
626 if (btrfs_timespec_sec(leaf, btrfs_root_otime(ri)))
627 print_timespec(leaf, btrfs_root_otime(ri),
628 "\t\totime ", "\n");
629 if (btrfs_timespec_sec(leaf, btrfs_root_stime(ri)))
630 print_timespec(leaf, btrfs_root_stime(ri),
631 "\t\tstime ", "\n");
632 if (btrfs_timespec_sec(leaf, btrfs_root_rtime(ri)))
633 print_timespec(leaf, btrfs_root_rtime(ri),
634 "\t\trtime ", "\n");
637 btrfs_disk_key_to_cpu(&drop_key, &root_item.drop_progress);
638 printf("\t\tdrop ");
639 btrfs_print_key(&root_item.drop_progress);
640 printf(" level %hhu\n", root_item.drop_level);
643 static void print_free_space_header(struct extent_buffer *leaf, int slot)
645 struct btrfs_free_space_header *header;
646 struct btrfs_disk_key location;
648 header = btrfs_item_ptr(leaf, slot, struct btrfs_free_space_header);
649 btrfs_free_space_key(leaf, header, &location);
650 printf("\t\tlocation ");
651 btrfs_print_key(&location);
652 printf("\n");
653 printf("\t\tcache generation %llu entries %llu bitmaps %llu\n",
654 (unsigned long long)btrfs_free_space_generation(leaf, header),
655 (unsigned long long)btrfs_free_space_entries(leaf, header),
656 (unsigned long long)btrfs_free_space_bitmaps(leaf, header));
659 void print_key_type(FILE *stream, u64 objectid, u8 type)
661 static const char* key_to_str[256] = {
662 [BTRFS_INODE_ITEM_KEY] = "INODE_ITEM",
663 [BTRFS_INODE_REF_KEY] = "INODE_REF",
664 [BTRFS_INODE_EXTREF_KEY] = "INODE_EXTREF",
665 [BTRFS_DIR_ITEM_KEY] = "DIR_ITEM",
666 [BTRFS_DIR_INDEX_KEY] = "DIR_INDEX",
667 [BTRFS_DIR_LOG_ITEM_KEY] = "DIR_LOG_ITEM",
668 [BTRFS_DIR_LOG_INDEX_KEY] = "DIR_LOG_INDEX",
669 [BTRFS_XATTR_ITEM_KEY] = "XATTR_ITEM",
670 [BTRFS_ORPHAN_ITEM_KEY] = "ORPHAN_ITEM",
671 [BTRFS_ROOT_ITEM_KEY] = "ROOT_ITEM",
672 [BTRFS_ROOT_REF_KEY] = "ROOT_REF",
673 [BTRFS_ROOT_BACKREF_KEY] = "ROOT_BACKREF",
674 [BTRFS_EXTENT_ITEM_KEY] = "EXTENT_ITEM",
675 [BTRFS_METADATA_ITEM_KEY] = "METADATA_ITEM",
676 [BTRFS_TREE_BLOCK_REF_KEY] = "TREE_BLOCK_REF",
677 [BTRFS_SHARED_BLOCK_REF_KEY] = "SHARED_BLOCK_REF",
678 [BTRFS_EXTENT_DATA_REF_KEY] = "EXTENT_DATA_REF",
679 [BTRFS_SHARED_DATA_REF_KEY] = "SHARED_DATA_REF",
680 [BTRFS_EXTENT_REF_V0_KEY] = "EXTENT_REF_V0",
681 [BTRFS_CSUM_ITEM_KEY] = "CSUM_ITEM",
682 [BTRFS_EXTENT_CSUM_KEY] = "EXTENT_CSUM",
683 [BTRFS_EXTENT_DATA_KEY] = "EXTENT_DATA",
684 [BTRFS_BLOCK_GROUP_ITEM_KEY] = "BLOCK_GROUP_ITEM",
685 [BTRFS_FREE_SPACE_INFO_KEY] = "FREE_SPACE_INFO",
686 [BTRFS_FREE_SPACE_EXTENT_KEY] = "FREE_SPACE_EXTENT",
687 [BTRFS_FREE_SPACE_BITMAP_KEY] = "FREE_SPACE_BITMAP",
688 [BTRFS_CHUNK_ITEM_KEY] = "CHUNK_ITEM",
689 [BTRFS_DEV_ITEM_KEY] = "DEV_ITEM",
690 [BTRFS_DEV_EXTENT_KEY] = "DEV_EXTENT",
691 [BTRFS_TEMPORARY_ITEM_KEY] = "TEMPORARY_ITEM",
692 [BTRFS_DEV_REPLACE_KEY] = "DEV_REPLACE",
693 [BTRFS_STRING_ITEM_KEY] = "STRING_ITEM",
694 [BTRFS_QGROUP_STATUS_KEY] = "QGROUP_STATUS",
695 [BTRFS_QGROUP_RELATION_KEY] = "QGROUP_RELATION",
696 [BTRFS_QGROUP_INFO_KEY] = "QGROUP_INFO",
697 [BTRFS_QGROUP_LIMIT_KEY] = "QGROUP_LIMIT",
698 [BTRFS_PERSISTENT_ITEM_KEY] = "PERSISTENT_ITEM",
699 [BTRFS_UUID_KEY_SUBVOL] = "UUID_KEY_SUBVOL",
700 [BTRFS_UUID_KEY_RECEIVED_SUBVOL] = "UUID_KEY_RECEIVED_SUBVOL",
703 if (type == 0 && objectid == BTRFS_FREE_SPACE_OBJECTID) {
704 fprintf(stream, "UNTYPED");
705 return;
709 if (key_to_str[type])
710 fputs(key_to_str[type], stream);
711 else
712 fprintf(stream, "UNKNOWN.%d", type);
715 void print_objectid(FILE *stream, u64 objectid, u8 type)
717 switch (type) {
718 case BTRFS_DEV_EXTENT_KEY:
719 /* device id */
720 fprintf(stream, "%llu", (unsigned long long)objectid);
721 return;
722 case BTRFS_QGROUP_RELATION_KEY:
723 fprintf(stream, "%llu/%llu", btrfs_qgroup_level(objectid),
724 btrfs_qgroup_subvid(objectid));
725 return;
726 case BTRFS_UUID_KEY_SUBVOL:
727 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
728 fprintf(stream, "0x%016llx", (unsigned long long)objectid);
729 return;
732 switch (objectid) {
733 case BTRFS_ROOT_TREE_OBJECTID:
734 if (type == BTRFS_DEV_ITEM_KEY)
735 fprintf(stream, "DEV_ITEMS");
736 else
737 fprintf(stream, "ROOT_TREE");
738 break;
739 case BTRFS_EXTENT_TREE_OBJECTID:
740 fprintf(stream, "EXTENT_TREE");
741 break;
742 case BTRFS_CHUNK_TREE_OBJECTID:
743 fprintf(stream, "CHUNK_TREE");
744 break;
745 case BTRFS_DEV_TREE_OBJECTID:
746 fprintf(stream, "DEV_TREE");
747 break;
748 case BTRFS_FS_TREE_OBJECTID:
749 fprintf(stream, "FS_TREE");
750 break;
751 case BTRFS_ROOT_TREE_DIR_OBJECTID:
752 fprintf(stream, "ROOT_TREE_DIR");
753 break;
754 case BTRFS_CSUM_TREE_OBJECTID:
755 fprintf(stream, "CSUM_TREE");
756 break;
757 case BTRFS_BALANCE_OBJECTID:
758 fprintf(stream, "BALANCE");
759 break;
760 case BTRFS_ORPHAN_OBJECTID:
761 fprintf(stream, "ORPHAN");
762 break;
763 case BTRFS_TREE_LOG_OBJECTID:
764 fprintf(stream, "TREE_LOG");
765 break;
766 case BTRFS_TREE_LOG_FIXUP_OBJECTID:
767 fprintf(stream, "LOG_FIXUP");
768 break;
769 case BTRFS_TREE_RELOC_OBJECTID:
770 fprintf(stream, "TREE_RELOC");
771 break;
772 case BTRFS_DATA_RELOC_TREE_OBJECTID:
773 fprintf(stream, "DATA_RELOC_TREE");
774 break;
775 case BTRFS_EXTENT_CSUM_OBJECTID:
776 fprintf(stream, "EXTENT_CSUM");
777 break;
778 case BTRFS_FREE_SPACE_OBJECTID:
779 fprintf(stream, "FREE_SPACE");
780 break;
781 case BTRFS_FREE_INO_OBJECTID:
782 fprintf(stream, "FREE_INO");
783 break;
784 case BTRFS_QUOTA_TREE_OBJECTID:
785 fprintf(stream, "QUOTA_TREE");
786 break;
787 case BTRFS_UUID_TREE_OBJECTID:
788 fprintf(stream, "UUID_TREE");
789 break;
790 case BTRFS_FREE_SPACE_TREE_OBJECTID:
791 fprintf(stream, "FREE_SPACE_TREE");
792 break;
793 case BTRFS_MULTIPLE_OBJECTIDS:
794 fprintf(stream, "MULTIPLE");
795 break;
796 case (u64)-1:
797 fprintf(stream, "-1");
798 break;
799 case BTRFS_FIRST_CHUNK_TREE_OBJECTID:
800 if (type == BTRFS_CHUNK_ITEM_KEY) {
801 fprintf(stream, "FIRST_CHUNK_TREE");
802 break;
804 /* fall-thru */
805 default:
806 fprintf(stream, "%llu", (unsigned long long)objectid);
810 void btrfs_print_key(struct btrfs_disk_key *disk_key)
812 u64 objectid = btrfs_disk_key_objectid(disk_key);
813 u8 type = btrfs_disk_key_type(disk_key);
814 u64 offset = btrfs_disk_key_offset(disk_key);
816 printf("key (");
817 print_objectid(stdout, objectid, type);
818 printf(" ");
819 print_key_type(stdout, objectid, type);
820 switch (type) {
821 case BTRFS_QGROUP_RELATION_KEY:
822 case BTRFS_QGROUP_INFO_KEY:
823 case BTRFS_QGROUP_LIMIT_KEY:
824 printf(" %llu/%llu)", btrfs_qgroup_level(offset),
825 btrfs_qgroup_subvid(offset));
826 break;
827 case BTRFS_UUID_KEY_SUBVOL:
828 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
829 printf(" 0x%016llx)", (unsigned long long)offset);
830 break;
833 * Key offsets of ROOT_ITEM point to tree root, print them in human
834 * readable format. Especially useful for trees like data/tree reloc
835 * tree, whose tree id can be negative.
837 case BTRFS_ROOT_ITEM_KEY:
838 printf(" ");
840 * Normally offset of ROOT_ITEM should present the generation
841 * of creation time of the root.
842 * However if this is reloc tree, offset is the subvolume
843 * id of its source. Here we do extra check on this.
845 if (objectid == BTRFS_TREE_RELOC_OBJECTID)
846 print_objectid(stdout, offset, type);
847 else
848 printf("%lld", offset);
849 printf(")");
850 break;
851 default:
852 if (offset == (u64)-1)
853 printf(" -1)");
854 else
855 printf(" %llu)", (unsigned long long)offset);
856 break;
860 static void print_uuid_item(struct extent_buffer *l, unsigned long offset,
861 u32 item_size)
863 if (item_size & (sizeof(u64) - 1)) {
864 printf("btrfs: uuid item with illegal size %lu!\n",
865 (unsigned long)item_size);
866 return;
868 while (item_size) {
869 __le64 subvol_id;
871 read_extent_buffer(l, &subvol_id, offset, sizeof(u64));
872 printf("\t\tsubvol_id %llu\n",
873 (unsigned long long)le64_to_cpu(subvol_id));
874 item_size -= sizeof(u64);
875 offset += sizeof(u64);
879 /* Btrfs inode flag stringification helper */
880 #define STRCAT_ONE_INODE_FLAG(flags, name, empty, dst) ({ \
881 if (flags & BTRFS_INODE_##name) { \
882 if (!empty) \
883 strcat(dst, "|"); \
884 strcat(dst, #name); \
885 empty = 0; \
890 * Caller should ensure sizeof(*ret) >= 102: all charactors plus '|' of
891 * BTRFS_INODE_* flags
893 static void inode_flags_to_str(u64 flags, char *ret)
895 int empty = 1;
897 STRCAT_ONE_INODE_FLAG(flags, NODATASUM, empty, ret);
898 STRCAT_ONE_INODE_FLAG(flags, NODATACOW, empty, ret);
899 STRCAT_ONE_INODE_FLAG(flags, READONLY, empty, ret);
900 STRCAT_ONE_INODE_FLAG(flags, NOCOMPRESS, empty, ret);
901 STRCAT_ONE_INODE_FLAG(flags, PREALLOC, empty, ret);
902 STRCAT_ONE_INODE_FLAG(flags, SYNC, empty, ret);
903 STRCAT_ONE_INODE_FLAG(flags, IMMUTABLE, empty, ret);
904 STRCAT_ONE_INODE_FLAG(flags, APPEND, empty, ret);
905 STRCAT_ONE_INODE_FLAG(flags, NODUMP, empty, ret);
906 STRCAT_ONE_INODE_FLAG(flags, NOATIME, empty, ret);
907 STRCAT_ONE_INODE_FLAG(flags, DIRSYNC, empty, ret);
908 STRCAT_ONE_INODE_FLAG(flags, COMPRESS, empty, ret);
909 if (empty)
910 strcat(ret, "none");
913 static void print_inode_item(struct extent_buffer *eb,
914 struct btrfs_inode_item *ii)
916 char flags_str[256];
918 memset(flags_str, 0, sizeof(flags_str));
919 inode_flags_to_str(btrfs_inode_flags(eb, ii), flags_str);
920 printf("\t\tgeneration %llu transid %llu size %llu nbytes %llu\n"
921 "\t\tblock group %llu mode %o links %u uid %u gid %u rdev %llu\n"
922 "\t\tsequence %llu flags 0x%llx(%s)\n",
923 (unsigned long long)btrfs_inode_generation(eb, ii),
924 (unsigned long long)btrfs_inode_transid(eb, ii),
925 (unsigned long long)btrfs_inode_size(eb, ii),
926 (unsigned long long)btrfs_inode_nbytes(eb, ii),
927 (unsigned long long)btrfs_inode_block_group(eb,ii),
928 btrfs_inode_mode(eb, ii),
929 btrfs_inode_nlink(eb, ii),
930 btrfs_inode_uid(eb, ii),
931 btrfs_inode_gid(eb, ii),
932 (unsigned long long)btrfs_inode_rdev(eb,ii),
933 (unsigned long long)btrfs_inode_sequence(eb, ii),
934 (unsigned long long)btrfs_inode_flags(eb,ii),
935 flags_str);
936 print_timespec(eb, btrfs_inode_atime(ii), "\t\tatime ", "\n");
937 print_timespec(eb, btrfs_inode_ctime(ii), "\t\tctime ", "\n");
938 print_timespec(eb, btrfs_inode_mtime(ii), "\t\tmtime ", "\n");
939 print_timespec(eb, btrfs_inode_otime(ii), "\t\totime ", "\n");
942 static void print_disk_balance_args(struct btrfs_disk_balance_args *ba)
944 printf("\t\tprofiles %llu devid %llu target %llu flags %llu\n",
945 (unsigned long long)le64_to_cpu(ba->profiles),
946 (unsigned long long)le64_to_cpu(ba->devid),
947 (unsigned long long)le64_to_cpu(ba->target),
948 (unsigned long long)le64_to_cpu(ba->flags));
949 printf("\t\tusage_min %u usage_max %u pstart %llu pend %llu\n",
950 le32_to_cpu(ba->usage_min),
951 le32_to_cpu(ba->usage_max),
952 (unsigned long long)le64_to_cpu(ba->pstart),
953 (unsigned long long)le64_to_cpu(ba->pend));
954 printf("\t\tvstart %llu vend %llu limit_min %u limit_max %u\n",
955 (unsigned long long)le64_to_cpu(ba->vstart),
956 (unsigned long long)le64_to_cpu(ba->vend),
957 le32_to_cpu(ba->limit_min),
958 le32_to_cpu(ba->limit_max));
959 printf("\t\tstripes_min %u stripes_max %u\n",
960 le32_to_cpu(ba->stripes_min),
961 le32_to_cpu(ba->stripes_max));
964 static void print_balance_item(struct extent_buffer *eb,
965 struct btrfs_balance_item *bi)
967 printf("\t\tbalance status flags %llu\n",
968 btrfs_balance_item_flags(eb, bi));
970 printf("\t\tDATA\n");
971 print_disk_balance_args(btrfs_balance_item_data(eb, bi));
972 printf("\t\tMETADATA\n");
973 print_disk_balance_args(btrfs_balance_item_meta(eb, bi));
974 printf("\t\tSYSTEM\n");
975 print_disk_balance_args(btrfs_balance_item_sys(eb, bi));
978 static void print_dev_stats(struct extent_buffer *eb,
979 struct btrfs_dev_stats_item *stats, u32 size)
981 int i;
982 u32 known = BTRFS_DEV_STAT_VALUES_MAX * sizeof(__le64);
983 __le64 *values = btrfs_dev_stats_values(eb, stats);
985 printf("\t\tdevice stats\n");
986 printf("\t\twrite_errs %llu read_errs %llu flush_errs %llu corruption_errs %llu generation %llu\n",
987 (unsigned long long)le64_to_cpu(values[BTRFS_DEV_STAT_WRITE_ERRS]),
988 (unsigned long long)le64_to_cpu(values[BTRFS_DEV_STAT_READ_ERRS]),
989 (unsigned long long)le64_to_cpu(values[BTRFS_DEV_STAT_FLUSH_ERRS]),
990 (unsigned long long)le64_to_cpu(values[BTRFS_DEV_STAT_CORRUPTION_ERRS]),
991 (unsigned long long)le64_to_cpu(values[BTRFS_DEV_STAT_GENERATION_ERRS]));
993 if (known < size) {
994 printf("\t\tunknown stats item bytes %u", size - known);
995 for (i = BTRFS_DEV_STAT_VALUES_MAX; i * sizeof(__le64) < size; i++) {
996 printf("\t\tunknown item %u offset %zu value %llu\n",
997 i, i * sizeof(__le64),
998 (unsigned long long)le64_to_cpu(values[i]));
1003 static void print_block_group_item(struct extent_buffer *eb,
1004 struct btrfs_block_group_item *bgi)
1006 struct btrfs_block_group_item bg_item;
1007 char flags_str[256];
1009 read_extent_buffer(eb, &bg_item, (unsigned long)bgi, sizeof(bg_item));
1010 memset(flags_str, 0, sizeof(flags_str));
1011 bg_flags_to_str(btrfs_block_group_flags(&bg_item), flags_str);
1012 printf("\t\tblock group used %llu chunk_objectid %llu flags %s\n",
1013 (unsigned long long)btrfs_block_group_used(&bg_item),
1014 (unsigned long long)btrfs_block_group_chunk_objectid(&bg_item),
1015 flags_str);
1018 static void print_extent_data_ref(struct extent_buffer *eb, int slot)
1020 struct btrfs_extent_data_ref *dref;
1022 dref = btrfs_item_ptr(eb, slot, struct btrfs_extent_data_ref);
1023 printf("\t\textent data backref root ");
1024 print_objectid(stdout,
1025 (unsigned long long)btrfs_extent_data_ref_root(eb, dref), 0);
1026 printf(" objectid %llu offset %llu count %u\n",
1027 (unsigned long long)btrfs_extent_data_ref_objectid(eb, dref),
1028 (unsigned long long)btrfs_extent_data_ref_offset(eb, dref),
1029 btrfs_extent_data_ref_count(eb, dref));
1032 static void print_shared_data_ref(struct extent_buffer *eb, int slot)
1034 struct btrfs_shared_data_ref *sref;
1036 sref = btrfs_item_ptr(eb, slot, struct btrfs_shared_data_ref);
1037 printf("\t\tshared data backref count %u\n",
1038 btrfs_shared_data_ref_count(eb, sref));
1041 static void print_free_space_info(struct extent_buffer *eb, int slot)
1043 struct btrfs_free_space_info *free_info;
1045 free_info = btrfs_item_ptr(eb, slot, struct btrfs_free_space_info);
1046 printf("\t\tfree space info extent count %u flags %u\n",
1047 (unsigned)btrfs_free_space_extent_count(eb, free_info),
1048 (unsigned)btrfs_free_space_flags(eb, free_info));
1051 static void print_dev_extent(struct extent_buffer *eb, int slot)
1053 struct btrfs_dev_extent *dev_extent;
1054 u8 uuid[BTRFS_UUID_SIZE];
1055 char uuid_str[BTRFS_UUID_UNPARSED_SIZE];
1057 dev_extent = btrfs_item_ptr(eb, slot, struct btrfs_dev_extent);
1058 read_extent_buffer(eb, uuid,
1059 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(dev_extent),
1060 BTRFS_UUID_SIZE);
1061 uuid_unparse(uuid, uuid_str);
1062 printf("\t\tdev extent chunk_tree %llu\n"
1063 "\t\tchunk_objectid %llu chunk_offset %llu "
1064 "length %llu\n"
1065 "\t\tchunk_tree_uuid %s\n",
1066 (unsigned long long)btrfs_dev_extent_chunk_tree(eb, dev_extent),
1067 (unsigned long long)btrfs_dev_extent_chunk_objectid(eb, dev_extent),
1068 (unsigned long long)btrfs_dev_extent_chunk_offset(eb, dev_extent),
1069 (unsigned long long)btrfs_dev_extent_length(eb, dev_extent),
1070 uuid_str);
1073 static void print_qgroup_status(struct extent_buffer *eb, int slot)
1075 struct btrfs_qgroup_status_item *qg_status;
1076 char flags_str[256];
1078 qg_status = btrfs_item_ptr(eb, slot, struct btrfs_qgroup_status_item);
1079 memset(flags_str, 0, sizeof(flags_str));
1080 qgroup_flags_to_str(btrfs_qgroup_status_flags(eb, qg_status),
1081 flags_str);
1082 printf("\t\tversion %llu generation %llu flags %s scan %lld\n",
1083 (unsigned long long)btrfs_qgroup_status_version(eb, qg_status),
1084 (unsigned long long)btrfs_qgroup_status_generation(eb, qg_status),
1085 flags_str,
1086 (unsigned long long)btrfs_qgroup_status_rescan(eb, qg_status));
1089 static void print_qgroup_info(struct extent_buffer *eb, int slot)
1091 struct btrfs_qgroup_info_item *qg_info;
1093 qg_info = btrfs_item_ptr(eb, slot, struct btrfs_qgroup_info_item);
1094 printf("\t\tgeneration %llu\n"
1095 "\t\treferenced %llu referenced_compressed %llu\n"
1096 "\t\texclusive %llu exclusive_compressed %llu\n",
1097 (unsigned long long)btrfs_qgroup_info_generation(eb, qg_info),
1098 (unsigned long long)btrfs_qgroup_info_referenced(eb, qg_info),
1099 (unsigned long long)btrfs_qgroup_info_referenced_compressed(eb,
1100 qg_info),
1101 (unsigned long long)btrfs_qgroup_info_exclusive(eb, qg_info),
1102 (unsigned long long)btrfs_qgroup_info_exclusive_compressed(eb,
1103 qg_info));
1106 static void print_qgroup_limit(struct extent_buffer *eb, int slot)
1108 struct btrfs_qgroup_limit_item *qg_limit;
1110 qg_limit = btrfs_item_ptr(eb, slot, struct btrfs_qgroup_limit_item);
1111 printf("\t\tflags %llx\n"
1112 "\t\tmax_referenced %lld max_exclusive %lld\n"
1113 "\t\trsv_referenced %lld rsv_exclusive %lld\n",
1114 (unsigned long long)btrfs_qgroup_limit_flags(eb, qg_limit),
1115 (long long)btrfs_qgroup_limit_max_referenced(eb, qg_limit),
1116 (long long)btrfs_qgroup_limit_max_exclusive(eb, qg_limit),
1117 (long long)btrfs_qgroup_limit_rsv_referenced(eb, qg_limit),
1118 (long long)btrfs_qgroup_limit_rsv_exclusive(eb, qg_limit));
1121 static void print_persistent_item(struct extent_buffer *eb, void *ptr,
1122 u32 item_size, u64 objectid, u64 offset)
1124 printf("\t\tpersistent item objectid ");
1125 print_objectid(stdout, objectid, BTRFS_PERSISTENT_ITEM_KEY);
1126 printf(" offset %llu\n", (unsigned long long)offset);
1127 switch (objectid) {
1128 case BTRFS_DEV_STATS_OBJECTID:
1129 print_dev_stats(eb, ptr, item_size);
1130 break;
1131 default:
1132 printf("\t\tunknown persistent item objectid %llu\n", objectid);
1136 static void print_temporary_item(struct extent_buffer *eb, void *ptr,
1137 u64 objectid, u64 offset)
1139 printf("\t\ttemporary item objectid ");
1140 print_objectid(stdout, objectid, BTRFS_TEMPORARY_ITEM_KEY);
1141 printf(" offset %llu\n", (unsigned long long)offset);
1142 switch (objectid) {
1143 case BTRFS_BALANCE_OBJECTID:
1144 print_balance_item(eb, ptr);
1145 break;
1146 default:
1147 printf("\t\tunknown temporary item objectid %llu\n", objectid);
1151 static void print_extent_csum(struct extent_buffer *eb,
1152 struct btrfs_fs_info *fs_info, u32 item_size, u64 start)
1154 u32 size;
1157 * If we don't have fs_info, only output its start position as we
1158 * don't have sectorsize for the calculation
1160 if (!fs_info) {
1161 printf("\t\trange start %llu\n", (unsigned long long)start);
1162 return;
1164 size = (item_size / btrfs_super_csum_size(fs_info->super_copy)) *
1165 fs_info->sectorsize;
1166 printf("\t\trange start %llu end %llu length %u\n",
1167 (unsigned long long)start,
1168 (unsigned long long)start + size, size);
1171 /* Caller must ensure sizeof(*ret) >= 14 "WRITTEN|RELOC" */
1172 static void header_flags_to_str(u64 flags, char *ret)
1174 int empty = 1;
1176 if (flags & BTRFS_HEADER_FLAG_WRITTEN) {
1177 empty = 0;
1178 strcpy(ret, "WRITTEN");
1180 if (flags & BTRFS_HEADER_FLAG_RELOC) {
1181 if (!empty)
1182 strcat(ret, "|");
1183 strcat(ret, "RELOC");
1187 void btrfs_print_leaf(struct extent_buffer *eb)
1189 struct btrfs_fs_info *fs_info = eb->fs_info;
1190 struct btrfs_item *item;
1191 struct btrfs_disk_key disk_key;
1192 char flags_str[128];
1193 u32 leaf_data_size = BTRFS_LEAF_DATA_SIZE(fs_info);
1194 u32 i;
1195 u32 nr;
1196 u64 flags;
1197 u8 backref_rev;
1199 flags = btrfs_header_flags(eb) & ~BTRFS_BACKREF_REV_MASK;
1200 backref_rev = btrfs_header_flags(eb) >> BTRFS_BACKREF_REV_SHIFT;
1201 header_flags_to_str(flags, flags_str);
1202 nr = btrfs_header_nritems(eb);
1204 printf("leaf %llu items %d free space %d generation %llu owner ",
1205 (unsigned long long)btrfs_header_bytenr(eb), nr,
1206 btrfs_leaf_free_space(eb),
1207 (unsigned long long)btrfs_header_generation(eb));
1208 print_objectid(stdout, btrfs_header_owner(eb), 0);
1209 printf("\n");
1210 printf("leaf %llu flags 0x%llx(%s) backref revision %d\n",
1211 btrfs_header_bytenr(eb), flags, flags_str, backref_rev);
1212 print_uuids(eb);
1213 fflush(stdout);
1215 for (i = 0; i < nr; i++) {
1216 u32 item_size;
1217 void *ptr;
1218 u64 objectid;
1219 u32 type;
1220 u64 offset;
1223 * Extra check on item pointers
1224 * Here we don't need to be as strict as kernel leaf check.
1225 * Only need to ensure all pointers are pointing range inside
1226 * the leaf, thus no segfault.
1228 if (btrfs_item_offset_nr(eb, i) > leaf_data_size ||
1229 btrfs_item_size_nr(eb, i) + btrfs_item_offset_nr(eb, i) >
1230 leaf_data_size) {
1231 error(
1232 "leaf %llu slot %u pointer invalid, offset %u size %u leaf data limit %u",
1233 btrfs_header_bytenr(eb), i,
1234 btrfs_item_offset_nr(eb, i),
1235 btrfs_item_size_nr(eb, i), leaf_data_size);
1236 error("skip remaining slots");
1237 break;
1239 item = btrfs_item_nr(i);
1240 item_size = btrfs_item_size(eb, item);
1241 /* Untyped extraction of slot from btrfs_item_ptr */
1242 ptr = btrfs_item_ptr(eb, i, void*);
1244 btrfs_item_key(eb, &disk_key, i);
1245 objectid = btrfs_disk_key_objectid(&disk_key);
1246 type = btrfs_disk_key_type(&disk_key);
1247 offset = btrfs_disk_key_offset(&disk_key);
1249 printf("\titem %d ", i);
1250 btrfs_print_key(&disk_key);
1251 printf(" itemoff %d itemsize %d\n",
1252 btrfs_item_offset(eb, item),
1253 btrfs_item_size(eb, item));
1255 if (type == 0 && objectid == BTRFS_FREE_SPACE_OBJECTID)
1256 print_free_space_header(eb, i);
1258 switch (type) {
1259 case BTRFS_INODE_ITEM_KEY:
1260 print_inode_item(eb, ptr);
1261 break;
1262 case BTRFS_INODE_REF_KEY:
1263 print_inode_ref_item(eb, item_size, ptr);
1264 break;
1265 case BTRFS_INODE_EXTREF_KEY:
1266 print_inode_extref_item(eb, item_size, ptr);
1267 break;
1268 case BTRFS_DIR_ITEM_KEY:
1269 case BTRFS_DIR_INDEX_KEY:
1270 case BTRFS_XATTR_ITEM_KEY:
1271 print_dir_item(eb, item_size, ptr);
1272 break;
1273 case BTRFS_DIR_LOG_INDEX_KEY:
1274 case BTRFS_DIR_LOG_ITEM_KEY: {
1275 struct btrfs_dir_log_item *dlog;
1277 dlog = btrfs_item_ptr(eb, i, struct btrfs_dir_log_item);
1278 printf("\t\tdir log end %Lu\n",
1279 (unsigned long long)btrfs_dir_log_end(eb, dlog));
1280 break;
1282 case BTRFS_ORPHAN_ITEM_KEY:
1283 printf("\t\torphan item\n");
1284 break;
1285 case BTRFS_ROOT_ITEM_KEY:
1286 print_root_item(eb, i);
1287 break;
1288 case BTRFS_ROOT_REF_KEY:
1289 print_root_ref(eb, i, "ref");
1290 break;
1291 case BTRFS_ROOT_BACKREF_KEY:
1292 print_root_ref(eb, i, "backref");
1293 break;
1294 case BTRFS_EXTENT_ITEM_KEY:
1295 print_extent_item(eb, i, 0);
1296 break;
1297 case BTRFS_METADATA_ITEM_KEY:
1298 print_extent_item(eb, i, 1);
1299 break;
1300 case BTRFS_TREE_BLOCK_REF_KEY:
1301 printf("\t\ttree block backref\n");
1302 break;
1303 case BTRFS_SHARED_BLOCK_REF_KEY:
1304 printf("\t\tshared block backref\n");
1305 break;
1306 case BTRFS_EXTENT_DATA_REF_KEY:
1307 print_extent_data_ref(eb, i);
1308 break;
1309 case BTRFS_SHARED_DATA_REF_KEY:
1310 print_shared_data_ref(eb, i);
1311 break;
1312 case BTRFS_EXTENT_REF_V0_KEY:
1313 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1314 print_extent_ref_v0(eb, i);
1315 #else
1316 BUG();
1317 #endif
1318 break;
1319 case BTRFS_CSUM_ITEM_KEY:
1320 printf("\t\tcsum item\n");
1321 break;
1322 case BTRFS_EXTENT_CSUM_KEY:
1323 print_extent_csum(eb, fs_info, item_size,
1324 offset);
1325 break;
1326 case BTRFS_EXTENT_DATA_KEY:
1327 print_file_extent_item(eb, item, i, ptr);
1328 break;
1329 case BTRFS_BLOCK_GROUP_ITEM_KEY:
1330 print_block_group_item(eb, ptr);
1331 break;
1332 case BTRFS_FREE_SPACE_INFO_KEY:
1333 print_free_space_info(eb, i);
1334 break;
1335 case BTRFS_FREE_SPACE_EXTENT_KEY:
1336 printf("\t\tfree space extent\n");
1337 break;
1338 case BTRFS_FREE_SPACE_BITMAP_KEY:
1339 printf("\t\tfree space bitmap\n");
1340 break;
1341 case BTRFS_CHUNK_ITEM_KEY:
1342 print_chunk_item(eb, ptr);
1343 break;
1344 case BTRFS_DEV_ITEM_KEY:
1345 print_dev_item(eb, ptr);
1346 break;
1347 case BTRFS_DEV_EXTENT_KEY:
1348 print_dev_extent(eb, i);
1349 break;
1350 case BTRFS_QGROUP_STATUS_KEY:
1351 print_qgroup_status(eb, i);
1352 break;
1353 case BTRFS_QGROUP_RELATION_KEY:
1354 break;
1355 case BTRFS_QGROUP_INFO_KEY:
1356 print_qgroup_info(eb, i);
1357 break;
1358 case BTRFS_QGROUP_LIMIT_KEY:
1359 print_qgroup_limit(eb, i);
1360 break;
1361 case BTRFS_UUID_KEY_SUBVOL:
1362 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
1363 print_uuid_item(eb, btrfs_item_ptr_offset(eb, i),
1364 btrfs_item_size_nr(eb, i));
1365 break;
1366 case BTRFS_STRING_ITEM_KEY: {
1367 const char *str = eb->data + btrfs_item_ptr_offset(eb, i);
1369 printf("\t\titem data %.*s\n", item_size, str);
1370 break;
1372 case BTRFS_PERSISTENT_ITEM_KEY:
1373 print_persistent_item(eb, ptr, item_size, objectid,
1374 offset);
1375 break;
1376 case BTRFS_TEMPORARY_ITEM_KEY:
1377 print_temporary_item(eb, ptr, objectid, offset);
1378 break;
1380 fflush(stdout);
1384 void btrfs_print_tree(struct extent_buffer *eb, int follow)
1386 u32 i;
1387 u32 nr;
1388 u32 ptr_num;
1389 struct btrfs_fs_info *fs_info = eb->fs_info;
1390 struct btrfs_disk_key disk_key;
1391 struct btrfs_key key;
1392 struct extent_buffer *next;
1394 if (!eb)
1395 return;
1396 nr = btrfs_header_nritems(eb);
1397 if (btrfs_is_leaf(eb)) {
1398 btrfs_print_leaf(eb);
1399 return;
1401 /* We are crossing eb boundary, this node must be corrupted */
1402 if (nr > BTRFS_NODEPTRS_PER_EXTENT_BUFFER(eb))
1403 warning(
1404 "node nr_items corrupted, has %u limit %u, continue anyway",
1405 nr, BTRFS_NODEPTRS_PER_EXTENT_BUFFER(eb));
1406 printf("node %llu level %d items %d free %u generation %llu owner ",
1407 (unsigned long long)eb->start,
1408 btrfs_header_level(eb), nr,
1409 (u32)BTRFS_NODEPTRS_PER_EXTENT_BUFFER(eb) - nr,
1410 (unsigned long long)btrfs_header_generation(eb));
1411 print_objectid(stdout, btrfs_header_owner(eb), 0);
1412 printf("\n");
1413 print_uuids(eb);
1414 fflush(stdout);
1415 ptr_num = BTRFS_NODEPTRS_PER_EXTENT_BUFFER(eb);
1416 for (i = 0; i < nr && i < ptr_num; i++) {
1417 u64 blocknr = btrfs_node_blockptr(eb, i);
1419 btrfs_node_key(eb, &disk_key, i);
1420 btrfs_disk_key_to_cpu(&key, &disk_key);
1421 printf("\t");
1422 btrfs_print_key(&disk_key);
1423 printf(" block %llu (%llu) gen %llu\n",
1424 (unsigned long long)blocknr,
1425 (unsigned long long)blocknr / eb->len,
1426 (unsigned long long)btrfs_node_ptr_generation(eb, i));
1427 fflush(stdout);
1429 if (!follow)
1430 return;
1432 if (follow && !fs_info)
1433 return;
1435 for (i = 0; i < nr; i++) {
1436 next = read_tree_block(fs_info,
1437 btrfs_node_blockptr(eb, i),
1438 btrfs_node_ptr_generation(eb, i));
1439 if (!extent_buffer_uptodate(next)) {
1440 fprintf(stderr, "failed to read %llu in tree %llu\n",
1441 (unsigned long long)btrfs_node_blockptr(eb, i),
1442 (unsigned long long)btrfs_header_owner(eb));
1443 continue;
1445 if (btrfs_header_level(next) != btrfs_header_level(eb) - 1) {
1446 warning(
1447 "eb corrupted: parent bytenr %llu slot %d level %d child bytenr %llu level has %d expect %d, skipping the slot",
1448 btrfs_header_bytenr(eb), i,
1449 btrfs_header_level(eb),
1450 btrfs_header_bytenr(next),
1451 btrfs_header_level(next),
1452 btrfs_header_level(eb) - 1);
1453 free_extent_buffer(next);
1454 continue;
1456 btrfs_print_tree(next, 1);
1457 free_extent_buffer(next);
1460 return;