Add debug-tree -e to print all allocated extents, and show-blocks to graph them
[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
30 static int print_usage(void)
31 {
32         fprintf(stderr, "usage: debug-tree [ -e ] device\n");
33         exit(1);
34 }
35
36 static void print_extent_leaf(struct btrfs_root *root, struct extent_buffer *l)
37 {
38         int i;
39         struct btrfs_item *item;
40         struct btrfs_extent_ref *ref;
41         struct btrfs_key key;
42         static u64 last = 0;
43         static u64 last_len = 0;
44         u32 nr = btrfs_header_nritems(l);
45         u32 type;
46
47         for (i = 0 ; i < nr ; i++) {
48                 item = btrfs_item_nr(l, i);
49                 btrfs_item_key_to_cpu(l, &key, i);
50                 type = btrfs_key_type(&key);
51                 switch (type) {
52                 case BTRFS_EXTENT_ITEM_KEY:
53                         last_len = key.offset;
54                         last = key.objectid;
55                         break;
56                 case BTRFS_EXTENT_REF_KEY:
57                         ref = btrfs_item_ptr(l, i, struct btrfs_extent_ref);
58                         printf("%llu %llu extent back ref root %llu gen %llu "
59                                "owner %llu offset %llu\n",
60                                (unsigned long long)last,
61                                (unsigned long long)last_len,
62                                (unsigned long long)btrfs_ref_root(l, ref),
63                                (unsigned long long)btrfs_ref_generation(l, ref),
64                                (unsigned long long)btrfs_ref_objectid(l, ref),
65                                (unsigned long long)btrfs_ref_offset(l, ref));
66                         break;
67                 };
68                 fflush(stdout);
69         }
70 }
71
72 static void print_extents(struct btrfs_root *root, struct extent_buffer *eb)
73 {
74         int i;
75         u32 nr;
76         u32 size;
77
78         if (!eb)
79                 return;
80         if (btrfs_is_leaf(eb)) {
81                 print_extent_leaf(root, eb);
82                 return;
83         }
84         size = btrfs_level_size(root, btrfs_header_level(eb) - 1);
85         nr = btrfs_header_nritems(eb);
86         for (i = 0; i < nr; i++) {
87                 struct extent_buffer *next = read_tree_block(root,
88                                              btrfs_node_blockptr(eb, i),
89                                              size);
90                 if (btrfs_is_leaf(next) &&
91                     btrfs_header_level(eb) != 1)
92                         BUG();
93                 if (btrfs_header_level(next) !=
94                         btrfs_header_level(eb) - 1)
95                         BUG();
96                 print_extents(root, next);
97                 free_extent_buffer(next);
98         }
99 }
100
101 int main(int ac, char **av)
102 {
103         struct btrfs_root *root;
104         struct btrfs_path path;
105         struct btrfs_key key;
106         struct btrfs_root_item ri;
107         struct extent_buffer *leaf;
108         struct btrfs_key found_key;
109         char uuidbuf[37];
110         int ret;
111         int slot;
112         int extent_only = 0;
113
114         radix_tree_init();
115
116         while(1) {
117                 int c;
118                 c = getopt(ac, av, "e");
119                 if (c < 0)
120                         break;
121                 switch(c) {
122                         case 'e':
123                                 extent_only = 1;
124                                 break;
125                         default:
126                                 print_usage();
127                 }
128         }
129         ac = ac - optind;
130         if (ac != 1)
131                 print_usage();
132
133         root = open_ctree(av[optind], 0);
134         if (!root) {
135                 fprintf(stderr, "unable to open %s\n", av[optind]);
136                 exit(1);
137         }
138         if (!extent_only) {
139                 printf("root tree\n");
140                 btrfs_print_tree(root->fs_info->tree_root,
141                                  root->fs_info->tree_root->node);
142         }
143         btrfs_init_path(&path);
144         key.offset = 0;
145         key.objectid = 0;
146         btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
147         ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
148                                         &key, &path, 0, 0);
149         BUG_ON(ret < 0);
150         while(1) {
151                 leaf = path.nodes[0];
152                 slot = path.slots[0];
153                 if (slot >= btrfs_header_nritems(leaf)) {
154                         ret = btrfs_next_leaf(root, &path);
155                         if (ret != 0)
156                                 break;
157                         leaf = path.nodes[0];
158                         slot = path.slots[0];
159                 }
160                 btrfs_item_key_to_cpu(leaf, &found_key, path.slots[0]);
161                 if (btrfs_key_type(&found_key) == BTRFS_ROOT_ITEM_KEY) {
162                         unsigned long offset;
163                         struct extent_buffer *buf;
164                         int skip = extent_only;
165
166                         offset = btrfs_item_ptr_offset(leaf, slot);
167                         read_extent_buffer(leaf, &ri, offset, sizeof(ri));
168                         buf = read_tree_block(root->fs_info->tree_root,
169                                               btrfs_root_bytenr(&ri),
170                                               root->leafsize);
171                         switch(found_key.objectid) {
172                         case BTRFS_ROOT_TREE_OBJECTID:
173                                 printf("root ");
174                                 break;
175                         case BTRFS_EXTENT_TREE_OBJECTID:
176                                 skip = 0;
177                                 if (!extent_only)
178                                         printf("extent tree ");
179                                 break;
180                         }
181                         if (!skip && !extent_only) {
182                                 printf("tree %llu %u %llu\n",
183                                        (unsigned long long)found_key.objectid,
184                                        found_key.type,
185                                        (unsigned long long)found_key.offset);
186                                 btrfs_print_tree(root, buf);
187                         } else if (extent_only && !skip) {
188                                 print_extents(root, buf);
189                         }
190                 }
191                 path.slots[0]++;
192         }
193         btrfs_release_path(root, &path);
194         if (extent_only)
195                 return 0;
196
197         printf("total bytes %llu\n",
198                (unsigned long long)btrfs_super_total_bytes(&root->fs_info->super_copy));
199         printf("bytes used %llu\n",
200                (unsigned long long)btrfs_super_bytes_used(&root->fs_info->super_copy));
201         uuidbuf[36] = '\0';
202         uuid_unparse(root->fs_info->super_copy.fsid, uuidbuf);
203         printf("uuid %s\n", uuidbuf);
204         return 0;
205 }