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