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