Josef Bacik | 2e405ad | 2019-06-20 15:37:45 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | |
David Sterba | 784352f | 2019-08-21 18:54:28 +0200 | [diff] [blame] | 3 | #include "misc.h" |
Josef Bacik | 2e405ad | 2019-06-20 15:37:45 -0400 | [diff] [blame] | 4 | #include "ctree.h" |
| 5 | #include "block-group.h" |
Josef Bacik | 3eeb322 | 2019-06-20 15:37:47 -0400 | [diff] [blame] | 6 | #include "space-info.h" |
Josef Bacik | 9f21246 | 2019-08-06 16:43:19 +0200 | [diff] [blame] | 7 | #include "disk-io.h" |
| 8 | #include "free-space-cache.h" |
| 9 | #include "free-space-tree.h" |
Josef Bacik | e3e0520 | 2019-06-20 15:37:55 -0400 | [diff] [blame] | 10 | #include "disk-io.h" |
| 11 | #include "volumes.h" |
| 12 | #include "transaction.h" |
| 13 | #include "ref-verify.h" |
Josef Bacik | 4358d963 | 2019-06-20 15:37:57 -0400 | [diff] [blame] | 14 | #include "sysfs.h" |
| 15 | #include "tree-log.h" |
Josef Bacik | 77745c0 | 2019-06-20 15:38:00 -0400 | [diff] [blame] | 16 | #include "delalloc-space.h" |
Josef Bacik | 2e405ad | 2019-06-20 15:37:45 -0400 | [diff] [blame] | 17 | |
Josef Bacik | 878d7b6 | 2019-06-20 15:38:05 -0400 | [diff] [blame] | 18 | /* |
| 19 | * Return target flags in extended format or 0 if restripe for this chunk_type |
| 20 | * is not in progress |
| 21 | * |
| 22 | * Should be called with balance_lock held |
| 23 | */ |
Josef Bacik | e11c040 | 2019-06-20 15:38:07 -0400 | [diff] [blame] | 24 | static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags) |
Josef Bacik | 878d7b6 | 2019-06-20 15:38:05 -0400 | [diff] [blame] | 25 | { |
| 26 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
| 27 | u64 target = 0; |
| 28 | |
| 29 | if (!bctl) |
| 30 | return 0; |
| 31 | |
| 32 | if (flags & BTRFS_BLOCK_GROUP_DATA && |
| 33 | bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) { |
| 34 | target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target; |
| 35 | } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM && |
| 36 | bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) { |
| 37 | target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target; |
| 38 | } else if (flags & BTRFS_BLOCK_GROUP_METADATA && |
| 39 | bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) { |
| 40 | target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target; |
| 41 | } |
| 42 | |
| 43 | return target; |
| 44 | } |
| 45 | |
| 46 | /* |
| 47 | * @flags: available profiles in extended format (see ctree.h) |
| 48 | * |
| 49 | * Return reduced profile in chunk format. If profile changing is in progress |
| 50 | * (either running or paused) picks the target profile (if it's already |
| 51 | * available), otherwise falls back to plain reducing. |
| 52 | */ |
| 53 | static u64 btrfs_reduce_alloc_profile(struct btrfs_fs_info *fs_info, u64 flags) |
| 54 | { |
| 55 | u64 num_devices = fs_info->fs_devices->rw_devices; |
| 56 | u64 target; |
| 57 | u64 raid_type; |
| 58 | u64 allowed = 0; |
| 59 | |
| 60 | /* |
| 61 | * See if restripe for this chunk_type is in progress, if so try to |
| 62 | * reduce to the target profile |
| 63 | */ |
| 64 | spin_lock(&fs_info->balance_lock); |
Josef Bacik | e11c040 | 2019-06-20 15:38:07 -0400 | [diff] [blame] | 65 | target = get_restripe_target(fs_info, flags); |
Josef Bacik | 878d7b6 | 2019-06-20 15:38:05 -0400 | [diff] [blame] | 66 | if (target) { |
| 67 | /* Pick target profile only if it's already available */ |
| 68 | if ((flags & target) & BTRFS_EXTENDED_PROFILE_MASK) { |
| 69 | spin_unlock(&fs_info->balance_lock); |
| 70 | return extended_to_chunk(target); |
| 71 | } |
| 72 | } |
| 73 | spin_unlock(&fs_info->balance_lock); |
| 74 | |
| 75 | /* First, mask out the RAID levels which aren't possible */ |
| 76 | for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) { |
| 77 | if (num_devices >= btrfs_raid_array[raid_type].devs_min) |
| 78 | allowed |= btrfs_raid_array[raid_type].bg_flag; |
| 79 | } |
| 80 | allowed &= flags; |
| 81 | |
| 82 | if (allowed & BTRFS_BLOCK_GROUP_RAID6) |
| 83 | allowed = BTRFS_BLOCK_GROUP_RAID6; |
| 84 | else if (allowed & BTRFS_BLOCK_GROUP_RAID5) |
| 85 | allowed = BTRFS_BLOCK_GROUP_RAID5; |
| 86 | else if (allowed & BTRFS_BLOCK_GROUP_RAID10) |
| 87 | allowed = BTRFS_BLOCK_GROUP_RAID10; |
| 88 | else if (allowed & BTRFS_BLOCK_GROUP_RAID1) |
| 89 | allowed = BTRFS_BLOCK_GROUP_RAID1; |
| 90 | else if (allowed & BTRFS_BLOCK_GROUP_RAID0) |
| 91 | allowed = BTRFS_BLOCK_GROUP_RAID0; |
| 92 | |
| 93 | flags &= ~BTRFS_BLOCK_GROUP_PROFILE_MASK; |
| 94 | |
| 95 | return extended_to_chunk(flags | allowed); |
| 96 | } |
| 97 | |
| 98 | static u64 get_alloc_profile(struct btrfs_fs_info *fs_info, u64 orig_flags) |
| 99 | { |
| 100 | unsigned seq; |
| 101 | u64 flags; |
| 102 | |
| 103 | do { |
| 104 | flags = orig_flags; |
| 105 | seq = read_seqbegin(&fs_info->profiles_lock); |
| 106 | |
| 107 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
| 108 | flags |= fs_info->avail_data_alloc_bits; |
| 109 | else if (flags & BTRFS_BLOCK_GROUP_SYSTEM) |
| 110 | flags |= fs_info->avail_system_alloc_bits; |
| 111 | else if (flags & BTRFS_BLOCK_GROUP_METADATA) |
| 112 | flags |= fs_info->avail_metadata_alloc_bits; |
| 113 | } while (read_seqretry(&fs_info->profiles_lock, seq)); |
| 114 | |
| 115 | return btrfs_reduce_alloc_profile(fs_info, flags); |
| 116 | } |
| 117 | |
| 118 | u64 btrfs_get_alloc_profile(struct btrfs_fs_info *fs_info, u64 orig_flags) |
| 119 | { |
| 120 | return get_alloc_profile(fs_info, orig_flags); |
| 121 | } |
| 122 | |
Josef Bacik | 3cad128 | 2019-06-20 15:37:46 -0400 | [diff] [blame] | 123 | void btrfs_get_block_group(struct btrfs_block_group_cache *cache) |
| 124 | { |
| 125 | atomic_inc(&cache->count); |
| 126 | } |
| 127 | |
| 128 | void btrfs_put_block_group(struct btrfs_block_group_cache *cache) |
| 129 | { |
| 130 | if (atomic_dec_and_test(&cache->count)) { |
| 131 | WARN_ON(cache->pinned > 0); |
| 132 | WARN_ON(cache->reserved > 0); |
| 133 | |
| 134 | /* |
| 135 | * If not empty, someone is still holding mutex of |
| 136 | * full_stripe_lock, which can only be released by caller. |
| 137 | * And it will definitely cause use-after-free when caller |
| 138 | * tries to release full stripe lock. |
| 139 | * |
| 140 | * No better way to resolve, but only to warn. |
| 141 | */ |
| 142 | WARN_ON(!RB_EMPTY_ROOT(&cache->full_stripe_locks_root.root)); |
| 143 | kfree(cache->free_space_ctl); |
| 144 | kfree(cache); |
| 145 | } |
| 146 | } |
| 147 | |
Josef Bacik | 2e405ad | 2019-06-20 15:37:45 -0400 | [diff] [blame] | 148 | /* |
Josef Bacik | 4358d963 | 2019-06-20 15:37:57 -0400 | [diff] [blame] | 149 | * This adds the block group to the fs_info rb tree for the block group cache |
| 150 | */ |
| 151 | static int btrfs_add_block_group_cache(struct btrfs_fs_info *info, |
| 152 | struct btrfs_block_group_cache *block_group) |
| 153 | { |
| 154 | struct rb_node **p; |
| 155 | struct rb_node *parent = NULL; |
| 156 | struct btrfs_block_group_cache *cache; |
| 157 | |
| 158 | spin_lock(&info->block_group_cache_lock); |
| 159 | p = &info->block_group_cache_tree.rb_node; |
| 160 | |
| 161 | while (*p) { |
| 162 | parent = *p; |
| 163 | cache = rb_entry(parent, struct btrfs_block_group_cache, |
| 164 | cache_node); |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame^] | 165 | if (block_group->start < cache->start) { |
Josef Bacik | 4358d963 | 2019-06-20 15:37:57 -0400 | [diff] [blame] | 166 | p = &(*p)->rb_left; |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame^] | 167 | } else if (block_group->start > cache->start) { |
Josef Bacik | 4358d963 | 2019-06-20 15:37:57 -0400 | [diff] [blame] | 168 | p = &(*p)->rb_right; |
| 169 | } else { |
| 170 | spin_unlock(&info->block_group_cache_lock); |
| 171 | return -EEXIST; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | rb_link_node(&block_group->cache_node, parent, p); |
| 176 | rb_insert_color(&block_group->cache_node, |
| 177 | &info->block_group_cache_tree); |
| 178 | |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame^] | 179 | if (info->first_logical_byte > block_group->start) |
| 180 | info->first_logical_byte = block_group->start; |
Josef Bacik | 4358d963 | 2019-06-20 15:37:57 -0400 | [diff] [blame] | 181 | |
| 182 | spin_unlock(&info->block_group_cache_lock); |
| 183 | |
| 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | /* |
Josef Bacik | 2e405ad | 2019-06-20 15:37:45 -0400 | [diff] [blame] | 188 | * This will return the block group at or after bytenr if contains is 0, else |
| 189 | * it will return the block group that contains the bytenr |
| 190 | */ |
| 191 | static struct btrfs_block_group_cache *block_group_cache_tree_search( |
| 192 | struct btrfs_fs_info *info, u64 bytenr, int contains) |
| 193 | { |
| 194 | struct btrfs_block_group_cache *cache, *ret = NULL; |
| 195 | struct rb_node *n; |
| 196 | u64 end, start; |
| 197 | |
| 198 | spin_lock(&info->block_group_cache_lock); |
| 199 | n = info->block_group_cache_tree.rb_node; |
| 200 | |
| 201 | while (n) { |
| 202 | cache = rb_entry(n, struct btrfs_block_group_cache, |
| 203 | cache_node); |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame^] | 204 | end = cache->start + cache->length - 1; |
| 205 | start = cache->start; |
Josef Bacik | 2e405ad | 2019-06-20 15:37:45 -0400 | [diff] [blame] | 206 | |
| 207 | if (bytenr < start) { |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame^] | 208 | if (!contains && (!ret || start < ret->start)) |
Josef Bacik | 2e405ad | 2019-06-20 15:37:45 -0400 | [diff] [blame] | 209 | ret = cache; |
| 210 | n = n->rb_left; |
| 211 | } else if (bytenr > start) { |
| 212 | if (contains && bytenr <= end) { |
| 213 | ret = cache; |
| 214 | break; |
| 215 | } |
| 216 | n = n->rb_right; |
| 217 | } else { |
| 218 | ret = cache; |
| 219 | break; |
| 220 | } |
| 221 | } |
| 222 | if (ret) { |
| 223 | btrfs_get_block_group(ret); |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame^] | 224 | if (bytenr == 0 && info->first_logical_byte > ret->start) |
| 225 | info->first_logical_byte = ret->start; |
Josef Bacik | 2e405ad | 2019-06-20 15:37:45 -0400 | [diff] [blame] | 226 | } |
| 227 | spin_unlock(&info->block_group_cache_lock); |
| 228 | |
| 229 | return ret; |
| 230 | } |
| 231 | |
| 232 | /* |
| 233 | * Return the block group that starts at or after bytenr |
| 234 | */ |
| 235 | struct btrfs_block_group_cache *btrfs_lookup_first_block_group( |
| 236 | struct btrfs_fs_info *info, u64 bytenr) |
| 237 | { |
| 238 | return block_group_cache_tree_search(info, bytenr, 0); |
| 239 | } |
| 240 | |
| 241 | /* |
| 242 | * Return the block group that contains the given bytenr |
| 243 | */ |
| 244 | struct btrfs_block_group_cache *btrfs_lookup_block_group( |
| 245 | struct btrfs_fs_info *info, u64 bytenr) |
| 246 | { |
| 247 | return block_group_cache_tree_search(info, bytenr, 1); |
| 248 | } |
| 249 | |
| 250 | struct btrfs_block_group_cache *btrfs_next_block_group( |
| 251 | struct btrfs_block_group_cache *cache) |
| 252 | { |
| 253 | struct btrfs_fs_info *fs_info = cache->fs_info; |
| 254 | struct rb_node *node; |
| 255 | |
| 256 | spin_lock(&fs_info->block_group_cache_lock); |
| 257 | |
| 258 | /* If our block group was removed, we need a full search. */ |
| 259 | if (RB_EMPTY_NODE(&cache->cache_node)) { |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame^] | 260 | const u64 next_bytenr = cache->start + cache->length; |
Josef Bacik | 2e405ad | 2019-06-20 15:37:45 -0400 | [diff] [blame] | 261 | |
| 262 | spin_unlock(&fs_info->block_group_cache_lock); |
| 263 | btrfs_put_block_group(cache); |
| 264 | cache = btrfs_lookup_first_block_group(fs_info, next_bytenr); return cache; |
| 265 | } |
| 266 | node = rb_next(&cache->cache_node); |
| 267 | btrfs_put_block_group(cache); |
| 268 | if (node) { |
| 269 | cache = rb_entry(node, struct btrfs_block_group_cache, |
| 270 | cache_node); |
| 271 | btrfs_get_block_group(cache); |
| 272 | } else |
| 273 | cache = NULL; |
| 274 | spin_unlock(&fs_info->block_group_cache_lock); |
| 275 | return cache; |
| 276 | } |
Josef Bacik | 3eeb322 | 2019-06-20 15:37:47 -0400 | [diff] [blame] | 277 | |
| 278 | bool btrfs_inc_nocow_writers(struct btrfs_fs_info *fs_info, u64 bytenr) |
| 279 | { |
| 280 | struct btrfs_block_group_cache *bg; |
| 281 | bool ret = true; |
| 282 | |
| 283 | bg = btrfs_lookup_block_group(fs_info, bytenr); |
| 284 | if (!bg) |
| 285 | return false; |
| 286 | |
| 287 | spin_lock(&bg->lock); |
| 288 | if (bg->ro) |
| 289 | ret = false; |
| 290 | else |
| 291 | atomic_inc(&bg->nocow_writers); |
| 292 | spin_unlock(&bg->lock); |
| 293 | |
| 294 | /* No put on block group, done by btrfs_dec_nocow_writers */ |
| 295 | if (!ret) |
| 296 | btrfs_put_block_group(bg); |
| 297 | |
| 298 | return ret; |
| 299 | } |
| 300 | |
| 301 | void btrfs_dec_nocow_writers(struct btrfs_fs_info *fs_info, u64 bytenr) |
| 302 | { |
| 303 | struct btrfs_block_group_cache *bg; |
| 304 | |
| 305 | bg = btrfs_lookup_block_group(fs_info, bytenr); |
| 306 | ASSERT(bg); |
| 307 | if (atomic_dec_and_test(&bg->nocow_writers)) |
| 308 | wake_up_var(&bg->nocow_writers); |
| 309 | /* |
| 310 | * Once for our lookup and once for the lookup done by a previous call |
| 311 | * to btrfs_inc_nocow_writers() |
| 312 | */ |
| 313 | btrfs_put_block_group(bg); |
| 314 | btrfs_put_block_group(bg); |
| 315 | } |
| 316 | |
| 317 | void btrfs_wait_nocow_writers(struct btrfs_block_group_cache *bg) |
| 318 | { |
| 319 | wait_var_event(&bg->nocow_writers, !atomic_read(&bg->nocow_writers)); |
| 320 | } |
| 321 | |
| 322 | void btrfs_dec_block_group_reservations(struct btrfs_fs_info *fs_info, |
| 323 | const u64 start) |
| 324 | { |
| 325 | struct btrfs_block_group_cache *bg; |
| 326 | |
| 327 | bg = btrfs_lookup_block_group(fs_info, start); |
| 328 | ASSERT(bg); |
| 329 | if (atomic_dec_and_test(&bg->reservations)) |
| 330 | wake_up_var(&bg->reservations); |
| 331 | btrfs_put_block_group(bg); |
| 332 | } |
| 333 | |
| 334 | void btrfs_wait_block_group_reservations(struct btrfs_block_group_cache *bg) |
| 335 | { |
| 336 | struct btrfs_space_info *space_info = bg->space_info; |
| 337 | |
| 338 | ASSERT(bg->ro); |
| 339 | |
| 340 | if (!(bg->flags & BTRFS_BLOCK_GROUP_DATA)) |
| 341 | return; |
| 342 | |
| 343 | /* |
| 344 | * Our block group is read only but before we set it to read only, |
| 345 | * some task might have had allocated an extent from it already, but it |
| 346 | * has not yet created a respective ordered extent (and added it to a |
| 347 | * root's list of ordered extents). |
| 348 | * Therefore wait for any task currently allocating extents, since the |
| 349 | * block group's reservations counter is incremented while a read lock |
| 350 | * on the groups' semaphore is held and decremented after releasing |
| 351 | * the read access on that semaphore and creating the ordered extent. |
| 352 | */ |
| 353 | down_write(&space_info->groups_sem); |
| 354 | up_write(&space_info->groups_sem); |
| 355 | |
| 356 | wait_var_event(&bg->reservations, !atomic_read(&bg->reservations)); |
| 357 | } |
Josef Bacik | 9f21246 | 2019-08-06 16:43:19 +0200 | [diff] [blame] | 358 | |
| 359 | struct btrfs_caching_control *btrfs_get_caching_control( |
| 360 | struct btrfs_block_group_cache *cache) |
| 361 | { |
| 362 | struct btrfs_caching_control *ctl; |
| 363 | |
| 364 | spin_lock(&cache->lock); |
| 365 | if (!cache->caching_ctl) { |
| 366 | spin_unlock(&cache->lock); |
| 367 | return NULL; |
| 368 | } |
| 369 | |
| 370 | ctl = cache->caching_ctl; |
| 371 | refcount_inc(&ctl->count); |
| 372 | spin_unlock(&cache->lock); |
| 373 | return ctl; |
| 374 | } |
| 375 | |
| 376 | void btrfs_put_caching_control(struct btrfs_caching_control *ctl) |
| 377 | { |
| 378 | if (refcount_dec_and_test(&ctl->count)) |
| 379 | kfree(ctl); |
| 380 | } |
| 381 | |
| 382 | /* |
| 383 | * When we wait for progress in the block group caching, its because our |
| 384 | * allocation attempt failed at least once. So, we must sleep and let some |
| 385 | * progress happen before we try again. |
| 386 | * |
| 387 | * This function will sleep at least once waiting for new free space to show |
| 388 | * up, and then it will check the block group free space numbers for our min |
| 389 | * num_bytes. Another option is to have it go ahead and look in the rbtree for |
| 390 | * a free extent of a given size, but this is a good start. |
| 391 | * |
| 392 | * Callers of this must check if cache->cached == BTRFS_CACHE_ERROR before using |
| 393 | * any of the information in this block group. |
| 394 | */ |
| 395 | void btrfs_wait_block_group_cache_progress(struct btrfs_block_group_cache *cache, |
| 396 | u64 num_bytes) |
| 397 | { |
| 398 | struct btrfs_caching_control *caching_ctl; |
| 399 | |
| 400 | caching_ctl = btrfs_get_caching_control(cache); |
| 401 | if (!caching_ctl) |
| 402 | return; |
| 403 | |
| 404 | wait_event(caching_ctl->wait, btrfs_block_group_cache_done(cache) || |
| 405 | (cache->free_space_ctl->free_space >= num_bytes)); |
| 406 | |
| 407 | btrfs_put_caching_control(caching_ctl); |
| 408 | } |
| 409 | |
| 410 | int btrfs_wait_block_group_cache_done(struct btrfs_block_group_cache *cache) |
| 411 | { |
| 412 | struct btrfs_caching_control *caching_ctl; |
| 413 | int ret = 0; |
| 414 | |
| 415 | caching_ctl = btrfs_get_caching_control(cache); |
| 416 | if (!caching_ctl) |
| 417 | return (cache->cached == BTRFS_CACHE_ERROR) ? -EIO : 0; |
| 418 | |
| 419 | wait_event(caching_ctl->wait, btrfs_block_group_cache_done(cache)); |
| 420 | if (cache->cached == BTRFS_CACHE_ERROR) |
| 421 | ret = -EIO; |
| 422 | btrfs_put_caching_control(caching_ctl); |
| 423 | return ret; |
| 424 | } |
| 425 | |
| 426 | #ifdef CONFIG_BTRFS_DEBUG |
Josef Bacik | e11c040 | 2019-06-20 15:38:07 -0400 | [diff] [blame] | 427 | static void fragment_free_space(struct btrfs_block_group_cache *block_group) |
Josef Bacik | 9f21246 | 2019-08-06 16:43:19 +0200 | [diff] [blame] | 428 | { |
| 429 | struct btrfs_fs_info *fs_info = block_group->fs_info; |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame^] | 430 | u64 start = block_group->start; |
| 431 | u64 len = block_group->length; |
Josef Bacik | 9f21246 | 2019-08-06 16:43:19 +0200 | [diff] [blame] | 432 | u64 chunk = block_group->flags & BTRFS_BLOCK_GROUP_METADATA ? |
| 433 | fs_info->nodesize : fs_info->sectorsize; |
| 434 | u64 step = chunk << 1; |
| 435 | |
| 436 | while (len > chunk) { |
| 437 | btrfs_remove_free_space(block_group, start, chunk); |
| 438 | start += step; |
| 439 | if (len < step) |
| 440 | len = 0; |
| 441 | else |
| 442 | len -= step; |
| 443 | } |
| 444 | } |
| 445 | #endif |
| 446 | |
| 447 | /* |
| 448 | * This is only called by btrfs_cache_block_group, since we could have freed |
| 449 | * extents we need to check the pinned_extents for any extents that can't be |
| 450 | * used yet since their free space will be released as soon as the transaction |
| 451 | * commits. |
| 452 | */ |
| 453 | u64 add_new_free_space(struct btrfs_block_group_cache *block_group, |
| 454 | u64 start, u64 end) |
| 455 | { |
| 456 | struct btrfs_fs_info *info = block_group->fs_info; |
| 457 | u64 extent_start, extent_end, size, total_added = 0; |
| 458 | int ret; |
| 459 | |
| 460 | while (start < end) { |
| 461 | ret = find_first_extent_bit(info->pinned_extents, start, |
| 462 | &extent_start, &extent_end, |
| 463 | EXTENT_DIRTY | EXTENT_UPTODATE, |
| 464 | NULL); |
| 465 | if (ret) |
| 466 | break; |
| 467 | |
| 468 | if (extent_start <= start) { |
| 469 | start = extent_end + 1; |
| 470 | } else if (extent_start > start && extent_start < end) { |
| 471 | size = extent_start - start; |
| 472 | total_added += size; |
| 473 | ret = btrfs_add_free_space(block_group, start, |
| 474 | size); |
| 475 | BUG_ON(ret); /* -ENOMEM or logic error */ |
| 476 | start = extent_end + 1; |
| 477 | } else { |
| 478 | break; |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | if (start < end) { |
| 483 | size = end - start; |
| 484 | total_added += size; |
| 485 | ret = btrfs_add_free_space(block_group, start, size); |
| 486 | BUG_ON(ret); /* -ENOMEM or logic error */ |
| 487 | } |
| 488 | |
| 489 | return total_added; |
| 490 | } |
| 491 | |
| 492 | static int load_extent_tree_free(struct btrfs_caching_control *caching_ctl) |
| 493 | { |
| 494 | struct btrfs_block_group_cache *block_group = caching_ctl->block_group; |
| 495 | struct btrfs_fs_info *fs_info = block_group->fs_info; |
| 496 | struct btrfs_root *extent_root = fs_info->extent_root; |
| 497 | struct btrfs_path *path; |
| 498 | struct extent_buffer *leaf; |
| 499 | struct btrfs_key key; |
| 500 | u64 total_found = 0; |
| 501 | u64 last = 0; |
| 502 | u32 nritems; |
| 503 | int ret; |
| 504 | bool wakeup = true; |
| 505 | |
| 506 | path = btrfs_alloc_path(); |
| 507 | if (!path) |
| 508 | return -ENOMEM; |
| 509 | |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame^] | 510 | last = max_t(u64, block_group->start, BTRFS_SUPER_INFO_OFFSET); |
Josef Bacik | 9f21246 | 2019-08-06 16:43:19 +0200 | [diff] [blame] | 511 | |
| 512 | #ifdef CONFIG_BTRFS_DEBUG |
| 513 | /* |
| 514 | * If we're fragmenting we don't want to make anybody think we can |
| 515 | * allocate from this block group until we've had a chance to fragment |
| 516 | * the free space. |
| 517 | */ |
| 518 | if (btrfs_should_fragment_free_space(block_group)) |
| 519 | wakeup = false; |
| 520 | #endif |
| 521 | /* |
| 522 | * We don't want to deadlock with somebody trying to allocate a new |
| 523 | * extent for the extent root while also trying to search the extent |
| 524 | * root to add free space. So we skip locking and search the commit |
| 525 | * root, since its read-only |
| 526 | */ |
| 527 | path->skip_locking = 1; |
| 528 | path->search_commit_root = 1; |
| 529 | path->reada = READA_FORWARD; |
| 530 | |
| 531 | key.objectid = last; |
| 532 | key.offset = 0; |
| 533 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 534 | |
| 535 | next: |
| 536 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
| 537 | if (ret < 0) |
| 538 | goto out; |
| 539 | |
| 540 | leaf = path->nodes[0]; |
| 541 | nritems = btrfs_header_nritems(leaf); |
| 542 | |
| 543 | while (1) { |
| 544 | if (btrfs_fs_closing(fs_info) > 1) { |
| 545 | last = (u64)-1; |
| 546 | break; |
| 547 | } |
| 548 | |
| 549 | if (path->slots[0] < nritems) { |
| 550 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 551 | } else { |
| 552 | ret = btrfs_find_next_key(extent_root, path, &key, 0, 0); |
| 553 | if (ret) |
| 554 | break; |
| 555 | |
| 556 | if (need_resched() || |
| 557 | rwsem_is_contended(&fs_info->commit_root_sem)) { |
| 558 | if (wakeup) |
| 559 | caching_ctl->progress = last; |
| 560 | btrfs_release_path(path); |
| 561 | up_read(&fs_info->commit_root_sem); |
| 562 | mutex_unlock(&caching_ctl->mutex); |
| 563 | cond_resched(); |
| 564 | mutex_lock(&caching_ctl->mutex); |
| 565 | down_read(&fs_info->commit_root_sem); |
| 566 | goto next; |
| 567 | } |
| 568 | |
| 569 | ret = btrfs_next_leaf(extent_root, path); |
| 570 | if (ret < 0) |
| 571 | goto out; |
| 572 | if (ret) |
| 573 | break; |
| 574 | leaf = path->nodes[0]; |
| 575 | nritems = btrfs_header_nritems(leaf); |
| 576 | continue; |
| 577 | } |
| 578 | |
| 579 | if (key.objectid < last) { |
| 580 | key.objectid = last; |
| 581 | key.offset = 0; |
| 582 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 583 | |
| 584 | if (wakeup) |
| 585 | caching_ctl->progress = last; |
| 586 | btrfs_release_path(path); |
| 587 | goto next; |
| 588 | } |
| 589 | |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame^] | 590 | if (key.objectid < block_group->start) { |
Josef Bacik | 9f21246 | 2019-08-06 16:43:19 +0200 | [diff] [blame] | 591 | path->slots[0]++; |
| 592 | continue; |
| 593 | } |
| 594 | |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame^] | 595 | if (key.objectid >= block_group->start + block_group->length) |
Josef Bacik | 9f21246 | 2019-08-06 16:43:19 +0200 | [diff] [blame] | 596 | break; |
| 597 | |
| 598 | if (key.type == BTRFS_EXTENT_ITEM_KEY || |
| 599 | key.type == BTRFS_METADATA_ITEM_KEY) { |
| 600 | total_found += add_new_free_space(block_group, last, |
| 601 | key.objectid); |
| 602 | if (key.type == BTRFS_METADATA_ITEM_KEY) |
| 603 | last = key.objectid + |
| 604 | fs_info->nodesize; |
| 605 | else |
| 606 | last = key.objectid + key.offset; |
| 607 | |
| 608 | if (total_found > CACHING_CTL_WAKE_UP) { |
| 609 | total_found = 0; |
| 610 | if (wakeup) |
| 611 | wake_up(&caching_ctl->wait); |
| 612 | } |
| 613 | } |
| 614 | path->slots[0]++; |
| 615 | } |
| 616 | ret = 0; |
| 617 | |
| 618 | total_found += add_new_free_space(block_group, last, |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame^] | 619 | block_group->start + block_group->length); |
Josef Bacik | 9f21246 | 2019-08-06 16:43:19 +0200 | [diff] [blame] | 620 | caching_ctl->progress = (u64)-1; |
| 621 | |
| 622 | out: |
| 623 | btrfs_free_path(path); |
| 624 | return ret; |
| 625 | } |
| 626 | |
| 627 | static noinline void caching_thread(struct btrfs_work *work) |
| 628 | { |
| 629 | struct btrfs_block_group_cache *block_group; |
| 630 | struct btrfs_fs_info *fs_info; |
| 631 | struct btrfs_caching_control *caching_ctl; |
| 632 | int ret; |
| 633 | |
| 634 | caching_ctl = container_of(work, struct btrfs_caching_control, work); |
| 635 | block_group = caching_ctl->block_group; |
| 636 | fs_info = block_group->fs_info; |
| 637 | |
| 638 | mutex_lock(&caching_ctl->mutex); |
| 639 | down_read(&fs_info->commit_root_sem); |
| 640 | |
| 641 | if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) |
| 642 | ret = load_free_space_tree(caching_ctl); |
| 643 | else |
| 644 | ret = load_extent_tree_free(caching_ctl); |
| 645 | |
| 646 | spin_lock(&block_group->lock); |
| 647 | block_group->caching_ctl = NULL; |
| 648 | block_group->cached = ret ? BTRFS_CACHE_ERROR : BTRFS_CACHE_FINISHED; |
| 649 | spin_unlock(&block_group->lock); |
| 650 | |
| 651 | #ifdef CONFIG_BTRFS_DEBUG |
| 652 | if (btrfs_should_fragment_free_space(block_group)) { |
| 653 | u64 bytes_used; |
| 654 | |
| 655 | spin_lock(&block_group->space_info->lock); |
| 656 | spin_lock(&block_group->lock); |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame^] | 657 | bytes_used = block_group->length - block_group->used; |
Josef Bacik | 9f21246 | 2019-08-06 16:43:19 +0200 | [diff] [blame] | 658 | block_group->space_info->bytes_used += bytes_used >> 1; |
| 659 | spin_unlock(&block_group->lock); |
| 660 | spin_unlock(&block_group->space_info->lock); |
Josef Bacik | e11c040 | 2019-06-20 15:38:07 -0400 | [diff] [blame] | 661 | fragment_free_space(block_group); |
Josef Bacik | 9f21246 | 2019-08-06 16:43:19 +0200 | [diff] [blame] | 662 | } |
| 663 | #endif |
| 664 | |
| 665 | caching_ctl->progress = (u64)-1; |
| 666 | |
| 667 | up_read(&fs_info->commit_root_sem); |
| 668 | btrfs_free_excluded_extents(block_group); |
| 669 | mutex_unlock(&caching_ctl->mutex); |
| 670 | |
| 671 | wake_up(&caching_ctl->wait); |
| 672 | |
| 673 | btrfs_put_caching_control(caching_ctl); |
| 674 | btrfs_put_block_group(block_group); |
| 675 | } |
| 676 | |
| 677 | int btrfs_cache_block_group(struct btrfs_block_group_cache *cache, |
| 678 | int load_cache_only) |
| 679 | { |
| 680 | DEFINE_WAIT(wait); |
| 681 | struct btrfs_fs_info *fs_info = cache->fs_info; |
| 682 | struct btrfs_caching_control *caching_ctl; |
| 683 | int ret = 0; |
| 684 | |
| 685 | caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS); |
| 686 | if (!caching_ctl) |
| 687 | return -ENOMEM; |
| 688 | |
| 689 | INIT_LIST_HEAD(&caching_ctl->list); |
| 690 | mutex_init(&caching_ctl->mutex); |
| 691 | init_waitqueue_head(&caching_ctl->wait); |
| 692 | caching_ctl->block_group = cache; |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame^] | 693 | caching_ctl->progress = cache->start; |
Josef Bacik | 9f21246 | 2019-08-06 16:43:19 +0200 | [diff] [blame] | 694 | refcount_set(&caching_ctl->count, 1); |
Omar Sandoval | a0cac0e | 2019-09-16 11:30:57 -0700 | [diff] [blame] | 695 | btrfs_init_work(&caching_ctl->work, caching_thread, NULL, NULL); |
Josef Bacik | 9f21246 | 2019-08-06 16:43:19 +0200 | [diff] [blame] | 696 | |
| 697 | spin_lock(&cache->lock); |
| 698 | /* |
| 699 | * This should be a rare occasion, but this could happen I think in the |
| 700 | * case where one thread starts to load the space cache info, and then |
| 701 | * some other thread starts a transaction commit which tries to do an |
| 702 | * allocation while the other thread is still loading the space cache |
| 703 | * info. The previous loop should have kept us from choosing this block |
| 704 | * group, but if we've moved to the state where we will wait on caching |
| 705 | * block groups we need to first check if we're doing a fast load here, |
| 706 | * so we can wait for it to finish, otherwise we could end up allocating |
| 707 | * from a block group who's cache gets evicted for one reason or |
| 708 | * another. |
| 709 | */ |
| 710 | while (cache->cached == BTRFS_CACHE_FAST) { |
| 711 | struct btrfs_caching_control *ctl; |
| 712 | |
| 713 | ctl = cache->caching_ctl; |
| 714 | refcount_inc(&ctl->count); |
| 715 | prepare_to_wait(&ctl->wait, &wait, TASK_UNINTERRUPTIBLE); |
| 716 | spin_unlock(&cache->lock); |
| 717 | |
| 718 | schedule(); |
| 719 | |
| 720 | finish_wait(&ctl->wait, &wait); |
| 721 | btrfs_put_caching_control(ctl); |
| 722 | spin_lock(&cache->lock); |
| 723 | } |
| 724 | |
| 725 | if (cache->cached != BTRFS_CACHE_NO) { |
| 726 | spin_unlock(&cache->lock); |
| 727 | kfree(caching_ctl); |
| 728 | return 0; |
| 729 | } |
| 730 | WARN_ON(cache->caching_ctl); |
| 731 | cache->caching_ctl = caching_ctl; |
| 732 | cache->cached = BTRFS_CACHE_FAST; |
| 733 | spin_unlock(&cache->lock); |
| 734 | |
| 735 | if (btrfs_test_opt(fs_info, SPACE_CACHE)) { |
| 736 | mutex_lock(&caching_ctl->mutex); |
| 737 | ret = load_free_space_cache(cache); |
| 738 | |
| 739 | spin_lock(&cache->lock); |
| 740 | if (ret == 1) { |
| 741 | cache->caching_ctl = NULL; |
| 742 | cache->cached = BTRFS_CACHE_FINISHED; |
| 743 | cache->last_byte_to_unpin = (u64)-1; |
| 744 | caching_ctl->progress = (u64)-1; |
| 745 | } else { |
| 746 | if (load_cache_only) { |
| 747 | cache->caching_ctl = NULL; |
| 748 | cache->cached = BTRFS_CACHE_NO; |
| 749 | } else { |
| 750 | cache->cached = BTRFS_CACHE_STARTED; |
| 751 | cache->has_caching_ctl = 1; |
| 752 | } |
| 753 | } |
| 754 | spin_unlock(&cache->lock); |
| 755 | #ifdef CONFIG_BTRFS_DEBUG |
| 756 | if (ret == 1 && |
| 757 | btrfs_should_fragment_free_space(cache)) { |
| 758 | u64 bytes_used; |
| 759 | |
| 760 | spin_lock(&cache->space_info->lock); |
| 761 | spin_lock(&cache->lock); |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame^] | 762 | bytes_used = cache->length - cache->used; |
Josef Bacik | 9f21246 | 2019-08-06 16:43:19 +0200 | [diff] [blame] | 763 | cache->space_info->bytes_used += bytes_used >> 1; |
| 764 | spin_unlock(&cache->lock); |
| 765 | spin_unlock(&cache->space_info->lock); |
Josef Bacik | e11c040 | 2019-06-20 15:38:07 -0400 | [diff] [blame] | 766 | fragment_free_space(cache); |
Josef Bacik | 9f21246 | 2019-08-06 16:43:19 +0200 | [diff] [blame] | 767 | } |
| 768 | #endif |
| 769 | mutex_unlock(&caching_ctl->mutex); |
| 770 | |
| 771 | wake_up(&caching_ctl->wait); |
| 772 | if (ret == 1) { |
| 773 | btrfs_put_caching_control(caching_ctl); |
| 774 | btrfs_free_excluded_extents(cache); |
| 775 | return 0; |
| 776 | } |
| 777 | } else { |
| 778 | /* |
| 779 | * We're either using the free space tree or no caching at all. |
| 780 | * Set cached to the appropriate value and wakeup any waiters. |
| 781 | */ |
| 782 | spin_lock(&cache->lock); |
| 783 | if (load_cache_only) { |
| 784 | cache->caching_ctl = NULL; |
| 785 | cache->cached = BTRFS_CACHE_NO; |
| 786 | } else { |
| 787 | cache->cached = BTRFS_CACHE_STARTED; |
| 788 | cache->has_caching_ctl = 1; |
| 789 | } |
| 790 | spin_unlock(&cache->lock); |
| 791 | wake_up(&caching_ctl->wait); |
| 792 | } |
| 793 | |
| 794 | if (load_cache_only) { |
| 795 | btrfs_put_caching_control(caching_ctl); |
| 796 | return 0; |
| 797 | } |
| 798 | |
| 799 | down_write(&fs_info->commit_root_sem); |
| 800 | refcount_inc(&caching_ctl->count); |
| 801 | list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups); |
| 802 | up_write(&fs_info->commit_root_sem); |
| 803 | |
| 804 | btrfs_get_block_group(cache); |
| 805 | |
| 806 | btrfs_queue_work(fs_info->caching_workers, &caching_ctl->work); |
| 807 | |
| 808 | return ret; |
| 809 | } |
Josef Bacik | e3e0520 | 2019-06-20 15:37:55 -0400 | [diff] [blame] | 810 | |
| 811 | static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags) |
| 812 | { |
| 813 | u64 extra_flags = chunk_to_extended(flags) & |
| 814 | BTRFS_EXTENDED_PROFILE_MASK; |
| 815 | |
| 816 | write_seqlock(&fs_info->profiles_lock); |
| 817 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
| 818 | fs_info->avail_data_alloc_bits &= ~extra_flags; |
| 819 | if (flags & BTRFS_BLOCK_GROUP_METADATA) |
| 820 | fs_info->avail_metadata_alloc_bits &= ~extra_flags; |
| 821 | if (flags & BTRFS_BLOCK_GROUP_SYSTEM) |
| 822 | fs_info->avail_system_alloc_bits &= ~extra_flags; |
| 823 | write_sequnlock(&fs_info->profiles_lock); |
| 824 | } |
| 825 | |
| 826 | /* |
| 827 | * Clear incompat bits for the following feature(s): |
| 828 | * |
| 829 | * - RAID56 - in case there's neither RAID5 nor RAID6 profile block group |
| 830 | * in the whole filesystem |
| 831 | */ |
| 832 | static void clear_incompat_bg_bits(struct btrfs_fs_info *fs_info, u64 flags) |
| 833 | { |
| 834 | if (flags & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
| 835 | struct list_head *head = &fs_info->space_info; |
| 836 | struct btrfs_space_info *sinfo; |
| 837 | |
| 838 | list_for_each_entry_rcu(sinfo, head, list) { |
| 839 | bool found = false; |
| 840 | |
| 841 | down_read(&sinfo->groups_sem); |
| 842 | if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID5])) |
| 843 | found = true; |
| 844 | if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID6])) |
| 845 | found = true; |
| 846 | up_read(&sinfo->groups_sem); |
| 847 | |
| 848 | if (found) |
| 849 | return; |
| 850 | } |
| 851 | btrfs_clear_fs_incompat(fs_info, RAID56); |
| 852 | } |
| 853 | } |
| 854 | |
| 855 | int btrfs_remove_block_group(struct btrfs_trans_handle *trans, |
| 856 | u64 group_start, struct extent_map *em) |
| 857 | { |
| 858 | struct btrfs_fs_info *fs_info = trans->fs_info; |
| 859 | struct btrfs_root *root = fs_info->extent_root; |
| 860 | struct btrfs_path *path; |
| 861 | struct btrfs_block_group_cache *block_group; |
| 862 | struct btrfs_free_cluster *cluster; |
| 863 | struct btrfs_root *tree_root = fs_info->tree_root; |
| 864 | struct btrfs_key key; |
| 865 | struct inode *inode; |
| 866 | struct kobject *kobj = NULL; |
| 867 | int ret; |
| 868 | int index; |
| 869 | int factor; |
| 870 | struct btrfs_caching_control *caching_ctl = NULL; |
| 871 | bool remove_em; |
| 872 | bool remove_rsv = false; |
| 873 | |
| 874 | block_group = btrfs_lookup_block_group(fs_info, group_start); |
| 875 | BUG_ON(!block_group); |
| 876 | BUG_ON(!block_group->ro); |
| 877 | |
| 878 | trace_btrfs_remove_block_group(block_group); |
| 879 | /* |
| 880 | * Free the reserved super bytes from this block group before |
| 881 | * remove it. |
| 882 | */ |
| 883 | btrfs_free_excluded_extents(block_group); |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame^] | 884 | btrfs_free_ref_tree_range(fs_info, block_group->start, |
| 885 | block_group->length); |
Josef Bacik | e3e0520 | 2019-06-20 15:37:55 -0400 | [diff] [blame] | 886 | |
Josef Bacik | e3e0520 | 2019-06-20 15:37:55 -0400 | [diff] [blame] | 887 | index = btrfs_bg_flags_to_raid_index(block_group->flags); |
| 888 | factor = btrfs_bg_type_to_factor(block_group->flags); |
| 889 | |
| 890 | /* make sure this block group isn't part of an allocation cluster */ |
| 891 | cluster = &fs_info->data_alloc_cluster; |
| 892 | spin_lock(&cluster->refill_lock); |
| 893 | btrfs_return_cluster_to_free_space(block_group, cluster); |
| 894 | spin_unlock(&cluster->refill_lock); |
| 895 | |
| 896 | /* |
| 897 | * make sure this block group isn't part of a metadata |
| 898 | * allocation cluster |
| 899 | */ |
| 900 | cluster = &fs_info->meta_alloc_cluster; |
| 901 | spin_lock(&cluster->refill_lock); |
| 902 | btrfs_return_cluster_to_free_space(block_group, cluster); |
| 903 | spin_unlock(&cluster->refill_lock); |
| 904 | |
| 905 | path = btrfs_alloc_path(); |
| 906 | if (!path) { |
| 907 | ret = -ENOMEM; |
| 908 | goto out; |
| 909 | } |
| 910 | |
| 911 | /* |
| 912 | * get the inode first so any iput calls done for the io_list |
| 913 | * aren't the final iput (no unlinks allowed now) |
| 914 | */ |
| 915 | inode = lookup_free_space_inode(block_group, path); |
| 916 | |
| 917 | mutex_lock(&trans->transaction->cache_write_mutex); |
| 918 | /* |
| 919 | * Make sure our free space cache IO is done before removing the |
| 920 | * free space inode |
| 921 | */ |
| 922 | spin_lock(&trans->transaction->dirty_bgs_lock); |
| 923 | if (!list_empty(&block_group->io_list)) { |
| 924 | list_del_init(&block_group->io_list); |
| 925 | |
| 926 | WARN_ON(!IS_ERR(inode) && inode != block_group->io_ctl.inode); |
| 927 | |
| 928 | spin_unlock(&trans->transaction->dirty_bgs_lock); |
| 929 | btrfs_wait_cache_io(trans, block_group, path); |
| 930 | btrfs_put_block_group(block_group); |
| 931 | spin_lock(&trans->transaction->dirty_bgs_lock); |
| 932 | } |
| 933 | |
| 934 | if (!list_empty(&block_group->dirty_list)) { |
| 935 | list_del_init(&block_group->dirty_list); |
| 936 | remove_rsv = true; |
| 937 | btrfs_put_block_group(block_group); |
| 938 | } |
| 939 | spin_unlock(&trans->transaction->dirty_bgs_lock); |
| 940 | mutex_unlock(&trans->transaction->cache_write_mutex); |
| 941 | |
| 942 | if (!IS_ERR(inode)) { |
| 943 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
| 944 | if (ret) { |
| 945 | btrfs_add_delayed_iput(inode); |
| 946 | goto out; |
| 947 | } |
| 948 | clear_nlink(inode); |
| 949 | /* One for the block groups ref */ |
| 950 | spin_lock(&block_group->lock); |
| 951 | if (block_group->iref) { |
| 952 | block_group->iref = 0; |
| 953 | block_group->inode = NULL; |
| 954 | spin_unlock(&block_group->lock); |
| 955 | iput(inode); |
| 956 | } else { |
| 957 | spin_unlock(&block_group->lock); |
| 958 | } |
| 959 | /* One for our lookup ref */ |
| 960 | btrfs_add_delayed_iput(inode); |
| 961 | } |
| 962 | |
| 963 | key.objectid = BTRFS_FREE_SPACE_OBJECTID; |
Josef Bacik | e3e0520 | 2019-06-20 15:37:55 -0400 | [diff] [blame] | 964 | key.type = 0; |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame^] | 965 | key.offset = block_group->start; |
Josef Bacik | e3e0520 | 2019-06-20 15:37:55 -0400 | [diff] [blame] | 966 | |
| 967 | ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1); |
| 968 | if (ret < 0) |
| 969 | goto out; |
| 970 | if (ret > 0) |
| 971 | btrfs_release_path(path); |
| 972 | if (ret == 0) { |
| 973 | ret = btrfs_del_item(trans, tree_root, path); |
| 974 | if (ret) |
| 975 | goto out; |
| 976 | btrfs_release_path(path); |
| 977 | } |
| 978 | |
| 979 | spin_lock(&fs_info->block_group_cache_lock); |
| 980 | rb_erase(&block_group->cache_node, |
| 981 | &fs_info->block_group_cache_tree); |
| 982 | RB_CLEAR_NODE(&block_group->cache_node); |
| 983 | |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame^] | 984 | if (fs_info->first_logical_byte == block_group->start) |
Josef Bacik | e3e0520 | 2019-06-20 15:37:55 -0400 | [diff] [blame] | 985 | fs_info->first_logical_byte = (u64)-1; |
| 986 | spin_unlock(&fs_info->block_group_cache_lock); |
| 987 | |
| 988 | down_write(&block_group->space_info->groups_sem); |
| 989 | /* |
| 990 | * we must use list_del_init so people can check to see if they |
| 991 | * are still on the list after taking the semaphore |
| 992 | */ |
| 993 | list_del_init(&block_group->list); |
| 994 | if (list_empty(&block_group->space_info->block_groups[index])) { |
| 995 | kobj = block_group->space_info->block_group_kobjs[index]; |
| 996 | block_group->space_info->block_group_kobjs[index] = NULL; |
| 997 | clear_avail_alloc_bits(fs_info, block_group->flags); |
| 998 | } |
| 999 | up_write(&block_group->space_info->groups_sem); |
| 1000 | clear_incompat_bg_bits(fs_info, block_group->flags); |
| 1001 | if (kobj) { |
| 1002 | kobject_del(kobj); |
| 1003 | kobject_put(kobj); |
| 1004 | } |
| 1005 | |
| 1006 | if (block_group->has_caching_ctl) |
| 1007 | caching_ctl = btrfs_get_caching_control(block_group); |
| 1008 | if (block_group->cached == BTRFS_CACHE_STARTED) |
| 1009 | btrfs_wait_block_group_cache_done(block_group); |
| 1010 | if (block_group->has_caching_ctl) { |
| 1011 | down_write(&fs_info->commit_root_sem); |
| 1012 | if (!caching_ctl) { |
| 1013 | struct btrfs_caching_control *ctl; |
| 1014 | |
| 1015 | list_for_each_entry(ctl, |
| 1016 | &fs_info->caching_block_groups, list) |
| 1017 | if (ctl->block_group == block_group) { |
| 1018 | caching_ctl = ctl; |
| 1019 | refcount_inc(&caching_ctl->count); |
| 1020 | break; |
| 1021 | } |
| 1022 | } |
| 1023 | if (caching_ctl) |
| 1024 | list_del_init(&caching_ctl->list); |
| 1025 | up_write(&fs_info->commit_root_sem); |
| 1026 | if (caching_ctl) { |
| 1027 | /* Once for the caching bgs list and once for us. */ |
| 1028 | btrfs_put_caching_control(caching_ctl); |
| 1029 | btrfs_put_caching_control(caching_ctl); |
| 1030 | } |
| 1031 | } |
| 1032 | |
| 1033 | spin_lock(&trans->transaction->dirty_bgs_lock); |
| 1034 | WARN_ON(!list_empty(&block_group->dirty_list)); |
| 1035 | WARN_ON(!list_empty(&block_group->io_list)); |
| 1036 | spin_unlock(&trans->transaction->dirty_bgs_lock); |
| 1037 | |
| 1038 | btrfs_remove_free_space_cache(block_group); |
| 1039 | |
| 1040 | spin_lock(&block_group->space_info->lock); |
| 1041 | list_del_init(&block_group->ro_list); |
| 1042 | |
| 1043 | if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) { |
| 1044 | WARN_ON(block_group->space_info->total_bytes |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame^] | 1045 | < block_group->length); |
Josef Bacik | e3e0520 | 2019-06-20 15:37:55 -0400 | [diff] [blame] | 1046 | WARN_ON(block_group->space_info->bytes_readonly |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame^] | 1047 | < block_group->length); |
Josef Bacik | e3e0520 | 2019-06-20 15:37:55 -0400 | [diff] [blame] | 1048 | WARN_ON(block_group->space_info->disk_total |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame^] | 1049 | < block_group->length * factor); |
Josef Bacik | e3e0520 | 2019-06-20 15:37:55 -0400 | [diff] [blame] | 1050 | } |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame^] | 1051 | block_group->space_info->total_bytes -= block_group->length; |
| 1052 | block_group->space_info->bytes_readonly -= block_group->length; |
| 1053 | block_group->space_info->disk_total -= block_group->length * factor; |
Josef Bacik | e3e0520 | 2019-06-20 15:37:55 -0400 | [diff] [blame] | 1054 | |
| 1055 | spin_unlock(&block_group->space_info->lock); |
| 1056 | |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame^] | 1057 | key.objectid = block_group->start; |
| 1058 | key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; |
| 1059 | key.offset = block_group->length; |
Josef Bacik | e3e0520 | 2019-06-20 15:37:55 -0400 | [diff] [blame] | 1060 | |
| 1061 | mutex_lock(&fs_info->chunk_mutex); |
| 1062 | spin_lock(&block_group->lock); |
| 1063 | block_group->removed = 1; |
| 1064 | /* |
| 1065 | * At this point trimming can't start on this block group, because we |
| 1066 | * removed the block group from the tree fs_info->block_group_cache_tree |
| 1067 | * so no one can't find it anymore and even if someone already got this |
| 1068 | * block group before we removed it from the rbtree, they have already |
| 1069 | * incremented block_group->trimming - if they didn't, they won't find |
| 1070 | * any free space entries because we already removed them all when we |
| 1071 | * called btrfs_remove_free_space_cache(). |
| 1072 | * |
| 1073 | * And we must not remove the extent map from the fs_info->mapping_tree |
| 1074 | * to prevent the same logical address range and physical device space |
| 1075 | * ranges from being reused for a new block group. This is because our |
| 1076 | * fs trim operation (btrfs_trim_fs() / btrfs_ioctl_fitrim()) is |
| 1077 | * completely transactionless, so while it is trimming a range the |
| 1078 | * currently running transaction might finish and a new one start, |
| 1079 | * allowing for new block groups to be created that can reuse the same |
| 1080 | * physical device locations unless we take this special care. |
| 1081 | * |
| 1082 | * There may also be an implicit trim operation if the file system |
| 1083 | * is mounted with -odiscard. The same protections must remain |
| 1084 | * in place until the extents have been discarded completely when |
| 1085 | * the transaction commit has completed. |
| 1086 | */ |
| 1087 | remove_em = (atomic_read(&block_group->trimming) == 0); |
| 1088 | spin_unlock(&block_group->lock); |
| 1089 | |
| 1090 | mutex_unlock(&fs_info->chunk_mutex); |
| 1091 | |
| 1092 | ret = remove_block_group_free_space(trans, block_group); |
| 1093 | if (ret) |
| 1094 | goto out; |
| 1095 | |
| 1096 | btrfs_put_block_group(block_group); |
| 1097 | btrfs_put_block_group(block_group); |
| 1098 | |
| 1099 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1100 | if (ret > 0) |
| 1101 | ret = -EIO; |
| 1102 | if (ret < 0) |
| 1103 | goto out; |
| 1104 | |
| 1105 | ret = btrfs_del_item(trans, root, path); |
| 1106 | if (ret) |
| 1107 | goto out; |
| 1108 | |
| 1109 | if (remove_em) { |
| 1110 | struct extent_map_tree *em_tree; |
| 1111 | |
| 1112 | em_tree = &fs_info->mapping_tree; |
| 1113 | write_lock(&em_tree->lock); |
| 1114 | remove_extent_mapping(em_tree, em); |
| 1115 | write_unlock(&em_tree->lock); |
| 1116 | /* once for the tree */ |
| 1117 | free_extent_map(em); |
| 1118 | } |
| 1119 | out: |
| 1120 | if (remove_rsv) |
| 1121 | btrfs_delayed_refs_rsv_release(fs_info, 1); |
| 1122 | btrfs_free_path(path); |
| 1123 | return ret; |
| 1124 | } |
| 1125 | |
| 1126 | struct btrfs_trans_handle *btrfs_start_trans_remove_block_group( |
| 1127 | struct btrfs_fs_info *fs_info, const u64 chunk_offset) |
| 1128 | { |
| 1129 | struct extent_map_tree *em_tree = &fs_info->mapping_tree; |
| 1130 | struct extent_map *em; |
| 1131 | struct map_lookup *map; |
| 1132 | unsigned int num_items; |
| 1133 | |
| 1134 | read_lock(&em_tree->lock); |
| 1135 | em = lookup_extent_mapping(em_tree, chunk_offset, 1); |
| 1136 | read_unlock(&em_tree->lock); |
| 1137 | ASSERT(em && em->start == chunk_offset); |
| 1138 | |
| 1139 | /* |
| 1140 | * We need to reserve 3 + N units from the metadata space info in order |
| 1141 | * to remove a block group (done at btrfs_remove_chunk() and at |
| 1142 | * btrfs_remove_block_group()), which are used for: |
| 1143 | * |
| 1144 | * 1 unit for adding the free space inode's orphan (located in the tree |
| 1145 | * of tree roots). |
| 1146 | * 1 unit for deleting the block group item (located in the extent |
| 1147 | * tree). |
| 1148 | * 1 unit for deleting the free space item (located in tree of tree |
| 1149 | * roots). |
| 1150 | * N units for deleting N device extent items corresponding to each |
| 1151 | * stripe (located in the device tree). |
| 1152 | * |
| 1153 | * In order to remove a block group we also need to reserve units in the |
| 1154 | * system space info in order to update the chunk tree (update one or |
| 1155 | * more device items and remove one chunk item), but this is done at |
| 1156 | * btrfs_remove_chunk() through a call to check_system_chunk(). |
| 1157 | */ |
| 1158 | map = em->map_lookup; |
| 1159 | num_items = 3 + map->num_stripes; |
| 1160 | free_extent_map(em); |
| 1161 | |
| 1162 | return btrfs_start_transaction_fallback_global_rsv(fs_info->extent_root, |
| 1163 | num_items, 1); |
| 1164 | } |
| 1165 | |
| 1166 | /* |
Josef Bacik | 26ce209 | 2019-06-20 15:37:59 -0400 | [diff] [blame] | 1167 | * Mark block group @cache read-only, so later write won't happen to block |
| 1168 | * group @cache. |
| 1169 | * |
| 1170 | * If @force is not set, this function will only mark the block group readonly |
| 1171 | * if we have enough free space (1M) in other metadata/system block groups. |
| 1172 | * If @force is not set, this function will mark the block group readonly |
| 1173 | * without checking free space. |
| 1174 | * |
| 1175 | * NOTE: This function doesn't care if other block groups can contain all the |
| 1176 | * data in this block group. That check should be done by relocation routine, |
| 1177 | * not this function. |
| 1178 | */ |
Josef Bacik | e11c040 | 2019-06-20 15:38:07 -0400 | [diff] [blame] | 1179 | static int inc_block_group_ro(struct btrfs_block_group_cache *cache, int force) |
Josef Bacik | 26ce209 | 2019-06-20 15:37:59 -0400 | [diff] [blame] | 1180 | { |
| 1181 | struct btrfs_space_info *sinfo = cache->space_info; |
| 1182 | u64 num_bytes; |
| 1183 | u64 sinfo_used; |
| 1184 | u64 min_allocable_bytes; |
| 1185 | int ret = -ENOSPC; |
| 1186 | |
| 1187 | /* |
| 1188 | * We need some metadata space and system metadata space for |
| 1189 | * allocating chunks in some corner cases until we force to set |
| 1190 | * it to be readonly. |
| 1191 | */ |
| 1192 | if ((sinfo->flags & |
| 1193 | (BTRFS_BLOCK_GROUP_SYSTEM | BTRFS_BLOCK_GROUP_METADATA)) && |
| 1194 | !force) |
| 1195 | min_allocable_bytes = SZ_1M; |
| 1196 | else |
| 1197 | min_allocable_bytes = 0; |
| 1198 | |
| 1199 | spin_lock(&sinfo->lock); |
| 1200 | spin_lock(&cache->lock); |
| 1201 | |
| 1202 | if (cache->ro) { |
| 1203 | cache->ro++; |
| 1204 | ret = 0; |
| 1205 | goto out; |
| 1206 | } |
| 1207 | |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame^] | 1208 | num_bytes = cache->length - cache->reserved - cache->pinned - |
David Sterba | bf38be6 | 2019-10-23 18:48:11 +0200 | [diff] [blame] | 1209 | cache->bytes_super - cache->used; |
Josef Bacik | 26ce209 | 2019-06-20 15:37:59 -0400 | [diff] [blame] | 1210 | sinfo_used = btrfs_space_info_used(sinfo, true); |
| 1211 | |
| 1212 | /* |
| 1213 | * sinfo_used + num_bytes should always <= sinfo->total_bytes. |
| 1214 | * |
| 1215 | * Here we make sure if we mark this bg RO, we still have enough |
| 1216 | * free space as buffer (if min_allocable_bytes is not 0). |
| 1217 | */ |
| 1218 | if (sinfo_used + num_bytes + min_allocable_bytes <= |
| 1219 | sinfo->total_bytes) { |
| 1220 | sinfo->bytes_readonly += num_bytes; |
| 1221 | cache->ro++; |
| 1222 | list_add_tail(&cache->ro_list, &sinfo->ro_bgs); |
| 1223 | ret = 0; |
| 1224 | } |
| 1225 | out: |
| 1226 | spin_unlock(&cache->lock); |
| 1227 | spin_unlock(&sinfo->lock); |
| 1228 | if (ret == -ENOSPC && btrfs_test_opt(cache->fs_info, ENOSPC_DEBUG)) { |
| 1229 | btrfs_info(cache->fs_info, |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame^] | 1230 | "unable to make block group %llu ro", cache->start); |
Josef Bacik | 26ce209 | 2019-06-20 15:37:59 -0400 | [diff] [blame] | 1231 | btrfs_info(cache->fs_info, |
| 1232 | "sinfo_used=%llu bg_num_bytes=%llu min_allocable=%llu", |
| 1233 | sinfo_used, num_bytes, min_allocable_bytes); |
| 1234 | btrfs_dump_space_info(cache->fs_info, cache->space_info, 0, 0); |
| 1235 | } |
| 1236 | return ret; |
| 1237 | } |
| 1238 | |
| 1239 | /* |
Josef Bacik | e3e0520 | 2019-06-20 15:37:55 -0400 | [diff] [blame] | 1240 | * Process the unused_bgs list and remove any that don't have any allocated |
| 1241 | * space inside of them. |
| 1242 | */ |
| 1243 | void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info) |
| 1244 | { |
| 1245 | struct btrfs_block_group_cache *block_group; |
| 1246 | struct btrfs_space_info *space_info; |
| 1247 | struct btrfs_trans_handle *trans; |
| 1248 | int ret = 0; |
| 1249 | |
| 1250 | if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags)) |
| 1251 | return; |
| 1252 | |
| 1253 | spin_lock(&fs_info->unused_bgs_lock); |
| 1254 | while (!list_empty(&fs_info->unused_bgs)) { |
| 1255 | u64 start, end; |
| 1256 | int trimming; |
| 1257 | |
| 1258 | block_group = list_first_entry(&fs_info->unused_bgs, |
| 1259 | struct btrfs_block_group_cache, |
| 1260 | bg_list); |
| 1261 | list_del_init(&block_group->bg_list); |
| 1262 | |
| 1263 | space_info = block_group->space_info; |
| 1264 | |
| 1265 | if (ret || btrfs_mixed_space_info(space_info)) { |
| 1266 | btrfs_put_block_group(block_group); |
| 1267 | continue; |
| 1268 | } |
| 1269 | spin_unlock(&fs_info->unused_bgs_lock); |
| 1270 | |
| 1271 | mutex_lock(&fs_info->delete_unused_bgs_mutex); |
| 1272 | |
| 1273 | /* Don't want to race with allocators so take the groups_sem */ |
| 1274 | down_write(&space_info->groups_sem); |
| 1275 | spin_lock(&block_group->lock); |
| 1276 | if (block_group->reserved || block_group->pinned || |
David Sterba | bf38be6 | 2019-10-23 18:48:11 +0200 | [diff] [blame] | 1277 | block_group->used || block_group->ro || |
Josef Bacik | e3e0520 | 2019-06-20 15:37:55 -0400 | [diff] [blame] | 1278 | list_is_singular(&block_group->list)) { |
| 1279 | /* |
| 1280 | * We want to bail if we made new allocations or have |
| 1281 | * outstanding allocations in this block group. We do |
| 1282 | * the ro check in case balance is currently acting on |
| 1283 | * this block group. |
| 1284 | */ |
| 1285 | trace_btrfs_skip_unused_block_group(block_group); |
| 1286 | spin_unlock(&block_group->lock); |
| 1287 | up_write(&space_info->groups_sem); |
| 1288 | goto next; |
| 1289 | } |
| 1290 | spin_unlock(&block_group->lock); |
| 1291 | |
| 1292 | /* We don't want to force the issue, only flip if it's ok. */ |
Josef Bacik | e11c040 | 2019-06-20 15:38:07 -0400 | [diff] [blame] | 1293 | ret = inc_block_group_ro(block_group, 0); |
Josef Bacik | e3e0520 | 2019-06-20 15:37:55 -0400 | [diff] [blame] | 1294 | up_write(&space_info->groups_sem); |
| 1295 | if (ret < 0) { |
| 1296 | ret = 0; |
| 1297 | goto next; |
| 1298 | } |
| 1299 | |
| 1300 | /* |
| 1301 | * Want to do this before we do anything else so we can recover |
| 1302 | * properly if we fail to join the transaction. |
| 1303 | */ |
| 1304 | trans = btrfs_start_trans_remove_block_group(fs_info, |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame^] | 1305 | block_group->start); |
Josef Bacik | e3e0520 | 2019-06-20 15:37:55 -0400 | [diff] [blame] | 1306 | if (IS_ERR(trans)) { |
| 1307 | btrfs_dec_block_group_ro(block_group); |
| 1308 | ret = PTR_ERR(trans); |
| 1309 | goto next; |
| 1310 | } |
| 1311 | |
| 1312 | /* |
| 1313 | * We could have pending pinned extents for this block group, |
| 1314 | * just delete them, we don't care about them anymore. |
| 1315 | */ |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame^] | 1316 | start = block_group->start; |
| 1317 | end = start + block_group->length - 1; |
Josef Bacik | e3e0520 | 2019-06-20 15:37:55 -0400 | [diff] [blame] | 1318 | /* |
| 1319 | * Hold the unused_bg_unpin_mutex lock to avoid racing with |
| 1320 | * btrfs_finish_extent_commit(). If we are at transaction N, |
| 1321 | * another task might be running finish_extent_commit() for the |
| 1322 | * previous transaction N - 1, and have seen a range belonging |
| 1323 | * to the block group in freed_extents[] before we were able to |
| 1324 | * clear the whole block group range from freed_extents[]. This |
| 1325 | * means that task can lookup for the block group after we |
| 1326 | * unpinned it from freed_extents[] and removed it, leading to |
| 1327 | * a BUG_ON() at btrfs_unpin_extent_range(). |
| 1328 | */ |
| 1329 | mutex_lock(&fs_info->unused_bg_unpin_mutex); |
| 1330 | ret = clear_extent_bits(&fs_info->freed_extents[0], start, end, |
| 1331 | EXTENT_DIRTY); |
| 1332 | if (ret) { |
| 1333 | mutex_unlock(&fs_info->unused_bg_unpin_mutex); |
| 1334 | btrfs_dec_block_group_ro(block_group); |
| 1335 | goto end_trans; |
| 1336 | } |
| 1337 | ret = clear_extent_bits(&fs_info->freed_extents[1], start, end, |
| 1338 | EXTENT_DIRTY); |
| 1339 | if (ret) { |
| 1340 | mutex_unlock(&fs_info->unused_bg_unpin_mutex); |
| 1341 | btrfs_dec_block_group_ro(block_group); |
| 1342 | goto end_trans; |
| 1343 | } |
| 1344 | mutex_unlock(&fs_info->unused_bg_unpin_mutex); |
| 1345 | |
| 1346 | /* Reset pinned so btrfs_put_block_group doesn't complain */ |
| 1347 | spin_lock(&space_info->lock); |
| 1348 | spin_lock(&block_group->lock); |
| 1349 | |
| 1350 | btrfs_space_info_update_bytes_pinned(fs_info, space_info, |
| 1351 | -block_group->pinned); |
| 1352 | space_info->bytes_readonly += block_group->pinned; |
| 1353 | percpu_counter_add_batch(&space_info->total_bytes_pinned, |
| 1354 | -block_group->pinned, |
| 1355 | BTRFS_TOTAL_BYTES_PINNED_BATCH); |
| 1356 | block_group->pinned = 0; |
| 1357 | |
| 1358 | spin_unlock(&block_group->lock); |
| 1359 | spin_unlock(&space_info->lock); |
| 1360 | |
| 1361 | /* DISCARD can flip during remount */ |
| 1362 | trimming = btrfs_test_opt(fs_info, DISCARD); |
| 1363 | |
| 1364 | /* Implicit trim during transaction commit. */ |
| 1365 | if (trimming) |
| 1366 | btrfs_get_block_group_trimming(block_group); |
| 1367 | |
| 1368 | /* |
| 1369 | * Btrfs_remove_chunk will abort the transaction if things go |
| 1370 | * horribly wrong. |
| 1371 | */ |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame^] | 1372 | ret = btrfs_remove_chunk(trans, block_group->start); |
Josef Bacik | e3e0520 | 2019-06-20 15:37:55 -0400 | [diff] [blame] | 1373 | |
| 1374 | if (ret) { |
| 1375 | if (trimming) |
| 1376 | btrfs_put_block_group_trimming(block_group); |
| 1377 | goto end_trans; |
| 1378 | } |
| 1379 | |
| 1380 | /* |
| 1381 | * If we're not mounted with -odiscard, we can just forget |
| 1382 | * about this block group. Otherwise we'll need to wait |
| 1383 | * until transaction commit to do the actual discard. |
| 1384 | */ |
| 1385 | if (trimming) { |
| 1386 | spin_lock(&fs_info->unused_bgs_lock); |
| 1387 | /* |
| 1388 | * A concurrent scrub might have added us to the list |
| 1389 | * fs_info->unused_bgs, so use a list_move operation |
| 1390 | * to add the block group to the deleted_bgs list. |
| 1391 | */ |
| 1392 | list_move(&block_group->bg_list, |
| 1393 | &trans->transaction->deleted_bgs); |
| 1394 | spin_unlock(&fs_info->unused_bgs_lock); |
| 1395 | btrfs_get_block_group(block_group); |
| 1396 | } |
| 1397 | end_trans: |
| 1398 | btrfs_end_transaction(trans); |
| 1399 | next: |
| 1400 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
| 1401 | btrfs_put_block_group(block_group); |
| 1402 | spin_lock(&fs_info->unused_bgs_lock); |
| 1403 | } |
| 1404 | spin_unlock(&fs_info->unused_bgs_lock); |
| 1405 | } |
| 1406 | |
| 1407 | void btrfs_mark_bg_unused(struct btrfs_block_group_cache *bg) |
| 1408 | { |
| 1409 | struct btrfs_fs_info *fs_info = bg->fs_info; |
| 1410 | |
| 1411 | spin_lock(&fs_info->unused_bgs_lock); |
| 1412 | if (list_empty(&bg->bg_list)) { |
| 1413 | btrfs_get_block_group(bg); |
| 1414 | trace_btrfs_add_unused_block_group(bg); |
| 1415 | list_add_tail(&bg->bg_list, &fs_info->unused_bgs); |
| 1416 | } |
| 1417 | spin_unlock(&fs_info->unused_bgs_lock); |
| 1418 | } |
Josef Bacik | 4358d963 | 2019-06-20 15:37:57 -0400 | [diff] [blame] | 1419 | |
| 1420 | static int find_first_block_group(struct btrfs_fs_info *fs_info, |
| 1421 | struct btrfs_path *path, |
| 1422 | struct btrfs_key *key) |
| 1423 | { |
| 1424 | struct btrfs_root *root = fs_info->extent_root; |
| 1425 | int ret = 0; |
| 1426 | struct btrfs_key found_key; |
| 1427 | struct extent_buffer *leaf; |
| 1428 | struct btrfs_block_group_item bg; |
| 1429 | u64 flags; |
| 1430 | int slot; |
| 1431 | |
| 1432 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); |
| 1433 | if (ret < 0) |
| 1434 | goto out; |
| 1435 | |
| 1436 | while (1) { |
| 1437 | slot = path->slots[0]; |
| 1438 | leaf = path->nodes[0]; |
| 1439 | if (slot >= btrfs_header_nritems(leaf)) { |
| 1440 | ret = btrfs_next_leaf(root, path); |
| 1441 | if (ret == 0) |
| 1442 | continue; |
| 1443 | if (ret < 0) |
| 1444 | goto out; |
| 1445 | break; |
| 1446 | } |
| 1447 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 1448 | |
| 1449 | if (found_key.objectid >= key->objectid && |
| 1450 | found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) { |
| 1451 | struct extent_map_tree *em_tree; |
| 1452 | struct extent_map *em; |
| 1453 | |
| 1454 | em_tree = &root->fs_info->mapping_tree; |
| 1455 | read_lock(&em_tree->lock); |
| 1456 | em = lookup_extent_mapping(em_tree, found_key.objectid, |
| 1457 | found_key.offset); |
| 1458 | read_unlock(&em_tree->lock); |
| 1459 | if (!em) { |
| 1460 | btrfs_err(fs_info, |
| 1461 | "logical %llu len %llu found bg but no related chunk", |
| 1462 | found_key.objectid, found_key.offset); |
| 1463 | ret = -ENOENT; |
| 1464 | } else if (em->start != found_key.objectid || |
| 1465 | em->len != found_key.offset) { |
| 1466 | btrfs_err(fs_info, |
| 1467 | "block group %llu len %llu mismatch with chunk %llu len %llu", |
| 1468 | found_key.objectid, found_key.offset, |
| 1469 | em->start, em->len); |
| 1470 | ret = -EUCLEAN; |
| 1471 | } else { |
| 1472 | read_extent_buffer(leaf, &bg, |
| 1473 | btrfs_item_ptr_offset(leaf, slot), |
| 1474 | sizeof(bg)); |
David Sterba | de0dc45 | 2019-10-23 18:48:18 +0200 | [diff] [blame] | 1475 | flags = btrfs_stack_block_group_flags(&bg) & |
Josef Bacik | 4358d963 | 2019-06-20 15:37:57 -0400 | [diff] [blame] | 1476 | BTRFS_BLOCK_GROUP_TYPE_MASK; |
| 1477 | |
| 1478 | if (flags != (em->map_lookup->type & |
| 1479 | BTRFS_BLOCK_GROUP_TYPE_MASK)) { |
| 1480 | btrfs_err(fs_info, |
| 1481 | "block group %llu len %llu type flags 0x%llx mismatch with chunk type flags 0x%llx", |
| 1482 | found_key.objectid, |
| 1483 | found_key.offset, flags, |
| 1484 | (BTRFS_BLOCK_GROUP_TYPE_MASK & |
| 1485 | em->map_lookup->type)); |
| 1486 | ret = -EUCLEAN; |
| 1487 | } else { |
| 1488 | ret = 0; |
| 1489 | } |
| 1490 | } |
| 1491 | free_extent_map(em); |
| 1492 | goto out; |
| 1493 | } |
| 1494 | path->slots[0]++; |
| 1495 | } |
| 1496 | out: |
| 1497 | return ret; |
| 1498 | } |
| 1499 | |
| 1500 | static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags) |
| 1501 | { |
| 1502 | u64 extra_flags = chunk_to_extended(flags) & |
| 1503 | BTRFS_EXTENDED_PROFILE_MASK; |
| 1504 | |
| 1505 | write_seqlock(&fs_info->profiles_lock); |
| 1506 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
| 1507 | fs_info->avail_data_alloc_bits |= extra_flags; |
| 1508 | if (flags & BTRFS_BLOCK_GROUP_METADATA) |
| 1509 | fs_info->avail_metadata_alloc_bits |= extra_flags; |
| 1510 | if (flags & BTRFS_BLOCK_GROUP_SYSTEM) |
| 1511 | fs_info->avail_system_alloc_bits |= extra_flags; |
| 1512 | write_sequnlock(&fs_info->profiles_lock); |
| 1513 | } |
| 1514 | |
| 1515 | static int exclude_super_stripes(struct btrfs_block_group_cache *cache) |
| 1516 | { |
| 1517 | struct btrfs_fs_info *fs_info = cache->fs_info; |
| 1518 | u64 bytenr; |
| 1519 | u64 *logical; |
| 1520 | int stripe_len; |
| 1521 | int i, nr, ret; |
| 1522 | |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame^] | 1523 | if (cache->start < BTRFS_SUPER_INFO_OFFSET) { |
| 1524 | stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->start; |
Josef Bacik | 4358d963 | 2019-06-20 15:37:57 -0400 | [diff] [blame] | 1525 | cache->bytes_super += stripe_len; |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame^] | 1526 | ret = btrfs_add_excluded_extent(fs_info, cache->start, |
Josef Bacik | 4358d963 | 2019-06-20 15:37:57 -0400 | [diff] [blame] | 1527 | stripe_len); |
| 1528 | if (ret) |
| 1529 | return ret; |
| 1530 | } |
| 1531 | |
| 1532 | for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { |
| 1533 | bytenr = btrfs_sb_offset(i); |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame^] | 1534 | ret = btrfs_rmap_block(fs_info, cache->start, |
Josef Bacik | 4358d963 | 2019-06-20 15:37:57 -0400 | [diff] [blame] | 1535 | bytenr, &logical, &nr, &stripe_len); |
| 1536 | if (ret) |
| 1537 | return ret; |
| 1538 | |
| 1539 | while (nr--) { |
| 1540 | u64 start, len; |
| 1541 | |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame^] | 1542 | if (logical[nr] > cache->start + cache->length) |
Josef Bacik | 4358d963 | 2019-06-20 15:37:57 -0400 | [diff] [blame] | 1543 | continue; |
| 1544 | |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame^] | 1545 | if (logical[nr] + stripe_len <= cache->start) |
Josef Bacik | 4358d963 | 2019-06-20 15:37:57 -0400 | [diff] [blame] | 1546 | continue; |
| 1547 | |
| 1548 | start = logical[nr]; |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame^] | 1549 | if (start < cache->start) { |
| 1550 | start = cache->start; |
Josef Bacik | 4358d963 | 2019-06-20 15:37:57 -0400 | [diff] [blame] | 1551 | len = (logical[nr] + stripe_len) - start; |
| 1552 | } else { |
| 1553 | len = min_t(u64, stripe_len, |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame^] | 1554 | cache->start + cache->length - start); |
Josef Bacik | 4358d963 | 2019-06-20 15:37:57 -0400 | [diff] [blame] | 1555 | } |
| 1556 | |
| 1557 | cache->bytes_super += len; |
| 1558 | ret = btrfs_add_excluded_extent(fs_info, start, len); |
| 1559 | if (ret) { |
| 1560 | kfree(logical); |
| 1561 | return ret; |
| 1562 | } |
| 1563 | } |
| 1564 | |
| 1565 | kfree(logical); |
| 1566 | } |
| 1567 | return 0; |
| 1568 | } |
| 1569 | |
| 1570 | static void link_block_group(struct btrfs_block_group_cache *cache) |
| 1571 | { |
| 1572 | struct btrfs_space_info *space_info = cache->space_info; |
| 1573 | int index = btrfs_bg_flags_to_raid_index(cache->flags); |
| 1574 | bool first = false; |
| 1575 | |
| 1576 | down_write(&space_info->groups_sem); |
| 1577 | if (list_empty(&space_info->block_groups[index])) |
| 1578 | first = true; |
| 1579 | list_add_tail(&cache->list, &space_info->block_groups[index]); |
| 1580 | up_write(&space_info->groups_sem); |
| 1581 | |
| 1582 | if (first) |
| 1583 | btrfs_sysfs_add_block_group_type(cache); |
| 1584 | } |
| 1585 | |
| 1586 | static struct btrfs_block_group_cache *btrfs_create_block_group_cache( |
| 1587 | struct btrfs_fs_info *fs_info, u64 start, u64 size) |
| 1588 | { |
| 1589 | struct btrfs_block_group_cache *cache; |
| 1590 | |
| 1591 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
| 1592 | if (!cache) |
| 1593 | return NULL; |
| 1594 | |
| 1595 | cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl), |
| 1596 | GFP_NOFS); |
| 1597 | if (!cache->free_space_ctl) { |
| 1598 | kfree(cache); |
| 1599 | return NULL; |
| 1600 | } |
| 1601 | |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame^] | 1602 | cache->start = start; |
| 1603 | cache->length = size; |
Josef Bacik | 4358d963 | 2019-06-20 15:37:57 -0400 | [diff] [blame] | 1604 | |
| 1605 | cache->fs_info = fs_info; |
| 1606 | cache->full_stripe_len = btrfs_full_stripe_len(fs_info, start); |
| 1607 | set_free_space_tree_thresholds(cache); |
| 1608 | |
| 1609 | atomic_set(&cache->count, 1); |
| 1610 | spin_lock_init(&cache->lock); |
| 1611 | init_rwsem(&cache->data_rwsem); |
| 1612 | INIT_LIST_HEAD(&cache->list); |
| 1613 | INIT_LIST_HEAD(&cache->cluster_list); |
| 1614 | INIT_LIST_HEAD(&cache->bg_list); |
| 1615 | INIT_LIST_HEAD(&cache->ro_list); |
| 1616 | INIT_LIST_HEAD(&cache->dirty_list); |
| 1617 | INIT_LIST_HEAD(&cache->io_list); |
| 1618 | btrfs_init_free_space_ctl(cache); |
| 1619 | atomic_set(&cache->trimming, 0); |
| 1620 | mutex_init(&cache->free_space_lock); |
| 1621 | btrfs_init_full_stripe_locks_tree(&cache->full_stripe_locks_root); |
| 1622 | |
| 1623 | return cache; |
| 1624 | } |
| 1625 | |
| 1626 | /* |
| 1627 | * Iterate all chunks and verify that each of them has the corresponding block |
| 1628 | * group |
| 1629 | */ |
| 1630 | static int check_chunk_block_group_mappings(struct btrfs_fs_info *fs_info) |
| 1631 | { |
| 1632 | struct extent_map_tree *map_tree = &fs_info->mapping_tree; |
| 1633 | struct extent_map *em; |
| 1634 | struct btrfs_block_group_cache *bg; |
| 1635 | u64 start = 0; |
| 1636 | int ret = 0; |
| 1637 | |
| 1638 | while (1) { |
| 1639 | read_lock(&map_tree->lock); |
| 1640 | /* |
| 1641 | * lookup_extent_mapping will return the first extent map |
| 1642 | * intersecting the range, so setting @len to 1 is enough to |
| 1643 | * get the first chunk. |
| 1644 | */ |
| 1645 | em = lookup_extent_mapping(map_tree, start, 1); |
| 1646 | read_unlock(&map_tree->lock); |
| 1647 | if (!em) |
| 1648 | break; |
| 1649 | |
| 1650 | bg = btrfs_lookup_block_group(fs_info, em->start); |
| 1651 | if (!bg) { |
| 1652 | btrfs_err(fs_info, |
| 1653 | "chunk start=%llu len=%llu doesn't have corresponding block group", |
| 1654 | em->start, em->len); |
| 1655 | ret = -EUCLEAN; |
| 1656 | free_extent_map(em); |
| 1657 | break; |
| 1658 | } |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame^] | 1659 | if (bg->start != em->start || bg->length != em->len || |
Josef Bacik | 4358d963 | 2019-06-20 15:37:57 -0400 | [diff] [blame] | 1660 | (bg->flags & BTRFS_BLOCK_GROUP_TYPE_MASK) != |
| 1661 | (em->map_lookup->type & BTRFS_BLOCK_GROUP_TYPE_MASK)) { |
| 1662 | btrfs_err(fs_info, |
| 1663 | "chunk start=%llu len=%llu flags=0x%llx doesn't match block group start=%llu len=%llu flags=0x%llx", |
| 1664 | em->start, em->len, |
| 1665 | em->map_lookup->type & BTRFS_BLOCK_GROUP_TYPE_MASK, |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame^] | 1666 | bg->start, bg->length, |
Josef Bacik | 4358d963 | 2019-06-20 15:37:57 -0400 | [diff] [blame] | 1667 | bg->flags & BTRFS_BLOCK_GROUP_TYPE_MASK); |
| 1668 | ret = -EUCLEAN; |
| 1669 | free_extent_map(em); |
| 1670 | btrfs_put_block_group(bg); |
| 1671 | break; |
| 1672 | } |
| 1673 | start = em->start + em->len; |
| 1674 | free_extent_map(em); |
| 1675 | btrfs_put_block_group(bg); |
| 1676 | } |
| 1677 | return ret; |
| 1678 | } |
| 1679 | |
| 1680 | int btrfs_read_block_groups(struct btrfs_fs_info *info) |
| 1681 | { |
| 1682 | struct btrfs_path *path; |
| 1683 | int ret; |
| 1684 | struct btrfs_block_group_cache *cache; |
| 1685 | struct btrfs_space_info *space_info; |
| 1686 | struct btrfs_key key; |
| 1687 | struct btrfs_key found_key; |
| 1688 | struct extent_buffer *leaf; |
| 1689 | int need_clear = 0; |
| 1690 | u64 cache_gen; |
| 1691 | u64 feature; |
| 1692 | int mixed; |
| 1693 | |
| 1694 | feature = btrfs_super_incompat_flags(info->super_copy); |
| 1695 | mixed = !!(feature & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS); |
| 1696 | |
| 1697 | key.objectid = 0; |
| 1698 | key.offset = 0; |
| 1699 | key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; |
| 1700 | path = btrfs_alloc_path(); |
| 1701 | if (!path) |
| 1702 | return -ENOMEM; |
| 1703 | path->reada = READA_FORWARD; |
| 1704 | |
| 1705 | cache_gen = btrfs_super_cache_generation(info->super_copy); |
| 1706 | if (btrfs_test_opt(info, SPACE_CACHE) && |
| 1707 | btrfs_super_generation(info->super_copy) != cache_gen) |
| 1708 | need_clear = 1; |
| 1709 | if (btrfs_test_opt(info, CLEAR_CACHE)) |
| 1710 | need_clear = 1; |
| 1711 | |
| 1712 | while (1) { |
David Sterba | bf38be6 | 2019-10-23 18:48:11 +0200 | [diff] [blame] | 1713 | struct btrfs_block_group_item bgi; |
| 1714 | |
Josef Bacik | 4358d963 | 2019-06-20 15:37:57 -0400 | [diff] [blame] | 1715 | ret = find_first_block_group(info, path, &key); |
| 1716 | if (ret > 0) |
| 1717 | break; |
| 1718 | if (ret != 0) |
| 1719 | goto error; |
| 1720 | |
| 1721 | leaf = path->nodes[0]; |
| 1722 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 1723 | |
| 1724 | cache = btrfs_create_block_group_cache(info, found_key.objectid, |
| 1725 | found_key.offset); |
| 1726 | if (!cache) { |
| 1727 | ret = -ENOMEM; |
| 1728 | goto error; |
| 1729 | } |
| 1730 | |
| 1731 | if (need_clear) { |
| 1732 | /* |
| 1733 | * When we mount with old space cache, we need to |
| 1734 | * set BTRFS_DC_CLEAR and set dirty flag. |
| 1735 | * |
| 1736 | * a) Setting 'BTRFS_DC_CLEAR' makes sure that we |
| 1737 | * truncate the old free space cache inode and |
| 1738 | * setup a new one. |
| 1739 | * b) Setting 'dirty flag' makes sure that we flush |
| 1740 | * the new space cache info onto disk. |
| 1741 | */ |
| 1742 | if (btrfs_test_opt(info, SPACE_CACHE)) |
| 1743 | cache->disk_cache_state = BTRFS_DC_CLEAR; |
| 1744 | } |
| 1745 | |
David Sterba | bf38be6 | 2019-10-23 18:48:11 +0200 | [diff] [blame] | 1746 | read_extent_buffer(leaf, &bgi, |
Josef Bacik | 4358d963 | 2019-06-20 15:37:57 -0400 | [diff] [blame] | 1747 | btrfs_item_ptr_offset(leaf, path->slots[0]), |
David Sterba | bf38be6 | 2019-10-23 18:48:11 +0200 | [diff] [blame] | 1748 | sizeof(bgi)); |
David Sterba | 3d97638 | 2019-10-23 18:48:15 +0200 | [diff] [blame] | 1749 | /* cache::chunk_objectid is unused */ |
David Sterba | de0dc45 | 2019-10-23 18:48:18 +0200 | [diff] [blame] | 1750 | cache->used = btrfs_stack_block_group_used(&bgi); |
| 1751 | cache->flags = btrfs_stack_block_group_flags(&bgi); |
Josef Bacik | 4358d963 | 2019-06-20 15:37:57 -0400 | [diff] [blame] | 1752 | if (!mixed && |
| 1753 | ((cache->flags & BTRFS_BLOCK_GROUP_METADATA) && |
| 1754 | (cache->flags & BTRFS_BLOCK_GROUP_DATA))) { |
| 1755 | btrfs_err(info, |
| 1756 | "bg %llu is a mixed block group but filesystem hasn't enabled mixed block groups", |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame^] | 1757 | cache->start); |
Qu Wenruo | 4b654ac | 2019-10-10 10:39:26 +0800 | [diff] [blame] | 1758 | btrfs_put_block_group(cache); |
Josef Bacik | 4358d963 | 2019-06-20 15:37:57 -0400 | [diff] [blame] | 1759 | ret = -EINVAL; |
| 1760 | goto error; |
| 1761 | } |
| 1762 | |
| 1763 | key.objectid = found_key.objectid + found_key.offset; |
| 1764 | btrfs_release_path(path); |
| 1765 | |
| 1766 | /* |
| 1767 | * We need to exclude the super stripes now so that the space |
| 1768 | * info has super bytes accounted for, otherwise we'll think |
| 1769 | * we have more space than we actually do. |
| 1770 | */ |
| 1771 | ret = exclude_super_stripes(cache); |
| 1772 | if (ret) { |
| 1773 | /* |
| 1774 | * We may have excluded something, so call this just in |
| 1775 | * case. |
| 1776 | */ |
| 1777 | btrfs_free_excluded_extents(cache); |
| 1778 | btrfs_put_block_group(cache); |
| 1779 | goto error; |
| 1780 | } |
| 1781 | |
| 1782 | /* |
| 1783 | * Check for two cases, either we are full, and therefore |
| 1784 | * don't need to bother with the caching work since we won't |
| 1785 | * find any space, or we are empty, and we can just add all |
| 1786 | * the space in and be done with it. This saves us _a_lot_ of |
| 1787 | * time, particularly in the full case. |
| 1788 | */ |
David Sterba | bf38be6 | 2019-10-23 18:48:11 +0200 | [diff] [blame] | 1789 | if (found_key.offset == cache->used) { |
Josef Bacik | 4358d963 | 2019-06-20 15:37:57 -0400 | [diff] [blame] | 1790 | cache->last_byte_to_unpin = (u64)-1; |
| 1791 | cache->cached = BTRFS_CACHE_FINISHED; |
| 1792 | btrfs_free_excluded_extents(cache); |
David Sterba | bf38be6 | 2019-10-23 18:48:11 +0200 | [diff] [blame] | 1793 | } else if (cache->used == 0) { |
Josef Bacik | 4358d963 | 2019-06-20 15:37:57 -0400 | [diff] [blame] | 1794 | cache->last_byte_to_unpin = (u64)-1; |
| 1795 | cache->cached = BTRFS_CACHE_FINISHED; |
| 1796 | add_new_free_space(cache, found_key.objectid, |
| 1797 | found_key.objectid + |
| 1798 | found_key.offset); |
| 1799 | btrfs_free_excluded_extents(cache); |
| 1800 | } |
| 1801 | |
| 1802 | ret = btrfs_add_block_group_cache(info, cache); |
| 1803 | if (ret) { |
| 1804 | btrfs_remove_free_space_cache(cache); |
| 1805 | btrfs_put_block_group(cache); |
| 1806 | goto error; |
| 1807 | } |
| 1808 | |
| 1809 | trace_btrfs_add_block_group(info, cache, 0); |
| 1810 | btrfs_update_space_info(info, cache->flags, found_key.offset, |
David Sterba | bf38be6 | 2019-10-23 18:48:11 +0200 | [diff] [blame] | 1811 | cache->used, |
Josef Bacik | 4358d963 | 2019-06-20 15:37:57 -0400 | [diff] [blame] | 1812 | cache->bytes_super, &space_info); |
| 1813 | |
| 1814 | cache->space_info = space_info; |
| 1815 | |
| 1816 | link_block_group(cache); |
| 1817 | |
| 1818 | set_avail_alloc_bits(info, cache->flags); |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame^] | 1819 | if (btrfs_chunk_readonly(info, cache->start)) { |
Josef Bacik | e11c040 | 2019-06-20 15:38:07 -0400 | [diff] [blame] | 1820 | inc_block_group_ro(cache, 1); |
David Sterba | bf38be6 | 2019-10-23 18:48:11 +0200 | [diff] [blame] | 1821 | } else if (cache->used == 0) { |
Josef Bacik | 4358d963 | 2019-06-20 15:37:57 -0400 | [diff] [blame] | 1822 | ASSERT(list_empty(&cache->bg_list)); |
| 1823 | btrfs_mark_bg_unused(cache); |
| 1824 | } |
| 1825 | } |
| 1826 | |
| 1827 | list_for_each_entry_rcu(space_info, &info->space_info, list) { |
| 1828 | if (!(btrfs_get_alloc_profile(info, space_info->flags) & |
| 1829 | (BTRFS_BLOCK_GROUP_RAID10 | |
| 1830 | BTRFS_BLOCK_GROUP_RAID1_MASK | |
| 1831 | BTRFS_BLOCK_GROUP_RAID56_MASK | |
| 1832 | BTRFS_BLOCK_GROUP_DUP))) |
| 1833 | continue; |
| 1834 | /* |
| 1835 | * Avoid allocating from un-mirrored block group if there are |
| 1836 | * mirrored block groups. |
| 1837 | */ |
| 1838 | list_for_each_entry(cache, |
| 1839 | &space_info->block_groups[BTRFS_RAID_RAID0], |
| 1840 | list) |
Josef Bacik | e11c040 | 2019-06-20 15:38:07 -0400 | [diff] [blame] | 1841 | inc_block_group_ro(cache, 1); |
Josef Bacik | 4358d963 | 2019-06-20 15:37:57 -0400 | [diff] [blame] | 1842 | list_for_each_entry(cache, |
| 1843 | &space_info->block_groups[BTRFS_RAID_SINGLE], |
| 1844 | list) |
Josef Bacik | e11c040 | 2019-06-20 15:38:07 -0400 | [diff] [blame] | 1845 | inc_block_group_ro(cache, 1); |
Josef Bacik | 4358d963 | 2019-06-20 15:37:57 -0400 | [diff] [blame] | 1846 | } |
| 1847 | |
| 1848 | btrfs_init_global_block_rsv(info); |
| 1849 | ret = check_chunk_block_group_mappings(info); |
| 1850 | error: |
| 1851 | btrfs_free_path(path); |
| 1852 | return ret; |
| 1853 | } |
| 1854 | |
| 1855 | void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans) |
| 1856 | { |
| 1857 | struct btrfs_fs_info *fs_info = trans->fs_info; |
| 1858 | struct btrfs_block_group_cache *block_group; |
| 1859 | struct btrfs_root *extent_root = fs_info->extent_root; |
| 1860 | struct btrfs_block_group_item item; |
| 1861 | struct btrfs_key key; |
| 1862 | int ret = 0; |
| 1863 | |
| 1864 | if (!trans->can_flush_pending_bgs) |
| 1865 | return; |
| 1866 | |
| 1867 | while (!list_empty(&trans->new_bgs)) { |
| 1868 | block_group = list_first_entry(&trans->new_bgs, |
| 1869 | struct btrfs_block_group_cache, |
| 1870 | bg_list); |
| 1871 | if (ret) |
| 1872 | goto next; |
| 1873 | |
| 1874 | spin_lock(&block_group->lock); |
David Sterba | de0dc45 | 2019-10-23 18:48:18 +0200 | [diff] [blame] | 1875 | btrfs_set_stack_block_group_used(&item, block_group->used); |
| 1876 | btrfs_set_stack_block_group_chunk_objectid(&item, |
David Sterba | 3d97638 | 2019-10-23 18:48:15 +0200 | [diff] [blame] | 1877 | BTRFS_FIRST_CHUNK_TREE_OBJECTID); |
David Sterba | de0dc45 | 2019-10-23 18:48:18 +0200 | [diff] [blame] | 1878 | btrfs_set_stack_block_group_flags(&item, block_group->flags); |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame^] | 1879 | key.objectid = block_group->start; |
| 1880 | key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; |
| 1881 | key.offset = block_group->length; |
Josef Bacik | 4358d963 | 2019-06-20 15:37:57 -0400 | [diff] [blame] | 1882 | spin_unlock(&block_group->lock); |
| 1883 | |
| 1884 | ret = btrfs_insert_item(trans, extent_root, &key, &item, |
| 1885 | sizeof(item)); |
| 1886 | if (ret) |
| 1887 | btrfs_abort_transaction(trans, ret); |
| 1888 | ret = btrfs_finish_chunk_alloc(trans, key.objectid, key.offset); |
| 1889 | if (ret) |
| 1890 | btrfs_abort_transaction(trans, ret); |
| 1891 | add_block_group_free_space(trans, block_group); |
| 1892 | /* Already aborted the transaction if it failed. */ |
| 1893 | next: |
| 1894 | btrfs_delayed_refs_rsv_release(fs_info, 1); |
| 1895 | list_del_init(&block_group->bg_list); |
| 1896 | } |
| 1897 | btrfs_trans_release_chunk_metadata(trans); |
| 1898 | } |
| 1899 | |
| 1900 | int btrfs_make_block_group(struct btrfs_trans_handle *trans, u64 bytes_used, |
| 1901 | u64 type, u64 chunk_offset, u64 size) |
| 1902 | { |
| 1903 | struct btrfs_fs_info *fs_info = trans->fs_info; |
| 1904 | struct btrfs_block_group_cache *cache; |
| 1905 | int ret; |
| 1906 | |
| 1907 | btrfs_set_log_full_commit(trans); |
| 1908 | |
| 1909 | cache = btrfs_create_block_group_cache(fs_info, chunk_offset, size); |
| 1910 | if (!cache) |
| 1911 | return -ENOMEM; |
| 1912 | |
David Sterba | bf38be6 | 2019-10-23 18:48:11 +0200 | [diff] [blame] | 1913 | cache->used = bytes_used; |
Josef Bacik | 4358d963 | 2019-06-20 15:37:57 -0400 | [diff] [blame] | 1914 | cache->flags = type; |
| 1915 | cache->last_byte_to_unpin = (u64)-1; |
| 1916 | cache->cached = BTRFS_CACHE_FINISHED; |
| 1917 | cache->needs_free_space = 1; |
| 1918 | ret = exclude_super_stripes(cache); |
| 1919 | if (ret) { |
| 1920 | /* We may have excluded something, so call this just in case */ |
| 1921 | btrfs_free_excluded_extents(cache); |
| 1922 | btrfs_put_block_group(cache); |
| 1923 | return ret; |
| 1924 | } |
| 1925 | |
| 1926 | add_new_free_space(cache, chunk_offset, chunk_offset + size); |
| 1927 | |
| 1928 | btrfs_free_excluded_extents(cache); |
| 1929 | |
| 1930 | #ifdef CONFIG_BTRFS_DEBUG |
| 1931 | if (btrfs_should_fragment_free_space(cache)) { |
| 1932 | u64 new_bytes_used = size - bytes_used; |
| 1933 | |
| 1934 | bytes_used += new_bytes_used >> 1; |
Josef Bacik | e11c040 | 2019-06-20 15:38:07 -0400 | [diff] [blame] | 1935 | fragment_free_space(cache); |
Josef Bacik | 4358d963 | 2019-06-20 15:37:57 -0400 | [diff] [blame] | 1936 | } |
| 1937 | #endif |
| 1938 | /* |
| 1939 | * Ensure the corresponding space_info object is created and |
| 1940 | * assigned to our block group. We want our bg to be added to the rbtree |
| 1941 | * with its ->space_info set. |
| 1942 | */ |
| 1943 | cache->space_info = btrfs_find_space_info(fs_info, cache->flags); |
| 1944 | ASSERT(cache->space_info); |
| 1945 | |
| 1946 | ret = btrfs_add_block_group_cache(fs_info, cache); |
| 1947 | if (ret) { |
| 1948 | btrfs_remove_free_space_cache(cache); |
| 1949 | btrfs_put_block_group(cache); |
| 1950 | return ret; |
| 1951 | } |
| 1952 | |
| 1953 | /* |
| 1954 | * Now that our block group has its ->space_info set and is inserted in |
| 1955 | * the rbtree, update the space info's counters. |
| 1956 | */ |
| 1957 | trace_btrfs_add_block_group(fs_info, cache, 1); |
| 1958 | btrfs_update_space_info(fs_info, cache->flags, size, bytes_used, |
| 1959 | cache->bytes_super, &cache->space_info); |
| 1960 | btrfs_update_global_block_rsv(fs_info); |
| 1961 | |
| 1962 | link_block_group(cache); |
| 1963 | |
| 1964 | list_add_tail(&cache->bg_list, &trans->new_bgs); |
| 1965 | trans->delayed_ref_updates++; |
| 1966 | btrfs_update_delayed_refs_rsv(trans); |
| 1967 | |
| 1968 | set_avail_alloc_bits(fs_info, type); |
| 1969 | return 0; |
| 1970 | } |
Josef Bacik | 26ce209 | 2019-06-20 15:37:59 -0400 | [diff] [blame] | 1971 | |
| 1972 | static u64 update_block_group_flags(struct btrfs_fs_info *fs_info, u64 flags) |
| 1973 | { |
| 1974 | u64 num_devices; |
| 1975 | u64 stripped; |
| 1976 | |
| 1977 | /* |
| 1978 | * if restripe for this chunk_type is on pick target profile and |
| 1979 | * return, otherwise do the usual balance |
| 1980 | */ |
Josef Bacik | e11c040 | 2019-06-20 15:38:07 -0400 | [diff] [blame] | 1981 | stripped = get_restripe_target(fs_info, flags); |
Josef Bacik | 26ce209 | 2019-06-20 15:37:59 -0400 | [diff] [blame] | 1982 | if (stripped) |
| 1983 | return extended_to_chunk(stripped); |
| 1984 | |
| 1985 | num_devices = fs_info->fs_devices->rw_devices; |
| 1986 | |
| 1987 | stripped = BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID56_MASK | |
| 1988 | BTRFS_BLOCK_GROUP_RAID1_MASK | BTRFS_BLOCK_GROUP_RAID10; |
| 1989 | |
| 1990 | if (num_devices == 1) { |
| 1991 | stripped |= BTRFS_BLOCK_GROUP_DUP; |
| 1992 | stripped = flags & ~stripped; |
| 1993 | |
| 1994 | /* turn raid0 into single device chunks */ |
| 1995 | if (flags & BTRFS_BLOCK_GROUP_RAID0) |
| 1996 | return stripped; |
| 1997 | |
| 1998 | /* turn mirroring into duplication */ |
| 1999 | if (flags & (BTRFS_BLOCK_GROUP_RAID1_MASK | |
| 2000 | BTRFS_BLOCK_GROUP_RAID10)) |
| 2001 | return stripped | BTRFS_BLOCK_GROUP_DUP; |
| 2002 | } else { |
| 2003 | /* they already had raid on here, just return */ |
| 2004 | if (flags & stripped) |
| 2005 | return flags; |
| 2006 | |
| 2007 | stripped |= BTRFS_BLOCK_GROUP_DUP; |
| 2008 | stripped = flags & ~stripped; |
| 2009 | |
| 2010 | /* switch duplicated blocks with raid1 */ |
| 2011 | if (flags & BTRFS_BLOCK_GROUP_DUP) |
| 2012 | return stripped | BTRFS_BLOCK_GROUP_RAID1; |
| 2013 | |
| 2014 | /* this is drive concat, leave it alone */ |
| 2015 | } |
| 2016 | |
| 2017 | return flags; |
| 2018 | } |
| 2019 | |
| 2020 | int btrfs_inc_block_group_ro(struct btrfs_block_group_cache *cache) |
| 2021 | |
| 2022 | { |
| 2023 | struct btrfs_fs_info *fs_info = cache->fs_info; |
| 2024 | struct btrfs_trans_handle *trans; |
| 2025 | u64 alloc_flags; |
| 2026 | int ret; |
| 2027 | |
| 2028 | again: |
| 2029 | trans = btrfs_join_transaction(fs_info->extent_root); |
| 2030 | if (IS_ERR(trans)) |
| 2031 | return PTR_ERR(trans); |
| 2032 | |
| 2033 | /* |
| 2034 | * we're not allowed to set block groups readonly after the dirty |
| 2035 | * block groups cache has started writing. If it already started, |
| 2036 | * back off and let this transaction commit |
| 2037 | */ |
| 2038 | mutex_lock(&fs_info->ro_block_group_mutex); |
| 2039 | if (test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &trans->transaction->flags)) { |
| 2040 | u64 transid = trans->transid; |
| 2041 | |
| 2042 | mutex_unlock(&fs_info->ro_block_group_mutex); |
| 2043 | btrfs_end_transaction(trans); |
| 2044 | |
| 2045 | ret = btrfs_wait_for_commit(fs_info, transid); |
| 2046 | if (ret) |
| 2047 | return ret; |
| 2048 | goto again; |
| 2049 | } |
| 2050 | |
| 2051 | /* |
| 2052 | * if we are changing raid levels, try to allocate a corresponding |
| 2053 | * block group with the new raid level. |
| 2054 | */ |
| 2055 | alloc_flags = update_block_group_flags(fs_info, cache->flags); |
| 2056 | if (alloc_flags != cache->flags) { |
| 2057 | ret = btrfs_chunk_alloc(trans, alloc_flags, CHUNK_ALLOC_FORCE); |
| 2058 | /* |
| 2059 | * ENOSPC is allowed here, we may have enough space |
| 2060 | * already allocated at the new raid level to |
| 2061 | * carry on |
| 2062 | */ |
| 2063 | if (ret == -ENOSPC) |
| 2064 | ret = 0; |
| 2065 | if (ret < 0) |
| 2066 | goto out; |
| 2067 | } |
| 2068 | |
Josef Bacik | e11c040 | 2019-06-20 15:38:07 -0400 | [diff] [blame] | 2069 | ret = inc_block_group_ro(cache, 0); |
Josef Bacik | 26ce209 | 2019-06-20 15:37:59 -0400 | [diff] [blame] | 2070 | if (!ret) |
| 2071 | goto out; |
| 2072 | alloc_flags = btrfs_get_alloc_profile(fs_info, cache->space_info->flags); |
| 2073 | ret = btrfs_chunk_alloc(trans, alloc_flags, CHUNK_ALLOC_FORCE); |
| 2074 | if (ret < 0) |
| 2075 | goto out; |
Josef Bacik | e11c040 | 2019-06-20 15:38:07 -0400 | [diff] [blame] | 2076 | ret = inc_block_group_ro(cache, 0); |
Josef Bacik | 26ce209 | 2019-06-20 15:37:59 -0400 | [diff] [blame] | 2077 | out: |
| 2078 | if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) { |
| 2079 | alloc_flags = update_block_group_flags(fs_info, cache->flags); |
| 2080 | mutex_lock(&fs_info->chunk_mutex); |
| 2081 | check_system_chunk(trans, alloc_flags); |
| 2082 | mutex_unlock(&fs_info->chunk_mutex); |
| 2083 | } |
| 2084 | mutex_unlock(&fs_info->ro_block_group_mutex); |
| 2085 | |
| 2086 | btrfs_end_transaction(trans); |
| 2087 | return ret; |
| 2088 | } |
| 2089 | |
| 2090 | void btrfs_dec_block_group_ro(struct btrfs_block_group_cache *cache) |
| 2091 | { |
| 2092 | struct btrfs_space_info *sinfo = cache->space_info; |
| 2093 | u64 num_bytes; |
| 2094 | |
| 2095 | BUG_ON(!cache->ro); |
| 2096 | |
| 2097 | spin_lock(&sinfo->lock); |
| 2098 | spin_lock(&cache->lock); |
| 2099 | if (!--cache->ro) { |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame^] | 2100 | num_bytes = cache->length - cache->reserved - |
David Sterba | bf38be6 | 2019-10-23 18:48:11 +0200 | [diff] [blame] | 2101 | cache->pinned - cache->bytes_super - cache->used; |
Josef Bacik | 26ce209 | 2019-06-20 15:37:59 -0400 | [diff] [blame] | 2102 | sinfo->bytes_readonly -= num_bytes; |
| 2103 | list_del_init(&cache->ro_list); |
| 2104 | } |
| 2105 | spin_unlock(&cache->lock); |
| 2106 | spin_unlock(&sinfo->lock); |
| 2107 | } |
Josef Bacik | 77745c0 | 2019-06-20 15:38:00 -0400 | [diff] [blame] | 2108 | |
| 2109 | static int write_one_cache_group(struct btrfs_trans_handle *trans, |
| 2110 | struct btrfs_path *path, |
| 2111 | struct btrfs_block_group_cache *cache) |
| 2112 | { |
| 2113 | struct btrfs_fs_info *fs_info = trans->fs_info; |
| 2114 | int ret; |
| 2115 | struct btrfs_root *extent_root = fs_info->extent_root; |
| 2116 | unsigned long bi; |
| 2117 | struct extent_buffer *leaf; |
David Sterba | bf38be6 | 2019-10-23 18:48:11 +0200 | [diff] [blame] | 2118 | struct btrfs_block_group_item bgi; |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame^] | 2119 | struct btrfs_key key; |
Josef Bacik | 77745c0 | 2019-06-20 15:38:00 -0400 | [diff] [blame] | 2120 | |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame^] | 2121 | key.objectid = cache->start; |
| 2122 | key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; |
| 2123 | key.offset = cache->length; |
| 2124 | |
| 2125 | ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 1); |
Josef Bacik | 77745c0 | 2019-06-20 15:38:00 -0400 | [diff] [blame] | 2126 | if (ret) { |
| 2127 | if (ret > 0) |
| 2128 | ret = -ENOENT; |
| 2129 | goto fail; |
| 2130 | } |
| 2131 | |
| 2132 | leaf = path->nodes[0]; |
| 2133 | bi = btrfs_item_ptr_offset(leaf, path->slots[0]); |
David Sterba | de0dc45 | 2019-10-23 18:48:18 +0200 | [diff] [blame] | 2134 | btrfs_set_stack_block_group_used(&bgi, cache->used); |
| 2135 | btrfs_set_stack_block_group_chunk_objectid(&bgi, |
David Sterba | 3d97638 | 2019-10-23 18:48:15 +0200 | [diff] [blame] | 2136 | BTRFS_FIRST_CHUNK_TREE_OBJECTID); |
David Sterba | de0dc45 | 2019-10-23 18:48:18 +0200 | [diff] [blame] | 2137 | btrfs_set_stack_block_group_flags(&bgi, cache->flags); |
David Sterba | bf38be6 | 2019-10-23 18:48:11 +0200 | [diff] [blame] | 2138 | write_extent_buffer(leaf, &bgi, bi, sizeof(bgi)); |
Josef Bacik | 77745c0 | 2019-06-20 15:38:00 -0400 | [diff] [blame] | 2139 | btrfs_mark_buffer_dirty(leaf); |
| 2140 | fail: |
| 2141 | btrfs_release_path(path); |
| 2142 | return ret; |
| 2143 | |
| 2144 | } |
| 2145 | |
| 2146 | static int cache_save_setup(struct btrfs_block_group_cache *block_group, |
| 2147 | struct btrfs_trans_handle *trans, |
| 2148 | struct btrfs_path *path) |
| 2149 | { |
| 2150 | struct btrfs_fs_info *fs_info = block_group->fs_info; |
| 2151 | struct btrfs_root *root = fs_info->tree_root; |
| 2152 | struct inode *inode = NULL; |
| 2153 | struct extent_changeset *data_reserved = NULL; |
| 2154 | u64 alloc_hint = 0; |
| 2155 | int dcs = BTRFS_DC_ERROR; |
| 2156 | u64 num_pages = 0; |
| 2157 | int retries = 0; |
| 2158 | int ret = 0; |
| 2159 | |
| 2160 | /* |
| 2161 | * If this block group is smaller than 100 megs don't bother caching the |
| 2162 | * block group. |
| 2163 | */ |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame^] | 2164 | if (block_group->length < (100 * SZ_1M)) { |
Josef Bacik | 77745c0 | 2019-06-20 15:38:00 -0400 | [diff] [blame] | 2165 | spin_lock(&block_group->lock); |
| 2166 | block_group->disk_cache_state = BTRFS_DC_WRITTEN; |
| 2167 | spin_unlock(&block_group->lock); |
| 2168 | return 0; |
| 2169 | } |
| 2170 | |
| 2171 | if (trans->aborted) |
| 2172 | return 0; |
| 2173 | again: |
| 2174 | inode = lookup_free_space_inode(block_group, path); |
| 2175 | if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) { |
| 2176 | ret = PTR_ERR(inode); |
| 2177 | btrfs_release_path(path); |
| 2178 | goto out; |
| 2179 | } |
| 2180 | |
| 2181 | if (IS_ERR(inode)) { |
| 2182 | BUG_ON(retries); |
| 2183 | retries++; |
| 2184 | |
| 2185 | if (block_group->ro) |
| 2186 | goto out_free; |
| 2187 | |
| 2188 | ret = create_free_space_inode(trans, block_group, path); |
| 2189 | if (ret) |
| 2190 | goto out_free; |
| 2191 | goto again; |
| 2192 | } |
| 2193 | |
| 2194 | /* |
| 2195 | * We want to set the generation to 0, that way if anything goes wrong |
| 2196 | * from here on out we know not to trust this cache when we load up next |
| 2197 | * time. |
| 2198 | */ |
| 2199 | BTRFS_I(inode)->generation = 0; |
| 2200 | ret = btrfs_update_inode(trans, root, inode); |
| 2201 | if (ret) { |
| 2202 | /* |
| 2203 | * So theoretically we could recover from this, simply set the |
| 2204 | * super cache generation to 0 so we know to invalidate the |
| 2205 | * cache, but then we'd have to keep track of the block groups |
| 2206 | * that fail this way so we know we _have_ to reset this cache |
| 2207 | * before the next commit or risk reading stale cache. So to |
| 2208 | * limit our exposure to horrible edge cases lets just abort the |
| 2209 | * transaction, this only happens in really bad situations |
| 2210 | * anyway. |
| 2211 | */ |
| 2212 | btrfs_abort_transaction(trans, ret); |
| 2213 | goto out_put; |
| 2214 | } |
| 2215 | WARN_ON(ret); |
| 2216 | |
| 2217 | /* We've already setup this transaction, go ahead and exit */ |
| 2218 | if (block_group->cache_generation == trans->transid && |
| 2219 | i_size_read(inode)) { |
| 2220 | dcs = BTRFS_DC_SETUP; |
| 2221 | goto out_put; |
| 2222 | } |
| 2223 | |
| 2224 | if (i_size_read(inode) > 0) { |
| 2225 | ret = btrfs_check_trunc_cache_free_space(fs_info, |
| 2226 | &fs_info->global_block_rsv); |
| 2227 | if (ret) |
| 2228 | goto out_put; |
| 2229 | |
| 2230 | ret = btrfs_truncate_free_space_cache(trans, NULL, inode); |
| 2231 | if (ret) |
| 2232 | goto out_put; |
| 2233 | } |
| 2234 | |
| 2235 | spin_lock(&block_group->lock); |
| 2236 | if (block_group->cached != BTRFS_CACHE_FINISHED || |
| 2237 | !btrfs_test_opt(fs_info, SPACE_CACHE)) { |
| 2238 | /* |
| 2239 | * don't bother trying to write stuff out _if_ |
| 2240 | * a) we're not cached, |
| 2241 | * b) we're with nospace_cache mount option, |
| 2242 | * c) we're with v2 space_cache (FREE_SPACE_TREE). |
| 2243 | */ |
| 2244 | dcs = BTRFS_DC_WRITTEN; |
| 2245 | spin_unlock(&block_group->lock); |
| 2246 | goto out_put; |
| 2247 | } |
| 2248 | spin_unlock(&block_group->lock); |
| 2249 | |
| 2250 | /* |
| 2251 | * We hit an ENOSPC when setting up the cache in this transaction, just |
| 2252 | * skip doing the setup, we've already cleared the cache so we're safe. |
| 2253 | */ |
| 2254 | if (test_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags)) { |
| 2255 | ret = -ENOSPC; |
| 2256 | goto out_put; |
| 2257 | } |
| 2258 | |
| 2259 | /* |
| 2260 | * Try to preallocate enough space based on how big the block group is. |
| 2261 | * Keep in mind this has to include any pinned space which could end up |
| 2262 | * taking up quite a bit since it's not folded into the other space |
| 2263 | * cache. |
| 2264 | */ |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame^] | 2265 | num_pages = div_u64(block_group->length, SZ_256M); |
Josef Bacik | 77745c0 | 2019-06-20 15:38:00 -0400 | [diff] [blame] | 2266 | if (!num_pages) |
| 2267 | num_pages = 1; |
| 2268 | |
| 2269 | num_pages *= 16; |
| 2270 | num_pages *= PAGE_SIZE; |
| 2271 | |
| 2272 | ret = btrfs_check_data_free_space(inode, &data_reserved, 0, num_pages); |
| 2273 | if (ret) |
| 2274 | goto out_put; |
| 2275 | |
| 2276 | ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages, |
| 2277 | num_pages, num_pages, |
| 2278 | &alloc_hint); |
| 2279 | /* |
| 2280 | * Our cache requires contiguous chunks so that we don't modify a bunch |
| 2281 | * of metadata or split extents when writing the cache out, which means |
| 2282 | * we can enospc if we are heavily fragmented in addition to just normal |
| 2283 | * out of space conditions. So if we hit this just skip setting up any |
| 2284 | * other block groups for this transaction, maybe we'll unpin enough |
| 2285 | * space the next time around. |
| 2286 | */ |
| 2287 | if (!ret) |
| 2288 | dcs = BTRFS_DC_SETUP; |
| 2289 | else if (ret == -ENOSPC) |
| 2290 | set_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags); |
| 2291 | |
| 2292 | out_put: |
| 2293 | iput(inode); |
| 2294 | out_free: |
| 2295 | btrfs_release_path(path); |
| 2296 | out: |
| 2297 | spin_lock(&block_group->lock); |
| 2298 | if (!ret && dcs == BTRFS_DC_SETUP) |
| 2299 | block_group->cache_generation = trans->transid; |
| 2300 | block_group->disk_cache_state = dcs; |
| 2301 | spin_unlock(&block_group->lock); |
| 2302 | |
| 2303 | extent_changeset_free(data_reserved); |
| 2304 | return ret; |
| 2305 | } |
| 2306 | |
| 2307 | int btrfs_setup_space_cache(struct btrfs_trans_handle *trans) |
| 2308 | { |
| 2309 | struct btrfs_fs_info *fs_info = trans->fs_info; |
| 2310 | struct btrfs_block_group_cache *cache, *tmp; |
| 2311 | struct btrfs_transaction *cur_trans = trans->transaction; |
| 2312 | struct btrfs_path *path; |
| 2313 | |
| 2314 | if (list_empty(&cur_trans->dirty_bgs) || |
| 2315 | !btrfs_test_opt(fs_info, SPACE_CACHE)) |
| 2316 | return 0; |
| 2317 | |
| 2318 | path = btrfs_alloc_path(); |
| 2319 | if (!path) |
| 2320 | return -ENOMEM; |
| 2321 | |
| 2322 | /* Could add new block groups, use _safe just in case */ |
| 2323 | list_for_each_entry_safe(cache, tmp, &cur_trans->dirty_bgs, |
| 2324 | dirty_list) { |
| 2325 | if (cache->disk_cache_state == BTRFS_DC_CLEAR) |
| 2326 | cache_save_setup(cache, trans, path); |
| 2327 | } |
| 2328 | |
| 2329 | btrfs_free_path(path); |
| 2330 | return 0; |
| 2331 | } |
| 2332 | |
| 2333 | /* |
| 2334 | * Transaction commit does final block group cache writeback during a critical |
| 2335 | * section where nothing is allowed to change the FS. This is required in |
| 2336 | * order for the cache to actually match the block group, but can introduce a |
| 2337 | * lot of latency into the commit. |
| 2338 | * |
| 2339 | * So, btrfs_start_dirty_block_groups is here to kick off block group cache IO. |
| 2340 | * There's a chance we'll have to redo some of it if the block group changes |
| 2341 | * again during the commit, but it greatly reduces the commit latency by |
| 2342 | * getting rid of the easy block groups while we're still allowing others to |
| 2343 | * join the commit. |
| 2344 | */ |
| 2345 | int btrfs_start_dirty_block_groups(struct btrfs_trans_handle *trans) |
| 2346 | { |
| 2347 | struct btrfs_fs_info *fs_info = trans->fs_info; |
| 2348 | struct btrfs_block_group_cache *cache; |
| 2349 | struct btrfs_transaction *cur_trans = trans->transaction; |
| 2350 | int ret = 0; |
| 2351 | int should_put; |
| 2352 | struct btrfs_path *path = NULL; |
| 2353 | LIST_HEAD(dirty); |
| 2354 | struct list_head *io = &cur_trans->io_bgs; |
| 2355 | int num_started = 0; |
| 2356 | int loops = 0; |
| 2357 | |
| 2358 | spin_lock(&cur_trans->dirty_bgs_lock); |
| 2359 | if (list_empty(&cur_trans->dirty_bgs)) { |
| 2360 | spin_unlock(&cur_trans->dirty_bgs_lock); |
| 2361 | return 0; |
| 2362 | } |
| 2363 | list_splice_init(&cur_trans->dirty_bgs, &dirty); |
| 2364 | spin_unlock(&cur_trans->dirty_bgs_lock); |
| 2365 | |
| 2366 | again: |
| 2367 | /* Make sure all the block groups on our dirty list actually exist */ |
| 2368 | btrfs_create_pending_block_groups(trans); |
| 2369 | |
| 2370 | if (!path) { |
| 2371 | path = btrfs_alloc_path(); |
| 2372 | if (!path) |
| 2373 | return -ENOMEM; |
| 2374 | } |
| 2375 | |
| 2376 | /* |
| 2377 | * cache_write_mutex is here only to save us from balance or automatic |
| 2378 | * removal of empty block groups deleting this block group while we are |
| 2379 | * writing out the cache |
| 2380 | */ |
| 2381 | mutex_lock(&trans->transaction->cache_write_mutex); |
| 2382 | while (!list_empty(&dirty)) { |
| 2383 | bool drop_reserve = true; |
| 2384 | |
| 2385 | cache = list_first_entry(&dirty, |
| 2386 | struct btrfs_block_group_cache, |
| 2387 | dirty_list); |
| 2388 | /* |
| 2389 | * This can happen if something re-dirties a block group that |
| 2390 | * is already under IO. Just wait for it to finish and then do |
| 2391 | * it all again |
| 2392 | */ |
| 2393 | if (!list_empty(&cache->io_list)) { |
| 2394 | list_del_init(&cache->io_list); |
| 2395 | btrfs_wait_cache_io(trans, cache, path); |
| 2396 | btrfs_put_block_group(cache); |
| 2397 | } |
| 2398 | |
| 2399 | |
| 2400 | /* |
| 2401 | * btrfs_wait_cache_io uses the cache->dirty_list to decide if |
| 2402 | * it should update the cache_state. Don't delete until after |
| 2403 | * we wait. |
| 2404 | * |
| 2405 | * Since we're not running in the commit critical section |
| 2406 | * we need the dirty_bgs_lock to protect from update_block_group |
| 2407 | */ |
| 2408 | spin_lock(&cur_trans->dirty_bgs_lock); |
| 2409 | list_del_init(&cache->dirty_list); |
| 2410 | spin_unlock(&cur_trans->dirty_bgs_lock); |
| 2411 | |
| 2412 | should_put = 1; |
| 2413 | |
| 2414 | cache_save_setup(cache, trans, path); |
| 2415 | |
| 2416 | if (cache->disk_cache_state == BTRFS_DC_SETUP) { |
| 2417 | cache->io_ctl.inode = NULL; |
| 2418 | ret = btrfs_write_out_cache(trans, cache, path); |
| 2419 | if (ret == 0 && cache->io_ctl.inode) { |
| 2420 | num_started++; |
| 2421 | should_put = 0; |
| 2422 | |
| 2423 | /* |
| 2424 | * The cache_write_mutex is protecting the |
| 2425 | * io_list, also refer to the definition of |
| 2426 | * btrfs_transaction::io_bgs for more details |
| 2427 | */ |
| 2428 | list_add_tail(&cache->io_list, io); |
| 2429 | } else { |
| 2430 | /* |
| 2431 | * If we failed to write the cache, the |
| 2432 | * generation will be bad and life goes on |
| 2433 | */ |
| 2434 | ret = 0; |
| 2435 | } |
| 2436 | } |
| 2437 | if (!ret) { |
| 2438 | ret = write_one_cache_group(trans, path, cache); |
| 2439 | /* |
| 2440 | * Our block group might still be attached to the list |
| 2441 | * of new block groups in the transaction handle of some |
| 2442 | * other task (struct btrfs_trans_handle->new_bgs). This |
| 2443 | * means its block group item isn't yet in the extent |
| 2444 | * tree. If this happens ignore the error, as we will |
| 2445 | * try again later in the critical section of the |
| 2446 | * transaction commit. |
| 2447 | */ |
| 2448 | if (ret == -ENOENT) { |
| 2449 | ret = 0; |
| 2450 | spin_lock(&cur_trans->dirty_bgs_lock); |
| 2451 | if (list_empty(&cache->dirty_list)) { |
| 2452 | list_add_tail(&cache->dirty_list, |
| 2453 | &cur_trans->dirty_bgs); |
| 2454 | btrfs_get_block_group(cache); |
| 2455 | drop_reserve = false; |
| 2456 | } |
| 2457 | spin_unlock(&cur_trans->dirty_bgs_lock); |
| 2458 | } else if (ret) { |
| 2459 | btrfs_abort_transaction(trans, ret); |
| 2460 | } |
| 2461 | } |
| 2462 | |
| 2463 | /* If it's not on the io list, we need to put the block group */ |
| 2464 | if (should_put) |
| 2465 | btrfs_put_block_group(cache); |
| 2466 | if (drop_reserve) |
| 2467 | btrfs_delayed_refs_rsv_release(fs_info, 1); |
| 2468 | |
| 2469 | if (ret) |
| 2470 | break; |
| 2471 | |
| 2472 | /* |
| 2473 | * Avoid blocking other tasks for too long. It might even save |
| 2474 | * us from writing caches for block groups that are going to be |
| 2475 | * removed. |
| 2476 | */ |
| 2477 | mutex_unlock(&trans->transaction->cache_write_mutex); |
| 2478 | mutex_lock(&trans->transaction->cache_write_mutex); |
| 2479 | } |
| 2480 | mutex_unlock(&trans->transaction->cache_write_mutex); |
| 2481 | |
| 2482 | /* |
| 2483 | * Go through delayed refs for all the stuff we've just kicked off |
| 2484 | * and then loop back (just once) |
| 2485 | */ |
| 2486 | ret = btrfs_run_delayed_refs(trans, 0); |
| 2487 | if (!ret && loops == 0) { |
| 2488 | loops++; |
| 2489 | spin_lock(&cur_trans->dirty_bgs_lock); |
| 2490 | list_splice_init(&cur_trans->dirty_bgs, &dirty); |
| 2491 | /* |
| 2492 | * dirty_bgs_lock protects us from concurrent block group |
| 2493 | * deletes too (not just cache_write_mutex). |
| 2494 | */ |
| 2495 | if (!list_empty(&dirty)) { |
| 2496 | spin_unlock(&cur_trans->dirty_bgs_lock); |
| 2497 | goto again; |
| 2498 | } |
| 2499 | spin_unlock(&cur_trans->dirty_bgs_lock); |
| 2500 | } else if (ret < 0) { |
| 2501 | btrfs_cleanup_dirty_bgs(cur_trans, fs_info); |
| 2502 | } |
| 2503 | |
| 2504 | btrfs_free_path(path); |
| 2505 | return ret; |
| 2506 | } |
| 2507 | |
| 2508 | int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans) |
| 2509 | { |
| 2510 | struct btrfs_fs_info *fs_info = trans->fs_info; |
| 2511 | struct btrfs_block_group_cache *cache; |
| 2512 | struct btrfs_transaction *cur_trans = trans->transaction; |
| 2513 | int ret = 0; |
| 2514 | int should_put; |
| 2515 | struct btrfs_path *path; |
| 2516 | struct list_head *io = &cur_trans->io_bgs; |
| 2517 | int num_started = 0; |
| 2518 | |
| 2519 | path = btrfs_alloc_path(); |
| 2520 | if (!path) |
| 2521 | return -ENOMEM; |
| 2522 | |
| 2523 | /* |
| 2524 | * Even though we are in the critical section of the transaction commit, |
| 2525 | * we can still have concurrent tasks adding elements to this |
| 2526 | * transaction's list of dirty block groups. These tasks correspond to |
| 2527 | * endio free space workers started when writeback finishes for a |
| 2528 | * space cache, which run inode.c:btrfs_finish_ordered_io(), and can |
| 2529 | * allocate new block groups as a result of COWing nodes of the root |
| 2530 | * tree when updating the free space inode. The writeback for the space |
| 2531 | * caches is triggered by an earlier call to |
| 2532 | * btrfs_start_dirty_block_groups() and iterations of the following |
| 2533 | * loop. |
| 2534 | * Also we want to do the cache_save_setup first and then run the |
| 2535 | * delayed refs to make sure we have the best chance at doing this all |
| 2536 | * in one shot. |
| 2537 | */ |
| 2538 | spin_lock(&cur_trans->dirty_bgs_lock); |
| 2539 | while (!list_empty(&cur_trans->dirty_bgs)) { |
| 2540 | cache = list_first_entry(&cur_trans->dirty_bgs, |
| 2541 | struct btrfs_block_group_cache, |
| 2542 | dirty_list); |
| 2543 | |
| 2544 | /* |
| 2545 | * This can happen if cache_save_setup re-dirties a block group |
| 2546 | * that is already under IO. Just wait for it to finish and |
| 2547 | * then do it all again |
| 2548 | */ |
| 2549 | if (!list_empty(&cache->io_list)) { |
| 2550 | spin_unlock(&cur_trans->dirty_bgs_lock); |
| 2551 | list_del_init(&cache->io_list); |
| 2552 | btrfs_wait_cache_io(trans, cache, path); |
| 2553 | btrfs_put_block_group(cache); |
| 2554 | spin_lock(&cur_trans->dirty_bgs_lock); |
| 2555 | } |
| 2556 | |
| 2557 | /* |
| 2558 | * Don't remove from the dirty list until after we've waited on |
| 2559 | * any pending IO |
| 2560 | */ |
| 2561 | list_del_init(&cache->dirty_list); |
| 2562 | spin_unlock(&cur_trans->dirty_bgs_lock); |
| 2563 | should_put = 1; |
| 2564 | |
| 2565 | cache_save_setup(cache, trans, path); |
| 2566 | |
| 2567 | if (!ret) |
| 2568 | ret = btrfs_run_delayed_refs(trans, |
| 2569 | (unsigned long) -1); |
| 2570 | |
| 2571 | if (!ret && cache->disk_cache_state == BTRFS_DC_SETUP) { |
| 2572 | cache->io_ctl.inode = NULL; |
| 2573 | ret = btrfs_write_out_cache(trans, cache, path); |
| 2574 | if (ret == 0 && cache->io_ctl.inode) { |
| 2575 | num_started++; |
| 2576 | should_put = 0; |
| 2577 | list_add_tail(&cache->io_list, io); |
| 2578 | } else { |
| 2579 | /* |
| 2580 | * If we failed to write the cache, the |
| 2581 | * generation will be bad and life goes on |
| 2582 | */ |
| 2583 | ret = 0; |
| 2584 | } |
| 2585 | } |
| 2586 | if (!ret) { |
| 2587 | ret = write_one_cache_group(trans, path, cache); |
| 2588 | /* |
| 2589 | * One of the free space endio workers might have |
| 2590 | * created a new block group while updating a free space |
| 2591 | * cache's inode (at inode.c:btrfs_finish_ordered_io()) |
| 2592 | * and hasn't released its transaction handle yet, in |
| 2593 | * which case the new block group is still attached to |
| 2594 | * its transaction handle and its creation has not |
| 2595 | * finished yet (no block group item in the extent tree |
| 2596 | * yet, etc). If this is the case, wait for all free |
| 2597 | * space endio workers to finish and retry. This is a |
| 2598 | * a very rare case so no need for a more efficient and |
| 2599 | * complex approach. |
| 2600 | */ |
| 2601 | if (ret == -ENOENT) { |
| 2602 | wait_event(cur_trans->writer_wait, |
| 2603 | atomic_read(&cur_trans->num_writers) == 1); |
| 2604 | ret = write_one_cache_group(trans, path, cache); |
| 2605 | } |
| 2606 | if (ret) |
| 2607 | btrfs_abort_transaction(trans, ret); |
| 2608 | } |
| 2609 | |
| 2610 | /* If its not on the io list, we need to put the block group */ |
| 2611 | if (should_put) |
| 2612 | btrfs_put_block_group(cache); |
| 2613 | btrfs_delayed_refs_rsv_release(fs_info, 1); |
| 2614 | spin_lock(&cur_trans->dirty_bgs_lock); |
| 2615 | } |
| 2616 | spin_unlock(&cur_trans->dirty_bgs_lock); |
| 2617 | |
| 2618 | /* |
| 2619 | * Refer to the definition of io_bgs member for details why it's safe |
| 2620 | * to use it without any locking |
| 2621 | */ |
| 2622 | while (!list_empty(io)) { |
| 2623 | cache = list_first_entry(io, struct btrfs_block_group_cache, |
| 2624 | io_list); |
| 2625 | list_del_init(&cache->io_list); |
| 2626 | btrfs_wait_cache_io(trans, cache, path); |
| 2627 | btrfs_put_block_group(cache); |
| 2628 | } |
| 2629 | |
| 2630 | btrfs_free_path(path); |
| 2631 | return ret; |
| 2632 | } |
Josef Bacik | 606d1bf | 2019-06-20 15:38:02 -0400 | [diff] [blame] | 2633 | |
| 2634 | int btrfs_update_block_group(struct btrfs_trans_handle *trans, |
| 2635 | u64 bytenr, u64 num_bytes, int alloc) |
| 2636 | { |
| 2637 | struct btrfs_fs_info *info = trans->fs_info; |
| 2638 | struct btrfs_block_group_cache *cache = NULL; |
| 2639 | u64 total = num_bytes; |
| 2640 | u64 old_val; |
| 2641 | u64 byte_in_group; |
| 2642 | int factor; |
| 2643 | int ret = 0; |
| 2644 | |
| 2645 | /* Block accounting for super block */ |
| 2646 | spin_lock(&info->delalloc_root_lock); |
| 2647 | old_val = btrfs_super_bytes_used(info->super_copy); |
| 2648 | if (alloc) |
| 2649 | old_val += num_bytes; |
| 2650 | else |
| 2651 | old_val -= num_bytes; |
| 2652 | btrfs_set_super_bytes_used(info->super_copy, old_val); |
| 2653 | spin_unlock(&info->delalloc_root_lock); |
| 2654 | |
| 2655 | while (total) { |
| 2656 | cache = btrfs_lookup_block_group(info, bytenr); |
| 2657 | if (!cache) { |
| 2658 | ret = -ENOENT; |
| 2659 | break; |
| 2660 | } |
| 2661 | factor = btrfs_bg_type_to_factor(cache->flags); |
| 2662 | |
| 2663 | /* |
| 2664 | * If this block group has free space cache written out, we |
| 2665 | * need to make sure to load it if we are removing space. This |
| 2666 | * is because we need the unpinning stage to actually add the |
| 2667 | * space back to the block group, otherwise we will leak space. |
| 2668 | */ |
Josef Bacik | a60adce | 2019-09-24 16:50:44 -0400 | [diff] [blame] | 2669 | if (!alloc && !btrfs_block_group_cache_done(cache)) |
Josef Bacik | 606d1bf | 2019-06-20 15:38:02 -0400 | [diff] [blame] | 2670 | btrfs_cache_block_group(cache, 1); |
| 2671 | |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame^] | 2672 | byte_in_group = bytenr - cache->start; |
| 2673 | WARN_ON(byte_in_group > cache->length); |
Josef Bacik | 606d1bf | 2019-06-20 15:38:02 -0400 | [diff] [blame] | 2674 | |
| 2675 | spin_lock(&cache->space_info->lock); |
| 2676 | spin_lock(&cache->lock); |
| 2677 | |
| 2678 | if (btrfs_test_opt(info, SPACE_CACHE) && |
| 2679 | cache->disk_cache_state < BTRFS_DC_CLEAR) |
| 2680 | cache->disk_cache_state = BTRFS_DC_CLEAR; |
| 2681 | |
David Sterba | bf38be6 | 2019-10-23 18:48:11 +0200 | [diff] [blame] | 2682 | old_val = cache->used; |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame^] | 2683 | num_bytes = min(total, cache->length - byte_in_group); |
Josef Bacik | 606d1bf | 2019-06-20 15:38:02 -0400 | [diff] [blame] | 2684 | if (alloc) { |
| 2685 | old_val += num_bytes; |
David Sterba | bf38be6 | 2019-10-23 18:48:11 +0200 | [diff] [blame] | 2686 | cache->used = old_val; |
Josef Bacik | 606d1bf | 2019-06-20 15:38:02 -0400 | [diff] [blame] | 2687 | cache->reserved -= num_bytes; |
| 2688 | cache->space_info->bytes_reserved -= num_bytes; |
| 2689 | cache->space_info->bytes_used += num_bytes; |
| 2690 | cache->space_info->disk_used += num_bytes * factor; |
| 2691 | spin_unlock(&cache->lock); |
| 2692 | spin_unlock(&cache->space_info->lock); |
| 2693 | } else { |
| 2694 | old_val -= num_bytes; |
David Sterba | bf38be6 | 2019-10-23 18:48:11 +0200 | [diff] [blame] | 2695 | cache->used = old_val; |
Josef Bacik | 606d1bf | 2019-06-20 15:38:02 -0400 | [diff] [blame] | 2696 | cache->pinned += num_bytes; |
| 2697 | btrfs_space_info_update_bytes_pinned(info, |
| 2698 | cache->space_info, num_bytes); |
| 2699 | cache->space_info->bytes_used -= num_bytes; |
| 2700 | cache->space_info->disk_used -= num_bytes * factor; |
| 2701 | spin_unlock(&cache->lock); |
| 2702 | spin_unlock(&cache->space_info->lock); |
| 2703 | |
Josef Bacik | 606d1bf | 2019-06-20 15:38:02 -0400 | [diff] [blame] | 2704 | percpu_counter_add_batch( |
| 2705 | &cache->space_info->total_bytes_pinned, |
| 2706 | num_bytes, |
| 2707 | BTRFS_TOTAL_BYTES_PINNED_BATCH); |
| 2708 | set_extent_dirty(info->pinned_extents, |
| 2709 | bytenr, bytenr + num_bytes - 1, |
| 2710 | GFP_NOFS | __GFP_NOFAIL); |
| 2711 | } |
| 2712 | |
| 2713 | spin_lock(&trans->transaction->dirty_bgs_lock); |
| 2714 | if (list_empty(&cache->dirty_list)) { |
| 2715 | list_add_tail(&cache->dirty_list, |
| 2716 | &trans->transaction->dirty_bgs); |
| 2717 | trans->delayed_ref_updates++; |
| 2718 | btrfs_get_block_group(cache); |
| 2719 | } |
| 2720 | spin_unlock(&trans->transaction->dirty_bgs_lock); |
| 2721 | |
| 2722 | /* |
| 2723 | * No longer have used bytes in this block group, queue it for |
| 2724 | * deletion. We do this after adding the block group to the |
| 2725 | * dirty list to avoid races between cleaner kthread and space |
| 2726 | * cache writeout. |
| 2727 | */ |
| 2728 | if (!alloc && old_val == 0) |
| 2729 | btrfs_mark_bg_unused(cache); |
| 2730 | |
| 2731 | btrfs_put_block_group(cache); |
| 2732 | total -= num_bytes; |
| 2733 | bytenr += num_bytes; |
| 2734 | } |
| 2735 | |
| 2736 | /* Modified block groups are accounted for in the delayed_refs_rsv. */ |
| 2737 | btrfs_update_delayed_refs_rsv(trans); |
| 2738 | return ret; |
| 2739 | } |
| 2740 | |
| 2741 | /** |
| 2742 | * btrfs_add_reserved_bytes - update the block_group and space info counters |
| 2743 | * @cache: The cache we are manipulating |
| 2744 | * @ram_bytes: The number of bytes of file content, and will be same to |
| 2745 | * @num_bytes except for the compress path. |
| 2746 | * @num_bytes: The number of bytes in question |
| 2747 | * @delalloc: The blocks are allocated for the delalloc write |
| 2748 | * |
| 2749 | * This is called by the allocator when it reserves space. If this is a |
| 2750 | * reservation and the block group has become read only we cannot make the |
| 2751 | * reservation and return -EAGAIN, otherwise this function always succeeds. |
| 2752 | */ |
| 2753 | int btrfs_add_reserved_bytes(struct btrfs_block_group_cache *cache, |
| 2754 | u64 ram_bytes, u64 num_bytes, int delalloc) |
| 2755 | { |
| 2756 | struct btrfs_space_info *space_info = cache->space_info; |
| 2757 | int ret = 0; |
| 2758 | |
| 2759 | spin_lock(&space_info->lock); |
| 2760 | spin_lock(&cache->lock); |
| 2761 | if (cache->ro) { |
| 2762 | ret = -EAGAIN; |
| 2763 | } else { |
| 2764 | cache->reserved += num_bytes; |
| 2765 | space_info->bytes_reserved += num_bytes; |
Josef Bacik | a43c383 | 2019-08-22 15:10:56 -0400 | [diff] [blame] | 2766 | trace_btrfs_space_reservation(cache->fs_info, "space_info", |
| 2767 | space_info->flags, num_bytes, 1); |
Josef Bacik | 606d1bf | 2019-06-20 15:38:02 -0400 | [diff] [blame] | 2768 | btrfs_space_info_update_bytes_may_use(cache->fs_info, |
| 2769 | space_info, -ram_bytes); |
| 2770 | if (delalloc) |
| 2771 | cache->delalloc_bytes += num_bytes; |
| 2772 | } |
| 2773 | spin_unlock(&cache->lock); |
| 2774 | spin_unlock(&space_info->lock); |
| 2775 | return ret; |
| 2776 | } |
| 2777 | |
| 2778 | /** |
| 2779 | * btrfs_free_reserved_bytes - update the block_group and space info counters |
| 2780 | * @cache: The cache we are manipulating |
| 2781 | * @num_bytes: The number of bytes in question |
| 2782 | * @delalloc: The blocks are allocated for the delalloc write |
| 2783 | * |
| 2784 | * This is called by somebody who is freeing space that was never actually used |
| 2785 | * on disk. For example if you reserve some space for a new leaf in transaction |
| 2786 | * A and before transaction A commits you free that leaf, you call this with |
| 2787 | * reserve set to 0 in order to clear the reservation. |
| 2788 | */ |
| 2789 | void btrfs_free_reserved_bytes(struct btrfs_block_group_cache *cache, |
| 2790 | u64 num_bytes, int delalloc) |
| 2791 | { |
| 2792 | struct btrfs_space_info *space_info = cache->space_info; |
| 2793 | |
| 2794 | spin_lock(&space_info->lock); |
| 2795 | spin_lock(&cache->lock); |
| 2796 | if (cache->ro) |
| 2797 | space_info->bytes_readonly += num_bytes; |
| 2798 | cache->reserved -= num_bytes; |
| 2799 | space_info->bytes_reserved -= num_bytes; |
| 2800 | space_info->max_extent_size = 0; |
| 2801 | |
| 2802 | if (delalloc) |
| 2803 | cache->delalloc_bytes -= num_bytes; |
| 2804 | spin_unlock(&cache->lock); |
| 2805 | spin_unlock(&space_info->lock); |
| 2806 | } |
Josef Bacik | 07730d8 | 2019-06-20 15:38:04 -0400 | [diff] [blame] | 2807 | |
| 2808 | static void force_metadata_allocation(struct btrfs_fs_info *info) |
| 2809 | { |
| 2810 | struct list_head *head = &info->space_info; |
| 2811 | struct btrfs_space_info *found; |
| 2812 | |
| 2813 | rcu_read_lock(); |
| 2814 | list_for_each_entry_rcu(found, head, list) { |
| 2815 | if (found->flags & BTRFS_BLOCK_GROUP_METADATA) |
| 2816 | found->force_alloc = CHUNK_ALLOC_FORCE; |
| 2817 | } |
| 2818 | rcu_read_unlock(); |
| 2819 | } |
| 2820 | |
| 2821 | static int should_alloc_chunk(struct btrfs_fs_info *fs_info, |
| 2822 | struct btrfs_space_info *sinfo, int force) |
| 2823 | { |
| 2824 | u64 bytes_used = btrfs_space_info_used(sinfo, false); |
| 2825 | u64 thresh; |
| 2826 | |
| 2827 | if (force == CHUNK_ALLOC_FORCE) |
| 2828 | return 1; |
| 2829 | |
| 2830 | /* |
| 2831 | * in limited mode, we want to have some free space up to |
| 2832 | * about 1% of the FS size. |
| 2833 | */ |
| 2834 | if (force == CHUNK_ALLOC_LIMITED) { |
| 2835 | thresh = btrfs_super_total_bytes(fs_info->super_copy); |
| 2836 | thresh = max_t(u64, SZ_64M, div_factor_fine(thresh, 1)); |
| 2837 | |
| 2838 | if (sinfo->total_bytes - bytes_used < thresh) |
| 2839 | return 1; |
| 2840 | } |
| 2841 | |
| 2842 | if (bytes_used + SZ_2M < div_factor(sinfo->total_bytes, 8)) |
| 2843 | return 0; |
| 2844 | return 1; |
| 2845 | } |
| 2846 | |
| 2847 | int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans, u64 type) |
| 2848 | { |
| 2849 | u64 alloc_flags = btrfs_get_alloc_profile(trans->fs_info, type); |
| 2850 | |
| 2851 | return btrfs_chunk_alloc(trans, alloc_flags, CHUNK_ALLOC_FORCE); |
| 2852 | } |
| 2853 | |
| 2854 | /* |
| 2855 | * If force is CHUNK_ALLOC_FORCE: |
| 2856 | * - return 1 if it successfully allocates a chunk, |
| 2857 | * - return errors including -ENOSPC otherwise. |
| 2858 | * If force is NOT CHUNK_ALLOC_FORCE: |
| 2859 | * - return 0 if it doesn't need to allocate a new chunk, |
| 2860 | * - return 1 if it successfully allocates a chunk, |
| 2861 | * - return errors including -ENOSPC otherwise. |
| 2862 | */ |
| 2863 | int btrfs_chunk_alloc(struct btrfs_trans_handle *trans, u64 flags, |
| 2864 | enum btrfs_chunk_alloc_enum force) |
| 2865 | { |
| 2866 | struct btrfs_fs_info *fs_info = trans->fs_info; |
| 2867 | struct btrfs_space_info *space_info; |
| 2868 | bool wait_for_alloc = false; |
| 2869 | bool should_alloc = false; |
| 2870 | int ret = 0; |
| 2871 | |
| 2872 | /* Don't re-enter if we're already allocating a chunk */ |
| 2873 | if (trans->allocating_chunk) |
| 2874 | return -ENOSPC; |
| 2875 | |
| 2876 | space_info = btrfs_find_space_info(fs_info, flags); |
| 2877 | ASSERT(space_info); |
| 2878 | |
| 2879 | do { |
| 2880 | spin_lock(&space_info->lock); |
| 2881 | if (force < space_info->force_alloc) |
| 2882 | force = space_info->force_alloc; |
| 2883 | should_alloc = should_alloc_chunk(fs_info, space_info, force); |
| 2884 | if (space_info->full) { |
| 2885 | /* No more free physical space */ |
| 2886 | if (should_alloc) |
| 2887 | ret = -ENOSPC; |
| 2888 | else |
| 2889 | ret = 0; |
| 2890 | spin_unlock(&space_info->lock); |
| 2891 | return ret; |
| 2892 | } else if (!should_alloc) { |
| 2893 | spin_unlock(&space_info->lock); |
| 2894 | return 0; |
| 2895 | } else if (space_info->chunk_alloc) { |
| 2896 | /* |
| 2897 | * Someone is already allocating, so we need to block |
| 2898 | * until this someone is finished and then loop to |
| 2899 | * recheck if we should continue with our allocation |
| 2900 | * attempt. |
| 2901 | */ |
| 2902 | wait_for_alloc = true; |
| 2903 | spin_unlock(&space_info->lock); |
| 2904 | mutex_lock(&fs_info->chunk_mutex); |
| 2905 | mutex_unlock(&fs_info->chunk_mutex); |
| 2906 | } else { |
| 2907 | /* Proceed with allocation */ |
| 2908 | space_info->chunk_alloc = 1; |
| 2909 | wait_for_alloc = false; |
| 2910 | spin_unlock(&space_info->lock); |
| 2911 | } |
| 2912 | |
| 2913 | cond_resched(); |
| 2914 | } while (wait_for_alloc); |
| 2915 | |
| 2916 | mutex_lock(&fs_info->chunk_mutex); |
| 2917 | trans->allocating_chunk = true; |
| 2918 | |
| 2919 | /* |
| 2920 | * If we have mixed data/metadata chunks we want to make sure we keep |
| 2921 | * allocating mixed chunks instead of individual chunks. |
| 2922 | */ |
| 2923 | if (btrfs_mixed_space_info(space_info)) |
| 2924 | flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA); |
| 2925 | |
| 2926 | /* |
| 2927 | * if we're doing a data chunk, go ahead and make sure that |
| 2928 | * we keep a reasonable number of metadata chunks allocated in the |
| 2929 | * FS as well. |
| 2930 | */ |
| 2931 | if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) { |
| 2932 | fs_info->data_chunk_allocations++; |
| 2933 | if (!(fs_info->data_chunk_allocations % |
| 2934 | fs_info->metadata_ratio)) |
| 2935 | force_metadata_allocation(fs_info); |
| 2936 | } |
| 2937 | |
| 2938 | /* |
| 2939 | * Check if we have enough space in SYSTEM chunk because we may need |
| 2940 | * to update devices. |
| 2941 | */ |
| 2942 | check_system_chunk(trans, flags); |
| 2943 | |
| 2944 | ret = btrfs_alloc_chunk(trans, flags); |
| 2945 | trans->allocating_chunk = false; |
| 2946 | |
| 2947 | spin_lock(&space_info->lock); |
| 2948 | if (ret < 0) { |
| 2949 | if (ret == -ENOSPC) |
| 2950 | space_info->full = 1; |
| 2951 | else |
| 2952 | goto out; |
| 2953 | } else { |
| 2954 | ret = 1; |
| 2955 | space_info->max_extent_size = 0; |
| 2956 | } |
| 2957 | |
| 2958 | space_info->force_alloc = CHUNK_ALLOC_NO_FORCE; |
| 2959 | out: |
| 2960 | space_info->chunk_alloc = 0; |
| 2961 | spin_unlock(&space_info->lock); |
| 2962 | mutex_unlock(&fs_info->chunk_mutex); |
| 2963 | /* |
| 2964 | * When we allocate a new chunk we reserve space in the chunk block |
| 2965 | * reserve to make sure we can COW nodes/leafs in the chunk tree or |
| 2966 | * add new nodes/leafs to it if we end up needing to do it when |
| 2967 | * inserting the chunk item and updating device items as part of the |
| 2968 | * second phase of chunk allocation, performed by |
| 2969 | * btrfs_finish_chunk_alloc(). So make sure we don't accumulate a |
| 2970 | * large number of new block groups to create in our transaction |
| 2971 | * handle's new_bgs list to avoid exhausting the chunk block reserve |
| 2972 | * in extreme cases - like having a single transaction create many new |
| 2973 | * block groups when starting to write out the free space caches of all |
| 2974 | * the block groups that were made dirty during the lifetime of the |
| 2975 | * transaction. |
| 2976 | */ |
| 2977 | if (trans->chunk_bytes_reserved >= (u64)SZ_2M) |
| 2978 | btrfs_create_pending_block_groups(trans); |
| 2979 | |
| 2980 | return ret; |
| 2981 | } |
| 2982 | |
| 2983 | static u64 get_profile_num_devs(struct btrfs_fs_info *fs_info, u64 type) |
| 2984 | { |
| 2985 | u64 num_dev; |
| 2986 | |
| 2987 | num_dev = btrfs_raid_array[btrfs_bg_flags_to_raid_index(type)].devs_max; |
| 2988 | if (!num_dev) |
| 2989 | num_dev = fs_info->fs_devices->rw_devices; |
| 2990 | |
| 2991 | return num_dev; |
| 2992 | } |
| 2993 | |
| 2994 | /* |
Marcos Paulo de Souza | a9143bd | 2019-10-07 21:50:38 -0300 | [diff] [blame] | 2995 | * Reserve space in the system space for allocating or removing a chunk |
Josef Bacik | 07730d8 | 2019-06-20 15:38:04 -0400 | [diff] [blame] | 2996 | */ |
| 2997 | void check_system_chunk(struct btrfs_trans_handle *trans, u64 type) |
| 2998 | { |
| 2999 | struct btrfs_fs_info *fs_info = trans->fs_info; |
| 3000 | struct btrfs_space_info *info; |
| 3001 | u64 left; |
| 3002 | u64 thresh; |
| 3003 | int ret = 0; |
| 3004 | u64 num_devs; |
| 3005 | |
| 3006 | /* |
| 3007 | * Needed because we can end up allocating a system chunk and for an |
| 3008 | * atomic and race free space reservation in the chunk block reserve. |
| 3009 | */ |
| 3010 | lockdep_assert_held(&fs_info->chunk_mutex); |
| 3011 | |
| 3012 | info = btrfs_find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM); |
| 3013 | spin_lock(&info->lock); |
| 3014 | left = info->total_bytes - btrfs_space_info_used(info, true); |
| 3015 | spin_unlock(&info->lock); |
| 3016 | |
| 3017 | num_devs = get_profile_num_devs(fs_info, type); |
| 3018 | |
| 3019 | /* num_devs device items to update and 1 chunk item to add or remove */ |
Josef Bacik | 2bd36e7 | 2019-08-22 15:14:33 -0400 | [diff] [blame] | 3020 | thresh = btrfs_calc_metadata_size(fs_info, num_devs) + |
| 3021 | btrfs_calc_insert_metadata_size(fs_info, 1); |
Josef Bacik | 07730d8 | 2019-06-20 15:38:04 -0400 | [diff] [blame] | 3022 | |
| 3023 | if (left < thresh && btrfs_test_opt(fs_info, ENOSPC_DEBUG)) { |
| 3024 | btrfs_info(fs_info, "left=%llu, need=%llu, flags=%llu", |
| 3025 | left, thresh, type); |
| 3026 | btrfs_dump_space_info(fs_info, info, 0, 0); |
| 3027 | } |
| 3028 | |
| 3029 | if (left < thresh) { |
| 3030 | u64 flags = btrfs_system_alloc_profile(fs_info); |
| 3031 | |
| 3032 | /* |
| 3033 | * Ignore failure to create system chunk. We might end up not |
| 3034 | * needing it, as we might not need to COW all nodes/leafs from |
| 3035 | * the paths we visit in the chunk tree (they were already COWed |
| 3036 | * or created in the current transaction for example). |
| 3037 | */ |
| 3038 | ret = btrfs_alloc_chunk(trans, flags); |
| 3039 | } |
| 3040 | |
| 3041 | if (!ret) { |
| 3042 | ret = btrfs_block_rsv_add(fs_info->chunk_root, |
| 3043 | &fs_info->chunk_block_rsv, |
| 3044 | thresh, BTRFS_RESERVE_NO_FLUSH); |
| 3045 | if (!ret) |
| 3046 | trans->chunk_bytes_reserved += thresh; |
| 3047 | } |
| 3048 | } |
| 3049 | |
Josef Bacik | 3e43c27 | 2019-06-20 15:38:06 -0400 | [diff] [blame] | 3050 | void btrfs_put_block_group_cache(struct btrfs_fs_info *info) |
| 3051 | { |
| 3052 | struct btrfs_block_group_cache *block_group; |
| 3053 | u64 last = 0; |
| 3054 | |
| 3055 | while (1) { |
| 3056 | struct inode *inode; |
| 3057 | |
| 3058 | block_group = btrfs_lookup_first_block_group(info, last); |
| 3059 | while (block_group) { |
| 3060 | btrfs_wait_block_group_cache_done(block_group); |
| 3061 | spin_lock(&block_group->lock); |
| 3062 | if (block_group->iref) |
| 3063 | break; |
| 3064 | spin_unlock(&block_group->lock); |
| 3065 | block_group = btrfs_next_block_group(block_group); |
| 3066 | } |
| 3067 | if (!block_group) { |
| 3068 | if (last == 0) |
| 3069 | break; |
| 3070 | last = 0; |
| 3071 | continue; |
| 3072 | } |
| 3073 | |
| 3074 | inode = block_group->inode; |
| 3075 | block_group->iref = 0; |
| 3076 | block_group->inode = NULL; |
| 3077 | spin_unlock(&block_group->lock); |
| 3078 | ASSERT(block_group->io_ctl.inode == NULL); |
| 3079 | iput(inode); |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame^] | 3080 | last = block_group->start + block_group->length; |
Josef Bacik | 3e43c27 | 2019-06-20 15:38:06 -0400 | [diff] [blame] | 3081 | btrfs_put_block_group(block_group); |
| 3082 | } |
| 3083 | } |
| 3084 | |
| 3085 | /* |
| 3086 | * Must be called only after stopping all workers, since we could have block |
| 3087 | * group caching kthreads running, and therefore they could race with us if we |
| 3088 | * freed the block groups before stopping them. |
| 3089 | */ |
| 3090 | int btrfs_free_block_groups(struct btrfs_fs_info *info) |
| 3091 | { |
| 3092 | struct btrfs_block_group_cache *block_group; |
| 3093 | struct btrfs_space_info *space_info; |
| 3094 | struct btrfs_caching_control *caching_ctl; |
| 3095 | struct rb_node *n; |
| 3096 | |
| 3097 | down_write(&info->commit_root_sem); |
| 3098 | while (!list_empty(&info->caching_block_groups)) { |
| 3099 | caching_ctl = list_entry(info->caching_block_groups.next, |
| 3100 | struct btrfs_caching_control, list); |
| 3101 | list_del(&caching_ctl->list); |
| 3102 | btrfs_put_caching_control(caching_ctl); |
| 3103 | } |
| 3104 | up_write(&info->commit_root_sem); |
| 3105 | |
| 3106 | spin_lock(&info->unused_bgs_lock); |
| 3107 | while (!list_empty(&info->unused_bgs)) { |
| 3108 | block_group = list_first_entry(&info->unused_bgs, |
| 3109 | struct btrfs_block_group_cache, |
| 3110 | bg_list); |
| 3111 | list_del_init(&block_group->bg_list); |
| 3112 | btrfs_put_block_group(block_group); |
| 3113 | } |
| 3114 | spin_unlock(&info->unused_bgs_lock); |
| 3115 | |
| 3116 | spin_lock(&info->block_group_cache_lock); |
| 3117 | while ((n = rb_last(&info->block_group_cache_tree)) != NULL) { |
| 3118 | block_group = rb_entry(n, struct btrfs_block_group_cache, |
| 3119 | cache_node); |
| 3120 | rb_erase(&block_group->cache_node, |
| 3121 | &info->block_group_cache_tree); |
| 3122 | RB_CLEAR_NODE(&block_group->cache_node); |
| 3123 | spin_unlock(&info->block_group_cache_lock); |
| 3124 | |
| 3125 | down_write(&block_group->space_info->groups_sem); |
| 3126 | list_del(&block_group->list); |
| 3127 | up_write(&block_group->space_info->groups_sem); |
| 3128 | |
| 3129 | /* |
| 3130 | * We haven't cached this block group, which means we could |
| 3131 | * possibly have excluded extents on this block group. |
| 3132 | */ |
| 3133 | if (block_group->cached == BTRFS_CACHE_NO || |
| 3134 | block_group->cached == BTRFS_CACHE_ERROR) |
| 3135 | btrfs_free_excluded_extents(block_group); |
| 3136 | |
| 3137 | btrfs_remove_free_space_cache(block_group); |
| 3138 | ASSERT(block_group->cached != BTRFS_CACHE_STARTED); |
| 3139 | ASSERT(list_empty(&block_group->dirty_list)); |
| 3140 | ASSERT(list_empty(&block_group->io_list)); |
| 3141 | ASSERT(list_empty(&block_group->bg_list)); |
| 3142 | ASSERT(atomic_read(&block_group->count) == 1); |
| 3143 | btrfs_put_block_group(block_group); |
| 3144 | |
| 3145 | spin_lock(&info->block_group_cache_lock); |
| 3146 | } |
| 3147 | spin_unlock(&info->block_group_cache_lock); |
| 3148 | |
| 3149 | /* |
| 3150 | * Now that all the block groups are freed, go through and free all the |
| 3151 | * space_info structs. This is only called during the final stages of |
| 3152 | * unmount, and so we know nobody is using them. We call |
| 3153 | * synchronize_rcu() once before we start, just to be on the safe side. |
| 3154 | */ |
| 3155 | synchronize_rcu(); |
| 3156 | |
| 3157 | btrfs_release_global_block_rsv(info); |
| 3158 | |
| 3159 | while (!list_empty(&info->space_info)) { |
| 3160 | space_info = list_entry(info->space_info.next, |
| 3161 | struct btrfs_space_info, |
| 3162 | list); |
| 3163 | |
| 3164 | /* |
| 3165 | * Do not hide this behind enospc_debug, this is actually |
| 3166 | * important and indicates a real bug if this happens. |
| 3167 | */ |
| 3168 | if (WARN_ON(space_info->bytes_pinned > 0 || |
| 3169 | space_info->bytes_reserved > 0 || |
| 3170 | space_info->bytes_may_use > 0)) |
| 3171 | btrfs_dump_space_info(info, space_info, 0, 0); |
| 3172 | list_del(&space_info->list); |
| 3173 | btrfs_sysfs_remove_space_info(space_info); |
| 3174 | } |
| 3175 | return 0; |
| 3176 | } |