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
{
28 const char *lv_split_name
;
29 const char *lv_name_full
;
39 uint32_t keep_mimages
;
43 const struct segment_type
*segtype
;
53 struct dm_list
*replace_pvh
;
55 struct logical_volume
*lv_to_poll
;
58 static int _lvconvert_name_params(struct lvconvert_params
*lp
,
59 struct cmd_context
*cmd
,
60 int *pargc
, char ***pargv
)
63 const char *vg_name
= NULL
;
70 log_error("Please specify a logical volume to act as "
71 "the snapshot origin.");
75 lp
->origin
= *pargv
[0];
76 (*pargv
)++, (*pargc
)--;
77 if (!(lp
->vg_name
= extract_vgname(cmd
, lp
->origin
))) {
78 log_error("The origin name should include the "
83 /* Strip the volume group from the origin */
84 if ((ptr
= strrchr(lp
->origin
, (int) '/')))
89 log_error("Please provide logical volume path");
93 lp
->lv_name
= lp
->lv_name_full
= (*pargv
)[0];
94 (*pargv
)++, (*pargc
)--;
96 if (strchr(lp
->lv_name_full
, '/') &&
97 (vg_name
= extract_vgname(cmd
, lp
->lv_name_full
)) &&
98 lp
->vg_name
&& strcmp(vg_name
, lp
->vg_name
)) {
99 log_error("Please use a single volume group name "
100 "(\"%s\" or \"%s\")", vg_name
, lp
->vg_name
);
105 lp
->vg_name
= vg_name
;
107 if (!validate_name(lp
->vg_name
)) {
108 log_error("Please provide a valid volume group name");
112 if ((ptr
= strrchr(lp
->lv_name_full
, '/')))
113 lp
->lv_name
= ptr
+ 1;
115 if (!lp
->merge_mirror
&& !apply_lvname_restrictions(lp
->lv_name
))
118 if (*pargc
&& lp
->snapshot
) {
119 log_error("Too many arguments provided for snapshots");
126 static int _read_params(struct lvconvert_params
*lp
, struct cmd_context
*cmd
,
127 int argc
, char **argv
)
131 struct arg_value_group_list
*group
;
133 int pagesize
= lvm_getpagesize();
135 memset(lp
, 0, sizeof(*lp
));
137 if ((arg_count(cmd
, snapshot_ARG
) || arg_count(cmd
, merge_ARG
)) &&
138 (arg_count(cmd
, mirrorlog_ARG
) || arg_count(cmd
, mirrors_ARG
) ||
139 arg_count(cmd
, repair_ARG
))) {
140 log_error("--snapshot or --merge argument cannot be mixed "
141 "with --mirrors, --repair or --mirrorlog");
145 if (!arg_count(cmd
, background_ARG
))
146 lp
->wait_completion
= 1;
148 if (arg_count(cmd
, snapshot_ARG
))
151 if (arg_count(cmd
, snapshot_ARG
) && arg_count(cmd
, merge_ARG
)) {
152 log_error("--snapshot and --merge are mutually exclusive");
156 if (arg_count(cmd
, splitmirrors_ARG
) && arg_count(cmd
, mirrors_ARG
)) {
157 log_error("--mirrors and --splitmirrors are "
158 "mutually exclusive");
163 * The '--splitmirrors n' argument is equivalent to '--mirrors -n'
164 * (note the minus sign), except that it signifies the additional
165 * intent to keep the mimage that is detached, rather than
168 if (arg_count(cmd
, splitmirrors_ARG
)) {
169 if (!arg_count(cmd
, name_ARG
) &&
170 !arg_count(cmd
, trackchanges_ARG
)) {
171 log_error("Please name the new logical volume using '--name'");
175 lp
->lv_split_name
= arg_value(cmd
, name_ARG
);
176 if (lp
->lv_split_name
&&
177 !apply_lvname_restrictions(lp
->lv_split_name
))
180 lp
->keep_mimages
= 1;
181 lp
->mirrors
= arg_uint_value(cmd
, splitmirrors_ARG
, 0);
182 lp
->mirrors_sign
= SIGN_MINUS
;
183 } else if (arg_count(cmd
, name_ARG
)) {
184 log_error("The 'name' argument is only valid"
185 " with --splitmirrors");
189 if (arg_count(cmd
, merge_ARG
)) {
190 if ((argc
== 1) && strstr(argv
[0], "_rimage_"))
191 lp
->merge_mirror
= 1;
196 if (arg_count(cmd
, mirrors_ARG
)) {
198 * --splitmirrors has been chosen as the mechanism for
199 * specifying the intent of detaching and keeping a mimage
200 * versus an additional qualifying argument being added here.
202 lp
->mirrors
= arg_uint_value(cmd
, mirrors_ARG
, 0);
203 lp
->mirrors_sign
= arg_sign_value(cmd
, mirrors_ARG
, SIGN_NONE
);
206 lp
->alloc
= (alloc_policy_t
) arg_uint_value(cmd
, alloc_ARG
, ALLOC_INHERIT
);
208 /* There are three types of lvconvert. */
209 if (lp
->merge
) { /* Snapshot merge */
210 if (arg_count(cmd
, regionsize_ARG
) || arg_count(cmd
, chunksize_ARG
) ||
211 arg_count(cmd
, zero_ARG
) || arg_count(cmd
, regionsize_ARG
) ||
212 arg_count(cmd
, stripes_long_ARG
) || arg_count(cmd
, stripesize_ARG
)) {
213 log_error("Only --background and --interval are valid "
214 "arguments for snapshot merge");
218 if (!(lp
->segtype
= get_segtype_from_string(cmd
, "snapshot")))
221 } else if (lp
->snapshot
) { /* Snapshot creation from pre-existing cow */
222 if (arg_count(cmd
, regionsize_ARG
)) {
223 log_error("--regionsize is only available with mirrors");
227 if (arg_count(cmd
, stripesize_ARG
) || arg_count(cmd
, stripes_long_ARG
)) {
228 log_error("--stripes and --stripesize are only available with striped mirrors");
232 if (arg_sign_value(cmd
, chunksize_ARG
, SIGN_NONE
) == SIGN_MINUS
) {
233 log_error("Negative chunk size is invalid");
236 lp
->chunk_size
= arg_uint_value(cmd
, chunksize_ARG
, 8);
237 if (lp
->chunk_size
< 8 || lp
->chunk_size
> 1024 ||
238 (lp
->chunk_size
& (lp
->chunk_size
- 1))) {
239 log_error("Chunk size must be a power of 2 in the "
243 log_verbose("Setting chunksize to %d sectors.", lp
->chunk_size
);
245 if (!(lp
->segtype
= get_segtype_from_string(cmd
, "snapshot")))
248 lp
->zero
= strcmp(arg_str_value(cmd
, zero_ARG
,
249 (lp
->segtype
->flags
&
250 SEG_CANNOT_BE_ZEROED
) ?
253 } else if (arg_count(cmd
, replace_ARG
)) { /* RAID device replacement */
254 lp
->replace_pv_count
= arg_count(cmd
, replace_ARG
);
255 lp
->replace_pvs
= dm_pool_alloc(cmd
->mem
, sizeof(char *) * lp
->replace_pv_count
);
256 if (!lp
->replace_pvs
)
260 dm_list_iterate_items(group
, &cmd
->arg_value_groups
) {
261 if (!grouped_arg_is_set(group
->arg_values
, replace_ARG
))
263 if (!(tmp_str
= grouped_arg_str_value(group
->arg_values
,
266 log_error("Failed to get '--replace' argument");
269 if (!(lp
->replace_pvs
[i
++] = dm_pool_strdup(cmd
->mem
,
273 } else { /* Mirrors (and some RAID functions) */
274 if (arg_count(cmd
, chunksize_ARG
)) {
275 log_error("--chunksize is only available with "
280 if (arg_count(cmd
, zero_ARG
)) {
281 log_error("--zero is only available with snapshots");
286 * --regionsize is only valid if converting an LV into a mirror.
287 * Checked when we know the state of the LV being converted.
290 if (arg_count(cmd
, regionsize_ARG
)) {
291 if (arg_sign_value(cmd
, regionsize_ARG
, SIGN_NONE
) ==
293 log_error("Negative regionsize is invalid");
296 lp
->region_size
= arg_uint_value(cmd
, regionsize_ARG
, 0);
298 region_size
= 2 * find_config_tree_int(cmd
,
299 "activation/mirror_region_size",
300 DEFAULT_MIRROR_REGION_SIZE
);
301 if (region_size
< 0) {
302 log_error("Negative regionsize in "
303 "configuration file is invalid");
306 lp
->region_size
= region_size
;
309 if (lp
->region_size
% (pagesize
>> SECTOR_SHIFT
)) {
310 log_error("Region size (%" PRIu32
") must be "
311 "a multiple of machine memory "
313 lp
->region_size
, pagesize
>> SECTOR_SHIFT
);
317 if (lp
->region_size
& (lp
->region_size
- 1)) {
318 log_error("Region size (%" PRIu32
319 ") must be a power of 2", lp
->region_size
);
323 if (!lp
->region_size
) {
324 log_error("Non-zero region size must be supplied.");
328 /* Default is never striped, regardless of existing LV configuration. */
329 if (!get_stripe_params(cmd
, &lp
->stripes
, &lp
->stripe_size
)) {
334 lp
->segtype
= get_segtype_from_string(cmd
, arg_str_value(cmd
, type_ARG
, "mirror"));
339 if (activation() && lp
->segtype
&& lp
->segtype
->ops
->target_present
&&
340 !lp
->segtype
->ops
->target_present(cmd
, NULL
, NULL
)) {
341 log_error("%s: Required device-mapper target(s) not "
342 "detected in your kernel", lp
->segtype
->name
);
346 if (!_lvconvert_name_params(lp
, cmd
, &argc
, &argv
))
355 static struct volume_group
*_get_lvconvert_vg(struct cmd_context
*cmd
,
357 const char *uuid
__attribute__((unused
)))
361 if (name
&& !strchr(name
, '/'))
362 return vg_read_for_update(cmd
, name
, NULL
, 0);
364 /* 'name' is the full LV name; must extract_vgname() */
365 return vg_read_for_update(cmd
, extract_vgname(cmd
, name
),
369 static struct logical_volume
*_get_lvconvert_lv(struct cmd_context
*cmd
__attribute__((unused
)),
370 struct volume_group
*vg
,
373 uint64_t lv_type
__attribute__((unused
)))
375 struct logical_volume
*lv
= find_lv(vg
, name
);
377 if (!lv
|| (uuid
&& strcmp(uuid
, (char *)&lv
->lvid
)))
383 static int _finish_lvconvert_mirror(struct cmd_context
*cmd
,
384 struct volume_group
*vg
,
385 struct logical_volume
*lv
,
386 struct dm_list
*lvs_changed
__attribute__((unused
)))
390 if (!(lv
->status
& CONVERTING
))
393 if (!collapse_mirrored_lv(lv
)) {
394 log_error("Failed to remove temporary sync layer.");
398 lv
->status
&= ~CONVERTING
;
400 log_very_verbose("Updating logical volume \"%s\" on disk(s)", lv
->name
);
405 if (!suspend_lv(cmd
, lv
)) {
406 log_error("Failed to lock %s", lv
->name
);
411 if (!vg_commit(vg
)) {
416 log_very_verbose("Updating \"%s\" in kernel", lv
->name
);
418 if (!resume_lv(cmd
, lv
)) {
419 log_error("Problem reactivating %s", lv
->name
);
424 log_print("Logical volume %s converted.", lv
->name
);
430 static int _finish_lvconvert_merge(struct cmd_context
*cmd
,
431 struct volume_group
*vg
,
432 struct logical_volume
*lv
,
433 struct dm_list
*lvs_changed
__attribute__((unused
)))
435 struct lv_segment
*snap_seg
= find_merging_cow(lv
);
437 log_error("Logical volume %s has no merging snapshot.", lv
->name
);
441 log_print("Merge of snapshot into logical volume %s has finished.", lv
->name
);
442 if (!lv_remove_single(cmd
, snap_seg
->cow
, DONT_PROMPT
)) {
443 log_error("Could not remove snapshot %s merged into %s.",
444 snap_seg
->cow
->name
, lv
->name
);
451 static progress_t
_poll_merge_progress(struct cmd_context
*cmd
,
452 struct logical_volume
*lv
,
453 const char *name
__attribute__((unused
)),
454 struct daemon_parms
*parms
)
456 percent_t percent
= PERCENT_0
;
458 if (!lv_snapshot_percent(lv
, &percent
)) {
459 log_error("%s: Failed query for merging percentage. Aborting merge.", lv
->name
);
460 return PROGRESS_CHECK_FAILED
;
461 } else if (percent
== PERCENT_INVALID
) {
462 log_error("%s: Merging snapshot invalidated. Aborting merge.", lv
->name
);
463 return PROGRESS_CHECK_FAILED
;
464 } else if (percent
== PERCENT_MERGE_FAILED
) {
465 log_error("%s: Merge failed. Retry merge or inspect manually.", lv
->name
);
466 return PROGRESS_CHECK_FAILED
;
469 if (parms
->progress_display
)
470 log_print("%s: %s: %.1f%%", lv
->name
, parms
->progress_title
,
471 percent_to_float(percent
));
473 log_verbose("%s: %s: %.1f%%", lv
->name
, parms
->progress_title
,
474 percent_to_float(percent
));
476 if (percent
== PERCENT_0
)
477 return PROGRESS_FINISHED_ALL
;
479 return PROGRESS_UNFINISHED
;
482 static struct poll_functions _lvconvert_mirror_fns
= {
483 .get_copy_vg
= _get_lvconvert_vg
,
484 .get_copy_lv
= _get_lvconvert_lv
,
485 .poll_progress
= poll_mirror_progress
,
486 .finish_copy
= _finish_lvconvert_mirror
,
489 static struct poll_functions _lvconvert_merge_fns
= {
490 .get_copy_vg
= _get_lvconvert_vg
,
491 .get_copy_lv
= _get_lvconvert_lv
,
492 .poll_progress
= _poll_merge_progress
,
493 .finish_copy
= _finish_lvconvert_merge
,
496 int lvconvert_poll(struct cmd_context
*cmd
, struct logical_volume
*lv
,
500 * FIXME allocate an "object key" structure with split
501 * out members (vg_name, lv_name, uuid, etc) and pass that
502 * around the lvconvert and polldaemon code
503 * - will avoid needless work, e.g. extract_vgname()
504 * - unfortunately there are enough overloaded "name" dragons in
505 * the polldaemon, lvconvert, pvmove code that a comprehensive
506 * audit/rework is needed
508 int len
= strlen(lv
->vg
->name
) + strlen(lv
->name
) + 2;
509 char *uuid
= alloca(sizeof(lv
->lvid
));
510 char *lv_full_name
= alloca(len
);
512 if (!uuid
|| !lv_full_name
)
515 if (dm_snprintf(lv_full_name
, len
, "%s/%s", lv
->vg
->name
, lv
->name
) < 0)
518 memcpy(uuid
, &lv
->lvid
, sizeof(lv
->lvid
));
520 if (!lv_is_merging_origin(lv
))
521 return poll_daemon(cmd
, lv_full_name
, uuid
, background
, 0,
522 &_lvconvert_mirror_fns
, "Converted");
524 return poll_daemon(cmd
, lv_full_name
, uuid
, background
, 0,
525 &_lvconvert_merge_fns
, "Merged");
528 static int _insert_lvconvert_layer(struct cmd_context
*cmd
,
529 struct logical_volume
*lv
)
531 char *format
, *layer_name
;
536 * We would like to give the same number for this layer
537 * and the newly added mimage.
538 * However, LV name of newly added mimage is determined *after*
539 * the LV name of this layer is determined.
541 * So, use generate_lv_name() to generate mimage name first
542 * and take the number from it.
545 len
= strlen(lv
->name
) + 32;
546 if (!(format
= alloca(len
)) ||
547 !(layer_name
= alloca(len
)) ||
548 dm_snprintf(format
, len
, "%s_mimage_%%d", lv
->name
) < 0) {
549 log_error("lvconvert: layer name allocation failed.");
553 if (!generate_lv_name(lv
->vg
, format
, layer_name
, len
) ||
554 sscanf(layer_name
, format
, &i
) != 1) {
555 log_error("lvconvert: layer name generation failed.");
559 if (dm_snprintf(layer_name
, len
, MIRROR_SYNC_LAYER
"_%d", i
) < 0) {
560 log_error("layer name allocation failed.");
564 if (!insert_layer_for_lv(cmd
, lv
, 0, layer_name
)) {
565 log_error("Failed to insert resync layer");
572 static int _failed_mirrors_count(struct logical_volume
*lv
)
574 struct lv_segment
*lvseg
;
578 dm_list_iterate_items(lvseg
, &lv
->segments
) {
579 if (!seg_is_mirrored(lvseg
))
581 for (s
= 0; s
< lvseg
->area_count
; s
++) {
582 if (seg_type(lvseg
, s
) == AREA_LV
) {
583 if (is_temporary_mirror_layer(seg_lv(lvseg
, s
)))
584 ret
+= _failed_mirrors_count(seg_lv(lvseg
, s
));
585 else if (seg_lv(lvseg
, s
)->status
& PARTIAL_LV
)
587 else if (seg_type(lvseg
, s
) == AREA_PV
&&
588 is_missing_pv(seg_pv(lvseg
, s
)))
597 static int _failed_logs_count(struct logical_volume
*lv
)
601 struct logical_volume
*log_lv
= first_seg(lv
)->log_lv
;
602 if (log_lv
&& (log_lv
->status
& PARTIAL_LV
)) {
603 if (log_lv
->status
& MIRRORED
)
604 ret
+= _failed_mirrors_count(log_lv
);
608 for (s
= 0; s
< first_seg(lv
)->area_count
; s
++) {
609 if (seg_type(first_seg(lv
), s
) == AREA_LV
&&
610 is_temporary_mirror_layer(seg_lv(first_seg(lv
), s
)))
611 ret
+= _failed_logs_count(seg_lv(first_seg(lv
), s
));
617 static struct dm_list
*_failed_pv_list(struct volume_group
*vg
)
619 struct dm_list
*failed_pvs
;
620 struct pv_list
*pvl
, *new_pvl
;
622 if (!(failed_pvs
= dm_pool_alloc(vg
->vgmem
, sizeof(*failed_pvs
)))) {
623 log_error("Allocation of list of failed_pvs failed.");
627 dm_list_init(failed_pvs
);
629 dm_list_iterate_items(pvl
, &vg
->pvs
) {
630 if (!is_missing_pv(pvl
->pv
))
634 * Finally, --repair will remove empty PVs.
635 * But we only want remove these which are output of repair,
636 * Do not count these which are already empty here.
637 * FIXME: code should traverse PV in LV not in whole VG.
638 * FIXME: layer violation? should it depend on vgreduce --removemising?
640 if (pvl
->pv
->pe_alloc_count
== 0)
643 if (!(new_pvl
= dm_pool_alloc(vg
->vgmem
, sizeof(*new_pvl
)))) {
644 log_error("Allocation of failed_pvs list entry failed.");
647 new_pvl
->pv
= pvl
->pv
;
648 dm_list_add(failed_pvs
, &new_pvl
->list
);
654 static int _is_partial_lv(struct logical_volume
*lv
,
655 void *baton
__attribute__((unused
)))
657 return lv
->status
& PARTIAL_LV
;
661 * Walk down the stacked mirror LV to the original mirror LV.
663 static struct logical_volume
*_original_lv(struct logical_volume
*lv
)
665 struct logical_volume
*next_lv
= lv
, *tmp_lv
;
667 while ((tmp_lv
= find_temporary_mirror(next_lv
)))
673 static void _lvconvert_mirrors_repair_ask(struct cmd_context
*cmd
,
674 int failed_log
, int failed_mirrors
,
675 int *replace_log
, int *replace_mirrors
)
677 const char *leg_policy
= NULL
, *log_policy
= NULL
;
679 int force
= arg_count(cmd
, force_ARG
);
680 int yes
= arg_count(cmd
, yes_ARG
);
682 *replace_log
= *replace_mirrors
= 1;
684 if (arg_count(cmd
, use_policies_ARG
)) {
685 leg_policy
= find_config_tree_str(cmd
,
686 "activation/mirror_image_fault_policy", NULL
);
688 leg_policy
= find_config_tree_str(cmd
,
689 "activation/mirror_device_fault_policy",
690 DEFAULT_MIRROR_DEVICE_FAULT_POLICY
);
691 log_policy
= find_config_tree_str(cmd
,
692 "activation/mirror_log_fault_policy",
693 DEFAULT_MIRROR_LOG_FAULT_POLICY
);
694 *replace_mirrors
= strcmp(leg_policy
, "remove");
695 *replace_log
= strcmp(log_policy
, "remove");
702 if (force
!= PROMPT
) {
703 *replace_log
= *replace_mirrors
= 0;
708 yes_no_prompt("Attempt to replace failed mirror log? [y/n]: ") == 'n') {
712 if (failed_mirrors
&&
713 yes_no_prompt("Attempt to replace failed mirror images "
714 "(requires full device resync)? [y/n]: ") == 'n') {
715 *replace_mirrors
= 0;
723 * Get the number of on-disk copies of the log.
728 static int _get_log_count(struct logical_volume
*lv
)
730 struct logical_volume
*log_lv
;
732 log_lv
= first_seg(_original_lv(lv
))->log_lv
;
734 return lv_mirror_count(log_lv
);
739 static int _lv_update_mirrored_log(struct logical_volume
*lv
,
740 struct dm_list
*operable_pvs
,
744 struct logical_volume
*log_lv
;
747 * When log_count is 0, mirrored log doesn't need to be
748 * updated here but it will be removed later.
753 log_lv
= first_seg(_original_lv(lv
))->log_lv
;
754 if (!log_lv
|| !(log_lv
->status
& MIRRORED
))
757 old_log_count
= _get_log_count(lv
);
758 if (old_log_count
== log_count
)
761 /* Reducing redundancy of the log */
762 return remove_mirror_images(log_lv
, log_count
,
763 is_mirror_image_removable
,
767 static int _reload_lv(struct cmd_context
*cmd
, struct logical_volume
*lv
);
768 static int _lv_update_log_type(struct cmd_context
*cmd
,
769 struct lvconvert_params
*lp
,
770 struct logical_volume
*lv
,
771 struct dm_list
*operable_pvs
,
775 uint32_t region_size
= (lp
) ? lp
->region_size
:
776 first_seg(lv
)->region_size
;
777 alloc_policy_t alloc
= (lp
) ? lp
->alloc
: lv
->alloc
;
778 struct logical_volume
*original_lv
;
779 struct logical_volume
*log_lv
;
781 old_log_count
= _get_log_count(lv
);
782 if (old_log_count
== log_count
)
785 original_lv
= _original_lv(lv
);
786 /* Remove an existing log completely */
788 if (!remove_mirror_log(cmd
, original_lv
, operable_pvs
,
789 arg_count(cmd
, yes_ARG
) ||
790 arg_count(cmd
, force_ARG
)))
795 log_lv
= first_seg(original_lv
)->log_lv
;
797 /* Adding redundancy to the log */
798 if (old_log_count
< log_count
) {
799 region_size
= adjusted_mirror_region_size(lv
->vg
->extent_size
,
803 if (!add_mirror_log(cmd
, original_lv
, log_count
,
804 region_size
, operable_pvs
, alloc
))
807 * FIXME: This simple approach won't work in cluster mirrors,
808 * but it doesn't matter because we don't support
809 * mirrored logs in cluster mirrors.
812 return _reload_lv(cmd
, log_lv
);
816 /* Reducing redundancy of the log */
817 return remove_mirror_images(log_lv
, log_count
,
818 is_mirror_image_removable
, operable_pvs
, 1U);
822 * Reomove missing and empty PVs from VG, if are also in provided list
824 static void _remove_missing_empty_pv(struct volume_group
*vg
, struct dm_list
*remove_pvs
)
826 struct pv_list
*pvl
, *pvl_vg
, *pvlt
;
832 dm_list_iterate_items(pvl
, remove_pvs
) {
833 dm_list_iterate_items_safe(pvl_vg
, pvlt
, &vg
->pvs
) {
834 if (!id_equal(&pvl
->pv
->id
, &pvl_vg
->pv
->id
) ||
835 !is_missing_pv(pvl_vg
->pv
) ||
836 pvl_vg
->pv
->pe_alloc_count
!= 0)
839 /* FIXME: duplication of vgreduce code, move this to library */
840 vg
->free_count
-= pvl_vg
->pv
->pe_count
;
841 vg
->extent_count
-= pvl_vg
->pv
->pe_count
;
842 del_pvl_from_vgs(vg
, pvl_vg
);
843 free_pv_fid(pvl_vg
->pv
);
850 if (!vg_write(vg
) || !vg_commit(vg
)) {
854 log_warn("%d missing and now unallocated Physical Volumes removed from VG.", removed
);
859 * _lvconvert_mirrors_parse_params
861 * This function performs the following:
862 * 1) Gets the old values of mimage and log counts
863 * 2) Parses the CLI args to find the new desired values
864 * 3) Adjusts 'lp->mirrors' to the appropriate absolute value.
865 * (Remember, 'lp->mirrors' is specified in terms of the number of "copies"
866 * vs. the number of mimages. It can also be a relative value.)
867 * 4) Sets 'lp->need_polling' if collapsing
868 * 5) Validates other mirror params
870 * Returns: 1 on success, 0 on error
872 static int _lvconvert_mirrors_parse_params(struct cmd_context
*cmd
,
873 struct logical_volume
*lv
,
874 struct lvconvert_params
*lp
,
875 uint32_t *old_mimage_count
,
876 uint32_t *old_log_count
,
877 uint32_t *new_mimage_count
,
878 uint32_t *new_log_count
)
880 int repair
= arg_count(cmd
, repair_ARG
);
881 const char *mirrorlog
;
882 *old_mimage_count
= lv_mirror_count(lv
);
883 *old_log_count
= _get_log_count(lv
);
886 * Collapsing a stack of mirrors:
888 * If called with no argument, try collapsing the resync layers
890 if (!arg_count(cmd
, mirrors_ARG
) && !arg_count(cmd
, mirrorlog_ARG
) &&
891 !arg_count(cmd
, corelog_ARG
) && !arg_count(cmd
, regionsize_ARG
) &&
892 !arg_count(cmd
, splitmirrors_ARG
) && !repair
) {
893 *new_mimage_count
= *old_mimage_count
;
894 *new_log_count
= *old_log_count
;
896 if (find_temporary_mirror(lv
) || (lv
->status
& CONVERTING
))
897 lp
->need_polling
= 1;
901 if ((arg_count(cmd
, mirrors_ARG
) && repair
) ||
902 (arg_count(cmd
, mirrorlog_ARG
) && repair
) ||
903 (arg_count(cmd
, corelog_ARG
) && repair
)) {
904 log_error("--repair cannot be used with --mirrors, --mirrorlog,"
909 if (arg_count(cmd
, mirrorlog_ARG
) && arg_count(cmd
, corelog_ARG
)) {
910 log_error("--mirrorlog and --corelog are incompatible");
915 * Adjusting mimage count?
917 if (!arg_count(cmd
, mirrors_ARG
) && !arg_count(cmd
, splitmirrors_ARG
))
918 lp
->mirrors
= *old_mimage_count
;
919 else if (lp
->mirrors_sign
== SIGN_PLUS
)
920 lp
->mirrors
= *old_mimage_count
+ lp
->mirrors
;
921 else if (lp
->mirrors_sign
== SIGN_MINUS
)
922 lp
->mirrors
= (*old_mimage_count
> lp
->mirrors
) ?
923 *old_mimage_count
- lp
->mirrors
: 0;
927 *new_mimage_count
= lp
->mirrors
;
929 /* Too many mimages? */
930 if (lp
->mirrors
> DEFAULT_MIRROR_MAX_IMAGES
) {
931 log_error("Only up to %d images in mirror supported currently.",
932 DEFAULT_MIRROR_MAX_IMAGES
);
936 /* Did the user try to subtract more legs than available? */
937 if (lp
->mirrors
< 1) {
938 log_error("Unable to reduce images by specified amount - only %d in %s",
939 *old_mimage_count
, lv
->name
);
944 * FIXME: It would be nice to say what we are adjusting to, but
945 * I really don't know whether to specify the # of copies or mimages.
947 if (*old_mimage_count
!= *new_mimage_count
)
948 log_verbose("Adjusting mirror image count of %s", lv
->name
);
953 * If we are converting from a mirror to another mirror or simply
954 * changing the log type, we start by assuming they want the log
955 * type the same and then parse the given args. OTOH, If we are
956 * converting from linear to mirror, then we start from the default
957 * position that the user would like a 'disk' log.
959 *new_log_count
= (*old_mimage_count
> 1) ? *old_log_count
: 1;
960 if (!arg_count(cmd
, corelog_ARG
) && !arg_count(cmd
, mirrorlog_ARG
))
963 if (arg_count(cmd
, corelog_ARG
))
966 mirrorlog
= arg_str_value(cmd
, mirrorlog_ARG
,
967 !*new_log_count
? "core" : DEFAULT_MIRRORLOG
);
969 if (!strcmp("mirrored", mirrorlog
))
971 else if (!strcmp("disk", mirrorlog
))
973 else if (!strcmp("core", mirrorlog
))
976 log_error("Unknown mirrorlog type: %s", mirrorlog
);
981 * No mirrored logs for cluster mirrors until
982 * log daemon is multi-threaded.
984 if ((*new_log_count
== 2) && vg_is_clustered(lv
->vg
)) {
985 log_error("Log type, \"mirrored\", is unavailable to cluster mirrors");
989 log_verbose("Setting logging type to %s", mirrorlog
);
992 * Region size must not change on existing mirrors
994 if (arg_count(cmd
, regionsize_ARG
) && (lv
->status
& MIRRORED
) &&
995 (lp
->region_size
!= first_seg(lv
)->region_size
)) {
996 log_error("Mirror log region size cannot be changed on "
997 "an existing mirror.");
1002 * For the most part, we cannot handle multi-segment mirrors. Bail out
1003 * early if we have encountered one.
1005 if ((lv
->status
& MIRRORED
) && dm_list_size(&lv
->segments
) != 1) {
1006 log_error("Logical volume %s has multiple "
1007 "mirror segments.", lv
->name
);
1014 static int _reload_lv(struct cmd_context
*cmd
, struct logical_volume
*lv
)
1016 log_very_verbose("Updating logical volume \"%s\" on disk(s)", lv
->name
);
1018 if (!vg_write(lv
->vg
))
1021 if (!suspend_lv(cmd
, lv
)) {
1022 log_error("Failed to lock %s", lv
->name
);
1027 if (!vg_commit(lv
->vg
)) {
1028 if (!resume_lv(cmd
, lv
))
1033 log_very_verbose("Updating \"%s\" in kernel", lv
->name
);
1035 if (!resume_lv(cmd
, lv
)) {
1036 log_error("Problem reactivating %s", lv
->name
);
1043 * _lvconvert_mirrors_aux
1045 * Add/remove mirror images and adjust log type. 'operable_pvs'
1046 * are the set of PVs open to removal or allocation - depending
1047 * on the operation being performed.
1049 static int _lvconvert_mirrors_aux(struct cmd_context
*cmd
,
1050 struct logical_volume
*lv
,
1051 struct lvconvert_params
*lp
,
1052 struct dm_list
*operable_pvs
,
1053 uint32_t new_mimage_count
,
1054 uint32_t new_log_count
)
1056 uint32_t region_size
;
1057 struct lv_segment
*seg
;
1058 struct logical_volume
*layer_lv
;
1059 uint32_t old_mimage_count
= lv_mirror_count(lv
);
1060 uint32_t old_log_count
= _get_log_count(lv
);
1062 if ((lp
->mirrors
== 1) && !(lv
->status
& MIRRORED
)) {
1063 log_error("Logical volume %s is already not mirrored.",
1068 region_size
= adjusted_mirror_region_size(lv
->vg
->extent_size
,
1073 operable_pvs
= lp
->pvh
;
1075 seg
= first_seg(lv
);
1078 * Up-convert from linear to mirror
1080 if (!(lv
->status
& MIRRORED
)) {
1081 /* FIXME Share code with lvcreate */
1084 * FIXME should we give not only lp->pvh, but also all PVs
1085 * currently taken by the mirror? Would make more sense from
1088 if (!lv_add_mirrors(cmd
, lv
, new_mimage_count
- 1, lp
->stripes
,
1089 lp
->stripe_size
, region_size
, new_log_count
, operable_pvs
,
1090 lp
->alloc
, MIRROR_BY_LV
))
1093 if (lp
->wait_completion
)
1094 lp
->need_polling
= 1;
1100 * Up-convert m-way mirror to n-way mirror
1102 if (new_mimage_count
> old_mimage_count
) {
1103 if (lv
->status
& LV_NOTSYNCED
) {
1104 log_error("Can't add mirror to out-of-sync mirrored "
1105 "LV: use lvchange --resync first.");
1110 * We allow snapshots of mirrors, but for now, we
1111 * do not allow up converting mirrors that are under
1112 * snapshots. The layering logic is somewhat complex,
1113 * and preliminary test show that the conversion can't
1114 * seem to get the correct %'age of completion.
1116 if (lv_is_origin(lv
)) {
1117 log_error("Can't add additional mirror images to "
1118 "mirrors that are under snapshots");
1123 * Is there already a convert in progress? We do not
1124 * currently allow more than one.
1126 if (find_temporary_mirror(lv
) || (lv
->status
& CONVERTING
)) {
1127 log_error("%s is already being converted. Unable to start another conversion.",
1133 * Log addition/removal should be done before the layer
1134 * insertion to make the end result consistent with
1135 * linear-to-mirror conversion.
1137 if (!_lv_update_log_type(cmd
, lp
, lv
,
1138 operable_pvs
, new_log_count
)) {
1143 /* Insert a temporary layer for syncing,
1144 * only if the original lv is using disk log. */
1145 if (seg
->log_lv
&& !_insert_lvconvert_layer(cmd
, lv
)) {
1146 log_error("Failed to insert resync layer");
1150 /* FIXME: can't have multiple mlogs. force corelog. */
1151 if (!lv_add_mirrors(cmd
, lv
,
1152 new_mimage_count
- old_mimage_count
,
1153 lp
->stripes
, lp
->stripe_size
,
1154 region_size
, 0U, operable_pvs
, lp
->alloc
,
1156 layer_lv
= seg_lv(first_seg(lv
), 0);
1157 if (!remove_layer_from_lv(lv
, layer_lv
) ||
1158 !deactivate_lv(cmd
, layer_lv
) ||
1159 !lv_remove(layer_lv
) || !vg_write(lv
->vg
) ||
1160 !vg_commit(lv
->vg
)) {
1161 log_error("ABORTING: Failed to remove "
1162 "temporary mirror layer %s.",
1164 log_error("Manual cleanup with vgcfgrestore "
1165 "and dmsetup may be required.");
1172 lv
->status
|= CONVERTING
;
1173 lp
->need_polling
= 1;
1175 goto out_skip_log_convert
;
1179 * Down-convert (reduce # of mimages).
1181 if (new_mimage_count
< old_mimage_count
) {
1182 uint32_t nmc
= old_mimage_count
- new_mimage_count
;
1183 uint32_t nlc
= (!new_log_count
|| lp
->mirrors
== 1) ? 1U : 0U;
1185 /* FIXME: Why did nlc used to be calculated that way? */
1187 /* Reduce number of mirrors */
1188 if (lp
->keep_mimages
) {
1189 if (arg_count(cmd
, trackchanges_ARG
)) {
1190 log_error("--trackchanges is not available "
1191 "to 'mirror' segment type");
1194 if (!lv_split_mirror_images(lv
, lp
->lv_split_name
,
1197 } else if (!lv_remove_mirrors(cmd
, lv
, nmc
, nlc
,
1198 is_mirror_image_removable
, operable_pvs
, 0))
1201 goto out
; /* Just in case someone puts code between */
1206 * Converting the log type
1208 if ((lv
->status
& MIRRORED
) && (old_log_count
!= new_log_count
)) {
1209 if (!_lv_update_log_type(cmd
, lp
, lv
,
1210 operable_pvs
, new_log_count
)) {
1216 out_skip_log_convert
:
1218 if (!_reload_lv(cmd
, lv
))
1224 int mirror_remove_missing(struct cmd_context
*cmd
,
1225 struct logical_volume
*lv
, int force
)
1227 struct dm_list
*failed_pvs
;
1228 int log_count
= _get_log_count(lv
) - _failed_logs_count(lv
);
1230 if (!(failed_pvs
= _failed_pv_list(lv
->vg
)))
1233 /* No point in keeping a log if the result is not a mirror */
1234 if (_failed_mirrors_count(lv
) + 1 >= lv_mirror_count(lv
))
1237 if (force
&& _failed_mirrors_count(lv
) == lv_mirror_count(lv
)) {
1238 log_error("No usable images left in %s.", lv
->name
);
1239 return lv_remove_with_dependencies(cmd
, lv
, DONT_PROMPT
, 0);
1243 * We must adjust the log first, or the entire mirror
1244 * will get stuck during a suspend.
1246 if (!_lv_update_mirrored_log(lv
, failed_pvs
, log_count
))
1249 if (_failed_mirrors_count(lv
) > 0 &&
1250 !lv_remove_mirrors(cmd
, lv
, _failed_mirrors_count(lv
),
1251 log_count
? 0U : 1U,
1252 _is_partial_lv
, NULL
, 0))
1255 if (!_lv_update_log_type(cmd
, NULL
, lv
, failed_pvs
,
1259 if (!_reload_lv(cmd
, lv
))
1266 * _lvconvert_mirrors_repair
1268 * This function operates in two phases. First, all of the bad
1269 * devices are removed from the mirror. Then, if desired by the
1270 * user, the devices are replaced.
1272 * 'old_mimage_count' and 'old_log_count' are there so we know
1273 * what to convert to after the removal of devices.
1275 static int _lvconvert_mirrors_repair(struct cmd_context
*cmd
,
1276 struct logical_volume
*lv
,
1277 struct lvconvert_params
*lp
)
1279 int failed_logs
= 0;
1280 int failed_mimages
= 0;
1281 int replace_logs
= 0;
1282 int replace_mimages
= 0;
1285 uint32_t original_mimages
= lv_mirror_count(lv
);
1286 uint32_t original_logs
= _get_log_count(lv
);
1288 cmd
->handles_missing_pvs
= 1;
1289 cmd
->partial_activation
= 1;
1290 lp
->need_polling
= 0;
1292 lv_check_transient(lv
); /* TODO check this in lib for all commands? */
1294 if (!(lv
->status
& PARTIAL_LV
)) {
1295 log_error("%s is consistent. Nothing to repair.", lv
->name
);
1299 failed_mimages
= _failed_mirrors_count(lv
);
1300 failed_logs
= _failed_logs_count(lv
);
1302 mirror_remove_missing(cmd
, lv
, 0);
1305 log_error("Mirror status: %d of %d images failed.",
1306 failed_mimages
, original_mimages
);
1309 * Count the failed log devices
1312 log_error("Mirror log status: %d of %d images failed.",
1313 failed_logs
, original_logs
);
1316 * Find out our policies
1318 _lvconvert_mirrors_repair_ask(cmd
, failed_logs
, failed_mimages
,
1319 &replace_logs
, &replace_mimages
);
1322 * Second phase - replace faulty devices
1324 lp
->mirrors
= replace_mimages
? original_mimages
: (original_mimages
- failed_mimages
);
1327 * It does not make sense to replace the log if the volume is no longer
1330 if (lp
->mirrors
== 1)
1333 log_count
= replace_logs
? original_logs
: (original_logs
- failed_logs
);
1335 while (replace_mimages
|| replace_logs
) {
1336 log_warn("Trying to up-convert to %d images, %d logs.", lp
->mirrors
, log_count
);
1337 if (_lvconvert_mirrors_aux(cmd
, lv
, lp
, NULL
,
1338 lp
->mirrors
, log_count
))
1341 if (lp
->mirrors
> 2)
1343 else if (log_count
> 0)
1346 break; /* nowhere to go, anymore... */
1350 if (replace_mimages
&& lv_mirror_count(lv
) != original_mimages
)
1351 log_warn("WARNING: Failed to replace %d of %d images in volume %s",
1352 original_mimages
- lv_mirror_count(lv
), original_mimages
, lv
->name
);
1353 if (replace_logs
&& _get_log_count(lv
) != original_logs
)
1354 log_warn("WARNING: Failed to replace %d of %d logs in volume %s",
1355 original_logs
- _get_log_count(lv
), original_logs
, lv
->name
);
1357 /* if (!arg_count(cmd, use_policies_ARG) && (lp->mirrors != old_mimage_count
1358 || log_count != old_log_count))
1365 * _lvconvert_mirrors
1367 * Determine what is being done. Are we doing a conversion, repair, or
1368 * collapsing a stack? Once determined, call helper functions.
1370 static int _lvconvert_mirrors(struct cmd_context
*cmd
,
1371 struct logical_volume
*lv
,
1372 struct lvconvert_params
*lp
)
1374 int repair
= arg_count(cmd
, repair_ARG
);
1375 uint32_t old_mimage_count
;
1376 uint32_t old_log_count
;
1377 uint32_t new_mimage_count
;
1378 uint32_t new_log_count
;
1380 if (lp
->merge_mirror
) {
1381 log_error("Unable to merge mirror images"
1382 "of segment type 'mirror'");
1386 /* Adjust mimage and/or log count */
1387 if (!_lvconvert_mirrors_parse_params(cmd
, lv
, lp
,
1388 &old_mimage_count
, &old_log_count
,
1389 &new_mimage_count
, &new_log_count
))
1392 if (((old_mimage_count
< new_mimage_count
&& old_log_count
> new_log_count
) ||
1393 (old_mimage_count
> new_mimage_count
&& old_log_count
< new_log_count
)) &&
1395 log_error("Cannot both allocate and free extents when "
1396 "specifying physical volumes to use.");
1397 log_error("Please specify the operation in two steps.");
1401 /* Nothing to do? (Probably finishing collapse.) */
1402 if ((old_mimage_count
== new_mimage_count
) &&
1403 (old_log_count
== new_log_count
) && !repair
)
1407 return _lvconvert_mirrors_repair(cmd
, lv
, lp
);
1409 if (!_lvconvert_mirrors_aux(cmd
, lv
, lp
, NULL
,
1410 new_mimage_count
, new_log_count
))
1413 if (!lp
->need_polling
)
1414 log_print("Logical volume %s converted.", lv
->name
);
1420 static int is_valid_raid_conversion(const struct segment_type
*from_segtype
,
1421 const struct segment_type
*to_segtype
)
1423 if (from_segtype
== to_segtype
)
1426 if (!segtype_is_raid(from_segtype
) && !segtype_is_raid(to_segtype
))
1427 return_0
; /* Not converting to or from RAID? */
1432 static void _lvconvert_raid_repair_ask(struct cmd_context
*cmd
, int *replace_dev
)
1434 const char *dev_policy
= NULL
;
1436 int force
= arg_count(cmd
, force_ARG
);
1437 int yes
= arg_count(cmd
, yes_ARG
);
1441 if (arg_count(cmd
, use_policies_ARG
)) {
1442 dev_policy
= find_config_tree_str(cmd
, "activation/raid_fault_policy", DEFAULT_RAID_FAULT_POLICY
);
1444 if (!strcmp(dev_policy
, "allocate") ||
1445 !strcmp(dev_policy
, "replace"))
1447 /* else if (!strcmp(dev_policy, "anything_else")) -- ignore */
1456 if (force
!= PROMPT
)
1459 if (yes_no_prompt("Attempt to replace failed RAID images "
1460 "(requires full device resync)? [y/n]: ") == 'y') {
1465 static int lvconvert_raid(struct logical_volume
*lv
, struct lvconvert_params
*lp
)
1468 int uninitialized_var(image_count
);
1469 struct dm_list
*failed_pvs
;
1470 struct cmd_context
*cmd
= lv
->vg
->cmd
;
1471 struct lv_segment
*seg
= first_seg(lv
);
1473 if (!arg_count(cmd
, type_ARG
))
1474 lp
->segtype
= seg
->segtype
;
1476 /* Can only change image count for raid1 and linear */
1477 if (arg_count(cmd
, mirrors_ARG
) &&
1478 !seg_is_mirrored(seg
) && !seg_is_linear(seg
)) {
1479 log_error("'--mirrors/-m' is not compatible with %s",
1480 seg
->segtype
->name
);
1484 if (!is_valid_raid_conversion(seg
->segtype
, lp
->segtype
)) {
1485 log_error("Unable to convert %s/%s from %s to %s",
1486 lv
->vg
->name
, lv
->name
,
1487 seg
->segtype
->name
, lp
->segtype
->name
);
1491 /* Change number of RAID1 images */
1492 if (arg_count(cmd
, mirrors_ARG
) || arg_count(cmd
, splitmirrors_ARG
)) {
1493 image_count
= lv_raid_image_count(lv
);
1494 if (lp
->mirrors_sign
== SIGN_PLUS
)
1495 image_count
+= lp
->mirrors
;
1496 else if (lp
->mirrors_sign
== SIGN_MINUS
)
1497 image_count
-= lp
->mirrors
;
1499 image_count
= lp
->mirrors
+ 1;
1501 if (image_count
< 1) {
1502 log_error("Unable to %s images by specified amount",
1503 arg_count(cmd
, splitmirrors_ARG
) ?
1504 "split" : "reduce");
1509 if (lp
->merge_mirror
)
1510 return lv_raid_merge(lv
);
1512 if (arg_count(cmd
, trackchanges_ARG
))
1513 return lv_raid_split_and_track(lv
, lp
->pvh
);
1515 if (arg_count(cmd
, splitmirrors_ARG
))
1516 return lv_raid_split(lv
, lp
->lv_split_name
,
1517 image_count
, lp
->pvh
);
1519 if (arg_count(cmd
, mirrors_ARG
))
1520 return lv_raid_change_image_count(lv
, image_count
, lp
->pvh
);
1522 if (arg_count(cmd
, type_ARG
))
1523 return lv_raid_reshape(lv
, lp
->segtype
);
1525 if (arg_count(cmd
, replace_ARG
))
1526 return lv_raid_replace(lv
, lp
->replace_pvh
, lp
->pvh
);
1528 if (arg_count(cmd
, repair_ARG
)) {
1529 _lvconvert_raid_repair_ask(cmd
, &replace
);
1532 if (!(failed_pvs
= _failed_pv_list(lv
->vg
)))
1535 if (!lv_raid_replace(lv
, failed_pvs
, lp
->pvh
)) {
1536 log_error("Failed to replace faulty devices in"
1537 " %s/%s.", lv
->vg
->name
, lv
->name
);
1541 log_print("Faulty devices in %s/%s successfully"
1542 " replaced.", lv
->vg
->name
, lv
->name
);
1546 /* "warn" if policy not set to replace */
1547 if (arg_count(cmd
, use_policies_ARG
))
1548 log_error("Use 'lvconvert --repair %s/%s' to "
1549 "replace failed device",
1550 lv
->vg
->name
, lv
->name
);
1554 log_error("Conversion operation not yet supported.");
1558 static int lvconvert_snapshot(struct cmd_context
*cmd
,
1559 struct logical_volume
*lv
,
1560 struct lvconvert_params
*lp
)
1562 struct logical_volume
*org
;
1565 if (!(org
= find_lv(lv
->vg
, lp
->origin
))) {
1566 log_error("Couldn't find origin volume '%s'.", lp
->origin
);
1571 log_error("Unable to use \"%s\" as both snapshot and origin.",
1576 if (org
->status
& (LOCKED
|PVMOVE
|MIRRORED
) || lv_is_cow(org
)) {
1577 log_error("Unable to convert an LV into a snapshot of a %s LV.",
1578 org
->status
& LOCKED
? "locked" :
1579 org
->status
& PVMOVE
? "pvmove" :
1580 org
->status
& MIRRORED
? "mirrored" :
1585 if (!lp
->zero
|| !(lv
->status
& LVM_WRITE
))
1586 log_warn("WARNING: \"%s\" not zeroed", lv
->name
);
1587 else if (!set_lv(cmd
, lv
, UINT64_C(0), 0)) {
1588 log_error("Aborting. Failed to wipe snapshot "
1589 "exception store.");
1593 if (!deactivate_lv(cmd
, lv
)) {
1594 log_error("Couldn't deactivate LV %s.", lv
->name
);
1598 if (!vg_add_snapshot(org
, lv
, NULL
, org
->le_count
, lp
->chunk_size
)) {
1599 log_error("Couldn't create snapshot.");
1603 /* store vg on disk(s) */
1604 if (!vg_write(lv
->vg
))
1607 if (!suspend_lv(cmd
, org
)) {
1608 log_error("Failed to suspend origin %s", org
->name
);
1613 if (!vg_commit(lv
->vg
))
1616 if (!resume_lv(cmd
, org
)) {
1617 log_error("Problem reactivating origin %s", org
->name
);
1621 log_print("Logical volume %s converted to snapshot.", lv
->name
);
1628 static int lvconvert_merge(struct cmd_context
*cmd
,
1629 struct logical_volume
*lv
,
1630 struct lvconvert_params
*lp
)
1633 int merge_on_activate
= 0;
1634 struct logical_volume
*origin
= origin_from_cow(lv
);
1635 struct lv_segment
*cow_seg
= find_cow(lv
);
1638 /* Check if merge is possible */
1639 if (lv_is_merging_cow(lv
)) {
1640 log_error("Snapshot %s is already merging", lv
->name
);
1643 if (lv_is_merging_origin(origin
)) {
1644 log_error("Snapshot %s is already merging into the origin",
1645 find_merging_cow(origin
)->cow
->name
);
1650 * Prevent merge with open device(s) as it would likely lead
1651 * to application/filesystem failure. Merge on origin's next
1652 * activation if either the origin or snapshot LV are currently
1655 * FIXME testing open_count is racey; snapshot-merge target's
1656 * constructor and DM should prevent appropriate devices from
1659 if (lv_info(cmd
, origin
, 0, &info
, 1, 0)) {
1660 if (info
.open_count
) {
1661 log_error("Can't merge over open origin volume");
1662 merge_on_activate
= 1;
1665 if (lv_info(cmd
, lv
, 0, &info
, 1, 0)) {
1666 if (info
.open_count
) {
1667 log_print("Can't merge when snapshot is open");
1668 merge_on_activate
= 1;
1672 init_snapshot_merge(cow_seg
, origin
);
1674 /* store vg on disk(s) */
1675 if (!vg_write(lv
->vg
))
1678 if (merge_on_activate
) {
1679 /* commit vg but skip starting the merge */
1680 if (!vg_commit(lv
->vg
))
1683 log_print("Merging of snapshot %s will start "
1684 "next activation.", lv
->name
);
1689 if (!suspend_lv(cmd
, origin
)) {
1690 log_error("Failed to suspend origin %s", origin
->name
);
1695 if (!vg_commit(lv
->vg
)) {
1696 if (!resume_lv(cmd
, origin
))
1701 if (!resume_lv(cmd
, origin
)) {
1702 log_error("Failed to reactivate origin %s", origin
->name
);
1706 lp
->need_polling
= 1;
1707 lp
->lv_to_poll
= origin
;
1710 log_print("Merging of volume %s started.", lv
->name
);
1716 static int _lvconvert_single(struct cmd_context
*cmd
, struct logical_volume
*lv
,
1719 struct lvconvert_params
*lp
= handle
;
1720 struct dm_list
*failed_pvs
;
1722 percent_t snap_percent
;
1724 if (lv
->status
& LOCKED
) {
1725 log_error("Cannot convert locked LV %s", lv
->name
);
1729 if (lv_is_cow(lv
) && !lp
->merge
) {
1730 log_error("Can't convert snapshot logical volume \"%s\"",
1735 if (lv
->status
& PVMOVE
) {
1736 log_error("Unable to convert pvmove LV %s", lv
->name
);
1740 if (arg_count(cmd
, repair_ARG
) &&
1741 !(lv
->status
& MIRRORED
) && !(lv
->status
& RAID
)) {
1742 if (arg_count(cmd
, use_policies_ARG
))
1743 return ECMD_PROCESSED
; /* nothing to be done here */
1744 log_error("Can't repair non-mirrored LV \"%s\".", lv
->name
);
1749 lp
->segtype
= first_seg(lv
)->segtype
;
1752 if (!lv_is_cow(lv
)) {
1753 log_error("Logical volume \"%s\" is not a snapshot",
1757 if (lv_info(lv
->vg
->cmd
, lv
, 0, &info
, 1, 0)
1758 && info
.exists
&& info
.live_table
&&
1759 (!lv_snapshot_percent(lv
, &snap_percent
) ||
1760 snap_percent
== PERCENT_INVALID
)) {
1761 log_error("Unable to merge invalidated snapshot LV \"%s\"", lv
->name
);
1764 if (!archive(lv
->vg
)) {
1768 if (!lvconvert_merge(cmd
, lv
, lp
)) {
1769 log_error("Unable to merge LV \"%s\" into its origin.", lv
->name
);
1772 } else if (lp
->snapshot
) {
1773 if (lv
->status
& MIRRORED
) {
1774 log_error("Unable to convert mirrored LV \"%s\" into a snapshot.", lv
->name
);
1777 if (!archive(lv
->vg
)) {
1781 if (!lvconvert_snapshot(cmd
, lv
, lp
)) {
1785 } else if (segtype_is_raid(lp
->segtype
) ||
1786 (lv
->status
& RAID
) || lp
->merge_mirror
) {
1787 if (!archive(lv
->vg
)) {
1791 if (!lvconvert_raid(lv
, lp
)) {
1796 if (!(failed_pvs
= _failed_pv_list(lv
->vg
))) {
1801 /* If repairing and using policies, remove missing PVs from VG */
1802 if (arg_count(cmd
, repair_ARG
) && arg_count(cmd
, use_policies_ARG
))
1803 _remove_missing_empty_pv(lv
->vg
, failed_pvs
);
1804 } else if (arg_count(cmd
, mirrors_ARG
) ||
1805 arg_count(cmd
, splitmirrors_ARG
) ||
1806 (lv
->status
& MIRRORED
)) {
1807 if (!archive(lv
->vg
)) {
1811 if (!_lvconvert_mirrors(cmd
, lv
, lp
)) {
1816 if (!(failed_pvs
= _failed_pv_list(lv
->vg
))) {
1821 /* If repairing and using policies, remove missing PVs from VG */
1822 if (arg_count(cmd
, repair_ARG
) && arg_count(cmd
, use_policies_ARG
))
1823 _remove_missing_empty_pv(lv
->vg
, failed_pvs
);
1826 return ECMD_PROCESSED
;
1830 * FIXME move to toollib along with the rest of the drop/reacquire
1831 * VG locking that is used by lvconvert_merge_single()
1833 static struct logical_volume
*get_vg_lock_and_logical_volume(struct cmd_context
*cmd
,
1834 const char *vg_name
,
1835 const char *lv_name
)
1838 * Returns NULL if the requested LV doesn't exist;
1839 * otherwise the caller must release_vg(lv->vg)
1840 * - it is also up to the caller to unlock_vg() as needed
1842 struct volume_group
*vg
;
1843 struct logical_volume
* lv
= NULL
;
1845 vg
= _get_lvconvert_vg(cmd
, vg_name
, NULL
);
1846 if (vg_read_error(vg
)) {
1851 if (!(lv
= _get_lvconvert_lv(cmd
, vg
, lv_name
, NULL
, 0))) {
1852 log_error("Can't find LV %s in VG %s", lv_name
, vg_name
);
1853 unlock_and_release_vg(cmd
, vg
, vg_name
);
1860 static int poll_logical_volume(struct cmd_context
*cmd
, struct logical_volume
*lv
,
1861 int wait_completion
)
1865 if (!lv_info(cmd
, lv
, 0, &info
, 0, 0) || !info
.exists
) {
1866 log_print("Conversion starts after activation.");
1867 return ECMD_PROCESSED
;
1869 return lvconvert_poll(cmd
, lv
, wait_completion
? 0 : 1U);
1872 static int lvconvert_single(struct cmd_context
*cmd
, struct lvconvert_params
*lp
)
1874 struct logical_volume
*lv
= NULL
;
1875 int ret
= ECMD_FAILED
;
1876 int saved_ignore_suspended_devices
= ignore_suspended_devices();
1878 if (arg_count(cmd
, repair_ARG
)) {
1879 init_ignore_suspended_devices(1);
1880 cmd
->handles_missing_pvs
= 1;
1883 lv
= get_vg_lock_and_logical_volume(cmd
, lp
->vg_name
, lp
->lv_name
);
1888 * lp->pvh holds the list of PVs available for allocation or removal
1891 if (!(lp
->pvh
= create_pv_list(cmd
->mem
, lv
->vg
, lp
->pv_count
,
1895 lp
->pvh
= &lv
->vg
->pvs
;
1897 if (lp
->replace_pv_count
&&
1898 !(lp
->replace_pvh
= create_pv_list(cmd
->mem
, lv
->vg
,
1899 lp
->replace_pv_count
,
1900 lp
->replace_pvs
, 0)))
1903 lp
->lv_to_poll
= lv
;
1904 ret
= _lvconvert_single(cmd
, lv
, lp
);
1906 unlock_vg(cmd
, lp
->vg_name
);
1908 if (ret
== ECMD_PROCESSED
&& lp
->need_polling
)
1909 ret
= poll_logical_volume(cmd
, lp
->lv_to_poll
,
1910 lp
->wait_completion
);
1914 init_ignore_suspended_devices(saved_ignore_suspended_devices
);
1918 static int lvconvert_merge_single(struct cmd_context
*cmd
, struct logical_volume
*lv
,
1921 struct lvconvert_params
*lp
= handle
;
1922 const char *vg_name
= NULL
;
1923 struct logical_volume
*refreshed_lv
= NULL
;
1927 * FIXME can't trust lv's VG to be current given that caller
1928 * is process_each_lv() -- poll_logical_volume() may have
1929 * already updated the VG's metadata in an earlier iteration.
1930 * - preemptively drop the VG lock, as is needed for
1931 * poll_logical_volume(), refresh LV (and VG in the process).
1933 vg_name
= lv
->vg
->name
;
1934 unlock_vg(cmd
, vg_name
);
1935 refreshed_lv
= get_vg_lock_and_logical_volume(cmd
, vg_name
, lv
->name
);
1936 if (!refreshed_lv
) {
1937 log_error("ABORTING: Can't reread LV %s/%s", vg_name
, lv
->name
);
1941 lp
->lv_to_poll
= refreshed_lv
;
1942 ret
= _lvconvert_single(cmd
, refreshed_lv
, lp
);
1944 if (ret
== ECMD_PROCESSED
&& lp
->need_polling
) {
1946 * Must drop VG lock, because lvconvert_poll() needs it,
1947 * then reacquire it after polling completes
1949 unlock_vg(cmd
, vg_name
);
1951 ret
= poll_logical_volume(cmd
, lp
->lv_to_poll
,
1952 lp
->wait_completion
);
1954 /* use LCK_VG_WRITE to match lvconvert()'s READ_FOR_UPDATE */
1955 if (!lock_vol(cmd
, vg_name
, LCK_VG_WRITE
)) {
1956 log_error("ABORTING: Can't relock VG for %s "
1957 "after polling finished", vg_name
);
1962 release_vg(refreshed_lv
->vg
);
1967 int lvconvert(struct cmd_context
* cmd
, int argc
, char **argv
)
1969 struct lvconvert_params lp
;
1971 if (!_read_params(&lp
, cmd
, argc
, argv
)) {
1973 return EINVALID_CMD_LINE
;
1978 log_error("Please provide logical volume path");
1979 return EINVALID_CMD_LINE
;
1981 return process_each_lv(cmd
, argc
, argv
, READ_FOR_UPDATE
, &lp
,
1982 &lvconvert_merge_single
);
1985 return lvconvert_single(cmd
, &lp
);