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 u64 start; 126 u64 length; 127 u64 pinned; 128 u64 reserved; 129 u64 used; 130 u64 delalloc_bytes; 131 u64 bytes_super; 132 u64 flags; 133 u64 cache_generation; 134 u64 global_root_id; 135 u64 remap_bytes; 136 u32 identity_remap_count; 137 138 /* 139 * The last committed used bytes of this block group, if the above @used 140 * is still the same as @last_used, we don't need to update block 141 * group item of this block group. 142 */ 143 u64 last_used; 144 /* The last committed remap_bytes value of this block group. */ 145 u64 last_remap_bytes; 146 /* The last commited identity_remap_count value of this block group. */ 147 u32 last_identity_remap_count; 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 unsigned int ro; 175 176 int disk_cache_state; 177 178 /* Cache tracking stuff */ 179 int cached; 180 struct btrfs_caching_control *caching_ctl; 181 182 struct btrfs_space_info *space_info; 183 184 /* Free space cache stuff */ 185 struct btrfs_free_space_ctl *free_space_ctl; 186 187 /* Block group cache stuff */ 188 struct rb_node cache_node; 189 190 /* For block groups in the same raid type */ 191 struct list_head list; 192 193 refcount_t refs; 194 195 /* 196 * List of struct btrfs_free_clusters for this block group. 197 * Today it will only have one thing on it, but that may change 198 */ 199 struct list_head cluster_list; 200 201 /* 202 * Used for several lists: 203 * 204 * 1) struct btrfs_fs_info::unused_bgs 205 * 2) struct btrfs_fs_info::reclaim_bgs 206 * 3) struct btrfs_transaction::deleted_bgs 207 * 4) struct btrfs_trans_handle::new_bgs 208 */ 209 struct list_head bg_list; 210 211 /* For read-only block groups */ 212 struct list_head ro_list; 213 214 /* 215 * When non-zero it means the block group's logical address and its 216 * device extents can not be reused for future block group allocations 217 * until the counter goes down to 0. This is to prevent them from being 218 * reused while some task is still using the block group after it was 219 * deleted - we want to make sure they can only be reused for new block 220 * groups after that task is done with the deleted block group. 221 */ 222 atomic_t frozen; 223 224 /* For discard operations */ 225 struct list_head discard_list; 226 int discard_index; 227 u64 discard_eligible_time; 228 u64 discard_cursor; 229 enum btrfs_discard_state discard_state; 230 231 /* For dirty block groups */ 232 struct list_head dirty_list; 233 struct list_head io_list; 234 235 struct btrfs_io_ctl io_ctl; 236 237 /* 238 * Incremented when doing extent allocations and holding a read lock 239 * on the space_info's groups_sem semaphore. 240 * Decremented when an ordered extent that represents an IO against this 241 * block group's range is created (after it's added to its inode's 242 * root's list of ordered extents) or immediately after the allocation 243 * if it's a metadata extent or fallocate extent (for these cases we 244 * don't create ordered extents). 245 */ 246 atomic_t reservations; 247 248 /* 249 * Incremented while holding the spinlock *lock* by a task checking if 250 * it can perform a nocow write (incremented if the value for the *ro* 251 * field is 0). Decremented by such tasks once they create an ordered 252 * extent or before that if some error happens before reaching that step. 253 * This is to prevent races between block group relocation and nocow 254 * writes through direct IO. 255 */ 256 atomic_t nocow_writers; 257 258 /* Lock for free space tree operations. */ 259 struct mutex free_space_lock; 260 261 /* Protected by @free_space_lock. */ 262 bool using_free_space_bitmaps; 263 /* Protected by @free_space_lock. */ 264 bool using_free_space_bitmaps_cached; 265 266 /* 267 * Number of extents in this block group used for swap files. 268 * All accesses protected by the spinlock 'lock'. 269 */ 270 int swap_extents; 271 272 /* 273 * Allocation offset for the block group to implement sequential 274 * allocation. This is used only on a zoned filesystem. 275 */ 276 u64 alloc_offset; 277 u64 zone_unusable; 278 u64 zone_capacity; 279 u64 meta_write_pointer; 280 struct btrfs_chunk_map *physical_map; 281 struct list_head active_bg_list; 282 struct work_struct zone_finish_work; 283 struct extent_buffer *last_eb; 284 enum btrfs_block_group_size_class size_class; 285 u64 reclaim_mark; 286 }; 287 288 static inline u64 btrfs_block_group_end(const struct btrfs_block_group *block_group) 289 { 290 return (block_group->start + block_group->length); 291 } 292 293 static inline bool btrfs_is_block_group_used(const struct btrfs_block_group *bg) 294 { 295 lockdep_assert_held(&bg->lock); 296 297 return (bg->used > 0 || bg->reserved > 0 || bg->pinned > 0 || 298 bg->remap_bytes > 0); 299 } 300 301 static inline bool btrfs_is_block_group_data_only(const struct btrfs_block_group *block_group) 302 { 303 /* 304 * In mixed mode the fragmentation is expected to be high, lowering the 305 * efficiency, so only proper data block groups are considered. 306 */ 307 return (block_group->flags & BTRFS_BLOCK_GROUP_DATA) && 308 !(block_group->flags & BTRFS_BLOCK_GROUP_METADATA); 309 } 310 311 static inline u64 btrfs_block_group_available_space(const struct btrfs_block_group *bg) 312 { 313 lockdep_assert_held(&bg->lock); 314 315 return (bg->length - bg->used - bg->pinned - bg->reserved - 316 bg->bytes_super - bg->zone_unusable); 317 } 318 319 #ifdef CONFIG_BTRFS_DEBUG 320 int btrfs_should_fragment_free_space(const struct btrfs_block_group *block_group); 321 #endif 322 323 struct btrfs_block_group *btrfs_lookup_first_block_group( 324 struct btrfs_fs_info *info, u64 bytenr); 325 struct btrfs_block_group *btrfs_lookup_block_group( 326 struct btrfs_fs_info *info, u64 bytenr); 327 struct btrfs_block_group *btrfs_next_block_group( 328 struct btrfs_block_group *cache); 329 void btrfs_get_block_group(struct btrfs_block_group *cache); 330 void btrfs_put_block_group(struct btrfs_block_group *cache); 331 void btrfs_dec_block_group_reservations(struct btrfs_fs_info *fs_info, 332 const u64 start); 333 void btrfs_wait_block_group_reservations(struct btrfs_block_group *bg); 334 struct btrfs_block_group *btrfs_inc_nocow_writers(struct btrfs_fs_info *fs_info, 335 u64 bytenr); 336 void btrfs_dec_nocow_writers(struct btrfs_block_group *bg); 337 void btrfs_wait_nocow_writers(struct btrfs_block_group *bg); 338 void btrfs_wait_block_group_cache_progress(struct btrfs_block_group *cache, 339 u64 num_bytes); 340 int btrfs_cache_block_group(struct btrfs_block_group *cache, bool wait); 341 struct btrfs_caching_control *btrfs_get_caching_control( 342 struct btrfs_block_group *cache); 343 int btrfs_add_new_free_space(struct btrfs_block_group *block_group, 344 u64 start, u64 end, u64 *total_added_ret); 345 struct btrfs_trans_handle *btrfs_start_trans_remove_block_group( 346 struct btrfs_fs_info *fs_info, 347 const u64 chunk_offset); 348 void btrfs_remove_bg_from_sinfo(struct btrfs_block_group *bg); 349 int btrfs_remove_block_group(struct btrfs_trans_handle *trans, 350 struct btrfs_chunk_map *map); 351 void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info); 352 void btrfs_mark_bg_unused(struct btrfs_block_group *bg); 353 void btrfs_reclaim_bgs_work(struct work_struct *work); 354 void btrfs_reclaim_bgs(struct btrfs_fs_info *fs_info); 355 void btrfs_mark_bg_to_reclaim(struct btrfs_block_group *bg); 356 int btrfs_read_block_groups(struct btrfs_fs_info *info); 357 struct btrfs_block_group *btrfs_make_block_group(struct btrfs_trans_handle *trans, 358 struct btrfs_space_info *space_info, 359 u64 type, u64 chunk_offset, u64 size); 360 void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans); 361 int btrfs_inc_block_group_ro(struct btrfs_block_group *cache, 362 bool do_chunk_alloc); 363 void btrfs_dec_block_group_ro(struct btrfs_block_group *cache); 364 int btrfs_start_dirty_block_groups(struct btrfs_trans_handle *trans); 365 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans); 366 int btrfs_setup_space_cache(struct btrfs_trans_handle *trans); 367 int btrfs_update_block_group(struct btrfs_trans_handle *trans, 368 u64 bytenr, u64 num_bytes, bool alloc); 369 int btrfs_add_reserved_bytes(struct btrfs_block_group *cache, 370 u64 ram_bytes, u64 num_bytes, bool delalloc, 371 bool force_wrong_size_class); 372 void btrfs_free_reserved_bytes(struct btrfs_block_group *cache, u64 num_bytes, 373 bool is_delalloc); 374 int btrfs_chunk_alloc(struct btrfs_trans_handle *trans, 375 struct btrfs_space_info *space_info, u64 flags, 376 enum btrfs_chunk_alloc_enum force); 377 int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans, u64 type); 378 void check_system_chunk(struct btrfs_trans_handle *trans, const u64 type); 379 void btrfs_reserve_chunk_metadata(struct btrfs_trans_handle *trans, 380 bool is_item_insertion); 381 u64 btrfs_get_alloc_profile(struct btrfs_fs_info *fs_info, u64 orig_flags); 382 void btrfs_put_block_group_cache(struct btrfs_fs_info *info); 383 int btrfs_free_block_groups(struct btrfs_fs_info *info); 384 int btrfs_rmap_block(struct btrfs_fs_info *fs_info, u64 chunk_start, 385 u64 physical, u64 **logical, int *naddrs, int *stripe_len); 386 387 static inline u64 btrfs_data_alloc_profile(struct btrfs_fs_info *fs_info) 388 { 389 return btrfs_get_alloc_profile(fs_info, BTRFS_BLOCK_GROUP_DATA); 390 } 391 392 static inline u64 btrfs_metadata_alloc_profile(struct btrfs_fs_info *fs_info) 393 { 394 return btrfs_get_alloc_profile(fs_info, BTRFS_BLOCK_GROUP_METADATA); 395 } 396 397 static inline u64 btrfs_system_alloc_profile(struct btrfs_fs_info *fs_info) 398 { 399 return btrfs_get_alloc_profile(fs_info, BTRFS_BLOCK_GROUP_SYSTEM); 400 } 401 402 static inline int btrfs_block_group_done(const struct btrfs_block_group *cache) 403 { 404 smp_mb(); 405 return cache->cached == BTRFS_CACHE_FINISHED || 406 cache->cached == BTRFS_CACHE_ERROR; 407 } 408 409 void btrfs_freeze_block_group(struct btrfs_block_group *cache); 410 void btrfs_unfreeze_block_group(struct btrfs_block_group *cache); 411 412 bool btrfs_inc_block_group_swap_extents(struct btrfs_block_group *bg); 413 void btrfs_dec_block_group_swap_extents(struct btrfs_block_group *bg, int amount); 414 415 enum btrfs_block_group_size_class btrfs_calc_block_group_size_class(u64 size); 416 int btrfs_use_block_group_size_class(struct btrfs_block_group *bg, 417 enum btrfs_block_group_size_class size_class, 418 bool force_wrong_size_class); 419 bool btrfs_block_group_should_use_size_class(const struct btrfs_block_group *bg); 420 void btrfs_mark_bg_fully_remapped(struct btrfs_block_group *bg, 421 struct btrfs_trans_handle *trans); 422 int btrfs_populate_fully_remapped_bgs_list(struct btrfs_fs_info *fs_info); 423 424 #endif /* BTRFS_BLOCK_GROUP_H */ 425