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