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