Switch to byte granular allocations
[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 <uuid/uuid.h>
22 #include "kerncompat.h"
23 #include "radix-tree.h"
24 #include "ctree.h"
25 #include "disk-io.h"
26 #include "print-tree.h"
27 #include "transaction.h"
28
29 int main(int ac, char **av) {
30         struct btrfs_super_block super;
31         struct btrfs_root *root;
32         struct btrfs_path path;
33         struct btrfs_key key;
34         struct btrfs_root_item *ri;
35         struct btrfs_leaf *leaf;
36         struct btrfs_key found_key;
37         char uuidbuf[37];
38         int ret;
39         int slot;
40
41         if (ac != 2) {
42                 fprintf(stderr, "usage: %s device\n", av[0]);
43                 exit(1);
44         }
45         radix_tree_init();
46         root = open_ctree(av[1], &super);
47         if (!root) {
48                 fprintf(stderr, "unable to open %s\n", av[1]);
49                 exit(1);
50         }
51         printf("root tree\n");
52         btrfs_print_tree(root->fs_info->tree_root,
53                          root->fs_info->tree_root->node);
54         btrfs_init_path(&path);
55         key.offset = 0;
56         key.objectid = 0;
57         btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
58         ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
59                                         &key, &path, 0, 0);
60         BUG_ON(ret < 0);
61         while(1) {
62                 leaf = &path.nodes[0]->leaf;
63                 slot = path.slots[0];
64                 if (slot >= btrfs_header_nritems(&leaf->header)) {
65                         ret = btrfs_next_leaf(root, &path);
66                         if (ret != 0)
67                                 break;
68                         leaf = &path.nodes[0]->leaf;
69                         slot = path.slots[0];
70                 }
71                 btrfs_disk_key_to_cpu(&found_key,
72                                       &leaf->items[path.slots[0]].key);
73                 if (btrfs_key_type(&found_key) == BTRFS_ROOT_ITEM_KEY) {
74                         struct btrfs_buffer *buf;
75                         ri = btrfs_item_ptr(leaf, path.slots[0],
76                                             struct btrfs_root_item);
77                         buf = read_tree_block(root->fs_info->tree_root,
78                                               btrfs_root_bytenr(ri),
79                                               root->leafsize);
80                         switch(found_key.objectid) {
81                         case BTRFS_ROOT_TREE_OBJECTID:
82                                 printf("root ");
83                                 break;
84                         case BTRFS_EXTENT_TREE_OBJECTID:
85                                 printf("extent tree ");
86                                 break;
87                         }
88                         printf("tree %llu %u %llu\n",
89                                (unsigned long long)found_key.objectid,
90                                found_key.type,
91                                (unsigned long long)found_key.offset);
92                         btrfs_print_tree(root, buf);
93                 }
94                 path.slots[0]++;
95         }
96         btrfs_release_path(root, &path);
97         printf("total bytes %llu\n",
98                (unsigned long long)btrfs_super_total_bytes(&super));
99         printf("bytes used %llu\n",
100                (unsigned long long)btrfs_super_bytes_used(&super));
101         uuidbuf[36] = '\0';
102         uuid_unparse(super.fsid, uuidbuf);
103         printf("uuid %s\n", uuidbuf);
104         return 0;
105 }