Linux Audio

Check our new training course

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