blob: 7748eecd9304c5e5bf485fd0c7c9bc76a4eeec3b [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;
8
Chris Mason3768f362007-03-13 16:47:54 -04009#define BTRFS_MAGIC "_BtRfS_M"
Chris Masoneb60cea2007-02-02 09:18:22 -050010
Chris Mason3768f362007-03-13 16:47:54 -040011#define BTRFS_ROOT_TREE_OBJECTID 1
12#define BTRFS_EXTENT_TREE_OBJECTID 2
Chris Mason9f5fae22007-03-20 14:38:32 -040013#define BTRFS_INODE_MAP_OBJECTID 3
14#define BTRFS_FS_TREE_OBJECTID 4
Chris Mason3768f362007-03-13 16:47:54 -040015
Chris Masonfec577f2007-02-26 10:40:21 -050016/*
Chris Masone20d96d2007-03-22 12:13:20 -040017 * we can actually store much bigger names, but lets not confuse the rest
18 * of linux
19 */
20#define BTRFS_NAME_LEN 255
21
22/*
Chris Masonfec577f2007-02-26 10:40:21 -050023 * the key defines the order in the tree, and so it also defines (optimal)
24 * block layout. objectid corresonds to the inode number. The flags
25 * tells us things about the object, and is a kind of stream selector.
26 * so for a given inode, keys with flags of 1 might refer to the inode
27 * data, flags of 2 may point to file data in the btree and flags == 3
28 * may point to extents.
29 *
30 * offset is the starting byte offset for this key in the stream.
Chris Masone2fa7222007-03-12 16:22:34 -040031 *
32 * btrfs_disk_key is in disk byte order. struct btrfs_key is always
33 * in cpu native order. Otherwise they are identical and their sizes
34 * should be the same (ie both packed)
Chris Masonfec577f2007-02-26 10:40:21 -050035 */
Chris Masone2fa7222007-03-12 16:22:34 -040036struct btrfs_disk_key {
37 __le64 objectid;
Chris Masona1516c82007-03-14 14:26:53 -040038 __le32 flags;
Chris Masona8a2ee02007-03-16 08:46:49 -040039 __le64 offset;
Chris Masone2fa7222007-03-12 16:22:34 -040040} __attribute__ ((__packed__));
41
42struct btrfs_key {
Chris Masoneb60cea2007-02-02 09:18:22 -050043 u64 objectid;
Chris Masona1516c82007-03-14 14:26:53 -040044 u32 flags;
Chris Masona8a2ee02007-03-16 08:46:49 -040045 u64 offset;
Chris Masoneb60cea2007-02-02 09:18:22 -050046} __attribute__ ((__packed__));
47
Chris Masonfec577f2007-02-26 10:40:21 -050048/*
49 * every tree block (leaf or node) starts with this header.
50 */
Chris Masonbb492bb2007-03-12 12:29:44 -040051struct btrfs_header {
Chris Mason3768f362007-03-13 16:47:54 -040052 u8 fsid[16]; /* FS specific uuid */
Chris Masonbb492bb2007-03-12 12:29:44 -040053 __le64 blocknr; /* which block this node is supposed to live in */
54 __le64 parentid; /* objectid of the tree root */
55 __le32 csum;
56 __le32 ham;
57 __le16 nritems;
58 __le16 flags;
Chris Masonfec577f2007-02-26 10:40:21 -050059 /* generation flags to be added */
Chris Masoneb60cea2007-02-02 09:18:22 -050060} __attribute__ ((__packed__));
61
Chris Mason234b63a2007-03-13 10:46:10 -040062#define BTRFS_MAX_LEVEL 8
Chris Mason123abc82007-03-14 14:14:43 -040063#define BTRFS_NODEPTRS_PER_BLOCK(r) (((r)->blocksize - \
64 sizeof(struct btrfs_header)) / \
65 (sizeof(struct btrfs_disk_key) + sizeof(u64)))
66#define __BTRFS_LEAF_DATA_SIZE(bs) ((bs) - sizeof(struct btrfs_header))
67#define BTRFS_LEAF_DATA_SIZE(r) (__BTRFS_LEAF_DATA_SIZE(r->blocksize))
Chris Masoneb60cea2007-02-02 09:18:22 -050068
Chris Masone20d96d2007-03-22 12:13:20 -040069struct buffer_head;
Chris Masonfec577f2007-02-26 10:40:21 -050070/*
Chris Masonfec577f2007-02-26 10:40:21 -050071 * the super block basically lists the main trees of the FS
72 * it currently lacks any block count etc etc
73 */
Chris Mason234b63a2007-03-13 10:46:10 -040074struct btrfs_super_block {
Chris Mason3768f362007-03-13 16:47:54 -040075 u8 fsid[16]; /* FS specific uuid */
76 __le64 blocknr; /* this block number */
77 __le32 csum;
78 __le64 magic;
Chris Mason123abc82007-03-14 14:14:43 -040079 __le32 blocksize;
Chris Mason3768f362007-03-13 16:47:54 -040080 __le64 generation;
81 __le64 root;
82 __le64 total_blocks;
83 __le64 blocks_used;
Chris Mason2e635a22007-03-21 11:12:56 -040084 __le64 root_dir_objectid;
Chris Masoncfaa7292007-02-21 17:04:57 -050085} __attribute__ ((__packed__));
86
Chris Masonfec577f2007-02-26 10:40:21 -050087/*
Chris Mason62e27492007-03-15 12:56:47 -040088 * A leaf is full of items. offset and size tell us where to find
Chris Masonfec577f2007-02-26 10:40:21 -050089 * the item in the leaf (relative to the start of the data area)
90 */
Chris Mason0783fcf2007-03-12 20:12:07 -040091struct btrfs_item {
Chris Masone2fa7222007-03-12 16:22:34 -040092 struct btrfs_disk_key key;
Chris Mason123abc82007-03-14 14:14:43 -040093 __le32 offset;
Chris Mason0783fcf2007-03-12 20:12:07 -040094 __le16 size;
Chris Masoneb60cea2007-02-02 09:18:22 -050095} __attribute__ ((__packed__));
96
Chris Masonfec577f2007-02-26 10:40:21 -050097/*
98 * leaves have an item area and a data area:
99 * [item0, item1....itemN] [free space] [dataN...data1, data0]
100 *
101 * The data is separate from the items to get the keys closer together
102 * during searches.
103 */
Chris Mason234b63a2007-03-13 10:46:10 -0400104struct btrfs_leaf {
Chris Masonbb492bb2007-03-12 12:29:44 -0400105 struct btrfs_header header;
Chris Mason123abc82007-03-14 14:14:43 -0400106 struct btrfs_item items[];
Chris Masoneb60cea2007-02-02 09:18:22 -0500107} __attribute__ ((__packed__));
108
Chris Masonfec577f2007-02-26 10:40:21 -0500109/*
110 * all non-leaf blocks are nodes, they hold only keys and pointers to
111 * other blocks
112 */
Chris Mason123abc82007-03-14 14:14:43 -0400113struct btrfs_key_ptr {
114 struct btrfs_disk_key key;
115 __le64 blockptr;
116} __attribute__ ((__packed__));
117
Chris Mason234b63a2007-03-13 10:46:10 -0400118struct btrfs_node {
Chris Masonbb492bb2007-03-12 12:29:44 -0400119 struct btrfs_header header;
Chris Mason123abc82007-03-14 14:14:43 -0400120 struct btrfs_key_ptr ptrs[];
Chris Masoneb60cea2007-02-02 09:18:22 -0500121} __attribute__ ((__packed__));
122
Chris Masonfec577f2007-02-26 10:40:21 -0500123/*
Chris Mason234b63a2007-03-13 10:46:10 -0400124 * btrfs_paths remember the path taken from the root down to the leaf.
125 * level 0 is always the leaf, and nodes[1...BTRFS_MAX_LEVEL] will point
Chris Masonfec577f2007-02-26 10:40:21 -0500126 * to any other levels that are present.
127 *
128 * The slots array records the index of the item or block pointer
129 * used while walking the tree.
130 */
Chris Mason234b63a2007-03-13 10:46:10 -0400131struct btrfs_path {
Chris Masone20d96d2007-03-22 12:13:20 -0400132 struct buffer_head *nodes[BTRFS_MAX_LEVEL];
Chris Mason234b63a2007-03-13 10:46:10 -0400133 int slots[BTRFS_MAX_LEVEL];
Chris Masoneb60cea2007-02-02 09:18:22 -0500134};
Chris Mason5de08d72007-02-24 06:24:44 -0500135
Chris Mason62e27492007-03-15 12:56:47 -0400136/*
137 * items in the extent btree are used to record the objectid of the
138 * owner of the block and the number of references
139 */
140struct btrfs_extent_item {
141 __le32 refs;
142 __le64 owner;
143} __attribute__ ((__packed__));
144
Chris Mason1e1d2702007-03-15 19:03:33 -0400145struct btrfs_inode_timespec {
146 __le32 sec;
147 __le32 nsec;
148} __attribute__ ((__packed__));
149
150/*
151 * there is no padding here on purpose. If you want to extent the inode,
152 * make a new item type
153 */
154struct btrfs_inode_item {
155 __le64 generation;
156 __le64 size;
157 __le64 nblocks;
158 __le32 nlink;
159 __le32 uid;
160 __le32 gid;
161 __le32 mode;
162 __le32 rdev;
163 __le16 flags;
164 __le16 compat_flags;
165 struct btrfs_inode_timespec atime;
166 struct btrfs_inode_timespec ctime;
167 struct btrfs_inode_timespec mtime;
168 struct btrfs_inode_timespec otime;
169} __attribute__ ((__packed__));
170
171/* inline data is just a blob of bytes */
172struct btrfs_inline_data_item {
173 u8 data;
174} __attribute__ ((__packed__));
175
Chris Mason62e27492007-03-15 12:56:47 -0400176struct btrfs_dir_item {
177 __le64 objectid;
178 __le16 flags;
Chris Masona8a2ee02007-03-16 08:46:49 -0400179 __le16 name_len;
Chris Mason62e27492007-03-15 12:56:47 -0400180 u8 type;
181} __attribute__ ((__packed__));
182
183struct btrfs_root_item {
184 __le64 blocknr;
185 __le32 flags;
186 __le64 block_limit;
187 __le64 blocks_used;
188 __le32 refs;
Chris Mason9f5fae22007-03-20 14:38:32 -0400189} __attribute__ ((__packed__));
190
191struct btrfs_file_extent_item {
192 /*
193 * disk space consumed by the extent, checksum blocks are included
194 * in these numbers
195 */
196 __le64 disk_blocknr;
197 __le64 disk_num_blocks;
198 /*
199 * the logical offset in file bytes (no csums)
200 * this extent record is for. This allows a file extent to point
201 * into the middle of an existing extent on disk, sharing it
202 * between two snapshots (useful if some bytes in the middle of the
203 * extent have changed
204 */
205 __le64 offset;
206 /*
207 * the logical number of file blocks (no csums included)
208 */
209 __le64 num_blocks;
210} __attribute__ ((__packed__));
211
212struct btrfs_inode_map_item {
213 struct btrfs_disk_key key;
214} __attribute__ ((__packed__));
215
216struct btrfs_fs_info {
217 struct btrfs_root *fs_root;
218 struct btrfs_root *extent_root;
219 struct btrfs_root *tree_root;
220 struct btrfs_root *inode_root;
221 struct btrfs_key current_insert;
222 struct btrfs_key last_insert;
Chris Mason9f5fae22007-03-20 14:38:32 -0400223 struct radix_tree_root pinned_radix;
Chris Mason9f5fae22007-03-20 14:38:32 -0400224 u64 last_inode_alloc;
225 u64 last_inode_alloc_dirid;
Chris Mason293ffd52007-03-20 15:57:25 -0400226 u64 generation;
Chris Mason9f5fae22007-03-20 14:38:32 -0400227 struct btrfs_trans_handle *running_transaction;
Chris Mason1261ec42007-03-20 20:35:03 -0400228 struct btrfs_super_block *disk_super;
Chris Masone20d96d2007-03-22 12:13:20 -0400229 struct buffer_head *sb_buffer;
230 struct super_block *sb;
Chris Mason62e27492007-03-15 12:56:47 -0400231};
232
233/*
234 * in ram representation of the tree. extent_root is used for all allocations
235 * and for the extent tree extent_root root. current_insert is used
236 * only for the extent tree.
237 */
238struct btrfs_root {
Chris Masone20d96d2007-03-22 12:13:20 -0400239 struct buffer_head *node;
240 struct buffer_head *commit_root;
Chris Mason62e27492007-03-15 12:56:47 -0400241 struct btrfs_root_item root_item;
242 struct btrfs_key root_key;
Chris Mason9f5fae22007-03-20 14:38:32 -0400243 struct btrfs_fs_info *fs_info;
Chris Mason62e27492007-03-15 12:56:47 -0400244 u32 blocksize;
Chris Mason9f5fae22007-03-20 14:38:32 -0400245 int ref_cows;
246 u32 type;
Chris Mason62e27492007-03-15 12:56:47 -0400247};
248
Chris Mason62e27492007-03-15 12:56:47 -0400249/* the lower bits in the key flags defines the item type */
250#define BTRFS_KEY_TYPE_MAX 256
251#define BTRFS_KEY_TYPE_MASK (BTRFS_KEY_TYPE_MAX - 1)
Chris Mason1e1d2702007-03-15 19:03:33 -0400252
253/*
254 * inode items have the data typically returned from stat and store other
255 * info about object characteristics. There is one for every file and dir in
256 * the FS
257 */
Chris Mason62e27492007-03-15 12:56:47 -0400258#define BTRFS_INODE_ITEM_KEY 1
Chris Mason1e1d2702007-03-15 19:03:33 -0400259
260/*
261 * dir items are the name -> inode pointers in a directory. There is one
262 * for every name in a directory.
263 */
Chris Mason62e27492007-03-15 12:56:47 -0400264#define BTRFS_DIR_ITEM_KEY 2
Chris Mason1e1d2702007-03-15 19:03:33 -0400265/*
266 * inline data is file data that fits in the btree.
267 */
268#define BTRFS_INLINE_DATA_KEY 3
269/*
270 * extent data is for data that can't fit in the btree. It points to
271 * a (hopefully) huge chunk of disk
272 */
273#define BTRFS_EXTENT_DATA_KEY 4
274/*
275 * root items point to tree roots. There are typically in the root
276 * tree used by the super block to find all the other trees
277 */
278#define BTRFS_ROOT_ITEM_KEY 5
279/*
280 * extent items are in the extent map tree. These record which blocks
281 * are used, and how many references there are to each block
282 */
283#define BTRFS_EXTENT_ITEM_KEY 6
Chris Mason9f5fae22007-03-20 14:38:32 -0400284
285/*
286 * the inode map records which inode numbers are in use and where
287 * they actually live on disk
288 */
289#define BTRFS_INODE_MAP_ITEM_KEY 7
Chris Mason1e1d2702007-03-15 19:03:33 -0400290/*
291 * string items are for debugging. They just store a short string of
292 * data in the FS
293 */
Chris Mason9f5fae22007-03-20 14:38:32 -0400294#define BTRFS_STRING_ITEM_KEY 8
Chris Mason1e1d2702007-03-15 19:03:33 -0400295
296static inline u64 btrfs_inode_generation(struct btrfs_inode_item *i)
297{
298 return le64_to_cpu(i->generation);
299}
300
301static inline void btrfs_set_inode_generation(struct btrfs_inode_item *i,
302 u64 val)
303{
304 i->generation = cpu_to_le64(val);
305}
306
307static inline u64 btrfs_inode_size(struct btrfs_inode_item *i)
308{
309 return le64_to_cpu(i->size);
310}
311
312static inline void btrfs_set_inode_size(struct btrfs_inode_item *i, u64 val)
313{
314 i->size = cpu_to_le64(val);
315}
316
317static inline u64 btrfs_inode_nblocks(struct btrfs_inode_item *i)
318{
319 return le64_to_cpu(i->nblocks);
320}
321
322static inline void btrfs_set_inode_nblocks(struct btrfs_inode_item *i, u64 val)
323{
324 i->nblocks = cpu_to_le64(val);
325}
326
327static inline u32 btrfs_inode_nlink(struct btrfs_inode_item *i)
328{
329 return le32_to_cpu(i->nlink);
330}
331
332static inline void btrfs_set_inode_nlink(struct btrfs_inode_item *i, u32 val)
333{
334 i->nlink = cpu_to_le32(val);
335}
336
337static inline u32 btrfs_inode_uid(struct btrfs_inode_item *i)
338{
339 return le32_to_cpu(i->uid);
340}
341
342static inline void btrfs_set_inode_uid(struct btrfs_inode_item *i, u32 val)
343{
344 i->uid = cpu_to_le32(val);
345}
346
347static inline u32 btrfs_inode_gid(struct btrfs_inode_item *i)
348{
349 return le32_to_cpu(i->gid);
350}
351
352static inline void btrfs_set_inode_gid(struct btrfs_inode_item *i, u32 val)
353{
354 i->gid = cpu_to_le32(val);
355}
356
357static inline u32 btrfs_inode_mode(struct btrfs_inode_item *i)
358{
359 return le32_to_cpu(i->mode);
360}
361
362static inline void btrfs_set_inode_mode(struct btrfs_inode_item *i, u32 val)
363{
364 i->mode = cpu_to_le32(val);
365}
366
367static inline u32 btrfs_inode_rdev(struct btrfs_inode_item *i)
368{
369 return le32_to_cpu(i->rdev);
370}
371
372static inline void btrfs_set_inode_rdev(struct btrfs_inode_item *i, u32 val)
373{
374 i->rdev = cpu_to_le32(val);
375}
376
377static inline u16 btrfs_inode_flags(struct btrfs_inode_item *i)
378{
379 return le16_to_cpu(i->flags);
380}
381
382static inline void btrfs_set_inode_flags(struct btrfs_inode_item *i, u16 val)
383{
384 i->flags = cpu_to_le16(val);
385}
386
387static inline u16 btrfs_inode_compat_flags(struct btrfs_inode_item *i)
388{
389 return le16_to_cpu(i->compat_flags);
390}
391
392static inline void btrfs_set_inode_compat_flags(struct btrfs_inode_item *i,
393 u16 val)
394{
395 i->compat_flags = cpu_to_le16(val);
396}
397
Chris Masone20d96d2007-03-22 12:13:20 -0400398static inline u32 btrfs_timespec_sec(struct btrfs_inode_timespec *ts)
399{
400 return le32_to_cpu(ts->sec);
401}
402
403static inline void btrfs_set_timespec_sec(struct btrfs_inode_timespec *ts,
404 u32 val)
405{
406 ts->sec = cpu_to_le32(val);
407}
408
409static inline u32 btrfs_timespec_nsec(struct btrfs_inode_timespec *ts)
410{
411 return le32_to_cpu(ts->nsec);
412}
413
414static inline void btrfs_set_timespec_nsec(struct btrfs_inode_timespec *ts,
415 u32 val)
416{
417 ts->nsec = cpu_to_le32(val);
418}
419
420
Chris Mason62e27492007-03-15 12:56:47 -0400421
Chris Mason234b63a2007-03-13 10:46:10 -0400422static inline u64 btrfs_extent_owner(struct btrfs_extent_item *ei)
Chris Masoncf27e1e2007-03-13 09:49:06 -0400423{
424 return le64_to_cpu(ei->owner);
425}
426
Chris Mason234b63a2007-03-13 10:46:10 -0400427static inline void btrfs_set_extent_owner(struct btrfs_extent_item *ei, u64 val)
Chris Masoncf27e1e2007-03-13 09:49:06 -0400428{
429 ei->owner = cpu_to_le64(val);
430}
431
Chris Mason234b63a2007-03-13 10:46:10 -0400432static inline u32 btrfs_extent_refs(struct btrfs_extent_item *ei)
Chris Masoncf27e1e2007-03-13 09:49:06 -0400433{
434 return le32_to_cpu(ei->refs);
435}
436
Chris Mason234b63a2007-03-13 10:46:10 -0400437static inline void btrfs_set_extent_refs(struct btrfs_extent_item *ei, u32 val)
Chris Masoncf27e1e2007-03-13 09:49:06 -0400438{
439 ei->refs = cpu_to_le32(val);
440}
441
Chris Mason234b63a2007-03-13 10:46:10 -0400442static inline u64 btrfs_node_blockptr(struct btrfs_node *n, int nr)
Chris Mason1d4f8a02007-03-13 09:28:32 -0400443{
Chris Mason123abc82007-03-14 14:14:43 -0400444 return le64_to_cpu(n->ptrs[nr].blockptr);
Chris Mason1d4f8a02007-03-13 09:28:32 -0400445}
446
Chris Mason234b63a2007-03-13 10:46:10 -0400447static inline void btrfs_set_node_blockptr(struct btrfs_node *n, int nr,
448 u64 val)
Chris Mason1d4f8a02007-03-13 09:28:32 -0400449{
Chris Mason123abc82007-03-14 14:14:43 -0400450 n->ptrs[nr].blockptr = cpu_to_le64(val);
Chris Mason1d4f8a02007-03-13 09:28:32 -0400451}
452
Chris Mason123abc82007-03-14 14:14:43 -0400453static inline u32 btrfs_item_offset(struct btrfs_item *item)
Chris Mason0783fcf2007-03-12 20:12:07 -0400454{
Chris Mason123abc82007-03-14 14:14:43 -0400455 return le32_to_cpu(item->offset);
Chris Mason0783fcf2007-03-12 20:12:07 -0400456}
457
Chris Mason123abc82007-03-14 14:14:43 -0400458static inline void btrfs_set_item_offset(struct btrfs_item *item, u32 val)
Chris Mason0783fcf2007-03-12 20:12:07 -0400459{
Chris Mason123abc82007-03-14 14:14:43 -0400460 item->offset = cpu_to_le32(val);
Chris Mason0783fcf2007-03-12 20:12:07 -0400461}
462
Chris Mason123abc82007-03-14 14:14:43 -0400463static inline u32 btrfs_item_end(struct btrfs_item *item)
Chris Mason0783fcf2007-03-12 20:12:07 -0400464{
Chris Mason123abc82007-03-14 14:14:43 -0400465 return le32_to_cpu(item->offset) + le16_to_cpu(item->size);
Chris Mason0783fcf2007-03-12 20:12:07 -0400466}
467
468static inline u16 btrfs_item_size(struct btrfs_item *item)
469{
470 return le16_to_cpu(item->size);
471}
472
473static inline void btrfs_set_item_size(struct btrfs_item *item, u16 val)
474{
475 item->size = cpu_to_le16(val);
476}
477
Chris Mason1d4f6402007-03-15 15:18:43 -0400478static inline u64 btrfs_dir_objectid(struct btrfs_dir_item *d)
479{
480 return le64_to_cpu(d->objectid);
481}
482
483static inline void btrfs_set_dir_objectid(struct btrfs_dir_item *d, u64 val)
484{
485 d->objectid = cpu_to_le64(val);
486}
487
488static inline u16 btrfs_dir_flags(struct btrfs_dir_item *d)
489{
490 return le16_to_cpu(d->flags);
491}
492
493static inline void btrfs_set_dir_flags(struct btrfs_dir_item *d, u16 val)
494{
495 d->flags = cpu_to_le16(val);
496}
497
498static inline u8 btrfs_dir_type(struct btrfs_dir_item *d)
499{
500 return d->type;
501}
502
503static inline void btrfs_set_dir_type(struct btrfs_dir_item *d, u8 val)
504{
505 d->type = val;
506}
507
Chris Masona8a2ee02007-03-16 08:46:49 -0400508static inline u16 btrfs_dir_name_len(struct btrfs_dir_item *d)
Chris Mason1d4f6402007-03-15 15:18:43 -0400509{
Chris Masona8a2ee02007-03-16 08:46:49 -0400510 return le16_to_cpu(d->name_len);
511}
512
513static inline void btrfs_set_dir_name_len(struct btrfs_dir_item *d, u16 val)
514{
515 d->name_len = cpu_to_le16(val);
Chris Mason1d4f6402007-03-15 15:18:43 -0400516}
517
Chris Masone2fa7222007-03-12 16:22:34 -0400518static inline void btrfs_disk_key_to_cpu(struct btrfs_key *cpu,
519 struct btrfs_disk_key *disk)
520{
521 cpu->offset = le64_to_cpu(disk->offset);
522 cpu->flags = le32_to_cpu(disk->flags);
523 cpu->objectid = le64_to_cpu(disk->objectid);
524}
525
526static inline void btrfs_cpu_key_to_disk(struct btrfs_disk_key *disk,
527 struct btrfs_key *cpu)
528{
529 disk->offset = cpu_to_le64(cpu->offset);
530 disk->flags = cpu_to_le32(cpu->flags);
531 disk->objectid = cpu_to_le64(cpu->objectid);
532}
533
Chris Mason62e27492007-03-15 12:56:47 -0400534static inline u64 btrfs_disk_key_objectid(struct btrfs_disk_key *disk)
Chris Masone2fa7222007-03-12 16:22:34 -0400535{
536 return le64_to_cpu(disk->objectid);
537}
538
Chris Mason62e27492007-03-15 12:56:47 -0400539static inline void btrfs_set_disk_key_objectid(struct btrfs_disk_key *disk,
540 u64 val)
Chris Masone2fa7222007-03-12 16:22:34 -0400541{
542 disk->objectid = cpu_to_le64(val);
543}
544
Chris Mason62e27492007-03-15 12:56:47 -0400545static inline u64 btrfs_disk_key_offset(struct btrfs_disk_key *disk)
Chris Masone2fa7222007-03-12 16:22:34 -0400546{
547 return le64_to_cpu(disk->offset);
548}
549
Chris Mason62e27492007-03-15 12:56:47 -0400550static inline void btrfs_set_disk_key_offset(struct btrfs_disk_key *disk,
551 u64 val)
Chris Masone2fa7222007-03-12 16:22:34 -0400552{
553 disk->offset = cpu_to_le64(val);
554}
555
Chris Mason62e27492007-03-15 12:56:47 -0400556static inline u32 btrfs_disk_key_flags(struct btrfs_disk_key *disk)
Chris Masone2fa7222007-03-12 16:22:34 -0400557{
558 return le32_to_cpu(disk->flags);
559}
560
Chris Mason62e27492007-03-15 12:56:47 -0400561static inline void btrfs_set_disk_key_flags(struct btrfs_disk_key *disk,
562 u32 val)
Chris Masone2fa7222007-03-12 16:22:34 -0400563{
564 disk->flags = cpu_to_le32(val);
565}
566
Chris Mason62e27492007-03-15 12:56:47 -0400567static inline u32 btrfs_key_type(struct btrfs_key *key)
568{
569 return key->flags & BTRFS_KEY_TYPE_MASK;
570}
571
572static inline u32 btrfs_disk_key_type(struct btrfs_disk_key *key)
573{
574 return le32_to_cpu(key->flags) & BTRFS_KEY_TYPE_MASK;
575}
576
577static inline void btrfs_set_key_type(struct btrfs_key *key, u32 type)
578{
579 BUG_ON(type >= BTRFS_KEY_TYPE_MAX);
580 key->flags = (key->flags & ~((u64)BTRFS_KEY_TYPE_MASK)) | type;
581}
582
583static inline void btrfs_set_disk_key_type(struct btrfs_disk_key *key, u32 type)
584{
585 u32 flags = btrfs_disk_key_flags(key);
586 BUG_ON(type >= BTRFS_KEY_TYPE_MAX);
587 flags = (flags & ~((u64)BTRFS_KEY_TYPE_MASK)) | type;
588 btrfs_set_disk_key_flags(key, flags);
589}
590
Chris Masonbb492bb2007-03-12 12:29:44 -0400591static inline u64 btrfs_header_blocknr(struct btrfs_header *h)
Chris Mason7518a232007-03-12 12:01:18 -0400592{
Chris Masonbb492bb2007-03-12 12:29:44 -0400593 return le64_to_cpu(h->blocknr);
Chris Mason7518a232007-03-12 12:01:18 -0400594}
595
Chris Masonbb492bb2007-03-12 12:29:44 -0400596static inline void btrfs_set_header_blocknr(struct btrfs_header *h, u64 blocknr)
Chris Mason7518a232007-03-12 12:01:18 -0400597{
Chris Masonbb492bb2007-03-12 12:29:44 -0400598 h->blocknr = cpu_to_le64(blocknr);
Chris Mason7518a232007-03-12 12:01:18 -0400599}
600
Chris Masonbb492bb2007-03-12 12:29:44 -0400601static inline u64 btrfs_header_parentid(struct btrfs_header *h)
Chris Mason7518a232007-03-12 12:01:18 -0400602{
Chris Masonbb492bb2007-03-12 12:29:44 -0400603 return le64_to_cpu(h->parentid);
Chris Mason7518a232007-03-12 12:01:18 -0400604}
605
Chris Masonbb492bb2007-03-12 12:29:44 -0400606static inline void btrfs_set_header_parentid(struct btrfs_header *h,
607 u64 parentid)
Chris Mason7518a232007-03-12 12:01:18 -0400608{
Chris Masonbb492bb2007-03-12 12:29:44 -0400609 h->parentid = cpu_to_le64(parentid);
Chris Mason7518a232007-03-12 12:01:18 -0400610}
611
Chris Masonbb492bb2007-03-12 12:29:44 -0400612static inline u16 btrfs_header_nritems(struct btrfs_header *h)
Chris Mason7518a232007-03-12 12:01:18 -0400613{
Chris Masonbb492bb2007-03-12 12:29:44 -0400614 return le16_to_cpu(h->nritems);
Chris Mason7518a232007-03-12 12:01:18 -0400615}
616
Chris Masonbb492bb2007-03-12 12:29:44 -0400617static inline void btrfs_set_header_nritems(struct btrfs_header *h, u16 val)
Chris Mason7518a232007-03-12 12:01:18 -0400618{
Chris Masonbb492bb2007-03-12 12:29:44 -0400619 h->nritems = cpu_to_le16(val);
Chris Mason7518a232007-03-12 12:01:18 -0400620}
621
Chris Masonbb492bb2007-03-12 12:29:44 -0400622static inline u16 btrfs_header_flags(struct btrfs_header *h)
Chris Mason7518a232007-03-12 12:01:18 -0400623{
Chris Masonbb492bb2007-03-12 12:29:44 -0400624 return le16_to_cpu(h->flags);
Chris Mason7518a232007-03-12 12:01:18 -0400625}
626
Chris Masonbb492bb2007-03-12 12:29:44 -0400627static inline void btrfs_set_header_flags(struct btrfs_header *h, u16 val)
Chris Mason7518a232007-03-12 12:01:18 -0400628{
Chris Masonbb492bb2007-03-12 12:29:44 -0400629 h->flags = cpu_to_le16(val);
Chris Mason7518a232007-03-12 12:01:18 -0400630}
631
Chris Masonbb492bb2007-03-12 12:29:44 -0400632static inline int btrfs_header_level(struct btrfs_header *h)
Chris Mason7518a232007-03-12 12:01:18 -0400633{
Chris Mason234b63a2007-03-13 10:46:10 -0400634 return btrfs_header_flags(h) & (BTRFS_MAX_LEVEL - 1);
Chris Mason7518a232007-03-12 12:01:18 -0400635}
636
Chris Masonbb492bb2007-03-12 12:29:44 -0400637static inline void btrfs_set_header_level(struct btrfs_header *h, int level)
Chris Mason7518a232007-03-12 12:01:18 -0400638{
Chris Masonbb492bb2007-03-12 12:29:44 -0400639 u16 flags;
Chris Mason234b63a2007-03-13 10:46:10 -0400640 BUG_ON(level > BTRFS_MAX_LEVEL);
641 flags = btrfs_header_flags(h) & ~(BTRFS_MAX_LEVEL - 1);
Chris Mason7518a232007-03-12 12:01:18 -0400642 btrfs_set_header_flags(h, flags | level);
643}
644
Chris Mason234b63a2007-03-13 10:46:10 -0400645static inline int btrfs_is_leaf(struct btrfs_node *n)
Chris Mason7518a232007-03-12 12:01:18 -0400646{
647 return (btrfs_header_level(&n->header) == 0);
648}
649
Chris Mason3768f362007-03-13 16:47:54 -0400650static inline u64 btrfs_root_blocknr(struct btrfs_root_item *item)
651{
652 return le64_to_cpu(item->blocknr);
653}
654
655static inline void btrfs_set_root_blocknr(struct btrfs_root_item *item, u64 val)
656{
657 item->blocknr = cpu_to_le64(val);
658}
659
660static inline u32 btrfs_root_refs(struct btrfs_root_item *item)
661{
662 return le32_to_cpu(item->refs);
663}
664
665static inline void btrfs_set_root_refs(struct btrfs_root_item *item, u32 val)
666{
667 item->refs = cpu_to_le32(val);
668}
669
670static inline u64 btrfs_super_blocknr(struct btrfs_super_block *s)
671{
672 return le64_to_cpu(s->blocknr);
673}
674
675static inline void btrfs_set_super_blocknr(struct btrfs_super_block *s, u64 val)
676{
677 s->blocknr = cpu_to_le64(val);
678}
679
680static inline u64 btrfs_super_root(struct btrfs_super_block *s)
681{
682 return le64_to_cpu(s->root);
683}
684
685static inline void btrfs_set_super_root(struct btrfs_super_block *s, u64 val)
686{
687 s->root = cpu_to_le64(val);
688}
689
690static inline u64 btrfs_super_total_blocks(struct btrfs_super_block *s)
691{
692 return le64_to_cpu(s->total_blocks);
693}
694
695static inline void btrfs_set_super_total_blocks(struct btrfs_super_block *s,
696 u64 val)
697{
698 s->total_blocks = cpu_to_le64(val);
699}
700
701static inline u64 btrfs_super_blocks_used(struct btrfs_super_block *s)
702{
703 return le64_to_cpu(s->blocks_used);
704}
705
706static inline void btrfs_set_super_blocks_used(struct btrfs_super_block *s,
707 u64 val)
708{
709 s->blocks_used = cpu_to_le64(val);
710}
711
Chris Mason123abc82007-03-14 14:14:43 -0400712static inline u32 btrfs_super_blocksize(struct btrfs_super_block *s)
Chris Mason3768f362007-03-13 16:47:54 -0400713{
Chris Mason123abc82007-03-14 14:14:43 -0400714 return le32_to_cpu(s->blocksize);
Chris Mason3768f362007-03-13 16:47:54 -0400715}
716
717static inline void btrfs_set_super_blocksize(struct btrfs_super_block *s,
Chris Mason123abc82007-03-14 14:14:43 -0400718 u32 val)
Chris Mason3768f362007-03-13 16:47:54 -0400719{
Chris Mason123abc82007-03-14 14:14:43 -0400720 s->blocksize = cpu_to_le32(val);
721}
722
Chris Mason2e635a22007-03-21 11:12:56 -0400723static inline u64 btrfs_super_root_dir(struct btrfs_super_block *s)
724{
725 return le64_to_cpu(s->root_dir_objectid);
726}
727
728static inline void btrfs_set_super_root_dir(struct btrfs_super_block *s, u64
729 val)
730{
731 s->root_dir_objectid = cpu_to_le64(val);
732}
733
Chris Mason123abc82007-03-14 14:14:43 -0400734static inline u8 *btrfs_leaf_data(struct btrfs_leaf *l)
735{
736 return (u8 *)l->items;
Chris Mason3768f362007-03-13 16:47:54 -0400737}
Chris Mason9f5fae22007-03-20 14:38:32 -0400738
739static inline u64 btrfs_file_extent_disk_blocknr(struct btrfs_file_extent_item
740 *e)
741{
742 return le64_to_cpu(e->disk_blocknr);
743}
744
745static inline void btrfs_set_file_extent_disk_blocknr(struct
746 btrfs_file_extent_item
747 *e, u64 val)
748{
749 e->disk_blocknr = cpu_to_le64(val);
750}
751
752static inline u64 btrfs_file_extent_disk_num_blocks(struct
753 btrfs_file_extent_item *e)
754{
755 return le64_to_cpu(e->disk_num_blocks);
756}
757
758static inline void btrfs_set_file_extent_disk_num_blocks(struct
759 btrfs_file_extent_item
760 *e, u64 val)
761{
762 e->disk_num_blocks = cpu_to_le64(val);
763}
764
765static inline u64 btrfs_file_extent_offset(struct btrfs_file_extent_item *e)
766{
767 return le64_to_cpu(e->offset);
768}
769
770static inline void btrfs_set_file_extent_offset(struct btrfs_file_extent_item
771 *e, u64 val)
772{
773 e->offset = cpu_to_le64(val);
774}
775
776static inline u64 btrfs_file_extent_num_blocks(struct btrfs_file_extent_item
777 *e)
778{
779 return le64_to_cpu(e->num_blocks);
780}
781
782static inline void btrfs_set_file_extent_num_blocks(struct
783 btrfs_file_extent_item *e,
784 u64 val)
785{
786 e->num_blocks = cpu_to_le64(val);
787}
788
Chris Masone20d96d2007-03-22 12:13:20 -0400789static inline struct btrfs_root *btrfs_sb(struct super_block *sb)
790{
791 return sb->s_fs_info;
792}
793
Chris Mason4beb1b82007-03-14 10:31:29 -0400794/* helper function to cast into the data area of the leaf. */
795#define btrfs_item_ptr(leaf, slot, type) \
Chris Mason123abc82007-03-14 14:14:43 -0400796 ((type *)(btrfs_leaf_data(leaf) + \
797 btrfs_item_offset((leaf)->items + (slot))))
Chris Mason4beb1b82007-03-14 10:31:29 -0400798
Chris Masone20d96d2007-03-22 12:13:20 -0400799struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masone089f052007-03-16 16:20:31 -0400800 struct btrfs_root *root);
801int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -0400802 struct buffer_head *buf);
Chris Masone089f052007-03-16 16:20:31 -0400803int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
804 *root, u64 blocknr, u64 num_blocks, int pin);
805int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
806 *root, struct btrfs_key *key, struct btrfs_path *p, int
807 ins_len, int cow);
Chris Mason234b63a2007-03-13 10:46:10 -0400808void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p);
809void btrfs_init_path(struct btrfs_path *p);
Chris Masone089f052007-03-16 16:20:31 -0400810int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
811 struct btrfs_path *path);
812int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
813 *root, struct btrfs_key *key, void *data, u32 data_size);
814int btrfs_insert_empty_item(struct btrfs_trans_handle *trans, struct btrfs_root
815 *root, struct btrfs_path *path, struct btrfs_key
816 *cpu_key, u32 data_size);
Chris Mason234b63a2007-03-13 10:46:10 -0400817int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path);
Chris Mason123abc82007-03-14 14:14:43 -0400818int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf);
Chris Masone089f052007-03-16 16:20:31 -0400819int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -0400820 *root, struct buffer_head *snap);
Chris Masone089f052007-03-16 16:20:31 -0400821int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
822 btrfs_root *root);
823int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
824 struct btrfs_key *key);
825int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
826 *root, struct btrfs_key *key, struct btrfs_root_item
827 *item);
828int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
829 *root, struct btrfs_key *key, struct btrfs_root_item
830 *item);
831int btrfs_find_last_root(struct btrfs_root *root, u64 objectid, struct
832 btrfs_root_item *item, struct btrfs_key *key);
833int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
834 *root, char *name, int name_len, u64 dir, u64
835 objectid, u8 type);
836int btrfs_lookup_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -0400837 *root, struct btrfs_path *path, u64 dir,
838 const char *name, int name_len, int mod);
Chris Mason1d4f6402007-03-15 15:18:43 -0400839int btrfs_match_dir_item_name(struct btrfs_root *root, struct btrfs_path *path,
840 char *name, int name_len);
Chris Mason9f5fae22007-03-20 14:38:32 -0400841int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
842 struct btrfs_root *fs_root,
843 u64 dirid, u64 *objectid);
844int btrfs_insert_inode_map(struct btrfs_trans_handle *trans,
845 struct btrfs_root *root,
846 u64 objectid, struct btrfs_key *location);
847int btrfs_lookup_inode_map(struct btrfs_trans_handle *trans,
848 struct btrfs_root *root, struct btrfs_path *path,
849 u64 objectid, int mod);
Chris Mason293ffd52007-03-20 15:57:25 -0400850int btrfs_insert_inode(struct btrfs_trans_handle *trans, struct btrfs_root
851 *root, u64 objectid, struct btrfs_inode_item
852 *inode_item);
853int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root
854 *root, struct btrfs_path *path, u64 objectid, int mod);
Chris Masoneb60cea2007-02-02 09:18:22 -0500855#endif