btrfs-progs: dump-tree: Fix duplicated output when using -t option
[btrfs-progs-unstable/devel.git] / cmds-inspect-dump-tree.c
blob2c6bec7fecbde0964077c9f912b40da41656caca
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 "cmds-inspect-dump-tree.h"
35 #include "help.h"
37 static void print_extents(struct btrfs_root *root, struct extent_buffer *eb)
39 struct extent_buffer *next;
40 int i;
41 u32 nr;
42 u32 size;
44 if (!eb)
45 return;
47 if (btrfs_is_leaf(eb)) {
48 btrfs_print_leaf(root, eb);
49 return;
52 size = root->nodesize;
53 nr = btrfs_header_nritems(eb);
54 for (i = 0; i < nr; i++) {
55 next = read_tree_block(root, 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;
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;
73 print_extents(root, next);
74 free_extent_buffer(next);
77 return;
79 out:
80 free_extent_buffer(next);
83 static void print_old_roots(struct btrfs_super_block *super)
85 struct btrfs_root_backup *backup;
86 int i;
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));
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));
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));
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));
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));
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));
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));
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
132 * Returns 0 if the tree id was not recognized.
134 static u64 treeid_from_string(const char *str, const char **end)
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 }
160 if (strncasecmp("BTRFS_", str, strlen("BTRFS_")) == 0)
161 str += strlen("BTRFS_");
163 for (i = 0; i < ARRAY_SIZE(tn); i++) {
164 int len = strlen(tn[i].name);
166 if (strncasecmp(tn[i].name, str, len) == 0) {
167 id = tn[i].id;
168 match = 1;
169 str += len;
170 break;
174 if (!match)
175 return 0;
177 if (strncasecmp("_TREE", str, strlen("_TREE")) == 0)
178 str += strlen("_TREE");
180 if (strncasecmp("_OBJECTID", str, strlen("_OBJECTID")) == 0)
181 str += strlen("_OBJECTID");
183 *end = str;
185 return id;
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.",
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
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 u64 block_only = 0;
225 struct btrfs_root *tree_root_scan;
226 u64 tree_id = 0;
228 while (1) {
229 int c;
230 static const struct option long_options[] = {
231 { "extents", no_argument, NULL, 'e'},
232 { "device", no_argument, NULL, 'd'},
233 { "roots", no_argument, NULL, 'r'},
234 { "backups", no_argument, NULL, 'R'},
235 { "uuid", no_argument, NULL, 'u'},
236 { "block", required_argument, NULL, 'b'},
237 { "tree", required_argument, NULL, 't'},
238 { NULL, 0, NULL, 0 }
241 c = getopt_long(argc, argv, "deb:rRut:", long_options, NULL);
242 if (c < 0)
243 break;
244 switch (c) {
245 case 'e':
246 extent_only = 1;
247 break;
248 case 'd':
249 device_only = 1;
250 break;
251 case 'r':
252 roots_only = 1;
253 break;
254 case 'u':
255 uuid_tree_only = 1;
256 break;
257 case 'R':
258 roots_only = 1;
259 root_backups = 1;
260 break;
261 case 'b':
262 block_only = arg_strtou64(optarg);
263 break;
264 case 't': {
265 const char *end = NULL;
267 if (string_is_numerical(optarg))
268 tree_id = arg_strtou64(optarg);
269 else
270 tree_id = treeid_from_string(optarg, &end);
272 if (!tree_id) {
273 error("unrecognized tree id: %s",
274 optarg);
275 exit(1);
278 if (end && *end) {
279 error("unexpected tree id suffix of '%s': %s",
280 optarg, end);
281 exit(1);
283 break;
285 default:
286 usage(cmd_inspect_dump_tree_usage);
290 if (check_argc_exact(argc - optind, 1))
291 usage(cmd_inspect_dump_tree_usage);
293 ret = check_arg_type(argv[optind]);
294 if (ret != BTRFS_ARG_BLKDEV && ret != BTRFS_ARG_REG) {
295 error("not a block device or regular file: %s", argv[optind]);
296 goto out;
299 printf("%s\n", PACKAGE_STRING);
301 info = open_ctree_fs_info(argv[optind], 0, 0, 0, OPEN_CTREE_PARTIAL);
302 if (!info) {
303 error("unable to open %s", argv[optind]);
304 goto out;
307 root = info->fs_root;
308 if (!root) {
309 error("unable to open %s", argv[optind]);
310 goto out;
313 if (block_only) {
314 leaf = read_tree_block(root,
315 block_only,
316 root->nodesize, 0);
318 if (extent_buffer_uptodate(leaf) &&
319 btrfs_header_level(leaf) != 0) {
320 free_extent_buffer(leaf);
321 leaf = NULL;
324 if (!leaf) {
325 leaf = read_tree_block(root,
326 block_only,
327 root->nodesize, 0);
329 if (!extent_buffer_uptodate(leaf)) {
330 error("failed to read %llu",
331 (unsigned long long)block_only);
332 goto close_root;
334 btrfs_print_tree(root, leaf, 0);
335 free_extent_buffer(leaf);
336 goto close_root;
339 if (!(extent_only || uuid_tree_only || tree_id)) {
340 if (roots_only) {
341 printf("root tree: %llu level %d\n",
342 (unsigned long long)info->tree_root->node->start,
343 btrfs_header_level(info->tree_root->node));
344 printf("chunk tree: %llu level %d\n",
345 (unsigned long long)info->chunk_root->node->start,
346 btrfs_header_level(info->chunk_root->node));
347 } else {
348 if (info->tree_root->node) {
349 printf("root tree\n");
350 btrfs_print_tree(info->tree_root,
351 info->tree_root->node, 1);
354 if (info->chunk_root->node) {
355 printf("chunk tree\n");
356 btrfs_print_tree(info->chunk_root,
357 info->chunk_root->node, 1);
361 tree_root_scan = info->tree_root;
363 btrfs_init_path(&path);
364 again:
365 if (!extent_buffer_uptodate(tree_root_scan->node))
366 goto no_node;
369 * Tree's that are not pointed by the tree of tree roots
371 if (tree_id && tree_id == BTRFS_ROOT_TREE_OBJECTID) {
372 if (!info->tree_root->node) {
373 error("cannot print root tree, invalid pointer");
374 goto close_root;
376 printf("root tree\n");
377 btrfs_print_tree(info->tree_root, info->tree_root->node, 1);
378 goto close_root;
381 if (tree_id && tree_id == BTRFS_CHUNK_TREE_OBJECTID) {
382 if (!info->chunk_root->node) {
383 error("cannot print chunk tree, invalid pointer");
384 goto close_root;
386 printf("chunk tree\n");
387 btrfs_print_tree(info->chunk_root, info->chunk_root->node, 1);
388 goto close_root;
391 key.offset = 0;
392 key.objectid = 0;
393 key.type = BTRFS_ROOT_ITEM_KEY;
394 ret = btrfs_search_slot(NULL, tree_root_scan, &key, &path, 0, 0);
395 if (ret < 0) {
396 error("cannot read ROOT_ITEM from tree %llu: %s",
397 (unsigned long long)tree_root_scan->root_key.objectid,
398 strerror(-ret));
399 goto close_root;
401 while (1) {
402 leaf = path.nodes[0];
403 slot = path.slots[0];
404 if (slot >= btrfs_header_nritems(leaf)) {
405 ret = btrfs_next_leaf(tree_root_scan, &path);
406 if (ret != 0)
407 break;
408 leaf = path.nodes[0];
409 slot = path.slots[0];
411 btrfs_item_key(leaf, &disk_key, path.slots[0]);
412 btrfs_disk_key_to_cpu(&found_key, &disk_key);
413 if (found_key.type == BTRFS_ROOT_ITEM_KEY) {
414 unsigned long offset;
415 struct extent_buffer *buf;
416 int skip = extent_only | device_only | uuid_tree_only;
418 offset = btrfs_item_ptr_offset(leaf, slot);
419 read_extent_buffer(leaf, &ri, offset, sizeof(ri));
420 buf = read_tree_block(tree_root_scan,
421 btrfs_root_bytenr(&ri),
422 tree_root_scan->nodesize,
424 if (!extent_buffer_uptodate(buf))
425 goto next;
426 if (tree_id && found_key.objectid != tree_id) {
427 free_extent_buffer(buf);
428 goto next;
431 switch (found_key.objectid) {
432 case BTRFS_ROOT_TREE_OBJECTID:
433 if (!skip)
434 printf("root");
435 break;
436 case BTRFS_EXTENT_TREE_OBJECTID:
437 if (!device_only && !uuid_tree_only)
438 skip = 0;
439 if (!skip)
440 printf("extent");
441 break;
442 case BTRFS_CHUNK_TREE_OBJECTID:
443 if (!skip) {
444 printf("chunk");
446 break;
447 case BTRFS_DEV_TREE_OBJECTID:
448 if (!uuid_tree_only)
449 skip = 0;
450 if (!skip)
451 printf("device");
452 break;
453 case BTRFS_FS_TREE_OBJECTID:
454 if (!skip) {
455 printf("fs");
457 break;
458 case BTRFS_ROOT_TREE_DIR_OBJECTID:
459 skip = 0;
460 printf("directory");
461 break;
462 case BTRFS_CSUM_TREE_OBJECTID:
463 if (!skip) {
464 printf("checksum");
466 break;
467 case BTRFS_ORPHAN_OBJECTID:
468 if (!skip) {
469 printf("orphan");
471 break;
472 case BTRFS_TREE_LOG_OBJECTID:
473 if (!skip) {
474 printf("log");
476 break;
477 case BTRFS_TREE_LOG_FIXUP_OBJECTID:
478 if (!skip) {
479 printf("log fixup");
481 break;
482 case BTRFS_TREE_RELOC_OBJECTID:
483 if (!skip) {
484 printf("reloc");
486 break;
487 case BTRFS_DATA_RELOC_TREE_OBJECTID:
488 if (!skip) {
489 printf("data reloc");
491 break;
492 case BTRFS_EXTENT_CSUM_OBJECTID:
493 if (!skip) {
494 printf("extent checksum");
496 break;
497 case BTRFS_QUOTA_TREE_OBJECTID:
498 if (!skip) {
499 printf("quota");
501 break;
502 case BTRFS_UUID_TREE_OBJECTID:
503 if (!extent_only && !device_only)
504 skip = 0;
505 if (!skip)
506 printf("uuid");
507 break;
508 case BTRFS_FREE_SPACE_TREE_OBJECTID:
509 if (!skip)
510 printf("free space");
511 break;
512 case BTRFS_MULTIPLE_OBJECTIDS:
513 if (!skip) {
514 printf("multiple");
516 break;
517 default:
518 if (!skip) {
519 printf("file");
522 if (extent_only && !skip) {
523 printf(" tree ");
524 btrfs_print_key(&disk_key);
525 printf("\n");
526 print_extents(tree_root_scan, buf);
527 } else if (!skip) {
528 printf(" tree ");
529 btrfs_print_key(&disk_key);
530 if (roots_only) {
531 printf(" %llu level %d\n",
532 (unsigned long long)buf->start,
533 btrfs_header_level(buf));
534 } else {
535 printf(" \n");
536 btrfs_print_tree(tree_root_scan, buf, 1);
539 free_extent_buffer(buf);
541 next:
542 path.slots[0]++;
544 no_node:
545 btrfs_release_path(&path);
547 if (tree_root_scan == info->tree_root && info->log_root_tree) {
548 tree_root_scan = info->log_root_tree;
549 goto again;
552 if (extent_only || device_only || uuid_tree_only)
553 goto close_root;
555 if (root_backups)
556 print_old_roots(info->super_copy);
558 printf("total bytes %llu\n",
559 (unsigned long long)btrfs_super_total_bytes(info->super_copy));
560 printf("bytes used %llu\n",
561 (unsigned long long)btrfs_super_bytes_used(info->super_copy));
562 uuidbuf[BTRFS_UUID_UNPARSED_SIZE - 1] = '\0';
563 uuid_unparse(info->super_copy->fsid, uuidbuf);
564 printf("uuid %s\n", uuidbuf);
565 close_root:
566 ret = close_ctree(root);
567 out:
568 return !!ret;