From a176184b7d8005eb36b07044315e925c4414d9a4 Mon Sep 17 00:00:00 2001 From: Zdenek Kabelac Date: Thu, 7 Dec 2023 18:07:56 +0100 Subject: [PATCH] thin_pool: code refactoring Move common code into thin_pool_set_params() (just like we use cache_set_params). --- lib/metadata/lv_manip.c | 16 +++++++--------- lib/metadata/metadata-exported.h | 7 +++++++ lib/metadata/thin_manip.c | 26 ++++++++++++++++++++++++++ tools/lvconvert.c | 23 ++++++++++------------- 4 files changed, 50 insertions(+), 22 deletions(-) diff --git a/lib/metadata/lv_manip.c b/lib/metadata/lv_manip.c index 55c411af7..3ecb58b2c 100644 --- a/lib/metadata/lv_manip.c +++ b/lib/metadata/lv_manip.c @@ -9486,18 +9486,16 @@ static struct logical_volume *_lv_create_an_lv(struct volume_group *vg, first_seg(lv)->min_recovery_rate = lp->min_recovery_rate; first_seg(lv)->max_recovery_rate = lp->max_recovery_rate; } else if (lv_is_thin_pool(lv)) { - first_seg(lv)->chunk_size = lp->chunk_size; - first_seg(lv)->zero_new_blocks = lp->zero_new_blocks; - first_seg(lv)->discards = lp->discards; - if ((first_seg(lv)->crop_metadata = lp->crop_metadata) == THIN_CROP_METADATA_NO) - lv->status |= LV_CROP_METADATA; - if (!recalculate_pool_chunk_size_with_dev_hints(lv, seg_lv(first_seg(lv), 0), - lp->thin_chunk_size_calc_policy)) { + if (!thin_pool_set_params(first_seg(lv), + lp->error_when_full, + lp->crop_metadata, + lp->thin_chunk_size_calc_policy, + lp->chunk_size, + lp->discards, + lp->zero_new_blocks)) { stack; goto revert_new_lv; } - if (lp->error_when_full) - lv->status |= LV_ERROR_WHEN_FULL; } else if (pool_lv && lv_is_virtual(lv) && /* not yet thin LV */ (seg = first_seg(lv)) && seg_is_thin(seg)) { /* going to be a thin volume */ diff --git a/lib/metadata/metadata-exported.h b/lib/metadata/metadata-exported.h index d735ca9a7..255474a56 100644 --- a/lib/metadata/metadata-exported.h +++ b/lib/metadata/metadata-exported.h @@ -909,6 +909,13 @@ int update_thin_pool_params(struct cmd_context *cmd, thin_crop_metadata_t *crop_metadata, int *chunk_size_calc_method, uint32_t *chunk_size, thin_discards_t *discards, thin_zero_t *zero_new_blocks); +int thin_pool_set_params(struct lv_segment *seg, + int error_when_full, + thin_crop_metadata_t crop_metadata, + int thin_chunk_size_calc_policy, + uint32_t chunk_size, + thin_discards_t discards, + thin_zero_t zero_new_blocks); struct lv_status_thin_pool { struct dm_pool *mem; diff --git a/lib/metadata/thin_manip.c b/lib/metadata/thin_manip.c index bb406a3d0..531ce6d3d 100644 --- a/lib/metadata/thin_manip.c +++ b/lib/metadata/thin_manip.c @@ -775,6 +775,32 @@ thin_crop_metadata_t get_thin_pool_crop_metadata(struct cmd_context *cmd, return crop; } +int thin_pool_set_params(struct lv_segment *seg, + int error_when_full, + thin_crop_metadata_t crop_metadata, + int thin_chunk_size_calc_policy, + uint32_t chunk_size, + thin_discards_t discards, + thin_zero_t zero_new_blocks) +{ + if (!recalculate_pool_chunk_size_with_dev_hints(seg->lv, seg_lv(seg, 0), + thin_chunk_size_calc_policy)) + return_0; + + if (error_when_full) + seg->lv->status |= LV_ERROR_WHEN_FULL; + + if ((seg->crop_metadata = crop_metadata) == THIN_CROP_METADATA_NO) + seg->lv->status |= LV_CROP_METADATA; + + seg->chunk_size = chunk_size; + seg->discards = discards; + seg->zero_new_blocks = zero_new_blocks; + seg->transaction_id = 0; + + return 1; +} + int update_thin_pool_params(struct cmd_context *cmd, struct profile *profile, uint32_t extent_size, diff --git a/tools/lvconvert.c b/tools/lvconvert.c index e947837d1..e16f1dc95 100644 --- a/tools/lvconvert.c +++ b/tools/lvconvert.c @@ -3423,23 +3423,20 @@ static int _lvconvert_to_pool(struct cmd_context *cmd, if (!cache_set_params(seg, chunk_size, cache_metadata_format, cache_mode, policy_name, policy_settings)) goto_bad; } else { - seg->transaction_id = 0; - seg->crop_metadata = crop_metadata; - seg->chunk_size = chunk_size; - seg->discards = discards; - seg->zero_new_blocks = zero_new_blocks; - if (crop_metadata == THIN_CROP_METADATA_NO) - pool_lv->status |= LV_CROP_METADATA; - if (!recalculate_pool_chunk_size_with_dev_hints(pool_lv, data_lv, chunk_calc)) - goto_bad; - /* Error when full */ if (arg_is_set(cmd, errorwhenfull_ARG)) - error_when_full = arg_uint_value(cmd, errorwhenfull_ARG, 0); + error_when_full = arg_int_value(cmd, errorwhenfull_ARG, 0); else error_when_full = find_config_tree_bool(cmd, activation_error_when_full_CFG, vg->profile); - if (error_when_full) - pool_lv->status |= LV_ERROR_WHEN_FULL; + + if (!thin_pool_set_params(seg, + error_when_full, + crop_metadata, + chunk_calc, + chunk_size, + discards, + zero_new_blocks)) + goto_bad; if (to_thin) { if (!thin_pool_prepare_metadata(metadata_lv, seg->chunk_size,