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