]> git.ipfire.org Git - thirdparty/linux.git/blame - fs/btrfs/ctree.h
btrfs: add definitions and documentation for encoded I/O ioctls
[thirdparty/linux.git] / fs / btrfs / ctree.h
CommitLineData
9888c340 1/* SPDX-License-Identifier: GPL-2.0 */
6cbd5570
CM
2/*
3 * Copyright (C) 2007 Oracle. All rights reserved.
6cbd5570
CM
4 */
5
9888c340
DS
6#ifndef BTRFS_CTREE_H
7#define BTRFS_CTREE_H
eb60ceac 8
810191ff 9#include <linux/mm.h>
174cd4b1 10#include <linux/sched/signal.h>
810191ff 11#include <linux/highmem.h>
e20d96d6 12#include <linux/fs.h>
a2de733c 13#include <linux/rwsem.h>
803b2f54 14#include <linux/semaphore.h>
58176a96 15#include <linux/completion.h>
04160088 16#include <linux/backing-dev.h>
e6dcd2dc 17#include <linux/wait.h>
5a0e3ad6 18#include <linux/slab.h>
1abe9b8a 19#include <trace/events/btrfs.h>
65019df8 20#include <asm/unaligned.h>
3b16a4e3 21#include <linux/pagemap.h>
55e301fd 22#include <linux/btrfs.h>
db671160 23#include <linux/btrfs_tree.h>
21c7e756 24#include <linux/workqueue.h>
f667aef6 25#include <linux/security.h>
ee22184b 26#include <linux/sizes.h>
897a41b1 27#include <linux/dynamic_debug.h>
1e4f4714 28#include <linux/refcount.h>
9678c543 29#include <linux/crc32c.h>
4e4cabec 30#include <linux/iomap.h>
9c7d3a54 31#include "extent-io-tree.h"
d1310b2e 32#include "extent_io.h"
5f39d397 33#include "extent_map.h"
8b712842 34#include "async-thread.h"
d12ffdd1 35#include "block-rsv.h"
2992df73 36#include "locking.h"
e20d96d6 37
e089f05c 38struct btrfs_trans_handle;
79154b1b 39struct btrfs_transaction;
a22285a6 40struct btrfs_pending_snapshot;
31890da0 41struct btrfs_delayed_ref_root;
8719aaae 42struct btrfs_space_info;
32da5386 43struct btrfs_block_group;
35b7e476 44extern struct kmem_cache *btrfs_trans_handle_cachep;
35b7e476 45extern struct kmem_cache *btrfs_bit_radix_cachep;
2c90e5d6 46extern struct kmem_cache *btrfs_path_cachep;
dc89e982 47extern struct kmem_cache *btrfs_free_space_cachep;
3acd4850 48extern struct kmem_cache *btrfs_free_space_bitmap_cachep;
e6dcd2dc 49struct btrfs_ordered_sum;
82fa113f 50struct btrfs_ref;
c3a3b19b 51struct btrfs_bio;
e089f05c 52
cdb4c574 53#define BTRFS_MAGIC 0x4D5F53665248425FULL /* ascii _BHRfS_M, no null */
eb60ceac 54
71a9c488
DS
55/*
56 * Maximum number of mirrors that can be available for all profiles counting
57 * the target device of dev-replace as one. During an active device replace
58 * procedure, the target device of the copy operation is a mirror for the
59 * filesystem data as well that can be used to read data in order to repair
60 * read errors on other disks.
61 *
8d6fac00 62 * Current value is derived from RAID1C4 with 4 copies.
71a9c488 63 */
8d6fac00 64#define BTRFS_MAX_MIRRORS (4 + 1)
94598ba8 65
4008c04a 66#define BTRFS_MAX_LEVEL 8
0b86a832 67
7c829b72
AJ
68#define BTRFS_OLDEST_GENERATION 0ULL
69
e20d96d6
CM
70/*
71 * we can actually store much bigger names, but lets not confuse the rest
72 * of linux
73 */
74#define BTRFS_NAME_LEN 255
75
f186373f
MF
76/*
77 * Theoretical limit is larger, but we keep this down to a sane
78 * value. That should limit greatly the possibility of collisions on
79 * inode ref items.
80 */
81#define BTRFS_LINK_MAX 65535U
82
3954401f 83#define BTRFS_EMPTY_DIR_SIZE 0
f254e52c 84
3d136a11
SB
85/* ioprio of readahead is set to idle */
86#define BTRFS_IOPRIO_READA (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0))
87
ee22184b 88#define BTRFS_DIRTY_METADATA_THRESH SZ_32M
e2d84521 89
dec59fa3
EL
90/*
91 * Use large batch size to reduce overhead of metadata updates. On the reader
92 * side, we only read it when we are close to ENOSPC and the read overhead is
93 * mostly related to the number of CPUs, so it is OK to use arbitrary large
94 * value here.
95 */
96#define BTRFS_TOTAL_BYTES_PINNED_BATCH SZ_128M
97
ee22184b 98#define BTRFS_MAX_EXTENT_SIZE SZ_128M
dcab6a3b 99
dfb79ddb
DZ
100/*
101 * Deltas are an effective way to populate global statistics. Give macro names
102 * to make it clear what we're doing. An example is discard_extents in
103 * btrfs_free_space_ctl.
104 */
105#define BTRFS_STAT_NR_ENTRIES 2
106#define BTRFS_STAT_CURR 0
107#define BTRFS_STAT_PREV 1
9678c543 108
823bb20a
DS
109/*
110 * Count how many BTRFS_MAX_EXTENT_SIZE cover the @size
111 */
112static inline u32 count_max_extents(u64 size)
113{
114 return div_u64(size + BTRFS_MAX_EXTENT_SIZE - 1, BTRFS_MAX_EXTENT_SIZE);
115}
116
0b86a832
CM
117static inline unsigned long btrfs_chunk_item_size(int num_stripes)
118{
119 BUG_ON(num_stripes == 0);
120 return sizeof(struct btrfs_chunk) +
121 sizeof(struct btrfs_stripe) * (num_stripes - 1);
122}
123
acce952b 124/*
b00146b5 125 * Runtime (in-memory) states of filesystem
acce952b 126 */
b00146b5
DS
127enum {
128 /* Global indicator of serious filesystem errors */
129 BTRFS_FS_STATE_ERROR,
130 /*
131 * Filesystem is being remounted, allow to skip some operations, like
132 * defrag
133 */
134 BTRFS_FS_STATE_REMOUNTING,
a0a1db70
FM
135 /* Filesystem in RO mode */
136 BTRFS_FS_STATE_RO,
b00146b5
DS
137 /* Track if a transaction abort has been reported on this filesystem */
138 BTRFS_FS_STATE_TRANS_ABORTED,
139 /*
140 * Bio operations should be blocked on this filesystem because a source
141 * or target device is being destroyed as part of a device replace
142 */
143 BTRFS_FS_STATE_DEV_REPLACING,
144 /* The btrfs_fs_info created for self-tests */
145 BTRFS_FS_STATE_DUMMY_FS_INFO,
056c8311
JB
146
147 BTRFS_FS_STATE_NO_CSUMS,
40cdc509
FM
148
149 /* Indicates there was an error cleaning up a log tree. */
150 BTRFS_FS_STATE_LOG_CLEANUP_ERROR,
b00146b5 151};
acce952b 152
5d4f98a2
YZ
153#define BTRFS_BACKREF_REV_MAX 256
154#define BTRFS_BACKREF_REV_SHIFT 56
155#define BTRFS_BACKREF_REV_MASK (((u64)BTRFS_BACKREF_REV_MAX - 1) << \
156 BTRFS_BACKREF_REV_SHIFT)
157
158#define BTRFS_OLD_BACKREF_REV 0
159#define BTRFS_MIXED_BACKREF_REV 1
63b10fc4 160
fec577fb
CM
161/*
162 * every tree block (leaf or node) starts with this header.
163 */
bb492bb0 164struct btrfs_header {
e17cade2 165 /* these first four must match the super block */
f254e52c 166 u8 csum[BTRFS_CSUM_SIZE];
5f39d397 167 u8 fsid[BTRFS_FSID_SIZE]; /* FS specific uuid */
db94535d 168 __le64 bytenr; /* which block this node is supposed to live in */
63b10fc4 169 __le64 flags;
e17cade2
CM
170
171 /* allowed to be different from the super from here on down */
172 u8 chunk_tree_uuid[BTRFS_UUID_SIZE];
7f5c1516 173 __le64 generation;
4d775673 174 __le64 owner;
5f39d397 175 __le32 nritems;
9a6f11ed 176 u8 level;
eb60ceac
CM
177} __attribute__ ((__packed__));
178
0b86a832
CM
179/*
180 * this is a very generous portion of the super block, giving us
181 * room to translate 14 chunks with 3 stripes each.
182 */
183#define BTRFS_SYSTEM_CHUNK_ARRAY_SIZE 2048
184
af31f5e5
CM
185/*
186 * just in case we somehow lose the roots and are not able to mount,
187 * we store an array of the roots from previous transactions
188 * in the super.
189 */
190#define BTRFS_NUM_BACKUP_ROOTS 4
191struct btrfs_root_backup {
192 __le64 tree_root;
193 __le64 tree_root_gen;
194
195 __le64 chunk_root;
196 __le64 chunk_root_gen;
197
198 __le64 extent_root;
199 __le64 extent_root_gen;
200
201 __le64 fs_root;
202 __le64 fs_root_gen;
203
204 __le64 dev_root;
205 __le64 dev_root_gen;
206
207 __le64 csum_root;
208 __le64 csum_root_gen;
209
210 __le64 total_bytes;
211 __le64 bytes_used;
212 __le64 num_devices;
213 /* future */
d1423248 214 __le64 unused_64[4];
af31f5e5
CM
215
216 u8 tree_root_level;
217 u8 chunk_root_level;
218 u8 extent_root_level;
219 u8 fs_root_level;
220 u8 dev_root_level;
221 u8 csum_root_level;
222 /* future and to align */
223 u8 unused_8[10];
224} __attribute__ ((__packed__));
225
38732474
QW
226#define BTRFS_SUPER_INFO_OFFSET SZ_64K
227#define BTRFS_SUPER_INFO_SIZE 4096
228
fec577fb
CM
229/*
230 * the super block basically lists the main trees of the FS
231 * it currently lacks any block count etc etc
232 */
234b63a0 233struct btrfs_super_block {
63b10fc4 234 /* the first 4 fields must match struct btrfs_header */
7239ff4b
NB
235 u8 csum[BTRFS_CSUM_SIZE];
236 /* FS specific UUID, visible to user */
237 u8 fsid[BTRFS_FSID_SIZE];
db94535d 238 __le64 bytenr; /* this block number */
63b10fc4 239 __le64 flags;
e17cade2
CM
240
241 /* allowed to be different from the btrfs_header from here own down */
3768f368 242 __le64 magic;
3768f368
CM
243 __le64 generation;
244 __le64 root;
0b86a832 245 __le64 chunk_root;
e02119d5 246 __le64 log_root;
c3027eb5
CM
247
248 /* this will help find the new super based on the log root */
249 __le64 log_root_transid;
db94535d
CM
250 __le64 total_bytes;
251 __le64 bytes_used;
2e635a27 252 __le64 root_dir_objectid;
8a4b83cc 253 __le64 num_devices;
5f39d397
CM
254 __le32 sectorsize;
255 __le32 nodesize;
707e8a07 256 __le32 __unused_leafsize;
87ee04eb 257 __le32 stripesize;
0b86a832 258 __le32 sys_chunk_array_size;
84234f3a 259 __le64 chunk_root_generation;
f2b636e8
JB
260 __le64 compat_flags;
261 __le64 compat_ro_flags;
262 __le64 incompat_flags;
607d432d 263 __le16 csum_type;
db94535d 264 u8 root_level;
0b86a832 265 u8 chunk_root_level;
e02119d5 266 u8 log_root_level;
0d81ba5d 267 struct btrfs_dev_item dev_item;
c3027eb5 268
7ae9c09d 269 char label[BTRFS_LABEL_SIZE];
c3027eb5 270
0af3d00b 271 __le64 cache_generation;
26432799 272 __le64 uuid_tree_generation;
0af3d00b 273
7239ff4b
NB
274 /* the UUID written into btree blocks */
275 u8 metadata_uuid[BTRFS_FSID_SIZE];
276
9c54e80d
JB
277 /* Extent tree v2 */
278 __le64 block_group_root;
279 __le64 block_group_root_generation;
280 u8 block_group_root_level;
281
c3027eb5 282 /* future expansion */
9c54e80d
JB
283 u8 reserved8[7];
284 __le64 reserved[25];
0b86a832 285 u8 sys_chunk_array[BTRFS_SYSTEM_CHUNK_ARRAY_SIZE];
af31f5e5 286 struct btrfs_root_backup super_roots[BTRFS_NUM_BACKUP_ROOTS];
38732474
QW
287
288 /* Padded to 4096 bytes */
289 u8 padding[565];
cfaa7295 290} __attribute__ ((__packed__));
38732474 291static_assert(sizeof(struct btrfs_super_block) == BTRFS_SUPER_INFO_SIZE);
cfaa7295 292
f2b636e8
JB
293/*
294 * Compat flags that we support. If any incompat flags are set other than the
295 * ones specified below then we will fail to mount
296 */
5d4f98a2 297#define BTRFS_FEATURE_COMPAT_SUPP 0ULL
2eaa055f
JM
298#define BTRFS_FEATURE_COMPAT_SAFE_SET 0ULL
299#define BTRFS_FEATURE_COMPAT_SAFE_CLEAR 0ULL
70f6d82e
OS
300
301#define BTRFS_FEATURE_COMPAT_RO_SUPP \
6675df31 302 (BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE | \
14605409
BB
303 BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE_VALID | \
304 BTRFS_FEATURE_COMPAT_RO_VERITY)
70f6d82e 305
2eaa055f
JM
306#define BTRFS_FEATURE_COMPAT_RO_SAFE_SET 0ULL
307#define BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR 0ULL
308
2c7d2a23
JB
309#ifdef CONFIG_BTRFS_DEBUG
310/*
311 * Extent tree v2 supported only with CONFIG_BTRFS_DEBUG
312 */
313#define BTRFS_FEATURE_INCOMPAT_SUPP \
314 (BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF | \
315 BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL | \
316 BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS | \
317 BTRFS_FEATURE_INCOMPAT_BIG_METADATA | \
318 BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO | \
319 BTRFS_FEATURE_INCOMPAT_COMPRESS_ZSTD | \
320 BTRFS_FEATURE_INCOMPAT_RAID56 | \
321 BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF | \
322 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA | \
323 BTRFS_FEATURE_INCOMPAT_NO_HOLES | \
324 BTRFS_FEATURE_INCOMPAT_METADATA_UUID | \
325 BTRFS_FEATURE_INCOMPAT_RAID1C34 | \
326 BTRFS_FEATURE_INCOMPAT_ZONED | \
327 BTRFS_FEATURE_INCOMPAT_EXTENT_TREE_V2)
328#else
0af3d00b
JB
329#define BTRFS_FEATURE_INCOMPAT_SUPP \
330 (BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF | \
67377734 331 BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL | \
a6fa6fae 332 BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS | \
727011e0 333 BTRFS_FEATURE_INCOMPAT_BIG_METADATA | \
f186373f 334 BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO | \
5c1aab1d 335 BTRFS_FEATURE_INCOMPAT_COMPRESS_ZSTD | \
53b381b3 336 BTRFS_FEATURE_INCOMPAT_RAID56 | \
3173a18f 337 BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF | \
16e7549f 338 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA | \
7239ff4b 339 BTRFS_FEATURE_INCOMPAT_NO_HOLES | \
cfbb825c 340 BTRFS_FEATURE_INCOMPAT_METADATA_UUID | \
9d294a68
NA
341 BTRFS_FEATURE_INCOMPAT_RAID1C34 | \
342 BTRFS_FEATURE_INCOMPAT_ZONED)
2c7d2a23 343#endif
f2b636e8 344
2eaa055f
JM
345#define BTRFS_FEATURE_INCOMPAT_SAFE_SET \
346 (BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF)
347#define BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR 0ULL
f2b636e8 348
fec577fb 349/*
62e2749e 350 * A leaf is full of items. offset and size tell us where to find
fec577fb
CM
351 * the item in the leaf (relative to the start of the data area)
352 */
0783fcfc 353struct btrfs_item {
e2fa7227 354 struct btrfs_disk_key key;
123abc88 355 __le32 offset;
5f39d397 356 __le32 size;
eb60ceac
CM
357} __attribute__ ((__packed__));
358
fec577fb
CM
359/*
360 * leaves have an item area and a data area:
361 * [item0, item1....itemN] [free space] [dataN...data1, data0]
362 *
363 * The data is separate from the items to get the keys closer together
364 * during searches.
365 */
234b63a0 366struct btrfs_leaf {
bb492bb0 367 struct btrfs_header header;
123abc88 368 struct btrfs_item items[];
eb60ceac
CM
369} __attribute__ ((__packed__));
370
fec577fb
CM
371/*
372 * all non-leaf blocks are nodes, they hold only keys and pointers to
373 * other blocks
374 */
123abc88
CM
375struct btrfs_key_ptr {
376 struct btrfs_disk_key key;
377 __le64 blockptr;
74493f7a 378 __le64 generation;
123abc88
CM
379} __attribute__ ((__packed__));
380
234b63a0 381struct btrfs_node {
bb492bb0 382 struct btrfs_header header;
123abc88 383 struct btrfs_key_ptr ptrs[];
eb60ceac
CM
384} __attribute__ ((__packed__));
385
ace75066
FM
386/* Read ahead values for struct btrfs_path.reada */
387enum {
388 READA_NONE,
389 READA_BACK,
390 READA_FORWARD,
391 /*
392 * Similar to READA_FORWARD but unlike it:
393 *
394 * 1) It will trigger readahead even for leaves that are not close to
395 * each other on disk;
396 * 2) It also triggers readahead for nodes;
397 * 3) During a search, even when a node or leaf is already in memory, it
398 * will still trigger readahead for other nodes and leaves that follow
399 * it.
400 *
401 * This is meant to be used only when we know we are iterating over the
402 * entire tree or a very large part of it.
403 */
404 READA_FORWARD_ALWAYS,
405};
406
fec577fb 407/*
234b63a0
CM
408 * btrfs_paths remember the path taken from the root down to the leaf.
409 * level 0 is always the leaf, and nodes[1...BTRFS_MAX_LEVEL] will point
fec577fb
CM
410 * to any other levels that are present.
411 *
412 * The slots array records the index of the item or block pointer
413 * used while walking the tree.
414 */
234b63a0 415struct btrfs_path {
5f39d397 416 struct extent_buffer *nodes[BTRFS_MAX_LEVEL];
234b63a0 417 int slots[BTRFS_MAX_LEVEL];
925baedd 418 /* if there is real range locking, this locks field will change */
4fb72bf2 419 u8 locks[BTRFS_MAX_LEVEL];
dccabfad 420 u8 reada;
925baedd 421 /* keep some upper locks as we walk down */
7853f15b 422 u8 lowest_level;
459931ec
CM
423
424 /*
425 * set by btrfs_split_item, tells search_slot to keep all locks
426 * and to force calls to keep space in the nodes
427 */
b9473439
CM
428 unsigned int search_for_split:1;
429 unsigned int keep_locks:1;
430 unsigned int skip_locking:1;
5d4f98a2 431 unsigned int search_commit_root:1;
3f8a18cc 432 unsigned int need_commit_sem:1;
5f5bc6b1 433 unsigned int skip_release_on_error:1;
9a664971 434 /*
435 * Indicate that new item (btrfs_search_slot) is extending already
436 * existing item and ins_len contains only the data size and not item
437 * header (ie. sizeof(struct btrfs_item) is not included).
438 */
439 unsigned int search_for_extension:1;
eb60ceac 440};
da17066c 441#define BTRFS_MAX_EXTENT_ITEM_SIZE(r) ((BTRFS_LEAF_DATA_SIZE(r->fs_info) >> 4) - \
5d4f98a2 442 sizeof(struct btrfs_item))
e922e087
SB
443struct btrfs_dev_replace {
444 u64 replace_state; /* see #define above */
a944442c
AP
445 time64_t time_started; /* seconds since 1-Jan-1970 */
446 time64_t time_stopped; /* seconds since 1-Jan-1970 */
e922e087
SB
447 atomic64_t num_write_errors;
448 atomic64_t num_uncorrectable_read_errors;
449
450 u64 cursor_left;
451 u64 committed_cursor_left;
452 u64 cursor_left_last_write_of_item;
453 u64 cursor_right;
454
455 u64 cont_reading_from_srcdev_mode; /* see #define above */
456
457 int is_valid;
458 int item_needs_writeback;
459 struct btrfs_device *srcdev;
460 struct btrfs_device *tgtdev;
461
e922e087 462 struct mutex lock_finishing_cancel_unmount;
129827e3 463 struct rw_semaphore rwsem;
e922e087
SB
464
465 struct btrfs_scrub_progress scrub_progress;
7f8d236a
DS
466
467 struct percpu_counter bio_counter;
468 wait_queue_head_t replace_wait;
e922e087
SB
469};
470
fa9c0d79
CM
471/*
472 * free clusters are used to claim free space in relatively large chunks,
583b7231
HK
473 * allowing us to do less seeky writes. They are used for all metadata
474 * allocations. In ssd_spread mode they are also used for data allocations.
fa9c0d79
CM
475 */
476struct btrfs_free_cluster {
477 spinlock_t lock;
478 spinlock_t refill_lock;
479 struct rb_root root;
480
481 /* largest extent in this cluster */
482 u64 max_size;
483
484 /* first extent starting offset */
485 u64 window_start;
486
c759c4e1
JB
487 /* We did a full search and couldn't create a cluster */
488 bool fragmented;
489
32da5386 490 struct btrfs_block_group *block_group;
fa9c0d79
CM
491 /*
492 * when a cluster is allocated from a block group, we put the
493 * cluster onto a list in the block group so that it can
494 * be freed before the block group is freed.
495 */
496 struct list_head block_group_list;
6324fbf3
CM
497};
498
817d52f8 499enum btrfs_caching_type {
bbe339cc
DS
500 BTRFS_CACHE_NO,
501 BTRFS_CACHE_STARTED,
502 BTRFS_CACHE_FAST,
503 BTRFS_CACHE_FINISHED,
504 BTRFS_CACHE_ERROR,
817d52f8
JB
505};
506
0966a7b1
QW
507/*
508 * Tree to record all locked full stripes of a RAID5/6 block group
509 */
510struct btrfs_full_stripe_locks_tree {
511 struct rb_root root;
512 struct mutex lock;
513};
514
b0643e59
DZ
515/* Discard control. */
516/*
517 * Async discard uses multiple lists to differentiate the discard filter
6e80d4f8
DZ
518 * parameters. Index 0 is for completely free block groups where we need to
519 * ensure the entire block group is trimmed without being lossy. Indices
520 * afterwards represent monotonically decreasing discard filter sizes to
521 * prioritize what should be discarded next.
b0643e59 522 */
7fe6d45e 523#define BTRFS_NR_DISCARD_LISTS 3
6e80d4f8
DZ
524#define BTRFS_DISCARD_INDEX_UNUSED 0
525#define BTRFS_DISCARD_INDEX_START 1
b0643e59
DZ
526
527struct btrfs_discard_ctl {
528 struct workqueue_struct *discard_workers;
529 struct delayed_work work;
530 spinlock_t lock;
531 struct btrfs_block_group *block_group;
532 struct list_head discard_list[BTRFS_NR_DISCARD_LISTS];
e93591bb 533 u64 prev_discard;
df903e5d 534 u64 prev_discard_time;
dfb79ddb 535 atomic_t discardable_extents;
5dc7c10b 536 atomic64_t discardable_bytes;
19b2a2c7 537 u64 max_discard_size;
6e88f116 538 u64 delay_ms;
a2309300 539 u32 iops_limit;
e93591bb 540 u32 kbps_limit;
9ddf648f
DZ
541 u64 discard_extent_bytes;
542 u64 discard_bitmap_bytes;
543 atomic64_t discard_bytes_saved;
b0643e59
DZ
544};
545
57056740 546void btrfs_init_async_reclaim_work(struct btrfs_fs_info *fs_info);
21c7e756 547
097b8a7c 548/* fs_info */
5d4f98a2 549struct reloc_control;
0b86a832 550struct btrfs_device;
8a4b83cc 551struct btrfs_fs_devices;
c9e9f97b 552struct btrfs_balance_control;
16cdcec7 553struct btrfs_delayed_root;
afcdd129 554
eede2bf3
OS
555/*
556 * Block group or device which contains an active swapfile. Used for preventing
557 * unsafe operations while a swapfile is active.
558 *
559 * These are sorted on (ptr, inode) (note that a block group or device can
560 * contain more than one swapfile). We compare the pointer values because we
561 * don't actually care what the object is, we just need a quick check whether
562 * the object exists in the rbtree.
563 */
564struct btrfs_swapfile_pin {
565 struct rb_node node;
566 void *ptr;
567 struct inode *inode;
568 /*
32da5386
DS
569 * If true, ptr points to a struct btrfs_block_group. Otherwise, ptr
570 * points to a struct btrfs_device.
eede2bf3
OS
571 */
572 bool is_block_group;
195a49ea
FM
573 /*
574 * Only used when 'is_block_group' is true and it is the number of
575 * extents used by a swapfile for this block group ('ptr' field).
576 */
577 int bg_extent_count;
eede2bf3
OS
578};
579
580bool btrfs_pinned_by_swapfile(struct btrfs_fs_info *fs_info, void *ptr);
581
eb1a524c 582enum {
eb1a524c
DS
583 BTRFS_FS_CLOSING_START,
584 BTRFS_FS_CLOSING_DONE,
585 BTRFS_FS_LOG_RECOVERING,
586 BTRFS_FS_OPEN,
587 BTRFS_FS_QUOTA_ENABLED,
588 BTRFS_FS_UPDATE_UUID_TREE_GEN,
589 BTRFS_FS_CREATING_FREE_SPACE_TREE,
590 BTRFS_FS_BTREE_ERR,
591 BTRFS_FS_LOG1_ERR,
592 BTRFS_FS_LOG2_ERR,
593 BTRFS_FS_QUOTA_OVERRIDE,
594 /* Used to record internally whether fs has been frozen */
595 BTRFS_FS_FROZEN,
eb1a524c
DS
596 /*
597 * Indicate that balance has been set up from the ioctl and is in the
598 * main phase. The fs_info::balance_ctl is initialized.
599 */
600 BTRFS_FS_BALANCE_RUNNING,
fd340d0f 601
907d2710
DS
602 /*
603 * Indicate that relocation of a chunk has started, it's set per chunk
604 * and is toggled between chunks.
605 */
606 BTRFS_FS_RELOC_RUNNING,
607
fd340d0f
JB
608 /* Indicate that the cleaner thread is awake and doing something. */
609 BTRFS_FS_CLEANER_RUNNING,
9b4e675a
DS
610
611 /*
612 * The checksumming has an optimized version and is considered fast,
613 * so we don't need to offload checksums to workqueues.
614 */
615 BTRFS_FS_CSUM_IMPL_FAST,
b0643e59
DZ
616
617 /* Indicate that the discard workqueue can service discards. */
618 BTRFS_FS_DISCARD_RUNNING,
94846229
BB
619
620 /* Indicate that we need to cleanup space cache v1 */
621 BTRFS_FS_CLEANUP_SPACE_CACHE_V1,
2f96e402
JB
622
623 /* Indicate that we can't trust the free space tree for caching yet */
624 BTRFS_FS_FREE_SPACE_TREE_UNTRUSTED,
bc03f39e
FM
625
626 /* Indicate whether there are any tree modification log users */
627 BTRFS_FS_TREE_MOD_LOG_USERS,
e9306ad4 628
fdfbf020
JB
629 /* Indicate that we want the transaction kthread to commit right now. */
630 BTRFS_FS_COMMIT_TRANS,
631
b4be6aef
JB
632 /* Indicate we have half completed snapshot deletions pending. */
633 BTRFS_FS_UNFINISHED_DROPS,
634
e9306ad4
QW
635#if BITS_PER_LONG == 32
636 /* Indicate if we have error/warn message printed on 32bit systems */
637 BTRFS_FS_32BIT_ERROR,
638 BTRFS_FS_32BIT_WARN,
639#endif
eb1a524c 640};
3009a62f 641
c3e1f96c
GR
642/*
643 * Exclusive operations (device replace, resize, device add/remove, balance)
644 */
645enum btrfs_exclusive_operation {
646 BTRFS_EXCLOP_NONE,
efc0e69c 647 BTRFS_EXCLOP_BALANCE_PAUSED,
c3e1f96c
GR
648 BTRFS_EXCLOP_BALANCE,
649 BTRFS_EXCLOP_DEV_ADD,
650 BTRFS_EXCLOP_DEV_REMOVE,
651 BTRFS_EXCLOP_DEV_REPLACE,
652 BTRFS_EXCLOP_RESIZE,
653 BTRFS_EXCLOP_SWAP_ACTIVATE,
654};
655
9f5fae2f 656struct btrfs_fs_info {
e17cade2 657 u8 chunk_tree_uuid[BTRFS_UUID_SIZE];
afcdd129 658 unsigned long flags;
62e2749e 659 struct btrfs_root *tree_root;
0b86a832
CM
660 struct btrfs_root *chunk_root;
661 struct btrfs_root *dev_root;
3de4586c 662 struct btrfs_root *fs_root;
416ac51d 663 struct btrfs_root *quota_root;
f7a81ea4 664 struct btrfs_root *uuid_root;
aeb935a4 665 struct btrfs_root *data_reloc_root;
9c54e80d 666 struct btrfs_root *block_group_root;
e02119d5
CM
667
668 /* the log root tree is a directory of all the other log roots */
669 struct btrfs_root *log_root_tree;
4df27c4d 670
abed4aaa
JB
671 /* The tree that holds the global roots (csum, extent, etc) */
672 rwlock_t global_root_lock;
673 struct rb_root global_root_tree;
674
4df27c4d 675 spinlock_t fs_roots_radix_lock;
0f7d52f4 676 struct radix_tree_root fs_roots_radix;
1a5bc167 677
0f9dd46c
JB
678 /* block group cache stuff */
679 spinlock_t block_group_cache_lock;
a1897fdd 680 u64 first_logical_byte;
0f9dd46c
JB
681 struct rb_root block_group_cache_tree;
682
2bf64758 683 /* keep track of unallocated space */
a5ed45f8 684 atomic64_t free_chunk_space;
2bf64758 685
fe119a6e
NB
686 /* Track ranges which are used by log trees blocks/logged data extents */
687 struct extent_io_tree excluded_extents;
1a5bc167 688
0b86a832 689 /* logical->physical extent mapping */
c8bf1b67 690 struct extent_map_tree mapping_tree;
0b86a832 691
16cdcec7
MX
692 /*
693 * block reservation for extent, checksum, root tree and
694 * delayed dir index item
695 */
f0486c68 696 struct btrfs_block_rsv global_block_rsv;
f0486c68
YZ
697 /* block reservation for metadata operations */
698 struct btrfs_block_rsv trans_block_rsv;
699 /* block reservation for chunk tree */
700 struct btrfs_block_rsv chunk_block_rsv;
6d668dda
JB
701 /* block reservation for delayed operations */
702 struct btrfs_block_rsv delayed_block_rsv;
ba2c4d4e
JB
703 /* block reservation for delayed refs */
704 struct btrfs_block_rsv delayed_refs_rsv;
f0486c68
YZ
705
706 struct btrfs_block_rsv empty_block_rsv;
707
293ffd5f 708 u64 generation;
15ee9bc7 709 u64 last_trans_committed;
d96b3424
FM
710 /*
711 * Generation of the last transaction used for block group relocation
712 * since the filesystem was last mounted (or 0 if none happened yet).
713 * Must be written and read while holding btrfs_fs_info::commit_root_sem.
714 */
715 u64 last_reloc_trans;
0a2b2a84 716 u64 avg_delayed_ref_runtime;
12fcfd22
CM
717
718 /*
719 * this is updated to the current trans every time a full commit
720 * is required instead of the faster short fsync log commits
721 */
722 u64 last_trans_log_full_commit;
25cd999e 723 unsigned long mount_opt;
572d9ab7
DS
724 /*
725 * Track requests for actions that need to be done during transaction
726 * commit (like for some mount options).
727 */
728 unsigned long pending_changes;
261507a0 729 unsigned long compress_type:4;
f51d2b59 730 unsigned int compress_level;
d3740608 731 u32 commit_interval;
8c6a3ee6
MX
732 /*
733 * It is a suggestive number, the read side is safe even it gets a
734 * wrong number because we will write out the data into a regular
735 * extent. The write side(mount/remount) is under ->s_umount lock,
736 * so it is also safe.
737 */
6f568d35 738 u64 max_inline;
0d0c71b3 739
79154b1b 740 struct btrfs_transaction *running_transaction;
e6dcd2dc 741 wait_queue_head_t transaction_throttle;
f9295749 742 wait_queue_head_t transaction_wait;
bb9c12c9 743 wait_queue_head_t transaction_blocked_wait;
771ed689 744 wait_queue_head_t async_submit_wait;
e02119d5 745
ceda0864
MX
746 /*
747 * Used to protect the incompat_flags, compat_flags, compat_ro_flags
748 * when they are updated.
749 *
750 * Because we do not clear the flags for ever, so we needn't use
751 * the lock on the read side.
752 *
753 * We also needn't use the lock when we mount the fs, because
754 * there is no other task which will update the flag.
755 */
756 spinlock_t super_lock;
6c41761f
DS
757 struct btrfs_super_block *super_copy;
758 struct btrfs_super_block *super_for_commit;
e20d96d6 759 struct super_block *sb;
d98237b3 760 struct inode *btree_inode;
e02119d5 761 struct mutex tree_log_mutex;
a74a4b97
CM
762 struct mutex transaction_kthread_mutex;
763 struct mutex cleaner_mutex;
925baedd 764 struct mutex chunk_mutex;
53b381b3 765
1bbc621e
CM
766 /*
767 * this is taken to make sure we don't set block groups ro after
768 * the free space cache has been allocated on them
769 */
770 struct mutex ro_block_group_mutex;
771
53b381b3
DW
772 /* this is used during read/modify/write to make sure
773 * no two ios are trying to mod the same stripe at the same
774 * time
775 */
776 struct btrfs_stripe_hash_table *stripe_hash_table;
777
5a3f23d5
CM
778 /*
779 * this protects the ordered operations list only while we are
780 * processing all of the entries on it. This way we make
781 * sure the commit code doesn't find the list temporarily empty
782 * because another function happens to be doing non-waiting preflush
783 * before jumping into the main commit.
784 */
785 struct mutex ordered_operations_mutex;
9ffba8cd 786
9e351cc8 787 struct rw_semaphore commit_root_sem;
5a3f23d5 788
c71bf099 789 struct rw_semaphore cleanup_work_sem;
76dda93c 790
c71bf099 791 struct rw_semaphore subvol_sem;
76dda93c 792
a4abeea4 793 spinlock_t trans_lock;
7585717f
CM
794 /*
795 * the reloc mutex goes with the trans lock, it is taken
796 * during commit to protect us from the relocation code
797 */
798 struct mutex reloc_mutex;
799
8fd17795 800 struct list_head trans_list;
facda1e7 801 struct list_head dead_roots;
11833d66 802 struct list_head caching_block_groups;
e02119d5 803
24bbcf04
YZ
804 spinlock_t delayed_iput_lock;
805 struct list_head delayed_iputs;
034f784d
JB
806 atomic_t nr_delayed_iputs;
807 wait_queue_head_t delayed_iputs_wait;
24bbcf04 808
fc36ed7e 809 atomic64_t tree_mod_seq;
f29021b2 810
7227ff4d 811 /* this protects tree_mod_log and tree_mod_seq_list */
f29021b2
JS
812 rwlock_t tree_mod_log_lock;
813 struct rb_root tree_mod_log;
7227ff4d 814 struct list_head tree_mod_seq_list;
f29021b2 815
771ed689 816 atomic_t async_delalloc_pages;
ce9adaa5 817
3eaa2885 818 /*
199c2a9c 819 * this is used to protect the following list -- ordered_roots.
3eaa2885 820 */
199c2a9c 821 spinlock_t ordered_root_lock;
5a3f23d5
CM
822
823 /*
199c2a9c
MX
824 * all fs/file tree roots in which there are data=ordered extents
825 * pending writeback are added into this list.
826 *
5a3f23d5
CM
827 * these can span multiple transactions and basically include
828 * every dirty data page that isn't from nodatacow
829 */
199c2a9c 830 struct list_head ordered_roots;
5a3f23d5 831
573bfb72 832 struct mutex delalloc_root_mutex;
eb73c1b7
MX
833 spinlock_t delalloc_root_lock;
834 /* all fs/file tree roots that have delalloc inodes. */
835 struct list_head delalloc_roots;
3eaa2885 836
8b712842
CM
837 /*
838 * there is a pool of worker threads for checksumming during writes
839 * and a pool for checksumming after reads. This is because readers
840 * can run with FS locks held, and the writers may be waiting for
841 * those locks. We don't want ordering in the pending list to cause
842 * deadlocks, and so the two are serviced separately.
1cc127b5
CM
843 *
844 * A third pool does submit_bio to avoid deadlocking with the other
845 * two
8b712842 846 */
d458b054
QW
847 struct btrfs_workqueue *workers;
848 struct btrfs_workqueue *delalloc_workers;
849 struct btrfs_workqueue *flush_workers;
850 struct btrfs_workqueue *endio_workers;
851 struct btrfs_workqueue *endio_meta_workers;
852 struct btrfs_workqueue *endio_raid56_workers;
853 struct btrfs_workqueue *rmw_workers;
854 struct btrfs_workqueue *endio_meta_write_workers;
855 struct btrfs_workqueue *endio_write_workers;
856 struct btrfs_workqueue *endio_freespace_worker;
d458b054 857 struct btrfs_workqueue *caching_workers;
bab39bf9 858
247e743c
CM
859 /*
860 * fixup workers take dirty pages that didn't properly go through
861 * the cow mechanism and make them safe to write. It happens
862 * for the sys_munmap function call path
863 */
d458b054
QW
864 struct btrfs_workqueue *fixup_workers;
865 struct btrfs_workqueue *delayed_workers;
a79b7d4b 866
a74a4b97
CM
867 struct task_struct *transaction_kthread;
868 struct task_struct *cleaner_kthread;
f7b885be 869 u32 thread_pool_size;
8b712842 870
6ab0a202 871 struct kobject *space_info_kobj;
49e5fb46 872 struct kobject *qgroups_kobj;
9f5fae2f 873
e2d84521
MX
874 /* used to keep from writing metadata until there is a nice batch */
875 struct percpu_counter dirty_metadata_bytes;
963d678b 876 struct percpu_counter delalloc_bytes;
5deb17e1 877 struct percpu_counter ordered_bytes;
e2d84521 878 s32 dirty_metadata_batch;
963d678b
MX
879 s32 delalloc_batch;
880
0b86a832
CM
881 struct list_head dirty_cowonly_roots;
882
8a4b83cc 883 struct btrfs_fs_devices *fs_devices;
4184ea7f
CM
884
885 /*
dc2d3005
JM
886 * The space_info list is effectively read only after initial
887 * setup. It is populated at mount time and cleaned up after
888 * all block groups are removed. RCU is used to protect it.
4184ea7f 889 */
6324fbf3 890 struct list_head space_info;
4184ea7f 891
b4d7c3c9
LZ
892 struct btrfs_space_info *data_sinfo;
893
5d4f98a2
YZ
894 struct reloc_control *reloc_ctl;
895
583b7231 896 /* data_alloc_cluster is only used in ssd_spread mode */
fa9c0d79
CM
897 struct btrfs_free_cluster data_alloc_cluster;
898
899 /* all metadata allocations go through this cluster */
900 struct btrfs_free_cluster meta_alloc_cluster;
d18a2c44 901
4cb5300b
CM
902 /* auto defrag inodes go here */
903 spinlock_t defrag_inodes_lock;
904 struct rb_root defrag_inodes;
905 atomic_t defrag_running;
906
de98ced9
MX
907 /* Used to protect avail_{data, metadata, system}_alloc_bits */
908 seqlock_t profiles_lock;
a46d11a8
ID
909 /*
910 * these three are in extended format (availability of single
911 * chunks is denoted by BTRFS_AVAIL_ALLOC_BIT_SINGLE bit, other
912 * types are denoted by corresponding BTRFS_BLOCK_GROUP_* bits)
913 */
d18a2c44
CM
914 u64 avail_data_alloc_bits;
915 u64 avail_metadata_alloc_bits;
916 u64 avail_system_alloc_bits;
788f20eb 917
c9e9f97b
ID
918 /* restriper state */
919 spinlock_t balance_lock;
920 struct mutex balance_mutex;
837d5b6e 921 atomic_t balance_pause_req;
a7e99c69 922 atomic_t balance_cancel_req;
c9e9f97b 923 struct btrfs_balance_control *balance_ctl;
837d5b6e 924 wait_queue_head_t balance_wait_q;
c9e9f97b 925
907d2710
DS
926 /* Cancellation requests for chunk relocation */
927 atomic_t reloc_cancel_req;
928
d612ac59
AJ
929 u32 data_chunk_allocations;
930 u32 metadata_ratio;
97e728d4 931
788f20eb 932 void *bdev_holder;
acce952b 933
a2de733c
AJ
934 /* private scrub information */
935 struct mutex scrub_lock;
936 atomic_t scrubs_running;
937 atomic_t scrub_pause_req;
938 atomic_t scrubs_paused;
939 atomic_t scrub_cancel_req;
940 wait_queue_head_t scrub_pause_wait;
c8352942
DS
941 /*
942 * The worker pointers are NULL iff the refcount is 0, ie. scrub is not
943 * running.
944 */
ff09c4ca 945 refcount_t scrub_workers_refcnt;
d458b054
QW
946 struct btrfs_workqueue *scrub_workers;
947 struct btrfs_workqueue *scrub_wr_completion_workers;
20b2e302 948 struct btrfs_workqueue *scrub_parity_workers;
8481dd80 949 struct btrfs_subpage_info *subpage_info;
a2de733c 950
b0643e59
DZ
951 struct btrfs_discard_ctl discard_ctl;
952
21adbd5c
SB
953#ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
954 u32 check_integrity_print_mask;
955#endif
416ac51d
AJ
956 /* is qgroup tracking in a consistent state? */
957 u64 qgroup_flags;
958
959 /* holds configuration and tracking. Protected by qgroup_lock */
960 struct rb_root qgroup_tree;
961 spinlock_t qgroup_lock;
962
1e8f9158
WS
963 /*
964 * used to avoid frequently calling ulist_alloc()/ulist_free()
965 * when doing qgroup accounting, it must be protected by qgroup_lock.
966 */
967 struct ulist *qgroup_ulist;
968
a855fbe6
FM
969 /*
970 * Protect user change for quota operations. If a transaction is needed,
971 * it must be started before locking this lock.
972 */
f2f6ed3d
WS
973 struct mutex qgroup_ioctl_lock;
974
416ac51d
AJ
975 /* list of dirty qgroups to be written at next commit */
976 struct list_head dirty_qgroups;
977
e69bcee3 978 /* used by qgroup for an efficient tree traversal */
416ac51d 979 u64 qgroup_seq;
21adbd5c 980
2f232036
JS
981 /* qgroup rescan items */
982 struct mutex qgroup_rescan_lock; /* protects the progress item */
983 struct btrfs_key qgroup_rescan_progress;
d458b054 984 struct btrfs_workqueue *qgroup_rescan_workers;
57254b6e 985 struct completion qgroup_rescan_completion;
b382a324 986 struct btrfs_work qgroup_rescan_work;
d2c609b8 987 bool qgroup_rescan_running; /* protected by qgroup_rescan_lock */
2f232036 988
acce952b 989 /* filesystem state */
87533c47 990 unsigned long fs_state;
16cdcec7
MX
991
992 struct btrfs_delayed_root *delayed_root;
af31f5e5 993
f28491e0
JB
994 /* Extent buffer radix tree */
995 spinlock_t buffer_lock;
478ef886 996 /* Entries are eb->start / sectorsize */
f28491e0
JB
997 struct radix_tree_root buffer_radix;
998
af31f5e5
CM
999 /* next backup root to be overwritten */
1000 int backup_root_index;
5af3e8cc 1001
e922e087
SB
1002 /* device replace state */
1003 struct btrfs_dev_replace dev_replace;
5ac00add 1004
803b2f54 1005 struct semaphore uuid_tree_rescan_sem;
21c7e756
MX
1006
1007 /* Used to reclaim the metadata space in the background. */
1008 struct work_struct async_reclaim_work;
57056740 1009 struct work_struct async_data_reclaim_work;
576fa348 1010 struct work_struct preempt_reclaim_work;
47ab2a6c 1011
18bb8bbf
JT
1012 /* Reclaim partially filled block groups in the background */
1013 struct work_struct reclaim_bgs_work;
1014 struct list_head reclaim_bgs;
1015 int bg_reclaim_threshold;
1016
47ab2a6c
JB
1017 spinlock_t unused_bgs_lock;
1018 struct list_head unused_bgs;
d4b450cd 1019 struct mutex unused_bg_unpin_mutex;
f3372065
JT
1020 /* Protect block groups that are going to be deleted */
1021 struct mutex reclaim_bgs_lock;
f667aef6 1022
da17066c
JM
1023 /* Cached block sizes */
1024 u32 nodesize;
1025 u32 sectorsize;
ab108d99
DS
1026 /* ilog2 of sectorsize, use to avoid 64bit division */
1027 u32 sectorsize_bits;
22b6331d 1028 u32 csum_size;
fe5ecbe8 1029 u32 csums_per_leaf;
da17066c 1030 u32 stripesize;
fd708b81 1031
eede2bf3
OS
1032 /* Block groups and devices containing active swapfiles. */
1033 spinlock_t swapfile_pins_lock;
1034 struct rb_root swapfile_pins;
1035
6d97c6e3
JT
1036 struct crypto_shash *csum_shash;
1037
0d7ed32c
DS
1038 /* Type of exclusive operation running, protected by super_lock */
1039 enum btrfs_exclusive_operation exclusive_operation;
c3e1f96c 1040
b70f5097
NA
1041 /*
1042 * Zone size > 0 when in ZONED mode, otherwise it's used for a check
1043 * if the mode is enabled
1044 */
1045 union {
1046 u64 zone_size;
1047 u64 zoned;
1048 };
1049
0bc09ca1 1050 struct mutex zoned_meta_io_lock;
40ab3be1
NA
1051 spinlock_t treelog_bg_lock;
1052 u64 treelog_bg;
862931c7 1053
c2707a25
JT
1054 /*
1055 * Start of the dedicated data relocation block group, protected by
1056 * relocation_bg_lock.
1057 */
1058 spinlock_t relocation_bg_lock;
1059 u64 data_reloc_bg;
1060
f7238e50
JB
1061 u64 nr_global_roots;
1062
afba2bc0
NA
1063 spinlock_t zone_active_bgs_lock;
1064 struct list_head zone_active_bgs;
1065
fd708b81
JB
1066#ifdef CONFIG_BTRFS_FS_REF_VERIFY
1067 spinlock_t ref_verify_lock;
1068 struct rb_root block_tree;
1069#endif
93945cb4
DZ
1070
1071#ifdef CONFIG_BTRFS_DEBUG
1072 struct kobject *debug_kobj;
e4faab84 1073 struct kobject *discard_debug_kobj;
bd647ce3 1074 struct list_head allocated_roots;
3fd63727
JB
1075
1076 spinlock_t eb_leak_lock;
1077 struct list_head allocated_ebs;
93945cb4 1078#endif
324ae4df 1079};
0b86a832 1080
da17066c
JM
1081static inline struct btrfs_fs_info *btrfs_sb(struct super_block *sb)
1082{
1083 return sb->s_fs_info;
1084}
1085
27cdeb70
MX
1086/*
1087 * The state of btrfs root
1088 */
61fa90c1
DS
1089enum {
1090 /*
1091 * btrfs_record_root_in_trans is a multi-step process, and it can race
1092 * with the balancing code. But the race is very small, and only the
1093 * first time the root is added to each transaction. So IN_TRANS_SETUP
1094 * is used to tell us when more checks are required
1095 */
1096 BTRFS_ROOT_IN_TRANS_SETUP,
92a7cc42
QW
1097
1098 /*
1099 * Set if tree blocks of this root can be shared by other roots.
1100 * Only subvolume trees and their reloc trees have this bit set.
1101 * Conflicts with TRACK_DIRTY bit.
1102 *
1103 * This affects two things:
1104 *
1105 * - How balance works
1106 * For shareable roots, we need to use reloc tree and do path
1107 * replacement for balance, and need various pre/post hooks for
1108 * snapshot creation to handle them.
1109 *
1110 * While for non-shareable trees, we just simply do a tree search
1111 * with COW.
1112 *
1113 * - How dirty roots are tracked
1114 * For shareable roots, btrfs_record_root_in_trans() is needed to
1115 * track them, while non-subvolume roots have TRACK_DIRTY bit, they
1116 * don't need to set this manually.
1117 */
1118 BTRFS_ROOT_SHAREABLE,
61fa90c1
DS
1119 BTRFS_ROOT_TRACK_DIRTY,
1120 BTRFS_ROOT_IN_RADIX,
1121 BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
1122 BTRFS_ROOT_DEFRAG_RUNNING,
1123 BTRFS_ROOT_FORCE_COW,
1124 BTRFS_ROOT_MULTI_LOG_TASKS,
1125 BTRFS_ROOT_DIRTY,
83354f07 1126 BTRFS_ROOT_DELETING,
d2311e69
QW
1127
1128 /*
1129 * Reloc tree is orphan, only kept here for qgroup delayed subtree scan
1130 *
1131 * Set for the subvolume tree owning the reloc tree.
1132 */
1133 BTRFS_ROOT_DEAD_RELOC_TREE,
78c52d9e
JB
1134 /* Mark dead root stored on device whose cleanup needs to be resumed */
1135 BTRFS_ROOT_DEAD_TREE,
47876f7c 1136 /* The root has a log tree. Used for subvolume roots and the tree root. */
e7a79811 1137 BTRFS_ROOT_HAS_LOG_TREE,
c53e9653
QW
1138 /* Qgroup flushing is in progress */
1139 BTRFS_ROOT_QGROUP_FLUSHING,
54230013
JB
1140 /* We started the orphan cleanup for this root. */
1141 BTRFS_ROOT_ORPHAN_CLEANUP,
b4be6aef
JB
1142 /* This root has a drop operation that was started previously. */
1143 BTRFS_ROOT_UNFINISHED_DROP,
61fa90c1 1144};
27cdeb70 1145
b4be6aef
JB
1146static inline void btrfs_wake_unfinished_drop(struct btrfs_fs_info *fs_info)
1147{
1148 clear_and_wake_up_bit(BTRFS_FS_UNFINISHED_DROPS, &fs_info->flags);
1149}
1150
370a11b8
QW
1151/*
1152 * Record swapped tree blocks of a subvolume tree for delayed subtree trace
1153 * code. For detail check comment in fs/btrfs/qgroup.c.
1154 */
1155struct btrfs_qgroup_swapped_blocks {
1156 spinlock_t lock;
1157 /* RM_EMPTY_ROOT() of above blocks[] */
1158 bool swapped;
1159 struct rb_root blocks[BTRFS_MAX_LEVEL];
1160};
1161
9f5fae2f
CM
1162/*
1163 * in ram representation of the tree. extent_root is used for all allocations
f2458e1d 1164 * and for the extent tree extent_root root.
9f5fae2f
CM
1165 */
1166struct btrfs_root {
abed4aaa
JB
1167 struct rb_node rb_node;
1168
5f39d397 1169 struct extent_buffer *node;
925baedd 1170
5f39d397 1171 struct extent_buffer *commit_root;
e02119d5 1172 struct btrfs_root *log_root;
1a40e23b 1173 struct btrfs_root *reloc_root;
31153d81 1174
27cdeb70 1175 unsigned long state;
62e2749e
CM
1176 struct btrfs_root_item root_item;
1177 struct btrfs_key root_key;
9f5fae2f 1178 struct btrfs_fs_info *fs_info;
d0c803c4
CM
1179 struct extent_io_tree dirty_log_pages;
1180
a2135011 1181 struct mutex objectid_mutex;
7237f183 1182
f0486c68
YZ
1183 spinlock_t accounting_lock;
1184 struct btrfs_block_rsv *block_rsv;
1185
e02119d5 1186 struct mutex log_mutex;
7237f183
YZ
1187 wait_queue_head_t log_writer_wait;
1188 wait_queue_head_t log_commit_wait[2];
8b050d35 1189 struct list_head log_ctxs[2];
a93e0168 1190 /* Used only for log trees of subvolumes, not for the log root tree */
7237f183
YZ
1191 atomic_t log_writers;
1192 atomic_t log_commit[2];
28a95795 1193 /* Used only for log trees of subvolumes, not for the log root tree */
2ecb7923 1194 atomic_t log_batch;
bb14a59b 1195 int log_transid;
d1433deb
MX
1196 /* No matter the commit succeeds or not*/
1197 int log_transid_committed;
1198 /* Just be updated when the commit succeeds. */
bb14a59b 1199 int last_log_commit;
ff782e0a 1200 pid_t log_start_pid;
ea8c2819 1201
0f7d52f4 1202 u64 last_trans;
5f39d397 1203
9f5fae2f 1204 u32 type;
13a8a7c8 1205
6b8fad57 1206 u64 free_objectid;
7585717f 1207
6702ed49 1208 struct btrfs_key defrag_progress;
0ef3e66b 1209 struct btrfs_key defrag_max;
0b86a832 1210
92a7cc42 1211 /* The dirty list is only used by non-shareable roots */
0b86a832 1212 struct list_head dirty_list;
7b128766 1213
5d4f98a2
YZ
1214 struct list_head root_list;
1215
2ab28f32
JB
1216 spinlock_t log_extents_lock[2];
1217 struct list_head logged_list[2];
1218
5d4f98a2
YZ
1219 spinlock_t inode_lock;
1220 /* red-black tree that keeps track of in-memory inodes */
1221 struct rb_root inode_tree;
1222
16cdcec7
MX
1223 /*
1224 * radix tree that keeps track of delayed nodes of every inode,
1225 * protected by inode_lock
1226 */
1227 struct radix_tree_root delayed_nodes_tree;
3394e160
CM
1228 /*
1229 * right now this just gets used so that a root has its own devid
1230 * for stat. It may be used for more later
1231 */
0ee5dc67 1232 dev_t anon_dev;
f1ebcc74 1233
5f3ab90a 1234 spinlock_t root_item_lock;
0700cea7 1235 refcount_t refs;
eb73c1b7 1236
573bfb72 1237 struct mutex delalloc_mutex;
eb73c1b7
MX
1238 spinlock_t delalloc_lock;
1239 /*
1240 * all of the inodes that have delalloc bytes. It is possible for
1241 * this list to be empty even when there is still dirty data=ordered
1242 * extents waiting to finish IO.
1243 */
1244 struct list_head delalloc_inodes;
1245 struct list_head delalloc_root;
1246 u64 nr_delalloc_inodes;
31f3d255
MX
1247
1248 struct mutex ordered_extent_mutex;
199c2a9c
MX
1249 /*
1250 * this is used by the balancing code to wait for all the pending
1251 * ordered extents
1252 */
1253 spinlock_t ordered_extent_lock;
1254
1255 /*
1256 * all of the data=ordered extents pending writeback
1257 * these can span multiple transactions and basically include
1258 * every dirty data page that isn't from nodatacow
1259 */
1260 struct list_head ordered_extents;
1261 struct list_head ordered_root;
1262 u64 nr_ordered_extents;
2c686537 1263
d2311e69
QW
1264 /*
1265 * Not empty if this subvolume root has gone through tree block swap
1266 * (relocation)
1267 *
1268 * Will be used by reloc_control::dirty_subvol_roots.
1269 */
1270 struct list_head reloc_dirty_list;
1271
2c686537
DS
1272 /*
1273 * Number of currently running SEND ioctls to prevent
1274 * manipulation with the read-only status via SUBVOL_SETFLAGS
1275 */
1276 int send_in_progress;
62d54f3a
FM
1277 /*
1278 * Number of currently running deduplication operations that have a
1279 * destination inode belonging to this root. Protected by the lock
1280 * root_item_lock.
1281 */
1282 int dedupe_in_progress;
dcc3eb96
NB
1283 /* For exclusion of snapshot creation and nocow writes */
1284 struct btrfs_drew_lock snapshot_lock;
1285
8ecebf4d 1286 atomic_t snapshot_force_cow;
8287475a
QW
1287
1288 /* For qgroup metadata reserved space */
1289 spinlock_t qgroup_meta_rsv_lock;
1290 u64 qgroup_meta_rsv_pertrans;
1291 u64 qgroup_meta_rsv_prealloc;
c53e9653 1292 wait_queue_head_t qgroup_flush_wait;
57ec5fb4 1293
eede2bf3
OS
1294 /* Number of active swapfiles */
1295 atomic_t nr_swapfiles;
1296
370a11b8
QW
1297 /* Record pairs of swapped blocks for qgroup */
1298 struct btrfs_qgroup_swapped_blocks swapped_blocks;
1299
e289f03e
FM
1300 /* Used only by log trees, when logging csum items */
1301 struct extent_io_tree log_csum_range;
1302
57ec5fb4
DS
1303#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
1304 u64 alloc_bytenr;
1305#endif
bd647ce3
JB
1306
1307#ifdef CONFIG_BTRFS_DEBUG
1308 struct list_head leak_list;
1309#endif
62e2749e 1310};
118c701e 1311
bf385648
FM
1312/*
1313 * Structure that conveys information about an extent that is going to replace
1314 * all the extents in a file range.
1315 */
1316struct btrfs_replace_extent_info {
690a5dbf
FM
1317 u64 disk_offset;
1318 u64 disk_len;
1319 u64 data_offset;
1320 u64 data_len;
1321 u64 file_offset;
fb870f6c 1322 /* Pointer to a file extent item of type regular or prealloc. */
690a5dbf 1323 char *extent_buf;
8fccebfa
FM
1324 /*
1325 * Set to true when attempting to replace a file range with a new extent
1326 * described by this structure, set to false when attempting to clone an
1327 * existing extent into a file range.
1328 */
1329 bool is_new_extent;
1330 /* Meaningful only if is_new_extent is true. */
1331 int qgroup_reserved;
1332 /*
1333 * Meaningful only if is_new_extent is true.
1334 * Used to track how many extent items we have already inserted in a
1335 * subvolume tree that refer to the extent described by this structure,
1336 * so that we know when to create a new delayed ref or update an existing
1337 * one.
1338 */
1339 int insertions;
690a5dbf
FM
1340};
1341
5893dfb9
FM
1342/* Arguments for btrfs_drop_extents() */
1343struct btrfs_drop_extents_args {
1344 /* Input parameters */
1345
1346 /*
1347 * If NULL, btrfs_drop_extents() will allocate and free its own path.
1348 * If 'replace_extent' is true, this must not be NULL. Also the path
1349 * is always released except if 'replace_extent' is true and
1350 * btrfs_drop_extents() sets 'extent_inserted' to true, in which case
1351 * the path is kept locked.
1352 */
1353 struct btrfs_path *path;
1354 /* Start offset of the range to drop extents from */
1355 u64 start;
1356 /* End (exclusive, last byte + 1) of the range to drop extents from */
1357 u64 end;
1358 /* If true drop all the extent maps in the range */
1359 bool drop_cache;
1360 /*
1361 * If true it means we want to insert a new extent after dropping all
1362 * the extents in the range. If this is true, the 'extent_item_size'
1363 * parameter must be set as well and the 'extent_inserted' field will
1364 * be set to true by btrfs_drop_extents() if it could insert the new
1365 * extent.
1366 * Note: when this is set to true the path must not be NULL.
1367 */
1368 bool replace_extent;
1369 /*
1370 * Used if 'replace_extent' is true. Size of the file extent item to
1371 * insert after dropping all existing extents in the range
1372 */
1373 u32 extent_item_size;
1374
1375 /* Output parameters */
1376
1377 /*
1378 * Set to the minimum between the input parameter 'end' and the end
1379 * (exclusive, last byte + 1) of the last dropped extent. This is always
1380 * set even if btrfs_drop_extents() returns an error.
1381 */
1382 u64 drop_end;
2766ff61
FM
1383 /*
1384 * The number of allocated bytes found in the range. This can be smaller
1385 * than the range's length when there are holes in the range.
1386 */
1387 u64 bytes_found;
5893dfb9
FM
1388 /*
1389 * Only set if 'replace_extent' is true. Set to true if we were able
1390 * to insert a replacement extent after dropping all extents in the
1391 * range, otherwise set to false by btrfs_drop_extents().
1392 * Also, if btrfs_drop_extents() has set this to true it means it
1393 * returned with the path locked, otherwise if it has set this to
1394 * false it has returned with the path released.
1395 */
1396 bool extent_inserted;
1397};
1398
23b5ec74 1399struct btrfs_file_private {
23b5ec74
JB
1400 void *filldir_buf;
1401};
1402
62e2749e 1403
da17066c 1404static inline u32 BTRFS_LEAF_DATA_SIZE(const struct btrfs_fs_info *info)
1db1ff92 1405{
118c701e
NB
1406
1407 return info->nodesize - sizeof(struct btrfs_header);
1db1ff92
JM
1408}
1409
3d9ec8c4
NB
1410#define BTRFS_LEAF_DATA_OFFSET offsetof(struct btrfs_leaf, items)
1411
da17066c 1412static inline u32 BTRFS_MAX_ITEM_SIZE(const struct btrfs_fs_info *info)
1db1ff92 1413{
da17066c 1414 return BTRFS_LEAF_DATA_SIZE(info) - sizeof(struct btrfs_item);
1db1ff92
JM
1415}
1416
da17066c 1417static inline u32 BTRFS_NODEPTRS_PER_BLOCK(const struct btrfs_fs_info *info)
1db1ff92 1418{
da17066c 1419 return BTRFS_LEAF_DATA_SIZE(info) / sizeof(struct btrfs_key_ptr);
1db1ff92
JM
1420}
1421
1422#define BTRFS_FILE_EXTENT_INLINE_DATA_START \
1423 (offsetof(struct btrfs_file_extent_item, disk_bytenr))
da17066c 1424static inline u32 BTRFS_MAX_INLINE_DATA_SIZE(const struct btrfs_fs_info *info)
1db1ff92 1425{
da17066c 1426 return BTRFS_MAX_ITEM_SIZE(info) -
1db1ff92
JM
1427 BTRFS_FILE_EXTENT_INLINE_DATA_START;
1428}
1429
da17066c 1430static inline u32 BTRFS_MAX_XATTR_SIZE(const struct btrfs_fs_info *info)
1db1ff92 1431{
da17066c 1432 return BTRFS_MAX_ITEM_SIZE(info) - sizeof(struct btrfs_dir_item);
1db1ff92
JM
1433}
1434
0942caa3
DS
1435/*
1436 * Flags for mount options.
1437 *
1438 * Note: don't forget to add new options to btrfs_show_options()
1439 */
ccd9395b
DS
1440enum {
1441 BTRFS_MOUNT_NODATASUM = (1UL << 0),
1442 BTRFS_MOUNT_NODATACOW = (1UL << 1),
1443 BTRFS_MOUNT_NOBARRIER = (1UL << 2),
1444 BTRFS_MOUNT_SSD = (1UL << 3),
1445 BTRFS_MOUNT_DEGRADED = (1UL << 4),
1446 BTRFS_MOUNT_COMPRESS = (1UL << 5),
1447 BTRFS_MOUNT_NOTREELOG = (1UL << 6),
1448 BTRFS_MOUNT_FLUSHONCOMMIT = (1UL << 7),
1449 BTRFS_MOUNT_SSD_SPREAD = (1UL << 8),
1450 BTRFS_MOUNT_NOSSD = (1UL << 9),
1451 BTRFS_MOUNT_DISCARD_SYNC = (1UL << 10),
1452 BTRFS_MOUNT_FORCE_COMPRESS = (1UL << 11),
1453 BTRFS_MOUNT_SPACE_CACHE = (1UL << 12),
1454 BTRFS_MOUNT_CLEAR_CACHE = (1UL << 13),
1455 BTRFS_MOUNT_USER_SUBVOL_RM_ALLOWED = (1UL << 14),
1456 BTRFS_MOUNT_ENOSPC_DEBUG = (1UL << 15),
1457 BTRFS_MOUNT_AUTO_DEFRAG = (1UL << 16),
1458 BTRFS_MOUNT_USEBACKUPROOT = (1UL << 17),
1459 BTRFS_MOUNT_SKIP_BALANCE = (1UL << 18),
1460 BTRFS_MOUNT_CHECK_INTEGRITY = (1UL << 19),
cbeaae4f 1461 BTRFS_MOUNT_CHECK_INTEGRITY_DATA = (1UL << 20),
ccd9395b
DS
1462 BTRFS_MOUNT_PANIC_ON_FATAL_ERROR = (1UL << 21),
1463 BTRFS_MOUNT_RESCAN_UUID_TREE = (1UL << 22),
1464 BTRFS_MOUNT_FRAGMENT_DATA = (1UL << 23),
1465 BTRFS_MOUNT_FRAGMENT_METADATA = (1UL << 24),
1466 BTRFS_MOUNT_FREE_SPACE_TREE = (1UL << 25),
1467 BTRFS_MOUNT_NOLOGREPLAY = (1UL << 26),
1468 BTRFS_MOUNT_REF_VERIFY = (1UL << 27),
1469 BTRFS_MOUNT_DISCARD_ASYNC = (1UL << 28),
1470 BTRFS_MOUNT_IGNOREBADROOTS = (1UL << 29),
1471 BTRFS_MOUNT_IGNOREDATACSUMS = (1UL << 30),
1472};
b6cda9bc 1473
8b87dc17 1474#define BTRFS_DEFAULT_COMMIT_INTERVAL (30)
f7e98a7f 1475#define BTRFS_DEFAULT_MAX_INLINE (2048)
8b87dc17 1476
b6cda9bc
CM
1477#define btrfs_clear_opt(o, opt) ((o) &= ~BTRFS_MOUNT_##opt)
1478#define btrfs_set_opt(o, opt) ((o) |= BTRFS_MOUNT_##opt)
dc81cdc5 1479#define btrfs_raw_test_opt(o, opt) ((o) & BTRFS_MOUNT_##opt)
3cdde224 1480#define btrfs_test_opt(fs_info, opt) ((fs_info)->mount_opt & \
b6cda9bc 1481 BTRFS_MOUNT_##opt)
572d9ab7 1482
3cdde224 1483#define btrfs_set_and_info(fs_info, opt, fmt, args...) \
60f8667b 1484do { \
3cdde224
JM
1485 if (!btrfs_test_opt(fs_info, opt)) \
1486 btrfs_info(fs_info, fmt, ##args); \
1487 btrfs_set_opt(fs_info->mount_opt, opt); \
60f8667b 1488} while (0)
9d89ce65 1489
3cdde224 1490#define btrfs_clear_and_info(fs_info, opt, fmt, args...) \
60f8667b 1491do { \
3cdde224
JM
1492 if (btrfs_test_opt(fs_info, opt)) \
1493 btrfs_info(fs_info, fmt, ##args); \
1494 btrfs_clear_opt(fs_info->mount_opt, opt); \
60f8667b 1495} while (0)
9d89ce65 1496
572d9ab7
DS
1497/*
1498 * Requests for changes that need to be done during transaction commit.
1499 *
1500 * Internal mount options that are used for special handling of the real
1501 * mount options (eg. cannot be set during remount and have to be set during
1502 * transaction commit)
1503 */
1504
5297199a 1505#define BTRFS_PENDING_COMMIT (0)
7e1876ac 1506
572d9ab7
DS
1507#define btrfs_test_pending(info, opt) \
1508 test_bit(BTRFS_PENDING_##opt, &(info)->pending_changes)
1509#define btrfs_set_pending(info, opt) \
1510 set_bit(BTRFS_PENDING_##opt, &(info)->pending_changes)
1511#define btrfs_clear_pending(info, opt) \
1512 clear_bit(BTRFS_PENDING_##opt, &(info)->pending_changes)
1513
1514/*
1515 * Helpers for setting pending mount option changes.
1516 *
1517 * Expects corresponding macros
1518 * BTRFS_PENDING_SET_ and CLEAR_ + short mount option name
1519 */
1520#define btrfs_set_pending_and_info(info, opt, fmt, args...) \
1521do { \
1522 if (!btrfs_raw_test_opt((info)->mount_opt, opt)) { \
1523 btrfs_info((info), fmt, ##args); \
1524 btrfs_set_pending((info), SET_##opt); \
1525 btrfs_clear_pending((info), CLEAR_##opt); \
1526 } \
1527} while(0)
1528
1529#define btrfs_clear_pending_and_info(info, opt, fmt, args...) \
1530do { \
1531 if (btrfs_raw_test_opt((info)->mount_opt, opt)) { \
1532 btrfs_info((info), fmt, ##args); \
1533 btrfs_set_pending((info), CLEAR_##opt); \
1534 btrfs_clear_pending((info), SET_##opt); \
1535 } \
1536} while(0)
1537
b98b6767
Y
1538/*
1539 * Inode flags
1540 */
77eea05e
BB
1541#define BTRFS_INODE_NODATASUM (1U << 0)
1542#define BTRFS_INODE_NODATACOW (1U << 1)
1543#define BTRFS_INODE_READONLY (1U << 2)
1544#define BTRFS_INODE_NOCOMPRESS (1U << 3)
1545#define BTRFS_INODE_PREALLOC (1U << 4)
1546#define BTRFS_INODE_SYNC (1U << 5)
1547#define BTRFS_INODE_IMMUTABLE (1U << 6)
1548#define BTRFS_INODE_APPEND (1U << 7)
1549#define BTRFS_INODE_NODUMP (1U << 8)
1550#define BTRFS_INODE_NOATIME (1U << 9)
1551#define BTRFS_INODE_DIRSYNC (1U << 10)
1552#define BTRFS_INODE_COMPRESS (1U << 11)
1553
1554#define BTRFS_INODE_ROOT_ITEM_INIT (1U << 31)
08fe4db1 1555
496245ca
QW
1556#define BTRFS_INODE_FLAG_MASK \
1557 (BTRFS_INODE_NODATASUM | \
1558 BTRFS_INODE_NODATACOW | \
1559 BTRFS_INODE_READONLY | \
1560 BTRFS_INODE_NOCOMPRESS | \
1561 BTRFS_INODE_PREALLOC | \
1562 BTRFS_INODE_SYNC | \
1563 BTRFS_INODE_IMMUTABLE | \
1564 BTRFS_INODE_APPEND | \
1565 BTRFS_INODE_NODUMP | \
1566 BTRFS_INODE_NOATIME | \
1567 BTRFS_INODE_DIRSYNC | \
1568 BTRFS_INODE_COMPRESS | \
1569 BTRFS_INODE_ROOT_ITEM_INIT)
1570
14605409
BB
1571#define BTRFS_INODE_RO_VERITY (1U << 0)
1572
1573#define BTRFS_INODE_RO_FLAG_MASK (BTRFS_INODE_RO_VERITY)
77eea05e 1574
cfed81a0 1575struct btrfs_map_token {
cc4c13d5 1576 struct extent_buffer *eb;
cfed81a0
CM
1577 char *kaddr;
1578 unsigned long offset;
1579};
1580
2e78c927 1581#define BTRFS_BYTES_TO_BLKS(fs_info, bytes) \
265fdfa6 1582 ((bytes) >> (fs_info)->sectorsize_bits)
2e78c927 1583
c82f823c
DS
1584static inline void btrfs_init_map_token(struct btrfs_map_token *token,
1585 struct extent_buffer *eb)
cfed81a0 1586{
c82f823c 1587 token->eb = eb;
870b388d
DS
1588 token->kaddr = page_address(eb->pages[0]);
1589 token->offset = 0;
cfed81a0
CM
1590}
1591
01327610 1592/* some macros to generate set/get functions for the struct fields. This
5f39d397
CM
1593 * assumes there is a lefoo_to_cpu for every type, so lets make a simple
1594 * one for u8:
1595 */
1596#define le8_to_cpu(v) (v)
1597#define cpu_to_le8(v) (v)
1598#define __le8 u8
1599
e97659ce
DS
1600static inline u8 get_unaligned_le8(const void *p)
1601{
1602 return *(u8 *)p;
1603}
1604
1605static inline void put_unaligned_le8(u8 val, void *p)
1606{
1607 *(u8 *)p = val;
1608}
1609
62e85577 1610#define read_eb_member(eb, ptr, type, member, result) (\
5f39d397
CM
1611 read_extent_buffer(eb, (char *)(result), \
1612 ((unsigned long)(ptr)) + \
1613 offsetof(type, member), \
1614 sizeof(((type *)0)->member)))
1615
62e85577 1616#define write_eb_member(eb, ptr, type, member, result) (\
5f39d397
CM
1617 write_extent_buffer(eb, (char *)(result), \
1618 ((unsigned long)(ptr)) + \
1619 offsetof(type, member), \
1620 sizeof(((type *)0)->member)))
1621
18077bb4 1622#define DECLARE_BTRFS_SETGET_BITS(bits) \
cc4c13d5
DS
1623u##bits btrfs_get_token_##bits(struct btrfs_map_token *token, \
1624 const void *ptr, unsigned long off); \
1625void btrfs_set_token_##bits(struct btrfs_map_token *token, \
1626 const void *ptr, unsigned long off, \
1627 u##bits val); \
cb495113
DS
1628u##bits btrfs_get_##bits(const struct extent_buffer *eb, \
1629 const void *ptr, unsigned long off); \
2b48966a 1630void btrfs_set_##bits(const struct extent_buffer *eb, void *ptr, \
cb495113 1631 unsigned long off, u##bits val);
18077bb4
LZ
1632
1633DECLARE_BTRFS_SETGET_BITS(8)
1634DECLARE_BTRFS_SETGET_BITS(16)
1635DECLARE_BTRFS_SETGET_BITS(32)
1636DECLARE_BTRFS_SETGET_BITS(64)
1637
5f39d397 1638#define BTRFS_SETGET_FUNCS(name, type, member, bits) \
1cbb1f45
JM
1639static inline u##bits btrfs_##name(const struct extent_buffer *eb, \
1640 const type *s) \
18077bb4 1641{ \
a55e65b8 1642 static_assert(sizeof(u##bits) == sizeof(((type *)0))->member); \
18077bb4
LZ
1643 return btrfs_get_##bits(eb, s, offsetof(type, member)); \
1644} \
2b48966a 1645static inline void btrfs_set_##name(const struct extent_buffer *eb, type *s, \
18077bb4
LZ
1646 u##bits val) \
1647{ \
a55e65b8 1648 static_assert(sizeof(u##bits) == sizeof(((type *)0))->member); \
18077bb4
LZ
1649 btrfs_set_##bits(eb, s, offsetof(type, member), val); \
1650} \
cc4c13d5
DS
1651static inline u##bits btrfs_token_##name(struct btrfs_map_token *token, \
1652 const type *s) \
18077bb4 1653{ \
a55e65b8 1654 static_assert(sizeof(u##bits) == sizeof(((type *)0))->member); \
cc4c13d5 1655 return btrfs_get_token_##bits(token, s, offsetof(type, member));\
18077bb4 1656} \
cc4c13d5
DS
1657static inline void btrfs_set_token_##name(struct btrfs_map_token *token,\
1658 type *s, u##bits val) \
18077bb4 1659{ \
a55e65b8 1660 static_assert(sizeof(u##bits) == sizeof(((type *)0))->member); \
cc4c13d5 1661 btrfs_set_token_##bits(token, s, offsetof(type, member), val); \
18077bb4 1662}
5f39d397
CM
1663
1664#define BTRFS_SETGET_HEADER_FUNCS(name, type, member, bits) \
1cbb1f45 1665static inline u##bits btrfs_##name(const struct extent_buffer *eb) \
5f39d397 1666{ \
884b07d0
QW
1667 const type *p = page_address(eb->pages[0]) + \
1668 offset_in_page(eb->start); \
e97659ce 1669 return get_unaligned_le##bits(&p->member); \
5f39d397 1670} \
2b48966a 1671static inline void btrfs_set_##name(const struct extent_buffer *eb, \
5f39d397
CM
1672 u##bits val) \
1673{ \
884b07d0 1674 type *p = page_address(eb->pages[0]) + offset_in_page(eb->start); \
e97659ce 1675 put_unaligned_le##bits(val, &p->member); \
5f39d397 1676}
9078a3e1 1677
5f39d397 1678#define BTRFS_SETGET_STACK_FUNCS(name, type, member, bits) \
1cbb1f45 1679static inline u##bits btrfs_##name(const type *s) \
5f39d397 1680{ \
e97659ce 1681 return get_unaligned_le##bits(&s->member); \
5f39d397
CM
1682} \
1683static inline void btrfs_set_##name(type *s, u##bits val) \
1684{ \
e97659ce 1685 put_unaligned_le##bits(val, &s->member); \
1e1d2701
CM
1686}
1687
2b48966a 1688static inline u64 btrfs_device_total_bytes(const struct extent_buffer *eb,
eca152ed
NB
1689 struct btrfs_dev_item *s)
1690{
a55e65b8
DS
1691 static_assert(sizeof(u64) ==
1692 sizeof(((struct btrfs_dev_item *)0))->total_bytes);
eca152ed
NB
1693 return btrfs_get_64(eb, s, offsetof(struct btrfs_dev_item,
1694 total_bytes));
1695}
2b48966a 1696static inline void btrfs_set_device_total_bytes(const struct extent_buffer *eb,
eca152ed
NB
1697 struct btrfs_dev_item *s,
1698 u64 val)
1699{
a55e65b8
DS
1700 static_assert(sizeof(u64) ==
1701 sizeof(((struct btrfs_dev_item *)0))->total_bytes);
7dfb8be1 1702 WARN_ON(!IS_ALIGNED(val, eb->fs_info->sectorsize));
eca152ed
NB
1703 btrfs_set_64(eb, s, offsetof(struct btrfs_dev_item, total_bytes), val);
1704}
1705
1706
0b86a832 1707BTRFS_SETGET_FUNCS(device_type, struct btrfs_dev_item, type, 64);
0b86a832
CM
1708BTRFS_SETGET_FUNCS(device_bytes_used, struct btrfs_dev_item, bytes_used, 64);
1709BTRFS_SETGET_FUNCS(device_io_align, struct btrfs_dev_item, io_align, 32);
1710BTRFS_SETGET_FUNCS(device_io_width, struct btrfs_dev_item, io_width, 32);
c3027eb5
CM
1711BTRFS_SETGET_FUNCS(device_start_offset, struct btrfs_dev_item,
1712 start_offset, 64);
0b86a832
CM
1713BTRFS_SETGET_FUNCS(device_sector_size, struct btrfs_dev_item, sector_size, 32);
1714BTRFS_SETGET_FUNCS(device_id, struct btrfs_dev_item, devid, 64);
e17cade2
CM
1715BTRFS_SETGET_FUNCS(device_group, struct btrfs_dev_item, dev_group, 32);
1716BTRFS_SETGET_FUNCS(device_seek_speed, struct btrfs_dev_item, seek_speed, 8);
1717BTRFS_SETGET_FUNCS(device_bandwidth, struct btrfs_dev_item, bandwidth, 8);
2b82032c 1718BTRFS_SETGET_FUNCS(device_generation, struct btrfs_dev_item, generation, 64);
0b86a832 1719
8a4b83cc
CM
1720BTRFS_SETGET_STACK_FUNCS(stack_device_type, struct btrfs_dev_item, type, 64);
1721BTRFS_SETGET_STACK_FUNCS(stack_device_total_bytes, struct btrfs_dev_item,
1722 total_bytes, 64);
1723BTRFS_SETGET_STACK_FUNCS(stack_device_bytes_used, struct btrfs_dev_item,
1724 bytes_used, 64);
1725BTRFS_SETGET_STACK_FUNCS(stack_device_io_align, struct btrfs_dev_item,
1726 io_align, 32);
1727BTRFS_SETGET_STACK_FUNCS(stack_device_io_width, struct btrfs_dev_item,
1728 io_width, 32);
1729BTRFS_SETGET_STACK_FUNCS(stack_device_sector_size, struct btrfs_dev_item,
1730 sector_size, 32);
1731BTRFS_SETGET_STACK_FUNCS(stack_device_id, struct btrfs_dev_item, devid, 64);
e17cade2
CM
1732BTRFS_SETGET_STACK_FUNCS(stack_device_group, struct btrfs_dev_item,
1733 dev_group, 32);
1734BTRFS_SETGET_STACK_FUNCS(stack_device_seek_speed, struct btrfs_dev_item,
1735 seek_speed, 8);
1736BTRFS_SETGET_STACK_FUNCS(stack_device_bandwidth, struct btrfs_dev_item,
1737 bandwidth, 8);
2b82032c
YZ
1738BTRFS_SETGET_STACK_FUNCS(stack_device_generation, struct btrfs_dev_item,
1739 generation, 64);
8a4b83cc 1740
410ba3a2 1741static inline unsigned long btrfs_device_uuid(struct btrfs_dev_item *d)
0b86a832 1742{
410ba3a2 1743 return (unsigned long)d + offsetof(struct btrfs_dev_item, uuid);
0b86a832
CM
1744}
1745
1473b24e 1746static inline unsigned long btrfs_device_fsid(struct btrfs_dev_item *d)
2b82032c 1747{
1473b24e 1748 return (unsigned long)d + offsetof(struct btrfs_dev_item, fsid);
2b82032c
YZ
1749}
1750
e17cade2 1751BTRFS_SETGET_FUNCS(chunk_length, struct btrfs_chunk, length, 64);
0b86a832
CM
1752BTRFS_SETGET_FUNCS(chunk_owner, struct btrfs_chunk, owner, 64);
1753BTRFS_SETGET_FUNCS(chunk_stripe_len, struct btrfs_chunk, stripe_len, 64);
1754BTRFS_SETGET_FUNCS(chunk_io_align, struct btrfs_chunk, io_align, 32);
1755BTRFS_SETGET_FUNCS(chunk_io_width, struct btrfs_chunk, io_width, 32);
1756BTRFS_SETGET_FUNCS(chunk_sector_size, struct btrfs_chunk, sector_size, 32);
1757BTRFS_SETGET_FUNCS(chunk_type, struct btrfs_chunk, type, 64);
1758BTRFS_SETGET_FUNCS(chunk_num_stripes, struct btrfs_chunk, num_stripes, 16);
321aecc6 1759BTRFS_SETGET_FUNCS(chunk_sub_stripes, struct btrfs_chunk, sub_stripes, 16);
0b86a832
CM
1760BTRFS_SETGET_FUNCS(stripe_devid, struct btrfs_stripe, devid, 64);
1761BTRFS_SETGET_FUNCS(stripe_offset, struct btrfs_stripe, offset, 64);
1762
e17cade2
CM
1763static inline char *btrfs_stripe_dev_uuid(struct btrfs_stripe *s)
1764{
1765 return (char *)s + offsetof(struct btrfs_stripe, dev_uuid);
1766}
1767
1768BTRFS_SETGET_STACK_FUNCS(stack_chunk_length, struct btrfs_chunk, length, 64);
0b86a832
CM
1769BTRFS_SETGET_STACK_FUNCS(stack_chunk_owner, struct btrfs_chunk, owner, 64);
1770BTRFS_SETGET_STACK_FUNCS(stack_chunk_stripe_len, struct btrfs_chunk,
1771 stripe_len, 64);
1772BTRFS_SETGET_STACK_FUNCS(stack_chunk_io_align, struct btrfs_chunk,
1773 io_align, 32);
1774BTRFS_SETGET_STACK_FUNCS(stack_chunk_io_width, struct btrfs_chunk,
1775 io_width, 32);
1776BTRFS_SETGET_STACK_FUNCS(stack_chunk_sector_size, struct btrfs_chunk,
1777 sector_size, 32);
1778BTRFS_SETGET_STACK_FUNCS(stack_chunk_type, struct btrfs_chunk, type, 64);
1779BTRFS_SETGET_STACK_FUNCS(stack_chunk_num_stripes, struct btrfs_chunk,
1780 num_stripes, 16);
321aecc6
CM
1781BTRFS_SETGET_STACK_FUNCS(stack_chunk_sub_stripes, struct btrfs_chunk,
1782 sub_stripes, 16);
0b86a832
CM
1783BTRFS_SETGET_STACK_FUNCS(stack_stripe_devid, struct btrfs_stripe, devid, 64);
1784BTRFS_SETGET_STACK_FUNCS(stack_stripe_offset, struct btrfs_stripe, offset, 64);
1785
1786static inline struct btrfs_stripe *btrfs_stripe_nr(struct btrfs_chunk *c,
1787 int nr)
1788{
1789 unsigned long offset = (unsigned long)c;
1790 offset += offsetof(struct btrfs_chunk, stripe);
1791 offset += nr * sizeof(struct btrfs_stripe);
1792 return (struct btrfs_stripe *)offset;
1793}
1794
a443755f
CM
1795static inline char *btrfs_stripe_dev_uuid_nr(struct btrfs_chunk *c, int nr)
1796{
1797 return btrfs_stripe_dev_uuid(btrfs_stripe_nr(c, nr));
1798}
1799
2b48966a 1800static inline u64 btrfs_stripe_offset_nr(const struct extent_buffer *eb,
0b86a832
CM
1801 struct btrfs_chunk *c, int nr)
1802{
1803 return btrfs_stripe_offset(eb, btrfs_stripe_nr(c, nr));
1804}
1805
2b48966a 1806static inline u64 btrfs_stripe_devid_nr(const struct extent_buffer *eb,
0b86a832
CM
1807 struct btrfs_chunk *c, int nr)
1808{
1809 return btrfs_stripe_devid(eb, btrfs_stripe_nr(c, nr));
1810}
1811
5f39d397 1812/* struct btrfs_block_group_item */
de0dc456 1813BTRFS_SETGET_STACK_FUNCS(stack_block_group_used, struct btrfs_block_group_item,
5f39d397 1814 used, 64);
0222dfdd 1815BTRFS_SETGET_FUNCS(block_group_used, struct btrfs_block_group_item,
5f39d397 1816 used, 64);
de0dc456 1817BTRFS_SETGET_STACK_FUNCS(stack_block_group_chunk_objectid,
0b86a832 1818 struct btrfs_block_group_item, chunk_objectid, 64);
e17cade2 1819
0222dfdd 1820BTRFS_SETGET_FUNCS(block_group_chunk_objectid,
0b86a832 1821 struct btrfs_block_group_item, chunk_objectid, 64);
0222dfdd 1822BTRFS_SETGET_FUNCS(block_group_flags,
0b86a832 1823 struct btrfs_block_group_item, flags, 64);
de0dc456 1824BTRFS_SETGET_STACK_FUNCS(stack_block_group_flags,
0b86a832 1825 struct btrfs_block_group_item, flags, 64);
1e1d2701 1826
208acb8c
OS
1827/* struct btrfs_free_space_info */
1828BTRFS_SETGET_FUNCS(free_space_extent_count, struct btrfs_free_space_info,
1829 extent_count, 32);
1830BTRFS_SETGET_FUNCS(free_space_flags, struct btrfs_free_space_info, flags, 32);
1831
3954401f
CM
1832/* struct btrfs_inode_ref */
1833BTRFS_SETGET_FUNCS(inode_ref_name_len, struct btrfs_inode_ref, name_len, 16);
aec7477b 1834BTRFS_SETGET_FUNCS(inode_ref_index, struct btrfs_inode_ref, index, 64);
3954401f 1835
f186373f
MF
1836/* struct btrfs_inode_extref */
1837BTRFS_SETGET_FUNCS(inode_extref_parent, struct btrfs_inode_extref,
1838 parent_objectid, 64);
1839BTRFS_SETGET_FUNCS(inode_extref_name_len, struct btrfs_inode_extref,
1840 name_len, 16);
1841BTRFS_SETGET_FUNCS(inode_extref_index, struct btrfs_inode_extref, index, 64);
1842
5f39d397
CM
1843/* struct btrfs_inode_item */
1844BTRFS_SETGET_FUNCS(inode_generation, struct btrfs_inode_item, generation, 64);
c3027eb5 1845BTRFS_SETGET_FUNCS(inode_sequence, struct btrfs_inode_item, sequence, 64);
e02119d5 1846BTRFS_SETGET_FUNCS(inode_transid, struct btrfs_inode_item, transid, 64);
5f39d397 1847BTRFS_SETGET_FUNCS(inode_size, struct btrfs_inode_item, size, 64);
a76a3cd4 1848BTRFS_SETGET_FUNCS(inode_nbytes, struct btrfs_inode_item, nbytes, 64);
5f39d397
CM
1849BTRFS_SETGET_FUNCS(inode_block_group, struct btrfs_inode_item, block_group, 64);
1850BTRFS_SETGET_FUNCS(inode_nlink, struct btrfs_inode_item, nlink, 32);
1851BTRFS_SETGET_FUNCS(inode_uid, struct btrfs_inode_item, uid, 32);
1852BTRFS_SETGET_FUNCS(inode_gid, struct btrfs_inode_item, gid, 32);
1853BTRFS_SETGET_FUNCS(inode_mode, struct btrfs_inode_item, mode, 32);
0b86a832 1854BTRFS_SETGET_FUNCS(inode_rdev, struct btrfs_inode_item, rdev, 64);
f2b636e8 1855BTRFS_SETGET_FUNCS(inode_flags, struct btrfs_inode_item, flags, 64);
3cae210f
QW
1856BTRFS_SETGET_STACK_FUNCS(stack_inode_generation, struct btrfs_inode_item,
1857 generation, 64);
1858BTRFS_SETGET_STACK_FUNCS(stack_inode_sequence, struct btrfs_inode_item,
1859 sequence, 64);
1860BTRFS_SETGET_STACK_FUNCS(stack_inode_transid, struct btrfs_inode_item,
1861 transid, 64);
1862BTRFS_SETGET_STACK_FUNCS(stack_inode_size, struct btrfs_inode_item, size, 64);
1863BTRFS_SETGET_STACK_FUNCS(stack_inode_nbytes, struct btrfs_inode_item,
1864 nbytes, 64);
1865BTRFS_SETGET_STACK_FUNCS(stack_inode_block_group, struct btrfs_inode_item,
1866 block_group, 64);
1867BTRFS_SETGET_STACK_FUNCS(stack_inode_nlink, struct btrfs_inode_item, nlink, 32);
1868BTRFS_SETGET_STACK_FUNCS(stack_inode_uid, struct btrfs_inode_item, uid, 32);
1869BTRFS_SETGET_STACK_FUNCS(stack_inode_gid, struct btrfs_inode_item, gid, 32);
1870BTRFS_SETGET_STACK_FUNCS(stack_inode_mode, struct btrfs_inode_item, mode, 32);
1871BTRFS_SETGET_STACK_FUNCS(stack_inode_rdev, struct btrfs_inode_item, rdev, 64);
1872BTRFS_SETGET_STACK_FUNCS(stack_inode_flags, struct btrfs_inode_item, flags, 64);
0b86a832
CM
1873BTRFS_SETGET_FUNCS(timespec_sec, struct btrfs_timespec, sec, 64);
1874BTRFS_SETGET_FUNCS(timespec_nsec, struct btrfs_timespec, nsec, 32);
3cae210f
QW
1875BTRFS_SETGET_STACK_FUNCS(stack_timespec_sec, struct btrfs_timespec, sec, 64);
1876BTRFS_SETGET_STACK_FUNCS(stack_timespec_nsec, struct btrfs_timespec, nsec, 32);
e20d96d6 1877
0b86a832 1878/* struct btrfs_dev_extent */
e17cade2
CM
1879BTRFS_SETGET_FUNCS(dev_extent_chunk_tree, struct btrfs_dev_extent,
1880 chunk_tree, 64);
1881BTRFS_SETGET_FUNCS(dev_extent_chunk_objectid, struct btrfs_dev_extent,
1882 chunk_objectid, 64);
1883BTRFS_SETGET_FUNCS(dev_extent_chunk_offset, struct btrfs_dev_extent,
1884 chunk_offset, 64);
0b86a832 1885BTRFS_SETGET_FUNCS(dev_extent_length, struct btrfs_dev_extent, length, 64);
5d4f98a2
YZ
1886BTRFS_SETGET_FUNCS(extent_refs, struct btrfs_extent_item, refs, 64);
1887BTRFS_SETGET_FUNCS(extent_generation, struct btrfs_extent_item,
1888 generation, 64);
1889BTRFS_SETGET_FUNCS(extent_flags, struct btrfs_extent_item, flags, 64);
74493f7a 1890
5d4f98a2
YZ
1891BTRFS_SETGET_FUNCS(tree_block_level, struct btrfs_tree_block_info, level, 8);
1892
2b48966a 1893static inline void btrfs_tree_block_key(const struct extent_buffer *eb,
5d4f98a2
YZ
1894 struct btrfs_tree_block_info *item,
1895 struct btrfs_disk_key *key)
1896{
1897 read_eb_member(eb, item, struct btrfs_tree_block_info, key, key);
1898}
1899
2b48966a 1900static inline void btrfs_set_tree_block_key(const struct extent_buffer *eb,
5d4f98a2
YZ
1901 struct btrfs_tree_block_info *item,
1902 struct btrfs_disk_key *key)
1903{
1904 write_eb_member(eb, item, struct btrfs_tree_block_info, key, key);
1905}
e20d96d6 1906
5d4f98a2
YZ
1907BTRFS_SETGET_FUNCS(extent_data_ref_root, struct btrfs_extent_data_ref,
1908 root, 64);
1909BTRFS_SETGET_FUNCS(extent_data_ref_objectid, struct btrfs_extent_data_ref,
1910 objectid, 64);
1911BTRFS_SETGET_FUNCS(extent_data_ref_offset, struct btrfs_extent_data_ref,
1912 offset, 64);
1913BTRFS_SETGET_FUNCS(extent_data_ref_count, struct btrfs_extent_data_ref,
1914 count, 32);
1915
1916BTRFS_SETGET_FUNCS(shared_data_ref_count, struct btrfs_shared_data_ref,
1917 count, 32);
1918
1919BTRFS_SETGET_FUNCS(extent_inline_ref_type, struct btrfs_extent_inline_ref,
1920 type, 8);
1921BTRFS_SETGET_FUNCS(extent_inline_ref_offset, struct btrfs_extent_inline_ref,
1922 offset, 64);
1923
1924static inline u32 btrfs_extent_inline_ref_size(int type)
1925{
1926 if (type == BTRFS_TREE_BLOCK_REF_KEY ||
1927 type == BTRFS_SHARED_BLOCK_REF_KEY)
1928 return sizeof(struct btrfs_extent_inline_ref);
1929 if (type == BTRFS_SHARED_DATA_REF_KEY)
1930 return sizeof(struct btrfs_shared_data_ref) +
1931 sizeof(struct btrfs_extent_inline_ref);
1932 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1933 return sizeof(struct btrfs_extent_data_ref) +
1934 offsetof(struct btrfs_extent_inline_ref, offset);
5d4f98a2
YZ
1935 return 0;
1936}
1937
5f39d397
CM
1938/* struct btrfs_node */
1939BTRFS_SETGET_FUNCS(key_blockptr, struct btrfs_key_ptr, blockptr, 64);
74493f7a 1940BTRFS_SETGET_FUNCS(key_generation, struct btrfs_key_ptr, generation, 64);
3cae210f
QW
1941BTRFS_SETGET_STACK_FUNCS(stack_key_blockptr, struct btrfs_key_ptr,
1942 blockptr, 64);
1943BTRFS_SETGET_STACK_FUNCS(stack_key_generation, struct btrfs_key_ptr,
1944 generation, 64);
e20d96d6 1945
2b48966a 1946static inline u64 btrfs_node_blockptr(const struct extent_buffer *eb, int nr)
cf27e1ee 1947{
5f39d397
CM
1948 unsigned long ptr;
1949 ptr = offsetof(struct btrfs_node, ptrs) +
1950 sizeof(struct btrfs_key_ptr) * nr;
1951 return btrfs_key_blockptr(eb, (struct btrfs_key_ptr *)ptr);
cf27e1ee
CM
1952}
1953
2b48966a 1954static inline void btrfs_set_node_blockptr(const struct extent_buffer *eb,
5f39d397 1955 int nr, u64 val)
cf27e1ee 1956{
5f39d397
CM
1957 unsigned long ptr;
1958 ptr = offsetof(struct btrfs_node, ptrs) +
1959 sizeof(struct btrfs_key_ptr) * nr;
1960 btrfs_set_key_blockptr(eb, (struct btrfs_key_ptr *)ptr, val);
cf27e1ee
CM
1961}
1962
2b48966a 1963static inline u64 btrfs_node_ptr_generation(const struct extent_buffer *eb, int nr)
74493f7a
CM
1964{
1965 unsigned long ptr;
1966 ptr = offsetof(struct btrfs_node, ptrs) +
1967 sizeof(struct btrfs_key_ptr) * nr;
1968 return btrfs_key_generation(eb, (struct btrfs_key_ptr *)ptr);
1969}
1970
2b48966a 1971static inline void btrfs_set_node_ptr_generation(const struct extent_buffer *eb,
74493f7a
CM
1972 int nr, u64 val)
1973{
1974 unsigned long ptr;
1975 ptr = offsetof(struct btrfs_node, ptrs) +
1976 sizeof(struct btrfs_key_ptr) * nr;
1977 btrfs_set_key_generation(eb, (struct btrfs_key_ptr *)ptr, val);
1978}
1979
810191ff 1980static inline unsigned long btrfs_node_key_ptr_offset(int nr)
4d775673 1981{
5f39d397
CM
1982 return offsetof(struct btrfs_node, ptrs) +
1983 sizeof(struct btrfs_key_ptr) * nr;
4d775673
CM
1984}
1985
1cbb1f45 1986void btrfs_node_key(const struct extent_buffer *eb,
e644d021
CM
1987 struct btrfs_disk_key *disk_key, int nr);
1988
2b48966a 1989static inline void btrfs_set_node_key(const struct extent_buffer *eb,
5f39d397 1990 struct btrfs_disk_key *disk_key, int nr)
1d4f8a0c 1991{
5f39d397
CM
1992 unsigned long ptr;
1993 ptr = btrfs_node_key_ptr_offset(nr);
1994 write_eb_member(eb, (struct btrfs_key_ptr *)ptr,
1995 struct btrfs_key_ptr, key, disk_key);
1d4f8a0c
CM
1996}
1997
5f39d397 1998/* struct btrfs_item */
3212fa14
JB
1999BTRFS_SETGET_FUNCS(raw_item_offset, struct btrfs_item, offset, 32);
2000BTRFS_SETGET_FUNCS(raw_item_size, struct btrfs_item, size, 32);
3cae210f
QW
2001BTRFS_SETGET_STACK_FUNCS(stack_item_offset, struct btrfs_item, offset, 32);
2002BTRFS_SETGET_STACK_FUNCS(stack_item_size, struct btrfs_item, size, 32);
4d775673 2003
5f39d397 2004static inline unsigned long btrfs_item_nr_offset(int nr)
1d4f8a0c 2005{
5f39d397
CM
2006 return offsetof(struct btrfs_leaf, items) +
2007 sizeof(struct btrfs_item) * nr;
1d4f8a0c
CM
2008}
2009
dd3cc16b 2010static inline struct btrfs_item *btrfs_item_nr(int nr)
0783fcfc 2011{
5f39d397 2012 return (struct btrfs_item *)btrfs_item_nr_offset(nr);
0783fcfc
CM
2013}
2014
3212fa14
JB
2015#define BTRFS_ITEM_SETGET_FUNCS(member) \
2016static inline u32 btrfs_item_##member(const struct extent_buffer *eb, \
2017 int slot) \
2018{ \
2019 return btrfs_raw_item_##member(eb, btrfs_item_nr(slot)); \
2020} \
2021static inline void btrfs_set_item_##member(const struct extent_buffer *eb, \
2022 int slot, u32 val) \
2023{ \
2024 btrfs_set_raw_item_##member(eb, btrfs_item_nr(slot), val); \
2025} \
2026static inline u32 btrfs_token_item_##member(struct btrfs_map_token *token, \
2027 int slot) \
2028{ \
2029 struct btrfs_item *item = btrfs_item_nr(slot); \
2030 return btrfs_token_raw_item_##member(token, item); \
2031} \
2032static inline void btrfs_set_token_item_##member(struct btrfs_map_token *token, \
2033 int slot, u32 val) \
2034{ \
2035 struct btrfs_item *item = btrfs_item_nr(slot); \
2036 btrfs_set_token_raw_item_##member(token, item, val); \
2037}
2038
2039BTRFS_ITEM_SETGET_FUNCS(offset)
2040BTRFS_ITEM_SETGET_FUNCS(size);
74794207 2041
dc2e724e 2042static inline u32 btrfs_item_data_end(const struct extent_buffer *eb, int nr)
5a08663d
JB
2043{
2044 return btrfs_item_offset(eb, nr) + btrfs_item_size(eb, nr);
2045}
2046
1cbb1f45 2047static inline void btrfs_item_key(const struct extent_buffer *eb,
5f39d397 2048 struct btrfs_disk_key *disk_key, int nr)
1d4f6404 2049{
dd3cc16b 2050 struct btrfs_item *item = btrfs_item_nr(nr);
5f39d397 2051 read_eb_member(eb, item, struct btrfs_item, key, disk_key);
1d4f6404
CM
2052}
2053
5f39d397
CM
2054static inline void btrfs_set_item_key(struct extent_buffer *eb,
2055 struct btrfs_disk_key *disk_key, int nr)
1d4f6404 2056{
dd3cc16b 2057 struct btrfs_item *item = btrfs_item_nr(nr);
5f39d397 2058 write_eb_member(eb, item, struct btrfs_item, key, disk_key);
1d4f6404
CM
2059}
2060
e02119d5
CM
2061BTRFS_SETGET_FUNCS(dir_log_end, struct btrfs_dir_log_item, end, 64);
2062
0660b5af
CM
2063/*
2064 * struct btrfs_root_ref
2065 */
2066BTRFS_SETGET_FUNCS(root_ref_dirid, struct btrfs_root_ref, dirid, 64);
2067BTRFS_SETGET_FUNCS(root_ref_sequence, struct btrfs_root_ref, sequence, 64);
2068BTRFS_SETGET_FUNCS(root_ref_name_len, struct btrfs_root_ref, name_len, 16);
2069
5f39d397 2070/* struct btrfs_dir_item */
5103e947 2071BTRFS_SETGET_FUNCS(dir_data_len, struct btrfs_dir_item, data_len, 16);
5f39d397
CM
2072BTRFS_SETGET_FUNCS(dir_type, struct btrfs_dir_item, type, 8);
2073BTRFS_SETGET_FUNCS(dir_name_len, struct btrfs_dir_item, name_len, 16);
e02119d5 2074BTRFS_SETGET_FUNCS(dir_transid, struct btrfs_dir_item, transid, 64);
3cae210f
QW
2075BTRFS_SETGET_STACK_FUNCS(stack_dir_type, struct btrfs_dir_item, type, 8);
2076BTRFS_SETGET_STACK_FUNCS(stack_dir_data_len, struct btrfs_dir_item,
2077 data_len, 16);
2078BTRFS_SETGET_STACK_FUNCS(stack_dir_name_len, struct btrfs_dir_item,
2079 name_len, 16);
2080BTRFS_SETGET_STACK_FUNCS(stack_dir_transid, struct btrfs_dir_item,
2081 transid, 64);
1d4f6404 2082
1cbb1f45
JM
2083static inline void btrfs_dir_item_key(const struct extent_buffer *eb,
2084 const struct btrfs_dir_item *item,
5f39d397 2085 struct btrfs_disk_key *key)
1d4f6404 2086{
5f39d397 2087 read_eb_member(eb, item, struct btrfs_dir_item, location, key);
1d4f6404
CM
2088}
2089
5f39d397
CM
2090static inline void btrfs_set_dir_item_key(struct extent_buffer *eb,
2091 struct btrfs_dir_item *item,
1cbb1f45 2092 const struct btrfs_disk_key *key)
a8a2ee0c 2093{
5f39d397 2094 write_eb_member(eb, item, struct btrfs_dir_item, location, key);
a8a2ee0c
CM
2095}
2096
0af3d00b
JB
2097BTRFS_SETGET_FUNCS(free_space_entries, struct btrfs_free_space_header,
2098 num_entries, 64);
2099BTRFS_SETGET_FUNCS(free_space_bitmaps, struct btrfs_free_space_header,
2100 num_bitmaps, 64);
2101BTRFS_SETGET_FUNCS(free_space_generation, struct btrfs_free_space_header,
2102 generation, 64);
2103
1cbb1f45
JM
2104static inline void btrfs_free_space_key(const struct extent_buffer *eb,
2105 const struct btrfs_free_space_header *h,
0af3d00b
JB
2106 struct btrfs_disk_key *key)
2107{
2108 read_eb_member(eb, h, struct btrfs_free_space_header, location, key);
2109}
2110
2111static inline void btrfs_set_free_space_key(struct extent_buffer *eb,
2112 struct btrfs_free_space_header *h,
1cbb1f45 2113 const struct btrfs_disk_key *key)
0af3d00b
JB
2114{
2115 write_eb_member(eb, h, struct btrfs_free_space_header, location, key);
2116}
2117
5f39d397
CM
2118/* struct btrfs_disk_key */
2119BTRFS_SETGET_STACK_FUNCS(disk_key_objectid, struct btrfs_disk_key,
2120 objectid, 64);
2121BTRFS_SETGET_STACK_FUNCS(disk_key_offset, struct btrfs_disk_key, offset, 64);
2122BTRFS_SETGET_STACK_FUNCS(disk_key_type, struct btrfs_disk_key, type, 8);
1d4f6404 2123
ce6ef5ab
DS
2124#ifdef __LITTLE_ENDIAN
2125
2126/*
2127 * Optimized helpers for little-endian architectures where CPU and on-disk
2128 * structures have the same endianness and we can skip conversions.
2129 */
2130
2131static inline void btrfs_disk_key_to_cpu(struct btrfs_key *cpu_key,
2132 const struct btrfs_disk_key *disk_key)
2133{
2134 memcpy(cpu_key, disk_key, sizeof(struct btrfs_key));
2135}
2136
2137static inline void btrfs_cpu_key_to_disk(struct btrfs_disk_key *disk_key,
2138 const struct btrfs_key *cpu_key)
2139{
2140 memcpy(disk_key, cpu_key, sizeof(struct btrfs_key));
2141}
2142
2143static inline void btrfs_node_key_to_cpu(const struct extent_buffer *eb,
2144 struct btrfs_key *cpu_key, int nr)
2145{
2146 struct btrfs_disk_key *disk_key = (struct btrfs_disk_key *)cpu_key;
2147
2148 btrfs_node_key(eb, disk_key, nr);
2149}
2150
2151static inline void btrfs_item_key_to_cpu(const struct extent_buffer *eb,
2152 struct btrfs_key *cpu_key, int nr)
2153{
2154 struct btrfs_disk_key *disk_key = (struct btrfs_disk_key *)cpu_key;
2155
2156 btrfs_item_key(eb, disk_key, nr);
2157}
2158
2159static inline void btrfs_dir_item_key_to_cpu(const struct extent_buffer *eb,
2160 const struct btrfs_dir_item *item,
2161 struct btrfs_key *cpu_key)
2162{
2163 struct btrfs_disk_key *disk_key = (struct btrfs_disk_key *)cpu_key;
2164
2165 btrfs_dir_item_key(eb, item, disk_key);
2166}
2167
2168#else
2169
e2fa7227 2170static inline void btrfs_disk_key_to_cpu(struct btrfs_key *cpu,
310712b2 2171 const struct btrfs_disk_key *disk)
e2fa7227
CM
2172{
2173 cpu->offset = le64_to_cpu(disk->offset);
5f39d397 2174 cpu->type = disk->type;
e2fa7227
CM
2175 cpu->objectid = le64_to_cpu(disk->objectid);
2176}
2177
2178static inline void btrfs_cpu_key_to_disk(struct btrfs_disk_key *disk,
310712b2 2179 const struct btrfs_key *cpu)
e2fa7227
CM
2180{
2181 disk->offset = cpu_to_le64(cpu->offset);
5f39d397 2182 disk->type = cpu->type;
e2fa7227
CM
2183 disk->objectid = cpu_to_le64(cpu->objectid);
2184}
2185
1cbb1f45
JM
2186static inline void btrfs_node_key_to_cpu(const struct extent_buffer *eb,
2187 struct btrfs_key *key, int nr)
7f5c1516 2188{
5f39d397
CM
2189 struct btrfs_disk_key disk_key;
2190 btrfs_node_key(eb, &disk_key, nr);
2191 btrfs_disk_key_to_cpu(key, &disk_key);
7f5c1516
CM
2192}
2193
1cbb1f45
JM
2194static inline void btrfs_item_key_to_cpu(const struct extent_buffer *eb,
2195 struct btrfs_key *key, int nr)
7f5c1516 2196{
5f39d397
CM
2197 struct btrfs_disk_key disk_key;
2198 btrfs_item_key(eb, &disk_key, nr);
2199 btrfs_disk_key_to_cpu(key, &disk_key);
7f5c1516
CM
2200}
2201
1cbb1f45
JM
2202static inline void btrfs_dir_item_key_to_cpu(const struct extent_buffer *eb,
2203 const struct btrfs_dir_item *item,
2204 struct btrfs_key *key)
4d775673 2205{
5f39d397
CM
2206 struct btrfs_disk_key disk_key;
2207 btrfs_dir_item_key(eb, item, &disk_key);
2208 btrfs_disk_key_to_cpu(key, &disk_key);
4d775673
CM
2209}
2210
ce6ef5ab
DS
2211#endif
2212
5f39d397 2213/* struct btrfs_header */
db94535d 2214BTRFS_SETGET_HEADER_FUNCS(header_bytenr, struct btrfs_header, bytenr, 64);
5f39d397
CM
2215BTRFS_SETGET_HEADER_FUNCS(header_generation, struct btrfs_header,
2216 generation, 64);
2217BTRFS_SETGET_HEADER_FUNCS(header_owner, struct btrfs_header, owner, 64);
2218BTRFS_SETGET_HEADER_FUNCS(header_nritems, struct btrfs_header, nritems, 32);
63b10fc4 2219BTRFS_SETGET_HEADER_FUNCS(header_flags, struct btrfs_header, flags, 64);
5f39d397 2220BTRFS_SETGET_HEADER_FUNCS(header_level, struct btrfs_header, level, 8);
3cae210f
QW
2221BTRFS_SETGET_STACK_FUNCS(stack_header_generation, struct btrfs_header,
2222 generation, 64);
2223BTRFS_SETGET_STACK_FUNCS(stack_header_owner, struct btrfs_header, owner, 64);
2224BTRFS_SETGET_STACK_FUNCS(stack_header_nritems, struct btrfs_header,
2225 nritems, 32);
2226BTRFS_SETGET_STACK_FUNCS(stack_header_bytenr, struct btrfs_header, bytenr, 64);
0f7d52f4 2227
1cbb1f45 2228static inline int btrfs_header_flag(const struct extent_buffer *eb, u64 flag)
63b10fc4
CM
2229{
2230 return (btrfs_header_flags(eb) & flag) == flag;
2231}
2232
80fbc341 2233static inline void btrfs_set_header_flag(struct extent_buffer *eb, u64 flag)
63b10fc4
CM
2234{
2235 u64 flags = btrfs_header_flags(eb);
2236 btrfs_set_header_flags(eb, flags | flag);
63b10fc4
CM
2237}
2238
80fbc341 2239static inline void btrfs_clear_header_flag(struct extent_buffer *eb, u64 flag)
63b10fc4
CM
2240{
2241 u64 flags = btrfs_header_flags(eb);
2242 btrfs_set_header_flags(eb, flags & ~flag);
63b10fc4
CM
2243}
2244
1cbb1f45 2245static inline int btrfs_header_backref_rev(const struct extent_buffer *eb)
5d4f98a2
YZ
2246{
2247 u64 flags = btrfs_header_flags(eb);
2248 return flags >> BTRFS_BACKREF_REV_SHIFT;
2249}
2250
2251static inline void btrfs_set_header_backref_rev(struct extent_buffer *eb,
2252 int rev)
2253{
2254 u64 flags = btrfs_header_flags(eb);
2255 flags &= ~BTRFS_BACKREF_REV_MASK;
2256 flags |= (u64)rev << BTRFS_BACKREF_REV_SHIFT;
2257 btrfs_set_header_flags(eb, flags);
2258}
2259
1cbb1f45 2260static inline int btrfs_is_leaf(const struct extent_buffer *eb)
3768f368 2261{
d397712b 2262 return btrfs_header_level(eb) == 0;
3768f368
CM
2263}
2264
5f39d397 2265/* struct btrfs_root_item */
84234f3a
YZ
2266BTRFS_SETGET_FUNCS(disk_root_generation, struct btrfs_root_item,
2267 generation, 64);
5f39d397 2268BTRFS_SETGET_FUNCS(disk_root_refs, struct btrfs_root_item, refs, 32);
db94535d
CM
2269BTRFS_SETGET_FUNCS(disk_root_bytenr, struct btrfs_root_item, bytenr, 64);
2270BTRFS_SETGET_FUNCS(disk_root_level, struct btrfs_root_item, level, 8);
3768f368 2271
84234f3a
YZ
2272BTRFS_SETGET_STACK_FUNCS(root_generation, struct btrfs_root_item,
2273 generation, 64);
db94535d 2274BTRFS_SETGET_STACK_FUNCS(root_bytenr, struct btrfs_root_item, bytenr, 64);
c8422684 2275BTRFS_SETGET_STACK_FUNCS(root_drop_level, struct btrfs_root_item, drop_level, 8);
db94535d 2276BTRFS_SETGET_STACK_FUNCS(root_level, struct btrfs_root_item, level, 8);
5f39d397
CM
2277BTRFS_SETGET_STACK_FUNCS(root_dirid, struct btrfs_root_item, root_dirid, 64);
2278BTRFS_SETGET_STACK_FUNCS(root_refs, struct btrfs_root_item, refs, 32);
f2b636e8 2279BTRFS_SETGET_STACK_FUNCS(root_flags, struct btrfs_root_item, flags, 64);
db94535d
CM
2280BTRFS_SETGET_STACK_FUNCS(root_used, struct btrfs_root_item, bytes_used, 64);
2281BTRFS_SETGET_STACK_FUNCS(root_limit, struct btrfs_root_item, byte_limit, 64);
80ff3856
YZ
2282BTRFS_SETGET_STACK_FUNCS(root_last_snapshot, struct btrfs_root_item,
2283 last_snapshot, 64);
8ea05e3a
AB
2284BTRFS_SETGET_STACK_FUNCS(root_generation_v2, struct btrfs_root_item,
2285 generation_v2, 64);
2286BTRFS_SETGET_STACK_FUNCS(root_ctransid, struct btrfs_root_item,
2287 ctransid, 64);
2288BTRFS_SETGET_STACK_FUNCS(root_otransid, struct btrfs_root_item,
2289 otransid, 64);
2290BTRFS_SETGET_STACK_FUNCS(root_stransid, struct btrfs_root_item,
2291 stransid, 64);
2292BTRFS_SETGET_STACK_FUNCS(root_rtransid, struct btrfs_root_item,
2293 rtransid, 64);
123abc88 2294
1cbb1f45 2295static inline bool btrfs_root_readonly(const struct btrfs_root *root)
b83cc969 2296{
49547068 2297 /* Byte-swap the constant at compile time, root_item::flags is LE */
6ed3cf2c 2298 return (root->root_item.flags & cpu_to_le64(BTRFS_ROOT_SUBVOL_RDONLY)) != 0;
b83cc969
LZ
2299}
2300
1cbb1f45 2301static inline bool btrfs_root_dead(const struct btrfs_root *root)
521e0546 2302{
49547068 2303 /* Byte-swap the constant at compile time, root_item::flags is LE */
521e0546
DS
2304 return (root->root_item.flags & cpu_to_le64(BTRFS_ROOT_SUBVOL_DEAD)) != 0;
2305}
2306
7a163608
FM
2307static inline u64 btrfs_root_id(const struct btrfs_root *root)
2308{
2309 return root->root_key.objectid;
2310}
2311
af31f5e5
CM
2312/* struct btrfs_root_backup */
2313BTRFS_SETGET_STACK_FUNCS(backup_tree_root, struct btrfs_root_backup,
2314 tree_root, 64);
2315BTRFS_SETGET_STACK_FUNCS(backup_tree_root_gen, struct btrfs_root_backup,
2316 tree_root_gen, 64);
2317BTRFS_SETGET_STACK_FUNCS(backup_tree_root_level, struct btrfs_root_backup,
2318 tree_root_level, 8);
2319
2320BTRFS_SETGET_STACK_FUNCS(backup_chunk_root, struct btrfs_root_backup,
2321 chunk_root, 64);
2322BTRFS_SETGET_STACK_FUNCS(backup_chunk_root_gen, struct btrfs_root_backup,
2323 chunk_root_gen, 64);
2324BTRFS_SETGET_STACK_FUNCS(backup_chunk_root_level, struct btrfs_root_backup,
2325 chunk_root_level, 8);
2326
2327BTRFS_SETGET_STACK_FUNCS(backup_extent_root, struct btrfs_root_backup,
2328 extent_root, 64);
2329BTRFS_SETGET_STACK_FUNCS(backup_extent_root_gen, struct btrfs_root_backup,
2330 extent_root_gen, 64);
2331BTRFS_SETGET_STACK_FUNCS(backup_extent_root_level, struct btrfs_root_backup,
2332 extent_root_level, 8);
2333
2334BTRFS_SETGET_STACK_FUNCS(backup_fs_root, struct btrfs_root_backup,
2335 fs_root, 64);
2336BTRFS_SETGET_STACK_FUNCS(backup_fs_root_gen, struct btrfs_root_backup,
2337 fs_root_gen, 64);
2338BTRFS_SETGET_STACK_FUNCS(backup_fs_root_level, struct btrfs_root_backup,
2339 fs_root_level, 8);
2340
2341BTRFS_SETGET_STACK_FUNCS(backup_dev_root, struct btrfs_root_backup,
2342 dev_root, 64);
2343BTRFS_SETGET_STACK_FUNCS(backup_dev_root_gen, struct btrfs_root_backup,
2344 dev_root_gen, 64);
2345BTRFS_SETGET_STACK_FUNCS(backup_dev_root_level, struct btrfs_root_backup,
2346 dev_root_level, 8);
2347
2348BTRFS_SETGET_STACK_FUNCS(backup_csum_root, struct btrfs_root_backup,
2349 csum_root, 64);
2350BTRFS_SETGET_STACK_FUNCS(backup_csum_root_gen, struct btrfs_root_backup,
2351 csum_root_gen, 64);
2352BTRFS_SETGET_STACK_FUNCS(backup_csum_root_level, struct btrfs_root_backup,
2353 csum_root_level, 8);
2354BTRFS_SETGET_STACK_FUNCS(backup_total_bytes, struct btrfs_root_backup,
2355 total_bytes, 64);
2356BTRFS_SETGET_STACK_FUNCS(backup_bytes_used, struct btrfs_root_backup,
2357 bytes_used, 64);
2358BTRFS_SETGET_STACK_FUNCS(backup_num_devices, struct btrfs_root_backup,
2359 num_devices, 64);
2360
9c54e80d
JB
2361/*
2362 * For extent tree v2 we overload the extent root with the block group root, as
2363 * we will have multiple extent roots.
2364 */
2365BTRFS_SETGET_STACK_FUNCS(backup_block_group_root, struct btrfs_root_backup,
2366 extent_root, 64);
2367BTRFS_SETGET_STACK_FUNCS(backup_block_group_root_gen, struct btrfs_root_backup,
2368 extent_root_gen, 64);
2369BTRFS_SETGET_STACK_FUNCS(backup_block_group_root_level,
2370 struct btrfs_root_backup, extent_root_level, 8);
2371
0940ebf6
ID
2372/* struct btrfs_balance_item */
2373BTRFS_SETGET_FUNCS(balance_flags, struct btrfs_balance_item, flags, 64);
607d432d 2374
1cbb1f45
JM
2375static inline void btrfs_balance_data(const struct extent_buffer *eb,
2376 const struct btrfs_balance_item *bi,
0940ebf6
ID
2377 struct btrfs_disk_balance_args *ba)
2378{
2379 read_eb_member(eb, bi, struct btrfs_balance_item, data, ba);
2380}
2381
2382static inline void btrfs_set_balance_data(struct extent_buffer *eb,
1cbb1f45
JM
2383 struct btrfs_balance_item *bi,
2384 const struct btrfs_disk_balance_args *ba)
0940ebf6
ID
2385{
2386 write_eb_member(eb, bi, struct btrfs_balance_item, data, ba);
2387}
2388
1cbb1f45
JM
2389static inline void btrfs_balance_meta(const struct extent_buffer *eb,
2390 const struct btrfs_balance_item *bi,
0940ebf6
ID
2391 struct btrfs_disk_balance_args *ba)
2392{
2393 read_eb_member(eb, bi, struct btrfs_balance_item, meta, ba);
2394}
2395
2396static inline void btrfs_set_balance_meta(struct extent_buffer *eb,
1cbb1f45
JM
2397 struct btrfs_balance_item *bi,
2398 const struct btrfs_disk_balance_args *ba)
0940ebf6
ID
2399{
2400 write_eb_member(eb, bi, struct btrfs_balance_item, meta, ba);
2401}
2402
1cbb1f45
JM
2403static inline void btrfs_balance_sys(const struct extent_buffer *eb,
2404 const struct btrfs_balance_item *bi,
0940ebf6
ID
2405 struct btrfs_disk_balance_args *ba)
2406{
2407 read_eb_member(eb, bi, struct btrfs_balance_item, sys, ba);
2408}
2409
2410static inline void btrfs_set_balance_sys(struct extent_buffer *eb,
1cbb1f45
JM
2411 struct btrfs_balance_item *bi,
2412 const struct btrfs_disk_balance_args *ba)
0940ebf6
ID
2413{
2414 write_eb_member(eb, bi, struct btrfs_balance_item, sys, ba);
2415}
2416
2417static inline void
2418btrfs_disk_balance_args_to_cpu(struct btrfs_balance_args *cpu,
1cbb1f45 2419 const struct btrfs_disk_balance_args *disk)
0940ebf6
ID
2420{
2421 memset(cpu, 0, sizeof(*cpu));
2422
2423 cpu->profiles = le64_to_cpu(disk->profiles);
2424 cpu->usage = le64_to_cpu(disk->usage);
2425 cpu->devid = le64_to_cpu(disk->devid);
2426 cpu->pstart = le64_to_cpu(disk->pstart);
2427 cpu->pend = le64_to_cpu(disk->pend);
2428 cpu->vstart = le64_to_cpu(disk->vstart);
2429 cpu->vend = le64_to_cpu(disk->vend);
2430 cpu->target = le64_to_cpu(disk->target);
2431 cpu->flags = le64_to_cpu(disk->flags);
7d824b6f 2432 cpu->limit = le64_to_cpu(disk->limit);
ed0df618
DS
2433 cpu->stripes_min = le32_to_cpu(disk->stripes_min);
2434 cpu->stripes_max = le32_to_cpu(disk->stripes_max);
0940ebf6
ID
2435}
2436
2437static inline void
2438btrfs_cpu_balance_args_to_disk(struct btrfs_disk_balance_args *disk,
1cbb1f45 2439 const struct btrfs_balance_args *cpu)
0940ebf6
ID
2440{
2441 memset(disk, 0, sizeof(*disk));
2442
2443 disk->profiles = cpu_to_le64(cpu->profiles);
2444 disk->usage = cpu_to_le64(cpu->usage);
2445 disk->devid = cpu_to_le64(cpu->devid);
2446 disk->pstart = cpu_to_le64(cpu->pstart);
2447 disk->pend = cpu_to_le64(cpu->pend);
2448 disk->vstart = cpu_to_le64(cpu->vstart);
2449 disk->vend = cpu_to_le64(cpu->vend);
2450 disk->target = cpu_to_le64(cpu->target);
2451 disk->flags = cpu_to_le64(cpu->flags);
7d824b6f 2452 disk->limit = cpu_to_le64(cpu->limit);
ed0df618
DS
2453 disk->stripes_min = cpu_to_le32(cpu->stripes_min);
2454 disk->stripes_max = cpu_to_le32(cpu->stripes_max);
0940ebf6
ID
2455}
2456
2457/* struct btrfs_super_block */
db94535d 2458BTRFS_SETGET_STACK_FUNCS(super_bytenr, struct btrfs_super_block, bytenr, 64);
a061fc8d 2459BTRFS_SETGET_STACK_FUNCS(super_flags, struct btrfs_super_block, flags, 64);
5f39d397
CM
2460BTRFS_SETGET_STACK_FUNCS(super_generation, struct btrfs_super_block,
2461 generation, 64);
2462BTRFS_SETGET_STACK_FUNCS(super_root, struct btrfs_super_block, root, 64);
0b86a832
CM
2463BTRFS_SETGET_STACK_FUNCS(super_sys_array_size,
2464 struct btrfs_super_block, sys_chunk_array_size, 32);
84234f3a
YZ
2465BTRFS_SETGET_STACK_FUNCS(super_chunk_root_generation,
2466 struct btrfs_super_block, chunk_root_generation, 64);
db94535d
CM
2467BTRFS_SETGET_STACK_FUNCS(super_root_level, struct btrfs_super_block,
2468 root_level, 8);
0b86a832
CM
2469BTRFS_SETGET_STACK_FUNCS(super_chunk_root, struct btrfs_super_block,
2470 chunk_root, 64);
2471BTRFS_SETGET_STACK_FUNCS(super_chunk_root_level, struct btrfs_super_block,
e02119d5
CM
2472 chunk_root_level, 8);
2473BTRFS_SETGET_STACK_FUNCS(super_log_root, struct btrfs_super_block,
2474 log_root, 64);
c3027eb5
CM
2475BTRFS_SETGET_STACK_FUNCS(super_log_root_transid, struct btrfs_super_block,
2476 log_root_transid, 64);
e02119d5
CM
2477BTRFS_SETGET_STACK_FUNCS(super_log_root_level, struct btrfs_super_block,
2478 log_root_level, 8);
db94535d
CM
2479BTRFS_SETGET_STACK_FUNCS(super_total_bytes, struct btrfs_super_block,
2480 total_bytes, 64);
2481BTRFS_SETGET_STACK_FUNCS(super_bytes_used, struct btrfs_super_block,
2482 bytes_used, 64);
5f39d397
CM
2483BTRFS_SETGET_STACK_FUNCS(super_sectorsize, struct btrfs_super_block,
2484 sectorsize, 32);
2485BTRFS_SETGET_STACK_FUNCS(super_nodesize, struct btrfs_super_block,
2486 nodesize, 32);
87ee04eb
CM
2487BTRFS_SETGET_STACK_FUNCS(super_stripesize, struct btrfs_super_block,
2488 stripesize, 32);
5f39d397
CM
2489BTRFS_SETGET_STACK_FUNCS(super_root_dir, struct btrfs_super_block,
2490 root_dir_objectid, 64);
8a4b83cc
CM
2491BTRFS_SETGET_STACK_FUNCS(super_num_devices, struct btrfs_super_block,
2492 num_devices, 64);
f2b636e8
JB
2493BTRFS_SETGET_STACK_FUNCS(super_compat_flags, struct btrfs_super_block,
2494 compat_flags, 64);
2495BTRFS_SETGET_STACK_FUNCS(super_compat_ro_flags, struct btrfs_super_block,
12534832 2496 compat_ro_flags, 64);
f2b636e8
JB
2497BTRFS_SETGET_STACK_FUNCS(super_incompat_flags, struct btrfs_super_block,
2498 incompat_flags, 64);
607d432d
JB
2499BTRFS_SETGET_STACK_FUNCS(super_csum_type, struct btrfs_super_block,
2500 csum_type, 16);
0af3d00b
JB
2501BTRFS_SETGET_STACK_FUNCS(super_cache_generation, struct btrfs_super_block,
2502 cache_generation, 64);
3cae210f 2503BTRFS_SETGET_STACK_FUNCS(super_magic, struct btrfs_super_block, magic, 64);
26432799
SB
2504BTRFS_SETGET_STACK_FUNCS(super_uuid_tree_generation, struct btrfs_super_block,
2505 uuid_tree_generation, 64);
9c54e80d
JB
2506BTRFS_SETGET_STACK_FUNCS(super_block_group_root, struct btrfs_super_block,
2507 block_group_root, 64);
2508BTRFS_SETGET_STACK_FUNCS(super_block_group_root_generation,
2509 struct btrfs_super_block,
2510 block_group_root_generation, 64);
2511BTRFS_SETGET_STACK_FUNCS(super_block_group_root_level, struct btrfs_super_block,
2512 block_group_root_level, 8);
607d432d 2513
af024ed2
JT
2514int btrfs_super_csum_size(const struct btrfs_super_block *s);
2515const char *btrfs_super_csum_name(u16 csum_type);
b4e967be 2516const char *btrfs_super_csum_driver(u16 csum_type);
604997b4 2517size_t __attribute_const__ btrfs_get_num_csums(void);
f7cea56c 2518
2e635a27 2519
851cd173
LB
2520/*
2521 * The leaf data grows from end-to-front in the node.
2522 * this returns the address of the start of the last item,
2523 * which is the stop of the leaf data stack
2524 */
8f881e8c 2525static inline unsigned int leaf_data_end(const struct extent_buffer *leaf)
851cd173
LB
2526{
2527 u32 nr = btrfs_header_nritems(leaf);
2528
2529 if (nr == 0)
8f881e8c 2530 return BTRFS_LEAF_DATA_SIZE(leaf->fs_info);
3212fa14 2531 return btrfs_item_offset(leaf, nr - 1);
851cd173
LB
2532}
2533
5f39d397 2534/* struct btrfs_file_extent_item */
203f44c5
QW
2535BTRFS_SETGET_STACK_FUNCS(stack_file_extent_type, struct btrfs_file_extent_item,
2536 type, 8);
3cae210f
QW
2537BTRFS_SETGET_STACK_FUNCS(stack_file_extent_disk_bytenr,
2538 struct btrfs_file_extent_item, disk_bytenr, 64);
2539BTRFS_SETGET_STACK_FUNCS(stack_file_extent_offset,
2540 struct btrfs_file_extent_item, offset, 64);
2541BTRFS_SETGET_STACK_FUNCS(stack_file_extent_generation,
2542 struct btrfs_file_extent_item, generation, 64);
2543BTRFS_SETGET_STACK_FUNCS(stack_file_extent_num_bytes,
2544 struct btrfs_file_extent_item, num_bytes, 64);
203f44c5
QW
2545BTRFS_SETGET_STACK_FUNCS(stack_file_extent_ram_bytes,
2546 struct btrfs_file_extent_item, ram_bytes, 64);
e20d6c5b
JB
2547BTRFS_SETGET_STACK_FUNCS(stack_file_extent_disk_num_bytes,
2548 struct btrfs_file_extent_item, disk_num_bytes, 64);
2549BTRFS_SETGET_STACK_FUNCS(stack_file_extent_compression,
2550 struct btrfs_file_extent_item, compression, 8);
9f5fae2f 2551
d397712b 2552static inline unsigned long
1cbb1f45 2553btrfs_file_extent_inline_start(const struct btrfs_file_extent_item *e)
236454df 2554{
7ec20afb 2555 return (unsigned long)e + BTRFS_FILE_EXTENT_INLINE_DATA_START;
236454df
CM
2556}
2557
2558static inline u32 btrfs_file_extent_calc_inline_size(u32 datasize)
2559{
7ec20afb 2560 return BTRFS_FILE_EXTENT_INLINE_DATA_START + datasize;
9f5fae2f
CM
2561}
2562
203f44c5 2563BTRFS_SETGET_FUNCS(file_extent_type, struct btrfs_file_extent_item, type, 8);
db94535d
CM
2564BTRFS_SETGET_FUNCS(file_extent_disk_bytenr, struct btrfs_file_extent_item,
2565 disk_bytenr, 64);
5f39d397
CM
2566BTRFS_SETGET_FUNCS(file_extent_generation, struct btrfs_file_extent_item,
2567 generation, 64);
db94535d
CM
2568BTRFS_SETGET_FUNCS(file_extent_disk_num_bytes, struct btrfs_file_extent_item,
2569 disk_num_bytes, 64);
5f39d397
CM
2570BTRFS_SETGET_FUNCS(file_extent_offset, struct btrfs_file_extent_item,
2571 offset, 64);
db94535d
CM
2572BTRFS_SETGET_FUNCS(file_extent_num_bytes, struct btrfs_file_extent_item,
2573 num_bytes, 64);
c8b97818
CM
2574BTRFS_SETGET_FUNCS(file_extent_ram_bytes, struct btrfs_file_extent_item,
2575 ram_bytes, 64);
2576BTRFS_SETGET_FUNCS(file_extent_compression, struct btrfs_file_extent_item,
2577 compression, 8);
2578BTRFS_SETGET_FUNCS(file_extent_encryption, struct btrfs_file_extent_item,
2579 encryption, 8);
2580BTRFS_SETGET_FUNCS(file_extent_other_encoding, struct btrfs_file_extent_item,
2581 other_encoding, 16);
2582
c8b97818
CM
2583/*
2584 * this returns the number of bytes used by the item on disk, minus the
2585 * size of any extent headers. If a file is compressed on disk, this is
2586 * the compressed size
2587 */
1cbb1f45
JM
2588static inline u32 btrfs_file_extent_inline_item_len(
2589 const struct extent_buffer *eb,
437bd07e 2590 int nr)
c8b97818 2591{
3212fa14 2592 return btrfs_item_size(eb, nr) - BTRFS_FILE_EXTENT_INLINE_DATA_START;
c8b97818 2593}
9f5fae2f 2594
630dc772
AJ
2595/* btrfs_qgroup_status_item */
2596BTRFS_SETGET_FUNCS(qgroup_status_generation, struct btrfs_qgroup_status_item,
2597 generation, 64);
2598BTRFS_SETGET_FUNCS(qgroup_status_version, struct btrfs_qgroup_status_item,
2599 version, 64);
2600BTRFS_SETGET_FUNCS(qgroup_status_flags, struct btrfs_qgroup_status_item,
2601 flags, 64);
2f232036
JS
2602BTRFS_SETGET_FUNCS(qgroup_status_rescan, struct btrfs_qgroup_status_item,
2603 rescan, 64);
630dc772
AJ
2604
2605/* btrfs_qgroup_info_item */
2606BTRFS_SETGET_FUNCS(qgroup_info_generation, struct btrfs_qgroup_info_item,
2607 generation, 64);
2608BTRFS_SETGET_FUNCS(qgroup_info_rfer, struct btrfs_qgroup_info_item, rfer, 64);
2609BTRFS_SETGET_FUNCS(qgroup_info_rfer_cmpr, struct btrfs_qgroup_info_item,
2610 rfer_cmpr, 64);
2611BTRFS_SETGET_FUNCS(qgroup_info_excl, struct btrfs_qgroup_info_item, excl, 64);
2612BTRFS_SETGET_FUNCS(qgroup_info_excl_cmpr, struct btrfs_qgroup_info_item,
2613 excl_cmpr, 64);
2614
2615BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_generation,
2616 struct btrfs_qgroup_info_item, generation, 64);
2617BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_rfer, struct btrfs_qgroup_info_item,
2618 rfer, 64);
2619BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_rfer_cmpr,
2620 struct btrfs_qgroup_info_item, rfer_cmpr, 64);
2621BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_excl, struct btrfs_qgroup_info_item,
2622 excl, 64);
2623BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_excl_cmpr,
2624 struct btrfs_qgroup_info_item, excl_cmpr, 64);
2625
2626/* btrfs_qgroup_limit_item */
2627BTRFS_SETGET_FUNCS(qgroup_limit_flags, struct btrfs_qgroup_limit_item,
2628 flags, 64);
2629BTRFS_SETGET_FUNCS(qgroup_limit_max_rfer, struct btrfs_qgroup_limit_item,
2630 max_rfer, 64);
2631BTRFS_SETGET_FUNCS(qgroup_limit_max_excl, struct btrfs_qgroup_limit_item,
2632 max_excl, 64);
2633BTRFS_SETGET_FUNCS(qgroup_limit_rsv_rfer, struct btrfs_qgroup_limit_item,
2634 rsv_rfer, 64);
2635BTRFS_SETGET_FUNCS(qgroup_limit_rsv_excl, struct btrfs_qgroup_limit_item,
2636 rsv_excl, 64);
2637
a2bff640
SB
2638/* btrfs_dev_replace_item */
2639BTRFS_SETGET_FUNCS(dev_replace_src_devid,
2640 struct btrfs_dev_replace_item, src_devid, 64);
2641BTRFS_SETGET_FUNCS(dev_replace_cont_reading_from_srcdev_mode,
2642 struct btrfs_dev_replace_item, cont_reading_from_srcdev_mode,
2643 64);
2644BTRFS_SETGET_FUNCS(dev_replace_replace_state, struct btrfs_dev_replace_item,
2645 replace_state, 64);
2646BTRFS_SETGET_FUNCS(dev_replace_time_started, struct btrfs_dev_replace_item,
2647 time_started, 64);
2648BTRFS_SETGET_FUNCS(dev_replace_time_stopped, struct btrfs_dev_replace_item,
2649 time_stopped, 64);
2650BTRFS_SETGET_FUNCS(dev_replace_num_write_errors, struct btrfs_dev_replace_item,
2651 num_write_errors, 64);
2652BTRFS_SETGET_FUNCS(dev_replace_num_uncorrectable_read_errors,
2653 struct btrfs_dev_replace_item, num_uncorrectable_read_errors,
2654 64);
2655BTRFS_SETGET_FUNCS(dev_replace_cursor_left, struct btrfs_dev_replace_item,
2656 cursor_left, 64);
2657BTRFS_SETGET_FUNCS(dev_replace_cursor_right, struct btrfs_dev_replace_item,
2658 cursor_right, 64);
2659
2660BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_src_devid,
2661 struct btrfs_dev_replace_item, src_devid, 64);
2662BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_cont_reading_from_srcdev_mode,
2663 struct btrfs_dev_replace_item,
2664 cont_reading_from_srcdev_mode, 64);
2665BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_replace_state,
2666 struct btrfs_dev_replace_item, replace_state, 64);
2667BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_time_started,
2668 struct btrfs_dev_replace_item, time_started, 64);
2669BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_time_stopped,
2670 struct btrfs_dev_replace_item, time_stopped, 64);
2671BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_num_write_errors,
2672 struct btrfs_dev_replace_item, num_write_errors, 64);
2673BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_num_uncorrectable_read_errors,
2674 struct btrfs_dev_replace_item,
2675 num_uncorrectable_read_errors, 64);
2676BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_cursor_left,
2677 struct btrfs_dev_replace_item, cursor_left, 64);
2678BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_cursor_right,
2679 struct btrfs_dev_replace_item, cursor_right, 64);
2680
4beb1b8b
CM
2681/* helper function to cast into the data area of the leaf. */
2682#define btrfs_item_ptr(leaf, slot, type) \
3d9ec8c4 2683 ((type *)(BTRFS_LEAF_DATA_OFFSET + \
3212fa14 2684 btrfs_item_offset(leaf, slot)))
5f39d397
CM
2685
2686#define btrfs_item_ptr_offset(leaf, slot) \
3d9ec8c4 2687 ((unsigned long)(BTRFS_LEAF_DATA_OFFSET + \
3212fa14 2688 btrfs_item_offset(leaf, slot)))
4beb1b8b 2689
65019df8
JT
2690static inline u32 btrfs_crc32c(u32 crc, const void *address, unsigned length)
2691{
2692 return crc32c(crc, address, length);
2693}
2694
2695static inline void btrfs_crc32c_final(u32 crc, u8 *result)
2696{
2697 put_unaligned_le32(~crc, result);
2698}
2699
9678c543
NB
2700static inline u64 btrfs_name_hash(const char *name, int len)
2701{
2702 return crc32c((u32)~1, name, len);
2703}
2704
2705/*
2706 * Figure the key offset of an extended inode ref
2707 */
2708static inline u64 btrfs_extref_hash(u64 parent_objectid, const char *name,
2709 int len)
2710{
2711 return (u64) crc32c(parent_objectid, name, len);
2712}
2713
3b16a4e3
JB
2714static inline gfp_t btrfs_alloc_write_mask(struct address_space *mapping)
2715{
c62d2555 2716 return mapping_gfp_constraint(mapping, ~__GFP_FS);
3b16a4e3
JB
2717}
2718
b18c6685 2719/* extent-tree.c */
28f75a0e 2720
167ce953 2721enum btrfs_inline_ref_type {
bbe339cc
DS
2722 BTRFS_REF_TYPE_INVALID,
2723 BTRFS_REF_TYPE_BLOCK,
2724 BTRFS_REF_TYPE_DATA,
2725 BTRFS_REF_TYPE_ANY,
167ce953
LB
2726};
2727
2728int btrfs_get_extent_inline_ref_type(const struct extent_buffer *eb,
2729 struct btrfs_extent_inline_ref *iref,
2730 enum btrfs_inline_ref_type is_data);
0785a9aa 2731u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset);
167ce953 2732
fe5ecbe8
DS
2733/*
2734 * Take the number of bytes to be checksummmed and figure out how many leaves
2735 * it would require to store the csums for that many bytes.
2736 */
2737static inline u64 btrfs_csum_bytes_to_leaves(
2738 const struct btrfs_fs_info *fs_info, u64 csum_bytes)
2739{
2740 const u64 num_csums = csum_bytes >> fs_info->sectorsize_bits;
2741
2742 return DIV_ROUND_UP_ULL(num_csums, fs_info->csums_per_leaf);
2743}
28f75a0e 2744
2bd36e7b
JB
2745/*
2746 * Use this if we would be adding new items, as we could split nodes as we cow
2747 * down the tree.
2748 */
2749static inline u64 btrfs_calc_insert_metadata_size(struct btrfs_fs_info *fs_info,
2750 unsigned num_items)
16cdcec7 2751{
70e7af24 2752 return (u64)fs_info->nodesize * BTRFS_MAX_LEVEL * 2 * num_items;
07127184
JB
2753}
2754
2755/*
2bd36e7b
JB
2756 * Doing a truncate or a modification won't result in new nodes or leaves, just
2757 * what we need for COW.
07127184 2758 */
2bd36e7b 2759static inline u64 btrfs_calc_metadata_size(struct btrfs_fs_info *fs_info,
07127184
JB
2760 unsigned num_items)
2761{
70e7af24 2762 return (u64)fs_info->nodesize * BTRFS_MAX_LEVEL * num_items;
16cdcec7
MX
2763}
2764
6f410d1b
JB
2765int btrfs_add_excluded_extent(struct btrfs_fs_info *fs_info,
2766 u64 start, u64 num_bytes);
32da5386 2767void btrfs_free_excluded_extents(struct btrfs_block_group *cache);
56bec294 2768int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
c79a70b1 2769 unsigned long count);
31890da0
JB
2770void btrfs_cleanup_ref_head_accounting(struct btrfs_fs_info *fs_info,
2771 struct btrfs_delayed_ref_root *delayed_refs,
2772 struct btrfs_delayed_ref_head *head);
2ff7e61e 2773int btrfs_lookup_data_extent(struct btrfs_fs_info *fs_info, u64 start, u64 len);
a22285a6 2774int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
2ff7e61e 2775 struct btrfs_fs_info *fs_info, u64 bytenr,
3173a18f 2776 u64 offset, int metadata, u64 *refs, u64 *flags);
b25c36f8
NB
2777int btrfs_pin_extent(struct btrfs_trans_handle *trans, u64 bytenr, u64 num,
2778 int reserved);
9fce5704 2779int btrfs_pin_extent_for_log_replay(struct btrfs_trans_handle *trans,
e688b725 2780 u64 bytenr, u64 num_bytes);
bcdc428c 2781int btrfs_exclude_logged_extents(struct extent_buffer *eb);
e4c3b2dc 2782int btrfs_cross_ref_exist(struct btrfs_root *root,
a84d5d42 2783 u64 objectid, u64 offset, u64 bytenr, bool strict);
4d75f8a9 2784struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans,
310712b2
OS
2785 struct btrfs_root *root,
2786 u64 parent, u64 root_objectid,
2787 const struct btrfs_disk_key *key,
2788 int level, u64 hint,
9631e4cc
JB
2789 u64 empty_size,
2790 enum btrfs_lock_nesting nest);
f0486c68 2791void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
7a163608 2792 u64 root_id,
f0486c68 2793 struct extent_buffer *buf,
5581a51a 2794 u64 parent, int last_ref);
5d4f98a2 2795int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
84f7d8e6 2796 struct btrfs_root *root, u64 owner,
5846a3c2
QW
2797 u64 offset, u64 ram_bytes,
2798 struct btrfs_key *ins);
5d4f98a2 2799int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
5d4f98a2
YZ
2800 u64 root_objectid, u64 owner, u64 offset,
2801 struct btrfs_key *ins);
18513091 2802int btrfs_reserve_extent(struct btrfs_root *root, u64 ram_bytes, u64 num_bytes,
00361589 2803 u64 min_alloc_size, u64 empty_size, u64 hint_byte,
e570fd27 2804 struct btrfs_key *ins, int is_data, int delalloc);
e089f05c 2805int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
e339a6b0 2806 struct extent_buffer *buf, int full_backref);
5d4f98a2 2807int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
e339a6b0 2808 struct extent_buffer *buf, int full_backref);
5d4f98a2 2809int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
42c9d0b5 2810 struct extent_buffer *eb, u64 flags,
b1c79e09 2811 int level, int is_data);
ffd4bb2a 2812int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_ref *ref);
5d4f98a2 2813
2ff7e61e
JM
2814int btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info,
2815 u64 start, u64 len, int delalloc);
7bfc1007 2816int btrfs_pin_reserved_extent(struct btrfs_trans_handle *trans, u64 start,
a0fbf736 2817 u64 len);
5ead2dd0 2818int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans);
b18c6685 2819int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
82fa113f 2820 struct btrfs_ref *generic_ref);
5d4f98a2 2821
4184ea7f 2822void btrfs_clear_space_info_full(struct btrfs_fs_info *info);
08e007d2 2823
fd7fb634
QW
2824/*
2825 * Different levels for to flush space when doing space reservations.
2826 *
2827 * The higher the level, the more methods we try to reclaim space.
2828 */
08e007d2
MX
2829enum btrfs_reserve_flush_enum {
2830 /* If we are in the transaction, we can't flush anything.*/
2831 BTRFS_RESERVE_NO_FLUSH,
fd7fb634 2832
08e007d2 2833 /*
fd7fb634
QW
2834 * Flush space by:
2835 * - Running delayed inode items
2836 * - Allocating a new chunk
08e007d2
MX
2837 */
2838 BTRFS_RESERVE_FLUSH_LIMIT,
fd7fb634
QW
2839
2840 /*
2841 * Flush space by:
2842 * - Running delayed inode items
2843 * - Running delayed refs
2844 * - Running delalloc and waiting for ordered extents
2845 * - Allocating a new chunk
2846 */
d3984c90 2847 BTRFS_RESERVE_FLUSH_EVICT,
fd7fb634
QW
2848
2849 /*
2850 * Flush space by above mentioned methods and by:
2851 * - Running delayed iputs
1a9fd417 2852 * - Committing transaction
fd7fb634 2853 *
1a9fd417 2854 * Can be interrupted by a fatal signal.
fd7fb634 2855 */
058e6d1d
JB
2856 BTRFS_RESERVE_FLUSH_DATA,
2857 BTRFS_RESERVE_FLUSH_FREE_SPACE_INODE,
08e007d2 2858 BTRFS_RESERVE_FLUSH_ALL,
fd7fb634
QW
2859
2860 /*
2861 * Pretty much the same as FLUSH_ALL, but can also steal space from
2862 * global rsv.
2863 *
1a9fd417 2864 * Can be interrupted by a fatal signal.
fd7fb634 2865 */
7f9fe614 2866 BTRFS_RESERVE_FLUSH_ALL_STEAL,
08e007d2
MX
2867};
2868
f376df2b
JB
2869enum btrfs_flush_state {
2870 FLUSH_DELAYED_ITEMS_NR = 1,
2871 FLUSH_DELAYED_ITEMS = 2,
413df725
JB
2872 FLUSH_DELAYED_REFS_NR = 3,
2873 FLUSH_DELAYED_REFS = 4,
2874 FLUSH_DELALLOC = 5,
2875 FLUSH_DELALLOC_WAIT = 6,
03fe78cc
JB
2876 FLUSH_DELALLOC_FULL = 7,
2877 ALLOC_CHUNK = 8,
2878 ALLOC_CHUNK_FORCE = 9,
2879 RUN_DELAYED_IPUTS = 10,
2880 COMMIT_TRANS = 11,
f376df2b
JB
2881};
2882
d5c12070
MX
2883int btrfs_subvolume_reserve_metadata(struct btrfs_root *root,
2884 struct btrfs_block_rsv *rsv,
c4c129db 2885 int nitems, bool use_global_rsv);
e85fde51 2886void btrfs_subvolume_release_metadata(struct btrfs_root *root,
7775c818 2887 struct btrfs_block_rsv *rsv);
8702ba93 2888void btrfs_delalloc_release_extents(struct btrfs_inode *inode, u64 num_bytes);
8b62f87b 2889
28c9b1e7
OS
2890int btrfs_delalloc_reserve_metadata(struct btrfs_inode *inode, u64 num_bytes,
2891 u64 disk_num_bytes);
6d07bcec 2892u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo);
2ff7e61e 2893int btrfs_error_unpin_extent_range(struct btrfs_fs_info *fs_info,
acce952b 2894 u64 start, u64 end);
2ff7e61e 2895int btrfs_discard_extent(struct btrfs_fs_info *fs_info, u64 bytenr,
1edb647b 2896 u64 num_bytes, u64 *actual_bytes);
2ff7e61e 2897int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range);
acce952b 2898
c59021f8 2899int btrfs_init_space_info(struct btrfs_fs_info *fs_info);
bed92eae
AJ
2900int btrfs_delayed_refs_qgroup_accounting(struct btrfs_trans_handle *trans,
2901 struct btrfs_fs_info *fs_info);
ea14b57f
DS
2902int btrfs_start_write_no_snapshotting(struct btrfs_root *root);
2903void btrfs_end_write_no_snapshotting(struct btrfs_root *root);
0bc19f90 2904void btrfs_wait_for_snapshot_creation(struct btrfs_root *root);
a5ed9182 2905
dee26a9f 2906/* ctree.c */
310712b2 2907int btrfs_bin_search(struct extent_buffer *eb, const struct btrfs_key *key,
e3b83361 2908 int *slot);
e1f60a65 2909int __pure btrfs_comp_cpu_keys(const struct btrfs_key *k1, const struct btrfs_key *k2);
0b86a832
CM
2910int btrfs_previous_item(struct btrfs_root *root,
2911 struct btrfs_path *path, u64 min_objectid,
2912 int type);
ade2e0b3
WS
2913int btrfs_previous_extent_item(struct btrfs_root *root,
2914 struct btrfs_path *path, u64 min_objectid);
b7a0365e
DD
2915void btrfs_set_item_key_safe(struct btrfs_fs_info *fs_info,
2916 struct btrfs_path *path,
310712b2 2917 const struct btrfs_key *new_key);
925baedd 2918struct extent_buffer *btrfs_root_node(struct btrfs_root *root);
e7a84565 2919int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
3f157a2f 2920 struct btrfs_key *key, int lowest_level,
de78b51a 2921 u64 min_trans);
3f157a2f 2922int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
de78b51a 2923 struct btrfs_path *path,
3f157a2f 2924 u64 min_trans);
4b231ae4
DS
2925struct extent_buffer *btrfs_read_node_slot(struct extent_buffer *parent,
2926 int slot);
2927
5f39d397
CM
2928int btrfs_cow_block(struct btrfs_trans_handle *trans,
2929 struct btrfs_root *root, struct extent_buffer *buf,
2930 struct extent_buffer *parent, int parent_slot,
9631e4cc
JB
2931 struct extent_buffer **cow_ret,
2932 enum btrfs_lock_nesting nest);
be20aa9d
CM
2933int btrfs_copy_root(struct btrfs_trans_handle *trans,
2934 struct btrfs_root *root,
2935 struct extent_buffer *buf,
2936 struct extent_buffer **cow_ret, u64 new_root_objectid);
5d4f98a2
YZ
2937int btrfs_block_can_be_shared(struct btrfs_root *root,
2938 struct extent_buffer *buf);
c71dd880 2939void btrfs_extend_item(struct btrfs_path *path, u32 data_size);
78ac4f9e 2940void btrfs_truncate_item(struct btrfs_path *path, u32 new_size, int from_end);
459931ec
CM
2941int btrfs_split_item(struct btrfs_trans_handle *trans,
2942 struct btrfs_root *root,
2943 struct btrfs_path *path,
310712b2 2944 const struct btrfs_key *new_key,
459931ec 2945 unsigned long split_offset);
ad48fd75
YZ
2946int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
2947 struct btrfs_root *root,
2948 struct btrfs_path *path,
310712b2 2949 const struct btrfs_key *new_key);
e33d5c3d
KN
2950int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *path,
2951 u64 inum, u64 ioff, u8 key_type, struct btrfs_key *found_key);
310712b2
OS
2952int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2953 const struct btrfs_key *key, struct btrfs_path *p,
2954 int ins_len, int cow);
2955int btrfs_search_old_slot(struct btrfs_root *root, const struct btrfs_key *key,
5d9e75c4 2956 struct btrfs_path *p, u64 time_seq);
2f38b3e1 2957int btrfs_search_slot_for_read(struct btrfs_root *root,
310712b2
OS
2958 const struct btrfs_key *key,
2959 struct btrfs_path *p, int find_higher,
2960 int return_any);
6702ed49 2961int btrfs_realloc_node(struct btrfs_trans_handle *trans,
5f39d397 2962 struct btrfs_root *root, struct extent_buffer *parent,
de78b51a 2963 int start_slot, u64 *last_ret,
a6b6e75e 2964 struct btrfs_key *progress);
b3b4aa74 2965void btrfs_release_path(struct btrfs_path *p);
2c90e5d6
CM
2966struct btrfs_path *btrfs_alloc_path(void);
2967void btrfs_free_path(struct btrfs_path *p);
b4ce94de 2968
85e21bac
CM
2969int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2970 struct btrfs_path *path, int slot, int nr);
85e21bac
CM
2971static inline int btrfs_del_item(struct btrfs_trans_handle *trans,
2972 struct btrfs_root *root,
2973 struct btrfs_path *path)
2974{
2975 return btrfs_del_items(trans, root, path, path->slots[0], 1);
2976}
2977
b7ef5f3a
FM
2978/*
2979 * Describes a batch of items to insert in a btree. This is used by
f0641656 2980 * btrfs_insert_empty_items().
b7ef5f3a
FM
2981 */
2982struct btrfs_item_batch {
2983 /*
2984 * Pointer to an array containing the keys of the items to insert (in
2985 * sorted order).
2986 */
2987 const struct btrfs_key *keys;
2988 /* Pointer to an array containing the data size for each item to insert. */
2989 const u32 *data_sizes;
2990 /*
2991 * The sum of data sizes for all items. The caller can compute this while
2992 * setting up the data_sizes array, so it ends up being more efficient
2993 * than having btrfs_insert_empty_items() or setup_item_for_insert()
2994 * doing it, as it would avoid an extra loop over a potentially large
2995 * array, and in the case of setup_item_for_insert(), we would be doing
2996 * it while holding a write lock on a leaf and often on upper level nodes
2997 * too, unnecessarily increasing the size of a critical section.
2998 */
2999 u32 total_data_size;
3000 /* Size of the keys and data_sizes arrays (number of items in the batch). */
3001 int nr;
3002};
3003
f0641656
FM
3004void btrfs_setup_item_for_insert(struct btrfs_root *root,
3005 struct btrfs_path *path,
3006 const struct btrfs_key *key,
3007 u32 data_size);
310712b2
OS
3008int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3009 const struct btrfs_key *key, void *data, u32 data_size);
9c58309d
CM
3010int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
3011 struct btrfs_root *root,
3012 struct btrfs_path *path,
b7ef5f3a 3013 const struct btrfs_item_batch *batch);
9c58309d
CM
3014
3015static inline int btrfs_insert_empty_item(struct btrfs_trans_handle *trans,
3016 struct btrfs_root *root,
3017 struct btrfs_path *path,
310712b2 3018 const struct btrfs_key *key,
9c58309d
CM
3019 u32 data_size)
3020{
b7ef5f3a
FM
3021 struct btrfs_item_batch batch;
3022
3023 batch.keys = key;
3024 batch.data_sizes = &data_size;
3025 batch.total_data_size = data_size;
3026 batch.nr = 1;
3027
3028 return btrfs_insert_empty_items(trans, root, path, &batch);
9c58309d
CM
3029}
3030
16e7549f 3031int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path);
3d7806ec
JS
3032int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
3033 u64 time_seq);
0ff40a91
MPS
3034
3035int btrfs_search_backwards(struct btrfs_root *root, struct btrfs_key *key,
3036 struct btrfs_path *path);
3037
1c8f52a5
AB
3038static inline int btrfs_next_old_item(struct btrfs_root *root,
3039 struct btrfs_path *p, u64 time_seq)
c7d22a3c
JS
3040{
3041 ++p->slots[0];
3042 if (p->slots[0] >= btrfs_header_nritems(p->nodes[0]))
1c8f52a5 3043 return btrfs_next_old_leaf(root, p, time_seq);
c7d22a3c
JS
3044 return 0;
3045}
809d6902
DS
3046
3047/*
3048 * Search the tree again to find a leaf with greater keys.
3049 *
3050 * Returns 0 if it found something or 1 if there are no greater leaves.
3051 * Returns < 0 on error.
3052 */
3053static inline int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
3054{
3055 return btrfs_next_old_leaf(root, path, 0);
3056}
3057
1c8f52a5
AB
3058static inline int btrfs_next_item(struct btrfs_root *root, struct btrfs_path *p)
3059{
3060 return btrfs_next_old_item(root, p, 0);
3061}
e902baac 3062int btrfs_leaf_free_space(struct extent_buffer *leaf);
0078a9f9
NB
3063int __must_check btrfs_drop_snapshot(struct btrfs_root *root, int update_ref,
3064 int for_reloc);
f82d02d9
YZ
3065int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
3066 struct btrfs_root *root,
3067 struct extent_buffer *node,
3068 struct extent_buffer *parent);
7841cb28
DS
3069static inline int btrfs_fs_closing(struct btrfs_fs_info *fs_info)
3070{
3071 /*
afcdd129 3072 * Do it this way so we only ever do one test_bit in the normal case.
7841cb28 3073 */
afcdd129
JB
3074 if (test_bit(BTRFS_FS_CLOSING_START, &fs_info->flags)) {
3075 if (test_bit(BTRFS_FS_CLOSING_DONE, &fs_info->flags))
3076 return 2;
3077 return 1;
3078 }
3079 return 0;
7841cb28 3080}
babbf170
MX
3081
3082/*
3083 * If we remount the fs to be R/O or umount the fs, the cleaner needn't do
3084 * anything except sleeping. This function is used to check the status of
3085 * the fs.
a0a1db70
FM
3086 * We check for BTRFS_FS_STATE_RO to avoid races with a concurrent remount,
3087 * since setting and checking for SB_RDONLY in the superblock's flags is not
3088 * atomic.
babbf170 3089 */
2ff7e61e 3090static inline int btrfs_need_cleaner_sleep(struct btrfs_fs_info *fs_info)
babbf170 3091{
a0a1db70
FM
3092 return test_bit(BTRFS_FS_STATE_RO, &fs_info->fs_state) ||
3093 btrfs_fs_closing(fs_info);
3094}
3095
3096static inline void btrfs_set_sb_rdonly(struct super_block *sb)
3097{
3098 sb->s_flags |= SB_RDONLY;
3099 set_bit(BTRFS_FS_STATE_RO, &btrfs_sb(sb)->fs_state);
3100}
3101
3102static inline void btrfs_clear_sb_rdonly(struct super_block *sb)
3103{
3104 sb->s_flags &= ~SB_RDONLY;
3105 clear_bit(BTRFS_FS_STATE_RO, &btrfs_sb(sb)->fs_state);
babbf170
MX
3106}
3107
dee26a9f 3108/* root-item.c */
6025c19f
LF
3109int btrfs_add_root_ref(struct btrfs_trans_handle *trans, u64 root_id,
3110 u64 ref_id, u64 dirid, u64 sequence, const char *name,
3111 int name_len);
3ee1c553
LF
3112int btrfs_del_root_ref(struct btrfs_trans_handle *trans, u64 root_id,
3113 u64 ref_id, u64 dirid, u64 *sequence, const char *name,
3114 int name_len);
1cd5447e 3115int btrfs_del_root(struct btrfs_trans_handle *trans,
ab9ce7d4 3116 const struct btrfs_key *key);
310712b2
OS
3117int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3118 const struct btrfs_key *key,
3119 struct btrfs_root_item *item);
b45a9d8b
JM
3120int __must_check btrfs_update_root(struct btrfs_trans_handle *trans,
3121 struct btrfs_root *root,
3122 struct btrfs_key *key,
3123 struct btrfs_root_item *item);
310712b2 3124int btrfs_find_root(struct btrfs_root *root, const struct btrfs_key *search_key,
cb517eab
MX
3125 struct btrfs_path *path, struct btrfs_root_item *root_item,
3126 struct btrfs_key *root_key);
6bccf3ab 3127int btrfs_find_orphan_roots(struct btrfs_fs_info *fs_info);
bf5f32ec
MF
3128void btrfs_set_root_node(struct btrfs_root_item *item,
3129 struct extent_buffer *node);
08fe4db1 3130void btrfs_check_and_init_root_item(struct btrfs_root_item *item);
8ea05e3a
AB
3131void btrfs_update_root_times(struct btrfs_trans_handle *trans,
3132 struct btrfs_root *root);
08fe4db1 3133
07b30a49 3134/* uuid-tree.c */
cdb345a8 3135int btrfs_uuid_tree_add(struct btrfs_trans_handle *trans, u8 *uuid, u8 type,
07b30a49 3136 u64 subid);
d1957791 3137int btrfs_uuid_tree_remove(struct btrfs_trans_handle *trans, u8 *uuid, u8 type,
07b30a49 3138 u64 subid);
560b7a4a 3139int btrfs_uuid_tree_iterate(struct btrfs_fs_info *fs_info);
07b30a49 3140
dee26a9f 3141/* dir-item.c */
9c52057c
CM
3142int btrfs_check_dir_item_collision(struct btrfs_root *root, u64 dir,
3143 const char *name, int name_len);
684572df 3144int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, const char *name,
8e7611cf 3145 int name_len, struct btrfs_inode *dir,
aec7477b 3146 struct btrfs_key *location, u8 type, u64 index);
7e38180e
CM
3147struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans,
3148 struct btrfs_root *root,
3149 struct btrfs_path *path, u64 dir,
3150 const char *name, int name_len,
3151 int mod);
3152struct btrfs_dir_item *
3153btrfs_lookup_dir_index_item(struct btrfs_trans_handle *trans,
3154 struct btrfs_root *root,
3155 struct btrfs_path *path, u64 dir,
8dcbc261 3156 u64 index, const char *name, int name_len,
7e38180e 3157 int mod);
4df27c4d
YZ
3158struct btrfs_dir_item *
3159btrfs_search_dir_index_item(struct btrfs_root *root,
3160 struct btrfs_path *path, u64 dirid,
3161 const char *name, int name_len);
7e38180e
CM
3162int btrfs_delete_one_dir_name(struct btrfs_trans_handle *trans,
3163 struct btrfs_root *root,
3164 struct btrfs_path *path,
3165 struct btrfs_dir_item *di);
5103e947 3166int btrfs_insert_xattr_item(struct btrfs_trans_handle *trans,
f34f57a3
YZ
3167 struct btrfs_root *root,
3168 struct btrfs_path *path, u64 objectid,
3169 const char *name, u16 name_len,
3170 const void *data, u16 data_len);
5103e947
JB
3171struct btrfs_dir_item *btrfs_lookup_xattr(struct btrfs_trans_handle *trans,
3172 struct btrfs_root *root,
3173 struct btrfs_path *path, u64 dir,
3174 const char *name, u16 name_len,
3175 int mod);
2ff7e61e 3176struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_fs_info *fs_info,
5f5bc6b1
FM
3177 struct btrfs_path *path,
3178 const char *name,
3179 int name_len);
7b128766
JB
3180
3181/* orphan.c */
3182int btrfs_insert_orphan_item(struct btrfs_trans_handle *trans,
3183 struct btrfs_root *root, u64 offset);
3184int btrfs_del_orphan_item(struct btrfs_trans_handle *trans,
3185 struct btrfs_root *root, u64 offset);
4df27c4d 3186int btrfs_find_orphan_item(struct btrfs_root *root, u64 offset);
7b128766 3187
dee26a9f 3188/* file-item.c */
facc8a22 3189struct btrfs_dio_private;
459931ec 3190int btrfs_del_csums(struct btrfs_trans_handle *trans,
40e046ac 3191 struct btrfs_root *root, u64 bytenr, u64 len);
6275193e 3192blk_status_t btrfs_lookup_bio_sums(struct inode *inode, struct bio *bio, u8 *dst);
b18c6685 3193int btrfs_insert_file_extent(struct btrfs_trans_handle *trans,
c8b97818
CM
3194 struct btrfs_root *root,
3195 u64 objectid, u64 pos,
3196 u64 disk_offset, u64 disk_num_bytes,
3197 u64 num_bytes, u64 offset, u64 ram_bytes,
3198 u8 compression, u8 encryption, u16 other_encoding);
dee26a9f
CM
3199int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
3200 struct btrfs_root *root,
3201 struct btrfs_path *path, u64 objectid,
db94535d 3202 u64 bytenr, int mod);
065631f6 3203int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans,
d20f7043 3204 struct btrfs_root *root,
e6dcd2dc 3205 struct btrfs_ordered_sum *sums);
bd242a08 3206blk_status_t btrfs_csum_one_bio(struct btrfs_inode *inode, struct bio *bio,
e331f6b1 3207 u64 offset, bool one_ordered);
a2de733c
AJ
3208int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end,
3209 struct list_head *list, int search_commit);
9cdc5124 3210void btrfs_extent_item_to_extent_map(struct btrfs_inode *inode,
7ffbb598
FM
3211 const struct btrfs_path *path,
3212 struct btrfs_file_extent_item *fi,
3213 const bool new_inline,
3214 struct extent_map *em);
41a2ee75
JB
3215int btrfs_inode_clear_file_extent_range(struct btrfs_inode *inode, u64 start,
3216 u64 len);
3217int btrfs_inode_set_file_extent_range(struct btrfs_inode *inode, u64 start,
3218 u64 len);
76aea537 3219void btrfs_inode_safe_disk_i_size_write(struct btrfs_inode *inode, u64 new_i_size);
a5eeb3d1 3220u64 btrfs_file_extent_end(const struct btrfs_path *path);
7ffbb598 3221
39279cc3 3222/* inode.c */
908930f3
NB
3223blk_status_t btrfs_submit_data_bio(struct inode *inode, struct bio *bio,
3224 int mirror_num, unsigned long bio_flags);
c3a3b19b
QW
3225unsigned int btrfs_verify_data_csum(struct btrfs_bio *bbio,
3226 u32 bio_offset, struct page *page,
3227 u64 start, u64 end);
fc4f21b1 3228struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
4ab47a8d 3229 u64 start, u64 len);
00361589 3230noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
7ee9e440 3231 u64 *orig_start, u64 *orig_block_len,
a84d5d42 3232 u64 *ram_bytes, bool strict);
4881ee5a 3233
2b877331
NB
3234void __btrfs_del_delalloc_inode(struct btrfs_root *root,
3235 struct btrfs_inode *inode);
3de4586c 3236struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry);
70ddc553 3237int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index);
e02119d5 3238int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4ec5934e 3239 struct btrfs_inode *dir, struct btrfs_inode *inode,
e02119d5
CM
3240 const char *name, int name_len);
3241int btrfs_add_link(struct btrfs_trans_handle *trans,
db0a669f 3242 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
e02119d5 3243 const char *name, int name_len, int add_backref, u64 index);
f60a2364 3244int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry);
217f42eb
NB
3245int btrfs_truncate_block(struct btrfs_inode *inode, loff_t from, loff_t len,
3246 int front);
e02119d5 3247
f9baa501 3248int btrfs_start_delalloc_snapshot(struct btrfs_root *root, bool in_reclaim_context);
9db4dc24 3249int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, long nr,
3d45f221 3250 bool in_reclaim_context);
c2566f22 3251int btrfs_set_extent_delalloc(struct btrfs_inode *inode, u64 start, u64 end,
e3b8a485 3252 unsigned int extra_bits,
330a5827 3253 struct extent_state **cached_state);
d2fb3437 3254int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
63541927 3255 struct btrfs_root *new_root,
4d4340c9
CB
3256 struct btrfs_root *parent_root,
3257 struct user_namespace *mnt_userns);
c629732d 3258 void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state,
e06a1fc9 3259 unsigned *bits);
a36bb5f9
NB
3260void btrfs_clear_delalloc_extent(struct inode *inode,
3261 struct extent_state *state, unsigned *bits);
5c848198
NB
3262void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new,
3263 struct extent_state *other);
abbb55f4
NB
3264void btrfs_split_delalloc_extent(struct inode *inode,
3265 struct extent_state *orig, u64 split);
d2a91064 3266void btrfs_set_range_writeback(struct btrfs_inode *inode, u64 start, u64 end);
a528a241 3267vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf);
9ebefb18 3268int btrfs_readpage(struct file *file, struct page *page);
bd555975 3269void btrfs_evict_inode(struct inode *inode);
a9185b41 3270int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc);
39279cc3
CM
3271struct inode *btrfs_alloc_inode(struct super_block *sb);
3272void btrfs_destroy_inode(struct inode *inode);
26602cab 3273void btrfs_free_inode(struct inode *inode);
45321ac5 3274int btrfs_drop_inode(struct inode *inode);
f5c29bd9 3275int __init btrfs_init_cachep(void);
e67c718b 3276void __cold btrfs_destroy_cachep(void);
0202e83f 3277struct inode *btrfs_iget_path(struct super_block *s, u64 ino,
4c66e0d4 3278 struct btrfs_root *root, struct btrfs_path *path);
0202e83f 3279struct inode *btrfs_iget(struct super_block *s, u64 ino, struct btrfs_root *root);
fc4f21b1 3280struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
de2c6615 3281 struct page *page, size_t pg_offset,
39b07b5d 3282 u64 start, u64 end);
a52d9a80 3283int btrfs_update_inode(struct btrfs_trans_handle *trans,
9a56fcd1 3284 struct btrfs_root *root, struct btrfs_inode *inode);
be6aef60 3285int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
729f7961 3286 struct btrfs_root *root, struct btrfs_inode *inode);
73f2e545
NB
3287int btrfs_orphan_add(struct btrfs_trans_handle *trans,
3288 struct btrfs_inode *inode);
66b4ffd1 3289int btrfs_orphan_cleanup(struct btrfs_root *root);
b06359a3 3290int btrfs_cont_expand(struct btrfs_inode *inode, loff_t oldsize, loff_t size);
24bbcf04 3291void btrfs_add_delayed_iput(struct inode *inode);
2ff7e61e 3292void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info);
034f784d 3293int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info);
efa56464
YZ
3294int btrfs_prealloc_file_range(struct inode *inode, int mode,
3295 u64 start, u64 num_bytes, u64 min_size,
3296 loff_t actual_len, u64 *alloc_hint);
0af3d00b
JB
3297int btrfs_prealloc_file_range_trans(struct inode *inode,
3298 struct btrfs_trans_handle *trans, int mode,
3299 u64 start, u64 num_bytes, u64 min_size,
3300 loff_t actual_len, u64 *alloc_hint);
98456b9c 3301int btrfs_run_delalloc_range(struct btrfs_inode *inode, struct page *locked_page,
5eaad97a
NB
3302 u64 start, u64 end, int *page_started, unsigned long *nr_written,
3303 struct writeback_control *wbc);
a129ffb8 3304int btrfs_writepage_cow_fixup(struct page *page);
38a39ac7
QW
3305void btrfs_writepage_endio_finish_ordered(struct btrfs_inode *inode,
3306 struct page *page, u64 start,
25c1252a 3307 u64 end, bool uptodate);
82d339d9 3308extern const struct dentry_operations btrfs_dentry_operations;
4e4cabec
GR
3309extern const struct iomap_ops btrfs_dio_iomap_ops;
3310extern const struct iomap_dio_ops btrfs_dio_ops;
f46b5a66 3311
a14b78ad
GR
3312/* Inode locking type flags, by default the exclusive lock is taken */
3313#define BTRFS_ILOCK_SHARED (1U << 0)
3314#define BTRFS_ILOCK_TRY (1U << 1)
8318ba79 3315#define BTRFS_ILOCK_MMAP (1U << 2)
a14b78ad
GR
3316
3317int btrfs_inode_lock(struct inode *inode, unsigned int ilock_flags);
3318void btrfs_inode_unlock(struct inode *inode, unsigned int ilock_flags);
2766ff61
FM
3319void btrfs_update_inode_bytes(struct btrfs_inode *inode,
3320 const u64 add_bytes,
3321 const u64 del_bytes);
f46b5a66
CH
3322
3323/* ioctl.c */
3324long btrfs_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
4c63c245 3325long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
97fc2977
MS
3326int btrfs_fileattr_get(struct dentry *dentry, struct fileattr *fa);
3327int btrfs_fileattr_set(struct user_namespace *mnt_userns,
3328 struct dentry *dentry, struct fileattr *fa);
d5131b65 3329int btrfs_ioctl_get_supported_features(void __user *arg);
7b6a221e 3330void btrfs_sync_inode_flags_to_i_flags(struct inode *inode);
e1f60a65 3331int __pure btrfs_is_empty_uuid(u8 *uuid);
1ccc2e8a 3332int btrfs_defrag_file(struct inode *inode, struct file_ra_state *ra,
4cb5300b 3333 struct btrfs_ioctl_defrag_range_args *range,
1ccc2e8a 3334 u64 newer_than, unsigned long max_to_defrag);
008ef096
DS
3335void btrfs_get_block_group_info(struct list_head *groups_list,
3336 struct btrfs_ioctl_space_info *space);
3337void btrfs_update_ioctl_balance_args(struct btrfs_fs_info *fs_info,
35a3621b 3338 struct btrfs_ioctl_balance_args *bargs);
c3e1f96c
GR
3339bool btrfs_exclop_start(struct btrfs_fs_info *fs_info,
3340 enum btrfs_exclusive_operation type);
578bda9e
DS
3341bool btrfs_exclop_start_try_lock(struct btrfs_fs_info *fs_info,
3342 enum btrfs_exclusive_operation type);
3343void btrfs_exclop_start_unlock(struct btrfs_fs_info *fs_info);
c3e1f96c 3344void btrfs_exclop_finish(struct btrfs_fs_info *fs_info);
efc0e69c
NB
3345void btrfs_exclop_balance(struct btrfs_fs_info *fs_info,
3346 enum btrfs_exclusive_operation op);
3347
35a3621b 3348
39279cc3 3349/* file.c */
f5c29bd9 3350int __init btrfs_auto_defrag_init(void);
e67c718b 3351void __cold btrfs_auto_defrag_exit(void);
4cb5300b 3352int btrfs_add_inode_defrag(struct btrfs_trans_handle *trans,
558732df 3353 struct btrfs_inode *inode, u32 extent_thresh);
4cb5300b 3354int btrfs_run_defrag_inodes(struct btrfs_fs_info *fs_info);
26176e7c 3355void btrfs_cleanup_defrag_inodes(struct btrfs_fs_info *fs_info);
02c24a82 3356int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync);
dcdbc059 3357void btrfs_drop_extent_cache(struct btrfs_inode *inode, u64 start, u64 end,
7014cdb4 3358 int skip_pinned);
828c0950 3359extern const struct file_operations btrfs_file_operations;
5dc562c5 3360int btrfs_drop_extents(struct btrfs_trans_handle *trans,
5893dfb9
FM
3361 struct btrfs_root *root, struct btrfs_inode *inode,
3362 struct btrfs_drop_extents_args *args);
bfc78479
NB
3363int btrfs_replace_file_extents(struct btrfs_inode *inode,
3364 struct btrfs_path *path, const u64 start,
3365 const u64 end,
bf385648 3366 struct btrfs_replace_extent_info *extent_info,
690a5dbf 3367 struct btrfs_trans_handle **trans_out);
d899e052 3368int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
7a6d7067 3369 struct btrfs_inode *inode, u64 start, u64 end);
6bf13c0c 3370int btrfs_release_file(struct inode *inode, struct file *file);
088545f6 3371int btrfs_dirty_pages(struct btrfs_inode *inode, struct page **pages,
2ff7e61e 3372 size_t num_pages, loff_t pos, size_t write_bytes,
aa8c1a41 3373 struct extent_state **cached, bool noreserve);
728404da 3374int btrfs_fdatawrite_range(struct inode *inode, loff_t start, loff_t end);
38d37aa9
QW
3375int btrfs_check_nocow_lock(struct btrfs_inode *inode, loff_t pos,
3376 size_t *write_bytes);
3377void btrfs_check_nocow_unlock(struct btrfs_inode *inode);
6bf13c0c 3378
6702ed49
CM
3379/* tree-defrag.c */
3380int btrfs_defrag_leaves(struct btrfs_trans_handle *trans,
de78b51a 3381 struct btrfs_root *root);
58176a96 3382
edbd8d4e 3383/* super.c */
2ff7e61e 3384int btrfs_parse_options(struct btrfs_fs_info *info, char *options,
96da0919 3385 unsigned long new_flags);
6bf13c0c 3386int btrfs_sync_fs(struct super_block *sb, int wait);
c0c907a4
MPS
3387char *btrfs_get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info,
3388 u64 subvol_objectid);
533574c6 3389
e67c718b 3390static inline __printf(2, 3) __cold
2fd57fcb
AB
3391void btrfs_no_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...)
3392{
3393}
3394
533574c6
JP
3395#ifdef CONFIG_PRINTK
3396__printf(2, 3)
e67c718b 3397__cold
c2cf52eb 3398void btrfs_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...);
533574c6 3399#else
2fd57fcb
AB
3400#define btrfs_printk(fs_info, fmt, args...) \
3401 btrfs_no_printk(fs_info, fmt, ##args)
533574c6
JP
3402#endif
3403
c2cf52eb
SK
3404#define btrfs_emerg(fs_info, fmt, args...) \
3405 btrfs_printk(fs_info, KERN_EMERG fmt, ##args)
3406#define btrfs_alert(fs_info, fmt, args...) \
3407 btrfs_printk(fs_info, KERN_ALERT fmt, ##args)
3408#define btrfs_crit(fs_info, fmt, args...) \
3409 btrfs_printk(fs_info, KERN_CRIT fmt, ##args)
3410#define btrfs_err(fs_info, fmt, args...) \
3411 btrfs_printk(fs_info, KERN_ERR fmt, ##args)
3412#define btrfs_warn(fs_info, fmt, args...) \
3413 btrfs_printk(fs_info, KERN_WARNING fmt, ##args)
3414#define btrfs_notice(fs_info, fmt, args...) \
3415 btrfs_printk(fs_info, KERN_NOTICE fmt, ##args)
3416#define btrfs_info(fs_info, fmt, args...) \
3417 btrfs_printk(fs_info, KERN_INFO fmt, ##args)
27a0dd61 3418
08a84e25
DS
3419/*
3420 * Wrappers that use printk_in_rcu
3421 */
3422#define btrfs_emerg_in_rcu(fs_info, fmt, args...) \
3423 btrfs_printk_in_rcu(fs_info, KERN_EMERG fmt, ##args)
3424#define btrfs_alert_in_rcu(fs_info, fmt, args...) \
3425 btrfs_printk_in_rcu(fs_info, KERN_ALERT fmt, ##args)
3426#define btrfs_crit_in_rcu(fs_info, fmt, args...) \
3427 btrfs_printk_in_rcu(fs_info, KERN_CRIT fmt, ##args)
3428#define btrfs_err_in_rcu(fs_info, fmt, args...) \
3429 btrfs_printk_in_rcu(fs_info, KERN_ERR fmt, ##args)
3430#define btrfs_warn_in_rcu(fs_info, fmt, args...) \
3431 btrfs_printk_in_rcu(fs_info, KERN_WARNING fmt, ##args)
3432#define btrfs_notice_in_rcu(fs_info, fmt, args...) \
3433 btrfs_printk_in_rcu(fs_info, KERN_NOTICE fmt, ##args)
3434#define btrfs_info_in_rcu(fs_info, fmt, args...) \
3435 btrfs_printk_in_rcu(fs_info, KERN_INFO fmt, ##args)
3436
24aa6b41
DS
3437/*
3438 * Wrappers that use a ratelimited printk_in_rcu
3439 */
3440#define btrfs_emerg_rl_in_rcu(fs_info, fmt, args...) \
3441 btrfs_printk_rl_in_rcu(fs_info, KERN_EMERG fmt, ##args)
3442#define btrfs_alert_rl_in_rcu(fs_info, fmt, args...) \
3443 btrfs_printk_rl_in_rcu(fs_info, KERN_ALERT fmt, ##args)
3444#define btrfs_crit_rl_in_rcu(fs_info, fmt, args...) \
3445 btrfs_printk_rl_in_rcu(fs_info, KERN_CRIT fmt, ##args)
3446#define btrfs_err_rl_in_rcu(fs_info, fmt, args...) \
3447 btrfs_printk_rl_in_rcu(fs_info, KERN_ERR fmt, ##args)
3448#define btrfs_warn_rl_in_rcu(fs_info, fmt, args...) \
3449 btrfs_printk_rl_in_rcu(fs_info, KERN_WARNING fmt, ##args)
3450#define btrfs_notice_rl_in_rcu(fs_info, fmt, args...) \
3451 btrfs_printk_rl_in_rcu(fs_info, KERN_NOTICE fmt, ##args)
3452#define btrfs_info_rl_in_rcu(fs_info, fmt, args...) \
3453 btrfs_printk_rl_in_rcu(fs_info, KERN_INFO fmt, ##args)
3454
1dd6d7ca
DS
3455/*
3456 * Wrappers that use a ratelimited printk
3457 */
3458#define btrfs_emerg_rl(fs_info, fmt, args...) \
3459 btrfs_printk_ratelimited(fs_info, KERN_EMERG fmt, ##args)
3460#define btrfs_alert_rl(fs_info, fmt, args...) \
3461 btrfs_printk_ratelimited(fs_info, KERN_ALERT fmt, ##args)
3462#define btrfs_crit_rl(fs_info, fmt, args...) \
3463 btrfs_printk_ratelimited(fs_info, KERN_CRIT fmt, ##args)
3464#define btrfs_err_rl(fs_info, fmt, args...) \
3465 btrfs_printk_ratelimited(fs_info, KERN_ERR fmt, ##args)
3466#define btrfs_warn_rl(fs_info, fmt, args...) \
3467 btrfs_printk_ratelimited(fs_info, KERN_WARNING fmt, ##args)
3468#define btrfs_notice_rl(fs_info, fmt, args...) \
3469 btrfs_printk_ratelimited(fs_info, KERN_NOTICE fmt, ##args)
3470#define btrfs_info_rl(fs_info, fmt, args...) \
3471 btrfs_printk_ratelimited(fs_info, KERN_INFO fmt, ##args)
897a41b1
JM
3472
3473#if defined(CONFIG_DYNAMIC_DEBUG)
3474#define btrfs_debug(fs_info, fmt, args...) \
afe1a715
RV
3475 _dynamic_func_call_no_desc(fmt, btrfs_printk, \
3476 fs_info, KERN_DEBUG fmt, ##args)
3477#define btrfs_debug_in_rcu(fs_info, fmt, args...) \
3478 _dynamic_func_call_no_desc(fmt, btrfs_printk_in_rcu, \
3479 fs_info, KERN_DEBUG fmt, ##args)
897a41b1 3480#define btrfs_debug_rl_in_rcu(fs_info, fmt, args...) \
afe1a715
RV
3481 _dynamic_func_call_no_desc(fmt, btrfs_printk_rl_in_rcu, \
3482 fs_info, KERN_DEBUG fmt, ##args)
3483#define btrfs_debug_rl(fs_info, fmt, args...) \
3484 _dynamic_func_call_no_desc(fmt, btrfs_printk_ratelimited, \
3485 fs_info, KERN_DEBUG fmt, ##args)
897a41b1 3486#elif defined(DEBUG)
c2cf52eb
SK
3487#define btrfs_debug(fs_info, fmt, args...) \
3488 btrfs_printk(fs_info, KERN_DEBUG fmt, ##args)
08a84e25
DS
3489#define btrfs_debug_in_rcu(fs_info, fmt, args...) \
3490 btrfs_printk_in_rcu(fs_info, KERN_DEBUG fmt, ##args)
24aa6b41
DS
3491#define btrfs_debug_rl_in_rcu(fs_info, fmt, args...) \
3492 btrfs_printk_rl_in_rcu(fs_info, KERN_DEBUG fmt, ##args)
1dd6d7ca
DS
3493#define btrfs_debug_rl(fs_info, fmt, args...) \
3494 btrfs_printk_ratelimited(fs_info, KERN_DEBUG fmt, ##args)
27a0dd61
FH
3495#else
3496#define btrfs_debug(fs_info, fmt, args...) \
c01f5f96 3497 btrfs_no_printk(fs_info, KERN_DEBUG fmt, ##args)
08a84e25 3498#define btrfs_debug_in_rcu(fs_info, fmt, args...) \
b6fdfbff 3499 btrfs_no_printk_in_rcu(fs_info, KERN_DEBUG fmt, ##args)
24aa6b41 3500#define btrfs_debug_rl_in_rcu(fs_info, fmt, args...) \
b6fdfbff 3501 btrfs_no_printk_in_rcu(fs_info, KERN_DEBUG fmt, ##args)
1dd6d7ca 3502#define btrfs_debug_rl(fs_info, fmt, args...) \
c01f5f96 3503 btrfs_no_printk(fs_info, KERN_DEBUG fmt, ##args)
27a0dd61 3504#endif
c2cf52eb 3505
08a84e25
DS
3506#define btrfs_printk_in_rcu(fs_info, fmt, args...) \
3507do { \
3508 rcu_read_lock(); \
3509 btrfs_printk(fs_info, fmt, ##args); \
b6fdfbff
MT
3510 rcu_read_unlock(); \
3511} while (0)
3512
3513#define btrfs_no_printk_in_rcu(fs_info, fmt, args...) \
3514do { \
3515 rcu_read_lock(); \
3516 btrfs_no_printk(fs_info, fmt, ##args); \
08a84e25
DS
3517 rcu_read_unlock(); \
3518} while (0)
3519
24aa6b41
DS
3520#define btrfs_printk_ratelimited(fs_info, fmt, args...) \
3521do { \
3522 static DEFINE_RATELIMIT_STATE(_rs, \
3523 DEFAULT_RATELIMIT_INTERVAL, \
3524 DEFAULT_RATELIMIT_BURST); \
3525 if (__ratelimit(&_rs)) \
3526 btrfs_printk(fs_info, fmt, ##args); \
3527} while (0)
3528
3529#define btrfs_printk_rl_in_rcu(fs_info, fmt, args...) \
3530do { \
3531 rcu_read_lock(); \
3532 btrfs_printk_ratelimited(fs_info, fmt, ##args); \
3533 rcu_read_unlock(); \
3534} while (0)
3535
68c467cb
DS
3536#ifdef CONFIG_BTRFS_ASSERT
3537__cold __noreturn
3538static inline void assertfail(const char *expr, const char *file, int line)
2e17c7c6 3539{
68c467cb
DS
3540 pr_err("assertion failed: %s, in %s:%d\n", expr, file, line);
3541 BUG();
2e17c7c6
JB
3542}
3543
68c467cb
DS
3544#define ASSERT(expr) \
3545 (likely(expr) ? (void)0 : assertfail(#expr, __FILE__, __LINE__))
3546
3547#else
3548static inline void assertfail(const char *expr, const char* file, int line) { }
3549#define ASSERT(expr) (void)(expr)
3550#endif
2e17c7c6 3551
e9306ad4
QW
3552#if BITS_PER_LONG == 32
3553#define BTRFS_32BIT_MAX_FILE_SIZE (((u64)ULONG_MAX + 1) << PAGE_SHIFT)
3554/*
3555 * The warning threshold is 5/8th of the MAX_LFS_FILESIZE that limits the logical
3556 * addresses of extents.
3557 *
3558 * For 4K page size it's about 10T, for 64K it's 160T.
3559 */
3560#define BTRFS_32BIT_EARLY_WARN_THRESHOLD (BTRFS_32BIT_MAX_FILE_SIZE * 5 / 8)
3561void btrfs_warn_32bit_limit(struct btrfs_fs_info *fs_info);
3562void btrfs_err_32bit_limit(struct btrfs_fs_info *fs_info);
3563#endif
3564
884b07d0
QW
3565/*
3566 * Get the correct offset inside the page of extent buffer.
3567 *
3568 * @eb: target extent buffer
3569 * @start: offset inside the extent buffer
3570 *
3571 * Will handle both sectorsize == PAGE_SIZE and sectorsize < PAGE_SIZE cases.
3572 */
3573static inline size_t get_eb_offset_in_page(const struct extent_buffer *eb,
3574 unsigned long offset)
3575{
3576 /*
3577 * For sectorsize == PAGE_SIZE case, eb->start will always be aligned
3578 * to PAGE_SIZE, thus adding it won't cause any difference.
3579 *
3580 * For sectorsize < PAGE_SIZE, we must only read the data that belongs
3581 * to the eb, thus we have to take the eb->start into consideration.
3582 */
3583 return offset_in_page(offset + eb->start);
3584}
3585
3586static inline unsigned long get_eb_page_index(unsigned long offset)
3587{
3588 /*
3589 * For sectorsize == PAGE_SIZE case, plain >> PAGE_SHIFT is enough.
3590 *
3591 * For sectorsize < PAGE_SIZE case, we only support 64K PAGE_SIZE,
3592 * and have ensured that all tree blocks are contained in one page,
3593 * thus we always get index == 0.
3594 */
3595 return offset >> PAGE_SHIFT;
3596}
3597
f8f591df
JT
3598/*
3599 * Use that for functions that are conditionally exported for sanity tests but
3600 * otherwise static
3601 */
3602#ifndef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
3603#define EXPORT_FOR_TESTS static
3604#else
3605#define EXPORT_FOR_TESTS
3606#endif
3607
ba3c2b19
NB
3608__cold
3609static inline void btrfs_print_v0_err(struct btrfs_fs_info *fs_info)
3610{
3611 btrfs_err(fs_info,
3612"Unsupported V0 extent filesystem detected. Aborting. Please re-create your filesystem with a newer kernel");
3613}
3614
533574c6 3615__printf(5, 6)
c0d19e2b 3616__cold
34d97007 3617void __btrfs_handle_fs_error(struct btrfs_fs_info *fs_info, const char *function,
4da35113 3618 unsigned int line, int errno, const char *fmt, ...);
acce952b 3619
4143cb8b 3620const char * __attribute_const__ btrfs_decode_error(int errno);
533574c6 3621
c0d19e2b 3622__cold
49b25e05 3623void __btrfs_abort_transaction(struct btrfs_trans_handle *trans,
66642832 3624 const char *function,
49b25e05
JM
3625 unsigned int line, int errno);
3626
c5f4ccb2
AJ
3627/*
3628 * Call btrfs_abort_transaction as early as possible when an error condition is
3629 * detected, that way the exact line number is reported.
3630 */
66642832 3631#define btrfs_abort_transaction(trans, errno) \
c5f4ccb2
AJ
3632do { \
3633 /* Report first abort since mount */ \
3634 if (!test_and_set_bit(BTRFS_FS_STATE_TRANS_ABORTED, \
66642832 3635 &((trans)->fs_info->fs_state))) { \
f95ebdbe 3636 if ((errno) != -EIO && (errno) != -EROFS) { \
e5d6b12f
CM
3637 WARN(1, KERN_DEBUG \
3638 "BTRFS: Transaction aborted (error %d)\n", \
3639 (errno)); \
3640 } else { \
71367b3f
JM
3641 btrfs_debug((trans)->fs_info, \
3642 "Transaction aborted (error %d)", \
e5d6b12f
CM
3643 (errno)); \
3644 } \
c5f4ccb2 3645 } \
66642832 3646 __btrfs_abort_transaction((trans), __func__, \
c5f4ccb2
AJ
3647 __LINE__, (errno)); \
3648} while (0)
3649
3650#define btrfs_handle_fs_error(fs_info, errno, fmt, args...) \
3651do { \
3652 __btrfs_handle_fs_error((fs_info), __func__, __LINE__, \
3653 (errno), fmt, ##args); \
3654} while (0)
3655
84961539
JB
3656#define BTRFS_FS_ERROR(fs_info) (unlikely(test_bit(BTRFS_FS_STATE_ERROR, \
3657 &(fs_info)->fs_state)))
40cdc509
FM
3658#define BTRFS_FS_LOG_CLEANUP_ERROR(fs_info) \
3659 (unlikely(test_bit(BTRFS_FS_STATE_LOG_CLEANUP_ERROR, \
3660 &(fs_info)->fs_state)))
84961539 3661
c5f4ccb2
AJ
3662__printf(5, 6)
3663__cold
3664void __btrfs_panic(struct btrfs_fs_info *fs_info, const char *function,
3665 unsigned int line, int errno, const char *fmt, ...);
3666/*
3667 * If BTRFS_MOUNT_PANIC_ON_FATAL_ERROR is in mount_opt, __btrfs_panic
3668 * will panic(). Otherwise we BUG() here.
3669 */
3670#define btrfs_panic(fs_info, errno, fmt, args...) \
3671do { \
3672 __btrfs_panic(fs_info, __func__, __LINE__, errno, fmt, ##args); \
3673 BUG(); \
3674} while (0)
3675
3676
3677/* compatibility and incompatibility defines */
3678
2b0ce2c2 3679#define btrfs_set_fs_incompat(__fs_info, opt) \
c9d713d5
DS
3680 __btrfs_set_fs_incompat((__fs_info), BTRFS_FEATURE_INCOMPAT_##opt, \
3681 #opt)
2b0ce2c2
MH
3682
3683static inline void __btrfs_set_fs_incompat(struct btrfs_fs_info *fs_info,
c9d713d5 3684 u64 flag, const char* name)
2b0ce2c2
MH
3685{
3686 struct btrfs_super_block *disk_super;
3687 u64 features;
3688
3689 disk_super = fs_info->super_copy;
3690 features = btrfs_super_incompat_flags(disk_super);
3691 if (!(features & flag)) {
ceda0864
MX
3692 spin_lock(&fs_info->super_lock);
3693 features = btrfs_super_incompat_flags(disk_super);
3694 if (!(features & flag)) {
3695 features |= flag;
3696 btrfs_set_super_incompat_flags(disk_super, features);
c9d713d5
DS
3697 btrfs_info(fs_info,
3698 "setting incompat feature flag for %s (0x%llx)",
3699 name, flag);
ceda0864
MX
3700 }
3701 spin_unlock(&fs_info->super_lock);
2b0ce2c2
MH
3702 }
3703}
3704
1abfbcdf 3705#define btrfs_clear_fs_incompat(__fs_info, opt) \
c9d713d5
DS
3706 __btrfs_clear_fs_incompat((__fs_info), BTRFS_FEATURE_INCOMPAT_##opt, \
3707 #opt)
1abfbcdf
OS
3708
3709static inline void __btrfs_clear_fs_incompat(struct btrfs_fs_info *fs_info,
c9d713d5 3710 u64 flag, const char* name)
1abfbcdf
OS
3711{
3712 struct btrfs_super_block *disk_super;
3713 u64 features;
3714
3715 disk_super = fs_info->super_copy;
3716 features = btrfs_super_incompat_flags(disk_super);
3717 if (features & flag) {
3718 spin_lock(&fs_info->super_lock);
3719 features = btrfs_super_incompat_flags(disk_super);
3720 if (features & flag) {
3721 features &= ~flag;
3722 btrfs_set_super_incompat_flags(disk_super, features);
c9d713d5
DS
3723 btrfs_info(fs_info,
3724 "clearing incompat feature flag for %s (0x%llx)",
3725 name, flag);
1abfbcdf
OS
3726 }
3727 spin_unlock(&fs_info->super_lock);
3728 }
3729}
3730
3173a18f
JB
3731#define btrfs_fs_incompat(fs_info, opt) \
3732 __btrfs_fs_incompat((fs_info), BTRFS_FEATURE_INCOMPAT_##opt)
3733
9780c497 3734static inline bool __btrfs_fs_incompat(struct btrfs_fs_info *fs_info, u64 flag)
3173a18f
JB
3735{
3736 struct btrfs_super_block *disk_super;
3737 disk_super = fs_info->super_copy;
3738 return !!(btrfs_super_incompat_flags(disk_super) & flag);
3739}
3740
1abfbcdf 3741#define btrfs_set_fs_compat_ro(__fs_info, opt) \
c9d713d5
DS
3742 __btrfs_set_fs_compat_ro((__fs_info), BTRFS_FEATURE_COMPAT_RO_##opt, \
3743 #opt)
1abfbcdf
OS
3744
3745static inline void __btrfs_set_fs_compat_ro(struct btrfs_fs_info *fs_info,
c9d713d5 3746 u64 flag, const char *name)
1abfbcdf
OS
3747{
3748 struct btrfs_super_block *disk_super;
3749 u64 features;
3750
3751 disk_super = fs_info->super_copy;
3752 features = btrfs_super_compat_ro_flags(disk_super);
3753 if (!(features & flag)) {
3754 spin_lock(&fs_info->super_lock);
3755 features = btrfs_super_compat_ro_flags(disk_super);
3756 if (!(features & flag)) {
3757 features |= flag;
3758 btrfs_set_super_compat_ro_flags(disk_super, features);
c9d713d5
DS
3759 btrfs_info(fs_info,
3760 "setting compat-ro feature flag for %s (0x%llx)",
3761 name, flag);
1abfbcdf
OS
3762 }
3763 spin_unlock(&fs_info->super_lock);
3764 }
3765}
3766
3767#define btrfs_clear_fs_compat_ro(__fs_info, opt) \
c9d713d5
DS
3768 __btrfs_clear_fs_compat_ro((__fs_info), BTRFS_FEATURE_COMPAT_RO_##opt, \
3769 #opt)
1abfbcdf
OS
3770
3771static inline void __btrfs_clear_fs_compat_ro(struct btrfs_fs_info *fs_info,
c9d713d5 3772 u64 flag, const char *name)
1abfbcdf
OS
3773{
3774 struct btrfs_super_block *disk_super;
3775 u64 features;
3776
3777 disk_super = fs_info->super_copy;
3778 features = btrfs_super_compat_ro_flags(disk_super);
3779 if (features & flag) {
3780 spin_lock(&fs_info->super_lock);
3781 features = btrfs_super_compat_ro_flags(disk_super);
3782 if (features & flag) {
3783 features &= ~flag;
3784 btrfs_set_super_compat_ro_flags(disk_super, features);
c9d713d5
DS
3785 btrfs_info(fs_info,
3786 "clearing compat-ro feature flag for %s (0x%llx)",
3787 name, flag);
1abfbcdf
OS
3788 }
3789 spin_unlock(&fs_info->super_lock);
3790 }
3791}
3792
3793#define btrfs_fs_compat_ro(fs_info, opt) \
3794 __btrfs_fs_compat_ro((fs_info), BTRFS_FEATURE_COMPAT_RO_##opt)
3795
3796static inline int __btrfs_fs_compat_ro(struct btrfs_fs_info *fs_info, u64 flag)
3797{
3798 struct btrfs_super_block *disk_super;
3799 disk_super = fs_info->super_copy;
3800 return !!(btrfs_super_compat_ro_flags(disk_super) & flag);
3801}
3802
33268eaf 3803/* acl.c */
0eda294d 3804#ifdef CONFIG_BTRFS_FS_POSIX_ACL
0cad6246 3805struct posix_acl *btrfs_get_acl(struct inode *inode, int type, bool rcu);
549c7297
CB
3806int btrfs_set_acl(struct user_namespace *mnt_userns, struct inode *inode,
3807 struct posix_acl *acl, int type);
f34f57a3
YZ
3808int btrfs_init_acl(struct btrfs_trans_handle *trans,
3809 struct inode *inode, struct inode *dir);
9b89d95a 3810#else
ed8f3737 3811#define btrfs_get_acl NULL
996a710d 3812#define btrfs_set_acl NULL
9b89d95a
LZ
3813static inline int btrfs_init_acl(struct btrfs_trans_handle *trans,
3814 struct inode *inode, struct inode *dir)
3815{
3816 return 0;
3817}
9b89d95a 3818#endif
0f9dd46c 3819
5d4f98a2 3820/* relocation.c */
6bccf3ab 3821int btrfs_relocate_block_group(struct btrfs_fs_info *fs_info, u64 group_start);
5d4f98a2
YZ
3822int btrfs_init_reloc_root(struct btrfs_trans_handle *trans,
3823 struct btrfs_root *root);
3824int btrfs_update_reloc_root(struct btrfs_trans_handle *trans,
3825 struct btrfs_root *root);
3826int btrfs_recover_relocation(struct btrfs_root *root);
7bfa9535 3827int btrfs_reloc_clone_csums(struct btrfs_inode *inode, u64 file_pos, u64 len);
83d4cfd4
JB
3828int btrfs_reloc_cow_block(struct btrfs_trans_handle *trans,
3829 struct btrfs_root *root, struct extent_buffer *buf,
3830 struct extent_buffer *cow);
147d256e 3831void btrfs_reloc_pre_snapshot(struct btrfs_pending_snapshot *pending,
3fd0a558 3832 u64 *bytes_to_reserve);
49b25e05 3833int btrfs_reloc_post_snapshot(struct btrfs_trans_handle *trans,
3fd0a558 3834 struct btrfs_pending_snapshot *pending);
726a3421 3835int btrfs_should_cancel_balance(struct btrfs_fs_info *fs_info);
2433bea5
QW
3836struct btrfs_root *find_reloc_root(struct btrfs_fs_info *fs_info,
3837 u64 bytenr);
55465730 3838int btrfs_should_ignore_reloc_root(struct btrfs_root *root);
a2de733c
AJ
3839
3840/* scrub.c */
aa1b8cd4
SB
3841int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
3842 u64 end, struct btrfs_scrub_progress *progress,
63a212ab 3843 int readonly, int is_dev_replace);
2ff7e61e
JM
3844void btrfs_scrub_pause(struct btrfs_fs_info *fs_info);
3845void btrfs_scrub_continue(struct btrfs_fs_info *fs_info);
aa1b8cd4 3846int btrfs_scrub_cancel(struct btrfs_fs_info *info);
163e97ee 3847int btrfs_scrub_cancel_dev(struct btrfs_device *dev);
2ff7e61e 3848int btrfs_scrub_progress(struct btrfs_fs_info *fs_info, u64 devid,
a2de733c 3849 struct btrfs_scrub_progress *progress);
0966a7b1
QW
3850static inline void btrfs_init_full_stripe_locks_tree(
3851 struct btrfs_full_stripe_locks_tree *locks_root)
3852{
3853 locks_root->root = RB_ROOT;
3854 mutex_init(&locks_root->lock);
3855}
c404e0dc
MX
3856
3857/* dev-replace.c */
3858void btrfs_bio_counter_inc_blocked(struct btrfs_fs_info *fs_info);
3859void btrfs_bio_counter_inc_noblocked(struct btrfs_fs_info *fs_info);
4245215d
MX
3860void btrfs_bio_counter_sub(struct btrfs_fs_info *fs_info, s64 amount);
3861
3862static inline void btrfs_bio_counter_dec(struct btrfs_fs_info *fs_info)
3863{
3864 btrfs_bio_counter_sub(fs_info, 1);
3865}
a2de733c 3866
95a06077
JS
3867static inline int is_fstree(u64 rootid)
3868{
3869 if (rootid == BTRFS_FS_TREE_OBJECTID ||
e09fe2d2
QW
3870 ((s64)rootid >= (s64)BTRFS_FIRST_FREE_OBJECTID &&
3871 !btrfs_qgroup_level(rootid)))
95a06077
JS
3872 return 1;
3873 return 0;
3874}
210549eb
DS
3875
3876static inline int btrfs_defrag_cancelled(struct btrfs_fs_info *fs_info)
3877{
3878 return signal_pending(current);
3879}
3880
14605409
BB
3881/* verity.c */
3882#ifdef CONFIG_FS_VERITY
3883
3884extern const struct fsverity_operations btrfs_verityops;
3885int btrfs_drop_verity_items(struct btrfs_inode *inode);
3886
3887BTRFS_SETGET_FUNCS(verity_descriptor_encryption, struct btrfs_verity_descriptor_item,
3888 encryption, 8);
3889BTRFS_SETGET_FUNCS(verity_descriptor_size, struct btrfs_verity_descriptor_item,
3890 size, 64);
3891BTRFS_SETGET_STACK_FUNCS(stack_verity_descriptor_encryption,
3892 struct btrfs_verity_descriptor_item, encryption, 8);
3893BTRFS_SETGET_STACK_FUNCS(stack_verity_descriptor_size,
3894 struct btrfs_verity_descriptor_item, size, 64);
3895
3896#else
3897
3898static inline int btrfs_drop_verity_items(struct btrfs_inode *inode)
3899{
3900 return 0;
3901}
3902
3903#endif
3904
aaedb55b
JB
3905/* Sanity test specific functions */
3906#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
3907void btrfs_test_destroy_inode(struct inode *inode);
f5ee5c9a 3908static inline int btrfs_is_testing(struct btrfs_fs_info *fs_info)
fccb84c9 3909{
b2fa1154
DS
3910 return test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state);
3911}
3912#else
3913static inline int btrfs_is_testing(struct btrfs_fs_info *fs_info)
3914{
fccb84c9
DS
3915 return 0;
3916}
b2fa1154 3917#endif
9888c340 3918
b70f5097
NA
3919static inline bool btrfs_is_zoned(const struct btrfs_fs_info *fs_info)
3920{
3921 return fs_info->zoned != 0;
3922}
3923
37f00a6d
JT
3924static inline bool btrfs_is_data_reloc_root(const struct btrfs_root *root)
3925{
3926 return root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID;
3927}
3928
f57ad937
QW
3929/*
3930 * We use page status Private2 to indicate there is an ordered extent with
3931 * unfinished IO.
3932 *
3933 * Rename the Private2 accessors to Ordered, to improve readability.
3934 */
3935#define PageOrdered(page) PagePrivate2(page)
3936#define SetPageOrdered(page) SetPagePrivate2(page)
3937#define ClearPageOrdered(page) ClearPagePrivate2(page)
3938
eb60ceac 3939#endif