Btrfs-progs: update btrfs-progs for subvol uuid+times support
[platform/upstream/btrfs-progs.git] / print-tree.c
1 /*
2  * Copyright (C) 2007 Oracle.  All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public
6  * License v2 as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public
14  * License along with this program; if not, write to the
15  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16  * Boston, MA 021110-1307, USA.
17  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <uuid/uuid.h>
22 #include "kerncompat.h"
23 #include "radix-tree.h"
24 #include "ctree.h"
25 #include "disk-io.h"
26 #include "print-tree.h"
27
28 static int print_dir_item(struct extent_buffer *eb, struct btrfs_item *item,
29                           struct btrfs_dir_item *di)
30 {
31         u32 total;
32         u32 cur = 0;
33         u32 len;
34         u32 name_len;
35         u32 data_len;
36         char namebuf[BTRFS_NAME_LEN];
37         struct btrfs_disk_key location;
38
39         total = btrfs_item_size(eb, item);
40         while(cur < total) {
41                 btrfs_dir_item_key(eb, di, &location);
42                 printf("\t\tlocation ");
43                 btrfs_print_key(&location);
44                 printf(" type %u\n", btrfs_dir_type(eb, di));
45                 name_len = btrfs_dir_name_len(eb, di);
46                 data_len = btrfs_dir_data_len(eb, di);
47                 len = (name_len <= sizeof(namebuf))? name_len: sizeof(namebuf);
48                 read_extent_buffer(eb, namebuf, (unsigned long)(di + 1), len);
49                 printf("\t\tnamelen %u datalen %u name: %.*s\n",
50                        name_len, data_len, len, namebuf);
51                 if (data_len) {
52                         len = (data_len <= sizeof(namebuf))? data_len: sizeof(namebuf);
53                         read_extent_buffer(eb, namebuf,
54                                 (unsigned long)(di + 1) + name_len, len);
55                         printf("\t\tdata %.*s\n", len, namebuf);
56                 }
57                 len = sizeof(*di) + name_len + data_len;
58                 di = (struct btrfs_dir_item *)((char *)di + len);
59                 cur += len;
60         }
61         return 0;
62 }
63
64 static int print_inode_ref_item(struct extent_buffer *eb, struct btrfs_item *item,
65                                 struct btrfs_inode_ref *ref)
66 {
67         u32 total;
68         u32 cur = 0;
69         u32 len;
70         u32 name_len;
71         u64 index;
72         char namebuf[BTRFS_NAME_LEN];
73         total = btrfs_item_size(eb, item);
74         while(cur < total) {
75                 name_len = btrfs_inode_ref_name_len(eb, ref);
76                 index = btrfs_inode_ref_index(eb, ref);
77                 len = (name_len <= sizeof(namebuf))? name_len: sizeof(namebuf);
78                 read_extent_buffer(eb, namebuf, (unsigned long)(ref + 1), len);
79                 printf("\t\tinode ref index %llu namelen %u name: %.*s\n",
80                        (unsigned long long)index, name_len, len, namebuf);
81                 len = sizeof(*ref) + name_len;
82                 ref = (struct btrfs_inode_ref *)((char *)ref + len);
83                 cur += len;
84         }
85         return 0;
86 }
87
88 static void print_chunk(struct extent_buffer *eb, struct btrfs_chunk *chunk)
89 {
90         int num_stripes = btrfs_chunk_num_stripes(eb, chunk);
91         int i;
92         printf("\t\tchunk length %llu owner %llu type %llu num_stripes %d\n",
93                (unsigned long long)btrfs_chunk_length(eb, chunk),
94                (unsigned long long)btrfs_chunk_owner(eb, chunk),
95                (unsigned long long)btrfs_chunk_type(eb, chunk),
96                num_stripes);
97         for (i = 0 ; i < num_stripes ; i++) {
98                 printf("\t\t\tstripe %d devid %llu offset %llu\n", i,
99                       (unsigned long long)btrfs_stripe_devid_nr(eb, chunk, i),
100                       (unsigned long long)btrfs_stripe_offset_nr(eb, chunk, i));
101         }
102 }
103
104 static void print_dev_item(struct extent_buffer *eb,
105                            struct btrfs_dev_item *dev_item)
106 {
107         printf("\t\tdev item devid %llu "
108                "total_bytes %llu bytes used %Lu\n",
109                (unsigned long long)btrfs_device_id(eb, dev_item),
110                (unsigned long long)btrfs_device_total_bytes(eb, dev_item),
111                (unsigned long long)btrfs_device_bytes_used(eb, dev_item));
112 }
113
114 static void print_uuids(struct extent_buffer *eb)
115 {
116         char fs_uuid[37];
117         char chunk_uuid[37];
118         u8 disk_uuid[BTRFS_UUID_SIZE];
119
120         read_extent_buffer(eb, disk_uuid, (unsigned long)btrfs_header_fsid(eb),
121                            BTRFS_FSID_SIZE);
122
123         fs_uuid[36] = '\0';
124         uuid_unparse(disk_uuid, fs_uuid);
125
126         read_extent_buffer(eb, disk_uuid,
127                            (unsigned long)btrfs_header_chunk_tree_uuid(eb),
128                            BTRFS_UUID_SIZE);
129
130         chunk_uuid[36] = '\0';
131         uuid_unparse(disk_uuid, chunk_uuid);
132         printf("fs uuid %s\nchunk uuid %s\n", fs_uuid, chunk_uuid);
133 }
134
135 static void print_file_extent_item(struct extent_buffer *eb,
136                                    struct btrfs_item *item,
137                                    struct btrfs_file_extent_item *fi)
138 {
139         int extent_type = btrfs_file_extent_type(eb, fi);
140
141         if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
142                 printf("\t\tinline extent data size %u "
143                        "ram %u compress %d\n",
144                   btrfs_file_extent_inline_item_len(eb, item),
145                   btrfs_file_extent_inline_len(eb, fi),
146                   btrfs_file_extent_compression(eb, fi));
147                 return;
148         }
149         if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
150                 printf("\t\tprealloc data disk byte %llu nr %llu\n",
151                   (unsigned long long)btrfs_file_extent_disk_bytenr(eb, fi),
152                   (unsigned long long)btrfs_file_extent_disk_num_bytes(eb, fi));
153                 printf("\t\tprealloc data offset %llu nr %llu\n",
154                   (unsigned long long)btrfs_file_extent_offset(eb, fi),
155                   (unsigned long long)btrfs_file_extent_num_bytes(eb, fi));
156                 return;
157         }
158         printf("\t\textent data disk byte %llu nr %llu\n",
159                 (unsigned long long)btrfs_file_extent_disk_bytenr(eb, fi),
160                 (unsigned long long)btrfs_file_extent_disk_num_bytes(eb, fi));
161         printf("\t\textent data offset %llu nr %llu ram %llu\n",
162                 (unsigned long long)btrfs_file_extent_offset(eb, fi),
163                 (unsigned long long)btrfs_file_extent_num_bytes(eb, fi),
164                 (unsigned long long)btrfs_file_extent_ram_bytes(eb, fi));
165         printf("\t\textent compression %d\n",
166                btrfs_file_extent_compression(eb, fi));
167 }
168
169 static void print_extent_item(struct extent_buffer *eb, int slot)
170 {
171         struct btrfs_extent_item *ei;
172         struct btrfs_extent_inline_ref *iref;
173         struct btrfs_extent_data_ref *dref;
174         struct btrfs_shared_data_ref *sref;
175         struct btrfs_disk_key key;
176         unsigned long end;
177         unsigned long ptr;
178         int type;
179         u32 item_size = btrfs_item_size_nr(eb, slot);
180         u64 flags;
181         u64 offset;
182
183         if (item_size < sizeof(*ei)) {
184 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
185                 struct btrfs_extent_item_v0 *ei0;
186                 BUG_ON(item_size != sizeof(*ei0));
187                 ei0 = btrfs_item_ptr(eb, slot, struct btrfs_extent_item_v0);
188                 printf("\t\textent refs %u\n",
189                        btrfs_extent_refs_v0(eb, ei0));
190                 return;
191 #else
192                 BUG();
193 #endif
194         }
195
196         ei = btrfs_item_ptr(eb, slot, struct btrfs_extent_item);
197         flags = btrfs_extent_flags(eb, ei);
198
199         printf("\t\textent refs %llu gen %llu flags %llu\n",
200                (unsigned long long)btrfs_extent_refs(eb, ei),
201                (unsigned long long)btrfs_extent_generation(eb, ei),
202                (unsigned long long)flags);
203
204         if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
205                 struct btrfs_tree_block_info *info;
206                 info = (struct btrfs_tree_block_info *)(ei + 1);
207                 btrfs_tree_block_key(eb, info, &key);
208                 printf("\t\ttree block key (%llu %x %llu) level %d\n",
209                        (unsigned long long)btrfs_disk_key_objectid(&key),
210                        key.type,
211                        (unsigned long long)btrfs_disk_key_offset(&key),
212                        btrfs_tree_block_level(eb, info));
213                 iref = (struct btrfs_extent_inline_ref *)(info + 1);
214         } else {
215                 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
216         }
217
218         ptr = (unsigned long)iref;
219         end = (unsigned long)ei + item_size;
220         while (ptr < end) {
221                 iref = (struct btrfs_extent_inline_ref *)ptr;
222                 type = btrfs_extent_inline_ref_type(eb, iref);
223                 offset = btrfs_extent_inline_ref_offset(eb, iref);
224                 switch (type) {
225                 case BTRFS_TREE_BLOCK_REF_KEY:
226                         printf("\t\ttree block backref root %llu\n",
227                                (unsigned long long)offset);
228                         break;
229                 case BTRFS_SHARED_BLOCK_REF_KEY:
230                         printf("\t\tshared block backref parent %llu\n",
231                                (unsigned long long)offset);
232                         break;
233                 case BTRFS_EXTENT_DATA_REF_KEY:
234                         dref = (struct btrfs_extent_data_ref *)(&iref->offset);
235                         printf("\t\textent data backref root %llu "
236                                "objectid %llu offset %llu count %u\n",
237                                (unsigned long long)btrfs_extent_data_ref_root(eb, dref),
238                                (unsigned long long)btrfs_extent_data_ref_objectid(eb, dref),
239                                (unsigned long long)btrfs_extent_data_ref_offset(eb, dref),
240                                btrfs_extent_data_ref_count(eb, dref));
241                         break;
242                 case BTRFS_SHARED_DATA_REF_KEY:
243                         sref = (struct btrfs_shared_data_ref *)(iref + 1);
244                         printf("\t\tshared data backref parent %llu count %u\n",
245                                (unsigned long long)offset,
246                                btrfs_shared_data_ref_count(eb, sref));
247                         break;
248                 default:
249                         return;
250                 }
251                 ptr += btrfs_extent_inline_ref_size(type);
252         }
253         WARN_ON(ptr > end);
254 }
255
256 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
257 static void print_extent_ref_v0(struct extent_buffer *eb, int slot)
258 {
259         struct btrfs_extent_ref_v0 *ref0;
260
261         ref0 = btrfs_item_ptr(eb, slot, struct btrfs_extent_ref_v0);
262         printf("\t\textent back ref root %llu gen %llu "
263                 "owner %llu num_refs %lu\n",
264                 (unsigned long long)btrfs_ref_root_v0(eb, ref0),
265                 (unsigned long long)btrfs_ref_generation_v0(eb, ref0),
266                 (unsigned long long)btrfs_ref_objectid_v0(eb, ref0),
267                 (unsigned long)btrfs_ref_count_v0(eb, ref0));
268 }
269 #endif
270
271 static void print_root_ref(struct extent_buffer *leaf, int slot, char *tag)
272 {
273         struct btrfs_root_ref *ref;
274         char namebuf[BTRFS_NAME_LEN];
275         int namelen;
276
277         ref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
278         namelen = btrfs_root_ref_name_len(leaf, ref);
279         read_extent_buffer(leaf, namebuf, (unsigned long)(ref + 1), namelen);
280         printf("\t\troot %s key dirid %llu sequence %llu name %.*s\n", tag,
281                (unsigned long long)btrfs_root_ref_dirid(leaf, ref),
282                (unsigned long long)btrfs_root_ref_sequence(leaf, ref),
283                namelen, namebuf);
284 }
285
286 static int count_bytes(void *buf, int len, char b)
287 {
288         int cnt = 0;
289         int i;
290         for (i = 0; i < len; i++) {
291                 if (((char*)buf)[i] == b)
292                         cnt++;
293         }
294         return cnt;
295 }
296
297 static void print_root(struct extent_buffer *leaf, int slot)
298 {
299         struct btrfs_root_item *ri;
300         struct btrfs_root_item root_item;
301         int len;
302         char uuid_str[128];
303
304         ri = btrfs_item_ptr(leaf, slot, struct btrfs_root_item);
305         len = btrfs_item_size_nr(leaf, slot);
306
307         memset(&root_item, 0, sizeof(root_item));
308         read_extent_buffer(leaf, &root_item, (unsigned long)ri, len);
309
310         printf("\t\troot data bytenr %llu level %d dirid %llu refs %u gen %llu\n",
311                 (unsigned long long)btrfs_root_bytenr(&root_item),
312                 btrfs_root_level(&root_item),
313                 (unsigned long long)btrfs_root_dirid(&root_item),
314                 btrfs_root_refs(&root_item),
315                 (unsigned long long)btrfs_root_generation(&root_item));
316
317         if (root_item.generation == root_item.generation_v2) {
318                 uuid_unparse(root_item.uuid, uuid_str);
319                 printf("\t\tuuid %s\n", uuid_str);
320                 if (count_bytes(root_item.parent_uuid, BTRFS_UUID_SIZE, 0) != BTRFS_UUID_SIZE) {
321                         uuid_unparse(root_item.parent_uuid, uuid_str);
322                         printf("\t\tparent_uuid %s\n", uuid_str);
323                 }
324                 if (count_bytes(root_item.received_uuid, BTRFS_UUID_SIZE, 0) != BTRFS_UUID_SIZE) {
325                         uuid_unparse(root_item.received_uuid, uuid_str);
326                         printf("\t\treceived_uuid %s\n", uuid_str);
327                 }
328                 if (root_item.ctransid) {
329                         printf("\t\tctransid %llu otransid %llu stransid %llu rtransid %llu\n",
330                                 btrfs_root_ctransid(&root_item),
331                                 btrfs_root_otransid(&root_item),
332                                 btrfs_root_stransid(&root_item),
333                                 btrfs_root_rtransid(&root_item));
334                 }
335         }
336         if (btrfs_root_refs(&root_item) == 0) {
337                 struct btrfs_key drop_key;
338                 btrfs_disk_key_to_cpu(&drop_key,
339                                       &root_item.drop_progress);
340                 printf("\t\tdrop ");
341                 btrfs_print_key(&root_item.drop_progress);
342                 printf(" level %d\n", root_item.drop_level);
343         }
344 }
345
346 static void print_free_space_header(struct extent_buffer *leaf, int slot)
347 {
348         struct btrfs_free_space_header *header;
349         struct btrfs_disk_key location;
350
351         header = btrfs_item_ptr(leaf, slot, struct btrfs_free_space_header);
352         btrfs_free_space_key(leaf, header, &location);
353         printf("\t\tlocation ");
354         btrfs_print_key(&location);
355         printf("\n");
356         printf("\t\tcache generation %llu entries %llu bitmaps %llu\n",
357                (unsigned long long)btrfs_free_space_generation(leaf, header),
358                (unsigned long long)btrfs_free_space_entries(leaf, header),
359                (unsigned long long)btrfs_free_space_bitmaps(leaf, header));
360 }
361
362 static void print_key_type(u64 objectid, u8 type)
363 {
364         if (type == 0 && objectid == BTRFS_FREE_SPACE_OBJECTID) {
365                 printf("UNTYPED");
366                 return;
367         }
368
369         switch (type) {
370         case BTRFS_INODE_ITEM_KEY:
371                 printf("INODE_ITEM");
372                 break;
373         case BTRFS_INODE_REF_KEY:
374                 printf("INODE_REF");
375                 break;
376         case BTRFS_DIR_ITEM_KEY:
377                 printf("DIR_ITEM");
378                 break;
379         case BTRFS_DIR_INDEX_KEY:
380                 printf("DIR_INDEX");
381                 break;
382         case BTRFS_DIR_LOG_ITEM_KEY:
383                 printf("DIR_LOG_ITEM");
384                 break;
385         case BTRFS_DIR_LOG_INDEX_KEY:
386                 printf("DIR_LOG_INDEX");
387                 break;
388         case BTRFS_XATTR_ITEM_KEY:
389                 printf("XATTR_ITEM");
390                 break;
391         case BTRFS_ORPHAN_ITEM_KEY:
392                 printf("ORPHAN_ITEM");
393                 break;
394         case BTRFS_ROOT_ITEM_KEY:
395                 printf("ROOT_ITEM");
396                 break;
397         case BTRFS_ROOT_REF_KEY:
398                 printf("ROOT_REF");
399                 break;
400         case BTRFS_ROOT_BACKREF_KEY:
401                 printf("ROOT_BACKREF");
402                 break;
403         case BTRFS_EXTENT_ITEM_KEY:
404                 printf("EXTENT_ITEM");
405                 break;
406         case BTRFS_TREE_BLOCK_REF_KEY:
407                 printf("TREE_BLOCK_REF");
408                 break;
409         case BTRFS_SHARED_BLOCK_REF_KEY:
410                 printf("SHARED_BLOCK_REF");
411                 break;
412         case BTRFS_EXTENT_DATA_REF_KEY:
413                 printf("EXTENT_DATA_REF");
414                 break;
415         case BTRFS_SHARED_DATA_REF_KEY:
416                 printf("SHARED_DATA_REF");
417                 break;
418         case BTRFS_EXTENT_REF_V0_KEY:
419                 printf("EXTENT_REF_V0");
420                 break;
421         case BTRFS_CSUM_ITEM_KEY:
422                 printf("CSUM_ITEM");
423                 break;
424         case BTRFS_EXTENT_CSUM_KEY:
425                 printf("EXTENT_CSUM");
426                 break;
427         case BTRFS_EXTENT_DATA_KEY:
428                 printf("EXTENT_DATA");
429                 break;
430         case BTRFS_BLOCK_GROUP_ITEM_KEY:
431                 printf("BLOCK_GROUP_ITEM");
432                 break;
433         case BTRFS_CHUNK_ITEM_KEY:
434                 printf("CHUNK_ITEM");
435                 break;
436         case BTRFS_DEV_ITEM_KEY:
437                 printf("DEV_ITEM");
438                 break;
439         case BTRFS_DEV_EXTENT_KEY:
440                 printf("DEV_EXTENT");
441                 break;
442         case BTRFS_BALANCE_ITEM_KEY:
443                 printf("BALANCE_ITEM");
444                 break;
445         case BTRFS_STRING_ITEM_KEY:
446                 printf("STRING_ITEM");
447                 break;
448         default:
449                 printf("UNKNOWN");
450         };
451 }
452
453 static void print_objectid(u64 objectid, u8 type)
454 {
455         if (type == BTRFS_DEV_EXTENT_KEY) {
456                 printf("%llu", (unsigned long long)objectid); /* device id */
457                 return;
458         }
459
460         switch (objectid) {
461         case BTRFS_ROOT_TREE_OBJECTID:
462                 if (type == BTRFS_DEV_ITEM_KEY)
463                         printf("DEV_ITEMS");
464                 else
465                         printf("ROOT_TREE");
466                 break;
467         case BTRFS_EXTENT_TREE_OBJECTID:
468                 printf("EXTENT_TREE");
469                 break;
470         case BTRFS_CHUNK_TREE_OBJECTID:
471                 printf("CHUNK_TREE");
472                 break;
473         case BTRFS_DEV_TREE_OBJECTID:
474                 printf("DEV_TREE");
475                 break;
476         case BTRFS_FS_TREE_OBJECTID:
477                 printf("FS_TREE");
478                 break;
479         case BTRFS_ROOT_TREE_DIR_OBJECTID:
480                 printf("ROOT_TREE_DIR");
481                 break;
482         case BTRFS_CSUM_TREE_OBJECTID:
483                 printf("CSUM_TREE");
484                 break;
485         case BTRFS_BALANCE_OBJECTID:
486                 printf("BALANCE");
487                 break;
488         case BTRFS_ORPHAN_OBJECTID:
489                 printf("ORPHAN");
490                 break;
491         case BTRFS_TREE_LOG_OBJECTID:
492                 printf("TREE_LOG");
493                 break;
494         case BTRFS_TREE_LOG_FIXUP_OBJECTID:
495                 printf("LOG_FIXUP");
496                 break;
497         case BTRFS_TREE_RELOC_OBJECTID:
498                 printf("TREE_RELOC");
499                 break;
500         case BTRFS_DATA_RELOC_TREE_OBJECTID:
501                 printf("DATA_RELOC_TREE");
502                 break;
503         case BTRFS_EXTENT_CSUM_OBJECTID:
504                 printf("EXTENT_CSUM");
505                 break;
506         case BTRFS_FREE_SPACE_OBJECTID:
507                 printf("FREE_SPACE");
508                 break;
509         case BTRFS_FREE_INO_OBJECTID:
510                 printf("FREE_INO");
511                 break;
512         case BTRFS_MULTIPLE_OBJECTIDS:
513                 printf("MULTIPLE");
514                 break;
515         case BTRFS_FIRST_CHUNK_TREE_OBJECTID:
516                 if (type == BTRFS_CHUNK_ITEM_KEY) {
517                         printf("FIRST_CHUNK_TREE");
518                         break;
519                 }
520                 /* fall-thru */
521         default:
522                 printf("%llu", (unsigned long long)objectid);
523         }
524 }
525
526 void btrfs_print_key(struct btrfs_disk_key *disk_key)
527 {
528         u64 objectid = btrfs_disk_key_objectid(disk_key);
529         u8 type = btrfs_disk_key_type(disk_key);
530
531         printf("key (");
532         print_objectid(objectid, type);
533         printf(" ");
534         print_key_type(objectid, type);
535         printf(" %llu)", (unsigned long long)btrfs_disk_key_offset(disk_key));
536 }
537
538 void btrfs_print_leaf(struct btrfs_root *root, struct extent_buffer *l)
539 {
540         int i;
541         char *str;
542         struct btrfs_item *item;
543         struct btrfs_dir_item *di;
544         struct btrfs_inode_item *ii;
545         struct btrfs_file_extent_item *fi;
546         struct btrfs_block_group_item *bi;
547         struct btrfs_extent_data_ref *dref;
548         struct btrfs_shared_data_ref *sref;
549         struct btrfs_inode_ref *iref;
550         struct btrfs_dev_extent *dev_extent;
551         struct btrfs_disk_key disk_key;
552         struct btrfs_block_group_item bg_item;
553         struct btrfs_dir_log_item *dlog;
554         u32 nr = btrfs_header_nritems(l);
555         u64 objectid;
556         u32 type;
557
558         printf("leaf %llu items %d free space %d generation %llu owner %llu\n",
559                 (unsigned long long)btrfs_header_bytenr(l), nr,
560                 btrfs_leaf_free_space(root, l),
561                 (unsigned long long)btrfs_header_generation(l),
562                 (unsigned long long)btrfs_header_owner(l));
563         print_uuids(l);
564         fflush(stdout);
565         for (i = 0 ; i < nr ; i++) {
566                 item = btrfs_item_nr(l, i);
567                 btrfs_item_key(l, &disk_key, i);
568                 objectid = btrfs_disk_key_objectid(&disk_key);
569                 type = btrfs_disk_key_type(&disk_key);
570                 printf("\titem %d ", i);
571                 btrfs_print_key(&disk_key);
572                 printf(" itemoff %d itemsize %d\n",
573                         btrfs_item_offset(l, item),
574                         btrfs_item_size(l, item));
575
576                 if (type == 0 && objectid == BTRFS_FREE_SPACE_OBJECTID)
577                         print_free_space_header(l, i);
578
579                 switch (type) {
580                 case BTRFS_INODE_ITEM_KEY:
581                         ii = btrfs_item_ptr(l, i, struct btrfs_inode_item);
582                         printf("\t\tinode generation %llu transid %llu size %llu block group %llu mode %o links %u\n",
583                                (unsigned long long)btrfs_inode_generation(l, ii),
584                                (unsigned long long)btrfs_inode_transid(l, ii),
585                                (unsigned long long)btrfs_inode_size(l, ii),
586                                (unsigned long long)btrfs_inode_block_group(l,ii),
587                                btrfs_inode_mode(l, ii),
588                                btrfs_inode_nlink(l, ii));
589                         break;
590                 case BTRFS_INODE_REF_KEY:
591                         iref = btrfs_item_ptr(l, i, struct btrfs_inode_ref);
592                         print_inode_ref_item(l, item, iref);
593                         break;
594                 case BTRFS_DIR_ITEM_KEY:
595                 case BTRFS_DIR_INDEX_KEY:
596                 case BTRFS_XATTR_ITEM_KEY:
597                         di = btrfs_item_ptr(l, i, struct btrfs_dir_item);
598                         print_dir_item(l, item, di);
599                         break;
600                 case BTRFS_DIR_LOG_INDEX_KEY:
601                 case BTRFS_DIR_LOG_ITEM_KEY:
602                         dlog = btrfs_item_ptr(l, i, struct btrfs_dir_log_item);
603                         printf("\t\tdir log end %Lu\n",
604                                (unsigned long long)btrfs_dir_log_end(l, dlog));
605                        break;
606                 case BTRFS_ORPHAN_ITEM_KEY:
607                         printf("\t\torphan item\n");
608                         break;
609                 case BTRFS_ROOT_ITEM_KEY:
610                         print_root(l, i);
611                         break;
612                 case BTRFS_ROOT_REF_KEY:
613                         print_root_ref(l, i, "ref");
614                         break;
615                 case BTRFS_ROOT_BACKREF_KEY:
616                         print_root_ref(l, i, "backref");
617                         break;
618                 case BTRFS_EXTENT_ITEM_KEY:
619                         print_extent_item(l, i);
620                         break;
621                 case BTRFS_TREE_BLOCK_REF_KEY:
622                         printf("\t\ttree block backref\n");
623                         break;
624                 case BTRFS_SHARED_BLOCK_REF_KEY:
625                         printf("\t\tshared block backref\n");
626                         break;
627                 case BTRFS_EXTENT_DATA_REF_KEY:
628                         dref = btrfs_item_ptr(l, i, struct btrfs_extent_data_ref);
629                         printf("\t\textent data backref root %llu "
630                                "objectid %llu offset %llu count %u\n",
631                                (unsigned long long)btrfs_extent_data_ref_root(l, dref),
632                                (unsigned long long)btrfs_extent_data_ref_objectid(l, dref),
633                                (unsigned long long)btrfs_extent_data_ref_offset(l, dref),
634                                btrfs_extent_data_ref_count(l, dref));
635                         break;
636                 case BTRFS_SHARED_DATA_REF_KEY:
637                         sref = btrfs_item_ptr(l, i, struct btrfs_shared_data_ref);
638                         printf("\t\tshared data backref count %u\n",
639                                btrfs_shared_data_ref_count(l, sref));
640                         break;
641                 case BTRFS_EXTENT_REF_V0_KEY:
642 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
643                         print_extent_ref_v0(l, i);
644 #else
645                         BUG();
646 #endif
647                         break;
648                 case BTRFS_CSUM_ITEM_KEY:
649                         printf("\t\tcsum item\n");
650                         break;
651                 case BTRFS_EXTENT_CSUM_KEY:
652                         printf("\t\textent csum item\n");
653                         break;
654                 case BTRFS_EXTENT_DATA_KEY:
655                         fi = btrfs_item_ptr(l, i,
656                                             struct btrfs_file_extent_item);
657                         print_file_extent_item(l, item, fi);
658                         break;
659                 case BTRFS_BLOCK_GROUP_ITEM_KEY:
660                         bi = btrfs_item_ptr(l, i,
661                                             struct btrfs_block_group_item);
662                         read_extent_buffer(l, &bg_item, (unsigned long)bi,
663                                            sizeof(bg_item));
664                         printf("\t\tblock group used %llu chunk_objectid %llu flags %llu\n",
665                                (unsigned long long)btrfs_block_group_used(&bg_item),
666                                (unsigned long long)btrfs_block_group_chunk_objectid(&bg_item),
667                                (unsigned long long)btrfs_block_group_flags(&bg_item));
668                         break;
669                 case BTRFS_CHUNK_ITEM_KEY:
670                         print_chunk(l, btrfs_item_ptr(l, i, struct btrfs_chunk));
671                         break;
672                 case BTRFS_DEV_ITEM_KEY:
673                         print_dev_item(l, btrfs_item_ptr(l, i,
674                                         struct btrfs_dev_item));
675                         break;
676                 case BTRFS_DEV_EXTENT_KEY:
677                         dev_extent = btrfs_item_ptr(l, i,
678                                                     struct btrfs_dev_extent);
679                         printf("\t\tdev extent chunk_tree %llu\n"
680                                "\t\tchunk objectid %llu chunk offset %llu "
681                                "length %llu\n",
682                                (unsigned long long)
683                                btrfs_dev_extent_chunk_tree(l, dev_extent),
684                                (unsigned long long)
685                                btrfs_dev_extent_chunk_objectid(l, dev_extent),
686                                (unsigned long long)
687                                btrfs_dev_extent_chunk_offset(l, dev_extent),
688                                (unsigned long long)
689                                btrfs_dev_extent_length(l, dev_extent));
690                         break;
691                 case BTRFS_STRING_ITEM_KEY:
692                         /* dirty, but it's simple */
693                         str = l->data + btrfs_item_ptr_offset(l, i);
694                         printf("\t\titem data %.*s\n", btrfs_item_size(l, item), str);
695                         break;
696                 };
697                 fflush(stdout);
698         }
699 }
700
701 void btrfs_print_tree(struct btrfs_root *root, struct extent_buffer *eb, int follow)
702 {
703         int i;
704         u32 nr;
705         u32 size;
706         struct btrfs_disk_key disk_key;
707         struct btrfs_key key;
708
709         if (!eb)
710                 return;
711         nr = btrfs_header_nritems(eb);
712         if (btrfs_is_leaf(eb)) {
713                 btrfs_print_leaf(root, eb);
714                 return;
715         }
716         printf("node %llu level %d items %d free %u generation %llu owner %llu\n",
717                (unsigned long long)eb->start,
718                 btrfs_header_level(eb), nr,
719                 (u32)BTRFS_NODEPTRS_PER_BLOCK(root) - nr,
720                 (unsigned long long)btrfs_header_generation(eb),
721                 (unsigned long long)btrfs_header_owner(eb));
722         print_uuids(eb);
723         fflush(stdout);
724         size = btrfs_level_size(root, btrfs_header_level(eb) - 1);
725         for (i = 0; i < nr; i++) {
726                 u64 blocknr = btrfs_node_blockptr(eb, i);
727                 btrfs_node_key(eb, &disk_key, i);
728                 btrfs_disk_key_to_cpu(&key, &disk_key);
729                 printf("\t");
730                 btrfs_print_key(&disk_key);
731                 printf(" block %llu (%llu) gen %llu\n",
732                        (unsigned long long)blocknr,
733                        (unsigned long long)blocknr / size,
734                        (unsigned long long)btrfs_node_ptr_generation(eb, i));
735                 fflush(stdout);
736         }
737         if (!follow)
738                 return;
739
740         for (i = 0; i < nr; i++) {
741                 struct extent_buffer *next = read_tree_block(root,
742                                              btrfs_node_blockptr(eb, i),
743                                              size,
744                                              btrfs_node_ptr_generation(eb, i));
745                 if (!next) {
746                         fprintf(stderr, "failed to read %llu in tree %llu\n",
747                                 (unsigned long long)btrfs_node_blockptr(eb, i),
748                                 (unsigned long long)btrfs_header_owner(eb));
749                         continue;
750                 }
751                 if (btrfs_is_leaf(next) &&
752                     btrfs_header_level(eb) != 1)
753                         BUG();
754                 if (btrfs_header_level(next) !=
755                         btrfs_header_level(eb) - 1)
756                         BUG();
757                 btrfs_print_tree(root, next, 1);
758                 free_extent_buffer(next);
759         }
760 }