1 /* SPDX-License-Identifier: GPL-2.0 */
2 
3 #ifndef BTRFS_FS_H
4 #define BTRFS_FS_H
5 
6 #include <linux/blkdev.h>
7 #include <linux/fs.h>
8 #include <linux/btrfs_tree.h>
9 #include <linux/sizes.h>
10 #include "extent-io-tree.h"
11 #include "extent_map.h"
12 #include "async-thread.h"
13 #include "block-rsv.h"
14 
15 #define BTRFS_MAX_EXTENT_SIZE SZ_128M
16 
17 #define BTRFS_OLDEST_GENERATION	0ULL
18 
19 #define BTRFS_EMPTY_DIR_SIZE 0
20 
21 #define BTRFS_DIRTY_METADATA_THRESH		SZ_32M
22 
23 #define BTRFS_SUPER_INFO_OFFSET			SZ_64K
24 #define BTRFS_SUPER_INFO_SIZE			4096
25 static_assert(sizeof(struct btrfs_super_block) == BTRFS_SUPER_INFO_SIZE);
26 
27 /*
28  * The reserved space at the beginning of each device.  It covers the primary
29  * super block and leaves space for potential use by other tools like
30  * bootloaders or to lower potential damage of accidental overwrite.
31  */
32 #define BTRFS_DEVICE_RANGE_RESERVED			(SZ_1M)
33 /*
34  * Runtime (in-memory) states of filesystem
35  */
36 enum {
37 	/* Global indicator of serious filesystem errors */
38 	BTRFS_FS_STATE_ERROR,
39 	/*
40 	 * Filesystem is being remounted, allow to skip some operations, like
41 	 * defrag
42 	 */
43 	BTRFS_FS_STATE_REMOUNTING,
44 	/* Filesystem in RO mode */
45 	BTRFS_FS_STATE_RO,
46 	/* Track if a transaction abort has been reported on this filesystem */
47 	BTRFS_FS_STATE_TRANS_ABORTED,
48 	/*
49 	 * Bio operations should be blocked on this filesystem because a source
50 	 * or target device is being destroyed as part of a device replace
51 	 */
52 	BTRFS_FS_STATE_DEV_REPLACING,
53 	/* The btrfs_fs_info created for self-tests */
54 	BTRFS_FS_STATE_DUMMY_FS_INFO,
55 
56 	BTRFS_FS_STATE_NO_CSUMS,
57 
58 	/* Indicates there was an error cleaning up a log tree. */
59 	BTRFS_FS_STATE_LOG_CLEANUP_ERROR,
60 
61 	BTRFS_FS_STATE_COUNT
62 };
63 
64 enum {
65 	BTRFS_FS_CLOSING_START,
66 	BTRFS_FS_CLOSING_DONE,
67 	BTRFS_FS_LOG_RECOVERING,
68 	BTRFS_FS_OPEN,
69 	BTRFS_FS_QUOTA_ENABLED,
70 	BTRFS_FS_UPDATE_UUID_TREE_GEN,
71 	BTRFS_FS_CREATING_FREE_SPACE_TREE,
72 	BTRFS_FS_BTREE_ERR,
73 	BTRFS_FS_LOG1_ERR,
74 	BTRFS_FS_LOG2_ERR,
75 	BTRFS_FS_QUOTA_OVERRIDE,
76 	/* Used to record internally whether fs has been frozen */
77 	BTRFS_FS_FROZEN,
78 	/*
79 	 * Indicate that balance has been set up from the ioctl and is in the
80 	 * main phase. The fs_info::balance_ctl is initialized.
81 	 */
82 	BTRFS_FS_BALANCE_RUNNING,
83 
84 	/*
85 	 * Indicate that relocation of a chunk has started, it's set per chunk
86 	 * and is toggled between chunks.
87 	 */
88 	BTRFS_FS_RELOC_RUNNING,
89 
90 	/* Indicate that the cleaner thread is awake and doing something. */
91 	BTRFS_FS_CLEANER_RUNNING,
92 
93 	/*
94 	 * The checksumming has an optimized version and is considered fast,
95 	 * so we don't need to offload checksums to workqueues.
96 	 */
97 	BTRFS_FS_CSUM_IMPL_FAST,
98 
99 	/* Indicate that the discard workqueue can service discards. */
100 	BTRFS_FS_DISCARD_RUNNING,
101 
102 	/* Indicate that we need to cleanup space cache v1 */
103 	BTRFS_FS_CLEANUP_SPACE_CACHE_V1,
104 
105 	/* Indicate that we can't trust the free space tree for caching yet */
106 	BTRFS_FS_FREE_SPACE_TREE_UNTRUSTED,
107 
108 	/* Indicate whether there are any tree modification log users */
109 	BTRFS_FS_TREE_MOD_LOG_USERS,
110 
111 	/* Indicate that we want the transaction kthread to commit right now. */
112 	BTRFS_FS_COMMIT_TRANS,
113 
114 	/* Indicate we have half completed snapshot deletions pending. */
115 	BTRFS_FS_UNFINISHED_DROPS,
116 
117 	/* Indicate we have to finish a zone to do next allocation. */
118 	BTRFS_FS_NEED_ZONE_FINISH,
119 
120 	/* Indicate that we want to commit the transaction. */
121 	BTRFS_FS_NEED_TRANS_COMMIT,
122 
123 	/*
124 	 * Indicate metadata over-commit is disabled. This is set when active
125 	 * zone tracking is needed.
126 	 */
127 	BTRFS_FS_NO_OVERCOMMIT,
128 
129 	/*
130 	 * Indicate if we have some features changed, this is mostly for
131 	 * cleaner thread to update the sysfs interface.
132 	 */
133 	BTRFS_FS_FEATURE_CHANGED,
134 
135 #if BITS_PER_LONG == 32
136 	/* Indicate if we have error/warn message printed on 32bit systems */
137 	BTRFS_FS_32BIT_ERROR,
138 	BTRFS_FS_32BIT_WARN,
139 #endif
140 };
141 
142 /*
143  * Flags for mount options.
144  *
145  * Note: don't forget to add new options to btrfs_show_options()
146  */
147 enum {
148 	BTRFS_MOUNT_NODATASUM			= (1UL << 0),
149 	BTRFS_MOUNT_NODATACOW			= (1UL << 1),
150 	BTRFS_MOUNT_NOBARRIER			= (1UL << 2),
151 	BTRFS_MOUNT_SSD				= (1UL << 3),
152 	BTRFS_MOUNT_DEGRADED			= (1UL << 4),
153 	BTRFS_MOUNT_COMPRESS			= (1UL << 5),
154 	BTRFS_MOUNT_NOTREELOG   		= (1UL << 6),
155 	BTRFS_MOUNT_FLUSHONCOMMIT		= (1UL << 7),
156 	BTRFS_MOUNT_SSD_SPREAD			= (1UL << 8),
157 	BTRFS_MOUNT_NOSSD			= (1UL << 9),
158 	BTRFS_MOUNT_DISCARD_SYNC		= (1UL << 10),
159 	BTRFS_MOUNT_FORCE_COMPRESS      	= (1UL << 11),
160 	BTRFS_MOUNT_SPACE_CACHE			= (1UL << 12),
161 	BTRFS_MOUNT_CLEAR_CACHE			= (1UL << 13),
162 	BTRFS_MOUNT_USER_SUBVOL_RM_ALLOWED	= (1UL << 14),
163 	BTRFS_MOUNT_ENOSPC_DEBUG		= (1UL << 15),
164 	BTRFS_MOUNT_AUTO_DEFRAG			= (1UL << 16),
165 	BTRFS_MOUNT_USEBACKUPROOT		= (1UL << 17),
166 	BTRFS_MOUNT_SKIP_BALANCE		= (1UL << 18),
167 	BTRFS_MOUNT_CHECK_INTEGRITY		= (1UL << 19),
168 	BTRFS_MOUNT_CHECK_INTEGRITY_DATA	= (1UL << 20),
169 	BTRFS_MOUNT_PANIC_ON_FATAL_ERROR	= (1UL << 21),
170 	BTRFS_MOUNT_RESCAN_UUID_TREE		= (1UL << 22),
171 	BTRFS_MOUNT_FRAGMENT_DATA		= (1UL << 23),
172 	BTRFS_MOUNT_FRAGMENT_METADATA		= (1UL << 24),
173 	BTRFS_MOUNT_FREE_SPACE_TREE		= (1UL << 25),
174 	BTRFS_MOUNT_NOLOGREPLAY			= (1UL << 26),
175 	BTRFS_MOUNT_REF_VERIFY			= (1UL << 27),
176 	BTRFS_MOUNT_DISCARD_ASYNC		= (1UL << 28),
177 	BTRFS_MOUNT_IGNOREBADROOTS		= (1UL << 29),
178 	BTRFS_MOUNT_IGNOREDATACSUMS		= (1UL << 30),
179 	BTRFS_MOUNT_NODISCARD			= (1UL << 31),
180 };
181 
182 /*
183  * Compat flags that we support.  If any incompat flags are set other than the
184  * ones specified below then we will fail to mount
185  */
186 #define BTRFS_FEATURE_COMPAT_SUPP		0ULL
187 #define BTRFS_FEATURE_COMPAT_SAFE_SET		0ULL
188 #define BTRFS_FEATURE_COMPAT_SAFE_CLEAR		0ULL
189 
190 #define BTRFS_FEATURE_COMPAT_RO_SUPP			\
191 	(BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE |	\
192 	 BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE_VALID | \
193 	 BTRFS_FEATURE_COMPAT_RO_VERITY |		\
194 	 BTRFS_FEATURE_COMPAT_RO_BLOCK_GROUP_TREE)
195 
196 #define BTRFS_FEATURE_COMPAT_RO_SAFE_SET	0ULL
197 #define BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR	0ULL
198 
199 #ifdef CONFIG_BTRFS_DEBUG
200 /*
201  * Extent tree v2 supported only with CONFIG_BTRFS_DEBUG
202  */
203 #define BTRFS_FEATURE_INCOMPAT_SUPP			\
204 	(BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF |		\
205 	 BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL |	\
206 	 BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS |		\
207 	 BTRFS_FEATURE_INCOMPAT_BIG_METADATA |		\
208 	 BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO |		\
209 	 BTRFS_FEATURE_INCOMPAT_COMPRESS_ZSTD |		\
210 	 BTRFS_FEATURE_INCOMPAT_RAID56 |		\
211 	 BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF |		\
212 	 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA |	\
213 	 BTRFS_FEATURE_INCOMPAT_NO_HOLES	|	\
214 	 BTRFS_FEATURE_INCOMPAT_METADATA_UUID	|	\
215 	 BTRFS_FEATURE_INCOMPAT_RAID1C34	|	\
216 	 BTRFS_FEATURE_INCOMPAT_ZONED		|	\
217 	 BTRFS_FEATURE_INCOMPAT_EXTENT_TREE_V2)
218 #else
219 #define BTRFS_FEATURE_INCOMPAT_SUPP			\
220 	(BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF |		\
221 	 BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL |	\
222 	 BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS |		\
223 	 BTRFS_FEATURE_INCOMPAT_BIG_METADATA |		\
224 	 BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO |		\
225 	 BTRFS_FEATURE_INCOMPAT_COMPRESS_ZSTD |		\
226 	 BTRFS_FEATURE_INCOMPAT_RAID56 |		\
227 	 BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF |		\
228 	 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA |	\
229 	 BTRFS_FEATURE_INCOMPAT_NO_HOLES	|	\
230 	 BTRFS_FEATURE_INCOMPAT_METADATA_UUID	|	\
231 	 BTRFS_FEATURE_INCOMPAT_RAID1C34	|	\
232 	 BTRFS_FEATURE_INCOMPAT_ZONED)
233 #endif
234 
235 #define BTRFS_FEATURE_INCOMPAT_SAFE_SET			\
236 	(BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF)
237 #define BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR		0ULL
238 
239 #define BTRFS_DEFAULT_COMMIT_INTERVAL	(30)
240 #define BTRFS_DEFAULT_MAX_INLINE	(2048)
241 
242 struct btrfs_dev_replace {
243 	/* See #define above */
244 	u64 replace_state;
245 	/* Seconds since 1-Jan-1970 */
246 	time64_t time_started;
247 	/* Seconds since 1-Jan-1970 */
248 	time64_t time_stopped;
249 	atomic64_t num_write_errors;
250 	atomic64_t num_uncorrectable_read_errors;
251 
252 	u64 cursor_left;
253 	u64 committed_cursor_left;
254 	u64 cursor_left_last_write_of_item;
255 	u64 cursor_right;
256 
257 	/* See #define above */
258 	u64 cont_reading_from_srcdev_mode;
259 
260 	int is_valid;
261 	int item_needs_writeback;
262 	struct btrfs_device *srcdev;
263 	struct btrfs_device *tgtdev;
264 
265 	struct mutex lock_finishing_cancel_unmount;
266 	struct rw_semaphore rwsem;
267 
268 	struct btrfs_scrub_progress scrub_progress;
269 
270 	struct percpu_counter bio_counter;
271 	wait_queue_head_t replace_wait;
272 };
273 
274 /*
275  * Free clusters are used to claim free space in relatively large chunks,
276  * allowing us to do less seeky writes. They are used for all metadata
277  * allocations. In ssd_spread mode they are also used for data allocations.
278  */
279 struct btrfs_free_cluster {
280 	spinlock_t lock;
281 	spinlock_t refill_lock;
282 	struct rb_root root;
283 
284 	/* Largest extent in this cluster */
285 	u64 max_size;
286 
287 	/* First extent starting offset */
288 	u64 window_start;
289 
290 	/* We did a full search and couldn't create a cluster */
291 	bool fragmented;
292 
293 	struct btrfs_block_group *block_group;
294 	/*
295 	 * When a cluster is allocated from a block group, we put the cluster
296 	 * onto a list in the block group so that it can be freed before the
297 	 * block group is freed.
298 	 */
299 	struct list_head block_group_list;
300 };
301 
302 /* Discard control. */
303 /*
304  * Async discard uses multiple lists to differentiate the discard filter
305  * parameters.  Index 0 is for completely free block groups where we need to
306  * ensure the entire block group is trimmed without being lossy.  Indices
307  * afterwards represent monotonically decreasing discard filter sizes to
308  * prioritize what should be discarded next.
309  */
310 #define BTRFS_NR_DISCARD_LISTS		3
311 #define BTRFS_DISCARD_INDEX_UNUSED	0
312 #define BTRFS_DISCARD_INDEX_START	1
313 
314 struct btrfs_discard_ctl {
315 	struct workqueue_struct *discard_workers;
316 	struct delayed_work work;
317 	spinlock_t lock;
318 	struct btrfs_block_group *block_group;
319 	struct list_head discard_list[BTRFS_NR_DISCARD_LISTS];
320 	u64 prev_discard;
321 	u64 prev_discard_time;
322 	atomic_t discardable_extents;
323 	atomic64_t discardable_bytes;
324 	u64 max_discard_size;
325 	u64 delay_ms;
326 	u32 iops_limit;
327 	u32 kbps_limit;
328 	u64 discard_extent_bytes;
329 	u64 discard_bitmap_bytes;
330 	atomic64_t discard_bytes_saved;
331 };
332 
333 /*
334  * Exclusive operations (device replace, resize, device add/remove, balance)
335  */
336 enum btrfs_exclusive_operation {
337 	BTRFS_EXCLOP_NONE,
338 	BTRFS_EXCLOP_BALANCE_PAUSED,
339 	BTRFS_EXCLOP_BALANCE,
340 	BTRFS_EXCLOP_DEV_ADD,
341 	BTRFS_EXCLOP_DEV_REMOVE,
342 	BTRFS_EXCLOP_DEV_REPLACE,
343 	BTRFS_EXCLOP_RESIZE,
344 	BTRFS_EXCLOP_SWAP_ACTIVATE,
345 };
346 
347 /* Store data about transaction commits, exported via sysfs. */
348 struct btrfs_commit_stats {
349 	/* Total number of commits */
350 	u64 commit_count;
351 	/* The maximum commit duration so far in ns */
352 	u64 max_commit_dur;
353 	/* The last commit duration in ns */
354 	u64 last_commit_dur;
355 	/* The total commit duration in ns */
356 	u64 total_commit_dur;
357 };
358 
359 struct btrfs_fs_info {
360 	u8 chunk_tree_uuid[BTRFS_UUID_SIZE];
361 	unsigned long flags;
362 	struct btrfs_root *tree_root;
363 	struct btrfs_root *chunk_root;
364 	struct btrfs_root *dev_root;
365 	struct btrfs_root *fs_root;
366 	struct btrfs_root *quota_root;
367 	struct btrfs_root *uuid_root;
368 	struct btrfs_root *data_reloc_root;
369 	struct btrfs_root *block_group_root;
370 
371 	/* The log root tree is a directory of all the other log roots */
372 	struct btrfs_root *log_root_tree;
373 
374 	/* The tree that holds the global roots (csum, extent, etc) */
375 	rwlock_t global_root_lock;
376 	struct rb_root global_root_tree;
377 
378 	spinlock_t fs_roots_radix_lock;
379 	struct radix_tree_root fs_roots_radix;
380 
381 	/* Block group cache stuff */
382 	rwlock_t block_group_cache_lock;
383 	struct rb_root_cached block_group_cache_tree;
384 
385 	/* Keep track of unallocated space */
386 	atomic64_t free_chunk_space;
387 
388 	/* Track ranges which are used by log trees blocks/logged data extents */
389 	struct extent_io_tree excluded_extents;
390 
391 	/* logical->physical extent mapping */
392 	struct extent_map_tree mapping_tree;
393 
394 	/*
395 	 * Block reservation for extent, checksum, root tree and delayed dir
396 	 * index item.
397 	 */
398 	struct btrfs_block_rsv global_block_rsv;
399 	/* Block reservation for metadata operations */
400 	struct btrfs_block_rsv trans_block_rsv;
401 	/* Block reservation for chunk tree */
402 	struct btrfs_block_rsv chunk_block_rsv;
403 	/* Block reservation for delayed operations */
404 	struct btrfs_block_rsv delayed_block_rsv;
405 	/* Block reservation for delayed refs */
406 	struct btrfs_block_rsv delayed_refs_rsv;
407 
408 	struct btrfs_block_rsv empty_block_rsv;
409 
410 	u64 generation;
411 	u64 last_trans_committed;
412 	/*
413 	 * Generation of the last transaction used for block group relocation
414 	 * since the filesystem was last mounted (or 0 if none happened yet).
415 	 * Must be written and read while holding btrfs_fs_info::commit_root_sem.
416 	 */
417 	u64 last_reloc_trans;
418 	u64 avg_delayed_ref_runtime;
419 
420 	/*
421 	 * This is updated to the current trans every time a full commit is
422 	 * required instead of the faster short fsync log commits
423 	 */
424 	u64 last_trans_log_full_commit;
425 	unsigned long mount_opt;
426 
427 	unsigned long compress_type:4;
428 	unsigned int compress_level;
429 	u32 commit_interval;
430 	/*
431 	 * It is a suggestive number, the read side is safe even it gets a
432 	 * wrong number because we will write out the data into a regular
433 	 * extent. The write side(mount/remount) is under ->s_umount lock,
434 	 * so it is also safe.
435 	 */
436 	u64 max_inline;
437 
438 	struct btrfs_transaction *running_transaction;
439 	wait_queue_head_t transaction_throttle;
440 	wait_queue_head_t transaction_wait;
441 	wait_queue_head_t transaction_blocked_wait;
442 	wait_queue_head_t async_submit_wait;
443 
444 	/*
445 	 * Used to protect the incompat_flags, compat_flags, compat_ro_flags
446 	 * when they are updated.
447 	 *
448 	 * Because we do not clear the flags for ever, so we needn't use
449 	 * the lock on the read side.
450 	 *
451 	 * We also needn't use the lock when we mount the fs, because
452 	 * there is no other task which will update the flag.
453 	 */
454 	spinlock_t super_lock;
455 	struct btrfs_super_block *super_copy;
456 	struct btrfs_super_block *super_for_commit;
457 	struct super_block *sb;
458 	struct inode *btree_inode;
459 	struct mutex tree_log_mutex;
460 	struct mutex transaction_kthread_mutex;
461 	struct mutex cleaner_mutex;
462 	struct mutex chunk_mutex;
463 
464 	/*
465 	 * This is taken to make sure we don't set block groups ro after the
466 	 * free space cache has been allocated on them.
467 	 */
468 	struct mutex ro_block_group_mutex;
469 
470 	/*
471 	 * This is used during read/modify/write to make sure no two ios are
472 	 * trying to mod the same stripe at the same time.
473 	 */
474 	struct btrfs_stripe_hash_table *stripe_hash_table;
475 
476 	/*
477 	 * This protects the ordered operations list only while we are
478 	 * processing all of the entries on it.  This way we make sure the
479 	 * commit code doesn't find the list temporarily empty because another
480 	 * function happens to be doing non-waiting preflush before jumping
481 	 * into the main commit.
482 	 */
483 	struct mutex ordered_operations_mutex;
484 
485 	struct rw_semaphore commit_root_sem;
486 
487 	struct rw_semaphore cleanup_work_sem;
488 
489 	struct rw_semaphore subvol_sem;
490 
491 	spinlock_t trans_lock;
492 	/*
493 	 * The reloc mutex goes with the trans lock, it is taken during commit
494 	 * to protect us from the relocation code.
495 	 */
496 	struct mutex reloc_mutex;
497 
498 	struct list_head trans_list;
499 	struct list_head dead_roots;
500 	struct list_head caching_block_groups;
501 
502 	spinlock_t delayed_iput_lock;
503 	struct list_head delayed_iputs;
504 	atomic_t nr_delayed_iputs;
505 	wait_queue_head_t delayed_iputs_wait;
506 
507 	atomic64_t tree_mod_seq;
508 
509 	/* This protects tree_mod_log and tree_mod_seq_list */
510 	rwlock_t tree_mod_log_lock;
511 	struct rb_root tree_mod_log;
512 	struct list_head tree_mod_seq_list;
513 
514 	atomic_t async_delalloc_pages;
515 
516 	/* This is used to protect the following list -- ordered_roots. */
517 	spinlock_t ordered_root_lock;
518 
519 	/*
520 	 * All fs/file tree roots in which there are data=ordered extents
521 	 * pending writeback are added into this list.
522 	 *
523 	 * These can span multiple transactions and basically include every
524 	 * dirty data page that isn't from nodatacow.
525 	 */
526 	struct list_head ordered_roots;
527 
528 	struct mutex delalloc_root_mutex;
529 	spinlock_t delalloc_root_lock;
530 	/* All fs/file tree roots that have delalloc inodes. */
531 	struct list_head delalloc_roots;
532 
533 	/*
534 	 * There is a pool of worker threads for checksumming during writes and
535 	 * a pool for checksumming after reads.  This is because readers can
536 	 * run with FS locks held, and the writers may be waiting for those
537 	 * locks.  We don't want ordering in the pending list to cause
538 	 * deadlocks, and so the two are serviced separately.
539 	 *
540 	 * A third pool does submit_bio to avoid deadlocking with the other two.
541 	 */
542 	struct btrfs_workqueue *workers;
543 	struct btrfs_workqueue *hipri_workers;
544 	struct btrfs_workqueue *delalloc_workers;
545 	struct btrfs_workqueue *flush_workers;
546 	struct workqueue_struct *endio_workers;
547 	struct workqueue_struct *endio_meta_workers;
548 	struct workqueue_struct *rmw_workers;
549 	struct workqueue_struct *compressed_write_workers;
550 	struct btrfs_workqueue *endio_write_workers;
551 	struct btrfs_workqueue *endio_freespace_worker;
552 	struct btrfs_workqueue *caching_workers;
553 
554 	/*
555 	 * Fixup workers take dirty pages that didn't properly go through the
556 	 * cow mechanism and make them safe to write.  It happens for the
557 	 * sys_munmap function call path.
558 	 */
559 	struct btrfs_workqueue *fixup_workers;
560 	struct btrfs_workqueue *delayed_workers;
561 
562 	struct task_struct *transaction_kthread;
563 	struct task_struct *cleaner_kthread;
564 	u32 thread_pool_size;
565 
566 	struct kobject *space_info_kobj;
567 	struct kobject *qgroups_kobj;
568 	struct kobject *discard_kobj;
569 
570 	/* Used to keep from writing metadata until there is a nice batch */
571 	struct percpu_counter dirty_metadata_bytes;
572 	struct percpu_counter delalloc_bytes;
573 	struct percpu_counter ordered_bytes;
574 	s32 dirty_metadata_batch;
575 	s32 delalloc_batch;
576 
577 	struct list_head dirty_cowonly_roots;
578 
579 	struct btrfs_fs_devices *fs_devices;
580 
581 	/*
582 	 * The space_info list is effectively read only after initial setup.
583 	 * It is populated at mount time and cleaned up after all block groups
584 	 * are removed.  RCU is used to protect it.
585 	 */
586 	struct list_head space_info;
587 
588 	struct btrfs_space_info *data_sinfo;
589 
590 	struct reloc_control *reloc_ctl;
591 
592 	/* data_alloc_cluster is only used in ssd_spread mode */
593 	struct btrfs_free_cluster data_alloc_cluster;
594 
595 	/* All metadata allocations go through this cluster. */
596 	struct btrfs_free_cluster meta_alloc_cluster;
597 
598 	/* Auto defrag inodes go here. */
599 	spinlock_t defrag_inodes_lock;
600 	struct rb_root defrag_inodes;
601 	atomic_t defrag_running;
602 
603 	/* Used to protect avail_{data, metadata, system}_alloc_bits */
604 	seqlock_t profiles_lock;
605 	/*
606 	 * These three are in extended format (availability of single chunks is
607 	 * denoted by BTRFS_AVAIL_ALLOC_BIT_SINGLE bit, other types are denoted
608 	 * by corresponding BTRFS_BLOCK_GROUP_* bits)
609 	 */
610 	u64 avail_data_alloc_bits;
611 	u64 avail_metadata_alloc_bits;
612 	u64 avail_system_alloc_bits;
613 
614 	/* Balance state */
615 	spinlock_t balance_lock;
616 	struct mutex balance_mutex;
617 	atomic_t balance_pause_req;
618 	atomic_t balance_cancel_req;
619 	struct btrfs_balance_control *balance_ctl;
620 	wait_queue_head_t balance_wait_q;
621 
622 	/* Cancellation requests for chunk relocation */
623 	atomic_t reloc_cancel_req;
624 
625 	u32 data_chunk_allocations;
626 	u32 metadata_ratio;
627 
628 	void *bdev_holder;
629 
630 	/* Private scrub information */
631 	struct mutex scrub_lock;
632 	atomic_t scrubs_running;
633 	atomic_t scrub_pause_req;
634 	atomic_t scrubs_paused;
635 	atomic_t scrub_cancel_req;
636 	wait_queue_head_t scrub_pause_wait;
637 	/*
638 	 * The worker pointers are NULL iff the refcount is 0, ie. scrub is not
639 	 * running.
640 	 */
641 	refcount_t scrub_workers_refcnt;
642 	struct workqueue_struct *scrub_workers;
643 	struct workqueue_struct *scrub_wr_completion_workers;
644 	struct workqueue_struct *scrub_parity_workers;
645 	struct btrfs_subpage_info *subpage_info;
646 
647 	struct btrfs_discard_ctl discard_ctl;
648 
649 #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
650 	u32 check_integrity_print_mask;
651 #endif
652 	/* Is qgroup tracking in a consistent state? */
653 	u64 qgroup_flags;
654 
655 	/* Holds configuration and tracking. Protected by qgroup_lock. */
656 	struct rb_root qgroup_tree;
657 	spinlock_t qgroup_lock;
658 
659 	/*
660 	 * Used to avoid frequently calling ulist_alloc()/ulist_free()
661 	 * when doing qgroup accounting, it must be protected by qgroup_lock.
662 	 */
663 	struct ulist *qgroup_ulist;
664 
665 	/*
666 	 * Protect user change for quota operations. If a transaction is needed,
667 	 * it must be started before locking this lock.
668 	 */
669 	struct mutex qgroup_ioctl_lock;
670 
671 	/* List of dirty qgroups to be written at next commit. */
672 	struct list_head dirty_qgroups;
673 
674 	/* Used by qgroup for an efficient tree traversal. */
675 	u64 qgroup_seq;
676 
677 	/* Qgroup rescan items. */
678 	/* Protects the progress item */
679 	struct mutex qgroup_rescan_lock;
680 	struct btrfs_key qgroup_rescan_progress;
681 	struct btrfs_workqueue *qgroup_rescan_workers;
682 	struct completion qgroup_rescan_completion;
683 	struct btrfs_work qgroup_rescan_work;
684 	/* Protected by qgroup_rescan_lock */
685 	bool qgroup_rescan_running;
686 	u8 qgroup_drop_subtree_thres;
687 
688 	/* Filesystem state */
689 	unsigned long fs_state;
690 
691 	struct btrfs_delayed_root *delayed_root;
692 
693 	/* Extent buffer radix tree */
694 	spinlock_t buffer_lock;
695 	/* Entries are eb->start / sectorsize */
696 	struct radix_tree_root buffer_radix;
697 
698 	/* Next backup root to be overwritten */
699 	int backup_root_index;
700 
701 	/* Device replace state */
702 	struct btrfs_dev_replace dev_replace;
703 
704 	struct semaphore uuid_tree_rescan_sem;
705 
706 	/* Used to reclaim the metadata space in the background. */
707 	struct work_struct async_reclaim_work;
708 	struct work_struct async_data_reclaim_work;
709 	struct work_struct preempt_reclaim_work;
710 
711 	/* Reclaim partially filled block groups in the background */
712 	struct work_struct reclaim_bgs_work;
713 	struct list_head reclaim_bgs;
714 	int bg_reclaim_threshold;
715 
716 	spinlock_t unused_bgs_lock;
717 	struct list_head unused_bgs;
718 	struct mutex unused_bg_unpin_mutex;
719 	/* Protect block groups that are going to be deleted */
720 	struct mutex reclaim_bgs_lock;
721 
722 	/* Cached block sizes */
723 	u32 nodesize;
724 	u32 sectorsize;
725 	/* ilog2 of sectorsize, use to avoid 64bit division */
726 	u32 sectorsize_bits;
727 	u32 csum_size;
728 	u32 csums_per_leaf;
729 	u32 stripesize;
730 
731 	/*
732 	 * Maximum size of an extent. BTRFS_MAX_EXTENT_SIZE on regular
733 	 * filesystem, on zoned it depends on the device constraints.
734 	 */
735 	u64 max_extent_size;
736 
737 	/* Block groups and devices containing active swapfiles. */
738 	spinlock_t swapfile_pins_lock;
739 	struct rb_root swapfile_pins;
740 
741 	struct crypto_shash *csum_shash;
742 
743 	/* Type of exclusive operation running, protected by super_lock */
744 	enum btrfs_exclusive_operation exclusive_operation;
745 
746 	/*
747 	 * Zone size > 0 when in ZONED mode, otherwise it's used for a check
748 	 * if the mode is enabled
749 	 */
750 	u64 zone_size;
751 
752 	/* Constraints for ZONE_APPEND commands: */
753 	struct queue_limits limits;
754 	u64 max_zone_append_size;
755 
756 	struct mutex zoned_meta_io_lock;
757 	spinlock_t treelog_bg_lock;
758 	u64 treelog_bg;
759 
760 	/*
761 	 * Start of the dedicated data relocation block group, protected by
762 	 * relocation_bg_lock.
763 	 */
764 	spinlock_t relocation_bg_lock;
765 	u64 data_reloc_bg;
766 	struct mutex zoned_data_reloc_io_lock;
767 
768 	u64 nr_global_roots;
769 
770 	spinlock_t zone_active_bgs_lock;
771 	struct list_head zone_active_bgs;
772 
773 	/* Updates are not protected by any lock */
774 	struct btrfs_commit_stats commit_stats;
775 
776 	/*
777 	 * Last generation where we dropped a non-relocation root.
778 	 * Use btrfs_set_last_root_drop_gen() and btrfs_get_last_root_drop_gen()
779 	 * to change it and to read it, respectively.
780 	 */
781 	u64 last_root_drop_gen;
782 
783 	/*
784 	 * Annotations for transaction events (structures are empty when
785 	 * compiled without lockdep).
786 	 */
787 	struct lockdep_map btrfs_trans_num_writers_map;
788 	struct lockdep_map btrfs_trans_num_extwriters_map;
789 	struct lockdep_map btrfs_state_change_map[4];
790 	struct lockdep_map btrfs_trans_pending_ordered_map;
791 	struct lockdep_map btrfs_ordered_extent_map;
792 
793 #ifdef CONFIG_BTRFS_FS_REF_VERIFY
794 	spinlock_t ref_verify_lock;
795 	struct rb_root block_tree;
796 #endif
797 
798 #ifdef CONFIG_BTRFS_DEBUG
799 	struct kobject *debug_kobj;
800 	struct list_head allocated_roots;
801 
802 	spinlock_t eb_leak_lock;
803 	struct list_head allocated_ebs;
804 #endif
805 };
806 
btrfs_set_last_root_drop_gen(struct btrfs_fs_info * fs_info,u64 gen)807 static inline void btrfs_set_last_root_drop_gen(struct btrfs_fs_info *fs_info,
808 						u64 gen)
809 {
810 	WRITE_ONCE(fs_info->last_root_drop_gen, gen);
811 }
812 
btrfs_get_last_root_drop_gen(const struct btrfs_fs_info * fs_info)813 static inline u64 btrfs_get_last_root_drop_gen(const struct btrfs_fs_info *fs_info)
814 {
815 	return READ_ONCE(fs_info->last_root_drop_gen);
816 }
817 
818 /*
819  * Take the number of bytes to be checksummed and figure out how many leaves
820  * it would require to store the csums for that many bytes.
821  */
btrfs_csum_bytes_to_leaves(const struct btrfs_fs_info * fs_info,u64 csum_bytes)822 static inline u64 btrfs_csum_bytes_to_leaves(
823 			const struct btrfs_fs_info *fs_info, u64 csum_bytes)
824 {
825 	const u64 num_csums = csum_bytes >> fs_info->sectorsize_bits;
826 
827 	return DIV_ROUND_UP_ULL(num_csums, fs_info->csums_per_leaf);
828 }
829 
830 /*
831  * Use this if we would be adding new items, as we could split nodes as we cow
832  * down the tree.
833  */
btrfs_calc_insert_metadata_size(struct btrfs_fs_info * fs_info,unsigned num_items)834 static inline u64 btrfs_calc_insert_metadata_size(struct btrfs_fs_info *fs_info,
835 						  unsigned num_items)
836 {
837 	return (u64)fs_info->nodesize * BTRFS_MAX_LEVEL * 2 * num_items;
838 }
839 
840 /*
841  * Doing a truncate or a modification won't result in new nodes or leaves, just
842  * what we need for COW.
843  */
btrfs_calc_metadata_size(struct btrfs_fs_info * fs_info,unsigned num_items)844 static inline u64 btrfs_calc_metadata_size(struct btrfs_fs_info *fs_info,
845 						 unsigned num_items)
846 {
847 	return (u64)fs_info->nodesize * BTRFS_MAX_LEVEL * num_items;
848 }
849 
850 #define BTRFS_MAX_EXTENT_ITEM_SIZE(r) ((BTRFS_LEAF_DATA_SIZE(r->fs_info) >> 4) - \
851 					sizeof(struct btrfs_item))
852 
btrfs_is_zoned(const struct btrfs_fs_info * fs_info)853 static inline bool btrfs_is_zoned(const struct btrfs_fs_info *fs_info)
854 {
855 	return fs_info->zone_size > 0;
856 }
857 
858 /*
859  * Count how many fs_info->max_extent_size cover the @size
860  */
count_max_extents(struct btrfs_fs_info * fs_info,u64 size)861 static inline u32 count_max_extents(struct btrfs_fs_info *fs_info, u64 size)
862 {
863 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
864 	if (!fs_info)
865 		return div_u64(size + BTRFS_MAX_EXTENT_SIZE - 1, BTRFS_MAX_EXTENT_SIZE);
866 #endif
867 
868 	return div_u64(size + fs_info->max_extent_size - 1, fs_info->max_extent_size);
869 }
870 
871 bool btrfs_exclop_start(struct btrfs_fs_info *fs_info,
872 			enum btrfs_exclusive_operation type);
873 bool btrfs_exclop_start_try_lock(struct btrfs_fs_info *fs_info,
874 				 enum btrfs_exclusive_operation type);
875 void btrfs_exclop_start_unlock(struct btrfs_fs_info *fs_info);
876 void btrfs_exclop_finish(struct btrfs_fs_info *fs_info);
877 void btrfs_exclop_balance(struct btrfs_fs_info *fs_info,
878 			  enum btrfs_exclusive_operation op);
879 
880 /* Compatibility and incompatibility defines */
881 void __btrfs_set_fs_incompat(struct btrfs_fs_info *fs_info, u64 flag,
882 			     const char *name);
883 void __btrfs_clear_fs_incompat(struct btrfs_fs_info *fs_info, u64 flag,
884 			       const char *name);
885 void __btrfs_set_fs_compat_ro(struct btrfs_fs_info *fs_info, u64 flag,
886 			      const char *name);
887 void __btrfs_clear_fs_compat_ro(struct btrfs_fs_info *fs_info, u64 flag,
888 				const char *name);
889 
890 #define __btrfs_fs_incompat(fs_info, flags)				\
891 	(!!(btrfs_super_incompat_flags((fs_info)->super_copy) & (flags)))
892 
893 #define __btrfs_fs_compat_ro(fs_info, flags)				\
894 	(!!(btrfs_super_compat_ro_flags((fs_info)->super_copy) & (flags)))
895 
896 #define btrfs_set_fs_incompat(__fs_info, opt)				\
897 	__btrfs_set_fs_incompat((__fs_info), BTRFS_FEATURE_INCOMPAT_##opt, #opt)
898 
899 #define btrfs_clear_fs_incompat(__fs_info, opt)				\
900 	__btrfs_clear_fs_incompat((__fs_info), BTRFS_FEATURE_INCOMPAT_##opt, #opt)
901 
902 #define btrfs_fs_incompat(fs_info, opt)					\
903 	__btrfs_fs_incompat((fs_info), BTRFS_FEATURE_INCOMPAT_##opt)
904 
905 #define btrfs_set_fs_compat_ro(__fs_info, opt)				\
906 	__btrfs_set_fs_compat_ro((__fs_info), BTRFS_FEATURE_COMPAT_RO_##opt, #opt)
907 
908 #define btrfs_clear_fs_compat_ro(__fs_info, opt)			\
909 	__btrfs_clear_fs_compat_ro((__fs_info), BTRFS_FEATURE_COMPAT_RO_##opt, #opt)
910 
911 #define btrfs_fs_compat_ro(fs_info, opt)				\
912 	__btrfs_fs_compat_ro((fs_info), BTRFS_FEATURE_COMPAT_RO_##opt)
913 
914 #define btrfs_clear_opt(o, opt)		((o) &= ~BTRFS_MOUNT_##opt)
915 #define btrfs_set_opt(o, opt)		((o) |= BTRFS_MOUNT_##opt)
916 #define btrfs_raw_test_opt(o, opt)	((o) & BTRFS_MOUNT_##opt)
917 #define btrfs_test_opt(fs_info, opt)	((fs_info)->mount_opt & \
918 					 BTRFS_MOUNT_##opt)
919 
920 #define btrfs_set_and_info(fs_info, opt, fmt, args...)			\
921 do {									\
922 	if (!btrfs_test_opt(fs_info, opt))				\
923 		btrfs_info(fs_info, fmt, ##args);			\
924 	btrfs_set_opt(fs_info->mount_opt, opt);				\
925 } while (0)
926 
927 #define btrfs_clear_and_info(fs_info, opt, fmt, args...)		\
928 do {									\
929 	if (btrfs_test_opt(fs_info, opt))				\
930 		btrfs_info(fs_info, fmt, ##args);			\
931 	btrfs_clear_opt(fs_info->mount_opt, opt);			\
932 } while (0)
933 
btrfs_fs_closing(struct btrfs_fs_info * fs_info)934 static inline int btrfs_fs_closing(struct btrfs_fs_info *fs_info)
935 {
936 	/* Do it this way so we only ever do one test_bit in the normal case. */
937 	if (test_bit(BTRFS_FS_CLOSING_START, &fs_info->flags)) {
938 		if (test_bit(BTRFS_FS_CLOSING_DONE, &fs_info->flags))
939 			return 2;
940 		return 1;
941 	}
942 	return 0;
943 }
944 
945 /*
946  * If we remount the fs to be R/O or umount the fs, the cleaner needn't do
947  * anything except sleeping. This function is used to check the status of
948  * the fs.
949  * We check for BTRFS_FS_STATE_RO to avoid races with a concurrent remount,
950  * since setting and checking for SB_RDONLY in the superblock's flags is not
951  * atomic.
952  */
btrfs_need_cleaner_sleep(struct btrfs_fs_info * fs_info)953 static inline int btrfs_need_cleaner_sleep(struct btrfs_fs_info *fs_info)
954 {
955 	return test_bit(BTRFS_FS_STATE_RO, &fs_info->fs_state) ||
956 		btrfs_fs_closing(fs_info);
957 }
958 
btrfs_wake_unfinished_drop(struct btrfs_fs_info * fs_info)959 static inline void btrfs_wake_unfinished_drop(struct btrfs_fs_info *fs_info)
960 {
961 	clear_and_wake_up_bit(BTRFS_FS_UNFINISHED_DROPS, &fs_info->flags);
962 }
963 
964 #define BTRFS_FS_ERROR(fs_info)	(unlikely(test_bit(BTRFS_FS_STATE_ERROR, \
965 						   &(fs_info)->fs_state)))
966 #define BTRFS_FS_LOG_CLEANUP_ERROR(fs_info)				\
967 	(unlikely(test_bit(BTRFS_FS_STATE_LOG_CLEANUP_ERROR,		\
968 			   &(fs_info)->fs_state)))
969 
970 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
971 
972 #define EXPORT_FOR_TESTS
973 
btrfs_is_testing(struct btrfs_fs_info * fs_info)974 static inline int btrfs_is_testing(struct btrfs_fs_info *fs_info)
975 {
976 	return test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state);
977 }
978 
979 void btrfs_test_destroy_inode(struct inode *inode);
980 
981 #else
982 
983 #define EXPORT_FOR_TESTS static
984 
btrfs_is_testing(struct btrfs_fs_info * fs_info)985 static inline int btrfs_is_testing(struct btrfs_fs_info *fs_info)
986 {
987 	return 0;
988 }
989 #endif
990 
991 #endif
992