blob: 6ff87f44c5d8d2ab08ec811b110e5f3fb8830473 [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 Masone20d96d2007-03-22 12:13:20 -04004#include <linux/fs.h>
Chris Mason8ef97622007-03-26 10:15:30 -04005#include "bit-radix.h"
Chris Masone20d96d2007-03-22 12:13:20 -04006
Chris Masone089f052007-03-16 16:20:31 -04007struct btrfs_trans_handle;
Chris Mason79154b12007-03-22 15:59:16 -04008struct btrfs_transaction;
Chris Masone089f052007-03-16 16:20:31 -04009
Chris Mason3768f362007-03-13 16:47:54 -040010#define BTRFS_MAGIC "_BtRfS_M"
Chris Masoneb60cea2007-02-02 09:18:22 -050011
Chris Mason6407bf62007-03-27 06:33:00 -040012#define BTRFS_ROOT_TREE_OBJECTID 1ULL
13#define BTRFS_EXTENT_TREE_OBJECTID 2ULL
14#define BTRFS_INODE_MAP_OBJECTID 3ULL
15#define BTRFS_FS_TREE_OBJECTID 4ULL
16#define BTRFS_FIRST_FREE_OBJECTID 5ULL
Chris Mason3768f362007-03-13 16:47:54 -040017
Chris Masonfec577f2007-02-26 10:40:21 -050018/*
Chris Masone20d96d2007-03-22 12:13:20 -040019 * we can actually store much bigger names, but lets not confuse the rest
20 * of linux
21 */
22#define BTRFS_NAME_LEN 255
23
24/*
Chris Masonfec577f2007-02-26 10:40:21 -050025 * the key defines the order in the tree, and so it also defines (optimal)
26 * block layout. objectid corresonds to the inode number. The flags
27 * tells us things about the object, and is a kind of stream selector.
28 * so for a given inode, keys with flags of 1 might refer to the inode
29 * data, flags of 2 may point to file data in the btree and flags == 3
30 * may point to extents.
31 *
32 * offset is the starting byte offset for this key in the stream.
Chris Masone2fa7222007-03-12 16:22:34 -040033 *
34 * btrfs_disk_key is in disk byte order. struct btrfs_key is always
35 * in cpu native order. Otherwise they are identical and their sizes
36 * should be the same (ie both packed)
Chris Masonfec577f2007-02-26 10:40:21 -050037 */
Chris Masone2fa7222007-03-12 16:22:34 -040038struct btrfs_disk_key {
39 __le64 objectid;
Chris Masona1516c82007-03-14 14:26:53 -040040 __le32 flags;
Chris Masona8a2ee02007-03-16 08:46:49 -040041 __le64 offset;
Chris Masone2fa7222007-03-12 16:22:34 -040042} __attribute__ ((__packed__));
43
44struct btrfs_key {
Chris Masoneb60cea2007-02-02 09:18:22 -050045 u64 objectid;
Chris Masona1516c82007-03-14 14:26:53 -040046 u32 flags;
Chris Masona8a2ee02007-03-16 08:46:49 -040047 u64 offset;
Chris Masoneb60cea2007-02-02 09:18:22 -050048} __attribute__ ((__packed__));
49
Chris Masonfec577f2007-02-26 10:40:21 -050050/*
51 * every tree block (leaf or node) starts with this header.
52 */
Chris Masonbb492bb2007-03-12 12:29:44 -040053struct btrfs_header {
Chris Mason87cbda52007-03-28 19:44:27 -040054 __le32 csum[8];
Chris Mason3768f362007-03-13 16:47:54 -040055 u8 fsid[16]; /* FS specific uuid */
Chris Masonbb492bb2007-03-12 12:29:44 -040056 __le64 blocknr; /* which block this node is supposed to live in */
Chris Mason7f5c1512007-03-23 15:56:19 -040057 __le64 generation;
Chris Masonbb492bb2007-03-12 12:29:44 -040058 __le64 parentid; /* objectid of the tree root */
Chris Masonbb492bb2007-03-12 12:29:44 -040059 __le32 ham;
60 __le16 nritems;
61 __le16 flags;
Chris Mason9a6f11e2007-03-27 09:06:38 -040062 u8 level;
Chris Masoneb60cea2007-02-02 09:18:22 -050063} __attribute__ ((__packed__));
64
Chris Mason234b63a2007-03-13 10:46:10 -040065#define BTRFS_MAX_LEVEL 8
Chris Mason123abc82007-03-14 14:14:43 -040066#define BTRFS_NODEPTRS_PER_BLOCK(r) (((r)->blocksize - \
67 sizeof(struct btrfs_header)) / \
68 (sizeof(struct btrfs_disk_key) + sizeof(u64)))
69#define __BTRFS_LEAF_DATA_SIZE(bs) ((bs) - sizeof(struct btrfs_header))
70#define BTRFS_LEAF_DATA_SIZE(r) (__BTRFS_LEAF_DATA_SIZE(r->blocksize))
Chris Masoneb60cea2007-02-02 09:18:22 -050071
Chris Masone20d96d2007-03-22 12:13:20 -040072struct buffer_head;
Chris Masonfec577f2007-02-26 10:40:21 -050073/*
Chris Masonfec577f2007-02-26 10:40:21 -050074 * the super block basically lists the main trees of the FS
75 * it currently lacks any block count etc etc
76 */
Chris Mason234b63a2007-03-13 10:46:10 -040077struct btrfs_super_block {
Chris Mason87cbda52007-03-28 19:44:27 -040078 __le32 csum[8];
79 /* the first 3 fields must match struct btrfs_header */
Chris Mason3768f362007-03-13 16:47:54 -040080 u8 fsid[16]; /* FS specific uuid */
81 __le64 blocknr; /* this block number */
Chris Mason3768f362007-03-13 16:47:54 -040082 __le64 magic;
Chris Mason123abc82007-03-14 14:14:43 -040083 __le32 blocksize;
Chris Mason3768f362007-03-13 16:47:54 -040084 __le64 generation;
85 __le64 root;
86 __le64 total_blocks;
87 __le64 blocks_used;
Chris Mason2e635a22007-03-21 11:12:56 -040088 __le64 root_dir_objectid;
Chris Masoncfaa7292007-02-21 17:04:57 -050089} __attribute__ ((__packed__));
90
Chris Masonfec577f2007-02-26 10:40:21 -050091/*
Chris Mason62e27492007-03-15 12:56:47 -040092 * A leaf is full of items. offset and size tell us where to find
Chris Masonfec577f2007-02-26 10:40:21 -050093 * the item in the leaf (relative to the start of the data area)
94 */
Chris Mason0783fcf2007-03-12 20:12:07 -040095struct btrfs_item {
Chris Masone2fa7222007-03-12 16:22:34 -040096 struct btrfs_disk_key key;
Chris Mason123abc82007-03-14 14:14:43 -040097 __le32 offset;
Chris Mason0783fcf2007-03-12 20:12:07 -040098 __le16 size;
Chris Masoneb60cea2007-02-02 09:18:22 -050099} __attribute__ ((__packed__));
100
Chris Masonfec577f2007-02-26 10:40:21 -0500101/*
102 * leaves have an item area and a data area:
103 * [item0, item1....itemN] [free space] [dataN...data1, data0]
104 *
105 * The data is separate from the items to get the keys closer together
106 * during searches.
107 */
Chris Mason234b63a2007-03-13 10:46:10 -0400108struct btrfs_leaf {
Chris Masonbb492bb2007-03-12 12:29:44 -0400109 struct btrfs_header header;
Chris Mason123abc82007-03-14 14:14:43 -0400110 struct btrfs_item items[];
Chris Masoneb60cea2007-02-02 09:18:22 -0500111} __attribute__ ((__packed__));
112
Chris Masonfec577f2007-02-26 10:40:21 -0500113/*
114 * all non-leaf blocks are nodes, they hold only keys and pointers to
115 * other blocks
116 */
Chris Mason123abc82007-03-14 14:14:43 -0400117struct btrfs_key_ptr {
118 struct btrfs_disk_key key;
119 __le64 blockptr;
120} __attribute__ ((__packed__));
121
Chris Mason234b63a2007-03-13 10:46:10 -0400122struct btrfs_node {
Chris Masonbb492bb2007-03-12 12:29:44 -0400123 struct btrfs_header header;
Chris Mason123abc82007-03-14 14:14:43 -0400124 struct btrfs_key_ptr ptrs[];
Chris Masoneb60cea2007-02-02 09:18:22 -0500125} __attribute__ ((__packed__));
126
Chris Masonfec577f2007-02-26 10:40:21 -0500127/*
Chris Mason234b63a2007-03-13 10:46:10 -0400128 * btrfs_paths remember the path taken from the root down to the leaf.
129 * level 0 is always the leaf, and nodes[1...BTRFS_MAX_LEVEL] will point
Chris Masonfec577f2007-02-26 10:40:21 -0500130 * to any other levels that are present.
131 *
132 * The slots array records the index of the item or block pointer
133 * used while walking the tree.
134 */
Chris Mason234b63a2007-03-13 10:46:10 -0400135struct btrfs_path {
Chris Masone20d96d2007-03-22 12:13:20 -0400136 struct buffer_head *nodes[BTRFS_MAX_LEVEL];
Chris Mason234b63a2007-03-13 10:46:10 -0400137 int slots[BTRFS_MAX_LEVEL];
Chris Masoneb60cea2007-02-02 09:18:22 -0500138};
Chris Mason5de08d72007-02-24 06:24:44 -0500139
Chris Mason62e27492007-03-15 12:56:47 -0400140/*
141 * items in the extent btree are used to record the objectid of the
142 * owner of the block and the number of references
143 */
144struct btrfs_extent_item {
145 __le32 refs;
146 __le64 owner;
147} __attribute__ ((__packed__));
148
Chris Mason1e1d2702007-03-15 19:03:33 -0400149struct btrfs_inode_timespec {
150 __le32 sec;
151 __le32 nsec;
152} __attribute__ ((__packed__));
153
154/*
155 * there is no padding here on purpose. If you want to extent the inode,
156 * make a new item type
157 */
158struct btrfs_inode_item {
159 __le64 generation;
160 __le64 size;
161 __le64 nblocks;
162 __le32 nlink;
163 __le32 uid;
164 __le32 gid;
165 __le32 mode;
166 __le32 rdev;
167 __le16 flags;
168 __le16 compat_flags;
169 struct btrfs_inode_timespec atime;
170 struct btrfs_inode_timespec ctime;
171 struct btrfs_inode_timespec mtime;
172 struct btrfs_inode_timespec otime;
173} __attribute__ ((__packed__));
174
175/* inline data is just a blob of bytes */
176struct btrfs_inline_data_item {
177 u8 data;
178} __attribute__ ((__packed__));
179
Chris Mason62e27492007-03-15 12:56:47 -0400180struct btrfs_dir_item {
181 __le64 objectid;
182 __le16 flags;
Chris Masona8a2ee02007-03-16 08:46:49 -0400183 __le16 name_len;
Chris Mason62e27492007-03-15 12:56:47 -0400184 u8 type;
185} __attribute__ ((__packed__));
186
187struct btrfs_root_item {
188 __le64 blocknr;
189 __le32 flags;
190 __le64 block_limit;
191 __le64 blocks_used;
192 __le32 refs;
Chris Mason9f5fae22007-03-20 14:38:32 -0400193} __attribute__ ((__packed__));
194
195struct btrfs_file_extent_item {
Chris Mason71951f32007-03-27 09:16:29 -0400196 __le64 generation;
Chris Mason9f5fae22007-03-20 14:38:32 -0400197 /*
198 * disk space consumed by the extent, checksum blocks are included
199 * in these numbers
200 */
201 __le64 disk_blocknr;
202 __le64 disk_num_blocks;
203 /*
Chris Masondee26a92007-03-26 16:00:06 -0400204 * the logical offset in file blocks (no csums)
Chris Mason9f5fae22007-03-20 14:38:32 -0400205 * this extent record is for. This allows a file extent to point
206 * into the middle of an existing extent on disk, sharing it
207 * between two snapshots (useful if some bytes in the middle of the
208 * extent have changed
209 */
210 __le64 offset;
211 /*
212 * the logical number of file blocks (no csums included)
213 */
214 __le64 num_blocks;
215} __attribute__ ((__packed__));
216
217struct btrfs_inode_map_item {
218 struct btrfs_disk_key key;
219} __attribute__ ((__packed__));
220
Chris Mason87cbda52007-03-28 19:44:27 -0400221struct crypto_hash;
Chris Mason9f5fae22007-03-20 14:38:32 -0400222struct btrfs_fs_info {
223 struct btrfs_root *fs_root;
224 struct btrfs_root *extent_root;
225 struct btrfs_root *tree_root;
226 struct btrfs_root *inode_root;
227 struct btrfs_key current_insert;
228 struct btrfs_key last_insert;
Chris Mason8ef97622007-03-26 10:15:30 -0400229 struct radix_tree_root pending_del_radix;
Chris Mason9f5fae22007-03-20 14:38:32 -0400230 struct radix_tree_root pinned_radix;
Chris Mason9f5fae22007-03-20 14:38:32 -0400231 u64 last_inode_alloc;
232 u64 last_inode_alloc_dirid;
Chris Mason293ffd52007-03-20 15:57:25 -0400233 u64 generation;
Chris Mason79154b12007-03-22 15:59:16 -0400234 struct btrfs_transaction *running_transaction;
Chris Mason1261ec42007-03-20 20:35:03 -0400235 struct btrfs_super_block *disk_super;
Chris Masone20d96d2007-03-22 12:13:20 -0400236 struct buffer_head *sb_buffer;
237 struct super_block *sb;
Chris Masond98237b2007-03-28 13:57:48 -0400238 struct inode *btree_inode;
Chris Mason79154b12007-03-22 15:59:16 -0400239 struct mutex trans_mutex;
Chris Masond561c022007-03-23 19:47:49 -0400240 struct mutex fs_mutex;
Chris Mason87cbda52007-03-28 19:44:27 -0400241 struct crypto_hash *hash_tfm;
242 spinlock_t hash_lock;
Chris Mason62e27492007-03-15 12:56:47 -0400243};
244
245/*
246 * in ram representation of the tree. extent_root is used for all allocations
247 * and for the extent tree extent_root root. current_insert is used
248 * only for the extent tree.
249 */
250struct btrfs_root {
Chris Masone20d96d2007-03-22 12:13:20 -0400251 struct buffer_head *node;
252 struct buffer_head *commit_root;
Chris Mason62e27492007-03-15 12:56:47 -0400253 struct btrfs_root_item root_item;
254 struct btrfs_key root_key;
Chris Mason9f5fae22007-03-20 14:38:32 -0400255 struct btrfs_fs_info *fs_info;
Chris Mason62e27492007-03-15 12:56:47 -0400256 u32 blocksize;
Chris Mason9f5fae22007-03-20 14:38:32 -0400257 int ref_cows;
258 u32 type;
Chris Mason62e27492007-03-15 12:56:47 -0400259};
260
Chris Mason62e27492007-03-15 12:56:47 -0400261/* the lower bits in the key flags defines the item type */
262#define BTRFS_KEY_TYPE_MAX 256
263#define BTRFS_KEY_TYPE_MASK (BTRFS_KEY_TYPE_MAX - 1)
Chris Mason1e1d2702007-03-15 19:03:33 -0400264
265/*
266 * inode items have the data typically returned from stat and store other
267 * info about object characteristics. There is one for every file and dir in
268 * the FS
269 */
Chris Mason62e27492007-03-15 12:56:47 -0400270#define BTRFS_INODE_ITEM_KEY 1
Chris Mason1e1d2702007-03-15 19:03:33 -0400271
272/*
273 * dir items are the name -> inode pointers in a directory. There is one
274 * for every name in a directory.
275 */
Chris Mason62e27492007-03-15 12:56:47 -0400276#define BTRFS_DIR_ITEM_KEY 2
Chris Mason1e1d2702007-03-15 19:03:33 -0400277/*
278 * inline data is file data that fits in the btree.
279 */
280#define BTRFS_INLINE_DATA_KEY 3
281/*
282 * extent data is for data that can't fit in the btree. It points to
283 * a (hopefully) huge chunk of disk
284 */
285#define BTRFS_EXTENT_DATA_KEY 4
286/*
287 * root items point to tree roots. There are typically in the root
288 * tree used by the super block to find all the other trees
289 */
290#define BTRFS_ROOT_ITEM_KEY 5
291/*
292 * extent items are in the extent map tree. These record which blocks
293 * are used, and how many references there are to each block
294 */
295#define BTRFS_EXTENT_ITEM_KEY 6
Chris Mason9f5fae22007-03-20 14:38:32 -0400296
297/*
298 * the inode map records which inode numbers are in use and where
299 * they actually live on disk
300 */
301#define BTRFS_INODE_MAP_ITEM_KEY 7
Chris Mason1e1d2702007-03-15 19:03:33 -0400302/*
303 * string items are for debugging. They just store a short string of
304 * data in the FS
305 */
Chris Mason9f5fae22007-03-20 14:38:32 -0400306#define BTRFS_STRING_ITEM_KEY 8
Chris Mason1e1d2702007-03-15 19:03:33 -0400307
308static inline u64 btrfs_inode_generation(struct btrfs_inode_item *i)
309{
310 return le64_to_cpu(i->generation);
311}
312
313static inline void btrfs_set_inode_generation(struct btrfs_inode_item *i,
314 u64 val)
315{
316 i->generation = cpu_to_le64(val);
317}
318
319static inline u64 btrfs_inode_size(struct btrfs_inode_item *i)
320{
321 return le64_to_cpu(i->size);
322}
323
324static inline void btrfs_set_inode_size(struct btrfs_inode_item *i, u64 val)
325{
326 i->size = cpu_to_le64(val);
327}
328
329static inline u64 btrfs_inode_nblocks(struct btrfs_inode_item *i)
330{
331 return le64_to_cpu(i->nblocks);
332}
333
334static inline void btrfs_set_inode_nblocks(struct btrfs_inode_item *i, u64 val)
335{
336 i->nblocks = cpu_to_le64(val);
337}
338
339static inline u32 btrfs_inode_nlink(struct btrfs_inode_item *i)
340{
341 return le32_to_cpu(i->nlink);
342}
343
344static inline void btrfs_set_inode_nlink(struct btrfs_inode_item *i, u32 val)
345{
346 i->nlink = cpu_to_le32(val);
347}
348
349static inline u32 btrfs_inode_uid(struct btrfs_inode_item *i)
350{
351 return le32_to_cpu(i->uid);
352}
353
354static inline void btrfs_set_inode_uid(struct btrfs_inode_item *i, u32 val)
355{
356 i->uid = cpu_to_le32(val);
357}
358
359static inline u32 btrfs_inode_gid(struct btrfs_inode_item *i)
360{
361 return le32_to_cpu(i->gid);
362}
363
364static inline void btrfs_set_inode_gid(struct btrfs_inode_item *i, u32 val)
365{
366 i->gid = cpu_to_le32(val);
367}
368
369static inline u32 btrfs_inode_mode(struct btrfs_inode_item *i)
370{
371 return le32_to_cpu(i->mode);
372}
373
374static inline void btrfs_set_inode_mode(struct btrfs_inode_item *i, u32 val)
375{
376 i->mode = cpu_to_le32(val);
377}
378
379static inline u32 btrfs_inode_rdev(struct btrfs_inode_item *i)
380{
381 return le32_to_cpu(i->rdev);
382}
383
384static inline void btrfs_set_inode_rdev(struct btrfs_inode_item *i, u32 val)
385{
386 i->rdev = cpu_to_le32(val);
387}
388
389static inline u16 btrfs_inode_flags(struct btrfs_inode_item *i)
390{
391 return le16_to_cpu(i->flags);
392}
393
394static inline void btrfs_set_inode_flags(struct btrfs_inode_item *i, u16 val)
395{
396 i->flags = cpu_to_le16(val);
397}
398
399static inline u16 btrfs_inode_compat_flags(struct btrfs_inode_item *i)
400{
401 return le16_to_cpu(i->compat_flags);
402}
403
404static inline void btrfs_set_inode_compat_flags(struct btrfs_inode_item *i,
405 u16 val)
406{
407 i->compat_flags = cpu_to_le16(val);
408}
409
Chris Masone20d96d2007-03-22 12:13:20 -0400410static inline u32 btrfs_timespec_sec(struct btrfs_inode_timespec *ts)
411{
412 return le32_to_cpu(ts->sec);
413}
414
415static inline void btrfs_set_timespec_sec(struct btrfs_inode_timespec *ts,
416 u32 val)
417{
418 ts->sec = cpu_to_le32(val);
419}
420
421static inline u32 btrfs_timespec_nsec(struct btrfs_inode_timespec *ts)
422{
423 return le32_to_cpu(ts->nsec);
424}
425
426static inline void btrfs_set_timespec_nsec(struct btrfs_inode_timespec *ts,
427 u32 val)
428{
429 ts->nsec = cpu_to_le32(val);
430}
431
432
Chris Mason62e27492007-03-15 12:56:47 -0400433
Chris Mason234b63a2007-03-13 10:46:10 -0400434static inline u64 btrfs_extent_owner(struct btrfs_extent_item *ei)
Chris Masoncf27e1e2007-03-13 09:49:06 -0400435{
436 return le64_to_cpu(ei->owner);
437}
438
Chris Mason234b63a2007-03-13 10:46:10 -0400439static inline void btrfs_set_extent_owner(struct btrfs_extent_item *ei, u64 val)
Chris Masoncf27e1e2007-03-13 09:49:06 -0400440{
441 ei->owner = cpu_to_le64(val);
442}
443
Chris Mason234b63a2007-03-13 10:46:10 -0400444static inline u32 btrfs_extent_refs(struct btrfs_extent_item *ei)
Chris Masoncf27e1e2007-03-13 09:49:06 -0400445{
446 return le32_to_cpu(ei->refs);
447}
448
Chris Mason234b63a2007-03-13 10:46:10 -0400449static inline void btrfs_set_extent_refs(struct btrfs_extent_item *ei, u32 val)
Chris Masoncf27e1e2007-03-13 09:49:06 -0400450{
451 ei->refs = cpu_to_le32(val);
452}
453
Chris Mason234b63a2007-03-13 10:46:10 -0400454static inline u64 btrfs_node_blockptr(struct btrfs_node *n, int nr)
Chris Mason1d4f8a02007-03-13 09:28:32 -0400455{
Chris Mason123abc82007-03-14 14:14:43 -0400456 return le64_to_cpu(n->ptrs[nr].blockptr);
Chris Mason1d4f8a02007-03-13 09:28:32 -0400457}
458
Chris Mason234b63a2007-03-13 10:46:10 -0400459static inline void btrfs_set_node_blockptr(struct btrfs_node *n, int nr,
460 u64 val)
Chris Mason1d4f8a02007-03-13 09:28:32 -0400461{
Chris Mason123abc82007-03-14 14:14:43 -0400462 n->ptrs[nr].blockptr = cpu_to_le64(val);
Chris Mason1d4f8a02007-03-13 09:28:32 -0400463}
464
Chris Mason123abc82007-03-14 14:14:43 -0400465static inline u32 btrfs_item_offset(struct btrfs_item *item)
Chris Mason0783fcf2007-03-12 20:12:07 -0400466{
Chris Mason123abc82007-03-14 14:14:43 -0400467 return le32_to_cpu(item->offset);
Chris Mason0783fcf2007-03-12 20:12:07 -0400468}
469
Chris Mason123abc82007-03-14 14:14:43 -0400470static inline void btrfs_set_item_offset(struct btrfs_item *item, u32 val)
Chris Mason0783fcf2007-03-12 20:12:07 -0400471{
Chris Mason123abc82007-03-14 14:14:43 -0400472 item->offset = cpu_to_le32(val);
Chris Mason0783fcf2007-03-12 20:12:07 -0400473}
474
Chris Mason123abc82007-03-14 14:14:43 -0400475static inline u32 btrfs_item_end(struct btrfs_item *item)
Chris Mason0783fcf2007-03-12 20:12:07 -0400476{
Chris Mason123abc82007-03-14 14:14:43 -0400477 return le32_to_cpu(item->offset) + le16_to_cpu(item->size);
Chris Mason0783fcf2007-03-12 20:12:07 -0400478}
479
480static inline u16 btrfs_item_size(struct btrfs_item *item)
481{
482 return le16_to_cpu(item->size);
483}
484
485static inline void btrfs_set_item_size(struct btrfs_item *item, u16 val)
486{
487 item->size = cpu_to_le16(val);
488}
489
Chris Mason1d4f6402007-03-15 15:18:43 -0400490static inline u64 btrfs_dir_objectid(struct btrfs_dir_item *d)
491{
492 return le64_to_cpu(d->objectid);
493}
494
495static inline void btrfs_set_dir_objectid(struct btrfs_dir_item *d, u64 val)
496{
497 d->objectid = cpu_to_le64(val);
498}
499
500static inline u16 btrfs_dir_flags(struct btrfs_dir_item *d)
501{
502 return le16_to_cpu(d->flags);
503}
504
505static inline void btrfs_set_dir_flags(struct btrfs_dir_item *d, u16 val)
506{
507 d->flags = cpu_to_le16(val);
508}
509
510static inline u8 btrfs_dir_type(struct btrfs_dir_item *d)
511{
512 return d->type;
513}
514
515static inline void btrfs_set_dir_type(struct btrfs_dir_item *d, u8 val)
516{
517 d->type = val;
518}
519
Chris Masona8a2ee02007-03-16 08:46:49 -0400520static inline u16 btrfs_dir_name_len(struct btrfs_dir_item *d)
Chris Mason1d4f6402007-03-15 15:18:43 -0400521{
Chris Masona8a2ee02007-03-16 08:46:49 -0400522 return le16_to_cpu(d->name_len);
523}
524
525static inline void btrfs_set_dir_name_len(struct btrfs_dir_item *d, u16 val)
526{
527 d->name_len = cpu_to_le16(val);
Chris Mason1d4f6402007-03-15 15:18:43 -0400528}
529
Chris Masone2fa7222007-03-12 16:22:34 -0400530static inline void btrfs_disk_key_to_cpu(struct btrfs_key *cpu,
531 struct btrfs_disk_key *disk)
532{
533 cpu->offset = le64_to_cpu(disk->offset);
534 cpu->flags = le32_to_cpu(disk->flags);
535 cpu->objectid = le64_to_cpu(disk->objectid);
536}
537
538static inline void btrfs_cpu_key_to_disk(struct btrfs_disk_key *disk,
539 struct btrfs_key *cpu)
540{
541 disk->offset = cpu_to_le64(cpu->offset);
542 disk->flags = cpu_to_le32(cpu->flags);
543 disk->objectid = cpu_to_le64(cpu->objectid);
544}
545
Chris Mason62e27492007-03-15 12:56:47 -0400546static inline u64 btrfs_disk_key_objectid(struct btrfs_disk_key *disk)
Chris Masone2fa7222007-03-12 16:22:34 -0400547{
548 return le64_to_cpu(disk->objectid);
549}
550
Chris Mason62e27492007-03-15 12:56:47 -0400551static inline void btrfs_set_disk_key_objectid(struct btrfs_disk_key *disk,
552 u64 val)
Chris Masone2fa7222007-03-12 16:22:34 -0400553{
554 disk->objectid = cpu_to_le64(val);
555}
556
Chris Mason62e27492007-03-15 12:56:47 -0400557static inline u64 btrfs_disk_key_offset(struct btrfs_disk_key *disk)
Chris Masone2fa7222007-03-12 16:22:34 -0400558{
559 return le64_to_cpu(disk->offset);
560}
561
Chris Mason62e27492007-03-15 12:56:47 -0400562static inline void btrfs_set_disk_key_offset(struct btrfs_disk_key *disk,
563 u64 val)
Chris Masone2fa7222007-03-12 16:22:34 -0400564{
565 disk->offset = cpu_to_le64(val);
566}
567
Chris Mason62e27492007-03-15 12:56:47 -0400568static inline u32 btrfs_disk_key_flags(struct btrfs_disk_key *disk)
Chris Masone2fa7222007-03-12 16:22:34 -0400569{
570 return le32_to_cpu(disk->flags);
571}
572
Chris Mason62e27492007-03-15 12:56:47 -0400573static inline void btrfs_set_disk_key_flags(struct btrfs_disk_key *disk,
574 u32 val)
Chris Masone2fa7222007-03-12 16:22:34 -0400575{
576 disk->flags = cpu_to_le32(val);
577}
578
Chris Mason62e27492007-03-15 12:56:47 -0400579static inline u32 btrfs_key_type(struct btrfs_key *key)
580{
581 return key->flags & BTRFS_KEY_TYPE_MASK;
582}
583
584static inline u32 btrfs_disk_key_type(struct btrfs_disk_key *key)
585{
586 return le32_to_cpu(key->flags) & BTRFS_KEY_TYPE_MASK;
587}
588
589static inline void btrfs_set_key_type(struct btrfs_key *key, u32 type)
590{
591 BUG_ON(type >= BTRFS_KEY_TYPE_MAX);
592 key->flags = (key->flags & ~((u64)BTRFS_KEY_TYPE_MASK)) | type;
593}
594
595static inline void btrfs_set_disk_key_type(struct btrfs_disk_key *key, u32 type)
596{
597 u32 flags = btrfs_disk_key_flags(key);
598 BUG_ON(type >= BTRFS_KEY_TYPE_MAX);
599 flags = (flags & ~((u64)BTRFS_KEY_TYPE_MASK)) | type;
600 btrfs_set_disk_key_flags(key, flags);
601}
602
Chris Masonbb492bb2007-03-12 12:29:44 -0400603static inline u64 btrfs_header_blocknr(struct btrfs_header *h)
Chris Mason7518a232007-03-12 12:01:18 -0400604{
Chris Masonbb492bb2007-03-12 12:29:44 -0400605 return le64_to_cpu(h->blocknr);
Chris Mason7518a232007-03-12 12:01:18 -0400606}
607
Chris Masonbb492bb2007-03-12 12:29:44 -0400608static inline void btrfs_set_header_blocknr(struct btrfs_header *h, u64 blocknr)
Chris Mason7518a232007-03-12 12:01:18 -0400609{
Chris Masonbb492bb2007-03-12 12:29:44 -0400610 h->blocknr = cpu_to_le64(blocknr);
Chris Mason7518a232007-03-12 12:01:18 -0400611}
612
Chris Mason7f5c1512007-03-23 15:56:19 -0400613static inline u64 btrfs_header_generation(struct btrfs_header *h)
614{
615 return le64_to_cpu(h->generation);
616}
617
618static inline void btrfs_set_header_generation(struct btrfs_header *h,
619 u64 val)
620{
621 h->generation = cpu_to_le64(val);
622}
623
Chris Masonbb492bb2007-03-12 12:29:44 -0400624static inline u64 btrfs_header_parentid(struct btrfs_header *h)
Chris Mason7518a232007-03-12 12:01:18 -0400625{
Chris Masonbb492bb2007-03-12 12:29:44 -0400626 return le64_to_cpu(h->parentid);
Chris Mason7518a232007-03-12 12:01:18 -0400627}
628
Chris Masonbb492bb2007-03-12 12:29:44 -0400629static inline void btrfs_set_header_parentid(struct btrfs_header *h,
630 u64 parentid)
Chris Mason7518a232007-03-12 12:01:18 -0400631{
Chris Masonbb492bb2007-03-12 12:29:44 -0400632 h->parentid = cpu_to_le64(parentid);
Chris Mason7518a232007-03-12 12:01:18 -0400633}
634
Chris Masonbb492bb2007-03-12 12:29:44 -0400635static inline u16 btrfs_header_nritems(struct btrfs_header *h)
Chris Mason7518a232007-03-12 12:01:18 -0400636{
Chris Masonbb492bb2007-03-12 12:29:44 -0400637 return le16_to_cpu(h->nritems);
Chris Mason7518a232007-03-12 12:01:18 -0400638}
639
Chris Masonbb492bb2007-03-12 12:29:44 -0400640static inline void btrfs_set_header_nritems(struct btrfs_header *h, u16 val)
Chris Mason7518a232007-03-12 12:01:18 -0400641{
Chris Masonbb492bb2007-03-12 12:29:44 -0400642 h->nritems = cpu_to_le16(val);
Chris Mason7518a232007-03-12 12:01:18 -0400643}
644
Chris Masonbb492bb2007-03-12 12:29:44 -0400645static inline u16 btrfs_header_flags(struct btrfs_header *h)
Chris Mason7518a232007-03-12 12:01:18 -0400646{
Chris Masonbb492bb2007-03-12 12:29:44 -0400647 return le16_to_cpu(h->flags);
Chris Mason7518a232007-03-12 12:01:18 -0400648}
649
Chris Masonbb492bb2007-03-12 12:29:44 -0400650static inline void btrfs_set_header_flags(struct btrfs_header *h, u16 val)
Chris Mason7518a232007-03-12 12:01:18 -0400651{
Chris Masonbb492bb2007-03-12 12:29:44 -0400652 h->flags = cpu_to_le16(val);
Chris Mason7518a232007-03-12 12:01:18 -0400653}
654
Chris Masonbb492bb2007-03-12 12:29:44 -0400655static inline int btrfs_header_level(struct btrfs_header *h)
Chris Mason7518a232007-03-12 12:01:18 -0400656{
Chris Mason9a6f11e2007-03-27 09:06:38 -0400657 return h->level;
Chris Mason7518a232007-03-12 12:01:18 -0400658}
659
Chris Masonbb492bb2007-03-12 12:29:44 -0400660static inline void btrfs_set_header_level(struct btrfs_header *h, int level)
Chris Mason7518a232007-03-12 12:01:18 -0400661{
Chris Mason234b63a2007-03-13 10:46:10 -0400662 BUG_ON(level > BTRFS_MAX_LEVEL);
Chris Mason9a6f11e2007-03-27 09:06:38 -0400663 h->level = level;
Chris Mason7518a232007-03-12 12:01:18 -0400664}
665
Chris Mason234b63a2007-03-13 10:46:10 -0400666static inline int btrfs_is_leaf(struct btrfs_node *n)
Chris Mason7518a232007-03-12 12:01:18 -0400667{
668 return (btrfs_header_level(&n->header) == 0);
669}
670
Chris Mason3768f362007-03-13 16:47:54 -0400671static inline u64 btrfs_root_blocknr(struct btrfs_root_item *item)
672{
673 return le64_to_cpu(item->blocknr);
674}
675
676static inline void btrfs_set_root_blocknr(struct btrfs_root_item *item, u64 val)
677{
678 item->blocknr = cpu_to_le64(val);
679}
680
681static inline u32 btrfs_root_refs(struct btrfs_root_item *item)
682{
683 return le32_to_cpu(item->refs);
684}
685
686static inline void btrfs_set_root_refs(struct btrfs_root_item *item, u32 val)
687{
688 item->refs = cpu_to_le32(val);
689}
690
691static inline u64 btrfs_super_blocknr(struct btrfs_super_block *s)
692{
693 return le64_to_cpu(s->blocknr);
694}
695
696static inline void btrfs_set_super_blocknr(struct btrfs_super_block *s, u64 val)
697{
698 s->blocknr = cpu_to_le64(val);
699}
700
701static inline u64 btrfs_super_root(struct btrfs_super_block *s)
702{
703 return le64_to_cpu(s->root);
704}
705
706static inline void btrfs_set_super_root(struct btrfs_super_block *s, u64 val)
707{
708 s->root = cpu_to_le64(val);
709}
710
711static inline u64 btrfs_super_total_blocks(struct btrfs_super_block *s)
712{
713 return le64_to_cpu(s->total_blocks);
714}
715
716static inline void btrfs_set_super_total_blocks(struct btrfs_super_block *s,
717 u64 val)
718{
719 s->total_blocks = cpu_to_le64(val);
720}
721
722static inline u64 btrfs_super_blocks_used(struct btrfs_super_block *s)
723{
724 return le64_to_cpu(s->blocks_used);
725}
726
727static inline void btrfs_set_super_blocks_used(struct btrfs_super_block *s,
728 u64 val)
729{
730 s->blocks_used = cpu_to_le64(val);
731}
732
Chris Mason123abc82007-03-14 14:14:43 -0400733static inline u32 btrfs_super_blocksize(struct btrfs_super_block *s)
Chris Mason3768f362007-03-13 16:47:54 -0400734{
Chris Mason123abc82007-03-14 14:14:43 -0400735 return le32_to_cpu(s->blocksize);
Chris Mason3768f362007-03-13 16:47:54 -0400736}
737
738static inline void btrfs_set_super_blocksize(struct btrfs_super_block *s,
Chris Mason123abc82007-03-14 14:14:43 -0400739 u32 val)
Chris Mason3768f362007-03-13 16:47:54 -0400740{
Chris Mason123abc82007-03-14 14:14:43 -0400741 s->blocksize = cpu_to_le32(val);
742}
743
Chris Mason2e635a22007-03-21 11:12:56 -0400744static inline u64 btrfs_super_root_dir(struct btrfs_super_block *s)
745{
746 return le64_to_cpu(s->root_dir_objectid);
747}
748
749static inline void btrfs_set_super_root_dir(struct btrfs_super_block *s, u64
750 val)
751{
752 s->root_dir_objectid = cpu_to_le64(val);
753}
754
Chris Mason123abc82007-03-14 14:14:43 -0400755static inline u8 *btrfs_leaf_data(struct btrfs_leaf *l)
756{
757 return (u8 *)l->items;
Chris Mason3768f362007-03-13 16:47:54 -0400758}
Chris Mason9f5fae22007-03-20 14:38:32 -0400759
760static inline u64 btrfs_file_extent_disk_blocknr(struct btrfs_file_extent_item
761 *e)
762{
763 return le64_to_cpu(e->disk_blocknr);
764}
765
766static inline void btrfs_set_file_extent_disk_blocknr(struct
767 btrfs_file_extent_item
768 *e, u64 val)
769{
770 e->disk_blocknr = cpu_to_le64(val);
771}
772
Chris Mason71951f32007-03-27 09:16:29 -0400773static inline u64 btrfs_file_extent_generation(struct btrfs_file_extent_item *e)
774{
775 return le64_to_cpu(e->generation);
776}
777
778static inline void btrfs_set_file_extent_generation(struct
779 btrfs_file_extent_item *e,
780 u64 val)
781{
782 e->generation = cpu_to_le64(val);
783}
784
Chris Mason9f5fae22007-03-20 14:38:32 -0400785static inline u64 btrfs_file_extent_disk_num_blocks(struct
786 btrfs_file_extent_item *e)
787{
788 return le64_to_cpu(e->disk_num_blocks);
789}
790
791static inline void btrfs_set_file_extent_disk_num_blocks(struct
792 btrfs_file_extent_item
793 *e, u64 val)
794{
795 e->disk_num_blocks = cpu_to_le64(val);
796}
797
798static inline u64 btrfs_file_extent_offset(struct btrfs_file_extent_item *e)
799{
800 return le64_to_cpu(e->offset);
801}
802
803static inline void btrfs_set_file_extent_offset(struct btrfs_file_extent_item
804 *e, u64 val)
805{
806 e->offset = cpu_to_le64(val);
807}
808
809static inline u64 btrfs_file_extent_num_blocks(struct btrfs_file_extent_item
810 *e)
811{
812 return le64_to_cpu(e->num_blocks);
813}
814
815static inline void btrfs_set_file_extent_num_blocks(struct
816 btrfs_file_extent_item *e,
817 u64 val)
818{
819 e->num_blocks = cpu_to_le64(val);
820}
821
Chris Masone20d96d2007-03-22 12:13:20 -0400822static inline struct btrfs_root *btrfs_sb(struct super_block *sb)
823{
824 return sb->s_fs_info;
825}
826
Chris Mason4beb1b82007-03-14 10:31:29 -0400827/* helper function to cast into the data area of the leaf. */
828#define btrfs_item_ptr(leaf, slot, type) \
Chris Mason123abc82007-03-14 14:14:43 -0400829 ((type *)(btrfs_leaf_data(leaf) + \
830 btrfs_item_offset((leaf)->items + (slot))))
Chris Mason4beb1b82007-03-14 10:31:29 -0400831
Chris Masondee26a92007-03-26 16:00:06 -0400832/* extent-item.c */
Chris Masone20d96d2007-03-22 12:13:20 -0400833struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masone089f052007-03-16 16:20:31 -0400834 struct btrfs_root *root);
Chris Masondee26a92007-03-26 16:00:06 -0400835int btrfs_alloc_extent(struct btrfs_trans_handle *trans, struct btrfs_root
836 *root, u64 num_blocks, u64 search_start, u64
837 search_end, u64 owner, struct btrfs_key *ins);
Chris Masone089f052007-03-16 16:20:31 -0400838int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -0400839 struct buffer_head *buf);
Chris Masone089f052007-03-16 16:20:31 -0400840int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
841 *root, u64 blocknr, u64 num_blocks, int pin);
Chris Masondee26a92007-03-26 16:00:06 -0400842int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
843 btrfs_root *root);
844/* ctree.c */
Chris Masone089f052007-03-16 16:20:31 -0400845int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
846 *root, struct btrfs_key *key, struct btrfs_path *p, int
847 ins_len, int cow);
Chris Mason234b63a2007-03-13 10:46:10 -0400848void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p);
849void btrfs_init_path(struct btrfs_path *p);
Chris Masone089f052007-03-16 16:20:31 -0400850int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
851 struct btrfs_path *path);
852int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
853 *root, struct btrfs_key *key, void *data, u32 data_size);
854int btrfs_insert_empty_item(struct btrfs_trans_handle *trans, struct btrfs_root
855 *root, struct btrfs_path *path, struct btrfs_key
856 *cpu_key, u32 data_size);
Chris Mason234b63a2007-03-13 10:46:10 -0400857int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path);
Chris Mason123abc82007-03-14 14:14:43 -0400858int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf);
Chris Masone089f052007-03-16 16:20:31 -0400859int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -0400860 *root, struct buffer_head *snap);
Chris Masondee26a92007-03-26 16:00:06 -0400861/* root-item.c */
Chris Masone089f052007-03-16 16:20:31 -0400862int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
863 struct btrfs_key *key);
864int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
865 *root, struct btrfs_key *key, struct btrfs_root_item
866 *item);
867int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
868 *root, struct btrfs_key *key, struct btrfs_root_item
869 *item);
870int btrfs_find_last_root(struct btrfs_root *root, u64 objectid, struct
871 btrfs_root_item *item, struct btrfs_key *key);
Chris Masondee26a92007-03-26 16:00:06 -0400872/* dir-item.c */
Chris Masone089f052007-03-16 16:20:31 -0400873int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond5719762007-03-23 10:01:08 -0400874 *root, const char *name, int name_len, u64 dir, u64
Chris Masone089f052007-03-16 16:20:31 -0400875 objectid, u8 type);
876int btrfs_lookup_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -0400877 *root, struct btrfs_path *path, u64 dir,
878 const char *name, int name_len, int mod);
Chris Mason1d4f6402007-03-15 15:18:43 -0400879int btrfs_match_dir_item_name(struct btrfs_root *root, struct btrfs_path *path,
Chris Mason7f5c1512007-03-23 15:56:19 -0400880 const char *name, int name_len);
Chris Masondee26a92007-03-26 16:00:06 -0400881/* inode-map.c */
Chris Mason9f5fae22007-03-20 14:38:32 -0400882int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
883 struct btrfs_root *fs_root,
884 u64 dirid, u64 *objectid);
885int btrfs_insert_inode_map(struct btrfs_trans_handle *trans,
886 struct btrfs_root *root,
887 u64 objectid, struct btrfs_key *location);
888int btrfs_lookup_inode_map(struct btrfs_trans_handle *trans,
889 struct btrfs_root *root, struct btrfs_path *path,
890 u64 objectid, int mod);
Chris Masondee26a92007-03-26 16:00:06 -0400891/* inode-item.c */
Chris Mason293ffd52007-03-20 15:57:25 -0400892int btrfs_insert_inode(struct btrfs_trans_handle *trans, struct btrfs_root
893 *root, u64 objectid, struct btrfs_inode_item
894 *inode_item);
895int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root
896 *root, struct btrfs_path *path, u64 objectid, int mod);
Chris Masondee26a92007-03-26 16:00:06 -0400897
898/* file-item.c */
899int btrfs_alloc_file_extent(struct btrfs_trans_handle *trans,
900 struct btrfs_root *root,
901 u64 objectid, u64 offset,
902 u64 num_blocks, u64 hint_block,
903 u64 *result);
904int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
905 struct btrfs_root *root,
906 struct btrfs_path *path, u64 objectid,
Chris Mason9773a782007-03-27 11:26:26 -0400907 u64 blocknr, int mod);
Chris Masoneb60cea2007-02-02 09:18:22 -0500908#endif