xref: /linux/fs/btrfs/fs.h (revision 400544639d2a11a9c1e276a912a9dff8fe4107dc)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 
3 #ifndef BTRFS_FS_H
4 #define BTRFS_FS_H
5 
6 #include <crypto/blake2b.h>
7 #include <crypto/sha2.h>
8 #include <linux/blkdev.h>
9 #include <linux/sizes.h>
10 #include <linux/time64.h>
11 #include <linux/compiler.h>
12 #include <linux/math.h>
13 #include <linux/atomic.h>
14 #include <linux/percpu_counter.h>
15 #include <linux/completion.h>
16 #include <linux/lockdep.h>
17 #include <linux/spinlock.h>
18 #include <linux/mutex.h>
19 #include <linux/rwsem.h>
20 #include <linux/semaphore.h>
21 #include <linux/list.h>
22 #include <linux/pagemap.h>
23 #include <linux/radix-tree.h>
24 #include <linux/workqueue.h>
25 #include <linux/wait.h>
26 #include <linux/wait_bit.h>
27 #include <linux/sched.h>
28 #include <linux/rbtree.h>
29 #include <linux/xxhash.h>
30 #include <linux/fserror.h>
31 #include <uapi/linux/btrfs.h>
32 #include <uapi/linux/btrfs_tree.h>
33 #include "extent-io-tree.h"
34 #include "async-thread.h"
35 #include "block-rsv.h"
36 #include "messages.h"
37 
38 struct inode;
39 struct super_block;
40 struct kobject;
41 struct reloc_control;
42 struct ulist;
43 struct btrfs_device;
44 struct btrfs_block_group;
45 struct btrfs_root;
46 struct btrfs_fs_devices;
47 struct btrfs_transaction;
48 struct btrfs_balance_control;
49 struct btrfs_subpage_info;
50 struct btrfs_stripe_hash_table;
51 struct btrfs_space_info;
52 
53 /*
54  * Minimum data and metadata block size.
55  *
56  * Normally it's 4K, but for testing subpage block size on 4K page systems, we
57  * allow DEBUG builds to accept 2K page size.
58  */
59 #ifdef CONFIG_BTRFS_DEBUG
60 #define BTRFS_MIN_BLOCKSIZE	(SZ_2K)
61 #else
62 #define BTRFS_MIN_BLOCKSIZE	(SZ_4K)
63 #endif
64 
65 #define BTRFS_MAX_BLOCKSIZE	(SZ_64K)
66 
67 #define BTRFS_MAX_EXTENT_SIZE SZ_128M
68 
69 /*
70  * Maximum length to trim in a single iteration to avoid holding device list
71  * mutex for too long.
72  */
73 #define BTRFS_MAX_TRIM_LENGTH			SZ_2G
74 
75 #define BTRFS_OLDEST_GENERATION	0ULL
76 
77 #define BTRFS_EMPTY_DIR_SIZE 0
78 
79 #define BTRFS_DIRTY_METADATA_THRESH		SZ_32M
80 
81 #define BTRFS_SUPER_INFO_OFFSET			SZ_64K
82 #define BTRFS_SUPER_INFO_SIZE			4096
83 static_assert(sizeof(struct btrfs_super_block) == BTRFS_SUPER_INFO_SIZE);
84 
85 /* Array of bytes with variable length, hexadecimal format 0x1234 */
86 #define BTRFS_CSUM_FMT				"0x%*phN"
87 #define BTRFS_CSUM_FMT_VALUE(size, bytes)	size, bytes
88 
89 #define BTRFS_KEY_FMT			"(%llu %u %llu)"
90 #define BTRFS_KEY_FMT_VALUE(key)	(key)->objectid, (key)->type, (key)->offset
91 
92 /*
93  * Number of metadata items necessary for an unlink operation:
94  *
95  * 1 for the possible orphan item
96  * 1 for the dir item
97  * 1 for the dir index
98  * 1 for the inode ref
99  * 1 for the inode
100  * 1 for the parent inode
101  */
102 #define BTRFS_UNLINK_METADATA_UNITS		6
103 
104 /*
105  * The reserved space at the beginning of each device.  It covers the primary
106  * super block and leaves space for potential use by other tools like
107  * bootloaders or to lower potential damage of accidental overwrite.
108  */
109 #define BTRFS_DEVICE_RANGE_RESERVED			(SZ_1M)
110 /*
111  * Runtime (in-memory) states of filesystem
112  */
113 enum {
114 	/*
115 	 * Filesystem is being remounted, allow to skip some operations, like
116 	 * defrag
117 	 */
118 	BTRFS_FS_STATE_REMOUNTING,
119 	/* Filesystem in RO mode */
120 	BTRFS_FS_STATE_RO,
121 	/* Track if a transaction abort has been reported on this filesystem */
122 	BTRFS_FS_STATE_TRANS_ABORTED,
123 	/* Track if log replay has failed. */
124 	BTRFS_FS_STATE_LOG_REPLAY_ABORTED,
125 	/*
126 	 * Bio operations should be blocked on this filesystem because a source
127 	 * or target device is being destroyed as part of a device replace
128 	 */
129 	BTRFS_FS_STATE_DEV_REPLACING,
130 	/* The btrfs_fs_info created for self-tests */
131 	BTRFS_FS_STATE_DUMMY_FS_INFO,
132 
133 	/* Checksum errors are ignored. */
134 	BTRFS_FS_STATE_NO_DATA_CSUMS,
135 	BTRFS_FS_STATE_SKIP_META_CSUMS,
136 
137 	/* Indicates there was an error cleaning up a log tree. */
138 	BTRFS_FS_STATE_LOG_CLEANUP_ERROR,
139 
140 	/* No more delayed iput can be queued. */
141 	BTRFS_FS_STATE_NO_DELAYED_IPUT,
142 
143 	/*
144 	 * Emergency shutdown, a step further than transaction aborted by
145 	 * rejecting all operations.
146 	 */
147 	BTRFS_FS_STATE_EMERGENCY_SHUTDOWN,
148 
149 	BTRFS_FS_STATE_COUNT
150 };
151 
152 enum {
153 	BTRFS_FS_CLOSING_START,
154 	BTRFS_FS_CLOSING_DONE,
155 	BTRFS_FS_LOG_RECOVERING,
156 	BTRFS_FS_OPEN,
157 	BTRFS_FS_QUOTA_ENABLED,
158 	BTRFS_FS_SQUOTA_ENABLING,
159 	BTRFS_FS_UPDATE_UUID_TREE_GEN,
160 	BTRFS_FS_CREATING_FREE_SPACE_TREE,
161 	BTRFS_FS_BTREE_ERR,
162 	BTRFS_FS_LOG1_ERR,
163 	BTRFS_FS_LOG2_ERR,
164 	BTRFS_FS_QUOTA_OVERRIDE,
165 	/* Used to record internally whether fs has been frozen */
166 	BTRFS_FS_FROZEN,
167 	/*
168 	 * Indicate that balance has been set up from the ioctl and is in the
169 	 * main phase. The fs_info::balance_ctl is initialized.
170 	 */
171 	BTRFS_FS_BALANCE_RUNNING,
172 
173 	/*
174 	 * Indicate that relocation of a chunk has started, it's set per chunk
175 	 * and is toggled between chunks.
176 	 */
177 	BTRFS_FS_RELOC_RUNNING,
178 
179 	/* Indicate that the cleaner thread is awake and doing something. */
180 	BTRFS_FS_CLEANER_RUNNING,
181 
182 	/*
183 	 * The checksumming has an optimized version and is considered fast,
184 	 * so we don't need to offload checksums to workqueues.
185 	 */
186 	BTRFS_FS_CSUM_IMPL_FAST,
187 
188 	/* Indicate that the discard workqueue can service discards. */
189 	BTRFS_FS_DISCARD_RUNNING,
190 
191 	/* Indicate that we need to cleanup space cache v1 */
192 	BTRFS_FS_CLEANUP_SPACE_CACHE_V1,
193 
194 	/* Indicate that we can't trust the free space tree for caching yet */
195 	BTRFS_FS_FREE_SPACE_TREE_UNTRUSTED,
196 
197 	/* Indicate whether there are any tree modification log users */
198 	BTRFS_FS_TREE_MOD_LOG_USERS,
199 
200 	/* Indicate that we want the transaction kthread to commit right now. */
201 	BTRFS_FS_COMMIT_TRANS,
202 
203 	/* Indicate we have half completed snapshot deletions pending. */
204 	BTRFS_FS_UNFINISHED_DROPS,
205 
206 	/* Indicate we have to finish a zone to do next allocation. */
207 	BTRFS_FS_NEED_ZONE_FINISH,
208 
209 	/* Indicate that we want to commit the transaction. */
210 	BTRFS_FS_NEED_TRANS_COMMIT,
211 
212 	/* This is set when active zone tracking is needed. */
213 	BTRFS_FS_ACTIVE_ZONE_TRACKING,
214 
215 	/*
216 	 * Indicate if we have some features changed, this is mostly for
217 	 * cleaner thread to update the sysfs interface.
218 	 */
219 	BTRFS_FS_FEATURE_CHANGED,
220 
221 	/*
222 	 * Indicate that we have found a tree block which is only aligned to
223 	 * sectorsize, but not to nodesize.  This should be rare nowadays.
224 	 */
225 	BTRFS_FS_UNALIGNED_TREE_BLOCK,
226 
227 #if BITS_PER_LONG == 32
228 	/* Indicate if we have error/warn message printed on 32bit systems */
229 	BTRFS_FS_32BIT_ERROR,
230 	BTRFS_FS_32BIT_WARN,
231 #endif
232 };
233 
234 /*
235  * Flags for mount options.
236  *
237  * Note: don't forget to add new options to btrfs_show_options()
238  */
239 enum {
240 	BTRFS_MOUNT_NODATASUM			= (1ULL << 0),
241 	BTRFS_MOUNT_NODATACOW			= (1ULL << 1),
242 	BTRFS_MOUNT_NOBARRIER			= (1ULL << 2),
243 	BTRFS_MOUNT_SSD				= (1ULL << 3),
244 	BTRFS_MOUNT_DEGRADED			= (1ULL << 4),
245 	BTRFS_MOUNT_COMPRESS			= (1ULL << 5),
246 	BTRFS_MOUNT_NOTREELOG			= (1ULL << 6),
247 	BTRFS_MOUNT_FLUSHONCOMMIT		= (1ULL << 7),
248 	BTRFS_MOUNT_SSD_SPREAD			= (1ULL << 8),
249 	BTRFS_MOUNT_NOSSD			= (1ULL << 9),
250 	BTRFS_MOUNT_DISCARD_SYNC		= (1ULL << 10),
251 	BTRFS_MOUNT_FORCE_COMPRESS		= (1ULL << 11),
252 	BTRFS_MOUNT_SPACE_CACHE			= (1ULL << 12),
253 	BTRFS_MOUNT_CLEAR_CACHE			= (1ULL << 13),
254 	BTRFS_MOUNT_USER_SUBVOL_RM_ALLOWED	= (1ULL << 14),
255 	BTRFS_MOUNT_ENOSPC_DEBUG		= (1ULL << 15),
256 	BTRFS_MOUNT_AUTO_DEFRAG			= (1ULL << 16),
257 	BTRFS_MOUNT_USEBACKUPROOT		= (1ULL << 17),
258 	BTRFS_MOUNT_SKIP_BALANCE		= (1ULL << 18),
259 	BTRFS_MOUNT_PANIC_ON_FATAL_ERROR	= (1ULL << 19),
260 	BTRFS_MOUNT_RESCAN_UUID_TREE		= (1ULL << 20),
261 	BTRFS_MOUNT_FRAGMENT_DATA		= (1ULL << 21),
262 	BTRFS_MOUNT_FRAGMENT_METADATA		= (1ULL << 22),
263 	BTRFS_MOUNT_FREE_SPACE_TREE		= (1ULL << 23),
264 	BTRFS_MOUNT_NOLOGREPLAY			= (1ULL << 24),
265 	BTRFS_MOUNT_REF_VERIFY			= (1ULL << 25),
266 	BTRFS_MOUNT_DISCARD_ASYNC		= (1ULL << 26),
267 	BTRFS_MOUNT_IGNOREBADROOTS		= (1ULL << 27),
268 	BTRFS_MOUNT_IGNOREDATACSUMS		= (1ULL << 28),
269 	BTRFS_MOUNT_NODISCARD			= (1ULL << 29),
270 	BTRFS_MOUNT_NOSPACECACHE		= (1ULL << 30),
271 	BTRFS_MOUNT_IGNOREMETACSUMS		= (1ULL << 31),
272 	BTRFS_MOUNT_IGNORESUPERFLAGS		= (1ULL << 32),
273 	BTRFS_MOUNT_REF_TRACKER			= (1ULL << 33),
274 };
275 
276 /* These mount options require a full read-only fs, no new transaction is allowed. */
277 #define BTRFS_MOUNT_FULL_RO_MASK		\
278 	(BTRFS_MOUNT_NOLOGREPLAY |		\
279 	 BTRFS_MOUNT_IGNOREBADROOTS |		\
280 	 BTRFS_MOUNT_IGNOREDATACSUMS |		\
281 	 BTRFS_MOUNT_IGNOREMETACSUMS |		\
282 	 BTRFS_MOUNT_IGNORESUPERFLAGS)
283 
284 /*
285  * Compat flags that we support.  If any incompat flags are set other than the
286  * ones specified below then we will fail to mount
287  */
288 #define BTRFS_FEATURE_COMPAT_SUPP		0ULL
289 #define BTRFS_FEATURE_COMPAT_SAFE_SET		0ULL
290 #define BTRFS_FEATURE_COMPAT_SAFE_CLEAR		0ULL
291 
292 #define BTRFS_FEATURE_COMPAT_RO_SUPP			\
293 	(BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE |	\
294 	 BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE_VALID | \
295 	 BTRFS_FEATURE_COMPAT_RO_VERITY |		\
296 	 BTRFS_FEATURE_COMPAT_RO_BLOCK_GROUP_TREE)
297 
298 #define BTRFS_FEATURE_COMPAT_RO_SAFE_SET	0ULL
299 #define BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR	0ULL
300 
301 #define BTRFS_FEATURE_INCOMPAT_SUPP_STABLE		\
302 	(BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF |		\
303 	 BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL |	\
304 	 BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS |		\
305 	 BTRFS_FEATURE_INCOMPAT_BIG_METADATA |		\
306 	 BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO |		\
307 	 BTRFS_FEATURE_INCOMPAT_COMPRESS_ZSTD |		\
308 	 BTRFS_FEATURE_INCOMPAT_RAID56 |		\
309 	 BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF |		\
310 	 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA |	\
311 	 BTRFS_FEATURE_INCOMPAT_NO_HOLES	|	\
312 	 BTRFS_FEATURE_INCOMPAT_METADATA_UUID	|	\
313 	 BTRFS_FEATURE_INCOMPAT_RAID1C34	|	\
314 	 BTRFS_FEATURE_INCOMPAT_ZONED		|	\
315 	 BTRFS_FEATURE_INCOMPAT_SIMPLE_QUOTA)
316 
317 #ifdef CONFIG_BTRFS_EXPERIMENTAL
318 	/*
319 	 * Features under development like Extent tree v2 support is enabled
320 	 * only under CONFIG_BTRFS_EXPERIMENTAL
321 	 */
322 #define BTRFS_FEATURE_INCOMPAT_SUPP		\
323 	(BTRFS_FEATURE_INCOMPAT_SUPP_STABLE |	\
324 	 BTRFS_FEATURE_INCOMPAT_RAID_STRIPE_TREE | \
325 	 BTRFS_FEATURE_INCOMPAT_EXTENT_TREE_V2 | \
326 	 BTRFS_FEATURE_INCOMPAT_REMAP_TREE)
327 
328 #else
329 
330 #define BTRFS_FEATURE_INCOMPAT_SUPP		\
331 	(BTRFS_FEATURE_INCOMPAT_SUPP_STABLE)
332 
333 #endif
334 
335 #define BTRFS_FEATURE_INCOMPAT_SAFE_SET			\
336 	(BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF)
337 #define BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR		0ULL
338 
339 #define BTRFS_DEFAULT_COMMIT_INTERVAL	(30)
340 #define BTRFS_WARNING_COMMIT_INTERVAL	(300)
341 #define BTRFS_DEFAULT_MAX_INLINE	(2048)
342 
343 enum btrfs_compression_type {
344 	BTRFS_COMPRESS_NONE  = 0,
345 	BTRFS_COMPRESS_ZLIB  = 1,
346 	BTRFS_COMPRESS_LZO   = 2,
347 	BTRFS_COMPRESS_ZSTD  = 3,
348 	BTRFS_NR_COMPRESS_TYPES = 4,
349 
350 	BTRFS_DEFRAG_DONT_COMPRESS,
351 };
352 
353 struct btrfs_dev_replace {
354 	/* See #define above */
355 	u64 replace_state;
356 	/* Seconds since 1-Jan-1970 */
357 	time64_t time_started;
358 	/* Seconds since 1-Jan-1970 */
359 	time64_t time_stopped;
360 	atomic64_t num_write_errors;
361 	atomic64_t num_uncorrectable_read_errors;
362 
363 	u64 cursor_left;
364 	u64 committed_cursor_left;
365 	u64 cursor_left_last_write_of_item;
366 	u64 cursor_right;
367 
368 	/* See #define above */
369 	u64 cont_reading_from_srcdev_mode;
370 
371 	int is_valid;
372 	int item_needs_writeback;
373 	struct btrfs_device *srcdev;
374 	struct btrfs_device *tgtdev;
375 
376 	struct mutex lock_finishing_cancel_unmount;
377 	struct rw_semaphore rwsem;
378 
379 	struct btrfs_scrub_progress scrub_progress;
380 
381 	struct percpu_counter bio_counter;
382 	wait_queue_head_t replace_wait;
383 
384 	struct task_struct *replace_task;
385 };
386 
387 /*
388  * Free clusters are used to claim free space in relatively large chunks,
389  * allowing us to do less seeky writes. They are used for all metadata
390  * allocations. In ssd_spread mode they are also used for data allocations.
391  */
392 struct btrfs_free_cluster {
393 	spinlock_t lock;
394 	spinlock_t refill_lock;
395 	struct rb_root root;
396 
397 	/* Largest extent in this cluster */
398 	u64 max_size;
399 
400 	/* First extent starting offset */
401 	u64 window_start;
402 
403 	/* We did a full search and couldn't create a cluster */
404 	bool fragmented;
405 
406 	struct btrfs_block_group *block_group;
407 	/*
408 	 * When a cluster is allocated from a block group, we put the cluster
409 	 * onto a list in the block group so that it can be freed before the
410 	 * block group is freed.
411 	 */
412 	struct list_head block_group_list;
413 };
414 
415 /* Discard control. */
416 /*
417  * Async discard uses multiple lists to differentiate the discard filter
418  * parameters.  Index 0 is for completely free block groups where we need to
419  * ensure the entire block group is trimmed without being lossy.  Indices
420  * afterwards represent monotonically decreasing discard filter sizes to
421  * prioritize what should be discarded next.
422  */
423 #define BTRFS_NR_DISCARD_LISTS		3
424 #define BTRFS_DISCARD_INDEX_UNUSED	0
425 #define BTRFS_DISCARD_INDEX_START	1
426 
427 struct btrfs_discard_ctl {
428 	struct workqueue_struct *discard_workers;
429 	struct delayed_work work;
430 	spinlock_t lock;
431 	struct btrfs_block_group *block_group;
432 	struct list_head discard_list[BTRFS_NR_DISCARD_LISTS];
433 	u64 prev_discard;
434 	u64 prev_discard_time;
435 	atomic_t discardable_extents;
436 	atomic64_t discardable_bytes;
437 	u64 max_discard_size;
438 	u64 delay_ms;
439 	u32 iops_limit;
440 	u32 kbps_limit;
441 	u64 discard_extent_bytes;
442 	u64 discard_bitmap_bytes;
443 	atomic64_t discard_bytes_saved;
444 };
445 
446 /*
447  * Exclusive operations (device replace, resize, device add/remove, balance)
448  */
449 enum btrfs_exclusive_operation {
450 	BTRFS_EXCLOP_NONE,
451 	BTRFS_EXCLOP_BALANCE_PAUSED,
452 	BTRFS_EXCLOP_BALANCE,
453 	BTRFS_EXCLOP_DEV_ADD,
454 	BTRFS_EXCLOP_DEV_REMOVE,
455 	BTRFS_EXCLOP_DEV_REPLACE,
456 	BTRFS_EXCLOP_RESIZE,
457 	BTRFS_EXCLOP_SWAP_ACTIVATE,
458 };
459 
460 /* Store data about transaction commits, exported via sysfs. */
461 struct btrfs_commit_stats {
462 	/* Total number of commits */
463 	u64 commit_count;
464 	/* The maximum commit duration so far in ns */
465 	u64 max_commit_dur;
466 	/* The last commit duration in ns */
467 	u64 last_commit_dur;
468 	/* The total commit duration in ns */
469 	u64 total_commit_dur;
470 	/* Start of the last critical section in ns. */
471 	u64 critical_section_start_time;
472 };
473 
474 struct btrfs_delayed_root {
475 	spinlock_t lock;
476 	int nodes;		/* for delayed nodes */
477 	struct list_head node_list;
478 	/*
479 	 * Used for delayed nodes which is waiting to be dealt with by the
480 	 * worker. If the delayed node is inserted into the work queue, we
481 	 * drop it from this list.
482 	 */
483 	struct list_head prepare_list;
484 	atomic_t items;		/* for delayed items */
485 	atomic_t items_seq;	/* for delayed items */
486 	wait_queue_head_t wait;
487 };
488 
489 struct btrfs_fs_info {
490 	u8 chunk_tree_uuid[BTRFS_UUID_SIZE];
491 	unsigned long flags;
492 	struct btrfs_root *tree_root;
493 	struct btrfs_root *chunk_root;
494 	struct btrfs_root *dev_root;
495 	struct btrfs_root *fs_root;
496 	struct btrfs_root *quota_root;
497 	struct btrfs_root *uuid_root;
498 	struct btrfs_root *data_reloc_root;
499 	struct btrfs_root *block_group_root;
500 	struct btrfs_root *stripe_root;
501 	struct btrfs_root *remap_root;
502 
503 	/* The log root tree is a directory of all the other log roots */
504 	struct btrfs_root *log_root_tree;
505 
506 	/* The tree that holds the global roots (csum, extent, etc) */
507 	rwlock_t global_root_lock;
508 	struct rb_root global_root_tree;
509 
510 	spinlock_t fs_roots_radix_lock;
511 	struct radix_tree_root fs_roots_radix;
512 
513 	/* Block group cache stuff */
514 	rwlock_t block_group_cache_lock;
515 	struct rb_root_cached block_group_cache_tree;
516 
517 	/* Keep track of unallocated space */
518 	atomic64_t free_chunk_space;
519 
520 	/* Track ranges which are used by log trees blocks/logged data extents */
521 	struct extent_io_tree excluded_extents;
522 
523 	/* logical->physical extent mapping */
524 	struct rb_root_cached mapping_tree;
525 	rwlock_t mapping_tree_lock;
526 
527 	/*
528 	 * Block reservation for extent, checksum, root tree and delayed dir
529 	 * index item.
530 	 */
531 	struct btrfs_block_rsv global_block_rsv;
532 	/* Block reservation for metadata operations */
533 	struct btrfs_block_rsv trans_block_rsv;
534 	/* Block reservation for chunk tree */
535 	struct btrfs_block_rsv chunk_block_rsv;
536 	/* Block reservation for remap tree. */
537 	struct btrfs_block_rsv remap_block_rsv;
538 	/* Block reservation for delayed operations */
539 	struct btrfs_block_rsv delayed_block_rsv;
540 	/* Block reservation for delayed refs */
541 	struct btrfs_block_rsv delayed_refs_rsv;
542 	/* Block reservation for treelog tree */
543 	struct btrfs_block_rsv treelog_rsv;
544 
545 	struct btrfs_block_rsv empty_block_rsv;
546 
547 	/*
548 	 * Updated while holding the lock 'trans_lock'. Due to the life cycle of
549 	 * a transaction, it can be directly read while holding a transaction
550 	 * handle, everywhere else must be read with btrfs_get_fs_generation().
551 	 * Should always be updated using btrfs_set_fs_generation().
552 	 */
553 	u64 generation;
554 	/*
555 	 * Always use btrfs_get_last_trans_committed() and
556 	 * btrfs_set_last_trans_committed() to read and update this field.
557 	 */
558 	u64 last_trans_committed;
559 	/*
560 	 * Generation of the last transaction used for block group relocation
561 	 * since the filesystem was last mounted (or 0 if none happened yet).
562 	 * Must be written and read while holding btrfs_fs_info::commit_root_sem.
563 	 */
564 	u64 last_reloc_trans;
565 
566 	/*
567 	 * This is updated to the current trans every time a full commit is
568 	 * required instead of the faster short fsync log commits
569 	 */
570 	u64 last_trans_log_full_commit;
571 	unsigned long long mount_opt;
572 
573 	/* Compress related structures. */
574 	void *compr_wsm[BTRFS_NR_COMPRESS_TYPES];
575 
576 	int compress_type;
577 	int compress_level;
578 	u32 commit_interval;
579 	/*
580 	 * It is a suggestive number, the read side is safe even it gets a
581 	 * wrong number because we will write out the data into a regular
582 	 * extent. The write side(mount/remount) is under ->s_umount lock,
583 	 * so it is also safe.
584 	 */
585 	u64 max_inline;
586 
587 	struct btrfs_transaction *running_transaction;
588 	wait_queue_head_t transaction_throttle;
589 	wait_queue_head_t transaction_wait;
590 	wait_queue_head_t transaction_blocked_wait;
591 	wait_queue_head_t async_submit_wait;
592 
593 	/*
594 	 * Used to protect the incompat_flags, compat_flags, compat_ro_flags
595 	 * when they are updated.
596 	 *
597 	 * Because we do not clear the flags for ever, so we needn't use
598 	 * the lock on the read side.
599 	 *
600 	 * We also needn't use the lock when we mount the fs, because
601 	 * there is no other task which will update the flag.
602 	 */
603 	spinlock_t super_lock;
604 	struct btrfs_super_block *super_copy;
605 	struct btrfs_super_block *super_for_commit;
606 	struct super_block *sb;
607 	struct inode *btree_inode;
608 	struct mutex tree_log_mutex;
609 	struct mutex transaction_kthread_mutex;
610 	struct mutex cleaner_mutex;
611 	struct mutex chunk_mutex;
612 	struct mutex remap_mutex;
613 
614 	/*
615 	 * This is taken to make sure we don't set block groups ro after the
616 	 * free space cache has been allocated on them.
617 	 */
618 	struct mutex ro_block_group_mutex;
619 
620 	/*
621 	 * This is used during read/modify/write to make sure no two ios are
622 	 * trying to mod the same stripe at the same time.
623 	 */
624 	struct btrfs_stripe_hash_table *stripe_hash_table;
625 
626 	/*
627 	 * This protects the ordered operations list only while we are
628 	 * processing all of the entries on it.  This way we make sure the
629 	 * commit code doesn't find the list temporarily empty because another
630 	 * function happens to be doing non-waiting preflush before jumping
631 	 * into the main commit.
632 	 */
633 	struct mutex ordered_operations_mutex;
634 
635 	struct rw_semaphore commit_root_sem;
636 
637 	struct rw_semaphore cleanup_work_sem;
638 
639 	struct rw_semaphore subvol_sem;
640 
641 	spinlock_t trans_lock;
642 	/*
643 	 * The reloc mutex goes with the trans lock, it is taken during commit
644 	 * to protect us from the relocation code.
645 	 */
646 	struct mutex reloc_mutex;
647 
648 	struct list_head trans_list;
649 	struct list_head dead_roots;
650 	struct list_head caching_block_groups;
651 
652 	spinlock_t delayed_iput_lock;
653 	struct list_head delayed_iputs;
654 	atomic_t nr_delayed_iputs;
655 	wait_queue_head_t delayed_iputs_wait;
656 
657 	atomic64_t tree_mod_seq;
658 
659 	/* This protects tree_mod_log and tree_mod_seq_list */
660 	rwlock_t tree_mod_log_lock;
661 	struct rb_root tree_mod_log;
662 	struct list_head tree_mod_seq_list;
663 
664 	atomic_t async_delalloc_pages;
665 
666 	/* This is used to protect the following list -- ordered_roots. */
667 	spinlock_t ordered_root_lock;
668 
669 	/*
670 	 * All fs/file tree roots in which there are data=ordered extents
671 	 * pending writeback are added into this list.
672 	 *
673 	 * These can span multiple transactions and basically include every
674 	 * dirty data page that isn't from nodatacow.
675 	 */
676 	struct list_head ordered_roots;
677 
678 	struct mutex delalloc_root_mutex;
679 	spinlock_t delalloc_root_lock;
680 	/* All fs/file tree roots that have delalloc inodes. */
681 	struct list_head delalloc_roots;
682 
683 	/*
684 	 * There is a pool of worker threads for checksumming during writes and
685 	 * a pool for checksumming after reads.  This is because readers can
686 	 * run with FS locks held, and the writers may be waiting for those
687 	 * locks.  We don't want ordering in the pending list to cause
688 	 * deadlocks, and so the two are serviced separately.
689 	 *
690 	 * A third pool does submit_bio to avoid deadlocking with the other two.
691 	 */
692 	struct btrfs_workqueue *workers;
693 	struct btrfs_workqueue *delalloc_workers;
694 	struct btrfs_workqueue *flush_workers;
695 	struct workqueue_struct *endio_workers;
696 	struct workqueue_struct *endio_meta_workers;
697 	struct workqueue_struct *rmw_workers;
698 	struct btrfs_workqueue *endio_write_workers;
699 	struct btrfs_workqueue *endio_freespace_worker;
700 	struct btrfs_workqueue *caching_workers;
701 
702 	/*
703 	 * Fixup workers take dirty pages that didn't properly go through the
704 	 * cow mechanism and make them safe to write.  It happens for the
705 	 * sys_munmap function call path.
706 	 */
707 	struct btrfs_workqueue *fixup_workers;
708 	struct btrfs_workqueue *delayed_workers;
709 
710 	struct task_struct *transaction_kthread;
711 	struct task_struct *cleaner_kthread;
712 	u32 thread_pool_size;
713 
714 	struct kobject *space_info_kobj;
715 	struct kobject *qgroups_kobj;
716 	struct kobject *discard_kobj;
717 
718 	/* Track the number of blocks (sectors) read by the filesystem. */
719 	struct percpu_counter stats_read_blocks;
720 
721 	/* Used to keep from writing metadata until there is a nice batch */
722 	struct percpu_counter dirty_metadata_bytes;
723 	struct percpu_counter delalloc_bytes;
724 	struct percpu_counter ordered_bytes;
725 	s32 dirty_metadata_batch;
726 	s32 delalloc_batch;
727 
728 	struct percpu_counter evictable_extent_maps;
729 	u64 em_shrinker_last_root;
730 	u64 em_shrinker_last_ino;
731 	atomic64_t em_shrinker_nr_to_scan;
732 	struct work_struct em_shrinker_work;
733 
734 	/* Protected by 'trans_lock'. */
735 	struct list_head dirty_cowonly_roots;
736 
737 	struct btrfs_fs_devices *fs_devices;
738 
739 	/*
740 	 * The space_info list is effectively read only after initial setup.
741 	 * It is populated at mount time and cleaned up after all block groups
742 	 * are removed.  RCU is used to protect it.
743 	 */
744 	struct list_head space_info;
745 
746 	struct btrfs_space_info *data_sinfo;
747 
748 	struct reloc_control *reloc_ctl;
749 
750 	/* data_alloc_cluster is only used in ssd_spread mode */
751 	struct btrfs_free_cluster data_alloc_cluster;
752 
753 	/* All metadata allocations go through this cluster. */
754 	struct btrfs_free_cluster meta_alloc_cluster;
755 
756 	/* Auto defrag inodes go here. */
757 	spinlock_t defrag_inodes_lock;
758 	struct rb_root defrag_inodes;
759 	atomic_t defrag_running;
760 
761 	/* Used to protect avail_{data, metadata, system}_alloc_bits */
762 	seqlock_t profiles_lock;
763 	/*
764 	 * These three are in extended format (availability of single chunks is
765 	 * denoted by BTRFS_AVAIL_ALLOC_BIT_SINGLE bit, other types are denoted
766 	 * by corresponding BTRFS_BLOCK_GROUP_* bits)
767 	 */
768 	u64 avail_data_alloc_bits;
769 	u64 avail_metadata_alloc_bits;
770 	u64 avail_system_alloc_bits;
771 
772 	/* Balance state */
773 	spinlock_t balance_lock;
774 	struct mutex balance_mutex;
775 	atomic_t balance_pause_req;
776 	atomic_t balance_cancel_req;
777 	struct btrfs_balance_control *balance_ctl;
778 	wait_queue_head_t balance_wait_q;
779 
780 	/* Cancellation requests for chunk relocation */
781 	atomic_t reloc_cancel_req;
782 
783 	u32 data_chunk_allocations;
784 	u32 metadata_ratio;
785 
786 	/* Private scrub information */
787 	struct mutex scrub_lock;
788 	atomic_t scrubs_running;
789 	atomic_t scrub_pause_req;
790 	atomic_t scrubs_paused;
791 	atomic_t scrub_cancel_req;
792 	wait_queue_head_t scrub_pause_wait;
793 	/*
794 	 * The worker pointers are NULL iff the refcount is 0, ie. scrub is not
795 	 * running.
796 	 */
797 	refcount_t scrub_workers_refcnt;
798 	struct workqueue_struct *scrub_workers;
799 
800 	struct btrfs_discard_ctl discard_ctl;
801 
802 	/* Is qgroup tracking in a consistent state? */
803 	u64 qgroup_flags;
804 
805 	/* Holds configuration and tracking. Protected by qgroup_lock. */
806 	struct rb_root qgroup_tree;
807 	spinlock_t qgroup_lock;
808 
809 	/*
810 	 * Protect user change for quota operations. If a transaction is needed,
811 	 * it must be started before locking this lock.
812 	 */
813 	struct mutex qgroup_ioctl_lock;
814 
815 	/* List of dirty qgroups to be written at next commit. */
816 	struct list_head dirty_qgroups;
817 
818 	/* Used by qgroup for an efficient tree traversal. */
819 	u64 qgroup_seq;
820 
821 	/* Qgroup rescan items. */
822 	/* Protects the progress item */
823 	struct mutex qgroup_rescan_lock;
824 	struct btrfs_key qgroup_rescan_progress;
825 	struct btrfs_workqueue *qgroup_rescan_workers;
826 	struct completion qgroup_rescan_completion;
827 	struct btrfs_work qgroup_rescan_work;
828 	/* Protected by qgroup_rescan_lock */
829 	bool qgroup_rescan_running;
830 	u8 qgroup_drop_subtree_thres;
831 	u64 qgroup_enable_gen;
832 
833 	/*
834 	 * If this is not 0, then it indicates a serious filesystem error has
835 	 * happened and it contains that error (negative errno value).
836 	 */
837 	int fs_error;
838 
839 	/* Filesystem state */
840 	unsigned long fs_state;
841 
842 	struct btrfs_delayed_root delayed_root;
843 
844 	/* Entries are eb->start >> nodesize_bits */
845 	struct xarray buffer_tree;
846 
847 	/* Next backup root to be overwritten */
848 	int backup_root_index;
849 
850 	/* Device replace state */
851 	struct btrfs_dev_replace dev_replace;
852 
853 	struct semaphore uuid_tree_rescan_sem;
854 
855 	/* Used to reclaim the metadata space in the background. */
856 	struct work_struct async_reclaim_work;
857 	struct work_struct async_data_reclaim_work;
858 	struct work_struct preempt_reclaim_work;
859 
860 	/* Reclaim partially filled block groups in the background */
861 	struct work_struct reclaim_bgs_work;
862 	/* Protected by unused_bgs_lock. */
863 	struct list_head reclaim_bgs;
864 	int bg_reclaim_threshold;
865 
866 	/* Protects the lists unused_bgs, reclaim_bgs, and fully_remapped_bgs. */
867 	spinlock_t unused_bgs_lock;
868 	/* Protected by unused_bgs_lock. */
869 	struct list_head unused_bgs;
870 	struct list_head fully_remapped_bgs;
871 	struct mutex unused_bg_unpin_mutex;
872 	/* Protect block groups that are going to be deleted */
873 	struct mutex reclaim_bgs_lock;
874 
875 	/* Cached block sizes */
876 	u32 nodesize;
877 	u32 nodesize_bits;
878 	u32 sectorsize;
879 	/* ilog2 of sectorsize, use to avoid 64bit division */
880 	u32 sectorsize_bits;
881 	u32 block_min_order;
882 	u32 block_max_order;
883 	u32 stripesize;
884 	u32 csum_size;
885 	u32 csums_per_leaf;
886 	u32 csum_type;
887 
888 	/*
889 	 * Maximum size of an extent. BTRFS_MAX_EXTENT_SIZE on regular
890 	 * filesystem, on zoned it depends on the device constraints.
891 	 */
892 	u64 max_extent_size;
893 
894 	/* Block groups and devices containing active swapfiles. */
895 	spinlock_t swapfile_pins_lock;
896 	struct rb_root swapfile_pins;
897 
898 	/* Type of exclusive operation running, protected by super_lock */
899 	enum btrfs_exclusive_operation exclusive_operation;
900 
901 	/*
902 	 * Zone size > 0 when in ZONED mode, otherwise it's used for a check
903 	 * if the mode is enabled
904 	 */
905 	u64 zone_size;
906 
907 	/* Constraints for ZONE_APPEND commands: */
908 	struct queue_limits limits;
909 	u64 max_zone_append_size;
910 
911 	struct mutex zoned_meta_io_lock;
912 	spinlock_t treelog_bg_lock;
913 	u64 treelog_bg;
914 
915 	/*
916 	 * Start of the dedicated data relocation block group, protected by
917 	 * relocation_bg_lock.
918 	 */
919 	spinlock_t relocation_bg_lock;
920 	u64 data_reloc_bg;
921 	struct mutex zoned_data_reloc_io_lock;
922 
923 	struct btrfs_block_group *active_meta_bg;
924 	struct btrfs_block_group *active_system_bg;
925 
926 	u64 nr_global_roots;
927 
928 	spinlock_t zone_active_bgs_lock;
929 	struct list_head zone_active_bgs;
930 
931 	/* Updates are not protected by any lock */
932 	struct btrfs_commit_stats commit_stats;
933 
934 	/*
935 	 * Last generation where we dropped a non-relocation root.
936 	 * Use btrfs_set_last_root_drop_gen() and btrfs_get_last_root_drop_gen()
937 	 * to change it and to read it, respectively.
938 	 */
939 	u64 last_root_drop_gen;
940 
941 	/*
942 	 * Annotations for transaction events (structures are empty when
943 	 * compiled without lockdep).
944 	 */
945 	struct lockdep_map btrfs_trans_num_writers_map;
946 	struct lockdep_map btrfs_trans_num_extwriters_map;
947 	struct lockdep_map btrfs_state_change_map[4];
948 	struct lockdep_map btrfs_trans_pending_ordered_map;
949 	struct lockdep_map btrfs_ordered_extent_map;
950 
951 #ifdef CONFIG_BTRFS_DEBUG
952 	spinlock_t ref_verify_lock;
953 	struct rb_root block_tree;
954 
955 	struct kobject *debug_kobj;
956 	struct list_head allocated_roots;
957 
958 	spinlock_t eb_leak_lock;
959 	struct list_head allocated_ebs;
960 #endif
961 };
962 
963 #define folio_to_inode(_folio)	(BTRFS_I(_Generic((_folio),			\
964 					  struct folio *: (_folio))->mapping->host))
965 
966 #define folio_to_fs_info(_folio) (folio_to_inode(_folio)->root->fs_info)
967 
968 #define inode_to_fs_info(_inode) (BTRFS_I(_Generic((_inode),			\
969 					   struct inode *: (_inode)))->root->fs_info)
970 
btrfs_alloc_write_mask(const struct address_space * mapping)971 static inline gfp_t btrfs_alloc_write_mask(const struct address_space *mapping)
972 {
973 	return mapping_gfp_constraint(mapping, ~__GFP_FS);
974 }
975 
976 /* Return the minimal folio size of the fs. */
btrfs_min_folio_size(const struct btrfs_fs_info * fs_info)977 static inline unsigned int btrfs_min_folio_size(const struct btrfs_fs_info *fs_info)
978 {
979 	return 1U << (PAGE_SHIFT + fs_info->block_min_order);
980 }
981 
btrfs_get_fs_generation(const struct btrfs_fs_info * fs_info)982 static inline u64 btrfs_get_fs_generation(const struct btrfs_fs_info *fs_info)
983 {
984 	return READ_ONCE(fs_info->generation);
985 }
986 
btrfs_set_fs_generation(struct btrfs_fs_info * fs_info,u64 gen)987 static inline void btrfs_set_fs_generation(struct btrfs_fs_info *fs_info, u64 gen)
988 {
989 	WRITE_ONCE(fs_info->generation, gen);
990 }
991 
btrfs_get_last_trans_committed(const struct btrfs_fs_info * fs_info)992 static inline u64 btrfs_get_last_trans_committed(const struct btrfs_fs_info *fs_info)
993 {
994 	return READ_ONCE(fs_info->last_trans_committed);
995 }
996 
btrfs_set_last_trans_committed(struct btrfs_fs_info * fs_info,u64 gen)997 static inline void btrfs_set_last_trans_committed(struct btrfs_fs_info *fs_info, u64 gen)
998 {
999 	WRITE_ONCE(fs_info->last_trans_committed, gen);
1000 }
1001 
btrfs_set_last_root_drop_gen(struct btrfs_fs_info * fs_info,u64 gen)1002 static inline void btrfs_set_last_root_drop_gen(struct btrfs_fs_info *fs_info,
1003 						u64 gen)
1004 {
1005 	WRITE_ONCE(fs_info->last_root_drop_gen, gen);
1006 }
1007 
btrfs_get_last_root_drop_gen(const struct btrfs_fs_info * fs_info)1008 static inline u64 btrfs_get_last_root_drop_gen(const struct btrfs_fs_info *fs_info)
1009 {
1010 	return READ_ONCE(fs_info->last_root_drop_gen);
1011 }
1012 
1013 /*
1014  * Take the number of bytes to be checksummed and figure out how many leaves
1015  * it would require to store the csums for that many bytes.
1016  */
btrfs_csum_bytes_to_leaves(const struct btrfs_fs_info * fs_info,u64 csum_bytes)1017 static inline u64 btrfs_csum_bytes_to_leaves(
1018 			const struct btrfs_fs_info *fs_info, u64 csum_bytes)
1019 {
1020 	const u64 num_csums = csum_bytes >> fs_info->sectorsize_bits;
1021 
1022 	return DIV_ROUND_UP_ULL(num_csums, fs_info->csums_per_leaf);
1023 }
1024 
1025 /*
1026  * Use this if we would be adding new items, as we could split nodes as we cow
1027  * down the tree.
1028  */
btrfs_calc_insert_metadata_size(const struct btrfs_fs_info * fs_info,unsigned num_items)1029 static inline u64 btrfs_calc_insert_metadata_size(const struct btrfs_fs_info *fs_info,
1030 						  unsigned num_items)
1031 {
1032 	return (u64)fs_info->nodesize * BTRFS_MAX_LEVEL * 2 * num_items;
1033 }
1034 
1035 /*
1036  * Doing a truncate or a modification won't result in new nodes or leaves, just
1037  * what we need for COW.
1038  */
btrfs_calc_metadata_size(const struct btrfs_fs_info * fs_info,unsigned num_items)1039 static inline u64 btrfs_calc_metadata_size(const struct btrfs_fs_info *fs_info,
1040 						 unsigned num_items)
1041 {
1042 	return (u64)fs_info->nodesize * BTRFS_MAX_LEVEL * num_items;
1043 }
1044 
1045 #define BTRFS_MAX_EXTENT_ITEM_SIZE(r) ((BTRFS_LEAF_DATA_SIZE(r->fs_info) >> 4) - \
1046 					sizeof(struct btrfs_item))
1047 
1048 #define BTRFS_BYTES_TO_BLKS(fs_info, bytes) ((bytes) >> (fs_info)->sectorsize_bits)
1049 
btrfs_is_zoned(const struct btrfs_fs_info * fs_info)1050 static inline bool btrfs_is_zoned(const struct btrfs_fs_info *fs_info)
1051 {
1052 	return IS_ENABLED(CONFIG_BLK_DEV_ZONED) && fs_info->zone_size > 0;
1053 }
1054 
1055 /*
1056  * Count how many fs_info->max_extent_size cover the @size
1057  */
count_max_extents(const struct btrfs_fs_info * fs_info,u64 size)1058 static inline u32 count_max_extents(const struct btrfs_fs_info *fs_info, u64 size)
1059 {
1060 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
1061 	if (!fs_info)
1062 		return div_u64(size + BTRFS_MAX_EXTENT_SIZE - 1, BTRFS_MAX_EXTENT_SIZE);
1063 #endif
1064 
1065 	return div_u64(size + fs_info->max_extent_size - 1, fs_info->max_extent_size);
1066 }
1067 
btrfs_blocks_per_folio(const struct btrfs_fs_info * fs_info,const struct folio * folio)1068 static inline unsigned int btrfs_blocks_per_folio(const struct btrfs_fs_info *fs_info,
1069 						  const struct folio *folio)
1070 {
1071 	return folio_size(folio) >> fs_info->sectorsize_bits;
1072 }
1073 
1074 bool __attribute_const__ btrfs_supported_blocksize(u32 blocksize);
1075 bool btrfs_exclop_start(struct btrfs_fs_info *fs_info,
1076 			enum btrfs_exclusive_operation type);
1077 bool btrfs_exclop_start_try_lock(struct btrfs_fs_info *fs_info,
1078 				 enum btrfs_exclusive_operation type);
1079 void btrfs_exclop_start_unlock(struct btrfs_fs_info *fs_info);
1080 void btrfs_exclop_finish(struct btrfs_fs_info *fs_info);
1081 void btrfs_exclop_balance(struct btrfs_fs_info *fs_info,
1082 			  enum btrfs_exclusive_operation op);
1083 
1084 int btrfs_check_ioctl_vol_args_path(const struct btrfs_ioctl_vol_args *vol_args);
1085 
1086 u16 btrfs_csum_type_size(u16 type);
1087 int btrfs_super_csum_size(const struct btrfs_super_block *s);
1088 const char *btrfs_super_csum_name(u16 csum_type);
1089 size_t __attribute_const__ btrfs_get_num_csums(void);
1090 struct btrfs_csum_ctx {
1091 	u16 csum_type;
1092 	union {
1093 		u32 crc32;
1094 		struct xxh64_state xxh64;
1095 		struct sha256_ctx sha256;
1096 		struct blake2b_ctx blake2b;
1097 	};
1098 };
1099 void btrfs_csum(u16 csum_type, const u8 *data, size_t len, u8 *out);
1100 void btrfs_csum_init(struct btrfs_csum_ctx *ctx, u16 csum_type);
1101 void btrfs_csum_update(struct btrfs_csum_ctx *ctx, const u8 *data, size_t len);
1102 void btrfs_csum_final(struct btrfs_csum_ctx *ctx, u8 *out);
1103 
btrfs_is_empty_uuid(const u8 * uuid)1104 static inline bool btrfs_is_empty_uuid(const u8 *uuid)
1105 {
1106 	return uuid_is_null((const uuid_t *)uuid);
1107 }
1108 
1109 /* Compatibility and incompatibility defines */
1110 void __btrfs_set_fs_incompat(struct btrfs_fs_info *fs_info, u64 flag,
1111 			     const char *name);
1112 void __btrfs_clear_fs_incompat(struct btrfs_fs_info *fs_info, u64 flag,
1113 			       const char *name);
1114 void __btrfs_set_fs_compat_ro(struct btrfs_fs_info *fs_info, u64 flag,
1115 			      const char *name);
1116 void __btrfs_clear_fs_compat_ro(struct btrfs_fs_info *fs_info, u64 flag,
1117 				const char *name);
1118 
1119 #define __btrfs_fs_incompat(fs_info, flags)				\
1120 	(!!(btrfs_super_incompat_flags((fs_info)->super_copy) & (flags)))
1121 
1122 #define __btrfs_fs_compat_ro(fs_info, flags)				\
1123 	(!!(btrfs_super_compat_ro_flags((fs_info)->super_copy) & (flags)))
1124 
1125 #define btrfs_set_fs_incompat(__fs_info, opt)				\
1126 	__btrfs_set_fs_incompat((__fs_info), BTRFS_FEATURE_INCOMPAT_##opt, #opt)
1127 
1128 #define btrfs_clear_fs_incompat(__fs_info, opt)				\
1129 	__btrfs_clear_fs_incompat((__fs_info), BTRFS_FEATURE_INCOMPAT_##opt, #opt)
1130 
1131 #define btrfs_fs_incompat(fs_info, opt)					\
1132 	__btrfs_fs_incompat((fs_info), BTRFS_FEATURE_INCOMPAT_##opt)
1133 
1134 #define btrfs_set_fs_compat_ro(__fs_info, opt)				\
1135 	__btrfs_set_fs_compat_ro((__fs_info), BTRFS_FEATURE_COMPAT_RO_##opt, #opt)
1136 
1137 #define btrfs_clear_fs_compat_ro(__fs_info, opt)			\
1138 	__btrfs_clear_fs_compat_ro((__fs_info), BTRFS_FEATURE_COMPAT_RO_##opt, #opt)
1139 
1140 #define btrfs_fs_compat_ro(fs_info, opt)				\
1141 	__btrfs_fs_compat_ro((fs_info), BTRFS_FEATURE_COMPAT_RO_##opt)
1142 
1143 #define btrfs_clear_opt(o, opt)		((o) &= ~BTRFS_MOUNT_##opt)
1144 #define btrfs_set_opt(o, opt)		((o) |= BTRFS_MOUNT_##opt)
1145 #define btrfs_raw_test_opt(o, opt)	((o) & BTRFS_MOUNT_##opt)
1146 #define btrfs_test_opt(fs_info, opt)	((fs_info)->mount_opt & \
1147 					 BTRFS_MOUNT_##opt)
1148 
btrfs_fs_closing(const struct btrfs_fs_info * fs_info)1149 static inline bool btrfs_fs_closing(const struct btrfs_fs_info *fs_info)
1150 {
1151 	return unlikely(test_bit(BTRFS_FS_CLOSING_START, &fs_info->flags));
1152 }
1153 
btrfs_fs_closing_done(const struct btrfs_fs_info * fs_info)1154 static inline bool btrfs_fs_closing_done(const struct btrfs_fs_info *fs_info)
1155 {
1156 	if (btrfs_fs_closing(fs_info) && test_bit(BTRFS_FS_CLOSING_DONE, &fs_info->flags))
1157 		return true;
1158 
1159 	return false;
1160 }
1161 
1162 /*
1163  * If we remount the fs to be R/O or umount the fs, the cleaner needn't do
1164  * anything except sleeping. This function is used to check the status of
1165  * the fs.
1166  * We check for BTRFS_FS_STATE_RO to avoid races with a concurrent remount,
1167  * since setting and checking for SB_RDONLY in the superblock's flags is not
1168  * atomic.
1169  */
btrfs_need_cleaner_sleep(const struct btrfs_fs_info * fs_info)1170 static inline int btrfs_need_cleaner_sleep(const struct btrfs_fs_info *fs_info)
1171 {
1172 	return test_bit(BTRFS_FS_STATE_RO, &fs_info->fs_state) ||
1173 		btrfs_fs_closing(fs_info);
1174 }
1175 
btrfs_wake_unfinished_drop(struct btrfs_fs_info * fs_info)1176 static inline void btrfs_wake_unfinished_drop(struct btrfs_fs_info *fs_info)
1177 {
1178 	clear_and_wake_up_bit(BTRFS_FS_UNFINISHED_DROPS, &fs_info->flags);
1179 }
1180 
1181 #define BTRFS_FS_ERROR(fs_info)	(READ_ONCE((fs_info)->fs_error))
1182 
1183 #define BTRFS_FS_LOG_CLEANUP_ERROR(fs_info)				\
1184 	(unlikely(test_bit(BTRFS_FS_STATE_LOG_CLEANUP_ERROR,		\
1185 			   &(fs_info)->fs_state)))
1186 
btrfs_is_shutdown(const struct btrfs_fs_info * fs_info)1187 static inline bool btrfs_is_shutdown(const struct btrfs_fs_info *fs_info)
1188 {
1189 	return unlikely(test_bit(BTRFS_FS_STATE_EMERGENCY_SHUTDOWN, &fs_info->fs_state));
1190 }
1191 
btrfs_force_shutdown(struct btrfs_fs_info * fs_info)1192 static inline void btrfs_force_shutdown(struct btrfs_fs_info *fs_info)
1193 {
1194 	/*
1195 	 * Here we do not want to use handle_fs_error(), which will mark the fs
1196 	 * read-only.
1197 	 * Some call sites like shutdown ioctl will mark the fs shutdown when
1198 	 * the fs is frozen. But thaw path will handle RO and RW fs
1199 	 * differently.
1200 	 *
1201 	 * So here we only mark the fs error without flipping it RO.
1202 	 */
1203 	WRITE_ONCE(fs_info->fs_error, -EIO);
1204 	if (!test_and_set_bit(BTRFS_FS_STATE_EMERGENCY_SHUTDOWN, &fs_info->fs_state)) {
1205 		btrfs_crit(fs_info, "emergency shutdown");
1206 		fserror_report_shutdown(fs_info->sb, GFP_KERNEL);
1207 	}
1208 }
1209 
1210 /*
1211  * We use folio flag owner_2 to indicate there is an ordered extent with
1212  * unfinished IO.
1213  */
1214 #define folio_test_ordered(folio)	folio_test_owner_2(folio)
1215 #define folio_set_ordered(folio)	folio_set_owner_2(folio)
1216 #define folio_clear_ordered(folio)	folio_clear_owner_2(folio)
1217 
1218 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
1219 
1220 #define EXPORT_FOR_TESTS
1221 
btrfs_is_testing(const struct btrfs_fs_info * fs_info)1222 static inline bool btrfs_is_testing(const struct btrfs_fs_info *fs_info)
1223 {
1224 	return unlikely(test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state));
1225 }
1226 
1227 void btrfs_test_destroy_inode(struct inode *inode);
1228 
1229 #else
1230 
1231 #define EXPORT_FOR_TESTS static
1232 
btrfs_is_testing(const struct btrfs_fs_info * fs_info)1233 static inline bool btrfs_is_testing(const struct btrfs_fs_info *fs_info)
1234 {
1235 	return false;
1236 }
1237 #endif
1238 
1239 #endif
1240