btrfs-progs new dir index support
[btrfs-progs-unstable/devel.git] / ctree.h
blobd6467f6f2a561112eb3b26572626761869e64969
1 /*
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__
20 #define __BTRFS__
22 #include "list.h"
23 #include "kerncompat.h"
24 #include "radix-tree.h"
25 #include "extent-cache.h"
26 #include "extent_io.h"
28 struct btrfs_root;
29 struct btrfs_trans_handle;
30 #define BTRFS_MAGIC "_B5RfS_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
59 * All files have objectids higher than this.
61 #define BTRFS_FIRST_FREE_OBJECTID 256ULL
62 #define BTRFS_FIRST_CHUNK_TREE_OBJECTID 256ULL
66 * the device items go into the chunk tree. The key is in the form
67 * [ 1 BTRFS_DEV_ITEM_KEY device_id ]
69 #define BTRFS_DEV_ITEMS_OBJECTID 1ULL
72 * we can actually store much bigger names, but lets not confuse the rest
73 * of linux
75 #define BTRFS_NAME_LEN 255
77 /* 32 bytes in various csum fields */
78 #define BTRFS_CSUM_SIZE 32
79 /* four bytes for CRC32 */
80 #define BTRFS_CRC32_SIZE 4
81 #define BTRFS_EMPTY_DIR_SIZE 0
83 #define BTRFS_FT_UNKNOWN 0
84 #define BTRFS_FT_REG_FILE 1
85 #define BTRFS_FT_DIR 2
86 #define BTRFS_FT_CHRDEV 3
87 #define BTRFS_FT_BLKDEV 4
88 #define BTRFS_FT_FIFO 5
89 #define BTRFS_FT_SOCK 6
90 #define BTRFS_FT_SYMLINK 7
91 #define BTRFS_FT_XATTR 8
92 #define BTRFS_FT_MAX 9
95 * the key defines the order in the tree, and so it also defines (optimal)
96 * block layout. objectid corresonds to the inode number. The flags
97 * tells us things about the object, and is a kind of stream selector.
98 * so for a given inode, keys with flags of 1 might refer to the inode
99 * data, flags of 2 may point to file data in the btree and flags == 3
100 * may point to extents.
102 * offset is the starting byte offset for this key in the stream.
104 * btrfs_disk_key is in disk byte order. struct btrfs_key is always
105 * in cpu native order. Otherwise they are identical and their sizes
106 * should be the same (ie both packed)
108 struct btrfs_disk_key {
109 __le64 objectid;
110 u8 type;
111 __le64 offset;
112 } __attribute__ ((__packed__));
114 struct btrfs_key {
115 u64 objectid;
116 u8 type;
117 u64 offset;
118 } __attribute__ ((__packed__));
120 struct btrfs_mapping_tree {
121 struct cache_tree cache_tree;
124 #define BTRFS_UUID_SIZE 16
125 struct btrfs_dev_item {
126 /* the internal btrfs device id */
127 __le64 devid;
129 /* size of the device */
130 __le64 total_bytes;
132 /* bytes used */
133 __le64 bytes_used;
135 /* optimal io alignment for this device */
136 __le32 io_align;
138 /* optimal io width for this device */
139 __le32 io_width;
141 /* minimal io size for this device */
142 __le32 sector_size;
144 /* type and info about this device */
145 __le64 type;
147 /* grouping information for allocation decisions */
148 __le32 dev_group;
150 /* seek speed 0-100 where 100 is fastest */
151 u8 seek_speed;
153 /* bandwidth 0-100 where 100 is fastest */
154 u8 bandwidth;
156 /* btrfs generated uuid for this device */
157 u8 uuid[BTRFS_UUID_SIZE];
158 } __attribute__ ((__packed__));
160 struct btrfs_stripe {
161 __le64 devid;
162 __le64 offset;
163 u8 dev_uuid[BTRFS_UUID_SIZE];
164 } __attribute__ ((__packed__));
166 struct btrfs_chunk {
167 /* size of this chunk in bytes */
168 __le64 length;
170 /* objectid of the root referencing this chunk */
171 __le64 owner;
173 __le64 stripe_len;
174 __le64 type;
176 /* optimal io alignment for this chunk */
177 __le32 io_align;
179 /* optimal io width for this chunk */
180 __le32 io_width;
182 /* minimal io size for this chunk */
183 __le32 sector_size;
185 /* 2^16 stripes is quite a lot, a second limit is the size of a single
186 * item in the btree
188 __le16 num_stripes;
190 /* sub stripes only matter for raid10 */
191 __le16 sub_stripes;
192 struct btrfs_stripe stripe;
193 /* additional stripes go here */
194 } __attribute__ ((__packed__));
196 static inline unsigned long btrfs_chunk_item_size(int num_stripes)
198 BUG_ON(num_stripes == 0);
199 return sizeof(struct btrfs_chunk) +
200 sizeof(struct btrfs_stripe) * (num_stripes - 1);
203 #define BTRFS_FSID_SIZE 16
204 #define BTRFS_HEADER_FLAG_WRITTEN (1 << 0)
207 * every tree block (leaf or node) starts with this header.
209 struct btrfs_header {
210 /* these first four must match the super block */
211 u8 csum[BTRFS_CSUM_SIZE];
212 u8 fsid[BTRFS_FSID_SIZE]; /* FS specific uuid */
213 __le64 bytenr; /* which block this node is supposed to live in */
214 __le64 flags;
216 /* allowed to be different from the super from here on down */
217 u8 chunk_tree_uuid[BTRFS_UUID_SIZE];
218 __le64 generation;
219 __le64 owner;
220 __le32 nritems;
221 u8 level;
222 } __attribute__ ((__packed__));
224 #define BTRFS_NODEPTRS_PER_BLOCK(r) (((r)->nodesize - \
225 sizeof(struct btrfs_header)) / \
226 sizeof(struct btrfs_key_ptr))
227 #define __BTRFS_LEAF_DATA_SIZE(bs) ((bs) - sizeof(struct btrfs_header))
228 #define BTRFS_LEAF_DATA_SIZE(r) (__BTRFS_LEAF_DATA_SIZE(r->leafsize))
229 #define BTRFS_MAX_INLINE_DATA_SIZE(r) (BTRFS_LEAF_DATA_SIZE(r) - \
230 sizeof(struct btrfs_item) - \
231 sizeof(struct btrfs_file_extent_item))
234 * this is a very generous portion of the super block, giving us
235 * room to translate 14 chunks with 3 stripes each.
237 #define BTRFS_SYSTEM_CHUNK_ARRAY_SIZE 2048
238 #define BTRFS_LABEL_SIZE 256
241 * the super block basically lists the main trees of the FS
242 * it currently lacks any block count etc etc
244 struct btrfs_super_block {
245 u8 csum[BTRFS_CSUM_SIZE];
246 /* the first 3 fields must match struct btrfs_header */
247 u8 fsid[BTRFS_FSID_SIZE]; /* FS specific uuid */
248 __le64 bytenr; /* this block number */
249 __le64 flags;
251 /* allowed to be different from the btrfs_header from here own down */
252 __le64 magic;
253 __le64 generation;
254 __le64 root;
255 __le64 chunk_root;
256 __le64 total_bytes;
257 __le64 bytes_used;
258 __le64 root_dir_objectid;
259 __le64 num_devices;
260 __le32 sectorsize;
261 __le32 nodesize;
262 __le32 leafsize;
263 __le32 stripesize;
264 __le32 sys_chunk_array_size;
265 u8 root_level;
266 u8 chunk_root_level;
267 struct btrfs_dev_item dev_item;
268 char label[BTRFS_LABEL_SIZE];
269 u8 sys_chunk_array[BTRFS_SYSTEM_CHUNK_ARRAY_SIZE];
270 } __attribute__ ((__packed__));
273 * A leaf is full of items. offset and size tell us where to find
274 * the item in the leaf (relative to the start of the data area)
276 struct btrfs_item {
277 struct btrfs_disk_key key;
278 __le32 offset;
279 __le32 size;
280 } __attribute__ ((__packed__));
283 * leaves have an item area and a data area:
284 * [item0, item1....itemN] [free space] [dataN...data1, data0]
286 * The data is separate from the items to get the keys closer together
287 * during searches.
289 struct btrfs_leaf {
290 struct btrfs_header header;
291 struct btrfs_item items[];
292 } __attribute__ ((__packed__));
295 * all non-leaf blocks are nodes, they hold only keys and pointers to
296 * other blocks
298 struct btrfs_key_ptr {
299 struct btrfs_disk_key key;
300 __le64 blockptr;
301 __le64 generation;
302 } __attribute__ ((__packed__));
304 struct btrfs_node {
305 struct btrfs_header header;
306 struct btrfs_key_ptr ptrs[];
307 } __attribute__ ((__packed__));
310 * btrfs_paths remember the path taken from the root down to the leaf.
311 * level 0 is always the leaf, and nodes[1...BTRFS_MAX_LEVEL] will point
312 * to any other levels that are present.
314 * The slots array records the index of the item or block pointer
315 * used while walking the tree.
317 struct btrfs_path {
318 struct extent_buffer *nodes[BTRFS_MAX_LEVEL];
319 int slots[BTRFS_MAX_LEVEL];
320 int reada;
321 int lowest_level;
325 * items in the extent btree are used to record the objectid of the
326 * owner of the block and the number of references
328 struct btrfs_extent_item {
329 __le32 refs;
330 } __attribute__ ((__packed__));
332 struct btrfs_extent_ref {
333 __le64 root;
334 __le64 generation;
335 __le64 objectid;
336 __le64 offset;
337 } __attribute__ ((__packed__));
339 /* dev extents record free space on individual devices. The owner
340 * field points back to the chunk allocation mapping tree that allocated
341 * the extent. The chunk tree uuid field is a way to double check the owner
343 struct btrfs_dev_extent {
344 __le64 chunk_tree;
345 __le64 chunk_objectid;
346 __le64 chunk_offset;
347 __le64 length;
348 u8 chunk_tree_uuid[BTRFS_UUID_SIZE];
349 } __attribute__ ((__packed__));
351 struct btrfs_inode_ref {
352 __le64 index;
353 __le16 name_len;
354 /* name goes here */
355 } __attribute__ ((__packed__));
357 struct btrfs_timespec {
358 __le64 sec;
359 __le32 nsec;
360 } __attribute__ ((__packed__));
363 * there is no padding here on purpose. If you want to extent the inode,
364 * make a new item type
366 struct btrfs_inode_item {
367 __le64 generation;
368 __le64 size;
369 __le64 nblocks;
370 __le64 block_group;
371 __le32 nlink;
372 __le32 uid;
373 __le32 gid;
374 __le32 mode;
375 __le64 rdev;
376 __le16 flags;
377 __le16 compat_flags;
378 struct btrfs_timespec atime;
379 struct btrfs_timespec ctime;
380 struct btrfs_timespec mtime;
381 struct btrfs_timespec otime;
382 } __attribute__ ((__packed__));
384 struct btrfs_dir_item {
385 struct btrfs_disk_key location;
386 __le16 data_len;
387 __le16 name_len;
388 u8 type;
389 } __attribute__ ((__packed__));
391 struct btrfs_root_item {
392 struct btrfs_inode_item inode;
393 __le64 root_dirid;
394 __le64 bytenr;
395 __le64 byte_limit;
396 __le64 bytes_used;
397 __le32 flags;
398 __le32 refs;
399 struct btrfs_disk_key drop_progress;
400 u8 drop_level;
401 u8 level;
402 } __attribute__ ((__packed__));
404 #define BTRFS_FILE_EXTENT_REG 0
405 #define BTRFS_FILE_EXTENT_INLINE 1
407 struct btrfs_file_extent_item {
408 __le64 generation;
409 u8 type;
411 * disk space consumed by the extent, checksum blocks are included
412 * in these numbers
414 __le64 disk_bytenr;
415 __le64 disk_num_bytes;
417 * the logical offset in file blocks (no csums)
418 * this extent record is for. This allows a file extent to point
419 * into the middle of an existing extent on disk, sharing it
420 * between two snapshots (useful if some bytes in the middle of the
421 * extent have changed
423 __le64 offset;
425 * the logical number of file blocks (no csums included)
427 __le64 num_bytes;
428 } __attribute__ ((__packed__));
430 struct btrfs_csum_item {
431 u8 csum;
432 } __attribute__ ((__packed__));
434 /* tag for the radix tree of block groups in ram */
435 #define BTRFS_BLOCK_GROUP_DATA (1 << 0)
436 #define BTRFS_BLOCK_GROUP_SYSTEM (1 << 1)
437 #define BTRFS_BLOCK_GROUP_METADATA (1 << 2)
438 #define BTRFS_BLOCK_GROUP_RAID0 (1 << 3)
439 #define BTRFS_BLOCK_GROUP_RAID1 (1 << 4)
440 #define BTRFS_BLOCK_GROUP_DUP (1 << 5)
441 #define BTRFS_BLOCK_GROUP_RAID10 (1 << 6)
443 struct btrfs_block_group_item {
444 __le64 used;
445 __le64 chunk_objectid;
446 __le64 flags;
447 } __attribute__ ((__packed__));
449 struct btrfs_space_info {
450 u64 flags;
451 u64 total_bytes;
452 u64 bytes_used;
453 u64 bytes_pinned;
454 int full;
455 struct list_head list;
458 struct btrfs_block_group_cache {
459 struct cache_extent cache;
460 struct btrfs_key key;
461 struct btrfs_block_group_item item;
462 struct btrfs_space_info *space_info;
463 u64 pinned;
464 u64 flags;
465 int cached;
468 struct btrfs_extent_ops {
469 int (*alloc_extent)(struct btrfs_root *root, u64 num_bytes,
470 u64 hint_byte, struct btrfs_key *ins);
471 int (*free_extent)(struct btrfs_root *root, u64 bytenr,
472 u64 num_bytes);
475 struct btrfs_device;
476 struct btrfs_fs_devices;
477 struct btrfs_fs_info {
478 u8 fsid[BTRFS_FSID_SIZE];
479 u8 chunk_tree_uuid[BTRFS_UUID_SIZE];
480 struct btrfs_root *fs_root;
481 struct btrfs_root *extent_root;
482 struct btrfs_root *tree_root;
483 struct btrfs_root *chunk_root;
484 struct btrfs_root *dev_root;
486 struct extent_io_tree extent_cache;
487 struct extent_io_tree free_space_cache;
488 struct extent_io_tree block_group_cache;
489 struct extent_io_tree pinned_extents;
490 struct extent_io_tree pending_del;
491 struct extent_io_tree extent_ins;
493 /* logical->physical extent mapping */
494 struct btrfs_mapping_tree mapping_tree;
496 u64 generation;
497 u64 last_trans_committed;
499 u64 avail_data_alloc_bits;
500 u64 avail_metadata_alloc_bits;
501 u64 avail_system_alloc_bits;
502 u64 data_alloc_profile;
503 u64 metadata_alloc_profile;
504 u64 system_alloc_profile;
505 u64 alloc_start;
507 struct btrfs_trans_handle *running_transaction;
508 struct btrfs_super_block super_copy;
509 struct extent_buffer *sb_buffer;
510 struct mutex fs_mutex;
511 u64 total_pinned;
513 struct btrfs_extent_ops *extent_ops;
514 struct list_head dirty_cowonly_roots;
516 struct btrfs_fs_devices *fs_devices;
517 struct list_head space_info;
518 int system_allocs;
519 int readonly;
523 * in ram representation of the tree. extent_root is used for all allocations
524 * and for the extent tree extent_root root.
526 struct btrfs_root {
527 struct extent_buffer *node;
528 struct extent_buffer *commit_root;
529 struct btrfs_root_item root_item;
530 struct btrfs_key root_key;
531 struct btrfs_fs_info *fs_info;
532 u64 objectid;
533 u64 last_trans;
535 /* data allocations are done in sectorsize units */
536 u32 sectorsize;
538 /* node allocations are done in nodesize units */
539 u32 nodesize;
541 /* leaf allocations are done in leafsize units */
542 u32 leafsize;
544 /* leaf allocations are done in leafsize units */
545 u32 stripesize;
547 int ref_cows;
548 int track_dirty;
551 u32 type;
552 u64 highest_inode;
553 u64 last_inode_alloc;
555 /* the dirty list is only used by non-reference counted roots */
556 struct list_head dirty_list;
560 * inode items have the data typically returned from stat and store other
561 * info about object characteristics. There is one for every file and dir in
562 * the FS
564 #define BTRFS_INODE_ITEM_KEY 1
565 #define BTRFS_INODE_REF_KEY 2
566 #define BTRFS_XATTR_ITEM_KEY 8
568 /* reserve 3-15 close to the inode for later flexibility */
571 * dir items are the name -> inode pointers in a directory. There is one
572 * for every name in a directory.
574 #define BTRFS_DIR_ITEM_KEY 16
575 #define BTRFS_DIR_INDEX_KEY 17
577 * extent data is for file data
579 #define BTRFS_EXTENT_DATA_KEY 18
581 * csum items have the checksums for data in the extents
583 #define BTRFS_CSUM_ITEM_KEY 19
585 /* reserve 20-31 for other file stuff */
588 * root items point to tree roots. There are typically in the root
589 * tree used by the super block to find all the other trees
591 #define BTRFS_ROOT_ITEM_KEY 32
593 * extent items are in the extent map tree. These record which blocks
594 * are used, and how many references there are to each block
596 #define BTRFS_EXTENT_ITEM_KEY 33
597 #define BTRFS_EXTENT_REF_KEY 34
600 * block groups give us hints into the extent allocation trees. Which
601 * blocks are free etc etc
603 #define BTRFS_BLOCK_GROUP_ITEM_KEY 50
605 #define BTRFS_DEV_EXTENT_KEY 75
606 #define BTRFS_DEV_ITEM_KEY 76
607 #define BTRFS_CHUNK_ITEM_KEY 77
610 * string items are for debugging. They just store a short string of
611 * data in the FS
613 #define BTRFS_STRING_ITEM_KEY 253
615 * Inode flags
617 #define BTRFS_INODE_NODATASUM (1 << 0)
618 #define BTRFS_INODE_NODATACOW (1 << 1)
619 #define BTRFS_INODE_READONLY (1 << 2)
621 #define read_eb_member(eb, ptr, type, member, result) ( \
622 read_extent_buffer(eb, (char *)(result), \
623 ((unsigned long)(ptr)) + \
624 offsetof(type, member), \
625 sizeof(((type *)0)->member)))
627 #define write_eb_member(eb, ptr, type, member, result) ( \
628 write_extent_buffer(eb, (char *)(result), \
629 ((unsigned long)(ptr)) + \
630 offsetof(type, member), \
631 sizeof(((type *)0)->member)))
633 #define BTRFS_SETGET_HEADER_FUNCS(name, type, member, bits) \
634 static inline u##bits btrfs_##name(struct extent_buffer *eb) \
636 struct btrfs_header *h = (struct btrfs_header *)eb->data; \
637 return le##bits##_to_cpu(h->member); \
639 static inline void btrfs_set_##name(struct extent_buffer *eb, \
640 u##bits val) \
642 struct btrfs_header *h = (struct btrfs_header *)eb->data; \
643 h->member = cpu_to_le##bits(val); \
646 #define BTRFS_SETGET_FUNCS(name, type, member, bits) \
647 static inline u##bits btrfs_##name(struct extent_buffer *eb, \
648 type *s) \
650 unsigned long offset = (unsigned long)s; \
651 type *p = (type *) (eb->data + offset); \
652 return le##bits##_to_cpu(p->member); \
654 static inline void btrfs_set_##name(struct extent_buffer *eb, \
655 type *s, u##bits val) \
657 unsigned long offset = (unsigned long)s; \
658 type *p = (type *) (eb->data + offset); \
659 p->member = cpu_to_le##bits(val); \
662 #define BTRFS_SETGET_STACK_FUNCS(name, type, member, bits) \
663 static inline u##bits btrfs_##name(type *s) \
665 return le##bits##_to_cpu(s->member); \
667 static inline void btrfs_set_##name(type *s, u##bits val) \
669 s->member = cpu_to_le##bits(val); \
672 BTRFS_SETGET_FUNCS(device_type, struct btrfs_dev_item, type, 64);
673 BTRFS_SETGET_FUNCS(device_total_bytes, struct btrfs_dev_item, total_bytes, 64);
674 BTRFS_SETGET_FUNCS(device_bytes_used, struct btrfs_dev_item, bytes_used, 64);
675 BTRFS_SETGET_FUNCS(device_io_align, struct btrfs_dev_item, io_align, 32);
676 BTRFS_SETGET_FUNCS(device_io_width, struct btrfs_dev_item, io_width, 32);
677 BTRFS_SETGET_FUNCS(device_sector_size, struct btrfs_dev_item, sector_size, 32);
678 BTRFS_SETGET_FUNCS(device_id, struct btrfs_dev_item, devid, 64);
679 BTRFS_SETGET_FUNCS(device_group, struct btrfs_dev_item, dev_group, 32);
680 BTRFS_SETGET_FUNCS(device_seek_speed, struct btrfs_dev_item, seek_speed, 8);
681 BTRFS_SETGET_FUNCS(device_bandwidth, struct btrfs_dev_item, bandwidth, 8);
683 BTRFS_SETGET_STACK_FUNCS(stack_device_type, struct btrfs_dev_item, type, 64);
684 BTRFS_SETGET_STACK_FUNCS(stack_device_total_bytes, struct btrfs_dev_item,
685 total_bytes, 64);
686 BTRFS_SETGET_STACK_FUNCS(stack_device_bytes_used, struct btrfs_dev_item,
687 bytes_used, 64);
688 BTRFS_SETGET_STACK_FUNCS(stack_device_io_align, struct btrfs_dev_item,
689 io_align, 32);
690 BTRFS_SETGET_STACK_FUNCS(stack_device_io_width, struct btrfs_dev_item,
691 io_width, 32);
692 BTRFS_SETGET_STACK_FUNCS(stack_device_sector_size, struct btrfs_dev_item,
693 sector_size, 32);
694 BTRFS_SETGET_STACK_FUNCS(stack_device_id, struct btrfs_dev_item, devid, 64);
695 BTRFS_SETGET_STACK_FUNCS(stack_device_group, struct btrfs_dev_item,
696 dev_group, 32);
697 BTRFS_SETGET_STACK_FUNCS(stack_device_seek_speed, struct btrfs_dev_item,
698 seek_speed, 8);
699 BTRFS_SETGET_STACK_FUNCS(stack_device_bandwidth, struct btrfs_dev_item,
700 bandwidth, 8);
702 static inline char *btrfs_device_uuid(struct btrfs_dev_item *d)
704 return (char *)d + offsetof(struct btrfs_dev_item, uuid);
707 BTRFS_SETGET_FUNCS(chunk_length, struct btrfs_chunk, length, 64);
708 BTRFS_SETGET_FUNCS(chunk_owner, struct btrfs_chunk, owner, 64);
709 BTRFS_SETGET_FUNCS(chunk_stripe_len, struct btrfs_chunk, stripe_len, 64);
710 BTRFS_SETGET_FUNCS(chunk_io_align, struct btrfs_chunk, io_align, 32);
711 BTRFS_SETGET_FUNCS(chunk_io_width, struct btrfs_chunk, io_width, 32);
712 BTRFS_SETGET_FUNCS(chunk_sector_size, struct btrfs_chunk, sector_size, 32);
713 BTRFS_SETGET_FUNCS(chunk_type, struct btrfs_chunk, type, 64);
714 BTRFS_SETGET_FUNCS(chunk_num_stripes, struct btrfs_chunk, num_stripes, 16);
715 BTRFS_SETGET_FUNCS(chunk_sub_stripes, struct btrfs_chunk, sub_stripes, 16);
716 BTRFS_SETGET_FUNCS(stripe_devid, struct btrfs_stripe, devid, 64);
717 BTRFS_SETGET_FUNCS(stripe_offset, struct btrfs_stripe, offset, 64);
719 static inline char *btrfs_stripe_dev_uuid(struct btrfs_stripe *s)
721 return (char *)s + offsetof(struct btrfs_stripe, dev_uuid);
724 BTRFS_SETGET_STACK_FUNCS(stack_chunk_length, struct btrfs_chunk, length, 64);
725 BTRFS_SETGET_STACK_FUNCS(stack_chunk_owner, struct btrfs_chunk, owner, 64);
726 BTRFS_SETGET_STACK_FUNCS(stack_chunk_stripe_len, struct btrfs_chunk,
727 stripe_len, 64);
728 BTRFS_SETGET_STACK_FUNCS(stack_chunk_io_align, struct btrfs_chunk,
729 io_align, 32);
730 BTRFS_SETGET_STACK_FUNCS(stack_chunk_io_width, struct btrfs_chunk,
731 io_width, 32);
732 BTRFS_SETGET_STACK_FUNCS(stack_chunk_sector_size, struct btrfs_chunk,
733 sector_size, 32);
734 BTRFS_SETGET_STACK_FUNCS(stack_chunk_type, struct btrfs_chunk, type, 64);
735 BTRFS_SETGET_STACK_FUNCS(stack_chunk_num_stripes, struct btrfs_chunk,
736 num_stripes, 16);
737 BTRFS_SETGET_STACK_FUNCS(stack_chunk_sub_stripes, struct btrfs_chunk,
738 sub_stripes, 16);
739 BTRFS_SETGET_STACK_FUNCS(stack_stripe_devid, struct btrfs_stripe, devid, 64);
740 BTRFS_SETGET_STACK_FUNCS(stack_stripe_offset, struct btrfs_stripe, offset, 64);
742 static inline struct btrfs_stripe *btrfs_stripe_nr(struct btrfs_chunk *c,
743 int nr)
745 unsigned long offset = (unsigned long)c;
746 offset += offsetof(struct btrfs_chunk, stripe);
747 offset += nr * sizeof(struct btrfs_stripe);
748 return (struct btrfs_stripe *)offset;
751 static inline char *btrfs_stripe_dev_uuid_nr(struct btrfs_chunk *c, int nr)
753 return btrfs_stripe_dev_uuid(btrfs_stripe_nr(c, nr));
756 static inline u64 btrfs_stripe_offset_nr(struct extent_buffer *eb,
757 struct btrfs_chunk *c, int nr)
759 return btrfs_stripe_offset(eb, btrfs_stripe_nr(c, nr));
762 static inline void btrfs_set_stripe_offset_nr(struct extent_buffer *eb,
763 struct btrfs_chunk *c, int nr,
764 u64 val)
766 btrfs_set_stripe_offset(eb, btrfs_stripe_nr(c, nr), val);
769 static inline u64 btrfs_stripe_devid_nr(struct extent_buffer *eb,
770 struct btrfs_chunk *c, int nr)
772 return btrfs_stripe_devid(eb, btrfs_stripe_nr(c, nr));
775 static inline void btrfs_set_stripe_devid_nr(struct extent_buffer *eb,
776 struct btrfs_chunk *c, int nr,
777 u64 val)
779 btrfs_set_stripe_devid(eb, btrfs_stripe_nr(c, nr), val);
782 /* struct btrfs_block_group_item */
783 BTRFS_SETGET_STACK_FUNCS(block_group_used, struct btrfs_block_group_item,
784 used, 64);
785 BTRFS_SETGET_FUNCS(disk_block_group_used, struct btrfs_block_group_item,
786 used, 64);
787 BTRFS_SETGET_STACK_FUNCS(block_group_chunk_objectid,
788 struct btrfs_block_group_item, chunk_objectid, 64);
790 BTRFS_SETGET_FUNCS(disk_block_group_chunk_objectid,
791 struct btrfs_block_group_item, chunk_objectid, 64);
792 BTRFS_SETGET_FUNCS(disk_block_group_flags,
793 struct btrfs_block_group_item, flags, 64);
794 BTRFS_SETGET_STACK_FUNCS(block_group_flags,
795 struct btrfs_block_group_item, flags, 64);
797 /* struct btrfs_inode_ref */
798 BTRFS_SETGET_FUNCS(inode_ref_name_len, struct btrfs_inode_ref, name_len, 16);
799 BTRFS_SETGET_FUNCS(inode_ref_index, struct btrfs_inode_ref, index, 64);
801 /* struct btrfs_inode_item */
802 BTRFS_SETGET_FUNCS(inode_generation, struct btrfs_inode_item, generation, 64);
803 BTRFS_SETGET_FUNCS(inode_size, struct btrfs_inode_item, size, 64);
804 BTRFS_SETGET_FUNCS(inode_nblocks, struct btrfs_inode_item, nblocks, 64);
805 BTRFS_SETGET_FUNCS(inode_block_group, struct btrfs_inode_item, block_group, 64);
806 BTRFS_SETGET_FUNCS(inode_nlink, struct btrfs_inode_item, nlink, 32);
807 BTRFS_SETGET_FUNCS(inode_uid, struct btrfs_inode_item, uid, 32);
808 BTRFS_SETGET_FUNCS(inode_gid, struct btrfs_inode_item, gid, 32);
809 BTRFS_SETGET_FUNCS(inode_mode, struct btrfs_inode_item, mode, 32);
810 BTRFS_SETGET_FUNCS(inode_rdev, struct btrfs_inode_item, rdev, 64);
811 BTRFS_SETGET_FUNCS(inode_flags, struct btrfs_inode_item, flags, 16);
812 BTRFS_SETGET_FUNCS(inode_compat_flags, struct btrfs_inode_item,
813 compat_flags, 16);
815 BTRFS_SETGET_STACK_FUNCS(stack_inode_generation,
816 struct btrfs_inode_item, generation, 64);
817 BTRFS_SETGET_STACK_FUNCS(stack_inode_size,
818 struct btrfs_inode_item, size, 64);
819 BTRFS_SETGET_STACK_FUNCS(stack_inode_nblocks,
820 struct btrfs_inode_item, nblocks, 64);
821 BTRFS_SETGET_STACK_FUNCS(stack_inode_block_group,
822 struct btrfs_inode_item, block_group, 64);
823 BTRFS_SETGET_STACK_FUNCS(stack_inode_nlink,
824 struct btrfs_inode_item, nlink, 32);
825 BTRFS_SETGET_STACK_FUNCS(stack_inode_uid,
826 struct btrfs_inode_item, uid, 32);
827 BTRFS_SETGET_STACK_FUNCS(stack_inode_gid,
828 struct btrfs_inode_item, gid, 32);
829 BTRFS_SETGET_STACK_FUNCS(stack_inode_mode,
830 struct btrfs_inode_item, mode, 32);
831 BTRFS_SETGET_STACK_FUNCS(stack_inode_rdev,
832 struct btrfs_inode_item, rdev, 64);
833 BTRFS_SETGET_STACK_FUNCS(stack_inode_flags,
834 struct btrfs_inode_item, flags, 16);
835 BTRFS_SETGET_STACK_FUNCS(stack_inode_compat_flags,
836 struct btrfs_inode_item, compat_flags, 16);
838 static inline struct btrfs_timespec *
839 btrfs_inode_atime(struct btrfs_inode_item *inode_item)
841 unsigned long ptr = (unsigned long)inode_item;
842 ptr += offsetof(struct btrfs_inode_item, atime);
843 return (struct btrfs_timespec *)ptr;
846 static inline struct btrfs_timespec *
847 btrfs_inode_mtime(struct btrfs_inode_item *inode_item)
849 unsigned long ptr = (unsigned long)inode_item;
850 ptr += offsetof(struct btrfs_inode_item, mtime);
851 return (struct btrfs_timespec *)ptr;
854 static inline struct btrfs_timespec *
855 btrfs_inode_ctime(struct btrfs_inode_item *inode_item)
857 unsigned long ptr = (unsigned long)inode_item;
858 ptr += offsetof(struct btrfs_inode_item, ctime);
859 return (struct btrfs_timespec *)ptr;
862 static inline struct btrfs_timespec *
863 btrfs_inode_otime(struct btrfs_inode_item *inode_item)
865 unsigned long ptr = (unsigned long)inode_item;
866 ptr += offsetof(struct btrfs_inode_item, otime);
867 return (struct btrfs_timespec *)ptr;
870 BTRFS_SETGET_FUNCS(timespec_sec, struct btrfs_timespec, sec, 64);
871 BTRFS_SETGET_FUNCS(timespec_nsec, struct btrfs_timespec, nsec, 32);
872 BTRFS_SETGET_STACK_FUNCS(stack_timespec_sec, struct btrfs_timespec,
873 sec, 64);
874 BTRFS_SETGET_STACK_FUNCS(stack_timespec_nsec, struct btrfs_timespec,
875 nsec, 32);
877 /* struct btrfs_dev_extent */
878 BTRFS_SETGET_FUNCS(dev_extent_chunk_tree, struct btrfs_dev_extent,
879 chunk_tree, 64);
880 BTRFS_SETGET_FUNCS(dev_extent_chunk_objectid, struct btrfs_dev_extent,
881 chunk_objectid, 64);
882 BTRFS_SETGET_FUNCS(dev_extent_chunk_offset, struct btrfs_dev_extent,
883 chunk_offset, 64);
884 BTRFS_SETGET_FUNCS(dev_extent_length, struct btrfs_dev_extent, length, 64);
886 static inline u8 *btrfs_dev_extent_chunk_tree_uuid(struct btrfs_dev_extent *dev)
888 unsigned long ptr = offsetof(struct btrfs_dev_extent, chunk_tree_uuid);
889 return (u8 *)((unsigned long)dev + ptr);
892 /* struct btrfs_extent_item */
893 BTRFS_SETGET_FUNCS(extent_refs, struct btrfs_extent_item, refs, 32);
895 /* struct btrfs_extent_ref */
896 BTRFS_SETGET_FUNCS(ref_root, struct btrfs_extent_ref, root, 64);
897 BTRFS_SETGET_FUNCS(ref_generation, struct btrfs_extent_ref, generation, 64);
898 BTRFS_SETGET_FUNCS(ref_objectid, struct btrfs_extent_ref, objectid, 64);
899 BTRFS_SETGET_FUNCS(ref_offset, struct btrfs_extent_ref, offset, 64);
901 BTRFS_SETGET_STACK_FUNCS(stack_ref_root, struct btrfs_extent_ref, root, 64);
902 BTRFS_SETGET_STACK_FUNCS(stack_ref_generation, struct btrfs_extent_ref,
903 generation, 64);
904 BTRFS_SETGET_STACK_FUNCS(stack_ref_objectid, struct btrfs_extent_ref,
905 objectid, 64);
906 BTRFS_SETGET_STACK_FUNCS(stack_ref_offset, struct btrfs_extent_ref, offset, 64);
908 BTRFS_SETGET_STACK_FUNCS(stack_extent_refs, struct btrfs_extent_item,
909 refs, 32);
911 /* struct btrfs_node */
912 BTRFS_SETGET_FUNCS(key_blockptr, struct btrfs_key_ptr, blockptr, 64);
913 BTRFS_SETGET_FUNCS(key_generation, struct btrfs_key_ptr, generation, 64);
915 static inline u64 btrfs_node_blockptr(struct extent_buffer *eb, int nr)
917 unsigned long ptr;
918 ptr = offsetof(struct btrfs_node, ptrs) +
919 sizeof(struct btrfs_key_ptr) * nr;
920 return btrfs_key_blockptr(eb, (struct btrfs_key_ptr *)ptr);
923 static inline void btrfs_set_node_blockptr(struct extent_buffer *eb,
924 int nr, u64 val)
926 unsigned long ptr;
927 ptr = offsetof(struct btrfs_node, ptrs) +
928 sizeof(struct btrfs_key_ptr) * nr;
929 btrfs_set_key_blockptr(eb, (struct btrfs_key_ptr *)ptr, val);
932 static inline u64 btrfs_node_ptr_generation(struct extent_buffer *eb, int nr)
934 unsigned long ptr;
935 ptr = offsetof(struct btrfs_node, ptrs) +
936 sizeof(struct btrfs_key_ptr) * nr;
937 return btrfs_key_generation(eb, (struct btrfs_key_ptr *)ptr);
940 static inline void btrfs_set_node_ptr_generation(struct extent_buffer *eb,
941 int nr, u64 val)
943 unsigned long ptr;
944 ptr = offsetof(struct btrfs_node, ptrs) +
945 sizeof(struct btrfs_key_ptr) * nr;
946 btrfs_set_key_generation(eb, (struct btrfs_key_ptr *)ptr, val);
949 static inline unsigned long btrfs_node_key_ptr_offset(int nr)
951 return offsetof(struct btrfs_node, ptrs) +
952 sizeof(struct btrfs_key_ptr) * nr;
955 static inline void btrfs_node_key(struct extent_buffer *eb,
956 struct btrfs_disk_key *disk_key, int nr)
958 unsigned long ptr;
959 ptr = btrfs_node_key_ptr_offset(nr);
960 read_eb_member(eb, (struct btrfs_key_ptr *)ptr,
961 struct btrfs_key_ptr, key, disk_key);
964 static inline void btrfs_set_node_key(struct extent_buffer *eb,
965 struct btrfs_disk_key *disk_key, int nr)
967 unsigned long ptr;
968 ptr = btrfs_node_key_ptr_offset(nr);
969 write_eb_member(eb, (struct btrfs_key_ptr *)ptr,
970 struct btrfs_key_ptr, key, disk_key);
973 /* struct btrfs_item */
974 BTRFS_SETGET_FUNCS(item_offset, struct btrfs_item, offset, 32);
975 BTRFS_SETGET_FUNCS(item_size, struct btrfs_item, size, 32);
977 static inline unsigned long btrfs_item_nr_offset(int nr)
979 return offsetof(struct btrfs_leaf, items) +
980 sizeof(struct btrfs_item) * nr;
983 static inline struct btrfs_item *btrfs_item_nr(struct extent_buffer *eb,
984 int nr)
986 return (struct btrfs_item *)btrfs_item_nr_offset(nr);
989 static inline u32 btrfs_item_end(struct extent_buffer *eb,
990 struct btrfs_item *item)
992 return btrfs_item_offset(eb, item) + btrfs_item_size(eb, item);
995 static inline u32 btrfs_item_end_nr(struct extent_buffer *eb, int nr)
997 return btrfs_item_end(eb, btrfs_item_nr(eb, nr));
1000 static inline u32 btrfs_item_offset_nr(struct extent_buffer *eb, int nr)
1002 return btrfs_item_offset(eb, btrfs_item_nr(eb, nr));
1005 static inline u32 btrfs_item_size_nr(struct extent_buffer *eb, int nr)
1007 return btrfs_item_size(eb, btrfs_item_nr(eb, nr));
1010 static inline void btrfs_item_key(struct extent_buffer *eb,
1011 struct btrfs_disk_key *disk_key, int nr)
1013 struct btrfs_item *item = btrfs_item_nr(eb, nr);
1014 read_eb_member(eb, item, struct btrfs_item, key, disk_key);
1017 static inline void btrfs_set_item_key(struct extent_buffer *eb,
1018 struct btrfs_disk_key *disk_key, int nr)
1020 struct btrfs_item *item = btrfs_item_nr(eb, nr);
1021 write_eb_member(eb, item, struct btrfs_item, key, disk_key);
1024 /* struct btrfs_dir_item */
1025 BTRFS_SETGET_FUNCS(dir_data_len, struct btrfs_dir_item, data_len, 16);
1026 BTRFS_SETGET_FUNCS(dir_type, struct btrfs_dir_item, type, 8);
1027 BTRFS_SETGET_FUNCS(dir_name_len, struct btrfs_dir_item, name_len, 16);
1029 static inline void btrfs_dir_item_key(struct extent_buffer *eb,
1030 struct btrfs_dir_item *item,
1031 struct btrfs_disk_key *key)
1033 read_eb_member(eb, item, struct btrfs_dir_item, location, key);
1036 static inline void btrfs_set_dir_item_key(struct extent_buffer *eb,
1037 struct btrfs_dir_item *item,
1038 struct btrfs_disk_key *key)
1040 write_eb_member(eb, item, struct btrfs_dir_item, location, key);
1043 /* struct btrfs_disk_key */
1044 BTRFS_SETGET_STACK_FUNCS(disk_key_objectid, struct btrfs_disk_key,
1045 objectid, 64);
1046 BTRFS_SETGET_STACK_FUNCS(disk_key_offset, struct btrfs_disk_key, offset, 64);
1047 BTRFS_SETGET_STACK_FUNCS(disk_key_type, struct btrfs_disk_key, type, 8);
1049 static inline void btrfs_disk_key_to_cpu(struct btrfs_key *cpu,
1050 struct btrfs_disk_key *disk)
1052 cpu->offset = le64_to_cpu(disk->offset);
1053 cpu->type = disk->type;
1054 cpu->objectid = le64_to_cpu(disk->objectid);
1057 static inline void btrfs_cpu_key_to_disk(struct btrfs_disk_key *disk,
1058 struct btrfs_key *cpu)
1060 disk->offset = cpu_to_le64(cpu->offset);
1061 disk->type = cpu->type;
1062 disk->objectid = cpu_to_le64(cpu->objectid);
1065 static inline void btrfs_node_key_to_cpu(struct extent_buffer *eb,
1066 struct btrfs_key *key, int nr)
1068 struct btrfs_disk_key disk_key;
1069 btrfs_node_key(eb, &disk_key, nr);
1070 btrfs_disk_key_to_cpu(key, &disk_key);
1073 static inline void btrfs_item_key_to_cpu(struct extent_buffer *eb,
1074 struct btrfs_key *key, int nr)
1076 struct btrfs_disk_key disk_key;
1077 btrfs_item_key(eb, &disk_key, nr);
1078 btrfs_disk_key_to_cpu(key, &disk_key);
1081 static inline void btrfs_dir_item_key_to_cpu(struct extent_buffer *eb,
1082 struct btrfs_dir_item *item,
1083 struct btrfs_key *key)
1085 struct btrfs_disk_key disk_key;
1086 btrfs_dir_item_key(eb, item, &disk_key);
1087 btrfs_disk_key_to_cpu(key, &disk_key);
1091 static inline u8 btrfs_key_type(struct btrfs_key *key)
1093 return key->type;
1096 static inline void btrfs_set_key_type(struct btrfs_key *key, u8 val)
1098 key->type = val;
1101 /* struct btrfs_header */
1102 BTRFS_SETGET_HEADER_FUNCS(header_bytenr, struct btrfs_header, bytenr, 64);
1103 BTRFS_SETGET_HEADER_FUNCS(header_generation, struct btrfs_header,
1104 generation, 64);
1105 BTRFS_SETGET_HEADER_FUNCS(header_owner, struct btrfs_header, owner, 64);
1106 BTRFS_SETGET_HEADER_FUNCS(header_nritems, struct btrfs_header, nritems, 32);
1107 BTRFS_SETGET_HEADER_FUNCS(header_flags, struct btrfs_header, flags, 64);
1108 BTRFS_SETGET_HEADER_FUNCS(header_level, struct btrfs_header, level, 8);
1110 static inline int btrfs_header_flag(struct extent_buffer *eb, u64 flag)
1112 return (btrfs_header_flags(eb) & flag) == flag;
1115 static inline int btrfs_set_header_flag(struct extent_buffer *eb, u64 flag)
1117 u64 flags = btrfs_header_flags(eb);
1118 btrfs_set_header_flags(eb, flags | flag);
1119 return (flags & flag) == flag;
1122 static inline int btrfs_clear_header_flag(struct extent_buffer *eb, u64 flag)
1124 u64 flags = btrfs_header_flags(eb);
1125 btrfs_set_header_flags(eb, flags & ~flag);
1126 return (flags & flag) == flag;
1129 static inline u8 *btrfs_header_fsid(struct extent_buffer *eb)
1131 unsigned long ptr = offsetof(struct btrfs_header, fsid);
1132 return (u8 *)ptr;
1135 static inline u8 *btrfs_header_chunk_tree_uuid(struct extent_buffer *eb)
1137 unsigned long ptr = offsetof(struct btrfs_header, chunk_tree_uuid);
1138 return (u8 *)ptr;
1141 static inline u8 *btrfs_super_fsid(struct extent_buffer *eb)
1143 unsigned long ptr = offsetof(struct btrfs_super_block, fsid);
1144 return (u8 *)ptr;
1147 static inline u8 *btrfs_header_csum(struct extent_buffer *eb)
1149 unsigned long ptr = offsetof(struct btrfs_header, csum);
1150 return (u8 *)ptr;
1153 static inline struct btrfs_node *btrfs_buffer_node(struct extent_buffer *eb)
1155 return NULL;
1158 static inline struct btrfs_leaf *btrfs_buffer_leaf(struct extent_buffer *eb)
1160 return NULL;
1163 static inline struct btrfs_header *btrfs_buffer_header(struct extent_buffer *eb)
1165 return NULL;
1168 static inline int btrfs_is_leaf(struct extent_buffer *eb)
1170 return (btrfs_header_level(eb) == 0);
1173 /* struct btrfs_root_item */
1174 BTRFS_SETGET_FUNCS(disk_root_refs, struct btrfs_root_item, refs, 32);
1175 BTRFS_SETGET_FUNCS(disk_root_bytenr, struct btrfs_root_item, bytenr, 64);
1176 BTRFS_SETGET_FUNCS(disk_root_level, struct btrfs_root_item, level, 8);
1178 BTRFS_SETGET_STACK_FUNCS(root_bytenr, struct btrfs_root_item, bytenr, 64);
1179 BTRFS_SETGET_STACK_FUNCS(root_level, struct btrfs_root_item, level, 8);
1180 BTRFS_SETGET_STACK_FUNCS(root_dirid, struct btrfs_root_item, root_dirid, 64);
1181 BTRFS_SETGET_STACK_FUNCS(root_refs, struct btrfs_root_item, refs, 32);
1182 BTRFS_SETGET_STACK_FUNCS(root_flags, struct btrfs_root_item, flags, 32);
1183 BTRFS_SETGET_STACK_FUNCS(root_used, struct btrfs_root_item, bytes_used, 64);
1184 BTRFS_SETGET_STACK_FUNCS(root_limit, struct btrfs_root_item, byte_limit, 64);
1186 /* struct btrfs_super_block */
1187 BTRFS_SETGET_STACK_FUNCS(super_bytenr, struct btrfs_super_block, bytenr, 64);
1188 BTRFS_SETGET_STACK_FUNCS(super_generation, struct btrfs_super_block,
1189 generation, 64);
1190 BTRFS_SETGET_STACK_FUNCS(super_root, struct btrfs_super_block, root, 64);
1191 BTRFS_SETGET_STACK_FUNCS(super_sys_array_size,
1192 struct btrfs_super_block, sys_chunk_array_size, 32);
1193 BTRFS_SETGET_STACK_FUNCS(super_root_level, struct btrfs_super_block,
1194 root_level, 8);
1195 BTRFS_SETGET_STACK_FUNCS(super_chunk_root, struct btrfs_super_block,
1196 chunk_root, 64);
1197 BTRFS_SETGET_STACK_FUNCS(super_chunk_root_level, struct btrfs_super_block,
1198 chunk_root_level, 64);
1199 BTRFS_SETGET_STACK_FUNCS(super_total_bytes, struct btrfs_super_block,
1200 total_bytes, 64);
1201 BTRFS_SETGET_STACK_FUNCS(super_bytes_used, struct btrfs_super_block,
1202 bytes_used, 64);
1203 BTRFS_SETGET_STACK_FUNCS(super_sectorsize, struct btrfs_super_block,
1204 sectorsize, 32);
1205 BTRFS_SETGET_STACK_FUNCS(super_nodesize, struct btrfs_super_block,
1206 nodesize, 32);
1207 BTRFS_SETGET_STACK_FUNCS(super_leafsize, struct btrfs_super_block,
1208 leafsize, 32);
1209 BTRFS_SETGET_STACK_FUNCS(super_stripesize, struct btrfs_super_block,
1210 stripesize, 32);
1211 BTRFS_SETGET_STACK_FUNCS(super_root_dir, struct btrfs_super_block,
1212 root_dir_objectid, 64);
1213 BTRFS_SETGET_STACK_FUNCS(super_num_devices, struct btrfs_super_block,
1214 num_devices, 64);
1216 static inline unsigned long btrfs_leaf_data(struct extent_buffer *l)
1218 return offsetof(struct btrfs_leaf, items);
1221 /* struct btrfs_file_extent_item */
1222 BTRFS_SETGET_FUNCS(file_extent_type, struct btrfs_file_extent_item, type, 8);
1224 static inline unsigned long btrfs_file_extent_inline_start(struct
1225 btrfs_file_extent_item *e)
1227 unsigned long offset = (unsigned long)e;
1228 offset += offsetof(struct btrfs_file_extent_item, disk_bytenr);
1229 return offset;
1232 static inline u32 btrfs_file_extent_calc_inline_size(u32 datasize)
1234 return offsetof(struct btrfs_file_extent_item, disk_bytenr) + datasize;
1237 static inline u32 btrfs_file_extent_inline_len(struct extent_buffer *eb,
1238 struct btrfs_item *e)
1240 unsigned long offset;
1241 offset = offsetof(struct btrfs_file_extent_item, disk_bytenr);
1242 return btrfs_item_size(eb, e) - offset;
1245 BTRFS_SETGET_FUNCS(file_extent_disk_bytenr, struct btrfs_file_extent_item,
1246 disk_bytenr, 64);
1247 BTRFS_SETGET_FUNCS(file_extent_generation, struct btrfs_file_extent_item,
1248 generation, 64);
1249 BTRFS_SETGET_FUNCS(file_extent_disk_num_bytes, struct btrfs_file_extent_item,
1250 disk_num_bytes, 64);
1251 BTRFS_SETGET_FUNCS(file_extent_offset, struct btrfs_file_extent_item,
1252 offset, 64);
1253 BTRFS_SETGET_FUNCS(file_extent_num_bytes, struct btrfs_file_extent_item,
1254 num_bytes, 64);
1256 static inline u32 btrfs_level_size(struct btrfs_root *root, int level) {
1257 if (level == 0)
1258 return root->leafsize;
1259 return root->nodesize;
1262 /* helper function to cast into the data area of the leaf. */
1263 #define btrfs_item_ptr(leaf, slot, type) \
1264 ((type *)(btrfs_leaf_data(leaf) + \
1265 btrfs_item_offset_nr(leaf, slot)))
1267 #define btrfs_item_ptr_offset(leaf, slot) \
1268 ((unsigned long)(btrfs_leaf_data(leaf) + \
1269 btrfs_item_offset_nr(leaf, slot)))
1271 /* extent-tree.c */
1272 u32 btrfs_count_snapshots_in_path(struct btrfs_root *root,
1273 struct btrfs_path *count_path,
1274 u64 first_extent);
1275 int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
1276 struct btrfs_root *root);
1277 int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy);
1278 struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
1279 btrfs_fs_info *info,
1280 u64 bytenr);
1281 struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
1282 struct btrfs_block_group_cache
1283 *hint, u64 search_start,
1284 int data, int owner);
1285 int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
1286 struct btrfs_root *root, u64 owner_objectid);
1287 struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
1288 struct btrfs_root *root, u32 size,
1289 u64 root_objectid,
1290 u64 hint, u64 empty_size);
1291 struct extent_buffer *__btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
1292 struct btrfs_root *root,
1293 u32 blocksize,
1294 u64 root_objectid,
1295 u64 ref_generation,
1296 u64 first_objectid,
1297 int level,
1298 u64 hint,
1299 u64 empty_size);
1300 int btrfs_grow_extent_tree(struct btrfs_trans_handle *trans,
1301 struct btrfs_root *root, u64 new_size);
1302 int btrfs_shrink_extent_tree(struct btrfs_root *root, u64 new_size);
1303 int btrfs_insert_extent_backref(struct btrfs_trans_handle *trans,
1304 struct btrfs_root *root,
1305 struct btrfs_path *path, u64 bytenr,
1306 u64 root_objectid, u64 ref_generation,
1307 u64 owner, u64 owner_offset);
1308 int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
1309 struct btrfs_root *root,
1310 u64 num_bytes, u64 root_objectid, u64 ref_generation,
1311 u64 owner, u64 owner_offset,
1312 u64 empty_size, u64 hint_byte,
1313 u64 search_end, struct btrfs_key *ins, int data);
1314 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1315 struct extent_buffer *buf);
1316 int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
1317 *root, u64 bytenr, u64 num_bytes,
1318 u64 root_objectid, u64 ref_generation,
1319 u64 owner_objectid, u64 owner_offset, int pin);
1320 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
1321 struct btrfs_root *root,
1322 struct extent_io_tree *unpin);
1323 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1324 struct btrfs_root *root,
1325 u64 bytenr, u64 num_bytes,
1326 u64 root_objectid, u64 ref_generation,
1327 u64 owner, u64 owner_offset);
1328 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1329 struct btrfs_root *root);
1330 int btrfs_free_block_groups(struct btrfs_fs_info *info);
1331 int btrfs_read_block_groups(struct btrfs_root *root);
1332 int btrfs_make_block_group(struct btrfs_trans_handle *trans,
1333 struct btrfs_root *root, u64 bytes_used,
1334 u64 type, u64 chunk_objectid, u64 chunk_offset,
1335 u64 size);
1336 int btrfs_make_block_groups(struct btrfs_trans_handle *trans,
1337 struct btrfs_root *root);
1338 u64 btrfs_hash_extent_ref(u64 root_objectid, u64 ref_generation,
1339 u64 owner, u64 owner_offset);
1340 int btrfs_update_block_group(struct btrfs_trans_handle *trans,
1341 struct btrfs_root *root, u64 bytenr, u64 num,
1342 int alloc, int mark_free);
1343 /* ctree.c */
1344 int btrfs_previous_item(struct btrfs_root *root,
1345 struct btrfs_path *path, u64 min_objectid,
1346 int type);
1347 int btrfs_comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2);
1348 int btrfs_cow_block(struct btrfs_trans_handle *trans,
1349 struct btrfs_root *root, struct extent_buffer *buf,
1350 struct extent_buffer *parent, int parent_slot,
1351 struct extent_buffer **cow_ret);
1352 int __btrfs_cow_block(struct btrfs_trans_handle *trans,
1353 struct btrfs_root *root,
1354 struct extent_buffer *buf,
1355 struct extent_buffer *parent, int parent_slot,
1356 struct extent_buffer **cow_ret,
1357 u64 search_start, u64 empty_size);
1358 int btrfs_copy_root(struct btrfs_trans_handle *trans,
1359 struct btrfs_root *root,
1360 struct extent_buffer *buf,
1361 struct extent_buffer **cow_ret, u64 new_root_objectid);
1362 int btrfs_extend_item(struct btrfs_trans_handle *trans, struct btrfs_root
1363 *root, struct btrfs_path *path, u32 data_size);
1364 int btrfs_truncate_item(struct btrfs_trans_handle *trans,
1365 struct btrfs_root *root,
1366 struct btrfs_path *path,
1367 u32 new_size, int from_end);
1368 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1369 *root, struct btrfs_key *key, struct btrfs_path *p, int
1370 ins_len, int cow);
1371 int btrfs_realloc_node(struct btrfs_trans_handle *trans,
1372 struct btrfs_root *root, struct extent_buffer *parent,
1373 int start_slot, int cache_only, u64 *last_ret,
1374 struct btrfs_key *progress);
1375 void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p);
1376 struct btrfs_path *btrfs_alloc_path(void);
1377 void btrfs_free_path(struct btrfs_path *p);
1378 void btrfs_init_path(struct btrfs_path *p);
1379 int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1380 struct btrfs_path *path, int slot, int nr);
1382 static inline int btrfs_del_item(struct btrfs_trans_handle *trans,
1383 struct btrfs_root *root,
1384 struct btrfs_path *path)
1386 return btrfs_del_items(trans, root, path, path->slots[0], 1);
1389 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
1390 *root, struct btrfs_key *key, void *data, u32 data_size);
1391 int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
1392 struct btrfs_root *root,
1393 struct btrfs_path *path,
1394 struct btrfs_key *cpu_key, u32 *data_size, int nr);
1396 static inline int btrfs_insert_empty_item(struct btrfs_trans_handle *trans,
1397 struct btrfs_root *root,
1398 struct btrfs_path *path,
1399 struct btrfs_key *key,
1400 u32 data_size)
1402 return btrfs_insert_empty_items(trans, root, path, key, &data_size, 1);
1405 int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path);
1406 int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path);
1407 int btrfs_leaf_free_space(struct btrfs_root *root, struct extent_buffer *leaf);
1408 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
1409 *root);
1412 /* root-item.c */
1413 int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1414 struct btrfs_key *key);
1415 int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
1416 *root, struct btrfs_key *key, struct btrfs_root_item
1417 *item);
1418 int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
1419 *root, struct btrfs_key *key, struct btrfs_root_item
1420 *item);
1421 int btrfs_find_last_root(struct btrfs_root *root, u64 objectid, struct
1422 btrfs_root_item *item, struct btrfs_key *key);
1423 int btrfs_find_dead_roots(struct btrfs_root *root, u64 objectid,
1424 struct btrfs_root *latest_root);
1425 /* dir-item.c */
1426 int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
1427 *root, const char *name, int name_len, u64 dir,
1428 struct btrfs_key *location, u8 type);
1429 struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans,
1430 struct btrfs_root *root,
1431 struct btrfs_path *path, u64 dir,
1432 const char *name, int name_len,
1433 int mod);
1434 struct btrfs_dir_item *
1435 btrfs_lookup_dir_index_item(struct btrfs_trans_handle *trans,
1436 struct btrfs_root *root,
1437 struct btrfs_path *path, u64 dir,
1438 u64 objectid, const char *name, int name_len,
1439 int mod);
1440 struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_root *root,
1441 struct btrfs_path *path,
1442 const char *name, int name_len);
1443 int btrfs_delete_one_dir_name(struct btrfs_trans_handle *trans,
1444 struct btrfs_root *root,
1445 struct btrfs_path *path,
1446 struct btrfs_dir_item *di);
1447 int btrfs_insert_xattr_item(struct btrfs_trans_handle *trans,
1448 struct btrfs_root *root, const char *name,
1449 u16 name_len, const void *data, u16 data_len,
1450 u64 dir);
1451 struct btrfs_dir_item *btrfs_lookup_xattr(struct btrfs_trans_handle *trans,
1452 struct btrfs_root *root,
1453 struct btrfs_path *path, u64 dir,
1454 const char *name, u16 name_len,
1455 int mod);
1456 /* inode-map.c */
1457 int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
1458 struct btrfs_root *fs_root,
1459 u64 dirid, u64 *objectid);
1460 int btrfs_find_highest_inode(struct btrfs_root *fs_root, u64 *objectid);
1462 /* inode-item.c */
1463 int btrfs_insert_inode_ref(struct btrfs_trans_handle *trans,
1464 struct btrfs_root *root,
1465 const char *name, int name_len,
1466 u64 inode_objectid, u64 ref_objectid, u64 index);
1467 int btrfs_del_inode_ref(struct btrfs_trans_handle *trans,
1468 struct btrfs_root *root,
1469 const char *name, int name_len,
1470 u64 inode_objectid, u64 ref_objectid);
1471 int btrfs_insert_empty_inode(struct btrfs_trans_handle *trans,
1472 struct btrfs_root *root,
1473 struct btrfs_path *path, u64 objectid);
1474 int btrfs_insert_inode(struct btrfs_trans_handle *trans, struct btrfs_root
1475 *root, u64 objectid, struct btrfs_inode_item
1476 *inode_item);
1477 int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root
1478 *root, struct btrfs_path *path,
1479 struct btrfs_key *location, int mod);
1481 /* file-item.c */
1482 int btrfs_insert_file_extent(struct btrfs_trans_handle *trans,
1483 struct btrfs_root *root,
1484 u64 objectid, u64 pos, u64 offset,
1485 u64 disk_num_bytes,
1486 u64 num_bytes);
1487 int btrfs_insert_inline_extent(struct btrfs_trans_handle *trans,
1488 struct btrfs_root *root, u64 objectid,
1489 u64 offset, char *buffer, size_t size);
1490 int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
1491 struct btrfs_root *root,
1492 struct btrfs_path *path, u64 objectid,
1493 u64 bytenr, int mod);
1494 int btrfs_csum_file_block(struct btrfs_trans_handle *trans,
1495 struct btrfs_root *root,
1496 struct btrfs_inode_item *inode,
1497 u64 objectid, u64 offset,
1498 char *data, size_t len);
1499 struct btrfs_csum_item *btrfs_lookup_csum(struct btrfs_trans_handle *trans,
1500 struct btrfs_root *root,
1501 struct btrfs_path *path,
1502 u64 objectid, u64 offset,
1503 int cow);
1504 int btrfs_csum_truncate(struct btrfs_trans_handle *trans,
1505 struct btrfs_root *root, struct btrfs_path *path,
1506 u64 isize);
1507 #endif