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