btrfs-progs: tests: enumerate RWX in convert tests
[btrfs-progs-unstable/devel.git] / cmds-inspect-dump-tree.c
blob43c8b67c8f3c9c373142b1c319159d6a23a19328
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"
36 static void print_extents(struct btrfs_root *root, struct extent_buffer *eb)
38 int i;
39 u32 nr;
40 u32 size;
42 if (!eb)
43 return;
45 if (btrfs_is_leaf(eb)) {
46 btrfs_print_leaf(root, eb);
47 return;
50 size = btrfs_level_size(root, btrfs_header_level(eb) - 1);
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);
70 static void print_old_roots(struct btrfs_super_block *super)
72 struct btrfs_root_backup *backup;
73 int i;
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));
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));
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));
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));
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));
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));
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));
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
119 * Returns 0 if the tree id was not recognized.
121 static u64 treeid_from_string(const char *str, const char **end)
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 }
147 if (strncasecmp("BTRFS_", str, strlen("BTRFS_")) == 0)
148 str += strlen("BTRFS_");
150 for (i = 0; i < ARRAY_SIZE(tn); i++) {
151 int len = strlen(tn[i].name);
153 if (strncasecmp(tn[i].name, str, len) == 0) {
154 id = tn[i].id;
155 match = 1;
156 str += len;
157 break;
161 if (!match)
162 return 0;
164 if (strncasecmp("_TREE", str, strlen("_TREE")) == 0)
165 str += strlen("_TREE");
167 if (strncasecmp("_OBJECTID", str, strlen("_OBJECTID")) == 0)
168 str += strlen("_OBJECTID");
170 *end = str;
172 return id;
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.",
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
193 int cmd_inspect_dump_tree(int argc, char **argv)
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;
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 }
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;
257 tree_id = treeid_from_string(optarg, &end);
259 if (*end) {
260 error("unexpected tree id suffix of '%s': %s\n",
261 optarg, end);
262 exit(1);
265 if (!tree_id) {
266 error("unrecognized tree id: %s\n",
267 optarg);
268 exit(1);
270 break;
271 default:
272 usage(cmd_inspect_dump_tree_usage);
276 if (check_argc_exact(argc - optind, 1))
277 usage(cmd_inspect_dump_tree_usage);
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;
285 printf("%s\n", PACKAGE_STRING);
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;
293 root = info->fs_root;
294 if (!root) {
295 error("unable to open %s", argv[optind]);
296 goto out;
299 if (block_only) {
300 leaf = read_tree_block(root,
301 block_only,
302 root->leafsize, 0);
304 if (extent_buffer_uptodate(leaf) &&
305 btrfs_header_level(leaf) != 0) {
306 free_extent_buffer(leaf);
307 leaf = NULL;
310 if (!leaf) {
311 leaf = read_tree_block(root,
312 block_only,
313 root->nodesize, 0);
315 if (!extent_buffer_uptodate(leaf)) {
316 error("failed to read %llu",
317 (unsigned long long)block_only);
318 goto close_root;
320 btrfs_print_tree(root, leaf, 0);
321 free_extent_buffer(leaf);
322 goto close_root;
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);
340 if (info->chunk_root->node) {
341 printf("chunk tree\n");
342 btrfs_print_tree(info->chunk_root,
343 info->chunk_root->node, 1);
347 tree_root_scan = info->tree_root;
349 btrfs_init_path(&path);
350 again:
351 if (!extent_buffer_uptodate(tree_root_scan->node))
352 goto no_node;
355 * Tree's that are not pointed by the tree of tree roots
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;
362 printf("root tree\n");
363 btrfs_print_tree(info->tree_root, info->tree_root->node, 1);
364 goto no_node;
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;
372 printf("chunk tree\n");
373 btrfs_print_tree(info->chunk_root, info->chunk_root->node, 1);
374 goto no_node;
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];
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;
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 btrfs_level_size(tree_root_scan,
404 btrfs_root_level(&ri)),
406 if (!extent_buffer_uptodate(buf))
407 goto next;
408 if (tree_id && found_key.objectid != tree_id) {
409 free_extent_buffer(buf);
410 goto next;
413 switch (found_key.objectid) {
414 case BTRFS_ROOT_TREE_OBJECTID:
415 if (!skip)
416 printf("root");
417 break;
418 case BTRFS_EXTENT_TREE_OBJECTID:
419 if (!device_only && !uuid_tree_only)
420 skip = 0;
421 if (!skip)
422 printf("extent");
423 break;
424 case BTRFS_CHUNK_TREE_OBJECTID:
425 if (!skip) {
426 printf("chunk");
428 break;
429 case BTRFS_DEV_TREE_OBJECTID:
430 if (!uuid_tree_only)
431 skip = 0;
432 if (!skip)
433 printf("device");
434 break;
435 case BTRFS_FS_TREE_OBJECTID:
436 if (!skip) {
437 printf("fs");
439 break;
440 case BTRFS_ROOT_TREE_DIR_OBJECTID:
441 skip = 0;
442 printf("directory");
443 break;
444 case BTRFS_CSUM_TREE_OBJECTID:
445 if (!skip) {
446 printf("checksum");
448 break;
449 case BTRFS_ORPHAN_OBJECTID:
450 if (!skip) {
451 printf("orphan");
453 break;
454 case BTRFS_TREE_LOG_OBJECTID:
455 if (!skip) {
456 printf("log");
458 break;
459 case BTRFS_TREE_LOG_FIXUP_OBJECTID:
460 if (!skip) {
461 printf("log fixup");
463 break;
464 case BTRFS_TREE_RELOC_OBJECTID:
465 if (!skip) {
466 printf("reloc");
468 break;
469 case BTRFS_DATA_RELOC_TREE_OBJECTID:
470 if (!skip) {
471 printf("data reloc");
473 break;
474 case BTRFS_EXTENT_CSUM_OBJECTID:
475 if (!skip) {
476 printf("extent checksum");
478 break;
479 case BTRFS_QUOTA_TREE_OBJECTID:
480 if (!skip) {
481 printf("quota");
483 break;
484 case BTRFS_UUID_TREE_OBJECTID:
485 if (!extent_only && !device_only)
486 skip = 0;
487 if (!skip)
488 printf("uuid");
489 break;
490 case BTRFS_FREE_SPACE_TREE_OBJECTID:
491 if (!skip)
492 printf("free space");
493 break;
494 case BTRFS_MULTIPLE_OBJECTIDS:
495 if (!skip) {
496 printf("multiple");
498 break;
499 default:
500 if (!skip) {
501 printf("file");
504 if (extent_only && !skip) {
505 printf(" tree ");
506 btrfs_print_key(&disk_key);
507 printf("\n");
508 print_extents(tree_root_scan, buf);
509 } else if (!skip) {
510 printf(" tree ");
511 btrfs_print_key(&disk_key);
512 if (roots_only) {
513 printf(" %llu level %d\n",
514 (unsigned long long)buf->start,
515 btrfs_header_level(buf));
516 } else {
517 printf(" \n");
518 btrfs_print_tree(tree_root_scan, buf, 1);
521 free_extent_buffer(buf);
523 next:
524 path.slots[0]++;
526 no_node:
527 btrfs_release_path(&path);
529 if (tree_root_scan == info->tree_root &&
530 info->log_root_tree) {
531 tree_root_scan = info->log_root_tree;
532 goto again;
535 if (extent_only || device_only || uuid_tree_only)
536 goto close_root;
538 if (root_backups)
539 print_old_roots(info->super_copy);
541 printf("total bytes %llu\n",
542 (unsigned long long)btrfs_super_total_bytes(info->super_copy));
543 printf("bytes used %llu\n",
544 (unsigned long long)btrfs_super_bytes_used(info->super_copy));
545 uuidbuf[BTRFS_UUID_UNPARSED_SIZE - 1] = '\0';
546 uuid_unparse(info->super_copy->fsid, uuidbuf);
547 printf("uuid %s\n", uuidbuf);
548 close_root:
549 ret = close_ctree(root);
550 out:
551 return !!ret;