]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/btrfs/ctree.h
Btrfs: merge leaves before split
[net-next-2.6.git] / fs / btrfs / ctree.h
CommitLineData
234b63a0
CM
1#ifndef __BTRFS__
2#define __BTRFS__
eb60ceac 3
ed2ff2cb 4#include "list.h"
e2fa7227 5#include "kerncompat.h"
ed2ff2cb 6
234b63a0 7#define BTRFS_BLOCKSIZE 1024
eb60ceac 8
fec577fb
CM
9/*
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.
e2fa7227
CM
18 *
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)
fec577fb 22 */
e2fa7227
CM
23struct btrfs_disk_key {
24 __le64 objectid;
25 __le32 flags;
26 __le64 offset;
27} __attribute__ ((__packed__));
28
29struct btrfs_key {
eb60ceac
CM
30 u64 objectid;
31 u32 flags;
32 u64 offset;
33} __attribute__ ((__packed__));
34
fec577fb
CM
35/*
36 * every tree block (leaf or node) starts with this header.
37 */
bb492bb0
CM
38struct 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;
fec577fb 46 /* generation flags to be added */
eb60ceac
CM
47} __attribute__ ((__packed__));
48
234b63a0
CM
49#define BTRFS_MAX_LEVEL 8
50#define NODEPTRS_PER_BLOCK ((BTRFS_BLOCKSIZE - sizeof(struct btrfs_header)) / \
e2fa7227 51 (sizeof(struct btrfs_disk_key) + sizeof(u64)))
eb60ceac 52
234b63a0 53struct btrfs_buffer;
d97e63b6 54
fec577fb
CM
55/*
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 */
234b63a0
CM
60struct btrfs_root {
61 struct btrfs_buffer *node;
62 struct btrfs_buffer *commit_root;
63 struct btrfs_root *extent_root;
e2fa7227
CM
64 struct btrfs_key current_insert;
65 struct btrfs_key last_insert;
eb60ceac
CM
66 int fp;
67 struct radix_tree_root cache_radix;
a28ec197 68 struct radix_tree_root pinned_radix;
ed2ff2cb
CM
69 struct list_head trans;
70 struct list_head cache;
71 int cache_size;
eb60ceac
CM
72};
73
fec577fb
CM
74/*
75 * describes a tree on disk
76 */
234b63a0 77struct btrfs_root_info {
d97e63b6
CM
78 u64 fsid[2]; /* FS specific uuid */
79 u64 blocknr; /* blocknr of this block */
80 u64 objectid; /* inode number of this root */
fec577fb 81 u64 tree_root; /* the tree root block */
d97e63b6
CM
82 u32 csum;
83 u32 ham;
d97e63b6
CM
84 u64 snapuuid[2]; /* root specific uuid */
85} __attribute__ ((__packed__));
86
fec577fb
CM
87/*
88 * the super block basically lists the main trees of the FS
89 * it currently lacks any block count etc etc
90 */
234b63a0
CM
91struct btrfs_super_block {
92 struct btrfs_root_info root_info;
93 struct btrfs_root_info extent_info;
cfaa7295
CM
94} __attribute__ ((__packed__));
95
fec577fb
CM
96/*
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 */
0783fcfc 101struct btrfs_item {
e2fa7227 102 struct btrfs_disk_key key;
0783fcfc
CM
103 __le16 offset;
104 __le16 size;
eb60ceac
CM
105} __attribute__ ((__packed__));
106
fec577fb
CM
107/*
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 */
234b63a0
CM
114#define LEAF_DATA_SIZE (BTRFS_BLOCKSIZE - sizeof(struct btrfs_header))
115struct btrfs_leaf {
bb492bb0 116 struct btrfs_header header;
eb60ceac 117 union {
0783fcfc
CM
118 struct btrfs_item items[LEAF_DATA_SIZE/
119 sizeof(struct btrfs_item)];
234b63a0 120 u8 data[BTRFS_BLOCKSIZE - sizeof(struct btrfs_header)];
eb60ceac
CM
121 };
122} __attribute__ ((__packed__));
123
fec577fb
CM
124/*
125 * all non-leaf blocks are nodes, they hold only keys and pointers to
126 * other blocks
127 */
234b63a0 128struct btrfs_node {
bb492bb0 129 struct btrfs_header header;
e2fa7227 130 struct btrfs_disk_key keys[NODEPTRS_PER_BLOCK];
1d4f8a0c 131 __le64 blockptrs[NODEPTRS_PER_BLOCK];
eb60ceac
CM
132} __attribute__ ((__packed__));
133
fec577fb
CM
134/*
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 */
234b63a0 138struct btrfs_extent_item {
cf27e1ee
CM
139 __le32 refs;
140 __le64 owner;
d97e63b6
CM
141} __attribute__ ((__packed__));
142
fec577fb 143/*
234b63a0
CM
144 * btrfs_paths remember the path taken from the root down to the leaf.
145 * level 0 is always the leaf, and nodes[1...BTRFS_MAX_LEVEL] will point
fec577fb
CM
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 */
234b63a0
CM
151struct btrfs_path {
152 struct btrfs_buffer *nodes[BTRFS_MAX_LEVEL];
153 int slots[BTRFS_MAX_LEVEL];
eb60ceac 154};
5de08d7d 155
234b63a0 156static inline u64 btrfs_extent_owner(struct btrfs_extent_item *ei)
cf27e1ee
CM
157{
158 return le64_to_cpu(ei->owner);
159}
160
234b63a0 161static inline void btrfs_set_extent_owner(struct btrfs_extent_item *ei, u64 val)
cf27e1ee
CM
162{
163 ei->owner = cpu_to_le64(val);
164}
165
234b63a0 166static inline u32 btrfs_extent_refs(struct btrfs_extent_item *ei)
cf27e1ee
CM
167{
168 return le32_to_cpu(ei->refs);
169}
170
234b63a0 171static inline void btrfs_set_extent_refs(struct btrfs_extent_item *ei, u32 val)
cf27e1ee
CM
172{
173 ei->refs = cpu_to_le32(val);
174}
175
234b63a0 176static inline u64 btrfs_node_blockptr(struct btrfs_node *n, int nr)
1d4f8a0c
CM
177{
178 return le64_to_cpu(n->blockptrs[nr]);
179}
180
234b63a0
CM
181static inline void btrfs_set_node_blockptr(struct btrfs_node *n, int nr,
182 u64 val)
1d4f8a0c
CM
183{
184 n->blockptrs[nr] = cpu_to_le64(val);
185}
186
0783fcfc
CM
187static inline u16 btrfs_item_offset(struct btrfs_item *item)
188{
189 return le16_to_cpu(item->offset);
190}
191
192static inline void btrfs_set_item_offset(struct btrfs_item *item, u16 val)
193{
194 item->offset = cpu_to_le16(val);
195}
196
197static inline u16 btrfs_item_end(struct btrfs_item *item)
198{
199 return le16_to_cpu(item->offset) + le16_to_cpu(item->size);
200}
201
202static inline u16 btrfs_item_size(struct btrfs_item *item)
203{
204 return le16_to_cpu(item->size);
205}
206
207static inline void btrfs_set_item_size(struct btrfs_item *item, u16 val)
208{
209 item->size = cpu_to_le16(val);
210}
211
e2fa7227
CM
212static inline void btrfs_disk_key_to_cpu(struct btrfs_key *cpu,
213 struct btrfs_disk_key *disk)
214{
215 cpu->offset = le64_to_cpu(disk->offset);
216 cpu->flags = le32_to_cpu(disk->flags);
217 cpu->objectid = le64_to_cpu(disk->objectid);
218}
219
220static inline void btrfs_cpu_key_to_disk(struct btrfs_disk_key *disk,
221 struct btrfs_key *cpu)
222{
223 disk->offset = cpu_to_le64(cpu->offset);
224 disk->flags = cpu_to_le32(cpu->flags);
225 disk->objectid = cpu_to_le64(cpu->objectid);
226}
227
228static inline u64 btrfs_key_objectid(struct btrfs_disk_key *disk)
229{
230 return le64_to_cpu(disk->objectid);
231}
232
233static inline void btrfs_set_key_objectid(struct btrfs_disk_key *disk,
234 u64 val)
235{
236 disk->objectid = cpu_to_le64(val);
237}
238
239static inline u64 btrfs_key_offset(struct btrfs_disk_key *disk)
240{
241 return le64_to_cpu(disk->offset);
242}
243
244static inline void btrfs_set_key_offset(struct btrfs_disk_key *disk,
245 u64 val)
246{
247 disk->offset = cpu_to_le64(val);
248}
249
250static inline u32 btrfs_key_flags(struct btrfs_disk_key *disk)
251{
252 return le32_to_cpu(disk->flags);
253}
254
255static inline void btrfs_set_key_flags(struct btrfs_disk_key *disk,
256 u32 val)
257{
258 disk->flags = cpu_to_le32(val);
259}
260
bb492bb0 261static inline u64 btrfs_header_blocknr(struct btrfs_header *h)
7518a238 262{
bb492bb0 263 return le64_to_cpu(h->blocknr);
7518a238
CM
264}
265
bb492bb0 266static inline void btrfs_set_header_blocknr(struct btrfs_header *h, u64 blocknr)
7518a238 267{
bb492bb0 268 h->blocknr = cpu_to_le64(blocknr);
7518a238
CM
269}
270
bb492bb0 271static inline u64 btrfs_header_parentid(struct btrfs_header *h)
7518a238 272{
bb492bb0 273 return le64_to_cpu(h->parentid);
7518a238
CM
274}
275
bb492bb0
CM
276static inline void btrfs_set_header_parentid(struct btrfs_header *h,
277 u64 parentid)
7518a238 278{
bb492bb0 279 h->parentid = cpu_to_le64(parentid);
7518a238
CM
280}
281
bb492bb0 282static inline u16 btrfs_header_nritems(struct btrfs_header *h)
7518a238 283{
bb492bb0 284 return le16_to_cpu(h->nritems);
7518a238
CM
285}
286
bb492bb0 287static inline void btrfs_set_header_nritems(struct btrfs_header *h, u16 val)
7518a238 288{
bb492bb0 289 h->nritems = cpu_to_le16(val);
7518a238
CM
290}
291
bb492bb0 292static inline u16 btrfs_header_flags(struct btrfs_header *h)
7518a238 293{
bb492bb0 294 return le16_to_cpu(h->flags);
7518a238
CM
295}
296
bb492bb0 297static inline void btrfs_set_header_flags(struct btrfs_header *h, u16 val)
7518a238 298{
bb492bb0 299 h->flags = cpu_to_le16(val);
7518a238
CM
300}
301
bb492bb0 302static inline int btrfs_header_level(struct btrfs_header *h)
7518a238 303{
234b63a0 304 return btrfs_header_flags(h) & (BTRFS_MAX_LEVEL - 1);
7518a238
CM
305}
306
bb492bb0 307static inline void btrfs_set_header_level(struct btrfs_header *h, int level)
7518a238 308{
bb492bb0 309 u16 flags;
234b63a0
CM
310 BUG_ON(level > BTRFS_MAX_LEVEL);
311 flags = btrfs_header_flags(h) & ~(BTRFS_MAX_LEVEL - 1);
7518a238
CM
312 btrfs_set_header_flags(h, flags | level);
313}
314
234b63a0 315static inline int btrfs_is_leaf(struct btrfs_node *n)
7518a238
CM
316{
317 return (btrfs_header_level(&n->header) == 0);
318}
319
234b63a0
CM
320struct btrfs_buffer *btrfs_alloc_free_block(struct btrfs_root *root);
321int btrfs_inc_ref(struct btrfs_root *root, struct btrfs_buffer *buf);
322int btrfs_free_extent(struct btrfs_root *root, u64 blocknr, u64 num_blocks);
323int btrfs_search_slot(struct btrfs_root *root, struct btrfs_key *key,
324 struct btrfs_path *p, int ins_len, int cow);
325void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p);
326void btrfs_init_path(struct btrfs_path *p);
327int btrfs_del_item(struct btrfs_root *root, struct btrfs_path *path);
328int btrfs_insert_item(struct btrfs_root *root, struct btrfs_key *key,
e2fa7227 329 void *data, int data_size);
234b63a0
CM
330int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path);
331int btrfs_leaf_free_space(struct btrfs_leaf *leaf);
332int btrfs_drop_snapshot(struct btrfs_root *root, struct btrfs_buffer *snap);
333int btrfs_finish_extent_commit(struct btrfs_root *root);
eb60ceac 334#endif