create a root dir in mkfs
[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         u32 type;
19
20         printf("leaf %Lu total ptrs %d free space %d\n",
21                 btrfs_header_blocknr(&l->header), nr,
22                 btrfs_leaf_free_space(root, l));
23         fflush(stdout);
24         for (i = 0 ; i < nr ; i++) {
25                 item = l->items + i;
26                 type = btrfs_disk_key_type(&item->key);
27                 printf("\titem %d key (%Lu %u %Lu) itemoff %d itemsize %d\n",
28                         i,
29                         btrfs_disk_key_objectid(&item->key),
30                         btrfs_disk_key_flags(&item->key),
31                         btrfs_disk_key_offset(&item->key),
32                         btrfs_item_offset(item),
33                         btrfs_item_size(item));
34                 switch (type) {
35                 case BTRFS_INODE_ITEM_KEY:
36                         ii = btrfs_item_ptr(l, i, struct btrfs_inode_item);
37                         printf("\t\tinode generation %Lu size %Lu mode %o\n",
38                                btrfs_inode_generation(ii),
39                                btrfs_inode_size(ii),
40                                btrfs_inode_mode(ii));
41                         break;
42                 case BTRFS_DIR_ITEM_KEY:
43                         di = btrfs_item_ptr(l, i, struct btrfs_dir_item);
44                         printf("\t\tdir oid %Lu flags %u type %u\n",
45                                 btrfs_dir_objectid(di),
46                                 btrfs_dir_flags(di),
47                                 btrfs_dir_type(di));
48                         printf("\t\tname %.*s\n",
49                                btrfs_dir_name_len(di),(char *)(di + 1));
50                         break;
51                 case BTRFS_ROOT_ITEM_KEY:
52                         ri = btrfs_item_ptr(l, i, struct btrfs_root_item);
53                         printf("\t\troot data blocknr %Lu refs %u\n",
54                                 btrfs_root_blocknr(ri), btrfs_root_refs(ri));
55                         break;
56                 case BTRFS_EXTENT_ITEM_KEY:
57                         ei = btrfs_item_ptr(l, i, struct btrfs_extent_item);
58                         printf("\t\textent data refs %u owner %Lu\n",
59                                 btrfs_extent_refs(ei), btrfs_extent_owner(ei));
60                         break;
61                 case BTRFS_INODE_MAP_ITEM_KEY:
62                         mi = btrfs_item_ptr(l, i, struct btrfs_inode_map_item);
63                         printf("\t\tinode map key %Lu %u %Lu\n",
64                                btrfs_disk_key_objectid(&mi->key),
65                                btrfs_disk_key_flags(&mi->key),
66                                btrfs_disk_key_offset(&mi->key));
67                         break;
68                 case BTRFS_STRING_ITEM_KEY:
69                         printf("\t\titem data %.*s\n", btrfs_item_size(item),
70                                 btrfs_leaf_data(l) + btrfs_item_offset(item));
71                         break;
72                 };
73                 fflush(stdout);
74         }
75 }
76 void btrfs_print_tree(struct btrfs_root *root, struct btrfs_buffer *t)
77 {
78         int i;
79         u32 nr;
80         struct btrfs_node *c;
81
82         if (!t)
83                 return;
84         c = &t->node;
85         nr = btrfs_header_nritems(&c->header);
86         if (btrfs_is_leaf(c)) {
87                 btrfs_print_leaf(root, (struct btrfs_leaf *)c);
88                 return;
89         }
90         printf("node %Lu level %d total ptrs %d free spc %u\n", t->blocknr,
91                 btrfs_header_level(&c->header), nr,
92                 (u32)BTRFS_NODEPTRS_PER_BLOCK(root) - nr);
93         fflush(stdout);
94         for (i = 0; i < nr; i++) {
95                 printf("\tkey %d (%Lu %u %Lu) block %Lu\n",
96                        i,
97                        c->ptrs[i].key.objectid,
98                        c->ptrs[i].key.flags,
99                        c->ptrs[i].key.offset,
100                        btrfs_node_blockptr(c, i));
101                 fflush(stdout);
102         }
103         for (i = 0; i < nr; i++) {
104                 struct btrfs_buffer *next_buf = read_tree_block(root,
105                                                 btrfs_node_blockptr(c, i));
106                 struct btrfs_node *next = &next_buf->node;
107                 if (btrfs_is_leaf(next) &&
108                     btrfs_header_level(&c->header) != 1)
109                         BUG();
110                 if (btrfs_header_level(&next->header) !=
111                         btrfs_header_level(&c->header) - 1)
112                         BUG();
113                 btrfs_print_tree(root, next_buf);
114                 btrfs_block_release(root, next_buf);
115         }
116 }
117