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