blob: 7c66b647ea241a9c377dfdb5726eddceda177d6c [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 Masoneb60cea2007-02-02 09:18:22 -05008
Chris Mason3768f362007-03-13 16:47:54 -04009#define BTRFS_ROOT_TREE_OBJECTID 1
10#define BTRFS_EXTENT_TREE_OBJECTID 2
11#define BTRFS_FS_TREE_OBJECTID 3
12
Chris Masonfec577f2007-02-26 10:40:21 -050013/*
14 * the key defines the order in the tree, and so it also defines (optimal)
15 * block layout. objectid corresonds to the inode number. The flags
16 * tells us things about the object, and is a kind of stream selector.
17 * so for a given inode, keys with flags of 1 might refer to the inode
18 * data, flags of 2 may point to file data in the btree and flags == 3
19 * may point to extents.
20 *
21 * offset is the starting byte offset for this key in the stream.
Chris Masone2fa7222007-03-12 16:22:34 -040022 *
23 * btrfs_disk_key is in disk byte order. struct btrfs_key is always
24 * in cpu native order. Otherwise they are identical and their sizes
25 * should be the same (ie both packed)
Chris Masonfec577f2007-02-26 10:40:21 -050026 */
Chris Masone2fa7222007-03-12 16:22:34 -040027struct btrfs_disk_key {
28 __le64 objectid;
Chris Masona1516c82007-03-14 14:26:53 -040029 __le32 flags;
Chris Masona8a2ee02007-03-16 08:46:49 -040030 __le64 offset;
Chris Masone2fa7222007-03-12 16:22:34 -040031} __attribute__ ((__packed__));
32
33struct btrfs_key {
Chris Masoneb60cea2007-02-02 09:18:22 -050034 u64 objectid;
Chris Masona1516c82007-03-14 14:26:53 -040035 u32 flags;
Chris Masona8a2ee02007-03-16 08:46:49 -040036 u64 offset;
Chris Masoneb60cea2007-02-02 09:18:22 -050037} __attribute__ ((__packed__));
38
Chris Masonfec577f2007-02-26 10:40:21 -050039/*
40 * every tree block (leaf or node) starts with this header.
41 */
Chris Masonbb492bb2007-03-12 12:29:44 -040042struct btrfs_header {
Chris Mason3768f362007-03-13 16:47:54 -040043 u8 fsid[16]; /* FS specific uuid */
Chris Masonbb492bb2007-03-12 12:29:44 -040044 __le64 blocknr; /* which block this node is supposed to live in */
45 __le64 parentid; /* objectid of the tree root */
46 __le32 csum;
47 __le32 ham;
48 __le16 nritems;
49 __le16 flags;
Chris Masonfec577f2007-02-26 10:40:21 -050050 /* generation flags to be added */
Chris Masoneb60cea2007-02-02 09:18:22 -050051} __attribute__ ((__packed__));
52
Chris Mason234b63a2007-03-13 10:46:10 -040053#define BTRFS_MAX_LEVEL 8
Chris Mason123abc82007-03-14 14:14:43 -040054#define BTRFS_NODEPTRS_PER_BLOCK(r) (((r)->blocksize - \
55 sizeof(struct btrfs_header)) / \
56 (sizeof(struct btrfs_disk_key) + sizeof(u64)))
57#define __BTRFS_LEAF_DATA_SIZE(bs) ((bs) - sizeof(struct btrfs_header))
58#define BTRFS_LEAF_DATA_SIZE(r) (__BTRFS_LEAF_DATA_SIZE(r->blocksize))
Chris Masoneb60cea2007-02-02 09:18:22 -050059
Chris Mason234b63a2007-03-13 10:46:10 -040060struct btrfs_buffer;
Chris Masonfec577f2007-02-26 10:40:21 -050061/*
Chris Masonfec577f2007-02-26 10:40:21 -050062 * the super block basically lists the main trees of the FS
63 * it currently lacks any block count etc etc
64 */
Chris Mason234b63a2007-03-13 10:46:10 -040065struct btrfs_super_block {
Chris Mason3768f362007-03-13 16:47:54 -040066 u8 fsid[16]; /* FS specific uuid */
67 __le64 blocknr; /* this block number */
68 __le32 csum;
69 __le64 magic;
Chris Mason123abc82007-03-14 14:14:43 -040070 __le32 blocksize;
Chris Mason3768f362007-03-13 16:47:54 -040071 __le64 generation;
72 __le64 root;
73 __le64 total_blocks;
74 __le64 blocks_used;
Chris Masoncfaa7292007-02-21 17:04:57 -050075} __attribute__ ((__packed__));
76
Chris Masonfec577f2007-02-26 10:40:21 -050077/*
Chris Mason62e27492007-03-15 12:56:47 -040078 * A leaf is full of items. offset and size tell us where to find
Chris Masonfec577f2007-02-26 10:40:21 -050079 * the item in the leaf (relative to the start of the data area)
80 */
Chris Mason0783fcf2007-03-12 20:12:07 -040081struct btrfs_item {
Chris Masone2fa7222007-03-12 16:22:34 -040082 struct btrfs_disk_key key;
Chris Mason123abc82007-03-14 14:14:43 -040083 __le32 offset;
Chris Mason0783fcf2007-03-12 20:12:07 -040084 __le16 size;
Chris Masoneb60cea2007-02-02 09:18:22 -050085} __attribute__ ((__packed__));
86
Chris Masonfec577f2007-02-26 10:40:21 -050087/*
88 * leaves have an item area and a data area:
89 * [item0, item1....itemN] [free space] [dataN...data1, data0]
90 *
91 * The data is separate from the items to get the keys closer together
92 * during searches.
93 */
Chris Mason234b63a2007-03-13 10:46:10 -040094struct btrfs_leaf {
Chris Masonbb492bb2007-03-12 12:29:44 -040095 struct btrfs_header header;
Chris Mason123abc82007-03-14 14:14:43 -040096 struct btrfs_item items[];
Chris Masoneb60cea2007-02-02 09:18:22 -050097} __attribute__ ((__packed__));
98
Chris Masonfec577f2007-02-26 10:40:21 -050099/*
100 * all non-leaf blocks are nodes, they hold only keys and pointers to
101 * other blocks
102 */
Chris Mason123abc82007-03-14 14:14:43 -0400103struct btrfs_key_ptr {
104 struct btrfs_disk_key key;
105 __le64 blockptr;
106} __attribute__ ((__packed__));
107
Chris Mason234b63a2007-03-13 10:46:10 -0400108struct btrfs_node {
Chris Masonbb492bb2007-03-12 12:29:44 -0400109 struct btrfs_header header;
Chris Mason123abc82007-03-14 14:14:43 -0400110 struct btrfs_key_ptr ptrs[];
Chris Masoneb60cea2007-02-02 09:18:22 -0500111} __attribute__ ((__packed__));
112
Chris Masonfec577f2007-02-26 10:40:21 -0500113/*
Chris Mason234b63a2007-03-13 10:46:10 -0400114 * btrfs_paths remember the path taken from the root down to the leaf.
115 * level 0 is always the leaf, and nodes[1...BTRFS_MAX_LEVEL] will point
Chris Masonfec577f2007-02-26 10:40:21 -0500116 * to any other levels that are present.
117 *
118 * The slots array records the index of the item or block pointer
119 * used while walking the tree.
120 */
Chris Mason234b63a2007-03-13 10:46:10 -0400121struct btrfs_path {
122 struct btrfs_buffer *nodes[BTRFS_MAX_LEVEL];
123 int slots[BTRFS_MAX_LEVEL];
Chris Masoneb60cea2007-02-02 09:18:22 -0500124};
Chris Mason5de08d72007-02-24 06:24:44 -0500125
Chris Mason62e27492007-03-15 12:56:47 -0400126/*
127 * items in the extent btree are used to record the objectid of the
128 * owner of the block and the number of references
129 */
130struct btrfs_extent_item {
131 __le32 refs;
132 __le64 owner;
133} __attribute__ ((__packed__));
134
Chris Mason1e1d2702007-03-15 19:03:33 -0400135struct btrfs_inode_timespec {
136 __le32 sec;
137 __le32 nsec;
138} __attribute__ ((__packed__));
139
140/*
141 * there is no padding here on purpose. If you want to extent the inode,
142 * make a new item type
143 */
144struct btrfs_inode_item {
145 __le64 generation;
146 __le64 size;
147 __le64 nblocks;
148 __le32 nlink;
149 __le32 uid;
150 __le32 gid;
151 __le32 mode;
152 __le32 rdev;
153 __le16 flags;
154 __le16 compat_flags;
155 struct btrfs_inode_timespec atime;
156 struct btrfs_inode_timespec ctime;
157 struct btrfs_inode_timespec mtime;
158 struct btrfs_inode_timespec otime;
159} __attribute__ ((__packed__));
160
161/* inline data is just a blob of bytes */
162struct btrfs_inline_data_item {
163 u8 data;
164} __attribute__ ((__packed__));
165
Chris Mason62e27492007-03-15 12:56:47 -0400166struct btrfs_dir_item {
167 __le64 objectid;
168 __le16 flags;
Chris Masona8a2ee02007-03-16 08:46:49 -0400169 __le16 name_len;
Chris Mason62e27492007-03-15 12:56:47 -0400170 u8 type;
171} __attribute__ ((__packed__));
172
173struct btrfs_root_item {
174 __le64 blocknr;
175 __le32 flags;
176 __le64 block_limit;
177 __le64 blocks_used;
178 __le32 refs;
179};
180
181/*
182 * in ram representation of the tree. extent_root is used for all allocations
183 * and for the extent tree extent_root root. current_insert is used
184 * only for the extent tree.
185 */
186struct btrfs_root {
187 struct btrfs_buffer *node;
188 struct btrfs_buffer *commit_root;
189 struct btrfs_root *extent_root;
190 struct btrfs_root *tree_root;
191 struct btrfs_key current_insert;
192 struct btrfs_key last_insert;
193 int fp;
194 struct radix_tree_root cache_radix;
195 struct radix_tree_root pinned_radix;
196 struct list_head trans;
197 struct list_head cache;
198 int cache_size;
199 int ref_cows;
200 struct btrfs_root_item root_item;
201 struct btrfs_key root_key;
202 u32 blocksize;
203};
204
Chris Mason62e27492007-03-15 12:56:47 -0400205/* the lower bits in the key flags defines the item type */
206#define BTRFS_KEY_TYPE_MAX 256
207#define BTRFS_KEY_TYPE_MASK (BTRFS_KEY_TYPE_MAX - 1)
Chris Mason1e1d2702007-03-15 19:03:33 -0400208
209/*
210 * inode items have the data typically returned from stat and store other
211 * info about object characteristics. There is one for every file and dir in
212 * the FS
213 */
Chris Mason62e27492007-03-15 12:56:47 -0400214#define BTRFS_INODE_ITEM_KEY 1
Chris Mason1e1d2702007-03-15 19:03:33 -0400215
216/*
217 * dir items are the name -> inode pointers in a directory. There is one
218 * for every name in a directory.
219 */
Chris Mason62e27492007-03-15 12:56:47 -0400220#define BTRFS_DIR_ITEM_KEY 2
Chris Mason1e1d2702007-03-15 19:03:33 -0400221/*
222 * inline data is file data that fits in the btree.
223 */
224#define BTRFS_INLINE_DATA_KEY 3
225/*
226 * extent data is for data that can't fit in the btree. It points to
227 * a (hopefully) huge chunk of disk
228 */
229#define BTRFS_EXTENT_DATA_KEY 4
230/*
231 * root items point to tree roots. There are typically in the root
232 * tree used by the super block to find all the other trees
233 */
234#define BTRFS_ROOT_ITEM_KEY 5
235/*
236 * extent items are in the extent map tree. These record which blocks
237 * are used, and how many references there are to each block
238 */
239#define BTRFS_EXTENT_ITEM_KEY 6
240/*
241 * string items are for debugging. They just store a short string of
242 * data in the FS
243 */
244#define BTRFS_STRING_ITEM_KEY 7
245
246static inline u64 btrfs_inode_generation(struct btrfs_inode_item *i)
247{
248 return le64_to_cpu(i->generation);
249}
250
251static inline void btrfs_set_inode_generation(struct btrfs_inode_item *i,
252 u64 val)
253{
254 i->generation = cpu_to_le64(val);
255}
256
257static inline u64 btrfs_inode_size(struct btrfs_inode_item *i)
258{
259 return le64_to_cpu(i->size);
260}
261
262static inline void btrfs_set_inode_size(struct btrfs_inode_item *i, u64 val)
263{
264 i->size = cpu_to_le64(val);
265}
266
267static inline u64 btrfs_inode_nblocks(struct btrfs_inode_item *i)
268{
269 return le64_to_cpu(i->nblocks);
270}
271
272static inline void btrfs_set_inode_nblocks(struct btrfs_inode_item *i, u64 val)
273{
274 i->nblocks = cpu_to_le64(val);
275}
276
277static inline u32 btrfs_inode_nlink(struct btrfs_inode_item *i)
278{
279 return le32_to_cpu(i->nlink);
280}
281
282static inline void btrfs_set_inode_nlink(struct btrfs_inode_item *i, u32 val)
283{
284 i->nlink = cpu_to_le32(val);
285}
286
287static inline u32 btrfs_inode_uid(struct btrfs_inode_item *i)
288{
289 return le32_to_cpu(i->uid);
290}
291
292static inline void btrfs_set_inode_uid(struct btrfs_inode_item *i, u32 val)
293{
294 i->uid = cpu_to_le32(val);
295}
296
297static inline u32 btrfs_inode_gid(struct btrfs_inode_item *i)
298{
299 return le32_to_cpu(i->gid);
300}
301
302static inline void btrfs_set_inode_gid(struct btrfs_inode_item *i, u32 val)
303{
304 i->gid = cpu_to_le32(val);
305}
306
307static inline u32 btrfs_inode_mode(struct btrfs_inode_item *i)
308{
309 return le32_to_cpu(i->mode);
310}
311
312static inline void btrfs_set_inode_mode(struct btrfs_inode_item *i, u32 val)
313{
314 i->mode = cpu_to_le32(val);
315}
316
317static inline u32 btrfs_inode_rdev(struct btrfs_inode_item *i)
318{
319 return le32_to_cpu(i->rdev);
320}
321
322static inline void btrfs_set_inode_rdev(struct btrfs_inode_item *i, u32 val)
323{
324 i->rdev = cpu_to_le32(val);
325}
326
327static inline u16 btrfs_inode_flags(struct btrfs_inode_item *i)
328{
329 return le16_to_cpu(i->flags);
330}
331
332static inline void btrfs_set_inode_flags(struct btrfs_inode_item *i, u16 val)
333{
334 i->flags = cpu_to_le16(val);
335}
336
337static inline u16 btrfs_inode_compat_flags(struct btrfs_inode_item *i)
338{
339 return le16_to_cpu(i->compat_flags);
340}
341
342static inline void btrfs_set_inode_compat_flags(struct btrfs_inode_item *i,
343 u16 val)
344{
345 i->compat_flags = cpu_to_le16(val);
346}
347
Chris Mason62e27492007-03-15 12:56:47 -0400348
Chris Mason234b63a2007-03-13 10:46:10 -0400349static inline u64 btrfs_extent_owner(struct btrfs_extent_item *ei)
Chris Masoncf27e1e2007-03-13 09:49:06 -0400350{
351 return le64_to_cpu(ei->owner);
352}
353
Chris Mason234b63a2007-03-13 10:46:10 -0400354static inline void btrfs_set_extent_owner(struct btrfs_extent_item *ei, u64 val)
Chris Masoncf27e1e2007-03-13 09:49:06 -0400355{
356 ei->owner = cpu_to_le64(val);
357}
358
Chris Mason234b63a2007-03-13 10:46:10 -0400359static inline u32 btrfs_extent_refs(struct btrfs_extent_item *ei)
Chris Masoncf27e1e2007-03-13 09:49:06 -0400360{
361 return le32_to_cpu(ei->refs);
362}
363
Chris Mason234b63a2007-03-13 10:46:10 -0400364static inline void btrfs_set_extent_refs(struct btrfs_extent_item *ei, u32 val)
Chris Masoncf27e1e2007-03-13 09:49:06 -0400365{
366 ei->refs = cpu_to_le32(val);
367}
368
Chris Mason234b63a2007-03-13 10:46:10 -0400369static inline u64 btrfs_node_blockptr(struct btrfs_node *n, int nr)
Chris Mason1d4f8a02007-03-13 09:28:32 -0400370{
Chris Mason123abc82007-03-14 14:14:43 -0400371 return le64_to_cpu(n->ptrs[nr].blockptr);
Chris Mason1d4f8a02007-03-13 09:28:32 -0400372}
373
Chris Mason234b63a2007-03-13 10:46:10 -0400374static inline void btrfs_set_node_blockptr(struct btrfs_node *n, int nr,
375 u64 val)
Chris Mason1d4f8a02007-03-13 09:28:32 -0400376{
Chris Mason123abc82007-03-14 14:14:43 -0400377 n->ptrs[nr].blockptr = cpu_to_le64(val);
Chris Mason1d4f8a02007-03-13 09:28:32 -0400378}
379
Chris Mason123abc82007-03-14 14:14:43 -0400380static inline u32 btrfs_item_offset(struct btrfs_item *item)
Chris Mason0783fcf2007-03-12 20:12:07 -0400381{
Chris Mason123abc82007-03-14 14:14:43 -0400382 return le32_to_cpu(item->offset);
Chris Mason0783fcf2007-03-12 20:12:07 -0400383}
384
Chris Mason123abc82007-03-14 14:14:43 -0400385static inline void btrfs_set_item_offset(struct btrfs_item *item, u32 val)
Chris Mason0783fcf2007-03-12 20:12:07 -0400386{
Chris Mason123abc82007-03-14 14:14:43 -0400387 item->offset = cpu_to_le32(val);
Chris Mason0783fcf2007-03-12 20:12:07 -0400388}
389
Chris Mason123abc82007-03-14 14:14:43 -0400390static inline u32 btrfs_item_end(struct btrfs_item *item)
Chris Mason0783fcf2007-03-12 20:12:07 -0400391{
Chris Mason123abc82007-03-14 14:14:43 -0400392 return le32_to_cpu(item->offset) + le16_to_cpu(item->size);
Chris Mason0783fcf2007-03-12 20:12:07 -0400393}
394
395static inline u16 btrfs_item_size(struct btrfs_item *item)
396{
397 return le16_to_cpu(item->size);
398}
399
400static inline void btrfs_set_item_size(struct btrfs_item *item, u16 val)
401{
402 item->size = cpu_to_le16(val);
403}
404
Chris Mason1d4f6402007-03-15 15:18:43 -0400405static inline u64 btrfs_dir_objectid(struct btrfs_dir_item *d)
406{
407 return le64_to_cpu(d->objectid);
408}
409
410static inline void btrfs_set_dir_objectid(struct btrfs_dir_item *d, u64 val)
411{
412 d->objectid = cpu_to_le64(val);
413}
414
415static inline u16 btrfs_dir_flags(struct btrfs_dir_item *d)
416{
417 return le16_to_cpu(d->flags);
418}
419
420static inline void btrfs_set_dir_flags(struct btrfs_dir_item *d, u16 val)
421{
422 d->flags = cpu_to_le16(val);
423}
424
425static inline u8 btrfs_dir_type(struct btrfs_dir_item *d)
426{
427 return d->type;
428}
429
430static inline void btrfs_set_dir_type(struct btrfs_dir_item *d, u8 val)
431{
432 d->type = val;
433}
434
Chris Masona8a2ee02007-03-16 08:46:49 -0400435static inline u16 btrfs_dir_name_len(struct btrfs_dir_item *d)
Chris Mason1d4f6402007-03-15 15:18:43 -0400436{
Chris Masona8a2ee02007-03-16 08:46:49 -0400437 return le16_to_cpu(d->name_len);
438}
439
440static inline void btrfs_set_dir_name_len(struct btrfs_dir_item *d, u16 val)
441{
442 d->name_len = cpu_to_le16(val);
Chris Mason1d4f6402007-03-15 15:18:43 -0400443}
444
Chris Masone2fa7222007-03-12 16:22:34 -0400445static inline void btrfs_disk_key_to_cpu(struct btrfs_key *cpu,
446 struct btrfs_disk_key *disk)
447{
448 cpu->offset = le64_to_cpu(disk->offset);
449 cpu->flags = le32_to_cpu(disk->flags);
450 cpu->objectid = le64_to_cpu(disk->objectid);
451}
452
453static inline void btrfs_cpu_key_to_disk(struct btrfs_disk_key *disk,
454 struct btrfs_key *cpu)
455{
456 disk->offset = cpu_to_le64(cpu->offset);
457 disk->flags = cpu_to_le32(cpu->flags);
458 disk->objectid = cpu_to_le64(cpu->objectid);
459}
460
Chris Mason62e27492007-03-15 12:56:47 -0400461static inline u64 btrfs_disk_key_objectid(struct btrfs_disk_key *disk)
Chris Masone2fa7222007-03-12 16:22:34 -0400462{
463 return le64_to_cpu(disk->objectid);
464}
465
Chris Mason62e27492007-03-15 12:56:47 -0400466static inline void btrfs_set_disk_key_objectid(struct btrfs_disk_key *disk,
467 u64 val)
Chris Masone2fa7222007-03-12 16:22:34 -0400468{
469 disk->objectid = cpu_to_le64(val);
470}
471
Chris Mason62e27492007-03-15 12:56:47 -0400472static inline u64 btrfs_disk_key_offset(struct btrfs_disk_key *disk)
Chris Masone2fa7222007-03-12 16:22:34 -0400473{
474 return le64_to_cpu(disk->offset);
475}
476
Chris Mason62e27492007-03-15 12:56:47 -0400477static inline void btrfs_set_disk_key_offset(struct btrfs_disk_key *disk,
478 u64 val)
Chris Masone2fa7222007-03-12 16:22:34 -0400479{
480 disk->offset = cpu_to_le64(val);
481}
482
Chris Mason62e27492007-03-15 12:56:47 -0400483static inline u32 btrfs_disk_key_flags(struct btrfs_disk_key *disk)
Chris Masone2fa7222007-03-12 16:22:34 -0400484{
485 return le32_to_cpu(disk->flags);
486}
487
Chris Mason62e27492007-03-15 12:56:47 -0400488static inline void btrfs_set_disk_key_flags(struct btrfs_disk_key *disk,
489 u32 val)
Chris Masone2fa7222007-03-12 16:22:34 -0400490{
491 disk->flags = cpu_to_le32(val);
492}
493
Chris Mason62e27492007-03-15 12:56:47 -0400494static inline u32 btrfs_key_type(struct btrfs_key *key)
495{
496 return key->flags & BTRFS_KEY_TYPE_MASK;
497}
498
499static inline u32 btrfs_disk_key_type(struct btrfs_disk_key *key)
500{
501 return le32_to_cpu(key->flags) & BTRFS_KEY_TYPE_MASK;
502}
503
504static inline void btrfs_set_key_type(struct btrfs_key *key, u32 type)
505{
506 BUG_ON(type >= BTRFS_KEY_TYPE_MAX);
507 key->flags = (key->flags & ~((u64)BTRFS_KEY_TYPE_MASK)) | type;
508}
509
510static inline void btrfs_set_disk_key_type(struct btrfs_disk_key *key, u32 type)
511{
512 u32 flags = btrfs_disk_key_flags(key);
513 BUG_ON(type >= BTRFS_KEY_TYPE_MAX);
514 flags = (flags & ~((u64)BTRFS_KEY_TYPE_MASK)) | type;
515 btrfs_set_disk_key_flags(key, flags);
516}
517
Chris Masonbb492bb2007-03-12 12:29:44 -0400518static inline u64 btrfs_header_blocknr(struct btrfs_header *h)
Chris Mason7518a232007-03-12 12:01:18 -0400519{
Chris Masonbb492bb2007-03-12 12:29:44 -0400520 return le64_to_cpu(h->blocknr);
Chris Mason7518a232007-03-12 12:01:18 -0400521}
522
Chris Masonbb492bb2007-03-12 12:29:44 -0400523static inline void btrfs_set_header_blocknr(struct btrfs_header *h, u64 blocknr)
Chris Mason7518a232007-03-12 12:01:18 -0400524{
Chris Masonbb492bb2007-03-12 12:29:44 -0400525 h->blocknr = cpu_to_le64(blocknr);
Chris Mason7518a232007-03-12 12:01:18 -0400526}
527
Chris Masonbb492bb2007-03-12 12:29:44 -0400528static inline u64 btrfs_header_parentid(struct btrfs_header *h)
Chris Mason7518a232007-03-12 12:01:18 -0400529{
Chris Masonbb492bb2007-03-12 12:29:44 -0400530 return le64_to_cpu(h->parentid);
Chris Mason7518a232007-03-12 12:01:18 -0400531}
532
Chris Masonbb492bb2007-03-12 12:29:44 -0400533static inline void btrfs_set_header_parentid(struct btrfs_header *h,
534 u64 parentid)
Chris Mason7518a232007-03-12 12:01:18 -0400535{
Chris Masonbb492bb2007-03-12 12:29:44 -0400536 h->parentid = cpu_to_le64(parentid);
Chris Mason7518a232007-03-12 12:01:18 -0400537}
538
Chris Masonbb492bb2007-03-12 12:29:44 -0400539static inline u16 btrfs_header_nritems(struct btrfs_header *h)
Chris Mason7518a232007-03-12 12:01:18 -0400540{
Chris Masonbb492bb2007-03-12 12:29:44 -0400541 return le16_to_cpu(h->nritems);
Chris Mason7518a232007-03-12 12:01:18 -0400542}
543
Chris Masonbb492bb2007-03-12 12:29:44 -0400544static inline void btrfs_set_header_nritems(struct btrfs_header *h, u16 val)
Chris Mason7518a232007-03-12 12:01:18 -0400545{
Chris Masonbb492bb2007-03-12 12:29:44 -0400546 h->nritems = cpu_to_le16(val);
Chris Mason7518a232007-03-12 12:01:18 -0400547}
548
Chris Masonbb492bb2007-03-12 12:29:44 -0400549static inline u16 btrfs_header_flags(struct btrfs_header *h)
Chris Mason7518a232007-03-12 12:01:18 -0400550{
Chris Masonbb492bb2007-03-12 12:29:44 -0400551 return le16_to_cpu(h->flags);
Chris Mason7518a232007-03-12 12:01:18 -0400552}
553
Chris Masonbb492bb2007-03-12 12:29:44 -0400554static inline void btrfs_set_header_flags(struct btrfs_header *h, u16 val)
Chris Mason7518a232007-03-12 12:01:18 -0400555{
Chris Masonbb492bb2007-03-12 12:29:44 -0400556 h->flags = cpu_to_le16(val);
Chris Mason7518a232007-03-12 12:01:18 -0400557}
558
Chris Masonbb492bb2007-03-12 12:29:44 -0400559static inline int btrfs_header_level(struct btrfs_header *h)
Chris Mason7518a232007-03-12 12:01:18 -0400560{
Chris Mason234b63a2007-03-13 10:46:10 -0400561 return btrfs_header_flags(h) & (BTRFS_MAX_LEVEL - 1);
Chris Mason7518a232007-03-12 12:01:18 -0400562}
563
Chris Masonbb492bb2007-03-12 12:29:44 -0400564static inline void btrfs_set_header_level(struct btrfs_header *h, int level)
Chris Mason7518a232007-03-12 12:01:18 -0400565{
Chris Masonbb492bb2007-03-12 12:29:44 -0400566 u16 flags;
Chris Mason234b63a2007-03-13 10:46:10 -0400567 BUG_ON(level > BTRFS_MAX_LEVEL);
568 flags = btrfs_header_flags(h) & ~(BTRFS_MAX_LEVEL - 1);
Chris Mason7518a232007-03-12 12:01:18 -0400569 btrfs_set_header_flags(h, flags | level);
570}
571
Chris Mason234b63a2007-03-13 10:46:10 -0400572static inline int btrfs_is_leaf(struct btrfs_node *n)
Chris Mason7518a232007-03-12 12:01:18 -0400573{
574 return (btrfs_header_level(&n->header) == 0);
575}
576
Chris Mason3768f362007-03-13 16:47:54 -0400577static inline u64 btrfs_root_blocknr(struct btrfs_root_item *item)
578{
579 return le64_to_cpu(item->blocknr);
580}
581
582static inline void btrfs_set_root_blocknr(struct btrfs_root_item *item, u64 val)
583{
584 item->blocknr = cpu_to_le64(val);
585}
586
587static inline u32 btrfs_root_refs(struct btrfs_root_item *item)
588{
589 return le32_to_cpu(item->refs);
590}
591
592static inline void btrfs_set_root_refs(struct btrfs_root_item *item, u32 val)
593{
594 item->refs = cpu_to_le32(val);
595}
596
597static inline u64 btrfs_super_blocknr(struct btrfs_super_block *s)
598{
599 return le64_to_cpu(s->blocknr);
600}
601
602static inline void btrfs_set_super_blocknr(struct btrfs_super_block *s, u64 val)
603{
604 s->blocknr = cpu_to_le64(val);
605}
606
607static inline u64 btrfs_super_root(struct btrfs_super_block *s)
608{
609 return le64_to_cpu(s->root);
610}
611
612static inline void btrfs_set_super_root(struct btrfs_super_block *s, u64 val)
613{
614 s->root = cpu_to_le64(val);
615}
616
617static inline u64 btrfs_super_total_blocks(struct btrfs_super_block *s)
618{
619 return le64_to_cpu(s->total_blocks);
620}
621
622static inline void btrfs_set_super_total_blocks(struct btrfs_super_block *s,
623 u64 val)
624{
625 s->total_blocks = cpu_to_le64(val);
626}
627
628static inline u64 btrfs_super_blocks_used(struct btrfs_super_block *s)
629{
630 return le64_to_cpu(s->blocks_used);
631}
632
633static inline void btrfs_set_super_blocks_used(struct btrfs_super_block *s,
634 u64 val)
635{
636 s->blocks_used = cpu_to_le64(val);
637}
638
Chris Mason123abc82007-03-14 14:14:43 -0400639static inline u32 btrfs_super_blocksize(struct btrfs_super_block *s)
Chris Mason3768f362007-03-13 16:47:54 -0400640{
Chris Mason123abc82007-03-14 14:14:43 -0400641 return le32_to_cpu(s->blocksize);
Chris Mason3768f362007-03-13 16:47:54 -0400642}
643
644static inline void btrfs_set_super_blocksize(struct btrfs_super_block *s,
Chris Mason123abc82007-03-14 14:14:43 -0400645 u32 val)
Chris Mason3768f362007-03-13 16:47:54 -0400646{
Chris Mason123abc82007-03-14 14:14:43 -0400647 s->blocksize = cpu_to_le32(val);
648}
649
650static inline u8 *btrfs_leaf_data(struct btrfs_leaf *l)
651{
652 return (u8 *)l->items;
Chris Mason3768f362007-03-13 16:47:54 -0400653}
Chris Mason4beb1b82007-03-14 10:31:29 -0400654/* helper function to cast into the data area of the leaf. */
655#define btrfs_item_ptr(leaf, slot, type) \
Chris Mason123abc82007-03-14 14:14:43 -0400656 ((type *)(btrfs_leaf_data(leaf) + \
657 btrfs_item_offset((leaf)->items + (slot))))
Chris Mason4beb1b82007-03-14 10:31:29 -0400658
Chris Mason234b63a2007-03-13 10:46:10 -0400659struct btrfs_buffer *btrfs_alloc_free_block(struct btrfs_root *root);
660int btrfs_inc_ref(struct btrfs_root *root, struct btrfs_buffer *buf);
661int btrfs_free_extent(struct btrfs_root *root, u64 blocknr, u64 num_blocks);
662int btrfs_search_slot(struct btrfs_root *root, struct btrfs_key *key,
663 struct btrfs_path *p, int ins_len, int cow);
664void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p);
665void btrfs_init_path(struct btrfs_path *p);
666int btrfs_del_item(struct btrfs_root *root, struct btrfs_path *path);
667int btrfs_insert_item(struct btrfs_root *root, struct btrfs_key *key,
Chris Mason62e27492007-03-15 12:56:47 -0400668 void *data, u32 data_size);
669int btrfs_insert_empty_item(struct btrfs_root *root, struct btrfs_path *path,
670 struct btrfs_key *cpu_key, u32 data_size);
Chris Mason234b63a2007-03-13 10:46:10 -0400671int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path);
Chris Mason123abc82007-03-14 14:14:43 -0400672int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf);
Chris Mason234b63a2007-03-13 10:46:10 -0400673int btrfs_drop_snapshot(struct btrfs_root *root, struct btrfs_buffer *snap);
674int btrfs_finish_extent_commit(struct btrfs_root *root);
Chris Mason3768f362007-03-13 16:47:54 -0400675int btrfs_del_root(struct btrfs_root *root, struct btrfs_key *key);
676int btrfs_insert_root(struct btrfs_root *root, struct btrfs_key *key,
677 struct btrfs_root_item *item);
678int btrfs_update_root(struct btrfs_root *root, struct btrfs_key *key,
679 struct btrfs_root_item *item);
680int btrfs_find_last_root(struct btrfs_root *root, u64 objectid,
681 struct btrfs_root_item *item, struct btrfs_key *key);
Chris Mason1d4f6402007-03-15 15:18:43 -0400682int btrfs_insert_dir_item(struct btrfs_root *root, char *name, int name_len,
683 u64 dir, u64 objectid, u8 type);
684int btrfs_lookup_dir_item(struct btrfs_root *root, struct btrfs_path *path,
685 u64 dir, char *name, int name_len, int mod);
686int btrfs_match_dir_item_name(struct btrfs_root *root, struct btrfs_path *path,
687 char *name, int name_len);
Chris Masoneb60cea2007-02-02 09:18:22 -0500688#endif