blob: 1897f3a65b4fd5846c0bd55212bc1a5069e48eea [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 Mason79154b12007-03-22 15:59:16 -0400236 struct mutex trans_mutex;
Chris Masond561c022007-03-23 19:47:49 -0400237 struct mutex fs_mutex;
Chris Mason62e27492007-03-15 12:56:47 -0400238};
239
240/*
241 * in ram representation of the tree. extent_root is used for all allocations
242 * and for the extent tree extent_root root. current_insert is used
243 * only for the extent tree.
244 */
245struct btrfs_root {
Chris Masone20d96d2007-03-22 12:13:20 -0400246 struct buffer_head *node;
247 struct buffer_head *commit_root;
Chris Mason62e27492007-03-15 12:56:47 -0400248 struct btrfs_root_item root_item;
249 struct btrfs_key root_key;
Chris Mason9f5fae22007-03-20 14:38:32 -0400250 struct btrfs_fs_info *fs_info;
Chris Mason62e27492007-03-15 12:56:47 -0400251 u32 blocksize;
Chris Mason9f5fae22007-03-20 14:38:32 -0400252 int ref_cows;
253 u32 type;
Chris Mason62e27492007-03-15 12:56:47 -0400254};
255
Chris Mason62e27492007-03-15 12:56:47 -0400256/* the lower bits in the key flags defines the item type */
257#define BTRFS_KEY_TYPE_MAX 256
258#define BTRFS_KEY_TYPE_MASK (BTRFS_KEY_TYPE_MAX - 1)
Chris Mason1e1d2702007-03-15 19:03:33 -0400259
260/*
261 * inode items have the data typically returned from stat and store other
262 * info about object characteristics. There is one for every file and dir in
263 * the FS
264 */
Chris Mason62e27492007-03-15 12:56:47 -0400265#define BTRFS_INODE_ITEM_KEY 1
Chris Mason1e1d2702007-03-15 19:03:33 -0400266
267/*
268 * dir items are the name -> inode pointers in a directory. There is one
269 * for every name in a directory.
270 */
Chris Mason62e27492007-03-15 12:56:47 -0400271#define BTRFS_DIR_ITEM_KEY 2
Chris Mason1e1d2702007-03-15 19:03:33 -0400272/*
273 * inline data is file data that fits in the btree.
274 */
275#define BTRFS_INLINE_DATA_KEY 3
276/*
277 * extent data is for data that can't fit in the btree. It points to
278 * a (hopefully) huge chunk of disk
279 */
280#define BTRFS_EXTENT_DATA_KEY 4
281/*
282 * root items point to tree roots. There are typically in the root
283 * tree used by the super block to find all the other trees
284 */
285#define BTRFS_ROOT_ITEM_KEY 5
286/*
287 * extent items are in the extent map tree. These record which blocks
288 * are used, and how many references there are to each block
289 */
290#define BTRFS_EXTENT_ITEM_KEY 6
Chris Mason9f5fae22007-03-20 14:38:32 -0400291
292/*
293 * the inode map records which inode numbers are in use and where
294 * they actually live on disk
295 */
296#define BTRFS_INODE_MAP_ITEM_KEY 7
Chris Mason1e1d2702007-03-15 19:03:33 -0400297/*
298 * string items are for debugging. They just store a short string of
299 * data in the FS
300 */
Chris Mason9f5fae22007-03-20 14:38:32 -0400301#define BTRFS_STRING_ITEM_KEY 8
Chris Mason1e1d2702007-03-15 19:03:33 -0400302
303static inline u64 btrfs_inode_generation(struct btrfs_inode_item *i)
304{
305 return le64_to_cpu(i->generation);
306}
307
308static inline void btrfs_set_inode_generation(struct btrfs_inode_item *i,
309 u64 val)
310{
311 i->generation = cpu_to_le64(val);
312}
313
314static inline u64 btrfs_inode_size(struct btrfs_inode_item *i)
315{
316 return le64_to_cpu(i->size);
317}
318
319static inline void btrfs_set_inode_size(struct btrfs_inode_item *i, u64 val)
320{
321 i->size = cpu_to_le64(val);
322}
323
324static inline u64 btrfs_inode_nblocks(struct btrfs_inode_item *i)
325{
326 return le64_to_cpu(i->nblocks);
327}
328
329static inline void btrfs_set_inode_nblocks(struct btrfs_inode_item *i, u64 val)
330{
331 i->nblocks = cpu_to_le64(val);
332}
333
334static inline u32 btrfs_inode_nlink(struct btrfs_inode_item *i)
335{
336 return le32_to_cpu(i->nlink);
337}
338
339static inline void btrfs_set_inode_nlink(struct btrfs_inode_item *i, u32 val)
340{
341 i->nlink = cpu_to_le32(val);
342}
343
344static inline u32 btrfs_inode_uid(struct btrfs_inode_item *i)
345{
346 return le32_to_cpu(i->uid);
347}
348
349static inline void btrfs_set_inode_uid(struct btrfs_inode_item *i, u32 val)
350{
351 i->uid = cpu_to_le32(val);
352}
353
354static inline u32 btrfs_inode_gid(struct btrfs_inode_item *i)
355{
356 return le32_to_cpu(i->gid);
357}
358
359static inline void btrfs_set_inode_gid(struct btrfs_inode_item *i, u32 val)
360{
361 i->gid = cpu_to_le32(val);
362}
363
364static inline u32 btrfs_inode_mode(struct btrfs_inode_item *i)
365{
366 return le32_to_cpu(i->mode);
367}
368
369static inline void btrfs_set_inode_mode(struct btrfs_inode_item *i, u32 val)
370{
371 i->mode = cpu_to_le32(val);
372}
373
374static inline u32 btrfs_inode_rdev(struct btrfs_inode_item *i)
375{
376 return le32_to_cpu(i->rdev);
377}
378
379static inline void btrfs_set_inode_rdev(struct btrfs_inode_item *i, u32 val)
380{
381 i->rdev = cpu_to_le32(val);
382}
383
384static inline u16 btrfs_inode_flags(struct btrfs_inode_item *i)
385{
386 return le16_to_cpu(i->flags);
387}
388
389static inline void btrfs_set_inode_flags(struct btrfs_inode_item *i, u16 val)
390{
391 i->flags = cpu_to_le16(val);
392}
393
394static inline u16 btrfs_inode_compat_flags(struct btrfs_inode_item *i)
395{
396 return le16_to_cpu(i->compat_flags);
397}
398
399static inline void btrfs_set_inode_compat_flags(struct btrfs_inode_item *i,
400 u16 val)
401{
402 i->compat_flags = cpu_to_le16(val);
403}
404
Chris Masone20d96d2007-03-22 12:13:20 -0400405static inline u32 btrfs_timespec_sec(struct btrfs_inode_timespec *ts)
406{
407 return le32_to_cpu(ts->sec);
408}
409
410static inline void btrfs_set_timespec_sec(struct btrfs_inode_timespec *ts,
411 u32 val)
412{
413 ts->sec = cpu_to_le32(val);
414}
415
416static inline u32 btrfs_timespec_nsec(struct btrfs_inode_timespec *ts)
417{
418 return le32_to_cpu(ts->nsec);
419}
420
421static inline void btrfs_set_timespec_nsec(struct btrfs_inode_timespec *ts,
422 u32 val)
423{
424 ts->nsec = cpu_to_le32(val);
425}
426
427
Chris Mason62e27492007-03-15 12:56:47 -0400428
Chris Mason234b63a2007-03-13 10:46:10 -0400429static inline u64 btrfs_extent_owner(struct btrfs_extent_item *ei)
Chris Masoncf27e1e2007-03-13 09:49:06 -0400430{
431 return le64_to_cpu(ei->owner);
432}
433
Chris Mason234b63a2007-03-13 10:46:10 -0400434static inline void btrfs_set_extent_owner(struct btrfs_extent_item *ei, u64 val)
Chris Masoncf27e1e2007-03-13 09:49:06 -0400435{
436 ei->owner = cpu_to_le64(val);
437}
438
Chris Mason234b63a2007-03-13 10:46:10 -0400439static inline u32 btrfs_extent_refs(struct btrfs_extent_item *ei)
Chris Masoncf27e1e2007-03-13 09:49:06 -0400440{
441 return le32_to_cpu(ei->refs);
442}
443
Chris Mason234b63a2007-03-13 10:46:10 -0400444static inline void btrfs_set_extent_refs(struct btrfs_extent_item *ei, u32 val)
Chris Masoncf27e1e2007-03-13 09:49:06 -0400445{
446 ei->refs = cpu_to_le32(val);
447}
448
Chris Mason234b63a2007-03-13 10:46:10 -0400449static inline u64 btrfs_node_blockptr(struct btrfs_node *n, int nr)
Chris Mason1d4f8a02007-03-13 09:28:32 -0400450{
Chris Mason123abc82007-03-14 14:14:43 -0400451 return le64_to_cpu(n->ptrs[nr].blockptr);
Chris Mason1d4f8a02007-03-13 09:28:32 -0400452}
453
Chris Mason234b63a2007-03-13 10:46:10 -0400454static inline void btrfs_set_node_blockptr(struct btrfs_node *n, int nr,
455 u64 val)
Chris Mason1d4f8a02007-03-13 09:28:32 -0400456{
Chris Mason123abc82007-03-14 14:14:43 -0400457 n->ptrs[nr].blockptr = cpu_to_le64(val);
Chris Mason1d4f8a02007-03-13 09:28:32 -0400458}
459
Chris Mason123abc82007-03-14 14:14:43 -0400460static inline u32 btrfs_item_offset(struct btrfs_item *item)
Chris Mason0783fcf2007-03-12 20:12:07 -0400461{
Chris Mason123abc82007-03-14 14:14:43 -0400462 return le32_to_cpu(item->offset);
Chris Mason0783fcf2007-03-12 20:12:07 -0400463}
464
Chris Mason123abc82007-03-14 14:14:43 -0400465static inline void btrfs_set_item_offset(struct btrfs_item *item, u32 val)
Chris Mason0783fcf2007-03-12 20:12:07 -0400466{
Chris Mason123abc82007-03-14 14:14:43 -0400467 item->offset = cpu_to_le32(val);
Chris Mason0783fcf2007-03-12 20:12:07 -0400468}
469
Chris Mason123abc82007-03-14 14:14:43 -0400470static inline u32 btrfs_item_end(struct btrfs_item *item)
Chris Mason0783fcf2007-03-12 20:12:07 -0400471{
Chris Mason123abc82007-03-14 14:14:43 -0400472 return le32_to_cpu(item->offset) + le16_to_cpu(item->size);
Chris Mason0783fcf2007-03-12 20:12:07 -0400473}
474
475static inline u16 btrfs_item_size(struct btrfs_item *item)
476{
477 return le16_to_cpu(item->size);
478}
479
480static inline void btrfs_set_item_size(struct btrfs_item *item, u16 val)
481{
482 item->size = cpu_to_le16(val);
483}
484
Chris Mason1d4f6402007-03-15 15:18:43 -0400485static inline u64 btrfs_dir_objectid(struct btrfs_dir_item *d)
486{
487 return le64_to_cpu(d->objectid);
488}
489
490static inline void btrfs_set_dir_objectid(struct btrfs_dir_item *d, u64 val)
491{
492 d->objectid = cpu_to_le64(val);
493}
494
495static inline u16 btrfs_dir_flags(struct btrfs_dir_item *d)
496{
497 return le16_to_cpu(d->flags);
498}
499
500static inline void btrfs_set_dir_flags(struct btrfs_dir_item *d, u16 val)
501{
502 d->flags = cpu_to_le16(val);
503}
504
505static inline u8 btrfs_dir_type(struct btrfs_dir_item *d)
506{
507 return d->type;
508}
509
510static inline void btrfs_set_dir_type(struct btrfs_dir_item *d, u8 val)
511{
512 d->type = val;
513}
514
Chris Masona8a2ee02007-03-16 08:46:49 -0400515static inline u16 btrfs_dir_name_len(struct btrfs_dir_item *d)
Chris Mason1d4f6402007-03-15 15:18:43 -0400516{
Chris Masona8a2ee02007-03-16 08:46:49 -0400517 return le16_to_cpu(d->name_len);
518}
519
520static inline void btrfs_set_dir_name_len(struct btrfs_dir_item *d, u16 val)
521{
522 d->name_len = cpu_to_le16(val);
Chris Mason1d4f6402007-03-15 15:18:43 -0400523}
524
Chris Masone2fa7222007-03-12 16:22:34 -0400525static inline void btrfs_disk_key_to_cpu(struct btrfs_key *cpu,
526 struct btrfs_disk_key *disk)
527{
528 cpu->offset = le64_to_cpu(disk->offset);
529 cpu->flags = le32_to_cpu(disk->flags);
530 cpu->objectid = le64_to_cpu(disk->objectid);
531}
532
533static inline void btrfs_cpu_key_to_disk(struct btrfs_disk_key *disk,
534 struct btrfs_key *cpu)
535{
536 disk->offset = cpu_to_le64(cpu->offset);
537 disk->flags = cpu_to_le32(cpu->flags);
538 disk->objectid = cpu_to_le64(cpu->objectid);
539}
540
Chris Mason62e27492007-03-15 12:56:47 -0400541static inline u64 btrfs_disk_key_objectid(struct btrfs_disk_key *disk)
Chris Masone2fa7222007-03-12 16:22:34 -0400542{
543 return le64_to_cpu(disk->objectid);
544}
545
Chris Mason62e27492007-03-15 12:56:47 -0400546static inline void btrfs_set_disk_key_objectid(struct btrfs_disk_key *disk,
547 u64 val)
Chris Masone2fa7222007-03-12 16:22:34 -0400548{
549 disk->objectid = cpu_to_le64(val);
550}
551
Chris Mason62e27492007-03-15 12:56:47 -0400552static inline u64 btrfs_disk_key_offset(struct btrfs_disk_key *disk)
Chris Masone2fa7222007-03-12 16:22:34 -0400553{
554 return le64_to_cpu(disk->offset);
555}
556
Chris Mason62e27492007-03-15 12:56:47 -0400557static inline void btrfs_set_disk_key_offset(struct btrfs_disk_key *disk,
558 u64 val)
Chris Masone2fa7222007-03-12 16:22:34 -0400559{
560 disk->offset = cpu_to_le64(val);
561}
562
Chris Mason62e27492007-03-15 12:56:47 -0400563static inline u32 btrfs_disk_key_flags(struct btrfs_disk_key *disk)
Chris Masone2fa7222007-03-12 16:22:34 -0400564{
565 return le32_to_cpu(disk->flags);
566}
567
Chris Mason62e27492007-03-15 12:56:47 -0400568static inline void btrfs_set_disk_key_flags(struct btrfs_disk_key *disk,
569 u32 val)
Chris Masone2fa7222007-03-12 16:22:34 -0400570{
571 disk->flags = cpu_to_le32(val);
572}
573
Chris Mason62e27492007-03-15 12:56:47 -0400574static inline u32 btrfs_key_type(struct btrfs_key *key)
575{
576 return key->flags & BTRFS_KEY_TYPE_MASK;
577}
578
579static inline u32 btrfs_disk_key_type(struct btrfs_disk_key *key)
580{
581 return le32_to_cpu(key->flags) & BTRFS_KEY_TYPE_MASK;
582}
583
584static inline void btrfs_set_key_type(struct btrfs_key *key, u32 type)
585{
586 BUG_ON(type >= BTRFS_KEY_TYPE_MAX);
587 key->flags = (key->flags & ~((u64)BTRFS_KEY_TYPE_MASK)) | type;
588}
589
590static inline void btrfs_set_disk_key_type(struct btrfs_disk_key *key, u32 type)
591{
592 u32 flags = btrfs_disk_key_flags(key);
593 BUG_ON(type >= BTRFS_KEY_TYPE_MAX);
594 flags = (flags & ~((u64)BTRFS_KEY_TYPE_MASK)) | type;
595 btrfs_set_disk_key_flags(key, flags);
596}
597
Chris Masonbb492bb2007-03-12 12:29:44 -0400598static inline u64 btrfs_header_blocknr(struct btrfs_header *h)
Chris Mason7518a232007-03-12 12:01:18 -0400599{
Chris Masonbb492bb2007-03-12 12:29:44 -0400600 return le64_to_cpu(h->blocknr);
Chris Mason7518a232007-03-12 12:01:18 -0400601}
602
Chris Masonbb492bb2007-03-12 12:29:44 -0400603static inline void btrfs_set_header_blocknr(struct btrfs_header *h, u64 blocknr)
Chris Mason7518a232007-03-12 12:01:18 -0400604{
Chris Masonbb492bb2007-03-12 12:29:44 -0400605 h->blocknr = cpu_to_le64(blocknr);
Chris Mason7518a232007-03-12 12:01:18 -0400606}
607
Chris Mason7f5c1512007-03-23 15:56:19 -0400608static inline u64 btrfs_header_generation(struct btrfs_header *h)
609{
610 return le64_to_cpu(h->generation);
611}
612
613static inline void btrfs_set_header_generation(struct btrfs_header *h,
614 u64 val)
615{
616 h->generation = cpu_to_le64(val);
617}
618
Chris Masonbb492bb2007-03-12 12:29:44 -0400619static inline u64 btrfs_header_parentid(struct btrfs_header *h)
Chris Mason7518a232007-03-12 12:01:18 -0400620{
Chris Masonbb492bb2007-03-12 12:29:44 -0400621 return le64_to_cpu(h->parentid);
Chris Mason7518a232007-03-12 12:01:18 -0400622}
623
Chris Masonbb492bb2007-03-12 12:29:44 -0400624static inline void btrfs_set_header_parentid(struct btrfs_header *h,
625 u64 parentid)
Chris Mason7518a232007-03-12 12:01:18 -0400626{
Chris Masonbb492bb2007-03-12 12:29:44 -0400627 h->parentid = cpu_to_le64(parentid);
Chris Mason7518a232007-03-12 12:01:18 -0400628}
629
Chris Masonbb492bb2007-03-12 12:29:44 -0400630static inline u16 btrfs_header_nritems(struct btrfs_header *h)
Chris Mason7518a232007-03-12 12:01:18 -0400631{
Chris Masonbb492bb2007-03-12 12:29:44 -0400632 return le16_to_cpu(h->nritems);
Chris Mason7518a232007-03-12 12:01:18 -0400633}
634
Chris Masonbb492bb2007-03-12 12:29:44 -0400635static inline void btrfs_set_header_nritems(struct btrfs_header *h, u16 val)
Chris Mason7518a232007-03-12 12:01:18 -0400636{
Chris Masonbb492bb2007-03-12 12:29:44 -0400637 h->nritems = cpu_to_le16(val);
Chris Mason7518a232007-03-12 12:01:18 -0400638}
639
Chris Masonbb492bb2007-03-12 12:29:44 -0400640static inline u16 btrfs_header_flags(struct btrfs_header *h)
Chris Mason7518a232007-03-12 12:01:18 -0400641{
Chris Masonbb492bb2007-03-12 12:29:44 -0400642 return le16_to_cpu(h->flags);
Chris Mason7518a232007-03-12 12:01:18 -0400643}
644
Chris Masonbb492bb2007-03-12 12:29:44 -0400645static inline void btrfs_set_header_flags(struct btrfs_header *h, u16 val)
Chris Mason7518a232007-03-12 12:01:18 -0400646{
Chris Masonbb492bb2007-03-12 12:29:44 -0400647 h->flags = cpu_to_le16(val);
Chris Mason7518a232007-03-12 12:01:18 -0400648}
649
Chris Masonbb492bb2007-03-12 12:29:44 -0400650static inline int btrfs_header_level(struct btrfs_header *h)
Chris Mason7518a232007-03-12 12:01:18 -0400651{
Chris Mason9a6f11e2007-03-27 09:06:38 -0400652 return h->level;
Chris Mason7518a232007-03-12 12:01:18 -0400653}
654
Chris Masonbb492bb2007-03-12 12:29:44 -0400655static inline void btrfs_set_header_level(struct btrfs_header *h, int level)
Chris Mason7518a232007-03-12 12:01:18 -0400656{
Chris Mason234b63a2007-03-13 10:46:10 -0400657 BUG_ON(level > BTRFS_MAX_LEVEL);
Chris Mason9a6f11e2007-03-27 09:06:38 -0400658 h->level = level;
Chris Mason7518a232007-03-12 12:01:18 -0400659}
660
Chris Mason234b63a2007-03-13 10:46:10 -0400661static inline int btrfs_is_leaf(struct btrfs_node *n)
Chris Mason7518a232007-03-12 12:01:18 -0400662{
663 return (btrfs_header_level(&n->header) == 0);
664}
665
Chris Mason3768f362007-03-13 16:47:54 -0400666static inline u64 btrfs_root_blocknr(struct btrfs_root_item *item)
667{
668 return le64_to_cpu(item->blocknr);
669}
670
671static inline void btrfs_set_root_blocknr(struct btrfs_root_item *item, u64 val)
672{
673 item->blocknr = cpu_to_le64(val);
674}
675
676static inline u32 btrfs_root_refs(struct btrfs_root_item *item)
677{
678 return le32_to_cpu(item->refs);
679}
680
681static inline void btrfs_set_root_refs(struct btrfs_root_item *item, u32 val)
682{
683 item->refs = cpu_to_le32(val);
684}
685
686static inline u64 btrfs_super_blocknr(struct btrfs_super_block *s)
687{
688 return le64_to_cpu(s->blocknr);
689}
690
691static inline void btrfs_set_super_blocknr(struct btrfs_super_block *s, u64 val)
692{
693 s->blocknr = cpu_to_le64(val);
694}
695
696static inline u64 btrfs_super_root(struct btrfs_super_block *s)
697{
698 return le64_to_cpu(s->root);
699}
700
701static inline void btrfs_set_super_root(struct btrfs_super_block *s, u64 val)
702{
703 s->root = cpu_to_le64(val);
704}
705
706static inline u64 btrfs_super_total_blocks(struct btrfs_super_block *s)
707{
708 return le64_to_cpu(s->total_blocks);
709}
710
711static inline void btrfs_set_super_total_blocks(struct btrfs_super_block *s,
712 u64 val)
713{
714 s->total_blocks = cpu_to_le64(val);
715}
716
717static inline u64 btrfs_super_blocks_used(struct btrfs_super_block *s)
718{
719 return le64_to_cpu(s->blocks_used);
720}
721
722static inline void btrfs_set_super_blocks_used(struct btrfs_super_block *s,
723 u64 val)
724{
725 s->blocks_used = cpu_to_le64(val);
726}
727
Chris Mason123abc82007-03-14 14:14:43 -0400728static inline u32 btrfs_super_blocksize(struct btrfs_super_block *s)
Chris Mason3768f362007-03-13 16:47:54 -0400729{
Chris Mason123abc82007-03-14 14:14:43 -0400730 return le32_to_cpu(s->blocksize);
Chris Mason3768f362007-03-13 16:47:54 -0400731}
732
733static inline void btrfs_set_super_blocksize(struct btrfs_super_block *s,
Chris Mason123abc82007-03-14 14:14:43 -0400734 u32 val)
Chris Mason3768f362007-03-13 16:47:54 -0400735{
Chris Mason123abc82007-03-14 14:14:43 -0400736 s->blocksize = cpu_to_le32(val);
737}
738
Chris Mason2e635a22007-03-21 11:12:56 -0400739static inline u64 btrfs_super_root_dir(struct btrfs_super_block *s)
740{
741 return le64_to_cpu(s->root_dir_objectid);
742}
743
744static inline void btrfs_set_super_root_dir(struct btrfs_super_block *s, u64
745 val)
746{
747 s->root_dir_objectid = cpu_to_le64(val);
748}
749
Chris Mason123abc82007-03-14 14:14:43 -0400750static inline u8 *btrfs_leaf_data(struct btrfs_leaf *l)
751{
752 return (u8 *)l->items;
Chris Mason3768f362007-03-13 16:47:54 -0400753}
Chris Mason9f5fae22007-03-20 14:38:32 -0400754
755static inline u64 btrfs_file_extent_disk_blocknr(struct btrfs_file_extent_item
756 *e)
757{
758 return le64_to_cpu(e->disk_blocknr);
759}
760
761static inline void btrfs_set_file_extent_disk_blocknr(struct
762 btrfs_file_extent_item
763 *e, u64 val)
764{
765 e->disk_blocknr = cpu_to_le64(val);
766}
767
Chris Mason71951f32007-03-27 09:16:29 -0400768static inline u64 btrfs_file_extent_generation(struct btrfs_file_extent_item *e)
769{
770 return le64_to_cpu(e->generation);
771}
772
773static inline void btrfs_set_file_extent_generation(struct
774 btrfs_file_extent_item *e,
775 u64 val)
776{
777 e->generation = cpu_to_le64(val);
778}
779
Chris Mason9f5fae22007-03-20 14:38:32 -0400780static inline u64 btrfs_file_extent_disk_num_blocks(struct
781 btrfs_file_extent_item *e)
782{
783 return le64_to_cpu(e->disk_num_blocks);
784}
785
786static inline void btrfs_set_file_extent_disk_num_blocks(struct
787 btrfs_file_extent_item
788 *e, u64 val)
789{
790 e->disk_num_blocks = cpu_to_le64(val);
791}
792
793static inline u64 btrfs_file_extent_offset(struct btrfs_file_extent_item *e)
794{
795 return le64_to_cpu(e->offset);
796}
797
798static inline void btrfs_set_file_extent_offset(struct btrfs_file_extent_item
799 *e, u64 val)
800{
801 e->offset = cpu_to_le64(val);
802}
803
804static inline u64 btrfs_file_extent_num_blocks(struct btrfs_file_extent_item
805 *e)
806{
807 return le64_to_cpu(e->num_blocks);
808}
809
810static inline void btrfs_set_file_extent_num_blocks(struct
811 btrfs_file_extent_item *e,
812 u64 val)
813{
814 e->num_blocks = cpu_to_le64(val);
815}
816
Chris Masone20d96d2007-03-22 12:13:20 -0400817static inline struct btrfs_root *btrfs_sb(struct super_block *sb)
818{
819 return sb->s_fs_info;
820}
821
Chris Mason4beb1b82007-03-14 10:31:29 -0400822/* helper function to cast into the data area of the leaf. */
823#define btrfs_item_ptr(leaf, slot, type) \
Chris Mason123abc82007-03-14 14:14:43 -0400824 ((type *)(btrfs_leaf_data(leaf) + \
825 btrfs_item_offset((leaf)->items + (slot))))
Chris Mason4beb1b82007-03-14 10:31:29 -0400826
Chris Masondee26a92007-03-26 16:00:06 -0400827/* extent-item.c */
Chris Masone20d96d2007-03-22 12:13:20 -0400828struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masone089f052007-03-16 16:20:31 -0400829 struct btrfs_root *root);
Chris Masondee26a92007-03-26 16:00:06 -0400830int btrfs_alloc_extent(struct btrfs_trans_handle *trans, struct btrfs_root
831 *root, u64 num_blocks, u64 search_start, u64
832 search_end, u64 owner, struct btrfs_key *ins);
Chris Masone089f052007-03-16 16:20:31 -0400833int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -0400834 struct buffer_head *buf);
Chris Masone089f052007-03-16 16:20:31 -0400835int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
836 *root, u64 blocknr, u64 num_blocks, int pin);
Chris Masondee26a92007-03-26 16:00:06 -0400837int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
838 btrfs_root *root);
839/* ctree.c */
Chris Masone089f052007-03-16 16:20:31 -0400840int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
841 *root, struct btrfs_key *key, struct btrfs_path *p, int
842 ins_len, int cow);
Chris Mason234b63a2007-03-13 10:46:10 -0400843void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p);
844void btrfs_init_path(struct btrfs_path *p);
Chris Masone089f052007-03-16 16:20:31 -0400845int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
846 struct btrfs_path *path);
847int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
848 *root, struct btrfs_key *key, void *data, u32 data_size);
849int btrfs_insert_empty_item(struct btrfs_trans_handle *trans, struct btrfs_root
850 *root, struct btrfs_path *path, struct btrfs_key
851 *cpu_key, u32 data_size);
Chris Mason234b63a2007-03-13 10:46:10 -0400852int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path);
Chris Mason123abc82007-03-14 14:14:43 -0400853int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf);
Chris Masone089f052007-03-16 16:20:31 -0400854int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -0400855 *root, struct buffer_head *snap);
Chris Masondee26a92007-03-26 16:00:06 -0400856/* root-item.c */
Chris Masone089f052007-03-16 16:20:31 -0400857int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
858 struct btrfs_key *key);
859int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
860 *root, struct btrfs_key *key, struct btrfs_root_item
861 *item);
862int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
863 *root, struct btrfs_key *key, struct btrfs_root_item
864 *item);
865int btrfs_find_last_root(struct btrfs_root *root, u64 objectid, struct
866 btrfs_root_item *item, struct btrfs_key *key);
Chris Masondee26a92007-03-26 16:00:06 -0400867/* dir-item.c */
Chris Masone089f052007-03-16 16:20:31 -0400868int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond5719762007-03-23 10:01:08 -0400869 *root, const char *name, int name_len, u64 dir, u64
Chris Masone089f052007-03-16 16:20:31 -0400870 objectid, u8 type);
871int btrfs_lookup_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -0400872 *root, struct btrfs_path *path, u64 dir,
873 const char *name, int name_len, int mod);
Chris Mason1d4f6402007-03-15 15:18:43 -0400874int btrfs_match_dir_item_name(struct btrfs_root *root, struct btrfs_path *path,
Chris Mason7f5c1512007-03-23 15:56:19 -0400875 const char *name, int name_len);
Chris Masondee26a92007-03-26 16:00:06 -0400876/* inode-map.c */
Chris Mason9f5fae22007-03-20 14:38:32 -0400877int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
878 struct btrfs_root *fs_root,
879 u64 dirid, u64 *objectid);
880int btrfs_insert_inode_map(struct btrfs_trans_handle *trans,
881 struct btrfs_root *root,
882 u64 objectid, struct btrfs_key *location);
883int btrfs_lookup_inode_map(struct btrfs_trans_handle *trans,
884 struct btrfs_root *root, struct btrfs_path *path,
885 u64 objectid, int mod);
Chris Masondee26a92007-03-26 16:00:06 -0400886/* inode-item.c */
Chris Mason293ffd52007-03-20 15:57:25 -0400887int btrfs_insert_inode(struct btrfs_trans_handle *trans, struct btrfs_root
888 *root, u64 objectid, struct btrfs_inode_item
889 *inode_item);
890int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root
891 *root, struct btrfs_path *path, u64 objectid, int mod);
Chris Masondee26a92007-03-26 16:00:06 -0400892
893/* file-item.c */
894int btrfs_alloc_file_extent(struct btrfs_trans_handle *trans,
895 struct btrfs_root *root,
896 u64 objectid, u64 offset,
897 u64 num_blocks, u64 hint_block,
898 u64 *result);
899int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
900 struct btrfs_root *root,
901 struct btrfs_path *path, u64 objectid,
Chris Mason9773a782007-03-27 11:26:26 -0400902 u64 blocknr, int mod);
Chris Masoneb60cea2007-02-02 09:18:22 -0500903#endif