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