Add back pointers from the inode to the directory that referenced it
[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 #include "radix-tree.h"
25 #include "extent-cache.h"
26 struct btrfs_root;
27 struct btrfs_trans_handle;
28 #define BTRFS_MAGIC "_B2RfS_M"
29
30 #define BTRFS_ROOT_TREE_OBJECTID 1ULL
31 #define BTRFS_EXTENT_TREE_OBJECTID 2ULL
32 #define BTRFS_FS_TREE_OBJECTID 3ULL
33 #define BTRFS_ROOT_TREE_DIR_OBJECTID 4ULL
34 #define BTRFS_FIRST_FREE_OBJECTID 5ULL
35
36 /*
37  * we can actually store much bigger names, but lets not confuse the rest
38  * of linux
39  */
40 #define BTRFS_NAME_LEN 255
41
42 /* 32 bytes in various csum fields */
43 #define BTRFS_CSUM_SIZE 32
44 /* four bytes for CRC32 */
45 #define BTRFS_CRC32_SIZE 4
46
47 #define BTRFS_FT_UNKNOWN        0
48 #define BTRFS_FT_REG_FILE       1
49 #define BTRFS_FT_DIR            2
50 #define BTRFS_FT_CHRDEV         3
51 #define BTRFS_FT_BLKDEV         4
52 #define BTRFS_FT_FIFO           5
53 #define BTRFS_FT_SOCK           6
54 #define BTRFS_FT_SYMLINK        7
55 #define BTRFS_FT_XATTR          8
56 #define BTRFS_FT_MAX            9
57
58 /*
59  * the key defines the order in the tree, and so it also defines (optimal)
60  * block layout.  objectid corresonds to the inode number.  The flags
61  * tells us things about the object, and is a kind of stream selector.
62  * so for a given inode, keys with flags of 1 might refer to the inode
63  * data, flags of 2 may point to file data in the btree and flags == 3
64  * may point to extents.
65  *
66  * offset is the starting byte offset for this key in the stream.
67  *
68  * btrfs_disk_key is in disk byte order.  struct btrfs_key is always
69  * in cpu native order.  Otherwise they are identical and their sizes
70  * should be the same (ie both packed)
71  */
72 struct btrfs_disk_key {
73         __le64 objectid;
74         u8 type;
75         __le64 offset;
76 } __attribute__ ((__packed__));
77
78 struct btrfs_key {
79         u64 objectid;
80         u8 type;
81         u64 offset;
82 } __attribute__ ((__packed__));
83
84 /*
85  * every tree block (leaf or node) starts with this header.
86  */
87 struct btrfs_header {
88         u8 csum[BTRFS_CSUM_SIZE];
89         u8 fsid[16]; /* FS specific uuid */
90         __le64 bytenr; /* which block this node is supposed to live in */
91         __le64 generation;
92         __le64 owner;
93         __le32 nritems;
94         __le16 flags;
95         u8 level;
96 } __attribute__ ((__packed__));
97
98 #define BTRFS_MAX_LEVEL 8
99 #define BTRFS_NODEPTRS_PER_BLOCK(r) (((r)->nodesize - \
100                                 sizeof(struct btrfs_header)) / \
101                                sizeof(struct btrfs_key_ptr))
102 #define __BTRFS_LEAF_DATA_SIZE(bs) ((bs) - sizeof(struct btrfs_header))
103 #define BTRFS_LEAF_DATA_SIZE(r) (__BTRFS_LEAF_DATA_SIZE(r->leafsize))
104 #define BTRFS_MAX_INLINE_DATA_SIZE(r) (BTRFS_LEAF_DATA_SIZE(r) - \
105                                         sizeof(struct btrfs_item) - \
106                                         sizeof(struct btrfs_file_extent_item))
107 struct btrfs_buffer;
108 /*
109  * the super block basically lists the main trees of the FS
110  * it currently lacks any block count etc etc
111  */
112 struct btrfs_super_block {
113         u8 csum[BTRFS_CSUM_SIZE];
114         /* the first 3 fields must match struct btrfs_header */
115         u8 fsid[16];    /* FS specific uuid */
116         __le64 bytenr; /* this block number */
117         __le64 magic;
118         __le64 generation;
119         __le64 root;
120         __le64 total_bytes;
121         __le64 bytes_used;
122         __le64 root_dir_objectid;
123         __le32 sectorsize;
124         __le32 nodesize;
125         __le32 leafsize;
126         __le32 stripesize;
127         u8 root_level;
128 } __attribute__ ((__packed__));
129
130 /*
131  * A leaf is full of items. offset and size tell us where to find
132  * the item in the leaf (relative to the start of the data area)
133  */
134 struct btrfs_item {
135         struct btrfs_disk_key key;
136         __le32 offset;
137         __le32 size;
138 } __attribute__ ((__packed__));
139
140 /*
141  * leaves have an item area and a data area:
142  * [item0, item1....itemN] [free space] [dataN...data1, data0]
143  *
144  * The data is separate from the items to get the keys closer together
145  * during searches.
146  */
147 struct btrfs_leaf {
148         struct btrfs_header header;
149         struct btrfs_item items[];
150 } __attribute__ ((__packed__));
151
152 /*
153  * all non-leaf blocks are nodes, they hold only keys and pointers to
154  * other blocks
155  */
156 struct btrfs_key_ptr {
157         struct btrfs_disk_key key;
158         __le64 blockptr;
159         __le64 generation;
160 } __attribute__ ((__packed__));
161
162 struct btrfs_node {
163         struct btrfs_header header;
164         struct btrfs_key_ptr ptrs[];
165 } __attribute__ ((__packed__));
166
167 /*
168  * btrfs_paths remember the path taken from the root down to the leaf.
169  * level 0 is always the leaf, and nodes[1...BTRFS_MAX_LEVEL] will point
170  * to any other levels that are present.
171  *
172  * The slots array records the index of the item or block pointer
173  * used while walking the tree.
174  */
175 struct btrfs_path {
176         struct btrfs_buffer *nodes[BTRFS_MAX_LEVEL];
177         int slots[BTRFS_MAX_LEVEL];
178 };
179
180 /*
181  * items in the extent btree are used to record the objectid of the
182  * owner of the block and the number of references
183  */
184 struct btrfs_extent_item {
185         __le32 refs;
186 } __attribute__ ((__packed__));
187
188 struct btrfs_extent_ref {
189         __le64 root;
190         __le64 generation;
191         __le64 objectid;
192         __le64 offset;
193 } __attribute__ ((__packed__));
194
195 struct btrfs_inode_ref {
196         __le16 name_len;
197         /* name goes here */
198 } __attribute__ ((__packed__));
199
200 struct btrfs_inode_timespec {
201         __le64 sec;
202         __le32 nsec;
203 } __attribute__ ((__packed__));
204
205 /*
206  * there is no padding here on purpose.  If you want to extent the inode,
207  * make a new item type
208  */
209 struct btrfs_inode_item {
210         __le64 generation;
211         __le64 size;
212         __le64 nblocks;
213         __le64 block_group;
214         __le32 nlink;
215         __le32 uid;
216         __le32 gid;
217         __le32 mode;
218         __le32 rdev;
219         __le16 flags;
220         __le16 compat_flags;
221         struct btrfs_inode_timespec atime;
222         struct btrfs_inode_timespec ctime;
223         struct btrfs_inode_timespec mtime;
224         struct btrfs_inode_timespec otime;
225 } __attribute__ ((__packed__));
226
227 /* inline data is just a blob of bytes */
228 struct btrfs_inline_data_item {
229         u8 data;
230 } __attribute__ ((__packed__));
231
232 struct btrfs_dir_item {
233         struct btrfs_disk_key location;
234         __le16 data_len;
235         __le16 name_len;
236         u8 type;
237 } __attribute__ ((__packed__));
238
239 struct btrfs_root_item {
240         struct btrfs_inode_item inode;
241         __le64 root_dirid;
242         __le64 bytenr;
243         __le64 byte_limit;
244         __le64 bytes_used;
245         __le32 flags;
246         __le32 refs;
247         struct btrfs_disk_key drop_progress;
248         u8 drop_level;
249         u8 level;
250 } __attribute__ ((__packed__));
251
252 #define BTRFS_FILE_EXTENT_REG 0
253 #define BTRFS_FILE_EXTENT_INLINE 1
254
255 struct btrfs_file_extent_item {
256         __le64 generation;
257         u8 type;
258         /*
259          * disk space consumed by the extent, checksum blocks are included
260          * in these numbers
261          */
262         __le64 disk_bytenr;
263         __le64 disk_num_bytes;
264         /*
265          * the logical offset in file blocks (no csums)
266          * this extent record is for.  This allows a file extent to point
267          * into the middle of an existing extent on disk, sharing it
268          * between two snapshots (useful if some bytes in the middle of the
269          * extent have changed
270          */
271         __le64 offset;
272         /*
273          * the logical number of file blocks (no csums included)
274          */
275         __le64 num_bytes;
276 } __attribute__ ((__packed__));
277
278 struct btrfs_csum_item {
279         u8 csum[BTRFS_CSUM_SIZE];
280 } __attribute__ ((__packed__));
281
282 /* tag for the radix tree of block groups in ram */
283 #define BTRFS_BLOCK_GROUP_DIRTY 0
284 #define BTRFS_BLOCK_GROUP_SIZE (256 * 1024 * 1024)
285
286
287 #define BTRFS_BLOCK_GROUP_DATA 1
288 struct btrfs_block_group_item {
289         __le64 used;
290         u8 flags;
291 } __attribute__ ((__packed__));
292
293 struct btrfs_block_group_cache {
294         struct cache_extent cache;
295         struct btrfs_key key;
296         struct btrfs_block_group_item item;
297         int dirty;
298 };
299
300 struct btrfs_fs_info {
301         struct btrfs_root *fs_root;
302         struct btrfs_root *extent_root;
303         struct btrfs_root *tree_root;
304         struct btrfs_key last_insert;
305         struct cache_tree extent_cache;
306         struct cache_tree block_group_cache;
307         struct cache_tree pending_tree;
308         struct cache_tree pinned_tree;
309         struct cache_tree del_pending;
310         struct list_head trans;
311         struct list_head cache;
312         u64 last_inode_alloc;
313         u64 last_inode_alloc_dirid;
314         u64 generation;
315         int cache_size;
316         int fp;
317         struct btrfs_trans_handle *running_transaction;
318         struct btrfs_super_block *disk_super;
319 };
320
321 /*
322  * in ram representation of the tree.  extent_root is used for all allocations
323  * and for the extent tree extent_root root.
324  */
325 struct btrfs_root {
326         struct btrfs_buffer *node;
327         struct btrfs_buffer *commit_root;
328         struct btrfs_root_item root_item;
329         struct btrfs_key root_key;
330         struct btrfs_fs_info *fs_info;
331
332         /* data allocations are done in sectorsize units */
333         u32 sectorsize;
334
335         /* node allocations are done in nodesize units */
336         u32 nodesize;
337
338         /* leaf allocations are done in leafsize units */
339         u32 leafsize;
340
341         /* leaf allocations are done in leafsize units */
342         u32 stripesize;
343
344         int ref_cows;
345         u32 type;
346 };
347
348 /* the lower bits in the key flags defines the item type */
349 #define BTRFS_KEY_TYPE_MAX      256
350 #define BTRFS_KEY_TYPE_SHIFT    24
351 #define BTRFS_KEY_TYPE_MASK     (((u32)BTRFS_KEY_TYPE_MAX - 1) << \
352                                   BTRFS_KEY_TYPE_SHIFT)
353
354 /*
355  * inode items have the data typically returned from stat and store other
356  * info about object characteristics.  There is one for every file and dir in
357  * the FS
358  */
359 #define BTRFS_INODE_ITEM_KEY            1
360 #define BTRFS_INODE_REF_KEY             2
361 #define BTRFS_XATTR_ITEM_KEY            8
362
363 /* reserve 3-15 close to the inode for later flexibility */
364
365 /*
366  * dir items are the name -> inode pointers in a directory.  There is one
367  * for every name in a directory.
368  */
369 #define BTRFS_DIR_ITEM_KEY      16
370 #define BTRFS_DIR_INDEX_KEY     17
371 /*
372  * extent data is for file data
373  */
374 #define BTRFS_EXTENT_DATA_KEY   18
375 /*
376  * csum items have the checksums for data in the extents
377  */
378 #define BTRFS_CSUM_ITEM_KEY     19
379
380 /* reserve 20-31 for other file stuff */
381
382 /*
383  * root items point to tree roots.  There are typically in the root
384  * tree used by the super block to find all the other trees
385  */
386 #define BTRFS_ROOT_ITEM_KEY     32
387 /*
388  * extent items are in the extent map tree.  These record which blocks
389  * are used, and how many references there are to each block
390  */
391 #define BTRFS_EXTENT_ITEM_KEY   33
392 #define BTRFS_EXTENT_REF_KEY    34
393
394 /*
395  * block groups give us hints into the extent allocation trees.  Which
396  * blocks are free etc etc
397  */
398 #define BTRFS_BLOCK_GROUP_ITEM_KEY 50
399
400 /*
401  * string items are for debugging.  They just store a short string of
402  * data in the FS
403  */
404 #define BTRFS_STRING_ITEM_KEY   253
405
406 #define BTRFS_SETGET_STACK_FUNCS(name, type, member, bits)              \
407 static inline u##bits btrfs_##name(type *s)                             \
408 {                                                                       \
409         return le##bits##_to_cpu(s->member);                            \
410 }                                                                       \
411 static inline void btrfs_set_##name(type *s, u##bits val)               \
412 {                                                                       \
413         s->member = cpu_to_le##bits(val);                               \
414 }
415
416 static inline u64 btrfs_block_group_used(struct btrfs_block_group_item *bi)
417 {
418         return le64_to_cpu(bi->used);
419 }
420
421 static inline void btrfs_set_block_group_used(struct
422                                                    btrfs_block_group_item *bi,
423                                                    u64 val)
424 {
425         bi->used = cpu_to_le64(val);
426 }
427
428 static inline u64 btrfs_inode_generation(struct btrfs_inode_item *i)
429 {
430         return le64_to_cpu(i->generation);
431 }
432
433 static inline void btrfs_set_inode_generation(struct btrfs_inode_item *i,
434                                               u64 val)
435 {
436         i->generation = cpu_to_le64(val);
437 }
438
439 static inline u64 btrfs_inode_size(struct btrfs_inode_item *i)
440 {
441         return le64_to_cpu(i->size);
442 }
443
444 static inline void btrfs_set_inode_size(struct btrfs_inode_item *i, u64 val)
445 {
446         i->size = cpu_to_le64(val);
447 }
448
449 static inline u64 btrfs_inode_nblocks(struct btrfs_inode_item *i)
450 {
451         return le64_to_cpu(i->nblocks);
452 }
453
454 static inline void btrfs_set_inode_nblocks(struct btrfs_inode_item *i, u64 val)
455 {
456         i->nblocks = cpu_to_le64(val);
457 }
458
459 static inline u64 btrfs_inode_block_group(struct btrfs_inode_item *i)
460 {
461         return le64_to_cpu(i->block_group);
462 }
463
464 static inline void btrfs_set_inode_block_group(struct btrfs_inode_item *i,
465                                                 u64 val)
466 {
467         i->block_group = cpu_to_le64(val);
468 }
469
470 static inline u32 btrfs_inode_nlink(struct btrfs_inode_item *i)
471 {
472         return le32_to_cpu(i->nlink);
473 }
474
475 static inline void btrfs_set_inode_nlink(struct btrfs_inode_item *i, u32 val)
476 {
477         i->nlink = cpu_to_le32(val);
478 }
479
480 static inline u32 btrfs_inode_uid(struct btrfs_inode_item *i)
481 {
482         return le32_to_cpu(i->uid);
483 }
484
485 static inline void btrfs_set_inode_uid(struct btrfs_inode_item *i, u32 val)
486 {
487         i->uid = cpu_to_le32(val);
488 }
489
490 static inline u32 btrfs_inode_gid(struct btrfs_inode_item *i)
491 {
492         return le32_to_cpu(i->gid);
493 }
494
495 static inline void btrfs_set_inode_gid(struct btrfs_inode_item *i, u32 val)
496 {
497         i->gid = cpu_to_le32(val);
498 }
499
500 static inline u32 btrfs_inode_mode(struct btrfs_inode_item *i)
501 {
502         return le32_to_cpu(i->mode);
503 }
504
505 static inline void btrfs_set_inode_mode(struct btrfs_inode_item *i, u32 val)
506 {
507         i->mode = cpu_to_le32(val);
508 }
509
510 static inline u32 btrfs_inode_rdev(struct btrfs_inode_item *i)
511 {
512         return le32_to_cpu(i->rdev);
513 }
514
515 static inline void btrfs_set_inode_rdev(struct btrfs_inode_item *i, u32 val)
516 {
517         i->rdev = cpu_to_le32(val);
518 }
519
520 static inline u16 btrfs_inode_flags(struct btrfs_inode_item *i)
521 {
522         return le16_to_cpu(i->flags);
523 }
524
525 static inline void btrfs_set_inode_flags(struct btrfs_inode_item *i, u16 val)
526 {
527         i->flags = cpu_to_le16(val);
528 }
529
530 static inline u16 btrfs_inode_compat_flags(struct btrfs_inode_item *i)
531 {
532         return le16_to_cpu(i->compat_flags);
533 }
534
535 static inline void btrfs_set_inode_compat_flags(struct btrfs_inode_item *i,
536                                                 u16 val)
537 {
538         i->compat_flags = cpu_to_le16(val);
539 }
540
541 static inline u64 btrfs_timespec_sec(struct btrfs_inode_timespec *ts)
542 {
543         return le64_to_cpu(ts->sec);
544 }
545
546 static inline void btrfs_set_timespec_sec(struct btrfs_inode_timespec *ts,
547                                           u64 val)
548 {
549         ts->sec = cpu_to_le64(val);
550 }
551
552 static inline u32 btrfs_timespec_nsec(struct btrfs_inode_timespec *ts)
553 {
554         return le32_to_cpu(ts->nsec);
555 }
556
557 static inline void btrfs_set_timespec_nsec(struct btrfs_inode_timespec *ts,
558                                           u32 val)
559 {
560         ts->nsec = cpu_to_le32(val);
561 }
562
563 BTRFS_SETGET_STACK_FUNCS(extent_refs, struct btrfs_extent_item, refs, 32);
564
565 BTRFS_SETGET_STACK_FUNCS(inode_ref_name_len, struct btrfs_inode_ref,
566                          name_len, 16);
567
568
569 BTRFS_SETGET_STACK_FUNCS(ref_root, struct btrfs_extent_ref, root, 64);
570 BTRFS_SETGET_STACK_FUNCS(ref_generation, struct btrfs_extent_ref,
571                          generation, 64);
572 BTRFS_SETGET_STACK_FUNCS(ref_objectid, struct btrfs_extent_ref, objectid, 64);
573 BTRFS_SETGET_STACK_FUNCS(ref_offset, struct btrfs_extent_ref, offset, 64);
574
575 static inline u64 btrfs_node_blockptr(struct btrfs_node *n, int nr)
576 {
577         return le64_to_cpu(n->ptrs[nr].blockptr);
578 }
579
580 static inline void btrfs_set_node_blockptr(struct btrfs_node *n, int nr,
581                                            u64 val)
582 {
583         n->ptrs[nr].blockptr = cpu_to_le64(val);
584 }
585
586 static inline u64 btrfs_node_ptr_generation(struct btrfs_node *n, int nr)
587 {
588         return le64_to_cpu(n->ptrs[nr].generation);
589 }
590
591 static inline void btrfs_set_node_ptr_generation(struct btrfs_node *n, int nr,
592                                                  u64 val)
593 {
594         n->ptrs[nr].generation = cpu_to_le64(val);
595 }
596
597 static inline u32 btrfs_item_offset(struct btrfs_item *item)
598 {
599         return le32_to_cpu(item->offset);
600 }
601
602 static inline void btrfs_set_item_offset(struct btrfs_item *item, u32 val)
603 {
604         item->offset = cpu_to_le32(val);
605 }
606
607 static inline u32 btrfs_item_end(struct btrfs_item *item)
608 {
609         return le32_to_cpu(item->offset) + le32_to_cpu(item->size);
610 }
611
612 static inline u32 btrfs_item_size(struct btrfs_item *item)
613 {
614         return le32_to_cpu(item->size);
615 }
616
617 static inline void btrfs_set_item_size(struct btrfs_item *item, u32 val)
618 {
619         item->size = cpu_to_le32(val);
620 }
621
622 static inline u8 btrfs_dir_type(struct btrfs_dir_item *d)
623 {
624         return d->type;
625 }
626
627 static inline void btrfs_set_dir_type(struct btrfs_dir_item *d, u8 val)
628 {
629         d->type = val;
630 }
631
632 static inline u16 btrfs_dir_name_len(struct btrfs_dir_item *d)
633 {
634         return le16_to_cpu(d->name_len);
635 }
636
637 static inline void btrfs_set_dir_name_len(struct btrfs_dir_item *d, u16 val)
638 {
639         d->name_len = cpu_to_le16(val);
640 }
641
642 static inline u16 btrfs_dir_data_len(struct btrfs_dir_item *d)
643 {
644         return le16_to_cpu(d->data_len);
645 }
646
647 static inline void btrfs_set_dir_data_len(struct btrfs_dir_item *d, u16 val)
648 {
649         d->data_len = cpu_to_le16(val);
650 }
651
652 static inline void btrfs_disk_key_to_cpu(struct btrfs_key *cpu,
653                                          struct btrfs_disk_key *disk)
654 {
655         cpu->offset = le64_to_cpu(disk->offset);
656         cpu->type = le32_to_cpu(disk->type);
657         cpu->objectid = le64_to_cpu(disk->objectid);
658 }
659
660 static inline void btrfs_cpu_key_to_disk(struct btrfs_disk_key *disk,
661                                          struct btrfs_key *cpu)
662 {
663         disk->offset = cpu_to_le64(cpu->offset);
664         disk->type = cpu_to_le32(cpu->type);
665         disk->objectid = cpu_to_le64(cpu->objectid);
666 }
667
668 static inline u64 btrfs_disk_key_objectid(struct btrfs_disk_key *disk)
669 {
670         return le64_to_cpu(disk->objectid);
671 }
672
673 static inline void btrfs_set_disk_key_objectid(struct btrfs_disk_key *disk,
674                                                u64 val)
675 {
676         disk->objectid = cpu_to_le64(val);
677 }
678
679 static inline u64 btrfs_disk_key_offset(struct btrfs_disk_key *disk)
680 {
681         return le64_to_cpu(disk->offset);
682 }
683
684 static inline void btrfs_set_disk_key_offset(struct btrfs_disk_key *disk,
685                                              u64 val)
686 {
687         disk->offset = cpu_to_le64(val);
688 }
689
690 static inline u8 btrfs_disk_key_type(struct btrfs_disk_key *key)
691 {
692         return key->type;
693 }
694
695 static inline void btrfs_set_disk_key_type(struct btrfs_disk_key *key, u8 val)
696 {
697         key->type = val;
698 }
699
700 static inline u32 btrfs_key_type(struct btrfs_key *key)
701 {
702         return key->type;
703 }
704
705 static inline void btrfs_set_key_type(struct btrfs_key *key, u32 val)
706 {
707         key->type = val;
708 }
709
710 static inline u64 btrfs_header_bytenr(struct btrfs_header *h)
711 {
712         return le64_to_cpu(h->bytenr);
713 }
714
715 static inline void btrfs_set_header_bytenr(struct btrfs_header *h, u64 bytenr)
716 {
717         h->bytenr = cpu_to_le64(bytenr);
718 }
719
720 static inline u64 btrfs_header_generation(struct btrfs_header *h)
721 {
722         return le64_to_cpu(h->generation);
723 }
724
725 static inline void btrfs_set_header_generation(struct btrfs_header *h,
726                                                u64 val)
727 {
728         h->generation = cpu_to_le64(val);
729 }
730
731 static inline u64 btrfs_header_owner(struct btrfs_header *h)
732 {
733         return le64_to_cpu(h->owner);
734 }
735
736 static inline void btrfs_set_header_owner(struct btrfs_header *h,
737                                                u64 val)
738 {
739         h->owner = cpu_to_le64(val);
740 }
741
742 static inline u32 btrfs_header_nritems(struct btrfs_header *h)
743 {
744         return le32_to_cpu(h->nritems);
745 }
746
747 static inline void btrfs_set_header_nritems(struct btrfs_header *h, u32 val)
748 {
749         h->nritems = cpu_to_le32(val);
750 }
751
752 static inline u16 btrfs_header_flags(struct btrfs_header *h)
753 {
754         return le16_to_cpu(h->flags);
755 }
756
757 static inline void btrfs_set_header_flags(struct btrfs_header *h, u16 val)
758 {
759         h->flags = cpu_to_le16(val);
760 }
761
762 static inline int btrfs_header_level(struct btrfs_header *h)
763 {
764         return h->level;
765 }
766
767 static inline void btrfs_set_header_level(struct btrfs_header *h, int level)
768 {
769         BUG_ON(level > BTRFS_MAX_LEVEL);
770         h->level = level;
771 }
772
773 static inline int btrfs_is_leaf(struct btrfs_node *n)
774 {
775         return (btrfs_header_level(&n->header) == 0);
776 }
777
778 static inline u64 btrfs_root_bytenr(struct btrfs_root_item *item)
779 {
780         return le64_to_cpu(item->bytenr);
781 }
782
783 static inline void btrfs_set_root_bytenr(struct btrfs_root_item *item, u64 val)
784 {
785         item->bytenr = cpu_to_le64(val);
786 }
787
788 static inline u64 btrfs_root_byte_limit(struct btrfs_root_item *item)
789 {
790         return le64_to_cpu(item->byte_limit);
791 }
792
793 static inline void btrfs_set_root_byte_limit(struct btrfs_root_item *item,
794                                              u64 val)
795 {
796         item->byte_limit = cpu_to_le64(val);
797 }
798
799 static inline u8 btrfs_root_level(struct btrfs_root_item *item)
800 {
801         return item->level;
802 }
803
804 static inline void btrfs_set_root_level(struct btrfs_root_item *item, u8 val)
805 {
806         item->level = val;
807 }
808
809 static inline u64 btrfs_root_dirid(struct btrfs_root_item *item)
810 {
811         return le64_to_cpu(item->root_dirid);
812 }
813
814 static inline void btrfs_set_root_dirid(struct btrfs_root_item *item, u64 val)
815 {
816         item->root_dirid = cpu_to_le64(val);
817 }
818
819 static inline u32 btrfs_root_refs(struct btrfs_root_item *item)
820 {
821         return le32_to_cpu(item->refs);
822 }
823
824 static inline void btrfs_set_root_refs(struct btrfs_root_item *item, u32 val)
825 {
826         item->refs = cpu_to_le32(val);
827 }
828
829 static inline u32 btrfs_root_flags(struct btrfs_root_item *item)
830 {
831         return le32_to_cpu(item->flags);
832 }
833
834 static inline void btrfs_set_root_flags(struct btrfs_root_item *item, u32 val)
835 {
836         item->flags = cpu_to_le32(val);
837 }
838
839 static inline void btrfs_set_root_bytes_used(struct btrfs_root_item *item,
840                                               u64 val)
841 {
842         item->bytes_used = cpu_to_le64(val);
843 }
844
845 static inline u64 btrfs_root_bytes_used(struct btrfs_root_item *item)
846 {
847         return le64_to_cpu(item->bytes_used);
848 }
849
850 static inline u64 btrfs_super_bytenr(struct btrfs_super_block *s)
851 {
852         return le64_to_cpu(s->bytenr);
853 }
854
855 static inline void btrfs_set_super_bytenr(struct btrfs_super_block *s, u64 val)
856 {
857         s->bytenr = cpu_to_le64(val);
858 }
859
860 static inline u64 btrfs_super_generation(struct btrfs_super_block *s)
861 {
862         return le64_to_cpu(s->generation);
863 }
864
865 static inline void btrfs_set_super_generation(struct btrfs_super_block *s,
866                                               u64 val)
867 {
868         s->generation = cpu_to_le64(val);
869 }
870
871 static inline u8 btrfs_super_root_level(struct btrfs_super_block *s)
872 {
873         return s->root_level;
874 }
875
876 static inline void btrfs_set_super_root_level(struct btrfs_super_block *s,
877                                               u8 val)
878 {
879         s->root_level = val;
880 }
881
882 static inline u64 btrfs_super_root(struct btrfs_super_block *s)
883 {
884         return le64_to_cpu(s->root);
885 }
886
887 static inline void btrfs_set_super_root(struct btrfs_super_block *s, u64 val)
888 {
889         s->root = cpu_to_le64(val);
890 }
891
892 static inline u64 btrfs_super_total_bytes(struct btrfs_super_block *s)
893 {
894         return le64_to_cpu(s->total_bytes);
895 }
896
897 static inline void btrfs_set_super_total_bytes(struct btrfs_super_block *s,
898                                                 u64 val)
899 {
900         s->total_bytes = cpu_to_le64(val);
901 }
902
903 static inline u64 btrfs_super_bytes_used(struct btrfs_super_block *s)
904 {
905         return le64_to_cpu(s->bytes_used);
906 }
907
908 static inline void btrfs_set_super_bytes_used(struct btrfs_super_block *s,
909                                                 u64 val)
910 {
911         s->bytes_used = cpu_to_le64(val);
912 }
913
914 static inline u32 btrfs_super_sectorsize(struct btrfs_super_block *s)
915 {
916         return le32_to_cpu(s->sectorsize);
917 }
918
919 static inline void btrfs_set_super_sectorsize(struct btrfs_super_block *s,
920                                                 u32 val)
921 {
922         s->sectorsize = cpu_to_le32(val);
923 }
924
925 static inline u32 btrfs_super_nodesize(struct btrfs_super_block *s)
926 {
927         return le32_to_cpu(s->nodesize);
928 }
929
930 static inline void btrfs_set_super_nodesize(struct btrfs_super_block *s,
931                                                 u32 val)
932 {
933         s->nodesize = cpu_to_le32(val);
934 }
935
936 static inline u32 btrfs_super_leafsize(struct btrfs_super_block *s)
937 {
938         return le32_to_cpu(s->leafsize);
939 }
940
941 static inline void btrfs_set_super_leafsize(struct btrfs_super_block *s,
942                                                 u32 val)
943 {
944         s->leafsize = cpu_to_le32(val);
945 }
946
947 static inline u32 btrfs_super_stripesize(struct btrfs_super_block *s)
948 {
949         return le32_to_cpu(s->stripesize);
950 }
951
952 static inline void btrfs_set_super_stripesize(struct btrfs_super_block *s,
953                                                 u32 val)
954 {
955         s->stripesize = cpu_to_le32(val);
956 }
957
958 static inline u64 btrfs_super_root_dir(struct btrfs_super_block *s)
959 {
960         return le64_to_cpu(s->root_dir_objectid);
961 }
962
963 static inline void btrfs_set_super_root_dir(struct btrfs_super_block *s, u64
964                                             val)
965 {
966         s->root_dir_objectid = cpu_to_le64(val);
967 }
968
969 static inline u8 *btrfs_leaf_data(struct btrfs_leaf *l)
970 {
971         return (u8 *)l->items;
972 }
973
974 static inline int btrfs_file_extent_type(struct btrfs_file_extent_item *e)
975 {
976         return e->type;
977 }
978 static inline void btrfs_set_file_extent_type(struct btrfs_file_extent_item *e,
979                                               u8 val)
980 {
981         e->type = val;
982 }
983
984 static inline char *btrfs_file_extent_inline_start(struct
985                                                    btrfs_file_extent_item *e)
986 {
987         return (char *)(&e->disk_bytenr);
988 }
989
990 static inline u32 btrfs_file_extent_calc_inline_size(u32 datasize)
991 {
992         return (unsigned long)(&((struct
993                   btrfs_file_extent_item *)NULL)->disk_bytenr) + datasize;
994 }
995
996 static inline u32 btrfs_file_extent_inline_len(struct btrfs_item *e)
997 {
998         struct btrfs_file_extent_item *fe = NULL;
999         return btrfs_item_size(e) - (unsigned long)(&fe->disk_bytenr);
1000 }
1001
1002 static inline u64 btrfs_file_extent_disk_bytenr(struct btrfs_file_extent_item
1003                                                  *e)
1004 {
1005         return le64_to_cpu(e->disk_bytenr);
1006 }
1007
1008 static inline void btrfs_set_file_extent_disk_bytenr(struct
1009                                                       btrfs_file_extent_item
1010                                                       *e, u64 val)
1011 {
1012         e->disk_bytenr = cpu_to_le64(val);
1013 }
1014
1015 static inline u64 btrfs_file_extent_generation(struct btrfs_file_extent_item *e)
1016 {
1017         return le64_to_cpu(e->generation);
1018 }
1019
1020 static inline void btrfs_set_file_extent_generation(struct
1021                                                     btrfs_file_extent_item *e,
1022                                                     u64 val)
1023 {
1024         e->generation = cpu_to_le64(val);
1025 }
1026
1027 static inline u64 btrfs_file_extent_disk_num_bytes(struct
1028                                                     btrfs_file_extent_item *e)
1029 {
1030         return le64_to_cpu(e->disk_num_bytes);
1031 }
1032
1033 static inline void btrfs_set_file_extent_disk_num_bytes(struct
1034                                                          btrfs_file_extent_item
1035                                                          *e, u64 val)
1036 {
1037         e->disk_num_bytes = cpu_to_le64(val);
1038 }
1039
1040 static inline u64 btrfs_file_extent_offset(struct btrfs_file_extent_item *e)
1041 {
1042         return le64_to_cpu(e->offset);
1043 }
1044
1045 static inline void btrfs_set_file_extent_offset(struct btrfs_file_extent_item
1046                                                 *e, u64 val)
1047 {
1048         e->offset = cpu_to_le64(val);
1049 }
1050
1051 static inline u64 btrfs_file_extent_num_bytes(struct btrfs_file_extent_item
1052                                                *e)
1053 {
1054         return le64_to_cpu(e->num_bytes);
1055 }
1056
1057 static inline void btrfs_set_file_extent_num_bytes(struct
1058                                                     btrfs_file_extent_item *e,
1059                                                     u64 val)
1060 {
1061         e->num_bytes = cpu_to_le64(val);
1062 }
1063
1064 /* helper function to cast into the data area of the leaf. */
1065 #define btrfs_item_ptr(leaf, slot, type) \
1066         ((type *)(btrfs_leaf_data(leaf) + \
1067         btrfs_item_offset((leaf)->items + (slot))))
1068 #define btrfs_item_ptr_offset(leaf, slot) \
1069         ((unsigned long)(btrfs_leaf_data(leaf) + \
1070         btrfs_item_offset_nr(leaf, slot)))
1071
1072 static inline u32 btrfs_level_size(struct btrfs_root *root, int level)
1073 {
1074         if (level == 0)
1075                 return root->leafsize;
1076         return root->nodesize;
1077 }
1078 int btrfs_comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2);
1079 struct btrfs_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
1080                                             struct btrfs_root *root,
1081                                             u32 blocksize);
1082 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1083                   struct btrfs_buffer *buf);
1084 int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
1085                        struct btrfs_root *root, u64 owner_objectid);
1086 int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
1087                       *root, u64 bytenr, u64 num_bytes,
1088                       u64 root_objectid, u64 root_generation,
1089                       u64 owner, u64 owner_offset, int pin);
1090 int btrfs_cow_block(struct btrfs_trans_handle *trans,
1091                     struct btrfs_root *root, struct btrfs_buffer *buf,
1092                     struct btrfs_buffer *parent, int parent_slot,
1093                     struct btrfs_buffer **cow_ret);
1094 int btrfs_extend_item(struct btrfs_trans_handle *trans, struct btrfs_root
1095                       *root, struct btrfs_path *path, u32 data_size);
1096 int btrfs_truncate_item(struct btrfs_trans_handle *trans,
1097                         struct btrfs_root *root,
1098                         struct btrfs_path *path,
1099                         u32 new_size, int from_end);
1100 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1101                       *root, struct btrfs_key *key, struct btrfs_path *p, int
1102                       ins_len, int cow);
1103 void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p);
1104 void btrfs_init_path(struct btrfs_path *p);
1105 int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1106                    struct btrfs_path *path);
1107 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
1108                       *root, struct btrfs_key *key, void *data, u32 data_size);
1109 int btrfs_insert_empty_item(struct btrfs_trans_handle *trans, struct btrfs_root
1110                             *root, struct btrfs_path *path, struct btrfs_key
1111                             *cpu_key, u32 data_size);
1112 int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path);
1113 int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf);
1114 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
1115                         *root, struct btrfs_buffer *snap);
1116 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
1117                                btrfs_root *root);
1118 int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1119                    struct btrfs_key *key);
1120 int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
1121                       *root, struct btrfs_key *key, struct btrfs_root_item
1122                       *item);
1123 int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
1124                       *root, struct btrfs_key *key, struct btrfs_root_item
1125                       *item);
1126 int btrfs_find_last_root(struct btrfs_root *root, u64 objectid, struct
1127                          btrfs_root_item *item, struct btrfs_key *key);
1128 int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
1129                           *root, char *name, int name_len, u64 dir,
1130                           struct btrfs_key *location, u8 type);
1131 struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans,
1132                           struct btrfs_root *root, struct btrfs_path *path,
1133                           u64 dir, char *name, int name_len, int mod);
1134 struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_root *root,
1135                               struct btrfs_path *path,
1136                               const char *name, int name_len);
1137 int btrfs_delete_one_dir_name(struct btrfs_trans_handle *trans,
1138                               struct btrfs_root *root,
1139                               struct btrfs_path *path,
1140                               struct btrfs_dir_item *di);
1141 int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
1142                              struct btrfs_root *fs_root,
1143                              u64 dirid, u64 *objectid);
1144 int btrfs_insert_inode(struct btrfs_trans_handle *trans, struct btrfs_root
1145                        *root, u64 objectid, struct btrfs_inode_item
1146                        *inode_item);
1147 int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root
1148                        *root, struct btrfs_path *path, u64 objectid, int mod);
1149 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1150                                     struct btrfs_root *root);
1151 int btrfs_free_block_groups(struct btrfs_fs_info *info);
1152 int btrfs_read_block_groups(struct btrfs_root *root);
1153 int btrfs_insert_block_group(struct btrfs_trans_handle *trans,
1154                              struct btrfs_root *root,
1155                              struct btrfs_key *key,
1156                              struct btrfs_block_group_item *bi);
1157 /* file-item.c */
1158 int btrfs_insert_file_extent(struct btrfs_trans_handle *trans,
1159                              struct btrfs_root *root,
1160                              u64 objectid, u64 pos, u64 offset,
1161                              u64 disk_num_bytes, u64 num_bytes);
1162 int btrfs_insert_inline_file_extent(struct btrfs_trans_handle *trans,
1163                                     struct btrfs_root *root, u64 objectid,
1164                                     u64 offset, char *buffer, size_t size);
1165 int btrfs_lookup_csum(struct btrfs_trans_handle *trans, struct btrfs_root
1166                       *root, struct btrfs_path *path, u64 objectid,
1167                       u64 offset, int cow, struct btrfs_csum_item **item_ret);
1168 int btrfs_csum_file_block(struct btrfs_trans_handle *trans, struct btrfs_root
1169                           *root, struct btrfs_inode_item *inode,
1170                           u64 objectid, u64 offset, char *data, size_t len);
1171 #endif