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