Lines Matching +full:count +full:- +full:threshold
1 // SPDX-License-Identifier: GPL-2.0-only
8 #include "dm-space-map.h"
9 #include "dm-space-map-common.h"
10 #include "dm-space-map-metadata.h"
14 #include <linux/device-mapper.h>
19 /*----------------------------------------------------------------*/
22 * An edge triggered threshold.
24 struct threshold { struct
27 dm_block_t threshold; argument
33 static void threshold_init(struct threshold *t) in threshold_init() argument
35 t->threshold_set = false; in threshold_init()
36 t->value_set = false; in threshold_init()
39 static void set_threshold(struct threshold *t, dm_block_t value, in set_threshold()
42 t->threshold_set = true; in set_threshold()
43 t->threshold = value; in set_threshold()
44 t->fn = fn; in set_threshold()
45 t->context = context; in set_threshold()
48 static bool below_threshold(struct threshold *t, dm_block_t value) in below_threshold()
50 return t->threshold_set && value <= t->threshold; in below_threshold()
53 static bool threshold_already_triggered(struct threshold *t) in threshold_already_triggered()
55 return t->value_set && below_threshold(t, t->current_value); in threshold_already_triggered()
58 static void check_threshold(struct threshold *t, dm_block_t value) in check_threshold()
62 t->fn(t->context); in check_threshold()
64 t->value_set = true; in check_threshold()
65 t->current_value = value; in check_threshold()
68 /*----------------------------------------------------------------*/
76 * For this reason we have a pool of pre-allocated blocks large enough to
105 brb->begin = 0; in brb_init()
106 brb->end = 0; in brb_init()
111 return brb->begin == brb->end; in brb_empty()
118 return r >= ARRAY_SIZE(brb->bops) ? 0 : r; in brb_next()
125 unsigned int next = brb_next(brb, brb->end); in brb_push()
131 if (next == brb->begin) in brb_push()
132 return -ENOMEM; in brb_push()
134 bop = brb->bops + brb->end; in brb_push()
135 bop->type = type; in brb_push()
136 bop->b = b; in brb_push()
137 bop->e = e; in brb_push()
139 brb->end = next; in brb_push()
149 return -ENODATA; in brb_peek()
151 bop = brb->bops + brb->begin; in brb_peek()
159 return -ENODATA; in brb_pop()
161 brb->begin = brb_next(brb, brb->begin); in brb_pop()
166 /*----------------------------------------------------------------*/
180 struct threshold threshold; member
185 int r = brb_push(&smm->uncommitted, type, b, e); in add_bop()
189 return -ENOMEM; in add_bop()
200 switch (op->type) { in commit_bop()
202 r = sm_ll_inc(&smm->ll, op->b, op->e, &nr_allocations); in commit_bop()
206 r = sm_ll_dec(&smm->ll, op->b, op->e, &nr_allocations); in commit_bop()
215 smm->recursion_count++; in in()
222 while (!brb_empty(&smm->uncommitted)) { in apply_bops()
225 r = brb_peek(&smm->uncommitted, &bop); in apply_bops()
235 brb_pop(&smm->uncommitted); in apply_bops()
248 if (!smm->recursion_count) { in out()
250 return -ENOMEM; in out()
253 if (smm->recursion_count == 1) in out()
256 smm->recursion_count--; in out()
273 return smm->recursion_count; in recursing()
283 static int sm_metadata_get_nr_blocks(struct dm_space_map *sm, dm_block_t *count) in sm_metadata_get_nr_blocks() argument
287 *count = smm->ll.nr_blocks; in sm_metadata_get_nr_blocks()
292 static int sm_metadata_get_nr_free(struct dm_space_map *sm, dm_block_t *count) in sm_metadata_get_nr_free() argument
296 *count = smm->old_ll.nr_blocks - smm->old_ll.nr_allocated - in sm_metadata_get_nr_free()
297 smm->allocated_this_transaction; in sm_metadata_get_nr_free()
314 for (i = smm->uncommitted.begin; in sm_metadata_get_count()
315 i != smm->uncommitted.end; in sm_metadata_get_count()
316 i = brb_next(&smm->uncommitted, i)) { in sm_metadata_get_count()
317 struct block_op *op = smm->uncommitted.bops + i; in sm_metadata_get_count()
319 if (b < op->b || b >= op->e) in sm_metadata_get_count()
322 switch (op->type) { in sm_metadata_get_count()
328 adjustment--; in sm_metadata_get_count()
333 r = sm_ll_lookup(&smm->ll, b, result); in sm_metadata_get_count()
354 for (i = smm->uncommitted.begin; in sm_metadata_count_is_more_than_one()
355 i != smm->uncommitted.end; in sm_metadata_count_is_more_than_one()
356 i = brb_next(&smm->uncommitted, i)) { in sm_metadata_count_is_more_than_one()
358 struct block_op *op = smm->uncommitted.bops + i; in sm_metadata_count_is_more_than_one()
360 if (b < op->b || b >= op->e) in sm_metadata_count_is_more_than_one()
363 switch (op->type) { in sm_metadata_count_is_more_than_one()
369 adjustment--; in sm_metadata_count_is_more_than_one()
379 r = sm_ll_lookup_bitmap(&smm->ll, b, &rc); in sm_metadata_count_is_more_than_one()
395 uint32_t count) in sm_metadata_set_count() argument
401 if (smm->recursion_count) { in sm_metadata_set_count()
403 return -EINVAL; in sm_metadata_set_count()
407 r = sm_ll_insert(&smm->ll, b, count, &nr_allocations); in sm_metadata_set_count()
425 r = sm_ll_inc(&smm->ll, b, e, &nr_allocations); in sm_metadata_inc_blocks()
442 r = sm_ll_dec(&smm->ll, b, e, &nr_allocations); in sm_metadata_dec_blocks()
458 r = sm_ll_find_common_free_block(&smm->old_ll, &smm->ll, smm->begin, smm->ll.nr_blocks, b); in sm_metadata_new_block_()
459 if (r == -ENOSPC) { in sm_metadata_new_block_()
461 * There's no free block between smm->begin and the end of the metadata device. in sm_metadata_new_block_()
462 * We search before smm->begin in case something has been freed. in sm_metadata_new_block_()
464 r = sm_ll_find_common_free_block(&smm->old_ll, &smm->ll, 0, smm->begin, b); in sm_metadata_new_block_()
470 smm->begin = *b + 1; in sm_metadata_new_block_()
476 r = sm_ll_inc(&smm->ll, *b, *b + 1, &nr_allocations); in sm_metadata_new_block_()
481 smm->allocated_this_transaction++; in sm_metadata_new_block_()
488 dm_block_t count; in sm_metadata_new_block() local
498 r = sm_metadata_get_nr_free(sm, &count); in sm_metadata_new_block()
500 DMERR_LIMIT("couldn't get free block count"); in sm_metadata_new_block()
504 check_threshold(&smm->threshold, count); in sm_metadata_new_block()
514 r = sm_ll_commit(&smm->ll); in sm_metadata_commit()
518 memcpy(&smm->old_ll, &smm->ll, sizeof(smm->old_ll)); in sm_metadata_commit()
519 smm->allocated_this_transaction = 0; in sm_metadata_commit()
525 dm_block_t threshold, in sm_metadata_register_threshold_callback() argument
531 set_threshold(&smm->threshold, threshold, fn, context); in sm_metadata_register_threshold_callback()
548 root_le.nr_blocks = cpu_to_le64(smm->ll.nr_blocks); in sm_metadata_copy_root()
549 root_le.nr_allocated = cpu_to_le64(smm->ll.nr_allocated); in sm_metadata_copy_root()
550 root_le.bitmap_root = cpu_to_le64(smm->ll.bitmap_root); in sm_metadata_copy_root()
551 root_le.ref_count_root = cpu_to_le64(smm->ll.ref_count_root); in sm_metadata_copy_root()
554 return -ENOSPC; in sm_metadata_copy_root()
580 /*----------------------------------------------------------------*/
594 return -EINVAL; in sm_bootstrap_extend()
597 static int sm_bootstrap_get_nr_blocks(struct dm_space_map *sm, dm_block_t *count) in sm_bootstrap_get_nr_blocks() argument
601 *count = smm->ll.nr_blocks; in sm_bootstrap_get_nr_blocks()
606 static int sm_bootstrap_get_nr_free(struct dm_space_map *sm, dm_block_t *count) in sm_bootstrap_get_nr_free() argument
610 *count = smm->ll.nr_blocks - smm->begin; in sm_bootstrap_get_nr_free()
620 *result = (b < smm->begin) ? 1 : 0; in sm_bootstrap_get_count()
634 uint32_t count) in sm_bootstrap_set_count() argument
638 return -EINVAL; in sm_bootstrap_set_count()
648 if (smm->begin == smm->ll.nr_blocks) in sm_bootstrap_new_block()
649 return -ENOSPC; in sm_bootstrap_new_block()
651 *b = smm->begin++; in sm_bootstrap_new_block()
689 return -EINVAL; in sm_bootstrap_root_size()
697 return -EINVAL; in sm_bootstrap_copy_root()
717 /*----------------------------------------------------------------*/
723 dm_block_t old_len = smm->ll.nr_blocks; in sm_metadata_extend()
728 smm->begin = old_len; in sm_metadata_extend()
734 r = sm_ll_extend(&smm->ll, extra_blocks); in sm_metadata_extend()
743 r = add_bop(smm, BOP_INC, old_len, smm->begin); in sm_metadata_extend()
747 old_len = smm->begin; in sm_metadata_extend()
755 r = sm_ll_commit(&smm->ll); in sm_metadata_extend()
759 } while (old_len != smm->begin); in sm_metadata_extend()
769 /*----------------------------------------------------------------*/
777 return ERR_PTR(-ENOMEM); in dm_sm_metadata_init()
779 memcpy(&smm->sm, &ops, sizeof(smm->sm)); in dm_sm_metadata_init()
781 return &smm->sm; in dm_sm_metadata_init()
792 smm->begin = superblock + 1; in dm_sm_metadata_create()
793 smm->recursion_count = 0; in dm_sm_metadata_create()
794 smm->allocated_this_transaction = 0; in dm_sm_metadata_create()
795 brb_init(&smm->uncommitted); in dm_sm_metadata_create()
796 threshold_init(&smm->threshold); in dm_sm_metadata_create()
798 memcpy(&smm->sm, &bootstrap_ops, sizeof(smm->sm)); in dm_sm_metadata_create()
800 r = sm_ll_new_metadata(&smm->ll, tm); in dm_sm_metadata_create()
804 r = sm_ll_extend(&smm->ll, nr_blocks); in dm_sm_metadata_create()
806 memcpy(&smm->sm, &ops, sizeof(smm->sm)); in dm_sm_metadata_create()
814 r = add_bop(smm, BOP_INC, superblock, smm->begin); in dm_sm_metadata_create()
834 r = sm_ll_open_metadata(&smm->ll, tm, root_le, len); in dm_sm_metadata_open()
838 smm->begin = 0; in dm_sm_metadata_open()
839 smm->recursion_count = 0; in dm_sm_metadata_open()
840 smm->allocated_this_transaction = 0; in dm_sm_metadata_open()
841 brb_init(&smm->uncommitted); in dm_sm_metadata_open()
842 threshold_init(&smm->threshold); in dm_sm_metadata_open()
844 memcpy(&smm->old_ll, &smm->ll, sizeof(smm->old_ll)); in dm_sm_metadata_open()