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.
19 #ifndef __BTRFS_CTREE__
20 #define __BTRFS_CTREE__
22 #include <linux/version.h>
24 #include <linux/highmem.h>
26 #include <linux/completion.h>
27 #include <linux/backing-dev.h>
28 #include <linux/wait.h>
29 #include <asm/kmap_types.h>
30 #include "bit-radix.h"
31 #include "extent_io.h"
32 #include "extent_map.h"
33 #include "async-thread.h"
35 struct btrfs_trans_handle;
36 struct btrfs_transaction;
37 extern struct kmem_cache *btrfs_trans_handle_cachep;
38 extern struct kmem_cache *btrfs_transaction_cachep;
39 extern struct kmem_cache *btrfs_bit_radix_cachep;
40 extern struct kmem_cache *btrfs_path_cachep;
41 struct btrfs_ordered_sum;
43 #define BTRFS_MAGIC "_B9RfS_M"
45 #define BTRFS_ACL_NOT_CACHED ((void *)-1)
48 # define BTRFS_MAX_LEVEL 7
50 # define BTRFS_MAX_LEVEL 8
53 /* holds pointers to all of the tree roots */
54 #define BTRFS_ROOT_TREE_OBJECTID 1ULL
56 /* stores information about which extents are in use, and reference counts */
57 #define BTRFS_EXTENT_TREE_OBJECTID 2ULL
60 * chunk tree stores translations from logical -> physical block numbering
61 * the super block points to the chunk tree
63 #define BTRFS_CHUNK_TREE_OBJECTID 3ULL
66 * stores information about which areas of a given device are in use.
67 * one per device. The tree of tree roots points to the device tree
69 #define BTRFS_DEV_TREE_OBJECTID 4ULL
71 /* one per subvolume, storing files and directories */
72 #define BTRFS_FS_TREE_OBJECTID 5ULL
74 /* directory objectid inside the root tree */
75 #define BTRFS_ROOT_TREE_DIR_OBJECTID 6ULL
77 /* orhpan objectid for tracking unlinked/truncated files */
78 #define BTRFS_ORPHAN_OBJECTID -5ULL
80 /* does write ahead logging to speed up fsyncs */
81 #define BTRFS_TREE_LOG_OBJECTID -6ULL
82 #define BTRFS_TREE_LOG_FIXUP_OBJECTID -7ULL
84 /* dummy objectid represents multiple objectids */
85 #define BTRFS_MULTIPLE_OBJECTIDS -255ULL
88 * All files have objectids in this range.
90 #define BTRFS_FIRST_FREE_OBJECTID 256ULL
91 #define BTRFS_LAST_FREE_OBJECTID -256ULL
92 #define BTRFS_FIRST_CHUNK_TREE_OBJECTID 256ULL
96 * the device items go into the chunk tree. The key is in the form
97 * [ 1 BTRFS_DEV_ITEM_KEY device_id ]
99 #define BTRFS_DEV_ITEMS_OBJECTID 1ULL
102 * we can actually store much bigger names, but lets not confuse the rest
105 #define BTRFS_NAME_LEN 255
107 /* 32 bytes in various csum fields */
108 #define BTRFS_CSUM_SIZE 32
109 /* four bytes for CRC32 */
110 #define BTRFS_CRC32_SIZE 4
111 #define BTRFS_EMPTY_DIR_SIZE 0
113 #define BTRFS_FT_UNKNOWN 0
114 #define BTRFS_FT_REG_FILE 1
115 #define BTRFS_FT_DIR 2
116 #define BTRFS_FT_CHRDEV 3
117 #define BTRFS_FT_BLKDEV 4
118 #define BTRFS_FT_FIFO 5
119 #define BTRFS_FT_SOCK 6
120 #define BTRFS_FT_SYMLINK 7
121 #define BTRFS_FT_XATTR 8
122 #define BTRFS_FT_MAX 9
125 * the key defines the order in the tree, and so it also defines (optimal)
126 * block layout. objectid corresonds to the inode number. The flags
127 * tells us things about the object, and is a kind of stream selector.
128 * so for a given inode, keys with flags of 1 might refer to the inode
129 * data, flags of 2 may point to file data in the btree and flags == 3
130 * may point to extents.
132 * offset is the starting byte offset for this key in the stream.
134 * btrfs_disk_key is in disk byte order. struct btrfs_key is always
135 * in cpu native order. Otherwise they are identical and their sizes
136 * should be the same (ie both packed)
138 struct btrfs_disk_key {
142 } __attribute__ ((__packed__));
148 } __attribute__ ((__packed__));
150 struct btrfs_mapping_tree {
151 struct extent_map_tree map_tree;
154 #define BTRFS_UUID_SIZE 16
155 struct btrfs_dev_item {
156 /* the internal btrfs device id */
159 /* size of the device */
165 /* optimal io alignment for this device */
168 /* optimal io width for this device */
171 /* minimal io size for this device */
174 /* type and info about this device */
177 /* grouping information for allocation decisions */
180 /* seek speed 0-100 where 100 is fastest */
183 /* bandwidth 0-100 where 100 is fastest */
186 /* btrfs generated uuid for this device */
187 u8 uuid[BTRFS_UUID_SIZE];
188 } __attribute__ ((__packed__));
190 struct btrfs_stripe {
193 u8 dev_uuid[BTRFS_UUID_SIZE];
194 } __attribute__ ((__packed__));
197 /* size of this chunk in bytes */
200 /* objectid of the root referencing this chunk */
206 /* optimal io alignment for this chunk */
209 /* optimal io width for this chunk */
212 /* minimal io size for this chunk */
215 /* 2^16 stripes is quite a lot, a second limit is the size of a single
220 /* sub stripes only matter for raid10 */
222 struct btrfs_stripe stripe;
223 /* additional stripes go here */
224 } __attribute__ ((__packed__));
226 static inline unsigned long btrfs_chunk_item_size(int num_stripes)
228 BUG_ON(num_stripes == 0);
229 return sizeof(struct btrfs_chunk) +
230 sizeof(struct btrfs_stripe) * (num_stripes - 1);
233 #define BTRFS_FSID_SIZE 16
234 #define BTRFS_HEADER_FLAG_WRITTEN (1 << 0)
237 * every tree block (leaf or node) starts with this header.
239 struct btrfs_header {
240 /* these first four must match the super block */
241 u8 csum[BTRFS_CSUM_SIZE];
242 u8 fsid[BTRFS_FSID_SIZE]; /* FS specific uuid */
243 __le64 bytenr; /* which block this node is supposed to live in */
246 /* allowed to be different from the super from here on down */
247 u8 chunk_tree_uuid[BTRFS_UUID_SIZE];
252 } __attribute__ ((__packed__));
254 #define BTRFS_NODEPTRS_PER_BLOCK(r) (((r)->nodesize - \
255 sizeof(struct btrfs_header)) / \
256 sizeof(struct btrfs_key_ptr))
257 #define __BTRFS_LEAF_DATA_SIZE(bs) ((bs) - sizeof(struct btrfs_header))
258 #define BTRFS_LEAF_DATA_SIZE(r) (__BTRFS_LEAF_DATA_SIZE(r->leafsize))
259 #define BTRFS_MAX_INLINE_DATA_SIZE(r) (BTRFS_LEAF_DATA_SIZE(r) - \
260 sizeof(struct btrfs_item) - \
261 sizeof(struct btrfs_file_extent_item))
265 * this is a very generous portion of the super block, giving us
266 * room to translate 14 chunks with 3 stripes each.
268 #define BTRFS_SYSTEM_CHUNK_ARRAY_SIZE 2048
269 #define BTRFS_LABEL_SIZE 256
272 * the super block basically lists the main trees of the FS
273 * it currently lacks any block count etc etc
275 struct btrfs_super_block {
276 u8 csum[BTRFS_CSUM_SIZE];
277 /* the first 4 fields must match struct btrfs_header */
278 u8 fsid[16]; /* FS specific uuid */
279 __le64 bytenr; /* this block number */
282 /* allowed to be different from the btrfs_header from here own down */
290 __le64 root_dir_objectid;
296 __le32 sys_chunk_array_size;
300 struct btrfs_dev_item dev_item;
301 char label[BTRFS_LABEL_SIZE];
302 u8 sys_chunk_array[BTRFS_SYSTEM_CHUNK_ARRAY_SIZE];
303 } __attribute__ ((__packed__));
306 * A leaf is full of items. offset and size tell us where to find
307 * the item in the leaf (relative to the start of the data area)
310 struct btrfs_disk_key key;
313 } __attribute__ ((__packed__));
316 * leaves have an item area and a data area:
317 * [item0, item1....itemN] [free space] [dataN...data1, data0]
319 * The data is separate from the items to get the keys closer together
323 struct btrfs_header header;
324 struct btrfs_item items[];
325 } __attribute__ ((__packed__));
328 * all non-leaf blocks are nodes, they hold only keys and pointers to
331 struct btrfs_key_ptr {
332 struct btrfs_disk_key key;
335 } __attribute__ ((__packed__));
338 struct btrfs_header header;
339 struct btrfs_key_ptr ptrs[];
340 } __attribute__ ((__packed__));
343 * btrfs_paths remember the path taken from the root down to the leaf.
344 * level 0 is always the leaf, and nodes[1...BTRFS_MAX_LEVEL] will point
345 * to any other levels that are present.
347 * The slots array records the index of the item or block pointer
348 * used while walking the tree.
351 struct extent_buffer *nodes[BTRFS_MAX_LEVEL];
352 int slots[BTRFS_MAX_LEVEL];
353 /* if there is real range locking, this locks field will change */
354 int locks[BTRFS_MAX_LEVEL];
356 /* keep some upper locks as we walk down */
363 * items in the extent btree are used to record the objectid of the
364 * owner of the block and the number of references
366 struct btrfs_extent_item {
368 } __attribute__ ((__packed__));
370 struct btrfs_extent_ref {
376 } __attribute__ ((__packed__));
378 /* dev extents record free space on individual devices. The owner
379 * field points back to the chunk allocation mapping tree that allocated
380 * the extent. The chunk tree uuid field is a way to double check the owner
382 struct btrfs_dev_extent {
384 __le64 chunk_objectid;
387 u8 chunk_tree_uuid[BTRFS_UUID_SIZE];
388 } __attribute__ ((__packed__));
390 struct btrfs_inode_ref {
394 } __attribute__ ((__packed__));
396 struct btrfs_timespec {
399 } __attribute__ ((__packed__));
402 * there is no padding here on purpose. If you want to extent the inode,
403 * make a new item type
405 struct btrfs_inode_item {
406 /* nfs style generation number */
408 /* transid that last touched this inode */
420 struct btrfs_timespec atime;
421 struct btrfs_timespec ctime;
422 struct btrfs_timespec mtime;
423 struct btrfs_timespec otime;
424 } __attribute__ ((__packed__));
426 struct btrfs_dir_log_item {
428 } __attribute__ ((__packed__));
430 struct btrfs_dir_item {
431 struct btrfs_disk_key location;
436 } __attribute__ ((__packed__));
438 struct btrfs_root_item {
439 struct btrfs_inode_item inode;
446 struct btrfs_disk_key drop_progress;
449 } __attribute__ ((__packed__));
451 #define BTRFS_FILE_EXTENT_REG 0
452 #define BTRFS_FILE_EXTENT_INLINE 1
454 struct btrfs_file_extent_item {
458 * disk space consumed by the extent, checksum blocks are included
462 __le64 disk_num_bytes;
464 * the logical offset in file blocks (no csums)
465 * this extent record is for. This allows a file extent to point
466 * into the middle of an existing extent on disk, sharing it
467 * between two snapshots (useful if some bytes in the middle of the
468 * extent have changed
472 * the logical number of file blocks (no csums included)
475 } __attribute__ ((__packed__));
477 struct btrfs_csum_item {
479 } __attribute__ ((__packed__));
481 /* different types of block groups (and chunks) */
482 #define BTRFS_BLOCK_GROUP_DATA (1 << 0)
483 #define BTRFS_BLOCK_GROUP_SYSTEM (1 << 1)
484 #define BTRFS_BLOCK_GROUP_METADATA (1 << 2)
485 #define BTRFS_BLOCK_GROUP_RAID0 (1 << 3)
486 #define BTRFS_BLOCK_GROUP_RAID1 (1 << 4)
487 #define BTRFS_BLOCK_GROUP_DUP (1 << 5)
488 #define BTRFS_BLOCK_GROUP_RAID10 (1 << 6)
490 struct btrfs_block_group_item {
492 __le64 chunk_objectid;
494 } __attribute__ ((__packed__));
496 struct btrfs_space_info {
504 struct list_head list;
506 /* for block groups in our same type */
507 struct list_head block_groups;
511 struct btrfs_free_space {
512 struct rb_node bytes_index;
513 struct rb_node offset_index;
518 struct btrfs_block_group_cache {
519 struct btrfs_key key;
520 struct btrfs_block_group_item item;
529 struct btrfs_space_info *space_info;
531 /* free space cache stuff */
532 struct rb_root free_space_bytes;
533 struct rb_root free_space_offset;
535 /* block group cache stuff */
536 struct rb_node cache_node;
538 /* for block groups in the same raid type */
539 struct list_head list;
543 struct btrfs_fs_devices;
544 struct btrfs_fs_info {
545 u8 fsid[BTRFS_FSID_SIZE];
546 u8 chunk_tree_uuid[BTRFS_UUID_SIZE];
547 struct btrfs_root *extent_root;
548 struct btrfs_root *tree_root;
549 struct btrfs_root *chunk_root;
550 struct btrfs_root *dev_root;
552 /* the log root tree is a directory of all the other log roots */
553 struct btrfs_root *log_root_tree;
554 struct radix_tree_root fs_roots_radix;
556 /* block group cache stuff */
557 spinlock_t block_group_cache_lock;
558 struct rb_root block_group_cache_tree;
560 struct extent_io_tree pinned_extents;
561 struct extent_io_tree pending_del;
562 struct extent_io_tree extent_ins;
564 /* logical->physical extent mapping */
565 struct btrfs_mapping_tree mapping_tree;
568 u64 last_trans_committed;
569 u64 last_trans_new_blockgroup;
570 u64 open_ioctl_trans;
571 unsigned long mount_opt;
575 struct btrfs_transaction *running_transaction;
576 wait_queue_head_t transaction_throttle;
577 wait_queue_head_t transaction_wait;
578 wait_queue_head_t async_submit_wait;
580 wait_queue_head_t tree_log_wait;
582 struct btrfs_super_block super_copy;
583 struct btrfs_super_block super_for_commit;
584 struct block_device *__bdev;
585 struct super_block *sb;
586 struct inode *btree_inode;
587 struct backing_dev_info bdi;
588 spinlock_t hash_lock;
589 struct mutex trans_mutex;
590 struct mutex tree_log_mutex;
591 struct mutex transaction_kthread_mutex;
592 struct mutex cleaner_mutex;
593 struct mutex alloc_mutex;
594 struct mutex chunk_mutex;
595 struct mutex drop_mutex;
596 struct mutex volume_mutex;
597 struct list_head trans_list;
598 struct list_head hashers;
599 struct list_head dead_roots;
601 atomic_t nr_async_submits;
602 atomic_t nr_async_bios;
603 atomic_t tree_log_writers;
604 atomic_t tree_log_commit;
605 unsigned long tree_log_batch;
606 u64 tree_log_transid;
609 * this is used by the balancing code to wait for all the pending
612 spinlock_t ordered_extent_lock;
613 struct list_head ordered_extents;
614 struct list_head delalloc_inodes;
617 * there is a pool of worker threads for checksumming during writes
618 * and a pool for checksumming after reads. This is because readers
619 * can run with FS locks held, and the writers may be waiting for
620 * those locks. We don't want ordering in the pending list to cause
621 * deadlocks, and so the two are serviced separately.
623 * A third pool does submit_bio to avoid deadlocking with the other
626 struct btrfs_workers workers;
627 struct btrfs_workers endio_workers;
628 struct btrfs_workers endio_write_workers;
629 struct btrfs_workers submit_workers;
631 * fixup workers take dirty pages that didn't properly go through
632 * the cow mechanism and make them safe to write. It happens
633 * for the sys_munmap function call path
635 struct btrfs_workers fixup_workers;
636 struct task_struct *transaction_kthread;
637 struct task_struct *cleaner_kthread;
638 int thread_pool_size;
640 struct kobject super_kobj;
641 struct completion kobj_unregister;
644 int log_root_recovering;
646 atomic_t throttle_gen;
649 struct list_head dirty_cowonly_roots;
651 struct btrfs_fs_devices *fs_devices;
652 struct list_head space_info;
653 spinlock_t delalloc_lock;
654 spinlock_t new_trans_lock;
660 spinlock_t ref_cache_lock;
661 u64 total_ref_cache_size;
663 u64 avail_data_alloc_bits;
664 u64 avail_metadata_alloc_bits;
665 u64 avail_system_alloc_bits;
666 u64 data_alloc_profile;
667 u64 metadata_alloc_profile;
668 u64 system_alloc_profile;
673 struct btrfs_leaf_ref_tree {
675 struct btrfs_leaf_ref *last;
676 struct list_head list;
681 * in ram representation of the tree. extent_root is used for all allocations
682 * and for the extent tree extent_root root.
684 struct btrfs_dirty_root;
686 struct extent_buffer *node;
688 /* the node lock is held while changing the node pointer */
689 spinlock_t node_lock;
691 struct extent_buffer *commit_root;
692 struct btrfs_leaf_ref_tree *ref_tree;
693 struct btrfs_leaf_ref_tree ref_tree_struct;
694 struct btrfs_dirty_root *dirty_root;
695 struct btrfs_root *log_root;
697 struct btrfs_root_item root_item;
698 struct btrfs_key root_key;
699 struct btrfs_fs_info *fs_info;
701 struct extent_io_tree dirty_log_pages;
703 struct kobject root_kobj;
704 struct completion kobj_unregister;
705 struct mutex objectid_mutex;
706 struct mutex log_mutex;
711 /* data allocations are done in sectorsize units */
714 /* node allocations are done in nodesize units */
717 /* leaf allocations are done in leafsize units */
724 u64 last_inode_alloc;
727 u64 defrag_trans_start;
728 struct btrfs_key defrag_progress;
729 struct btrfs_key defrag_max;
735 /* the dirty list is only used by non-reference counted roots */
736 struct list_head dirty_list;
738 spinlock_t list_lock;
739 struct list_head dead_list;
740 struct list_head orphan_list;
745 * inode items have the data typically returned from stat and store other
746 * info about object characteristics. There is one for every file and dir in
749 #define BTRFS_INODE_ITEM_KEY 1
750 #define BTRFS_INODE_REF_KEY 2
751 #define BTRFS_XATTR_ITEM_KEY 8
752 #define BTRFS_ORPHAN_ITEM_KEY 9
753 /* reserve 2-15 close to the inode for later flexibility */
756 * dir items are the name -> inode pointers in a directory. There is one
757 * for every name in a directory.
759 #define BTRFS_DIR_LOG_ITEM_KEY 14
760 #define BTRFS_DIR_LOG_INDEX_KEY 15
761 #define BTRFS_DIR_ITEM_KEY 16
762 #define BTRFS_DIR_INDEX_KEY 17
764 * extent data is for file data
766 #define BTRFS_EXTENT_DATA_KEY 18
768 * csum items have the checksums for data in the extents
770 #define BTRFS_CSUM_ITEM_KEY 19
773 /* reserve 21-31 for other file/dir stuff */
776 * root items point to tree roots. There are typically in the root
777 * tree used by the super block to find all the other trees
779 #define BTRFS_ROOT_ITEM_KEY 32
781 * extent items are in the extent map tree. These record which blocks
782 * are used, and how many references there are to each block
784 #define BTRFS_EXTENT_ITEM_KEY 33
785 #define BTRFS_EXTENT_REF_KEY 34
788 * block groups give us hints into the extent allocation trees. Which
789 * blocks are free etc etc
791 #define BTRFS_BLOCK_GROUP_ITEM_KEY 50
793 #define BTRFS_DEV_EXTENT_KEY 75
794 #define BTRFS_DEV_ITEM_KEY 76
795 #define BTRFS_CHUNK_ITEM_KEY 77
798 * string items are for debugging. They just store a short string of
801 #define BTRFS_STRING_ITEM_KEY 253
803 #define BTRFS_MOUNT_NODATASUM (1 << 0)
804 #define BTRFS_MOUNT_NODATACOW (1 << 1)
805 #define BTRFS_MOUNT_NOBARRIER (1 << 2)
806 #define BTRFS_MOUNT_SSD (1 << 3)
807 #define BTRFS_MOUNT_DEGRADED (1 << 4)
809 #define btrfs_clear_opt(o, opt) ((o) &= ~BTRFS_MOUNT_##opt)
810 #define btrfs_set_opt(o, opt) ((o) |= BTRFS_MOUNT_##opt)
811 #define btrfs_test_opt(root, opt) ((root)->fs_info->mount_opt & \
816 #define BTRFS_INODE_NODATASUM (1 << 0)
817 #define BTRFS_INODE_NODATACOW (1 << 1)
818 #define BTRFS_INODE_READONLY (1 << 2)
819 #define btrfs_clear_flag(inode, flag) (BTRFS_I(inode)->flags &= \
821 #define btrfs_set_flag(inode, flag) (BTRFS_I(inode)->flags |= \
823 #define btrfs_test_flag(inode, flag) (BTRFS_I(inode)->flags & \
825 /* some macros to generate set/get funcs for the struct fields. This
826 * assumes there is a lefoo_to_cpu for every type, so lets make a simple
829 #define le8_to_cpu(v) (v)
830 #define cpu_to_le8(v) (v)
833 #define read_eb_member(eb, ptr, type, member, result) ( \
834 read_extent_buffer(eb, (char *)(result), \
835 ((unsigned long)(ptr)) + \
836 offsetof(type, member), \
837 sizeof(((type *)0)->member)))
839 #define write_eb_member(eb, ptr, type, member, result) ( \
840 write_extent_buffer(eb, (char *)(result), \
841 ((unsigned long)(ptr)) + \
842 offsetof(type, member), \
843 sizeof(((type *)0)->member)))
845 #ifndef BTRFS_SETGET_FUNCS
846 #define BTRFS_SETGET_FUNCS(name, type, member, bits) \
847 u##bits btrfs_##name(struct extent_buffer *eb, type *s); \
848 void btrfs_set_##name(struct extent_buffer *eb, type *s, u##bits val);
851 #define BTRFS_SETGET_HEADER_FUNCS(name, type, member, bits) \
852 static inline u##bits btrfs_##name(struct extent_buffer *eb) \
854 type *p = kmap_atomic(eb->first_page, KM_USER0); \
855 u##bits res = le##bits##_to_cpu(p->member); \
856 kunmap_atomic(p, KM_USER0); \
859 static inline void btrfs_set_##name(struct extent_buffer *eb, \
862 type *p = kmap_atomic(eb->first_page, KM_USER0); \
863 p->member = cpu_to_le##bits(val); \
864 kunmap_atomic(p, KM_USER0); \
867 #define BTRFS_SETGET_STACK_FUNCS(name, type, member, bits) \
868 static inline u##bits btrfs_##name(type *s) \
870 return le##bits##_to_cpu(s->member); \
872 static inline void btrfs_set_##name(type *s, u##bits val) \
874 s->member = cpu_to_le##bits(val); \
877 BTRFS_SETGET_FUNCS(device_type, struct btrfs_dev_item, type, 64);
878 BTRFS_SETGET_FUNCS(device_total_bytes, struct btrfs_dev_item, total_bytes, 64);
879 BTRFS_SETGET_FUNCS(device_bytes_used, struct btrfs_dev_item, bytes_used, 64);
880 BTRFS_SETGET_FUNCS(device_io_align, struct btrfs_dev_item, io_align, 32);
881 BTRFS_SETGET_FUNCS(device_io_width, struct btrfs_dev_item, io_width, 32);
882 BTRFS_SETGET_FUNCS(device_sector_size, struct btrfs_dev_item, sector_size, 32);
883 BTRFS_SETGET_FUNCS(device_id, struct btrfs_dev_item, devid, 64);
884 BTRFS_SETGET_FUNCS(device_group, struct btrfs_dev_item, dev_group, 32);
885 BTRFS_SETGET_FUNCS(device_seek_speed, struct btrfs_dev_item, seek_speed, 8);
886 BTRFS_SETGET_FUNCS(device_bandwidth, struct btrfs_dev_item, bandwidth, 8);
888 BTRFS_SETGET_STACK_FUNCS(stack_device_type, struct btrfs_dev_item, type, 64);
889 BTRFS_SETGET_STACK_FUNCS(stack_device_total_bytes, struct btrfs_dev_item,
891 BTRFS_SETGET_STACK_FUNCS(stack_device_bytes_used, struct btrfs_dev_item,
893 BTRFS_SETGET_STACK_FUNCS(stack_device_io_align, struct btrfs_dev_item,
895 BTRFS_SETGET_STACK_FUNCS(stack_device_io_width, struct btrfs_dev_item,
897 BTRFS_SETGET_STACK_FUNCS(stack_device_sector_size, struct btrfs_dev_item,
899 BTRFS_SETGET_STACK_FUNCS(stack_device_id, struct btrfs_dev_item, devid, 64);
900 BTRFS_SETGET_STACK_FUNCS(stack_device_group, struct btrfs_dev_item,
902 BTRFS_SETGET_STACK_FUNCS(stack_device_seek_speed, struct btrfs_dev_item,
904 BTRFS_SETGET_STACK_FUNCS(stack_device_bandwidth, struct btrfs_dev_item,
907 static inline char *btrfs_device_uuid(struct btrfs_dev_item *d)
909 return (char *)d + offsetof(struct btrfs_dev_item, uuid);
912 BTRFS_SETGET_FUNCS(chunk_length, struct btrfs_chunk, length, 64);
913 BTRFS_SETGET_FUNCS(chunk_owner, struct btrfs_chunk, owner, 64);
914 BTRFS_SETGET_FUNCS(chunk_stripe_len, struct btrfs_chunk, stripe_len, 64);
915 BTRFS_SETGET_FUNCS(chunk_io_align, struct btrfs_chunk, io_align, 32);
916 BTRFS_SETGET_FUNCS(chunk_io_width, struct btrfs_chunk, io_width, 32);
917 BTRFS_SETGET_FUNCS(chunk_sector_size, struct btrfs_chunk, sector_size, 32);
918 BTRFS_SETGET_FUNCS(chunk_type, struct btrfs_chunk, type, 64);
919 BTRFS_SETGET_FUNCS(chunk_num_stripes, struct btrfs_chunk, num_stripes, 16);
920 BTRFS_SETGET_FUNCS(chunk_sub_stripes, struct btrfs_chunk, sub_stripes, 16);
921 BTRFS_SETGET_FUNCS(stripe_devid, struct btrfs_stripe, devid, 64);
922 BTRFS_SETGET_FUNCS(stripe_offset, struct btrfs_stripe, offset, 64);
924 static inline char *btrfs_stripe_dev_uuid(struct btrfs_stripe *s)
926 return (char *)s + offsetof(struct btrfs_stripe, dev_uuid);
929 BTRFS_SETGET_STACK_FUNCS(stack_chunk_length, struct btrfs_chunk, length, 64);
930 BTRFS_SETGET_STACK_FUNCS(stack_chunk_owner, struct btrfs_chunk, owner, 64);
931 BTRFS_SETGET_STACK_FUNCS(stack_chunk_stripe_len, struct btrfs_chunk,
933 BTRFS_SETGET_STACK_FUNCS(stack_chunk_io_align, struct btrfs_chunk,
935 BTRFS_SETGET_STACK_FUNCS(stack_chunk_io_width, struct btrfs_chunk,
937 BTRFS_SETGET_STACK_FUNCS(stack_chunk_sector_size, struct btrfs_chunk,
939 BTRFS_SETGET_STACK_FUNCS(stack_chunk_type, struct btrfs_chunk, type, 64);
940 BTRFS_SETGET_STACK_FUNCS(stack_chunk_num_stripes, struct btrfs_chunk,
942 BTRFS_SETGET_STACK_FUNCS(stack_chunk_sub_stripes, struct btrfs_chunk,
944 BTRFS_SETGET_STACK_FUNCS(stack_stripe_devid, struct btrfs_stripe, devid, 64);
945 BTRFS_SETGET_STACK_FUNCS(stack_stripe_offset, struct btrfs_stripe, offset, 64);
947 static inline struct btrfs_stripe *btrfs_stripe_nr(struct btrfs_chunk *c,
950 unsigned long offset = (unsigned long)c;
951 offset += offsetof(struct btrfs_chunk, stripe);
952 offset += nr * sizeof(struct btrfs_stripe);
953 return (struct btrfs_stripe *)offset;
956 static inline char *btrfs_stripe_dev_uuid_nr(struct btrfs_chunk *c, int nr)
958 return btrfs_stripe_dev_uuid(btrfs_stripe_nr(c, nr));
961 static inline u64 btrfs_stripe_offset_nr(struct extent_buffer *eb,
962 struct btrfs_chunk *c, int nr)
964 return btrfs_stripe_offset(eb, btrfs_stripe_nr(c, nr));
967 static inline void btrfs_set_stripe_offset_nr(struct extent_buffer *eb,
968 struct btrfs_chunk *c, int nr,
971 btrfs_set_stripe_offset(eb, btrfs_stripe_nr(c, nr), val);
974 static inline u64 btrfs_stripe_devid_nr(struct extent_buffer *eb,
975 struct btrfs_chunk *c, int nr)
977 return btrfs_stripe_devid(eb, btrfs_stripe_nr(c, nr));
980 static inline void btrfs_set_stripe_devid_nr(struct extent_buffer *eb,
981 struct btrfs_chunk *c, int nr,
984 btrfs_set_stripe_devid(eb, btrfs_stripe_nr(c, nr), val);
987 /* struct btrfs_block_group_item */
988 BTRFS_SETGET_STACK_FUNCS(block_group_used, struct btrfs_block_group_item,
990 BTRFS_SETGET_FUNCS(disk_block_group_used, struct btrfs_block_group_item,
992 BTRFS_SETGET_STACK_FUNCS(block_group_chunk_objectid,
993 struct btrfs_block_group_item, chunk_objectid, 64);
995 BTRFS_SETGET_FUNCS(disk_block_group_chunk_objectid,
996 struct btrfs_block_group_item, chunk_objectid, 64);
997 BTRFS_SETGET_FUNCS(disk_block_group_flags,
998 struct btrfs_block_group_item, flags, 64);
999 BTRFS_SETGET_STACK_FUNCS(block_group_flags,
1000 struct btrfs_block_group_item, flags, 64);
1002 /* struct btrfs_inode_ref */
1003 BTRFS_SETGET_FUNCS(inode_ref_name_len, struct btrfs_inode_ref, name_len, 16);
1004 BTRFS_SETGET_FUNCS(inode_ref_index, struct btrfs_inode_ref, index, 64);
1006 /* struct btrfs_inode_item */
1007 BTRFS_SETGET_FUNCS(inode_generation, struct btrfs_inode_item, generation, 64);
1008 BTRFS_SETGET_FUNCS(inode_transid, struct btrfs_inode_item, transid, 64);
1009 BTRFS_SETGET_FUNCS(inode_size, struct btrfs_inode_item, size, 64);
1010 BTRFS_SETGET_FUNCS(inode_nblocks, struct btrfs_inode_item, nblocks, 64);
1011 BTRFS_SETGET_FUNCS(inode_block_group, struct btrfs_inode_item, block_group, 64);
1012 BTRFS_SETGET_FUNCS(inode_nlink, struct btrfs_inode_item, nlink, 32);
1013 BTRFS_SETGET_FUNCS(inode_uid, struct btrfs_inode_item, uid, 32);
1014 BTRFS_SETGET_FUNCS(inode_gid, struct btrfs_inode_item, gid, 32);
1015 BTRFS_SETGET_FUNCS(inode_mode, struct btrfs_inode_item, mode, 32);
1016 BTRFS_SETGET_FUNCS(inode_rdev, struct btrfs_inode_item, rdev, 64);
1017 BTRFS_SETGET_FUNCS(inode_flags, struct btrfs_inode_item, flags, 16);
1018 BTRFS_SETGET_FUNCS(inode_compat_flags, struct btrfs_inode_item,
1021 static inline struct btrfs_timespec *
1022 btrfs_inode_atime(struct btrfs_inode_item *inode_item)
1024 unsigned long ptr = (unsigned long)inode_item;
1025 ptr += offsetof(struct btrfs_inode_item, atime);
1026 return (struct btrfs_timespec *)ptr;
1029 static inline struct btrfs_timespec *
1030 btrfs_inode_mtime(struct btrfs_inode_item *inode_item)
1032 unsigned long ptr = (unsigned long)inode_item;
1033 ptr += offsetof(struct btrfs_inode_item, mtime);
1034 return (struct btrfs_timespec *)ptr;
1037 static inline struct btrfs_timespec *
1038 btrfs_inode_ctime(struct btrfs_inode_item *inode_item)
1040 unsigned long ptr = (unsigned long)inode_item;
1041 ptr += offsetof(struct btrfs_inode_item, ctime);
1042 return (struct btrfs_timespec *)ptr;
1045 static inline struct btrfs_timespec *
1046 btrfs_inode_otime(struct btrfs_inode_item *inode_item)
1048 unsigned long ptr = (unsigned long)inode_item;
1049 ptr += offsetof(struct btrfs_inode_item, otime);
1050 return (struct btrfs_timespec *)ptr;
1053 BTRFS_SETGET_FUNCS(timespec_sec, struct btrfs_timespec, sec, 64);
1054 BTRFS_SETGET_FUNCS(timespec_nsec, struct btrfs_timespec, nsec, 32);
1056 /* struct btrfs_dev_extent */
1057 BTRFS_SETGET_FUNCS(dev_extent_chunk_tree, struct btrfs_dev_extent,
1059 BTRFS_SETGET_FUNCS(dev_extent_chunk_objectid, struct btrfs_dev_extent,
1060 chunk_objectid, 64);
1061 BTRFS_SETGET_FUNCS(dev_extent_chunk_offset, struct btrfs_dev_extent,
1063 BTRFS_SETGET_FUNCS(dev_extent_length, struct btrfs_dev_extent, length, 64);
1065 static inline u8 *btrfs_dev_extent_chunk_tree_uuid(struct btrfs_dev_extent *dev)
1067 unsigned long ptr = offsetof(struct btrfs_dev_extent, chunk_tree_uuid);
1068 return (u8 *)((unsigned long)dev + ptr);
1071 /* struct btrfs_extent_ref */
1072 BTRFS_SETGET_FUNCS(ref_root, struct btrfs_extent_ref, root, 64);
1073 BTRFS_SETGET_FUNCS(ref_generation, struct btrfs_extent_ref, generation, 64);
1074 BTRFS_SETGET_FUNCS(ref_objectid, struct btrfs_extent_ref, objectid, 64);
1075 BTRFS_SETGET_FUNCS(ref_offset, struct btrfs_extent_ref, offset, 64);
1076 BTRFS_SETGET_FUNCS(ref_num_refs, struct btrfs_extent_ref, num_refs, 32);
1078 BTRFS_SETGET_STACK_FUNCS(stack_ref_root, struct btrfs_extent_ref, root, 64);
1079 BTRFS_SETGET_STACK_FUNCS(stack_ref_generation, struct btrfs_extent_ref,
1081 BTRFS_SETGET_STACK_FUNCS(stack_ref_objectid, struct btrfs_extent_ref,
1083 BTRFS_SETGET_STACK_FUNCS(stack_ref_offset, struct btrfs_extent_ref,
1085 BTRFS_SETGET_STACK_FUNCS(stack_ref_num_refs, struct btrfs_extent_ref,
1088 /* struct btrfs_extent_item */
1089 BTRFS_SETGET_FUNCS(extent_refs, struct btrfs_extent_item, refs, 32);
1090 BTRFS_SETGET_STACK_FUNCS(stack_extent_refs, struct btrfs_extent_item,
1093 /* struct btrfs_node */
1094 BTRFS_SETGET_FUNCS(key_blockptr, struct btrfs_key_ptr, blockptr, 64);
1095 BTRFS_SETGET_FUNCS(key_generation, struct btrfs_key_ptr, generation, 64);
1097 static inline u64 btrfs_node_blockptr(struct extent_buffer *eb, int nr)
1100 ptr = offsetof(struct btrfs_node, ptrs) +
1101 sizeof(struct btrfs_key_ptr) * nr;
1102 return btrfs_key_blockptr(eb, (struct btrfs_key_ptr *)ptr);
1105 static inline void btrfs_set_node_blockptr(struct extent_buffer *eb,
1109 ptr = offsetof(struct btrfs_node, ptrs) +
1110 sizeof(struct btrfs_key_ptr) * nr;
1111 btrfs_set_key_blockptr(eb, (struct btrfs_key_ptr *)ptr, val);
1114 static inline u64 btrfs_node_ptr_generation(struct extent_buffer *eb, int nr)
1117 ptr = offsetof(struct btrfs_node, ptrs) +
1118 sizeof(struct btrfs_key_ptr) * nr;
1119 return btrfs_key_generation(eb, (struct btrfs_key_ptr *)ptr);
1122 static inline void btrfs_set_node_ptr_generation(struct extent_buffer *eb,
1126 ptr = offsetof(struct btrfs_node, ptrs) +
1127 sizeof(struct btrfs_key_ptr) * nr;
1128 btrfs_set_key_generation(eb, (struct btrfs_key_ptr *)ptr, val);
1131 static inline unsigned long btrfs_node_key_ptr_offset(int nr)
1133 return offsetof(struct btrfs_node, ptrs) +
1134 sizeof(struct btrfs_key_ptr) * nr;
1137 void btrfs_node_key(struct extent_buffer *eb,
1138 struct btrfs_disk_key *disk_key, int nr);
1140 static inline void btrfs_set_node_key(struct extent_buffer *eb,
1141 struct btrfs_disk_key *disk_key, int nr)
1144 ptr = btrfs_node_key_ptr_offset(nr);
1145 write_eb_member(eb, (struct btrfs_key_ptr *)ptr,
1146 struct btrfs_key_ptr, key, disk_key);
1149 /* struct btrfs_item */
1150 BTRFS_SETGET_FUNCS(item_offset, struct btrfs_item, offset, 32);
1151 BTRFS_SETGET_FUNCS(item_size, struct btrfs_item, size, 32);
1153 static inline unsigned long btrfs_item_nr_offset(int nr)
1155 return offsetof(struct btrfs_leaf, items) +
1156 sizeof(struct btrfs_item) * nr;
1159 static inline struct btrfs_item *btrfs_item_nr(struct extent_buffer *eb,
1162 return (struct btrfs_item *)btrfs_item_nr_offset(nr);
1165 static inline u32 btrfs_item_end(struct extent_buffer *eb,
1166 struct btrfs_item *item)
1168 return btrfs_item_offset(eb, item) + btrfs_item_size(eb, item);
1171 static inline u32 btrfs_item_end_nr(struct extent_buffer *eb, int nr)
1173 return btrfs_item_end(eb, btrfs_item_nr(eb, nr));
1176 static inline u32 btrfs_item_offset_nr(struct extent_buffer *eb, int nr)
1178 return btrfs_item_offset(eb, btrfs_item_nr(eb, nr));
1181 static inline u32 btrfs_item_size_nr(struct extent_buffer *eb, int nr)
1183 return btrfs_item_size(eb, btrfs_item_nr(eb, nr));
1186 static inline void btrfs_item_key(struct extent_buffer *eb,
1187 struct btrfs_disk_key *disk_key, int nr)
1189 struct btrfs_item *item = btrfs_item_nr(eb, nr);
1190 read_eb_member(eb, item, struct btrfs_item, key, disk_key);
1193 static inline void btrfs_set_item_key(struct extent_buffer *eb,
1194 struct btrfs_disk_key *disk_key, int nr)
1196 struct btrfs_item *item = btrfs_item_nr(eb, nr);
1197 write_eb_member(eb, item, struct btrfs_item, key, disk_key);
1200 BTRFS_SETGET_FUNCS(dir_log_end, struct btrfs_dir_log_item, end, 64);
1202 /* struct btrfs_dir_item */
1203 BTRFS_SETGET_FUNCS(dir_data_len, struct btrfs_dir_item, data_len, 16);
1204 BTRFS_SETGET_FUNCS(dir_type, struct btrfs_dir_item, type, 8);
1205 BTRFS_SETGET_FUNCS(dir_name_len, struct btrfs_dir_item, name_len, 16);
1206 BTRFS_SETGET_FUNCS(dir_transid, struct btrfs_dir_item, transid, 64);
1208 static inline void btrfs_dir_item_key(struct extent_buffer *eb,
1209 struct btrfs_dir_item *item,
1210 struct btrfs_disk_key *key)
1212 read_eb_member(eb, item, struct btrfs_dir_item, location, key);
1215 static inline void btrfs_set_dir_item_key(struct extent_buffer *eb,
1216 struct btrfs_dir_item *item,
1217 struct btrfs_disk_key *key)
1219 write_eb_member(eb, item, struct btrfs_dir_item, location, key);
1222 /* struct btrfs_disk_key */
1223 BTRFS_SETGET_STACK_FUNCS(disk_key_objectid, struct btrfs_disk_key,
1225 BTRFS_SETGET_STACK_FUNCS(disk_key_offset, struct btrfs_disk_key, offset, 64);
1226 BTRFS_SETGET_STACK_FUNCS(disk_key_type, struct btrfs_disk_key, type, 8);
1228 static inline void btrfs_disk_key_to_cpu(struct btrfs_key *cpu,
1229 struct btrfs_disk_key *disk)
1231 cpu->offset = le64_to_cpu(disk->offset);
1232 cpu->type = disk->type;
1233 cpu->objectid = le64_to_cpu(disk->objectid);
1236 static inline void btrfs_cpu_key_to_disk(struct btrfs_disk_key *disk,
1237 struct btrfs_key *cpu)
1239 disk->offset = cpu_to_le64(cpu->offset);
1240 disk->type = cpu->type;
1241 disk->objectid = cpu_to_le64(cpu->objectid);
1244 static inline void btrfs_node_key_to_cpu(struct extent_buffer *eb,
1245 struct btrfs_key *key, int nr)
1247 struct btrfs_disk_key disk_key;
1248 btrfs_node_key(eb, &disk_key, nr);
1249 btrfs_disk_key_to_cpu(key, &disk_key);
1252 static inline void btrfs_item_key_to_cpu(struct extent_buffer *eb,
1253 struct btrfs_key *key, int nr)
1255 struct btrfs_disk_key disk_key;
1256 btrfs_item_key(eb, &disk_key, nr);
1257 btrfs_disk_key_to_cpu(key, &disk_key);
1260 static inline void btrfs_dir_item_key_to_cpu(struct extent_buffer *eb,
1261 struct btrfs_dir_item *item,
1262 struct btrfs_key *key)
1264 struct btrfs_disk_key disk_key;
1265 btrfs_dir_item_key(eb, item, &disk_key);
1266 btrfs_disk_key_to_cpu(key, &disk_key);
1270 static inline u8 btrfs_key_type(struct btrfs_key *key)
1275 static inline void btrfs_set_key_type(struct btrfs_key *key, u8 val)
1280 /* struct btrfs_header */
1281 BTRFS_SETGET_HEADER_FUNCS(header_bytenr, struct btrfs_header, bytenr, 64);
1282 BTRFS_SETGET_HEADER_FUNCS(header_generation, struct btrfs_header,
1284 BTRFS_SETGET_HEADER_FUNCS(header_owner, struct btrfs_header, owner, 64);
1285 BTRFS_SETGET_HEADER_FUNCS(header_nritems, struct btrfs_header, nritems, 32);
1286 BTRFS_SETGET_HEADER_FUNCS(header_flags, struct btrfs_header, flags, 64);
1287 BTRFS_SETGET_HEADER_FUNCS(header_level, struct btrfs_header, level, 8);
1289 static inline int btrfs_header_flag(struct extent_buffer *eb, u64 flag)
1291 return (btrfs_header_flags(eb) & flag) == flag;
1294 static inline int btrfs_set_header_flag(struct extent_buffer *eb, u64 flag)
1296 u64 flags = btrfs_header_flags(eb);
1297 btrfs_set_header_flags(eb, flags | flag);
1298 return (flags & flag) == flag;
1301 static inline int btrfs_clear_header_flag(struct extent_buffer *eb, u64 flag)
1303 u64 flags = btrfs_header_flags(eb);
1304 btrfs_set_header_flags(eb, flags & ~flag);
1305 return (flags & flag) == flag;
1308 static inline u8 *btrfs_header_fsid(struct extent_buffer *eb)
1310 unsigned long ptr = offsetof(struct btrfs_header, fsid);
1314 static inline u8 *btrfs_header_chunk_tree_uuid(struct extent_buffer *eb)
1316 unsigned long ptr = offsetof(struct btrfs_header, chunk_tree_uuid);
1320 static inline u8 *btrfs_super_fsid(struct extent_buffer *eb)
1322 unsigned long ptr = offsetof(struct btrfs_super_block, fsid);
1326 static inline u8 *btrfs_header_csum(struct extent_buffer *eb)
1328 unsigned long ptr = offsetof(struct btrfs_header, csum);
1332 static inline struct btrfs_node *btrfs_buffer_node(struct extent_buffer *eb)
1337 static inline struct btrfs_leaf *btrfs_buffer_leaf(struct extent_buffer *eb)
1342 static inline struct btrfs_header *btrfs_buffer_header(struct extent_buffer *eb)
1347 static inline int btrfs_is_leaf(struct extent_buffer *eb)
1349 return (btrfs_header_level(eb) == 0);
1352 /* struct btrfs_root_item */
1353 BTRFS_SETGET_FUNCS(disk_root_refs, struct btrfs_root_item, refs, 32);
1354 BTRFS_SETGET_FUNCS(disk_root_bytenr, struct btrfs_root_item, bytenr, 64);
1355 BTRFS_SETGET_FUNCS(disk_root_level, struct btrfs_root_item, level, 8);
1357 BTRFS_SETGET_STACK_FUNCS(root_bytenr, struct btrfs_root_item, bytenr, 64);
1358 BTRFS_SETGET_STACK_FUNCS(root_level, struct btrfs_root_item, level, 8);
1359 BTRFS_SETGET_STACK_FUNCS(root_dirid, struct btrfs_root_item, root_dirid, 64);
1360 BTRFS_SETGET_STACK_FUNCS(root_refs, struct btrfs_root_item, refs, 32);
1361 BTRFS_SETGET_STACK_FUNCS(root_flags, struct btrfs_root_item, flags, 32);
1362 BTRFS_SETGET_STACK_FUNCS(root_used, struct btrfs_root_item, bytes_used, 64);
1363 BTRFS_SETGET_STACK_FUNCS(root_limit, struct btrfs_root_item, byte_limit, 64);
1365 /* struct btrfs_super_block */
1366 BTRFS_SETGET_STACK_FUNCS(super_bytenr, struct btrfs_super_block, bytenr, 64);
1367 BTRFS_SETGET_STACK_FUNCS(super_flags, struct btrfs_super_block, flags, 64);
1368 BTRFS_SETGET_STACK_FUNCS(super_generation, struct btrfs_super_block,
1370 BTRFS_SETGET_STACK_FUNCS(super_root, struct btrfs_super_block, root, 64);
1371 BTRFS_SETGET_STACK_FUNCS(super_sys_array_size,
1372 struct btrfs_super_block, sys_chunk_array_size, 32);
1373 BTRFS_SETGET_STACK_FUNCS(super_root_level, struct btrfs_super_block,
1375 BTRFS_SETGET_STACK_FUNCS(super_chunk_root, struct btrfs_super_block,
1377 BTRFS_SETGET_STACK_FUNCS(super_chunk_root_level, struct btrfs_super_block,
1378 chunk_root_level, 8);
1379 BTRFS_SETGET_STACK_FUNCS(super_log_root, struct btrfs_super_block,
1381 BTRFS_SETGET_STACK_FUNCS(super_log_root_level, struct btrfs_super_block,
1383 BTRFS_SETGET_STACK_FUNCS(super_total_bytes, struct btrfs_super_block,
1385 BTRFS_SETGET_STACK_FUNCS(super_bytes_used, struct btrfs_super_block,
1387 BTRFS_SETGET_STACK_FUNCS(super_sectorsize, struct btrfs_super_block,
1389 BTRFS_SETGET_STACK_FUNCS(super_nodesize, struct btrfs_super_block,
1391 BTRFS_SETGET_STACK_FUNCS(super_leafsize, struct btrfs_super_block,
1393 BTRFS_SETGET_STACK_FUNCS(super_stripesize, struct btrfs_super_block,
1395 BTRFS_SETGET_STACK_FUNCS(super_root_dir, struct btrfs_super_block,
1396 root_dir_objectid, 64);
1397 BTRFS_SETGET_STACK_FUNCS(super_num_devices, struct btrfs_super_block,
1400 static inline unsigned long btrfs_leaf_data(struct extent_buffer *l)
1402 return offsetof(struct btrfs_leaf, items);
1405 /* struct btrfs_file_extent_item */
1406 BTRFS_SETGET_FUNCS(file_extent_type, struct btrfs_file_extent_item, type, 8);
1408 static inline unsigned long btrfs_file_extent_inline_start(struct
1409 btrfs_file_extent_item *e)
1411 unsigned long offset = (unsigned long)e;
1412 offset += offsetof(struct btrfs_file_extent_item, disk_bytenr);
1416 static inline u32 btrfs_file_extent_calc_inline_size(u32 datasize)
1418 return offsetof(struct btrfs_file_extent_item, disk_bytenr) + datasize;
1421 static inline u32 btrfs_file_extent_inline_len(struct extent_buffer *eb,
1422 struct btrfs_item *e)
1424 unsigned long offset;
1425 offset = offsetof(struct btrfs_file_extent_item, disk_bytenr);
1426 return btrfs_item_size(eb, e) - offset;
1429 BTRFS_SETGET_FUNCS(file_extent_disk_bytenr, struct btrfs_file_extent_item,
1431 BTRFS_SETGET_FUNCS(file_extent_generation, struct btrfs_file_extent_item,
1433 BTRFS_SETGET_FUNCS(file_extent_disk_num_bytes, struct btrfs_file_extent_item,
1434 disk_num_bytes, 64);
1435 BTRFS_SETGET_FUNCS(file_extent_offset, struct btrfs_file_extent_item,
1437 BTRFS_SETGET_FUNCS(file_extent_num_bytes, struct btrfs_file_extent_item,
1440 static inline struct btrfs_root *btrfs_sb(struct super_block *sb)
1442 return sb->s_fs_info;
1445 static inline int btrfs_set_root_name(struct btrfs_root *root,
1446 const char *name, int len)
1448 /* if we already have a name just free it */
1452 root->name = kmalloc(len+1, GFP_KERNEL);
1456 memcpy(root->name, name, len);
1457 root->name[len] ='\0';
1462 static inline u32 btrfs_level_size(struct btrfs_root *root, int level) {
1464 return root->leafsize;
1465 return root->nodesize;
1468 /* helper function to cast into the data area of the leaf. */
1469 #define btrfs_item_ptr(leaf, slot, type) \
1470 ((type *)(btrfs_leaf_data(leaf) + \
1471 btrfs_item_offset_nr(leaf, slot)))
1473 #define btrfs_item_ptr_offset(leaf, slot) \
1474 ((unsigned long)(btrfs_leaf_data(leaf) + \
1475 btrfs_item_offset_nr(leaf, slot)))
1477 static inline struct dentry *fdentry(struct file *file)
1479 return file->f_path.dentry;
1483 int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len);
1484 int btrfs_update_pinned_extents(struct btrfs_root *root,
1485 u64 bytenr, u64 num, int pin);
1486 int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans,
1487 struct btrfs_root *root, struct extent_buffer *leaf);
1488 int btrfs_cross_ref_exists(struct btrfs_trans_handle *trans,
1489 struct btrfs_root *root,
1490 struct btrfs_key *key, u64 bytenr);
1491 int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
1492 struct btrfs_root *root);
1493 int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy);
1494 struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
1495 btrfs_fs_info *info,
1497 struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
1498 struct btrfs_block_group_cache
1499 *hint, u64 search_start,
1500 int data, int owner);
1501 struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
1502 struct btrfs_root *root,
1503 u32 blocksize, u64 parent,
1509 struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
1510 struct btrfs_root *root,
1511 u64 bytenr, u32 blocksize);
1512 int btrfs_shrink_extent_tree(struct btrfs_root *root, u64 new_size);
1513 int btrfs_insert_extent_backref(struct btrfs_trans_handle *trans,
1514 struct btrfs_root *root,
1515 struct btrfs_path *path,
1516 u64 bytenr, u64 parent,
1517 u64 root_objectid, u64 ref_generation,
1518 u64 owner, u64 owner_offset);
1519 int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
1520 struct btrfs_root *root,
1521 u64 num_bytes, u64 parent, u64 min_bytes,
1522 u64 root_objectid, u64 ref_generation,
1523 u64 owner, u64 owner_offset,
1524 u64 empty_size, u64 hint_byte,
1525 u64 search_end, struct btrfs_key *ins, u64 data);
1526 int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
1527 struct btrfs_root *root, u64 parent,
1528 u64 root_objectid, u64 ref_generation,
1529 u64 owner, u64 owner_offset,
1530 struct btrfs_key *ins);
1531 int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans,
1532 struct btrfs_root *root, u64 parent,
1533 u64 root_objectid, u64 ref_generation,
1534 u64 owner, u64 owner_offset,
1535 struct btrfs_key *ins);
1536 int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
1537 struct btrfs_root *root,
1538 u64 num_bytes, u64 min_alloc_size,
1539 u64 empty_size, u64 hint_byte,
1540 u64 search_end, struct btrfs_key *ins,
1542 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1543 struct extent_buffer *orig_buf, struct extent_buffer *buf,
1545 int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1546 struct extent_buffer *buf, u32 nr_extents);
1547 int btrfs_update_ref(struct btrfs_trans_handle *trans,
1548 struct btrfs_root *root, struct extent_buffer *orig_buf,
1549 struct extent_buffer *buf, int start_slot, int nr);
1550 int btrfs_free_extent(struct btrfs_trans_handle *trans,
1551 struct btrfs_root *root,
1552 u64 bytenr, u64 num_bytes, u64 parent,
1553 u64 root_objectid, u64 ref_generation,
1554 u64 owner_objectid, u64 owner_offset, int pin);
1555 int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len);
1556 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
1557 struct btrfs_root *root,
1558 struct extent_io_tree *unpin);
1559 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1560 struct btrfs_root *root,
1561 u64 bytenr, u64 num_bytes, u64 parent,
1562 u64 root_objectid, u64 ref_generation,
1563 u64 owner, u64 owner_offset);
1564 int btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
1565 struct btrfs_root *root, u64 bytenr,
1566 u64 orig_parent, u64 parent,
1567 u64 root_objectid, u64 ref_generation,
1568 u64 owner, u64 owner_offset);
1569 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1570 struct btrfs_root *root);
1571 int btrfs_free_block_groups(struct btrfs_fs_info *info);
1572 int btrfs_read_block_groups(struct btrfs_root *root);
1573 int btrfs_make_block_group(struct btrfs_trans_handle *trans,
1574 struct btrfs_root *root, u64 bytes_used,
1575 u64 type, u64 chunk_objectid, u64 chunk_offset,
1578 int btrfs_previous_item(struct btrfs_root *root,
1579 struct btrfs_path *path, u64 min_objectid,
1581 int btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
1582 struct btrfs_root *root, struct btrfs_path *path,
1583 struct btrfs_key *new_key);
1584 struct extent_buffer *btrfs_root_node(struct btrfs_root *root);
1585 struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root);
1586 int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
1587 struct btrfs_key *key, int lowest_level,
1588 int cache_only, u64 min_trans);
1589 int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
1590 struct btrfs_key *max_key,
1591 struct btrfs_path *path, int cache_only,
1593 int btrfs_cow_block(struct btrfs_trans_handle *trans,
1594 struct btrfs_root *root, struct extent_buffer *buf,
1595 struct extent_buffer *parent, int parent_slot,
1596 struct extent_buffer **cow_ret, u64 prealloc_dest);
1597 int btrfs_copy_root(struct btrfs_trans_handle *trans,
1598 struct btrfs_root *root,
1599 struct extent_buffer *buf,
1600 struct extent_buffer **cow_ret, u64 new_root_objectid);
1601 int btrfs_extend_item(struct btrfs_trans_handle *trans, struct btrfs_root
1602 *root, struct btrfs_path *path, u32 data_size);
1603 int btrfs_truncate_item(struct btrfs_trans_handle *trans,
1604 struct btrfs_root *root,
1605 struct btrfs_path *path,
1606 u32 new_size, int from_end);
1607 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1608 *root, struct btrfs_key *key, struct btrfs_path *p, int
1610 int btrfs_realloc_node(struct btrfs_trans_handle *trans,
1611 struct btrfs_root *root, struct extent_buffer *parent,
1612 int start_slot, int cache_only, u64 *last_ret,
1613 struct btrfs_key *progress);
1614 void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p);
1615 struct btrfs_path *btrfs_alloc_path(void);
1616 void btrfs_free_path(struct btrfs_path *p);
1617 void btrfs_init_path(struct btrfs_path *p);
1618 int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1619 struct btrfs_path *path, int slot, int nr);
1621 static inline int btrfs_del_item(struct btrfs_trans_handle *trans,
1622 struct btrfs_root *root,
1623 struct btrfs_path *path)
1625 return btrfs_del_items(trans, root, path, path->slots[0], 1);
1628 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
1629 *root, struct btrfs_key *key, void *data, u32 data_size);
1630 int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
1631 struct btrfs_root *root,
1632 struct btrfs_path *path,
1633 struct btrfs_key *cpu_key, u32 *data_size, int nr);
1635 static inline int btrfs_insert_empty_item(struct btrfs_trans_handle *trans,
1636 struct btrfs_root *root,
1637 struct btrfs_path *path,
1638 struct btrfs_key *key,
1641 return btrfs_insert_empty_items(trans, root, path, key, &data_size, 1);
1644 int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path);
1645 int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path);
1646 int btrfs_leaf_free_space(struct btrfs_root *root, struct extent_buffer *leaf);
1647 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
1650 int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1651 struct btrfs_key *key);
1652 int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
1653 *root, struct btrfs_key *key, struct btrfs_root_item
1655 int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
1656 *root, struct btrfs_key *key, struct btrfs_root_item
1658 int btrfs_find_last_root(struct btrfs_root *root, u64 objectid, struct
1659 btrfs_root_item *item, struct btrfs_key *key);
1660 int btrfs_search_root(struct btrfs_root *root, u64 search_start,
1661 u64 *found_objectid);
1662 int btrfs_find_dead_roots(struct btrfs_root *root, u64 objectid,
1663 struct btrfs_root *latest_root);
1665 int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
1666 *root, const char *name, int name_len, u64 dir,
1667 struct btrfs_key *location, u8 type, u64 index);
1668 struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans,
1669 struct btrfs_root *root,
1670 struct btrfs_path *path, u64 dir,
1671 const char *name, int name_len,
1673 struct btrfs_dir_item *
1674 btrfs_lookup_dir_index_item(struct btrfs_trans_handle *trans,
1675 struct btrfs_root *root,
1676 struct btrfs_path *path, u64 dir,
1677 u64 objectid, const char *name, int name_len,
1679 struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_root *root,
1680 struct btrfs_path *path,
1681 const char *name, int name_len);
1682 int btrfs_delete_one_dir_name(struct btrfs_trans_handle *trans,
1683 struct btrfs_root *root,
1684 struct btrfs_path *path,
1685 struct btrfs_dir_item *di);
1686 int btrfs_insert_xattr_item(struct btrfs_trans_handle *trans,
1687 struct btrfs_root *root, const char *name,
1688 u16 name_len, const void *data, u16 data_len,
1690 struct btrfs_dir_item *btrfs_lookup_xattr(struct btrfs_trans_handle *trans,
1691 struct btrfs_root *root,
1692 struct btrfs_path *path, u64 dir,
1693 const char *name, u16 name_len,
1697 int btrfs_insert_orphan_item(struct btrfs_trans_handle *trans,
1698 struct btrfs_root *root, u64 offset);
1699 int btrfs_del_orphan_item(struct btrfs_trans_handle *trans,
1700 struct btrfs_root *root, u64 offset);
1703 int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
1704 struct btrfs_root *fs_root,
1705 u64 dirid, u64 *objectid);
1706 int btrfs_find_highest_inode(struct btrfs_root *fs_root, u64 *objectid);
1709 int btrfs_insert_inode_ref(struct btrfs_trans_handle *trans,
1710 struct btrfs_root *root,
1711 const char *name, int name_len,
1712 u64 inode_objectid, u64 ref_objectid, u64 index);
1713 int btrfs_del_inode_ref(struct btrfs_trans_handle *trans,
1714 struct btrfs_root *root,
1715 const char *name, int name_len,
1716 u64 inode_objectid, u64 ref_objectid, u64 *index);
1717 int btrfs_insert_empty_inode(struct btrfs_trans_handle *trans,
1718 struct btrfs_root *root,
1719 struct btrfs_path *path, u64 objectid);
1720 int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root
1721 *root, struct btrfs_path *path,
1722 struct btrfs_key *location, int mod);
1725 int btrfs_lookup_bio_sums(struct btrfs_root *root, struct inode *inode,
1727 int btrfs_insert_file_extent(struct btrfs_trans_handle *trans,
1728 struct btrfs_root *root,
1729 u64 objectid, u64 pos, u64 disk_offset,
1731 u64 num_bytes, u64 offset);
1732 int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
1733 struct btrfs_root *root,
1734 struct btrfs_path *path, u64 objectid,
1735 u64 bytenr, int mod);
1736 int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans,
1737 struct btrfs_root *root, struct inode *inode,
1738 struct btrfs_ordered_sum *sums);
1739 int btrfs_csum_one_bio(struct btrfs_root *root, struct inode *inode,
1741 struct btrfs_csum_item *btrfs_lookup_csum(struct btrfs_trans_handle *trans,
1742 struct btrfs_root *root,
1743 struct btrfs_path *path,
1744 u64 objectid, u64 offset,
1746 int btrfs_csum_truncate(struct btrfs_trans_handle *trans,
1747 struct btrfs_root *root, struct btrfs_path *path,
1751 /* RHEL and EL kernels have a patch that renames PG_checked to FsMisc */
1752 #if defined(ClearPageFsMisc) && !defined(ClearPageChecked)
1753 #define ClearPageChecked ClearPageFsMisc
1754 #define SetPageChecked SetPageFsMisc
1755 #define PageChecked PageFsMisc
1758 int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
1759 struct btrfs_root *root,
1760 struct inode *dir, struct inode *inode,
1761 const char *name, int name_len);
1762 int btrfs_add_link(struct btrfs_trans_handle *trans,
1763 struct inode *parent_inode, struct inode *inode,
1764 const char *name, int name_len, int add_backref, u64 index);
1765 int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
1766 struct btrfs_root *root,
1767 struct inode *inode, u64 new_size,
1770 int btrfs_start_delalloc_inodes(struct btrfs_root *root);
1771 int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end);
1772 int btrfs_writepages(struct address_space *mapping,
1773 struct writeback_control *wbc);
1774 int btrfs_create_subvol_root(struct btrfs_root *new_root,
1775 struct btrfs_trans_handle *trans, u64 new_dirid,
1776 struct btrfs_block_group_cache *block_group);
1778 void btrfs_invalidate_dcache_root(struct btrfs_root *root, char *name,
1781 int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
1782 size_t size, struct bio *bio);
1784 static inline void dec_i_blocks(struct inode *inode, u64 dec)
1787 if (dec <= inode->i_blocks)
1788 inode->i_blocks -= dec;
1790 inode->i_blocks = 0;
1793 unsigned long btrfs_force_ra(struct address_space *mapping,
1794 struct file_ra_state *ra, struct file *file,
1795 pgoff_t offset, pgoff_t last_index);
1796 int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
1798 int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page);
1799 int btrfs_readpage(struct file *file, struct page *page);
1800 void btrfs_delete_inode(struct inode *inode);
1801 void btrfs_put_inode(struct inode *inode);
1802 void btrfs_read_locked_inode(struct inode *inode);
1803 int btrfs_write_inode(struct inode *inode, int wait);
1804 void btrfs_dirty_inode(struct inode *inode);
1805 struct inode *btrfs_alloc_inode(struct super_block *sb);
1806 void btrfs_destroy_inode(struct inode *inode);
1807 int btrfs_init_cachep(void);
1808 void btrfs_destroy_cachep(void);
1809 long btrfs_ioctl_trans_end(struct file *file);
1810 struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1811 struct btrfs_root *root);
1812 struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
1813 struct btrfs_root *root, int *is_new);
1814 int btrfs_commit_write(struct file *file, struct page *page,
1815 unsigned from, unsigned to);
1816 struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
1817 size_t page_offset, u64 start, u64 end,
1819 int btrfs_update_inode(struct btrfs_trans_handle *trans,
1820 struct btrfs_root *root,
1821 struct inode *inode);
1824 long btrfs_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
1827 int btrfs_sync_file(struct file *file, struct dentry *dentry, int datasync);
1828 int btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end);
1829 int btrfs_check_file(struct btrfs_root *root, struct inode *inode);
1830 extern struct file_operations btrfs_file_operations;
1831 int btrfs_drop_extents(struct btrfs_trans_handle *trans,
1832 struct btrfs_root *root, struct inode *inode,
1833 u64 start, u64 end, u64 inline_limit, u64 *hint_block);
1834 int btrfs_release_file(struct inode *inode, struct file *file);
1837 int btrfs_defrag_leaves(struct btrfs_trans_handle *trans,
1838 struct btrfs_root *root, int cache_only);
1841 int btrfs_init_sysfs(void);
1842 void btrfs_exit_sysfs(void);
1843 int btrfs_sysfs_add_super(struct btrfs_fs_info *fs);
1844 int btrfs_sysfs_add_root(struct btrfs_root *root);
1845 void btrfs_sysfs_del_root(struct btrfs_root *root);
1846 void btrfs_sysfs_del_super(struct btrfs_fs_info *root);
1849 ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size);
1852 u64 btrfs_parse_size(char *str);
1853 int btrfs_parse_options(struct btrfs_root *root, char *options);
1854 int btrfs_sync_fs(struct super_block *sb, int wait);
1857 int btrfs_check_acl(struct inode *inode, int mask);
1858 int btrfs_init_acl(struct inode *inode, struct inode *dir);
1859 int btrfs_acl_chmod(struct inode *inode);
1861 /* free-space-cache.c */
1862 int btrfs_add_free_space(struct btrfs_block_group_cache *block_group,
1863 u64 bytenr, u64 size);
1864 int btrfs_remove_free_space(struct btrfs_block_group_cache *block_group,
1865 u64 bytenr, u64 size);
1866 void btrfs_remove_free_space_cache(struct btrfs_block_group_cache
1868 struct btrfs_free_space *btrfs_find_free_space(struct btrfs_block_group_cache
1869 *block_group, u64 offset,
1871 void btrfs_dump_free_space(struct btrfs_block_group_cache *block_group,
1873 u64 btrfs_block_group_free_space(struct btrfs_block_group_cache *block_group);