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