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