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