blob: b30b2ce724550ccd0d633c8c4d6e06060e50f278 [file] [log] [blame]
Chris Mason234b63a2007-03-13 10:46:10 -04001#ifndef __BTRFS__
2#define __BTRFS__
Chris Masoneb60cea2007-02-02 09:18:22 -05003
Chris Masoned2ff2c2007-03-01 18:59:40 -05004#include "list.h"
Chris Masone2fa7222007-03-12 16:22:34 -04005#include "kerncompat.h"
Chris Masoned2ff2c2007-03-01 18:59:40 -05006
Chris Masone089f052007-03-16 16:20:31 -04007struct btrfs_trans_handle;
8
Chris Mason3768f362007-03-13 16:47:54 -04009#define BTRFS_MAGIC "_BtRfS_M"
Chris Masoneb60cea2007-02-02 09:18:22 -050010
Chris Mason3768f362007-03-13 16:47:54 -040011#define BTRFS_ROOT_TREE_OBJECTID 1
12#define BTRFS_EXTENT_TREE_OBJECTID 2
Chris Mason9f5fae22007-03-20 14:38:32 -040013#define BTRFS_INODE_MAP_OBJECTID 3
14#define BTRFS_FS_TREE_OBJECTID 4
Chris Mason3768f362007-03-13 16:47:54 -040015
Chris Masonfec577f2007-02-26 10:40:21 -050016/*
17 * the key defines the order in the tree, and so it also defines (optimal)
18 * block layout. objectid corresonds to the inode number. The flags
19 * tells us things about the object, and is a kind of stream selector.
20 * so for a given inode, keys with flags of 1 might refer to the inode
21 * data, flags of 2 may point to file data in the btree and flags == 3
22 * may point to extents.
23 *
24 * offset is the starting byte offset for this key in the stream.
Chris Masone2fa7222007-03-12 16:22:34 -040025 *
26 * btrfs_disk_key is in disk byte order. struct btrfs_key is always
27 * in cpu native order. Otherwise they are identical and their sizes
28 * should be the same (ie both packed)
Chris Masonfec577f2007-02-26 10:40:21 -050029 */
Chris Masone2fa7222007-03-12 16:22:34 -040030struct btrfs_disk_key {
31 __le64 objectid;
Chris Masona1516c82007-03-14 14:26:53 -040032 __le32 flags;
Chris Masona8a2ee02007-03-16 08:46:49 -040033 __le64 offset;
Chris Masone2fa7222007-03-12 16:22:34 -040034} __attribute__ ((__packed__));
35
36struct btrfs_key {
Chris Masoneb60cea2007-02-02 09:18:22 -050037 u64 objectid;
Chris Masona1516c82007-03-14 14:26:53 -040038 u32 flags;
Chris Masona8a2ee02007-03-16 08:46:49 -040039 u64 offset;
Chris Masoneb60cea2007-02-02 09:18:22 -050040} __attribute__ ((__packed__));
41
Chris Masonfec577f2007-02-26 10:40:21 -050042/*
43 * every tree block (leaf or node) starts with this header.
44 */
Chris Masonbb492bb2007-03-12 12:29:44 -040045struct btrfs_header {
Chris Mason3768f362007-03-13 16:47:54 -040046 u8 fsid[16]; /* FS specific uuid */
Chris Masonbb492bb2007-03-12 12:29:44 -040047 __le64 blocknr; /* which block this node is supposed to live in */
48 __le64 parentid; /* objectid of the tree root */
49 __le32 csum;
50 __le32 ham;
51 __le16 nritems;
52 __le16 flags;
Chris Masonfec577f2007-02-26 10:40:21 -050053 /* generation flags to be added */
Chris Masoneb60cea2007-02-02 09:18:22 -050054} __attribute__ ((__packed__));
55
Chris Mason234b63a2007-03-13 10:46:10 -040056#define BTRFS_MAX_LEVEL 8
Chris Mason123abc82007-03-14 14:14:43 -040057#define BTRFS_NODEPTRS_PER_BLOCK(r) (((r)->blocksize - \
58 sizeof(struct btrfs_header)) / \
59 (sizeof(struct btrfs_disk_key) + sizeof(u64)))
60#define __BTRFS_LEAF_DATA_SIZE(bs) ((bs) - sizeof(struct btrfs_header))
61#define BTRFS_LEAF_DATA_SIZE(r) (__BTRFS_LEAF_DATA_SIZE(r->blocksize))
Chris Masoneb60cea2007-02-02 09:18:22 -050062
Chris Mason234b63a2007-03-13 10:46:10 -040063struct btrfs_buffer;
Chris Masonfec577f2007-02-26 10:40:21 -050064/*
Chris Masonfec577f2007-02-26 10:40:21 -050065 * the super block basically lists the main trees of the FS
66 * it currently lacks any block count etc etc
67 */
Chris Mason234b63a2007-03-13 10:46:10 -040068struct btrfs_super_block {
Chris Mason3768f362007-03-13 16:47:54 -040069 u8 fsid[16]; /* FS specific uuid */
70 __le64 blocknr; /* this block number */
71 __le32 csum;
72 __le64 magic;
Chris Mason123abc82007-03-14 14:14:43 -040073 __le32 blocksize;
Chris Mason3768f362007-03-13 16:47:54 -040074 __le64 generation;
75 __le64 root;
76 __le64 total_blocks;
77 __le64 blocks_used;
Chris Masoncfaa7292007-02-21 17:04:57 -050078} __attribute__ ((__packed__));
79
Chris Masonfec577f2007-02-26 10:40:21 -050080/*
Chris Mason62e27492007-03-15 12:56:47 -040081 * A leaf is full of items. offset and size tell us where to find
Chris Masonfec577f2007-02-26 10:40:21 -050082 * the item in the leaf (relative to the start of the data area)
83 */
Chris Mason0783fcf2007-03-12 20:12:07 -040084struct btrfs_item {
Chris Masone2fa7222007-03-12 16:22:34 -040085 struct btrfs_disk_key key;
Chris Mason123abc82007-03-14 14:14:43 -040086 __le32 offset;
Chris Mason0783fcf2007-03-12 20:12:07 -040087 __le16 size;
Chris Masoneb60cea2007-02-02 09:18:22 -050088} __attribute__ ((__packed__));
89
Chris Masonfec577f2007-02-26 10:40:21 -050090/*
91 * leaves have an item area and a data area:
92 * [item0, item1....itemN] [free space] [dataN...data1, data0]
93 *
94 * The data is separate from the items to get the keys closer together
95 * during searches.
96 */
Chris Mason234b63a2007-03-13 10:46:10 -040097struct btrfs_leaf {
Chris Masonbb492bb2007-03-12 12:29:44 -040098 struct btrfs_header header;
Chris Mason123abc82007-03-14 14:14:43 -040099 struct btrfs_item items[];
Chris Masoneb60cea2007-02-02 09:18:22 -0500100} __attribute__ ((__packed__));
101
Chris Masonfec577f2007-02-26 10:40:21 -0500102/*
103 * all non-leaf blocks are nodes, they hold only keys and pointers to
104 * other blocks
105 */
Chris Mason123abc82007-03-14 14:14:43 -0400106struct btrfs_key_ptr {
107 struct btrfs_disk_key key;
108 __le64 blockptr;
109} __attribute__ ((__packed__));
110
Chris Mason234b63a2007-03-13 10:46:10 -0400111struct btrfs_node {
Chris Masonbb492bb2007-03-12 12:29:44 -0400112 struct btrfs_header header;
Chris Mason123abc82007-03-14 14:14:43 -0400113 struct btrfs_key_ptr ptrs[];
Chris Masoneb60cea2007-02-02 09:18:22 -0500114} __attribute__ ((__packed__));
115
Chris Masonfec577f2007-02-26 10:40:21 -0500116/*
Chris Mason234b63a2007-03-13 10:46:10 -0400117 * btrfs_paths remember the path taken from the root down to the leaf.
118 * level 0 is always the leaf, and nodes[1...BTRFS_MAX_LEVEL] will point
Chris Masonfec577f2007-02-26 10:40:21 -0500119 * to any other levels that are present.
120 *
121 * The slots array records the index of the item or block pointer
122 * used while walking the tree.
123 */
Chris Mason234b63a2007-03-13 10:46:10 -0400124struct btrfs_path {
125 struct btrfs_buffer *nodes[BTRFS_MAX_LEVEL];
126 int slots[BTRFS_MAX_LEVEL];
Chris Masoneb60cea2007-02-02 09:18:22 -0500127};
Chris Mason5de08d72007-02-24 06:24:44 -0500128
Chris Mason62e27492007-03-15 12:56:47 -0400129/*
130 * items in the extent btree are used to record the objectid of the
131 * owner of the block and the number of references
132 */
133struct btrfs_extent_item {
134 __le32 refs;
135 __le64 owner;
136} __attribute__ ((__packed__));
137
Chris Mason1e1d2702007-03-15 19:03:33 -0400138struct btrfs_inode_timespec {
139 __le32 sec;
140 __le32 nsec;
141} __attribute__ ((__packed__));
142
143/*
144 * there is no padding here on purpose. If you want to extent the inode,
145 * make a new item type
146 */
147struct btrfs_inode_item {
148 __le64 generation;
149 __le64 size;
150 __le64 nblocks;
151 __le32 nlink;
152 __le32 uid;
153 __le32 gid;
154 __le32 mode;
155 __le32 rdev;
156 __le16 flags;
157 __le16 compat_flags;
158 struct btrfs_inode_timespec atime;
159 struct btrfs_inode_timespec ctime;
160 struct btrfs_inode_timespec mtime;
161 struct btrfs_inode_timespec otime;
162} __attribute__ ((__packed__));
163
164/* inline data is just a blob of bytes */
165struct btrfs_inline_data_item {
166 u8 data;
167} __attribute__ ((__packed__));
168
Chris Mason62e27492007-03-15 12:56:47 -0400169struct btrfs_dir_item {
170 __le64 objectid;
171 __le16 flags;
Chris Masona8a2ee02007-03-16 08:46:49 -0400172 __le16 name_len;
Chris Mason62e27492007-03-15 12:56:47 -0400173 u8 type;
174} __attribute__ ((__packed__));
175
176struct btrfs_root_item {
177 __le64 blocknr;
178 __le32 flags;
179 __le64 block_limit;
180 __le64 blocks_used;
181 __le32 refs;
Chris Mason9f5fae22007-03-20 14:38:32 -0400182} __attribute__ ((__packed__));
183
184struct btrfs_file_extent_item {
185 /*
186 * disk space consumed by the extent, checksum blocks are included
187 * in these numbers
188 */
189 __le64 disk_blocknr;
190 __le64 disk_num_blocks;
191 /*
192 * the logical offset in file bytes (no csums)
193 * this extent record is for. This allows a file extent to point
194 * into the middle of an existing extent on disk, sharing it
195 * between two snapshots (useful if some bytes in the middle of the
196 * extent have changed
197 */
198 __le64 offset;
199 /*
200 * the logical number of file blocks (no csums included)
201 */
202 __le64 num_blocks;
203} __attribute__ ((__packed__));
204
205struct btrfs_inode_map_item {
206 struct btrfs_disk_key key;
207} __attribute__ ((__packed__));
208
209struct btrfs_fs_info {
210 struct btrfs_root *fs_root;
211 struct btrfs_root *extent_root;
212 struct btrfs_root *tree_root;
213 struct btrfs_root *inode_root;
214 struct btrfs_key current_insert;
215 struct btrfs_key last_insert;
216 struct radix_tree_root cache_radix;
217 struct radix_tree_root pinned_radix;
218 struct list_head trans;
219 struct list_head cache;
220 u64 last_inode_alloc;
221 u64 last_inode_alloc_dirid;
Chris Mason293ffd52007-03-20 15:57:25 -0400222 u64 generation;
Chris Mason9f5fae22007-03-20 14:38:32 -0400223 int cache_size;
224 int fp;
225 struct btrfs_trans_handle *running_transaction;
Chris Mason62e27492007-03-15 12:56:47 -0400226};
227
228/*
229 * in ram representation of the tree. extent_root is used for all allocations
230 * and for the extent tree extent_root root. current_insert is used
231 * only for the extent tree.
232 */
233struct btrfs_root {
234 struct btrfs_buffer *node;
235 struct btrfs_buffer *commit_root;
Chris Mason62e27492007-03-15 12:56:47 -0400236 struct btrfs_root_item root_item;
237 struct btrfs_key root_key;
Chris Mason9f5fae22007-03-20 14:38:32 -0400238 struct btrfs_fs_info *fs_info;
Chris Mason62e27492007-03-15 12:56:47 -0400239 u32 blocksize;
Chris Mason9f5fae22007-03-20 14:38:32 -0400240 int ref_cows;
241 u32 type;
Chris Mason62e27492007-03-15 12:56:47 -0400242};
243
Chris Mason62e27492007-03-15 12:56:47 -0400244/* the lower bits in the key flags defines the item type */
245#define BTRFS_KEY_TYPE_MAX 256
246#define BTRFS_KEY_TYPE_MASK (BTRFS_KEY_TYPE_MAX - 1)
Chris Mason1e1d2702007-03-15 19:03:33 -0400247
248/*
249 * inode items have the data typically returned from stat and store other
250 * info about object characteristics. There is one for every file and dir in
251 * the FS
252 */
Chris Mason62e27492007-03-15 12:56:47 -0400253#define BTRFS_INODE_ITEM_KEY 1
Chris Mason1e1d2702007-03-15 19:03:33 -0400254
255/*
256 * dir items are the name -> inode pointers in a directory. There is one
257 * for every name in a directory.
258 */
Chris Mason62e27492007-03-15 12:56:47 -0400259#define BTRFS_DIR_ITEM_KEY 2
Chris Mason1e1d2702007-03-15 19:03:33 -0400260/*
261 * inline data is file data that fits in the btree.
262 */
263#define BTRFS_INLINE_DATA_KEY 3
264/*
265 * extent data is for data that can't fit in the btree. It points to
266 * a (hopefully) huge chunk of disk
267 */
268#define BTRFS_EXTENT_DATA_KEY 4
269/*
270 * root items point to tree roots. There are typically in the root
271 * tree used by the super block to find all the other trees
272 */
273#define BTRFS_ROOT_ITEM_KEY 5
274/*
275 * extent items are in the extent map tree. These record which blocks
276 * are used, and how many references there are to each block
277 */
278#define BTRFS_EXTENT_ITEM_KEY 6
Chris Mason9f5fae22007-03-20 14:38:32 -0400279
280/*
281 * the inode map records which inode numbers are in use and where
282 * they actually live on disk
283 */
284#define BTRFS_INODE_MAP_ITEM_KEY 7
Chris Mason1e1d2702007-03-15 19:03:33 -0400285/*
286 * string items are for debugging. They just store a short string of
287 * data in the FS
288 */
Chris Mason9f5fae22007-03-20 14:38:32 -0400289#define BTRFS_STRING_ITEM_KEY 8
Chris Mason1e1d2702007-03-15 19:03:33 -0400290
291static inline u64 btrfs_inode_generation(struct btrfs_inode_item *i)
292{
293 return le64_to_cpu(i->generation);
294}
295
296static inline void btrfs_set_inode_generation(struct btrfs_inode_item *i,
297 u64 val)
298{
299 i->generation = cpu_to_le64(val);
300}
301
302static inline u64 btrfs_inode_size(struct btrfs_inode_item *i)
303{
304 return le64_to_cpu(i->size);
305}
306
307static inline void btrfs_set_inode_size(struct btrfs_inode_item *i, u64 val)
308{
309 i->size = cpu_to_le64(val);
310}
311
312static inline u64 btrfs_inode_nblocks(struct btrfs_inode_item *i)
313{
314 return le64_to_cpu(i->nblocks);
315}
316
317static inline void btrfs_set_inode_nblocks(struct btrfs_inode_item *i, u64 val)
318{
319 i->nblocks = cpu_to_le64(val);
320}
321
322static inline u32 btrfs_inode_nlink(struct btrfs_inode_item *i)
323{
324 return le32_to_cpu(i->nlink);
325}
326
327static inline void btrfs_set_inode_nlink(struct btrfs_inode_item *i, u32 val)
328{
329 i->nlink = cpu_to_le32(val);
330}
331
332static inline u32 btrfs_inode_uid(struct btrfs_inode_item *i)
333{
334 return le32_to_cpu(i->uid);
335}
336
337static inline void btrfs_set_inode_uid(struct btrfs_inode_item *i, u32 val)
338{
339 i->uid = cpu_to_le32(val);
340}
341
342static inline u32 btrfs_inode_gid(struct btrfs_inode_item *i)
343{
344 return le32_to_cpu(i->gid);
345}
346
347static inline void btrfs_set_inode_gid(struct btrfs_inode_item *i, u32 val)
348{
349 i->gid = cpu_to_le32(val);
350}
351
352static inline u32 btrfs_inode_mode(struct btrfs_inode_item *i)
353{
354 return le32_to_cpu(i->mode);
355}
356
357static inline void btrfs_set_inode_mode(struct btrfs_inode_item *i, u32 val)
358{
359 i->mode = cpu_to_le32(val);
360}
361
362static inline u32 btrfs_inode_rdev(struct btrfs_inode_item *i)
363{
364 return le32_to_cpu(i->rdev);
365}
366
367static inline void btrfs_set_inode_rdev(struct btrfs_inode_item *i, u32 val)
368{
369 i->rdev = cpu_to_le32(val);
370}
371
372static inline u16 btrfs_inode_flags(struct btrfs_inode_item *i)
373{
374 return le16_to_cpu(i->flags);
375}
376
377static inline void btrfs_set_inode_flags(struct btrfs_inode_item *i, u16 val)
378{
379 i->flags = cpu_to_le16(val);
380}
381
382static inline u16 btrfs_inode_compat_flags(struct btrfs_inode_item *i)
383{
384 return le16_to_cpu(i->compat_flags);
385}
386
387static inline void btrfs_set_inode_compat_flags(struct btrfs_inode_item *i,
388 u16 val)
389{
390 i->compat_flags = cpu_to_le16(val);
391}
392
Chris Mason62e27492007-03-15 12:56:47 -0400393
Chris Mason234b63a2007-03-13 10:46:10 -0400394static inline u64 btrfs_extent_owner(struct btrfs_extent_item *ei)
Chris Masoncf27e1e2007-03-13 09:49:06 -0400395{
396 return le64_to_cpu(ei->owner);
397}
398
Chris Mason234b63a2007-03-13 10:46:10 -0400399static inline void btrfs_set_extent_owner(struct btrfs_extent_item *ei, u64 val)
Chris Masoncf27e1e2007-03-13 09:49:06 -0400400{
401 ei->owner = cpu_to_le64(val);
402}
403
Chris Mason234b63a2007-03-13 10:46:10 -0400404static inline u32 btrfs_extent_refs(struct btrfs_extent_item *ei)
Chris Masoncf27e1e2007-03-13 09:49:06 -0400405{
406 return le32_to_cpu(ei->refs);
407}
408
Chris Mason234b63a2007-03-13 10:46:10 -0400409static inline void btrfs_set_extent_refs(struct btrfs_extent_item *ei, u32 val)
Chris Masoncf27e1e2007-03-13 09:49:06 -0400410{
411 ei->refs = cpu_to_le32(val);
412}
413
Chris Mason234b63a2007-03-13 10:46:10 -0400414static inline u64 btrfs_node_blockptr(struct btrfs_node *n, int nr)
Chris Mason1d4f8a02007-03-13 09:28:32 -0400415{
Chris Mason123abc82007-03-14 14:14:43 -0400416 return le64_to_cpu(n->ptrs[nr].blockptr);
Chris Mason1d4f8a02007-03-13 09:28:32 -0400417}
418
Chris Mason234b63a2007-03-13 10:46:10 -0400419static inline void btrfs_set_node_blockptr(struct btrfs_node *n, int nr,
420 u64 val)
Chris Mason1d4f8a02007-03-13 09:28:32 -0400421{
Chris Mason123abc82007-03-14 14:14:43 -0400422 n->ptrs[nr].blockptr = cpu_to_le64(val);
Chris Mason1d4f8a02007-03-13 09:28:32 -0400423}
424
Chris Mason123abc82007-03-14 14:14:43 -0400425static inline u32 btrfs_item_offset(struct btrfs_item *item)
Chris Mason0783fcf2007-03-12 20:12:07 -0400426{
Chris Mason123abc82007-03-14 14:14:43 -0400427 return le32_to_cpu(item->offset);
Chris Mason0783fcf2007-03-12 20:12:07 -0400428}
429
Chris Mason123abc82007-03-14 14:14:43 -0400430static inline void btrfs_set_item_offset(struct btrfs_item *item, u32 val)
Chris Mason0783fcf2007-03-12 20:12:07 -0400431{
Chris Mason123abc82007-03-14 14:14:43 -0400432 item->offset = cpu_to_le32(val);
Chris Mason0783fcf2007-03-12 20:12:07 -0400433}
434
Chris Mason123abc82007-03-14 14:14:43 -0400435static inline u32 btrfs_item_end(struct btrfs_item *item)
Chris Mason0783fcf2007-03-12 20:12:07 -0400436{
Chris Mason123abc82007-03-14 14:14:43 -0400437 return le32_to_cpu(item->offset) + le16_to_cpu(item->size);
Chris Mason0783fcf2007-03-12 20:12:07 -0400438}
439
440static inline u16 btrfs_item_size(struct btrfs_item *item)
441{
442 return le16_to_cpu(item->size);
443}
444
445static inline void btrfs_set_item_size(struct btrfs_item *item, u16 val)
446{
447 item->size = cpu_to_le16(val);
448}
449
Chris Mason1d4f6402007-03-15 15:18:43 -0400450static inline u64 btrfs_dir_objectid(struct btrfs_dir_item *d)
451{
452 return le64_to_cpu(d->objectid);
453}
454
455static inline void btrfs_set_dir_objectid(struct btrfs_dir_item *d, u64 val)
456{
457 d->objectid = cpu_to_le64(val);
458}
459
460static inline u16 btrfs_dir_flags(struct btrfs_dir_item *d)
461{
462 return le16_to_cpu(d->flags);
463}
464
465static inline void btrfs_set_dir_flags(struct btrfs_dir_item *d, u16 val)
466{
467 d->flags = cpu_to_le16(val);
468}
469
470static inline u8 btrfs_dir_type(struct btrfs_dir_item *d)
471{
472 return d->type;
473}
474
475static inline void btrfs_set_dir_type(struct btrfs_dir_item *d, u8 val)
476{
477 d->type = val;
478}
479
Chris Masona8a2ee02007-03-16 08:46:49 -0400480static inline u16 btrfs_dir_name_len(struct btrfs_dir_item *d)
Chris Mason1d4f6402007-03-15 15:18:43 -0400481{
Chris Masona8a2ee02007-03-16 08:46:49 -0400482 return le16_to_cpu(d->name_len);
483}
484
485static inline void btrfs_set_dir_name_len(struct btrfs_dir_item *d, u16 val)
486{
487 d->name_len = cpu_to_le16(val);
Chris Mason1d4f6402007-03-15 15:18:43 -0400488}
489
Chris Masone2fa7222007-03-12 16:22:34 -0400490static inline void btrfs_disk_key_to_cpu(struct btrfs_key *cpu,
491 struct btrfs_disk_key *disk)
492{
493 cpu->offset = le64_to_cpu(disk->offset);
494 cpu->flags = le32_to_cpu(disk->flags);
495 cpu->objectid = le64_to_cpu(disk->objectid);
496}
497
498static inline void btrfs_cpu_key_to_disk(struct btrfs_disk_key *disk,
499 struct btrfs_key *cpu)
500{
501 disk->offset = cpu_to_le64(cpu->offset);
502 disk->flags = cpu_to_le32(cpu->flags);
503 disk->objectid = cpu_to_le64(cpu->objectid);
504}
505
Chris Mason62e27492007-03-15 12:56:47 -0400506static inline u64 btrfs_disk_key_objectid(struct btrfs_disk_key *disk)
Chris Masone2fa7222007-03-12 16:22:34 -0400507{
508 return le64_to_cpu(disk->objectid);
509}
510
Chris Mason62e27492007-03-15 12:56:47 -0400511static inline void btrfs_set_disk_key_objectid(struct btrfs_disk_key *disk,
512 u64 val)
Chris Masone2fa7222007-03-12 16:22:34 -0400513{
514 disk->objectid = cpu_to_le64(val);
515}
516
Chris Mason62e27492007-03-15 12:56:47 -0400517static inline u64 btrfs_disk_key_offset(struct btrfs_disk_key *disk)
Chris Masone2fa7222007-03-12 16:22:34 -0400518{
519 return le64_to_cpu(disk->offset);
520}
521
Chris Mason62e27492007-03-15 12:56:47 -0400522static inline void btrfs_set_disk_key_offset(struct btrfs_disk_key *disk,
523 u64 val)
Chris Masone2fa7222007-03-12 16:22:34 -0400524{
525 disk->offset = cpu_to_le64(val);
526}
527
Chris Mason62e27492007-03-15 12:56:47 -0400528static inline u32 btrfs_disk_key_flags(struct btrfs_disk_key *disk)
Chris Masone2fa7222007-03-12 16:22:34 -0400529{
530 return le32_to_cpu(disk->flags);
531}
532
Chris Mason62e27492007-03-15 12:56:47 -0400533static inline void btrfs_set_disk_key_flags(struct btrfs_disk_key *disk,
534 u32 val)
Chris Masone2fa7222007-03-12 16:22:34 -0400535{
536 disk->flags = cpu_to_le32(val);
537}
538
Chris Mason62e27492007-03-15 12:56:47 -0400539static inline u32 btrfs_key_type(struct btrfs_key *key)
540{
541 return key->flags & BTRFS_KEY_TYPE_MASK;
542}
543
544static inline u32 btrfs_disk_key_type(struct btrfs_disk_key *key)
545{
546 return le32_to_cpu(key->flags) & BTRFS_KEY_TYPE_MASK;
547}
548
549static inline void btrfs_set_key_type(struct btrfs_key *key, u32 type)
550{
551 BUG_ON(type >= BTRFS_KEY_TYPE_MAX);
552 key->flags = (key->flags & ~((u64)BTRFS_KEY_TYPE_MASK)) | type;
553}
554
555static inline void btrfs_set_disk_key_type(struct btrfs_disk_key *key, u32 type)
556{
557 u32 flags = btrfs_disk_key_flags(key);
558 BUG_ON(type >= BTRFS_KEY_TYPE_MAX);
559 flags = (flags & ~((u64)BTRFS_KEY_TYPE_MASK)) | type;
560 btrfs_set_disk_key_flags(key, flags);
561}
562
Chris Masonbb492bb2007-03-12 12:29:44 -0400563static inline u64 btrfs_header_blocknr(struct btrfs_header *h)
Chris Mason7518a232007-03-12 12:01:18 -0400564{
Chris Masonbb492bb2007-03-12 12:29:44 -0400565 return le64_to_cpu(h->blocknr);
Chris Mason7518a232007-03-12 12:01:18 -0400566}
567
Chris Masonbb492bb2007-03-12 12:29:44 -0400568static inline void btrfs_set_header_blocknr(struct btrfs_header *h, u64 blocknr)
Chris Mason7518a232007-03-12 12:01:18 -0400569{
Chris Masonbb492bb2007-03-12 12:29:44 -0400570 h->blocknr = cpu_to_le64(blocknr);
Chris Mason7518a232007-03-12 12:01:18 -0400571}
572
Chris Masonbb492bb2007-03-12 12:29:44 -0400573static inline u64 btrfs_header_parentid(struct btrfs_header *h)
Chris Mason7518a232007-03-12 12:01:18 -0400574{
Chris Masonbb492bb2007-03-12 12:29:44 -0400575 return le64_to_cpu(h->parentid);
Chris Mason7518a232007-03-12 12:01:18 -0400576}
577
Chris Masonbb492bb2007-03-12 12:29:44 -0400578static inline void btrfs_set_header_parentid(struct btrfs_header *h,
579 u64 parentid)
Chris Mason7518a232007-03-12 12:01:18 -0400580{
Chris Masonbb492bb2007-03-12 12:29:44 -0400581 h->parentid = cpu_to_le64(parentid);
Chris Mason7518a232007-03-12 12:01:18 -0400582}
583
Chris Masonbb492bb2007-03-12 12:29:44 -0400584static inline u16 btrfs_header_nritems(struct btrfs_header *h)
Chris Mason7518a232007-03-12 12:01:18 -0400585{
Chris Masonbb492bb2007-03-12 12:29:44 -0400586 return le16_to_cpu(h->nritems);
Chris Mason7518a232007-03-12 12:01:18 -0400587}
588
Chris Masonbb492bb2007-03-12 12:29:44 -0400589static inline void btrfs_set_header_nritems(struct btrfs_header *h, u16 val)
Chris Mason7518a232007-03-12 12:01:18 -0400590{
Chris Masonbb492bb2007-03-12 12:29:44 -0400591 h->nritems = cpu_to_le16(val);
Chris Mason7518a232007-03-12 12:01:18 -0400592}
593
Chris Masonbb492bb2007-03-12 12:29:44 -0400594static inline u16 btrfs_header_flags(struct btrfs_header *h)
Chris Mason7518a232007-03-12 12:01:18 -0400595{
Chris Masonbb492bb2007-03-12 12:29:44 -0400596 return le16_to_cpu(h->flags);
Chris Mason7518a232007-03-12 12:01:18 -0400597}
598
Chris Masonbb492bb2007-03-12 12:29:44 -0400599static inline void btrfs_set_header_flags(struct btrfs_header *h, u16 val)
Chris Mason7518a232007-03-12 12:01:18 -0400600{
Chris Masonbb492bb2007-03-12 12:29:44 -0400601 h->flags = cpu_to_le16(val);
Chris Mason7518a232007-03-12 12:01:18 -0400602}
603
Chris Masonbb492bb2007-03-12 12:29:44 -0400604static inline int btrfs_header_level(struct btrfs_header *h)
Chris Mason7518a232007-03-12 12:01:18 -0400605{
Chris Mason234b63a2007-03-13 10:46:10 -0400606 return btrfs_header_flags(h) & (BTRFS_MAX_LEVEL - 1);
Chris Mason7518a232007-03-12 12:01:18 -0400607}
608
Chris Masonbb492bb2007-03-12 12:29:44 -0400609static inline void btrfs_set_header_level(struct btrfs_header *h, int level)
Chris Mason7518a232007-03-12 12:01:18 -0400610{
Chris Masonbb492bb2007-03-12 12:29:44 -0400611 u16 flags;
Chris Mason234b63a2007-03-13 10:46:10 -0400612 BUG_ON(level > BTRFS_MAX_LEVEL);
613 flags = btrfs_header_flags(h) & ~(BTRFS_MAX_LEVEL - 1);
Chris Mason7518a232007-03-12 12:01:18 -0400614 btrfs_set_header_flags(h, flags | level);
615}
616
Chris Mason234b63a2007-03-13 10:46:10 -0400617static inline int btrfs_is_leaf(struct btrfs_node *n)
Chris Mason7518a232007-03-12 12:01:18 -0400618{
619 return (btrfs_header_level(&n->header) == 0);
620}
621
Chris Mason3768f362007-03-13 16:47:54 -0400622static inline u64 btrfs_root_blocknr(struct btrfs_root_item *item)
623{
624 return le64_to_cpu(item->blocknr);
625}
626
627static inline void btrfs_set_root_blocknr(struct btrfs_root_item *item, u64 val)
628{
629 item->blocknr = cpu_to_le64(val);
630}
631
632static inline u32 btrfs_root_refs(struct btrfs_root_item *item)
633{
634 return le32_to_cpu(item->refs);
635}
636
637static inline void btrfs_set_root_refs(struct btrfs_root_item *item, u32 val)
638{
639 item->refs = cpu_to_le32(val);
640}
641
642static inline u64 btrfs_super_blocknr(struct btrfs_super_block *s)
643{
644 return le64_to_cpu(s->blocknr);
645}
646
647static inline void btrfs_set_super_blocknr(struct btrfs_super_block *s, u64 val)
648{
649 s->blocknr = cpu_to_le64(val);
650}
651
652static inline u64 btrfs_super_root(struct btrfs_super_block *s)
653{
654 return le64_to_cpu(s->root);
655}
656
657static inline void btrfs_set_super_root(struct btrfs_super_block *s, u64 val)
658{
659 s->root = cpu_to_le64(val);
660}
661
662static inline u64 btrfs_super_total_blocks(struct btrfs_super_block *s)
663{
664 return le64_to_cpu(s->total_blocks);
665}
666
667static inline void btrfs_set_super_total_blocks(struct btrfs_super_block *s,
668 u64 val)
669{
670 s->total_blocks = cpu_to_le64(val);
671}
672
673static inline u64 btrfs_super_blocks_used(struct btrfs_super_block *s)
674{
675 return le64_to_cpu(s->blocks_used);
676}
677
678static inline void btrfs_set_super_blocks_used(struct btrfs_super_block *s,
679 u64 val)
680{
681 s->blocks_used = cpu_to_le64(val);
682}
683
Chris Mason123abc82007-03-14 14:14:43 -0400684static inline u32 btrfs_super_blocksize(struct btrfs_super_block *s)
Chris Mason3768f362007-03-13 16:47:54 -0400685{
Chris Mason123abc82007-03-14 14:14:43 -0400686 return le32_to_cpu(s->blocksize);
Chris Mason3768f362007-03-13 16:47:54 -0400687}
688
689static inline void btrfs_set_super_blocksize(struct btrfs_super_block *s,
Chris Mason123abc82007-03-14 14:14:43 -0400690 u32 val)
Chris Mason3768f362007-03-13 16:47:54 -0400691{
Chris Mason123abc82007-03-14 14:14:43 -0400692 s->blocksize = cpu_to_le32(val);
693}
694
695static inline u8 *btrfs_leaf_data(struct btrfs_leaf *l)
696{
697 return (u8 *)l->items;
Chris Mason3768f362007-03-13 16:47:54 -0400698}
Chris Mason9f5fae22007-03-20 14:38:32 -0400699
700static inline u64 btrfs_file_extent_disk_blocknr(struct btrfs_file_extent_item
701 *e)
702{
703 return le64_to_cpu(e->disk_blocknr);
704}
705
706static inline void btrfs_set_file_extent_disk_blocknr(struct
707 btrfs_file_extent_item
708 *e, u64 val)
709{
710 e->disk_blocknr = cpu_to_le64(val);
711}
712
713static inline u64 btrfs_file_extent_disk_num_blocks(struct
714 btrfs_file_extent_item *e)
715{
716 return le64_to_cpu(e->disk_num_blocks);
717}
718
719static inline void btrfs_set_file_extent_disk_num_blocks(struct
720 btrfs_file_extent_item
721 *e, u64 val)
722{
723 e->disk_num_blocks = cpu_to_le64(val);
724}
725
726static inline u64 btrfs_file_extent_offset(struct btrfs_file_extent_item *e)
727{
728 return le64_to_cpu(e->offset);
729}
730
731static inline void btrfs_set_file_extent_offset(struct btrfs_file_extent_item
732 *e, u64 val)
733{
734 e->offset = cpu_to_le64(val);
735}
736
737static inline u64 btrfs_file_extent_num_blocks(struct btrfs_file_extent_item
738 *e)
739{
740 return le64_to_cpu(e->num_blocks);
741}
742
743static inline void btrfs_set_file_extent_num_blocks(struct
744 btrfs_file_extent_item *e,
745 u64 val)
746{
747 e->num_blocks = cpu_to_le64(val);
748}
749
Chris Mason4beb1b82007-03-14 10:31:29 -0400750/* helper function to cast into the data area of the leaf. */
751#define btrfs_item_ptr(leaf, slot, type) \
Chris Mason123abc82007-03-14 14:14:43 -0400752 ((type *)(btrfs_leaf_data(leaf) + \
753 btrfs_item_offset((leaf)->items + (slot))))
Chris Mason4beb1b82007-03-14 10:31:29 -0400754
Chris Masone089f052007-03-16 16:20:31 -0400755struct btrfs_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
756 struct btrfs_root *root);
757int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
758 struct btrfs_buffer *buf);
759int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
760 *root, u64 blocknr, u64 num_blocks, int pin);
761int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
762 *root, struct btrfs_key *key, struct btrfs_path *p, int
763 ins_len, int cow);
Chris Mason234b63a2007-03-13 10:46:10 -0400764void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p);
765void btrfs_init_path(struct btrfs_path *p);
Chris Masone089f052007-03-16 16:20:31 -0400766int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
767 struct btrfs_path *path);
768int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
769 *root, struct btrfs_key *key, void *data, u32 data_size);
770int btrfs_insert_empty_item(struct btrfs_trans_handle *trans, struct btrfs_root
771 *root, struct btrfs_path *path, struct btrfs_key
772 *cpu_key, u32 data_size);
Chris Mason234b63a2007-03-13 10:46:10 -0400773int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path);
Chris Mason123abc82007-03-14 14:14:43 -0400774int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf);
Chris Masone089f052007-03-16 16:20:31 -0400775int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
776 *root, struct btrfs_buffer *snap);
777int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
778 btrfs_root *root);
779int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
780 struct btrfs_key *key);
781int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
782 *root, struct btrfs_key *key, struct btrfs_root_item
783 *item);
784int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
785 *root, struct btrfs_key *key, struct btrfs_root_item
786 *item);
787int btrfs_find_last_root(struct btrfs_root *root, u64 objectid, struct
788 btrfs_root_item *item, struct btrfs_key *key);
789int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
790 *root, char *name, int name_len, u64 dir, u64
791 objectid, u8 type);
792int btrfs_lookup_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
793 *root, struct btrfs_path *path, u64 dir, char *name,
794 int name_len, int mod);
Chris Mason1d4f6402007-03-15 15:18:43 -0400795int btrfs_match_dir_item_name(struct btrfs_root *root, struct btrfs_path *path,
796 char *name, int name_len);
Chris Mason9f5fae22007-03-20 14:38:32 -0400797int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
798 struct btrfs_root *fs_root,
799 u64 dirid, u64 *objectid);
800int btrfs_insert_inode_map(struct btrfs_trans_handle *trans,
801 struct btrfs_root *root,
802 u64 objectid, struct btrfs_key *location);
803int btrfs_lookup_inode_map(struct btrfs_trans_handle *trans,
804 struct btrfs_root *root, struct btrfs_path *path,
805 u64 objectid, int mod);
Chris Mason293ffd52007-03-20 15:57:25 -0400806int btrfs_insert_inode(struct btrfs_trans_handle *trans, struct btrfs_root
807 *root, u64 objectid, struct btrfs_inode_item
808 *inode_item);
809int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root
810 *root, struct btrfs_path *path, u64 objectid, int mod);
Chris Masoneb60cea2007-02-02 09:18:22 -0500811#endif