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