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