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