Add rollback support for the converter
[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_root *root;
31         struct btrfs_path path;
32         struct btrfs_key key;
33         struct btrfs_root_item ri;
34         struct extent_buffer *leaf;
35         struct btrfs_key found_key;
36         char uuidbuf[37];
37         int ret;
38         int slot;
39
40         if (ac != 2) {
41                 fprintf(stderr, "usage: %s device\n", av[0]);
42                 exit(1);
43         }
44         radix_tree_init();
45         root = open_ctree(av[1], 0);
46         if (!root) {
47                 fprintf(stderr, "unable to open %s\n", av[1]);
48                 exit(1);
49         }
50         printf("root tree\n");
51         btrfs_print_tree(root->fs_info->tree_root,
52                          root->fs_info->tree_root->node);
53         btrfs_init_path(&path);
54         key.offset = 0;
55         key.objectid = 0;
56         btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
57         ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
58                                         &key, &path, 0, 0);
59         BUG_ON(ret < 0);
60         while(1) {
61                 leaf = path.nodes[0];
62                 slot = path.slots[0];
63                 if (slot >= btrfs_header_nritems(leaf)) {
64                         ret = btrfs_next_leaf(root, &path);
65                         if (ret != 0)
66                                 break;
67                         leaf = path.nodes[0];
68                         slot = path.slots[0];
69                 }
70                 btrfs_item_key_to_cpu(leaf, &found_key, path.slots[0]);
71                 if (btrfs_key_type(&found_key) == BTRFS_ROOT_ITEM_KEY) {
72                         unsigned long offset;
73                         struct extent_buffer *buf;
74                         offset = btrfs_item_ptr_offset(leaf, slot);
75                         read_extent_buffer(leaf, &ri, offset, sizeof(ri));
76                         buf = read_tree_block(root->fs_info->tree_root,
77                                               btrfs_root_bytenr(&ri),
78                                               root->leafsize);
79                         switch(found_key.objectid) {
80                         case BTRFS_ROOT_TREE_OBJECTID:
81                                 printf("root ");
82                                 break;
83                         case BTRFS_EXTENT_TREE_OBJECTID:
84                                 printf("extent tree ");
85                                 break;
86                         }
87                         printf("tree %llu %u %llu\n",
88                                (unsigned long long)found_key.objectid,
89                                found_key.type,
90                                (unsigned long long)found_key.offset);
91                         btrfs_print_tree(root, buf);
92                 }
93                 path.slots[0]++;
94         }
95         btrfs_release_path(root, &path);
96         printf("total bytes %llu\n",
97                (unsigned long long)btrfs_super_total_bytes(&root->fs_info->super_copy));
98         printf("bytes used %llu\n",
99                (unsigned long long)btrfs_super_bytes_used(&root->fs_info->super_copy));
100         uuidbuf[36] = '\0';
101         uuid_unparse(root->fs_info->super_copy.fsid, uuidbuf);
102         printf("uuid %s\n", uuidbuf);
103         return 0;
104 }