key flag reorg
[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         struct btrfs_csum_item *ci;
20         char *p;
21         u32 type;
22
23         printf("leaf %Lu ptrs %d free space %d generation %Lu\n",
24                 btrfs_header_blocknr(&l->header), nr,
25                 btrfs_leaf_free_space(root, l),
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 %x %Lu) itemoff %d itemsize %d\n",
32                         i,
33                         btrfs_disk_key_objectid(&item->key),
34                         btrfs_disk_key_flags(&item->key),
35                         btrfs_disk_key_offset(&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\n",
78                                 btrfs_extent_refs(ei));
79                         break;
80                 case BTRFS_CSUM_ITEM_KEY:
81                         ci = btrfs_item_ptr(l, i,
82                                             struct btrfs_csum_item);
83                         printf("\t\tcsum item\n");
84                         break;
85                 case BTRFS_EXTENT_DATA_KEY:
86                         fi = btrfs_item_ptr(l, i,
87                                             struct btrfs_file_extent_item);
88                         printf("\t\textent data disk block %Lu nr %Lu\n",
89                                btrfs_file_extent_disk_blocknr(fi),
90                                btrfs_file_extent_disk_num_blocks(fi));
91                         printf("\t\textent data offset %Lu nr %Lu\n",
92                                btrfs_file_extent_offset(fi),
93                                btrfs_file_extent_num_blocks(fi));
94                         break;
95                 case BTRFS_DEV_ITEM_KEY:
96                         devi = btrfs_item_ptr(l, i, struct btrfs_device_item);
97                         printf("\t\tdev id %Lu namelen %u name %.*s\n",
98                                btrfs_device_id(devi),
99                                 btrfs_device_pathlen(devi),
100                                 btrfs_device_pathlen(devi),
101                                 (char *)(devi + 1));
102                         break;
103                 case BTRFS_STRING_ITEM_KEY:
104                         printf("\t\titem data %.*s\n", btrfs_item_size(item),
105                                 btrfs_leaf_data(l) + btrfs_item_offset(item));
106                         break;
107                 };
108                 fflush(stdout);
109         }
110 }
111 void btrfs_print_tree(struct btrfs_root *root, struct btrfs_buffer *t)
112 {
113         int i;
114         u32 nr;
115         struct btrfs_node *c;
116
117         if (!t)
118                 return;
119         c = &t->node;
120         nr = btrfs_header_nritems(&c->header);
121         if (btrfs_is_leaf(c)) {
122                 btrfs_print_leaf(root, (struct btrfs_leaf *)c);
123                 return;
124         }
125         printf("node %Lu level %d ptrs %d free %u generation %Lu\n",
126                t->blocknr,
127                 btrfs_header_level(&c->header), nr,
128                 (u32)BTRFS_NODEPTRS_PER_BLOCK(root) - nr,
129                 btrfs_header_generation(&c->header));
130         fflush(stdout);
131         for (i = 0; i < nr; i++) {
132                 printf("\tkey %d (%Lu %x %Lu) block %Lu\n",
133                        i,
134                        c->ptrs[i].key.objectid,
135                        c->ptrs[i].key.flags,
136                        c->ptrs[i].key.offset,
137                        btrfs_node_blockptr(c, i));
138                 fflush(stdout);
139         }
140         for (i = 0; i < nr; i++) {
141                 struct btrfs_buffer *next_buf = read_tree_block(root,
142                                                 btrfs_node_blockptr(c, i));
143                 struct btrfs_node *next = &next_buf->node;
144                 if (btrfs_is_leaf(next) &&
145                     btrfs_header_level(&c->header) != 1)
146                         BUG();
147                 if (btrfs_header_level(&next->header) !=
148                         btrfs_header_level(&c->header) - 1)
149                         BUG();
150                 btrfs_print_tree(root, next_buf);
151                 btrfs_block_release(root, next_buf);
152         }
153 }
154