btrfs-show-super: don't try to print not-superblocks
[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 "kerncompat.h"
24 #include "radix-tree.h"
25 #include "ctree.h"
26 #include "disk-io.h"
27 #include "print-tree.h"
28 #include "transaction.h"
29 #include "version.h"
30 #include "utils.h"
31
32 static int print_usage(void)
33 {
34         fprintf(stderr, "usage: btrfs-debug-tree [-e] [-d] [-r] [-R] [-u]\n");
35         fprintf(stderr, "                        [-b block_num ] device\n");
36         fprintf(stderr, "\t-e : print detailed extents info\n");
37         fprintf(stderr, "\t-d : print info of btrfs device and root tree dirs"
38                     " only\n");
39         fprintf(stderr, "\t-r : print info of roots only\n");
40         fprintf(stderr, "\t-R : print info of roots and root backups\n");
41         fprintf(stderr, "\t-u : print info of uuid tree only\n");
42         fprintf(stderr, "\t-b block_num : print info of the specified block"
43                     " only\n");
44         fprintf(stderr, "%s\n", BTRFS_BUILD_VERSION);
45         exit(1);
46 }
47
48 static void print_extents(struct btrfs_root *root, struct extent_buffer *eb)
49 {
50         int i;
51         u32 nr;
52         u32 size;
53
54         if (!eb)
55                 return;
56
57         if (btrfs_is_leaf(eb)) {
58                 btrfs_print_leaf(root, eb);
59                 return;
60         }
61
62         size = btrfs_level_size(root, btrfs_header_level(eb) - 1);
63         nr = btrfs_header_nritems(eb);
64         for (i = 0; i < nr; i++) {
65                 struct extent_buffer *next = read_tree_block(root,
66                                              btrfs_node_blockptr(eb, i),
67                                              size,
68                                              btrfs_node_ptr_generation(eb, i));
69                 if (btrfs_is_leaf(next) &&
70                     btrfs_header_level(eb) != 1)
71                         BUG();
72                 if (btrfs_header_level(next) !=
73                         btrfs_header_level(eb) - 1)
74                         BUG();
75                 print_extents(root, next);
76                 free_extent_buffer(next);
77         }
78 }
79
80 static void print_old_roots(struct btrfs_super_block *super)
81 {
82         struct btrfs_root_backup *backup;
83         int i;
84
85         for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) {
86                 backup = super->super_roots + i;
87                 printf("btrfs root backup slot %d\n", i);
88                 printf("\ttree root gen %llu block %llu\n",
89                        (unsigned long long)btrfs_backup_tree_root_gen(backup),
90                        (unsigned long long)btrfs_backup_tree_root(backup));
91
92                 printf("\t\textent root gen %llu block %llu\n",
93                        (unsigned long long)btrfs_backup_extent_root_gen(backup),
94                        (unsigned long long)btrfs_backup_extent_root(backup));
95
96                 printf("\t\tchunk root gen %llu block %llu\n",
97                        (unsigned long long)btrfs_backup_chunk_root_gen(backup),
98                        (unsigned long long)btrfs_backup_chunk_root(backup));
99
100                 printf("\t\tdevice root gen %llu block %llu\n",
101                        (unsigned long long)btrfs_backup_dev_root_gen(backup),
102                        (unsigned long long)btrfs_backup_dev_root(backup));
103
104                 printf("\t\tcsum root gen %llu block %llu\n",
105                        (unsigned long long)btrfs_backup_csum_root_gen(backup),
106                        (unsigned long long)btrfs_backup_csum_root(backup));
107
108                 printf("\t\tfs root gen %llu block %llu\n",
109                        (unsigned long long)btrfs_backup_fs_root_gen(backup),
110                        (unsigned long long)btrfs_backup_fs_root(backup));
111
112                 printf("\t\t%llu used %llu total %llu devices\n",
113                        (unsigned long long)btrfs_backup_bytes_used(backup),
114                        (unsigned long long)btrfs_backup_total_bytes(backup),
115                        (unsigned long long)btrfs_backup_num_devices(backup));
116         }
117 }
118
119 int main(int ac, char **av)
120 {
121         struct btrfs_root *root;
122         struct btrfs_fs_info *info;
123         struct btrfs_path path;
124         struct btrfs_key key;
125         struct btrfs_root_item ri;
126         struct extent_buffer *leaf;
127         struct btrfs_disk_key disk_key;
128         struct btrfs_key found_key;
129         char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
130         int ret;
131         int slot;
132         int extent_only = 0;
133         int device_only = 0;
134         int uuid_tree_only = 0;
135         int roots_only = 0;
136         int root_backups = 0;
137         u64 block_only = 0;
138         struct btrfs_root *tree_root_scan;
139
140         radix_tree_init();
141
142         while(1) {
143                 int c;
144                 c = getopt(ac, av, "deb:rRu");
145                 if (c < 0)
146                         break;
147                 switch(c) {
148                         case 'e':
149                                 extent_only = 1;
150                                 break;
151                         case 'd':
152                                 device_only = 1;
153                                 break;
154                         case 'r':
155                                 roots_only = 1;
156                                 break;
157                         case 'u':
158                                 uuid_tree_only = 1;
159                                 break;
160                         case 'R':
161                                 roots_only = 1;
162                                 root_backups = 1;
163                                 break;
164                         case 'b':
165                                 block_only = arg_strtou64(optarg);
166                                 break;
167                         default:
168                                 print_usage();
169                 }
170         }
171         ac = ac - optind;
172         if (ac != 1)
173                 print_usage();
174
175         info = open_ctree_fs_info(av[optind], 0, 0, OPEN_CTREE_PARTIAL);
176         if (!info) {
177                 fprintf(stderr, "unable to open %s\n", av[optind]);
178                 exit(1);
179         }
180
181         root = info->fs_root;
182         if (!root) {
183                 fprintf(stderr, "unable to open %s\n", av[optind]);
184                 exit(1);
185         }
186
187         if (block_only) {
188                 leaf = read_tree_block(root,
189                                       block_only,
190                                       root->leafsize, 0);
191
192                 if (leaf && btrfs_header_level(leaf) != 0) {
193                         free_extent_buffer(leaf);
194                         leaf = NULL;
195                 }
196
197                 if (!leaf) {
198                         leaf = read_tree_block(root,
199                                               block_only,
200                                               root->nodesize, 0);
201                 }
202                 if (!leaf) {
203                         fprintf(stderr, "failed to read %llu\n",
204                                 (unsigned long long)block_only);
205                         goto close_root;
206                 }
207                 btrfs_print_tree(root, leaf, 0);
208                 goto close_root;
209         }
210
211         if (!(extent_only || uuid_tree_only)) {
212                 if (roots_only) {
213                         printf("root tree: %llu level %d\n",
214                              (unsigned long long)info->tree_root->node->start,
215                              btrfs_header_level(info->tree_root->node));
216                         printf("chunk tree: %llu level %d\n",
217                              (unsigned long long)info->chunk_root->node->start,
218                              btrfs_header_level(info->chunk_root->node));
219                 } else {
220                         if (info->tree_root->node) {
221                                 printf("root tree\n");
222                                 btrfs_print_tree(info->tree_root,
223                                                  info->tree_root->node, 1);
224                         }
225
226                         if (info->chunk_root->node) {
227                                 printf("chunk tree\n");
228                                 btrfs_print_tree(info->chunk_root,
229                                                  info->chunk_root->node, 1);
230                         }
231                 }
232         }
233         tree_root_scan = info->tree_root;
234
235         btrfs_init_path(&path);
236 again:
237         if (!extent_buffer_uptodate(tree_root_scan->node))
238                 goto no_node;
239
240         key.offset = 0;
241         key.objectid = 0;
242         btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
243         ret = btrfs_search_slot(NULL, tree_root_scan, &key, &path, 0, 0);
244         BUG_ON(ret < 0);
245         while(1) {
246                 leaf = path.nodes[0];
247                 slot = path.slots[0];
248                 if (slot >= btrfs_header_nritems(leaf)) {
249                         ret = btrfs_next_leaf(tree_root_scan, &path);
250                         if (ret != 0)
251                                 break;
252                         leaf = path.nodes[0];
253                         slot = path.slots[0];
254                 }
255                 btrfs_item_key(leaf, &disk_key, path.slots[0]);
256                 btrfs_disk_key_to_cpu(&found_key, &disk_key);
257                 if (btrfs_key_type(&found_key) == BTRFS_ROOT_ITEM_KEY) {
258                         unsigned long offset;
259                         struct extent_buffer *buf;
260                         int skip = extent_only | device_only | uuid_tree_only;
261
262                         offset = btrfs_item_ptr_offset(leaf, slot);
263                         read_extent_buffer(leaf, &ri, offset, sizeof(ri));
264                         buf = read_tree_block(tree_root_scan,
265                                               btrfs_root_bytenr(&ri),
266                                               btrfs_level_size(tree_root_scan,
267                                                         btrfs_root_level(&ri)),
268                                               0);
269                         if (!extent_buffer_uptodate(buf))
270                                 goto next;
271
272                         switch(found_key.objectid) {
273                         case BTRFS_ROOT_TREE_OBJECTID:
274                                 if (!skip)
275                                         printf("root");
276                                 break;
277                         case BTRFS_EXTENT_TREE_OBJECTID:
278                                 if (!device_only && !uuid_tree_only)
279                                         skip = 0;
280                                 if (!skip)
281                                         printf("extent");
282                                 break;
283                         case BTRFS_CHUNK_TREE_OBJECTID:
284                                 if (!skip) {
285                                         printf("chunk");
286                                 }
287                                 break;
288                         case BTRFS_DEV_TREE_OBJECTID:
289                                 if (!uuid_tree_only)
290                                         skip = 0;
291                                 if (!skip)
292                                         printf("device");
293                                 break;
294                         case BTRFS_FS_TREE_OBJECTID:
295                                 if (!skip) {
296                                         printf("fs");
297                                 }
298                                 break;
299                         case BTRFS_ROOT_TREE_DIR_OBJECTID:
300                                 skip = 0;
301                                 printf("directory");
302                                 break;
303                         case BTRFS_CSUM_TREE_OBJECTID:
304                                 if (!skip) {
305                                         printf("checksum");
306                                 }
307                                 break;
308                         case BTRFS_ORPHAN_OBJECTID:
309                                 if (!skip) {
310                                         printf("orphan");
311                                 }
312                                 break;
313                         case BTRFS_TREE_LOG_OBJECTID:
314                                 if (!skip) {
315                                         printf("log");
316                                 }
317                                 break;
318                         case BTRFS_TREE_LOG_FIXUP_OBJECTID:
319                                 if (!skip) {
320                                         printf("log fixup");
321                                 }
322                                 break;
323                         case BTRFS_TREE_RELOC_OBJECTID:
324                                 if (!skip) {
325                                         printf("reloc");
326                                 }
327                                 break;
328                         case BTRFS_DATA_RELOC_TREE_OBJECTID:
329                                 if (!skip) {
330                                         printf("data reloc");
331                                 }
332                                 break;
333                         case BTRFS_EXTENT_CSUM_OBJECTID:
334                                 if (!skip) {
335                                         printf("extent checksum");
336                                 }
337                                 break;
338                         case BTRFS_QUOTA_TREE_OBJECTID:
339                                 if (!skip) {
340                                         printf("quota");
341                                 }
342                                 break;
343                         case BTRFS_UUID_TREE_OBJECTID:
344                                 if (!extent_only && !device_only)
345                                         skip = 0;
346                                 if (!skip)
347                                         printf("uuid");
348                                 break;
349                         case BTRFS_MULTIPLE_OBJECTIDS:
350                                 if (!skip) {
351                                         printf("multiple");
352                                 }
353                                 break;
354                         default:
355                                 if (!skip) {
356                                         printf("file");
357                                 }
358                         }
359                         if (extent_only && !skip) {
360                                 print_extents(tree_root_scan, buf);
361                         } else if (!skip) {
362                                 printf(" tree ");
363                                 btrfs_print_key(&disk_key);
364                                 if (roots_only) {
365                                         printf(" %llu level %d\n",
366                                                (unsigned long long)buf->start,
367                                                btrfs_header_level(buf));
368                                 } else {
369                                         printf(" \n");
370                                         btrfs_print_tree(tree_root_scan, buf, 1);
371                                 }
372                         }
373                         free_extent_buffer(buf);
374                 }
375 next:
376                 path.slots[0]++;
377         }
378 no_node:
379         btrfs_release_path(&path);
380
381         if (tree_root_scan == info->tree_root &&
382             info->log_root_tree) {
383                 tree_root_scan = info->log_root_tree;
384                 goto again;
385         }
386
387         if (extent_only || device_only || uuid_tree_only)
388                 goto close_root;
389
390         if (root_backups)
391                 print_old_roots(info->super_copy);
392
393         printf("total bytes %llu\n",
394                (unsigned long long)btrfs_super_total_bytes(info->super_copy));
395         printf("bytes used %llu\n",
396                (unsigned long long)btrfs_super_bytes_used(info->super_copy));
397         uuidbuf[BTRFS_UUID_UNPARSED_SIZE - 1] = '\0';
398         uuid_unparse(info->super_copy->fsid, uuidbuf);
399         printf("uuid %s\n", uuidbuf);
400         printf("%s\n", BTRFS_BUILD_VERSION);
401 close_root:
402         return close_ctree(root);
403 }