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
200 ASSERT(block_group->length != 0); in btrfs_add_block_group_cache()
202 write_lock(&info->block_group_cache_lock); in btrfs_add_block_group_cache()
204 exist = rb_find_add_cached(&block_group->cache_node, in btrfs_add_block_group_cache()
205 &info->block_group_cache_tree, btrfs_bg_start_cmp); in btrfs_add_block_group_cache()
207 ret = -EEXIST; in btrfs_add_block_group_cache()
208 write_unlock(&info->block_group_cache_lock); in btrfs_add_block_group_cache()
214 * This will return the block group at or after bytenr if contains is 0, else
215 * it will return the block group that contains the bytenr
220 struct btrfs_block_group *cache, *ret = NULL; in block_group_cache_tree_search() local
224 read_lock(&info->block_group_cache_lock); in block_group_cache_tree_search()
225 n = info->block_group_cache_tree.rb_root.rb_node; in block_group_cache_tree_search()
228 cache = rb_entry(n, struct btrfs_block_group, cache_node); in block_group_cache_tree_search()
229 end = cache->start + cache->length - 1; in block_group_cache_tree_search()
230 start = cache->start; in block_group_cache_tree_search()
233 if (!contains && (!ret || start < ret->start)) in block_group_cache_tree_search()
234 ret = cache; in block_group_cache_tree_search()
235 n = n->rb_left; in block_group_cache_tree_search()
238 ret = cache; in block_group_cache_tree_search()
241 n = n->rb_right; in block_group_cache_tree_search()
243 ret = cache; in block_group_cache_tree_search()
249 read_unlock(&info->block_group_cache_lock); in block_group_cache_tree_search()
255 * Return the block group that starts at or after bytenr
264 * Return the block group that contains the given bytenr
273 struct btrfs_block_group *cache) in btrfs_next_block_group() argument
275 struct btrfs_fs_info *fs_info = cache->fs_info; in btrfs_next_block_group()
278 read_lock(&fs_info->block_group_cache_lock); in btrfs_next_block_group()
280 /* If our block group was removed, we need a full search. */ in btrfs_next_block_group()
281 if (RB_EMPTY_NODE(&cache->cache_node)) { in btrfs_next_block_group()
282 const u64 next_bytenr = cache->start + cache->length; in btrfs_next_block_group()
284 read_unlock(&fs_info->block_group_cache_lock); in btrfs_next_block_group()
285 btrfs_put_block_group(cache); in btrfs_next_block_group()
288 node = rb_next(&cache->cache_node); in btrfs_next_block_group()
289 btrfs_put_block_group(cache); in btrfs_next_block_group()
291 cache = rb_entry(node, struct btrfs_block_group, cache_node); in btrfs_next_block_group()
292 btrfs_get_block_group(cache); in btrfs_next_block_group()
294 cache = NULL; in btrfs_next_block_group()
295 read_unlock(&fs_info->block_group_cache_lock); in btrfs_next_block_group()
296 return cache; in btrfs_next_block_group()
306 * number of NOCOW writers in the block group that contains the extent, as long
307 * as the block group exists and it's currently not in read-only mode.
309 * Returns: A non-NULL block group pointer if we can do a NOCOW write, the caller
324 spin_lock(&bg->lock); in btrfs_inc_nocow_writers()
325 if (bg->ro) in btrfs_inc_nocow_writers()
328 atomic_inc(&bg->nocow_writers); in btrfs_inc_nocow_writers()
329 spin_unlock(&bg->lock); in btrfs_inc_nocow_writers()
336 /* No put on block group, done by btrfs_dec_nocow_writers(). */ in btrfs_inc_nocow_writers()
341 * Decrement the number of NOCOW writers in a block group.
344 * and on the block group returned by that call. Typically this is called after
348 * After this call, the caller should not use the block group anymore. It it wants
353 if (atomic_dec_and_test(&bg->nocow_writers)) in btrfs_dec_nocow_writers()
354 wake_up_var(&bg->nocow_writers); in btrfs_dec_nocow_writers()
362 wait_var_event(&bg->nocow_writers, !atomic_read(&bg->nocow_writers)); in btrfs_wait_nocow_writers()
372 if (atomic_dec_and_test(&bg->reservations)) in btrfs_dec_block_group_reservations()
373 wake_up_var(&bg->reservations); in btrfs_dec_block_group_reservations()
379 struct btrfs_space_info *space_info = bg->space_info; in btrfs_wait_block_group_reservations()
381 ASSERT(bg->ro); in btrfs_wait_block_group_reservations()
383 if (!(bg->flags & BTRFS_BLOCK_GROUP_DATA)) in btrfs_wait_block_group_reservations()
387 * Our block group is read only but before we set it to read only, in btrfs_wait_block_group_reservations()
392 * block group's reservations counter is incremented while a read lock in btrfs_wait_block_group_reservations()
396 down_write(&space_info->groups_sem); in btrfs_wait_block_group_reservations()
397 up_write(&space_info->groups_sem); in btrfs_wait_block_group_reservations()
399 wait_var_event(&bg->reservations, !atomic_read(&bg->reservations)); in btrfs_wait_block_group_reservations()
403 struct btrfs_block_group *cache) in btrfs_get_caching_control() argument
407 spin_lock(&cache->lock); in btrfs_get_caching_control()
408 if (!cache->caching_ctl) { in btrfs_get_caching_control()
409 spin_unlock(&cache->lock); in btrfs_get_caching_control()
413 ctl = cache->caching_ctl; in btrfs_get_caching_control()
414 refcount_inc(&ctl->count); in btrfs_get_caching_control()
415 spin_unlock(&cache->lock); in btrfs_get_caching_control()
421 if (refcount_dec_and_test(&ctl->count)) in btrfs_put_caching_control()
426 * When we wait for progress in the block group caching, its because our
431 * up, and then it will check the block group free space numbers for our min
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()
634 * Best effort attempt to compute a block group's size class while caching it.
636 * @block_group: the block group we are caching
643 * them at even steps through the block group and pick the smallest size class
649 * If we are caching in a block group from disk, then there are three major cases
651 * 1. the block group is well behaved and all extents in it are the same size
653 * 2. the block group is mostly one size class with rare exceptions for last
655 * 3. the block group was populated before size classes and can have a totally
658 * In case 1, looking at any extent in the block group will yield the correct
670 struct btrfs_fs_info *fs_info = block_group->fs_info; in load_block_group_size_class()
673 u64 min_size = block_group->length; in load_block_group_size_class()
680 lockdep_assert_held(&caching_ctl->mutex); in load_block_group_size_class()
681 lockdep_assert_held_read(&fs_info->commit_root_sem); in load_block_group_size_class()
692 spin_lock(&block_group->lock); in load_block_group_size_class()
693 block_group->size_class = size_class; in load_block_group_size_class()
694 spin_unlock(&block_group->lock); in load_block_group_size_class()
702 struct btrfs_block_group *block_group = caching_ctl->block_group; in load_extent_tree_free()
703 struct btrfs_fs_info *fs_info = block_group->fs_info; in load_extent_tree_free()
716 return -ENOMEM; in load_extent_tree_free()
718 last = max_t(u64, block_group->start, BTRFS_SUPER_INFO_OFFSET); in load_extent_tree_free()
724 * allocate from this block group until we've had a chance to fragment in load_extent_tree_free()
734 * root, since its read-only in load_extent_tree_free()
736 path->skip_locking = 1; in load_extent_tree_free()
737 path->search_commit_root = 1; in load_extent_tree_free()
738 path->reada = READA_FORWARD; in load_extent_tree_free()
749 leaf = path->nodes[0]; in load_extent_tree_free()
754 last = (u64)-1; in load_extent_tree_free()
758 if (path->slots[0] < nritems) { in load_extent_tree_free()
759 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); in load_extent_tree_free()
766 rwsem_is_contended(&fs_info->commit_root_sem)) { in load_extent_tree_free()
768 up_read(&fs_info->commit_root_sem); in load_extent_tree_free()
769 mutex_unlock(&caching_ctl->mutex); in load_extent_tree_free()
771 mutex_lock(&caching_ctl->mutex); in load_extent_tree_free()
772 down_read(&fs_info->commit_root_sem); in load_extent_tree_free()
781 leaf = path->nodes[0]; in load_extent_tree_free()
794 if (key.objectid < block_group->start) { in load_extent_tree_free()
795 path->slots[0]++; in load_extent_tree_free()
799 if (key.objectid >= block_group->start + block_group->length) in load_extent_tree_free()
813 fs_info->nodesize; in load_extent_tree_free()
820 atomic_inc(&caching_ctl->progress); in load_extent_tree_free()
821 wake_up(&caching_ctl->wait); in load_extent_tree_free()
825 path->slots[0]++; in load_extent_tree_free()
829 block_group->start + block_group->length, in load_extent_tree_free()
838 clear_extent_bits(&bg->fs_info->excluded_extents, bg->start, in btrfs_free_excluded_extents()
839 bg->start + bg->length - 1, EXTENT_UPTODATE); in btrfs_free_excluded_extents()
850 block_group = caching_ctl->block_group; in caching_thread()
851 fs_info = block_group->fs_info; in caching_thread()
853 mutex_lock(&caching_ctl->mutex); in caching_thread()
854 down_read(&fs_info->commit_root_sem); in caching_thread()
865 * We failed to load the space cache, set ourselves to in caching_thread()
868 spin_lock(&block_group->lock); in caching_thread()
869 block_group->cached = BTRFS_CACHE_STARTED; in caching_thread()
870 spin_unlock(&block_group->lock); in caching_thread()
871 wake_up(&caching_ctl->wait); in caching_thread()
876 * can't actually cache from the free space tree as our commit root and in caching_thread()
882 !(test_bit(BTRFS_FS_FREE_SPACE_TREE_UNTRUSTED, &fs_info->flags))) in caching_thread()
887 spin_lock(&block_group->lock); in caching_thread()
888 block_group->caching_ctl = NULL; in caching_thread()
889 block_group->cached = ret ? BTRFS_CACHE_ERROR : BTRFS_CACHE_FINISHED; in caching_thread()
890 spin_unlock(&block_group->lock); in caching_thread()
896 spin_lock(&block_group->space_info->lock); in caching_thread()
897 spin_lock(&block_group->lock); in caching_thread()
898 bytes_used = block_group->length - block_group->used; in caching_thread()
899 block_group->space_info->bytes_used += bytes_used >> 1; in caching_thread()
900 spin_unlock(&block_group->lock); in caching_thread()
901 spin_unlock(&block_group->space_info->lock); in caching_thread()
906 up_read(&fs_info->commit_root_sem); in caching_thread()
908 mutex_unlock(&caching_ctl->mutex); in caching_thread()
910 wake_up(&caching_ctl->wait); in caching_thread()
916 int btrfs_cache_block_group(struct btrfs_block_group *cache, bool wait) in btrfs_cache_block_group() argument
918 struct btrfs_fs_info *fs_info = cache->fs_info; in btrfs_cache_block_group()
922 /* Allocator for zoned filesystems does not use the cache at all */ in btrfs_cache_block_group()
928 return -ENOMEM; in btrfs_cache_block_group()
930 INIT_LIST_HEAD(&caching_ctl->list); in btrfs_cache_block_group()
931 mutex_init(&caching_ctl->mutex); in btrfs_cache_block_group()
932 init_waitqueue_head(&caching_ctl->wait); in btrfs_cache_block_group()
933 caching_ctl->block_group = cache; in btrfs_cache_block_group()
934 refcount_set(&caching_ctl->count, 2); in btrfs_cache_block_group()
935 atomic_set(&caching_ctl->progress, 0); in btrfs_cache_block_group()
936 btrfs_init_work(&caching_ctl->work, caching_thread, NULL); in btrfs_cache_block_group()
938 spin_lock(&cache->lock); in btrfs_cache_block_group()
939 if (cache->cached != BTRFS_CACHE_NO) { in btrfs_cache_block_group()
942 caching_ctl = cache->caching_ctl; in btrfs_cache_block_group()
944 refcount_inc(&caching_ctl->count); in btrfs_cache_block_group()
945 spin_unlock(&cache->lock); in btrfs_cache_block_group()
948 WARN_ON(cache->caching_ctl); in btrfs_cache_block_group()
949 cache->caching_ctl = caching_ctl; in btrfs_cache_block_group()
950 cache->cached = BTRFS_CACHE_STARTED; in btrfs_cache_block_group()
951 spin_unlock(&cache->lock); in btrfs_cache_block_group()
953 write_lock(&fs_info->block_group_cache_lock); in btrfs_cache_block_group()
954 refcount_inc(&caching_ctl->count); in btrfs_cache_block_group()
955 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups); in btrfs_cache_block_group()
956 write_unlock(&fs_info->block_group_cache_lock); in btrfs_cache_block_group()
958 btrfs_get_block_group(cache); in btrfs_cache_block_group()
960 btrfs_queue_work(fs_info->caching_workers, &caching_ctl->work); in btrfs_cache_block_group()
963 ret = btrfs_caching_ctl_wait_done(cache, caching_ctl); in btrfs_cache_block_group()
975 write_seqlock(&fs_info->profiles_lock); in clear_avail_alloc_bits()
977 fs_info->avail_data_alloc_bits &= ~extra_flags; in clear_avail_alloc_bits()
979 fs_info->avail_metadata_alloc_bits &= ~extra_flags; in clear_avail_alloc_bits()
981 fs_info->avail_system_alloc_bits &= ~extra_flags; in clear_avail_alloc_bits()
982 write_sequnlock(&fs_info->profiles_lock); in clear_avail_alloc_bits()
988 * - RAID56 - in case there's neither RAID5 nor RAID6 profile block group
991 * - RAID1C34 - same as above for RAID1C3 and RAID1C4 block groups
1001 struct list_head *head = &fs_info->space_info; in clear_incompat_bg_bits()
1005 down_read(&sinfo->groups_sem); in clear_incompat_bg_bits()
1006 if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID5])) in clear_incompat_bg_bits()
1008 if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID6])) in clear_incompat_bg_bits()
1010 if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID1C3])) in clear_incompat_bg_bits()
1012 if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID1C4])) in clear_incompat_bg_bits()
1014 up_read(&sinfo->groups_sem); in clear_incompat_bg_bits()
1026 return fs_info->block_group_root; in btrfs_block_group_root()
1034 struct btrfs_fs_info *fs_info = trans->fs_info; in remove_block_group_item()
1040 key.objectid = block_group->start; in remove_block_group_item()
1042 key.offset = block_group->length; in remove_block_group_item()
1044 ret = btrfs_search_slot(trans, root, &key, path, -1, 1); in remove_block_group_item()
1046 ret = -ENOENT; in remove_block_group_item()
1057 struct btrfs_fs_info *fs_info = trans->fs_info; in btrfs_remove_block_group()
1070 block_group = btrfs_lookup_block_group(fs_info, map->start); in btrfs_remove_block_group()
1072 return -ENOENT; in btrfs_remove_block_group()
1074 BUG_ON(!block_group->ro); in btrfs_remove_block_group()
1078 * Free the reserved super bytes from this block group before in btrfs_remove_block_group()
1082 btrfs_free_ref_tree_range(fs_info, block_group->start, in btrfs_remove_block_group()
1083 block_group->length); in btrfs_remove_block_group()
1085 index = btrfs_bg_flags_to_raid_index(block_group->flags); in btrfs_remove_block_group()
1086 factor = btrfs_bg_type_to_factor(block_group->flags); in btrfs_remove_block_group()
1088 /* make sure this block group isn't part of an allocation cluster */ in btrfs_remove_block_group()
1089 cluster = &fs_info->data_alloc_cluster; in btrfs_remove_block_group()
1090 spin_lock(&cluster->refill_lock); in btrfs_remove_block_group()
1092 spin_unlock(&cluster->refill_lock); in btrfs_remove_block_group()
1095 * make sure this block group isn't part of a metadata in btrfs_remove_block_group()
1098 cluster = &fs_info->meta_alloc_cluster; in btrfs_remove_block_group()
1099 spin_lock(&cluster->refill_lock); in btrfs_remove_block_group()
1101 spin_unlock(&cluster->refill_lock); in btrfs_remove_block_group()
1108 ret = -ENOMEM; in btrfs_remove_block_group()
1118 mutex_lock(&trans->transaction->cache_write_mutex); in btrfs_remove_block_group()
1120 * Make sure our free space cache IO is done before removing the in btrfs_remove_block_group()
1123 spin_lock(&trans->transaction->dirty_bgs_lock); in btrfs_remove_block_group()
1124 if (!list_empty(&block_group->io_list)) { in btrfs_remove_block_group()
1125 list_del_init(&block_group->io_list); in btrfs_remove_block_group()
1127 WARN_ON(!IS_ERR(inode) && inode != block_group->io_ctl.inode); in btrfs_remove_block_group()
1129 spin_unlock(&trans->transaction->dirty_bgs_lock); in btrfs_remove_block_group()
1132 spin_lock(&trans->transaction->dirty_bgs_lock); in btrfs_remove_block_group()
1135 if (!list_empty(&block_group->dirty_list)) { in btrfs_remove_block_group()
1136 list_del_init(&block_group->dirty_list); in btrfs_remove_block_group()
1140 spin_unlock(&trans->transaction->dirty_bgs_lock); in btrfs_remove_block_group()
1141 mutex_unlock(&trans->transaction->cache_write_mutex); in btrfs_remove_block_group()
1147 write_lock(&fs_info->block_group_cache_lock); in btrfs_remove_block_group()
1148 rb_erase_cached(&block_group->cache_node, in btrfs_remove_block_group()
1149 &fs_info->block_group_cache_tree); in btrfs_remove_block_group()
1150 RB_CLEAR_NODE(&block_group->cache_node); in btrfs_remove_block_group()
1152 /* Once for the block groups rbtree */ in btrfs_remove_block_group()
1155 write_unlock(&fs_info->block_group_cache_lock); in btrfs_remove_block_group()
1157 down_write(&block_group->space_info->groups_sem); in btrfs_remove_block_group()
1162 list_del_init(&block_group->list); in btrfs_remove_block_group()
1163 if (list_empty(&block_group->space_info->block_groups[index])) { in btrfs_remove_block_group()
1164 kobj = block_group->space_info->block_group_kobjs[index]; in btrfs_remove_block_group()
1165 block_group->space_info->block_group_kobjs[index] = NULL; in btrfs_remove_block_group()
1166 clear_avail_alloc_bits(fs_info, block_group->flags); in btrfs_remove_block_group()
1168 up_write(&block_group->space_info->groups_sem); in btrfs_remove_block_group()
1169 clear_incompat_bg_bits(fs_info, block_group->flags); in btrfs_remove_block_group()
1175 if (block_group->cached == BTRFS_CACHE_STARTED) in btrfs_remove_block_group()
1178 write_lock(&fs_info->block_group_cache_lock); in btrfs_remove_block_group()
1183 list_for_each_entry(ctl, &fs_info->caching_block_groups, list) { in btrfs_remove_block_group()
1184 if (ctl->block_group == block_group) { in btrfs_remove_block_group()
1186 refcount_inc(&caching_ctl->count); in btrfs_remove_block_group()
1192 list_del_init(&caching_ctl->list); in btrfs_remove_block_group()
1193 write_unlock(&fs_info->block_group_cache_lock); in btrfs_remove_block_group()
1201 spin_lock(&trans->transaction->dirty_bgs_lock); in btrfs_remove_block_group()
1202 WARN_ON(!list_empty(&block_group->dirty_list)); in btrfs_remove_block_group()
1203 WARN_ON(!list_empty(&block_group->io_list)); in btrfs_remove_block_group()
1204 spin_unlock(&trans->transaction->dirty_bgs_lock); in btrfs_remove_block_group()
1208 spin_lock(&block_group->space_info->lock); in btrfs_remove_block_group()
1209 list_del_init(&block_group->ro_list); in btrfs_remove_block_group()
1212 WARN_ON(block_group->space_info->total_bytes in btrfs_remove_block_group()
1213 < block_group->length); in btrfs_remove_block_group()
1214 WARN_ON(block_group->space_info->bytes_readonly in btrfs_remove_block_group()
1215 < block_group->length - block_group->zone_unusable); in btrfs_remove_block_group()
1216 WARN_ON(block_group->space_info->bytes_zone_unusable in btrfs_remove_block_group()
1217 < block_group->zone_unusable); in btrfs_remove_block_group()
1218 WARN_ON(block_group->space_info->disk_total in btrfs_remove_block_group()
1219 < block_group->length * factor); in btrfs_remove_block_group()
1221 block_group->space_info->total_bytes -= block_group->length; in btrfs_remove_block_group()
1222 block_group->space_info->bytes_readonly -= in btrfs_remove_block_group()
1223 (block_group->length - block_group->zone_unusable); in btrfs_remove_block_group()
1224 btrfs_space_info_update_bytes_zone_unusable(block_group->space_info, in btrfs_remove_block_group()
1225 -block_group->zone_unusable); in btrfs_remove_block_group()
1226 block_group->space_info->disk_total -= block_group->length * factor; in btrfs_remove_block_group()
1228 spin_unlock(&block_group->space_info->lock); in btrfs_remove_block_group()
1231 * Remove the free space for the block group from the free space tree in btrfs_remove_block_group()
1232 * and the block group's item from the extent tree before marking the in btrfs_remove_block_group()
1233 * block group as removed. This is to prevent races with tasks that in btrfs_remove_block_group()
1234 * freeze and unfreeze a block group, this task and another task in btrfs_remove_block_group()
1235 * allocating a new block group - the unfreeze task ends up removing in btrfs_remove_block_group()
1236 * the block group's extent map before the task calling this function in btrfs_remove_block_group()
1237 * deletes the block group item from the extent tree, allowing for in btrfs_remove_block_group()
1238 * another task to attempt to create another block group with the same in btrfs_remove_block_group()
1239 * item key (and failing with -EEXIST and a transaction abort). in btrfs_remove_block_group()
1249 spin_lock(&block_group->lock); in btrfs_remove_block_group()
1250 set_bit(BLOCK_GROUP_FLAG_REMOVED, &block_group->runtime_flags); in btrfs_remove_block_group()
1253 * At this point trimming or scrub can't start on this block group, in btrfs_remove_block_group()
1254 * because we removed the block group from the rbtree in btrfs_remove_block_group()
1255 * fs_info->block_group_cache_tree so no one can't find it anymore and in btrfs_remove_block_group()
1256 * even if someone already got this block group before we removed it in btrfs_remove_block_group()
1257 * from the rbtree, they have already incremented block_group->frozen - in btrfs_remove_block_group()
1262 * And we must not remove the chunk map from the fs_info->mapping_tree in btrfs_remove_block_group()
1264 * ranges from being reused for a new block group. This is needed to in btrfs_remove_block_group()
1270 * allowing for new block groups to be created that can reuse the same in btrfs_remove_block_group()
1274 * is mounted with -odiscard. The same protections must remain in btrfs_remove_block_group()
1278 remove_map = (atomic_read(&block_group->frozen) == 0); in btrfs_remove_block_group()
1279 spin_unlock(&block_group->lock); in btrfs_remove_block_group()
1302 ASSERT(map->start == chunk_offset); in btrfs_start_trans_remove_block_group()
1306 * to remove a block group (done at btrfs_remove_chunk() and at in btrfs_start_trans_remove_block_group()
1311 * 1 unit for deleting the block group item (located in the extent in btrfs_start_trans_remove_block_group()
1318 * In order to remove a block group we also need to reserve units in the in btrfs_start_trans_remove_block_group()
1323 num_items = 3 + map->num_stripes; in btrfs_start_trans_remove_block_group()
1330 * Mark block group @cache read-only, so later write won't happen to block
1331 * group @cache.
1333 * If @force is not set, this function will only mark the block group readonly
1334 * if we have enough free space (1M) in other metadata/system block groups.
1335 * If @force is not set, this function will mark the block group readonly
1338 * NOTE: This function doesn't care if other block groups can contain all the
1339 * data in this block group. That check should be done by relocation routine,
1342 static int inc_block_group_ro(struct btrfs_block_group *cache, int force) in inc_block_group_ro() argument
1344 struct btrfs_space_info *sinfo = cache->space_info; in inc_block_group_ro()
1346 int ret = -ENOSPC; in inc_block_group_ro()
1348 spin_lock(&sinfo->lock); in inc_block_group_ro()
1349 spin_lock(&cache->lock); in inc_block_group_ro()
1351 if (cache->swap_extents) { in inc_block_group_ro()
1352 ret = -ETXTBSY; in inc_block_group_ro()
1356 if (cache->ro) { in inc_block_group_ro()
1357 cache->ro++; in inc_block_group_ro()
1362 num_bytes = cache->length - cache->reserved - cache->pinned - in inc_block_group_ro()
1363 cache->bytes_super - cache->zone_unusable - cache->used; in inc_block_group_ro()
1371 } else if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA) { in inc_block_group_ro()
1378 if (sinfo_used + num_bytes <= sinfo->total_bytes) in inc_block_group_ro()
1385 * leeway to allow us to mark this block group as read only. in inc_block_group_ro()
1387 if (btrfs_can_overcommit(cache->fs_info, sinfo, num_bytes, in inc_block_group_ro()
1393 sinfo->bytes_readonly += num_bytes; in inc_block_group_ro()
1394 if (btrfs_is_zoned(cache->fs_info)) { in inc_block_group_ro()
1396 sinfo->bytes_readonly += cache->zone_unusable; in inc_block_group_ro()
1397 btrfs_space_info_update_bytes_zone_unusable(sinfo, -cache->zone_unusable); in inc_block_group_ro()
1398 cache->zone_unusable = 0; in inc_block_group_ro()
1400 cache->ro++; in inc_block_group_ro()
1401 list_add_tail(&cache->ro_list, &sinfo->ro_bgs); in inc_block_group_ro()
1404 spin_unlock(&cache->lock); in inc_block_group_ro()
1405 spin_unlock(&sinfo->lock); in inc_block_group_ro()
1406 if (ret == -ENOSPC && btrfs_test_opt(cache->fs_info, ENOSPC_DEBUG)) { in inc_block_group_ro()
1407 btrfs_info(cache->fs_info, in inc_block_group_ro()
1408 "unable to make block group %llu ro", cache->start); in inc_block_group_ro()
1409 btrfs_dump_space_info(cache->fs_info, cache->space_info, 0, 0); in inc_block_group_ro()
1417 struct btrfs_fs_info *fs_info = trans->fs_info; in clean_pinned_extents()
1419 const u64 start = bg->start; in clean_pinned_extents()
1420 const u64 end = start + bg->length - 1; in clean_pinned_extents()
1423 spin_lock(&fs_info->trans_lock); in clean_pinned_extents()
1424 if (trans->transaction->list.prev != &fs_info->trans_list) { in clean_pinned_extents()
1425 prev_trans = list_last_entry(&trans->transaction->list, in clean_pinned_extents()
1427 refcount_inc(&prev_trans->use_count); in clean_pinned_extents()
1429 spin_unlock(&fs_info->trans_lock); in clean_pinned_extents()
1435 * transaction N - 1, and have seen a range belonging to the block in clean_pinned_extents()
1436 * group in pinned_extents before we were able to clear the whole block in clean_pinned_extents()
1438 * the block group after we unpinned it from pinned_extents and removed in clean_pinned_extents()
1441 mutex_lock(&fs_info->unused_bg_unpin_mutex); in clean_pinned_extents()
1443 ret = clear_extent_bits(&prev_trans->pinned_extents, start, end, in clean_pinned_extents()
1449 ret = clear_extent_bits(&trans->transaction->pinned_extents, start, end, in clean_pinned_extents()
1452 mutex_unlock(&fs_info->unused_bg_unpin_mutex); in clean_pinned_extents()
1472 if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags)) in btrfs_delete_unused_bgs()
1482 if (!mutex_trylock(&fs_info->reclaim_bgs_lock)) in btrfs_delete_unused_bgs()
1485 spin_lock(&fs_info->unused_bgs_lock); in btrfs_delete_unused_bgs()
1486 while (!list_empty(&fs_info->unused_bgs)) { in btrfs_delete_unused_bgs()
1490 block_group = list_first_entry(&fs_info->unused_bgs, in btrfs_delete_unused_bgs()
1493 list_del_init(&block_group->bg_list); in btrfs_delete_unused_bgs()
1495 space_info = block_group->space_info; in btrfs_delete_unused_bgs()
1501 spin_unlock(&fs_info->unused_bgs_lock); in btrfs_delete_unused_bgs()
1503 btrfs_discard_cancel_work(&fs_info->discard_ctl, block_group); in btrfs_delete_unused_bgs()
1506 down_write(&space_info->groups_sem); in btrfs_delete_unused_bgs()
1509 * Async discard moves the final block group discard to be prior in btrfs_delete_unused_bgs()
1516 up_write(&space_info->groups_sem); in btrfs_delete_unused_bgs()
1518 btrfs_discard_queue_work(&fs_info->discard_ctl, in btrfs_delete_unused_bgs()
1523 spin_lock(&space_info->lock); in btrfs_delete_unused_bgs()
1524 spin_lock(&block_group->lock); in btrfs_delete_unused_bgs()
1525 if (btrfs_is_block_group_used(block_group) || block_group->ro || in btrfs_delete_unused_bgs()
1526 list_is_singular(&block_group->list)) { in btrfs_delete_unused_bgs()
1529 * outstanding allocations in this block group. We do in btrfs_delete_unused_bgs()
1531 * this block group. in btrfs_delete_unused_bgs()
1533 * Also bail out if this is the only block group for its in btrfs_delete_unused_bgs()
1535 * information from fs_info->avail_*_alloc_bits and the in btrfs_delete_unused_bgs()
1536 * next block group of this type would be created with a in btrfs_delete_unused_bgs()
1538 * fs_info->avail_*_alloc_bits would be 0. in btrfs_delete_unused_bgs()
1541 spin_unlock(&block_group->lock); in btrfs_delete_unused_bgs()
1542 spin_unlock(&space_info->lock); in btrfs_delete_unused_bgs()
1543 up_write(&space_info->groups_sem); in btrfs_delete_unused_bgs()
1548 * The block group may be unused but there may be space reserved in btrfs_delete_unused_bgs()
1549 * accounting with the existence of that block group, that is, in btrfs_delete_unused_bgs()
1550 * space_info->bytes_may_use was incremented by a task but no in btrfs_delete_unused_bgs()
1551 * space was yet allocated from the block group by the task. in btrfs_delete_unused_bgs()
1559 * of this block group is less than the used space of the in btrfs_delete_unused_bgs()
1560 * space_info - if that's the case, then it means we have tasks in btrfs_delete_unused_bgs()
1561 * that might be relying on the block group in order to allocate in btrfs_delete_unused_bgs()
1562 * extents, and add back the block group to the unused list when in btrfs_delete_unused_bgs()
1564 * needing to allocate extents from the block group. in btrfs_delete_unused_bgs()
1567 if (space_info->total_bytes - block_group->length < used && in btrfs_delete_unused_bgs()
1568 block_group->zone_unusable < block_group->length) { in btrfs_delete_unused_bgs()
1572 * fs_info->unused_bgs list. in btrfs_delete_unused_bgs()
1575 list_add_tail(&block_group->bg_list, &retry_list); in btrfs_delete_unused_bgs()
1578 spin_unlock(&block_group->lock); in btrfs_delete_unused_bgs()
1579 spin_unlock(&space_info->lock); in btrfs_delete_unused_bgs()
1580 up_write(&space_info->groups_sem); in btrfs_delete_unused_bgs()
1584 spin_unlock(&block_group->lock); in btrfs_delete_unused_bgs()
1585 spin_unlock(&space_info->lock); in btrfs_delete_unused_bgs()
1589 up_write(&space_info->groups_sem); in btrfs_delete_unused_bgs()
1598 if (ret == -EAGAIN) in btrfs_delete_unused_bgs()
1608 block_group->start); in btrfs_delete_unused_bgs()
1616 * We could have pending pinned extents for this block group, in btrfs_delete_unused_bgs()
1631 spin_lock(&fs_info->discard_ctl.lock); in btrfs_delete_unused_bgs()
1632 if (!list_empty(&block_group->discard_list)) { in btrfs_delete_unused_bgs()
1633 spin_unlock(&fs_info->discard_ctl.lock); in btrfs_delete_unused_bgs()
1635 btrfs_discard_queue_work(&fs_info->discard_ctl, in btrfs_delete_unused_bgs()
1639 spin_unlock(&fs_info->discard_ctl.lock); in btrfs_delete_unused_bgs()
1642 spin_lock(&space_info->lock); in btrfs_delete_unused_bgs()
1643 spin_lock(&block_group->lock); in btrfs_delete_unused_bgs()
1645 btrfs_space_info_update_bytes_pinned(space_info, -block_group->pinned); in btrfs_delete_unused_bgs()
1646 space_info->bytes_readonly += block_group->pinned; in btrfs_delete_unused_bgs()
1647 block_group->pinned = 0; in btrfs_delete_unused_bgs()
1649 spin_unlock(&block_group->lock); in btrfs_delete_unused_bgs()
1650 spin_unlock(&space_info->lock); in btrfs_delete_unused_bgs()
1653 * The normal path here is an unused block group is passed here, in btrfs_delete_unused_bgs()
1656 * before coming down the unused block group path as trimming in btrfs_delete_unused_bgs()
1664 * need to reset sequential-required zones. in btrfs_delete_unused_bgs()
1677 ret = btrfs_remove_chunk(trans, block_group->start); in btrfs_delete_unused_bgs()
1686 * If we're not mounted with -odiscard, we can just forget in btrfs_delete_unused_bgs()
1687 * about this block group. Otherwise we'll need to wait in btrfs_delete_unused_bgs()
1691 spin_lock(&fs_info->unused_bgs_lock); in btrfs_delete_unused_bgs()
1694 * fs_info->unused_bgs, so use a list_move operation in btrfs_delete_unused_bgs()
1695 * to add the block group to the deleted_bgs list. in btrfs_delete_unused_bgs()
1697 list_move(&block_group->bg_list, in btrfs_delete_unused_bgs()
1698 &trans->transaction->deleted_bgs); in btrfs_delete_unused_bgs()
1699 spin_unlock(&fs_info->unused_bgs_lock); in btrfs_delete_unused_bgs()
1706 spin_lock(&fs_info->unused_bgs_lock); in btrfs_delete_unused_bgs()
1708 list_splice_tail(&retry_list, &fs_info->unused_bgs); in btrfs_delete_unused_bgs()
1709 spin_unlock(&fs_info->unused_bgs_lock); in btrfs_delete_unused_bgs()
1710 mutex_unlock(&fs_info->reclaim_bgs_lock); in btrfs_delete_unused_bgs()
1715 spin_lock(&fs_info->unused_bgs_lock); in btrfs_delete_unused_bgs()
1716 list_splice_tail(&retry_list, &fs_info->unused_bgs); in btrfs_delete_unused_bgs()
1717 spin_unlock(&fs_info->unused_bgs_lock); in btrfs_delete_unused_bgs()
1718 mutex_unlock(&fs_info->reclaim_bgs_lock); in btrfs_delete_unused_bgs()
1725 struct btrfs_fs_info *fs_info = bg->fs_info; in btrfs_mark_bg_unused()
1727 spin_lock(&fs_info->unused_bgs_lock); in btrfs_mark_bg_unused()
1728 if (list_empty(&bg->bg_list)) { in btrfs_mark_bg_unused()
1731 list_add_tail(&bg->bg_list, &fs_info->unused_bgs); in btrfs_mark_bg_unused()
1732 } else if (!test_bit(BLOCK_GROUP_FLAG_NEW, &bg->runtime_flags)) { in btrfs_mark_bg_unused()
1733 /* Pull out the block group from the reclaim_bgs list. */ in btrfs_mark_bg_unused()
1735 list_move_tail(&bg->bg_list, &fs_info->unused_bgs); in btrfs_mark_bg_unused()
1737 spin_unlock(&fs_info->unused_bgs_lock); in btrfs_mark_bg_unused()
1741 * We want block groups with a low number of used bytes to be in the beginning
1752 return bg1->used > bg2->used; in reclaim_bgs_cmp()
1764 const int thresh_pct = btrfs_calc_reclaim_threshold(bg->space_info); in should_reclaim_block_group()
1765 u64 thresh_bytes = mult_perc(bg->length, thresh_pct); in should_reclaim_block_group()
1766 const u64 new_val = bg->used; in should_reclaim_block_group()
1774 * brand new block group and we don't want to relocate new block groups. in should_reclaim_block_group()
1791 if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags)) in btrfs_reclaim_bgs_work()
1800 sb_start_write(fs_info->sb); in btrfs_reclaim_bgs_work()
1803 sb_end_write(fs_info->sb); in btrfs_reclaim_bgs_work()
1811 if (!mutex_trylock(&fs_info->reclaim_bgs_lock)) { in btrfs_reclaim_bgs_work()
1813 sb_end_write(fs_info->sb); in btrfs_reclaim_bgs_work()
1817 spin_lock(&fs_info->unused_bgs_lock); in btrfs_reclaim_bgs_work()
1820 * The block groups might still be in use and reachable via bg_list, in btrfs_reclaim_bgs_work()
1823 list_sort(NULL, &fs_info->reclaim_bgs, reclaim_bgs_cmp); in btrfs_reclaim_bgs_work()
1824 while (!list_empty(&fs_info->reclaim_bgs)) { in btrfs_reclaim_bgs_work()
1830 bg = list_first_entry(&fs_info->reclaim_bgs, in btrfs_reclaim_bgs_work()
1833 list_del_init(&bg->bg_list); in btrfs_reclaim_bgs_work()
1835 space_info = bg->space_info; in btrfs_reclaim_bgs_work()
1836 spin_unlock(&fs_info->unused_bgs_lock); in btrfs_reclaim_bgs_work()
1839 down_write(&space_info->groups_sem); in btrfs_reclaim_bgs_work()
1841 spin_lock(&space_info->lock); in btrfs_reclaim_bgs_work()
1842 spin_lock(&bg->lock); in btrfs_reclaim_bgs_work()
1843 if (bg->reserved || bg->pinned || bg->ro) { in btrfs_reclaim_bgs_work()
1846 * outstanding allocations in this block group. We do in btrfs_reclaim_bgs_work()
1848 * this block group. in btrfs_reclaim_bgs_work()
1850 spin_unlock(&bg->lock); in btrfs_reclaim_bgs_work()
1851 spin_unlock(&space_info->lock); in btrfs_reclaim_bgs_work()
1852 up_write(&space_info->groups_sem); in btrfs_reclaim_bgs_work()
1855 if (bg->used == 0) { in btrfs_reclaim_bgs_work()
1857 * It is possible that we trigger relocation on a block in btrfs_reclaim_bgs_work()
1863 * for the non-existent extents and running some extra in btrfs_reclaim_bgs_work()
1865 * other mechanisms for dealing with empty block groups. in btrfs_reclaim_bgs_work()
1869 spin_unlock(&bg->lock); in btrfs_reclaim_bgs_work()
1870 spin_unlock(&space_info->lock); in btrfs_reclaim_bgs_work()
1871 up_write(&space_info->groups_sem); in btrfs_reclaim_bgs_work()
1876 * The block group might no longer meet the reclaim condition by in btrfs_reclaim_bgs_work()
1885 if (!should_reclaim_block_group(bg, bg->length)) { in btrfs_reclaim_bgs_work()
1886 spin_unlock(&bg->lock); in btrfs_reclaim_bgs_work()
1887 spin_unlock(&space_info->lock); in btrfs_reclaim_bgs_work()
1888 up_write(&space_info->groups_sem); in btrfs_reclaim_bgs_work()
1891 spin_unlock(&bg->lock); in btrfs_reclaim_bgs_work()
1892 spin_unlock(&space_info->lock); in btrfs_reclaim_bgs_work()
1895 * Get out fast, in case we're read-only or unmounting the in btrfs_reclaim_bgs_work()
1896 * filesystem. It is OK to drop block groups from the list even in btrfs_reclaim_bgs_work()
1897 * for the read-only case. As we did sb_start_write(), in btrfs_reclaim_bgs_work()
1898 * "mount -o remount,ro" won't happen and read-only filesystem in btrfs_reclaim_bgs_work()
1899 * means it is forced read-only due to a fatal error. So, it in btrfs_reclaim_bgs_work()
1900 * never gets back to read-write to let us reclaim again. in btrfs_reclaim_bgs_work()
1903 up_write(&space_info->groups_sem); in btrfs_reclaim_bgs_work()
1908 * Cache the zone_unusable value before turning the block group in btrfs_reclaim_bgs_work()
1910 * zone_unusable value gets moved to the block group's read-only in btrfs_reclaim_bgs_work()
1913 zone_unusable = bg->zone_unusable; in btrfs_reclaim_bgs_work()
1915 up_write(&space_info->groups_sem); in btrfs_reclaim_bgs_work()
1921 * "used" and "reserved" counters. We have set the block group in btrfs_reclaim_bgs_work()
1924 * not yet been done - btrfs_update_block_group() was not yet in btrfs_reclaim_bgs_work()
1926 * size from the "reserved" counter to the "used" counter - this in btrfs_reclaim_bgs_work()
1932 * it does the actual work to move extents out of the block in btrfs_reclaim_bgs_work()
1936 spin_lock(&bg->lock); in btrfs_reclaim_bgs_work()
1937 used = bg->used; in btrfs_reclaim_bgs_work()
1938 reserved = bg->reserved; in btrfs_reclaim_bgs_work()
1939 spin_unlock(&bg->lock); in btrfs_reclaim_bgs_work()
1943 bg->start, in btrfs_reclaim_bgs_work()
1944 div64_u64(used * 100, bg->length), in btrfs_reclaim_bgs_work()
1945 div64_u64(reserved * 100, bg->length), in btrfs_reclaim_bgs_work()
1946 div64_u64(zone_unusable * 100, bg->length)); in btrfs_reclaim_bgs_work()
1948 ret = btrfs_relocate_chunk(fs_info, bg->start); in btrfs_reclaim_bgs_work()
1952 bg->start); in btrfs_reclaim_bgs_work()
1955 spin_lock(&space_info->lock); in btrfs_reclaim_bgs_work()
1956 space_info->reclaim_errors++; in btrfs_reclaim_bgs_work()
1957 if (READ_ONCE(space_info->periodic_reclaim)) in btrfs_reclaim_bgs_work()
1958 space_info->periodic_reclaim_ready = false; in btrfs_reclaim_bgs_work()
1959 spin_unlock(&space_info->lock); in btrfs_reclaim_bgs_work()
1961 spin_lock(&space_info->lock); in btrfs_reclaim_bgs_work()
1962 space_info->reclaim_count++; in btrfs_reclaim_bgs_work()
1963 space_info->reclaim_bytes += used; in btrfs_reclaim_bgs_work()
1964 space_info->reclaim_bytes += reserved; in btrfs_reclaim_bgs_work()
1965 spin_unlock(&space_info->lock); in btrfs_reclaim_bgs_work()
1968 if (ret && !READ_ONCE(space_info->periodic_reclaim)) { in btrfs_reclaim_bgs_work()
1970 spin_lock(&fs_info->unused_bgs_lock); in btrfs_reclaim_bgs_work()
1972 * This block group might be added to the unused list in btrfs_reclaim_bgs_work()
1976 if (list_empty(&bg->bg_list)) { in btrfs_reclaim_bgs_work()
1978 list_add_tail(&bg->bg_list, &retry_list); in btrfs_reclaim_bgs_work()
1980 spin_unlock(&fs_info->unused_bgs_lock); in btrfs_reclaim_bgs_work()
1984 mutex_unlock(&fs_info->reclaim_bgs_lock); in btrfs_reclaim_bgs_work()
1986 * Reclaiming all the block groups in the list can take really in btrfs_reclaim_bgs_work()
1987 * long. Prioritize cleaning up unused block groups. in btrfs_reclaim_bgs_work()
1994 if (!mutex_trylock(&fs_info->reclaim_bgs_lock)) in btrfs_reclaim_bgs_work()
1996 spin_lock(&fs_info->unused_bgs_lock); in btrfs_reclaim_bgs_work()
1998 spin_unlock(&fs_info->unused_bgs_lock); in btrfs_reclaim_bgs_work()
1999 mutex_unlock(&fs_info->reclaim_bgs_lock); in btrfs_reclaim_bgs_work()
2001 spin_lock(&fs_info->unused_bgs_lock); in btrfs_reclaim_bgs_work()
2002 list_splice_tail(&retry_list, &fs_info->reclaim_bgs); in btrfs_reclaim_bgs_work()
2003 spin_unlock(&fs_info->unused_bgs_lock); in btrfs_reclaim_bgs_work()
2005 sb_end_write(fs_info->sb); in btrfs_reclaim_bgs_work()
2011 spin_lock(&fs_info->unused_bgs_lock); in btrfs_reclaim_bgs()
2012 if (!list_empty(&fs_info->reclaim_bgs)) in btrfs_reclaim_bgs()
2013 queue_work(system_unbound_wq, &fs_info->reclaim_bgs_work); in btrfs_reclaim_bgs()
2014 spin_unlock(&fs_info->unused_bgs_lock); in btrfs_reclaim_bgs()
2019 struct btrfs_fs_info *fs_info = bg->fs_info; in btrfs_mark_bg_to_reclaim()
2021 spin_lock(&fs_info->unused_bgs_lock); in btrfs_mark_bg_to_reclaim()
2022 if (list_empty(&bg->bg_list)) { in btrfs_mark_bg_to_reclaim()
2025 list_add_tail(&bg->bg_list, &fs_info->reclaim_bgs); in btrfs_mark_bg_to_reclaim()
2027 spin_unlock(&fs_info->unused_bgs_lock); in btrfs_mark_bg_to_reclaim()
2040 slot = path->slots[0]; in read_bg_from_eb()
2041 leaf = path->nodes[0]; in read_bg_from_eb()
2043 map = btrfs_find_chunk_map(fs_info, key->objectid, key->offset); in read_bg_from_eb()
2047 key->objectid, key->offset); in read_bg_from_eb()
2048 return -ENOENT; in read_bg_from_eb()
2051 if (map->start != key->objectid || map->chunk_len != key->offset) { in read_bg_from_eb()
2053 "block group %llu len %llu mismatch with chunk %llu len %llu", in read_bg_from_eb()
2054 key->objectid, key->offset, map->start, map->chunk_len); in read_bg_from_eb()
2055 ret = -EUCLEAN; in read_bg_from_eb()
2064 if (flags != (map->type & BTRFS_BLOCK_GROUP_TYPE_MASK)) { in read_bg_from_eb()
2066 "block group %llu len %llu type flags 0x%llx mismatch with chunk type flags 0x%llx", in read_bg_from_eb()
2067 key->objectid, key->offset, flags, in read_bg_from_eb()
2068 (BTRFS_BLOCK_GROUP_TYPE_MASK & map->type)); in read_bg_from_eb()
2069 ret = -EUCLEAN; in read_bg_from_eb()
2086 if (found_key.objectid >= key->objectid && in find_first_block_group()
2099 write_seqlock(&fs_info->profiles_lock); in set_avail_alloc_bits()
2101 fs_info->avail_data_alloc_bits |= extra_flags; in set_avail_alloc_bits()
2103 fs_info->avail_metadata_alloc_bits |= extra_flags; in set_avail_alloc_bits()
2105 fs_info->avail_system_alloc_bits |= extra_flags; in set_avail_alloc_bits()
2106 write_sequnlock(&fs_info->profiles_lock); in set_avail_alloc_bits()
2113 * @chunk_start: logical address of block group
2117 * @stripe_len: size of IO stripe for the given block group
2120 * Used primarily to exclude those portions of a block group that contain super
2121 * block copies.
2136 return -EIO; in btrfs_rmap_block()
2138 data_stripe_length = map->stripe_size; in btrfs_rmap_block()
2140 chunk_start = map->start; in btrfs_rmap_block()
2143 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) in btrfs_rmap_block()
2146 buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS); in btrfs_rmap_block()
2148 ret = -ENOMEM; in btrfs_rmap_block()
2152 for (i = 0; i < map->num_stripes; i++) { in btrfs_rmap_block()
2158 if (!in_range(physical, map->stripes[i].physical, in btrfs_rmap_block()
2162 stripe_nr = (physical - map->stripes[i].physical) >> in btrfs_rmap_block()
2164 offset = (physical - map->stripes[i].physical) & in btrfs_rmap_block()
2167 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | in btrfs_rmap_block()
2169 stripe_nr = div_u64(stripe_nr * map->num_stripes + i, in btrfs_rmap_block()
2170 map->sub_stripes); in btrfs_rmap_block()
2174 * instead of map->stripe_len in btrfs_rmap_block()
2198 static int exclude_super_stripes(struct btrfs_block_group *cache) in exclude_super_stripes() argument
2200 struct btrfs_fs_info *fs_info = cache->fs_info; in exclude_super_stripes()
2207 if (cache->start < BTRFS_SUPER_INFO_OFFSET) { in exclude_super_stripes()
2208 stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->start; in exclude_super_stripes()
2209 cache->bytes_super += stripe_len; in exclude_super_stripes()
2210 ret = set_extent_bit(&fs_info->excluded_extents, cache->start, in exclude_super_stripes()
2211 cache->start + stripe_len - 1, in exclude_super_stripes()
2219 ret = btrfs_rmap_block(fs_info, cache->start, in exclude_super_stripes()
2228 "zoned: block group %llu must not contain super block", in exclude_super_stripes()
2229 cache->start); in exclude_super_stripes()
2230 return -EUCLEAN; in exclude_super_stripes()
2233 while (nr--) { in exclude_super_stripes()
2235 cache->start + cache->length - logical[nr]); in exclude_super_stripes()
2237 cache->bytes_super += len; in exclude_super_stripes()
2238 ret = set_extent_bit(&fs_info->excluded_extents, logical[nr], in exclude_super_stripes()
2239 logical[nr] + len - 1, in exclude_super_stripes()
2255 struct btrfs_block_group *cache; in btrfs_create_block_group_cache() local
2257 cache = kzalloc(sizeof(*cache), GFP_NOFS); in btrfs_create_block_group_cache()
2258 if (!cache) in btrfs_create_block_group_cache()
2261 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl), in btrfs_create_block_group_cache()
2263 if (!cache->free_space_ctl) { in btrfs_create_block_group_cache()
2264 kfree(cache); in btrfs_create_block_group_cache()
2268 cache->start = start; in btrfs_create_block_group_cache()
2270 cache->fs_info = fs_info; in btrfs_create_block_group_cache()
2271 cache->full_stripe_len = btrfs_full_stripe_len(fs_info, start); in btrfs_create_block_group_cache()
2273 cache->discard_index = BTRFS_DISCARD_INDEX_UNUSED; in btrfs_create_block_group_cache()
2275 refcount_set(&cache->refs, 1); in btrfs_create_block_group_cache()
2276 spin_lock_init(&cache->lock); in btrfs_create_block_group_cache()
2277 init_rwsem(&cache->data_rwsem); in btrfs_create_block_group_cache()
2278 INIT_LIST_HEAD(&cache->list); in btrfs_create_block_group_cache()
2279 INIT_LIST_HEAD(&cache->cluster_list); in btrfs_create_block_group_cache()
2280 INIT_LIST_HEAD(&cache->bg_list); in btrfs_create_block_group_cache()
2281 INIT_LIST_HEAD(&cache->ro_list); in btrfs_create_block_group_cache()
2282 INIT_LIST_HEAD(&cache->discard_list); in btrfs_create_block_group_cache()
2283 INIT_LIST_HEAD(&cache->dirty_list); in btrfs_create_block_group_cache()
2284 INIT_LIST_HEAD(&cache->io_list); in btrfs_create_block_group_cache()
2285 INIT_LIST_HEAD(&cache->active_bg_list); in btrfs_create_block_group_cache()
2286 btrfs_init_free_space_ctl(cache, cache->free_space_ctl); in btrfs_create_block_group_cache()
2287 atomic_set(&cache->frozen, 0); in btrfs_create_block_group_cache()
2288 mutex_init(&cache->free_space_lock); in btrfs_create_block_group_cache()
2290 return cache; in btrfs_create_block_group_cache()
2294 * Iterate all chunks and verify that each of them has the corresponding block
2315 bg = btrfs_lookup_block_group(fs_info, map->start); in check_chunk_block_group_mappings()
2318 "chunk start=%llu len=%llu doesn't have corresponding block group", in check_chunk_block_group_mappings()
2319 map->start, map->chunk_len); in check_chunk_block_group_mappings()
2320 ret = -EUCLEAN; in check_chunk_block_group_mappings()
2324 if (bg->start != map->start || bg->length != map->chunk_len || in check_chunk_block_group_mappings()
2325 (bg->flags & BTRFS_BLOCK_GROUP_TYPE_MASK) != in check_chunk_block_group_mappings()
2326 (map->type & BTRFS_BLOCK_GROUP_TYPE_MASK)) { in check_chunk_block_group_mappings()
2328 "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()
2329 map->start, map->chunk_len, in check_chunk_block_group_mappings()
2330 map->type & BTRFS_BLOCK_GROUP_TYPE_MASK, in check_chunk_block_group_mappings()
2331 bg->start, bg->length, in check_chunk_block_group_mappings()
2332 bg->flags & BTRFS_BLOCK_GROUP_TYPE_MASK); in check_chunk_block_group_mappings()
2333 ret = -EUCLEAN; in check_chunk_block_group_mappings()
2338 start = map->start + map->chunk_len; in check_chunk_block_group_mappings()
2350 struct btrfs_block_group *cache; in read_one_block_group() local
2354 ASSERT(key->type == BTRFS_BLOCK_GROUP_ITEM_KEY); in read_one_block_group()
2356 cache = btrfs_create_block_group_cache(info, key->objectid); in read_one_block_group()
2357 if (!cache) in read_one_block_group()
2358 return -ENOMEM; in read_one_block_group()
2360 cache->length = key->offset; in read_one_block_group()
2361 cache->used = btrfs_stack_block_group_used(bgi); in read_one_block_group()
2362 cache->commit_used = cache->used; in read_one_block_group()
2363 cache->flags = btrfs_stack_block_group_flags(bgi); in read_one_block_group()
2364 cache->global_root_id = btrfs_stack_block_group_chunk_objectid(bgi); in read_one_block_group()
2366 set_free_space_tree_thresholds(cache); in read_one_block_group()
2370 * When we mount with old space cache, we need to in read_one_block_group()
2374 * truncate the old free space cache inode and in read_one_block_group()
2377 * the new space cache info onto disk. in read_one_block_group()
2380 cache->disk_cache_state = BTRFS_DC_CLEAR; in read_one_block_group()
2382 if (!mixed && ((cache->flags & BTRFS_BLOCK_GROUP_METADATA) && in read_one_block_group()
2383 (cache->flags & BTRFS_BLOCK_GROUP_DATA))) { in read_one_block_group()
2385 "bg %llu is a mixed block group but filesystem hasn't enabled mixed block groups", in read_one_block_group()
2386 cache->start); in read_one_block_group()
2387 ret = -EINVAL; in read_one_block_group()
2391 ret = btrfs_load_block_group_zone_info(cache, false); in read_one_block_group()
2394 cache->start); in read_one_block_group()
2403 ret = exclude_super_stripes(cache); in read_one_block_group()
2406 btrfs_free_excluded_extents(cache); in read_one_block_group()
2412 * free space for a block group. So, we don't need any caching work. in read_one_block_group()
2423 btrfs_calc_zone_unusable(cache); in read_one_block_group()
2425 btrfs_free_excluded_extents(cache); in read_one_block_group()
2426 } else if (cache->length == cache->used) { in read_one_block_group()
2427 cache->cached = BTRFS_CACHE_FINISHED; in read_one_block_group()
2428 btrfs_free_excluded_extents(cache); in read_one_block_group()
2429 } else if (cache->used == 0) { in read_one_block_group()
2430 cache->cached = BTRFS_CACHE_FINISHED; in read_one_block_group()
2431 ret = btrfs_add_new_free_space(cache, cache->start, in read_one_block_group()
2432 cache->start + cache->length, NULL); in read_one_block_group()
2433 btrfs_free_excluded_extents(cache); in read_one_block_group()
2438 ret = btrfs_add_block_group_cache(info, cache); in read_one_block_group()
2440 btrfs_remove_free_space_cache(cache); in read_one_block_group()
2443 trace_btrfs_add_block_group(info, cache, 0); in read_one_block_group()
2444 btrfs_add_bg_to_space_info(info, cache); in read_one_block_group()
2446 set_avail_alloc_bits(info, cache->flags); in read_one_block_group()
2447 if (btrfs_chunk_writeable(info, cache->start)) { in read_one_block_group()
2448 if (cache->used == 0) { in read_one_block_group()
2449 ASSERT(list_empty(&cache->bg_list)); in read_one_block_group()
2451 btrfs_discard_queue_work(&info->discard_ctl, cache); in read_one_block_group()
2453 btrfs_mark_bg_unused(cache); in read_one_block_group()
2456 inc_block_group_ro(cache, 1); in read_one_block_group()
2461 btrfs_put_block_group(cache); in read_one_block_group()
2470 for (node = rb_first_cached(&fs_info->mapping_tree); node; node = rb_next(node)) { in fill_dummy_bgs()
2475 bg = btrfs_create_block_group_cache(fs_info, map->start); in fill_dummy_bgs()
2477 ret = -ENOMEM; in fill_dummy_bgs()
2481 /* Fill dummy cache as FULL */ in fill_dummy_bgs()
2482 bg->length = map->chunk_len; in fill_dummy_bgs()
2483 bg->flags = map->type; in fill_dummy_bgs()
2484 bg->cached = BTRFS_CACHE_FINISHED; in fill_dummy_bgs()
2485 bg->used = map->chunk_len; in fill_dummy_bgs()
2486 bg->flags = map->type; in fill_dummy_bgs()
2489 * We may have some valid block group cache added already, in in fill_dummy_bgs()
2492 if (ret == -EEXIST) { in fill_dummy_bgs()
2506 set_avail_alloc_bits(fs_info, bg->flags); in fill_dummy_bgs()
2518 struct btrfs_block_group *cache; in btrfs_read_block_groups() local
2526 * unsupported RO options. The fs can never be mounted read-write, so no in btrfs_read_block_groups()
2527 * need to waste time searching block group items. in btrfs_read_block_groups()
2532 if (!root || (btrfs_super_compat_ro_flags(info->super_copy) & in btrfs_read_block_groups()
2541 return -ENOMEM; in btrfs_read_block_groups()
2543 cache_gen = btrfs_super_cache_generation(info->super_copy); in btrfs_read_block_groups()
2545 btrfs_super_generation(info->super_copy) != cache_gen) in btrfs_read_block_groups()
2561 leaf = path->nodes[0]; in btrfs_read_block_groups()
2562 slot = path->slots[0]; in btrfs_read_block_groups()
2577 list_for_each_entry(space_info, &info->space_info, list) { in btrfs_read_block_groups()
2581 if (list_empty(&space_info->block_groups[i])) in btrfs_read_block_groups()
2583 cache = list_first_entry(&space_info->block_groups[i], in btrfs_read_block_groups()
2586 btrfs_sysfs_add_block_group_type(cache); in btrfs_read_block_groups()
2589 if (!(btrfs_get_alloc_profile(info, space_info->flags) & in btrfs_read_block_groups()
2596 * Avoid allocating from un-mirrored block group if there are in btrfs_read_block_groups()
2597 * mirrored block groups. in btrfs_read_block_groups()
2599 list_for_each_entry(cache, in btrfs_read_block_groups()
2600 &space_info->block_groups[BTRFS_RAID_RAID0], in btrfs_read_block_groups()
2602 inc_block_group_ro(cache, 1); in btrfs_read_block_groups()
2603 list_for_each_entry(cache, in btrfs_read_block_groups()
2604 &space_info->block_groups[BTRFS_RAID_SINGLE], in btrfs_read_block_groups()
2606 inc_block_group_ro(cache, 1); in btrfs_read_block_groups()
2616 * Try to fill the tree using dummy block groups so that the user can in btrfs_read_block_groups()
2634 struct btrfs_fs_info *fs_info = trans->fs_info; in insert_block_group_item()
2641 spin_lock(&block_group->lock); in insert_block_group_item()
2642 btrfs_set_stack_block_group_used(&bgi, block_group->used); in insert_block_group_item()
2644 block_group->global_root_id); in insert_block_group_item()
2645 btrfs_set_stack_block_group_flags(&bgi, block_group->flags); in insert_block_group_item()
2646 old_commit_used = block_group->commit_used; in insert_block_group_item()
2647 block_group->commit_used = block_group->used; in insert_block_group_item()
2648 key.objectid = block_group->start; in insert_block_group_item()
2650 key.offset = block_group->length; in insert_block_group_item()
2651 spin_unlock(&block_group->lock); in insert_block_group_item()
2655 spin_lock(&block_group->lock); in insert_block_group_item()
2656 block_group->commit_used = old_commit_used; in insert_block_group_item()
2657 spin_unlock(&block_group->lock); in insert_block_group_item()
2667 struct btrfs_fs_info *fs_info = device->fs_info; in insert_dev_extent()
2668 struct btrfs_root *root = fs_info->dev_root; in insert_dev_extent()
2675 WARN_ON(!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state)); in insert_dev_extent()
2676 WARN_ON(test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)); in insert_dev_extent()
2679 return -ENOMEM; in insert_dev_extent()
2681 key.objectid = device->devid; in insert_dev_extent()
2688 leaf = path->nodes[0]; in insert_dev_extent()
2689 extent = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_extent); in insert_dev_extent()
2710 struct btrfs_fs_info *fs_info = trans->fs_info; in insert_dev_extents()
2726 * (dev-replace.c:btrfs_dev_replace_finishing()), so we could grab the in insert_dev_extents()
2730 mutex_lock(&fs_info->fs_devices->device_list_mutex); in insert_dev_extents()
2731 for (i = 0; i < map->num_stripes; i++) { in insert_dev_extents()
2732 device = map->stripes[i].dev; in insert_dev_extents()
2733 dev_offset = map->stripes[i].physical; in insert_dev_extents()
2736 map->stripe_size); in insert_dev_extents()
2740 mutex_unlock(&fs_info->fs_devices->device_list_mutex); in insert_dev_extents()
2755 struct btrfs_fs_info *fs_info = trans->fs_info; in btrfs_create_pending_block_groups()
2759 while (!list_empty(&trans->new_bgs)) { in btrfs_create_pending_block_groups()
2762 block_group = list_first_entry(&trans->new_bgs, in btrfs_create_pending_block_groups()
2768 index = btrfs_bg_flags_to_raid_index(block_group->flags); in btrfs_create_pending_block_groups()
2774 &block_group->runtime_flags)) { in btrfs_create_pending_block_groups()
2775 mutex_lock(&fs_info->chunk_mutex); in btrfs_create_pending_block_groups()
2777 mutex_unlock(&fs_info->chunk_mutex); in btrfs_create_pending_block_groups()
2781 ret = insert_dev_extents(trans, block_group->start, in btrfs_create_pending_block_groups()
2782 block_group->length); in btrfs_create_pending_block_groups()
2793 if (block_group->space_info->block_group_kobjs[index] == NULL) in btrfs_create_pending_block_groups()
2800 spin_lock(&fs_info->unused_bgs_lock); in btrfs_create_pending_block_groups()
2801 list_del_init(&block_group->bg_list); in btrfs_create_pending_block_groups()
2802 clear_bit(BLOCK_GROUP_FLAG_NEW, &block_group->runtime_flags); in btrfs_create_pending_block_groups()
2803 spin_unlock(&fs_info->unused_bgs_lock); in btrfs_create_pending_block_groups()
2806 * If the block group is still unused, add it to the list of in btrfs_create_pending_block_groups()
2807 * unused block groups. The block group may have been created in in btrfs_create_pending_block_groups()
2811 * so the block group may become unused for a long time. For in btrfs_create_pending_block_groups()
2828 spin_lock(&block_group->lock); in btrfs_create_pending_block_groups()
2830 spin_unlock(&block_group->lock); in btrfs_create_pending_block_groups()
2840 * For extent tree v2 we use the block_group_item->chunk_offset to point at our
2852 if (btrfs_super_total_bytes(fs_info->super_copy) <= (SZ_1G * 10ULL)) in calculate_global_root_id()
2856 div64_u64_rem(offset, fs_info->nr_global_roots, &index); in calculate_global_root_id()
2864 struct btrfs_fs_info *fs_info = trans->fs_info; in btrfs_make_block_group()
2865 struct btrfs_block_group *cache; in btrfs_make_block_group() local
2870 cache = btrfs_create_block_group_cache(fs_info, chunk_offset); in btrfs_make_block_group()
2871 if (!cache) in btrfs_make_block_group()
2872 return ERR_PTR(-ENOMEM); in btrfs_make_block_group()
2875 * Mark it as new before adding it to the rbtree of block groups or any in btrfs_make_block_group()
2879 set_bit(BLOCK_GROUP_FLAG_NEW, &cache->runtime_flags); in btrfs_make_block_group()
2881 cache->length = size; in btrfs_make_block_group()
2882 set_free_space_tree_thresholds(cache); in btrfs_make_block_group()
2883 cache->flags = type; in btrfs_make_block_group()
2884 cache->cached = BTRFS_CACHE_FINISHED; in btrfs_make_block_group()
2885 cache->global_root_id = calculate_global_root_id(fs_info, cache->start); in btrfs_make_block_group()
2888 set_bit(BLOCK_GROUP_FLAG_NEEDS_FREE_SPACE, &cache->runtime_flags); in btrfs_make_block_group()
2890 ret = btrfs_load_block_group_zone_info(cache, true); in btrfs_make_block_group()
2892 btrfs_put_block_group(cache); in btrfs_make_block_group()
2896 ret = exclude_super_stripes(cache); in btrfs_make_block_group()
2899 btrfs_free_excluded_extents(cache); in btrfs_make_block_group()
2900 btrfs_put_block_group(cache); in btrfs_make_block_group()
2904 ret = btrfs_add_new_free_space(cache, chunk_offset, chunk_offset + size, NULL); in btrfs_make_block_group()
2905 btrfs_free_excluded_extents(cache); in btrfs_make_block_group()
2907 btrfs_put_block_group(cache); in btrfs_make_block_group()
2913 * assigned to our block group. We want our bg to be added to the rbtree in btrfs_make_block_group()
2914 * with its ->space_info set. in btrfs_make_block_group()
2916 cache->space_info = btrfs_find_space_info(fs_info, cache->flags); in btrfs_make_block_group()
2917 ASSERT(cache->space_info); in btrfs_make_block_group()
2919 ret = btrfs_add_block_group_cache(fs_info, cache); in btrfs_make_block_group()
2921 btrfs_remove_free_space_cache(cache); in btrfs_make_block_group()
2922 btrfs_put_block_group(cache); in btrfs_make_block_group()
2927 * Now that our block group has its ->space_info set and is inserted in in btrfs_make_block_group()
2930 trace_btrfs_add_block_group(fs_info, cache, 1); in btrfs_make_block_group()
2931 btrfs_add_bg_to_space_info(fs_info, cache); in btrfs_make_block_group()
2935 if (btrfs_should_fragment_free_space(cache)) { in btrfs_make_block_group()
2936 cache->space_info->bytes_used += size >> 1; in btrfs_make_block_group()
2937 fragment_free_space(cache); in btrfs_make_block_group()
2941 list_add_tail(&cache->bg_list, &trans->new_bgs); in btrfs_make_block_group()
2945 return cache; in btrfs_make_block_group()
2949 * Mark one block group RO, can be called several times for the same block
2952 * @cache: the destination block group
2953 * @do_chunk_alloc: whether need to do chunk pre-allocation, this is to
2955 * block group RO.
2957 int btrfs_inc_block_group_ro(struct btrfs_block_group *cache, in btrfs_inc_block_group_ro() argument
2960 struct btrfs_fs_info *fs_info = cache->fs_info; in btrfs_inc_block_group_ro()
2968 * This can only happen when we are doing read-only scrub on read-only in btrfs_inc_block_group_ro()
2970 * In that case we should not start a new transaction on read-only fs. in btrfs_inc_block_group_ro()
2973 if (sb_rdonly(fs_info->sb)) { in btrfs_inc_block_group_ro()
2974 mutex_lock(&fs_info->ro_block_group_mutex); in btrfs_inc_block_group_ro()
2975 ret = inc_block_group_ro(cache, 0); in btrfs_inc_block_group_ro()
2976 mutex_unlock(&fs_info->ro_block_group_mutex); in btrfs_inc_block_group_ro()
2988 * We're not allowed to set block groups readonly after the dirty in btrfs_inc_block_group_ro()
2989 * block group cache has started writing. If it already started, in btrfs_inc_block_group_ro()
2992 mutex_lock(&fs_info->ro_block_group_mutex); in btrfs_inc_block_group_ro()
2993 if (test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &trans->transaction->flags)) { in btrfs_inc_block_group_ro()
2994 u64 transid = trans->transid; in btrfs_inc_block_group_ro()
2996 mutex_unlock(&fs_info->ro_block_group_mutex); in btrfs_inc_block_group_ro()
3009 * corresponding block group with the new raid level. in btrfs_inc_block_group_ro()
3011 alloc_flags = btrfs_get_alloc_profile(fs_info, cache->flags); in btrfs_inc_block_group_ro()
3012 if (alloc_flags != cache->flags) { in btrfs_inc_block_group_ro()
3019 if (ret == -ENOSPC) in btrfs_inc_block_group_ro()
3026 ret = inc_block_group_ro(cache, 0); in btrfs_inc_block_group_ro()
3029 if (ret == -ETXTBSY) in btrfs_inc_block_group_ro()
3035 * we still want to try our best to mark the block group read-only. in btrfs_inc_block_group_ro()
3037 if (!do_chunk_alloc && ret == -ENOSPC && in btrfs_inc_block_group_ro()
3038 (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM)) in btrfs_inc_block_group_ro()
3041 alloc_flags = btrfs_get_alloc_profile(fs_info, cache->space_info->flags); in btrfs_inc_block_group_ro()
3049 ret = btrfs_zoned_activate_one_bg(fs_info, cache->space_info, true); in btrfs_inc_block_group_ro()
3053 ret = inc_block_group_ro(cache, 0); in btrfs_inc_block_group_ro()
3054 if (ret == -ETXTBSY) in btrfs_inc_block_group_ro()
3057 if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) { in btrfs_inc_block_group_ro()
3058 alloc_flags = btrfs_get_alloc_profile(fs_info, cache->flags); in btrfs_inc_block_group_ro()
3059 mutex_lock(&fs_info->chunk_mutex); in btrfs_inc_block_group_ro()
3061 mutex_unlock(&fs_info->chunk_mutex); in btrfs_inc_block_group_ro()
3064 mutex_unlock(&fs_info->ro_block_group_mutex); in btrfs_inc_block_group_ro()
3070 void btrfs_dec_block_group_ro(struct btrfs_block_group *cache) in btrfs_dec_block_group_ro() argument
3072 struct btrfs_space_info *sinfo = cache->space_info; in btrfs_dec_block_group_ro()
3075 BUG_ON(!cache->ro); in btrfs_dec_block_group_ro()
3077 spin_lock(&sinfo->lock); in btrfs_dec_block_group_ro()
3078 spin_lock(&cache->lock); in btrfs_dec_block_group_ro()
3079 if (!--cache->ro) { in btrfs_dec_block_group_ro()
3080 if (btrfs_is_zoned(cache->fs_info)) { in btrfs_dec_block_group_ro()
3082 cache->zone_unusable = in btrfs_dec_block_group_ro()
3083 (cache->alloc_offset - cache->used - cache->pinned - in btrfs_dec_block_group_ro()
3084 cache->reserved) + in btrfs_dec_block_group_ro()
3085 (cache->length - cache->zone_capacity); in btrfs_dec_block_group_ro()
3086 btrfs_space_info_update_bytes_zone_unusable(sinfo, cache->zone_unusable); in btrfs_dec_block_group_ro()
3087 sinfo->bytes_readonly -= cache->zone_unusable; in btrfs_dec_block_group_ro()
3089 num_bytes = cache->length - cache->reserved - in btrfs_dec_block_group_ro()
3090 cache->pinned - cache->bytes_super - in btrfs_dec_block_group_ro()
3091 cache->zone_unusable - cache->used; in btrfs_dec_block_group_ro()
3092 sinfo->bytes_readonly -= num_bytes; in btrfs_dec_block_group_ro()
3093 list_del_init(&cache->ro_list); in btrfs_dec_block_group_ro()
3095 spin_unlock(&cache->lock); in btrfs_dec_block_group_ro()
3096 spin_unlock(&sinfo->lock); in btrfs_dec_block_group_ro()
3101 struct btrfs_block_group *cache) in update_block_group_item() argument
3103 struct btrfs_fs_info *fs_info = trans->fs_info; in update_block_group_item()
3114 * Block group items update can be triggered out of commit transaction in update_block_group_item()
3116 * We cannot use cache->used directly outside of the spin lock, as it in update_block_group_item()
3119 spin_lock(&cache->lock); in update_block_group_item()
3120 old_commit_used = cache->commit_used; in update_block_group_item()
3121 used = cache->used; in update_block_group_item()
3123 if (cache->commit_used == used) { in update_block_group_item()
3124 spin_unlock(&cache->lock); in update_block_group_item()
3127 cache->commit_used = used; in update_block_group_item()
3128 spin_unlock(&cache->lock); in update_block_group_item()
3130 key.objectid = cache->start; in update_block_group_item()
3132 key.offset = cache->length; in update_block_group_item()
3137 ret = -ENOENT; in update_block_group_item()
3141 leaf = path->nodes[0]; in update_block_group_item()
3142 bi = btrfs_item_ptr_offset(leaf, path->slots[0]); in update_block_group_item()
3145 cache->global_root_id); in update_block_group_item()
3146 btrfs_set_stack_block_group_flags(&bgi, cache->flags); in update_block_group_item()
3151 * We didn't update the block group item, need to revert commit_used in update_block_group_item()
3152 * unless the block group item didn't exist yet - this is to prevent a in update_block_group_item()
3153 * race with a concurrent insertion of the block group item, with in update_block_group_item()
3156 * insertion set it to a value greater than 0 - if the block group later in update_block_group_item()
3159 if (ret < 0 && ret != -ENOENT) { in update_block_group_item()
3160 spin_lock(&cache->lock); in update_block_group_item()
3161 cache->commit_used = old_commit_used; in update_block_group_item()
3162 spin_unlock(&cache->lock); in update_block_group_item()
3172 struct btrfs_fs_info *fs_info = block_group->fs_info; in cache_save_setup()
3185 * If this block group is smaller than 100 megs don't bother caching the in cache_save_setup()
3186 * block group. in cache_save_setup()
3188 if (block_group->length < (100 * SZ_1M)) { in cache_save_setup()
3189 spin_lock(&block_group->lock); in cache_save_setup()
3190 block_group->disk_cache_state = BTRFS_DC_WRITTEN; in cache_save_setup()
3191 spin_unlock(&block_group->lock); in cache_save_setup()
3199 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) { in cache_save_setup()
3209 if (block_group->ro) in cache_save_setup()
3220 * from here on out we know not to trust this cache when we load up next in cache_save_setup()
3223 BTRFS_I(inode)->generation = 0; in cache_save_setup()
3228 * super cache generation to 0 so we know to invalidate the in cache_save_setup()
3229 * cache, but then we'd have to keep track of the block groups in cache_save_setup()
3230 * that fail this way so we know we _have_ to reset this cache in cache_save_setup()
3231 * before the next commit or risk reading stale cache. So to in cache_save_setup()
3242 if (block_group->cache_generation == trans->transid && in cache_save_setup()
3250 &fs_info->global_block_rsv); in cache_save_setup()
3259 spin_lock(&block_group->lock); in cache_save_setup()
3260 if (block_group->cached != BTRFS_CACHE_FINISHED || in cache_save_setup()
3269 spin_unlock(&block_group->lock); in cache_save_setup()
3272 spin_unlock(&block_group->lock); in cache_save_setup()
3275 * We hit an ENOSPC when setting up the cache in this transaction, just in cache_save_setup()
3276 * skip doing the setup, we've already cleared the cache so we're safe. in cache_save_setup()
3278 if (test_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags)) { in cache_save_setup()
3279 ret = -ENOSPC; in cache_save_setup()
3284 * Try to preallocate enough space based on how big the block group is. in cache_save_setup()
3287 * cache. in cache_save_setup()
3289 cache_size = div_u64(block_group->length, SZ_256M); in cache_save_setup()
3294 cache_size *= fs_info->sectorsize; in cache_save_setup()
3305 * Our cache requires contiguous chunks so that we don't modify a bunch in cache_save_setup()
3306 * of metadata or split extents when writing the cache out, which means in cache_save_setup()
3309 * other block groups for this transaction, maybe we'll unpin enough in cache_save_setup()
3314 else if (ret == -ENOSPC) in cache_save_setup()
3315 set_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags); in cache_save_setup()
3322 spin_lock(&block_group->lock); in cache_save_setup()
3324 block_group->cache_generation = trans->transid; in cache_save_setup()
3325 block_group->disk_cache_state = dcs; in cache_save_setup()
3326 spin_unlock(&block_group->lock); in cache_save_setup()
3334 struct btrfs_fs_info *fs_info = trans->fs_info; in btrfs_setup_space_cache()
3335 struct btrfs_block_group *cache, *tmp; in btrfs_setup_space_cache() local
3336 struct btrfs_transaction *cur_trans = trans->transaction; in btrfs_setup_space_cache()
3339 if (list_empty(&cur_trans->dirty_bgs) || in btrfs_setup_space_cache()
3345 return -ENOMEM; in btrfs_setup_space_cache()
3347 /* Could add new block groups, use _safe just in case */ in btrfs_setup_space_cache()
3348 list_for_each_entry_safe(cache, tmp, &cur_trans->dirty_bgs, in btrfs_setup_space_cache()
3350 if (cache->disk_cache_state == BTRFS_DC_CLEAR) in btrfs_setup_space_cache()
3351 cache_save_setup(cache, trans, path); in btrfs_setup_space_cache()
3359 * Transaction commit does final block group cache writeback during a critical
3361 * order for the cache to actually match the block group, but can introduce a
3364 * So, btrfs_start_dirty_block_groups is here to kick off block group cache IO.
3365 * There's a chance we'll have to redo some of it if the block group changes
3367 * getting rid of the easy block groups while we're still allowing others to
3372 struct btrfs_fs_info *fs_info = trans->fs_info; in btrfs_start_dirty_block_groups()
3373 struct btrfs_block_group *cache; in btrfs_start_dirty_block_groups() local
3374 struct btrfs_transaction *cur_trans = trans->transaction; in btrfs_start_dirty_block_groups()
3379 struct list_head *io = &cur_trans->io_bgs; in btrfs_start_dirty_block_groups()
3382 spin_lock(&cur_trans->dirty_bgs_lock); in btrfs_start_dirty_block_groups()
3383 if (list_empty(&cur_trans->dirty_bgs)) { in btrfs_start_dirty_block_groups()
3384 spin_unlock(&cur_trans->dirty_bgs_lock); in btrfs_start_dirty_block_groups()
3387 list_splice_init(&cur_trans->dirty_bgs, &dirty); in btrfs_start_dirty_block_groups()
3388 spin_unlock(&cur_trans->dirty_bgs_lock); in btrfs_start_dirty_block_groups()
3391 /* Make sure all the block groups on our dirty list actually exist */ in btrfs_start_dirty_block_groups()
3397 ret = -ENOMEM; in btrfs_start_dirty_block_groups()
3404 * removal of empty block groups deleting this block group while we are in btrfs_start_dirty_block_groups()
3405 * writing out the cache in btrfs_start_dirty_block_groups()
3407 mutex_lock(&trans->transaction->cache_write_mutex); in btrfs_start_dirty_block_groups()
3411 cache = list_first_entry(&dirty, struct btrfs_block_group, in btrfs_start_dirty_block_groups()
3414 * This can happen if something re-dirties a block group that in btrfs_start_dirty_block_groups()
3418 if (!list_empty(&cache->io_list)) { in btrfs_start_dirty_block_groups()
3419 list_del_init(&cache->io_list); in btrfs_start_dirty_block_groups()
3420 btrfs_wait_cache_io(trans, cache, path); in btrfs_start_dirty_block_groups()
3421 btrfs_put_block_group(cache); in btrfs_start_dirty_block_groups()
3426 * btrfs_wait_cache_io uses the cache->dirty_list to decide if in btrfs_start_dirty_block_groups()
3433 spin_lock(&cur_trans->dirty_bgs_lock); in btrfs_start_dirty_block_groups()
3434 list_del_init(&cache->dirty_list); in btrfs_start_dirty_block_groups()
3435 spin_unlock(&cur_trans->dirty_bgs_lock); in btrfs_start_dirty_block_groups()
3439 cache_save_setup(cache, trans, path); in btrfs_start_dirty_block_groups()
3441 if (cache->disk_cache_state == BTRFS_DC_SETUP) { in btrfs_start_dirty_block_groups()
3442 cache->io_ctl.inode = NULL; in btrfs_start_dirty_block_groups()
3443 ret = btrfs_write_out_cache(trans, cache, path); in btrfs_start_dirty_block_groups()
3444 if (ret == 0 && cache->io_ctl.inode) { in btrfs_start_dirty_block_groups()
3452 list_add_tail(&cache->io_list, io); in btrfs_start_dirty_block_groups()
3455 * If we failed to write the cache, the in btrfs_start_dirty_block_groups()
3462 ret = update_block_group_item(trans, path, cache); in btrfs_start_dirty_block_groups()
3464 * Our block group might still be attached to the list in btrfs_start_dirty_block_groups()
3465 * of new block groups in the transaction handle of some in btrfs_start_dirty_block_groups()
3466 * other task (struct btrfs_trans_handle->new_bgs). This in btrfs_start_dirty_block_groups()
3467 * means its block group item isn't yet in the extent in btrfs_start_dirty_block_groups()
3472 if (ret == -ENOENT) { in btrfs_start_dirty_block_groups()
3474 spin_lock(&cur_trans->dirty_bgs_lock); in btrfs_start_dirty_block_groups()
3475 if (list_empty(&cache->dirty_list)) { in btrfs_start_dirty_block_groups()
3476 list_add_tail(&cache->dirty_list, in btrfs_start_dirty_block_groups()
3477 &cur_trans->dirty_bgs); in btrfs_start_dirty_block_groups()
3478 btrfs_get_block_group(cache); in btrfs_start_dirty_block_groups()
3481 spin_unlock(&cur_trans->dirty_bgs_lock); in btrfs_start_dirty_block_groups()
3487 /* If it's not on the io list, we need to put the block group */ in btrfs_start_dirty_block_groups()
3489 btrfs_put_block_group(cache); in btrfs_start_dirty_block_groups()
3494 * us from writing caches for block groups that are going to be in btrfs_start_dirty_block_groups()
3497 mutex_unlock(&trans->transaction->cache_write_mutex); in btrfs_start_dirty_block_groups()
3500 mutex_lock(&trans->transaction->cache_write_mutex); in btrfs_start_dirty_block_groups()
3502 mutex_unlock(&trans->transaction->cache_write_mutex); in btrfs_start_dirty_block_groups()
3512 spin_lock(&cur_trans->dirty_bgs_lock); in btrfs_start_dirty_block_groups()
3513 list_splice_init(&cur_trans->dirty_bgs, &dirty); in btrfs_start_dirty_block_groups()
3515 * dirty_bgs_lock protects us from concurrent block group in btrfs_start_dirty_block_groups()
3519 spin_unlock(&cur_trans->dirty_bgs_lock); in btrfs_start_dirty_block_groups()
3522 spin_unlock(&cur_trans->dirty_bgs_lock); in btrfs_start_dirty_block_groups()
3526 spin_lock(&cur_trans->dirty_bgs_lock); in btrfs_start_dirty_block_groups()
3527 list_splice_init(&dirty, &cur_trans->dirty_bgs); in btrfs_start_dirty_block_groups()
3528 spin_unlock(&cur_trans->dirty_bgs_lock); in btrfs_start_dirty_block_groups()
3538 struct btrfs_fs_info *fs_info = trans->fs_info; in btrfs_write_dirty_block_groups()
3539 struct btrfs_block_group *cache; in btrfs_write_dirty_block_groups() local
3540 struct btrfs_transaction *cur_trans = trans->transaction; in btrfs_write_dirty_block_groups()
3544 struct list_head *io = &cur_trans->io_bgs; in btrfs_write_dirty_block_groups()
3548 return -ENOMEM; in btrfs_write_dirty_block_groups()
3553 * transaction's list of dirty block groups. These tasks correspond to in btrfs_write_dirty_block_groups()
3555 * space cache, which run inode.c:btrfs_finish_ordered_io(), and can in btrfs_write_dirty_block_groups()
3556 * allocate new block groups as a result of COWing nodes of the root in btrfs_write_dirty_block_groups()
3565 spin_lock(&cur_trans->dirty_bgs_lock); in btrfs_write_dirty_block_groups()
3566 while (!list_empty(&cur_trans->dirty_bgs)) { in btrfs_write_dirty_block_groups()
3567 cache = list_first_entry(&cur_trans->dirty_bgs, in btrfs_write_dirty_block_groups()
3572 * This can happen if cache_save_setup re-dirties a block group in btrfs_write_dirty_block_groups()
3576 if (!list_empty(&cache->io_list)) { in btrfs_write_dirty_block_groups()
3577 spin_unlock(&cur_trans->dirty_bgs_lock); in btrfs_write_dirty_block_groups()
3578 list_del_init(&cache->io_list); in btrfs_write_dirty_block_groups()
3579 btrfs_wait_cache_io(trans, cache, path); in btrfs_write_dirty_block_groups()
3580 btrfs_put_block_group(cache); in btrfs_write_dirty_block_groups()
3581 spin_lock(&cur_trans->dirty_bgs_lock); in btrfs_write_dirty_block_groups()
3588 list_del_init(&cache->dirty_list); in btrfs_write_dirty_block_groups()
3589 spin_unlock(&cur_trans->dirty_bgs_lock); in btrfs_write_dirty_block_groups()
3592 cache_save_setup(cache, trans, path); in btrfs_write_dirty_block_groups()
3597 if (!ret && cache->disk_cache_state == BTRFS_DC_SETUP) { in btrfs_write_dirty_block_groups()
3598 cache->io_ctl.inode = NULL; in btrfs_write_dirty_block_groups()
3599 ret = btrfs_write_out_cache(trans, cache, path); in btrfs_write_dirty_block_groups()
3600 if (ret == 0 && cache->io_ctl.inode) { in btrfs_write_dirty_block_groups()
3602 list_add_tail(&cache->io_list, io); in btrfs_write_dirty_block_groups()
3605 * If we failed to write the cache, the in btrfs_write_dirty_block_groups()
3612 ret = update_block_group_item(trans, path, cache); in btrfs_write_dirty_block_groups()
3615 * created a new block group while updating a free space in btrfs_write_dirty_block_groups()
3616 * cache's inode (at inode.c:btrfs_finish_ordered_io()) in btrfs_write_dirty_block_groups()
3618 * which case the new block group is still attached to in btrfs_write_dirty_block_groups()
3620 * finished yet (no block group item in the extent tree in btrfs_write_dirty_block_groups()
3626 if (ret == -ENOENT) { in btrfs_write_dirty_block_groups()
3627 wait_event(cur_trans->writer_wait, in btrfs_write_dirty_block_groups()
3628 atomic_read(&cur_trans->num_writers) == 1); in btrfs_write_dirty_block_groups()
3629 ret = update_block_group_item(trans, path, cache); in btrfs_write_dirty_block_groups()
3635 /* If its not on the io list, we need to put the block group */ in btrfs_write_dirty_block_groups()
3637 btrfs_put_block_group(cache); in btrfs_write_dirty_block_groups()
3639 spin_lock(&cur_trans->dirty_bgs_lock); in btrfs_write_dirty_block_groups()
3641 spin_unlock(&cur_trans->dirty_bgs_lock); in btrfs_write_dirty_block_groups()
3648 cache = list_first_entry(io, struct btrfs_block_group, in btrfs_write_dirty_block_groups()
3650 list_del_init(&cache->io_list); in btrfs_write_dirty_block_groups()
3651 btrfs_wait_cache_io(trans, cache, path); in btrfs_write_dirty_block_groups()
3652 btrfs_put_block_group(cache); in btrfs_write_dirty_block_groups()
3662 struct btrfs_fs_info *info = trans->fs_info; in btrfs_update_block_group()
3664 struct btrfs_block_group *cache; in btrfs_update_block_group() local
3670 /* Block accounting for super block */ in btrfs_update_block_group()
3671 spin_lock(&info->delalloc_root_lock); in btrfs_update_block_group()
3672 old_val = btrfs_super_bytes_used(info->super_copy); in btrfs_update_block_group()
3676 old_val -= num_bytes; in btrfs_update_block_group()
3677 btrfs_set_super_bytes_used(info->super_copy, old_val); in btrfs_update_block_group()
3678 spin_unlock(&info->delalloc_root_lock); in btrfs_update_block_group()
3680 cache = btrfs_lookup_block_group(info, bytenr); in btrfs_update_block_group()
3681 if (!cache) in btrfs_update_block_group()
3682 return -ENOENT; in btrfs_update_block_group()
3684 /* An extent can not span multiple block groups. */ in btrfs_update_block_group()
3685 ASSERT(bytenr + num_bytes <= cache->start + cache->length); in btrfs_update_block_group()
3687 space_info = cache->space_info; in btrfs_update_block_group()
3688 factor = btrfs_bg_type_to_factor(cache->flags); in btrfs_update_block_group()
3691 * If this block group has free space cache written out, we need to make in btrfs_update_block_group()
3693 * the unpinning stage to actually add the space back to the block group, in btrfs_update_block_group()
3696 if (!alloc && !btrfs_block_group_done(cache)) in btrfs_update_block_group()
3697 btrfs_cache_block_group(cache, true); in btrfs_update_block_group()
3699 spin_lock(&space_info->lock); in btrfs_update_block_group()
3700 spin_lock(&cache->lock); in btrfs_update_block_group()
3703 cache->disk_cache_state < BTRFS_DC_CLEAR) in btrfs_update_block_group()
3704 cache->disk_cache_state = BTRFS_DC_CLEAR; in btrfs_update_block_group()
3706 old_val = cache->used; in btrfs_update_block_group()
3709 cache->used = old_val; in btrfs_update_block_group()
3710 cache->reserved -= num_bytes; in btrfs_update_block_group()
3711 cache->reclaim_mark = 0; in btrfs_update_block_group()
3712 space_info->bytes_reserved -= num_bytes; in btrfs_update_block_group()
3713 space_info->bytes_used += num_bytes; in btrfs_update_block_group()
3714 space_info->disk_used += num_bytes * factor; in btrfs_update_block_group()
3715 if (READ_ONCE(space_info->periodic_reclaim)) in btrfs_update_block_group()
3716 btrfs_space_info_update_reclaimable(space_info, -num_bytes); in btrfs_update_block_group()
3717 spin_unlock(&cache->lock); in btrfs_update_block_group()
3718 spin_unlock(&space_info->lock); in btrfs_update_block_group()
3720 old_val -= num_bytes; in btrfs_update_block_group()
3721 cache->used = old_val; in btrfs_update_block_group()
3722 cache->pinned += num_bytes; in btrfs_update_block_group()
3724 space_info->bytes_used -= num_bytes; in btrfs_update_block_group()
3725 space_info->disk_used -= num_bytes * factor; in btrfs_update_block_group()
3726 if (READ_ONCE(space_info->periodic_reclaim)) in btrfs_update_block_group()
3729 reclaim = should_reclaim_block_group(cache, num_bytes); in btrfs_update_block_group()
3731 spin_unlock(&cache->lock); in btrfs_update_block_group()
3732 spin_unlock(&space_info->lock); in btrfs_update_block_group()
3734 set_extent_bit(&trans->transaction->pinned_extents, bytenr, in btrfs_update_block_group()
3735 bytenr + num_bytes - 1, EXTENT_DIRTY, NULL); in btrfs_update_block_group()
3738 spin_lock(&trans->transaction->dirty_bgs_lock); in btrfs_update_block_group()
3739 if (list_empty(&cache->dirty_list)) { in btrfs_update_block_group()
3740 list_add_tail(&cache->dirty_list, &trans->transaction->dirty_bgs); in btrfs_update_block_group()
3742 btrfs_get_block_group(cache); in btrfs_update_block_group()
3744 spin_unlock(&trans->transaction->dirty_bgs_lock); in btrfs_update_block_group()
3747 * No longer have used bytes in this block group, queue it for deletion. in btrfs_update_block_group()
3748 * We do this after adding the block group to the dirty list to avoid in btrfs_update_block_group()
3749 * races between cleaner kthread and space cache writeout. in btrfs_update_block_group()
3753 btrfs_mark_bg_unused(cache); in btrfs_update_block_group()
3755 btrfs_mark_bg_to_reclaim(cache); in btrfs_update_block_group()
3758 btrfs_put_block_group(cache); in btrfs_update_block_group()
3760 /* Modified block groups are accounted for in the delayed_refs_rsv. */ in btrfs_update_block_group()
3770 * @cache: The cache we are manipulating
3777 * reservation and the block group has become read only we cannot make the
3778 * reservation and return -EAGAIN, otherwise this function always succeeds.
3780 int btrfs_add_reserved_bytes(struct btrfs_block_group *cache, in btrfs_add_reserved_bytes() argument
3784 struct btrfs_space_info *space_info = cache->space_info; in btrfs_add_reserved_bytes()
3788 spin_lock(&space_info->lock); in btrfs_add_reserved_bytes()
3789 spin_lock(&cache->lock); in btrfs_add_reserved_bytes()
3790 if (cache->ro) { in btrfs_add_reserved_bytes()
3791 ret = -EAGAIN; in btrfs_add_reserved_bytes()
3795 if (btrfs_block_group_should_use_size_class(cache)) { in btrfs_add_reserved_bytes()
3797 ret = btrfs_use_block_group_size_class(cache, size_class, force_wrong_size_class); in btrfs_add_reserved_bytes()
3801 cache->reserved += num_bytes; in btrfs_add_reserved_bytes()
3802 space_info->bytes_reserved += num_bytes; in btrfs_add_reserved_bytes()
3803 trace_btrfs_space_reservation(cache->fs_info, "space_info", in btrfs_add_reserved_bytes()
3804 space_info->flags, num_bytes, 1); in btrfs_add_reserved_bytes()
3805 btrfs_space_info_update_bytes_may_use(space_info, -ram_bytes); in btrfs_add_reserved_bytes()
3807 cache->delalloc_bytes += num_bytes; in btrfs_add_reserved_bytes()
3814 btrfs_try_granting_tickets(cache->fs_info, space_info); in btrfs_add_reserved_bytes()
3816 spin_unlock(&cache->lock); in btrfs_add_reserved_bytes()
3817 spin_unlock(&space_info->lock); in btrfs_add_reserved_bytes()
3824 * @cache: The cache we are manipulating
3833 void btrfs_free_reserved_bytes(struct btrfs_block_group *cache, in btrfs_free_reserved_bytes() argument
3836 struct btrfs_space_info *space_info = cache->space_info; in btrfs_free_reserved_bytes()
3838 spin_lock(&space_info->lock); in btrfs_free_reserved_bytes()
3839 spin_lock(&cache->lock); in btrfs_free_reserved_bytes()
3840 if (cache->ro) in btrfs_free_reserved_bytes()
3841 space_info->bytes_readonly += num_bytes; in btrfs_free_reserved_bytes()
3842 else if (btrfs_is_zoned(cache->fs_info)) in btrfs_free_reserved_bytes()
3843 space_info->bytes_zone_unusable += num_bytes; in btrfs_free_reserved_bytes()
3844 cache->reserved -= num_bytes; in btrfs_free_reserved_bytes()
3845 space_info->bytes_reserved -= num_bytes; in btrfs_free_reserved_bytes()
3846 space_info->max_extent_size = 0; in btrfs_free_reserved_bytes()
3849 cache->delalloc_bytes -= num_bytes; in btrfs_free_reserved_bytes()
3850 spin_unlock(&cache->lock); in btrfs_free_reserved_bytes()
3852 btrfs_try_granting_tickets(cache->fs_info, space_info); in btrfs_free_reserved_bytes()
3853 spin_unlock(&space_info->lock); in btrfs_free_reserved_bytes()
3858 struct list_head *head = &info->space_info; in force_metadata_allocation()
3862 if (found->flags & BTRFS_BLOCK_GROUP_METADATA) in force_metadata_allocation()
3863 found->force_alloc = CHUNK_ALLOC_FORCE; in force_metadata_allocation()
3881 thresh = btrfs_super_total_bytes(fs_info->super_copy); in should_alloc_chunk()
3884 if (sinfo->total_bytes - bytes_used < thresh) in should_alloc_chunk()
3888 if (bytes_used + SZ_2M < mult_perc(sinfo->total_bytes, 80)) in should_alloc_chunk()
3895 u64 alloc_flags = btrfs_get_alloc_profile(trans->fs_info, type); in btrfs_force_chunk_alloc()
3909 * system block group if needed. in do_chunk_alloc()
3921 * Normally we are not expected to fail with -ENOSPC here, since we have in do_chunk_alloc()
3926 * existing system block groups have a profile which can not be used in do_chunk_alloc()
3933 * none of the block groups can be used for extent allocation since they in do_chunk_alloc()
3937 * block groups and check if they have a usable profile and enough space in do_chunk_alloc()
3938 * can be slow on very large filesystems, so we tolerate the -ENOSPC and in do_chunk_alloc()
3944 * block group to allocate from when we called check_system_chunk() in do_chunk_alloc()
3945 * above. However right after we called it, the only system block group in do_chunk_alloc()
3949 * handle and scrub uses the commit root to search for block groups; in do_chunk_alloc()
3951 * 3) We had one system block group with enough free space when we called in do_chunk_alloc()
3955 * block group (discard removes a free space entry, discards it, and in do_chunk_alloc()
3956 * then adds back the entry to the block group cache). in do_chunk_alloc()
3958 if (ret == -ENOSPC) { in do_chunk_alloc()
3959 const u64 sys_flags = btrfs_system_alloc_profile(trans->fs_info); in do_chunk_alloc()
3997 * 1) Phase 1 - through btrfs_chunk_alloc() we allocate device extents for
3998 * the chunk, the chunk mapping, create its block group and add the items
3999 * that belong in the chunk btree to it - more specifically, we need to
4002 * 2) Phase 2 - through btrfs_create_pending_block_groups(), we add the block
4008 * trigger chunk allocation and attempted to insert the new block group item
4019 * allocate a new block group (chunk) because the only one that had enough
4021 * device replace, block group reclaim thread, etc), so we can not use it
4026 * the filesystem was mounted in degraded mode, none of the existing block
4028 * profile (for e.g. mounting a 2 devices filesystem, where all block groups
4031 * example, it will trigger allocation of a new metadata block group with a
4037 * example, it does not find any free extent in any metadata block group,
4038 * therefore forced to try to allocate a new metadata block group.
4040 * meanwhile - this typically happens with tasks that don't reserve space
4049 * the only metadata block group that had free space (discard starts by
4050 * removing a free space entry from a block group, then does the discard
4052 * block group).
4055 * a seed device - we must create new metadata and system chunks without adding
4056 * any of the block group items to the chunk, extent and device btrees. If we
4058 * btrees, since all the chunks from the seed device are read-only.
4073 * btrfs_reserve_chunk_metadata() - the former is used when allocating a data or
4075 * a modification to the chunk btree - use cases for the later are adding,
4081 * holding fs_info->chunk_mutex. This is important to guarantee that while COWing
4087 * that mutex. The same logic applies to removing chunks - we must reserve system
4089 * while holding fs_info->chunk_mutex.
4094 * - return 1 if it successfully allocates a chunk,
4095 * - return errors including -ENOSPC otherwise.
4097 * - return 0 if it doesn't need to allocate a new chunk,
4098 * - return 1 if it successfully allocates a chunk,
4099 * - return errors including -ENOSPC otherwise.
4104 struct btrfs_fs_info *fs_info = trans->fs_info; in btrfs_chunk_alloc()
4117 /* Don't re-enter if we're already allocating a chunk */ in btrfs_chunk_alloc()
4118 if (trans->allocating_chunk) in btrfs_chunk_alloc()
4119 return -ENOSPC; in btrfs_chunk_alloc()
4129 * lock on it and on its parent - if the COW operation triggers a system in btrfs_chunk_alloc()
4138 * here - this happens in the cases described above at do_chunk_alloc(). in btrfs_chunk_alloc()
4142 return -ENOSPC; in btrfs_chunk_alloc()
4148 spin_lock(&space_info->lock); in btrfs_chunk_alloc()
4149 if (force < space_info->force_alloc) in btrfs_chunk_alloc()
4150 force = space_info->force_alloc; in btrfs_chunk_alloc()
4152 if (space_info->full) { in btrfs_chunk_alloc()
4155 ret = -ENOSPC; in btrfs_chunk_alloc()
4158 spin_unlock(&space_info->lock); in btrfs_chunk_alloc()
4161 spin_unlock(&space_info->lock); in btrfs_chunk_alloc()
4163 } else if (space_info->chunk_alloc) { in btrfs_chunk_alloc()
4165 * Someone is already allocating, so we need to block in btrfs_chunk_alloc()
4172 spin_unlock(&space_info->lock); in btrfs_chunk_alloc()
4173 mutex_lock(&fs_info->chunk_mutex); in btrfs_chunk_alloc()
4174 mutex_unlock(&fs_info->chunk_mutex); in btrfs_chunk_alloc()
4177 space_info->chunk_alloc = 1; in btrfs_chunk_alloc()
4179 spin_unlock(&space_info->lock); in btrfs_chunk_alloc()
4185 mutex_lock(&fs_info->chunk_mutex); in btrfs_chunk_alloc()
4186 trans->allocating_chunk = true; in btrfs_chunk_alloc()
4200 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) { in btrfs_chunk_alloc()
4201 fs_info->data_chunk_allocations++; in btrfs_chunk_alloc()
4202 if (!(fs_info->data_chunk_allocations % in btrfs_chunk_alloc()
4203 fs_info->metadata_ratio)) in btrfs_chunk_alloc()
4208 trans->allocating_chunk = false; in btrfs_chunk_alloc()
4214 * New block group is likely to be used soon. Try to activate in btrfs_chunk_alloc()
4223 spin_lock(&space_info->lock); in btrfs_chunk_alloc()
4225 if (ret == -ENOSPC) in btrfs_chunk_alloc()
4226 space_info->full = 1; in btrfs_chunk_alloc()
4231 space_info->max_extent_size = 0; in btrfs_chunk_alloc()
4234 space_info->force_alloc = CHUNK_ALLOC_NO_FORCE; in btrfs_chunk_alloc()
4236 space_info->chunk_alloc = 0; in btrfs_chunk_alloc()
4237 spin_unlock(&space_info->lock); in btrfs_chunk_alloc()
4238 mutex_unlock(&fs_info->chunk_mutex); in btrfs_chunk_alloc()
4249 num_dev = fs_info->fs_devices->rw_devices; in get_profile_num_devs()
4258 struct btrfs_fs_info *fs_info = trans->fs_info; in reserve_chunk_space()
4265 * atomic and race free space reservation in the chunk block reserve. in reserve_chunk_space()
4267 lockdep_assert_held(&fs_info->chunk_mutex); in reserve_chunk_space()
4270 spin_lock(&info->lock); in reserve_chunk_space()
4271 left = info->total_bytes - btrfs_space_info_used(info, true); in reserve_chunk_space()
4272 spin_unlock(&info->lock); in reserve_chunk_space()
4307 * the cases described at do_chunk_alloc() - the system in reserve_chunk_space()
4308 * block group we just created was just turned into RO in reserve_chunk_space()
4318 &fs_info->chunk_block_rsv, in reserve_chunk_space()
4321 trans->chunk_bytes_reserved += bytes; in reserve_chunk_space()
4327 * The caller must be holding fs_info->chunk_mutex.
4331 struct btrfs_fs_info *fs_info = trans->fs_info; in check_system_chunk()
4352 * block group allocation and removal, to avoid a deadlock with a concurrent
4353 * task that is allocating a metadata or data block group and therefore needs to
4361 struct btrfs_fs_info *fs_info = trans->fs_info; in btrfs_reserve_chunk_metadata()
4369 mutex_lock(&fs_info->chunk_mutex); in btrfs_reserve_chunk_metadata()
4371 mutex_unlock(&fs_info->chunk_mutex); in btrfs_reserve_chunk_metadata()
4381 spin_lock(&block_group->lock); in btrfs_put_block_group_cache()
4383 &block_group->runtime_flags)) { in btrfs_put_block_group_cache()
4384 struct btrfs_inode *inode = block_group->inode; in btrfs_put_block_group_cache()
4386 block_group->inode = NULL; in btrfs_put_block_group_cache()
4387 spin_unlock(&block_group->lock); in btrfs_put_block_group_cache()
4389 ASSERT(block_group->io_ctl.inode == NULL); in btrfs_put_block_group_cache()
4390 iput(&inode->vfs_inode); in btrfs_put_block_group_cache()
4392 spin_unlock(&block_group->lock); in btrfs_put_block_group_cache()
4399 * Must be called only after stopping all workers, since we could have block
4401 * freed the block groups before stopping them.
4411 if (info->active_meta_bg) { in btrfs_free_block_groups()
4412 btrfs_put_block_group(info->active_meta_bg); in btrfs_free_block_groups()
4413 info->active_meta_bg = NULL; in btrfs_free_block_groups()
4415 if (info->active_system_bg) { in btrfs_free_block_groups()
4416 btrfs_put_block_group(info->active_system_bg); in btrfs_free_block_groups()
4417 info->active_system_bg = NULL; in btrfs_free_block_groups()
4421 write_lock(&info->block_group_cache_lock); in btrfs_free_block_groups()
4422 while (!list_empty(&info->caching_block_groups)) { in btrfs_free_block_groups()
4423 caching_ctl = list_entry(info->caching_block_groups.next, in btrfs_free_block_groups()
4425 list_del(&caching_ctl->list); in btrfs_free_block_groups()
4428 write_unlock(&info->block_group_cache_lock); in btrfs_free_block_groups()
4430 spin_lock(&info->unused_bgs_lock); in btrfs_free_block_groups()
4431 while (!list_empty(&info->unused_bgs)) { in btrfs_free_block_groups()
4432 block_group = list_first_entry(&info->unused_bgs, in btrfs_free_block_groups()
4435 list_del_init(&block_group->bg_list); in btrfs_free_block_groups()
4439 while (!list_empty(&info->reclaim_bgs)) { in btrfs_free_block_groups()
4440 block_group = list_first_entry(&info->reclaim_bgs, in btrfs_free_block_groups()
4443 list_del_init(&block_group->bg_list); in btrfs_free_block_groups()
4446 spin_unlock(&info->unused_bgs_lock); in btrfs_free_block_groups()
4448 spin_lock(&info->zone_active_bgs_lock); in btrfs_free_block_groups()
4449 while (!list_empty(&info->zone_active_bgs)) { in btrfs_free_block_groups()
4450 block_group = list_first_entry(&info->zone_active_bgs, in btrfs_free_block_groups()
4453 list_del_init(&block_group->active_bg_list); in btrfs_free_block_groups()
4456 spin_unlock(&info->zone_active_bgs_lock); in btrfs_free_block_groups()
4458 write_lock(&info->block_group_cache_lock); in btrfs_free_block_groups()
4459 while ((n = rb_last(&info->block_group_cache_tree.rb_root)) != NULL) { in btrfs_free_block_groups()
4462 rb_erase_cached(&block_group->cache_node, in btrfs_free_block_groups()
4463 &info->block_group_cache_tree); in btrfs_free_block_groups()
4464 RB_CLEAR_NODE(&block_group->cache_node); in btrfs_free_block_groups()
4465 write_unlock(&info->block_group_cache_lock); in btrfs_free_block_groups()
4467 down_write(&block_group->space_info->groups_sem); in btrfs_free_block_groups()
4468 list_del(&block_group->list); in btrfs_free_block_groups()
4469 up_write(&block_group->space_info->groups_sem); in btrfs_free_block_groups()
4472 * We haven't cached this block group, which means we could in btrfs_free_block_groups()
4473 * possibly have excluded extents on this block group. in btrfs_free_block_groups()
4475 if (block_group->cached == BTRFS_CACHE_NO || in btrfs_free_block_groups()
4476 block_group->cached == BTRFS_CACHE_ERROR) in btrfs_free_block_groups()
4480 ASSERT(block_group->cached != BTRFS_CACHE_STARTED); in btrfs_free_block_groups()
4481 ASSERT(list_empty(&block_group->dirty_list)); in btrfs_free_block_groups()
4482 ASSERT(list_empty(&block_group->io_list)); in btrfs_free_block_groups()
4483 ASSERT(list_empty(&block_group->bg_list)); in btrfs_free_block_groups()
4484 ASSERT(refcount_read(&block_group->refs) == 1); in btrfs_free_block_groups()
4485 ASSERT(block_group->swap_extents == 0); in btrfs_free_block_groups()
4488 write_lock(&info->block_group_cache_lock); in btrfs_free_block_groups()
4490 write_unlock(&info->block_group_cache_lock); in btrfs_free_block_groups()
4494 while (!list_empty(&info->space_info)) { in btrfs_free_block_groups()
4495 space_info = list_entry(info->space_info.next, in btrfs_free_block_groups()
4503 if (WARN_ON(space_info->bytes_pinned > 0 || in btrfs_free_block_groups()
4504 space_info->bytes_may_use > 0)) in btrfs_free_block_groups()
4514 if (!(space_info->flags & BTRFS_BLOCK_GROUP_METADATA) || in btrfs_free_block_groups()
4516 if (WARN_ON(space_info->bytes_reserved > 0)) in btrfs_free_block_groups()
4520 WARN_ON(space_info->reclaim_size > 0); in btrfs_free_block_groups()
4521 list_del(&space_info->list); in btrfs_free_block_groups()
4527 void btrfs_freeze_block_group(struct btrfs_block_group *cache) in btrfs_freeze_block_group() argument
4529 atomic_inc(&cache->frozen); in btrfs_freeze_block_group()
4534 struct btrfs_fs_info *fs_info = block_group->fs_info; in btrfs_unfreeze_block_group()
4537 spin_lock(&block_group->lock); in btrfs_unfreeze_block_group()
4538 cleanup = (atomic_dec_and_test(&block_group->frozen) && in btrfs_unfreeze_block_group()
4539 test_bit(BLOCK_GROUP_FLAG_REMOVED, &block_group->runtime_flags)); in btrfs_unfreeze_block_group()
4540 spin_unlock(&block_group->lock); in btrfs_unfreeze_block_group()
4545 map = btrfs_find_chunk_map(fs_info, block_group->start, 1); in btrfs_unfreeze_block_group()
4556 * tasks trimming this block group have left 1 entry each one. in btrfs_unfreeze_block_group()
4567 spin_lock(&bg->lock); in btrfs_inc_block_group_swap_extents()
4568 if (bg->ro) in btrfs_inc_block_group_swap_extents()
4571 bg->swap_extents++; in btrfs_inc_block_group_swap_extents()
4572 spin_unlock(&bg->lock); in btrfs_inc_block_group_swap_extents()
4579 spin_lock(&bg->lock); in btrfs_dec_block_group_swap_extents()
4580 ASSERT(!bg->ro); in btrfs_dec_block_group_swap_extents()
4581 ASSERT(bg->swap_extents >= amount); in btrfs_dec_block_group_swap_extents()
4582 bg->swap_extents -= amount; in btrfs_dec_block_group_swap_extents()
4583 spin_unlock(&bg->lock); in btrfs_dec_block_group_swap_extents()
4596 * Handle a block group allocating an extent in a size class
4598 * @bg: The block group we allocated in.
4603 * Returns: 0 if the size class was valid for this block_group, -EAGAIN in the
4607 * find_free_extent will skip block groups with a mismatched size class until
4609 * force_wrong_size_class. However, if a block group is newly allocated and
4621 if (bg->size_class == size_class) in btrfs_use_block_group_size_class()
4633 if (bg->size_class != BTRFS_BG_SZ_NONE) { in btrfs_use_block_group_size_class()
4636 return -EAGAIN; in btrfs_use_block_group_size_class()
4639 * The happy new block group case: the new allocation is the first in btrfs_use_block_group_size_class()
4642 bg->size_class = size_class; in btrfs_use_block_group_size_class()
4649 if (btrfs_is_zoned(bg->fs_info)) in btrfs_block_group_should_use_size_class()