blob: 68cafae6a85045cddcf7958a4999d2174e2686dd [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/radix-tree.h>
5#include <linux/fs.h>
6
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 Mason3768f362007-03-13 16:47:54 -040012#define BTRFS_ROOT_TREE_OBJECTID 1
13#define BTRFS_EXTENT_TREE_OBJECTID 2
Chris Mason9f5fae22007-03-20 14:38:32 -040014#define BTRFS_INODE_MAP_OBJECTID 3
15#define BTRFS_FS_TREE_OBJECTID 4
Chris Mason3768f362007-03-13 16:47:54 -040016
Chris Masonfec577f2007-02-26 10:40:21 -050017/*
Chris Masone20d96d2007-03-22 12:13:20 -040018 * we can actually store much bigger names, but lets not confuse the rest
19 * of linux
20 */
21#define BTRFS_NAME_LEN 255
22
23/*
Chris Masonfec577f2007-02-26 10:40:21 -050024 * the key defines the order in the tree, and so it also defines (optimal)
25 * block layout. objectid corresonds to the inode number. The flags
26 * tells us things about the object, and is a kind of stream selector.
27 * so for a given inode, keys with flags of 1 might refer to the inode
28 * data, flags of 2 may point to file data in the btree and flags == 3
29 * may point to extents.
30 *
31 * offset is the starting byte offset for this key in the stream.
Chris Masone2fa7222007-03-12 16:22:34 -040032 *
33 * btrfs_disk_key is in disk byte order. struct btrfs_key is always
34 * in cpu native order. Otherwise they are identical and their sizes
35 * should be the same (ie both packed)
Chris Masonfec577f2007-02-26 10:40:21 -050036 */
Chris Masone2fa7222007-03-12 16:22:34 -040037struct btrfs_disk_key {
38 __le64 objectid;
Chris Masona1516c82007-03-14 14:26:53 -040039 __le32 flags;
Chris Masona8a2ee02007-03-16 08:46:49 -040040 __le64 offset;
Chris Masone2fa7222007-03-12 16:22:34 -040041} __attribute__ ((__packed__));
42
43struct btrfs_key {
Chris Masoneb60cea2007-02-02 09:18:22 -050044 u64 objectid;
Chris Masona1516c82007-03-14 14:26:53 -040045 u32 flags;
Chris Masona8a2ee02007-03-16 08:46:49 -040046 u64 offset;
Chris Masoneb60cea2007-02-02 09:18:22 -050047} __attribute__ ((__packed__));
48
Chris Masonfec577f2007-02-26 10:40:21 -050049/*
50 * every tree block (leaf or node) starts with this header.
51 */
Chris Masonbb492bb2007-03-12 12:29:44 -040052struct btrfs_header {
Chris Mason3768f362007-03-13 16:47:54 -040053 u8 fsid[16]; /* FS specific uuid */
Chris Masonbb492bb2007-03-12 12:29:44 -040054 __le64 blocknr; /* which block this node is supposed to live in */
Chris Mason7f5c1512007-03-23 15:56:19 -040055 __le64 generation;
Chris Masonbb492bb2007-03-12 12:29:44 -040056 __le64 parentid; /* objectid of the tree root */
57 __le32 csum;
58 __le32 ham;
59 __le16 nritems;
60 __le16 flags;
Chris Masonfec577f2007-02-26 10:40:21 -050061 /* generation flags to be added */
Chris Masoneb60cea2007-02-02 09:18:22 -050062} __attribute__ ((__packed__));
63
Chris Mason234b63a2007-03-13 10:46:10 -040064#define BTRFS_MAX_LEVEL 8
Chris Mason123abc82007-03-14 14:14:43 -040065#define BTRFS_NODEPTRS_PER_BLOCK(r) (((r)->blocksize - \
66 sizeof(struct btrfs_header)) / \
67 (sizeof(struct btrfs_disk_key) + sizeof(u64)))
68#define __BTRFS_LEAF_DATA_SIZE(bs) ((bs) - sizeof(struct btrfs_header))
69#define BTRFS_LEAF_DATA_SIZE(r) (__BTRFS_LEAF_DATA_SIZE(r->blocksize))
Chris Masoneb60cea2007-02-02 09:18:22 -050070
Chris Masone20d96d2007-03-22 12:13:20 -040071struct buffer_head;
Chris Masonfec577f2007-02-26 10:40:21 -050072/*
Chris Masonfec577f2007-02-26 10:40:21 -050073 * the super block basically lists the main trees of the FS
74 * it currently lacks any block count etc etc
75 */
Chris Mason234b63a2007-03-13 10:46:10 -040076struct btrfs_super_block {
Chris Mason3768f362007-03-13 16:47:54 -040077 u8 fsid[16]; /* FS specific uuid */
78 __le64 blocknr; /* this block number */
79 __le32 csum;
80 __le64 magic;
Chris Mason123abc82007-03-14 14:14:43 -040081 __le32 blocksize;
Chris Mason3768f362007-03-13 16:47:54 -040082 __le64 generation;
83 __le64 root;
84 __le64 total_blocks;
85 __le64 blocks_used;
Chris Mason2e635a22007-03-21 11:12:56 -040086 __le64 root_dir_objectid;
Chris Masoncfaa7292007-02-21 17:04:57 -050087} __attribute__ ((__packed__));
88
Chris Masonfec577f2007-02-26 10:40:21 -050089/*
Chris Mason62e27492007-03-15 12:56:47 -040090 * A leaf is full of items. offset and size tell us where to find
Chris Masonfec577f2007-02-26 10:40:21 -050091 * the item in the leaf (relative to the start of the data area)
92 */
Chris Mason0783fcf2007-03-12 20:12:07 -040093struct btrfs_item {
Chris Masone2fa7222007-03-12 16:22:34 -040094 struct btrfs_disk_key key;
Chris Mason123abc82007-03-14 14:14:43 -040095 __le32 offset;
Chris Mason0783fcf2007-03-12 20:12:07 -040096 __le16 size;
Chris Masoneb60cea2007-02-02 09:18:22 -050097} __attribute__ ((__packed__));
98
Chris Masonfec577f2007-02-26 10:40:21 -050099/*
100 * leaves have an item area and a data area:
101 * [item0, item1....itemN] [free space] [dataN...data1, data0]
102 *
103 * The data is separate from the items to get the keys closer together
104 * during searches.
105 */
Chris Mason234b63a2007-03-13 10:46:10 -0400106struct btrfs_leaf {
Chris Masonbb492bb2007-03-12 12:29:44 -0400107 struct btrfs_header header;
Chris Mason123abc82007-03-14 14:14:43 -0400108 struct btrfs_item items[];
Chris Masoneb60cea2007-02-02 09:18:22 -0500109} __attribute__ ((__packed__));
110
Chris Masonfec577f2007-02-26 10:40:21 -0500111/*
112 * all non-leaf blocks are nodes, they hold only keys and pointers to
113 * other blocks
114 */
Chris Mason123abc82007-03-14 14:14:43 -0400115struct btrfs_key_ptr {
116 struct btrfs_disk_key key;
117 __le64 blockptr;
118} __attribute__ ((__packed__));
119
Chris Mason234b63a2007-03-13 10:46:10 -0400120struct btrfs_node {
Chris Masonbb492bb2007-03-12 12:29:44 -0400121 struct btrfs_header header;
Chris Mason123abc82007-03-14 14:14:43 -0400122 struct btrfs_key_ptr ptrs[];
Chris Masoneb60cea2007-02-02 09:18:22 -0500123} __attribute__ ((__packed__));
124
Chris Masonfec577f2007-02-26 10:40:21 -0500125/*
Chris Mason234b63a2007-03-13 10:46:10 -0400126 * btrfs_paths remember the path taken from the root down to the leaf.
127 * level 0 is always the leaf, and nodes[1...BTRFS_MAX_LEVEL] will point
Chris Masonfec577f2007-02-26 10:40:21 -0500128 * to any other levels that are present.
129 *
130 * The slots array records the index of the item or block pointer
131 * used while walking the tree.
132 */
Chris Mason234b63a2007-03-13 10:46:10 -0400133struct btrfs_path {
Chris Masone20d96d2007-03-22 12:13:20 -0400134 struct buffer_head *nodes[BTRFS_MAX_LEVEL];
Chris Mason234b63a2007-03-13 10:46:10 -0400135 int slots[BTRFS_MAX_LEVEL];
Chris Masoneb60cea2007-02-02 09:18:22 -0500136};
Chris Mason5de08d72007-02-24 06:24:44 -0500137
Chris Mason62e27492007-03-15 12:56:47 -0400138/*
139 * items in the extent btree are used to record the objectid of the
140 * owner of the block and the number of references
141 */
142struct btrfs_extent_item {
143 __le32 refs;
144 __le64 owner;
145} __attribute__ ((__packed__));
146
Chris Mason1e1d2702007-03-15 19:03:33 -0400147struct btrfs_inode_timespec {
148 __le32 sec;
149 __le32 nsec;
150} __attribute__ ((__packed__));
151
152/*
153 * there is no padding here on purpose. If you want to extent the inode,
154 * make a new item type
155 */
156struct btrfs_inode_item {
157 __le64 generation;
158 __le64 size;
159 __le64 nblocks;
160 __le32 nlink;
161 __le32 uid;
162 __le32 gid;
163 __le32 mode;
164 __le32 rdev;
165 __le16 flags;
166 __le16 compat_flags;
167 struct btrfs_inode_timespec atime;
168 struct btrfs_inode_timespec ctime;
169 struct btrfs_inode_timespec mtime;
170 struct btrfs_inode_timespec otime;
171} __attribute__ ((__packed__));
172
173/* inline data is just a blob of bytes */
174struct btrfs_inline_data_item {
175 u8 data;
176} __attribute__ ((__packed__));
177
Chris Mason62e27492007-03-15 12:56:47 -0400178struct btrfs_dir_item {
179 __le64 objectid;
180 __le16 flags;
Chris Masona8a2ee02007-03-16 08:46:49 -0400181 __le16 name_len;
Chris Mason62e27492007-03-15 12:56:47 -0400182 u8 type;
183} __attribute__ ((__packed__));
184
185struct btrfs_root_item {
186 __le64 blocknr;
187 __le32 flags;
188 __le64 block_limit;
189 __le64 blocks_used;
190 __le32 refs;
Chris Mason9f5fae22007-03-20 14:38:32 -0400191} __attribute__ ((__packed__));
192
193struct btrfs_file_extent_item {
194 /*
195 * disk space consumed by the extent, checksum blocks are included
196 * in these numbers
197 */
198 __le64 disk_blocknr;
199 __le64 disk_num_blocks;
200 /*
201 * the logical offset in file bytes (no csums)
202 * this extent record is for. This allows a file extent to point
203 * into the middle of an existing extent on disk, sharing it
204 * between two snapshots (useful if some bytes in the middle of the
205 * extent have changed
206 */
207 __le64 offset;
208 /*
209 * the logical number of file blocks (no csums included)
210 */
211 __le64 num_blocks;
212} __attribute__ ((__packed__));
213
214struct btrfs_inode_map_item {
215 struct btrfs_disk_key key;
216} __attribute__ ((__packed__));
217
218struct btrfs_fs_info {
219 struct btrfs_root *fs_root;
220 struct btrfs_root *extent_root;
221 struct btrfs_root *tree_root;
222 struct btrfs_root *inode_root;
223 struct btrfs_key current_insert;
224 struct btrfs_key last_insert;
Chris Mason9f5fae22007-03-20 14:38:32 -0400225 struct radix_tree_root pinned_radix;
Chris Mason9f5fae22007-03-20 14:38:32 -0400226 u64 last_inode_alloc;
227 u64 last_inode_alloc_dirid;
Chris Mason293ffd52007-03-20 15:57:25 -0400228 u64 generation;
Chris Mason79154b12007-03-22 15:59:16 -0400229 struct btrfs_transaction *running_transaction;
Chris Mason1261ec42007-03-20 20:35:03 -0400230 struct btrfs_super_block *disk_super;
Chris Masone20d96d2007-03-22 12:13:20 -0400231 struct buffer_head *sb_buffer;
232 struct super_block *sb;
Chris Mason79154b12007-03-22 15:59:16 -0400233 struct mutex trans_mutex;
Chris Masond561c022007-03-23 19:47:49 -0400234 struct mutex fs_mutex;
Chris Mason62e27492007-03-15 12:56:47 -0400235};
236
237/*
238 * in ram representation of the tree. extent_root is used for all allocations
239 * and for the extent tree extent_root root. current_insert is used
240 * only for the extent tree.
241 */
242struct btrfs_root {
Chris Masone20d96d2007-03-22 12:13:20 -0400243 struct buffer_head *node;
244 struct buffer_head *commit_root;
Chris Mason62e27492007-03-15 12:56:47 -0400245 struct btrfs_root_item root_item;
246 struct btrfs_key root_key;
Chris Mason9f5fae22007-03-20 14:38:32 -0400247 struct btrfs_fs_info *fs_info;
Chris Mason62e27492007-03-15 12:56:47 -0400248 u32 blocksize;
Chris Mason9f5fae22007-03-20 14:38:32 -0400249 int ref_cows;
250 u32 type;
Chris Mason62e27492007-03-15 12:56:47 -0400251};
252
Chris Mason62e27492007-03-15 12:56:47 -0400253/* the lower bits in the key flags defines the item type */
254#define BTRFS_KEY_TYPE_MAX 256
255#define BTRFS_KEY_TYPE_MASK (BTRFS_KEY_TYPE_MAX - 1)
Chris Mason1e1d2702007-03-15 19:03:33 -0400256
257/*
258 * inode items have the data typically returned from stat and store other
259 * info about object characteristics. There is one for every file and dir in
260 * the FS
261 */
Chris Mason62e27492007-03-15 12:56:47 -0400262#define BTRFS_INODE_ITEM_KEY 1
Chris Mason1e1d2702007-03-15 19:03:33 -0400263
264/*
265 * dir items are the name -> inode pointers in a directory. There is one
266 * for every name in a directory.
267 */
Chris Mason62e27492007-03-15 12:56:47 -0400268#define BTRFS_DIR_ITEM_KEY 2
Chris Mason1e1d2702007-03-15 19:03:33 -0400269/*
270 * inline data is file data that fits in the btree.
271 */
272#define BTRFS_INLINE_DATA_KEY 3
273/*
274 * extent data is for data that can't fit in the btree. It points to
275 * a (hopefully) huge chunk of disk
276 */
277#define BTRFS_EXTENT_DATA_KEY 4
278/*
279 * root items point to tree roots. There are typically in the root
280 * tree used by the super block to find all the other trees
281 */
282#define BTRFS_ROOT_ITEM_KEY 5
283/*
284 * extent items are in the extent map tree. These record which blocks
285 * are used, and how many references there are to each block
286 */
287#define BTRFS_EXTENT_ITEM_KEY 6
Chris Mason9f5fae22007-03-20 14:38:32 -0400288
289/*
290 * the inode map records which inode numbers are in use and where
291 * they actually live on disk
292 */
293#define BTRFS_INODE_MAP_ITEM_KEY 7
Chris Mason1e1d2702007-03-15 19:03:33 -0400294/*
295 * string items are for debugging. They just store a short string of
296 * data in the FS
297 */
Chris Mason9f5fae22007-03-20 14:38:32 -0400298#define BTRFS_STRING_ITEM_KEY 8
Chris Mason1e1d2702007-03-15 19:03:33 -0400299
300static inline u64 btrfs_inode_generation(struct btrfs_inode_item *i)
301{
302 return le64_to_cpu(i->generation);
303}
304
305static inline void btrfs_set_inode_generation(struct btrfs_inode_item *i,
306 u64 val)
307{
308 i->generation = cpu_to_le64(val);
309}
310
311static inline u64 btrfs_inode_size(struct btrfs_inode_item *i)
312{
313 return le64_to_cpu(i->size);
314}
315
316static inline void btrfs_set_inode_size(struct btrfs_inode_item *i, u64 val)
317{
318 i->size = cpu_to_le64(val);
319}
320
321static inline u64 btrfs_inode_nblocks(struct btrfs_inode_item *i)
322{
323 return le64_to_cpu(i->nblocks);
324}
325
326static inline void btrfs_set_inode_nblocks(struct btrfs_inode_item *i, u64 val)
327{
328 i->nblocks = cpu_to_le64(val);
329}
330
331static inline u32 btrfs_inode_nlink(struct btrfs_inode_item *i)
332{
333 return le32_to_cpu(i->nlink);
334}
335
336static inline void btrfs_set_inode_nlink(struct btrfs_inode_item *i, u32 val)
337{
338 i->nlink = cpu_to_le32(val);
339}
340
341static inline u32 btrfs_inode_uid(struct btrfs_inode_item *i)
342{
343 return le32_to_cpu(i->uid);
344}
345
346static inline void btrfs_set_inode_uid(struct btrfs_inode_item *i, u32 val)
347{
348 i->uid = cpu_to_le32(val);
349}
350
351static inline u32 btrfs_inode_gid(struct btrfs_inode_item *i)
352{
353 return le32_to_cpu(i->gid);
354}
355
356static inline void btrfs_set_inode_gid(struct btrfs_inode_item *i, u32 val)
357{
358 i->gid = cpu_to_le32(val);
359}
360
361static inline u32 btrfs_inode_mode(struct btrfs_inode_item *i)
362{
363 return le32_to_cpu(i->mode);
364}
365
366static inline void btrfs_set_inode_mode(struct btrfs_inode_item *i, u32 val)
367{
368 i->mode = cpu_to_le32(val);
369}
370
371static inline u32 btrfs_inode_rdev(struct btrfs_inode_item *i)
372{
373 return le32_to_cpu(i->rdev);
374}
375
376static inline void btrfs_set_inode_rdev(struct btrfs_inode_item *i, u32 val)
377{
378 i->rdev = cpu_to_le32(val);
379}
380
381static inline u16 btrfs_inode_flags(struct btrfs_inode_item *i)
382{
383 return le16_to_cpu(i->flags);
384}
385
386static inline void btrfs_set_inode_flags(struct btrfs_inode_item *i, u16 val)
387{
388 i->flags = cpu_to_le16(val);
389}
390
391static inline u16 btrfs_inode_compat_flags(struct btrfs_inode_item *i)
392{
393 return le16_to_cpu(i->compat_flags);
394}
395
396static inline void btrfs_set_inode_compat_flags(struct btrfs_inode_item *i,
397 u16 val)
398{
399 i->compat_flags = cpu_to_le16(val);
400}
401
Chris Masone20d96d2007-03-22 12:13:20 -0400402static inline u32 btrfs_timespec_sec(struct btrfs_inode_timespec *ts)
403{
404 return le32_to_cpu(ts->sec);
405}
406
407static inline void btrfs_set_timespec_sec(struct btrfs_inode_timespec *ts,
408 u32 val)
409{
410 ts->sec = cpu_to_le32(val);
411}
412
413static inline u32 btrfs_timespec_nsec(struct btrfs_inode_timespec *ts)
414{
415 return le32_to_cpu(ts->nsec);
416}
417
418static inline void btrfs_set_timespec_nsec(struct btrfs_inode_timespec *ts,
419 u32 val)
420{
421 ts->nsec = cpu_to_le32(val);
422}
423
424
Chris Mason62e27492007-03-15 12:56:47 -0400425
Chris Mason234b63a2007-03-13 10:46:10 -0400426static inline u64 btrfs_extent_owner(struct btrfs_extent_item *ei)
Chris Masoncf27e1e2007-03-13 09:49:06 -0400427{
428 return le64_to_cpu(ei->owner);
429}
430
Chris Mason234b63a2007-03-13 10:46:10 -0400431static inline void btrfs_set_extent_owner(struct btrfs_extent_item *ei, u64 val)
Chris Masoncf27e1e2007-03-13 09:49:06 -0400432{
433 ei->owner = cpu_to_le64(val);
434}
435
Chris Mason234b63a2007-03-13 10:46:10 -0400436static inline u32 btrfs_extent_refs(struct btrfs_extent_item *ei)
Chris Masoncf27e1e2007-03-13 09:49:06 -0400437{
438 return le32_to_cpu(ei->refs);
439}
440
Chris Mason234b63a2007-03-13 10:46:10 -0400441static inline void btrfs_set_extent_refs(struct btrfs_extent_item *ei, u32 val)
Chris Masoncf27e1e2007-03-13 09:49:06 -0400442{
443 ei->refs = cpu_to_le32(val);
444}
445
Chris Mason234b63a2007-03-13 10:46:10 -0400446static inline u64 btrfs_node_blockptr(struct btrfs_node *n, int nr)
Chris Mason1d4f8a02007-03-13 09:28:32 -0400447{
Chris Mason123abc82007-03-14 14:14:43 -0400448 return le64_to_cpu(n->ptrs[nr].blockptr);
Chris Mason1d4f8a02007-03-13 09:28:32 -0400449}
450
Chris Mason234b63a2007-03-13 10:46:10 -0400451static inline void btrfs_set_node_blockptr(struct btrfs_node *n, int nr,
452 u64 val)
Chris Mason1d4f8a02007-03-13 09:28:32 -0400453{
Chris Mason123abc82007-03-14 14:14:43 -0400454 n->ptrs[nr].blockptr = cpu_to_le64(val);
Chris Mason1d4f8a02007-03-13 09:28:32 -0400455}
456
Chris Mason123abc82007-03-14 14:14:43 -0400457static inline u32 btrfs_item_offset(struct btrfs_item *item)
Chris Mason0783fcf2007-03-12 20:12:07 -0400458{
Chris Mason123abc82007-03-14 14:14:43 -0400459 return le32_to_cpu(item->offset);
Chris Mason0783fcf2007-03-12 20:12:07 -0400460}
461
Chris Mason123abc82007-03-14 14:14:43 -0400462static inline void btrfs_set_item_offset(struct btrfs_item *item, u32 val)
Chris Mason0783fcf2007-03-12 20:12:07 -0400463{
Chris Mason123abc82007-03-14 14:14:43 -0400464 item->offset = cpu_to_le32(val);
Chris Mason0783fcf2007-03-12 20:12:07 -0400465}
466
Chris Mason123abc82007-03-14 14:14:43 -0400467static inline u32 btrfs_item_end(struct btrfs_item *item)
Chris Mason0783fcf2007-03-12 20:12:07 -0400468{
Chris Mason123abc82007-03-14 14:14:43 -0400469 return le32_to_cpu(item->offset) + le16_to_cpu(item->size);
Chris Mason0783fcf2007-03-12 20:12:07 -0400470}
471
472static inline u16 btrfs_item_size(struct btrfs_item *item)
473{
474 return le16_to_cpu(item->size);
475}
476
477static inline void btrfs_set_item_size(struct btrfs_item *item, u16 val)
478{
479 item->size = cpu_to_le16(val);
480}
481
Chris Mason1d4f6402007-03-15 15:18:43 -0400482static inline u64 btrfs_dir_objectid(struct btrfs_dir_item *d)
483{
484 return le64_to_cpu(d->objectid);
485}
486
487static inline void btrfs_set_dir_objectid(struct btrfs_dir_item *d, u64 val)
488{
489 d->objectid = cpu_to_le64(val);
490}
491
492static inline u16 btrfs_dir_flags(struct btrfs_dir_item *d)
493{
494 return le16_to_cpu(d->flags);
495}
496
497static inline void btrfs_set_dir_flags(struct btrfs_dir_item *d, u16 val)
498{
499 d->flags = cpu_to_le16(val);
500}
501
502static inline u8 btrfs_dir_type(struct btrfs_dir_item *d)
503{
504 return d->type;
505}
506
507static inline void btrfs_set_dir_type(struct btrfs_dir_item *d, u8 val)
508{
509 d->type = val;
510}
511
Chris Masona8a2ee02007-03-16 08:46:49 -0400512static inline u16 btrfs_dir_name_len(struct btrfs_dir_item *d)
Chris Mason1d4f6402007-03-15 15:18:43 -0400513{
Chris Masona8a2ee02007-03-16 08:46:49 -0400514 return le16_to_cpu(d->name_len);
515}
516
517static inline void btrfs_set_dir_name_len(struct btrfs_dir_item *d, u16 val)
518{
519 d->name_len = cpu_to_le16(val);
Chris Mason1d4f6402007-03-15 15:18:43 -0400520}
521
Chris Masone2fa7222007-03-12 16:22:34 -0400522static inline void btrfs_disk_key_to_cpu(struct btrfs_key *cpu,
523 struct btrfs_disk_key *disk)
524{
525 cpu->offset = le64_to_cpu(disk->offset);
526 cpu->flags = le32_to_cpu(disk->flags);
527 cpu->objectid = le64_to_cpu(disk->objectid);
528}
529
530static inline void btrfs_cpu_key_to_disk(struct btrfs_disk_key *disk,
531 struct btrfs_key *cpu)
532{
533 disk->offset = cpu_to_le64(cpu->offset);
534 disk->flags = cpu_to_le32(cpu->flags);
535 disk->objectid = cpu_to_le64(cpu->objectid);
536}
537
Chris Mason62e27492007-03-15 12:56:47 -0400538static inline u64 btrfs_disk_key_objectid(struct btrfs_disk_key *disk)
Chris Masone2fa7222007-03-12 16:22:34 -0400539{
540 return le64_to_cpu(disk->objectid);
541}
542
Chris Mason62e27492007-03-15 12:56:47 -0400543static inline void btrfs_set_disk_key_objectid(struct btrfs_disk_key *disk,
544 u64 val)
Chris Masone2fa7222007-03-12 16:22:34 -0400545{
546 disk->objectid = cpu_to_le64(val);
547}
548
Chris Mason62e27492007-03-15 12:56:47 -0400549static inline u64 btrfs_disk_key_offset(struct btrfs_disk_key *disk)
Chris Masone2fa7222007-03-12 16:22:34 -0400550{
551 return le64_to_cpu(disk->offset);
552}
553
Chris Mason62e27492007-03-15 12:56:47 -0400554static inline void btrfs_set_disk_key_offset(struct btrfs_disk_key *disk,
555 u64 val)
Chris Masone2fa7222007-03-12 16:22:34 -0400556{
557 disk->offset = cpu_to_le64(val);
558}
559
Chris Mason62e27492007-03-15 12:56:47 -0400560static inline u32 btrfs_disk_key_flags(struct btrfs_disk_key *disk)
Chris Masone2fa7222007-03-12 16:22:34 -0400561{
562 return le32_to_cpu(disk->flags);
563}
564
Chris Mason62e27492007-03-15 12:56:47 -0400565static inline void btrfs_set_disk_key_flags(struct btrfs_disk_key *disk,
566 u32 val)
Chris Masone2fa7222007-03-12 16:22:34 -0400567{
568 disk->flags = cpu_to_le32(val);
569}
570
Chris Mason62e27492007-03-15 12:56:47 -0400571static inline u32 btrfs_key_type(struct btrfs_key *key)
572{
573 return key->flags & BTRFS_KEY_TYPE_MASK;
574}
575
576static inline u32 btrfs_disk_key_type(struct btrfs_disk_key *key)
577{
578 return le32_to_cpu(key->flags) & BTRFS_KEY_TYPE_MASK;
579}
580
581static inline void btrfs_set_key_type(struct btrfs_key *key, u32 type)
582{
583 BUG_ON(type >= BTRFS_KEY_TYPE_MAX);
584 key->flags = (key->flags & ~((u64)BTRFS_KEY_TYPE_MASK)) | type;
585}
586
587static inline void btrfs_set_disk_key_type(struct btrfs_disk_key *key, u32 type)
588{
589 u32 flags = btrfs_disk_key_flags(key);
590 BUG_ON(type >= BTRFS_KEY_TYPE_MAX);
591 flags = (flags & ~((u64)BTRFS_KEY_TYPE_MASK)) | type;
592 btrfs_set_disk_key_flags(key, flags);
593}
594
Chris Masonbb492bb2007-03-12 12:29:44 -0400595static inline u64 btrfs_header_blocknr(struct btrfs_header *h)
Chris Mason7518a232007-03-12 12:01:18 -0400596{
Chris Masonbb492bb2007-03-12 12:29:44 -0400597 return le64_to_cpu(h->blocknr);
Chris Mason7518a232007-03-12 12:01:18 -0400598}
599
Chris Masonbb492bb2007-03-12 12:29:44 -0400600static inline void btrfs_set_header_blocknr(struct btrfs_header *h, u64 blocknr)
Chris Mason7518a232007-03-12 12:01:18 -0400601{
Chris Masonbb492bb2007-03-12 12:29:44 -0400602 h->blocknr = cpu_to_le64(blocknr);
Chris Mason7518a232007-03-12 12:01:18 -0400603}
604
Chris Mason7f5c1512007-03-23 15:56:19 -0400605static inline u64 btrfs_header_generation(struct btrfs_header *h)
606{
607 return le64_to_cpu(h->generation);
608}
609
610static inline void btrfs_set_header_generation(struct btrfs_header *h,
611 u64 val)
612{
613 h->generation = cpu_to_le64(val);
614}
615
Chris Masonbb492bb2007-03-12 12:29:44 -0400616static inline u64 btrfs_header_parentid(struct btrfs_header *h)
Chris Mason7518a232007-03-12 12:01:18 -0400617{
Chris Masonbb492bb2007-03-12 12:29:44 -0400618 return le64_to_cpu(h->parentid);
Chris Mason7518a232007-03-12 12:01:18 -0400619}
620
Chris Masonbb492bb2007-03-12 12:29:44 -0400621static inline void btrfs_set_header_parentid(struct btrfs_header *h,
622 u64 parentid)
Chris Mason7518a232007-03-12 12:01:18 -0400623{
Chris Masonbb492bb2007-03-12 12:29:44 -0400624 h->parentid = cpu_to_le64(parentid);
Chris Mason7518a232007-03-12 12:01:18 -0400625}
626
Chris Masonbb492bb2007-03-12 12:29:44 -0400627static inline u16 btrfs_header_nritems(struct btrfs_header *h)
Chris Mason7518a232007-03-12 12:01:18 -0400628{
Chris Masonbb492bb2007-03-12 12:29:44 -0400629 return le16_to_cpu(h->nritems);
Chris Mason7518a232007-03-12 12:01:18 -0400630}
631
Chris Masonbb492bb2007-03-12 12:29:44 -0400632static inline void btrfs_set_header_nritems(struct btrfs_header *h, u16 val)
Chris Mason7518a232007-03-12 12:01:18 -0400633{
Chris Masonbb492bb2007-03-12 12:29:44 -0400634 h->nritems = cpu_to_le16(val);
Chris Mason7518a232007-03-12 12:01:18 -0400635}
636
Chris Masonbb492bb2007-03-12 12:29:44 -0400637static inline u16 btrfs_header_flags(struct btrfs_header *h)
Chris Mason7518a232007-03-12 12:01:18 -0400638{
Chris Masonbb492bb2007-03-12 12:29:44 -0400639 return le16_to_cpu(h->flags);
Chris Mason7518a232007-03-12 12:01:18 -0400640}
641
Chris Masonbb492bb2007-03-12 12:29:44 -0400642static inline void btrfs_set_header_flags(struct btrfs_header *h, u16 val)
Chris Mason7518a232007-03-12 12:01:18 -0400643{
Chris Masonbb492bb2007-03-12 12:29:44 -0400644 h->flags = cpu_to_le16(val);
Chris Mason7518a232007-03-12 12:01:18 -0400645}
646
Chris Masonbb492bb2007-03-12 12:29:44 -0400647static inline int btrfs_header_level(struct btrfs_header *h)
Chris Mason7518a232007-03-12 12:01:18 -0400648{
Chris Mason234b63a2007-03-13 10:46:10 -0400649 return btrfs_header_flags(h) & (BTRFS_MAX_LEVEL - 1);
Chris Mason7518a232007-03-12 12:01:18 -0400650}
651
Chris Masonbb492bb2007-03-12 12:29:44 -0400652static inline void btrfs_set_header_level(struct btrfs_header *h, int level)
Chris Mason7518a232007-03-12 12:01:18 -0400653{
Chris Masonbb492bb2007-03-12 12:29:44 -0400654 u16 flags;
Chris Mason234b63a2007-03-13 10:46:10 -0400655 BUG_ON(level > BTRFS_MAX_LEVEL);
656 flags = btrfs_header_flags(h) & ~(BTRFS_MAX_LEVEL - 1);
Chris Mason7518a232007-03-12 12:01:18 -0400657 btrfs_set_header_flags(h, flags | level);
658}
659
Chris Mason234b63a2007-03-13 10:46:10 -0400660static inline int btrfs_is_leaf(struct btrfs_node *n)
Chris Mason7518a232007-03-12 12:01:18 -0400661{
662 return (btrfs_header_level(&n->header) == 0);
663}
664
Chris Mason3768f362007-03-13 16:47:54 -0400665static inline u64 btrfs_root_blocknr(struct btrfs_root_item *item)
666{
667 return le64_to_cpu(item->blocknr);
668}
669
670static inline void btrfs_set_root_blocknr(struct btrfs_root_item *item, u64 val)
671{
672 item->blocknr = cpu_to_le64(val);
673}
674
675static inline u32 btrfs_root_refs(struct btrfs_root_item *item)
676{
677 return le32_to_cpu(item->refs);
678}
679
680static inline void btrfs_set_root_refs(struct btrfs_root_item *item, u32 val)
681{
682 item->refs = cpu_to_le32(val);
683}
684
685static inline u64 btrfs_super_blocknr(struct btrfs_super_block *s)
686{
687 return le64_to_cpu(s->blocknr);
688}
689
690static inline void btrfs_set_super_blocknr(struct btrfs_super_block *s, u64 val)
691{
692 s->blocknr = cpu_to_le64(val);
693}
694
695static inline u64 btrfs_super_root(struct btrfs_super_block *s)
696{
697 return le64_to_cpu(s->root);
698}
699
700static inline void btrfs_set_super_root(struct btrfs_super_block *s, u64 val)
701{
702 s->root = cpu_to_le64(val);
703}
704
705static inline u64 btrfs_super_total_blocks(struct btrfs_super_block *s)
706{
707 return le64_to_cpu(s->total_blocks);
708}
709
710static inline void btrfs_set_super_total_blocks(struct btrfs_super_block *s,
711 u64 val)
712{
713 s->total_blocks = cpu_to_le64(val);
714}
715
716static inline u64 btrfs_super_blocks_used(struct btrfs_super_block *s)
717{
718 return le64_to_cpu(s->blocks_used);
719}
720
721static inline void btrfs_set_super_blocks_used(struct btrfs_super_block *s,
722 u64 val)
723{
724 s->blocks_used = cpu_to_le64(val);
725}
726
Chris Mason123abc82007-03-14 14:14:43 -0400727static inline u32 btrfs_super_blocksize(struct btrfs_super_block *s)
Chris Mason3768f362007-03-13 16:47:54 -0400728{
Chris Mason123abc82007-03-14 14:14:43 -0400729 return le32_to_cpu(s->blocksize);
Chris Mason3768f362007-03-13 16:47:54 -0400730}
731
732static inline void btrfs_set_super_blocksize(struct btrfs_super_block *s,
Chris Mason123abc82007-03-14 14:14:43 -0400733 u32 val)
Chris Mason3768f362007-03-13 16:47:54 -0400734{
Chris Mason123abc82007-03-14 14:14:43 -0400735 s->blocksize = cpu_to_le32(val);
736}
737
Chris Mason2e635a22007-03-21 11:12:56 -0400738static inline u64 btrfs_super_root_dir(struct btrfs_super_block *s)
739{
740 return le64_to_cpu(s->root_dir_objectid);
741}
742
743static inline void btrfs_set_super_root_dir(struct btrfs_super_block *s, u64
744 val)
745{
746 s->root_dir_objectid = cpu_to_le64(val);
747}
748
Chris Mason123abc82007-03-14 14:14:43 -0400749static inline u8 *btrfs_leaf_data(struct btrfs_leaf *l)
750{
751 return (u8 *)l->items;
Chris Mason3768f362007-03-13 16:47:54 -0400752}
Chris Mason9f5fae22007-03-20 14:38:32 -0400753
754static inline u64 btrfs_file_extent_disk_blocknr(struct btrfs_file_extent_item
755 *e)
756{
757 return le64_to_cpu(e->disk_blocknr);
758}
759
760static inline void btrfs_set_file_extent_disk_blocknr(struct
761 btrfs_file_extent_item
762 *e, u64 val)
763{
764 e->disk_blocknr = cpu_to_le64(val);
765}
766
767static inline u64 btrfs_file_extent_disk_num_blocks(struct
768 btrfs_file_extent_item *e)
769{
770 return le64_to_cpu(e->disk_num_blocks);
771}
772
773static inline void btrfs_set_file_extent_disk_num_blocks(struct
774 btrfs_file_extent_item
775 *e, u64 val)
776{
777 e->disk_num_blocks = cpu_to_le64(val);
778}
779
780static inline u64 btrfs_file_extent_offset(struct btrfs_file_extent_item *e)
781{
782 return le64_to_cpu(e->offset);
783}
784
785static inline void btrfs_set_file_extent_offset(struct btrfs_file_extent_item
786 *e, u64 val)
787{
788 e->offset = cpu_to_le64(val);
789}
790
791static inline u64 btrfs_file_extent_num_blocks(struct btrfs_file_extent_item
792 *e)
793{
794 return le64_to_cpu(e->num_blocks);
795}
796
797static inline void btrfs_set_file_extent_num_blocks(struct
798 btrfs_file_extent_item *e,
799 u64 val)
800{
801 e->num_blocks = cpu_to_le64(val);
802}
803
Chris Masone20d96d2007-03-22 12:13:20 -0400804static inline struct btrfs_root *btrfs_sb(struct super_block *sb)
805{
806 return sb->s_fs_info;
807}
808
Chris Mason4beb1b82007-03-14 10:31:29 -0400809/* helper function to cast into the data area of the leaf. */
810#define btrfs_item_ptr(leaf, slot, type) \
Chris Mason123abc82007-03-14 14:14:43 -0400811 ((type *)(btrfs_leaf_data(leaf) + \
812 btrfs_item_offset((leaf)->items + (slot))))
Chris Mason4beb1b82007-03-14 10:31:29 -0400813
Chris Masone20d96d2007-03-22 12:13:20 -0400814struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masone089f052007-03-16 16:20:31 -0400815 struct btrfs_root *root);
816int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -0400817 struct buffer_head *buf);
Chris Masone089f052007-03-16 16:20:31 -0400818int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
819 *root, u64 blocknr, u64 num_blocks, int pin);
820int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
821 *root, struct btrfs_key *key, struct btrfs_path *p, int
822 ins_len, int cow);
Chris Mason234b63a2007-03-13 10:46:10 -0400823void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p);
824void btrfs_init_path(struct btrfs_path *p);
Chris Masone089f052007-03-16 16:20:31 -0400825int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
826 struct btrfs_path *path);
827int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
828 *root, struct btrfs_key *key, void *data, u32 data_size);
829int btrfs_insert_empty_item(struct btrfs_trans_handle *trans, struct btrfs_root
830 *root, struct btrfs_path *path, struct btrfs_key
831 *cpu_key, u32 data_size);
Chris Mason234b63a2007-03-13 10:46:10 -0400832int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path);
Chris Mason123abc82007-03-14 14:14:43 -0400833int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf);
Chris Masone089f052007-03-16 16:20:31 -0400834int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -0400835 *root, struct buffer_head *snap);
Chris Masone089f052007-03-16 16:20:31 -0400836int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
837 btrfs_root *root);
838int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
839 struct btrfs_key *key);
840int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
841 *root, struct btrfs_key *key, struct btrfs_root_item
842 *item);
843int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
844 *root, struct btrfs_key *key, struct btrfs_root_item
845 *item);
846int btrfs_find_last_root(struct btrfs_root *root, u64 objectid, struct
847 btrfs_root_item *item, struct btrfs_key *key);
848int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond5719762007-03-23 10:01:08 -0400849 *root, const char *name, int name_len, u64 dir, u64
Chris Masone089f052007-03-16 16:20:31 -0400850 objectid, u8 type);
851int btrfs_lookup_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -0400852 *root, struct btrfs_path *path, u64 dir,
853 const char *name, int name_len, int mod);
Chris Mason1d4f6402007-03-15 15:18:43 -0400854int btrfs_match_dir_item_name(struct btrfs_root *root, struct btrfs_path *path,
Chris Mason7f5c1512007-03-23 15:56:19 -0400855 const char *name, int name_len);
Chris Mason9f5fae22007-03-20 14:38:32 -0400856int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
857 struct btrfs_root *fs_root,
858 u64 dirid, u64 *objectid);
859int btrfs_insert_inode_map(struct btrfs_trans_handle *trans,
860 struct btrfs_root *root,
861 u64 objectid, struct btrfs_key *location);
862int btrfs_lookup_inode_map(struct btrfs_trans_handle *trans,
863 struct btrfs_root *root, struct btrfs_path *path,
864 u64 objectid, int mod);
Chris Mason293ffd52007-03-20 15:57:25 -0400865int btrfs_insert_inode(struct btrfs_trans_handle *trans, struct btrfs_root
866 *root, u64 objectid, struct btrfs_inode_item
867 *inode_item);
868int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root
869 *root, struct btrfs_path *path, u64 objectid, int mod);
Chris Masoneb60cea2007-02-02 09:18:22 -0500870#endif