Commit | Line | Data |
---|---|---|
eb60ceac CM |
1 | #ifndef __CTREE__ |
2 | #define __CTREE__ | |
3 | ||
fec577fb | 4 | #define CTREE_BLOCKSIZE 1024 |
eb60ceac | 5 | |
fec577fb CM |
6 | /* |
7 | * the key defines the order in the tree, and so it also defines (optimal) | |
8 | * block layout. objectid corresonds to the inode number. The flags | |
9 | * tells us things about the object, and is a kind of stream selector. | |
10 | * so for a given inode, keys with flags of 1 might refer to the inode | |
11 | * data, flags of 2 may point to file data in the btree and flags == 3 | |
12 | * may point to extents. | |
13 | * | |
14 | * offset is the starting byte offset for this key in the stream. | |
15 | */ | |
eb60ceac CM |
16 | struct key { |
17 | u64 objectid; | |
18 | u32 flags; | |
19 | u64 offset; | |
20 | } __attribute__ ((__packed__)); | |
21 | ||
fec577fb CM |
22 | /* |
23 | * every tree block (leaf or node) starts with this header. | |
24 | */ | |
eb60ceac CM |
25 | struct header { |
26 | u64 fsid[2]; /* FS specific uuid */ | |
fec577fb CM |
27 | u64 blocknr; /* which block this node is supposed to live in */ |
28 | u64 parentid; /* objectid of the tree root */ | |
eb60ceac CM |
29 | u32 csum; |
30 | u32 ham; | |
31 | u16 nritems; | |
32 | u16 flags; | |
fec577fb | 33 | /* generation flags to be added */ |
eb60ceac CM |
34 | } __attribute__ ((__packed__)); |
35 | ||
36 | #define NODEPTRS_PER_BLOCK ((CTREE_BLOCKSIZE - sizeof(struct header)) / \ | |
37 | (sizeof(struct key) + sizeof(u64))) | |
38 | ||
d97e63b6 | 39 | #define MAX_LEVEL 8 |
eb60ceac CM |
40 | #define node_level(f) ((f) & (MAX_LEVEL-1)) |
41 | #define is_leaf(f) (node_level(f) == 0) | |
42 | ||
43 | struct tree_buffer; | |
d97e63b6 | 44 | |
fec577fb CM |
45 | /* |
46 | * in ram representation of the tree. extent_root is used for all allocations | |
47 | * and for the extent tree extent_root root. current_insert is used | |
48 | * only for the extent tree. | |
49 | */ | |
eb60ceac CM |
50 | struct ctree_root { |
51 | struct tree_buffer *node; | |
d97e63b6 | 52 | struct ctree_root *extent_root; |
9a8dd150 | 53 | struct key current_insert; |
eb60ceac CM |
54 | int fp; |
55 | struct radix_tree_root cache_radix; | |
56 | }; | |
57 | ||
fec577fb CM |
58 | /* |
59 | * describes a tree on disk | |
60 | */ | |
d97e63b6 CM |
61 | struct ctree_root_info { |
62 | u64 fsid[2]; /* FS specific uuid */ | |
63 | u64 blocknr; /* blocknr of this block */ | |
64 | u64 objectid; /* inode number of this root */ | |
fec577fb | 65 | u64 tree_root; /* the tree root block */ |
d97e63b6 CM |
66 | u32 csum; |
67 | u32 ham; | |
d97e63b6 CM |
68 | u64 snapuuid[2]; /* root specific uuid */ |
69 | } __attribute__ ((__packed__)); | |
70 | ||
fec577fb CM |
71 | /* |
72 | * the super block basically lists the main trees of the FS | |
73 | * it currently lacks any block count etc etc | |
74 | */ | |
cfaa7295 CM |
75 | struct ctree_super_block { |
76 | struct ctree_root_info root_info; | |
77 | struct ctree_root_info extent_info; | |
78 | } __attribute__ ((__packed__)); | |
79 | ||
fec577fb CM |
80 | /* |
81 | * A leaf is full of items. The exact type of item is defined by | |
82 | * the key flags parameter. offset and size tell us where to find | |
83 | * the item in the leaf (relative to the start of the data area) | |
84 | */ | |
eb60ceac CM |
85 | struct item { |
86 | struct key key; | |
87 | u16 offset; | |
88 | u16 size; | |
89 | } __attribute__ ((__packed__)); | |
90 | ||
fec577fb CM |
91 | /* |
92 | * leaves have an item area and a data area: | |
93 | * [item0, item1....itemN] [free space] [dataN...data1, data0] | |
94 | * | |
95 | * The data is separate from the items to get the keys closer together | |
96 | * during searches. | |
97 | */ | |
eb60ceac CM |
98 | #define LEAF_DATA_SIZE (CTREE_BLOCKSIZE - sizeof(struct header)) |
99 | struct leaf { | |
100 | struct header header; | |
101 | union { | |
102 | struct item items[LEAF_DATA_SIZE/sizeof(struct item)]; | |
103 | u8 data[CTREE_BLOCKSIZE-sizeof(struct header)]; | |
104 | }; | |
105 | } __attribute__ ((__packed__)); | |
106 | ||
fec577fb CM |
107 | /* |
108 | * all non-leaf blocks are nodes, they hold only keys and pointers to | |
109 | * other blocks | |
110 | */ | |
eb60ceac CM |
111 | struct node { |
112 | struct header header; | |
113 | struct key keys[NODEPTRS_PER_BLOCK]; | |
114 | u64 blockptrs[NODEPTRS_PER_BLOCK]; | |
115 | } __attribute__ ((__packed__)); | |
116 | ||
fec577fb CM |
117 | /* |
118 | * items in the extent btree are used to record the objectid of the | |
119 | * owner of the block and the number of references | |
120 | */ | |
d97e63b6 CM |
121 | struct extent_item { |
122 | u32 refs; | |
123 | u64 owner; | |
124 | } __attribute__ ((__packed__)); | |
125 | ||
fec577fb CM |
126 | /* |
127 | * ctree_paths remember the path taken from the root down to the leaf. | |
128 | * level 0 is always the leaf, and nodes[1...MAX_LEVEL] will point | |
129 | * to any other levels that are present. | |
130 | * | |
131 | * The slots array records the index of the item or block pointer | |
132 | * used while walking the tree. | |
133 | */ | |
eb60ceac CM |
134 | struct ctree_path { |
135 | struct tree_buffer *nodes[MAX_LEVEL]; | |
136 | int slots[MAX_LEVEL]; | |
137 | }; | |
5de08d7d CM |
138 | |
139 | struct tree_buffer *alloc_free_block(struct ctree_root *root); | |
140 | int free_extent(struct ctree_root *root, u64 blocknr, u64 num_blocks); | |
141 | int search_slot(struct ctree_root *root, struct key *key, struct ctree_path *p, int ins_len); | |
142 | void release_path(struct ctree_root *root, struct ctree_path *p); | |
143 | void init_path(struct ctree_path *p); | |
144 | int del_item(struct ctree_root *root, struct ctree_path *path); | |
145 | int insert_item(struct ctree_root *root, struct key *key, void *data, int data_size); | |
146 | int next_leaf(struct ctree_root *root, struct ctree_path *path); | |
147 | int leaf_free_space(struct leaf *leaf); | |
eb60ceac | 148 | #endif |