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