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