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