btrfs-progs: tests: Add SHARED_DATA_REF test image for check lowmem mode
[btrfs-progs-unstable/devel.git] / cmds-inspect-dump-tree.c
blob5a5ca5363cfd8e637dc3a81f660b3d37f041680a
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 if (info->log_root_tree)
348 printf("log root tree: %llu level %d\n",
349 info->log_root_tree->node->start,
350 btrfs_header_level(
351 info->log_root_tree->node));
352 } else {
353 if (info->tree_root->node) {
354 printf("root tree\n");
355 btrfs_print_tree(info->tree_root,
356 info->tree_root->node, 1);
359 if (info->chunk_root->node) {
360 printf("chunk tree\n");
361 btrfs_print_tree(info->chunk_root,
362 info->chunk_root->node, 1);
365 if (info->log_root_tree) {
366 printf("log root tree\n");
367 btrfs_print_tree(info->log_root_tree,
368 info->log_root_tree->node, 1);
372 tree_root_scan = info->tree_root;
374 btrfs_init_path(&path);
375 again:
376 if (!extent_buffer_uptodate(tree_root_scan->node))
377 goto no_node;
380 * Tree's that are not pointed by the tree of tree roots
382 if (tree_id && tree_id == BTRFS_ROOT_TREE_OBJECTID) {
383 if (!info->tree_root->node) {
384 error("cannot print root tree, invalid pointer");
385 goto close_root;
387 printf("root tree\n");
388 btrfs_print_tree(info->tree_root, info->tree_root->node, 1);
389 goto close_root;
392 if (tree_id && tree_id == BTRFS_CHUNK_TREE_OBJECTID) {
393 if (!info->chunk_root->node) {
394 error("cannot print chunk tree, invalid pointer");
395 goto close_root;
397 printf("chunk tree\n");
398 btrfs_print_tree(info->chunk_root, info->chunk_root->node, 1);
399 goto close_root;
402 if (tree_id && tree_id == BTRFS_TREE_LOG_OBJECTID) {
403 if (!info->log_root_tree) {
404 error("cannot print log root tree, invalid pointer");
405 goto close_root;
407 printf("log root tree\n");
408 btrfs_print_tree(info->log_root_tree, info->log_root_tree->node,
410 goto close_root;
413 key.offset = 0;
414 key.objectid = 0;
415 key.type = BTRFS_ROOT_ITEM_KEY;
416 ret = btrfs_search_slot(NULL, tree_root_scan, &key, &path, 0, 0);
417 if (ret < 0) {
418 error("cannot read ROOT_ITEM from tree %llu: %s",
419 (unsigned long long)tree_root_scan->root_key.objectid,
420 strerror(-ret));
421 goto close_root;
423 while (1) {
424 leaf = path.nodes[0];
425 slot = path.slots[0];
426 if (slot >= btrfs_header_nritems(leaf)) {
427 ret = btrfs_next_leaf(tree_root_scan, &path);
428 if (ret != 0)
429 break;
430 leaf = path.nodes[0];
431 slot = path.slots[0];
433 btrfs_item_key(leaf, &disk_key, path.slots[0]);
434 btrfs_disk_key_to_cpu(&found_key, &disk_key);
435 if (found_key.type == BTRFS_ROOT_ITEM_KEY) {
436 unsigned long offset;
437 struct extent_buffer *buf;
438 int skip = extent_only | device_only | uuid_tree_only;
440 offset = btrfs_item_ptr_offset(leaf, slot);
441 read_extent_buffer(leaf, &ri, offset, sizeof(ri));
442 buf = read_tree_block(tree_root_scan,
443 btrfs_root_bytenr(&ri),
444 tree_root_scan->nodesize,
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;
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");
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");
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");
488 break;
489 case BTRFS_ORPHAN_OBJECTID:
490 if (!skip) {
491 printf("orphan");
493 break;
494 case BTRFS_TREE_LOG_OBJECTID:
495 if (!skip) {
496 printf("log");
498 break;
499 case BTRFS_TREE_LOG_FIXUP_OBJECTID:
500 if (!skip) {
501 printf("log fixup");
503 break;
504 case BTRFS_TREE_RELOC_OBJECTID:
505 if (!skip) {
506 printf("reloc");
508 break;
509 case BTRFS_DATA_RELOC_TREE_OBJECTID:
510 if (!skip) {
511 printf("data reloc");
513 break;
514 case BTRFS_EXTENT_CSUM_OBJECTID:
515 if (!skip) {
516 printf("extent checksum");
518 break;
519 case BTRFS_QUOTA_TREE_OBJECTID:
520 if (!skip) {
521 printf("quota");
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");
538 break;
539 default:
540 if (!skip) {
541 printf("file");
544 if (extent_only && !skip) {
545 printf(" tree ");
546 btrfs_print_key(&disk_key);
547 printf("\n");
548 print_extents(tree_root_scan, 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(tree_root_scan, buf, 1);
561 free_extent_buffer(buf);
563 next:
564 path.slots[0]++;
566 no_node:
567 btrfs_release_path(&path);
569 if (tree_root_scan == info->tree_root && info->log_root_tree) {
570 tree_root_scan = info->log_root_tree;
571 goto again;
574 if (extent_only || device_only || uuid_tree_only)
575 goto close_root;
577 if (root_backups)
578 print_old_roots(info->super_copy);
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;