2 * Copyright (C) 2005-2007 Red Hat, Inc. All rights reserved.
4 * This file is part of LVM2.
6 * This copyrighted material is made available to anyone wishing to use,
7 * modify, copy, or redistribute it subject to the terms and conditions
8 * of the GNU Lesser General Public License v.2.1.
10 * You should have received a copy of the GNU Lesser General Public License
11 * along with this program; if not, write to the Free Software Foundation,
12 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 #include "polldaemon.h"
20 struct lvconvert_params {
27 const char *lv_split_name;
28 const char *lv_name_full;
38 uint32_t keep_mimages;
42 struct segment_type *segtype;
49 struct dm_list *failed_pvs;
51 struct logical_volume *lv_to_poll;
54 static int _lvconvert_name_params(struct lvconvert_params *lp,
55 struct cmd_context *cmd,
56 int *pargc, char ***pargv)
59 const char *vg_name = NULL;
66 log_error("Please specify a logical volume to act as "
67 "the snapshot origin.");
71 lp->origin = *pargv[0];
72 (*pargv)++, (*pargc)--;
73 if (!(lp->vg_name = extract_vgname(cmd, lp->origin))) {
74 log_error("The origin name should include the "
79 /* Strip the volume group from the origin */
80 if ((ptr = strrchr(lp->origin, (int) '/')))
85 log_error("Please provide logical volume path");
89 lp->lv_name = lp->lv_name_full = (*pargv)[0];
90 (*pargv)++, (*pargc)--;
92 if (strchr(lp->lv_name_full, '/') &&
93 (vg_name = extract_vgname(cmd, lp->lv_name_full)) &&
94 lp->vg_name && strcmp(vg_name, lp->vg_name)) {
95 log_error("Please use a single volume group name "
96 "(\"%s\" or \"%s\")", vg_name, lp->vg_name);
101 lp->vg_name = vg_name;
103 if (!validate_name(lp->vg_name)) {
104 log_error("Please provide a valid volume group name");
108 if ((ptr = strrchr(lp->lv_name_full, '/')))
109 lp->lv_name = ptr + 1;
111 if (!apply_lvname_restrictions(lp->lv_name))
114 if (*pargc && lp->snapshot) {
115 log_error("Too many arguments provided for snapshots");
122 static int _read_params(struct lvconvert_params *lp, struct cmd_context *cmd,
123 int argc, char **argv)
126 int pagesize = lvm_getpagesize();
128 memset(lp, 0, sizeof(*lp));
130 if ((arg_count(cmd, snapshot_ARG) || arg_count(cmd, merge_ARG)) &&
131 (arg_count(cmd, mirrorlog_ARG) || arg_count(cmd, mirrors_ARG) ||
132 arg_count(cmd, repair_ARG))) {
133 log_error("--snapshot or --merge argument cannot be mixed "
134 "with --mirrors, --repair or --log");
138 if (!arg_count(cmd, background_ARG))
139 lp->wait_completion = 1;
141 if (arg_count(cmd, snapshot_ARG))
144 if (arg_count(cmd, snapshot_ARG) && arg_count(cmd, merge_ARG)) {
145 log_error("--snapshot and --merge are mutually exclusive");
149 if (arg_count(cmd, splitmirrors_ARG) && arg_count(cmd, mirrors_ARG)) {
150 log_error("--mirrors and --splitmirrors are "
151 "mutually exclusive");
156 * The '--splitmirrors n' argument is equivalent to '--mirrors -n'
157 * (note the minus sign), except that it signifies the additional
158 * intent to keep the mimage that is detached, rather than
161 if (arg_count(cmd, splitmirrors_ARG)) {
162 if (!arg_count(cmd, name_ARG)) {
163 log_error("Please name the new logical volume using '--name'");
167 lp->lv_split_name = arg_value(cmd, name_ARG);
168 if (!apply_lvname_restrictions(lp->lv_split_name))
171 lp->keep_mimages = 1;
172 if (arg_sign_value(cmd, mirrors_ARG, 0) == SIGN_MINUS) {
173 log_error("Argument to --splitmirrors"
174 " cannot be negative");
177 lp->mirrors = arg_uint_value(cmd, splitmirrors_ARG, 0);
178 lp->mirrors_sign = SIGN_MINUS;
179 } else if (arg_count(cmd, name_ARG)) {
180 log_error("The 'name' argument is only valid"
181 " with --splitmirrors");
185 if (arg_count(cmd, merge_ARG))
188 if (arg_count(cmd, mirrors_ARG)) {
190 * --splitmirrors has been chosen as the mechanism for
191 * specifying the intent of detaching and keeping a mimage
192 * versus an additional qualifying argument being added here.
194 lp->mirrors = arg_uint_value(cmd, mirrors_ARG, 0);
195 lp->mirrors_sign = arg_sign_value(cmd, mirrors_ARG, 0);
198 lp->alloc = arg_uint_value(cmd, alloc_ARG, ALLOC_INHERIT);
200 /* There are three types of lvconvert. */
201 if (lp->merge) { /* Snapshot merge */
202 if (arg_count(cmd, regionsize_ARG) || arg_count(cmd, chunksize_ARG) ||
203 arg_count(cmd, zero_ARG) || arg_count(cmd, regionsize_ARG) ||
204 arg_count(cmd, stripes_long_ARG) || arg_count(cmd, stripesize_ARG)) {
205 log_error("Only --background and --interval are valid "
206 "arguments for snapshot merge");
210 if (!(lp->segtype = get_segtype_from_string(cmd, "snapshot")))
213 } else if (lp->snapshot) { /* Snapshot creation from pre-existing cow */
214 if (arg_count(cmd, regionsize_ARG)) {
215 log_error("--regionsize is only available with mirrors");
219 if (arg_count(cmd, stripesize_ARG) || arg_count(cmd, stripes_long_ARG)) {
220 log_error("--stripes and --stripesize are only available with striped mirrors");
224 if (arg_sign_value(cmd, chunksize_ARG, 0) == SIGN_MINUS) {
225 log_error("Negative chunk size is invalid");
228 lp->chunk_size = arg_uint_value(cmd, chunksize_ARG, 8);
229 if (lp->chunk_size < 8 || lp->chunk_size > 1024 ||
230 (lp->chunk_size & (lp->chunk_size - 1))) {
231 log_error("Chunk size must be a power of 2 in the "
235 log_verbose("Setting chunksize to %d sectors.", lp->chunk_size);
237 if (!(lp->segtype = get_segtype_from_string(cmd, "snapshot")))
240 lp->zero = strcmp(arg_str_value(cmd, zero_ARG,
241 (lp->segtype->flags &
242 SEG_CANNOT_BE_ZEROED) ?
245 } else { /* Mirrors */
246 if (arg_count(cmd, chunksize_ARG)) {
247 log_error("--chunksize is only available with "
252 if (arg_count(cmd, zero_ARG)) {
253 log_error("--zero is only available with snapshots");
258 * --regionsize is only valid if converting an LV into a mirror.
259 * Checked when we know the state of the LV being converted.
262 if (arg_count(cmd, regionsize_ARG)) {
263 if (arg_sign_value(cmd, regionsize_ARG, 0) ==
265 log_error("Negative regionsize is invalid");
268 lp->region_size = arg_uint_value(cmd, regionsize_ARG, 0);
270 region_size = 2 * find_config_tree_int(cmd,
271 "activation/mirror_region_size",
272 DEFAULT_MIRROR_REGION_SIZE);
273 if (region_size < 0) {
274 log_error("Negative regionsize in "
275 "configuration file is invalid");
278 lp->region_size = region_size;
281 if (lp->region_size % (pagesize >> SECTOR_SHIFT)) {
282 log_error("Region size (%" PRIu32 ") must be "
283 "a multiple of machine memory "
285 lp->region_size, pagesize >> SECTOR_SHIFT);
289 if (lp->region_size & (lp->region_size - 1)) {
290 log_error("Region size (%" PRIu32
291 ") must be a power of 2", lp->region_size);
295 if (!lp->region_size) {
296 log_error("Non-zero region size must be supplied.");
300 /* Default is never striped, regardless of existing LV configuration. */
301 if (!get_stripe_params(cmd, &lp->stripes, &lp->stripe_size)) {
306 if (!(lp->segtype = get_segtype_from_string(cmd, "mirror")))
310 if (activation() && lp->segtype->ops->target_present &&
311 !lp->segtype->ops->target_present(cmd, NULL, NULL)) {
312 log_error("%s: Required device-mapper target(s) not "
313 "detected in your kernel", lp->segtype->name);
317 if (!_lvconvert_name_params(lp, cmd, &argc, &argv))
322 lp->failed_pvs = NULL;
327 static struct volume_group *_get_lvconvert_vg(struct cmd_context *cmd,
329 const char *uuid __attribute__((unused)))
333 if (name && !strchr(name, '/'))
334 return vg_read_for_update(cmd, name, NULL, 0);
336 /* 'name' is the full LV name; must extract_vgname() */
337 return vg_read_for_update(cmd, extract_vgname(cmd, name),
341 static struct logical_volume *_get_lvconvert_lv(struct cmd_context *cmd __attribute__((unused)),
342 struct volume_group *vg,
345 uint32_t lv_type __attribute__((unused)))
347 struct logical_volume *lv = find_lv(vg, name);
349 if (!lv || (uuid && strcmp(uuid, (char *)&lv->lvid)))
355 static int _finish_lvconvert_mirror(struct cmd_context *cmd,
356 struct volume_group *vg,
357 struct logical_volume *lv,
358 struct dm_list *lvs_changed __attribute__((unused)))
362 if (!(lv->status & CONVERTING))
365 if (!collapse_mirrored_lv(lv)) {
366 log_error("Failed to remove temporary sync layer.");
370 lv->status &= ~CONVERTING;
372 log_very_verbose("Updating logical volume \"%s\" on disk(s)", lv->name);
377 if (!suspend_lv(cmd, lv)) {
378 log_error("Failed to lock %s", lv->name);
383 if (!vg_commit(vg)) {
388 log_very_verbose("Updating \"%s\" in kernel", lv->name);
390 if (!resume_lv(cmd, lv)) {
391 log_error("Problem reactivating %s", lv->name);
396 log_print("Logical volume %s converted.", lv->name);
402 static int _finish_lvconvert_merge(struct cmd_context *cmd,
403 struct volume_group *vg,
404 struct logical_volume *lv,
405 struct dm_list *lvs_changed __attribute__((unused)))
407 struct lv_segment *snap_seg = find_merging_cow(lv);
409 log_error("Logical volume %s has no merging snapshot.", lv->name);
413 log_print("Merge of snapshot into logical volume %s has finished.", lv->name);
414 if (!lv_remove_single(cmd, snap_seg->cow, DONT_PROMPT)) {
415 log_error("Could not remove snapshot %s merged into %s.",
416 snap_seg->cow->name, lv->name);
423 static progress_t _poll_merge_progress(struct cmd_context *cmd,
424 struct logical_volume *lv,
425 const char *name __attribute__((unused)),
426 struct daemon_parms *parms)
428 percent_t percent = PERCENT_0;
430 if (!lv_snapshot_percent(lv, &percent)) {
431 log_error("%s: Failed query for merging percentage. Aborting merge.", lv->name);
432 return PROGRESS_CHECK_FAILED;
433 } else if (percent == PERCENT_INVALID) {
434 log_error("%s: Merging snapshot invalidated. Aborting merge.", lv->name);
435 return PROGRESS_CHECK_FAILED;
438 if (parms->progress_display)
439 log_print("%s: %s: %.1f%%", lv->name, parms->progress_title,
440 percent_to_float(percent));
442 log_verbose("%s: %s: %.1f%%", lv->name, parms->progress_title,
443 percent_to_float(percent));
445 if (percent == PERCENT_0)
446 return PROGRESS_FINISHED_ALL;
448 return PROGRESS_UNFINISHED;
451 static struct poll_functions _lvconvert_mirror_fns = {
452 .get_copy_vg = _get_lvconvert_vg,
453 .get_copy_lv = _get_lvconvert_lv,
454 .poll_progress = poll_mirror_progress,
455 .finish_copy = _finish_lvconvert_mirror,
458 static struct poll_functions _lvconvert_merge_fns = {
459 .get_copy_vg = _get_lvconvert_vg,
460 .get_copy_lv = _get_lvconvert_lv,
461 .poll_progress = _poll_merge_progress,
462 .finish_copy = _finish_lvconvert_merge,
465 int lvconvert_poll(struct cmd_context *cmd, struct logical_volume *lv,
469 * FIXME allocate an "object key" structure with split
470 * out members (vg_name, lv_name, uuid, etc) and pass that
471 * around the lvconvert and polldaemon code
472 * - will avoid needless work, e.g. extract_vgname()
473 * - unfortunately there are enough overloaded "name" dragons in
474 * the polldaemon, lvconvert, pvmove code that a comprehensive
475 * audit/rework is needed
477 int len = strlen(lv->vg->name) + strlen(lv->name) + 2;
478 char *uuid = alloca(sizeof(lv->lvid));
479 char *lv_full_name = alloca(len);
481 if (!uuid || !lv_full_name)
484 if (!dm_snprintf(lv_full_name, len, "%s/%s", lv->vg->name, lv->name))
487 memcpy(uuid, &lv->lvid, sizeof(lv->lvid));
489 if (!lv_is_merging_origin(lv))
490 return poll_daemon(cmd, lv_full_name, uuid, background, 0,
491 &_lvconvert_mirror_fns, "Converted");
493 return poll_daemon(cmd, lv_full_name, uuid, background, 0,
494 &_lvconvert_merge_fns, "Merged");
497 static int _insert_lvconvert_layer(struct cmd_context *cmd,
498 struct logical_volume *lv)
500 char *format, *layer_name;
505 * We would like to give the same number for this layer
506 * and the newly added mimage.
507 * However, LV name of newly added mimage is determined *after*
508 * the LV name of this layer is determined.
510 * So, use generate_lv_name() to generate mimage name first
511 * and take the number from it.
514 len = strlen(lv->name) + 32;
515 if (!(format = alloca(len)) ||
516 !(layer_name = alloca(len)) ||
517 dm_snprintf(format, len, "%s_mimage_%%d", lv->name) < 0) {
518 log_error("lvconvert: layer name allocation failed.");
522 if (!generate_lv_name(lv->vg, format, layer_name, len) ||
523 sscanf(layer_name, format, &i) != 1) {
524 log_error("lvconvert: layer name generation failed.");
528 if (dm_snprintf(layer_name, len, MIRROR_SYNC_LAYER "_%d", i) < 0) {
529 log_error("layer name allocation failed.");
533 if (!insert_layer_for_lv(cmd, lv, 0, layer_name)) {
534 log_error("Failed to insert resync layer");
541 static int _area_missing(struct lv_segment *lvseg, int s)
543 if (seg_type(lvseg, s) == AREA_LV) {
544 if (seg_lv(lvseg, s)->status & PARTIAL_LV)
546 } else if ((seg_type(lvseg, s) == AREA_PV) &&
547 (is_missing_pv(seg_pv(lvseg, s))))
553 /* FIXME we want to handle mirror stacks here... */
554 static int _failed_mirrors_count(struct logical_volume *lv)
556 struct lv_segment *lvseg;
560 dm_list_iterate_items(lvseg, &lv->segments) {
561 if (!seg_is_mirrored(lvseg))
563 for (s = 0; s < lvseg->area_count; s++)
564 if (_area_missing(lvseg, s))
571 static struct dm_list *_failed_pv_list(struct volume_group *vg)
573 struct dm_list *failed_pvs;
574 struct pv_list *pvl, *new_pvl;
576 if (!(failed_pvs = dm_pool_alloc(vg->vgmem, sizeof(*failed_pvs)))) {
577 log_error("Allocation of list of failed_pvs failed.");
581 dm_list_init(failed_pvs);
583 dm_list_iterate_items(pvl, &vg->pvs) {
584 if (!is_missing_pv(pvl->pv))
588 * Finally, --repair will remove empty PVs.
589 * But we only want remove these which are output of repair,
590 * Do not count these which are already empty here.
591 * FIXME: code should traverse PV in LV not in whole VG.
592 * FIXME: layer violation? should it depend on vgreduce --removemising?
594 if (pvl->pv->pe_alloc_count == 0)
597 if (!(new_pvl = dm_pool_alloc(vg->vgmem, sizeof(*new_pvl)))) {
598 log_error("Allocation of failed_pvs list entry failed.");
601 new_pvl->pv = pvl->pv;
602 dm_list_add(failed_pvs, &new_pvl->list);
608 static int _is_partial_lv(struct logical_volume *lv,
609 void *baton __attribute__((unused)))
611 return lv->status & PARTIAL_LV;
615 * Walk down the stacked mirror LV to the original mirror LV.
617 static struct logical_volume *_original_lv(struct logical_volume *lv)
619 struct logical_volume *next_lv = lv, *tmp_lv;
621 while ((tmp_lv = find_temporary_mirror(next_lv)))
627 static void _lvconvert_mirrors_repair_ask(struct cmd_context *cmd,
628 int failed_log, int failed_mirrors,
629 int *replace_log, int *replace_mirrors)
631 const char *leg_policy = NULL, *log_policy = NULL;
633 int force = arg_count(cmd, force_ARG);
634 int yes = arg_count(cmd, yes_ARG);
636 *replace_log = *replace_mirrors = 1;
638 if (arg_count(cmd, use_policies_ARG)) {
639 leg_policy = find_config_tree_str(cmd,
640 "activation/mirror_image_fault_policy", NULL);
642 leg_policy = find_config_tree_str(cmd,
643 "activation/mirror_device_fault_policy",
644 DEFAULT_MIRROR_DEVICE_FAULT_POLICY);
645 log_policy = find_config_tree_str(cmd,
646 "activation/mirror_log_fault_policy",
647 DEFAULT_MIRROR_LOG_FAULT_POLICY);
648 *replace_mirrors = strcmp(leg_policy, "remove");
649 *replace_log = strcmp(log_policy, "remove");
656 if (force != PROMPT) {
657 *replace_log = *replace_mirrors = 0;
662 yes_no_prompt("Attempt to replace failed mirror log? [y/n]: ") == 'n') {
666 if (failed_mirrors &&
667 yes_no_prompt("Attempt to replace failed mirror images "
668 "(requires full device resync)? [y/n]: ") == 'n') {
669 *replace_mirrors = 0;
677 * Get the number of on-disk copies of the log.
682 static int _get_log_count(struct logical_volume *lv)
684 struct logical_volume *log_lv;
686 log_lv = first_seg(_original_lv(lv))->log_lv;
690 return lv_mirror_count(log_lv);
693 static int _lv_update_mirrored_log(struct logical_volume *lv,
694 struct dm_list *operable_pvs,
698 struct logical_volume *log_lv;
701 * When log_count is 0, mirrored log doesn't need to be
702 * updated here but it will be removed later.
707 log_lv = first_seg(_original_lv(lv))->log_lv;
708 if (!log_lv || !(log_lv->status & MIRRORED))
711 old_log_count = _get_log_count(lv);
712 if (old_log_count == log_count)
715 /* Reducing redundancy of the log */
716 return remove_mirror_images(log_lv, log_count,
717 is_mirror_image_removable,
721 static int _lv_update_log_type(struct cmd_context *cmd,
722 struct lvconvert_params *lp,
723 struct logical_volume *lv,
724 struct dm_list *operable_pvs,
727 uint32_t region_size;
729 struct logical_volume *original_lv;
730 struct logical_volume *log_lv;
732 old_log_count = _get_log_count(lv);
733 if (old_log_count == log_count)
736 original_lv = _original_lv(lv);
737 region_size = adjusted_mirror_region_size(lv->vg->extent_size,
741 /* Add a log where there is none */
742 if (!old_log_count) {
743 if (!add_mirror_log(cmd, original_lv, log_count,
744 region_size, operable_pvs, lp->alloc))
749 /* Remove an existing log completely */
751 if (!remove_mirror_log(cmd, original_lv, operable_pvs,
752 arg_count(cmd, yes_ARG) ||
753 arg_count(cmd, force_ARG)))
758 log_lv = first_seg(original_lv)->log_lv;
760 /* Adding redundancy to the log */
761 if (old_log_count < log_count) {
762 log_error("Adding log redundancy not supported yet.");
763 log_error("Try converting the log to 'core' first.");
767 /* Reducing redundancy of the log */
768 return remove_mirror_images(log_lv, log_count, is_mirror_image_removable, operable_pvs, 1U);
772 * Reomove missing and empty PVs from VG, if are also in provided list
774 static void _remove_missing_empty_pv(struct volume_group *vg, struct dm_list *remove_pvs)
776 struct pv_list *pvl, *pvl_vg, *pvlt;
782 dm_list_iterate_items(pvl, remove_pvs) {
783 dm_list_iterate_items_safe(pvl_vg, pvlt, &vg->pvs) {
784 if (!id_equal(&pvl->pv->id, &pvl_vg->pv->id) ||
785 !is_missing_pv(pvl_vg->pv) ||
786 pvl_vg->pv->pe_alloc_count != 0)
789 /* FIXME: duplication of vgreduce code, move this to library */
790 vg->free_count -= pvl_vg->pv->pe_count;
791 vg->extent_count -= pvl_vg->pv->pe_count;
792 del_pvl_from_vgs(vg, pvl_vg);
799 if (!vg_write(vg) || !vg_commit(vg)) {
803 log_warn("%d missing and now unallocated Physical Volumes removed from VG.", removed);
808 * _lvconvert_mirrors_parse_params
810 * This function performs the following:
811 * 1) Gets the old values of mimage and log counts
812 * 2) Parses the CLI args to find the new desired values
813 * 3) Adjusts 'lp->mirrors' to the appropriate absolute value.
814 * (Remember, 'lp->mirrors' is specified in terms of the number of "copies"
815 * vs. the number of mimages. It can also be a relative value.)
816 * 4) Sets 'lp->need_polling' if collapsing
817 * 5) Validates other mirror params
819 * Returns: 1 on success, 0 on error
821 static int _lvconvert_mirrors_parse_params(struct cmd_context *cmd,
822 struct logical_volume *lv,
823 struct lvconvert_params *lp,
824 uint32_t *old_mimage_count,
825 uint32_t *old_log_count,
826 uint32_t *new_mimage_count,
827 uint32_t *new_log_count)
829 int repair = arg_count(cmd, repair_ARG);
830 const char *mirrorlog;
831 *old_mimage_count = lv_mirror_count(lv);
832 *old_log_count = _get_log_count(lv);
835 * Collapsing a stack of mirrors:
837 * If called with no argument, try collapsing the resync layers
839 if (!arg_count(cmd, mirrors_ARG) && !arg_count(cmd, mirrorlog_ARG) &&
840 !arg_count(cmd, corelog_ARG) && !arg_count(cmd, regionsize_ARG) &&
841 !arg_count(cmd, splitmirrors_ARG) && !repair) {
842 *new_mimage_count = *old_mimage_count;
843 *new_log_count = *old_log_count;
845 if (find_temporary_mirror(lv) || (lv->status & CONVERTING))
846 lp->need_polling = 1;
850 if ((arg_count(cmd, mirrors_ARG) && repair) ||
851 (arg_count(cmd, mirrorlog_ARG) && repair) ||
852 (arg_count(cmd, corelog_ARG) && repair)) {
853 log_error("--repair cannot be used with --mirrors, --mirrorlog,"
858 if (arg_count(cmd, mirrorlog_ARG) && arg_count(cmd, corelog_ARG)) {
859 log_error("--mirrorlog and --corelog are incompatible");
864 * Adjusting mimage count?
866 if (!arg_count(cmd, mirrors_ARG) && !arg_count(cmd, splitmirrors_ARG))
867 lp->mirrors = *old_mimage_count;
868 else if (lp->mirrors_sign == SIGN_PLUS)
869 lp->mirrors = *old_mimage_count + lp->mirrors;
870 else if (lp->mirrors_sign == SIGN_MINUS)
871 lp->mirrors = (*old_mimage_count > lp->mirrors) ?
872 *old_mimage_count - lp->mirrors: 0;
876 *new_mimage_count = lp->mirrors;
878 /* Too many mimages? */
879 if (lp->mirrors > DEFAULT_MIRROR_MAX_IMAGES) {
880 log_error("Only up to %d images in mirror supported currently.",
881 DEFAULT_MIRROR_MAX_IMAGES);
885 /* Did the user try to subtract more legs than available? */
886 if (lp->mirrors < 1) {
887 log_error("Unable to reduce images by specified amount - only %d in %s",
888 *old_mimage_count, lv->name);
893 * FIXME: It would be nice to say what we are adjusting to, but
894 * I really don't know whether to specify the # of copies or mimages.
896 if (*old_mimage_count != *new_mimage_count)
897 log_verbose("Adjusting mirror image count of %s", lv->name);
902 * If we are converting from a mirror to another mirror or simply
903 * changing the log type, we start by assuming they want the log
904 * type the same and then parse the given args. OTOH, If we are
905 * converting from linear to mirror, then we start from the default
906 * position that the user would like a 'disk' log.
908 *new_log_count = (*old_mimage_count > 1) ? *old_log_count : 1;
909 if (!arg_count(cmd, corelog_ARG) && !arg_count(cmd, mirrorlog_ARG))
912 if (arg_count(cmd, corelog_ARG))
915 mirrorlog = arg_str_value(cmd, mirrorlog_ARG,
916 !*new_log_count ? "core" : DEFAULT_MIRRORLOG);
918 if (!strcmp("mirrored", mirrorlog))
920 else if (!strcmp("disk", mirrorlog))
922 else if (!strcmp("core", mirrorlog))
925 log_error("Unknown mirrorlog type: %s", mirrorlog);
930 * No mirrored logs for cluster mirrors until
931 * log daemon is multi-threaded.
933 if ((*new_log_count == 2) && vg_is_clustered(lv->vg)) {
934 log_error("Log type, \"mirrored\", is unavailable to cluster mirrors");
938 log_verbose("Setting logging type to %s", mirrorlog);
941 * Region size must not change on existing mirrors
943 if (arg_count(cmd, regionsize_ARG) && (lv->status & MIRRORED) &&
944 (lp->region_size != first_seg(lv)->region_size)) {
945 log_error("Mirror log region size cannot be changed on "
946 "an existing mirror.");
951 * For the most part, we cannot handle multi-segment mirrors. Bail out
952 * early if we have encountered one.
954 if ((lv->status & MIRRORED) && dm_list_size(&lv->segments) != 1) {
955 log_error("Logical volume %s has multiple "
956 "mirror segments.", lv->name);
963 static int _reload_lv(struct cmd_context *cmd, struct logical_volume *lv)
965 log_very_verbose("Updating logical volume \"%s\" on disk(s)", lv->name);
967 if (!vg_write(lv->vg))
970 if (!suspend_lv(cmd, lv)) {
971 log_error("Failed to lock %s", lv->name);
976 if (!vg_commit(lv->vg)) {
977 if (!resume_lv(cmd, lv))
982 log_very_verbose("Updating \"%s\" in kernel", lv->name);
984 if (!resume_lv(cmd, lv)) {
985 log_error("Problem reactivating %s", lv->name);
992 * _lvconvert_mirrors_aux
994 * Add/remove mirror images and adjust log type. 'operable_pvs'
995 * are the set of PVs open to removal or allocation - depending
996 * on the operation being performed.
998 static int _lvconvert_mirrors_aux(struct cmd_context *cmd,
999 struct logical_volume *lv,
1000 struct lvconvert_params *lp,
1001 struct dm_list *operable_pvs,
1002 uint32_t new_mimage_count,
1003 uint32_t new_log_count)
1005 uint32_t region_size;
1006 struct lv_segment *seg;
1007 struct logical_volume *layer_lv;
1008 uint32_t old_mimage_count = lv_mirror_count(lv);
1009 uint32_t old_log_count = _get_log_count(lv);
1011 if ((lp->mirrors == 1) && !(lv->status & MIRRORED)) {
1012 log_error("Logical volume %s is already not mirrored.",
1017 region_size = adjusted_mirror_region_size(lv->vg->extent_size,
1022 operable_pvs = lp->pvh;
1024 seg = first_seg(lv);
1027 * Up-convert from linear to mirror
1029 if (!(lv->status & MIRRORED)) {
1030 /* FIXME Share code with lvcreate */
1033 * FIXME should we give not only lp->pvh, but also all PVs
1034 * currently taken by the mirror? Would make more sense from
1037 if (!lv_add_mirrors(cmd, lv, new_mimage_count - 1, lp->stripes,
1038 lp->stripe_size, region_size, new_log_count, operable_pvs,
1039 lp->alloc, MIRROR_BY_LV)) {
1043 if (lp->wait_completion)
1044 lp->need_polling = 1;
1050 * Up-convert m-way mirror to n-way mirror
1052 if (new_mimage_count > old_mimage_count) {
1053 if (lv->status & MIRROR_NOTSYNCED) {
1054 log_error("Can't add mirror to out-of-sync mirrored "
1055 "LV: use lvchange --resync first.");
1060 * We allow snapshots of mirrors, but for now, we
1061 * do not allow up converting mirrors that are under
1062 * snapshots. The layering logic is somewhat complex,
1063 * and preliminary test show that the conversion can't
1064 * seem to get the correct %'age of completion.
1066 if (lv_is_origin(lv)) {
1067 log_error("Can't add additional mirror images to "
1068 "mirrors that are under snapshots");
1073 * Is there already a convert in progress? We do not
1074 * currently allow more than one.
1076 if (find_temporary_mirror(lv) || (lv->status & CONVERTING)) {
1077 log_error("%s is already being converted. Unable to start another conversion.",
1083 * Is there already a convert in progress? We do not
1084 * currently allow more than one.
1086 if (find_temporary_mirror(lv) || (lv->status & CONVERTING)) {
1087 log_error("%s is already being converted. Unable to start another conversion.",
1093 * Log addition/removal should be done before the layer
1094 * insertion to make the end result consistent with
1095 * linear-to-mirror conversion.
1097 if (!_lv_update_log_type(cmd, lp, lv,
1098 operable_pvs, new_log_count)) {
1103 /* Insert a temporary layer for syncing,
1104 * only if the original lv is using disk log. */
1105 if (seg->log_lv && !_insert_lvconvert_layer(cmd, lv)) {
1106 log_error("Failed to insert resync layer");
1110 /* FIXME: can't have multiple mlogs. force corelog. */
1111 if (!lv_add_mirrors(cmd, lv,
1112 new_mimage_count - old_mimage_count, lp->stripes, lp->stripe_size,
1113 region_size, 0U, operable_pvs, lp->alloc,
1115 layer_lv = seg_lv(first_seg(lv), 0);
1116 if (!remove_layer_from_lv(lv, layer_lv) ||
1117 !deactivate_lv(cmd, layer_lv) ||
1118 !lv_remove(layer_lv) || !vg_write(lv->vg) ||
1119 !vg_commit(lv->vg)) {
1120 log_error("ABORTING: Failed to remove "
1121 "temporary mirror layer %s.",
1123 log_error("Manual cleanup with vgcfgrestore "
1124 "and dmsetup may be required.");
1131 lv->status |= CONVERTING;
1132 lp->need_polling = 1;
1134 goto out_skip_log_convert;
1138 * Down-convert (reduce # of mimages).
1140 if (new_mimage_count < old_mimage_count) {
1141 uint32_t nmc = old_mimage_count - new_mimage_count;
1142 uint32_t nlc = (!new_log_count || lp->mirrors == 1) ? 1U : 0U;
1144 /* FIXME: Why did nlc used to be calculated that way? */
1146 /* Reduce number of mirrors */
1147 if (lp->keep_mimages) {
1148 if (!lv_split_mirror_images(lv, lp->lv_split_name,
1151 } else if (!lv_remove_mirrors(cmd, lv, nmc, nlc,
1152 is_mirror_image_removable, operable_pvs, 0))
1155 goto out; /* Just in case someone puts code between */
1160 * Converting the log type
1162 if ((lv->status & MIRRORED) && (old_log_count != new_log_count)) {
1163 if (!_lv_update_log_type(cmd, lp, lv,
1164 operable_pvs, new_log_count)) {
1170 out_skip_log_convert:
1172 if (!_reload_lv(cmd, lv))
1179 * _lvconvert_mirrors_repair
1181 * This function operates in two phases. First, all of the bad
1182 * devices are removed from the mirror. Then, if desired by the
1183 * user, the devices are replaced.
1185 * 'old_mimage_count' and 'old_log_count' are there so we know
1186 * what to convert to after the removal of devices.
1188 static int _lvconvert_mirrors_repair(struct cmd_context *cmd,
1189 struct logical_volume *lv,
1190 struct lvconvert_params *lp,
1191 uint32_t old_mimage_count,
1192 uint32_t old_log_count)
1195 int failed_mirrors = 0;
1196 int replace_log = 0;
1197 int replace_mirrors = 0;
1198 uint32_t new_log_count, log_count;
1199 struct logical_volume *log_lv;
1201 cmd->handles_missing_pvs = 1;
1202 cmd->partial_activation = 1;
1203 lp->need_polling = 0;
1205 lv_check_transient(lv); /* TODO check this in lib for all commands? */
1207 if (!(lv->status & PARTIAL_LV)) {
1208 log_error("%s is consistent. Nothing to repair.", lv->name);
1213 * Count the failed mimages - negative if 'lv' is not a mirror
1215 if ((failed_mirrors = _failed_mirrors_count(lv)) < 0)
1218 lp->mirrors = old_mimage_count - failed_mirrors;
1220 if (lp->mirrors != old_mimage_count)
1221 log_error("Mirror status: %d of %d images failed.",
1222 failed_mirrors, old_mimage_count);
1225 * Count the failed log devices
1227 new_log_count = old_log_count;
1228 log_lv = first_seg(lv)->log_lv;
1230 new_log_count = lv_mirror_count(log_lv);
1231 if (log_lv->status & PARTIAL_LV) {
1233 if (log_lv->status & MIRRORED)
1234 new_log_count -= _failed_mirrors_count(log_lv);
1239 if (old_log_count != new_log_count)
1240 log_error("Mirror log status: %d of %d images failed%s",
1241 old_log_count - new_log_count, old_log_count,
1242 (!new_log_count) ? " - switching to core" : "");
1245 * Find out our policies
1247 _lvconvert_mirrors_repair_ask(cmd, failed_log, failed_mirrors,
1248 &replace_log, &replace_mirrors);
1251 * First phase - remove faulty devices
1253 if (!(lp->failed_pvs = _failed_pv_list(lv->vg)))
1256 log_count = new_log_count;
1259 * We must adjust the log first, or the entire mirror
1260 * will get stuck during a suspend.
1262 if (!_lv_update_mirrored_log(lv, lp->failed_pvs, log_count))
1265 if (lp->mirrors == 1)
1268 if (failed_mirrors) {
1269 if (!lv_remove_mirrors(cmd, lv, failed_mirrors,
1270 log_count ? 0U : 1U,
1271 _is_partial_lv, NULL, 0))
1275 if (!_lv_update_log_type(cmd, lp, lv, lp->failed_pvs,
1279 if (!_reload_lv(cmd, lv))
1283 * Second phase - replace faulty devices
1286 if (replace_mirrors)
1287 lp->mirrors = old_mimage_count;
1290 * It does not make sense to replace the log if the volume is no longer
1293 if (!replace_mirrors && lp->mirrors == 1)
1296 log_count = replace_log ? old_log_count : new_log_count;
1298 while (replace_mirrors || replace_log) {
1299 log_warn("Trying to up-convert to %d images, %d logs.", lp->mirrors, log_count);
1300 if (_lvconvert_mirrors_aux(cmd, lv, lp, NULL,
1301 lp->mirrors, log_count))
1304 if (lp->mirrors > 2)
1306 else if (log_count > 0)
1309 break; /* nowhere to go, anymore... */
1313 if (replace_mirrors && lp->mirrors != old_mimage_count)
1314 log_warn("WARNING: Failed to replace %d of %d images in volume %s",
1315 old_mimage_count - lp->mirrors, old_mimage_count, lv->name);
1316 if (replace_log && log_count != old_log_count)
1317 log_warn("WARNING: Failed to replace %d of %d logs in volume %s",
1318 old_log_count - log_count, old_log_count, lv->name);
1320 /* if (!arg_count(cmd, use_policies_ARG) && (lp->mirrors != old_mimage_count
1321 || log_count != old_log_count))
1328 * _lvconvert_mirrors
1330 * Determine what is being done. Are we doing a conversion, repair, or
1331 * collapsing a stack? Once determined, call helper functions.
1333 static int _lvconvert_mirrors(struct cmd_context *cmd,
1334 struct logical_volume *lv,
1335 struct lvconvert_params *lp)
1337 int repair = arg_count(cmd, repair_ARG);
1338 uint32_t old_mimage_count;
1339 uint32_t old_log_count;
1340 uint32_t new_mimage_count;
1341 uint32_t new_log_count;
1343 /* Adjust mimage and/or log count */
1344 if (!_lvconvert_mirrors_parse_params(cmd, lv, lp,
1345 &old_mimage_count, &old_log_count,
1346 &new_mimage_count, &new_log_count))
1349 if (((old_mimage_count < new_mimage_count && old_log_count > new_log_count) ||
1350 (old_mimage_count > new_mimage_count && old_log_count < new_log_count)) &&
1352 log_error("Cannot both allocate and free extents when specifying physical"
1353 " volumes to use.");
1354 log_error("Please specify the operation in two steps.");
1358 /* Nothing to do? (Probably finishing collapse.) */
1359 if ((old_mimage_count == new_mimage_count) &&
1360 (old_log_count == new_log_count) && !repair)
1364 return _lvconvert_mirrors_repair(cmd, lv, lp,
1368 if (!_lvconvert_mirrors_aux(cmd, lv, lp, NULL,
1369 new_mimage_count, new_log_count))
1372 if (!lp->need_polling)
1373 log_print("Logical volume %s converted.", lv->name);
1379 static int lvconvert_snapshot(struct cmd_context *cmd,
1380 struct logical_volume *lv,
1381 struct lvconvert_params *lp)
1383 struct logical_volume *org;
1386 if (!(org = find_lv(lv->vg, lp->origin))) {
1387 log_error("Couldn't find origin volume '%s'.", lp->origin);
1392 log_error("Unable to use \"%s\" as both snapshot and origin.",
1397 if (org->status & (LOCKED|PVMOVE|MIRRORED) || lv_is_cow(org)) {
1398 log_error("Unable to create a snapshot of a %s LV.",
1399 org->status & LOCKED ? "locked" :
1400 org->status & PVMOVE ? "pvmove" :
1401 org->status & MIRRORED ? "mirrored" :
1406 if (!lp->zero || !(lv->status & LVM_WRITE))
1407 log_warn("WARNING: \"%s\" not zeroed", lv->name);
1408 else if (!set_lv(cmd, lv, UINT64_C(0), 0)) {
1409 log_error("Aborting. Failed to wipe snapshot "
1410 "exception store.");
1414 if (!deactivate_lv(cmd, lv)) {
1415 log_error("Couldn't deactivate LV %s.", lv->name);
1419 if (!vg_add_snapshot(org, lv, NULL, org->le_count, lp->chunk_size)) {
1420 log_error("Couldn't create snapshot.");
1424 /* store vg on disk(s) */
1425 if (!vg_write(lv->vg))
1428 if (!suspend_lv(cmd, org)) {
1429 log_error("Failed to suspend origin %s", org->name);
1434 if (!vg_commit(lv->vg))
1437 if (!resume_lv(cmd, org)) {
1438 log_error("Problem reactivating origin %s", org->name);
1442 log_print("Logical volume %s converted to snapshot.", lv->name);
1449 static int lvconvert_merge(struct cmd_context *cmd,
1450 struct logical_volume *lv,
1451 struct lvconvert_params *lp)
1454 int merge_on_activate = 0;
1455 struct logical_volume *origin = origin_from_cow(lv);
1456 struct lv_segment *cow_seg = find_cow(lv);
1459 /* Check if merge is possible */
1460 if (lv_is_merging_cow(lv)) {
1461 log_error("Snapshot %s is already merging", lv->name);
1464 if (lv_is_merging_origin(origin)) {
1465 log_error("Snapshot %s is already merging into the origin",
1466 find_merging_cow(origin)->cow->name);
1471 * Prevent merge with open device(s) as it would likely lead
1472 * to application/filesystem failure. Merge on origin's next
1473 * activation if either the origin or snapshot LV are currently
1476 * FIXME testing open_count is racey; snapshot-merge target's
1477 * constructor and DM should prevent appropriate devices from
1480 if (lv_info(cmd, origin, 0, &info, 1, 0)) {
1481 if (info.open_count) {
1482 log_error("Can't merge over open origin volume");
1483 merge_on_activate = 1;
1486 if (lv_info(cmd, lv, 0, &info, 1, 0)) {
1487 if (info.open_count) {
1488 log_print("Can't merge when snapshot is open");
1489 merge_on_activate = 1;
1493 init_snapshot_merge(cow_seg, origin);
1495 /* store vg on disk(s) */
1496 if (!vg_write(lv->vg))
1499 if (merge_on_activate) {
1500 /* commit vg but skip starting the merge */
1501 if (!vg_commit(lv->vg))
1504 log_print("Merging of snapshot %s will start "
1505 "next activation.", lv->name);
1510 if (!suspend_lv(cmd, origin)) {
1511 log_error("Failed to suspend origin %s", origin->name);
1516 if (!vg_commit(lv->vg)) {
1517 if (!resume_lv(cmd, origin))
1522 if (!resume_lv(cmd, origin)) {
1523 log_error("Failed to reactivate origin %s", origin->name);
1527 lp->need_polling = 1;
1528 lp->lv_to_poll = origin;
1531 log_print("Merging of volume %s started.", lv->name);
1537 static int _lvconvert_single(struct cmd_context *cmd, struct logical_volume *lv,
1540 struct lvconvert_params *lp = handle;
1542 if (lv->status & LOCKED) {
1543 log_error("Cannot convert locked LV %s", lv->name);
1547 if (lv_is_cow(lv) && !lp->merge) {
1548 log_error("Can't convert snapshot logical volume \"%s\"",
1553 if (lv->status & PVMOVE) {
1554 log_error("Unable to convert pvmove LV %s", lv->name);
1558 if (arg_count(cmd, repair_ARG) && !(lv->status & MIRRORED)) {
1559 if (arg_count(cmd, use_policies_ARG))
1560 return ECMD_PROCESSED; /* nothing to be done here */
1561 log_error("Can't repair non-mirrored LV \"%s\".", lv->name);
1566 if (!lv_is_cow(lv)) {
1567 log_error("Logical volume \"%s\" is not a snapshot",
1571 if (!archive(lv->vg)) {
1575 if (!lvconvert_merge(cmd, lv, lp)) {
1576 log_error("Unable to merge LV \"%s\" into its origin.", lv->name);
1579 } else if (lp->snapshot) {
1580 if (lv->status & MIRRORED) {
1581 log_error("Unable to convert mirrored LV \"%s\" into a snapshot.", lv->name);
1584 if (!archive(lv->vg)) {
1588 if (!lvconvert_snapshot(cmd, lv, lp)) {
1592 } else if (arg_count(cmd, mirrors_ARG) ||
1593 arg_count(cmd, splitmirrors_ARG) ||
1594 (lv->status & MIRRORED)) {
1595 if (!archive(lv->vg)) {
1599 if (!_lvconvert_mirrors(cmd, lv, lp)) {
1604 /* If repairing and using policies, remove missing PVs from VG */
1605 if (arg_count(cmd, repair_ARG) && arg_count(cmd, use_policies_ARG))
1606 _remove_missing_empty_pv(lv->vg, lp->failed_pvs);
1609 return ECMD_PROCESSED;
1613 * FIXME move to toollib along with the rest of the drop/reacquire
1614 * VG locking that is used by lvconvert_merge_single()
1616 static struct logical_volume *get_vg_lock_and_logical_volume(struct cmd_context *cmd,
1617 const char *vg_name,
1618 const char *lv_name)
1621 * Returns NULL if the requested LV doesn't exist;
1622 * otherwise the caller must free_vg(lv->vg)
1623 * - it is also up to the caller to unlock_vg() as needed
1625 struct volume_group *vg;
1626 struct logical_volume* lv = NULL;
1628 vg = _get_lvconvert_vg(cmd, vg_name, NULL);
1629 if (vg_read_error(vg)) {
1634 if (!(lv = _get_lvconvert_lv(cmd, vg, lv_name, NULL, 0))) {
1635 log_error("Can't find LV %s in VG %s", lv_name, vg_name);
1636 unlock_and_free_vg(cmd, vg, vg_name);
1643 static int poll_logical_volume(struct cmd_context *cmd, struct logical_volume *lv,
1644 int wait_completion)
1648 if (!lv_info(cmd, lv, 0, &info, 1, 0) || !info.exists) {
1649 log_print("Conversion starts after activation.");
1650 return ECMD_PROCESSED;
1652 return lvconvert_poll(cmd, lv, wait_completion ? 0 : 1U);
1655 static int lvconvert_single(struct cmd_context *cmd, struct lvconvert_params *lp)
1657 struct logical_volume *lv = NULL;
1658 int ret = ECMD_FAILED;
1659 int saved_ignore_suspended_devices = ignore_suspended_devices();
1661 if (arg_count(cmd, repair_ARG)) {
1662 init_ignore_suspended_devices(1);
1663 cmd->handles_missing_pvs = 1;
1666 lv = get_vg_lock_and_logical_volume(cmd, lp->vg_name, lp->lv_name);
1671 * lp->pvh holds the list of PVs available for allocation or removal
1674 if (!(lp->pvh = create_pv_list(cmd->mem, lv->vg, lp->pv_count,
1678 lp->pvh = &lv->vg->pvs;
1680 lp->lv_to_poll = lv;
1681 ret = _lvconvert_single(cmd, lv, lp);
1683 unlock_vg(cmd, lp->vg_name);
1685 if (ret == ECMD_PROCESSED && lp->need_polling)
1686 ret = poll_logical_volume(cmd, lp->lv_to_poll,
1687 lp->wait_completion);
1691 init_ignore_suspended_devices(saved_ignore_suspended_devices);
1695 static int lvconvert_merge_single(struct cmd_context *cmd, struct logical_volume *lv,
1698 struct lvconvert_params *lp = handle;
1699 const char *vg_name = NULL;
1700 struct logical_volume *refreshed_lv = NULL;
1704 * FIXME can't trust lv's VG to be current given that caller
1705 * is process_each_lv() -- poll_logical_volume() may have
1706 * already updated the VG's metadata in an earlier iteration.
1707 * - preemptively drop the VG lock, as is needed for
1708 * poll_logical_volume(), refresh LV (and VG in the process).
1710 vg_name = lv->vg->name;
1711 unlock_vg(cmd, vg_name);
1712 refreshed_lv = get_vg_lock_and_logical_volume(cmd, vg_name, lv->name);
1713 if (!refreshed_lv) {
1714 log_error("ABORTING: Can't reread LV %s/%s", vg_name, lv->name);
1718 lp->lv_to_poll = refreshed_lv;
1719 ret = _lvconvert_single(cmd, refreshed_lv, lp);
1721 if (ret == ECMD_PROCESSED && lp->need_polling) {
1723 * Must drop VG lock, because lvconvert_poll() needs it,
1724 * then reacquire it after polling completes
1726 unlock_vg(cmd, vg_name);
1728 ret = poll_logical_volume(cmd, lp->lv_to_poll,
1729 lp->wait_completion);
1731 /* use LCK_VG_WRITE to match lvconvert()'s READ_FOR_UPDATE */
1732 if (!lock_vol(cmd, vg_name, LCK_VG_WRITE)) {
1733 log_error("ABORTING: Can't relock VG for %s "
1734 "after polling finished", vg_name);
1739 free_vg(refreshed_lv->vg);
1744 int lvconvert(struct cmd_context * cmd, int argc, char **argv)
1746 struct lvconvert_params lp;
1748 if (!_read_params(&lp, cmd, argc, argv)) {
1750 return EINVALID_CMD_LINE;
1755 log_error("Please provide logical volume path");
1756 return EINVALID_CMD_LINE;
1758 return process_each_lv(cmd, argc, argv, READ_FOR_UPDATE, &lp,
1759 &lvconvert_merge_single);
1762 return lvconvert_single(cmd, &lp);