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