0a6332d16b9452f3b84fc7ba82dd9a0e8be1cbca
[platform/upstream/btrfs-progs.git] / print-tree.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "kerncompat.h"
4 #include "radix-tree.h"
5 #include "ctree.h"
6 #include "disk-io.h"
7
8 void btrfs_print_leaf(struct btrfs_root *root, struct btrfs_leaf *l)
9 {
10         int i;
11         u32 nr = btrfs_header_nritems(&l->header);
12         struct btrfs_item *item;
13         struct btrfs_extent_item *ei;
14         struct btrfs_root_item *ri;
15         struct btrfs_dir_item *di;
16         struct btrfs_inode_map_item *mi;
17         struct btrfs_inode_item *ii;
18         struct btrfs_file_extent_item *fi;
19         char *p;
20         u32 type;
21
22         printf("leaf %Lu ptrs %d free space %d parent %Lu generation %Lu\n",
23                 btrfs_header_blocknr(&l->header), nr,
24                 btrfs_leaf_free_space(root, l),
25                 btrfs_header_parentid(&l->header),
26                 btrfs_header_generation(&l->header));
27         fflush(stdout);
28         for (i = 0 ; i < nr ; i++) {
29                 item = l->items + i;
30                 type = btrfs_disk_key_type(&item->key);
31                 printf("\titem %d key (%Lu %Lu %u) itemoff %d itemsize %d\n",
32                         i,
33                         btrfs_disk_key_objectid(&item->key),
34                         btrfs_disk_key_offset(&item->key),
35                         btrfs_disk_key_flags(&item->key),
36                         btrfs_item_offset(item),
37                         btrfs_item_size(item));
38                 switch (type) {
39                 case BTRFS_INODE_ITEM_KEY:
40                         ii = btrfs_item_ptr(l, i, struct btrfs_inode_item);
41                         printf("\t\tinode generation %Lu size %Lu mode %o\n",
42                                btrfs_inode_generation(ii),
43                                btrfs_inode_size(ii),
44                                btrfs_inode_mode(ii));
45                         break;
46                 case BTRFS_INLINE_DATA_KEY:
47                         p = btrfs_item_ptr(l, i, char);
48                         printf("\t\tinline data %.*s\n", 10, p);
49                         break;
50                 case BTRFS_DIR_ITEM_KEY:
51                         di = btrfs_item_ptr(l, i, struct btrfs_dir_item);
52                         printf("\t\tdir oid %Lu flags %u type %u\n",
53                                 btrfs_disk_key_objectid(&di->location),
54                                 btrfs_dir_flags(di),
55                                 btrfs_dir_type(di));
56                         printf("\t\tname %.*s\n",
57                                btrfs_dir_name_len(di),(char *)(di + 1));
58                         break;
59                 case BTRFS_DIR_INDEX_KEY:
60                         di = btrfs_item_ptr(l, i, struct btrfs_dir_item);
61                         printf("\t\tdir index %Lu flags %u type %u\n",
62                                 btrfs_disk_key_objectid(&di->location),
63                                 btrfs_dir_flags(di),
64                                 btrfs_dir_type(di));
65                         printf("\t\tname %.*s\n",
66                                btrfs_dir_name_len(di),(char *)(di + 1));
67                         break;
68                 case BTRFS_ROOT_ITEM_KEY:
69                         ri = btrfs_item_ptr(l, i, struct btrfs_root_item);
70                         printf("\t\troot data blocknr %Lu dirid %Lu refs %u\n",
71                                 btrfs_root_blocknr(ri),
72                                 btrfs_root_dirid(ri),
73                                 btrfs_root_refs(ri));
74                         break;
75                 case BTRFS_EXTENT_ITEM_KEY:
76                         ei = btrfs_item_ptr(l, i, struct btrfs_extent_item);
77                         printf("\t\textent data refs %u owner %Lu\n",
78                                 btrfs_extent_refs(ei), btrfs_extent_owner(ei));
79                         break;
80                 case BTRFS_INODE_MAP_ITEM_KEY:
81                         mi = btrfs_item_ptr(l, i, struct btrfs_inode_map_item);
82                         printf("\t\tinode map key %Lu %Lu %u\n",
83                                btrfs_disk_key_objectid(&mi->key),
84                                btrfs_disk_key_offset(&mi->key),
85                                btrfs_disk_key_flags(&mi->key));
86                         break;
87                 case BTRFS_EXTENT_DATA_KEY:
88                         fi = btrfs_item_ptr(l, i,
89                                             struct btrfs_file_extent_item);
90                         printf("\t\textent data disk block %Lu nr %Lu\n",
91                                btrfs_file_extent_disk_blocknr(fi),
92                                btrfs_file_extent_disk_num_blocks(fi));
93                         printf("\t\textent data offset %Lu nr %Lu\n",
94                                btrfs_file_extent_offset(fi),
95                                btrfs_file_extent_num_blocks(fi));
96                         break;
97                 case BTRFS_STRING_ITEM_KEY:
98                         printf("\t\titem data %.*s\n", btrfs_item_size(item),
99                                 btrfs_leaf_data(l) + btrfs_item_offset(item));
100                         break;
101                 };
102                 fflush(stdout);
103         }
104 }
105 void btrfs_print_tree(struct btrfs_root *root, struct btrfs_buffer *t)
106 {
107         int i;
108         u32 nr;
109         struct btrfs_node *c;
110
111         if (!t)
112                 return;
113         c = &t->node;
114         nr = btrfs_header_nritems(&c->header);
115         if (btrfs_is_leaf(c)) {
116                 btrfs_print_leaf(root, (struct btrfs_leaf *)c);
117                 return;
118         }
119         printf("node %Lu level %d ptrs %d free %u parent %Lu generation %Lu\n",
120                t->blocknr,
121                 btrfs_header_level(&c->header), nr,
122                 (u32)BTRFS_NODEPTRS_PER_BLOCK(root) - nr,
123                 btrfs_header_parentid(&c->header),
124                 btrfs_header_generation(&c->header));
125         fflush(stdout);
126         for (i = 0; i < nr; i++) {
127                 printf("\tkey %d (%Lu %u %Lu) block %Lu\n",
128                        i,
129                        c->ptrs[i].key.objectid,
130                        c->ptrs[i].key.flags,
131                        c->ptrs[i].key.offset,
132                        btrfs_node_blockptr(c, i));
133                 fflush(stdout);
134         }
135         for (i = 0; i < nr; i++) {
136                 struct btrfs_buffer *next_buf = read_tree_block(root,
137                                                 btrfs_node_blockptr(c, i));
138                 struct btrfs_node *next = &next_buf->node;
139                 if (btrfs_is_leaf(next) &&
140                     btrfs_header_level(&c->header) != 1)
141                         BUG();
142                 if (btrfs_header_level(&next->header) !=
143                         btrfs_header_level(&c->header) - 1)
144                         BUG();
145                 btrfs_print_tree(root, next_buf);
146                 btrfs_block_release(root, next_buf);
147         }
148 }
149