btrfs-progs: docs: update dump-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 = 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);
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 const char * const cmd_inspect_dump_tree_usage[] = {
110         "btrfs inspect-internal dump-tree [options] device",
111         "Dump tree structures from a given device",
112         "Dump tree structures from a given device in textual form, expand keys to human",
113         "readable equivalents where possible.",
114         "Note: contains file names, consider that if you're asked to send the dump",
115         "for analysis.",
116         "",
117         "-e|--extents           print only extent info: extent and device trees",
118         "-d|--device            print only device info: tree root, chunk and device trees",
119         "-r|--roots             print only short root node info",
120         "-R|--backups           same as --roots plus print backup root info",
121         "-u|--uuid              print only the uuid tree",
122         "-b|--block <block_num> print info from the specified block only",
123         "-t|--tree <tree_id>    print only the tree with the given tree_id",
124         NULL
125 };
126
127 int cmd_inspect_dump_tree(int argc, char **argv)
128 {
129         struct btrfs_root *root;
130         struct btrfs_fs_info *info;
131         struct btrfs_path path;
132         struct btrfs_key key;
133         struct btrfs_root_item ri;
134         struct extent_buffer *leaf;
135         struct btrfs_disk_key disk_key;
136         struct btrfs_key found_key;
137         char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
138         int ret;
139         int slot;
140         int extent_only = 0;
141         int device_only = 0;
142         int uuid_tree_only = 0;
143         int roots_only = 0;
144         int root_backups = 0;
145         u64 block_only = 0;
146         struct btrfs_root *tree_root_scan;
147         u64 tree_id = 0;
148
149         while (1) {
150                 int c;
151                 static const struct option long_options[] = {
152                         { "extents", no_argument, NULL, 'e'},
153                         { "device", no_argument, NULL, 'd'},
154                         { "roots", no_argument, NULL, 'r'},
155                         { "backups", no_argument, NULL, 'R'},
156                         { "uuid", no_argument, NULL, 'u'},
157                         { "block", required_argument, NULL, 'b'},
158                         { "tree", required_argument, NULL, 't'},
159                         { NULL, 0, NULL, 0 }
160                 };
161
162                 c = getopt_long(argc, argv, "deb:rRut:", long_options, NULL);
163                 if (c < 0)
164                         break;
165                 switch (c) {
166                 case 'e':
167                         extent_only = 1;
168                         break;
169                 case 'd':
170                         device_only = 1;
171                         break;
172                 case 'r':
173                         roots_only = 1;
174                         break;
175                 case 'u':
176                         uuid_tree_only = 1;
177                         break;
178                 case 'R':
179                         roots_only = 1;
180                         root_backups = 1;
181                         break;
182                 case 'b':
183                         block_only = arg_strtou64(optarg);
184                         break;
185                 case 't':
186                         tree_id = arg_strtou64(optarg);
187                         break;
188                 default:
189                         usage(cmd_inspect_dump_tree_usage);
190                 }
191         }
192
193         if (check_argc_exact(argc - optind, 1))
194                 usage(cmd_inspect_dump_tree_usage);
195
196         ret = check_arg_type(argv[optind]);
197         if (ret != BTRFS_ARG_BLKDEV && ret != BTRFS_ARG_REG) {
198                 error("not a block device or regular file: %s", argv[optind]);
199                 goto out;
200         }
201
202         printf("%s\n", PACKAGE_STRING);
203
204         info = open_ctree_fs_info(argv[optind], 0, 0, 0, OPEN_CTREE_PARTIAL);
205         if (!info) {
206                 error("unable to open %s", argv[optind]);
207                 goto out;
208         }
209
210         root = info->fs_root;
211         if (!root) {
212                 error("unable to open %s", argv[optind]);
213                 goto out;
214         }
215
216         if (block_only) {
217                 leaf = read_tree_block(root,
218                                       block_only,
219                                       root->leafsize, 0);
220
221                 if (extent_buffer_uptodate(leaf) &&
222                     btrfs_header_level(leaf) != 0) {
223                         free_extent_buffer(leaf);
224                         leaf = NULL;
225                 }
226
227                 if (!leaf) {
228                         leaf = read_tree_block(root,
229                                               block_only,
230                                               root->nodesize, 0);
231                 }
232                 if (!extent_buffer_uptodate(leaf)) {
233                         error("failed to read %llu",
234                                 (unsigned long long)block_only);
235                         goto close_root;
236                 }
237                 btrfs_print_tree(root, leaf, 0);
238                 free_extent_buffer(leaf);
239                 goto close_root;
240         }
241
242         if (!(extent_only || uuid_tree_only || tree_id)) {
243                 if (roots_only) {
244                         printf("root tree: %llu level %d\n",
245                              (unsigned long long)info->tree_root->node->start,
246                              btrfs_header_level(info->tree_root->node));
247                         printf("chunk tree: %llu level %d\n",
248                              (unsigned long long)info->chunk_root->node->start,
249                              btrfs_header_level(info->chunk_root->node));
250                 } else {
251                         if (info->tree_root->node) {
252                                 printf("root tree\n");
253                                 btrfs_print_tree(info->tree_root,
254                                                  info->tree_root->node, 1);
255                         }
256
257                         if (info->chunk_root->node) {
258                                 printf("chunk tree\n");
259                                 btrfs_print_tree(info->chunk_root,
260                                                  info->chunk_root->node, 1);
261                         }
262                 }
263         }
264         tree_root_scan = info->tree_root;
265
266         btrfs_init_path(&path);
267 again:
268         if (!extent_buffer_uptodate(tree_root_scan->node))
269                 goto no_node;
270
271         /*
272          * Tree's that are not pointed by the tree of tree roots
273          */
274         if (tree_id && tree_id == BTRFS_ROOT_TREE_OBJECTID) {
275                 if (!info->tree_root->node) {
276                         error("cannot print root tree, invalid pointer");
277                         goto no_node;
278                 }
279                 printf("root tree\n");
280                 btrfs_print_tree(info->tree_root, info->tree_root->node, 1);
281                 goto no_node;
282         }
283
284         if (tree_id && tree_id == BTRFS_CHUNK_TREE_OBJECTID) {
285                 if (!info->chunk_root->node) {
286                         error("cannot print chunk tree, invalid pointer");
287                         goto no_node;
288                 }
289                 printf("chunk tree\n");
290                 btrfs_print_tree(info->chunk_root, info->chunk_root->node, 1);
291                 goto no_node;
292         }
293
294         key.offset = 0;
295         key.objectid = 0;
296         btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
297         ret = btrfs_search_slot(NULL, tree_root_scan, &key, &path, 0, 0);
298         BUG_ON(ret < 0);
299         while (1) {
300                 leaf = path.nodes[0];
301                 slot = path.slots[0];
302                 if (slot >= btrfs_header_nritems(leaf)) {
303                         ret = btrfs_next_leaf(tree_root_scan, &path);
304                         if (ret != 0)
305                                 break;
306                         leaf = path.nodes[0];
307                         slot = path.slots[0];
308                 }
309                 btrfs_item_key(leaf, &disk_key, path.slots[0]);
310                 btrfs_disk_key_to_cpu(&found_key, &disk_key);
311                 if (btrfs_key_type(&found_key) == BTRFS_ROOT_ITEM_KEY) {
312                         unsigned long offset;
313                         struct extent_buffer *buf;
314                         int skip = extent_only | device_only | uuid_tree_only;
315
316                         offset = btrfs_item_ptr_offset(leaf, slot);
317                         read_extent_buffer(leaf, &ri, offset, sizeof(ri));
318                         buf = read_tree_block(tree_root_scan,
319                                               btrfs_root_bytenr(&ri),
320                                               btrfs_level_size(tree_root_scan,
321                                                         btrfs_root_level(&ri)),
322                                               0);
323                         if (!extent_buffer_uptodate(buf))
324                                 goto next;
325                         if (tree_id && found_key.objectid != tree_id) {
326                                 free_extent_buffer(buf);
327                                 goto next;
328                         }
329
330                         switch (found_key.objectid) {
331                         case BTRFS_ROOT_TREE_OBJECTID:
332                                 if (!skip)
333                                         printf("root");
334                                 break;
335                         case BTRFS_EXTENT_TREE_OBJECTID:
336                                 if (!device_only && !uuid_tree_only)
337                                         skip = 0;
338                                 if (!skip)
339                                         printf("extent");
340                                 break;
341                         case BTRFS_CHUNK_TREE_OBJECTID:
342                                 if (!skip) {
343                                         printf("chunk");
344                                 }
345                                 break;
346                         case BTRFS_DEV_TREE_OBJECTID:
347                                 if (!uuid_tree_only)
348                                         skip = 0;
349                                 if (!skip)
350                                         printf("device");
351                                 break;
352                         case BTRFS_FS_TREE_OBJECTID:
353                                 if (!skip) {
354                                         printf("fs");
355                                 }
356                                 break;
357                         case BTRFS_ROOT_TREE_DIR_OBJECTID:
358                                 skip = 0;
359                                 printf("directory");
360                                 break;
361                         case BTRFS_CSUM_TREE_OBJECTID:
362                                 if (!skip) {
363                                         printf("checksum");
364                                 }
365                                 break;
366                         case BTRFS_ORPHAN_OBJECTID:
367                                 if (!skip) {
368                                         printf("orphan");
369                                 }
370                                 break;
371                         case BTRFS_TREE_LOG_OBJECTID:
372                                 if (!skip) {
373                                         printf("log");
374                                 }
375                                 break;
376                         case BTRFS_TREE_LOG_FIXUP_OBJECTID:
377                                 if (!skip) {
378                                         printf("log fixup");
379                                 }
380                                 break;
381                         case BTRFS_TREE_RELOC_OBJECTID:
382                                 if (!skip) {
383                                         printf("reloc");
384                                 }
385                                 break;
386                         case BTRFS_DATA_RELOC_TREE_OBJECTID:
387                                 if (!skip) {
388                                         printf("data reloc");
389                                 }
390                                 break;
391                         case BTRFS_EXTENT_CSUM_OBJECTID:
392                                 if (!skip) {
393                                         printf("extent checksum");
394                                 }
395                                 break;
396                         case BTRFS_QUOTA_TREE_OBJECTID:
397                                 if (!skip) {
398                                         printf("quota");
399                                 }
400                                 break;
401                         case BTRFS_UUID_TREE_OBJECTID:
402                                 if (!extent_only && !device_only)
403                                         skip = 0;
404                                 if (!skip)
405                                         printf("uuid");
406                                 break;
407                         case BTRFS_FREE_SPACE_TREE_OBJECTID:
408                                 if (!skip)
409                                         printf("free space");
410                                 break;
411                         case BTRFS_MULTIPLE_OBJECTIDS:
412                                 if (!skip) {
413                                         printf("multiple");
414                                 }
415                                 break;
416                         default:
417                                 if (!skip) {
418                                         printf("file");
419                                 }
420                         }
421                         if (extent_only && !skip) {
422                                 printf(" tree ");
423                                 btrfs_print_key(&disk_key);
424                                 printf("\n");
425                                 print_extents(tree_root_scan, buf);
426                         } else if (!skip) {
427                                 printf(" tree ");
428                                 btrfs_print_key(&disk_key);
429                                 if (roots_only) {
430                                         printf(" %llu level %d\n",
431                                                (unsigned long long)buf->start,
432                                                btrfs_header_level(buf));
433                                 } else {
434                                         printf(" \n");
435                                         btrfs_print_tree(tree_root_scan, buf, 1);
436                                 }
437                         }
438                         free_extent_buffer(buf);
439                 }
440 next:
441                 path.slots[0]++;
442         }
443 no_node:
444         btrfs_release_path(&path);
445
446         if (tree_root_scan == info->tree_root &&
447             info->log_root_tree) {
448                 tree_root_scan = info->log_root_tree;
449                 goto again;
450         }
451
452         if (extent_only || device_only || uuid_tree_only)
453                 goto close_root;
454
455         if (root_backups)
456                 print_old_roots(info->super_copy);
457
458         printf("total bytes %llu\n",
459                (unsigned long long)btrfs_super_total_bytes(info->super_copy));
460         printf("bytes used %llu\n",
461                (unsigned long long)btrfs_super_bytes_used(info->super_copy));
462         uuidbuf[BTRFS_UUID_UNPARSED_SIZE - 1] = '\0';
463         uuid_unparse(info->super_copy->fsid, uuidbuf);
464         printf("uuid %s\n", uuidbuf);
465 close_root:
466         ret = close_ctree(root);
467 out:
468         return !!ret;
469 }