blob: 59439552a45c2a5ac4a0fa77075881a24f9696ad [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
465/* struct btrfs_extent_item */
466BTRFS_SETGET_FUNCS(extent_refs, struct btrfs_extent_item, refs, 64);
467BTRFS_SETGET_STACK_FUNCS(stack_extent_refs, struct btrfs_extent_item, refs, 64);
468BTRFS_SETGET_FUNCS(extent_generation, struct btrfs_extent_item,
469 generation, 64);
470BTRFS_SETGET_FUNCS(extent_flags, struct btrfs_extent_item, flags, 64);
471BTRFS_SETGET_STACK_FUNCS(stack_extent_flags, struct btrfs_extent_item, flags, 64);
472
473BTRFS_SETGET_FUNCS(extent_refs_v0, struct btrfs_extent_item_v0, refs, 32);
474
475BTRFS_SETGET_FUNCS(tree_block_level, struct btrfs_tree_block_info, level, 8);
476
477static inline void btrfs_tree_block_key(struct extent_buffer *eb,
478 struct btrfs_tree_block_info *item,
479 struct btrfs_disk_key *key)
Marek Behún4eff4262017-09-03 17:00:26 +0200480{
Qu Wenruo6925bd22020-06-24 18:02:53 +0200481 read_eb_member(eb, item, struct btrfs_tree_block_info, key, key);
Marek Behún4eff4262017-09-03 17:00:26 +0200482}
483
Qu Wenruo6925bd22020-06-24 18:02:53 +0200484static inline void btrfs_set_tree_block_key(struct extent_buffer *eb,
485 struct btrfs_tree_block_info *item,
486 struct btrfs_disk_key *key)
Marek Behún4eff4262017-09-03 17:00:26 +0200487{
Qu Wenruo6925bd22020-06-24 18:02:53 +0200488 write_eb_member(eb, item, struct btrfs_tree_block_info, key, key);
Marek Behún4eff4262017-09-03 17:00:26 +0200489}
490
Qu Wenruo6925bd22020-06-24 18:02:53 +0200491BTRFS_SETGET_FUNCS(extent_data_ref_root, struct btrfs_extent_data_ref,
492 root, 64);
493BTRFS_SETGET_FUNCS(extent_data_ref_objectid, struct btrfs_extent_data_ref,
494 objectid, 64);
495BTRFS_SETGET_FUNCS(extent_data_ref_offset, struct btrfs_extent_data_ref,
496 offset, 64);
497BTRFS_SETGET_FUNCS(extent_data_ref_count, struct btrfs_extent_data_ref,
498 count, 32);
499
500BTRFS_SETGET_FUNCS(shared_data_ref_count, struct btrfs_shared_data_ref,
501 count, 32);
502
503BTRFS_SETGET_FUNCS(extent_inline_ref_type, struct btrfs_extent_inline_ref,
504 type, 8);
505BTRFS_SETGET_FUNCS(extent_inline_ref_offset, struct btrfs_extent_inline_ref,
506 offset, 64);
507BTRFS_SETGET_STACK_FUNCS(stack_extent_inline_ref_type,
508 struct btrfs_extent_inline_ref, type, 8);
509BTRFS_SETGET_STACK_FUNCS(stack_extent_inline_ref_offset,
510 struct btrfs_extent_inline_ref, offset, 64);
511
512static inline u32 btrfs_extent_inline_ref_size(int type)
Marek Behún4eff4262017-09-03 17:00:26 +0200513{
Qu Wenruo6925bd22020-06-24 18:02:53 +0200514 if (type == BTRFS_TREE_BLOCK_REF_KEY ||
515 type == BTRFS_SHARED_BLOCK_REF_KEY)
516 return sizeof(struct btrfs_extent_inline_ref);
517 if (type == BTRFS_SHARED_DATA_REF_KEY)
518 return sizeof(struct btrfs_shared_data_ref) +
519 sizeof(struct btrfs_extent_inline_ref);
520 if (type == BTRFS_EXTENT_DATA_REF_KEY)
521 return sizeof(struct btrfs_extent_data_ref) +
522 offsetof(struct btrfs_extent_inline_ref, offset);
523 BUG();
524 return 0;
Marek Behún4eff4262017-09-03 17:00:26 +0200525}
526
Qu Wenruo6925bd22020-06-24 18:02:53 +0200527BTRFS_SETGET_FUNCS(ref_root_v0, struct btrfs_extent_ref_v0, root, 64);
528BTRFS_SETGET_FUNCS(ref_generation_v0, struct btrfs_extent_ref_v0,
529 generation, 64);
530BTRFS_SETGET_FUNCS(ref_objectid_v0, struct btrfs_extent_ref_v0, objectid, 64);
531BTRFS_SETGET_FUNCS(ref_count_v0, struct btrfs_extent_ref_v0, count, 32);
Marek Behún4eff4262017-09-03 17:00:26 +0200532
Qu Wenruo6925bd22020-06-24 18:02:53 +0200533/* struct btrfs_node */
534BTRFS_SETGET_FUNCS(key_blockptr, struct btrfs_key_ptr, blockptr, 64);
535BTRFS_SETGET_FUNCS(key_generation, struct btrfs_key_ptr, generation, 64);
Marek Behún4eff4262017-09-03 17:00:26 +0200536
Qu Wenruo6925bd22020-06-24 18:02:53 +0200537static inline u64 btrfs_node_blockptr(struct extent_buffer *eb, int nr)
538{
539 unsigned long ptr;
540 ptr = offsetof(struct btrfs_node, ptrs) +
541 sizeof(struct btrfs_key_ptr) * nr;
542 return btrfs_key_blockptr(eb, (struct btrfs_key_ptr *)ptr);
543}
544
545static inline void btrfs_set_node_blockptr(struct extent_buffer *eb,
546 int nr, u64 val)
547{
548 unsigned long ptr;
549 ptr = offsetof(struct btrfs_node, ptrs) +
550 sizeof(struct btrfs_key_ptr) * nr;
551 btrfs_set_key_blockptr(eb, (struct btrfs_key_ptr *)ptr, val);
552}
553
554static inline u64 btrfs_node_ptr_generation(struct extent_buffer *eb, int nr)
555{
556 unsigned long ptr;
557 ptr = offsetof(struct btrfs_node, ptrs) +
558 sizeof(struct btrfs_key_ptr) * nr;
559 return btrfs_key_generation(eb, (struct btrfs_key_ptr *)ptr);
560}
561
562static inline void btrfs_set_node_ptr_generation(struct extent_buffer *eb,
563 int nr, u64 val)
564{
565 unsigned long ptr;
566 ptr = offsetof(struct btrfs_node, ptrs) +
567 sizeof(struct btrfs_key_ptr) * nr;
568 btrfs_set_key_generation(eb, (struct btrfs_key_ptr *)ptr, val);
569}
570
571static inline unsigned long btrfs_node_key_ptr_offset(int nr)
572{
573 return offsetof(struct btrfs_node, ptrs) +
574 sizeof(struct btrfs_key_ptr) * nr;
575}
576
577static inline void btrfs_node_key(struct extent_buffer *eb,
578 struct btrfs_disk_key *disk_key, int nr)
579{
580 unsigned long ptr;
581 ptr = btrfs_node_key_ptr_offset(nr);
582 read_eb_member(eb, (struct btrfs_key_ptr *)ptr,
583 struct btrfs_key_ptr, key, disk_key);
584}
585
586static inline void btrfs_set_node_key(struct extent_buffer *eb,
587 struct btrfs_disk_key *disk_key, int nr)
588{
589 unsigned long ptr;
590 ptr = btrfs_node_key_ptr_offset(nr);
591 write_eb_member(eb, (struct btrfs_key_ptr *)ptr,
592 struct btrfs_key_ptr, key, disk_key);
593}
594
595/* struct btrfs_item */
596BTRFS_SETGET_FUNCS(item_offset, struct btrfs_item, offset, 32);
597BTRFS_SETGET_FUNCS(item_size, struct btrfs_item, size, 32);
598
599static inline unsigned long btrfs_item_nr_offset(int nr)
600{
601 return offsetof(struct btrfs_leaf, items) +
602 sizeof(struct btrfs_item) * nr;
603}
604
605static inline struct btrfs_item *btrfs_item_nr(int nr)
606{
607 return (struct btrfs_item *)btrfs_item_nr_offset(nr);
608}
609
610static inline u32 btrfs_item_end(struct extent_buffer *eb,
611 struct btrfs_item *item)
612{
613 return btrfs_item_offset(eb, item) + btrfs_item_size(eb, item);
614}
615
616static inline u32 btrfs_item_end_nr(struct extent_buffer *eb, int nr)
617{
618 return btrfs_item_end(eb, btrfs_item_nr(nr));
619}
620
621static inline u32 btrfs_item_offset_nr(const struct extent_buffer *eb, int nr)
622{
623 return btrfs_item_offset(eb, btrfs_item_nr(nr));
624}
625
626static inline u32 btrfs_item_size_nr(struct extent_buffer *eb, int nr)
627{
628 return btrfs_item_size(eb, btrfs_item_nr(nr));
629}
630
631static inline void btrfs_item_key(struct extent_buffer *eb,
632 struct btrfs_disk_key *disk_key, int nr)
633{
634 struct btrfs_item *item = btrfs_item_nr(nr);
635 read_eb_member(eb, item, struct btrfs_item, key, disk_key);
636}
637
638static inline void btrfs_set_item_key(struct extent_buffer *eb,
639 struct btrfs_disk_key *disk_key, int nr)
640{
641 struct btrfs_item *item = btrfs_item_nr(nr);
642 write_eb_member(eb, item, struct btrfs_item, key, disk_key);
643}
644
645BTRFS_SETGET_FUNCS(dir_log_end, struct btrfs_dir_log_item, end, 64);
646
647/*
648 * struct btrfs_root_ref
649 */
650BTRFS_SETGET_FUNCS(root_ref_dirid, struct btrfs_root_ref, dirid, 64);
651BTRFS_SETGET_FUNCS(root_ref_sequence, struct btrfs_root_ref, sequence, 64);
652BTRFS_SETGET_FUNCS(root_ref_name_len, struct btrfs_root_ref, name_len, 16);
653
654BTRFS_SETGET_STACK_FUNCS(stack_root_ref_dirid, struct btrfs_root_ref, dirid, 64);
655BTRFS_SETGET_STACK_FUNCS(stack_root_ref_sequence, struct btrfs_root_ref, sequence, 64);
656BTRFS_SETGET_STACK_FUNCS(stack_root_ref_name_len, struct btrfs_root_ref, name_len, 16);
657
658/* struct btrfs_dir_item */
659BTRFS_SETGET_FUNCS(dir_data_len, struct btrfs_dir_item, data_len, 16);
660BTRFS_SETGET_FUNCS(dir_type, struct btrfs_dir_item, type, 8);
661BTRFS_SETGET_FUNCS(dir_name_len, struct btrfs_dir_item, name_len, 16);
662BTRFS_SETGET_FUNCS(dir_transid, struct btrfs_dir_item, transid, 64);
663
664BTRFS_SETGET_STACK_FUNCS(stack_dir_data_len, struct btrfs_dir_item, data_len, 16);
665BTRFS_SETGET_STACK_FUNCS(stack_dir_type, struct btrfs_dir_item, type, 8);
666BTRFS_SETGET_STACK_FUNCS(stack_dir_name_len, struct btrfs_dir_item, name_len, 16);
667BTRFS_SETGET_STACK_FUNCS(stack_dir_transid, struct btrfs_dir_item, transid, 64);
668
669static inline void btrfs_dir_item_key(struct extent_buffer *eb,
670 struct btrfs_dir_item *item,
671 struct btrfs_disk_key *key)
672{
673 read_eb_member(eb, item, struct btrfs_dir_item, location, key);
674}
675
676static inline void btrfs_set_dir_item_key(struct extent_buffer *eb,
677 struct btrfs_dir_item *item,
678 struct btrfs_disk_key *key)
679{
680 write_eb_member(eb, item, struct btrfs_dir_item, location, key);
681}
682
683/* struct btrfs_free_space_header */
684BTRFS_SETGET_FUNCS(free_space_entries, struct btrfs_free_space_header,
685 num_entries, 64);
686BTRFS_SETGET_FUNCS(free_space_bitmaps, struct btrfs_free_space_header,
687 num_bitmaps, 64);
688BTRFS_SETGET_FUNCS(free_space_generation, struct btrfs_free_space_header,
689 generation, 64);
690
691static inline void btrfs_free_space_key(struct extent_buffer *eb,
692 struct btrfs_free_space_header *h,
693 struct btrfs_disk_key *key)
694{
695 read_eb_member(eb, h, struct btrfs_free_space_header, location, key);
696}
697
698static inline void btrfs_set_free_space_key(struct extent_buffer *eb,
699 struct btrfs_free_space_header *h,
700 struct btrfs_disk_key *key)
701{
702 write_eb_member(eb, h, struct btrfs_free_space_header, location, key);
703}
704
705/* struct btrfs_disk_key */
706BTRFS_SETGET_STACK_FUNCS(disk_key_objectid, struct btrfs_disk_key,
707 objectid, 64);
708BTRFS_SETGET_STACK_FUNCS(disk_key_offset, struct btrfs_disk_key, offset, 64);
709BTRFS_SETGET_STACK_FUNCS(disk_key_type, struct btrfs_disk_key, type, 8);
710
711static inline void btrfs_disk_key_to_cpu(struct btrfs_key *cpu,
712 struct btrfs_disk_key *disk)
713{
714 cpu->offset = le64_to_cpu(disk->offset);
715 cpu->type = disk->type;
716 cpu->objectid = le64_to_cpu(disk->objectid);
717}
718
719static inline void btrfs_cpu_key_to_disk(struct btrfs_disk_key *disk,
720 const struct btrfs_key *cpu)
721{
722 disk->offset = cpu_to_le64(cpu->offset);
723 disk->type = cpu->type;
724 disk->objectid = cpu_to_le64(cpu->objectid);
725}
726
727static inline void btrfs_node_key_to_cpu(struct extent_buffer *eb,
728 struct btrfs_key *key, int nr)
729{
730 struct btrfs_disk_key disk_key;
731 btrfs_node_key(eb, &disk_key, nr);
732 btrfs_disk_key_to_cpu(key, &disk_key);
733}
734
735static inline void btrfs_item_key_to_cpu(struct extent_buffer *eb,
736 struct btrfs_key *key, int nr)
737{
738 struct btrfs_disk_key disk_key;
739 btrfs_item_key(eb, &disk_key, nr);
740 btrfs_disk_key_to_cpu(key, &disk_key);
741}
742
743static inline void btrfs_dir_item_key_to_cpu(struct extent_buffer *eb,
744 struct btrfs_dir_item *item,
745 struct btrfs_key *key)
746{
747 struct btrfs_disk_key disk_key;
748 btrfs_dir_item_key(eb, item, &disk_key);
749 btrfs_disk_key_to_cpu(key, &disk_key);
750}
751
752/* struct btrfs_header */
753BTRFS_SETGET_HEADER_FUNCS(header_bytenr, struct btrfs_header, bytenr, 64);
754BTRFS_SETGET_HEADER_FUNCS(header_generation, struct btrfs_header,
755 generation, 64);
756BTRFS_SETGET_HEADER_FUNCS(header_owner, struct btrfs_header, owner, 64);
757BTRFS_SETGET_HEADER_FUNCS(header_nritems, struct btrfs_header, nritems, 32);
758BTRFS_SETGET_HEADER_FUNCS(header_flags, struct btrfs_header, flags, 64);
759BTRFS_SETGET_HEADER_FUNCS(header_level, struct btrfs_header, level, 8);
760BTRFS_SETGET_STACK_FUNCS(stack_header_bytenr, struct btrfs_header, bytenr, 64);
761BTRFS_SETGET_STACK_FUNCS(stack_header_nritems, struct btrfs_header, nritems,
762 32);
763BTRFS_SETGET_STACK_FUNCS(stack_header_owner, struct btrfs_header, owner, 64);
764BTRFS_SETGET_STACK_FUNCS(stack_header_generation, struct btrfs_header,
765 generation, 64);
766
767static inline int btrfs_header_flag(struct extent_buffer *eb, u64 flag)
768{
769 return (btrfs_header_flags(eb) & flag) == flag;
770}
771
772static inline int btrfs_set_header_flag(struct extent_buffer *eb, u64 flag)
773{
774 u64 flags = btrfs_header_flags(eb);
775 btrfs_set_header_flags(eb, flags | flag);
776 return (flags & flag) == flag;
777}
778
779static inline int btrfs_clear_header_flag(struct extent_buffer *eb, u64 flag)
780{
781 u64 flags = btrfs_header_flags(eb);
782 btrfs_set_header_flags(eb, flags & ~flag);
783 return (flags & flag) == flag;
784}
785
786static inline int btrfs_header_backref_rev(struct extent_buffer *eb)
787{
788 u64 flags = btrfs_header_flags(eb);
789 return flags >> BTRFS_BACKREF_REV_SHIFT;
790}
791
792static inline void btrfs_set_header_backref_rev(struct extent_buffer *eb,
793 int rev)
794{
795 u64 flags = btrfs_header_flags(eb);
796 flags &= ~BTRFS_BACKREF_REV_MASK;
797 flags |= (u64)rev << BTRFS_BACKREF_REV_SHIFT;
798 btrfs_set_header_flags(eb, flags);
799}
800
801static inline unsigned long btrfs_header_fsid(void)
802{
803 return offsetof(struct btrfs_header, fsid);
804}
805
806static inline unsigned long btrfs_header_chunk_tree_uuid(struct extent_buffer *eb)
807{
808 return offsetof(struct btrfs_header, chunk_tree_uuid);
809}
810
811static inline u8 *btrfs_header_csum(struct extent_buffer *eb)
812{
813 unsigned long ptr = offsetof(struct btrfs_header, csum);
814 return (u8 *)ptr;
815}
816
817static inline int btrfs_is_leaf(struct extent_buffer *eb)
818{
819 return (btrfs_header_level(eb) == 0);
820}
821
822/* struct btrfs_root_item */
823BTRFS_SETGET_FUNCS(disk_root_generation, struct btrfs_root_item,
824 generation, 64);
825BTRFS_SETGET_FUNCS(disk_root_refs, struct btrfs_root_item, refs, 32);
826BTRFS_SETGET_FUNCS(disk_root_bytenr, struct btrfs_root_item, bytenr, 64);
827BTRFS_SETGET_FUNCS(disk_root_level, struct btrfs_root_item, level, 8);
828
829BTRFS_SETGET_STACK_FUNCS(root_generation, struct btrfs_root_item,
830 generation, 64);
831BTRFS_SETGET_STACK_FUNCS(root_bytenr, struct btrfs_root_item, bytenr, 64);
832BTRFS_SETGET_STACK_FUNCS(root_level, struct btrfs_root_item, level, 8);
833BTRFS_SETGET_STACK_FUNCS(root_dirid, struct btrfs_root_item, root_dirid, 64);
834BTRFS_SETGET_STACK_FUNCS(root_refs, struct btrfs_root_item, refs, 32);
835BTRFS_SETGET_STACK_FUNCS(root_flags, struct btrfs_root_item, flags, 64);
836BTRFS_SETGET_STACK_FUNCS(root_used, struct btrfs_root_item, bytes_used, 64);
837BTRFS_SETGET_STACK_FUNCS(root_limit, struct btrfs_root_item, byte_limit, 64);
838BTRFS_SETGET_STACK_FUNCS(root_last_snapshot, struct btrfs_root_item,
839 last_snapshot, 64);
840BTRFS_SETGET_STACK_FUNCS(root_generation_v2, struct btrfs_root_item,
841 generation_v2, 64);
842BTRFS_SETGET_STACK_FUNCS(root_ctransid, struct btrfs_root_item,
843 ctransid, 64);
844BTRFS_SETGET_STACK_FUNCS(root_otransid, struct btrfs_root_item,
845 otransid, 64);
846BTRFS_SETGET_STACK_FUNCS(root_stransid, struct btrfs_root_item,
847 stransid, 64);
848BTRFS_SETGET_STACK_FUNCS(root_rtransid, struct btrfs_root_item,
849 rtransid, 64);
850
851static inline struct btrfs_timespec* btrfs_root_ctime(
852 struct btrfs_root_item *root_item)
853{
854 unsigned long ptr = (unsigned long)root_item;
855 ptr += offsetof(struct btrfs_root_item, ctime);
856 return (struct btrfs_timespec *)ptr;
857}
858
859static inline struct btrfs_timespec* btrfs_root_otime(
860 struct btrfs_root_item *root_item)
861{
862 unsigned long ptr = (unsigned long)root_item;
863 ptr += offsetof(struct btrfs_root_item, otime);
864 return (struct btrfs_timespec *)ptr;
865}
866
867static inline struct btrfs_timespec* btrfs_root_stime(
868 struct btrfs_root_item *root_item)
869{
870 unsigned long ptr = (unsigned long)root_item;
871 ptr += offsetof(struct btrfs_root_item, stime);
872 return (struct btrfs_timespec *)ptr;
873}
874
875static inline struct btrfs_timespec* btrfs_root_rtime(
876 struct btrfs_root_item *root_item)
877{
878 unsigned long ptr = (unsigned long)root_item;
879 ptr += offsetof(struct btrfs_root_item, rtime);
880 return (struct btrfs_timespec *)ptr;
881}
882
883/* struct btrfs_root_backup */
884BTRFS_SETGET_STACK_FUNCS(backup_tree_root, struct btrfs_root_backup,
885 tree_root, 64);
886BTRFS_SETGET_STACK_FUNCS(backup_tree_root_gen, struct btrfs_root_backup,
887 tree_root_gen, 64);
888BTRFS_SETGET_STACK_FUNCS(backup_tree_root_level, struct btrfs_root_backup,
889 tree_root_level, 8);
890
891BTRFS_SETGET_STACK_FUNCS(backup_chunk_root, struct btrfs_root_backup,
892 chunk_root, 64);
893BTRFS_SETGET_STACK_FUNCS(backup_chunk_root_gen, struct btrfs_root_backup,
894 chunk_root_gen, 64);
895BTRFS_SETGET_STACK_FUNCS(backup_chunk_root_level, struct btrfs_root_backup,
896 chunk_root_level, 8);
897
898BTRFS_SETGET_STACK_FUNCS(backup_extent_root, struct btrfs_root_backup,
899 extent_root, 64);
900BTRFS_SETGET_STACK_FUNCS(backup_extent_root_gen, struct btrfs_root_backup,
901 extent_root_gen, 64);
902BTRFS_SETGET_STACK_FUNCS(backup_extent_root_level, struct btrfs_root_backup,
903 extent_root_level, 8);
904
905BTRFS_SETGET_STACK_FUNCS(backup_fs_root, struct btrfs_root_backup,
906 fs_root, 64);
907BTRFS_SETGET_STACK_FUNCS(backup_fs_root_gen, struct btrfs_root_backup,
908 fs_root_gen, 64);
909BTRFS_SETGET_STACK_FUNCS(backup_fs_root_level, struct btrfs_root_backup,
910 fs_root_level, 8);
911
912BTRFS_SETGET_STACK_FUNCS(backup_dev_root, struct btrfs_root_backup,
913 dev_root, 64);
914BTRFS_SETGET_STACK_FUNCS(backup_dev_root_gen, struct btrfs_root_backup,
915 dev_root_gen, 64);
916BTRFS_SETGET_STACK_FUNCS(backup_dev_root_level, struct btrfs_root_backup,
917 dev_root_level, 8);
918
919BTRFS_SETGET_STACK_FUNCS(backup_csum_root, struct btrfs_root_backup,
920 csum_root, 64);
921BTRFS_SETGET_STACK_FUNCS(backup_csum_root_gen, struct btrfs_root_backup,
922 csum_root_gen, 64);
923BTRFS_SETGET_STACK_FUNCS(backup_csum_root_level, struct btrfs_root_backup,
924 csum_root_level, 8);
925BTRFS_SETGET_STACK_FUNCS(backup_total_bytes, struct btrfs_root_backup,
926 total_bytes, 64);
927BTRFS_SETGET_STACK_FUNCS(backup_bytes_used, struct btrfs_root_backup,
928 bytes_used, 64);
929BTRFS_SETGET_STACK_FUNCS(backup_num_devices, struct btrfs_root_backup,
930 num_devices, 64);
Qu Wenruo1a618082020-06-24 18:02:49 +0200931
932/* struct btrfs_super_block */
933
934BTRFS_SETGET_STACK_FUNCS(super_bytenr, struct btrfs_super_block, bytenr, 64);
935BTRFS_SETGET_STACK_FUNCS(super_flags, struct btrfs_super_block, flags, 64);
936BTRFS_SETGET_STACK_FUNCS(super_generation, struct btrfs_super_block,
937 generation, 64);
938BTRFS_SETGET_STACK_FUNCS(super_root, struct btrfs_super_block, root, 64);
939BTRFS_SETGET_STACK_FUNCS(super_sys_array_size,
940 struct btrfs_super_block, sys_chunk_array_size, 32);
941BTRFS_SETGET_STACK_FUNCS(super_chunk_root_generation,
942 struct btrfs_super_block, chunk_root_generation, 64);
943BTRFS_SETGET_STACK_FUNCS(super_root_level, struct btrfs_super_block,
944 root_level, 8);
945BTRFS_SETGET_STACK_FUNCS(super_chunk_root, struct btrfs_super_block,
946 chunk_root, 64);
947BTRFS_SETGET_STACK_FUNCS(super_chunk_root_level, struct btrfs_super_block,
948 chunk_root_level, 8);
949BTRFS_SETGET_STACK_FUNCS(super_log_root, struct btrfs_super_block,
950 log_root, 64);
951BTRFS_SETGET_STACK_FUNCS(super_log_root_transid, struct btrfs_super_block,
952 log_root_transid, 64);
953BTRFS_SETGET_STACK_FUNCS(super_log_root_level, struct btrfs_super_block,
954 log_root_level, 8);
955BTRFS_SETGET_STACK_FUNCS(super_total_bytes, struct btrfs_super_block,
956 total_bytes, 64);
957BTRFS_SETGET_STACK_FUNCS(super_bytes_used, struct btrfs_super_block,
958 bytes_used, 64);
959BTRFS_SETGET_STACK_FUNCS(super_sectorsize, struct btrfs_super_block,
960 sectorsize, 32);
961BTRFS_SETGET_STACK_FUNCS(super_nodesize, struct btrfs_super_block,
962 nodesize, 32);
963BTRFS_SETGET_STACK_FUNCS(super_stripesize, struct btrfs_super_block,
964 stripesize, 32);
965BTRFS_SETGET_STACK_FUNCS(super_root_dir, struct btrfs_super_block,
966 root_dir_objectid, 64);
967BTRFS_SETGET_STACK_FUNCS(super_num_devices, struct btrfs_super_block,
968 num_devices, 64);
969BTRFS_SETGET_STACK_FUNCS(super_compat_flags, struct btrfs_super_block,
970 compat_flags, 64);
971BTRFS_SETGET_STACK_FUNCS(super_compat_ro_flags, struct btrfs_super_block,
972 compat_ro_flags, 64);
973BTRFS_SETGET_STACK_FUNCS(super_incompat_flags, struct btrfs_super_block,
974 incompat_flags, 64);
975BTRFS_SETGET_STACK_FUNCS(super_csum_type, struct btrfs_super_block,
976 csum_type, 16);
977BTRFS_SETGET_STACK_FUNCS(super_cache_generation, struct btrfs_super_block,
978 cache_generation, 64);
979BTRFS_SETGET_STACK_FUNCS(super_uuid_tree_generation, struct btrfs_super_block,
980 uuid_tree_generation, 64);
981BTRFS_SETGET_STACK_FUNCS(super_magic, struct btrfs_super_block, magic, 64);
982
Qu Wenruo6925bd22020-06-24 18:02:53 +0200983static inline unsigned long btrfs_leaf_data(struct extent_buffer *l)
984{
985 return offsetof(struct btrfs_leaf, items);
986}
987
988/* struct btrfs_file_extent_item */
989BTRFS_SETGET_FUNCS(file_extent_type, struct btrfs_file_extent_item, type, 8);
990BTRFS_SETGET_STACK_FUNCS(stack_file_extent_type, struct btrfs_file_extent_item, type, 8);
991
992static inline unsigned long btrfs_file_extent_inline_start(struct
993 btrfs_file_extent_item *e)
994{
995 unsigned long offset = (unsigned long)e;
996 offset += offsetof(struct btrfs_file_extent_item, disk_bytenr);
997 return offset;
998}
999
1000static inline u32 btrfs_file_extent_calc_inline_size(u32 datasize)
1001{
1002 return offsetof(struct btrfs_file_extent_item, disk_bytenr) + datasize;
1003}
1004
1005BTRFS_SETGET_FUNCS(file_extent_disk_bytenr, struct btrfs_file_extent_item,
1006 disk_bytenr, 64);
1007BTRFS_SETGET_STACK_FUNCS(stack_file_extent_disk_bytenr, struct btrfs_file_extent_item,
1008 disk_bytenr, 64);
1009BTRFS_SETGET_FUNCS(file_extent_generation, struct btrfs_file_extent_item,
1010 generation, 64);
1011BTRFS_SETGET_STACK_FUNCS(stack_file_extent_generation, struct btrfs_file_extent_item,
1012 generation, 64);
1013BTRFS_SETGET_FUNCS(file_extent_disk_num_bytes, struct btrfs_file_extent_item,
1014 disk_num_bytes, 64);
1015BTRFS_SETGET_FUNCS(file_extent_offset, struct btrfs_file_extent_item,
1016 offset, 64);
1017BTRFS_SETGET_STACK_FUNCS(stack_file_extent_offset, struct btrfs_file_extent_item,
1018 offset, 64);
1019BTRFS_SETGET_FUNCS(file_extent_num_bytes, struct btrfs_file_extent_item,
1020 num_bytes, 64);
1021BTRFS_SETGET_STACK_FUNCS(stack_file_extent_num_bytes, struct btrfs_file_extent_item,
1022 num_bytes, 64);
1023BTRFS_SETGET_FUNCS(file_extent_ram_bytes, struct btrfs_file_extent_item,
1024 ram_bytes, 64);
1025BTRFS_SETGET_STACK_FUNCS(stack_file_extent_ram_bytes, struct btrfs_file_extent_item,
1026 ram_bytes, 64);
1027BTRFS_SETGET_FUNCS(file_extent_compression, struct btrfs_file_extent_item,
1028 compression, 8);
1029BTRFS_SETGET_STACK_FUNCS(stack_file_extent_compression, struct btrfs_file_extent_item,
1030 compression, 8);
1031BTRFS_SETGET_FUNCS(file_extent_encryption, struct btrfs_file_extent_item,
1032 encryption, 8);
1033BTRFS_SETGET_FUNCS(file_extent_other_encoding, struct btrfs_file_extent_item,
1034 other_encoding, 16);
1035
1036/* btrfs_qgroup_status_item */
1037BTRFS_SETGET_FUNCS(qgroup_status_version, struct btrfs_qgroup_status_item,
1038 version, 64);
1039BTRFS_SETGET_FUNCS(qgroup_status_generation, struct btrfs_qgroup_status_item,
1040 generation, 64);
1041BTRFS_SETGET_FUNCS(qgroup_status_flags, struct btrfs_qgroup_status_item,
1042 flags, 64);
1043BTRFS_SETGET_FUNCS(qgroup_status_rescan, struct btrfs_qgroup_status_item,
1044 rescan, 64);
1045
1046BTRFS_SETGET_STACK_FUNCS(stack_qgroup_status_version,
1047 struct btrfs_qgroup_status_item, version, 64);
1048BTRFS_SETGET_STACK_FUNCS(stack_qgroup_status_generation,
1049 struct btrfs_qgroup_status_item, generation, 64);
1050BTRFS_SETGET_STACK_FUNCS(stack_qgroup_status_flags,
1051 struct btrfs_qgroup_status_item, flags, 64);
1052BTRFS_SETGET_STACK_FUNCS(stack_qgroup_status_rescan,
1053 struct btrfs_qgroup_status_item, rescan, 64);
1054
1055/* btrfs_qgroup_info_item */
1056BTRFS_SETGET_FUNCS(qgroup_info_generation, struct btrfs_qgroup_info_item,
1057 generation, 64);
1058BTRFS_SETGET_FUNCS(qgroup_info_referenced, struct btrfs_qgroup_info_item,
1059 rfer, 64);
1060BTRFS_SETGET_FUNCS(qgroup_info_referenced_compressed,
1061 struct btrfs_qgroup_info_item, rfer_cmpr, 64);
1062BTRFS_SETGET_FUNCS(qgroup_info_exclusive, struct btrfs_qgroup_info_item,
1063 excl, 64);
1064BTRFS_SETGET_FUNCS(qgroup_info_exclusive_compressed,
1065 struct btrfs_qgroup_info_item, excl_cmpr, 64);
1066
1067BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_generation,
1068 struct btrfs_qgroup_info_item, generation, 64);
1069BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_referenced,
1070 struct btrfs_qgroup_info_item, rfer, 64);
1071BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_referenced_compressed,
1072 struct btrfs_qgroup_info_item, rfer_cmpr, 64);
1073BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_exclusive,
1074 struct btrfs_qgroup_info_item, excl, 64);
1075BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_exclusive_compressed,
1076 struct btrfs_qgroup_info_item, excl_cmpr, 64);
1077
1078/* btrfs_qgroup_limit_item */
1079BTRFS_SETGET_FUNCS(qgroup_limit_flags, struct btrfs_qgroup_limit_item,
1080 flags, 64);
1081BTRFS_SETGET_FUNCS(qgroup_limit_max_referenced, struct btrfs_qgroup_limit_item,
1082 max_rfer, 64);
1083BTRFS_SETGET_FUNCS(qgroup_limit_max_exclusive, struct btrfs_qgroup_limit_item,
1084 max_excl, 64);
1085BTRFS_SETGET_FUNCS(qgroup_limit_rsv_referenced, struct btrfs_qgroup_limit_item,
1086 rsv_rfer, 64);
1087BTRFS_SETGET_FUNCS(qgroup_limit_rsv_exclusive, struct btrfs_qgroup_limit_item,
1088 rsv_excl, 64);
1089
1090BTRFS_SETGET_STACK_FUNCS(stack_qgroup_limit_flags,
1091 struct btrfs_qgroup_limit_item, flags, 64);
1092BTRFS_SETGET_STACK_FUNCS(stack_qgroup_limit_max_referenced,
1093 struct btrfs_qgroup_limit_item, max_rfer, 64);
1094BTRFS_SETGET_STACK_FUNCS(stack_qgroup_limit_max_exclusive,
1095 struct btrfs_qgroup_limit_item, max_excl, 64);
1096BTRFS_SETGET_STACK_FUNCS(stack_qgroup_limit_rsv_referenced,
1097 struct btrfs_qgroup_limit_item, rsv_rfer, 64);
1098BTRFS_SETGET_STACK_FUNCS(stack_qgroup_limit_rsv_exclusive,
1099 struct btrfs_qgroup_limit_item, rsv_excl, 64);
1100
1101/* btrfs_balance_item */
1102BTRFS_SETGET_FUNCS(balance_item_flags, struct btrfs_balance_item, flags, 64);
1103
1104static inline struct btrfs_disk_balance_args* btrfs_balance_item_data(
1105 struct extent_buffer *eb, struct btrfs_balance_item *bi)
1106{
1107 unsigned long offset = (unsigned long)bi;
1108 struct btrfs_balance_item *p;
1109 p = (struct btrfs_balance_item *)(eb->data + offset);
1110 return &p->data;
1111}
1112
1113static inline struct btrfs_disk_balance_args* btrfs_balance_item_meta(
1114 struct extent_buffer *eb, struct btrfs_balance_item *bi)
1115{
1116 unsigned long offset = (unsigned long)bi;
1117 struct btrfs_balance_item *p;
1118 p = (struct btrfs_balance_item *)(eb->data + offset);
1119 return &p->meta;
1120}
1121
1122static inline struct btrfs_disk_balance_args* btrfs_balance_item_sys(
1123 struct extent_buffer *eb, struct btrfs_balance_item *bi)
1124{
1125 unsigned long offset = (unsigned long)bi;
1126 struct btrfs_balance_item *p;
1127 p = (struct btrfs_balance_item *)(eb->data + offset);
1128 return &p->sys;
1129}
1130
1131static inline u64 btrfs_dev_stats_value(const struct extent_buffer *eb,
1132 const struct btrfs_dev_stats_item *ptr,
1133 int index)
1134{
1135 u64 val;
1136
1137 read_extent_buffer(eb, &val,
1138 offsetof(struct btrfs_dev_stats_item, values) +
1139 ((unsigned long)ptr) + (index * sizeof(u64)),
1140 sizeof(val));
1141 return val;
1142}
1143
1144/*
1145 * this returns the number of bytes used by the item on disk, minus the
1146 * size of any extent headers. If a file is compressed on disk, this is
1147 * the compressed size
1148 */
1149static inline u32 btrfs_file_extent_inline_item_len(struct extent_buffer *eb,
1150 struct btrfs_item *e)
1151{
1152 unsigned long offset;
1153 offset = offsetof(struct btrfs_file_extent_item, disk_bytenr);
1154 return btrfs_item_size(eb, e) - offset;
1155}
1156
1157#define btrfs_fs_incompat(fs_info, opt) \
1158 __btrfs_fs_incompat((fs_info), BTRFS_FEATURE_INCOMPAT_##opt)
1159
1160static inline bool __btrfs_fs_incompat(struct btrfs_fs_info *fs_info, u64 flag)
1161{
1162 struct btrfs_super_block *disk_super;
1163 disk_super = fs_info->super_copy;
1164 return !!(btrfs_super_incompat_flags(disk_super) & flag);
1165}
1166
1167#define btrfs_fs_compat_ro(fs_info, opt) \
1168 __btrfs_fs_compat_ro((fs_info), BTRFS_FEATURE_COMPAT_RO_##opt)
1169
1170static inline int __btrfs_fs_compat_ro(struct btrfs_fs_info *fs_info, u64 flag)
1171{
1172 struct btrfs_super_block *disk_super;
1173 disk_super = fs_info->super_copy;
1174 return !!(btrfs_super_compat_ro_flags(disk_super) & flag);
1175}
1176
1177/* helper function to cast into the data area of the leaf. */
1178#define btrfs_item_ptr(leaf, slot, type) \
1179 ((type *)(btrfs_leaf_data(leaf) + \
1180 btrfs_item_offset_nr(leaf, slot)))
1181
1182#define btrfs_item_ptr_offset(leaf, slot) \
1183 ((unsigned long)(btrfs_leaf_data(leaf) + \
1184 btrfs_item_offset_nr(leaf, slot)))
1185
1186static inline u64 btrfs_name_hash(const char *name, int len)
1187{
1188 return (u64)crc32c((u32)~1, (u8 *)name, len);
1189}
1190
1191/*
1192 * Figure the key offset of an extended inode ref
1193 */
1194static inline u64 btrfs_extref_hash(u64 parent_objectid, const char *name,
1195 int len)
1196{
1197 return crc32(parent_objectid, (u8 *)name, len);
1198}
1199
1200union btrfs_tree_node {
1201 struct btrfs_header header;
1202 struct btrfs_leaf leaf;
1203 struct btrfs_node node;
1204};
1205
Qu Wenruo6925bd22020-06-24 18:02:53 +02001206#define btrfs_path_item_ptr(p,t) \
1207 ((t *) btrfs_path_leaf_data((p)))
1208
1209u16 btrfs_super_csum_size(const struct btrfs_super_block *s);
1210const char *btrfs_super_csum_name(u16 csum_type);
1211u16 btrfs_csum_type_size(u16 csum_type);
1212size_t btrfs_super_num_csums(void);
1213
Qu Wenruo1d5a7b72020-06-24 18:03:01 +02001214/* root-tree.c */
1215int btrfs_find_last_root(struct btrfs_root *root, u64 objectid,
1216 struct btrfs_root_item *item, struct btrfs_key *key);
1217
Qu Wenruodca22af2020-06-24 18:03:02 +02001218/* dir-item.c */
1219struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans,
1220 struct btrfs_root *root,
1221 struct btrfs_path *path, u64 dir,
1222 const char *name, int name_len,
1223 int mod);
Qu Wenruo348848a2020-06-24 18:03:06 +02001224typedef int (*btrfs_iter_dir_callback_t)(struct btrfs_root *root,
1225 struct extent_buffer *eb,
1226 struct btrfs_dir_item *di);
1227int btrfs_iter_dir(struct btrfs_root *root, u64 ino,
1228 btrfs_iter_dir_callback_t callback);
Qu Wenruoe565bd42020-06-24 18:03:05 +02001229/* inode.c */
1230int btrfs_lookup_path(struct btrfs_root *root, u64 ino, const char *filename,
1231 struct btrfs_root **root_ret, u64 *ino_ret,
1232 u8 *type_ret, int symlink_limit);
Qu Wenruo83b04df2020-06-24 18:03:09 +02001233int btrfs_read_extent_inline(struct btrfs_path *path,
1234 struct btrfs_file_extent_item *fi, char *dest);
1235int btrfs_read_extent_reg(struct btrfs_path *path,
1236 struct btrfs_file_extent_item *fi, u64 offset,
1237 int len, char *dest);
Qu Wenruodca22af2020-06-24 18:03:02 +02001238
Qu Wenruod85f9592020-06-24 18:02:55 +02001239/* ctree.c */
1240int btrfs_comp_cpu_keys(const struct btrfs_key *k1, const struct btrfs_key *k2);
1241enum btrfs_tree_block_status
1242btrfs_check_node(struct btrfs_fs_info *fs_info,
1243 struct btrfs_disk_key *parent_key, struct extent_buffer *buf);
1244enum btrfs_tree_block_status
1245btrfs_check_leaf(struct btrfs_fs_info *fs_info,
1246 struct btrfs_disk_key *parent_key, struct extent_buffer *buf);
Qu Wenruo0af17042020-06-24 18:02:59 +02001247struct extent_buffer *read_node_slot(struct btrfs_fs_info *fs_info,
1248 struct extent_buffer *parent, int slot);
1249int btrfs_previous_item(struct btrfs_root *root,
1250 struct btrfs_path *path, u64 min_objectid,
1251 int type);
1252int btrfs_next_sibling_tree_block(struct btrfs_fs_info *fs_info,
1253 struct btrfs_path *path);
1254/*
1255 * Walk up the tree as far as necessary to find the next leaf.
1256 *
1257 * returns 0 if it found something or 1 if there are no greater leaves.
1258 * returns < 0 on io errors.
1259 */
1260static inline int btrfs_next_leaf(struct btrfs_root *root,
1261 struct btrfs_path *path)
1262{
1263 path->lowest_level = 0;
1264 return btrfs_next_sibling_tree_block(root->fs_info, path);
1265}
1266
1267static inline int btrfs_next_item(struct btrfs_root *root,
1268 struct btrfs_path *p)
1269{
1270 ++p->slots[0];
1271 if (p->slots[0] >= btrfs_header_nritems(p->nodes[0]))
1272 return btrfs_next_leaf(root, p);
1273 return 0;
1274}
1275
1276int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path);
Qu Wenruod85f9592020-06-24 18:02:55 +02001277int btrfs_leaf_free_space(struct extent_buffer *leaf);
Qu Wenruo0af17042020-06-24 18:02:59 +02001278int btrfs_search_slot(struct btrfs_trans_handle *trans,
1279 struct btrfs_root *root, const struct btrfs_key *key,
1280 struct btrfs_path *p, int ins_len, int cow);
1281int btrfs_search_slot_for_read(struct btrfs_root *root,
1282 const struct btrfs_key *key,
1283 struct btrfs_path *p, int find_higher,
1284 int return_any);
1285void btrfs_release_path(struct btrfs_path *p);
1286struct btrfs_path *btrfs_alloc_path(void);
1287void btrfs_free_path(struct btrfs_path *p);
1288static inline void btrfs_init_path(struct btrfs_path *p)
1289{
1290 memset(p, 0, sizeof(*p));
1291}
1292int btrfs_bin_search(struct extent_buffer *eb, const struct btrfs_key *key,
1293 int *slot);
1294int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *found_path,
1295 u64 iobjectid, u64 ioff, u8 key_type,
1296 struct btrfs_key *found_key);
Marek Behún4eff4262017-09-03 17:00:26 +02001297#endif /* __BTRFS_CTREE_H__ */