Lines Matching +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 const 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 btrfs_free_chunk_map(cache->physical_map); in btrfs_put_block_group()
172 kfree(cache); in btrfs_put_block_group()
184 if (new_bg->start < exist_bg->start) in btrfs_bg_start_cmp()
185 return -1; in btrfs_bg_start_cmp()
186 if (new_bg->start > exist_bg->start) in btrfs_bg_start_cmp()
192 * This adds the block group to the fs_info rb tree for the block group cache
200 ASSERT(block_group->length != 0); in btrfs_add_block_group_cache()
202 write_lock(&info->block_group_cache_lock); in btrfs_add_block_group_cache()
204 exist = rb_find_add_cached(&block_group->cache_node, in btrfs_add_block_group_cache()
205 &info->block_group_cache_tree, btrfs_bg_start_cmp); in btrfs_add_block_group_cache()
207 ret = -EEXIST; in btrfs_add_block_group_cache()
208 write_unlock(&info->block_group_cache_lock); in btrfs_add_block_group_cache()
214 * This will return the block group at or after bytenr if contains is 0, else
215 * it will return the block group that contains the bytenr
220 struct btrfs_block_group *cache, *ret = NULL; in block_group_cache_tree_search() local
224 read_lock(&info->block_group_cache_lock); in block_group_cache_tree_search()
225 n = info->block_group_cache_tree.rb_root.rb_node; in block_group_cache_tree_search()
228 cache = rb_entry(n, struct btrfs_block_group, cache_node); in block_group_cache_tree_search()
229 end = cache->start + cache->length - 1; in block_group_cache_tree_search()
230 start = cache->start; in block_group_cache_tree_search()
233 if (!contains && (!ret || start < ret->start)) in block_group_cache_tree_search()
234 ret = cache; in block_group_cache_tree_search()
235 n = n->rb_left; in block_group_cache_tree_search()
238 ret = cache; in block_group_cache_tree_search()
241 n = n->rb_right; in block_group_cache_tree_search()
243 ret = cache; in block_group_cache_tree_search()
249 read_unlock(&info->block_group_cache_lock); in block_group_cache_tree_search()
255 * Return the block group that starts at or after bytenr
264 * Return the block group that contains the given bytenr
273 struct btrfs_block_group *cache) in btrfs_next_block_group() argument
275 struct btrfs_fs_info *fs_info = cache->fs_info; in btrfs_next_block_group()
278 read_lock(&fs_info->block_group_cache_lock); in btrfs_next_block_group()
280 /* If our block group was removed, we need a full search. */ in btrfs_next_block_group()
281 if (RB_EMPTY_NODE(&cache->cache_node)) { in btrfs_next_block_group()
282 const u64 next_bytenr = cache->start + cache->length; in btrfs_next_block_group()
284 read_unlock(&fs_info->block_group_cache_lock); in btrfs_next_block_group()
285 btrfs_put_block_group(cache); in btrfs_next_block_group()
288 node = rb_next(&cache->cache_node); in btrfs_next_block_group()
289 btrfs_put_block_group(cache); in btrfs_next_block_group()
291 cache = rb_entry(node, struct btrfs_block_group, cache_node); in btrfs_next_block_group()
292 btrfs_get_block_group(cache); in btrfs_next_block_group()
294 cache = NULL; in btrfs_next_block_group()
295 read_unlock(&fs_info->block_group_cache_lock); in btrfs_next_block_group()
296 return cache; in btrfs_next_block_group()
306 * number of NOCOW writers in the block group that contains the extent, as long
307 * as the block group exists and it's currently not in read-only mode.
309 * Returns: A non-NULL block group pointer if we can do a NOCOW write, the caller
324 spin_lock(&bg->lock); in btrfs_inc_nocow_writers()
325 if (bg->ro) in btrfs_inc_nocow_writers()
328 atomic_inc(&bg->nocow_writers); in btrfs_inc_nocow_writers()
329 spin_unlock(&bg->lock); in btrfs_inc_nocow_writers()
336 /* No put on block group, done by btrfs_dec_nocow_writers(). */ in btrfs_inc_nocow_writers()
341 * Decrement the number of NOCOW writers in a block group.
344 * and on the block group returned by that call. Typically this is called after
348 * After this call, the caller should not use the block group anymore. It it wants
353 if (atomic_dec_and_test(&bg->nocow_writers)) in btrfs_dec_nocow_writers()
354 wake_up_var(&bg->nocow_writers); in btrfs_dec_nocow_writers()
362 wait_var_event(&bg->nocow_writers, !atomic_read(&bg->nocow_writers)); in btrfs_wait_nocow_writers()
372 if (atomic_dec_and_test(&bg->reservations)) in btrfs_dec_block_group_reservations()
373 wake_up_var(&bg->reservations); in btrfs_dec_block_group_reservations()
379 struct btrfs_space_info *space_info = bg->space_info; in btrfs_wait_block_group_reservations()
381 ASSERT(bg->ro); in btrfs_wait_block_group_reservations()
383 if (!(bg->flags & BTRFS_BLOCK_GROUP_DATA)) in btrfs_wait_block_group_reservations()
387 * Our block group is read only but before we set it to read only, in btrfs_wait_block_group_reservations()
392 * block group's reservations counter is incremented while a read lock in btrfs_wait_block_group_reservations()
396 down_write(&space_info->groups_sem); in btrfs_wait_block_group_reservations()
397 up_write(&space_info->groups_sem); in btrfs_wait_block_group_reservations()
399 wait_var_event(&bg->reservations, !atomic_read(&bg->reservations)); in btrfs_wait_block_group_reservations()
403 struct btrfs_block_group *cache) in btrfs_get_caching_control() argument
407 spin_lock(&cache->lock); in btrfs_get_caching_control()
408 if (!cache->caching_ctl) { in btrfs_get_caching_control()
409 spin_unlock(&cache->lock); in btrfs_get_caching_control()
413 ctl = cache->caching_ctl; in btrfs_get_caching_control()
414 refcount_inc(&ctl->count); in btrfs_get_caching_control()
415 spin_unlock(&cache->lock); in btrfs_get_caching_control()
421 if (refcount_dec_and_test(&ctl->count)) in btrfs_put_caching_control()
426 * When we wait for progress in the block group caching, its because our
431 * up, and then it will check the block group free space numbers for our min
433 * a free extent of a given size, but this is a good start.
435 * Callers of this must check if cache->cached == BTRFS_CACHE_ERROR before using
436 * any of the information in this block group.
438 void btrfs_wait_block_group_cache_progress(struct btrfs_block_group *cache, in btrfs_wait_block_group_cache_progress() argument
444 caching_ctl = btrfs_get_caching_control(cache); in btrfs_wait_block_group_cache_progress()
449 * We've already failed to allocate from this block group, so even if in btrfs_wait_block_group_cache_progress()
450 * there's enough space in the block group it isn't contiguous enough to in btrfs_wait_block_group_cache_progress()
454 progress = atomic_read(&caching_ctl->progress); in btrfs_wait_block_group_cache_progress()
456 wait_event(caching_ctl->wait, btrfs_block_group_done(cache) || in btrfs_wait_block_group_cache_progress()
457 (progress != atomic_read(&caching_ctl->progress) && in btrfs_wait_block_group_cache_progress()
458 (cache->free_space_ctl->free_space >= num_bytes))); in btrfs_wait_block_group_cache_progress()
463 static int btrfs_caching_ctl_wait_done(struct btrfs_block_group *cache, in btrfs_caching_ctl_wait_done() argument
466 wait_event(caching_ctl->wait, btrfs_block_group_done(cache)); in btrfs_caching_ctl_wait_done()
467 return cache->cached == BTRFS_CACHE_ERROR ? -EIO : 0; in btrfs_caching_ctl_wait_done()
470 static int btrfs_wait_block_group_cache_done(struct btrfs_block_group *cache) in btrfs_wait_block_group_cache_done() argument
475 caching_ctl = btrfs_get_caching_control(cache); in btrfs_wait_block_group_cache_done()
477 return (cache->cached == BTRFS_CACHE_ERROR) ? -EIO : 0; in btrfs_wait_block_group_cache_done()
478 ret = btrfs_caching_ctl_wait_done(cache, caching_ctl); in btrfs_wait_block_group_cache_done()
486 struct btrfs_fs_info *fs_info = block_group->fs_info; in fragment_free_space()
487 u64 start = block_group->start; in fragment_free_space()
488 u64 len = block_group->length; in fragment_free_space()
489 u64 chunk = block_group->flags & BTRFS_BLOCK_GROUP_METADATA ? in fragment_free_space()
490 fs_info->nodesize : fs_info->sectorsize; in fragment_free_space()
499 len -= step; in fragment_free_space()
505 * Add a free space range to the in memory free space cache of a block group.
506 * This checks if the range contains super block locations and any such
507 * locations are not added to the free space cache.
509 * @block_group: The target block group.
513 * added to the block group's free space cache.
520 struct btrfs_fs_info *info = block_group->fs_info; in btrfs_add_new_free_space()
521 u64 extent_start, extent_end, size; in btrfs_add_new_free_space() local
528 if (!find_first_extent_bit(&info->excluded_extents, start, in btrfs_add_new_free_space()
537 size = extent_start - start; in btrfs_add_new_free_space()
539 start, size); in btrfs_add_new_free_space()
543 *total_added_ret += size; in btrfs_add_new_free_space()
551 size = end - start; in btrfs_add_new_free_space()
553 size); in btrfs_add_new_free_space()
557 *total_added_ret += size; in btrfs_add_new_free_space()
564 * Get an arbitrary extent item index / max_index through the block group
566 * @block_group the block group to sample from
567 * @index: the integral step through the block group to grab from
571 * Pre-conditions on indices:
583 struct btrfs_fs_info *fs_info = block_group->fs_info; in sample_block_group_extent_item()
586 u64 search_end = block_group->start + block_group->length; in sample_block_group_extent_item()
594 lockdep_assert_held(&caching_ctl->mutex); in sample_block_group_extent_item()
595 lockdep_assert_held_read(&fs_info->commit_root_sem); in sample_block_group_extent_item()
599 return -ENOMEM; in sample_block_group_extent_item()
601 extent_root = btrfs_extent_root(fs_info, max_t(u64, block_group->start, in sample_block_group_extent_item()
604 path->skip_locking = 1; in sample_block_group_extent_item()
605 path->search_commit_root = 1; in sample_block_group_extent_item()
606 path->reada = READA_FORWARD; in sample_block_group_extent_item()
608 search_offset = index * div_u64(block_group->length, max_index); in sample_block_group_extent_item()
609 search_key.objectid = block_group->start + search_offset; in sample_block_group_extent_item()
614 /* Success; sampled an extent item in the block group */ in sample_block_group_extent_item()
615 if (found_key->type == BTRFS_EXTENT_ITEM_KEY && in sample_block_group_extent_item()
616 found_key->objectid >= block_group->start && in sample_block_group_extent_item()
617 found_key->objectid + found_key->offset <= search_end) in sample_block_group_extent_item()
621 if (found_key->objectid >= search_end) { in sample_block_group_extent_item()
627 lockdep_assert_held(&caching_ctl->mutex); in sample_block_group_extent_item()
628 lockdep_assert_held_read(&fs_info->commit_root_sem); in sample_block_group_extent_item()
634 * Best effort attempt to compute a block group's size class while caching it.
636 * @block_group: the block group we are caching
638 * We cannot infer the size class while adding free space extents, because that
643 * them at even steps through the block group and pick the smallest size class
644 * we see. Since size class is best effort, and not guaranteed in general,
649 * If we are caching in a block group from disk, then there are three major cases
651 * 1. the block group is well behaved and all extents in it are the same size
653 * 2. the block group is mostly one size class with rare exceptions for last
655 * 3. the block group was populated before size classes and can have a totally
656 * arbitrary mix of size classes.
658 * In case 1, looking at any extent in the block group will yield the correct
659 * result. For the mixed cases, taking the minimum size class seems like a good
660 * approximation, since gaps from frees will be usable to the size class. For
670 struct btrfs_fs_info *fs_info = block_group->fs_info; in load_block_group_size_class()
673 u64 min_size = block_group->length; in load_block_group_size_class()
680 lockdep_assert_held(&caching_ctl->mutex); in load_block_group_size_class()
681 lockdep_assert_held_read(&fs_info->commit_root_sem); in load_block_group_size_class()
692 spin_lock(&block_group->lock); in load_block_group_size_class()
693 block_group->size_class = size_class; in load_block_group_size_class()
694 spin_unlock(&block_group->lock); in load_block_group_size_class()
702 struct btrfs_block_group *block_group = caching_ctl->block_group; in load_extent_tree_free()
703 struct btrfs_fs_info *fs_info = block_group->fs_info; in load_extent_tree_free()
716 return -ENOMEM; in load_extent_tree_free()
718 last = max_t(u64, block_group->start, BTRFS_SUPER_INFO_OFFSET); in load_extent_tree_free()
724 * allocate from this block group until we've had a chance to fragment in load_extent_tree_free()
734 * root, since its read-only in load_extent_tree_free()
736 path->skip_locking = 1; in load_extent_tree_free()
737 path->search_commit_root = 1; in load_extent_tree_free()
738 path->reada = READA_FORWARD; in load_extent_tree_free()
749 leaf = path->nodes[0]; in load_extent_tree_free()
754 last = (u64)-1; in load_extent_tree_free()
758 if (path->slots[0] < nritems) { in load_extent_tree_free()
759 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); in load_extent_tree_free()
766 rwsem_is_contended(&fs_info->commit_root_sem)) { in load_extent_tree_free()
768 up_read(&fs_info->commit_root_sem); in load_extent_tree_free()
769 mutex_unlock(&caching_ctl->mutex); in load_extent_tree_free()
771 mutex_lock(&caching_ctl->mutex); in load_extent_tree_free()
772 down_read(&fs_info->commit_root_sem); in load_extent_tree_free()
781 leaf = path->nodes[0]; in load_extent_tree_free()
794 if (key.objectid < block_group->start) { in load_extent_tree_free()
795 path->slots[0]++; in load_extent_tree_free()
799 if (key.objectid >= block_group->start + block_group->length) in load_extent_tree_free()
813 fs_info->nodesize; in load_extent_tree_free()
820 atomic_inc(&caching_ctl->progress); in load_extent_tree_free()
821 wake_up(&caching_ctl->wait); in load_extent_tree_free()
825 path->slots[0]++; in load_extent_tree_free()
829 block_group->start + block_group->length, in load_extent_tree_free()
838 clear_extent_bits(&bg->fs_info->excluded_extents, bg->start, in btrfs_free_excluded_extents()
839 bg->start + bg->length - 1, EXTENT_UPTODATE); in btrfs_free_excluded_extents()
850 block_group = caching_ctl->block_group; in caching_thread()
851 fs_info = block_group->fs_info; in caching_thread()
853 mutex_lock(&caching_ctl->mutex); in caching_thread()
854 down_read(&fs_info->commit_root_sem); in caching_thread()
865 * We failed to load the space cache, set ourselves to in caching_thread()
868 spin_lock(&block_group->lock); in caching_thread()
869 block_group->cached = BTRFS_CACHE_STARTED; in caching_thread()
870 spin_unlock(&block_group->lock); in caching_thread()
871 wake_up(&caching_ctl->wait); in caching_thread()
876 * can't actually cache from the free space tree as our commit root and in caching_thread()
882 !(test_bit(BTRFS_FS_FREE_SPACE_TREE_UNTRUSTED, &fs_info->flags))) in caching_thread()
887 spin_lock(&block_group->lock); in caching_thread()
888 block_group->caching_ctl = NULL; in caching_thread()
889 block_group->cached = ret ? BTRFS_CACHE_ERROR : BTRFS_CACHE_FINISHED; in caching_thread()
890 spin_unlock(&block_group->lock); in caching_thread()
896 spin_lock(&block_group->space_info->lock); in caching_thread()
897 spin_lock(&block_group->lock); in caching_thread()
898 bytes_used = block_group->length - block_group->used; in caching_thread()
899 block_group->space_info->bytes_used += bytes_used >> 1; in caching_thread()
900 spin_unlock(&block_group->lock); in caching_thread()
901 spin_unlock(&block_group->space_info->lock); in caching_thread()
906 up_read(&fs_info->commit_root_sem); in caching_thread()
908 mutex_unlock(&caching_ctl->mutex); in caching_thread()
910 wake_up(&caching_ctl->wait); in caching_thread()
916 int btrfs_cache_block_group(struct btrfs_block_group *cache, bool wait) in btrfs_cache_block_group() argument
918 struct btrfs_fs_info *fs_info = cache->fs_info; in btrfs_cache_block_group()
922 /* Allocator for zoned filesystems does not use the cache at all */ in btrfs_cache_block_group()
928 return -ENOMEM; in btrfs_cache_block_group()
930 INIT_LIST_HEAD(&caching_ctl->list); in btrfs_cache_block_group()
931 mutex_init(&caching_ctl->mutex); in btrfs_cache_block_group()
932 init_waitqueue_head(&caching_ctl->wait); in btrfs_cache_block_group()
933 caching_ctl->block_group = cache; in btrfs_cache_block_group()
934 refcount_set(&caching_ctl->count, 2); in btrfs_cache_block_group()
935 atomic_set(&caching_ctl->progress, 0); in btrfs_cache_block_group()
936 btrfs_init_work(&caching_ctl->work, caching_thread, NULL); in btrfs_cache_block_group()
938 spin_lock(&cache->lock); in btrfs_cache_block_group()
939 if (cache->cached != BTRFS_CACHE_NO) { in btrfs_cache_block_group()
942 caching_ctl = cache->caching_ctl; in btrfs_cache_block_group()
944 refcount_inc(&caching_ctl->count); in btrfs_cache_block_group()
945 spin_unlock(&cache->lock); in btrfs_cache_block_group()
948 WARN_ON(cache->caching_ctl); in btrfs_cache_block_group()
949 cache->caching_ctl = caching_ctl; in btrfs_cache_block_group()
950 cache->cached = BTRFS_CACHE_STARTED; in btrfs_cache_block_group()
951 spin_unlock(&cache->lock); in btrfs_cache_block_group()
953 write_lock(&fs_info->block_group_cache_lock); in btrfs_cache_block_group()
954 refcount_inc(&caching_ctl->count); in btrfs_cache_block_group()
955 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups); in btrfs_cache_block_group()
956 write_unlock(&fs_info->block_group_cache_lock); in btrfs_cache_block_group()
958 btrfs_get_block_group(cache); in btrfs_cache_block_group()
960 btrfs_queue_work(fs_info->caching_workers, &caching_ctl->work); in btrfs_cache_block_group()
963 ret = btrfs_caching_ctl_wait_done(cache, caching_ctl); in btrfs_cache_block_group()
975 write_seqlock(&fs_info->profiles_lock); in clear_avail_alloc_bits()
977 fs_info->avail_data_alloc_bits &= ~extra_flags; in clear_avail_alloc_bits()
979 fs_info->avail_metadata_alloc_bits &= ~extra_flags; in clear_avail_alloc_bits()
981 fs_info->avail_system_alloc_bits &= ~extra_flags; in clear_avail_alloc_bits()
982 write_sequnlock(&fs_info->profiles_lock); in clear_avail_alloc_bits()
988 * - RAID56 - in case there's neither RAID5 nor RAID6 profile block group
991 * - RAID1C34 - same as above for RAID1C3 and RAID1C4 block groups
1001 struct list_head *head = &fs_info->space_info; in clear_incompat_bg_bits()
1005 down_read(&sinfo->groups_sem); in clear_incompat_bg_bits()
1006 if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID5])) in clear_incompat_bg_bits()
1008 if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID6])) in clear_incompat_bg_bits()
1010 if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID1C3])) in clear_incompat_bg_bits()
1012 if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID1C4])) in clear_incompat_bg_bits()
1014 up_read(&sinfo->groups_sem); in clear_incompat_bg_bits()
1026 return fs_info->block_group_root; in btrfs_block_group_root()
1034 struct btrfs_fs_info *fs_info = trans->fs_info; in remove_block_group_item()
1040 key.objectid = block_group->start; in remove_block_group_item()
1042 key.offset = block_group->length; in remove_block_group_item()
1044 ret = btrfs_search_slot(trans, root, &key, path, -1, 1); in remove_block_group_item()
1046 ret = -ENOENT; in remove_block_group_item()
1057 struct btrfs_fs_info *fs_info = trans->fs_info; in btrfs_remove_block_group()
1070 block_group = btrfs_lookup_block_group(fs_info, map->start); in btrfs_remove_block_group()
1072 return -ENOENT; in btrfs_remove_block_group()
1074 BUG_ON(!block_group->ro); in btrfs_remove_block_group()
1078 * Free the reserved super bytes from this block group before in btrfs_remove_block_group()
1082 btrfs_free_ref_tree_range(fs_info, block_group->start, in btrfs_remove_block_group()
1083 block_group->length); in btrfs_remove_block_group()
1085 index = btrfs_bg_flags_to_raid_index(block_group->flags); in btrfs_remove_block_group()
1086 factor = btrfs_bg_type_to_factor(block_group->flags); in btrfs_remove_block_group()
1088 /* make sure this block group isn't part of an allocation cluster */ in btrfs_remove_block_group()
1089 cluster = &fs_info->data_alloc_cluster; in btrfs_remove_block_group()
1090 spin_lock(&cluster->refill_lock); in btrfs_remove_block_group()
1092 spin_unlock(&cluster->refill_lock); in btrfs_remove_block_group()
1095 * make sure this block group isn't part of a metadata in btrfs_remove_block_group()
1098 cluster = &fs_info->meta_alloc_cluster; in btrfs_remove_block_group()
1099 spin_lock(&cluster->refill_lock); in btrfs_remove_block_group()
1101 spin_unlock(&cluster->refill_lock); in btrfs_remove_block_group()
1108 ret = -ENOMEM; in btrfs_remove_block_group()
1118 mutex_lock(&trans->transaction->cache_write_mutex); in btrfs_remove_block_group()
1120 * Make sure our free space cache IO is done before removing the in btrfs_remove_block_group()
1123 spin_lock(&trans->transaction->dirty_bgs_lock); in btrfs_remove_block_group()
1124 if (!list_empty(&block_group->io_list)) { in btrfs_remove_block_group()
1125 list_del_init(&block_group->io_list); in btrfs_remove_block_group()
1127 WARN_ON(!IS_ERR(inode) && inode != block_group->io_ctl.inode); in btrfs_remove_block_group()
1129 spin_unlock(&trans->transaction->dirty_bgs_lock); in btrfs_remove_block_group()
1132 spin_lock(&trans->transaction->dirty_bgs_lock); in btrfs_remove_block_group()
1135 if (!list_empty(&block_group->dirty_list)) { in btrfs_remove_block_group()
1136 list_del_init(&block_group->dirty_list); in btrfs_remove_block_group()
1140 spin_unlock(&trans->transaction->dirty_bgs_lock); in btrfs_remove_block_group()
1141 mutex_unlock(&trans->transaction->cache_write_mutex); in btrfs_remove_block_group()
1147 write_lock(&fs_info->block_group_cache_lock); in btrfs_remove_block_group()
1148 rb_erase_cached(&block_group->cache_node, in btrfs_remove_block_group()
1149 &fs_info->block_group_cache_tree); in btrfs_remove_block_group()
1150 RB_CLEAR_NODE(&block_group->cache_node); in btrfs_remove_block_group()
1152 /* Once for the block groups rbtree */ in btrfs_remove_block_group()
1155 write_unlock(&fs_info->block_group_cache_lock); in btrfs_remove_block_group()
1157 down_write(&block_group->space_info->groups_sem); in btrfs_remove_block_group()
1162 list_del_init(&block_group->list); in btrfs_remove_block_group()
1163 if (list_empty(&block_group->space_info->block_groups[index])) { in btrfs_remove_block_group()
1164 kobj = block_group->space_info->block_group_kobjs[index]; in btrfs_remove_block_group()
1165 block_group->space_info->block_group_kobjs[index] = NULL; in btrfs_remove_block_group()
1166 clear_avail_alloc_bits(fs_info, block_group->flags); in btrfs_remove_block_group()
1168 up_write(&block_group->space_info->groups_sem); in btrfs_remove_block_group()
1169 clear_incompat_bg_bits(fs_info, block_group->flags); in btrfs_remove_block_group()
1175 if (block_group->cached == BTRFS_CACHE_STARTED) in btrfs_remove_block_group()
1178 write_lock(&fs_info->block_group_cache_lock); in btrfs_remove_block_group()
1183 list_for_each_entry(ctl, &fs_info->caching_block_groups, list) { in btrfs_remove_block_group()
1184 if (ctl->block_group == block_group) { in btrfs_remove_block_group()
1186 refcount_inc(&caching_ctl->count); in btrfs_remove_block_group()
1192 list_del_init(&caching_ctl->list); in btrfs_remove_block_group()
1193 write_unlock(&fs_info->block_group_cache_lock); in btrfs_remove_block_group()
1201 spin_lock(&trans->transaction->dirty_bgs_lock); in btrfs_remove_block_group()
1202 WARN_ON(!list_empty(&block_group->dirty_list)); in btrfs_remove_block_group()
1203 WARN_ON(!list_empty(&block_group->io_list)); in btrfs_remove_block_group()
1204 spin_unlock(&trans->transaction->dirty_bgs_lock); in btrfs_remove_block_group()
1208 spin_lock(&block_group->space_info->lock); in btrfs_remove_block_group()
1209 list_del_init(&block_group->ro_list); in btrfs_remove_block_group()
1212 WARN_ON(block_group->space_info->total_bytes in btrfs_remove_block_group()
1213 < block_group->length); in btrfs_remove_block_group()
1214 WARN_ON(block_group->space_info->bytes_readonly in btrfs_remove_block_group()
1215 < block_group->length - block_group->zone_unusable); in btrfs_remove_block_group()
1216 WARN_ON(block_group->space_info->bytes_zone_unusable in btrfs_remove_block_group()
1217 < block_group->zone_unusable); in btrfs_remove_block_group()
1218 WARN_ON(block_group->space_info->disk_total in btrfs_remove_block_group()
1219 < block_group->length * factor); in btrfs_remove_block_group()
1221 block_group->space_info->total_bytes -= block_group->length; in btrfs_remove_block_group()
1222 block_group->space_info->bytes_readonly -= in btrfs_remove_block_group()
1223 (block_group->length - block_group->zone_unusable); in btrfs_remove_block_group()
1224 btrfs_space_info_update_bytes_zone_unusable(block_group->space_info, in btrfs_remove_block_group()
1225 -block_group->zone_unusable); in btrfs_remove_block_group()
1226 block_group->space_info->disk_total -= block_group->length * factor; in btrfs_remove_block_group()
1228 spin_unlock(&block_group->space_info->lock); in btrfs_remove_block_group()
1231 * Remove the free space for the block group from the free space tree in btrfs_remove_block_group()
1232 * and the block group's item from the extent tree before marking the in btrfs_remove_block_group()
1233 * block group as removed. This is to prevent races with tasks that in btrfs_remove_block_group()
1234 * freeze and unfreeze a block group, this task and another task in btrfs_remove_block_group()
1235 * allocating a new block group - the unfreeze task ends up removing in btrfs_remove_block_group()
1236 * the block group's extent map before the task calling this function in btrfs_remove_block_group()
1237 * deletes the block group item from the extent tree, allowing for in btrfs_remove_block_group()
1238 * another task to attempt to create another block group with the same in btrfs_remove_block_group()
1239 * item key (and failing with -EEXIST and a transaction abort). in btrfs_remove_block_group()
1249 spin_lock(&block_group->lock); in btrfs_remove_block_group()
1250 set_bit(BLOCK_GROUP_FLAG_REMOVED, &block_group->runtime_flags); in btrfs_remove_block_group()
1253 * At this point trimming or scrub can't start on this block group, in btrfs_remove_block_group()
1254 * because we removed the block group from the rbtree in btrfs_remove_block_group()
1255 * fs_info->block_group_cache_tree so no one can't find it anymore and in btrfs_remove_block_group()
1256 * even if someone already got this block group before we removed it in btrfs_remove_block_group()
1257 * from the rbtree, they have already incremented block_group->frozen - in btrfs_remove_block_group()
1262 * And we must not remove the chunk map from the fs_info->mapping_tree in btrfs_remove_block_group()
1264 * ranges from being reused for a new block group. This is needed to in btrfs_remove_block_group()
1270 * allowing for new block groups to be created that can reuse the same in btrfs_remove_block_group()
1274 * is mounted with -odiscard. The same protections must remain in btrfs_remove_block_group()
1278 remove_map = (atomic_read(&block_group->frozen) == 0); in btrfs_remove_block_group()
1279 spin_unlock(&block_group->lock); in btrfs_remove_block_group()
1302 ASSERT(map->start == chunk_offset); in btrfs_start_trans_remove_block_group()
1306 * to remove a block group (done at btrfs_remove_chunk() and at in btrfs_start_trans_remove_block_group()
1311 * 1 unit for deleting the block group item (located in the extent in btrfs_start_trans_remove_block_group()
1318 * In order to remove a block group we also need to reserve units in the in btrfs_start_trans_remove_block_group()
1323 num_items = 3 + map->num_stripes; in btrfs_start_trans_remove_block_group()
1330 * Mark block group @cache read-only, so later write won't happen to block
1331 * group @cache.
1333 * If @force is not set, this function will only mark the block group readonly
1334 * if we have enough free space (1M) in other metadata/system block groups.
1335 * If @force is not set, this function will mark the block group readonly
1338 * NOTE: This function doesn't care if other block groups can contain all the
1339 * data in this block group. That check should be done by relocation routine,
1342 static int inc_block_group_ro(struct btrfs_block_group *cache, int force) in inc_block_group_ro() argument
1344 struct btrfs_space_info *sinfo = cache->space_info; in inc_block_group_ro()
1346 int ret = -ENOSPC; in inc_block_group_ro()
1348 spin_lock(&sinfo->lock); in inc_block_group_ro()
1349 spin_lock(&cache->lock); in inc_block_group_ro()
1351 if (cache->swap_extents) { in inc_block_group_ro()
1352 ret = -ETXTBSY; in inc_block_group_ro()
1356 if (cache->ro) { in inc_block_group_ro()
1357 cache->ro++; in inc_block_group_ro()
1362 num_bytes = cache->length - cache->reserved - cache->pinned - in inc_block_group_ro()
1363 cache->bytes_super - cache->zone_unusable - cache->used; in inc_block_group_ro()
1371 } else if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA) { in inc_block_group_ro()
1378 if (sinfo_used + num_bytes <= sinfo->total_bytes) in inc_block_group_ro()
1385 * leeway to allow us to mark this block group as read only. in inc_block_group_ro()
1387 if (btrfs_can_overcommit(cache->fs_info, sinfo, num_bytes, in inc_block_group_ro()
1393 sinfo->bytes_readonly += num_bytes; in inc_block_group_ro()
1394 if (btrfs_is_zoned(cache->fs_info)) { in inc_block_group_ro()
1396 sinfo->bytes_readonly += cache->zone_unusable; in inc_block_group_ro()
1397 btrfs_space_info_update_bytes_zone_unusable(sinfo, -cache->zone_unusable); in inc_block_group_ro()
1398 cache->zone_unusable = 0; in inc_block_group_ro()
1400 cache->ro++; in inc_block_group_ro()
1401 list_add_tail(&cache->ro_list, &sinfo->ro_bgs); in inc_block_group_ro()
1404 spin_unlock(&cache->lock); in inc_block_group_ro()
1405 spin_unlock(&sinfo->lock); in inc_block_group_ro()
1406 if (ret == -ENOSPC && btrfs_test_opt(cache->fs_info, ENOSPC_DEBUG)) { in inc_block_group_ro()
1407 btrfs_info(cache->fs_info, in inc_block_group_ro()
1408 "unable to make block group %llu ro", cache->start); in inc_block_group_ro()
1409 btrfs_dump_space_info(cache->fs_info, cache->space_info, 0, 0); in inc_block_group_ro()
1417 struct btrfs_fs_info *fs_info = trans->fs_info; in clean_pinned_extents()
1419 const u64 start = bg->start; in clean_pinned_extents()
1420 const u64 end = start + bg->length - 1; in clean_pinned_extents()
1423 spin_lock(&fs_info->trans_lock); in clean_pinned_extents()
1424 if (trans->transaction->list.prev != &fs_info->trans_list) { in clean_pinned_extents()
1425 prev_trans = list_last_entry(&trans->transaction->list, in clean_pinned_extents()
1427 refcount_inc(&prev_trans->use_count); in clean_pinned_extents()
1429 spin_unlock(&fs_info->trans_lock); in clean_pinned_extents()
1435 * transaction N - 1, and have seen a range belonging to the block in clean_pinned_extents()
1436 * group in pinned_extents before we were able to clear the whole block in clean_pinned_extents()
1438 * the block group after we unpinned it from pinned_extents and removed in clean_pinned_extents()
1441 mutex_lock(&fs_info->unused_bg_unpin_mutex); in clean_pinned_extents()
1443 ret = clear_extent_bits(&prev_trans->pinned_extents, start, end, in clean_pinned_extents()
1449 ret = clear_extent_bits(&trans->transaction->pinned_extents, start, end, in clean_pinned_extents()
1452 mutex_unlock(&fs_info->unused_bg_unpin_mutex); in clean_pinned_extents()
1472 if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags)) in btrfs_delete_unused_bgs()
1482 if (!mutex_trylock(&fs_info->reclaim_bgs_lock)) in btrfs_delete_unused_bgs()
1485 spin_lock(&fs_info->unused_bgs_lock); in btrfs_delete_unused_bgs()
1486 while (!list_empty(&fs_info->unused_bgs)) { in btrfs_delete_unused_bgs()
1490 block_group = list_first_entry(&fs_info->unused_bgs, in btrfs_delete_unused_bgs()
1493 list_del_init(&block_group->bg_list); in btrfs_delete_unused_bgs()
1495 space_info = block_group->space_info; in btrfs_delete_unused_bgs()
1501 spin_unlock(&fs_info->unused_bgs_lock); in btrfs_delete_unused_bgs()
1503 btrfs_discard_cancel_work(&fs_info->discard_ctl, block_group); in btrfs_delete_unused_bgs()
1506 down_write(&space_info->groups_sem); in btrfs_delete_unused_bgs()
1509 * Async discard moves the final block group discard to be prior in btrfs_delete_unused_bgs()
1516 up_write(&space_info->groups_sem); in btrfs_delete_unused_bgs()
1518 btrfs_discard_queue_work(&fs_info->discard_ctl, in btrfs_delete_unused_bgs()
1523 spin_lock(&space_info->lock); in btrfs_delete_unused_bgs()
1524 spin_lock(&block_group->lock); in btrfs_delete_unused_bgs()
1525 if (btrfs_is_block_group_used(block_group) || block_group->ro || in btrfs_delete_unused_bgs()
1526 list_is_singular(&block_group->list)) { in btrfs_delete_unused_bgs()
1529 * outstanding allocations in this block group. We do in btrfs_delete_unused_bgs()
1531 * this block group. in btrfs_delete_unused_bgs()
1533 * Also bail out if this is the only block group for its in btrfs_delete_unused_bgs()
1535 * information from fs_info->avail_*_alloc_bits and the in btrfs_delete_unused_bgs()
1536 * next block group of this type would be created with a in btrfs_delete_unused_bgs()
1538 * fs_info->avail_*_alloc_bits would be 0. in btrfs_delete_unused_bgs()
1541 spin_unlock(&block_group->lock); in btrfs_delete_unused_bgs()
1542 spin_unlock(&space_info->lock); in btrfs_delete_unused_bgs()
1543 up_write(&space_info->groups_sem); in btrfs_delete_unused_bgs()
1548 * The block group may be unused but there may be space reserved in btrfs_delete_unused_bgs()
1549 * accounting with the existence of that block group, that is, in btrfs_delete_unused_bgs()
1550 * space_info->bytes_may_use was incremented by a task but no in btrfs_delete_unused_bgs()
1551 * space was yet allocated from the block group by the task. in btrfs_delete_unused_bgs()
1558 * So check if the total space of the space_info minus the size in btrfs_delete_unused_bgs()
1559 * of this block group is less than the used space of the in btrfs_delete_unused_bgs()
1560 * space_info - if that's the case, then it means we have tasks in btrfs_delete_unused_bgs()
1561 * that might be relying on the block group in order to allocate in btrfs_delete_unused_bgs()
1562 * extents, and add back the block group to the unused list when in btrfs_delete_unused_bgs()
1564 * needing to allocate extents from the block group. in btrfs_delete_unused_bgs()
1567 if (space_info->total_bytes - block_group->length < used && in btrfs_delete_unused_bgs()
1568 block_group->zone_unusable < block_group->length) { in btrfs_delete_unused_bgs()
1572 * fs_info->unused_bgs list. in btrfs_delete_unused_bgs()
1575 list_add_tail(&block_group->bg_list, &retry_list); in btrfs_delete_unused_bgs()
1578 spin_unlock(&block_group->lock); in btrfs_delete_unused_bgs()
1579 spin_unlock(&space_info->lock); in btrfs_delete_unused_bgs()
1580 up_write(&space_info->groups_sem); in btrfs_delete_unused_bgs()
1584 spin_unlock(&block_group->lock); in btrfs_delete_unused_bgs()
1585 spin_unlock(&space_info->lock); in btrfs_delete_unused_bgs()
1589 up_write(&space_info->groups_sem); in btrfs_delete_unused_bgs()
1598 if (ret == -EAGAIN) in btrfs_delete_unused_bgs()
1608 block_group->start); in btrfs_delete_unused_bgs()
1616 * We could have pending pinned extents for this block group, in btrfs_delete_unused_bgs()
1631 spin_lock(&fs_info->discard_ctl.lock); in btrfs_delete_unused_bgs()
1632 if (!list_empty(&block_group->discard_list)) { in btrfs_delete_unused_bgs()
1633 spin_unlock(&fs_info->discard_ctl.lock); in btrfs_delete_unused_bgs()
1635 btrfs_discard_queue_work(&fs_info->discard_ctl, in btrfs_delete_unused_bgs()
1639 spin_unlock(&fs_info->discard_ctl.lock); in btrfs_delete_unused_bgs()
1642 spin_lock(&space_info->lock); in btrfs_delete_unused_bgs()
1643 spin_lock(&block_group->lock); in btrfs_delete_unused_bgs()
1645 btrfs_space_info_update_bytes_pinned(space_info, -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 int thresh_pct = btrfs_calc_reclaim_threshold(bg->space_info); in should_reclaim_block_group()
1765 u64 thresh_bytes = mult_perc(bg->length, thresh_pct); in should_reclaim_block_group()
1766 const u64 new_val = bg->used; in should_reclaim_block_group()
1774 * brand new block group and we don't want to relocate new block groups. in should_reclaim_block_group()
1791 if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags)) in btrfs_reclaim_bgs_work()
1800 sb_start_write(fs_info->sb); in btrfs_reclaim_bgs_work()
1803 sb_end_write(fs_info->sb); in btrfs_reclaim_bgs_work()
1811 if (!mutex_trylock(&fs_info->reclaim_bgs_lock)) { in btrfs_reclaim_bgs_work()
1813 sb_end_write(fs_info->sb); in btrfs_reclaim_bgs_work()
1817 spin_lock(&fs_info->unused_bgs_lock); in btrfs_reclaim_bgs_work()
1820 * The block groups might still be in use and reachable via bg_list, in btrfs_reclaim_bgs_work()
1823 list_sort(NULL, &fs_info->reclaim_bgs, reclaim_bgs_cmp); in btrfs_reclaim_bgs_work()
1824 while (!list_empty(&fs_info->reclaim_bgs)) { in btrfs_reclaim_bgs_work()
1829 bg = list_first_entry(&fs_info->reclaim_bgs, in btrfs_reclaim_bgs_work()
1832 list_del_init(&bg->bg_list); in btrfs_reclaim_bgs_work()
1834 space_info = bg->space_info; in btrfs_reclaim_bgs_work()
1835 spin_unlock(&fs_info->unused_bgs_lock); in btrfs_reclaim_bgs_work()
1838 down_write(&space_info->groups_sem); in btrfs_reclaim_bgs_work()
1840 spin_lock(&space_info->lock); in btrfs_reclaim_bgs_work()
1841 spin_lock(&bg->lock); in btrfs_reclaim_bgs_work()
1842 if (bg->reserved || bg->pinned || bg->ro) { in btrfs_reclaim_bgs_work()
1845 * outstanding allocations in this block group. We do in btrfs_reclaim_bgs_work()
1847 * this block group. in btrfs_reclaim_bgs_work()
1849 spin_unlock(&bg->lock); in btrfs_reclaim_bgs_work()
1850 spin_unlock(&space_info->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 spin_unlock(&space_info->lock); in btrfs_reclaim_bgs_work()
1870 up_write(&space_info->groups_sem); in btrfs_reclaim_bgs_work()
1875 * The block group might no longer meet the reclaim condition by in btrfs_reclaim_bgs_work()
1884 if (!should_reclaim_block_group(bg, bg->length)) { in btrfs_reclaim_bgs_work()
1885 spin_unlock(&bg->lock); in btrfs_reclaim_bgs_work()
1886 spin_unlock(&space_info->lock); in btrfs_reclaim_bgs_work()
1887 up_write(&space_info->groups_sem); in btrfs_reclaim_bgs_work()
1890 spin_unlock(&bg->lock); in btrfs_reclaim_bgs_work()
1891 spin_unlock(&space_info->lock); in btrfs_reclaim_bgs_work()
1894 * Get out fast, in case we're read-only or unmounting the in btrfs_reclaim_bgs_work()
1895 * filesystem. It is OK to drop block groups from the list even in btrfs_reclaim_bgs_work()
1896 * for the read-only case. As we did sb_start_write(), in btrfs_reclaim_bgs_work()
1897 * "mount -o remount,ro" won't happen and read-only filesystem in btrfs_reclaim_bgs_work()
1898 * means it is forced read-only due to a fatal error. So, it in btrfs_reclaim_bgs_work()
1899 * never gets back to read-write to let us reclaim again. in btrfs_reclaim_bgs_work()
1902 up_write(&space_info->groups_sem); in btrfs_reclaim_bgs_work()
1907 * Cache the zone_unusable value before turning the block group in btrfs_reclaim_bgs_work()
1909 * zone_unusable value gets moved to the block group's read-only in btrfs_reclaim_bgs_work()
1912 zone_unusable = bg->zone_unusable; in btrfs_reclaim_bgs_work()
1914 up_write(&space_info->groups_sem); in btrfs_reclaim_bgs_work()
1920 bg->start, in btrfs_reclaim_bgs_work()
1921 div64_u64(bg->used * 100, bg->length), in btrfs_reclaim_bgs_work()
1922 div64_u64(zone_unusable * 100, bg->length)); in btrfs_reclaim_bgs_work()
1924 reclaimed = bg->used; in btrfs_reclaim_bgs_work()
1925 ret = btrfs_relocate_chunk(fs_info, bg->start); in btrfs_reclaim_bgs_work()
1929 bg->start); in btrfs_reclaim_bgs_work()
1931 spin_lock(&space_info->lock); in btrfs_reclaim_bgs_work()
1932 space_info->reclaim_errors++; in btrfs_reclaim_bgs_work()
1933 if (READ_ONCE(space_info->periodic_reclaim)) in btrfs_reclaim_bgs_work()
1934 space_info->periodic_reclaim_ready = false; in btrfs_reclaim_bgs_work()
1935 spin_unlock(&space_info->lock); in btrfs_reclaim_bgs_work()
1937 spin_lock(&space_info->lock); in btrfs_reclaim_bgs_work()
1938 space_info->reclaim_count++; in btrfs_reclaim_bgs_work()
1939 space_info->reclaim_bytes += reclaimed; in btrfs_reclaim_bgs_work()
1940 spin_unlock(&space_info->lock); in btrfs_reclaim_bgs_work()
1943 if (ret && !READ_ONCE(space_info->periodic_reclaim)) { in btrfs_reclaim_bgs_work()
1945 spin_lock(&fs_info->unused_bgs_lock); in btrfs_reclaim_bgs_work()
1947 * This block group might be added to the unused list in btrfs_reclaim_bgs_work()
1951 if (list_empty(&bg->bg_list)) { in btrfs_reclaim_bgs_work()
1953 list_add_tail(&bg->bg_list, &retry_list); in btrfs_reclaim_bgs_work()
1955 spin_unlock(&fs_info->unused_bgs_lock); in btrfs_reclaim_bgs_work()
1959 mutex_unlock(&fs_info->reclaim_bgs_lock); in btrfs_reclaim_bgs_work()
1961 * Reclaiming all the block groups in the list can take really in btrfs_reclaim_bgs_work()
1962 * long. Prioritize cleaning up unused block groups. in btrfs_reclaim_bgs_work()
1969 if (!mutex_trylock(&fs_info->reclaim_bgs_lock)) in btrfs_reclaim_bgs_work()
1971 spin_lock(&fs_info->unused_bgs_lock); in btrfs_reclaim_bgs_work()
1973 spin_unlock(&fs_info->unused_bgs_lock); in btrfs_reclaim_bgs_work()
1974 mutex_unlock(&fs_info->reclaim_bgs_lock); in btrfs_reclaim_bgs_work()
1976 spin_lock(&fs_info->unused_bgs_lock); in btrfs_reclaim_bgs_work()
1977 list_splice_tail(&retry_list, &fs_info->reclaim_bgs); in btrfs_reclaim_bgs_work()
1978 spin_unlock(&fs_info->unused_bgs_lock); in btrfs_reclaim_bgs_work()
1980 sb_end_write(fs_info->sb); in btrfs_reclaim_bgs_work()
1986 spin_lock(&fs_info->unused_bgs_lock); in btrfs_reclaim_bgs()
1987 if (!list_empty(&fs_info->reclaim_bgs)) in btrfs_reclaim_bgs()
1988 queue_work(system_unbound_wq, &fs_info->reclaim_bgs_work); in btrfs_reclaim_bgs()
1989 spin_unlock(&fs_info->unused_bgs_lock); in btrfs_reclaim_bgs()
1994 struct btrfs_fs_info *fs_info = bg->fs_info; in btrfs_mark_bg_to_reclaim()
1996 spin_lock(&fs_info->unused_bgs_lock); in btrfs_mark_bg_to_reclaim()
1997 if (list_empty(&bg->bg_list)) { in btrfs_mark_bg_to_reclaim()
2000 list_add_tail(&bg->bg_list, &fs_info->reclaim_bgs); in btrfs_mark_bg_to_reclaim()
2002 spin_unlock(&fs_info->unused_bgs_lock); in btrfs_mark_bg_to_reclaim()
2015 slot = path->slots[0]; in read_bg_from_eb()
2016 leaf = path->nodes[0]; in read_bg_from_eb()
2018 map = btrfs_find_chunk_map(fs_info, key->objectid, key->offset); in read_bg_from_eb()
2022 key->objectid, key->offset); in read_bg_from_eb()
2023 return -ENOENT; in read_bg_from_eb()
2026 if (map->start != key->objectid || map->chunk_len != key->offset) { in read_bg_from_eb()
2028 "block group %llu len %llu mismatch with chunk %llu len %llu", in read_bg_from_eb()
2029 key->objectid, key->offset, map->start, map->chunk_len); in read_bg_from_eb()
2030 ret = -EUCLEAN; in read_bg_from_eb()
2039 if (flags != (map->type & BTRFS_BLOCK_GROUP_TYPE_MASK)) { in read_bg_from_eb()
2041 "block group %llu len %llu type flags 0x%llx mismatch with chunk type flags 0x%llx", in read_bg_from_eb()
2042 key->objectid, key->offset, flags, in read_bg_from_eb()
2043 (BTRFS_BLOCK_GROUP_TYPE_MASK & map->type)); in read_bg_from_eb()
2044 ret = -EUCLEAN; in read_bg_from_eb()
2061 if (found_key.objectid >= key->objectid && in find_first_block_group()
2074 write_seqlock(&fs_info->profiles_lock); in set_avail_alloc_bits()
2076 fs_info->avail_data_alloc_bits |= extra_flags; in set_avail_alloc_bits()
2078 fs_info->avail_metadata_alloc_bits |= extra_flags; in set_avail_alloc_bits()
2080 fs_info->avail_system_alloc_bits |= extra_flags; in set_avail_alloc_bits()
2081 write_sequnlock(&fs_info->profiles_lock); in set_avail_alloc_bits()
2088 * @chunk_start: logical address of block group
2092 * @stripe_len: size of IO stripe for the given block group
2095 * Used primarily to exclude those portions of a block group that contain super
2096 * block copies.
2111 return -EIO; in btrfs_rmap_block()
2113 data_stripe_length = map->stripe_size; in btrfs_rmap_block()
2115 chunk_start = map->start; in btrfs_rmap_block()
2118 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) in btrfs_rmap_block()
2121 buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS); in btrfs_rmap_block()
2123 ret = -ENOMEM; in btrfs_rmap_block()
2127 for (i = 0; i < map->num_stripes; i++) { in btrfs_rmap_block()
2133 if (!in_range(physical, map->stripes[i].physical, in btrfs_rmap_block()
2137 stripe_nr = (physical - map->stripes[i].physical) >> in btrfs_rmap_block()
2139 offset = (physical - map->stripes[i].physical) & in btrfs_rmap_block()
2142 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | in btrfs_rmap_block()
2144 stripe_nr = div_u64(stripe_nr * map->num_stripes + i, in btrfs_rmap_block()
2145 map->sub_stripes); in btrfs_rmap_block()
2149 * instead of map->stripe_len in btrfs_rmap_block()
2173 static int exclude_super_stripes(struct btrfs_block_group *cache) in exclude_super_stripes() argument
2175 struct btrfs_fs_info *fs_info = cache->fs_info; in exclude_super_stripes()
2182 if (cache->start < BTRFS_SUPER_INFO_OFFSET) { in exclude_super_stripes()
2183 stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->start; in exclude_super_stripes()
2184 cache->bytes_super += stripe_len; in exclude_super_stripes()
2185 ret = set_extent_bit(&fs_info->excluded_extents, cache->start, in exclude_super_stripes()
2186 cache->start + stripe_len - 1, in exclude_super_stripes()
2194 ret = btrfs_rmap_block(fs_info, cache->start, in exclude_super_stripes()
2203 "zoned: block group %llu must not contain super block", in exclude_super_stripes()
2204 cache->start); in exclude_super_stripes()
2205 return -EUCLEAN; in exclude_super_stripes()
2208 while (nr--) { in exclude_super_stripes()
2210 cache->start + cache->length - logical[nr]); in exclude_super_stripes()
2212 cache->bytes_super += len; in exclude_super_stripes()
2213 ret = set_extent_bit(&fs_info->excluded_extents, logical[nr], in exclude_super_stripes()
2214 logical[nr] + len - 1, in exclude_super_stripes()
2230 struct btrfs_block_group *cache; in btrfs_create_block_group_cache() local
2232 cache = kzalloc(sizeof(*cache), GFP_NOFS); in btrfs_create_block_group_cache()
2233 if (!cache) in btrfs_create_block_group_cache()
2236 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl), in btrfs_create_block_group_cache()
2238 if (!cache->free_space_ctl) { in btrfs_create_block_group_cache()
2239 kfree(cache); in btrfs_create_block_group_cache()
2243 cache->start = start; in btrfs_create_block_group_cache()
2245 cache->fs_info = fs_info; in btrfs_create_block_group_cache()
2246 cache->full_stripe_len = btrfs_full_stripe_len(fs_info, start); in btrfs_create_block_group_cache()
2248 cache->discard_index = BTRFS_DISCARD_INDEX_UNUSED; in btrfs_create_block_group_cache()
2250 refcount_set(&cache->refs, 1); in btrfs_create_block_group_cache()
2251 spin_lock_init(&cache->lock); in btrfs_create_block_group_cache()
2252 init_rwsem(&cache->data_rwsem); in btrfs_create_block_group_cache()
2253 INIT_LIST_HEAD(&cache->list); in btrfs_create_block_group_cache()
2254 INIT_LIST_HEAD(&cache->cluster_list); in btrfs_create_block_group_cache()
2255 INIT_LIST_HEAD(&cache->bg_list); in btrfs_create_block_group_cache()
2256 INIT_LIST_HEAD(&cache->ro_list); in btrfs_create_block_group_cache()
2257 INIT_LIST_HEAD(&cache->discard_list); in btrfs_create_block_group_cache()
2258 INIT_LIST_HEAD(&cache->dirty_list); in btrfs_create_block_group_cache()
2259 INIT_LIST_HEAD(&cache->io_list); in btrfs_create_block_group_cache()
2260 INIT_LIST_HEAD(&cache->active_bg_list); in btrfs_create_block_group_cache()
2261 btrfs_init_free_space_ctl(cache, cache->free_space_ctl); in btrfs_create_block_group_cache()
2262 atomic_set(&cache->frozen, 0); in btrfs_create_block_group_cache()
2263 mutex_init(&cache->free_space_lock); in btrfs_create_block_group_cache()
2265 return cache; in btrfs_create_block_group_cache()
2269 * Iterate all chunks and verify that each of them has the corresponding block
2290 bg = btrfs_lookup_block_group(fs_info, map->start); in check_chunk_block_group_mappings()
2293 "chunk start=%llu len=%llu doesn't have corresponding block group", in check_chunk_block_group_mappings()
2294 map->start, map->chunk_len); in check_chunk_block_group_mappings()
2295 ret = -EUCLEAN; in check_chunk_block_group_mappings()
2299 if (bg->start != map->start || bg->length != map->chunk_len || in check_chunk_block_group_mappings()
2300 (bg->flags & BTRFS_BLOCK_GROUP_TYPE_MASK) != in check_chunk_block_group_mappings()
2301 (map->type & BTRFS_BLOCK_GROUP_TYPE_MASK)) { in check_chunk_block_group_mappings()
2303 "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()
2304 map->start, map->chunk_len, in check_chunk_block_group_mappings()
2305 map->type & BTRFS_BLOCK_GROUP_TYPE_MASK, in check_chunk_block_group_mappings()
2306 bg->start, bg->length, in check_chunk_block_group_mappings()
2307 bg->flags & BTRFS_BLOCK_GROUP_TYPE_MASK); in check_chunk_block_group_mappings()
2308 ret = -EUCLEAN; in check_chunk_block_group_mappings()
2313 start = map->start + map->chunk_len; in check_chunk_block_group_mappings()
2325 struct btrfs_block_group *cache; in read_one_block_group() local
2329 ASSERT(key->type == BTRFS_BLOCK_GROUP_ITEM_KEY); in read_one_block_group()
2331 cache = btrfs_create_block_group_cache(info, key->objectid); in read_one_block_group()
2332 if (!cache) in read_one_block_group()
2333 return -ENOMEM; in read_one_block_group()
2335 cache->length = key->offset; in read_one_block_group()
2336 cache->used = btrfs_stack_block_group_used(bgi); in read_one_block_group()
2337 cache->commit_used = cache->used; in read_one_block_group()
2338 cache->flags = btrfs_stack_block_group_flags(bgi); in read_one_block_group()
2339 cache->global_root_id = btrfs_stack_block_group_chunk_objectid(bgi); in read_one_block_group()
2341 set_free_space_tree_thresholds(cache); in read_one_block_group()
2345 * When we mount with old space cache, we need to in read_one_block_group()
2349 * truncate the old free space cache inode and in read_one_block_group()
2352 * the new space cache info onto disk. in read_one_block_group()
2355 cache->disk_cache_state = BTRFS_DC_CLEAR; in read_one_block_group()
2357 if (!mixed && ((cache->flags & BTRFS_BLOCK_GROUP_METADATA) && in read_one_block_group()
2358 (cache->flags & BTRFS_BLOCK_GROUP_DATA))) { in read_one_block_group()
2360 "bg %llu is a mixed block group but filesystem hasn't enabled mixed block groups", in read_one_block_group()
2361 cache->start); in read_one_block_group()
2362 ret = -EINVAL; in read_one_block_group()
2366 ret = btrfs_load_block_group_zone_info(cache, false); in read_one_block_group()
2369 cache->start); in read_one_block_group()
2378 ret = exclude_super_stripes(cache); in read_one_block_group()
2381 btrfs_free_excluded_extents(cache); in read_one_block_group()
2387 * free space for a block group. So, we don't need any caching work. in read_one_block_group()
2398 btrfs_calc_zone_unusable(cache); in read_one_block_group()
2400 btrfs_free_excluded_extents(cache); in read_one_block_group()
2401 } else if (cache->length == cache->used) { in read_one_block_group()
2402 cache->cached = BTRFS_CACHE_FINISHED; in read_one_block_group()
2403 btrfs_free_excluded_extents(cache); in read_one_block_group()
2404 } else if (cache->used == 0) { in read_one_block_group()
2405 cache->cached = BTRFS_CACHE_FINISHED; in read_one_block_group()
2406 ret = btrfs_add_new_free_space(cache, cache->start, in read_one_block_group()
2407 cache->start + cache->length, NULL); in read_one_block_group()
2408 btrfs_free_excluded_extents(cache); in read_one_block_group()
2413 ret = btrfs_add_block_group_cache(info, cache); in read_one_block_group()
2415 btrfs_remove_free_space_cache(cache); in read_one_block_group()
2418 trace_btrfs_add_block_group(info, cache, 0); in read_one_block_group()
2419 btrfs_add_bg_to_space_info(info, cache); in read_one_block_group()
2421 set_avail_alloc_bits(info, cache->flags); in read_one_block_group()
2422 if (btrfs_chunk_writeable(info, cache->start)) { in read_one_block_group()
2423 if (cache->used == 0) { in read_one_block_group()
2424 ASSERT(list_empty(&cache->bg_list)); in read_one_block_group()
2426 btrfs_discard_queue_work(&info->discard_ctl, cache); in read_one_block_group()
2428 btrfs_mark_bg_unused(cache); in read_one_block_group()
2431 inc_block_group_ro(cache, 1); in read_one_block_group()
2436 btrfs_put_block_group(cache); in read_one_block_group()
2445 for (node = rb_first_cached(&fs_info->mapping_tree); node; node = rb_next(node)) { in fill_dummy_bgs()
2450 bg = btrfs_create_block_group_cache(fs_info, map->start); in fill_dummy_bgs()
2452 ret = -ENOMEM; in fill_dummy_bgs()
2456 /* Fill dummy cache as FULL */ in fill_dummy_bgs()
2457 bg->length = map->chunk_len; in fill_dummy_bgs()
2458 bg->flags = map->type; in fill_dummy_bgs()
2459 bg->cached = BTRFS_CACHE_FINISHED; in fill_dummy_bgs()
2460 bg->used = map->chunk_len; in fill_dummy_bgs()
2461 bg->flags = map->type; in fill_dummy_bgs()
2464 * We may have some valid block group cache added already, in in fill_dummy_bgs()
2467 if (ret == -EEXIST) { in fill_dummy_bgs()
2481 set_avail_alloc_bits(fs_info, bg->flags); in fill_dummy_bgs()
2493 struct btrfs_block_group *cache; in btrfs_read_block_groups() local
2501 * unsupported RO options. The fs can never be mounted read-write, so no in btrfs_read_block_groups()
2502 * need to waste time searching block group items. in btrfs_read_block_groups()
2507 if (!root || (btrfs_super_compat_ro_flags(info->super_copy) & in btrfs_read_block_groups()
2516 return -ENOMEM; in btrfs_read_block_groups()
2518 cache_gen = btrfs_super_cache_generation(info->super_copy); in btrfs_read_block_groups()
2520 btrfs_super_generation(info->super_copy) != cache_gen) in btrfs_read_block_groups()
2536 leaf = path->nodes[0]; in btrfs_read_block_groups()
2537 slot = path->slots[0]; in btrfs_read_block_groups()
2552 list_for_each_entry(space_info, &info->space_info, list) { in btrfs_read_block_groups()
2556 if (list_empty(&space_info->block_groups[i])) in btrfs_read_block_groups()
2558 cache = list_first_entry(&space_info->block_groups[i], in btrfs_read_block_groups()
2561 btrfs_sysfs_add_block_group_type(cache); in btrfs_read_block_groups()
2564 if (!(btrfs_get_alloc_profile(info, space_info->flags) & in btrfs_read_block_groups()
2571 * Avoid allocating from un-mirrored block group if there are in btrfs_read_block_groups()
2572 * mirrored block groups. in btrfs_read_block_groups()
2574 list_for_each_entry(cache, in btrfs_read_block_groups()
2575 &space_info->block_groups[BTRFS_RAID_RAID0], in btrfs_read_block_groups()
2577 inc_block_group_ro(cache, 1); in btrfs_read_block_groups()
2578 list_for_each_entry(cache, in btrfs_read_block_groups()
2579 &space_info->block_groups[BTRFS_RAID_SINGLE], in btrfs_read_block_groups()
2581 inc_block_group_ro(cache, 1); in btrfs_read_block_groups()
2591 * Try to fill the tree using dummy block groups so that the user can in btrfs_read_block_groups()
2609 struct btrfs_fs_info *fs_info = trans->fs_info; in insert_block_group_item()
2616 spin_lock(&block_group->lock); in insert_block_group_item()
2617 btrfs_set_stack_block_group_used(&bgi, block_group->used); in insert_block_group_item()
2619 block_group->global_root_id); in insert_block_group_item()
2620 btrfs_set_stack_block_group_flags(&bgi, block_group->flags); in insert_block_group_item()
2621 old_commit_used = block_group->commit_used; in insert_block_group_item()
2622 block_group->commit_used = block_group->used; in insert_block_group_item()
2623 key.objectid = block_group->start; in insert_block_group_item()
2625 key.offset = block_group->length; in insert_block_group_item()
2626 spin_unlock(&block_group->lock); in insert_block_group_item()
2630 spin_lock(&block_group->lock); in insert_block_group_item()
2631 block_group->commit_used = old_commit_used; in insert_block_group_item()
2632 spin_unlock(&block_group->lock); in insert_block_group_item()
2642 struct btrfs_fs_info *fs_info = device->fs_info; in insert_dev_extent()
2643 struct btrfs_root *root = fs_info->dev_root; in insert_dev_extent()
2650 WARN_ON(!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state)); in insert_dev_extent()
2651 WARN_ON(test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)); in insert_dev_extent()
2654 return -ENOMEM; in insert_dev_extent()
2656 key.objectid = device->devid; in insert_dev_extent()
2663 leaf = path->nodes[0]; in insert_dev_extent()
2664 extent = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_extent); in insert_dev_extent()
2685 struct btrfs_fs_info *fs_info = trans->fs_info; in insert_dev_extents()
2701 * (dev-replace.c:btrfs_dev_replace_finishing()), so we could grab the in insert_dev_extents()
2705 mutex_lock(&fs_info->fs_devices->device_list_mutex); in insert_dev_extents()
2706 for (i = 0; i < map->num_stripes; i++) { in insert_dev_extents()
2707 device = map->stripes[i].dev; in insert_dev_extents()
2708 dev_offset = map->stripes[i].physical; in insert_dev_extents()
2711 map->stripe_size); in insert_dev_extents()
2715 mutex_unlock(&fs_info->fs_devices->device_list_mutex); in insert_dev_extents()
2730 struct btrfs_fs_info *fs_info = trans->fs_info; in btrfs_create_pending_block_groups()
2734 while (!list_empty(&trans->new_bgs)) { in btrfs_create_pending_block_groups()
2737 block_group = list_first_entry(&trans->new_bgs, in btrfs_create_pending_block_groups()
2743 index = btrfs_bg_flags_to_raid_index(block_group->flags); in btrfs_create_pending_block_groups()
2749 &block_group->runtime_flags)) { in btrfs_create_pending_block_groups()
2750 mutex_lock(&fs_info->chunk_mutex); in btrfs_create_pending_block_groups()
2752 mutex_unlock(&fs_info->chunk_mutex); in btrfs_create_pending_block_groups()
2756 ret = insert_dev_extents(trans, block_group->start, in btrfs_create_pending_block_groups()
2757 block_group->length); in btrfs_create_pending_block_groups()
2768 if (block_group->space_info->block_group_kobjs[index] == NULL) in btrfs_create_pending_block_groups()
2774 list_del_init(&block_group->bg_list); in btrfs_create_pending_block_groups()
2775 clear_bit(BLOCK_GROUP_FLAG_NEW, &block_group->runtime_flags); in btrfs_create_pending_block_groups()
2778 * If the block group is still unused, add it to the list of in btrfs_create_pending_block_groups()
2779 * unused block groups. The block group may have been created in in btrfs_create_pending_block_groups()
2783 * so the block group may become unused for a long time. For in btrfs_create_pending_block_groups()
2792 * uncompressed data size, because the compression is only done in btrfs_create_pending_block_groups()
2795 * size because the data may be incompressible in the worst case. in btrfs_create_pending_block_groups()
2800 spin_lock(&block_group->lock); in btrfs_create_pending_block_groups()
2802 spin_unlock(&block_group->lock); in btrfs_create_pending_block_groups()
2812 * For extent tree v2 we use the block_group_item->chunk_offset to point at our
2824 if (btrfs_super_total_bytes(fs_info->super_copy) <= (SZ_1G * 10ULL)) in calculate_global_root_id()
2828 div64_u64_rem(offset, fs_info->nr_global_roots, &index); in calculate_global_root_id()
2834 u64 chunk_offset, u64 size) in btrfs_make_block_group() argument
2836 struct btrfs_fs_info *fs_info = trans->fs_info; in btrfs_make_block_group()
2837 struct btrfs_block_group *cache; in btrfs_make_block_group() local
2842 cache = btrfs_create_block_group_cache(fs_info, chunk_offset); in btrfs_make_block_group()
2843 if (!cache) in btrfs_make_block_group()
2844 return ERR_PTR(-ENOMEM); in btrfs_make_block_group()
2847 * Mark it as new before adding it to the rbtree of block groups or any in btrfs_make_block_group()
2851 set_bit(BLOCK_GROUP_FLAG_NEW, &cache->runtime_flags); in btrfs_make_block_group()
2853 cache->length = size; in btrfs_make_block_group()
2854 set_free_space_tree_thresholds(cache); in btrfs_make_block_group()
2855 cache->flags = type; in btrfs_make_block_group()
2856 cache->cached = BTRFS_CACHE_FINISHED; in btrfs_make_block_group()
2857 cache->global_root_id = calculate_global_root_id(fs_info, cache->start); in btrfs_make_block_group()
2860 set_bit(BLOCK_GROUP_FLAG_NEEDS_FREE_SPACE, &cache->runtime_flags); in btrfs_make_block_group()
2862 ret = btrfs_load_block_group_zone_info(cache, true); in btrfs_make_block_group()
2864 btrfs_put_block_group(cache); in btrfs_make_block_group()
2868 ret = exclude_super_stripes(cache); in btrfs_make_block_group()
2871 btrfs_free_excluded_extents(cache); in btrfs_make_block_group()
2872 btrfs_put_block_group(cache); in btrfs_make_block_group()
2876 ret = btrfs_add_new_free_space(cache, chunk_offset, chunk_offset + size, NULL); in btrfs_make_block_group()
2877 btrfs_free_excluded_extents(cache); in btrfs_make_block_group()
2879 btrfs_put_block_group(cache); in btrfs_make_block_group()
2885 * assigned to our block group. We want our bg to be added to the rbtree in btrfs_make_block_group()
2886 * with its ->space_info set. in btrfs_make_block_group()
2888 cache->space_info = btrfs_find_space_info(fs_info, cache->flags); in btrfs_make_block_group()
2889 ASSERT(cache->space_info); in btrfs_make_block_group()
2891 ret = btrfs_add_block_group_cache(fs_info, cache); in btrfs_make_block_group()
2893 btrfs_remove_free_space_cache(cache); in btrfs_make_block_group()
2894 btrfs_put_block_group(cache); in btrfs_make_block_group()
2899 * Now that our block group has its ->space_info set and is inserted in in btrfs_make_block_group()
2902 trace_btrfs_add_block_group(fs_info, cache, 1); in btrfs_make_block_group()
2903 btrfs_add_bg_to_space_info(fs_info, cache); in btrfs_make_block_group()
2907 if (btrfs_should_fragment_free_space(cache)) { in btrfs_make_block_group()
2908 cache->space_info->bytes_used += size >> 1; in btrfs_make_block_group()
2909 fragment_free_space(cache); in btrfs_make_block_group()
2913 list_add_tail(&cache->bg_list, &trans->new_bgs); in btrfs_make_block_group()
2917 return cache; in btrfs_make_block_group()
2921 * Mark one block group RO, can be called several times for the same block
2924 * @cache: the destination block group
2925 * @do_chunk_alloc: whether need to do chunk pre-allocation, this is to
2927 * block group RO.
2929 int btrfs_inc_block_group_ro(struct btrfs_block_group *cache, in btrfs_inc_block_group_ro() argument
2932 struct btrfs_fs_info *fs_info = cache->fs_info; in btrfs_inc_block_group_ro()
2940 * This can only happen when we are doing read-only scrub on read-only in btrfs_inc_block_group_ro()
2942 * In that case we should not start a new transaction on read-only fs. in btrfs_inc_block_group_ro()
2945 if (sb_rdonly(fs_info->sb)) { in btrfs_inc_block_group_ro()
2946 mutex_lock(&fs_info->ro_block_group_mutex); in btrfs_inc_block_group_ro()
2947 ret = inc_block_group_ro(cache, 0); in btrfs_inc_block_group_ro()
2948 mutex_unlock(&fs_info->ro_block_group_mutex); in btrfs_inc_block_group_ro()
2960 * We're not allowed to set block groups readonly after the dirty in btrfs_inc_block_group_ro()
2961 * block group cache has started writing. If it already started, in btrfs_inc_block_group_ro()
2964 mutex_lock(&fs_info->ro_block_group_mutex); in btrfs_inc_block_group_ro()
2965 if (test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &trans->transaction->flags)) { in btrfs_inc_block_group_ro()
2966 u64 transid = trans->transid; in btrfs_inc_block_group_ro()
2968 mutex_unlock(&fs_info->ro_block_group_mutex); in btrfs_inc_block_group_ro()
2981 * corresponding block group with the new raid level. in btrfs_inc_block_group_ro()
2983 alloc_flags = btrfs_get_alloc_profile(fs_info, cache->flags); in btrfs_inc_block_group_ro()
2984 if (alloc_flags != cache->flags) { in btrfs_inc_block_group_ro()
2991 if (ret == -ENOSPC) in btrfs_inc_block_group_ro()
2998 ret = inc_block_group_ro(cache, 0); in btrfs_inc_block_group_ro()
3001 if (ret == -ETXTBSY) in btrfs_inc_block_group_ro()
3007 * we still want to try our best to mark the block group read-only. in btrfs_inc_block_group_ro()
3009 if (!do_chunk_alloc && ret == -ENOSPC && in btrfs_inc_block_group_ro()
3010 (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM)) in btrfs_inc_block_group_ro()
3013 alloc_flags = btrfs_get_alloc_profile(fs_info, cache->space_info->flags); in btrfs_inc_block_group_ro()
3021 ret = btrfs_zoned_activate_one_bg(fs_info, cache->space_info, true); in btrfs_inc_block_group_ro()
3025 ret = inc_block_group_ro(cache, 0); in btrfs_inc_block_group_ro()
3026 if (ret == -ETXTBSY) in btrfs_inc_block_group_ro()
3029 if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) { in btrfs_inc_block_group_ro()
3030 alloc_flags = btrfs_get_alloc_profile(fs_info, cache->flags); in btrfs_inc_block_group_ro()
3031 mutex_lock(&fs_info->chunk_mutex); in btrfs_inc_block_group_ro()
3033 mutex_unlock(&fs_info->chunk_mutex); in btrfs_inc_block_group_ro()
3036 mutex_unlock(&fs_info->ro_block_group_mutex); in btrfs_inc_block_group_ro()
3042 void btrfs_dec_block_group_ro(struct btrfs_block_group *cache) in btrfs_dec_block_group_ro() argument
3044 struct btrfs_space_info *sinfo = cache->space_info; in btrfs_dec_block_group_ro()
3047 BUG_ON(!cache->ro); in btrfs_dec_block_group_ro()
3049 spin_lock(&sinfo->lock); in btrfs_dec_block_group_ro()
3050 spin_lock(&cache->lock); in btrfs_dec_block_group_ro()
3051 if (!--cache->ro) { in btrfs_dec_block_group_ro()
3052 if (btrfs_is_zoned(cache->fs_info)) { in btrfs_dec_block_group_ro()
3054 cache->zone_unusable = in btrfs_dec_block_group_ro()
3055 (cache->alloc_offset - cache->used - cache->pinned - in btrfs_dec_block_group_ro()
3056 cache->reserved) + in btrfs_dec_block_group_ro()
3057 (cache->length - cache->zone_capacity); in btrfs_dec_block_group_ro()
3058 btrfs_space_info_update_bytes_zone_unusable(sinfo, cache->zone_unusable); in btrfs_dec_block_group_ro()
3059 sinfo->bytes_readonly -= cache->zone_unusable; in btrfs_dec_block_group_ro()
3061 num_bytes = cache->length - cache->reserved - in btrfs_dec_block_group_ro()
3062 cache->pinned - cache->bytes_super - in btrfs_dec_block_group_ro()
3063 cache->zone_unusable - cache->used; in btrfs_dec_block_group_ro()
3064 sinfo->bytes_readonly -= num_bytes; in btrfs_dec_block_group_ro()
3065 list_del_init(&cache->ro_list); in btrfs_dec_block_group_ro()
3067 spin_unlock(&cache->lock); in btrfs_dec_block_group_ro()
3068 spin_unlock(&sinfo->lock); in btrfs_dec_block_group_ro()
3073 struct btrfs_block_group *cache) in update_block_group_item() argument
3075 struct btrfs_fs_info *fs_info = trans->fs_info; in update_block_group_item()
3086 * Block group items update can be triggered out of commit transaction in update_block_group_item()
3088 * We cannot use cache->used directly outside of the spin lock, as it in update_block_group_item()
3091 spin_lock(&cache->lock); in update_block_group_item()
3092 old_commit_used = cache->commit_used; in update_block_group_item()
3093 used = cache->used; in update_block_group_item()
3095 if (cache->commit_used == used) { in update_block_group_item()
3096 spin_unlock(&cache->lock); in update_block_group_item()
3099 cache->commit_used = used; in update_block_group_item()
3100 spin_unlock(&cache->lock); in update_block_group_item()
3102 key.objectid = cache->start; in update_block_group_item()
3104 key.offset = cache->length; in update_block_group_item()
3109 ret = -ENOENT; in update_block_group_item()
3113 leaf = path->nodes[0]; in update_block_group_item()
3114 bi = btrfs_item_ptr_offset(leaf, path->slots[0]); in update_block_group_item()
3117 cache->global_root_id); in update_block_group_item()
3118 btrfs_set_stack_block_group_flags(&bgi, cache->flags); in update_block_group_item()
3123 * We didn't update the block group item, need to revert commit_used in update_block_group_item()
3124 * unless the block group item didn't exist yet - this is to prevent a in update_block_group_item()
3125 * race with a concurrent insertion of the block group item, with in update_block_group_item()
3128 * insertion set it to a value greater than 0 - if the block group later in update_block_group_item()
3131 if (ret < 0 && ret != -ENOENT) { in update_block_group_item()
3132 spin_lock(&cache->lock); in update_block_group_item()
3133 cache->commit_used = old_commit_used; in update_block_group_item()
3134 spin_unlock(&cache->lock); in update_block_group_item()
3144 struct btrfs_fs_info *fs_info = block_group->fs_info; in cache_save_setup()
3157 * If this block group is smaller than 100 megs don't bother caching the in cache_save_setup()
3158 * block group. in cache_save_setup()
3160 if (block_group->length < (100 * SZ_1M)) { in cache_save_setup()
3161 spin_lock(&block_group->lock); in cache_save_setup()
3162 block_group->disk_cache_state = BTRFS_DC_WRITTEN; in cache_save_setup()
3163 spin_unlock(&block_group->lock); in cache_save_setup()
3171 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) { in cache_save_setup()
3181 if (block_group->ro) in cache_save_setup()
3192 * from here on out we know not to trust this cache when we load up next in cache_save_setup()
3195 BTRFS_I(inode)->generation = 0; in cache_save_setup()
3200 * super cache generation to 0 so we know to invalidate the in cache_save_setup()
3201 * cache, but then we'd have to keep track of the block groups in cache_save_setup()
3202 * that fail this way so we know we _have_ to reset this cache in cache_save_setup()
3203 * before the next commit or risk reading stale cache. So to in cache_save_setup()
3214 if (block_group->cache_generation == trans->transid && in cache_save_setup()
3222 &fs_info->global_block_rsv); in cache_save_setup()
3231 spin_lock(&block_group->lock); in cache_save_setup()
3232 if (block_group->cached != BTRFS_CACHE_FINISHED || in cache_save_setup()
3241 spin_unlock(&block_group->lock); in cache_save_setup()
3244 spin_unlock(&block_group->lock); in cache_save_setup()
3247 * We hit an ENOSPC when setting up the cache in this transaction, just in cache_save_setup()
3248 * skip doing the setup, we've already cleared the cache so we're safe. in cache_save_setup()
3250 if (test_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags)) { in cache_save_setup()
3251 ret = -ENOSPC; in cache_save_setup()
3256 * Try to preallocate enough space based on how big the block group is. in cache_save_setup()
3259 * cache. in cache_save_setup()
3261 cache_size = div_u64(block_group->length, SZ_256M); in cache_save_setup()
3266 cache_size *= fs_info->sectorsize; in cache_save_setup()
3277 * Our cache requires contiguous chunks so that we don't modify a bunch in cache_save_setup()
3278 * of metadata or split extents when writing the cache out, which means in cache_save_setup()
3281 * other block groups for this transaction, maybe we'll unpin enough in cache_save_setup()
3286 else if (ret == -ENOSPC) in cache_save_setup()
3287 set_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags); in cache_save_setup()
3294 spin_lock(&block_group->lock); in cache_save_setup()
3296 block_group->cache_generation = trans->transid; in cache_save_setup()
3297 block_group->disk_cache_state = dcs; in cache_save_setup()
3298 spin_unlock(&block_group->lock); in cache_save_setup()
3306 struct btrfs_fs_info *fs_info = trans->fs_info; in btrfs_setup_space_cache()
3307 struct btrfs_block_group *cache, *tmp; in btrfs_setup_space_cache() local
3308 struct btrfs_transaction *cur_trans = trans->transaction; in btrfs_setup_space_cache()
3311 if (list_empty(&cur_trans->dirty_bgs) || in btrfs_setup_space_cache()
3317 return -ENOMEM; in btrfs_setup_space_cache()
3319 /* Could add new block groups, use _safe just in case */ in btrfs_setup_space_cache()
3320 list_for_each_entry_safe(cache, tmp, &cur_trans->dirty_bgs, in btrfs_setup_space_cache()
3322 if (cache->disk_cache_state == BTRFS_DC_CLEAR) in btrfs_setup_space_cache()
3323 cache_save_setup(cache, trans, path); in btrfs_setup_space_cache()
3331 * Transaction commit does final block group cache writeback during a critical
3333 * order for the cache to actually match the block group, but can introduce a
3336 * So, btrfs_start_dirty_block_groups is here to kick off block group cache IO.
3337 * There's a chance we'll have to redo some of it if the block group changes
3339 * getting rid of the easy block groups while we're still allowing others to
3344 struct btrfs_fs_info *fs_info = trans->fs_info; in btrfs_start_dirty_block_groups()
3345 struct btrfs_block_group *cache; in btrfs_start_dirty_block_groups() local
3346 struct btrfs_transaction *cur_trans = trans->transaction; in btrfs_start_dirty_block_groups()
3351 struct list_head *io = &cur_trans->io_bgs; in btrfs_start_dirty_block_groups()
3354 spin_lock(&cur_trans->dirty_bgs_lock); in btrfs_start_dirty_block_groups()
3355 if (list_empty(&cur_trans->dirty_bgs)) { in btrfs_start_dirty_block_groups()
3356 spin_unlock(&cur_trans->dirty_bgs_lock); in btrfs_start_dirty_block_groups()
3359 list_splice_init(&cur_trans->dirty_bgs, &dirty); in btrfs_start_dirty_block_groups()
3360 spin_unlock(&cur_trans->dirty_bgs_lock); in btrfs_start_dirty_block_groups()
3363 /* Make sure all the block groups on our dirty list actually exist */ in btrfs_start_dirty_block_groups()
3369 ret = -ENOMEM; in btrfs_start_dirty_block_groups()
3376 * removal of empty block groups deleting this block group while we are in btrfs_start_dirty_block_groups()
3377 * writing out the cache in btrfs_start_dirty_block_groups()
3379 mutex_lock(&trans->transaction->cache_write_mutex); in btrfs_start_dirty_block_groups()
3383 cache = list_first_entry(&dirty, struct btrfs_block_group, in btrfs_start_dirty_block_groups()
3386 * This can happen if something re-dirties a block group that in btrfs_start_dirty_block_groups()
3390 if (!list_empty(&cache->io_list)) { in btrfs_start_dirty_block_groups()
3391 list_del_init(&cache->io_list); in btrfs_start_dirty_block_groups()
3392 btrfs_wait_cache_io(trans, cache, path); in btrfs_start_dirty_block_groups()
3393 btrfs_put_block_group(cache); in btrfs_start_dirty_block_groups()
3398 * btrfs_wait_cache_io uses the cache->dirty_list to decide if in btrfs_start_dirty_block_groups()
3405 spin_lock(&cur_trans->dirty_bgs_lock); in btrfs_start_dirty_block_groups()
3406 list_del_init(&cache->dirty_list); in btrfs_start_dirty_block_groups()
3407 spin_unlock(&cur_trans->dirty_bgs_lock); in btrfs_start_dirty_block_groups()
3411 cache_save_setup(cache, trans, path); in btrfs_start_dirty_block_groups()
3413 if (cache->disk_cache_state == BTRFS_DC_SETUP) { in btrfs_start_dirty_block_groups()
3414 cache->io_ctl.inode = NULL; in btrfs_start_dirty_block_groups()
3415 ret = btrfs_write_out_cache(trans, cache, path); in btrfs_start_dirty_block_groups()
3416 if (ret == 0 && cache->io_ctl.inode) { in btrfs_start_dirty_block_groups()
3424 list_add_tail(&cache->io_list, io); in btrfs_start_dirty_block_groups()
3427 * If we failed to write the cache, the in btrfs_start_dirty_block_groups()
3434 ret = update_block_group_item(trans, path, cache); in btrfs_start_dirty_block_groups()
3436 * Our block group might still be attached to the list in btrfs_start_dirty_block_groups()
3437 * of new block groups in the transaction handle of some in btrfs_start_dirty_block_groups()
3438 * other task (struct btrfs_trans_handle->new_bgs). This in btrfs_start_dirty_block_groups()
3439 * means its block group item isn't yet in the extent in btrfs_start_dirty_block_groups()
3444 if (ret == -ENOENT) { in btrfs_start_dirty_block_groups()
3446 spin_lock(&cur_trans->dirty_bgs_lock); in btrfs_start_dirty_block_groups()
3447 if (list_empty(&cache->dirty_list)) { in btrfs_start_dirty_block_groups()
3448 list_add_tail(&cache->dirty_list, in btrfs_start_dirty_block_groups()
3449 &cur_trans->dirty_bgs); in btrfs_start_dirty_block_groups()
3450 btrfs_get_block_group(cache); in btrfs_start_dirty_block_groups()
3453 spin_unlock(&cur_trans->dirty_bgs_lock); in btrfs_start_dirty_block_groups()
3459 /* If it's not on the io list, we need to put the block group */ in btrfs_start_dirty_block_groups()
3461 btrfs_put_block_group(cache); in btrfs_start_dirty_block_groups()
3466 * us from writing caches for block groups that are going to be in btrfs_start_dirty_block_groups()
3469 mutex_unlock(&trans->transaction->cache_write_mutex); in btrfs_start_dirty_block_groups()
3472 mutex_lock(&trans->transaction->cache_write_mutex); in btrfs_start_dirty_block_groups()
3474 mutex_unlock(&trans->transaction->cache_write_mutex); in btrfs_start_dirty_block_groups()
3484 spin_lock(&cur_trans->dirty_bgs_lock); in btrfs_start_dirty_block_groups()
3485 list_splice_init(&cur_trans->dirty_bgs, &dirty); in btrfs_start_dirty_block_groups()
3487 * dirty_bgs_lock protects us from concurrent block group in btrfs_start_dirty_block_groups()
3491 spin_unlock(&cur_trans->dirty_bgs_lock); in btrfs_start_dirty_block_groups()
3494 spin_unlock(&cur_trans->dirty_bgs_lock); in btrfs_start_dirty_block_groups()
3498 spin_lock(&cur_trans->dirty_bgs_lock); in btrfs_start_dirty_block_groups()
3499 list_splice_init(&dirty, &cur_trans->dirty_bgs); in btrfs_start_dirty_block_groups()
3500 spin_unlock(&cur_trans->dirty_bgs_lock); in btrfs_start_dirty_block_groups()
3510 struct btrfs_fs_info *fs_info = trans->fs_info; in btrfs_write_dirty_block_groups()
3511 struct btrfs_block_group *cache; in btrfs_write_dirty_block_groups() local
3512 struct btrfs_transaction *cur_trans = trans->transaction; in btrfs_write_dirty_block_groups()
3516 struct list_head *io = &cur_trans->io_bgs; in btrfs_write_dirty_block_groups()
3520 return -ENOMEM; in btrfs_write_dirty_block_groups()
3525 * transaction's list of dirty block groups. These tasks correspond to in btrfs_write_dirty_block_groups()
3527 * space cache, which run inode.c:btrfs_finish_ordered_io(), and can in btrfs_write_dirty_block_groups()
3528 * allocate new block groups as a result of COWing nodes of the root in btrfs_write_dirty_block_groups()
3537 spin_lock(&cur_trans->dirty_bgs_lock); in btrfs_write_dirty_block_groups()
3538 while (!list_empty(&cur_trans->dirty_bgs)) { in btrfs_write_dirty_block_groups()
3539 cache = list_first_entry(&cur_trans->dirty_bgs, in btrfs_write_dirty_block_groups()
3544 * This can happen if cache_save_setup re-dirties a block group in btrfs_write_dirty_block_groups()
3548 if (!list_empty(&cache->io_list)) { in btrfs_write_dirty_block_groups()
3549 spin_unlock(&cur_trans->dirty_bgs_lock); in btrfs_write_dirty_block_groups()
3550 list_del_init(&cache->io_list); in btrfs_write_dirty_block_groups()
3551 btrfs_wait_cache_io(trans, cache, path); in btrfs_write_dirty_block_groups()
3552 btrfs_put_block_group(cache); in btrfs_write_dirty_block_groups()
3553 spin_lock(&cur_trans->dirty_bgs_lock); in btrfs_write_dirty_block_groups()
3560 list_del_init(&cache->dirty_list); in btrfs_write_dirty_block_groups()
3561 spin_unlock(&cur_trans->dirty_bgs_lock); in btrfs_write_dirty_block_groups()
3564 cache_save_setup(cache, trans, path); in btrfs_write_dirty_block_groups()
3569 if (!ret && cache->disk_cache_state == BTRFS_DC_SETUP) { in btrfs_write_dirty_block_groups()
3570 cache->io_ctl.inode = NULL; in btrfs_write_dirty_block_groups()
3571 ret = btrfs_write_out_cache(trans, cache, path); in btrfs_write_dirty_block_groups()
3572 if (ret == 0 && cache->io_ctl.inode) { in btrfs_write_dirty_block_groups()
3574 list_add_tail(&cache->io_list, io); in btrfs_write_dirty_block_groups()
3577 * If we failed to write the cache, the in btrfs_write_dirty_block_groups()
3584 ret = update_block_group_item(trans, path, cache); in btrfs_write_dirty_block_groups()
3587 * created a new block group while updating a free space in btrfs_write_dirty_block_groups()
3588 * cache's inode (at inode.c:btrfs_finish_ordered_io()) in btrfs_write_dirty_block_groups()
3590 * which case the new block group is still attached to in btrfs_write_dirty_block_groups()
3592 * finished yet (no block group item in the extent tree in btrfs_write_dirty_block_groups()
3598 if (ret == -ENOENT) { in btrfs_write_dirty_block_groups()
3599 wait_event(cur_trans->writer_wait, in btrfs_write_dirty_block_groups()
3600 atomic_read(&cur_trans->num_writers) == 1); in btrfs_write_dirty_block_groups()
3601 ret = update_block_group_item(trans, path, cache); in btrfs_write_dirty_block_groups()
3607 /* If its not on the io list, we need to put the block group */ in btrfs_write_dirty_block_groups()
3609 btrfs_put_block_group(cache); in btrfs_write_dirty_block_groups()
3611 spin_lock(&cur_trans->dirty_bgs_lock); in btrfs_write_dirty_block_groups()
3613 spin_unlock(&cur_trans->dirty_bgs_lock); in btrfs_write_dirty_block_groups()
3620 cache = list_first_entry(io, struct btrfs_block_group, in btrfs_write_dirty_block_groups()
3622 list_del_init(&cache->io_list); in btrfs_write_dirty_block_groups()
3623 btrfs_wait_cache_io(trans, cache, path); in btrfs_write_dirty_block_groups()
3624 btrfs_put_block_group(cache); in btrfs_write_dirty_block_groups()
3634 struct btrfs_fs_info *info = trans->fs_info; in btrfs_update_block_group()
3636 struct btrfs_block_group *cache; in btrfs_update_block_group() local
3642 /* Block accounting for super block */ in btrfs_update_block_group()
3643 spin_lock(&info->delalloc_root_lock); in btrfs_update_block_group()
3644 old_val = btrfs_super_bytes_used(info->super_copy); in btrfs_update_block_group()
3648 old_val -= num_bytes; in btrfs_update_block_group()
3649 btrfs_set_super_bytes_used(info->super_copy, old_val); in btrfs_update_block_group()
3650 spin_unlock(&info->delalloc_root_lock); in btrfs_update_block_group()
3652 cache = btrfs_lookup_block_group(info, bytenr); in btrfs_update_block_group()
3653 if (!cache) in btrfs_update_block_group()
3654 return -ENOENT; in btrfs_update_block_group()
3656 /* An extent can not span multiple block groups. */ in btrfs_update_block_group()
3657 ASSERT(bytenr + num_bytes <= cache->start + cache->length); in btrfs_update_block_group()
3659 space_info = cache->space_info; in btrfs_update_block_group()
3660 factor = btrfs_bg_type_to_factor(cache->flags); in btrfs_update_block_group()
3663 * If this block group has free space cache written out, we need to make in btrfs_update_block_group()
3665 * the unpinning stage to actually add the space back to the block group, in btrfs_update_block_group()
3668 if (!alloc && !btrfs_block_group_done(cache)) in btrfs_update_block_group()
3669 btrfs_cache_block_group(cache, true); in btrfs_update_block_group()
3671 spin_lock(&space_info->lock); in btrfs_update_block_group()
3672 spin_lock(&cache->lock); in btrfs_update_block_group()
3675 cache->disk_cache_state < BTRFS_DC_CLEAR) in btrfs_update_block_group()
3676 cache->disk_cache_state = BTRFS_DC_CLEAR; in btrfs_update_block_group()
3678 old_val = cache->used; in btrfs_update_block_group()
3681 cache->used = old_val; in btrfs_update_block_group()
3682 cache->reserved -= num_bytes; in btrfs_update_block_group()
3683 cache->reclaim_mark = 0; in btrfs_update_block_group()
3684 space_info->bytes_reserved -= num_bytes; in btrfs_update_block_group()
3685 space_info->bytes_used += num_bytes; in btrfs_update_block_group()
3686 space_info->disk_used += num_bytes * factor; in btrfs_update_block_group()
3687 if (READ_ONCE(space_info->periodic_reclaim)) in btrfs_update_block_group()
3688 btrfs_space_info_update_reclaimable(space_info, -num_bytes); in btrfs_update_block_group()
3689 spin_unlock(&cache->lock); in btrfs_update_block_group()
3690 spin_unlock(&space_info->lock); in btrfs_update_block_group()
3692 old_val -= num_bytes; in btrfs_update_block_group()
3693 cache->used = old_val; in btrfs_update_block_group()
3694 cache->pinned += num_bytes; in btrfs_update_block_group()
3696 space_info->bytes_used -= num_bytes; in btrfs_update_block_group()
3697 space_info->disk_used -= num_bytes * factor; in btrfs_update_block_group()
3698 if (READ_ONCE(space_info->periodic_reclaim)) in btrfs_update_block_group()
3701 reclaim = should_reclaim_block_group(cache, num_bytes); in btrfs_update_block_group()
3703 spin_unlock(&cache->lock); in btrfs_update_block_group()
3704 spin_unlock(&space_info->lock); in btrfs_update_block_group()
3706 set_extent_bit(&trans->transaction->pinned_extents, bytenr, in btrfs_update_block_group()
3707 bytenr + num_bytes - 1, EXTENT_DIRTY, NULL); in btrfs_update_block_group()
3710 spin_lock(&trans->transaction->dirty_bgs_lock); in btrfs_update_block_group()
3711 if (list_empty(&cache->dirty_list)) { in btrfs_update_block_group()
3712 list_add_tail(&cache->dirty_list, &trans->transaction->dirty_bgs); in btrfs_update_block_group()
3714 btrfs_get_block_group(cache); in btrfs_update_block_group()
3716 spin_unlock(&trans->transaction->dirty_bgs_lock); in btrfs_update_block_group()
3719 * No longer have used bytes in this block group, queue it for deletion. in btrfs_update_block_group()
3720 * We do this after adding the block group to the dirty list to avoid in btrfs_update_block_group()
3721 * races between cleaner kthread and space cache writeout. in btrfs_update_block_group()
3725 btrfs_mark_bg_unused(cache); in btrfs_update_block_group()
3727 btrfs_mark_bg_to_reclaim(cache); in btrfs_update_block_group()
3730 btrfs_put_block_group(cache); in btrfs_update_block_group()
3732 /* Modified block groups are accounted for in the delayed_refs_rsv. */ in btrfs_update_block_group()
3742 * @cache: The cache we are manipulating
3749 * reservation and the block group has become read only we cannot make the
3750 * reservation and return -EAGAIN, otherwise this function always succeeds.
3752 int btrfs_add_reserved_bytes(struct btrfs_block_group *cache, in btrfs_add_reserved_bytes() argument
3756 struct btrfs_space_info *space_info = cache->space_info; in btrfs_add_reserved_bytes()
3760 spin_lock(&space_info->lock); in btrfs_add_reserved_bytes()
3761 spin_lock(&cache->lock); in btrfs_add_reserved_bytes()
3762 if (cache->ro) { in btrfs_add_reserved_bytes()
3763 ret = -EAGAIN; in btrfs_add_reserved_bytes()
3767 if (btrfs_block_group_should_use_size_class(cache)) { in btrfs_add_reserved_bytes()
3769 ret = btrfs_use_block_group_size_class(cache, size_class, force_wrong_size_class); in btrfs_add_reserved_bytes()
3773 cache->reserved += num_bytes; in btrfs_add_reserved_bytes()
3774 space_info->bytes_reserved += num_bytes; in btrfs_add_reserved_bytes()
3775 trace_btrfs_space_reservation(cache->fs_info, "space_info", in btrfs_add_reserved_bytes()
3776 space_info->flags, num_bytes, 1); in btrfs_add_reserved_bytes()
3777 btrfs_space_info_update_bytes_may_use(space_info, -ram_bytes); in btrfs_add_reserved_bytes()
3779 cache->delalloc_bytes += num_bytes; in btrfs_add_reserved_bytes()
3786 btrfs_try_granting_tickets(cache->fs_info, space_info); in btrfs_add_reserved_bytes()
3788 spin_unlock(&cache->lock); in btrfs_add_reserved_bytes()
3789 spin_unlock(&space_info->lock); in btrfs_add_reserved_bytes()
3796 * @cache: The cache we are manipulating
3805 void btrfs_free_reserved_bytes(struct btrfs_block_group *cache, in btrfs_free_reserved_bytes() argument
3808 struct btrfs_space_info *space_info = cache->space_info; in btrfs_free_reserved_bytes()
3810 spin_lock(&space_info->lock); in btrfs_free_reserved_bytes()
3811 spin_lock(&cache->lock); in btrfs_free_reserved_bytes()
3812 if (cache->ro) in btrfs_free_reserved_bytes()
3813 space_info->bytes_readonly += num_bytes; in btrfs_free_reserved_bytes()
3814 else if (btrfs_is_zoned(cache->fs_info)) in btrfs_free_reserved_bytes()
3815 space_info->bytes_zone_unusable += num_bytes; in btrfs_free_reserved_bytes()
3816 cache->reserved -= num_bytes; in btrfs_free_reserved_bytes()
3817 space_info->bytes_reserved -= num_bytes; in btrfs_free_reserved_bytes()
3818 space_info->max_extent_size = 0; in btrfs_free_reserved_bytes()
3821 cache->delalloc_bytes -= num_bytes; in btrfs_free_reserved_bytes()
3822 spin_unlock(&cache->lock); in btrfs_free_reserved_bytes()
3824 btrfs_try_granting_tickets(cache->fs_info, space_info); in btrfs_free_reserved_bytes()
3825 spin_unlock(&space_info->lock); in btrfs_free_reserved_bytes()
3830 struct list_head *head = &info->space_info; in force_metadata_allocation()
3834 if (found->flags & BTRFS_BLOCK_GROUP_METADATA) in force_metadata_allocation()
3835 found->force_alloc = CHUNK_ALLOC_FORCE; in force_metadata_allocation()
3850 * about 1% of the FS size. in should_alloc_chunk()
3853 thresh = btrfs_super_total_bytes(fs_info->super_copy); in should_alloc_chunk()
3856 if (sinfo->total_bytes - bytes_used < thresh) in should_alloc_chunk()
3860 if (bytes_used + SZ_2M < mult_perc(sinfo->total_bytes, 80)) in should_alloc_chunk()
3867 u64 alloc_flags = btrfs_get_alloc_profile(trans->fs_info, type); in btrfs_force_chunk_alloc()
3881 * system block group if needed. in do_chunk_alloc()
3893 * Normally we are not expected to fail with -ENOSPC here, since we have in do_chunk_alloc()
3898 * existing system block groups have a profile which can not be used in do_chunk_alloc()
3905 * none of the block groups can be used for extent allocation since they in do_chunk_alloc()
3909 * block groups and check if they have a usable profile and enough space in do_chunk_alloc()
3910 * can be slow on very large filesystems, so we tolerate the -ENOSPC and in do_chunk_alloc()
3916 * block group to allocate from when we called check_system_chunk() in do_chunk_alloc()
3917 * above. However right after we called it, the only system block group in do_chunk_alloc()
3921 * handle and scrub uses the commit root to search for block groups; in do_chunk_alloc()
3923 * 3) We had one system block group with enough free space when we called in do_chunk_alloc()
3927 * block group (discard removes a free space entry, discards it, and in do_chunk_alloc()
3928 * then adds back the entry to the block group cache). in do_chunk_alloc()
3930 if (ret == -ENOSPC) { in do_chunk_alloc()
3931 const u64 sys_flags = btrfs_system_alloc_profile(trans->fs_info); in do_chunk_alloc()
3969 * 1) Phase 1 - through btrfs_chunk_alloc() we allocate device extents for
3970 * the chunk, the chunk mapping, create its block group and add the items
3971 * that belong in the chunk btree to it - more specifically, we need to
3974 * 2) Phase 2 - through btrfs_create_pending_block_groups(), we add the block
3980 * trigger chunk allocation and attempted to insert the new block group item
3991 * allocate a new block group (chunk) because the only one that had enough
3993 * device replace, block group reclaim thread, etc), so we can not use it
3998 * the filesystem was mounted in degraded mode, none of the existing block
4000 * profile (for e.g. mounting a 2 devices filesystem, where all block groups
4003 * example, it will trigger allocation of a new metadata block group with a
4009 * example, it does not find any free extent in any metadata block group,
4010 * therefore forced to try to allocate a new metadata block group.
4012 * meanwhile - this typically happens with tasks that don't reserve space
4021 * the only metadata block group that had free space (discard starts by
4022 * removing a free space entry from a block group, then does the discard
4024 * block group).
4027 * a seed device - we must create new metadata and system chunks without adding
4028 * any of the block group items to the chunk, extent and device btrees. If we
4030 * btrees, since all the chunks from the seed device are read-only.
4037 * superblock. This is easier to trigger if using a btree node/leaf size of 64K
4045 * btrfs_reserve_chunk_metadata() - the former is used when allocating a data or
4047 * a modification to the chunk btree - use cases for the later are adding,
4053 * holding fs_info->chunk_mutex. This is important to guarantee that while COWing
4059 * that mutex. The same logic applies to removing chunks - we must reserve system
4061 * while holding fs_info->chunk_mutex.
4066 * - return 1 if it successfully allocates a chunk,
4067 * - return errors including -ENOSPC otherwise.
4069 * - return 0 if it doesn't need to allocate a new chunk,
4070 * - return 1 if it successfully allocates a chunk,
4071 * - return errors including -ENOSPC otherwise.
4076 struct btrfs_fs_info *fs_info = trans->fs_info; in btrfs_chunk_alloc()
4089 /* Don't re-enter if we're already allocating a chunk */ in btrfs_chunk_alloc()
4090 if (trans->allocating_chunk) in btrfs_chunk_alloc()
4091 return -ENOSPC; in btrfs_chunk_alloc()
4101 * lock on it and on its parent - if the COW operation triggers a system in btrfs_chunk_alloc()
4110 * here - this happens in the cases described above at do_chunk_alloc(). in btrfs_chunk_alloc()
4114 return -ENOSPC; in btrfs_chunk_alloc()
4120 spin_lock(&space_info->lock); in btrfs_chunk_alloc()
4121 if (force < space_info->force_alloc) in btrfs_chunk_alloc()
4122 force = space_info->force_alloc; in btrfs_chunk_alloc()
4124 if (space_info->full) { in btrfs_chunk_alloc()
4127 ret = -ENOSPC; in btrfs_chunk_alloc()
4130 spin_unlock(&space_info->lock); in btrfs_chunk_alloc()
4133 spin_unlock(&space_info->lock); in btrfs_chunk_alloc()
4135 } else if (space_info->chunk_alloc) { in btrfs_chunk_alloc()
4137 * Someone is already allocating, so we need to block in btrfs_chunk_alloc()
4144 spin_unlock(&space_info->lock); in btrfs_chunk_alloc()
4145 mutex_lock(&fs_info->chunk_mutex); in btrfs_chunk_alloc()
4146 mutex_unlock(&fs_info->chunk_mutex); in btrfs_chunk_alloc()
4149 space_info->chunk_alloc = 1; in btrfs_chunk_alloc()
4151 spin_unlock(&space_info->lock); in btrfs_chunk_alloc()
4157 mutex_lock(&fs_info->chunk_mutex); in btrfs_chunk_alloc()
4158 trans->allocating_chunk = true; in btrfs_chunk_alloc()
4172 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) { in btrfs_chunk_alloc()
4173 fs_info->data_chunk_allocations++; in btrfs_chunk_alloc()
4174 if (!(fs_info->data_chunk_allocations % in btrfs_chunk_alloc()
4175 fs_info->metadata_ratio)) in btrfs_chunk_alloc()
4180 trans->allocating_chunk = false; in btrfs_chunk_alloc()
4186 * New block group is likely to be used soon. Try to activate in btrfs_chunk_alloc()
4195 spin_lock(&space_info->lock); in btrfs_chunk_alloc()
4197 if (ret == -ENOSPC) in btrfs_chunk_alloc()
4198 space_info->full = 1; in btrfs_chunk_alloc()
4203 space_info->max_extent_size = 0; in btrfs_chunk_alloc()
4206 space_info->force_alloc = CHUNK_ALLOC_NO_FORCE; in btrfs_chunk_alloc()
4208 space_info->chunk_alloc = 0; in btrfs_chunk_alloc()
4209 spin_unlock(&space_info->lock); in btrfs_chunk_alloc()
4210 mutex_unlock(&fs_info->chunk_mutex); in btrfs_chunk_alloc()
4221 num_dev = fs_info->fs_devices->rw_devices; in get_profile_num_devs()
4230 struct btrfs_fs_info *fs_info = trans->fs_info; in reserve_chunk_space()
4237 * atomic and race free space reservation in the chunk block reserve. in reserve_chunk_space()
4239 lockdep_assert_held(&fs_info->chunk_mutex); in reserve_chunk_space()
4242 spin_lock(&info->lock); in reserve_chunk_space()
4243 left = info->total_bytes - btrfs_space_info_used(info, true); in reserve_chunk_space()
4244 spin_unlock(&info->lock); in reserve_chunk_space()
4279 * the cases described at do_chunk_alloc() - the system in reserve_chunk_space()
4280 * block group we just created was just turned into RO in reserve_chunk_space()
4290 &fs_info->chunk_block_rsv, in reserve_chunk_space()
4293 trans->chunk_bytes_reserved += bytes; in reserve_chunk_space()
4299 * The caller must be holding fs_info->chunk_mutex.
4303 struct btrfs_fs_info *fs_info = trans->fs_info; in check_system_chunk()
4324 * block group allocation and removal, to avoid a deadlock with a concurrent
4325 * task that is allocating a metadata or data block group and therefore needs to
4333 struct btrfs_fs_info *fs_info = trans->fs_info; in btrfs_reserve_chunk_metadata()
4341 mutex_lock(&fs_info->chunk_mutex); in btrfs_reserve_chunk_metadata()
4343 mutex_unlock(&fs_info->chunk_mutex); in btrfs_reserve_chunk_metadata()
4353 spin_lock(&block_group->lock); in btrfs_put_block_group_cache()
4355 &block_group->runtime_flags)) { in btrfs_put_block_group_cache()
4356 struct btrfs_inode *inode = block_group->inode; in btrfs_put_block_group_cache()
4358 block_group->inode = NULL; in btrfs_put_block_group_cache()
4359 spin_unlock(&block_group->lock); in btrfs_put_block_group_cache()
4361 ASSERT(block_group->io_ctl.inode == NULL); in btrfs_put_block_group_cache()
4362 iput(&inode->vfs_inode); in btrfs_put_block_group_cache()
4364 spin_unlock(&block_group->lock); in btrfs_put_block_group_cache()
4371 * Must be called only after stopping all workers, since we could have block
4373 * freed the block groups before stopping them.
4383 if (info->active_meta_bg) { in btrfs_free_block_groups()
4384 btrfs_put_block_group(info->active_meta_bg); in btrfs_free_block_groups()
4385 info->active_meta_bg = NULL; in btrfs_free_block_groups()
4387 if (info->active_system_bg) { in btrfs_free_block_groups()
4388 btrfs_put_block_group(info->active_system_bg); in btrfs_free_block_groups()
4389 info->active_system_bg = NULL; in btrfs_free_block_groups()
4393 write_lock(&info->block_group_cache_lock); in btrfs_free_block_groups()
4394 while (!list_empty(&info->caching_block_groups)) { in btrfs_free_block_groups()
4395 caching_ctl = list_entry(info->caching_block_groups.next, in btrfs_free_block_groups()
4397 list_del(&caching_ctl->list); in btrfs_free_block_groups()
4400 write_unlock(&info->block_group_cache_lock); in btrfs_free_block_groups()
4402 spin_lock(&info->unused_bgs_lock); in btrfs_free_block_groups()
4403 while (!list_empty(&info->unused_bgs)) { in btrfs_free_block_groups()
4404 block_group = list_first_entry(&info->unused_bgs, in btrfs_free_block_groups()
4407 list_del_init(&block_group->bg_list); in btrfs_free_block_groups()
4411 while (!list_empty(&info->reclaim_bgs)) { in btrfs_free_block_groups()
4412 block_group = list_first_entry(&info->reclaim_bgs, in btrfs_free_block_groups()
4415 list_del_init(&block_group->bg_list); in btrfs_free_block_groups()
4418 spin_unlock(&info->unused_bgs_lock); in btrfs_free_block_groups()
4420 spin_lock(&info->zone_active_bgs_lock); in btrfs_free_block_groups()
4421 while (!list_empty(&info->zone_active_bgs)) { in btrfs_free_block_groups()
4422 block_group = list_first_entry(&info->zone_active_bgs, in btrfs_free_block_groups()
4425 list_del_init(&block_group->active_bg_list); in btrfs_free_block_groups()
4428 spin_unlock(&info->zone_active_bgs_lock); in btrfs_free_block_groups()
4430 write_lock(&info->block_group_cache_lock); in btrfs_free_block_groups()
4431 while ((n = rb_last(&info->block_group_cache_tree.rb_root)) != NULL) { in btrfs_free_block_groups()
4434 rb_erase_cached(&block_group->cache_node, in btrfs_free_block_groups()
4435 &info->block_group_cache_tree); in btrfs_free_block_groups()
4436 RB_CLEAR_NODE(&block_group->cache_node); in btrfs_free_block_groups()
4437 write_unlock(&info->block_group_cache_lock); in btrfs_free_block_groups()
4439 down_write(&block_group->space_info->groups_sem); in btrfs_free_block_groups()
4440 list_del(&block_group->list); in btrfs_free_block_groups()
4441 up_write(&block_group->space_info->groups_sem); in btrfs_free_block_groups()
4444 * We haven't cached this block group, which means we could in btrfs_free_block_groups()
4445 * possibly have excluded extents on this block group. in btrfs_free_block_groups()
4447 if (block_group->cached == BTRFS_CACHE_NO || in btrfs_free_block_groups()
4448 block_group->cached == BTRFS_CACHE_ERROR) in btrfs_free_block_groups()
4452 ASSERT(block_group->cached != BTRFS_CACHE_STARTED); in btrfs_free_block_groups()
4453 ASSERT(list_empty(&block_group->dirty_list)); in btrfs_free_block_groups()
4454 ASSERT(list_empty(&block_group->io_list)); in btrfs_free_block_groups()
4455 ASSERT(list_empty(&block_group->bg_list)); in btrfs_free_block_groups()
4456 ASSERT(refcount_read(&block_group->refs) == 1); in btrfs_free_block_groups()
4457 ASSERT(block_group->swap_extents == 0); in btrfs_free_block_groups()
4460 write_lock(&info->block_group_cache_lock); in btrfs_free_block_groups()
4462 write_unlock(&info->block_group_cache_lock); in btrfs_free_block_groups()
4466 while (!list_empty(&info->space_info)) { in btrfs_free_block_groups()
4467 space_info = list_entry(info->space_info.next, in btrfs_free_block_groups()
4475 if (WARN_ON(space_info->bytes_pinned > 0 || in btrfs_free_block_groups()
4476 space_info->bytes_may_use > 0)) in btrfs_free_block_groups()
4486 if (!(space_info->flags & BTRFS_BLOCK_GROUP_METADATA) || in btrfs_free_block_groups()
4488 if (WARN_ON(space_info->bytes_reserved > 0)) in btrfs_free_block_groups()
4492 WARN_ON(space_info->reclaim_size > 0); in btrfs_free_block_groups()
4493 list_del(&space_info->list); in btrfs_free_block_groups()
4499 void btrfs_freeze_block_group(struct btrfs_block_group *cache) in btrfs_freeze_block_group() argument
4501 atomic_inc(&cache->frozen); in btrfs_freeze_block_group()
4506 struct btrfs_fs_info *fs_info = block_group->fs_info; in btrfs_unfreeze_block_group()
4509 spin_lock(&block_group->lock); in btrfs_unfreeze_block_group()
4510 cleanup = (atomic_dec_and_test(&block_group->frozen) && in btrfs_unfreeze_block_group()
4511 test_bit(BLOCK_GROUP_FLAG_REMOVED, &block_group->runtime_flags)); in btrfs_unfreeze_block_group()
4512 spin_unlock(&block_group->lock); in btrfs_unfreeze_block_group()
4517 map = btrfs_find_chunk_map(fs_info, block_group->start, 1); in btrfs_unfreeze_block_group()
4528 * tasks trimming this block group have left 1 entry each one. in btrfs_unfreeze_block_group()
4539 spin_lock(&bg->lock); in btrfs_inc_block_group_swap_extents()
4540 if (bg->ro) in btrfs_inc_block_group_swap_extents()
4543 bg->swap_extents++; in btrfs_inc_block_group_swap_extents()
4544 spin_unlock(&bg->lock); in btrfs_inc_block_group_swap_extents()
4551 spin_lock(&bg->lock); in btrfs_dec_block_group_swap_extents()
4552 ASSERT(!bg->ro); in btrfs_dec_block_group_swap_extents()
4553 ASSERT(bg->swap_extents >= amount); in btrfs_dec_block_group_swap_extents()
4554 bg->swap_extents -= amount; in btrfs_dec_block_group_swap_extents()
4555 spin_unlock(&bg->lock); in btrfs_dec_block_group_swap_extents()
4558 enum btrfs_block_group_size_class btrfs_calc_block_group_size_class(u64 size) in btrfs_calc_block_group_size_class() argument
4560 if (size <= SZ_128K) in btrfs_calc_block_group_size_class()
4562 if (size <= SZ_8M) in btrfs_calc_block_group_size_class()
4568 * Handle a block group allocating an extent in a size class
4570 * @bg: The block group we allocated in.
4571 * @size_class: The size class of the allocation.
4573 * mismatched size classes.
4575 * Returns: 0 if the size class was valid for this block_group, -EAGAIN in the
4576 * case of a race that leads to the wrong size class without
4579 * find_free_extent will skip block groups with a mismatched size class until
4581 * force_wrong_size_class. However, if a block group is newly allocated and
4582 * doesn't yet have a size class, then it is possible for two allocations of
4592 /* The new allocation is in the right size class, do nothing */ in btrfs_use_block_group_size_class()
4593 if (bg->size_class == size_class) in btrfs_use_block_group_size_class()
4596 * The new allocation is in a mismatched size class. in btrfs_use_block_group_size_class()
4605 if (bg->size_class != BTRFS_BG_SZ_NONE) { in btrfs_use_block_group_size_class()
4608 return -EAGAIN; in btrfs_use_block_group_size_class()
4611 * The happy new block group case: the new allocation is the first in btrfs_use_block_group_size_class()
4614 bg->size_class = size_class; in btrfs_use_block_group_size_class()
4621 if (btrfs_is_zoned(bg->fs_info)) in btrfs_block_group_should_use_size_class()