blob: b2ebed756c13ddd77ff2f73df4c975820356ca4e [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 Mason3768f362007-03-13 16:47:54 -040054 u8 fsid[16]; /* FS specific uuid */
Chris Masonbb492bb2007-03-12 12:29:44 -040055 __le64 blocknr; /* which block this node is supposed to live in */
Chris Mason7f5c1512007-03-23 15:56:19 -040056 __le64 generation;
Chris Masonbb492bb2007-03-12 12:29:44 -040057 __le64 parentid; /* objectid of the tree root */
58 __le32 csum;
59 __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 Mason3768f362007-03-13 16:47:54 -040078 u8 fsid[16]; /* FS specific uuid */
79 __le64 blocknr; /* this block number */
80 __le32 csum;
81 __le64 magic;
Chris Mason123abc82007-03-14 14:14:43 -040082 __le32 blocksize;
Chris Mason3768f362007-03-13 16:47:54 -040083 __le64 generation;
84 __le64 root;
85 __le64 total_blocks;
86 __le64 blocks_used;
Chris Mason2e635a22007-03-21 11:12:56 -040087 __le64 root_dir_objectid;
Chris Masoncfaa7292007-02-21 17:04:57 -050088} __attribute__ ((__packed__));
89
Chris Masonfec577f2007-02-26 10:40:21 -050090/*
Chris Mason62e27492007-03-15 12:56:47 -040091 * A leaf is full of items. offset and size tell us where to find
Chris Masonfec577f2007-02-26 10:40:21 -050092 * the item in the leaf (relative to the start of the data area)
93 */
Chris Mason0783fcf2007-03-12 20:12:07 -040094struct btrfs_item {
Chris Masone2fa7222007-03-12 16:22:34 -040095 struct btrfs_disk_key key;
Chris Mason123abc82007-03-14 14:14:43 -040096 __le32 offset;
Chris Mason0783fcf2007-03-12 20:12:07 -040097 __le16 size;
Chris Masoneb60cea2007-02-02 09:18:22 -050098} __attribute__ ((__packed__));
99
Chris Masonfec577f2007-02-26 10:40:21 -0500100/*
101 * leaves have an item area and a data area:
102 * [item0, item1....itemN] [free space] [dataN...data1, data0]
103 *
104 * The data is separate from the items to get the keys closer together
105 * during searches.
106 */
Chris Mason234b63a2007-03-13 10:46:10 -0400107struct btrfs_leaf {
Chris Masonbb492bb2007-03-12 12:29:44 -0400108 struct btrfs_header header;
Chris Mason123abc82007-03-14 14:14:43 -0400109 struct btrfs_item items[];
Chris Masoneb60cea2007-02-02 09:18:22 -0500110} __attribute__ ((__packed__));
111
Chris Masonfec577f2007-02-26 10:40:21 -0500112/*
113 * all non-leaf blocks are nodes, they hold only keys and pointers to
114 * other blocks
115 */
Chris Mason123abc82007-03-14 14:14:43 -0400116struct btrfs_key_ptr {
117 struct btrfs_disk_key key;
118 __le64 blockptr;
119} __attribute__ ((__packed__));
120
Chris Mason234b63a2007-03-13 10:46:10 -0400121struct btrfs_node {
Chris Masonbb492bb2007-03-12 12:29:44 -0400122 struct btrfs_header header;
Chris Mason123abc82007-03-14 14:14:43 -0400123 struct btrfs_key_ptr ptrs[];
Chris Masoneb60cea2007-02-02 09:18:22 -0500124} __attribute__ ((__packed__));
125
Chris Masonfec577f2007-02-26 10:40:21 -0500126/*
Chris Mason234b63a2007-03-13 10:46:10 -0400127 * btrfs_paths remember the path taken from the root down to the leaf.
128 * level 0 is always the leaf, and nodes[1...BTRFS_MAX_LEVEL] will point
Chris Masonfec577f2007-02-26 10:40:21 -0500129 * to any other levels that are present.
130 *
131 * The slots array records the index of the item or block pointer
132 * used while walking the tree.
133 */
Chris Mason234b63a2007-03-13 10:46:10 -0400134struct btrfs_path {
Chris Masone20d96d2007-03-22 12:13:20 -0400135 struct buffer_head *nodes[BTRFS_MAX_LEVEL];
Chris Mason234b63a2007-03-13 10:46:10 -0400136 int slots[BTRFS_MAX_LEVEL];
Chris Masoneb60cea2007-02-02 09:18:22 -0500137};
Chris Mason5de08d72007-02-24 06:24:44 -0500138
Chris Mason62e27492007-03-15 12:56:47 -0400139/*
140 * items in the extent btree are used to record the objectid of the
141 * owner of the block and the number of references
142 */
143struct btrfs_extent_item {
144 __le32 refs;
145 __le64 owner;
146} __attribute__ ((__packed__));
147
Chris Mason1e1d2702007-03-15 19:03:33 -0400148struct btrfs_inode_timespec {
149 __le32 sec;
150 __le32 nsec;
151} __attribute__ ((__packed__));
152
153/*
154 * there is no padding here on purpose. If you want to extent the inode,
155 * make a new item type
156 */
157struct btrfs_inode_item {
158 __le64 generation;
159 __le64 size;
160 __le64 nblocks;
161 __le32 nlink;
162 __le32 uid;
163 __le32 gid;
164 __le32 mode;
165 __le32 rdev;
166 __le16 flags;
167 __le16 compat_flags;
168 struct btrfs_inode_timespec atime;
169 struct btrfs_inode_timespec ctime;
170 struct btrfs_inode_timespec mtime;
171 struct btrfs_inode_timespec otime;
172} __attribute__ ((__packed__));
173
174/* inline data is just a blob of bytes */
175struct btrfs_inline_data_item {
176 u8 data;
177} __attribute__ ((__packed__));
178
Chris Mason62e27492007-03-15 12:56:47 -0400179struct btrfs_dir_item {
180 __le64 objectid;
181 __le16 flags;
Chris Masona8a2ee02007-03-16 08:46:49 -0400182 __le16 name_len;
Chris Mason62e27492007-03-15 12:56:47 -0400183 u8 type;
184} __attribute__ ((__packed__));
185
186struct btrfs_root_item {
187 __le64 blocknr;
188 __le32 flags;
189 __le64 block_limit;
190 __le64 blocks_used;
191 __le32 refs;
Chris Mason9f5fae22007-03-20 14:38:32 -0400192} __attribute__ ((__packed__));
193
194struct btrfs_file_extent_item {
Chris Mason71951f32007-03-27 09:16:29 -0400195 __le64 generation;
Chris Mason9f5fae22007-03-20 14:38:32 -0400196 /*
197 * disk space consumed by the extent, checksum blocks are included
198 * in these numbers
199 */
200 __le64 disk_blocknr;
201 __le64 disk_num_blocks;
202 /*
Chris Masondee26a92007-03-26 16:00:06 -0400203 * the logical offset in file blocks (no csums)
Chris Mason9f5fae22007-03-20 14:38:32 -0400204 * this extent record is for. This allows a file extent to point
205 * into the middle of an existing extent on disk, sharing it
206 * between two snapshots (useful if some bytes in the middle of the
207 * extent have changed
208 */
209 __le64 offset;
210 /*
211 * the logical number of file blocks (no csums included)
212 */
213 __le64 num_blocks;
214} __attribute__ ((__packed__));
215
216struct btrfs_inode_map_item {
217 struct btrfs_disk_key key;
218} __attribute__ ((__packed__));
219
220struct btrfs_fs_info {
221 struct btrfs_root *fs_root;
222 struct btrfs_root *extent_root;
223 struct btrfs_root *tree_root;
224 struct btrfs_root *inode_root;
225 struct btrfs_key current_insert;
226 struct btrfs_key last_insert;
Chris Mason8ef97622007-03-26 10:15:30 -0400227 struct radix_tree_root pending_del_radix;
Chris Mason9f5fae22007-03-20 14:38:32 -0400228 struct radix_tree_root pinned_radix;
Chris Mason9f5fae22007-03-20 14:38:32 -0400229 u64 last_inode_alloc;
230 u64 last_inode_alloc_dirid;
Chris Mason293ffd52007-03-20 15:57:25 -0400231 u64 generation;
Chris Mason79154b12007-03-22 15:59:16 -0400232 struct btrfs_transaction *running_transaction;
Chris Mason1261ec42007-03-20 20:35:03 -0400233 struct btrfs_super_block *disk_super;
Chris Masone20d96d2007-03-22 12:13:20 -0400234 struct buffer_head *sb_buffer;
235 struct super_block *sb;
Chris Masond98237b2007-03-28 13:57:48 -0400236 struct inode *btree_inode;
Chris Mason79154b12007-03-22 15:59:16 -0400237 struct mutex trans_mutex;
Chris Masond561c022007-03-23 19:47:49 -0400238 struct mutex fs_mutex;
Chris Mason62e27492007-03-15 12:56:47 -0400239};
240
241/*
242 * in ram representation of the tree. extent_root is used for all allocations
243 * and for the extent tree extent_root root. current_insert is used
244 * only for the extent tree.
245 */
246struct btrfs_root {
Chris Masone20d96d2007-03-22 12:13:20 -0400247 struct buffer_head *node;
248 struct buffer_head *commit_root;
Chris Mason62e27492007-03-15 12:56:47 -0400249 struct btrfs_root_item root_item;
250 struct btrfs_key root_key;
Chris Mason9f5fae22007-03-20 14:38:32 -0400251 struct btrfs_fs_info *fs_info;
Chris Mason62e27492007-03-15 12:56:47 -0400252 u32 blocksize;
Chris Mason9f5fae22007-03-20 14:38:32 -0400253 int ref_cows;
254 u32 type;
Chris Mason62e27492007-03-15 12:56:47 -0400255};
256
Chris Mason62e27492007-03-15 12:56:47 -0400257/* the lower bits in the key flags defines the item type */
258#define BTRFS_KEY_TYPE_MAX 256
259#define BTRFS_KEY_TYPE_MASK (BTRFS_KEY_TYPE_MAX - 1)
Chris Mason1e1d2702007-03-15 19:03:33 -0400260
261/*
262 * inode items have the data typically returned from stat and store other
263 * info about object characteristics. There is one for every file and dir in
264 * the FS
265 */
Chris Mason62e27492007-03-15 12:56:47 -0400266#define BTRFS_INODE_ITEM_KEY 1
Chris Mason1e1d2702007-03-15 19:03:33 -0400267
268/*
269 * dir items are the name -> inode pointers in a directory. There is one
270 * for every name in a directory.
271 */
Chris Mason62e27492007-03-15 12:56:47 -0400272#define BTRFS_DIR_ITEM_KEY 2
Chris Mason1e1d2702007-03-15 19:03:33 -0400273/*
274 * inline data is file data that fits in the btree.
275 */
276#define BTRFS_INLINE_DATA_KEY 3
277/*
278 * extent data is for data that can't fit in the btree. It points to
279 * a (hopefully) huge chunk of disk
280 */
281#define BTRFS_EXTENT_DATA_KEY 4
282/*
283 * root items point to tree roots. There are typically in the root
284 * tree used by the super block to find all the other trees
285 */
286#define BTRFS_ROOT_ITEM_KEY 5
287/*
288 * extent items are in the extent map tree. These record which blocks
289 * are used, and how many references there are to each block
290 */
291#define BTRFS_EXTENT_ITEM_KEY 6
Chris Mason9f5fae22007-03-20 14:38:32 -0400292
293/*
294 * the inode map records which inode numbers are in use and where
295 * they actually live on disk
296 */
297#define BTRFS_INODE_MAP_ITEM_KEY 7
Chris Mason1e1d2702007-03-15 19:03:33 -0400298/*
299 * string items are for debugging. They just store a short string of
300 * data in the FS
301 */
Chris Mason9f5fae22007-03-20 14:38:32 -0400302#define BTRFS_STRING_ITEM_KEY 8
Chris Mason1e1d2702007-03-15 19:03:33 -0400303
304static inline u64 btrfs_inode_generation(struct btrfs_inode_item *i)
305{
306 return le64_to_cpu(i->generation);
307}
308
309static inline void btrfs_set_inode_generation(struct btrfs_inode_item *i,
310 u64 val)
311{
312 i->generation = cpu_to_le64(val);
313}
314
315static inline u64 btrfs_inode_size(struct btrfs_inode_item *i)
316{
317 return le64_to_cpu(i->size);
318}
319
320static inline void btrfs_set_inode_size(struct btrfs_inode_item *i, u64 val)
321{
322 i->size = cpu_to_le64(val);
323}
324
325static inline u64 btrfs_inode_nblocks(struct btrfs_inode_item *i)
326{
327 return le64_to_cpu(i->nblocks);
328}
329
330static inline void btrfs_set_inode_nblocks(struct btrfs_inode_item *i, u64 val)
331{
332 i->nblocks = cpu_to_le64(val);
333}
334
335static inline u32 btrfs_inode_nlink(struct btrfs_inode_item *i)
336{
337 return le32_to_cpu(i->nlink);
338}
339
340static inline void btrfs_set_inode_nlink(struct btrfs_inode_item *i, u32 val)
341{
342 i->nlink = cpu_to_le32(val);
343}
344
345static inline u32 btrfs_inode_uid(struct btrfs_inode_item *i)
346{
347 return le32_to_cpu(i->uid);
348}
349
350static inline void btrfs_set_inode_uid(struct btrfs_inode_item *i, u32 val)
351{
352 i->uid = cpu_to_le32(val);
353}
354
355static inline u32 btrfs_inode_gid(struct btrfs_inode_item *i)
356{
357 return le32_to_cpu(i->gid);
358}
359
360static inline void btrfs_set_inode_gid(struct btrfs_inode_item *i, u32 val)
361{
362 i->gid = cpu_to_le32(val);
363}
364
365static inline u32 btrfs_inode_mode(struct btrfs_inode_item *i)
366{
367 return le32_to_cpu(i->mode);
368}
369
370static inline void btrfs_set_inode_mode(struct btrfs_inode_item *i, u32 val)
371{
372 i->mode = cpu_to_le32(val);
373}
374
375static inline u32 btrfs_inode_rdev(struct btrfs_inode_item *i)
376{
377 return le32_to_cpu(i->rdev);
378}
379
380static inline void btrfs_set_inode_rdev(struct btrfs_inode_item *i, u32 val)
381{
382 i->rdev = cpu_to_le32(val);
383}
384
385static inline u16 btrfs_inode_flags(struct btrfs_inode_item *i)
386{
387 return le16_to_cpu(i->flags);
388}
389
390static inline void btrfs_set_inode_flags(struct btrfs_inode_item *i, u16 val)
391{
392 i->flags = cpu_to_le16(val);
393}
394
395static inline u16 btrfs_inode_compat_flags(struct btrfs_inode_item *i)
396{
397 return le16_to_cpu(i->compat_flags);
398}
399
400static inline void btrfs_set_inode_compat_flags(struct btrfs_inode_item *i,
401 u16 val)
402{
403 i->compat_flags = cpu_to_le16(val);
404}
405
Chris Masone20d96d2007-03-22 12:13:20 -0400406static inline u32 btrfs_timespec_sec(struct btrfs_inode_timespec *ts)
407{
408 return le32_to_cpu(ts->sec);
409}
410
411static inline void btrfs_set_timespec_sec(struct btrfs_inode_timespec *ts,
412 u32 val)
413{
414 ts->sec = cpu_to_le32(val);
415}
416
417static inline u32 btrfs_timespec_nsec(struct btrfs_inode_timespec *ts)
418{
419 return le32_to_cpu(ts->nsec);
420}
421
422static inline void btrfs_set_timespec_nsec(struct btrfs_inode_timespec *ts,
423 u32 val)
424{
425 ts->nsec = cpu_to_le32(val);
426}
427
428
Chris Mason62e27492007-03-15 12:56:47 -0400429
Chris Mason234b63a2007-03-13 10:46:10 -0400430static inline u64 btrfs_extent_owner(struct btrfs_extent_item *ei)
Chris Masoncf27e1e2007-03-13 09:49:06 -0400431{
432 return le64_to_cpu(ei->owner);
433}
434
Chris Mason234b63a2007-03-13 10:46:10 -0400435static inline void btrfs_set_extent_owner(struct btrfs_extent_item *ei, u64 val)
Chris Masoncf27e1e2007-03-13 09:49:06 -0400436{
437 ei->owner = cpu_to_le64(val);
438}
439
Chris Mason234b63a2007-03-13 10:46:10 -0400440static inline u32 btrfs_extent_refs(struct btrfs_extent_item *ei)
Chris Masoncf27e1e2007-03-13 09:49:06 -0400441{
442 return le32_to_cpu(ei->refs);
443}
444
Chris Mason234b63a2007-03-13 10:46:10 -0400445static inline void btrfs_set_extent_refs(struct btrfs_extent_item *ei, u32 val)
Chris Masoncf27e1e2007-03-13 09:49:06 -0400446{
447 ei->refs = cpu_to_le32(val);
448}
449
Chris Mason234b63a2007-03-13 10:46:10 -0400450static inline u64 btrfs_node_blockptr(struct btrfs_node *n, int nr)
Chris Mason1d4f8a02007-03-13 09:28:32 -0400451{
Chris Mason123abc82007-03-14 14:14:43 -0400452 return le64_to_cpu(n->ptrs[nr].blockptr);
Chris Mason1d4f8a02007-03-13 09:28:32 -0400453}
454
Chris Mason234b63a2007-03-13 10:46:10 -0400455static inline void btrfs_set_node_blockptr(struct btrfs_node *n, int nr,
456 u64 val)
Chris Mason1d4f8a02007-03-13 09:28:32 -0400457{
Chris Mason123abc82007-03-14 14:14:43 -0400458 n->ptrs[nr].blockptr = cpu_to_le64(val);
Chris Mason1d4f8a02007-03-13 09:28:32 -0400459}
460
Chris Mason123abc82007-03-14 14:14:43 -0400461static inline u32 btrfs_item_offset(struct btrfs_item *item)
Chris Mason0783fcf2007-03-12 20:12:07 -0400462{
Chris Mason123abc82007-03-14 14:14:43 -0400463 return le32_to_cpu(item->offset);
Chris Mason0783fcf2007-03-12 20:12:07 -0400464}
465
Chris Mason123abc82007-03-14 14:14:43 -0400466static inline void btrfs_set_item_offset(struct btrfs_item *item, u32 val)
Chris Mason0783fcf2007-03-12 20:12:07 -0400467{
Chris Mason123abc82007-03-14 14:14:43 -0400468 item->offset = cpu_to_le32(val);
Chris Mason0783fcf2007-03-12 20:12:07 -0400469}
470
Chris Mason123abc82007-03-14 14:14:43 -0400471static inline u32 btrfs_item_end(struct btrfs_item *item)
Chris Mason0783fcf2007-03-12 20:12:07 -0400472{
Chris Mason123abc82007-03-14 14:14:43 -0400473 return le32_to_cpu(item->offset) + le16_to_cpu(item->size);
Chris Mason0783fcf2007-03-12 20:12:07 -0400474}
475
476static inline u16 btrfs_item_size(struct btrfs_item *item)
477{
478 return le16_to_cpu(item->size);
479}
480
481static inline void btrfs_set_item_size(struct btrfs_item *item, u16 val)
482{
483 item->size = cpu_to_le16(val);
484}
485
Chris Mason1d4f6402007-03-15 15:18:43 -0400486static inline u64 btrfs_dir_objectid(struct btrfs_dir_item *d)
487{
488 return le64_to_cpu(d->objectid);
489}
490
491static inline void btrfs_set_dir_objectid(struct btrfs_dir_item *d, u64 val)
492{
493 d->objectid = cpu_to_le64(val);
494}
495
496static inline u16 btrfs_dir_flags(struct btrfs_dir_item *d)
497{
498 return le16_to_cpu(d->flags);
499}
500
501static inline void btrfs_set_dir_flags(struct btrfs_dir_item *d, u16 val)
502{
503 d->flags = cpu_to_le16(val);
504}
505
506static inline u8 btrfs_dir_type(struct btrfs_dir_item *d)
507{
508 return d->type;
509}
510
511static inline void btrfs_set_dir_type(struct btrfs_dir_item *d, u8 val)
512{
513 d->type = val;
514}
515
Chris Masona8a2ee02007-03-16 08:46:49 -0400516static inline u16 btrfs_dir_name_len(struct btrfs_dir_item *d)
Chris Mason1d4f6402007-03-15 15:18:43 -0400517{
Chris Masona8a2ee02007-03-16 08:46:49 -0400518 return le16_to_cpu(d->name_len);
519}
520
521static inline void btrfs_set_dir_name_len(struct btrfs_dir_item *d, u16 val)
522{
523 d->name_len = cpu_to_le16(val);
Chris Mason1d4f6402007-03-15 15:18:43 -0400524}
525
Chris Masone2fa7222007-03-12 16:22:34 -0400526static inline void btrfs_disk_key_to_cpu(struct btrfs_key *cpu,
527 struct btrfs_disk_key *disk)
528{
529 cpu->offset = le64_to_cpu(disk->offset);
530 cpu->flags = le32_to_cpu(disk->flags);
531 cpu->objectid = le64_to_cpu(disk->objectid);
532}
533
534static inline void btrfs_cpu_key_to_disk(struct btrfs_disk_key *disk,
535 struct btrfs_key *cpu)
536{
537 disk->offset = cpu_to_le64(cpu->offset);
538 disk->flags = cpu_to_le32(cpu->flags);
539 disk->objectid = cpu_to_le64(cpu->objectid);
540}
541
Chris Mason62e27492007-03-15 12:56:47 -0400542static inline u64 btrfs_disk_key_objectid(struct btrfs_disk_key *disk)
Chris Masone2fa7222007-03-12 16:22:34 -0400543{
544 return le64_to_cpu(disk->objectid);
545}
546
Chris Mason62e27492007-03-15 12:56:47 -0400547static inline void btrfs_set_disk_key_objectid(struct btrfs_disk_key *disk,
548 u64 val)
Chris Masone2fa7222007-03-12 16:22:34 -0400549{
550 disk->objectid = cpu_to_le64(val);
551}
552
Chris Mason62e27492007-03-15 12:56:47 -0400553static inline u64 btrfs_disk_key_offset(struct btrfs_disk_key *disk)
Chris Masone2fa7222007-03-12 16:22:34 -0400554{
555 return le64_to_cpu(disk->offset);
556}
557
Chris Mason62e27492007-03-15 12:56:47 -0400558static inline void btrfs_set_disk_key_offset(struct btrfs_disk_key *disk,
559 u64 val)
Chris Masone2fa7222007-03-12 16:22:34 -0400560{
561 disk->offset = cpu_to_le64(val);
562}
563
Chris Mason62e27492007-03-15 12:56:47 -0400564static inline u32 btrfs_disk_key_flags(struct btrfs_disk_key *disk)
Chris Masone2fa7222007-03-12 16:22:34 -0400565{
566 return le32_to_cpu(disk->flags);
567}
568
Chris Mason62e27492007-03-15 12:56:47 -0400569static inline void btrfs_set_disk_key_flags(struct btrfs_disk_key *disk,
570 u32 val)
Chris Masone2fa7222007-03-12 16:22:34 -0400571{
572 disk->flags = cpu_to_le32(val);
573}
574
Chris Mason62e27492007-03-15 12:56:47 -0400575static inline u32 btrfs_key_type(struct btrfs_key *key)
576{
577 return key->flags & BTRFS_KEY_TYPE_MASK;
578}
579
580static inline u32 btrfs_disk_key_type(struct btrfs_disk_key *key)
581{
582 return le32_to_cpu(key->flags) & BTRFS_KEY_TYPE_MASK;
583}
584
585static inline void btrfs_set_key_type(struct btrfs_key *key, u32 type)
586{
587 BUG_ON(type >= BTRFS_KEY_TYPE_MAX);
588 key->flags = (key->flags & ~((u64)BTRFS_KEY_TYPE_MASK)) | type;
589}
590
591static inline void btrfs_set_disk_key_type(struct btrfs_disk_key *key, u32 type)
592{
593 u32 flags = btrfs_disk_key_flags(key);
594 BUG_ON(type >= BTRFS_KEY_TYPE_MAX);
595 flags = (flags & ~((u64)BTRFS_KEY_TYPE_MASK)) | type;
596 btrfs_set_disk_key_flags(key, flags);
597}
598
Chris Masonbb492bb2007-03-12 12:29:44 -0400599static inline u64 btrfs_header_blocknr(struct btrfs_header *h)
Chris Mason7518a232007-03-12 12:01:18 -0400600{
Chris Masonbb492bb2007-03-12 12:29:44 -0400601 return le64_to_cpu(h->blocknr);
Chris Mason7518a232007-03-12 12:01:18 -0400602}
603
Chris Masonbb492bb2007-03-12 12:29:44 -0400604static inline void btrfs_set_header_blocknr(struct btrfs_header *h, u64 blocknr)
Chris Mason7518a232007-03-12 12:01:18 -0400605{
Chris Masonbb492bb2007-03-12 12:29:44 -0400606 h->blocknr = cpu_to_le64(blocknr);
Chris Mason7518a232007-03-12 12:01:18 -0400607}
608
Chris Mason7f5c1512007-03-23 15:56:19 -0400609static inline u64 btrfs_header_generation(struct btrfs_header *h)
610{
611 return le64_to_cpu(h->generation);
612}
613
614static inline void btrfs_set_header_generation(struct btrfs_header *h,
615 u64 val)
616{
617 h->generation = cpu_to_le64(val);
618}
619
Chris Masonbb492bb2007-03-12 12:29:44 -0400620static inline u64 btrfs_header_parentid(struct btrfs_header *h)
Chris Mason7518a232007-03-12 12:01:18 -0400621{
Chris Masonbb492bb2007-03-12 12:29:44 -0400622 return le64_to_cpu(h->parentid);
Chris Mason7518a232007-03-12 12:01:18 -0400623}
624
Chris Masonbb492bb2007-03-12 12:29:44 -0400625static inline void btrfs_set_header_parentid(struct btrfs_header *h,
626 u64 parentid)
Chris Mason7518a232007-03-12 12:01:18 -0400627{
Chris Masonbb492bb2007-03-12 12:29:44 -0400628 h->parentid = cpu_to_le64(parentid);
Chris Mason7518a232007-03-12 12:01:18 -0400629}
630
Chris Masonbb492bb2007-03-12 12:29:44 -0400631static inline u16 btrfs_header_nritems(struct btrfs_header *h)
Chris Mason7518a232007-03-12 12:01:18 -0400632{
Chris Masonbb492bb2007-03-12 12:29:44 -0400633 return le16_to_cpu(h->nritems);
Chris Mason7518a232007-03-12 12:01:18 -0400634}
635
Chris Masonbb492bb2007-03-12 12:29:44 -0400636static inline void btrfs_set_header_nritems(struct btrfs_header *h, u16 val)
Chris Mason7518a232007-03-12 12:01:18 -0400637{
Chris Masonbb492bb2007-03-12 12:29:44 -0400638 h->nritems = cpu_to_le16(val);
Chris Mason7518a232007-03-12 12:01:18 -0400639}
640
Chris Masonbb492bb2007-03-12 12:29:44 -0400641static inline u16 btrfs_header_flags(struct btrfs_header *h)
Chris Mason7518a232007-03-12 12:01:18 -0400642{
Chris Masonbb492bb2007-03-12 12:29:44 -0400643 return le16_to_cpu(h->flags);
Chris Mason7518a232007-03-12 12:01:18 -0400644}
645
Chris Masonbb492bb2007-03-12 12:29:44 -0400646static inline void btrfs_set_header_flags(struct btrfs_header *h, u16 val)
Chris Mason7518a232007-03-12 12:01:18 -0400647{
Chris Masonbb492bb2007-03-12 12:29:44 -0400648 h->flags = cpu_to_le16(val);
Chris Mason7518a232007-03-12 12:01:18 -0400649}
650
Chris Masonbb492bb2007-03-12 12:29:44 -0400651static inline int btrfs_header_level(struct btrfs_header *h)
Chris Mason7518a232007-03-12 12:01:18 -0400652{
Chris Mason9a6f11e2007-03-27 09:06:38 -0400653 return h->level;
Chris Mason7518a232007-03-12 12:01:18 -0400654}
655
Chris Masonbb492bb2007-03-12 12:29:44 -0400656static inline void btrfs_set_header_level(struct btrfs_header *h, int level)
Chris Mason7518a232007-03-12 12:01:18 -0400657{
Chris Mason234b63a2007-03-13 10:46:10 -0400658 BUG_ON(level > BTRFS_MAX_LEVEL);
Chris Mason9a6f11e2007-03-27 09:06:38 -0400659 h->level = level;
Chris Mason7518a232007-03-12 12:01:18 -0400660}
661
Chris Mason234b63a2007-03-13 10:46:10 -0400662static inline int btrfs_is_leaf(struct btrfs_node *n)
Chris Mason7518a232007-03-12 12:01:18 -0400663{
664 return (btrfs_header_level(&n->header) == 0);
665}
666
Chris Mason3768f362007-03-13 16:47:54 -0400667static inline u64 btrfs_root_blocknr(struct btrfs_root_item *item)
668{
669 return le64_to_cpu(item->blocknr);
670}
671
672static inline void btrfs_set_root_blocknr(struct btrfs_root_item *item, u64 val)
673{
674 item->blocknr = cpu_to_le64(val);
675}
676
677static inline u32 btrfs_root_refs(struct btrfs_root_item *item)
678{
679 return le32_to_cpu(item->refs);
680}
681
682static inline void btrfs_set_root_refs(struct btrfs_root_item *item, u32 val)
683{
684 item->refs = cpu_to_le32(val);
685}
686
687static inline u64 btrfs_super_blocknr(struct btrfs_super_block *s)
688{
689 return le64_to_cpu(s->blocknr);
690}
691
692static inline void btrfs_set_super_blocknr(struct btrfs_super_block *s, u64 val)
693{
694 s->blocknr = cpu_to_le64(val);
695}
696
697static inline u64 btrfs_super_root(struct btrfs_super_block *s)
698{
699 return le64_to_cpu(s->root);
700}
701
702static inline void btrfs_set_super_root(struct btrfs_super_block *s, u64 val)
703{
704 s->root = cpu_to_le64(val);
705}
706
707static inline u64 btrfs_super_total_blocks(struct btrfs_super_block *s)
708{
709 return le64_to_cpu(s->total_blocks);
710}
711
712static inline void btrfs_set_super_total_blocks(struct btrfs_super_block *s,
713 u64 val)
714{
715 s->total_blocks = cpu_to_le64(val);
716}
717
718static inline u64 btrfs_super_blocks_used(struct btrfs_super_block *s)
719{
720 return le64_to_cpu(s->blocks_used);
721}
722
723static inline void btrfs_set_super_blocks_used(struct btrfs_super_block *s,
724 u64 val)
725{
726 s->blocks_used = cpu_to_le64(val);
727}
728
Chris Mason123abc82007-03-14 14:14:43 -0400729static inline u32 btrfs_super_blocksize(struct btrfs_super_block *s)
Chris Mason3768f362007-03-13 16:47:54 -0400730{
Chris Mason123abc82007-03-14 14:14:43 -0400731 return le32_to_cpu(s->blocksize);
Chris Mason3768f362007-03-13 16:47:54 -0400732}
733
734static inline void btrfs_set_super_blocksize(struct btrfs_super_block *s,
Chris Mason123abc82007-03-14 14:14:43 -0400735 u32 val)
Chris Mason3768f362007-03-13 16:47:54 -0400736{
Chris Mason123abc82007-03-14 14:14:43 -0400737 s->blocksize = cpu_to_le32(val);
738}
739
Chris Mason2e635a22007-03-21 11:12:56 -0400740static inline u64 btrfs_super_root_dir(struct btrfs_super_block *s)
741{
742 return le64_to_cpu(s->root_dir_objectid);
743}
744
745static inline void btrfs_set_super_root_dir(struct btrfs_super_block *s, u64
746 val)
747{
748 s->root_dir_objectid = cpu_to_le64(val);
749}
750
Chris Mason123abc82007-03-14 14:14:43 -0400751static inline u8 *btrfs_leaf_data(struct btrfs_leaf *l)
752{
753 return (u8 *)l->items;
Chris Mason3768f362007-03-13 16:47:54 -0400754}
Chris Mason9f5fae22007-03-20 14:38:32 -0400755
756static inline u64 btrfs_file_extent_disk_blocknr(struct btrfs_file_extent_item
757 *e)
758{
759 return le64_to_cpu(e->disk_blocknr);
760}
761
762static inline void btrfs_set_file_extent_disk_blocknr(struct
763 btrfs_file_extent_item
764 *e, u64 val)
765{
766 e->disk_blocknr = cpu_to_le64(val);
767}
768
Chris Mason71951f32007-03-27 09:16:29 -0400769static inline u64 btrfs_file_extent_generation(struct btrfs_file_extent_item *e)
770{
771 return le64_to_cpu(e->generation);
772}
773
774static inline void btrfs_set_file_extent_generation(struct
775 btrfs_file_extent_item *e,
776 u64 val)
777{
778 e->generation = cpu_to_le64(val);
779}
780
Chris Mason9f5fae22007-03-20 14:38:32 -0400781static inline u64 btrfs_file_extent_disk_num_blocks(struct
782 btrfs_file_extent_item *e)
783{
784 return le64_to_cpu(e->disk_num_blocks);
785}
786
787static inline void btrfs_set_file_extent_disk_num_blocks(struct
788 btrfs_file_extent_item
789 *e, u64 val)
790{
791 e->disk_num_blocks = cpu_to_le64(val);
792}
793
794static inline u64 btrfs_file_extent_offset(struct btrfs_file_extent_item *e)
795{
796 return le64_to_cpu(e->offset);
797}
798
799static inline void btrfs_set_file_extent_offset(struct btrfs_file_extent_item
800 *e, u64 val)
801{
802 e->offset = cpu_to_le64(val);
803}
804
805static inline u64 btrfs_file_extent_num_blocks(struct btrfs_file_extent_item
806 *e)
807{
808 return le64_to_cpu(e->num_blocks);
809}
810
811static inline void btrfs_set_file_extent_num_blocks(struct
812 btrfs_file_extent_item *e,
813 u64 val)
814{
815 e->num_blocks = cpu_to_le64(val);
816}
817
Chris Masone20d96d2007-03-22 12:13:20 -0400818static inline struct btrfs_root *btrfs_sb(struct super_block *sb)
819{
820 return sb->s_fs_info;
821}
822
Chris Mason4beb1b82007-03-14 10:31:29 -0400823/* helper function to cast into the data area of the leaf. */
824#define btrfs_item_ptr(leaf, slot, type) \
Chris Mason123abc82007-03-14 14:14:43 -0400825 ((type *)(btrfs_leaf_data(leaf) + \
826 btrfs_item_offset((leaf)->items + (slot))))
Chris Mason4beb1b82007-03-14 10:31:29 -0400827
Chris Masondee26a92007-03-26 16:00:06 -0400828/* extent-item.c */
Chris Masone20d96d2007-03-22 12:13:20 -0400829struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masone089f052007-03-16 16:20:31 -0400830 struct btrfs_root *root);
Chris Masondee26a92007-03-26 16:00:06 -0400831int btrfs_alloc_extent(struct btrfs_trans_handle *trans, struct btrfs_root
832 *root, u64 num_blocks, u64 search_start, u64
833 search_end, u64 owner, struct btrfs_key *ins);
Chris Masone089f052007-03-16 16:20:31 -0400834int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -0400835 struct buffer_head *buf);
Chris Masone089f052007-03-16 16:20:31 -0400836int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
837 *root, u64 blocknr, u64 num_blocks, int pin);
Chris Masondee26a92007-03-26 16:00:06 -0400838int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
839 btrfs_root *root);
840/* ctree.c */
Chris Masone089f052007-03-16 16:20:31 -0400841int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
842 *root, struct btrfs_key *key, struct btrfs_path *p, int
843 ins_len, int cow);
Chris Mason234b63a2007-03-13 10:46:10 -0400844void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p);
845void btrfs_init_path(struct btrfs_path *p);
Chris Masone089f052007-03-16 16:20:31 -0400846int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
847 struct btrfs_path *path);
848int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
849 *root, struct btrfs_key *key, void *data, u32 data_size);
850int btrfs_insert_empty_item(struct btrfs_trans_handle *trans, struct btrfs_root
851 *root, struct btrfs_path *path, struct btrfs_key
852 *cpu_key, u32 data_size);
Chris Mason234b63a2007-03-13 10:46:10 -0400853int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path);
Chris Mason123abc82007-03-14 14:14:43 -0400854int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf);
Chris Masone089f052007-03-16 16:20:31 -0400855int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -0400856 *root, struct buffer_head *snap);
Chris Masondee26a92007-03-26 16:00:06 -0400857/* root-item.c */
Chris Masone089f052007-03-16 16:20:31 -0400858int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
859 struct btrfs_key *key);
860int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
861 *root, struct btrfs_key *key, struct btrfs_root_item
862 *item);
863int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
864 *root, struct btrfs_key *key, struct btrfs_root_item
865 *item);
866int btrfs_find_last_root(struct btrfs_root *root, u64 objectid, struct
867 btrfs_root_item *item, struct btrfs_key *key);
Chris Masondee26a92007-03-26 16:00:06 -0400868/* dir-item.c */
Chris Masone089f052007-03-16 16:20:31 -0400869int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond5719762007-03-23 10:01:08 -0400870 *root, const char *name, int name_len, u64 dir, u64
Chris Masone089f052007-03-16 16:20:31 -0400871 objectid, u8 type);
872int btrfs_lookup_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -0400873 *root, struct btrfs_path *path, u64 dir,
874 const char *name, int name_len, int mod);
Chris Mason1d4f6402007-03-15 15:18:43 -0400875int btrfs_match_dir_item_name(struct btrfs_root *root, struct btrfs_path *path,
Chris Mason7f5c1512007-03-23 15:56:19 -0400876 const char *name, int name_len);
Chris Masondee26a92007-03-26 16:00:06 -0400877/* inode-map.c */
Chris Mason9f5fae22007-03-20 14:38:32 -0400878int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
879 struct btrfs_root *fs_root,
880 u64 dirid, u64 *objectid);
881int btrfs_insert_inode_map(struct btrfs_trans_handle *trans,
882 struct btrfs_root *root,
883 u64 objectid, struct btrfs_key *location);
884int btrfs_lookup_inode_map(struct btrfs_trans_handle *trans,
885 struct btrfs_root *root, struct btrfs_path *path,
886 u64 objectid, int mod);
Chris Masondee26a92007-03-26 16:00:06 -0400887/* inode-item.c */
Chris Mason293ffd52007-03-20 15:57:25 -0400888int btrfs_insert_inode(struct btrfs_trans_handle *trans, struct btrfs_root
889 *root, u64 objectid, struct btrfs_inode_item
890 *inode_item);
891int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root
892 *root, struct btrfs_path *path, u64 objectid, int mod);
Chris Masondee26a92007-03-26 16:00:06 -0400893
894/* file-item.c */
895int btrfs_alloc_file_extent(struct btrfs_trans_handle *trans,
896 struct btrfs_root *root,
897 u64 objectid, u64 offset,
898 u64 num_blocks, u64 hint_block,
899 u64 *result);
900int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
901 struct btrfs_root *root,
902 struct btrfs_path *path, u64 objectid,
Chris Mason9773a782007-03-27 11:26:26 -0400903 u64 blocknr, int mod);
Chris Masoneb60cea2007-02-02 09:18:22 -0500904#endif