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