From 5dbc97a563f381ccc96db6f6277a4f75bc232861 Mon Sep 17 00:00:00 2001 From: David Teigland Date: Tue, 13 Dec 2016 14:13:29 -0600 Subject: lvconvert: move code Should be no functional changes. lvconvert.c type conversions for raid/mirror/linear/striped lvconvert_pool.c thin/cache/pool creation and utilities lvconvert_snapshot.c cow snapshot creation and utilties lvconvert_other.c generic commands that are routed to a specific command in another file once the LV type is known. lvconvert_poll.c polling utilities --- tools/Makefile.in | 3 + tools/lvconvert.c | 3497 ++++++-------------------------------------- tools/lvconvert_other.c | 350 +++++ tools/lvconvert_poll.c | 115 ++ tools/lvconvert_poll.h | 20 + tools/lvconvert_pool.c | 1660 +++++++++++++++++++++ tools/lvconvert_snapshot.c | 423 ++++++ tools/toollib.c | 37 + tools/toollib.h | 2 + tools/tools.h | 24 + 10 files changed, 3096 insertions(+), 3035 deletions(-) create mode 100644 tools/lvconvert_other.c create mode 100644 tools/lvconvert_pool.c create mode 100644 tools/lvconvert_snapshot.c diff --git a/tools/Makefile.in b/tools/Makefile.in index 887bc697a..c9e5d4415 100644 --- a/tools/Makefile.in +++ b/tools/Makefile.in @@ -21,6 +21,9 @@ SOURCES =\ formats.c \ lvchange.c \ lvconvert.c \ + lvconvert_pool.c \ + lvconvert_snapshot.c \ + lvconvert_other.c \ lvconvert_poll.c \ lvcreate.c \ lvdisplay.c \ diff --git a/tools/lvconvert.c b/tools/lvconvert.c index adc0d14b2..d7f789e6f 100644 --- a/tools/lvconvert.c +++ b/tools/lvconvert.c @@ -86,12 +86,6 @@ struct lvconvert_params { const char *origin_name; }; -struct convert_poll_id_list { - struct dm_list list; - struct poll_operation_id *id; - unsigned is_merging_origin:1; - unsigned is_merging_origin_thin:1; -}; /* FIXME Temporary function until the enum replaces the separate variables */ static void _set_conv_type(struct lvconvert_params *lp, int conv_type) @@ -406,100 +400,6 @@ static int _read_params(struct cmd_context *cmd, int argc, char **argv, return 1; } -static struct poll_functions _lvconvert_mirror_fns = { - .poll_progress = poll_mirror_progress, - .finish_copy = lvconvert_mirror_finish, -}; - -static struct poll_functions _lvconvert_merge_fns = { - .poll_progress = poll_merge_progress, - .finish_copy = lvconvert_merge_finish, -}; - -static struct poll_functions _lvconvert_thin_merge_fns = { - .poll_progress = poll_thin_merge_progress, - .finish_copy = lvconvert_merge_finish, -}; - -static struct poll_operation_id *_create_id(struct cmd_context *cmd, - const char *vg_name, - const char *lv_name, - const char *uuid) -{ - struct poll_operation_id *id; - char lv_full_name[NAME_LEN]; - - if (!vg_name || !lv_name || !uuid) { - log_error(INTERNAL_ERROR "Wrong params for lvconvert _create_id."); - return NULL; - } - - if (dm_snprintf(lv_full_name, sizeof(lv_full_name), "%s/%s", vg_name, lv_name) < 0) { - log_error(INTERNAL_ERROR "Name \"%s/%s\" is too long.", vg_name, lv_name); - return NULL; - } - - if (!(id = dm_pool_alloc(cmd->mem, sizeof(*id)))) { - log_error("Poll operation ID allocation failed."); - return NULL; - } - - if (!(id->display_name = dm_pool_strdup(cmd->mem, lv_full_name)) || - !(id->lv_name = strchr(id->display_name, '/')) || - !(id->vg_name = dm_pool_strdup(cmd->mem, vg_name)) || - !(id->uuid = dm_pool_strdup(cmd->mem, uuid))) { - log_error("Failed to copy one or more poll operation ID members."); - dm_pool_free(cmd->mem, id); - return NULL; - } - - id->lv_name++; /* skip over '/' */ - - return id; -} - -static int _lvconvert_poll_by_id(struct cmd_context *cmd, struct poll_operation_id *id, - unsigned background, - int is_merging_origin, - int is_merging_origin_thin) -{ - if (test_mode()) - return ECMD_PROCESSED; - - if (is_merging_origin) - return poll_daemon(cmd, background, - (MERGING | (is_merging_origin_thin ? THIN_VOLUME : SNAPSHOT)), - is_merging_origin_thin ? &_lvconvert_thin_merge_fns : &_lvconvert_merge_fns, - "Merged", id); - else - return poll_daemon(cmd, background, CONVERTING, - &_lvconvert_mirror_fns, "Converted", id); -} - -int lvconvert_poll(struct cmd_context *cmd, struct logical_volume *lv, - unsigned background) -{ - int r; - struct poll_operation_id *id = _create_id(cmd, lv->vg->name, lv->name, lv->lvid.s); - int is_merging_origin = 0; - int is_merging_origin_thin = 0; - - if (!id) { - log_error("Failed to allocate poll identifier for lvconvert."); - return ECMD_FAILED; - } - - /* FIXME: check this in polling instead */ - if (lv_is_merging_origin(lv)) { - is_merging_origin = 1; - is_merging_origin_thin = seg_is_thin_volume(find_snapshot(lv)); - } - - r = _lvconvert_poll_by_id(cmd, id, background, is_merging_origin, is_merging_origin_thin); - - return r; -} - static int _insert_lvconvert_layer(struct cmd_context *cmd, struct logical_volume *lv) { @@ -584,44 +484,6 @@ static int _failed_logs_count(struct logical_volume *lv) return ret; } - -static struct dm_list *_failed_pv_list(struct volume_group *vg) -{ - struct dm_list *failed_pvs; - struct pv_list *pvl, *new_pvl; - - if (!(failed_pvs = dm_pool_alloc(vg->vgmem, sizeof(*failed_pvs)))) { - log_error("Allocation of list of failed_pvs failed."); - return NULL; - } - - dm_list_init(failed_pvs); - - dm_list_iterate_items(pvl, &vg->pvs) { - if (!is_missing_pv(pvl->pv)) - continue; - - /* - * Finally, --repair will remove empty PVs. - * But we only want remove these which are output of repair, - * Do not count these which are already empty here. - * FIXME: code should traverse PV in LV not in whole VG. - * FIXME: layer violation? should it depend on vgreduce --removemising? - */ - if (pvl->pv->pe_alloc_count == 0) - continue; - - if (!(new_pvl = dm_pool_alloc(vg->vgmem, sizeof(*new_pvl)))) { - log_error("Allocation of failed_pvs list entry failed."); - return NULL; - } - new_pvl->pv = pvl->pv; - dm_list_add(failed_pvs, &new_pvl->list); - } - - return failed_pvs; -} - static int _is_partial_lv(struct logical_volume *lv, void *baton __attribute__((unused))) { @@ -781,43 +643,6 @@ static int _lv_update_log_type(struct cmd_context *cmd, is_mirror_image_removable, operable_pvs, 1U); } -/* - * Reomove missing and empty PVs from VG, if are also in provided list - */ -static void _remove_missing_empty_pv(struct volume_group *vg, struct dm_list *remove_pvs) -{ - struct pv_list *pvl, *pvl_vg, *pvlt; - int removed = 0; - - if (!remove_pvs) - return; - - dm_list_iterate_items(pvl, remove_pvs) { - dm_list_iterate_items_safe(pvl_vg, pvlt, &vg->pvs) { - if (!id_equal(&pvl->pv->id, &pvl_vg->pv->id) || - !is_missing_pv(pvl_vg->pv) || - pvl_vg->pv->pe_alloc_count != 0) - continue; - - /* FIXME: duplication of vgreduce code, move this to library */ - vg->free_count -= pvl_vg->pv->pe_count; - vg->extent_count -= pvl_vg->pv->pe_count; - del_pvl_from_vgs(vg, pvl_vg); - free_pv_fid(pvl_vg->pv); - - removed++; - } - } - - if (removed) { - if (!vg_write(vg) || !vg_commit(vg)) { - stack; - return; - } - log_warn("%d missing and now unallocated Physical Volumes removed from VG.", removed); - } -} - /* * _lvconvert_mirrors_parse_params * @@ -1135,7 +960,7 @@ int mirror_remove_missing(struct cmd_context *cmd, struct dm_list *failed_pvs; int log_count = _get_log_count(lv) - _failed_logs_count(lv); - if (!(failed_pvs = _failed_pv_list(lv->vg))) + if (!(failed_pvs = failed_pv_list(lv->vg))) return_0; if (force && _failed_mirrors_count(lv) == (int)lv_mirror_count(lv)) { @@ -1578,2973 +1403,629 @@ try_new_takeover_or_reshape: return 0; } -static int _lvconvert_splitsnapshot(struct cmd_context *cmd, struct logical_volume *cow) +int lvconvert_repair_pvs_mirror(struct cmd_context *cmd, struct logical_volume *lv, + struct processing_handle *handle, + struct dm_list *use_pvh) { - struct volume_group *vg = cow->vg; - const char *cow_name = display_lvname(cow); - - if (lv_is_virtual_origin(origin_from_cow(cow))) { - log_error("Unable to split off snapshot %s with virtual origin.", cow_name); - return 0; - } + struct lvconvert_result *lr = (struct lvconvert_result *) handle->custom_handle; + struct lvconvert_params lp = { 0 }; + struct convert_poll_id_list *idl; + struct lvinfo info; + int ret; - if (!(vg->fid->fmt->features & FMT_MDAS)) { - log_error("Unable to split off snapshot %s using old LVM1-style metadata.", cow_name); - return 0; - } + /* + * FIXME: temporary use of lp because _lvconvert_mirrors_repair() + * and _aux() still use lp fields everywhere. + * Migrate them away from using lp (for the most part just use + * local variables, and check arg_values directly). + */ - if (is_lockd_type(vg->lock_type)) { - /* FIXME: we need to create a lock for the new LV. */ - log_error("Unable to split snapshots in VG with lock_type %s.", vg->lock_type); - return 0; - } + /* + * Fill in any lp fields here that this fn expects to be set before + * it's called. It's hard to tell what the old code expects in lp + * for repair; it doesn't take the stripes option, but it seems to + * expect lp.stripes to be set to 1. + */ + lp.alloc = (alloc_policy_t) arg_uint_value(cmd, alloc_ARG, ALLOC_INHERIT); + lp.stripes = 1; - if (lv_is_active_locally(cow)) { - if (!lv_check_not_in_use(cow, 1)) - return_0; + ret = _lvconvert_mirrors_repair(cmd, lv, &lp, use_pvh); - if ((arg_count(cmd, force_ARG) == PROMPT) && - !arg_count(cmd, yes_ARG) && - lv_is_visible(cow) && - lv_is_active(cow)) { - if (yes_no_prompt("Do you really want to split off active " - "logical volume %s? [y/n]: ", cow_name) == 'n') { - log_error("Logical volume %s not split.", cow_name); + if (lp.need_polling) { + if (!lv_info(cmd, lv, 0, &info, 0, 0) || !info.exists) + log_print_unless_silent("Conversion starts after activation."); + else { + if (!(idl = convert_poll_id_list_create(cmd, lv))) return 0; - } + dm_list_add(&lr->poll_idls, &idl->list); } + lr->need_polling = 1; } - if (!archive(vg)) - return_0; - - log_verbose("Splitting snapshot %s from its origin.", cow_name); - - if (!vg_remove_snapshot(cow)) - return_0; - - backup(vg); - - log_print_unless_silent("Logical Volume %s split from its origin.", cow_name); - - return 1; + return ret; } -static int _lvconvert_split_and_keep_cachepool(struct cmd_context *cmd, - struct logical_volume *lv, - struct logical_volume *cachepool_lv) +static void _lvconvert_repair_pvs_raid_ask(struct cmd_context *cmd, int *do_it) { - log_debug("Detaching cache pool %s from cache LV %s.", - display_lvname(cachepool_lv), display_lvname(lv)); - - if (!archive(lv->vg)) - return_0; - - if (!lv_cache_remove(lv)) - return_0; - - if (!vg_write(lv->vg) || !vg_commit(lv->vg)) - return_0; - - backup(lv->vg); - - log_print_unless_silent("Logical volume %s is not cached and cache pool %s is unused.", - display_lvname(lv), display_lvname(cachepool_lv)); - - return 1; -} + const char *dev_policy; -static int _lvconvert_split_and_remove_cachepool(struct cmd_context *cmd, - struct logical_volume *lv, - struct logical_volume *cachepool_lv) -{ - struct lv_segment *seg; - struct logical_volume *remove_lv; + *do_it = 1; - seg = first_seg(lv); + if (arg_is_set(cmd, usepolicies_ARG)) { + dev_policy = find_config_tree_str(cmd, activation_raid_fault_policy_CFG, NULL); - if (lv_is_partial(seg_lv(seg, 0))) { - log_warn("WARNING: Cache origin logical volume %s is missing.", - display_lvname(seg_lv(seg, 0))); - remove_lv = lv; /* When origin is missing, drop everything */ - } else - remove_lv = seg->pool_lv; - - if (lv_is_partial(seg_lv(first_seg(seg->pool_lv), 0))) - log_warn("WARNING: Cache pool data logical volume %s is missing.", - display_lvname(seg_lv(first_seg(seg->pool_lv), 0))); - - if (lv_is_partial(first_seg(seg->pool_lv)->metadata_lv)) - log_warn("WARNING: Cache pool metadata logical volume %s is missing.", - display_lvname(first_seg(seg->pool_lv)->metadata_lv)); - - /* TODO: Check for failed cache as well to get prompting? */ - if (lv_is_partial(lv)) { - if (first_seg(seg->pool_lv)->cache_mode != CACHE_MODE_WRITETHROUGH) { - if (!arg_count(cmd, force_ARG)) { - log_error("Conversion aborted."); - log_error("Cannot uncache writethrough cache volume %s without --force.", - display_lvname(lv)); - return 0; - } - log_warn("WARNING: Uncaching of partially missing writethrough cache volume %s might destroy your data.", - display_lvname(lv)); - } + if (!strcmp(dev_policy, "allocate") || + !strcmp(dev_policy, "replace")) + return; - if (!arg_count(cmd, yes_ARG) && - yes_no_prompt("Do you really want to uncache %s with missing LVs? [y/n]: ", - display_lvname(lv)) == 'n') { - log_error("Conversion aborted."); - return 0; - } + /* else if (!strcmp(dev_policy, "anything_else")) -- no replace */ + *do_it = 0; + return; } - if (lvremove_single(cmd, remove_lv, NULL) != ECMD_PROCESSED) - return_0; - - if (remove_lv != lv) - log_print_unless_silent("Logical volume %s is not cached.", display_lvname(lv)); - - return 1; + if (!arg_count(cmd, yes_ARG) && + yes_no_prompt("Attempt to replace failed RAID images " + "(requires full device resync)? [y/n]: ") == 'n') { + *do_it = 0; + } } -static int _lvconvert_snapshot(struct cmd_context *cmd, - struct logical_volume *lv, - const char *origin_name) +int lvconvert_repair_pvs_raid(struct cmd_context *cmd, struct logical_volume *lv, + struct processing_handle *handle, + struct dm_list *use_pvh) { - struct logical_volume *org; - const char *snap_name = display_lvname(lv); - uint32_t chunk_size; - int zero; - - if (!(org = find_lv(lv->vg, origin_name))) { - log_error("Couldn't find origin volume %s in Volume group %s.", - origin_name, lv->vg->name); - return 0; - } - - if (org == lv) { - log_error("Unable to use %s as both snapshot and origin.", snap_name); - return 0; - } - - chunk_size = arg_uint_value(cmd, chunksize_ARG, 8); - if (chunk_size < 8 || chunk_size > 1024 || !is_power_of_2(chunk_size)) { - log_error("Chunk size must be a power of 2 in the range 4K to 512K."); - return 0; - } - log_verbose("Setting chunk size to %s.", display_size(cmd, chunk_size)); - - if (!cow_has_min_chunks(lv->vg, lv->le_count, chunk_size)) - return_0; - - /* - * check_lv_rules() checks cannot be done via command definition - * rules because this LV is not processed by process_each_lv. - */ - if (lv_is_locked(org) || lv_is_pvmove(org)) { - log_error("Unable to use LV %s as snapshot origin: LV is %s.", - display_lvname(lv), lv_is_locked(org) ? "locked" : "pvmove"); - return 0; - } + struct dm_list *failed_pvs; + int do_it; - /* - * check_lv_types() checks cannot be done via command definition - * LV_foo specification because this LV is not processed by process_each_lv. - */ - if (lv_is_cache_type(org) || - lv_is_thin_type(org) || - lv_is_mirrored(org) || - lv_is_cow(org)) { - log_error("Unable to use LV %s as snapshot origin: invald LV type.", - display_lvname(lv)); + if (!lv_is_active_exclusive_locally(lv_lock_holder(lv))) { + log_error("%s must be active %sto perform this operation.", + display_lvname(lv), + vg_is_clustered(lv->vg) ? + "exclusive locally " : ""); return 0; } - log_warn("WARNING: Converting logical volume %s to snapshot exception store.", - snap_name); - log_warn("THIS WILL DESTROY CONTENT OF LOGICAL VOLUME (filesystem etc.)"); - - if (!arg_count(cmd, yes_ARG) && - yes_no_prompt("Do you really want to convert %s? [y/n]: ", - snap_name) == 'n') { - log_error("Conversion aborted."); - return 0; - } + _lvconvert_repair_pvs_raid_ask(cmd, &do_it); - if (!deactivate_lv(cmd, lv)) { - log_error("Couldn't deactivate logical volume %s.", snap_name); - return 0; - } + if (do_it) { + if (!(failed_pvs = failed_pv_list(lv->vg))) + return_0; - if (first_seg(lv)->segtype->flags & SEG_CANNOT_BE_ZEROED) - zero = 0; - else - zero = arg_int_value(cmd, zero_ARG, 1); - - if (!zero || !(lv->status & LVM_WRITE)) - log_warn("WARNING: %s not zeroed.", snap_name); - else { - lv->status |= LV_TEMPORARY; - if (!activate_lv_local(cmd, lv) || - !wipe_lv(lv, (struct wipe_params) { .do_zero = 1 })) { - log_error("Aborting. Failed to wipe snapshot exception store."); - return 0; - } - lv->status &= ~LV_TEMPORARY; - /* Deactivates cleared metadata LV */ - if (!deactivate_lv_local(lv->vg->cmd, lv)) { - log_error("Failed to deactivate zeroed snapshot exception store."); + if (!lv_raid_replace(lv, arg_count(cmd, force_ARG), failed_pvs, use_pvh)) { + log_error("Failed to replace faulty devices in %s.", + display_lvname(lv)); return 0; } - } - - if (!archive(lv->vg)) - return_0; - if (!vg_add_snapshot(org, lv, NULL, org->le_count, chunk_size)) { - log_error("Couldn't create snapshot."); - return 0; + log_print_unless_silent("Faulty devices in %s successfully replaced.", + display_lvname(lv)); + return 1; } - /* store vg on disk(s) */ - if (!lv_update_and_reload(org)) - return_0; - - log_print_unless_silent("Logical volume %s converted to snapshot.", snap_name); - + /* "warn" if policy not set to replace */ + if (arg_is_set(cmd, usepolicies_ARG)) + log_warn("Use 'lvconvert --repair %s' to replace " + "failed device.", display_lvname(lv)); return 1; } -static int _lvconvert_merge_old_snapshot(struct cmd_context *cmd, - struct logical_volume *lv, - struct logical_volume **lv_to_poll) -{ - int merge_on_activate = 0; - struct logical_volume *origin = origin_from_cow(lv); - struct lv_segment *snap_seg = find_snapshot(lv); - struct lvinfo info; - dm_percent_t snap_percent; +/* + * Functions called to perform a specific operation on a specific LV type. + * + * _convert__ + * + * For cases where an operation does not apply to the LV itself, but + * is implicitly redirected to a sub-LV, these functions locate the + * correct sub-LV and call the operation on that sub-LV. If a sub-LV + * of the proper type is not found, these functions report the error. + * + * FIXME: the _lvconvert_foo() functions can be cleaned up since they + * are now only called for valid combinations of LV type and operation. + * After that happens, the code remaining in those functions can be + * moved into the _convert_lvtype_operation() functions below. + */ - if (lv_is_external_origin(origin_from_cow(lv))) { - log_error("Cannot merge snapshot \"%s\" into " - "the read-only external origin \"%s\".", - lv->name, origin_from_cow(lv)->name); - return 0; - } +/* + * Change the number of images in a mirror LV. + * lvconvert --mirrors Number LV + */ +static int _convert_mirror_number(struct cmd_context *cmd, struct logical_volume *lv, + struct lvconvert_params *lp) +{ + return _lvconvert_mirrors(cmd, lv, lp); +} - /* FIXME: test when snapshot is remotely active */ - if (lv_info(cmd, lv, 0, &info, 1, 0) - && info.exists && info.live_table && - (!lv_snapshot_percent(lv, &snap_percent) || - snap_percent == DM_PERCENT_INVALID)) { - log_error("Unable to merge invalidated snapshot LV \"%s\".", - lv->name); - return 0; - } +/* + * Split images from a mirror LV and use them to create a new LV. + * lvconvert --splitmirrors Number LV + * + * Required options: + * --name Name + */ - if (snap_seg->segtype->ops->target_present && - !snap_seg->segtype->ops->target_present(cmd, snap_seg, NULL)) { - log_error("Can't initialize snapshot merge. " - "Missing support in kernel?"); - return 0; - } +static int _convert_mirror_splitmirrors(struct cmd_context *cmd, struct logical_volume *lv, + struct lvconvert_params *lp) +{ + return _lvconvert_mirrors(cmd, lv, lp); +} - if (!archive(lv->vg)) - return_0; - - /* - * Prevent merge with open device(s) as it would likely lead - * to application/filesystem failure. Merge on origin's next - * activation if either the origin or snapshot LV are currently - * open. - * - * FIXME testing open_count is racey; snapshot-merge target's - * constructor and DM should prevent appropriate devices from - * being open. - */ - if (lv_is_active_locally(origin)) { - if (!lv_check_not_in_use(origin, 0)) { - log_print_unless_silent("Can't merge until origin volume is closed."); - merge_on_activate = 1; - } else if (!lv_check_not_in_use(lv, 0)) { - log_print_unless_silent("Can't merge until snapshot is closed."); - merge_on_activate = 1; - } - } else if (vg_is_clustered(origin->vg) && lv_is_active(origin)) { - /* When it's active somewhere else */ - log_print_unless_silent("Can't check whether remotely active snapshot is open."); - merge_on_activate = 1; - } - - init_snapshot_merge(snap_seg, origin); - - if (merge_on_activate) { - /* Store and commit vg but skip starting the merge */ - if (!vg_write(lv->vg) || !vg_commit(lv->vg)) - return_0; - backup(lv->vg); - } else { - /* Perform merge */ - if (!lv_update_and_reload(origin)) - return_0; - - *lv_to_poll = origin; - } - - if (merge_on_activate) - log_print_unless_silent("Merging of snapshot %s will occur on " - "next activation of %s.", - display_lvname(lv), display_lvname(origin)); - else - log_print_unless_silent("Merging of volume %s started.", - display_lvname(lv)); - - return 1; -} - -static int _lvconvert_merge_thin_snapshot(struct cmd_context *cmd, - struct logical_volume *lv) -{ - int origin_is_active = 0, r = 0; - struct lv_segment *snap_seg = first_seg(lv); - struct logical_volume *origin = snap_seg->origin; - - if (!origin) { - log_error("%s is not a mergeable logical volume.", - display_lvname(lv)); - return 0; - } - - /* Check if merge is possible */ - if (lv_is_merging_origin(origin)) { - log_error("Snapshot %s is already merging into the origin.", - display_lvname(find_snapshot(origin)->lv)); - return 0; - } - - if (lv_is_external_origin(origin)) { - if (!(origin = origin_from_cow(lv))) - log_error(INTERNAL_ERROR "%s is missing origin.", - display_lvname(lv)); - else - log_error("%s is read-only external origin %s.", - display_lvname(lv), display_lvname(origin)); - return 0; - } - - if (lv_is_origin(origin)) { - log_error("Merging into the old snapshot origin %s is not supported.", - display_lvname(origin)); - return 0; - } - - if (!archive(lv->vg)) - return_0; - - /* - * Prevent merge with open device(s) as it would likely lead - * to application/filesystem failure. Merge on origin's next - * activation if either the origin or snapshot LV can't be - * deactivated. - */ - if (!deactivate_lv(cmd, lv)) - log_print_unless_silent("Delaying merge since snapshot is open."); - else if ((origin_is_active = lv_is_active(origin)) && - !deactivate_lv(cmd, origin)) - log_print_unless_silent("Delaying merge since origin volume is open."); - else { - /* - * Both thin snapshot and origin are inactive, - * replace the origin LV with its snapshot LV. - */ - if (!thin_merge_finish(cmd, origin, lv)) - goto_out; - - if (origin_is_active && !activate_lv(cmd, lv)) { - log_error("Failed to reactivate origin %s.", - display_lvname(lv)); - goto out; - } - - r = 1; - goto out; - } - - init_snapshot_merge(snap_seg, origin); - - /* Commit vg, merge will start with next activation */ - if (!vg_write(lv->vg) || !vg_commit(lv->vg)) - return_0; - - r = 1; -out: - backup(lv->vg); - - if (r) - log_print_unless_silent("Merging of thin snapshot %s will occur on " - "next activation of %s.", - display_lvname(lv), display_lvname(origin)); - - return r; -} - -static int _lvconvert_thin_pool_repair(struct cmd_context *cmd, - struct logical_volume *pool_lv, - struct dm_list *pvh, int poolmetadataspare) -{ - const char *dmdir = dm_dir(); - const char *thin_dump = - find_config_tree_str_allow_empty(cmd, global_thin_dump_executable_CFG, NULL); - const char *thin_repair = - find_config_tree_str_allow_empty(cmd, global_thin_repair_executable_CFG, NULL); - const struct dm_config_node *cn; - const struct dm_config_value *cv; - int ret = 0, status; - int args = 0; - const char *argv[19]; /* Max supported 10 args */ - char *dm_name, *trans_id_str; - char meta_path[PATH_MAX]; - char pms_path[PATH_MAX]; - uint64_t trans_id; - struct logical_volume *pmslv; - struct logical_volume *mlv = first_seg(pool_lv)->metadata_lv; - struct pipe_data pdata; - FILE *f; - - if (!thin_repair || !thin_repair[0]) { - log_error("Thin repair commnand is not configured. Repair is disabled."); - return 0; /* Checking disabled */ - } - - pmslv = pool_lv->vg->pool_metadata_spare_lv; - - /* Check we have pool metadata spare LV */ - if (!handle_pool_metadata_spare(pool_lv->vg, 0, pvh, 1)) - return_0; - - if (pmslv != pool_lv->vg->pool_metadata_spare_lv) { - if (!vg_write(pool_lv->vg) || !vg_commit(pool_lv->vg)) - return_0; - pmslv = pool_lv->vg->pool_metadata_spare_lv; - } - - if (!(dm_name = dm_build_dm_name(cmd->mem, mlv->vg->name, - mlv->name, NULL)) || - (dm_snprintf(meta_path, sizeof(meta_path), "%s/%s", dmdir, dm_name) < 0)) { - log_error("Failed to build thin metadata path."); - return 0; - } - - if (!(dm_name = dm_build_dm_name(cmd->mem, pmslv->vg->name, - pmslv->name, NULL)) || - (dm_snprintf(pms_path, sizeof(pms_path), "%s/%s", dmdir, dm_name) < 0)) { - log_error("Failed to build pool metadata spare path."); - return 0; - } - - if (!(cn = find_config_tree_array(cmd, global_thin_repair_options_CFG, NULL))) { - log_error(INTERNAL_ERROR "Unable to find configuration for global/thin_repair_options"); - return 0; - } - - for (cv = cn->v; cv && args < 16; cv = cv->next) { - if (cv->type != DM_CFG_STRING) { - log_error("Invalid string in config file: " - "global/thin_repair_options"); - return 0; - } - argv[++args] = cv->v.str; - } - - if (args == 10) { - log_error("Too many options for thin repair command."); - return 0; - } - - argv[0] = thin_repair; - argv[++args] = "-i"; - argv[++args] = meta_path; - argv[++args] = "-o"; - argv[++args] = pms_path; - argv[++args] = NULL; - - if (pool_is_active(pool_lv)) { - log_error("Only inactive pool can be repaired."); - return 0; - } - - if (!activate_lv_local(cmd, pmslv)) { - log_error("Cannot activate pool metadata spare volume %s.", - pmslv->name); - return 0; - } - - if (!activate_lv_local(cmd, mlv)) { - log_error("Cannot activate thin pool metadata volume %s.", - mlv->name); - goto deactivate_pmslv; - } - - if (!(ret = exec_cmd(cmd, (const char * const *)argv, &status, 1))) { - log_error("Repair of thin metadata volume of thin pool %s failed (status:%d). " - "Manual repair required!", - display_lvname(pool_lv), status); - goto deactivate_mlv; - } - - if (thin_dump[0]) { - argv[0] = thin_dump; - argv[1] = pms_path; - argv[2] = NULL; - - if (!(f = pipe_open(cmd, argv, 0, &pdata))) - log_warn("WARNING: Cannot read output from %s %s.", thin_dump, pms_path); - else { - /* - * Scan only the 1st. line for transation id. - * Watch out, if the thin_dump format changes - */ - if (fgets(meta_path, sizeof(meta_path), f) && - (trans_id_str = strstr(meta_path, "transaction=\"")) && - (sscanf(trans_id_str + 13, FMTu64, &trans_id) == 1) && - (trans_id != first_seg(pool_lv)->transaction_id) && - ((trans_id - 1) != first_seg(pool_lv)->transaction_id)) - log_error("Transaction id " FMTu64 " from pool \"%s/%s\" " - "does not match repaired transaction id " - FMTu64 " from %s.", - first_seg(pool_lv)->transaction_id, - pool_lv->vg->name, pool_lv->name, trans_id, - pms_path); - - (void) pipe_close(&pdata); /* killing pipe */ - } - } - -deactivate_mlv: - if (!deactivate_lv(cmd, mlv)) { - log_error("Cannot deactivate thin pool metadata volume %s.", - mlv->name); - return 0; - } - -deactivate_pmslv: - if (!deactivate_lv(cmd, pmslv)) { - log_error("Cannot deactivate thin pool metadata volume %s.", - mlv->name); - return 0; - } - - if (!ret) - return 0; - - if (pmslv == pool_lv->vg->pool_metadata_spare_lv) { - pool_lv->vg->pool_metadata_spare_lv = NULL; - pmslv->status &= ~POOL_METADATA_SPARE; - lv_set_visible(pmslv); - } - - /* Try to allocate new pool metadata spare LV */ - if (!handle_pool_metadata_spare(pool_lv->vg, 0, pvh, poolmetadataspare)) - stack; - - if (dm_snprintf(meta_path, sizeof(meta_path), "%s_meta%%d", pool_lv->name) < 0) { - log_error("Can't prepare new metadata name for %s.", pool_lv->name); - return 0; - } - - if (!generate_lv_name(pool_lv->vg, meta_path, pms_path, sizeof(pms_path))) { - log_error("Can't generate new name for %s.", meta_path); - return 0; - } - - if (!detach_pool_metadata_lv(first_seg(pool_lv), &mlv)) - return_0; - - /* Swap _pmspare and _tmeta name */ - if (!swap_lv_identifiers(cmd, mlv, pmslv)) - return_0; - - if (!attach_pool_metadata_lv(first_seg(pool_lv), pmslv)) - return_0; - - /* Used _tmeta (now _pmspare) becomes _meta%d */ - if (!lv_rename_update(cmd, mlv, pms_path, 0)) - return_0; - - if (!vg_write(pool_lv->vg) || !vg_commit(pool_lv->vg)) - return_0; - - log_warn("WARNING: If everything works, remove %s volume.", - display_lvname(mlv)); - - log_warn("WARNING: Use pvmove command to move %s on the best fitting PV.", - display_lvname(first_seg(pool_lv)->metadata_lv)); - - return 1; -} - -static int _lvconvert_to_thin_with_external(struct cmd_context *cmd, - struct logical_volume *lv, - struct logical_volume *thinpool_lv) -{ - struct volume_group *vg = lv->vg; - struct logical_volume *thin_lv; - const char *origin_name; - - struct lvcreate_params lvc = { - .activate = CHANGE_AEY, - .alloc = ALLOC_INHERIT, - .major = -1, - .minor = -1, - .suppress_zero_warn = 1, /* Suppress warning for this thin */ - .permission = LVM_READ, - .pool_name = thinpool_lv->name, - .pvh = &vg->pvs, - .read_ahead = DM_READ_AHEAD_AUTO, - .stripes = 1, - .virtual_extents = lv->le_count, - }; - - if (lv == thinpool_lv) { - log_error("Can't use same LV %s for thin pool and thin volume.", - display_lvname(thinpool_lv)); - return 0; - } - - if ((origin_name = arg_str_value(cmd, originname_ARG, NULL))) - if (!validate_restricted_lvname_param(cmd, &vg->name, &origin_name)) - return_0; - - /* - * If NULL, an auto-generated 'lvol' name is used. - * If set, the lv create code checks the name isn't used. - */ - lvc.lv_name = origin_name; - - if (is_lockd_type(vg->lock_type)) { - /* - * FIXME: external origins don't work in lockd VGs. - * Prior to the lvconvert, there's a lock associated with - * the uuid of the external origin LV. After the convert, - * that uuid belongs to the new thin LV, and a new LV with - * a new uuid exists as the non-thin, readonly external LV. - * We'd need to remove the lock for the previous uuid - * (the new thin LV will have no lock), and create a new - * lock for the new LV uuid used by the external LV. - */ - log_error("Can't use lock_type %s LV as external origin.", - vg->lock_type); - return 0; - } - - dm_list_init(&lvc.tags); - - if (!pool_supports_external_origin(first_seg(thinpool_lv), lv)) - return_0; - - if (!(lvc.segtype = get_segtype_from_string(cmd, SEG_TYPE_NAME_THIN))) - return_0; - - if (!archive(vg)) - return_0; - - /* - * New thin LV needs to be created (all messages sent to pool) In this - * case thin volume is created READ-ONLY and also warn about not - * zeroing is suppressed. - * - * The new thin LV is created with the origin_name, or an autogenerated - * 'lvol' name. Then the names and ids are swapped between the thin LV - * and the original/external LV. So, the thin LV gets the name and id - * of the original LV arg, and the original LV arg gets the origin_name - * or the autogenerated name. - */ - - if (!(thin_lv = lv_create_single(vg, &lvc))) - return_0; - - if (!deactivate_lv(cmd, thin_lv)) { - log_error("Aborting. Unable to deactivate new LV. " - "Manual intervention required."); - return 0; - } - - /* - * Crashing till this point will leave plain thin volume - * which could be easily removed by the user after i.e. power-off - */ - - if (!swap_lv_identifiers(cmd, thin_lv, lv)) { - stack; - goto revert_new_lv; - } - - /* Preserve read-write status of original LV here */ - thin_lv->status |= (lv->status & LVM_WRITE); - - if (!attach_thin_external_origin(first_seg(thin_lv), lv)) { - stack; - goto revert_new_lv; - } - - if (!lv_update_and_reload(thin_lv)) { - stack; - goto deactivate_and_revert_new_lv; - } - - log_print_unless_silent("Converted %s to thin volume with external origin %s.", - display_lvname(thin_lv), display_lvname(lv)); - - return 1; - -deactivate_and_revert_new_lv: - if (!swap_lv_identifiers(cmd, thin_lv, lv)) - stack; - - if (!deactivate_lv(cmd, thin_lv)) { - log_error("Unable to deactivate failed new LV. " - "Manual intervention required."); - return 0; - } - - if (!detach_thin_external_origin(first_seg(thin_lv))) - return_0; - -revert_new_lv: - /* FIXME Better to revert to backup of metadata? */ - if (!lv_remove(thin_lv) || !vg_write(vg) || !vg_commit(vg)) - log_error("Manual intervention may be required to remove " - "abandoned LV(s) before retrying."); - else - backup(vg); - - return 0; -} - -static int _lvconvert_swap_pool_metadata(struct cmd_context *cmd, - struct logical_volume *lv, - struct logical_volume *metadata_lv) -{ - struct volume_group *vg = lv->vg; - struct logical_volume *prev_metadata_lv; - struct lv_segment *seg; - struct lv_types *lvtype; - char meta_name[NAME_LEN]; - const char *swap_name; - uint32_t chunk_size; - int is_thinpool; - int is_cachepool; - int lvt_enum; - - is_thinpool = lv_is_thin_pool(lv); - is_cachepool = lv_is_cache_pool(lv); - lvt_enum = get_lvt_enum(metadata_lv); - lvtype = get_lv_type(lvt_enum); - - if (lvt_enum != striped_LVT && lvt_enum != linear_LVT && lvt_enum != raid_LVT) { - log_error("LV %s with type %s cannot be used as a metadata LV.", - display_lvname(metadata_lv), lvtype ? lvtype->name : "unknown"); - return 0; - } - - if (!lv_is_visible(metadata_lv)) { - log_error("Can't convert internal LV %s.", - display_lvname(metadata_lv)); - return 0; - } - - if (lv_is_locked(metadata_lv)) { - log_error("Can't convert locked LV %s.", - display_lvname(metadata_lv)); - return 0; - } - - if (lv_is_origin(metadata_lv) || - lv_is_merging_origin(metadata_lv) || - lv_is_external_origin(metadata_lv) || - lv_is_virtual(metadata_lv)) { - log_error("Pool metadata LV %s is of an unsupported type.", - display_lvname(metadata_lv)); - return 0; - } - - /* FIXME cache pool */ - if (is_thinpool && pool_is_active(lv)) { - /* If any volume referencing pool active - abort here */ - log_error("Cannot convert pool %s with active volumes.", - display_lvname(lv)); - return 0; - } - - if ((dm_snprintf(meta_name, sizeof(meta_name), "%s%s", lv->name, is_cachepool ? "_cmeta" : "_tmeta") < 0)) { - log_error("Failed to create internal lv names, pool name is too long."); - return 0; - } - - seg = first_seg(lv); - - /* Normally do NOT change chunk size when swapping */ - - if (arg_is_set(cmd, chunksize_ARG)) { - chunk_size = arg_uint_value(cmd, chunksize_ARG, 0); - - if ((chunk_size != seg->chunk_size) && !dm_list_empty(&lv->segs_using_this_lv)) { - if (arg_count(cmd, force_ARG) == PROMPT) { - log_error("Chunk size can be only changed with --force. Conversion aborted."); - return 0; - } - - if (!validate_pool_chunk_size(cmd, seg->segtype, chunk_size)) - return_0; - - log_warn("WARNING: Changing chunk size %s to %s for %s pool volume.", - display_size(cmd, seg->chunk_size), - display_size(cmd, chunk_size), - display_lvname(lv)); - - /* Ok, user has likely some serious reason for this */ - if (!arg_count(cmd, yes_ARG) && - yes_no_prompt("Do you really want to change chunk size for %s pool volume? [y/n]: ", - display_lvname(lv)) == 'n') { - log_error("Conversion aborted."); - return 0; - } - } - - seg->chunk_size = chunk_size; - } - - if (!arg_count(cmd, yes_ARG) && - yes_no_prompt("Do you want to swap metadata of %s pool with metadata volume %s? [y/n]: ", - display_lvname(lv), - display_lvname(metadata_lv)) == 'n') { - log_error("Conversion aborted."); - return 0; - } - - if (!deactivate_lv(cmd, metadata_lv)) { - log_error("Aborting. Failed to deactivate %s.", - display_lvname(metadata_lv)); - return 0; - } - - if (!archive(vg)) - return_0; - - /* Swap names between old and new metadata LV */ - - if (!detach_pool_metadata_lv(seg, &prev_metadata_lv)) - return_0; - - swap_name = metadata_lv->name; - - if (!lv_rename_update(cmd, metadata_lv, "pvmove_tmeta", 0)) - return_0; - - /* Give the previous metadata LV the name of the LV replacing it. */ - - if (!lv_rename_update(cmd, prev_metadata_lv, swap_name, 0)) - return_0; - - /* Rename deactivated metadata LV to have _tmeta suffix */ - - if (!lv_rename_update(cmd, metadata_lv, meta_name, 0)) - return_0; - - if (!attach_pool_metadata_lv(seg, metadata_lv)) - return_0; - - if (!vg_write(vg) || !vg_commit(vg)) - return_0; - - backup(vg); - return 1; -} - -/* - * Create a new pool LV, using the lv arg as the data sub LV. - * The metadata sub LV is either a new LV created here, or an - * existing LV specified by --poolmetadata. - */ - -static int _lvconvert_to_pool(struct cmd_context *cmd, - struct logical_volume *lv, - int to_thinpool, - int to_cachepool, - struct dm_list *use_pvh) -{ - struct volume_group *vg = lv->vg; - struct logical_volume *metadata_lv = NULL; /* existing or created */ - struct logical_volume *data_lv; /* lv arg renamed */ - struct logical_volume *pool_lv; /* new lv created here */ - const char *pool_metadata_name; /* user-specified lv name */ - const char *pool_name; /* name of original lv arg */ - char meta_name[NAME_LEN]; /* generated sub lv name */ - char data_name[NAME_LEN]; /* generated sub lv name */ - struct segment_type *pool_segtype; /* thinpool or cachepool */ - struct lv_segment *seg; - unsigned int target_attr = ~0; - unsigned int passed_args = 0; - unsigned int activate_pool; - unsigned int zero_metadata; - uint64_t meta_size; - uint32_t meta_extents; - uint32_t chunk_size; - int chunk_calc; - int r = 0; - - /* for handling lvmlockd cases */ - char *lockd_data_args = NULL; - char *lockd_meta_args = NULL; - char *lockd_data_name = NULL; - char *lockd_meta_name = NULL; - struct id lockd_data_id; - struct id lockd_meta_id; - - - if (lv_is_thin_pool(lv) || lv_is_cache_pool(lv)) { - log_error(INTERNAL_ERROR "LV %s is already a pool.", display_lvname(lv)); - return 0; - } - - pool_segtype = to_cachepool ? get_segtype_from_string(cmd, SEG_TYPE_NAME_CACHE_POOL) : - get_segtype_from_string(cmd, SEG_TYPE_NAME_THIN_POOL); - - if (!pool_segtype->ops->target_present(cmd, NULL, &target_attr)) { - log_error("%s: Required device-mapper target(s) not detected in your kernel.", pool_segtype->name); - return 0; - } - - /* Allow to have only thinpool active and restore it's active state. */ - activate_pool = to_thinpool && lv_is_active(lv); - - /* Wipe metadata_lv by default, but allow skipping this for cache pools. */ - zero_metadata = to_cachepool ? arg_int_value(cmd, zero_ARG, 1) : 1; - - /* An existing LV needs to have its lock freed once it becomes a data LV. */ - if (is_lockd_type(vg->lock_type) && lv->lock_args) { - lockd_data_args = dm_pool_strdup(cmd->mem, lv->lock_args); - lockd_data_name = dm_pool_strdup(cmd->mem, lv->name); - memcpy(&lockd_data_id, &lv->lvid.id[1], sizeof(struct id)); - } - - /* - * If an existing LV is to be used as the metadata LV, - * verify that it's in a usable state. These checks are - * not done by command def rules because this LV is not - * processed by process_each_lv. - */ - - if ((pool_metadata_name = arg_str_value(cmd, poolmetadata_ARG, NULL))) { - if (!(metadata_lv = find_lv(vg, pool_metadata_name))) { - log_error("Unknown pool metadata LV %s.", pool_metadata_name); - return 0; - } - - /* An existing LV needs to have its lock freed once it becomes a meta LV. */ - if (is_lockd_type(vg->lock_type) && metadata_lv->lock_args) { - lockd_meta_args = dm_pool_strdup(cmd->mem, metadata_lv->lock_args); - lockd_meta_name = dm_pool_strdup(cmd->mem, metadata_lv->name); - memcpy(&lockd_meta_id, &metadata_lv->lvid.id[1], sizeof(struct id)); - } - - if (metadata_lv == lv) { - log_error("Can't use same LV for pool data and metadata LV %s.", - display_lvname(metadata_lv)); - return 0; - } - - if (!lv_is_visible(metadata_lv)) { - log_error("Can't convert internal LV %s.", - display_lvname(metadata_lv)); - return 0; - } - - if (lv_is_locked(metadata_lv)) { - log_error("Can't convert locked LV %s.", - display_lvname(metadata_lv)); - return 0; - } - - if (lv_is_mirror(metadata_lv)) { - log_error("Mirror logical volumes cannot be used for pool metadata."); - log_print_unless_silent("Try \"%s\" segment type instead.", SEG_TYPE_NAME_RAID1); - return 0; - } - - /* FIXME Tidy up all these type restrictions. */ - if (lv_is_cache_type(metadata_lv) || - lv_is_thin_type(metadata_lv) || - lv_is_cow(metadata_lv) || lv_is_merging_cow(metadata_lv) || - lv_is_origin(metadata_lv) || lv_is_merging_origin(metadata_lv) || - lv_is_external_origin(metadata_lv) || - lv_is_virtual(metadata_lv)) { - log_error("Pool metadata LV %s is of an unsupported type.", - display_lvname(metadata_lv)); - return 0; - } - } - - /* - * Determine the size of the metadata LV and the chunk size. When an - * existing LV is to be used for metadata, this introduces some - * constraints/defaults. When chunk_size=0 and/or meta_extents=0 are - * passed to the "update params" function, defaults are calculated and - * returned. - */ - - if (arg_is_set(cmd, chunksize_ARG)) { - passed_args |= PASS_ARG_CHUNK_SIZE; - chunk_size = arg_uint_value(cmd, chunksize_ARG, 0); - if (!validate_pool_chunk_size(cmd, pool_segtype, chunk_size)) - return_0; - } else { - /* A default will be chosen by the "update" function. */ - chunk_size = 0; - } - - if (arg_is_set(cmd, poolmetadatasize_ARG)) { - meta_size = arg_uint64_value(cmd, poolmetadatasize_ARG, UINT64_C(0)); - meta_extents = extents_from_size(cmd, meta_size, vg->extent_size); - passed_args |= PASS_ARG_POOL_METADATA_SIZE; - } else if (metadata_lv) { - meta_extents = metadata_lv->le_count; - passed_args |= PASS_ARG_POOL_METADATA_SIZE; - } else { - /* A default will be chosen by the "update" function. */ - meta_extents = 0; - } - - /* Tell the "update" function to ignore these, they are handled below. */ - passed_args |= PASS_ARG_DISCARDS | PASS_ARG_ZERO; - - /* - * Validate and/or choose defaults for meta_extents and chunk_size, - * this involves some complicated calculations. - */ - - if (to_cachepool) { - if (!update_cache_pool_params(pool_segtype, vg, target_attr, - passed_args, lv->le_count, - &meta_extents, - &chunk_calc, - &chunk_size)) - return_0; - } else { - if (!update_thin_pool_params(pool_segtype, vg, target_attr, - passed_args, lv->le_count, - &meta_extents, - &chunk_calc, - &chunk_size, - NULL, NULL)) - return_0; - } - - if ((uint64_t)chunk_size > ((uint64_t)lv->le_count * vg->extent_size)) { - log_error("Pool data LV %s is too small (%s) for specified chunk size (%s).", - display_lvname(lv), - display_size(cmd, (uint64_t)lv->le_count * vg->extent_size), - display_size(cmd, chunk_size)); - return 0; - } - - if (metadata_lv && (meta_extents > metadata_lv->le_count)) { - log_error("Pool metadata LV %s is too small (%u extents) for required metadata (%u extents).", - display_lvname(metadata_lv), metadata_lv->le_count, meta_extents); - return 0; - } - - log_verbose("Pool metadata extents %u chunk_size %u", meta_extents, chunk_size); - - - /* - * Verify that user wants to use these LVs. - */ - - log_warn("WARNING: Converting logical volume %s%s%s to %s pool's data%s %s metadata wiping.", - display_lvname(lv), - metadata_lv ? " and " : "", - metadata_lv ? display_lvname(metadata_lv) : "", - to_cachepool ? "cache" : "thin", - metadata_lv ? " and metadata volumes" : " volume", - zero_metadata ? "with" : "WITHOUT"); - - if (zero_metadata) - log_warn("THIS WILL DESTROY CONTENT OF LOGICAL VOLUME (filesystem etc.)"); - else if (to_cachepool) - log_warn("WARNING: Using mismatched cache pool metadata MAY DESTROY YOUR DATA!"); - - if (!arg_count(cmd, yes_ARG) && - yes_no_prompt("Do you really want to convert %s%s%s? [y/n]: ", - display_lvname(lv), - metadata_lv ? " and " : "", - metadata_lv ? display_lvname(metadata_lv) : "") == 'n') { - log_error("Conversion aborted."); - return 0; - } - - /* - * The internal LV names for pool data/meta LVs. - */ - - if ((dm_snprintf(meta_name, sizeof(meta_name), "%s%s", lv->name, to_cachepool ? "_cmeta" : "_tmeta") < 0) || - (dm_snprintf(data_name, sizeof(data_name), "%s%s", lv->name, to_cachepool ? "_cdata" : "_tdata") < 0)) { - log_error("Failed to create internal lv names, pool name is too long."); - return 0; - } - - /* - * If a new metadata LV needs to be created, collect the settings for - * the new LV and create it. - * - * If an existing LV is used for metadata, deactivate/activate/wipe it. - */ - - if (!metadata_lv) { - uint32_t meta_stripes; - uint32_t meta_stripe_size; - uint32_t meta_readahead; - alloc_policy_t meta_alloc; - unsigned meta_stripes_supplied; - unsigned meta_stripe_size_supplied; - - if (!get_stripe_params(cmd, get_segtype_from_string(cmd, SEG_TYPE_NAME_STRIPED), - &meta_stripes, - &meta_stripe_size, - &meta_stripes_supplied, - &meta_stripe_size_supplied)) - return_0; - - meta_readahead = arg_uint_value(cmd, readahead_ARG, cmd->default_settings.read_ahead); - meta_alloc = (alloc_policy_t) arg_uint_value(cmd, alloc_ARG, ALLOC_INHERIT); - - if (!archive(vg)) - return_0; - - if (!(metadata_lv = alloc_pool_metadata(lv, - meta_name, - meta_readahead, - meta_stripes, - meta_stripe_size, - meta_extents, - meta_alloc, - use_pvh))) - return_0; - } else { - if (!deactivate_lv(cmd, metadata_lv)) { - log_error("Aborting. Failed to deactivate %s.", - display_lvname(metadata_lv)); - return 0; - } - - if (!archive(vg)) - return_0; - - if (zero_metadata) { - metadata_lv->status |= LV_TEMPORARY; - if (!activate_lv_local(cmd, metadata_lv)) { - log_error("Aborting. Failed to activate metadata lv."); - return 0; - } - - if (!wipe_lv(metadata_lv, (struct wipe_params) { .do_zero = 1 })) { - log_error("Aborting. Failed to wipe metadata lv."); - return 0; - } - } - } - - /* - * Deactivate the data LV and metadata LV. - * We are changing target type, so deactivate first. - */ - - if (!deactivate_lv(cmd, metadata_lv)) { - log_error("Aborting. Failed to deactivate metadata lv. " - "Manual intervention required."); - return 0; - } - - if (!deactivate_lv(cmd, lv)) { - log_error("Aborting. Failed to deactivate logical volume %s.", - display_lvname(lv)); - return 0; - } - - /* - * When the LV referenced by the original function arg "lv" - * is renamed, it is then referenced as "data_lv". - * - * pool_name pool name taken from lv arg - * data_name sub lv name, generated - * meta_name sub lv name, generated - * - * pool_lv new lv for pool object, created here - * data_lv sub lv, was lv arg, now renamed - * metadata_lv sub lv, existing or created here - */ - - data_lv = lv; - pool_name = lv->name; /* Use original LV name for pool name */ - - /* - * Rename the original LV arg to the internal data LV naming scheme. - * - * Since we wish to have underlaying devs to match _[ct]data - * rename data LV to match pool LV subtree first, - * also checks for visible LV. - * - * FIXME: any more types prohibited here? - */ - - if (!lv_rename_update(cmd, data_lv, data_name, 0)) - return_0; - - /* - * Create LV structures for the new pool LV object, - * and connect it to the data/meta LVs. - */ - - if (!(pool_lv = lv_create_empty(pool_name, NULL, - (to_cachepool ? CACHE_POOL : THIN_POOL) | VISIBLE_LV | LVM_READ | LVM_WRITE, - ALLOC_INHERIT, vg))) { - log_error("Creation of pool LV failed."); - return 0; - } - - /* Allocate a new pool segment */ - if (!(seg = alloc_lv_segment(pool_segtype, pool_lv, 0, data_lv->le_count, - pool_lv->status, 0, NULL, 1, - data_lv->le_count, 0, 0, 0, NULL))) - return_0; - - /* Add the new segment to the layer LV */ - dm_list_add(&pool_lv->segments, &seg->list); - pool_lv->le_count = data_lv->le_count; - pool_lv->size = data_lv->size; - - if (!attach_pool_data_lv(seg, data_lv)) - return_0; - - /* - * Create a new lock for a thin pool LV. A cache pool LV has no lock. - * Locks are removed from existing LVs that are being converted to - * data and meta LVs (they are unlocked and deleted below.) - */ - if (is_lockd_type(vg->lock_type)) { - if (to_cachepool) { - data_lv->lock_args = NULL; - metadata_lv->lock_args = NULL; - } else { - data_lv->lock_args = NULL; - metadata_lv->lock_args = NULL; - - if (!strcmp(vg->lock_type, "sanlock")) - pool_lv->lock_args = "pending"; - else if (!strcmp(vg->lock_type, "dlm")) - pool_lv->lock_args = "dlm"; - /* The lock_args will be set in vg_write(). */ - } - } - - /* - * Apply settings to the new pool seg, from command line, from - * defaults, sometimes adjusted. - */ - - seg->transaction_id = 0; - seg->chunk_size = chunk_size; - - if (to_cachepool) { - cache_mode_t cache_mode = 0; - const char *policy_name = NULL; - struct dm_config_tree *policy_settings = NULL; - - if (!get_cache_params(cmd, &cache_mode, &policy_name, &policy_settings)) - return_0; - - if (cache_mode && - !cache_set_cache_mode(seg, cache_mode)) - return_0; - - if ((policy_name || policy_settings) && - !cache_set_policy(seg, policy_name, policy_settings)) - return_0; - - if (policy_settings) - dm_config_destroy(policy_settings); - } else { - const char *discards_name; - - if (arg_is_set(cmd, zero_ARG)) - seg->zero_new_blocks = arg_int_value(cmd, zero_ARG, 0); - else - seg->zero_new_blocks = find_config_tree_bool(cmd, allocation_thin_pool_zero_CFG, vg->profile); - - if (arg_is_set(cmd, discards_ARG)) - seg->discards = (thin_discards_t) arg_uint_value(cmd, discards_ARG, THIN_DISCARDS_PASSDOWN); - else { - if (!(discards_name = find_config_tree_str(cmd, allocation_thin_pool_discards_CFG, vg->profile))) - return_0; - if (!set_pool_discards(&seg->discards, discards_name)) - return_0; - } - } - - /* - * Rename deactivated metadata LV to have _tmeta suffix. - * Implicit checks if metadata_lv is visible. - */ - if (pool_metadata_name && - !lv_rename_update(cmd, metadata_lv, meta_name, 0)) - return_0; - - if (!attach_pool_metadata_lv(seg, metadata_lv)) - return_0; - - if (!handle_pool_metadata_spare(vg, - metadata_lv->le_count, - use_pvh, - arg_int_value(cmd, poolmetadataspare_ARG, DEFAULT_POOL_METADATA_SPARE))) - return_0; - - if (!vg_write(vg) || !vg_commit(vg)) - return_0; - - if (seg->zero_new_blocks && - seg->chunk_size >= DEFAULT_THIN_POOL_CHUNK_SIZE_PERFORMANCE * 2) - log_warn("WARNING: Pool zeroing and large %s chunk size slows down provisioning.", - display_size(cmd, seg->chunk_size)); - - if (activate_pool && !lockd_lv(cmd, pool_lv, "ex", LDLV_PERSISTENT)) { - log_error("Failed to lock pool LV %s.", display_lvname(pool_lv)); - goto out; - } - - if (activate_pool && - !activate_lv_excl(cmd, pool_lv)) { - log_error("Failed to activate pool logical volume %s.", - display_lvname(pool_lv)); - /* Deactivate subvolumes */ - if (!deactivate_lv(cmd, seg_lv(seg, 0))) - log_error("Failed to deactivate pool data logical volume %s.", - display_lvname(seg_lv(seg, 0))); - if (!deactivate_lv(cmd, seg->metadata_lv)) - log_error("Failed to deactivate pool metadata logical volume %s.", - display_lvname(seg->metadata_lv)); - goto out; - } - - r = 1; - -out: - backup(vg); - - if (r) - log_print_unless_silent("Converted %s to %s pool.", - display_lvname(lv), - to_cachepool ? "cache" : "thin"); - - /* - * Unlock and free the locks from existing LVs that became pool data - * and meta LVs. - */ - if (lockd_data_name) { - if (!lockd_lv_name(cmd, vg, lockd_data_name, &lockd_data_id, lockd_data_args, "un", LDLV_PERSISTENT)) - log_error("Failed to unlock pool data LV %s/%s", vg->name, lockd_data_name); - lockd_free_lv(cmd, vg, lockd_data_name, &lockd_data_id, lockd_data_args); - } - - if (lockd_meta_name) { - if (!lockd_lv_name(cmd, vg, lockd_meta_name, &lockd_meta_id, lockd_meta_args, "un", LDLV_PERSISTENT)) - log_error("Failed to unlock pool metadata LV %s/%s", vg->name, lockd_meta_name); - lockd_free_lv(cmd, vg, lockd_meta_name, &lockd_meta_id, lockd_meta_args); - } - - return r; -#if 0 -revert_new_lv: - /* TBD */ - if (!pool_metadata_lv_name) { - if (!deactivate_lv(cmd, metadata_lv)) { - log_error("Failed to deactivate metadata lv."); - return 0; - } - if (!lv_remove(metadata_lv) || !vg_write(vg) || !vg_commit(vg)) - log_error("Manual intervention may be required to remove " - "abandoned LV(s) before retrying."); - else - backup(vg); - } - - return 0; -#endif -} - -static int _lvconvert_to_cache_vol(struct cmd_context *cmd, - struct logical_volume *lv, - struct logical_volume *cachepool_lv) -{ - struct logical_volume *cache_lv; - cache_mode_t cache_mode = 0; - const char *policy_name = NULL; - struct dm_config_tree *policy_settings = NULL; - - if (!validate_lv_cache_create_pool(cachepool_lv)) - return_0; - - if (!get_cache_params(cmd, &cache_mode, &policy_name, &policy_settings)) - return_0; - - if (!archive(lv->vg)) - return_0; - - if (!(cache_lv = lv_cache_create(cachepool_lv, lv))) - return_0; - - if (!cache_set_cache_mode(first_seg(cache_lv), cache_mode)) - return_0; - - if (!cache_set_policy(first_seg(cache_lv), policy_name, policy_settings)) - return_0; - - if (policy_settings) - dm_config_destroy(policy_settings); - - cache_check_for_warns(first_seg(cache_lv)); - - if (!lv_update_and_reload(cache_lv)) - return_0; - - log_print_unless_silent("Logical volume %s is now cached.", - display_lvname(cache_lv)); - - return 1; -} - -/* - * Functions called to perform a specific operation on a specific LV type. - * - * _convert__ - * - * For cases where an operation does not apply to the LV itself, but - * is implicitly redirected to a sub-LV, these functions locate the - * correct sub-LV and call the operation on that sub-LV. If a sub-LV - * of the proper type is not found, these functions report the error. - * - * FIXME: the _lvconvert_foo() functions can be cleaned up since they - * are now only called for valid combinations of LV type and operation. - * After that happens, the code remaining in those functions can be - * moved into the _convert_lvtype_operation() functions below. - */ - -/* - * Change the number of images in a mirror LV. - * lvconvert --mirrors Number LV - */ -static int _convert_mirror_number(struct cmd_context *cmd, struct logical_volume *lv, - struct lvconvert_params *lp) -{ - return _lvconvert_mirrors(cmd, lv, lp); -} - -/* - * Split images from a mirror LV and use them to create a new LV. - * lvconvert --splitmirrors Number LV - * - * Required options: - * --name Name - */ - -static int _convert_mirror_splitmirrors(struct cmd_context *cmd, struct logical_volume *lv, - struct lvconvert_params *lp) -{ - return _lvconvert_mirrors(cmd, lv, lp); -} - -/* - * Change the type of log used by a mirror LV. - * lvconvert --mirrorlog Type LV - */ -static int _convert_mirror_log(struct cmd_context *cmd, struct logical_volume *lv, - struct lvconvert_params *lp) -{ - return _lvconvert_mirrors(cmd, lv, lp); -} - -/* - * Convert mirror LV to linear LV. - * lvconvert --type linear LV - * - * Alternate syntax: - * lvconvert --mirrors 0 LV - */ -static int _convert_mirror_linear(struct cmd_context *cmd, struct logical_volume *lv, - struct lvconvert_params *lp) -{ - return _lvconvert_mirrors(cmd, lv, lp); -} - -/* - * Convert mirror LV to raid1 LV. - * lvconvert --type raid1 LV - */ -static int _convert_mirror_raid(struct cmd_context *cmd, struct logical_volume *lv, - struct lvconvert_params *lp) -{ - return _lvconvert_raid(lv, lp); -} - -/* - * Change the number of images in a raid1 LV. - * lvconvert --mirrors Number LV - */ -static int _convert_raid_number(struct cmd_context *cmd, struct logical_volume *lv, - struct lvconvert_params *lp) -{ - return _lvconvert_raid(lv, lp); -} - -/* - * Split images from a raid1 LV and use them to create a new LV. - * lvconvert --splitmirrors Number LV - * - * Required options: - * --trackchanges | --name Name - */ -static int _convert_raid_splitmirrors(struct cmd_context *cmd, struct logical_volume *lv, - struct lvconvert_params *lp) -{ - /* FIXME: split the splitmirrors section out of _lvconvert_raid and call it here. */ - return _lvconvert_raid(lv, lp); -} - -/* - * Convert a raid* LV to use a different raid level. - * lvconvert --type raid* LV - */ -static int _convert_raid_raid(struct cmd_context *cmd, struct logical_volume *lv, - struct lvconvert_params *lp) -{ - return _lvconvert_raid(lv, lp); -} - -/* - * Convert a raid* LV to a mirror LV. - * lvconvert --type mirror LV - */ -static int _convert_raid_mirror(struct cmd_context *cmd, struct logical_volume *lv, - struct lvconvert_params *lp) -{ - return _lvconvert_raid(lv, lp); -} - -/* - * Convert a raid* LV to a striped LV. - * lvconvert --type striped LV - */ -static int _convert_raid_striped(struct cmd_context *cmd, struct logical_volume *lv, - struct lvconvert_params *lp) -{ - return _lvconvert_raid(lv, lp); -} - -/* - * Convert a raid* LV to a linear LV. - * lvconvert --type linear LV - */ -static int _convert_raid_linear(struct cmd_context *cmd, struct logical_volume *lv, - struct lvconvert_params *lp) -{ - return _lvconvert_raid(lv, lp); -} - -/* - * Convert a striped/linear LV to a mirror LV. - * lvconvert --type mirror LV - * - * Required options: - * --mirrors Number - * - * Alternate syntax: - * This is equivalent to above when global/mirror_segtype_default="mirror". - * lvconvert --mirrors Number LV - */ -static int _convert_striped_mirror(struct cmd_context *cmd, struct logical_volume *lv, - struct lvconvert_params *lp) -{ - return _lvconvert_mirrors(cmd, lv, lp); -} - -/* - * Convert a striped/linear LV to a raid* LV. - * lvconvert --type raid* LV - * - * Required options: - * --mirrors Number - * - * Alternate syntax: - * This is equivalent to above when global/mirror_segtype_default="raid1". - * lvconvert --mirrors Number LV - */ -static int _convert_striped_raid(struct cmd_context *cmd, struct logical_volume *lv, - struct lvconvert_params *lp) -{ - return _lvconvert_raid(lv, lp); -} - -/* - * Functions called to perform all valid operations on a given LV type. - * - * _convert_ - */ - -static int _convert_mirror(struct cmd_context *cmd, struct logical_volume *lv, - struct lvconvert_params *lp) -{ - if (arg_is_set(cmd, mirrors_ARG)) - return _convert_mirror_number(cmd, lv, lp); - - if (arg_is_set(cmd, splitmirrors_ARG)) - return _convert_mirror_splitmirrors(cmd, lv, lp); - - if (arg_is_set(cmd, mirrorlog_ARG) || arg_is_set(cmd, corelog_ARG)) - return _convert_mirror_log(cmd, lv, lp); - - if (_linear_type_requested(lp->type_str)) - return _convert_mirror_linear(cmd, lv, lp); - - if (segtype_is_raid(lp->segtype)) - return _convert_mirror_raid(cmd, lv, lp); - - log_error("Operation not permitted on mirror LV %s.", display_lvname(lv)); - log_error("Operations permitted on a mirror LV are:\n" - " --mirrors\n" - " --splitmirrors\n" - " --mirrorlog\n" - " --type linear\n" - " --type raid*\n"); - - return 0; -} - -static int _convert_raid(struct cmd_context *cmd, struct logical_volume *lv, - struct lvconvert_params *lp) -{ - /* Permitted convert options on visible or hidden RaidLVs */ - const char *permitted_options = lv_is_visible(lv) ? - " --mirrors\n" - " --splitmirrors\n" - " --type raid*\n" - " --type mirror\n" - " --type striped\n" - " --type linear\n" - : - " --mirrors\n" - " --splitmirrors\n" - " --replace\n" - " --type raid*\n" - " --type mirror\n" - " --type striped\n" - " --type linear\n"; - - /* Applicable to any hidden _or_ visible LVs. */ - if (arg_is_set(cmd, mirrors_ARG)) - return _convert_raid_number(cmd, lv, lp); - - if (arg_is_set(cmd, splitmirrors_ARG)) - return _convert_raid_splitmirrors(cmd, lv, lp); - - if (segtype_is_raid(lp->segtype)) { - /* Only --type allowed on hidden RaidLV. */ - if (!lv_is_visible(lv) && !arg_is_set(cmd, type_ARG)) - goto out; - - return _convert_raid_raid(cmd, lv, lp); - } - - if (segtype_is_mirror(lp->segtype)) - return _convert_raid_mirror(cmd, lv, lp); - - if (!strcmp(lp->type_str, SEG_TYPE_NAME_STRIPED)) - return _convert_raid_striped(cmd, lv, lp); - - if (_linear_type_requested(lp->type_str)) - return _convert_raid_linear(cmd, lv, lp); - -out: - log_error("Operation not permitted on raid LV %s.", display_lvname(lv)); - log_error("Operations permitted on a raid LV are:\n%s", permitted_options); - - return 0; -} - -static int _convert_striped(struct cmd_context *cmd, struct logical_volume *lv, - struct lvconvert_params *lp) -{ - const char *mirrors_type = find_config_tree_str(cmd, global_mirror_segtype_default_CFG, NULL); - - if (!strcmp(lp->type_str, SEG_TYPE_NAME_MIRROR)) - return _convert_striped_mirror(cmd, lv, lp); - - if (segtype_is_raid(lp->segtype)) - return _convert_striped_raid(cmd, lv, lp); - - /* --mirrors can mean --type mirror or --type raid1 depending on config setting. */ - - if (arg_is_set(cmd, mirrors_ARG) && mirrors_type && !strcmp(mirrors_type, SEG_TYPE_NAME_MIRROR)) - return _convert_striped_mirror(cmd, lv, lp); - - if (arg_is_set(cmd, mirrors_ARG) && mirrors_type && !strcmp(mirrors_type, SEG_TYPE_NAME_RAID1)) - return _convert_striped_raid(cmd, lv, lp); - - log_error("Operation not permitted on striped or linear LV %s.", display_lvname(lv)); - log_error("Operations permitted on a striped or linear LV are:\n" - " --type mirror\n" - " --type raid*\n"); - - return 0; -} - -/* - * Main entry point. - * lvconvert performs a specific on a specific . - * - * The is specified by command line args. - * The is found using lv_is_foo(lv) functions. - * - * for each lvtype, - * _convert_lvtype(); - * for each arg_is_set(operation) - * _convert_lvtype_operation(); - * - * FIXME: this code (identifying/routing each unique operation through - * _convert_lvtype_op) was designed to work based on the new type that - * the user entered after --type, not the final segment type in lp->type_str. - * Sometimes the two differ because tricks are played with lp->type_str. - * So, when the use of arg_type_str(type_ARG) here was replaced with - * lp->type_str, some commands are no longer identified/routed correctly. - */ -static int _lvconvert(struct cmd_context *cmd, struct logical_volume *lv, - struct lvconvert_params *lp) -{ - struct lv_segment *seg = first_seg(lv); - int ret = 0; - - /* - * Check some conditions that can never be processed. - */ - - if (lv_is_locked(lv)) { - log_error("Cannot convert locked LV %s.", display_lvname(lv)); - goto out; - } - - if (lv_is_pvmove(lv)) { - log_error("Cannot convert pvmove LV %s.", display_lvname(lv)); - goto out; - } - - if (!lv_is_visible(lv)) { - /* - * FIXME: there are some exceptions to the rule of only - * operating on visible LVs. These should be fixed by running - * the command on the visible LV with an option indicating - * which sub LV is intended rather than naming the !visible LV. - */ - if (!lv_is_cache_pool_metadata(lv) && - !lv_is_cache_pool_data(lv) && - !lv_is_thin_pool_metadata(lv) && - !lv_is_thin_pool_data(lv) && - !lv_is_used_cache_pool(lv) && - !lv_is_mirrored(lv) && - !lv_is_raid(lv)) { - log_error("Cannot convert internal LV %s.", display_lvname(lv)); - goto out; - } - } - - /* Set up segtype either from type_str or else to match the existing one. */ - if (!*lp->type_str) - lp->segtype = seg->segtype; - else if (!(lp->segtype = get_segtype_from_string(cmd, lp->type_str))) - goto_out; - - if (!strcmp(lp->type_str, SEG_TYPE_NAME_MIRROR)) { - if (!lp->mirrors_supplied && !seg_is_raid1(seg)) { - log_error("Conversions to --type mirror require -m/--mirrors"); - goto out; - } - } - - /* lv->segtype can't be NULL */ - if (activation() && lp->segtype->ops->target_present && - !lp->segtype->ops->target_present(cmd, NULL, &lp->target_attr)) { - log_error("%s: Required device-mapper target(s) not " - "detected in your kernel.", lp->segtype->name); - goto out; - } - - /* Process striping parameters */ - /* FIXME This is incomplete */ - if (_mirror_or_raid_type_requested(cmd, lp->type_str) || _raid0_type_requested(lp->type_str) || - _striped_type_requested(lp->type_str) || lp->mirrorlog || lp->corelog) { - /* FIXME Handle +/- adjustments too? */ - if (!get_stripe_params(cmd, lp->segtype, &lp->stripes, &lp->stripe_size, &lp->stripes_supplied, &lp->stripe_size_supplied)) - goto_out; - - if (_raid0_type_requested(lp->type_str) || _striped_type_requested(lp->type_str)) - /* FIXME Shouldn't need to override get_stripe_params which defaults to 1 stripe (i.e. linear)! */ - /* The default keeps existing number of stripes, handled inside the library code */ - if (!arg_is_set(cmd, stripes_long_ARG)) - lp->stripes = 0; - } - - /* any operations on a cache LV are directed to the cache origin LV. */ - if (lv_is_cache(lv)) - lv = seg_lv(first_seg(lv), 0); - - /* - * Each LV type that can be converted. - * (The existing type of the LV, not a requested type.) - */ - if (lv_is_mirror(lv)) { - ret = _convert_mirror(cmd, lv, lp); - goto out; - } - - if (lv_is_raid(lv)) { - ret = _convert_raid(cmd, lv, lp); - goto out; - } - - /* - * FIXME: add lv_is_striped() and lv_is_linear()? - * This does not include raid0 which is caught by the test above. - * If operations differ between striped and linear, split this case. - */ - if (segtype_is_striped(seg->segtype) || segtype_is_linear(seg->segtype)) { - ret = _convert_striped(cmd, lv, lp); - goto out; - } - - /* - * The intention is to explicitly check all cases above and never - * reach here, but this covers anything that was missed. - */ - log_error("Cannot convert LV %s.", display_lvname(lv)); - -out: - return ret ? ECMD_PROCESSED : ECMD_FAILED; -} - -static struct convert_poll_id_list* _convert_poll_id_list_create(struct cmd_context *cmd, - const struct logical_volume *lv) -{ - struct convert_poll_id_list *idl = (struct convert_poll_id_list *) dm_pool_alloc(cmd->mem, sizeof(struct convert_poll_id_list)); - - if (!idl) { - log_error("Convert poll ID list allocation failed."); - return NULL; - } - - if (!(idl->id = _create_id(cmd, lv->vg->name, lv->name, lv->lvid.s))) { - dm_pool_free(cmd->mem, idl); - return_NULL; - } - - idl->is_merging_origin = lv_is_merging_origin(lv); - idl->is_merging_origin_thin = idl->is_merging_origin && seg_is_thin_volume(find_snapshot(lv)); - - return idl; -} - -static int _lvconvert_and_add_to_poll_list(struct cmd_context *cmd, - struct lvconvert_params *lp, - struct logical_volume *lv) -{ - int ret; - struct lvinfo info; - struct convert_poll_id_list *idl; - - /* _lvconvert() call may alter the reference in lp->lv_to_poll */ - if ((ret = _lvconvert(cmd, lv, lp)) != ECMD_PROCESSED) - stack; - else if (lp->need_polling) { - if (!lv_info(cmd, lp->lv_to_poll, 0, &info, 0, 0) || !info.exists) - log_print_unless_silent("Conversion starts after activation."); - else { - if (!(idl = _convert_poll_id_list_create(cmd, lp->lv_to_poll))) - return_ECMD_FAILED; - dm_list_add(&lp->idls, &idl->list); - } - } - - return ret; -} - -static int _lvconvert_single(struct cmd_context *cmd, struct logical_volume *lv, - struct processing_handle *handle) -{ - struct lvconvert_params *lp = (struct lvconvert_params *) handle->custom_handle; - struct volume_group *vg = lv->vg; - - if (test_mode() && is_lockd_type(vg->lock_type)) { - log_error("Test mode is not yet supported with lock type %s.", - vg->lock_type); - return ECMD_FAILED; - } - - /* - * lp->pvh holds the list of PVs available for allocation or removal - */ - if (lp->pv_count) { - if (!(lp->pvh = create_pv_list(cmd->mem, vg, lp->pv_count, lp->pvs, 0))) - return_ECMD_FAILED; - } else - lp->pvh = &vg->pvs; - - lp->lv_to_poll = lv; - - return _lvconvert_and_add_to_poll_list(cmd, lp, lv); -} - -int lvconvert(struct cmd_context * cmd, int argc, char **argv) -{ - int poll_ret, ret; - int saved_ignore_suspended_devices; - struct convert_poll_id_list *idl; - struct lvconvert_params lp = { - .conv_type = CONV_OTHER, - .target_attr = ~0, - .idls = DM_LIST_HEAD_INIT(lp.idls), - }; - struct processing_handle *handle = init_processing_handle(cmd, NULL); - - cmd->command->flags &= ~GET_VGNAME_FROM_OPTIONS; - - if (!handle) { - log_error("Failed to initialize processing handle."); - return ECMD_FAILED; - } - - handle->custom_handle = &lp; - - if (!_read_params(cmd, argc, argv, &lp)) { - ret = EINVALID_CMD_LINE; - goto_out; - } - - saved_ignore_suspended_devices = ignore_suspended_devices(); - - ret = process_each_lv(cmd, 0, NULL, lp.vg_name, lp.lv_name, READ_FOR_UPDATE, - handle, NULL, &_lvconvert_single); - - init_ignore_suspended_devices(saved_ignore_suspended_devices); - - dm_list_iterate_items(idl, &lp.idls) { - poll_ret = _lvconvert_poll_by_id(cmd, idl->id, - lp.wait_completion ? 0 : 1U, - idl->is_merging_origin, - idl->is_merging_origin_thin); - if (poll_ret > ret) - ret = poll_ret; - } - -out: - destroy_processing_handle(cmd, handle); - - return ret; -} - -/* - * Below is code that has transitioned to using command defs. - * ---------------------------------------------------------- - * - * This code does not use read_params (or any other param reading - * functions associated with it), or the lp struct. Those have - * been primary vehicles for entangling all the lvconvert operations, - * so avoiding them is important for untangling. They were also - * heavily used for trying to figure out what the lvconvert operation - * was meant to be doing, and that is no longer needed since the - * command def provides it. - * - * All input data is already available from cmd->arg_values and - * cmd->position_argv (the --option args in the former, the position - * args in the later.) There is no need to copy these values into - * another redundant struct of input values which just obfuscates. - * - * The new lvconvert_result struct, passed via custom_handle, is - * used for *returning* data from processing, not for passing data - * into processing. - */ - - -/* - * FIXME: it's very unlikely that the same !visible exceptions apply to every - * lvconvert command. Add specific !visible exceptions in command-specific - * check functions. - */ - -static int _lvconvert_generic_check(struct cmd_context *cmd, struct logical_volume *lv, - struct processing_handle *handle, - int lv_is_named_arg) -{ - if (!lv_is_visible(lv)) { - if (lv_is_cache_pool_metadata(lv) || - lv_is_cache_pool_data(lv) || - lv_is_thin_pool_metadata(lv) || - lv_is_thin_pool_data(lv) || - lv_is_used_cache_pool(lv) || - lv_is_mirrored(lv) || - lv_is_raid(lv)) { - return 1; - } - - log_error("Operation not permitted (%s %d) on hidden LV %s.", - cmd->command->command_line_id, cmd->command->command_line_enum, - display_lvname(lv)); - return 0; - } - - return 1; -} - -/* - * Data/results accumulated during processing. - */ -struct lvconvert_result { - int need_polling; - struct dm_list poll_idls; -}; - - -/* - * repair-related lvconvert utilities - */ - -static int _lvconvert_repair_pvs_mirror(struct cmd_context *cmd, struct logical_volume *lv, - struct processing_handle *handle, - struct dm_list *use_pvh) -{ - struct lvconvert_result *lr = (struct lvconvert_result *) handle->custom_handle; - struct lvconvert_params lp = { 0 }; - struct convert_poll_id_list *idl; - struct lvinfo info; - int ret; - - /* - * FIXME: temporary use of lp because _lvconvert_mirrors_repair() - * and _aux() still use lp fields everywhere. - * Migrate them away from using lp (for the most part just use - * local variables, and check arg_values directly). - */ - - /* - * Fill in any lp fields here that this fn expects to be set before - * it's called. It's hard to tell what the old code expects in lp - * for repair; it doesn't take the stripes option, but it seems to - * expect lp.stripes to be set to 1. - */ - lp.alloc = (alloc_policy_t) arg_uint_value(cmd, alloc_ARG, ALLOC_INHERIT); - lp.stripes = 1; - - ret = _lvconvert_mirrors_repair(cmd, lv, &lp, use_pvh); - - if (lp.need_polling) { - if (!lv_info(cmd, lv, 0, &info, 0, 0) || !info.exists) - log_print_unless_silent("Conversion starts after activation."); - else { - if (!(idl = _convert_poll_id_list_create(cmd, lv))) - return 0; - dm_list_add(&lr->poll_idls, &idl->list); - } - lr->need_polling = 1; - } - - return ret; -} - -static void _lvconvert_repair_pvs_raid_ask(struct cmd_context *cmd, int *do_it) -{ - const char *dev_policy; - - *do_it = 1; - - if (arg_is_set(cmd, usepolicies_ARG)) { - dev_policy = find_config_tree_str(cmd, activation_raid_fault_policy_CFG, NULL); - - if (!strcmp(dev_policy, "allocate") || - !strcmp(dev_policy, "replace")) - return; - - /* else if (!strcmp(dev_policy, "anything_else")) -- no replace */ - *do_it = 0; - return; - } - - if (!arg_count(cmd, yes_ARG) && - yes_no_prompt("Attempt to replace failed RAID images " - "(requires full device resync)? [y/n]: ") == 'n') { - *do_it = 0; - } -} - -static int _lvconvert_repair_pvs_raid(struct cmd_context *cmd, struct logical_volume *lv, - struct processing_handle *handle, - struct dm_list *use_pvh) -{ - struct dm_list *failed_pvs; - int do_it; - - if (!lv_is_active_exclusive_locally(lv_lock_holder(lv))) { - log_error("%s must be active %sto perform this operation.", - display_lvname(lv), - vg_is_clustered(lv->vg) ? - "exclusive locally " : ""); - return 0; - } - - _lvconvert_repair_pvs_raid_ask(cmd, &do_it); - - if (do_it) { - if (!(failed_pvs = _failed_pv_list(lv->vg))) - return_0; - - if (!lv_raid_replace(lv, arg_count(cmd, force_ARG), failed_pvs, use_pvh)) { - log_error("Failed to replace faulty devices in %s.", - display_lvname(lv)); - return 0; - } - - log_print_unless_silent("Faulty devices in %s successfully replaced.", - display_lvname(lv)); - return 1; - } - - /* "warn" if policy not set to replace */ - if (arg_is_set(cmd, usepolicies_ARG)) - log_warn("Use 'lvconvert --repair %s' to replace " - "failed device.", display_lvname(lv)); - return 1; -} - -static int _lvconvert_repair_pvs(struct cmd_context *cmd, struct logical_volume *lv, - struct processing_handle *handle) -{ - struct dm_list *failed_pvs; - struct dm_list *use_pvh; - int ret; - - if (cmd->position_argc > 1) { - /* First pos arg is required LV, remaining are optional PVs. */ - if (!(use_pvh = create_pv_list(cmd->mem, lv->vg, cmd->position_argc - 1, cmd->position_argv + 1, 0))) - return_ECMD_FAILED; - } else - use_pvh = &lv->vg->pvs; - - if (lv_is_raid(lv)) - ret = _lvconvert_repair_pvs_raid(cmd, lv, handle, use_pvh); - else if (lv_is_mirror(lv)) - ret = _lvconvert_repair_pvs_mirror(cmd, lv, handle, use_pvh); - else - ret = 0; - - if (ret && arg_is_set(cmd, usepolicies_ARG)) { - if ((failed_pvs = _failed_pv_list(lv->vg))) - _remove_missing_empty_pv(lv->vg, failed_pvs); - } - - return ret ? ECMD_PROCESSED : ECMD_FAILED; -} - -static int _lvconvert_repair_thinpool(struct cmd_context *cmd, struct logical_volume *lv, - struct processing_handle *handle) -{ - int poolmetadataspare = arg_int_value(cmd, poolmetadataspare_ARG, DEFAULT_POOL_METADATA_SPARE); - struct dm_list *use_pvh; - int ret; - - if (cmd->position_argc > 1) { - /* First pos arg is required LV, remaining are optional PVs. */ - if (!(use_pvh = create_pv_list(cmd->mem, lv->vg, cmd->position_argc - 1, cmd->position_argv + 1, 0))) - return_ECMD_FAILED; - } else - use_pvh = &lv->vg->pvs; - - ret = _lvconvert_thin_pool_repair(cmd, lv, use_pvh, poolmetadataspare); - - return ret ? ECMD_PROCESSED : ECMD_FAILED; -} - -static int _lvconvert_repair_pvs_or_thinpool_single(struct cmd_context *cmd, struct logical_volume *lv, - struct processing_handle *handle) -{ - if (lv_is_thin_pool(lv)) - return _lvconvert_repair_thinpool(cmd, lv, handle); - else if (lv_is_raid(lv) || lv_is_mirror(lv)) - return _lvconvert_repair_pvs(cmd, lv, handle); - else - return_ECMD_FAILED; -} - -/* - * FIXME: add option --repair-pvs to call _lvconvert_repair_pvs() directly, - * and option --repair-thinpool to call _lvconvert_repair_thinpool(). - */ -int lvconvert_repair_pvs_or_thinpool_cmd(struct cmd_context *cmd, int argc, char **argv) -{ - struct processing_handle *handle; - struct lvconvert_result lr = { 0 }; - struct convert_poll_id_list *idl; - int saved_ignore_suspended_devices; - int ret, poll_ret; - - dm_list_init(&lr.poll_idls); - - if (!(handle = init_processing_handle(cmd, NULL))) { - log_error("Failed to initialize processing handle."); - return ECMD_FAILED; - } - - handle->custom_handle = &lr; - - saved_ignore_suspended_devices = ignore_suspended_devices(); - init_ignore_suspended_devices(1); - - cmd->handles_missing_pvs = 1; - - ret = process_each_lv(cmd, 1, cmd->position_argv, NULL, NULL, READ_FOR_UPDATE, - handle, &_lvconvert_generic_check, &_lvconvert_repair_pvs_or_thinpool_single); - - init_ignore_suspended_devices(saved_ignore_suspended_devices); - - if (lr.need_polling) { - dm_list_iterate_items(idl, &lr.poll_idls) { - poll_ret = _lvconvert_poll_by_id(cmd, idl->id, - arg_is_set(cmd, background_ARG), 0, 0); - if (poll_ret > ret) - ret = poll_ret; - } - } - - destroy_processing_handle(cmd, handle); - - return ret; -} - -static int _lvconvert_replace_pv_single(struct cmd_context *cmd, struct logical_volume *lv, - struct processing_handle *handle) -{ - struct arg_value_group_list *group; - const char *tmp_str; - struct dm_list *use_pvh; - struct dm_list *replace_pvh; - char **replace_pvs; - int replace_pv_count; - int i; - - if (cmd->position_argc > 1) { - /* First pos arg is required LV, remaining are optional PVs. */ - if (!(use_pvh = create_pv_list(cmd->mem, lv->vg, cmd->position_argc - 1, cmd->position_argv + 1, 0))) - return_ECMD_FAILED; - } else - use_pvh = &lv->vg->pvs; - - if (!(replace_pv_count = arg_count(cmd, replace_ARG))) - return_ECMD_FAILED; - - if (!(replace_pvs = dm_pool_alloc(cmd->mem, sizeof(char *) * replace_pv_count))) - return_ECMD_FAILED; - - i = 0; - dm_list_iterate_items(group, &cmd->arg_value_groups) { - if (!grouped_arg_is_set(group->arg_values, replace_ARG)) - continue; - if (!(tmp_str = grouped_arg_str_value(group->arg_values, replace_ARG, NULL))) { - log_error("Failed to get '--replace' argument"); - return_ECMD_FAILED; - } - if (!(replace_pvs[i++] = dm_pool_strdup(cmd->mem, tmp_str))) - return_ECMD_FAILED; - } - - if (!(replace_pvh = create_pv_list(cmd->mem, lv->vg, replace_pv_count, replace_pvs, 0))) - return_ECMD_FAILED; - - if (!lv_raid_replace(lv, arg_count(cmd, force_ARG), replace_pvh, use_pvh)) - return_ECMD_FAILED; - - return ECMD_PROCESSED; -} - -int lvconvert_replace_pv_cmd(struct cmd_context *cmd, int argc, char **argv) -{ - struct processing_handle *handle; - struct lvconvert_result lr = { 0 }; - int ret; - - if (!(handle = init_processing_handle(cmd, NULL))) { - log_error("Failed to initialize processing handle."); - return ECMD_FAILED; - } - - handle->custom_handle = &lr; - - ret = process_each_lv(cmd, 1, cmd->position_argv, NULL, NULL, READ_FOR_UPDATE, - handle, &_lvconvert_generic_check, &_lvconvert_replace_pv_single); - - destroy_processing_handle(cmd, handle); - - return ret; -} - - -/* - * snapshot-related lvconvert utilities - */ - - -/* - * Merge a COW snapshot LV into its origin. - */ - -static int _lvconvert_merge_snapshot_single(struct cmd_context *cmd, - struct logical_volume *lv, - struct processing_handle *handle) -{ - struct lvconvert_result *lr = (struct lvconvert_result *) handle->custom_handle; - struct logical_volume *lv_to_poll = NULL; - struct convert_poll_id_list *idl; - - if (!_lvconvert_merge_old_snapshot(cmd, lv, &lv_to_poll)) - return_ECMD_FAILED; - - if (lv_to_poll) { - if (!(idl = _convert_poll_id_list_create(cmd, lv_to_poll))) - return_ECMD_FAILED; - dm_list_add(&lr->poll_idls, &idl->list); - lr->need_polling = 1; - } - - return ECMD_PROCESSED; -} - -static int _lvconvert_merge_snapshot_check(struct cmd_context *cmd, struct logical_volume *lv, - struct processing_handle *handle, - int lv_is_named_arg) -{ - if (!lv_is_visible(lv)) { - log_error("Operation not permitted (%s %d) on hidden LV %s.", - cmd->command->command_line_id, cmd->command->command_line_enum, - display_lvname(lv)); - return 0; - } - - return 1; -} - -int lvconvert_merge_snapshot_cmd(struct cmd_context *cmd, int argc, char **argv) -{ - struct processing_handle *handle; - struct lvconvert_result lr = { 0 }; - struct convert_poll_id_list *idl; - int ret, poll_ret; - - dm_list_init(&lr.poll_idls); - - if (!(handle = init_processing_handle(cmd, NULL))) { - log_error("Failed to initialize processing handle."); - return ECMD_FAILED; - } - - handle->custom_handle = &lr; - - ret = process_each_lv(cmd, cmd->position_argc, cmd->position_argv, NULL, NULL, READ_FOR_UPDATE, - handle, &_lvconvert_merge_snapshot_check, &_lvconvert_merge_snapshot_single); - - if (lr.need_polling) { - dm_list_iterate_items(idl, &lr.poll_idls) { - poll_ret = _lvconvert_poll_by_id(cmd, idl->id, - arg_is_set(cmd, background_ARG), 1, 0); - if (poll_ret > ret) - ret = poll_ret; - } - } - - destroy_processing_handle(cmd, handle); - - return ret; +/* + * Change the type of log used by a mirror LV. + * lvconvert --mirrorlog Type LV + */ +static int _convert_mirror_log(struct cmd_context *cmd, struct logical_volume *lv, + struct lvconvert_params *lp) +{ + return _lvconvert_mirrors(cmd, lv, lp); } /* - * Separate a COW snapshot from its origin. + * Convert mirror LV to linear LV. + * lvconvert --type linear LV * - * lvconvert --splitsnapshot LV_snapshot - * lvconvert_split_cow_snapshot + * Alternate syntax: + * lvconvert --mirrors 0 LV */ - -static int _lvconvert_split_snapshot_single(struct cmd_context *cmd, - struct logical_volume *lv, - struct processing_handle *handle) +static int _convert_mirror_linear(struct cmd_context *cmd, struct logical_volume *lv, + struct lvconvert_params *lp) { - if (!_lvconvert_splitsnapshot(cmd, lv)) - return_ECMD_FAILED; + return _lvconvert_mirrors(cmd, lv, lp); +} - return ECMD_PROCESSED; +/* + * Convert mirror LV to raid1 LV. + * lvconvert --type raid1 LV + */ +static int _convert_mirror_raid(struct cmd_context *cmd, struct logical_volume *lv, + struct lvconvert_params *lp) +{ + return _lvconvert_raid(lv, lp); } -int lvconvert_split_snapshot_cmd(struct cmd_context *cmd, int argc, char **argv) +/* + * Change the number of images in a raid1 LV. + * lvconvert --mirrors Number LV + */ +static int _convert_raid_number(struct cmd_context *cmd, struct logical_volume *lv, + struct lvconvert_params *lp) { - return process_each_lv(cmd, 1, cmd->position_argv, NULL, NULL, READ_FOR_UPDATE, - NULL, &_lvconvert_generic_check, &_lvconvert_split_snapshot_single); + return _lvconvert_raid(lv, lp); } /* - * Combine two LVs that were once an origin/cow pair of LVs, were then - * separated with --splitsnapshot, and now with this command are combined again - * into the origin/cow pair. - * - * This is an obscure command that has little to no real uses. - * - * The command has unusual handling of position args. The first position arg - * will become the origin LV, and is not processed by process_each_lv. The - * second position arg will become the cow LV and is processed by - * process_each_lv. - * - * The single function can grab the origin LV from position_argv[0]. - * - * begin with an ordinary LV foo: - * lvcreate -n foo -L 1 vg - * - * create a cow snapshot of foo named foosnap: - * lvcreate -s -L 1 -n foosnap vg/foo - * - * now, foo is an "origin LV" and foosnap is a "cow LV" - * (foosnap matches LV_snapshot aka lv_is_cow) - * - * split the two LVs apart: - * lvconvert --splitsnapshot vg/foosnap - * - * now, foo is *not* an origin LV and foosnap is *not* a cow LV - * (foosnap does not match LV_snapshot) - * - * now, combine the two LVs again: - * lvconvert --snapshot vg/foo vg/foosnap - * - * after this, foosnap will match LV_snapshot again. + * Split images from a raid1 LV and use them to create a new LV. + * lvconvert --splitmirrors Number LV * - * FIXME: when splitsnapshot is run, the previous cow LV should be - * flagged in the metadata somehow, and then that flag should be - * required here. As it is now, the first and second args - * (origin and cow) can be swapped and nothing catches it. + * Required options: + * --trackchanges | --name Name */ - -static int _lvconvert_combine_split_snapshot_single(struct cmd_context *cmd, - struct logical_volume *lv, - struct processing_handle *handle) +static int _convert_raid_splitmirrors(struct cmd_context *cmd, struct logical_volume *lv, + struct lvconvert_params *lp) { - const char *origin_name = cmd->position_argv[0]; - - /* If origin_name includes VG name, the VG name is removed. */ - if (!validate_lvname_param(cmd, &lv->vg->name, &origin_name)) - return_ECMD_FAILED; - - if (!_lvconvert_snapshot(cmd, lv, origin_name)) - return_ECMD_FAILED; - - return ECMD_PROCESSED; + /* FIXME: split the splitmirrors section out of _lvconvert_raid and call it here. */ + return _lvconvert_raid(lv, lp); } -int lvconvert_combine_split_snapshot_cmd(struct cmd_context *cmd, int argc, char **argv) +/* + * Convert a raid* LV to use a different raid level. + * lvconvert --type raid* LV + */ +static int _convert_raid_raid(struct cmd_context *cmd, struct logical_volume *lv, + struct lvconvert_params *lp) { - return process_each_lv(cmd, 1, cmd->position_argv + 1, NULL, NULL, READ_FOR_UPDATE, - NULL, &_lvconvert_generic_check, &_lvconvert_combine_split_snapshot_single); + return _lvconvert_raid(lv, lp); } -static int _lvconvert_start_poll_single(struct cmd_context *cmd, - struct logical_volume *lv, - struct processing_handle *handle) +/* + * Convert a raid* LV to a mirror LV. + * lvconvert --type mirror LV + */ +static int _convert_raid_mirror(struct cmd_context *cmd, struct logical_volume *lv, + struct lvconvert_params *lp) { - struct lvconvert_result *lr = (struct lvconvert_result *) handle->custom_handle; - struct convert_poll_id_list *idl; - - if (!(idl = _convert_poll_id_list_create(cmd, lv))) - return_ECMD_FAILED; - dm_list_add(&lr->poll_idls, &idl->list); - - lr->need_polling = 1; - - return ECMD_PROCESSED; + return _lvconvert_raid(lv, lp); } -int lvconvert_start_poll_cmd(struct cmd_context *cmd, int argc, char **argv) +/* + * Convert a raid* LV to a striped LV. + * lvconvert --type striped LV + */ +static int _convert_raid_striped(struct cmd_context *cmd, struct logical_volume *lv, + struct lvconvert_params *lp) { - struct processing_handle *handle; - struct lvconvert_result lr = { 0 }; - struct convert_poll_id_list *idl; - int saved_ignore_suspended_devices; - int ret, poll_ret; - - dm_list_init(&lr.poll_idls); - - if (!(handle = init_processing_handle(cmd, NULL))) { - log_error("Failed to initialize processing handle."); - return ECMD_FAILED; - } - - handle->custom_handle = &lr; - - saved_ignore_suspended_devices = ignore_suspended_devices(); - init_ignore_suspended_devices(1); - - cmd->handles_missing_pvs = 1; - - ret = process_each_lv(cmd, 1, cmd->position_argv, NULL, NULL, READ_FOR_UPDATE, - handle, NULL, &_lvconvert_start_poll_single); - - init_ignore_suspended_devices(saved_ignore_suspended_devices); - - if (lr.need_polling) { - dm_list_iterate_items(idl, &lr.poll_idls) { - poll_ret = _lvconvert_poll_by_id(cmd, idl->id, - arg_is_set(cmd, background_ARG), 0, 0); - if (poll_ret > ret) - ret = poll_ret; - } - } - - destroy_processing_handle(cmd, handle); - - return ret; + return _lvconvert_raid(lv, lp); } -static int _lvconvert_to_pool_single(struct cmd_context *cmd, - struct logical_volume *lv, - struct processing_handle *handle) +/* + * Convert a raid* LV to a linear LV. + * lvconvert --type linear LV + */ +static int _convert_raid_linear(struct cmd_context *cmd, struct logical_volume *lv, + struct lvconvert_params *lp) { - struct dm_list *use_pvh = NULL; - int to_thinpool = 0; - int to_cachepool = 0; - - switch (cmd->command->command_line_enum) { - case lvconvert_to_thinpool_CMD: - to_thinpool = 1; - break; - case lvconvert_to_cachepool_CMD: - to_cachepool = 1; - break; - default: - log_error(INTERNAL_ERROR "Invalid lvconvert pool command"); - return 0; - }; - - if (cmd->position_argc > 1) { - /* First pos arg is required LV, remaining are optional PVs. */ - if (!(use_pvh = create_pv_list(cmd->mem, lv->vg, cmd->position_argc - 1, cmd->position_argv + 1, 0))) - return_ECMD_FAILED; - } else - use_pvh = &lv->vg->pvs; - - if (!_lvconvert_to_pool(cmd, lv, to_thinpool, to_cachepool, use_pvh)) - return_ECMD_FAILED; - - return ECMD_PROCESSED; + return _lvconvert_raid(lv, lp); } /* - * The LV position arg is used as thinpool/cachepool data LV. + * Convert a striped/linear LV to a mirror LV. + * lvconvert --type mirror LV + * + * Required options: + * --mirrors Number + * + * Alternate syntax: + * This is equivalent to above when global/mirror_segtype_default="mirror". + * lvconvert --mirrors Number LV */ +static int _convert_striped_mirror(struct cmd_context *cmd, struct logical_volume *lv, + struct lvconvert_params *lp) +{ + return _lvconvert_mirrors(cmd, lv, lp); +} -int lvconvert_to_pool_cmd(struct cmd_context *cmd, int argc, char **argv) +/* + * Convert a striped/linear LV to a raid* LV. + * lvconvert --type raid* LV + * + * Required options: + * --mirrors Number + * + * Alternate syntax: + * This is equivalent to above when global/mirror_segtype_default="raid1". + * lvconvert --mirrors Number LV + */ +static int _convert_striped_raid(struct cmd_context *cmd, struct logical_volume *lv, + struct lvconvert_params *lp) { - return process_each_lv(cmd, 1, cmd->position_argv, NULL, NULL, READ_FOR_UPDATE, - NULL, NULL, &_lvconvert_to_pool_single); + return _lvconvert_raid(lv, lp); } /* - * Reformats non-standard command form into standard command form. + * Functions called to perform all valid operations on a given LV type. * - * In the command variants with no position LV arg, the LV arg is taken from - * the --thinpool/--cachepool arg, and the position args are modified to match - * the standard command form. + * _convert_ */ -int lvconvert_to_pool_noarg_cmd(struct cmd_context *cmd, int argc, char **argv) +static int _convert_mirror(struct cmd_context *cmd, struct logical_volume *lv, + struct lvconvert_params *lp) { - struct command *new_command; - char *pool_data_name; - int i, p; - - switch (cmd->command->command_line_enum) { - case lvconvert_to_thinpool_noarg_CMD: - pool_data_name = (char *)arg_str_value(cmd, thinpool_ARG, NULL); - new_command = get_command(lvconvert_to_thinpool_CMD); - break; - case lvconvert_to_cachepool_noarg_CMD: - pool_data_name = (char *)arg_str_value(cmd, cachepool_ARG, NULL); - new_command = get_command(lvconvert_to_cachepool_CMD); - break; - default: - log_error(INTERNAL_ERROR "Unknown pool conversion."); - return 0; - }; + if (arg_is_set(cmd, mirrors_ARG)) + return _convert_mirror_number(cmd, lv, lp); - log_debug("Changing command line id %s %d to standard form %s %d", - cmd->command->command_line_id, cmd->command->command_line_enum, - new_command->command_line_id, new_command->command_line_enum); + if (arg_is_set(cmd, splitmirrors_ARG)) + return _convert_mirror_splitmirrors(cmd, lv, lp); + + if (arg_is_set(cmd, mirrorlog_ARG) || arg_is_set(cmd, corelog_ARG)) + return _convert_mirror_log(cmd, lv, lp); - /* Make the LV the first position arg. */ + if (_linear_type_requested(lp->type_str)) + return _convert_mirror_linear(cmd, lv, lp); - p = cmd->position_argc; - for (i = 0; i < cmd->position_argc; i++) - cmd->position_argv[p] = cmd->position_argv[p-1]; + if (segtype_is_raid(lp->segtype)) + return _convert_mirror_raid(cmd, lv, lp); - cmd->position_argv[0] = pool_data_name; - cmd->position_argc++; - cmd->command = new_command; + log_error("Operation not permitted on mirror LV %s.", display_lvname(lv)); + log_error("Operations permitted on a mirror LV are:\n" + " --mirrors\n" + " --splitmirrors\n" + " --mirrorlog\n" + " --type linear\n" + " --type raid*\n"); - return lvconvert_to_pool_cmd(cmd, argc, argv); + return 0; } -static int _lvconvert_to_cache_vol_single(struct cmd_context *cmd, - struct logical_volume *lv, - struct processing_handle *handle) +static int _convert_raid(struct cmd_context *cmd, struct logical_volume *lv, + struct lvconvert_params *lp) { - struct volume_group *vg = lv->vg; - struct logical_volume *cachepool_lv; - const char *cachepool_name; - uint32_t chunk_size = 0; - - if (!(cachepool_name = arg_str_value(cmd, cachepool_ARG, NULL))) - goto_out; - - if (!validate_lvname_param(cmd, &vg->name, &cachepool_name)) - goto_out; - - if (!(cachepool_lv = find_lv(vg, cachepool_name))) { - log_error("Cache pool %s not found.", cachepool_name); - goto out; - } - - /* - * If cachepool_lv is not yet a cache pool, convert it to one. - * If using an existing cache pool, wipe it. - */ + /* Permitted convert options on visible or hidden RaidLVs */ + const char *permitted_options = lv_is_visible(lv) ? + " --mirrors\n" + " --splitmirrors\n" + " --type raid*\n" + " --type mirror\n" + " --type striped\n" + " --type linear\n" + : + " --mirrors\n" + " --splitmirrors\n" + " --replace\n" + " --type raid*\n" + " --type mirror\n" + " --type striped\n" + " --type linear\n"; - if (!lv_is_cache_pool(cachepool_lv)) { - int lvt_enum = get_lvt_enum(cachepool_lv); - struct lv_types *lvtype = get_lv_type(lvt_enum); + /* Applicable to any hidden _or_ visible LVs. */ + if (arg_is_set(cmd, mirrors_ARG)) + return _convert_raid_number(cmd, lv, lp); - if (lvt_enum != striped_LVT && lvt_enum != linear_LVT && lvt_enum != raid_LVT) { - log_error("LV %s with type %s cannot be converted to a cache pool.", - display_lvname(cachepool_lv), lvtype ? lvtype->name : "unknown"); - goto out; - } + if (arg_is_set(cmd, splitmirrors_ARG)) + return _convert_raid_splitmirrors(cmd, lv, lp); - if (!_lvconvert_to_pool(cmd, cachepool_lv, 0, 1, &vg->pvs)) { - log_error("LV %s could not be converted to a cache pool.", - display_lvname(cachepool_lv)); + if (segtype_is_raid(lp->segtype)) { + /* Only --type allowed on hidden RaidLV. */ + if (!lv_is_visible(lv) && !arg_is_set(cmd, type_ARG)) goto out; - } - if (!(cachepool_lv = find_lv(vg, cachepool_name))) { - log_error("LV %s cannot be found.", display_lvname(cachepool_lv)); - goto out; - } + return _convert_raid_raid(cmd, lv, lp); + } - if (!lv_is_cache_pool(cachepool_lv)) { - log_error("LV %s is not a cache pool.", display_lvname(cachepool_lv)); - goto out; - } - } else { - if (!dm_list_empty(&cachepool_lv->segs_using_this_lv)) { - log_error("Cache pool %s is already in use.", cachepool_name); - goto out; - } + if (segtype_is_mirror(lp->segtype)) + return _convert_raid_mirror(cmd, lv, lp); - if (arg_is_set(cmd, chunksize_ARG)) - chunk_size = arg_uint_value(cmd, chunksize_ARG, 0); - if (!chunk_size) - chunk_size = first_seg(cachepool_lv)->chunk_size; + if (!strcmp(lp->type_str, SEG_TYPE_NAME_STRIPED)) + return _convert_raid_striped(cmd, lv, lp); - /* FIXME: why is chunk_size read and checked if it's not used? */ + if (_linear_type_requested(lp->type_str)) + return _convert_raid_linear(cmd, lv, lp); - if (!validate_lv_cache_chunk_size(cachepool_lv, chunk_size)) - goto_out; +out: + log_error("Operation not permitted on raid LV %s.", display_lvname(lv)); + log_error("Operations permitted on a raid LV are:\n%s", permitted_options); - /* Note: requires rather deep know-how to skip zeroing */ - if (!arg_is_set(cmd, zero_ARG)) { - if (!arg_is_set(cmd, yes_ARG) && - yes_no_prompt("Do you want wipe existing metadata of cache pool %s? [y/n]: ", - display_lvname(cachepool_lv)) == 'n') { - log_error("Conversion aborted."); - log_error("To preserve cache metadata add option \"--zero n\"."); - log_warn("WARNING: Reusing mismatched cache pool metadata MAY DESTROY YOUR DATA!"); - goto out; - } - /* Wiping confirmed, go ahead */ - if (!wipe_cache_pool(cachepool_lv)) - goto_out; - } else if (arg_int_value(cmd, zero_ARG, 0)) { - if (!wipe_cache_pool(cachepool_lv)) - goto_out; - } else { - log_warn("WARNING: Reusing cache pool metadata %s for volume caching.", - display_lvname(cachepool_lv)); - } + return 0; +} - } +static int _convert_striped(struct cmd_context *cmd, struct logical_volume *lv, + struct lvconvert_params *lp) +{ + const char *mirrors_type = find_config_tree_str(cmd, global_mirror_segtype_default_CFG, NULL); - /* When the lv arg is a thinpool, redirect command to data sub lv. */ + if (!strcmp(lp->type_str, SEG_TYPE_NAME_MIRROR)) + return _convert_striped_mirror(cmd, lv, lp); - if (lv_is_thin_pool(lv)) { - lv = seg_lv(first_seg(lv), 0); - log_verbose("Redirecting operation to data sub LV %s.", display_lvname(lv)); - } + if (segtype_is_raid(lp->segtype)) + return _convert_striped_raid(cmd, lv, lp); - /* Convert lv to cache vol using cachepool_lv. */ + /* --mirrors can mean --type mirror or --type raid1 depending on config setting. */ - if (!_lvconvert_to_cache_vol(cmd, lv, cachepool_lv)) - goto_out; + if (arg_is_set(cmd, mirrors_ARG) && mirrors_type && !strcmp(mirrors_type, SEG_TYPE_NAME_MIRROR)) + return _convert_striped_mirror(cmd, lv, lp); - return ECMD_PROCESSED; + if (arg_is_set(cmd, mirrors_ARG) && mirrors_type && !strcmp(mirrors_type, SEG_TYPE_NAME_RAID1)) + return _convert_striped_raid(cmd, lv, lp); - out: - return ECMD_FAILED; -} + log_error("Operation not permitted on striped or linear LV %s.", display_lvname(lv)); + log_error("Operations permitted on a striped or linear LV are:\n" + " --type mirror\n" + " --type raid*\n"); -int lvconvert_to_cache_vol_cmd(struct cmd_context *cmd, int argc, char **argv) -{ - return process_each_lv(cmd, 1, cmd->position_argv, NULL, NULL, READ_FOR_UPDATE, - NULL, NULL, &_lvconvert_to_cache_vol_single); + return 0; } -static int _lvconvert_to_thin_with_external_single(struct cmd_context *cmd, - struct logical_volume *lv, - struct processing_handle *handle) +/* + * Main entry point. + * lvconvert performs a specific on a specific . + * + * The is specified by command line args. + * The is found using lv_is_foo(lv) functions. + * + * for each lvtype, + * _convert_lvtype(); + * for each arg_is_set(operation) + * _convert_lvtype_operation(); + * + * FIXME: this code (identifying/routing each unique operation through + * _convert_lvtype_op) was designed to work based on the new type that + * the user entered after --type, not the final segment type in lp->type_str. + * Sometimes the two differ because tricks are played with lp->type_str. + * So, when the use of arg_type_str(type_ARG) here was replaced with + * lp->type_str, some commands are no longer identified/routed correctly. + */ +static int _lvconvert(struct cmd_context *cmd, struct logical_volume *lv, + struct lvconvert_params *lp) { - struct volume_group *vg = lv->vg; - struct logical_volume *thinpool_lv; - const char *thinpool_name; - - if (!(thinpool_name = arg_str_value(cmd, thinpool_ARG, NULL))) - goto_out; + struct lv_segment *seg = first_seg(lv); + int ret = 0; - if (!validate_lvname_param(cmd, &vg->name, &thinpool_name)) - goto_out; + /* + * Check some conditions that can never be processed. + */ - if (!(thinpool_lv = find_lv(vg, thinpool_name))) { - log_error("Thin pool %s not found.", thinpool_name); + if (lv_is_locked(lv)) { + log_error("Cannot convert locked LV %s.", display_lvname(lv)); goto out; } - /* If thinpool_lv is not yet a thin pool, convert it to one. */ - - if (!lv_is_thin_pool(thinpool_lv)) { - int lvt_enum = get_lvt_enum(thinpool_lv); - struct lv_types *lvtype = get_lv_type(lvt_enum); - - if (lvt_enum != striped_LVT && lvt_enum != linear_LVT && lvt_enum != raid_LVT) { - log_error("LV %s with type %s cannot be converted to a thin pool.", - display_lvname(thinpool_lv), lvtype ? lvtype->name : "unknown"); - goto out; - } + if (lv_is_pvmove(lv)) { + log_error("Cannot convert pvmove LV %s.", display_lvname(lv)); + goto out; + } - if (!_lvconvert_to_pool(cmd, thinpool_lv, 1, 0, &vg->pvs)) { - log_error("LV %s could not be converted to a thin pool.", - display_lvname(thinpool_lv)); + if (!lv_is_visible(lv)) { + /* + * FIXME: there are some exceptions to the rule of only + * operating on visible LVs. These should be fixed by running + * the command on the visible LV with an option indicating + * which sub LV is intended rather than naming the !visible LV. + */ + if (!lv_is_cache_pool_metadata(lv) && + !lv_is_cache_pool_data(lv) && + !lv_is_thin_pool_metadata(lv) && + !lv_is_thin_pool_data(lv) && + !lv_is_used_cache_pool(lv) && + !lv_is_mirrored(lv) && + !lv_is_raid(lv)) { + log_error("Cannot convert internal LV %s.", display_lvname(lv)); goto out; } + } - if (!(thinpool_lv = find_lv(vg, thinpool_name))) { - log_error("LV %s cannot be found.", display_lvname(thinpool_lv)); - goto out; - } + /* Set up segtype either from type_str or else to match the existing one. */ + if (!*lp->type_str) + lp->segtype = seg->segtype; + else if (!(lp->segtype = get_segtype_from_string(cmd, lp->type_str))) + goto_out; - if (!lv_is_thin_pool(thinpool_lv)) { - log_error("LV %s is not a thin pool.", display_lvname(thinpool_lv)); + if (!strcmp(lp->type_str, SEG_TYPE_NAME_MIRROR)) { + if (!lp->mirrors_supplied && !seg_is_raid1(seg)) { + log_error("Conversions to --type mirror require -m/--mirrors"); goto out; } } - /* Convert lv to thin with external origin using thinpool_lv. */ - - if (!_lvconvert_to_thin_with_external(cmd, lv, thinpool_lv)) - goto_out; - - return ECMD_PROCESSED; - - out: - return ECMD_FAILED; -} - -int lvconvert_to_thin_with_external_cmd(struct cmd_context *cmd, int argc, char **argv) -{ - return process_each_lv(cmd, 1, cmd->position_argv, NULL, NULL, READ_FOR_UPDATE, - NULL, NULL, &_lvconvert_to_thin_with_external_single); -} + /* lv->segtype can't be NULL */ + if (activation() && lp->segtype->ops->target_present && + !lp->segtype->ops->target_present(cmd, NULL, &lp->target_attr)) { + log_error("%s: Required device-mapper target(s) not " + "detected in your kernel.", lp->segtype->name); + goto out; + } -static int _lvconvert_swap_pool_metadata_single(struct cmd_context *cmd, - struct logical_volume *lv, - struct processing_handle *handle) -{ - struct volume_group *vg = lv->vg; - struct logical_volume *metadata_lv; - const char *metadata_name; + /* Process striping parameters */ + /* FIXME This is incomplete */ + if (_mirror_or_raid_type_requested(cmd, lp->type_str) || _raid0_type_requested(lp->type_str) || + _striped_type_requested(lp->type_str) || lp->mirrorlog || lp->corelog) { + /* FIXME Handle +/- adjustments too? */ + if (!get_stripe_params(cmd, lp->segtype, &lp->stripes, &lp->stripe_size, &lp->stripes_supplied, &lp->stripe_size_supplied)) + goto_out; - if (!(metadata_name = arg_str_value(cmd, poolmetadata_ARG, NULL))) - goto_out; + if (_raid0_type_requested(lp->type_str) || _striped_type_requested(lp->type_str)) + /* FIXME Shouldn't need to override get_stripe_params which defaults to 1 stripe (i.e. linear)! */ + /* The default keeps existing number of stripes, handled inside the library code */ + if (!arg_is_set(cmd, stripes_long_ARG)) + lp->stripes = 0; + } - if (!validate_lvname_param(cmd, &vg->name, &metadata_name)) - goto_out; + /* any operations on a cache LV are directed to the cache origin LV. */ + if (lv_is_cache(lv)) + lv = seg_lv(first_seg(lv), 0); - if (!(metadata_lv = find_lv(vg, metadata_name))) { - log_error("Metadata LV %s not found.", metadata_name); + /* + * Each LV type that can be converted. + * (The existing type of the LV, not a requested type.) + */ + if (lv_is_mirror(lv)) { + ret = _convert_mirror(cmd, lv, lp); goto out; } - if (metadata_lv == lv) { - log_error("Can't use same LV for pool data and metadata LV %s.", - display_lvname(metadata_lv)); + if (lv_is_raid(lv)) { + ret = _convert_raid(cmd, lv, lp); goto out; } - if (!_lvconvert_swap_pool_metadata(cmd, lv, metadata_lv)) - goto_out; - - return ECMD_PROCESSED; + /* + * FIXME: add lv_is_striped() and lv_is_linear()? + * This does not include raid0 which is caught by the test above. + * If operations differ between striped and linear, split this case. + */ + if (segtype_is_striped(seg->segtype) || segtype_is_linear(seg->segtype)) { + ret = _convert_striped(cmd, lv, lp); + goto out; + } - out: - return ECMD_FAILED; -} + /* + * The intention is to explicitly check all cases above and never + * reach here, but this covers anything that was missed. + */ + log_error("Cannot convert LV %s.", display_lvname(lv)); -int lvconvert_swap_pool_metadata_cmd(struct cmd_context *cmd, int argc, char **argv) -{ - return process_each_lv(cmd, 1, cmd->position_argv, NULL, NULL, READ_FOR_UPDATE, - NULL, NULL, &_lvconvert_swap_pool_metadata_single); +out: + return ret ? ECMD_PROCESSED : ECMD_FAILED; } -#if 0 -int lvconvert_swap_pool_metadata_noarg_cmd(struct cmd_context *cmd, int argc, char **argv) +static int _lvconvert_and_add_to_poll_list(struct cmd_context *cmd, + struct lvconvert_params *lp, + struct logical_volume *lv) { - struct command *new_command; - char *pool_name; - - switch (cmd->command->command_line_enum) { - case lvconvert_swap_thinpool_metadata_CMD: - pool_name = (char *)arg_str_value(cmd, thinpool_ARG, NULL); - break; - case lvconvert_swap_cachepool_metadata_CMD: - pool_name = (char *)arg_str_value(cmd, cachepool_ARG, NULL); - break; - default: - log_error(INTERNAL_ERROR "Unknown pool conversion."); - return 0; - }; - - new_command = get_command(lvconvert_swap_pool_metadata_CMD); - - log_debug("Changing command line id %s %d to standard form %s %d", - cmd->command->command_line_id, cmd->command->command_line_enum, - new_command->command_line_id, new_command->command_line_enum); - - /* Make the LV the first position arg. */ + int ret; + struct lvinfo info; + struct convert_poll_id_list *idl; - cmd->position_argv[0] = pool_name; - cmd->position_argc++; - cmd->command = new_command; + /* _lvconvert() call may alter the reference in lp->lv_to_poll */ + if ((ret = _lvconvert(cmd, lv, lp)) != ECMD_PROCESSED) + stack; + else if (lp->need_polling) { + if (!lv_info(cmd, lp->lv_to_poll, 0, &info, 0, 0) || !info.exists) + log_print_unless_silent("Conversion starts after activation."); + else { + if (!(idl = convert_poll_id_list_create(cmd, lp->lv_to_poll))) + return_ECMD_FAILED; + dm_list_add(&lp->idls, &idl->list); + } + } - return lvconvert_swap_pool_metadata_cmd(cmd, argc, argv); + return ret; } -#endif -static int _lvconvert_merge_thin_single(struct cmd_context *cmd, - struct logical_volume *lv, - struct processing_handle *handle) +static int _lvconvert_single(struct cmd_context *cmd, struct logical_volume *lv, + struct processing_handle *handle) { - if (!_lvconvert_merge_thin_snapshot(cmd, lv)) + struct lvconvert_params *lp = (struct lvconvert_params *) handle->custom_handle; + struct volume_group *vg = lv->vg; + + if (test_mode() && is_lockd_type(vg->lock_type)) { + log_error("Test mode is not yet supported with lock type %s.", + vg->lock_type); return ECMD_FAILED; + } - return ECMD_PROCESSED; -} + /* + * lp->pvh holds the list of PVs available for allocation or removal + */ + if (lp->pv_count) { + if (!(lp->pvh = create_pv_list(cmd->mem, vg, lp->pv_count, lp->pvs, 0))) + return_ECMD_FAILED; + } else + lp->pvh = &vg->pvs; -int lvconvert_merge_thin_cmd(struct cmd_context *cmd, int argc, char **argv) -{ - return process_each_lv(cmd, cmd->position_argc, cmd->position_argv, NULL, NULL, READ_FOR_UPDATE, - NULL, NULL, &_lvconvert_merge_thin_single); + lp->lv_to_poll = lv; + + return _lvconvert_and_add_to_poll_list(cmd, lp, lv); } -static int _lvconvert_split_cachepool_single(struct cmd_context *cmd, - struct logical_volume *lv, - struct processing_handle *handle) +int lvconvert(struct cmd_context * cmd, int argc, char **argv) { - struct logical_volume *cache_lv = NULL; - struct logical_volume *cachepool_lv = NULL; - struct lv_segment *seg; - int ret; - - if (lv_is_cache(lv)) { - cache_lv = lv; - cachepool_lv = first_seg(cache_lv)->pool_lv; - - } else if (lv_is_cache_pool(lv)) { - cachepool_lv = lv; - - if ((dm_list_size(&cachepool_lv->segs_using_this_lv) == 1) && - (seg = get_only_segment_using_this_lv(cachepool_lv)) && - seg_is_cache(seg)) - cache_lv = seg->lv; + int poll_ret, ret; + int saved_ignore_suspended_devices; + struct convert_poll_id_list *idl; + struct lvconvert_params lp = { + .conv_type = CONV_OTHER, + .target_attr = ~0, + .idls = DM_LIST_HEAD_INIT(lp.idls), + }; + struct processing_handle *handle = init_processing_handle(cmd, NULL); - } else if (lv_is_thin_pool(lv)) { - cache_lv = seg_lv(first_seg(lv), 0); /* cached _tdata */ - cachepool_lv = first_seg(cache_lv)->pool_lv; - } + cmd->command->flags &= ~GET_VGNAME_FROM_OPTIONS; - if (!cache_lv) { - log_error("Cannot find cache LV from %s.", display_lvname(lv)); + if (!handle) { + log_error("Failed to initialize processing handle."); return ECMD_FAILED; } - if (!cachepool_lv) { - log_error("Cannot find cache pool LV from %s.", display_lvname(lv)); - return ECMD_FAILED; - } + handle->custom_handle = &lp; - switch (cmd->command->command_line_enum) { - case lvconvert_split_and_keep_cachepool_CMD: - ret = _lvconvert_split_and_keep_cachepool(cmd, cache_lv, cachepool_lv); - break; - - case lvconvert_split_and_remove_cachepool_CMD: - ret = _lvconvert_split_and_remove_cachepool(cmd, cache_lv, cachepool_lv); - break; - default: - log_error(INTERNAL_ERROR "Unknown cache pool split."); - ret = 0; + if (!_read_params(cmd, argc, argv, &lp)) { + ret = EINVALID_CMD_LINE; + goto_out; } - if (!ret) - return ECMD_FAILED; + saved_ignore_suspended_devices = ignore_suspended_devices(); - return ECMD_PROCESSED; -} + ret = process_each_lv(cmd, 0, NULL, lp.vg_name, lp.lv_name, READ_FOR_UPDATE, + handle, NULL, &_lvconvert_single); -int lvconvert_split_cachepool_cmd(struct cmd_context *cmd, int argc, char **argv) -{ - if (cmd->command->command_line_enum == lvconvert_split_and_remove_cachepool_CMD) { - cmd->handles_missing_pvs = 1; - cmd->partial_activation = 1; + init_ignore_suspended_devices(saved_ignore_suspended_devices); + + dm_list_iterate_items(idl, &lp.idls) { + poll_ret = lvconvert_poll_by_id(cmd, idl->id, + lp.wait_completion ? 0 : 1U, + idl->is_merging_origin, + idl->is_merging_origin_thin); + if (poll_ret > ret) + ret = poll_ret; } - return process_each_lv(cmd, 1, cmd->position_argv, NULL, NULL, READ_FOR_UPDATE, - NULL, NULL, &_lvconvert_split_cachepool_single); +out: + destroy_processing_handle(cmd, handle); + + return ret; } -static int _lvconvert_merge_mirror_images_single(struct cmd_context *cmd, - struct logical_volume *lv, - struct processing_handle *handle) +int lvconvert_merge_mirror_images_single(struct cmd_context *cmd, + struct logical_volume *lv, + struct processing_handle *handle) { if (!lv_raid_merge(lv)) return ECMD_FAILED; @@ -4557,60 +2038,6 @@ int lvconvert_merge_mirror_images_cmd(struct cmd_context *cmd, int argc, char ** cmd->command->flags &= ~GET_VGNAME_FROM_OPTIONS; return process_each_lv(cmd, cmd->position_argc, cmd->position_argv, NULL, NULL, READ_FOR_UPDATE, - NULL, NULL, &_lvconvert_merge_mirror_images_single); -} - -static int _lvconvert_merge_generic_single(struct cmd_context *cmd, - struct logical_volume *lv, - struct processing_handle *handle) -{ - int ret; - - if (lv_is_cow(lv)) - ret = _lvconvert_merge_snapshot_single(cmd, lv, handle); - - else if (lv_is_thin_volume(lv)) - ret = _lvconvert_merge_thin_single(cmd, lv, handle); - - else - ret = _lvconvert_merge_mirror_images_single(cmd, lv, handle); - - return ret; -} - -int lvconvert_merge_cmd(struct cmd_context *cmd, int argc, char **argv) -{ - struct processing_handle *handle; - struct lvconvert_result lr = { 0 }; - struct convert_poll_id_list *idl; - int ret, poll_ret; - - dm_list_init(&lr.poll_idls); - - if (!(handle = init_processing_handle(cmd, NULL))) { - log_error("Failed to initialize processing handle."); - return ECMD_FAILED; - } - - handle->custom_handle = &lr; - - cmd->command->flags &= ~GET_VGNAME_FROM_OPTIONS; - - ret = process_each_lv(cmd, cmd->position_argc, cmd->position_argv, NULL, NULL, READ_FOR_UPDATE, - handle, NULL, &_lvconvert_merge_generic_single); - - /* polling is only used by merge_snapshot */ - if (lr.need_polling) { - dm_list_iterate_items(idl, &lr.poll_idls) { - poll_ret = _lvconvert_poll_by_id(cmd, idl->id, - arg_is_set(cmd, background_ARG), 1, 0); - if (poll_ret > ret) - ret = poll_ret; - } - } - - destroy_processing_handle(cmd, handle); - - return ret; + NULL, NULL, &lvconvert_merge_mirror_images_single); } diff --git a/tools/lvconvert_other.c b/tools/lvconvert_other.c new file mode 100644 index 000000000..b26ffb013 --- /dev/null +++ b/tools/lvconvert_other.c @@ -0,0 +1,350 @@ +/* + * Copyright (C) 2005-2016 Red Hat, Inc. All rights reserved. + * + * This file is part of LVM2. + * + * This copyrighted material is made available to anyone wishing to use, + * modify, copy, or redistribute it subject to the terms and conditions + * of the GNU Lesser General Public License v.2.1. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "tools.h" + +#include "polldaemon.h" +#include "lv_alloc.h" +#include "lvconvert_poll.h" +#include "command-lines-count.h" + +/* + * FIXME: it's very unlikely that the same !visible exceptions apply to every + * lvconvert command. Add specific !visible exceptions in command-specific + * check functions. + */ + +int lvconvert_generic_check(struct cmd_context *cmd, struct logical_volume *lv, + struct processing_handle *handle, + int lv_is_named_arg) +{ + if (!lv_is_visible(lv)) { + if (lv_is_cache_pool_metadata(lv) || + lv_is_cache_pool_data(lv) || + lv_is_thin_pool_metadata(lv) || + lv_is_thin_pool_data(lv) || + lv_is_used_cache_pool(lv) || + lv_is_mirrored(lv) || + lv_is_raid(lv)) { + return 1; + } + + log_error("Operation not permitted (%s %d) on hidden LV %s.", + cmd->command->command_line_id, cmd->command->command_line_enum, + display_lvname(lv)); + return 0; + } + + return 1; +} + +/* + * Reomove missing and empty PVs from VG, if are also in provided list + */ +static void _remove_missing_empty_pv(struct volume_group *vg, struct dm_list *remove_pvs) +{ + struct pv_list *pvl, *pvl_vg, *pvlt; + int removed = 0; + + if (!remove_pvs) + return; + + dm_list_iterate_items(pvl, remove_pvs) { + dm_list_iterate_items_safe(pvl_vg, pvlt, &vg->pvs) { + if (!id_equal(&pvl->pv->id, &pvl_vg->pv->id) || + !is_missing_pv(pvl_vg->pv) || + pvl_vg->pv->pe_alloc_count != 0) + continue; + + /* FIXME: duplication of vgreduce code, move this to library */ + vg->free_count -= pvl_vg->pv->pe_count; + vg->extent_count -= pvl_vg->pv->pe_count; + del_pvl_from_vgs(vg, pvl_vg); + free_pv_fid(pvl_vg->pv); + + removed++; + } + } + + if (removed) { + if (!vg_write(vg) || !vg_commit(vg)) { + stack; + return; + } + log_warn("%d missing and now unallocated Physical Volumes removed from VG.", removed); + } +} + +static int _lvconvert_repair_pvs(struct cmd_context *cmd, struct logical_volume *lv, + struct processing_handle *handle) +{ + struct dm_list *failed_pvs; + struct dm_list *use_pvh; + int ret; + + if (cmd->position_argc > 1) { + /* First pos arg is required LV, remaining are optional PVs. */ + if (!(use_pvh = create_pv_list(cmd->mem, lv->vg, cmd->position_argc - 1, cmd->position_argv + 1, 0))) + return_ECMD_FAILED; + } else + use_pvh = &lv->vg->pvs; + + if (lv_is_raid(lv)) + ret = lvconvert_repair_pvs_raid(cmd, lv, handle, use_pvh); + else if (lv_is_mirror(lv)) + ret = lvconvert_repair_pvs_mirror(cmd, lv, handle, use_pvh); + else + ret = 0; + + if (ret && arg_is_set(cmd, usepolicies_ARG)) { + if ((failed_pvs = failed_pv_list(lv->vg))) + _remove_missing_empty_pv(lv->vg, failed_pvs); + } + + return ret ? ECMD_PROCESSED : ECMD_FAILED; +} + +static int _lvconvert_repair_pvs_or_thinpool_single(struct cmd_context *cmd, struct logical_volume *lv, + struct processing_handle *handle) +{ + if (lv_is_thin_pool(lv)) + return lvconvert_repair_thinpool(cmd, lv, handle); + else if (lv_is_raid(lv) || lv_is_mirror(lv)) + return _lvconvert_repair_pvs(cmd, lv, handle); + else + return_ECMD_FAILED; +} + +/* + * FIXME: add option --repair-pvs to call _lvconvert_repair_pvs() directly, + * and option --repair-thinpool to call _lvconvert_repair_thinpool(). + */ +int lvconvert_repair_pvs_or_thinpool_cmd(struct cmd_context *cmd, int argc, char **argv) +{ + struct processing_handle *handle; + struct lvconvert_result lr = { 0 }; + struct convert_poll_id_list *idl; + int saved_ignore_suspended_devices; + int ret, poll_ret; + + dm_list_init(&lr.poll_idls); + + if (!(handle = init_processing_handle(cmd, NULL))) { + log_error("Failed to initialize processing handle."); + return ECMD_FAILED; + } + + handle->custom_handle = &lr; + + saved_ignore_suspended_devices = ignore_suspended_devices(); + init_ignore_suspended_devices(1); + + cmd->handles_missing_pvs = 1; + + ret = process_each_lv(cmd, 1, cmd->position_argv, NULL, NULL, READ_FOR_UPDATE, + handle, &lvconvert_generic_check, &_lvconvert_repair_pvs_or_thinpool_single); + + init_ignore_suspended_devices(saved_ignore_suspended_devices); + + if (lr.need_polling) { + dm_list_iterate_items(idl, &lr.poll_idls) { + poll_ret = lvconvert_poll_by_id(cmd, idl->id, + arg_is_set(cmd, background_ARG), 0, 0); + if (poll_ret > ret) + ret = poll_ret; + } + } + + destroy_processing_handle(cmd, handle); + + return ret; +} + +static int _lvconvert_replace_pv_single(struct cmd_context *cmd, struct logical_volume *lv, + struct processing_handle *handle) +{ + struct arg_value_group_list *group; + const char *tmp_str; + struct dm_list *use_pvh; + struct dm_list *replace_pvh; + char **replace_pvs; + int replace_pv_count; + int i; + + if (cmd->position_argc > 1) { + /* First pos arg is required LV, remaining are optional PVs. */ + if (!(use_pvh = create_pv_list(cmd->mem, lv->vg, cmd->position_argc - 1, cmd->position_argv + 1, 0))) + return_ECMD_FAILED; + } else + use_pvh = &lv->vg->pvs; + + if (!(replace_pv_count = arg_count(cmd, replace_ARG))) + return_ECMD_FAILED; + + if (!(replace_pvs = dm_pool_alloc(cmd->mem, sizeof(char *) * replace_pv_count))) + return_ECMD_FAILED; + + i = 0; + dm_list_iterate_items(group, &cmd->arg_value_groups) { + if (!grouped_arg_is_set(group->arg_values, replace_ARG)) + continue; + if (!(tmp_str = grouped_arg_str_value(group->arg_values, replace_ARG, NULL))) { + log_error("Failed to get '--replace' argument"); + return_ECMD_FAILED; + } + if (!(replace_pvs[i++] = dm_pool_strdup(cmd->mem, tmp_str))) + return_ECMD_FAILED; + } + + if (!(replace_pvh = create_pv_list(cmd->mem, lv->vg, replace_pv_count, replace_pvs, 0))) + return_ECMD_FAILED; + + if (!lv_raid_replace(lv, arg_count(cmd, force_ARG), replace_pvh, use_pvh)) + return_ECMD_FAILED; + + return ECMD_PROCESSED; +} + +int lvconvert_replace_pv_cmd(struct cmd_context *cmd, int argc, char **argv) +{ + struct processing_handle *handle; + struct lvconvert_result lr = { 0 }; + int ret; + + if (!(handle = init_processing_handle(cmd, NULL))) { + log_error("Failed to initialize processing handle."); + return ECMD_FAILED; + } + + handle->custom_handle = &lr; + + ret = process_each_lv(cmd, 1, cmd->position_argv, NULL, NULL, READ_FOR_UPDATE, + handle, &lvconvert_generic_check, &_lvconvert_replace_pv_single); + + destroy_processing_handle(cmd, handle); + + return ret; +} + +static int _lvconvert_start_poll_single(struct cmd_context *cmd, + struct logical_volume *lv, + struct processing_handle *handle) +{ + struct lvconvert_result *lr = (struct lvconvert_result *) handle->custom_handle; + struct convert_poll_id_list *idl; + + if (!(idl = convert_poll_id_list_create(cmd, lv))) + return_ECMD_FAILED; + dm_list_add(&lr->poll_idls, &idl->list); + + lr->need_polling = 1; + + return ECMD_PROCESSED; +} + +int lvconvert_start_poll_cmd(struct cmd_context *cmd, int argc, char **argv) +{ + struct processing_handle *handle; + struct lvconvert_result lr = { 0 }; + struct convert_poll_id_list *idl; + int saved_ignore_suspended_devices; + int ret, poll_ret; + + dm_list_init(&lr.poll_idls); + + if (!(handle = init_processing_handle(cmd, NULL))) { + log_error("Failed to initialize processing handle."); + return ECMD_FAILED; + } + + handle->custom_handle = &lr; + + saved_ignore_suspended_devices = ignore_suspended_devices(); + init_ignore_suspended_devices(1); + + cmd->handles_missing_pvs = 1; + + ret = process_each_lv(cmd, 1, cmd->position_argv, NULL, NULL, READ_FOR_UPDATE, + handle, NULL, &_lvconvert_start_poll_single); + + init_ignore_suspended_devices(saved_ignore_suspended_devices); + + if (lr.need_polling) { + dm_list_iterate_items(idl, &lr.poll_idls) { + poll_ret = lvconvert_poll_by_id(cmd, idl->id, + arg_is_set(cmd, background_ARG), 0, 0); + if (poll_ret > ret) + ret = poll_ret; + } + } + + destroy_processing_handle(cmd, handle); + + return ret; +} + +static int _lvconvert_merge_generic_single(struct cmd_context *cmd, + struct logical_volume *lv, + struct processing_handle *handle) +{ + int ret; + + if (lv_is_cow(lv)) + ret = lvconvert_merge_snapshot_single(cmd, lv, handle); + + else if (lv_is_thin_volume(lv)) + ret = lvconvert_merge_thin_single(cmd, lv, handle); + + else + ret = lvconvert_merge_mirror_images_single(cmd, lv, handle); + + return ret; +} + +int lvconvert_merge_cmd(struct cmd_context *cmd, int argc, char **argv) +{ + struct processing_handle *handle; + struct lvconvert_result lr = { 0 }; + struct convert_poll_id_list *idl; + int ret, poll_ret; + + dm_list_init(&lr.poll_idls); + + if (!(handle = init_processing_handle(cmd, NULL))) { + log_error("Failed to initialize processing handle."); + return ECMD_FAILED; + } + + handle->custom_handle = &lr; + + cmd->command->flags &= ~GET_VGNAME_FROM_OPTIONS; + + ret = process_each_lv(cmd, cmd->position_argc, cmd->position_argv, NULL, NULL, READ_FOR_UPDATE, + handle, NULL, &_lvconvert_merge_generic_single); + + /* polling is only used by merge_snapshot */ + if (lr.need_polling) { + dm_list_iterate_items(idl, &lr.poll_idls) { + poll_ret = lvconvert_poll_by_id(cmd, idl->id, + arg_is_set(cmd, background_ARG), 1, 0); + if (poll_ret > ret) + ret = poll_ret; + } + } + + destroy_processing_handle(cmd, handle); + + return ret; +} + diff --git a/tools/lvconvert_poll.c b/tools/lvconvert_poll.c index 36db1c751..f427b62e6 100644 --- a/tools/lvconvert_poll.c +++ b/tools/lvconvert_poll.c @@ -188,3 +188,118 @@ progress_t poll_thin_merge_progress(struct cmd_context *cmd, return PROGRESS_FINISHED_ALL; /* Merging happend */ } + +static struct poll_operation_id *_create_id(struct cmd_context *cmd, + const char *vg_name, + const char *lv_name, + const char *uuid) +{ + struct poll_operation_id *id; + char lv_full_name[NAME_LEN]; + + if (!vg_name || !lv_name || !uuid) { + log_error(INTERNAL_ERROR "Wrong params for lvconvert _create_id."); + return NULL; + } + + if (dm_snprintf(lv_full_name, sizeof(lv_full_name), "%s/%s", vg_name, lv_name) < 0) { + log_error(INTERNAL_ERROR "Name \"%s/%s\" is too long.", vg_name, lv_name); + return NULL; + } + + if (!(id = dm_pool_alloc(cmd->mem, sizeof(*id)))) { + log_error("Poll operation ID allocation failed."); + return NULL; + } + + if (!(id->display_name = dm_pool_strdup(cmd->mem, lv_full_name)) || + !(id->lv_name = strchr(id->display_name, '/')) || + !(id->vg_name = dm_pool_strdup(cmd->mem, vg_name)) || + !(id->uuid = dm_pool_strdup(cmd->mem, uuid))) { + log_error("Failed to copy one or more poll operation ID members."); + dm_pool_free(cmd->mem, id); + return NULL; + } + + id->lv_name++; /* skip over '/' */ + + return id; +} + +static struct poll_functions _lvconvert_mirror_fns = { + .poll_progress = poll_mirror_progress, + .finish_copy = lvconvert_mirror_finish, +}; + +static struct poll_functions _lvconvert_merge_fns = { + .poll_progress = poll_merge_progress, + .finish_copy = lvconvert_merge_finish, +}; + +static struct poll_functions _lvconvert_thin_merge_fns = { + .poll_progress = poll_thin_merge_progress, + .finish_copy = lvconvert_merge_finish, +}; + +int lvconvert_poll_by_id(struct cmd_context *cmd, struct poll_operation_id *id, + unsigned background, + int is_merging_origin, + int is_merging_origin_thin) +{ + if (test_mode()) + return ECMD_PROCESSED; + + if (is_merging_origin) + return poll_daemon(cmd, background, + (MERGING | (is_merging_origin_thin ? THIN_VOLUME : SNAPSHOT)), + is_merging_origin_thin ? &_lvconvert_thin_merge_fns : &_lvconvert_merge_fns, + "Merged", id); + else + return poll_daemon(cmd, background, CONVERTING, + &_lvconvert_mirror_fns, "Converted", id); +} + +int lvconvert_poll(struct cmd_context *cmd, struct logical_volume *lv, + unsigned background) +{ + int r; + struct poll_operation_id *id = _create_id(cmd, lv->vg->name, lv->name, lv->lvid.s); + int is_merging_origin = 0; + int is_merging_origin_thin = 0; + + if (!id) { + log_error("Failed to allocate poll identifier for lvconvert."); + return ECMD_FAILED; + } + + /* FIXME: check this in polling instead */ + if (lv_is_merging_origin(lv)) { + is_merging_origin = 1; + is_merging_origin_thin = seg_is_thin_volume(find_snapshot(lv)); + } + + r = lvconvert_poll_by_id(cmd, id, background, is_merging_origin, is_merging_origin_thin); + + return r; +} + +struct convert_poll_id_list *convert_poll_id_list_create(struct cmd_context *cmd, const struct logical_volume *lv) +{ + struct convert_poll_id_list *idl = (struct convert_poll_id_list *) dm_pool_alloc(cmd->mem, sizeof(struct convert_poll_id_list)); + + if (!idl) { + log_error("Convert poll ID list allocation failed."); + return NULL; + } + + if (!(idl->id = _create_id(cmd, lv->vg->name, lv->name, lv->lvid.s))) { + dm_pool_free(cmd->mem, idl); + return_NULL; + } + + idl->is_merging_origin = lv_is_merging_origin(lv); + idl->is_merging_origin_thin = idl->is_merging_origin && seg_is_thin_volume(find_snapshot(lv)); + + return idl; +} + diff --git a/tools/lvconvert_poll.h b/tools/lvconvert_poll.h index 7fdcb14a6..46fbf9f57 100644 --- a/tools/lvconvert_poll.h +++ b/tools/lvconvert_poll.h @@ -21,6 +21,26 @@ struct cmd_context; struct logical_volume; struct volume_group; + /* + * Data/results accumulated during processing. + */ +struct lvconvert_result { + int need_polling; + struct dm_list poll_idls; +}; + +struct convert_poll_id_list { + struct dm_list list; + struct poll_operation_id *id; + unsigned is_merging_origin:1; + unsigned is_merging_origin_thin:1; +}; + +int lvconvert_poll_by_id(struct cmd_context *cmd, struct poll_operation_id *id, + unsigned background, int is_merging_origin, int is_merging_origin_thin); +struct convert_poll_id_list *convert_poll_id_list_create(struct cmd_context *cmd, + const struct logical_volume *lv); + int lvconvert_mirror_finish(struct cmd_context *cmd, struct volume_group *vg, struct logical_volume *lv, diff --git a/tools/lvconvert_pool.c b/tools/lvconvert_pool.c new file mode 100644 index 000000000..aa1ab3c89 --- /dev/null +++ b/tools/lvconvert_pool.c @@ -0,0 +1,1660 @@ +/* + * Copyright (C) 2005-2016 Red Hat, Inc. All rights reserved. + * + * This file is part of LVM2. + * + * This copyrighted material is made available to anyone wishing to use, + * modify, copy, or redistribute it subject to the terms and conditions + * of the GNU Lesser General Public License v.2.1. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "tools.h" + +#include "polldaemon.h" +#include "lv_alloc.h" +#include "lvconvert_poll.h" +#include "command-lines-count.h" + +static int _lvconvert_thin_pool_repair(struct cmd_context *cmd, + struct logical_volume *pool_lv, + struct dm_list *pvh, int poolmetadataspare) +{ + const char *dmdir = dm_dir(); + const char *thin_dump = + find_config_tree_str_allow_empty(cmd, global_thin_dump_executable_CFG, NULL); + const char *thin_repair = + find_config_tree_str_allow_empty(cmd, global_thin_repair_executable_CFG, NULL); + const struct dm_config_node *cn; + const struct dm_config_value *cv; + int ret = 0, status; + int args = 0; + const char *argv[19]; /* Max supported 10 args */ + char *dm_name, *trans_id_str; + char meta_path[PATH_MAX]; + char pms_path[PATH_MAX]; + uint64_t trans_id; + struct logical_volume *pmslv; + struct logical_volume *mlv = first_seg(pool_lv)->metadata_lv; + struct pipe_data pdata; + FILE *f; + + if (!thin_repair || !thin_repair[0]) { + log_error("Thin repair commnand is not configured. Repair is disabled."); + return 0; /* Checking disabled */ + } + + pmslv = pool_lv->vg->pool_metadata_spare_lv; + + /* Check we have pool metadata spare LV */ + if (!handle_pool_metadata_spare(pool_lv->vg, 0, pvh, 1)) + return_0; + + if (pmslv != pool_lv->vg->pool_metadata_spare_lv) { + if (!vg_write(pool_lv->vg) || !vg_commit(pool_lv->vg)) + return_0; + pmslv = pool_lv->vg->pool_metadata_spare_lv; + } + + if (!(dm_name = dm_build_dm_name(cmd->mem, mlv->vg->name, + mlv->name, NULL)) || + (dm_snprintf(meta_path, sizeof(meta_path), "%s/%s", dmdir, dm_name) < 0)) { + log_error("Failed to build thin metadata path."); + return 0; + } + + if (!(dm_name = dm_build_dm_name(cmd->mem, pmslv->vg->name, + pmslv->name, NULL)) || + (dm_snprintf(pms_path, sizeof(pms_path), "%s/%s", dmdir, dm_name) < 0)) { + log_error("Failed to build pool metadata spare path."); + return 0; + } + + if (!(cn = find_config_tree_array(cmd, global_thin_repair_options_CFG, NULL))) { + log_error(INTERNAL_ERROR "Unable to find configuration for global/thin_repair_options"); + return 0; + } + + for (cv = cn->v; cv && args < 16; cv = cv->next) { + if (cv->type != DM_CFG_STRING) { + log_error("Invalid string in config file: " + "global/thin_repair_options"); + return 0; + } + argv[++args] = cv->v.str; + } + + if (args == 10) { + log_error("Too many options for thin repair command."); + return 0; + } + + argv[0] = thin_repair; + argv[++args] = "-i"; + argv[++args] = meta_path; + argv[++args] = "-o"; + argv[++args] = pms_path; + argv[++args] = NULL; + + if (pool_is_active(pool_lv)) { + log_error("Only inactive pool can be repaired."); + return 0; + } + + if (!activate_lv_local(cmd, pmslv)) { + log_error("Cannot activate pool metadata spare volume %s.", + pmslv->name); + return 0; + } + + if (!activate_lv_local(cmd, mlv)) { + log_error("Cannot activate thin pool metadata volume %s.", + mlv->name); + goto deactivate_pmslv; + } + + if (!(ret = exec_cmd(cmd, (const char * const *)argv, &status, 1))) { + log_error("Repair of thin metadata volume of thin pool %s failed (status:%d). " + "Manual repair required!", + display_lvname(pool_lv), status); + goto deactivate_mlv; + } + + if (thin_dump[0]) { + argv[0] = thin_dump; + argv[1] = pms_path; + argv[2] = NULL; + + if (!(f = pipe_open(cmd, argv, 0, &pdata))) + log_warn("WARNING: Cannot read output from %s %s.", thin_dump, pms_path); + else { + /* + * Scan only the 1st. line for transation id. + * Watch out, if the thin_dump format changes + */ + if (fgets(meta_path, sizeof(meta_path), f) && + (trans_id_str = strstr(meta_path, "transaction=\"")) && + (sscanf(trans_id_str + 13, FMTu64, &trans_id) == 1) && + (trans_id != first_seg(pool_lv)->transaction_id) && + ((trans_id - 1) != first_seg(pool_lv)->transaction_id)) + log_error("Transaction id " FMTu64 " from pool \"%s/%s\" " + "does not match repaired transaction id " + FMTu64 " from %s.", + first_seg(pool_lv)->transaction_id, + pool_lv->vg->name, pool_lv->name, trans_id, + pms_path); + + (void) pipe_close(&pdata); /* killing pipe */ + } + } + +deactivate_mlv: + if (!deactivate_lv(cmd, mlv)) { + log_error("Cannot deactivate thin pool metadata volume %s.", + mlv->name); + return 0; + } + +deactivate_pmslv: + if (!deactivate_lv(cmd, pmslv)) { + log_error("Cannot deactivate thin pool metadata volume %s.", + mlv->name); + return 0; + } + + if (!ret) + return 0; + + if (pmslv == pool_lv->vg->pool_metadata_spare_lv) { + pool_lv->vg->pool_metadata_spare_lv = NULL; + pmslv->status &= ~POOL_METADATA_SPARE; + lv_set_visible(pmslv); + } + + /* Try to allocate new pool metadata spare LV */ + if (!handle_pool_metadata_spare(pool_lv->vg, 0, pvh, poolmetadataspare)) + stack; + + if (dm_snprintf(meta_path, sizeof(meta_path), "%s_meta%%d", pool_lv->name) < 0) { + log_error("Can't prepare new metadata name for %s.", pool_lv->name); + return 0; + } + + if (!generate_lv_name(pool_lv->vg, meta_path, pms_path, sizeof(pms_path))) { + log_error("Can't generate new name for %s.", meta_path); + return 0; + } + + if (!detach_pool_metadata_lv(first_seg(pool_lv), &mlv)) + return_0; + + /* Swap _pmspare and _tmeta name */ + if (!swap_lv_identifiers(cmd, mlv, pmslv)) + return_0; + + if (!attach_pool_metadata_lv(first_seg(pool_lv), pmslv)) + return_0; + + /* Used _tmeta (now _pmspare) becomes _meta%d */ + if (!lv_rename_update(cmd, mlv, pms_path, 0)) + return_0; + + if (!vg_write(pool_lv->vg) || !vg_commit(pool_lv->vg)) + return_0; + + log_warn("WARNING: If everything works, remove %s volume.", + display_lvname(mlv)); + + log_warn("WARNING: Use pvmove command to move %s on the best fitting PV.", + display_lvname(first_seg(pool_lv)->metadata_lv)); + + return 1; +} + +int lvconvert_repair_thinpool(struct cmd_context *cmd, struct logical_volume *lv, + struct processing_handle *handle) +{ + int poolmetadataspare = arg_int_value(cmd, poolmetadataspare_ARG, DEFAULT_POOL_METADATA_SPARE); + struct dm_list *use_pvh; + int ret; + + if (cmd->position_argc > 1) { + /* First pos arg is required LV, remaining are optional PVs. */ + if (!(use_pvh = create_pv_list(cmd->mem, lv->vg, cmd->position_argc - 1, cmd->position_argv + 1, 0))) + return_ECMD_FAILED; + } else + use_pvh = &lv->vg->pvs; + + ret = _lvconvert_thin_pool_repair(cmd, lv, use_pvh, poolmetadataspare); + + return ret ? ECMD_PROCESSED : ECMD_FAILED; +} + +static int _lvconvert_merge_thin_snapshot(struct cmd_context *cmd, + struct logical_volume *lv) +{ + int origin_is_active = 0, r = 0; + struct lv_segment *snap_seg = first_seg(lv); + struct logical_volume *origin = snap_seg->origin; + + if (!origin) { + log_error("%s is not a mergeable logical volume.", + display_lvname(lv)); + return 0; + } + + /* Check if merge is possible */ + if (lv_is_merging_origin(origin)) { + log_error("Snapshot %s is already merging into the origin.", + display_lvname(find_snapshot(origin)->lv)); + return 0; + } + + if (lv_is_external_origin(origin)) { + if (!(origin = origin_from_cow(lv))) + log_error(INTERNAL_ERROR "%s is missing origin.", + display_lvname(lv)); + else + log_error("%s is read-only external origin %s.", + display_lvname(lv), display_lvname(origin)); + return 0; + } + + if (lv_is_origin(origin)) { + log_error("Merging into the old snapshot origin %s is not supported.", + display_lvname(origin)); + return 0; + } + + if (!archive(lv->vg)) + return_0; + + /* + * Prevent merge with open device(s) as it would likely lead + * to application/filesystem failure. Merge on origin's next + * activation if either the origin or snapshot LV can't be + * deactivated. + */ + if (!deactivate_lv(cmd, lv)) + log_print_unless_silent("Delaying merge since snapshot is open."); + else if ((origin_is_active = lv_is_active(origin)) && + !deactivate_lv(cmd, origin)) + log_print_unless_silent("Delaying merge since origin volume is open."); + else { + /* + * Both thin snapshot and origin are inactive, + * replace the origin LV with its snapshot LV. + */ + if (!thin_merge_finish(cmd, origin, lv)) + goto_out; + + if (origin_is_active && !activate_lv(cmd, lv)) { + log_error("Failed to reactivate origin %s.", + display_lvname(lv)); + goto out; + } + + r = 1; + goto out; + } + + init_snapshot_merge(snap_seg, origin); + + /* Commit vg, merge will start with next activation */ + if (!vg_write(lv->vg) || !vg_commit(lv->vg)) + return_0; + + r = 1; +out: + backup(lv->vg); + + if (r) + log_print_unless_silent("Merging of thin snapshot %s will occur on " + "next activation of %s.", + display_lvname(lv), display_lvname(origin)); + + return r; +} + +static int _lvconvert_split_and_keep_cachepool(struct cmd_context *cmd, + struct logical_volume *lv, + struct logical_volume *cachepool_lv) +{ + log_debug("Detaching cache pool %s from cache LV %s.", + display_lvname(cachepool_lv), display_lvname(lv)); + + if (!archive(lv->vg)) + return_0; + + if (!lv_cache_remove(lv)) + return_0; + + if (!vg_write(lv->vg) || !vg_commit(lv->vg)) + return_0; + + backup(lv->vg); + + log_print_unless_silent("Logical volume %s is not cached and cache pool %s is unused.", + display_lvname(lv), display_lvname(cachepool_lv)); + + return 1; +} + +static int _lvconvert_split_and_remove_cachepool(struct cmd_context *cmd, + struct logical_volume *lv, + struct logical_volume *cachepool_lv) +{ + struct lv_segment *seg; + struct logical_volume *remove_lv; + + seg = first_seg(lv); + + if (lv_is_partial(seg_lv(seg, 0))) { + log_warn("WARNING: Cache origin logical volume %s is missing.", + display_lvname(seg_lv(seg, 0))); + remove_lv = lv; /* When origin is missing, drop everything */ + } else + remove_lv = seg->pool_lv; + + if (lv_is_partial(seg_lv(first_seg(seg->pool_lv), 0))) + log_warn("WARNING: Cache pool data logical volume %s is missing.", + display_lvname(seg_lv(first_seg(seg->pool_lv), 0))); + + if (lv_is_partial(first_seg(seg->pool_lv)->metadata_lv)) + log_warn("WARNING: Cache pool metadata logical volume %s is missing.", + display_lvname(first_seg(seg->pool_lv)->metadata_lv)); + + /* TODO: Check for failed cache as well to get prompting? */ + if (lv_is_partial(lv)) { + if (first_seg(seg->pool_lv)->cache_mode != CACHE_MODE_WRITETHROUGH) { + if (!arg_count(cmd, force_ARG)) { + log_error("Conversion aborted."); + log_error("Cannot uncache writethrough cache volume %s without --force.", + display_lvname(lv)); + return 0; + } + log_warn("WARNING: Uncaching of partially missing writethrough cache volume %s might destroy your data.", + display_lvname(lv)); + } + + if (!arg_count(cmd, yes_ARG) && + yes_no_prompt("Do you really want to uncache %s with missing LVs? [y/n]: ", + display_lvname(lv)) == 'n') { + log_error("Conversion aborted."); + return 0; + } + } + + if (lvremove_single(cmd, remove_lv, NULL) != ECMD_PROCESSED) + return_0; + + if (remove_lv != lv) + log_print_unless_silent("Logical volume %s is not cached.", display_lvname(lv)); + + return 1; +} + +static int _lvconvert_to_thin_with_external(struct cmd_context *cmd, + struct logical_volume *lv, + struct logical_volume *thinpool_lv) +{ + struct volume_group *vg = lv->vg; + struct logical_volume *thin_lv; + const char *origin_name; + + struct lvcreate_params lvc = { + .activate = CHANGE_AEY, + .alloc = ALLOC_INHERIT, + .major = -1, + .minor = -1, + .suppress_zero_warn = 1, /* Suppress warning for this thin */ + .permission = LVM_READ, + .pool_name = thinpool_lv->name, + .pvh = &vg->pvs, + .read_ahead = DM_READ_AHEAD_AUTO, + .stripes = 1, + .virtual_extents = lv->le_count, + }; + + if (lv == thinpool_lv) { + log_error("Can't use same LV %s for thin pool and thin volume.", + display_lvname(thinpool_lv)); + return 0; + } + + if ((origin_name = arg_str_value(cmd, originname_ARG, NULL))) + if (!validate_restricted_lvname_param(cmd, &vg->name, &origin_name)) + return_0; + + /* + * If NULL, an auto-generated 'lvol' name is used. + * If set, the lv create code checks the name isn't used. + */ + lvc.lv_name = origin_name; + + if (is_lockd_type(vg->lock_type)) { + /* + * FIXME: external origins don't work in lockd VGs. + * Prior to the lvconvert, there's a lock associated with + * the uuid of the external origin LV. After the convert, + * that uuid belongs to the new thin LV, and a new LV with + * a new uuid exists as the non-thin, readonly external LV. + * We'd need to remove the lock for the previous uuid + * (the new thin LV will have no lock), and create a new + * lock for the new LV uuid used by the external LV. + */ + log_error("Can't use lock_type %s LV as external origin.", + vg->lock_type); + return 0; + } + + dm_list_init(&lvc.tags); + + if (!pool_supports_external_origin(first_seg(thinpool_lv), lv)) + return_0; + + if (!(lvc.segtype = get_segtype_from_string(cmd, SEG_TYPE_NAME_THIN))) + return_0; + + if (!archive(vg)) + return_0; + + /* + * New thin LV needs to be created (all messages sent to pool) In this + * case thin volume is created READ-ONLY and also warn about not + * zeroing is suppressed. + * + * The new thin LV is created with the origin_name, or an autogenerated + * 'lvol' name. Then the names and ids are swapped between the thin LV + * and the original/external LV. So, the thin LV gets the name and id + * of the original LV arg, and the original LV arg gets the origin_name + * or the autogenerated name. + */ + + if (!(thin_lv = lv_create_single(vg, &lvc))) + return_0; + + if (!deactivate_lv(cmd, thin_lv)) { + log_error("Aborting. Unable to deactivate new LV. " + "Manual intervention required."); + return 0; + } + + /* + * Crashing till this point will leave plain thin volume + * which could be easily removed by the user after i.e. power-off + */ + + if (!swap_lv_identifiers(cmd, thin_lv, lv)) { + stack; + goto revert_new_lv; + } + + /* Preserve read-write status of original LV here */ + thin_lv->status |= (lv->status & LVM_WRITE); + + if (!attach_thin_external_origin(first_seg(thin_lv), lv)) { + stack; + goto revert_new_lv; + } + + if (!lv_update_and_reload(thin_lv)) { + stack; + goto deactivate_and_revert_new_lv; + } + + log_print_unless_silent("Converted %s to thin volume with external origin %s.", + display_lvname(thin_lv), display_lvname(lv)); + + return 1; + +deactivate_and_revert_new_lv: + if (!swap_lv_identifiers(cmd, thin_lv, lv)) + stack; + + if (!deactivate_lv(cmd, thin_lv)) { + log_error("Unable to deactivate failed new LV. " + "Manual intervention required."); + return 0; + } + + if (!detach_thin_external_origin(first_seg(thin_lv))) + return_0; + +revert_new_lv: + /* FIXME Better to revert to backup of metadata? */ + if (!lv_remove(thin_lv) || !vg_write(vg) || !vg_commit(vg)) + log_error("Manual intervention may be required to remove " + "abandoned LV(s) before retrying."); + else + backup(vg); + + return 0; +} + +static int _lvconvert_swap_pool_metadata(struct cmd_context *cmd, + struct logical_volume *lv, + struct logical_volume *metadata_lv) +{ + struct volume_group *vg = lv->vg; + struct logical_volume *prev_metadata_lv; + struct lv_segment *seg; + struct lv_types *lvtype; + char meta_name[NAME_LEN]; + const char *swap_name; + uint32_t chunk_size; + int is_thinpool; + int is_cachepool; + int lvt_enum; + + is_thinpool = lv_is_thin_pool(lv); + is_cachepool = lv_is_cache_pool(lv); + lvt_enum = get_lvt_enum(metadata_lv); + lvtype = get_lv_type(lvt_enum); + + if (lvt_enum != striped_LVT && lvt_enum != linear_LVT && lvt_enum != raid_LVT) { + log_error("LV %s with type %s cannot be used as a metadata LV.", + display_lvname(metadata_lv), lvtype ? lvtype->name : "unknown"); + return 0; + } + + if (!lv_is_visible(metadata_lv)) { + log_error("Can't convert internal LV %s.", + display_lvname(metadata_lv)); + return 0; + } + + if (lv_is_locked(metadata_lv)) { + log_error("Can't convert locked LV %s.", + display_lvname(metadata_lv)); + return 0; + } + + if (lv_is_origin(metadata_lv) || + lv_is_merging_origin(metadata_lv) || + lv_is_external_origin(metadata_lv) || + lv_is_virtual(metadata_lv)) { + log_error("Pool metadata LV %s is of an unsupported type.", + display_lvname(metadata_lv)); + return 0; + } + + /* FIXME cache pool */ + if (is_thinpool && pool_is_active(lv)) { + /* If any volume referencing pool active - abort here */ + log_error("Cannot convert pool %s with active volumes.", + display_lvname(lv)); + return 0; + } + + if ((dm_snprintf(meta_name, sizeof(meta_name), "%s%s", lv->name, is_cachepool ? "_cmeta" : "_tmeta") < 0)) { + log_error("Failed to create internal lv names, pool name is too long."); + return 0; + } + + seg = first_seg(lv); + + /* Normally do NOT change chunk size when swapping */ + + if (arg_is_set(cmd, chunksize_ARG)) { + chunk_size = arg_uint_value(cmd, chunksize_ARG, 0); + + if ((chunk_size != seg->chunk_size) && !dm_list_empty(&lv->segs_using_this_lv)) { + if (arg_count(cmd, force_ARG) == PROMPT) { + log_error("Chunk size can be only changed with --force. Conversion aborted."); + return 0; + } + + if (!validate_pool_chunk_size(cmd, seg->segtype, chunk_size)) + return_0; + + log_warn("WARNING: Changing chunk size %s to %s for %s pool volume.", + display_size(cmd, seg->chunk_size), + display_size(cmd, chunk_size), + display_lvname(lv)); + + /* Ok, user has likely some serious reason for this */ + if (!arg_count(cmd, yes_ARG) && + yes_no_prompt("Do you really want to change chunk size for %s pool volume? [y/n]: ", + display_lvname(lv)) == 'n') { + log_error("Conversion aborted."); + return 0; + } + } + + seg->chunk_size = chunk_size; + } + + if (!arg_count(cmd, yes_ARG) && + yes_no_prompt("Do you want to swap metadata of %s pool with metadata volume %s? [y/n]: ", + display_lvname(lv), + display_lvname(metadata_lv)) == 'n') { + log_error("Conversion aborted."); + return 0; + } + + if (!deactivate_lv(cmd, metadata_lv)) { + log_error("Aborting. Failed to deactivate %s.", + display_lvname(metadata_lv)); + return 0; + } + + if (!archive(vg)) + return_0; + + /* Swap names between old and new metadata LV */ + + if (!detach_pool_metadata_lv(seg, &prev_metadata_lv)) + return_0; + + swap_name = metadata_lv->name; + + if (!lv_rename_update(cmd, metadata_lv, "pvmove_tmeta", 0)) + return_0; + + /* Give the previous metadata LV the name of the LV replacing it. */ + + if (!lv_rename_update(cmd, prev_metadata_lv, swap_name, 0)) + return_0; + + /* Rename deactivated metadata LV to have _tmeta suffix */ + + if (!lv_rename_update(cmd, metadata_lv, meta_name, 0)) + return_0; + + if (!attach_pool_metadata_lv(seg, metadata_lv)) + return_0; + + if (!vg_write(vg) || !vg_commit(vg)) + return_0; + + backup(vg); + return 1; +} + +/* + * Create a new pool LV, using the lv arg as the data sub LV. + * The metadata sub LV is either a new LV created here, or an + * existing LV specified by --poolmetadata. + */ + +static int _lvconvert_to_pool(struct cmd_context *cmd, + struct logical_volume *lv, + int to_thinpool, + int to_cachepool, + struct dm_list *use_pvh) +{ + struct volume_group *vg = lv->vg; + struct logical_volume *metadata_lv = NULL; /* existing or created */ + struct logical_volume *data_lv; /* lv arg renamed */ + struct logical_volume *pool_lv; /* new lv created here */ + const char *pool_metadata_name; /* user-specified lv name */ + const char *pool_name; /* name of original lv arg */ + char meta_name[NAME_LEN]; /* generated sub lv name */ + char data_name[NAME_LEN]; /* generated sub lv name */ + struct segment_type *pool_segtype; /* thinpool or cachepool */ + struct lv_segment *seg; + unsigned int target_attr = ~0; + unsigned int passed_args = 0; + unsigned int activate_pool; + unsigned int zero_metadata; + uint64_t meta_size; + uint32_t meta_extents; + uint32_t chunk_size; + int chunk_calc; + int r = 0; + + /* for handling lvmlockd cases */ + char *lockd_data_args = NULL; + char *lockd_meta_args = NULL; + char *lockd_data_name = NULL; + char *lockd_meta_name = NULL; + struct id lockd_data_id; + struct id lockd_meta_id; + + + if (lv_is_thin_pool(lv) || lv_is_cache_pool(lv)) { + log_error(INTERNAL_ERROR "LV %s is already a pool.", display_lvname(lv)); + return 0; + } + + pool_segtype = to_cachepool ? get_segtype_from_string(cmd, SEG_TYPE_NAME_CACHE_POOL) : + get_segtype_from_string(cmd, SEG_TYPE_NAME_THIN_POOL); + + if (!pool_segtype->ops->target_present(cmd, NULL, &target_attr)) { + log_error("%s: Required device-mapper target(s) not detected in your kernel.", pool_segtype->name); + return 0; + } + + /* Allow to have only thinpool active and restore it's active state. */ + activate_pool = to_thinpool && lv_is_active(lv); + + /* Wipe metadata_lv by default, but allow skipping this for cache pools. */ + zero_metadata = to_cachepool ? arg_int_value(cmd, zero_ARG, 1) : 1; + + /* An existing LV needs to have its lock freed once it becomes a data LV. */ + if (is_lockd_type(vg->lock_type) && lv->lock_args) { + lockd_data_args = dm_pool_strdup(cmd->mem, lv->lock_args); + lockd_data_name = dm_pool_strdup(cmd->mem, lv->name); + memcpy(&lockd_data_id, &lv->lvid.id[1], sizeof(struct id)); + } + + /* + * If an existing LV is to be used as the metadata LV, + * verify that it's in a usable state. These checks are + * not done by command def rules because this LV is not + * processed by process_each_lv. + */ + + if ((pool_metadata_name = arg_str_value(cmd, poolmetadata_ARG, NULL))) { + if (!(metadata_lv = find_lv(vg, pool_metadata_name))) { + log_error("Unknown pool metadata LV %s.", pool_metadata_name); + return 0; + } + + /* An existing LV needs to have its lock freed once it becomes a meta LV. */ + if (is_lockd_type(vg->lock_type) && metadata_lv->lock_args) { + lockd_meta_args = dm_pool_strdup(cmd->mem, metadata_lv->lock_args); + lockd_meta_name = dm_pool_strdup(cmd->mem, metadata_lv->name); + memcpy(&lockd_meta_id, &metadata_lv->lvid.id[1], sizeof(struct id)); + } + + if (metadata_lv == lv) { + log_error("Can't use same LV for pool data and metadata LV %s.", + display_lvname(metadata_lv)); + return 0; + } + + if (!lv_is_visible(metadata_lv)) { + log_error("Can't convert internal LV %s.", + display_lvname(metadata_lv)); + return 0; + } + + if (lv_is_locked(metadata_lv)) { + log_error("Can't convert locked LV %s.", + display_lvname(metadata_lv)); + return 0; + } + + if (lv_is_mirror(metadata_lv)) { + log_error("Mirror logical volumes cannot be used for pool metadata."); + log_print_unless_silent("Try \"%s\" segment type instead.", SEG_TYPE_NAME_RAID1); + return 0; + } + + /* FIXME Tidy up all these type restrictions. */ + if (lv_is_cache_type(metadata_lv) || + lv_is_thin_type(metadata_lv) || + lv_is_cow(metadata_lv) || lv_is_merging_cow(metadata_lv) || + lv_is_origin(metadata_lv) || lv_is_merging_origin(metadata_lv) || + lv_is_external_origin(metadata_lv) || + lv_is_virtual(metadata_lv)) { + log_error("Pool metadata LV %s is of an unsupported type.", + display_lvname(metadata_lv)); + return 0; + } + } + + /* + * Determine the size of the metadata LV and the chunk size. When an + * existing LV is to be used for metadata, this introduces some + * constraints/defaults. When chunk_size=0 and/or meta_extents=0 are + * passed to the "update params" function, defaults are calculated and + * returned. + */ + + if (arg_is_set(cmd, chunksize_ARG)) { + passed_args |= PASS_ARG_CHUNK_SIZE; + chunk_size = arg_uint_value(cmd, chunksize_ARG, 0); + if (!validate_pool_chunk_size(cmd, pool_segtype, chunk_size)) + return_0; + } else { + /* A default will be chosen by the "update" function. */ + chunk_size = 0; + } + + if (arg_is_set(cmd, poolmetadatasize_ARG)) { + meta_size = arg_uint64_value(cmd, poolmetadatasize_ARG, UINT64_C(0)); + meta_extents = extents_from_size(cmd, meta_size, vg->extent_size); + passed_args |= PASS_ARG_POOL_METADATA_SIZE; + } else if (metadata_lv) { + meta_extents = metadata_lv->le_count; + passed_args |= PASS_ARG_POOL_METADATA_SIZE; + } else { + /* A default will be chosen by the "update" function. */ + meta_extents = 0; + } + + /* Tell the "update" function to ignore these, they are handled below. */ + passed_args |= PASS_ARG_DISCARDS | PASS_ARG_ZERO; + + /* + * Validate and/or choose defaults for meta_extents and chunk_size, + * this involves some complicated calculations. + */ + + if (to_cachepool) { + if (!update_cache_pool_params(pool_segtype, vg, target_attr, + passed_args, lv->le_count, + &meta_extents, + &chunk_calc, + &chunk_size)) + return_0; + } else { + if (!update_thin_pool_params(pool_segtype, vg, target_attr, + passed_args, lv->le_count, + &meta_extents, + &chunk_calc, + &chunk_size, + NULL, NULL)) + return_0; + } + + if ((uint64_t)chunk_size > ((uint64_t)lv->le_count * vg->extent_size)) { + log_error("Pool data LV %s is too small (%s) for specified chunk size (%s).", + display_lvname(lv), + display_size(cmd, (uint64_t)lv->le_count * vg->extent_size), + display_size(cmd, chunk_size)); + return 0; + } + + if (metadata_lv && (meta_extents > metadata_lv->le_count)) { + log_error("Pool metadata LV %s is too small (%u extents) for required metadata (%u extents).", + display_lvname(metadata_lv), metadata_lv->le_count, meta_extents); + return 0; + } + + log_verbose("Pool metadata extents %u chunk_size %u", meta_extents, chunk_size); + + + /* + * Verify that user wants to use these LVs. + */ + + log_warn("WARNING: Converting logical volume %s%s%s to %s pool's data%s %s metadata wiping.", + display_lvname(lv), + metadata_lv ? " and " : "", + metadata_lv ? display_lvname(metadata_lv) : "", + to_cachepool ? "cache" : "thin", + metadata_lv ? " and metadata volumes" : " volume", + zero_metadata ? "with" : "WITHOUT"); + + if (zero_metadata) + log_warn("THIS WILL DESTROY CONTENT OF LOGICAL VOLUME (filesystem etc.)"); + else if (to_cachepool) + log_warn("WARNING: Using mismatched cache pool metadata MAY DESTROY YOUR DATA!"); + + if (!arg_count(cmd, yes_ARG) && + yes_no_prompt("Do you really want to convert %s%s%s? [y/n]: ", + display_lvname(lv), + metadata_lv ? " and " : "", + metadata_lv ? display_lvname(metadata_lv) : "") == 'n') { + log_error("Conversion aborted."); + return 0; + } + + /* + * The internal LV names for pool data/meta LVs. + */ + + if ((dm_snprintf(meta_name, sizeof(meta_name), "%s%s", lv->name, to_cachepool ? "_cmeta" : "_tmeta") < 0) || + (dm_snprintf(data_name, sizeof(data_name), "%s%s", lv->name, to_cachepool ? "_cdata" : "_tdata") < 0)) { + log_error("Failed to create internal lv names, pool name is too long."); + return 0; + } + + /* + * If a new metadata LV needs to be created, collect the settings for + * the new LV and create it. + * + * If an existing LV is used for metadata, deactivate/activate/wipe it. + */ + + if (!metadata_lv) { + uint32_t meta_stripes; + uint32_t meta_stripe_size; + uint32_t meta_readahead; + alloc_policy_t meta_alloc; + unsigned meta_stripes_supplied; + unsigned meta_stripe_size_supplied; + + if (!get_stripe_params(cmd, get_segtype_from_string(cmd, SEG_TYPE_NAME_STRIPED), + &meta_stripes, + &meta_stripe_size, + &meta_stripes_supplied, + &meta_stripe_size_supplied)) + return_0; + + meta_readahead = arg_uint_value(cmd, readahead_ARG, cmd->default_settings.read_ahead); + meta_alloc = (alloc_policy_t) arg_uint_value(cmd, alloc_ARG, ALLOC_INHERIT); + + if (!archive(vg)) + return_0; + + if (!(metadata_lv = alloc_pool_metadata(lv, + meta_name, + meta_readahead, + meta_stripes, + meta_stripe_size, + meta_extents, + meta_alloc, + use_pvh))) + return_0; + } else { + if (!deactivate_lv(cmd, metadata_lv)) { + log_error("Aborting. Failed to deactivate %s.", + display_lvname(metadata_lv)); + return 0; + } + + if (!archive(vg)) + return_0; + + if (zero_metadata) { + metadata_lv->status |= LV_TEMPORARY; + if (!activate_lv_local(cmd, metadata_lv)) { + log_error("Aborting. Failed to activate metadata lv."); + return 0; + } + + if (!wipe_lv(metadata_lv, (struct wipe_params) { .do_zero = 1 })) { + log_error("Aborting. Failed to wipe metadata lv."); + return 0; + } + } + } + + /* + * Deactivate the data LV and metadata LV. + * We are changing target type, so deactivate first. + */ + + if (!deactivate_lv(cmd, metadata_lv)) { + log_error("Aborting. Failed to deactivate metadata lv. " + "Manual intervention required."); + return 0; + } + + if (!deactivate_lv(cmd, lv)) { + log_error("Aborting. Failed to deactivate logical volume %s.", + display_lvname(lv)); + return 0; + } + + /* + * When the LV referenced by the original function arg "lv" + * is renamed, it is then referenced as "data_lv". + * + * pool_name pool name taken from lv arg + * data_name sub lv name, generated + * meta_name sub lv name, generated + * + * pool_lv new lv for pool object, created here + * data_lv sub lv, was lv arg, now renamed + * metadata_lv sub lv, existing or created here + */ + + data_lv = lv; + pool_name = lv->name; /* Use original LV name for pool name */ + + /* + * Rename the original LV arg to the internal data LV naming scheme. + * + * Since we wish to have underlaying devs to match _[ct]data + * rename data LV to match pool LV subtree first, + * also checks for visible LV. + * + * FIXME: any more types prohibited here? + */ + + if (!lv_rename_update(cmd, data_lv, data_name, 0)) + return_0; + + /* + * Create LV structures for the new pool LV object, + * and connect it to the data/meta LVs. + */ + + if (!(pool_lv = lv_create_empty(pool_name, NULL, + (to_cachepool ? CACHE_POOL : THIN_POOL) | VISIBLE_LV | LVM_READ | LVM_WRITE, + ALLOC_INHERIT, vg))) { + log_error("Creation of pool LV failed."); + return 0; + } + + /* Allocate a new pool segment */ + if (!(seg = alloc_lv_segment(pool_segtype, pool_lv, 0, data_lv->le_count, + pool_lv->status, 0, NULL, 1, + data_lv->le_count, 0, 0, 0, NULL))) + return_0; + + /* Add the new segment to the layer LV */ + dm_list_add(&pool_lv->segments, &seg->list); + pool_lv->le_count = data_lv->le_count; + pool_lv->size = data_lv->size; + + if (!attach_pool_data_lv(seg, data_lv)) + return_0; + + /* + * Create a new lock for a thin pool LV. A cache pool LV has no lock. + * Locks are removed from existing LVs that are being converted to + * data and meta LVs (they are unlocked and deleted below.) + */ + if (is_lockd_type(vg->lock_type)) { + if (to_cachepool) { + data_lv->lock_args = NULL; + metadata_lv->lock_args = NULL; + } else { + data_lv->lock_args = NULL; + metadata_lv->lock_args = NULL; + + if (!strcmp(vg->lock_type, "sanlock")) + pool_lv->lock_args = "pending"; + else if (!strcmp(vg->lock_type, "dlm")) + pool_lv->lock_args = "dlm"; + /* The lock_args will be set in vg_write(). */ + } + } + + /* + * Apply settings to the new pool seg, from command line, from + * defaults, sometimes adjusted. + */ + + seg->transaction_id = 0; + seg->chunk_size = chunk_size; + + if (to_cachepool) { + cache_mode_t cache_mode = 0; + const char *policy_name = NULL; + struct dm_config_tree *policy_settings = NULL; + + if (!get_cache_params(cmd, &cache_mode, &policy_name, &policy_settings)) + return_0; + + if (cache_mode && + !cache_set_cache_mode(seg, cache_mode)) + return_0; + + if ((policy_name || policy_settings) && + !cache_set_policy(seg, policy_name, policy_settings)) + return_0; + + if (policy_settings) + dm_config_destroy(policy_settings); + } else { + const char *discards_name; + + if (arg_is_set(cmd, zero_ARG)) + seg->zero_new_blocks = arg_int_value(cmd, zero_ARG, 0); + else + seg->zero_new_blocks = find_config_tree_bool(cmd, allocation_thin_pool_zero_CFG, vg->profile); + + if (arg_is_set(cmd, discards_ARG)) + seg->discards = (thin_discards_t) arg_uint_value(cmd, discards_ARG, THIN_DISCARDS_PASSDOWN); + else { + if (!(discards_name = find_config_tree_str(cmd, allocation_thin_pool_discards_CFG, vg->profile))) + return_0; + if (!set_pool_discards(&seg->discards, discards_name)) + return_0; + } + } + + /* + * Rename deactivated metadata LV to have _tmeta suffix. + * Implicit checks if metadata_lv is visible. + */ + if (pool_metadata_name && + !lv_rename_update(cmd, metadata_lv, meta_name, 0)) + return_0; + + if (!attach_pool_metadata_lv(seg, metadata_lv)) + return_0; + + if (!handle_pool_metadata_spare(vg, + metadata_lv->le_count, + use_pvh, + arg_int_value(cmd, poolmetadataspare_ARG, DEFAULT_POOL_METADATA_SPARE))) + return_0; + + if (!vg_write(vg) || !vg_commit(vg)) + return_0; + + if (seg->zero_new_blocks && + seg->chunk_size >= DEFAULT_THIN_POOL_CHUNK_SIZE_PERFORMANCE * 2) + log_warn("WARNING: Pool zeroing and large %s chunk size slows down provisioning.", + display_size(cmd, seg->chunk_size)); + + if (activate_pool && !lockd_lv(cmd, pool_lv, "ex", LDLV_PERSISTENT)) { + log_error("Failed to lock pool LV %s.", display_lvname(pool_lv)); + goto out; + } + + if (activate_pool && + !activate_lv_excl(cmd, pool_lv)) { + log_error("Failed to activate pool logical volume %s.", + display_lvname(pool_lv)); + /* Deactivate subvolumes */ + if (!deactivate_lv(cmd, seg_lv(seg, 0))) + log_error("Failed to deactivate pool data logical volume %s.", + display_lvname(seg_lv(seg, 0))); + if (!deactivate_lv(cmd, seg->metadata_lv)) + log_error("Failed to deactivate pool metadata logical volume %s.", + display_lvname(seg->metadata_lv)); + goto out; + } + + r = 1; + +out: + backup(vg); + + if (r) + log_print_unless_silent("Converted %s to %s pool.", + display_lvname(lv), + to_cachepool ? "cache" : "thin"); + + /* + * Unlock and free the locks from existing LVs that became pool data + * and meta LVs. + */ + if (lockd_data_name) { + if (!lockd_lv_name(cmd, vg, lockd_data_name, &lockd_data_id, lockd_data_args, "un", LDLV_PERSISTENT)) + log_error("Failed to unlock pool data LV %s/%s", vg->name, lockd_data_name); + lockd_free_lv(cmd, vg, lockd_data_name, &lockd_data_id, lockd_data_args); + } + + if (lockd_meta_name) { + if (!lockd_lv_name(cmd, vg, lockd_meta_name, &lockd_meta_id, lockd_meta_args, "un", LDLV_PERSISTENT)) + log_error("Failed to unlock pool metadata LV %s/%s", vg->name, lockd_meta_name); + lockd_free_lv(cmd, vg, lockd_meta_name, &lockd_meta_id, lockd_meta_args); + } + + return r; +#if 0 +revert_new_lv: + /* TBD */ + if (!pool_metadata_lv_name) { + if (!deactivate_lv(cmd, metadata_lv)) { + log_error("Failed to deactivate metadata lv."); + return 0; + } + if (!lv_remove(metadata_lv) || !vg_write(vg) || !vg_commit(vg)) + log_error("Manual intervention may be required to remove " + "abandoned LV(s) before retrying."); + else + backup(vg); + } + + return 0; +#endif +} + +static int _lvconvert_to_cache_vol(struct cmd_context *cmd, + struct logical_volume *lv, + struct logical_volume *cachepool_lv) +{ + struct logical_volume *cache_lv; + cache_mode_t cache_mode = 0; + const char *policy_name = NULL; + struct dm_config_tree *policy_settings = NULL; + + if (!validate_lv_cache_create_pool(cachepool_lv)) + return_0; + + if (!get_cache_params(cmd, &cache_mode, &policy_name, &policy_settings)) + return_0; + + if (!archive(lv->vg)) + return_0; + + if (!(cache_lv = lv_cache_create(cachepool_lv, lv))) + return_0; + + if (!cache_set_cache_mode(first_seg(cache_lv), cache_mode)) + return_0; + + if (!cache_set_policy(first_seg(cache_lv), policy_name, policy_settings)) + return_0; + + if (policy_settings) + dm_config_destroy(policy_settings); + + cache_check_for_warns(first_seg(cache_lv)); + + if (!lv_update_and_reload(cache_lv)) + return_0; + + log_print_unless_silent("Logical volume %s is now cached.", + display_lvname(cache_lv)); + + return 1; +} + +static int _lvconvert_to_pool_single(struct cmd_context *cmd, + struct logical_volume *lv, + struct processing_handle *handle) +{ + struct dm_list *use_pvh = NULL; + int to_thinpool = 0; + int to_cachepool = 0; + + switch (cmd->command->command_line_enum) { + case lvconvert_to_thinpool_CMD: + to_thinpool = 1; + break; + case lvconvert_to_cachepool_CMD: + to_cachepool = 1; + break; + default: + log_error(INTERNAL_ERROR "Invalid lvconvert pool command"); + return 0; + }; + + if (cmd->position_argc > 1) { + /* First pos arg is required LV, remaining are optional PVs. */ + if (!(use_pvh = create_pv_list(cmd->mem, lv->vg, cmd->position_argc - 1, cmd->position_argv + 1, 0))) + return_ECMD_FAILED; + } else + use_pvh = &lv->vg->pvs; + + if (!_lvconvert_to_pool(cmd, lv, to_thinpool, to_cachepool, use_pvh)) + return_ECMD_FAILED; + + return ECMD_PROCESSED; +} + +/* + * The LV position arg is used as thinpool/cachepool data LV. + */ + +int lvconvert_to_pool_cmd(struct cmd_context *cmd, int argc, char **argv) +{ + return process_each_lv(cmd, 1, cmd->position_argv, NULL, NULL, READ_FOR_UPDATE, + NULL, NULL, &_lvconvert_to_pool_single); +} + +/* + * Reformats non-standard command form into standard command form. + * + * In the command variants with no position LV arg, the LV arg is taken from + * the --thinpool/--cachepool arg, and the position args are modified to match + * the standard command form. + */ + +int lvconvert_to_pool_noarg_cmd(struct cmd_context *cmd, int argc, char **argv) +{ + struct command *new_command; + char *pool_data_name; + int i, p; + + switch (cmd->command->command_line_enum) { + case lvconvert_to_thinpool_noarg_CMD: + pool_data_name = (char *)arg_str_value(cmd, thinpool_ARG, NULL); + new_command = get_command(lvconvert_to_thinpool_CMD); + break; + case lvconvert_to_cachepool_noarg_CMD: + pool_data_name = (char *)arg_str_value(cmd, cachepool_ARG, NULL); + new_command = get_command(lvconvert_to_cachepool_CMD); + break; + default: + log_error(INTERNAL_ERROR "Unknown pool conversion."); + return 0; + }; + + log_debug("Changing command line id %s %d to standard form %s %d", + cmd->command->command_line_id, cmd->command->command_line_enum, + new_command->command_line_id, new_command->command_line_enum); + + /* Make the LV the first position arg. */ + + p = cmd->position_argc; + for (i = 0; i < cmd->position_argc; i++) + cmd->position_argv[p] = cmd->position_argv[p-1]; + + cmd->position_argv[0] = pool_data_name; + cmd->position_argc++; + cmd->command = new_command; + + return lvconvert_to_pool_cmd(cmd, argc, argv); +} + +static int _lvconvert_to_cache_vol_single(struct cmd_context *cmd, + struct logical_volume *lv, + struct processing_handle *handle) +{ + struct volume_group *vg = lv->vg; + struct logical_volume *cachepool_lv; + const char *cachepool_name; + uint32_t chunk_size = 0; + + if (!(cachepool_name = arg_str_value(cmd, cachepool_ARG, NULL))) + goto_out; + + if (!validate_lvname_param(cmd, &vg->name, &cachepool_name)) + goto_out; + + if (!(cachepool_lv = find_lv(vg, cachepool_name))) { + log_error("Cache pool %s not found.", cachepool_name); + goto out; + } + + /* + * If cachepool_lv is not yet a cache pool, convert it to one. + * If using an existing cache pool, wipe it. + */ + + if (!lv_is_cache_pool(cachepool_lv)) { + int lvt_enum = get_lvt_enum(cachepool_lv); + struct lv_types *lvtype = get_lv_type(lvt_enum); + + if (lvt_enum != striped_LVT && lvt_enum != linear_LVT && lvt_enum != raid_LVT) { + log_error("LV %s with type %s cannot be converted to a cache pool.", + display_lvname(cachepool_lv), lvtype ? lvtype->name : "unknown"); + goto out; + } + + if (!_lvconvert_to_pool(cmd, cachepool_lv, 0, 1, &vg->pvs)) { + log_error("LV %s could not be converted to a cache pool.", + display_lvname(cachepool_lv)); + goto out; + } + + if (!(cachepool_lv = find_lv(vg, cachepool_name))) { + log_error("LV %s cannot be found.", display_lvname(cachepool_lv)); + goto out; + } + + if (!lv_is_cache_pool(cachepool_lv)) { + log_error("LV %s is not a cache pool.", display_lvname(cachepool_lv)); + goto out; + } + } else { + if (!dm_list_empty(&cachepool_lv->segs_using_this_lv)) { + log_error("Cache pool %s is already in use.", cachepool_name); + goto out; + } + + if (arg_is_set(cmd, chunksize_ARG)) + chunk_size = arg_uint_value(cmd, chunksize_ARG, 0); + if (!chunk_size) + chunk_size = first_seg(cachepool_lv)->chunk_size; + + /* FIXME: why is chunk_size read and checked if it's not used? */ + + if (!validate_lv_cache_chunk_size(cachepool_lv, chunk_size)) + goto_out; + + /* Note: requires rather deep know-how to skip zeroing */ + if (!arg_is_set(cmd, zero_ARG)) { + if (!arg_is_set(cmd, yes_ARG) && + yes_no_prompt("Do you want wipe existing metadata of cache pool %s? [y/n]: ", + display_lvname(cachepool_lv)) == 'n') { + log_error("Conversion aborted."); + log_error("To preserve cache metadata add option \"--zero n\"."); + log_warn("WARNING: Reusing mismatched cache pool metadata MAY DESTROY YOUR DATA!"); + goto out; + } + /* Wiping confirmed, go ahead */ + if (!wipe_cache_pool(cachepool_lv)) + goto_out; + } else if (arg_int_value(cmd, zero_ARG, 0)) { + if (!wipe_cache_pool(cachepool_lv)) + goto_out; + } else { + log_warn("WARNING: Reusing cache pool metadata %s for volume caching.", + display_lvname(cachepool_lv)); + } + + } + + /* When the lv arg is a thinpool, redirect command to data sub lv. */ + + if (lv_is_thin_pool(lv)) { + lv = seg_lv(first_seg(lv), 0); + log_verbose("Redirecting operation to data sub LV %s.", display_lvname(lv)); + } + + /* Convert lv to cache vol using cachepool_lv. */ + + if (!_lvconvert_to_cache_vol(cmd, lv, cachepool_lv)) + goto_out; + + return ECMD_PROCESSED; + + out: + return ECMD_FAILED; +} + +int lvconvert_to_cache_vol_cmd(struct cmd_context *cmd, int argc, char **argv) +{ + return process_each_lv(cmd, 1, cmd->position_argv, NULL, NULL, READ_FOR_UPDATE, + NULL, NULL, &_lvconvert_to_cache_vol_single); +} + +static int _lvconvert_to_thin_with_external_single(struct cmd_context *cmd, + struct logical_volume *lv, + struct processing_handle *handle) +{ + struct volume_group *vg = lv->vg; + struct logical_volume *thinpool_lv; + const char *thinpool_name; + + if (!(thinpool_name = arg_str_value(cmd, thinpool_ARG, NULL))) + goto_out; + + if (!validate_lvname_param(cmd, &vg->name, &thinpool_name)) + goto_out; + + if (!(thinpool_lv = find_lv(vg, thinpool_name))) { + log_error("Thin pool %s not found.", thinpool_name); + goto out; + } + + /* If thinpool_lv is not yet a thin pool, convert it to one. */ + + if (!lv_is_thin_pool(thinpool_lv)) { + int lvt_enum = get_lvt_enum(thinpool_lv); + struct lv_types *lvtype = get_lv_type(lvt_enum); + + if (lvt_enum != striped_LVT && lvt_enum != linear_LVT && lvt_enum != raid_LVT) { + log_error("LV %s with type %s cannot be converted to a thin pool.", + display_lvname(thinpool_lv), lvtype ? lvtype->name : "unknown"); + goto out; + } + + if (!_lvconvert_to_pool(cmd, thinpool_lv, 1, 0, &vg->pvs)) { + log_error("LV %s could not be converted to a thin pool.", + display_lvname(thinpool_lv)); + goto out; + } + + if (!(thinpool_lv = find_lv(vg, thinpool_name))) { + log_error("LV %s cannot be found.", display_lvname(thinpool_lv)); + goto out; + } + + if (!lv_is_thin_pool(thinpool_lv)) { + log_error("LV %s is not a thin pool.", display_lvname(thinpool_lv)); + goto out; + } + } + + /* Convert lv to thin with external origin using thinpool_lv. */ + + if (!_lvconvert_to_thin_with_external(cmd, lv, thinpool_lv)) + goto_out; + + return ECMD_PROCESSED; + + out: + return ECMD_FAILED; +} + +int lvconvert_to_thin_with_external_cmd(struct cmd_context *cmd, int argc, char **argv) +{ + return process_each_lv(cmd, 1, cmd->position_argv, NULL, NULL, READ_FOR_UPDATE, + NULL, NULL, &_lvconvert_to_thin_with_external_single); +} + +static int _lvconvert_swap_pool_metadata_single(struct cmd_context *cmd, + struct logical_volume *lv, + struct processing_handle *handle) +{ + struct volume_group *vg = lv->vg; + struct logical_volume *metadata_lv; + const char *metadata_name; + + if (!(metadata_name = arg_str_value(cmd, poolmetadata_ARG, NULL))) + goto_out; + + if (!validate_lvname_param(cmd, &vg->name, &metadata_name)) + goto_out; + + if (!(metadata_lv = find_lv(vg, metadata_name))) { + log_error("Metadata LV %s not found.", metadata_name); + goto out; + } + + if (metadata_lv == lv) { + log_error("Can't use same LV for pool data and metadata LV %s.", + display_lvname(metadata_lv)); + goto out; + } + + if (!_lvconvert_swap_pool_metadata(cmd, lv, metadata_lv)) + goto_out; + + return ECMD_PROCESSED; + + out: + return ECMD_FAILED; +} + +int lvconvert_swap_pool_metadata_cmd(struct cmd_context *cmd, int argc, char **argv) +{ + return process_each_lv(cmd, 1, cmd->position_argv, NULL, NULL, READ_FOR_UPDATE, + NULL, NULL, &_lvconvert_swap_pool_metadata_single); +} + +#if 0 +int lvconvert_swap_pool_metadata_noarg_cmd(struct cmd_context *cmd, int argc, char **argv) +{ + struct command *new_command; + char *pool_name; + + switch (cmd->command->command_line_enum) { + case lvconvert_swap_thinpool_metadata_CMD: + pool_name = (char *)arg_str_value(cmd, thinpool_ARG, NULL); + break; + case lvconvert_swap_cachepool_metadata_CMD: + pool_name = (char *)arg_str_value(cmd, cachepool_ARG, NULL); + break; + default: + log_error(INTERNAL_ERROR "Unknown pool conversion."); + return 0; + }; + + new_command = get_command(lvconvert_swap_pool_metadata_CMD); + + log_debug("Changing command line id %s %d to standard form %s %d", + cmd->command->command_line_id, cmd->command->command_line_enum, + new_command->command_line_id, new_command->command_line_enum); + + /* Make the LV the first position arg. */ + + cmd->position_argv[0] = pool_name; + cmd->position_argc++; + cmd->command = new_command; + + return lvconvert_swap_pool_metadata_cmd(cmd, argc, argv); +} +#endif + +int lvconvert_merge_thin_single(struct cmd_context *cmd, + struct logical_volume *lv, + struct processing_handle *handle) +{ + if (!_lvconvert_merge_thin_snapshot(cmd, lv)) + return ECMD_FAILED; + + return ECMD_PROCESSED; +} + +int lvconvert_merge_thin_cmd(struct cmd_context *cmd, int argc, char **argv) +{ + return process_each_lv(cmd, cmd->position_argc, cmd->position_argv, NULL, NULL, READ_FOR_UPDATE, + NULL, NULL, &lvconvert_merge_thin_single); +} + +static int _lvconvert_split_cachepool_single(struct cmd_context *cmd, + struct logical_volume *lv, + struct processing_handle *handle) +{ + struct logical_volume *cache_lv = NULL; + struct logical_volume *cachepool_lv = NULL; + struct lv_segment *seg; + int ret; + + if (lv_is_cache(lv)) { + cache_lv = lv; + cachepool_lv = first_seg(cache_lv)->pool_lv; + + } else if (lv_is_cache_pool(lv)) { + cachepool_lv = lv; + + if ((dm_list_size(&cachepool_lv->segs_using_this_lv) == 1) && + (seg = get_only_segment_using_this_lv(cachepool_lv)) && + seg_is_cache(seg)) + cache_lv = seg->lv; + + } else if (lv_is_thin_pool(lv)) { + cache_lv = seg_lv(first_seg(lv), 0); /* cached _tdata */ + cachepool_lv = first_seg(cache_lv)->pool_lv; + } + + if (!cache_lv) { + log_error("Cannot find cache LV from %s.", display_lvname(lv)); + return ECMD_FAILED; + } + + if (!cachepool_lv) { + log_error("Cannot find cache pool LV from %s.", display_lvname(lv)); + return ECMD_FAILED; + } + + switch (cmd->command->command_line_enum) { + case lvconvert_split_and_keep_cachepool_CMD: + ret = _lvconvert_split_and_keep_cachepool(cmd, cache_lv, cachepool_lv); + break; + + case lvconvert_split_and_remove_cachepool_CMD: + ret = _lvconvert_split_and_remove_cachepool(cmd, cache_lv, cachepool_lv); + break; + default: + log_error(INTERNAL_ERROR "Unknown cache pool split."); + ret = 0; + } + + if (!ret) + return ECMD_FAILED; + + return ECMD_PROCESSED; +} + +int lvconvert_split_cachepool_cmd(struct cmd_context *cmd, int argc, char **argv) +{ + if (cmd->command->command_line_enum == lvconvert_split_and_remove_cachepool_CMD) { + cmd->handles_missing_pvs = 1; + cmd->partial_activation = 1; + } + + return process_each_lv(cmd, 1, cmd->position_argv, NULL, NULL, READ_FOR_UPDATE, + NULL, NULL, &_lvconvert_split_cachepool_single); +} + diff --git a/tools/lvconvert_snapshot.c b/tools/lvconvert_snapshot.c new file mode 100644 index 000000000..51ac9dcf8 --- /dev/null +++ b/tools/lvconvert_snapshot.c @@ -0,0 +1,423 @@ +/* + * Copyright (C) 2005-2016 Red Hat, Inc. All rights reserved. + * + * This file is part of LVM2. + * + * This copyrighted material is made available to anyone wishing to use, + * modify, copy, or redistribute it subject to the terms and conditions + * of the GNU Lesser General Public License v.2.1. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "tools.h" + +#include "polldaemon.h" +#include "lv_alloc.h" +#include "lvconvert_poll.h" +#include "command-lines-count.h" + +static int _lvconvert_snapshot(struct cmd_context *cmd, + struct logical_volume *lv, + const char *origin_name) +{ + struct logical_volume *org; + const char *snap_name = display_lvname(lv); + uint32_t chunk_size; + int zero; + + if (!(org = find_lv(lv->vg, origin_name))) { + log_error("Couldn't find origin volume %s in Volume group %s.", + origin_name, lv->vg->name); + return 0; + } + + if (org == lv) { + log_error("Unable to use %s as both snapshot and origin.", snap_name); + return 0; + } + + chunk_size = arg_uint_value(cmd, chunksize_ARG, 8); + if (chunk_size < 8 || chunk_size > 1024 || !is_power_of_2(chunk_size)) { + log_error("Chunk size must be a power of 2 in the range 4K to 512K."); + return 0; + } + log_verbose("Setting chunk size to %s.", display_size(cmd, chunk_size)); + + if (!cow_has_min_chunks(lv->vg, lv->le_count, chunk_size)) + return_0; + + /* + * check_lv_rules() checks cannot be done via command definition + * rules because this LV is not processed by process_each_lv. + */ + if (lv_is_locked(org) || lv_is_pvmove(org)) { + log_error("Unable to use LV %s as snapshot origin: LV is %s.", + display_lvname(lv), lv_is_locked(org) ? "locked" : "pvmove"); + return 0; + } + + /* + * check_lv_types() checks cannot be done via command definition + * LV_foo specification because this LV is not processed by process_each_lv. + */ + if (lv_is_cache_type(org) || + lv_is_thin_type(org) || + lv_is_mirrored(org) || + lv_is_cow(org)) { + log_error("Unable to use LV %s as snapshot origin: invald LV type.", + display_lvname(lv)); + return 0; + } + + log_warn("WARNING: Converting logical volume %s to snapshot exception store.", + snap_name); + log_warn("THIS WILL DESTROY CONTENT OF LOGICAL VOLUME (filesystem etc.)"); + + if (!arg_count(cmd, yes_ARG) && + yes_no_prompt("Do you really want to convert %s? [y/n]: ", + snap_name) == 'n') { + log_error("Conversion aborted."); + return 0; + } + + if (!deactivate_lv(cmd, lv)) { + log_error("Couldn't deactivate logical volume %s.", snap_name); + return 0; + } + + if (first_seg(lv)->segtype->flags & SEG_CANNOT_BE_ZEROED) + zero = 0; + else + zero = arg_int_value(cmd, zero_ARG, 1); + + if (!zero || !(lv->status & LVM_WRITE)) + log_warn("WARNING: %s not zeroed.", snap_name); + else { + lv->status |= LV_TEMPORARY; + if (!activate_lv_local(cmd, lv) || + !wipe_lv(lv, (struct wipe_params) { .do_zero = 1 })) { + log_error("Aborting. Failed to wipe snapshot exception store."); + return 0; + } + lv->status &= ~LV_TEMPORARY; + /* Deactivates cleared metadata LV */ + if (!deactivate_lv_local(lv->vg->cmd, lv)) { + log_error("Failed to deactivate zeroed snapshot exception store."); + return 0; + } + } + + if (!archive(lv->vg)) + return_0; + + if (!vg_add_snapshot(org, lv, NULL, org->le_count, chunk_size)) { + log_error("Couldn't create snapshot."); + return 0; + } + + /* store vg on disk(s) */ + if (!lv_update_and_reload(org)) + return_0; + + log_print_unless_silent("Logical volume %s converted to snapshot.", snap_name); + + return 1; +} + +static int _lvconvert_merge_old_snapshot(struct cmd_context *cmd, + struct logical_volume *lv, + struct logical_volume **lv_to_poll) +{ + int merge_on_activate = 0; + struct logical_volume *origin = origin_from_cow(lv); + struct lv_segment *snap_seg = find_snapshot(lv); + struct lvinfo info; + dm_percent_t snap_percent; + + if (lv_is_external_origin(origin_from_cow(lv))) { + log_error("Cannot merge snapshot \"%s\" into " + "the read-only external origin \"%s\".", + lv->name, origin_from_cow(lv)->name); + return 0; + } + + /* FIXME: test when snapshot is remotely active */ + if (lv_info(cmd, lv, 0, &info, 1, 0) + && info.exists && info.live_table && + (!lv_snapshot_percent(lv, &snap_percent) || + snap_percent == DM_PERCENT_INVALID)) { + log_error("Unable to merge invalidated snapshot LV \"%s\".", + lv->name); + return 0; + } + + if (snap_seg->segtype->ops->target_present && + !snap_seg->segtype->ops->target_present(cmd, snap_seg, NULL)) { + log_error("Can't initialize snapshot merge. " + "Missing support in kernel?"); + return 0; + } + + if (!archive(lv->vg)) + return_0; + + /* + * Prevent merge with open device(s) as it would likely lead + * to application/filesystem failure. Merge on origin's next + * activation if either the origin or snapshot LV are currently + * open. + * + * FIXME testing open_count is racey; snapshot-merge target's + * constructor and DM should prevent appropriate devices from + * being open. + */ + if (lv_is_active_locally(origin)) { + if (!lv_check_not_in_use(origin, 0)) { + log_print_unless_silent("Can't merge until origin volume is closed."); + merge_on_activate = 1; + } else if (!lv_check_not_in_use(lv, 0)) { + log_print_unless_silent("Can't merge until snapshot is closed."); + merge_on_activate = 1; + } + } else if (vg_is_clustered(origin->vg) && lv_is_active(origin)) { + /* When it's active somewhere else */ + log_print_unless_silent("Can't check whether remotely active snapshot is open."); + merge_on_activate = 1; + } + + init_snapshot_merge(snap_seg, origin); + + if (merge_on_activate) { + /* Store and commit vg but skip starting the merge */ + if (!vg_write(lv->vg) || !vg_commit(lv->vg)) + return_0; + backup(lv->vg); + } else { + /* Perform merge */ + if (!lv_update_and_reload(origin)) + return_0; + + *lv_to_poll = origin; + } + + if (merge_on_activate) + log_print_unless_silent("Merging of snapshot %s will occur on " + "next activation of %s.", + display_lvname(lv), display_lvname(origin)); + else + log_print_unless_silent("Merging of volume %s started.", + display_lvname(lv)); + + return 1; +} + +static int _lvconvert_splitsnapshot(struct cmd_context *cmd, struct logical_volume *cow) +{ + struct volume_group *vg = cow->vg; + const char *cow_name = display_lvname(cow); + + if (lv_is_virtual_origin(origin_from_cow(cow))) { + log_error("Unable to split off snapshot %s with virtual origin.", cow_name); + return 0; + } + + if (!(vg->fid->fmt->features & FMT_MDAS)) { + log_error("Unable to split off snapshot %s using old LVM1-style metadata.", cow_name); + return 0; + } + + if (is_lockd_type(vg->lock_type)) { + /* FIXME: we need to create a lock for the new LV. */ + log_error("Unable to split snapshots in VG with lock_type %s.", vg->lock_type); + return 0; + } + + if (lv_is_active_locally(cow)) { + if (!lv_check_not_in_use(cow, 1)) + return_0; + + if ((arg_count(cmd, force_ARG) == PROMPT) && + !arg_count(cmd, yes_ARG) && + lv_is_visible(cow) && + lv_is_active(cow)) { + if (yes_no_prompt("Do you really want to split off active " + "logical volume %s? [y/n]: ", cow_name) == 'n') { + log_error("Logical volume %s not split.", cow_name); + return 0; + } + } + } + + if (!archive(vg)) + return_0; + + log_verbose("Splitting snapshot %s from its origin.", cow_name); + + if (!vg_remove_snapshot(cow)) + return_0; + + backup(vg); + + log_print_unless_silent("Logical Volume %s split from its origin.", cow_name); + + return 1; +} + +/* + * Merge a COW snapshot LV into its origin. + */ +int lvconvert_merge_snapshot_single(struct cmd_context *cmd, + struct logical_volume *lv, + struct processing_handle *handle) +{ + struct lvconvert_result *lr = (struct lvconvert_result *) handle->custom_handle; + struct logical_volume *lv_to_poll = NULL; + struct convert_poll_id_list *idl; + + if (!_lvconvert_merge_old_snapshot(cmd, lv, &lv_to_poll)) + return_ECMD_FAILED; + + if (lv_to_poll) { + if (!(idl = convert_poll_id_list_create(cmd, lv_to_poll))) + return_ECMD_FAILED; + dm_list_add(&lr->poll_idls, &idl->list); + lr->need_polling = 1; + } + + return ECMD_PROCESSED; +} + +static int _lvconvert_merge_snapshot_check(struct cmd_context *cmd, struct logical_volume *lv, + struct processing_handle *handle, + int lv_is_named_arg) +{ + if (!lv_is_visible(lv)) { + log_error("Operation not permitted (%s %d) on hidden LV %s.", + cmd->command->command_line_id, cmd->command->command_line_enum, + display_lvname(lv)); + return 0; + } + + return 1; +} + +int lvconvert_merge_snapshot_cmd(struct cmd_context *cmd, int argc, char **argv) +{ + struct processing_handle *handle; + struct lvconvert_result lr = { 0 }; + struct convert_poll_id_list *idl; + int ret, poll_ret; + + dm_list_init(&lr.poll_idls); + + if (!(handle = init_processing_handle(cmd, NULL))) { + log_error("Failed to initialize processing handle."); + return ECMD_FAILED; + } + + handle->custom_handle = &lr; + + ret = process_each_lv(cmd, cmd->position_argc, cmd->position_argv, NULL, NULL, READ_FOR_UPDATE, + handle, &_lvconvert_merge_snapshot_check, &lvconvert_merge_snapshot_single); + + if (lr.need_polling) { + dm_list_iterate_items(idl, &lr.poll_idls) { + poll_ret = lvconvert_poll_by_id(cmd, idl->id, + arg_is_set(cmd, background_ARG), 1, 0); + if (poll_ret > ret) + ret = poll_ret; + } + } + + destroy_processing_handle(cmd, handle); + + return ret; +} + +/* + * Separate a COW snapshot from its origin. + * + * lvconvert --splitsnapshot LV_snapshot + * lvconvert_split_cow_snapshot + */ + +static int _lvconvert_split_snapshot_single(struct cmd_context *cmd, + struct logical_volume *lv, + struct processing_handle *handle) +{ + if (!_lvconvert_splitsnapshot(cmd, lv)) + return_ECMD_FAILED; + + return ECMD_PROCESSED; +} + +int lvconvert_split_snapshot_cmd(struct cmd_context *cmd, int argc, char **argv) +{ + return process_each_lv(cmd, 1, cmd->position_argv, NULL, NULL, READ_FOR_UPDATE, + NULL, &lvconvert_generic_check, &_lvconvert_split_snapshot_single); +} + +/* + * Combine two LVs that were once an origin/cow pair of LVs, were then + * separated with --splitsnapshot, and now with this command are combined again + * into the origin/cow pair. + * + * This is an obscure command that has little to no real uses. + * + * The command has unusual handling of position args. The first position arg + * will become the origin LV, and is not processed by process_each_lv. The + * second position arg will become the cow LV and is processed by + * process_each_lv. + * + * The single function can grab the origin LV from position_argv[0]. + * + * begin with an ordinary LV foo: + * lvcreate -n foo -L 1 vg + * + * create a cow snapshot of foo named foosnap: + * lvcreate -s -L 1 -n foosnap vg/foo + * + * now, foo is an "origin LV" and foosnap is a "cow LV" + * (foosnap matches LV_snapshot aka lv_is_cow) + * + * split the two LVs apart: + * lvconvert --splitsnapshot vg/foosnap + * + * now, foo is *not* an origin LV and foosnap is *not* a cow LV + * (foosnap does not match LV_snapshot) + * + * now, combine the two LVs again: + * lvconvert --snapshot vg/foo vg/foosnap + * + * after this, foosnap will match LV_snapshot again. + * + * FIXME: when splitsnapshot is run, the previous cow LV should be + * flagged in the metadata somehow, and then that flag should be + * required here. As it is now, the first and second args + * (origin and cow) can be swapped and nothing catches it. + */ + +static int _lvconvert_combine_split_snapshot_single(struct cmd_context *cmd, + struct logical_volume *lv, + struct processing_handle *handle) +{ + const char *origin_name = cmd->position_argv[0]; + + /* If origin_name includes VG name, the VG name is removed. */ + if (!validate_lvname_param(cmd, &lv->vg->name, &origin_name)) + return_ECMD_FAILED; + + if (!_lvconvert_snapshot(cmd, lv, origin_name)) + return_ECMD_FAILED; + + return ECMD_PROCESSED; +} + +int lvconvert_combine_split_snapshot_cmd(struct cmd_context *cmd, int argc, char **argv) +{ + return process_each_lv(cmd, 1, cmd->position_argv + 1, NULL, NULL, READ_FOR_UPDATE, + NULL, &lvconvert_generic_check, &_lvconvert_combine_split_snapshot_single); +} diff --git a/tools/toollib.c b/tools/toollib.c index aa821fbfb..cbd497aa1 100644 --- a/tools/toollib.c +++ b/tools/toollib.c @@ -5746,3 +5746,40 @@ out: return 0; } +struct dm_list *failed_pv_list(struct volume_group *vg) +{ + struct dm_list *failed_pvs; + struct pv_list *pvl, *new_pvl; + + if (!(failed_pvs = dm_pool_alloc(vg->vgmem, sizeof(*failed_pvs)))) { + log_error("Allocation of list of failed_pvs failed."); + return NULL; + } + + dm_list_init(failed_pvs); + + dm_list_iterate_items(pvl, &vg->pvs) { + if (!is_missing_pv(pvl->pv)) + continue; + + /* + * Finally, --repair will remove empty PVs. + * But we only want remove these which are output of repair, + * Do not count these which are already empty here. + * FIXME: code should traverse PV in LV not in whole VG. + * FIXME: layer violation? should it depend on vgreduce --removemising? + */ + if (pvl->pv->pe_alloc_count == 0) + continue; + + if (!(new_pvl = dm_pool_alloc(vg->vgmem, sizeof(*new_pvl)))) { + log_error("Allocation of failed_pvs list entry failed."); + return NULL; + } + new_pvl->pv = pvl->pv; + dm_list_add(failed_pvs, &new_pvl->list); + } + + return failed_pvs; +} + diff --git a/tools/toollib.h b/tools/toollib.h index 504721e86..a35cfcb5d 100644 --- a/tools/toollib.h +++ b/tools/toollib.h @@ -242,4 +242,6 @@ int lvremove_single(struct cmd_context *cmd, struct logical_volume *lv, int get_lvt_enum(struct logical_volume *lv); +struct dm_list *failed_pv_list(struct volume_group *vg); + #endif diff --git a/tools/tools.h b/tools/tools.h index e165a4dda..7342fcb08 100644 --- a/tools/tools.h +++ b/tools/tools.h @@ -231,6 +231,7 @@ int pvmove_poll(struct cmd_context *cmd, const char *pv_name, const char *uuid, const char *vg_name, const char *lv_name, unsigned background); int lvconvert_poll(struct cmd_context *cmd, struct logical_volume *lv, unsigned background); + int mirror_remove_missing(struct cmd_context *cmd, struct logical_volume *lv, int force); @@ -274,4 +275,27 @@ int lvconvert_merge_mirror_images_cmd(struct cmd_context *cmd, int argc, char ** int lvconvert_merge_cmd(struct cmd_context *cmd, int argc, char **argv); +int lvconvert_generic_check(struct cmd_context *cmd, struct logical_volume *lv, + struct processing_handle *handle, + int lv_is_named_arg); + + +int lvconvert_repair_pvs_mirror(struct cmd_context *cmd, struct logical_volume *lv, + struct processing_handle *handle, + struct dm_list *use_pvh); +int lvconvert_repair_pvs_raid(struct cmd_context *cmd, struct logical_volume *lv, + struct processing_handle *handle, + struct dm_list *use_pvh); +int lvconvert_repair_thinpool(struct cmd_context *cmd, struct logical_volume *lv, + struct processing_handle *handle); +int lvconvert_merge_snapshot_single(struct cmd_context *cmd, + struct logical_volume *lv, + struct processing_handle *handle); +int lvconvert_merge_thin_single(struct cmd_context *cmd, + struct logical_volume *lv, + struct processing_handle *handle); +int lvconvert_merge_mirror_images_single(struct cmd_context *cmd, + struct logical_volume *lv, + struct processing_handle *handle); + #endif -- cgit v1.2.1