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