aae47a949ccd597dfed0e7ad6ec4282cc897b9db
[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
28 static int print_dir_item(struct extent_buffer *eb, struct btrfs_item *item,
29                           struct btrfs_dir_item *di)
30 {
31         u32 total;
32         u32 cur = 0;
33         u32 len;
34         u32 name_len;
35         u32 data_len;
36         char namebuf[BTRFS_NAME_LEN];
37         struct btrfs_disk_key location;
38
39         total = btrfs_item_size(eb, item);
40         while(cur < total) {
41                 btrfs_dir_item_key(eb, di, &location);
42                 printf("\t\tlocation ");
43                 btrfs_print_key(&location);
44                 printf(" type %u\n", btrfs_dir_type(eb, di));
45                 name_len = btrfs_dir_name_len(eb, di);
46                 data_len = btrfs_dir_data_len(eb, di);
47                 len = (name_len <= sizeof(namebuf))? name_len: sizeof(namebuf);
48                 read_extent_buffer(eb, namebuf, (unsigned long)(di + 1), len);
49                 printf("\t\tnamelen %u datalen %u name: %.*s\n",
50                        name_len, data_len, len, namebuf);
51                 if (data_len) {
52                         len = (data_len <= sizeof(namebuf))? data_len: sizeof(namebuf);
53                         read_extent_buffer(eb, namebuf,
54                                 (unsigned long)(di + 1) + name_len, len);
55                         printf("\t\tdata %.*s\n", len, namebuf);
56                 }
57                 len = sizeof(*di) + name_len + data_len;
58                 di = (struct btrfs_dir_item *)((char *)di + len);
59                 cur += len;
60         }
61         return 0;
62 }
63
64 static int print_inode_extref_item(struct extent_buffer *eb,
65                                    struct btrfs_item *item,
66                                    struct btrfs_inode_extref *extref)
67 {
68         u32 total;
69         u32 cur = 0;
70         u32 len;
71         u32 name_len = 0;
72         u64 index = 0;
73         u64 parent_objid;
74         char namebuf[BTRFS_NAME_LEN];
75
76         total = btrfs_item_size(eb, item);
77
78         while (cur < total) {
79                 index = btrfs_inode_extref_index(eb, extref);
80                 name_len = btrfs_inode_extref_name_len(eb, extref);
81                 parent_objid = btrfs_inode_extref_parent(eb, extref);
82
83                 len = (name_len <= sizeof(namebuf))? name_len: sizeof(namebuf);
84
85                 read_extent_buffer(eb, namebuf, (unsigned long)(extref->name), len);
86
87                 printf("\t\tinode extref index %llu parent %llu namelen %u "
88                        "name: %.*s\n",
89                        (unsigned long long)index,
90                        (unsigned long long)parent_objid,
91                        name_len, len, namebuf);
92
93                 len = sizeof(*extref) + name_len;
94                 extref = (struct btrfs_inode_extref *)((char *)extref + len);
95                 cur += len;
96         }
97         return 0;
98 }
99
100 static int print_inode_ref_item(struct extent_buffer *eb, struct btrfs_item *item,
101                                 struct btrfs_inode_ref *ref)
102 {
103         u32 total;
104         u32 cur = 0;
105         u32 len;
106         u32 name_len;
107         u64 index;
108         char namebuf[BTRFS_NAME_LEN];
109         total = btrfs_item_size(eb, item);
110         while(cur < total) {
111                 name_len = btrfs_inode_ref_name_len(eb, ref);
112                 index = btrfs_inode_ref_index(eb, ref);
113                 len = (name_len <= sizeof(namebuf))? name_len: sizeof(namebuf);
114                 read_extent_buffer(eb, namebuf, (unsigned long)(ref + 1), len);
115                 printf("\t\tinode ref index %llu namelen %u name: %.*s\n",
116                        (unsigned long long)index, name_len, len, namebuf);
117                 len = sizeof(*ref) + name_len;
118                 ref = (struct btrfs_inode_ref *)((char *)ref + len);
119                 cur += len;
120         }
121         return 0;
122 }
123
124 static void print_chunk(struct extent_buffer *eb, struct btrfs_chunk *chunk)
125 {
126         int num_stripes = btrfs_chunk_num_stripes(eb, chunk);
127         int i;
128         printf("\t\tchunk length %llu owner %llu type %llu num_stripes %d\n",
129                (unsigned long long)btrfs_chunk_length(eb, chunk),
130                (unsigned long long)btrfs_chunk_owner(eb, chunk),
131                (unsigned long long)btrfs_chunk_type(eb, chunk),
132                num_stripes);
133         for (i = 0 ; i < num_stripes ; i++) {
134                 printf("\t\t\tstripe %d devid %llu offset %llu\n", i,
135                       (unsigned long long)btrfs_stripe_devid_nr(eb, chunk, i),
136                       (unsigned long long)btrfs_stripe_offset_nr(eb, chunk, i));
137         }
138 }
139
140 static void print_dev_item(struct extent_buffer *eb,
141                            struct btrfs_dev_item *dev_item)
142 {
143         printf("\t\tdev item devid %llu "
144                "total_bytes %llu bytes used %Lu\n",
145                (unsigned long long)btrfs_device_id(eb, dev_item),
146                (unsigned long long)btrfs_device_total_bytes(eb, dev_item),
147                (unsigned long long)btrfs_device_bytes_used(eb, dev_item));
148 }
149
150 static void print_uuids(struct extent_buffer *eb)
151 {
152         char fs_uuid[37];
153         char chunk_uuid[37];
154         u8 disk_uuid[BTRFS_UUID_SIZE];
155
156         read_extent_buffer(eb, disk_uuid, (unsigned long)btrfs_header_fsid(eb),
157                            BTRFS_FSID_SIZE);
158
159         fs_uuid[36] = '\0';
160         uuid_unparse(disk_uuid, fs_uuid);
161
162         read_extent_buffer(eb, disk_uuid,
163                            (unsigned long)btrfs_header_chunk_tree_uuid(eb),
164                            BTRFS_UUID_SIZE);
165
166         chunk_uuid[36] = '\0';
167         uuid_unparse(disk_uuid, chunk_uuid);
168         printf("fs uuid %s\nchunk uuid %s\n", fs_uuid, chunk_uuid);
169 }
170
171 static void print_file_extent_item(struct extent_buffer *eb,
172                                    struct btrfs_item *item,
173                                    struct btrfs_file_extent_item *fi)
174 {
175         int extent_type = btrfs_file_extent_type(eb, fi);
176
177         if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
178                 printf("\t\tinline extent data size %u "
179                        "ram %u compress %d\n",
180                   btrfs_file_extent_inline_item_len(eb, item),
181                   btrfs_file_extent_inline_len(eb, fi),
182                   btrfs_file_extent_compression(eb, fi));
183                 return;
184         }
185         if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
186                 printf("\t\tprealloc data disk byte %llu nr %llu\n",
187                   (unsigned long long)btrfs_file_extent_disk_bytenr(eb, fi),
188                   (unsigned long long)btrfs_file_extent_disk_num_bytes(eb, fi));
189                 printf("\t\tprealloc data offset %llu nr %llu\n",
190                   (unsigned long long)btrfs_file_extent_offset(eb, fi),
191                   (unsigned long long)btrfs_file_extent_num_bytes(eb, fi));
192                 return;
193         }
194         printf("\t\textent data disk byte %llu nr %llu\n",
195                 (unsigned long long)btrfs_file_extent_disk_bytenr(eb, fi),
196                 (unsigned long long)btrfs_file_extent_disk_num_bytes(eb, fi));
197         printf("\t\textent data offset %llu nr %llu ram %llu\n",
198                 (unsigned long long)btrfs_file_extent_offset(eb, fi),
199                 (unsigned long long)btrfs_file_extent_num_bytes(eb, fi),
200                 (unsigned long long)btrfs_file_extent_ram_bytes(eb, fi));
201         printf("\t\textent compression %d\n",
202                btrfs_file_extent_compression(eb, fi));
203 }
204
205 static void print_extent_item(struct extent_buffer *eb, int slot, int metadata)
206 {
207         struct btrfs_extent_item *ei;
208         struct btrfs_extent_inline_ref *iref;
209         struct btrfs_extent_data_ref *dref;
210         struct btrfs_shared_data_ref *sref;
211         struct btrfs_disk_key key;
212         unsigned long end;
213         unsigned long ptr;
214         int type;
215         u32 item_size = btrfs_item_size_nr(eb, slot);
216         u64 flags;
217         u64 offset;
218
219         if (item_size < sizeof(*ei)) {
220 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
221                 struct btrfs_extent_item_v0 *ei0;
222                 BUG_ON(item_size != sizeof(*ei0));
223                 ei0 = btrfs_item_ptr(eb, slot, struct btrfs_extent_item_v0);
224                 printf("\t\textent refs %u\n",
225                        btrfs_extent_refs_v0(eb, ei0));
226                 return;
227 #else
228                 BUG();
229 #endif
230         }
231
232         ei = btrfs_item_ptr(eb, slot, struct btrfs_extent_item);
233         flags = btrfs_extent_flags(eb, ei);
234
235         printf("\t\textent refs %llu gen %llu flags %llu\n",
236                (unsigned long long)btrfs_extent_refs(eb, ei),
237                (unsigned long long)btrfs_extent_generation(eb, ei),
238                (unsigned long long)flags);
239
240         if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !metadata) {
241                 struct btrfs_tree_block_info *info;
242                 info = (struct btrfs_tree_block_info *)(ei + 1);
243                 btrfs_tree_block_key(eb, info, &key);
244                 printf("\t\ttree block ");
245                 btrfs_print_key(&key);
246                 printf(" level %d\n", btrfs_tree_block_level(eb, info));
247                 iref = (struct btrfs_extent_inline_ref *)(info + 1);
248         } else if (metadata) {
249                 struct btrfs_key tmp;
250
251                 btrfs_item_key_to_cpu(eb, &tmp, slot);
252                 printf("\t\ttree block skinny level %d\n", (int)tmp.offset);
253                 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
254         } else{
255                 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
256         }
257
258         ptr = (unsigned long)iref;
259         end = (unsigned long)ei + item_size;
260         while (ptr < end) {
261                 iref = (struct btrfs_extent_inline_ref *)ptr;
262                 type = btrfs_extent_inline_ref_type(eb, iref);
263                 offset = btrfs_extent_inline_ref_offset(eb, iref);
264                 switch (type) {
265                 case BTRFS_TREE_BLOCK_REF_KEY:
266                         printf("\t\ttree block backref root %llu\n",
267                                (unsigned long long)offset);
268                         break;
269                 case BTRFS_SHARED_BLOCK_REF_KEY:
270                         printf("\t\tshared block backref parent %llu\n",
271                                (unsigned long long)offset);
272                         break;
273                 case BTRFS_EXTENT_DATA_REF_KEY:
274                         dref = (struct btrfs_extent_data_ref *)(&iref->offset);
275                         printf("\t\textent data backref root %llu "
276                                "objectid %llu offset %llu count %u\n",
277                                (unsigned long long)btrfs_extent_data_ref_root(eb, dref),
278                                (unsigned long long)btrfs_extent_data_ref_objectid(eb, dref),
279                                (unsigned long long)btrfs_extent_data_ref_offset(eb, dref),
280                                btrfs_extent_data_ref_count(eb, dref));
281                         break;
282                 case BTRFS_SHARED_DATA_REF_KEY:
283                         sref = (struct btrfs_shared_data_ref *)(iref + 1);
284                         printf("\t\tshared data backref parent %llu count %u\n",
285                                (unsigned long long)offset,
286                                btrfs_shared_data_ref_count(eb, sref));
287                         break;
288                 default:
289                         return;
290                 }
291                 ptr += btrfs_extent_inline_ref_size(type);
292         }
293         WARN_ON(ptr > end);
294 }
295
296 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
297 static void print_extent_ref_v0(struct extent_buffer *eb, int slot)
298 {
299         struct btrfs_extent_ref_v0 *ref0;
300
301         ref0 = btrfs_item_ptr(eb, slot, struct btrfs_extent_ref_v0);
302         printf("\t\textent back ref root %llu gen %llu "
303                 "owner %llu num_refs %lu\n",
304                 (unsigned long long)btrfs_ref_root_v0(eb, ref0),
305                 (unsigned long long)btrfs_ref_generation_v0(eb, ref0),
306                 (unsigned long long)btrfs_ref_objectid_v0(eb, ref0),
307                 (unsigned long)btrfs_ref_count_v0(eb, ref0));
308 }
309 #endif
310
311 static void print_root_ref(struct extent_buffer *leaf, int slot, char *tag)
312 {
313         struct btrfs_root_ref *ref;
314         char namebuf[BTRFS_NAME_LEN];
315         int namelen;
316
317         ref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
318         namelen = btrfs_root_ref_name_len(leaf, ref);
319         read_extent_buffer(leaf, namebuf, (unsigned long)(ref + 1), namelen);
320         printf("\t\troot %s key dirid %llu sequence %llu name %.*s\n", tag,
321                (unsigned long long)btrfs_root_ref_dirid(leaf, ref),
322                (unsigned long long)btrfs_root_ref_sequence(leaf, ref),
323                namelen, namebuf);
324 }
325
326 static int count_bytes(void *buf, int len, char b)
327 {
328         int cnt = 0;
329         int i;
330         for (i = 0; i < len; i++) {
331                 if (((char*)buf)[i] == b)
332                         cnt++;
333         }
334         return cnt;
335 }
336
337 static void print_root(struct extent_buffer *leaf, int slot)
338 {
339         struct btrfs_root_item *ri;
340         struct btrfs_root_item root_item;
341         int len;
342         char uuid_str[128];
343
344         ri = btrfs_item_ptr(leaf, slot, struct btrfs_root_item);
345         len = btrfs_item_size_nr(leaf, slot);
346
347         memset(&root_item, 0, sizeof(root_item));
348         read_extent_buffer(leaf, &root_item, (unsigned long)ri, len);
349
350         printf("\t\troot data bytenr %llu level %d dirid %llu refs %u gen %llu\n",
351                 (unsigned long long)btrfs_root_bytenr(&root_item),
352                 btrfs_root_level(&root_item),
353                 (unsigned long long)btrfs_root_dirid(&root_item),
354                 btrfs_root_refs(&root_item),
355                 (unsigned long long)btrfs_root_generation(&root_item));
356
357         if (root_item.generation == root_item.generation_v2) {
358                 uuid_unparse(root_item.uuid, uuid_str);
359                 printf("\t\tuuid %s\n", uuid_str);
360                 if (count_bytes(root_item.parent_uuid, BTRFS_UUID_SIZE, 0) != BTRFS_UUID_SIZE) {
361                         uuid_unparse(root_item.parent_uuid, uuid_str);
362                         printf("\t\tparent_uuid %s\n", uuid_str);
363                 }
364                 if (count_bytes(root_item.received_uuid, BTRFS_UUID_SIZE, 0) != BTRFS_UUID_SIZE) {
365                         uuid_unparse(root_item.received_uuid, uuid_str);
366                         printf("\t\treceived_uuid %s\n", uuid_str);
367                 }
368                 if (root_item.ctransid) {
369                         printf("\t\tctransid %llu otransid %llu stransid %llu rtransid %llu\n",
370                                 btrfs_root_ctransid(&root_item),
371                                 btrfs_root_otransid(&root_item),
372                                 btrfs_root_stransid(&root_item),
373                                 btrfs_root_rtransid(&root_item));
374                 }
375         }
376         if (btrfs_root_refs(&root_item) == 0) {
377                 struct btrfs_key drop_key;
378                 btrfs_disk_key_to_cpu(&drop_key,
379                                       &root_item.drop_progress);
380                 printf("\t\tdrop ");
381                 btrfs_print_key(&root_item.drop_progress);
382                 printf(" level %d\n", root_item.drop_level);
383         }
384 }
385
386 static void print_free_space_header(struct extent_buffer *leaf, int slot)
387 {
388         struct btrfs_free_space_header *header;
389         struct btrfs_disk_key location;
390
391         header = btrfs_item_ptr(leaf, slot, struct btrfs_free_space_header);
392         btrfs_free_space_key(leaf, header, &location);
393         printf("\t\tlocation ");
394         btrfs_print_key(&location);
395         printf("\n");
396         printf("\t\tcache generation %llu entries %llu bitmaps %llu\n",
397                (unsigned long long)btrfs_free_space_generation(leaf, header),
398                (unsigned long long)btrfs_free_space_entries(leaf, header),
399                (unsigned long long)btrfs_free_space_bitmaps(leaf, header));
400 }
401
402 static void print_key_type(u64 objectid, u8 type)
403 {
404         if (type == 0 && objectid == BTRFS_FREE_SPACE_OBJECTID) {
405                 printf("UNTYPED");
406                 return;
407         }
408
409         switch (type) {
410         case BTRFS_INODE_ITEM_KEY:
411                 printf("INODE_ITEM");
412                 break;
413         case BTRFS_INODE_REF_KEY:
414                 printf("INODE_REF");
415                 break;
416         case BTRFS_INODE_EXTREF_KEY:
417                 printf("INODE_EXTREF");
418                 break;
419         case BTRFS_DIR_ITEM_KEY:
420                 printf("DIR_ITEM");
421                 break;
422         case BTRFS_DIR_INDEX_KEY:
423                 printf("DIR_INDEX");
424                 break;
425         case BTRFS_DIR_LOG_ITEM_KEY:
426                 printf("DIR_LOG_ITEM");
427                 break;
428         case BTRFS_DIR_LOG_INDEX_KEY:
429                 printf("DIR_LOG_INDEX");
430                 break;
431         case BTRFS_XATTR_ITEM_KEY:
432                 printf("XATTR_ITEM");
433                 break;
434         case BTRFS_ORPHAN_ITEM_KEY:
435                 printf("ORPHAN_ITEM");
436                 break;
437         case BTRFS_ROOT_ITEM_KEY:
438                 printf("ROOT_ITEM");
439                 break;
440         case BTRFS_ROOT_REF_KEY:
441                 printf("ROOT_REF");
442                 break;
443         case BTRFS_ROOT_BACKREF_KEY:
444                 printf("ROOT_BACKREF");
445                 break;
446         case BTRFS_EXTENT_ITEM_KEY:
447                 printf("EXTENT_ITEM");
448                 break;
449         case BTRFS_METADATA_ITEM_KEY:
450                 printf("METADATA_ITEM");
451                 break;
452         case BTRFS_TREE_BLOCK_REF_KEY:
453                 printf("TREE_BLOCK_REF");
454                 break;
455         case BTRFS_SHARED_BLOCK_REF_KEY:
456                 printf("SHARED_BLOCK_REF");
457                 break;
458         case BTRFS_EXTENT_DATA_REF_KEY:
459                 printf("EXTENT_DATA_REF");
460                 break;
461         case BTRFS_SHARED_DATA_REF_KEY:
462                 printf("SHARED_DATA_REF");
463                 break;
464         case BTRFS_EXTENT_REF_V0_KEY:
465                 printf("EXTENT_REF_V0");
466                 break;
467         case BTRFS_CSUM_ITEM_KEY:
468                 printf("CSUM_ITEM");
469                 break;
470         case BTRFS_EXTENT_CSUM_KEY:
471                 printf("EXTENT_CSUM");
472                 break;
473         case BTRFS_EXTENT_DATA_KEY:
474                 printf("EXTENT_DATA");
475                 break;
476         case BTRFS_BLOCK_GROUP_ITEM_KEY:
477                 printf("BLOCK_GROUP_ITEM");
478                 break;
479         case BTRFS_CHUNK_ITEM_KEY:
480                 printf("CHUNK_ITEM");
481                 break;
482         case BTRFS_DEV_ITEM_KEY:
483                 printf("DEV_ITEM");
484                 break;
485         case BTRFS_DEV_EXTENT_KEY:
486                 printf("DEV_EXTENT");
487                 break;
488         case BTRFS_BALANCE_ITEM_KEY:
489                 printf("BALANCE_ITEM");
490                 break;
491         case BTRFS_DEV_REPLACE_KEY:
492                 printf("DEV_REPLACE_ITEM");
493                 break;
494         case BTRFS_STRING_ITEM_KEY:
495                 printf("STRING_ITEM");
496                 break;
497         case BTRFS_QGROUP_STATUS_KEY:
498                 printf("BTRFS_STATUS_KEY");
499                 break;
500         case BTRFS_QGROUP_RELATION_KEY:
501                 printf("BTRFS_QGROUP_RELATION_KEY");
502                 break;
503         case BTRFS_QGROUP_INFO_KEY:
504                 printf("BTRFS_QGROUP_INFO_KEY");
505                 break;
506         case BTRFS_QGROUP_LIMIT_KEY:
507                 printf("BTRFS_QGROUP_LIMIT_KEY");
508                 break;
509         case BTRFS_DEV_STATS_KEY:
510                 printf("DEV_STATS_ITEM");
511                 break;
512         default:
513                 printf("UNKNOWN.%d", type);
514         };
515 }
516
517 static void print_objectid(u64 objectid, u8 type)
518 {
519         if (type == BTRFS_DEV_EXTENT_KEY) {
520                 printf("%llu", (unsigned long long)objectid); /* device id */
521                 return;
522         }
523         switch (type) {
524         case BTRFS_QGROUP_RELATION_KEY:
525                 printf("%llu/%llu", objectid >> 48,
526                         objectid & ((1ll << 48) - 1));
527                 return;
528         }
529
530         switch (objectid) {
531         case BTRFS_ROOT_TREE_OBJECTID:
532                 if (type == BTRFS_DEV_ITEM_KEY)
533                         printf("DEV_ITEMS");
534                 else
535                         printf("ROOT_TREE");
536                 break;
537         case BTRFS_EXTENT_TREE_OBJECTID:
538                 printf("EXTENT_TREE");
539                 break;
540         case BTRFS_CHUNK_TREE_OBJECTID:
541                 printf("CHUNK_TREE");
542                 break;
543         case BTRFS_DEV_TREE_OBJECTID:
544                 printf("DEV_TREE");
545                 break;
546         case BTRFS_FS_TREE_OBJECTID:
547                 printf("FS_TREE");
548                 break;
549         case BTRFS_ROOT_TREE_DIR_OBJECTID:
550                 printf("ROOT_TREE_DIR");
551                 break;
552         case BTRFS_CSUM_TREE_OBJECTID:
553                 printf("CSUM_TREE");
554                 break;
555         case BTRFS_BALANCE_OBJECTID:
556                 printf("BALANCE");
557                 break;
558         case BTRFS_ORPHAN_OBJECTID:
559                 printf("ORPHAN");
560                 break;
561         case BTRFS_TREE_LOG_OBJECTID:
562                 printf("TREE_LOG");
563                 break;
564         case BTRFS_TREE_LOG_FIXUP_OBJECTID:
565                 printf("LOG_FIXUP");
566                 break;
567         case BTRFS_TREE_RELOC_OBJECTID:
568                 printf("TREE_RELOC");
569                 break;
570         case BTRFS_DATA_RELOC_TREE_OBJECTID:
571                 printf("DATA_RELOC_TREE");
572                 break;
573         case BTRFS_EXTENT_CSUM_OBJECTID:
574                 printf("EXTENT_CSUM");
575                 break;
576         case BTRFS_FREE_SPACE_OBJECTID:
577                 printf("FREE_SPACE");
578                 break;
579         case BTRFS_FREE_INO_OBJECTID:
580                 printf("FREE_INO");
581                 break;
582         case BTRFS_QUOTA_TREE_OBJECTID:
583                 printf("QUOTA_TREE");
584                 break;
585         case BTRFS_MULTIPLE_OBJECTIDS:
586                 printf("MULTIPLE");
587                 break;
588         case (u64)-1:
589                 printf("-1");
590                 break;
591         case BTRFS_FIRST_CHUNK_TREE_OBJECTID:
592                 if (type == BTRFS_CHUNK_ITEM_KEY) {
593                         printf("FIRST_CHUNK_TREE");
594                         break;
595                 }
596                 /* fall-thru */
597         default:
598                 printf("%llu", (unsigned long long)objectid);
599         }
600 }
601
602 void btrfs_print_key(struct btrfs_disk_key *disk_key)
603 {
604         u64 objectid = btrfs_disk_key_objectid(disk_key);
605         u8 type = btrfs_disk_key_type(disk_key);
606         u64 offset = btrfs_disk_key_offset(disk_key);
607
608         printf("key (");
609         print_objectid(objectid, type);
610         printf(" ");
611         print_key_type(objectid, type);
612         switch (type) {
613         case BTRFS_QGROUP_RELATION_KEY:
614         case BTRFS_QGROUP_INFO_KEY:
615         case BTRFS_QGROUP_LIMIT_KEY:
616                 printf(" %llu/%llu)", (unsigned long long)(offset >> 48),
617                         (unsigned long long)(offset & ((1ll << 48) - 1)));
618                 break;
619         default:
620                 if (offset == (u64)-1)
621                         printf(" -1)");
622                 else
623                         printf(" %llu)", (unsigned long long)offset);
624                 break;
625         }
626 }
627
628 void btrfs_print_leaf(struct btrfs_root *root, struct extent_buffer *l)
629 {
630         int i;
631         char *str;
632         struct btrfs_item *item;
633         struct btrfs_dir_item *di;
634         struct btrfs_inode_item *ii;
635         struct btrfs_file_extent_item *fi;
636         struct btrfs_block_group_item *bi;
637         struct btrfs_extent_data_ref *dref;
638         struct btrfs_shared_data_ref *sref;
639         struct btrfs_inode_ref *iref;
640         struct btrfs_inode_extref *iref2;
641         struct btrfs_dev_extent *dev_extent;
642         struct btrfs_disk_key disk_key;
643         struct btrfs_block_group_item bg_item;
644         struct btrfs_dir_log_item *dlog;
645         struct btrfs_qgroup_info_item *qg_info;
646         struct btrfs_qgroup_limit_item *qg_limit;
647         struct btrfs_qgroup_status_item *qg_status;
648         u32 nr = btrfs_header_nritems(l);
649         u64 objectid;
650         u32 type;
651
652         printf("leaf %llu items %d free space %d generation %llu owner %llu\n",
653                 (unsigned long long)btrfs_header_bytenr(l), nr,
654                 btrfs_leaf_free_space(root, l),
655                 (unsigned long long)btrfs_header_generation(l),
656                 (unsigned long long)btrfs_header_owner(l));
657         print_uuids(l);
658         fflush(stdout);
659         for (i = 0 ; i < nr ; i++) {
660                 item = btrfs_item_nr(l, i);
661                 btrfs_item_key(l, &disk_key, i);
662                 objectid = btrfs_disk_key_objectid(&disk_key);
663                 type = btrfs_disk_key_type(&disk_key);
664                 printf("\titem %d ", i);
665                 btrfs_print_key(&disk_key);
666                 printf(" itemoff %d itemsize %d\n",
667                         btrfs_item_offset(l, item),
668                         btrfs_item_size(l, item));
669
670                 if (type == 0 && objectid == BTRFS_FREE_SPACE_OBJECTID)
671                         print_free_space_header(l, i);
672
673                 switch (type) {
674                 case BTRFS_INODE_ITEM_KEY:
675                         ii = btrfs_item_ptr(l, i, struct btrfs_inode_item);
676                         printf("\t\tinode generation %llu transid %llu size %llu block group %llu mode %o links %u\n",
677                                (unsigned long long)btrfs_inode_generation(l, ii),
678                                (unsigned long long)btrfs_inode_transid(l, ii),
679                                (unsigned long long)btrfs_inode_size(l, ii),
680                                (unsigned long long)btrfs_inode_block_group(l,ii),
681                                btrfs_inode_mode(l, ii),
682                                btrfs_inode_nlink(l, ii));
683                         break;
684                 case BTRFS_INODE_REF_KEY:
685                         iref = btrfs_item_ptr(l, i, struct btrfs_inode_ref);
686                         print_inode_ref_item(l, item, iref);
687                         break;
688                 case BTRFS_INODE_EXTREF_KEY:
689                         iref2 = btrfs_item_ptr(l, i, struct btrfs_inode_extref);
690                         print_inode_extref_item(l, item, iref2);
691                         break;
692                 case BTRFS_DIR_ITEM_KEY:
693                 case BTRFS_DIR_INDEX_KEY:
694                 case BTRFS_XATTR_ITEM_KEY:
695                         di = btrfs_item_ptr(l, i, struct btrfs_dir_item);
696                         print_dir_item(l, item, di);
697                         break;
698                 case BTRFS_DIR_LOG_INDEX_KEY:
699                 case BTRFS_DIR_LOG_ITEM_KEY:
700                         dlog = btrfs_item_ptr(l, i, struct btrfs_dir_log_item);
701                         printf("\t\tdir log end %Lu\n",
702                                (unsigned long long)btrfs_dir_log_end(l, dlog));
703                        break;
704                 case BTRFS_ORPHAN_ITEM_KEY:
705                         printf("\t\torphan item\n");
706                         break;
707                 case BTRFS_ROOT_ITEM_KEY:
708                         print_root(l, i);
709                         break;
710                 case BTRFS_ROOT_REF_KEY:
711                         print_root_ref(l, i, "ref");
712                         break;
713                 case BTRFS_ROOT_BACKREF_KEY:
714                         print_root_ref(l, i, "backref");
715                         break;
716                 case BTRFS_EXTENT_ITEM_KEY:
717                         print_extent_item(l, i, 0);
718                         break;
719                 case BTRFS_METADATA_ITEM_KEY:
720                         print_extent_item(l, i, 1);
721                         break;
722                 case BTRFS_TREE_BLOCK_REF_KEY:
723                         printf("\t\ttree block backref\n");
724                         break;
725                 case BTRFS_SHARED_BLOCK_REF_KEY:
726                         printf("\t\tshared block backref\n");
727                         break;
728                 case BTRFS_EXTENT_DATA_REF_KEY:
729                         dref = btrfs_item_ptr(l, i, struct btrfs_extent_data_ref);
730                         printf("\t\textent data backref root %llu "
731                                "objectid %llu offset %llu count %u\n",
732                                (unsigned long long)btrfs_extent_data_ref_root(l, dref),
733                                (unsigned long long)btrfs_extent_data_ref_objectid(l, dref),
734                                (unsigned long long)btrfs_extent_data_ref_offset(l, dref),
735                                btrfs_extent_data_ref_count(l, dref));
736                         break;
737                 case BTRFS_SHARED_DATA_REF_KEY:
738                         sref = btrfs_item_ptr(l, i, struct btrfs_shared_data_ref);
739                         printf("\t\tshared data backref count %u\n",
740                                btrfs_shared_data_ref_count(l, sref));
741                         break;
742                 case BTRFS_EXTENT_REF_V0_KEY:
743 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
744                         print_extent_ref_v0(l, i);
745 #else
746                         BUG();
747 #endif
748                         break;
749                 case BTRFS_CSUM_ITEM_KEY:
750                         printf("\t\tcsum item\n");
751                         break;
752                 case BTRFS_EXTENT_CSUM_KEY:
753                         printf("\t\textent csum item\n");
754                         break;
755                 case BTRFS_EXTENT_DATA_KEY:
756                         fi = btrfs_item_ptr(l, i,
757                                             struct btrfs_file_extent_item);
758                         print_file_extent_item(l, item, fi);
759                         break;
760                 case BTRFS_BLOCK_GROUP_ITEM_KEY:
761                         bi = btrfs_item_ptr(l, i,
762                                             struct btrfs_block_group_item);
763                         read_extent_buffer(l, &bg_item, (unsigned long)bi,
764                                            sizeof(bg_item));
765                         printf("\t\tblock group used %llu chunk_objectid %llu flags %llu\n",
766                                (unsigned long long)btrfs_block_group_used(&bg_item),
767                                (unsigned long long)btrfs_block_group_chunk_objectid(&bg_item),
768                                (unsigned long long)btrfs_block_group_flags(&bg_item));
769                         break;
770                 case BTRFS_CHUNK_ITEM_KEY:
771                         print_chunk(l, btrfs_item_ptr(l, i, struct btrfs_chunk));
772                         break;
773                 case BTRFS_DEV_ITEM_KEY:
774                         print_dev_item(l, btrfs_item_ptr(l, i,
775                                         struct btrfs_dev_item));
776                         break;
777                 case BTRFS_DEV_EXTENT_KEY:
778                         dev_extent = btrfs_item_ptr(l, i,
779                                                     struct btrfs_dev_extent);
780                         printf("\t\tdev extent chunk_tree %llu\n"
781                                "\t\tchunk objectid %llu chunk offset %llu "
782                                "length %llu\n",
783                                (unsigned long long)
784                                btrfs_dev_extent_chunk_tree(l, dev_extent),
785                                (unsigned long long)
786                                btrfs_dev_extent_chunk_objectid(l, dev_extent),
787                                (unsigned long long)
788                                btrfs_dev_extent_chunk_offset(l, dev_extent),
789                                (unsigned long long)
790                                btrfs_dev_extent_length(l, dev_extent));
791                         break;
792                 case BTRFS_QGROUP_STATUS_KEY:
793                         qg_status = btrfs_item_ptr(l, i,
794                                         struct btrfs_qgroup_status_item);
795                         printf("\t\tversion %llu generation %llu flags %#llx "
796                                 "scan %lld\n",
797                                 (unsigned long long)
798                                 btrfs_qgroup_status_version(l, qg_status),
799                                 (unsigned long long)
800                                 btrfs_qgroup_status_generation(l, qg_status),
801                                 (unsigned long long)
802                                 btrfs_qgroup_status_flags(l, qg_status),
803                                 (unsigned long long)
804                                 btrfs_qgroup_status_scan(l, qg_status));
805                         break;
806                 case BTRFS_QGROUP_RELATION_KEY:
807                         break;
808                 case BTRFS_QGROUP_INFO_KEY:
809                         qg_info = btrfs_item_ptr(l, i,
810                                                  struct btrfs_qgroup_info_item);
811                         printf("\t\tgeneration %llu\n"
812                              "\t\treferenced %lld referenced compressed %lld\n"
813                              "\t\texclusive %lld exclusive compressed %lld\n",
814                                (unsigned long long)
815                                btrfs_qgroup_info_generation(l, qg_info),
816                                (long long)
817                                btrfs_qgroup_info_referenced(l, qg_info),
818                                (long long)
819                                btrfs_qgroup_info_referenced_compressed(l,
820                                                                        qg_info),
821                                (long long)
822                                btrfs_qgroup_info_exclusive(l, qg_info),
823                                (long long)
824                                btrfs_qgroup_info_exclusive_compressed(l,
825                                                                       qg_info));
826                         break;
827                 case BTRFS_QGROUP_LIMIT_KEY:
828                         qg_limit = btrfs_item_ptr(l, i,
829                                          struct btrfs_qgroup_limit_item);
830                         printf("\t\tflags %llx\n"
831                              "\t\tmax referenced %lld max exclusive %lld\n"
832                              "\t\trsv referenced %lld rsv exclusive %lld\n",
833                                (unsigned long long)
834                                btrfs_qgroup_limit_flags(l, qg_limit),
835                                (long long)
836                                btrfs_qgroup_limit_max_referenced(l, qg_limit),
837                                (long long)
838                                btrfs_qgroup_limit_max_exclusive(l, qg_limit),
839                                (long long)
840                                btrfs_qgroup_limit_rsv_referenced(l, qg_limit),
841                                (long long)
842                                btrfs_qgroup_limit_rsv_exclusive(l, qg_limit));
843                         break;
844                 case BTRFS_STRING_ITEM_KEY:
845                         /* dirty, but it's simple */
846                         str = l->data + btrfs_item_ptr_offset(l, i);
847                         printf("\t\titem data %.*s\n", btrfs_item_size(l, item), str);
848                         break;
849                 case BTRFS_DEV_STATS_KEY:
850                         printf("\t\tdevice stats\n");
851                         break;
852                 };
853                 fflush(stdout);
854         }
855 }
856
857 void btrfs_print_tree(struct btrfs_root *root, struct extent_buffer *eb, int follow)
858 {
859         int i;
860         u32 nr;
861         u32 size;
862         struct btrfs_disk_key disk_key;
863         struct btrfs_key key;
864
865         if (!eb)
866                 return;
867         nr = btrfs_header_nritems(eb);
868         if (btrfs_is_leaf(eb)) {
869                 btrfs_print_leaf(root, eb);
870                 return;
871         }
872         printf("node %llu level %d items %d free %u generation %llu owner %llu\n",
873                (unsigned long long)eb->start,
874                 btrfs_header_level(eb), nr,
875                 (u32)BTRFS_NODEPTRS_PER_BLOCK(root) - nr,
876                 (unsigned long long)btrfs_header_generation(eb),
877                 (unsigned long long)btrfs_header_owner(eb));
878         print_uuids(eb);
879         fflush(stdout);
880         size = btrfs_level_size(root, btrfs_header_level(eb) - 1);
881         for (i = 0; i < nr; i++) {
882                 u64 blocknr = btrfs_node_blockptr(eb, i);
883                 btrfs_node_key(eb, &disk_key, i);
884                 btrfs_disk_key_to_cpu(&key, &disk_key);
885                 printf("\t");
886                 btrfs_print_key(&disk_key);
887                 printf(" block %llu (%llu) gen %llu\n",
888                        (unsigned long long)blocknr,
889                        (unsigned long long)blocknr / size,
890                        (unsigned long long)btrfs_node_ptr_generation(eb, i));
891                 fflush(stdout);
892         }
893         if (!follow)
894                 return;
895
896         for (i = 0; i < nr; i++) {
897                 struct extent_buffer *next = read_tree_block(root,
898                                              btrfs_node_blockptr(eb, i),
899                                              size,
900                                              btrfs_node_ptr_generation(eb, i));
901                 if (!next) {
902                         fprintf(stderr, "failed to read %llu in tree %llu\n",
903                                 (unsigned long long)btrfs_node_blockptr(eb, i),
904                                 (unsigned long long)btrfs_header_owner(eb));
905                         continue;
906                 }
907                 if (btrfs_is_leaf(next) &&
908                     btrfs_header_level(eb) != 1)
909                         BUG();
910                 if (btrfs_header_level(next) !=
911                         btrfs_header_level(eb) - 1)
912                         BUG();
913                 btrfs_print_tree(root, next, 1);
914                 free_extent_buffer(next);
915         }
916 }