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