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