btrfs-progs: test: fix name generation not to contain trailing spaces
[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         static const char* dir_item_str[] = {
35                 [BTRFS_FT_REG_FILE]     = "FILE",
36                 [BTRFS_FT_DIR]          = "DIR",
37                 [BTRFS_FT_CHRDEV]       = "CHRDEV",
38                 [BTRFS_FT_BLKDEV]       = "BLKDEV",
39                 [BTRFS_FT_FIFO]         = "FIFO",
40                 [BTRFS_FT_SOCK]         = "SOCK",
41                 [BTRFS_FT_SYMLINK]      = "SYMLINK",
42                 [BTRFS_FT_XATTR]        = "XATTR"
43         };
44
45         if (type < ARRAY_SIZE(dir_item_str) && dir_item_str[type])
46                 printf("%s", dir_item_str[type]);
47         else
48                 printf("DIR_ITEM.%u", type);
49 }
50
51 static void print_dir_item(struct extent_buffer *eb, u32 size,
52                           struct btrfs_dir_item *di)
53 {
54         u32 cur = 0;
55         u32 len;
56         u32 name_len;
57         u32 data_len;
58         char namebuf[BTRFS_NAME_LEN];
59         struct btrfs_disk_key location;
60
61         while (cur < size) {
62                 btrfs_dir_item_key(eb, di, &location);
63                 printf("\t\tlocation ");
64                 btrfs_print_key(&location);
65                 printf(" type ");
66                 print_dir_item_type(eb, di);
67                 printf("\n");
68                 name_len = btrfs_dir_name_len(eb, di);
69                 data_len = btrfs_dir_data_len(eb, di);
70                 len = (name_len <= sizeof(namebuf))? name_len: sizeof(namebuf);
71                 read_extent_buffer(eb, namebuf, (unsigned long)(di + 1), len);
72                 printf("\t\ttransid %llu data_len %u name_len %u\n",
73                                 btrfs_dir_transid(eb, di),
74                                 data_len, name_len);
75                 printf("\t\tname: %.*s\n", len, namebuf);
76                 if (data_len) {
77                         len = (data_len <= sizeof(namebuf))? data_len: sizeof(namebuf);
78                         read_extent_buffer(eb, namebuf,
79                                 (unsigned long)(di + 1) + name_len, len);
80                         printf("\t\tdata %.*s\n", len, namebuf);
81                 }
82                 len = sizeof(*di) + name_len + data_len;
83                 di = (struct btrfs_dir_item *)((char *)di + len);
84                 cur += len;
85         }
86 }
87
88 static void print_inode_extref_item(struct extent_buffer *eb, u32 size,
89                 struct btrfs_inode_extref *extref)
90 {
91         u32 cur = 0;
92         u32 len;
93         u32 name_len = 0;
94         u64 index = 0;
95         u64 parent_objid;
96         char namebuf[BTRFS_NAME_LEN];
97
98         while (cur < size) {
99                 index = btrfs_inode_extref_index(eb, extref);
100                 name_len = btrfs_inode_extref_name_len(eb, extref);
101                 parent_objid = btrfs_inode_extref_parent(eb, extref);
102
103                 len = (name_len <= sizeof(namebuf))? name_len: sizeof(namebuf);
104
105                 read_extent_buffer(eb, namebuf, (unsigned long)(extref->name), len);
106
107                 printf("\t\tindex %llu parent %llu namelen %u name: %.*s\n",
108                        (unsigned long long)index,
109                        (unsigned long long)parent_objid,
110                        name_len, len, namebuf);
111
112                 len = sizeof(*extref) + name_len;
113                 extref = (struct btrfs_inode_extref *)((char *)extref + len);
114                 cur += len;
115         }
116 }
117
118 static void print_inode_ref_item(struct extent_buffer *eb, u32 size,
119                                 struct btrfs_inode_ref *ref)
120 {
121         u32 cur = 0;
122         u32 len;
123         u32 name_len;
124         u64 index;
125         char namebuf[BTRFS_NAME_LEN];
126
127         while (cur < size) {
128                 name_len = btrfs_inode_ref_name_len(eb, ref);
129                 index = btrfs_inode_ref_index(eb, ref);
130                 len = (name_len <= sizeof(namebuf))? name_len: sizeof(namebuf);
131                 read_extent_buffer(eb, namebuf, (unsigned long)(ref + 1), len);
132                 printf("\t\tindex %llu namelen %u name: %.*s\n",
133                        (unsigned long long)index, name_len, len, namebuf);
134                 len = sizeof(*ref) + name_len;
135                 ref = (struct btrfs_inode_ref *)((char *)ref + len);
136                 cur += len;
137         }
138 }
139
140 /* Caller should ensure sizeof(*ret)>=21 "DATA|METADATA|RAID10" */
141 static void bg_flags_to_str(u64 flags, char *ret)
142 {
143         int empty = 1;
144
145         if (flags & BTRFS_BLOCK_GROUP_DATA) {
146                 empty = 0;
147                 strcpy(ret, "DATA");
148         }
149         if (flags & BTRFS_BLOCK_GROUP_METADATA) {
150                 if (!empty)
151                         strcat(ret, "|");
152                 strcat(ret, "METADATA");
153         }
154         if (flags & BTRFS_BLOCK_GROUP_SYSTEM) {
155                 if (!empty)
156                         strcat(ret, "|");
157                 strcat(ret, "SYSTEM");
158         }
159         switch (flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
160         case BTRFS_BLOCK_GROUP_RAID0:
161                 strcat(ret, "|RAID0");
162                 break;
163         case BTRFS_BLOCK_GROUP_RAID1:
164                 strcat(ret, "|RAID1");
165                 break;
166         case BTRFS_BLOCK_GROUP_DUP:
167                 strcat(ret, "|DUP");
168                 break;
169         case BTRFS_BLOCK_GROUP_RAID10:
170                 strcat(ret, "|RAID10");
171                 break;
172         case BTRFS_BLOCK_GROUP_RAID5:
173                 strcat(ret, "|RAID5");
174                 break;
175         case BTRFS_BLOCK_GROUP_RAID6:
176                 strcat(ret, "|RAID6");
177                 break;
178         default:
179                 break;
180         }
181 }
182
183 /* Caller should ensure sizeof(*ret)>= 26 "OFF|SCANNING|INCONSISTENT" */
184 static void qgroup_flags_to_str(u64 flags, char *ret)
185 {
186         if (flags & BTRFS_QGROUP_STATUS_FLAG_ON)
187                 strcpy(ret, "ON");
188         else
189                 strcpy(ret, "OFF");
190
191         if (flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
192                 strcat(ret, "|SCANNING");
193         if (flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT)
194                 strcat(ret, "|INCONSISTENT");
195 }
196
197 void print_chunk_item(struct extent_buffer *eb, struct btrfs_chunk *chunk)
198 {
199         u16 num_stripes = btrfs_chunk_num_stripes(eb, chunk);
200         int i;
201         u32 chunk_item_size = btrfs_chunk_item_size(num_stripes);
202         char chunk_flags_str[32] = {0};
203
204         if ((unsigned long)chunk + chunk_item_size > eb->len) {
205                 printf("\t\tchunk item invalid\n");
206                 return;
207         }
208
209         bg_flags_to_str(btrfs_chunk_type(eb, chunk), chunk_flags_str);
210         printf("\t\tlength %llu owner %llu stripe_len %llu type %s\n",
211                (unsigned long long)btrfs_chunk_length(eb, chunk),
212                (unsigned long long)btrfs_chunk_owner(eb, chunk),
213                (unsigned long long)btrfs_chunk_stripe_len(eb, chunk),
214                 chunk_flags_str);
215         printf("\t\tio_align %u io_width %u sector_size %u\n",
216                         btrfs_chunk_io_align(eb, chunk),
217                         btrfs_chunk_io_width(eb, chunk),
218                         btrfs_chunk_sector_size(eb, chunk));
219         printf("\t\tnum_stripes %hu sub_stripes %hu\n", num_stripes,
220                         btrfs_chunk_sub_stripes(eb, chunk));
221         for (i = 0 ; i < num_stripes ; i++) {
222                 unsigned char dev_uuid[BTRFS_UUID_SIZE];
223                 char str_dev_uuid[BTRFS_UUID_UNPARSED_SIZE];
224                 u64 uuid_offset;
225                 u64 stripe_offset;
226
227                 uuid_offset = (unsigned long)btrfs_stripe_dev_uuid_nr(chunk, i);
228                 stripe_offset = (unsigned long)btrfs_stripe_nr(chunk, i);
229
230                 if (uuid_offset < stripe_offset ||
231                         (uuid_offset + BTRFS_UUID_SIZE) >
232                                 (stripe_offset + sizeof(struct btrfs_stripe))) {
233                         printf("\t\t\tstripe %d invalid\n", i);
234                         break;
235                 }
236
237                 read_extent_buffer(eb, dev_uuid,
238                         uuid_offset,
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 uuid_str[BTRFS_UUID_UNPARSED_SIZE];
252         char fsid_str[BTRFS_UUID_UNPARSED_SIZE];
253         u8 uuid[BTRFS_UUID_SIZE];
254         u8 fsid[BTRFS_UUID_SIZE];
255
256         read_extent_buffer(eb, uuid,
257                            (unsigned long)btrfs_device_uuid(dev_item),
258                            BTRFS_UUID_SIZE);
259         uuid_unparse(uuid, uuid_str);
260         read_extent_buffer(eb, fsid,
261                            (unsigned long)btrfs_device_fsid(dev_item),
262                            BTRFS_UUID_SIZE);
263         uuid_unparse(fsid, fsid_str);
264         printf("\t\tdevid %llu total_bytes %llu bytes_used %Lu\n"
265                "\t\tio_align %u io_width %u sector_size %u type %llu\n"
266                "\t\tgeneration %llu start_offset %llu dev_group %u\n"
267                "\t\tseek_speed %hhu bandwidth %hhu\n"
268                "\t\tuuid %s\n"
269                "\t\tfsid %s\n",
270                (unsigned long long)btrfs_device_id(eb, dev_item),
271                (unsigned long long)btrfs_device_total_bytes(eb, dev_item),
272                (unsigned long long)btrfs_device_bytes_used(eb, dev_item),
273                btrfs_device_io_align(eb, dev_item),
274                btrfs_device_io_width(eb, dev_item),
275                btrfs_device_sector_size(eb, dev_item),
276                (unsigned long long)btrfs_device_type(eb, dev_item),
277                (unsigned long long)btrfs_device_generation(eb, dev_item),
278                (unsigned long long)btrfs_device_start_offset(eb, dev_item),
279                btrfs_device_group(eb, dev_item),
280                btrfs_device_seek_speed(eb, dev_item),
281                btrfs_device_bandwidth(eb, dev_item),
282                uuid_str, fsid_str);
283 }
284
285 static void print_uuids(struct extent_buffer *eb)
286 {
287         char fs_uuid[BTRFS_UUID_UNPARSED_SIZE];
288         char chunk_uuid[BTRFS_UUID_UNPARSED_SIZE];
289         u8 disk_uuid[BTRFS_UUID_SIZE];
290
291         read_extent_buffer(eb, disk_uuid, btrfs_header_fsid(),
292                            BTRFS_FSID_SIZE);
293
294         fs_uuid[BTRFS_UUID_UNPARSED_SIZE - 1] = '\0';
295         uuid_unparse(disk_uuid, fs_uuid);
296
297         read_extent_buffer(eb, disk_uuid,
298                            btrfs_header_chunk_tree_uuid(eb),
299                            BTRFS_UUID_SIZE);
300
301         chunk_uuid[BTRFS_UUID_UNPARSED_SIZE - 1] = '\0';
302         uuid_unparse(disk_uuid, chunk_uuid);
303         printf("fs uuid %s\nchunk uuid %s\n", fs_uuid, chunk_uuid);
304 }
305
306 static void compress_type_to_str(u8 compress_type, char *ret)
307 {
308         switch (compress_type) {
309         case BTRFS_COMPRESS_NONE:
310                 strcpy(ret, "none");
311                 break;
312         case BTRFS_COMPRESS_ZLIB:
313                 strcpy(ret, "zlib");
314                 break;
315         case BTRFS_COMPRESS_LZO:
316                 strcpy(ret, "lzo");
317                 break;
318         default:
319                 sprintf(ret, "UNKNOWN.%d", compress_type);
320         }
321 }
322
323 static const char* file_extent_type_to_str(u8 type)
324 {
325         switch (type) {
326         case BTRFS_FILE_EXTENT_INLINE: return "inline";
327         case BTRFS_FILE_EXTENT_PREALLOC: return "prealloc";
328         case BTRFS_FILE_EXTENT_REG: return "regular";
329         default: return "unknown";
330         }
331 }
332
333 static void print_file_extent_item(struct extent_buffer *eb,
334                                    struct btrfs_item *item,
335                                    int slot,
336                                    struct btrfs_file_extent_item *fi)
337 {
338         unsigned char extent_type = btrfs_file_extent_type(eb, fi);
339         char compress_str[16];
340
341         compress_type_to_str(btrfs_file_extent_compression(eb, fi),
342                              compress_str);
343
344         printf("\t\tgeneration %llu type %hhu (%s)\n",
345                         btrfs_file_extent_generation(eb, fi),
346                         extent_type, file_extent_type_to_str(extent_type));
347
348         if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
349                 printf("\t\tinline extent data size %u ram_bytes %u compression %hhu (%s)\n",
350                                 btrfs_file_extent_inline_item_len(eb, item),
351                                 btrfs_file_extent_inline_len(eb, slot, fi),
352                                 btrfs_file_extent_compression(eb, fi),
353                                 compress_str);
354                 return;
355         }
356         if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
357                 printf("\t\tprealloc data disk byte %llu nr %llu\n",
358                   (unsigned long long)btrfs_file_extent_disk_bytenr(eb, fi),
359                   (unsigned long long)btrfs_file_extent_disk_num_bytes(eb, fi));
360                 printf("\t\tprealloc data offset %llu nr %llu\n",
361                   (unsigned long long)btrfs_file_extent_offset(eb, fi),
362                   (unsigned long long)btrfs_file_extent_num_bytes(eb, fi));
363                 return;
364         }
365         printf("\t\textent data disk byte %llu nr %llu\n",
366                 (unsigned long long)btrfs_file_extent_disk_bytenr(eb, fi),
367                 (unsigned long long)btrfs_file_extent_disk_num_bytes(eb, fi));
368         printf("\t\textent data offset %llu nr %llu ram %llu\n",
369                 (unsigned long long)btrfs_file_extent_offset(eb, fi),
370                 (unsigned long long)btrfs_file_extent_num_bytes(eb, fi),
371                 (unsigned long long)btrfs_file_extent_ram_bytes(eb, fi));
372         printf("\t\textent compression %hhu (%s)\n",
373                         btrfs_file_extent_compression(eb, fi),
374                         compress_str);
375 }
376
377 /* Caller should ensure sizeof(*ret) >= 16("DATA|TREE_BLOCK") */
378 static void extent_flags_to_str(u64 flags, char *ret)
379 {
380         int empty = 1;
381
382         if (flags & BTRFS_EXTENT_FLAG_DATA) {
383                 empty = 0;
384                 strcpy(ret, "DATA");
385         }
386         if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
387                 if (!empty) {
388                         empty = 0;
389                         strcat(ret, "|");
390                 }
391                 strcat(ret, "TREE_BLOCK");
392         }
393         if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
394                 strcat(ret, "|");
395                 strcat(ret, "FULL_BACKREF");
396         }
397 }
398
399 void print_extent_item(struct extent_buffer *eb, int slot, int metadata)
400 {
401         struct btrfs_extent_item *ei;
402         struct btrfs_extent_inline_ref *iref;
403         struct btrfs_extent_data_ref *dref;
404         struct btrfs_shared_data_ref *sref;
405         struct btrfs_disk_key key;
406         unsigned long end;
407         unsigned long ptr;
408         int type;
409         u32 item_size = btrfs_item_size_nr(eb, slot);
410         u64 flags;
411         u64 offset;
412         char flags_str[32] = {0};
413
414         if (item_size < sizeof(*ei)) {
415 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
416                 struct btrfs_extent_item_v0 *ei0;
417                 BUG_ON(item_size != sizeof(*ei0));
418                 ei0 = btrfs_item_ptr(eb, slot, struct btrfs_extent_item_v0);
419                 printf("\t\trefs %u\n",
420                        btrfs_extent_refs_v0(eb, ei0));
421                 return;
422 #else
423                 BUG();
424 #endif
425         }
426
427         ei = btrfs_item_ptr(eb, slot, struct btrfs_extent_item);
428         flags = btrfs_extent_flags(eb, ei);
429         extent_flags_to_str(flags, flags_str);
430
431         printf("\t\trefs %llu gen %llu flags %s\n",
432                (unsigned long long)btrfs_extent_refs(eb, ei),
433                (unsigned long long)btrfs_extent_generation(eb, ei),
434                flags_str);
435
436         if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !metadata) {
437                 struct btrfs_tree_block_info *info;
438                 info = (struct btrfs_tree_block_info *)(ei + 1);
439                 btrfs_tree_block_key(eb, info, &key);
440                 printf("\t\ttree block ");
441                 btrfs_print_key(&key);
442                 printf(" level %d\n", btrfs_tree_block_level(eb, info));
443                 iref = (struct btrfs_extent_inline_ref *)(info + 1);
444         } else if (metadata) {
445                 struct btrfs_key tmp;
446
447                 btrfs_item_key_to_cpu(eb, &tmp, slot);
448                 printf("\t\ttree block skinny level %d\n", (int)tmp.offset);
449                 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
450         } else{
451                 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
452         }
453
454         ptr = (unsigned long)iref;
455         end = (unsigned long)ei + item_size;
456         while (ptr < end) {
457                 iref = (struct btrfs_extent_inline_ref *)ptr;
458                 type = btrfs_extent_inline_ref_type(eb, iref);
459                 offset = btrfs_extent_inline_ref_offset(eb, iref);
460                 switch (type) {
461                 case BTRFS_TREE_BLOCK_REF_KEY:
462                         printf("\t\ttree block backref root %llu\n",
463                                (unsigned long long)offset);
464                         break;
465                 case BTRFS_SHARED_BLOCK_REF_KEY:
466                         printf("\t\tshared block backref parent %llu\n",
467                                (unsigned long long)offset);
468                         break;
469                 case BTRFS_EXTENT_DATA_REF_KEY:
470                         dref = (struct btrfs_extent_data_ref *)(&iref->offset);
471                         printf("\t\textent data backref root %llu "
472                                "objectid %llu offset %llu count %u\n",
473                                (unsigned long long)btrfs_extent_data_ref_root(eb, dref),
474                                (unsigned long long)btrfs_extent_data_ref_objectid(eb, dref),
475                                (unsigned long long)btrfs_extent_data_ref_offset(eb, dref),
476                                btrfs_extent_data_ref_count(eb, dref));
477                         break;
478                 case BTRFS_SHARED_DATA_REF_KEY:
479                         sref = (struct btrfs_shared_data_ref *)(iref + 1);
480                         printf("\t\tshared data backref parent %llu count %u\n",
481                                (unsigned long long)offset,
482                                btrfs_shared_data_ref_count(eb, sref));
483                         break;
484                 default:
485                         return;
486                 }
487                 ptr += btrfs_extent_inline_ref_size(type);
488         }
489         WARN_ON(ptr > end);
490 }
491
492 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
493 static void print_extent_ref_v0(struct extent_buffer *eb, int slot)
494 {
495         struct btrfs_extent_ref_v0 *ref0;
496
497         ref0 = btrfs_item_ptr(eb, slot, struct btrfs_extent_ref_v0);
498         printf("\t\textent back ref root %llu gen %llu "
499                 "owner %llu num_refs %lu\n",
500                 (unsigned long long)btrfs_ref_root_v0(eb, ref0),
501                 (unsigned long long)btrfs_ref_generation_v0(eb, ref0),
502                 (unsigned long long)btrfs_ref_objectid_v0(eb, ref0),
503                 (unsigned long)btrfs_ref_count_v0(eb, ref0));
504 }
505 #endif
506
507 static void print_root_ref(struct extent_buffer *leaf, int slot, const char *tag)
508 {
509         struct btrfs_root_ref *ref;
510         char namebuf[BTRFS_NAME_LEN];
511         int namelen;
512
513         ref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
514         namelen = btrfs_root_ref_name_len(leaf, ref);
515         read_extent_buffer(leaf, namebuf, (unsigned long)(ref + 1), namelen);
516         printf("\t\troot %s key dirid %llu sequence %llu name %.*s\n", tag,
517                (unsigned long long)btrfs_root_ref_dirid(leaf, ref),
518                (unsigned long long)btrfs_root_ref_sequence(leaf, ref),
519                namelen, namebuf);
520 }
521
522 static int empty_uuid(const u8 *uuid)
523 {
524         int i;
525
526         for (i = 0; i < BTRFS_UUID_SIZE; i++)
527                 if (uuid[i])
528                         return 0;
529         return 1;
530 }
531
532 /*
533  * Caller must ensure sizeof(*ret) >= 7 "RDONLY"
534  */
535 static void root_flags_to_str(u64 flags, char *ret)
536 {
537         if (flags & BTRFS_ROOT_SUBVOL_RDONLY)
538                 strcat(ret, "RDONLY");
539         else
540                 strcat(ret, "none");
541 }
542
543 static void print_root_item(struct extent_buffer *leaf, int slot)
544 {
545         struct btrfs_root_item *ri;
546         struct btrfs_root_item root_item;
547         int len;
548         char uuid_str[BTRFS_UUID_UNPARSED_SIZE];
549         char flags_str[32] = {0};
550         struct btrfs_key drop_key;
551
552         ri = btrfs_item_ptr(leaf, slot, struct btrfs_root_item);
553         len = btrfs_item_size_nr(leaf, slot);
554
555         memset(&root_item, 0, sizeof(root_item));
556         read_extent_buffer(leaf, &root_item, (unsigned long)ri, len);
557         root_flags_to_str(btrfs_root_flags(&root_item), flags_str);
558
559         printf("\t\tgeneration %llu root_dirid %llu bytenr %llu level %hhu refs %u\n",
560                 (unsigned long long)btrfs_root_generation(&root_item),
561                 (unsigned long long)btrfs_root_dirid(&root_item),
562                 (unsigned long long)btrfs_root_bytenr(&root_item),
563                 btrfs_root_level(&root_item),
564                 btrfs_root_refs(&root_item));
565         printf("\t\tlastsnap %llu byte_limit %llu bytes_used %llu flags 0x%llx(%s)\n",
566                 (unsigned long long)btrfs_root_last_snapshot(&root_item),
567                 (unsigned long long)btrfs_root_limit(&root_item),
568                 (unsigned long long)btrfs_root_used(&root_item),
569                 (unsigned long long)btrfs_root_flags(&root_item),
570                 flags_str);
571
572         if (root_item.generation == root_item.generation_v2) {
573                 uuid_unparse(root_item.uuid, uuid_str);
574                 printf("\t\tuuid %s\n", uuid_str);
575                 if (!empty_uuid(root_item.parent_uuid)) {
576                         uuid_unparse(root_item.parent_uuid, uuid_str);
577                         printf("\t\tparent_uuid %s\n", uuid_str);
578                 }
579                 if (!empty_uuid(root_item.received_uuid)) {
580                         uuid_unparse(root_item.received_uuid, uuid_str);
581                         printf("\t\treceived_uuid %s\n", uuid_str);
582                 }
583                 if (root_item.ctransid) {
584                         printf("\t\tctransid %llu otransid %llu stransid %llu rtransid %llu\n",
585                                 btrfs_root_ctransid(&root_item),
586                                 btrfs_root_otransid(&root_item),
587                                 btrfs_root_stransid(&root_item),
588                                 btrfs_root_rtransid(&root_item));
589                 }
590         }
591
592         btrfs_disk_key_to_cpu(&drop_key, &root_item.drop_progress);
593         printf("\t\tdrop ");
594         btrfs_print_key(&root_item.drop_progress);
595         printf(" level %hhu\n", root_item.drop_level);
596 }
597
598 static void print_free_space_header(struct extent_buffer *leaf, int slot)
599 {
600         struct btrfs_free_space_header *header;
601         struct btrfs_disk_key location;
602
603         header = btrfs_item_ptr(leaf, slot, struct btrfs_free_space_header);
604         btrfs_free_space_key(leaf, header, &location);
605         printf("\t\tlocation ");
606         btrfs_print_key(&location);
607         printf("\n");
608         printf("\t\tcache generation %llu entries %llu bitmaps %llu\n",
609                (unsigned long long)btrfs_free_space_generation(leaf, header),
610                (unsigned long long)btrfs_free_space_entries(leaf, header),
611                (unsigned long long)btrfs_free_space_bitmaps(leaf, header));
612 }
613
614 void print_key_type(FILE *stream, u64 objectid, u8 type)
615 {
616         static const char* key_to_str[256] = {
617                 [BTRFS_INODE_ITEM_KEY]          = "INODE_ITEM",
618                 [BTRFS_INODE_REF_KEY]           = "INODE_REF",
619                 [BTRFS_INODE_EXTREF_KEY]        = "INODE_EXTREF",
620                 [BTRFS_DIR_ITEM_KEY]            = "DIR_ITEM",
621                 [BTRFS_DIR_INDEX_KEY]           = "DIR_INDEX",
622                 [BTRFS_DIR_LOG_ITEM_KEY]        = "DIR_LOG_ITEM",
623                 [BTRFS_DIR_LOG_INDEX_KEY]       = "DIR_LOG_INDEX",
624                 [BTRFS_XATTR_ITEM_KEY]          = "XATTR_ITEM",
625                 [BTRFS_ORPHAN_ITEM_KEY]         = "ORPHAN_ITEM",
626                 [BTRFS_ROOT_ITEM_KEY]           = "ROOT_ITEM",
627                 [BTRFS_ROOT_REF_KEY]            = "ROOT_REF",
628                 [BTRFS_ROOT_BACKREF_KEY]        = "ROOT_BACKREF",
629                 [BTRFS_EXTENT_ITEM_KEY]         = "EXTENT_ITEM",
630                 [BTRFS_METADATA_ITEM_KEY]       = "METADATA_ITEM",
631                 [BTRFS_TREE_BLOCK_REF_KEY]      = "TREE_BLOCK_REF",
632                 [BTRFS_SHARED_BLOCK_REF_KEY]    = "SHARED_BLOCK_REF",
633                 [BTRFS_EXTENT_DATA_REF_KEY]     = "EXTENT_DATA_REF",
634                 [BTRFS_SHARED_DATA_REF_KEY]     = "SHARED_DATA_REF",
635                 [BTRFS_EXTENT_REF_V0_KEY]       = "EXTENT_REF_V0",
636                 [BTRFS_CSUM_ITEM_KEY]           = "CSUM_ITEM",
637                 [BTRFS_EXTENT_CSUM_KEY]         = "EXTENT_CSUM",
638                 [BTRFS_EXTENT_DATA_KEY]         = "EXTENT_DATA",
639                 [BTRFS_BLOCK_GROUP_ITEM_KEY]    = "BLOCK_GROUP_ITEM",
640                 [BTRFS_FREE_SPACE_INFO_KEY]     = "FREE_SPACE_INFO",
641                 [BTRFS_FREE_SPACE_EXTENT_KEY]   = "FREE_SPACE_EXTENT",
642                 [BTRFS_FREE_SPACE_BITMAP_KEY]   = "FREE_SPACE_BITMAP",
643                 [BTRFS_CHUNK_ITEM_KEY]          = "CHUNK_ITEM",
644                 [BTRFS_DEV_ITEM_KEY]            = "DEV_ITEM",
645                 [BTRFS_DEV_EXTENT_KEY]          = "DEV_EXTENT",
646                 [BTRFS_TEMPORARY_ITEM_KEY]      = "TEMPORARY_ITEM",
647                 [BTRFS_DEV_REPLACE_KEY]         = "DEV_REPLACE",
648                 [BTRFS_STRING_ITEM_KEY]         = "STRING_ITEM",
649                 [BTRFS_QGROUP_STATUS_KEY]       = "QGROUP_STATUS",
650                 [BTRFS_QGROUP_RELATION_KEY]     = "QGROUP_RELATION",
651                 [BTRFS_QGROUP_INFO_KEY]         = "QGROUP_INFO",
652                 [BTRFS_QGROUP_LIMIT_KEY]        = "QGROUP_LIMIT",
653                 [BTRFS_PERSISTENT_ITEM_KEY]     = "PERSISTENT_ITEM",
654                 [BTRFS_UUID_KEY_SUBVOL]         = "UUID_KEY_SUBVOL",
655                 [BTRFS_UUID_KEY_RECEIVED_SUBVOL] = "UUID_KEY_RECEIVED_SUBVOL",
656         };
657
658         if (type == 0 && objectid == BTRFS_FREE_SPACE_OBJECTID) {
659                 fprintf(stream, "UNTYPED");
660                 return;
661         }
662
663
664         if (key_to_str[type])
665                 fputs(key_to_str[type], stream);
666         else
667                 fprintf(stream, "UNKNOWN.%d", type);
668 }
669
670 void print_objectid(FILE *stream, u64 objectid, u8 type)
671 {
672         switch (type) {
673         case BTRFS_DEV_EXTENT_KEY:
674                 /* device id */
675                 fprintf(stream, "%llu", (unsigned long long)objectid);
676                 return;
677         case BTRFS_QGROUP_RELATION_KEY:
678                 fprintf(stream, "%llu/%llu", btrfs_qgroup_level(objectid),
679                        btrfs_qgroup_subvid(objectid));
680                 return;
681         case BTRFS_UUID_KEY_SUBVOL:
682         case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
683                 fprintf(stream, "0x%016llx", (unsigned long long)objectid);
684                 return;
685         }
686
687         switch (objectid) {
688         case BTRFS_ROOT_TREE_OBJECTID:
689                 if (type == BTRFS_DEV_ITEM_KEY)
690                         fprintf(stream, "DEV_ITEMS");
691                 else
692                         fprintf(stream, "ROOT_TREE");
693                 break;
694         case BTRFS_EXTENT_TREE_OBJECTID:
695                 fprintf(stream, "EXTENT_TREE");
696                 break;
697         case BTRFS_CHUNK_TREE_OBJECTID:
698                 fprintf(stream, "CHUNK_TREE");
699                 break;
700         case BTRFS_DEV_TREE_OBJECTID:
701                 fprintf(stream, "DEV_TREE");
702                 break;
703         case BTRFS_FS_TREE_OBJECTID:
704                 fprintf(stream, "FS_TREE");
705                 break;
706         case BTRFS_ROOT_TREE_DIR_OBJECTID:
707                 fprintf(stream, "ROOT_TREE_DIR");
708                 break;
709         case BTRFS_CSUM_TREE_OBJECTID:
710                 fprintf(stream, "CSUM_TREE");
711                 break;
712         case BTRFS_BALANCE_OBJECTID:
713                 fprintf(stream, "BALANCE");
714                 break;
715         case BTRFS_ORPHAN_OBJECTID:
716                 fprintf(stream, "ORPHAN");
717                 break;
718         case BTRFS_TREE_LOG_OBJECTID:
719                 fprintf(stream, "TREE_LOG");
720                 break;
721         case BTRFS_TREE_LOG_FIXUP_OBJECTID:
722                 fprintf(stream, "LOG_FIXUP");
723                 break;
724         case BTRFS_TREE_RELOC_OBJECTID:
725                 fprintf(stream, "TREE_RELOC");
726                 break;
727         case BTRFS_DATA_RELOC_TREE_OBJECTID:
728                 fprintf(stream, "DATA_RELOC_TREE");
729                 break;
730         case BTRFS_EXTENT_CSUM_OBJECTID:
731                 fprintf(stream, "EXTENT_CSUM");
732                 break;
733         case BTRFS_FREE_SPACE_OBJECTID:
734                 fprintf(stream, "FREE_SPACE");
735                 break;
736         case BTRFS_FREE_INO_OBJECTID:
737                 fprintf(stream, "FREE_INO");
738                 break;
739         case BTRFS_QUOTA_TREE_OBJECTID:
740                 fprintf(stream, "QUOTA_TREE");
741                 break;
742         case BTRFS_UUID_TREE_OBJECTID:
743                 fprintf(stream, "UUID_TREE");
744                 break;
745         case BTRFS_FREE_SPACE_TREE_OBJECTID:
746                 fprintf(stream, "FREE_SPACE_TREE");
747                 break;
748         case BTRFS_MULTIPLE_OBJECTIDS:
749                 fprintf(stream, "MULTIPLE");
750                 break;
751         case (u64)-1:
752                 fprintf(stream, "-1");
753                 break;
754         case BTRFS_FIRST_CHUNK_TREE_OBJECTID:
755                 if (type == BTRFS_CHUNK_ITEM_KEY) {
756                         fprintf(stream, "FIRST_CHUNK_TREE");
757                         break;
758                 }
759                 /* fall-thru */
760         default:
761                 fprintf(stream, "%llu", (unsigned long long)objectid);
762         }
763 }
764
765 void btrfs_print_key(struct btrfs_disk_key *disk_key)
766 {
767         u64 objectid = btrfs_disk_key_objectid(disk_key);
768         u8 type = btrfs_disk_key_type(disk_key);
769         u64 offset = btrfs_disk_key_offset(disk_key);
770
771         printf("key (");
772         print_objectid(stdout, objectid, type);
773         printf(" ");
774         print_key_type(stdout, objectid, type);
775         switch (type) {
776         case BTRFS_QGROUP_RELATION_KEY:
777         case BTRFS_QGROUP_INFO_KEY:
778         case BTRFS_QGROUP_LIMIT_KEY:
779                 printf(" %llu/%llu)", btrfs_qgroup_level(offset),
780                        btrfs_qgroup_subvid(offset));
781                 break;
782         case BTRFS_UUID_KEY_SUBVOL:
783         case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
784                 printf(" 0x%016llx)", (unsigned long long)offset);
785                 break;
786         default:
787                 if (offset == (u64)-1)
788                         printf(" -1)");
789                 else
790                         printf(" %llu)", (unsigned long long)offset);
791                 break;
792         }
793 }
794
795 static void print_uuid_item(struct extent_buffer *l, unsigned long offset,
796                             u32 item_size)
797 {
798         if (item_size & (sizeof(u64) - 1)) {
799                 printf("btrfs: uuid item with illegal size %lu!\n",
800                        (unsigned long)item_size);
801                 return;
802         }
803         while (item_size) {
804                 __le64 subvol_id;
805
806                 read_extent_buffer(l, &subvol_id, offset, sizeof(u64));
807                 printf("\t\tsubvol_id %llu\n",
808                         (unsigned long long)le64_to_cpu(subvol_id));
809                 item_size -= sizeof(u64);
810                 offset += sizeof(u64);
811         }
812 }
813
814 /* Btrfs inode flag stringification helper */
815 #define STRCAT_ONE_INODE_FLAG(flags, name, empty, dst) ({                       \
816         if (flags & BTRFS_INODE_##name) {                               \
817                 if (!empty)                                             \
818                         strcat(dst, "|");                               \
819                 strcat(dst, #name);                                     \
820                 empty = 0;                                              \
821         }                                                               \
822 })
823
824 /*
825  * Caller should ensure sizeof(*ret) >= 102: all charactors plus '|' of
826  * BTRFS_INODE_* flags
827  */
828 static void inode_flags_to_str(u64 flags, char *ret)
829 {
830         int empty = 1;
831
832         STRCAT_ONE_INODE_FLAG(flags, NODATASUM, empty, ret);
833         STRCAT_ONE_INODE_FLAG(flags, NODATACOW, empty, ret);
834         STRCAT_ONE_INODE_FLAG(flags, READONLY, empty, ret);
835         STRCAT_ONE_INODE_FLAG(flags, NOCOMPRESS, empty, ret);
836         STRCAT_ONE_INODE_FLAG(flags, PREALLOC, empty, ret);
837         STRCAT_ONE_INODE_FLAG(flags, SYNC, empty, ret);
838         STRCAT_ONE_INODE_FLAG(flags, IMMUTABLE, empty, ret);
839         STRCAT_ONE_INODE_FLAG(flags, APPEND, empty, ret);
840         STRCAT_ONE_INODE_FLAG(flags, NODUMP, empty, ret);
841         STRCAT_ONE_INODE_FLAG(flags, NOATIME, empty, ret);
842         STRCAT_ONE_INODE_FLAG(flags, DIRSYNC, empty, ret);
843         STRCAT_ONE_INODE_FLAG(flags, COMPRESS, empty, ret);
844         if (empty)
845                 strcat(ret, "none");
846 }
847
848 static void print_timespec(struct extent_buffer *eb,
849                 struct btrfs_timespec *timespec, const char *prefix,
850                 const char *suffix)
851 {
852         struct tm tm;
853         u64 tmp_u64;
854         u32 tmp_u32;
855         time_t tmp_time;
856         char timestamp[256];
857
858         tmp_u64 = btrfs_timespec_sec(eb, timespec);
859         tmp_u32 = btrfs_timespec_nsec(eb, timespec);
860         tmp_time = tmp_u64;
861         localtime_r(&tmp_time, &tm);
862         strftime(timestamp, sizeof(timestamp),
863                         "%Y-%m-%d %H:%M:%S", &tm);
864         printf("%s%llu.%u (%s)%s", prefix, (unsigned long long)tmp_u64, tmp_u32,
865                         timestamp, suffix);
866 }
867
868 static void print_inode_item(struct extent_buffer *eb,
869                 struct btrfs_inode_item *ii)
870 {
871         char flags_str[256];
872
873         memset(flags_str, 0, sizeof(flags_str));
874         inode_flags_to_str(btrfs_inode_flags(eb, ii), flags_str);
875         printf("\t\tgeneration %llu transid %llu size %llu nbytes %llu\n"
876                "\t\tblock group %llu mode %o links %u uid %u gid %u rdev %llu\n"
877                "\t\tsequence %llu flags 0x%llx(%s)\n",
878                (unsigned long long)btrfs_inode_generation(eb, ii),
879                (unsigned long long)btrfs_inode_transid(eb, ii),
880                (unsigned long long)btrfs_inode_size(eb, ii),
881                (unsigned long long)btrfs_inode_nbytes(eb, ii),
882                (unsigned long long)btrfs_inode_block_group(eb,ii),
883                btrfs_inode_mode(eb, ii),
884                btrfs_inode_nlink(eb, ii),
885                btrfs_inode_uid(eb, ii),
886                btrfs_inode_gid(eb, ii),
887                (unsigned long long)btrfs_inode_rdev(eb,ii),
888                (unsigned long long)btrfs_inode_flags(eb,ii),
889                (unsigned long long)btrfs_inode_sequence(eb, ii),
890                flags_str);
891         print_timespec(eb, btrfs_inode_atime(ii), "\t\tatime ", "\n");
892         print_timespec(eb, btrfs_inode_ctime(ii), "\t\tctime ", "\n");
893         print_timespec(eb, btrfs_inode_mtime(ii), "\t\tmtime ", "\n");
894         print_timespec(eb, btrfs_inode_otime(ii), "\t\totime ", "\n");
895 }
896
897 static void print_disk_balance_args(struct btrfs_disk_balance_args *ba)
898 {
899         printf("\t\tprofiles %llu devid %llu target %llu flags %llu\n",
900                         (unsigned long long)le64_to_cpu(ba->profiles),
901                         (unsigned long long)le64_to_cpu(ba->devid),
902                         (unsigned long long)le64_to_cpu(ba->target),
903                         (unsigned long long)le64_to_cpu(ba->flags));
904         printf("\t\tusage_min %u usage_max %u pstart %llu pend %llu\n",
905                         le32_to_cpu(ba->usage_min),
906                         le32_to_cpu(ba->usage_max),
907                         (unsigned long long)le64_to_cpu(ba->pstart),
908                         (unsigned long long)le64_to_cpu(ba->pend));
909         printf("\t\tvstart %llu vend %llu limit_min %u limit_max %u\n",
910                         (unsigned long long)le64_to_cpu(ba->vstart),
911                         (unsigned long long)le64_to_cpu(ba->vend),
912                         le32_to_cpu(ba->limit_min),
913                         le32_to_cpu(ba->limit_max));
914         printf("\t\tstripes_min %u stripes_max %u\n",
915                         le32_to_cpu(ba->stripes_min),
916                         le32_to_cpu(ba->stripes_max));
917 }
918
919 static void print_balance_item(struct extent_buffer *eb,
920                 struct btrfs_balance_item *bi)
921 {
922         printf("\t\tbalance status flags %llu\n",
923                         btrfs_balance_item_flags(eb, bi));
924
925         printf("\t\tDATA\n");
926         print_disk_balance_args(btrfs_balance_item_data(eb, bi));
927         printf("\t\tMETADATA\n");
928         print_disk_balance_args(btrfs_balance_item_meta(eb, bi));
929         printf("\t\tSYSTEM\n");
930         print_disk_balance_args(btrfs_balance_item_sys(eb, bi));
931 }
932
933 static void print_dev_stats(struct extent_buffer *eb,
934                 struct btrfs_dev_stats_item *stats, u32 size)
935 {
936         int i;
937         u32 known = BTRFS_DEV_STAT_VALUES_MAX * sizeof(__le64);
938         __le64 *values = btrfs_dev_stats_values(eb, stats);
939
940         printf("\t\tdevice stats\n");
941         printf("\t\twrite_errs %llu read_errs %llu flush_errs %llu corruption_errs %llu generation %llu\n",
942                 (unsigned long long)le64_to_cpu(values[BTRFS_DEV_STAT_WRITE_ERRS]),
943                 (unsigned long long)le64_to_cpu(values[BTRFS_DEV_STAT_READ_ERRS]),
944                 (unsigned long long)le64_to_cpu(values[BTRFS_DEV_STAT_FLUSH_ERRS]),
945                 (unsigned long long)le64_to_cpu(values[BTRFS_DEV_STAT_CORRUPTION_ERRS]),
946                 (unsigned long long)le64_to_cpu(values[BTRFS_DEV_STAT_GENERATION_ERRS]));
947
948         if (known < size) {
949                 printf("\t\tunknown stats item bytes %u", size - known);
950                 for (i = BTRFS_DEV_STAT_VALUES_MAX; i * sizeof(__le64) < size; i++) {
951                         printf("\t\tunknown item %u offset %zu value %llu\n",
952                                 i, i * sizeof(__le64),
953                                 (unsigned long long)le64_to_cpu(values[i]));
954                 }
955         }
956 }
957
958 static void print_block_group_item(struct extent_buffer *eb,
959                 struct btrfs_block_group_item *bgi)
960 {
961         struct btrfs_block_group_item bg_item;
962         char flags_str[256];
963
964         read_extent_buffer(eb, &bg_item, (unsigned long)bgi, sizeof(bg_item));
965         memset(flags_str, 0, sizeof(flags_str));
966         bg_flags_to_str(btrfs_block_group_flags(&bg_item), flags_str);
967         printf("\t\tblock group used %llu chunk_objectid %llu flags %s\n",
968                 (unsigned long long)btrfs_block_group_used(&bg_item),
969                 (unsigned long long)btrfs_block_group_chunk_objectid(&bg_item),
970                 flags_str);
971 }
972
973 static void print_extent_data_ref(struct extent_buffer *eb, int slot)
974 {
975         struct btrfs_extent_data_ref *dref;
976
977         dref = btrfs_item_ptr(eb, slot, struct btrfs_extent_data_ref);
978         printf("\t\textent data backref root %llu "
979                 "objectid %llu offset %llu count %u\n",
980                 (unsigned long long)btrfs_extent_data_ref_root(eb, dref),
981                 (unsigned long long)btrfs_extent_data_ref_objectid(eb, dref),
982                 (unsigned long long)btrfs_extent_data_ref_offset(eb, dref),
983                 btrfs_extent_data_ref_count(eb, dref));
984 }
985
986 static void print_shared_data_ref(struct extent_buffer *eb, int slot)
987 {
988         struct btrfs_shared_data_ref *sref;
989
990         sref = btrfs_item_ptr(eb, slot, struct btrfs_shared_data_ref);
991         printf("\t\tshared data backref count %u\n",
992                 btrfs_shared_data_ref_count(eb, sref));
993 }
994
995 static void print_free_space_info(struct extent_buffer *eb, int slot)
996 {
997         struct btrfs_free_space_info *free_info;
998
999         free_info = btrfs_item_ptr(eb, slot, struct btrfs_free_space_info);
1000         printf("\t\tfree space info extent count %u flags %u\n",
1001                 (unsigned)btrfs_free_space_extent_count(eb, free_info),
1002                 (unsigned)btrfs_free_space_flags(eb, free_info));
1003 }
1004
1005 static void print_dev_extent(struct extent_buffer *eb, int slot)
1006 {
1007         struct btrfs_dev_extent *dev_extent;
1008         u8 uuid[BTRFS_UUID_SIZE];
1009         char uuid_str[BTRFS_UUID_UNPARSED_SIZE];
1010
1011         dev_extent = btrfs_item_ptr(eb, slot, struct btrfs_dev_extent);
1012         read_extent_buffer(eb, uuid,
1013                 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(dev_extent),
1014                 BTRFS_UUID_SIZE);
1015         uuid_unparse(uuid, uuid_str);
1016         printf("\t\tdev extent chunk_tree %llu\n"
1017                 "\t\tchunk_objectid %llu chunk_offset %llu "
1018                 "length %llu\n"
1019                 "\t\tchunk_tree_uuid %s\n",
1020                 (unsigned long long)btrfs_dev_extent_chunk_tree(eb, dev_extent),
1021                 (unsigned long long)btrfs_dev_extent_chunk_objectid(eb, dev_extent),
1022                 (unsigned long long)btrfs_dev_extent_chunk_offset(eb, dev_extent),
1023                 (unsigned long long)btrfs_dev_extent_length(eb, dev_extent),
1024                 uuid_str);
1025 }
1026
1027 static void print_qgroup_status(struct extent_buffer *eb, int slot)
1028 {
1029         struct btrfs_qgroup_status_item *qg_status;
1030         char flags_str[256];
1031
1032         qg_status = btrfs_item_ptr(eb, slot, struct btrfs_qgroup_status_item);
1033         memset(flags_str, 0, sizeof(flags_str));
1034         qgroup_flags_to_str(btrfs_qgroup_status_flags(eb, qg_status),
1035                                         flags_str);
1036         printf("\t\tversion %llu generation %llu flags %s scan %lld\n",
1037                 (unsigned long long)btrfs_qgroup_status_version(eb, qg_status),
1038                 (unsigned long long)btrfs_qgroup_status_generation(eb, qg_status),
1039                 flags_str,
1040                 (unsigned long long)btrfs_qgroup_status_rescan(eb, qg_status));
1041 }
1042
1043 static void print_qgroup_info(struct extent_buffer *eb, int slot)
1044 {
1045         struct btrfs_qgroup_info_item *qg_info;
1046
1047         qg_info = btrfs_item_ptr(eb, slot, struct btrfs_qgroup_info_item);
1048         printf("\t\tgeneration %llu\n"
1049                 "\t\treferenced %llu referenced_compressed %llu\n"
1050                 "\t\texclusive %llu exclusive_compressed %llu\n",
1051                 (unsigned long long)btrfs_qgroup_info_generation(eb, qg_info),
1052                 (unsigned long long)btrfs_qgroup_info_referenced(eb, qg_info),
1053                 (unsigned long long)btrfs_qgroup_info_referenced_compressed(eb,
1054                                                                        qg_info),
1055                 (unsigned long long)btrfs_qgroup_info_exclusive(eb, qg_info),
1056                 (unsigned long long)btrfs_qgroup_info_exclusive_compressed(eb,
1057                                                                       qg_info));
1058 }
1059
1060 static void print_qgroup_limit(struct extent_buffer *eb, int slot)
1061 {
1062         struct btrfs_qgroup_limit_item *qg_limit;
1063
1064         qg_limit = btrfs_item_ptr(eb, slot, struct btrfs_qgroup_limit_item);
1065         printf("\t\tflags %llx\n"
1066                 "\t\tmax_referenced %lld max_exclusive %lld\n"
1067                 "\t\trsv_referenced %lld rsv_exclusive %lld\n",
1068                 (unsigned long long)btrfs_qgroup_limit_flags(eb, qg_limit),
1069                 (long long)btrfs_qgroup_limit_max_referenced(eb, qg_limit),
1070                 (long long)btrfs_qgroup_limit_max_exclusive(eb, qg_limit),
1071                 (long long)btrfs_qgroup_limit_rsv_referenced(eb, qg_limit),
1072                 (long long)btrfs_qgroup_limit_rsv_exclusive(eb, qg_limit));
1073 }
1074
1075 static void print_persistent_item(struct extent_buffer *eb, void *ptr,
1076                 u32 item_size, u64 objectid, u64 offset)
1077 {
1078         printf("\t\tpersistent item objectid ");
1079         print_objectid(stdout, objectid, BTRFS_PERSISTENT_ITEM_KEY);
1080         printf(" offset %llu\n", (unsigned long long)offset);
1081         switch (objectid) {
1082         case BTRFS_DEV_STATS_OBJECTID:
1083                 print_dev_stats(eb, ptr, item_size);
1084                 break;
1085         default:
1086                 printf("\t\tunknown persistent item objectid %llu\n", objectid);
1087         }
1088 }
1089
1090 static void print_temporary_item(struct extent_buffer *eb, void *ptr,
1091                 u64 objectid, u64 offset)
1092 {
1093         printf("\t\ttemporary item objectid ");
1094         print_objectid(stdout, objectid, BTRFS_TEMPORARY_ITEM_KEY);
1095         printf(" offset %llu\n", (unsigned long long)offset);
1096         switch (objectid) {
1097         case BTRFS_BALANCE_OBJECTID:
1098                 print_balance_item(eb, ptr);
1099                 break;
1100         default:
1101                 printf("\t\tunknown temporary item objectid %llu\n", objectid);
1102         }
1103 }
1104
1105 static void print_extent_csum(struct extent_buffer *eb,
1106                 struct btrfs_fs_info *fs_info, u32 item_size, u64 start)
1107 {
1108         u32 size;
1109
1110         size = (item_size / btrfs_super_csum_size(fs_info->super_copy)) *
1111                         fs_info->sectorsize;
1112         printf("\t\trange start %llu end %llu length %u\n",
1113                         (unsigned long long)start,
1114                         (unsigned long long)start + size, size);
1115 }
1116
1117 /* Caller must ensure sizeof(*ret) >= 14 "WRITTEN|RELOC" */
1118 static void header_flags_to_str(u64 flags, char *ret)
1119 {
1120         int empty = 1;
1121
1122         if (flags & BTRFS_HEADER_FLAG_WRITTEN) {
1123                 empty = 0;
1124                 strcpy(ret, "WRITTEN");
1125         }
1126         if (flags & BTRFS_HEADER_FLAG_RELOC) {
1127                 if (!empty)
1128                         strcat(ret, "|");
1129                 strcat(ret, "RELOC");
1130         }
1131 }
1132
1133 void btrfs_print_leaf(struct btrfs_root *root, struct extent_buffer *eb)
1134 {
1135         struct btrfs_item *item;
1136         struct btrfs_disk_key disk_key;
1137         char flags_str[128];
1138         u32 i;
1139         u32 nr;
1140         u64 flags;
1141         u8 backref_rev;
1142
1143         flags = btrfs_header_flags(eb) & ~BTRFS_BACKREF_REV_MASK;
1144         backref_rev = btrfs_header_flags(eb) >> BTRFS_BACKREF_REV_SHIFT;
1145         header_flags_to_str(flags, flags_str);
1146         nr = btrfs_header_nritems(eb);
1147
1148         printf("leaf %llu items %d free space %d generation %llu owner %llu\n",
1149                 (unsigned long long)btrfs_header_bytenr(eb), nr,
1150                 btrfs_leaf_free_space(root, eb),
1151                 (unsigned long long)btrfs_header_generation(eb),
1152                 (unsigned long long)btrfs_header_owner(eb));
1153         printf("leaf %llu flags 0x%llx(%s) backref revision %d\n",
1154                 btrfs_header_bytenr(eb), flags, flags_str, backref_rev);
1155         print_uuids(eb);
1156         fflush(stdout);
1157
1158         for (i = 0; i < nr; i++) {
1159                 u32 item_size;
1160                 void *ptr;
1161                 u64 objectid;
1162                 u32 type;
1163                 u64 offset;
1164
1165                 item = btrfs_item_nr(i);
1166                 item_size = btrfs_item_size(eb, item);
1167                 /* Untyped extraction of slot from btrfs_item_ptr */
1168                 ptr = btrfs_item_ptr(eb, i, void*);
1169
1170                 btrfs_item_key(eb, &disk_key, i);
1171                 objectid = btrfs_disk_key_objectid(&disk_key);
1172                 type = btrfs_disk_key_type(&disk_key);
1173                 offset = btrfs_disk_key_offset(&disk_key);
1174
1175                 printf("\titem %d ", i);
1176                 btrfs_print_key(&disk_key);
1177                 printf(" itemoff %d itemsize %d\n",
1178                         btrfs_item_offset(eb, item),
1179                         btrfs_item_size(eb, item));
1180
1181                 if (type == 0 && objectid == BTRFS_FREE_SPACE_OBJECTID)
1182                         print_free_space_header(eb, i);
1183
1184                 switch (type) {
1185                 case BTRFS_INODE_ITEM_KEY:
1186                         print_inode_item(eb, ptr);
1187                         break;
1188                 case BTRFS_INODE_REF_KEY:
1189                         print_inode_ref_item(eb, item_size, ptr);
1190                         break;
1191                 case BTRFS_INODE_EXTREF_KEY:
1192                         print_inode_extref_item(eb, item_size, ptr);
1193                         break;
1194                 case BTRFS_DIR_ITEM_KEY:
1195                 case BTRFS_DIR_INDEX_KEY:
1196                 case BTRFS_XATTR_ITEM_KEY:
1197                         print_dir_item(eb, item_size, ptr);
1198                         break;
1199                 case BTRFS_DIR_LOG_INDEX_KEY:
1200                 case BTRFS_DIR_LOG_ITEM_KEY: {
1201                         struct btrfs_dir_log_item *dlog;
1202
1203                         dlog = btrfs_item_ptr(eb, i, struct btrfs_dir_log_item);
1204                         printf("\t\tdir log end %Lu\n",
1205                                (unsigned long long)btrfs_dir_log_end(eb, dlog));
1206                         break;
1207                         }
1208                 case BTRFS_ORPHAN_ITEM_KEY:
1209                         printf("\t\torphan item\n");
1210                         break;
1211                 case BTRFS_ROOT_ITEM_KEY:
1212                         print_root_item(eb, i);
1213                         break;
1214                 case BTRFS_ROOT_REF_KEY:
1215                         print_root_ref(eb, i, "ref");
1216                         break;
1217                 case BTRFS_ROOT_BACKREF_KEY:
1218                         print_root_ref(eb, i, "backref");
1219                         break;
1220                 case BTRFS_EXTENT_ITEM_KEY:
1221                         print_extent_item(eb, i, 0);
1222                         break;
1223                 case BTRFS_METADATA_ITEM_KEY:
1224                         print_extent_item(eb, i, 1);
1225                         break;
1226                 case BTRFS_TREE_BLOCK_REF_KEY:
1227                         printf("\t\ttree block backref\n");
1228                         break;
1229                 case BTRFS_SHARED_BLOCK_REF_KEY:
1230                         printf("\t\tshared block backref\n");
1231                         break;
1232                 case BTRFS_EXTENT_DATA_REF_KEY:
1233                         print_extent_data_ref(eb, i);
1234                         break;
1235                 case BTRFS_SHARED_DATA_REF_KEY:
1236                         print_shared_data_ref(eb, i);
1237                         break;
1238                 case BTRFS_EXTENT_REF_V0_KEY:
1239 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1240                         print_extent_ref_v0(eb, i);
1241 #else
1242                         BUG();
1243 #endif
1244                         break;
1245                 case BTRFS_CSUM_ITEM_KEY:
1246                         printf("\t\tcsum item\n");
1247                         break;
1248                 case BTRFS_EXTENT_CSUM_KEY:
1249                         print_extent_csum(eb, root->fs_info, item_size,
1250                                         disk_key.offset);
1251                         break;
1252                 case BTRFS_EXTENT_DATA_KEY:
1253                         print_file_extent_item(eb, item, i, ptr);
1254                         break;
1255                 case BTRFS_BLOCK_GROUP_ITEM_KEY:
1256                         print_block_group_item(eb, ptr);
1257                         break;
1258                 case BTRFS_FREE_SPACE_INFO_KEY:
1259                         print_free_space_info(eb, i);
1260                         break;
1261                 case BTRFS_FREE_SPACE_EXTENT_KEY:
1262                         printf("\t\tfree space extent\n");
1263                         break;
1264                 case BTRFS_FREE_SPACE_BITMAP_KEY:
1265                         printf("\t\tfree space bitmap\n");
1266                         break;
1267                 case BTRFS_CHUNK_ITEM_KEY:
1268                         print_chunk_item(eb, ptr);
1269                         break;
1270                 case BTRFS_DEV_ITEM_KEY:
1271                         print_dev_item(eb, ptr);
1272                         break;
1273                 case BTRFS_DEV_EXTENT_KEY:
1274                         print_dev_extent(eb, i);
1275                         break;
1276                 case BTRFS_QGROUP_STATUS_KEY:
1277                         print_qgroup_status(eb, i);
1278                         break;
1279                 case BTRFS_QGROUP_RELATION_KEY:
1280                         break;
1281                 case BTRFS_QGROUP_INFO_KEY:
1282                         print_qgroup_info(eb, i);
1283                         break;
1284                 case BTRFS_QGROUP_LIMIT_KEY:
1285                         print_qgroup_limit(eb, i);
1286                         break;
1287                 case BTRFS_UUID_KEY_SUBVOL:
1288                 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
1289                         print_uuid_item(eb, btrfs_item_ptr_offset(eb, i),
1290                                         btrfs_item_size_nr(eb, i));
1291                         break;
1292                 case BTRFS_STRING_ITEM_KEY: {
1293                         const char *str = eb->data + btrfs_item_ptr_offset(eb, i);
1294
1295                         printf("\t\titem data %.*s\n", item_size, str);
1296                         break;
1297                         }
1298                 case BTRFS_PERSISTENT_ITEM_KEY:
1299                         print_persistent_item(eb, ptr, item_size, objectid,
1300                                         offset);
1301                         break;
1302                 case BTRFS_TEMPORARY_ITEM_KEY:
1303                         print_temporary_item(eb, ptr, objectid, offset);
1304                         break;
1305                 };
1306                 fflush(stdout);
1307         }
1308 }
1309
1310 void btrfs_print_tree(struct btrfs_root *root, struct extent_buffer *eb, int follow)
1311 {
1312         u32 i;
1313         u32 nr;
1314         struct btrfs_disk_key disk_key;
1315         struct btrfs_key key;
1316         struct extent_buffer *next;
1317
1318         if (!eb)
1319                 return;
1320         nr = btrfs_header_nritems(eb);
1321         if (btrfs_is_leaf(eb)) {
1322                 btrfs_print_leaf(root, eb);
1323                 return;
1324         }
1325         printf("node %llu level %d items %d free %u generation %llu owner %llu\n",
1326                (unsigned long long)eb->start,
1327                 btrfs_header_level(eb), nr,
1328                 (u32)BTRFS_NODEPTRS_PER_BLOCK(root) - nr,
1329                 (unsigned long long)btrfs_header_generation(eb),
1330                 (unsigned long long)btrfs_header_owner(eb));
1331         print_uuids(eb);
1332         fflush(stdout);
1333         for (i = 0; i < nr; i++) {
1334                 u64 blocknr = btrfs_node_blockptr(eb, i);
1335                 btrfs_node_key(eb, &disk_key, i);
1336                 btrfs_disk_key_to_cpu(&key, &disk_key);
1337                 printf("\t");
1338                 btrfs_print_key(&disk_key);
1339                 printf(" block %llu (%llu) gen %llu\n",
1340                        (unsigned long long)blocknr,
1341                        (unsigned long long)blocknr / root->fs_info->nodesize,
1342                        (unsigned long long)btrfs_node_ptr_generation(eb, i));
1343                 fflush(stdout);
1344         }
1345         if (!follow)
1346                 return;
1347
1348         for (i = 0; i < nr; i++) {
1349                 next = read_tree_block(root->fs_info,
1350                                 btrfs_node_blockptr(eb, i),
1351                                 btrfs_node_ptr_generation(eb, i));
1352                 if (!extent_buffer_uptodate(next)) {
1353                         fprintf(stderr, "failed to read %llu in tree %llu\n",
1354                                 (unsigned long long)btrfs_node_blockptr(eb, i),
1355                                 (unsigned long long)btrfs_header_owner(eb));
1356                         continue;
1357                 }
1358                 if (btrfs_is_leaf(next) && btrfs_header_level(eb) != 1) {
1359                         warning(
1360         "eb corrupted: item %d eb level %d next level %d, skipping the rest",
1361                                 i, btrfs_header_level(next),
1362                                 btrfs_header_level(eb));
1363                         goto out;
1364                 }
1365                 if (btrfs_header_level(next) != btrfs_header_level(eb) - 1) {
1366                         warning(
1367         "eb corrupted: item %d eb level %d next level %d, skipping the rest",
1368                                 i, btrfs_header_level(next),
1369                                 btrfs_header_level(eb));
1370                         goto out;
1371                 }
1372                 btrfs_print_tree(root, next, 1);
1373                 free_extent_buffer(next);
1374         }
1375
1376         return;
1377
1378 out:
1379         free_extent_buffer(next);
1380 }