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