1 // SPDX-License-Identifier: GPL-2.0
5 #include "block-group.h"
6 #include "space-info.h"
8 #include "free-space-cache.h"
9 #include "free-space-tree.h"
11 #include "transaction.h"
12 #include "ref-verify.h"
15 #include "delalloc-space.h"
21 * Return target flags in extended format or 0 if restripe for this chunk_type
24 * Should be called with balance_lock held
26 static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags)
28 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
34 if (flags & BTRFS_BLOCK_GROUP_DATA &&
35 bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) {
36 target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target;
37 } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM &&
38 bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
39 target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target;
40 } else if (flags & BTRFS_BLOCK_GROUP_METADATA &&
41 bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) {
42 target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target;
49 * @flags: available profiles in extended format (see ctree.h)
51 * Return reduced profile in chunk format. If profile changing is in progress
52 * (either running or paused) picks the target profile (if it's already
53 * available), otherwise falls back to plain reducing.
55 static u64 btrfs_reduce_alloc_profile(struct btrfs_fs_info *fs_info, u64 flags)
57 u64 num_devices = fs_info->fs_devices->rw_devices;
63 * See if restripe for this chunk_type is in progress, if so try to
64 * reduce to the target profile
66 spin_lock(&fs_info->balance_lock);
67 target = get_restripe_target(fs_info, flags);
69 spin_unlock(&fs_info->balance_lock);
70 return extended_to_chunk(target);
72 spin_unlock(&fs_info->balance_lock);
74 /* First, mask out the RAID levels which aren't possible */
75 for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) {
76 if (num_devices >= btrfs_raid_array[raid_type].devs_min)
77 allowed |= btrfs_raid_array[raid_type].bg_flag;
81 if (allowed & BTRFS_BLOCK_GROUP_RAID6)
82 allowed = BTRFS_BLOCK_GROUP_RAID6;
83 else if (allowed & BTRFS_BLOCK_GROUP_RAID5)
84 allowed = BTRFS_BLOCK_GROUP_RAID5;
85 else if (allowed & BTRFS_BLOCK_GROUP_RAID10)
86 allowed = BTRFS_BLOCK_GROUP_RAID10;
87 else if (allowed & BTRFS_BLOCK_GROUP_RAID1)
88 allowed = BTRFS_BLOCK_GROUP_RAID1;
89 else if (allowed & BTRFS_BLOCK_GROUP_RAID0)
90 allowed = BTRFS_BLOCK_GROUP_RAID0;
92 flags &= ~BTRFS_BLOCK_GROUP_PROFILE_MASK;
94 return extended_to_chunk(flags | allowed);
97 u64 btrfs_get_alloc_profile(struct btrfs_fs_info *fs_info, u64 orig_flags)
104 seq = read_seqbegin(&fs_info->profiles_lock);
106 if (flags & BTRFS_BLOCK_GROUP_DATA)
107 flags |= fs_info->avail_data_alloc_bits;
108 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
109 flags |= fs_info->avail_system_alloc_bits;
110 else if (flags & BTRFS_BLOCK_GROUP_METADATA)
111 flags |= fs_info->avail_metadata_alloc_bits;
112 } while (read_seqretry(&fs_info->profiles_lock, seq));
114 return btrfs_reduce_alloc_profile(fs_info, flags);
117 void btrfs_get_block_group(struct btrfs_block_group *cache)
119 refcount_inc(&cache->refs);
122 void btrfs_put_block_group(struct btrfs_block_group *cache)
124 if (refcount_dec_and_test(&cache->refs)) {
125 WARN_ON(cache->pinned > 0);
127 * If there was a failure to cleanup a log tree, very likely due
128 * to an IO failure on a writeback attempt of one or more of its
129 * extent buffers, we could not do proper (and cheap) unaccounting
130 * of their reserved space, so don't warn on reserved > 0 in that
133 if (!(cache->flags & BTRFS_BLOCK_GROUP_METADATA) ||
134 !BTRFS_FS_LOG_CLEANUP_ERROR(cache->fs_info))
135 WARN_ON(cache->reserved > 0);
138 * A block_group shouldn't be on the discard_list anymore.
139 * Remove the block_group from the discard_list to prevent us
140 * from causing a panic due to NULL pointer dereference.
142 if (WARN_ON(!list_empty(&cache->discard_list)))
143 btrfs_discard_cancel_work(&cache->fs_info->discard_ctl,
147 * If not empty, someone is still holding mutex of
148 * full_stripe_lock, which can only be released by caller.
149 * And it will definitely cause use-after-free when caller
150 * tries to release full stripe lock.
152 * No better way to resolve, but only to warn.
154 WARN_ON(!RB_EMPTY_ROOT(&cache->full_stripe_locks_root.root));
155 kfree(cache->free_space_ctl);
161 * This adds the block group to the fs_info rb tree for the block group cache
163 static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
164 struct btrfs_block_group *block_group)
167 struct rb_node *parent = NULL;
168 struct btrfs_block_group *cache;
170 ASSERT(block_group->length != 0);
172 spin_lock(&info->block_group_cache_lock);
173 p = &info->block_group_cache_tree.rb_node;
177 cache = rb_entry(parent, struct btrfs_block_group, cache_node);
178 if (block_group->start < cache->start) {
180 } else if (block_group->start > cache->start) {
183 spin_unlock(&info->block_group_cache_lock);
188 rb_link_node(&block_group->cache_node, parent, p);
189 rb_insert_color(&block_group->cache_node,
190 &info->block_group_cache_tree);
192 if (info->first_logical_byte > block_group->start)
193 info->first_logical_byte = block_group->start;
195 spin_unlock(&info->block_group_cache_lock);
201 * This will return the block group at or after bytenr if contains is 0, else
202 * it will return the block group that contains the bytenr
204 static struct btrfs_block_group *block_group_cache_tree_search(
205 struct btrfs_fs_info *info, u64 bytenr, int contains)
207 struct btrfs_block_group *cache, *ret = NULL;
211 spin_lock(&info->block_group_cache_lock);
212 n = info->block_group_cache_tree.rb_node;
215 cache = rb_entry(n, struct btrfs_block_group, cache_node);
216 end = cache->start + cache->length - 1;
217 start = cache->start;
219 if (bytenr < start) {
220 if (!contains && (!ret || start < ret->start))
223 } else if (bytenr > start) {
224 if (contains && bytenr <= end) {
235 btrfs_get_block_group(ret);
236 if (bytenr == 0 && info->first_logical_byte > ret->start)
237 info->first_logical_byte = ret->start;
239 spin_unlock(&info->block_group_cache_lock);
245 * Return the block group that starts at or after bytenr
247 struct btrfs_block_group *btrfs_lookup_first_block_group(
248 struct btrfs_fs_info *info, u64 bytenr)
250 return block_group_cache_tree_search(info, bytenr, 0);
254 * Return the block group that contains the given bytenr
256 struct btrfs_block_group *btrfs_lookup_block_group(
257 struct btrfs_fs_info *info, u64 bytenr)
259 return block_group_cache_tree_search(info, bytenr, 1);
262 struct btrfs_block_group *btrfs_next_block_group(
263 struct btrfs_block_group *cache)
265 struct btrfs_fs_info *fs_info = cache->fs_info;
266 struct rb_node *node;
268 spin_lock(&fs_info->block_group_cache_lock);
270 /* If our block group was removed, we need a full search. */
271 if (RB_EMPTY_NODE(&cache->cache_node)) {
272 const u64 next_bytenr = cache->start + cache->length;
274 spin_unlock(&fs_info->block_group_cache_lock);
275 btrfs_put_block_group(cache);
276 cache = btrfs_lookup_first_block_group(fs_info, next_bytenr); return cache;
278 node = rb_next(&cache->cache_node);
279 btrfs_put_block_group(cache);
281 cache = rb_entry(node, struct btrfs_block_group, cache_node);
282 btrfs_get_block_group(cache);
285 spin_unlock(&fs_info->block_group_cache_lock);
289 bool btrfs_inc_nocow_writers(struct btrfs_fs_info *fs_info, u64 bytenr)
291 struct btrfs_block_group *bg;
294 bg = btrfs_lookup_block_group(fs_info, bytenr);
298 spin_lock(&bg->lock);
302 atomic_inc(&bg->nocow_writers);
303 spin_unlock(&bg->lock);
305 /* No put on block group, done by btrfs_dec_nocow_writers */
307 btrfs_put_block_group(bg);
312 void btrfs_dec_nocow_writers(struct btrfs_fs_info *fs_info, u64 bytenr)
314 struct btrfs_block_group *bg;
316 bg = btrfs_lookup_block_group(fs_info, bytenr);
318 if (atomic_dec_and_test(&bg->nocow_writers))
319 wake_up_var(&bg->nocow_writers);
321 * Once for our lookup and once for the lookup done by a previous call
322 * to btrfs_inc_nocow_writers()
324 btrfs_put_block_group(bg);
325 btrfs_put_block_group(bg);
328 void btrfs_wait_nocow_writers(struct btrfs_block_group *bg)
330 wait_var_event(&bg->nocow_writers, !atomic_read(&bg->nocow_writers));
333 void btrfs_dec_block_group_reservations(struct btrfs_fs_info *fs_info,
336 struct btrfs_block_group *bg;
338 bg = btrfs_lookup_block_group(fs_info, start);
340 if (atomic_dec_and_test(&bg->reservations))
341 wake_up_var(&bg->reservations);
342 btrfs_put_block_group(bg);
345 void btrfs_wait_block_group_reservations(struct btrfs_block_group *bg)
347 struct btrfs_space_info *space_info = bg->space_info;
351 if (!(bg->flags & BTRFS_BLOCK_GROUP_DATA))
355 * Our block group is read only but before we set it to read only,
356 * some task might have had allocated an extent from it already, but it
357 * has not yet created a respective ordered extent (and added it to a
358 * root's list of ordered extents).
359 * Therefore wait for any task currently allocating extents, since the
360 * block group's reservations counter is incremented while a read lock
361 * on the groups' semaphore is held and decremented after releasing
362 * the read access on that semaphore and creating the ordered extent.
364 down_write(&space_info->groups_sem);
365 up_write(&space_info->groups_sem);
367 wait_var_event(&bg->reservations, !atomic_read(&bg->reservations));
370 struct btrfs_caching_control *btrfs_get_caching_control(
371 struct btrfs_block_group *cache)
373 struct btrfs_caching_control *ctl;
375 spin_lock(&cache->lock);
376 if (!cache->caching_ctl) {
377 spin_unlock(&cache->lock);
381 ctl = cache->caching_ctl;
382 refcount_inc(&ctl->count);
383 spin_unlock(&cache->lock);
387 void btrfs_put_caching_control(struct btrfs_caching_control *ctl)
389 if (refcount_dec_and_test(&ctl->count))
394 * When we wait for progress in the block group caching, its because our
395 * allocation attempt failed at least once. So, we must sleep and let some
396 * progress happen before we try again.
398 * This function will sleep at least once waiting for new free space to show
399 * up, and then it will check the block group free space numbers for our min
400 * num_bytes. Another option is to have it go ahead and look in the rbtree for
401 * a free extent of a given size, but this is a good start.
403 * Callers of this must check if cache->cached == BTRFS_CACHE_ERROR before using
404 * any of the information in this block group.
406 void btrfs_wait_block_group_cache_progress(struct btrfs_block_group *cache,
409 struct btrfs_caching_control *caching_ctl;
411 caching_ctl = btrfs_get_caching_control(cache);
415 wait_event(caching_ctl->wait, btrfs_block_group_done(cache) ||
416 (cache->free_space_ctl->free_space >= num_bytes));
418 btrfs_put_caching_control(caching_ctl);
421 static int btrfs_caching_ctl_wait_done(struct btrfs_block_group *cache,
422 struct btrfs_caching_control *caching_ctl)
424 wait_event(caching_ctl->wait, btrfs_block_group_done(cache));
425 return cache->cached == BTRFS_CACHE_ERROR ? -EIO : 0;
428 static int btrfs_wait_block_group_cache_done(struct btrfs_block_group *cache)
430 struct btrfs_caching_control *caching_ctl;
433 caching_ctl = btrfs_get_caching_control(cache);
435 return (cache->cached == BTRFS_CACHE_ERROR) ? -EIO : 0;
436 ret = btrfs_caching_ctl_wait_done(cache, caching_ctl);
437 btrfs_put_caching_control(caching_ctl);
441 #ifdef CONFIG_BTRFS_DEBUG
442 static void fragment_free_space(struct btrfs_block_group *block_group)
444 struct btrfs_fs_info *fs_info = block_group->fs_info;
445 u64 start = block_group->start;
446 u64 len = block_group->length;
447 u64 chunk = block_group->flags & BTRFS_BLOCK_GROUP_METADATA ?
448 fs_info->nodesize : fs_info->sectorsize;
449 u64 step = chunk << 1;
451 while (len > chunk) {
452 btrfs_remove_free_space(block_group, start, chunk);
463 * This is only called by btrfs_cache_block_group, since we could have freed
464 * extents we need to check the pinned_extents for any extents that can't be
465 * used yet since their free space will be released as soon as the transaction
468 u64 add_new_free_space(struct btrfs_block_group *block_group, u64 start, u64 end)
470 struct btrfs_fs_info *info = block_group->fs_info;
471 u64 extent_start, extent_end, size, total_added = 0;
474 while (start < end) {
475 ret = find_first_extent_bit(&info->excluded_extents, start,
476 &extent_start, &extent_end,
477 EXTENT_DIRTY | EXTENT_UPTODATE,
482 if (extent_start <= start) {
483 start = extent_end + 1;
484 } else if (extent_start > start && extent_start < end) {
485 size = extent_start - start;
487 ret = btrfs_add_free_space_async_trimmed(block_group,
489 BUG_ON(ret); /* -ENOMEM or logic error */
490 start = extent_end + 1;
499 ret = btrfs_add_free_space_async_trimmed(block_group, start,
501 BUG_ON(ret); /* -ENOMEM or logic error */
507 static int load_extent_tree_free(struct btrfs_caching_control *caching_ctl)
509 struct btrfs_block_group *block_group = caching_ctl->block_group;
510 struct btrfs_fs_info *fs_info = block_group->fs_info;
511 struct btrfs_root *extent_root = fs_info->extent_root;
512 struct btrfs_path *path;
513 struct extent_buffer *leaf;
514 struct btrfs_key key;
521 path = btrfs_alloc_path();
525 last = max_t(u64, block_group->start, BTRFS_SUPER_INFO_OFFSET);
527 #ifdef CONFIG_BTRFS_DEBUG
529 * If we're fragmenting we don't want to make anybody think we can
530 * allocate from this block group until we've had a chance to fragment
533 if (btrfs_should_fragment_free_space(block_group))
537 * We don't want to deadlock with somebody trying to allocate a new
538 * extent for the extent root while also trying to search the extent
539 * root to add free space. So we skip locking and search the commit
540 * root, since its read-only
542 path->skip_locking = 1;
543 path->search_commit_root = 1;
544 path->reada = READA_FORWARD;
548 key.type = BTRFS_EXTENT_ITEM_KEY;
551 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
555 leaf = path->nodes[0];
556 nritems = btrfs_header_nritems(leaf);
559 if (btrfs_fs_closing(fs_info) > 1) {
564 if (path->slots[0] < nritems) {
565 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
567 ret = btrfs_find_next_key(extent_root, path, &key, 0, 0);
571 if (need_resched() ||
572 rwsem_is_contended(&fs_info->commit_root_sem)) {
574 caching_ctl->progress = last;
575 btrfs_release_path(path);
576 up_read(&fs_info->commit_root_sem);
577 mutex_unlock(&caching_ctl->mutex);
579 mutex_lock(&caching_ctl->mutex);
580 down_read(&fs_info->commit_root_sem);
584 ret = btrfs_next_leaf(extent_root, path);
589 leaf = path->nodes[0];
590 nritems = btrfs_header_nritems(leaf);
594 if (key.objectid < last) {
597 key.type = BTRFS_EXTENT_ITEM_KEY;
600 caching_ctl->progress = last;
601 btrfs_release_path(path);
605 if (key.objectid < block_group->start) {
610 if (key.objectid >= block_group->start + block_group->length)
613 if (key.type == BTRFS_EXTENT_ITEM_KEY ||
614 key.type == BTRFS_METADATA_ITEM_KEY) {
615 total_found += add_new_free_space(block_group, last,
617 if (key.type == BTRFS_METADATA_ITEM_KEY)
618 last = key.objectid +
621 last = key.objectid + key.offset;
623 if (total_found > CACHING_CTL_WAKE_UP) {
626 wake_up(&caching_ctl->wait);
633 total_found += add_new_free_space(block_group, last,
634 block_group->start + block_group->length);
635 caching_ctl->progress = (u64)-1;
638 btrfs_free_path(path);
642 static noinline void caching_thread(struct btrfs_work *work)
644 struct btrfs_block_group *block_group;
645 struct btrfs_fs_info *fs_info;
646 struct btrfs_caching_control *caching_ctl;
649 caching_ctl = container_of(work, struct btrfs_caching_control, work);
650 block_group = caching_ctl->block_group;
651 fs_info = block_group->fs_info;
653 mutex_lock(&caching_ctl->mutex);
654 down_read(&fs_info->commit_root_sem);
656 if (btrfs_test_opt(fs_info, SPACE_CACHE)) {
657 ret = load_free_space_cache(block_group);
664 * We failed to load the space cache, set ourselves to
665 * CACHE_STARTED and carry on.
667 spin_lock(&block_group->lock);
668 block_group->cached = BTRFS_CACHE_STARTED;
669 spin_unlock(&block_group->lock);
670 wake_up(&caching_ctl->wait);
674 * If we are in the transaction that populated the free space tree we
675 * can't actually cache from the free space tree as our commit root and
676 * real root are the same, so we could change the contents of the blocks
677 * while caching. Instead do the slow caching in this case, and after
678 * the transaction has committed we will be safe.
680 if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) &&
681 !(test_bit(BTRFS_FS_FREE_SPACE_TREE_UNTRUSTED, &fs_info->flags)))
682 ret = load_free_space_tree(caching_ctl);
684 ret = load_extent_tree_free(caching_ctl);
686 spin_lock(&block_group->lock);
687 block_group->caching_ctl = NULL;
688 block_group->cached = ret ? BTRFS_CACHE_ERROR : BTRFS_CACHE_FINISHED;
689 spin_unlock(&block_group->lock);
691 #ifdef CONFIG_BTRFS_DEBUG
692 if (btrfs_should_fragment_free_space(block_group)) {
695 spin_lock(&block_group->space_info->lock);
696 spin_lock(&block_group->lock);
697 bytes_used = block_group->length - block_group->used;
698 block_group->space_info->bytes_used += bytes_used >> 1;
699 spin_unlock(&block_group->lock);
700 spin_unlock(&block_group->space_info->lock);
701 fragment_free_space(block_group);
705 caching_ctl->progress = (u64)-1;
707 up_read(&fs_info->commit_root_sem);
708 btrfs_free_excluded_extents(block_group);
709 mutex_unlock(&caching_ctl->mutex);
711 wake_up(&caching_ctl->wait);
713 btrfs_put_caching_control(caching_ctl);
714 btrfs_put_block_group(block_group);
717 int btrfs_cache_block_group(struct btrfs_block_group *cache, bool wait)
719 struct btrfs_fs_info *fs_info = cache->fs_info;
720 struct btrfs_caching_control *caching_ctl = NULL;
723 /* Allocator for zoned filesystems does not use the cache at all */
724 if (btrfs_is_zoned(fs_info))
727 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS);
731 INIT_LIST_HEAD(&caching_ctl->list);
732 mutex_init(&caching_ctl->mutex);
733 init_waitqueue_head(&caching_ctl->wait);
734 caching_ctl->block_group = cache;
735 caching_ctl->progress = cache->start;
736 refcount_set(&caching_ctl->count, 2);
737 btrfs_init_work(&caching_ctl->work, caching_thread, NULL, NULL);
739 spin_lock(&cache->lock);
740 if (cache->cached != BTRFS_CACHE_NO) {
743 caching_ctl = cache->caching_ctl;
745 refcount_inc(&caching_ctl->count);
746 spin_unlock(&cache->lock);
749 WARN_ON(cache->caching_ctl);
750 cache->caching_ctl = caching_ctl;
751 cache->cached = BTRFS_CACHE_STARTED;
752 cache->has_caching_ctl = 1;
753 spin_unlock(&cache->lock);
755 spin_lock(&fs_info->block_group_cache_lock);
756 refcount_inc(&caching_ctl->count);
757 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
758 spin_unlock(&fs_info->block_group_cache_lock);
760 btrfs_get_block_group(cache);
762 btrfs_queue_work(fs_info->caching_workers, &caching_ctl->work);
764 if (wait && caching_ctl)
765 ret = btrfs_caching_ctl_wait_done(cache, caching_ctl);
767 btrfs_put_caching_control(caching_ctl);
772 static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
774 u64 extra_flags = chunk_to_extended(flags) &
775 BTRFS_EXTENDED_PROFILE_MASK;
777 write_seqlock(&fs_info->profiles_lock);
778 if (flags & BTRFS_BLOCK_GROUP_DATA)
779 fs_info->avail_data_alloc_bits &= ~extra_flags;
780 if (flags & BTRFS_BLOCK_GROUP_METADATA)
781 fs_info->avail_metadata_alloc_bits &= ~extra_flags;
782 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
783 fs_info->avail_system_alloc_bits &= ~extra_flags;
784 write_sequnlock(&fs_info->profiles_lock);
788 * Clear incompat bits for the following feature(s):
790 * - RAID56 - in case there's neither RAID5 nor RAID6 profile block group
791 * in the whole filesystem
793 * - RAID1C34 - same as above for RAID1C3 and RAID1C4 block groups
795 static void clear_incompat_bg_bits(struct btrfs_fs_info *fs_info, u64 flags)
797 bool found_raid56 = false;
798 bool found_raid1c34 = false;
800 if ((flags & BTRFS_BLOCK_GROUP_RAID56_MASK) ||
801 (flags & BTRFS_BLOCK_GROUP_RAID1C3) ||
802 (flags & BTRFS_BLOCK_GROUP_RAID1C4)) {
803 struct list_head *head = &fs_info->space_info;
804 struct btrfs_space_info *sinfo;
806 list_for_each_entry_rcu(sinfo, head, list) {
807 down_read(&sinfo->groups_sem);
808 if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID5]))
810 if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID6]))
812 if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID1C3]))
813 found_raid1c34 = true;
814 if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID1C4]))
815 found_raid1c34 = true;
816 up_read(&sinfo->groups_sem);
819 btrfs_clear_fs_incompat(fs_info, RAID56);
821 btrfs_clear_fs_incompat(fs_info, RAID1C34);
825 static int remove_block_group_item(struct btrfs_trans_handle *trans,
826 struct btrfs_path *path,
827 struct btrfs_block_group *block_group)
829 struct btrfs_fs_info *fs_info = trans->fs_info;
830 struct btrfs_root *root;
831 struct btrfs_key key;
834 root = fs_info->extent_root;
835 key.objectid = block_group->start;
836 key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
837 key.offset = block_group->length;
839 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
845 ret = btrfs_del_item(trans, root, path);
849 int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
850 u64 group_start, struct extent_map *em)
852 struct btrfs_fs_info *fs_info = trans->fs_info;
853 struct btrfs_path *path;
854 struct btrfs_block_group *block_group;
855 struct btrfs_free_cluster *cluster;
857 struct kobject *kobj = NULL;
861 struct btrfs_caching_control *caching_ctl = NULL;
863 bool remove_rsv = false;
865 block_group = btrfs_lookup_block_group(fs_info, group_start);
866 BUG_ON(!block_group);
867 BUG_ON(!block_group->ro);
869 trace_btrfs_remove_block_group(block_group);
871 * Free the reserved super bytes from this block group before
874 btrfs_free_excluded_extents(block_group);
875 btrfs_free_ref_tree_range(fs_info, block_group->start,
876 block_group->length);
878 index = btrfs_bg_flags_to_raid_index(block_group->flags);
879 factor = btrfs_bg_type_to_factor(block_group->flags);
881 /* make sure this block group isn't part of an allocation cluster */
882 cluster = &fs_info->data_alloc_cluster;
883 spin_lock(&cluster->refill_lock);
884 btrfs_return_cluster_to_free_space(block_group, cluster);
885 spin_unlock(&cluster->refill_lock);
888 * make sure this block group isn't part of a metadata
891 cluster = &fs_info->meta_alloc_cluster;
892 spin_lock(&cluster->refill_lock);
893 btrfs_return_cluster_to_free_space(block_group, cluster);
894 spin_unlock(&cluster->refill_lock);
896 btrfs_clear_treelog_bg(block_group);
897 btrfs_clear_data_reloc_bg(block_group);
899 path = btrfs_alloc_path();
906 * get the inode first so any iput calls done for the io_list
907 * aren't the final iput (no unlinks allowed now)
909 inode = lookup_free_space_inode(block_group, path);
911 mutex_lock(&trans->transaction->cache_write_mutex);
913 * Make sure our free space cache IO is done before removing the
916 spin_lock(&trans->transaction->dirty_bgs_lock);
917 if (!list_empty(&block_group->io_list)) {
918 list_del_init(&block_group->io_list);
920 WARN_ON(!IS_ERR(inode) && inode != block_group->io_ctl.inode);
922 spin_unlock(&trans->transaction->dirty_bgs_lock);
923 btrfs_wait_cache_io(trans, block_group, path);
924 btrfs_put_block_group(block_group);
925 spin_lock(&trans->transaction->dirty_bgs_lock);
928 if (!list_empty(&block_group->dirty_list)) {
929 list_del_init(&block_group->dirty_list);
931 btrfs_put_block_group(block_group);
933 spin_unlock(&trans->transaction->dirty_bgs_lock);
934 mutex_unlock(&trans->transaction->cache_write_mutex);
936 ret = btrfs_remove_free_space_inode(trans, inode, block_group);
940 spin_lock(&fs_info->block_group_cache_lock);
941 rb_erase(&block_group->cache_node,
942 &fs_info->block_group_cache_tree);
943 RB_CLEAR_NODE(&block_group->cache_node);
945 /* Once for the block groups rbtree */
946 btrfs_put_block_group(block_group);
948 if (fs_info->first_logical_byte == block_group->start)
949 fs_info->first_logical_byte = (u64)-1;
950 spin_unlock(&fs_info->block_group_cache_lock);
952 down_write(&block_group->space_info->groups_sem);
954 * we must use list_del_init so people can check to see if they
955 * are still on the list after taking the semaphore
957 list_del_init(&block_group->list);
958 if (list_empty(&block_group->space_info->block_groups[index])) {
959 kobj = block_group->space_info->block_group_kobjs[index];
960 block_group->space_info->block_group_kobjs[index] = NULL;
961 clear_avail_alloc_bits(fs_info, block_group->flags);
963 up_write(&block_group->space_info->groups_sem);
964 clear_incompat_bg_bits(fs_info, block_group->flags);
970 if (block_group->has_caching_ctl)
971 caching_ctl = btrfs_get_caching_control(block_group);
972 if (block_group->cached == BTRFS_CACHE_STARTED)
973 btrfs_wait_block_group_cache_done(block_group);
974 if (block_group->has_caching_ctl) {
975 spin_lock(&fs_info->block_group_cache_lock);
977 struct btrfs_caching_control *ctl;
979 list_for_each_entry(ctl,
980 &fs_info->caching_block_groups, list)
981 if (ctl->block_group == block_group) {
983 refcount_inc(&caching_ctl->count);
988 list_del_init(&caching_ctl->list);
989 spin_unlock(&fs_info->block_group_cache_lock);
991 /* Once for the caching bgs list and once for us. */
992 btrfs_put_caching_control(caching_ctl);
993 btrfs_put_caching_control(caching_ctl);
997 spin_lock(&trans->transaction->dirty_bgs_lock);
998 WARN_ON(!list_empty(&block_group->dirty_list));
999 WARN_ON(!list_empty(&block_group->io_list));
1000 spin_unlock(&trans->transaction->dirty_bgs_lock);
1002 btrfs_remove_free_space_cache(block_group);
1004 spin_lock(&block_group->space_info->lock);
1005 list_del_init(&block_group->ro_list);
1007 if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
1008 WARN_ON(block_group->space_info->total_bytes
1009 < block_group->length);
1010 WARN_ON(block_group->space_info->bytes_readonly
1011 < block_group->length - block_group->zone_unusable);
1012 WARN_ON(block_group->space_info->bytes_zone_unusable
1013 < block_group->zone_unusable);
1014 WARN_ON(block_group->space_info->disk_total
1015 < block_group->length * factor);
1017 block_group->space_info->total_bytes -= block_group->length;
1018 block_group->space_info->bytes_readonly -=
1019 (block_group->length - block_group->zone_unusable);
1020 block_group->space_info->bytes_zone_unusable -=
1021 block_group->zone_unusable;
1022 block_group->space_info->disk_total -= block_group->length * factor;
1024 spin_unlock(&block_group->space_info->lock);
1027 * Remove the free space for the block group from the free space tree
1028 * and the block group's item from the extent tree before marking the
1029 * block group as removed. This is to prevent races with tasks that
1030 * freeze and unfreeze a block group, this task and another task
1031 * allocating a new block group - the unfreeze task ends up removing
1032 * the block group's extent map before the task calling this function
1033 * deletes the block group item from the extent tree, allowing for
1034 * another task to attempt to create another block group with the same
1035 * item key (and failing with -EEXIST and a transaction abort).
1037 ret = remove_block_group_free_space(trans, block_group);
1041 ret = remove_block_group_item(trans, path, block_group);
1045 spin_lock(&block_group->lock);
1046 block_group->removed = 1;
1048 * At this point trimming or scrub can't start on this block group,
1049 * because we removed the block group from the rbtree
1050 * fs_info->block_group_cache_tree so no one can't find it anymore and
1051 * even if someone already got this block group before we removed it
1052 * from the rbtree, they have already incremented block_group->frozen -
1053 * if they didn't, for the trimming case they won't find any free space
1054 * entries because we already removed them all when we called
1055 * btrfs_remove_free_space_cache().
1057 * And we must not remove the extent map from the fs_info->mapping_tree
1058 * to prevent the same logical address range and physical device space
1059 * ranges from being reused for a new block group. This is needed to
1060 * avoid races with trimming and scrub.
1062 * An fs trim operation (btrfs_trim_fs() / btrfs_ioctl_fitrim()) is
1063 * completely transactionless, so while it is trimming a range the
1064 * currently running transaction might finish and a new one start,
1065 * allowing for new block groups to be created that can reuse the same
1066 * physical device locations unless we take this special care.
1068 * There may also be an implicit trim operation if the file system
1069 * is mounted with -odiscard. The same protections must remain
1070 * in place until the extents have been discarded completely when
1071 * the transaction commit has completed.
1073 remove_em = (atomic_read(&block_group->frozen) == 0);
1074 spin_unlock(&block_group->lock);
1077 struct extent_map_tree *em_tree;
1079 em_tree = &fs_info->mapping_tree;
1080 write_lock(&em_tree->lock);
1081 remove_extent_mapping(em_tree, em);
1082 write_unlock(&em_tree->lock);
1083 /* once for the tree */
1084 free_extent_map(em);
1088 /* Once for the lookup reference */
1089 btrfs_put_block_group(block_group);
1091 btrfs_delayed_refs_rsv_release(fs_info, 1);
1092 btrfs_free_path(path);
1096 struct btrfs_trans_handle *btrfs_start_trans_remove_block_group(
1097 struct btrfs_fs_info *fs_info, const u64 chunk_offset)
1099 struct extent_map_tree *em_tree = &fs_info->mapping_tree;
1100 struct extent_map *em;
1101 struct map_lookup *map;
1102 unsigned int num_items;
1104 read_lock(&em_tree->lock);
1105 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
1106 read_unlock(&em_tree->lock);
1107 ASSERT(em && em->start == chunk_offset);
1110 * We need to reserve 3 + N units from the metadata space info in order
1111 * to remove a block group (done at btrfs_remove_chunk() and at
1112 * btrfs_remove_block_group()), which are used for:
1114 * 1 unit for adding the free space inode's orphan (located in the tree
1116 * 1 unit for deleting the block group item (located in the extent
1118 * 1 unit for deleting the free space item (located in tree of tree
1120 * N units for deleting N device extent items corresponding to each
1121 * stripe (located in the device tree).
1123 * In order to remove a block group we also need to reserve units in the
1124 * system space info in order to update the chunk tree (update one or
1125 * more device items and remove one chunk item), but this is done at
1126 * btrfs_remove_chunk() through a call to check_system_chunk().
1128 map = em->map_lookup;
1129 num_items = 3 + map->num_stripes;
1130 free_extent_map(em);
1132 return btrfs_start_transaction_fallback_global_rsv(fs_info->extent_root,
1137 * Mark block group @cache read-only, so later write won't happen to block
1140 * If @force is not set, this function will only mark the block group readonly
1141 * if we have enough free space (1M) in other metadata/system block groups.
1142 * If @force is not set, this function will mark the block group readonly
1143 * without checking free space.
1145 * NOTE: This function doesn't care if other block groups can contain all the
1146 * data in this block group. That check should be done by relocation routine,
1147 * not this function.
1149 static int inc_block_group_ro(struct btrfs_block_group *cache, int force)
1151 struct btrfs_space_info *sinfo = cache->space_info;
1155 spin_lock(&sinfo->lock);
1156 spin_lock(&cache->lock);
1158 if (cache->swap_extents) {
1169 num_bytes = cache->length - cache->reserved - cache->pinned -
1170 cache->bytes_super - cache->zone_unusable - cache->used;
1173 * Data never overcommits, even in mixed mode, so do just the straight
1174 * check of left over space in how much we have allocated.
1178 } else if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA) {
1179 u64 sinfo_used = btrfs_space_info_used(sinfo, true);
1182 * Here we make sure if we mark this bg RO, we still have enough
1183 * free space as buffer.
1185 if (sinfo_used + num_bytes <= sinfo->total_bytes)
1189 * We overcommit metadata, so we need to do the
1190 * btrfs_can_overcommit check here, and we need to pass in
1191 * BTRFS_RESERVE_NO_FLUSH to give ourselves the most amount of
1192 * leeway to allow us to mark this block group as read only.
1194 if (btrfs_can_overcommit(cache->fs_info, sinfo, num_bytes,
1195 BTRFS_RESERVE_NO_FLUSH))
1200 sinfo->bytes_readonly += num_bytes;
1201 if (btrfs_is_zoned(cache->fs_info)) {
1202 /* Migrate zone_unusable bytes to readonly */
1203 sinfo->bytes_readonly += cache->zone_unusable;
1204 sinfo->bytes_zone_unusable -= cache->zone_unusable;
1205 cache->zone_unusable = 0;
1208 list_add_tail(&cache->ro_list, &sinfo->ro_bgs);
1211 spin_unlock(&cache->lock);
1212 spin_unlock(&sinfo->lock);
1213 if (ret == -ENOSPC && btrfs_test_opt(cache->fs_info, ENOSPC_DEBUG)) {
1214 btrfs_info(cache->fs_info,
1215 "unable to make block group %llu ro", cache->start);
1216 btrfs_dump_space_info(cache->fs_info, cache->space_info, 0, 0);
1221 static bool clean_pinned_extents(struct btrfs_trans_handle *trans,
1222 struct btrfs_block_group *bg)
1224 struct btrfs_fs_info *fs_info = bg->fs_info;
1225 struct btrfs_transaction *prev_trans = NULL;
1226 const u64 start = bg->start;
1227 const u64 end = start + bg->length - 1;
1230 spin_lock(&fs_info->trans_lock);
1231 if (trans->transaction->list.prev != &fs_info->trans_list) {
1232 prev_trans = list_last_entry(&trans->transaction->list,
1233 struct btrfs_transaction, list);
1234 refcount_inc(&prev_trans->use_count);
1236 spin_unlock(&fs_info->trans_lock);
1239 * Hold the unused_bg_unpin_mutex lock to avoid racing with
1240 * btrfs_finish_extent_commit(). If we are at transaction N, another
1241 * task might be running finish_extent_commit() for the previous
1242 * transaction N - 1, and have seen a range belonging to the block
1243 * group in pinned_extents before we were able to clear the whole block
1244 * group range from pinned_extents. This means that task can lookup for
1245 * the block group after we unpinned it from pinned_extents and removed
1246 * it, leading to a BUG_ON() at unpin_extent_range().
1248 mutex_lock(&fs_info->unused_bg_unpin_mutex);
1250 ret = clear_extent_bits(&prev_trans->pinned_extents, start, end,
1256 ret = clear_extent_bits(&trans->transaction->pinned_extents, start, end,
1259 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
1261 btrfs_put_transaction(prev_trans);
1267 * Process the unused_bgs list and remove any that don't have any allocated
1268 * space inside of them.
1270 void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info)
1272 struct btrfs_block_group *block_group;
1273 struct btrfs_space_info *space_info;
1274 struct btrfs_trans_handle *trans;
1275 const bool async_trim_enabled = btrfs_test_opt(fs_info, DISCARD_ASYNC);
1278 if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags))
1282 * Long running balances can keep us blocked here for eternity, so
1283 * simply skip deletion if we're unable to get the mutex.
1285 if (!mutex_trylock(&fs_info->reclaim_bgs_lock))
1288 spin_lock(&fs_info->unused_bgs_lock);
1289 while (!list_empty(&fs_info->unused_bgs)) {
1292 block_group = list_first_entry(&fs_info->unused_bgs,
1293 struct btrfs_block_group,
1295 list_del_init(&block_group->bg_list);
1297 space_info = block_group->space_info;
1299 if (ret || btrfs_mixed_space_info(space_info)) {
1300 btrfs_put_block_group(block_group);
1303 spin_unlock(&fs_info->unused_bgs_lock);
1305 btrfs_discard_cancel_work(&fs_info->discard_ctl, block_group);
1307 /* Don't want to race with allocators so take the groups_sem */
1308 down_write(&space_info->groups_sem);
1311 * Async discard moves the final block group discard to be prior
1312 * to the unused_bgs code path. Therefore, if it's not fully
1313 * trimmed, punt it back to the async discard lists.
1315 if (btrfs_test_opt(fs_info, DISCARD_ASYNC) &&
1316 !btrfs_is_free_space_trimmed(block_group)) {
1317 trace_btrfs_skip_unused_block_group(block_group);
1318 up_write(&space_info->groups_sem);
1319 /* Requeue if we failed because of async discard */
1320 btrfs_discard_queue_work(&fs_info->discard_ctl,
1325 spin_lock(&block_group->lock);
1326 if (block_group->reserved || block_group->pinned ||
1327 block_group->used || block_group->ro ||
1328 list_is_singular(&block_group->list)) {
1330 * We want to bail if we made new allocations or have
1331 * outstanding allocations in this block group. We do
1332 * the ro check in case balance is currently acting on
1335 trace_btrfs_skip_unused_block_group(block_group);
1336 spin_unlock(&block_group->lock);
1337 up_write(&space_info->groups_sem);
1340 spin_unlock(&block_group->lock);
1342 /* We don't want to force the issue, only flip if it's ok. */
1343 ret = inc_block_group_ro(block_group, 0);
1344 up_write(&space_info->groups_sem);
1351 * Want to do this before we do anything else so we can recover
1352 * properly if we fail to join the transaction.
1354 trans = btrfs_start_trans_remove_block_group(fs_info,
1355 block_group->start);
1356 if (IS_ERR(trans)) {
1357 btrfs_dec_block_group_ro(block_group);
1358 ret = PTR_ERR(trans);
1363 * We could have pending pinned extents for this block group,
1364 * just delete them, we don't care about them anymore.
1366 if (!clean_pinned_extents(trans, block_group)) {
1367 btrfs_dec_block_group_ro(block_group);
1372 * At this point, the block_group is read only and should fail
1373 * new allocations. However, btrfs_finish_extent_commit() can
1374 * cause this block_group to be placed back on the discard
1375 * lists because now the block_group isn't fully discarded.
1376 * Bail here and try again later after discarding everything.
1378 spin_lock(&fs_info->discard_ctl.lock);
1379 if (!list_empty(&block_group->discard_list)) {
1380 spin_unlock(&fs_info->discard_ctl.lock);
1381 btrfs_dec_block_group_ro(block_group);
1382 btrfs_discard_queue_work(&fs_info->discard_ctl,
1386 spin_unlock(&fs_info->discard_ctl.lock);
1388 /* Reset pinned so btrfs_put_block_group doesn't complain */
1389 spin_lock(&space_info->lock);
1390 spin_lock(&block_group->lock);
1392 btrfs_space_info_update_bytes_pinned(fs_info, space_info,
1393 -block_group->pinned);
1394 space_info->bytes_readonly += block_group->pinned;
1395 block_group->pinned = 0;
1397 spin_unlock(&block_group->lock);
1398 spin_unlock(&space_info->lock);
1401 * The normal path here is an unused block group is passed here,
1402 * then trimming is handled in the transaction commit path.
1403 * Async discard interposes before this to do the trimming
1404 * before coming down the unused block group path as trimming
1405 * will no longer be done later in the transaction commit path.
1407 if (!async_trim_enabled && btrfs_test_opt(fs_info, DISCARD_ASYNC))
1411 * DISCARD can flip during remount. On zoned filesystems, we
1412 * need to reset sequential-required zones.
1414 trimming = btrfs_test_opt(fs_info, DISCARD_SYNC) ||
1415 btrfs_is_zoned(fs_info);
1417 /* Implicit trim during transaction commit. */
1419 btrfs_freeze_block_group(block_group);
1422 * Btrfs_remove_chunk will abort the transaction if things go
1425 ret = btrfs_remove_chunk(trans, block_group->start);
1429 btrfs_unfreeze_block_group(block_group);
1434 * If we're not mounted with -odiscard, we can just forget
1435 * about this block group. Otherwise we'll need to wait
1436 * until transaction commit to do the actual discard.
1439 spin_lock(&fs_info->unused_bgs_lock);
1441 * A concurrent scrub might have added us to the list
1442 * fs_info->unused_bgs, so use a list_move operation
1443 * to add the block group to the deleted_bgs list.
1445 list_move(&block_group->bg_list,
1446 &trans->transaction->deleted_bgs);
1447 spin_unlock(&fs_info->unused_bgs_lock);
1448 btrfs_get_block_group(block_group);
1451 btrfs_end_transaction(trans);
1453 btrfs_put_block_group(block_group);
1454 spin_lock(&fs_info->unused_bgs_lock);
1456 spin_unlock(&fs_info->unused_bgs_lock);
1457 mutex_unlock(&fs_info->reclaim_bgs_lock);
1461 btrfs_end_transaction(trans);
1462 mutex_unlock(&fs_info->reclaim_bgs_lock);
1463 btrfs_put_block_group(block_group);
1464 btrfs_discard_punt_unused_bgs_list(fs_info);
1467 void btrfs_mark_bg_unused(struct btrfs_block_group *bg)
1469 struct btrfs_fs_info *fs_info = bg->fs_info;
1471 spin_lock(&fs_info->unused_bgs_lock);
1472 if (list_empty(&bg->bg_list)) {
1473 btrfs_get_block_group(bg);
1474 trace_btrfs_add_unused_block_group(bg);
1475 list_add_tail(&bg->bg_list, &fs_info->unused_bgs);
1477 spin_unlock(&fs_info->unused_bgs_lock);
1480 void btrfs_reclaim_bgs_work(struct work_struct *work)
1482 struct btrfs_fs_info *fs_info =
1483 container_of(work, struct btrfs_fs_info, reclaim_bgs_work);
1484 struct btrfs_block_group *bg;
1485 struct btrfs_space_info *space_info;
1487 if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags))
1490 sb_start_write(fs_info->sb);
1492 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_BALANCE)) {
1493 sb_end_write(fs_info->sb);
1498 * Long running balances can keep us blocked here for eternity, so
1499 * simply skip reclaim if we're unable to get the mutex.
1501 if (!mutex_trylock(&fs_info->reclaim_bgs_lock)) {
1502 btrfs_exclop_finish(fs_info);
1503 sb_end_write(fs_info->sb);
1507 spin_lock(&fs_info->unused_bgs_lock);
1508 while (!list_empty(&fs_info->reclaim_bgs)) {
1512 bg = list_first_entry(&fs_info->reclaim_bgs,
1513 struct btrfs_block_group,
1515 list_del_init(&bg->bg_list);
1517 space_info = bg->space_info;
1518 spin_unlock(&fs_info->unused_bgs_lock);
1520 /* Don't race with allocators so take the groups_sem */
1521 down_write(&space_info->groups_sem);
1523 spin_lock(&bg->lock);
1524 if (bg->reserved || bg->pinned || bg->ro) {
1526 * We want to bail if we made new allocations or have
1527 * outstanding allocations in this block group. We do
1528 * the ro check in case balance is currently acting on
1531 spin_unlock(&bg->lock);
1532 up_write(&space_info->groups_sem);
1535 spin_unlock(&bg->lock);
1537 /* Get out fast, in case we're unmounting the filesystem */
1538 if (btrfs_fs_closing(fs_info)) {
1539 up_write(&space_info->groups_sem);
1544 * Cache the zone_unusable value before turning the block group
1545 * to read only. As soon as the blog group is read only it's
1546 * zone_unusable value gets moved to the block group's read-only
1547 * bytes and isn't available for calculations anymore.
1549 zone_unusable = bg->zone_unusable;
1550 ret = inc_block_group_ro(bg, 0);
1551 up_write(&space_info->groups_sem);
1556 "reclaiming chunk %llu with %llu%% used %llu%% unusable",
1557 bg->start, div_u64(bg->used * 100, bg->length),
1558 div64_u64(zone_unusable * 100, bg->length));
1559 trace_btrfs_reclaim_block_group(bg);
1560 ret = btrfs_relocate_chunk(fs_info, bg->start);
1562 btrfs_dec_block_group_ro(bg);
1563 btrfs_err(fs_info, "error relocating chunk %llu",
1568 btrfs_put_block_group(bg);
1569 spin_lock(&fs_info->unused_bgs_lock);
1571 spin_unlock(&fs_info->unused_bgs_lock);
1572 mutex_unlock(&fs_info->reclaim_bgs_lock);
1573 btrfs_exclop_finish(fs_info);
1574 sb_end_write(fs_info->sb);
1577 void btrfs_reclaim_bgs(struct btrfs_fs_info *fs_info)
1579 spin_lock(&fs_info->unused_bgs_lock);
1580 if (!list_empty(&fs_info->reclaim_bgs))
1581 queue_work(system_unbound_wq, &fs_info->reclaim_bgs_work);
1582 spin_unlock(&fs_info->unused_bgs_lock);
1585 void btrfs_mark_bg_to_reclaim(struct btrfs_block_group *bg)
1587 struct btrfs_fs_info *fs_info = bg->fs_info;
1589 spin_lock(&fs_info->unused_bgs_lock);
1590 if (list_empty(&bg->bg_list)) {
1591 btrfs_get_block_group(bg);
1592 trace_btrfs_add_reclaim_block_group(bg);
1593 list_add_tail(&bg->bg_list, &fs_info->reclaim_bgs);
1595 spin_unlock(&fs_info->unused_bgs_lock);
1598 static int read_bg_from_eb(struct btrfs_fs_info *fs_info, struct btrfs_key *key,
1599 struct btrfs_path *path)
1601 struct extent_map_tree *em_tree;
1602 struct extent_map *em;
1603 struct btrfs_block_group_item bg;
1604 struct extent_buffer *leaf;
1609 slot = path->slots[0];
1610 leaf = path->nodes[0];
1612 em_tree = &fs_info->mapping_tree;
1613 read_lock(&em_tree->lock);
1614 em = lookup_extent_mapping(em_tree, key->objectid, key->offset);
1615 read_unlock(&em_tree->lock);
1618 "logical %llu len %llu found bg but no related chunk",
1619 key->objectid, key->offset);
1623 if (em->start != key->objectid || em->len != key->offset) {
1625 "block group %llu len %llu mismatch with chunk %llu len %llu",
1626 key->objectid, key->offset, em->start, em->len);
1631 read_extent_buffer(leaf, &bg, btrfs_item_ptr_offset(leaf, slot),
1633 flags = btrfs_stack_block_group_flags(&bg) &
1634 BTRFS_BLOCK_GROUP_TYPE_MASK;
1636 if (flags != (em->map_lookup->type & BTRFS_BLOCK_GROUP_TYPE_MASK)) {
1638 "block group %llu len %llu type flags 0x%llx mismatch with chunk type flags 0x%llx",
1639 key->objectid, key->offset, flags,
1640 (BTRFS_BLOCK_GROUP_TYPE_MASK & em->map_lookup->type));
1645 free_extent_map(em);
1649 static int find_first_block_group(struct btrfs_fs_info *fs_info,
1650 struct btrfs_path *path,
1651 struct btrfs_key *key)
1653 struct btrfs_root *root = fs_info->extent_root;
1655 struct btrfs_key found_key;
1656 struct extent_buffer *leaf;
1659 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
1664 slot = path->slots[0];
1665 leaf = path->nodes[0];
1666 if (slot >= btrfs_header_nritems(leaf)) {
1667 ret = btrfs_next_leaf(root, path);
1674 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1676 if (found_key.objectid >= key->objectid &&
1677 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
1678 ret = read_bg_from_eb(fs_info, &found_key, path);
1688 static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1690 u64 extra_flags = chunk_to_extended(flags) &
1691 BTRFS_EXTENDED_PROFILE_MASK;
1693 write_seqlock(&fs_info->profiles_lock);
1694 if (flags & BTRFS_BLOCK_GROUP_DATA)
1695 fs_info->avail_data_alloc_bits |= extra_flags;
1696 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1697 fs_info->avail_metadata_alloc_bits |= extra_flags;
1698 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1699 fs_info->avail_system_alloc_bits |= extra_flags;
1700 write_sequnlock(&fs_info->profiles_lock);
1704 * Map a physical disk address to a list of logical addresses
1706 * @fs_info: the filesystem
1707 * @chunk_start: logical address of block group
1708 * @bdev: physical device to resolve, can be NULL to indicate any device
1709 * @physical: physical address to map to logical addresses
1710 * @logical: return array of logical addresses which map to @physical
1711 * @naddrs: length of @logical
1712 * @stripe_len: size of IO stripe for the given block group
1714 * Maps a particular @physical disk address to a list of @logical addresses.
1715 * Used primarily to exclude those portions of a block group that contain super
1718 int btrfs_rmap_block(struct btrfs_fs_info *fs_info, u64 chunk_start,
1719 struct block_device *bdev, u64 physical, u64 **logical,
1720 int *naddrs, int *stripe_len)
1722 struct extent_map *em;
1723 struct map_lookup *map;
1726 u64 data_stripe_length;
1731 em = btrfs_get_chunk_map(fs_info, chunk_start, 1);
1735 map = em->map_lookup;
1736 data_stripe_length = em->orig_block_len;
1737 io_stripe_size = map->stripe_len;
1738 chunk_start = em->start;
1740 /* For RAID5/6 adjust to a full IO stripe length */
1741 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
1742 io_stripe_size = map->stripe_len * nr_data_stripes(map);
1744 buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS);
1750 for (i = 0; i < map->num_stripes; i++) {
1751 bool already_inserted = false;
1756 if (!in_range(physical, map->stripes[i].physical,
1757 data_stripe_length))
1760 if (bdev && map->stripes[i].dev->bdev != bdev)
1763 stripe_nr = physical - map->stripes[i].physical;
1764 stripe_nr = div64_u64_rem(stripe_nr, map->stripe_len, &offset);
1766 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
1767 stripe_nr = stripe_nr * map->num_stripes + i;
1768 stripe_nr = div_u64(stripe_nr, map->sub_stripes);
1769 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
1770 stripe_nr = stripe_nr * map->num_stripes + i;
1773 * The remaining case would be for RAID56, multiply by
1774 * nr_data_stripes(). Alternatively, just use rmap_len below
1775 * instead of map->stripe_len
1778 bytenr = chunk_start + stripe_nr * io_stripe_size + offset;
1780 /* Ensure we don't add duplicate addresses */
1781 for (j = 0; j < nr; j++) {
1782 if (buf[j] == bytenr) {
1783 already_inserted = true;
1788 if (!already_inserted)
1794 *stripe_len = io_stripe_size;
1796 free_extent_map(em);
1800 static int exclude_super_stripes(struct btrfs_block_group *cache)
1802 struct btrfs_fs_info *fs_info = cache->fs_info;
1803 const bool zoned = btrfs_is_zoned(fs_info);
1809 if (cache->start < BTRFS_SUPER_INFO_OFFSET) {
1810 stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->start;
1811 cache->bytes_super += stripe_len;
1812 ret = btrfs_add_excluded_extent(fs_info, cache->start,
1818 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1819 bytenr = btrfs_sb_offset(i);
1820 ret = btrfs_rmap_block(fs_info, cache->start, NULL,
1821 bytenr, &logical, &nr, &stripe_len);
1825 /* Shouldn't have super stripes in sequential zones */
1828 "zoned: block group %llu must not contain super block",
1834 u64 len = min_t(u64, stripe_len,
1835 cache->start + cache->length - logical[nr]);
1837 cache->bytes_super += len;
1838 ret = btrfs_add_excluded_extent(fs_info, logical[nr],
1851 static void link_block_group(struct btrfs_block_group *cache)
1853 struct btrfs_space_info *space_info = cache->space_info;
1854 int index = btrfs_bg_flags_to_raid_index(cache->flags);
1856 down_write(&space_info->groups_sem);
1857 list_add_tail(&cache->list, &space_info->block_groups[index]);
1858 up_write(&space_info->groups_sem);
1861 static struct btrfs_block_group *btrfs_create_block_group_cache(
1862 struct btrfs_fs_info *fs_info, u64 start)
1864 struct btrfs_block_group *cache;
1866 cache = kzalloc(sizeof(*cache), GFP_NOFS);
1870 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
1872 if (!cache->free_space_ctl) {
1877 cache->start = start;
1879 cache->fs_info = fs_info;
1880 cache->full_stripe_len = btrfs_full_stripe_len(fs_info, start);
1882 cache->discard_index = BTRFS_DISCARD_INDEX_UNUSED;
1884 refcount_set(&cache->refs, 1);
1885 spin_lock_init(&cache->lock);
1886 init_rwsem(&cache->data_rwsem);
1887 INIT_LIST_HEAD(&cache->list);
1888 INIT_LIST_HEAD(&cache->cluster_list);
1889 INIT_LIST_HEAD(&cache->bg_list);
1890 INIT_LIST_HEAD(&cache->ro_list);
1891 INIT_LIST_HEAD(&cache->discard_list);
1892 INIT_LIST_HEAD(&cache->dirty_list);
1893 INIT_LIST_HEAD(&cache->io_list);
1894 btrfs_init_free_space_ctl(cache, cache->free_space_ctl);
1895 atomic_set(&cache->frozen, 0);
1896 mutex_init(&cache->free_space_lock);
1897 btrfs_init_full_stripe_locks_tree(&cache->full_stripe_locks_root);
1903 * Iterate all chunks and verify that each of them has the corresponding block
1906 static int check_chunk_block_group_mappings(struct btrfs_fs_info *fs_info)
1908 struct extent_map_tree *map_tree = &fs_info->mapping_tree;
1909 struct extent_map *em;
1910 struct btrfs_block_group *bg;
1915 read_lock(&map_tree->lock);
1917 * lookup_extent_mapping will return the first extent map
1918 * intersecting the range, so setting @len to 1 is enough to
1919 * get the first chunk.
1921 em = lookup_extent_mapping(map_tree, start, 1);
1922 read_unlock(&map_tree->lock);
1926 bg = btrfs_lookup_block_group(fs_info, em->start);
1929 "chunk start=%llu len=%llu doesn't have corresponding block group",
1930 em->start, em->len);
1932 free_extent_map(em);
1935 if (bg->start != em->start || bg->length != em->len ||
1936 (bg->flags & BTRFS_BLOCK_GROUP_TYPE_MASK) !=
1937 (em->map_lookup->type & BTRFS_BLOCK_GROUP_TYPE_MASK)) {
1939 "chunk start=%llu len=%llu flags=0x%llx doesn't match block group start=%llu len=%llu flags=0x%llx",
1941 em->map_lookup->type & BTRFS_BLOCK_GROUP_TYPE_MASK,
1942 bg->start, bg->length,
1943 bg->flags & BTRFS_BLOCK_GROUP_TYPE_MASK);
1945 free_extent_map(em);
1946 btrfs_put_block_group(bg);
1949 start = em->start + em->len;
1950 free_extent_map(em);
1951 btrfs_put_block_group(bg);
1956 static int read_one_block_group(struct btrfs_fs_info *info,
1957 struct btrfs_block_group_item *bgi,
1958 const struct btrfs_key *key,
1961 struct btrfs_block_group *cache;
1962 struct btrfs_space_info *space_info;
1963 const bool mixed = btrfs_fs_incompat(info, MIXED_GROUPS);
1966 ASSERT(key->type == BTRFS_BLOCK_GROUP_ITEM_KEY);
1968 cache = btrfs_create_block_group_cache(info, key->objectid);
1972 cache->length = key->offset;
1973 cache->used = btrfs_stack_block_group_used(bgi);
1974 cache->flags = btrfs_stack_block_group_flags(bgi);
1976 set_free_space_tree_thresholds(cache);
1980 * When we mount with old space cache, we need to
1981 * set BTRFS_DC_CLEAR and set dirty flag.
1983 * a) Setting 'BTRFS_DC_CLEAR' makes sure that we
1984 * truncate the old free space cache inode and
1986 * b) Setting 'dirty flag' makes sure that we flush
1987 * the new space cache info onto disk.
1989 if (btrfs_test_opt(info, SPACE_CACHE))
1990 cache->disk_cache_state = BTRFS_DC_CLEAR;
1992 if (!mixed && ((cache->flags & BTRFS_BLOCK_GROUP_METADATA) &&
1993 (cache->flags & BTRFS_BLOCK_GROUP_DATA))) {
1995 "bg %llu is a mixed block group but filesystem hasn't enabled mixed block groups",
2001 ret = btrfs_load_block_group_zone_info(cache, false);
2003 btrfs_err(info, "zoned: failed to load zone info of bg %llu",
2009 * We need to exclude the super stripes now so that the space info has
2010 * super bytes accounted for, otherwise we'll think we have more space
2011 * than we actually do.
2013 ret = exclude_super_stripes(cache);
2015 /* We may have excluded something, so call this just in case. */
2016 btrfs_free_excluded_extents(cache);
2021 * For zoned filesystem, space after the allocation offset is the only
2022 * free space for a block group. So, we don't need any caching work.
2023 * btrfs_calc_zone_unusable() will set the amount of free space and
2024 * zone_unusable space.
2026 * For regular filesystem, check for two cases, either we are full, and
2027 * therefore don't need to bother with the caching work since we won't
2028 * find any space, or we are empty, and we can just add all the space
2029 * in and be done with it. This saves us _a_lot_ of time, particularly
2032 if (btrfs_is_zoned(info)) {
2033 btrfs_calc_zone_unusable(cache);
2034 } else if (cache->length == cache->used) {
2035 cache->last_byte_to_unpin = (u64)-1;
2036 cache->cached = BTRFS_CACHE_FINISHED;
2037 btrfs_free_excluded_extents(cache);
2038 } else if (cache->used == 0) {
2039 cache->last_byte_to_unpin = (u64)-1;
2040 cache->cached = BTRFS_CACHE_FINISHED;
2041 add_new_free_space(cache, cache->start,
2042 cache->start + cache->length);
2043 btrfs_free_excluded_extents(cache);
2046 ret = btrfs_add_block_group_cache(info, cache);
2048 btrfs_remove_free_space_cache(cache);
2051 trace_btrfs_add_block_group(info, cache, 0);
2052 btrfs_update_space_info(info, cache->flags, cache->length,
2053 cache->used, cache->bytes_super,
2054 cache->zone_unusable, &space_info);
2056 cache->space_info = space_info;
2058 link_block_group(cache);
2060 set_avail_alloc_bits(info, cache->flags);
2061 if (btrfs_chunk_readonly(info, cache->start)) {
2062 inc_block_group_ro(cache, 1);
2063 } else if (cache->used == 0) {
2064 ASSERT(list_empty(&cache->bg_list));
2065 if (btrfs_test_opt(info, DISCARD_ASYNC))
2066 btrfs_discard_queue_work(&info->discard_ctl, cache);
2068 btrfs_mark_bg_unused(cache);
2072 btrfs_put_block_group(cache);
2076 static int fill_dummy_bgs(struct btrfs_fs_info *fs_info)
2078 struct extent_map_tree *em_tree = &fs_info->mapping_tree;
2079 struct btrfs_space_info *space_info;
2080 struct rb_node *node;
2083 for (node = rb_first_cached(&em_tree->map); node; node = rb_next(node)) {
2084 struct extent_map *em;
2085 struct map_lookup *map;
2086 struct btrfs_block_group *bg;
2088 em = rb_entry(node, struct extent_map, rb_node);
2089 map = em->map_lookup;
2090 bg = btrfs_create_block_group_cache(fs_info, em->start);
2096 /* Fill dummy cache as FULL */
2097 bg->length = em->len;
2098 bg->flags = map->type;
2099 bg->last_byte_to_unpin = (u64)-1;
2100 bg->cached = BTRFS_CACHE_FINISHED;
2102 bg->flags = map->type;
2103 ret = btrfs_add_block_group_cache(fs_info, bg);
2105 * We may have some valid block group cache added already, in
2106 * that case we skip to the next one.
2108 if (ret == -EEXIST) {
2110 btrfs_put_block_group(bg);
2115 btrfs_remove_free_space_cache(bg);
2116 btrfs_put_block_group(bg);
2120 btrfs_update_space_info(fs_info, bg->flags, em->len, em->len,
2122 bg->space_info = space_info;
2123 link_block_group(bg);
2125 set_avail_alloc_bits(fs_info, bg->flags);
2128 btrfs_init_global_block_rsv(fs_info);
2132 int btrfs_read_block_groups(struct btrfs_fs_info *info)
2134 struct btrfs_path *path;
2136 struct btrfs_block_group *cache;
2137 struct btrfs_space_info *space_info;
2138 struct btrfs_key key;
2143 * Either no extent root (with ibadroots rescue option) or we have
2144 * unsupported RO options. The fs can never be mounted read-write, so no
2145 * need to waste time searching block group items.
2147 * This also allows new extent tree related changes to be RO compat,
2148 * no need for a full incompat flag.
2150 if (!info->extent_root || (btrfs_super_compat_ro_flags(info->super_copy) &
2151 ~BTRFS_FEATURE_COMPAT_RO_SUPP))
2152 return fill_dummy_bgs(info);
2156 key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
2157 path = btrfs_alloc_path();
2161 cache_gen = btrfs_super_cache_generation(info->super_copy);
2162 if (btrfs_test_opt(info, SPACE_CACHE) &&
2163 btrfs_super_generation(info->super_copy) != cache_gen)
2165 if (btrfs_test_opt(info, CLEAR_CACHE))
2169 struct btrfs_block_group_item bgi;
2170 struct extent_buffer *leaf;
2173 ret = find_first_block_group(info, path, &key);
2179 leaf = path->nodes[0];
2180 slot = path->slots[0];
2182 read_extent_buffer(leaf, &bgi, btrfs_item_ptr_offset(leaf, slot),
2185 btrfs_item_key_to_cpu(leaf, &key, slot);
2186 btrfs_release_path(path);
2187 ret = read_one_block_group(info, &bgi, &key, need_clear);
2190 key.objectid += key.offset;
2193 btrfs_release_path(path);
2195 list_for_each_entry(space_info, &info->space_info, list) {
2198 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
2199 if (list_empty(&space_info->block_groups[i]))
2201 cache = list_first_entry(&space_info->block_groups[i],
2202 struct btrfs_block_group,
2204 btrfs_sysfs_add_block_group_type(cache);
2207 if (!(btrfs_get_alloc_profile(info, space_info->flags) &
2208 (BTRFS_BLOCK_GROUP_RAID10 |
2209 BTRFS_BLOCK_GROUP_RAID1_MASK |
2210 BTRFS_BLOCK_GROUP_RAID56_MASK |
2211 BTRFS_BLOCK_GROUP_DUP)))
2214 * Avoid allocating from un-mirrored block group if there are
2215 * mirrored block groups.
2217 list_for_each_entry(cache,
2218 &space_info->block_groups[BTRFS_RAID_RAID0],
2220 inc_block_group_ro(cache, 1);
2221 list_for_each_entry(cache,
2222 &space_info->block_groups[BTRFS_RAID_SINGLE],
2224 inc_block_group_ro(cache, 1);
2227 btrfs_init_global_block_rsv(info);
2228 ret = check_chunk_block_group_mappings(info);
2230 btrfs_free_path(path);
2232 * We've hit some error while reading the extent tree, and have
2233 * rescue=ibadroots mount option.
2234 * Try to fill the tree using dummy block groups so that the user can
2235 * continue to mount and grab their data.
2237 if (ret && btrfs_test_opt(info, IGNOREBADROOTS))
2238 ret = fill_dummy_bgs(info);
2243 * This function, insert_block_group_item(), belongs to the phase 2 of chunk
2246 * See the comment at btrfs_chunk_alloc() for details about the chunk allocation
2249 static int insert_block_group_item(struct btrfs_trans_handle *trans,
2250 struct btrfs_block_group *block_group)
2252 struct btrfs_fs_info *fs_info = trans->fs_info;
2253 struct btrfs_block_group_item bgi;
2254 struct btrfs_root *root;
2255 struct btrfs_key key;
2257 spin_lock(&block_group->lock);
2258 btrfs_set_stack_block_group_used(&bgi, block_group->used);
2259 btrfs_set_stack_block_group_chunk_objectid(&bgi,
2260 BTRFS_FIRST_CHUNK_TREE_OBJECTID);
2261 btrfs_set_stack_block_group_flags(&bgi, block_group->flags);
2262 key.objectid = block_group->start;
2263 key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
2264 key.offset = block_group->length;
2265 spin_unlock(&block_group->lock);
2267 root = fs_info->extent_root;
2268 return btrfs_insert_item(trans, root, &key, &bgi, sizeof(bgi));
2271 static int insert_dev_extent(struct btrfs_trans_handle *trans,
2272 struct btrfs_device *device, u64 chunk_offset,
2273 u64 start, u64 num_bytes)
2275 struct btrfs_fs_info *fs_info = device->fs_info;
2276 struct btrfs_root *root = fs_info->dev_root;
2277 struct btrfs_path *path;
2278 struct btrfs_dev_extent *extent;
2279 struct extent_buffer *leaf;
2280 struct btrfs_key key;
2283 WARN_ON(!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state));
2284 WARN_ON(test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state));
2285 path = btrfs_alloc_path();
2289 key.objectid = device->devid;
2290 key.type = BTRFS_DEV_EXTENT_KEY;
2292 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*extent));
2296 leaf = path->nodes[0];
2297 extent = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_extent);
2298 btrfs_set_dev_extent_chunk_tree(leaf, extent, BTRFS_CHUNK_TREE_OBJECTID);
2299 btrfs_set_dev_extent_chunk_objectid(leaf, extent,
2300 BTRFS_FIRST_CHUNK_TREE_OBJECTID);
2301 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
2303 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
2304 btrfs_mark_buffer_dirty(leaf);
2306 btrfs_free_path(path);
2311 * This function belongs to phase 2.
2313 * See the comment at btrfs_chunk_alloc() for details about the chunk allocation
2316 static int insert_dev_extents(struct btrfs_trans_handle *trans,
2317 u64 chunk_offset, u64 chunk_size)
2319 struct btrfs_fs_info *fs_info = trans->fs_info;
2320 struct btrfs_device *device;
2321 struct extent_map *em;
2322 struct map_lookup *map;
2328 em = btrfs_get_chunk_map(fs_info, chunk_offset, chunk_size);
2332 map = em->map_lookup;
2333 stripe_size = em->orig_block_len;
2336 * Take the device list mutex to prevent races with the final phase of
2337 * a device replace operation that replaces the device object associated
2338 * with the map's stripes, because the device object's id can change
2339 * at any time during that final phase of the device replace operation
2340 * (dev-replace.c:btrfs_dev_replace_finishing()), so we could grab the
2341 * replaced device and then see it with an ID of BTRFS_DEV_REPLACE_DEVID,
2342 * resulting in persisting a device extent item with such ID.
2344 mutex_lock(&fs_info->fs_devices->device_list_mutex);
2345 for (i = 0; i < map->num_stripes; i++) {
2346 device = map->stripes[i].dev;
2347 dev_offset = map->stripes[i].physical;
2349 ret = insert_dev_extent(trans, device, chunk_offset, dev_offset,
2354 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
2356 free_extent_map(em);
2361 * This function, btrfs_create_pending_block_groups(), belongs to the phase 2 of
2364 * See the comment at btrfs_chunk_alloc() for details about the chunk allocation
2367 void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans)
2369 struct btrfs_fs_info *fs_info = trans->fs_info;
2370 struct btrfs_block_group *block_group;
2373 while (!list_empty(&trans->new_bgs)) {
2376 block_group = list_first_entry(&trans->new_bgs,
2377 struct btrfs_block_group,
2382 index = btrfs_bg_flags_to_raid_index(block_group->flags);
2384 ret = insert_block_group_item(trans, block_group);
2386 btrfs_abort_transaction(trans, ret);
2387 if (!block_group->chunk_item_inserted) {
2388 mutex_lock(&fs_info->chunk_mutex);
2389 ret = btrfs_chunk_alloc_add_chunk_item(trans, block_group);
2390 mutex_unlock(&fs_info->chunk_mutex);
2392 btrfs_abort_transaction(trans, ret);
2394 ret = insert_dev_extents(trans, block_group->start,
2395 block_group->length);
2397 btrfs_abort_transaction(trans, ret);
2398 add_block_group_free_space(trans, block_group);
2401 * If we restriped during balance, we may have added a new raid
2402 * type, so now add the sysfs entries when it is safe to do so.
2403 * We don't have to worry about locking here as it's handled in
2404 * btrfs_sysfs_add_block_group_type.
2406 if (block_group->space_info->block_group_kobjs[index] == NULL)
2407 btrfs_sysfs_add_block_group_type(block_group);
2409 /* Already aborted the transaction if it failed. */
2411 btrfs_delayed_refs_rsv_release(fs_info, 1);
2412 list_del_init(&block_group->bg_list);
2414 btrfs_trans_release_chunk_metadata(trans);
2417 struct btrfs_block_group *btrfs_make_block_group(struct btrfs_trans_handle *trans,
2418 u64 bytes_used, u64 type,
2419 u64 chunk_offset, u64 size)
2421 struct btrfs_fs_info *fs_info = trans->fs_info;
2422 struct btrfs_block_group *cache;
2425 btrfs_set_log_full_commit(trans);
2427 cache = btrfs_create_block_group_cache(fs_info, chunk_offset);
2429 return ERR_PTR(-ENOMEM);
2431 cache->length = size;
2432 set_free_space_tree_thresholds(cache);
2433 cache->used = bytes_used;
2434 cache->flags = type;
2435 cache->last_byte_to_unpin = (u64)-1;
2436 cache->cached = BTRFS_CACHE_FINISHED;
2437 if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE))
2438 cache->needs_free_space = 1;
2440 ret = btrfs_load_block_group_zone_info(cache, true);
2442 btrfs_put_block_group(cache);
2443 return ERR_PTR(ret);
2446 ret = exclude_super_stripes(cache);
2448 /* We may have excluded something, so call this just in case */
2449 btrfs_free_excluded_extents(cache);
2450 btrfs_put_block_group(cache);
2451 return ERR_PTR(ret);
2454 add_new_free_space(cache, chunk_offset, chunk_offset + size);
2456 btrfs_free_excluded_extents(cache);
2458 #ifdef CONFIG_BTRFS_DEBUG
2459 if (btrfs_should_fragment_free_space(cache)) {
2460 u64 new_bytes_used = size - bytes_used;
2462 bytes_used += new_bytes_used >> 1;
2463 fragment_free_space(cache);
2467 * Ensure the corresponding space_info object is created and
2468 * assigned to our block group. We want our bg to be added to the rbtree
2469 * with its ->space_info set.
2471 cache->space_info = btrfs_find_space_info(fs_info, cache->flags);
2472 ASSERT(cache->space_info);
2474 ret = btrfs_add_block_group_cache(fs_info, cache);
2476 btrfs_remove_free_space_cache(cache);
2477 btrfs_put_block_group(cache);
2478 return ERR_PTR(ret);
2482 * Now that our block group has its ->space_info set and is inserted in
2483 * the rbtree, update the space info's counters.
2485 trace_btrfs_add_block_group(fs_info, cache, 1);
2486 btrfs_update_space_info(fs_info, cache->flags, size, bytes_used,
2487 cache->bytes_super, 0, &cache->space_info);
2488 btrfs_update_global_block_rsv(fs_info);
2490 link_block_group(cache);
2492 list_add_tail(&cache->bg_list, &trans->new_bgs);
2493 trans->delayed_ref_updates++;
2494 btrfs_update_delayed_refs_rsv(trans);
2496 set_avail_alloc_bits(fs_info, type);
2501 * Mark one block group RO, can be called several times for the same block
2504 * @cache: the destination block group
2505 * @do_chunk_alloc: whether need to do chunk pre-allocation, this is to
2506 * ensure we still have some free space after marking this
2509 int btrfs_inc_block_group_ro(struct btrfs_block_group *cache,
2510 bool do_chunk_alloc)
2512 struct btrfs_fs_info *fs_info = cache->fs_info;
2513 struct btrfs_trans_handle *trans;
2516 bool dirty_bg_running;
2519 * This can only happen when we are doing read-only scrub on read-only
2521 * In that case we should not start a new transaction on read-only fs.
2522 * Thus here we skip all chunk allocations.
2524 if (sb_rdonly(fs_info->sb)) {
2525 mutex_lock(&fs_info->ro_block_group_mutex);
2526 ret = inc_block_group_ro(cache, 0);
2527 mutex_unlock(&fs_info->ro_block_group_mutex);
2532 trans = btrfs_join_transaction(fs_info->extent_root);
2534 return PTR_ERR(trans);
2536 dirty_bg_running = false;
2539 * We're not allowed to set block groups readonly after the dirty
2540 * block group cache has started writing. If it already started,
2541 * back off and let this transaction commit.
2543 mutex_lock(&fs_info->ro_block_group_mutex);
2544 if (test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &trans->transaction->flags)) {
2545 u64 transid = trans->transid;
2547 mutex_unlock(&fs_info->ro_block_group_mutex);
2548 btrfs_end_transaction(trans);
2550 ret = btrfs_wait_for_commit(fs_info, transid);
2553 dirty_bg_running = true;
2555 } while (dirty_bg_running);
2557 if (do_chunk_alloc) {
2559 * If we are changing raid levels, try to allocate a
2560 * corresponding block group with the new raid level.
2562 alloc_flags = btrfs_get_alloc_profile(fs_info, cache->flags);
2563 if (alloc_flags != cache->flags) {
2564 ret = btrfs_chunk_alloc(trans, alloc_flags,
2567 * ENOSPC is allowed here, we may have enough space
2568 * already allocated at the new raid level to carry on
2577 ret = inc_block_group_ro(cache, 0);
2578 if (!do_chunk_alloc || ret == -ETXTBSY)
2582 alloc_flags = btrfs_get_alloc_profile(fs_info, cache->space_info->flags);
2583 ret = btrfs_chunk_alloc(trans, alloc_flags, CHUNK_ALLOC_FORCE);
2586 ret = inc_block_group_ro(cache, 0);
2587 if (ret == -ETXTBSY)
2590 if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
2591 alloc_flags = btrfs_get_alloc_profile(fs_info, cache->flags);
2592 mutex_lock(&fs_info->chunk_mutex);
2593 check_system_chunk(trans, alloc_flags);
2594 mutex_unlock(&fs_info->chunk_mutex);
2597 mutex_unlock(&fs_info->ro_block_group_mutex);
2599 btrfs_end_transaction(trans);
2603 void btrfs_dec_block_group_ro(struct btrfs_block_group *cache)
2605 struct btrfs_space_info *sinfo = cache->space_info;
2610 spin_lock(&sinfo->lock);
2611 spin_lock(&cache->lock);
2613 if (btrfs_is_zoned(cache->fs_info)) {
2614 /* Migrate zone_unusable bytes back */
2615 cache->zone_unusable = cache->alloc_offset - cache->used;
2616 sinfo->bytes_zone_unusable += cache->zone_unusable;
2617 sinfo->bytes_readonly -= cache->zone_unusable;
2619 num_bytes = cache->length - cache->reserved -
2620 cache->pinned - cache->bytes_super -
2621 cache->zone_unusable - cache->used;
2622 sinfo->bytes_readonly -= num_bytes;
2623 list_del_init(&cache->ro_list);
2625 spin_unlock(&cache->lock);
2626 spin_unlock(&sinfo->lock);
2629 static int update_block_group_item(struct btrfs_trans_handle *trans,
2630 struct btrfs_path *path,
2631 struct btrfs_block_group *cache)
2633 struct btrfs_fs_info *fs_info = trans->fs_info;
2635 struct btrfs_root *root = fs_info->extent_root;
2637 struct extent_buffer *leaf;
2638 struct btrfs_block_group_item bgi;
2639 struct btrfs_key key;
2641 key.objectid = cache->start;
2642 key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
2643 key.offset = cache->length;
2645 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2652 leaf = path->nodes[0];
2653 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
2654 btrfs_set_stack_block_group_used(&bgi, cache->used);
2655 btrfs_set_stack_block_group_chunk_objectid(&bgi,
2656 BTRFS_FIRST_CHUNK_TREE_OBJECTID);
2657 btrfs_set_stack_block_group_flags(&bgi, cache->flags);
2658 write_extent_buffer(leaf, &bgi, bi, sizeof(bgi));
2659 btrfs_mark_buffer_dirty(leaf);
2661 btrfs_release_path(path);
2666 static int cache_save_setup(struct btrfs_block_group *block_group,
2667 struct btrfs_trans_handle *trans,
2668 struct btrfs_path *path)
2670 struct btrfs_fs_info *fs_info = block_group->fs_info;
2671 struct btrfs_root *root = fs_info->tree_root;
2672 struct inode *inode = NULL;
2673 struct extent_changeset *data_reserved = NULL;
2675 int dcs = BTRFS_DC_ERROR;
2680 if (!btrfs_test_opt(fs_info, SPACE_CACHE))
2684 * If this block group is smaller than 100 megs don't bother caching the
2687 if (block_group->length < (100 * SZ_1M)) {
2688 spin_lock(&block_group->lock);
2689 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
2690 spin_unlock(&block_group->lock);
2694 if (TRANS_ABORTED(trans))
2697 inode = lookup_free_space_inode(block_group, path);
2698 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
2699 ret = PTR_ERR(inode);
2700 btrfs_release_path(path);
2704 if (IS_ERR(inode)) {
2708 if (block_group->ro)
2711 ret = create_free_space_inode(trans, block_group, path);
2718 * We want to set the generation to 0, that way if anything goes wrong
2719 * from here on out we know not to trust this cache when we load up next
2722 BTRFS_I(inode)->generation = 0;
2723 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
2726 * So theoretically we could recover from this, simply set the
2727 * super cache generation to 0 so we know to invalidate the
2728 * cache, but then we'd have to keep track of the block groups
2729 * that fail this way so we know we _have_ to reset this cache
2730 * before the next commit or risk reading stale cache. So to
2731 * limit our exposure to horrible edge cases lets just abort the
2732 * transaction, this only happens in really bad situations
2735 btrfs_abort_transaction(trans, ret);
2740 /* We've already setup this transaction, go ahead and exit */
2741 if (block_group->cache_generation == trans->transid &&
2742 i_size_read(inode)) {
2743 dcs = BTRFS_DC_SETUP;
2747 if (i_size_read(inode) > 0) {
2748 ret = btrfs_check_trunc_cache_free_space(fs_info,
2749 &fs_info->global_block_rsv);
2753 ret = btrfs_truncate_free_space_cache(trans, NULL, inode);
2758 spin_lock(&block_group->lock);
2759 if (block_group->cached != BTRFS_CACHE_FINISHED ||
2760 !btrfs_test_opt(fs_info, SPACE_CACHE)) {
2762 * don't bother trying to write stuff out _if_
2763 * a) we're not cached,
2764 * b) we're with nospace_cache mount option,
2765 * c) we're with v2 space_cache (FREE_SPACE_TREE).
2767 dcs = BTRFS_DC_WRITTEN;
2768 spin_unlock(&block_group->lock);
2771 spin_unlock(&block_group->lock);
2774 * We hit an ENOSPC when setting up the cache in this transaction, just
2775 * skip doing the setup, we've already cleared the cache so we're safe.
2777 if (test_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags)) {
2783 * Try to preallocate enough space based on how big the block group is.
2784 * Keep in mind this has to include any pinned space which could end up
2785 * taking up quite a bit since it's not folded into the other space
2788 cache_size = div_u64(block_group->length, SZ_256M);
2793 cache_size *= fs_info->sectorsize;
2795 ret = btrfs_check_data_free_space(BTRFS_I(inode), &data_reserved, 0,
2800 ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, cache_size,
2801 cache_size, cache_size,
2804 * Our cache requires contiguous chunks so that we don't modify a bunch
2805 * of metadata or split extents when writing the cache out, which means
2806 * we can enospc if we are heavily fragmented in addition to just normal
2807 * out of space conditions. So if we hit this just skip setting up any
2808 * other block groups for this transaction, maybe we'll unpin enough
2809 * space the next time around.
2812 dcs = BTRFS_DC_SETUP;
2813 else if (ret == -ENOSPC)
2814 set_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags);
2819 btrfs_release_path(path);
2821 spin_lock(&block_group->lock);
2822 if (!ret && dcs == BTRFS_DC_SETUP)
2823 block_group->cache_generation = trans->transid;
2824 block_group->disk_cache_state = dcs;
2825 spin_unlock(&block_group->lock);
2827 extent_changeset_free(data_reserved);
2831 int btrfs_setup_space_cache(struct btrfs_trans_handle *trans)
2833 struct btrfs_fs_info *fs_info = trans->fs_info;
2834 struct btrfs_block_group *cache, *tmp;
2835 struct btrfs_transaction *cur_trans = trans->transaction;
2836 struct btrfs_path *path;
2838 if (list_empty(&cur_trans->dirty_bgs) ||
2839 !btrfs_test_opt(fs_info, SPACE_CACHE))
2842 path = btrfs_alloc_path();
2846 /* Could add new block groups, use _safe just in case */
2847 list_for_each_entry_safe(cache, tmp, &cur_trans->dirty_bgs,
2849 if (cache->disk_cache_state == BTRFS_DC_CLEAR)
2850 cache_save_setup(cache, trans, path);
2853 btrfs_free_path(path);
2858 * Transaction commit does final block group cache writeback during a critical
2859 * section where nothing is allowed to change the FS. This is required in
2860 * order for the cache to actually match the block group, but can introduce a
2861 * lot of latency into the commit.
2863 * So, btrfs_start_dirty_block_groups is here to kick off block group cache IO.
2864 * There's a chance we'll have to redo some of it if the block group changes
2865 * again during the commit, but it greatly reduces the commit latency by
2866 * getting rid of the easy block groups while we're still allowing others to
2869 int btrfs_start_dirty_block_groups(struct btrfs_trans_handle *trans)
2871 struct btrfs_fs_info *fs_info = trans->fs_info;
2872 struct btrfs_block_group *cache;
2873 struct btrfs_transaction *cur_trans = trans->transaction;
2876 struct btrfs_path *path = NULL;
2878 struct list_head *io = &cur_trans->io_bgs;
2881 spin_lock(&cur_trans->dirty_bgs_lock);
2882 if (list_empty(&cur_trans->dirty_bgs)) {
2883 spin_unlock(&cur_trans->dirty_bgs_lock);
2886 list_splice_init(&cur_trans->dirty_bgs, &dirty);
2887 spin_unlock(&cur_trans->dirty_bgs_lock);
2890 /* Make sure all the block groups on our dirty list actually exist */
2891 btrfs_create_pending_block_groups(trans);
2894 path = btrfs_alloc_path();
2902 * cache_write_mutex is here only to save us from balance or automatic
2903 * removal of empty block groups deleting this block group while we are
2904 * writing out the cache
2906 mutex_lock(&trans->transaction->cache_write_mutex);
2907 while (!list_empty(&dirty)) {
2908 bool drop_reserve = true;
2910 cache = list_first_entry(&dirty, struct btrfs_block_group,
2913 * This can happen if something re-dirties a block group that
2914 * is already under IO. Just wait for it to finish and then do
2917 if (!list_empty(&cache->io_list)) {
2918 list_del_init(&cache->io_list);
2919 btrfs_wait_cache_io(trans, cache, path);
2920 btrfs_put_block_group(cache);
2925 * btrfs_wait_cache_io uses the cache->dirty_list to decide if
2926 * it should update the cache_state. Don't delete until after
2929 * Since we're not running in the commit critical section
2930 * we need the dirty_bgs_lock to protect from update_block_group
2932 spin_lock(&cur_trans->dirty_bgs_lock);
2933 list_del_init(&cache->dirty_list);
2934 spin_unlock(&cur_trans->dirty_bgs_lock);
2938 cache_save_setup(cache, trans, path);
2940 if (cache->disk_cache_state == BTRFS_DC_SETUP) {
2941 cache->io_ctl.inode = NULL;
2942 ret = btrfs_write_out_cache(trans, cache, path);
2943 if (ret == 0 && cache->io_ctl.inode) {
2947 * The cache_write_mutex is protecting the
2948 * io_list, also refer to the definition of
2949 * btrfs_transaction::io_bgs for more details
2951 list_add_tail(&cache->io_list, io);
2954 * If we failed to write the cache, the
2955 * generation will be bad and life goes on
2961 ret = update_block_group_item(trans, path, cache);
2963 * Our block group might still be attached to the list
2964 * of new block groups in the transaction handle of some
2965 * other task (struct btrfs_trans_handle->new_bgs). This
2966 * means its block group item isn't yet in the extent
2967 * tree. If this happens ignore the error, as we will
2968 * try again later in the critical section of the
2969 * transaction commit.
2971 if (ret == -ENOENT) {
2973 spin_lock(&cur_trans->dirty_bgs_lock);
2974 if (list_empty(&cache->dirty_list)) {
2975 list_add_tail(&cache->dirty_list,
2976 &cur_trans->dirty_bgs);
2977 btrfs_get_block_group(cache);
2978 drop_reserve = false;
2980 spin_unlock(&cur_trans->dirty_bgs_lock);
2982 btrfs_abort_transaction(trans, ret);
2986 /* If it's not on the io list, we need to put the block group */
2988 btrfs_put_block_group(cache);
2990 btrfs_delayed_refs_rsv_release(fs_info, 1);
2992 * Avoid blocking other tasks for too long. It might even save
2993 * us from writing caches for block groups that are going to be
2996 mutex_unlock(&trans->transaction->cache_write_mutex);
2999 mutex_lock(&trans->transaction->cache_write_mutex);
3001 mutex_unlock(&trans->transaction->cache_write_mutex);
3004 * Go through delayed refs for all the stuff we've just kicked off
3005 * and then loop back (just once)
3008 ret = btrfs_run_delayed_refs(trans, 0);
3009 if (!ret && loops == 0) {
3011 spin_lock(&cur_trans->dirty_bgs_lock);
3012 list_splice_init(&cur_trans->dirty_bgs, &dirty);
3014 * dirty_bgs_lock protects us from concurrent block group
3015 * deletes too (not just cache_write_mutex).
3017 if (!list_empty(&dirty)) {
3018 spin_unlock(&cur_trans->dirty_bgs_lock);
3021 spin_unlock(&cur_trans->dirty_bgs_lock);
3025 spin_lock(&cur_trans->dirty_bgs_lock);
3026 list_splice_init(&dirty, &cur_trans->dirty_bgs);
3027 spin_unlock(&cur_trans->dirty_bgs_lock);
3028 btrfs_cleanup_dirty_bgs(cur_trans, fs_info);
3031 btrfs_free_path(path);
3035 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans)
3037 struct btrfs_fs_info *fs_info = trans->fs_info;
3038 struct btrfs_block_group *cache;
3039 struct btrfs_transaction *cur_trans = trans->transaction;
3042 struct btrfs_path *path;
3043 struct list_head *io = &cur_trans->io_bgs;
3045 path = btrfs_alloc_path();
3050 * Even though we are in the critical section of the transaction commit,
3051 * we can still have concurrent tasks adding elements to this
3052 * transaction's list of dirty block groups. These tasks correspond to
3053 * endio free space workers started when writeback finishes for a
3054 * space cache, which run inode.c:btrfs_finish_ordered_io(), and can
3055 * allocate new block groups as a result of COWing nodes of the root
3056 * tree when updating the free space inode. The writeback for the space
3057 * caches is triggered by an earlier call to
3058 * btrfs_start_dirty_block_groups() and iterations of the following
3060 * Also we want to do the cache_save_setup first and then run the
3061 * delayed refs to make sure we have the best chance at doing this all
3064 spin_lock(&cur_trans->dirty_bgs_lock);
3065 while (!list_empty(&cur_trans->dirty_bgs)) {
3066 cache = list_first_entry(&cur_trans->dirty_bgs,
3067 struct btrfs_block_group,
3071 * This can happen if cache_save_setup re-dirties a block group
3072 * that is already under IO. Just wait for it to finish and
3073 * then do it all again
3075 if (!list_empty(&cache->io_list)) {
3076 spin_unlock(&cur_trans->dirty_bgs_lock);
3077 list_del_init(&cache->io_list);
3078 btrfs_wait_cache_io(trans, cache, path);
3079 btrfs_put_block_group(cache);
3080 spin_lock(&cur_trans->dirty_bgs_lock);
3084 * Don't remove from the dirty list until after we've waited on
3087 list_del_init(&cache->dirty_list);
3088 spin_unlock(&cur_trans->dirty_bgs_lock);
3091 cache_save_setup(cache, trans, path);
3094 ret = btrfs_run_delayed_refs(trans,
3095 (unsigned long) -1);
3097 if (!ret && cache->disk_cache_state == BTRFS_DC_SETUP) {
3098 cache->io_ctl.inode = NULL;
3099 ret = btrfs_write_out_cache(trans, cache, path);
3100 if (ret == 0 && cache->io_ctl.inode) {
3102 list_add_tail(&cache->io_list, io);
3105 * If we failed to write the cache, the
3106 * generation will be bad and life goes on
3112 ret = update_block_group_item(trans, path, cache);
3114 * One of the free space endio workers might have
3115 * created a new block group while updating a free space
3116 * cache's inode (at inode.c:btrfs_finish_ordered_io())
3117 * and hasn't released its transaction handle yet, in
3118 * which case the new block group is still attached to
3119 * its transaction handle and its creation has not
3120 * finished yet (no block group item in the extent tree
3121 * yet, etc). If this is the case, wait for all free
3122 * space endio workers to finish and retry. This is a
3123 * very rare case so no need for a more efficient and
3126 if (ret == -ENOENT) {
3127 wait_event(cur_trans->writer_wait,
3128 atomic_read(&cur_trans->num_writers) == 1);
3129 ret = update_block_group_item(trans, path, cache);
3132 btrfs_abort_transaction(trans, ret);
3135 /* If its not on the io list, we need to put the block group */
3137 btrfs_put_block_group(cache);
3138 btrfs_delayed_refs_rsv_release(fs_info, 1);
3139 spin_lock(&cur_trans->dirty_bgs_lock);
3141 spin_unlock(&cur_trans->dirty_bgs_lock);
3144 * Refer to the definition of io_bgs member for details why it's safe
3145 * to use it without any locking
3147 while (!list_empty(io)) {
3148 cache = list_first_entry(io, struct btrfs_block_group,
3150 list_del_init(&cache->io_list);
3151 btrfs_wait_cache_io(trans, cache, path);
3152 btrfs_put_block_group(cache);
3155 btrfs_free_path(path);
3159 int btrfs_update_block_group(struct btrfs_trans_handle *trans,
3160 u64 bytenr, u64 num_bytes, int alloc)
3162 struct btrfs_fs_info *info = trans->fs_info;
3163 struct btrfs_block_group *cache = NULL;
3164 u64 total = num_bytes;
3170 /* Block accounting for super block */
3171 spin_lock(&info->delalloc_root_lock);
3172 old_val = btrfs_super_bytes_used(info->super_copy);
3174 old_val += num_bytes;
3176 old_val -= num_bytes;
3177 btrfs_set_super_bytes_used(info->super_copy, old_val);
3178 spin_unlock(&info->delalloc_root_lock);
3181 cache = btrfs_lookup_block_group(info, bytenr);
3186 factor = btrfs_bg_type_to_factor(cache->flags);
3189 * If this block group has free space cache written out, we
3190 * need to make sure to load it if we are removing space. This
3191 * is because we need the unpinning stage to actually add the
3192 * space back to the block group, otherwise we will leak space.
3194 if (!alloc && !btrfs_block_group_done(cache))
3195 btrfs_cache_block_group(cache, true);
3197 byte_in_group = bytenr - cache->start;
3198 WARN_ON(byte_in_group > cache->length);
3200 spin_lock(&cache->space_info->lock);
3201 spin_lock(&cache->lock);
3203 if (btrfs_test_opt(info, SPACE_CACHE) &&
3204 cache->disk_cache_state < BTRFS_DC_CLEAR)
3205 cache->disk_cache_state = BTRFS_DC_CLEAR;
3207 old_val = cache->used;
3208 num_bytes = min(total, cache->length - byte_in_group);
3210 old_val += num_bytes;
3211 cache->used = old_val;
3212 cache->reserved -= num_bytes;
3213 cache->space_info->bytes_reserved -= num_bytes;
3214 cache->space_info->bytes_used += num_bytes;
3215 cache->space_info->disk_used += num_bytes * factor;
3216 spin_unlock(&cache->lock);
3217 spin_unlock(&cache->space_info->lock);
3219 old_val -= num_bytes;
3220 cache->used = old_val;
3221 cache->pinned += num_bytes;
3222 btrfs_space_info_update_bytes_pinned(info,
3223 cache->space_info, num_bytes);
3224 cache->space_info->bytes_used -= num_bytes;
3225 cache->space_info->disk_used -= num_bytes * factor;
3226 spin_unlock(&cache->lock);
3227 spin_unlock(&cache->space_info->lock);
3229 set_extent_dirty(&trans->transaction->pinned_extents,
3230 bytenr, bytenr + num_bytes - 1,
3231 GFP_NOFS | __GFP_NOFAIL);
3234 spin_lock(&trans->transaction->dirty_bgs_lock);
3235 if (list_empty(&cache->dirty_list)) {
3236 list_add_tail(&cache->dirty_list,
3237 &trans->transaction->dirty_bgs);
3238 trans->delayed_ref_updates++;
3239 btrfs_get_block_group(cache);
3241 spin_unlock(&trans->transaction->dirty_bgs_lock);
3244 * No longer have used bytes in this block group, queue it for
3245 * deletion. We do this after adding the block group to the
3246 * dirty list to avoid races between cleaner kthread and space
3249 if (!alloc && old_val == 0) {
3250 if (!btrfs_test_opt(info, DISCARD_ASYNC))
3251 btrfs_mark_bg_unused(cache);
3254 btrfs_put_block_group(cache);
3256 bytenr += num_bytes;
3259 /* Modified block groups are accounted for in the delayed_refs_rsv. */
3260 btrfs_update_delayed_refs_rsv(trans);
3265 * btrfs_add_reserved_bytes - update the block_group and space info counters
3266 * @cache: The cache we are manipulating
3267 * @ram_bytes: The number of bytes of file content, and will be same to
3268 * @num_bytes except for the compress path.
3269 * @num_bytes: The number of bytes in question
3270 * @delalloc: The blocks are allocated for the delalloc write
3272 * This is called by the allocator when it reserves space. If this is a
3273 * reservation and the block group has become read only we cannot make the
3274 * reservation and return -EAGAIN, otherwise this function always succeeds.
3276 int btrfs_add_reserved_bytes(struct btrfs_block_group *cache,
3277 u64 ram_bytes, u64 num_bytes, int delalloc)
3279 struct btrfs_space_info *space_info = cache->space_info;
3282 spin_lock(&space_info->lock);
3283 spin_lock(&cache->lock);
3287 cache->reserved += num_bytes;
3288 space_info->bytes_reserved += num_bytes;
3289 trace_btrfs_space_reservation(cache->fs_info, "space_info",
3290 space_info->flags, num_bytes, 1);
3291 btrfs_space_info_update_bytes_may_use(cache->fs_info,
3292 space_info, -ram_bytes);
3294 cache->delalloc_bytes += num_bytes;
3297 * Compression can use less space than we reserved, so wake
3298 * tickets if that happens
3300 if (num_bytes < ram_bytes)
3301 btrfs_try_granting_tickets(cache->fs_info, space_info);
3303 spin_unlock(&cache->lock);
3304 spin_unlock(&space_info->lock);
3309 * btrfs_free_reserved_bytes - update the block_group and space info counters
3310 * @cache: The cache we are manipulating
3311 * @num_bytes: The number of bytes in question
3312 * @delalloc: The blocks are allocated for the delalloc write
3314 * This is called by somebody who is freeing space that was never actually used
3315 * on disk. For example if you reserve some space for a new leaf in transaction
3316 * A and before transaction A commits you free that leaf, you call this with
3317 * reserve set to 0 in order to clear the reservation.
3319 void btrfs_free_reserved_bytes(struct btrfs_block_group *cache,
3320 u64 num_bytes, int delalloc)
3322 struct btrfs_space_info *space_info = cache->space_info;
3324 spin_lock(&space_info->lock);
3325 spin_lock(&cache->lock);
3327 space_info->bytes_readonly += num_bytes;
3328 cache->reserved -= num_bytes;
3329 space_info->bytes_reserved -= num_bytes;
3330 space_info->max_extent_size = 0;
3333 cache->delalloc_bytes -= num_bytes;
3334 spin_unlock(&cache->lock);
3336 btrfs_try_granting_tickets(cache->fs_info, space_info);
3337 spin_unlock(&space_info->lock);
3340 static void force_metadata_allocation(struct btrfs_fs_info *info)
3342 struct list_head *head = &info->space_info;
3343 struct btrfs_space_info *found;
3345 list_for_each_entry(found, head, list) {
3346 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
3347 found->force_alloc = CHUNK_ALLOC_FORCE;
3351 static int should_alloc_chunk(struct btrfs_fs_info *fs_info,
3352 struct btrfs_space_info *sinfo, int force)
3354 u64 bytes_used = btrfs_space_info_used(sinfo, false);
3357 if (force == CHUNK_ALLOC_FORCE)
3361 * in limited mode, we want to have some free space up to
3362 * about 1% of the FS size.
3364 if (force == CHUNK_ALLOC_LIMITED) {
3365 thresh = btrfs_super_total_bytes(fs_info->super_copy);
3366 thresh = max_t(u64, SZ_64M, div_factor_fine(thresh, 1));
3368 if (sinfo->total_bytes - bytes_used < thresh)
3372 if (bytes_used + SZ_2M < div_factor(sinfo->total_bytes, 8))
3377 int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans, u64 type)
3379 u64 alloc_flags = btrfs_get_alloc_profile(trans->fs_info, type);
3381 return btrfs_chunk_alloc(trans, alloc_flags, CHUNK_ALLOC_FORCE);
3384 static int do_chunk_alloc(struct btrfs_trans_handle *trans, u64 flags)
3386 struct btrfs_block_group *bg;
3390 * Check if we have enough space in the system space info because we
3391 * will need to update device items in the chunk btree and insert a new
3392 * chunk item in the chunk btree as well. This will allocate a new
3393 * system block group if needed.
3395 check_system_chunk(trans, flags);
3397 bg = btrfs_create_chunk(trans, flags);
3403 ret = btrfs_chunk_alloc_add_chunk_item(trans, bg);
3405 * Normally we are not expected to fail with -ENOSPC here, since we have
3406 * previously reserved space in the system space_info and allocated one
3407 * new system chunk if necessary. However there are two exceptions:
3409 * 1) We may have enough free space in the system space_info but all the
3410 * existing system block groups have a profile which can not be used
3411 * for extent allocation.
3413 * This happens when mounting in degraded mode. For example we have a
3414 * RAID1 filesystem with 2 devices, lose one device and mount the fs
3415 * using the other device in degraded mode. If we then allocate a chunk,
3416 * we may have enough free space in the existing system space_info, but
3417 * none of the block groups can be used for extent allocation since they
3418 * have a RAID1 profile, and because we are in degraded mode with a
3419 * single device, we are forced to allocate a new system chunk with a
3420 * SINGLE profile. Making check_system_chunk() iterate over all system
3421 * block groups and check if they have a usable profile and enough space
3422 * can be slow on very large filesystems, so we tolerate the -ENOSPC and
3423 * try again after forcing allocation of a new system chunk. Like this
3424 * we avoid paying the cost of that search in normal circumstances, when
3425 * we were not mounted in degraded mode;
3427 * 2) We had enough free space info the system space_info, and one suitable
3428 * block group to allocate from when we called check_system_chunk()
3429 * above. However right after we called it, the only system block group
3430 * with enough free space got turned into RO mode by a running scrub,
3431 * and in this case we have to allocate a new one and retry. We only
3432 * need do this allocate and retry once, since we have a transaction
3433 * handle and scrub uses the commit root to search for block groups.
3435 if (ret == -ENOSPC) {
3436 const u64 sys_flags = btrfs_system_alloc_profile(trans->fs_info);
3437 struct btrfs_block_group *sys_bg;
3439 sys_bg = btrfs_create_chunk(trans, sys_flags);
3440 if (IS_ERR(sys_bg)) {
3441 ret = PTR_ERR(sys_bg);
3442 btrfs_abort_transaction(trans, ret);
3446 ret = btrfs_chunk_alloc_add_chunk_item(trans, sys_bg);
3448 btrfs_abort_transaction(trans, ret);
3452 ret = btrfs_chunk_alloc_add_chunk_item(trans, bg);
3454 btrfs_abort_transaction(trans, ret);
3458 btrfs_abort_transaction(trans, ret);
3462 btrfs_trans_release_chunk_metadata(trans);
3468 * Chunk allocation is done in 2 phases:
3470 * 1) Phase 1 - through btrfs_chunk_alloc() we allocate device extents for
3471 * the chunk, the chunk mapping, create its block group and add the items
3472 * that belong in the chunk btree to it - more specifically, we need to
3473 * update device items in the chunk btree and add a new chunk item to it.
3475 * 2) Phase 2 - through btrfs_create_pending_block_groups(), we add the block
3476 * group item to the extent btree and the device extent items to the devices
3479 * This is done to prevent deadlocks. For example when COWing a node from the
3480 * extent btree we are holding a write lock on the node's parent and if we
3481 * trigger chunk allocation and attempted to insert the new block group item
3482 * in the extent btree right way, we could deadlock because the path for the
3483 * insertion can include that parent node. At first glance it seems impossible
3484 * to trigger chunk allocation after starting a transaction since tasks should
3485 * reserve enough transaction units (metadata space), however while that is true
3486 * most of the time, chunk allocation may still be triggered for several reasons:
3488 * 1) When reserving metadata, we check if there is enough free space in the
3489 * metadata space_info and therefore don't trigger allocation of a new chunk.
3490 * However later when the task actually tries to COW an extent buffer from
3491 * the extent btree or from the device btree for example, it is forced to
3492 * allocate a new block group (chunk) because the only one that had enough
3493 * free space was just turned to RO mode by a running scrub for example (or
3494 * device replace, block group reclaim thread, etc), so we can not use it
3495 * for allocating an extent and end up being forced to allocate a new one;
3497 * 2) Because we only check that the metadata space_info has enough free bytes,
3498 * we end up not allocating a new metadata chunk in that case. However if
3499 * the filesystem was mounted in degraded mode, none of the existing block
3500 * groups might be suitable for extent allocation due to their incompatible
3501 * profile (for e.g. mounting a 2 devices filesystem, where all block groups
3502 * use a RAID1 profile, in degraded mode using a single device). In this case
3503 * when the task attempts to COW some extent buffer of the extent btree for
3504 * example, it will trigger allocation of a new metadata block group with a
3505 * suitable profile (SINGLE profile in the example of the degraded mount of
3506 * the RAID1 filesystem);
3508 * 3) The task has reserved enough transaction units / metadata space, but when
3509 * it attempts to COW an extent buffer from the extent or device btree for
3510 * example, it does not find any free extent in any metadata block group,
3511 * therefore forced to try to allocate a new metadata block group.
3512 * This is because some other task allocated all available extents in the
3513 * meanwhile - this typically happens with tasks that don't reserve space
3514 * properly, either intentionally or as a bug. One example where this is
3515 * done intentionally is fsync, as it does not reserve any transaction units
3516 * and ends up allocating a variable number of metadata extents for log
3517 * tree extent buffers.
3519 * We also need this 2 phases setup when adding a device to a filesystem with
3520 * a seed device - we must create new metadata and system chunks without adding
3521 * any of the block group items to the chunk, extent and device btrees. If we
3522 * did not do it this way, we would get ENOSPC when attempting to update those
3523 * btrees, since all the chunks from the seed device are read-only.
3525 * Phase 1 does the updates and insertions to the chunk btree because if we had
3526 * it done in phase 2 and have a thundering herd of tasks allocating chunks in
3527 * parallel, we risk having too many system chunks allocated by many tasks if
3528 * many tasks reach phase 1 without the previous ones completing phase 2. In the
3529 * extreme case this leads to exhaustion of the system chunk array in the
3530 * superblock. This is easier to trigger if using a btree node/leaf size of 64K
3531 * and with RAID filesystems (so we have more device items in the chunk btree).
3532 * This has happened before and commit eafa4fd0ad0607 ("btrfs: fix exhaustion of
3533 * the system chunk array due to concurrent allocations") provides more details.
3535 * Allocation of system chunks does not happen through this function. A task that
3536 * needs to update the chunk btree (the only btree that uses system chunks), must
3537 * preallocate chunk space by calling either check_system_chunk() or
3538 * btrfs_reserve_chunk_metadata() - the former is used when allocating a data or
3539 * metadata chunk or when removing a chunk, while the later is used before doing
3540 * a modification to the chunk btree - use cases for the later are adding,
3541 * removing and resizing a device as well as relocation of a system chunk.
3542 * See the comment below for more details.
3544 * The reservation of system space, done through check_system_chunk(), as well
3545 * as all the updates and insertions into the chunk btree must be done while
3546 * holding fs_info->chunk_mutex. This is important to guarantee that while COWing
3547 * an extent buffer from the chunks btree we never trigger allocation of a new
3548 * system chunk, which would result in a deadlock (trying to lock twice an
3549 * extent buffer of the chunk btree, first time before triggering the chunk
3550 * allocation and the second time during chunk allocation while attempting to
3551 * update the chunks btree). The system chunk array is also updated while holding
3552 * that mutex. The same logic applies to removing chunks - we must reserve system
3553 * space, update the chunk btree and the system chunk array in the superblock
3554 * while holding fs_info->chunk_mutex.
3556 * This function, btrfs_chunk_alloc(), belongs to phase 1.
3558 * If @force is CHUNK_ALLOC_FORCE:
3559 * - return 1 if it successfully allocates a chunk,
3560 * - return errors including -ENOSPC otherwise.
3561 * If @force is NOT CHUNK_ALLOC_FORCE:
3562 * - return 0 if it doesn't need to allocate a new chunk,
3563 * - return 1 if it successfully allocates a chunk,
3564 * - return errors including -ENOSPC otherwise.
3566 int btrfs_chunk_alloc(struct btrfs_trans_handle *trans, u64 flags,
3567 enum btrfs_chunk_alloc_enum force)
3569 struct btrfs_fs_info *fs_info = trans->fs_info;
3570 struct btrfs_space_info *space_info;
3571 bool wait_for_alloc = false;
3572 bool should_alloc = false;
3575 /* Don't re-enter if we're already allocating a chunk */
3576 if (trans->allocating_chunk)
3579 * Allocation of system chunks can not happen through this path, as we
3580 * could end up in a deadlock if we are allocating a data or metadata
3581 * chunk and there is another task modifying the chunk btree.
3583 * This is because while we are holding the chunk mutex, we will attempt
3584 * to add the new chunk item to the chunk btree or update an existing
3585 * device item in the chunk btree, while the other task that is modifying
3586 * the chunk btree is attempting to COW an extent buffer while holding a
3587 * lock on it and on its parent - if the COW operation triggers a system
3588 * chunk allocation, then we can deadlock because we are holding the
3589 * chunk mutex and we may need to access that extent buffer or its parent
3590 * in order to add the chunk item or update a device item.
3592 * Tasks that want to modify the chunk tree should reserve system space
3593 * before updating the chunk btree, by calling either
3594 * btrfs_reserve_chunk_metadata() or check_system_chunk().
3595 * It's possible that after a task reserves the space, it still ends up
3596 * here - this happens in the cases described above at do_chunk_alloc().
3597 * The task will have to either retry or fail.
3599 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3602 space_info = btrfs_find_space_info(fs_info, flags);
3606 spin_lock(&space_info->lock);
3607 if (force < space_info->force_alloc)
3608 force = space_info->force_alloc;
3609 should_alloc = should_alloc_chunk(fs_info, space_info, force);
3610 if (space_info->full) {
3611 /* No more free physical space */
3616 spin_unlock(&space_info->lock);
3618 } else if (!should_alloc) {
3619 spin_unlock(&space_info->lock);
3621 } else if (space_info->chunk_alloc) {
3623 * Someone is already allocating, so we need to block
3624 * until this someone is finished and then loop to
3625 * recheck if we should continue with our allocation
3628 wait_for_alloc = true;
3629 force = CHUNK_ALLOC_NO_FORCE;
3630 spin_unlock(&space_info->lock);
3631 mutex_lock(&fs_info->chunk_mutex);
3632 mutex_unlock(&fs_info->chunk_mutex);
3634 /* Proceed with allocation */
3635 space_info->chunk_alloc = 1;
3636 wait_for_alloc = false;
3637 spin_unlock(&space_info->lock);
3641 } while (wait_for_alloc);
3643 mutex_lock(&fs_info->chunk_mutex);
3644 trans->allocating_chunk = true;
3647 * If we have mixed data/metadata chunks we want to make sure we keep
3648 * allocating mixed chunks instead of individual chunks.
3650 if (btrfs_mixed_space_info(space_info))
3651 flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
3654 * if we're doing a data chunk, go ahead and make sure that
3655 * we keep a reasonable number of metadata chunks allocated in the
3658 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
3659 fs_info->data_chunk_allocations++;
3660 if (!(fs_info->data_chunk_allocations %
3661 fs_info->metadata_ratio))
3662 force_metadata_allocation(fs_info);
3665 ret = do_chunk_alloc(trans, flags);
3666 trans->allocating_chunk = false;
3668 spin_lock(&space_info->lock);
3671 space_info->full = 1;
3676 space_info->max_extent_size = 0;
3679 space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
3681 space_info->chunk_alloc = 0;
3682 spin_unlock(&space_info->lock);
3683 mutex_unlock(&fs_info->chunk_mutex);
3688 static u64 get_profile_num_devs(struct btrfs_fs_info *fs_info, u64 type)
3692 num_dev = btrfs_raid_array[btrfs_bg_flags_to_raid_index(type)].devs_max;
3694 num_dev = fs_info->fs_devices->rw_devices;
3699 static void reserve_chunk_space(struct btrfs_trans_handle *trans,
3703 struct btrfs_fs_info *fs_info = trans->fs_info;
3704 struct btrfs_space_info *info;
3709 * Needed because we can end up allocating a system chunk and for an
3710 * atomic and race free space reservation in the chunk block reserve.
3712 lockdep_assert_held(&fs_info->chunk_mutex);
3714 info = btrfs_find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
3715 spin_lock(&info->lock);
3716 left = info->total_bytes - btrfs_space_info_used(info, true);
3717 spin_unlock(&info->lock);
3719 if (left < bytes && btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
3720 btrfs_info(fs_info, "left=%llu, need=%llu, flags=%llu",
3722 btrfs_dump_space_info(fs_info, info, 0, 0);
3726 u64 flags = btrfs_system_alloc_profile(fs_info);
3727 struct btrfs_block_group *bg;
3730 * Ignore failure to create system chunk. We might end up not
3731 * needing it, as we might not need to COW all nodes/leafs from
3732 * the paths we visit in the chunk tree (they were already COWed
3733 * or created in the current transaction for example).
3735 bg = btrfs_create_chunk(trans, flags);
3740 * If we fail to add the chunk item here, we end up
3741 * trying again at phase 2 of chunk allocation, at
3742 * btrfs_create_pending_block_groups(). So ignore
3743 * any error here. An ENOSPC here could happen, due to
3744 * the cases described at do_chunk_alloc() - the system
3745 * block group we just created was just turned into RO
3746 * mode by a scrub for example, or a running discard
3747 * temporarily removed its free space entries, etc.
3749 btrfs_chunk_alloc_add_chunk_item(trans, bg);
3754 ret = btrfs_block_rsv_add(fs_info->chunk_root,
3755 &fs_info->chunk_block_rsv,
3756 bytes, BTRFS_RESERVE_NO_FLUSH);
3758 trans->chunk_bytes_reserved += bytes;
3763 * Reserve space in the system space for allocating or removing a chunk.
3764 * The caller must be holding fs_info->chunk_mutex.
3766 void check_system_chunk(struct btrfs_trans_handle *trans, u64 type)
3768 struct btrfs_fs_info *fs_info = trans->fs_info;
3769 const u64 num_devs = get_profile_num_devs(fs_info, type);
3772 /* num_devs device items to update and 1 chunk item to add or remove. */
3773 bytes = btrfs_calc_metadata_size(fs_info, num_devs) +
3774 btrfs_calc_insert_metadata_size(fs_info, 1);
3776 reserve_chunk_space(trans, bytes, type);
3780 * Reserve space in the system space, if needed, for doing a modification to the
3783 * @trans: A transaction handle.
3784 * @is_item_insertion: Indicate if the modification is for inserting a new item
3785 * in the chunk btree or if it's for the deletion or update
3786 * of an existing item.
3788 * This is used in a context where we need to update the chunk btree outside
3789 * block group allocation and removal, to avoid a deadlock with a concurrent
3790 * task that is allocating a metadata or data block group and therefore needs to
3791 * update the chunk btree while holding the chunk mutex. After the update to the
3792 * chunk btree is done, btrfs_trans_release_chunk_metadata() should be called.
3795 void btrfs_reserve_chunk_metadata(struct btrfs_trans_handle *trans,
3796 bool is_item_insertion)
3798 struct btrfs_fs_info *fs_info = trans->fs_info;
3801 if (is_item_insertion)
3802 bytes = btrfs_calc_insert_metadata_size(fs_info, 1);
3804 bytes = btrfs_calc_metadata_size(fs_info, 1);
3806 mutex_lock(&fs_info->chunk_mutex);
3807 reserve_chunk_space(trans, bytes, BTRFS_BLOCK_GROUP_SYSTEM);
3808 mutex_unlock(&fs_info->chunk_mutex);
3811 void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
3813 struct btrfs_block_group *block_group;
3817 struct inode *inode;
3819 block_group = btrfs_lookup_first_block_group(info, last);
3820 while (block_group) {
3821 btrfs_wait_block_group_cache_done(block_group);
3822 spin_lock(&block_group->lock);
3823 if (block_group->iref)
3825 spin_unlock(&block_group->lock);
3826 block_group = btrfs_next_block_group(block_group);
3835 inode = block_group->inode;
3836 block_group->iref = 0;
3837 block_group->inode = NULL;
3838 spin_unlock(&block_group->lock);
3839 ASSERT(block_group->io_ctl.inode == NULL);
3841 last = block_group->start + block_group->length;
3842 btrfs_put_block_group(block_group);
3847 * Must be called only after stopping all workers, since we could have block
3848 * group caching kthreads running, and therefore they could race with us if we
3849 * freed the block groups before stopping them.
3851 int btrfs_free_block_groups(struct btrfs_fs_info *info)
3853 struct btrfs_block_group *block_group;
3854 struct btrfs_space_info *space_info;
3855 struct btrfs_caching_control *caching_ctl;
3858 spin_lock(&info->block_group_cache_lock);
3859 while (!list_empty(&info->caching_block_groups)) {
3860 caching_ctl = list_entry(info->caching_block_groups.next,
3861 struct btrfs_caching_control, list);
3862 list_del(&caching_ctl->list);
3863 btrfs_put_caching_control(caching_ctl);
3865 spin_unlock(&info->block_group_cache_lock);
3867 spin_lock(&info->unused_bgs_lock);
3868 while (!list_empty(&info->unused_bgs)) {
3869 block_group = list_first_entry(&info->unused_bgs,
3870 struct btrfs_block_group,
3872 list_del_init(&block_group->bg_list);
3873 btrfs_put_block_group(block_group);
3875 spin_unlock(&info->unused_bgs_lock);
3877 spin_lock(&info->unused_bgs_lock);
3878 while (!list_empty(&info->reclaim_bgs)) {
3879 block_group = list_first_entry(&info->reclaim_bgs,
3880 struct btrfs_block_group,
3882 list_del_init(&block_group->bg_list);
3883 btrfs_put_block_group(block_group);
3885 spin_unlock(&info->unused_bgs_lock);
3887 spin_lock(&info->block_group_cache_lock);
3888 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
3889 block_group = rb_entry(n, struct btrfs_block_group,
3891 rb_erase(&block_group->cache_node,
3892 &info->block_group_cache_tree);
3893 RB_CLEAR_NODE(&block_group->cache_node);
3894 spin_unlock(&info->block_group_cache_lock);
3896 down_write(&block_group->space_info->groups_sem);
3897 list_del(&block_group->list);
3898 up_write(&block_group->space_info->groups_sem);
3901 * We haven't cached this block group, which means we could
3902 * possibly have excluded extents on this block group.
3904 if (block_group->cached == BTRFS_CACHE_NO ||
3905 block_group->cached == BTRFS_CACHE_ERROR)
3906 btrfs_free_excluded_extents(block_group);
3908 btrfs_remove_free_space_cache(block_group);
3909 ASSERT(block_group->cached != BTRFS_CACHE_STARTED);
3910 ASSERT(list_empty(&block_group->dirty_list));
3911 ASSERT(list_empty(&block_group->io_list));
3912 ASSERT(list_empty(&block_group->bg_list));
3913 ASSERT(refcount_read(&block_group->refs) == 1);
3914 ASSERT(block_group->swap_extents == 0);
3915 btrfs_put_block_group(block_group);
3917 spin_lock(&info->block_group_cache_lock);
3919 spin_unlock(&info->block_group_cache_lock);
3921 btrfs_release_global_block_rsv(info);
3923 while (!list_empty(&info->space_info)) {
3924 space_info = list_entry(info->space_info.next,
3925 struct btrfs_space_info,
3929 * Do not hide this behind enospc_debug, this is actually
3930 * important and indicates a real bug if this happens.
3932 if (WARN_ON(space_info->bytes_pinned > 0 ||
3933 space_info->bytes_may_use > 0))
3934 btrfs_dump_space_info(info, space_info, 0, 0);
3937 * If there was a failure to cleanup a log tree, very likely due
3938 * to an IO failure on a writeback attempt of one or more of its
3939 * extent buffers, we could not do proper (and cheap) unaccounting
3940 * of their reserved space, so don't warn on bytes_reserved > 0 in
3943 if (!(space_info->flags & BTRFS_BLOCK_GROUP_METADATA) ||
3944 !BTRFS_FS_LOG_CLEANUP_ERROR(info)) {
3945 if (WARN_ON(space_info->bytes_reserved > 0))
3946 btrfs_dump_space_info(info, space_info, 0, 0);
3949 WARN_ON(space_info->reclaim_size > 0);
3950 list_del(&space_info->list);
3951 btrfs_sysfs_remove_space_info(space_info);
3956 void btrfs_freeze_block_group(struct btrfs_block_group *cache)
3958 atomic_inc(&cache->frozen);
3961 void btrfs_unfreeze_block_group(struct btrfs_block_group *block_group)
3963 struct btrfs_fs_info *fs_info = block_group->fs_info;
3964 struct extent_map_tree *em_tree;
3965 struct extent_map *em;
3968 spin_lock(&block_group->lock);
3969 cleanup = (atomic_dec_and_test(&block_group->frozen) &&
3970 block_group->removed);
3971 spin_unlock(&block_group->lock);
3974 em_tree = &fs_info->mapping_tree;
3975 write_lock(&em_tree->lock);
3976 em = lookup_extent_mapping(em_tree, block_group->start,
3978 BUG_ON(!em); /* logic error, can't happen */
3979 remove_extent_mapping(em_tree, em);
3980 write_unlock(&em_tree->lock);
3982 /* once for us and once for the tree */
3983 free_extent_map(em);
3984 free_extent_map(em);
3987 * We may have left one free space entry and other possible
3988 * tasks trimming this block group have left 1 entry each one.
3991 __btrfs_remove_free_space_cache(block_group->free_space_ctl);
3995 bool btrfs_inc_block_group_swap_extents(struct btrfs_block_group *bg)
3999 spin_lock(&bg->lock);
4004 spin_unlock(&bg->lock);
4009 void btrfs_dec_block_group_swap_extents(struct btrfs_block_group *bg, int amount)
4011 spin_lock(&bg->lock);
4013 ASSERT(bg->swap_extents >= amount);
4014 bg->swap_extents -= amount;
4015 spin_unlock(&bg->lock);