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