blob: 68f0af39777f14c07db95fd85706cb7f52325b85 [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 Masone089f052007-03-16 16:20:31 -04007struct btrfs_trans_handle;
8
Chris Mason3768f362007-03-13 16:47:54 -04009#define BTRFS_MAGIC "_BtRfS_M"
Chris Masoneb60cea2007-02-02 09:18:22 -050010
Chris Mason3768f362007-03-13 16:47:54 -040011#define BTRFS_ROOT_TREE_OBJECTID 1
12#define BTRFS_EXTENT_TREE_OBJECTID 2
13#define BTRFS_FS_TREE_OBJECTID 3
14
Chris Masonfec577f2007-02-26 10:40:21 -050015/*
16 * the key defines the order in the tree, and so it also defines (optimal)
17 * block layout. objectid corresonds to the inode number. The flags
18 * tells us things about the object, and is a kind of stream selector.
19 * so for a given inode, keys with flags of 1 might refer to the inode
20 * data, flags of 2 may point to file data in the btree and flags == 3
21 * may point to extents.
22 *
23 * offset is the starting byte offset for this key in the stream.
Chris Masone2fa7222007-03-12 16:22:34 -040024 *
25 * btrfs_disk_key is in disk byte order. struct btrfs_key is always
26 * in cpu native order. Otherwise they are identical and their sizes
27 * should be the same (ie both packed)
Chris Masonfec577f2007-02-26 10:40:21 -050028 */
Chris Masone2fa7222007-03-12 16:22:34 -040029struct btrfs_disk_key {
30 __le64 objectid;
Chris Masona1516c82007-03-14 14:26:53 -040031 __le32 flags;
Chris Masona8a2ee02007-03-16 08:46:49 -040032 __le64 offset;
Chris Masone2fa7222007-03-12 16:22:34 -040033} __attribute__ ((__packed__));
34
35struct btrfs_key {
Chris Masoneb60cea2007-02-02 09:18:22 -050036 u64 objectid;
Chris Masona1516c82007-03-14 14:26:53 -040037 u32 flags;
Chris Masona8a2ee02007-03-16 08:46:49 -040038 u64 offset;
Chris Masoneb60cea2007-02-02 09:18:22 -050039} __attribute__ ((__packed__));
40
Chris Masonfec577f2007-02-26 10:40:21 -050041/*
42 * every tree block (leaf or node) starts with this header.
43 */
Chris Masonbb492bb2007-03-12 12:29:44 -040044struct btrfs_header {
Chris Mason3768f362007-03-13 16:47:54 -040045 u8 fsid[16]; /* FS specific uuid */
Chris Masonbb492bb2007-03-12 12:29:44 -040046 __le64 blocknr; /* which block this node is supposed to live in */
47 __le64 parentid; /* objectid of the tree root */
48 __le32 csum;
49 __le32 ham;
50 __le16 nritems;
51 __le16 flags;
Chris Masonfec577f2007-02-26 10:40:21 -050052 /* generation flags to be added */
Chris Masoneb60cea2007-02-02 09:18:22 -050053} __attribute__ ((__packed__));
54
Chris Mason234b63a2007-03-13 10:46:10 -040055#define BTRFS_MAX_LEVEL 8
Chris Mason123abc82007-03-14 14:14:43 -040056#define BTRFS_NODEPTRS_PER_BLOCK(r) (((r)->blocksize - \
57 sizeof(struct btrfs_header)) / \
58 (sizeof(struct btrfs_disk_key) + sizeof(u64)))
59#define __BTRFS_LEAF_DATA_SIZE(bs) ((bs) - sizeof(struct btrfs_header))
60#define BTRFS_LEAF_DATA_SIZE(r) (__BTRFS_LEAF_DATA_SIZE(r->blocksize))
Chris Masoneb60cea2007-02-02 09:18:22 -050061
Chris Mason234b63a2007-03-13 10:46:10 -040062struct btrfs_buffer;
Chris Masonfec577f2007-02-26 10:40:21 -050063/*
Chris Masonfec577f2007-02-26 10:40:21 -050064 * the super block basically lists the main trees of the FS
65 * it currently lacks any block count etc etc
66 */
Chris Mason234b63a2007-03-13 10:46:10 -040067struct btrfs_super_block {
Chris Mason3768f362007-03-13 16:47:54 -040068 u8 fsid[16]; /* FS specific uuid */
69 __le64 blocknr; /* this block number */
70 __le32 csum;
71 __le64 magic;
Chris Mason123abc82007-03-14 14:14:43 -040072 __le32 blocksize;
Chris Mason3768f362007-03-13 16:47:54 -040073 __le64 generation;
74 __le64 root;
75 __le64 total_blocks;
76 __le64 blocks_used;
Chris Masoncfaa7292007-02-21 17:04:57 -050077} __attribute__ ((__packed__));
78
Chris Masonfec577f2007-02-26 10:40:21 -050079/*
Chris Mason62e27492007-03-15 12:56:47 -040080 * A leaf is full of items. offset and size tell us where to find
Chris Masonfec577f2007-02-26 10:40:21 -050081 * the item in the leaf (relative to the start of the data area)
82 */
Chris Mason0783fcf2007-03-12 20:12:07 -040083struct btrfs_item {
Chris Masone2fa7222007-03-12 16:22:34 -040084 struct btrfs_disk_key key;
Chris Mason123abc82007-03-14 14:14:43 -040085 __le32 offset;
Chris Mason0783fcf2007-03-12 20:12:07 -040086 __le16 size;
Chris Masoneb60cea2007-02-02 09:18:22 -050087} __attribute__ ((__packed__));
88
Chris Masonfec577f2007-02-26 10:40:21 -050089/*
90 * leaves have an item area and a data area:
91 * [item0, item1....itemN] [free space] [dataN...data1, data0]
92 *
93 * The data is separate from the items to get the keys closer together
94 * during searches.
95 */
Chris Mason234b63a2007-03-13 10:46:10 -040096struct btrfs_leaf {
Chris Masonbb492bb2007-03-12 12:29:44 -040097 struct btrfs_header header;
Chris Mason123abc82007-03-14 14:14:43 -040098 struct btrfs_item items[];
Chris Masoneb60cea2007-02-02 09:18:22 -050099} __attribute__ ((__packed__));
100
Chris Masonfec577f2007-02-26 10:40:21 -0500101/*
102 * all non-leaf blocks are nodes, they hold only keys and pointers to
103 * other blocks
104 */
Chris Mason123abc82007-03-14 14:14:43 -0400105struct btrfs_key_ptr {
106 struct btrfs_disk_key key;
107 __le64 blockptr;
108} __attribute__ ((__packed__));
109
Chris Mason234b63a2007-03-13 10:46:10 -0400110struct btrfs_node {
Chris Masonbb492bb2007-03-12 12:29:44 -0400111 struct btrfs_header header;
Chris Mason123abc82007-03-14 14:14:43 -0400112 struct btrfs_key_ptr ptrs[];
Chris Masoneb60cea2007-02-02 09:18:22 -0500113} __attribute__ ((__packed__));
114
Chris Masonfec577f2007-02-26 10:40:21 -0500115/*
Chris Mason234b63a2007-03-13 10:46:10 -0400116 * btrfs_paths remember the path taken from the root down to the leaf.
117 * level 0 is always the leaf, and nodes[1...BTRFS_MAX_LEVEL] will point
Chris Masonfec577f2007-02-26 10:40:21 -0500118 * to any other levels that are present.
119 *
120 * The slots array records the index of the item or block pointer
121 * used while walking the tree.
122 */
Chris Mason234b63a2007-03-13 10:46:10 -0400123struct btrfs_path {
124 struct btrfs_buffer *nodes[BTRFS_MAX_LEVEL];
125 int slots[BTRFS_MAX_LEVEL];
Chris Masoneb60cea2007-02-02 09:18:22 -0500126};
Chris Mason5de08d72007-02-24 06:24:44 -0500127
Chris Mason62e27492007-03-15 12:56:47 -0400128/*
129 * items in the extent btree are used to record the objectid of the
130 * owner of the block and the number of references
131 */
132struct btrfs_extent_item {
133 __le32 refs;
134 __le64 owner;
135} __attribute__ ((__packed__));
136
Chris Mason1e1d2702007-03-15 19:03:33 -0400137struct btrfs_inode_timespec {
138 __le32 sec;
139 __le32 nsec;
140} __attribute__ ((__packed__));
141
142/*
143 * there is no padding here on purpose. If you want to extent the inode,
144 * make a new item type
145 */
146struct btrfs_inode_item {
147 __le64 generation;
148 __le64 size;
149 __le64 nblocks;
150 __le32 nlink;
151 __le32 uid;
152 __le32 gid;
153 __le32 mode;
154 __le32 rdev;
155 __le16 flags;
156 __le16 compat_flags;
157 struct btrfs_inode_timespec atime;
158 struct btrfs_inode_timespec ctime;
159 struct btrfs_inode_timespec mtime;
160 struct btrfs_inode_timespec otime;
161} __attribute__ ((__packed__));
162
163/* inline data is just a blob of bytes */
164struct btrfs_inline_data_item {
165 u8 data;
166} __attribute__ ((__packed__));
167
Chris Mason62e27492007-03-15 12:56:47 -0400168struct btrfs_dir_item {
169 __le64 objectid;
170 __le16 flags;
Chris Masona8a2ee02007-03-16 08:46:49 -0400171 __le16 name_len;
Chris Mason62e27492007-03-15 12:56:47 -0400172 u8 type;
173} __attribute__ ((__packed__));
174
175struct btrfs_root_item {
176 __le64 blocknr;
177 __le32 flags;
178 __le64 block_limit;
179 __le64 blocks_used;
180 __le32 refs;
181};
182
183/*
184 * in ram representation of the tree. extent_root is used for all allocations
185 * and for the extent tree extent_root root. current_insert is used
186 * only for the extent tree.
187 */
188struct btrfs_root {
189 struct btrfs_buffer *node;
190 struct btrfs_buffer *commit_root;
191 struct btrfs_root *extent_root;
192 struct btrfs_root *tree_root;
193 struct btrfs_key current_insert;
194 struct btrfs_key last_insert;
195 int fp;
196 struct radix_tree_root cache_radix;
197 struct radix_tree_root pinned_radix;
198 struct list_head trans;
199 struct list_head cache;
200 int cache_size;
201 int ref_cows;
202 struct btrfs_root_item root_item;
203 struct btrfs_key root_key;
204 u32 blocksize;
Chris Masone089f052007-03-16 16:20:31 -0400205 struct btrfs_trans_handle *running_transaction;
Chris Mason62e27492007-03-15 12:56:47 -0400206};
207
Chris Mason62e27492007-03-15 12:56:47 -0400208/* the lower bits in the key flags defines the item type */
209#define BTRFS_KEY_TYPE_MAX 256
210#define BTRFS_KEY_TYPE_MASK (BTRFS_KEY_TYPE_MAX - 1)
Chris Mason1e1d2702007-03-15 19:03:33 -0400211
212/*
213 * inode items have the data typically returned from stat and store other
214 * info about object characteristics. There is one for every file and dir in
215 * the FS
216 */
Chris Mason62e27492007-03-15 12:56:47 -0400217#define BTRFS_INODE_ITEM_KEY 1
Chris Mason1e1d2702007-03-15 19:03:33 -0400218
219/*
220 * dir items are the name -> inode pointers in a directory. There is one
221 * for every name in a directory.
222 */
Chris Mason62e27492007-03-15 12:56:47 -0400223#define BTRFS_DIR_ITEM_KEY 2
Chris Mason1e1d2702007-03-15 19:03:33 -0400224/*
225 * inline data is file data that fits in the btree.
226 */
227#define BTRFS_INLINE_DATA_KEY 3
228/*
229 * extent data is for data that can't fit in the btree. It points to
230 * a (hopefully) huge chunk of disk
231 */
232#define BTRFS_EXTENT_DATA_KEY 4
233/*
234 * root items point to tree roots. There are typically in the root
235 * tree used by the super block to find all the other trees
236 */
237#define BTRFS_ROOT_ITEM_KEY 5
238/*
239 * extent items are in the extent map tree. These record which blocks
240 * are used, and how many references there are to each block
241 */
242#define BTRFS_EXTENT_ITEM_KEY 6
243/*
244 * string items are for debugging. They just store a short string of
245 * data in the FS
246 */
247#define BTRFS_STRING_ITEM_KEY 7
248
249static inline u64 btrfs_inode_generation(struct btrfs_inode_item *i)
250{
251 return le64_to_cpu(i->generation);
252}
253
254static inline void btrfs_set_inode_generation(struct btrfs_inode_item *i,
255 u64 val)
256{
257 i->generation = cpu_to_le64(val);
258}
259
260static inline u64 btrfs_inode_size(struct btrfs_inode_item *i)
261{
262 return le64_to_cpu(i->size);
263}
264
265static inline void btrfs_set_inode_size(struct btrfs_inode_item *i, u64 val)
266{
267 i->size = cpu_to_le64(val);
268}
269
270static inline u64 btrfs_inode_nblocks(struct btrfs_inode_item *i)
271{
272 return le64_to_cpu(i->nblocks);
273}
274
275static inline void btrfs_set_inode_nblocks(struct btrfs_inode_item *i, u64 val)
276{
277 i->nblocks = cpu_to_le64(val);
278}
279
280static inline u32 btrfs_inode_nlink(struct btrfs_inode_item *i)
281{
282 return le32_to_cpu(i->nlink);
283}
284
285static inline void btrfs_set_inode_nlink(struct btrfs_inode_item *i, u32 val)
286{
287 i->nlink = cpu_to_le32(val);
288}
289
290static inline u32 btrfs_inode_uid(struct btrfs_inode_item *i)
291{
292 return le32_to_cpu(i->uid);
293}
294
295static inline void btrfs_set_inode_uid(struct btrfs_inode_item *i, u32 val)
296{
297 i->uid = cpu_to_le32(val);
298}
299
300static inline u32 btrfs_inode_gid(struct btrfs_inode_item *i)
301{
302 return le32_to_cpu(i->gid);
303}
304
305static inline void btrfs_set_inode_gid(struct btrfs_inode_item *i, u32 val)
306{
307 i->gid = cpu_to_le32(val);
308}
309
310static inline u32 btrfs_inode_mode(struct btrfs_inode_item *i)
311{
312 return le32_to_cpu(i->mode);
313}
314
315static inline void btrfs_set_inode_mode(struct btrfs_inode_item *i, u32 val)
316{
317 i->mode = cpu_to_le32(val);
318}
319
320static inline u32 btrfs_inode_rdev(struct btrfs_inode_item *i)
321{
322 return le32_to_cpu(i->rdev);
323}
324
325static inline void btrfs_set_inode_rdev(struct btrfs_inode_item *i, u32 val)
326{
327 i->rdev = cpu_to_le32(val);
328}
329
330static inline u16 btrfs_inode_flags(struct btrfs_inode_item *i)
331{
332 return le16_to_cpu(i->flags);
333}
334
335static inline void btrfs_set_inode_flags(struct btrfs_inode_item *i, u16 val)
336{
337 i->flags = cpu_to_le16(val);
338}
339
340static inline u16 btrfs_inode_compat_flags(struct btrfs_inode_item *i)
341{
342 return le16_to_cpu(i->compat_flags);
343}
344
345static inline void btrfs_set_inode_compat_flags(struct btrfs_inode_item *i,
346 u16 val)
347{
348 i->compat_flags = cpu_to_le16(val);
349}
350
Chris Mason62e27492007-03-15 12:56:47 -0400351
Chris Mason234b63a2007-03-13 10:46:10 -0400352static inline u64 btrfs_extent_owner(struct btrfs_extent_item *ei)
Chris Masoncf27e1e2007-03-13 09:49:06 -0400353{
354 return le64_to_cpu(ei->owner);
355}
356
Chris Mason234b63a2007-03-13 10:46:10 -0400357static inline void btrfs_set_extent_owner(struct btrfs_extent_item *ei, u64 val)
Chris Masoncf27e1e2007-03-13 09:49:06 -0400358{
359 ei->owner = cpu_to_le64(val);
360}
361
Chris Mason234b63a2007-03-13 10:46:10 -0400362static inline u32 btrfs_extent_refs(struct btrfs_extent_item *ei)
Chris Masoncf27e1e2007-03-13 09:49:06 -0400363{
364 return le32_to_cpu(ei->refs);
365}
366
Chris Mason234b63a2007-03-13 10:46:10 -0400367static inline void btrfs_set_extent_refs(struct btrfs_extent_item *ei, u32 val)
Chris Masoncf27e1e2007-03-13 09:49:06 -0400368{
369 ei->refs = cpu_to_le32(val);
370}
371
Chris Mason234b63a2007-03-13 10:46:10 -0400372static inline u64 btrfs_node_blockptr(struct btrfs_node *n, int nr)
Chris Mason1d4f8a02007-03-13 09:28:32 -0400373{
Chris Mason123abc82007-03-14 14:14:43 -0400374 return le64_to_cpu(n->ptrs[nr].blockptr);
Chris Mason1d4f8a02007-03-13 09:28:32 -0400375}
376
Chris Mason234b63a2007-03-13 10:46:10 -0400377static inline void btrfs_set_node_blockptr(struct btrfs_node *n, int nr,
378 u64 val)
Chris Mason1d4f8a02007-03-13 09:28:32 -0400379{
Chris Mason123abc82007-03-14 14:14:43 -0400380 n->ptrs[nr].blockptr = cpu_to_le64(val);
Chris Mason1d4f8a02007-03-13 09:28:32 -0400381}
382
Chris Mason123abc82007-03-14 14:14:43 -0400383static inline u32 btrfs_item_offset(struct btrfs_item *item)
Chris Mason0783fcf2007-03-12 20:12:07 -0400384{
Chris Mason123abc82007-03-14 14:14:43 -0400385 return le32_to_cpu(item->offset);
Chris Mason0783fcf2007-03-12 20:12:07 -0400386}
387
Chris Mason123abc82007-03-14 14:14:43 -0400388static inline void btrfs_set_item_offset(struct btrfs_item *item, u32 val)
Chris Mason0783fcf2007-03-12 20:12:07 -0400389{
Chris Mason123abc82007-03-14 14:14:43 -0400390 item->offset = cpu_to_le32(val);
Chris Mason0783fcf2007-03-12 20:12:07 -0400391}
392
Chris Mason123abc82007-03-14 14:14:43 -0400393static inline u32 btrfs_item_end(struct btrfs_item *item)
Chris Mason0783fcf2007-03-12 20:12:07 -0400394{
Chris Mason123abc82007-03-14 14:14:43 -0400395 return le32_to_cpu(item->offset) + le16_to_cpu(item->size);
Chris Mason0783fcf2007-03-12 20:12:07 -0400396}
397
398static inline u16 btrfs_item_size(struct btrfs_item *item)
399{
400 return le16_to_cpu(item->size);
401}
402
403static inline void btrfs_set_item_size(struct btrfs_item *item, u16 val)
404{
405 item->size = cpu_to_le16(val);
406}
407
Chris Mason1d4f6402007-03-15 15:18:43 -0400408static inline u64 btrfs_dir_objectid(struct btrfs_dir_item *d)
409{
410 return le64_to_cpu(d->objectid);
411}
412
413static inline void btrfs_set_dir_objectid(struct btrfs_dir_item *d, u64 val)
414{
415 d->objectid = cpu_to_le64(val);
416}
417
418static inline u16 btrfs_dir_flags(struct btrfs_dir_item *d)
419{
420 return le16_to_cpu(d->flags);
421}
422
423static inline void btrfs_set_dir_flags(struct btrfs_dir_item *d, u16 val)
424{
425 d->flags = cpu_to_le16(val);
426}
427
428static inline u8 btrfs_dir_type(struct btrfs_dir_item *d)
429{
430 return d->type;
431}
432
433static inline void btrfs_set_dir_type(struct btrfs_dir_item *d, u8 val)
434{
435 d->type = val;
436}
437
Chris Masona8a2ee02007-03-16 08:46:49 -0400438static inline u16 btrfs_dir_name_len(struct btrfs_dir_item *d)
Chris Mason1d4f6402007-03-15 15:18:43 -0400439{
Chris Masona8a2ee02007-03-16 08:46:49 -0400440 return le16_to_cpu(d->name_len);
441}
442
443static inline void btrfs_set_dir_name_len(struct btrfs_dir_item *d, u16 val)
444{
445 d->name_len = cpu_to_le16(val);
Chris Mason1d4f6402007-03-15 15:18:43 -0400446}
447
Chris Masone2fa7222007-03-12 16:22:34 -0400448static inline void btrfs_disk_key_to_cpu(struct btrfs_key *cpu,
449 struct btrfs_disk_key *disk)
450{
451 cpu->offset = le64_to_cpu(disk->offset);
452 cpu->flags = le32_to_cpu(disk->flags);
453 cpu->objectid = le64_to_cpu(disk->objectid);
454}
455
456static inline void btrfs_cpu_key_to_disk(struct btrfs_disk_key *disk,
457 struct btrfs_key *cpu)
458{
459 disk->offset = cpu_to_le64(cpu->offset);
460 disk->flags = cpu_to_le32(cpu->flags);
461 disk->objectid = cpu_to_le64(cpu->objectid);
462}
463
Chris Mason62e27492007-03-15 12:56:47 -0400464static inline u64 btrfs_disk_key_objectid(struct btrfs_disk_key *disk)
Chris Masone2fa7222007-03-12 16:22:34 -0400465{
466 return le64_to_cpu(disk->objectid);
467}
468
Chris Mason62e27492007-03-15 12:56:47 -0400469static inline void btrfs_set_disk_key_objectid(struct btrfs_disk_key *disk,
470 u64 val)
Chris Masone2fa7222007-03-12 16:22:34 -0400471{
472 disk->objectid = cpu_to_le64(val);
473}
474
Chris Mason62e27492007-03-15 12:56:47 -0400475static inline u64 btrfs_disk_key_offset(struct btrfs_disk_key *disk)
Chris Masone2fa7222007-03-12 16:22:34 -0400476{
477 return le64_to_cpu(disk->offset);
478}
479
Chris Mason62e27492007-03-15 12:56:47 -0400480static inline void btrfs_set_disk_key_offset(struct btrfs_disk_key *disk,
481 u64 val)
Chris Masone2fa7222007-03-12 16:22:34 -0400482{
483 disk->offset = cpu_to_le64(val);
484}
485
Chris Mason62e27492007-03-15 12:56:47 -0400486static inline u32 btrfs_disk_key_flags(struct btrfs_disk_key *disk)
Chris Masone2fa7222007-03-12 16:22:34 -0400487{
488 return le32_to_cpu(disk->flags);
489}
490
Chris Mason62e27492007-03-15 12:56:47 -0400491static inline void btrfs_set_disk_key_flags(struct btrfs_disk_key *disk,
492 u32 val)
Chris Masone2fa7222007-03-12 16:22:34 -0400493{
494 disk->flags = cpu_to_le32(val);
495}
496
Chris Mason62e27492007-03-15 12:56:47 -0400497static inline u32 btrfs_key_type(struct btrfs_key *key)
498{
499 return key->flags & BTRFS_KEY_TYPE_MASK;
500}
501
502static inline u32 btrfs_disk_key_type(struct btrfs_disk_key *key)
503{
504 return le32_to_cpu(key->flags) & BTRFS_KEY_TYPE_MASK;
505}
506
507static inline void btrfs_set_key_type(struct btrfs_key *key, u32 type)
508{
509 BUG_ON(type >= BTRFS_KEY_TYPE_MAX);
510 key->flags = (key->flags & ~((u64)BTRFS_KEY_TYPE_MASK)) | type;
511}
512
513static inline void btrfs_set_disk_key_type(struct btrfs_disk_key *key, u32 type)
514{
515 u32 flags = btrfs_disk_key_flags(key);
516 BUG_ON(type >= BTRFS_KEY_TYPE_MAX);
517 flags = (flags & ~((u64)BTRFS_KEY_TYPE_MASK)) | type;
518 btrfs_set_disk_key_flags(key, flags);
519}
520
Chris Masonbb492bb2007-03-12 12:29:44 -0400521static inline u64 btrfs_header_blocknr(struct btrfs_header *h)
Chris Mason7518a232007-03-12 12:01:18 -0400522{
Chris Masonbb492bb2007-03-12 12:29:44 -0400523 return le64_to_cpu(h->blocknr);
Chris Mason7518a232007-03-12 12:01:18 -0400524}
525
Chris Masonbb492bb2007-03-12 12:29:44 -0400526static inline void btrfs_set_header_blocknr(struct btrfs_header *h, u64 blocknr)
Chris Mason7518a232007-03-12 12:01:18 -0400527{
Chris Masonbb492bb2007-03-12 12:29:44 -0400528 h->blocknr = cpu_to_le64(blocknr);
Chris Mason7518a232007-03-12 12:01:18 -0400529}
530
Chris Masonbb492bb2007-03-12 12:29:44 -0400531static inline u64 btrfs_header_parentid(struct btrfs_header *h)
Chris Mason7518a232007-03-12 12:01:18 -0400532{
Chris Masonbb492bb2007-03-12 12:29:44 -0400533 return le64_to_cpu(h->parentid);
Chris Mason7518a232007-03-12 12:01:18 -0400534}
535
Chris Masonbb492bb2007-03-12 12:29:44 -0400536static inline void btrfs_set_header_parentid(struct btrfs_header *h,
537 u64 parentid)
Chris Mason7518a232007-03-12 12:01:18 -0400538{
Chris Masonbb492bb2007-03-12 12:29:44 -0400539 h->parentid = cpu_to_le64(parentid);
Chris Mason7518a232007-03-12 12:01:18 -0400540}
541
Chris Masonbb492bb2007-03-12 12:29:44 -0400542static inline u16 btrfs_header_nritems(struct btrfs_header *h)
Chris Mason7518a232007-03-12 12:01:18 -0400543{
Chris Masonbb492bb2007-03-12 12:29:44 -0400544 return le16_to_cpu(h->nritems);
Chris Mason7518a232007-03-12 12:01:18 -0400545}
546
Chris Masonbb492bb2007-03-12 12:29:44 -0400547static inline void btrfs_set_header_nritems(struct btrfs_header *h, u16 val)
Chris Mason7518a232007-03-12 12:01:18 -0400548{
Chris Masonbb492bb2007-03-12 12:29:44 -0400549 h->nritems = cpu_to_le16(val);
Chris Mason7518a232007-03-12 12:01:18 -0400550}
551
Chris Masonbb492bb2007-03-12 12:29:44 -0400552static inline u16 btrfs_header_flags(struct btrfs_header *h)
Chris Mason7518a232007-03-12 12:01:18 -0400553{
Chris Masonbb492bb2007-03-12 12:29:44 -0400554 return le16_to_cpu(h->flags);
Chris Mason7518a232007-03-12 12:01:18 -0400555}
556
Chris Masonbb492bb2007-03-12 12:29:44 -0400557static inline void btrfs_set_header_flags(struct btrfs_header *h, u16 val)
Chris Mason7518a232007-03-12 12:01:18 -0400558{
Chris Masonbb492bb2007-03-12 12:29:44 -0400559 h->flags = cpu_to_le16(val);
Chris Mason7518a232007-03-12 12:01:18 -0400560}
561
Chris Masonbb492bb2007-03-12 12:29:44 -0400562static inline int btrfs_header_level(struct btrfs_header *h)
Chris Mason7518a232007-03-12 12:01:18 -0400563{
Chris Mason234b63a2007-03-13 10:46:10 -0400564 return btrfs_header_flags(h) & (BTRFS_MAX_LEVEL - 1);
Chris Mason7518a232007-03-12 12:01:18 -0400565}
566
Chris Masonbb492bb2007-03-12 12:29:44 -0400567static inline void btrfs_set_header_level(struct btrfs_header *h, int level)
Chris Mason7518a232007-03-12 12:01:18 -0400568{
Chris Masonbb492bb2007-03-12 12:29:44 -0400569 u16 flags;
Chris Mason234b63a2007-03-13 10:46:10 -0400570 BUG_ON(level > BTRFS_MAX_LEVEL);
571 flags = btrfs_header_flags(h) & ~(BTRFS_MAX_LEVEL - 1);
Chris Mason7518a232007-03-12 12:01:18 -0400572 btrfs_set_header_flags(h, flags | level);
573}
574
Chris Mason234b63a2007-03-13 10:46:10 -0400575static inline int btrfs_is_leaf(struct btrfs_node *n)
Chris Mason7518a232007-03-12 12:01:18 -0400576{
577 return (btrfs_header_level(&n->header) == 0);
578}
579
Chris Mason3768f362007-03-13 16:47:54 -0400580static inline u64 btrfs_root_blocknr(struct btrfs_root_item *item)
581{
582 return le64_to_cpu(item->blocknr);
583}
584
585static inline void btrfs_set_root_blocknr(struct btrfs_root_item *item, u64 val)
586{
587 item->blocknr = cpu_to_le64(val);
588}
589
590static inline u32 btrfs_root_refs(struct btrfs_root_item *item)
591{
592 return le32_to_cpu(item->refs);
593}
594
595static inline void btrfs_set_root_refs(struct btrfs_root_item *item, u32 val)
596{
597 item->refs = cpu_to_le32(val);
598}
599
600static inline u64 btrfs_super_blocknr(struct btrfs_super_block *s)
601{
602 return le64_to_cpu(s->blocknr);
603}
604
605static inline void btrfs_set_super_blocknr(struct btrfs_super_block *s, u64 val)
606{
607 s->blocknr = cpu_to_le64(val);
608}
609
610static inline u64 btrfs_super_root(struct btrfs_super_block *s)
611{
612 return le64_to_cpu(s->root);
613}
614
615static inline void btrfs_set_super_root(struct btrfs_super_block *s, u64 val)
616{
617 s->root = cpu_to_le64(val);
618}
619
620static inline u64 btrfs_super_total_blocks(struct btrfs_super_block *s)
621{
622 return le64_to_cpu(s->total_blocks);
623}
624
625static inline void btrfs_set_super_total_blocks(struct btrfs_super_block *s,
626 u64 val)
627{
628 s->total_blocks = cpu_to_le64(val);
629}
630
631static inline u64 btrfs_super_blocks_used(struct btrfs_super_block *s)
632{
633 return le64_to_cpu(s->blocks_used);
634}
635
636static inline void btrfs_set_super_blocks_used(struct btrfs_super_block *s,
637 u64 val)
638{
639 s->blocks_used = cpu_to_le64(val);
640}
641
Chris Mason123abc82007-03-14 14:14:43 -0400642static inline u32 btrfs_super_blocksize(struct btrfs_super_block *s)
Chris Mason3768f362007-03-13 16:47:54 -0400643{
Chris Mason123abc82007-03-14 14:14:43 -0400644 return le32_to_cpu(s->blocksize);
Chris Mason3768f362007-03-13 16:47:54 -0400645}
646
647static inline void btrfs_set_super_blocksize(struct btrfs_super_block *s,
Chris Mason123abc82007-03-14 14:14:43 -0400648 u32 val)
Chris Mason3768f362007-03-13 16:47:54 -0400649{
Chris Mason123abc82007-03-14 14:14:43 -0400650 s->blocksize = cpu_to_le32(val);
651}
652
653static inline u8 *btrfs_leaf_data(struct btrfs_leaf *l)
654{
655 return (u8 *)l->items;
Chris Mason3768f362007-03-13 16:47:54 -0400656}
Chris Mason4beb1b82007-03-14 10:31:29 -0400657/* helper function to cast into the data area of the leaf. */
658#define btrfs_item_ptr(leaf, slot, type) \
Chris Mason123abc82007-03-14 14:14:43 -0400659 ((type *)(btrfs_leaf_data(leaf) + \
660 btrfs_item_offset((leaf)->items + (slot))))
Chris Mason4beb1b82007-03-14 10:31:29 -0400661
Chris Masone089f052007-03-16 16:20:31 -0400662struct btrfs_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
663 struct btrfs_root *root);
664int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
665 struct btrfs_buffer *buf);
666int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
667 *root, u64 blocknr, u64 num_blocks, int pin);
668int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
669 *root, struct btrfs_key *key, struct btrfs_path *p, int
670 ins_len, int cow);
Chris Mason234b63a2007-03-13 10:46:10 -0400671void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p);
672void btrfs_init_path(struct btrfs_path *p);
Chris Masone089f052007-03-16 16:20:31 -0400673int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
674 struct btrfs_path *path);
675int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
676 *root, struct btrfs_key *key, void *data, u32 data_size);
677int btrfs_insert_empty_item(struct btrfs_trans_handle *trans, struct btrfs_root
678 *root, struct btrfs_path *path, struct btrfs_key
679 *cpu_key, u32 data_size);
Chris Mason234b63a2007-03-13 10:46:10 -0400680int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path);
Chris Mason123abc82007-03-14 14:14:43 -0400681int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf);
Chris Masone089f052007-03-16 16:20:31 -0400682int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
683 *root, struct btrfs_buffer *snap);
684int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
685 btrfs_root *root);
686int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
687 struct btrfs_key *key);
688int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
689 *root, struct btrfs_key *key, struct btrfs_root_item
690 *item);
691int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
692 *root, struct btrfs_key *key, struct btrfs_root_item
693 *item);
694int btrfs_find_last_root(struct btrfs_root *root, u64 objectid, struct
695 btrfs_root_item *item, struct btrfs_key *key);
696int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
697 *root, char *name, int name_len, u64 dir, u64
698 objectid, u8 type);
699int btrfs_lookup_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
700 *root, struct btrfs_path *path, u64 dir, char *name,
701 int name_len, int mod);
Chris Mason1d4f6402007-03-15 15:18:43 -0400702int btrfs_match_dir_item_name(struct btrfs_root *root, struct btrfs_path *path,
703 char *name, int name_len);
Chris Masoneb60cea2007-02-02 09:18:22 -0500704#endif