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