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