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