blob: 05c7707263f5e21448f54ca9ff7493662eee1173 [file] [log] [blame]
Chris Mason234b63a2007-03-13 10:46:10 -04001#ifndef __BTRFS__
2#define __BTRFS__
Chris Masoneb60cea2007-02-02 09:18:22 -05003
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 Mason3768f362007-03-13 16:47:54 -04007#define BTRFS_MAGIC "_BtRfS_M"
Chris Mason234b63a2007-03-13 10:46:10 -04008#define BTRFS_BLOCKSIZE 1024
Chris Masoneb60cea2007-02-02 09:18:22 -05009
Chris Mason3768f362007-03-13 16:47:54 -040010#define BTRFS_ROOT_TREE_OBJECTID 1
11#define BTRFS_EXTENT_TREE_OBJECTID 2
12#define BTRFS_FS_TREE_OBJECTID 3
13
Chris Masonfec577f2007-02-26 10:40:21 -050014/*
15 * the key defines the order in the tree, and so it also defines (optimal)
16 * block layout. objectid corresonds to the inode number. The flags
17 * tells us things about the object, and is a kind of stream selector.
18 * so for a given inode, keys with flags of 1 might refer to the inode
19 * data, flags of 2 may point to file data in the btree and flags == 3
20 * may point to extents.
21 *
22 * offset is the starting byte offset for this key in the stream.
Chris Masone2fa7222007-03-12 16:22:34 -040023 *
24 * btrfs_disk_key is in disk byte order. struct btrfs_key is always
25 * in cpu native order. Otherwise they are identical and their sizes
26 * should be the same (ie both packed)
Chris Masonfec577f2007-02-26 10:40:21 -050027 */
Chris Masone2fa7222007-03-12 16:22:34 -040028struct btrfs_disk_key {
29 __le64 objectid;
30 __le32 flags;
31 __le64 offset;
32} __attribute__ ((__packed__));
33
34struct btrfs_key {
Chris Masoneb60cea2007-02-02 09:18:22 -050035 u64 objectid;
36 u32 flags;
37 u64 offset;
38} __attribute__ ((__packed__));
39
Chris Masonfec577f2007-02-26 10:40:21 -050040/*
41 * every tree block (leaf or node) starts with this header.
42 */
Chris Masonbb492bb2007-03-12 12:29:44 -040043struct btrfs_header {
Chris Mason3768f362007-03-13 16:47:54 -040044 u8 fsid[16]; /* FS specific uuid */
Chris Masonbb492bb2007-03-12 12:29:44 -040045 __le64 blocknr; /* which block this node is supposed to live in */
46 __le64 parentid; /* objectid of the tree root */
47 __le32 csum;
48 __le32 ham;
49 __le16 nritems;
50 __le16 flags;
Chris Masonfec577f2007-02-26 10:40:21 -050051 /* generation flags to be added */
Chris Masoneb60cea2007-02-02 09:18:22 -050052} __attribute__ ((__packed__));
53
Chris Mason234b63a2007-03-13 10:46:10 -040054#define BTRFS_MAX_LEVEL 8
55#define NODEPTRS_PER_BLOCK ((BTRFS_BLOCKSIZE - sizeof(struct btrfs_header)) / \
Chris Masone2fa7222007-03-12 16:22:34 -040056 (sizeof(struct btrfs_disk_key) + sizeof(u64)))
Chris Masoneb60cea2007-02-02 09:18:22 -050057
Chris Mason234b63a2007-03-13 10:46:10 -040058struct btrfs_buffer;
Chris Masond97e63b2007-02-20 16:40:44 -050059
Chris Mason3768f362007-03-13 16:47:54 -040060struct btrfs_root_item {
61 __le64 blocknr;
62 __le32 flags;
63 __le64 block_limit;
64 __le64 blocks_used;
65 __le32 refs;
66};
67
Chris Masonfec577f2007-02-26 10:40:21 -050068/*
69 * in ram representation of the tree. extent_root is used for all allocations
70 * and for the extent tree extent_root root. current_insert is used
71 * only for the extent tree.
72 */
Chris Mason234b63a2007-03-13 10:46:10 -040073struct btrfs_root {
74 struct btrfs_buffer *node;
75 struct btrfs_buffer *commit_root;
76 struct btrfs_root *extent_root;
Chris Mason3768f362007-03-13 16:47:54 -040077 struct btrfs_root *tree_root;
Chris Masone2fa7222007-03-12 16:22:34 -040078 struct btrfs_key current_insert;
79 struct btrfs_key last_insert;
Chris Masoneb60cea2007-02-02 09:18:22 -050080 int fp;
81 struct radix_tree_root cache_radix;
Chris Masona28ec192007-03-06 20:08:01 -050082 struct radix_tree_root pinned_radix;
Chris Masoned2ff2c2007-03-01 18:59:40 -050083 struct list_head trans;
84 struct list_head cache;
85 int cache_size;
Chris Mason3768f362007-03-13 16:47:54 -040086 int ref_cows;
87 struct btrfs_root_item root_item;
88 struct btrfs_key root_key;
Chris Masoneb60cea2007-02-02 09:18:22 -050089};
90
Chris Masonfec577f2007-02-26 10:40:21 -050091/*
Chris Masonfec577f2007-02-26 10:40:21 -050092 * the super block basically lists the main trees of the FS
93 * it currently lacks any block count etc etc
94 */
Chris Mason234b63a2007-03-13 10:46:10 -040095struct btrfs_super_block {
Chris Mason3768f362007-03-13 16:47:54 -040096 u8 fsid[16]; /* FS specific uuid */
97 __le64 blocknr; /* this block number */
98 __le32 csum;
99 __le64 magic;
100 __le16 blocksize;
101 __le64 generation;
102 __le64 root;
103 __le64 total_blocks;
104 __le64 blocks_used;
Chris Masoncfaa7292007-02-21 17:04:57 -0500105} __attribute__ ((__packed__));
106
Chris Masonfec577f2007-02-26 10:40:21 -0500107/*
108 * A leaf is full of items. The exact type of item is defined by
109 * the key flags parameter. offset and size tell us where to find
110 * the item in the leaf (relative to the start of the data area)
111 */
Chris Mason0783fcf2007-03-12 20:12:07 -0400112struct btrfs_item {
Chris Masone2fa7222007-03-12 16:22:34 -0400113 struct btrfs_disk_key key;
Chris Mason0783fcf2007-03-12 20:12:07 -0400114 __le16 offset;
115 __le16 size;
Chris Masoneb60cea2007-02-02 09:18:22 -0500116} __attribute__ ((__packed__));
117
Chris Masonfec577f2007-02-26 10:40:21 -0500118/*
119 * leaves have an item area and a data area:
120 * [item0, item1....itemN] [free space] [dataN...data1, data0]
121 *
122 * The data is separate from the items to get the keys closer together
123 * during searches.
124 */
Chris Mason234b63a2007-03-13 10:46:10 -0400125#define LEAF_DATA_SIZE (BTRFS_BLOCKSIZE - sizeof(struct btrfs_header))
126struct btrfs_leaf {
Chris Masonbb492bb2007-03-12 12:29:44 -0400127 struct btrfs_header header;
Chris Masoneb60cea2007-02-02 09:18:22 -0500128 union {
Chris Mason0783fcf2007-03-12 20:12:07 -0400129 struct btrfs_item items[LEAF_DATA_SIZE/
130 sizeof(struct btrfs_item)];
Chris Mason234b63a2007-03-13 10:46:10 -0400131 u8 data[BTRFS_BLOCKSIZE - sizeof(struct btrfs_header)];
Chris Masoneb60cea2007-02-02 09:18:22 -0500132 };
133} __attribute__ ((__packed__));
134
Chris Masonfec577f2007-02-26 10:40:21 -0500135/*
136 * all non-leaf blocks are nodes, they hold only keys and pointers to
137 * other blocks
138 */
Chris Mason234b63a2007-03-13 10:46:10 -0400139struct btrfs_node {
Chris Masonbb492bb2007-03-12 12:29:44 -0400140 struct btrfs_header header;
Chris Masone2fa7222007-03-12 16:22:34 -0400141 struct btrfs_disk_key keys[NODEPTRS_PER_BLOCK];
Chris Mason1d4f8a02007-03-13 09:28:32 -0400142 __le64 blockptrs[NODEPTRS_PER_BLOCK];
Chris Masoneb60cea2007-02-02 09:18:22 -0500143} __attribute__ ((__packed__));
144
Chris Masonfec577f2007-02-26 10:40:21 -0500145/*
146 * items in the extent btree are used to record the objectid of the
147 * owner of the block and the number of references
148 */
Chris Mason234b63a2007-03-13 10:46:10 -0400149struct btrfs_extent_item {
Chris Masoncf27e1e2007-03-13 09:49:06 -0400150 __le32 refs;
151 __le64 owner;
Chris Masond97e63b2007-02-20 16:40:44 -0500152} __attribute__ ((__packed__));
153
Chris Masonfec577f2007-02-26 10:40:21 -0500154/*
Chris Mason234b63a2007-03-13 10:46:10 -0400155 * btrfs_paths remember the path taken from the root down to the leaf.
156 * level 0 is always the leaf, and nodes[1...BTRFS_MAX_LEVEL] will point
Chris Masonfec577f2007-02-26 10:40:21 -0500157 * to any other levels that are present.
158 *
159 * The slots array records the index of the item or block pointer
160 * used while walking the tree.
161 */
Chris Mason234b63a2007-03-13 10:46:10 -0400162struct btrfs_path {
163 struct btrfs_buffer *nodes[BTRFS_MAX_LEVEL];
164 int slots[BTRFS_MAX_LEVEL];
Chris Masoneb60cea2007-02-02 09:18:22 -0500165};
Chris Mason5de08d72007-02-24 06:24:44 -0500166
Chris Mason234b63a2007-03-13 10:46:10 -0400167static inline u64 btrfs_extent_owner(struct btrfs_extent_item *ei)
Chris Masoncf27e1e2007-03-13 09:49:06 -0400168{
169 return le64_to_cpu(ei->owner);
170}
171
Chris Mason234b63a2007-03-13 10:46:10 -0400172static inline void btrfs_set_extent_owner(struct btrfs_extent_item *ei, u64 val)
Chris Masoncf27e1e2007-03-13 09:49:06 -0400173{
174 ei->owner = cpu_to_le64(val);
175}
176
Chris Mason234b63a2007-03-13 10:46:10 -0400177static inline u32 btrfs_extent_refs(struct btrfs_extent_item *ei)
Chris Masoncf27e1e2007-03-13 09:49:06 -0400178{
179 return le32_to_cpu(ei->refs);
180}
181
Chris Mason234b63a2007-03-13 10:46:10 -0400182static inline void btrfs_set_extent_refs(struct btrfs_extent_item *ei, u32 val)
Chris Masoncf27e1e2007-03-13 09:49:06 -0400183{
184 ei->refs = cpu_to_le32(val);
185}
186
Chris Mason234b63a2007-03-13 10:46:10 -0400187static inline u64 btrfs_node_blockptr(struct btrfs_node *n, int nr)
Chris Mason1d4f8a02007-03-13 09:28:32 -0400188{
189 return le64_to_cpu(n->blockptrs[nr]);
190}
191
Chris Mason234b63a2007-03-13 10:46:10 -0400192static inline void btrfs_set_node_blockptr(struct btrfs_node *n, int nr,
193 u64 val)
Chris Mason1d4f8a02007-03-13 09:28:32 -0400194{
195 n->blockptrs[nr] = cpu_to_le64(val);
196}
197
Chris Mason0783fcf2007-03-12 20:12:07 -0400198static inline u16 btrfs_item_offset(struct btrfs_item *item)
199{
200 return le16_to_cpu(item->offset);
201}
202
203static inline void btrfs_set_item_offset(struct btrfs_item *item, u16 val)
204{
205 item->offset = cpu_to_le16(val);
206}
207
208static inline u16 btrfs_item_end(struct btrfs_item *item)
209{
210 return le16_to_cpu(item->offset) + le16_to_cpu(item->size);
211}
212
213static inline u16 btrfs_item_size(struct btrfs_item *item)
214{
215 return le16_to_cpu(item->size);
216}
217
218static inline void btrfs_set_item_size(struct btrfs_item *item, u16 val)
219{
220 item->size = cpu_to_le16(val);
221}
222
Chris Masone2fa7222007-03-12 16:22:34 -0400223static inline void btrfs_disk_key_to_cpu(struct btrfs_key *cpu,
224 struct btrfs_disk_key *disk)
225{
226 cpu->offset = le64_to_cpu(disk->offset);
227 cpu->flags = le32_to_cpu(disk->flags);
228 cpu->objectid = le64_to_cpu(disk->objectid);
229}
230
231static inline void btrfs_cpu_key_to_disk(struct btrfs_disk_key *disk,
232 struct btrfs_key *cpu)
233{
234 disk->offset = cpu_to_le64(cpu->offset);
235 disk->flags = cpu_to_le32(cpu->flags);
236 disk->objectid = cpu_to_le64(cpu->objectid);
237}
238
239static inline u64 btrfs_key_objectid(struct btrfs_disk_key *disk)
240{
241 return le64_to_cpu(disk->objectid);
242}
243
244static inline void btrfs_set_key_objectid(struct btrfs_disk_key *disk,
245 u64 val)
246{
247 disk->objectid = cpu_to_le64(val);
248}
249
250static inline u64 btrfs_key_offset(struct btrfs_disk_key *disk)
251{
252 return le64_to_cpu(disk->offset);
253}
254
255static inline void btrfs_set_key_offset(struct btrfs_disk_key *disk,
256 u64 val)
257{
258 disk->offset = cpu_to_le64(val);
259}
260
261static inline u32 btrfs_key_flags(struct btrfs_disk_key *disk)
262{
263 return le32_to_cpu(disk->flags);
264}
265
266static inline void btrfs_set_key_flags(struct btrfs_disk_key *disk,
267 u32 val)
268{
269 disk->flags = cpu_to_le32(val);
270}
271
Chris Masonbb492bb2007-03-12 12:29:44 -0400272static inline u64 btrfs_header_blocknr(struct btrfs_header *h)
Chris Mason7518a232007-03-12 12:01:18 -0400273{
Chris Masonbb492bb2007-03-12 12:29:44 -0400274 return le64_to_cpu(h->blocknr);
Chris Mason7518a232007-03-12 12:01:18 -0400275}
276
Chris Masonbb492bb2007-03-12 12:29:44 -0400277static inline void btrfs_set_header_blocknr(struct btrfs_header *h, u64 blocknr)
Chris Mason7518a232007-03-12 12:01:18 -0400278{
Chris Masonbb492bb2007-03-12 12:29:44 -0400279 h->blocknr = cpu_to_le64(blocknr);
Chris Mason7518a232007-03-12 12:01:18 -0400280}
281
Chris Masonbb492bb2007-03-12 12:29:44 -0400282static inline u64 btrfs_header_parentid(struct btrfs_header *h)
Chris Mason7518a232007-03-12 12:01:18 -0400283{
Chris Masonbb492bb2007-03-12 12:29:44 -0400284 return le64_to_cpu(h->parentid);
Chris Mason7518a232007-03-12 12:01:18 -0400285}
286
Chris Masonbb492bb2007-03-12 12:29:44 -0400287static inline void btrfs_set_header_parentid(struct btrfs_header *h,
288 u64 parentid)
Chris Mason7518a232007-03-12 12:01:18 -0400289{
Chris Masonbb492bb2007-03-12 12:29:44 -0400290 h->parentid = cpu_to_le64(parentid);
Chris Mason7518a232007-03-12 12:01:18 -0400291}
292
Chris Masonbb492bb2007-03-12 12:29:44 -0400293static inline u16 btrfs_header_nritems(struct btrfs_header *h)
Chris Mason7518a232007-03-12 12:01:18 -0400294{
Chris Masonbb492bb2007-03-12 12:29:44 -0400295 return le16_to_cpu(h->nritems);
Chris Mason7518a232007-03-12 12:01:18 -0400296}
297
Chris Masonbb492bb2007-03-12 12:29:44 -0400298static inline void btrfs_set_header_nritems(struct btrfs_header *h, u16 val)
Chris Mason7518a232007-03-12 12:01:18 -0400299{
Chris Masonbb492bb2007-03-12 12:29:44 -0400300 h->nritems = cpu_to_le16(val);
Chris Mason7518a232007-03-12 12:01:18 -0400301}
302
Chris Masonbb492bb2007-03-12 12:29:44 -0400303static inline u16 btrfs_header_flags(struct btrfs_header *h)
Chris Mason7518a232007-03-12 12:01:18 -0400304{
Chris Masonbb492bb2007-03-12 12:29:44 -0400305 return le16_to_cpu(h->flags);
Chris Mason7518a232007-03-12 12:01:18 -0400306}
307
Chris Masonbb492bb2007-03-12 12:29:44 -0400308static inline void btrfs_set_header_flags(struct btrfs_header *h, u16 val)
Chris Mason7518a232007-03-12 12:01:18 -0400309{
Chris Masonbb492bb2007-03-12 12:29:44 -0400310 h->flags = cpu_to_le16(val);
Chris Mason7518a232007-03-12 12:01:18 -0400311}
312
Chris Masonbb492bb2007-03-12 12:29:44 -0400313static inline int btrfs_header_level(struct btrfs_header *h)
Chris Mason7518a232007-03-12 12:01:18 -0400314{
Chris Mason234b63a2007-03-13 10:46:10 -0400315 return btrfs_header_flags(h) & (BTRFS_MAX_LEVEL - 1);
Chris Mason7518a232007-03-12 12:01:18 -0400316}
317
Chris Masonbb492bb2007-03-12 12:29:44 -0400318static inline void btrfs_set_header_level(struct btrfs_header *h, int level)
Chris Mason7518a232007-03-12 12:01:18 -0400319{
Chris Masonbb492bb2007-03-12 12:29:44 -0400320 u16 flags;
Chris Mason234b63a2007-03-13 10:46:10 -0400321 BUG_ON(level > BTRFS_MAX_LEVEL);
322 flags = btrfs_header_flags(h) & ~(BTRFS_MAX_LEVEL - 1);
Chris Mason7518a232007-03-12 12:01:18 -0400323 btrfs_set_header_flags(h, flags | level);
324}
325
Chris Mason234b63a2007-03-13 10:46:10 -0400326static inline int btrfs_is_leaf(struct btrfs_node *n)
Chris Mason7518a232007-03-12 12:01:18 -0400327{
328 return (btrfs_header_level(&n->header) == 0);
329}
330
Chris Mason3768f362007-03-13 16:47:54 -0400331static inline u64 btrfs_root_blocknr(struct btrfs_root_item *item)
332{
333 return le64_to_cpu(item->blocknr);
334}
335
336static inline void btrfs_set_root_blocknr(struct btrfs_root_item *item, u64 val)
337{
338 item->blocknr = cpu_to_le64(val);
339}
340
341static inline u32 btrfs_root_refs(struct btrfs_root_item *item)
342{
343 return le32_to_cpu(item->refs);
344}
345
346static inline void btrfs_set_root_refs(struct btrfs_root_item *item, u32 val)
347{
348 item->refs = cpu_to_le32(val);
349}
350
351static inline u64 btrfs_super_blocknr(struct btrfs_super_block *s)
352{
353 return le64_to_cpu(s->blocknr);
354}
355
356static inline void btrfs_set_super_blocknr(struct btrfs_super_block *s, u64 val)
357{
358 s->blocknr = cpu_to_le64(val);
359}
360
361static inline u64 btrfs_super_root(struct btrfs_super_block *s)
362{
363 return le64_to_cpu(s->root);
364}
365
366static inline void btrfs_set_super_root(struct btrfs_super_block *s, u64 val)
367{
368 s->root = cpu_to_le64(val);
369}
370
371static inline u64 btrfs_super_total_blocks(struct btrfs_super_block *s)
372{
373 return le64_to_cpu(s->total_blocks);
374}
375
376static inline void btrfs_set_super_total_blocks(struct btrfs_super_block *s,
377 u64 val)
378{
379 s->total_blocks = cpu_to_le64(val);
380}
381
382static inline u64 btrfs_super_blocks_used(struct btrfs_super_block *s)
383{
384 return le64_to_cpu(s->blocks_used);
385}
386
387static inline void btrfs_set_super_blocks_used(struct btrfs_super_block *s,
388 u64 val)
389{
390 s->blocks_used = cpu_to_le64(val);
391}
392
393static inline u16 btrfs_super_blocksize(struct btrfs_super_block *s)
394{
395 return le16_to_cpu(s->blocksize);
396}
397
398static inline void btrfs_set_super_blocksize(struct btrfs_super_block *s,
399 u16 val)
400{
401 s->blocksize = cpu_to_le16(val);
402}
403
Chris Mason4beb1b82007-03-14 10:31:29 -0400404/* helper function to cast into the data area of the leaf. */
405#define btrfs_item_ptr(leaf, slot, type) \
406 ((type *)((leaf)->data + btrfs_item_offset((leaf)->items + (slot))))
407
Chris Mason234b63a2007-03-13 10:46:10 -0400408struct btrfs_buffer *btrfs_alloc_free_block(struct btrfs_root *root);
409int btrfs_inc_ref(struct btrfs_root *root, struct btrfs_buffer *buf);
410int btrfs_free_extent(struct btrfs_root *root, u64 blocknr, u64 num_blocks);
411int btrfs_search_slot(struct btrfs_root *root, struct btrfs_key *key,
412 struct btrfs_path *p, int ins_len, int cow);
413void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p);
414void btrfs_init_path(struct btrfs_path *p);
415int btrfs_del_item(struct btrfs_root *root, struct btrfs_path *path);
416int btrfs_insert_item(struct btrfs_root *root, struct btrfs_key *key,
Chris Masone2fa7222007-03-12 16:22:34 -0400417 void *data, int data_size);
Chris Mason234b63a2007-03-13 10:46:10 -0400418int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path);
419int btrfs_leaf_free_space(struct btrfs_leaf *leaf);
420int btrfs_drop_snapshot(struct btrfs_root *root, struct btrfs_buffer *snap);
421int btrfs_finish_extent_commit(struct btrfs_root *root);
Chris Mason3768f362007-03-13 16:47:54 -0400422int btrfs_del_root(struct btrfs_root *root, struct btrfs_key *key);
423int btrfs_insert_root(struct btrfs_root *root, struct btrfs_key *key,
424 struct btrfs_root_item *item);
425int btrfs_update_root(struct btrfs_root *root, struct btrfs_key *key,
426 struct btrfs_root_item *item);
427int btrfs_find_last_root(struct btrfs_root *root, u64 objectid,
428 struct btrfs_root_item *item, struct btrfs_key *key);
Chris Masoneb60cea2007-02-02 09:18:22 -0500429#endif