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