Lines Matching +full:i +full:- +full:cache +full:- +full:block +full:- +full:size

1 // SPDX-License-Identifier: GPL-2.0
7 #include "block-group.h"
8 #include "space-info.h"
9 #include "disk-io.h"
10 #include "free-space-cache.h"
11 #include "free-space-tree.h"
14 #include "ref-verify.h"
16 #include "tree-log.h"
17 #include "delalloc-space.h"
23 #include "extent-tree.h"
28 struct btrfs_fs_info *fs_info = block_group->fs_info; in btrfs_should_fragment_free_space()
31 block_group->flags & BTRFS_BLOCK_GROUP_METADATA) || in btrfs_should_fragment_free_space()
33 block_group->flags & BTRFS_BLOCK_GROUP_DATA); in btrfs_should_fragment_free_space()
45 struct btrfs_balance_control *bctl = fs_info->balance_ctl; in get_restripe_target()
52 bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) { in get_restripe_target()
53 target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target; in get_restripe_target()
55 bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) { in get_restripe_target()
56 target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target; in get_restripe_target()
58 bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) { in get_restripe_target()
59 target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target; in get_restripe_target()
74 u64 num_devices = fs_info->fs_devices->rw_devices; in btrfs_reduce_alloc_profile()
83 spin_lock(&fs_info->balance_lock); in btrfs_reduce_alloc_profile()
86 spin_unlock(&fs_info->balance_lock); in btrfs_reduce_alloc_profile()
89 spin_unlock(&fs_info->balance_lock); in btrfs_reduce_alloc_profile()
98 /* Select the highest-redundancy RAID level. */ in btrfs_reduce_alloc_profile()
128 seq = read_seqbegin(&fs_info->profiles_lock); in btrfs_get_alloc_profile()
131 flags |= fs_info->avail_data_alloc_bits; in btrfs_get_alloc_profile()
133 flags |= fs_info->avail_system_alloc_bits; in btrfs_get_alloc_profile()
135 flags |= fs_info->avail_metadata_alloc_bits; in btrfs_get_alloc_profile()
136 } while (read_seqretry(&fs_info->profiles_lock, seq)); in btrfs_get_alloc_profile()
141 void btrfs_get_block_group(struct btrfs_block_group *cache) in btrfs_get_block_group() argument
143 refcount_inc(&cache->refs); in btrfs_get_block_group()
146 void btrfs_put_block_group(struct btrfs_block_group *cache) in btrfs_put_block_group() argument
148 if (refcount_dec_and_test(&cache->refs)) { in btrfs_put_block_group()
149 WARN_ON(cache->pinned > 0); in btrfs_put_block_group()
157 if (!(cache->flags & BTRFS_BLOCK_GROUP_METADATA) || in btrfs_put_block_group()
158 !BTRFS_FS_LOG_CLEANUP_ERROR(cache->fs_info)) in btrfs_put_block_group()
159 WARN_ON(cache->reserved > 0); in btrfs_put_block_group()
166 if (WARN_ON(!list_empty(&cache->discard_list))) in btrfs_put_block_group()
167 btrfs_discard_cancel_work(&cache->fs_info->discard_ctl, in btrfs_put_block_group()
168 cache); in btrfs_put_block_group()
170 kfree(cache->free_space_ctl); in btrfs_put_block_group()
171 kfree(cache->physical_map); in btrfs_put_block_group()
172 kfree(cache); in btrfs_put_block_group()
177 * This adds the block group to the fs_info rb tree for the block group cache
184 struct btrfs_block_group *cache; in btrfs_add_block_group_cache() local
187 ASSERT(block_group->length != 0); in btrfs_add_block_group_cache()
189 write_lock(&info->block_group_cache_lock); in btrfs_add_block_group_cache()
190 p = &info->block_group_cache_tree.rb_root.rb_node; in btrfs_add_block_group_cache()
194 cache = rb_entry(parent, struct btrfs_block_group, cache_node); in btrfs_add_block_group_cache()
195 if (block_group->start < cache->start) { in btrfs_add_block_group_cache()
196 p = &(*p)->rb_left; in btrfs_add_block_group_cache()
197 } else if (block_group->start > cache->start) { in btrfs_add_block_group_cache()
198 p = &(*p)->rb_right; in btrfs_add_block_group_cache()
201 write_unlock(&info->block_group_cache_lock); in btrfs_add_block_group_cache()
202 return -EEXIST; in btrfs_add_block_group_cache()
206 rb_link_node(&block_group->cache_node, parent, p); in btrfs_add_block_group_cache()
207 rb_insert_color_cached(&block_group->cache_node, in btrfs_add_block_group_cache()
208 &info->block_group_cache_tree, leftmost); in btrfs_add_block_group_cache()
210 write_unlock(&info->block_group_cache_lock); in btrfs_add_block_group_cache()
216 * This will return the block group at or after bytenr if contains is 0, else
217 * it will return the block group that contains the bytenr
222 struct btrfs_block_group *cache, *ret = NULL; in block_group_cache_tree_search() local
226 read_lock(&info->block_group_cache_lock); in block_group_cache_tree_search()
227 n = info->block_group_cache_tree.rb_root.rb_node; in block_group_cache_tree_search()
230 cache = rb_entry(n, struct btrfs_block_group, cache_node); in block_group_cache_tree_search()
231 end = cache->start + cache->length - 1; in block_group_cache_tree_search()
232 start = cache->start; in block_group_cache_tree_search()
235 if (!contains && (!ret || start < ret->start)) in block_group_cache_tree_search()
236 ret = cache; in block_group_cache_tree_search()
237 n = n->rb_left; in block_group_cache_tree_search()
240 ret = cache; in block_group_cache_tree_search()
243 n = n->rb_right; in block_group_cache_tree_search()
245 ret = cache; in block_group_cache_tree_search()
251 read_unlock(&info->block_group_cache_lock); in block_group_cache_tree_search()
257 * Return the block group that starts at or after bytenr
266 * Return the block group that contains the given bytenr
275 struct btrfs_block_group *cache) in btrfs_next_block_group() argument
277 struct btrfs_fs_info *fs_info = cache->fs_info; in btrfs_next_block_group()
280 read_lock(&fs_info->block_group_cache_lock); in btrfs_next_block_group()
282 /* If our block group was removed, we need a full search. */ in btrfs_next_block_group()
283 if (RB_EMPTY_NODE(&cache->cache_node)) { in btrfs_next_block_group()
284 const u64 next_bytenr = cache->start + cache->length; in btrfs_next_block_group()
286 read_unlock(&fs_info->block_group_cache_lock); in btrfs_next_block_group()
287 btrfs_put_block_group(cache); in btrfs_next_block_group()
290 node = rb_next(&cache->cache_node); in btrfs_next_block_group()
291 btrfs_put_block_group(cache); in btrfs_next_block_group()
293 cache = rb_entry(node, struct btrfs_block_group, cache_node); in btrfs_next_block_group()
294 btrfs_get_block_group(cache); in btrfs_next_block_group()
296 cache = NULL; in btrfs_next_block_group()
297 read_unlock(&fs_info->block_group_cache_lock); in btrfs_next_block_group()
298 return cache; in btrfs_next_block_group()
308 * number of NOCOW writers in the block group that contains the extent, as long
309 * as the block group exists and it's currently not in read-only mode.
311 * Returns: A non-NULL block group pointer if we can do a NOCOW write, the caller
326 spin_lock(&bg->lock); in btrfs_inc_nocow_writers()
327 if (bg->ro) in btrfs_inc_nocow_writers()
330 atomic_inc(&bg->nocow_writers); in btrfs_inc_nocow_writers()
331 spin_unlock(&bg->lock); in btrfs_inc_nocow_writers()
338 /* No put on block group, done by btrfs_dec_nocow_writers(). */ in btrfs_inc_nocow_writers()
343 * Decrement the number of NOCOW writers in a block group.
346 * and on the block group returned by that call. Typically this is called after
350 * After this call, the caller should not use the block group anymore. It it wants
355 if (atomic_dec_and_test(&bg->nocow_writers)) in btrfs_dec_nocow_writers()
356 wake_up_var(&bg->nocow_writers); in btrfs_dec_nocow_writers()
364 wait_var_event(&bg->nocow_writers, !atomic_read(&bg->nocow_writers)); in btrfs_wait_nocow_writers()
374 if (atomic_dec_and_test(&bg->reservations)) in btrfs_dec_block_group_reservations()
375 wake_up_var(&bg->reservations); in btrfs_dec_block_group_reservations()
381 struct btrfs_space_info *space_info = bg->space_info; in btrfs_wait_block_group_reservations()
383 ASSERT(bg->ro); in btrfs_wait_block_group_reservations()
385 if (!(bg->flags & BTRFS_BLOCK_GROUP_DATA)) in btrfs_wait_block_group_reservations()
389 * Our block group is read only but before we set it to read only, in btrfs_wait_block_group_reservations()
394 * block group's reservations counter is incremented while a read lock in btrfs_wait_block_group_reservations()
398 down_write(&space_info->groups_sem); in btrfs_wait_block_group_reservations()
399 up_write(&space_info->groups_sem); in btrfs_wait_block_group_reservations()
401 wait_var_event(&bg->reservations, !atomic_read(&bg->reservations)); in btrfs_wait_block_group_reservations()
405 struct btrfs_block_group *cache) in btrfs_get_caching_control() argument
409 spin_lock(&cache->lock); in btrfs_get_caching_control()
410 if (!cache->caching_ctl) { in btrfs_get_caching_control()
411 spin_unlock(&cache->lock); in btrfs_get_caching_control()
415 ctl = cache->caching_ctl; in btrfs_get_caching_control()
416 refcount_inc(&ctl->count); in btrfs_get_caching_control()
417 spin_unlock(&cache->lock); in btrfs_get_caching_control()
423 if (refcount_dec_and_test(&ctl->count)) in btrfs_put_caching_control()
428 * When we wait for progress in the block group caching, its because our
433 * up, and then it will check the block group free space numbers for our min
435 * a free extent of a given size, but this is a good start.
437 * Callers of this must check if cache->cached == BTRFS_CACHE_ERROR before using
438 * any of the information in this block group.
440 void btrfs_wait_block_group_cache_progress(struct btrfs_block_group *cache, in btrfs_wait_block_group_cache_progress() argument
446 caching_ctl = btrfs_get_caching_control(cache); in btrfs_wait_block_group_cache_progress()
451 * We've already failed to allocate from this block group, so even if in btrfs_wait_block_group_cache_progress()
452 * there's enough space in the block group it isn't contiguous enough to in btrfs_wait_block_group_cache_progress()
456 progress = atomic_read(&caching_ctl->progress); in btrfs_wait_block_group_cache_progress()
458 wait_event(caching_ctl->wait, btrfs_block_group_done(cache) || in btrfs_wait_block_group_cache_progress()
459 (progress != atomic_read(&caching_ctl->progress) && in btrfs_wait_block_group_cache_progress()
460 (cache->free_space_ctl->free_space >= num_bytes))); in btrfs_wait_block_group_cache_progress()
465 static int btrfs_caching_ctl_wait_done(struct btrfs_block_group *cache, in btrfs_caching_ctl_wait_done() argument
468 wait_event(caching_ctl->wait, btrfs_block_group_done(cache)); in btrfs_caching_ctl_wait_done()
469 return cache->cached == BTRFS_CACHE_ERROR ? -EIO : 0; in btrfs_caching_ctl_wait_done()
472 static int btrfs_wait_block_group_cache_done(struct btrfs_block_group *cache) in btrfs_wait_block_group_cache_done() argument
477 caching_ctl = btrfs_get_caching_control(cache); in btrfs_wait_block_group_cache_done()
479 return (cache->cached == BTRFS_CACHE_ERROR) ? -EIO : 0; in btrfs_wait_block_group_cache_done()
480 ret = btrfs_caching_ctl_wait_done(cache, caching_ctl); in btrfs_wait_block_group_cache_done()
488 struct btrfs_fs_info *fs_info = block_group->fs_info; in fragment_free_space()
489 u64 start = block_group->start; in fragment_free_space()
490 u64 len = block_group->length; in fragment_free_space()
491 u64 chunk = block_group->flags & BTRFS_BLOCK_GROUP_METADATA ? in fragment_free_space()
492 fs_info->nodesize : fs_info->sectorsize; in fragment_free_space()
501 len -= step; in fragment_free_space()
507 * Add a free space range to the in memory free space cache of a block group.
508 * This checks if the range contains super block locations and any such
509 * locations are not added to the free space cache.
511 * @block_group: The target block group.
515 * added to the block group's free space cache.
522 struct btrfs_fs_info *info = block_group->fs_info; in btrfs_add_new_free_space()
523 u64 extent_start, extent_end, size; in btrfs_add_new_free_space() local
530 if (!find_first_extent_bit(&info->excluded_extents, start, in btrfs_add_new_free_space()
539 size = extent_start - start; in btrfs_add_new_free_space()
541 start, size); in btrfs_add_new_free_space()
545 *total_added_ret += size; in btrfs_add_new_free_space()
553 size = end - start; in btrfs_add_new_free_space()
555 size); in btrfs_add_new_free_space()
559 *total_added_ret += size; in btrfs_add_new_free_space()
566 * Get an arbitrary extent item index / max_index through the block group
568 * @block_group the block group to sample from
569 * @index: the integral step through the block group to grab from
573 * Pre-conditions on indices:
585 struct btrfs_fs_info *fs_info = block_group->fs_info; in sample_block_group_extent_item()
588 u64 search_end = block_group->start + block_group->length; in sample_block_group_extent_item()
596 lockdep_assert_held(&caching_ctl->mutex); in sample_block_group_extent_item()
597 lockdep_assert_held_read(&fs_info->commit_root_sem); in sample_block_group_extent_item()
601 return -ENOMEM; in sample_block_group_extent_item()
603 extent_root = btrfs_extent_root(fs_info, max_t(u64, block_group->start, in sample_block_group_extent_item()
606 path->skip_locking = 1; in sample_block_group_extent_item()
607 path->search_commit_root = 1; in sample_block_group_extent_item()
608 path->reada = READA_FORWARD; in sample_block_group_extent_item()
610 search_offset = index * div_u64(block_group->length, max_index); in sample_block_group_extent_item()
611 search_key.objectid = block_group->start + search_offset; in sample_block_group_extent_item()
616 /* Success; sampled an extent item in the block group */ in sample_block_group_extent_item()
617 if (found_key->type == BTRFS_EXTENT_ITEM_KEY && in sample_block_group_extent_item()
618 found_key->objectid >= block_group->start && in sample_block_group_extent_item()
619 found_key->objectid + found_key->offset <= search_end) in sample_block_group_extent_item()
623 if (found_key->objectid >= search_end) { in sample_block_group_extent_item()
629 lockdep_assert_held(&caching_ctl->mutex); in sample_block_group_extent_item()
630 lockdep_assert_held_read(&fs_info->commit_root_sem); in sample_block_group_extent_item()
636 * Best effort attempt to compute a block group's size class while caching it.
638 * @block_group: the block group we are caching
640 * We cannot infer the size class while adding free space extents, because that
645 * them at even steps through the block group and pick the smallest size class
646 * we see. Since size class is best effort, and not guaranteed in general,
651 * If we are caching in a block group from disk, then there are three major cases
653 * 1. the block group is well behaved and all extents in it are the same size
655 * 2. the block group is mostly one size class with rare exceptions for last
657 * 3. the block group was populated before size classes and can have a totally
658 * arbitrary mix of size classes.
660 * In case 1, looking at any extent in the block group will yield the correct
661 * result. For the mixed cases, taking the minimum size class seems like a good
662 * approximation, since gaps from frees will be usable to the size class. For
672 struct btrfs_fs_info *fs_info = block_group->fs_info; in load_block_group_size_class()
674 int i; in load_block_group_size_class() local
675 u64 min_size = block_group->length; in load_block_group_size_class()
682 lockdep_assert_held(&caching_ctl->mutex); in load_block_group_size_class()
683 lockdep_assert_held_read(&fs_info->commit_root_sem); in load_block_group_size_class()
684 for (i = 0; i < 5; ++i) { in load_block_group_size_class()
685 ret = sample_block_group_extent_item(caching_ctl, block_group, i, 5, &key); in load_block_group_size_class()
694 spin_lock(&block_group->lock); in load_block_group_size_class()
695 block_group->size_class = size_class; in load_block_group_size_class()
696 spin_unlock(&block_group->lock); in load_block_group_size_class()
704 struct btrfs_block_group *block_group = caching_ctl->block_group; in load_extent_tree_free()
705 struct btrfs_fs_info *fs_info = block_group->fs_info; in load_extent_tree_free()
718 return -ENOMEM; in load_extent_tree_free()
720 last = max_t(u64, block_group->start, BTRFS_SUPER_INFO_OFFSET); in load_extent_tree_free()
726 * allocate from this block group until we've had a chance to fragment in load_extent_tree_free()
736 * root, since its read-only in load_extent_tree_free()
738 path->skip_locking = 1; in load_extent_tree_free()
739 path->search_commit_root = 1; in load_extent_tree_free()
740 path->reada = READA_FORWARD; in load_extent_tree_free()
751 leaf = path->nodes[0]; in load_extent_tree_free()
756 last = (u64)-1; in load_extent_tree_free()
760 if (path->slots[0] < nritems) { in load_extent_tree_free()
761 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); in load_extent_tree_free()
768 rwsem_is_contended(&fs_info->commit_root_sem)) { in load_extent_tree_free()
770 up_read(&fs_info->commit_root_sem); in load_extent_tree_free()
771 mutex_unlock(&caching_ctl->mutex); in load_extent_tree_free()
773 mutex_lock(&caching_ctl->mutex); in load_extent_tree_free()
774 down_read(&fs_info->commit_root_sem); in load_extent_tree_free()
783 leaf = path->nodes[0]; in load_extent_tree_free()
796 if (key.objectid < block_group->start) { in load_extent_tree_free()
797 path->slots[0]++; in load_extent_tree_free()
801 if (key.objectid >= block_group->start + block_group->length) in load_extent_tree_free()
815 fs_info->nodesize; in load_extent_tree_free()
822 atomic_inc(&caching_ctl->progress); in load_extent_tree_free()
823 wake_up(&caching_ctl->wait); in load_extent_tree_free()
827 path->slots[0]++; in load_extent_tree_free()
831 block_group->start + block_group->length, in load_extent_tree_free()
840 clear_extent_bits(&bg->fs_info->excluded_extents, bg->start, in btrfs_free_excluded_extents()
841 bg->start + bg->length - 1, EXTENT_UPTODATE); in btrfs_free_excluded_extents()
852 block_group = caching_ctl->block_group; in caching_thread()
853 fs_info = block_group->fs_info; in caching_thread()
855 mutex_lock(&caching_ctl->mutex); in caching_thread()
856 down_read(&fs_info->commit_root_sem); in caching_thread()
867 * We failed to load the space cache, set ourselves to in caching_thread()
870 spin_lock(&block_group->lock); in caching_thread()
871 block_group->cached = BTRFS_CACHE_STARTED; in caching_thread()
872 spin_unlock(&block_group->lock); in caching_thread()
873 wake_up(&caching_ctl->wait); in caching_thread()
878 * can't actually cache from the free space tree as our commit root and in caching_thread()
884 !(test_bit(BTRFS_FS_FREE_SPACE_TREE_UNTRUSTED, &fs_info->flags))) in caching_thread()
889 spin_lock(&block_group->lock); in caching_thread()
890 block_group->caching_ctl = NULL; in caching_thread()
891 block_group->cached = ret ? BTRFS_CACHE_ERROR : BTRFS_CACHE_FINISHED; in caching_thread()
892 spin_unlock(&block_group->lock); in caching_thread()
898 spin_lock(&block_group->space_info->lock); in caching_thread()
899 spin_lock(&block_group->lock); in caching_thread()
900 bytes_used = block_group->length - block_group->used; in caching_thread()
901 block_group->space_info->bytes_used += bytes_used >> 1; in caching_thread()
902 spin_unlock(&block_group->lock); in caching_thread()
903 spin_unlock(&block_group->space_info->lock); in caching_thread()
908 up_read(&fs_info->commit_root_sem); in caching_thread()
910 mutex_unlock(&caching_ctl->mutex); in caching_thread()
912 wake_up(&caching_ctl->wait); in caching_thread()
918 int btrfs_cache_block_group(struct btrfs_block_group *cache, bool wait) in btrfs_cache_block_group() argument
920 struct btrfs_fs_info *fs_info = cache->fs_info; in btrfs_cache_block_group()
924 /* Allocator for zoned filesystems does not use the cache at all */ in btrfs_cache_block_group()
930 return -ENOMEM; in btrfs_cache_block_group()
932 INIT_LIST_HEAD(&caching_ctl->list); in btrfs_cache_block_group()
933 mutex_init(&caching_ctl->mutex); in btrfs_cache_block_group()
934 init_waitqueue_head(&caching_ctl->wait); in btrfs_cache_block_group()
935 caching_ctl->block_group = cache; in btrfs_cache_block_group()
936 refcount_set(&caching_ctl->count, 2); in btrfs_cache_block_group()
937 atomic_set(&caching_ctl->progress, 0); in btrfs_cache_block_group()
938 btrfs_init_work(&caching_ctl->work, caching_thread, NULL, NULL); in btrfs_cache_block_group()
940 spin_lock(&cache->lock); in btrfs_cache_block_group()
941 if (cache->cached != BTRFS_CACHE_NO) { in btrfs_cache_block_group()
944 caching_ctl = cache->caching_ctl; in btrfs_cache_block_group()
946 refcount_inc(&caching_ctl->count); in btrfs_cache_block_group()
947 spin_unlock(&cache->lock); in btrfs_cache_block_group()
950 WARN_ON(cache->caching_ctl); in btrfs_cache_block_group()
951 cache->caching_ctl = caching_ctl; in btrfs_cache_block_group()
952 cache->cached = BTRFS_CACHE_STARTED; in btrfs_cache_block_group()
953 spin_unlock(&cache->lock); in btrfs_cache_block_group()
955 write_lock(&fs_info->block_group_cache_lock); in btrfs_cache_block_group()
956 refcount_inc(&caching_ctl->count); in btrfs_cache_block_group()
957 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups); in btrfs_cache_block_group()
958 write_unlock(&fs_info->block_group_cache_lock); in btrfs_cache_block_group()
960 btrfs_get_block_group(cache); in btrfs_cache_block_group()
962 btrfs_queue_work(fs_info->caching_workers, &caching_ctl->work); in btrfs_cache_block_group()
965 ret = btrfs_caching_ctl_wait_done(cache, caching_ctl); in btrfs_cache_block_group()
977 write_seqlock(&fs_info->profiles_lock); in clear_avail_alloc_bits()
979 fs_info->avail_data_alloc_bits &= ~extra_flags; in clear_avail_alloc_bits()
981 fs_info->avail_metadata_alloc_bits &= ~extra_flags; in clear_avail_alloc_bits()
983 fs_info->avail_system_alloc_bits &= ~extra_flags; in clear_avail_alloc_bits()
984 write_sequnlock(&fs_info->profiles_lock); in clear_avail_alloc_bits()
990 * - RAID56 - in case there's neither RAID5 nor RAID6 profile block group
993 * - RAID1C34 - same as above for RAID1C3 and RAID1C4 block groups
1003 struct list_head *head = &fs_info->space_info; in clear_incompat_bg_bits()
1007 down_read(&sinfo->groups_sem); in clear_incompat_bg_bits()
1008 if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID5])) in clear_incompat_bg_bits()
1010 if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID6])) in clear_incompat_bg_bits()
1012 if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID1C3])) in clear_incompat_bg_bits()
1014 if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID1C4])) in clear_incompat_bg_bits()
1016 up_read(&sinfo->groups_sem); in clear_incompat_bg_bits()
1029 struct btrfs_fs_info *fs_info = trans->fs_info; in remove_block_group_item()
1035 key.objectid = block_group->start; in remove_block_group_item()
1037 key.offset = block_group->length; in remove_block_group_item()
1039 ret = btrfs_search_slot(trans, root, &key, path, -1, 1); in remove_block_group_item()
1041 ret = -ENOENT; in remove_block_group_item()
1052 struct btrfs_fs_info *fs_info = trans->fs_info; in btrfs_remove_block_group()
1067 BUG_ON(!block_group->ro); in btrfs_remove_block_group()
1071 * Free the reserved super bytes from this block group before in btrfs_remove_block_group()
1075 btrfs_free_ref_tree_range(fs_info, block_group->start, in btrfs_remove_block_group()
1076 block_group->length); in btrfs_remove_block_group()
1078 index = btrfs_bg_flags_to_raid_index(block_group->flags); in btrfs_remove_block_group()
1079 factor = btrfs_bg_type_to_factor(block_group->flags); in btrfs_remove_block_group()
1081 /* make sure this block group isn't part of an allocation cluster */ in btrfs_remove_block_group()
1082 cluster = &fs_info->data_alloc_cluster; in btrfs_remove_block_group()
1083 spin_lock(&cluster->refill_lock); in btrfs_remove_block_group()
1085 spin_unlock(&cluster->refill_lock); in btrfs_remove_block_group()
1088 * make sure this block group isn't part of a metadata in btrfs_remove_block_group()
1091 cluster = &fs_info->meta_alloc_cluster; in btrfs_remove_block_group()
1092 spin_lock(&cluster->refill_lock); in btrfs_remove_block_group()
1094 spin_unlock(&cluster->refill_lock); in btrfs_remove_block_group()
1101 ret = -ENOMEM; in btrfs_remove_block_group()
1111 mutex_lock(&trans->transaction->cache_write_mutex); in btrfs_remove_block_group()
1113 * Make sure our free space cache IO is done before removing the in btrfs_remove_block_group()
1116 spin_lock(&trans->transaction->dirty_bgs_lock); in btrfs_remove_block_group()
1117 if (!list_empty(&block_group->io_list)) { in btrfs_remove_block_group()
1118 list_del_init(&block_group->io_list); in btrfs_remove_block_group()
1120 WARN_ON(!IS_ERR(inode) && inode != block_group->io_ctl.inode); in btrfs_remove_block_group()
1122 spin_unlock(&trans->transaction->dirty_bgs_lock); in btrfs_remove_block_group()
1125 spin_lock(&trans->transaction->dirty_bgs_lock); in btrfs_remove_block_group()
1128 if (!list_empty(&block_group->dirty_list)) { in btrfs_remove_block_group()
1129 list_del_init(&block_group->dirty_list); in btrfs_remove_block_group()
1133 spin_unlock(&trans->transaction->dirty_bgs_lock); in btrfs_remove_block_group()
1134 mutex_unlock(&trans->transaction->cache_write_mutex); in btrfs_remove_block_group()
1140 write_lock(&fs_info->block_group_cache_lock); in btrfs_remove_block_group()
1141 rb_erase_cached(&block_group->cache_node, in btrfs_remove_block_group()
1142 &fs_info->block_group_cache_tree); in btrfs_remove_block_group()
1143 RB_CLEAR_NODE(&block_group->cache_node); in btrfs_remove_block_group()
1145 /* Once for the block groups rbtree */ in btrfs_remove_block_group()
1148 write_unlock(&fs_info->block_group_cache_lock); in btrfs_remove_block_group()
1150 down_write(&block_group->space_info->groups_sem); in btrfs_remove_block_group()
1155 list_del_init(&block_group->list); in btrfs_remove_block_group()
1156 if (list_empty(&block_group->space_info->block_groups[index])) { in btrfs_remove_block_group()
1157 kobj = block_group->space_info->block_group_kobjs[index]; in btrfs_remove_block_group()
1158 block_group->space_info->block_group_kobjs[index] = NULL; in btrfs_remove_block_group()
1159 clear_avail_alloc_bits(fs_info, block_group->flags); in btrfs_remove_block_group()
1161 up_write(&block_group->space_info->groups_sem); in btrfs_remove_block_group()
1162 clear_incompat_bg_bits(fs_info, block_group->flags); in btrfs_remove_block_group()
1168 if (block_group->cached == BTRFS_CACHE_STARTED) in btrfs_remove_block_group()
1171 write_lock(&fs_info->block_group_cache_lock); in btrfs_remove_block_group()
1176 list_for_each_entry(ctl, &fs_info->caching_block_groups, list) { in btrfs_remove_block_group()
1177 if (ctl->block_group == block_group) { in btrfs_remove_block_group()
1179 refcount_inc(&caching_ctl->count); in btrfs_remove_block_group()
1185 list_del_init(&caching_ctl->list); in btrfs_remove_block_group()
1186 write_unlock(&fs_info->block_group_cache_lock); in btrfs_remove_block_group()
1194 spin_lock(&trans->transaction->dirty_bgs_lock); in btrfs_remove_block_group()
1195 WARN_ON(!list_empty(&block_group->dirty_list)); in btrfs_remove_block_group()
1196 WARN_ON(!list_empty(&block_group->io_list)); in btrfs_remove_block_group()
1197 spin_unlock(&trans->transaction->dirty_bgs_lock); in btrfs_remove_block_group()
1201 spin_lock(&block_group->space_info->lock); in btrfs_remove_block_group()
1202 list_del_init(&block_group->ro_list); in btrfs_remove_block_group()
1205 WARN_ON(block_group->space_info->total_bytes in btrfs_remove_block_group()
1206 < block_group->length); in btrfs_remove_block_group()
1207 WARN_ON(block_group->space_info->bytes_readonly in btrfs_remove_block_group()
1208 < block_group->length - block_group->zone_unusable); in btrfs_remove_block_group()
1209 WARN_ON(block_group->space_info->bytes_zone_unusable in btrfs_remove_block_group()
1210 < block_group->zone_unusable); in btrfs_remove_block_group()
1211 WARN_ON(block_group->space_info->disk_total in btrfs_remove_block_group()
1212 < block_group->length * factor); in btrfs_remove_block_group()
1214 block_group->space_info->total_bytes -= block_group->length; in btrfs_remove_block_group()
1215 block_group->space_info->bytes_readonly -= in btrfs_remove_block_group()
1216 (block_group->length - block_group->zone_unusable); in btrfs_remove_block_group()
1217 btrfs_space_info_update_bytes_zone_unusable(fs_info, block_group->space_info, in btrfs_remove_block_group()
1218 -block_group->zone_unusable); in btrfs_remove_block_group()
1219 block_group->space_info->disk_total -= block_group->length * factor; in btrfs_remove_block_group()
1221 spin_unlock(&block_group->space_info->lock); in btrfs_remove_block_group()
1224 * Remove the free space for the block group from the free space tree in btrfs_remove_block_group()
1225 * and the block group's item from the extent tree before marking the in btrfs_remove_block_group()
1226 * block group as removed. This is to prevent races with tasks that in btrfs_remove_block_group()
1227 * freeze and unfreeze a block group, this task and another task in btrfs_remove_block_group()
1228 * allocating a new block group - the unfreeze task ends up removing in btrfs_remove_block_group()
1229 * the block group's extent map before the task calling this function in btrfs_remove_block_group()
1230 * deletes the block group item from the extent tree, allowing for in btrfs_remove_block_group()
1231 * another task to attempt to create another block group with the same in btrfs_remove_block_group()
1232 * item key (and failing with -EEXIST and a transaction abort). in btrfs_remove_block_group()
1242 spin_lock(&block_group->lock); in btrfs_remove_block_group()
1243 set_bit(BLOCK_GROUP_FLAG_REMOVED, &block_group->runtime_flags); in btrfs_remove_block_group()
1246 * At this point trimming or scrub can't start on this block group, in btrfs_remove_block_group()
1247 * because we removed the block group from the rbtree in btrfs_remove_block_group()
1248 * fs_info->block_group_cache_tree so no one can't find it anymore and in btrfs_remove_block_group()
1249 * even if someone already got this block group before we removed it in btrfs_remove_block_group()
1250 * from the rbtree, they have already incremented block_group->frozen - in btrfs_remove_block_group()
1255 * And we must not remove the extent map from the fs_info->mapping_tree in btrfs_remove_block_group()
1257 * ranges from being reused for a new block group. This is needed to in btrfs_remove_block_group()
1263 * allowing for new block groups to be created that can reuse the same in btrfs_remove_block_group()
1267 * is mounted with -odiscard. The same protections must remain in btrfs_remove_block_group()
1271 remove_em = (atomic_read(&block_group->frozen) == 0); in btrfs_remove_block_group()
1272 spin_unlock(&block_group->lock); in btrfs_remove_block_group()
1277 em_tree = &fs_info->mapping_tree; in btrfs_remove_block_group()
1278 write_lock(&em_tree->lock); in btrfs_remove_block_group()
1280 write_unlock(&em_tree->lock); in btrfs_remove_block_group()
1298 struct extent_map_tree *em_tree = &fs_info->mapping_tree; in btrfs_start_trans_remove_block_group()
1303 read_lock(&em_tree->lock); in btrfs_start_trans_remove_block_group()
1305 read_unlock(&em_tree->lock); in btrfs_start_trans_remove_block_group()
1306 ASSERT(em && em->start == chunk_offset); in btrfs_start_trans_remove_block_group()
1310 * to remove a block group (done at btrfs_remove_chunk() and at in btrfs_start_trans_remove_block_group()
1315 * 1 unit for deleting the block group item (located in the extent in btrfs_start_trans_remove_block_group()
1322 * In order to remove a block group we also need to reserve units in the in btrfs_start_trans_remove_block_group()
1327 map = em->map_lookup; in btrfs_start_trans_remove_block_group()
1328 num_items = 3 + map->num_stripes; in btrfs_start_trans_remove_block_group()
1335 * Mark block group @cache read-only, so later write won't happen to block
1336 * group @cache.
1338 * If @force is not set, this function will only mark the block group readonly
1339 * if we have enough free space (1M) in other metadata/system block groups.
1340 * If @force is not set, this function will mark the block group readonly
1343 * NOTE: This function doesn't care if other block groups can contain all the
1344 * data in this block group. That check should be done by relocation routine,
1347 static int inc_block_group_ro(struct btrfs_block_group *cache, int force) in inc_block_group_ro() argument
1349 struct btrfs_space_info *sinfo = cache->space_info; in inc_block_group_ro()
1351 int ret = -ENOSPC; in inc_block_group_ro()
1353 spin_lock(&sinfo->lock); in inc_block_group_ro()
1354 spin_lock(&cache->lock); in inc_block_group_ro()
1356 if (cache->swap_extents) { in inc_block_group_ro()
1357 ret = -ETXTBSY; in inc_block_group_ro()
1361 if (cache->ro) { in inc_block_group_ro()
1362 cache->ro++; in inc_block_group_ro()
1367 num_bytes = cache->length - cache->reserved - cache->pinned - in inc_block_group_ro()
1368 cache->bytes_super - cache->zone_unusable - cache->used; in inc_block_group_ro()
1376 } else if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA) { in inc_block_group_ro()
1383 if (sinfo_used + num_bytes <= sinfo->total_bytes) in inc_block_group_ro()
1390 * leeway to allow us to mark this block group as read only. in inc_block_group_ro()
1392 if (btrfs_can_overcommit(cache->fs_info, sinfo, num_bytes, in inc_block_group_ro()
1398 sinfo->bytes_readonly += num_bytes; in inc_block_group_ro()
1399 if (btrfs_is_zoned(cache->fs_info)) { in inc_block_group_ro()
1401 sinfo->bytes_readonly += cache->zone_unusable; in inc_block_group_ro()
1402 btrfs_space_info_update_bytes_zone_unusable(cache->fs_info, sinfo, in inc_block_group_ro()
1403 -cache->zone_unusable); in inc_block_group_ro()
1404 cache->zone_unusable = 0; in inc_block_group_ro()
1406 cache->ro++; in inc_block_group_ro()
1407 list_add_tail(&cache->ro_list, &sinfo->ro_bgs); in inc_block_group_ro()
1410 spin_unlock(&cache->lock); in inc_block_group_ro()
1411 spin_unlock(&sinfo->lock); in inc_block_group_ro()
1412 if (ret == -ENOSPC && btrfs_test_opt(cache->fs_info, ENOSPC_DEBUG)) { in inc_block_group_ro()
1413 btrfs_info(cache->fs_info, in inc_block_group_ro()
1414 "unable to make block group %llu ro", cache->start); in inc_block_group_ro()
1415 btrfs_dump_space_info(cache->fs_info, cache->space_info, 0, 0); in inc_block_group_ro()
1423 struct btrfs_fs_info *fs_info = bg->fs_info; in clean_pinned_extents()
1425 const u64 start = bg->start; in clean_pinned_extents()
1426 const u64 end = start + bg->length - 1; in clean_pinned_extents()
1429 spin_lock(&fs_info->trans_lock); in clean_pinned_extents()
1430 if (trans->transaction->list.prev != &fs_info->trans_list) { in clean_pinned_extents()
1431 prev_trans = list_last_entry(&trans->transaction->list, in clean_pinned_extents()
1433 refcount_inc(&prev_trans->use_count); in clean_pinned_extents()
1435 spin_unlock(&fs_info->trans_lock); in clean_pinned_extents()
1441 * transaction N - 1, and have seen a range belonging to the block in clean_pinned_extents()
1442 * group in pinned_extents before we were able to clear the whole block in clean_pinned_extents()
1444 * the block group after we unpinned it from pinned_extents and removed in clean_pinned_extents()
1447 mutex_lock(&fs_info->unused_bg_unpin_mutex); in clean_pinned_extents()
1449 ret = clear_extent_bits(&prev_trans->pinned_extents, start, end, in clean_pinned_extents()
1455 ret = clear_extent_bits(&trans->transaction->pinned_extents, start, end, in clean_pinned_extents()
1458 mutex_unlock(&fs_info->unused_bg_unpin_mutex); in clean_pinned_extents()
1478 if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags)) in btrfs_delete_unused_bgs()
1488 if (!mutex_trylock(&fs_info->reclaim_bgs_lock)) in btrfs_delete_unused_bgs()
1491 spin_lock(&fs_info->unused_bgs_lock); in btrfs_delete_unused_bgs()
1492 while (!list_empty(&fs_info->unused_bgs)) { in btrfs_delete_unused_bgs()
1496 block_group = list_first_entry(&fs_info->unused_bgs, in btrfs_delete_unused_bgs()
1499 list_del_init(&block_group->bg_list); in btrfs_delete_unused_bgs()
1501 space_info = block_group->space_info; in btrfs_delete_unused_bgs()
1507 spin_unlock(&fs_info->unused_bgs_lock); in btrfs_delete_unused_bgs()
1509 btrfs_discard_cancel_work(&fs_info->discard_ctl, block_group); in btrfs_delete_unused_bgs()
1512 down_write(&space_info->groups_sem); in btrfs_delete_unused_bgs()
1515 * Async discard moves the final block group discard to be prior in btrfs_delete_unused_bgs()
1522 up_write(&space_info->groups_sem); in btrfs_delete_unused_bgs()
1524 btrfs_discard_queue_work(&fs_info->discard_ctl, in btrfs_delete_unused_bgs()
1529 spin_lock(&space_info->lock); in btrfs_delete_unused_bgs()
1530 spin_lock(&block_group->lock); in btrfs_delete_unused_bgs()
1531 if (btrfs_is_block_group_used(block_group) || block_group->ro || in btrfs_delete_unused_bgs()
1532 list_is_singular(&block_group->list)) { in btrfs_delete_unused_bgs()
1535 * outstanding allocations in this block group. We do in btrfs_delete_unused_bgs()
1537 * this block group. in btrfs_delete_unused_bgs()
1540 spin_unlock(&block_group->lock); in btrfs_delete_unused_bgs()
1541 spin_unlock(&space_info->lock); in btrfs_delete_unused_bgs()
1542 up_write(&space_info->groups_sem); in btrfs_delete_unused_bgs()
1547 * The block group may be unused but there may be space reserved in btrfs_delete_unused_bgs()
1548 * accounting with the existence of that block group, that is, in btrfs_delete_unused_bgs()
1549 * space_info->bytes_may_use was incremented by a task but no in btrfs_delete_unused_bgs()
1550 * space was yet allocated from the block group by the task. in btrfs_delete_unused_bgs()
1557 * So check if the total space of the space_info minus the size in btrfs_delete_unused_bgs()
1558 * of this block group is less than the used space of the in btrfs_delete_unused_bgs()
1559 * space_info - if that's the case, then it means we have tasks in btrfs_delete_unused_bgs()
1560 * that might be relying on the block group in order to allocate in btrfs_delete_unused_bgs()
1561 * extents, and add back the block group to the unused list when in btrfs_delete_unused_bgs()
1563 * needing to allocate extents from the block group. in btrfs_delete_unused_bgs()
1566 if (space_info->total_bytes - block_group->length < used && in btrfs_delete_unused_bgs()
1567 block_group->zone_unusable < block_group->length) { in btrfs_delete_unused_bgs()
1571 * fs_info->unused_bgs list. in btrfs_delete_unused_bgs()
1574 list_add_tail(&block_group->bg_list, &retry_list); in btrfs_delete_unused_bgs()
1577 spin_unlock(&block_group->lock); in btrfs_delete_unused_bgs()
1578 spin_unlock(&space_info->lock); in btrfs_delete_unused_bgs()
1579 up_write(&space_info->groups_sem); in btrfs_delete_unused_bgs()
1583 spin_unlock(&block_group->lock); in btrfs_delete_unused_bgs()
1584 spin_unlock(&space_info->lock); in btrfs_delete_unused_bgs()
1588 up_write(&space_info->groups_sem); in btrfs_delete_unused_bgs()
1597 if (ret == -EAGAIN) in btrfs_delete_unused_bgs()
1607 block_group->start); in btrfs_delete_unused_bgs()
1615 * We could have pending pinned extents for this block group, in btrfs_delete_unused_bgs()
1630 spin_lock(&fs_info->discard_ctl.lock); in btrfs_delete_unused_bgs()
1631 if (!list_empty(&block_group->discard_list)) { in btrfs_delete_unused_bgs()
1632 spin_unlock(&fs_info->discard_ctl.lock); in btrfs_delete_unused_bgs()
1634 btrfs_discard_queue_work(&fs_info->discard_ctl, in btrfs_delete_unused_bgs()
1638 spin_unlock(&fs_info->discard_ctl.lock); in btrfs_delete_unused_bgs()
1641 spin_lock(&space_info->lock); in btrfs_delete_unused_bgs()
1642 spin_lock(&block_group->lock); in btrfs_delete_unused_bgs()
1645 -block_group->pinned); in btrfs_delete_unused_bgs()
1646 space_info->bytes_readonly += block_group->pinned; in btrfs_delete_unused_bgs()
1647 block_group->pinned = 0; in btrfs_delete_unused_bgs()
1649 spin_unlock(&block_group->lock); in btrfs_delete_unused_bgs()
1650 spin_unlock(&space_info->lock); in btrfs_delete_unused_bgs()
1653 * The normal path here is an unused block group is passed here, in btrfs_delete_unused_bgs()
1656 * before coming down the unused block group path as trimming in btrfs_delete_unused_bgs()
1664 * need to reset sequential-required zones. in btrfs_delete_unused_bgs()
1677 ret = btrfs_remove_chunk(trans, block_group->start); in btrfs_delete_unused_bgs()
1686 * If we're not mounted with -odiscard, we can just forget in btrfs_delete_unused_bgs()
1687 * about this block group. Otherwise we'll need to wait in btrfs_delete_unused_bgs()
1691 spin_lock(&fs_info->unused_bgs_lock); in btrfs_delete_unused_bgs()
1694 * fs_info->unused_bgs, so use a list_move operation in btrfs_delete_unused_bgs()
1695 * to add the block group to the deleted_bgs list. in btrfs_delete_unused_bgs()
1697 list_move(&block_group->bg_list, in btrfs_delete_unused_bgs()
1698 &trans->transaction->deleted_bgs); in btrfs_delete_unused_bgs()
1699 spin_unlock(&fs_info->unused_bgs_lock); in btrfs_delete_unused_bgs()
1706 spin_lock(&fs_info->unused_bgs_lock); in btrfs_delete_unused_bgs()
1708 list_splice_tail(&retry_list, &fs_info->unused_bgs); in btrfs_delete_unused_bgs()
1709 spin_unlock(&fs_info->unused_bgs_lock); in btrfs_delete_unused_bgs()
1710 mutex_unlock(&fs_info->reclaim_bgs_lock); in btrfs_delete_unused_bgs()
1715 spin_lock(&fs_info->unused_bgs_lock); in btrfs_delete_unused_bgs()
1716 list_splice_tail(&retry_list, &fs_info->unused_bgs); in btrfs_delete_unused_bgs()
1717 spin_unlock(&fs_info->unused_bgs_lock); in btrfs_delete_unused_bgs()
1718 mutex_unlock(&fs_info->reclaim_bgs_lock); in btrfs_delete_unused_bgs()
1725 struct btrfs_fs_info *fs_info = bg->fs_info; in btrfs_mark_bg_unused()
1727 spin_lock(&fs_info->unused_bgs_lock); in btrfs_mark_bg_unused()
1728 if (list_empty(&bg->bg_list)) { in btrfs_mark_bg_unused()
1731 list_add_tail(&bg->bg_list, &fs_info->unused_bgs); in btrfs_mark_bg_unused()
1732 } else if (!test_bit(BLOCK_GROUP_FLAG_NEW, &bg->runtime_flags)) { in btrfs_mark_bg_unused()
1733 /* Pull out the block group from the reclaim_bgs list. */ in btrfs_mark_bg_unused()
1735 list_move_tail(&bg->bg_list, &fs_info->unused_bgs); in btrfs_mark_bg_unused()
1737 spin_unlock(&fs_info->unused_bgs_lock); in btrfs_mark_bg_unused()
1741 * We want block groups with a low number of used bytes to be in the beginning
1752 return bg1->used > bg2->used; in reclaim_bgs_cmp()
1764 const struct btrfs_space_info *space_info = bg->space_info; in should_reclaim_block_group()
1765 const int reclaim_thresh = READ_ONCE(space_info->bg_reclaim_threshold); in should_reclaim_block_group()
1766 const u64 new_val = bg->used; in should_reclaim_block_group()
1773 thresh = mult_perc(bg->length, reclaim_thresh); in should_reclaim_block_group()
1777 * brand new block group and we don't want to relocate new block groups. in should_reclaim_block_group()
1794 if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags)) in btrfs_reclaim_bgs_work()
1803 sb_start_write(fs_info->sb); in btrfs_reclaim_bgs_work()
1806 sb_end_write(fs_info->sb); in btrfs_reclaim_bgs_work()
1814 if (!mutex_trylock(&fs_info->reclaim_bgs_lock)) { in btrfs_reclaim_bgs_work()
1816 sb_end_write(fs_info->sb); in btrfs_reclaim_bgs_work()
1820 spin_lock(&fs_info->unused_bgs_lock); in btrfs_reclaim_bgs_work()
1823 * The block groups might still be in use and reachable via bg_list, in btrfs_reclaim_bgs_work()
1826 list_sort(NULL, &fs_info->reclaim_bgs, reclaim_bgs_cmp); in btrfs_reclaim_bgs_work()
1827 while (!list_empty(&fs_info->reclaim_bgs)) { in btrfs_reclaim_bgs_work()
1831 bg = list_first_entry(&fs_info->reclaim_bgs, in btrfs_reclaim_bgs_work()
1834 list_del_init(&bg->bg_list); in btrfs_reclaim_bgs_work()
1836 space_info = bg->space_info; in btrfs_reclaim_bgs_work()
1837 spin_unlock(&fs_info->unused_bgs_lock); in btrfs_reclaim_bgs_work()
1840 down_write(&space_info->groups_sem); in btrfs_reclaim_bgs_work()
1842 spin_lock(&bg->lock); in btrfs_reclaim_bgs_work()
1843 if (bg->reserved || bg->pinned || bg->ro) { in btrfs_reclaim_bgs_work()
1846 * outstanding allocations in this block group. We do in btrfs_reclaim_bgs_work()
1848 * this block group. in btrfs_reclaim_bgs_work()
1850 spin_unlock(&bg->lock); in btrfs_reclaim_bgs_work()
1851 up_write(&space_info->groups_sem); in btrfs_reclaim_bgs_work()
1854 if (bg->used == 0) { in btrfs_reclaim_bgs_work()
1856 * It is possible that we trigger relocation on a block in btrfs_reclaim_bgs_work()
1862 * for the non-existent extents and running some extra in btrfs_reclaim_bgs_work()
1864 * other mechanisms for dealing with empty block groups. in btrfs_reclaim_bgs_work()
1868 spin_unlock(&bg->lock); in btrfs_reclaim_bgs_work()
1869 up_write(&space_info->groups_sem); in btrfs_reclaim_bgs_work()
1874 * The block group might no longer meet the reclaim condition by in btrfs_reclaim_bgs_work()
1883 if (!should_reclaim_block_group(bg, bg->length)) { in btrfs_reclaim_bgs_work()
1884 spin_unlock(&bg->lock); in btrfs_reclaim_bgs_work()
1885 up_write(&space_info->groups_sem); in btrfs_reclaim_bgs_work()
1888 spin_unlock(&bg->lock); in btrfs_reclaim_bgs_work()
1891 * Get out fast, in case we're read-only or unmounting the in btrfs_reclaim_bgs_work()
1892 * filesystem. It is OK to drop block groups from the list even in btrfs_reclaim_bgs_work()
1893 * for the read-only case. As we did sb_start_write(), in btrfs_reclaim_bgs_work()
1894 * "mount -o remount,ro" won't happen and read-only filesystem in btrfs_reclaim_bgs_work()
1895 * means it is forced read-only due to a fatal error. So, it in btrfs_reclaim_bgs_work()
1896 * never gets back to read-write to let us reclaim again. in btrfs_reclaim_bgs_work()
1899 up_write(&space_info->groups_sem); in btrfs_reclaim_bgs_work()
1904 * Cache the zone_unusable value before turning the block group in btrfs_reclaim_bgs_work()
1906 * zone_unusable value gets moved to the block group's read-only in btrfs_reclaim_bgs_work()
1909 zone_unusable = bg->zone_unusable; in btrfs_reclaim_bgs_work()
1911 up_write(&space_info->groups_sem); in btrfs_reclaim_bgs_work()
1917 bg->start, in btrfs_reclaim_bgs_work()
1918 div64_u64(bg->used * 100, bg->length), in btrfs_reclaim_bgs_work()
1919 div64_u64(zone_unusable * 100, bg->length)); in btrfs_reclaim_bgs_work()
1921 ret = btrfs_relocate_chunk(fs_info, bg->start); in btrfs_reclaim_bgs_work()
1925 bg->start); in btrfs_reclaim_bgs_work()
1931 spin_lock(&fs_info->unused_bgs_lock); in btrfs_reclaim_bgs_work()
1933 * This block group might be added to the unused list in btrfs_reclaim_bgs_work()
1937 if (list_empty(&bg->bg_list)) { in btrfs_reclaim_bgs_work()
1939 list_add_tail(&bg->bg_list, &retry_list); in btrfs_reclaim_bgs_work()
1941 spin_unlock(&fs_info->unused_bgs_lock); in btrfs_reclaim_bgs_work()
1945 mutex_unlock(&fs_info->reclaim_bgs_lock); in btrfs_reclaim_bgs_work()
1947 * Reclaiming all the block groups in the list can take really in btrfs_reclaim_bgs_work()
1948 * long. Prioritize cleaning up unused block groups. in btrfs_reclaim_bgs_work()
1955 if (!mutex_trylock(&fs_info->reclaim_bgs_lock)) in btrfs_reclaim_bgs_work()
1957 spin_lock(&fs_info->unused_bgs_lock); in btrfs_reclaim_bgs_work()
1959 spin_unlock(&fs_info->unused_bgs_lock); in btrfs_reclaim_bgs_work()
1960 mutex_unlock(&fs_info->reclaim_bgs_lock); in btrfs_reclaim_bgs_work()
1962 spin_lock(&fs_info->unused_bgs_lock); in btrfs_reclaim_bgs_work()
1963 list_splice_tail(&retry_list, &fs_info->reclaim_bgs); in btrfs_reclaim_bgs_work()
1964 spin_unlock(&fs_info->unused_bgs_lock); in btrfs_reclaim_bgs_work()
1966 sb_end_write(fs_info->sb); in btrfs_reclaim_bgs_work()
1971 spin_lock(&fs_info->unused_bgs_lock); in btrfs_reclaim_bgs()
1972 if (!list_empty(&fs_info->reclaim_bgs)) in btrfs_reclaim_bgs()
1973 queue_work(system_unbound_wq, &fs_info->reclaim_bgs_work); in btrfs_reclaim_bgs()
1974 spin_unlock(&fs_info->unused_bgs_lock); in btrfs_reclaim_bgs()
1979 struct btrfs_fs_info *fs_info = bg->fs_info; in btrfs_mark_bg_to_reclaim()
1981 spin_lock(&fs_info->unused_bgs_lock); in btrfs_mark_bg_to_reclaim()
1982 if (list_empty(&bg->bg_list)) { in btrfs_mark_bg_to_reclaim()
1985 list_add_tail(&bg->bg_list, &fs_info->reclaim_bgs); in btrfs_mark_bg_to_reclaim()
1987 spin_unlock(&fs_info->unused_bgs_lock); in btrfs_mark_bg_to_reclaim()
2001 slot = path->slots[0]; in read_bg_from_eb()
2002 leaf = path->nodes[0]; in read_bg_from_eb()
2004 em_tree = &fs_info->mapping_tree; in read_bg_from_eb()
2005 read_lock(&em_tree->lock); in read_bg_from_eb()
2006 em = lookup_extent_mapping(em_tree, key->objectid, key->offset); in read_bg_from_eb()
2007 read_unlock(&em_tree->lock); in read_bg_from_eb()
2011 key->objectid, key->offset); in read_bg_from_eb()
2012 return -ENOENT; in read_bg_from_eb()
2015 if (em->start != key->objectid || em->len != key->offset) { in read_bg_from_eb()
2017 "block group %llu len %llu mismatch with chunk %llu len %llu", in read_bg_from_eb()
2018 key->objectid, key->offset, em->start, em->len); in read_bg_from_eb()
2019 ret = -EUCLEAN; in read_bg_from_eb()
2028 if (flags != (em->map_lookup->type & BTRFS_BLOCK_GROUP_TYPE_MASK)) { in read_bg_from_eb()
2030 "block group %llu len %llu type flags 0x%llx mismatch with chunk type flags 0x%llx", in read_bg_from_eb()
2031 key->objectid, key->offset, flags, in read_bg_from_eb()
2032 (BTRFS_BLOCK_GROUP_TYPE_MASK & em->map_lookup->type)); in read_bg_from_eb()
2033 ret = -EUCLEAN; in read_bg_from_eb()
2050 if (found_key.objectid >= key->objectid && in find_first_block_group()
2063 write_seqlock(&fs_info->profiles_lock); in set_avail_alloc_bits()
2065 fs_info->avail_data_alloc_bits |= extra_flags; in set_avail_alloc_bits()
2067 fs_info->avail_metadata_alloc_bits |= extra_flags; in set_avail_alloc_bits()
2069 fs_info->avail_system_alloc_bits |= extra_flags; in set_avail_alloc_bits()
2070 write_sequnlock(&fs_info->profiles_lock); in set_avail_alloc_bits()
2077 * @chunk_start: logical address of block group
2081 * @stripe_len: size of IO stripe for the given block group
2084 * Used primarily to exclude those portions of a block group that contain super
2085 * block copies.
2096 int i, nr = 0; in btrfs_rmap_block() local
2101 return -EIO; in btrfs_rmap_block()
2103 map = em->map_lookup; in btrfs_rmap_block()
2104 data_stripe_length = em->orig_block_len; in btrfs_rmap_block()
2106 chunk_start = em->start; in btrfs_rmap_block()
2109 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) in btrfs_rmap_block()
2112 buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS); in btrfs_rmap_block()
2114 ret = -ENOMEM; in btrfs_rmap_block()
2118 for (i = 0; i < map->num_stripes; i++) { in btrfs_rmap_block()
2124 if (!in_range(physical, map->stripes[i].physical, in btrfs_rmap_block()
2128 stripe_nr = (physical - map->stripes[i].physical) >> in btrfs_rmap_block()
2130 offset = (physical - map->stripes[i].physical) & in btrfs_rmap_block()
2133 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | in btrfs_rmap_block()
2135 stripe_nr = div_u64(stripe_nr * map->num_stripes + i, in btrfs_rmap_block()
2136 map->sub_stripes); in btrfs_rmap_block()
2140 * instead of map->stripe_len in btrfs_rmap_block()
2164 static int exclude_super_stripes(struct btrfs_block_group *cache) in exclude_super_stripes() argument
2166 struct btrfs_fs_info *fs_info = cache->fs_info; in exclude_super_stripes()
2171 int i, nr, ret; in exclude_super_stripes() local
2173 if (cache->start < BTRFS_SUPER_INFO_OFFSET) { in exclude_super_stripes()
2174 stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->start; in exclude_super_stripes()
2175 cache->bytes_super += stripe_len; in exclude_super_stripes()
2176 ret = set_extent_bit(&fs_info->excluded_extents, cache->start, in exclude_super_stripes()
2177 cache->start + stripe_len - 1, in exclude_super_stripes()
2183 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { in exclude_super_stripes()
2184 bytenr = btrfs_sb_offset(i); in exclude_super_stripes()
2185 ret = btrfs_rmap_block(fs_info, cache->start, in exclude_super_stripes()
2194 "zoned: block group %llu must not contain super block", in exclude_super_stripes()
2195 cache->start); in exclude_super_stripes()
2196 return -EUCLEAN; in exclude_super_stripes()
2199 while (nr--) { in exclude_super_stripes()
2201 cache->start + cache->length - logical[nr]); in exclude_super_stripes()
2203 cache->bytes_super += len; in exclude_super_stripes()
2204 ret = set_extent_bit(&fs_info->excluded_extents, logical[nr], in exclude_super_stripes()
2205 logical[nr] + len - 1, in exclude_super_stripes()
2221 struct btrfs_block_group *cache; in btrfs_create_block_group_cache() local
2223 cache = kzalloc(sizeof(*cache), GFP_NOFS); in btrfs_create_block_group_cache()
2224 if (!cache) in btrfs_create_block_group_cache()
2227 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl), in btrfs_create_block_group_cache()
2229 if (!cache->free_space_ctl) { in btrfs_create_block_group_cache()
2230 kfree(cache); in btrfs_create_block_group_cache()
2234 cache->start = start; in btrfs_create_block_group_cache()
2236 cache->fs_info = fs_info; in btrfs_create_block_group_cache()
2237 cache->full_stripe_len = btrfs_full_stripe_len(fs_info, start); in btrfs_create_block_group_cache()
2239 cache->discard_index = BTRFS_DISCARD_INDEX_UNUSED; in btrfs_create_block_group_cache()
2241 refcount_set(&cache->refs, 1); in btrfs_create_block_group_cache()
2242 spin_lock_init(&cache->lock); in btrfs_create_block_group_cache()
2243 init_rwsem(&cache->data_rwsem); in btrfs_create_block_group_cache()
2244 INIT_LIST_HEAD(&cache->list); in btrfs_create_block_group_cache()
2245 INIT_LIST_HEAD(&cache->cluster_list); in btrfs_create_block_group_cache()
2246 INIT_LIST_HEAD(&cache->bg_list); in btrfs_create_block_group_cache()
2247 INIT_LIST_HEAD(&cache->ro_list); in btrfs_create_block_group_cache()
2248 INIT_LIST_HEAD(&cache->discard_list); in btrfs_create_block_group_cache()
2249 INIT_LIST_HEAD(&cache->dirty_list); in btrfs_create_block_group_cache()
2250 INIT_LIST_HEAD(&cache->io_list); in btrfs_create_block_group_cache()
2251 INIT_LIST_HEAD(&cache->active_bg_list); in btrfs_create_block_group_cache()
2252 btrfs_init_free_space_ctl(cache, cache->free_space_ctl); in btrfs_create_block_group_cache()
2253 atomic_set(&cache->frozen, 0); in btrfs_create_block_group_cache()
2254 mutex_init(&cache->free_space_lock); in btrfs_create_block_group_cache()
2256 return cache; in btrfs_create_block_group_cache()
2260 * Iterate all chunks and verify that each of them has the corresponding block
2265 struct extent_map_tree *map_tree = &fs_info->mapping_tree; in check_chunk_block_group_mappings()
2272 read_lock(&map_tree->lock); in check_chunk_block_group_mappings()
2279 read_unlock(&map_tree->lock); in check_chunk_block_group_mappings()
2283 bg = btrfs_lookup_block_group(fs_info, em->start); in check_chunk_block_group_mappings()
2286 "chunk start=%llu len=%llu doesn't have corresponding block group", in check_chunk_block_group_mappings()
2287 em->start, em->len); in check_chunk_block_group_mappings()
2288 ret = -EUCLEAN; in check_chunk_block_group_mappings()
2292 if (bg->start != em->start || bg->length != em->len || in check_chunk_block_group_mappings()
2293 (bg->flags & BTRFS_BLOCK_GROUP_TYPE_MASK) != in check_chunk_block_group_mappings()
2294 (em->map_lookup->type & BTRFS_BLOCK_GROUP_TYPE_MASK)) { in check_chunk_block_group_mappings()
2296 "chunk start=%llu len=%llu flags=0x%llx doesn't match block group start=%llu len=%llu flags=0x%llx", in check_chunk_block_group_mappings()
2297 em->start, em->len, in check_chunk_block_group_mappings()
2298 em->map_lookup->type & BTRFS_BLOCK_GROUP_TYPE_MASK, in check_chunk_block_group_mappings()
2299 bg->start, bg->length, in check_chunk_block_group_mappings()
2300 bg->flags & BTRFS_BLOCK_GROUP_TYPE_MASK); in check_chunk_block_group_mappings()
2301 ret = -EUCLEAN; in check_chunk_block_group_mappings()
2306 start = em->start + em->len; in check_chunk_block_group_mappings()
2318 struct btrfs_block_group *cache; in read_one_block_group() local
2322 ASSERT(key->type == BTRFS_BLOCK_GROUP_ITEM_KEY); in read_one_block_group()
2324 cache = btrfs_create_block_group_cache(info, key->objectid); in read_one_block_group()
2325 if (!cache) in read_one_block_group()
2326 return -ENOMEM; in read_one_block_group()
2328 cache->length = key->offset; in read_one_block_group()
2329 cache->used = btrfs_stack_block_group_used(bgi); in read_one_block_group()
2330 cache->commit_used = cache->used; in read_one_block_group()
2331 cache->flags = btrfs_stack_block_group_flags(bgi); in read_one_block_group()
2332 cache->global_root_id = btrfs_stack_block_group_chunk_objectid(bgi); in read_one_block_group()
2334 set_free_space_tree_thresholds(cache); in read_one_block_group()
2338 * When we mount with old space cache, we need to in read_one_block_group()
2342 * truncate the old free space cache inode and in read_one_block_group()
2345 * the new space cache info onto disk. in read_one_block_group()
2348 cache->disk_cache_state = BTRFS_DC_CLEAR; in read_one_block_group()
2350 if (!mixed && ((cache->flags & BTRFS_BLOCK_GROUP_METADATA) && in read_one_block_group()
2351 (cache->flags & BTRFS_BLOCK_GROUP_DATA))) { in read_one_block_group()
2353 "bg %llu is a mixed block group but filesystem hasn't enabled mixed block groups", in read_one_block_group()
2354 cache->start); in read_one_block_group()
2355 ret = -EINVAL; in read_one_block_group()
2359 ret = btrfs_load_block_group_zone_info(cache, false); in read_one_block_group()
2362 cache->start); in read_one_block_group()
2371 ret = exclude_super_stripes(cache); in read_one_block_group()
2374 btrfs_free_excluded_extents(cache); in read_one_block_group()
2380 * free space for a block group. So, we don't need any caching work. in read_one_block_group()
2391 btrfs_calc_zone_unusable(cache); in read_one_block_group()
2393 btrfs_free_excluded_extents(cache); in read_one_block_group()
2394 } else if (cache->length == cache->used) { in read_one_block_group()
2395 cache->cached = BTRFS_CACHE_FINISHED; in read_one_block_group()
2396 btrfs_free_excluded_extents(cache); in read_one_block_group()
2397 } else if (cache->used == 0) { in read_one_block_group()
2398 cache->cached = BTRFS_CACHE_FINISHED; in read_one_block_group()
2399 ret = btrfs_add_new_free_space(cache, cache->start, in read_one_block_group()
2400 cache->start + cache->length, NULL); in read_one_block_group()
2401 btrfs_free_excluded_extents(cache); in read_one_block_group()
2406 ret = btrfs_add_block_group_cache(info, cache); in read_one_block_group()
2408 btrfs_remove_free_space_cache(cache); in read_one_block_group()
2411 trace_btrfs_add_block_group(info, cache, 0); in read_one_block_group()
2412 btrfs_add_bg_to_space_info(info, cache); in read_one_block_group()
2414 set_avail_alloc_bits(info, cache->flags); in read_one_block_group()
2415 if (btrfs_chunk_writeable(info, cache->start)) { in read_one_block_group()
2416 if (cache->used == 0) { in read_one_block_group()
2417 ASSERT(list_empty(&cache->bg_list)); in read_one_block_group()
2419 btrfs_discard_queue_work(&info->discard_ctl, cache); in read_one_block_group()
2421 btrfs_mark_bg_unused(cache); in read_one_block_group()
2424 inc_block_group_ro(cache, 1); in read_one_block_group()
2429 btrfs_put_block_group(cache); in read_one_block_group()
2435 struct extent_map_tree *em_tree = &fs_info->mapping_tree; in fill_dummy_bgs()
2439 for (node = rb_first_cached(&em_tree->map); node; node = rb_next(node)) { in fill_dummy_bgs()
2445 map = em->map_lookup; in fill_dummy_bgs()
2446 bg = btrfs_create_block_group_cache(fs_info, em->start); in fill_dummy_bgs()
2448 ret = -ENOMEM; in fill_dummy_bgs()
2452 /* Fill dummy cache as FULL */ in fill_dummy_bgs()
2453 bg->length = em->len; in fill_dummy_bgs()
2454 bg->flags = map->type; in fill_dummy_bgs()
2455 bg->cached = BTRFS_CACHE_FINISHED; in fill_dummy_bgs()
2456 bg->used = em->len; in fill_dummy_bgs()
2457 bg->flags = map->type; in fill_dummy_bgs()
2460 * We may have some valid block group cache added already, in in fill_dummy_bgs()
2463 if (ret == -EEXIST) { in fill_dummy_bgs()
2477 set_avail_alloc_bits(fs_info, bg->flags); in fill_dummy_bgs()
2489 struct btrfs_block_group *cache; in btrfs_read_block_groups() local
2497 * unsupported RO options. The fs can never be mounted read-write, so no in btrfs_read_block_groups()
2498 * need to waste time searching block group items. in btrfs_read_block_groups()
2503 if (!root || (btrfs_super_compat_ro_flags(info->super_copy) & in btrfs_read_block_groups()
2512 return -ENOMEM; in btrfs_read_block_groups()
2514 cache_gen = btrfs_super_cache_generation(info->super_copy); in btrfs_read_block_groups()
2516 btrfs_super_generation(info->super_copy) != cache_gen) in btrfs_read_block_groups()
2532 leaf = path->nodes[0]; in btrfs_read_block_groups()
2533 slot = path->slots[0]; in btrfs_read_block_groups()
2548 list_for_each_entry(space_info, &info->space_info, list) { in btrfs_read_block_groups()
2549 int i; in btrfs_read_block_groups() local
2551 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) { in btrfs_read_block_groups()
2552 if (list_empty(&space_info->block_groups[i])) in btrfs_read_block_groups()
2554 cache = list_first_entry(&space_info->block_groups[i], in btrfs_read_block_groups()
2557 btrfs_sysfs_add_block_group_type(cache); in btrfs_read_block_groups()
2560 if (!(btrfs_get_alloc_profile(info, space_info->flags) & in btrfs_read_block_groups()
2567 * Avoid allocating from un-mirrored block group if there are in btrfs_read_block_groups()
2568 * mirrored block groups. in btrfs_read_block_groups()
2570 list_for_each_entry(cache, in btrfs_read_block_groups()
2571 &space_info->block_groups[BTRFS_RAID_RAID0], in btrfs_read_block_groups()
2573 inc_block_group_ro(cache, 1); in btrfs_read_block_groups()
2574 list_for_each_entry(cache, in btrfs_read_block_groups()
2575 &space_info->block_groups[BTRFS_RAID_SINGLE], in btrfs_read_block_groups()
2577 inc_block_group_ro(cache, 1); in btrfs_read_block_groups()
2587 * Try to fill the tree using dummy block groups so that the user can in btrfs_read_block_groups()
2605 struct btrfs_fs_info *fs_info = trans->fs_info; in insert_block_group_item()
2612 spin_lock(&block_group->lock); in insert_block_group_item()
2613 btrfs_set_stack_block_group_used(&bgi, block_group->used); in insert_block_group_item()
2615 block_group->global_root_id); in insert_block_group_item()
2616 btrfs_set_stack_block_group_flags(&bgi, block_group->flags); in insert_block_group_item()
2617 old_commit_used = block_group->commit_used; in insert_block_group_item()
2618 block_group->commit_used = block_group->used; in insert_block_group_item()
2619 key.objectid = block_group->start; in insert_block_group_item()
2621 key.offset = block_group->length; in insert_block_group_item()
2622 spin_unlock(&block_group->lock); in insert_block_group_item()
2626 spin_lock(&block_group->lock); in insert_block_group_item()
2627 block_group->commit_used = old_commit_used; in insert_block_group_item()
2628 spin_unlock(&block_group->lock); in insert_block_group_item()
2638 struct btrfs_fs_info *fs_info = device->fs_info; in insert_dev_extent()
2639 struct btrfs_root *root = fs_info->dev_root; in insert_dev_extent()
2646 WARN_ON(!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state)); in insert_dev_extent()
2647 WARN_ON(test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)); in insert_dev_extent()
2650 return -ENOMEM; in insert_dev_extent()
2652 key.objectid = device->devid; in insert_dev_extent()
2659 leaf = path->nodes[0]; in insert_dev_extent()
2660 extent = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_extent); in insert_dev_extent()
2682 struct btrfs_fs_info *fs_info = trans->fs_info; in insert_dev_extents()
2688 int i; in insert_dev_extents() local
2695 map = em->map_lookup; in insert_dev_extents()
2696 stripe_size = em->orig_block_len; in insert_dev_extents()
2703 * (dev-replace.c:btrfs_dev_replace_finishing()), so we could grab the in insert_dev_extents()
2707 mutex_lock(&fs_info->fs_devices->device_list_mutex); in insert_dev_extents()
2708 for (i = 0; i < map->num_stripes; i++) { in insert_dev_extents()
2709 device = map->stripes[i].dev; in insert_dev_extents()
2710 dev_offset = map->stripes[i].physical; in insert_dev_extents()
2717 mutex_unlock(&fs_info->fs_devices->device_list_mutex); in insert_dev_extents()
2732 struct btrfs_fs_info *fs_info = trans->fs_info; in btrfs_create_pending_block_groups()
2736 while (!list_empty(&trans->new_bgs)) { in btrfs_create_pending_block_groups()
2739 block_group = list_first_entry(&trans->new_bgs, in btrfs_create_pending_block_groups()
2745 index = btrfs_bg_flags_to_raid_index(block_group->flags); in btrfs_create_pending_block_groups()
2751 &block_group->runtime_flags)) { in btrfs_create_pending_block_groups()
2752 mutex_lock(&fs_info->chunk_mutex); in btrfs_create_pending_block_groups()
2754 mutex_unlock(&fs_info->chunk_mutex); in btrfs_create_pending_block_groups()
2758 ret = insert_dev_extents(trans, block_group->start, in btrfs_create_pending_block_groups()
2759 block_group->length); in btrfs_create_pending_block_groups()
2770 if (block_group->space_info->block_group_kobjs[index] == NULL) in btrfs_create_pending_block_groups()
2776 list_del_init(&block_group->bg_list); in btrfs_create_pending_block_groups()
2777 clear_bit(BLOCK_GROUP_FLAG_NEW, &block_group->runtime_flags); in btrfs_create_pending_block_groups()
2783 * For extent tree v2 we use the block_group_item->chunk_offset to point at our
2795 if (btrfs_super_total_bytes(fs_info->super_copy) <= (SZ_1G * 10ULL)) in calculate_global_root_id()
2799 div64_u64_rem(offset, fs_info->nr_global_roots, &index); in calculate_global_root_id()
2805 u64 chunk_offset, u64 size) in btrfs_make_block_group() argument
2807 struct btrfs_fs_info *fs_info = trans->fs_info; in btrfs_make_block_group()
2808 struct btrfs_block_group *cache; in btrfs_make_block_group() local
2813 cache = btrfs_create_block_group_cache(fs_info, chunk_offset); in btrfs_make_block_group()
2814 if (!cache) in btrfs_make_block_group()
2815 return ERR_PTR(-ENOMEM); in btrfs_make_block_group()
2818 * Mark it as new before adding it to the rbtree of block groups or any in btrfs_make_block_group()
2822 set_bit(BLOCK_GROUP_FLAG_NEW, &cache->runtime_flags); in btrfs_make_block_group()
2824 cache->length = size; in btrfs_make_block_group()
2825 set_free_space_tree_thresholds(cache); in btrfs_make_block_group()
2826 cache->flags = type; in btrfs_make_block_group()
2827 cache->cached = BTRFS_CACHE_FINISHED; in btrfs_make_block_group()
2828 cache->global_root_id = calculate_global_root_id(fs_info, cache->start); in btrfs_make_block_group()
2831 set_bit(BLOCK_GROUP_FLAG_NEEDS_FREE_SPACE, &cache->runtime_flags); in btrfs_make_block_group()
2833 ret = btrfs_load_block_group_zone_info(cache, true); in btrfs_make_block_group()
2835 btrfs_put_block_group(cache); in btrfs_make_block_group()
2839 ret = exclude_super_stripes(cache); in btrfs_make_block_group()
2842 btrfs_free_excluded_extents(cache); in btrfs_make_block_group()
2843 btrfs_put_block_group(cache); in btrfs_make_block_group()
2847 ret = btrfs_add_new_free_space(cache, chunk_offset, chunk_offset + size, NULL); in btrfs_make_block_group()
2848 btrfs_free_excluded_extents(cache); in btrfs_make_block_group()
2850 btrfs_put_block_group(cache); in btrfs_make_block_group()
2856 * assigned to our block group. We want our bg to be added to the rbtree in btrfs_make_block_group()
2857 * with its ->space_info set. in btrfs_make_block_group()
2859 cache->space_info = btrfs_find_space_info(fs_info, cache->flags); in btrfs_make_block_group()
2860 ASSERT(cache->space_info); in btrfs_make_block_group()
2862 ret = btrfs_add_block_group_cache(fs_info, cache); in btrfs_make_block_group()
2864 btrfs_remove_free_space_cache(cache); in btrfs_make_block_group()
2865 btrfs_put_block_group(cache); in btrfs_make_block_group()
2870 * Now that our block group has its ->space_info set and is inserted in in btrfs_make_block_group()
2873 trace_btrfs_add_block_group(fs_info, cache, 1); in btrfs_make_block_group()
2874 btrfs_add_bg_to_space_info(fs_info, cache); in btrfs_make_block_group()
2878 if (btrfs_should_fragment_free_space(cache)) { in btrfs_make_block_group()
2879 cache->space_info->bytes_used += size >> 1; in btrfs_make_block_group()
2880 fragment_free_space(cache); in btrfs_make_block_group()
2884 list_add_tail(&cache->bg_list, &trans->new_bgs); in btrfs_make_block_group()
2885 trans->delayed_ref_updates++; in btrfs_make_block_group()
2889 return cache; in btrfs_make_block_group()
2893 * Mark one block group RO, can be called several times for the same block
2896 * @cache: the destination block group
2897 * @do_chunk_alloc: whether need to do chunk pre-allocation, this is to
2899 * block group RO.
2901 int btrfs_inc_block_group_ro(struct btrfs_block_group *cache, in btrfs_inc_block_group_ro() argument
2904 struct btrfs_fs_info *fs_info = cache->fs_info; in btrfs_inc_block_group_ro()
2912 * This can only happen when we are doing read-only scrub on read-only in btrfs_inc_block_group_ro()
2914 * In that case we should not start a new transaction on read-only fs. in btrfs_inc_block_group_ro()
2917 if (sb_rdonly(fs_info->sb)) { in btrfs_inc_block_group_ro()
2918 mutex_lock(&fs_info->ro_block_group_mutex); in btrfs_inc_block_group_ro()
2919 ret = inc_block_group_ro(cache, 0); in btrfs_inc_block_group_ro()
2920 mutex_unlock(&fs_info->ro_block_group_mutex); in btrfs_inc_block_group_ro()
2932 * We're not allowed to set block groups readonly after the dirty in btrfs_inc_block_group_ro()
2933 * block group cache has started writing. If it already started, in btrfs_inc_block_group_ro()
2936 mutex_lock(&fs_info->ro_block_group_mutex); in btrfs_inc_block_group_ro()
2937 if (test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &trans->transaction->flags)) { in btrfs_inc_block_group_ro()
2938 u64 transid = trans->transid; in btrfs_inc_block_group_ro()
2940 mutex_unlock(&fs_info->ro_block_group_mutex); in btrfs_inc_block_group_ro()
2953 * corresponding block group with the new raid level. in btrfs_inc_block_group_ro()
2955 alloc_flags = btrfs_get_alloc_profile(fs_info, cache->flags); in btrfs_inc_block_group_ro()
2956 if (alloc_flags != cache->flags) { in btrfs_inc_block_group_ro()
2963 if (ret == -ENOSPC) in btrfs_inc_block_group_ro()
2970 ret = inc_block_group_ro(cache, 0); in btrfs_inc_block_group_ro()
2973 if (ret == -ETXTBSY) in btrfs_inc_block_group_ro()
2979 * we still want to try our best to mark the block group read-only. in btrfs_inc_block_group_ro()
2981 if (!do_chunk_alloc && ret == -ENOSPC && in btrfs_inc_block_group_ro()
2982 (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM)) in btrfs_inc_block_group_ro()
2985 alloc_flags = btrfs_get_alloc_profile(fs_info, cache->space_info->flags); in btrfs_inc_block_group_ro()
2993 ret = btrfs_zoned_activate_one_bg(fs_info, cache->space_info, true); in btrfs_inc_block_group_ro()
2997 ret = inc_block_group_ro(cache, 0); in btrfs_inc_block_group_ro()
2998 if (ret == -ETXTBSY) in btrfs_inc_block_group_ro()
3001 if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) { in btrfs_inc_block_group_ro()
3002 alloc_flags = btrfs_get_alloc_profile(fs_info, cache->flags); in btrfs_inc_block_group_ro()
3003 mutex_lock(&fs_info->chunk_mutex); in btrfs_inc_block_group_ro()
3005 mutex_unlock(&fs_info->chunk_mutex); in btrfs_inc_block_group_ro()
3008 mutex_unlock(&fs_info->ro_block_group_mutex); in btrfs_inc_block_group_ro()
3014 void btrfs_dec_block_group_ro(struct btrfs_block_group *cache) in btrfs_dec_block_group_ro() argument
3016 struct btrfs_space_info *sinfo = cache->space_info; in btrfs_dec_block_group_ro()
3019 BUG_ON(!cache->ro); in btrfs_dec_block_group_ro()
3021 spin_lock(&sinfo->lock); in btrfs_dec_block_group_ro()
3022 spin_lock(&cache->lock); in btrfs_dec_block_group_ro()
3023 if (!--cache->ro) { in btrfs_dec_block_group_ro()
3024 if (btrfs_is_zoned(cache->fs_info)) { in btrfs_dec_block_group_ro()
3026 cache->zone_unusable = in btrfs_dec_block_group_ro()
3027 (cache->alloc_offset - cache->used - cache->pinned - in btrfs_dec_block_group_ro()
3028 cache->reserved) + in btrfs_dec_block_group_ro()
3029 (cache->length - cache->zone_capacity); in btrfs_dec_block_group_ro()
3030 btrfs_space_info_update_bytes_zone_unusable(cache->fs_info, sinfo, in btrfs_dec_block_group_ro()
3031 cache->zone_unusable); in btrfs_dec_block_group_ro()
3032 sinfo->bytes_readonly -= cache->zone_unusable; in btrfs_dec_block_group_ro()
3034 num_bytes = cache->length - cache->reserved - in btrfs_dec_block_group_ro()
3035 cache->pinned - cache->bytes_super - in btrfs_dec_block_group_ro()
3036 cache->zone_unusable - cache->used; in btrfs_dec_block_group_ro()
3037 sinfo->bytes_readonly -= num_bytes; in btrfs_dec_block_group_ro()
3038 list_del_init(&cache->ro_list); in btrfs_dec_block_group_ro()
3040 spin_unlock(&cache->lock); in btrfs_dec_block_group_ro()
3041 spin_unlock(&sinfo->lock); in btrfs_dec_block_group_ro()
3046 struct btrfs_block_group *cache) in update_block_group_item() argument
3048 struct btrfs_fs_info *fs_info = trans->fs_info; in update_block_group_item()
3059 * Block group items update can be triggered out of commit transaction in update_block_group_item()
3061 * We cannot use cache->used directly outside of the spin lock, as it in update_block_group_item()
3064 spin_lock(&cache->lock); in update_block_group_item()
3065 old_commit_used = cache->commit_used; in update_block_group_item()
3066 used = cache->used; in update_block_group_item()
3068 if (cache->commit_used == used) { in update_block_group_item()
3069 spin_unlock(&cache->lock); in update_block_group_item()
3072 cache->commit_used = used; in update_block_group_item()
3073 spin_unlock(&cache->lock); in update_block_group_item()
3075 key.objectid = cache->start; in update_block_group_item()
3077 key.offset = cache->length; in update_block_group_item()
3082 ret = -ENOENT; in update_block_group_item()
3086 leaf = path->nodes[0]; in update_block_group_item()
3087 bi = btrfs_item_ptr_offset(leaf, path->slots[0]); in update_block_group_item()
3090 cache->global_root_id); in update_block_group_item()
3091 btrfs_set_stack_block_group_flags(&bgi, cache->flags); in update_block_group_item()
3097 * We didn't update the block group item, need to revert commit_used in update_block_group_item()
3098 * unless the block group item didn't exist yet - this is to prevent a in update_block_group_item()
3099 * race with a concurrent insertion of the block group item, with in update_block_group_item()
3102 * insertion set it to a value greater than 0 - if the block group later in update_block_group_item()
3105 if (ret < 0 && ret != -ENOENT) { in update_block_group_item()
3106 spin_lock(&cache->lock); in update_block_group_item()
3107 cache->commit_used = old_commit_used; in update_block_group_item()
3108 spin_unlock(&cache->lock); in update_block_group_item()
3118 struct btrfs_fs_info *fs_info = block_group->fs_info; in cache_save_setup()
3119 struct btrfs_root *root = fs_info->tree_root; in cache_save_setup()
3132 * If this block group is smaller than 100 megs don't bother caching the in cache_save_setup()
3133 * block group. in cache_save_setup()
3135 if (block_group->length < (100 * SZ_1M)) { in cache_save_setup()
3136 spin_lock(&block_group->lock); in cache_save_setup()
3137 block_group->disk_cache_state = BTRFS_DC_WRITTEN; in cache_save_setup()
3138 spin_unlock(&block_group->lock); in cache_save_setup()
3146 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) { in cache_save_setup()
3156 if (block_group->ro) in cache_save_setup()
3167 * from here on out we know not to trust this cache when we load up next in cache_save_setup()
3170 BTRFS_I(inode)->generation = 0; in cache_save_setup()
3175 * super cache generation to 0 so we know to invalidate the in cache_save_setup()
3176 * cache, but then we'd have to keep track of the block groups in cache_save_setup()
3177 * that fail this way so we know we _have_ to reset this cache in cache_save_setup()
3178 * before the next commit or risk reading stale cache. So to in cache_save_setup()
3189 if (block_group->cache_generation == trans->transid && in cache_save_setup()
3197 &fs_info->global_block_rsv); in cache_save_setup()
3206 spin_lock(&block_group->lock); in cache_save_setup()
3207 if (block_group->cached != BTRFS_CACHE_FINISHED || in cache_save_setup()
3216 spin_unlock(&block_group->lock); in cache_save_setup()
3219 spin_unlock(&block_group->lock); in cache_save_setup()
3222 * We hit an ENOSPC when setting up the cache in this transaction, just in cache_save_setup()
3223 * skip doing the setup, we've already cleared the cache so we're safe. in cache_save_setup()
3225 if (test_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags)) { in cache_save_setup()
3226 ret = -ENOSPC; in cache_save_setup()
3231 * Try to preallocate enough space based on how big the block group is. in cache_save_setup()
3234 * cache. in cache_save_setup()
3236 cache_size = div_u64(block_group->length, SZ_256M); in cache_save_setup()
3241 cache_size *= fs_info->sectorsize; in cache_save_setup()
3252 * Our cache requires contiguous chunks so that we don't modify a bunch in cache_save_setup()
3253 * of metadata or split extents when writing the cache out, which means in cache_save_setup()
3256 * other block groups for this transaction, maybe we'll unpin enough in cache_save_setup()
3261 else if (ret == -ENOSPC) in cache_save_setup()
3262 set_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags); in cache_save_setup()
3269 spin_lock(&block_group->lock); in cache_save_setup()
3271 block_group->cache_generation = trans->transid; in cache_save_setup()
3272 block_group->disk_cache_state = dcs; in cache_save_setup()
3273 spin_unlock(&block_group->lock); in cache_save_setup()
3281 struct btrfs_fs_info *fs_info = trans->fs_info; in btrfs_setup_space_cache()
3282 struct btrfs_block_group *cache, *tmp; in btrfs_setup_space_cache() local
3283 struct btrfs_transaction *cur_trans = trans->transaction; in btrfs_setup_space_cache()
3286 if (list_empty(&cur_trans->dirty_bgs) || in btrfs_setup_space_cache()
3292 return -ENOMEM; in btrfs_setup_space_cache()
3294 /* Could add new block groups, use _safe just in case */ in btrfs_setup_space_cache()
3295 list_for_each_entry_safe(cache, tmp, &cur_trans->dirty_bgs, in btrfs_setup_space_cache()
3297 if (cache->disk_cache_state == BTRFS_DC_CLEAR) in btrfs_setup_space_cache()
3298 cache_save_setup(cache, trans, path); in btrfs_setup_space_cache()
3306 * Transaction commit does final block group cache writeback during a critical
3308 * order for the cache to actually match the block group, but can introduce a
3311 * So, btrfs_start_dirty_block_groups is here to kick off block group cache IO.
3312 * There's a chance we'll have to redo some of it if the block group changes
3314 * getting rid of the easy block groups while we're still allowing others to
3319 struct btrfs_fs_info *fs_info = trans->fs_info; in btrfs_start_dirty_block_groups()
3320 struct btrfs_block_group *cache; in btrfs_start_dirty_block_groups() local
3321 struct btrfs_transaction *cur_trans = trans->transaction; in btrfs_start_dirty_block_groups()
3326 struct list_head *io = &cur_trans->io_bgs; in btrfs_start_dirty_block_groups()
3329 spin_lock(&cur_trans->dirty_bgs_lock); in btrfs_start_dirty_block_groups()
3330 if (list_empty(&cur_trans->dirty_bgs)) { in btrfs_start_dirty_block_groups()
3331 spin_unlock(&cur_trans->dirty_bgs_lock); in btrfs_start_dirty_block_groups()
3334 list_splice_init(&cur_trans->dirty_bgs, &dirty); in btrfs_start_dirty_block_groups()
3335 spin_unlock(&cur_trans->dirty_bgs_lock); in btrfs_start_dirty_block_groups()
3338 /* Make sure all the block groups on our dirty list actually exist */ in btrfs_start_dirty_block_groups()
3344 ret = -ENOMEM; in btrfs_start_dirty_block_groups()
3351 * removal of empty block groups deleting this block group while we are in btrfs_start_dirty_block_groups()
3352 * writing out the cache in btrfs_start_dirty_block_groups()
3354 mutex_lock(&trans->transaction->cache_write_mutex); in btrfs_start_dirty_block_groups()
3358 cache = list_first_entry(&dirty, struct btrfs_block_group, in btrfs_start_dirty_block_groups()
3361 * This can happen if something re-dirties a block group that in btrfs_start_dirty_block_groups()
3365 if (!list_empty(&cache->io_list)) { in btrfs_start_dirty_block_groups()
3366 list_del_init(&cache->io_list); in btrfs_start_dirty_block_groups()
3367 btrfs_wait_cache_io(trans, cache, path); in btrfs_start_dirty_block_groups()
3368 btrfs_put_block_group(cache); in btrfs_start_dirty_block_groups()
3373 * btrfs_wait_cache_io uses the cache->dirty_list to decide if in btrfs_start_dirty_block_groups()
3380 spin_lock(&cur_trans->dirty_bgs_lock); in btrfs_start_dirty_block_groups()
3381 list_del_init(&cache->dirty_list); in btrfs_start_dirty_block_groups()
3382 spin_unlock(&cur_trans->dirty_bgs_lock); in btrfs_start_dirty_block_groups()
3386 cache_save_setup(cache, trans, path); in btrfs_start_dirty_block_groups()
3388 if (cache->disk_cache_state == BTRFS_DC_SETUP) { in btrfs_start_dirty_block_groups()
3389 cache->io_ctl.inode = NULL; in btrfs_start_dirty_block_groups()
3390 ret = btrfs_write_out_cache(trans, cache, path); in btrfs_start_dirty_block_groups()
3391 if (ret == 0 && cache->io_ctl.inode) { in btrfs_start_dirty_block_groups()
3399 list_add_tail(&cache->io_list, io); in btrfs_start_dirty_block_groups()
3402 * If we failed to write the cache, the in btrfs_start_dirty_block_groups()
3409 ret = update_block_group_item(trans, path, cache); in btrfs_start_dirty_block_groups()
3411 * Our block group might still be attached to the list in btrfs_start_dirty_block_groups()
3412 * of new block groups in the transaction handle of some in btrfs_start_dirty_block_groups()
3413 * other task (struct btrfs_trans_handle->new_bgs). This in btrfs_start_dirty_block_groups()
3414 * means its block group item isn't yet in the extent in btrfs_start_dirty_block_groups()
3419 if (ret == -ENOENT) { in btrfs_start_dirty_block_groups()
3421 spin_lock(&cur_trans->dirty_bgs_lock); in btrfs_start_dirty_block_groups()
3422 if (list_empty(&cache->dirty_list)) { in btrfs_start_dirty_block_groups()
3423 list_add_tail(&cache->dirty_list, in btrfs_start_dirty_block_groups()
3424 &cur_trans->dirty_bgs); in btrfs_start_dirty_block_groups()
3425 btrfs_get_block_group(cache); in btrfs_start_dirty_block_groups()
3428 spin_unlock(&cur_trans->dirty_bgs_lock); in btrfs_start_dirty_block_groups()
3434 /* If it's not on the io list, we need to put the block group */ in btrfs_start_dirty_block_groups()
3436 btrfs_put_block_group(cache); in btrfs_start_dirty_block_groups()
3441 * us from writing caches for block groups that are going to be in btrfs_start_dirty_block_groups()
3444 mutex_unlock(&trans->transaction->cache_write_mutex); in btrfs_start_dirty_block_groups()
3447 mutex_lock(&trans->transaction->cache_write_mutex); in btrfs_start_dirty_block_groups()
3449 mutex_unlock(&trans->transaction->cache_write_mutex); in btrfs_start_dirty_block_groups()
3459 spin_lock(&cur_trans->dirty_bgs_lock); in btrfs_start_dirty_block_groups()
3460 list_splice_init(&cur_trans->dirty_bgs, &dirty); in btrfs_start_dirty_block_groups()
3462 * dirty_bgs_lock protects us from concurrent block group in btrfs_start_dirty_block_groups()
3466 spin_unlock(&cur_trans->dirty_bgs_lock); in btrfs_start_dirty_block_groups()
3469 spin_unlock(&cur_trans->dirty_bgs_lock); in btrfs_start_dirty_block_groups()
3473 spin_lock(&cur_trans->dirty_bgs_lock); in btrfs_start_dirty_block_groups()
3474 list_splice_init(&dirty, &cur_trans->dirty_bgs); in btrfs_start_dirty_block_groups()
3475 spin_unlock(&cur_trans->dirty_bgs_lock); in btrfs_start_dirty_block_groups()
3485 struct btrfs_fs_info *fs_info = trans->fs_info; in btrfs_write_dirty_block_groups()
3486 struct btrfs_block_group *cache; in btrfs_write_dirty_block_groups() local
3487 struct btrfs_transaction *cur_trans = trans->transaction; in btrfs_write_dirty_block_groups()
3491 struct list_head *io = &cur_trans->io_bgs; in btrfs_write_dirty_block_groups()
3495 return -ENOMEM; in btrfs_write_dirty_block_groups()
3500 * transaction's list of dirty block groups. These tasks correspond to in btrfs_write_dirty_block_groups()
3502 * space cache, which run inode.c:btrfs_finish_ordered_io(), and can in btrfs_write_dirty_block_groups()
3503 * allocate new block groups as a result of COWing nodes of the root in btrfs_write_dirty_block_groups()
3512 spin_lock(&cur_trans->dirty_bgs_lock); in btrfs_write_dirty_block_groups()
3513 while (!list_empty(&cur_trans->dirty_bgs)) { in btrfs_write_dirty_block_groups()
3514 cache = list_first_entry(&cur_trans->dirty_bgs, in btrfs_write_dirty_block_groups()
3519 * This can happen if cache_save_setup re-dirties a block group in btrfs_write_dirty_block_groups()
3523 if (!list_empty(&cache->io_list)) { in btrfs_write_dirty_block_groups()
3524 spin_unlock(&cur_trans->dirty_bgs_lock); in btrfs_write_dirty_block_groups()
3525 list_del_init(&cache->io_list); in btrfs_write_dirty_block_groups()
3526 btrfs_wait_cache_io(trans, cache, path); in btrfs_write_dirty_block_groups()
3527 btrfs_put_block_group(cache); in btrfs_write_dirty_block_groups()
3528 spin_lock(&cur_trans->dirty_bgs_lock); in btrfs_write_dirty_block_groups()
3535 list_del_init(&cache->dirty_list); in btrfs_write_dirty_block_groups()
3536 spin_unlock(&cur_trans->dirty_bgs_lock); in btrfs_write_dirty_block_groups()
3539 cache_save_setup(cache, trans, path); in btrfs_write_dirty_block_groups()
3543 (unsigned long) -1); in btrfs_write_dirty_block_groups()
3545 if (!ret && cache->disk_cache_state == BTRFS_DC_SETUP) { in btrfs_write_dirty_block_groups()
3546 cache->io_ctl.inode = NULL; in btrfs_write_dirty_block_groups()
3547 ret = btrfs_write_out_cache(trans, cache, path); in btrfs_write_dirty_block_groups()
3548 if (ret == 0 && cache->io_ctl.inode) { in btrfs_write_dirty_block_groups()
3550 list_add_tail(&cache->io_list, io); in btrfs_write_dirty_block_groups()
3553 * If we failed to write the cache, the in btrfs_write_dirty_block_groups()
3560 ret = update_block_group_item(trans, path, cache); in btrfs_write_dirty_block_groups()
3563 * created a new block group while updating a free space in btrfs_write_dirty_block_groups()
3564 * cache's inode (at inode.c:btrfs_finish_ordered_io()) in btrfs_write_dirty_block_groups()
3566 * which case the new block group is still attached to in btrfs_write_dirty_block_groups()
3568 * finished yet (no block group item in the extent tree in btrfs_write_dirty_block_groups()
3574 if (ret == -ENOENT) { in btrfs_write_dirty_block_groups()
3575 wait_event(cur_trans->writer_wait, in btrfs_write_dirty_block_groups()
3576 atomic_read(&cur_trans->num_writers) == 1); in btrfs_write_dirty_block_groups()
3577 ret = update_block_group_item(trans, path, cache); in btrfs_write_dirty_block_groups()
3583 /* If its not on the io list, we need to put the block group */ in btrfs_write_dirty_block_groups()
3585 btrfs_put_block_group(cache); in btrfs_write_dirty_block_groups()
3587 spin_lock(&cur_trans->dirty_bgs_lock); in btrfs_write_dirty_block_groups()
3589 spin_unlock(&cur_trans->dirty_bgs_lock); in btrfs_write_dirty_block_groups()
3596 cache = list_first_entry(io, struct btrfs_block_group, in btrfs_write_dirty_block_groups()
3598 list_del_init(&cache->io_list); in btrfs_write_dirty_block_groups()
3599 btrfs_wait_cache_io(trans, cache, path); in btrfs_write_dirty_block_groups()
3600 btrfs_put_block_group(cache); in btrfs_write_dirty_block_groups()
3610 struct btrfs_fs_info *info = trans->fs_info; in btrfs_update_block_group()
3611 struct btrfs_block_group *cache = NULL; in btrfs_update_block_group() local
3618 /* Block accounting for super block */ in btrfs_update_block_group()
3619 spin_lock(&info->delalloc_root_lock); in btrfs_update_block_group()
3620 old_val = btrfs_super_bytes_used(info->super_copy); in btrfs_update_block_group()
3624 old_val -= num_bytes; in btrfs_update_block_group()
3625 btrfs_set_super_bytes_used(info->super_copy, old_val); in btrfs_update_block_group()
3626 spin_unlock(&info->delalloc_root_lock); in btrfs_update_block_group()
3632 cache = btrfs_lookup_block_group(info, bytenr); in btrfs_update_block_group()
3633 if (!cache) { in btrfs_update_block_group()
3634 ret = -ENOENT; in btrfs_update_block_group()
3637 space_info = cache->space_info; in btrfs_update_block_group()
3638 factor = btrfs_bg_type_to_factor(cache->flags); in btrfs_update_block_group()
3641 * If this block group has free space cache written out, we in btrfs_update_block_group()
3644 * space back to the block group, otherwise we will leak space. in btrfs_update_block_group()
3646 if (!alloc && !btrfs_block_group_done(cache)) in btrfs_update_block_group()
3647 btrfs_cache_block_group(cache, true); in btrfs_update_block_group()
3649 byte_in_group = bytenr - cache->start; in btrfs_update_block_group()
3650 WARN_ON(byte_in_group > cache->length); in btrfs_update_block_group()
3652 spin_lock(&space_info->lock); in btrfs_update_block_group()
3653 spin_lock(&cache->lock); in btrfs_update_block_group()
3656 cache->disk_cache_state < BTRFS_DC_CLEAR) in btrfs_update_block_group()
3657 cache->disk_cache_state = BTRFS_DC_CLEAR; in btrfs_update_block_group()
3659 old_val = cache->used; in btrfs_update_block_group()
3660 num_bytes = min(total, cache->length - byte_in_group); in btrfs_update_block_group()
3663 cache->used = old_val; in btrfs_update_block_group()
3664 cache->reserved -= num_bytes; in btrfs_update_block_group()
3665 space_info->bytes_reserved -= num_bytes; in btrfs_update_block_group()
3666 space_info->bytes_used += num_bytes; in btrfs_update_block_group()
3667 space_info->disk_used += num_bytes * factor; in btrfs_update_block_group()
3668 spin_unlock(&cache->lock); in btrfs_update_block_group()
3669 spin_unlock(&space_info->lock); in btrfs_update_block_group()
3671 old_val -= num_bytes; in btrfs_update_block_group()
3672 cache->used = old_val; in btrfs_update_block_group()
3673 cache->pinned += num_bytes; in btrfs_update_block_group()
3676 space_info->bytes_used -= num_bytes; in btrfs_update_block_group()
3677 space_info->disk_used -= num_bytes * factor; in btrfs_update_block_group()
3679 reclaim = should_reclaim_block_group(cache, num_bytes); in btrfs_update_block_group()
3681 spin_unlock(&cache->lock); in btrfs_update_block_group()
3682 spin_unlock(&space_info->lock); in btrfs_update_block_group()
3684 set_extent_bit(&trans->transaction->pinned_extents, in btrfs_update_block_group()
3685 bytenr, bytenr + num_bytes - 1, in btrfs_update_block_group()
3689 spin_lock(&trans->transaction->dirty_bgs_lock); in btrfs_update_block_group()
3690 if (list_empty(&cache->dirty_list)) { in btrfs_update_block_group()
3691 list_add_tail(&cache->dirty_list, in btrfs_update_block_group()
3692 &trans->transaction->dirty_bgs); in btrfs_update_block_group()
3693 trans->delayed_ref_updates++; in btrfs_update_block_group()
3694 btrfs_get_block_group(cache); in btrfs_update_block_group()
3696 spin_unlock(&trans->transaction->dirty_bgs_lock); in btrfs_update_block_group()
3699 * No longer have used bytes in this block group, queue it for in btrfs_update_block_group()
3700 * deletion. We do this after adding the block group to the in btrfs_update_block_group()
3702 * cache writeout. in btrfs_update_block_group()
3706 btrfs_mark_bg_unused(cache); in btrfs_update_block_group()
3708 btrfs_mark_bg_to_reclaim(cache); in btrfs_update_block_group()
3711 btrfs_put_block_group(cache); in btrfs_update_block_group()
3712 total -= num_bytes; in btrfs_update_block_group()
3716 /* Modified block groups are accounted for in the delayed_refs_rsv. */ in btrfs_update_block_group()
3724 * @cache: The cache we are manipulating
3731 * reservation and the block group has become read only we cannot make the
3732 * reservation and return -EAGAIN, otherwise this function always succeeds.
3734 int btrfs_add_reserved_bytes(struct btrfs_block_group *cache, in btrfs_add_reserved_bytes() argument
3738 struct btrfs_space_info *space_info = cache->space_info; in btrfs_add_reserved_bytes()
3742 spin_lock(&space_info->lock); in btrfs_add_reserved_bytes()
3743 spin_lock(&cache->lock); in btrfs_add_reserved_bytes()
3744 if (cache->ro) { in btrfs_add_reserved_bytes()
3745 ret = -EAGAIN; in btrfs_add_reserved_bytes()
3749 if (btrfs_block_group_should_use_size_class(cache)) { in btrfs_add_reserved_bytes()
3751 ret = btrfs_use_block_group_size_class(cache, size_class, force_wrong_size_class); in btrfs_add_reserved_bytes()
3755 cache->reserved += num_bytes; in btrfs_add_reserved_bytes()
3756 space_info->bytes_reserved += num_bytes; in btrfs_add_reserved_bytes()
3757 trace_btrfs_space_reservation(cache->fs_info, "space_info", in btrfs_add_reserved_bytes()
3758 space_info->flags, num_bytes, 1); in btrfs_add_reserved_bytes()
3759 btrfs_space_info_update_bytes_may_use(cache->fs_info, in btrfs_add_reserved_bytes()
3760 space_info, -ram_bytes); in btrfs_add_reserved_bytes()
3762 cache->delalloc_bytes += num_bytes; in btrfs_add_reserved_bytes()
3769 btrfs_try_granting_tickets(cache->fs_info, space_info); in btrfs_add_reserved_bytes()
3771 spin_unlock(&cache->lock); in btrfs_add_reserved_bytes()
3772 spin_unlock(&space_info->lock); in btrfs_add_reserved_bytes()
3779 * @cache: The cache we are manipulating
3788 void btrfs_free_reserved_bytes(struct btrfs_block_group *cache, in btrfs_free_reserved_bytes() argument
3791 struct btrfs_space_info *space_info = cache->space_info; in btrfs_free_reserved_bytes()
3793 spin_lock(&space_info->lock); in btrfs_free_reserved_bytes()
3794 spin_lock(&cache->lock); in btrfs_free_reserved_bytes()
3795 if (cache->ro) in btrfs_free_reserved_bytes()
3796 space_info->bytes_readonly += num_bytes; in btrfs_free_reserved_bytes()
3797 else if (btrfs_is_zoned(cache->fs_info)) in btrfs_free_reserved_bytes()
3798 space_info->bytes_zone_unusable += num_bytes; in btrfs_free_reserved_bytes()
3799 cache->reserved -= num_bytes; in btrfs_free_reserved_bytes()
3800 space_info->bytes_reserved -= num_bytes; in btrfs_free_reserved_bytes()
3801 space_info->max_extent_size = 0; in btrfs_free_reserved_bytes()
3804 cache->delalloc_bytes -= num_bytes; in btrfs_free_reserved_bytes()
3805 spin_unlock(&cache->lock); in btrfs_free_reserved_bytes()
3807 btrfs_try_granting_tickets(cache->fs_info, space_info); in btrfs_free_reserved_bytes()
3808 spin_unlock(&space_info->lock); in btrfs_free_reserved_bytes()
3813 struct list_head *head = &info->space_info; in force_metadata_allocation()
3817 if (found->flags & BTRFS_BLOCK_GROUP_METADATA) in force_metadata_allocation()
3818 found->force_alloc = CHUNK_ALLOC_FORCE; in force_metadata_allocation()
3833 * about 1% of the FS size. in should_alloc_chunk()
3836 thresh = btrfs_super_total_bytes(fs_info->super_copy); in should_alloc_chunk()
3839 if (sinfo->total_bytes - bytes_used < thresh) in should_alloc_chunk()
3843 if (bytes_used + SZ_2M < mult_perc(sinfo->total_bytes, 80)) in should_alloc_chunk()
3850 u64 alloc_flags = btrfs_get_alloc_profile(trans->fs_info, type); in btrfs_force_chunk_alloc()
3864 * system block group if needed. in do_chunk_alloc()
3876 * Normally we are not expected to fail with -ENOSPC here, since we have in do_chunk_alloc()
3881 * existing system block groups have a profile which can not be used in do_chunk_alloc()
3888 * none of the block groups can be used for extent allocation since they in do_chunk_alloc()
3892 * block groups and check if they have a usable profile and enough space in do_chunk_alloc()
3893 * can be slow on very large filesystems, so we tolerate the -ENOSPC and in do_chunk_alloc()
3899 * block group to allocate from when we called check_system_chunk() in do_chunk_alloc()
3900 * above. However right after we called it, the only system block group in do_chunk_alloc()
3904 * handle and scrub uses the commit root to search for block groups; in do_chunk_alloc()
3906 * 3) We had one system block group with enough free space when we called in do_chunk_alloc()
3910 * block group (discard removes a free space entry, discards it, and in do_chunk_alloc()
3911 * then adds back the entry to the block group cache). in do_chunk_alloc()
3913 if (ret == -ENOSPC) { in do_chunk_alloc()
3914 const u64 sys_flags = btrfs_system_alloc_profile(trans->fs_info); in do_chunk_alloc()
3952 * 1) Phase 1 - through btrfs_chunk_alloc() we allocate device extents for
3953 * the chunk, the chunk mapping, create its block group and add the items
3954 * that belong in the chunk btree to it - more specifically, we need to
3957 * 2) Phase 2 - through btrfs_create_pending_block_groups(), we add the block
3963 * trigger chunk allocation and attempted to insert the new block group item
3974 * allocate a new block group (chunk) because the only one that had enough
3976 * device replace, block group reclaim thread, etc), so we can not use it
3981 * the filesystem was mounted in degraded mode, none of the existing block
3983 * profile (for e.g. mounting a 2 devices filesystem, where all block groups
3986 * example, it will trigger allocation of a new metadata block group with a
3992 * example, it does not find any free extent in any metadata block group,
3993 * therefore forced to try to allocate a new metadata block group.
3995 * meanwhile - this typically happens with tasks that don't reserve space
4004 * the only metadata block group that had free space (discard starts by
4005 * removing a free space entry from a block group, then does the discard
4007 * block group).
4010 * a seed device - we must create new metadata and system chunks without adding
4011 * any of the block group items to the chunk, extent and device btrees. If we
4013 * btrees, since all the chunks from the seed device are read-only.
4020 * superblock. This is easier to trigger if using a btree node/leaf size of 64K
4028 * btrfs_reserve_chunk_metadata() - the former is used when allocating a data or
4030 * a modification to the chunk btree - use cases for the later are adding,
4036 * holding fs_info->chunk_mutex. This is important to guarantee that while COWing
4042 * that mutex. The same logic applies to removing chunks - we must reserve system
4044 * while holding fs_info->chunk_mutex.
4049 * - return 1 if it successfully allocates a chunk,
4050 * - return errors including -ENOSPC otherwise.
4052 * - return 0 if it doesn't need to allocate a new chunk,
4053 * - return 1 if it successfully allocates a chunk,
4054 * - return errors including -ENOSPC otherwise.
4059 struct btrfs_fs_info *fs_info = trans->fs_info; in btrfs_chunk_alloc()
4072 /* Don't re-enter if we're already allocating a chunk */ in btrfs_chunk_alloc()
4073 if (trans->allocating_chunk) in btrfs_chunk_alloc()
4074 return -ENOSPC; in btrfs_chunk_alloc()
4084 * lock on it and on its parent - if the COW operation triggers a system in btrfs_chunk_alloc()
4093 * here - this happens in the cases described above at do_chunk_alloc(). in btrfs_chunk_alloc()
4097 return -ENOSPC; in btrfs_chunk_alloc()
4103 spin_lock(&space_info->lock); in btrfs_chunk_alloc()
4104 if (force < space_info->force_alloc) in btrfs_chunk_alloc()
4105 force = space_info->force_alloc; in btrfs_chunk_alloc()
4107 if (space_info->full) { in btrfs_chunk_alloc()
4110 ret = -ENOSPC; in btrfs_chunk_alloc()
4113 spin_unlock(&space_info->lock); in btrfs_chunk_alloc()
4116 spin_unlock(&space_info->lock); in btrfs_chunk_alloc()
4118 } else if (space_info->chunk_alloc) { in btrfs_chunk_alloc()
4120 * Someone is already allocating, so we need to block in btrfs_chunk_alloc()
4127 spin_unlock(&space_info->lock); in btrfs_chunk_alloc()
4128 mutex_lock(&fs_info->chunk_mutex); in btrfs_chunk_alloc()
4129 mutex_unlock(&fs_info->chunk_mutex); in btrfs_chunk_alloc()
4132 space_info->chunk_alloc = 1; in btrfs_chunk_alloc()
4134 spin_unlock(&space_info->lock); in btrfs_chunk_alloc()
4140 mutex_lock(&fs_info->chunk_mutex); in btrfs_chunk_alloc()
4141 trans->allocating_chunk = true; in btrfs_chunk_alloc()
4155 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) { in btrfs_chunk_alloc()
4156 fs_info->data_chunk_allocations++; in btrfs_chunk_alloc()
4157 if (!(fs_info->data_chunk_allocations % in btrfs_chunk_alloc()
4158 fs_info->metadata_ratio)) in btrfs_chunk_alloc()
4163 trans->allocating_chunk = false; in btrfs_chunk_alloc()
4169 * New block group is likely to be used soon. Try to activate in btrfs_chunk_alloc()
4178 spin_lock(&space_info->lock); in btrfs_chunk_alloc()
4180 if (ret == -ENOSPC) in btrfs_chunk_alloc()
4181 space_info->full = 1; in btrfs_chunk_alloc()
4186 space_info->max_extent_size = 0; in btrfs_chunk_alloc()
4189 space_info->force_alloc = CHUNK_ALLOC_NO_FORCE; in btrfs_chunk_alloc()
4191 space_info->chunk_alloc = 0; in btrfs_chunk_alloc()
4192 spin_unlock(&space_info->lock); in btrfs_chunk_alloc()
4193 mutex_unlock(&fs_info->chunk_mutex); in btrfs_chunk_alloc()
4204 num_dev = fs_info->fs_devices->rw_devices; in get_profile_num_devs()
4213 struct btrfs_fs_info *fs_info = trans->fs_info; in reserve_chunk_space()
4220 * atomic and race free space reservation in the chunk block reserve. in reserve_chunk_space()
4222 lockdep_assert_held(&fs_info->chunk_mutex); in reserve_chunk_space()
4225 spin_lock(&info->lock); in reserve_chunk_space()
4226 left = info->total_bytes - btrfs_space_info_used(info, true); in reserve_chunk_space()
4227 spin_unlock(&info->lock); in reserve_chunk_space()
4262 * the cases described at do_chunk_alloc() - the system in reserve_chunk_space()
4263 * block group we just created was just turned into RO in reserve_chunk_space()
4273 &fs_info->chunk_block_rsv, in reserve_chunk_space()
4276 trans->chunk_bytes_reserved += bytes; in reserve_chunk_space()
4282 * The caller must be holding fs_info->chunk_mutex.
4286 struct btrfs_fs_info *fs_info = trans->fs_info; in check_system_chunk()
4307 * block group allocation and removal, to avoid a deadlock with a concurrent
4308 * task that is allocating a metadata or data block group and therefore needs to
4316 struct btrfs_fs_info *fs_info = trans->fs_info; in btrfs_reserve_chunk_metadata()
4324 mutex_lock(&fs_info->chunk_mutex); in btrfs_reserve_chunk_metadata()
4326 mutex_unlock(&fs_info->chunk_mutex); in btrfs_reserve_chunk_metadata()
4336 spin_lock(&block_group->lock); in btrfs_put_block_group_cache()
4338 &block_group->runtime_flags)) { in btrfs_put_block_group_cache()
4339 struct inode *inode = block_group->inode; in btrfs_put_block_group_cache()
4341 block_group->inode = NULL; in btrfs_put_block_group_cache()
4342 spin_unlock(&block_group->lock); in btrfs_put_block_group_cache()
4344 ASSERT(block_group->io_ctl.inode == NULL); in btrfs_put_block_group_cache()
4347 spin_unlock(&block_group->lock); in btrfs_put_block_group_cache()
4354 * Must be called only after stopping all workers, since we could have block
4356 * freed the block groups before stopping them.
4366 if (info->active_meta_bg) { in btrfs_free_block_groups()
4367 btrfs_put_block_group(info->active_meta_bg); in btrfs_free_block_groups()
4368 info->active_meta_bg = NULL; in btrfs_free_block_groups()
4370 if (info->active_system_bg) { in btrfs_free_block_groups()
4371 btrfs_put_block_group(info->active_system_bg); in btrfs_free_block_groups()
4372 info->active_system_bg = NULL; in btrfs_free_block_groups()
4376 write_lock(&info->block_group_cache_lock); in btrfs_free_block_groups()
4377 while (!list_empty(&info->caching_block_groups)) { in btrfs_free_block_groups()
4378 caching_ctl = list_entry(info->caching_block_groups.next, in btrfs_free_block_groups()
4380 list_del(&caching_ctl->list); in btrfs_free_block_groups()
4383 write_unlock(&info->block_group_cache_lock); in btrfs_free_block_groups()
4385 spin_lock(&info->unused_bgs_lock); in btrfs_free_block_groups()
4386 while (!list_empty(&info->unused_bgs)) { in btrfs_free_block_groups()
4387 block_group = list_first_entry(&info->unused_bgs, in btrfs_free_block_groups()
4390 list_del_init(&block_group->bg_list); in btrfs_free_block_groups()
4394 while (!list_empty(&info->reclaim_bgs)) { in btrfs_free_block_groups()
4395 block_group = list_first_entry(&info->reclaim_bgs, in btrfs_free_block_groups()
4398 list_del_init(&block_group->bg_list); in btrfs_free_block_groups()
4401 spin_unlock(&info->unused_bgs_lock); in btrfs_free_block_groups()
4403 spin_lock(&info->zone_active_bgs_lock); in btrfs_free_block_groups()
4404 while (!list_empty(&info->zone_active_bgs)) { in btrfs_free_block_groups()
4405 block_group = list_first_entry(&info->zone_active_bgs, in btrfs_free_block_groups()
4408 list_del_init(&block_group->active_bg_list); in btrfs_free_block_groups()
4411 spin_unlock(&info->zone_active_bgs_lock); in btrfs_free_block_groups()
4413 write_lock(&info->block_group_cache_lock); in btrfs_free_block_groups()
4414 while ((n = rb_last(&info->block_group_cache_tree.rb_root)) != NULL) { in btrfs_free_block_groups()
4417 rb_erase_cached(&block_group->cache_node, in btrfs_free_block_groups()
4418 &info->block_group_cache_tree); in btrfs_free_block_groups()
4419 RB_CLEAR_NODE(&block_group->cache_node); in btrfs_free_block_groups()
4420 write_unlock(&info->block_group_cache_lock); in btrfs_free_block_groups()
4422 down_write(&block_group->space_info->groups_sem); in btrfs_free_block_groups()
4423 list_del(&block_group->list); in btrfs_free_block_groups()
4424 up_write(&block_group->space_info->groups_sem); in btrfs_free_block_groups()
4427 * We haven't cached this block group, which means we could in btrfs_free_block_groups()
4428 * possibly have excluded extents on this block group. in btrfs_free_block_groups()
4430 if (block_group->cached == BTRFS_CACHE_NO || in btrfs_free_block_groups()
4431 block_group->cached == BTRFS_CACHE_ERROR) in btrfs_free_block_groups()
4435 ASSERT(block_group->cached != BTRFS_CACHE_STARTED); in btrfs_free_block_groups()
4436 ASSERT(list_empty(&block_group->dirty_list)); in btrfs_free_block_groups()
4437 ASSERT(list_empty(&block_group->io_list)); in btrfs_free_block_groups()
4438 ASSERT(list_empty(&block_group->bg_list)); in btrfs_free_block_groups()
4439 ASSERT(refcount_read(&block_group->refs) == 1); in btrfs_free_block_groups()
4440 ASSERT(block_group->swap_extents == 0); in btrfs_free_block_groups()
4443 write_lock(&info->block_group_cache_lock); in btrfs_free_block_groups()
4445 write_unlock(&info->block_group_cache_lock); in btrfs_free_block_groups()
4449 while (!list_empty(&info->space_info)) { in btrfs_free_block_groups()
4450 space_info = list_entry(info->space_info.next, in btrfs_free_block_groups()
4458 if (WARN_ON(space_info->bytes_pinned > 0 || in btrfs_free_block_groups()
4459 space_info->bytes_may_use > 0)) in btrfs_free_block_groups()
4469 if (!(space_info->flags & BTRFS_BLOCK_GROUP_METADATA) || in btrfs_free_block_groups()
4471 if (WARN_ON(space_info->bytes_reserved > 0)) in btrfs_free_block_groups()
4475 WARN_ON(space_info->reclaim_size > 0); in btrfs_free_block_groups()
4476 list_del(&space_info->list); in btrfs_free_block_groups()
4482 void btrfs_freeze_block_group(struct btrfs_block_group *cache) in btrfs_freeze_block_group() argument
4484 atomic_inc(&cache->frozen); in btrfs_freeze_block_group()
4489 struct btrfs_fs_info *fs_info = block_group->fs_info; in btrfs_unfreeze_block_group()
4494 spin_lock(&block_group->lock); in btrfs_unfreeze_block_group()
4495 cleanup = (atomic_dec_and_test(&block_group->frozen) && in btrfs_unfreeze_block_group()
4496 test_bit(BLOCK_GROUP_FLAG_REMOVED, &block_group->runtime_flags)); in btrfs_unfreeze_block_group()
4497 spin_unlock(&block_group->lock); in btrfs_unfreeze_block_group()
4500 em_tree = &fs_info->mapping_tree; in btrfs_unfreeze_block_group()
4501 write_lock(&em_tree->lock); in btrfs_unfreeze_block_group()
4502 em = lookup_extent_mapping(em_tree, block_group->start, in btrfs_unfreeze_block_group()
4506 write_unlock(&em_tree->lock); in btrfs_unfreeze_block_group()
4514 * tasks trimming this block group have left 1 entry each one. in btrfs_unfreeze_block_group()
4525 spin_lock(&bg->lock); in btrfs_inc_block_group_swap_extents()
4526 if (bg->ro) in btrfs_inc_block_group_swap_extents()
4529 bg->swap_extents++; in btrfs_inc_block_group_swap_extents()
4530 spin_unlock(&bg->lock); in btrfs_inc_block_group_swap_extents()
4537 spin_lock(&bg->lock); in btrfs_dec_block_group_swap_extents()
4538 ASSERT(!bg->ro); in btrfs_dec_block_group_swap_extents()
4539 ASSERT(bg->swap_extents >= amount); in btrfs_dec_block_group_swap_extents()
4540 bg->swap_extents -= amount; in btrfs_dec_block_group_swap_extents()
4541 spin_unlock(&bg->lock); in btrfs_dec_block_group_swap_extents()
4544 enum btrfs_block_group_size_class btrfs_calc_block_group_size_class(u64 size) in btrfs_calc_block_group_size_class() argument
4546 if (size <= SZ_128K) in btrfs_calc_block_group_size_class()
4548 if (size <= SZ_8M) in btrfs_calc_block_group_size_class()
4554 * Handle a block group allocating an extent in a size class
4556 * @bg: The block group we allocated in.
4557 * @size_class: The size class of the allocation.
4559 * mismatched size classes.
4561 * Returns: 0 if the size class was valid for this block_group, -EAGAIN in the
4562 * case of a race that leads to the wrong size class without
4565 * find_free_extent will skip block groups with a mismatched size class until
4567 * force_wrong_size_class. However, if a block group is newly allocated and
4568 * doesn't yet have a size class, then it is possible for two allocations of
4578 /* The new allocation is in the right size class, do nothing */ in btrfs_use_block_group_size_class()
4579 if (bg->size_class == size_class) in btrfs_use_block_group_size_class()
4582 * The new allocation is in a mismatched size class. in btrfs_use_block_group_size_class()
4591 if (bg->size_class != BTRFS_BG_SZ_NONE) { in btrfs_use_block_group_size_class()
4594 return -EAGAIN; in btrfs_use_block_group_size_class()
4597 * The happy new block group case: the new allocation is the first in btrfs_use_block_group_size_class()
4600 bg->size_class = size_class; in btrfs_use_block_group_size_class()
4607 if (btrfs_is_zoned(bg->fs_info)) in btrfs_block_group_should_use_size_class()