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