blob: 1db24e6d6d906d462e29fc8c50523ae71e685780 [file] [log] [blame]
Josef Bacik2e405ad2019-06-20 15:37:45 -04001// SPDX-License-Identifier: GPL-2.0
2
Johannes Thumshirn2ca0ec72021-10-14 18:39:02 +09003#include <linux/list_sort.h>
David Sterba784352f2019-08-21 18:54:28 +02004#include "misc.h"
Josef Bacik2e405ad2019-06-20 15:37:45 -04005#include "ctree.h"
6#include "block-group.h"
Josef Bacik3eeb3222019-06-20 15:37:47 -04007#include "space-info.h"
Josef Bacik9f212462019-08-06 16:43:19 +02008#include "disk-io.h"
9#include "free-space-cache.h"
10#include "free-space-tree.h"
Josef Bacike3e05202019-06-20 15:37:55 -040011#include "volumes.h"
12#include "transaction.h"
13#include "ref-verify.h"
Josef Bacik4358d9632019-06-20 15:37:57 -040014#include "sysfs.h"
15#include "tree-log.h"
Josef Bacik77745c02019-06-20 15:38:00 -040016#include "delalloc-space.h"
Dennis Zhoub0643e52019-12-13 16:22:14 -080017#include "discard.h"
Nikolay Borisov96a14332019-12-10 19:57:51 +020018#include "raid56.h"
Naohiro Aota08e11a32021-02-04 19:21:50 +090019#include "zoned.h"
Josef Bacik2e405ad2019-06-20 15:37:45 -040020
Josef Bacik878d7b62019-06-20 15:38:05 -040021/*
22 * Return target flags in extended format or 0 if restripe for this chunk_type
23 * is not in progress
24 *
25 * Should be called with balance_lock held
26 */
Josef Bacike11c0402019-06-20 15:38:07 -040027static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags)
Josef Bacik878d7b62019-06-20 15:38:05 -040028{
29 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
30 u64 target = 0;
31
32 if (!bctl)
33 return 0;
34
35 if (flags & BTRFS_BLOCK_GROUP_DATA &&
36 bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) {
37 target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target;
38 } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM &&
39 bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
40 target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target;
41 } else if (flags & BTRFS_BLOCK_GROUP_METADATA &&
42 bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) {
43 target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target;
44 }
45
46 return target;
47}
48
49/*
50 * @flags: available profiles in extended format (see ctree.h)
51 *
52 * Return reduced profile in chunk format. If profile changing is in progress
53 * (either running or paused) picks the target profile (if it's already
54 * available), otherwise falls back to plain reducing.
55 */
56static u64 btrfs_reduce_alloc_profile(struct btrfs_fs_info *fs_info, u64 flags)
57{
58 u64 num_devices = fs_info->fs_devices->rw_devices;
59 u64 target;
60 u64 raid_type;
61 u64 allowed = 0;
62
63 /*
64 * See if restripe for this chunk_type is in progress, if so try to
65 * reduce to the target profile
66 */
67 spin_lock(&fs_info->balance_lock);
Josef Bacike11c0402019-06-20 15:38:07 -040068 target = get_restripe_target(fs_info, flags);
Josef Bacik878d7b62019-06-20 15:38:05 -040069 if (target) {
Josef Bacik162e0a12020-07-21 10:48:46 -040070 spin_unlock(&fs_info->balance_lock);
71 return extended_to_chunk(target);
Josef Bacik878d7b62019-06-20 15:38:05 -040072 }
73 spin_unlock(&fs_info->balance_lock);
74
75 /* First, mask out the RAID levels which aren't possible */
76 for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) {
77 if (num_devices >= btrfs_raid_array[raid_type].devs_min)
78 allowed |= btrfs_raid_array[raid_type].bg_flag;
79 }
80 allowed &= flags;
81
82 if (allowed & BTRFS_BLOCK_GROUP_RAID6)
83 allowed = BTRFS_BLOCK_GROUP_RAID6;
84 else if (allowed & BTRFS_BLOCK_GROUP_RAID5)
85 allowed = BTRFS_BLOCK_GROUP_RAID5;
86 else if (allowed & BTRFS_BLOCK_GROUP_RAID10)
87 allowed = BTRFS_BLOCK_GROUP_RAID10;
88 else if (allowed & BTRFS_BLOCK_GROUP_RAID1)
89 allowed = BTRFS_BLOCK_GROUP_RAID1;
90 else if (allowed & BTRFS_BLOCK_GROUP_RAID0)
91 allowed = BTRFS_BLOCK_GROUP_RAID0;
92
93 flags &= ~BTRFS_BLOCK_GROUP_PROFILE_MASK;
94
95 return extended_to_chunk(flags | allowed);
96}
97
Johannes Thumshirnef0a82d2020-01-02 17:14:57 +010098u64 btrfs_get_alloc_profile(struct btrfs_fs_info *fs_info, u64 orig_flags)
Josef Bacik878d7b62019-06-20 15:38:05 -040099{
100 unsigned seq;
101 u64 flags;
102
103 do {
104 flags = orig_flags;
105 seq = read_seqbegin(&fs_info->profiles_lock);
106
107 if (flags & BTRFS_BLOCK_GROUP_DATA)
108 flags |= fs_info->avail_data_alloc_bits;
109 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
110 flags |= fs_info->avail_system_alloc_bits;
111 else if (flags & BTRFS_BLOCK_GROUP_METADATA)
112 flags |= fs_info->avail_metadata_alloc_bits;
113 } while (read_seqretry(&fs_info->profiles_lock, seq));
114
115 return btrfs_reduce_alloc_profile(fs_info, flags);
116}
117
David Sterba32da53862019-10-29 19:20:18 +0100118void btrfs_get_block_group(struct btrfs_block_group *cache)
Josef Bacik3cad1282019-06-20 15:37:46 -0400119{
Josef Bacik48aaeeb2020-07-06 09:14:11 -0400120 refcount_inc(&cache->refs);
Josef Bacik3cad1282019-06-20 15:37:46 -0400121}
122
David Sterba32da53862019-10-29 19:20:18 +0100123void btrfs_put_block_group(struct btrfs_block_group *cache)
Josef Bacik3cad1282019-06-20 15:37:46 -0400124{
Josef Bacik48aaeeb2020-07-06 09:14:11 -0400125 if (refcount_dec_and_test(&cache->refs)) {
Josef Bacik3cad1282019-06-20 15:37:46 -0400126 WARN_ON(cache->pinned > 0);
127 WARN_ON(cache->reserved > 0);
128
129 /*
Dennis Zhoub0643e52019-12-13 16:22:14 -0800130 * A block_group shouldn't be on the discard_list anymore.
131 * Remove the block_group from the discard_list to prevent us
132 * from causing a panic due to NULL pointer dereference.
133 */
134 if (WARN_ON(!list_empty(&cache->discard_list)))
135 btrfs_discard_cancel_work(&cache->fs_info->discard_ctl,
136 cache);
137
138 /*
Josef Bacik3cad1282019-06-20 15:37:46 -0400139 * If not empty, someone is still holding mutex of
140 * full_stripe_lock, which can only be released by caller.
141 * And it will definitely cause use-after-free when caller
142 * tries to release full stripe lock.
143 *
144 * No better way to resolve, but only to warn.
145 */
146 WARN_ON(!RB_EMPTY_ROOT(&cache->full_stripe_locks_root.root));
147 kfree(cache->free_space_ctl);
Naohiro Aotadafc340d2021-08-19 21:19:16 +0900148 kfree(cache->physical_map);
Josef Bacik3cad1282019-06-20 15:37:46 -0400149 kfree(cache);
150 }
151}
152
Josef Bacik2e405ad2019-06-20 15:37:45 -0400153/*
Josef Bacik4358d9632019-06-20 15:37:57 -0400154 * This adds the block group to the fs_info rb tree for the block group cache
155 */
156static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
David Sterba32da53862019-10-29 19:20:18 +0100157 struct btrfs_block_group *block_group)
Josef Bacik4358d9632019-06-20 15:37:57 -0400158{
159 struct rb_node **p;
160 struct rb_node *parent = NULL;
David Sterba32da53862019-10-29 19:20:18 +0100161 struct btrfs_block_group *cache;
Josef Bacik4358d9632019-06-20 15:37:57 -0400162
Qu Wenruo9afc6642020-05-05 07:58:20 +0800163 ASSERT(block_group->length != 0);
164
Josef Bacik4358d9632019-06-20 15:37:57 -0400165 spin_lock(&info->block_group_cache_lock);
166 p = &info->block_group_cache_tree.rb_node;
167
168 while (*p) {
169 parent = *p;
David Sterba32da53862019-10-29 19:20:18 +0100170 cache = rb_entry(parent, struct btrfs_block_group, cache_node);
David Sterbab3470b52019-10-23 18:48:22 +0200171 if (block_group->start < cache->start) {
Josef Bacik4358d9632019-06-20 15:37:57 -0400172 p = &(*p)->rb_left;
David Sterbab3470b52019-10-23 18:48:22 +0200173 } else if (block_group->start > cache->start) {
Josef Bacik4358d9632019-06-20 15:37:57 -0400174 p = &(*p)->rb_right;
175 } else {
176 spin_unlock(&info->block_group_cache_lock);
177 return -EEXIST;
178 }
179 }
180
181 rb_link_node(&block_group->cache_node, parent, p);
182 rb_insert_color(&block_group->cache_node,
183 &info->block_group_cache_tree);
184
David Sterbab3470b52019-10-23 18:48:22 +0200185 if (info->first_logical_byte > block_group->start)
186 info->first_logical_byte = block_group->start;
Josef Bacik4358d9632019-06-20 15:37:57 -0400187
188 spin_unlock(&info->block_group_cache_lock);
189
190 return 0;
191}
192
193/*
Josef Bacik2e405ad2019-06-20 15:37:45 -0400194 * This will return the block group at or after bytenr if contains is 0, else
195 * it will return the block group that contains the bytenr
196 */
David Sterba32da53862019-10-29 19:20:18 +0100197static struct btrfs_block_group *block_group_cache_tree_search(
Josef Bacik2e405ad2019-06-20 15:37:45 -0400198 struct btrfs_fs_info *info, u64 bytenr, int contains)
199{
David Sterba32da53862019-10-29 19:20:18 +0100200 struct btrfs_block_group *cache, *ret = NULL;
Josef Bacik2e405ad2019-06-20 15:37:45 -0400201 struct rb_node *n;
202 u64 end, start;
203
204 spin_lock(&info->block_group_cache_lock);
205 n = info->block_group_cache_tree.rb_node;
206
207 while (n) {
David Sterba32da53862019-10-29 19:20:18 +0100208 cache = rb_entry(n, struct btrfs_block_group, cache_node);
David Sterbab3470b52019-10-23 18:48:22 +0200209 end = cache->start + cache->length - 1;
210 start = cache->start;
Josef Bacik2e405ad2019-06-20 15:37:45 -0400211
212 if (bytenr < start) {
David Sterbab3470b52019-10-23 18:48:22 +0200213 if (!contains && (!ret || start < ret->start))
Josef Bacik2e405ad2019-06-20 15:37:45 -0400214 ret = cache;
215 n = n->rb_left;
216 } else if (bytenr > start) {
217 if (contains && bytenr <= end) {
218 ret = cache;
219 break;
220 }
221 n = n->rb_right;
222 } else {
223 ret = cache;
224 break;
225 }
226 }
227 if (ret) {
228 btrfs_get_block_group(ret);
David Sterbab3470b52019-10-23 18:48:22 +0200229 if (bytenr == 0 && info->first_logical_byte > ret->start)
230 info->first_logical_byte = ret->start;
Josef Bacik2e405ad2019-06-20 15:37:45 -0400231 }
232 spin_unlock(&info->block_group_cache_lock);
233
234 return ret;
235}
236
237/*
238 * Return the block group that starts at or after bytenr
239 */
David Sterba32da53862019-10-29 19:20:18 +0100240struct btrfs_block_group *btrfs_lookup_first_block_group(
Josef Bacik2e405ad2019-06-20 15:37:45 -0400241 struct btrfs_fs_info *info, u64 bytenr)
242{
243 return block_group_cache_tree_search(info, bytenr, 0);
244}
245
246/*
247 * Return the block group that contains the given bytenr
248 */
David Sterba32da53862019-10-29 19:20:18 +0100249struct btrfs_block_group *btrfs_lookup_block_group(
Josef Bacik2e405ad2019-06-20 15:37:45 -0400250 struct btrfs_fs_info *info, u64 bytenr)
251{
252 return block_group_cache_tree_search(info, bytenr, 1);
253}
254
David Sterba32da53862019-10-29 19:20:18 +0100255struct btrfs_block_group *btrfs_next_block_group(
256 struct btrfs_block_group *cache)
Josef Bacik2e405ad2019-06-20 15:37:45 -0400257{
258 struct btrfs_fs_info *fs_info = cache->fs_info;
259 struct rb_node *node;
260
261 spin_lock(&fs_info->block_group_cache_lock);
262
263 /* If our block group was removed, we need a full search. */
264 if (RB_EMPTY_NODE(&cache->cache_node)) {
David Sterbab3470b52019-10-23 18:48:22 +0200265 const u64 next_bytenr = cache->start + cache->length;
Josef Bacik2e405ad2019-06-20 15:37:45 -0400266
267 spin_unlock(&fs_info->block_group_cache_lock);
268 btrfs_put_block_group(cache);
269 cache = btrfs_lookup_first_block_group(fs_info, next_bytenr); return cache;
270 }
271 node = rb_next(&cache->cache_node);
272 btrfs_put_block_group(cache);
273 if (node) {
David Sterba32da53862019-10-29 19:20:18 +0100274 cache = rb_entry(node, struct btrfs_block_group, cache_node);
Josef Bacik2e405ad2019-06-20 15:37:45 -0400275 btrfs_get_block_group(cache);
276 } else
277 cache = NULL;
278 spin_unlock(&fs_info->block_group_cache_lock);
279 return cache;
280}
Josef Bacik3eeb3222019-06-20 15:37:47 -0400281
282bool btrfs_inc_nocow_writers(struct btrfs_fs_info *fs_info, u64 bytenr)
283{
David Sterba32da53862019-10-29 19:20:18 +0100284 struct btrfs_block_group *bg;
Josef Bacik3eeb3222019-06-20 15:37:47 -0400285 bool ret = true;
286
287 bg = btrfs_lookup_block_group(fs_info, bytenr);
288 if (!bg)
289 return false;
290
291 spin_lock(&bg->lock);
292 if (bg->ro)
293 ret = false;
294 else
295 atomic_inc(&bg->nocow_writers);
296 spin_unlock(&bg->lock);
297
298 /* No put on block group, done by btrfs_dec_nocow_writers */
299 if (!ret)
300 btrfs_put_block_group(bg);
301
302 return ret;
303}
304
305void btrfs_dec_nocow_writers(struct btrfs_fs_info *fs_info, u64 bytenr)
306{
David Sterba32da53862019-10-29 19:20:18 +0100307 struct btrfs_block_group *bg;
Josef Bacik3eeb3222019-06-20 15:37:47 -0400308
309 bg = btrfs_lookup_block_group(fs_info, bytenr);
310 ASSERT(bg);
311 if (atomic_dec_and_test(&bg->nocow_writers))
312 wake_up_var(&bg->nocow_writers);
313 /*
314 * Once for our lookup and once for the lookup done by a previous call
315 * to btrfs_inc_nocow_writers()
316 */
317 btrfs_put_block_group(bg);
318 btrfs_put_block_group(bg);
319}
320
David Sterba32da53862019-10-29 19:20:18 +0100321void btrfs_wait_nocow_writers(struct btrfs_block_group *bg)
Josef Bacik3eeb3222019-06-20 15:37:47 -0400322{
323 wait_var_event(&bg->nocow_writers, !atomic_read(&bg->nocow_writers));
324}
325
326void btrfs_dec_block_group_reservations(struct btrfs_fs_info *fs_info,
327 const u64 start)
328{
David Sterba32da53862019-10-29 19:20:18 +0100329 struct btrfs_block_group *bg;
Josef Bacik3eeb3222019-06-20 15:37:47 -0400330
331 bg = btrfs_lookup_block_group(fs_info, start);
332 ASSERT(bg);
333 if (atomic_dec_and_test(&bg->reservations))
334 wake_up_var(&bg->reservations);
335 btrfs_put_block_group(bg);
336}
337
David Sterba32da53862019-10-29 19:20:18 +0100338void btrfs_wait_block_group_reservations(struct btrfs_block_group *bg)
Josef Bacik3eeb3222019-06-20 15:37:47 -0400339{
340 struct btrfs_space_info *space_info = bg->space_info;
341
342 ASSERT(bg->ro);
343
344 if (!(bg->flags & BTRFS_BLOCK_GROUP_DATA))
345 return;
346
347 /*
348 * Our block group is read only but before we set it to read only,
349 * some task might have had allocated an extent from it already, but it
350 * has not yet created a respective ordered extent (and added it to a
351 * root's list of ordered extents).
352 * Therefore wait for any task currently allocating extents, since the
353 * block group's reservations counter is incremented while a read lock
354 * on the groups' semaphore is held and decremented after releasing
355 * the read access on that semaphore and creating the ordered extent.
356 */
357 down_write(&space_info->groups_sem);
358 up_write(&space_info->groups_sem);
359
360 wait_var_event(&bg->reservations, !atomic_read(&bg->reservations));
361}
Josef Bacik9f212462019-08-06 16:43:19 +0200362
363struct btrfs_caching_control *btrfs_get_caching_control(
David Sterba32da53862019-10-29 19:20:18 +0100364 struct btrfs_block_group *cache)
Josef Bacik9f212462019-08-06 16:43:19 +0200365{
366 struct btrfs_caching_control *ctl;
367
368 spin_lock(&cache->lock);
369 if (!cache->caching_ctl) {
370 spin_unlock(&cache->lock);
371 return NULL;
372 }
373
374 ctl = cache->caching_ctl;
375 refcount_inc(&ctl->count);
376 spin_unlock(&cache->lock);
377 return ctl;
378}
379
380void btrfs_put_caching_control(struct btrfs_caching_control *ctl)
381{
382 if (refcount_dec_and_test(&ctl->count))
383 kfree(ctl);
384}
385
386/*
387 * When we wait for progress in the block group caching, its because our
388 * allocation attempt failed at least once. So, we must sleep and let some
389 * progress happen before we try again.
390 *
391 * This function will sleep at least once waiting for new free space to show
392 * up, and then it will check the block group free space numbers for our min
393 * num_bytes. Another option is to have it go ahead and look in the rbtree for
394 * a free extent of a given size, but this is a good start.
395 *
396 * Callers of this must check if cache->cached == BTRFS_CACHE_ERROR before using
397 * any of the information in this block group.
398 */
David Sterba32da53862019-10-29 19:20:18 +0100399void btrfs_wait_block_group_cache_progress(struct btrfs_block_group *cache,
Josef Bacik9f212462019-08-06 16:43:19 +0200400 u64 num_bytes)
401{
402 struct btrfs_caching_control *caching_ctl;
403
404 caching_ctl = btrfs_get_caching_control(cache);
405 if (!caching_ctl)
406 return;
407
David Sterba32da53862019-10-29 19:20:18 +0100408 wait_event(caching_ctl->wait, btrfs_block_group_done(cache) ||
Josef Bacik9f212462019-08-06 16:43:19 +0200409 (cache->free_space_ctl->free_space >= num_bytes));
410
411 btrfs_put_caching_control(caching_ctl);
412}
413
David Sterba32da53862019-10-29 19:20:18 +0100414int btrfs_wait_block_group_cache_done(struct btrfs_block_group *cache)
Josef Bacik9f212462019-08-06 16:43:19 +0200415{
416 struct btrfs_caching_control *caching_ctl;
417 int ret = 0;
418
419 caching_ctl = btrfs_get_caching_control(cache);
420 if (!caching_ctl)
421 return (cache->cached == BTRFS_CACHE_ERROR) ? -EIO : 0;
422
David Sterba32da53862019-10-29 19:20:18 +0100423 wait_event(caching_ctl->wait, btrfs_block_group_done(cache));
Josef Bacik9f212462019-08-06 16:43:19 +0200424 if (cache->cached == BTRFS_CACHE_ERROR)
425 ret = -EIO;
426 btrfs_put_caching_control(caching_ctl);
427 return ret;
428}
429
Josef Bacike7478532020-10-23 09:58:10 -0400430static bool space_cache_v1_done(struct btrfs_block_group *cache)
431{
432 bool ret;
433
434 spin_lock(&cache->lock);
435 ret = cache->cached != BTRFS_CACHE_FAST;
436 spin_unlock(&cache->lock);
437
438 return ret;
439}
440
441void btrfs_wait_space_cache_v1_finished(struct btrfs_block_group *cache,
442 struct btrfs_caching_control *caching_ctl)
443{
444 wait_event(caching_ctl->wait, space_cache_v1_done(cache));
445}
446
Josef Bacik9f212462019-08-06 16:43:19 +0200447#ifdef CONFIG_BTRFS_DEBUG
David Sterba32da53862019-10-29 19:20:18 +0100448static void fragment_free_space(struct btrfs_block_group *block_group)
Josef Bacik9f212462019-08-06 16:43:19 +0200449{
450 struct btrfs_fs_info *fs_info = block_group->fs_info;
David Sterbab3470b52019-10-23 18:48:22 +0200451 u64 start = block_group->start;
452 u64 len = block_group->length;
Josef Bacik9f212462019-08-06 16:43:19 +0200453 u64 chunk = block_group->flags & BTRFS_BLOCK_GROUP_METADATA ?
454 fs_info->nodesize : fs_info->sectorsize;
455 u64 step = chunk << 1;
456
457 while (len > chunk) {
458 btrfs_remove_free_space(block_group, start, chunk);
459 start += step;
460 if (len < step)
461 len = 0;
462 else
463 len -= step;
464 }
465}
466#endif
467
468/*
469 * This is only called by btrfs_cache_block_group, since we could have freed
470 * extents we need to check the pinned_extents for any extents that can't be
471 * used yet since their free space will be released as soon as the transaction
472 * commits.
473 */
David Sterba32da53862019-10-29 19:20:18 +0100474u64 add_new_free_space(struct btrfs_block_group *block_group, u64 start, u64 end)
Josef Bacik9f212462019-08-06 16:43:19 +0200475{
476 struct btrfs_fs_info *info = block_group->fs_info;
477 u64 extent_start, extent_end, size, total_added = 0;
478 int ret;
479
480 while (start < end) {
Nikolay Borisovfe119a62020-01-20 16:09:18 +0200481 ret = find_first_extent_bit(&info->excluded_extents, start,
Josef Bacik9f212462019-08-06 16:43:19 +0200482 &extent_start, &extent_end,
483 EXTENT_DIRTY | EXTENT_UPTODATE,
484 NULL);
485 if (ret)
486 break;
487
488 if (extent_start <= start) {
489 start = extent_end + 1;
490 } else if (extent_start > start && extent_start < end) {
491 size = extent_start - start;
492 total_added += size;
Dennis Zhoub0643e52019-12-13 16:22:14 -0800493 ret = btrfs_add_free_space_async_trimmed(block_group,
494 start, size);
Josef Bacik9f212462019-08-06 16:43:19 +0200495 BUG_ON(ret); /* -ENOMEM or logic error */
496 start = extent_end + 1;
497 } else {
498 break;
499 }
500 }
501
502 if (start < end) {
503 size = end - start;
504 total_added += size;
Dennis Zhoub0643e52019-12-13 16:22:14 -0800505 ret = btrfs_add_free_space_async_trimmed(block_group, start,
506 size);
Josef Bacik9f212462019-08-06 16:43:19 +0200507 BUG_ON(ret); /* -ENOMEM or logic error */
508 }
509
510 return total_added;
511}
512
513static int load_extent_tree_free(struct btrfs_caching_control *caching_ctl)
514{
David Sterba32da53862019-10-29 19:20:18 +0100515 struct btrfs_block_group *block_group = caching_ctl->block_group;
Josef Bacik9f212462019-08-06 16:43:19 +0200516 struct btrfs_fs_info *fs_info = block_group->fs_info;
Josef Bacik29cbcf42021-11-05 16:45:45 -0400517 struct btrfs_root *extent_root;
Josef Bacik9f212462019-08-06 16:43:19 +0200518 struct btrfs_path *path;
519 struct extent_buffer *leaf;
520 struct btrfs_key key;
521 u64 total_found = 0;
522 u64 last = 0;
523 u32 nritems;
524 int ret;
525 bool wakeup = true;
526
527 path = btrfs_alloc_path();
528 if (!path)
529 return -ENOMEM;
530
David Sterbab3470b52019-10-23 18:48:22 +0200531 last = max_t(u64, block_group->start, BTRFS_SUPER_INFO_OFFSET);
Josef Bacik29cbcf42021-11-05 16:45:45 -0400532 extent_root = btrfs_extent_root(fs_info, last);
Josef Bacik9f212462019-08-06 16:43:19 +0200533
534#ifdef CONFIG_BTRFS_DEBUG
535 /*
536 * If we're fragmenting we don't want to make anybody think we can
537 * allocate from this block group until we've had a chance to fragment
538 * the free space.
539 */
540 if (btrfs_should_fragment_free_space(block_group))
541 wakeup = false;
542#endif
543 /*
544 * We don't want to deadlock with somebody trying to allocate a new
545 * extent for the extent root while also trying to search the extent
546 * root to add free space. So we skip locking and search the commit
547 * root, since its read-only
548 */
549 path->skip_locking = 1;
550 path->search_commit_root = 1;
551 path->reada = READA_FORWARD;
552
553 key.objectid = last;
554 key.offset = 0;
555 key.type = BTRFS_EXTENT_ITEM_KEY;
556
557next:
558 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
559 if (ret < 0)
560 goto out;
561
562 leaf = path->nodes[0];
563 nritems = btrfs_header_nritems(leaf);
564
565 while (1) {
566 if (btrfs_fs_closing(fs_info) > 1) {
567 last = (u64)-1;
568 break;
569 }
570
571 if (path->slots[0] < nritems) {
572 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
573 } else {
574 ret = btrfs_find_next_key(extent_root, path, &key, 0, 0);
575 if (ret)
576 break;
577
578 if (need_resched() ||
579 rwsem_is_contended(&fs_info->commit_root_sem)) {
580 if (wakeup)
581 caching_ctl->progress = last;
582 btrfs_release_path(path);
583 up_read(&fs_info->commit_root_sem);
584 mutex_unlock(&caching_ctl->mutex);
585 cond_resched();
586 mutex_lock(&caching_ctl->mutex);
587 down_read(&fs_info->commit_root_sem);
588 goto next;
589 }
590
591 ret = btrfs_next_leaf(extent_root, path);
592 if (ret < 0)
593 goto out;
594 if (ret)
595 break;
596 leaf = path->nodes[0];
597 nritems = btrfs_header_nritems(leaf);
598 continue;
599 }
600
601 if (key.objectid < last) {
602 key.objectid = last;
603 key.offset = 0;
604 key.type = BTRFS_EXTENT_ITEM_KEY;
605
606 if (wakeup)
607 caching_ctl->progress = last;
608 btrfs_release_path(path);
609 goto next;
610 }
611
David Sterbab3470b52019-10-23 18:48:22 +0200612 if (key.objectid < block_group->start) {
Josef Bacik9f212462019-08-06 16:43:19 +0200613 path->slots[0]++;
614 continue;
615 }
616
David Sterbab3470b52019-10-23 18:48:22 +0200617 if (key.objectid >= block_group->start + block_group->length)
Josef Bacik9f212462019-08-06 16:43:19 +0200618 break;
619
620 if (key.type == BTRFS_EXTENT_ITEM_KEY ||
621 key.type == BTRFS_METADATA_ITEM_KEY) {
622 total_found += add_new_free_space(block_group, last,
623 key.objectid);
624 if (key.type == BTRFS_METADATA_ITEM_KEY)
625 last = key.objectid +
626 fs_info->nodesize;
627 else
628 last = key.objectid + key.offset;
629
630 if (total_found > CACHING_CTL_WAKE_UP) {
631 total_found = 0;
632 if (wakeup)
633 wake_up(&caching_ctl->wait);
634 }
635 }
636 path->slots[0]++;
637 }
638 ret = 0;
639
640 total_found += add_new_free_space(block_group, last,
David Sterbab3470b52019-10-23 18:48:22 +0200641 block_group->start + block_group->length);
Josef Bacik9f212462019-08-06 16:43:19 +0200642 caching_ctl->progress = (u64)-1;
643
644out:
645 btrfs_free_path(path);
646 return ret;
647}
648
649static noinline void caching_thread(struct btrfs_work *work)
650{
David Sterba32da53862019-10-29 19:20:18 +0100651 struct btrfs_block_group *block_group;
Josef Bacik9f212462019-08-06 16:43:19 +0200652 struct btrfs_fs_info *fs_info;
653 struct btrfs_caching_control *caching_ctl;
654 int ret;
655
656 caching_ctl = container_of(work, struct btrfs_caching_control, work);
657 block_group = caching_ctl->block_group;
658 fs_info = block_group->fs_info;
659
660 mutex_lock(&caching_ctl->mutex);
661 down_read(&fs_info->commit_root_sem);
662
Josef Bacike7478532020-10-23 09:58:10 -0400663 if (btrfs_test_opt(fs_info, SPACE_CACHE)) {
664 ret = load_free_space_cache(block_group);
665 if (ret == 1) {
666 ret = 0;
667 goto done;
668 }
669
670 /*
671 * We failed to load the space cache, set ourselves to
672 * CACHE_STARTED and carry on.
673 */
674 spin_lock(&block_group->lock);
675 block_group->cached = BTRFS_CACHE_STARTED;
676 spin_unlock(&block_group->lock);
677 wake_up(&caching_ctl->wait);
678 }
679
Josef Bacik2f96e402021-01-15 16:26:17 -0500680 /*
681 * If we are in the transaction that populated the free space tree we
682 * can't actually cache from the free space tree as our commit root and
683 * real root are the same, so we could change the contents of the blocks
684 * while caching. Instead do the slow caching in this case, and after
685 * the transaction has committed we will be safe.
686 */
687 if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) &&
688 !(test_bit(BTRFS_FS_FREE_SPACE_TREE_UNTRUSTED, &fs_info->flags)))
Josef Bacik9f212462019-08-06 16:43:19 +0200689 ret = load_free_space_tree(caching_ctl);
690 else
691 ret = load_extent_tree_free(caching_ctl);
Josef Bacike7478532020-10-23 09:58:10 -0400692done:
Josef Bacik9f212462019-08-06 16:43:19 +0200693 spin_lock(&block_group->lock);
694 block_group->caching_ctl = NULL;
695 block_group->cached = ret ? BTRFS_CACHE_ERROR : BTRFS_CACHE_FINISHED;
696 spin_unlock(&block_group->lock);
697
698#ifdef CONFIG_BTRFS_DEBUG
699 if (btrfs_should_fragment_free_space(block_group)) {
700 u64 bytes_used;
701
702 spin_lock(&block_group->space_info->lock);
703 spin_lock(&block_group->lock);
David Sterbab3470b52019-10-23 18:48:22 +0200704 bytes_used = block_group->length - block_group->used;
Josef Bacik9f212462019-08-06 16:43:19 +0200705 block_group->space_info->bytes_used += bytes_used >> 1;
706 spin_unlock(&block_group->lock);
707 spin_unlock(&block_group->space_info->lock);
Josef Bacike11c0402019-06-20 15:38:07 -0400708 fragment_free_space(block_group);
Josef Bacik9f212462019-08-06 16:43:19 +0200709 }
710#endif
711
712 caching_ctl->progress = (u64)-1;
713
714 up_read(&fs_info->commit_root_sem);
715 btrfs_free_excluded_extents(block_group);
716 mutex_unlock(&caching_ctl->mutex);
717
718 wake_up(&caching_ctl->wait);
719
720 btrfs_put_caching_control(caching_ctl);
721 btrfs_put_block_group(block_group);
722}
723
David Sterba32da53862019-10-29 19:20:18 +0100724int btrfs_cache_block_group(struct btrfs_block_group *cache, int load_cache_only)
Josef Bacik9f212462019-08-06 16:43:19 +0200725{
726 DEFINE_WAIT(wait);
727 struct btrfs_fs_info *fs_info = cache->fs_info;
Josef Bacike7478532020-10-23 09:58:10 -0400728 struct btrfs_caching_control *caching_ctl = NULL;
Josef Bacik9f212462019-08-06 16:43:19 +0200729 int ret = 0;
730
Naohiro Aota2eda5702021-02-04 19:21:53 +0900731 /* Allocator for zoned filesystems does not use the cache at all */
732 if (btrfs_is_zoned(fs_info))
733 return 0;
734
Josef Bacik9f212462019-08-06 16:43:19 +0200735 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS);
736 if (!caching_ctl)
737 return -ENOMEM;
738
739 INIT_LIST_HEAD(&caching_ctl->list);
740 mutex_init(&caching_ctl->mutex);
741 init_waitqueue_head(&caching_ctl->wait);
742 caching_ctl->block_group = cache;
David Sterbab3470b52019-10-23 18:48:22 +0200743 caching_ctl->progress = cache->start;
Josef Bacike7478532020-10-23 09:58:10 -0400744 refcount_set(&caching_ctl->count, 2);
Omar Sandovala0cac0e2019-09-16 11:30:57 -0700745 btrfs_init_work(&caching_ctl->work, caching_thread, NULL, NULL);
Josef Bacik9f212462019-08-06 16:43:19 +0200746
747 spin_lock(&cache->lock);
Josef Bacik9f212462019-08-06 16:43:19 +0200748 if (cache->cached != BTRFS_CACHE_NO) {
Josef Bacik9f212462019-08-06 16:43:19 +0200749 kfree(caching_ctl);
Josef Bacike7478532020-10-23 09:58:10 -0400750
751 caching_ctl = cache->caching_ctl;
752 if (caching_ctl)
753 refcount_inc(&caching_ctl->count);
754 spin_unlock(&cache->lock);
755 goto out;
Josef Bacik9f212462019-08-06 16:43:19 +0200756 }
757 WARN_ON(cache->caching_ctl);
758 cache->caching_ctl = caching_ctl;
Josef Bacike7478532020-10-23 09:58:10 -0400759 if (btrfs_test_opt(fs_info, SPACE_CACHE))
760 cache->cached = BTRFS_CACHE_FAST;
761 else
762 cache->cached = BTRFS_CACHE_STARTED;
763 cache->has_caching_ctl = 1;
Josef Bacik9f212462019-08-06 16:43:19 +0200764 spin_unlock(&cache->lock);
765
Josef Bacikbbb86a32020-10-23 09:58:11 -0400766 spin_lock(&fs_info->block_group_cache_lock);
Josef Bacik9f212462019-08-06 16:43:19 +0200767 refcount_inc(&caching_ctl->count);
768 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
Josef Bacikbbb86a32020-10-23 09:58:11 -0400769 spin_unlock(&fs_info->block_group_cache_lock);
Josef Bacik9f212462019-08-06 16:43:19 +0200770
771 btrfs_get_block_group(cache);
772
773 btrfs_queue_work(fs_info->caching_workers, &caching_ctl->work);
Josef Bacike7478532020-10-23 09:58:10 -0400774out:
775 if (load_cache_only && caching_ctl)
776 btrfs_wait_space_cache_v1_finished(cache, caching_ctl);
777 if (caching_ctl)
778 btrfs_put_caching_control(caching_ctl);
Josef Bacik9f212462019-08-06 16:43:19 +0200779
780 return ret;
781}
Josef Bacike3e05202019-06-20 15:37:55 -0400782
783static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
784{
785 u64 extra_flags = chunk_to_extended(flags) &
786 BTRFS_EXTENDED_PROFILE_MASK;
787
788 write_seqlock(&fs_info->profiles_lock);
789 if (flags & BTRFS_BLOCK_GROUP_DATA)
790 fs_info->avail_data_alloc_bits &= ~extra_flags;
791 if (flags & BTRFS_BLOCK_GROUP_METADATA)
792 fs_info->avail_metadata_alloc_bits &= ~extra_flags;
793 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
794 fs_info->avail_system_alloc_bits &= ~extra_flags;
795 write_sequnlock(&fs_info->profiles_lock);
796}
797
798/*
799 * Clear incompat bits for the following feature(s):
800 *
801 * - RAID56 - in case there's neither RAID5 nor RAID6 profile block group
802 * in the whole filesystem
David Sterba9c907442019-10-31 15:52:01 +0100803 *
804 * - RAID1C34 - same as above for RAID1C3 and RAID1C4 block groups
Josef Bacike3e05202019-06-20 15:37:55 -0400805 */
806static void clear_incompat_bg_bits(struct btrfs_fs_info *fs_info, u64 flags)
807{
David Sterba9c907442019-10-31 15:52:01 +0100808 bool found_raid56 = false;
809 bool found_raid1c34 = false;
810
811 if ((flags & BTRFS_BLOCK_GROUP_RAID56_MASK) ||
812 (flags & BTRFS_BLOCK_GROUP_RAID1C3) ||
813 (flags & BTRFS_BLOCK_GROUP_RAID1C4)) {
Josef Bacike3e05202019-06-20 15:37:55 -0400814 struct list_head *head = &fs_info->space_info;
815 struct btrfs_space_info *sinfo;
816
817 list_for_each_entry_rcu(sinfo, head, list) {
Josef Bacike3e05202019-06-20 15:37:55 -0400818 down_read(&sinfo->groups_sem);
819 if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID5]))
David Sterba9c907442019-10-31 15:52:01 +0100820 found_raid56 = true;
Josef Bacike3e05202019-06-20 15:37:55 -0400821 if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID6]))
David Sterba9c907442019-10-31 15:52:01 +0100822 found_raid56 = true;
823 if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID1C3]))
824 found_raid1c34 = true;
825 if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID1C4]))
826 found_raid1c34 = true;
Josef Bacike3e05202019-06-20 15:37:55 -0400827 up_read(&sinfo->groups_sem);
Josef Bacike3e05202019-06-20 15:37:55 -0400828 }
Filipe Mananad8e6fd52020-03-20 18:43:48 +0000829 if (!found_raid56)
David Sterba9c907442019-10-31 15:52:01 +0100830 btrfs_clear_fs_incompat(fs_info, RAID56);
Filipe Mananad8e6fd52020-03-20 18:43:48 +0000831 if (!found_raid1c34)
David Sterba9c907442019-10-31 15:52:01 +0100832 btrfs_clear_fs_incompat(fs_info, RAID1C34);
Josef Bacike3e05202019-06-20 15:37:55 -0400833 }
834}
835
Qu Wenruo73576232020-05-05 07:58:21 +0800836static int remove_block_group_item(struct btrfs_trans_handle *trans,
837 struct btrfs_path *path,
838 struct btrfs_block_group *block_group)
839{
840 struct btrfs_fs_info *fs_info = trans->fs_info;
841 struct btrfs_root *root;
842 struct btrfs_key key;
843 int ret;
844
Josef Bacikdfe8aec2021-11-05 16:45:36 -0400845 root = btrfs_block_group_root(fs_info);
Qu Wenruo73576232020-05-05 07:58:21 +0800846 key.objectid = block_group->start;
847 key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
848 key.offset = block_group->length;
849
850 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
851 if (ret > 0)
852 ret = -ENOENT;
853 if (ret < 0)
854 return ret;
855
856 ret = btrfs_del_item(trans, root, path);
857 return ret;
858}
859
Josef Bacike3e05202019-06-20 15:37:55 -0400860int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
861 u64 group_start, struct extent_map *em)
862{
863 struct btrfs_fs_info *fs_info = trans->fs_info;
Josef Bacike3e05202019-06-20 15:37:55 -0400864 struct btrfs_path *path;
David Sterba32da53862019-10-29 19:20:18 +0100865 struct btrfs_block_group *block_group;
Josef Bacike3e05202019-06-20 15:37:55 -0400866 struct btrfs_free_cluster *cluster;
Josef Bacike3e05202019-06-20 15:37:55 -0400867 struct inode *inode;
868 struct kobject *kobj = NULL;
869 int ret;
870 int index;
871 int factor;
872 struct btrfs_caching_control *caching_ctl = NULL;
873 bool remove_em;
874 bool remove_rsv = false;
875
876 block_group = btrfs_lookup_block_group(fs_info, group_start);
877 BUG_ON(!block_group);
878 BUG_ON(!block_group->ro);
879
880 trace_btrfs_remove_block_group(block_group);
881 /*
882 * Free the reserved super bytes from this block group before
883 * remove it.
884 */
885 btrfs_free_excluded_extents(block_group);
David Sterbab3470b52019-10-23 18:48:22 +0200886 btrfs_free_ref_tree_range(fs_info, block_group->start,
887 block_group->length);
Josef Bacike3e05202019-06-20 15:37:55 -0400888
Josef Bacike3e05202019-06-20 15:37:55 -0400889 index = btrfs_bg_flags_to_raid_index(block_group->flags);
890 factor = btrfs_bg_type_to_factor(block_group->flags);
891
892 /* make sure this block group isn't part of an allocation cluster */
893 cluster = &fs_info->data_alloc_cluster;
894 spin_lock(&cluster->refill_lock);
895 btrfs_return_cluster_to_free_space(block_group, cluster);
896 spin_unlock(&cluster->refill_lock);
897
898 /*
899 * make sure this block group isn't part of a metadata
900 * allocation cluster
901 */
902 cluster = &fs_info->meta_alloc_cluster;
903 spin_lock(&cluster->refill_lock);
904 btrfs_return_cluster_to_free_space(block_group, cluster);
905 spin_unlock(&cluster->refill_lock);
906
Naohiro Aota40ab3be2021-02-04 19:22:18 +0900907 btrfs_clear_treelog_bg(block_group);
Johannes Thumshirnc2707a22021-09-09 01:19:26 +0900908 btrfs_clear_data_reloc_bg(block_group);
Naohiro Aota40ab3be2021-02-04 19:22:18 +0900909
Josef Bacike3e05202019-06-20 15:37:55 -0400910 path = btrfs_alloc_path();
911 if (!path) {
912 ret = -ENOMEM;
Filipe Manana9fecd132020-06-01 19:12:06 +0100913 goto out;
Josef Bacike3e05202019-06-20 15:37:55 -0400914 }
915
916 /*
917 * get the inode first so any iput calls done for the io_list
918 * aren't the final iput (no unlinks allowed now)
919 */
920 inode = lookup_free_space_inode(block_group, path);
921
922 mutex_lock(&trans->transaction->cache_write_mutex);
923 /*
924 * Make sure our free space cache IO is done before removing the
925 * free space inode
926 */
927 spin_lock(&trans->transaction->dirty_bgs_lock);
928 if (!list_empty(&block_group->io_list)) {
929 list_del_init(&block_group->io_list);
930
931 WARN_ON(!IS_ERR(inode) && inode != block_group->io_ctl.inode);
932
933 spin_unlock(&trans->transaction->dirty_bgs_lock);
934 btrfs_wait_cache_io(trans, block_group, path);
935 btrfs_put_block_group(block_group);
936 spin_lock(&trans->transaction->dirty_bgs_lock);
937 }
938
939 if (!list_empty(&block_group->dirty_list)) {
940 list_del_init(&block_group->dirty_list);
941 remove_rsv = true;
942 btrfs_put_block_group(block_group);
943 }
944 spin_unlock(&trans->transaction->dirty_bgs_lock);
945 mutex_unlock(&trans->transaction->cache_write_mutex);
946
Boris Burkov36b216c2020-11-18 15:06:25 -0800947 ret = btrfs_remove_free_space_inode(trans, inode, block_group);
948 if (ret)
Filipe Manana9fecd132020-06-01 19:12:06 +0100949 goto out;
Josef Bacike3e05202019-06-20 15:37:55 -0400950
951 spin_lock(&fs_info->block_group_cache_lock);
952 rb_erase(&block_group->cache_node,
953 &fs_info->block_group_cache_tree);
954 RB_CLEAR_NODE(&block_group->cache_node);
955
Filipe Manana9fecd132020-06-01 19:12:06 +0100956 /* Once for the block groups rbtree */
957 btrfs_put_block_group(block_group);
958
David Sterbab3470b52019-10-23 18:48:22 +0200959 if (fs_info->first_logical_byte == block_group->start)
Josef Bacike3e05202019-06-20 15:37:55 -0400960 fs_info->first_logical_byte = (u64)-1;
961 spin_unlock(&fs_info->block_group_cache_lock);
962
963 down_write(&block_group->space_info->groups_sem);
964 /*
965 * we must use list_del_init so people can check to see if they
966 * are still on the list after taking the semaphore
967 */
968 list_del_init(&block_group->list);
969 if (list_empty(&block_group->space_info->block_groups[index])) {
970 kobj = block_group->space_info->block_group_kobjs[index];
971 block_group->space_info->block_group_kobjs[index] = NULL;
972 clear_avail_alloc_bits(fs_info, block_group->flags);
973 }
974 up_write(&block_group->space_info->groups_sem);
975 clear_incompat_bg_bits(fs_info, block_group->flags);
976 if (kobj) {
977 kobject_del(kobj);
978 kobject_put(kobj);
979 }
980
981 if (block_group->has_caching_ctl)
982 caching_ctl = btrfs_get_caching_control(block_group);
983 if (block_group->cached == BTRFS_CACHE_STARTED)
984 btrfs_wait_block_group_cache_done(block_group);
985 if (block_group->has_caching_ctl) {
Josef Bacikbbb86a32020-10-23 09:58:11 -0400986 spin_lock(&fs_info->block_group_cache_lock);
Josef Bacike3e05202019-06-20 15:37:55 -0400987 if (!caching_ctl) {
988 struct btrfs_caching_control *ctl;
989
990 list_for_each_entry(ctl,
991 &fs_info->caching_block_groups, list)
992 if (ctl->block_group == block_group) {
993 caching_ctl = ctl;
994 refcount_inc(&caching_ctl->count);
995 break;
996 }
997 }
998 if (caching_ctl)
999 list_del_init(&caching_ctl->list);
Josef Bacikbbb86a32020-10-23 09:58:11 -04001000 spin_unlock(&fs_info->block_group_cache_lock);
Josef Bacike3e05202019-06-20 15:37:55 -04001001 if (caching_ctl) {
1002 /* Once for the caching bgs list and once for us. */
1003 btrfs_put_caching_control(caching_ctl);
1004 btrfs_put_caching_control(caching_ctl);
1005 }
1006 }
1007
1008 spin_lock(&trans->transaction->dirty_bgs_lock);
1009 WARN_ON(!list_empty(&block_group->dirty_list));
1010 WARN_ON(!list_empty(&block_group->io_list));
1011 spin_unlock(&trans->transaction->dirty_bgs_lock);
1012
1013 btrfs_remove_free_space_cache(block_group);
1014
1015 spin_lock(&block_group->space_info->lock);
1016 list_del_init(&block_group->ro_list);
1017
1018 if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
1019 WARN_ON(block_group->space_info->total_bytes
David Sterbab3470b52019-10-23 18:48:22 +02001020 < block_group->length);
Josef Bacike3e05202019-06-20 15:37:55 -04001021 WARN_ON(block_group->space_info->bytes_readonly
Naohiro Aota169e0da2021-02-04 19:21:52 +09001022 < block_group->length - block_group->zone_unusable);
1023 WARN_ON(block_group->space_info->bytes_zone_unusable
1024 < block_group->zone_unusable);
Josef Bacike3e05202019-06-20 15:37:55 -04001025 WARN_ON(block_group->space_info->disk_total
David Sterbab3470b52019-10-23 18:48:22 +02001026 < block_group->length * factor);
Josef Bacike3e05202019-06-20 15:37:55 -04001027 }
David Sterbab3470b52019-10-23 18:48:22 +02001028 block_group->space_info->total_bytes -= block_group->length;
Naohiro Aota169e0da2021-02-04 19:21:52 +09001029 block_group->space_info->bytes_readonly -=
1030 (block_group->length - block_group->zone_unusable);
1031 block_group->space_info->bytes_zone_unusable -=
1032 block_group->zone_unusable;
David Sterbab3470b52019-10-23 18:48:22 +02001033 block_group->space_info->disk_total -= block_group->length * factor;
Josef Bacike3e05202019-06-20 15:37:55 -04001034
1035 spin_unlock(&block_group->space_info->lock);
1036
Filipe Mananaffcb9d42020-06-01 19:12:19 +01001037 /*
1038 * Remove the free space for the block group from the free space tree
1039 * and the block group's item from the extent tree before marking the
1040 * block group as removed. This is to prevent races with tasks that
1041 * freeze and unfreeze a block group, this task and another task
1042 * allocating a new block group - the unfreeze task ends up removing
1043 * the block group's extent map before the task calling this function
1044 * deletes the block group item from the extent tree, allowing for
1045 * another task to attempt to create another block group with the same
1046 * item key (and failing with -EEXIST and a transaction abort).
1047 */
1048 ret = remove_block_group_free_space(trans, block_group);
1049 if (ret)
1050 goto out;
1051
1052 ret = remove_block_group_item(trans, path, block_group);
1053 if (ret < 0)
1054 goto out;
1055
Josef Bacike3e05202019-06-20 15:37:55 -04001056 spin_lock(&block_group->lock);
1057 block_group->removed = 1;
1058 /*
Filipe Manana6b7304a2020-05-08 11:01:47 +01001059 * At this point trimming or scrub can't start on this block group,
1060 * because we removed the block group from the rbtree
1061 * fs_info->block_group_cache_tree so no one can't find it anymore and
1062 * even if someone already got this block group before we removed it
1063 * from the rbtree, they have already incremented block_group->frozen -
1064 * if they didn't, for the trimming case they won't find any free space
1065 * entries because we already removed them all when we called
1066 * btrfs_remove_free_space_cache().
Josef Bacike3e05202019-06-20 15:37:55 -04001067 *
1068 * And we must not remove the extent map from the fs_info->mapping_tree
1069 * to prevent the same logical address range and physical device space
Filipe Manana6b7304a2020-05-08 11:01:47 +01001070 * ranges from being reused for a new block group. This is needed to
1071 * avoid races with trimming and scrub.
1072 *
1073 * An fs trim operation (btrfs_trim_fs() / btrfs_ioctl_fitrim()) is
Josef Bacike3e05202019-06-20 15:37:55 -04001074 * completely transactionless, so while it is trimming a range the
1075 * currently running transaction might finish and a new one start,
1076 * allowing for new block groups to be created that can reuse the same
1077 * physical device locations unless we take this special care.
1078 *
1079 * There may also be an implicit trim operation if the file system
1080 * is mounted with -odiscard. The same protections must remain
1081 * in place until the extents have been discarded completely when
1082 * the transaction commit has completed.
1083 */
Filipe Manana6b7304a2020-05-08 11:01:47 +01001084 remove_em = (atomic_read(&block_group->frozen) == 0);
Josef Bacike3e05202019-06-20 15:37:55 -04001085 spin_unlock(&block_group->lock);
1086
Josef Bacike3e05202019-06-20 15:37:55 -04001087 if (remove_em) {
1088 struct extent_map_tree *em_tree;
1089
1090 em_tree = &fs_info->mapping_tree;
1091 write_lock(&em_tree->lock);
1092 remove_extent_mapping(em_tree, em);
1093 write_unlock(&em_tree->lock);
1094 /* once for the tree */
1095 free_extent_map(em);
1096 }
Xiyu Yangf6033c52020-04-21 10:54:11 +08001097
Filipe Manana9fecd132020-06-01 19:12:06 +01001098out:
Xiyu Yangf6033c52020-04-21 10:54:11 +08001099 /* Once for the lookup reference */
1100 btrfs_put_block_group(block_group);
Josef Bacike3e05202019-06-20 15:37:55 -04001101 if (remove_rsv)
1102 btrfs_delayed_refs_rsv_release(fs_info, 1);
1103 btrfs_free_path(path);
1104 return ret;
1105}
1106
1107struct btrfs_trans_handle *btrfs_start_trans_remove_block_group(
1108 struct btrfs_fs_info *fs_info, const u64 chunk_offset)
1109{
Josef Bacikdfe8aec2021-11-05 16:45:36 -04001110 struct btrfs_root *root = btrfs_block_group_root(fs_info);
Josef Bacike3e05202019-06-20 15:37:55 -04001111 struct extent_map_tree *em_tree = &fs_info->mapping_tree;
1112 struct extent_map *em;
1113 struct map_lookup *map;
1114 unsigned int num_items;
1115
1116 read_lock(&em_tree->lock);
1117 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
1118 read_unlock(&em_tree->lock);
1119 ASSERT(em && em->start == chunk_offset);
1120
1121 /*
1122 * We need to reserve 3 + N units from the metadata space info in order
1123 * to remove a block group (done at btrfs_remove_chunk() and at
1124 * btrfs_remove_block_group()), which are used for:
1125 *
1126 * 1 unit for adding the free space inode's orphan (located in the tree
1127 * of tree roots).
1128 * 1 unit for deleting the block group item (located in the extent
1129 * tree).
1130 * 1 unit for deleting the free space item (located in tree of tree
1131 * roots).
1132 * N units for deleting N device extent items corresponding to each
1133 * stripe (located in the device tree).
1134 *
1135 * In order to remove a block group we also need to reserve units in the
1136 * system space info in order to update the chunk tree (update one or
1137 * more device items and remove one chunk item), but this is done at
1138 * btrfs_remove_chunk() through a call to check_system_chunk().
1139 */
1140 map = em->map_lookup;
1141 num_items = 3 + map->num_stripes;
1142 free_extent_map(em);
1143
Josef Bacikdfe8aec2021-11-05 16:45:36 -04001144 return btrfs_start_transaction_fallback_global_rsv(root, num_items);
Josef Bacike3e05202019-06-20 15:37:55 -04001145}
1146
1147/*
Josef Bacik26ce2092019-06-20 15:37:59 -04001148 * Mark block group @cache read-only, so later write won't happen to block
1149 * group @cache.
1150 *
1151 * If @force is not set, this function will only mark the block group readonly
1152 * if we have enough free space (1M) in other metadata/system block groups.
1153 * If @force is not set, this function will mark the block group readonly
1154 * without checking free space.
1155 *
1156 * NOTE: This function doesn't care if other block groups can contain all the
1157 * data in this block group. That check should be done by relocation routine,
1158 * not this function.
1159 */
David Sterba32da53862019-10-29 19:20:18 +01001160static int inc_block_group_ro(struct btrfs_block_group *cache, int force)
Josef Bacik26ce2092019-06-20 15:37:59 -04001161{
1162 struct btrfs_space_info *sinfo = cache->space_info;
1163 u64 num_bytes;
Josef Bacik26ce2092019-06-20 15:37:59 -04001164 int ret = -ENOSPC;
1165
Josef Bacik26ce2092019-06-20 15:37:59 -04001166 spin_lock(&sinfo->lock);
1167 spin_lock(&cache->lock);
1168
Filipe Manana195a49e2021-02-05 12:55:37 +00001169 if (cache->swap_extents) {
1170 ret = -ETXTBSY;
1171 goto out;
1172 }
1173
Josef Bacik26ce2092019-06-20 15:37:59 -04001174 if (cache->ro) {
1175 cache->ro++;
1176 ret = 0;
1177 goto out;
1178 }
1179
David Sterbab3470b52019-10-23 18:48:22 +02001180 num_bytes = cache->length - cache->reserved - cache->pinned -
Naohiro Aota169e0da2021-02-04 19:21:52 +09001181 cache->bytes_super - cache->zone_unusable - cache->used;
Josef Bacik26ce2092019-06-20 15:37:59 -04001182
1183 /*
Josef Bacika30a3d22020-01-17 09:07:39 -05001184 * Data never overcommits, even in mixed mode, so do just the straight
1185 * check of left over space in how much we have allocated.
Josef Bacik26ce2092019-06-20 15:37:59 -04001186 */
Josef Bacika30a3d22020-01-17 09:07:39 -05001187 if (force) {
1188 ret = 0;
1189 } else if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA) {
1190 u64 sinfo_used = btrfs_space_info_used(sinfo, true);
1191
1192 /*
1193 * Here we make sure if we mark this bg RO, we still have enough
1194 * free space as buffer.
1195 */
1196 if (sinfo_used + num_bytes <= sinfo->total_bytes)
1197 ret = 0;
1198 } else {
1199 /*
1200 * We overcommit metadata, so we need to do the
1201 * btrfs_can_overcommit check here, and we need to pass in
1202 * BTRFS_RESERVE_NO_FLUSH to give ourselves the most amount of
1203 * leeway to allow us to mark this block group as read only.
1204 */
1205 if (btrfs_can_overcommit(cache->fs_info, sinfo, num_bytes,
1206 BTRFS_RESERVE_NO_FLUSH))
1207 ret = 0;
1208 }
1209
1210 if (!ret) {
Josef Bacik26ce2092019-06-20 15:37:59 -04001211 sinfo->bytes_readonly += num_bytes;
Naohiro Aota169e0da2021-02-04 19:21:52 +09001212 if (btrfs_is_zoned(cache->fs_info)) {
1213 /* Migrate zone_unusable bytes to readonly */
1214 sinfo->bytes_readonly += cache->zone_unusable;
1215 sinfo->bytes_zone_unusable -= cache->zone_unusable;
1216 cache->zone_unusable = 0;
1217 }
Josef Bacik26ce2092019-06-20 15:37:59 -04001218 cache->ro++;
1219 list_add_tail(&cache->ro_list, &sinfo->ro_bgs);
Josef Bacik26ce2092019-06-20 15:37:59 -04001220 }
1221out:
1222 spin_unlock(&cache->lock);
1223 spin_unlock(&sinfo->lock);
1224 if (ret == -ENOSPC && btrfs_test_opt(cache->fs_info, ENOSPC_DEBUG)) {
1225 btrfs_info(cache->fs_info,
David Sterbab3470b52019-10-23 18:48:22 +02001226 "unable to make block group %llu ro", cache->start);
Josef Bacik26ce2092019-06-20 15:37:59 -04001227 btrfs_dump_space_info(cache->fs_info, cache->space_info, 0, 0);
1228 }
1229 return ret;
1230}
1231
Nikolay Borisovfe119a62020-01-20 16:09:18 +02001232static bool clean_pinned_extents(struct btrfs_trans_handle *trans,
1233 struct btrfs_block_group *bg)
Nikolay Borisov45bb5d62020-01-20 16:09:17 +02001234{
1235 struct btrfs_fs_info *fs_info = bg->fs_info;
Nikolay Borisovfe119a62020-01-20 16:09:18 +02001236 struct btrfs_transaction *prev_trans = NULL;
Nikolay Borisov45bb5d62020-01-20 16:09:17 +02001237 const u64 start = bg->start;
1238 const u64 end = start + bg->length - 1;
1239 int ret;
1240
Nikolay Borisovfe119a62020-01-20 16:09:18 +02001241 spin_lock(&fs_info->trans_lock);
1242 if (trans->transaction->list.prev != &fs_info->trans_list) {
1243 prev_trans = list_last_entry(&trans->transaction->list,
1244 struct btrfs_transaction, list);
1245 refcount_inc(&prev_trans->use_count);
1246 }
1247 spin_unlock(&fs_info->trans_lock);
1248
Nikolay Borisov45bb5d62020-01-20 16:09:17 +02001249 /*
1250 * Hold the unused_bg_unpin_mutex lock to avoid racing with
1251 * btrfs_finish_extent_commit(). If we are at transaction N, another
1252 * task might be running finish_extent_commit() for the previous
1253 * transaction N - 1, and have seen a range belonging to the block
Nikolay Borisovfe119a62020-01-20 16:09:18 +02001254 * group in pinned_extents before we were able to clear the whole block
1255 * group range from pinned_extents. This means that task can lookup for
1256 * the block group after we unpinned it from pinned_extents and removed
1257 * it, leading to a BUG_ON() at unpin_extent_range().
Nikolay Borisov45bb5d62020-01-20 16:09:17 +02001258 */
1259 mutex_lock(&fs_info->unused_bg_unpin_mutex);
Nikolay Borisovfe119a62020-01-20 16:09:18 +02001260 if (prev_trans) {
1261 ret = clear_extent_bits(&prev_trans->pinned_extents, start, end,
1262 EXTENT_DIRTY);
1263 if (ret)
Filipe Manana534cf532020-04-17 16:36:50 +01001264 goto out;
Nikolay Borisovfe119a62020-01-20 16:09:18 +02001265 }
Nikolay Borisov45bb5d62020-01-20 16:09:17 +02001266
Nikolay Borisovfe119a62020-01-20 16:09:18 +02001267 ret = clear_extent_bits(&trans->transaction->pinned_extents, start, end,
Nikolay Borisov45bb5d62020-01-20 16:09:17 +02001268 EXTENT_DIRTY);
Filipe Manana534cf532020-04-17 16:36:50 +01001269out:
Nikolay Borisov45bb5d62020-01-20 16:09:17 +02001270 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Filipe Manana5150bf12020-04-17 16:36:15 +01001271 if (prev_trans)
1272 btrfs_put_transaction(prev_trans);
Nikolay Borisov45bb5d62020-01-20 16:09:17 +02001273
Filipe Manana534cf532020-04-17 16:36:50 +01001274 return ret == 0;
Nikolay Borisov45bb5d62020-01-20 16:09:17 +02001275}
1276
Josef Bacik26ce2092019-06-20 15:37:59 -04001277/*
Josef Bacike3e05202019-06-20 15:37:55 -04001278 * Process the unused_bgs list and remove any that don't have any allocated
1279 * space inside of them.
1280 */
1281void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info)
1282{
David Sterba32da53862019-10-29 19:20:18 +01001283 struct btrfs_block_group *block_group;
Josef Bacike3e05202019-06-20 15:37:55 -04001284 struct btrfs_space_info *space_info;
1285 struct btrfs_trans_handle *trans;
Dennis Zhou6e80d4f2019-12-13 16:22:15 -08001286 const bool async_trim_enabled = btrfs_test_opt(fs_info, DISCARD_ASYNC);
Josef Bacike3e05202019-06-20 15:37:55 -04001287 int ret = 0;
1288
1289 if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags))
1290 return;
1291
Josef Bacikddfd08c2020-12-18 14:24:19 -05001292 /*
1293 * Long running balances can keep us blocked here for eternity, so
1294 * simply skip deletion if we're unable to get the mutex.
1295 */
Johannes Thumshirnf3372062021-04-19 16:41:01 +09001296 if (!mutex_trylock(&fs_info->reclaim_bgs_lock))
Josef Bacikddfd08c2020-12-18 14:24:19 -05001297 return;
1298
Josef Bacike3e05202019-06-20 15:37:55 -04001299 spin_lock(&fs_info->unused_bgs_lock);
1300 while (!list_empty(&fs_info->unused_bgs)) {
Josef Bacike3e05202019-06-20 15:37:55 -04001301 int trimming;
1302
1303 block_group = list_first_entry(&fs_info->unused_bgs,
David Sterba32da53862019-10-29 19:20:18 +01001304 struct btrfs_block_group,
Josef Bacike3e05202019-06-20 15:37:55 -04001305 bg_list);
1306 list_del_init(&block_group->bg_list);
1307
1308 space_info = block_group->space_info;
1309
1310 if (ret || btrfs_mixed_space_info(space_info)) {
1311 btrfs_put_block_group(block_group);
1312 continue;
1313 }
1314 spin_unlock(&fs_info->unused_bgs_lock);
1315
Dennis Zhoub0643e52019-12-13 16:22:14 -08001316 btrfs_discard_cancel_work(&fs_info->discard_ctl, block_group);
1317
Josef Bacike3e05202019-06-20 15:37:55 -04001318 /* Don't want to race with allocators so take the groups_sem */
1319 down_write(&space_info->groups_sem);
Dennis Zhou6e80d4f2019-12-13 16:22:15 -08001320
1321 /*
1322 * Async discard moves the final block group discard to be prior
1323 * to the unused_bgs code path. Therefore, if it's not fully
1324 * trimmed, punt it back to the async discard lists.
1325 */
1326 if (btrfs_test_opt(fs_info, DISCARD_ASYNC) &&
1327 !btrfs_is_free_space_trimmed(block_group)) {
1328 trace_btrfs_skip_unused_block_group(block_group);
1329 up_write(&space_info->groups_sem);
1330 /* Requeue if we failed because of async discard */
1331 btrfs_discard_queue_work(&fs_info->discard_ctl,
1332 block_group);
1333 goto next;
1334 }
1335
Josef Bacike3e05202019-06-20 15:37:55 -04001336 spin_lock(&block_group->lock);
1337 if (block_group->reserved || block_group->pinned ||
David Sterbabf38be62019-10-23 18:48:11 +02001338 block_group->used || block_group->ro ||
Josef Bacike3e05202019-06-20 15:37:55 -04001339 list_is_singular(&block_group->list)) {
1340 /*
1341 * We want to bail if we made new allocations or have
1342 * outstanding allocations in this block group. We do
1343 * the ro check in case balance is currently acting on
1344 * this block group.
1345 */
1346 trace_btrfs_skip_unused_block_group(block_group);
1347 spin_unlock(&block_group->lock);
1348 up_write(&space_info->groups_sem);
1349 goto next;
1350 }
1351 spin_unlock(&block_group->lock);
1352
1353 /* We don't want to force the issue, only flip if it's ok. */
Josef Bacike11c0402019-06-20 15:38:07 -04001354 ret = inc_block_group_ro(block_group, 0);
Josef Bacike3e05202019-06-20 15:37:55 -04001355 up_write(&space_info->groups_sem);
1356 if (ret < 0) {
1357 ret = 0;
1358 goto next;
1359 }
1360
1361 /*
1362 * Want to do this before we do anything else so we can recover
1363 * properly if we fail to join the transaction.
1364 */
1365 trans = btrfs_start_trans_remove_block_group(fs_info,
David Sterbab3470b52019-10-23 18:48:22 +02001366 block_group->start);
Josef Bacike3e05202019-06-20 15:37:55 -04001367 if (IS_ERR(trans)) {
1368 btrfs_dec_block_group_ro(block_group);
1369 ret = PTR_ERR(trans);
1370 goto next;
1371 }
1372
1373 /*
1374 * We could have pending pinned extents for this block group,
1375 * just delete them, we don't care about them anymore.
1376 */
Filipe Manana534cf532020-04-17 16:36:50 +01001377 if (!clean_pinned_extents(trans, block_group)) {
1378 btrfs_dec_block_group_ro(block_group);
Josef Bacike3e05202019-06-20 15:37:55 -04001379 goto end_trans;
Filipe Manana534cf532020-04-17 16:36:50 +01001380 }
Josef Bacike3e05202019-06-20 15:37:55 -04001381
Dennis Zhoub0643e52019-12-13 16:22:14 -08001382 /*
1383 * At this point, the block_group is read only and should fail
1384 * new allocations. However, btrfs_finish_extent_commit() can
1385 * cause this block_group to be placed back on the discard
1386 * lists because now the block_group isn't fully discarded.
1387 * Bail here and try again later after discarding everything.
1388 */
1389 spin_lock(&fs_info->discard_ctl.lock);
1390 if (!list_empty(&block_group->discard_list)) {
1391 spin_unlock(&fs_info->discard_ctl.lock);
1392 btrfs_dec_block_group_ro(block_group);
1393 btrfs_discard_queue_work(&fs_info->discard_ctl,
1394 block_group);
1395 goto end_trans;
1396 }
1397 spin_unlock(&fs_info->discard_ctl.lock);
1398
Josef Bacike3e05202019-06-20 15:37:55 -04001399 /* Reset pinned so btrfs_put_block_group doesn't complain */
1400 spin_lock(&space_info->lock);
1401 spin_lock(&block_group->lock);
1402
1403 btrfs_space_info_update_bytes_pinned(fs_info, space_info,
1404 -block_group->pinned);
1405 space_info->bytes_readonly += block_group->pinned;
Josef Bacike3e05202019-06-20 15:37:55 -04001406 block_group->pinned = 0;
1407
1408 spin_unlock(&block_group->lock);
1409 spin_unlock(&space_info->lock);
1410
Dennis Zhou6e80d4f2019-12-13 16:22:15 -08001411 /*
1412 * The normal path here is an unused block group is passed here,
1413 * then trimming is handled in the transaction commit path.
1414 * Async discard interposes before this to do the trimming
1415 * before coming down the unused block group path as trimming
1416 * will no longer be done later in the transaction commit path.
1417 */
1418 if (!async_trim_enabled && btrfs_test_opt(fs_info, DISCARD_ASYNC))
1419 goto flip_async;
1420
Naohiro Aotadcba6e42021-02-04 19:21:56 +09001421 /*
1422 * DISCARD can flip during remount. On zoned filesystems, we
1423 * need to reset sequential-required zones.
1424 */
1425 trimming = btrfs_test_opt(fs_info, DISCARD_SYNC) ||
1426 btrfs_is_zoned(fs_info);
Josef Bacike3e05202019-06-20 15:37:55 -04001427
1428 /* Implicit trim during transaction commit. */
1429 if (trimming)
Filipe Manana6b7304a2020-05-08 11:01:47 +01001430 btrfs_freeze_block_group(block_group);
Josef Bacike3e05202019-06-20 15:37:55 -04001431
1432 /*
1433 * Btrfs_remove_chunk will abort the transaction if things go
1434 * horribly wrong.
1435 */
David Sterbab3470b52019-10-23 18:48:22 +02001436 ret = btrfs_remove_chunk(trans, block_group->start);
Josef Bacike3e05202019-06-20 15:37:55 -04001437
1438 if (ret) {
1439 if (trimming)
Filipe Manana6b7304a2020-05-08 11:01:47 +01001440 btrfs_unfreeze_block_group(block_group);
Josef Bacike3e05202019-06-20 15:37:55 -04001441 goto end_trans;
1442 }
1443
1444 /*
1445 * If we're not mounted with -odiscard, we can just forget
1446 * about this block group. Otherwise we'll need to wait
1447 * until transaction commit to do the actual discard.
1448 */
1449 if (trimming) {
1450 spin_lock(&fs_info->unused_bgs_lock);
1451 /*
1452 * A concurrent scrub might have added us to the list
1453 * fs_info->unused_bgs, so use a list_move operation
1454 * to add the block group to the deleted_bgs list.
1455 */
1456 list_move(&block_group->bg_list,
1457 &trans->transaction->deleted_bgs);
1458 spin_unlock(&fs_info->unused_bgs_lock);
1459 btrfs_get_block_group(block_group);
1460 }
1461end_trans:
1462 btrfs_end_transaction(trans);
1463next:
Josef Bacike3e05202019-06-20 15:37:55 -04001464 btrfs_put_block_group(block_group);
1465 spin_lock(&fs_info->unused_bgs_lock);
1466 }
1467 spin_unlock(&fs_info->unused_bgs_lock);
Johannes Thumshirnf3372062021-04-19 16:41:01 +09001468 mutex_unlock(&fs_info->reclaim_bgs_lock);
Dennis Zhou6e80d4f2019-12-13 16:22:15 -08001469 return;
1470
1471flip_async:
1472 btrfs_end_transaction(trans);
Johannes Thumshirnf3372062021-04-19 16:41:01 +09001473 mutex_unlock(&fs_info->reclaim_bgs_lock);
Dennis Zhou6e80d4f2019-12-13 16:22:15 -08001474 btrfs_put_block_group(block_group);
1475 btrfs_discard_punt_unused_bgs_list(fs_info);
Josef Bacike3e05202019-06-20 15:37:55 -04001476}
1477
David Sterba32da53862019-10-29 19:20:18 +01001478void btrfs_mark_bg_unused(struct btrfs_block_group *bg)
Josef Bacike3e05202019-06-20 15:37:55 -04001479{
1480 struct btrfs_fs_info *fs_info = bg->fs_info;
1481
1482 spin_lock(&fs_info->unused_bgs_lock);
1483 if (list_empty(&bg->bg_list)) {
1484 btrfs_get_block_group(bg);
1485 trace_btrfs_add_unused_block_group(bg);
1486 list_add_tail(&bg->bg_list, &fs_info->unused_bgs);
1487 }
1488 spin_unlock(&fs_info->unused_bgs_lock);
1489}
Josef Bacik4358d9632019-06-20 15:37:57 -04001490
Johannes Thumshirn2ca0ec72021-10-14 18:39:02 +09001491/*
1492 * We want block groups with a low number of used bytes to be in the beginning
1493 * of the list, so they will get reclaimed first.
1494 */
1495static int reclaim_bgs_cmp(void *unused, const struct list_head *a,
1496 const struct list_head *b)
1497{
1498 const struct btrfs_block_group *bg1, *bg2;
1499
1500 bg1 = list_entry(a, struct btrfs_block_group, bg_list);
1501 bg2 = list_entry(b, struct btrfs_block_group, bg_list);
1502
1503 return bg1->used > bg2->used;
1504}
1505
Johannes Thumshirn18bb8bb2021-04-19 16:41:02 +09001506void btrfs_reclaim_bgs_work(struct work_struct *work)
1507{
1508 struct btrfs_fs_info *fs_info =
1509 container_of(work, struct btrfs_fs_info, reclaim_bgs_work);
1510 struct btrfs_block_group *bg;
1511 struct btrfs_space_info *space_info;
Johannes Thumshirn18bb8bb2021-04-19 16:41:02 +09001512
1513 if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags))
1514 return;
1515
1516 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_BALANCE))
1517 return;
1518
Johannes Thumshirn9cc0b832021-07-06 01:32:38 +09001519 /*
1520 * Long running balances can keep us blocked here for eternity, so
1521 * simply skip reclaim if we're unable to get the mutex.
1522 */
1523 if (!mutex_trylock(&fs_info->reclaim_bgs_lock)) {
1524 btrfs_exclop_finish(fs_info);
1525 return;
1526 }
1527
Johannes Thumshirn18bb8bb2021-04-19 16:41:02 +09001528 spin_lock(&fs_info->unused_bgs_lock);
Johannes Thumshirn2ca0ec72021-10-14 18:39:02 +09001529 /*
1530 * Sort happens under lock because we can't simply splice it and sort.
1531 * The block groups might still be in use and reachable via bg_list,
1532 * and their presence in the reclaim_bgs list must be preserved.
1533 */
1534 list_sort(NULL, &fs_info->reclaim_bgs, reclaim_bgs_cmp);
Johannes Thumshirn18bb8bb2021-04-19 16:41:02 +09001535 while (!list_empty(&fs_info->reclaim_bgs)) {
Johannes Thumshirn5f93e772021-06-29 03:16:46 +09001536 u64 zone_unusable;
Filipe Manana1cea5cf2021-06-21 11:10:38 +01001537 int ret = 0;
1538
Johannes Thumshirn18bb8bb2021-04-19 16:41:02 +09001539 bg = list_first_entry(&fs_info->reclaim_bgs,
1540 struct btrfs_block_group,
1541 bg_list);
1542 list_del_init(&bg->bg_list);
1543
1544 space_info = bg->space_info;
1545 spin_unlock(&fs_info->unused_bgs_lock);
1546
1547 /* Don't race with allocators so take the groups_sem */
1548 down_write(&space_info->groups_sem);
1549
1550 spin_lock(&bg->lock);
1551 if (bg->reserved || bg->pinned || bg->ro) {
1552 /*
1553 * We want to bail if we made new allocations or have
1554 * outstanding allocations in this block group. We do
1555 * the ro check in case balance is currently acting on
1556 * this block group.
1557 */
1558 spin_unlock(&bg->lock);
1559 up_write(&space_info->groups_sem);
1560 goto next;
1561 }
1562 spin_unlock(&bg->lock);
1563
1564 /* Get out fast, in case we're unmounting the filesystem */
1565 if (btrfs_fs_closing(fs_info)) {
1566 up_write(&space_info->groups_sem);
1567 goto next;
1568 }
1569
Johannes Thumshirn5f93e772021-06-29 03:16:46 +09001570 /*
1571 * Cache the zone_unusable value before turning the block group
1572 * to read only. As soon as the blog group is read only it's
1573 * zone_unusable value gets moved to the block group's read-only
1574 * bytes and isn't available for calculations anymore.
1575 */
1576 zone_unusable = bg->zone_unusable;
Johannes Thumshirn18bb8bb2021-04-19 16:41:02 +09001577 ret = inc_block_group_ro(bg, 0);
1578 up_write(&space_info->groups_sem);
1579 if (ret < 0)
1580 goto next;
1581
Johannes Thumshirn5f93e772021-06-29 03:16:46 +09001582 btrfs_info(fs_info,
1583 "reclaiming chunk %llu with %llu%% used %llu%% unusable",
1584 bg->start, div_u64(bg->used * 100, bg->length),
1585 div64_u64(zone_unusable * 100, bg->length));
Johannes Thumshirn18bb8bb2021-04-19 16:41:02 +09001586 trace_btrfs_reclaim_block_group(bg);
1587 ret = btrfs_relocate_chunk(fs_info, bg->start);
Filipe Mananad96b3422021-11-22 12:03:38 +00001588 if (ret)
Johannes Thumshirn18bb8bb2021-04-19 16:41:02 +09001589 btrfs_err(fs_info, "error relocating chunk %llu",
1590 bg->start);
1591
1592next:
Filipe Mananad96b3422021-11-22 12:03:38 +00001593 btrfs_put_block_group(bg);
Johannes Thumshirn18bb8bb2021-04-19 16:41:02 +09001594 spin_lock(&fs_info->unused_bgs_lock);
1595 }
1596 spin_unlock(&fs_info->unused_bgs_lock);
1597 mutex_unlock(&fs_info->reclaim_bgs_lock);
1598 btrfs_exclop_finish(fs_info);
1599}
1600
1601void btrfs_reclaim_bgs(struct btrfs_fs_info *fs_info)
1602{
1603 spin_lock(&fs_info->unused_bgs_lock);
1604 if (!list_empty(&fs_info->reclaim_bgs))
1605 queue_work(system_unbound_wq, &fs_info->reclaim_bgs_work);
1606 spin_unlock(&fs_info->unused_bgs_lock);
1607}
1608
1609void btrfs_mark_bg_to_reclaim(struct btrfs_block_group *bg)
1610{
1611 struct btrfs_fs_info *fs_info = bg->fs_info;
1612
1613 spin_lock(&fs_info->unused_bgs_lock);
1614 if (list_empty(&bg->bg_list)) {
1615 btrfs_get_block_group(bg);
1616 trace_btrfs_add_reclaim_block_group(bg);
1617 list_add_tail(&bg->bg_list, &fs_info->reclaim_bgs);
1618 }
1619 spin_unlock(&fs_info->unused_bgs_lock);
1620}
1621
Johannes Thumshirne3ba67a2020-06-02 19:05:57 +09001622static int read_bg_from_eb(struct btrfs_fs_info *fs_info, struct btrfs_key *key,
1623 struct btrfs_path *path)
1624{
1625 struct extent_map_tree *em_tree;
1626 struct extent_map *em;
1627 struct btrfs_block_group_item bg;
1628 struct extent_buffer *leaf;
1629 int slot;
1630 u64 flags;
1631 int ret = 0;
1632
1633 slot = path->slots[0];
1634 leaf = path->nodes[0];
1635
1636 em_tree = &fs_info->mapping_tree;
1637 read_lock(&em_tree->lock);
1638 em = lookup_extent_mapping(em_tree, key->objectid, key->offset);
1639 read_unlock(&em_tree->lock);
1640 if (!em) {
1641 btrfs_err(fs_info,
1642 "logical %llu len %llu found bg but no related chunk",
1643 key->objectid, key->offset);
1644 return -ENOENT;
1645 }
1646
1647 if (em->start != key->objectid || em->len != key->offset) {
1648 btrfs_err(fs_info,
1649 "block group %llu len %llu mismatch with chunk %llu len %llu",
1650 key->objectid, key->offset, em->start, em->len);
1651 ret = -EUCLEAN;
1652 goto out_free_em;
1653 }
1654
1655 read_extent_buffer(leaf, &bg, btrfs_item_ptr_offset(leaf, slot),
1656 sizeof(bg));
1657 flags = btrfs_stack_block_group_flags(&bg) &
1658 BTRFS_BLOCK_GROUP_TYPE_MASK;
1659
1660 if (flags != (em->map_lookup->type & BTRFS_BLOCK_GROUP_TYPE_MASK)) {
1661 btrfs_err(fs_info,
1662"block group %llu len %llu type flags 0x%llx mismatch with chunk type flags 0x%llx",
1663 key->objectid, key->offset, flags,
1664 (BTRFS_BLOCK_GROUP_TYPE_MASK & em->map_lookup->type));
1665 ret = -EUCLEAN;
1666 }
1667
1668out_free_em:
1669 free_extent_map(em);
1670 return ret;
1671}
1672
Josef Bacik4358d9632019-06-20 15:37:57 -04001673static int find_first_block_group(struct btrfs_fs_info *fs_info,
1674 struct btrfs_path *path,
1675 struct btrfs_key *key)
1676{
Josef Bacikdfe8aec2021-11-05 16:45:36 -04001677 struct btrfs_root *root = btrfs_block_group_root(fs_info);
Johannes Thumshirne3ba67a2020-06-02 19:05:57 +09001678 int ret;
Josef Bacik4358d9632019-06-20 15:37:57 -04001679 struct btrfs_key found_key;
1680 struct extent_buffer *leaf;
Josef Bacik4358d9632019-06-20 15:37:57 -04001681 int slot;
1682
1683 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
1684 if (ret < 0)
Johannes Thumshirne3ba67a2020-06-02 19:05:57 +09001685 return ret;
Josef Bacik4358d9632019-06-20 15:37:57 -04001686
1687 while (1) {
1688 slot = path->slots[0];
1689 leaf = path->nodes[0];
1690 if (slot >= btrfs_header_nritems(leaf)) {
1691 ret = btrfs_next_leaf(root, path);
1692 if (ret == 0)
1693 continue;
1694 if (ret < 0)
1695 goto out;
1696 break;
1697 }
1698 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1699
1700 if (found_key.objectid >= key->objectid &&
1701 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
Johannes Thumshirne3ba67a2020-06-02 19:05:57 +09001702 ret = read_bg_from_eb(fs_info, &found_key, path);
1703 break;
Josef Bacik4358d9632019-06-20 15:37:57 -04001704 }
Johannes Thumshirne3ba67a2020-06-02 19:05:57 +09001705
Josef Bacik4358d9632019-06-20 15:37:57 -04001706 path->slots[0]++;
1707 }
1708out:
1709 return ret;
1710}
1711
1712static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1713{
1714 u64 extra_flags = chunk_to_extended(flags) &
1715 BTRFS_EXTENDED_PROFILE_MASK;
1716
1717 write_seqlock(&fs_info->profiles_lock);
1718 if (flags & BTRFS_BLOCK_GROUP_DATA)
1719 fs_info->avail_data_alloc_bits |= extra_flags;
1720 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1721 fs_info->avail_metadata_alloc_bits |= extra_flags;
1722 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1723 fs_info->avail_system_alloc_bits |= extra_flags;
1724 write_sequnlock(&fs_info->profiles_lock);
1725}
1726
Nikolay Borisov96a14332019-12-10 19:57:51 +02001727/**
Nikolay Borisov9ee9b972021-01-22 11:57:58 +02001728 * Map a physical disk address to a list of logical addresses
1729 *
1730 * @fs_info: the filesystem
Nikolay Borisov96a14332019-12-10 19:57:51 +02001731 * @chunk_start: logical address of block group
Naohiro Aota138082f2021-02-04 19:22:02 +09001732 * @bdev: physical device to resolve, can be NULL to indicate any device
Nikolay Borisov96a14332019-12-10 19:57:51 +02001733 * @physical: physical address to map to logical addresses
1734 * @logical: return array of logical addresses which map to @physical
1735 * @naddrs: length of @logical
1736 * @stripe_len: size of IO stripe for the given block group
1737 *
1738 * Maps a particular @physical disk address to a list of @logical addresses.
1739 * Used primarily to exclude those portions of a block group that contain super
1740 * block copies.
1741 */
Nikolay Borisov96a14332019-12-10 19:57:51 +02001742int btrfs_rmap_block(struct btrfs_fs_info *fs_info, u64 chunk_start,
Naohiro Aota138082f2021-02-04 19:22:02 +09001743 struct block_device *bdev, u64 physical, u64 **logical,
1744 int *naddrs, int *stripe_len)
Nikolay Borisov96a14332019-12-10 19:57:51 +02001745{
1746 struct extent_map *em;
1747 struct map_lookup *map;
1748 u64 *buf;
1749 u64 bytenr;
Nikolay Borisov1776ad12019-11-19 14:05:53 +02001750 u64 data_stripe_length;
1751 u64 io_stripe_size;
1752 int i, nr = 0;
1753 int ret = 0;
Nikolay Borisov96a14332019-12-10 19:57:51 +02001754
1755 em = btrfs_get_chunk_map(fs_info, chunk_start, 1);
1756 if (IS_ERR(em))
1757 return -EIO;
1758
1759 map = em->map_lookup;
Nikolay Borisov9e22b922020-04-03 16:40:34 +03001760 data_stripe_length = em->orig_block_len;
Nikolay Borisov1776ad12019-11-19 14:05:53 +02001761 io_stripe_size = map->stripe_len;
Naohiro Aota138082f2021-02-04 19:22:02 +09001762 chunk_start = em->start;
Nikolay Borisov96a14332019-12-10 19:57:51 +02001763
Nikolay Borisov9e22b922020-04-03 16:40:34 +03001764 /* For RAID5/6 adjust to a full IO stripe length */
1765 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
Nikolay Borisov1776ad12019-11-19 14:05:53 +02001766 io_stripe_size = map->stripe_len * nr_data_stripes(map);
Nikolay Borisov96a14332019-12-10 19:57:51 +02001767
1768 buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS);
Nikolay Borisov1776ad12019-11-19 14:05:53 +02001769 if (!buf) {
1770 ret = -ENOMEM;
1771 goto out;
1772 }
Nikolay Borisov96a14332019-12-10 19:57:51 +02001773
1774 for (i = 0; i < map->num_stripes; i++) {
Nikolay Borisov1776ad12019-11-19 14:05:53 +02001775 bool already_inserted = false;
1776 u64 stripe_nr;
Naohiro Aota138082f2021-02-04 19:22:02 +09001777 u64 offset;
Nikolay Borisov1776ad12019-11-19 14:05:53 +02001778 int j;
1779
1780 if (!in_range(physical, map->stripes[i].physical,
1781 data_stripe_length))
Nikolay Borisov96a14332019-12-10 19:57:51 +02001782 continue;
1783
Naohiro Aota138082f2021-02-04 19:22:02 +09001784 if (bdev && map->stripes[i].dev->bdev != bdev)
1785 continue;
1786
Nikolay Borisov96a14332019-12-10 19:57:51 +02001787 stripe_nr = physical - map->stripes[i].physical;
Naohiro Aota138082f2021-02-04 19:22:02 +09001788 stripe_nr = div64_u64_rem(stripe_nr, map->stripe_len, &offset);
Nikolay Borisov96a14332019-12-10 19:57:51 +02001789
1790 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
1791 stripe_nr = stripe_nr * map->num_stripes + i;
1792 stripe_nr = div_u64(stripe_nr, map->sub_stripes);
1793 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
1794 stripe_nr = stripe_nr * map->num_stripes + i;
1795 }
1796 /*
1797 * The remaining case would be for RAID56, multiply by
1798 * nr_data_stripes(). Alternatively, just use rmap_len below
1799 * instead of map->stripe_len
1800 */
1801
Naohiro Aota138082f2021-02-04 19:22:02 +09001802 bytenr = chunk_start + stripe_nr * io_stripe_size + offset;
Nikolay Borisov1776ad12019-11-19 14:05:53 +02001803
1804 /* Ensure we don't add duplicate addresses */
Nikolay Borisov96a14332019-12-10 19:57:51 +02001805 for (j = 0; j < nr; j++) {
Nikolay Borisov1776ad12019-11-19 14:05:53 +02001806 if (buf[j] == bytenr) {
1807 already_inserted = true;
Nikolay Borisov96a14332019-12-10 19:57:51 +02001808 break;
Nikolay Borisov1776ad12019-11-19 14:05:53 +02001809 }
Nikolay Borisov96a14332019-12-10 19:57:51 +02001810 }
Nikolay Borisov1776ad12019-11-19 14:05:53 +02001811
1812 if (!already_inserted)
Nikolay Borisov96a14332019-12-10 19:57:51 +02001813 buf[nr++] = bytenr;
Nikolay Borisov96a14332019-12-10 19:57:51 +02001814 }
1815
1816 *logical = buf;
1817 *naddrs = nr;
Nikolay Borisov1776ad12019-11-19 14:05:53 +02001818 *stripe_len = io_stripe_size;
1819out:
Nikolay Borisov96a14332019-12-10 19:57:51 +02001820 free_extent_map(em);
Nikolay Borisov1776ad12019-11-19 14:05:53 +02001821 return ret;
Nikolay Borisov96a14332019-12-10 19:57:51 +02001822}
1823
David Sterba32da53862019-10-29 19:20:18 +01001824static int exclude_super_stripes(struct btrfs_block_group *cache)
Josef Bacik4358d9632019-06-20 15:37:57 -04001825{
1826 struct btrfs_fs_info *fs_info = cache->fs_info;
Naohiro Aota12659252020-11-10 20:26:14 +09001827 const bool zoned = btrfs_is_zoned(fs_info);
Josef Bacik4358d9632019-06-20 15:37:57 -04001828 u64 bytenr;
1829 u64 *logical;
1830 int stripe_len;
1831 int i, nr, ret;
1832
David Sterbab3470b52019-10-23 18:48:22 +02001833 if (cache->start < BTRFS_SUPER_INFO_OFFSET) {
1834 stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->start;
Josef Bacik4358d9632019-06-20 15:37:57 -04001835 cache->bytes_super += stripe_len;
David Sterbab3470b52019-10-23 18:48:22 +02001836 ret = btrfs_add_excluded_extent(fs_info, cache->start,
Josef Bacik4358d9632019-06-20 15:37:57 -04001837 stripe_len);
1838 if (ret)
1839 return ret;
1840 }
1841
1842 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1843 bytenr = btrfs_sb_offset(i);
Naohiro Aota138082f2021-02-04 19:22:02 +09001844 ret = btrfs_rmap_block(fs_info, cache->start, NULL,
Josef Bacik4358d9632019-06-20 15:37:57 -04001845 bytenr, &logical, &nr, &stripe_len);
1846 if (ret)
1847 return ret;
1848
Naohiro Aota12659252020-11-10 20:26:14 +09001849 /* Shouldn't have super stripes in sequential zones */
1850 if (zoned && nr) {
1851 btrfs_err(fs_info,
1852 "zoned: block group %llu must not contain super block",
1853 cache->start);
1854 return -EUCLEAN;
1855 }
1856
Josef Bacik4358d9632019-06-20 15:37:57 -04001857 while (nr--) {
Nikolay Borisov96f9b0f2020-04-03 16:40:35 +03001858 u64 len = min_t(u64, stripe_len,
1859 cache->start + cache->length - logical[nr]);
Josef Bacik4358d9632019-06-20 15:37:57 -04001860
1861 cache->bytes_super += len;
Nikolay Borisov96f9b0f2020-04-03 16:40:35 +03001862 ret = btrfs_add_excluded_extent(fs_info, logical[nr],
1863 len);
Josef Bacik4358d9632019-06-20 15:37:57 -04001864 if (ret) {
1865 kfree(logical);
1866 return ret;
1867 }
1868 }
1869
1870 kfree(logical);
1871 }
1872 return 0;
1873}
1874
David Sterba32da53862019-10-29 19:20:18 +01001875static void link_block_group(struct btrfs_block_group *cache)
Josef Bacik4358d9632019-06-20 15:37:57 -04001876{
1877 struct btrfs_space_info *space_info = cache->space_info;
1878 int index = btrfs_bg_flags_to_raid_index(cache->flags);
Josef Bacik4358d9632019-06-20 15:37:57 -04001879
1880 down_write(&space_info->groups_sem);
Josef Bacik4358d9632019-06-20 15:37:57 -04001881 list_add_tail(&cache->list, &space_info->block_groups[index]);
1882 up_write(&space_info->groups_sem);
Josef Bacik4358d9632019-06-20 15:37:57 -04001883}
1884
David Sterba32da53862019-10-29 19:20:18 +01001885static struct btrfs_block_group *btrfs_create_block_group_cache(
Qu Wenruo9afc6642020-05-05 07:58:20 +08001886 struct btrfs_fs_info *fs_info, u64 start)
Josef Bacik4358d9632019-06-20 15:37:57 -04001887{
David Sterba32da53862019-10-29 19:20:18 +01001888 struct btrfs_block_group *cache;
Josef Bacik4358d9632019-06-20 15:37:57 -04001889
1890 cache = kzalloc(sizeof(*cache), GFP_NOFS);
1891 if (!cache)
1892 return NULL;
1893
1894 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
1895 GFP_NOFS);
1896 if (!cache->free_space_ctl) {
1897 kfree(cache);
1898 return NULL;
1899 }
1900
David Sterbab3470b52019-10-23 18:48:22 +02001901 cache->start = start;
Josef Bacik4358d9632019-06-20 15:37:57 -04001902
1903 cache->fs_info = fs_info;
1904 cache->full_stripe_len = btrfs_full_stripe_len(fs_info, start);
Josef Bacik4358d9632019-06-20 15:37:57 -04001905
Dennis Zhou6e80d4f2019-12-13 16:22:15 -08001906 cache->discard_index = BTRFS_DISCARD_INDEX_UNUSED;
1907
Josef Bacik48aaeeb2020-07-06 09:14:11 -04001908 refcount_set(&cache->refs, 1);
Josef Bacik4358d9632019-06-20 15:37:57 -04001909 spin_lock_init(&cache->lock);
1910 init_rwsem(&cache->data_rwsem);
1911 INIT_LIST_HEAD(&cache->list);
1912 INIT_LIST_HEAD(&cache->cluster_list);
1913 INIT_LIST_HEAD(&cache->bg_list);
1914 INIT_LIST_HEAD(&cache->ro_list);
Dennis Zhoub0643e52019-12-13 16:22:14 -08001915 INIT_LIST_HEAD(&cache->discard_list);
Josef Bacik4358d9632019-06-20 15:37:57 -04001916 INIT_LIST_HEAD(&cache->dirty_list);
1917 INIT_LIST_HEAD(&cache->io_list);
Naohiro Aotaafba2bc2021-08-19 21:19:17 +09001918 INIT_LIST_HEAD(&cache->active_bg_list);
Josef Bacikcd799092020-10-23 09:58:08 -04001919 btrfs_init_free_space_ctl(cache, cache->free_space_ctl);
Filipe Manana6b7304a2020-05-08 11:01:47 +01001920 atomic_set(&cache->frozen, 0);
Josef Bacik4358d9632019-06-20 15:37:57 -04001921 mutex_init(&cache->free_space_lock);
1922 btrfs_init_full_stripe_locks_tree(&cache->full_stripe_locks_root);
1923
1924 return cache;
1925}
1926
1927/*
1928 * Iterate all chunks and verify that each of them has the corresponding block
1929 * group
1930 */
1931static int check_chunk_block_group_mappings(struct btrfs_fs_info *fs_info)
1932{
1933 struct extent_map_tree *map_tree = &fs_info->mapping_tree;
1934 struct extent_map *em;
David Sterba32da53862019-10-29 19:20:18 +01001935 struct btrfs_block_group *bg;
Josef Bacik4358d9632019-06-20 15:37:57 -04001936 u64 start = 0;
1937 int ret = 0;
1938
1939 while (1) {
1940 read_lock(&map_tree->lock);
1941 /*
1942 * lookup_extent_mapping will return the first extent map
1943 * intersecting the range, so setting @len to 1 is enough to
1944 * get the first chunk.
1945 */
1946 em = lookup_extent_mapping(map_tree, start, 1);
1947 read_unlock(&map_tree->lock);
1948 if (!em)
1949 break;
1950
1951 bg = btrfs_lookup_block_group(fs_info, em->start);
1952 if (!bg) {
1953 btrfs_err(fs_info,
1954 "chunk start=%llu len=%llu doesn't have corresponding block group",
1955 em->start, em->len);
1956 ret = -EUCLEAN;
1957 free_extent_map(em);
1958 break;
1959 }
David Sterbab3470b52019-10-23 18:48:22 +02001960 if (bg->start != em->start || bg->length != em->len ||
Josef Bacik4358d9632019-06-20 15:37:57 -04001961 (bg->flags & BTRFS_BLOCK_GROUP_TYPE_MASK) !=
1962 (em->map_lookup->type & BTRFS_BLOCK_GROUP_TYPE_MASK)) {
1963 btrfs_err(fs_info,
1964"chunk start=%llu len=%llu flags=0x%llx doesn't match block group start=%llu len=%llu flags=0x%llx",
1965 em->start, em->len,
1966 em->map_lookup->type & BTRFS_BLOCK_GROUP_TYPE_MASK,
David Sterbab3470b52019-10-23 18:48:22 +02001967 bg->start, bg->length,
Josef Bacik4358d9632019-06-20 15:37:57 -04001968 bg->flags & BTRFS_BLOCK_GROUP_TYPE_MASK);
1969 ret = -EUCLEAN;
1970 free_extent_map(em);
1971 btrfs_put_block_group(bg);
1972 break;
1973 }
1974 start = em->start + em->len;
1975 free_extent_map(em);
1976 btrfs_put_block_group(bg);
1977 }
1978 return ret;
1979}
1980
Qu Wenruoffb9e0f2019-10-10 10:39:27 +08001981static int read_one_block_group(struct btrfs_fs_info *info,
Johannes Thumshirn4afd2fe2021-02-04 19:21:44 +09001982 struct btrfs_block_group_item *bgi,
Qu Wenruod49a2dd2019-11-05 09:35:35 +08001983 const struct btrfs_key *key,
Qu Wenruoffb9e0f2019-10-10 10:39:27 +08001984 int need_clear)
1985{
David Sterba32da53862019-10-29 19:20:18 +01001986 struct btrfs_block_group *cache;
Qu Wenruoffb9e0f2019-10-10 10:39:27 +08001987 struct btrfs_space_info *space_info;
Qu Wenruoffb9e0f2019-10-10 10:39:27 +08001988 const bool mixed = btrfs_fs_incompat(info, MIXED_GROUPS);
Qu Wenruoffb9e0f2019-10-10 10:39:27 +08001989 int ret;
1990
Qu Wenruod49a2dd2019-11-05 09:35:35 +08001991 ASSERT(key->type == BTRFS_BLOCK_GROUP_ITEM_KEY);
Qu Wenruoffb9e0f2019-10-10 10:39:27 +08001992
Qu Wenruo9afc6642020-05-05 07:58:20 +08001993 cache = btrfs_create_block_group_cache(info, key->objectid);
Qu Wenruoffb9e0f2019-10-10 10:39:27 +08001994 if (!cache)
1995 return -ENOMEM;
1996
Johannes Thumshirn4afd2fe2021-02-04 19:21:44 +09001997 cache->length = key->offset;
1998 cache->used = btrfs_stack_block_group_used(bgi);
1999 cache->flags = btrfs_stack_block_group_flags(bgi);
Qu Wenruo9afc6642020-05-05 07:58:20 +08002000
Marcos Paulo de Souzae3e39c72020-08-21 11:54:44 -03002001 set_free_space_tree_thresholds(cache);
2002
Qu Wenruoffb9e0f2019-10-10 10:39:27 +08002003 if (need_clear) {
2004 /*
2005 * When we mount with old space cache, we need to
2006 * set BTRFS_DC_CLEAR and set dirty flag.
2007 *
2008 * a) Setting 'BTRFS_DC_CLEAR' makes sure that we
2009 * truncate the old free space cache inode and
2010 * setup a new one.
2011 * b) Setting 'dirty flag' makes sure that we flush
2012 * the new space cache info onto disk.
2013 */
2014 if (btrfs_test_opt(info, SPACE_CACHE))
2015 cache->disk_cache_state = BTRFS_DC_CLEAR;
2016 }
Qu Wenruoffb9e0f2019-10-10 10:39:27 +08002017 if (!mixed && ((cache->flags & BTRFS_BLOCK_GROUP_METADATA) &&
2018 (cache->flags & BTRFS_BLOCK_GROUP_DATA))) {
2019 btrfs_err(info,
2020"bg %llu is a mixed block group but filesystem hasn't enabled mixed block groups",
2021 cache->start);
2022 ret = -EINVAL;
2023 goto error;
2024 }
2025
Naohiro Aotaa94794d2021-02-04 19:21:51 +09002026 ret = btrfs_load_block_group_zone_info(cache, false);
Naohiro Aota08e11a32021-02-04 19:21:50 +09002027 if (ret) {
2028 btrfs_err(info, "zoned: failed to load zone info of bg %llu",
2029 cache->start);
2030 goto error;
2031 }
2032
Qu Wenruoffb9e0f2019-10-10 10:39:27 +08002033 /*
2034 * We need to exclude the super stripes now so that the space info has
2035 * super bytes accounted for, otherwise we'll think we have more space
2036 * than we actually do.
2037 */
2038 ret = exclude_super_stripes(cache);
2039 if (ret) {
2040 /* We may have excluded something, so call this just in case. */
2041 btrfs_free_excluded_extents(cache);
2042 goto error;
2043 }
2044
2045 /*
Naohiro Aota169e0da2021-02-04 19:21:52 +09002046 * For zoned filesystem, space after the allocation offset is the only
2047 * free space for a block group. So, we don't need any caching work.
2048 * btrfs_calc_zone_unusable() will set the amount of free space and
2049 * zone_unusable space.
2050 *
2051 * For regular filesystem, check for two cases, either we are full, and
2052 * therefore don't need to bother with the caching work since we won't
2053 * find any space, or we are empty, and we can just add all the space
2054 * in and be done with it. This saves us _a_lot_ of time, particularly
2055 * in the full case.
Qu Wenruoffb9e0f2019-10-10 10:39:27 +08002056 */
Naohiro Aota169e0da2021-02-04 19:21:52 +09002057 if (btrfs_is_zoned(info)) {
2058 btrfs_calc_zone_unusable(cache);
Naohiro Aotac46c4242021-08-19 21:19:09 +09002059 /* Should not have any excluded extents. Just in case, though. */
2060 btrfs_free_excluded_extents(cache);
Naohiro Aota169e0da2021-02-04 19:21:52 +09002061 } else if (cache->length == cache->used) {
Qu Wenruoffb9e0f2019-10-10 10:39:27 +08002062 cache->last_byte_to_unpin = (u64)-1;
2063 cache->cached = BTRFS_CACHE_FINISHED;
2064 btrfs_free_excluded_extents(cache);
2065 } else if (cache->used == 0) {
2066 cache->last_byte_to_unpin = (u64)-1;
2067 cache->cached = BTRFS_CACHE_FINISHED;
Qu Wenruo9afc6642020-05-05 07:58:20 +08002068 add_new_free_space(cache, cache->start,
2069 cache->start + cache->length);
Qu Wenruoffb9e0f2019-10-10 10:39:27 +08002070 btrfs_free_excluded_extents(cache);
2071 }
2072
2073 ret = btrfs_add_block_group_cache(info, cache);
2074 if (ret) {
2075 btrfs_remove_free_space_cache(cache);
2076 goto error;
2077 }
2078 trace_btrfs_add_block_group(info, cache, 0);
Qu Wenruo9afc6642020-05-05 07:58:20 +08002079 btrfs_update_space_info(info, cache->flags, cache->length,
Naohiro Aota169e0da2021-02-04 19:21:52 +09002080 cache->used, cache->bytes_super,
2081 cache->zone_unusable, &space_info);
Qu Wenruoffb9e0f2019-10-10 10:39:27 +08002082
2083 cache->space_info = space_info;
2084
2085 link_block_group(cache);
2086
2087 set_avail_alloc_bits(info, cache->flags);
Anand Jaina09f23c2021-08-24 13:27:42 +08002088 if (btrfs_chunk_writeable(info, cache->start)) {
2089 if (cache->used == 0) {
2090 ASSERT(list_empty(&cache->bg_list));
2091 if (btrfs_test_opt(info, DISCARD_ASYNC))
2092 btrfs_discard_queue_work(&info->discard_ctl, cache);
2093 else
2094 btrfs_mark_bg_unused(cache);
2095 }
2096 } else {
Qu Wenruoffb9e0f2019-10-10 10:39:27 +08002097 inc_block_group_ro(cache, 1);
Qu Wenruoffb9e0f2019-10-10 10:39:27 +08002098 }
Anand Jaina09f23c2021-08-24 13:27:42 +08002099
Qu Wenruoffb9e0f2019-10-10 10:39:27 +08002100 return 0;
2101error:
2102 btrfs_put_block_group(cache);
2103 return ret;
2104}
2105
Josef Bacik42437a62020-10-16 11:29:18 -04002106static int fill_dummy_bgs(struct btrfs_fs_info *fs_info)
2107{
2108 struct extent_map_tree *em_tree = &fs_info->mapping_tree;
2109 struct btrfs_space_info *space_info;
2110 struct rb_node *node;
2111 int ret = 0;
2112
2113 for (node = rb_first_cached(&em_tree->map); node; node = rb_next(node)) {
2114 struct extent_map *em;
2115 struct map_lookup *map;
2116 struct btrfs_block_group *bg;
2117
2118 em = rb_entry(node, struct extent_map, rb_node);
2119 map = em->map_lookup;
2120 bg = btrfs_create_block_group_cache(fs_info, em->start);
2121 if (!bg) {
2122 ret = -ENOMEM;
2123 break;
2124 }
2125
2126 /* Fill dummy cache as FULL */
2127 bg->length = em->len;
2128 bg->flags = map->type;
2129 bg->last_byte_to_unpin = (u64)-1;
2130 bg->cached = BTRFS_CACHE_FINISHED;
2131 bg->used = em->len;
2132 bg->flags = map->type;
2133 ret = btrfs_add_block_group_cache(fs_info, bg);
Qu Wenruo2b297262021-07-19 13:43:04 +08002134 /*
2135 * We may have some valid block group cache added already, in
2136 * that case we skip to the next one.
2137 */
2138 if (ret == -EEXIST) {
2139 ret = 0;
2140 btrfs_put_block_group(bg);
2141 continue;
2142 }
2143
Josef Bacik42437a62020-10-16 11:29:18 -04002144 if (ret) {
2145 btrfs_remove_free_space_cache(bg);
2146 btrfs_put_block_group(bg);
2147 break;
2148 }
Qu Wenruo2b297262021-07-19 13:43:04 +08002149
Josef Bacik42437a62020-10-16 11:29:18 -04002150 btrfs_update_space_info(fs_info, bg->flags, em->len, em->len,
Naohiro Aota169e0da2021-02-04 19:21:52 +09002151 0, 0, &space_info);
Josef Bacik42437a62020-10-16 11:29:18 -04002152 bg->space_info = space_info;
2153 link_block_group(bg);
2154
2155 set_avail_alloc_bits(fs_info, bg->flags);
2156 }
2157 if (!ret)
2158 btrfs_init_global_block_rsv(fs_info);
2159 return ret;
2160}
2161
Josef Bacik4358d9632019-06-20 15:37:57 -04002162int btrfs_read_block_groups(struct btrfs_fs_info *info)
2163{
Josef Bacikdfe8aec2021-11-05 16:45:36 -04002164 struct btrfs_root *root = btrfs_block_group_root(info);
Josef Bacik4358d9632019-06-20 15:37:57 -04002165 struct btrfs_path *path;
2166 int ret;
David Sterba32da53862019-10-29 19:20:18 +01002167 struct btrfs_block_group *cache;
Josef Bacik4358d9632019-06-20 15:37:57 -04002168 struct btrfs_space_info *space_info;
2169 struct btrfs_key key;
Josef Bacik4358d9632019-06-20 15:37:57 -04002170 int need_clear = 0;
2171 u64 cache_gen;
Josef Bacik4358d9632019-06-20 15:37:57 -04002172
Josef Bacikdfe8aec2021-11-05 16:45:36 -04002173 if (!root)
Josef Bacik42437a62020-10-16 11:29:18 -04002174 return fill_dummy_bgs(info);
2175
Josef Bacik4358d9632019-06-20 15:37:57 -04002176 key.objectid = 0;
2177 key.offset = 0;
2178 key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
2179 path = btrfs_alloc_path();
2180 if (!path)
2181 return -ENOMEM;
Josef Bacik4358d9632019-06-20 15:37:57 -04002182
2183 cache_gen = btrfs_super_cache_generation(info->super_copy);
2184 if (btrfs_test_opt(info, SPACE_CACHE) &&
2185 btrfs_super_generation(info->super_copy) != cache_gen)
2186 need_clear = 1;
2187 if (btrfs_test_opt(info, CLEAR_CACHE))
2188 need_clear = 1;
2189
2190 while (1) {
Johannes Thumshirn4afd2fe2021-02-04 19:21:44 +09002191 struct btrfs_block_group_item bgi;
2192 struct extent_buffer *leaf;
2193 int slot;
2194
Josef Bacik4358d9632019-06-20 15:37:57 -04002195 ret = find_first_block_group(info, path, &key);
2196 if (ret > 0)
2197 break;
2198 if (ret != 0)
2199 goto error;
2200
Johannes Thumshirn4afd2fe2021-02-04 19:21:44 +09002201 leaf = path->nodes[0];
2202 slot = path->slots[0];
2203
2204 read_extent_buffer(leaf, &bgi, btrfs_item_ptr_offset(leaf, slot),
2205 sizeof(bgi));
2206
2207 btrfs_item_key_to_cpu(leaf, &key, slot);
2208 btrfs_release_path(path);
2209 ret = read_one_block_group(info, &bgi, &key, need_clear);
Qu Wenruoffb9e0f2019-10-10 10:39:27 +08002210 if (ret < 0)
Josef Bacik4358d9632019-06-20 15:37:57 -04002211 goto error;
Qu Wenruoffb9e0f2019-10-10 10:39:27 +08002212 key.objectid += key.offset;
2213 key.offset = 0;
Josef Bacik4358d9632019-06-20 15:37:57 -04002214 }
Josef Bacik7837fa82020-10-14 17:00:51 -04002215 btrfs_release_path(path);
Josef Bacik4358d9632019-06-20 15:37:57 -04002216
Josef Bacik72804902020-09-01 17:40:37 -04002217 list_for_each_entry(space_info, &info->space_info, list) {
Josef Bacik49ea1122020-09-01 17:40:38 -04002218 int i;
2219
2220 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
2221 if (list_empty(&space_info->block_groups[i]))
2222 continue;
2223 cache = list_first_entry(&space_info->block_groups[i],
2224 struct btrfs_block_group,
2225 list);
2226 btrfs_sysfs_add_block_group_type(cache);
2227 }
2228
Josef Bacik4358d9632019-06-20 15:37:57 -04002229 if (!(btrfs_get_alloc_profile(info, space_info->flags) &
2230 (BTRFS_BLOCK_GROUP_RAID10 |
2231 BTRFS_BLOCK_GROUP_RAID1_MASK |
2232 BTRFS_BLOCK_GROUP_RAID56_MASK |
2233 BTRFS_BLOCK_GROUP_DUP)))
2234 continue;
2235 /*
2236 * Avoid allocating from un-mirrored block group if there are
2237 * mirrored block groups.
2238 */
2239 list_for_each_entry(cache,
2240 &space_info->block_groups[BTRFS_RAID_RAID0],
2241 list)
Josef Bacike11c0402019-06-20 15:38:07 -04002242 inc_block_group_ro(cache, 1);
Josef Bacik4358d9632019-06-20 15:37:57 -04002243 list_for_each_entry(cache,
2244 &space_info->block_groups[BTRFS_RAID_SINGLE],
2245 list)
Josef Bacike11c0402019-06-20 15:38:07 -04002246 inc_block_group_ro(cache, 1);
Josef Bacik4358d9632019-06-20 15:37:57 -04002247 }
2248
2249 btrfs_init_global_block_rsv(info);
2250 ret = check_chunk_block_group_mappings(info);
2251error:
2252 btrfs_free_path(path);
Qu Wenruo2b297262021-07-19 13:43:04 +08002253 /*
2254 * We've hit some error while reading the extent tree, and have
2255 * rescue=ibadroots mount option.
2256 * Try to fill the tree using dummy block groups so that the user can
2257 * continue to mount and grab their data.
2258 */
2259 if (ret && btrfs_test_opt(info, IGNOREBADROOTS))
2260 ret = fill_dummy_bgs(info);
Josef Bacik4358d9632019-06-20 15:37:57 -04002261 return ret;
2262}
2263
Filipe Manana79bd3712021-06-29 14:43:06 +01002264/*
2265 * This function, insert_block_group_item(), belongs to the phase 2 of chunk
2266 * allocation.
2267 *
2268 * See the comment at btrfs_chunk_alloc() for details about the chunk allocation
2269 * phases.
2270 */
Qu Wenruo97f47282020-05-05 07:58:22 +08002271static int insert_block_group_item(struct btrfs_trans_handle *trans,
2272 struct btrfs_block_group *block_group)
2273{
2274 struct btrfs_fs_info *fs_info = trans->fs_info;
2275 struct btrfs_block_group_item bgi;
Josef Bacikdfe8aec2021-11-05 16:45:36 -04002276 struct btrfs_root *root = btrfs_block_group_root(fs_info);
Qu Wenruo97f47282020-05-05 07:58:22 +08002277 struct btrfs_key key;
2278
2279 spin_lock(&block_group->lock);
2280 btrfs_set_stack_block_group_used(&bgi, block_group->used);
2281 btrfs_set_stack_block_group_chunk_objectid(&bgi,
2282 BTRFS_FIRST_CHUNK_TREE_OBJECTID);
2283 btrfs_set_stack_block_group_flags(&bgi, block_group->flags);
2284 key.objectid = block_group->start;
2285 key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
2286 key.offset = block_group->length;
2287 spin_unlock(&block_group->lock);
2288
Qu Wenruo97f47282020-05-05 07:58:22 +08002289 return btrfs_insert_item(trans, root, &key, &bgi, sizeof(bgi));
2290}
2291
Nikolay Borisov2eadb9e2021-07-05 12:29:19 +03002292static int insert_dev_extent(struct btrfs_trans_handle *trans,
2293 struct btrfs_device *device, u64 chunk_offset,
2294 u64 start, u64 num_bytes)
2295{
2296 struct btrfs_fs_info *fs_info = device->fs_info;
2297 struct btrfs_root *root = fs_info->dev_root;
2298 struct btrfs_path *path;
2299 struct btrfs_dev_extent *extent;
2300 struct extent_buffer *leaf;
2301 struct btrfs_key key;
2302 int ret;
2303
2304 WARN_ON(!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state));
2305 WARN_ON(test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state));
2306 path = btrfs_alloc_path();
2307 if (!path)
2308 return -ENOMEM;
2309
2310 key.objectid = device->devid;
2311 key.type = BTRFS_DEV_EXTENT_KEY;
2312 key.offset = start;
2313 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*extent));
2314 if (ret)
2315 goto out;
2316
2317 leaf = path->nodes[0];
2318 extent = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_extent);
2319 btrfs_set_dev_extent_chunk_tree(leaf, extent, BTRFS_CHUNK_TREE_OBJECTID);
2320 btrfs_set_dev_extent_chunk_objectid(leaf, extent,
2321 BTRFS_FIRST_CHUNK_TREE_OBJECTID);
2322 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
2323
2324 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
2325 btrfs_mark_buffer_dirty(leaf);
2326out:
2327 btrfs_free_path(path);
2328 return ret;
2329}
2330
2331/*
2332 * This function belongs to phase 2.
2333 *
2334 * See the comment at btrfs_chunk_alloc() for details about the chunk allocation
2335 * phases.
2336 */
2337static int insert_dev_extents(struct btrfs_trans_handle *trans,
2338 u64 chunk_offset, u64 chunk_size)
2339{
2340 struct btrfs_fs_info *fs_info = trans->fs_info;
2341 struct btrfs_device *device;
2342 struct extent_map *em;
2343 struct map_lookup *map;
2344 u64 dev_offset;
2345 u64 stripe_size;
2346 int i;
2347 int ret = 0;
2348
2349 em = btrfs_get_chunk_map(fs_info, chunk_offset, chunk_size);
2350 if (IS_ERR(em))
2351 return PTR_ERR(em);
2352
2353 map = em->map_lookup;
2354 stripe_size = em->orig_block_len;
2355
2356 /*
2357 * Take the device list mutex to prevent races with the final phase of
2358 * a device replace operation that replaces the device object associated
2359 * with the map's stripes, because the device object's id can change
2360 * at any time during that final phase of the device replace operation
2361 * (dev-replace.c:btrfs_dev_replace_finishing()), so we could grab the
2362 * replaced device and then see it with an ID of BTRFS_DEV_REPLACE_DEVID,
2363 * resulting in persisting a device extent item with such ID.
2364 */
2365 mutex_lock(&fs_info->fs_devices->device_list_mutex);
2366 for (i = 0; i < map->num_stripes; i++) {
2367 device = map->stripes[i].dev;
2368 dev_offset = map->stripes[i].physical;
2369
2370 ret = insert_dev_extent(trans, device, chunk_offset, dev_offset,
2371 stripe_size);
2372 if (ret)
2373 break;
2374 }
2375 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
2376
2377 free_extent_map(em);
2378 return ret;
2379}
2380
Filipe Manana79bd3712021-06-29 14:43:06 +01002381/*
2382 * This function, btrfs_create_pending_block_groups(), belongs to the phase 2 of
2383 * chunk allocation.
2384 *
2385 * See the comment at btrfs_chunk_alloc() for details about the chunk allocation
2386 * phases.
2387 */
Josef Bacik4358d9632019-06-20 15:37:57 -04002388void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans)
2389{
2390 struct btrfs_fs_info *fs_info = trans->fs_info;
David Sterba32da53862019-10-29 19:20:18 +01002391 struct btrfs_block_group *block_group;
Josef Bacik4358d9632019-06-20 15:37:57 -04002392 int ret = 0;
2393
Josef Bacik4358d9632019-06-20 15:37:57 -04002394 while (!list_empty(&trans->new_bgs)) {
Josef Bacik49ea1122020-09-01 17:40:38 -04002395 int index;
2396
Josef Bacik4358d9632019-06-20 15:37:57 -04002397 block_group = list_first_entry(&trans->new_bgs,
David Sterba32da53862019-10-29 19:20:18 +01002398 struct btrfs_block_group,
Josef Bacik4358d9632019-06-20 15:37:57 -04002399 bg_list);
2400 if (ret)
2401 goto next;
2402
Josef Bacik49ea1122020-09-01 17:40:38 -04002403 index = btrfs_bg_flags_to_raid_index(block_group->flags);
2404
Qu Wenruo97f47282020-05-05 07:58:22 +08002405 ret = insert_block_group_item(trans, block_group);
Josef Bacik4358d9632019-06-20 15:37:57 -04002406 if (ret)
2407 btrfs_abort_transaction(trans, ret);
Filipe Manana79bd3712021-06-29 14:43:06 +01002408 if (!block_group->chunk_item_inserted) {
2409 mutex_lock(&fs_info->chunk_mutex);
2410 ret = btrfs_chunk_alloc_add_chunk_item(trans, block_group);
2411 mutex_unlock(&fs_info->chunk_mutex);
2412 if (ret)
2413 btrfs_abort_transaction(trans, ret);
2414 }
Nikolay Borisov2eadb9e2021-07-05 12:29:19 +03002415 ret = insert_dev_extents(trans, block_group->start,
2416 block_group->length);
Josef Bacik4358d9632019-06-20 15:37:57 -04002417 if (ret)
2418 btrfs_abort_transaction(trans, ret);
2419 add_block_group_free_space(trans, block_group);
Josef Bacik49ea1122020-09-01 17:40:38 -04002420
2421 /*
2422 * If we restriped during balance, we may have added a new raid
2423 * type, so now add the sysfs entries when it is safe to do so.
2424 * We don't have to worry about locking here as it's handled in
2425 * btrfs_sysfs_add_block_group_type.
2426 */
2427 if (block_group->space_info->block_group_kobjs[index] == NULL)
2428 btrfs_sysfs_add_block_group_type(block_group);
2429
Josef Bacik4358d9632019-06-20 15:37:57 -04002430 /* Already aborted the transaction if it failed. */
2431next:
2432 btrfs_delayed_refs_rsv_release(fs_info, 1);
2433 list_del_init(&block_group->bg_list);
2434 }
2435 btrfs_trans_release_chunk_metadata(trans);
2436}
2437
Filipe Manana79bd3712021-06-29 14:43:06 +01002438struct btrfs_block_group *btrfs_make_block_group(struct btrfs_trans_handle *trans,
2439 u64 bytes_used, u64 type,
2440 u64 chunk_offset, u64 size)
Josef Bacik4358d9632019-06-20 15:37:57 -04002441{
2442 struct btrfs_fs_info *fs_info = trans->fs_info;
David Sterba32da53862019-10-29 19:20:18 +01002443 struct btrfs_block_group *cache;
Josef Bacik4358d9632019-06-20 15:37:57 -04002444 int ret;
2445
2446 btrfs_set_log_full_commit(trans);
2447
Qu Wenruo9afc6642020-05-05 07:58:20 +08002448 cache = btrfs_create_block_group_cache(fs_info, chunk_offset);
Josef Bacik4358d9632019-06-20 15:37:57 -04002449 if (!cache)
Filipe Manana79bd3712021-06-29 14:43:06 +01002450 return ERR_PTR(-ENOMEM);
Josef Bacik4358d9632019-06-20 15:37:57 -04002451
Qu Wenruo9afc6642020-05-05 07:58:20 +08002452 cache->length = size;
Marcos Paulo de Souzae3e39c72020-08-21 11:54:44 -03002453 set_free_space_tree_thresholds(cache);
David Sterbabf38be62019-10-23 18:48:11 +02002454 cache->used = bytes_used;
Josef Bacik4358d9632019-06-20 15:37:57 -04002455 cache->flags = type;
2456 cache->last_byte_to_unpin = (u64)-1;
2457 cache->cached = BTRFS_CACHE_FINISHED;
Boris Burkov997e3e22020-11-18 15:06:18 -08002458 if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE))
2459 cache->needs_free_space = 1;
Naohiro Aota08e11a32021-02-04 19:21:50 +09002460
Naohiro Aotaa94794d2021-02-04 19:21:51 +09002461 ret = btrfs_load_block_group_zone_info(cache, true);
Naohiro Aota08e11a32021-02-04 19:21:50 +09002462 if (ret) {
2463 btrfs_put_block_group(cache);
Filipe Manana79bd3712021-06-29 14:43:06 +01002464 return ERR_PTR(ret);
Naohiro Aota08e11a32021-02-04 19:21:50 +09002465 }
2466
Naohiro Aotaeb66a012021-08-19 21:19:20 +09002467 /*
2468 * New block group is likely to be used soon. Try to activate it now.
2469 * Failure is OK for now.
2470 */
2471 btrfs_zone_activate(cache);
2472
Josef Bacik4358d9632019-06-20 15:37:57 -04002473 ret = exclude_super_stripes(cache);
2474 if (ret) {
2475 /* We may have excluded something, so call this just in case */
2476 btrfs_free_excluded_extents(cache);
2477 btrfs_put_block_group(cache);
Filipe Manana79bd3712021-06-29 14:43:06 +01002478 return ERR_PTR(ret);
Josef Bacik4358d9632019-06-20 15:37:57 -04002479 }
2480
2481 add_new_free_space(cache, chunk_offset, chunk_offset + size);
2482
2483 btrfs_free_excluded_extents(cache);
2484
2485#ifdef CONFIG_BTRFS_DEBUG
2486 if (btrfs_should_fragment_free_space(cache)) {
2487 u64 new_bytes_used = size - bytes_used;
2488
2489 bytes_used += new_bytes_used >> 1;
Josef Bacike11c0402019-06-20 15:38:07 -04002490 fragment_free_space(cache);
Josef Bacik4358d9632019-06-20 15:37:57 -04002491 }
2492#endif
2493 /*
2494 * Ensure the corresponding space_info object is created and
2495 * assigned to our block group. We want our bg to be added to the rbtree
2496 * with its ->space_info set.
2497 */
2498 cache->space_info = btrfs_find_space_info(fs_info, cache->flags);
2499 ASSERT(cache->space_info);
2500
2501 ret = btrfs_add_block_group_cache(fs_info, cache);
2502 if (ret) {
2503 btrfs_remove_free_space_cache(cache);
2504 btrfs_put_block_group(cache);
Filipe Manana79bd3712021-06-29 14:43:06 +01002505 return ERR_PTR(ret);
Josef Bacik4358d9632019-06-20 15:37:57 -04002506 }
2507
2508 /*
2509 * Now that our block group has its ->space_info set and is inserted in
2510 * the rbtree, update the space info's counters.
2511 */
2512 trace_btrfs_add_block_group(fs_info, cache, 1);
2513 btrfs_update_space_info(fs_info, cache->flags, size, bytes_used,
Naohiro Aota98173252021-08-19 21:19:10 +09002514 cache->bytes_super, cache->zone_unusable,
2515 &cache->space_info);
Josef Bacik4358d9632019-06-20 15:37:57 -04002516 btrfs_update_global_block_rsv(fs_info);
2517
2518 link_block_group(cache);
2519
2520 list_add_tail(&cache->bg_list, &trans->new_bgs);
2521 trans->delayed_ref_updates++;
2522 btrfs_update_delayed_refs_rsv(trans);
2523
2524 set_avail_alloc_bits(fs_info, type);
Filipe Manana79bd3712021-06-29 14:43:06 +01002525 return cache;
Josef Bacik4358d9632019-06-20 15:37:57 -04002526}
Josef Bacik26ce2092019-06-20 15:37:59 -04002527
Qu Wenruob12de522019-11-15 10:09:00 +08002528/*
2529 * Mark one block group RO, can be called several times for the same block
2530 * group.
2531 *
2532 * @cache: the destination block group
2533 * @do_chunk_alloc: whether need to do chunk pre-allocation, this is to
2534 * ensure we still have some free space after marking this
2535 * block group RO.
2536 */
2537int btrfs_inc_block_group_ro(struct btrfs_block_group *cache,
2538 bool do_chunk_alloc)
Josef Bacik26ce2092019-06-20 15:37:59 -04002539{
2540 struct btrfs_fs_info *fs_info = cache->fs_info;
2541 struct btrfs_trans_handle *trans;
Josef Bacikdfe8aec2021-11-05 16:45:36 -04002542 struct btrfs_root *root = btrfs_block_group_root(fs_info);
Josef Bacik26ce2092019-06-20 15:37:59 -04002543 u64 alloc_flags;
2544 int ret;
Nikolay Borisovb6e9f162021-02-17 15:12:50 +02002545 bool dirty_bg_running;
Josef Bacik26ce2092019-06-20 15:37:59 -04002546
Nikolay Borisovb6e9f162021-02-17 15:12:50 +02002547 do {
Josef Bacikdfe8aec2021-11-05 16:45:36 -04002548 trans = btrfs_join_transaction(root);
Nikolay Borisovb6e9f162021-02-17 15:12:50 +02002549 if (IS_ERR(trans))
2550 return PTR_ERR(trans);
Josef Bacik26ce2092019-06-20 15:37:59 -04002551
Nikolay Borisovb6e9f162021-02-17 15:12:50 +02002552 dirty_bg_running = false;
Josef Bacik26ce2092019-06-20 15:37:59 -04002553
Nikolay Borisovb6e9f162021-02-17 15:12:50 +02002554 /*
2555 * We're not allowed to set block groups readonly after the dirty
2556 * block group cache has started writing. If it already started,
2557 * back off and let this transaction commit.
2558 */
2559 mutex_lock(&fs_info->ro_block_group_mutex);
2560 if (test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &trans->transaction->flags)) {
2561 u64 transid = trans->transid;
Josef Bacik26ce2092019-06-20 15:37:59 -04002562
Nikolay Borisovb6e9f162021-02-17 15:12:50 +02002563 mutex_unlock(&fs_info->ro_block_group_mutex);
2564 btrfs_end_transaction(trans);
2565
2566 ret = btrfs_wait_for_commit(fs_info, transid);
2567 if (ret)
2568 return ret;
2569 dirty_bg_running = true;
2570 }
2571 } while (dirty_bg_running);
Josef Bacik26ce2092019-06-20 15:37:59 -04002572
Qu Wenruob12de522019-11-15 10:09:00 +08002573 if (do_chunk_alloc) {
Josef Bacik26ce2092019-06-20 15:37:59 -04002574 /*
Qu Wenruob12de522019-11-15 10:09:00 +08002575 * If we are changing raid levels, try to allocate a
2576 * corresponding block group with the new raid level.
Josef Bacik26ce2092019-06-20 15:37:59 -04002577 */
Josef Bacik349e1202020-07-21 10:48:45 -04002578 alloc_flags = btrfs_get_alloc_profile(fs_info, cache->flags);
Qu Wenruob12de522019-11-15 10:09:00 +08002579 if (alloc_flags != cache->flags) {
2580 ret = btrfs_chunk_alloc(trans, alloc_flags,
2581 CHUNK_ALLOC_FORCE);
2582 /*
2583 * ENOSPC is allowed here, we may have enough space
2584 * already allocated at the new raid level to carry on
2585 */
2586 if (ret == -ENOSPC)
2587 ret = 0;
2588 if (ret < 0)
2589 goto out;
2590 }
Josef Bacik26ce2092019-06-20 15:37:59 -04002591 }
2592
Josef Bacika7a63acc2020-01-17 09:07:38 -05002593 ret = inc_block_group_ro(cache, 0);
Filipe Manana195a49e2021-02-05 12:55:37 +00002594 if (!do_chunk_alloc || ret == -ETXTBSY)
Qu Wenruob12de522019-11-15 10:09:00 +08002595 goto unlock_out;
Josef Bacik26ce2092019-06-20 15:37:59 -04002596 if (!ret)
2597 goto out;
2598 alloc_flags = btrfs_get_alloc_profile(fs_info, cache->space_info->flags);
2599 ret = btrfs_chunk_alloc(trans, alloc_flags, CHUNK_ALLOC_FORCE);
2600 if (ret < 0)
2601 goto out;
Josef Bacike11c0402019-06-20 15:38:07 -04002602 ret = inc_block_group_ro(cache, 0);
Filipe Manana195a49e2021-02-05 12:55:37 +00002603 if (ret == -ETXTBSY)
2604 goto unlock_out;
Josef Bacik26ce2092019-06-20 15:37:59 -04002605out:
2606 if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
Josef Bacik349e1202020-07-21 10:48:45 -04002607 alloc_flags = btrfs_get_alloc_profile(fs_info, cache->flags);
Josef Bacik26ce2092019-06-20 15:37:59 -04002608 mutex_lock(&fs_info->chunk_mutex);
2609 check_system_chunk(trans, alloc_flags);
2610 mutex_unlock(&fs_info->chunk_mutex);
2611 }
Qu Wenruob12de522019-11-15 10:09:00 +08002612unlock_out:
Josef Bacik26ce2092019-06-20 15:37:59 -04002613 mutex_unlock(&fs_info->ro_block_group_mutex);
2614
2615 btrfs_end_transaction(trans);
2616 return ret;
2617}
2618
David Sterba32da53862019-10-29 19:20:18 +01002619void btrfs_dec_block_group_ro(struct btrfs_block_group *cache)
Josef Bacik26ce2092019-06-20 15:37:59 -04002620{
2621 struct btrfs_space_info *sinfo = cache->space_info;
2622 u64 num_bytes;
2623
2624 BUG_ON(!cache->ro);
2625
2626 spin_lock(&sinfo->lock);
2627 spin_lock(&cache->lock);
2628 if (!--cache->ro) {
Naohiro Aota169e0da2021-02-04 19:21:52 +09002629 if (btrfs_is_zoned(cache->fs_info)) {
2630 /* Migrate zone_unusable bytes back */
Naohiro Aota98173252021-08-19 21:19:10 +09002631 cache->zone_unusable =
2632 (cache->alloc_offset - cache->used) +
2633 (cache->length - cache->zone_capacity);
Naohiro Aota169e0da2021-02-04 19:21:52 +09002634 sinfo->bytes_zone_unusable += cache->zone_unusable;
2635 sinfo->bytes_readonly -= cache->zone_unusable;
2636 }
Naohiro Aotaf9f28e52021-06-17 13:56:18 +09002637 num_bytes = cache->length - cache->reserved -
2638 cache->pinned - cache->bytes_super -
2639 cache->zone_unusable - cache->used;
2640 sinfo->bytes_readonly -= num_bytes;
Josef Bacik26ce2092019-06-20 15:37:59 -04002641 list_del_init(&cache->ro_list);
2642 }
2643 spin_unlock(&cache->lock);
2644 spin_unlock(&sinfo->lock);
2645}
Josef Bacik77745c02019-06-20 15:38:00 -04002646
Qu Wenruo3be4d8e2020-05-05 07:58:23 +08002647static int update_block_group_item(struct btrfs_trans_handle *trans,
2648 struct btrfs_path *path,
2649 struct btrfs_block_group *cache)
Josef Bacik77745c02019-06-20 15:38:00 -04002650{
2651 struct btrfs_fs_info *fs_info = trans->fs_info;
2652 int ret;
Josef Bacikdfe8aec2021-11-05 16:45:36 -04002653 struct btrfs_root *root = btrfs_block_group_root(fs_info);
Josef Bacik77745c02019-06-20 15:38:00 -04002654 unsigned long bi;
2655 struct extent_buffer *leaf;
David Sterbabf38be62019-10-23 18:48:11 +02002656 struct btrfs_block_group_item bgi;
David Sterbab3470b52019-10-23 18:48:22 +02002657 struct btrfs_key key;
Josef Bacik77745c02019-06-20 15:38:00 -04002658
David Sterbab3470b52019-10-23 18:48:22 +02002659 key.objectid = cache->start;
2660 key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
2661 key.offset = cache->length;
2662
Qu Wenruo3be4d8e2020-05-05 07:58:23 +08002663 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Josef Bacik77745c02019-06-20 15:38:00 -04002664 if (ret) {
2665 if (ret > 0)
2666 ret = -ENOENT;
2667 goto fail;
2668 }
2669
2670 leaf = path->nodes[0];
2671 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
David Sterbade0dc452019-10-23 18:48:18 +02002672 btrfs_set_stack_block_group_used(&bgi, cache->used);
2673 btrfs_set_stack_block_group_chunk_objectid(&bgi,
David Sterba3d976382019-10-23 18:48:15 +02002674 BTRFS_FIRST_CHUNK_TREE_OBJECTID);
David Sterbade0dc452019-10-23 18:48:18 +02002675 btrfs_set_stack_block_group_flags(&bgi, cache->flags);
David Sterbabf38be62019-10-23 18:48:11 +02002676 write_extent_buffer(leaf, &bgi, bi, sizeof(bgi));
Josef Bacik77745c02019-06-20 15:38:00 -04002677 btrfs_mark_buffer_dirty(leaf);
2678fail:
2679 btrfs_release_path(path);
2680 return ret;
2681
2682}
2683
David Sterba32da53862019-10-29 19:20:18 +01002684static int cache_save_setup(struct btrfs_block_group *block_group,
Josef Bacik77745c02019-06-20 15:38:00 -04002685 struct btrfs_trans_handle *trans,
2686 struct btrfs_path *path)
2687{
2688 struct btrfs_fs_info *fs_info = block_group->fs_info;
2689 struct btrfs_root *root = fs_info->tree_root;
2690 struct inode *inode = NULL;
2691 struct extent_changeset *data_reserved = NULL;
2692 u64 alloc_hint = 0;
2693 int dcs = BTRFS_DC_ERROR;
Qu Wenruo0044ae12021-04-13 14:23:14 +08002694 u64 cache_size = 0;
Josef Bacik77745c02019-06-20 15:38:00 -04002695 int retries = 0;
2696 int ret = 0;
2697
Boris Burkovaf456a22020-11-18 15:06:26 -08002698 if (!btrfs_test_opt(fs_info, SPACE_CACHE))
2699 return 0;
2700
Josef Bacik77745c02019-06-20 15:38:00 -04002701 /*
2702 * If this block group is smaller than 100 megs don't bother caching the
2703 * block group.
2704 */
David Sterbab3470b52019-10-23 18:48:22 +02002705 if (block_group->length < (100 * SZ_1M)) {
Josef Bacik77745c02019-06-20 15:38:00 -04002706 spin_lock(&block_group->lock);
2707 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
2708 spin_unlock(&block_group->lock);
2709 return 0;
2710 }
2711
David Sterbabf31f872020-02-05 17:34:34 +01002712 if (TRANS_ABORTED(trans))
Josef Bacik77745c02019-06-20 15:38:00 -04002713 return 0;
2714again:
2715 inode = lookup_free_space_inode(block_group, path);
2716 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
2717 ret = PTR_ERR(inode);
2718 btrfs_release_path(path);
2719 goto out;
2720 }
2721
2722 if (IS_ERR(inode)) {
2723 BUG_ON(retries);
2724 retries++;
2725
2726 if (block_group->ro)
2727 goto out_free;
2728
2729 ret = create_free_space_inode(trans, block_group, path);
2730 if (ret)
2731 goto out_free;
2732 goto again;
2733 }
2734
2735 /*
2736 * We want to set the generation to 0, that way if anything goes wrong
2737 * from here on out we know not to trust this cache when we load up next
2738 * time.
2739 */
2740 BTRFS_I(inode)->generation = 0;
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02002741 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Josef Bacik77745c02019-06-20 15:38:00 -04002742 if (ret) {
2743 /*
2744 * So theoretically we could recover from this, simply set the
2745 * super cache generation to 0 so we know to invalidate the
2746 * cache, but then we'd have to keep track of the block groups
2747 * that fail this way so we know we _have_ to reset this cache
2748 * before the next commit or risk reading stale cache. So to
2749 * limit our exposure to horrible edge cases lets just abort the
2750 * transaction, this only happens in really bad situations
2751 * anyway.
2752 */
2753 btrfs_abort_transaction(trans, ret);
2754 goto out_put;
2755 }
2756 WARN_ON(ret);
2757
2758 /* We've already setup this transaction, go ahead and exit */
2759 if (block_group->cache_generation == trans->transid &&
2760 i_size_read(inode)) {
2761 dcs = BTRFS_DC_SETUP;
2762 goto out_put;
2763 }
2764
2765 if (i_size_read(inode) > 0) {
2766 ret = btrfs_check_trunc_cache_free_space(fs_info,
2767 &fs_info->global_block_rsv);
2768 if (ret)
2769 goto out_put;
2770
2771 ret = btrfs_truncate_free_space_cache(trans, NULL, inode);
2772 if (ret)
2773 goto out_put;
2774 }
2775
2776 spin_lock(&block_group->lock);
2777 if (block_group->cached != BTRFS_CACHE_FINISHED ||
2778 !btrfs_test_opt(fs_info, SPACE_CACHE)) {
2779 /*
2780 * don't bother trying to write stuff out _if_
2781 * a) we're not cached,
2782 * b) we're with nospace_cache mount option,
2783 * c) we're with v2 space_cache (FREE_SPACE_TREE).
2784 */
2785 dcs = BTRFS_DC_WRITTEN;
2786 spin_unlock(&block_group->lock);
2787 goto out_put;
2788 }
2789 spin_unlock(&block_group->lock);
2790
2791 /*
2792 * We hit an ENOSPC when setting up the cache in this transaction, just
2793 * skip doing the setup, we've already cleared the cache so we're safe.
2794 */
2795 if (test_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags)) {
2796 ret = -ENOSPC;
2797 goto out_put;
2798 }
2799
2800 /*
2801 * Try to preallocate enough space based on how big the block group is.
2802 * Keep in mind this has to include any pinned space which could end up
2803 * taking up quite a bit since it's not folded into the other space
2804 * cache.
2805 */
Qu Wenruo0044ae12021-04-13 14:23:14 +08002806 cache_size = div_u64(block_group->length, SZ_256M);
2807 if (!cache_size)
2808 cache_size = 1;
Josef Bacik77745c02019-06-20 15:38:00 -04002809
Qu Wenruo0044ae12021-04-13 14:23:14 +08002810 cache_size *= 16;
2811 cache_size *= fs_info->sectorsize;
Josef Bacik77745c02019-06-20 15:38:00 -04002812
Nikolay Borisov36ea6f32020-06-03 08:55:41 +03002813 ret = btrfs_check_data_free_space(BTRFS_I(inode), &data_reserved, 0,
Qu Wenruo0044ae12021-04-13 14:23:14 +08002814 cache_size);
Josef Bacik77745c02019-06-20 15:38:00 -04002815 if (ret)
2816 goto out_put;
2817
Qu Wenruo0044ae12021-04-13 14:23:14 +08002818 ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, cache_size,
2819 cache_size, cache_size,
Josef Bacik77745c02019-06-20 15:38:00 -04002820 &alloc_hint);
2821 /*
2822 * Our cache requires contiguous chunks so that we don't modify a bunch
2823 * of metadata or split extents when writing the cache out, which means
2824 * we can enospc if we are heavily fragmented in addition to just normal
2825 * out of space conditions. So if we hit this just skip setting up any
2826 * other block groups for this transaction, maybe we'll unpin enough
2827 * space the next time around.
2828 */
2829 if (!ret)
2830 dcs = BTRFS_DC_SETUP;
2831 else if (ret == -ENOSPC)
2832 set_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags);
2833
2834out_put:
2835 iput(inode);
2836out_free:
2837 btrfs_release_path(path);
2838out:
2839 spin_lock(&block_group->lock);
2840 if (!ret && dcs == BTRFS_DC_SETUP)
2841 block_group->cache_generation = trans->transid;
2842 block_group->disk_cache_state = dcs;
2843 spin_unlock(&block_group->lock);
2844
2845 extent_changeset_free(data_reserved);
2846 return ret;
2847}
2848
2849int btrfs_setup_space_cache(struct btrfs_trans_handle *trans)
2850{
2851 struct btrfs_fs_info *fs_info = trans->fs_info;
David Sterba32da53862019-10-29 19:20:18 +01002852 struct btrfs_block_group *cache, *tmp;
Josef Bacik77745c02019-06-20 15:38:00 -04002853 struct btrfs_transaction *cur_trans = trans->transaction;
2854 struct btrfs_path *path;
2855
2856 if (list_empty(&cur_trans->dirty_bgs) ||
2857 !btrfs_test_opt(fs_info, SPACE_CACHE))
2858 return 0;
2859
2860 path = btrfs_alloc_path();
2861 if (!path)
2862 return -ENOMEM;
2863
2864 /* Could add new block groups, use _safe just in case */
2865 list_for_each_entry_safe(cache, tmp, &cur_trans->dirty_bgs,
2866 dirty_list) {
2867 if (cache->disk_cache_state == BTRFS_DC_CLEAR)
2868 cache_save_setup(cache, trans, path);
2869 }
2870
2871 btrfs_free_path(path);
2872 return 0;
2873}
2874
2875/*
2876 * Transaction commit does final block group cache writeback during a critical
2877 * section where nothing is allowed to change the FS. This is required in
2878 * order for the cache to actually match the block group, but can introduce a
2879 * lot of latency into the commit.
2880 *
2881 * So, btrfs_start_dirty_block_groups is here to kick off block group cache IO.
2882 * There's a chance we'll have to redo some of it if the block group changes
2883 * again during the commit, but it greatly reduces the commit latency by
2884 * getting rid of the easy block groups while we're still allowing others to
2885 * join the commit.
2886 */
2887int btrfs_start_dirty_block_groups(struct btrfs_trans_handle *trans)
2888{
2889 struct btrfs_fs_info *fs_info = trans->fs_info;
David Sterba32da53862019-10-29 19:20:18 +01002890 struct btrfs_block_group *cache;
Josef Bacik77745c02019-06-20 15:38:00 -04002891 struct btrfs_transaction *cur_trans = trans->transaction;
2892 int ret = 0;
2893 int should_put;
2894 struct btrfs_path *path = NULL;
2895 LIST_HEAD(dirty);
2896 struct list_head *io = &cur_trans->io_bgs;
2897 int num_started = 0;
2898 int loops = 0;
2899
2900 spin_lock(&cur_trans->dirty_bgs_lock);
2901 if (list_empty(&cur_trans->dirty_bgs)) {
2902 spin_unlock(&cur_trans->dirty_bgs_lock);
2903 return 0;
2904 }
2905 list_splice_init(&cur_trans->dirty_bgs, &dirty);
2906 spin_unlock(&cur_trans->dirty_bgs_lock);
2907
2908again:
2909 /* Make sure all the block groups on our dirty list actually exist */
2910 btrfs_create_pending_block_groups(trans);
2911
2912 if (!path) {
2913 path = btrfs_alloc_path();
Josef Bacik938fcbf2021-01-14 14:02:43 -05002914 if (!path) {
2915 ret = -ENOMEM;
2916 goto out;
2917 }
Josef Bacik77745c02019-06-20 15:38:00 -04002918 }
2919
2920 /*
2921 * cache_write_mutex is here only to save us from balance or automatic
2922 * removal of empty block groups deleting this block group while we are
2923 * writing out the cache
2924 */
2925 mutex_lock(&trans->transaction->cache_write_mutex);
2926 while (!list_empty(&dirty)) {
2927 bool drop_reserve = true;
2928
David Sterba32da53862019-10-29 19:20:18 +01002929 cache = list_first_entry(&dirty, struct btrfs_block_group,
Josef Bacik77745c02019-06-20 15:38:00 -04002930 dirty_list);
2931 /*
2932 * This can happen if something re-dirties a block group that
2933 * is already under IO. Just wait for it to finish and then do
2934 * it all again
2935 */
2936 if (!list_empty(&cache->io_list)) {
2937 list_del_init(&cache->io_list);
2938 btrfs_wait_cache_io(trans, cache, path);
2939 btrfs_put_block_group(cache);
2940 }
2941
2942
2943 /*
2944 * btrfs_wait_cache_io uses the cache->dirty_list to decide if
2945 * it should update the cache_state. Don't delete until after
2946 * we wait.
2947 *
2948 * Since we're not running in the commit critical section
2949 * we need the dirty_bgs_lock to protect from update_block_group
2950 */
2951 spin_lock(&cur_trans->dirty_bgs_lock);
2952 list_del_init(&cache->dirty_list);
2953 spin_unlock(&cur_trans->dirty_bgs_lock);
2954
2955 should_put = 1;
2956
2957 cache_save_setup(cache, trans, path);
2958
2959 if (cache->disk_cache_state == BTRFS_DC_SETUP) {
2960 cache->io_ctl.inode = NULL;
2961 ret = btrfs_write_out_cache(trans, cache, path);
2962 if (ret == 0 && cache->io_ctl.inode) {
2963 num_started++;
2964 should_put = 0;
2965
2966 /*
2967 * The cache_write_mutex is protecting the
2968 * io_list, also refer to the definition of
2969 * btrfs_transaction::io_bgs for more details
2970 */
2971 list_add_tail(&cache->io_list, io);
2972 } else {
2973 /*
2974 * If we failed to write the cache, the
2975 * generation will be bad and life goes on
2976 */
2977 ret = 0;
2978 }
2979 }
2980 if (!ret) {
Qu Wenruo3be4d8e2020-05-05 07:58:23 +08002981 ret = update_block_group_item(trans, path, cache);
Josef Bacik77745c02019-06-20 15:38:00 -04002982 /*
2983 * Our block group might still be attached to the list
2984 * of new block groups in the transaction handle of some
2985 * other task (struct btrfs_trans_handle->new_bgs). This
2986 * means its block group item isn't yet in the extent
2987 * tree. If this happens ignore the error, as we will
2988 * try again later in the critical section of the
2989 * transaction commit.
2990 */
2991 if (ret == -ENOENT) {
2992 ret = 0;
2993 spin_lock(&cur_trans->dirty_bgs_lock);
2994 if (list_empty(&cache->dirty_list)) {
2995 list_add_tail(&cache->dirty_list,
2996 &cur_trans->dirty_bgs);
2997 btrfs_get_block_group(cache);
2998 drop_reserve = false;
2999 }
3000 spin_unlock(&cur_trans->dirty_bgs_lock);
3001 } else if (ret) {
3002 btrfs_abort_transaction(trans, ret);
3003 }
3004 }
3005
3006 /* If it's not on the io list, we need to put the block group */
3007 if (should_put)
3008 btrfs_put_block_group(cache);
3009 if (drop_reserve)
3010 btrfs_delayed_refs_rsv_release(fs_info, 1);
Josef Bacik77745c02019-06-20 15:38:00 -04003011 /*
3012 * Avoid blocking other tasks for too long. It might even save
3013 * us from writing caches for block groups that are going to be
3014 * removed.
3015 */
3016 mutex_unlock(&trans->transaction->cache_write_mutex);
Josef Bacik938fcbf2021-01-14 14:02:43 -05003017 if (ret)
3018 goto out;
Josef Bacik77745c02019-06-20 15:38:00 -04003019 mutex_lock(&trans->transaction->cache_write_mutex);
3020 }
3021 mutex_unlock(&trans->transaction->cache_write_mutex);
3022
3023 /*
3024 * Go through delayed refs for all the stuff we've just kicked off
3025 * and then loop back (just once)
3026 */
Josef Bacik34d1eb02020-12-16 11:22:17 -05003027 if (!ret)
3028 ret = btrfs_run_delayed_refs(trans, 0);
Josef Bacik77745c02019-06-20 15:38:00 -04003029 if (!ret && loops == 0) {
3030 loops++;
3031 spin_lock(&cur_trans->dirty_bgs_lock);
3032 list_splice_init(&cur_trans->dirty_bgs, &dirty);
3033 /*
3034 * dirty_bgs_lock protects us from concurrent block group
3035 * deletes too (not just cache_write_mutex).
3036 */
3037 if (!list_empty(&dirty)) {
3038 spin_unlock(&cur_trans->dirty_bgs_lock);
3039 goto again;
3040 }
3041 spin_unlock(&cur_trans->dirty_bgs_lock);
Josef Bacik938fcbf2021-01-14 14:02:43 -05003042 }
3043out:
3044 if (ret < 0) {
3045 spin_lock(&cur_trans->dirty_bgs_lock);
3046 list_splice_init(&dirty, &cur_trans->dirty_bgs);
3047 spin_unlock(&cur_trans->dirty_bgs_lock);
Josef Bacik77745c02019-06-20 15:38:00 -04003048 btrfs_cleanup_dirty_bgs(cur_trans, fs_info);
3049 }
3050
3051 btrfs_free_path(path);
3052 return ret;
3053}
3054
3055int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans)
3056{
3057 struct btrfs_fs_info *fs_info = trans->fs_info;
David Sterba32da53862019-10-29 19:20:18 +01003058 struct btrfs_block_group *cache;
Josef Bacik77745c02019-06-20 15:38:00 -04003059 struct btrfs_transaction *cur_trans = trans->transaction;
3060 int ret = 0;
3061 int should_put;
3062 struct btrfs_path *path;
3063 struct list_head *io = &cur_trans->io_bgs;
3064 int num_started = 0;
3065
3066 path = btrfs_alloc_path();
3067 if (!path)
3068 return -ENOMEM;
3069
3070 /*
3071 * Even though we are in the critical section of the transaction commit,
3072 * we can still have concurrent tasks adding elements to this
3073 * transaction's list of dirty block groups. These tasks correspond to
3074 * endio free space workers started when writeback finishes for a
3075 * space cache, which run inode.c:btrfs_finish_ordered_io(), and can
3076 * allocate new block groups as a result of COWing nodes of the root
3077 * tree when updating the free space inode. The writeback for the space
3078 * caches is triggered by an earlier call to
3079 * btrfs_start_dirty_block_groups() and iterations of the following
3080 * loop.
3081 * Also we want to do the cache_save_setup first and then run the
3082 * delayed refs to make sure we have the best chance at doing this all
3083 * in one shot.
3084 */
3085 spin_lock(&cur_trans->dirty_bgs_lock);
3086 while (!list_empty(&cur_trans->dirty_bgs)) {
3087 cache = list_first_entry(&cur_trans->dirty_bgs,
David Sterba32da53862019-10-29 19:20:18 +01003088 struct btrfs_block_group,
Josef Bacik77745c02019-06-20 15:38:00 -04003089 dirty_list);
3090
3091 /*
3092 * This can happen if cache_save_setup re-dirties a block group
3093 * that is already under IO. Just wait for it to finish and
3094 * then do it all again
3095 */
3096 if (!list_empty(&cache->io_list)) {
3097 spin_unlock(&cur_trans->dirty_bgs_lock);
3098 list_del_init(&cache->io_list);
3099 btrfs_wait_cache_io(trans, cache, path);
3100 btrfs_put_block_group(cache);
3101 spin_lock(&cur_trans->dirty_bgs_lock);
3102 }
3103
3104 /*
3105 * Don't remove from the dirty list until after we've waited on
3106 * any pending IO
3107 */
3108 list_del_init(&cache->dirty_list);
3109 spin_unlock(&cur_trans->dirty_bgs_lock);
3110 should_put = 1;
3111
3112 cache_save_setup(cache, trans, path);
3113
3114 if (!ret)
3115 ret = btrfs_run_delayed_refs(trans,
3116 (unsigned long) -1);
3117
3118 if (!ret && cache->disk_cache_state == BTRFS_DC_SETUP) {
3119 cache->io_ctl.inode = NULL;
3120 ret = btrfs_write_out_cache(trans, cache, path);
3121 if (ret == 0 && cache->io_ctl.inode) {
3122 num_started++;
3123 should_put = 0;
3124 list_add_tail(&cache->io_list, io);
3125 } else {
3126 /*
3127 * If we failed to write the cache, the
3128 * generation will be bad and life goes on
3129 */
3130 ret = 0;
3131 }
3132 }
3133 if (!ret) {
Qu Wenruo3be4d8e2020-05-05 07:58:23 +08003134 ret = update_block_group_item(trans, path, cache);
Josef Bacik77745c02019-06-20 15:38:00 -04003135 /*
3136 * One of the free space endio workers might have
3137 * created a new block group while updating a free space
3138 * cache's inode (at inode.c:btrfs_finish_ordered_io())
3139 * and hasn't released its transaction handle yet, in
3140 * which case the new block group is still attached to
3141 * its transaction handle and its creation has not
3142 * finished yet (no block group item in the extent tree
3143 * yet, etc). If this is the case, wait for all free
3144 * space endio workers to finish and retry. This is a
Randy Dunlap260db432020-08-04 19:48:34 -07003145 * very rare case so no need for a more efficient and
Josef Bacik77745c02019-06-20 15:38:00 -04003146 * complex approach.
3147 */
3148 if (ret == -ENOENT) {
3149 wait_event(cur_trans->writer_wait,
3150 atomic_read(&cur_trans->num_writers) == 1);
Qu Wenruo3be4d8e2020-05-05 07:58:23 +08003151 ret = update_block_group_item(trans, path, cache);
Josef Bacik77745c02019-06-20 15:38:00 -04003152 }
3153 if (ret)
3154 btrfs_abort_transaction(trans, ret);
3155 }
3156
3157 /* If its not on the io list, we need to put the block group */
3158 if (should_put)
3159 btrfs_put_block_group(cache);
3160 btrfs_delayed_refs_rsv_release(fs_info, 1);
3161 spin_lock(&cur_trans->dirty_bgs_lock);
3162 }
3163 spin_unlock(&cur_trans->dirty_bgs_lock);
3164
3165 /*
3166 * Refer to the definition of io_bgs member for details why it's safe
3167 * to use it without any locking
3168 */
3169 while (!list_empty(io)) {
David Sterba32da53862019-10-29 19:20:18 +01003170 cache = list_first_entry(io, struct btrfs_block_group,
Josef Bacik77745c02019-06-20 15:38:00 -04003171 io_list);
3172 list_del_init(&cache->io_list);
3173 btrfs_wait_cache_io(trans, cache, path);
3174 btrfs_put_block_group(cache);
3175 }
3176
3177 btrfs_free_path(path);
3178 return ret;
3179}
Josef Bacik606d1bf2019-06-20 15:38:02 -04003180
3181int btrfs_update_block_group(struct btrfs_trans_handle *trans,
Anand Jain11b66fa2021-10-13 14:05:14 +08003182 u64 bytenr, u64 num_bytes, bool alloc)
Josef Bacik606d1bf2019-06-20 15:38:02 -04003183{
3184 struct btrfs_fs_info *info = trans->fs_info;
David Sterba32da53862019-10-29 19:20:18 +01003185 struct btrfs_block_group *cache = NULL;
Josef Bacik606d1bf2019-06-20 15:38:02 -04003186 u64 total = num_bytes;
3187 u64 old_val;
3188 u64 byte_in_group;
3189 int factor;
3190 int ret = 0;
3191
3192 /* Block accounting for super block */
3193 spin_lock(&info->delalloc_root_lock);
3194 old_val = btrfs_super_bytes_used(info->super_copy);
3195 if (alloc)
3196 old_val += num_bytes;
3197 else
3198 old_val -= num_bytes;
3199 btrfs_set_super_bytes_used(info->super_copy, old_val);
3200 spin_unlock(&info->delalloc_root_lock);
3201
3202 while (total) {
3203 cache = btrfs_lookup_block_group(info, bytenr);
3204 if (!cache) {
3205 ret = -ENOENT;
3206 break;
3207 }
3208 factor = btrfs_bg_type_to_factor(cache->flags);
3209
3210 /*
3211 * If this block group has free space cache written out, we
3212 * need to make sure to load it if we are removing space. This
3213 * is because we need the unpinning stage to actually add the
3214 * space back to the block group, otherwise we will leak space.
3215 */
David Sterba32da53862019-10-29 19:20:18 +01003216 if (!alloc && !btrfs_block_group_done(cache))
Josef Bacik606d1bf2019-06-20 15:38:02 -04003217 btrfs_cache_block_group(cache, 1);
3218
David Sterbab3470b52019-10-23 18:48:22 +02003219 byte_in_group = bytenr - cache->start;
3220 WARN_ON(byte_in_group > cache->length);
Josef Bacik606d1bf2019-06-20 15:38:02 -04003221
3222 spin_lock(&cache->space_info->lock);
3223 spin_lock(&cache->lock);
3224
3225 if (btrfs_test_opt(info, SPACE_CACHE) &&
3226 cache->disk_cache_state < BTRFS_DC_CLEAR)
3227 cache->disk_cache_state = BTRFS_DC_CLEAR;
3228
David Sterbabf38be62019-10-23 18:48:11 +02003229 old_val = cache->used;
David Sterbab3470b52019-10-23 18:48:22 +02003230 num_bytes = min(total, cache->length - byte_in_group);
Josef Bacik606d1bf2019-06-20 15:38:02 -04003231 if (alloc) {
3232 old_val += num_bytes;
David Sterbabf38be62019-10-23 18:48:11 +02003233 cache->used = old_val;
Josef Bacik606d1bf2019-06-20 15:38:02 -04003234 cache->reserved -= num_bytes;
3235 cache->space_info->bytes_reserved -= num_bytes;
3236 cache->space_info->bytes_used += num_bytes;
3237 cache->space_info->disk_used += num_bytes * factor;
3238 spin_unlock(&cache->lock);
3239 spin_unlock(&cache->space_info->lock);
3240 } else {
3241 old_val -= num_bytes;
David Sterbabf38be62019-10-23 18:48:11 +02003242 cache->used = old_val;
Josef Bacik606d1bf2019-06-20 15:38:02 -04003243 cache->pinned += num_bytes;
3244 btrfs_space_info_update_bytes_pinned(info,
3245 cache->space_info, num_bytes);
3246 cache->space_info->bytes_used -= num_bytes;
3247 cache->space_info->disk_used -= num_bytes * factor;
3248 spin_unlock(&cache->lock);
3249 spin_unlock(&cache->space_info->lock);
3250
Nikolay Borisovfe119a62020-01-20 16:09:18 +02003251 set_extent_dirty(&trans->transaction->pinned_extents,
Josef Bacik606d1bf2019-06-20 15:38:02 -04003252 bytenr, bytenr + num_bytes - 1,
3253 GFP_NOFS | __GFP_NOFAIL);
3254 }
3255
3256 spin_lock(&trans->transaction->dirty_bgs_lock);
3257 if (list_empty(&cache->dirty_list)) {
3258 list_add_tail(&cache->dirty_list,
3259 &trans->transaction->dirty_bgs);
3260 trans->delayed_ref_updates++;
3261 btrfs_get_block_group(cache);
3262 }
3263 spin_unlock(&trans->transaction->dirty_bgs_lock);
3264
3265 /*
3266 * No longer have used bytes in this block group, queue it for
3267 * deletion. We do this after adding the block group to the
3268 * dirty list to avoid races between cleaner kthread and space
3269 * cache writeout.
3270 */
Dennis Zhou6e80d4f2019-12-13 16:22:15 -08003271 if (!alloc && old_val == 0) {
3272 if (!btrfs_test_opt(info, DISCARD_ASYNC))
3273 btrfs_mark_bg_unused(cache);
3274 }
Josef Bacik606d1bf2019-06-20 15:38:02 -04003275
3276 btrfs_put_block_group(cache);
3277 total -= num_bytes;
3278 bytenr += num_bytes;
3279 }
3280
3281 /* Modified block groups are accounted for in the delayed_refs_rsv. */
3282 btrfs_update_delayed_refs_rsv(trans);
3283 return ret;
3284}
3285
3286/**
3287 * btrfs_add_reserved_bytes - update the block_group and space info counters
3288 * @cache: The cache we are manipulating
3289 * @ram_bytes: The number of bytes of file content, and will be same to
3290 * @num_bytes except for the compress path.
3291 * @num_bytes: The number of bytes in question
3292 * @delalloc: The blocks are allocated for the delalloc write
3293 *
3294 * This is called by the allocator when it reserves space. If this is a
3295 * reservation and the block group has become read only we cannot make the
3296 * reservation and return -EAGAIN, otherwise this function always succeeds.
3297 */
David Sterba32da53862019-10-29 19:20:18 +01003298int btrfs_add_reserved_bytes(struct btrfs_block_group *cache,
Josef Bacik606d1bf2019-06-20 15:38:02 -04003299 u64 ram_bytes, u64 num_bytes, int delalloc)
3300{
3301 struct btrfs_space_info *space_info = cache->space_info;
3302 int ret = 0;
3303
3304 spin_lock(&space_info->lock);
3305 spin_lock(&cache->lock);
3306 if (cache->ro) {
3307 ret = -EAGAIN;
3308 } else {
3309 cache->reserved += num_bytes;
3310 space_info->bytes_reserved += num_bytes;
Josef Bacika43c3832019-08-22 15:10:56 -04003311 trace_btrfs_space_reservation(cache->fs_info, "space_info",
3312 space_info->flags, num_bytes, 1);
Josef Bacik606d1bf2019-06-20 15:38:02 -04003313 btrfs_space_info_update_bytes_may_use(cache->fs_info,
3314 space_info, -ram_bytes);
3315 if (delalloc)
3316 cache->delalloc_bytes += num_bytes;
Josef Bacik99ffb432020-07-21 10:22:19 -04003317
3318 /*
3319 * Compression can use less space than we reserved, so wake
3320 * tickets if that happens
3321 */
3322 if (num_bytes < ram_bytes)
3323 btrfs_try_granting_tickets(cache->fs_info, space_info);
Josef Bacik606d1bf2019-06-20 15:38:02 -04003324 }
3325 spin_unlock(&cache->lock);
3326 spin_unlock(&space_info->lock);
3327 return ret;
3328}
3329
3330/**
3331 * btrfs_free_reserved_bytes - update the block_group and space info counters
3332 * @cache: The cache we are manipulating
3333 * @num_bytes: The number of bytes in question
3334 * @delalloc: The blocks are allocated for the delalloc write
3335 *
3336 * This is called by somebody who is freeing space that was never actually used
3337 * on disk. For example if you reserve some space for a new leaf in transaction
3338 * A and before transaction A commits you free that leaf, you call this with
3339 * reserve set to 0 in order to clear the reservation.
3340 */
David Sterba32da53862019-10-29 19:20:18 +01003341void btrfs_free_reserved_bytes(struct btrfs_block_group *cache,
Josef Bacik606d1bf2019-06-20 15:38:02 -04003342 u64 num_bytes, int delalloc)
3343{
3344 struct btrfs_space_info *space_info = cache->space_info;
3345
3346 spin_lock(&space_info->lock);
3347 spin_lock(&cache->lock);
3348 if (cache->ro)
3349 space_info->bytes_readonly += num_bytes;
3350 cache->reserved -= num_bytes;
3351 space_info->bytes_reserved -= num_bytes;
3352 space_info->max_extent_size = 0;
3353
3354 if (delalloc)
3355 cache->delalloc_bytes -= num_bytes;
3356 spin_unlock(&cache->lock);
Josef Bacik33082342020-07-21 10:22:17 -04003357
3358 btrfs_try_granting_tickets(cache->fs_info, space_info);
Josef Bacik606d1bf2019-06-20 15:38:02 -04003359 spin_unlock(&space_info->lock);
3360}
Josef Bacik07730d82019-06-20 15:38:04 -04003361
3362static void force_metadata_allocation(struct btrfs_fs_info *info)
3363{
3364 struct list_head *head = &info->space_info;
3365 struct btrfs_space_info *found;
3366
Josef Bacik72804902020-09-01 17:40:37 -04003367 list_for_each_entry(found, head, list) {
Josef Bacik07730d82019-06-20 15:38:04 -04003368 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
3369 found->force_alloc = CHUNK_ALLOC_FORCE;
3370 }
Josef Bacik07730d82019-06-20 15:38:04 -04003371}
3372
3373static int should_alloc_chunk(struct btrfs_fs_info *fs_info,
3374 struct btrfs_space_info *sinfo, int force)
3375{
3376 u64 bytes_used = btrfs_space_info_used(sinfo, false);
3377 u64 thresh;
3378
3379 if (force == CHUNK_ALLOC_FORCE)
3380 return 1;
3381
3382 /*
3383 * in limited mode, we want to have some free space up to
3384 * about 1% of the FS size.
3385 */
3386 if (force == CHUNK_ALLOC_LIMITED) {
3387 thresh = btrfs_super_total_bytes(fs_info->super_copy);
3388 thresh = max_t(u64, SZ_64M, div_factor_fine(thresh, 1));
3389
3390 if (sinfo->total_bytes - bytes_used < thresh)
3391 return 1;
3392 }
3393
3394 if (bytes_used + SZ_2M < div_factor(sinfo->total_bytes, 8))
3395 return 0;
3396 return 1;
3397}
3398
3399int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans, u64 type)
3400{
3401 u64 alloc_flags = btrfs_get_alloc_profile(trans->fs_info, type);
3402
3403 return btrfs_chunk_alloc(trans, alloc_flags, CHUNK_ALLOC_FORCE);
3404}
3405
Filipe Manana79bd3712021-06-29 14:43:06 +01003406static int do_chunk_alloc(struct btrfs_trans_handle *trans, u64 flags)
3407{
3408 struct btrfs_block_group *bg;
3409 int ret;
3410
3411 /*
3412 * Check if we have enough space in the system space info because we
3413 * will need to update device items in the chunk btree and insert a new
3414 * chunk item in the chunk btree as well. This will allocate a new
3415 * system block group if needed.
3416 */
3417 check_system_chunk(trans, flags);
3418
Nikolay Borisovf6f39f72021-08-18 13:41:19 +03003419 bg = btrfs_create_chunk(trans, flags);
Filipe Manana79bd3712021-06-29 14:43:06 +01003420 if (IS_ERR(bg)) {
3421 ret = PTR_ERR(bg);
3422 goto out;
3423 }
3424
Filipe Manana79bd3712021-06-29 14:43:06 +01003425 ret = btrfs_chunk_alloc_add_chunk_item(trans, bg);
3426 /*
3427 * Normally we are not expected to fail with -ENOSPC here, since we have
3428 * previously reserved space in the system space_info and allocated one
Filipe Mananaecd84d52021-10-13 10:12:50 +01003429 * new system chunk if necessary. However there are three exceptions:
Filipe Manana79bd3712021-06-29 14:43:06 +01003430 *
3431 * 1) We may have enough free space in the system space_info but all the
3432 * existing system block groups have a profile which can not be used
3433 * for extent allocation.
3434 *
3435 * This happens when mounting in degraded mode. For example we have a
3436 * RAID1 filesystem with 2 devices, lose one device and mount the fs
3437 * using the other device in degraded mode. If we then allocate a chunk,
3438 * we may have enough free space in the existing system space_info, but
3439 * none of the block groups can be used for extent allocation since they
3440 * have a RAID1 profile, and because we are in degraded mode with a
3441 * single device, we are forced to allocate a new system chunk with a
3442 * SINGLE profile. Making check_system_chunk() iterate over all system
3443 * block groups and check if they have a usable profile and enough space
3444 * can be slow on very large filesystems, so we tolerate the -ENOSPC and
3445 * try again after forcing allocation of a new system chunk. Like this
3446 * we avoid paying the cost of that search in normal circumstances, when
3447 * we were not mounted in degraded mode;
3448 *
3449 * 2) We had enough free space info the system space_info, and one suitable
3450 * block group to allocate from when we called check_system_chunk()
3451 * above. However right after we called it, the only system block group
3452 * with enough free space got turned into RO mode by a running scrub,
3453 * and in this case we have to allocate a new one and retry. We only
3454 * need do this allocate and retry once, since we have a transaction
Filipe Mananaecd84d52021-10-13 10:12:50 +01003455 * handle and scrub uses the commit root to search for block groups;
3456 *
3457 * 3) We had one system block group with enough free space when we called
3458 * check_system_chunk(), but after that, right before we tried to
3459 * allocate the last extent buffer we needed, a discard operation came
3460 * in and it temporarily removed the last free space entry from the
3461 * block group (discard removes a free space entry, discards it, and
3462 * then adds back the entry to the block group cache).
Filipe Manana79bd3712021-06-29 14:43:06 +01003463 */
3464 if (ret == -ENOSPC) {
3465 const u64 sys_flags = btrfs_system_alloc_profile(trans->fs_info);
3466 struct btrfs_block_group *sys_bg;
3467
Nikolay Borisovf6f39f72021-08-18 13:41:19 +03003468 sys_bg = btrfs_create_chunk(trans, sys_flags);
Filipe Manana79bd3712021-06-29 14:43:06 +01003469 if (IS_ERR(sys_bg)) {
3470 ret = PTR_ERR(sys_bg);
3471 btrfs_abort_transaction(trans, ret);
3472 goto out;
3473 }
3474
3475 ret = btrfs_chunk_alloc_add_chunk_item(trans, sys_bg);
3476 if (ret) {
3477 btrfs_abort_transaction(trans, ret);
3478 goto out;
3479 }
3480
3481 ret = btrfs_chunk_alloc_add_chunk_item(trans, bg);
3482 if (ret) {
3483 btrfs_abort_transaction(trans, ret);
3484 goto out;
3485 }
3486 } else if (ret) {
3487 btrfs_abort_transaction(trans, ret);
3488 goto out;
3489 }
3490out:
3491 btrfs_trans_release_chunk_metadata(trans);
3492
3493 return ret;
3494}
3495
Josef Bacik07730d82019-06-20 15:38:04 -04003496/*
Filipe Manana79bd3712021-06-29 14:43:06 +01003497 * Chunk allocation is done in 2 phases:
3498 *
3499 * 1) Phase 1 - through btrfs_chunk_alloc() we allocate device extents for
3500 * the chunk, the chunk mapping, create its block group and add the items
3501 * that belong in the chunk btree to it - more specifically, we need to
3502 * update device items in the chunk btree and add a new chunk item to it.
3503 *
3504 * 2) Phase 2 - through btrfs_create_pending_block_groups(), we add the block
3505 * group item to the extent btree and the device extent items to the devices
3506 * btree.
3507 *
3508 * This is done to prevent deadlocks. For example when COWing a node from the
3509 * extent btree we are holding a write lock on the node's parent and if we
3510 * trigger chunk allocation and attempted to insert the new block group item
3511 * in the extent btree right way, we could deadlock because the path for the
3512 * insertion can include that parent node. At first glance it seems impossible
3513 * to trigger chunk allocation after starting a transaction since tasks should
3514 * reserve enough transaction units (metadata space), however while that is true
3515 * most of the time, chunk allocation may still be triggered for several reasons:
3516 *
3517 * 1) When reserving metadata, we check if there is enough free space in the
3518 * metadata space_info and therefore don't trigger allocation of a new chunk.
3519 * However later when the task actually tries to COW an extent buffer from
3520 * the extent btree or from the device btree for example, it is forced to
3521 * allocate a new block group (chunk) because the only one that had enough
3522 * free space was just turned to RO mode by a running scrub for example (or
3523 * device replace, block group reclaim thread, etc), so we can not use it
3524 * for allocating an extent and end up being forced to allocate a new one;
3525 *
3526 * 2) Because we only check that the metadata space_info has enough free bytes,
3527 * we end up not allocating a new metadata chunk in that case. However if
3528 * the filesystem was mounted in degraded mode, none of the existing block
3529 * groups might be suitable for extent allocation due to their incompatible
3530 * profile (for e.g. mounting a 2 devices filesystem, where all block groups
3531 * use a RAID1 profile, in degraded mode using a single device). In this case
3532 * when the task attempts to COW some extent buffer of the extent btree for
3533 * example, it will trigger allocation of a new metadata block group with a
3534 * suitable profile (SINGLE profile in the example of the degraded mount of
3535 * the RAID1 filesystem);
3536 *
3537 * 3) The task has reserved enough transaction units / metadata space, but when
3538 * it attempts to COW an extent buffer from the extent or device btree for
3539 * example, it does not find any free extent in any metadata block group,
3540 * therefore forced to try to allocate a new metadata block group.
3541 * This is because some other task allocated all available extents in the
3542 * meanwhile - this typically happens with tasks that don't reserve space
3543 * properly, either intentionally or as a bug. One example where this is
3544 * done intentionally is fsync, as it does not reserve any transaction units
3545 * and ends up allocating a variable number of metadata extents for log
Filipe Mananaecd84d52021-10-13 10:12:50 +01003546 * tree extent buffers;
3547 *
3548 * 4) The task has reserved enough transaction units / metadata space, but right
3549 * before it tries to allocate the last extent buffer it needs, a discard
3550 * operation comes in and, temporarily, removes the last free space entry from
3551 * the only metadata block group that had free space (discard starts by
3552 * removing a free space entry from a block group, then does the discard
3553 * operation and, once it's done, it adds back the free space entry to the
3554 * block group).
Filipe Manana79bd3712021-06-29 14:43:06 +01003555 *
3556 * We also need this 2 phases setup when adding a device to a filesystem with
3557 * a seed device - we must create new metadata and system chunks without adding
3558 * any of the block group items to the chunk, extent and device btrees. If we
3559 * did not do it this way, we would get ENOSPC when attempting to update those
3560 * btrees, since all the chunks from the seed device are read-only.
3561 *
3562 * Phase 1 does the updates and insertions to the chunk btree because if we had
3563 * it done in phase 2 and have a thundering herd of tasks allocating chunks in
3564 * parallel, we risk having too many system chunks allocated by many tasks if
3565 * many tasks reach phase 1 without the previous ones completing phase 2. In the
3566 * extreme case this leads to exhaustion of the system chunk array in the
3567 * superblock. This is easier to trigger if using a btree node/leaf size of 64K
3568 * and with RAID filesystems (so we have more device items in the chunk btree).
3569 * This has happened before and commit eafa4fd0ad0607 ("btrfs: fix exhaustion of
3570 * the system chunk array due to concurrent allocations") provides more details.
3571 *
Filipe Manana2bb2e002021-10-13 10:12:49 +01003572 * Allocation of system chunks does not happen through this function. A task that
3573 * needs to update the chunk btree (the only btree that uses system chunks), must
3574 * preallocate chunk space by calling either check_system_chunk() or
3575 * btrfs_reserve_chunk_metadata() - the former is used when allocating a data or
3576 * metadata chunk or when removing a chunk, while the later is used before doing
3577 * a modification to the chunk btree - use cases for the later are adding,
3578 * removing and resizing a device as well as relocation of a system chunk.
3579 * See the comment below for more details.
Filipe Manana79bd3712021-06-29 14:43:06 +01003580 *
3581 * The reservation of system space, done through check_system_chunk(), as well
3582 * as all the updates and insertions into the chunk btree must be done while
3583 * holding fs_info->chunk_mutex. This is important to guarantee that while COWing
3584 * an extent buffer from the chunks btree we never trigger allocation of a new
3585 * system chunk, which would result in a deadlock (trying to lock twice an
3586 * extent buffer of the chunk btree, first time before triggering the chunk
3587 * allocation and the second time during chunk allocation while attempting to
3588 * update the chunks btree). The system chunk array is also updated while holding
3589 * that mutex. The same logic applies to removing chunks - we must reserve system
3590 * space, update the chunk btree and the system chunk array in the superblock
3591 * while holding fs_info->chunk_mutex.
3592 *
3593 * This function, btrfs_chunk_alloc(), belongs to phase 1.
3594 *
3595 * If @force is CHUNK_ALLOC_FORCE:
Josef Bacik07730d82019-06-20 15:38:04 -04003596 * - return 1 if it successfully allocates a chunk,
3597 * - return errors including -ENOSPC otherwise.
Filipe Manana79bd3712021-06-29 14:43:06 +01003598 * If @force is NOT CHUNK_ALLOC_FORCE:
Josef Bacik07730d82019-06-20 15:38:04 -04003599 * - return 0 if it doesn't need to allocate a new chunk,
3600 * - return 1 if it successfully allocates a chunk,
3601 * - return errors including -ENOSPC otherwise.
3602 */
3603int btrfs_chunk_alloc(struct btrfs_trans_handle *trans, u64 flags,
3604 enum btrfs_chunk_alloc_enum force)
3605{
3606 struct btrfs_fs_info *fs_info = trans->fs_info;
3607 struct btrfs_space_info *space_info;
3608 bool wait_for_alloc = false;
3609 bool should_alloc = false;
3610 int ret = 0;
3611
3612 /* Don't re-enter if we're already allocating a chunk */
3613 if (trans->allocating_chunk)
3614 return -ENOSPC;
Filipe Manana79bd3712021-06-29 14:43:06 +01003615 /*
Filipe Manana2bb2e002021-10-13 10:12:49 +01003616 * Allocation of system chunks can not happen through this path, as we
3617 * could end up in a deadlock if we are allocating a data or metadata
3618 * chunk and there is another task modifying the chunk btree.
3619 *
3620 * This is because while we are holding the chunk mutex, we will attempt
3621 * to add the new chunk item to the chunk btree or update an existing
3622 * device item in the chunk btree, while the other task that is modifying
3623 * the chunk btree is attempting to COW an extent buffer while holding a
3624 * lock on it and on its parent - if the COW operation triggers a system
3625 * chunk allocation, then we can deadlock because we are holding the
3626 * chunk mutex and we may need to access that extent buffer or its parent
3627 * in order to add the chunk item or update a device item.
3628 *
3629 * Tasks that want to modify the chunk tree should reserve system space
3630 * before updating the chunk btree, by calling either
3631 * btrfs_reserve_chunk_metadata() or check_system_chunk().
3632 * It's possible that after a task reserves the space, it still ends up
3633 * here - this happens in the cases described above at do_chunk_alloc().
3634 * The task will have to either retry or fail.
Filipe Manana79bd3712021-06-29 14:43:06 +01003635 */
Filipe Manana2bb2e002021-10-13 10:12:49 +01003636 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
Filipe Manana79bd3712021-06-29 14:43:06 +01003637 return -ENOSPC;
Josef Bacik07730d82019-06-20 15:38:04 -04003638
3639 space_info = btrfs_find_space_info(fs_info, flags);
3640 ASSERT(space_info);
3641
3642 do {
3643 spin_lock(&space_info->lock);
3644 if (force < space_info->force_alloc)
3645 force = space_info->force_alloc;
3646 should_alloc = should_alloc_chunk(fs_info, space_info, force);
3647 if (space_info->full) {
3648 /* No more free physical space */
3649 if (should_alloc)
3650 ret = -ENOSPC;
3651 else
3652 ret = 0;
3653 spin_unlock(&space_info->lock);
3654 return ret;
3655 } else if (!should_alloc) {
3656 spin_unlock(&space_info->lock);
3657 return 0;
3658 } else if (space_info->chunk_alloc) {
3659 /*
3660 * Someone is already allocating, so we need to block
3661 * until this someone is finished and then loop to
3662 * recheck if we should continue with our allocation
3663 * attempt.
3664 */
3665 wait_for_alloc = true;
3666 spin_unlock(&space_info->lock);
3667 mutex_lock(&fs_info->chunk_mutex);
3668 mutex_unlock(&fs_info->chunk_mutex);
3669 } else {
3670 /* Proceed with allocation */
3671 space_info->chunk_alloc = 1;
3672 wait_for_alloc = false;
3673 spin_unlock(&space_info->lock);
3674 }
3675
3676 cond_resched();
3677 } while (wait_for_alloc);
3678
3679 mutex_lock(&fs_info->chunk_mutex);
3680 trans->allocating_chunk = true;
3681
3682 /*
3683 * If we have mixed data/metadata chunks we want to make sure we keep
3684 * allocating mixed chunks instead of individual chunks.
3685 */
3686 if (btrfs_mixed_space_info(space_info))
3687 flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
3688
3689 /*
3690 * if we're doing a data chunk, go ahead and make sure that
3691 * we keep a reasonable number of metadata chunks allocated in the
3692 * FS as well.
3693 */
3694 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
3695 fs_info->data_chunk_allocations++;
3696 if (!(fs_info->data_chunk_allocations %
3697 fs_info->metadata_ratio))
3698 force_metadata_allocation(fs_info);
3699 }
3700
Filipe Manana79bd3712021-06-29 14:43:06 +01003701 ret = do_chunk_alloc(trans, flags);
Josef Bacik07730d82019-06-20 15:38:04 -04003702 trans->allocating_chunk = false;
3703
3704 spin_lock(&space_info->lock);
3705 if (ret < 0) {
3706 if (ret == -ENOSPC)
3707 space_info->full = 1;
3708 else
3709 goto out;
3710 } else {
3711 ret = 1;
3712 space_info->max_extent_size = 0;
3713 }
3714
3715 space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
3716out:
3717 space_info->chunk_alloc = 0;
3718 spin_unlock(&space_info->lock);
3719 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik07730d82019-06-20 15:38:04 -04003720
3721 return ret;
3722}
3723
3724static u64 get_profile_num_devs(struct btrfs_fs_info *fs_info, u64 type)
3725{
3726 u64 num_dev;
3727
3728 num_dev = btrfs_raid_array[btrfs_bg_flags_to_raid_index(type)].devs_max;
3729 if (!num_dev)
3730 num_dev = fs_info->fs_devices->rw_devices;
3731
3732 return num_dev;
3733}
3734
Filipe Manana2bb2e002021-10-13 10:12:49 +01003735static void reserve_chunk_space(struct btrfs_trans_handle *trans,
3736 u64 bytes,
3737 u64 type)
Josef Bacik07730d82019-06-20 15:38:04 -04003738{
3739 struct btrfs_fs_info *fs_info = trans->fs_info;
3740 struct btrfs_space_info *info;
3741 u64 left;
Josef Bacik07730d82019-06-20 15:38:04 -04003742 int ret = 0;
Josef Bacik07730d82019-06-20 15:38:04 -04003743
3744 /*
3745 * Needed because we can end up allocating a system chunk and for an
3746 * atomic and race free space reservation in the chunk block reserve.
3747 */
3748 lockdep_assert_held(&fs_info->chunk_mutex);
3749
3750 info = btrfs_find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
3751 spin_lock(&info->lock);
3752 left = info->total_bytes - btrfs_space_info_used(info, true);
3753 spin_unlock(&info->lock);
3754
Filipe Manana2bb2e002021-10-13 10:12:49 +01003755 if (left < bytes && btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
Josef Bacik07730d82019-06-20 15:38:04 -04003756 btrfs_info(fs_info, "left=%llu, need=%llu, flags=%llu",
Filipe Manana2bb2e002021-10-13 10:12:49 +01003757 left, bytes, type);
Josef Bacik07730d82019-06-20 15:38:04 -04003758 btrfs_dump_space_info(fs_info, info, 0, 0);
3759 }
3760
Filipe Manana2bb2e002021-10-13 10:12:49 +01003761 if (left < bytes) {
Josef Bacik07730d82019-06-20 15:38:04 -04003762 u64 flags = btrfs_system_alloc_profile(fs_info);
Filipe Manana79bd3712021-06-29 14:43:06 +01003763 struct btrfs_block_group *bg;
Josef Bacik07730d82019-06-20 15:38:04 -04003764
3765 /*
3766 * Ignore failure to create system chunk. We might end up not
3767 * needing it, as we might not need to COW all nodes/leafs from
3768 * the paths we visit in the chunk tree (they were already COWed
3769 * or created in the current transaction for example).
3770 */
Nikolay Borisovf6f39f72021-08-18 13:41:19 +03003771 bg = btrfs_create_chunk(trans, flags);
Filipe Manana79bd3712021-06-29 14:43:06 +01003772 if (IS_ERR(bg)) {
3773 ret = PTR_ERR(bg);
Filipe Manana2bb2e002021-10-13 10:12:49 +01003774 } else {
Filipe Manana79bd3712021-06-29 14:43:06 +01003775 /*
3776 * If we fail to add the chunk item here, we end up
3777 * trying again at phase 2 of chunk allocation, at
3778 * btrfs_create_pending_block_groups(). So ignore
Filipe Manana2bb2e002021-10-13 10:12:49 +01003779 * any error here. An ENOSPC here could happen, due to
3780 * the cases described at do_chunk_alloc() - the system
3781 * block group we just created was just turned into RO
3782 * mode by a scrub for example, or a running discard
3783 * temporarily removed its free space entries, etc.
Filipe Manana79bd3712021-06-29 14:43:06 +01003784 */
3785 btrfs_chunk_alloc_add_chunk_item(trans, bg);
3786 }
Josef Bacik07730d82019-06-20 15:38:04 -04003787 }
3788
3789 if (!ret) {
Josef Bacik92705012021-11-09 10:12:07 -05003790 ret = btrfs_block_rsv_add(fs_info,
Josef Bacik07730d82019-06-20 15:38:04 -04003791 &fs_info->chunk_block_rsv,
Filipe Manana2bb2e002021-10-13 10:12:49 +01003792 bytes, BTRFS_RESERVE_NO_FLUSH);
Filipe Manana1cb3db12021-06-29 14:43:05 +01003793 if (!ret)
Filipe Manana2bb2e002021-10-13 10:12:49 +01003794 trans->chunk_bytes_reserved += bytes;
Josef Bacik07730d82019-06-20 15:38:04 -04003795 }
3796}
3797
Filipe Manana2bb2e002021-10-13 10:12:49 +01003798/*
3799 * Reserve space in the system space for allocating or removing a chunk.
3800 * The caller must be holding fs_info->chunk_mutex.
3801 */
3802void check_system_chunk(struct btrfs_trans_handle *trans, u64 type)
3803{
3804 struct btrfs_fs_info *fs_info = trans->fs_info;
3805 const u64 num_devs = get_profile_num_devs(fs_info, type);
3806 u64 bytes;
3807
3808 /* num_devs device items to update and 1 chunk item to add or remove. */
3809 bytes = btrfs_calc_metadata_size(fs_info, num_devs) +
3810 btrfs_calc_insert_metadata_size(fs_info, 1);
3811
3812 reserve_chunk_space(trans, bytes, type);
3813}
3814
3815/*
3816 * Reserve space in the system space, if needed, for doing a modification to the
3817 * chunk btree.
3818 *
3819 * @trans: A transaction handle.
3820 * @is_item_insertion: Indicate if the modification is for inserting a new item
3821 * in the chunk btree or if it's for the deletion or update
3822 * of an existing item.
3823 *
3824 * This is used in a context where we need to update the chunk btree outside
3825 * block group allocation and removal, to avoid a deadlock with a concurrent
3826 * task that is allocating a metadata or data block group and therefore needs to
3827 * update the chunk btree while holding the chunk mutex. After the update to the
3828 * chunk btree is done, btrfs_trans_release_chunk_metadata() should be called.
3829 *
3830 */
3831void btrfs_reserve_chunk_metadata(struct btrfs_trans_handle *trans,
3832 bool is_item_insertion)
3833{
3834 struct btrfs_fs_info *fs_info = trans->fs_info;
3835 u64 bytes;
3836
3837 if (is_item_insertion)
3838 bytes = btrfs_calc_insert_metadata_size(fs_info, 1);
3839 else
3840 bytes = btrfs_calc_metadata_size(fs_info, 1);
3841
3842 mutex_lock(&fs_info->chunk_mutex);
3843 reserve_chunk_space(trans, bytes, BTRFS_BLOCK_GROUP_SYSTEM);
3844 mutex_unlock(&fs_info->chunk_mutex);
3845}
3846
Josef Bacik3e43c272019-06-20 15:38:06 -04003847void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
3848{
David Sterba32da53862019-10-29 19:20:18 +01003849 struct btrfs_block_group *block_group;
Josef Bacik3e43c272019-06-20 15:38:06 -04003850 u64 last = 0;
3851
3852 while (1) {
3853 struct inode *inode;
3854
3855 block_group = btrfs_lookup_first_block_group(info, last);
3856 while (block_group) {
3857 btrfs_wait_block_group_cache_done(block_group);
3858 spin_lock(&block_group->lock);
3859 if (block_group->iref)
3860 break;
3861 spin_unlock(&block_group->lock);
3862 block_group = btrfs_next_block_group(block_group);
3863 }
3864 if (!block_group) {
3865 if (last == 0)
3866 break;
3867 last = 0;
3868 continue;
3869 }
3870
3871 inode = block_group->inode;
3872 block_group->iref = 0;
3873 block_group->inode = NULL;
3874 spin_unlock(&block_group->lock);
3875 ASSERT(block_group->io_ctl.inode == NULL);
3876 iput(inode);
David Sterbab3470b52019-10-23 18:48:22 +02003877 last = block_group->start + block_group->length;
Josef Bacik3e43c272019-06-20 15:38:06 -04003878 btrfs_put_block_group(block_group);
3879 }
3880}
3881
3882/*
3883 * Must be called only after stopping all workers, since we could have block
3884 * group caching kthreads running, and therefore they could race with us if we
3885 * freed the block groups before stopping them.
3886 */
3887int btrfs_free_block_groups(struct btrfs_fs_info *info)
3888{
David Sterba32da53862019-10-29 19:20:18 +01003889 struct btrfs_block_group *block_group;
Josef Bacik3e43c272019-06-20 15:38:06 -04003890 struct btrfs_space_info *space_info;
3891 struct btrfs_caching_control *caching_ctl;
3892 struct rb_node *n;
3893
Josef Bacikbbb86a32020-10-23 09:58:11 -04003894 spin_lock(&info->block_group_cache_lock);
Josef Bacik3e43c272019-06-20 15:38:06 -04003895 while (!list_empty(&info->caching_block_groups)) {
3896 caching_ctl = list_entry(info->caching_block_groups.next,
3897 struct btrfs_caching_control, list);
3898 list_del(&caching_ctl->list);
3899 btrfs_put_caching_control(caching_ctl);
3900 }
Josef Bacikbbb86a32020-10-23 09:58:11 -04003901 spin_unlock(&info->block_group_cache_lock);
Josef Bacik3e43c272019-06-20 15:38:06 -04003902
3903 spin_lock(&info->unused_bgs_lock);
3904 while (!list_empty(&info->unused_bgs)) {
3905 block_group = list_first_entry(&info->unused_bgs,
David Sterba32da53862019-10-29 19:20:18 +01003906 struct btrfs_block_group,
Josef Bacik3e43c272019-06-20 15:38:06 -04003907 bg_list);
3908 list_del_init(&block_group->bg_list);
3909 btrfs_put_block_group(block_group);
3910 }
Josef Bacik3e43c272019-06-20 15:38:06 -04003911
Johannes Thumshirn18bb8bb2021-04-19 16:41:02 +09003912 while (!list_empty(&info->reclaim_bgs)) {
3913 block_group = list_first_entry(&info->reclaim_bgs,
3914 struct btrfs_block_group,
3915 bg_list);
3916 list_del_init(&block_group->bg_list);
3917 btrfs_put_block_group(block_group);
3918 }
3919 spin_unlock(&info->unused_bgs_lock);
3920
Naohiro Aotaafba2bc2021-08-19 21:19:17 +09003921 spin_lock(&info->zone_active_bgs_lock);
3922 while (!list_empty(&info->zone_active_bgs)) {
3923 block_group = list_first_entry(&info->zone_active_bgs,
3924 struct btrfs_block_group,
3925 active_bg_list);
3926 list_del_init(&block_group->active_bg_list);
3927 btrfs_put_block_group(block_group);
3928 }
3929 spin_unlock(&info->zone_active_bgs_lock);
3930
Josef Bacik3e43c272019-06-20 15:38:06 -04003931 spin_lock(&info->block_group_cache_lock);
3932 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
David Sterba32da53862019-10-29 19:20:18 +01003933 block_group = rb_entry(n, struct btrfs_block_group,
Josef Bacik3e43c272019-06-20 15:38:06 -04003934 cache_node);
3935 rb_erase(&block_group->cache_node,
3936 &info->block_group_cache_tree);
3937 RB_CLEAR_NODE(&block_group->cache_node);
3938 spin_unlock(&info->block_group_cache_lock);
3939
3940 down_write(&block_group->space_info->groups_sem);
3941 list_del(&block_group->list);
3942 up_write(&block_group->space_info->groups_sem);
3943
3944 /*
3945 * We haven't cached this block group, which means we could
3946 * possibly have excluded extents on this block group.
3947 */
3948 if (block_group->cached == BTRFS_CACHE_NO ||
3949 block_group->cached == BTRFS_CACHE_ERROR)
3950 btrfs_free_excluded_extents(block_group);
3951
3952 btrfs_remove_free_space_cache(block_group);
3953 ASSERT(block_group->cached != BTRFS_CACHE_STARTED);
3954 ASSERT(list_empty(&block_group->dirty_list));
3955 ASSERT(list_empty(&block_group->io_list));
3956 ASSERT(list_empty(&block_group->bg_list));
Josef Bacik48aaeeb2020-07-06 09:14:11 -04003957 ASSERT(refcount_read(&block_group->refs) == 1);
Filipe Manana195a49e2021-02-05 12:55:37 +00003958 ASSERT(block_group->swap_extents == 0);
Josef Bacik3e43c272019-06-20 15:38:06 -04003959 btrfs_put_block_group(block_group);
3960
3961 spin_lock(&info->block_group_cache_lock);
3962 }
3963 spin_unlock(&info->block_group_cache_lock);
3964
Josef Bacik3e43c272019-06-20 15:38:06 -04003965 btrfs_release_global_block_rsv(info);
3966
3967 while (!list_empty(&info->space_info)) {
3968 space_info = list_entry(info->space_info.next,
3969 struct btrfs_space_info,
3970 list);
3971
3972 /*
3973 * Do not hide this behind enospc_debug, this is actually
3974 * important and indicates a real bug if this happens.
3975 */
3976 if (WARN_ON(space_info->bytes_pinned > 0 ||
3977 space_info->bytes_reserved > 0 ||
3978 space_info->bytes_may_use > 0))
3979 btrfs_dump_space_info(info, space_info, 0, 0);
Filipe Mananad611add2020-04-07 11:38:49 +01003980 WARN_ON(space_info->reclaim_size > 0);
Josef Bacik3e43c272019-06-20 15:38:06 -04003981 list_del(&space_info->list);
3982 btrfs_sysfs_remove_space_info(space_info);
3983 }
3984 return 0;
3985}
Filipe Manana684b7522020-05-08 11:01:59 +01003986
3987void btrfs_freeze_block_group(struct btrfs_block_group *cache)
3988{
3989 atomic_inc(&cache->frozen);
3990}
3991
3992void btrfs_unfreeze_block_group(struct btrfs_block_group *block_group)
3993{
3994 struct btrfs_fs_info *fs_info = block_group->fs_info;
3995 struct extent_map_tree *em_tree;
3996 struct extent_map *em;
3997 bool cleanup;
3998
3999 spin_lock(&block_group->lock);
4000 cleanup = (atomic_dec_and_test(&block_group->frozen) &&
4001 block_group->removed);
4002 spin_unlock(&block_group->lock);
4003
4004 if (cleanup) {
Filipe Manana684b7522020-05-08 11:01:59 +01004005 em_tree = &fs_info->mapping_tree;
4006 write_lock(&em_tree->lock);
4007 em = lookup_extent_mapping(em_tree, block_group->start,
4008 1);
4009 BUG_ON(!em); /* logic error, can't happen */
4010 remove_extent_mapping(em_tree, em);
4011 write_unlock(&em_tree->lock);
Filipe Manana684b7522020-05-08 11:01:59 +01004012
4013 /* once for us and once for the tree */
4014 free_extent_map(em);
4015 free_extent_map(em);
4016
4017 /*
4018 * We may have left one free space entry and other possible
4019 * tasks trimming this block group have left 1 entry each one.
4020 * Free them if any.
4021 */
4022 __btrfs_remove_free_space_cache(block_group->free_space_ctl);
4023 }
4024}
Filipe Manana195a49e2021-02-05 12:55:37 +00004025
4026bool btrfs_inc_block_group_swap_extents(struct btrfs_block_group *bg)
4027{
4028 bool ret = true;
4029
4030 spin_lock(&bg->lock);
4031 if (bg->ro)
4032 ret = false;
4033 else
4034 bg->swap_extents++;
4035 spin_unlock(&bg->lock);
4036
4037 return ret;
4038}
4039
4040void btrfs_dec_block_group_swap_extents(struct btrfs_block_group *bg, int amount)
4041{
4042 spin_lock(&bg->lock);
4043 ASSERT(!bg->ro);
4044 ASSERT(bg->swap_extents >= amount);
4045 bg->swap_extents -= amount;
4046 spin_unlock(&bg->lock);
4047}