Loading...
1/* SPDX-License-Identifier: GPL-2.0 */
2
3#ifndef BTRFS_ACCESSORS_H
4#define BTRFS_ACCESSORS_H
5
6struct btrfs_map_token {
7 struct extent_buffer *eb;
8 char *kaddr;
9 unsigned long offset;
10};
11
12void btrfs_init_map_token(struct btrfs_map_token *token, struct extent_buffer *eb);
13
14/*
15 * Some macros to generate set/get functions for the struct fields. This
16 * assumes there is a lefoo_to_cpu for every type, so lets make a simple one
17 * for u8:
18 */
19#define le8_to_cpu(v) (v)
20#define cpu_to_le8(v) (v)
21#define __le8 u8
22
23static inline u8 get_unaligned_le8(const void *p)
24{
25 return *(u8 *)p;
26}
27
28static inline void put_unaligned_le8(u8 val, void *p)
29{
30 *(u8 *)p = val;
31}
32
33#define read_eb_member(eb, ptr, type, member, result) (\
34 read_extent_buffer(eb, (char *)(result), \
35 ((unsigned long)(ptr)) + \
36 offsetof(type, member), \
37 sizeof(((type *)0)->member)))
38
39#define write_eb_member(eb, ptr, type, member, result) (\
40 write_extent_buffer(eb, (char *)(result), \
41 ((unsigned long)(ptr)) + \
42 offsetof(type, member), \
43 sizeof(((type *)0)->member)))
44
45#define DECLARE_BTRFS_SETGET_BITS(bits) \
46u##bits btrfs_get_token_##bits(struct btrfs_map_token *token, \
47 const void *ptr, unsigned long off); \
48void btrfs_set_token_##bits(struct btrfs_map_token *token, \
49 const void *ptr, unsigned long off, \
50 u##bits val); \
51u##bits btrfs_get_##bits(const struct extent_buffer *eb, \
52 const void *ptr, unsigned long off); \
53void btrfs_set_##bits(const struct extent_buffer *eb, void *ptr, \
54 unsigned long off, u##bits val);
55
56DECLARE_BTRFS_SETGET_BITS(8)
57DECLARE_BTRFS_SETGET_BITS(16)
58DECLARE_BTRFS_SETGET_BITS(32)
59DECLARE_BTRFS_SETGET_BITS(64)
60
61#define BTRFS_SETGET_FUNCS(name, type, member, bits) \
62static inline u##bits btrfs_##name(const struct extent_buffer *eb, \
63 const type *s) \
64{ \
65 static_assert(sizeof(u##bits) == sizeof(((type *)0))->member); \
66 return btrfs_get_##bits(eb, s, offsetof(type, member)); \
67} \
68static inline void btrfs_set_##name(const struct extent_buffer *eb, type *s, \
69 u##bits val) \
70{ \
71 static_assert(sizeof(u##bits) == sizeof(((type *)0))->member); \
72 btrfs_set_##bits(eb, s, offsetof(type, member), val); \
73} \
74static inline u##bits btrfs_token_##name(struct btrfs_map_token *token, \
75 const type *s) \
76{ \
77 static_assert(sizeof(u##bits) == sizeof(((type *)0))->member); \
78 return btrfs_get_token_##bits(token, s, offsetof(type, member));\
79} \
80static inline void btrfs_set_token_##name(struct btrfs_map_token *token,\
81 type *s, u##bits val) \
82{ \
83 static_assert(sizeof(u##bits) == sizeof(((type *)0))->member); \
84 btrfs_set_token_##bits(token, s, offsetof(type, member), val); \
85}
86
87#define BTRFS_SETGET_HEADER_FUNCS(name, type, member, bits) \
88static inline u##bits btrfs_##name(const struct extent_buffer *eb) \
89{ \
90 const type *p = page_address(eb->pages[0]) + \
91 offset_in_page(eb->start); \
92 return get_unaligned_le##bits(&p->member); \
93} \
94static inline void btrfs_set_##name(const struct extent_buffer *eb, \
95 u##bits val) \
96{ \
97 type *p = page_address(eb->pages[0]) + offset_in_page(eb->start); \
98 put_unaligned_le##bits(val, &p->member); \
99}
100
101#define BTRFS_SETGET_STACK_FUNCS(name, type, member, bits) \
102static inline u##bits btrfs_##name(const type *s) \
103{ \
104 return get_unaligned_le##bits(&s->member); \
105} \
106static inline void btrfs_set_##name(type *s, u##bits val) \
107{ \
108 put_unaligned_le##bits(val, &s->member); \
109}
110
111static inline u64 btrfs_device_total_bytes(const struct extent_buffer *eb,
112 struct btrfs_dev_item *s)
113{
114 static_assert(sizeof(u64) ==
115 sizeof(((struct btrfs_dev_item *)0))->total_bytes);
116 return btrfs_get_64(eb, s, offsetof(struct btrfs_dev_item,
117 total_bytes));
118}
119static inline void btrfs_set_device_total_bytes(const struct extent_buffer *eb,
120 struct btrfs_dev_item *s,
121 u64 val)
122{
123 static_assert(sizeof(u64) ==
124 sizeof(((struct btrfs_dev_item *)0))->total_bytes);
125 WARN_ON(!IS_ALIGNED(val, eb->fs_info->sectorsize));
126 btrfs_set_64(eb, s, offsetof(struct btrfs_dev_item, total_bytes), val);
127}
128
129BTRFS_SETGET_FUNCS(device_type, struct btrfs_dev_item, type, 64);
130BTRFS_SETGET_FUNCS(device_bytes_used, struct btrfs_dev_item, bytes_used, 64);
131BTRFS_SETGET_FUNCS(device_io_align, struct btrfs_dev_item, io_align, 32);
132BTRFS_SETGET_FUNCS(device_io_width, struct btrfs_dev_item, io_width, 32);
133BTRFS_SETGET_FUNCS(device_start_offset, struct btrfs_dev_item, start_offset, 64);
134BTRFS_SETGET_FUNCS(device_sector_size, struct btrfs_dev_item, sector_size, 32);
135BTRFS_SETGET_FUNCS(device_id, struct btrfs_dev_item, devid, 64);
136BTRFS_SETGET_FUNCS(device_group, struct btrfs_dev_item, dev_group, 32);
137BTRFS_SETGET_FUNCS(device_seek_speed, struct btrfs_dev_item, seek_speed, 8);
138BTRFS_SETGET_FUNCS(device_bandwidth, struct btrfs_dev_item, bandwidth, 8);
139BTRFS_SETGET_FUNCS(device_generation, struct btrfs_dev_item, generation, 64);
140
141BTRFS_SETGET_STACK_FUNCS(stack_device_type, struct btrfs_dev_item, type, 64);
142BTRFS_SETGET_STACK_FUNCS(stack_device_total_bytes, struct btrfs_dev_item,
143 total_bytes, 64);
144BTRFS_SETGET_STACK_FUNCS(stack_device_bytes_used, struct btrfs_dev_item,
145 bytes_used, 64);
146BTRFS_SETGET_STACK_FUNCS(stack_device_io_align, struct btrfs_dev_item,
147 io_align, 32);
148BTRFS_SETGET_STACK_FUNCS(stack_device_io_width, struct btrfs_dev_item,
149 io_width, 32);
150BTRFS_SETGET_STACK_FUNCS(stack_device_sector_size, struct btrfs_dev_item,
151 sector_size, 32);
152BTRFS_SETGET_STACK_FUNCS(stack_device_id, struct btrfs_dev_item, devid, 64);
153BTRFS_SETGET_STACK_FUNCS(stack_device_group, struct btrfs_dev_item, dev_group, 32);
154BTRFS_SETGET_STACK_FUNCS(stack_device_seek_speed, struct btrfs_dev_item,
155 seek_speed, 8);
156BTRFS_SETGET_STACK_FUNCS(stack_device_bandwidth, struct btrfs_dev_item,
157 bandwidth, 8);
158BTRFS_SETGET_STACK_FUNCS(stack_device_generation, struct btrfs_dev_item,
159 generation, 64);
160
161static inline unsigned long btrfs_device_uuid(struct btrfs_dev_item *d)
162{
163 return (unsigned long)d + offsetof(struct btrfs_dev_item, uuid);
164}
165
166static inline unsigned long btrfs_device_fsid(struct btrfs_dev_item *d)
167{
168 return (unsigned long)d + offsetof(struct btrfs_dev_item, fsid);
169}
170
171BTRFS_SETGET_FUNCS(chunk_length, struct btrfs_chunk, length, 64);
172BTRFS_SETGET_FUNCS(chunk_owner, struct btrfs_chunk, owner, 64);
173BTRFS_SETGET_FUNCS(chunk_stripe_len, struct btrfs_chunk, stripe_len, 64);
174BTRFS_SETGET_FUNCS(chunk_io_align, struct btrfs_chunk, io_align, 32);
175BTRFS_SETGET_FUNCS(chunk_io_width, struct btrfs_chunk, io_width, 32);
176BTRFS_SETGET_FUNCS(chunk_sector_size, struct btrfs_chunk, sector_size, 32);
177BTRFS_SETGET_FUNCS(chunk_type, struct btrfs_chunk, type, 64);
178BTRFS_SETGET_FUNCS(chunk_num_stripes, struct btrfs_chunk, num_stripes, 16);
179BTRFS_SETGET_FUNCS(chunk_sub_stripes, struct btrfs_chunk, sub_stripes, 16);
180BTRFS_SETGET_FUNCS(stripe_devid, struct btrfs_stripe, devid, 64);
181BTRFS_SETGET_FUNCS(stripe_offset, struct btrfs_stripe, offset, 64);
182
183static inline char *btrfs_stripe_dev_uuid(struct btrfs_stripe *s)
184{
185 return (char *)s + offsetof(struct btrfs_stripe, dev_uuid);
186}
187
188BTRFS_SETGET_STACK_FUNCS(stack_chunk_length, struct btrfs_chunk, length, 64);
189BTRFS_SETGET_STACK_FUNCS(stack_chunk_owner, struct btrfs_chunk, owner, 64);
190BTRFS_SETGET_STACK_FUNCS(stack_chunk_stripe_len, struct btrfs_chunk,
191 stripe_len, 64);
192BTRFS_SETGET_STACK_FUNCS(stack_chunk_io_align, struct btrfs_chunk, io_align, 32);
193BTRFS_SETGET_STACK_FUNCS(stack_chunk_io_width, struct btrfs_chunk, io_width, 32);
194BTRFS_SETGET_STACK_FUNCS(stack_chunk_sector_size, struct btrfs_chunk,
195 sector_size, 32);
196BTRFS_SETGET_STACK_FUNCS(stack_chunk_type, struct btrfs_chunk, type, 64);
197BTRFS_SETGET_STACK_FUNCS(stack_chunk_num_stripes, struct btrfs_chunk,
198 num_stripes, 16);
199BTRFS_SETGET_STACK_FUNCS(stack_chunk_sub_stripes, struct btrfs_chunk,
200 sub_stripes, 16);
201BTRFS_SETGET_STACK_FUNCS(stack_stripe_devid, struct btrfs_stripe, devid, 64);
202BTRFS_SETGET_STACK_FUNCS(stack_stripe_offset, struct btrfs_stripe, offset, 64);
203
204static inline struct btrfs_stripe *btrfs_stripe_nr(struct btrfs_chunk *c, int nr)
205{
206 unsigned long offset = (unsigned long)c;
207
208 offset += offsetof(struct btrfs_chunk, stripe);
209 offset += nr * sizeof(struct btrfs_stripe);
210 return (struct btrfs_stripe *)offset;
211}
212
213static inline char *btrfs_stripe_dev_uuid_nr(struct btrfs_chunk *c, int nr)
214{
215 return btrfs_stripe_dev_uuid(btrfs_stripe_nr(c, nr));
216}
217
218static inline u64 btrfs_stripe_offset_nr(const struct extent_buffer *eb,
219 struct btrfs_chunk *c, int nr)
220{
221 return btrfs_stripe_offset(eb, btrfs_stripe_nr(c, nr));
222}
223
224static inline void btrfs_set_stripe_offset_nr(struct extent_buffer *eb,
225 struct btrfs_chunk *c, int nr,
226 u64 val)
227{
228 btrfs_set_stripe_offset(eb, btrfs_stripe_nr(c, nr), val);
229}
230
231static inline u64 btrfs_stripe_devid_nr(const struct extent_buffer *eb,
232 struct btrfs_chunk *c, int nr)
233{
234 return btrfs_stripe_devid(eb, btrfs_stripe_nr(c, nr));
235}
236
237static inline void btrfs_set_stripe_devid_nr(struct extent_buffer *eb,
238 struct btrfs_chunk *c, int nr,
239 u64 val)
240{
241 btrfs_set_stripe_devid(eb, btrfs_stripe_nr(c, nr), val);
242}
243
244/* struct btrfs_block_group_item */
245BTRFS_SETGET_STACK_FUNCS(stack_block_group_used, struct btrfs_block_group_item,
246 used, 64);
247BTRFS_SETGET_FUNCS(block_group_used, struct btrfs_block_group_item, used, 64);
248BTRFS_SETGET_STACK_FUNCS(stack_block_group_chunk_objectid,
249 struct btrfs_block_group_item, chunk_objectid, 64);
250
251BTRFS_SETGET_FUNCS(block_group_chunk_objectid,
252 struct btrfs_block_group_item, chunk_objectid, 64);
253BTRFS_SETGET_FUNCS(block_group_flags, struct btrfs_block_group_item, flags, 64);
254BTRFS_SETGET_STACK_FUNCS(stack_block_group_flags,
255 struct btrfs_block_group_item, flags, 64);
256
257/* struct btrfs_free_space_info */
258BTRFS_SETGET_FUNCS(free_space_extent_count, struct btrfs_free_space_info,
259 extent_count, 32);
260BTRFS_SETGET_FUNCS(free_space_flags, struct btrfs_free_space_info, flags, 32);
261
262/* struct btrfs_inode_ref */
263BTRFS_SETGET_FUNCS(inode_ref_name_len, struct btrfs_inode_ref, name_len, 16);
264BTRFS_SETGET_FUNCS(inode_ref_index, struct btrfs_inode_ref, index, 64);
265BTRFS_SETGET_STACK_FUNCS(stack_inode_ref_name_len, struct btrfs_inode_ref, name_len, 16);
266BTRFS_SETGET_STACK_FUNCS(stack_inode_ref_index, struct btrfs_inode_ref, index, 64);
267
268/* struct btrfs_inode_extref */
269BTRFS_SETGET_FUNCS(inode_extref_parent, struct btrfs_inode_extref,
270 parent_objectid, 64);
271BTRFS_SETGET_FUNCS(inode_extref_name_len, struct btrfs_inode_extref,
272 name_len, 16);
273BTRFS_SETGET_FUNCS(inode_extref_index, struct btrfs_inode_extref, index, 64);
274
275/* struct btrfs_inode_item */
276BTRFS_SETGET_FUNCS(inode_generation, struct btrfs_inode_item, generation, 64);
277BTRFS_SETGET_FUNCS(inode_sequence, struct btrfs_inode_item, sequence, 64);
278BTRFS_SETGET_FUNCS(inode_transid, struct btrfs_inode_item, transid, 64);
279BTRFS_SETGET_FUNCS(inode_size, struct btrfs_inode_item, size, 64);
280BTRFS_SETGET_FUNCS(inode_nbytes, struct btrfs_inode_item, nbytes, 64);
281BTRFS_SETGET_FUNCS(inode_block_group, struct btrfs_inode_item, block_group, 64);
282BTRFS_SETGET_FUNCS(inode_nlink, struct btrfs_inode_item, nlink, 32);
283BTRFS_SETGET_FUNCS(inode_uid, struct btrfs_inode_item, uid, 32);
284BTRFS_SETGET_FUNCS(inode_gid, struct btrfs_inode_item, gid, 32);
285BTRFS_SETGET_FUNCS(inode_mode, struct btrfs_inode_item, mode, 32);
286BTRFS_SETGET_FUNCS(inode_rdev, struct btrfs_inode_item, rdev, 64);
287BTRFS_SETGET_FUNCS(inode_flags, struct btrfs_inode_item, flags, 64);
288BTRFS_SETGET_STACK_FUNCS(stack_inode_generation, struct btrfs_inode_item,
289 generation, 64);
290BTRFS_SETGET_STACK_FUNCS(stack_inode_sequence, struct btrfs_inode_item,
291 sequence, 64);
292BTRFS_SETGET_STACK_FUNCS(stack_inode_transid, struct btrfs_inode_item,
293 transid, 64);
294BTRFS_SETGET_STACK_FUNCS(stack_inode_size, struct btrfs_inode_item, size, 64);
295BTRFS_SETGET_STACK_FUNCS(stack_inode_nbytes, struct btrfs_inode_item, nbytes, 64);
296BTRFS_SETGET_STACK_FUNCS(stack_inode_block_group, struct btrfs_inode_item,
297 block_group, 64);
298BTRFS_SETGET_STACK_FUNCS(stack_inode_nlink, struct btrfs_inode_item, nlink, 32);
299BTRFS_SETGET_STACK_FUNCS(stack_inode_uid, struct btrfs_inode_item, uid, 32);
300BTRFS_SETGET_STACK_FUNCS(stack_inode_gid, struct btrfs_inode_item, gid, 32);
301BTRFS_SETGET_STACK_FUNCS(stack_inode_mode, struct btrfs_inode_item, mode, 32);
302BTRFS_SETGET_STACK_FUNCS(stack_inode_rdev, struct btrfs_inode_item, rdev, 64);
303BTRFS_SETGET_STACK_FUNCS(stack_inode_flags, struct btrfs_inode_item, flags, 64);
304BTRFS_SETGET_FUNCS(timespec_sec, struct btrfs_timespec, sec, 64);
305BTRFS_SETGET_FUNCS(timespec_nsec, struct btrfs_timespec, nsec, 32);
306BTRFS_SETGET_STACK_FUNCS(stack_timespec_sec, struct btrfs_timespec, sec, 64);
307BTRFS_SETGET_STACK_FUNCS(stack_timespec_nsec, struct btrfs_timespec, nsec, 32);
308
309/* struct btrfs_dev_extent */
310BTRFS_SETGET_FUNCS(dev_extent_chunk_tree, struct btrfs_dev_extent, chunk_tree, 64);
311BTRFS_SETGET_FUNCS(dev_extent_chunk_objectid, struct btrfs_dev_extent,
312 chunk_objectid, 64);
313BTRFS_SETGET_FUNCS(dev_extent_chunk_offset, struct btrfs_dev_extent,
314 chunk_offset, 64);
315BTRFS_SETGET_FUNCS(dev_extent_length, struct btrfs_dev_extent, length, 64);
316BTRFS_SETGET_STACK_FUNCS(stack_dev_extent_chunk_tree, struct btrfs_dev_extent,
317 chunk_tree, 64);
318BTRFS_SETGET_STACK_FUNCS(stack_dev_extent_chunk_objectid, struct btrfs_dev_extent,
319 chunk_objectid, 64);
320BTRFS_SETGET_STACK_FUNCS(stack_dev_extent_chunk_offset, struct btrfs_dev_extent,
321 chunk_offset, 64);
322BTRFS_SETGET_STACK_FUNCS(stack_dev_extent_length, struct btrfs_dev_extent, length, 64);
323
324BTRFS_SETGET_FUNCS(extent_refs, struct btrfs_extent_item, refs, 64);
325BTRFS_SETGET_FUNCS(extent_generation, struct btrfs_extent_item, generation, 64);
326BTRFS_SETGET_FUNCS(extent_flags, struct btrfs_extent_item, flags, 64);
327
328BTRFS_SETGET_FUNCS(tree_block_level, struct btrfs_tree_block_info, level, 8);
329
330static inline void btrfs_tree_block_key(const struct extent_buffer *eb,
331 struct btrfs_tree_block_info *item,
332 struct btrfs_disk_key *key)
333{
334 read_eb_member(eb, item, struct btrfs_tree_block_info, key, key);
335}
336
337static inline void btrfs_set_tree_block_key(const struct extent_buffer *eb,
338 struct btrfs_tree_block_info *item,
339 struct btrfs_disk_key *key)
340{
341 write_eb_member(eb, item, struct btrfs_tree_block_info, key, key);
342}
343
344BTRFS_SETGET_FUNCS(extent_data_ref_root, struct btrfs_extent_data_ref, root, 64);
345BTRFS_SETGET_FUNCS(extent_data_ref_objectid, struct btrfs_extent_data_ref,
346 objectid, 64);
347BTRFS_SETGET_FUNCS(extent_data_ref_offset, struct btrfs_extent_data_ref,
348 offset, 64);
349BTRFS_SETGET_FUNCS(extent_data_ref_count, struct btrfs_extent_data_ref, count, 32);
350
351BTRFS_SETGET_FUNCS(shared_data_ref_count, struct btrfs_shared_data_ref, count, 32);
352
353BTRFS_SETGET_FUNCS(extent_inline_ref_type, struct btrfs_extent_inline_ref,
354 type, 8);
355BTRFS_SETGET_FUNCS(extent_inline_ref_offset, struct btrfs_extent_inline_ref,
356 offset, 64);
357
358static inline u32 btrfs_extent_inline_ref_size(int type)
359{
360 if (type == BTRFS_TREE_BLOCK_REF_KEY ||
361 type == BTRFS_SHARED_BLOCK_REF_KEY)
362 return sizeof(struct btrfs_extent_inline_ref);
363 if (type == BTRFS_SHARED_DATA_REF_KEY)
364 return sizeof(struct btrfs_shared_data_ref) +
365 sizeof(struct btrfs_extent_inline_ref);
366 if (type == BTRFS_EXTENT_DATA_REF_KEY)
367 return sizeof(struct btrfs_extent_data_ref) +
368 offsetof(struct btrfs_extent_inline_ref, offset);
369 return 0;
370}
371
372/* struct btrfs_node */
373BTRFS_SETGET_FUNCS(key_blockptr, struct btrfs_key_ptr, blockptr, 64);
374BTRFS_SETGET_FUNCS(key_generation, struct btrfs_key_ptr, generation, 64);
375BTRFS_SETGET_STACK_FUNCS(stack_key_blockptr, struct btrfs_key_ptr, blockptr, 64);
376BTRFS_SETGET_STACK_FUNCS(stack_key_generation, struct btrfs_key_ptr,
377 generation, 64);
378
379static inline u64 btrfs_node_blockptr(const struct extent_buffer *eb, int nr)
380{
381 unsigned long ptr;
382
383 ptr = offsetof(struct btrfs_node, ptrs) +
384 sizeof(struct btrfs_key_ptr) * nr;
385 return btrfs_key_blockptr(eb, (struct btrfs_key_ptr *)ptr);
386}
387
388static inline void btrfs_set_node_blockptr(const struct extent_buffer *eb,
389 int nr, u64 val)
390{
391 unsigned long ptr;
392
393 ptr = offsetof(struct btrfs_node, ptrs) +
394 sizeof(struct btrfs_key_ptr) * nr;
395 btrfs_set_key_blockptr(eb, (struct btrfs_key_ptr *)ptr, val);
396}
397
398static inline u64 btrfs_node_ptr_generation(const struct extent_buffer *eb, int nr)
399{
400 unsigned long ptr;
401
402 ptr = offsetof(struct btrfs_node, ptrs) +
403 sizeof(struct btrfs_key_ptr) * nr;
404 return btrfs_key_generation(eb, (struct btrfs_key_ptr *)ptr);
405}
406
407static inline void btrfs_set_node_ptr_generation(const struct extent_buffer *eb,
408 int nr, u64 val)
409{
410 unsigned long ptr;
411
412 ptr = offsetof(struct btrfs_node, ptrs) +
413 sizeof(struct btrfs_key_ptr) * nr;
414 btrfs_set_key_generation(eb, (struct btrfs_key_ptr *)ptr, val);
415}
416
417static inline unsigned long btrfs_node_key_ptr_offset(const struct extent_buffer *eb, int nr)
418{
419 return offsetof(struct btrfs_node, ptrs) +
420 sizeof(struct btrfs_key_ptr) * nr;
421}
422
423void btrfs_node_key(const struct extent_buffer *eb,
424 struct btrfs_disk_key *disk_key, int nr);
425
426static inline void btrfs_set_node_key(const struct extent_buffer *eb,
427 struct btrfs_disk_key *disk_key, int nr)
428{
429 unsigned long ptr;
430
431 ptr = btrfs_node_key_ptr_offset(eb, nr);
432 write_eb_member(eb, (struct btrfs_key_ptr *)ptr,
433 struct btrfs_key_ptr, key, disk_key);
434}
435
436/* struct btrfs_item */
437BTRFS_SETGET_FUNCS(raw_item_offset, struct btrfs_item, offset, 32);
438BTRFS_SETGET_FUNCS(raw_item_size, struct btrfs_item, size, 32);
439BTRFS_SETGET_STACK_FUNCS(stack_item_offset, struct btrfs_item, offset, 32);
440BTRFS_SETGET_STACK_FUNCS(stack_item_size, struct btrfs_item, size, 32);
441
442static inline unsigned long btrfs_item_nr_offset(const struct extent_buffer *eb, int nr)
443{
444 return offsetof(struct btrfs_leaf, items) +
445 sizeof(struct btrfs_item) * nr;
446}
447
448static inline struct btrfs_item *btrfs_item_nr(const struct extent_buffer *eb, int nr)
449{
450 return (struct btrfs_item *)btrfs_item_nr_offset(eb, nr);
451}
452
453#define BTRFS_ITEM_SETGET_FUNCS(member) \
454static inline u32 btrfs_item_##member(const struct extent_buffer *eb, int slot) \
455{ \
456 return btrfs_raw_item_##member(eb, btrfs_item_nr(eb, slot)); \
457} \
458static inline void btrfs_set_item_##member(const struct extent_buffer *eb, \
459 int slot, u32 val) \
460{ \
461 btrfs_set_raw_item_##member(eb, btrfs_item_nr(eb, slot), val); \
462} \
463static inline u32 btrfs_token_item_##member(struct btrfs_map_token *token, \
464 int slot) \
465{ \
466 struct btrfs_item *item = btrfs_item_nr(token->eb, slot); \
467 return btrfs_token_raw_item_##member(token, item); \
468} \
469static inline void btrfs_set_token_item_##member(struct btrfs_map_token *token, \
470 int slot, u32 val) \
471{ \
472 struct btrfs_item *item = btrfs_item_nr(token->eb, slot); \
473 btrfs_set_token_raw_item_##member(token, item, val); \
474}
475
476BTRFS_ITEM_SETGET_FUNCS(offset)
477BTRFS_ITEM_SETGET_FUNCS(size);
478
479static inline u32 btrfs_item_data_end(const struct extent_buffer *eb, int nr)
480{
481 return btrfs_item_offset(eb, nr) + btrfs_item_size(eb, nr);
482}
483
484static inline void btrfs_item_key(const struct extent_buffer *eb,
485 struct btrfs_disk_key *disk_key, int nr)
486{
487 struct btrfs_item *item = btrfs_item_nr(eb, nr);
488
489 read_eb_member(eb, item, struct btrfs_item, key, disk_key);
490}
491
492static inline void btrfs_set_item_key(struct extent_buffer *eb,
493 struct btrfs_disk_key *disk_key, int nr)
494{
495 struct btrfs_item *item = btrfs_item_nr(eb, nr);
496
497 write_eb_member(eb, item, struct btrfs_item, key, disk_key);
498}
499
500BTRFS_SETGET_FUNCS(dir_log_end, struct btrfs_dir_log_item, end, 64);
501
502/* struct btrfs_root_ref */
503BTRFS_SETGET_FUNCS(root_ref_dirid, struct btrfs_root_ref, dirid, 64);
504BTRFS_SETGET_FUNCS(root_ref_sequence, struct btrfs_root_ref, sequence, 64);
505BTRFS_SETGET_FUNCS(root_ref_name_len, struct btrfs_root_ref, name_len, 16);
506BTRFS_SETGET_STACK_FUNCS(stack_root_ref_dirid, struct btrfs_root_ref, dirid, 64);
507BTRFS_SETGET_STACK_FUNCS(stack_root_ref_sequence, struct btrfs_root_ref, sequence, 64);
508BTRFS_SETGET_STACK_FUNCS(stack_root_ref_name_len, struct btrfs_root_ref, name_len, 16);
509
510/* struct btrfs_dir_item */
511BTRFS_SETGET_FUNCS(dir_data_len, struct btrfs_dir_item, data_len, 16);
512BTRFS_SETGET_FUNCS(dir_flags, struct btrfs_dir_item, type, 8);
513BTRFS_SETGET_FUNCS(dir_name_len, struct btrfs_dir_item, name_len, 16);
514BTRFS_SETGET_FUNCS(dir_transid, struct btrfs_dir_item, transid, 64);
515BTRFS_SETGET_STACK_FUNCS(stack_dir_flags, struct btrfs_dir_item, type, 8);
516BTRFS_SETGET_STACK_FUNCS(stack_dir_data_len, struct btrfs_dir_item, data_len, 16);
517BTRFS_SETGET_STACK_FUNCS(stack_dir_name_len, struct btrfs_dir_item, name_len, 16);
518BTRFS_SETGET_STACK_FUNCS(stack_dir_transid, struct btrfs_dir_item, transid, 64);
519
520static inline u8 btrfs_dir_ftype(const struct extent_buffer *eb,
521 const struct btrfs_dir_item *item)
522{
523 return btrfs_dir_flags_to_ftype(btrfs_dir_flags(eb, item));
524}
525
526static inline u8 btrfs_stack_dir_ftype(const struct btrfs_dir_item *item)
527{
528 return btrfs_dir_flags_to_ftype(btrfs_stack_dir_flags(item));
529}
530
531static inline void btrfs_dir_item_key(const struct extent_buffer *eb,
532 const struct btrfs_dir_item *item,
533 struct btrfs_disk_key *key)
534{
535 read_eb_member(eb, item, struct btrfs_dir_item, location, key);
536}
537
538static inline void btrfs_set_dir_item_key(struct extent_buffer *eb,
539 struct btrfs_dir_item *item,
540 const struct btrfs_disk_key *key)
541{
542 write_eb_member(eb, item, struct btrfs_dir_item, location, key);
543}
544
545BTRFS_SETGET_FUNCS(free_space_entries, struct btrfs_free_space_header,
546 num_entries, 64);
547BTRFS_SETGET_FUNCS(free_space_bitmaps, struct btrfs_free_space_header,
548 num_bitmaps, 64);
549BTRFS_SETGET_FUNCS(free_space_generation, struct btrfs_free_space_header,
550 generation, 64);
551
552static inline void btrfs_free_space_key(const struct extent_buffer *eb,
553 const struct btrfs_free_space_header *h,
554 struct btrfs_disk_key *key)
555{
556 read_eb_member(eb, h, struct btrfs_free_space_header, location, key);
557}
558
559static inline void btrfs_set_free_space_key(struct extent_buffer *eb,
560 struct btrfs_free_space_header *h,
561 const struct btrfs_disk_key *key)
562{
563 write_eb_member(eb, h, struct btrfs_free_space_header, location, key);
564}
565
566/* struct btrfs_disk_key */
567BTRFS_SETGET_STACK_FUNCS(disk_key_objectid, struct btrfs_disk_key, objectid, 64);
568BTRFS_SETGET_STACK_FUNCS(disk_key_offset, struct btrfs_disk_key, offset, 64);
569BTRFS_SETGET_STACK_FUNCS(disk_key_type, struct btrfs_disk_key, type, 8);
570
571#ifdef __LITTLE_ENDIAN
572
573/*
574 * Optimized helpers for little-endian architectures where CPU and on-disk
575 * structures have the same endianness and we can skip conversions.
576 */
577
578static inline void btrfs_disk_key_to_cpu(struct btrfs_key *cpu_key,
579 const struct btrfs_disk_key *disk_key)
580{
581 memcpy(cpu_key, disk_key, sizeof(struct btrfs_key));
582}
583
584static inline void btrfs_cpu_key_to_disk(struct btrfs_disk_key *disk_key,
585 const struct btrfs_key *cpu_key)
586{
587 memcpy(disk_key, cpu_key, sizeof(struct btrfs_key));
588}
589
590static inline void btrfs_node_key_to_cpu(const struct extent_buffer *eb,
591 struct btrfs_key *cpu_key, int nr)
592{
593 struct btrfs_disk_key *disk_key = (struct btrfs_disk_key *)cpu_key;
594
595 btrfs_node_key(eb, disk_key, nr);
596}
597
598static inline void btrfs_item_key_to_cpu(const struct extent_buffer *eb,
599 struct btrfs_key *cpu_key, int nr)
600{
601 struct btrfs_disk_key *disk_key = (struct btrfs_disk_key *)cpu_key;
602
603 btrfs_item_key(eb, disk_key, nr);
604}
605
606static inline void btrfs_dir_item_key_to_cpu(const struct extent_buffer *eb,
607 const struct btrfs_dir_item *item,
608 struct btrfs_key *cpu_key)
609{
610 struct btrfs_disk_key *disk_key = (struct btrfs_disk_key *)cpu_key;
611
612 btrfs_dir_item_key(eb, item, disk_key);
613}
614
615#else
616
617static inline void btrfs_disk_key_to_cpu(struct btrfs_key *cpu,
618 const struct btrfs_disk_key *disk)
619{
620 cpu->offset = le64_to_cpu(disk->offset);
621 cpu->type = disk->type;
622 cpu->objectid = le64_to_cpu(disk->objectid);
623}
624
625static inline void btrfs_cpu_key_to_disk(struct btrfs_disk_key *disk,
626 const struct btrfs_key *cpu)
627{
628 disk->offset = cpu_to_le64(cpu->offset);
629 disk->type = cpu->type;
630 disk->objectid = cpu_to_le64(cpu->objectid);
631}
632
633static inline void btrfs_node_key_to_cpu(const struct extent_buffer *eb,
634 struct btrfs_key *key, int nr)
635{
636 struct btrfs_disk_key disk_key;
637
638 btrfs_node_key(eb, &disk_key, nr);
639 btrfs_disk_key_to_cpu(key, &disk_key);
640}
641
642static inline void btrfs_item_key_to_cpu(const struct extent_buffer *eb,
643 struct btrfs_key *key, int nr)
644{
645 struct btrfs_disk_key disk_key;
646
647 btrfs_item_key(eb, &disk_key, nr);
648 btrfs_disk_key_to_cpu(key, &disk_key);
649}
650
651static inline void btrfs_dir_item_key_to_cpu(const struct extent_buffer *eb,
652 const struct btrfs_dir_item *item,
653 struct btrfs_key *key)
654{
655 struct btrfs_disk_key disk_key;
656
657 btrfs_dir_item_key(eb, item, &disk_key);
658 btrfs_disk_key_to_cpu(key, &disk_key);
659}
660
661#endif
662
663/* struct btrfs_header */
664BTRFS_SETGET_HEADER_FUNCS(header_bytenr, struct btrfs_header, bytenr, 64);
665BTRFS_SETGET_HEADER_FUNCS(header_generation, struct btrfs_header, generation, 64);
666BTRFS_SETGET_HEADER_FUNCS(header_owner, struct btrfs_header, owner, 64);
667BTRFS_SETGET_HEADER_FUNCS(header_nritems, struct btrfs_header, nritems, 32);
668BTRFS_SETGET_HEADER_FUNCS(header_flags, struct btrfs_header, flags, 64);
669BTRFS_SETGET_HEADER_FUNCS(header_level, struct btrfs_header, level, 8);
670BTRFS_SETGET_STACK_FUNCS(stack_header_generation, struct btrfs_header,
671 generation, 64);
672BTRFS_SETGET_STACK_FUNCS(stack_header_owner, struct btrfs_header, owner, 64);
673BTRFS_SETGET_STACK_FUNCS(stack_header_nritems, struct btrfs_header, nritems, 32);
674BTRFS_SETGET_STACK_FUNCS(stack_header_bytenr, struct btrfs_header, bytenr, 64);
675
676static inline int btrfs_header_flag(const struct extent_buffer *eb, u64 flag)
677{
678 return (btrfs_header_flags(eb) & flag) == flag;
679}
680
681static inline void btrfs_set_header_flag(struct extent_buffer *eb, u64 flag)
682{
683 u64 flags = btrfs_header_flags(eb);
684
685 btrfs_set_header_flags(eb, flags | flag);
686}
687
688static inline void btrfs_clear_header_flag(struct extent_buffer *eb, u64 flag)
689{
690 u64 flags = btrfs_header_flags(eb);
691
692 btrfs_set_header_flags(eb, flags & ~flag);
693}
694
695static inline int btrfs_header_backref_rev(const struct extent_buffer *eb)
696{
697 u64 flags = btrfs_header_flags(eb);
698
699 return flags >> BTRFS_BACKREF_REV_SHIFT;
700}
701
702static inline void btrfs_set_header_backref_rev(struct extent_buffer *eb, int rev)
703{
704 u64 flags = btrfs_header_flags(eb);
705
706 flags &= ~BTRFS_BACKREF_REV_MASK;
707 flags |= (u64)rev << BTRFS_BACKREF_REV_SHIFT;
708 btrfs_set_header_flags(eb, flags);
709}
710
711static inline int btrfs_is_leaf(const struct extent_buffer *eb)
712{
713 return btrfs_header_level(eb) == 0;
714}
715
716/* struct btrfs_root_item */
717BTRFS_SETGET_FUNCS(disk_root_generation, struct btrfs_root_item, generation, 64);
718BTRFS_SETGET_FUNCS(disk_root_refs, struct btrfs_root_item, refs, 32);
719BTRFS_SETGET_FUNCS(disk_root_bytenr, struct btrfs_root_item, bytenr, 64);
720BTRFS_SETGET_FUNCS(disk_root_level, struct btrfs_root_item, level, 8);
721
722BTRFS_SETGET_STACK_FUNCS(root_generation, struct btrfs_root_item, generation, 64);
723BTRFS_SETGET_STACK_FUNCS(root_bytenr, struct btrfs_root_item, bytenr, 64);
724BTRFS_SETGET_STACK_FUNCS(root_drop_level, struct btrfs_root_item, drop_level, 8);
725BTRFS_SETGET_STACK_FUNCS(root_level, struct btrfs_root_item, level, 8);
726BTRFS_SETGET_STACK_FUNCS(root_dirid, struct btrfs_root_item, root_dirid, 64);
727BTRFS_SETGET_STACK_FUNCS(root_refs, struct btrfs_root_item, refs, 32);
728BTRFS_SETGET_STACK_FUNCS(root_flags, struct btrfs_root_item, flags, 64);
729BTRFS_SETGET_STACK_FUNCS(root_used, struct btrfs_root_item, bytes_used, 64);
730BTRFS_SETGET_STACK_FUNCS(root_limit, struct btrfs_root_item, byte_limit, 64);
731BTRFS_SETGET_STACK_FUNCS(root_last_snapshot, struct btrfs_root_item,
732 last_snapshot, 64);
733BTRFS_SETGET_STACK_FUNCS(root_generation_v2, struct btrfs_root_item,
734 generation_v2, 64);
735BTRFS_SETGET_STACK_FUNCS(root_ctransid, struct btrfs_root_item, ctransid, 64);
736BTRFS_SETGET_STACK_FUNCS(root_otransid, struct btrfs_root_item, otransid, 64);
737BTRFS_SETGET_STACK_FUNCS(root_stransid, struct btrfs_root_item, stransid, 64);
738BTRFS_SETGET_STACK_FUNCS(root_rtransid, struct btrfs_root_item, rtransid, 64);
739
740/* struct btrfs_root_backup */
741BTRFS_SETGET_STACK_FUNCS(backup_tree_root, struct btrfs_root_backup,
742 tree_root, 64);
743BTRFS_SETGET_STACK_FUNCS(backup_tree_root_gen, struct btrfs_root_backup,
744 tree_root_gen, 64);
745BTRFS_SETGET_STACK_FUNCS(backup_tree_root_level, struct btrfs_root_backup,
746 tree_root_level, 8);
747
748BTRFS_SETGET_STACK_FUNCS(backup_chunk_root, struct btrfs_root_backup,
749 chunk_root, 64);
750BTRFS_SETGET_STACK_FUNCS(backup_chunk_root_gen, struct btrfs_root_backup,
751 chunk_root_gen, 64);
752BTRFS_SETGET_STACK_FUNCS(backup_chunk_root_level, struct btrfs_root_backup,
753 chunk_root_level, 8);
754
755BTRFS_SETGET_STACK_FUNCS(backup_extent_root, struct btrfs_root_backup,
756 extent_root, 64);
757BTRFS_SETGET_STACK_FUNCS(backup_extent_root_gen, struct btrfs_root_backup,
758 extent_root_gen, 64);
759BTRFS_SETGET_STACK_FUNCS(backup_extent_root_level, struct btrfs_root_backup,
760 extent_root_level, 8);
761
762BTRFS_SETGET_STACK_FUNCS(backup_fs_root, struct btrfs_root_backup,
763 fs_root, 64);
764BTRFS_SETGET_STACK_FUNCS(backup_fs_root_gen, struct btrfs_root_backup,
765 fs_root_gen, 64);
766BTRFS_SETGET_STACK_FUNCS(backup_fs_root_level, struct btrfs_root_backup,
767 fs_root_level, 8);
768
769BTRFS_SETGET_STACK_FUNCS(backup_dev_root, struct btrfs_root_backup,
770 dev_root, 64);
771BTRFS_SETGET_STACK_FUNCS(backup_dev_root_gen, struct btrfs_root_backup,
772 dev_root_gen, 64);
773BTRFS_SETGET_STACK_FUNCS(backup_dev_root_level, struct btrfs_root_backup,
774 dev_root_level, 8);
775
776BTRFS_SETGET_STACK_FUNCS(backup_csum_root, struct btrfs_root_backup,
777 csum_root, 64);
778BTRFS_SETGET_STACK_FUNCS(backup_csum_root_gen, struct btrfs_root_backup,
779 csum_root_gen, 64);
780BTRFS_SETGET_STACK_FUNCS(backup_csum_root_level, struct btrfs_root_backup,
781 csum_root_level, 8);
782BTRFS_SETGET_STACK_FUNCS(backup_total_bytes, struct btrfs_root_backup,
783 total_bytes, 64);
784BTRFS_SETGET_STACK_FUNCS(backup_bytes_used, struct btrfs_root_backup,
785 bytes_used, 64);
786BTRFS_SETGET_STACK_FUNCS(backup_num_devices, struct btrfs_root_backup,
787 num_devices, 64);
788
789/* struct btrfs_balance_item */
790BTRFS_SETGET_FUNCS(balance_flags, struct btrfs_balance_item, flags, 64);
791
792static inline void btrfs_balance_data(const struct extent_buffer *eb,
793 const struct btrfs_balance_item *bi,
794 struct btrfs_disk_balance_args *ba)
795{
796 read_eb_member(eb, bi, struct btrfs_balance_item, data, ba);
797}
798
799static inline void btrfs_set_balance_data(struct extent_buffer *eb,
800 struct btrfs_balance_item *bi,
801 const struct btrfs_disk_balance_args *ba)
802{
803 write_eb_member(eb, bi, struct btrfs_balance_item, data, ba);
804}
805
806static inline void btrfs_balance_meta(const struct extent_buffer *eb,
807 const struct btrfs_balance_item *bi,
808 struct btrfs_disk_balance_args *ba)
809{
810 read_eb_member(eb, bi, struct btrfs_balance_item, meta, ba);
811}
812
813static inline void btrfs_set_balance_meta(struct extent_buffer *eb,
814 struct btrfs_balance_item *bi,
815 const struct btrfs_disk_balance_args *ba)
816{
817 write_eb_member(eb, bi, struct btrfs_balance_item, meta, ba);
818}
819
820static inline void btrfs_balance_sys(const struct extent_buffer *eb,
821 const struct btrfs_balance_item *bi,
822 struct btrfs_disk_balance_args *ba)
823{
824 read_eb_member(eb, bi, struct btrfs_balance_item, sys, ba);
825}
826
827static inline void btrfs_set_balance_sys(struct extent_buffer *eb,
828 struct btrfs_balance_item *bi,
829 const struct btrfs_disk_balance_args *ba)
830{
831 write_eb_member(eb, bi, struct btrfs_balance_item, sys, ba);
832}
833
834static inline void btrfs_disk_balance_args_to_cpu(struct btrfs_balance_args *cpu,
835 const struct btrfs_disk_balance_args *disk)
836{
837 memset(cpu, 0, sizeof(*cpu));
838
839 cpu->profiles = le64_to_cpu(disk->profiles);
840 cpu->usage = le64_to_cpu(disk->usage);
841 cpu->devid = le64_to_cpu(disk->devid);
842 cpu->pstart = le64_to_cpu(disk->pstart);
843 cpu->pend = le64_to_cpu(disk->pend);
844 cpu->vstart = le64_to_cpu(disk->vstart);
845 cpu->vend = le64_to_cpu(disk->vend);
846 cpu->target = le64_to_cpu(disk->target);
847 cpu->flags = le64_to_cpu(disk->flags);
848 cpu->limit = le64_to_cpu(disk->limit);
849 cpu->stripes_min = le32_to_cpu(disk->stripes_min);
850 cpu->stripes_max = le32_to_cpu(disk->stripes_max);
851}
852
853static inline void btrfs_cpu_balance_args_to_disk(
854 struct btrfs_disk_balance_args *disk,
855 const struct btrfs_balance_args *cpu)
856{
857 memset(disk, 0, sizeof(*disk));
858
859 disk->profiles = cpu_to_le64(cpu->profiles);
860 disk->usage = cpu_to_le64(cpu->usage);
861 disk->devid = cpu_to_le64(cpu->devid);
862 disk->pstart = cpu_to_le64(cpu->pstart);
863 disk->pend = cpu_to_le64(cpu->pend);
864 disk->vstart = cpu_to_le64(cpu->vstart);
865 disk->vend = cpu_to_le64(cpu->vend);
866 disk->target = cpu_to_le64(cpu->target);
867 disk->flags = cpu_to_le64(cpu->flags);
868 disk->limit = cpu_to_le64(cpu->limit);
869 disk->stripes_min = cpu_to_le32(cpu->stripes_min);
870 disk->stripes_max = cpu_to_le32(cpu->stripes_max);
871}
872
873/* struct btrfs_super_block */
874BTRFS_SETGET_STACK_FUNCS(super_bytenr, struct btrfs_super_block, bytenr, 64);
875BTRFS_SETGET_STACK_FUNCS(super_flags, struct btrfs_super_block, flags, 64);
876BTRFS_SETGET_STACK_FUNCS(super_generation, struct btrfs_super_block,
877 generation, 64);
878BTRFS_SETGET_STACK_FUNCS(super_root, struct btrfs_super_block, root, 64);
879BTRFS_SETGET_STACK_FUNCS(super_sys_array_size,
880 struct btrfs_super_block, sys_chunk_array_size, 32);
881BTRFS_SETGET_STACK_FUNCS(super_chunk_root_generation,
882 struct btrfs_super_block, chunk_root_generation, 64);
883BTRFS_SETGET_STACK_FUNCS(super_root_level, struct btrfs_super_block,
884 root_level, 8);
885BTRFS_SETGET_STACK_FUNCS(super_chunk_root, struct btrfs_super_block,
886 chunk_root, 64);
887BTRFS_SETGET_STACK_FUNCS(super_chunk_root_level, struct btrfs_super_block,
888 chunk_root_level, 8);
889BTRFS_SETGET_STACK_FUNCS(super_log_root, struct btrfs_super_block, log_root, 64);
890BTRFS_SETGET_STACK_FUNCS(super_log_root_level, struct btrfs_super_block,
891 log_root_level, 8);
892BTRFS_SETGET_STACK_FUNCS(super_total_bytes, struct btrfs_super_block,
893 total_bytes, 64);
894BTRFS_SETGET_STACK_FUNCS(super_bytes_used, struct btrfs_super_block,
895 bytes_used, 64);
896BTRFS_SETGET_STACK_FUNCS(super_sectorsize, struct btrfs_super_block,
897 sectorsize, 32);
898BTRFS_SETGET_STACK_FUNCS(super_nodesize, struct btrfs_super_block,
899 nodesize, 32);
900BTRFS_SETGET_STACK_FUNCS(super_stripesize, struct btrfs_super_block,
901 stripesize, 32);
902BTRFS_SETGET_STACK_FUNCS(super_root_dir, struct btrfs_super_block,
903 root_dir_objectid, 64);
904BTRFS_SETGET_STACK_FUNCS(super_num_devices, struct btrfs_super_block,
905 num_devices, 64);
906BTRFS_SETGET_STACK_FUNCS(super_compat_flags, struct btrfs_super_block,
907 compat_flags, 64);
908BTRFS_SETGET_STACK_FUNCS(super_compat_ro_flags, struct btrfs_super_block,
909 compat_ro_flags, 64);
910BTRFS_SETGET_STACK_FUNCS(super_incompat_flags, struct btrfs_super_block,
911 incompat_flags, 64);
912BTRFS_SETGET_STACK_FUNCS(super_csum_type, struct btrfs_super_block,
913 csum_type, 16);
914BTRFS_SETGET_STACK_FUNCS(super_cache_generation, struct btrfs_super_block,
915 cache_generation, 64);
916BTRFS_SETGET_STACK_FUNCS(super_magic, struct btrfs_super_block, magic, 64);
917BTRFS_SETGET_STACK_FUNCS(super_uuid_tree_generation, struct btrfs_super_block,
918 uuid_tree_generation, 64);
919BTRFS_SETGET_STACK_FUNCS(super_nr_global_roots, struct btrfs_super_block,
920 nr_global_roots, 64);
921
922/* struct btrfs_file_extent_item */
923BTRFS_SETGET_STACK_FUNCS(stack_file_extent_type, struct btrfs_file_extent_item,
924 type, 8);
925BTRFS_SETGET_STACK_FUNCS(stack_file_extent_disk_bytenr,
926 struct btrfs_file_extent_item, disk_bytenr, 64);
927BTRFS_SETGET_STACK_FUNCS(stack_file_extent_offset,
928 struct btrfs_file_extent_item, offset, 64);
929BTRFS_SETGET_STACK_FUNCS(stack_file_extent_generation,
930 struct btrfs_file_extent_item, generation, 64);
931BTRFS_SETGET_STACK_FUNCS(stack_file_extent_num_bytes,
932 struct btrfs_file_extent_item, num_bytes, 64);
933BTRFS_SETGET_STACK_FUNCS(stack_file_extent_ram_bytes,
934 struct btrfs_file_extent_item, ram_bytes, 64);
935BTRFS_SETGET_STACK_FUNCS(stack_file_extent_disk_num_bytes,
936 struct btrfs_file_extent_item, disk_num_bytes, 64);
937BTRFS_SETGET_STACK_FUNCS(stack_file_extent_compression,
938 struct btrfs_file_extent_item, compression, 8);
939
940
941BTRFS_SETGET_FUNCS(file_extent_type, struct btrfs_file_extent_item, type, 8);
942BTRFS_SETGET_FUNCS(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_FUNCS(file_extent_disk_num_bytes, struct btrfs_file_extent_item,
947 disk_num_bytes, 64);
948BTRFS_SETGET_FUNCS(file_extent_offset, struct btrfs_file_extent_item,
949 offset, 64);
950BTRFS_SETGET_FUNCS(file_extent_num_bytes, struct btrfs_file_extent_item,
951 num_bytes, 64);
952BTRFS_SETGET_FUNCS(file_extent_ram_bytes, struct btrfs_file_extent_item,
953 ram_bytes, 64);
954BTRFS_SETGET_FUNCS(file_extent_compression, struct btrfs_file_extent_item,
955 compression, 8);
956BTRFS_SETGET_FUNCS(file_extent_encryption, struct btrfs_file_extent_item,
957 encryption, 8);
958BTRFS_SETGET_FUNCS(file_extent_other_encoding, struct btrfs_file_extent_item,
959 other_encoding, 16);
960
961/* btrfs_qgroup_status_item */
962BTRFS_SETGET_FUNCS(qgroup_status_generation, struct btrfs_qgroup_status_item,
963 generation, 64);
964BTRFS_SETGET_FUNCS(qgroup_status_version, struct btrfs_qgroup_status_item,
965 version, 64);
966BTRFS_SETGET_FUNCS(qgroup_status_flags, struct btrfs_qgroup_status_item,
967 flags, 64);
968BTRFS_SETGET_FUNCS(qgroup_status_rescan, struct btrfs_qgroup_status_item,
969 rescan, 64);
970
971/* btrfs_qgroup_info_item */
972BTRFS_SETGET_FUNCS(qgroup_info_generation, struct btrfs_qgroup_info_item,
973 generation, 64);
974BTRFS_SETGET_FUNCS(qgroup_info_rfer, struct btrfs_qgroup_info_item, rfer, 64);
975BTRFS_SETGET_FUNCS(qgroup_info_rfer_cmpr, struct btrfs_qgroup_info_item,
976 rfer_cmpr, 64);
977BTRFS_SETGET_FUNCS(qgroup_info_excl, struct btrfs_qgroup_info_item, excl, 64);
978BTRFS_SETGET_FUNCS(qgroup_info_excl_cmpr, struct btrfs_qgroup_info_item,
979 excl_cmpr, 64);
980
981BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_generation,
982 struct btrfs_qgroup_info_item, generation, 64);
983BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_rfer, struct btrfs_qgroup_info_item,
984 rfer, 64);
985BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_rfer_cmpr,
986 struct btrfs_qgroup_info_item, rfer_cmpr, 64);
987BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_excl, struct btrfs_qgroup_info_item,
988 excl, 64);
989BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_excl_cmpr,
990 struct btrfs_qgroup_info_item, excl_cmpr, 64);
991
992/* btrfs_qgroup_limit_item */
993BTRFS_SETGET_FUNCS(qgroup_limit_flags, struct btrfs_qgroup_limit_item, flags, 64);
994BTRFS_SETGET_FUNCS(qgroup_limit_max_rfer, struct btrfs_qgroup_limit_item,
995 max_rfer, 64);
996BTRFS_SETGET_FUNCS(qgroup_limit_max_excl, struct btrfs_qgroup_limit_item,
997 max_excl, 64);
998BTRFS_SETGET_FUNCS(qgroup_limit_rsv_rfer, struct btrfs_qgroup_limit_item,
999 rsv_rfer, 64);
1000BTRFS_SETGET_FUNCS(qgroup_limit_rsv_excl, struct btrfs_qgroup_limit_item,
1001 rsv_excl, 64);
1002BTRFS_SETGET_STACK_FUNCS(stack_qgroup_limit_flags,
1003 struct btrfs_qgroup_limit_item, flags, 64);
1004BTRFS_SETGET_STACK_FUNCS(stack_qgroup_limit_max_rfer,
1005 struct btrfs_qgroup_limit_item, max_rfer, 64);
1006BTRFS_SETGET_STACK_FUNCS(stack_qgroup_limit_max_excl,
1007 struct btrfs_qgroup_limit_item, max_excl, 64);
1008BTRFS_SETGET_STACK_FUNCS(stack_qgroup_limit_rsv_rfer,
1009 struct btrfs_qgroup_limit_item, rsv_rfer, 64);
1010BTRFS_SETGET_STACK_FUNCS(stack_qgroup_limit_rsv_excl,
1011 struct btrfs_qgroup_limit_item, rsv_excl, 64);
1012
1013/* btrfs_dev_replace_item */
1014BTRFS_SETGET_FUNCS(dev_replace_src_devid,
1015 struct btrfs_dev_replace_item, src_devid, 64);
1016BTRFS_SETGET_FUNCS(dev_replace_cont_reading_from_srcdev_mode,
1017 struct btrfs_dev_replace_item, cont_reading_from_srcdev_mode,
1018 64);
1019BTRFS_SETGET_FUNCS(dev_replace_replace_state, struct btrfs_dev_replace_item,
1020 replace_state, 64);
1021BTRFS_SETGET_FUNCS(dev_replace_time_started, struct btrfs_dev_replace_item,
1022 time_started, 64);
1023BTRFS_SETGET_FUNCS(dev_replace_time_stopped, struct btrfs_dev_replace_item,
1024 time_stopped, 64);
1025BTRFS_SETGET_FUNCS(dev_replace_num_write_errors, struct btrfs_dev_replace_item,
1026 num_write_errors, 64);
1027BTRFS_SETGET_FUNCS(dev_replace_num_uncorrectable_read_errors,
1028 struct btrfs_dev_replace_item, num_uncorrectable_read_errors,
1029 64);
1030BTRFS_SETGET_FUNCS(dev_replace_cursor_left, struct btrfs_dev_replace_item,
1031 cursor_left, 64);
1032BTRFS_SETGET_FUNCS(dev_replace_cursor_right, struct btrfs_dev_replace_item,
1033 cursor_right, 64);
1034
1035BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_src_devid,
1036 struct btrfs_dev_replace_item, src_devid, 64);
1037BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_cont_reading_from_srcdev_mode,
1038 struct btrfs_dev_replace_item,
1039 cont_reading_from_srcdev_mode, 64);
1040BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_replace_state,
1041 struct btrfs_dev_replace_item, replace_state, 64);
1042BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_time_started,
1043 struct btrfs_dev_replace_item, time_started, 64);
1044BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_time_stopped,
1045 struct btrfs_dev_replace_item, time_stopped, 64);
1046BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_num_write_errors,
1047 struct btrfs_dev_replace_item, num_write_errors, 64);
1048BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_num_uncorrectable_read_errors,
1049 struct btrfs_dev_replace_item,
1050 num_uncorrectable_read_errors, 64);
1051BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_cursor_left,
1052 struct btrfs_dev_replace_item, cursor_left, 64);
1053BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_cursor_right,
1054 struct btrfs_dev_replace_item, cursor_right, 64);
1055
1056/* btrfs_verity_descriptor_item */
1057BTRFS_SETGET_FUNCS(verity_descriptor_encryption, struct btrfs_verity_descriptor_item,
1058 encryption, 8);
1059BTRFS_SETGET_FUNCS(verity_descriptor_size, struct btrfs_verity_descriptor_item,
1060 size, 64);
1061BTRFS_SETGET_STACK_FUNCS(stack_verity_descriptor_encryption,
1062 struct btrfs_verity_descriptor_item, encryption, 8);
1063BTRFS_SETGET_STACK_FUNCS(stack_verity_descriptor_size,
1064 struct btrfs_verity_descriptor_item, size, 64);
1065
1066/* Cast into the data area of the leaf. */
1067#define btrfs_item_ptr(leaf, slot, type) \
1068 ((type *)(btrfs_item_nr_offset(leaf, 0) + btrfs_item_offset(leaf, slot)))
1069
1070#define btrfs_item_ptr_offset(leaf, slot) \
1071 ((unsigned long)(btrfs_item_nr_offset(leaf, 0) + btrfs_item_offset(leaf, slot)))
1072
1073#endif
1/* SPDX-License-Identifier: GPL-2.0 */
2
3#ifndef BTRFS_ACCESSORS_H
4#define BTRFS_ACCESSORS_H
5
6#include <asm/unaligned.h>
7#include <linux/stddef.h>
8#include <linux/types.h>
9#include <linux/align.h>
10#include <linux/build_bug.h>
11#include <linux/compiler.h>
12#include <linux/string.h>
13#include <linux/mm.h>
14#include <uapi/linux/btrfs_tree.h>
15
16struct extent_buffer;
17
18struct btrfs_map_token {
19 struct extent_buffer *eb;
20 char *kaddr;
21 unsigned long offset;
22};
23
24void btrfs_init_map_token(struct btrfs_map_token *token, struct extent_buffer *eb);
25
26/*
27 * Some macros to generate set/get functions for the struct fields. This
28 * assumes there is a lefoo_to_cpu for every type, so lets make a simple one
29 * for u8:
30 */
31#define le8_to_cpu(v) (v)
32#define cpu_to_le8(v) (v)
33#define __le8 u8
34
35static inline u8 get_unaligned_le8(const void *p)
36{
37 return *(u8 *)p;
38}
39
40static inline void put_unaligned_le8(u8 val, void *p)
41{
42 *(u8 *)p = val;
43}
44
45#define read_eb_member(eb, ptr, type, member, result) (\
46 read_extent_buffer(eb, (char *)(result), \
47 ((unsigned long)(ptr)) + \
48 offsetof(type, member), \
49 sizeof_field(type, member)))
50
51#define write_eb_member(eb, ptr, type, member, result) (\
52 write_extent_buffer(eb, (char *)(result), \
53 ((unsigned long)(ptr)) + \
54 offsetof(type, member), \
55 sizeof_field(type, member)))
56
57#define DECLARE_BTRFS_SETGET_BITS(bits) \
58u##bits btrfs_get_token_##bits(struct btrfs_map_token *token, \
59 const void *ptr, unsigned long off); \
60void btrfs_set_token_##bits(struct btrfs_map_token *token, \
61 const void *ptr, unsigned long off, \
62 u##bits val); \
63u##bits btrfs_get_##bits(const struct extent_buffer *eb, \
64 const void *ptr, unsigned long off); \
65void btrfs_set_##bits(const struct extent_buffer *eb, void *ptr, \
66 unsigned long off, u##bits val);
67
68DECLARE_BTRFS_SETGET_BITS(8)
69DECLARE_BTRFS_SETGET_BITS(16)
70DECLARE_BTRFS_SETGET_BITS(32)
71DECLARE_BTRFS_SETGET_BITS(64)
72
73#define BTRFS_SETGET_FUNCS(name, type, member, bits) \
74static inline u##bits btrfs_##name(const struct extent_buffer *eb, \
75 const type *s) \
76{ \
77 static_assert(sizeof(u##bits) == sizeof_field(type, member)); \
78 return btrfs_get_##bits(eb, s, offsetof(type, member)); \
79} \
80static inline void btrfs_set_##name(const struct extent_buffer *eb, type *s, \
81 u##bits val) \
82{ \
83 static_assert(sizeof(u##bits) == sizeof_field(type, member)); \
84 btrfs_set_##bits(eb, s, offsetof(type, member), val); \
85} \
86static inline u##bits btrfs_token_##name(struct btrfs_map_token *token, \
87 const type *s) \
88{ \
89 static_assert(sizeof(u##bits) == sizeof_field(type, member)); \
90 return btrfs_get_token_##bits(token, s, offsetof(type, member));\
91} \
92static inline void btrfs_set_token_##name(struct btrfs_map_token *token,\
93 type *s, u##bits val) \
94{ \
95 static_assert(sizeof(u##bits) == sizeof_field(type, member)); \
96 btrfs_set_token_##bits(token, s, offsetof(type, member), val); \
97}
98
99#define BTRFS_SETGET_HEADER_FUNCS(name, type, member, bits) \
100static inline u##bits btrfs_##name(const struct extent_buffer *eb) \
101{ \
102 const type *p = folio_address(eb->folios[0]) + \
103 offset_in_page(eb->start); \
104 return get_unaligned_le##bits(&p->member); \
105} \
106static inline void btrfs_set_##name(const struct extent_buffer *eb, \
107 u##bits val) \
108{ \
109 type *p = folio_address(eb->folios[0]) + offset_in_page(eb->start); \
110 put_unaligned_le##bits(val, &p->member); \
111}
112
113#define BTRFS_SETGET_STACK_FUNCS(name, type, member, bits) \
114static inline u##bits btrfs_##name(const type *s) \
115{ \
116 return get_unaligned_le##bits(&s->member); \
117} \
118static inline void btrfs_set_##name(type *s, u##bits val) \
119{ \
120 put_unaligned_le##bits(val, &s->member); \
121}
122
123static inline u64 btrfs_device_total_bytes(const struct extent_buffer *eb,
124 struct btrfs_dev_item *s)
125{
126 static_assert(sizeof(u64) == sizeof_field(struct btrfs_dev_item, total_bytes));
127 return btrfs_get_64(eb, s, offsetof(struct btrfs_dev_item, total_bytes));
128}
129static inline void btrfs_set_device_total_bytes(const struct extent_buffer *eb,
130 struct btrfs_dev_item *s,
131 u64 val)
132{
133 static_assert(sizeof(u64) == sizeof_field(struct btrfs_dev_item, total_bytes));
134 WARN_ON(!IS_ALIGNED(val, eb->fs_info->sectorsize));
135 btrfs_set_64(eb, s, offsetof(struct btrfs_dev_item, total_bytes), val);
136}
137
138BTRFS_SETGET_FUNCS(device_type, struct btrfs_dev_item, type, 64);
139BTRFS_SETGET_FUNCS(device_bytes_used, struct btrfs_dev_item, bytes_used, 64);
140BTRFS_SETGET_FUNCS(device_io_align, struct btrfs_dev_item, io_align, 32);
141BTRFS_SETGET_FUNCS(device_io_width, struct btrfs_dev_item, io_width, 32);
142BTRFS_SETGET_FUNCS(device_start_offset, struct btrfs_dev_item, start_offset, 64);
143BTRFS_SETGET_FUNCS(device_sector_size, struct btrfs_dev_item, sector_size, 32);
144BTRFS_SETGET_FUNCS(device_id, struct btrfs_dev_item, devid, 64);
145BTRFS_SETGET_FUNCS(device_group, struct btrfs_dev_item, dev_group, 32);
146BTRFS_SETGET_FUNCS(device_seek_speed, struct btrfs_dev_item, seek_speed, 8);
147BTRFS_SETGET_FUNCS(device_bandwidth, struct btrfs_dev_item, bandwidth, 8);
148BTRFS_SETGET_FUNCS(device_generation, struct btrfs_dev_item, generation, 64);
149
150BTRFS_SETGET_STACK_FUNCS(stack_device_type, struct btrfs_dev_item, type, 64);
151BTRFS_SETGET_STACK_FUNCS(stack_device_total_bytes, struct btrfs_dev_item,
152 total_bytes, 64);
153BTRFS_SETGET_STACK_FUNCS(stack_device_bytes_used, struct btrfs_dev_item,
154 bytes_used, 64);
155BTRFS_SETGET_STACK_FUNCS(stack_device_io_align, struct btrfs_dev_item,
156 io_align, 32);
157BTRFS_SETGET_STACK_FUNCS(stack_device_io_width, struct btrfs_dev_item,
158 io_width, 32);
159BTRFS_SETGET_STACK_FUNCS(stack_device_sector_size, struct btrfs_dev_item,
160 sector_size, 32);
161BTRFS_SETGET_STACK_FUNCS(stack_device_id, struct btrfs_dev_item, devid, 64);
162BTRFS_SETGET_STACK_FUNCS(stack_device_group, struct btrfs_dev_item, dev_group, 32);
163BTRFS_SETGET_STACK_FUNCS(stack_device_seek_speed, struct btrfs_dev_item,
164 seek_speed, 8);
165BTRFS_SETGET_STACK_FUNCS(stack_device_bandwidth, struct btrfs_dev_item,
166 bandwidth, 8);
167BTRFS_SETGET_STACK_FUNCS(stack_device_generation, struct btrfs_dev_item,
168 generation, 64);
169
170static inline unsigned long btrfs_device_uuid(struct btrfs_dev_item *d)
171{
172 return (unsigned long)d + offsetof(struct btrfs_dev_item, uuid);
173}
174
175static inline unsigned long btrfs_device_fsid(struct btrfs_dev_item *d)
176{
177 return (unsigned long)d + offsetof(struct btrfs_dev_item, fsid);
178}
179
180BTRFS_SETGET_FUNCS(chunk_length, struct btrfs_chunk, length, 64);
181BTRFS_SETGET_FUNCS(chunk_owner, struct btrfs_chunk, owner, 64);
182BTRFS_SETGET_FUNCS(chunk_stripe_len, struct btrfs_chunk, stripe_len, 64);
183BTRFS_SETGET_FUNCS(chunk_io_align, struct btrfs_chunk, io_align, 32);
184BTRFS_SETGET_FUNCS(chunk_io_width, struct btrfs_chunk, io_width, 32);
185BTRFS_SETGET_FUNCS(chunk_sector_size, struct btrfs_chunk, sector_size, 32);
186BTRFS_SETGET_FUNCS(chunk_type, struct btrfs_chunk, type, 64);
187BTRFS_SETGET_FUNCS(chunk_num_stripes, struct btrfs_chunk, num_stripes, 16);
188BTRFS_SETGET_FUNCS(chunk_sub_stripes, struct btrfs_chunk, sub_stripes, 16);
189BTRFS_SETGET_FUNCS(stripe_devid, struct btrfs_stripe, devid, 64);
190BTRFS_SETGET_FUNCS(stripe_offset, struct btrfs_stripe, offset, 64);
191
192static inline char *btrfs_stripe_dev_uuid(struct btrfs_stripe *s)
193{
194 return (char *)s + offsetof(struct btrfs_stripe, dev_uuid);
195}
196
197BTRFS_SETGET_STACK_FUNCS(stack_chunk_length, struct btrfs_chunk, length, 64);
198BTRFS_SETGET_STACK_FUNCS(stack_chunk_owner, struct btrfs_chunk, owner, 64);
199BTRFS_SETGET_STACK_FUNCS(stack_chunk_stripe_len, struct btrfs_chunk,
200 stripe_len, 64);
201BTRFS_SETGET_STACK_FUNCS(stack_chunk_io_align, struct btrfs_chunk, io_align, 32);
202BTRFS_SETGET_STACK_FUNCS(stack_chunk_io_width, struct btrfs_chunk, io_width, 32);
203BTRFS_SETGET_STACK_FUNCS(stack_chunk_sector_size, struct btrfs_chunk,
204 sector_size, 32);
205BTRFS_SETGET_STACK_FUNCS(stack_chunk_type, struct btrfs_chunk, type, 64);
206BTRFS_SETGET_STACK_FUNCS(stack_chunk_num_stripes, struct btrfs_chunk,
207 num_stripes, 16);
208BTRFS_SETGET_STACK_FUNCS(stack_chunk_sub_stripes, struct btrfs_chunk,
209 sub_stripes, 16);
210BTRFS_SETGET_STACK_FUNCS(stack_stripe_devid, struct btrfs_stripe, devid, 64);
211BTRFS_SETGET_STACK_FUNCS(stack_stripe_offset, struct btrfs_stripe, offset, 64);
212
213static inline struct btrfs_stripe *btrfs_stripe_nr(struct btrfs_chunk *c, int nr)
214{
215 unsigned long offset = (unsigned long)c;
216
217 offset += offsetof(struct btrfs_chunk, stripe);
218 offset += nr * sizeof(struct btrfs_stripe);
219 return (struct btrfs_stripe *)offset;
220}
221
222static inline char *btrfs_stripe_dev_uuid_nr(struct btrfs_chunk *c, int nr)
223{
224 return btrfs_stripe_dev_uuid(btrfs_stripe_nr(c, nr));
225}
226
227static inline u64 btrfs_stripe_offset_nr(const struct extent_buffer *eb,
228 struct btrfs_chunk *c, int nr)
229{
230 return btrfs_stripe_offset(eb, btrfs_stripe_nr(c, nr));
231}
232
233static inline void btrfs_set_stripe_offset_nr(struct extent_buffer *eb,
234 struct btrfs_chunk *c, int nr,
235 u64 val)
236{
237 btrfs_set_stripe_offset(eb, btrfs_stripe_nr(c, nr), val);
238}
239
240static inline u64 btrfs_stripe_devid_nr(const struct extent_buffer *eb,
241 struct btrfs_chunk *c, int nr)
242{
243 return btrfs_stripe_devid(eb, btrfs_stripe_nr(c, nr));
244}
245
246static inline void btrfs_set_stripe_devid_nr(struct extent_buffer *eb,
247 struct btrfs_chunk *c, int nr,
248 u64 val)
249{
250 btrfs_set_stripe_devid(eb, btrfs_stripe_nr(c, nr), val);
251}
252
253/* struct btrfs_block_group_item */
254BTRFS_SETGET_STACK_FUNCS(stack_block_group_used, struct btrfs_block_group_item,
255 used, 64);
256BTRFS_SETGET_FUNCS(block_group_used, struct btrfs_block_group_item, used, 64);
257BTRFS_SETGET_STACK_FUNCS(stack_block_group_chunk_objectid,
258 struct btrfs_block_group_item, chunk_objectid, 64);
259
260BTRFS_SETGET_FUNCS(block_group_chunk_objectid,
261 struct btrfs_block_group_item, chunk_objectid, 64);
262BTRFS_SETGET_FUNCS(block_group_flags, struct btrfs_block_group_item, flags, 64);
263BTRFS_SETGET_STACK_FUNCS(stack_block_group_flags,
264 struct btrfs_block_group_item, flags, 64);
265
266/* struct btrfs_free_space_info */
267BTRFS_SETGET_FUNCS(free_space_extent_count, struct btrfs_free_space_info,
268 extent_count, 32);
269BTRFS_SETGET_FUNCS(free_space_flags, struct btrfs_free_space_info, flags, 32);
270
271/* struct btrfs_inode_ref */
272BTRFS_SETGET_FUNCS(inode_ref_name_len, struct btrfs_inode_ref, name_len, 16);
273BTRFS_SETGET_FUNCS(inode_ref_index, struct btrfs_inode_ref, index, 64);
274BTRFS_SETGET_STACK_FUNCS(stack_inode_ref_name_len, struct btrfs_inode_ref, name_len, 16);
275BTRFS_SETGET_STACK_FUNCS(stack_inode_ref_index, struct btrfs_inode_ref, index, 64);
276
277/* struct btrfs_inode_extref */
278BTRFS_SETGET_FUNCS(inode_extref_parent, struct btrfs_inode_extref,
279 parent_objectid, 64);
280BTRFS_SETGET_FUNCS(inode_extref_name_len, struct btrfs_inode_extref,
281 name_len, 16);
282BTRFS_SETGET_FUNCS(inode_extref_index, struct btrfs_inode_extref, index, 64);
283
284/* struct btrfs_inode_item */
285BTRFS_SETGET_FUNCS(inode_generation, struct btrfs_inode_item, generation, 64);
286BTRFS_SETGET_FUNCS(inode_sequence, struct btrfs_inode_item, sequence, 64);
287BTRFS_SETGET_FUNCS(inode_transid, struct btrfs_inode_item, transid, 64);
288BTRFS_SETGET_FUNCS(inode_size, struct btrfs_inode_item, size, 64);
289BTRFS_SETGET_FUNCS(inode_nbytes, struct btrfs_inode_item, nbytes, 64);
290BTRFS_SETGET_FUNCS(inode_block_group, struct btrfs_inode_item, block_group, 64);
291BTRFS_SETGET_FUNCS(inode_nlink, struct btrfs_inode_item, nlink, 32);
292BTRFS_SETGET_FUNCS(inode_uid, struct btrfs_inode_item, uid, 32);
293BTRFS_SETGET_FUNCS(inode_gid, struct btrfs_inode_item, gid, 32);
294BTRFS_SETGET_FUNCS(inode_mode, struct btrfs_inode_item, mode, 32);
295BTRFS_SETGET_FUNCS(inode_rdev, struct btrfs_inode_item, rdev, 64);
296BTRFS_SETGET_FUNCS(inode_flags, struct btrfs_inode_item, flags, 64);
297BTRFS_SETGET_STACK_FUNCS(stack_inode_generation, struct btrfs_inode_item,
298 generation, 64);
299BTRFS_SETGET_STACK_FUNCS(stack_inode_sequence, struct btrfs_inode_item,
300 sequence, 64);
301BTRFS_SETGET_STACK_FUNCS(stack_inode_transid, struct btrfs_inode_item,
302 transid, 64);
303BTRFS_SETGET_STACK_FUNCS(stack_inode_size, struct btrfs_inode_item, size, 64);
304BTRFS_SETGET_STACK_FUNCS(stack_inode_nbytes, struct btrfs_inode_item, nbytes, 64);
305BTRFS_SETGET_STACK_FUNCS(stack_inode_block_group, struct btrfs_inode_item,
306 block_group, 64);
307BTRFS_SETGET_STACK_FUNCS(stack_inode_nlink, struct btrfs_inode_item, nlink, 32);
308BTRFS_SETGET_STACK_FUNCS(stack_inode_uid, struct btrfs_inode_item, uid, 32);
309BTRFS_SETGET_STACK_FUNCS(stack_inode_gid, struct btrfs_inode_item, gid, 32);
310BTRFS_SETGET_STACK_FUNCS(stack_inode_mode, struct btrfs_inode_item, mode, 32);
311BTRFS_SETGET_STACK_FUNCS(stack_inode_rdev, struct btrfs_inode_item, rdev, 64);
312BTRFS_SETGET_STACK_FUNCS(stack_inode_flags, struct btrfs_inode_item, flags, 64);
313BTRFS_SETGET_FUNCS(timespec_sec, struct btrfs_timespec, sec, 64);
314BTRFS_SETGET_FUNCS(timespec_nsec, struct btrfs_timespec, nsec, 32);
315BTRFS_SETGET_STACK_FUNCS(stack_timespec_sec, struct btrfs_timespec, sec, 64);
316BTRFS_SETGET_STACK_FUNCS(stack_timespec_nsec, struct btrfs_timespec, nsec, 32);
317
318BTRFS_SETGET_FUNCS(stripe_extent_encoding, struct btrfs_stripe_extent, encoding, 8);
319BTRFS_SETGET_FUNCS(raid_stride_devid, struct btrfs_raid_stride, devid, 64);
320BTRFS_SETGET_FUNCS(raid_stride_physical, struct btrfs_raid_stride, physical, 64);
321BTRFS_SETGET_STACK_FUNCS(stack_stripe_extent_encoding,
322 struct btrfs_stripe_extent, encoding, 8);
323BTRFS_SETGET_STACK_FUNCS(stack_raid_stride_devid, struct btrfs_raid_stride, devid, 64);
324BTRFS_SETGET_STACK_FUNCS(stack_raid_stride_physical, struct btrfs_raid_stride, physical, 64);
325
326/* struct btrfs_dev_extent */
327BTRFS_SETGET_FUNCS(dev_extent_chunk_tree, struct btrfs_dev_extent, chunk_tree, 64);
328BTRFS_SETGET_FUNCS(dev_extent_chunk_objectid, struct btrfs_dev_extent,
329 chunk_objectid, 64);
330BTRFS_SETGET_FUNCS(dev_extent_chunk_offset, struct btrfs_dev_extent,
331 chunk_offset, 64);
332BTRFS_SETGET_FUNCS(dev_extent_length, struct btrfs_dev_extent, length, 64);
333BTRFS_SETGET_STACK_FUNCS(stack_dev_extent_chunk_tree, struct btrfs_dev_extent,
334 chunk_tree, 64);
335BTRFS_SETGET_STACK_FUNCS(stack_dev_extent_chunk_objectid, struct btrfs_dev_extent,
336 chunk_objectid, 64);
337BTRFS_SETGET_STACK_FUNCS(stack_dev_extent_chunk_offset, struct btrfs_dev_extent,
338 chunk_offset, 64);
339BTRFS_SETGET_STACK_FUNCS(stack_dev_extent_length, struct btrfs_dev_extent, length, 64);
340
341BTRFS_SETGET_FUNCS(extent_refs, struct btrfs_extent_item, refs, 64);
342BTRFS_SETGET_FUNCS(extent_generation, struct btrfs_extent_item, generation, 64);
343BTRFS_SETGET_FUNCS(extent_flags, struct btrfs_extent_item, flags, 64);
344
345BTRFS_SETGET_FUNCS(tree_block_level, struct btrfs_tree_block_info, level, 8);
346
347static inline void btrfs_tree_block_key(const struct extent_buffer *eb,
348 struct btrfs_tree_block_info *item,
349 struct btrfs_disk_key *key)
350{
351 read_eb_member(eb, item, struct btrfs_tree_block_info, key, key);
352}
353
354static inline void btrfs_set_tree_block_key(const struct extent_buffer *eb,
355 struct btrfs_tree_block_info *item,
356 struct btrfs_disk_key *key)
357{
358 write_eb_member(eb, item, struct btrfs_tree_block_info, key, key);
359}
360
361BTRFS_SETGET_FUNCS(extent_data_ref_root, struct btrfs_extent_data_ref, root, 64);
362BTRFS_SETGET_FUNCS(extent_data_ref_objectid, struct btrfs_extent_data_ref,
363 objectid, 64);
364BTRFS_SETGET_FUNCS(extent_data_ref_offset, struct btrfs_extent_data_ref,
365 offset, 64);
366BTRFS_SETGET_FUNCS(extent_data_ref_count, struct btrfs_extent_data_ref, count, 32);
367
368BTRFS_SETGET_FUNCS(shared_data_ref_count, struct btrfs_shared_data_ref, count, 32);
369
370BTRFS_SETGET_FUNCS(extent_owner_ref_root_id, struct btrfs_extent_owner_ref,
371 root_id, 64);
372
373BTRFS_SETGET_FUNCS(extent_inline_ref_type, struct btrfs_extent_inline_ref,
374 type, 8);
375BTRFS_SETGET_FUNCS(extent_inline_ref_offset, struct btrfs_extent_inline_ref,
376 offset, 64);
377
378static inline u32 btrfs_extent_inline_ref_size(int type)
379{
380 if (type == BTRFS_TREE_BLOCK_REF_KEY ||
381 type == BTRFS_SHARED_BLOCK_REF_KEY)
382 return sizeof(struct btrfs_extent_inline_ref);
383 if (type == BTRFS_SHARED_DATA_REF_KEY)
384 return sizeof(struct btrfs_shared_data_ref) +
385 sizeof(struct btrfs_extent_inline_ref);
386 if (type == BTRFS_EXTENT_DATA_REF_KEY)
387 return sizeof(struct btrfs_extent_data_ref) +
388 offsetof(struct btrfs_extent_inline_ref, offset);
389 if (type == BTRFS_EXTENT_OWNER_REF_KEY)
390 return sizeof(struct btrfs_extent_inline_ref);
391 return 0;
392}
393
394/* struct btrfs_node */
395BTRFS_SETGET_FUNCS(key_blockptr, struct btrfs_key_ptr, blockptr, 64);
396BTRFS_SETGET_FUNCS(key_generation, struct btrfs_key_ptr, generation, 64);
397BTRFS_SETGET_STACK_FUNCS(stack_key_blockptr, struct btrfs_key_ptr, blockptr, 64);
398BTRFS_SETGET_STACK_FUNCS(stack_key_generation, struct btrfs_key_ptr,
399 generation, 64);
400
401static inline u64 btrfs_node_blockptr(const struct extent_buffer *eb, int nr)
402{
403 unsigned long ptr;
404
405 ptr = offsetof(struct btrfs_node, ptrs) +
406 sizeof(struct btrfs_key_ptr) * nr;
407 return btrfs_key_blockptr(eb, (struct btrfs_key_ptr *)ptr);
408}
409
410static inline void btrfs_set_node_blockptr(const struct extent_buffer *eb,
411 int nr, u64 val)
412{
413 unsigned long ptr;
414
415 ptr = offsetof(struct btrfs_node, ptrs) +
416 sizeof(struct btrfs_key_ptr) * nr;
417 btrfs_set_key_blockptr(eb, (struct btrfs_key_ptr *)ptr, val);
418}
419
420static inline u64 btrfs_node_ptr_generation(const struct extent_buffer *eb, int nr)
421{
422 unsigned long ptr;
423
424 ptr = offsetof(struct btrfs_node, ptrs) +
425 sizeof(struct btrfs_key_ptr) * nr;
426 return btrfs_key_generation(eb, (struct btrfs_key_ptr *)ptr);
427}
428
429static inline void btrfs_set_node_ptr_generation(const struct extent_buffer *eb,
430 int nr, u64 val)
431{
432 unsigned long ptr;
433
434 ptr = offsetof(struct btrfs_node, ptrs) +
435 sizeof(struct btrfs_key_ptr) * nr;
436 btrfs_set_key_generation(eb, (struct btrfs_key_ptr *)ptr, val);
437}
438
439static inline unsigned long btrfs_node_key_ptr_offset(const struct extent_buffer *eb, int nr)
440{
441 return offsetof(struct btrfs_node, ptrs) +
442 sizeof(struct btrfs_key_ptr) * nr;
443}
444
445void btrfs_node_key(const struct extent_buffer *eb,
446 struct btrfs_disk_key *disk_key, int nr);
447
448static inline void btrfs_set_node_key(const struct extent_buffer *eb,
449 struct btrfs_disk_key *disk_key, int nr)
450{
451 unsigned long ptr;
452
453 ptr = btrfs_node_key_ptr_offset(eb, nr);
454 write_eb_member(eb, (struct btrfs_key_ptr *)ptr,
455 struct btrfs_key_ptr, key, disk_key);
456}
457
458/* struct btrfs_item */
459BTRFS_SETGET_FUNCS(raw_item_offset, struct btrfs_item, offset, 32);
460BTRFS_SETGET_FUNCS(raw_item_size, struct btrfs_item, size, 32);
461BTRFS_SETGET_STACK_FUNCS(stack_item_offset, struct btrfs_item, offset, 32);
462BTRFS_SETGET_STACK_FUNCS(stack_item_size, struct btrfs_item, size, 32);
463
464static inline unsigned long btrfs_item_nr_offset(const struct extent_buffer *eb, int nr)
465{
466 return offsetof(struct btrfs_leaf, items) +
467 sizeof(struct btrfs_item) * nr;
468}
469
470static inline struct btrfs_item *btrfs_item_nr(const struct extent_buffer *eb, int nr)
471{
472 return (struct btrfs_item *)btrfs_item_nr_offset(eb, nr);
473}
474
475#define BTRFS_ITEM_SETGET_FUNCS(member) \
476static inline u32 btrfs_item_##member(const struct extent_buffer *eb, int slot) \
477{ \
478 return btrfs_raw_item_##member(eb, btrfs_item_nr(eb, slot)); \
479} \
480static inline void btrfs_set_item_##member(const struct extent_buffer *eb, \
481 int slot, u32 val) \
482{ \
483 btrfs_set_raw_item_##member(eb, btrfs_item_nr(eb, slot), val); \
484} \
485static inline u32 btrfs_token_item_##member(struct btrfs_map_token *token, \
486 int slot) \
487{ \
488 struct btrfs_item *item = btrfs_item_nr(token->eb, slot); \
489 return btrfs_token_raw_item_##member(token, item); \
490} \
491static inline void btrfs_set_token_item_##member(struct btrfs_map_token *token, \
492 int slot, u32 val) \
493{ \
494 struct btrfs_item *item = btrfs_item_nr(token->eb, slot); \
495 btrfs_set_token_raw_item_##member(token, item, val); \
496}
497
498BTRFS_ITEM_SETGET_FUNCS(offset)
499BTRFS_ITEM_SETGET_FUNCS(size);
500
501static inline u32 btrfs_item_data_end(const struct extent_buffer *eb, int nr)
502{
503 return btrfs_item_offset(eb, nr) + btrfs_item_size(eb, nr);
504}
505
506static inline void btrfs_item_key(const struct extent_buffer *eb,
507 struct btrfs_disk_key *disk_key, int nr)
508{
509 struct btrfs_item *item = btrfs_item_nr(eb, nr);
510
511 read_eb_member(eb, item, struct btrfs_item, key, disk_key);
512}
513
514static inline void btrfs_set_item_key(struct extent_buffer *eb,
515 struct btrfs_disk_key *disk_key, int nr)
516{
517 struct btrfs_item *item = btrfs_item_nr(eb, nr);
518
519 write_eb_member(eb, item, struct btrfs_item, key, disk_key);
520}
521
522BTRFS_SETGET_FUNCS(dir_log_end, struct btrfs_dir_log_item, end, 64);
523
524/* struct btrfs_root_ref */
525BTRFS_SETGET_FUNCS(root_ref_dirid, struct btrfs_root_ref, dirid, 64);
526BTRFS_SETGET_FUNCS(root_ref_sequence, struct btrfs_root_ref, sequence, 64);
527BTRFS_SETGET_FUNCS(root_ref_name_len, struct btrfs_root_ref, name_len, 16);
528BTRFS_SETGET_STACK_FUNCS(stack_root_ref_dirid, struct btrfs_root_ref, dirid, 64);
529BTRFS_SETGET_STACK_FUNCS(stack_root_ref_sequence, struct btrfs_root_ref, sequence, 64);
530BTRFS_SETGET_STACK_FUNCS(stack_root_ref_name_len, struct btrfs_root_ref, name_len, 16);
531
532/* struct btrfs_dir_item */
533BTRFS_SETGET_FUNCS(dir_data_len, struct btrfs_dir_item, data_len, 16);
534BTRFS_SETGET_FUNCS(dir_flags, struct btrfs_dir_item, type, 8);
535BTRFS_SETGET_FUNCS(dir_name_len, struct btrfs_dir_item, name_len, 16);
536BTRFS_SETGET_FUNCS(dir_transid, struct btrfs_dir_item, transid, 64);
537BTRFS_SETGET_STACK_FUNCS(stack_dir_flags, struct btrfs_dir_item, type, 8);
538BTRFS_SETGET_STACK_FUNCS(stack_dir_data_len, struct btrfs_dir_item, data_len, 16);
539BTRFS_SETGET_STACK_FUNCS(stack_dir_name_len, struct btrfs_dir_item, name_len, 16);
540BTRFS_SETGET_STACK_FUNCS(stack_dir_transid, struct btrfs_dir_item, transid, 64);
541
542static inline u8 btrfs_dir_ftype(const struct extent_buffer *eb,
543 const struct btrfs_dir_item *item)
544{
545 return btrfs_dir_flags_to_ftype(btrfs_dir_flags(eb, item));
546}
547
548static inline u8 btrfs_stack_dir_ftype(const struct btrfs_dir_item *item)
549{
550 return btrfs_dir_flags_to_ftype(btrfs_stack_dir_flags(item));
551}
552
553static inline void btrfs_dir_item_key(const struct extent_buffer *eb,
554 const struct btrfs_dir_item *item,
555 struct btrfs_disk_key *key)
556{
557 read_eb_member(eb, item, struct btrfs_dir_item, location, key);
558}
559
560static inline void btrfs_set_dir_item_key(struct extent_buffer *eb,
561 struct btrfs_dir_item *item,
562 const struct btrfs_disk_key *key)
563{
564 write_eb_member(eb, item, struct btrfs_dir_item, location, key);
565}
566
567BTRFS_SETGET_FUNCS(free_space_entries, struct btrfs_free_space_header,
568 num_entries, 64);
569BTRFS_SETGET_FUNCS(free_space_bitmaps, struct btrfs_free_space_header,
570 num_bitmaps, 64);
571BTRFS_SETGET_FUNCS(free_space_generation, struct btrfs_free_space_header,
572 generation, 64);
573
574static inline void btrfs_free_space_key(const struct extent_buffer *eb,
575 const struct btrfs_free_space_header *h,
576 struct btrfs_disk_key *key)
577{
578 read_eb_member(eb, h, struct btrfs_free_space_header, location, key);
579}
580
581static inline void btrfs_set_free_space_key(struct extent_buffer *eb,
582 struct btrfs_free_space_header *h,
583 const struct btrfs_disk_key *key)
584{
585 write_eb_member(eb, h, struct btrfs_free_space_header, location, key);
586}
587
588/* struct btrfs_disk_key */
589BTRFS_SETGET_STACK_FUNCS(disk_key_objectid, struct btrfs_disk_key, objectid, 64);
590BTRFS_SETGET_STACK_FUNCS(disk_key_offset, struct btrfs_disk_key, offset, 64);
591BTRFS_SETGET_STACK_FUNCS(disk_key_type, struct btrfs_disk_key, type, 8);
592
593#ifdef __LITTLE_ENDIAN
594
595/*
596 * Optimized helpers for little-endian architectures where CPU and on-disk
597 * structures have the same endianness and we can skip conversions.
598 */
599
600static inline void btrfs_disk_key_to_cpu(struct btrfs_key *cpu_key,
601 const struct btrfs_disk_key *disk_key)
602{
603 memcpy(cpu_key, disk_key, sizeof(struct btrfs_key));
604}
605
606static inline void btrfs_cpu_key_to_disk(struct btrfs_disk_key *disk_key,
607 const struct btrfs_key *cpu_key)
608{
609 memcpy(disk_key, cpu_key, sizeof(struct btrfs_key));
610}
611
612static inline void btrfs_node_key_to_cpu(const struct extent_buffer *eb,
613 struct btrfs_key *cpu_key, int nr)
614{
615 struct btrfs_disk_key *disk_key = (struct btrfs_disk_key *)cpu_key;
616
617 btrfs_node_key(eb, disk_key, nr);
618}
619
620static inline void btrfs_item_key_to_cpu(const struct extent_buffer *eb,
621 struct btrfs_key *cpu_key, int nr)
622{
623 struct btrfs_disk_key *disk_key = (struct btrfs_disk_key *)cpu_key;
624
625 btrfs_item_key(eb, disk_key, nr);
626}
627
628static inline void btrfs_dir_item_key_to_cpu(const struct extent_buffer *eb,
629 const struct btrfs_dir_item *item,
630 struct btrfs_key *cpu_key)
631{
632 struct btrfs_disk_key *disk_key = (struct btrfs_disk_key *)cpu_key;
633
634 btrfs_dir_item_key(eb, item, disk_key);
635}
636
637#else
638
639static inline void btrfs_disk_key_to_cpu(struct btrfs_key *cpu,
640 const struct btrfs_disk_key *disk)
641{
642 cpu->offset = le64_to_cpu(disk->offset);
643 cpu->type = disk->type;
644 cpu->objectid = le64_to_cpu(disk->objectid);
645}
646
647static inline void btrfs_cpu_key_to_disk(struct btrfs_disk_key *disk,
648 const struct btrfs_key *cpu)
649{
650 disk->offset = cpu_to_le64(cpu->offset);
651 disk->type = cpu->type;
652 disk->objectid = cpu_to_le64(cpu->objectid);
653}
654
655static inline void btrfs_node_key_to_cpu(const struct extent_buffer *eb,
656 struct btrfs_key *key, int nr)
657{
658 struct btrfs_disk_key disk_key;
659
660 btrfs_node_key(eb, &disk_key, nr);
661 btrfs_disk_key_to_cpu(key, &disk_key);
662}
663
664static inline void btrfs_item_key_to_cpu(const struct extent_buffer *eb,
665 struct btrfs_key *key, int nr)
666{
667 struct btrfs_disk_key disk_key;
668
669 btrfs_item_key(eb, &disk_key, nr);
670 btrfs_disk_key_to_cpu(key, &disk_key);
671}
672
673static inline void btrfs_dir_item_key_to_cpu(const struct extent_buffer *eb,
674 const struct btrfs_dir_item *item,
675 struct btrfs_key *key)
676{
677 struct btrfs_disk_key disk_key;
678
679 btrfs_dir_item_key(eb, item, &disk_key);
680 btrfs_disk_key_to_cpu(key, &disk_key);
681}
682
683#endif
684
685/* struct btrfs_header */
686BTRFS_SETGET_HEADER_FUNCS(header_bytenr, struct btrfs_header, bytenr, 64);
687BTRFS_SETGET_HEADER_FUNCS(header_generation, struct btrfs_header, generation, 64);
688BTRFS_SETGET_HEADER_FUNCS(header_owner, struct btrfs_header, owner, 64);
689BTRFS_SETGET_HEADER_FUNCS(header_nritems, struct btrfs_header, nritems, 32);
690BTRFS_SETGET_HEADER_FUNCS(header_flags, struct btrfs_header, flags, 64);
691BTRFS_SETGET_HEADER_FUNCS(header_level, struct btrfs_header, level, 8);
692BTRFS_SETGET_STACK_FUNCS(stack_header_generation, struct btrfs_header,
693 generation, 64);
694BTRFS_SETGET_STACK_FUNCS(stack_header_owner, struct btrfs_header, owner, 64);
695BTRFS_SETGET_STACK_FUNCS(stack_header_nritems, struct btrfs_header, nritems, 32);
696BTRFS_SETGET_STACK_FUNCS(stack_header_bytenr, struct btrfs_header, bytenr, 64);
697
698static inline int btrfs_header_flag(const struct extent_buffer *eb, u64 flag)
699{
700 return (btrfs_header_flags(eb) & flag) == flag;
701}
702
703static inline void btrfs_set_header_flag(struct extent_buffer *eb, u64 flag)
704{
705 u64 flags = btrfs_header_flags(eb);
706
707 btrfs_set_header_flags(eb, flags | flag);
708}
709
710static inline void btrfs_clear_header_flag(struct extent_buffer *eb, u64 flag)
711{
712 u64 flags = btrfs_header_flags(eb);
713
714 btrfs_set_header_flags(eb, flags & ~flag);
715}
716
717static inline int btrfs_header_backref_rev(const struct extent_buffer *eb)
718{
719 u64 flags = btrfs_header_flags(eb);
720
721 return flags >> BTRFS_BACKREF_REV_SHIFT;
722}
723
724static inline void btrfs_set_header_backref_rev(struct extent_buffer *eb, int rev)
725{
726 u64 flags = btrfs_header_flags(eb);
727
728 flags &= ~BTRFS_BACKREF_REV_MASK;
729 flags |= (u64)rev << BTRFS_BACKREF_REV_SHIFT;
730 btrfs_set_header_flags(eb, flags);
731}
732
733static inline int btrfs_is_leaf(const struct extent_buffer *eb)
734{
735 return btrfs_header_level(eb) == 0;
736}
737
738/* struct btrfs_root_item */
739BTRFS_SETGET_FUNCS(disk_root_generation, struct btrfs_root_item, generation, 64);
740BTRFS_SETGET_FUNCS(disk_root_refs, struct btrfs_root_item, refs, 32);
741BTRFS_SETGET_FUNCS(disk_root_bytenr, struct btrfs_root_item, bytenr, 64);
742BTRFS_SETGET_FUNCS(disk_root_level, struct btrfs_root_item, level, 8);
743
744BTRFS_SETGET_STACK_FUNCS(root_generation, struct btrfs_root_item, generation, 64);
745BTRFS_SETGET_STACK_FUNCS(root_bytenr, struct btrfs_root_item, bytenr, 64);
746BTRFS_SETGET_STACK_FUNCS(root_drop_level, struct btrfs_root_item, drop_level, 8);
747BTRFS_SETGET_STACK_FUNCS(root_level, struct btrfs_root_item, level, 8);
748BTRFS_SETGET_STACK_FUNCS(root_dirid, struct btrfs_root_item, root_dirid, 64);
749BTRFS_SETGET_STACK_FUNCS(root_refs, struct btrfs_root_item, refs, 32);
750BTRFS_SETGET_STACK_FUNCS(root_flags, struct btrfs_root_item, flags, 64);
751BTRFS_SETGET_STACK_FUNCS(root_used, struct btrfs_root_item, bytes_used, 64);
752BTRFS_SETGET_STACK_FUNCS(root_limit, struct btrfs_root_item, byte_limit, 64);
753BTRFS_SETGET_STACK_FUNCS(root_last_snapshot, struct btrfs_root_item,
754 last_snapshot, 64);
755BTRFS_SETGET_STACK_FUNCS(root_generation_v2, struct btrfs_root_item,
756 generation_v2, 64);
757BTRFS_SETGET_STACK_FUNCS(root_ctransid, struct btrfs_root_item, ctransid, 64);
758BTRFS_SETGET_STACK_FUNCS(root_otransid, struct btrfs_root_item, otransid, 64);
759BTRFS_SETGET_STACK_FUNCS(root_stransid, struct btrfs_root_item, stransid, 64);
760BTRFS_SETGET_STACK_FUNCS(root_rtransid, struct btrfs_root_item, rtransid, 64);
761
762/* struct btrfs_root_backup */
763BTRFS_SETGET_STACK_FUNCS(backup_tree_root, struct btrfs_root_backup,
764 tree_root, 64);
765BTRFS_SETGET_STACK_FUNCS(backup_tree_root_gen, struct btrfs_root_backup,
766 tree_root_gen, 64);
767BTRFS_SETGET_STACK_FUNCS(backup_tree_root_level, struct btrfs_root_backup,
768 tree_root_level, 8);
769
770BTRFS_SETGET_STACK_FUNCS(backup_chunk_root, struct btrfs_root_backup,
771 chunk_root, 64);
772BTRFS_SETGET_STACK_FUNCS(backup_chunk_root_gen, struct btrfs_root_backup,
773 chunk_root_gen, 64);
774BTRFS_SETGET_STACK_FUNCS(backup_chunk_root_level, struct btrfs_root_backup,
775 chunk_root_level, 8);
776
777BTRFS_SETGET_STACK_FUNCS(backup_extent_root, struct btrfs_root_backup,
778 extent_root, 64);
779BTRFS_SETGET_STACK_FUNCS(backup_extent_root_gen, struct btrfs_root_backup,
780 extent_root_gen, 64);
781BTRFS_SETGET_STACK_FUNCS(backup_extent_root_level, struct btrfs_root_backup,
782 extent_root_level, 8);
783
784BTRFS_SETGET_STACK_FUNCS(backup_fs_root, struct btrfs_root_backup,
785 fs_root, 64);
786BTRFS_SETGET_STACK_FUNCS(backup_fs_root_gen, struct btrfs_root_backup,
787 fs_root_gen, 64);
788BTRFS_SETGET_STACK_FUNCS(backup_fs_root_level, struct btrfs_root_backup,
789 fs_root_level, 8);
790
791BTRFS_SETGET_STACK_FUNCS(backup_dev_root, struct btrfs_root_backup,
792 dev_root, 64);
793BTRFS_SETGET_STACK_FUNCS(backup_dev_root_gen, struct btrfs_root_backup,
794 dev_root_gen, 64);
795BTRFS_SETGET_STACK_FUNCS(backup_dev_root_level, struct btrfs_root_backup,
796 dev_root_level, 8);
797
798BTRFS_SETGET_STACK_FUNCS(backup_csum_root, struct btrfs_root_backup,
799 csum_root, 64);
800BTRFS_SETGET_STACK_FUNCS(backup_csum_root_gen, struct btrfs_root_backup,
801 csum_root_gen, 64);
802BTRFS_SETGET_STACK_FUNCS(backup_csum_root_level, struct btrfs_root_backup,
803 csum_root_level, 8);
804BTRFS_SETGET_STACK_FUNCS(backup_total_bytes, struct btrfs_root_backup,
805 total_bytes, 64);
806BTRFS_SETGET_STACK_FUNCS(backup_bytes_used, struct btrfs_root_backup,
807 bytes_used, 64);
808BTRFS_SETGET_STACK_FUNCS(backup_num_devices, struct btrfs_root_backup,
809 num_devices, 64);
810
811/* struct btrfs_balance_item */
812BTRFS_SETGET_FUNCS(balance_flags, struct btrfs_balance_item, flags, 64);
813
814static inline void btrfs_balance_data(const struct extent_buffer *eb,
815 const struct btrfs_balance_item *bi,
816 struct btrfs_disk_balance_args *ba)
817{
818 read_eb_member(eb, bi, struct btrfs_balance_item, data, ba);
819}
820
821static inline void btrfs_set_balance_data(struct extent_buffer *eb,
822 struct btrfs_balance_item *bi,
823 const struct btrfs_disk_balance_args *ba)
824{
825 write_eb_member(eb, bi, struct btrfs_balance_item, data, ba);
826}
827
828static inline void btrfs_balance_meta(const struct extent_buffer *eb,
829 const struct btrfs_balance_item *bi,
830 struct btrfs_disk_balance_args *ba)
831{
832 read_eb_member(eb, bi, struct btrfs_balance_item, meta, ba);
833}
834
835static inline void btrfs_set_balance_meta(struct extent_buffer *eb,
836 struct btrfs_balance_item *bi,
837 const struct btrfs_disk_balance_args *ba)
838{
839 write_eb_member(eb, bi, struct btrfs_balance_item, meta, ba);
840}
841
842static inline void btrfs_balance_sys(const struct extent_buffer *eb,
843 const struct btrfs_balance_item *bi,
844 struct btrfs_disk_balance_args *ba)
845{
846 read_eb_member(eb, bi, struct btrfs_balance_item, sys, ba);
847}
848
849static inline void btrfs_set_balance_sys(struct extent_buffer *eb,
850 struct btrfs_balance_item *bi,
851 const struct btrfs_disk_balance_args *ba)
852{
853 write_eb_member(eb, bi, struct btrfs_balance_item, sys, ba);
854}
855
856/* struct btrfs_super_block */
857BTRFS_SETGET_STACK_FUNCS(super_bytenr, struct btrfs_super_block, bytenr, 64);
858BTRFS_SETGET_STACK_FUNCS(super_flags, struct btrfs_super_block, flags, 64);
859BTRFS_SETGET_STACK_FUNCS(super_generation, struct btrfs_super_block,
860 generation, 64);
861BTRFS_SETGET_STACK_FUNCS(super_root, struct btrfs_super_block, root, 64);
862BTRFS_SETGET_STACK_FUNCS(super_sys_array_size,
863 struct btrfs_super_block, sys_chunk_array_size, 32);
864BTRFS_SETGET_STACK_FUNCS(super_chunk_root_generation,
865 struct btrfs_super_block, chunk_root_generation, 64);
866BTRFS_SETGET_STACK_FUNCS(super_root_level, struct btrfs_super_block,
867 root_level, 8);
868BTRFS_SETGET_STACK_FUNCS(super_chunk_root, struct btrfs_super_block,
869 chunk_root, 64);
870BTRFS_SETGET_STACK_FUNCS(super_chunk_root_level, struct btrfs_super_block,
871 chunk_root_level, 8);
872BTRFS_SETGET_STACK_FUNCS(super_log_root, struct btrfs_super_block, log_root, 64);
873BTRFS_SETGET_STACK_FUNCS(super_log_root_level, struct btrfs_super_block,
874 log_root_level, 8);
875BTRFS_SETGET_STACK_FUNCS(super_total_bytes, struct btrfs_super_block,
876 total_bytes, 64);
877BTRFS_SETGET_STACK_FUNCS(super_bytes_used, struct btrfs_super_block,
878 bytes_used, 64);
879BTRFS_SETGET_STACK_FUNCS(super_sectorsize, struct btrfs_super_block,
880 sectorsize, 32);
881BTRFS_SETGET_STACK_FUNCS(super_nodesize, struct btrfs_super_block,
882 nodesize, 32);
883BTRFS_SETGET_STACK_FUNCS(super_stripesize, struct btrfs_super_block,
884 stripesize, 32);
885BTRFS_SETGET_STACK_FUNCS(super_root_dir, struct btrfs_super_block,
886 root_dir_objectid, 64);
887BTRFS_SETGET_STACK_FUNCS(super_num_devices, struct btrfs_super_block,
888 num_devices, 64);
889BTRFS_SETGET_STACK_FUNCS(super_compat_flags, struct btrfs_super_block,
890 compat_flags, 64);
891BTRFS_SETGET_STACK_FUNCS(super_compat_ro_flags, struct btrfs_super_block,
892 compat_ro_flags, 64);
893BTRFS_SETGET_STACK_FUNCS(super_incompat_flags, struct btrfs_super_block,
894 incompat_flags, 64);
895BTRFS_SETGET_STACK_FUNCS(super_csum_type, struct btrfs_super_block,
896 csum_type, 16);
897BTRFS_SETGET_STACK_FUNCS(super_cache_generation, struct btrfs_super_block,
898 cache_generation, 64);
899BTRFS_SETGET_STACK_FUNCS(super_magic, struct btrfs_super_block, magic, 64);
900BTRFS_SETGET_STACK_FUNCS(super_uuid_tree_generation, struct btrfs_super_block,
901 uuid_tree_generation, 64);
902BTRFS_SETGET_STACK_FUNCS(super_nr_global_roots, struct btrfs_super_block,
903 nr_global_roots, 64);
904
905/* struct btrfs_file_extent_item */
906BTRFS_SETGET_STACK_FUNCS(stack_file_extent_type, struct btrfs_file_extent_item,
907 type, 8);
908BTRFS_SETGET_STACK_FUNCS(stack_file_extent_disk_bytenr,
909 struct btrfs_file_extent_item, disk_bytenr, 64);
910BTRFS_SETGET_STACK_FUNCS(stack_file_extent_offset,
911 struct btrfs_file_extent_item, offset, 64);
912BTRFS_SETGET_STACK_FUNCS(stack_file_extent_generation,
913 struct btrfs_file_extent_item, generation, 64);
914BTRFS_SETGET_STACK_FUNCS(stack_file_extent_num_bytes,
915 struct btrfs_file_extent_item, num_bytes, 64);
916BTRFS_SETGET_STACK_FUNCS(stack_file_extent_ram_bytes,
917 struct btrfs_file_extent_item, ram_bytes, 64);
918BTRFS_SETGET_STACK_FUNCS(stack_file_extent_disk_num_bytes,
919 struct btrfs_file_extent_item, disk_num_bytes, 64);
920BTRFS_SETGET_STACK_FUNCS(stack_file_extent_compression,
921 struct btrfs_file_extent_item, compression, 8);
922
923
924BTRFS_SETGET_FUNCS(file_extent_type, struct btrfs_file_extent_item, type, 8);
925BTRFS_SETGET_FUNCS(file_extent_disk_bytenr, struct btrfs_file_extent_item,
926 disk_bytenr, 64);
927BTRFS_SETGET_FUNCS(file_extent_generation, struct btrfs_file_extent_item,
928 generation, 64);
929BTRFS_SETGET_FUNCS(file_extent_disk_num_bytes, struct btrfs_file_extent_item,
930 disk_num_bytes, 64);
931BTRFS_SETGET_FUNCS(file_extent_offset, struct btrfs_file_extent_item,
932 offset, 64);
933BTRFS_SETGET_FUNCS(file_extent_num_bytes, struct btrfs_file_extent_item,
934 num_bytes, 64);
935BTRFS_SETGET_FUNCS(file_extent_ram_bytes, struct btrfs_file_extent_item,
936 ram_bytes, 64);
937BTRFS_SETGET_FUNCS(file_extent_compression, struct btrfs_file_extent_item,
938 compression, 8);
939BTRFS_SETGET_FUNCS(file_extent_encryption, struct btrfs_file_extent_item,
940 encryption, 8);
941BTRFS_SETGET_FUNCS(file_extent_other_encoding, struct btrfs_file_extent_item,
942 other_encoding, 16);
943
944/* btrfs_qgroup_status_item */
945BTRFS_SETGET_FUNCS(qgroup_status_generation, struct btrfs_qgroup_status_item,
946 generation, 64);
947BTRFS_SETGET_FUNCS(qgroup_status_version, struct btrfs_qgroup_status_item,
948 version, 64);
949BTRFS_SETGET_FUNCS(qgroup_status_flags, struct btrfs_qgroup_status_item,
950 flags, 64);
951BTRFS_SETGET_FUNCS(qgroup_status_rescan, struct btrfs_qgroup_status_item,
952 rescan, 64);
953BTRFS_SETGET_FUNCS(qgroup_status_enable_gen, struct btrfs_qgroup_status_item,
954 enable_gen, 64);
955
956/* btrfs_qgroup_info_item */
957BTRFS_SETGET_FUNCS(qgroup_info_generation, struct btrfs_qgroup_info_item,
958 generation, 64);
959BTRFS_SETGET_FUNCS(qgroup_info_rfer, struct btrfs_qgroup_info_item, rfer, 64);
960BTRFS_SETGET_FUNCS(qgroup_info_rfer_cmpr, struct btrfs_qgroup_info_item,
961 rfer_cmpr, 64);
962BTRFS_SETGET_FUNCS(qgroup_info_excl, struct btrfs_qgroup_info_item, excl, 64);
963BTRFS_SETGET_FUNCS(qgroup_info_excl_cmpr, struct btrfs_qgroup_info_item,
964 excl_cmpr, 64);
965
966BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_generation,
967 struct btrfs_qgroup_info_item, generation, 64);
968BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_rfer, struct btrfs_qgroup_info_item,
969 rfer, 64);
970BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_rfer_cmpr,
971 struct btrfs_qgroup_info_item, rfer_cmpr, 64);
972BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_excl, struct btrfs_qgroup_info_item,
973 excl, 64);
974BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_excl_cmpr,
975 struct btrfs_qgroup_info_item, excl_cmpr, 64);
976
977/* btrfs_qgroup_limit_item */
978BTRFS_SETGET_FUNCS(qgroup_limit_flags, struct btrfs_qgroup_limit_item, flags, 64);
979BTRFS_SETGET_FUNCS(qgroup_limit_max_rfer, struct btrfs_qgroup_limit_item,
980 max_rfer, 64);
981BTRFS_SETGET_FUNCS(qgroup_limit_max_excl, struct btrfs_qgroup_limit_item,
982 max_excl, 64);
983BTRFS_SETGET_FUNCS(qgroup_limit_rsv_rfer, struct btrfs_qgroup_limit_item,
984 rsv_rfer, 64);
985BTRFS_SETGET_FUNCS(qgroup_limit_rsv_excl, struct btrfs_qgroup_limit_item,
986 rsv_excl, 64);
987BTRFS_SETGET_STACK_FUNCS(stack_qgroup_limit_flags,
988 struct btrfs_qgroup_limit_item, flags, 64);
989BTRFS_SETGET_STACK_FUNCS(stack_qgroup_limit_max_rfer,
990 struct btrfs_qgroup_limit_item, max_rfer, 64);
991BTRFS_SETGET_STACK_FUNCS(stack_qgroup_limit_max_excl,
992 struct btrfs_qgroup_limit_item, max_excl, 64);
993BTRFS_SETGET_STACK_FUNCS(stack_qgroup_limit_rsv_rfer,
994 struct btrfs_qgroup_limit_item, rsv_rfer, 64);
995BTRFS_SETGET_STACK_FUNCS(stack_qgroup_limit_rsv_excl,
996 struct btrfs_qgroup_limit_item, rsv_excl, 64);
997
998/* btrfs_dev_replace_item */
999BTRFS_SETGET_FUNCS(dev_replace_src_devid,
1000 struct btrfs_dev_replace_item, src_devid, 64);
1001BTRFS_SETGET_FUNCS(dev_replace_cont_reading_from_srcdev_mode,
1002 struct btrfs_dev_replace_item, cont_reading_from_srcdev_mode,
1003 64);
1004BTRFS_SETGET_FUNCS(dev_replace_replace_state, struct btrfs_dev_replace_item,
1005 replace_state, 64);
1006BTRFS_SETGET_FUNCS(dev_replace_time_started, struct btrfs_dev_replace_item,
1007 time_started, 64);
1008BTRFS_SETGET_FUNCS(dev_replace_time_stopped, struct btrfs_dev_replace_item,
1009 time_stopped, 64);
1010BTRFS_SETGET_FUNCS(dev_replace_num_write_errors, struct btrfs_dev_replace_item,
1011 num_write_errors, 64);
1012BTRFS_SETGET_FUNCS(dev_replace_num_uncorrectable_read_errors,
1013 struct btrfs_dev_replace_item, num_uncorrectable_read_errors,
1014 64);
1015BTRFS_SETGET_FUNCS(dev_replace_cursor_left, struct btrfs_dev_replace_item,
1016 cursor_left, 64);
1017BTRFS_SETGET_FUNCS(dev_replace_cursor_right, struct btrfs_dev_replace_item,
1018 cursor_right, 64);
1019
1020BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_src_devid,
1021 struct btrfs_dev_replace_item, src_devid, 64);
1022BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_cont_reading_from_srcdev_mode,
1023 struct btrfs_dev_replace_item,
1024 cont_reading_from_srcdev_mode, 64);
1025BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_replace_state,
1026 struct btrfs_dev_replace_item, replace_state, 64);
1027BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_time_started,
1028 struct btrfs_dev_replace_item, time_started, 64);
1029BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_time_stopped,
1030 struct btrfs_dev_replace_item, time_stopped, 64);
1031BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_num_write_errors,
1032 struct btrfs_dev_replace_item, num_write_errors, 64);
1033BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_num_uncorrectable_read_errors,
1034 struct btrfs_dev_replace_item,
1035 num_uncorrectable_read_errors, 64);
1036BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_cursor_left,
1037 struct btrfs_dev_replace_item, cursor_left, 64);
1038BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_cursor_right,
1039 struct btrfs_dev_replace_item, cursor_right, 64);
1040
1041/* btrfs_verity_descriptor_item */
1042BTRFS_SETGET_FUNCS(verity_descriptor_encryption, struct btrfs_verity_descriptor_item,
1043 encryption, 8);
1044BTRFS_SETGET_FUNCS(verity_descriptor_size, struct btrfs_verity_descriptor_item,
1045 size, 64);
1046BTRFS_SETGET_STACK_FUNCS(stack_verity_descriptor_encryption,
1047 struct btrfs_verity_descriptor_item, encryption, 8);
1048BTRFS_SETGET_STACK_FUNCS(stack_verity_descriptor_size,
1049 struct btrfs_verity_descriptor_item, size, 64);
1050
1051/* Cast into the data area of the leaf. */
1052#define btrfs_item_ptr(leaf, slot, type) \
1053 ((type *)(btrfs_item_nr_offset(leaf, 0) + btrfs_item_offset(leaf, slot)))
1054
1055#define btrfs_item_ptr_offset(leaf, slot) \
1056 ((unsigned long)(btrfs_item_nr_offset(leaf, 0) + btrfs_item_offset(leaf, slot)))
1057
1058#endif