Btrfs: transaction handles everywhere
[deliverable/linux.git] / fs / btrfs / ctree.h
1 #ifndef __BTRFS__
2 #define __BTRFS__
3
4 #include "list.h"
5 #include "kerncompat.h"
6
7 struct btrfs_trans_handle;
8
9 #define BTRFS_MAGIC "_BtRfS_M"
10
11 #define BTRFS_ROOT_TREE_OBJECTID 1
12 #define BTRFS_EXTENT_TREE_OBJECTID 2
13 #define BTRFS_FS_TREE_OBJECTID 3
14
15 /*
16 * the key defines the order in the tree, and so it also defines (optimal)
17 * block layout. objectid corresonds to the inode number. The flags
18 * tells us things about the object, and is a kind of stream selector.
19 * so for a given inode, keys with flags of 1 might refer to the inode
20 * data, flags of 2 may point to file data in the btree and flags == 3
21 * may point to extents.
22 *
23 * offset is the starting byte offset for this key in the stream.
24 *
25 * btrfs_disk_key is in disk byte order. struct btrfs_key is always
26 * in cpu native order. Otherwise they are identical and their sizes
27 * should be the same (ie both packed)
28 */
29 struct btrfs_disk_key {
30 __le64 objectid;
31 __le32 flags;
32 __le64 offset;
33 } __attribute__ ((__packed__));
34
35 struct btrfs_key {
36 u64 objectid;
37 u32 flags;
38 u64 offset;
39 } __attribute__ ((__packed__));
40
41 /*
42 * every tree block (leaf or node) starts with this header.
43 */
44 struct btrfs_header {
45 u8 fsid[16]; /* FS specific uuid */
46 __le64 blocknr; /* which block this node is supposed to live in */
47 __le64 parentid; /* objectid of the tree root */
48 __le32 csum;
49 __le32 ham;
50 __le16 nritems;
51 __le16 flags;
52 /* generation flags to be added */
53 } __attribute__ ((__packed__));
54
55 #define BTRFS_MAX_LEVEL 8
56 #define BTRFS_NODEPTRS_PER_BLOCK(r) (((r)->blocksize - \
57 sizeof(struct btrfs_header)) / \
58 (sizeof(struct btrfs_disk_key) + sizeof(u64)))
59 #define __BTRFS_LEAF_DATA_SIZE(bs) ((bs) - sizeof(struct btrfs_header))
60 #define BTRFS_LEAF_DATA_SIZE(r) (__BTRFS_LEAF_DATA_SIZE(r->blocksize))
61
62 struct btrfs_buffer;
63 /*
64 * the super block basically lists the main trees of the FS
65 * it currently lacks any block count etc etc
66 */
67 struct btrfs_super_block {
68 u8 fsid[16]; /* FS specific uuid */
69 __le64 blocknr; /* this block number */
70 __le32 csum;
71 __le64 magic;
72 __le32 blocksize;
73 __le64 generation;
74 __le64 root;
75 __le64 total_blocks;
76 __le64 blocks_used;
77 } __attribute__ ((__packed__));
78
79 /*
80 * A leaf is full of items. offset and size tell us where to find
81 * the item in the leaf (relative to the start of the data area)
82 */
83 struct btrfs_item {
84 struct btrfs_disk_key key;
85 __le32 offset;
86 __le16 size;
87 } __attribute__ ((__packed__));
88
89 /*
90 * leaves have an item area and a data area:
91 * [item0, item1....itemN] [free space] [dataN...data1, data0]
92 *
93 * The data is separate from the items to get the keys closer together
94 * during searches.
95 */
96 struct btrfs_leaf {
97 struct btrfs_header header;
98 struct btrfs_item items[];
99 } __attribute__ ((__packed__));
100
101 /*
102 * all non-leaf blocks are nodes, they hold only keys and pointers to
103 * other blocks
104 */
105 struct btrfs_key_ptr {
106 struct btrfs_disk_key key;
107 __le64 blockptr;
108 } __attribute__ ((__packed__));
109
110 struct btrfs_node {
111 struct btrfs_header header;
112 struct btrfs_key_ptr ptrs[];
113 } __attribute__ ((__packed__));
114
115 /*
116 * btrfs_paths remember the path taken from the root down to the leaf.
117 * level 0 is always the leaf, and nodes[1...BTRFS_MAX_LEVEL] will point
118 * to any other levels that are present.
119 *
120 * The slots array records the index of the item or block pointer
121 * used while walking the tree.
122 */
123 struct btrfs_path {
124 struct btrfs_buffer *nodes[BTRFS_MAX_LEVEL];
125 int slots[BTRFS_MAX_LEVEL];
126 };
127
128 /*
129 * items in the extent btree are used to record the objectid of the
130 * owner of the block and the number of references
131 */
132 struct btrfs_extent_item {
133 __le32 refs;
134 __le64 owner;
135 } __attribute__ ((__packed__));
136
137 struct btrfs_inode_timespec {
138 __le32 sec;
139 __le32 nsec;
140 } __attribute__ ((__packed__));
141
142 /*
143 * there is no padding here on purpose. If you want to extent the inode,
144 * make a new item type
145 */
146 struct btrfs_inode_item {
147 __le64 generation;
148 __le64 size;
149 __le64 nblocks;
150 __le32 nlink;
151 __le32 uid;
152 __le32 gid;
153 __le32 mode;
154 __le32 rdev;
155 __le16 flags;
156 __le16 compat_flags;
157 struct btrfs_inode_timespec atime;
158 struct btrfs_inode_timespec ctime;
159 struct btrfs_inode_timespec mtime;
160 struct btrfs_inode_timespec otime;
161 } __attribute__ ((__packed__));
162
163 /* inline data is just a blob of bytes */
164 struct btrfs_inline_data_item {
165 u8 data;
166 } __attribute__ ((__packed__));
167
168 struct btrfs_dir_item {
169 __le64 objectid;
170 __le16 flags;
171 __le16 name_len;
172 u8 type;
173 } __attribute__ ((__packed__));
174
175 struct btrfs_root_item {
176 __le64 blocknr;
177 __le32 flags;
178 __le64 block_limit;
179 __le64 blocks_used;
180 __le32 refs;
181 };
182
183 /*
184 * in ram representation of the tree. extent_root is used for all allocations
185 * and for the extent tree extent_root root. current_insert is used
186 * only for the extent tree.
187 */
188 struct btrfs_root {
189 struct btrfs_buffer *node;
190 struct btrfs_buffer *commit_root;
191 struct btrfs_root *extent_root;
192 struct btrfs_root *tree_root;
193 struct btrfs_key current_insert;
194 struct btrfs_key last_insert;
195 int fp;
196 struct radix_tree_root cache_radix;
197 struct radix_tree_root pinned_radix;
198 struct list_head trans;
199 struct list_head cache;
200 int cache_size;
201 int ref_cows;
202 struct btrfs_root_item root_item;
203 struct btrfs_key root_key;
204 u32 blocksize;
205 struct btrfs_trans_handle *running_transaction;
206 };
207
208 /* the lower bits in the key flags defines the item type */
209 #define BTRFS_KEY_TYPE_MAX 256
210 #define BTRFS_KEY_TYPE_MASK (BTRFS_KEY_TYPE_MAX - 1)
211
212 /*
213 * inode items have the data typically returned from stat and store other
214 * info about object characteristics. There is one for every file and dir in
215 * the FS
216 */
217 #define BTRFS_INODE_ITEM_KEY 1
218
219 /*
220 * dir items are the name -> inode pointers in a directory. There is one
221 * for every name in a directory.
222 */
223 #define BTRFS_DIR_ITEM_KEY 2
224 /*
225 * inline data is file data that fits in the btree.
226 */
227 #define BTRFS_INLINE_DATA_KEY 3
228 /*
229 * extent data is for data that can't fit in the btree. It points to
230 * a (hopefully) huge chunk of disk
231 */
232 #define BTRFS_EXTENT_DATA_KEY 4
233 /*
234 * root items point to tree roots. There are typically in the root
235 * tree used by the super block to find all the other trees
236 */
237 #define BTRFS_ROOT_ITEM_KEY 5
238 /*
239 * extent items are in the extent map tree. These record which blocks
240 * are used, and how many references there are to each block
241 */
242 #define BTRFS_EXTENT_ITEM_KEY 6
243 /*
244 * string items are for debugging. They just store a short string of
245 * data in the FS
246 */
247 #define BTRFS_STRING_ITEM_KEY 7
248
249 static inline u64 btrfs_inode_generation(struct btrfs_inode_item *i)
250 {
251 return le64_to_cpu(i->generation);
252 }
253
254 static inline void btrfs_set_inode_generation(struct btrfs_inode_item *i,
255 u64 val)
256 {
257 i->generation = cpu_to_le64(val);
258 }
259
260 static inline u64 btrfs_inode_size(struct btrfs_inode_item *i)
261 {
262 return le64_to_cpu(i->size);
263 }
264
265 static inline void btrfs_set_inode_size(struct btrfs_inode_item *i, u64 val)
266 {
267 i->size = cpu_to_le64(val);
268 }
269
270 static inline u64 btrfs_inode_nblocks(struct btrfs_inode_item *i)
271 {
272 return le64_to_cpu(i->nblocks);
273 }
274
275 static inline void btrfs_set_inode_nblocks(struct btrfs_inode_item *i, u64 val)
276 {
277 i->nblocks = cpu_to_le64(val);
278 }
279
280 static inline u32 btrfs_inode_nlink(struct btrfs_inode_item *i)
281 {
282 return le32_to_cpu(i->nlink);
283 }
284
285 static inline void btrfs_set_inode_nlink(struct btrfs_inode_item *i, u32 val)
286 {
287 i->nlink = cpu_to_le32(val);
288 }
289
290 static inline u32 btrfs_inode_uid(struct btrfs_inode_item *i)
291 {
292 return le32_to_cpu(i->uid);
293 }
294
295 static inline void btrfs_set_inode_uid(struct btrfs_inode_item *i, u32 val)
296 {
297 i->uid = cpu_to_le32(val);
298 }
299
300 static inline u32 btrfs_inode_gid(struct btrfs_inode_item *i)
301 {
302 return le32_to_cpu(i->gid);
303 }
304
305 static inline void btrfs_set_inode_gid(struct btrfs_inode_item *i, u32 val)
306 {
307 i->gid = cpu_to_le32(val);
308 }
309
310 static inline u32 btrfs_inode_mode(struct btrfs_inode_item *i)
311 {
312 return le32_to_cpu(i->mode);
313 }
314
315 static inline void btrfs_set_inode_mode(struct btrfs_inode_item *i, u32 val)
316 {
317 i->mode = cpu_to_le32(val);
318 }
319
320 static inline u32 btrfs_inode_rdev(struct btrfs_inode_item *i)
321 {
322 return le32_to_cpu(i->rdev);
323 }
324
325 static inline void btrfs_set_inode_rdev(struct btrfs_inode_item *i, u32 val)
326 {
327 i->rdev = cpu_to_le32(val);
328 }
329
330 static inline u16 btrfs_inode_flags(struct btrfs_inode_item *i)
331 {
332 return le16_to_cpu(i->flags);
333 }
334
335 static inline void btrfs_set_inode_flags(struct btrfs_inode_item *i, u16 val)
336 {
337 i->flags = cpu_to_le16(val);
338 }
339
340 static inline u16 btrfs_inode_compat_flags(struct btrfs_inode_item *i)
341 {
342 return le16_to_cpu(i->compat_flags);
343 }
344
345 static inline void btrfs_set_inode_compat_flags(struct btrfs_inode_item *i,
346 u16 val)
347 {
348 i->compat_flags = cpu_to_le16(val);
349 }
350
351
352 static inline u64 btrfs_extent_owner(struct btrfs_extent_item *ei)
353 {
354 return le64_to_cpu(ei->owner);
355 }
356
357 static inline void btrfs_set_extent_owner(struct btrfs_extent_item *ei, u64 val)
358 {
359 ei->owner = cpu_to_le64(val);
360 }
361
362 static inline u32 btrfs_extent_refs(struct btrfs_extent_item *ei)
363 {
364 return le32_to_cpu(ei->refs);
365 }
366
367 static inline void btrfs_set_extent_refs(struct btrfs_extent_item *ei, u32 val)
368 {
369 ei->refs = cpu_to_le32(val);
370 }
371
372 static inline u64 btrfs_node_blockptr(struct btrfs_node *n, int nr)
373 {
374 return le64_to_cpu(n->ptrs[nr].blockptr);
375 }
376
377 static inline void btrfs_set_node_blockptr(struct btrfs_node *n, int nr,
378 u64 val)
379 {
380 n->ptrs[nr].blockptr = cpu_to_le64(val);
381 }
382
383 static inline u32 btrfs_item_offset(struct btrfs_item *item)
384 {
385 return le32_to_cpu(item->offset);
386 }
387
388 static inline void btrfs_set_item_offset(struct btrfs_item *item, u32 val)
389 {
390 item->offset = cpu_to_le32(val);
391 }
392
393 static inline u32 btrfs_item_end(struct btrfs_item *item)
394 {
395 return le32_to_cpu(item->offset) + le16_to_cpu(item->size);
396 }
397
398 static inline u16 btrfs_item_size(struct btrfs_item *item)
399 {
400 return le16_to_cpu(item->size);
401 }
402
403 static inline void btrfs_set_item_size(struct btrfs_item *item, u16 val)
404 {
405 item->size = cpu_to_le16(val);
406 }
407
408 static inline u64 btrfs_dir_objectid(struct btrfs_dir_item *d)
409 {
410 return le64_to_cpu(d->objectid);
411 }
412
413 static inline void btrfs_set_dir_objectid(struct btrfs_dir_item *d, u64 val)
414 {
415 d->objectid = cpu_to_le64(val);
416 }
417
418 static inline u16 btrfs_dir_flags(struct btrfs_dir_item *d)
419 {
420 return le16_to_cpu(d->flags);
421 }
422
423 static inline void btrfs_set_dir_flags(struct btrfs_dir_item *d, u16 val)
424 {
425 d->flags = cpu_to_le16(val);
426 }
427
428 static inline u8 btrfs_dir_type(struct btrfs_dir_item *d)
429 {
430 return d->type;
431 }
432
433 static inline void btrfs_set_dir_type(struct btrfs_dir_item *d, u8 val)
434 {
435 d->type = val;
436 }
437
438 static inline u16 btrfs_dir_name_len(struct btrfs_dir_item *d)
439 {
440 return le16_to_cpu(d->name_len);
441 }
442
443 static inline void btrfs_set_dir_name_len(struct btrfs_dir_item *d, u16 val)
444 {
445 d->name_len = cpu_to_le16(val);
446 }
447
448 static inline void btrfs_disk_key_to_cpu(struct btrfs_key *cpu,
449 struct btrfs_disk_key *disk)
450 {
451 cpu->offset = le64_to_cpu(disk->offset);
452 cpu->flags = le32_to_cpu(disk->flags);
453 cpu->objectid = le64_to_cpu(disk->objectid);
454 }
455
456 static inline void btrfs_cpu_key_to_disk(struct btrfs_disk_key *disk,
457 struct btrfs_key *cpu)
458 {
459 disk->offset = cpu_to_le64(cpu->offset);
460 disk->flags = cpu_to_le32(cpu->flags);
461 disk->objectid = cpu_to_le64(cpu->objectid);
462 }
463
464 static inline u64 btrfs_disk_key_objectid(struct btrfs_disk_key *disk)
465 {
466 return le64_to_cpu(disk->objectid);
467 }
468
469 static inline void btrfs_set_disk_key_objectid(struct btrfs_disk_key *disk,
470 u64 val)
471 {
472 disk->objectid = cpu_to_le64(val);
473 }
474
475 static inline u64 btrfs_disk_key_offset(struct btrfs_disk_key *disk)
476 {
477 return le64_to_cpu(disk->offset);
478 }
479
480 static inline void btrfs_set_disk_key_offset(struct btrfs_disk_key *disk,
481 u64 val)
482 {
483 disk->offset = cpu_to_le64(val);
484 }
485
486 static inline u32 btrfs_disk_key_flags(struct btrfs_disk_key *disk)
487 {
488 return le32_to_cpu(disk->flags);
489 }
490
491 static inline void btrfs_set_disk_key_flags(struct btrfs_disk_key *disk,
492 u32 val)
493 {
494 disk->flags = cpu_to_le32(val);
495 }
496
497 static inline u32 btrfs_key_type(struct btrfs_key *key)
498 {
499 return key->flags & BTRFS_KEY_TYPE_MASK;
500 }
501
502 static inline u32 btrfs_disk_key_type(struct btrfs_disk_key *key)
503 {
504 return le32_to_cpu(key->flags) & BTRFS_KEY_TYPE_MASK;
505 }
506
507 static inline void btrfs_set_key_type(struct btrfs_key *key, u32 type)
508 {
509 BUG_ON(type >= BTRFS_KEY_TYPE_MAX);
510 key->flags = (key->flags & ~((u64)BTRFS_KEY_TYPE_MASK)) | type;
511 }
512
513 static inline void btrfs_set_disk_key_type(struct btrfs_disk_key *key, u32 type)
514 {
515 u32 flags = btrfs_disk_key_flags(key);
516 BUG_ON(type >= BTRFS_KEY_TYPE_MAX);
517 flags = (flags & ~((u64)BTRFS_KEY_TYPE_MASK)) | type;
518 btrfs_set_disk_key_flags(key, flags);
519 }
520
521 static inline u64 btrfs_header_blocknr(struct btrfs_header *h)
522 {
523 return le64_to_cpu(h->blocknr);
524 }
525
526 static inline void btrfs_set_header_blocknr(struct btrfs_header *h, u64 blocknr)
527 {
528 h->blocknr = cpu_to_le64(blocknr);
529 }
530
531 static inline u64 btrfs_header_parentid(struct btrfs_header *h)
532 {
533 return le64_to_cpu(h->parentid);
534 }
535
536 static inline void btrfs_set_header_parentid(struct btrfs_header *h,
537 u64 parentid)
538 {
539 h->parentid = cpu_to_le64(parentid);
540 }
541
542 static inline u16 btrfs_header_nritems(struct btrfs_header *h)
543 {
544 return le16_to_cpu(h->nritems);
545 }
546
547 static inline void btrfs_set_header_nritems(struct btrfs_header *h, u16 val)
548 {
549 h->nritems = cpu_to_le16(val);
550 }
551
552 static inline u16 btrfs_header_flags(struct btrfs_header *h)
553 {
554 return le16_to_cpu(h->flags);
555 }
556
557 static inline void btrfs_set_header_flags(struct btrfs_header *h, u16 val)
558 {
559 h->flags = cpu_to_le16(val);
560 }
561
562 static inline int btrfs_header_level(struct btrfs_header *h)
563 {
564 return btrfs_header_flags(h) & (BTRFS_MAX_LEVEL - 1);
565 }
566
567 static inline void btrfs_set_header_level(struct btrfs_header *h, int level)
568 {
569 u16 flags;
570 BUG_ON(level > BTRFS_MAX_LEVEL);
571 flags = btrfs_header_flags(h) & ~(BTRFS_MAX_LEVEL - 1);
572 btrfs_set_header_flags(h, flags | level);
573 }
574
575 static inline int btrfs_is_leaf(struct btrfs_node *n)
576 {
577 return (btrfs_header_level(&n->header) == 0);
578 }
579
580 static inline u64 btrfs_root_blocknr(struct btrfs_root_item *item)
581 {
582 return le64_to_cpu(item->blocknr);
583 }
584
585 static inline void btrfs_set_root_blocknr(struct btrfs_root_item *item, u64 val)
586 {
587 item->blocknr = cpu_to_le64(val);
588 }
589
590 static inline u32 btrfs_root_refs(struct btrfs_root_item *item)
591 {
592 return le32_to_cpu(item->refs);
593 }
594
595 static inline void btrfs_set_root_refs(struct btrfs_root_item *item, u32 val)
596 {
597 item->refs = cpu_to_le32(val);
598 }
599
600 static inline u64 btrfs_super_blocknr(struct btrfs_super_block *s)
601 {
602 return le64_to_cpu(s->blocknr);
603 }
604
605 static inline void btrfs_set_super_blocknr(struct btrfs_super_block *s, u64 val)
606 {
607 s->blocknr = cpu_to_le64(val);
608 }
609
610 static inline u64 btrfs_super_root(struct btrfs_super_block *s)
611 {
612 return le64_to_cpu(s->root);
613 }
614
615 static inline void btrfs_set_super_root(struct btrfs_super_block *s, u64 val)
616 {
617 s->root = cpu_to_le64(val);
618 }
619
620 static inline u64 btrfs_super_total_blocks(struct btrfs_super_block *s)
621 {
622 return le64_to_cpu(s->total_blocks);
623 }
624
625 static inline void btrfs_set_super_total_blocks(struct btrfs_super_block *s,
626 u64 val)
627 {
628 s->total_blocks = cpu_to_le64(val);
629 }
630
631 static inline u64 btrfs_super_blocks_used(struct btrfs_super_block *s)
632 {
633 return le64_to_cpu(s->blocks_used);
634 }
635
636 static inline void btrfs_set_super_blocks_used(struct btrfs_super_block *s,
637 u64 val)
638 {
639 s->blocks_used = cpu_to_le64(val);
640 }
641
642 static inline u32 btrfs_super_blocksize(struct btrfs_super_block *s)
643 {
644 return le32_to_cpu(s->blocksize);
645 }
646
647 static inline void btrfs_set_super_blocksize(struct btrfs_super_block *s,
648 u32 val)
649 {
650 s->blocksize = cpu_to_le32(val);
651 }
652
653 static inline u8 *btrfs_leaf_data(struct btrfs_leaf *l)
654 {
655 return (u8 *)l->items;
656 }
657 /* helper function to cast into the data area of the leaf. */
658 #define btrfs_item_ptr(leaf, slot, type) \
659 ((type *)(btrfs_leaf_data(leaf) + \
660 btrfs_item_offset((leaf)->items + (slot))))
661
662 struct btrfs_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
663 struct btrfs_root *root);
664 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
665 struct btrfs_buffer *buf);
666 int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
667 *root, u64 blocknr, u64 num_blocks, int pin);
668 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
669 *root, struct btrfs_key *key, struct btrfs_path *p, int
670 ins_len, int cow);
671 void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p);
672 void btrfs_init_path(struct btrfs_path *p);
673 int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
674 struct btrfs_path *path);
675 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
676 *root, struct btrfs_key *key, void *data, u32 data_size);
677 int btrfs_insert_empty_item(struct btrfs_trans_handle *trans, struct btrfs_root
678 *root, struct btrfs_path *path, struct btrfs_key
679 *cpu_key, u32 data_size);
680 int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path);
681 int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf);
682 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
683 *root, struct btrfs_buffer *snap);
684 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
685 btrfs_root *root);
686 int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
687 struct btrfs_key *key);
688 int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
689 *root, struct btrfs_key *key, struct btrfs_root_item
690 *item);
691 int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
692 *root, struct btrfs_key *key, struct btrfs_root_item
693 *item);
694 int btrfs_find_last_root(struct btrfs_root *root, u64 objectid, struct
695 btrfs_root_item *item, struct btrfs_key *key);
696 int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
697 *root, char *name, int name_len, u64 dir, u64
698 objectid, u8 type);
699 int btrfs_lookup_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
700 *root, struct btrfs_path *path, u64 dir, char *name,
701 int name_len, int mod);
702 int btrfs_match_dir_item_name(struct btrfs_root *root, struct btrfs_path *path,
703 char *name, int name_len);
704 #endif
This page took 0.567609 seconds and 6 git commands to generate.