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