05f9a81e1abb23892cca5625ba9a4075920529f6
[platform/upstream/btrfs-progs.git] / btrfs-debug-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 "utils.h"
33
34 static int print_usage(int ret)
35 {
36         fprintf(stderr, "usage: btrfs-debug-tree [-e] [-d] [-r] [-R] [-u]\n");
37         fprintf(stderr, "                        [-b block_num ] device\n");
38         fprintf(stderr, "\t-e : print detailed extents info\n");
39         fprintf(stderr, "\t-d : print info of btrfs device and root tree dirs"
40                     " only\n");
41         fprintf(stderr, "\t-r : print info of roots only\n");
42         fprintf(stderr, "\t-R : print info of roots and root backups\n");
43         fprintf(stderr, "\t-u : print info of uuid tree only\n");
44         fprintf(stderr, "\t-b block_num : print info of the specified block"
45                     " only\n");
46         fprintf(stderr,
47                 "\t-t tree_id : print only the tree with the given id\n");
48         fprintf(stderr, "%s\n", PACKAGE_STRING);
49         exit(ret);
50 }
51
52 static void print_extents(struct btrfs_root *root, struct extent_buffer *eb)
53 {
54         int i;
55         u32 nr;
56         u32 size;
57
58         if (!eb)
59                 return;
60
61         if (btrfs_is_leaf(eb)) {
62                 btrfs_print_leaf(root, eb);
63                 return;
64         }
65
66         size = btrfs_level_size(root, btrfs_header_level(eb) - 1);
67         nr = btrfs_header_nritems(eb);
68         for (i = 0; i < nr; i++) {
69                 struct extent_buffer *next = read_tree_block(root,
70                                              btrfs_node_blockptr(eb, i),
71                                              size,
72                                              btrfs_node_ptr_generation(eb, i));
73                 if (!extent_buffer_uptodate(next))
74                         continue;
75                 if (btrfs_is_leaf(next) &&
76                     btrfs_header_level(eb) != 1)
77                         BUG();
78                 if (btrfs_header_level(next) !=
79                         btrfs_header_level(eb) - 1)
80                         BUG();
81                 print_extents(root, next);
82                 free_extent_buffer(next);
83         }
84 }
85
86 static void print_old_roots(struct btrfs_super_block *super)
87 {
88         struct btrfs_root_backup *backup;
89         int i;
90
91         for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) {
92                 backup = super->super_roots + i;
93                 printf("btrfs root backup slot %d\n", i);
94                 printf("\ttree root gen %llu block %llu\n",
95                        (unsigned long long)btrfs_backup_tree_root_gen(backup),
96                        (unsigned long long)btrfs_backup_tree_root(backup));
97
98                 printf("\t\textent root gen %llu block %llu\n",
99                        (unsigned long long)btrfs_backup_extent_root_gen(backup),
100                        (unsigned long long)btrfs_backup_extent_root(backup));
101
102                 printf("\t\tchunk root gen %llu block %llu\n",
103                        (unsigned long long)btrfs_backup_chunk_root_gen(backup),
104                        (unsigned long long)btrfs_backup_chunk_root(backup));
105
106                 printf("\t\tdevice root gen %llu block %llu\n",
107                        (unsigned long long)btrfs_backup_dev_root_gen(backup),
108                        (unsigned long long)btrfs_backup_dev_root(backup));
109
110                 printf("\t\tcsum root gen %llu block %llu\n",
111                        (unsigned long long)btrfs_backup_csum_root_gen(backup),
112                        (unsigned long long)btrfs_backup_csum_root(backup));
113
114                 printf("\t\tfs root gen %llu block %llu\n",
115                        (unsigned long long)btrfs_backup_fs_root_gen(backup),
116                        (unsigned long long)btrfs_backup_fs_root(backup));
117
118                 printf("\t\t%llu used %llu total %llu devices\n",
119                        (unsigned long long)btrfs_backup_bytes_used(backup),
120                        (unsigned long long)btrfs_backup_total_bytes(backup),
121                        (unsigned long long)btrfs_backup_num_devices(backup));
122         }
123 }
124
125 int main(int ac, char **av)
126 {
127         struct btrfs_root *root;
128         struct btrfs_fs_info *info;
129         struct btrfs_path path;
130         struct btrfs_key key;
131         struct btrfs_root_item ri;
132         struct extent_buffer *leaf;
133         struct btrfs_disk_key disk_key;
134         struct btrfs_key found_key;
135         char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
136         int ret;
137         int slot;
138         int extent_only = 0;
139         int device_only = 0;
140         int uuid_tree_only = 0;
141         int roots_only = 0;
142         int root_backups = 0;
143         u64 block_only = 0;
144         struct btrfs_root *tree_root_scan;
145         u64 tree_id = 0;
146
147         radix_tree_init();
148
149         while(1) {
150                 int c;
151                 static const struct option long_options[] = {
152                         { "help", no_argument, NULL, GETOPT_VAL_HELP},
153                         { NULL, 0, NULL, 0 }
154                 };
155
156                 c = getopt_long(ac, av, "deb:rRut:", long_options, NULL);
157                 if (c < 0)
158                         break;
159                 switch(c) {
160                         case 'e':
161                                 extent_only = 1;
162                                 break;
163                         case 'd':
164                                 device_only = 1;
165                                 break;
166                         case 'r':
167                                 roots_only = 1;
168                                 break;
169                         case 'u':
170                                 uuid_tree_only = 1;
171                                 break;
172                         case 'R':
173                                 roots_only = 1;
174                                 root_backups = 1;
175                                 break;
176                         case 'b':
177                                 block_only = arg_strtou64(optarg);
178                                 break;
179                         case 't':
180                                 tree_id = arg_strtou64(optarg);
181                                 break;
182                         case GETOPT_VAL_HELP:
183                         default:
184                                 print_usage(c != GETOPT_VAL_HELP);
185                 }
186         }
187         set_argv0(av);
188         ac = ac - optind;
189         if (check_argc_exact(ac, 1))
190                 print_usage(1);
191
192         ret = check_arg_type(av[optind]);
193         if (ret != BTRFS_ARG_BLKDEV && ret != BTRFS_ARG_REG) {
194                 fprintf(stderr, "'%s' is not a block device or regular file\n",
195                         av[optind]);
196                 exit(1);
197         }
198
199         info = open_ctree_fs_info(av[optind], 0, 0, OPEN_CTREE_PARTIAL);
200         if (!info) {
201                 fprintf(stderr, "unable to open %s\n", av[optind]);
202                 exit(1);
203         }
204
205         root = info->fs_root;
206         if (!root) {
207                 fprintf(stderr, "unable to open %s\n", av[optind]);
208                 exit(1);
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                         fprintf(stderr, "failed to read %llu\n",
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) {
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) {
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                                 print_extents(tree_root_scan, buf);
418                         } else if (!skip) {
419                                 printf(" tree ");
420                                 btrfs_print_key(&disk_key);
421                                 if (roots_only) {
422                                         printf(" %llu level %d\n",
423                                                (unsigned long long)buf->start,
424                                                btrfs_header_level(buf));
425                                 } else {
426                                         printf(" \n");
427                                         btrfs_print_tree(tree_root_scan, buf, 1);
428                                 }
429                         }
430                         free_extent_buffer(buf);
431                 }
432 next:
433                 path.slots[0]++;
434         }
435 no_node:
436         btrfs_release_path(&path);
437
438         if (tree_root_scan == info->tree_root &&
439             info->log_root_tree) {
440                 tree_root_scan = info->log_root_tree;
441                 goto again;
442         }
443
444         if (extent_only || device_only || uuid_tree_only)
445                 goto close_root;
446
447         if (root_backups)
448                 print_old_roots(info->super_copy);
449
450         printf("total bytes %llu\n",
451                (unsigned long long)btrfs_super_total_bytes(info->super_copy));
452         printf("bytes used %llu\n",
453                (unsigned long long)btrfs_super_bytes_used(info->super_copy));
454         uuidbuf[BTRFS_UUID_UNPARSED_SIZE - 1] = '\0';
455         uuid_unparse(info->super_copy->fsid, uuidbuf);
456         printf("uuid %s\n", uuidbuf);
457         printf("%s\n", PACKAGE_STRING);
458 close_root:
459         ret = close_ctree(root);
460         btrfs_close_all_devices();
461         return ret;
462 }