1 /* SPDX-License-Identifier: GPL-2.0 */ 2 3 #ifndef BTRFS_BLOCK_GROUP_H 4 #define BTRFS_BLOCK_GROUP_H 5 6 #include <linux/atomic.h> 7 #include <linux/mutex.h> 8 #include <linux/list.h> 9 #include <linux/spinlock.h> 10 #include <linux/refcount.h> 11 #include <linux/wait.h> 12 #include <linux/sizes.h> 13 #include <linux/rwsem.h> 14 #include <linux/rbtree.h> 15 #include <uapi/linux/btrfs_tree.h> 16 #include "free-space-cache.h" 17 18 struct btrfs_chunk_map; 19 struct btrfs_fs_info; 20 struct btrfs_inode; 21 struct btrfs_trans_handle; 22 23 enum btrfs_disk_cache_state { 24 BTRFS_DC_WRITTEN, 25 BTRFS_DC_ERROR, 26 BTRFS_DC_CLEAR, 27 BTRFS_DC_SETUP, 28 }; 29 30 enum btrfs_block_group_size_class { 31 /* Unset */ 32 BTRFS_BG_SZ_NONE, 33 /* 0 < size <= 128K */ 34 BTRFS_BG_SZ_SMALL, 35 /* 128K < size <= 8M */ 36 BTRFS_BG_SZ_MEDIUM, 37 /* 8M < size < BG_LENGTH */ 38 BTRFS_BG_SZ_LARGE, 39 }; 40 41 /* 42 * This describes the state of the block_group for async discard. This is due 43 * to the two pass nature of it where extent discarding is prioritized over 44 * bitmap discarding. BTRFS_DISCARD_RESET_CURSOR is set when we are resetting 45 * between lists to prevent contention for discard state variables 46 * (eg. discard_cursor). 47 */ 48 enum btrfs_discard_state { 49 BTRFS_DISCARD_EXTENTS, 50 BTRFS_DISCARD_BITMAPS, 51 BTRFS_DISCARD_RESET_CURSOR, 52 BTRFS_DISCARD_FULLY_REMAPPED, 53 }; 54 55 /* 56 * Control flags for do_chunk_alloc's force field CHUNK_ALLOC_NO_FORCE means to 57 * only allocate a chunk if we really need one. 58 * 59 * CHUNK_ALLOC_LIMITED means to only try and allocate one if we have very few 60 * chunks already allocated. This is used as part of the clustering code to 61 * help make sure we have a good pool of storage to cluster in, without filling 62 * the FS with empty chunks 63 * 64 * CHUNK_ALLOC_FORCE means it must try to allocate one 65 * 66 * CHUNK_ALLOC_FORCE_FOR_EXTENT like CHUNK_ALLOC_FORCE but called from 67 * find_free_extent() that also activates the zone 68 */ 69 enum btrfs_chunk_alloc_enum { 70 CHUNK_ALLOC_NO_FORCE, 71 CHUNK_ALLOC_LIMITED, 72 CHUNK_ALLOC_FORCE, 73 CHUNK_ALLOC_FORCE_FOR_EXTENT, 74 }; 75 76 /* Block group flags set at runtime */ 77 enum btrfs_block_group_flags { 78 BLOCK_GROUP_FLAG_IREF, 79 BLOCK_GROUP_FLAG_REMOVED, 80 BLOCK_GROUP_FLAG_TO_COPY, 81 BLOCK_GROUP_FLAG_RELOCATING_REPAIR, 82 BLOCK_GROUP_FLAG_CHUNK_ITEM_INSERTED, 83 BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, 84 BLOCK_GROUP_FLAG_ZONED_DATA_RELOC, 85 /* Does the block group need to be added to the free space tree? */ 86 BLOCK_GROUP_FLAG_NEEDS_FREE_SPACE, 87 /* Set after we add a new block group to the free space tree. */ 88 BLOCK_GROUP_FLAG_FREE_SPACE_ADDED, 89 /* Indicate that the block group is placed on a sequential zone */ 90 BLOCK_GROUP_FLAG_SEQUENTIAL_ZONE, 91 /* 92 * Indicate that block group is in the list of new block groups of a 93 * transaction. 94 */ 95 BLOCK_GROUP_FLAG_NEW, 96 BLOCK_GROUP_FLAG_FULLY_REMAPPED, 97 BLOCK_GROUP_FLAG_STRIPE_REMOVAL_PENDING, 98 }; 99 100 enum btrfs_caching_type { 101 BTRFS_CACHE_NO, 102 BTRFS_CACHE_STARTED, 103 BTRFS_CACHE_FINISHED, 104 BTRFS_CACHE_ERROR, 105 }; 106 107 struct btrfs_caching_control { 108 struct list_head list; 109 struct mutex mutex; 110 wait_queue_head_t wait; 111 struct btrfs_work work; 112 struct btrfs_block_group *block_group; 113 /* Track progress of caching during allocation. */ 114 atomic_t progress; 115 refcount_t count; 116 }; 117 118 /* Once caching_thread() finds this much free space, it will wake up waiters. */ 119 #define CACHING_CTL_WAKE_UP SZ_2M 120 121 struct btrfs_block_group { 122 struct btrfs_fs_info *fs_info; 123 struct btrfs_inode *inode; 124 spinlock_t lock; 125 unsigned int ro; 126 u64 start; 127 u64 length; 128 u64 pinned; 129 u64 reserved; 130 u64 used; 131 u64 delalloc_bytes; 132 u64 bytes_super; 133 u64 flags; 134 u64 cache_generation; 135 u64 global_root_id; 136 u64 remap_bytes; 137 u32 identity_remap_count; 138 /* The last committed identity_remap_count value of this block group. */ 139 u32 last_identity_remap_count; 140 /* 141 * The last committed used bytes of this block group, if the above @used 142 * is still the same as @last_used, we don't need to update block 143 * group item of this block group. 144 */ 145 u64 last_used; 146 /* The last committed remap_bytes value of this block group. */ 147 u64 last_remap_bytes; 148 /* The last committed flags value for this block group. */ 149 u64 last_flags; 150 151 /* 152 * If the free space extent count exceeds this number, convert the block 153 * group to bitmaps. 154 */ 155 u32 bitmap_high_thresh; 156 157 /* 158 * If the free space extent count drops below this number, convert the 159 * block group back to extents. 160 */ 161 u32 bitmap_low_thresh; 162 163 /* 164 * It is just used for the delayed data space allocation because 165 * only the data space allocation and the relative metadata update 166 * can be done cross the transaction. 167 */ 168 struct rw_semaphore data_rwsem; 169 170 /* For raid56, this is a full stripe, without parity */ 171 unsigned long full_stripe_len; 172 unsigned long runtime_flags; 173 174 enum btrfs_disk_cache_state disk_cache_state; 175 176 /* Cache tracking stuff */ 177 enum btrfs_caching_type cached; 178 struct btrfs_caching_control *caching_ctl; 179 180 struct btrfs_space_info *space_info; 181 182 /* Free space cache stuff */ 183 struct btrfs_free_space_ctl *free_space_ctl; 184 185 /* Block group cache stuff */ 186 struct rb_node cache_node; 187 188 /* For block groups in the same raid type */ 189 struct list_head list; 190 191 refcount_t refs; 192 193 /* 194 * When non-zero it means the block group's logical address and its 195 * device extents can not be reused for future block group allocations 196 * until the counter goes down to 0. This is to prevent them from being 197 * reused while some task is still using the block group after it was 198 * deleted - we want to make sure they can only be reused for new block 199 * groups after that task is done with the deleted block group. 200 */ 201 atomic_t frozen; 202 203 /* 204 * List of struct btrfs_free_clusters for this block group. 205 * Today it will only have one thing on it, but that may change 206 */ 207 struct list_head cluster_list; 208 209 /* 210 * Used for several lists: 211 * 212 * 1) struct btrfs_fs_info::unused_bgs 213 * 2) struct btrfs_fs_info::reclaim_bgs 214 * 3) struct btrfs_transaction::deleted_bgs 215 * 4) struct btrfs_trans_handle::new_bgs 216 */ 217 struct list_head bg_list; 218 219 /* For read-only block groups */ 220 struct list_head ro_list; 221 222 /* For discard operations */ 223 struct list_head discard_list; 224 int discard_index; 225 enum btrfs_discard_state discard_state; 226 u64 discard_eligible_time; 227 u64 discard_cursor; 228 229 /* For dirty block groups */ 230 struct list_head dirty_list; 231 struct list_head io_list; 232 233 struct btrfs_io_ctl io_ctl; 234 235 /* 236 * Incremented when doing extent allocations and holding a read lock 237 * on the space_info's groups_sem semaphore. 238 * Decremented when an ordered extent that represents an IO against this 239 * block group's range is created (after it's added to its inode's 240 * root's list of ordered extents) or immediately after the allocation 241 * if it's a metadata extent or fallocate extent (for these cases we 242 * don't create ordered extents). 243 */ 244 atomic_t reservations; 245 246 /* 247 * Incremented while holding the spinlock *lock* by a task checking if 248 * it can perform a nocow write (incremented if the value for the *ro* 249 * field is 0). Decremented by such tasks once they create an ordered 250 * extent or before that if some error happens before reaching that step. 251 * This is to prevent races between block group relocation and nocow 252 * writes through direct IO. 253 */ 254 atomic_t nocow_writers; 255 256 /* Lock for free space tree operations. */ 257 struct mutex free_space_lock; 258 259 /* Protected by @free_space_lock. */ 260 bool using_free_space_bitmaps; 261 /* Protected by @free_space_lock. */ 262 bool using_free_space_bitmaps_cached; 263 264 enum btrfs_block_group_size_class size_class:8; 265 266 /* If set, this blockgroup is not used for allocation between two reclaim sweeps. */ 267 bool reclaim_mark; 268 269 /* 270 * Number of extents in this block group used for swap files. 271 * All accesses protected by the spinlock 'lock'. 272 */ 273 int swap_extents; 274 275 /* 276 * Allocation offset for the block group to implement sequential 277 * allocation. This is used only on a zoned filesystem. 278 */ 279 u64 alloc_offset; 280 u64 zone_unusable; 281 u64 zone_capacity; 282 u64 meta_write_pointer; 283 struct btrfs_chunk_map *physical_map; 284 struct list_head active_bg_list; 285 struct work_struct zone_finish_work; 286 struct extent_buffer *last_eb; 287 }; 288 289 static inline u64 btrfs_block_group_end(const struct btrfs_block_group *block_group) 290 { 291 return (block_group->start + block_group->length); 292 } 293 294 static inline bool btrfs_is_block_group_used(const struct btrfs_block_group *bg) 295 { 296 lockdep_assert_held(&bg->lock); 297 298 return (bg->used > 0 || bg->reserved > 0 || bg->pinned > 0 || 299 bg->remap_bytes > 0); 300 } 301 302 static inline bool btrfs_is_block_group_data_only(const struct btrfs_block_group *block_group) 303 { 304 /* 305 * In mixed mode the fragmentation is expected to be high, lowering the 306 * efficiency, so only proper data block groups are considered. 307 */ 308 return (block_group->flags & BTRFS_BLOCK_GROUP_DATA) && 309 !(block_group->flags & BTRFS_BLOCK_GROUP_METADATA); 310 } 311 312 static inline u64 btrfs_block_group_available_space(const struct btrfs_block_group *bg) 313 { 314 lockdep_assert_held(&bg->lock); 315 316 return (bg->length - bg->used - bg->pinned - bg->reserved - 317 bg->bytes_super - bg->zone_unusable); 318 } 319 320 #ifdef CONFIG_BTRFS_DEBUG 321 int btrfs_should_fragment_free_space(const struct btrfs_block_group *block_group); 322 #endif 323 324 int __init btrfs_init_block_group(void); 325 void __cold btrfs_exit_block_group(void); 326 327 struct btrfs_block_group *btrfs_lookup_first_block_group( 328 struct btrfs_fs_info *info, u64 bytenr); 329 struct btrfs_block_group *btrfs_lookup_block_group( 330 struct btrfs_fs_info *info, u64 bytenr); 331 struct btrfs_block_group *btrfs_next_block_group( 332 struct btrfs_block_group *cache); 333 void btrfs_get_block_group(struct btrfs_block_group *cache); 334 void btrfs_put_block_group(struct btrfs_block_group *cache); 335 void btrfs_dec_block_group_reservations(struct btrfs_fs_info *fs_info, 336 const u64 start); 337 void btrfs_wait_block_group_reservations(struct btrfs_block_group *bg); 338 struct btrfs_block_group *btrfs_inc_nocow_writers(struct btrfs_fs_info *fs_info, 339 u64 bytenr); 340 void btrfs_dec_nocow_writers(struct btrfs_block_group *bg); 341 void btrfs_wait_nocow_writers(struct btrfs_block_group *bg); 342 void btrfs_wait_block_group_cache_progress(struct btrfs_block_group *cache, 343 u64 num_bytes); 344 int btrfs_cache_block_group(struct btrfs_block_group *cache, bool wait); 345 struct btrfs_caching_control *btrfs_get_caching_control( 346 struct btrfs_block_group *cache); 347 int btrfs_add_new_free_space(struct btrfs_block_group *block_group, 348 u64 start, u64 end, u64 *total_added_ret); 349 struct btrfs_trans_handle *btrfs_start_trans_remove_block_group( 350 struct btrfs_fs_info *fs_info, 351 const u64 chunk_offset); 352 void btrfs_remove_bg_from_sinfo(struct btrfs_block_group *bg); 353 int btrfs_remove_block_group(struct btrfs_trans_handle *trans, 354 struct btrfs_chunk_map *map); 355 void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info); 356 void btrfs_mark_bg_unused(struct btrfs_block_group *bg); 357 void btrfs_reclaim_block_groups(struct btrfs_fs_info *fs_info, unsigned int limit); 358 void btrfs_reclaim_bgs_work(struct work_struct *work); 359 void btrfs_reclaim_bgs(struct btrfs_fs_info *fs_info); 360 void btrfs_mark_bg_to_reclaim(struct btrfs_block_group *bg); 361 int btrfs_read_block_groups(struct btrfs_fs_info *info); 362 struct btrfs_block_group *btrfs_make_block_group(struct btrfs_trans_handle *trans, 363 struct btrfs_space_info *space_info, 364 u64 type, u64 chunk_offset, u64 size); 365 void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans); 366 int btrfs_inc_block_group_ro(struct btrfs_block_group *cache, 367 bool do_chunk_alloc); 368 void btrfs_dec_block_group_ro(struct btrfs_block_group *cache); 369 int btrfs_start_dirty_block_groups(struct btrfs_trans_handle *trans); 370 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans); 371 int btrfs_setup_space_cache(struct btrfs_trans_handle *trans); 372 int btrfs_update_block_group(struct btrfs_trans_handle *trans, 373 u64 bytenr, u64 num_bytes, bool alloc); 374 int btrfs_add_reserved_bytes(struct btrfs_block_group *cache, 375 u64 ram_bytes, u64 num_bytes, bool delalloc, 376 bool force_wrong_size_class); 377 void btrfs_free_reserved_bytes(struct btrfs_block_group *cache, u64 num_bytes, 378 bool is_delalloc); 379 int btrfs_chunk_alloc(struct btrfs_trans_handle *trans, 380 struct btrfs_space_info *space_info, u64 flags, 381 enum btrfs_chunk_alloc_enum force); 382 int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans, u64 type); 383 void check_system_chunk(struct btrfs_trans_handle *trans, const u64 type); 384 void btrfs_reserve_chunk_metadata(struct btrfs_trans_handle *trans, 385 bool is_item_insertion); 386 u64 btrfs_get_alloc_profile(struct btrfs_fs_info *fs_info, u64 orig_flags); 387 void btrfs_put_block_group_cache(struct btrfs_fs_info *info); 388 int btrfs_free_block_groups(struct btrfs_fs_info *info); 389 int btrfs_rmap_block(struct btrfs_fs_info *fs_info, u64 chunk_start, 390 u64 physical, u64 **logical, int *naddrs, int *stripe_len); 391 392 static inline u64 btrfs_data_alloc_profile(struct btrfs_fs_info *fs_info) 393 { 394 return btrfs_get_alloc_profile(fs_info, BTRFS_BLOCK_GROUP_DATA); 395 } 396 397 static inline u64 btrfs_metadata_alloc_profile(struct btrfs_fs_info *fs_info) 398 { 399 return btrfs_get_alloc_profile(fs_info, BTRFS_BLOCK_GROUP_METADATA); 400 } 401 402 static inline u64 btrfs_system_alloc_profile(struct btrfs_fs_info *fs_info) 403 { 404 return btrfs_get_alloc_profile(fs_info, BTRFS_BLOCK_GROUP_SYSTEM); 405 } 406 407 static inline int btrfs_block_group_done(const struct btrfs_block_group *cache) 408 { 409 smp_mb(); 410 return cache->cached == BTRFS_CACHE_FINISHED || 411 cache->cached == BTRFS_CACHE_ERROR; 412 } 413 414 void btrfs_freeze_block_group(struct btrfs_block_group *cache); 415 void btrfs_unfreeze_block_group(struct btrfs_block_group *cache); 416 417 bool btrfs_inc_block_group_swap_extents(struct btrfs_block_group *bg); 418 void btrfs_dec_block_group_swap_extents(struct btrfs_block_group *bg, int amount); 419 420 enum btrfs_block_group_size_class btrfs_calc_block_group_size_class(u64 size); 421 int btrfs_use_block_group_size_class(struct btrfs_block_group *bg, 422 enum btrfs_block_group_size_class size_class, 423 bool force_wrong_size_class); 424 bool btrfs_block_group_should_use_size_class(const struct btrfs_block_group *bg); 425 void btrfs_mark_bg_fully_remapped(struct btrfs_block_group *bg, 426 struct btrfs_trans_handle *trans); 427 int btrfs_populate_fully_remapped_bgs_list(struct btrfs_fs_info *fs_info); 428 429 #endif /* BTRFS_BLOCK_GROUP_H */ 430