btrfs-progs: Add new option for specify chunk root bytenr
[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         info = open_ctree_fs_info(argv[optind], 0, 0, 0, OPEN_CTREE_PARTIAL);
198         if (!info) {
199                 error("unable to open %s", argv[optind]);
200                 goto out;
201         }
202
203         root = info->fs_root;
204         if (!root) {
205                 error("unable to open %s", argv[optind]);
206                 goto out;
207         }
208
209         if (block_only) {
210                 leaf = read_tree_block(root,
211                                       block_only,
212                                       root->leafsize, 0);
213
214                 if (extent_buffer_uptodate(leaf) &&
215                     btrfs_header_level(leaf) != 0) {
216                         free_extent_buffer(leaf);
217                         leaf = NULL;
218                 }
219
220                 if (!leaf) {
221                         leaf = read_tree_block(root,
222                                               block_only,
223                                               root->nodesize, 0);
224                 }
225                 if (!extent_buffer_uptodate(leaf)) {
226                         error("failed to read %llu",
227                                 (unsigned long long)block_only);
228                         goto close_root;
229                 }
230                 btrfs_print_tree(root, leaf, 0);
231                 free_extent_buffer(leaf);
232                 goto close_root;
233         }
234
235         if (!(extent_only || uuid_tree_only || tree_id)) {
236                 if (roots_only) {
237                         printf("root tree: %llu level %d\n",
238                              (unsigned long long)info->tree_root->node->start,
239                              btrfs_header_level(info->tree_root->node));
240                         printf("chunk tree: %llu level %d\n",
241                              (unsigned long long)info->chunk_root->node->start,
242                              btrfs_header_level(info->chunk_root->node));
243                 } else {
244                         if (info->tree_root->node) {
245                                 printf("root tree\n");
246                                 btrfs_print_tree(info->tree_root,
247                                                  info->tree_root->node, 1);
248                         }
249
250                         if (info->chunk_root->node) {
251                                 printf("chunk tree\n");
252                                 btrfs_print_tree(info->chunk_root,
253                                                  info->chunk_root->node, 1);
254                         }
255                 }
256         }
257         tree_root_scan = info->tree_root;
258
259         btrfs_init_path(&path);
260 again:
261         if (!extent_buffer_uptodate(tree_root_scan->node))
262                 goto no_node;
263
264         /*
265          * Tree's that are not pointed by the tree of tree roots
266          */
267         if (tree_id && tree_id == BTRFS_ROOT_TREE_OBJECTID) {
268                 if (!info->tree_root->node) {
269                         error("cannot print root tree, invalid pointer");
270                         goto no_node;
271                 }
272                 printf("root tree\n");
273                 btrfs_print_tree(info->tree_root, info->tree_root->node, 1);
274                 goto no_node;
275         }
276
277         if (tree_id && tree_id == BTRFS_CHUNK_TREE_OBJECTID) {
278                 if (!info->chunk_root->node) {
279                         error("cannot print chunk tree, invalid pointer");
280                         goto no_node;
281                 }
282                 printf("chunk tree\n");
283                 btrfs_print_tree(info->chunk_root, info->chunk_root->node, 1);
284                 goto no_node;
285         }
286
287         key.offset = 0;
288         key.objectid = 0;
289         btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
290         ret = btrfs_search_slot(NULL, tree_root_scan, &key, &path, 0, 0);
291         BUG_ON(ret < 0);
292         while (1) {
293                 leaf = path.nodes[0];
294                 slot = path.slots[0];
295                 if (slot >= btrfs_header_nritems(leaf)) {
296                         ret = btrfs_next_leaf(tree_root_scan, &path);
297                         if (ret != 0)
298                                 break;
299                         leaf = path.nodes[0];
300                         slot = path.slots[0];
301                 }
302                 btrfs_item_key(leaf, &disk_key, path.slots[0]);
303                 btrfs_disk_key_to_cpu(&found_key, &disk_key);
304                 if (btrfs_key_type(&found_key) == BTRFS_ROOT_ITEM_KEY) {
305                         unsigned long offset;
306                         struct extent_buffer *buf;
307                         int skip = extent_only | device_only | uuid_tree_only;
308
309                         offset = btrfs_item_ptr_offset(leaf, slot);
310                         read_extent_buffer(leaf, &ri, offset, sizeof(ri));
311                         buf = read_tree_block(tree_root_scan,
312                                               btrfs_root_bytenr(&ri),
313                                               btrfs_level_size(tree_root_scan,
314                                                         btrfs_root_level(&ri)),
315                                               0);
316                         if (!extent_buffer_uptodate(buf))
317                                 goto next;
318                         if (tree_id && found_key.objectid != tree_id) {
319                                 free_extent_buffer(buf);
320                                 goto next;
321                         }
322
323                         switch (found_key.objectid) {
324                         case BTRFS_ROOT_TREE_OBJECTID:
325                                 if (!skip)
326                                         printf("root");
327                                 break;
328                         case BTRFS_EXTENT_TREE_OBJECTID:
329                                 if (!device_only && !uuid_tree_only)
330                                         skip = 0;
331                                 if (!skip)
332                                         printf("extent");
333                                 break;
334                         case BTRFS_CHUNK_TREE_OBJECTID:
335                                 if (!skip) {
336                                         printf("chunk");
337                                 }
338                                 break;
339                         case BTRFS_DEV_TREE_OBJECTID:
340                                 if (!uuid_tree_only)
341                                         skip = 0;
342                                 if (!skip)
343                                         printf("device");
344                                 break;
345                         case BTRFS_FS_TREE_OBJECTID:
346                                 if (!skip) {
347                                         printf("fs");
348                                 }
349                                 break;
350                         case BTRFS_ROOT_TREE_DIR_OBJECTID:
351                                 skip = 0;
352                                 printf("directory");
353                                 break;
354                         case BTRFS_CSUM_TREE_OBJECTID:
355                                 if (!skip) {
356                                         printf("checksum");
357                                 }
358                                 break;
359                         case BTRFS_ORPHAN_OBJECTID:
360                                 if (!skip) {
361                                         printf("orphan");
362                                 }
363                                 break;
364                         case BTRFS_TREE_LOG_OBJECTID:
365                                 if (!skip) {
366                                         printf("log");
367                                 }
368                                 break;
369                         case BTRFS_TREE_LOG_FIXUP_OBJECTID:
370                                 if (!skip) {
371                                         printf("log fixup");
372                                 }
373                                 break;
374                         case BTRFS_TREE_RELOC_OBJECTID:
375                                 if (!skip) {
376                                         printf("reloc");
377                                 }
378                                 break;
379                         case BTRFS_DATA_RELOC_TREE_OBJECTID:
380                                 if (!skip) {
381                                         printf("data reloc");
382                                 }
383                                 break;
384                         case BTRFS_EXTENT_CSUM_OBJECTID:
385                                 if (!skip) {
386                                         printf("extent checksum");
387                                 }
388                                 break;
389                         case BTRFS_QUOTA_TREE_OBJECTID:
390                                 if (!skip) {
391                                         printf("quota");
392                                 }
393                                 break;
394                         case BTRFS_UUID_TREE_OBJECTID:
395                                 if (!extent_only && !device_only)
396                                         skip = 0;
397                                 if (!skip)
398                                         printf("uuid");
399                                 break;
400                         case BTRFS_FREE_SPACE_TREE_OBJECTID:
401                                 if (!skip)
402                                         printf("free space");
403                                 break;
404                         case BTRFS_MULTIPLE_OBJECTIDS:
405                                 if (!skip) {
406                                         printf("multiple");
407                                 }
408                                 break;
409                         default:
410                                 if (!skip) {
411                                         printf("file");
412                                 }
413                         }
414                         if (extent_only && !skip) {
415                                 print_extents(tree_root_scan, buf);
416                         } else if (!skip) {
417                                 printf(" tree ");
418                                 btrfs_print_key(&disk_key);
419                                 if (roots_only) {
420                                         printf(" %llu level %d\n",
421                                                (unsigned long long)buf->start,
422                                                btrfs_header_level(buf));
423                                 } else {
424                                         printf(" \n");
425                                         btrfs_print_tree(tree_root_scan, buf, 1);
426                                 }
427                         }
428                         free_extent_buffer(buf);
429                 }
430 next:
431                 path.slots[0]++;
432         }
433 no_node:
434         btrfs_release_path(&path);
435
436         if (tree_root_scan == info->tree_root &&
437             info->log_root_tree) {
438                 tree_root_scan = info->log_root_tree;
439                 goto again;
440         }
441
442         if (extent_only || device_only || uuid_tree_only)
443                 goto close_root;
444
445         if (root_backups)
446                 print_old_roots(info->super_copy);
447
448         printf("total bytes %llu\n",
449                (unsigned long long)btrfs_super_total_bytes(info->super_copy));
450         printf("bytes used %llu\n",
451                (unsigned long long)btrfs_super_bytes_used(info->super_copy));
452         uuidbuf[BTRFS_UUID_UNPARSED_SIZE - 1] = '\0';
453         uuid_unparse(info->super_copy->fsid, uuidbuf);
454         printf("uuid %s\n", uuidbuf);
455         printf("%s\n", PACKAGE_STRING);
456 close_root:
457         ret = close_ctree(root);
458 out:
459         return !!ret;
460 }