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

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
196 struct btrfs_fs_info *fs_info = block_group->fs_info; in btrfs_add_block_group_cache()
200 ASSERT(block_group->length != 0); in btrfs_add_block_group_cache()
202 write_lock(&fs_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 &fs_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(&fs_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
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()
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()
551 size = end - start; 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()
633 * Best effort attempt to compute a block group's size class while caching it.
635 * @block_group: the block group we are caching
642 * them at even steps through the block group and pick the smallest size class
648 * If we are caching in a block group from disk, then there are three major cases
650 * 1. the block group is well behaved and all extents in it are the same size
652 * 2. the block group is mostly one size class with rare exceptions for last
654 * 3. the block group was populated before size classes and can have a totally
657 * In case 1, looking at any extent in the block group will yield the correct
669 struct btrfs_fs_info *fs_info = block_group->fs_info; in load_block_group_size_class()
672 u64 min_size = block_group->length; in load_block_group_size_class()
679 lockdep_assert_held(&caching_ctl->mutex); in load_block_group_size_class()
680 lockdep_assert_held_read(&fs_info->commit_root_sem); in load_block_group_size_class()
691 spin_lock(&block_group->lock); in load_block_group_size_class()
692 block_group->size_class = size_class; in load_block_group_size_class()
693 spin_unlock(&block_group->lock); in load_block_group_size_class()
701 struct btrfs_block_group *block_group = caching_ctl->block_group; in load_extent_tree_free()
702 struct btrfs_fs_info *fs_info = block_group->fs_info; in load_extent_tree_free()
715 return -ENOMEM; in load_extent_tree_free()
717 last = max_t(u64, block_group->start, BTRFS_SUPER_INFO_OFFSET); in load_extent_tree_free()
723 * allocate from this block group until we've had a chance to fragment in load_extent_tree_free()
733 * root, since its read-only in load_extent_tree_free()
735 path->skip_locking = 1; in load_extent_tree_free()
736 path->search_commit_root = 1; in load_extent_tree_free()
737 path->reada = READA_FORWARD; in load_extent_tree_free()
748 leaf = path->nodes[0]; in load_extent_tree_free()
753 last = (u64)-1; in load_extent_tree_free()
757 if (path->slots[0] < nritems) { in load_extent_tree_free()
758 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); in load_extent_tree_free()
765 rwsem_is_contended(&fs_info->commit_root_sem)) { in load_extent_tree_free()
767 up_read(&fs_info->commit_root_sem); in load_extent_tree_free()
768 mutex_unlock(&caching_ctl->mutex); in load_extent_tree_free()
770 mutex_lock(&caching_ctl->mutex); in load_extent_tree_free()
771 down_read(&fs_info->commit_root_sem); in load_extent_tree_free()
780 leaf = path->nodes[0]; in load_extent_tree_free()
793 if (key.objectid < block_group->start) { in load_extent_tree_free()
794 path->slots[0]++; in load_extent_tree_free()
798 if (key.objectid >= block_group->start + block_group->length) in load_extent_tree_free()
812 fs_info->nodesize; in load_extent_tree_free()
819 atomic_inc(&caching_ctl->progress); in load_extent_tree_free()
820 wake_up(&caching_ctl->wait); in load_extent_tree_free()
824 path->slots[0]++; in load_extent_tree_free()
828 block_group->start + block_group->length, in load_extent_tree_free()
837 clear_extent_bits(&bg->fs_info->excluded_extents, bg->start, in btrfs_free_excluded_extents()
838 bg->start + bg->length - 1, EXTENT_UPTODATE); in btrfs_free_excluded_extents()
849 block_group = caching_ctl->block_group; in caching_thread()
850 fs_info = block_group->fs_info; in caching_thread()
852 mutex_lock(&caching_ctl->mutex); in caching_thread()
853 down_read(&fs_info->commit_root_sem); in caching_thread()
864 * We failed to load the space cache, set ourselves to in caching_thread()
867 spin_lock(&block_group->lock); in caching_thread()
868 block_group->cached = BTRFS_CACHE_STARTED; in caching_thread()
869 spin_unlock(&block_group->lock); in caching_thread()
870 wake_up(&caching_ctl->wait); in caching_thread()
875 * can't actually cache from the free space tree as our commit root and in caching_thread()
881 !(test_bit(BTRFS_FS_FREE_SPACE_TREE_UNTRUSTED, &fs_info->flags))) in caching_thread()
886 spin_lock(&block_group->lock); in caching_thread()
887 block_group->caching_ctl = NULL; in caching_thread()
888 block_group->cached = ret ? BTRFS_CACHE_ERROR : BTRFS_CACHE_FINISHED; in caching_thread()
889 spin_unlock(&block_group->lock); in caching_thread()
895 spin_lock(&block_group->space_info->lock); in caching_thread()
896 spin_lock(&block_group->lock); in caching_thread()
897 bytes_used = block_group->length - block_group->used; in caching_thread()
898 block_group->space_info->bytes_used += bytes_used >> 1; in caching_thread()
899 spin_unlock(&block_group->lock); in caching_thread()
900 spin_unlock(&block_group->space_info->lock); in caching_thread()
905 up_read(&fs_info->commit_root_sem); in caching_thread()
907 mutex_unlock(&caching_ctl->mutex); in caching_thread()
909 wake_up(&caching_ctl->wait); in caching_thread()
915 int btrfs_cache_block_group(struct btrfs_block_group *cache, bool wait) in btrfs_cache_block_group() argument
917 struct btrfs_fs_info *fs_info = cache->fs_info; in btrfs_cache_block_group()
921 /* Allocator for zoned filesystems does not use the cache at all */ in btrfs_cache_block_group()
927 return -ENOMEM; in btrfs_cache_block_group()
929 INIT_LIST_HEAD(&caching_ctl->list); in btrfs_cache_block_group()
930 mutex_init(&caching_ctl->mutex); in btrfs_cache_block_group()
931 init_waitqueue_head(&caching_ctl->wait); in btrfs_cache_block_group()
932 caching_ctl->block_group = cache; in btrfs_cache_block_group()
933 refcount_set(&caching_ctl->count, 2); in btrfs_cache_block_group()
934 atomic_set(&caching_ctl->progress, 0); in btrfs_cache_block_group()
935 btrfs_init_work(&caching_ctl->work, caching_thread, NULL); in btrfs_cache_block_group()
937 spin_lock(&cache->lock); in btrfs_cache_block_group()
938 if (cache->cached != BTRFS_CACHE_NO) { in btrfs_cache_block_group()
941 caching_ctl = cache->caching_ctl; in btrfs_cache_block_group()
943 refcount_inc(&caching_ctl->count); in btrfs_cache_block_group()
944 spin_unlock(&cache->lock); in btrfs_cache_block_group()
947 WARN_ON(cache->caching_ctl); in btrfs_cache_block_group()
948 cache->caching_ctl = caching_ctl; in btrfs_cache_block_group()
949 cache->cached = BTRFS_CACHE_STARTED; in btrfs_cache_block_group()
950 spin_unlock(&cache->lock); in btrfs_cache_block_group()
952 write_lock(&fs_info->block_group_cache_lock); in btrfs_cache_block_group()
953 refcount_inc(&caching_ctl->count); in btrfs_cache_block_group()
954 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups); in btrfs_cache_block_group()
955 write_unlock(&fs_info->block_group_cache_lock); in btrfs_cache_block_group()
957 btrfs_get_block_group(cache); in btrfs_cache_block_group()
959 btrfs_queue_work(fs_info->caching_workers, &caching_ctl->work); in btrfs_cache_block_group()
962 ret = btrfs_caching_ctl_wait_done(cache, caching_ctl); in btrfs_cache_block_group()
974 write_seqlock(&fs_info->profiles_lock); in clear_avail_alloc_bits()
976 fs_info->avail_data_alloc_bits &= ~extra_flags; in clear_avail_alloc_bits()
978 fs_info->avail_metadata_alloc_bits &= ~extra_flags; in clear_avail_alloc_bits()
980 fs_info->avail_system_alloc_bits &= ~extra_flags; in clear_avail_alloc_bits()
981 write_sequnlock(&fs_info->profiles_lock); in clear_avail_alloc_bits()
987 * - RAID56 - in case there's neither RAID5 nor RAID6 profile block group
990 * - RAID1C34 - same as above for RAID1C3 and RAID1C4 block groups
1000 struct list_head *head = &fs_info->space_info; in clear_incompat_bg_bits()
1004 down_read(&sinfo->groups_sem); in clear_incompat_bg_bits()
1005 if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID5])) in clear_incompat_bg_bits()
1007 if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID6])) in clear_incompat_bg_bits()
1009 if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID1C3])) in clear_incompat_bg_bits()
1011 if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID1C4])) in clear_incompat_bg_bits()
1013 up_read(&sinfo->groups_sem); in clear_incompat_bg_bits()
1025 return fs_info->block_group_root; in btrfs_block_group_root()
1033 struct btrfs_fs_info *fs_info = trans->fs_info; in remove_block_group_item()
1039 key.objectid = block_group->start; in remove_block_group_item()
1041 key.offset = block_group->length; in remove_block_group_item()
1043 ret = btrfs_search_slot(trans, root, &key, path, -1, 1); in remove_block_group_item()
1045 ret = -ENOENT; in remove_block_group_item()
1056 struct btrfs_fs_info *fs_info = trans->fs_info; in btrfs_remove_block_group()
1069 block_group = btrfs_lookup_block_group(fs_info, map->start); in btrfs_remove_block_group()
1071 return -ENOENT; in btrfs_remove_block_group()
1073 BUG_ON(!block_group->ro); in btrfs_remove_block_group()
1077 * Free the reserved super bytes from this block group before in btrfs_remove_block_group()
1081 btrfs_free_ref_tree_range(fs_info, block_group->start, in btrfs_remove_block_group()
1082 block_group->length); in btrfs_remove_block_group()
1084 index = btrfs_bg_flags_to_raid_index(block_group->flags); in btrfs_remove_block_group()
1085 factor = btrfs_bg_type_to_factor(block_group->flags); in btrfs_remove_block_group()
1087 /* make sure this block group isn't part of an allocation cluster */ in btrfs_remove_block_group()
1088 cluster = &fs_info->data_alloc_cluster; in btrfs_remove_block_group()
1089 spin_lock(&cluster->refill_lock); in btrfs_remove_block_group()
1091 spin_unlock(&cluster->refill_lock); in btrfs_remove_block_group()
1094 * make sure this block group isn't part of a metadata in btrfs_remove_block_group()
1097 cluster = &fs_info->meta_alloc_cluster; in btrfs_remove_block_group()
1098 spin_lock(&cluster->refill_lock); in btrfs_remove_block_group()
1100 spin_unlock(&cluster->refill_lock); in btrfs_remove_block_group()
1107 ret = -ENOMEM; in btrfs_remove_block_group()
1117 mutex_lock(&trans->transaction->cache_write_mutex); in btrfs_remove_block_group()
1119 * Make sure our free space cache IO is done before removing the in btrfs_remove_block_group()
1122 spin_lock(&trans->transaction->dirty_bgs_lock); in btrfs_remove_block_group()
1123 if (!list_empty(&block_group->io_list)) { in btrfs_remove_block_group()
1124 list_del_init(&block_group->io_list); in btrfs_remove_block_group()
1126 WARN_ON(!IS_ERR(inode) && inode != block_group->io_ctl.inode); in btrfs_remove_block_group()
1128 spin_unlock(&trans->transaction->dirty_bgs_lock); in btrfs_remove_block_group()
1131 spin_lock(&trans->transaction->dirty_bgs_lock); in btrfs_remove_block_group()
1134 if (!list_empty(&block_group->dirty_list)) { in btrfs_remove_block_group()
1135 list_del_init(&block_group->dirty_list); in btrfs_remove_block_group()
1139 spin_unlock(&trans->transaction->dirty_bgs_lock); in btrfs_remove_block_group()
1140 mutex_unlock(&trans->transaction->cache_write_mutex); in btrfs_remove_block_group()
1146 write_lock(&fs_info->block_group_cache_lock); in btrfs_remove_block_group()
1147 rb_erase_cached(&block_group->cache_node, in btrfs_remove_block_group()
1148 &fs_info->block_group_cache_tree); in btrfs_remove_block_group()
1149 RB_CLEAR_NODE(&block_group->cache_node); in btrfs_remove_block_group()
1151 /* Once for the block groups rbtree */ in btrfs_remove_block_group()
1154 write_unlock(&fs_info->block_group_cache_lock); in btrfs_remove_block_group()
1156 down_write(&block_group->space_info->groups_sem); in btrfs_remove_block_group()
1161 list_del_init(&block_group->list); in btrfs_remove_block_group()
1162 if (list_empty(&block_group->space_info->block_groups[index])) { in btrfs_remove_block_group()
1163 kobj = block_group->space_info->block_group_kobjs[index]; in btrfs_remove_block_group()
1164 block_group->space_info->block_group_kobjs[index] = NULL; in btrfs_remove_block_group()
1165 clear_avail_alloc_bits(fs_info, block_group->flags); in btrfs_remove_block_group()
1167 up_write(&block_group->space_info->groups_sem); in btrfs_remove_block_group()
1168 clear_incompat_bg_bits(fs_info, block_group->flags); in btrfs_remove_block_group()
1174 if (block_group->cached == BTRFS_CACHE_STARTED) in btrfs_remove_block_group()
1177 write_lock(&fs_info->block_group_cache_lock); in btrfs_remove_block_group()
1182 list_for_each_entry(ctl, &fs_info->caching_block_groups, list) { in btrfs_remove_block_group()
1183 if (ctl->block_group == block_group) { in btrfs_remove_block_group()
1185 refcount_inc(&caching_ctl->count); in btrfs_remove_block_group()
1191 list_del_init(&caching_ctl->list); in btrfs_remove_block_group()
1192 write_unlock(&fs_info->block_group_cache_lock); in btrfs_remove_block_group()
1200 spin_lock(&trans->transaction->dirty_bgs_lock); in btrfs_remove_block_group()
1201 WARN_ON(!list_empty(&block_group->dirty_list)); in btrfs_remove_block_group()
1202 WARN_ON(!list_empty(&block_group->io_list)); in btrfs_remove_block_group()
1203 spin_unlock(&trans->transaction->dirty_bgs_lock); in btrfs_remove_block_group()
1207 spin_lock(&block_group->space_info->lock); in btrfs_remove_block_group()
1208 list_del_init(&block_group->ro_list); in btrfs_remove_block_group()
1211 WARN_ON(block_group->space_info->total_bytes in btrfs_remove_block_group()
1212 < block_group->length); in btrfs_remove_block_group()
1213 WARN_ON(block_group->space_info->bytes_readonly in btrfs_remove_block_group()
1214 < block_group->length - block_group->zone_unusable); in btrfs_remove_block_group()
1215 WARN_ON(block_group->space_info->bytes_zone_unusable in btrfs_remove_block_group()
1216 < block_group->zone_unusable); in btrfs_remove_block_group()
1217 WARN_ON(block_group->space_info->disk_total in btrfs_remove_block_group()
1218 < block_group->length * factor); in btrfs_remove_block_group()
1220 block_group->space_info->total_bytes -= block_group->length; in btrfs_remove_block_group()
1221 block_group->space_info->bytes_readonly -= in btrfs_remove_block_group()
1222 (block_group->length - block_group->zone_unusable); in btrfs_remove_block_group()
1223 btrfs_space_info_update_bytes_zone_unusable(block_group->space_info, in btrfs_remove_block_group()
1224 -block_group->zone_unusable); in btrfs_remove_block_group()
1225 block_group->space_info->disk_total -= block_group->length * factor; in btrfs_remove_block_group()
1227 spin_unlock(&block_group->space_info->lock); in btrfs_remove_block_group()
1230 * Remove the free space for the block group from the free space tree in btrfs_remove_block_group()
1231 * and the block group's item from the extent tree before marking the in btrfs_remove_block_group()
1232 * block group as removed. This is to prevent races with tasks that in btrfs_remove_block_group()
1233 * freeze and unfreeze a block group, this task and another task in btrfs_remove_block_group()
1234 * allocating a new block group - the unfreeze task ends up removing in btrfs_remove_block_group()
1235 * the block group's extent map before the task calling this function in btrfs_remove_block_group()
1236 * deletes the block group item from the extent tree, allowing for in btrfs_remove_block_group()
1237 * another task to attempt to create another block group with the same in btrfs_remove_block_group()
1238 * item key (and failing with -EEXIST and a transaction abort). in btrfs_remove_block_group()
1248 spin_lock(&block_group->lock); in btrfs_remove_block_group()
1249 set_bit(BLOCK_GROUP_FLAG_REMOVED, &block_group->runtime_flags); in btrfs_remove_block_group()
1252 * At this point trimming or scrub can't start on this block group, in btrfs_remove_block_group()
1253 * because we removed the block group from the rbtree in btrfs_remove_block_group()
1254 * fs_info->block_group_cache_tree so no one can't find it anymore and in btrfs_remove_block_group()
1255 * even if someone already got this block group before we removed it in btrfs_remove_block_group()
1256 * from the rbtree, they have already incremented block_group->frozen - in btrfs_remove_block_group()
1261 * And we must not remove the chunk map from the fs_info->mapping_tree in btrfs_remove_block_group()
1263 * ranges from being reused for a new block group. This is needed to in btrfs_remove_block_group()
1269 * allowing for new block groups to be created that can reuse the same in btrfs_remove_block_group()
1273 * is mounted with -odiscard. The same protections must remain in btrfs_remove_block_group()
1277 remove_map = (atomic_read(&block_group->frozen) == 0); in btrfs_remove_block_group()
1278 spin_unlock(&block_group->lock); in btrfs_remove_block_group()
1301 ASSERT(map->start == chunk_offset); in btrfs_start_trans_remove_block_group()
1305 * to remove a block group (done at btrfs_remove_chunk() and at in btrfs_start_trans_remove_block_group()
1310 * 1 unit for deleting the block group item (located in the extent in btrfs_start_trans_remove_block_group()
1317 * In order to remove a block group we also need to reserve units in the in btrfs_start_trans_remove_block_group()
1322 num_items = 3 + map->num_stripes; in btrfs_start_trans_remove_block_group()
1329 * Mark block group @cache read-only, so later write won't happen to block
1330 * group @cache.
1332 * If @force is not set, this function will only mark the block group readonly
1333 * if we have enough free space (1M) in other metadata/system block groups.
1334 * If @force is not set, this function will mark the block group readonly
1337 * NOTE: This function doesn't care if other block groups can contain all the
1338 * data in this block group. That check should be done by relocation routine,
1341 static int inc_block_group_ro(struct btrfs_block_group *cache, int force) in inc_block_group_ro() argument
1343 struct btrfs_space_info *sinfo = cache->space_info; in inc_block_group_ro()
1345 int ret = -ENOSPC; in inc_block_group_ro()
1347 spin_lock(&sinfo->lock); in inc_block_group_ro()
1348 spin_lock(&cache->lock); in inc_block_group_ro()
1350 if (cache->swap_extents) { in inc_block_group_ro()
1351 ret = -ETXTBSY; in inc_block_group_ro()
1355 if (cache->ro) { in inc_block_group_ro()
1356 cache->ro++; in inc_block_group_ro()
1361 num_bytes = cache->length - cache->reserved - cache->pinned - in inc_block_group_ro()
1362 cache->bytes_super - cache->zone_unusable - cache->used; in inc_block_group_ro()
1370 } else if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA) { in inc_block_group_ro()
1377 if (sinfo_used + num_bytes <= sinfo->total_bytes) in inc_block_group_ro()
1384 * leeway to allow us to mark this block group as read only. in inc_block_group_ro()
1386 if (btrfs_can_overcommit(cache->fs_info, sinfo, num_bytes, in inc_block_group_ro()
1392 sinfo->bytes_readonly += num_bytes; in inc_block_group_ro()
1393 if (btrfs_is_zoned(cache->fs_info)) { in inc_block_group_ro()
1395 sinfo->bytes_readonly += cache->zone_unusable; in inc_block_group_ro()
1396 btrfs_space_info_update_bytes_zone_unusable(sinfo, -cache->zone_unusable); in inc_block_group_ro()
1397 cache->zone_unusable = 0; in inc_block_group_ro()
1399 cache->ro++; in inc_block_group_ro()
1400 list_add_tail(&cache->ro_list, &sinfo->ro_bgs); in inc_block_group_ro()
1403 spin_unlock(&cache->lock); in inc_block_group_ro()
1404 spin_unlock(&sinfo->lock); in inc_block_group_ro()
1405 if (ret == -ENOSPC && btrfs_test_opt(cache->fs_info, ENOSPC_DEBUG)) { in inc_block_group_ro()
1406 btrfs_info(cache->fs_info, in inc_block_group_ro()
1407 "unable to make block group %llu ro", cache->start); in inc_block_group_ro()
1408 btrfs_dump_space_info(cache->fs_info, cache->space_info, 0, 0); in inc_block_group_ro()
1416 struct btrfs_fs_info *fs_info = trans->fs_info; in clean_pinned_extents()
1418 const u64 start = bg->start; in clean_pinned_extents()
1419 const u64 end = start + bg->length - 1; in clean_pinned_extents()
1422 spin_lock(&fs_info->trans_lock); in clean_pinned_extents()
1423 if (trans->transaction->list.prev != &fs_info->trans_list) { in clean_pinned_extents()
1424 prev_trans = list_last_entry(&trans->transaction->list, in clean_pinned_extents()
1426 refcount_inc(&prev_trans->use_count); in clean_pinned_extents()
1428 spin_unlock(&fs_info->trans_lock); in clean_pinned_extents()
1434 * transaction N - 1, and have seen a range belonging to the block in clean_pinned_extents()
1435 * group in pinned_extents before we were able to clear the whole block in clean_pinned_extents()
1437 * the block group after we unpinned it from pinned_extents and removed in clean_pinned_extents()
1440 mutex_lock(&fs_info->unused_bg_unpin_mutex); in clean_pinned_extents()
1442 ret = clear_extent_bits(&prev_trans->pinned_extents, start, end, in clean_pinned_extents()
1448 ret = clear_extent_bits(&trans->transaction->pinned_extents, start, end, in clean_pinned_extents()
1451 mutex_unlock(&fs_info->unused_bg_unpin_mutex); in clean_pinned_extents()
1471 struct btrfs_fs_info *fs_info = bg->fs_info; in btrfs_link_bg_list()
1474 spin_lock(&fs_info->unused_bgs_lock); in btrfs_link_bg_list()
1475 if (list_empty(&bg->bg_list)) { in btrfs_link_bg_list()
1477 list_add_tail(&bg->bg_list, list); in btrfs_link_bg_list()
1480 spin_unlock(&fs_info->unused_bgs_lock); in btrfs_link_bg_list()
1497 if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags)) in btrfs_delete_unused_bgs()
1507 if (!mutex_trylock(&fs_info->reclaim_bgs_lock)) in btrfs_delete_unused_bgs()
1510 spin_lock(&fs_info->unused_bgs_lock); in btrfs_delete_unused_bgs()
1511 while (!list_empty(&fs_info->unused_bgs)) { in btrfs_delete_unused_bgs()
1515 block_group = list_first_entry(&fs_info->unused_bgs, in btrfs_delete_unused_bgs()
1518 list_del_init(&block_group->bg_list); in btrfs_delete_unused_bgs()
1520 space_info = block_group->space_info; in btrfs_delete_unused_bgs()
1526 spin_unlock(&fs_info->unused_bgs_lock); in btrfs_delete_unused_bgs()
1528 btrfs_discard_cancel_work(&fs_info->discard_ctl, block_group); in btrfs_delete_unused_bgs()
1531 down_write(&space_info->groups_sem); in btrfs_delete_unused_bgs()
1534 * Async discard moves the final block group discard to be prior in btrfs_delete_unused_bgs()
1541 up_write(&space_info->groups_sem); in btrfs_delete_unused_bgs()
1543 btrfs_discard_queue_work(&fs_info->discard_ctl, in btrfs_delete_unused_bgs()
1548 spin_lock(&space_info->lock); in btrfs_delete_unused_bgs()
1549 spin_lock(&block_group->lock); in btrfs_delete_unused_bgs()
1550 if (btrfs_is_block_group_used(block_group) || block_group->ro || in btrfs_delete_unused_bgs()
1551 list_is_singular(&block_group->list)) { in btrfs_delete_unused_bgs()
1554 * outstanding allocations in this block group. We do in btrfs_delete_unused_bgs()
1556 * this block group. in btrfs_delete_unused_bgs()
1558 * Also bail out if this is the only block group for its in btrfs_delete_unused_bgs()
1560 * information from fs_info->avail_*_alloc_bits and the in btrfs_delete_unused_bgs()
1561 * next block group of this type would be created with a in btrfs_delete_unused_bgs()
1563 * fs_info->avail_*_alloc_bits would be 0. in btrfs_delete_unused_bgs()
1566 spin_unlock(&block_group->lock); in btrfs_delete_unused_bgs()
1567 spin_unlock(&space_info->lock); in btrfs_delete_unused_bgs()
1568 up_write(&space_info->groups_sem); in btrfs_delete_unused_bgs()
1573 * The block group may be unused but there may be space reserved in btrfs_delete_unused_bgs()
1574 * accounting with the existence of that block group, that is, in btrfs_delete_unused_bgs()
1575 * space_info->bytes_may_use was incremented by a task but no in btrfs_delete_unused_bgs()
1576 * space was yet allocated from the block group by the task. in btrfs_delete_unused_bgs()
1584 * of this block group is less than the used space of the in btrfs_delete_unused_bgs()
1585 * space_info - if that's the case, then it means we have tasks in btrfs_delete_unused_bgs()
1586 * that might be relying on the block group in order to allocate in btrfs_delete_unused_bgs()
1587 * extents, and add back the block group to the unused list when in btrfs_delete_unused_bgs()
1589 * needing to allocate extents from the block group. in btrfs_delete_unused_bgs()
1592 if (space_info->total_bytes - block_group->length < used && in btrfs_delete_unused_bgs()
1593 block_group->zone_unusable < block_group->length) { in btrfs_delete_unused_bgs()
1597 * fs_info->unused_bgs list. in btrfs_delete_unused_bgs()
1602 spin_unlock(&block_group->lock); in btrfs_delete_unused_bgs()
1603 spin_unlock(&space_info->lock); in btrfs_delete_unused_bgs()
1604 up_write(&space_info->groups_sem); in btrfs_delete_unused_bgs()
1608 spin_unlock(&block_group->lock); in btrfs_delete_unused_bgs()
1609 spin_unlock(&space_info->lock); in btrfs_delete_unused_bgs()
1613 up_write(&space_info->groups_sem); in btrfs_delete_unused_bgs()
1622 if (ret == -EAGAIN) in btrfs_delete_unused_bgs()
1632 block_group->start); in btrfs_delete_unused_bgs()
1640 * We could have pending pinned extents for this block group, in btrfs_delete_unused_bgs()
1655 spin_lock(&fs_info->discard_ctl.lock); in btrfs_delete_unused_bgs()
1656 if (!list_empty(&block_group->discard_list)) { in btrfs_delete_unused_bgs()
1657 spin_unlock(&fs_info->discard_ctl.lock); in btrfs_delete_unused_bgs()
1659 btrfs_discard_queue_work(&fs_info->discard_ctl, in btrfs_delete_unused_bgs()
1663 spin_unlock(&fs_info->discard_ctl.lock); in btrfs_delete_unused_bgs()
1666 spin_lock(&space_info->lock); in btrfs_delete_unused_bgs()
1667 spin_lock(&block_group->lock); in btrfs_delete_unused_bgs()
1669 btrfs_space_info_update_bytes_pinned(space_info, -block_group->pinned); in btrfs_delete_unused_bgs()
1670 space_info->bytes_readonly += block_group->pinned; in btrfs_delete_unused_bgs()
1671 block_group->pinned = 0; in btrfs_delete_unused_bgs()
1673 spin_unlock(&block_group->lock); in btrfs_delete_unused_bgs()
1674 spin_unlock(&space_info->lock); in btrfs_delete_unused_bgs()
1677 * The normal path here is an unused block group is passed here, in btrfs_delete_unused_bgs()
1680 * before coming down the unused block group path as trimming in btrfs_delete_unused_bgs()
1688 * need to reset sequential-required zones. in btrfs_delete_unused_bgs()
1701 ret = btrfs_remove_chunk(trans, block_group->start); in btrfs_delete_unused_bgs()
1710 * If we're not mounted with -odiscard, we can just forget in btrfs_delete_unused_bgs()
1711 * about this block group. Otherwise we'll need to wait in btrfs_delete_unused_bgs()
1715 spin_lock(&fs_info->unused_bgs_lock); in btrfs_delete_unused_bgs()
1718 * fs_info->unused_bgs, so use a list_move operation in btrfs_delete_unused_bgs()
1719 * to add the block group to the deleted_bgs list. in btrfs_delete_unused_bgs()
1721 list_move(&block_group->bg_list, in btrfs_delete_unused_bgs()
1722 &trans->transaction->deleted_bgs); in btrfs_delete_unused_bgs()
1723 spin_unlock(&fs_info->unused_bgs_lock); in btrfs_delete_unused_bgs()
1730 spin_lock(&fs_info->unused_bgs_lock); in btrfs_delete_unused_bgs()
1732 list_splice_tail(&retry_list, &fs_info->unused_bgs); in btrfs_delete_unused_bgs()
1733 spin_unlock(&fs_info->unused_bgs_lock); in btrfs_delete_unused_bgs()
1734 mutex_unlock(&fs_info->reclaim_bgs_lock); in btrfs_delete_unused_bgs()
1739 spin_lock(&fs_info->unused_bgs_lock); in btrfs_delete_unused_bgs()
1740 list_splice_tail(&retry_list, &fs_info->unused_bgs); in btrfs_delete_unused_bgs()
1741 spin_unlock(&fs_info->unused_bgs_lock); in btrfs_delete_unused_bgs()
1742 mutex_unlock(&fs_info->reclaim_bgs_lock); in btrfs_delete_unused_bgs()
1749 struct btrfs_fs_info *fs_info = bg->fs_info; in btrfs_mark_bg_unused()
1751 spin_lock(&fs_info->unused_bgs_lock); in btrfs_mark_bg_unused()
1752 if (list_empty(&bg->bg_list)) { in btrfs_mark_bg_unused()
1755 list_add_tail(&bg->bg_list, &fs_info->unused_bgs); in btrfs_mark_bg_unused()
1756 } else if (!test_bit(BLOCK_GROUP_FLAG_NEW, &bg->runtime_flags)) { in btrfs_mark_bg_unused()
1757 /* Pull out the block group from the reclaim_bgs list. */ in btrfs_mark_bg_unused()
1759 list_move_tail(&bg->bg_list, &fs_info->unused_bgs); in btrfs_mark_bg_unused()
1761 spin_unlock(&fs_info->unused_bgs_lock); in btrfs_mark_bg_unused()
1765 * We want block groups with a low number of used bytes to be in the beginning
1776 return bg1->used > bg2->used; in reclaim_bgs_cmp()
1788 const int thresh_pct = btrfs_calc_reclaim_threshold(bg->space_info); in should_reclaim_block_group()
1789 u64 thresh_bytes = mult_perc(bg->length, thresh_pct); in should_reclaim_block_group()
1790 const u64 new_val = bg->used; in should_reclaim_block_group()
1798 * brand new block group and we don't want to relocate new block groups. in should_reclaim_block_group()
1815 if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags)) in btrfs_reclaim_bgs_work()
1824 sb_start_write(fs_info->sb); in btrfs_reclaim_bgs_work()
1827 sb_end_write(fs_info->sb); in btrfs_reclaim_bgs_work()
1835 if (!mutex_trylock(&fs_info->reclaim_bgs_lock)) { in btrfs_reclaim_bgs_work()
1837 sb_end_write(fs_info->sb); in btrfs_reclaim_bgs_work()
1841 spin_lock(&fs_info->unused_bgs_lock); in btrfs_reclaim_bgs_work()
1844 * The block groups might still be in use and reachable via bg_list, in btrfs_reclaim_bgs_work()
1847 list_sort(NULL, &fs_info->reclaim_bgs, reclaim_bgs_cmp); in btrfs_reclaim_bgs_work()
1848 while (!list_empty(&fs_info->reclaim_bgs)) { in btrfs_reclaim_bgs_work()
1854 bg = list_first_entry(&fs_info->reclaim_bgs, in btrfs_reclaim_bgs_work()
1857 list_del_init(&bg->bg_list); in btrfs_reclaim_bgs_work()
1859 space_info = bg->space_info; in btrfs_reclaim_bgs_work()
1860 spin_unlock(&fs_info->unused_bgs_lock); in btrfs_reclaim_bgs_work()
1863 down_write(&space_info->groups_sem); in btrfs_reclaim_bgs_work()
1865 spin_lock(&space_info->lock); in btrfs_reclaim_bgs_work()
1866 spin_lock(&bg->lock); in btrfs_reclaim_bgs_work()
1867 if (bg->reserved || bg->pinned || bg->ro) { in btrfs_reclaim_bgs_work()
1870 * outstanding allocations in this block group. We do in btrfs_reclaim_bgs_work()
1872 * this block group. in btrfs_reclaim_bgs_work()
1874 spin_unlock(&bg->lock); in btrfs_reclaim_bgs_work()
1875 spin_unlock(&space_info->lock); in btrfs_reclaim_bgs_work()
1876 up_write(&space_info->groups_sem); in btrfs_reclaim_bgs_work()
1879 if (bg->used == 0) { in btrfs_reclaim_bgs_work()
1881 * It is possible that we trigger relocation on a block in btrfs_reclaim_bgs_work()
1887 * for the non-existent extents and running some extra in btrfs_reclaim_bgs_work()
1889 * other mechanisms for dealing with empty block groups. in btrfs_reclaim_bgs_work()
1893 spin_unlock(&bg->lock); in btrfs_reclaim_bgs_work()
1894 spin_unlock(&space_info->lock); in btrfs_reclaim_bgs_work()
1895 up_write(&space_info->groups_sem); in btrfs_reclaim_bgs_work()
1900 * The block group might no longer meet the reclaim condition by in btrfs_reclaim_bgs_work()
1909 if (!should_reclaim_block_group(bg, bg->length)) { in btrfs_reclaim_bgs_work()
1910 spin_unlock(&bg->lock); in btrfs_reclaim_bgs_work()
1911 spin_unlock(&space_info->lock); in btrfs_reclaim_bgs_work()
1912 up_write(&space_info->groups_sem); in btrfs_reclaim_bgs_work()
1917 * Cache the zone_unusable value before turning the block group in btrfs_reclaim_bgs_work()
1918 * to read only. As soon as the block group is read only it's in btrfs_reclaim_bgs_work()
1919 * zone_unusable value gets moved to the block group's read-only in btrfs_reclaim_bgs_work()
1921 * cache it before unlocking the block group, to prevent races in btrfs_reclaim_bgs_work()
1924 zone_unusable = bg->zone_unusable; in btrfs_reclaim_bgs_work()
1926 spin_unlock(&bg->lock); in btrfs_reclaim_bgs_work()
1927 spin_unlock(&space_info->lock); in btrfs_reclaim_bgs_work()
1930 * Get out fast, in case we're read-only or unmounting the in btrfs_reclaim_bgs_work()
1931 * filesystem. It is OK to drop block groups from the list even in btrfs_reclaim_bgs_work()
1932 * for the read-only case. As we did sb_start_write(), in btrfs_reclaim_bgs_work()
1933 * "mount -o remount,ro" won't happen and read-only filesystem in btrfs_reclaim_bgs_work()
1934 * means it is forced read-only due to a fatal error. So, it in btrfs_reclaim_bgs_work()
1935 * never gets back to read-write to let us reclaim again. in btrfs_reclaim_bgs_work()
1938 up_write(&space_info->groups_sem); in btrfs_reclaim_bgs_work()
1943 up_write(&space_info->groups_sem); in btrfs_reclaim_bgs_work()
1949 * "used" and "reserved" counters. We have set the block group in btrfs_reclaim_bgs_work()
1952 * not yet been done - btrfs_update_block_group() was not yet in btrfs_reclaim_bgs_work()
1954 * size from the "reserved" counter to the "used" counter - this in btrfs_reclaim_bgs_work()
1960 * it does the actual work to move extents out of the block in btrfs_reclaim_bgs_work()
1964 spin_lock(&bg->lock); in btrfs_reclaim_bgs_work()
1965 used = bg->used; in btrfs_reclaim_bgs_work()
1966 reserved = bg->reserved; in btrfs_reclaim_bgs_work()
1967 spin_unlock(&bg->lock); in btrfs_reclaim_bgs_work()
1971 bg->start, in btrfs_reclaim_bgs_work()
1972 div64_u64(used * 100, bg->length), in btrfs_reclaim_bgs_work()
1973 div64_u64(reserved * 100, bg->length), in btrfs_reclaim_bgs_work()
1974 div64_u64(zone_unusable * 100, bg->length)); in btrfs_reclaim_bgs_work()
1976 ret = btrfs_relocate_chunk(fs_info, bg->start); in btrfs_reclaim_bgs_work()
1980 bg->start); in btrfs_reclaim_bgs_work()
1983 spin_lock(&space_info->lock); in btrfs_reclaim_bgs_work()
1984 space_info->reclaim_errors++; in btrfs_reclaim_bgs_work()
1985 if (READ_ONCE(space_info->periodic_reclaim)) in btrfs_reclaim_bgs_work()
1986 space_info->periodic_reclaim_ready = false; in btrfs_reclaim_bgs_work()
1987 spin_unlock(&space_info->lock); in btrfs_reclaim_bgs_work()
1989 spin_lock(&space_info->lock); in btrfs_reclaim_bgs_work()
1990 space_info->reclaim_count++; in btrfs_reclaim_bgs_work()
1991 space_info->reclaim_bytes += used; in btrfs_reclaim_bgs_work()
1992 space_info->reclaim_bytes += reserved; in btrfs_reclaim_bgs_work()
1993 spin_unlock(&space_info->lock); in btrfs_reclaim_bgs_work()
1996 if (ret && !READ_ONCE(space_info->periodic_reclaim)) in btrfs_reclaim_bgs_work()
2000 mutex_unlock(&fs_info->reclaim_bgs_lock); in btrfs_reclaim_bgs_work()
2002 * Reclaiming all the block groups in the list can take really in btrfs_reclaim_bgs_work()
2003 * long. Prioritize cleaning up unused block groups. in btrfs_reclaim_bgs_work()
2010 if (!mutex_trylock(&fs_info->reclaim_bgs_lock)) in btrfs_reclaim_bgs_work()
2012 spin_lock(&fs_info->unused_bgs_lock); in btrfs_reclaim_bgs_work()
2014 spin_unlock(&fs_info->unused_bgs_lock); in btrfs_reclaim_bgs_work()
2015 mutex_unlock(&fs_info->reclaim_bgs_lock); in btrfs_reclaim_bgs_work()
2017 spin_lock(&fs_info->unused_bgs_lock); in btrfs_reclaim_bgs_work()
2018 list_splice_tail(&retry_list, &fs_info->reclaim_bgs); in btrfs_reclaim_bgs_work()
2019 spin_unlock(&fs_info->unused_bgs_lock); in btrfs_reclaim_bgs_work()
2021 sb_end_write(fs_info->sb); in btrfs_reclaim_bgs_work()
2027 spin_lock(&fs_info->unused_bgs_lock); in btrfs_reclaim_bgs()
2028 if (!list_empty(&fs_info->reclaim_bgs)) in btrfs_reclaim_bgs()
2029 queue_work(system_unbound_wq, &fs_info->reclaim_bgs_work); in btrfs_reclaim_bgs()
2030 spin_unlock(&fs_info->unused_bgs_lock); in btrfs_reclaim_bgs()
2035 struct btrfs_fs_info *fs_info = bg->fs_info; in btrfs_mark_bg_to_reclaim()
2037 if (btrfs_link_bg_list(bg, &fs_info->reclaim_bgs)) in btrfs_mark_bg_to_reclaim()
2051 slot = path->slots[0]; in read_bg_from_eb()
2052 leaf = path->nodes[0]; in read_bg_from_eb()
2054 map = btrfs_find_chunk_map(fs_info, key->objectid, key->offset); in read_bg_from_eb()
2058 key->objectid, key->offset); in read_bg_from_eb()
2059 return -ENOENT; in read_bg_from_eb()
2062 if (map->start != key->objectid || map->chunk_len != key->offset) { in read_bg_from_eb()
2064 "block group %llu len %llu mismatch with chunk %llu len %llu", in read_bg_from_eb()
2065 key->objectid, key->offset, map->start, map->chunk_len); in read_bg_from_eb()
2066 ret = -EUCLEAN; in read_bg_from_eb()
2075 if (flags != (map->type & BTRFS_BLOCK_GROUP_TYPE_MASK)) { in read_bg_from_eb()
2077 "block group %llu len %llu type flags 0x%llx mismatch with chunk type flags 0x%llx", in read_bg_from_eb()
2078 key->objectid, key->offset, flags, in read_bg_from_eb()
2079 (BTRFS_BLOCK_GROUP_TYPE_MASK & map->type)); in read_bg_from_eb()
2080 ret = -EUCLEAN; in read_bg_from_eb()
2097 if (found_key.objectid >= key->objectid && in find_first_block_group()
2110 write_seqlock(&fs_info->profiles_lock); in set_avail_alloc_bits()
2112 fs_info->avail_data_alloc_bits |= extra_flags; in set_avail_alloc_bits()
2114 fs_info->avail_metadata_alloc_bits |= extra_flags; in set_avail_alloc_bits()
2116 fs_info->avail_system_alloc_bits |= extra_flags; in set_avail_alloc_bits()
2117 write_sequnlock(&fs_info->profiles_lock); in set_avail_alloc_bits()
2124 * @chunk_start: logical address of block group
2128 * @stripe_len: size of IO stripe for the given block group
2131 * Used primarily to exclude those portions of a block group that contain super
2132 * block copies.
2147 return -EIO; in btrfs_rmap_block()
2149 data_stripe_length = map->stripe_size; in btrfs_rmap_block()
2151 chunk_start = map->start; in btrfs_rmap_block()
2154 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) in btrfs_rmap_block()
2157 buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS); in btrfs_rmap_block()
2159 ret = -ENOMEM; in btrfs_rmap_block()
2163 for (i = 0; i < map->num_stripes; i++) { in btrfs_rmap_block()
2169 if (!in_range(physical, map->stripes[i].physical, in btrfs_rmap_block()
2173 stripe_nr = (physical - map->stripes[i].physical) >> in btrfs_rmap_block()
2175 offset = (physical - map->stripes[i].physical) & in btrfs_rmap_block()
2178 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | in btrfs_rmap_block()
2180 stripe_nr = div_u64(stripe_nr * map->num_stripes + i, in btrfs_rmap_block()
2181 map->sub_stripes); in btrfs_rmap_block()
2185 * instead of map->stripe_len in btrfs_rmap_block()
2209 static int exclude_super_stripes(struct btrfs_block_group *cache) in exclude_super_stripes() argument
2211 struct btrfs_fs_info *fs_info = cache->fs_info; in exclude_super_stripes()
2218 if (cache->start < BTRFS_SUPER_INFO_OFFSET) { in exclude_super_stripes()
2219 stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->start; in exclude_super_stripes()
2220 cache->bytes_super += stripe_len; in exclude_super_stripes()
2221 ret = set_extent_bit(&fs_info->excluded_extents, cache->start, in exclude_super_stripes()
2222 cache->start + stripe_len - 1, in exclude_super_stripes()
2230 ret = btrfs_rmap_block(fs_info, cache->start, in exclude_super_stripes()
2239 "zoned: block group %llu must not contain super block", in exclude_super_stripes()
2240 cache->start); in exclude_super_stripes()
2241 return -EUCLEAN; in exclude_super_stripes()
2244 while (nr--) { in exclude_super_stripes()
2246 cache->start + cache->length - logical[nr]); in exclude_super_stripes()
2248 cache->bytes_super += len; in exclude_super_stripes()
2249 ret = set_extent_bit(&fs_info->excluded_extents, logical[nr], in exclude_super_stripes()
2250 logical[nr] + len - 1, in exclude_super_stripes()
2266 struct btrfs_block_group *cache; in btrfs_create_block_group_cache() local
2268 cache = kzalloc(sizeof(*cache), GFP_NOFS); in btrfs_create_block_group_cache()
2269 if (!cache) in btrfs_create_block_group_cache()
2272 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl), in btrfs_create_block_group_cache()
2274 if (!cache->free_space_ctl) { in btrfs_create_block_group_cache()
2275 kfree(cache); in btrfs_create_block_group_cache()
2279 cache->start = start; in btrfs_create_block_group_cache()
2281 cache->fs_info = fs_info; in btrfs_create_block_group_cache()
2282 cache->full_stripe_len = btrfs_full_stripe_len(fs_info, start); in btrfs_create_block_group_cache()
2284 cache->discard_index = BTRFS_DISCARD_INDEX_UNUSED; in btrfs_create_block_group_cache()
2286 refcount_set(&cache->refs, 1); in btrfs_create_block_group_cache()
2287 spin_lock_init(&cache->lock); in btrfs_create_block_group_cache()
2288 init_rwsem(&cache->data_rwsem); in btrfs_create_block_group_cache()
2289 INIT_LIST_HEAD(&cache->list); in btrfs_create_block_group_cache()
2290 INIT_LIST_HEAD(&cache->cluster_list); in btrfs_create_block_group_cache()
2291 INIT_LIST_HEAD(&cache->bg_list); in btrfs_create_block_group_cache()
2292 INIT_LIST_HEAD(&cache->ro_list); in btrfs_create_block_group_cache()
2293 INIT_LIST_HEAD(&cache->discard_list); in btrfs_create_block_group_cache()
2294 INIT_LIST_HEAD(&cache->dirty_list); in btrfs_create_block_group_cache()
2295 INIT_LIST_HEAD(&cache->io_list); in btrfs_create_block_group_cache()
2296 INIT_LIST_HEAD(&cache->active_bg_list); in btrfs_create_block_group_cache()
2297 btrfs_init_free_space_ctl(cache, cache->free_space_ctl); in btrfs_create_block_group_cache()
2298 atomic_set(&cache->frozen, 0); in btrfs_create_block_group_cache()
2299 mutex_init(&cache->free_space_lock); in btrfs_create_block_group_cache()
2301 return cache; in btrfs_create_block_group_cache()
2305 * Iterate all chunks and verify that each of them has the corresponding block
2326 bg = btrfs_lookup_block_group(fs_info, map->start); in check_chunk_block_group_mappings()
2329 "chunk start=%llu len=%llu doesn't have corresponding block group", in check_chunk_block_group_mappings()
2330 map->start, map->chunk_len); in check_chunk_block_group_mappings()
2331 ret = -EUCLEAN; in check_chunk_block_group_mappings()
2335 if (bg->start != map->start || bg->length != map->chunk_len || in check_chunk_block_group_mappings()
2336 (bg->flags & BTRFS_BLOCK_GROUP_TYPE_MASK) != in check_chunk_block_group_mappings()
2337 (map->type & BTRFS_BLOCK_GROUP_TYPE_MASK)) { in check_chunk_block_group_mappings()
2339 "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()
2340 map->start, map->chunk_len, in check_chunk_block_group_mappings()
2341 map->type & BTRFS_BLOCK_GROUP_TYPE_MASK, in check_chunk_block_group_mappings()
2342 bg->start, bg->length, in check_chunk_block_group_mappings()
2343 bg->flags & BTRFS_BLOCK_GROUP_TYPE_MASK); in check_chunk_block_group_mappings()
2344 ret = -EUCLEAN; in check_chunk_block_group_mappings()
2349 start = map->start + map->chunk_len; in check_chunk_block_group_mappings()
2361 struct btrfs_block_group *cache; in read_one_block_group() local
2365 ASSERT(key->type == BTRFS_BLOCK_GROUP_ITEM_KEY); in read_one_block_group()
2367 cache = btrfs_create_block_group_cache(info, key->objectid); in read_one_block_group()
2368 if (!cache) in read_one_block_group()
2369 return -ENOMEM; in read_one_block_group()
2371 cache->length = key->offset; in read_one_block_group()
2372 cache->used = btrfs_stack_block_group_used(bgi); in read_one_block_group()
2373 cache->commit_used = cache->used; in read_one_block_group()
2374 cache->flags = btrfs_stack_block_group_flags(bgi); in read_one_block_group()
2375 cache->global_root_id = btrfs_stack_block_group_chunk_objectid(bgi); in read_one_block_group()
2377 set_free_space_tree_thresholds(cache); in read_one_block_group()
2381 * When we mount with old space cache, we need to in read_one_block_group()
2385 * truncate the old free space cache inode and in read_one_block_group()
2388 * the new space cache info onto disk. in read_one_block_group()
2391 cache->disk_cache_state = BTRFS_DC_CLEAR; in read_one_block_group()
2393 if (!mixed && ((cache->flags & BTRFS_BLOCK_GROUP_METADATA) && in read_one_block_group()
2394 (cache->flags & BTRFS_BLOCK_GROUP_DATA))) { in read_one_block_group()
2396 "bg %llu is a mixed block group but filesystem hasn't enabled mixed block groups", in read_one_block_group()
2397 cache->start); in read_one_block_group()
2398 ret = -EINVAL; in read_one_block_group()
2402 ret = btrfs_load_block_group_zone_info(cache, false); in read_one_block_group()
2405 cache->start); in read_one_block_group()
2414 ret = exclude_super_stripes(cache); in read_one_block_group()
2417 btrfs_free_excluded_extents(cache); in read_one_block_group()
2423 * free space for a block group. So, we don't need any caching work. in read_one_block_group()
2434 btrfs_calc_zone_unusable(cache); in read_one_block_group()
2436 btrfs_free_excluded_extents(cache); in read_one_block_group()
2437 } else if (cache->length == cache->used) { in read_one_block_group()
2438 cache->cached = BTRFS_CACHE_FINISHED; in read_one_block_group()
2439 btrfs_free_excluded_extents(cache); in read_one_block_group()
2440 } else if (cache->used == 0) { in read_one_block_group()
2441 cache->cached = BTRFS_CACHE_FINISHED; in read_one_block_group()
2442 ret = btrfs_add_new_free_space(cache, cache->start, in read_one_block_group()
2443 cache->start + cache->length, NULL); in read_one_block_group()
2444 btrfs_free_excluded_extents(cache); in read_one_block_group()
2449 ret = btrfs_add_block_group_cache(cache); in read_one_block_group()
2451 btrfs_remove_free_space_cache(cache); in read_one_block_group()
2454 trace_btrfs_add_block_group(info, cache, 0); in read_one_block_group()
2455 btrfs_add_bg_to_space_info(info, cache); in read_one_block_group()
2457 set_avail_alloc_bits(info, cache->flags); in read_one_block_group()
2458 if (btrfs_chunk_writeable(info, cache->start)) { in read_one_block_group()
2459 if (cache->used == 0) { in read_one_block_group()
2460 ASSERT(list_empty(&cache->bg_list)); in read_one_block_group()
2462 btrfs_discard_queue_work(&info->discard_ctl, cache); in read_one_block_group()
2464 btrfs_mark_bg_unused(cache); in read_one_block_group()
2467 inc_block_group_ro(cache, 1); in read_one_block_group()
2472 btrfs_put_block_group(cache); in read_one_block_group()
2481 for (node = rb_first_cached(&fs_info->mapping_tree); node; node = rb_next(node)) { in fill_dummy_bgs()
2486 bg = btrfs_create_block_group_cache(fs_info, map->start); in fill_dummy_bgs()
2488 ret = -ENOMEM; in fill_dummy_bgs()
2492 /* Fill dummy cache as FULL */ in fill_dummy_bgs()
2493 bg->length = map->chunk_len; in fill_dummy_bgs()
2494 bg->flags = map->type; in fill_dummy_bgs()
2495 bg->cached = BTRFS_CACHE_FINISHED; in fill_dummy_bgs()
2496 bg->used = map->chunk_len; in fill_dummy_bgs()
2497 bg->flags = map->type; in fill_dummy_bgs()
2500 * We may have some valid block group cache added already, in in fill_dummy_bgs()
2503 if (ret == -EEXIST) { in fill_dummy_bgs()
2517 set_avail_alloc_bits(fs_info, bg->flags); in fill_dummy_bgs()
2529 struct btrfs_block_group *cache; in btrfs_read_block_groups() local
2537 * unsupported RO options. The fs can never be mounted read-write, so no in btrfs_read_block_groups()
2538 * need to waste time searching block group items. in btrfs_read_block_groups()
2543 if (!root || (btrfs_super_compat_ro_flags(info->super_copy) & in btrfs_read_block_groups()
2552 return -ENOMEM; in btrfs_read_block_groups()
2554 cache_gen = btrfs_super_cache_generation(info->super_copy); in btrfs_read_block_groups()
2556 btrfs_super_generation(info->super_copy) != cache_gen) in btrfs_read_block_groups()
2572 leaf = path->nodes[0]; in btrfs_read_block_groups()
2573 slot = path->slots[0]; in btrfs_read_block_groups()
2588 list_for_each_entry(space_info, &info->space_info, list) { in btrfs_read_block_groups()
2592 if (list_empty(&space_info->block_groups[i])) in btrfs_read_block_groups()
2594 cache = list_first_entry(&space_info->block_groups[i], in btrfs_read_block_groups()
2597 btrfs_sysfs_add_block_group_type(cache); in btrfs_read_block_groups()
2600 if (!(btrfs_get_alloc_profile(info, space_info->flags) & in btrfs_read_block_groups()
2607 * Avoid allocating from un-mirrored block group if there are in btrfs_read_block_groups()
2608 * mirrored block groups. in btrfs_read_block_groups()
2610 list_for_each_entry(cache, in btrfs_read_block_groups()
2611 &space_info->block_groups[BTRFS_RAID_RAID0], in btrfs_read_block_groups()
2613 inc_block_group_ro(cache, 1); in btrfs_read_block_groups()
2614 list_for_each_entry(cache, in btrfs_read_block_groups()
2615 &space_info->block_groups[BTRFS_RAID_SINGLE], in btrfs_read_block_groups()
2617 inc_block_group_ro(cache, 1); in btrfs_read_block_groups()
2627 * Try to fill the tree using dummy block groups so that the user can in btrfs_read_block_groups()
2645 struct btrfs_fs_info *fs_info = trans->fs_info; in insert_block_group_item()
2652 spin_lock(&block_group->lock); in insert_block_group_item()
2653 btrfs_set_stack_block_group_used(&bgi, block_group->used); in insert_block_group_item()
2655 block_group->global_root_id); in insert_block_group_item()
2656 btrfs_set_stack_block_group_flags(&bgi, block_group->flags); in insert_block_group_item()
2657 old_commit_used = block_group->commit_used; in insert_block_group_item()
2658 block_group->commit_used = block_group->used; in insert_block_group_item()
2659 key.objectid = block_group->start; in insert_block_group_item()
2661 key.offset = block_group->length; in insert_block_group_item()
2662 spin_unlock(&block_group->lock); in insert_block_group_item()
2666 spin_lock(&block_group->lock); in insert_block_group_item()
2667 block_group->commit_used = old_commit_used; in insert_block_group_item()
2668 spin_unlock(&block_group->lock); in insert_block_group_item()
2678 struct btrfs_fs_info *fs_info = device->fs_info; in insert_dev_extent()
2679 struct btrfs_root *root = fs_info->dev_root; in insert_dev_extent()
2686 WARN_ON(!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state)); in insert_dev_extent()
2687 WARN_ON(test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)); in insert_dev_extent()
2690 return -ENOMEM; in insert_dev_extent()
2692 key.objectid = device->devid; in insert_dev_extent()
2699 leaf = path->nodes[0]; in insert_dev_extent()
2700 extent = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_extent); in insert_dev_extent()
2719 struct btrfs_fs_info *fs_info = trans->fs_info; in insert_dev_extents()
2735 * (dev-replace.c:btrfs_dev_replace_finishing()), so we could grab the in insert_dev_extents()
2739 mutex_lock(&fs_info->fs_devices->device_list_mutex); in insert_dev_extents()
2740 for (i = 0; i < map->num_stripes; i++) { in insert_dev_extents()
2741 device = map->stripes[i].dev; in insert_dev_extents()
2742 dev_offset = map->stripes[i].physical; in insert_dev_extents()
2745 map->stripe_size); in insert_dev_extents()
2749 mutex_unlock(&fs_info->fs_devices->device_list_mutex); in insert_dev_extents()
2764 struct btrfs_fs_info *fs_info = trans->fs_info; in btrfs_create_pending_block_groups()
2768 while (!list_empty(&trans->new_bgs)) { in btrfs_create_pending_block_groups()
2771 block_group = list_first_entry(&trans->new_bgs, in btrfs_create_pending_block_groups()
2777 index = btrfs_bg_flags_to_raid_index(block_group->flags); in btrfs_create_pending_block_groups()
2783 &block_group->runtime_flags)) { in btrfs_create_pending_block_groups()
2784 mutex_lock(&fs_info->chunk_mutex); in btrfs_create_pending_block_groups()
2786 mutex_unlock(&fs_info->chunk_mutex); in btrfs_create_pending_block_groups()
2790 ret = insert_dev_extents(trans, block_group->start, in btrfs_create_pending_block_groups()
2791 block_group->length); in btrfs_create_pending_block_groups()
2802 if (block_group->space_info->block_group_kobjs[index] == NULL) in btrfs_create_pending_block_groups()
2809 spin_lock(&fs_info->unused_bgs_lock); in btrfs_create_pending_block_groups()
2810 list_del_init(&block_group->bg_list); in btrfs_create_pending_block_groups()
2811 clear_bit(BLOCK_GROUP_FLAG_NEW, &block_group->runtime_flags); in btrfs_create_pending_block_groups()
2813 spin_unlock(&fs_info->unused_bgs_lock); in btrfs_create_pending_block_groups()
2816 * If the block group is still unused, add it to the list of in btrfs_create_pending_block_groups()
2817 * unused block groups. The block group may have been created in in btrfs_create_pending_block_groups()
2821 * so the block group may become unused for a long time. For in btrfs_create_pending_block_groups()
2838 spin_lock(&block_group->lock); in btrfs_create_pending_block_groups()
2840 spin_unlock(&block_group->lock); in btrfs_create_pending_block_groups()
2850 * For extent tree v2 we use the block_group_item->chunk_offset to point at our
2862 if (btrfs_super_total_bytes(fs_info->super_copy) <= (SZ_1G * 10ULL)) in calculate_global_root_id()
2866 div64_u64_rem(offset, fs_info->nr_global_roots, &index); in calculate_global_root_id()
2874 struct btrfs_fs_info *fs_info = trans->fs_info; in btrfs_make_block_group()
2875 struct btrfs_block_group *cache; in btrfs_make_block_group() local
2880 cache = btrfs_create_block_group_cache(fs_info, chunk_offset); in btrfs_make_block_group()
2881 if (!cache) in btrfs_make_block_group()
2882 return ERR_PTR(-ENOMEM); in btrfs_make_block_group()
2885 * Mark it as new before adding it to the rbtree of block groups or any in btrfs_make_block_group()
2889 set_bit(BLOCK_GROUP_FLAG_NEW, &cache->runtime_flags); in btrfs_make_block_group()
2891 cache->length = size; in btrfs_make_block_group()
2892 set_free_space_tree_thresholds(cache); in btrfs_make_block_group()
2893 cache->flags = type; in btrfs_make_block_group()
2894 cache->cached = BTRFS_CACHE_FINISHED; in btrfs_make_block_group()
2895 cache->global_root_id = calculate_global_root_id(fs_info, cache->start); in btrfs_make_block_group()
2898 set_bit(BLOCK_GROUP_FLAG_NEEDS_FREE_SPACE, &cache->runtime_flags); in btrfs_make_block_group()
2900 ret = btrfs_load_block_group_zone_info(cache, true); in btrfs_make_block_group()
2902 btrfs_put_block_group(cache); in btrfs_make_block_group()
2906 ret = exclude_super_stripes(cache); in btrfs_make_block_group()
2909 btrfs_free_excluded_extents(cache); in btrfs_make_block_group()
2910 btrfs_put_block_group(cache); in btrfs_make_block_group()
2914 ret = btrfs_add_new_free_space(cache, chunk_offset, chunk_offset + size, NULL); in btrfs_make_block_group()
2915 btrfs_free_excluded_extents(cache); in btrfs_make_block_group()
2917 btrfs_put_block_group(cache); in btrfs_make_block_group()
2923 * assigned to our block group. We want our bg to be added to the rbtree in btrfs_make_block_group()
2924 * with its ->space_info set. in btrfs_make_block_group()
2926 cache->space_info = btrfs_find_space_info(fs_info, cache->flags); in btrfs_make_block_group()
2927 ASSERT(cache->space_info); in btrfs_make_block_group()
2929 ret = btrfs_add_block_group_cache(cache); in btrfs_make_block_group()
2931 btrfs_remove_free_space_cache(cache); in btrfs_make_block_group()
2932 btrfs_put_block_group(cache); in btrfs_make_block_group()
2937 * Now that our block group has its ->space_info set and is inserted in in btrfs_make_block_group()
2940 trace_btrfs_add_block_group(fs_info, cache, 1); in btrfs_make_block_group()
2941 btrfs_add_bg_to_space_info(fs_info, cache); in btrfs_make_block_group()
2945 if (btrfs_should_fragment_free_space(cache)) { in btrfs_make_block_group()
2946 cache->space_info->bytes_used += size >> 1; in btrfs_make_block_group()
2947 fragment_free_space(cache); in btrfs_make_block_group()
2951 btrfs_link_bg_list(cache, &trans->new_bgs); in btrfs_make_block_group()
2955 return cache; in btrfs_make_block_group()
2959 * Mark one block group RO, can be called several times for the same block
2962 * @cache: the destination block group
2963 * @do_chunk_alloc: whether need to do chunk pre-allocation, this is to
2965 * block group RO.
2967 int btrfs_inc_block_group_ro(struct btrfs_block_group *cache, in btrfs_inc_block_group_ro() argument
2970 struct btrfs_fs_info *fs_info = cache->fs_info; in btrfs_inc_block_group_ro()
2978 * This can only happen when we are doing read-only scrub on read-only in btrfs_inc_block_group_ro()
2980 * In that case we should not start a new transaction on read-only fs. in btrfs_inc_block_group_ro()
2983 if (sb_rdonly(fs_info->sb)) { in btrfs_inc_block_group_ro()
2984 mutex_lock(&fs_info->ro_block_group_mutex); in btrfs_inc_block_group_ro()
2985 ret = inc_block_group_ro(cache, 0); in btrfs_inc_block_group_ro()
2986 mutex_unlock(&fs_info->ro_block_group_mutex); in btrfs_inc_block_group_ro()
2998 * We're not allowed to set block groups readonly after the dirty in btrfs_inc_block_group_ro()
2999 * block group cache has started writing. If it already started, in btrfs_inc_block_group_ro()
3002 mutex_lock(&fs_info->ro_block_group_mutex); in btrfs_inc_block_group_ro()
3003 if (test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &trans->transaction->flags)) { in btrfs_inc_block_group_ro()
3004 u64 transid = trans->transid; in btrfs_inc_block_group_ro()
3006 mutex_unlock(&fs_info->ro_block_group_mutex); in btrfs_inc_block_group_ro()
3019 * corresponding block group with the new raid level. in btrfs_inc_block_group_ro()
3021 alloc_flags = btrfs_get_alloc_profile(fs_info, cache->flags); in btrfs_inc_block_group_ro()
3022 if (alloc_flags != cache->flags) { in btrfs_inc_block_group_ro()
3029 if (ret == -ENOSPC) in btrfs_inc_block_group_ro()
3036 ret = inc_block_group_ro(cache, 0); in btrfs_inc_block_group_ro()
3039 if (ret == -ETXTBSY) in btrfs_inc_block_group_ro()
3045 * we still want to try our best to mark the block group read-only. in btrfs_inc_block_group_ro()
3047 if (!do_chunk_alloc && ret == -ENOSPC && in btrfs_inc_block_group_ro()
3048 (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM)) in btrfs_inc_block_group_ro()
3051 alloc_flags = btrfs_get_alloc_profile(fs_info, cache->space_info->flags); in btrfs_inc_block_group_ro()
3059 ret = btrfs_zoned_activate_one_bg(fs_info, cache->space_info, true); in btrfs_inc_block_group_ro()
3063 ret = inc_block_group_ro(cache, 0); in btrfs_inc_block_group_ro()
3064 if (ret == -ETXTBSY) in btrfs_inc_block_group_ro()
3067 if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) { in btrfs_inc_block_group_ro()
3068 alloc_flags = btrfs_get_alloc_profile(fs_info, cache->flags); in btrfs_inc_block_group_ro()
3069 mutex_lock(&fs_info->chunk_mutex); in btrfs_inc_block_group_ro()
3071 mutex_unlock(&fs_info->chunk_mutex); in btrfs_inc_block_group_ro()
3074 mutex_unlock(&fs_info->ro_block_group_mutex); in btrfs_inc_block_group_ro()
3080 void btrfs_dec_block_group_ro(struct btrfs_block_group *cache) in btrfs_dec_block_group_ro() argument
3082 struct btrfs_space_info *sinfo = cache->space_info; in btrfs_dec_block_group_ro()
3085 BUG_ON(!cache->ro); in btrfs_dec_block_group_ro()
3087 spin_lock(&sinfo->lock); in btrfs_dec_block_group_ro()
3088 spin_lock(&cache->lock); in btrfs_dec_block_group_ro()
3089 if (!--cache->ro) { in btrfs_dec_block_group_ro()
3090 if (btrfs_is_zoned(cache->fs_info)) { in btrfs_dec_block_group_ro()
3092 cache->zone_unusable = in btrfs_dec_block_group_ro()
3093 (cache->alloc_offset - cache->used - cache->pinned - in btrfs_dec_block_group_ro()
3094 cache->reserved) + in btrfs_dec_block_group_ro()
3095 (cache->length - cache->zone_capacity); in btrfs_dec_block_group_ro()
3096 btrfs_space_info_update_bytes_zone_unusable(sinfo, cache->zone_unusable); in btrfs_dec_block_group_ro()
3097 sinfo->bytes_readonly -= cache->zone_unusable; in btrfs_dec_block_group_ro()
3099 num_bytes = cache->length - cache->reserved - in btrfs_dec_block_group_ro()
3100 cache->pinned - cache->bytes_super - in btrfs_dec_block_group_ro()
3101 cache->zone_unusable - cache->used; in btrfs_dec_block_group_ro()
3102 sinfo->bytes_readonly -= num_bytes; in btrfs_dec_block_group_ro()
3103 list_del_init(&cache->ro_list); in btrfs_dec_block_group_ro()
3105 spin_unlock(&cache->lock); in btrfs_dec_block_group_ro()
3106 spin_unlock(&sinfo->lock); in btrfs_dec_block_group_ro()
3111 struct btrfs_block_group *cache) in update_block_group_item() argument
3113 struct btrfs_fs_info *fs_info = trans->fs_info; in update_block_group_item()
3124 * Block group items update can be triggered out of commit transaction in update_block_group_item()
3126 * We cannot use cache->used directly outside of the spin lock, as it in update_block_group_item()
3129 spin_lock(&cache->lock); in update_block_group_item()
3130 old_commit_used = cache->commit_used; in update_block_group_item()
3131 used = cache->used; in update_block_group_item()
3133 if (cache->commit_used == used) { in update_block_group_item()
3134 spin_unlock(&cache->lock); in update_block_group_item()
3137 cache->commit_used = used; in update_block_group_item()
3138 spin_unlock(&cache->lock); in update_block_group_item()
3140 key.objectid = cache->start; in update_block_group_item()
3142 key.offset = cache->length; in update_block_group_item()
3147 ret = -ENOENT; in update_block_group_item()
3151 leaf = path->nodes[0]; in update_block_group_item()
3152 bi = btrfs_item_ptr_offset(leaf, path->slots[0]); in update_block_group_item()
3155 cache->global_root_id); in update_block_group_item()
3156 btrfs_set_stack_block_group_flags(&bgi, cache->flags); in update_block_group_item()
3161 * We didn't update the block group item, need to revert commit_used in update_block_group_item()
3162 * unless the block group item didn't exist yet - this is to prevent a in update_block_group_item()
3163 * race with a concurrent insertion of the block group item, with in update_block_group_item()
3166 * insertion set it to a value greater than 0 - if the block group later in update_block_group_item()
3169 if (ret < 0 && ret != -ENOENT) { in update_block_group_item()
3170 spin_lock(&cache->lock); in update_block_group_item()
3171 cache->commit_used = old_commit_used; in update_block_group_item()
3172 spin_unlock(&cache->lock); in update_block_group_item()
3182 struct btrfs_fs_info *fs_info = block_group->fs_info; in cache_save_setup()
3195 * If this block group is smaller than 100 megs don't bother caching the in cache_save_setup()
3196 * block group. in cache_save_setup()
3198 if (block_group->length < (100 * SZ_1M)) { in cache_save_setup()
3199 spin_lock(&block_group->lock); in cache_save_setup()
3200 block_group->disk_cache_state = BTRFS_DC_WRITTEN; in cache_save_setup()
3201 spin_unlock(&block_group->lock); in cache_save_setup()
3209 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) { in cache_save_setup()
3219 if (block_group->ro) in cache_save_setup()
3230 * from here on out we know not to trust this cache when we load up next in cache_save_setup()
3233 BTRFS_I(inode)->generation = 0; in cache_save_setup()
3238 * super cache generation to 0 so we know to invalidate the in cache_save_setup()
3239 * cache, but then we'd have to keep track of the block groups in cache_save_setup()
3240 * that fail this way so we know we _have_ to reset this cache in cache_save_setup()
3241 * before the next commit or risk reading stale cache. So to in cache_save_setup()
3252 if (block_group->cache_generation == trans->transid && in cache_save_setup()
3260 &fs_info->global_block_rsv); in cache_save_setup()
3269 spin_lock(&block_group->lock); in cache_save_setup()
3270 if (block_group->cached != BTRFS_CACHE_FINISHED || in cache_save_setup()
3279 spin_unlock(&block_group->lock); in cache_save_setup()
3282 spin_unlock(&block_group->lock); in cache_save_setup()
3285 * We hit an ENOSPC when setting up the cache in this transaction, just in cache_save_setup()
3286 * skip doing the setup, we've already cleared the cache so we're safe. in cache_save_setup()
3288 if (test_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags)) { in cache_save_setup()
3289 ret = -ENOSPC; in cache_save_setup()
3294 * Try to preallocate enough space based on how big the block group is. in cache_save_setup()
3297 * cache. in cache_save_setup()
3299 cache_size = div_u64(block_group->length, SZ_256M); in cache_save_setup()
3304 cache_size *= fs_info->sectorsize; in cache_save_setup()
3315 * Our cache requires contiguous chunks so that we don't modify a bunch in cache_save_setup()
3316 * of metadata or split extents when writing the cache out, which means in cache_save_setup()
3319 * other block groups for this transaction, maybe we'll unpin enough in cache_save_setup()
3324 else if (ret == -ENOSPC) in cache_save_setup()
3325 set_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags); in cache_save_setup()
3332 spin_lock(&block_group->lock); in cache_save_setup()
3334 block_group->cache_generation = trans->transid; in cache_save_setup()
3335 block_group->disk_cache_state = dcs; in cache_save_setup()
3336 spin_unlock(&block_group->lock); in cache_save_setup()
3344 struct btrfs_fs_info *fs_info = trans->fs_info; in btrfs_setup_space_cache()
3345 struct btrfs_block_group *cache, *tmp; in btrfs_setup_space_cache() local
3346 struct btrfs_transaction *cur_trans = trans->transaction; in btrfs_setup_space_cache()
3349 if (list_empty(&cur_trans->dirty_bgs) || in btrfs_setup_space_cache()
3355 return -ENOMEM; in btrfs_setup_space_cache()
3357 /* Could add new block groups, use _safe just in case */ in btrfs_setup_space_cache()
3358 list_for_each_entry_safe(cache, tmp, &cur_trans->dirty_bgs, in btrfs_setup_space_cache()
3360 if (cache->disk_cache_state == BTRFS_DC_CLEAR) in btrfs_setup_space_cache()
3361 cache_save_setup(cache, trans, path); in btrfs_setup_space_cache()
3368 * Transaction commit does final block group cache writeback during a critical
3370 * order for the cache to actually match the block group, but can introduce a
3373 * So, btrfs_start_dirty_block_groups is here to kick off block group cache IO.
3374 * There's a chance we'll have to redo some of it if the block group changes
3376 * getting rid of the easy block groups while we're still allowing others to
3381 struct btrfs_fs_info *fs_info = trans->fs_info; in btrfs_start_dirty_block_groups()
3382 struct btrfs_block_group *cache; in btrfs_start_dirty_block_groups() local
3383 struct btrfs_transaction *cur_trans = trans->transaction; in btrfs_start_dirty_block_groups()
3388 struct list_head *io = &cur_trans->io_bgs; in btrfs_start_dirty_block_groups()
3391 spin_lock(&cur_trans->dirty_bgs_lock); in btrfs_start_dirty_block_groups()
3392 if (list_empty(&cur_trans->dirty_bgs)) { in btrfs_start_dirty_block_groups()
3393 spin_unlock(&cur_trans->dirty_bgs_lock); in btrfs_start_dirty_block_groups()
3396 list_splice_init(&cur_trans->dirty_bgs, &dirty); in btrfs_start_dirty_block_groups()
3397 spin_unlock(&cur_trans->dirty_bgs_lock); in btrfs_start_dirty_block_groups()
3400 /* Make sure all the block groups on our dirty list actually exist */ in btrfs_start_dirty_block_groups()
3406 ret = -ENOMEM; in btrfs_start_dirty_block_groups()
3413 * removal of empty block groups deleting this block group while we are in btrfs_start_dirty_block_groups()
3414 * writing out the cache in btrfs_start_dirty_block_groups()
3416 mutex_lock(&trans->transaction->cache_write_mutex); in btrfs_start_dirty_block_groups()
3420 cache = list_first_entry(&dirty, struct btrfs_block_group, in btrfs_start_dirty_block_groups()
3423 * This can happen if something re-dirties a block group that in btrfs_start_dirty_block_groups()
3427 if (!list_empty(&cache->io_list)) { in btrfs_start_dirty_block_groups()
3428 list_del_init(&cache->io_list); in btrfs_start_dirty_block_groups()
3429 btrfs_wait_cache_io(trans, cache, path); in btrfs_start_dirty_block_groups()
3430 btrfs_put_block_group(cache); in btrfs_start_dirty_block_groups()
3435 * btrfs_wait_cache_io uses the cache->dirty_list to decide if in btrfs_start_dirty_block_groups()
3442 spin_lock(&cur_trans->dirty_bgs_lock); in btrfs_start_dirty_block_groups()
3443 list_del_init(&cache->dirty_list); in btrfs_start_dirty_block_groups()
3444 spin_unlock(&cur_trans->dirty_bgs_lock); in btrfs_start_dirty_block_groups()
3448 cache_save_setup(cache, trans, path); in btrfs_start_dirty_block_groups()
3450 if (cache->disk_cache_state == BTRFS_DC_SETUP) { in btrfs_start_dirty_block_groups()
3451 cache->io_ctl.inode = NULL; in btrfs_start_dirty_block_groups()
3452 ret = btrfs_write_out_cache(trans, cache, path); in btrfs_start_dirty_block_groups()
3453 if (ret == 0 && cache->io_ctl.inode) { in btrfs_start_dirty_block_groups()
3461 list_add_tail(&cache->io_list, io); in btrfs_start_dirty_block_groups()
3464 * If we failed to write the cache, the in btrfs_start_dirty_block_groups()
3471 ret = update_block_group_item(trans, path, cache); in btrfs_start_dirty_block_groups()
3473 * Our block group might still be attached to the list in btrfs_start_dirty_block_groups()
3474 * of new block groups in the transaction handle of some in btrfs_start_dirty_block_groups()
3475 * other task (struct btrfs_trans_handle->new_bgs). This in btrfs_start_dirty_block_groups()
3476 * means its block group item isn't yet in the extent in btrfs_start_dirty_block_groups()
3481 if (ret == -ENOENT) { in btrfs_start_dirty_block_groups()
3483 spin_lock(&cur_trans->dirty_bgs_lock); in btrfs_start_dirty_block_groups()
3484 if (list_empty(&cache->dirty_list)) { in btrfs_start_dirty_block_groups()
3485 list_add_tail(&cache->dirty_list, in btrfs_start_dirty_block_groups()
3486 &cur_trans->dirty_bgs); in btrfs_start_dirty_block_groups()
3487 btrfs_get_block_group(cache); in btrfs_start_dirty_block_groups()
3490 spin_unlock(&cur_trans->dirty_bgs_lock); in btrfs_start_dirty_block_groups()
3496 /* If it's not on the io list, we need to put the block group */ in btrfs_start_dirty_block_groups()
3498 btrfs_put_block_group(cache); in btrfs_start_dirty_block_groups()
3503 * us from writing caches for block groups that are going to be in btrfs_start_dirty_block_groups()
3506 mutex_unlock(&trans->transaction->cache_write_mutex); in btrfs_start_dirty_block_groups()
3509 mutex_lock(&trans->transaction->cache_write_mutex); in btrfs_start_dirty_block_groups()
3511 mutex_unlock(&trans->transaction->cache_write_mutex); in btrfs_start_dirty_block_groups()
3521 spin_lock(&cur_trans->dirty_bgs_lock); in btrfs_start_dirty_block_groups()
3522 list_splice_init(&cur_trans->dirty_bgs, &dirty); in btrfs_start_dirty_block_groups()
3524 * dirty_bgs_lock protects us from concurrent block group in btrfs_start_dirty_block_groups()
3528 spin_unlock(&cur_trans->dirty_bgs_lock); in btrfs_start_dirty_block_groups()
3531 spin_unlock(&cur_trans->dirty_bgs_lock); in btrfs_start_dirty_block_groups()
3535 spin_lock(&cur_trans->dirty_bgs_lock); in btrfs_start_dirty_block_groups()
3536 list_splice_init(&dirty, &cur_trans->dirty_bgs); in btrfs_start_dirty_block_groups()
3537 spin_unlock(&cur_trans->dirty_bgs_lock); in btrfs_start_dirty_block_groups()
3546 struct btrfs_fs_info *fs_info = trans->fs_info; in btrfs_write_dirty_block_groups()
3547 struct btrfs_block_group *cache; in btrfs_write_dirty_block_groups() local
3548 struct btrfs_transaction *cur_trans = trans->transaction; in btrfs_write_dirty_block_groups()
3552 struct list_head *io = &cur_trans->io_bgs; in btrfs_write_dirty_block_groups()
3556 return -ENOMEM; in btrfs_write_dirty_block_groups()
3561 * transaction's list of dirty block groups. These tasks correspond to in btrfs_write_dirty_block_groups()
3563 * space cache, which run inode.c:btrfs_finish_ordered_io(), and can in btrfs_write_dirty_block_groups()
3564 * allocate new block groups as a result of COWing nodes of the root in btrfs_write_dirty_block_groups()
3573 spin_lock(&cur_trans->dirty_bgs_lock); in btrfs_write_dirty_block_groups()
3574 while (!list_empty(&cur_trans->dirty_bgs)) { in btrfs_write_dirty_block_groups()
3575 cache = list_first_entry(&cur_trans->dirty_bgs, in btrfs_write_dirty_block_groups()
3580 * This can happen if cache_save_setup re-dirties a block group in btrfs_write_dirty_block_groups()
3584 if (!list_empty(&cache->io_list)) { in btrfs_write_dirty_block_groups()
3585 spin_unlock(&cur_trans->dirty_bgs_lock); in btrfs_write_dirty_block_groups()
3586 list_del_init(&cache->io_list); in btrfs_write_dirty_block_groups()
3587 btrfs_wait_cache_io(trans, cache, path); in btrfs_write_dirty_block_groups()
3588 btrfs_put_block_group(cache); in btrfs_write_dirty_block_groups()
3589 spin_lock(&cur_trans->dirty_bgs_lock); in btrfs_write_dirty_block_groups()
3596 list_del_init(&cache->dirty_list); in btrfs_write_dirty_block_groups()
3597 spin_unlock(&cur_trans->dirty_bgs_lock); in btrfs_write_dirty_block_groups()
3600 cache_save_setup(cache, trans, path); in btrfs_write_dirty_block_groups()
3605 if (!ret && cache->disk_cache_state == BTRFS_DC_SETUP) { in btrfs_write_dirty_block_groups()
3606 cache->io_ctl.inode = NULL; in btrfs_write_dirty_block_groups()
3607 ret = btrfs_write_out_cache(trans, cache, path); in btrfs_write_dirty_block_groups()
3608 if (ret == 0 && cache->io_ctl.inode) { in btrfs_write_dirty_block_groups()
3610 list_add_tail(&cache->io_list, io); in btrfs_write_dirty_block_groups()
3613 * If we failed to write the cache, the in btrfs_write_dirty_block_groups()
3620 ret = update_block_group_item(trans, path, cache); in btrfs_write_dirty_block_groups()
3623 * created a new block group while updating a free space in btrfs_write_dirty_block_groups()
3624 * cache's inode (at inode.c:btrfs_finish_ordered_io()) in btrfs_write_dirty_block_groups()
3626 * which case the new block group is still attached to in btrfs_write_dirty_block_groups()
3628 * finished yet (no block group item in the extent tree in btrfs_write_dirty_block_groups()
3634 if (ret == -ENOENT) { in btrfs_write_dirty_block_groups()
3635 wait_event(cur_trans->writer_wait, in btrfs_write_dirty_block_groups()
3636 atomic_read(&cur_trans->num_writers) == 1); in btrfs_write_dirty_block_groups()
3637 ret = update_block_group_item(trans, path, cache); in btrfs_write_dirty_block_groups()
3643 /* If its not on the io list, we need to put the block group */ in btrfs_write_dirty_block_groups()
3645 btrfs_put_block_group(cache); in btrfs_write_dirty_block_groups()
3647 spin_lock(&cur_trans->dirty_bgs_lock); in btrfs_write_dirty_block_groups()
3649 spin_unlock(&cur_trans->dirty_bgs_lock); in btrfs_write_dirty_block_groups()
3656 cache = list_first_entry(io, struct btrfs_block_group, in btrfs_write_dirty_block_groups()
3658 list_del_init(&cache->io_list); in btrfs_write_dirty_block_groups()
3659 btrfs_wait_cache_io(trans, cache, path); in btrfs_write_dirty_block_groups()
3660 btrfs_put_block_group(cache); in btrfs_write_dirty_block_groups()
3669 struct btrfs_fs_info *info = trans->fs_info; in btrfs_update_block_group()
3671 struct btrfs_block_group *cache; in btrfs_update_block_group() local
3677 /* Block accounting for super block */ in btrfs_update_block_group()
3678 spin_lock(&info->delalloc_root_lock); in btrfs_update_block_group()
3679 old_val = btrfs_super_bytes_used(info->super_copy); in btrfs_update_block_group()
3683 old_val -= num_bytes; in btrfs_update_block_group()
3684 btrfs_set_super_bytes_used(info->super_copy, old_val); in btrfs_update_block_group()
3685 spin_unlock(&info->delalloc_root_lock); in btrfs_update_block_group()
3687 cache = btrfs_lookup_block_group(info, bytenr); in btrfs_update_block_group()
3688 if (!cache) in btrfs_update_block_group()
3689 return -ENOENT; in btrfs_update_block_group()
3691 /* An extent can not span multiple block groups. */ in btrfs_update_block_group()
3692 ASSERT(bytenr + num_bytes <= cache->start + cache->length); in btrfs_update_block_group()
3694 space_info = cache->space_info; in btrfs_update_block_group()
3695 factor = btrfs_bg_type_to_factor(cache->flags); in btrfs_update_block_group()
3698 * If this block group has free space cache written out, we need to make in btrfs_update_block_group()
3700 * the unpinning stage to actually add the space back to the block group, in btrfs_update_block_group()
3703 if (!alloc && !btrfs_block_group_done(cache)) in btrfs_update_block_group()
3704 btrfs_cache_block_group(cache, true); in btrfs_update_block_group()
3706 spin_lock(&space_info->lock); in btrfs_update_block_group()
3707 spin_lock(&cache->lock); in btrfs_update_block_group()
3710 cache->disk_cache_state < BTRFS_DC_CLEAR) in btrfs_update_block_group()
3711 cache->disk_cache_state = BTRFS_DC_CLEAR; in btrfs_update_block_group()
3713 old_val = cache->used; in btrfs_update_block_group()
3716 cache->used = old_val; in btrfs_update_block_group()
3717 cache->reserved -= num_bytes; in btrfs_update_block_group()
3718 cache->reclaim_mark = 0; in btrfs_update_block_group()
3719 space_info->bytes_reserved -= num_bytes; in btrfs_update_block_group()
3720 space_info->bytes_used += num_bytes; in btrfs_update_block_group()
3721 space_info->disk_used += num_bytes * factor; in btrfs_update_block_group()
3722 if (READ_ONCE(space_info->periodic_reclaim)) in btrfs_update_block_group()
3723 btrfs_space_info_update_reclaimable(space_info, -num_bytes); in btrfs_update_block_group()
3724 spin_unlock(&cache->lock); in btrfs_update_block_group()
3725 spin_unlock(&space_info->lock); in btrfs_update_block_group()
3727 old_val -= num_bytes; in btrfs_update_block_group()
3728 cache->used = old_val; in btrfs_update_block_group()
3729 cache->pinned += num_bytes; in btrfs_update_block_group()
3731 space_info->bytes_used -= num_bytes; in btrfs_update_block_group()
3732 space_info->disk_used -= num_bytes * factor; in btrfs_update_block_group()
3733 if (READ_ONCE(space_info->periodic_reclaim)) in btrfs_update_block_group()
3736 reclaim = should_reclaim_block_group(cache, num_bytes); in btrfs_update_block_group()
3738 spin_unlock(&cache->lock); in btrfs_update_block_group()
3739 spin_unlock(&space_info->lock); in btrfs_update_block_group()
3741 set_extent_bit(&trans->transaction->pinned_extents, bytenr, in btrfs_update_block_group()
3742 bytenr + num_bytes - 1, EXTENT_DIRTY, NULL); in btrfs_update_block_group()
3745 spin_lock(&trans->transaction->dirty_bgs_lock); in btrfs_update_block_group()
3746 if (list_empty(&cache->dirty_list)) { in btrfs_update_block_group()
3747 list_add_tail(&cache->dirty_list, &trans->transaction->dirty_bgs); in btrfs_update_block_group()
3749 btrfs_get_block_group(cache); in btrfs_update_block_group()
3751 spin_unlock(&trans->transaction->dirty_bgs_lock); in btrfs_update_block_group()
3754 * No longer have used bytes in this block group, queue it for deletion. in btrfs_update_block_group()
3755 * We do this after adding the block group to the dirty list to avoid in btrfs_update_block_group()
3756 * races between cleaner kthread and space cache writeout. in btrfs_update_block_group()
3760 btrfs_mark_bg_unused(cache); in btrfs_update_block_group()
3762 btrfs_mark_bg_to_reclaim(cache); in btrfs_update_block_group()
3765 btrfs_put_block_group(cache); in btrfs_update_block_group()
3767 /* Modified block groups are accounted for in the delayed_refs_rsv. */ in btrfs_update_block_group()
3777 * @cache: The cache we are manipulating
3784 * reservation and the block group has become read only we cannot make the
3785 * reservation and return -EAGAIN, otherwise this function always succeeds.
3787 int btrfs_add_reserved_bytes(struct btrfs_block_group *cache, in btrfs_add_reserved_bytes() argument
3791 struct btrfs_space_info *space_info = cache->space_info; in btrfs_add_reserved_bytes()
3795 spin_lock(&space_info->lock); in btrfs_add_reserved_bytes()
3796 spin_lock(&cache->lock); in btrfs_add_reserved_bytes()
3797 if (cache->ro) { in btrfs_add_reserved_bytes()
3798 ret = -EAGAIN; in btrfs_add_reserved_bytes()
3802 if (btrfs_block_group_should_use_size_class(cache)) { in btrfs_add_reserved_bytes()
3804 ret = btrfs_use_block_group_size_class(cache, size_class, force_wrong_size_class); in btrfs_add_reserved_bytes()
3808 cache->reserved += num_bytes; in btrfs_add_reserved_bytes()
3809 space_info->bytes_reserved += num_bytes; in btrfs_add_reserved_bytes()
3810 trace_btrfs_space_reservation(cache->fs_info, "space_info", in btrfs_add_reserved_bytes()
3811 space_info->flags, num_bytes, 1); in btrfs_add_reserved_bytes()
3812 btrfs_space_info_update_bytes_may_use(space_info, -ram_bytes); in btrfs_add_reserved_bytes()
3814 cache->delalloc_bytes += num_bytes; in btrfs_add_reserved_bytes()
3821 btrfs_try_granting_tickets(cache->fs_info, space_info); in btrfs_add_reserved_bytes()
3823 spin_unlock(&cache->lock); in btrfs_add_reserved_bytes()
3824 spin_unlock(&space_info->lock); in btrfs_add_reserved_bytes()
3831 * @cache: The cache we are manipulating
3840 void btrfs_free_reserved_bytes(struct btrfs_block_group *cache, in btrfs_free_reserved_bytes() argument
3843 struct btrfs_space_info *space_info = cache->space_info; in btrfs_free_reserved_bytes()
3845 spin_lock(&space_info->lock); in btrfs_free_reserved_bytes()
3846 spin_lock(&cache->lock); in btrfs_free_reserved_bytes()
3847 if (cache->ro) in btrfs_free_reserved_bytes()
3848 space_info->bytes_readonly += num_bytes; in btrfs_free_reserved_bytes()
3849 else if (btrfs_is_zoned(cache->fs_info)) in btrfs_free_reserved_bytes()
3850 space_info->bytes_zone_unusable += num_bytes; in btrfs_free_reserved_bytes()
3851 cache->reserved -= num_bytes; in btrfs_free_reserved_bytes()
3852 space_info->bytes_reserved -= num_bytes; in btrfs_free_reserved_bytes()
3853 space_info->max_extent_size = 0; in btrfs_free_reserved_bytes()
3856 cache->delalloc_bytes -= num_bytes; in btrfs_free_reserved_bytes()
3857 spin_unlock(&cache->lock); in btrfs_free_reserved_bytes()
3859 btrfs_try_granting_tickets(cache->fs_info, space_info); in btrfs_free_reserved_bytes()
3860 spin_unlock(&space_info->lock); in btrfs_free_reserved_bytes()
3865 struct list_head *head = &info->space_info; in force_metadata_allocation()
3869 if (found->flags & BTRFS_BLOCK_GROUP_METADATA) in force_metadata_allocation()
3870 found->force_alloc = CHUNK_ALLOC_FORCE; in force_metadata_allocation()
3888 thresh = btrfs_super_total_bytes(fs_info->super_copy); in should_alloc_chunk()
3891 if (sinfo->total_bytes - bytes_used < thresh) in should_alloc_chunk()
3895 if (bytes_used + SZ_2M < mult_perc(sinfo->total_bytes, 80)) in should_alloc_chunk()
3902 u64 alloc_flags = btrfs_get_alloc_profile(trans->fs_info, type); in btrfs_force_chunk_alloc()
3916 * system block group if needed. in do_chunk_alloc()
3928 * Normally we are not expected to fail with -ENOSPC here, since we have in do_chunk_alloc()
3933 * existing system block groups have a profile which can not be used in do_chunk_alloc()
3940 * none of the block groups can be used for extent allocation since they in do_chunk_alloc()
3944 * block groups and check if they have a usable profile and enough space in do_chunk_alloc()
3945 * can be slow on very large filesystems, so we tolerate the -ENOSPC and in do_chunk_alloc()
3951 * block group to allocate from when we called check_system_chunk() in do_chunk_alloc()
3952 * above. However right after we called it, the only system block group in do_chunk_alloc()
3956 * handle and scrub uses the commit root to search for block groups; in do_chunk_alloc()
3958 * 3) We had one system block group with enough free space when we called in do_chunk_alloc()
3962 * block group (discard removes a free space entry, discards it, and in do_chunk_alloc()
3963 * then adds back the entry to the block group cache). in do_chunk_alloc()
3965 if (ret == -ENOSPC) { in do_chunk_alloc()
3966 const u64 sys_flags = btrfs_system_alloc_profile(trans->fs_info); in do_chunk_alloc()
4004 * 1) Phase 1 - through btrfs_chunk_alloc() we allocate device extents for
4005 * the chunk, the chunk mapping, create its block group and add the items
4006 * that belong in the chunk btree to it - more specifically, we need to
4009 * 2) Phase 2 - through btrfs_create_pending_block_groups(), we add the block
4015 * trigger chunk allocation and attempted to insert the new block group item
4026 * allocate a new block group (chunk) because the only one that had enough
4028 * device replace, block group reclaim thread, etc), so we can not use it
4033 * the filesystem was mounted in degraded mode, none of the existing block
4035 * profile (for e.g. mounting a 2 devices filesystem, where all block groups
4038 * example, it will trigger allocation of a new metadata block group with a
4044 * example, it does not find any free extent in any metadata block group,
4045 * therefore forced to try to allocate a new metadata block group.
4047 * meanwhile - this typically happens with tasks that don't reserve space
4056 * the only metadata block group that had free space (discard starts by
4057 * removing a free space entry from a block group, then does the discard
4059 * block group).
4062 * a seed device - we must create new metadata and system chunks without adding
4063 * any of the block group items to the chunk, extent and device btrees. If we
4065 * btrees, since all the chunks from the seed device are read-only.
4080 * btrfs_reserve_chunk_metadata() - the former is used when allocating a data or
4082 * a modification to the chunk btree - use cases for the later are adding,
4088 * holding fs_info->chunk_mutex. This is important to guarantee that while COWing
4094 * that mutex. The same logic applies to removing chunks - we must reserve system
4096 * while holding fs_info->chunk_mutex.
4101 * - return 1 if it successfully allocates a chunk,
4102 * - return errors including -ENOSPC otherwise.
4104 * - return 0 if it doesn't need to allocate a new chunk,
4105 * - return 1 if it successfully allocates a chunk,
4106 * - return errors including -ENOSPC otherwise.
4111 struct btrfs_fs_info *fs_info = trans->fs_info; in btrfs_chunk_alloc()
4124 /* Don't re-enter if we're already allocating a chunk */ in btrfs_chunk_alloc()
4125 if (trans->allocating_chunk) in btrfs_chunk_alloc()
4126 return -ENOSPC; in btrfs_chunk_alloc()
4136 * lock on it and on its parent - if the COW operation triggers a system in btrfs_chunk_alloc()
4145 * here - this happens in the cases described above at do_chunk_alloc(). in btrfs_chunk_alloc()
4149 return -ENOSPC; in btrfs_chunk_alloc()
4155 spin_lock(&space_info->lock); in btrfs_chunk_alloc()
4156 if (force < space_info->force_alloc) in btrfs_chunk_alloc()
4157 force = space_info->force_alloc; in btrfs_chunk_alloc()
4159 if (space_info->full) { in btrfs_chunk_alloc()
4162 ret = -ENOSPC; in btrfs_chunk_alloc()
4165 spin_unlock(&space_info->lock); in btrfs_chunk_alloc()
4168 spin_unlock(&space_info->lock); in btrfs_chunk_alloc()
4170 } else if (space_info->chunk_alloc) { in btrfs_chunk_alloc()
4172 * Someone is already allocating, so we need to block in btrfs_chunk_alloc()
4179 spin_unlock(&space_info->lock); in btrfs_chunk_alloc()
4180 mutex_lock(&fs_info->chunk_mutex); in btrfs_chunk_alloc()
4181 mutex_unlock(&fs_info->chunk_mutex); in btrfs_chunk_alloc()
4184 space_info->chunk_alloc = 1; in btrfs_chunk_alloc()
4186 spin_unlock(&space_info->lock); in btrfs_chunk_alloc()
4192 mutex_lock(&fs_info->chunk_mutex); in btrfs_chunk_alloc()
4193 trans->allocating_chunk = true; in btrfs_chunk_alloc()
4207 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) { in btrfs_chunk_alloc()
4208 fs_info->data_chunk_allocations++; in btrfs_chunk_alloc()
4209 if (!(fs_info->data_chunk_allocations % in btrfs_chunk_alloc()
4210 fs_info->metadata_ratio)) in btrfs_chunk_alloc()
4215 trans->allocating_chunk = false; in btrfs_chunk_alloc()
4221 * New block group is likely to be used soon. Try to activate in btrfs_chunk_alloc()
4230 spin_lock(&space_info->lock); in btrfs_chunk_alloc()
4232 if (ret == -ENOSPC) in btrfs_chunk_alloc()
4233 space_info->full = 1; in btrfs_chunk_alloc()
4238 space_info->max_extent_size = 0; in btrfs_chunk_alloc()
4241 space_info->force_alloc = CHUNK_ALLOC_NO_FORCE; in btrfs_chunk_alloc()
4243 space_info->chunk_alloc = 0; in btrfs_chunk_alloc()
4244 spin_unlock(&space_info->lock); in btrfs_chunk_alloc()
4245 mutex_unlock(&fs_info->chunk_mutex); in btrfs_chunk_alloc()
4256 num_dev = fs_info->fs_devices->rw_devices; in get_profile_num_devs()
4265 struct btrfs_fs_info *fs_info = trans->fs_info; in reserve_chunk_space()
4272 * atomic and race free space reservation in the chunk block reserve. in reserve_chunk_space()
4274 lockdep_assert_held(&fs_info->chunk_mutex); in reserve_chunk_space()
4277 spin_lock(&info->lock); in reserve_chunk_space()
4278 left = info->total_bytes - btrfs_space_info_used(info, true); in reserve_chunk_space()
4279 spin_unlock(&info->lock); in reserve_chunk_space()
4314 * the cases described at do_chunk_alloc() - the system in reserve_chunk_space()
4315 * block group we just created was just turned into RO in reserve_chunk_space()
4325 &fs_info->chunk_block_rsv, in reserve_chunk_space()
4328 trans->chunk_bytes_reserved += bytes; in reserve_chunk_space()
4334 * The caller must be holding fs_info->chunk_mutex.
4338 struct btrfs_fs_info *fs_info = trans->fs_info; in check_system_chunk()
4359 * block group allocation and removal, to avoid a deadlock with a concurrent
4360 * task that is allocating a metadata or data block group and therefore needs to
4368 struct btrfs_fs_info *fs_info = trans->fs_info; in btrfs_reserve_chunk_metadata()
4376 mutex_lock(&fs_info->chunk_mutex); in btrfs_reserve_chunk_metadata()
4378 mutex_unlock(&fs_info->chunk_mutex); in btrfs_reserve_chunk_metadata()
4388 spin_lock(&block_group->lock); in btrfs_put_block_group_cache()
4390 &block_group->runtime_flags)) { in btrfs_put_block_group_cache()
4391 struct btrfs_inode *inode = block_group->inode; in btrfs_put_block_group_cache()
4393 block_group->inode = NULL; in btrfs_put_block_group_cache()
4394 spin_unlock(&block_group->lock); in btrfs_put_block_group_cache()
4396 ASSERT(block_group->io_ctl.inode == NULL); in btrfs_put_block_group_cache()
4397 iput(&inode->vfs_inode); in btrfs_put_block_group_cache()
4399 spin_unlock(&block_group->lock); in btrfs_put_block_group_cache()
4406 * Must be called only after stopping all workers, since we could have block
4408 * freed the block groups before stopping them.
4418 if (info->active_meta_bg) { in btrfs_free_block_groups()
4419 btrfs_put_block_group(info->active_meta_bg); in btrfs_free_block_groups()
4420 info->active_meta_bg = NULL; in btrfs_free_block_groups()
4422 if (info->active_system_bg) { in btrfs_free_block_groups()
4423 btrfs_put_block_group(info->active_system_bg); in btrfs_free_block_groups()
4424 info->active_system_bg = NULL; in btrfs_free_block_groups()
4428 write_lock(&info->block_group_cache_lock); in btrfs_free_block_groups()
4429 while (!list_empty(&info->caching_block_groups)) { in btrfs_free_block_groups()
4430 caching_ctl = list_entry(info->caching_block_groups.next, in btrfs_free_block_groups()
4432 list_del(&caching_ctl->list); in btrfs_free_block_groups()
4435 write_unlock(&info->block_group_cache_lock); in btrfs_free_block_groups()
4437 spin_lock(&info->unused_bgs_lock); in btrfs_free_block_groups()
4438 while (!list_empty(&info->unused_bgs)) { in btrfs_free_block_groups()
4439 block_group = list_first_entry(&info->unused_bgs, in btrfs_free_block_groups()
4442 list_del_init(&block_group->bg_list); in btrfs_free_block_groups()
4446 while (!list_empty(&info->reclaim_bgs)) { in btrfs_free_block_groups()
4447 block_group = list_first_entry(&info->reclaim_bgs, in btrfs_free_block_groups()
4450 list_del_init(&block_group->bg_list); in btrfs_free_block_groups()
4453 spin_unlock(&info->unused_bgs_lock); in btrfs_free_block_groups()
4455 spin_lock(&info->zone_active_bgs_lock); in btrfs_free_block_groups()
4456 while (!list_empty(&info->zone_active_bgs)) { in btrfs_free_block_groups()
4457 block_group = list_first_entry(&info->zone_active_bgs, in btrfs_free_block_groups()
4460 list_del_init(&block_group->active_bg_list); in btrfs_free_block_groups()
4463 spin_unlock(&info->zone_active_bgs_lock); in btrfs_free_block_groups()
4465 write_lock(&info->block_group_cache_lock); in btrfs_free_block_groups()
4466 while ((n = rb_last(&info->block_group_cache_tree.rb_root)) != NULL) { in btrfs_free_block_groups()
4469 rb_erase_cached(&block_group->cache_node, in btrfs_free_block_groups()
4470 &info->block_group_cache_tree); in btrfs_free_block_groups()
4471 RB_CLEAR_NODE(&block_group->cache_node); in btrfs_free_block_groups()
4472 write_unlock(&info->block_group_cache_lock); in btrfs_free_block_groups()
4474 down_write(&block_group->space_info->groups_sem); in btrfs_free_block_groups()
4475 list_del(&block_group->list); in btrfs_free_block_groups()
4476 up_write(&block_group->space_info->groups_sem); in btrfs_free_block_groups()
4479 * We haven't cached this block group, which means we could in btrfs_free_block_groups()
4480 * possibly have excluded extents on this block group. in btrfs_free_block_groups()
4482 if (block_group->cached == BTRFS_CACHE_NO || in btrfs_free_block_groups()
4483 block_group->cached == BTRFS_CACHE_ERROR) in btrfs_free_block_groups()
4487 ASSERT(block_group->cached != BTRFS_CACHE_STARTED); in btrfs_free_block_groups()
4488 ASSERT(list_empty(&block_group->dirty_list)); in btrfs_free_block_groups()
4489 ASSERT(list_empty(&block_group->io_list)); in btrfs_free_block_groups()
4490 ASSERT(list_empty(&block_group->bg_list)); in btrfs_free_block_groups()
4491 ASSERT(refcount_read(&block_group->refs) == 1); in btrfs_free_block_groups()
4492 ASSERT(block_group->swap_extents == 0); in btrfs_free_block_groups()
4495 write_lock(&info->block_group_cache_lock); in btrfs_free_block_groups()
4497 write_unlock(&info->block_group_cache_lock); in btrfs_free_block_groups()
4501 while (!list_empty(&info->space_info)) { in btrfs_free_block_groups()
4502 space_info = list_entry(info->space_info.next, in btrfs_free_block_groups()
4510 if (WARN_ON(space_info->bytes_pinned > 0 || in btrfs_free_block_groups()
4511 space_info->bytes_may_use > 0)) in btrfs_free_block_groups()
4521 if (!(space_info->flags & BTRFS_BLOCK_GROUP_METADATA) || in btrfs_free_block_groups()
4523 if (WARN_ON(space_info->bytes_reserved > 0)) in btrfs_free_block_groups()
4527 WARN_ON(space_info->reclaim_size > 0); in btrfs_free_block_groups()
4528 list_del(&space_info->list); in btrfs_free_block_groups()
4534 void btrfs_freeze_block_group(struct btrfs_block_group *cache) in btrfs_freeze_block_group() argument
4536 atomic_inc(&cache->frozen); in btrfs_freeze_block_group()
4541 struct btrfs_fs_info *fs_info = block_group->fs_info; in btrfs_unfreeze_block_group()
4544 spin_lock(&block_group->lock); in btrfs_unfreeze_block_group()
4545 cleanup = (atomic_dec_and_test(&block_group->frozen) && in btrfs_unfreeze_block_group()
4546 test_bit(BLOCK_GROUP_FLAG_REMOVED, &block_group->runtime_flags)); in btrfs_unfreeze_block_group()
4547 spin_unlock(&block_group->lock); in btrfs_unfreeze_block_group()
4552 map = btrfs_find_chunk_map(fs_info, block_group->start, 1); in btrfs_unfreeze_block_group()
4563 * tasks trimming this block group have left 1 entry each one. in btrfs_unfreeze_block_group()
4574 spin_lock(&bg->lock); in btrfs_inc_block_group_swap_extents()
4575 if (bg->ro) in btrfs_inc_block_group_swap_extents()
4578 bg->swap_extents++; in btrfs_inc_block_group_swap_extents()
4579 spin_unlock(&bg->lock); in btrfs_inc_block_group_swap_extents()
4586 spin_lock(&bg->lock); in btrfs_dec_block_group_swap_extents()
4587 ASSERT(!bg->ro); in btrfs_dec_block_group_swap_extents()
4588 ASSERT(bg->swap_extents >= amount); in btrfs_dec_block_group_swap_extents()
4589 bg->swap_extents -= amount; in btrfs_dec_block_group_swap_extents()
4590 spin_unlock(&bg->lock); in btrfs_dec_block_group_swap_extents()
4603 * Handle a block group allocating an extent in a size class
4605 * @bg: The block group we allocated in.
4610 * Returns: 0 if the size class was valid for this block_group, -EAGAIN in the
4614 * find_free_extent will skip block groups with a mismatched size class until
4616 * force_wrong_size_class. However, if a block group is newly allocated and
4628 if (bg->size_class == size_class) in btrfs_use_block_group_size_class()
4640 if (bg->size_class != BTRFS_BG_SZ_NONE) { in btrfs_use_block_group_size_class()
4643 return -EAGAIN; in btrfs_use_block_group_size_class()
4646 * The happy new block group case: the new allocation is the first in btrfs_use_block_group_size_class()
4649 bg->size_class = size_class; in btrfs_use_block_group_size_class()
4656 if (btrfs_is_zoned(bg->fs_info)) in btrfs_block_group_should_use_size_class()