Lines Matching +full:full +full:- +full:size

1 // SPDX-License-Identifier: GPL-2.0
5 #include "block-rsv.h"
6 #include "space-info.h"
8 #include "block-group.h"
16 * reservations. Each block_rsv has a ->size and a ->reserved. ->size is
17 * how large we want our block rsv to be, ->reserved is how much space is
20 * ->failfast exists for the truncate case, and is described below.
24 * -> Reserve
28 * accounted for in space_info->bytes_may_use, and then add the bytes to
29 * ->reserved, and ->size in the case of btrfs_block_rsv_add.
31 * ->size is an over-estimation of how much we may use for a particular
34 * -> Use
39 * ->reserved has enough space for our tree block allocation. Once
40 * successful we subtract fs_info->nodesize from ->reserved.
42 * -> Finish
46 * from ->size, and then subtract ->size from ->reserved and free up the
51 * space_info->bytes_may_use.
70 * The individual item sizes are determined by the per-inode size
72 * straightforward, it's just the calculation of ->size encodes a lot of
78 * We assume each one of these delayed refs are going to use a full
79 * reservation. We use the transaction items and pre-reserve space for every
80 * operation, and use this reservation to refill any gap between ->size and
81 * ->reserved that may exist.
84 * count from ->size and free up reservations as necessary. Since this is
96 * allocate a block reserve, set it to some size, and then truncate bytes
97 * until we have no space left. With ->failfast set we'll simply return
101 * re-reserve.
109 struct btrfs_space_info *space_info = block_rsv->space_info; in block_rsv_release_bytes()
113 spin_lock(&block_rsv->lock); in block_rsv_release_bytes()
114 if (num_bytes == (u64)-1) { in block_rsv_release_bytes()
115 num_bytes = block_rsv->size; in block_rsv_release_bytes()
116 qgroup_to_release = block_rsv->qgroup_rsv_size; in block_rsv_release_bytes()
118 block_rsv->size -= num_bytes; in block_rsv_release_bytes()
119 if (block_rsv->reserved >= block_rsv->size) { in block_rsv_release_bytes()
120 num_bytes = block_rsv->reserved - block_rsv->size; in block_rsv_release_bytes()
121 block_rsv->reserved = block_rsv->size; in block_rsv_release_bytes()
122 block_rsv->full = true; in block_rsv_release_bytes()
127 block_rsv->qgroup_rsv_reserved >= block_rsv->qgroup_rsv_size) { in block_rsv_release_bytes()
128 qgroup_to_release = block_rsv->qgroup_rsv_reserved - in block_rsv_release_bytes()
129 block_rsv->qgroup_rsv_size; in block_rsv_release_bytes()
130 block_rsv->qgroup_rsv_reserved = block_rsv->qgroup_rsv_size; in block_rsv_release_bytes()
134 spin_unlock(&block_rsv->lock); in block_rsv_release_bytes()
139 spin_lock(&dest->lock); in block_rsv_release_bytes()
140 if (!dest->full) { in block_rsv_release_bytes()
143 bytes_to_add = dest->size - dest->reserved; in block_rsv_release_bytes()
145 dest->reserved += bytes_to_add; in block_rsv_release_bytes()
146 if (dest->reserved >= dest->size) in block_rsv_release_bytes()
147 dest->full = true; in block_rsv_release_bytes()
148 num_bytes -= bytes_to_add; in block_rsv_release_bytes()
150 spin_unlock(&dest->lock); in block_rsv_release_bytes()
179 spin_lock_init(&rsv->lock); in btrfs_init_block_rsv()
180 rsv->type = type; in btrfs_init_block_rsv()
188 rsv->space_info = btrfs_find_space_info(fs_info, in btrfs_init_metadata_block_rsv()
210 btrfs_block_rsv_release(fs_info, rsv, (u64)-1, NULL); in btrfs_free_block_rsv()
223 ret = btrfs_reserve_metadata_bytes(fs_info, block_rsv->space_info, in btrfs_block_rsv_add()
234 int ret = -ENOSPC; in btrfs_block_rsv_check()
236 spin_lock(&block_rsv->lock); in btrfs_block_rsv_check()
237 num_bytes = mult_perc(block_rsv->size, min_percent); in btrfs_block_rsv_check()
238 if (block_rsv->reserved >= num_bytes) in btrfs_block_rsv_check()
240 spin_unlock(&block_rsv->lock); in btrfs_block_rsv_check()
249 int ret = -ENOSPC; in btrfs_block_rsv_refill()
254 spin_lock(&block_rsv->lock); in btrfs_block_rsv_refill()
255 if (block_rsv->reserved >= num_bytes) in btrfs_block_rsv_refill()
258 num_bytes -= block_rsv->reserved; in btrfs_block_rsv_refill()
259 spin_unlock(&block_rsv->lock); in btrfs_block_rsv_refill()
264 ret = btrfs_reserve_metadata_bytes(fs_info, block_rsv->space_info, in btrfs_block_rsv_refill()
278 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; in btrfs_block_rsv_release()
279 struct btrfs_block_rsv *delayed_rsv = &fs_info->delayed_refs_rsv; in btrfs_block_rsv_release()
284 * otherwise dump into the global delayed reserve if it is not full. in btrfs_block_rsv_release()
286 if (block_rsv->type == BTRFS_BLOCK_RSV_DELOPS) in btrfs_block_rsv_release()
291 if (target && block_rsv->space_info != target->space_info) in btrfs_block_rsv_release()
300 int ret = -ENOSPC; in btrfs_block_rsv_use_bytes()
302 spin_lock(&block_rsv->lock); in btrfs_block_rsv_use_bytes()
303 if (block_rsv->reserved >= num_bytes) { in btrfs_block_rsv_use_bytes()
304 block_rsv->reserved -= num_bytes; in btrfs_block_rsv_use_bytes()
305 if (block_rsv->reserved < block_rsv->size) in btrfs_block_rsv_use_bytes()
306 block_rsv->full = false; in btrfs_block_rsv_use_bytes()
309 spin_unlock(&block_rsv->lock); in btrfs_block_rsv_use_bytes()
316 spin_lock(&block_rsv->lock); in btrfs_block_rsv_add_bytes()
317 block_rsv->reserved += num_bytes; in btrfs_block_rsv_add_bytes()
319 block_rsv->size += num_bytes; in btrfs_block_rsv_add_bytes()
320 else if (block_rsv->reserved >= block_rsv->size) in btrfs_block_rsv_add_bytes()
321 block_rsv->full = true; in btrfs_block_rsv_add_bytes()
322 spin_unlock(&block_rsv->lock); in btrfs_block_rsv_add_bytes()
327 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; in btrfs_update_global_block_rsv()
328 struct btrfs_space_info *sinfo = block_rsv->space_info; in btrfs_update_global_block_rsv()
330 u64 num_bytes = btrfs_root_used(&fs_info->tree_root->root_item); in btrfs_update_global_block_rsv()
334 * The global block rsv is based on the size of the extent tree, the in btrfs_update_global_block_rsv()
341 read_lock(&fs_info->global_root_lock); in btrfs_update_global_block_rsv()
342 rbtree_postorder_for_each_entry_safe(root, tmp, &fs_info->global_root_tree, in btrfs_update_global_block_rsv()
347 num_bytes += btrfs_root_used(&root->root_item); in btrfs_update_global_block_rsv()
351 read_unlock(&fs_info->global_root_lock); in btrfs_update_global_block_rsv()
354 num_bytes += btrfs_root_used(&fs_info->block_group_root->root_item); in btrfs_update_global_block_rsv()
359 num_bytes += btrfs_root_used(&fs_info->stripe_root->root_item); in btrfs_update_global_block_rsv()
379 spin_lock(&sinfo->lock); in btrfs_update_global_block_rsv()
380 spin_lock(&block_rsv->lock); in btrfs_update_global_block_rsv()
382 block_rsv->size = min_t(u64, num_bytes, SZ_512M); in btrfs_update_global_block_rsv()
384 if (block_rsv->reserved < block_rsv->size) { in btrfs_update_global_block_rsv()
385 num_bytes = block_rsv->size - block_rsv->reserved; in btrfs_update_global_block_rsv()
388 block_rsv->reserved = block_rsv->size; in btrfs_update_global_block_rsv()
389 } else if (block_rsv->reserved > block_rsv->size) { in btrfs_update_global_block_rsv()
390 num_bytes = block_rsv->reserved - block_rsv->size; in btrfs_update_global_block_rsv()
392 -num_bytes); in btrfs_update_global_block_rsv()
393 block_rsv->reserved = block_rsv->size; in btrfs_update_global_block_rsv()
397 block_rsv->full = (block_rsv->reserved == block_rsv->size); in btrfs_update_global_block_rsv()
399 if (block_rsv->size >= sinfo->total_bytes) in btrfs_update_global_block_rsv()
400 sinfo->force_alloc = CHUNK_ALLOC_FORCE; in btrfs_update_global_block_rsv()
401 spin_unlock(&block_rsv->lock); in btrfs_update_global_block_rsv()
402 spin_unlock(&sinfo->lock); in btrfs_update_global_block_rsv()
407 struct btrfs_fs_info *fs_info = root->fs_info; in btrfs_init_root_block_rsv()
415 root->block_rsv = &fs_info->delayed_refs_rsv; in btrfs_init_root_block_rsv()
420 root->block_rsv = &fs_info->global_block_rsv; in btrfs_init_root_block_rsv()
423 root->block_rsv = &fs_info->chunk_block_rsv; in btrfs_init_root_block_rsv()
426 root->block_rsv = NULL; in btrfs_init_root_block_rsv()
436 fs_info->chunk_block_rsv.space_info = space_info; in btrfs_init_global_block_rsv()
439 fs_info->global_block_rsv.space_info = space_info; in btrfs_init_global_block_rsv()
440 fs_info->trans_block_rsv.space_info = space_info; in btrfs_init_global_block_rsv()
441 fs_info->empty_block_rsv.space_info = space_info; in btrfs_init_global_block_rsv()
442 fs_info->delayed_block_rsv.space_info = space_info; in btrfs_init_global_block_rsv()
443 fs_info->delayed_refs_rsv.space_info = space_info; in btrfs_init_global_block_rsv()
450 btrfs_block_rsv_release(fs_info, &fs_info->global_block_rsv, (u64)-1, in btrfs_release_global_block_rsv()
452 WARN_ON(fs_info->trans_block_rsv.size > 0); in btrfs_release_global_block_rsv()
453 WARN_ON(fs_info->trans_block_rsv.reserved > 0); in btrfs_release_global_block_rsv()
454 WARN_ON(fs_info->chunk_block_rsv.size > 0); in btrfs_release_global_block_rsv()
455 WARN_ON(fs_info->chunk_block_rsv.reserved > 0); in btrfs_release_global_block_rsv()
456 WARN_ON(fs_info->delayed_block_rsv.size > 0); in btrfs_release_global_block_rsv()
457 WARN_ON(fs_info->delayed_block_rsv.reserved > 0); in btrfs_release_global_block_rsv()
458 WARN_ON(fs_info->delayed_refs_rsv.reserved > 0); in btrfs_release_global_block_rsv()
459 WARN_ON(fs_info->delayed_refs_rsv.size > 0); in btrfs_release_global_block_rsv()
466 struct btrfs_fs_info *fs_info = root->fs_info; in get_block_rsv()
469 if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state) || in get_block_rsv()
470 (root == fs_info->uuid_root) || in get_block_rsv()
471 (trans->adding_csums && btrfs_root_id(root) == BTRFS_CSUM_TREE_OBJECTID)) in get_block_rsv()
472 block_rsv = trans->block_rsv; in get_block_rsv()
475 block_rsv = root->block_rsv; in get_block_rsv()
478 block_rsv = &fs_info->empty_block_rsv; in get_block_rsv()
487 struct btrfs_fs_info *fs_info = root->fs_info; in btrfs_use_block_rsv()
489 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; in btrfs_use_block_rsv()
502 if (block_rsv->failfast) in btrfs_use_block_rsv()
505 if (block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL && !global_updated) { in btrfs_use_block_rsv()
515 if (block_rsv->type != BTRFS_BLOCK_RSV_DELREFS && in btrfs_use_block_rsv()
523 block_rsv->type, ret); in btrfs_use_block_rsv()
526 ret = btrfs_reserve_metadata_bytes(fs_info, block_rsv->space_info, in btrfs_use_block_rsv()
535 if (block_rsv->type != BTRFS_BLOCK_RSV_GLOBAL && in btrfs_use_block_rsv()
536 block_rsv->space_info == global_rsv->space_info) { in btrfs_use_block_rsv()
548 ret = btrfs_reserve_metadata_bytes(fs_info, block_rsv->space_info, blocksize, in btrfs_use_block_rsv()
566 spin_lock(&rsv->lock); in btrfs_check_trunc_cache_free_space()
567 if (rsv->reserved < needed_bytes) in btrfs_check_trunc_cache_free_space()
568 ret = -ENOSPC; in btrfs_check_trunc_cache_free_space()
571 spin_unlock(&rsv->lock); in btrfs_check_trunc_cache_free_space()