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_io.h"
29 struct btrfs_trans_handle;
30 #define BTRFS_MAGIC "_BHRfS_M"
32 #define BTRFS_MAX_LEVEL 8
34 /* holds pointers to all of the tree roots */
35 #define BTRFS_ROOT_TREE_OBJECTID 1ULL
37 /* stores information about which extents are in use, and reference counts */
38 #define BTRFS_EXTENT_TREE_OBJECTID 2ULL
41 * chunk tree stores translations from logical -> physical block numbering
42 * the super block points to the chunk tree
44 #define BTRFS_CHUNK_TREE_OBJECTID 3ULL
47 * stores information about which areas of a given device are in use.
48 * one per device. The tree of tree roots points to the device tree
50 #define BTRFS_DEV_TREE_OBJECTID 4ULL
52 /* one per subvolume, storing files and directories */
53 #define BTRFS_FS_TREE_OBJECTID 5ULL
55 /* directory objectid inside the root tree */
56 #define BTRFS_ROOT_TREE_DIR_OBJECTID 6ULL
57 /* holds checksums of all the data extents */
58 #define BTRFS_CSUM_TREE_OBJECTID 7ULL
61 /* oprhan objectid for tracking unlinked/truncated files */
62 #define BTRFS_ORPHAN_OBJECTID -5ULL
64 /* does write ahead logging to speed up fsyncs */
65 #define BTRFS_TREE_LOG_OBJECTID -6ULL
66 #define BTRFS_TREE_LOG_FIXUP_OBJECTID -7ULL
69 #define BTRFS_TREE_RELOC_OBJECTID -8ULL
70 #define BTRFS_DATA_RELOC_TREE_OBJECTID -9ULL
73 * extent checksums all have this objectid
74 * this allows them to share the logging tree
77 #define BTRFS_EXTENT_CSUM_OBJECTID -10ULL
79 /* dummy objectid represents multiple objectids */
80 #define BTRFS_MULTIPLE_OBJECTIDS -255ULL
83 * All files have objectids in this range.
85 #define BTRFS_FIRST_FREE_OBJECTID 256ULL
86 #define BTRFS_LAST_FREE_OBJECTID -256ULL
87 #define BTRFS_FIRST_CHUNK_TREE_OBJECTID 256ULL
92 * the device items go into the chunk tree. The key is in the form
93 * [ 1 BTRFS_DEV_ITEM_KEY device_id ]
95 #define BTRFS_DEV_ITEMS_OBJECTID 1ULL
98 * we can actually store much bigger names, but lets not confuse the rest
101 #define BTRFS_NAME_LEN 255
103 /* 32 bytes in various csum fields */
104 #define BTRFS_CSUM_SIZE 32
107 #define BTRFS_CSUM_TYPE_CRC32 0
111 #define BTRFS_CSUM_TYPE_CRC32 0
113 static int btrfs_csum_sizes[] = { 4, 0 };
115 /* four bytes for CRC32 */
116 #define BTRFS_CRC32_SIZE 4
117 #define BTRFS_EMPTY_DIR_SIZE 0
119 #define BTRFS_FT_UNKNOWN 0
120 #define BTRFS_FT_REG_FILE 1
121 #define BTRFS_FT_DIR 2
122 #define BTRFS_FT_CHRDEV 3
123 #define BTRFS_FT_BLKDEV 4
124 #define BTRFS_FT_FIFO 5
125 #define BTRFS_FT_SOCK 6
126 #define BTRFS_FT_SYMLINK 7
127 #define BTRFS_FT_XATTR 8
128 #define BTRFS_FT_MAX 9
131 * the key defines the order in the tree, and so it also defines (optimal)
132 * block layout. objectid corresonds to the inode number. The flags
133 * tells us things about the object, and is a kind of stream selector.
134 * so for a given inode, keys with flags of 1 might refer to the inode
135 * data, flags of 2 may point to file data in the btree and flags == 3
136 * may point to extents.
138 * offset is the starting byte offset for this key in the stream.
140 * btrfs_disk_key is in disk byte order. struct btrfs_key is always
141 * in cpu native order. Otherwise they are identical and their sizes
142 * should be the same (ie both packed)
144 struct btrfs_disk_key {
148 } __attribute__ ((__packed__));
154 } __attribute__ ((__packed__));
156 struct btrfs_mapping_tree {
157 struct cache_tree cache_tree;
160 #define BTRFS_UUID_SIZE 16
161 struct btrfs_dev_item {
162 /* the internal btrfs device id */
165 /* size of the device */
171 /* optimal io alignment for this device */
174 /* optimal io width for this device */
177 /* minimal io size for this device */
180 /* type and info about this device */
183 /* expected generation for this device */
187 * starting byte of this partition on the device,
188 * to allowr for stripe alignment in the future
192 /* grouping information for allocation decisions */
195 /* seek speed 0-100 where 100 is fastest */
198 /* bandwidth 0-100 where 100 is fastest */
201 /* btrfs generated uuid for this device */
202 u8 uuid[BTRFS_UUID_SIZE];
204 /* uuid of FS who owns this device */
205 u8 fsid[BTRFS_UUID_SIZE];
206 } __attribute__ ((__packed__));
208 struct btrfs_stripe {
211 u8 dev_uuid[BTRFS_UUID_SIZE];
212 } __attribute__ ((__packed__));
215 /* size of this chunk in bytes */
218 /* objectid of the root referencing this chunk */
224 /* optimal io alignment for this chunk */
227 /* optimal io width for this chunk */
230 /* minimal io size for this chunk */
233 /* 2^16 stripes is quite a lot, a second limit is the size of a single
238 /* sub stripes only matter for raid10 */
240 struct btrfs_stripe stripe;
241 /* additional stripes go here */
242 } __attribute__ ((__packed__));
244 static inline unsigned long btrfs_chunk_item_size(int num_stripes)
246 BUG_ON(num_stripes == 0);
247 return sizeof(struct btrfs_chunk) +
248 sizeof(struct btrfs_stripe) * (num_stripes - 1);
251 #define BTRFS_FSID_SIZE 16
252 #define BTRFS_HEADER_FLAG_WRITTEN (1 << 0)
255 * every tree block (leaf or node) starts with this header.
257 struct btrfs_header {
258 /* these first four must match the super block */
259 u8 csum[BTRFS_CSUM_SIZE];
260 u8 fsid[BTRFS_FSID_SIZE]; /* FS specific uuid */
261 __le64 bytenr; /* which block this node is supposed to live in */
264 /* allowed to be different from the super from here on down */
265 u8 chunk_tree_uuid[BTRFS_UUID_SIZE];
270 } __attribute__ ((__packed__));
272 #define BTRFS_NODEPTRS_PER_BLOCK(r) (((r)->nodesize - \
273 sizeof(struct btrfs_header)) / \
274 sizeof(struct btrfs_key_ptr))
275 #define __BTRFS_LEAF_DATA_SIZE(bs) ((bs) - sizeof(struct btrfs_header))
276 #define BTRFS_LEAF_DATA_SIZE(r) (__BTRFS_LEAF_DATA_SIZE(r->leafsize))
277 #define BTRFS_MAX_INLINE_DATA_SIZE(r) (BTRFS_LEAF_DATA_SIZE(r) - \
278 sizeof(struct btrfs_item) - \
279 sizeof(struct btrfs_file_extent_item))
281 #define BTRFS_SUPER_FLAG_SEEDING (1ULL << 32)
282 #define BTRFS_SUPER_FLAG_METADUMP (1ULL << 33)
285 * this is a very generous portion of the super block, giving us
286 * room to translate 14 chunks with 3 stripes each.
288 #define BTRFS_SYSTEM_CHUNK_ARRAY_SIZE 2048
289 #define BTRFS_LABEL_SIZE 256
292 * the super block basically lists the main trees of the FS
293 * it currently lacks any block count etc etc
295 struct btrfs_super_block {
296 u8 csum[BTRFS_CSUM_SIZE];
297 /* the first 3 fields must match struct btrfs_header */
298 u8 fsid[BTRFS_FSID_SIZE]; /* FS specific uuid */
299 __le64 bytenr; /* this block number */
302 /* allowed to be different from the btrfs_header from here own down */
309 /* this will help find the new super based on the log root */
310 __le64 log_root_transid;
313 __le64 root_dir_objectid;
319 __le32 sys_chunk_array_size;
320 __le64 chunk_root_generation;
322 __le64 compat_ro_flags;
323 __le64 incompat_flags;
328 struct btrfs_dev_item dev_item;
330 char label[BTRFS_LABEL_SIZE];
332 /* future expansion */
334 u8 sys_chunk_array[BTRFS_SYSTEM_CHUNK_ARRAY_SIZE];
335 } __attribute__ ((__packed__));
338 * Compat flags that we support. If any incompat flags are set other than the
339 * ones specified below then we will fail to mount
341 #define BTRFS_FEATURE_COMPAT_SUPP 0x0
342 #define BTRFS_FEATURE_COMPAT_RO_SUPP 0x0
343 #define BTRFS_FEATURE_INCOMPAT_SUPP 0x0
346 * A leaf is full of items. offset and size tell us where to find
347 * the item in the leaf (relative to the start of the data area)
350 struct btrfs_disk_key key;
353 } __attribute__ ((__packed__));
356 * leaves have an item area and a data area:
357 * [item0, item1....itemN] [free space] [dataN...data1, data0]
359 * The data is separate from the items to get the keys closer together
363 struct btrfs_header header;
364 struct btrfs_item items[];
365 } __attribute__ ((__packed__));
368 * all non-leaf blocks are nodes, they hold only keys and pointers to
371 struct btrfs_key_ptr {
372 struct btrfs_disk_key key;
375 } __attribute__ ((__packed__));
378 struct btrfs_header header;
379 struct btrfs_key_ptr ptrs[];
380 } __attribute__ ((__packed__));
383 * btrfs_paths remember the path taken from the root down to the leaf.
384 * level 0 is always the leaf, and nodes[1...BTRFS_MAX_LEVEL] will point
385 * to any other levels that are present.
387 * The slots array records the index of the item or block pointer
388 * used while walking the tree.
391 struct extent_buffer *nodes[BTRFS_MAX_LEVEL];
392 int slots[BTRFS_MAX_LEVEL];
397 * set by btrfs_split_item, tells search_slot to keep all locks
398 * and to force calls to keep space in the nodes
400 int search_for_split;
404 * items in the extent btree are used to record the objectid of the
405 * owner of the block and the number of references
407 struct btrfs_extent_item {
409 } __attribute__ ((__packed__));
411 struct btrfs_extent_ref {
416 } __attribute__ ((__packed__));
418 /* dev extents record free space on individual devices. The owner
419 * field points back to the chunk allocation mapping tree that allocated
420 * the extent. The chunk tree uuid field is a way to double check the owner
422 struct btrfs_dev_extent {
424 __le64 chunk_objectid;
427 u8 chunk_tree_uuid[BTRFS_UUID_SIZE];
428 } __attribute__ ((__packed__));
430 struct btrfs_inode_ref {
434 } __attribute__ ((__packed__));
436 struct btrfs_timespec {
439 } __attribute__ ((__packed__));
442 BTRFS_COMPRESS_NONE = 0,
443 BTRFS_COMPRESS_ZLIB = 1,
444 BTRFS_COMPRESS_LAST = 2,
445 } btrfs_compression_type;
447 /* we don't understand any encryption methods right now */
449 BTRFS_ENCRYPTION_NONE = 0,
450 BTRFS_ENCRYPTION_LAST = 1,
451 } btrfs_encryption_type;
453 struct btrfs_inode_item {
454 /* nfs style generation number */
456 /* transid that last touched this inode */
468 /* modification sequence number for NFS */
472 * a little future expansion, for more than this we can
473 * just grow the inode item and version it
476 struct btrfs_timespec atime;
477 struct btrfs_timespec ctime;
478 struct btrfs_timespec mtime;
479 struct btrfs_timespec otime;
480 } __attribute__ ((__packed__));
482 struct btrfs_dir_item {
483 struct btrfs_disk_key location;
488 } __attribute__ ((__packed__));
490 struct btrfs_root_item {
491 struct btrfs_inode_item inode;
497 __le64 last_snapshot;
500 struct btrfs_disk_key drop_progress;
503 } __attribute__ ((__packed__));
506 * this is used for both forward and backward root refs
508 struct btrfs_root_ref {
512 } __attribute__ ((__packed__));
514 #define BTRFS_FILE_EXTENT_INLINE 0
515 #define BTRFS_FILE_EXTENT_REG 1
516 #define BTRFS_FILE_EXTENT_PREALLOC 2
518 struct btrfs_file_extent_item {
520 * transaction id that created this extent
524 * max number of bytes to hold this extent in ram
525 * when we split a compressed extent we can't know how big
526 * each of the resulting pieces will be. So, this is
527 * an upper limit on the size of the extent in ram instead of
533 * 32 bits for the various ways we might encode the data,
534 * including compression and encryption. If any of these
535 * are set to something a given disk format doesn't understand
536 * it is treated like an incompat flag for reading and writing,
541 __le16 other_encoding; /* spare for later use */
543 /* are we inline data or a real extent? */
547 * disk space consumed by the extent, checksum blocks are included
551 __le64 disk_num_bytes;
553 * the logical offset in file blocks (no csums)
554 * this extent record is for. This allows a file extent to point
555 * into the middle of an existing extent on disk, sharing it
556 * between two snapshots (useful if some bytes in the middle of the
557 * extent have changed
561 * the logical number of file blocks (no csums included)
565 } __attribute__ ((__packed__));
567 struct btrfs_csum_item {
569 } __attribute__ ((__packed__));
571 /* tag for the radix tree of block groups in ram */
572 #define BTRFS_BLOCK_GROUP_DATA (1 << 0)
573 #define BTRFS_BLOCK_GROUP_SYSTEM (1 << 1)
574 #define BTRFS_BLOCK_GROUP_METADATA (1 << 2)
575 #define BTRFS_BLOCK_GROUP_RAID0 (1 << 3)
576 #define BTRFS_BLOCK_GROUP_RAID1 (1 << 4)
577 #define BTRFS_BLOCK_GROUP_DUP (1 << 5)
578 #define BTRFS_BLOCK_GROUP_RAID10 (1 << 6)
580 struct btrfs_block_group_item {
582 __le64 chunk_objectid;
584 } __attribute__ ((__packed__));
586 struct btrfs_space_info {
592 struct list_head list;
595 struct btrfs_block_group_cache {
596 struct cache_extent cache;
597 struct btrfs_key key;
598 struct btrfs_block_group_item item;
599 struct btrfs_space_info *space_info;
606 struct btrfs_extent_ops {
607 int (*alloc_extent)(struct btrfs_root *root, u64 num_bytes,
608 u64 hint_byte, struct btrfs_key *ins);
609 int (*free_extent)(struct btrfs_root *root, u64 bytenr,
614 struct btrfs_fs_devices;
615 struct btrfs_fs_info {
616 u8 fsid[BTRFS_FSID_SIZE];
617 u8 chunk_tree_uuid[BTRFS_UUID_SIZE];
618 struct btrfs_root *fs_root;
619 struct btrfs_root *extent_root;
620 struct btrfs_root *tree_root;
621 struct btrfs_root *chunk_root;
622 struct btrfs_root *dev_root;
623 struct btrfs_root *csum_root;
625 /* the log root tree is a directory of all the other log roots */
626 struct btrfs_root *log_root_tree;
628 struct extent_io_tree extent_cache;
629 struct extent_io_tree free_space_cache;
630 struct extent_io_tree block_group_cache;
631 struct extent_io_tree pinned_extents;
632 struct extent_io_tree pending_del;
633 struct extent_io_tree extent_ins;
635 /* logical->physical extent mapping */
636 struct btrfs_mapping_tree mapping_tree;
639 u64 last_trans_committed;
641 u64 avail_data_alloc_bits;
642 u64 avail_metadata_alloc_bits;
643 u64 avail_system_alloc_bits;
644 u64 data_alloc_profile;
645 u64 metadata_alloc_profile;
646 u64 system_alloc_profile;
649 struct btrfs_trans_handle *running_transaction;
650 struct btrfs_super_block super_copy;
651 struct mutex fs_mutex;
656 struct btrfs_extent_ops *extent_ops;
657 struct list_head dirty_cowonly_roots;
659 struct btrfs_fs_devices *fs_devices;
660 struct list_head space_info;
666 * in ram representation of the tree. extent_root is used for all allocations
667 * and for the extent tree extent_root root.
670 struct extent_buffer *node;
671 struct extent_buffer *commit_root;
672 struct btrfs_root_item root_item;
673 struct btrfs_key root_key;
674 struct btrfs_fs_info *fs_info;
678 /* data allocations are done in sectorsize units */
681 /* node allocations are done in nodesize units */
684 /* leaf allocations are done in leafsize units */
687 /* leaf allocations are done in leafsize units */
696 u64 last_inode_alloc;
698 /* the dirty list is only used by non-reference counted roots */
699 struct list_head dirty_list;
703 * inode items have the data typically returned from stat and store other
704 * info about object characteristics. There is one for every file and dir in
707 #define BTRFS_INODE_ITEM_KEY 1
708 #define BTRFS_INODE_REF_KEY 12
709 #define BTRFS_XATTR_ITEM_KEY 24
710 #define BTRFS_ORPHAN_ITEM_KEY 48
712 #define BTRFS_DIR_LOG_ITEM_KEY 60
713 #define BTRFS_DIR_LOG_INDEX_KEY 72
715 * dir items are the name -> inode pointers in a directory. There is one
716 * for every name in a directory.
718 #define BTRFS_DIR_ITEM_KEY 84
719 #define BTRFS_DIR_INDEX_KEY 96
722 * extent data is for file data
724 #define BTRFS_EXTENT_DATA_KEY 108
727 * csum items have the checksums for data in the extents
729 #define BTRFS_CSUM_ITEM_KEY 120
731 * extent csums are stored in a separate tree and hold csums for
732 * an entire extent on disk.
734 #define BTRFS_EXTENT_CSUM_KEY 128
737 * root items point to tree roots. There are typically in the root
738 * tree used by the super block to find all the other trees
740 #define BTRFS_ROOT_ITEM_KEY 132
743 * root backrefs tie subvols and snapshots to the directory entries that
746 #define BTRFS_ROOT_BACKREF_KEY 144
749 * root refs make a fast index for listing all of the snapshots and
750 * subvolumes referenced by a given root. They point directly to the
751 * directory item in the root that references the subvol
753 #define BTRFS_ROOT_REF_KEY 156
756 * extent items are in the extent map tree. These record which blocks
757 * are used, and how many references there are to each block
759 #define BTRFS_EXTENT_ITEM_KEY 168
760 #define BTRFS_EXTENT_REF_KEY 180
763 * block groups give us hints into the extent allocation trees. Which
764 * blocks are free etc etc
766 #define BTRFS_BLOCK_GROUP_ITEM_KEY 192
768 #define BTRFS_DEV_EXTENT_KEY 204
769 #define BTRFS_DEV_ITEM_KEY 216
770 #define BTRFS_CHUNK_ITEM_KEY 228
773 * string items are for debugging. They just store a short string of
776 #define BTRFS_STRING_ITEM_KEY 253
780 #define BTRFS_INODE_NODATASUM (1 << 0)
781 #define BTRFS_INODE_NODATACOW (1 << 1)
782 #define BTRFS_INODE_READONLY (1 << 2)
784 #define read_eb_member(eb, ptr, type, member, result) ( \
785 read_extent_buffer(eb, (char *)(result), \
786 ((unsigned long)(ptr)) + \
787 offsetof(type, member), \
788 sizeof(((type *)0)->member)))
790 #define write_eb_member(eb, ptr, type, member, result) ( \
791 write_extent_buffer(eb, (char *)(result), \
792 ((unsigned long)(ptr)) + \
793 offsetof(type, member), \
794 sizeof(((type *)0)->member)))
796 #define BTRFS_SETGET_HEADER_FUNCS(name, type, member, bits) \
797 static inline u##bits btrfs_##name(struct extent_buffer *eb) \
799 struct btrfs_header *h = (struct btrfs_header *)eb->data; \
800 return le##bits##_to_cpu(h->member); \
802 static inline void btrfs_set_##name(struct extent_buffer *eb, \
805 struct btrfs_header *h = (struct btrfs_header *)eb->data; \
806 h->member = cpu_to_le##bits(val); \
809 #define BTRFS_SETGET_FUNCS(name, type, member, bits) \
810 static inline u##bits btrfs_##name(struct extent_buffer *eb, \
813 unsigned long offset = (unsigned long)s; \
814 type *p = (type *) (eb->data + offset); \
815 return le##bits##_to_cpu(p->member); \
817 static inline void btrfs_set_##name(struct extent_buffer *eb, \
818 type *s, u##bits val) \
820 unsigned long offset = (unsigned long)s; \
821 type *p = (type *) (eb->data + offset); \
822 p->member = cpu_to_le##bits(val); \
825 #define BTRFS_SETGET_STACK_FUNCS(name, type, member, bits) \
826 static inline u##bits btrfs_##name(type *s) \
828 return le##bits##_to_cpu(s->member); \
830 static inline void btrfs_set_##name(type *s, u##bits val) \
832 s->member = cpu_to_le##bits(val); \
835 BTRFS_SETGET_FUNCS(device_type, struct btrfs_dev_item, type, 64);
836 BTRFS_SETGET_FUNCS(device_total_bytes, struct btrfs_dev_item, total_bytes, 64);
837 BTRFS_SETGET_FUNCS(device_bytes_used, struct btrfs_dev_item, bytes_used, 64);
838 BTRFS_SETGET_FUNCS(device_io_align, struct btrfs_dev_item, io_align, 32);
839 BTRFS_SETGET_FUNCS(device_io_width, struct btrfs_dev_item, io_width, 32);
840 BTRFS_SETGET_FUNCS(device_start_offset, struct btrfs_dev_item,
842 BTRFS_SETGET_FUNCS(device_sector_size, struct btrfs_dev_item, sector_size, 32);
843 BTRFS_SETGET_FUNCS(device_id, struct btrfs_dev_item, devid, 64);
844 BTRFS_SETGET_FUNCS(device_group, struct btrfs_dev_item, dev_group, 32);
845 BTRFS_SETGET_FUNCS(device_seek_speed, struct btrfs_dev_item, seek_speed, 8);
846 BTRFS_SETGET_FUNCS(device_bandwidth, struct btrfs_dev_item, bandwidth, 8);
847 BTRFS_SETGET_FUNCS(device_generation, struct btrfs_dev_item, generation, 64);
849 BTRFS_SETGET_STACK_FUNCS(stack_device_type, struct btrfs_dev_item, type, 64);
850 BTRFS_SETGET_STACK_FUNCS(stack_device_total_bytes, struct btrfs_dev_item,
852 BTRFS_SETGET_STACK_FUNCS(stack_device_bytes_used, struct btrfs_dev_item,
854 BTRFS_SETGET_STACK_FUNCS(stack_device_io_align, struct btrfs_dev_item,
856 BTRFS_SETGET_STACK_FUNCS(stack_device_io_width, struct btrfs_dev_item,
858 BTRFS_SETGET_STACK_FUNCS(stack_device_sector_size, struct btrfs_dev_item,
860 BTRFS_SETGET_STACK_FUNCS(stack_device_id, struct btrfs_dev_item, devid, 64);
861 BTRFS_SETGET_STACK_FUNCS(stack_device_group, struct btrfs_dev_item,
863 BTRFS_SETGET_STACK_FUNCS(stack_device_seek_speed, struct btrfs_dev_item,
865 BTRFS_SETGET_STACK_FUNCS(stack_device_bandwidth, struct btrfs_dev_item,
867 BTRFS_SETGET_STACK_FUNCS(stack_device_generation, struct btrfs_dev_item,
870 static inline char *btrfs_device_uuid(struct btrfs_dev_item *d)
872 return (char *)d + offsetof(struct btrfs_dev_item, uuid);
875 static inline char *btrfs_device_fsid(struct btrfs_dev_item *d)
877 return (char *)d + offsetof(struct btrfs_dev_item, fsid);
880 BTRFS_SETGET_FUNCS(chunk_length, struct btrfs_chunk, length, 64);
881 BTRFS_SETGET_FUNCS(chunk_owner, struct btrfs_chunk, owner, 64);
882 BTRFS_SETGET_FUNCS(chunk_stripe_len, struct btrfs_chunk, stripe_len, 64);
883 BTRFS_SETGET_FUNCS(chunk_io_align, struct btrfs_chunk, io_align, 32);
884 BTRFS_SETGET_FUNCS(chunk_io_width, struct btrfs_chunk, io_width, 32);
885 BTRFS_SETGET_FUNCS(chunk_sector_size, struct btrfs_chunk, sector_size, 32);
886 BTRFS_SETGET_FUNCS(chunk_type, struct btrfs_chunk, type, 64);
887 BTRFS_SETGET_FUNCS(chunk_num_stripes, struct btrfs_chunk, num_stripes, 16);
888 BTRFS_SETGET_FUNCS(chunk_sub_stripes, struct btrfs_chunk, sub_stripes, 16);
889 BTRFS_SETGET_FUNCS(stripe_devid, struct btrfs_stripe, devid, 64);
890 BTRFS_SETGET_FUNCS(stripe_offset, struct btrfs_stripe, offset, 64);
892 static inline char *btrfs_stripe_dev_uuid(struct btrfs_stripe *s)
894 return (char *)s + offsetof(struct btrfs_stripe, dev_uuid);
897 BTRFS_SETGET_STACK_FUNCS(stack_chunk_length, struct btrfs_chunk, length, 64);
898 BTRFS_SETGET_STACK_FUNCS(stack_chunk_owner, struct btrfs_chunk, owner, 64);
899 BTRFS_SETGET_STACK_FUNCS(stack_chunk_stripe_len, struct btrfs_chunk,
901 BTRFS_SETGET_STACK_FUNCS(stack_chunk_io_align, struct btrfs_chunk,
903 BTRFS_SETGET_STACK_FUNCS(stack_chunk_io_width, struct btrfs_chunk,
905 BTRFS_SETGET_STACK_FUNCS(stack_chunk_sector_size, struct btrfs_chunk,
907 BTRFS_SETGET_STACK_FUNCS(stack_chunk_type, struct btrfs_chunk, type, 64);
908 BTRFS_SETGET_STACK_FUNCS(stack_chunk_num_stripes, struct btrfs_chunk,
910 BTRFS_SETGET_STACK_FUNCS(stack_chunk_sub_stripes, struct btrfs_chunk,
912 BTRFS_SETGET_STACK_FUNCS(stack_stripe_devid, struct btrfs_stripe, devid, 64);
913 BTRFS_SETGET_STACK_FUNCS(stack_stripe_offset, struct btrfs_stripe, offset, 64);
915 static inline struct btrfs_stripe *btrfs_stripe_nr(struct btrfs_chunk *c,
918 unsigned long offset = (unsigned long)c;
919 offset += offsetof(struct btrfs_chunk, stripe);
920 offset += nr * sizeof(struct btrfs_stripe);
921 return (struct btrfs_stripe *)offset;
924 static inline char *btrfs_stripe_dev_uuid_nr(struct btrfs_chunk *c, int nr)
926 return btrfs_stripe_dev_uuid(btrfs_stripe_nr(c, nr));
929 static inline u64 btrfs_stripe_offset_nr(struct extent_buffer *eb,
930 struct btrfs_chunk *c, int nr)
932 return btrfs_stripe_offset(eb, btrfs_stripe_nr(c, nr));
935 static inline void btrfs_set_stripe_offset_nr(struct extent_buffer *eb,
936 struct btrfs_chunk *c, int nr,
939 btrfs_set_stripe_offset(eb, btrfs_stripe_nr(c, nr), val);
942 static inline u64 btrfs_stripe_devid_nr(struct extent_buffer *eb,
943 struct btrfs_chunk *c, int nr)
945 return btrfs_stripe_devid(eb, btrfs_stripe_nr(c, nr));
948 static inline void btrfs_set_stripe_devid_nr(struct extent_buffer *eb,
949 struct btrfs_chunk *c, int nr,
952 btrfs_set_stripe_devid(eb, btrfs_stripe_nr(c, nr), val);
955 /* struct btrfs_block_group_item */
956 BTRFS_SETGET_STACK_FUNCS(block_group_used, struct btrfs_block_group_item,
958 BTRFS_SETGET_FUNCS(disk_block_group_used, struct btrfs_block_group_item,
960 BTRFS_SETGET_STACK_FUNCS(block_group_chunk_objectid,
961 struct btrfs_block_group_item, chunk_objectid, 64);
963 BTRFS_SETGET_FUNCS(disk_block_group_chunk_objectid,
964 struct btrfs_block_group_item, chunk_objectid, 64);
965 BTRFS_SETGET_FUNCS(disk_block_group_flags,
966 struct btrfs_block_group_item, flags, 64);
967 BTRFS_SETGET_STACK_FUNCS(block_group_flags,
968 struct btrfs_block_group_item, flags, 64);
970 /* struct btrfs_inode_ref */
971 BTRFS_SETGET_FUNCS(inode_ref_name_len, struct btrfs_inode_ref, name_len, 16);
972 BTRFS_SETGET_FUNCS(inode_ref_index, struct btrfs_inode_ref, index, 64);
974 /* struct btrfs_inode_item */
975 BTRFS_SETGET_FUNCS(inode_generation, struct btrfs_inode_item, generation, 64);
976 BTRFS_SETGET_FUNCS(inode_sequence, struct btrfs_inode_item, sequence, 64);
977 BTRFS_SETGET_FUNCS(inode_transid, struct btrfs_inode_item, transid, 64);
978 BTRFS_SETGET_FUNCS(inode_size, struct btrfs_inode_item, size, 64);
979 BTRFS_SETGET_FUNCS(inode_nbytes, struct btrfs_inode_item, nbytes, 64);
980 BTRFS_SETGET_FUNCS(inode_block_group, struct btrfs_inode_item, block_group, 64);
981 BTRFS_SETGET_FUNCS(inode_nlink, struct btrfs_inode_item, nlink, 32);
982 BTRFS_SETGET_FUNCS(inode_uid, struct btrfs_inode_item, uid, 32);
983 BTRFS_SETGET_FUNCS(inode_gid, struct btrfs_inode_item, gid, 32);
984 BTRFS_SETGET_FUNCS(inode_mode, struct btrfs_inode_item, mode, 32);
985 BTRFS_SETGET_FUNCS(inode_rdev, struct btrfs_inode_item, rdev, 64);
986 BTRFS_SETGET_FUNCS(inode_flags, struct btrfs_inode_item, flags, 64);
988 BTRFS_SETGET_STACK_FUNCS(stack_inode_generation,
989 struct btrfs_inode_item, generation, 64);
990 BTRFS_SETGET_STACK_FUNCS(stack_inode_sequence,
991 struct btrfs_inode_item, generation, 64);
992 BTRFS_SETGET_STACK_FUNCS(stack_inode_size,
993 struct btrfs_inode_item, size, 64);
994 BTRFS_SETGET_STACK_FUNCS(stack_inode_nbytes,
995 struct btrfs_inode_item, nbytes, 64);
996 BTRFS_SETGET_STACK_FUNCS(stack_inode_block_group,
997 struct btrfs_inode_item, block_group, 64);
998 BTRFS_SETGET_STACK_FUNCS(stack_inode_nlink,
999 struct btrfs_inode_item, nlink, 32);
1000 BTRFS_SETGET_STACK_FUNCS(stack_inode_uid,
1001 struct btrfs_inode_item, uid, 32);
1002 BTRFS_SETGET_STACK_FUNCS(stack_inode_gid,
1003 struct btrfs_inode_item, gid, 32);
1004 BTRFS_SETGET_STACK_FUNCS(stack_inode_mode,
1005 struct btrfs_inode_item, mode, 32);
1006 BTRFS_SETGET_STACK_FUNCS(stack_inode_rdev,
1007 struct btrfs_inode_item, rdev, 64);
1008 BTRFS_SETGET_STACK_FUNCS(stack_inode_flags,
1009 struct btrfs_inode_item, flags, 64);
1011 static inline struct btrfs_timespec *
1012 btrfs_inode_atime(struct btrfs_inode_item *inode_item)
1014 unsigned long ptr = (unsigned long)inode_item;
1015 ptr += offsetof(struct btrfs_inode_item, atime);
1016 return (struct btrfs_timespec *)ptr;
1019 static inline struct btrfs_timespec *
1020 btrfs_inode_mtime(struct btrfs_inode_item *inode_item)
1022 unsigned long ptr = (unsigned long)inode_item;
1023 ptr += offsetof(struct btrfs_inode_item, mtime);
1024 return (struct btrfs_timespec *)ptr;
1027 static inline struct btrfs_timespec *
1028 btrfs_inode_ctime(struct btrfs_inode_item *inode_item)
1030 unsigned long ptr = (unsigned long)inode_item;
1031 ptr += offsetof(struct btrfs_inode_item, ctime);
1032 return (struct btrfs_timespec *)ptr;
1035 static inline struct btrfs_timespec *
1036 btrfs_inode_otime(struct btrfs_inode_item *inode_item)
1038 unsigned long ptr = (unsigned long)inode_item;
1039 ptr += offsetof(struct btrfs_inode_item, otime);
1040 return (struct btrfs_timespec *)ptr;
1043 BTRFS_SETGET_FUNCS(timespec_sec, struct btrfs_timespec, sec, 64);
1044 BTRFS_SETGET_FUNCS(timespec_nsec, struct btrfs_timespec, nsec, 32);
1045 BTRFS_SETGET_STACK_FUNCS(stack_timespec_sec, struct btrfs_timespec,
1047 BTRFS_SETGET_STACK_FUNCS(stack_timespec_nsec, struct btrfs_timespec,
1050 /* struct btrfs_dev_extent */
1051 BTRFS_SETGET_FUNCS(dev_extent_chunk_tree, struct btrfs_dev_extent,
1053 BTRFS_SETGET_FUNCS(dev_extent_chunk_objectid, struct btrfs_dev_extent,
1054 chunk_objectid, 64);
1055 BTRFS_SETGET_FUNCS(dev_extent_chunk_offset, struct btrfs_dev_extent,
1057 BTRFS_SETGET_FUNCS(dev_extent_length, struct btrfs_dev_extent, length, 64);
1059 static inline u8 *btrfs_dev_extent_chunk_tree_uuid(struct btrfs_dev_extent *dev)
1061 unsigned long ptr = offsetof(struct btrfs_dev_extent, chunk_tree_uuid);
1062 return (u8 *)((unsigned long)dev + ptr);
1065 /* struct btrfs_extent_ref */
1066 BTRFS_SETGET_FUNCS(ref_root, struct btrfs_extent_ref, root, 64);
1067 BTRFS_SETGET_FUNCS(ref_generation, struct btrfs_extent_ref, generation, 64);
1068 BTRFS_SETGET_FUNCS(ref_objectid, struct btrfs_extent_ref, objectid, 64);
1069 BTRFS_SETGET_FUNCS(ref_num_refs, struct btrfs_extent_ref, num_refs, 32);
1071 BTRFS_SETGET_STACK_FUNCS(stack_ref_root, struct btrfs_extent_ref, root, 64);
1072 BTRFS_SETGET_STACK_FUNCS(stack_ref_generation, struct btrfs_extent_ref,
1074 BTRFS_SETGET_STACK_FUNCS(stack_ref_objectid, struct btrfs_extent_ref,
1076 BTRFS_SETGET_STACK_FUNCS(stack_ref_num_refs, struct btrfs_extent_ref,
1079 /* struct btrfs_extent_item */
1080 BTRFS_SETGET_FUNCS(extent_refs, struct btrfs_extent_item, refs, 32);
1081 BTRFS_SETGET_STACK_FUNCS(stack_extent_refs, struct btrfs_extent_item,
1084 /* struct btrfs_node */
1085 BTRFS_SETGET_FUNCS(key_blockptr, struct btrfs_key_ptr, blockptr, 64);
1086 BTRFS_SETGET_FUNCS(key_generation, struct btrfs_key_ptr, generation, 64);
1088 static inline u64 btrfs_node_blockptr(struct extent_buffer *eb, int nr)
1091 ptr = offsetof(struct btrfs_node, ptrs) +
1092 sizeof(struct btrfs_key_ptr) * nr;
1093 return btrfs_key_blockptr(eb, (struct btrfs_key_ptr *)ptr);
1096 static inline void btrfs_set_node_blockptr(struct extent_buffer *eb,
1100 ptr = offsetof(struct btrfs_node, ptrs) +
1101 sizeof(struct btrfs_key_ptr) * nr;
1102 btrfs_set_key_blockptr(eb, (struct btrfs_key_ptr *)ptr, val);
1105 static inline u64 btrfs_node_ptr_generation(struct extent_buffer *eb, int nr)
1108 ptr = offsetof(struct btrfs_node, ptrs) +
1109 sizeof(struct btrfs_key_ptr) * nr;
1110 return btrfs_key_generation(eb, (struct btrfs_key_ptr *)ptr);
1113 static inline void btrfs_set_node_ptr_generation(struct extent_buffer *eb,
1117 ptr = offsetof(struct btrfs_node, ptrs) +
1118 sizeof(struct btrfs_key_ptr) * nr;
1119 btrfs_set_key_generation(eb, (struct btrfs_key_ptr *)ptr, val);
1122 static inline unsigned long btrfs_node_key_ptr_offset(int nr)
1124 return offsetof(struct btrfs_node, ptrs) +
1125 sizeof(struct btrfs_key_ptr) * nr;
1128 static inline void btrfs_node_key(struct extent_buffer *eb,
1129 struct btrfs_disk_key *disk_key, int nr)
1132 ptr = btrfs_node_key_ptr_offset(nr);
1133 read_eb_member(eb, (struct btrfs_key_ptr *)ptr,
1134 struct btrfs_key_ptr, key, disk_key);
1137 static inline void btrfs_set_node_key(struct extent_buffer *eb,
1138 struct btrfs_disk_key *disk_key, int nr)
1141 ptr = btrfs_node_key_ptr_offset(nr);
1142 write_eb_member(eb, (struct btrfs_key_ptr *)ptr,
1143 struct btrfs_key_ptr, key, disk_key);
1146 /* struct btrfs_item */
1147 BTRFS_SETGET_FUNCS(item_offset, struct btrfs_item, offset, 32);
1148 BTRFS_SETGET_FUNCS(item_size, struct btrfs_item, size, 32);
1150 static inline unsigned long btrfs_item_nr_offset(int nr)
1152 return offsetof(struct btrfs_leaf, items) +
1153 sizeof(struct btrfs_item) * nr;
1156 static inline struct btrfs_item *btrfs_item_nr(struct extent_buffer *eb,
1159 return (struct btrfs_item *)btrfs_item_nr_offset(nr);
1162 static inline u32 btrfs_item_end(struct extent_buffer *eb,
1163 struct btrfs_item *item)
1165 return btrfs_item_offset(eb, item) + btrfs_item_size(eb, item);
1168 static inline u32 btrfs_item_end_nr(struct extent_buffer *eb, int nr)
1170 return btrfs_item_end(eb, btrfs_item_nr(eb, nr));
1173 static inline u32 btrfs_item_offset_nr(struct extent_buffer *eb, int nr)
1175 return btrfs_item_offset(eb, btrfs_item_nr(eb, nr));
1178 static inline u32 btrfs_item_size_nr(struct extent_buffer *eb, int nr)
1180 return btrfs_item_size(eb, btrfs_item_nr(eb, nr));
1183 static inline void btrfs_item_key(struct extent_buffer *eb,
1184 struct btrfs_disk_key *disk_key, int nr)
1186 struct btrfs_item *item = btrfs_item_nr(eb, nr);
1187 read_eb_member(eb, item, struct btrfs_item, key, disk_key);
1190 static inline void btrfs_set_item_key(struct extent_buffer *eb,
1191 struct btrfs_disk_key *disk_key, int nr)
1193 struct btrfs_item *item = btrfs_item_nr(eb, nr);
1194 write_eb_member(eb, item, struct btrfs_item, key, disk_key);
1198 * struct btrfs_root_ref
1200 BTRFS_SETGET_FUNCS(root_ref_dirid, struct btrfs_root_ref, dirid, 64);
1201 BTRFS_SETGET_FUNCS(root_ref_sequence, struct btrfs_root_ref, sequence, 64);
1202 BTRFS_SETGET_FUNCS(root_ref_name_len, struct btrfs_root_ref, name_len, 16);
1204 /* struct btrfs_dir_item */
1205 BTRFS_SETGET_FUNCS(dir_data_len, struct btrfs_dir_item, data_len, 16);
1206 BTRFS_SETGET_FUNCS(dir_type, struct btrfs_dir_item, type, 8);
1207 BTRFS_SETGET_FUNCS(dir_name_len, struct btrfs_dir_item, name_len, 16);
1208 BTRFS_SETGET_FUNCS(dir_transid, struct btrfs_dir_item, transid, 64);
1210 static inline void btrfs_dir_item_key(struct extent_buffer *eb,
1211 struct btrfs_dir_item *item,
1212 struct btrfs_disk_key *key)
1214 read_eb_member(eb, item, struct btrfs_dir_item, location, key);
1217 static inline void btrfs_set_dir_item_key(struct extent_buffer *eb,
1218 struct btrfs_dir_item *item,
1219 struct btrfs_disk_key *key)
1221 write_eb_member(eb, item, struct btrfs_dir_item, location, key);
1224 /* struct btrfs_disk_key */
1225 BTRFS_SETGET_STACK_FUNCS(disk_key_objectid, struct btrfs_disk_key,
1227 BTRFS_SETGET_STACK_FUNCS(disk_key_offset, struct btrfs_disk_key, offset, 64);
1228 BTRFS_SETGET_STACK_FUNCS(disk_key_type, struct btrfs_disk_key, type, 8);
1230 static inline void btrfs_disk_key_to_cpu(struct btrfs_key *cpu,
1231 struct btrfs_disk_key *disk)
1233 cpu->offset = le64_to_cpu(disk->offset);
1234 cpu->type = disk->type;
1235 cpu->objectid = le64_to_cpu(disk->objectid);
1238 static inline void btrfs_cpu_key_to_disk(struct btrfs_disk_key *disk,
1239 struct btrfs_key *cpu)
1241 disk->offset = cpu_to_le64(cpu->offset);
1242 disk->type = cpu->type;
1243 disk->objectid = cpu_to_le64(cpu->objectid);
1246 static inline void btrfs_node_key_to_cpu(struct extent_buffer *eb,
1247 struct btrfs_key *key, int nr)
1249 struct btrfs_disk_key disk_key;
1250 btrfs_node_key(eb, &disk_key, nr);
1251 btrfs_disk_key_to_cpu(key, &disk_key);
1254 static inline void btrfs_item_key_to_cpu(struct extent_buffer *eb,
1255 struct btrfs_key *key, int nr)
1257 struct btrfs_disk_key disk_key;
1258 btrfs_item_key(eb, &disk_key, nr);
1259 btrfs_disk_key_to_cpu(key, &disk_key);
1262 static inline void btrfs_dir_item_key_to_cpu(struct extent_buffer *eb,
1263 struct btrfs_dir_item *item,
1264 struct btrfs_key *key)
1266 struct btrfs_disk_key disk_key;
1267 btrfs_dir_item_key(eb, item, &disk_key);
1268 btrfs_disk_key_to_cpu(key, &disk_key);
1272 static inline u8 btrfs_key_type(struct btrfs_key *key)
1277 static inline void btrfs_set_key_type(struct btrfs_key *key, u8 val)
1282 /* struct btrfs_header */
1283 BTRFS_SETGET_HEADER_FUNCS(header_bytenr, struct btrfs_header, bytenr, 64);
1284 BTRFS_SETGET_HEADER_FUNCS(header_generation, struct btrfs_header,
1286 BTRFS_SETGET_HEADER_FUNCS(header_owner, struct btrfs_header, owner, 64);
1287 BTRFS_SETGET_HEADER_FUNCS(header_nritems, struct btrfs_header, nritems, 32);
1288 BTRFS_SETGET_HEADER_FUNCS(header_flags, struct btrfs_header, flags, 64);
1289 BTRFS_SETGET_HEADER_FUNCS(header_level, struct btrfs_header, level, 8);
1291 static inline int btrfs_header_flag(struct extent_buffer *eb, u64 flag)
1293 return (btrfs_header_flags(eb) & flag) == flag;
1296 static inline int btrfs_set_header_flag(struct extent_buffer *eb, u64 flag)
1298 u64 flags = btrfs_header_flags(eb);
1299 btrfs_set_header_flags(eb, flags | flag);
1300 return (flags & flag) == flag;
1303 static inline int btrfs_clear_header_flag(struct extent_buffer *eb, u64 flag)
1305 u64 flags = btrfs_header_flags(eb);
1306 btrfs_set_header_flags(eb, flags & ~flag);
1307 return (flags & flag) == flag;
1310 static inline u8 *btrfs_header_fsid(struct extent_buffer *eb)
1312 unsigned long ptr = offsetof(struct btrfs_header, fsid);
1316 static inline u8 *btrfs_header_chunk_tree_uuid(struct extent_buffer *eb)
1318 unsigned long ptr = offsetof(struct btrfs_header, chunk_tree_uuid);
1322 static inline u8 *btrfs_super_fsid(struct extent_buffer *eb)
1324 unsigned long ptr = offsetof(struct btrfs_super_block, fsid);
1328 static inline u8 *btrfs_header_csum(struct extent_buffer *eb)
1330 unsigned long ptr = offsetof(struct btrfs_header, csum);
1334 static inline struct btrfs_node *btrfs_buffer_node(struct extent_buffer *eb)
1339 static inline struct btrfs_leaf *btrfs_buffer_leaf(struct extent_buffer *eb)
1344 static inline struct btrfs_header *btrfs_buffer_header(struct extent_buffer *eb)
1349 static inline int btrfs_is_leaf(struct extent_buffer *eb)
1351 return (btrfs_header_level(eb) == 0);
1354 /* struct btrfs_root_item */
1355 BTRFS_SETGET_FUNCS(disk_root_generation, struct btrfs_root_item,
1357 BTRFS_SETGET_FUNCS(disk_root_refs, struct btrfs_root_item, refs, 32);
1358 BTRFS_SETGET_FUNCS(disk_root_bytenr, struct btrfs_root_item, bytenr, 64);
1359 BTRFS_SETGET_FUNCS(disk_root_level, struct btrfs_root_item, level, 8);
1361 BTRFS_SETGET_STACK_FUNCS(root_generation, struct btrfs_root_item,
1363 BTRFS_SETGET_STACK_FUNCS(root_bytenr, struct btrfs_root_item, bytenr, 64);
1364 BTRFS_SETGET_STACK_FUNCS(root_level, struct btrfs_root_item, level, 8);
1365 BTRFS_SETGET_STACK_FUNCS(root_dirid, struct btrfs_root_item, root_dirid, 64);
1366 BTRFS_SETGET_STACK_FUNCS(root_refs, struct btrfs_root_item, refs, 32);
1367 BTRFS_SETGET_STACK_FUNCS(root_flags, struct btrfs_root_item, flags, 64);
1368 BTRFS_SETGET_STACK_FUNCS(root_used, struct btrfs_root_item, bytes_used, 64);
1369 BTRFS_SETGET_STACK_FUNCS(root_limit, struct btrfs_root_item, byte_limit, 64);
1370 BTRFS_SETGET_STACK_FUNCS(root_last_snapshot, struct btrfs_root_item,
1374 /* struct btrfs_super_block */
1376 BTRFS_SETGET_STACK_FUNCS(super_bytenr, struct btrfs_super_block, bytenr, 64);
1377 BTRFS_SETGET_STACK_FUNCS(super_flags, struct btrfs_super_block, flags, 64);
1378 BTRFS_SETGET_STACK_FUNCS(super_generation, struct btrfs_super_block,
1380 BTRFS_SETGET_STACK_FUNCS(super_root, struct btrfs_super_block, root, 64);
1381 BTRFS_SETGET_STACK_FUNCS(super_sys_array_size,
1382 struct btrfs_super_block, sys_chunk_array_size, 32);
1383 BTRFS_SETGET_STACK_FUNCS(super_chunk_root_generation,
1384 struct btrfs_super_block, chunk_root_generation, 64);
1385 BTRFS_SETGET_STACK_FUNCS(super_root_level, struct btrfs_super_block,
1387 BTRFS_SETGET_STACK_FUNCS(super_chunk_root, struct btrfs_super_block,
1389 BTRFS_SETGET_STACK_FUNCS(super_chunk_root_level, struct btrfs_super_block,
1390 chunk_root_level, 8);
1391 BTRFS_SETGET_STACK_FUNCS(super_log_root, struct btrfs_super_block,
1393 BTRFS_SETGET_STACK_FUNCS(super_log_root_transid, struct btrfs_super_block,
1394 log_root_transid, 64);
1395 BTRFS_SETGET_STACK_FUNCS(super_log_root_level, struct btrfs_super_block,
1397 BTRFS_SETGET_STACK_FUNCS(super_total_bytes, struct btrfs_super_block,
1399 BTRFS_SETGET_STACK_FUNCS(super_bytes_used, struct btrfs_super_block,
1401 BTRFS_SETGET_STACK_FUNCS(super_sectorsize, struct btrfs_super_block,
1403 BTRFS_SETGET_STACK_FUNCS(super_nodesize, struct btrfs_super_block,
1405 BTRFS_SETGET_STACK_FUNCS(super_leafsize, struct btrfs_super_block,
1407 BTRFS_SETGET_STACK_FUNCS(super_stripesize, struct btrfs_super_block,
1409 BTRFS_SETGET_STACK_FUNCS(super_root_dir, struct btrfs_super_block,
1410 root_dir_objectid, 64);
1411 BTRFS_SETGET_STACK_FUNCS(super_num_devices, struct btrfs_super_block,
1413 BTRFS_SETGET_STACK_FUNCS(super_compat_flags, struct btrfs_super_block,
1415 BTRFS_SETGET_STACK_FUNCS(super_compat_ro_flags, struct btrfs_super_block,
1417 BTRFS_SETGET_STACK_FUNCS(super_incompat_flags, struct btrfs_super_block,
1418 incompat_flags, 64);
1419 BTRFS_SETGET_STACK_FUNCS(super_csum_type, struct btrfs_super_block,
1422 static inline int btrfs_super_csum_size(struct btrfs_super_block *s)
1424 int t = btrfs_super_csum_type(s);
1425 BUG_ON(t >= ARRAY_SIZE(btrfs_csum_sizes));
1426 return btrfs_csum_sizes[t];
1429 static inline unsigned long btrfs_leaf_data(struct extent_buffer *l)
1431 return offsetof(struct btrfs_leaf, items);
1434 /* struct btrfs_file_extent_item */
1435 BTRFS_SETGET_FUNCS(file_extent_type, struct btrfs_file_extent_item, type, 8);
1437 static inline unsigned long btrfs_file_extent_inline_start(struct
1438 btrfs_file_extent_item *e)
1440 unsigned long offset = (unsigned long)e;
1441 offset += offsetof(struct btrfs_file_extent_item, disk_bytenr);
1445 static inline u32 btrfs_file_extent_calc_inline_size(u32 datasize)
1447 return offsetof(struct btrfs_file_extent_item, disk_bytenr) + datasize;
1450 BTRFS_SETGET_FUNCS(file_extent_disk_bytenr, struct btrfs_file_extent_item,
1452 BTRFS_SETGET_FUNCS(file_extent_generation, struct btrfs_file_extent_item,
1454 BTRFS_SETGET_FUNCS(file_extent_disk_num_bytes, struct btrfs_file_extent_item,
1455 disk_num_bytes, 64);
1456 BTRFS_SETGET_FUNCS(file_extent_offset, struct btrfs_file_extent_item,
1458 BTRFS_SETGET_FUNCS(file_extent_num_bytes, struct btrfs_file_extent_item,
1460 BTRFS_SETGET_FUNCS(file_extent_ram_bytes, struct btrfs_file_extent_item,
1462 BTRFS_SETGET_FUNCS(file_extent_compression, struct btrfs_file_extent_item,
1464 BTRFS_SETGET_FUNCS(file_extent_encryption, struct btrfs_file_extent_item,
1466 BTRFS_SETGET_FUNCS(file_extent_other_encoding, struct btrfs_file_extent_item,
1467 other_encoding, 16);
1469 /* this returns the number of file bytes represented by the inline item.
1470 * If an item is compressed, this is the uncompressed size
1472 static inline u32 btrfs_file_extent_inline_len(struct extent_buffer *eb,
1473 struct btrfs_file_extent_item *e)
1475 return btrfs_file_extent_ram_bytes(eb, e);
1479 * this returns the number of bytes used by the item on disk, minus the
1480 * size of any extent headers. If a file is compressed on disk, this is
1481 * the compressed size
1483 static inline u32 btrfs_file_extent_inline_item_len(struct extent_buffer *eb,
1484 struct btrfs_item *e)
1486 unsigned long offset;
1487 offset = offsetof(struct btrfs_file_extent_item, disk_bytenr);
1488 return btrfs_item_size(eb, e) - offset;
1491 static inline u32 btrfs_level_size(struct btrfs_root *root, int level) {
1493 return root->leafsize;
1494 return root->nodesize;
1497 /* helper function to cast into the data area of the leaf. */
1498 #define btrfs_item_ptr(leaf, slot, type) \
1499 ((type *)(btrfs_leaf_data(leaf) + \
1500 btrfs_item_offset_nr(leaf, slot)))
1502 #define btrfs_item_ptr_offset(leaf, slot) \
1503 ((unsigned long)(btrfs_leaf_data(leaf) + \
1504 btrfs_item_offset_nr(leaf, slot)))
1507 int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
1508 struct btrfs_root *root);
1509 int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy);
1510 struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
1511 btrfs_fs_info *info,
1513 struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
1514 struct btrfs_block_group_cache
1515 *hint, u64 search_start,
1516 int data, int owner);
1517 struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
1518 struct btrfs_root *root,
1519 u32 blocksize, u64 parent,
1525 int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
1526 struct btrfs_root *root,
1527 u64 num_bytes, u64 parent,
1528 u64 root_objectid, u64 ref_generation,
1529 u64 owner, u64 empty_size, u64 hint_byte,
1530 u64 search_end, struct btrfs_key *ins, int data);
1531 int btrfs_lookup_extent_ref(struct btrfs_trans_handle *trans,
1532 struct btrfs_root *root, u64 bytenr,
1533 u64 num_bytes, u32 *refs);
1534 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1535 struct extent_buffer *orig_buf, struct extent_buffer *buf,
1537 int btrfs_update_ref(struct btrfs_trans_handle *trans,
1538 struct btrfs_root *root, struct extent_buffer *orig_buf,
1539 struct extent_buffer *buf, int start_slot, int nr);
1540 int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
1541 *root, u64 bytenr, u64 num_bytes, u64 parent,
1542 u64 root_objectid, u64 ref_generation,
1543 u64 owner_objectid, int pin);
1544 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
1545 struct btrfs_root *root,
1546 struct extent_io_tree *unpin);
1547 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1548 struct btrfs_root *root,
1549 u64 bytenr, u64 num_bytes, u64 parent,
1550 u64 root_objectid, u64 ref_generation,
1551 u64 owner_objectid);
1552 int btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
1553 struct btrfs_root *root, u64 bytenr,
1554 u64 orig_parent, u64 parent,
1555 u64 root_objectid, u64 ref_generation,
1556 u64 owner_objectid);
1557 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1558 struct btrfs_root *root);
1559 int btrfs_free_block_groups(struct btrfs_fs_info *info);
1560 int btrfs_read_block_groups(struct btrfs_root *root);
1561 int btrfs_make_block_group(struct btrfs_trans_handle *trans,
1562 struct btrfs_root *root, u64 bytes_used,
1563 u64 type, u64 chunk_objectid, u64 chunk_offset,
1565 int btrfs_make_block_groups(struct btrfs_trans_handle *trans,
1566 struct btrfs_root *root);
1567 int btrfs_update_block_group(struct btrfs_trans_handle *trans,
1568 struct btrfs_root *root, u64 bytenr, u64 num,
1569 int alloc, int mark_free);
1571 int btrfs_previous_item(struct btrfs_root *root,
1572 struct btrfs_path *path, u64 min_objectid,
1574 int btrfs_comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2);
1575 int btrfs_cow_block(struct btrfs_trans_handle *trans,
1576 struct btrfs_root *root, struct extent_buffer *buf,
1577 struct extent_buffer *parent, int parent_slot,
1578 struct extent_buffer **cow_ret);
1579 int __btrfs_cow_block(struct btrfs_trans_handle *trans,
1580 struct btrfs_root *root,
1581 struct extent_buffer *buf,
1582 struct extent_buffer *parent, int parent_slot,
1583 struct extent_buffer **cow_ret,
1584 u64 search_start, u64 empty_size);
1585 int btrfs_copy_root(struct btrfs_trans_handle *trans,
1586 struct btrfs_root *root,
1587 struct extent_buffer *buf,
1588 struct extent_buffer **cow_ret, u64 new_root_objectid);
1589 int btrfs_extend_item(struct btrfs_trans_handle *trans, struct btrfs_root
1590 *root, struct btrfs_path *path, u32 data_size);
1591 int btrfs_truncate_item(struct btrfs_trans_handle *trans,
1592 struct btrfs_root *root,
1593 struct btrfs_path *path,
1594 u32 new_size, int from_end);
1595 int btrfs_split_item(struct btrfs_trans_handle *trans,
1596 struct btrfs_root *root,
1597 struct btrfs_path *path,
1598 struct btrfs_key *new_key,
1599 unsigned long split_offset);
1600 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1601 *root, struct btrfs_key *key, struct btrfs_path *p, int
1603 int btrfs_realloc_node(struct btrfs_trans_handle *trans,
1604 struct btrfs_root *root, struct extent_buffer *parent,
1605 int start_slot, int cache_only, u64 *last_ret,
1606 struct btrfs_key *progress);
1607 void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p);
1608 struct btrfs_path *btrfs_alloc_path(void);
1609 void btrfs_free_path(struct btrfs_path *p);
1610 void btrfs_init_path(struct btrfs_path *p);
1611 int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1612 struct btrfs_path *path, int slot, int nr);
1614 static inline int btrfs_del_item(struct btrfs_trans_handle *trans,
1615 struct btrfs_root *root,
1616 struct btrfs_path *path)
1618 return btrfs_del_items(trans, root, path, path->slots[0], 1);
1621 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
1622 *root, struct btrfs_key *key, void *data, u32 data_size);
1623 int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
1624 struct btrfs_root *root,
1625 struct btrfs_path *path,
1626 struct btrfs_key *cpu_key, u32 *data_size, int nr);
1628 static inline int btrfs_insert_empty_item(struct btrfs_trans_handle *trans,
1629 struct btrfs_root *root,
1630 struct btrfs_path *path,
1631 struct btrfs_key *key,
1634 return btrfs_insert_empty_items(trans, root, path, key, &data_size, 1);
1637 int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path);
1638 int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path);
1639 int btrfs_leaf_free_space(struct btrfs_root *root, struct extent_buffer *leaf);
1640 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
1642 int btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
1643 struct btrfs_root *root, struct btrfs_path *path,
1644 struct btrfs_key *new_key);
1647 int btrfs_add_root_ref(struct btrfs_trans_handle *trans,
1648 struct btrfs_root *tree_root,
1649 u64 root_id, u8 type, u64 ref_id,
1650 u64 dirid, u64 sequence,
1651 const char *name, int name_len);
1652 int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1653 struct btrfs_key *key);
1654 int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
1655 *root, struct btrfs_key *key, struct btrfs_root_item
1657 int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
1658 *root, struct btrfs_key *key, struct btrfs_root_item
1660 int btrfs_find_last_root(struct btrfs_root *root, u64 objectid, struct
1661 btrfs_root_item *item, struct btrfs_key *key);
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,
1696 int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
1697 struct btrfs_root *fs_root,
1698 u64 dirid, u64 *objectid);
1699 int btrfs_find_highest_inode(struct btrfs_root *fs_root, u64 *objectid);
1702 int btrfs_insert_inode_ref(struct btrfs_trans_handle *trans,
1703 struct btrfs_root *root,
1704 const char *name, int name_len,
1705 u64 inode_objectid, u64 ref_objectid, u64 index);
1706 int btrfs_del_inode_ref(struct btrfs_trans_handle *trans,
1707 struct btrfs_root *root,
1708 const char *name, int name_len,
1709 u64 inode_objectid, u64 ref_objectid);
1710 int btrfs_insert_empty_inode(struct btrfs_trans_handle *trans,
1711 struct btrfs_root *root,
1712 struct btrfs_path *path, u64 objectid);
1713 int btrfs_insert_inode(struct btrfs_trans_handle *trans, struct btrfs_root
1714 *root, u64 objectid, struct btrfs_inode_item
1716 int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root
1717 *root, struct btrfs_path *path,
1718 struct btrfs_key *location, int mod);
1721 int btrfs_del_csums(struct btrfs_trans_handle *trans,
1722 struct btrfs_root *root, u64 bytenr, u64 len);
1723 int btrfs_insert_file_extent(struct btrfs_trans_handle *trans,
1724 struct btrfs_root *root,
1725 u64 objectid, u64 pos, u64 offset,
1728 int btrfs_insert_inline_extent(struct btrfs_trans_handle *trans,
1729 struct btrfs_root *root, u64 objectid,
1730 u64 offset, char *buffer, size_t size);
1731 int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
1732 struct btrfs_root *root,
1733 struct btrfs_path *path, u64 objectid,
1734 u64 bytenr, int mod);
1735 int btrfs_csum_file_block(struct btrfs_trans_handle *trans,
1736 struct btrfs_root *root, u64 alloc_end,
1737 u64 bytenr, char *data, size_t len);
1738 struct btrfs_csum_item *btrfs_lookup_csum(struct btrfs_trans_handle *trans,
1739 struct btrfs_root *root,
1740 struct btrfs_path *path,
1741 u64 bytenr, int cow);
1742 int btrfs_csum_truncate(struct btrfs_trans_handle *trans,
1743 struct btrfs_root *root, struct btrfs_path *path,