blob: e658c88aeeef7543a33495fd5c12cf47b2e243de [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>
Qu Wenruo6925bd22020-06-24 18:02:53 +020016#include <linux/unaligned/le_byteshift.h>
17#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))
Marek Behún4eff4262017-09-03 17:00:26 +020055/* ioprio of readahead is set to idle */
56#define BTRFS_IOPRIO_READA (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0))
57
58#define BTRFS_DIRTY_METADATA_THRESH SZ_32M
59
60#define BTRFS_MAX_EXTENT_SIZE SZ_128M
61
Qu Wenruod85f9592020-06-24 18:02:55 +020062enum btrfs_tree_block_status {
63 BTRFS_TREE_BLOCK_CLEAN,
64 BTRFS_TREE_BLOCK_INVALID_NRITEMS,
65 BTRFS_TREE_BLOCK_INVALID_PARENT_KEY,
66 BTRFS_TREE_BLOCK_BAD_KEY_ORDER,
67 BTRFS_TREE_BLOCK_INVALID_LEVEL,
68 BTRFS_TREE_BLOCK_INVALID_FREE_SPACE,
69 BTRFS_TREE_BLOCK_INVALID_OFFSETS,
70};
71
Qu Wenruofd63dbb2020-06-24 18:02:58 +020072struct btrfs_root {
73 struct extent_buffer *node;
74 struct btrfs_root_item root_item;
75 struct btrfs_key root_key;
76 struct btrfs_fs_info *fs_info;
77 u64 objectid;
78 u64 last_trans;
79
80 int ref_cows;
81 int track_dirty;
82
83 u32 type;
84 u64 last_inode_alloc;
85
86 struct rb_node rb_node;
87};
88
Qu Wenruo6925bd22020-06-24 18:02:53 +020089struct btrfs_device;
90struct btrfs_fs_devices;
91struct btrfs_fs_info {
92 u8 chunk_tree_uuid[BTRFS_UUID_SIZE];
93 u8 *new_chunk_tree_uuid;
94 struct btrfs_root *fs_root;
95 struct btrfs_root *tree_root;
96 struct btrfs_root *chunk_root;
97 struct btrfs_root *csum_root;
98
99 struct rb_root fs_root_tree;
100
101 struct extent_io_tree extent_cache;
102
103 /* logical->physical extent mapping */
104 struct btrfs_mapping_tree mapping_tree;
105
106 u64 last_trans_committed;
107
108 struct btrfs_super_block *super_copy;
109
110 struct btrfs_fs_devices *fs_devices;
111
112 /* Cached block sizes */
113 u32 nodesize;
114 u32 sectorsize;
115 u32 stripesize;
116};
117
Qu Wenruod85f9592020-06-24 18:02:55 +0200118static inline u32 BTRFS_MAX_ITEM_SIZE(const struct btrfs_fs_info *info)
119{
120 return BTRFS_LEAF_DATA_SIZE(info) - sizeof(struct btrfs_item);
121}
122
123static inline u32 BTRFS_NODEPTRS_PER_BLOCK(const struct btrfs_fs_info *info)
124{
125 return BTRFS_LEAF_DATA_SIZE(info) / sizeof(struct btrfs_key_ptr);
126}
127
128static inline u32 BTRFS_NODEPTRS_PER_EXTENT_BUFFER(const struct extent_buffer *eb)
129{
130 BUG_ON(eb->fs_info && eb->fs_info->nodesize != eb->len);
131 return __BTRFS_LEAF_DATA_SIZE(eb->len) / sizeof(struct btrfs_key_ptr);
132}
133
134#define BTRFS_FILE_EXTENT_INLINE_DATA_START \
135 (offsetof(struct btrfs_file_extent_item, disk_bytenr))
136static inline u32 BTRFS_MAX_INLINE_DATA_SIZE(const struct btrfs_fs_info *info)
137{
138 return BTRFS_MAX_ITEM_SIZE(info) -
139 BTRFS_FILE_EXTENT_INLINE_DATA_START;
140}
141
142static inline u32 BTRFS_MAX_XATTR_SIZE(const struct btrfs_fs_info *info)
143{
144 return BTRFS_MAX_ITEM_SIZE(info) - sizeof(struct btrfs_dir_item);
145}
146
Marek Behún4eff4262017-09-03 17:00:26 +0200147/*
148 * File system states
149 */
150#define BTRFS_FS_STATE_ERROR 0
151#define BTRFS_FS_STATE_REMOUNTING 1
152#define BTRFS_FS_STATE_TRANS_ABORTED 2
153#define BTRFS_FS_STATE_DEV_REPLACING 3
154#define BTRFS_FS_STATE_DUMMY_FS_INFO 4
155
Qu Wenruoc43c5292020-06-24 18:02:52 +0200156#define read_eb_member(eb, ptr, type, member, result) ( \
157 read_extent_buffer(eb, (char *)(result), \
158 ((unsigned long)(ptr)) + \
159 offsetof(type, member), \
160 sizeof(((type *)0)->member)))
161
162#define write_eb_member(eb, ptr, type, member, result) ( \
163 write_extent_buffer(eb, (char *)(result), \
164 ((unsigned long)(ptr)) + \
165 offsetof(type, member), \
166 sizeof(((type *)0)->member)))
167
Qu Wenruo6925bd22020-06-24 18:02:53 +0200168#define BTRFS_SETGET_HEADER_FUNCS(name, type, member, bits) \
169static inline u##bits btrfs_##name(const struct extent_buffer *eb) \
170{ \
171 const struct btrfs_header *h = (struct btrfs_header *)eb->data; \
172 return le##bits##_to_cpu(h->member); \
173} \
174static inline void btrfs_set_##name(struct extent_buffer *eb, \
175 u##bits val) \
176{ \
177 struct btrfs_header *h = (struct btrfs_header *)eb->data; \
178 h->member = cpu_to_le##bits(val); \
179}
180
181#define BTRFS_SETGET_FUNCS(name, type, member, bits) \
182static inline u##bits btrfs_##name(const struct extent_buffer *eb, \
183 const type *s) \
184{ \
185 unsigned long offset = (unsigned long)s; \
186 const type *p = (type *) (eb->data + offset); \
187 return get_unaligned_le##bits(&p->member); \
188} \
189static inline void btrfs_set_##name(struct extent_buffer *eb, \
190 type *s, u##bits val) \
191{ \
192 unsigned long offset = (unsigned long)s; \
193 type *p = (type *) (eb->data + offset); \
194 put_unaligned_le##bits(val, &p->member); \
195}
196
Qu Wenruo1a618082020-06-24 18:02:49 +0200197#define BTRFS_SETGET_STACK_FUNCS(name, type, member, bits) \
198static inline u##bits btrfs_##name(const type *s) \
199{ \
200 return le##bits##_to_cpu(s->member); \
201} \
202static inline void btrfs_set_##name(type *s, u##bits val) \
203{ \
204 s->member = cpu_to_le##bits(val); \
205}
206
Qu Wenruo6925bd22020-06-24 18:02:53 +0200207BTRFS_SETGET_FUNCS(device_type, struct btrfs_dev_item, type, 64);
208BTRFS_SETGET_FUNCS(device_total_bytes, struct btrfs_dev_item, total_bytes, 64);
209BTRFS_SETGET_FUNCS(device_bytes_used, struct btrfs_dev_item, bytes_used, 64);
210BTRFS_SETGET_FUNCS(device_io_align, struct btrfs_dev_item, io_align, 32);
211BTRFS_SETGET_FUNCS(device_io_width, struct btrfs_dev_item, io_width, 32);
212BTRFS_SETGET_FUNCS(device_start_offset, struct btrfs_dev_item,
213 start_offset, 64);
214BTRFS_SETGET_FUNCS(device_sector_size, struct btrfs_dev_item, sector_size, 32);
215BTRFS_SETGET_FUNCS(device_id, struct btrfs_dev_item, devid, 64);
216BTRFS_SETGET_FUNCS(device_group, struct btrfs_dev_item, dev_group, 32);
217BTRFS_SETGET_FUNCS(device_seek_speed, struct btrfs_dev_item, seek_speed, 8);
218BTRFS_SETGET_FUNCS(device_bandwidth, struct btrfs_dev_item, bandwidth, 8);
219BTRFS_SETGET_FUNCS(device_generation, struct btrfs_dev_item, generation, 64);
Marek Behún4eff4262017-09-03 17:00:26 +0200220
Qu Wenruo6925bd22020-06-24 18:02:53 +0200221BTRFS_SETGET_STACK_FUNCS(stack_device_type, struct btrfs_dev_item, type, 64);
222BTRFS_SETGET_STACK_FUNCS(stack_device_total_bytes, struct btrfs_dev_item,
223 total_bytes, 64);
224BTRFS_SETGET_STACK_FUNCS(stack_device_bytes_used, struct btrfs_dev_item,
225 bytes_used, 64);
226BTRFS_SETGET_STACK_FUNCS(stack_device_io_align, struct btrfs_dev_item,
227 io_align, 32);
228BTRFS_SETGET_STACK_FUNCS(stack_device_io_width, struct btrfs_dev_item,
229 io_width, 32);
230BTRFS_SETGET_STACK_FUNCS(stack_device_sector_size, struct btrfs_dev_item,
231 sector_size, 32);
232BTRFS_SETGET_STACK_FUNCS(stack_device_id, struct btrfs_dev_item, devid, 64);
233BTRFS_SETGET_STACK_FUNCS(stack_device_group, struct btrfs_dev_item,
234 dev_group, 32);
235BTRFS_SETGET_STACK_FUNCS(stack_device_seek_speed, struct btrfs_dev_item,
236 seek_speed, 8);
237BTRFS_SETGET_STACK_FUNCS(stack_device_bandwidth, struct btrfs_dev_item,
238 bandwidth, 8);
239BTRFS_SETGET_STACK_FUNCS(stack_device_generation, struct btrfs_dev_item,
240 generation, 64);
Marek Behún4eff4262017-09-03 17:00:26 +0200241
Qu Wenruo6925bd22020-06-24 18:02:53 +0200242static inline char *btrfs_device_uuid(struct btrfs_dev_item *d)
243{
244 return (char *)d + offsetof(struct btrfs_dev_item, uuid);
245}
Marek Behún4eff4262017-09-03 17:00:26 +0200246
Qu Wenruo6925bd22020-06-24 18:02:53 +0200247static inline char *btrfs_device_fsid(struct btrfs_dev_item *d)
248{
249 return (char *)d + offsetof(struct btrfs_dev_item, fsid);
250}
Qu Wenruoc43c5292020-06-24 18:02:52 +0200251
Qu Wenruo6925bd22020-06-24 18:02:53 +0200252BTRFS_SETGET_FUNCS(chunk_length, struct btrfs_chunk, length, 64);
253BTRFS_SETGET_FUNCS(chunk_owner, struct btrfs_chunk, owner, 64);
254BTRFS_SETGET_FUNCS(chunk_stripe_len, struct btrfs_chunk, stripe_len, 64);
255BTRFS_SETGET_FUNCS(chunk_io_align, struct btrfs_chunk, io_align, 32);
256BTRFS_SETGET_FUNCS(chunk_io_width, struct btrfs_chunk, io_width, 32);
257BTRFS_SETGET_FUNCS(chunk_sector_size, struct btrfs_chunk, sector_size, 32);
258BTRFS_SETGET_FUNCS(chunk_type, struct btrfs_chunk, type, 64);
259BTRFS_SETGET_FUNCS(chunk_num_stripes, struct btrfs_chunk, num_stripes, 16);
260BTRFS_SETGET_FUNCS(chunk_sub_stripes, struct btrfs_chunk, sub_stripes, 16);
261BTRFS_SETGET_FUNCS(stripe_devid, struct btrfs_stripe, devid, 64);
262BTRFS_SETGET_FUNCS(stripe_offset, struct btrfs_stripe, offset, 64);
Qu Wenruoc43c5292020-06-24 18:02:52 +0200263
Qu Wenruo6925bd22020-06-24 18:02:53 +0200264static inline char *btrfs_stripe_dev_uuid(struct btrfs_stripe *s)
265{
266 return (char *)s + offsetof(struct btrfs_stripe, dev_uuid);
267}
Qu Wenruoc43c5292020-06-24 18:02:52 +0200268
Qu Wenruo6925bd22020-06-24 18:02:53 +0200269BTRFS_SETGET_STACK_FUNCS(stack_chunk_length, struct btrfs_chunk, length, 64);
270BTRFS_SETGET_STACK_FUNCS(stack_chunk_owner, struct btrfs_chunk, owner, 64);
271BTRFS_SETGET_STACK_FUNCS(stack_chunk_stripe_len, struct btrfs_chunk,
272 stripe_len, 64);
273BTRFS_SETGET_STACK_FUNCS(stack_chunk_io_align, struct btrfs_chunk,
274 io_align, 32);
275BTRFS_SETGET_STACK_FUNCS(stack_chunk_io_width, struct btrfs_chunk,
276 io_width, 32);
277BTRFS_SETGET_STACK_FUNCS(stack_chunk_sector_size, struct btrfs_chunk,
278 sector_size, 32);
279BTRFS_SETGET_STACK_FUNCS(stack_chunk_type, struct btrfs_chunk, type, 64);
280BTRFS_SETGET_STACK_FUNCS(stack_chunk_num_stripes, struct btrfs_chunk,
281 num_stripes, 16);
282BTRFS_SETGET_STACK_FUNCS(stack_chunk_sub_stripes, struct btrfs_chunk,
283 sub_stripes, 16);
284BTRFS_SETGET_STACK_FUNCS(stack_stripe_devid, struct btrfs_stripe, devid, 64);
285BTRFS_SETGET_STACK_FUNCS(stack_stripe_offset, struct btrfs_stripe, offset, 64);
Qu Wenruoc43c5292020-06-24 18:02:52 +0200286
Qu Wenruo6925bd22020-06-24 18:02:53 +0200287static inline struct btrfs_stripe *btrfs_stripe_nr(struct btrfs_chunk *c,
288 int nr)
289{
290 unsigned long offset = (unsigned long)c;
291 offset += offsetof(struct btrfs_chunk, stripe);
292 offset += nr * sizeof(struct btrfs_stripe);
293 return (struct btrfs_stripe *)offset;
294}
Qu Wenruoc43c5292020-06-24 18:02:52 +0200295
Qu Wenruo6925bd22020-06-24 18:02:53 +0200296static inline char *btrfs_stripe_dev_uuid_nr(struct btrfs_chunk *c, int nr)
297{
298 return btrfs_stripe_dev_uuid(btrfs_stripe_nr(c, nr));
299}
Qu Wenruoc43c5292020-06-24 18:02:52 +0200300
Qu Wenruo6925bd22020-06-24 18:02:53 +0200301static inline u64 btrfs_stripe_offset_nr(struct extent_buffer *eb,
302 struct btrfs_chunk *c, int nr)
303{
304 return btrfs_stripe_offset(eb, btrfs_stripe_nr(c, nr));
305}
Qu Wenruoc43c5292020-06-24 18:02:52 +0200306
Qu Wenruo6925bd22020-06-24 18:02:53 +0200307static inline void btrfs_set_stripe_offset_nr(struct extent_buffer *eb,
308 struct btrfs_chunk *c, int nr,
309 u64 val)
310{
311 btrfs_set_stripe_offset(eb, btrfs_stripe_nr(c, nr), val);
312}
Qu Wenruoc43c5292020-06-24 18:02:52 +0200313
Qu Wenruo6925bd22020-06-24 18:02:53 +0200314static inline u64 btrfs_stripe_devid_nr(struct extent_buffer *eb,
315 struct btrfs_chunk *c, int nr)
316{
317 return btrfs_stripe_devid(eb, btrfs_stripe_nr(c, nr));
318}
Qu Wenruoc43c5292020-06-24 18:02:52 +0200319
Qu Wenruo6925bd22020-06-24 18:02:53 +0200320static inline void btrfs_set_stripe_devid_nr(struct extent_buffer *eb,
321 struct btrfs_chunk *c, int nr,
322 u64 val)
323{
324 btrfs_set_stripe_devid(eb, btrfs_stripe_nr(c, nr), val);
325}
Qu Wenruoc43c5292020-06-24 18:02:52 +0200326
Qu Wenruo6925bd22020-06-24 18:02:53 +0200327/* struct btrfs_block_group_item */
328BTRFS_SETGET_STACK_FUNCS(block_group_used, struct btrfs_block_group_item,
329 used, 64);
330BTRFS_SETGET_FUNCS(disk_block_group_used, struct btrfs_block_group_item,
331 used, 64);
332BTRFS_SETGET_STACK_FUNCS(block_group_chunk_objectid,
333 struct btrfs_block_group_item, chunk_objectid, 64);
Marek Behún4eff4262017-09-03 17:00:26 +0200334
Qu Wenruo6925bd22020-06-24 18:02:53 +0200335BTRFS_SETGET_FUNCS(disk_block_group_chunk_objectid,
336 struct btrfs_block_group_item, chunk_objectid, 64);
337BTRFS_SETGET_FUNCS(disk_block_group_flags,
338 struct btrfs_block_group_item, flags, 64);
339BTRFS_SETGET_STACK_FUNCS(block_group_flags,
340 struct btrfs_block_group_item, flags, 64);
341
342/* struct btrfs_free_space_info */
343BTRFS_SETGET_FUNCS(free_space_extent_count, struct btrfs_free_space_info,
344 extent_count, 32);
345BTRFS_SETGET_FUNCS(free_space_flags, struct btrfs_free_space_info, flags, 32);
346
347/* struct btrfs_inode_ref */
348BTRFS_SETGET_FUNCS(inode_ref_name_len, struct btrfs_inode_ref, name_len, 16);
349BTRFS_SETGET_STACK_FUNCS(stack_inode_ref_name_len, struct btrfs_inode_ref, name_len, 16);
350BTRFS_SETGET_FUNCS(inode_ref_index, struct btrfs_inode_ref, index, 64);
351
352/* struct btrfs_inode_extref */
353BTRFS_SETGET_FUNCS(inode_extref_parent, struct btrfs_inode_extref,
354 parent_objectid, 64);
355BTRFS_SETGET_FUNCS(inode_extref_name_len, struct btrfs_inode_extref,
356 name_len, 16);
357BTRFS_SETGET_FUNCS(inode_extref_index, struct btrfs_inode_extref, index, 64);
358
359/* struct btrfs_inode_item */
360BTRFS_SETGET_FUNCS(inode_generation, struct btrfs_inode_item, generation, 64);
361BTRFS_SETGET_FUNCS(inode_sequence, struct btrfs_inode_item, sequence, 64);
362BTRFS_SETGET_FUNCS(inode_transid, struct btrfs_inode_item, transid, 64);
363BTRFS_SETGET_FUNCS(inode_size, struct btrfs_inode_item, size, 64);
364BTRFS_SETGET_FUNCS(inode_nbytes, struct btrfs_inode_item, nbytes, 64);
365BTRFS_SETGET_FUNCS(inode_block_group, struct btrfs_inode_item, block_group, 64);
366BTRFS_SETGET_FUNCS(inode_nlink, struct btrfs_inode_item, nlink, 32);
367BTRFS_SETGET_FUNCS(inode_uid, struct btrfs_inode_item, uid, 32);
368BTRFS_SETGET_FUNCS(inode_gid, struct btrfs_inode_item, gid, 32);
369BTRFS_SETGET_FUNCS(inode_mode, struct btrfs_inode_item, mode, 32);
370BTRFS_SETGET_FUNCS(inode_rdev, struct btrfs_inode_item, rdev, 64);
371BTRFS_SETGET_FUNCS(inode_flags, struct btrfs_inode_item, flags, 64);
372
373BTRFS_SETGET_STACK_FUNCS(stack_inode_generation,
374 struct btrfs_inode_item, generation, 64);
375BTRFS_SETGET_STACK_FUNCS(stack_inode_sequence,
376 struct btrfs_inode_item, sequence, 64);
377BTRFS_SETGET_STACK_FUNCS(stack_inode_transid,
378 struct btrfs_inode_item, transid, 64);
379BTRFS_SETGET_STACK_FUNCS(stack_inode_size,
380 struct btrfs_inode_item, size, 64);
381BTRFS_SETGET_STACK_FUNCS(stack_inode_nbytes,
382 struct btrfs_inode_item, nbytes, 64);
383BTRFS_SETGET_STACK_FUNCS(stack_inode_block_group,
384 struct btrfs_inode_item, block_group, 64);
385BTRFS_SETGET_STACK_FUNCS(stack_inode_nlink,
386 struct btrfs_inode_item, nlink, 32);
387BTRFS_SETGET_STACK_FUNCS(stack_inode_uid,
388 struct btrfs_inode_item, uid, 32);
389BTRFS_SETGET_STACK_FUNCS(stack_inode_gid,
390 struct btrfs_inode_item, gid, 32);
391BTRFS_SETGET_STACK_FUNCS(stack_inode_mode,
392 struct btrfs_inode_item, mode, 32);
393BTRFS_SETGET_STACK_FUNCS(stack_inode_rdev,
394 struct btrfs_inode_item, rdev, 64);
395BTRFS_SETGET_STACK_FUNCS(stack_inode_flags,
396 struct btrfs_inode_item, flags, 64);
397
398static inline struct btrfs_timespec *
399btrfs_inode_atime(struct btrfs_inode_item *inode_item)
400{
401 unsigned long ptr = (unsigned long)inode_item;
402 ptr += offsetof(struct btrfs_inode_item, atime);
403 return (struct btrfs_timespec *)ptr;
Marek Behún4eff4262017-09-03 17:00:26 +0200404}
405
Qu Wenruo6925bd22020-06-24 18:02:53 +0200406static inline struct btrfs_timespec *
407btrfs_inode_mtime(struct btrfs_inode_item *inode_item)
Marek Behún4eff4262017-09-03 17:00:26 +0200408{
Qu Wenruo6925bd22020-06-24 18:02:53 +0200409 unsigned long ptr = (unsigned long)inode_item;
410 ptr += offsetof(struct btrfs_inode_item, mtime);
411 return (struct btrfs_timespec *)ptr;
412}
Marek Behún4eff4262017-09-03 17:00:26 +0200413
Qu Wenruo6925bd22020-06-24 18:02:53 +0200414static inline struct btrfs_timespec *
415btrfs_inode_ctime(struct btrfs_inode_item *inode_item)
416{
417 unsigned long ptr = (unsigned long)inode_item;
418 ptr += offsetof(struct btrfs_inode_item, ctime);
419 return (struct btrfs_timespec *)ptr;
420}
Marek Behún4eff4262017-09-03 17:00:26 +0200421
Qu Wenruo6925bd22020-06-24 18:02:53 +0200422static inline struct btrfs_timespec *
423btrfs_inode_otime(struct btrfs_inode_item *inode_item)
424{
425 unsigned long ptr = (unsigned long)inode_item;
426 ptr += offsetof(struct btrfs_inode_item, otime);
427 return (struct btrfs_timespec *)ptr;
428}
Marek Behún4eff4262017-09-03 17:00:26 +0200429
Qu Wenruo6925bd22020-06-24 18:02:53 +0200430BTRFS_SETGET_FUNCS(timespec_sec, struct btrfs_timespec, sec, 64);
431BTRFS_SETGET_FUNCS(timespec_nsec, struct btrfs_timespec, nsec, 32);
432BTRFS_SETGET_STACK_FUNCS(stack_timespec_sec, struct btrfs_timespec,
433 sec, 64);
434BTRFS_SETGET_STACK_FUNCS(stack_timespec_nsec, struct btrfs_timespec,
435 nsec, 32);
Pierre Bourdon9fffe322019-04-13 23:50:49 +0200436
Qu Wenruo6925bd22020-06-24 18:02:53 +0200437/* struct btrfs_dev_extent */
438BTRFS_SETGET_FUNCS(dev_extent_chunk_tree, struct btrfs_dev_extent,
439 chunk_tree, 64);
440BTRFS_SETGET_FUNCS(dev_extent_chunk_objectid, struct btrfs_dev_extent,
441 chunk_objectid, 64);
442BTRFS_SETGET_FUNCS(dev_extent_chunk_offset, struct btrfs_dev_extent,
443 chunk_offset, 64);
444BTRFS_SETGET_FUNCS(dev_extent_length, struct btrfs_dev_extent, length, 64);
445
446BTRFS_SETGET_STACK_FUNCS(stack_dev_extent_length, struct btrfs_dev_extent,
447 length, 64);
448
449static inline u8 *btrfs_dev_extent_chunk_tree_uuid(struct btrfs_dev_extent *dev)
450{
451 unsigned long ptr = offsetof(struct btrfs_dev_extent, chunk_tree_uuid);
452 return (u8 *)((unsigned long)dev + ptr);
Marek Behún4eff4262017-09-03 17:00:26 +0200453}
454
Qu Wenruo6925bd22020-06-24 18:02:53 +0200455
456/* struct btrfs_extent_item */
457BTRFS_SETGET_FUNCS(extent_refs, struct btrfs_extent_item, refs, 64);
458BTRFS_SETGET_STACK_FUNCS(stack_extent_refs, struct btrfs_extent_item, refs, 64);
459BTRFS_SETGET_FUNCS(extent_generation, struct btrfs_extent_item,
460 generation, 64);
461BTRFS_SETGET_FUNCS(extent_flags, struct btrfs_extent_item, flags, 64);
462BTRFS_SETGET_STACK_FUNCS(stack_extent_flags, struct btrfs_extent_item, flags, 64);
463
464BTRFS_SETGET_FUNCS(extent_refs_v0, struct btrfs_extent_item_v0, refs, 32);
465
466BTRFS_SETGET_FUNCS(tree_block_level, struct btrfs_tree_block_info, level, 8);
467
468static inline void btrfs_tree_block_key(struct extent_buffer *eb,
469 struct btrfs_tree_block_info *item,
470 struct btrfs_disk_key *key)
Marek Behún4eff4262017-09-03 17:00:26 +0200471{
Qu Wenruo6925bd22020-06-24 18:02:53 +0200472 read_eb_member(eb, item, struct btrfs_tree_block_info, key, key);
Marek Behún4eff4262017-09-03 17:00:26 +0200473}
474
Qu Wenruo6925bd22020-06-24 18:02:53 +0200475static inline void btrfs_set_tree_block_key(struct extent_buffer *eb,
476 struct btrfs_tree_block_info *item,
477 struct btrfs_disk_key *key)
Marek Behún4eff4262017-09-03 17:00:26 +0200478{
Qu Wenruo6925bd22020-06-24 18:02:53 +0200479 write_eb_member(eb, item, struct btrfs_tree_block_info, key, key);
Marek Behún4eff4262017-09-03 17:00:26 +0200480}
481
Qu Wenruo6925bd22020-06-24 18:02:53 +0200482BTRFS_SETGET_FUNCS(extent_data_ref_root, struct btrfs_extent_data_ref,
483 root, 64);
484BTRFS_SETGET_FUNCS(extent_data_ref_objectid, struct btrfs_extent_data_ref,
485 objectid, 64);
486BTRFS_SETGET_FUNCS(extent_data_ref_offset, struct btrfs_extent_data_ref,
487 offset, 64);
488BTRFS_SETGET_FUNCS(extent_data_ref_count, struct btrfs_extent_data_ref,
489 count, 32);
490
491BTRFS_SETGET_FUNCS(shared_data_ref_count, struct btrfs_shared_data_ref,
492 count, 32);
493
494BTRFS_SETGET_FUNCS(extent_inline_ref_type, struct btrfs_extent_inline_ref,
495 type, 8);
496BTRFS_SETGET_FUNCS(extent_inline_ref_offset, struct btrfs_extent_inline_ref,
497 offset, 64);
498BTRFS_SETGET_STACK_FUNCS(stack_extent_inline_ref_type,
499 struct btrfs_extent_inline_ref, type, 8);
500BTRFS_SETGET_STACK_FUNCS(stack_extent_inline_ref_offset,
501 struct btrfs_extent_inline_ref, offset, 64);
502
503static inline u32 btrfs_extent_inline_ref_size(int type)
Marek Behún4eff4262017-09-03 17:00:26 +0200504{
Qu Wenruo6925bd22020-06-24 18:02:53 +0200505 if (type == BTRFS_TREE_BLOCK_REF_KEY ||
506 type == BTRFS_SHARED_BLOCK_REF_KEY)
507 return sizeof(struct btrfs_extent_inline_ref);
508 if (type == BTRFS_SHARED_DATA_REF_KEY)
509 return sizeof(struct btrfs_shared_data_ref) +
510 sizeof(struct btrfs_extent_inline_ref);
511 if (type == BTRFS_EXTENT_DATA_REF_KEY)
512 return sizeof(struct btrfs_extent_data_ref) +
513 offsetof(struct btrfs_extent_inline_ref, offset);
514 BUG();
515 return 0;
Marek Behún4eff4262017-09-03 17:00:26 +0200516}
517
Qu Wenruo6925bd22020-06-24 18:02:53 +0200518BTRFS_SETGET_FUNCS(ref_root_v0, struct btrfs_extent_ref_v0, root, 64);
519BTRFS_SETGET_FUNCS(ref_generation_v0, struct btrfs_extent_ref_v0,
520 generation, 64);
521BTRFS_SETGET_FUNCS(ref_objectid_v0, struct btrfs_extent_ref_v0, objectid, 64);
522BTRFS_SETGET_FUNCS(ref_count_v0, struct btrfs_extent_ref_v0, count, 32);
Marek Behún4eff4262017-09-03 17:00:26 +0200523
Qu Wenruo6925bd22020-06-24 18:02:53 +0200524/* struct btrfs_node */
525BTRFS_SETGET_FUNCS(key_blockptr, struct btrfs_key_ptr, blockptr, 64);
526BTRFS_SETGET_FUNCS(key_generation, struct btrfs_key_ptr, generation, 64);
Marek Behún4eff4262017-09-03 17:00:26 +0200527
Qu Wenruo6925bd22020-06-24 18:02:53 +0200528static inline u64 btrfs_node_blockptr(struct extent_buffer *eb, int nr)
529{
530 unsigned long ptr;
531 ptr = offsetof(struct btrfs_node, ptrs) +
532 sizeof(struct btrfs_key_ptr) * nr;
533 return btrfs_key_blockptr(eb, (struct btrfs_key_ptr *)ptr);
534}
535
536static inline void btrfs_set_node_blockptr(struct extent_buffer *eb,
537 int nr, u64 val)
538{
539 unsigned long ptr;
540 ptr = offsetof(struct btrfs_node, ptrs) +
541 sizeof(struct btrfs_key_ptr) * nr;
542 btrfs_set_key_blockptr(eb, (struct btrfs_key_ptr *)ptr, val);
543}
544
545static inline u64 btrfs_node_ptr_generation(struct extent_buffer *eb, int nr)
546{
547 unsigned long ptr;
548 ptr = offsetof(struct btrfs_node, ptrs) +
549 sizeof(struct btrfs_key_ptr) * nr;
550 return btrfs_key_generation(eb, (struct btrfs_key_ptr *)ptr);
551}
552
553static inline void btrfs_set_node_ptr_generation(struct extent_buffer *eb,
554 int nr, u64 val)
555{
556 unsigned long ptr;
557 ptr = offsetof(struct btrfs_node, ptrs) +
558 sizeof(struct btrfs_key_ptr) * nr;
559 btrfs_set_key_generation(eb, (struct btrfs_key_ptr *)ptr, val);
560}
561
562static inline unsigned long btrfs_node_key_ptr_offset(int nr)
563{
564 return offsetof(struct btrfs_node, ptrs) +
565 sizeof(struct btrfs_key_ptr) * nr;
566}
567
568static inline void btrfs_node_key(struct extent_buffer *eb,
569 struct btrfs_disk_key *disk_key, int nr)
570{
571 unsigned long ptr;
572 ptr = btrfs_node_key_ptr_offset(nr);
573 read_eb_member(eb, (struct btrfs_key_ptr *)ptr,
574 struct btrfs_key_ptr, key, disk_key);
575}
576
577static inline void btrfs_set_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 write_eb_member(eb, (struct btrfs_key_ptr *)ptr,
583 struct btrfs_key_ptr, key, disk_key);
584}
585
586/* struct btrfs_item */
587BTRFS_SETGET_FUNCS(item_offset, struct btrfs_item, offset, 32);
588BTRFS_SETGET_FUNCS(item_size, struct btrfs_item, size, 32);
589
590static inline unsigned long btrfs_item_nr_offset(int nr)
591{
592 return offsetof(struct btrfs_leaf, items) +
593 sizeof(struct btrfs_item) * nr;
594}
595
596static inline struct btrfs_item *btrfs_item_nr(int nr)
597{
598 return (struct btrfs_item *)btrfs_item_nr_offset(nr);
599}
600
601static inline u32 btrfs_item_end(struct extent_buffer *eb,
602 struct btrfs_item *item)
603{
604 return btrfs_item_offset(eb, item) + btrfs_item_size(eb, item);
605}
606
607static inline u32 btrfs_item_end_nr(struct extent_buffer *eb, int nr)
608{
609 return btrfs_item_end(eb, btrfs_item_nr(nr));
610}
611
612static inline u32 btrfs_item_offset_nr(const struct extent_buffer *eb, int nr)
613{
614 return btrfs_item_offset(eb, btrfs_item_nr(nr));
615}
616
617static inline u32 btrfs_item_size_nr(struct extent_buffer *eb, int nr)
618{
619 return btrfs_item_size(eb, btrfs_item_nr(nr));
620}
621
622static inline void btrfs_item_key(struct extent_buffer *eb,
623 struct btrfs_disk_key *disk_key, int nr)
624{
625 struct btrfs_item *item = btrfs_item_nr(nr);
626 read_eb_member(eb, item, struct btrfs_item, key, disk_key);
627}
628
629static inline void btrfs_set_item_key(struct extent_buffer *eb,
630 struct btrfs_disk_key *disk_key, int nr)
631{
632 struct btrfs_item *item = btrfs_item_nr(nr);
633 write_eb_member(eb, item, struct btrfs_item, key, disk_key);
634}
635
636BTRFS_SETGET_FUNCS(dir_log_end, struct btrfs_dir_log_item, end, 64);
637
638/*
639 * struct btrfs_root_ref
640 */
641BTRFS_SETGET_FUNCS(root_ref_dirid, struct btrfs_root_ref, dirid, 64);
642BTRFS_SETGET_FUNCS(root_ref_sequence, struct btrfs_root_ref, sequence, 64);
643BTRFS_SETGET_FUNCS(root_ref_name_len, struct btrfs_root_ref, name_len, 16);
644
645BTRFS_SETGET_STACK_FUNCS(stack_root_ref_dirid, struct btrfs_root_ref, dirid, 64);
646BTRFS_SETGET_STACK_FUNCS(stack_root_ref_sequence, struct btrfs_root_ref, sequence, 64);
647BTRFS_SETGET_STACK_FUNCS(stack_root_ref_name_len, struct btrfs_root_ref, name_len, 16);
648
649/* struct btrfs_dir_item */
650BTRFS_SETGET_FUNCS(dir_data_len, struct btrfs_dir_item, data_len, 16);
651BTRFS_SETGET_FUNCS(dir_type, struct btrfs_dir_item, type, 8);
652BTRFS_SETGET_FUNCS(dir_name_len, struct btrfs_dir_item, name_len, 16);
653BTRFS_SETGET_FUNCS(dir_transid, struct btrfs_dir_item, transid, 64);
654
655BTRFS_SETGET_STACK_FUNCS(stack_dir_data_len, struct btrfs_dir_item, data_len, 16);
656BTRFS_SETGET_STACK_FUNCS(stack_dir_type, struct btrfs_dir_item, type, 8);
657BTRFS_SETGET_STACK_FUNCS(stack_dir_name_len, struct btrfs_dir_item, name_len, 16);
658BTRFS_SETGET_STACK_FUNCS(stack_dir_transid, struct btrfs_dir_item, transid, 64);
659
660static inline void btrfs_dir_item_key(struct extent_buffer *eb,
661 struct btrfs_dir_item *item,
662 struct btrfs_disk_key *key)
663{
664 read_eb_member(eb, item, struct btrfs_dir_item, location, key);
665}
666
667static inline void btrfs_set_dir_item_key(struct extent_buffer *eb,
668 struct btrfs_dir_item *item,
669 struct btrfs_disk_key *key)
670{
671 write_eb_member(eb, item, struct btrfs_dir_item, location, key);
672}
673
674/* struct btrfs_free_space_header */
675BTRFS_SETGET_FUNCS(free_space_entries, struct btrfs_free_space_header,
676 num_entries, 64);
677BTRFS_SETGET_FUNCS(free_space_bitmaps, struct btrfs_free_space_header,
678 num_bitmaps, 64);
679BTRFS_SETGET_FUNCS(free_space_generation, struct btrfs_free_space_header,
680 generation, 64);
681
682static inline void btrfs_free_space_key(struct extent_buffer *eb,
683 struct btrfs_free_space_header *h,
684 struct btrfs_disk_key *key)
685{
686 read_eb_member(eb, h, struct btrfs_free_space_header, location, key);
687}
688
689static inline void btrfs_set_free_space_key(struct extent_buffer *eb,
690 struct btrfs_free_space_header *h,
691 struct btrfs_disk_key *key)
692{
693 write_eb_member(eb, h, struct btrfs_free_space_header, location, key);
694}
695
696/* struct btrfs_disk_key */
697BTRFS_SETGET_STACK_FUNCS(disk_key_objectid, struct btrfs_disk_key,
698 objectid, 64);
699BTRFS_SETGET_STACK_FUNCS(disk_key_offset, struct btrfs_disk_key, offset, 64);
700BTRFS_SETGET_STACK_FUNCS(disk_key_type, struct btrfs_disk_key, type, 8);
701
702static inline void btrfs_disk_key_to_cpu(struct btrfs_key *cpu,
703 struct btrfs_disk_key *disk)
704{
705 cpu->offset = le64_to_cpu(disk->offset);
706 cpu->type = disk->type;
707 cpu->objectid = le64_to_cpu(disk->objectid);
708}
709
710static inline void btrfs_cpu_key_to_disk(struct btrfs_disk_key *disk,
711 const struct btrfs_key *cpu)
712{
713 disk->offset = cpu_to_le64(cpu->offset);
714 disk->type = cpu->type;
715 disk->objectid = cpu_to_le64(cpu->objectid);
716}
717
718static inline void btrfs_node_key_to_cpu(struct extent_buffer *eb,
719 struct btrfs_key *key, int nr)
720{
721 struct btrfs_disk_key disk_key;
722 btrfs_node_key(eb, &disk_key, nr);
723 btrfs_disk_key_to_cpu(key, &disk_key);
724}
725
726static inline void btrfs_item_key_to_cpu(struct extent_buffer *eb,
727 struct btrfs_key *key, int nr)
728{
729 struct btrfs_disk_key disk_key;
730 btrfs_item_key(eb, &disk_key, nr);
731 btrfs_disk_key_to_cpu(key, &disk_key);
732}
733
734static inline void btrfs_dir_item_key_to_cpu(struct extent_buffer *eb,
735 struct btrfs_dir_item *item,
736 struct btrfs_key *key)
737{
738 struct btrfs_disk_key disk_key;
739 btrfs_dir_item_key(eb, item, &disk_key);
740 btrfs_disk_key_to_cpu(key, &disk_key);
741}
742
743/* struct btrfs_header */
744BTRFS_SETGET_HEADER_FUNCS(header_bytenr, struct btrfs_header, bytenr, 64);
745BTRFS_SETGET_HEADER_FUNCS(header_generation, struct btrfs_header,
746 generation, 64);
747BTRFS_SETGET_HEADER_FUNCS(header_owner, struct btrfs_header, owner, 64);
748BTRFS_SETGET_HEADER_FUNCS(header_nritems, struct btrfs_header, nritems, 32);
749BTRFS_SETGET_HEADER_FUNCS(header_flags, struct btrfs_header, flags, 64);
750BTRFS_SETGET_HEADER_FUNCS(header_level, struct btrfs_header, level, 8);
751BTRFS_SETGET_STACK_FUNCS(stack_header_bytenr, struct btrfs_header, bytenr, 64);
752BTRFS_SETGET_STACK_FUNCS(stack_header_nritems, struct btrfs_header, nritems,
753 32);
754BTRFS_SETGET_STACK_FUNCS(stack_header_owner, struct btrfs_header, owner, 64);
755BTRFS_SETGET_STACK_FUNCS(stack_header_generation, struct btrfs_header,
756 generation, 64);
757
758static inline int btrfs_header_flag(struct extent_buffer *eb, u64 flag)
759{
760 return (btrfs_header_flags(eb) & flag) == flag;
761}
762
763static inline int btrfs_set_header_flag(struct extent_buffer *eb, u64 flag)
764{
765 u64 flags = btrfs_header_flags(eb);
766 btrfs_set_header_flags(eb, flags | flag);
767 return (flags & flag) == flag;
768}
769
770static inline int btrfs_clear_header_flag(struct extent_buffer *eb, u64 flag)
771{
772 u64 flags = btrfs_header_flags(eb);
773 btrfs_set_header_flags(eb, flags & ~flag);
774 return (flags & flag) == flag;
775}
776
777static inline int btrfs_header_backref_rev(struct extent_buffer *eb)
778{
779 u64 flags = btrfs_header_flags(eb);
780 return flags >> BTRFS_BACKREF_REV_SHIFT;
781}
782
783static inline void btrfs_set_header_backref_rev(struct extent_buffer *eb,
784 int rev)
785{
786 u64 flags = btrfs_header_flags(eb);
787 flags &= ~BTRFS_BACKREF_REV_MASK;
788 flags |= (u64)rev << BTRFS_BACKREF_REV_SHIFT;
789 btrfs_set_header_flags(eb, flags);
790}
791
792static inline unsigned long btrfs_header_fsid(void)
793{
794 return offsetof(struct btrfs_header, fsid);
795}
796
797static inline unsigned long btrfs_header_chunk_tree_uuid(struct extent_buffer *eb)
798{
799 return offsetof(struct btrfs_header, chunk_tree_uuid);
800}
801
802static inline u8 *btrfs_header_csum(struct extent_buffer *eb)
803{
804 unsigned long ptr = offsetof(struct btrfs_header, csum);
805 return (u8 *)ptr;
806}
807
808static inline int btrfs_is_leaf(struct extent_buffer *eb)
809{
810 return (btrfs_header_level(eb) == 0);
811}
812
813/* struct btrfs_root_item */
814BTRFS_SETGET_FUNCS(disk_root_generation, struct btrfs_root_item,
815 generation, 64);
816BTRFS_SETGET_FUNCS(disk_root_refs, struct btrfs_root_item, refs, 32);
817BTRFS_SETGET_FUNCS(disk_root_bytenr, struct btrfs_root_item, bytenr, 64);
818BTRFS_SETGET_FUNCS(disk_root_level, struct btrfs_root_item, level, 8);
819
820BTRFS_SETGET_STACK_FUNCS(root_generation, struct btrfs_root_item,
821 generation, 64);
822BTRFS_SETGET_STACK_FUNCS(root_bytenr, struct btrfs_root_item, bytenr, 64);
823BTRFS_SETGET_STACK_FUNCS(root_level, struct btrfs_root_item, level, 8);
824BTRFS_SETGET_STACK_FUNCS(root_dirid, struct btrfs_root_item, root_dirid, 64);
825BTRFS_SETGET_STACK_FUNCS(root_refs, struct btrfs_root_item, refs, 32);
826BTRFS_SETGET_STACK_FUNCS(root_flags, struct btrfs_root_item, flags, 64);
827BTRFS_SETGET_STACK_FUNCS(root_used, struct btrfs_root_item, bytes_used, 64);
828BTRFS_SETGET_STACK_FUNCS(root_limit, struct btrfs_root_item, byte_limit, 64);
829BTRFS_SETGET_STACK_FUNCS(root_last_snapshot, struct btrfs_root_item,
830 last_snapshot, 64);
831BTRFS_SETGET_STACK_FUNCS(root_generation_v2, struct btrfs_root_item,
832 generation_v2, 64);
833BTRFS_SETGET_STACK_FUNCS(root_ctransid, struct btrfs_root_item,
834 ctransid, 64);
835BTRFS_SETGET_STACK_FUNCS(root_otransid, struct btrfs_root_item,
836 otransid, 64);
837BTRFS_SETGET_STACK_FUNCS(root_stransid, struct btrfs_root_item,
838 stransid, 64);
839BTRFS_SETGET_STACK_FUNCS(root_rtransid, struct btrfs_root_item,
840 rtransid, 64);
841
842static inline struct btrfs_timespec* btrfs_root_ctime(
843 struct btrfs_root_item *root_item)
844{
845 unsigned long ptr = (unsigned long)root_item;
846 ptr += offsetof(struct btrfs_root_item, ctime);
847 return (struct btrfs_timespec *)ptr;
848}
849
850static inline struct btrfs_timespec* btrfs_root_otime(
851 struct btrfs_root_item *root_item)
852{
853 unsigned long ptr = (unsigned long)root_item;
854 ptr += offsetof(struct btrfs_root_item, otime);
855 return (struct btrfs_timespec *)ptr;
856}
857
858static inline struct btrfs_timespec* btrfs_root_stime(
859 struct btrfs_root_item *root_item)
860{
861 unsigned long ptr = (unsigned long)root_item;
862 ptr += offsetof(struct btrfs_root_item, stime);
863 return (struct btrfs_timespec *)ptr;
864}
865
866static inline struct btrfs_timespec* btrfs_root_rtime(
867 struct btrfs_root_item *root_item)
868{
869 unsigned long ptr = (unsigned long)root_item;
870 ptr += offsetof(struct btrfs_root_item, rtime);
871 return (struct btrfs_timespec *)ptr;
872}
873
874/* struct btrfs_root_backup */
875BTRFS_SETGET_STACK_FUNCS(backup_tree_root, struct btrfs_root_backup,
876 tree_root, 64);
877BTRFS_SETGET_STACK_FUNCS(backup_tree_root_gen, struct btrfs_root_backup,
878 tree_root_gen, 64);
879BTRFS_SETGET_STACK_FUNCS(backup_tree_root_level, struct btrfs_root_backup,
880 tree_root_level, 8);
881
882BTRFS_SETGET_STACK_FUNCS(backup_chunk_root, struct btrfs_root_backup,
883 chunk_root, 64);
884BTRFS_SETGET_STACK_FUNCS(backup_chunk_root_gen, struct btrfs_root_backup,
885 chunk_root_gen, 64);
886BTRFS_SETGET_STACK_FUNCS(backup_chunk_root_level, struct btrfs_root_backup,
887 chunk_root_level, 8);
888
889BTRFS_SETGET_STACK_FUNCS(backup_extent_root, struct btrfs_root_backup,
890 extent_root, 64);
891BTRFS_SETGET_STACK_FUNCS(backup_extent_root_gen, struct btrfs_root_backup,
892 extent_root_gen, 64);
893BTRFS_SETGET_STACK_FUNCS(backup_extent_root_level, struct btrfs_root_backup,
894 extent_root_level, 8);
895
896BTRFS_SETGET_STACK_FUNCS(backup_fs_root, struct btrfs_root_backup,
897 fs_root, 64);
898BTRFS_SETGET_STACK_FUNCS(backup_fs_root_gen, struct btrfs_root_backup,
899 fs_root_gen, 64);
900BTRFS_SETGET_STACK_FUNCS(backup_fs_root_level, struct btrfs_root_backup,
901 fs_root_level, 8);
902
903BTRFS_SETGET_STACK_FUNCS(backup_dev_root, struct btrfs_root_backup,
904 dev_root, 64);
905BTRFS_SETGET_STACK_FUNCS(backup_dev_root_gen, struct btrfs_root_backup,
906 dev_root_gen, 64);
907BTRFS_SETGET_STACK_FUNCS(backup_dev_root_level, struct btrfs_root_backup,
908 dev_root_level, 8);
909
910BTRFS_SETGET_STACK_FUNCS(backup_csum_root, struct btrfs_root_backup,
911 csum_root, 64);
912BTRFS_SETGET_STACK_FUNCS(backup_csum_root_gen, struct btrfs_root_backup,
913 csum_root_gen, 64);
914BTRFS_SETGET_STACK_FUNCS(backup_csum_root_level, struct btrfs_root_backup,
915 csum_root_level, 8);
916BTRFS_SETGET_STACK_FUNCS(backup_total_bytes, struct btrfs_root_backup,
917 total_bytes, 64);
918BTRFS_SETGET_STACK_FUNCS(backup_bytes_used, struct btrfs_root_backup,
919 bytes_used, 64);
920BTRFS_SETGET_STACK_FUNCS(backup_num_devices, struct btrfs_root_backup,
921 num_devices, 64);
Qu Wenruo1a618082020-06-24 18:02:49 +0200922
923/* struct btrfs_super_block */
924
925BTRFS_SETGET_STACK_FUNCS(super_bytenr, struct btrfs_super_block, bytenr, 64);
926BTRFS_SETGET_STACK_FUNCS(super_flags, struct btrfs_super_block, flags, 64);
927BTRFS_SETGET_STACK_FUNCS(super_generation, struct btrfs_super_block,
928 generation, 64);
929BTRFS_SETGET_STACK_FUNCS(super_root, struct btrfs_super_block, root, 64);
930BTRFS_SETGET_STACK_FUNCS(super_sys_array_size,
931 struct btrfs_super_block, sys_chunk_array_size, 32);
932BTRFS_SETGET_STACK_FUNCS(super_chunk_root_generation,
933 struct btrfs_super_block, chunk_root_generation, 64);
934BTRFS_SETGET_STACK_FUNCS(super_root_level, struct btrfs_super_block,
935 root_level, 8);
936BTRFS_SETGET_STACK_FUNCS(super_chunk_root, struct btrfs_super_block,
937 chunk_root, 64);
938BTRFS_SETGET_STACK_FUNCS(super_chunk_root_level, struct btrfs_super_block,
939 chunk_root_level, 8);
940BTRFS_SETGET_STACK_FUNCS(super_log_root, struct btrfs_super_block,
941 log_root, 64);
942BTRFS_SETGET_STACK_FUNCS(super_log_root_transid, struct btrfs_super_block,
943 log_root_transid, 64);
944BTRFS_SETGET_STACK_FUNCS(super_log_root_level, struct btrfs_super_block,
945 log_root_level, 8);
946BTRFS_SETGET_STACK_FUNCS(super_total_bytes, struct btrfs_super_block,
947 total_bytes, 64);
948BTRFS_SETGET_STACK_FUNCS(super_bytes_used, struct btrfs_super_block,
949 bytes_used, 64);
950BTRFS_SETGET_STACK_FUNCS(super_sectorsize, struct btrfs_super_block,
951 sectorsize, 32);
952BTRFS_SETGET_STACK_FUNCS(super_nodesize, struct btrfs_super_block,
953 nodesize, 32);
954BTRFS_SETGET_STACK_FUNCS(super_stripesize, struct btrfs_super_block,
955 stripesize, 32);
956BTRFS_SETGET_STACK_FUNCS(super_root_dir, struct btrfs_super_block,
957 root_dir_objectid, 64);
958BTRFS_SETGET_STACK_FUNCS(super_num_devices, struct btrfs_super_block,
959 num_devices, 64);
960BTRFS_SETGET_STACK_FUNCS(super_compat_flags, struct btrfs_super_block,
961 compat_flags, 64);
962BTRFS_SETGET_STACK_FUNCS(super_compat_ro_flags, struct btrfs_super_block,
963 compat_ro_flags, 64);
964BTRFS_SETGET_STACK_FUNCS(super_incompat_flags, struct btrfs_super_block,
965 incompat_flags, 64);
966BTRFS_SETGET_STACK_FUNCS(super_csum_type, struct btrfs_super_block,
967 csum_type, 16);
968BTRFS_SETGET_STACK_FUNCS(super_cache_generation, struct btrfs_super_block,
969 cache_generation, 64);
970BTRFS_SETGET_STACK_FUNCS(super_uuid_tree_generation, struct btrfs_super_block,
971 uuid_tree_generation, 64);
972BTRFS_SETGET_STACK_FUNCS(super_magic, struct btrfs_super_block, magic, 64);
973
Qu Wenruo6925bd22020-06-24 18:02:53 +0200974static inline unsigned long btrfs_leaf_data(struct extent_buffer *l)
975{
976 return offsetof(struct btrfs_leaf, items);
977}
978
979/* struct btrfs_file_extent_item */
980BTRFS_SETGET_FUNCS(file_extent_type, struct btrfs_file_extent_item, type, 8);
981BTRFS_SETGET_STACK_FUNCS(stack_file_extent_type, struct btrfs_file_extent_item, type, 8);
982
983static inline unsigned long btrfs_file_extent_inline_start(struct
984 btrfs_file_extent_item *e)
985{
986 unsigned long offset = (unsigned long)e;
987 offset += offsetof(struct btrfs_file_extent_item, disk_bytenr);
988 return offset;
989}
990
991static inline u32 btrfs_file_extent_calc_inline_size(u32 datasize)
992{
993 return offsetof(struct btrfs_file_extent_item, disk_bytenr) + datasize;
994}
995
996BTRFS_SETGET_FUNCS(file_extent_disk_bytenr, struct btrfs_file_extent_item,
997 disk_bytenr, 64);
998BTRFS_SETGET_STACK_FUNCS(stack_file_extent_disk_bytenr, struct btrfs_file_extent_item,
999 disk_bytenr, 64);
1000BTRFS_SETGET_FUNCS(file_extent_generation, struct btrfs_file_extent_item,
1001 generation, 64);
1002BTRFS_SETGET_STACK_FUNCS(stack_file_extent_generation, struct btrfs_file_extent_item,
1003 generation, 64);
1004BTRFS_SETGET_FUNCS(file_extent_disk_num_bytes, struct btrfs_file_extent_item,
1005 disk_num_bytes, 64);
1006BTRFS_SETGET_FUNCS(file_extent_offset, struct btrfs_file_extent_item,
1007 offset, 64);
1008BTRFS_SETGET_STACK_FUNCS(stack_file_extent_offset, struct btrfs_file_extent_item,
1009 offset, 64);
1010BTRFS_SETGET_FUNCS(file_extent_num_bytes, struct btrfs_file_extent_item,
1011 num_bytes, 64);
1012BTRFS_SETGET_STACK_FUNCS(stack_file_extent_num_bytes, struct btrfs_file_extent_item,
1013 num_bytes, 64);
1014BTRFS_SETGET_FUNCS(file_extent_ram_bytes, struct btrfs_file_extent_item,
1015 ram_bytes, 64);
1016BTRFS_SETGET_STACK_FUNCS(stack_file_extent_ram_bytes, struct btrfs_file_extent_item,
1017 ram_bytes, 64);
1018BTRFS_SETGET_FUNCS(file_extent_compression, struct btrfs_file_extent_item,
1019 compression, 8);
1020BTRFS_SETGET_STACK_FUNCS(stack_file_extent_compression, struct btrfs_file_extent_item,
1021 compression, 8);
1022BTRFS_SETGET_FUNCS(file_extent_encryption, struct btrfs_file_extent_item,
1023 encryption, 8);
1024BTRFS_SETGET_FUNCS(file_extent_other_encoding, struct btrfs_file_extent_item,
1025 other_encoding, 16);
1026
1027/* btrfs_qgroup_status_item */
1028BTRFS_SETGET_FUNCS(qgroup_status_version, struct btrfs_qgroup_status_item,
1029 version, 64);
1030BTRFS_SETGET_FUNCS(qgroup_status_generation, struct btrfs_qgroup_status_item,
1031 generation, 64);
1032BTRFS_SETGET_FUNCS(qgroup_status_flags, struct btrfs_qgroup_status_item,
1033 flags, 64);
1034BTRFS_SETGET_FUNCS(qgroup_status_rescan, struct btrfs_qgroup_status_item,
1035 rescan, 64);
1036
1037BTRFS_SETGET_STACK_FUNCS(stack_qgroup_status_version,
1038 struct btrfs_qgroup_status_item, version, 64);
1039BTRFS_SETGET_STACK_FUNCS(stack_qgroup_status_generation,
1040 struct btrfs_qgroup_status_item, generation, 64);
1041BTRFS_SETGET_STACK_FUNCS(stack_qgroup_status_flags,
1042 struct btrfs_qgroup_status_item, flags, 64);
1043BTRFS_SETGET_STACK_FUNCS(stack_qgroup_status_rescan,
1044 struct btrfs_qgroup_status_item, rescan, 64);
1045
1046/* btrfs_qgroup_info_item */
1047BTRFS_SETGET_FUNCS(qgroup_info_generation, struct btrfs_qgroup_info_item,
1048 generation, 64);
1049BTRFS_SETGET_FUNCS(qgroup_info_referenced, struct btrfs_qgroup_info_item,
1050 rfer, 64);
1051BTRFS_SETGET_FUNCS(qgroup_info_referenced_compressed,
1052 struct btrfs_qgroup_info_item, rfer_cmpr, 64);
1053BTRFS_SETGET_FUNCS(qgroup_info_exclusive, struct btrfs_qgroup_info_item,
1054 excl, 64);
1055BTRFS_SETGET_FUNCS(qgroup_info_exclusive_compressed,
1056 struct btrfs_qgroup_info_item, excl_cmpr, 64);
1057
1058BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_generation,
1059 struct btrfs_qgroup_info_item, generation, 64);
1060BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_referenced,
1061 struct btrfs_qgroup_info_item, rfer, 64);
1062BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_referenced_compressed,
1063 struct btrfs_qgroup_info_item, rfer_cmpr, 64);
1064BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_exclusive,
1065 struct btrfs_qgroup_info_item, excl, 64);
1066BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_exclusive_compressed,
1067 struct btrfs_qgroup_info_item, excl_cmpr, 64);
1068
1069/* btrfs_qgroup_limit_item */
1070BTRFS_SETGET_FUNCS(qgroup_limit_flags, struct btrfs_qgroup_limit_item,
1071 flags, 64);
1072BTRFS_SETGET_FUNCS(qgroup_limit_max_referenced, struct btrfs_qgroup_limit_item,
1073 max_rfer, 64);
1074BTRFS_SETGET_FUNCS(qgroup_limit_max_exclusive, struct btrfs_qgroup_limit_item,
1075 max_excl, 64);
1076BTRFS_SETGET_FUNCS(qgroup_limit_rsv_referenced, struct btrfs_qgroup_limit_item,
1077 rsv_rfer, 64);
1078BTRFS_SETGET_FUNCS(qgroup_limit_rsv_exclusive, struct btrfs_qgroup_limit_item,
1079 rsv_excl, 64);
1080
1081BTRFS_SETGET_STACK_FUNCS(stack_qgroup_limit_flags,
1082 struct btrfs_qgroup_limit_item, flags, 64);
1083BTRFS_SETGET_STACK_FUNCS(stack_qgroup_limit_max_referenced,
1084 struct btrfs_qgroup_limit_item, max_rfer, 64);
1085BTRFS_SETGET_STACK_FUNCS(stack_qgroup_limit_max_exclusive,
1086 struct btrfs_qgroup_limit_item, max_excl, 64);
1087BTRFS_SETGET_STACK_FUNCS(stack_qgroup_limit_rsv_referenced,
1088 struct btrfs_qgroup_limit_item, rsv_rfer, 64);
1089BTRFS_SETGET_STACK_FUNCS(stack_qgroup_limit_rsv_exclusive,
1090 struct btrfs_qgroup_limit_item, rsv_excl, 64);
1091
1092/* btrfs_balance_item */
1093BTRFS_SETGET_FUNCS(balance_item_flags, struct btrfs_balance_item, flags, 64);
1094
1095static inline struct btrfs_disk_balance_args* btrfs_balance_item_data(
1096 struct extent_buffer *eb, struct btrfs_balance_item *bi)
1097{
1098 unsigned long offset = (unsigned long)bi;
1099 struct btrfs_balance_item *p;
1100 p = (struct btrfs_balance_item *)(eb->data + offset);
1101 return &p->data;
1102}
1103
1104static inline struct btrfs_disk_balance_args* btrfs_balance_item_meta(
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->meta;
1111}
1112
1113static inline struct btrfs_disk_balance_args* btrfs_balance_item_sys(
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->sys;
1120}
1121
1122static inline u64 btrfs_dev_stats_value(const struct extent_buffer *eb,
1123 const struct btrfs_dev_stats_item *ptr,
1124 int index)
1125{
1126 u64 val;
1127
1128 read_extent_buffer(eb, &val,
1129 offsetof(struct btrfs_dev_stats_item, values) +
1130 ((unsigned long)ptr) + (index * sizeof(u64)),
1131 sizeof(val));
1132 return val;
1133}
1134
1135/*
1136 * this returns the number of bytes used by the item on disk, minus the
1137 * size of any extent headers. If a file is compressed on disk, this is
1138 * the compressed size
1139 */
1140static inline u32 btrfs_file_extent_inline_item_len(struct extent_buffer *eb,
1141 struct btrfs_item *e)
1142{
1143 unsigned long offset;
1144 offset = offsetof(struct btrfs_file_extent_item, disk_bytenr);
1145 return btrfs_item_size(eb, e) - offset;
1146}
1147
1148#define btrfs_fs_incompat(fs_info, opt) \
1149 __btrfs_fs_incompat((fs_info), BTRFS_FEATURE_INCOMPAT_##opt)
1150
1151static inline bool __btrfs_fs_incompat(struct btrfs_fs_info *fs_info, u64 flag)
1152{
1153 struct btrfs_super_block *disk_super;
1154 disk_super = fs_info->super_copy;
1155 return !!(btrfs_super_incompat_flags(disk_super) & flag);
1156}
1157
1158#define btrfs_fs_compat_ro(fs_info, opt) \
1159 __btrfs_fs_compat_ro((fs_info), BTRFS_FEATURE_COMPAT_RO_##opt)
1160
1161static inline int __btrfs_fs_compat_ro(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_compat_ro_flags(disk_super) & flag);
1166}
1167
1168/* helper function to cast into the data area of the leaf. */
1169#define btrfs_item_ptr(leaf, slot, type) \
1170 ((type *)(btrfs_leaf_data(leaf) + \
1171 btrfs_item_offset_nr(leaf, slot)))
1172
1173#define btrfs_item_ptr_offset(leaf, slot) \
1174 ((unsigned long)(btrfs_leaf_data(leaf) + \
1175 btrfs_item_offset_nr(leaf, slot)))
1176
1177static inline u64 btrfs_name_hash(const char *name, int len)
1178{
1179 return (u64)crc32c((u32)~1, (u8 *)name, len);
1180}
1181
1182/*
1183 * Figure the key offset of an extended inode ref
1184 */
1185static inline u64 btrfs_extref_hash(u64 parent_objectid, const char *name,
1186 int len)
1187{
1188 return crc32(parent_objectid, (u8 *)name, len);
1189}
1190
1191union btrfs_tree_node {
1192 struct btrfs_header header;
1193 struct btrfs_leaf leaf;
1194 struct btrfs_node node;
1195};
1196
Qu Wenruoddd0cb82020-06-24 18:02:56 +02001197struct __btrfs_path {
Qu Wenruo6925bd22020-06-24 18:02:53 +02001198 union btrfs_tree_node *nodes[BTRFS_MAX_LEVEL];
1199 u32 slots[BTRFS_MAX_LEVEL];
1200};
1201
Qu Wenruof6d5af72020-06-24 18:02:57 +02001202struct __btrfs_root {
Qu Wenruo6925bd22020-06-24 18:02:53 +02001203 u64 objectid;
1204 u64 bytenr;
1205 u64 root_dirid;
1206};
1207
Qu Wenruod85f9592020-06-24 18:02:55 +02001208int __btrfs_comp_keys(struct btrfs_key *, struct btrfs_key *);
Qu Wenruo6925bd22020-06-24 18:02:53 +02001209int btrfs_comp_keys_type(struct btrfs_key *, struct btrfs_key *);
1210int btrfs_bin_search(union btrfs_tree_node *, struct btrfs_key *, int *);
Qu Wenruoddd0cb82020-06-24 18:02:56 +02001211void __btrfs_free_path(struct __btrfs_path *);
Qu Wenruof6d5af72020-06-24 18:02:57 +02001212int btrfs_search_tree(const struct __btrfs_root *, struct btrfs_key *,
Qu Wenruoddd0cb82020-06-24 18:02:56 +02001213 struct __btrfs_path *);
1214int btrfs_prev_slot(struct __btrfs_path *);
1215int btrfs_next_slot(struct __btrfs_path *);
Qu Wenruo6925bd22020-06-24 18:02:53 +02001216
Qu Wenruoddd0cb82020-06-24 18:02:56 +02001217static inline struct btrfs_key *btrfs_path_leaf_key(struct __btrfs_path *p) {
Qu Wenruo6925bd22020-06-24 18:02:53 +02001218 /* At tree read time we have converted the endian for btrfs_disk_key */
1219 return (struct btrfs_key *)&p->nodes[0]->leaf.items[p->slots[0]].key;
1220}
1221
1222static inline struct btrfs_key *
Qu Wenruof6d5af72020-06-24 18:02:57 +02001223btrfs_search_tree_key_type(const struct __btrfs_root *root, u64 objectid,
Qu Wenruoddd0cb82020-06-24 18:02:56 +02001224 u8 type, struct __btrfs_path *path)
Qu Wenruo6925bd22020-06-24 18:02:53 +02001225{
1226 struct btrfs_key key, *res;
1227
1228 key.objectid = objectid;
1229 key.type = type;
1230 key.offset = 0;
1231
1232 if (btrfs_search_tree(root, &key, path))
1233 return NULL;
1234
1235 res = btrfs_path_leaf_key(path);
1236 if (btrfs_comp_keys_type(&key, res)) {
Qu Wenruoddd0cb82020-06-24 18:02:56 +02001237 __btrfs_free_path(path);
Qu Wenruo6925bd22020-06-24 18:02:53 +02001238 return NULL;
1239 }
1240
1241 return res;
1242}
1243
Qu Wenruoddd0cb82020-06-24 18:02:56 +02001244static inline u32 btrfs_path_item_size(struct __btrfs_path *p)
Qu Wenruo6925bd22020-06-24 18:02:53 +02001245{
1246 return p->nodes[0]->leaf.items[p->slots[0]].size;
1247}
1248
1249static inline void *__btrfs_leaf_data(struct btrfs_leaf *leaf, u32 slot)
1250{
1251 return ((u8 *) leaf) + sizeof(struct btrfs_header)
1252 + leaf->items[slot].offset;
1253}
1254
Qu Wenruoddd0cb82020-06-24 18:02:56 +02001255static inline void *btrfs_path_leaf_data(struct __btrfs_path *p)
Qu Wenruo6925bd22020-06-24 18:02:53 +02001256{
1257 return __btrfs_leaf_data(&p->nodes[0]->leaf, p->slots[0]);
1258}
1259
1260#define btrfs_path_item_ptr(p,t) \
1261 ((t *) btrfs_path_leaf_data((p)))
1262
1263u16 btrfs_super_csum_size(const struct btrfs_super_block *s);
1264const char *btrfs_super_csum_name(u16 csum_type);
1265u16 btrfs_csum_type_size(u16 csum_type);
1266size_t btrfs_super_num_csums(void);
1267
Qu Wenruod85f9592020-06-24 18:02:55 +02001268/* ctree.c */
1269int btrfs_comp_cpu_keys(const struct btrfs_key *k1, const struct btrfs_key *k2);
1270enum btrfs_tree_block_status
1271btrfs_check_node(struct btrfs_fs_info *fs_info,
1272 struct btrfs_disk_key *parent_key, struct extent_buffer *buf);
1273enum btrfs_tree_block_status
1274btrfs_check_leaf(struct btrfs_fs_info *fs_info,
1275 struct btrfs_disk_key *parent_key, struct extent_buffer *buf);
1276int btrfs_leaf_free_space(struct extent_buffer *leaf);
Marek Behún4eff4262017-09-03 17:00:26 +02001277#endif /* __BTRFS_CTREE_H__ */