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