blob: b7be09d5df839994e39fa96ee6e77f465e66d037 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Marek Behún4eff4262017-09-03 17:00:26 +02002/*
3 * From linux/fs/btrfs/ctree.h
4 * Copyright (C) 2007,2008 Oracle. All rights reserved.
5 *
Marek Behúnd63726e2022-06-01 17:17:06 +02006 * Modified in 2017 by Marek Behún, CZ.NIC, kabel@kernel.org
Marek Behún4eff4262017-09-03 17:00:26 +02007 */
8
9#ifndef __BTRFS_CTREE_H__
10#define __BTRFS_CTREE_H__
11
Marek Behún4eff4262017-09-03 17:00:26 +020012#include <compiler.h>
Qu Wenruoc43c5292020-06-24 18:02:52 +020013#include <linux/rbtree.h>
Qu Wenruof6377ff2020-06-24 18:02:54 +020014#include <linux/bug.h>
Pali Rohára0eae462022-04-07 14:51:03 +020015#include <asm/unaligned.h>
Qu Wenruo6925bd22020-06-24 18:02:53 +020016#include <u-boot/crc.h>
Qu Wenruo07d977f2020-06-24 18:02:47 +020017#include "kernel-shared/btrfs_tree.h"
Qu Wenruo6925bd22020-06-24 18:02:53 +020018#include "crypto/hash.h"
Qu Wenruo1a618082020-06-24 18:02:49 +020019#include "compat.h"
Qu Wenruoc43c5292020-06-24 18:02:52 +020020#include "extent-io.h"
Marek Behún4eff4262017-09-03 17:00:26 +020021
22#define BTRFS_MAX_MIRRORS 3
23
Marek Behún4eff4262017-09-03 17:00:26 +020024/*
25 * the max metadata block size. This limit is somewhat artificial,
26 * but the memmove costs go through the roof for larger blocks.
27 */
28#define BTRFS_MAX_METADATA_BLOCKSIZE 65536
29
30/*
Marek Behún4eff4262017-09-03 17:00:26 +020031 * Theoretical limit is larger, but we keep this down to a sane
32 * value. That should limit greatly the possibility of collisions on
33 * inode ref items.
34 */
35#define BTRFS_LINK_MAX 65535U
36
Marek Behún4eff4262017-09-03 17:00:26 +020037/* four bytes for CRC32 */
38#define BTRFS_EMPTY_DIR_SIZE 0
39
Qu Wenruo6925bd22020-06-24 18:02:53 +020040struct btrfs_mapping_tree {
41 struct cache_tree cache_tree;
42};
43
44static inline unsigned long btrfs_chunk_item_size(int num_stripes)
45{
46 BUG_ON(num_stripes == 0);
47 return sizeof(struct btrfs_chunk) +
48 sizeof(struct btrfs_stripe) * (num_stripes - 1);
49}
50
51#define __BTRFS_LEAF_DATA_SIZE(bs) ((bs) - sizeof(struct btrfs_header))
52#define BTRFS_LEAF_DATA_SIZE(fs_info) \
53 (__BTRFS_LEAF_DATA_SIZE(fs_info->nodesize))
Qu Wenruo0af17042020-06-24 18:02:59 +020054
55struct btrfs_path {
56 struct extent_buffer *nodes[BTRFS_MAX_LEVEL];
57 int slots[BTRFS_MAX_LEVEL];
58
59 /* keep some upper locks as we walk down */
60 u8 lowest_level;
61};
62
Marek Behún4eff4262017-09-03 17:00:26 +020063/* ioprio of readahead is set to idle */
64#define BTRFS_IOPRIO_READA (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0))
65
66#define BTRFS_DIRTY_METADATA_THRESH SZ_32M
67
68#define BTRFS_MAX_EXTENT_SIZE SZ_128M
69
Qu Wenruod85f9592020-06-24 18:02:55 +020070enum btrfs_tree_block_status {
71 BTRFS_TREE_BLOCK_CLEAN,
72 BTRFS_TREE_BLOCK_INVALID_NRITEMS,
73 BTRFS_TREE_BLOCK_INVALID_PARENT_KEY,
74 BTRFS_TREE_BLOCK_BAD_KEY_ORDER,
75 BTRFS_TREE_BLOCK_INVALID_LEVEL,
76 BTRFS_TREE_BLOCK_INVALID_FREE_SPACE,
77 BTRFS_TREE_BLOCK_INVALID_OFFSETS,
78};
79
Qu Wenruofd63dbb2020-06-24 18:02:58 +020080struct btrfs_root {
81 struct extent_buffer *node;
82 struct btrfs_root_item root_item;
83 struct btrfs_key root_key;
84 struct btrfs_fs_info *fs_info;
85 u64 objectid;
86 u64 last_trans;
87
88 int ref_cows;
89 int track_dirty;
90
91 u32 type;
92 u64 last_inode_alloc;
93
94 struct rb_node rb_node;
95};
96
Qu Wenruo0af17042020-06-24 18:02:59 +020097struct btrfs_trans_handle;
Qu Wenruo6925bd22020-06-24 18:02:53 +020098struct btrfs_device;
99struct btrfs_fs_devices;
100struct btrfs_fs_info {
101 u8 chunk_tree_uuid[BTRFS_UUID_SIZE];
102 u8 *new_chunk_tree_uuid;
103 struct btrfs_root *fs_root;
104 struct btrfs_root *tree_root;
105 struct btrfs_root *chunk_root;
106 struct btrfs_root *csum_root;
107
108 struct rb_root fs_root_tree;
109
110 struct extent_io_tree extent_cache;
111
112 /* logical->physical extent mapping */
113 struct btrfs_mapping_tree mapping_tree;
114
115 u64 last_trans_committed;
116
117 struct btrfs_super_block *super_copy;
118
119 struct btrfs_fs_devices *fs_devices;
120
121 /* Cached block sizes */
122 u32 nodesize;
123 u32 sectorsize;
124 u32 stripesize;
125};
126
Qu Wenruod85f9592020-06-24 18:02:55 +0200127static inline u32 BTRFS_MAX_ITEM_SIZE(const struct btrfs_fs_info *info)
128{
129 return BTRFS_LEAF_DATA_SIZE(info) - sizeof(struct btrfs_item);
130}
131
132static inline u32 BTRFS_NODEPTRS_PER_BLOCK(const struct btrfs_fs_info *info)
133{
134 return BTRFS_LEAF_DATA_SIZE(info) / sizeof(struct btrfs_key_ptr);
135}
136
137static inline u32 BTRFS_NODEPTRS_PER_EXTENT_BUFFER(const struct extent_buffer *eb)
138{
139 BUG_ON(eb->fs_info && eb->fs_info->nodesize != eb->len);
140 return __BTRFS_LEAF_DATA_SIZE(eb->len) / sizeof(struct btrfs_key_ptr);
141}
142
143#define BTRFS_FILE_EXTENT_INLINE_DATA_START \
144 (offsetof(struct btrfs_file_extent_item, disk_bytenr))
145static inline u32 BTRFS_MAX_INLINE_DATA_SIZE(const struct btrfs_fs_info *info)
146{
147 return BTRFS_MAX_ITEM_SIZE(info) -
148 BTRFS_FILE_EXTENT_INLINE_DATA_START;
149}
150
151static inline u32 BTRFS_MAX_XATTR_SIZE(const struct btrfs_fs_info *info)
152{
153 return BTRFS_MAX_ITEM_SIZE(info) - sizeof(struct btrfs_dir_item);
154}
155
Marek Behún4eff4262017-09-03 17:00:26 +0200156/*
157 * File system states
158 */
159#define BTRFS_FS_STATE_ERROR 0
160#define BTRFS_FS_STATE_REMOUNTING 1
161#define BTRFS_FS_STATE_TRANS_ABORTED 2
162#define BTRFS_FS_STATE_DEV_REPLACING 3
163#define BTRFS_FS_STATE_DUMMY_FS_INFO 4
164
Qu Wenruoc43c5292020-06-24 18:02:52 +0200165#define read_eb_member(eb, ptr, type, member, result) ( \
166 read_extent_buffer(eb, (char *)(result), \
167 ((unsigned long)(ptr)) + \
168 offsetof(type, member), \
169 sizeof(((type *)0)->member)))
170
171#define write_eb_member(eb, ptr, type, member, result) ( \
172 write_extent_buffer(eb, (char *)(result), \
173 ((unsigned long)(ptr)) + \
174 offsetof(type, member), \
175 sizeof(((type *)0)->member)))
176
Qu Wenruo6925bd22020-06-24 18:02:53 +0200177#define BTRFS_SETGET_HEADER_FUNCS(name, type, member, bits) \
178static inline u##bits btrfs_##name(const struct extent_buffer *eb) \
179{ \
180 const struct btrfs_header *h = (struct btrfs_header *)eb->data; \
181 return le##bits##_to_cpu(h->member); \
182} \
183static inline void btrfs_set_##name(struct extent_buffer *eb, \
184 u##bits val) \
185{ \
186 struct btrfs_header *h = (struct btrfs_header *)eb->data; \
187 h->member = cpu_to_le##bits(val); \
188}
189
190#define BTRFS_SETGET_FUNCS(name, type, member, bits) \
191static inline u##bits btrfs_##name(const struct extent_buffer *eb, \
192 const type *s) \
193{ \
194 unsigned long offset = (unsigned long)s; \
195 const type *p = (type *) (eb->data + offset); \
196 return get_unaligned_le##bits(&p->member); \
197} \
198static inline void btrfs_set_##name(struct extent_buffer *eb, \
199 type *s, u##bits val) \
200{ \
201 unsigned long offset = (unsigned long)s; \
202 type *p = (type *) (eb->data + offset); \
203 put_unaligned_le##bits(val, &p->member); \
204}
205
Qu Wenruo1a618082020-06-24 18:02:49 +0200206#define BTRFS_SETGET_STACK_FUNCS(name, type, member, bits) \
207static inline u##bits btrfs_##name(const type *s) \
208{ \
209 return le##bits##_to_cpu(s->member); \
210} \
211static inline void btrfs_set_##name(type *s, u##bits val) \
212{ \
213 s->member = cpu_to_le##bits(val); \
214}
215
Qu Wenruo6925bd22020-06-24 18:02:53 +0200216BTRFS_SETGET_FUNCS(device_type, struct btrfs_dev_item, type, 64);
217BTRFS_SETGET_FUNCS(device_total_bytes, struct btrfs_dev_item, total_bytes, 64);
218BTRFS_SETGET_FUNCS(device_bytes_used, struct btrfs_dev_item, bytes_used, 64);
219BTRFS_SETGET_FUNCS(device_io_align, struct btrfs_dev_item, io_align, 32);
220BTRFS_SETGET_FUNCS(device_io_width, struct btrfs_dev_item, io_width, 32);
221BTRFS_SETGET_FUNCS(device_start_offset, struct btrfs_dev_item,
222 start_offset, 64);
223BTRFS_SETGET_FUNCS(device_sector_size, struct btrfs_dev_item, sector_size, 32);
224BTRFS_SETGET_FUNCS(device_id, struct btrfs_dev_item, devid, 64);
225BTRFS_SETGET_FUNCS(device_group, struct btrfs_dev_item, dev_group, 32);
226BTRFS_SETGET_FUNCS(device_seek_speed, struct btrfs_dev_item, seek_speed, 8);
227BTRFS_SETGET_FUNCS(device_bandwidth, struct btrfs_dev_item, bandwidth, 8);
228BTRFS_SETGET_FUNCS(device_generation, struct btrfs_dev_item, generation, 64);
Marek Behún4eff4262017-09-03 17:00:26 +0200229
Qu Wenruo6925bd22020-06-24 18:02:53 +0200230BTRFS_SETGET_STACK_FUNCS(stack_device_type, struct btrfs_dev_item, type, 64);
231BTRFS_SETGET_STACK_FUNCS(stack_device_total_bytes, struct btrfs_dev_item,
232 total_bytes, 64);
233BTRFS_SETGET_STACK_FUNCS(stack_device_bytes_used, struct btrfs_dev_item,
234 bytes_used, 64);
235BTRFS_SETGET_STACK_FUNCS(stack_device_io_align, struct btrfs_dev_item,
236 io_align, 32);
237BTRFS_SETGET_STACK_FUNCS(stack_device_io_width, struct btrfs_dev_item,
238 io_width, 32);
239BTRFS_SETGET_STACK_FUNCS(stack_device_sector_size, struct btrfs_dev_item,
240 sector_size, 32);
241BTRFS_SETGET_STACK_FUNCS(stack_device_id, struct btrfs_dev_item, devid, 64);
242BTRFS_SETGET_STACK_FUNCS(stack_device_group, struct btrfs_dev_item,
243 dev_group, 32);
244BTRFS_SETGET_STACK_FUNCS(stack_device_seek_speed, struct btrfs_dev_item,
245 seek_speed, 8);
246BTRFS_SETGET_STACK_FUNCS(stack_device_bandwidth, struct btrfs_dev_item,
247 bandwidth, 8);
248BTRFS_SETGET_STACK_FUNCS(stack_device_generation, struct btrfs_dev_item,
249 generation, 64);
Marek Behún4eff4262017-09-03 17:00:26 +0200250
Qu Wenruo6925bd22020-06-24 18:02:53 +0200251static inline char *btrfs_device_uuid(struct btrfs_dev_item *d)
252{
253 return (char *)d + offsetof(struct btrfs_dev_item, uuid);
254}
Marek Behún4eff4262017-09-03 17:00:26 +0200255
Qu Wenruo6925bd22020-06-24 18:02:53 +0200256static inline char *btrfs_device_fsid(struct btrfs_dev_item *d)
257{
258 return (char *)d + offsetof(struct btrfs_dev_item, fsid);
259}
Qu Wenruoc43c5292020-06-24 18:02:52 +0200260
Qu Wenruo6925bd22020-06-24 18:02:53 +0200261BTRFS_SETGET_FUNCS(chunk_length, struct btrfs_chunk, length, 64);
262BTRFS_SETGET_FUNCS(chunk_owner, struct btrfs_chunk, owner, 64);
263BTRFS_SETGET_FUNCS(chunk_stripe_len, struct btrfs_chunk, stripe_len, 64);
264BTRFS_SETGET_FUNCS(chunk_io_align, struct btrfs_chunk, io_align, 32);
265BTRFS_SETGET_FUNCS(chunk_io_width, struct btrfs_chunk, io_width, 32);
266BTRFS_SETGET_FUNCS(chunk_sector_size, struct btrfs_chunk, sector_size, 32);
267BTRFS_SETGET_FUNCS(chunk_type, struct btrfs_chunk, type, 64);
268BTRFS_SETGET_FUNCS(chunk_num_stripes, struct btrfs_chunk, num_stripes, 16);
269BTRFS_SETGET_FUNCS(chunk_sub_stripes, struct btrfs_chunk, sub_stripes, 16);
270BTRFS_SETGET_FUNCS(stripe_devid, struct btrfs_stripe, devid, 64);
271BTRFS_SETGET_FUNCS(stripe_offset, struct btrfs_stripe, offset, 64);
Qu Wenruoc43c5292020-06-24 18:02:52 +0200272
Qu Wenruo6925bd22020-06-24 18:02:53 +0200273static inline char *btrfs_stripe_dev_uuid(struct btrfs_stripe *s)
274{
275 return (char *)s + offsetof(struct btrfs_stripe, dev_uuid);
276}
Qu Wenruoc43c5292020-06-24 18:02:52 +0200277
Qu Wenruo6925bd22020-06-24 18:02:53 +0200278BTRFS_SETGET_STACK_FUNCS(stack_chunk_length, struct btrfs_chunk, length, 64);
279BTRFS_SETGET_STACK_FUNCS(stack_chunk_owner, struct btrfs_chunk, owner, 64);
280BTRFS_SETGET_STACK_FUNCS(stack_chunk_stripe_len, struct btrfs_chunk,
281 stripe_len, 64);
282BTRFS_SETGET_STACK_FUNCS(stack_chunk_io_align, struct btrfs_chunk,
283 io_align, 32);
284BTRFS_SETGET_STACK_FUNCS(stack_chunk_io_width, struct btrfs_chunk,
285 io_width, 32);
286BTRFS_SETGET_STACK_FUNCS(stack_chunk_sector_size, struct btrfs_chunk,
287 sector_size, 32);
288BTRFS_SETGET_STACK_FUNCS(stack_chunk_type, struct btrfs_chunk, type, 64);
289BTRFS_SETGET_STACK_FUNCS(stack_chunk_num_stripes, struct btrfs_chunk,
290 num_stripes, 16);
291BTRFS_SETGET_STACK_FUNCS(stack_chunk_sub_stripes, struct btrfs_chunk,
292 sub_stripes, 16);
293BTRFS_SETGET_STACK_FUNCS(stack_stripe_devid, struct btrfs_stripe, devid, 64);
294BTRFS_SETGET_STACK_FUNCS(stack_stripe_offset, struct btrfs_stripe, offset, 64);
Qu Wenruoc43c5292020-06-24 18:02:52 +0200295
Qu Wenruo6925bd22020-06-24 18:02:53 +0200296static inline struct btrfs_stripe *btrfs_stripe_nr(struct btrfs_chunk *c,
297 int nr)
298{
299 unsigned long offset = (unsigned long)c;
300 offset += offsetof(struct btrfs_chunk, stripe);
301 offset += nr * sizeof(struct btrfs_stripe);
302 return (struct btrfs_stripe *)offset;
303}
Qu Wenruoc43c5292020-06-24 18:02:52 +0200304
Qu Wenruo6925bd22020-06-24 18:02:53 +0200305static inline char *btrfs_stripe_dev_uuid_nr(struct btrfs_chunk *c, int nr)
306{
307 return btrfs_stripe_dev_uuid(btrfs_stripe_nr(c, nr));
308}
Qu Wenruoc43c5292020-06-24 18:02:52 +0200309
Qu Wenruo6925bd22020-06-24 18:02:53 +0200310static inline u64 btrfs_stripe_offset_nr(struct extent_buffer *eb,
311 struct btrfs_chunk *c, int nr)
312{
313 return btrfs_stripe_offset(eb, btrfs_stripe_nr(c, nr));
314}
Qu Wenruoc43c5292020-06-24 18:02:52 +0200315
Qu Wenruo6925bd22020-06-24 18:02:53 +0200316static inline void btrfs_set_stripe_offset_nr(struct extent_buffer *eb,
317 struct btrfs_chunk *c, int nr,
318 u64 val)
319{
320 btrfs_set_stripe_offset(eb, btrfs_stripe_nr(c, nr), val);
321}
Qu Wenruoc43c5292020-06-24 18:02:52 +0200322
Qu Wenruo6925bd22020-06-24 18:02:53 +0200323static inline u64 btrfs_stripe_devid_nr(struct extent_buffer *eb,
324 struct btrfs_chunk *c, int nr)
325{
326 return btrfs_stripe_devid(eb, btrfs_stripe_nr(c, nr));
327}
Qu Wenruoc43c5292020-06-24 18:02:52 +0200328
Qu Wenruo6925bd22020-06-24 18:02:53 +0200329static inline void btrfs_set_stripe_devid_nr(struct extent_buffer *eb,
330 struct btrfs_chunk *c, int nr,
331 u64 val)
332{
333 btrfs_set_stripe_devid(eb, btrfs_stripe_nr(c, nr), val);
334}
Qu Wenruoc43c5292020-06-24 18:02:52 +0200335
Qu Wenruo6925bd22020-06-24 18:02:53 +0200336/* struct btrfs_block_group_item */
337BTRFS_SETGET_STACK_FUNCS(block_group_used, struct btrfs_block_group_item,
338 used, 64);
339BTRFS_SETGET_FUNCS(disk_block_group_used, struct btrfs_block_group_item,
340 used, 64);
341BTRFS_SETGET_STACK_FUNCS(block_group_chunk_objectid,
342 struct btrfs_block_group_item, chunk_objectid, 64);
Marek Behún4eff4262017-09-03 17:00:26 +0200343
Qu Wenruo6925bd22020-06-24 18:02:53 +0200344BTRFS_SETGET_FUNCS(disk_block_group_chunk_objectid,
345 struct btrfs_block_group_item, chunk_objectid, 64);
346BTRFS_SETGET_FUNCS(disk_block_group_flags,
347 struct btrfs_block_group_item, flags, 64);
348BTRFS_SETGET_STACK_FUNCS(block_group_flags,
349 struct btrfs_block_group_item, flags, 64);
350
351/* struct btrfs_free_space_info */
352BTRFS_SETGET_FUNCS(free_space_extent_count, struct btrfs_free_space_info,
353 extent_count, 32);
354BTRFS_SETGET_FUNCS(free_space_flags, struct btrfs_free_space_info, flags, 32);
355
356/* struct btrfs_inode_ref */
357BTRFS_SETGET_FUNCS(inode_ref_name_len, struct btrfs_inode_ref, name_len, 16);
358BTRFS_SETGET_STACK_FUNCS(stack_inode_ref_name_len, struct btrfs_inode_ref, name_len, 16);
359BTRFS_SETGET_FUNCS(inode_ref_index, struct btrfs_inode_ref, index, 64);
360
361/* struct btrfs_inode_extref */
362BTRFS_SETGET_FUNCS(inode_extref_parent, struct btrfs_inode_extref,
363 parent_objectid, 64);
364BTRFS_SETGET_FUNCS(inode_extref_name_len, struct btrfs_inode_extref,
365 name_len, 16);
366BTRFS_SETGET_FUNCS(inode_extref_index, struct btrfs_inode_extref, index, 64);
367
368/* struct btrfs_inode_item */
369BTRFS_SETGET_FUNCS(inode_generation, struct btrfs_inode_item, generation, 64);
370BTRFS_SETGET_FUNCS(inode_sequence, struct btrfs_inode_item, sequence, 64);
371BTRFS_SETGET_FUNCS(inode_transid, struct btrfs_inode_item, transid, 64);
372BTRFS_SETGET_FUNCS(inode_size, struct btrfs_inode_item, size, 64);
373BTRFS_SETGET_FUNCS(inode_nbytes, struct btrfs_inode_item, nbytes, 64);
374BTRFS_SETGET_FUNCS(inode_block_group, struct btrfs_inode_item, block_group, 64);
375BTRFS_SETGET_FUNCS(inode_nlink, struct btrfs_inode_item, nlink, 32);
376BTRFS_SETGET_FUNCS(inode_uid, struct btrfs_inode_item, uid, 32);
377BTRFS_SETGET_FUNCS(inode_gid, struct btrfs_inode_item, gid, 32);
378BTRFS_SETGET_FUNCS(inode_mode, struct btrfs_inode_item, mode, 32);
379BTRFS_SETGET_FUNCS(inode_rdev, struct btrfs_inode_item, rdev, 64);
380BTRFS_SETGET_FUNCS(inode_flags, struct btrfs_inode_item, flags, 64);
381
382BTRFS_SETGET_STACK_FUNCS(stack_inode_generation,
383 struct btrfs_inode_item, generation, 64);
384BTRFS_SETGET_STACK_FUNCS(stack_inode_sequence,
385 struct btrfs_inode_item, sequence, 64);
386BTRFS_SETGET_STACK_FUNCS(stack_inode_transid,
387 struct btrfs_inode_item, transid, 64);
388BTRFS_SETGET_STACK_FUNCS(stack_inode_size,
389 struct btrfs_inode_item, size, 64);
390BTRFS_SETGET_STACK_FUNCS(stack_inode_nbytes,
391 struct btrfs_inode_item, nbytes, 64);
392BTRFS_SETGET_STACK_FUNCS(stack_inode_block_group,
393 struct btrfs_inode_item, block_group, 64);
394BTRFS_SETGET_STACK_FUNCS(stack_inode_nlink,
395 struct btrfs_inode_item, nlink, 32);
396BTRFS_SETGET_STACK_FUNCS(stack_inode_uid,
397 struct btrfs_inode_item, uid, 32);
398BTRFS_SETGET_STACK_FUNCS(stack_inode_gid,
399 struct btrfs_inode_item, gid, 32);
400BTRFS_SETGET_STACK_FUNCS(stack_inode_mode,
401 struct btrfs_inode_item, mode, 32);
402BTRFS_SETGET_STACK_FUNCS(stack_inode_rdev,
403 struct btrfs_inode_item, rdev, 64);
404BTRFS_SETGET_STACK_FUNCS(stack_inode_flags,
405 struct btrfs_inode_item, flags, 64);
406
407static inline struct btrfs_timespec *
408btrfs_inode_atime(struct btrfs_inode_item *inode_item)
409{
410 unsigned long ptr = (unsigned long)inode_item;
411 ptr += offsetof(struct btrfs_inode_item, atime);
412 return (struct btrfs_timespec *)ptr;
Marek Behún4eff4262017-09-03 17:00:26 +0200413}
414
Qu Wenruo6925bd22020-06-24 18:02:53 +0200415static inline struct btrfs_timespec *
416btrfs_inode_mtime(struct btrfs_inode_item *inode_item)
Marek Behún4eff4262017-09-03 17:00:26 +0200417{
Qu Wenruo6925bd22020-06-24 18:02:53 +0200418 unsigned long ptr = (unsigned long)inode_item;
419 ptr += offsetof(struct btrfs_inode_item, mtime);
420 return (struct btrfs_timespec *)ptr;
421}
Marek Behún4eff4262017-09-03 17:00:26 +0200422
Qu Wenruo6925bd22020-06-24 18:02:53 +0200423static inline struct btrfs_timespec *
424btrfs_inode_ctime(struct btrfs_inode_item *inode_item)
425{
426 unsigned long ptr = (unsigned long)inode_item;
427 ptr += offsetof(struct btrfs_inode_item, ctime);
428 return (struct btrfs_timespec *)ptr;
429}
Marek Behún4eff4262017-09-03 17:00:26 +0200430
Qu Wenruo6925bd22020-06-24 18:02:53 +0200431static inline struct btrfs_timespec *
432btrfs_inode_otime(struct btrfs_inode_item *inode_item)
433{
434 unsigned long ptr = (unsigned long)inode_item;
435 ptr += offsetof(struct btrfs_inode_item, otime);
436 return (struct btrfs_timespec *)ptr;
437}
Marek Behún4eff4262017-09-03 17:00:26 +0200438
Qu Wenruo6925bd22020-06-24 18:02:53 +0200439BTRFS_SETGET_FUNCS(timespec_sec, struct btrfs_timespec, sec, 64);
440BTRFS_SETGET_FUNCS(timespec_nsec, struct btrfs_timespec, nsec, 32);
441BTRFS_SETGET_STACK_FUNCS(stack_timespec_sec, struct btrfs_timespec,
442 sec, 64);
443BTRFS_SETGET_STACK_FUNCS(stack_timespec_nsec, struct btrfs_timespec,
444 nsec, 32);
Pierre Bourdon9fffe322019-04-13 23:50:49 +0200445
Qu Wenruo6925bd22020-06-24 18:02:53 +0200446/* struct btrfs_dev_extent */
447BTRFS_SETGET_FUNCS(dev_extent_chunk_tree, struct btrfs_dev_extent,
448 chunk_tree, 64);
449BTRFS_SETGET_FUNCS(dev_extent_chunk_objectid, struct btrfs_dev_extent,
450 chunk_objectid, 64);
451BTRFS_SETGET_FUNCS(dev_extent_chunk_offset, struct btrfs_dev_extent,
452 chunk_offset, 64);
453BTRFS_SETGET_FUNCS(dev_extent_length, struct btrfs_dev_extent, length, 64);
454
455BTRFS_SETGET_STACK_FUNCS(stack_dev_extent_length, struct btrfs_dev_extent,
456 length, 64);
457
458static inline u8 *btrfs_dev_extent_chunk_tree_uuid(struct btrfs_dev_extent *dev)
459{
460 unsigned long ptr = offsetof(struct btrfs_dev_extent, chunk_tree_uuid);
461 return (u8 *)((unsigned long)dev + ptr);
Marek Behún4eff4262017-09-03 17:00:26 +0200462}
463
Qu Wenruo6925bd22020-06-24 18:02:53 +0200464/* struct btrfs_extent_item */
465BTRFS_SETGET_FUNCS(extent_refs, struct btrfs_extent_item, refs, 64);
466BTRFS_SETGET_STACK_FUNCS(stack_extent_refs, struct btrfs_extent_item, refs, 64);
467BTRFS_SETGET_FUNCS(extent_generation, struct btrfs_extent_item,
468 generation, 64);
469BTRFS_SETGET_FUNCS(extent_flags, struct btrfs_extent_item, flags, 64);
470BTRFS_SETGET_STACK_FUNCS(stack_extent_flags, struct btrfs_extent_item, flags, 64);
471
472BTRFS_SETGET_FUNCS(extent_refs_v0, struct btrfs_extent_item_v0, refs, 32);
473
474BTRFS_SETGET_FUNCS(tree_block_level, struct btrfs_tree_block_info, level, 8);
475
476static inline void btrfs_tree_block_key(struct extent_buffer *eb,
477 struct btrfs_tree_block_info *item,
478 struct btrfs_disk_key *key)
Marek Behún4eff4262017-09-03 17:00:26 +0200479{
Qu Wenruo6925bd22020-06-24 18:02:53 +0200480 read_eb_member(eb, item, struct btrfs_tree_block_info, key, key);
Marek Behún4eff4262017-09-03 17:00:26 +0200481}
482
Qu Wenruo6925bd22020-06-24 18:02:53 +0200483static inline void btrfs_set_tree_block_key(struct extent_buffer *eb,
484 struct btrfs_tree_block_info *item,
485 struct btrfs_disk_key *key)
Marek Behún4eff4262017-09-03 17:00:26 +0200486{
Qu Wenruo6925bd22020-06-24 18:02:53 +0200487 write_eb_member(eb, item, struct btrfs_tree_block_info, key, key);
Marek Behún4eff4262017-09-03 17:00:26 +0200488}
489
Qu Wenruo6925bd22020-06-24 18:02:53 +0200490BTRFS_SETGET_FUNCS(extent_data_ref_root, struct btrfs_extent_data_ref,
491 root, 64);
492BTRFS_SETGET_FUNCS(extent_data_ref_objectid, struct btrfs_extent_data_ref,
493 objectid, 64);
494BTRFS_SETGET_FUNCS(extent_data_ref_offset, struct btrfs_extent_data_ref,
495 offset, 64);
496BTRFS_SETGET_FUNCS(extent_data_ref_count, struct btrfs_extent_data_ref,
497 count, 32);
498
499BTRFS_SETGET_FUNCS(shared_data_ref_count, struct btrfs_shared_data_ref,
500 count, 32);
501
502BTRFS_SETGET_FUNCS(extent_inline_ref_type, struct btrfs_extent_inline_ref,
503 type, 8);
504BTRFS_SETGET_FUNCS(extent_inline_ref_offset, struct btrfs_extent_inline_ref,
505 offset, 64);
506BTRFS_SETGET_STACK_FUNCS(stack_extent_inline_ref_type,
507 struct btrfs_extent_inline_ref, type, 8);
508BTRFS_SETGET_STACK_FUNCS(stack_extent_inline_ref_offset,
509 struct btrfs_extent_inline_ref, offset, 64);
510
511static inline u32 btrfs_extent_inline_ref_size(int type)
Marek Behún4eff4262017-09-03 17:00:26 +0200512{
Qu Wenruo6925bd22020-06-24 18:02:53 +0200513 if (type == BTRFS_TREE_BLOCK_REF_KEY ||
514 type == BTRFS_SHARED_BLOCK_REF_KEY)
515 return sizeof(struct btrfs_extent_inline_ref);
516 if (type == BTRFS_SHARED_DATA_REF_KEY)
517 return sizeof(struct btrfs_shared_data_ref) +
518 sizeof(struct btrfs_extent_inline_ref);
519 if (type == BTRFS_EXTENT_DATA_REF_KEY)
520 return sizeof(struct btrfs_extent_data_ref) +
521 offsetof(struct btrfs_extent_inline_ref, offset);
522 BUG();
523 return 0;
Marek Behún4eff4262017-09-03 17:00:26 +0200524}
525
Qu Wenruo6925bd22020-06-24 18:02:53 +0200526BTRFS_SETGET_FUNCS(ref_root_v0, struct btrfs_extent_ref_v0, root, 64);
527BTRFS_SETGET_FUNCS(ref_generation_v0, struct btrfs_extent_ref_v0,
528 generation, 64);
529BTRFS_SETGET_FUNCS(ref_objectid_v0, struct btrfs_extent_ref_v0, objectid, 64);
530BTRFS_SETGET_FUNCS(ref_count_v0, struct btrfs_extent_ref_v0, count, 32);
Marek Behún4eff4262017-09-03 17:00:26 +0200531
Qu Wenruo6925bd22020-06-24 18:02:53 +0200532/* struct btrfs_node */
533BTRFS_SETGET_FUNCS(key_blockptr, struct btrfs_key_ptr, blockptr, 64);
534BTRFS_SETGET_FUNCS(key_generation, struct btrfs_key_ptr, generation, 64);
Marek Behún4eff4262017-09-03 17:00:26 +0200535
Qu Wenruo6925bd22020-06-24 18:02:53 +0200536static inline u64 btrfs_node_blockptr(struct extent_buffer *eb, int nr)
537{
538 unsigned long ptr;
539 ptr = offsetof(struct btrfs_node, ptrs) +
540 sizeof(struct btrfs_key_ptr) * nr;
541 return btrfs_key_blockptr(eb, (struct btrfs_key_ptr *)ptr);
542}
543
544static inline void btrfs_set_node_blockptr(struct extent_buffer *eb,
545 int nr, u64 val)
546{
547 unsigned long ptr;
548 ptr = offsetof(struct btrfs_node, ptrs) +
549 sizeof(struct btrfs_key_ptr) * nr;
550 btrfs_set_key_blockptr(eb, (struct btrfs_key_ptr *)ptr, val);
551}
552
553static inline u64 btrfs_node_ptr_generation(struct extent_buffer *eb, int nr)
554{
555 unsigned long ptr;
556 ptr = offsetof(struct btrfs_node, ptrs) +
557 sizeof(struct btrfs_key_ptr) * nr;
558 return btrfs_key_generation(eb, (struct btrfs_key_ptr *)ptr);
559}
560
561static inline void btrfs_set_node_ptr_generation(struct extent_buffer *eb,
562 int nr, u64 val)
563{
564 unsigned long ptr;
565 ptr = offsetof(struct btrfs_node, ptrs) +
566 sizeof(struct btrfs_key_ptr) * nr;
567 btrfs_set_key_generation(eb, (struct btrfs_key_ptr *)ptr, val);
568}
569
570static inline unsigned long btrfs_node_key_ptr_offset(int nr)
571{
572 return offsetof(struct btrfs_node, ptrs) +
573 sizeof(struct btrfs_key_ptr) * nr;
574}
575
576static inline void btrfs_node_key(struct extent_buffer *eb,
577 struct btrfs_disk_key *disk_key, int nr)
578{
579 unsigned long ptr;
580 ptr = btrfs_node_key_ptr_offset(nr);
581 read_eb_member(eb, (struct btrfs_key_ptr *)ptr,
582 struct btrfs_key_ptr, key, disk_key);
583}
584
585static inline void btrfs_set_node_key(struct extent_buffer *eb,
586 struct btrfs_disk_key *disk_key, int nr)
587{
588 unsigned long ptr;
589 ptr = btrfs_node_key_ptr_offset(nr);
590 write_eb_member(eb, (struct btrfs_key_ptr *)ptr,
591 struct btrfs_key_ptr, key, disk_key);
592}
593
594/* struct btrfs_item */
595BTRFS_SETGET_FUNCS(item_offset, struct btrfs_item, offset, 32);
596BTRFS_SETGET_FUNCS(item_size, struct btrfs_item, size, 32);
597
598static inline unsigned long btrfs_item_nr_offset(int nr)
599{
600 return offsetof(struct btrfs_leaf, items) +
601 sizeof(struct btrfs_item) * nr;
602}
603
604static inline struct btrfs_item *btrfs_item_nr(int nr)
605{
606 return (struct btrfs_item *)btrfs_item_nr_offset(nr);
607}
608
609static inline u32 btrfs_item_end(struct extent_buffer *eb,
610 struct btrfs_item *item)
611{
612 return btrfs_item_offset(eb, item) + btrfs_item_size(eb, item);
613}
614
615static inline u32 btrfs_item_end_nr(struct extent_buffer *eb, int nr)
616{
617 return btrfs_item_end(eb, btrfs_item_nr(nr));
618}
619
620static inline u32 btrfs_item_offset_nr(const struct extent_buffer *eb, int nr)
621{
622 return btrfs_item_offset(eb, btrfs_item_nr(nr));
623}
624
625static inline u32 btrfs_item_size_nr(struct extent_buffer *eb, int nr)
626{
627 return btrfs_item_size(eb, btrfs_item_nr(nr));
628}
629
630static inline void btrfs_item_key(struct extent_buffer *eb,
631 struct btrfs_disk_key *disk_key, int nr)
632{
633 struct btrfs_item *item = btrfs_item_nr(nr);
634 read_eb_member(eb, item, struct btrfs_item, key, disk_key);
635}
636
637static inline void btrfs_set_item_key(struct extent_buffer *eb,
638 struct btrfs_disk_key *disk_key, int nr)
639{
640 struct btrfs_item *item = btrfs_item_nr(nr);
641 write_eb_member(eb, item, struct btrfs_item, key, disk_key);
642}
643
644BTRFS_SETGET_FUNCS(dir_log_end, struct btrfs_dir_log_item, end, 64);
645
646/*
647 * struct btrfs_root_ref
648 */
649BTRFS_SETGET_FUNCS(root_ref_dirid, struct btrfs_root_ref, dirid, 64);
650BTRFS_SETGET_FUNCS(root_ref_sequence, struct btrfs_root_ref, sequence, 64);
651BTRFS_SETGET_FUNCS(root_ref_name_len, struct btrfs_root_ref, name_len, 16);
652
653BTRFS_SETGET_STACK_FUNCS(stack_root_ref_dirid, struct btrfs_root_ref, dirid, 64);
654BTRFS_SETGET_STACK_FUNCS(stack_root_ref_sequence, struct btrfs_root_ref, sequence, 64);
655BTRFS_SETGET_STACK_FUNCS(stack_root_ref_name_len, struct btrfs_root_ref, name_len, 16);
656
657/* struct btrfs_dir_item */
658BTRFS_SETGET_FUNCS(dir_data_len, struct btrfs_dir_item, data_len, 16);
659BTRFS_SETGET_FUNCS(dir_type, struct btrfs_dir_item, type, 8);
660BTRFS_SETGET_FUNCS(dir_name_len, struct btrfs_dir_item, name_len, 16);
661BTRFS_SETGET_FUNCS(dir_transid, struct btrfs_dir_item, transid, 64);
662
663BTRFS_SETGET_STACK_FUNCS(stack_dir_data_len, struct btrfs_dir_item, data_len, 16);
664BTRFS_SETGET_STACK_FUNCS(stack_dir_type, struct btrfs_dir_item, type, 8);
665BTRFS_SETGET_STACK_FUNCS(stack_dir_name_len, struct btrfs_dir_item, name_len, 16);
666BTRFS_SETGET_STACK_FUNCS(stack_dir_transid, struct btrfs_dir_item, transid, 64);
667
668static inline void btrfs_dir_item_key(struct extent_buffer *eb,
669 struct btrfs_dir_item *item,
670 struct btrfs_disk_key *key)
671{
672 read_eb_member(eb, item, struct btrfs_dir_item, location, key);
673}
674
675static inline void btrfs_set_dir_item_key(struct extent_buffer *eb,
676 struct btrfs_dir_item *item,
677 struct btrfs_disk_key *key)
678{
679 write_eb_member(eb, item, struct btrfs_dir_item, location, key);
680}
681
682/* struct btrfs_free_space_header */
683BTRFS_SETGET_FUNCS(free_space_entries, struct btrfs_free_space_header,
684 num_entries, 64);
685BTRFS_SETGET_FUNCS(free_space_bitmaps, struct btrfs_free_space_header,
686 num_bitmaps, 64);
687BTRFS_SETGET_FUNCS(free_space_generation, struct btrfs_free_space_header,
688 generation, 64);
689
690static inline void btrfs_free_space_key(struct extent_buffer *eb,
691 struct btrfs_free_space_header *h,
692 struct btrfs_disk_key *key)
693{
694 read_eb_member(eb, h, struct btrfs_free_space_header, location, key);
695}
696
697static inline void btrfs_set_free_space_key(struct extent_buffer *eb,
698 struct btrfs_free_space_header *h,
699 struct btrfs_disk_key *key)
700{
701 write_eb_member(eb, h, struct btrfs_free_space_header, location, key);
702}
703
704/* struct btrfs_disk_key */
705BTRFS_SETGET_STACK_FUNCS(disk_key_objectid, struct btrfs_disk_key,
706 objectid, 64);
707BTRFS_SETGET_STACK_FUNCS(disk_key_offset, struct btrfs_disk_key, offset, 64);
708BTRFS_SETGET_STACK_FUNCS(disk_key_type, struct btrfs_disk_key, type, 8);
709
710static inline void btrfs_disk_key_to_cpu(struct btrfs_key *cpu,
711 struct btrfs_disk_key *disk)
712{
713 cpu->offset = le64_to_cpu(disk->offset);
714 cpu->type = disk->type;
715 cpu->objectid = le64_to_cpu(disk->objectid);
716}
717
718static inline void btrfs_cpu_key_to_disk(struct btrfs_disk_key *disk,
719 const struct btrfs_key *cpu)
720{
721 disk->offset = cpu_to_le64(cpu->offset);
722 disk->type = cpu->type;
723 disk->objectid = cpu_to_le64(cpu->objectid);
724}
725
726static inline void btrfs_node_key_to_cpu(struct extent_buffer *eb,
727 struct btrfs_key *key, int nr)
728{
729 struct btrfs_disk_key disk_key;
730 btrfs_node_key(eb, &disk_key, nr);
731 btrfs_disk_key_to_cpu(key, &disk_key);
732}
733
734static inline void btrfs_item_key_to_cpu(struct extent_buffer *eb,
735 struct btrfs_key *key, int nr)
736{
737 struct btrfs_disk_key disk_key;
738 btrfs_item_key(eb, &disk_key, nr);
739 btrfs_disk_key_to_cpu(key, &disk_key);
740}
741
742static inline void btrfs_dir_item_key_to_cpu(struct extent_buffer *eb,
743 struct btrfs_dir_item *item,
744 struct btrfs_key *key)
745{
746 struct btrfs_disk_key disk_key;
747 btrfs_dir_item_key(eb, item, &disk_key);
748 btrfs_disk_key_to_cpu(key, &disk_key);
749}
750
751/* struct btrfs_header */
752BTRFS_SETGET_HEADER_FUNCS(header_bytenr, struct btrfs_header, bytenr, 64);
753BTRFS_SETGET_HEADER_FUNCS(header_generation, struct btrfs_header,
754 generation, 64);
755BTRFS_SETGET_HEADER_FUNCS(header_owner, struct btrfs_header, owner, 64);
756BTRFS_SETGET_HEADER_FUNCS(header_nritems, struct btrfs_header, nritems, 32);
757BTRFS_SETGET_HEADER_FUNCS(header_flags, struct btrfs_header, flags, 64);
758BTRFS_SETGET_HEADER_FUNCS(header_level, struct btrfs_header, level, 8);
759BTRFS_SETGET_STACK_FUNCS(stack_header_bytenr, struct btrfs_header, bytenr, 64);
760BTRFS_SETGET_STACK_FUNCS(stack_header_nritems, struct btrfs_header, nritems,
761 32);
762BTRFS_SETGET_STACK_FUNCS(stack_header_owner, struct btrfs_header, owner, 64);
763BTRFS_SETGET_STACK_FUNCS(stack_header_generation, struct btrfs_header,
764 generation, 64);
765
766static inline int btrfs_header_flag(struct extent_buffer *eb, u64 flag)
767{
768 return (btrfs_header_flags(eb) & flag) == flag;
769}
770
771static inline int btrfs_set_header_flag(struct extent_buffer *eb, u64 flag)
772{
773 u64 flags = btrfs_header_flags(eb);
774 btrfs_set_header_flags(eb, flags | flag);
775 return (flags & flag) == flag;
776}
777
778static inline int btrfs_clear_header_flag(struct extent_buffer *eb, u64 flag)
779{
780 u64 flags = btrfs_header_flags(eb);
781 btrfs_set_header_flags(eb, flags & ~flag);
782 return (flags & flag) == flag;
783}
784
785static inline int btrfs_header_backref_rev(struct extent_buffer *eb)
786{
787 u64 flags = btrfs_header_flags(eb);
788 return flags >> BTRFS_BACKREF_REV_SHIFT;
789}
790
791static inline void btrfs_set_header_backref_rev(struct extent_buffer *eb,
792 int rev)
793{
794 u64 flags = btrfs_header_flags(eb);
795 flags &= ~BTRFS_BACKREF_REV_MASK;
796 flags |= (u64)rev << BTRFS_BACKREF_REV_SHIFT;
797 btrfs_set_header_flags(eb, flags);
798}
799
800static inline unsigned long btrfs_header_fsid(void)
801{
802 return offsetof(struct btrfs_header, fsid);
803}
804
805static inline unsigned long btrfs_header_chunk_tree_uuid(struct extent_buffer *eb)
806{
807 return offsetof(struct btrfs_header, chunk_tree_uuid);
808}
809
810static inline u8 *btrfs_header_csum(struct extent_buffer *eb)
811{
812 unsigned long ptr = offsetof(struct btrfs_header, csum);
813 return (u8 *)ptr;
814}
815
816static inline int btrfs_is_leaf(struct extent_buffer *eb)
817{
818 return (btrfs_header_level(eb) == 0);
819}
820
821/* struct btrfs_root_item */
822BTRFS_SETGET_FUNCS(disk_root_generation, struct btrfs_root_item,
823 generation, 64);
824BTRFS_SETGET_FUNCS(disk_root_refs, struct btrfs_root_item, refs, 32);
825BTRFS_SETGET_FUNCS(disk_root_bytenr, struct btrfs_root_item, bytenr, 64);
826BTRFS_SETGET_FUNCS(disk_root_level, struct btrfs_root_item, level, 8);
827
828BTRFS_SETGET_STACK_FUNCS(root_generation, struct btrfs_root_item,
829 generation, 64);
830BTRFS_SETGET_STACK_FUNCS(root_bytenr, struct btrfs_root_item, bytenr, 64);
831BTRFS_SETGET_STACK_FUNCS(root_level, struct btrfs_root_item, level, 8);
832BTRFS_SETGET_STACK_FUNCS(root_dirid, struct btrfs_root_item, root_dirid, 64);
833BTRFS_SETGET_STACK_FUNCS(root_refs, struct btrfs_root_item, refs, 32);
834BTRFS_SETGET_STACK_FUNCS(root_flags, struct btrfs_root_item, flags, 64);
835BTRFS_SETGET_STACK_FUNCS(root_used, struct btrfs_root_item, bytes_used, 64);
836BTRFS_SETGET_STACK_FUNCS(root_limit, struct btrfs_root_item, byte_limit, 64);
837BTRFS_SETGET_STACK_FUNCS(root_last_snapshot, struct btrfs_root_item,
838 last_snapshot, 64);
839BTRFS_SETGET_STACK_FUNCS(root_generation_v2, struct btrfs_root_item,
840 generation_v2, 64);
841BTRFS_SETGET_STACK_FUNCS(root_ctransid, struct btrfs_root_item,
842 ctransid, 64);
843BTRFS_SETGET_STACK_FUNCS(root_otransid, struct btrfs_root_item,
844 otransid, 64);
845BTRFS_SETGET_STACK_FUNCS(root_stransid, struct btrfs_root_item,
846 stransid, 64);
847BTRFS_SETGET_STACK_FUNCS(root_rtransid, struct btrfs_root_item,
848 rtransid, 64);
849
850static inline struct btrfs_timespec* btrfs_root_ctime(
851 struct btrfs_root_item *root_item)
852{
853 unsigned long ptr = (unsigned long)root_item;
854 ptr += offsetof(struct btrfs_root_item, ctime);
855 return (struct btrfs_timespec *)ptr;
856}
857
858static inline struct btrfs_timespec* btrfs_root_otime(
859 struct btrfs_root_item *root_item)
860{
861 unsigned long ptr = (unsigned long)root_item;
862 ptr += offsetof(struct btrfs_root_item, otime);
863 return (struct btrfs_timespec *)ptr;
864}
865
866static inline struct btrfs_timespec* btrfs_root_stime(
867 struct btrfs_root_item *root_item)
868{
869 unsigned long ptr = (unsigned long)root_item;
870 ptr += offsetof(struct btrfs_root_item, stime);
871 return (struct btrfs_timespec *)ptr;
872}
873
874static inline struct btrfs_timespec* btrfs_root_rtime(
875 struct btrfs_root_item *root_item)
876{
877 unsigned long ptr = (unsigned long)root_item;
878 ptr += offsetof(struct btrfs_root_item, rtime);
879 return (struct btrfs_timespec *)ptr;
880}
881
882/* struct btrfs_root_backup */
883BTRFS_SETGET_STACK_FUNCS(backup_tree_root, struct btrfs_root_backup,
884 tree_root, 64);
885BTRFS_SETGET_STACK_FUNCS(backup_tree_root_gen, struct btrfs_root_backup,
886 tree_root_gen, 64);
887BTRFS_SETGET_STACK_FUNCS(backup_tree_root_level, struct btrfs_root_backup,
888 tree_root_level, 8);
889
890BTRFS_SETGET_STACK_FUNCS(backup_chunk_root, struct btrfs_root_backup,
891 chunk_root, 64);
892BTRFS_SETGET_STACK_FUNCS(backup_chunk_root_gen, struct btrfs_root_backup,
893 chunk_root_gen, 64);
894BTRFS_SETGET_STACK_FUNCS(backup_chunk_root_level, struct btrfs_root_backup,
895 chunk_root_level, 8);
896
897BTRFS_SETGET_STACK_FUNCS(backup_extent_root, struct btrfs_root_backup,
898 extent_root, 64);
899BTRFS_SETGET_STACK_FUNCS(backup_extent_root_gen, struct btrfs_root_backup,
900 extent_root_gen, 64);
901BTRFS_SETGET_STACK_FUNCS(backup_extent_root_level, struct btrfs_root_backup,
902 extent_root_level, 8);
903
904BTRFS_SETGET_STACK_FUNCS(backup_fs_root, struct btrfs_root_backup,
905 fs_root, 64);
906BTRFS_SETGET_STACK_FUNCS(backup_fs_root_gen, struct btrfs_root_backup,
907 fs_root_gen, 64);
908BTRFS_SETGET_STACK_FUNCS(backup_fs_root_level, struct btrfs_root_backup,
909 fs_root_level, 8);
910
911BTRFS_SETGET_STACK_FUNCS(backup_dev_root, struct btrfs_root_backup,
912 dev_root, 64);
913BTRFS_SETGET_STACK_FUNCS(backup_dev_root_gen, struct btrfs_root_backup,
914 dev_root_gen, 64);
915BTRFS_SETGET_STACK_FUNCS(backup_dev_root_level, struct btrfs_root_backup,
916 dev_root_level, 8);
917
918BTRFS_SETGET_STACK_FUNCS(backup_csum_root, struct btrfs_root_backup,
919 csum_root, 64);
920BTRFS_SETGET_STACK_FUNCS(backup_csum_root_gen, struct btrfs_root_backup,
921 csum_root_gen, 64);
922BTRFS_SETGET_STACK_FUNCS(backup_csum_root_level, struct btrfs_root_backup,
923 csum_root_level, 8);
924BTRFS_SETGET_STACK_FUNCS(backup_total_bytes, struct btrfs_root_backup,
925 total_bytes, 64);
926BTRFS_SETGET_STACK_FUNCS(backup_bytes_used, struct btrfs_root_backup,
927 bytes_used, 64);
928BTRFS_SETGET_STACK_FUNCS(backup_num_devices, struct btrfs_root_backup,
929 num_devices, 64);
Qu Wenruo1a618082020-06-24 18:02:49 +0200930
931/* struct btrfs_super_block */
932
933BTRFS_SETGET_STACK_FUNCS(super_bytenr, struct btrfs_super_block, bytenr, 64);
934BTRFS_SETGET_STACK_FUNCS(super_flags, struct btrfs_super_block, flags, 64);
935BTRFS_SETGET_STACK_FUNCS(super_generation, struct btrfs_super_block,
936 generation, 64);
937BTRFS_SETGET_STACK_FUNCS(super_root, struct btrfs_super_block, root, 64);
938BTRFS_SETGET_STACK_FUNCS(super_sys_array_size,
939 struct btrfs_super_block, sys_chunk_array_size, 32);
940BTRFS_SETGET_STACK_FUNCS(super_chunk_root_generation,
941 struct btrfs_super_block, chunk_root_generation, 64);
942BTRFS_SETGET_STACK_FUNCS(super_root_level, struct btrfs_super_block,
943 root_level, 8);
944BTRFS_SETGET_STACK_FUNCS(super_chunk_root, struct btrfs_super_block,
945 chunk_root, 64);
946BTRFS_SETGET_STACK_FUNCS(super_chunk_root_level, struct btrfs_super_block,
947 chunk_root_level, 8);
948BTRFS_SETGET_STACK_FUNCS(super_log_root, struct btrfs_super_block,
949 log_root, 64);
950BTRFS_SETGET_STACK_FUNCS(super_log_root_transid, struct btrfs_super_block,
951 log_root_transid, 64);
952BTRFS_SETGET_STACK_FUNCS(super_log_root_level, struct btrfs_super_block,
953 log_root_level, 8);
954BTRFS_SETGET_STACK_FUNCS(super_total_bytes, struct btrfs_super_block,
955 total_bytes, 64);
956BTRFS_SETGET_STACK_FUNCS(super_bytes_used, struct btrfs_super_block,
957 bytes_used, 64);
958BTRFS_SETGET_STACK_FUNCS(super_sectorsize, struct btrfs_super_block,
959 sectorsize, 32);
960BTRFS_SETGET_STACK_FUNCS(super_nodesize, struct btrfs_super_block,
961 nodesize, 32);
962BTRFS_SETGET_STACK_FUNCS(super_stripesize, struct btrfs_super_block,
963 stripesize, 32);
964BTRFS_SETGET_STACK_FUNCS(super_root_dir, struct btrfs_super_block,
965 root_dir_objectid, 64);
966BTRFS_SETGET_STACK_FUNCS(super_num_devices, struct btrfs_super_block,
967 num_devices, 64);
968BTRFS_SETGET_STACK_FUNCS(super_compat_flags, struct btrfs_super_block,
969 compat_flags, 64);
970BTRFS_SETGET_STACK_FUNCS(super_compat_ro_flags, struct btrfs_super_block,
971 compat_ro_flags, 64);
972BTRFS_SETGET_STACK_FUNCS(super_incompat_flags, struct btrfs_super_block,
973 incompat_flags, 64);
974BTRFS_SETGET_STACK_FUNCS(super_csum_type, struct btrfs_super_block,
975 csum_type, 16);
976BTRFS_SETGET_STACK_FUNCS(super_cache_generation, struct btrfs_super_block,
977 cache_generation, 64);
978BTRFS_SETGET_STACK_FUNCS(super_uuid_tree_generation, struct btrfs_super_block,
979 uuid_tree_generation, 64);
980BTRFS_SETGET_STACK_FUNCS(super_magic, struct btrfs_super_block, magic, 64);
981
Qu Wenruo6925bd22020-06-24 18:02:53 +0200982static inline unsigned long btrfs_leaf_data(struct extent_buffer *l)
983{
984 return offsetof(struct btrfs_leaf, items);
985}
986
987/* struct btrfs_file_extent_item */
988BTRFS_SETGET_FUNCS(file_extent_type, struct btrfs_file_extent_item, type, 8);
989BTRFS_SETGET_STACK_FUNCS(stack_file_extent_type, struct btrfs_file_extent_item, type, 8);
990
991static inline unsigned long btrfs_file_extent_inline_start(struct
992 btrfs_file_extent_item *e)
993{
994 unsigned long offset = (unsigned long)e;
995 offset += offsetof(struct btrfs_file_extent_item, disk_bytenr);
996 return offset;
997}
998
999static inline u32 btrfs_file_extent_calc_inline_size(u32 datasize)
1000{
1001 return offsetof(struct btrfs_file_extent_item, disk_bytenr) + datasize;
1002}
1003
1004BTRFS_SETGET_FUNCS(file_extent_disk_bytenr, struct btrfs_file_extent_item,
1005 disk_bytenr, 64);
1006BTRFS_SETGET_STACK_FUNCS(stack_file_extent_disk_bytenr, struct btrfs_file_extent_item,
1007 disk_bytenr, 64);
1008BTRFS_SETGET_FUNCS(file_extent_generation, struct btrfs_file_extent_item,
1009 generation, 64);
1010BTRFS_SETGET_STACK_FUNCS(stack_file_extent_generation, struct btrfs_file_extent_item,
1011 generation, 64);
1012BTRFS_SETGET_FUNCS(file_extent_disk_num_bytes, struct btrfs_file_extent_item,
1013 disk_num_bytes, 64);
1014BTRFS_SETGET_FUNCS(file_extent_offset, struct btrfs_file_extent_item,
1015 offset, 64);
1016BTRFS_SETGET_STACK_FUNCS(stack_file_extent_offset, struct btrfs_file_extent_item,
1017 offset, 64);
1018BTRFS_SETGET_FUNCS(file_extent_num_bytes, struct btrfs_file_extent_item,
1019 num_bytes, 64);
1020BTRFS_SETGET_STACK_FUNCS(stack_file_extent_num_bytes, struct btrfs_file_extent_item,
1021 num_bytes, 64);
1022BTRFS_SETGET_FUNCS(file_extent_ram_bytes, struct btrfs_file_extent_item,
1023 ram_bytes, 64);
1024BTRFS_SETGET_STACK_FUNCS(stack_file_extent_ram_bytes, struct btrfs_file_extent_item,
1025 ram_bytes, 64);
1026BTRFS_SETGET_FUNCS(file_extent_compression, struct btrfs_file_extent_item,
1027 compression, 8);
1028BTRFS_SETGET_STACK_FUNCS(stack_file_extent_compression, struct btrfs_file_extent_item,
1029 compression, 8);
1030BTRFS_SETGET_FUNCS(file_extent_encryption, struct btrfs_file_extent_item,
1031 encryption, 8);
1032BTRFS_SETGET_FUNCS(file_extent_other_encoding, struct btrfs_file_extent_item,
1033 other_encoding, 16);
1034
1035/* btrfs_qgroup_status_item */
1036BTRFS_SETGET_FUNCS(qgroup_status_version, struct btrfs_qgroup_status_item,
1037 version, 64);
1038BTRFS_SETGET_FUNCS(qgroup_status_generation, struct btrfs_qgroup_status_item,
1039 generation, 64);
1040BTRFS_SETGET_FUNCS(qgroup_status_flags, struct btrfs_qgroup_status_item,
1041 flags, 64);
1042BTRFS_SETGET_FUNCS(qgroup_status_rescan, struct btrfs_qgroup_status_item,
1043 rescan, 64);
1044
1045BTRFS_SETGET_STACK_FUNCS(stack_qgroup_status_version,
1046 struct btrfs_qgroup_status_item, version, 64);
1047BTRFS_SETGET_STACK_FUNCS(stack_qgroup_status_generation,
1048 struct btrfs_qgroup_status_item, generation, 64);
1049BTRFS_SETGET_STACK_FUNCS(stack_qgroup_status_flags,
1050 struct btrfs_qgroup_status_item, flags, 64);
1051BTRFS_SETGET_STACK_FUNCS(stack_qgroup_status_rescan,
1052 struct btrfs_qgroup_status_item, rescan, 64);
1053
1054/* btrfs_qgroup_info_item */
1055BTRFS_SETGET_FUNCS(qgroup_info_generation, struct btrfs_qgroup_info_item,
1056 generation, 64);
1057BTRFS_SETGET_FUNCS(qgroup_info_referenced, struct btrfs_qgroup_info_item,
1058 rfer, 64);
1059BTRFS_SETGET_FUNCS(qgroup_info_referenced_compressed,
1060 struct btrfs_qgroup_info_item, rfer_cmpr, 64);
1061BTRFS_SETGET_FUNCS(qgroup_info_exclusive, struct btrfs_qgroup_info_item,
1062 excl, 64);
1063BTRFS_SETGET_FUNCS(qgroup_info_exclusive_compressed,
1064 struct btrfs_qgroup_info_item, excl_cmpr, 64);
1065
1066BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_generation,
1067 struct btrfs_qgroup_info_item, generation, 64);
1068BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_referenced,
1069 struct btrfs_qgroup_info_item, rfer, 64);
1070BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_referenced_compressed,
1071 struct btrfs_qgroup_info_item, rfer_cmpr, 64);
1072BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_exclusive,
1073 struct btrfs_qgroup_info_item, excl, 64);
1074BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_exclusive_compressed,
1075 struct btrfs_qgroup_info_item, excl_cmpr, 64);
1076
1077/* btrfs_qgroup_limit_item */
1078BTRFS_SETGET_FUNCS(qgroup_limit_flags, struct btrfs_qgroup_limit_item,
1079 flags, 64);
1080BTRFS_SETGET_FUNCS(qgroup_limit_max_referenced, struct btrfs_qgroup_limit_item,
1081 max_rfer, 64);
1082BTRFS_SETGET_FUNCS(qgroup_limit_max_exclusive, struct btrfs_qgroup_limit_item,
1083 max_excl, 64);
1084BTRFS_SETGET_FUNCS(qgroup_limit_rsv_referenced, struct btrfs_qgroup_limit_item,
1085 rsv_rfer, 64);
1086BTRFS_SETGET_FUNCS(qgroup_limit_rsv_exclusive, struct btrfs_qgroup_limit_item,
1087 rsv_excl, 64);
1088
1089BTRFS_SETGET_STACK_FUNCS(stack_qgroup_limit_flags,
1090 struct btrfs_qgroup_limit_item, flags, 64);
1091BTRFS_SETGET_STACK_FUNCS(stack_qgroup_limit_max_referenced,
1092 struct btrfs_qgroup_limit_item, max_rfer, 64);
1093BTRFS_SETGET_STACK_FUNCS(stack_qgroup_limit_max_exclusive,
1094 struct btrfs_qgroup_limit_item, max_excl, 64);
1095BTRFS_SETGET_STACK_FUNCS(stack_qgroup_limit_rsv_referenced,
1096 struct btrfs_qgroup_limit_item, rsv_rfer, 64);
1097BTRFS_SETGET_STACK_FUNCS(stack_qgroup_limit_rsv_exclusive,
1098 struct btrfs_qgroup_limit_item, rsv_excl, 64);
1099
1100/* btrfs_balance_item */
1101BTRFS_SETGET_FUNCS(balance_item_flags, struct btrfs_balance_item, flags, 64);
1102
1103static inline struct btrfs_disk_balance_args* btrfs_balance_item_data(
1104 struct extent_buffer *eb, struct btrfs_balance_item *bi)
1105{
1106 unsigned long offset = (unsigned long)bi;
1107 struct btrfs_balance_item *p;
1108 p = (struct btrfs_balance_item *)(eb->data + offset);
1109 return &p->data;
1110}
1111
1112static inline struct btrfs_disk_balance_args* btrfs_balance_item_meta(
1113 struct extent_buffer *eb, struct btrfs_balance_item *bi)
1114{
1115 unsigned long offset = (unsigned long)bi;
1116 struct btrfs_balance_item *p;
1117 p = (struct btrfs_balance_item *)(eb->data + offset);
1118 return &p->meta;
1119}
1120
1121static inline struct btrfs_disk_balance_args* btrfs_balance_item_sys(
1122 struct extent_buffer *eb, struct btrfs_balance_item *bi)
1123{
1124 unsigned long offset = (unsigned long)bi;
1125 struct btrfs_balance_item *p;
1126 p = (struct btrfs_balance_item *)(eb->data + offset);
1127 return &p->sys;
1128}
1129
1130static inline u64 btrfs_dev_stats_value(const struct extent_buffer *eb,
1131 const struct btrfs_dev_stats_item *ptr,
1132 int index)
1133{
1134 u64 val;
1135
1136 read_extent_buffer(eb, &val,
1137 offsetof(struct btrfs_dev_stats_item, values) +
1138 ((unsigned long)ptr) + (index * sizeof(u64)),
1139 sizeof(val));
1140 return val;
1141}
1142
1143/*
1144 * this returns the number of bytes used by the item on disk, minus the
1145 * size of any extent headers. If a file is compressed on disk, this is
1146 * the compressed size
1147 */
1148static inline u32 btrfs_file_extent_inline_item_len(struct extent_buffer *eb,
1149 struct btrfs_item *e)
1150{
1151 unsigned long offset;
1152 offset = offsetof(struct btrfs_file_extent_item, disk_bytenr);
1153 return btrfs_item_size(eb, e) - offset;
1154}
1155
1156#define btrfs_fs_incompat(fs_info, opt) \
1157 __btrfs_fs_incompat((fs_info), BTRFS_FEATURE_INCOMPAT_##opt)
1158
1159static inline bool __btrfs_fs_incompat(struct btrfs_fs_info *fs_info, u64 flag)
1160{
1161 struct btrfs_super_block *disk_super;
1162 disk_super = fs_info->super_copy;
1163 return !!(btrfs_super_incompat_flags(disk_super) & flag);
1164}
1165
1166#define btrfs_fs_compat_ro(fs_info, opt) \
1167 __btrfs_fs_compat_ro((fs_info), BTRFS_FEATURE_COMPAT_RO_##opt)
1168
1169static inline int __btrfs_fs_compat_ro(struct btrfs_fs_info *fs_info, u64 flag)
1170{
1171 struct btrfs_super_block *disk_super;
1172 disk_super = fs_info->super_copy;
1173 return !!(btrfs_super_compat_ro_flags(disk_super) & flag);
1174}
1175
1176/* helper function to cast into the data area of the leaf. */
1177#define btrfs_item_ptr(leaf, slot, type) \
1178 ((type *)(btrfs_leaf_data(leaf) + \
1179 btrfs_item_offset_nr(leaf, slot)))
1180
1181#define btrfs_item_ptr_offset(leaf, slot) \
1182 ((unsigned long)(btrfs_leaf_data(leaf) + \
1183 btrfs_item_offset_nr(leaf, slot)))
1184
1185static inline u64 btrfs_name_hash(const char *name, int len)
1186{
1187 return (u64)crc32c((u32)~1, (u8 *)name, len);
1188}
1189
1190/*
1191 * Figure the key offset of an extended inode ref
1192 */
1193static inline u64 btrfs_extref_hash(u64 parent_objectid, const char *name,
1194 int len)
1195{
1196 return crc32(parent_objectid, (u8 *)name, len);
1197}
1198
1199union btrfs_tree_node {
1200 struct btrfs_header header;
1201 struct btrfs_leaf leaf;
1202 struct btrfs_node node;
1203};
1204
Qu Wenruo6925bd22020-06-24 18:02:53 +02001205#define btrfs_path_item_ptr(p,t) \
1206 ((t *) btrfs_path_leaf_data((p)))
1207
1208u16 btrfs_super_csum_size(const struct btrfs_super_block *s);
1209const char *btrfs_super_csum_name(u16 csum_type);
1210u16 btrfs_csum_type_size(u16 csum_type);
1211size_t btrfs_super_num_csums(void);
1212
Qu Wenruo1d5a7b72020-06-24 18:03:01 +02001213/* root-tree.c */
1214int btrfs_find_last_root(struct btrfs_root *root, u64 objectid,
1215 struct btrfs_root_item *item, struct btrfs_key *key);
1216
Qu Wenruodca22af2020-06-24 18:03:02 +02001217/* dir-item.c */
1218struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans,
1219 struct btrfs_root *root,
1220 struct btrfs_path *path, u64 dir,
1221 const char *name, int name_len,
1222 int mod);
Qu Wenruo348848a2020-06-24 18:03:06 +02001223typedef int (*btrfs_iter_dir_callback_t)(struct btrfs_root *root,
1224 struct extent_buffer *eb,
1225 struct btrfs_dir_item *di);
1226int btrfs_iter_dir(struct btrfs_root *root, u64 ino,
1227 btrfs_iter_dir_callback_t callback);
Qu Wenruoe565bd42020-06-24 18:03:05 +02001228/* inode.c */
1229int btrfs_lookup_path(struct btrfs_root *root, u64 ino, const char *filename,
1230 struct btrfs_root **root_ret, u64 *ino_ret,
1231 u8 *type_ret, int symlink_limit);
Qu Wenruo83b04df2020-06-24 18:03:09 +02001232int btrfs_read_extent_inline(struct btrfs_path *path,
1233 struct btrfs_file_extent_item *fi, char *dest);
1234int btrfs_read_extent_reg(struct btrfs_path *path,
1235 struct btrfs_file_extent_item *fi, u64 offset,
1236 int len, char *dest);
Qu Wenruodca22af2020-06-24 18:03:02 +02001237
Qu Wenruod85f9592020-06-24 18:02:55 +02001238/* ctree.c */
1239int btrfs_comp_cpu_keys(const struct btrfs_key *k1, const struct btrfs_key *k2);
1240enum btrfs_tree_block_status
1241btrfs_check_node(struct btrfs_fs_info *fs_info,
1242 struct btrfs_disk_key *parent_key, struct extent_buffer *buf);
1243enum btrfs_tree_block_status
1244btrfs_check_leaf(struct btrfs_fs_info *fs_info,
1245 struct btrfs_disk_key *parent_key, struct extent_buffer *buf);
Qu Wenruo0af17042020-06-24 18:02:59 +02001246struct extent_buffer *read_node_slot(struct btrfs_fs_info *fs_info,
1247 struct extent_buffer *parent, int slot);
1248int btrfs_previous_item(struct btrfs_root *root,
1249 struct btrfs_path *path, u64 min_objectid,
1250 int type);
1251int btrfs_next_sibling_tree_block(struct btrfs_fs_info *fs_info,
1252 struct btrfs_path *path);
1253/*
1254 * Walk up the tree as far as necessary to find the next leaf.
1255 *
1256 * returns 0 if it found something or 1 if there are no greater leaves.
1257 * returns < 0 on io errors.
1258 */
1259static inline int btrfs_next_leaf(struct btrfs_root *root,
1260 struct btrfs_path *path)
1261{
1262 path->lowest_level = 0;
1263 return btrfs_next_sibling_tree_block(root->fs_info, path);
1264}
1265
1266static inline int btrfs_next_item(struct btrfs_root *root,
1267 struct btrfs_path *p)
1268{
1269 ++p->slots[0];
1270 if (p->slots[0] >= btrfs_header_nritems(p->nodes[0]))
1271 return btrfs_next_leaf(root, p);
1272 return 0;
1273}
1274
1275int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path);
Qu Wenruod85f9592020-06-24 18:02:55 +02001276int btrfs_leaf_free_space(struct extent_buffer *leaf);
Qu Wenruo0af17042020-06-24 18:02:59 +02001277int btrfs_search_slot(struct btrfs_trans_handle *trans,
1278 struct btrfs_root *root, const struct btrfs_key *key,
1279 struct btrfs_path *p, int ins_len, int cow);
1280int btrfs_search_slot_for_read(struct btrfs_root *root,
1281 const struct btrfs_key *key,
1282 struct btrfs_path *p, int find_higher,
1283 int return_any);
1284void btrfs_release_path(struct btrfs_path *p);
1285struct btrfs_path *btrfs_alloc_path(void);
1286void btrfs_free_path(struct btrfs_path *p);
1287static inline void btrfs_init_path(struct btrfs_path *p)
1288{
1289 memset(p, 0, sizeof(*p));
1290}
1291int btrfs_bin_search(struct extent_buffer *eb, const struct btrfs_key *key,
1292 int *slot);
1293int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *found_path,
1294 u64 iobjectid, u64 ioff, u8 key_type,
1295 struct btrfs_key *found_key);
Marek Behún4eff4262017-09-03 17:00:26 +02001296#endif /* __BTRFS_CTREE_H__ */