btrfs-progs: check/original: Avoid infinite loop when failed to repair inode
[btrfs-progs-unstable/devel.git] / cmds-inspect-dump-tree.c
blobc8acd55a0c3a91dac8c05a175eba55d033115a7a
1 /*
2 * Copyright (C) 2007 Oracle. All rights reserved.
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.
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.
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.
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <uuid/uuid.h>
23 #include <getopt.h>
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"
36 static void print_extents(struct extent_buffer *eb)
38 struct btrfs_fs_info *fs_info = eb->fs_info;
39 struct extent_buffer *next;
40 int i;
41 u32 nr;
43 if (!eb)
44 return;
46 if (btrfs_is_leaf(eb)) {
47 btrfs_print_leaf(eb);
48 return;
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;
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;
72 print_extents(next);
73 free_extent_buffer(next);
76 return;
78 out:
79 free_extent_buffer(next);
82 static void print_old_roots(struct btrfs_super_block *super)
84 struct btrfs_root_backup *backup;
85 int i;
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));
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));
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));
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));
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));
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));
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));
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
131 * Returns 0 if the tree id was not recognized.
133 static u64 treeid_from_string(const char *str, const char **end)
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 }
159 if (strncasecmp("BTRFS_", str, strlen("BTRFS_")) == 0)
160 str += strlen("BTRFS_");
162 for (i = 0; i < ARRAY_SIZE(tn); i++) {
163 int len = strlen(tn[i].name);
165 if (strncasecmp(tn[i].name, str, len) == 0) {
166 id = tn[i].id;
167 match = 1;
168 str += len;
169 break;
173 if (!match)
174 return 0;
176 if (strncasecmp("_TREE", str, strlen("_TREE")) == 0)
177 str += strlen("_TREE");
179 if (strncasecmp("_OBJECTID", str, strlen("_OBJECTID")) == 0)
180 str += strlen("_OBJECTID");
182 *end = str;
184 return id;
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.",
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
206 int cmd_inspect_dump_tree(int argc, char **argv)
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;
225 u64 block_only = 0;
226 struct btrfs_root *tree_root_scan;
227 u64 tree_id = 0;
228 bool follow = false;
231 * For debug-tree, we care nothing about extent tree (it's just backref
232 * and usage accounting, only makes sense for RW operations).
233 * Use NO_BLOCK_GROUPS here could also speedup open_ctree() and allow us
234 * to inspect fs with corrupted extent tree blocks, and show as many good
235 * tree blocks as possible.
237 open_ctree_flags = OPEN_CTREE_PARTIAL | OPEN_CTREE_NO_BLOCK_GROUPS;
238 optind = 0;
239 while (1) {
240 int c;
241 enum { GETOPT_VAL_FOLLOW = 256 };
242 static const struct option long_options[] = {
243 { "extents", no_argument, NULL, 'e'},
244 { "device", no_argument, NULL, 'd'},
245 { "roots", no_argument, NULL, 'r'},
246 { "backups", no_argument, NULL, 'R'},
247 { "uuid", no_argument, NULL, 'u'},
248 { "block", required_argument, NULL, 'b'},
249 { "tree", required_argument, NULL, 't'},
250 { "follow", no_argument, NULL, GETOPT_VAL_FOLLOW },
251 { NULL, 0, NULL, 0 }
254 c = getopt_long(argc, argv, "deb:rRut:", long_options, NULL);
255 if (c < 0)
256 break;
257 switch (c) {
258 case 'e':
259 extent_only = 1;
260 break;
261 case 'd':
262 device_only = 1;
263 break;
264 case 'r':
265 roots_only = 1;
266 break;
267 case 'u':
268 uuid_tree_only = 1;
269 break;
270 case 'R':
271 roots_only = 1;
272 root_backups = 1;
273 break;
274 case 'b':
276 * If only showing one block, no need to fill roots
277 * other than chunk root
279 open_ctree_flags |= __OPEN_CTREE_RETURN_CHUNK_ROOT;
280 block_only = arg_strtou64(optarg);
281 break;
282 case 't': {
283 const char *end = NULL;
285 if (string_is_numerical(optarg))
286 tree_id = arg_strtou64(optarg);
287 else
288 tree_id = treeid_from_string(optarg, &end);
290 if (!tree_id) {
291 error("unrecognized tree id: %s",
292 optarg);
293 exit(1);
296 if (end && *end) {
297 error("unexpected tree id suffix of '%s': %s",
298 optarg, end);
299 exit(1);
301 break;
303 case GETOPT_VAL_FOLLOW:
304 follow = true;
305 break;
306 default:
307 usage(cmd_inspect_dump_tree_usage);
311 if (check_argc_exact(argc - optind, 1))
312 usage(cmd_inspect_dump_tree_usage);
314 ret = check_arg_type(argv[optind]);
315 if (ret != BTRFS_ARG_BLKDEV && ret != BTRFS_ARG_REG) {
316 error("not a block device or regular file: %s", argv[optind]);
317 goto out;
320 printf("%s\n", PACKAGE_STRING);
322 info = open_ctree_fs_info(argv[optind], 0, 0, 0, open_ctree_flags);
323 if (!info) {
324 error("unable to open %s", argv[optind]);
325 goto out;
328 if (block_only) {
329 root = info->chunk_root;
330 leaf = read_tree_block(info, block_only, 0);
331 if (extent_buffer_uptodate(leaf) &&
332 btrfs_header_level(leaf) != 0) {
333 free_extent_buffer(leaf);
334 leaf = NULL;
337 if (!leaf)
338 leaf = read_tree_block(info, block_only, 0);
339 if (!extent_buffer_uptodate(leaf)) {
340 error("failed to read %llu",
341 (unsigned long long)block_only);
342 goto close_root;
344 btrfs_print_tree(leaf, follow);
345 free_extent_buffer(leaf);
346 goto close_root;
349 root = info->fs_root;
350 if (!root) {
351 error("unable to open %s", argv[optind]);
352 goto out;
355 if (!(extent_only || uuid_tree_only || tree_id)) {
356 if (roots_only) {
357 printf("root tree: %llu level %d\n",
358 (unsigned long long)info->tree_root->node->start,
359 btrfs_header_level(info->tree_root->node));
360 printf("chunk tree: %llu level %d\n",
361 (unsigned long long)info->chunk_root->node->start,
362 btrfs_header_level(info->chunk_root->node));
363 if (info->log_root_tree)
364 printf("log root tree: %llu level %d\n",
365 info->log_root_tree->node->start,
366 btrfs_header_level(
367 info->log_root_tree->node));
368 } else {
369 if (info->tree_root->node) {
370 printf("root tree\n");
371 btrfs_print_tree(info->tree_root->node, 1);
374 if (info->chunk_root->node) {
375 printf("chunk tree\n");
376 btrfs_print_tree(info->chunk_root->node, 1);
379 if (info->log_root_tree) {
380 printf("log root tree\n");
381 btrfs_print_tree(info->log_root_tree->node, 1);
385 tree_root_scan = info->tree_root;
387 btrfs_init_path(&path);
388 again:
389 if (!extent_buffer_uptodate(tree_root_scan->node))
390 goto no_node;
393 * Tree's that are not pointed by the tree of tree roots
395 if (tree_id && tree_id == BTRFS_ROOT_TREE_OBJECTID) {
396 if (!info->tree_root->node) {
397 error("cannot print root tree, invalid pointer");
398 goto close_root;
400 printf("root tree\n");
401 btrfs_print_tree(info->tree_root->node, 1);
402 goto close_root;
405 if (tree_id && tree_id == BTRFS_CHUNK_TREE_OBJECTID) {
406 if (!info->chunk_root->node) {
407 error("cannot print chunk tree, invalid pointer");
408 goto close_root;
410 printf("chunk tree\n");
411 btrfs_print_tree(info->chunk_root->node, 1);
412 goto close_root;
415 if (tree_id && tree_id == BTRFS_TREE_LOG_OBJECTID) {
416 if (!info->log_root_tree) {
417 error("cannot print log root tree, invalid pointer");
418 goto close_root;
420 printf("log root tree\n");
421 btrfs_print_tree(info->log_root_tree->node, 1);
422 goto close_root;
425 key.offset = 0;
426 key.objectid = 0;
427 key.type = BTRFS_ROOT_ITEM_KEY;
428 ret = btrfs_search_slot(NULL, tree_root_scan, &key, &path, 0, 0);
429 if (ret < 0) {
430 error("cannot read ROOT_ITEM from tree %llu: %s",
431 (unsigned long long)tree_root_scan->root_key.objectid,
432 strerror(-ret));
433 goto close_root;
435 while (1) {
436 leaf = path.nodes[0];
437 slot = path.slots[0];
438 if (slot >= btrfs_header_nritems(leaf)) {
439 ret = btrfs_next_leaf(tree_root_scan, &path);
440 if (ret != 0)
441 break;
442 leaf = path.nodes[0];
443 slot = path.slots[0];
445 btrfs_item_key(leaf, &disk_key, path.slots[0]);
446 btrfs_disk_key_to_cpu(&found_key, &disk_key);
447 if (found_key.type == BTRFS_ROOT_ITEM_KEY) {
448 unsigned long offset;
449 struct extent_buffer *buf;
450 int skip = extent_only | device_only | uuid_tree_only;
452 offset = btrfs_item_ptr_offset(leaf, slot);
453 read_extent_buffer(leaf, &ri, offset, sizeof(ri));
454 buf = read_tree_block(info, btrfs_root_bytenr(&ri), 0);
455 if (!extent_buffer_uptodate(buf))
456 goto next;
457 if (tree_id && found_key.objectid != tree_id) {
458 free_extent_buffer(buf);
459 goto next;
462 switch (found_key.objectid) {
463 case BTRFS_ROOT_TREE_OBJECTID:
464 if (!skip)
465 printf("root");
466 break;
467 case BTRFS_EXTENT_TREE_OBJECTID:
468 if (!device_only && !uuid_tree_only)
469 skip = 0;
470 if (!skip)
471 printf("extent");
472 break;
473 case BTRFS_CHUNK_TREE_OBJECTID:
474 if (!skip) {
475 printf("chunk");
477 break;
478 case BTRFS_DEV_TREE_OBJECTID:
479 if (!uuid_tree_only)
480 skip = 0;
481 if (!skip)
482 printf("device");
483 break;
484 case BTRFS_FS_TREE_OBJECTID:
485 if (!skip) {
486 printf("fs");
488 break;
489 case BTRFS_ROOT_TREE_DIR_OBJECTID:
490 skip = 0;
491 printf("directory");
492 break;
493 case BTRFS_CSUM_TREE_OBJECTID:
494 if (!skip) {
495 printf("checksum");
497 break;
498 case BTRFS_ORPHAN_OBJECTID:
499 if (!skip) {
500 printf("orphan");
502 break;
503 case BTRFS_TREE_LOG_OBJECTID:
504 if (!skip) {
505 printf("log");
507 break;
508 case BTRFS_TREE_LOG_FIXUP_OBJECTID:
509 if (!skip) {
510 printf("log fixup");
512 break;
513 case BTRFS_TREE_RELOC_OBJECTID:
514 if (!skip) {
515 printf("reloc");
517 break;
518 case BTRFS_DATA_RELOC_TREE_OBJECTID:
519 if (!skip) {
520 printf("data reloc");
522 break;
523 case BTRFS_EXTENT_CSUM_OBJECTID:
524 if (!skip) {
525 printf("extent checksum");
527 break;
528 case BTRFS_QUOTA_TREE_OBJECTID:
529 if (!skip) {
530 printf("quota");
532 break;
533 case BTRFS_UUID_TREE_OBJECTID:
534 if (!extent_only && !device_only)
535 skip = 0;
536 if (!skip)
537 printf("uuid");
538 break;
539 case BTRFS_FREE_SPACE_TREE_OBJECTID:
540 if (!skip)
541 printf("free space");
542 break;
543 case BTRFS_MULTIPLE_OBJECTIDS:
544 if (!skip) {
545 printf("multiple");
547 break;
548 default:
549 if (!skip) {
550 printf("file");
553 if (extent_only && !skip) {
554 printf(" tree ");
555 btrfs_print_key(&disk_key);
556 printf("\n");
557 print_extents(buf);
558 } else if (!skip) {
559 printf(" tree ");
560 btrfs_print_key(&disk_key);
561 if (roots_only) {
562 printf(" %llu level %d\n",
563 (unsigned long long)buf->start,
564 btrfs_header_level(buf));
565 } else {
566 printf(" \n");
567 btrfs_print_tree(buf, 1);
570 free_extent_buffer(buf);
572 next:
573 path.slots[0]++;
575 no_node:
576 btrfs_release_path(&path);
578 if (tree_root_scan == info->tree_root && info->log_root_tree) {
579 tree_root_scan = info->log_root_tree;
580 goto again;
583 if (extent_only || device_only || uuid_tree_only)
584 goto close_root;
586 if (root_backups)
587 print_old_roots(info->super_copy);
589 printf("total bytes %llu\n",
590 (unsigned long long)btrfs_super_total_bytes(info->super_copy));
591 printf("bytes used %llu\n",
592 (unsigned long long)btrfs_super_bytes_used(info->super_copy));
593 uuidbuf[BTRFS_UUID_UNPARSED_SIZE - 1] = '\0';
594 uuid_unparse(info->super_copy->fsid, uuidbuf);
595 printf("uuid %s\n", uuidbuf);
596 close_root:
597 ret = close_ctree(root);
598 out:
599 return !!ret;