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