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