1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * From linux/fs/btrfs/ctree.h
4 * Copyright (C) 2007,2008 Oracle. All rights reserved.
6 * Modified in 2017 by Marek Behun, CZ.NIC, marek.behun@nic.cz
9 #ifndef __BTRFS_CTREE_H__
10 #define __BTRFS_CTREE_H__
14 #include <linux/rbtree.h>
15 #include <linux/bug.h>
16 #include <linux/unaligned/le_byteshift.h>
17 #include <u-boot/crc.h>
18 #include "kernel-shared/btrfs_tree.h"
19 #include "crypto/hash.h"
21 #include "extent-io.h"
23 #define BTRFS_MAX_MIRRORS 3
26 * the max metadata block size. This limit is somewhat artificial,
27 * but the memmove costs go through the roof for larger blocks.
29 #define BTRFS_MAX_METADATA_BLOCKSIZE 65536
32 * Theoretical limit is larger, but we keep this down to a sane
33 * value. That should limit greatly the possibility of collisions on
36 #define BTRFS_LINK_MAX 65535U
38 /* four bytes for CRC32 */
39 #define BTRFS_EMPTY_DIR_SIZE 0
41 struct btrfs_mapping_tree {
42 struct cache_tree cache_tree;
45 static inline unsigned long btrfs_chunk_item_size(int num_stripes)
47 BUG_ON(num_stripes == 0);
48 return sizeof(struct btrfs_chunk) +
49 sizeof(struct btrfs_stripe) * (num_stripes - 1);
52 #define __BTRFS_LEAF_DATA_SIZE(bs) ((bs) - sizeof(struct btrfs_header))
53 #define BTRFS_LEAF_DATA_SIZE(fs_info) \
54 (__BTRFS_LEAF_DATA_SIZE(fs_info->nodesize))
55 /* ioprio of readahead is set to idle */
56 #define BTRFS_IOPRIO_READA (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0))
58 #define BTRFS_DIRTY_METADATA_THRESH SZ_32M
60 #define BTRFS_MAX_EXTENT_SIZE SZ_128M
62 enum btrfs_tree_block_status {
63 BTRFS_TREE_BLOCK_CLEAN,
64 BTRFS_TREE_BLOCK_INVALID_NRITEMS,
65 BTRFS_TREE_BLOCK_INVALID_PARENT_KEY,
66 BTRFS_TREE_BLOCK_BAD_KEY_ORDER,
67 BTRFS_TREE_BLOCK_INVALID_LEVEL,
68 BTRFS_TREE_BLOCK_INVALID_FREE_SPACE,
69 BTRFS_TREE_BLOCK_INVALID_OFFSETS,
73 struct extent_buffer *node;
74 struct btrfs_root_item root_item;
75 struct btrfs_key root_key;
76 struct btrfs_fs_info *fs_info;
86 struct rb_node rb_node;
90 struct btrfs_fs_devices;
91 struct btrfs_fs_info {
92 u8 chunk_tree_uuid[BTRFS_UUID_SIZE];
93 u8 *new_chunk_tree_uuid;
94 struct btrfs_root *fs_root;
95 struct btrfs_root *tree_root;
96 struct btrfs_root *chunk_root;
97 struct btrfs_root *csum_root;
99 struct rb_root fs_root_tree;
101 struct extent_io_tree extent_cache;
103 /* logical->physical extent mapping */
104 struct btrfs_mapping_tree mapping_tree;
106 u64 last_trans_committed;
108 struct btrfs_super_block *super_copy;
110 struct btrfs_fs_devices *fs_devices;
112 /* Cached block sizes */
118 static inline u32 BTRFS_MAX_ITEM_SIZE(const struct btrfs_fs_info *info)
120 return BTRFS_LEAF_DATA_SIZE(info) - sizeof(struct btrfs_item);
123 static inline u32 BTRFS_NODEPTRS_PER_BLOCK(const struct btrfs_fs_info *info)
125 return BTRFS_LEAF_DATA_SIZE(info) / sizeof(struct btrfs_key_ptr);
128 static inline u32 BTRFS_NODEPTRS_PER_EXTENT_BUFFER(const struct extent_buffer *eb)
130 BUG_ON(eb->fs_info && eb->fs_info->nodesize != eb->len);
131 return __BTRFS_LEAF_DATA_SIZE(eb->len) / sizeof(struct btrfs_key_ptr);
134 #define BTRFS_FILE_EXTENT_INLINE_DATA_START \
135 (offsetof(struct btrfs_file_extent_item, disk_bytenr))
136 static inline u32 BTRFS_MAX_INLINE_DATA_SIZE(const struct btrfs_fs_info *info)
138 return BTRFS_MAX_ITEM_SIZE(info) -
139 BTRFS_FILE_EXTENT_INLINE_DATA_START;
142 static inline u32 BTRFS_MAX_XATTR_SIZE(const struct btrfs_fs_info *info)
144 return BTRFS_MAX_ITEM_SIZE(info) - sizeof(struct btrfs_dir_item);
150 #define BTRFS_FS_STATE_ERROR 0
151 #define BTRFS_FS_STATE_REMOUNTING 1
152 #define BTRFS_FS_STATE_TRANS_ABORTED 2
153 #define BTRFS_FS_STATE_DEV_REPLACING 3
154 #define BTRFS_FS_STATE_DUMMY_FS_INFO 4
156 #define read_eb_member(eb, ptr, type, member, result) ( \
157 read_extent_buffer(eb, (char *)(result), \
158 ((unsigned long)(ptr)) + \
159 offsetof(type, member), \
160 sizeof(((type *)0)->member)))
162 #define write_eb_member(eb, ptr, type, member, result) ( \
163 write_extent_buffer(eb, (char *)(result), \
164 ((unsigned long)(ptr)) + \
165 offsetof(type, member), \
166 sizeof(((type *)0)->member)))
168 #define BTRFS_SETGET_HEADER_FUNCS(name, type, member, bits) \
169 static inline u##bits btrfs_##name(const struct extent_buffer *eb) \
171 const struct btrfs_header *h = (struct btrfs_header *)eb->data; \
172 return le##bits##_to_cpu(h->member); \
174 static inline void btrfs_set_##name(struct extent_buffer *eb, \
177 struct btrfs_header *h = (struct btrfs_header *)eb->data; \
178 h->member = cpu_to_le##bits(val); \
181 #define BTRFS_SETGET_FUNCS(name, type, member, bits) \
182 static inline u##bits btrfs_##name(const struct extent_buffer *eb, \
185 unsigned long offset = (unsigned long)s; \
186 const type *p = (type *) (eb->data + offset); \
187 return get_unaligned_le##bits(&p->member); \
189 static inline void btrfs_set_##name(struct extent_buffer *eb, \
190 type *s, u##bits val) \
192 unsigned long offset = (unsigned long)s; \
193 type *p = (type *) (eb->data + offset); \
194 put_unaligned_le##bits(val, &p->member); \
197 #define BTRFS_SETGET_STACK_FUNCS(name, type, member, bits) \
198 static inline u##bits btrfs_##name(const type *s) \
200 return le##bits##_to_cpu(s->member); \
202 static inline void btrfs_set_##name(type *s, u##bits val) \
204 s->member = cpu_to_le##bits(val); \
207 BTRFS_SETGET_FUNCS(device_type, struct btrfs_dev_item, type, 64);
208 BTRFS_SETGET_FUNCS(device_total_bytes, struct btrfs_dev_item, total_bytes, 64);
209 BTRFS_SETGET_FUNCS(device_bytes_used, struct btrfs_dev_item, bytes_used, 64);
210 BTRFS_SETGET_FUNCS(device_io_align, struct btrfs_dev_item, io_align, 32);
211 BTRFS_SETGET_FUNCS(device_io_width, struct btrfs_dev_item, io_width, 32);
212 BTRFS_SETGET_FUNCS(device_start_offset, struct btrfs_dev_item,
214 BTRFS_SETGET_FUNCS(device_sector_size, struct btrfs_dev_item, sector_size, 32);
215 BTRFS_SETGET_FUNCS(device_id, struct btrfs_dev_item, devid, 64);
216 BTRFS_SETGET_FUNCS(device_group, struct btrfs_dev_item, dev_group, 32);
217 BTRFS_SETGET_FUNCS(device_seek_speed, struct btrfs_dev_item, seek_speed, 8);
218 BTRFS_SETGET_FUNCS(device_bandwidth, struct btrfs_dev_item, bandwidth, 8);
219 BTRFS_SETGET_FUNCS(device_generation, struct btrfs_dev_item, generation, 64);
221 BTRFS_SETGET_STACK_FUNCS(stack_device_type, struct btrfs_dev_item, type, 64);
222 BTRFS_SETGET_STACK_FUNCS(stack_device_total_bytes, struct btrfs_dev_item,
224 BTRFS_SETGET_STACK_FUNCS(stack_device_bytes_used, struct btrfs_dev_item,
226 BTRFS_SETGET_STACK_FUNCS(stack_device_io_align, struct btrfs_dev_item,
228 BTRFS_SETGET_STACK_FUNCS(stack_device_io_width, struct btrfs_dev_item,
230 BTRFS_SETGET_STACK_FUNCS(stack_device_sector_size, struct btrfs_dev_item,
232 BTRFS_SETGET_STACK_FUNCS(stack_device_id, struct btrfs_dev_item, devid, 64);
233 BTRFS_SETGET_STACK_FUNCS(stack_device_group, struct btrfs_dev_item,
235 BTRFS_SETGET_STACK_FUNCS(stack_device_seek_speed, struct btrfs_dev_item,
237 BTRFS_SETGET_STACK_FUNCS(stack_device_bandwidth, struct btrfs_dev_item,
239 BTRFS_SETGET_STACK_FUNCS(stack_device_generation, struct btrfs_dev_item,
242 static inline char *btrfs_device_uuid(struct btrfs_dev_item *d)
244 return (char *)d + offsetof(struct btrfs_dev_item, uuid);
247 static inline char *btrfs_device_fsid(struct btrfs_dev_item *d)
249 return (char *)d + offsetof(struct btrfs_dev_item, fsid);
252 BTRFS_SETGET_FUNCS(chunk_length, struct btrfs_chunk, length, 64);
253 BTRFS_SETGET_FUNCS(chunk_owner, struct btrfs_chunk, owner, 64);
254 BTRFS_SETGET_FUNCS(chunk_stripe_len, struct btrfs_chunk, stripe_len, 64);
255 BTRFS_SETGET_FUNCS(chunk_io_align, struct btrfs_chunk, io_align, 32);
256 BTRFS_SETGET_FUNCS(chunk_io_width, struct btrfs_chunk, io_width, 32);
257 BTRFS_SETGET_FUNCS(chunk_sector_size, struct btrfs_chunk, sector_size, 32);
258 BTRFS_SETGET_FUNCS(chunk_type, struct btrfs_chunk, type, 64);
259 BTRFS_SETGET_FUNCS(chunk_num_stripes, struct btrfs_chunk, num_stripes, 16);
260 BTRFS_SETGET_FUNCS(chunk_sub_stripes, struct btrfs_chunk, sub_stripes, 16);
261 BTRFS_SETGET_FUNCS(stripe_devid, struct btrfs_stripe, devid, 64);
262 BTRFS_SETGET_FUNCS(stripe_offset, struct btrfs_stripe, offset, 64);
264 static inline char *btrfs_stripe_dev_uuid(struct btrfs_stripe *s)
266 return (char *)s + offsetof(struct btrfs_stripe, dev_uuid);
269 BTRFS_SETGET_STACK_FUNCS(stack_chunk_length, struct btrfs_chunk, length, 64);
270 BTRFS_SETGET_STACK_FUNCS(stack_chunk_owner, struct btrfs_chunk, owner, 64);
271 BTRFS_SETGET_STACK_FUNCS(stack_chunk_stripe_len, struct btrfs_chunk,
273 BTRFS_SETGET_STACK_FUNCS(stack_chunk_io_align, struct btrfs_chunk,
275 BTRFS_SETGET_STACK_FUNCS(stack_chunk_io_width, struct btrfs_chunk,
277 BTRFS_SETGET_STACK_FUNCS(stack_chunk_sector_size, struct btrfs_chunk,
279 BTRFS_SETGET_STACK_FUNCS(stack_chunk_type, struct btrfs_chunk, type, 64);
280 BTRFS_SETGET_STACK_FUNCS(stack_chunk_num_stripes, struct btrfs_chunk,
282 BTRFS_SETGET_STACK_FUNCS(stack_chunk_sub_stripes, struct btrfs_chunk,
284 BTRFS_SETGET_STACK_FUNCS(stack_stripe_devid, struct btrfs_stripe, devid, 64);
285 BTRFS_SETGET_STACK_FUNCS(stack_stripe_offset, struct btrfs_stripe, offset, 64);
287 static inline struct btrfs_stripe *btrfs_stripe_nr(struct btrfs_chunk *c,
290 unsigned long offset = (unsigned long)c;
291 offset += offsetof(struct btrfs_chunk, stripe);
292 offset += nr * sizeof(struct btrfs_stripe);
293 return (struct btrfs_stripe *)offset;
296 static inline char *btrfs_stripe_dev_uuid_nr(struct btrfs_chunk *c, int nr)
298 return btrfs_stripe_dev_uuid(btrfs_stripe_nr(c, nr));
301 static inline u64 btrfs_stripe_offset_nr(struct extent_buffer *eb,
302 struct btrfs_chunk *c, int nr)
304 return btrfs_stripe_offset(eb, btrfs_stripe_nr(c, nr));
307 static inline void btrfs_set_stripe_offset_nr(struct extent_buffer *eb,
308 struct btrfs_chunk *c, int nr,
311 btrfs_set_stripe_offset(eb, btrfs_stripe_nr(c, nr), val);
314 static inline u64 btrfs_stripe_devid_nr(struct extent_buffer *eb,
315 struct btrfs_chunk *c, int nr)
317 return btrfs_stripe_devid(eb, btrfs_stripe_nr(c, nr));
320 static inline void btrfs_set_stripe_devid_nr(struct extent_buffer *eb,
321 struct btrfs_chunk *c, int nr,
324 btrfs_set_stripe_devid(eb, btrfs_stripe_nr(c, nr), val);
327 /* struct btrfs_block_group_item */
328 BTRFS_SETGET_STACK_FUNCS(block_group_used, struct btrfs_block_group_item,
330 BTRFS_SETGET_FUNCS(disk_block_group_used, struct btrfs_block_group_item,
332 BTRFS_SETGET_STACK_FUNCS(block_group_chunk_objectid,
333 struct btrfs_block_group_item, chunk_objectid, 64);
335 BTRFS_SETGET_FUNCS(disk_block_group_chunk_objectid,
336 struct btrfs_block_group_item, chunk_objectid, 64);
337 BTRFS_SETGET_FUNCS(disk_block_group_flags,
338 struct btrfs_block_group_item, flags, 64);
339 BTRFS_SETGET_STACK_FUNCS(block_group_flags,
340 struct btrfs_block_group_item, flags, 64);
342 /* struct btrfs_free_space_info */
343 BTRFS_SETGET_FUNCS(free_space_extent_count, struct btrfs_free_space_info,
345 BTRFS_SETGET_FUNCS(free_space_flags, struct btrfs_free_space_info, flags, 32);
347 /* struct btrfs_inode_ref */
348 BTRFS_SETGET_FUNCS(inode_ref_name_len, struct btrfs_inode_ref, name_len, 16);
349 BTRFS_SETGET_STACK_FUNCS(stack_inode_ref_name_len, struct btrfs_inode_ref, name_len, 16);
350 BTRFS_SETGET_FUNCS(inode_ref_index, struct btrfs_inode_ref, index, 64);
352 /* struct btrfs_inode_extref */
353 BTRFS_SETGET_FUNCS(inode_extref_parent, struct btrfs_inode_extref,
354 parent_objectid, 64);
355 BTRFS_SETGET_FUNCS(inode_extref_name_len, struct btrfs_inode_extref,
357 BTRFS_SETGET_FUNCS(inode_extref_index, struct btrfs_inode_extref, index, 64);
359 /* struct btrfs_inode_item */
360 BTRFS_SETGET_FUNCS(inode_generation, struct btrfs_inode_item, generation, 64);
361 BTRFS_SETGET_FUNCS(inode_sequence, struct btrfs_inode_item, sequence, 64);
362 BTRFS_SETGET_FUNCS(inode_transid, struct btrfs_inode_item, transid, 64);
363 BTRFS_SETGET_FUNCS(inode_size, struct btrfs_inode_item, size, 64);
364 BTRFS_SETGET_FUNCS(inode_nbytes, struct btrfs_inode_item, nbytes, 64);
365 BTRFS_SETGET_FUNCS(inode_block_group, struct btrfs_inode_item, block_group, 64);
366 BTRFS_SETGET_FUNCS(inode_nlink, struct btrfs_inode_item, nlink, 32);
367 BTRFS_SETGET_FUNCS(inode_uid, struct btrfs_inode_item, uid, 32);
368 BTRFS_SETGET_FUNCS(inode_gid, struct btrfs_inode_item, gid, 32);
369 BTRFS_SETGET_FUNCS(inode_mode, struct btrfs_inode_item, mode, 32);
370 BTRFS_SETGET_FUNCS(inode_rdev, struct btrfs_inode_item, rdev, 64);
371 BTRFS_SETGET_FUNCS(inode_flags, struct btrfs_inode_item, flags, 64);
373 BTRFS_SETGET_STACK_FUNCS(stack_inode_generation,
374 struct btrfs_inode_item, generation, 64);
375 BTRFS_SETGET_STACK_FUNCS(stack_inode_sequence,
376 struct btrfs_inode_item, sequence, 64);
377 BTRFS_SETGET_STACK_FUNCS(stack_inode_transid,
378 struct btrfs_inode_item, transid, 64);
379 BTRFS_SETGET_STACK_FUNCS(stack_inode_size,
380 struct btrfs_inode_item, size, 64);
381 BTRFS_SETGET_STACK_FUNCS(stack_inode_nbytes,
382 struct btrfs_inode_item, nbytes, 64);
383 BTRFS_SETGET_STACK_FUNCS(stack_inode_block_group,
384 struct btrfs_inode_item, block_group, 64);
385 BTRFS_SETGET_STACK_FUNCS(stack_inode_nlink,
386 struct btrfs_inode_item, nlink, 32);
387 BTRFS_SETGET_STACK_FUNCS(stack_inode_uid,
388 struct btrfs_inode_item, uid, 32);
389 BTRFS_SETGET_STACK_FUNCS(stack_inode_gid,
390 struct btrfs_inode_item, gid, 32);
391 BTRFS_SETGET_STACK_FUNCS(stack_inode_mode,
392 struct btrfs_inode_item, mode, 32);
393 BTRFS_SETGET_STACK_FUNCS(stack_inode_rdev,
394 struct btrfs_inode_item, rdev, 64);
395 BTRFS_SETGET_STACK_FUNCS(stack_inode_flags,
396 struct btrfs_inode_item, flags, 64);
398 static inline struct btrfs_timespec *
399 btrfs_inode_atime(struct btrfs_inode_item *inode_item)
401 unsigned long ptr = (unsigned long)inode_item;
402 ptr += offsetof(struct btrfs_inode_item, atime);
403 return (struct btrfs_timespec *)ptr;
406 static inline struct btrfs_timespec *
407 btrfs_inode_mtime(struct btrfs_inode_item *inode_item)
409 unsigned long ptr = (unsigned long)inode_item;
410 ptr += offsetof(struct btrfs_inode_item, mtime);
411 return (struct btrfs_timespec *)ptr;
414 static inline struct btrfs_timespec *
415 btrfs_inode_ctime(struct btrfs_inode_item *inode_item)
417 unsigned long ptr = (unsigned long)inode_item;
418 ptr += offsetof(struct btrfs_inode_item, ctime);
419 return (struct btrfs_timespec *)ptr;
422 static inline struct btrfs_timespec *
423 btrfs_inode_otime(struct btrfs_inode_item *inode_item)
425 unsigned long ptr = (unsigned long)inode_item;
426 ptr += offsetof(struct btrfs_inode_item, otime);
427 return (struct btrfs_timespec *)ptr;
430 BTRFS_SETGET_FUNCS(timespec_sec, struct btrfs_timespec, sec, 64);
431 BTRFS_SETGET_FUNCS(timespec_nsec, struct btrfs_timespec, nsec, 32);
432 BTRFS_SETGET_STACK_FUNCS(stack_timespec_sec, struct btrfs_timespec,
434 BTRFS_SETGET_STACK_FUNCS(stack_timespec_nsec, struct btrfs_timespec,
437 /* struct btrfs_dev_extent */
438 BTRFS_SETGET_FUNCS(dev_extent_chunk_tree, struct btrfs_dev_extent,
440 BTRFS_SETGET_FUNCS(dev_extent_chunk_objectid, struct btrfs_dev_extent,
442 BTRFS_SETGET_FUNCS(dev_extent_chunk_offset, struct btrfs_dev_extent,
444 BTRFS_SETGET_FUNCS(dev_extent_length, struct btrfs_dev_extent, length, 64);
446 BTRFS_SETGET_STACK_FUNCS(stack_dev_extent_length, struct btrfs_dev_extent,
449 static inline u8 *btrfs_dev_extent_chunk_tree_uuid(struct btrfs_dev_extent *dev)
451 unsigned long ptr = offsetof(struct btrfs_dev_extent, chunk_tree_uuid);
452 return (u8 *)((unsigned long)dev + ptr);
456 /* struct btrfs_extent_item */
457 BTRFS_SETGET_FUNCS(extent_refs, struct btrfs_extent_item, refs, 64);
458 BTRFS_SETGET_STACK_FUNCS(stack_extent_refs, struct btrfs_extent_item, refs, 64);
459 BTRFS_SETGET_FUNCS(extent_generation, struct btrfs_extent_item,
461 BTRFS_SETGET_FUNCS(extent_flags, struct btrfs_extent_item, flags, 64);
462 BTRFS_SETGET_STACK_FUNCS(stack_extent_flags, struct btrfs_extent_item, flags, 64);
464 BTRFS_SETGET_FUNCS(extent_refs_v0, struct btrfs_extent_item_v0, refs, 32);
466 BTRFS_SETGET_FUNCS(tree_block_level, struct btrfs_tree_block_info, level, 8);
468 static inline void btrfs_tree_block_key(struct extent_buffer *eb,
469 struct btrfs_tree_block_info *item,
470 struct btrfs_disk_key *key)
472 read_eb_member(eb, item, struct btrfs_tree_block_info, key, key);
475 static inline void btrfs_set_tree_block_key(struct extent_buffer *eb,
476 struct btrfs_tree_block_info *item,
477 struct btrfs_disk_key *key)
479 write_eb_member(eb, item, struct btrfs_tree_block_info, key, key);
482 BTRFS_SETGET_FUNCS(extent_data_ref_root, struct btrfs_extent_data_ref,
484 BTRFS_SETGET_FUNCS(extent_data_ref_objectid, struct btrfs_extent_data_ref,
486 BTRFS_SETGET_FUNCS(extent_data_ref_offset, struct btrfs_extent_data_ref,
488 BTRFS_SETGET_FUNCS(extent_data_ref_count, struct btrfs_extent_data_ref,
491 BTRFS_SETGET_FUNCS(shared_data_ref_count, struct btrfs_shared_data_ref,
494 BTRFS_SETGET_FUNCS(extent_inline_ref_type, struct btrfs_extent_inline_ref,
496 BTRFS_SETGET_FUNCS(extent_inline_ref_offset, struct btrfs_extent_inline_ref,
498 BTRFS_SETGET_STACK_FUNCS(stack_extent_inline_ref_type,
499 struct btrfs_extent_inline_ref, type, 8);
500 BTRFS_SETGET_STACK_FUNCS(stack_extent_inline_ref_offset,
501 struct btrfs_extent_inline_ref, offset, 64);
503 static inline u32 btrfs_extent_inline_ref_size(int type)
505 if (type == BTRFS_TREE_BLOCK_REF_KEY ||
506 type == BTRFS_SHARED_BLOCK_REF_KEY)
507 return sizeof(struct btrfs_extent_inline_ref);
508 if (type == BTRFS_SHARED_DATA_REF_KEY)
509 return sizeof(struct btrfs_shared_data_ref) +
510 sizeof(struct btrfs_extent_inline_ref);
511 if (type == BTRFS_EXTENT_DATA_REF_KEY)
512 return sizeof(struct btrfs_extent_data_ref) +
513 offsetof(struct btrfs_extent_inline_ref, offset);
518 BTRFS_SETGET_FUNCS(ref_root_v0, struct btrfs_extent_ref_v0, root, 64);
519 BTRFS_SETGET_FUNCS(ref_generation_v0, struct btrfs_extent_ref_v0,
521 BTRFS_SETGET_FUNCS(ref_objectid_v0, struct btrfs_extent_ref_v0, objectid, 64);
522 BTRFS_SETGET_FUNCS(ref_count_v0, struct btrfs_extent_ref_v0, count, 32);
524 /* struct btrfs_node */
525 BTRFS_SETGET_FUNCS(key_blockptr, struct btrfs_key_ptr, blockptr, 64);
526 BTRFS_SETGET_FUNCS(key_generation, struct btrfs_key_ptr, generation, 64);
528 static inline u64 btrfs_node_blockptr(struct extent_buffer *eb, int nr)
531 ptr = offsetof(struct btrfs_node, ptrs) +
532 sizeof(struct btrfs_key_ptr) * nr;
533 return btrfs_key_blockptr(eb, (struct btrfs_key_ptr *)ptr);
536 static inline void btrfs_set_node_blockptr(struct extent_buffer *eb,
540 ptr = offsetof(struct btrfs_node, ptrs) +
541 sizeof(struct btrfs_key_ptr) * nr;
542 btrfs_set_key_blockptr(eb, (struct btrfs_key_ptr *)ptr, val);
545 static inline u64 btrfs_node_ptr_generation(struct extent_buffer *eb, int nr)
548 ptr = offsetof(struct btrfs_node, ptrs) +
549 sizeof(struct btrfs_key_ptr) * nr;
550 return btrfs_key_generation(eb, (struct btrfs_key_ptr *)ptr);
553 static inline void btrfs_set_node_ptr_generation(struct extent_buffer *eb,
557 ptr = offsetof(struct btrfs_node, ptrs) +
558 sizeof(struct btrfs_key_ptr) * nr;
559 btrfs_set_key_generation(eb, (struct btrfs_key_ptr *)ptr, val);
562 static inline unsigned long btrfs_node_key_ptr_offset(int nr)
564 return offsetof(struct btrfs_node, ptrs) +
565 sizeof(struct btrfs_key_ptr) * nr;
568 static inline void btrfs_node_key(struct extent_buffer *eb,
569 struct btrfs_disk_key *disk_key, int nr)
572 ptr = btrfs_node_key_ptr_offset(nr);
573 read_eb_member(eb, (struct btrfs_key_ptr *)ptr,
574 struct btrfs_key_ptr, key, disk_key);
577 static inline void btrfs_set_node_key(struct extent_buffer *eb,
578 struct btrfs_disk_key *disk_key, int nr)
581 ptr = btrfs_node_key_ptr_offset(nr);
582 write_eb_member(eb, (struct btrfs_key_ptr *)ptr,
583 struct btrfs_key_ptr, key, disk_key);
586 /* struct btrfs_item */
587 BTRFS_SETGET_FUNCS(item_offset, struct btrfs_item, offset, 32);
588 BTRFS_SETGET_FUNCS(item_size, struct btrfs_item, size, 32);
590 static inline unsigned long btrfs_item_nr_offset(int nr)
592 return offsetof(struct btrfs_leaf, items) +
593 sizeof(struct btrfs_item) * nr;
596 static inline struct btrfs_item *btrfs_item_nr(int nr)
598 return (struct btrfs_item *)btrfs_item_nr_offset(nr);
601 static inline u32 btrfs_item_end(struct extent_buffer *eb,
602 struct btrfs_item *item)
604 return btrfs_item_offset(eb, item) + btrfs_item_size(eb, item);
607 static inline u32 btrfs_item_end_nr(struct extent_buffer *eb, int nr)
609 return btrfs_item_end(eb, btrfs_item_nr(nr));
612 static inline u32 btrfs_item_offset_nr(const struct extent_buffer *eb, int nr)
614 return btrfs_item_offset(eb, btrfs_item_nr(nr));
617 static inline u32 btrfs_item_size_nr(struct extent_buffer *eb, int nr)
619 return btrfs_item_size(eb, btrfs_item_nr(nr));
622 static inline void btrfs_item_key(struct extent_buffer *eb,
623 struct btrfs_disk_key *disk_key, int nr)
625 struct btrfs_item *item = btrfs_item_nr(nr);
626 read_eb_member(eb, item, struct btrfs_item, key, disk_key);
629 static inline void btrfs_set_item_key(struct extent_buffer *eb,
630 struct btrfs_disk_key *disk_key, int nr)
632 struct btrfs_item *item = btrfs_item_nr(nr);
633 write_eb_member(eb, item, struct btrfs_item, key, disk_key);
636 BTRFS_SETGET_FUNCS(dir_log_end, struct btrfs_dir_log_item, end, 64);
639 * struct btrfs_root_ref
641 BTRFS_SETGET_FUNCS(root_ref_dirid, struct btrfs_root_ref, dirid, 64);
642 BTRFS_SETGET_FUNCS(root_ref_sequence, struct btrfs_root_ref, sequence, 64);
643 BTRFS_SETGET_FUNCS(root_ref_name_len, struct btrfs_root_ref, name_len, 16);
645 BTRFS_SETGET_STACK_FUNCS(stack_root_ref_dirid, struct btrfs_root_ref, dirid, 64);
646 BTRFS_SETGET_STACK_FUNCS(stack_root_ref_sequence, struct btrfs_root_ref, sequence, 64);
647 BTRFS_SETGET_STACK_FUNCS(stack_root_ref_name_len, struct btrfs_root_ref, name_len, 16);
649 /* struct btrfs_dir_item */
650 BTRFS_SETGET_FUNCS(dir_data_len, struct btrfs_dir_item, data_len, 16);
651 BTRFS_SETGET_FUNCS(dir_type, struct btrfs_dir_item, type, 8);
652 BTRFS_SETGET_FUNCS(dir_name_len, struct btrfs_dir_item, name_len, 16);
653 BTRFS_SETGET_FUNCS(dir_transid, struct btrfs_dir_item, transid, 64);
655 BTRFS_SETGET_STACK_FUNCS(stack_dir_data_len, struct btrfs_dir_item, data_len, 16);
656 BTRFS_SETGET_STACK_FUNCS(stack_dir_type, struct btrfs_dir_item, type, 8);
657 BTRFS_SETGET_STACK_FUNCS(stack_dir_name_len, struct btrfs_dir_item, name_len, 16);
658 BTRFS_SETGET_STACK_FUNCS(stack_dir_transid, struct btrfs_dir_item, transid, 64);
660 static inline void btrfs_dir_item_key(struct extent_buffer *eb,
661 struct btrfs_dir_item *item,
662 struct btrfs_disk_key *key)
664 read_eb_member(eb, item, struct btrfs_dir_item, location, key);
667 static inline void btrfs_set_dir_item_key(struct extent_buffer *eb,
668 struct btrfs_dir_item *item,
669 struct btrfs_disk_key *key)
671 write_eb_member(eb, item, struct btrfs_dir_item, location, key);
674 /* struct btrfs_free_space_header */
675 BTRFS_SETGET_FUNCS(free_space_entries, struct btrfs_free_space_header,
677 BTRFS_SETGET_FUNCS(free_space_bitmaps, struct btrfs_free_space_header,
679 BTRFS_SETGET_FUNCS(free_space_generation, struct btrfs_free_space_header,
682 static inline void btrfs_free_space_key(struct extent_buffer *eb,
683 struct btrfs_free_space_header *h,
684 struct btrfs_disk_key *key)
686 read_eb_member(eb, h, struct btrfs_free_space_header, location, key);
689 static inline void btrfs_set_free_space_key(struct extent_buffer *eb,
690 struct btrfs_free_space_header *h,
691 struct btrfs_disk_key *key)
693 write_eb_member(eb, h, struct btrfs_free_space_header, location, key);
696 /* struct btrfs_disk_key */
697 BTRFS_SETGET_STACK_FUNCS(disk_key_objectid, struct btrfs_disk_key,
699 BTRFS_SETGET_STACK_FUNCS(disk_key_offset, struct btrfs_disk_key, offset, 64);
700 BTRFS_SETGET_STACK_FUNCS(disk_key_type, struct btrfs_disk_key, type, 8);
702 static inline void btrfs_disk_key_to_cpu(struct btrfs_key *cpu,
703 struct btrfs_disk_key *disk)
705 cpu->offset = le64_to_cpu(disk->offset);
706 cpu->type = disk->type;
707 cpu->objectid = le64_to_cpu(disk->objectid);
710 static inline void btrfs_cpu_key_to_disk(struct btrfs_disk_key *disk,
711 const struct btrfs_key *cpu)
713 disk->offset = cpu_to_le64(cpu->offset);
714 disk->type = cpu->type;
715 disk->objectid = cpu_to_le64(cpu->objectid);
718 static inline void btrfs_node_key_to_cpu(struct extent_buffer *eb,
719 struct btrfs_key *key, int nr)
721 struct btrfs_disk_key disk_key;
722 btrfs_node_key(eb, &disk_key, nr);
723 btrfs_disk_key_to_cpu(key, &disk_key);
726 static inline void btrfs_item_key_to_cpu(struct extent_buffer *eb,
727 struct btrfs_key *key, int nr)
729 struct btrfs_disk_key disk_key;
730 btrfs_item_key(eb, &disk_key, nr);
731 btrfs_disk_key_to_cpu(key, &disk_key);
734 static inline void btrfs_dir_item_key_to_cpu(struct extent_buffer *eb,
735 struct btrfs_dir_item *item,
736 struct btrfs_key *key)
738 struct btrfs_disk_key disk_key;
739 btrfs_dir_item_key(eb, item, &disk_key);
740 btrfs_disk_key_to_cpu(key, &disk_key);
743 /* struct btrfs_header */
744 BTRFS_SETGET_HEADER_FUNCS(header_bytenr, struct btrfs_header, bytenr, 64);
745 BTRFS_SETGET_HEADER_FUNCS(header_generation, struct btrfs_header,
747 BTRFS_SETGET_HEADER_FUNCS(header_owner, struct btrfs_header, owner, 64);
748 BTRFS_SETGET_HEADER_FUNCS(header_nritems, struct btrfs_header, nritems, 32);
749 BTRFS_SETGET_HEADER_FUNCS(header_flags, struct btrfs_header, flags, 64);
750 BTRFS_SETGET_HEADER_FUNCS(header_level, struct btrfs_header, level, 8);
751 BTRFS_SETGET_STACK_FUNCS(stack_header_bytenr, struct btrfs_header, bytenr, 64);
752 BTRFS_SETGET_STACK_FUNCS(stack_header_nritems, struct btrfs_header, nritems,
754 BTRFS_SETGET_STACK_FUNCS(stack_header_owner, struct btrfs_header, owner, 64);
755 BTRFS_SETGET_STACK_FUNCS(stack_header_generation, struct btrfs_header,
758 static inline int btrfs_header_flag(struct extent_buffer *eb, u64 flag)
760 return (btrfs_header_flags(eb) & flag) == flag;
763 static inline int btrfs_set_header_flag(struct extent_buffer *eb, u64 flag)
765 u64 flags = btrfs_header_flags(eb);
766 btrfs_set_header_flags(eb, flags | flag);
767 return (flags & flag) == flag;
770 static inline int btrfs_clear_header_flag(struct extent_buffer *eb, u64 flag)
772 u64 flags = btrfs_header_flags(eb);
773 btrfs_set_header_flags(eb, flags & ~flag);
774 return (flags & flag) == flag;
777 static inline int btrfs_header_backref_rev(struct extent_buffer *eb)
779 u64 flags = btrfs_header_flags(eb);
780 return flags >> BTRFS_BACKREF_REV_SHIFT;
783 static inline void btrfs_set_header_backref_rev(struct extent_buffer *eb,
786 u64 flags = btrfs_header_flags(eb);
787 flags &= ~BTRFS_BACKREF_REV_MASK;
788 flags |= (u64)rev << BTRFS_BACKREF_REV_SHIFT;
789 btrfs_set_header_flags(eb, flags);
792 static inline unsigned long btrfs_header_fsid(void)
794 return offsetof(struct btrfs_header, fsid);
797 static inline unsigned long btrfs_header_chunk_tree_uuid(struct extent_buffer *eb)
799 return offsetof(struct btrfs_header, chunk_tree_uuid);
802 static inline u8 *btrfs_header_csum(struct extent_buffer *eb)
804 unsigned long ptr = offsetof(struct btrfs_header, csum);
808 static inline int btrfs_is_leaf(struct extent_buffer *eb)
810 return (btrfs_header_level(eb) == 0);
813 /* struct btrfs_root_item */
814 BTRFS_SETGET_FUNCS(disk_root_generation, struct btrfs_root_item,
816 BTRFS_SETGET_FUNCS(disk_root_refs, struct btrfs_root_item, refs, 32);
817 BTRFS_SETGET_FUNCS(disk_root_bytenr, struct btrfs_root_item, bytenr, 64);
818 BTRFS_SETGET_FUNCS(disk_root_level, struct btrfs_root_item, level, 8);
820 BTRFS_SETGET_STACK_FUNCS(root_generation, struct btrfs_root_item,
822 BTRFS_SETGET_STACK_FUNCS(root_bytenr, struct btrfs_root_item, bytenr, 64);
823 BTRFS_SETGET_STACK_FUNCS(root_level, struct btrfs_root_item, level, 8);
824 BTRFS_SETGET_STACK_FUNCS(root_dirid, struct btrfs_root_item, root_dirid, 64);
825 BTRFS_SETGET_STACK_FUNCS(root_refs, struct btrfs_root_item, refs, 32);
826 BTRFS_SETGET_STACK_FUNCS(root_flags, struct btrfs_root_item, flags, 64);
827 BTRFS_SETGET_STACK_FUNCS(root_used, struct btrfs_root_item, bytes_used, 64);
828 BTRFS_SETGET_STACK_FUNCS(root_limit, struct btrfs_root_item, byte_limit, 64);
829 BTRFS_SETGET_STACK_FUNCS(root_last_snapshot, struct btrfs_root_item,
831 BTRFS_SETGET_STACK_FUNCS(root_generation_v2, struct btrfs_root_item,
833 BTRFS_SETGET_STACK_FUNCS(root_ctransid, struct btrfs_root_item,
835 BTRFS_SETGET_STACK_FUNCS(root_otransid, struct btrfs_root_item,
837 BTRFS_SETGET_STACK_FUNCS(root_stransid, struct btrfs_root_item,
839 BTRFS_SETGET_STACK_FUNCS(root_rtransid, struct btrfs_root_item,
842 static inline struct btrfs_timespec* btrfs_root_ctime(
843 struct btrfs_root_item *root_item)
845 unsigned long ptr = (unsigned long)root_item;
846 ptr += offsetof(struct btrfs_root_item, ctime);
847 return (struct btrfs_timespec *)ptr;
850 static inline struct btrfs_timespec* btrfs_root_otime(
851 struct btrfs_root_item *root_item)
853 unsigned long ptr = (unsigned long)root_item;
854 ptr += offsetof(struct btrfs_root_item, otime);
855 return (struct btrfs_timespec *)ptr;
858 static inline struct btrfs_timespec* btrfs_root_stime(
859 struct btrfs_root_item *root_item)
861 unsigned long ptr = (unsigned long)root_item;
862 ptr += offsetof(struct btrfs_root_item, stime);
863 return (struct btrfs_timespec *)ptr;
866 static inline struct btrfs_timespec* btrfs_root_rtime(
867 struct btrfs_root_item *root_item)
869 unsigned long ptr = (unsigned long)root_item;
870 ptr += offsetof(struct btrfs_root_item, rtime);
871 return (struct btrfs_timespec *)ptr;
874 /* struct btrfs_root_backup */
875 BTRFS_SETGET_STACK_FUNCS(backup_tree_root, struct btrfs_root_backup,
877 BTRFS_SETGET_STACK_FUNCS(backup_tree_root_gen, struct btrfs_root_backup,
879 BTRFS_SETGET_STACK_FUNCS(backup_tree_root_level, struct btrfs_root_backup,
882 BTRFS_SETGET_STACK_FUNCS(backup_chunk_root, struct btrfs_root_backup,
884 BTRFS_SETGET_STACK_FUNCS(backup_chunk_root_gen, struct btrfs_root_backup,
886 BTRFS_SETGET_STACK_FUNCS(backup_chunk_root_level, struct btrfs_root_backup,
887 chunk_root_level, 8);
889 BTRFS_SETGET_STACK_FUNCS(backup_extent_root, struct btrfs_root_backup,
891 BTRFS_SETGET_STACK_FUNCS(backup_extent_root_gen, struct btrfs_root_backup,
892 extent_root_gen, 64);
893 BTRFS_SETGET_STACK_FUNCS(backup_extent_root_level, struct btrfs_root_backup,
894 extent_root_level, 8);
896 BTRFS_SETGET_STACK_FUNCS(backup_fs_root, struct btrfs_root_backup,
898 BTRFS_SETGET_STACK_FUNCS(backup_fs_root_gen, struct btrfs_root_backup,
900 BTRFS_SETGET_STACK_FUNCS(backup_fs_root_level, struct btrfs_root_backup,
903 BTRFS_SETGET_STACK_FUNCS(backup_dev_root, struct btrfs_root_backup,
905 BTRFS_SETGET_STACK_FUNCS(backup_dev_root_gen, struct btrfs_root_backup,
907 BTRFS_SETGET_STACK_FUNCS(backup_dev_root_level, struct btrfs_root_backup,
910 BTRFS_SETGET_STACK_FUNCS(backup_csum_root, struct btrfs_root_backup,
912 BTRFS_SETGET_STACK_FUNCS(backup_csum_root_gen, struct btrfs_root_backup,
914 BTRFS_SETGET_STACK_FUNCS(backup_csum_root_level, struct btrfs_root_backup,
916 BTRFS_SETGET_STACK_FUNCS(backup_total_bytes, struct btrfs_root_backup,
918 BTRFS_SETGET_STACK_FUNCS(backup_bytes_used, struct btrfs_root_backup,
920 BTRFS_SETGET_STACK_FUNCS(backup_num_devices, struct btrfs_root_backup,
923 /* struct btrfs_super_block */
925 BTRFS_SETGET_STACK_FUNCS(super_bytenr, struct btrfs_super_block, bytenr, 64);
926 BTRFS_SETGET_STACK_FUNCS(super_flags, struct btrfs_super_block, flags, 64);
927 BTRFS_SETGET_STACK_FUNCS(super_generation, struct btrfs_super_block,
929 BTRFS_SETGET_STACK_FUNCS(super_root, struct btrfs_super_block, root, 64);
930 BTRFS_SETGET_STACK_FUNCS(super_sys_array_size,
931 struct btrfs_super_block, sys_chunk_array_size, 32);
932 BTRFS_SETGET_STACK_FUNCS(super_chunk_root_generation,
933 struct btrfs_super_block, chunk_root_generation, 64);
934 BTRFS_SETGET_STACK_FUNCS(super_root_level, struct btrfs_super_block,
936 BTRFS_SETGET_STACK_FUNCS(super_chunk_root, struct btrfs_super_block,
938 BTRFS_SETGET_STACK_FUNCS(super_chunk_root_level, struct btrfs_super_block,
939 chunk_root_level, 8);
940 BTRFS_SETGET_STACK_FUNCS(super_log_root, struct btrfs_super_block,
942 BTRFS_SETGET_STACK_FUNCS(super_log_root_transid, struct btrfs_super_block,
943 log_root_transid, 64);
944 BTRFS_SETGET_STACK_FUNCS(super_log_root_level, struct btrfs_super_block,
946 BTRFS_SETGET_STACK_FUNCS(super_total_bytes, struct btrfs_super_block,
948 BTRFS_SETGET_STACK_FUNCS(super_bytes_used, struct btrfs_super_block,
950 BTRFS_SETGET_STACK_FUNCS(super_sectorsize, struct btrfs_super_block,
952 BTRFS_SETGET_STACK_FUNCS(super_nodesize, struct btrfs_super_block,
954 BTRFS_SETGET_STACK_FUNCS(super_stripesize, struct btrfs_super_block,
956 BTRFS_SETGET_STACK_FUNCS(super_root_dir, struct btrfs_super_block,
957 root_dir_objectid, 64);
958 BTRFS_SETGET_STACK_FUNCS(super_num_devices, struct btrfs_super_block,
960 BTRFS_SETGET_STACK_FUNCS(super_compat_flags, struct btrfs_super_block,
962 BTRFS_SETGET_STACK_FUNCS(super_compat_ro_flags, struct btrfs_super_block,
963 compat_ro_flags, 64);
964 BTRFS_SETGET_STACK_FUNCS(super_incompat_flags, struct btrfs_super_block,
966 BTRFS_SETGET_STACK_FUNCS(super_csum_type, struct btrfs_super_block,
968 BTRFS_SETGET_STACK_FUNCS(super_cache_generation, struct btrfs_super_block,
969 cache_generation, 64);
970 BTRFS_SETGET_STACK_FUNCS(super_uuid_tree_generation, struct btrfs_super_block,
971 uuid_tree_generation, 64);
972 BTRFS_SETGET_STACK_FUNCS(super_magic, struct btrfs_super_block, magic, 64);
974 static inline unsigned long btrfs_leaf_data(struct extent_buffer *l)
976 return offsetof(struct btrfs_leaf, items);
979 /* struct btrfs_file_extent_item */
980 BTRFS_SETGET_FUNCS(file_extent_type, struct btrfs_file_extent_item, type, 8);
981 BTRFS_SETGET_STACK_FUNCS(stack_file_extent_type, struct btrfs_file_extent_item, type, 8);
983 static inline unsigned long btrfs_file_extent_inline_start(struct
984 btrfs_file_extent_item *e)
986 unsigned long offset = (unsigned long)e;
987 offset += offsetof(struct btrfs_file_extent_item, disk_bytenr);
991 static inline u32 btrfs_file_extent_calc_inline_size(u32 datasize)
993 return offsetof(struct btrfs_file_extent_item, disk_bytenr) + datasize;
996 BTRFS_SETGET_FUNCS(file_extent_disk_bytenr, struct btrfs_file_extent_item,
998 BTRFS_SETGET_STACK_FUNCS(stack_file_extent_disk_bytenr, struct btrfs_file_extent_item,
1000 BTRFS_SETGET_FUNCS(file_extent_generation, struct btrfs_file_extent_item,
1002 BTRFS_SETGET_STACK_FUNCS(stack_file_extent_generation, struct btrfs_file_extent_item,
1004 BTRFS_SETGET_FUNCS(file_extent_disk_num_bytes, struct btrfs_file_extent_item,
1005 disk_num_bytes, 64);
1006 BTRFS_SETGET_FUNCS(file_extent_offset, struct btrfs_file_extent_item,
1008 BTRFS_SETGET_STACK_FUNCS(stack_file_extent_offset, struct btrfs_file_extent_item,
1010 BTRFS_SETGET_FUNCS(file_extent_num_bytes, struct btrfs_file_extent_item,
1012 BTRFS_SETGET_STACK_FUNCS(stack_file_extent_num_bytes, struct btrfs_file_extent_item,
1014 BTRFS_SETGET_FUNCS(file_extent_ram_bytes, struct btrfs_file_extent_item,
1016 BTRFS_SETGET_STACK_FUNCS(stack_file_extent_ram_bytes, struct btrfs_file_extent_item,
1018 BTRFS_SETGET_FUNCS(file_extent_compression, struct btrfs_file_extent_item,
1020 BTRFS_SETGET_STACK_FUNCS(stack_file_extent_compression, struct btrfs_file_extent_item,
1022 BTRFS_SETGET_FUNCS(file_extent_encryption, struct btrfs_file_extent_item,
1024 BTRFS_SETGET_FUNCS(file_extent_other_encoding, struct btrfs_file_extent_item,
1025 other_encoding, 16);
1027 /* btrfs_qgroup_status_item */
1028 BTRFS_SETGET_FUNCS(qgroup_status_version, struct btrfs_qgroup_status_item,
1030 BTRFS_SETGET_FUNCS(qgroup_status_generation, struct btrfs_qgroup_status_item,
1032 BTRFS_SETGET_FUNCS(qgroup_status_flags, struct btrfs_qgroup_status_item,
1034 BTRFS_SETGET_FUNCS(qgroup_status_rescan, struct btrfs_qgroup_status_item,
1037 BTRFS_SETGET_STACK_FUNCS(stack_qgroup_status_version,
1038 struct btrfs_qgroup_status_item, version, 64);
1039 BTRFS_SETGET_STACK_FUNCS(stack_qgroup_status_generation,
1040 struct btrfs_qgroup_status_item, generation, 64);
1041 BTRFS_SETGET_STACK_FUNCS(stack_qgroup_status_flags,
1042 struct btrfs_qgroup_status_item, flags, 64);
1043 BTRFS_SETGET_STACK_FUNCS(stack_qgroup_status_rescan,
1044 struct btrfs_qgroup_status_item, rescan, 64);
1046 /* btrfs_qgroup_info_item */
1047 BTRFS_SETGET_FUNCS(qgroup_info_generation, struct btrfs_qgroup_info_item,
1049 BTRFS_SETGET_FUNCS(qgroup_info_referenced, struct btrfs_qgroup_info_item,
1051 BTRFS_SETGET_FUNCS(qgroup_info_referenced_compressed,
1052 struct btrfs_qgroup_info_item, rfer_cmpr, 64);
1053 BTRFS_SETGET_FUNCS(qgroup_info_exclusive, struct btrfs_qgroup_info_item,
1055 BTRFS_SETGET_FUNCS(qgroup_info_exclusive_compressed,
1056 struct btrfs_qgroup_info_item, excl_cmpr, 64);
1058 BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_generation,
1059 struct btrfs_qgroup_info_item, generation, 64);
1060 BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_referenced,
1061 struct btrfs_qgroup_info_item, rfer, 64);
1062 BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_referenced_compressed,
1063 struct btrfs_qgroup_info_item, rfer_cmpr, 64);
1064 BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_exclusive,
1065 struct btrfs_qgroup_info_item, excl, 64);
1066 BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_exclusive_compressed,
1067 struct btrfs_qgroup_info_item, excl_cmpr, 64);
1069 /* btrfs_qgroup_limit_item */
1070 BTRFS_SETGET_FUNCS(qgroup_limit_flags, struct btrfs_qgroup_limit_item,
1072 BTRFS_SETGET_FUNCS(qgroup_limit_max_referenced, struct btrfs_qgroup_limit_item,
1074 BTRFS_SETGET_FUNCS(qgroup_limit_max_exclusive, struct btrfs_qgroup_limit_item,
1076 BTRFS_SETGET_FUNCS(qgroup_limit_rsv_referenced, struct btrfs_qgroup_limit_item,
1078 BTRFS_SETGET_FUNCS(qgroup_limit_rsv_exclusive, struct btrfs_qgroup_limit_item,
1081 BTRFS_SETGET_STACK_FUNCS(stack_qgroup_limit_flags,
1082 struct btrfs_qgroup_limit_item, flags, 64);
1083 BTRFS_SETGET_STACK_FUNCS(stack_qgroup_limit_max_referenced,
1084 struct btrfs_qgroup_limit_item, max_rfer, 64);
1085 BTRFS_SETGET_STACK_FUNCS(stack_qgroup_limit_max_exclusive,
1086 struct btrfs_qgroup_limit_item, max_excl, 64);
1087 BTRFS_SETGET_STACK_FUNCS(stack_qgroup_limit_rsv_referenced,
1088 struct btrfs_qgroup_limit_item, rsv_rfer, 64);
1089 BTRFS_SETGET_STACK_FUNCS(stack_qgroup_limit_rsv_exclusive,
1090 struct btrfs_qgroup_limit_item, rsv_excl, 64);
1092 /* btrfs_balance_item */
1093 BTRFS_SETGET_FUNCS(balance_item_flags, struct btrfs_balance_item, flags, 64);
1095 static inline struct btrfs_disk_balance_args* btrfs_balance_item_data(
1096 struct extent_buffer *eb, struct btrfs_balance_item *bi)
1098 unsigned long offset = (unsigned long)bi;
1099 struct btrfs_balance_item *p;
1100 p = (struct btrfs_balance_item *)(eb->data + offset);
1104 static inline struct btrfs_disk_balance_args* btrfs_balance_item_meta(
1105 struct extent_buffer *eb, struct btrfs_balance_item *bi)
1107 unsigned long offset = (unsigned long)bi;
1108 struct btrfs_balance_item *p;
1109 p = (struct btrfs_balance_item *)(eb->data + offset);
1113 static inline struct btrfs_disk_balance_args* btrfs_balance_item_sys(
1114 struct extent_buffer *eb, struct btrfs_balance_item *bi)
1116 unsigned long offset = (unsigned long)bi;
1117 struct btrfs_balance_item *p;
1118 p = (struct btrfs_balance_item *)(eb->data + offset);
1122 static inline u64 btrfs_dev_stats_value(const struct extent_buffer *eb,
1123 const struct btrfs_dev_stats_item *ptr,
1128 read_extent_buffer(eb, &val,
1129 offsetof(struct btrfs_dev_stats_item, values) +
1130 ((unsigned long)ptr) + (index * sizeof(u64)),
1136 * this returns the number of bytes used by the item on disk, minus the
1137 * size of any extent headers. If a file is compressed on disk, this is
1138 * the compressed size
1140 static inline u32 btrfs_file_extent_inline_item_len(struct extent_buffer *eb,
1141 struct btrfs_item *e)
1143 unsigned long offset;
1144 offset = offsetof(struct btrfs_file_extent_item, disk_bytenr);
1145 return btrfs_item_size(eb, e) - offset;
1148 #define btrfs_fs_incompat(fs_info, opt) \
1149 __btrfs_fs_incompat((fs_info), BTRFS_FEATURE_INCOMPAT_##opt)
1151 static inline bool __btrfs_fs_incompat(struct btrfs_fs_info *fs_info, u64 flag)
1153 struct btrfs_super_block *disk_super;
1154 disk_super = fs_info->super_copy;
1155 return !!(btrfs_super_incompat_flags(disk_super) & flag);
1158 #define btrfs_fs_compat_ro(fs_info, opt) \
1159 __btrfs_fs_compat_ro((fs_info), BTRFS_FEATURE_COMPAT_RO_##opt)
1161 static inline int __btrfs_fs_compat_ro(struct btrfs_fs_info *fs_info, u64 flag)
1163 struct btrfs_super_block *disk_super;
1164 disk_super = fs_info->super_copy;
1165 return !!(btrfs_super_compat_ro_flags(disk_super) & flag);
1168 /* helper function to cast into the data area of the leaf. */
1169 #define btrfs_item_ptr(leaf, slot, type) \
1170 ((type *)(btrfs_leaf_data(leaf) + \
1171 btrfs_item_offset_nr(leaf, slot)))
1173 #define btrfs_item_ptr_offset(leaf, slot) \
1174 ((unsigned long)(btrfs_leaf_data(leaf) + \
1175 btrfs_item_offset_nr(leaf, slot)))
1177 static inline u64 btrfs_name_hash(const char *name, int len)
1179 return (u64)crc32c((u32)~1, (u8 *)name, len);
1183 * Figure the key offset of an extended inode ref
1185 static inline u64 btrfs_extref_hash(u64 parent_objectid, const char *name,
1188 return crc32(parent_objectid, (u8 *)name, len);
1191 union btrfs_tree_node {
1192 struct btrfs_header header;
1193 struct btrfs_leaf leaf;
1194 struct btrfs_node node;
1197 struct __btrfs_path {
1198 union btrfs_tree_node *nodes[BTRFS_MAX_LEVEL];
1199 u32 slots[BTRFS_MAX_LEVEL];
1202 struct __btrfs_root {
1208 int __btrfs_comp_keys(struct btrfs_key *, struct btrfs_key *);
1209 int btrfs_comp_keys_type(struct btrfs_key *, struct btrfs_key *);
1210 int btrfs_bin_search(union btrfs_tree_node *, struct btrfs_key *, int *);
1211 void __btrfs_free_path(struct __btrfs_path *);
1212 int btrfs_search_tree(const struct __btrfs_root *, struct btrfs_key *,
1213 struct __btrfs_path *);
1214 int btrfs_prev_slot(struct __btrfs_path *);
1215 int btrfs_next_slot(struct __btrfs_path *);
1217 static inline struct btrfs_key *btrfs_path_leaf_key(struct __btrfs_path *p) {
1218 /* At tree read time we have converted the endian for btrfs_disk_key */
1219 return (struct btrfs_key *)&p->nodes[0]->leaf.items[p->slots[0]].key;
1222 static inline struct btrfs_key *
1223 btrfs_search_tree_key_type(const struct __btrfs_root *root, u64 objectid,
1224 u8 type, struct __btrfs_path *path)
1226 struct btrfs_key key, *res;
1228 key.objectid = objectid;
1232 if (btrfs_search_tree(root, &key, path))
1235 res = btrfs_path_leaf_key(path);
1236 if (btrfs_comp_keys_type(&key, res)) {
1237 __btrfs_free_path(path);
1244 static inline u32 btrfs_path_item_size(struct __btrfs_path *p)
1246 return p->nodes[0]->leaf.items[p->slots[0]].size;
1249 static inline void *__btrfs_leaf_data(struct btrfs_leaf *leaf, u32 slot)
1251 return ((u8 *) leaf) + sizeof(struct btrfs_header)
1252 + leaf->items[slot].offset;
1255 static inline void *btrfs_path_leaf_data(struct __btrfs_path *p)
1257 return __btrfs_leaf_data(&p->nodes[0]->leaf, p->slots[0]);
1260 #define btrfs_path_item_ptr(p,t) \
1261 ((t *) btrfs_path_leaf_data((p)))
1263 u16 btrfs_super_csum_size(const struct btrfs_super_block *s);
1264 const char *btrfs_super_csum_name(u16 csum_type);
1265 u16 btrfs_csum_type_size(u16 csum_type);
1266 size_t btrfs_super_num_csums(void);
1269 int btrfs_comp_cpu_keys(const struct btrfs_key *k1, const struct btrfs_key *k2);
1270 enum btrfs_tree_block_status
1271 btrfs_check_node(struct btrfs_fs_info *fs_info,
1272 struct btrfs_disk_key *parent_key, struct extent_buffer *buf);
1273 enum btrfs_tree_block_status
1274 btrfs_check_leaf(struct btrfs_fs_info *fs_info,
1275 struct btrfs_disk_key *parent_key, struct extent_buffer *buf);
1276 int btrfs_leaf_free_space(struct extent_buffer *leaf);
1277 #endif /* __BTRFS_CTREE_H__ */