]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/btrfs/ctree.h
btrfs_file_write -- first pass
[net-next-2.6.git] / fs / btrfs / ctree.h
CommitLineData
234b63a0
CM
1#ifndef __BTRFS__
2#define __BTRFS__
eb60ceac 3
e20d96d6 4#include <linux/fs.h>
8ef97622 5#include "bit-radix.h"
e20d96d6 6
e089f05c 7struct btrfs_trans_handle;
79154b1b 8struct btrfs_transaction;
e089f05c 9
3768f368 10#define BTRFS_MAGIC "_BtRfS_M"
eb60ceac 11
6407bf6d
CM
12#define BTRFS_ROOT_TREE_OBJECTID 1ULL
13#define BTRFS_EXTENT_TREE_OBJECTID 2ULL
14#define BTRFS_INODE_MAP_OBJECTID 3ULL
15#define BTRFS_FS_TREE_OBJECTID 4ULL
16#define BTRFS_FIRST_FREE_OBJECTID 5ULL
3768f368 17
e20d96d6
CM
18/*
19 * we can actually store much bigger names, but lets not confuse the rest
20 * of linux
21 */
22#define BTRFS_NAME_LEN 255
23
fec577fb
CM
24/*
25 * the key defines the order in the tree, and so it also defines (optimal)
26 * block layout. objectid corresonds to the inode number. The flags
27 * tells us things about the object, and is a kind of stream selector.
28 * so for a given inode, keys with flags of 1 might refer to the inode
29 * data, flags of 2 may point to file data in the btree and flags == 3
30 * may point to extents.
31 *
32 * offset is the starting byte offset for this key in the stream.
e2fa7227
CM
33 *
34 * btrfs_disk_key is in disk byte order. struct btrfs_key is always
35 * in cpu native order. Otherwise they are identical and their sizes
36 * should be the same (ie both packed)
fec577fb 37 */
e2fa7227
CM
38struct btrfs_disk_key {
39 __le64 objectid;
a1516c89 40 __le32 flags;
a8a2ee0c 41 __le64 offset;
e2fa7227
CM
42} __attribute__ ((__packed__));
43
44struct btrfs_key {
eb60ceac 45 u64 objectid;
a1516c89 46 u32 flags;
a8a2ee0c 47 u64 offset;
eb60ceac
CM
48} __attribute__ ((__packed__));
49
fec577fb
CM
50/*
51 * every tree block (leaf or node) starts with this header.
52 */
bb492bb0 53struct btrfs_header {
87cbda5c 54 __le32 csum[8];
3768f368 55 u8 fsid[16]; /* FS specific uuid */
bb492bb0 56 __le64 blocknr; /* which block this node is supposed to live in */
7f5c1516 57 __le64 generation;
bb492bb0 58 __le64 parentid; /* objectid of the tree root */
bb492bb0
CM
59 __le32 ham;
60 __le16 nritems;
61 __le16 flags;
9a6f11ed 62 u8 level;
eb60ceac
CM
63} __attribute__ ((__packed__));
64
234b63a0 65#define BTRFS_MAX_LEVEL 8
123abc88
CM
66#define BTRFS_NODEPTRS_PER_BLOCK(r) (((r)->blocksize - \
67 sizeof(struct btrfs_header)) / \
68 (sizeof(struct btrfs_disk_key) + sizeof(u64)))
69#define __BTRFS_LEAF_DATA_SIZE(bs) ((bs) - sizeof(struct btrfs_header))
70#define BTRFS_LEAF_DATA_SIZE(r) (__BTRFS_LEAF_DATA_SIZE(r->blocksize))
eb60ceac 71
e20d96d6 72struct buffer_head;
fec577fb
CM
73/*
74 * the super block basically lists the main trees of the FS
75 * it currently lacks any block count etc etc
76 */
234b63a0 77struct btrfs_super_block {
87cbda5c
CM
78 __le32 csum[8];
79 /* the first 3 fields must match struct btrfs_header */
3768f368
CM
80 u8 fsid[16]; /* FS specific uuid */
81 __le64 blocknr; /* this block number */
3768f368 82 __le64 magic;
123abc88 83 __le32 blocksize;
3768f368
CM
84 __le64 generation;
85 __le64 root;
86 __le64 total_blocks;
87 __le64 blocks_used;
2e635a27 88 __le64 root_dir_objectid;
cfaa7295
CM
89} __attribute__ ((__packed__));
90
fec577fb 91/*
62e2749e 92 * A leaf is full of items. offset and size tell us where to find
fec577fb
CM
93 * the item in the leaf (relative to the start of the data area)
94 */
0783fcfc 95struct btrfs_item {
e2fa7227 96 struct btrfs_disk_key key;
123abc88 97 __le32 offset;
0783fcfc 98 __le16 size;
eb60ceac
CM
99} __attribute__ ((__packed__));
100
fec577fb
CM
101/*
102 * leaves have an item area and a data area:
103 * [item0, item1....itemN] [free space] [dataN...data1, data0]
104 *
105 * The data is separate from the items to get the keys closer together
106 * during searches.
107 */
234b63a0 108struct btrfs_leaf {
bb492bb0 109 struct btrfs_header header;
123abc88 110 struct btrfs_item items[];
eb60ceac
CM
111} __attribute__ ((__packed__));
112
fec577fb
CM
113/*
114 * all non-leaf blocks are nodes, they hold only keys and pointers to
115 * other blocks
116 */
123abc88
CM
117struct btrfs_key_ptr {
118 struct btrfs_disk_key key;
119 __le64 blockptr;
120} __attribute__ ((__packed__));
121
234b63a0 122struct btrfs_node {
bb492bb0 123 struct btrfs_header header;
123abc88 124 struct btrfs_key_ptr ptrs[];
eb60ceac
CM
125} __attribute__ ((__packed__));
126
fec577fb 127/*
234b63a0
CM
128 * btrfs_paths remember the path taken from the root down to the leaf.
129 * level 0 is always the leaf, and nodes[1...BTRFS_MAX_LEVEL] will point
fec577fb
CM
130 * to any other levels that are present.
131 *
132 * The slots array records the index of the item or block pointer
133 * used while walking the tree.
134 */
234b63a0 135struct btrfs_path {
e20d96d6 136 struct buffer_head *nodes[BTRFS_MAX_LEVEL];
234b63a0 137 int slots[BTRFS_MAX_LEVEL];
eb60ceac 138};
5de08d7d 139
62e2749e
CM
140/*
141 * items in the extent btree are used to record the objectid of the
142 * owner of the block and the number of references
143 */
144struct btrfs_extent_item {
145 __le32 refs;
146 __le64 owner;
147} __attribute__ ((__packed__));
148
1e1d2701
CM
149struct btrfs_inode_timespec {
150 __le32 sec;
151 __le32 nsec;
152} __attribute__ ((__packed__));
153
154/*
155 * there is no padding here on purpose. If you want to extent the inode,
156 * make a new item type
157 */
158struct btrfs_inode_item {
159 __le64 generation;
160 __le64 size;
161 __le64 nblocks;
162 __le32 nlink;
163 __le32 uid;
164 __le32 gid;
165 __le32 mode;
166 __le32 rdev;
167 __le16 flags;
168 __le16 compat_flags;
169 struct btrfs_inode_timespec atime;
170 struct btrfs_inode_timespec ctime;
171 struct btrfs_inode_timespec mtime;
172 struct btrfs_inode_timespec otime;
173} __attribute__ ((__packed__));
174
175/* inline data is just a blob of bytes */
176struct btrfs_inline_data_item {
177 u8 data;
178} __attribute__ ((__packed__));
179
62e2749e
CM
180struct btrfs_dir_item {
181 __le64 objectid;
182 __le16 flags;
a8a2ee0c 183 __le16 name_len;
62e2749e
CM
184 u8 type;
185} __attribute__ ((__packed__));
186
187struct btrfs_root_item {
188 __le64 blocknr;
189 __le32 flags;
190 __le64 block_limit;
191 __le64 blocks_used;
192 __le32 refs;
9f5fae2f 193} __attribute__ ((__packed__));
62e2749e 194
9f5fae2f 195struct btrfs_file_extent_item {
71951f35 196 __le64 generation;
9f5fae2f
CM
197 /*
198 * disk space consumed by the extent, checksum blocks are included
199 * in these numbers
200 */
201 __le64 disk_blocknr;
202 __le64 disk_num_blocks;
203 /*
dee26a9f 204 * the logical offset in file blocks (no csums)
9f5fae2f
CM
205 * this extent record is for. This allows a file extent to point
206 * into the middle of an existing extent on disk, sharing it
207 * between two snapshots (useful if some bytes in the middle of the
208 * extent have changed
209 */
210 __le64 offset;
211 /*
212 * the logical number of file blocks (no csums included)
213 */
214 __le64 num_blocks;
215} __attribute__ ((__packed__));
216
217struct btrfs_inode_map_item {
218 struct btrfs_disk_key key;
219} __attribute__ ((__packed__));
220
87cbda5c 221struct crypto_hash;
9f5fae2f
CM
222struct btrfs_fs_info {
223 struct btrfs_root *fs_root;
62e2749e
CM
224 struct btrfs_root *extent_root;
225 struct btrfs_root *tree_root;
9f5fae2f 226 struct btrfs_root *inode_root;
62e2749e
CM
227 struct btrfs_key current_insert;
228 struct btrfs_key last_insert;
8ef97622 229 struct radix_tree_root pending_del_radix;
62e2749e 230 struct radix_tree_root pinned_radix;
9f5fae2f
CM
231 u64 last_inode_alloc;
232 u64 last_inode_alloc_dirid;
293ffd5f 233 u64 generation;
79154b1b 234 struct btrfs_transaction *running_transaction;
1261ec42 235 struct btrfs_super_block *disk_super;
e20d96d6
CM
236 struct buffer_head *sb_buffer;
237 struct super_block *sb;
d98237b3 238 struct inode *btree_inode;
79154b1b 239 struct mutex trans_mutex;
d561c025 240 struct mutex fs_mutex;
87cbda5c
CM
241 struct crypto_hash *hash_tfm;
242 spinlock_t hash_lock;
9f5fae2f
CM
243};
244
245/*
246 * in ram representation of the tree. extent_root is used for all allocations
247 * and for the extent tree extent_root root. current_insert is used
248 * only for the extent tree.
249 */
250struct btrfs_root {
e20d96d6
CM
251 struct buffer_head *node;
252 struct buffer_head *commit_root;
62e2749e
CM
253 struct btrfs_root_item root_item;
254 struct btrfs_key root_key;
9f5fae2f 255 struct btrfs_fs_info *fs_info;
62e2749e 256 u32 blocksize;
9f5fae2f
CM
257 int ref_cows;
258 u32 type;
62e2749e
CM
259};
260
62e2749e
CM
261/* the lower bits in the key flags defines the item type */
262#define BTRFS_KEY_TYPE_MAX 256
263#define BTRFS_KEY_TYPE_MASK (BTRFS_KEY_TYPE_MAX - 1)
1e1d2701
CM
264
265/*
266 * inode items have the data typically returned from stat and store other
267 * info about object characteristics. There is one for every file and dir in
268 * the FS
269 */
62e2749e 270#define BTRFS_INODE_ITEM_KEY 1
1e1d2701
CM
271
272/*
273 * dir items are the name -> inode pointers in a directory. There is one
274 * for every name in a directory.
275 */
62e2749e 276#define BTRFS_DIR_ITEM_KEY 2
1e1d2701
CM
277/*
278 * inline data is file data that fits in the btree.
279 */
280#define BTRFS_INLINE_DATA_KEY 3
281/*
282 * extent data is for data that can't fit in the btree. It points to
283 * a (hopefully) huge chunk of disk
284 */
285#define BTRFS_EXTENT_DATA_KEY 4
286/*
287 * root items point to tree roots. There are typically in the root
288 * tree used by the super block to find all the other trees
289 */
290#define BTRFS_ROOT_ITEM_KEY 5
291/*
292 * extent items are in the extent map tree. These record which blocks
293 * are used, and how many references there are to each block
294 */
295#define BTRFS_EXTENT_ITEM_KEY 6
9f5fae2f
CM
296
297/*
298 * the inode map records which inode numbers are in use and where
299 * they actually live on disk
300 */
301#define BTRFS_INODE_MAP_ITEM_KEY 7
1e1d2701
CM
302/*
303 * string items are for debugging. They just store a short string of
304 * data in the FS
305 */
9f5fae2f 306#define BTRFS_STRING_ITEM_KEY 8
1e1d2701
CM
307
308static inline u64 btrfs_inode_generation(struct btrfs_inode_item *i)
309{
310 return le64_to_cpu(i->generation);
311}
312
313static inline void btrfs_set_inode_generation(struct btrfs_inode_item *i,
314 u64 val)
315{
316 i->generation = cpu_to_le64(val);
317}
318
319static inline u64 btrfs_inode_size(struct btrfs_inode_item *i)
320{
321 return le64_to_cpu(i->size);
322}
323
324static inline void btrfs_set_inode_size(struct btrfs_inode_item *i, u64 val)
325{
326 i->size = cpu_to_le64(val);
327}
328
329static inline u64 btrfs_inode_nblocks(struct btrfs_inode_item *i)
330{
331 return le64_to_cpu(i->nblocks);
332}
333
334static inline void btrfs_set_inode_nblocks(struct btrfs_inode_item *i, u64 val)
335{
336 i->nblocks = cpu_to_le64(val);
337}
338
339static inline u32 btrfs_inode_nlink(struct btrfs_inode_item *i)
340{
341 return le32_to_cpu(i->nlink);
342}
343
344static inline void btrfs_set_inode_nlink(struct btrfs_inode_item *i, u32 val)
345{
346 i->nlink = cpu_to_le32(val);
347}
348
349static inline u32 btrfs_inode_uid(struct btrfs_inode_item *i)
350{
351 return le32_to_cpu(i->uid);
352}
353
354static inline void btrfs_set_inode_uid(struct btrfs_inode_item *i, u32 val)
355{
356 i->uid = cpu_to_le32(val);
357}
358
359static inline u32 btrfs_inode_gid(struct btrfs_inode_item *i)
360{
361 return le32_to_cpu(i->gid);
362}
363
364static inline void btrfs_set_inode_gid(struct btrfs_inode_item *i, u32 val)
365{
366 i->gid = cpu_to_le32(val);
367}
368
369static inline u32 btrfs_inode_mode(struct btrfs_inode_item *i)
370{
371 return le32_to_cpu(i->mode);
372}
373
374static inline void btrfs_set_inode_mode(struct btrfs_inode_item *i, u32 val)
375{
376 i->mode = cpu_to_le32(val);
377}
378
379static inline u32 btrfs_inode_rdev(struct btrfs_inode_item *i)
380{
381 return le32_to_cpu(i->rdev);
382}
383
384static inline void btrfs_set_inode_rdev(struct btrfs_inode_item *i, u32 val)
385{
386 i->rdev = cpu_to_le32(val);
387}
388
389static inline u16 btrfs_inode_flags(struct btrfs_inode_item *i)
390{
391 return le16_to_cpu(i->flags);
392}
393
394static inline void btrfs_set_inode_flags(struct btrfs_inode_item *i, u16 val)
395{
396 i->flags = cpu_to_le16(val);
397}
398
399static inline u16 btrfs_inode_compat_flags(struct btrfs_inode_item *i)
400{
401 return le16_to_cpu(i->compat_flags);
402}
403
404static inline void btrfs_set_inode_compat_flags(struct btrfs_inode_item *i,
405 u16 val)
406{
407 i->compat_flags = cpu_to_le16(val);
408}
409
e20d96d6
CM
410static inline u32 btrfs_timespec_sec(struct btrfs_inode_timespec *ts)
411{
412 return le32_to_cpu(ts->sec);
413}
414
415static inline void btrfs_set_timespec_sec(struct btrfs_inode_timespec *ts,
416 u32 val)
417{
418 ts->sec = cpu_to_le32(val);
419}
420
421static inline u32 btrfs_timespec_nsec(struct btrfs_inode_timespec *ts)
422{
423 return le32_to_cpu(ts->nsec);
424}
425
426static inline void btrfs_set_timespec_nsec(struct btrfs_inode_timespec *ts,
427 u32 val)
428{
429 ts->nsec = cpu_to_le32(val);
430}
431
432
62e2749e 433
234b63a0 434static inline u64 btrfs_extent_owner(struct btrfs_extent_item *ei)
cf27e1ee
CM
435{
436 return le64_to_cpu(ei->owner);
437}
438
234b63a0 439static inline void btrfs_set_extent_owner(struct btrfs_extent_item *ei, u64 val)
cf27e1ee
CM
440{
441 ei->owner = cpu_to_le64(val);
442}
443
234b63a0 444static inline u32 btrfs_extent_refs(struct btrfs_extent_item *ei)
cf27e1ee
CM
445{
446 return le32_to_cpu(ei->refs);
447}
448
234b63a0 449static inline void btrfs_set_extent_refs(struct btrfs_extent_item *ei, u32 val)
cf27e1ee
CM
450{
451 ei->refs = cpu_to_le32(val);
452}
453
234b63a0 454static inline u64 btrfs_node_blockptr(struct btrfs_node *n, int nr)
1d4f8a0c 455{
123abc88 456 return le64_to_cpu(n->ptrs[nr].blockptr);
1d4f8a0c
CM
457}
458
234b63a0
CM
459static inline void btrfs_set_node_blockptr(struct btrfs_node *n, int nr,
460 u64 val)
1d4f8a0c 461{
123abc88 462 n->ptrs[nr].blockptr = cpu_to_le64(val);
1d4f8a0c
CM
463}
464
123abc88 465static inline u32 btrfs_item_offset(struct btrfs_item *item)
0783fcfc 466{
123abc88 467 return le32_to_cpu(item->offset);
0783fcfc
CM
468}
469
123abc88 470static inline void btrfs_set_item_offset(struct btrfs_item *item, u32 val)
0783fcfc 471{
123abc88 472 item->offset = cpu_to_le32(val);
0783fcfc
CM
473}
474
123abc88 475static inline u32 btrfs_item_end(struct btrfs_item *item)
0783fcfc 476{
123abc88 477 return le32_to_cpu(item->offset) + le16_to_cpu(item->size);
0783fcfc
CM
478}
479
480static inline u16 btrfs_item_size(struct btrfs_item *item)
481{
482 return le16_to_cpu(item->size);
483}
484
485static inline void btrfs_set_item_size(struct btrfs_item *item, u16 val)
486{
487 item->size = cpu_to_le16(val);
488}
489
1d4f6404
CM
490static inline u64 btrfs_dir_objectid(struct btrfs_dir_item *d)
491{
492 return le64_to_cpu(d->objectid);
493}
494
495static inline void btrfs_set_dir_objectid(struct btrfs_dir_item *d, u64 val)
496{
497 d->objectid = cpu_to_le64(val);
498}
499
500static inline u16 btrfs_dir_flags(struct btrfs_dir_item *d)
501{
502 return le16_to_cpu(d->flags);
503}
504
505static inline void btrfs_set_dir_flags(struct btrfs_dir_item *d, u16 val)
506{
507 d->flags = cpu_to_le16(val);
508}
509
510static inline u8 btrfs_dir_type(struct btrfs_dir_item *d)
511{
512 return d->type;
513}
514
515static inline void btrfs_set_dir_type(struct btrfs_dir_item *d, u8 val)
516{
517 d->type = val;
518}
519
a8a2ee0c
CM
520static inline u16 btrfs_dir_name_len(struct btrfs_dir_item *d)
521{
522 return le16_to_cpu(d->name_len);
523}
524
525static inline void btrfs_set_dir_name_len(struct btrfs_dir_item *d, u16 val)
1d4f6404 526{
a8a2ee0c 527 d->name_len = cpu_to_le16(val);
1d4f6404
CM
528}
529
e2fa7227
CM
530static inline void btrfs_disk_key_to_cpu(struct btrfs_key *cpu,
531 struct btrfs_disk_key *disk)
532{
533 cpu->offset = le64_to_cpu(disk->offset);
534 cpu->flags = le32_to_cpu(disk->flags);
535 cpu->objectid = le64_to_cpu(disk->objectid);
536}
537
538static inline void btrfs_cpu_key_to_disk(struct btrfs_disk_key *disk,
539 struct btrfs_key *cpu)
540{
541 disk->offset = cpu_to_le64(cpu->offset);
542 disk->flags = cpu_to_le32(cpu->flags);
543 disk->objectid = cpu_to_le64(cpu->objectid);
544}
545
62e2749e 546static inline u64 btrfs_disk_key_objectid(struct btrfs_disk_key *disk)
e2fa7227
CM
547{
548 return le64_to_cpu(disk->objectid);
549}
550
62e2749e
CM
551static inline void btrfs_set_disk_key_objectid(struct btrfs_disk_key *disk,
552 u64 val)
e2fa7227
CM
553{
554 disk->objectid = cpu_to_le64(val);
555}
556
62e2749e 557static inline u64 btrfs_disk_key_offset(struct btrfs_disk_key *disk)
e2fa7227
CM
558{
559 return le64_to_cpu(disk->offset);
560}
561
62e2749e
CM
562static inline void btrfs_set_disk_key_offset(struct btrfs_disk_key *disk,
563 u64 val)
e2fa7227
CM
564{
565 disk->offset = cpu_to_le64(val);
566}
567
62e2749e 568static inline u32 btrfs_disk_key_flags(struct btrfs_disk_key *disk)
e2fa7227
CM
569{
570 return le32_to_cpu(disk->flags);
571}
572
62e2749e
CM
573static inline void btrfs_set_disk_key_flags(struct btrfs_disk_key *disk,
574 u32 val)
e2fa7227
CM
575{
576 disk->flags = cpu_to_le32(val);
577}
578
62e2749e
CM
579static inline u32 btrfs_key_type(struct btrfs_key *key)
580{
581 return key->flags & BTRFS_KEY_TYPE_MASK;
582}
583
584static inline u32 btrfs_disk_key_type(struct btrfs_disk_key *key)
585{
586 return le32_to_cpu(key->flags) & BTRFS_KEY_TYPE_MASK;
587}
588
589static inline void btrfs_set_key_type(struct btrfs_key *key, u32 type)
590{
591 BUG_ON(type >= BTRFS_KEY_TYPE_MAX);
592 key->flags = (key->flags & ~((u64)BTRFS_KEY_TYPE_MASK)) | type;
593}
594
595static inline void btrfs_set_disk_key_type(struct btrfs_disk_key *key, u32 type)
596{
597 u32 flags = btrfs_disk_key_flags(key);
598 BUG_ON(type >= BTRFS_KEY_TYPE_MAX);
599 flags = (flags & ~((u64)BTRFS_KEY_TYPE_MASK)) | type;
600 btrfs_set_disk_key_flags(key, flags);
601}
602
bb492bb0 603static inline u64 btrfs_header_blocknr(struct btrfs_header *h)
7518a238 604{
bb492bb0 605 return le64_to_cpu(h->blocknr);
7518a238
CM
606}
607
bb492bb0 608static inline void btrfs_set_header_blocknr(struct btrfs_header *h, u64 blocknr)
7518a238 609{
bb492bb0 610 h->blocknr = cpu_to_le64(blocknr);
7518a238
CM
611}
612
7f5c1516
CM
613static inline u64 btrfs_header_generation(struct btrfs_header *h)
614{
615 return le64_to_cpu(h->generation);
616}
617
618static inline void btrfs_set_header_generation(struct btrfs_header *h,
619 u64 val)
620{
621 h->generation = cpu_to_le64(val);
622}
623
bb492bb0 624static inline u64 btrfs_header_parentid(struct btrfs_header *h)
7518a238 625{
bb492bb0 626 return le64_to_cpu(h->parentid);
7518a238
CM
627}
628
bb492bb0
CM
629static inline void btrfs_set_header_parentid(struct btrfs_header *h,
630 u64 parentid)
7518a238 631{
bb492bb0 632 h->parentid = cpu_to_le64(parentid);
7518a238
CM
633}
634
bb492bb0 635static inline u16 btrfs_header_nritems(struct btrfs_header *h)
7518a238 636{
bb492bb0 637 return le16_to_cpu(h->nritems);
7518a238
CM
638}
639
bb492bb0 640static inline void btrfs_set_header_nritems(struct btrfs_header *h, u16 val)
7518a238 641{
bb492bb0 642 h->nritems = cpu_to_le16(val);
7518a238
CM
643}
644
bb492bb0 645static inline u16 btrfs_header_flags(struct btrfs_header *h)
7518a238 646{
bb492bb0 647 return le16_to_cpu(h->flags);
7518a238
CM
648}
649
bb492bb0 650static inline void btrfs_set_header_flags(struct btrfs_header *h, u16 val)
7518a238 651{
bb492bb0 652 h->flags = cpu_to_le16(val);
7518a238
CM
653}
654
bb492bb0 655static inline int btrfs_header_level(struct btrfs_header *h)
7518a238 656{
9a6f11ed 657 return h->level;
7518a238
CM
658}
659
bb492bb0 660static inline void btrfs_set_header_level(struct btrfs_header *h, int level)
7518a238 661{
234b63a0 662 BUG_ON(level > BTRFS_MAX_LEVEL);
9a6f11ed 663 h->level = level;
7518a238
CM
664}
665
234b63a0 666static inline int btrfs_is_leaf(struct btrfs_node *n)
7518a238
CM
667{
668 return (btrfs_header_level(&n->header) == 0);
669}
670
3768f368
CM
671static inline u64 btrfs_root_blocknr(struct btrfs_root_item *item)
672{
673 return le64_to_cpu(item->blocknr);
674}
675
676static inline void btrfs_set_root_blocknr(struct btrfs_root_item *item, u64 val)
677{
678 item->blocknr = cpu_to_le64(val);
679}
680
681static inline u32 btrfs_root_refs(struct btrfs_root_item *item)
682{
683 return le32_to_cpu(item->refs);
684}
685
686static inline void btrfs_set_root_refs(struct btrfs_root_item *item, u32 val)
687{
688 item->refs = cpu_to_le32(val);
689}
690
691static inline u64 btrfs_super_blocknr(struct btrfs_super_block *s)
692{
693 return le64_to_cpu(s->blocknr);
694}
695
696static inline void btrfs_set_super_blocknr(struct btrfs_super_block *s, u64 val)
697{
698 s->blocknr = cpu_to_le64(val);
699}
700
701static inline u64 btrfs_super_root(struct btrfs_super_block *s)
702{
703 return le64_to_cpu(s->root);
704}
705
706static inline void btrfs_set_super_root(struct btrfs_super_block *s, u64 val)
707{
708 s->root = cpu_to_le64(val);
709}
710
711static inline u64 btrfs_super_total_blocks(struct btrfs_super_block *s)
712{
713 return le64_to_cpu(s->total_blocks);
714}
715
716static inline void btrfs_set_super_total_blocks(struct btrfs_super_block *s,
717 u64 val)
718{
719 s->total_blocks = cpu_to_le64(val);
720}
721
722static inline u64 btrfs_super_blocks_used(struct btrfs_super_block *s)
723{
724 return le64_to_cpu(s->blocks_used);
725}
726
727static inline void btrfs_set_super_blocks_used(struct btrfs_super_block *s,
728 u64 val)
729{
730 s->blocks_used = cpu_to_le64(val);
731}
732
123abc88 733static inline u32 btrfs_super_blocksize(struct btrfs_super_block *s)
3768f368 734{
123abc88 735 return le32_to_cpu(s->blocksize);
3768f368
CM
736}
737
738static inline void btrfs_set_super_blocksize(struct btrfs_super_block *s,
123abc88
CM
739 u32 val)
740{
741 s->blocksize = cpu_to_le32(val);
742}
743
2e635a27
CM
744static inline u64 btrfs_super_root_dir(struct btrfs_super_block *s)
745{
746 return le64_to_cpu(s->root_dir_objectid);
747}
748
749static inline void btrfs_set_super_root_dir(struct btrfs_super_block *s, u64
750 val)
751{
752 s->root_dir_objectid = cpu_to_le64(val);
753}
754
123abc88 755static inline u8 *btrfs_leaf_data(struct btrfs_leaf *l)
3768f368 756{
123abc88 757 return (u8 *)l->items;
3768f368 758}
9f5fae2f
CM
759
760static inline u64 btrfs_file_extent_disk_blocknr(struct btrfs_file_extent_item
761 *e)
762{
763 return le64_to_cpu(e->disk_blocknr);
764}
765
766static inline void btrfs_set_file_extent_disk_blocknr(struct
767 btrfs_file_extent_item
768 *e, u64 val)
769{
770 e->disk_blocknr = cpu_to_le64(val);
771}
772
71951f35
CM
773static inline u64 btrfs_file_extent_generation(struct btrfs_file_extent_item *e)
774{
775 return le64_to_cpu(e->generation);
776}
777
778static inline void btrfs_set_file_extent_generation(struct
779 btrfs_file_extent_item *e,
780 u64 val)
781{
782 e->generation = cpu_to_le64(val);
783}
784
9f5fae2f
CM
785static inline u64 btrfs_file_extent_disk_num_blocks(struct
786 btrfs_file_extent_item *e)
787{
788 return le64_to_cpu(e->disk_num_blocks);
789}
790
791static inline void btrfs_set_file_extent_disk_num_blocks(struct
792 btrfs_file_extent_item
793 *e, u64 val)
794{
795 e->disk_num_blocks = cpu_to_le64(val);
796}
797
798static inline u64 btrfs_file_extent_offset(struct btrfs_file_extent_item *e)
799{
800 return le64_to_cpu(e->offset);
801}
802
803static inline void btrfs_set_file_extent_offset(struct btrfs_file_extent_item
804 *e, u64 val)
805{
806 e->offset = cpu_to_le64(val);
807}
808
809static inline u64 btrfs_file_extent_num_blocks(struct btrfs_file_extent_item
810 *e)
811{
812 return le64_to_cpu(e->num_blocks);
813}
814
815static inline void btrfs_set_file_extent_num_blocks(struct
816 btrfs_file_extent_item *e,
817 u64 val)
818{
819 e->num_blocks = cpu_to_le64(val);
820}
821
e20d96d6
CM
822static inline struct btrfs_root *btrfs_sb(struct super_block *sb)
823{
824 return sb->s_fs_info;
825}
826
4beb1b8b
CM
827/* helper function to cast into the data area of the leaf. */
828#define btrfs_item_ptr(leaf, slot, type) \
123abc88
CM
829 ((type *)(btrfs_leaf_data(leaf) + \
830 btrfs_item_offset((leaf)->items + (slot))))
4beb1b8b 831
dee26a9f 832/* extent-item.c */
e20d96d6 833struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
e089f05c 834 struct btrfs_root *root);
dee26a9f
CM
835int btrfs_alloc_extent(struct btrfs_trans_handle *trans, struct btrfs_root
836 *root, u64 num_blocks, u64 search_start, u64
837 search_end, u64 owner, struct btrfs_key *ins);
e089f05c 838int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
e20d96d6 839 struct buffer_head *buf);
e089f05c
CM
840int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
841 *root, u64 blocknr, u64 num_blocks, int pin);
dee26a9f
CM
842int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
843 btrfs_root *root);
844/* ctree.c */
e089f05c
CM
845int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
846 *root, struct btrfs_key *key, struct btrfs_path *p, int
847 ins_len, int cow);
234b63a0
CM
848void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p);
849void btrfs_init_path(struct btrfs_path *p);
e089f05c
CM
850int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
851 struct btrfs_path *path);
852int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
853 *root, struct btrfs_key *key, void *data, u32 data_size);
854int btrfs_insert_empty_item(struct btrfs_trans_handle *trans, struct btrfs_root
855 *root, struct btrfs_path *path, struct btrfs_key
856 *cpu_key, u32 data_size);
234b63a0 857int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path);
123abc88 858int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf);
e089f05c 859int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
e20d96d6 860 *root, struct buffer_head *snap);
dee26a9f 861/* root-item.c */
e089f05c
CM
862int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
863 struct btrfs_key *key);
864int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
865 *root, struct btrfs_key *key, struct btrfs_root_item
866 *item);
867int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
868 *root, struct btrfs_key *key, struct btrfs_root_item
869 *item);
870int btrfs_find_last_root(struct btrfs_root *root, u64 objectid, struct
871 btrfs_root_item *item, struct btrfs_key *key);
dee26a9f 872/* dir-item.c */
e089f05c 873int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
d5719762 874 *root, const char *name, int name_len, u64 dir, u64
e089f05c
CM
875 objectid, u8 type);
876int btrfs_lookup_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
e20d96d6
CM
877 *root, struct btrfs_path *path, u64 dir,
878 const char *name, int name_len, int mod);
1d4f6404 879int btrfs_match_dir_item_name(struct btrfs_root *root, struct btrfs_path *path,
7f5c1516 880 const char *name, int name_len);
dee26a9f 881/* inode-map.c */
9f5fae2f
CM
882int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
883 struct btrfs_root *fs_root,
884 u64 dirid, u64 *objectid);
885int btrfs_insert_inode_map(struct btrfs_trans_handle *trans,
886 struct btrfs_root *root,
887 u64 objectid, struct btrfs_key *location);
888int btrfs_lookup_inode_map(struct btrfs_trans_handle *trans,
889 struct btrfs_root *root, struct btrfs_path *path,
890 u64 objectid, int mod);
dee26a9f 891/* inode-item.c */
293ffd5f
CM
892int btrfs_insert_inode(struct btrfs_trans_handle *trans, struct btrfs_root
893 *root, u64 objectid, struct btrfs_inode_item
894 *inode_item);
895int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root
896 *root, struct btrfs_path *path, u64 objectid, int mod);
dee26a9f
CM
897
898/* file-item.c */
899int btrfs_alloc_file_extent(struct btrfs_trans_handle *trans,
900 struct btrfs_root *root,
901 u64 objectid, u64 offset,
902 u64 num_blocks, u64 hint_block,
903 u64 *result);
904int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
905 struct btrfs_root *root,
906 struct btrfs_path *path, u64 objectid,
9773a788 907 u64 blocknr, int mod);
eb60ceac 908#endif