2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
23 #include "kerncompat.h"
24 #include "radix-tree.h"
25 #include "extent-cache.h"
26 #include "extent_map.h"
29 struct btrfs_trans_handle;
30 #define BTRFS_MAGIC "_B2RfS_M"
32 #define BTRFS_MAX_LEVEL 8
33 #define BTRFS_ROOT_TREE_OBJECTID 1ULL
34 #define BTRFS_EXTENT_TREE_OBJECTID 2ULL
35 #define BTRFS_FS_TREE_OBJECTID 3ULL
36 #define BTRFS_ROOT_TREE_DIR_OBJECTID 4ULL
37 #define BTRFS_FIRST_FREE_OBJECTID 256ULL
40 * we can actually store much bigger names, but lets not confuse the rest
43 #define BTRFS_NAME_LEN 255
45 /* 32 bytes in various csum fields */
46 #define BTRFS_CSUM_SIZE 32
47 /* four bytes for CRC32 */
48 #define BTRFS_CRC32_SIZE 4
49 #define BTRFS_EMPTY_DIR_SIZE 0
51 #define BTRFS_FT_UNKNOWN 0
52 #define BTRFS_FT_REG_FILE 1
53 #define BTRFS_FT_DIR 2
54 #define BTRFS_FT_CHRDEV 3
55 #define BTRFS_FT_BLKDEV 4
56 #define BTRFS_FT_FIFO 5
57 #define BTRFS_FT_SOCK 6
58 #define BTRFS_FT_SYMLINK 7
59 #define BTRFS_FT_XATTR 8
60 #define BTRFS_FT_MAX 9
63 * the key defines the order in the tree, and so it also defines (optimal)
64 * block layout. objectid corresonds to the inode number. The flags
65 * tells us things about the object, and is a kind of stream selector.
66 * so for a given inode, keys with flags of 1 might refer to the inode
67 * data, flags of 2 may point to file data in the btree and flags == 3
68 * may point to extents.
70 * offset is the starting byte offset for this key in the stream.
72 * btrfs_disk_key is in disk byte order. struct btrfs_key is always
73 * in cpu native order. Otherwise they are identical and their sizes
74 * should be the same (ie both packed)
76 struct btrfs_disk_key {
80 } __attribute__ ((__packed__));
86 } __attribute__ ((__packed__));
88 #define BTRFS_FSID_SIZE 16
90 * every tree block (leaf or node) starts with this header.
93 u8 csum[BTRFS_CSUM_SIZE];
94 u8 fsid[BTRFS_FSID_SIZE]; /* FS specific uuid */
95 __le64 bytenr; /* which block this node is supposed to live in */
101 } __attribute__ ((__packed__));
103 #define BTRFS_NODEPTRS_PER_BLOCK(r) (((r)->nodesize - \
104 sizeof(struct btrfs_header)) / \
105 sizeof(struct btrfs_key_ptr))
106 #define __BTRFS_LEAF_DATA_SIZE(bs) ((bs) - sizeof(struct btrfs_header))
107 #define BTRFS_LEAF_DATA_SIZE(r) (__BTRFS_LEAF_DATA_SIZE(r->leafsize))
108 #define BTRFS_MAX_INLINE_DATA_SIZE(r) (BTRFS_LEAF_DATA_SIZE(r) - \
109 sizeof(struct btrfs_item) - \
110 sizeof(struct btrfs_file_extent_item))
112 * the super block basically lists the main trees of the FS
113 * it currently lacks any block count etc etc
115 struct btrfs_super_block {
116 u8 csum[BTRFS_CSUM_SIZE];
117 /* the first 3 fields must match struct btrfs_header */
118 u8 fsid[BTRFS_FSID_SIZE]; /* FS specific uuid */
119 __le64 bytenr; /* this block number */
125 __le64 root_dir_objectid;
131 } __attribute__ ((__packed__));
134 * A leaf is full of items. offset and size tell us where to find
135 * the item in the leaf (relative to the start of the data area)
138 struct btrfs_disk_key key;
141 } __attribute__ ((__packed__));
144 * leaves have an item area and a data area:
145 * [item0, item1....itemN] [free space] [dataN...data1, data0]
147 * The data is separate from the items to get the keys closer together
151 struct btrfs_header header;
152 struct btrfs_item items[];
153 } __attribute__ ((__packed__));
156 * all non-leaf blocks are nodes, they hold only keys and pointers to
159 struct btrfs_key_ptr {
160 struct btrfs_disk_key key;
163 } __attribute__ ((__packed__));
166 struct btrfs_header header;
167 struct btrfs_key_ptr ptrs[];
168 } __attribute__ ((__packed__));
171 * btrfs_paths remember the path taken from the root down to the leaf.
172 * level 0 is always the leaf, and nodes[1...BTRFS_MAX_LEVEL] will point
173 * to any other levels that are present.
175 * The slots array records the index of the item or block pointer
176 * used while walking the tree.
179 struct extent_buffer *nodes[BTRFS_MAX_LEVEL];
180 int slots[BTRFS_MAX_LEVEL];
186 * items in the extent btree are used to record the objectid of the
187 * owner of the block and the number of references
189 struct btrfs_extent_item {
191 } __attribute__ ((__packed__));
193 struct btrfs_extent_ref {
198 } __attribute__ ((__packed__));
200 struct btrfs_inode_ref {
203 } __attribute__ ((__packed__));
205 struct btrfs_inode_timespec {
208 } __attribute__ ((__packed__));
211 * there is no padding here on purpose. If you want to extent the inode,
212 * make a new item type
214 struct btrfs_inode_item {
226 struct btrfs_inode_timespec atime;
227 struct btrfs_inode_timespec ctime;
228 struct btrfs_inode_timespec mtime;
229 struct btrfs_inode_timespec otime;
230 } __attribute__ ((__packed__));
232 struct btrfs_dir_item {
233 struct btrfs_disk_key location;
237 } __attribute__ ((__packed__));
239 struct btrfs_root_item {
240 struct btrfs_inode_item inode;
247 struct btrfs_disk_key drop_progress;
250 } __attribute__ ((__packed__));
252 #define BTRFS_FILE_EXTENT_REG 0
253 #define BTRFS_FILE_EXTENT_INLINE 1
255 struct btrfs_file_extent_item {
259 * disk space consumed by the extent, checksum blocks are included
263 __le64 disk_num_bytes;
265 * the logical offset in file blocks (no csums)
266 * this extent record is for. This allows a file extent to point
267 * into the middle of an existing extent on disk, sharing it
268 * between two snapshots (useful if some bytes in the middle of the
269 * extent have changed
273 * the logical number of file blocks (no csums included)
276 } __attribute__ ((__packed__));
278 struct btrfs_csum_item {
280 } __attribute__ ((__packed__));
282 /* tag for the radix tree of block groups in ram */
283 #define BTRFS_BLOCK_GROUP_SIZE (256 * 1024 * 1024)
285 #define BTRFS_BLOCK_GROUP_DATA 1
286 #define BTRFS_BLOCK_GROUP_MIXED 2
288 struct btrfs_block_group_item {
291 } __attribute__ ((__packed__));
293 struct btrfs_block_group_cache {
294 struct cache_extent cache;
295 struct btrfs_key key;
296 struct btrfs_block_group_item item;
301 struct btrfs_extent_ops {
302 int (*alloc_extent)(struct btrfs_root *root, u64 num_bytes,
303 u64 hint_byte, struct btrfs_key *ins);
304 int (*free_extent)(struct btrfs_root *root, u64 bytenr,
308 struct btrfs_fs_info {
309 u8 fsid[BTRFS_FSID_SIZE];
310 struct btrfs_root *fs_root;
311 struct btrfs_root *extent_root;
312 struct btrfs_root *tree_root;
314 struct extent_map_tree extent_cache;
315 struct extent_map_tree free_space_cache;
316 struct extent_map_tree block_group_cache;
317 struct extent_map_tree pending_tree;
318 struct extent_map_tree pinned_extents;
319 struct extent_map_tree del_pending;
320 struct extent_map_tree pending_del;
321 struct extent_map_tree extent_ins;
324 u64 last_trans_committed;
325 struct btrfs_trans_handle *running_transaction;
326 struct btrfs_super_block super_copy;
327 struct extent_buffer *sb_buffer;
328 struct mutex fs_mutex;
332 struct btrfs_extent_ops *extent_ops;
336 * in ram representation of the tree. extent_root is used for all allocations
337 * and for the extent tree extent_root root.
340 struct extent_buffer *node;
341 struct extent_buffer *commit_root;
342 struct btrfs_root_item root_item;
343 struct btrfs_key root_key;
344 struct btrfs_fs_info *fs_info;
348 /* data allocations are done in sectorsize units */
351 /* node allocations are done in nodesize units */
354 /* leaf allocations are done in leafsize units */
357 /* leaf allocations are done in leafsize units */
364 u64 last_inode_alloc;
368 * inode items have the data typically returned from stat and store other
369 * info about object characteristics. There is one for every file and dir in
372 #define BTRFS_INODE_ITEM_KEY 1
373 #define BTRFS_INODE_REF_KEY 2
374 #define BTRFS_XATTR_ITEM_KEY 8
376 /* reserve 3-15 close to the inode for later flexibility */
379 * dir items are the name -> inode pointers in a directory. There is one
380 * for every name in a directory.
382 #define BTRFS_DIR_ITEM_KEY 16
383 #define BTRFS_DIR_INDEX_KEY 17
385 * extent data is for file data
387 #define BTRFS_EXTENT_DATA_KEY 18
389 * csum items have the checksums for data in the extents
391 #define BTRFS_CSUM_ITEM_KEY 19
393 /* reserve 20-31 for other file stuff */
396 * root items point to tree roots. There are typically in the root
397 * tree used by the super block to find all the other trees
399 #define BTRFS_ROOT_ITEM_KEY 32
401 * extent items are in the extent map tree. These record which blocks
402 * are used, and how many references there are to each block
404 #define BTRFS_EXTENT_ITEM_KEY 33
405 #define BTRFS_EXTENT_REF_KEY 34
408 * block groups give us hints into the extent allocation trees. Which
409 * blocks are free etc etc
411 #define BTRFS_BLOCK_GROUP_ITEM_KEY 50
414 * string items are for debugging. They just store a short string of
417 #define BTRFS_STRING_ITEM_KEY 253
421 #define BTRFS_INODE_NODATASUM (1 << 0)
422 #define BTRFS_INODE_NODATACOW (1 << 1)
423 #define BTRFS_INODE_READONLY (1 << 2)
425 #define read_eb_member(eb, ptr, type, member, result) ( \
426 read_extent_buffer(eb, (char *)(result), \
427 ((unsigned long)(ptr)) + \
428 offsetof(type, member), \
429 sizeof(((type *)0)->member)))
431 #define write_eb_member(eb, ptr, type, member, result) ( \
432 write_extent_buffer(eb, (char *)(result), \
433 ((unsigned long)(ptr)) + \
434 offsetof(type, member), \
435 sizeof(((type *)0)->member)))
437 #define BTRFS_SETGET_HEADER_FUNCS(name, type, member, bits) \
438 static inline u##bits btrfs_##name(struct extent_buffer *eb) \
440 struct btrfs_header *h = (struct btrfs_header *)eb->data; \
441 return le##bits##_to_cpu(h->member); \
443 static inline void btrfs_set_##name(struct extent_buffer *eb, \
446 struct btrfs_header *h = (struct btrfs_header *)eb->data; \
447 h->member = cpu_to_le##bits(val); \
450 #define BTRFS_SETGET_FUNCS(name, type, member, bits) \
451 static inline u##bits btrfs_##name(struct extent_buffer *eb, \
454 unsigned long offset = (unsigned long)s + \
455 offsetof(type, member); \
456 __le##bits *tmp = (__le##bits *)(eb->data + offset); \
457 return le##bits##_to_cpu(*tmp); \
459 static inline void btrfs_set_##name(struct extent_buffer *eb, \
460 type *s, u##bits val) \
462 unsigned long offset = (unsigned long)s + \
463 offsetof(type, member); \
464 __le##bits *tmp = (__le##bits *)(eb->data + offset); \
465 *tmp = cpu_to_le##bits(val); \
468 #define BTRFS_SETGET_STACK_FUNCS(name, type, member, bits) \
469 static inline u##bits btrfs_##name(type *s) \
471 return le##bits##_to_cpu(s->member); \
473 static inline void btrfs_set_##name(type *s, u##bits val) \
475 s->member = cpu_to_le##bits(val); \
478 /* struct btrfs_block_group_item */
479 BTRFS_SETGET_STACK_FUNCS(block_group_used, struct btrfs_block_group_item,
481 BTRFS_SETGET_FUNCS(disk_block_group_used, struct btrfs_block_group_item,
484 /* struct btrfs_inode_ref */
485 BTRFS_SETGET_FUNCS(inode_ref_name_len, struct btrfs_inode_ref, name_len, 16);
487 /* struct btrfs_inode_item */
488 BTRFS_SETGET_FUNCS(inode_generation, struct btrfs_inode_item, generation, 64);
489 BTRFS_SETGET_FUNCS(inode_size, struct btrfs_inode_item, size, 64);
490 BTRFS_SETGET_FUNCS(inode_nblocks, struct btrfs_inode_item, nblocks, 64);
491 BTRFS_SETGET_FUNCS(inode_block_group, struct btrfs_inode_item, block_group, 64);
492 BTRFS_SETGET_FUNCS(inode_nlink, struct btrfs_inode_item, nlink, 32);
493 BTRFS_SETGET_FUNCS(inode_uid, struct btrfs_inode_item, uid, 32);
494 BTRFS_SETGET_FUNCS(inode_gid, struct btrfs_inode_item, gid, 32);
495 BTRFS_SETGET_FUNCS(inode_mode, struct btrfs_inode_item, mode, 32);
496 BTRFS_SETGET_FUNCS(inode_rdev, struct btrfs_inode_item, rdev, 32);
497 BTRFS_SETGET_FUNCS(inode_flags, struct btrfs_inode_item, flags, 16);
498 BTRFS_SETGET_FUNCS(inode_compat_flags, struct btrfs_inode_item,
501 BTRFS_SETGET_STACK_FUNCS(stack_inode_generation,
502 struct btrfs_inode_item, generation, 64);
503 BTRFS_SETGET_STACK_FUNCS(stack_inode_size,
504 struct btrfs_inode_item, size, 64);
505 BTRFS_SETGET_STACK_FUNCS(stack_inode_nblocks,
506 struct btrfs_inode_item, nblocks, 64);
507 BTRFS_SETGET_STACK_FUNCS(stack_inode_block_group,
508 struct btrfs_inode_item, block_group, 64);
509 BTRFS_SETGET_STACK_FUNCS(stack_inode_nlink,
510 struct btrfs_inode_item, nlink, 32);
511 BTRFS_SETGET_STACK_FUNCS(stack_inode_uid,
512 struct btrfs_inode_item, uid, 32);
513 BTRFS_SETGET_STACK_FUNCS(stack_inode_gid,
514 struct btrfs_inode_item, gid, 32);
515 BTRFS_SETGET_STACK_FUNCS(stack_inode_mode,
516 struct btrfs_inode_item, mode, 32);
517 BTRFS_SETGET_STACK_FUNCS(stack_inode_rdev,
518 struct btrfs_inode_item, rdev, 32);
519 BTRFS_SETGET_STACK_FUNCS(stack_inode_flags,
520 struct btrfs_inode_item, flags, 16);
521 BTRFS_SETGET_STACK_FUNCS(stack_inode_compat_flags,
522 struct btrfs_inode_item, compat_flags, 16);
524 static inline struct btrfs_inode_timespec *
525 btrfs_inode_atime(struct btrfs_inode_item *inode_item)
527 unsigned long ptr = (unsigned long)inode_item;
528 ptr += offsetof(struct btrfs_inode_item, atime);
529 return (struct btrfs_inode_timespec *)ptr;
532 static inline struct btrfs_inode_timespec *
533 btrfs_inode_mtime(struct btrfs_inode_item *inode_item)
535 unsigned long ptr = (unsigned long)inode_item;
536 ptr += offsetof(struct btrfs_inode_item, mtime);
537 return (struct btrfs_inode_timespec *)ptr;
540 static inline struct btrfs_inode_timespec *
541 btrfs_inode_ctime(struct btrfs_inode_item *inode_item)
543 unsigned long ptr = (unsigned long)inode_item;
544 ptr += offsetof(struct btrfs_inode_item, ctime);
545 return (struct btrfs_inode_timespec *)ptr;
548 static inline struct btrfs_inode_timespec *
549 btrfs_inode_otime(struct btrfs_inode_item *inode_item)
551 unsigned long ptr = (unsigned long)inode_item;
552 ptr += offsetof(struct btrfs_inode_item, otime);
553 return (struct btrfs_inode_timespec *)ptr;
556 BTRFS_SETGET_FUNCS(timespec_sec, struct btrfs_inode_timespec, sec, 64);
557 BTRFS_SETGET_FUNCS(timespec_nsec, struct btrfs_inode_timespec, nsec, 32);
558 BTRFS_SETGET_STACK_FUNCS(stack_timespec_sec, struct btrfs_inode_timespec,
560 BTRFS_SETGET_STACK_FUNCS(stack_timespec_nsec, struct btrfs_inode_timespec,
563 /* struct btrfs_extent_item */
564 BTRFS_SETGET_FUNCS(extent_refs, struct btrfs_extent_item, refs, 32);
566 /* struct btrfs_extent_ref */
567 BTRFS_SETGET_FUNCS(ref_root, struct btrfs_extent_ref, root, 64);
568 BTRFS_SETGET_FUNCS(ref_generation, struct btrfs_extent_ref, generation, 64);
569 BTRFS_SETGET_FUNCS(ref_objectid, struct btrfs_extent_ref, objectid, 64);
570 BTRFS_SETGET_FUNCS(ref_offset, struct btrfs_extent_ref, offset, 64);
572 BTRFS_SETGET_STACK_FUNCS(stack_ref_root, struct btrfs_extent_ref, root, 64);
573 BTRFS_SETGET_STACK_FUNCS(stack_ref_generation, struct btrfs_extent_ref,
575 BTRFS_SETGET_STACK_FUNCS(stack_ref_objectid, struct btrfs_extent_ref,
577 BTRFS_SETGET_STACK_FUNCS(stack_ref_offset, struct btrfs_extent_ref, offset, 64);
579 BTRFS_SETGET_STACK_FUNCS(stack_extent_refs, struct btrfs_extent_item,
582 /* struct btrfs_node */
583 BTRFS_SETGET_FUNCS(key_blockptr, struct btrfs_key_ptr, blockptr, 64);
584 BTRFS_SETGET_FUNCS(key_generation, struct btrfs_key_ptr, generation, 64);
586 static inline u64 btrfs_node_blockptr(struct extent_buffer *eb, int nr)
589 ptr = offsetof(struct btrfs_node, ptrs) +
590 sizeof(struct btrfs_key_ptr) * nr;
591 return btrfs_key_blockptr(eb, (struct btrfs_key_ptr *)ptr);
594 static inline void btrfs_set_node_blockptr(struct extent_buffer *eb,
598 ptr = offsetof(struct btrfs_node, ptrs) +
599 sizeof(struct btrfs_key_ptr) * nr;
600 btrfs_set_key_blockptr(eb, (struct btrfs_key_ptr *)ptr, val);
603 static inline u64 btrfs_node_ptr_generation(struct extent_buffer *eb, int nr)
606 ptr = offsetof(struct btrfs_node, ptrs) +
607 sizeof(struct btrfs_key_ptr) * nr;
608 return btrfs_key_generation(eb, (struct btrfs_key_ptr *)ptr);
611 static inline void btrfs_set_node_ptr_generation(struct extent_buffer *eb,
615 ptr = offsetof(struct btrfs_node, ptrs) +
616 sizeof(struct btrfs_key_ptr) * nr;
617 btrfs_set_key_generation(eb, (struct btrfs_key_ptr *)ptr, val);
620 static inline unsigned long btrfs_node_key_ptr_offset(int nr)
622 return offsetof(struct btrfs_node, ptrs) +
623 sizeof(struct btrfs_key_ptr) * nr;
626 static inline void btrfs_node_key(struct extent_buffer *eb,
627 struct btrfs_disk_key *disk_key, int nr)
630 ptr = btrfs_node_key_ptr_offset(nr);
631 read_eb_member(eb, (struct btrfs_key_ptr *)ptr,
632 struct btrfs_key_ptr, key, disk_key);
635 static inline void btrfs_set_node_key(struct extent_buffer *eb,
636 struct btrfs_disk_key *disk_key, int nr)
639 ptr = btrfs_node_key_ptr_offset(nr);
640 write_eb_member(eb, (struct btrfs_key_ptr *)ptr,
641 struct btrfs_key_ptr, key, disk_key);
644 /* struct btrfs_item */
645 BTRFS_SETGET_FUNCS(item_offset, struct btrfs_item, offset, 32);
646 BTRFS_SETGET_FUNCS(item_size, struct btrfs_item, size, 32);
648 static inline unsigned long btrfs_item_nr_offset(int nr)
650 return offsetof(struct btrfs_leaf, items) +
651 sizeof(struct btrfs_item) * nr;
654 static inline struct btrfs_item *btrfs_item_nr(struct extent_buffer *eb,
657 return (struct btrfs_item *)btrfs_item_nr_offset(nr);
660 static inline u32 btrfs_item_end(struct extent_buffer *eb,
661 struct btrfs_item *item)
663 return btrfs_item_offset(eb, item) + btrfs_item_size(eb, item);
666 static inline u32 btrfs_item_end_nr(struct extent_buffer *eb, int nr)
668 return btrfs_item_end(eb, btrfs_item_nr(eb, nr));
671 static inline u32 btrfs_item_offset_nr(struct extent_buffer *eb, int nr)
673 return btrfs_item_offset(eb, btrfs_item_nr(eb, nr));
676 static inline u32 btrfs_item_size_nr(struct extent_buffer *eb, int nr)
678 return btrfs_item_size(eb, btrfs_item_nr(eb, nr));
681 static inline void btrfs_item_key(struct extent_buffer *eb,
682 struct btrfs_disk_key *disk_key, int nr)
684 struct btrfs_item *item = btrfs_item_nr(eb, nr);
685 read_eb_member(eb, item, struct btrfs_item, key, disk_key);
688 static inline void btrfs_set_item_key(struct extent_buffer *eb,
689 struct btrfs_disk_key *disk_key, int nr)
691 struct btrfs_item *item = btrfs_item_nr(eb, nr);
692 write_eb_member(eb, item, struct btrfs_item, key, disk_key);
695 /* struct btrfs_dir_item */
696 BTRFS_SETGET_FUNCS(dir_data_len, struct btrfs_dir_item, data_len, 16);
697 BTRFS_SETGET_FUNCS(dir_type, struct btrfs_dir_item, type, 8);
698 BTRFS_SETGET_FUNCS(dir_name_len, struct btrfs_dir_item, name_len, 16);
700 static inline void btrfs_dir_item_key(struct extent_buffer *eb,
701 struct btrfs_dir_item *item,
702 struct btrfs_disk_key *key)
704 read_eb_member(eb, item, struct btrfs_dir_item, location, key);
707 static inline void btrfs_set_dir_item_key(struct extent_buffer *eb,
708 struct btrfs_dir_item *item,
709 struct btrfs_disk_key *key)
711 write_eb_member(eb, item, struct btrfs_dir_item, location, key);
714 /* struct btrfs_disk_key */
715 BTRFS_SETGET_STACK_FUNCS(disk_key_objectid, struct btrfs_disk_key,
717 BTRFS_SETGET_STACK_FUNCS(disk_key_offset, struct btrfs_disk_key, offset, 64);
718 BTRFS_SETGET_STACK_FUNCS(disk_key_type, struct btrfs_disk_key, type, 8);
720 static inline void btrfs_disk_key_to_cpu(struct btrfs_key *cpu,
721 struct btrfs_disk_key *disk)
723 cpu->offset = le64_to_cpu(disk->offset);
724 cpu->type = disk->type;
725 cpu->objectid = le64_to_cpu(disk->objectid);
728 static inline void btrfs_cpu_key_to_disk(struct btrfs_disk_key *disk,
729 struct btrfs_key *cpu)
731 disk->offset = cpu_to_le64(cpu->offset);
732 disk->type = cpu->type;
733 disk->objectid = cpu_to_le64(cpu->objectid);
736 static inline void btrfs_node_key_to_cpu(struct extent_buffer *eb,
737 struct btrfs_key *key, int nr)
739 struct btrfs_disk_key disk_key;
740 btrfs_node_key(eb, &disk_key, nr);
741 btrfs_disk_key_to_cpu(key, &disk_key);
744 static inline void btrfs_item_key_to_cpu(struct extent_buffer *eb,
745 struct btrfs_key *key, int nr)
747 struct btrfs_disk_key disk_key;
748 btrfs_item_key(eb, &disk_key, nr);
749 btrfs_disk_key_to_cpu(key, &disk_key);
752 static inline void btrfs_dir_item_key_to_cpu(struct extent_buffer *eb,
753 struct btrfs_dir_item *item,
754 struct btrfs_key *key)
756 struct btrfs_disk_key disk_key;
757 btrfs_dir_item_key(eb, item, &disk_key);
758 btrfs_disk_key_to_cpu(key, &disk_key);
762 static inline u8 btrfs_key_type(struct btrfs_key *key)
767 static inline void btrfs_set_key_type(struct btrfs_key *key, u8 val)
772 /* struct btrfs_header */
773 BTRFS_SETGET_HEADER_FUNCS(header_bytenr, struct btrfs_header, bytenr, 64);
774 BTRFS_SETGET_HEADER_FUNCS(header_generation, struct btrfs_header,
776 BTRFS_SETGET_HEADER_FUNCS(header_owner, struct btrfs_header, owner, 64);
777 BTRFS_SETGET_HEADER_FUNCS(header_nritems, struct btrfs_header, nritems, 32);
778 BTRFS_SETGET_HEADER_FUNCS(header_flags, struct btrfs_header, flags, 16);
779 BTRFS_SETGET_HEADER_FUNCS(header_level, struct btrfs_header, level, 8);
781 static inline u8 *btrfs_header_fsid(struct extent_buffer *eb)
783 unsigned long ptr = offsetof(struct btrfs_header, fsid);
787 static inline u8 *btrfs_super_fsid(struct extent_buffer *eb)
789 unsigned long ptr = offsetof(struct btrfs_super_block, fsid);
793 static inline u8 *btrfs_header_csum(struct extent_buffer *eb)
795 unsigned long ptr = offsetof(struct btrfs_header, csum);
799 static inline struct btrfs_node *btrfs_buffer_node(struct extent_buffer *eb)
804 static inline struct btrfs_leaf *btrfs_buffer_leaf(struct extent_buffer *eb)
809 static inline struct btrfs_header *btrfs_buffer_header(struct extent_buffer *eb)
814 static inline int btrfs_is_leaf(struct extent_buffer *eb)
816 return (btrfs_header_level(eb) == 0);
819 /* struct btrfs_root_item */
820 BTRFS_SETGET_FUNCS(disk_root_refs, struct btrfs_root_item, refs, 32);
821 BTRFS_SETGET_FUNCS(disk_root_bytenr, struct btrfs_root_item, bytenr, 64);
822 BTRFS_SETGET_FUNCS(disk_root_level, struct btrfs_root_item, level, 8);
824 BTRFS_SETGET_STACK_FUNCS(root_bytenr, struct btrfs_root_item, bytenr, 64);
825 BTRFS_SETGET_STACK_FUNCS(root_level, struct btrfs_root_item, level, 8);
826 BTRFS_SETGET_STACK_FUNCS(root_dirid, struct btrfs_root_item, root_dirid, 64);
827 BTRFS_SETGET_STACK_FUNCS(root_refs, struct btrfs_root_item, refs, 32);
828 BTRFS_SETGET_STACK_FUNCS(root_flags, struct btrfs_root_item, flags, 32);
829 BTRFS_SETGET_STACK_FUNCS(root_used, struct btrfs_root_item, bytes_used, 64);
830 BTRFS_SETGET_STACK_FUNCS(root_limit, struct btrfs_root_item, byte_limit, 64);
832 /* struct btrfs_super_block */
833 BTRFS_SETGET_STACK_FUNCS(super_bytenr, struct btrfs_super_block, bytenr, 64);
834 BTRFS_SETGET_STACK_FUNCS(super_generation, struct btrfs_super_block,
836 BTRFS_SETGET_STACK_FUNCS(super_root, struct btrfs_super_block, root, 64);
837 BTRFS_SETGET_STACK_FUNCS(super_root_level, struct btrfs_super_block,
839 BTRFS_SETGET_STACK_FUNCS(super_total_bytes, struct btrfs_super_block,
841 BTRFS_SETGET_STACK_FUNCS(super_bytes_used, struct btrfs_super_block,
843 BTRFS_SETGET_STACK_FUNCS(super_sectorsize, struct btrfs_super_block,
845 BTRFS_SETGET_STACK_FUNCS(super_nodesize, struct btrfs_super_block,
847 BTRFS_SETGET_STACK_FUNCS(super_leafsize, struct btrfs_super_block,
849 BTRFS_SETGET_STACK_FUNCS(super_stripesize, struct btrfs_super_block,
851 BTRFS_SETGET_STACK_FUNCS(super_root_dir, struct btrfs_super_block,
852 root_dir_objectid, 64);
854 static inline unsigned long btrfs_leaf_data(struct extent_buffer *l)
856 return offsetof(struct btrfs_leaf, items);
859 /* struct btrfs_file_extent_item */
860 BTRFS_SETGET_FUNCS(file_extent_type, struct btrfs_file_extent_item, type, 8);
862 static inline unsigned long btrfs_file_extent_inline_start(struct
863 btrfs_file_extent_item *e)
865 unsigned long offset = (unsigned long)e;
866 offset += offsetof(struct btrfs_file_extent_item, disk_bytenr);
870 static inline u32 btrfs_file_extent_calc_inline_size(u32 datasize)
872 return offsetof(struct btrfs_file_extent_item, disk_bytenr) + datasize;
875 static inline u32 btrfs_file_extent_inline_len(struct extent_buffer *eb,
876 struct btrfs_item *e)
878 unsigned long offset;
879 offset = offsetof(struct btrfs_file_extent_item, disk_bytenr);
880 return btrfs_item_size(eb, e) - offset;
883 BTRFS_SETGET_FUNCS(file_extent_disk_bytenr, struct btrfs_file_extent_item,
885 BTRFS_SETGET_FUNCS(file_extent_generation, struct btrfs_file_extent_item,
887 BTRFS_SETGET_FUNCS(file_extent_disk_num_bytes, struct btrfs_file_extent_item,
889 BTRFS_SETGET_FUNCS(file_extent_offset, struct btrfs_file_extent_item,
891 BTRFS_SETGET_FUNCS(file_extent_num_bytes, struct btrfs_file_extent_item,
894 static inline u32 btrfs_level_size(struct btrfs_root *root, int level) {
896 return root->leafsize;
897 return root->nodesize;
900 /* helper function to cast into the data area of the leaf. */
901 #define btrfs_item_ptr(leaf, slot, type) \
902 ((type *)(btrfs_leaf_data(leaf) + \
903 btrfs_item_offset_nr(leaf, slot)))
905 #define btrfs_item_ptr_offset(leaf, slot) \
906 ((unsigned long)(btrfs_leaf_data(leaf) + \
907 btrfs_item_offset_nr(leaf, slot)))
910 u32 btrfs_count_snapshots_in_path(struct btrfs_root *root,
911 struct btrfs_path *count_path,
913 int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
914 struct btrfs_root *root);
915 int btrfs_copy_pinned(struct btrfs_root *root, struct extent_map_tree *copy);
916 struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
919 struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
920 struct btrfs_block_group_cache
921 *hint, u64 search_start,
922 int data, int owner);
923 int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
924 struct btrfs_root *root, u64 owner_objectid);
925 struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
926 struct btrfs_root *root, u32 size,
928 u64 hint, u64 empty_size);
929 struct extent_buffer *__btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
930 struct btrfs_root *root,
938 int btrfs_grow_extent_tree(struct btrfs_trans_handle *trans,
939 struct btrfs_root *root, u64 new_size);
940 int btrfs_shrink_extent_tree(struct btrfs_root *root, u64 new_size);
941 int btrfs_insert_extent_backref(struct btrfs_trans_handle *trans,
942 struct btrfs_root *root,
943 struct btrfs_path *path, u64 bytenr,
944 u64 root_objectid, u64 ref_generation,
945 u64 owner, u64 owner_offset);
946 int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
947 struct btrfs_root *root,
948 u64 num_bytes, u64 root_objectid, u64 ref_generation,
949 u64 owner, u64 owner_offset,
950 u64 empty_size, u64 hint_byte,
951 u64 search_end, struct btrfs_key *ins, int data);
952 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
953 struct extent_buffer *buf);
954 int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
955 *root, u64 bytenr, u64 num_bytes,
956 u64 root_objectid, u64 ref_generation,
957 u64 owner_objectid, u64 owner_offset, int pin);
958 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
959 struct btrfs_root *root,
960 struct extent_map_tree *unpin);
961 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
962 struct btrfs_root *root,
963 u64 bytenr, u64 num_bytes,
964 u64 root_objectid, u64 ref_generation,
965 u64 owner, u64 owner_offset);
966 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
967 struct btrfs_root *root);
968 int btrfs_free_block_groups(struct btrfs_fs_info *info);
969 int btrfs_read_block_groups(struct btrfs_root *root);
970 int btrfs_make_block_groups(struct btrfs_trans_handle *trans,
971 struct btrfs_root *root);
972 u64 btrfs_hash_extent_ref(u64 root_objectid, u64 ref_generation,
973 u64 owner, u64 owner_offset);
974 int btrfs_update_block_group(struct btrfs_trans_handle *trans,
975 struct btrfs_root *root, u64 bytenr, u64 num,
976 int alloc, int mark_free, int data);
978 int btrfs_comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2);
979 int btrfs_cow_block(struct btrfs_trans_handle *trans,
980 struct btrfs_root *root, struct extent_buffer *buf,
981 struct extent_buffer *parent, int parent_slot,
982 struct extent_buffer **cow_ret);
983 int btrfs_copy_root(struct btrfs_trans_handle *trans,
984 struct btrfs_root *root,
985 struct extent_buffer *buf,
986 struct extent_buffer **cow_ret, u64 new_root_objectid);
987 int btrfs_extend_item(struct btrfs_trans_handle *trans, struct btrfs_root
988 *root, struct btrfs_path *path, u32 data_size);
989 int btrfs_truncate_item(struct btrfs_trans_handle *trans,
990 struct btrfs_root *root,
991 struct btrfs_path *path,
992 u32 new_size, int from_end);
993 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
994 *root, struct btrfs_key *key, struct btrfs_path *p, int
996 int btrfs_realloc_node(struct btrfs_trans_handle *trans,
997 struct btrfs_root *root, struct extent_buffer *parent,
998 int start_slot, int cache_only, u64 *last_ret,
999 struct btrfs_key *progress);
1000 void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p);
1001 struct btrfs_path *btrfs_alloc_path(void);
1002 void btrfs_free_path(struct btrfs_path *p);
1003 void btrfs_init_path(struct btrfs_path *p);
1004 int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1005 struct btrfs_path *path);
1006 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
1007 *root, struct btrfs_key *key, void *data, u32 data_size);
1008 int btrfs_insert_empty_item(struct btrfs_trans_handle *trans, struct btrfs_root
1009 *root, struct btrfs_path *path, struct btrfs_key
1010 *cpu_key, u32 data_size);
1011 int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path);
1012 int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path);
1013 int btrfs_leaf_free_space(struct btrfs_root *root, struct extent_buffer *leaf);
1014 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
1019 int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1020 struct btrfs_key *key);
1021 int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
1022 *root, struct btrfs_key *key, struct btrfs_root_item
1024 int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
1025 *root, struct btrfs_key *key, struct btrfs_root_item
1027 int btrfs_find_last_root(struct btrfs_root *root, u64 objectid, struct
1028 btrfs_root_item *item, struct btrfs_key *key);
1029 int btrfs_find_dead_roots(struct btrfs_root *root, u64 objectid,
1030 struct btrfs_root *latest_root);
1032 int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
1033 *root, const char *name, int name_len, u64 dir,
1034 struct btrfs_key *location, u8 type);
1035 struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans,
1036 struct btrfs_root *root,
1037 struct btrfs_path *path, u64 dir,
1038 const char *name, int name_len,
1040 struct btrfs_dir_item *
1041 btrfs_lookup_dir_index_item(struct btrfs_trans_handle *trans,
1042 struct btrfs_root *root,
1043 struct btrfs_path *path, u64 dir,
1044 u64 objectid, const char *name, int name_len,
1046 struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_root *root,
1047 struct btrfs_path *path,
1048 const char *name, int name_len);
1049 int btrfs_delete_one_dir_name(struct btrfs_trans_handle *trans,
1050 struct btrfs_root *root,
1051 struct btrfs_path *path,
1052 struct btrfs_dir_item *di);
1053 int btrfs_insert_xattr_item(struct btrfs_trans_handle *trans,
1054 struct btrfs_root *root, const char *name,
1055 u16 name_len, const void *data, u16 data_len,
1057 struct btrfs_dir_item *btrfs_lookup_xattr(struct btrfs_trans_handle *trans,
1058 struct btrfs_root *root,
1059 struct btrfs_path *path, u64 dir,
1060 const char *name, u16 name_len,
1063 int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
1064 struct btrfs_root *fs_root,
1065 u64 dirid, u64 *objectid);
1066 int btrfs_find_highest_inode(struct btrfs_root *fs_root, u64 *objectid);
1069 int btrfs_insert_inode_ref(struct btrfs_trans_handle *trans,
1070 struct btrfs_root *root,
1071 const char *name, int name_len,
1072 u64 inode_objectid, u64 ref_objectid);
1073 int btrfs_del_inode_ref(struct btrfs_trans_handle *trans,
1074 struct btrfs_root *root,
1075 const char *name, int name_len,
1076 u64 inode_objectid, u64 ref_objectid);
1077 int btrfs_insert_empty_inode(struct btrfs_trans_handle *trans,
1078 struct btrfs_root *root,
1079 struct btrfs_path *path, u64 objectid);
1080 int btrfs_insert_inode(struct btrfs_trans_handle *trans, struct btrfs_root
1081 *root, u64 objectid, struct btrfs_inode_item
1083 int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root
1084 *root, struct btrfs_path *path,
1085 struct btrfs_key *location, int mod);
1088 int btrfs_insert_file_extent(struct btrfs_trans_handle *trans,
1089 struct btrfs_root *root,
1090 u64 objectid, u64 pos, u64 offset,
1093 int btrfs_insert_inline_extent(struct btrfs_trans_handle *trans,
1094 struct btrfs_root *root, u64 objectid,
1095 u64 offset, char *buffer, size_t size);
1096 int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
1097 struct btrfs_root *root,
1098 struct btrfs_path *path, u64 objectid,
1099 u64 bytenr, int mod);
1100 int btrfs_csum_file_block(struct btrfs_trans_handle *trans,
1101 struct btrfs_root *root,
1102 struct btrfs_inode_item *inode,
1103 u64 objectid, u64 offset,
1104 char *data, size_t len);
1105 struct btrfs_csum_item *btrfs_lookup_csum(struct btrfs_trans_handle *trans,
1106 struct btrfs_root *root,
1107 struct btrfs_path *path,
1108 u64 objectid, u64 offset,
1110 int btrfs_csum_truncate(struct btrfs_trans_handle *trans,
1111 struct btrfs_root *root, struct btrfs_path *path,