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