btrfs-progs: tests: Use '-t btrfs' mount option in tests
[platform/upstream/btrfs-progs.git] / cmds-inspect-dump-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 <unistd.h>
22 #include <uuid/uuid.h>
23 #include <getopt.h>
24
25 #include "kerncompat.h"
26 #include "radix-tree.h"
27 #include "ctree.h"
28 #include "disk-io.h"
29 #include "print-tree.h"
30 #include "transaction.h"
31 #include "volumes.h"
32 #include "commands.h"
33 #include "utils.h"
34 #include "cmds-inspect-dump-tree.h"
35 #include "help.h"
36
37 static void print_extents(struct btrfs_root *root, struct extent_buffer *eb)
38 {
39         struct extent_buffer *next;
40         int i;
41         u32 nr;
42         u32 size;
43
44         if (!eb)
45                 return;
46
47         if (btrfs_is_leaf(eb)) {
48                 btrfs_print_leaf(root, eb);
49                 return;
50         }
51
52         size = root->fs_info->nodesize;
53         nr = btrfs_header_nritems(eb);
54         for (i = 0; i < nr; i++) {
55                 next = read_tree_block(root->fs_info,
56                                 btrfs_node_blockptr(eb, i),
57                                 size, btrfs_node_ptr_generation(eb, i));
58                 if (!extent_buffer_uptodate(next))
59                         continue;
60                 if (btrfs_is_leaf(next) && btrfs_header_level(eb) != 1) {
61                         warning(
62         "eb corrupted: item %d eb level %d next level %d, skipping the rest",
63                                 i, btrfs_header_level(next),
64                                 btrfs_header_level(eb));
65                         goto out;
66                 }
67                 if (btrfs_header_level(next) != btrfs_header_level(eb) - 1) {
68                         warning(
69         "eb corrupted: item %d eb level %d next level %d, skipping the rest",
70                                 i, btrfs_header_level(next),
71                                 btrfs_header_level(eb));
72                         goto out;
73                 }
74                 print_extents(root, next);
75                 free_extent_buffer(next);
76         }
77
78         return;
79
80 out:
81         free_extent_buffer(next);
82 }
83
84 static void print_old_roots(struct btrfs_super_block *super)
85 {
86         struct btrfs_root_backup *backup;
87         int i;
88
89         for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) {
90                 backup = super->super_roots + i;
91                 printf("btrfs root backup slot %d\n", i);
92                 printf("\ttree root gen %llu block %llu\n",
93                        (unsigned long long)btrfs_backup_tree_root_gen(backup),
94                        (unsigned long long)btrfs_backup_tree_root(backup));
95
96                 printf("\t\textent root gen %llu block %llu\n",
97                        (unsigned long long)btrfs_backup_extent_root_gen(backup),
98                        (unsigned long long)btrfs_backup_extent_root(backup));
99
100                 printf("\t\tchunk root gen %llu block %llu\n",
101                        (unsigned long long)btrfs_backup_chunk_root_gen(backup),
102                        (unsigned long long)btrfs_backup_chunk_root(backup));
103
104                 printf("\t\tdevice root gen %llu block %llu\n",
105                        (unsigned long long)btrfs_backup_dev_root_gen(backup),
106                        (unsigned long long)btrfs_backup_dev_root(backup));
107
108                 printf("\t\tcsum root gen %llu block %llu\n",
109                        (unsigned long long)btrfs_backup_csum_root_gen(backup),
110                        (unsigned long long)btrfs_backup_csum_root(backup));
111
112                 printf("\t\tfs root gen %llu block %llu\n",
113                        (unsigned long long)btrfs_backup_fs_root_gen(backup),
114                        (unsigned long long)btrfs_backup_fs_root(backup));
115
116                 printf("\t\t%llu used %llu total %llu devices\n",
117                        (unsigned long long)btrfs_backup_bytes_used(backup),
118                        (unsigned long long)btrfs_backup_total_bytes(backup),
119                        (unsigned long long)btrfs_backup_num_devices(backup));
120         }
121 }
122
123 /*
124  * Convert a tree name from various forms to the numerical id if possible
125  * Accepted forms:
126  * - case does not matter
127  * - same as the key name, BTRFS_ROOT_TREE_OBJECTID
128  * - dtto shortened, BTRFS_ROOT_TREE
129  * - dtto without prefix, ROOT_TREE
130  * - common name, ROOT, CHUNK, EXTENT, ...
131  * - dtto alias, DEVICE for DEV, CHECKSUM for CSUM
132  *
133  * Returns 0 if the tree id was not recognized.
134  */
135 static u64 treeid_from_string(const char *str, const char **end)
136 {
137         int match = 0;
138         int i;
139         u64 id;
140         static struct treename {
141                 const char *name;
142                 u64 id;
143         } tn[] = {
144                 { "ROOT", BTRFS_ROOT_TREE_OBJECTID },
145                 { "EXTENT", BTRFS_EXTENT_TREE_OBJECTID },
146                 { "CHUNK", BTRFS_CHUNK_TREE_OBJECTID },
147                 { "DEVICE", BTRFS_DEV_TREE_OBJECTID },
148                 { "DEV", BTRFS_DEV_TREE_OBJECTID },
149                 { "FS_TREE", BTRFS_FS_TREE_OBJECTID },
150                 { "CSUM", BTRFS_CSUM_TREE_OBJECTID },
151                 { "CHECKSUM", BTRFS_CSUM_TREE_OBJECTID },
152                 { "QUOTA", BTRFS_QUOTA_TREE_OBJECTID },
153                 { "UUID", BTRFS_UUID_TREE_OBJECTID },
154                 { "FREE_SPACE", BTRFS_FREE_SPACE_TREE_OBJECTID },
155                 { "TREE_LOG_FIXUP", BTRFS_TREE_LOG_FIXUP_OBJECTID },
156                 { "TREE_LOG", BTRFS_TREE_LOG_OBJECTID },
157                 { "TREE_RELOC", BTRFS_TREE_RELOC_OBJECTID },
158                 { "DATA_RELOC", BTRFS_DATA_RELOC_TREE_OBJECTID }
159         };
160
161         if (strncasecmp("BTRFS_", str, strlen("BTRFS_")) == 0)
162                 str += strlen("BTRFS_");
163
164         for (i = 0; i < ARRAY_SIZE(tn); i++) {
165                 int len = strlen(tn[i].name);
166
167                 if (strncasecmp(tn[i].name, str, len) == 0) {
168                         id = tn[i].id;
169                         match = 1;
170                         str += len;
171                         break;
172                 }
173         }
174
175         if (!match)
176                 return 0;
177
178         if (strncasecmp("_TREE", str, strlen("_TREE")) == 0)
179                 str += strlen("_TREE");
180
181         if (strncasecmp("_OBJECTID", str, strlen("_OBJECTID")) == 0)
182                 str += strlen("_OBJECTID");
183
184         *end = str;
185
186         return id;
187 }
188
189 const char * const cmd_inspect_dump_tree_usage[] = {
190         "btrfs inspect-internal dump-tree [options] device",
191         "Dump tree structures from a given device",
192         "Dump tree structures from a given device in textual form, expand keys to human",
193         "readable equivalents where possible.",
194         "Note: contains file names, consider that if you're asked to send the dump",
195         "for analysis.",
196         "",
197         "-e|--extents           print only extent info: extent and device trees",
198         "-d|--device            print only device info: tree root, chunk and device trees",
199         "-r|--roots             print only short root node info",
200         "-R|--backups           same as --roots plus print backup root info",
201         "-u|--uuid              print only the uuid tree",
202         "-b|--block <block_num> print info from the specified block only",
203         "-t|--tree <tree_id>    print only tree with the given id (string or number)",
204         NULL
205 };
206
207 int cmd_inspect_dump_tree(int argc, char **argv)
208 {
209         struct btrfs_root *root;
210         struct btrfs_fs_info *info;
211         struct btrfs_path path;
212         struct btrfs_key key;
213         struct btrfs_root_item ri;
214         struct extent_buffer *leaf;
215         struct btrfs_disk_key disk_key;
216         struct btrfs_key found_key;
217         char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
218         int ret;
219         int slot;
220         int extent_only = 0;
221         int device_only = 0;
222         int uuid_tree_only = 0;
223         int roots_only = 0;
224         int root_backups = 0;
225         u64 block_only = 0;
226         struct btrfs_root *tree_root_scan;
227         u64 tree_id = 0;
228
229         while (1) {
230                 int c;
231                 static const struct option long_options[] = {
232                         { "extents", no_argument, NULL, 'e'},
233                         { "device", no_argument, NULL, 'd'},
234                         { "roots", no_argument, NULL, 'r'},
235                         { "backups", no_argument, NULL, 'R'},
236                         { "uuid", no_argument, NULL, 'u'},
237                         { "block", required_argument, NULL, 'b'},
238                         { "tree", required_argument, NULL, 't'},
239                         { NULL, 0, NULL, 0 }
240                 };
241
242                 c = getopt_long(argc, argv, "deb:rRut:", long_options, NULL);
243                 if (c < 0)
244                         break;
245                 switch (c) {
246                 case 'e':
247                         extent_only = 1;
248                         break;
249                 case 'd':
250                         device_only = 1;
251                         break;
252                 case 'r':
253                         roots_only = 1;
254                         break;
255                 case 'u':
256                         uuid_tree_only = 1;
257                         break;
258                 case 'R':
259                         roots_only = 1;
260                         root_backups = 1;
261                         break;
262                 case 'b':
263                         block_only = arg_strtou64(optarg);
264                         break;
265                 case 't': {
266                         const char *end = NULL;
267
268                         if (string_is_numerical(optarg))
269                                 tree_id = arg_strtou64(optarg);
270                         else
271                                 tree_id = treeid_from_string(optarg, &end);
272
273                         if (!tree_id) {
274                                 error("unrecognized tree id: %s",
275                                                 optarg);
276                                 exit(1);
277                         }
278
279                         if (end && *end) {
280                                 error("unexpected tree id suffix of '%s': %s",
281                                                 optarg, end);
282                                 exit(1);
283                         }
284                         break;
285                         }
286                 default:
287                         usage(cmd_inspect_dump_tree_usage);
288                 }
289         }
290
291         if (check_argc_exact(argc - optind, 1))
292                 usage(cmd_inspect_dump_tree_usage);
293
294         ret = check_arg_type(argv[optind]);
295         if (ret != BTRFS_ARG_BLKDEV && ret != BTRFS_ARG_REG) {
296                 error("not a block device or regular file: %s", argv[optind]);
297                 goto out;
298         }
299
300         printf("%s\n", PACKAGE_STRING);
301
302         info = open_ctree_fs_info(argv[optind], 0, 0, 0, OPEN_CTREE_PARTIAL);
303         if (!info) {
304                 error("unable to open %s", argv[optind]);
305                 goto out;
306         }
307
308         root = info->fs_root;
309         if (!root) {
310                 error("unable to open %s", argv[optind]);
311                 goto out;
312         }
313
314         if (block_only) {
315                 leaf = read_tree_block(info,
316                                       block_only,
317                                       info->nodesize, 0);
318
319                 if (extent_buffer_uptodate(leaf) &&
320                     btrfs_header_level(leaf) != 0) {
321                         free_extent_buffer(leaf);
322                         leaf = NULL;
323                 }
324
325                 if (!leaf) {
326                         leaf = read_tree_block(info,
327                                               block_only,
328                                               info->nodesize, 0);
329                 }
330                 if (!extent_buffer_uptodate(leaf)) {
331                         error("failed to read %llu",
332                                 (unsigned long long)block_only);
333                         goto close_root;
334                 }
335                 btrfs_print_tree(root, leaf, 0);
336                 free_extent_buffer(leaf);
337                 goto close_root;
338         }
339
340         if (!(extent_only || uuid_tree_only || tree_id)) {
341                 if (roots_only) {
342                         printf("root tree: %llu level %d\n",
343                              (unsigned long long)info->tree_root->node->start,
344                              btrfs_header_level(info->tree_root->node));
345                         printf("chunk tree: %llu level %d\n",
346                              (unsigned long long)info->chunk_root->node->start,
347                              btrfs_header_level(info->chunk_root->node));
348                         if (info->log_root_tree)
349                                 printf("log root tree: %llu level %d\n",
350                                        info->log_root_tree->node->start,
351                                         btrfs_header_level(
352                                                 info->log_root_tree->node));
353                 } else {
354                         if (info->tree_root->node) {
355                                 printf("root tree\n");
356                                 btrfs_print_tree(info->tree_root,
357                                                  info->tree_root->node, 1);
358                         }
359
360                         if (info->chunk_root->node) {
361                                 printf("chunk tree\n");
362                                 btrfs_print_tree(info->chunk_root,
363                                                  info->chunk_root->node, 1);
364                         }
365
366                         if (info->log_root_tree) {
367                                 printf("log root tree\n");
368                                 btrfs_print_tree(info->log_root_tree,
369                                                 info->log_root_tree->node, 1);
370                         }
371                 }
372         }
373         tree_root_scan = info->tree_root;
374
375         btrfs_init_path(&path);
376 again:
377         if (!extent_buffer_uptodate(tree_root_scan->node))
378                 goto no_node;
379
380         /*
381          * Tree's that are not pointed by the tree of tree roots
382          */
383         if (tree_id && tree_id == BTRFS_ROOT_TREE_OBJECTID) {
384                 if (!info->tree_root->node) {
385                         error("cannot print root tree, invalid pointer");
386                         goto close_root;
387                 }
388                 printf("root tree\n");
389                 btrfs_print_tree(info->tree_root, info->tree_root->node, 1);
390                 goto close_root;
391         }
392
393         if (tree_id && tree_id == BTRFS_CHUNK_TREE_OBJECTID) {
394                 if (!info->chunk_root->node) {
395                         error("cannot print chunk tree, invalid pointer");
396                         goto close_root;
397                 }
398                 printf("chunk tree\n");
399                 btrfs_print_tree(info->chunk_root, info->chunk_root->node, 1);
400                 goto close_root;
401         }
402
403         if (tree_id && tree_id == BTRFS_TREE_LOG_OBJECTID) {
404                 if (!info->log_root_tree) {
405                         error("cannot print log root tree, invalid pointer");
406                         goto close_root;
407                 }
408                 printf("log root tree\n");
409                 btrfs_print_tree(info->log_root_tree, info->log_root_tree->node,
410                                  1);
411                 goto close_root;
412         }
413
414         key.offset = 0;
415         key.objectid = 0;
416         key.type = BTRFS_ROOT_ITEM_KEY;
417         ret = btrfs_search_slot(NULL, tree_root_scan, &key, &path, 0, 0);
418         if (ret < 0) {
419                 error("cannot read ROOT_ITEM from tree %llu: %s",
420                         (unsigned long long)tree_root_scan->root_key.objectid,
421                         strerror(-ret));
422                 goto close_root;
423         }
424         while (1) {
425                 leaf = path.nodes[0];
426                 slot = path.slots[0];
427                 if (slot >= btrfs_header_nritems(leaf)) {
428                         ret = btrfs_next_leaf(tree_root_scan, &path);
429                         if (ret != 0)
430                                 break;
431                         leaf = path.nodes[0];
432                         slot = path.slots[0];
433                 }
434                 btrfs_item_key(leaf, &disk_key, path.slots[0]);
435                 btrfs_disk_key_to_cpu(&found_key, &disk_key);
436                 if (found_key.type == BTRFS_ROOT_ITEM_KEY) {
437                         unsigned long offset;
438                         struct extent_buffer *buf;
439                         int skip = extent_only | device_only | uuid_tree_only;
440
441                         offset = btrfs_item_ptr_offset(leaf, slot);
442                         read_extent_buffer(leaf, &ri, offset, sizeof(ri));
443                         buf = read_tree_block(info, btrfs_root_bytenr(&ri),
444                                               info->nodesize, 0);
445                         if (!extent_buffer_uptodate(buf))
446                                 goto next;
447                         if (tree_id && found_key.objectid != tree_id) {
448                                 free_extent_buffer(buf);
449                                 goto next;
450                         }
451
452                         switch (found_key.objectid) {
453                         case BTRFS_ROOT_TREE_OBJECTID:
454                                 if (!skip)
455                                         printf("root");
456                                 break;
457                         case BTRFS_EXTENT_TREE_OBJECTID:
458                                 if (!device_only && !uuid_tree_only)
459                                         skip = 0;
460                                 if (!skip)
461                                         printf("extent");
462                                 break;
463                         case BTRFS_CHUNK_TREE_OBJECTID:
464                                 if (!skip) {
465                                         printf("chunk");
466                                 }
467                                 break;
468                         case BTRFS_DEV_TREE_OBJECTID:
469                                 if (!uuid_tree_only)
470                                         skip = 0;
471                                 if (!skip)
472                                         printf("device");
473                                 break;
474                         case BTRFS_FS_TREE_OBJECTID:
475                                 if (!skip) {
476                                         printf("fs");
477                                 }
478                                 break;
479                         case BTRFS_ROOT_TREE_DIR_OBJECTID:
480                                 skip = 0;
481                                 printf("directory");
482                                 break;
483                         case BTRFS_CSUM_TREE_OBJECTID:
484                                 if (!skip) {
485                                         printf("checksum");
486                                 }
487                                 break;
488                         case BTRFS_ORPHAN_OBJECTID:
489                                 if (!skip) {
490                                         printf("orphan");
491                                 }
492                                 break;
493                         case BTRFS_TREE_LOG_OBJECTID:
494                                 if (!skip) {
495                                         printf("log");
496                                 }
497                                 break;
498                         case BTRFS_TREE_LOG_FIXUP_OBJECTID:
499                                 if (!skip) {
500                                         printf("log fixup");
501                                 }
502                                 break;
503                         case BTRFS_TREE_RELOC_OBJECTID:
504                                 if (!skip) {
505                                         printf("reloc");
506                                 }
507                                 break;
508                         case BTRFS_DATA_RELOC_TREE_OBJECTID:
509                                 if (!skip) {
510                                         printf("data reloc");
511                                 }
512                                 break;
513                         case BTRFS_EXTENT_CSUM_OBJECTID:
514                                 if (!skip) {
515                                         printf("extent checksum");
516                                 }
517                                 break;
518                         case BTRFS_QUOTA_TREE_OBJECTID:
519                                 if (!skip) {
520                                         printf("quota");
521                                 }
522                                 break;
523                         case BTRFS_UUID_TREE_OBJECTID:
524                                 if (!extent_only && !device_only)
525                                         skip = 0;
526                                 if (!skip)
527                                         printf("uuid");
528                                 break;
529                         case BTRFS_FREE_SPACE_TREE_OBJECTID:
530                                 if (!skip)
531                                         printf("free space");
532                                 break;
533                         case BTRFS_MULTIPLE_OBJECTIDS:
534                                 if (!skip) {
535                                         printf("multiple");
536                                 }
537                                 break;
538                         default:
539                                 if (!skip) {
540                                         printf("file");
541                                 }
542                         }
543                         if (extent_only && !skip) {
544                                 printf(" tree ");
545                                 btrfs_print_key(&disk_key);
546                                 printf("\n");
547                                 print_extents(tree_root_scan, buf);
548                         } else if (!skip) {
549                                 printf(" tree ");
550                                 btrfs_print_key(&disk_key);
551                                 if (roots_only) {
552                                         printf(" %llu level %d\n",
553                                                (unsigned long long)buf->start,
554                                                btrfs_header_level(buf));
555                                 } else {
556                                         printf(" \n");
557                                         btrfs_print_tree(tree_root_scan, buf, 1);
558                                 }
559                         }
560                         free_extent_buffer(buf);
561                 }
562 next:
563                 path.slots[0]++;
564         }
565 no_node:
566         btrfs_release_path(&path);
567
568         if (tree_root_scan == info->tree_root && info->log_root_tree) {
569                 tree_root_scan = info->log_root_tree;
570                 goto again;
571         }
572
573         if (extent_only || device_only || uuid_tree_only)
574                 goto close_root;
575
576         if (root_backups)
577                 print_old_roots(info->super_copy);
578
579         printf("total bytes %llu\n",
580                (unsigned long long)btrfs_super_total_bytes(info->super_copy));
581         printf("bytes used %llu\n",
582                (unsigned long long)btrfs_super_bytes_used(info->super_copy));
583         uuidbuf[BTRFS_UUID_UNPARSED_SIZE - 1] = '\0';
584         uuid_unparse(info->super_copy->fsid, uuidbuf);
585         printf("uuid %s\n", uuidbuf);
586 close_root:
587         ret = close_ctree(root);
588 out:
589         return !!ret;
590 }