blob: a8454c401cee5d67c901071a2e6f9c6b3343b413 [file] [log] [blame]
Chris Masoneb60cea2007-02-02 09:18:22 -05001#ifndef __CTREE__
2#define __CTREE__
3
Chris Masoned2ff2c2007-03-01 18:59:40 -05004#include "list.h"
Chris Masone2fa7222007-03-12 16:22:34 -04005#include "kerncompat.h"
Chris Masoned2ff2c2007-03-01 18:59:40 -05006
Chris Masonfec577f2007-02-26 10:40:21 -05007#define CTREE_BLOCKSIZE 1024
Chris Masoneb60cea2007-02-02 09:18:22 -05008
Chris Masonfec577f2007-02-26 10:40:21 -05009/*
10 * the key defines the order in the tree, and so it also defines (optimal)
11 * block layout. objectid corresonds to the inode number. The flags
12 * tells us things about the object, and is a kind of stream selector.
13 * so for a given inode, keys with flags of 1 might refer to the inode
14 * data, flags of 2 may point to file data in the btree and flags == 3
15 * may point to extents.
16 *
17 * offset is the starting byte offset for this key in the stream.
Chris Masone2fa7222007-03-12 16:22:34 -040018 *
19 * btrfs_disk_key is in disk byte order. struct btrfs_key is always
20 * in cpu native order. Otherwise they are identical and their sizes
21 * should be the same (ie both packed)
Chris Masonfec577f2007-02-26 10:40:21 -050022 */
Chris Masone2fa7222007-03-12 16:22:34 -040023struct btrfs_disk_key {
24 __le64 objectid;
25 __le32 flags;
26 __le64 offset;
27} __attribute__ ((__packed__));
28
29struct btrfs_key {
Chris Masoneb60cea2007-02-02 09:18:22 -050030 u64 objectid;
31 u32 flags;
32 u64 offset;
33} __attribute__ ((__packed__));
34
Chris Masonfec577f2007-02-26 10:40:21 -050035/*
36 * every tree block (leaf or node) starts with this header.
37 */
Chris Masonbb492bb2007-03-12 12:29:44 -040038struct btrfs_header {
39 __le64 fsid[2]; /* FS specific uuid */
40 __le64 blocknr; /* which block this node is supposed to live in */
41 __le64 parentid; /* objectid of the tree root */
42 __le32 csum;
43 __le32 ham;
44 __le16 nritems;
45 __le16 flags;
Chris Masonfec577f2007-02-26 10:40:21 -050046 /* generation flags to be added */
Chris Masoneb60cea2007-02-02 09:18:22 -050047} __attribute__ ((__packed__));
48
Chris Mason7518a232007-03-12 12:01:18 -040049#define MAX_LEVEL 8
Chris Masonbb492bb2007-03-12 12:29:44 -040050#define NODEPTRS_PER_BLOCK ((CTREE_BLOCKSIZE - sizeof(struct btrfs_header)) / \
Chris Masone2fa7222007-03-12 16:22:34 -040051 (sizeof(struct btrfs_disk_key) + sizeof(u64)))
Chris Masoneb60cea2007-02-02 09:18:22 -050052
Chris Masoneb60cea2007-02-02 09:18:22 -050053struct tree_buffer;
Chris Masond97e63b2007-02-20 16:40:44 -050054
Chris Masonfec577f2007-02-26 10:40:21 -050055/*
56 * in ram representation of the tree. extent_root is used for all allocations
57 * and for the extent tree extent_root root. current_insert is used
58 * only for the extent tree.
59 */
Chris Masoneb60cea2007-02-02 09:18:22 -050060struct ctree_root {
61 struct tree_buffer *node;
Chris Masona28ec192007-03-06 20:08:01 -050062 struct tree_buffer *commit_root;
Chris Masond97e63b2007-02-20 16:40:44 -050063 struct ctree_root *extent_root;
Chris Masone2fa7222007-03-12 16:22:34 -040064 struct btrfs_key current_insert;
65 struct btrfs_key last_insert;
Chris Masoneb60cea2007-02-02 09:18:22 -050066 int fp;
67 struct radix_tree_root cache_radix;
Chris Masona28ec192007-03-06 20:08:01 -050068 struct radix_tree_root pinned_radix;
Chris Masoned2ff2c2007-03-01 18:59:40 -050069 struct list_head trans;
70 struct list_head cache;
71 int cache_size;
Chris Masoneb60cea2007-02-02 09:18:22 -050072};
73
Chris Masonfec577f2007-02-26 10:40:21 -050074/*
75 * describes a tree on disk
76 */
Chris Masond97e63b2007-02-20 16:40:44 -050077struct ctree_root_info {
78 u64 fsid[2]; /* FS specific uuid */
79 u64 blocknr; /* blocknr of this block */
80 u64 objectid; /* inode number of this root */
Chris Masonfec577f2007-02-26 10:40:21 -050081 u64 tree_root; /* the tree root block */
Chris Masond97e63b2007-02-20 16:40:44 -050082 u32 csum;
83 u32 ham;
Chris Masond97e63b2007-02-20 16:40:44 -050084 u64 snapuuid[2]; /* root specific uuid */
85} __attribute__ ((__packed__));
86
Chris Masonfec577f2007-02-26 10:40:21 -050087/*
88 * the super block basically lists the main trees of the FS
89 * it currently lacks any block count etc etc
90 */
Chris Masoncfaa7292007-02-21 17:04:57 -050091struct ctree_super_block {
92 struct ctree_root_info root_info;
93 struct ctree_root_info extent_info;
94} __attribute__ ((__packed__));
95
Chris Masonfec577f2007-02-26 10:40:21 -050096/*
97 * A leaf is full of items. The exact type of item is defined by
98 * the key flags parameter. offset and size tell us where to find
99 * the item in the leaf (relative to the start of the data area)
100 */
Chris Mason0783fcf2007-03-12 20:12:07 -0400101struct btrfs_item {
Chris Masone2fa7222007-03-12 16:22:34 -0400102 struct btrfs_disk_key key;
Chris Mason0783fcf2007-03-12 20:12:07 -0400103 __le16 offset;
104 __le16 size;
Chris Masoneb60cea2007-02-02 09:18:22 -0500105} __attribute__ ((__packed__));
106
Chris Masonfec577f2007-02-26 10:40:21 -0500107/*
108 * leaves have an item area and a data area:
109 * [item0, item1....itemN] [free space] [dataN...data1, data0]
110 *
111 * The data is separate from the items to get the keys closer together
112 * during searches.
113 */
Chris Masonbb492bb2007-03-12 12:29:44 -0400114#define LEAF_DATA_SIZE (CTREE_BLOCKSIZE - sizeof(struct btrfs_header))
Chris Masoneb60cea2007-02-02 09:18:22 -0500115struct leaf {
Chris Masonbb492bb2007-03-12 12:29:44 -0400116 struct btrfs_header header;
Chris Masoneb60cea2007-02-02 09:18:22 -0500117 union {
Chris Mason0783fcf2007-03-12 20:12:07 -0400118 struct btrfs_item items[LEAF_DATA_SIZE/
119 sizeof(struct btrfs_item)];
Chris Masonbb492bb2007-03-12 12:29:44 -0400120 u8 data[CTREE_BLOCKSIZE-sizeof(struct btrfs_header)];
Chris Masoneb60cea2007-02-02 09:18:22 -0500121 };
122} __attribute__ ((__packed__));
123
Chris Masonfec577f2007-02-26 10:40:21 -0500124/*
125 * all non-leaf blocks are nodes, they hold only keys and pointers to
126 * other blocks
127 */
Chris Masoneb60cea2007-02-02 09:18:22 -0500128struct node {
Chris Masonbb492bb2007-03-12 12:29:44 -0400129 struct btrfs_header header;
Chris Masone2fa7222007-03-12 16:22:34 -0400130 struct btrfs_disk_key keys[NODEPTRS_PER_BLOCK];
Chris Mason1d4f8a02007-03-13 09:28:32 -0400131 __le64 blockptrs[NODEPTRS_PER_BLOCK];
Chris Masoneb60cea2007-02-02 09:18:22 -0500132} __attribute__ ((__packed__));
133
Chris Masonfec577f2007-02-26 10:40:21 -0500134/*
135 * items in the extent btree are used to record the objectid of the
136 * owner of the block and the number of references
137 */
Chris Masond97e63b2007-02-20 16:40:44 -0500138struct extent_item {
139 u32 refs;
140 u64 owner;
141} __attribute__ ((__packed__));
142
Chris Masonfec577f2007-02-26 10:40:21 -0500143/*
144 * ctree_paths remember the path taken from the root down to the leaf.
145 * level 0 is always the leaf, and nodes[1...MAX_LEVEL] will point
146 * to any other levels that are present.
147 *
148 * The slots array records the index of the item or block pointer
149 * used while walking the tree.
150 */
Chris Masoneb60cea2007-02-02 09:18:22 -0500151struct ctree_path {
152 struct tree_buffer *nodes[MAX_LEVEL];
153 int slots[MAX_LEVEL];
154};
Chris Mason5de08d72007-02-24 06:24:44 -0500155
Chris Mason1d4f8a02007-03-13 09:28:32 -0400156static inline u64 btrfs_node_blockptr(struct node *n, int nr)
157{
158 return le64_to_cpu(n->blockptrs[nr]);
159}
160
161static inline void btrfs_set_node_blockptr(struct node *n, int nr, u64 val)
162{
163 n->blockptrs[nr] = cpu_to_le64(val);
164}
165
Chris Mason0783fcf2007-03-12 20:12:07 -0400166static inline u16 btrfs_item_offset(struct btrfs_item *item)
167{
168 return le16_to_cpu(item->offset);
169}
170
171static inline void btrfs_set_item_offset(struct btrfs_item *item, u16 val)
172{
173 item->offset = cpu_to_le16(val);
174}
175
176static inline u16 btrfs_item_end(struct btrfs_item *item)
177{
178 return le16_to_cpu(item->offset) + le16_to_cpu(item->size);
179}
180
181static inline u16 btrfs_item_size(struct btrfs_item *item)
182{
183 return le16_to_cpu(item->size);
184}
185
186static inline void btrfs_set_item_size(struct btrfs_item *item, u16 val)
187{
188 item->size = cpu_to_le16(val);
189}
190
Chris Masone2fa7222007-03-12 16:22:34 -0400191static inline void btrfs_disk_key_to_cpu(struct btrfs_key *cpu,
192 struct btrfs_disk_key *disk)
193{
194 cpu->offset = le64_to_cpu(disk->offset);
195 cpu->flags = le32_to_cpu(disk->flags);
196 cpu->objectid = le64_to_cpu(disk->objectid);
197}
198
199static inline void btrfs_cpu_key_to_disk(struct btrfs_disk_key *disk,
200 struct btrfs_key *cpu)
201{
202 disk->offset = cpu_to_le64(cpu->offset);
203 disk->flags = cpu_to_le32(cpu->flags);
204 disk->objectid = cpu_to_le64(cpu->objectid);
205}
206
207static inline u64 btrfs_key_objectid(struct btrfs_disk_key *disk)
208{
209 return le64_to_cpu(disk->objectid);
210}
211
212static inline void btrfs_set_key_objectid(struct btrfs_disk_key *disk,
213 u64 val)
214{
215 disk->objectid = cpu_to_le64(val);
216}
217
218static inline u64 btrfs_key_offset(struct btrfs_disk_key *disk)
219{
220 return le64_to_cpu(disk->offset);
221}
222
223static inline void btrfs_set_key_offset(struct btrfs_disk_key *disk,
224 u64 val)
225{
226 disk->offset = cpu_to_le64(val);
227}
228
229static inline u32 btrfs_key_flags(struct btrfs_disk_key *disk)
230{
231 return le32_to_cpu(disk->flags);
232}
233
234static inline void btrfs_set_key_flags(struct btrfs_disk_key *disk,
235 u32 val)
236{
237 disk->flags = cpu_to_le32(val);
238}
239
Chris Masonbb492bb2007-03-12 12:29:44 -0400240static inline u64 btrfs_header_blocknr(struct btrfs_header *h)
Chris Mason7518a232007-03-12 12:01:18 -0400241{
Chris Masonbb492bb2007-03-12 12:29:44 -0400242 return le64_to_cpu(h->blocknr);
Chris Mason7518a232007-03-12 12:01:18 -0400243}
244
Chris Masonbb492bb2007-03-12 12:29:44 -0400245static inline void btrfs_set_header_blocknr(struct btrfs_header *h, u64 blocknr)
Chris Mason7518a232007-03-12 12:01:18 -0400246{
Chris Masonbb492bb2007-03-12 12:29:44 -0400247 h->blocknr = cpu_to_le64(blocknr);
Chris Mason7518a232007-03-12 12:01:18 -0400248}
249
Chris Masonbb492bb2007-03-12 12:29:44 -0400250static inline u64 btrfs_header_parentid(struct btrfs_header *h)
Chris Mason7518a232007-03-12 12:01:18 -0400251{
Chris Masonbb492bb2007-03-12 12:29:44 -0400252 return le64_to_cpu(h->parentid);
Chris Mason7518a232007-03-12 12:01:18 -0400253}
254
Chris Masonbb492bb2007-03-12 12:29:44 -0400255static inline void btrfs_set_header_parentid(struct btrfs_header *h,
256 u64 parentid)
Chris Mason7518a232007-03-12 12:01:18 -0400257{
Chris Masonbb492bb2007-03-12 12:29:44 -0400258 h->parentid = cpu_to_le64(parentid);
Chris Mason7518a232007-03-12 12:01:18 -0400259}
260
Chris Masonbb492bb2007-03-12 12:29:44 -0400261static inline u16 btrfs_header_nritems(struct btrfs_header *h)
Chris Mason7518a232007-03-12 12:01:18 -0400262{
Chris Masonbb492bb2007-03-12 12:29:44 -0400263 return le16_to_cpu(h->nritems);
Chris Mason7518a232007-03-12 12:01:18 -0400264}
265
Chris Masonbb492bb2007-03-12 12:29:44 -0400266static inline void btrfs_set_header_nritems(struct btrfs_header *h, u16 val)
Chris Mason7518a232007-03-12 12:01:18 -0400267{
Chris Masonbb492bb2007-03-12 12:29:44 -0400268 h->nritems = cpu_to_le16(val);
Chris Mason7518a232007-03-12 12:01:18 -0400269}
270
Chris Masonbb492bb2007-03-12 12:29:44 -0400271static inline u16 btrfs_header_flags(struct btrfs_header *h)
Chris Mason7518a232007-03-12 12:01:18 -0400272{
Chris Masonbb492bb2007-03-12 12:29:44 -0400273 return le16_to_cpu(h->flags);
Chris Mason7518a232007-03-12 12:01:18 -0400274}
275
Chris Masonbb492bb2007-03-12 12:29:44 -0400276static inline void btrfs_set_header_flags(struct btrfs_header *h, u16 val)
Chris Mason7518a232007-03-12 12:01:18 -0400277{
Chris Masonbb492bb2007-03-12 12:29:44 -0400278 h->flags = cpu_to_le16(val);
Chris Mason7518a232007-03-12 12:01:18 -0400279}
280
Chris Masonbb492bb2007-03-12 12:29:44 -0400281static inline int btrfs_header_level(struct btrfs_header *h)
Chris Mason7518a232007-03-12 12:01:18 -0400282{
283 return btrfs_header_flags(h) & (MAX_LEVEL - 1);
284}
285
Chris Masonbb492bb2007-03-12 12:29:44 -0400286static inline void btrfs_set_header_level(struct btrfs_header *h, int level)
Chris Mason7518a232007-03-12 12:01:18 -0400287{
Chris Masonbb492bb2007-03-12 12:29:44 -0400288 u16 flags;
Chris Mason7518a232007-03-12 12:01:18 -0400289 BUG_ON(level > MAX_LEVEL);
290 flags = btrfs_header_flags(h) & ~(MAX_LEVEL - 1);
291 btrfs_set_header_flags(h, flags | level);
292}
293
294static inline int btrfs_is_leaf(struct node *n)
295{
296 return (btrfs_header_level(&n->header) == 0);
297}
298
Chris Mason5de08d72007-02-24 06:24:44 -0500299struct tree_buffer *alloc_free_block(struct ctree_root *root);
Chris Mason02217ed2007-03-02 16:08:05 -0500300int btrfs_inc_ref(struct ctree_root *root, struct tree_buffer *buf);
Chris Mason5de08d72007-02-24 06:24:44 -0500301int free_extent(struct ctree_root *root, u64 blocknr, u64 num_blocks);
Chris Masone2fa7222007-03-12 16:22:34 -0400302int search_slot(struct ctree_root *root, struct btrfs_key *key,
303 struct ctree_path *p, int ins_len, int cow);
Chris Mason5de08d72007-02-24 06:24:44 -0500304void release_path(struct ctree_root *root, struct ctree_path *p);
305void init_path(struct ctree_path *p);
306int del_item(struct ctree_root *root, struct ctree_path *path);
Chris Masone2fa7222007-03-12 16:22:34 -0400307int insert_item(struct ctree_root *root, struct btrfs_key *key,
308 void *data, int data_size);
Chris Mason5de08d72007-02-24 06:24:44 -0500309int next_leaf(struct ctree_root *root, struct ctree_path *path);
310int leaf_free_space(struct leaf *leaf);
Chris Masona28ec192007-03-06 20:08:01 -0500311int btrfs_drop_snapshot(struct ctree_root *root, struct tree_buffer *snap);
312int btrfs_finish_extent_commit(struct ctree_root *root);
Chris Masoneb60cea2007-02-02 09:18:22 -0500313#endif