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