4 * Copyright (C) 2003-2004 Sistina Software, Inc. All rights reserved.
5 * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
7 * This file is part of LVM2.
9 * This copyrighted material is made available to anyone wishing to use,
10 * modify, copy, or redistribute it subject to the terms and conditions
11 * of the GNU Lesser General Public License v.2.1.
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program; if not, write to the Free Software Foundation,
15 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include "toolcontext.h"
26 #include "lvm-string.h"
28 #include "locking.h" /* FIXME Should not be used in this file */
31 #include "defaults.h" /* FIXME: should this be defaults.h? */
33 /* These are necessary for _write_log_header() */
35 #define MIRROR_MAGIC 0x4D695272
36 #define MIRROR_DISK_VERSION 2
38 /* These are the flags that represent the mirror failure restoration policies */
39 #define MIRROR_REMOVE 0
40 #define MIRROR_ALLOCATE 1
41 #define MIRROR_ALLOCATE_ANYWHERE 2
44 * Returns true if the lv is temporary mirror layer for resync
46 int is_temporary_mirror_layer(const struct logical_volume
*lv
)
48 if (lv
->status
& MIRROR_IMAGE
49 && lv
->status
& MIRRORED
50 && !(lv
->status
& LOCKED
))
57 * Return a temporary LV for resyncing added mirror image.
58 * Add other mirror legs to lvs list.
60 struct logical_volume
*find_temporary_mirror(const struct logical_volume
*lv
)
62 struct lv_segment
*seg
;
64 if (!(lv
->status
& MIRRORED
))
69 /* Temporary mirror is always area_num == 0 */
70 if (seg_type(seg
, 0) == AREA_LV
&&
71 is_temporary_mirror_layer(seg_lv(seg
, 0)))
72 return seg_lv(seg
, 0);
78 * Returns the number of mirrors of the LV
80 uint32_t lv_mirror_count(const struct logical_volume
*lv
)
82 struct lv_segment
*seg
;
85 if (!(lv
->status
& MIRRORED
))
89 mirrors
= seg
->area_count
;
91 for (s
= 0; s
< seg
->area_count
; s
++) {
92 if (seg_type(seg
, s
) != AREA_LV
)
94 if (is_temporary_mirror_layer(seg_lv(seg
, s
)))
95 mirrors
+= lv_mirror_count(seg_lv(seg
, s
)) - 1;
101 struct lv_segment
*find_mirror_seg(struct lv_segment
*seg
)
103 struct lv_segment
*mirror_seg
;
105 mirror_seg
= get_only_segment_using_this_lv(seg
->lv
);
108 log_error("Failed to find mirror_seg for %s", seg
->lv
->name
);
112 if (!seg_is_mirrored(mirror_seg
)) {
113 log_error("%s on %s is not a mirror segments",
114 mirror_seg
->lv
->name
, seg
->lv
->name
);
122 * Reduce the region size if necessary to ensure
123 * the volume size is a multiple of the region size.
125 uint32_t adjusted_mirror_region_size(uint32_t extent_size
, uint32_t extents
,
126 uint32_t region_size
)
130 region_max
= (1 << (ffs((int)extents
) - 1)) * (uint64_t) extent_size
;
132 if (region_max
< UINT32_MAX
&& region_size
> region_max
) {
133 region_size
= (uint32_t) region_max
;
134 log_print("Using reduced mirror region size of %" PRIu32
135 " sectors", region_size
);
142 * shift_mirror_images
144 * @mimage: The position (index) of the image to move to the end
146 * When dealing with removal of legs, we often move a 'removable leg'
147 * to the back of the 'areas' array. It is critically important not
148 * to simply swap it for the last area in the array. This would have
149 * the affect of reordering the remaining legs - altering position of
150 * the primary. So, we must shuffle all of the areas in the array
151 * to maintain their relative position before moving the 'removable
154 * Short illustration of the problem:
155 * - Mirror consists of legs A, B, C and we want to remove A
156 * - We swap A and C and then remove A, leaving C, B
157 * This scenario is problematic in failure cases where A dies, because
158 * B becomes the primary. If the above happens, we effectively throw
159 * away any changes made between the time of failure and the time of
160 * restructuring the mirror.
162 * So, any time we want to move areas to the end to be removed, use
165 int shift_mirror_images(struct lv_segment
*mirrored_seg
, unsigned mimage
)
168 struct lv_segment_area area
;
170 if (mimage
>= mirrored_seg
->area_count
) {
171 log_error("Invalid index (%u) of mirror image supplied "
172 "to shift_mirror_images()", mimage
);
176 area
= mirrored_seg
->areas
[mimage
];
178 /* Shift remaining images down to fill the hole */
179 for (i
= mimage
+ 1; i
< mirrored_seg
->area_count
; i
++)
180 mirrored_seg
->areas
[i
-1] = mirrored_seg
->areas
[i
];
182 /* Place this one at the end */
183 mirrored_seg
->areas
[i
-1] = area
;
189 * This function writes a new header to the mirror log header to the lv
191 * Returns: 1 on success, 0 on failure
193 static int _write_log_header(struct cmd_context
*cmd
, struct logical_volume
*lv
)
197 struct { /* The mirror log header */
203 log_header
.magic
= xlate32(MIRROR_MAGIC
);
204 log_header
.version
= xlate32(MIRROR_DISK_VERSION
);
205 log_header
.nr_regions
= xlate64((uint64_t)-1);
207 if (!(name
= dm_pool_alloc(cmd
->mem
, PATH_MAX
))) {
208 log_error("Name allocation failed - log header not written (%s)",
213 if (dm_snprintf(name
, PATH_MAX
, "%s%s/%s", cmd
->dev_dir
,
214 lv
->vg
->name
, lv
->name
) < 0) {
215 log_error("Name too long - log header not written (%s)", lv
->name
);
219 log_verbose("Writing log header to device, %s", lv
->name
);
221 if (!(dev
= dev_cache_get(name
, NULL
))) {
222 log_error("%s: not found: log header not written", name
);
226 if (!dev_open_quiet(dev
))
229 if (!dev_write(dev
, UINT64_C(0), sizeof(log_header
), &log_header
)) {
230 log_error("Failed to write log header to %s", name
);
231 dev_close_immediate(dev
);
235 dev_close_immediate(dev
);
241 * Initialize mirror log contents
243 static int _init_mirror_log(struct cmd_context
*cmd
,
244 struct logical_volume
*log_lv
, int in_sync
,
245 struct dm_list
*tags
, int remove_on_failure
)
249 uint32_t orig_status
= log_lv
->status
;
252 if (!activation() && in_sync
) {
253 log_error("Aborting. Unable to create in-sync mirror log "
254 "while activation is disabled.");
258 /* If the LV is active, deactivate it first. */
259 if (lv_info(cmd
, log_lv
, &info
, 0, 0) && info
.exists
) {
260 if (!deactivate_lv(cmd
, log_lv
))
265 /* Temporary make it visible for set_lv() */
266 lv_set_visible(log_lv
);
268 /* Temporary tag mirror log for activation */
269 dm_list_iterate_items(sl
, tags
)
270 if (!str_list_add(cmd
->mem
, &log_lv
->tags
, sl
->str
)) {
271 log_error("Aborting. Unable to tag mirror log.");
275 /* store mirror log on disk(s) */
276 if (!vg_write(log_lv
->vg
) || !vg_commit(log_lv
->vg
))
281 if (!activate_lv(cmd
, log_lv
)) {
282 log_error("Aborting. Failed to activate mirror log.");
286 /* Remove the temporary tags */
287 dm_list_iterate_items(sl
, tags
)
288 if (!str_list_del(&log_lv
->tags
, sl
->str
))
289 log_error("Failed to remove tag %s from mirror log.",
292 if (activation() && !set_lv(cmd
, log_lv
, log_lv
->size
,
294 log_error("Aborting. Failed to wipe mirror log.");
295 goto deactivate_and_revert_new_lv
;
298 if (activation() && !_write_log_header(cmd
, log_lv
)) {
299 log_error("Aborting. Failed to write mirror log header.");
300 goto deactivate_and_revert_new_lv
;
303 if (!deactivate_lv(cmd
, log_lv
)) {
304 log_error("Aborting. Failed to deactivate mirror log. "
305 "Manual intervention required.");
309 lv_set_hidden(log_lv
);
311 if (was_active
&& !activate_lv(cmd
, log_lv
))
316 deactivate_and_revert_new_lv
:
317 if (!deactivate_lv(cmd
, log_lv
)) {
318 log_error("Unable to deactivate mirror log LV. "
319 "Manual intervention required.");
324 log_lv
->status
= orig_status
;
326 dm_list_iterate_items(sl
, tags
)
327 if (!str_list_del(&log_lv
->tags
, sl
->str
))
328 log_error("Failed to remove tag %s from mirror log.",
331 if (remove_on_failure
&& !lv_remove(log_lv
)) {
332 log_error("Manual intervention may be required to remove "
333 "abandoned log LV before retrying.");
337 if (!vg_write(log_lv
->vg
) || !vg_commit(log_lv
->vg
))
338 log_error("Manual intervention may be required to "
339 "remove/restore abandoned log LV before retrying.");
344 if (was_active
&& !remove_on_failure
&& !activate_lv(cmd
, log_lv
))
351 * Delete independent/orphan LV, it must acquire lock.
353 static int _delete_lv(struct logical_volume
*mirror_lv
, struct logical_volume
*lv
)
355 struct cmd_context
*cmd
= mirror_lv
->vg
->cmd
;
358 /* Inherit tags - maybe needed for activation */
359 if (!str_list_match_list(&mirror_lv
->tags
, &lv
->tags
)) {
360 dm_list_iterate_items(sl
, &mirror_lv
->tags
)
361 if (!str_list_add(cmd
->mem
, &lv
->tags
, sl
->str
)) {
362 log_error("Aborting. Unable to tag.");
366 if (!vg_write(mirror_lv
->vg
) ||
367 !vg_commit(mirror_lv
->vg
)) {
368 log_error("Intermediate VG commit for orphan volume failed.");
373 if (!activate_lv(cmd
, lv
))
376 if (!deactivate_lv(cmd
, lv
))
385 static int _merge_mirror_images(struct logical_volume
*lv
,
386 const struct dm_list
*mimages
)
388 uint32_t addition
= dm_list_size(mimages
);
389 struct logical_volume
**img_lvs
;
396 if (!(img_lvs
= alloca(sizeof(*img_lvs
) * addition
)))
399 dm_list_iterate_items(lvl
, mimages
)
400 img_lvs
[i
++] = lvl
->lv
;
402 return lv_add_mirror_lvs(lv
, img_lvs
, addition
,
403 MIRROR_IMAGE
, first_seg(lv
)->region_size
);
406 /* Unlink the relationship between the segment and its log_lv */
407 struct logical_volume
*detach_mirror_log(struct lv_segment
*mirrored_seg
)
409 struct logical_volume
*log_lv
;
411 if (!mirrored_seg
->log_lv
)
414 log_lv
= mirrored_seg
->log_lv
;
415 mirrored_seg
->log_lv
= NULL
;
416 lv_set_visible(log_lv
);
417 log_lv
->status
&= ~MIRROR_LOG
;
418 remove_seg_from_segs_using_this_lv(log_lv
, mirrored_seg
);
423 /* Check if mirror image LV is removable with regard to given removable_pvs */
424 static int _is_mirror_image_removable(struct logical_volume
*mimage_lv
,
425 struct dm_list
*removable_pvs
)
427 struct physical_volume
*pv
;
428 struct lv_segment
*seg
;
433 dm_list_iterate_items(seg
, &mimage_lv
->segments
) {
434 for (s
= 0; s
< seg
->area_count
; s
++) {
435 if (seg_type(seg
, s
) != AREA_PV
) {
436 /* FIXME Recurse for AREA_LV? */
437 /* Structure of seg_lv is unknown.
438 * Not removing this LV for safety. */
445 dm_list_iterate_items(pvl
, removable_pvs
) {
446 if (id_equal(&pv
->id
, &pvl
->pv
->id
)) {
450 if (pvl
->pv
->dev
&& pv
->dev
&&
451 pv
->dev
->dev
== pvl
->pv
->dev
->dev
) {
465 * Remove num_removed images from mirrored_seg
468 * num_removed: the requested (maximum) number of mirrors to be removed
469 * removable_pvs: if not NULL, only mirrors using PVs in this list
471 * remove_log: if non-zero, log_lv will be removed
472 * (even if it's 0, log_lv will be removed if there is no
473 * mirror remaining after the removal)
474 * collapse: if non-zero, instead of removing, remove the temporary
475 * mirror layer and merge mirrors to the original LV.
476 * removable_pvs should be NULL and num_removed should be
477 * seg->area_count - 1.
478 * removed: if non NULL, the number of removed mirror images is set
481 * If collapse is non-zero, <removed> is guaranteed to be equal to num_removed.
484 * Failure (0) means something unexpected has happend and
485 * the caller should abort.
486 * Even if no mirror was removed (e.g. no LV matches to 'removable_pvs'),
487 * returns success (1).
489 static int _remove_mirror_images(struct logical_volume
*lv
,
490 uint32_t num_removed
,
491 struct dm_list
*removable_pvs
,
492 unsigned remove_log
, unsigned collapse
,
497 struct logical_volume
*sub_lv
;
498 struct logical_volume
*detached_log_lv
= NULL
;
499 struct logical_volume
*lv1
= NULL
;
500 struct lv_segment
*mirrored_seg
= first_seg(lv
);
501 uint32_t old_area_count
= mirrored_seg
->area_count
;
502 uint32_t new_area_count
= mirrored_seg
->area_count
;
504 struct dm_list tmp_orphan_lvs
;
509 log_very_verbose("Reducing mirror set from %" PRIu32
" to %"
510 PRIu32
" image(s)%s.",
511 old_area_count
, old_area_count
- num_removed
,
512 remove_log
? " and no log volume" : "");
515 (removable_pvs
|| (old_area_count
- num_removed
!= 1))) {
516 log_error("Incompatible parameters to _remove_mirror_images");
520 /* Move removable_pvs to end of array */
522 for (s
= 0; s
< mirrored_seg
->area_count
&&
523 old_area_count
- new_area_count
< num_removed
; s
++) {
524 sub_lv
= seg_lv(mirrored_seg
, s
);
526 if (!is_temporary_mirror_layer(sub_lv
) &&
527 _is_mirror_image_removable(sub_lv
, removable_pvs
)) {
528 if (!shift_mirror_images(mirrored_seg
, s
))
533 if (num_removed
&& old_area_count
== new_area_count
)
536 new_area_count
= old_area_count
- num_removed
;
538 /* Remove mimage LVs from the segment */
539 dm_list_init(&tmp_orphan_lvs
);
540 for (m
= new_area_count
; m
< mirrored_seg
->area_count
; m
++) {
541 seg_lv(mirrored_seg
, m
)->status
&= ~MIRROR_IMAGE
;
542 lv_set_visible(seg_lv(mirrored_seg
, m
));
543 if (!(lvl
= dm_pool_alloc(lv
->vg
->cmd
->mem
, sizeof(*lvl
)))) {
544 log_error("lv_list alloc failed");
547 lvl
->lv
= seg_lv(mirrored_seg
, m
);
548 dm_list_add(&tmp_orphan_lvs
, &lvl
->list
);
549 release_lv_segment_area(mirrored_seg
, m
, mirrored_seg
->area_len
);
551 mirrored_seg
->area_count
= new_area_count
;
553 /* If no more mirrors, remove mirror layer */
554 /* As an exceptional case, if the lv is temporary layer,
555 * leave the LV as mirrored and let the lvconvert completion
556 * to remove the layer. */
557 if (new_area_count
== 1 && !is_temporary_mirror_layer(lv
)) {
558 lv1
= seg_lv(mirrored_seg
, 0);
559 lv1
->status
&= ~MIRROR_IMAGE
;
561 detached_log_lv
= detach_mirror_log(mirrored_seg
);
562 if (!remove_layer_from_lv(lv
, lv1
))
564 lv
->status
&= ~MIRRORED
;
565 lv
->status
&= ~MIRROR_NOTSYNCED
;
566 if (collapse
&& !_merge_mirror_images(lv
, &tmp_orphan_lvs
)) {
567 log_error("Failed to add mirror images");
570 } else if (new_area_count
== 0) {
571 log_very_verbose("All mimages of %s are gone", lv
->name
);
573 /* All mirror images are gone.
574 * It can happen for vgreduce --removemissing. */
575 detached_log_lv
= detach_mirror_log(mirrored_seg
);
576 lv
->status
&= ~MIRRORED
;
577 lv
->status
&= ~MIRROR_NOTSYNCED
;
578 if (!replace_lv_with_error_segment(lv
))
580 } else if (remove_log
)
581 detached_log_lv
= detach_mirror_log(mirrored_seg
);
584 * To successfully remove these unwanted LVs we need to
585 * remove the LVs from the mirror set, commit that metadata
586 * then deactivate and remove them fully.
589 if (!vg_write(mirrored_seg
->lv
->vg
)) {
590 log_error("intermediate VG write failed.");
594 if (!suspend_lv(mirrored_seg
->lv
->vg
->cmd
, mirrored_seg
->lv
)) {
595 log_error("Failed to lock %s", mirrored_seg
->lv
->name
);
596 vg_revert(mirrored_seg
->lv
->vg
);
600 if (!vg_commit(mirrored_seg
->lv
->vg
)) {
601 resume_lv(mirrored_seg
->lv
->vg
->cmd
, mirrored_seg
->lv
);
605 log_very_verbose("Updating \"%s\" in kernel", mirrored_seg
->lv
->name
);
608 * Avoid having same mirror target loaded twice simultaneously by first
609 * resuming the removed LV which now contains an error segment.
610 * As it's now detached from mirrored_seg->lv we must resume it
614 if (!resume_lv(lv1
->vg
->cmd
, lv1
)) {
615 log_error("Problem resuming temporary LV, %s", lv1
->name
);
620 * The code above calls a suspend_lv once, however we now need
621 * to resume 2 LVs, due to image removal: the mirror image
622 * itself here, and now the remaining mirror LV. Since
623 * suspend_lv/resume_lv call memlock_inc/memlock_dec and these
624 * need to be balanced, we need to call an extra memlock_inc()
625 * here to balance for the this extra resume -- the following
626 * one could otherwise either deadlock due to suspended
627 * devices, or alternatively drop memlock_count below 0.
632 if (!resume_lv(mirrored_seg
->lv
->vg
->cmd
, mirrored_seg
->lv
)) {
633 log_error("Problem reactivating %s", mirrored_seg
->lv
->name
);
637 /* Save or delete the 'orphan' LVs */
639 dm_list_iterate_items(lvl
, &tmp_orphan_lvs
)
640 if (!_delete_lv(lv
, lvl
->lv
))
644 if (lv1
&& !_delete_lv(lv
, lv1
))
647 if (detached_log_lv
&& !_delete_lv(lv
, detached_log_lv
))
650 /* Mirror with only 1 area is 'in sync'. */
651 if (new_area_count
== 1 && is_temporary_mirror_layer(lv
)) {
652 if (first_seg(lv
)->log_lv
&&
653 !_init_mirror_log(lv
->vg
->cmd
, first_seg(lv
)->log_lv
,
655 /* As a result, unnecessary sync may run after
656 * collapsing. But safe.*/
657 log_error("Failed to initialize log device");
663 *removed
= old_area_count
- new_area_count
;
665 log_very_verbose("%" PRIu32
" image(s) removed from %s",
666 old_area_count
- num_removed
, lv
->name
);
672 * Remove the number of mirror images from the LV
674 int remove_mirror_images(struct logical_volume
*lv
, uint32_t num_mirrors
,
675 struct dm_list
*removable_pvs
, unsigned remove_log
)
677 uint32_t num_removed
, removed_once
, r
;
678 uint32_t existing_mirrors
= lv_mirror_count(lv
);
679 struct logical_volume
*next_lv
= lv
;
681 num_removed
= existing_mirrors
- num_mirrors
;
683 /* num_removed can be 0 if the function is called just to remove log */
685 if (num_removed
< first_seg(next_lv
)->area_count
)
686 removed_once
= num_removed
;
688 removed_once
= first_seg(next_lv
)->area_count
- 1;
690 if (!_remove_mirror_images(next_lv
, removed_once
,
691 removable_pvs
, remove_log
, 0, &r
))
694 if (r
< removed_once
) {
695 /* Some mirrors are removed from the temporary mirror,
696 * but the temporary layer still exists.
697 * Down the stack and retry for remainder. */
698 next_lv
= find_temporary_mirror(next_lv
);
702 } while (next_lv
&& num_removed
);
705 if (num_removed
== existing_mirrors
- num_mirrors
)
706 log_error("No mirror images found using specified PVs.");
708 log_error("%u images are removed out of requested %u.",
709 existing_mirrors
- lv_mirror_count(lv
),
710 existing_mirrors
- num_mirrors
);
718 static int _mirrored_lv_in_sync(struct logical_volume
*lv
)
721 percent_range_t percent_range
;
723 if (!lv_mirror_percent(lv
->vg
->cmd
, lv
, 0, &sync_percent
,
724 &percent_range
, NULL
)) {
725 log_error("Unable to determine mirror sync status of %s/%s.",
726 lv
->vg
->name
, lv
->name
);
730 return (percent_range
== PERCENT_100
) ? 1 : 0;
734 * Collapsing temporary mirror layers.
736 * When mirrors are added to already-mirrored LV, a temporary mirror layer
737 * is inserted at the top of the stack to reduce resync work.
738 * The function will remove the intermediate layer and collapse the stack
739 * as far as mirrors are in-sync.
741 * The function is destructive: to remove intermediate mirror layers,
742 * VG metadata commits and suspend/resume are necessary.
744 int collapse_mirrored_lv(struct logical_volume
*lv
)
746 struct logical_volume
*tmp_lv
;
747 struct lv_segment
*mirror_seg
;
749 while ((tmp_lv
= find_temporary_mirror(lv
))) {
750 mirror_seg
= find_mirror_seg(first_seg(tmp_lv
));
752 log_error("Failed to find mirrored LV for %s",
757 if (!_mirrored_lv_in_sync(mirror_seg
->lv
)) {
758 log_verbose("Not collapsing %s: out-of-sync",
759 mirror_seg
->lv
->name
);
763 if (!_remove_mirror_images(mirror_seg
->lv
,
764 mirror_seg
->area_count
- 1,
766 log_error("Failed to release mirror images");
774 static int get_mirror_fault_policy(struct cmd_context
*cmd
__attribute((unused
)),
780 policy
= find_config_str(NULL
, "activation/mirror_log_fault_policy",
781 DEFAULT_MIRROR_LOG_FAULT_POLICY
);
783 policy
= find_config_str(NULL
, "activation/mirror_device_fault_policy",
784 DEFAULT_MIRROR_DEV_FAULT_POLICY
);
786 if (!strcmp(policy
, "remove"))
787 return MIRROR_REMOVE
;
788 else if (!strcmp(policy
, "allocate"))
789 return MIRROR_ALLOCATE
;
790 else if (!strcmp(policy
, "allocate_anywhere"))
791 return MIRROR_ALLOCATE_ANYWHERE
;
794 log_error("Bad activation/mirror_log_fault_policy");
796 log_error("Bad activation/mirror_device_fault_policy");
798 return MIRROR_REMOVE
;
801 static int get_mirror_log_fault_policy(struct cmd_context
*cmd
)
803 return get_mirror_fault_policy(cmd
, 1);
806 static int get_mirror_device_fault_policy(struct cmd_context
*cmd
)
808 return get_mirror_fault_policy(cmd
, 0);
812 * replace_mirror_images
813 * @mirrored_seg: segment (which may be linear now) to restore
814 * @num_mirrors: number of copies we should end up with
815 * @replace_log: replace log if not present
816 * @in_sync: was the original mirror in-sync?
818 * in_sync will be set to 0 if new mirror devices are being added
819 * In other words, it is only useful if the log (and only the log)
822 * Returns: 0 on failure, 1 on reconfig, -1 if no reconfig done
824 static int replace_mirror_images(struct lv_segment
*mirrored_seg
,
825 uint32_t num_mirrors
,
826 int log_policy
, int in_sync
)
829 struct logical_volume
*lv
= mirrored_seg
->lv
;
831 /* FIXME: Use lvconvert rather than duplicating its code */
833 if (mirrored_seg
->area_count
< num_mirrors
) {
834 log_error("WARNING: Failed to replace mirror device in %s/%s",
835 mirrored_seg
->lv
->vg
->name
, mirrored_seg
->lv
->name
);
837 if ((mirrored_seg
->area_count
> 1) && !mirrored_seg
->log_lv
)
838 log_error("WARNING: Use 'lvconvert -m %d %s/%s --corelog' to replace failed devices",
839 num_mirrors
- 1, lv
->vg
->name
, lv
->name
);
841 log_error("WARNING: Use 'lvconvert -m %d %s/%s' to replace failed devices",
842 num_mirrors
- 1, lv
->vg
->name
, lv
->name
);
845 /* REMEMBER/FIXME: set in_sync to 0 if a new mirror device was added */
850 * FIXME: right now, we ignore the allocation policy specified to
851 * allocate the new log.
853 if ((mirrored_seg
->area_count
> 1) && !mirrored_seg
->log_lv
&&
854 (log_policy
!= MIRROR_REMOVE
)) {
855 log_error("WARNING: Failed to replace mirror log device in %s/%s",
856 lv
->vg
->name
, lv
->name
);
858 log_error("WARNING: Use 'lvconvert -m %d %s/%s' to replace failed devices",
859 mirrored_seg
->area_count
- 1 , lv
->vg
->name
, lv
->name
);
866 int reconfigure_mirror_images(struct lv_segment
*mirrored_seg
, uint32_t num_mirrors
,
867 struct dm_list
*removable_pvs
, unsigned remove_log
)
871 int log_policy
, dev_policy
;
872 uint32_t old_num_mirrors
= mirrored_seg
->area_count
;
873 int had_log
= (mirrored_seg
->log_lv
) ? 1 : 0;
875 /* was the mirror in-sync before problems? */
876 in_sync
= _mirrored_lv_in_sync(mirrored_seg
->lv
);
879 * While we are only removing devices, we can have sync set.
880 * Setting this is only useful if we are moving to core log
881 * otherwise the disk log will contain the sync information
883 init_mirror_in_sync(in_sync
);
885 r
= _remove_mirror_images(mirrored_seg
->lv
, old_num_mirrors
- num_mirrors
,
886 removable_pvs
, remove_log
, 0, NULL
);
888 /* Unable to remove bad devices */
891 log_warn("WARNING: Bad device removed from mirror volume, %s/%s",
892 mirrored_seg
->lv
->vg
->name
, mirrored_seg
->lv
->name
);
894 log_policy
= get_mirror_log_fault_policy(mirrored_seg
->lv
->vg
->cmd
);
895 dev_policy
= get_mirror_device_fault_policy(mirrored_seg
->lv
->vg
->cmd
);
897 r
= replace_mirror_images(mirrored_seg
,
898 (dev_policy
!= MIRROR_REMOVE
) ?
899 old_num_mirrors
: num_mirrors
,
900 log_policy
, in_sync
);
903 /* Failed to replace device(s) */
904 log_error("WARNING: Unable to find substitute device for mirror volume, %s/%s",
905 mirrored_seg
->lv
->vg
->name
, mirrored_seg
->lv
->name
);
907 /* Success in replacing device(s) */
908 log_warn("WARNING: Mirror volume, %s/%s restored - substitute for failed device found.",
909 mirrored_seg
->lv
->vg
->name
, mirrored_seg
->lv
->name
);
911 /* Bad device removed, but not replaced because of policy */
912 if (mirrored_seg
->area_count
== 1) {
913 log_warn("WARNING: Mirror volume, %s/%s converted to linear due to device failure.",
914 mirrored_seg
->lv
->vg
->name
, mirrored_seg
->lv
->name
);
915 } else if (had_log
&& !mirrored_seg
->log_lv
) {
916 log_warn("WARNING: Mirror volume, %s/%s disk log removed due to device failure.",
917 mirrored_seg
->lv
->vg
->name
, mirrored_seg
->lv
->name
);
920 * If we made it here, we at least removed the bad device.
921 * Consider this success.
926 static int _create_mimage_lvs(struct alloc_handle
*ah
,
927 uint32_t num_mirrors
,
928 struct logical_volume
*lv
,
929 struct logical_volume
**img_lvs
)
935 len
= strlen(lv
->name
) + 32;
936 if (!(img_name
= alloca(len
))) {
937 log_error("img_name allocation failed. "
938 "Remove new LV and retry.");
942 if (dm_snprintf(img_name
, len
, "%s_mimage_%%d", lv
->name
) < 0) {
943 log_error("img_name allocation failed. "
944 "Remove new LV and retry.");
948 for (m
= 0; m
< num_mirrors
; m
++) {
949 if (!(img_lvs
[m
] = lv_create_empty(img_name
,
950 NULL
, LVM_READ
| LVM_WRITE
,
951 ALLOC_INHERIT
, lv
->vg
))) {
952 log_error("Aborting. Failed to create mirror image LV. "
953 "Remove new LV and retry.");
957 if (!lv_add_segment(ah
, m
, 1, img_lvs
[m
],
958 get_segtype_from_string(lv
->vg
->cmd
,
961 log_error("Aborting. Failed to add mirror image segment "
962 "to %s. Remove new LV and retry.",
972 * Remove mirrors from each segment.
973 * 'new_mirrors' is the number of mirrors after the removal. '0' for linear.
974 * If 'status_mask' is non-zero, the removal happens only when all segments
975 * has the status bits on.
977 int remove_mirrors_from_segments(struct logical_volume
*lv
,
978 uint32_t new_mirrors
, uint32_t status_mask
)
980 struct lv_segment
*seg
;
983 /* Check the segment params are compatible */
984 dm_list_iterate_items(seg
, &lv
->segments
) {
985 if (!seg_is_mirrored(seg
)) {
986 log_error("Segment is not mirrored: %s:%" PRIu32
,
989 } if ((seg
->status
& status_mask
) != status_mask
) {
990 log_error("Segment status does not match: %s:%" PRIu32
991 " status:0x%x/0x%x", lv
->name
, seg
->le
,
992 seg
->status
, status_mask
);
997 /* Convert the segments */
998 dm_list_iterate_items(seg
, &lv
->segments
) {
999 if (!new_mirrors
&& seg
->extents_copied
== seg
->area_len
) {
1000 if (!move_lv_segment_area(seg
, 0, seg
, 1))
1004 for (s
= new_mirrors
+ 1; s
< seg
->area_count
; s
++)
1005 release_lv_segment_area(seg
, s
, seg
->area_len
);
1007 seg
->area_count
= new_mirrors
+ 1;
1010 seg
->segtype
= get_segtype_from_string(lv
->vg
->cmd
,
1017 const char *get_pvmove_pvname_from_lv_mirr(struct logical_volume
*lv_mirr
)
1019 struct lv_segment
*seg
;
1021 dm_list_iterate_items(seg
, &lv_mirr
->segments
) {
1022 if (!seg_is_mirrored(seg
))
1024 if (seg_type(seg
, 0) != AREA_PV
)
1026 return dev_name(seg_dev(seg
, 0));
1032 const char *get_pvmove_pvname_from_lv(struct logical_volume
*lv
)
1034 struct lv_segment
*seg
;
1037 dm_list_iterate_items(seg
, &lv
->segments
) {
1038 for (s
= 0; s
< seg
->area_count
; s
++) {
1039 if (seg_type(seg
, s
) != AREA_LV
)
1041 return get_pvmove_pvname_from_lv_mirr(seg_lv(seg
, s
));
1048 struct logical_volume
*find_pvmove_lv(struct volume_group
*vg
,
1052 struct lv_list
*lvl
;
1053 struct logical_volume
*lv
;
1054 struct lv_segment
*seg
;
1056 /* Loop through all LVs */
1057 dm_list_iterate_items(lvl
, &vg
->lvs
) {
1060 if (!(lv
->status
& lv_type
))
1063 /* Check segment origins point to pvname */
1064 dm_list_iterate_items(seg
, &lv
->segments
) {
1065 if (seg_type(seg
, 0) != AREA_PV
)
1067 if (seg_dev(seg
, 0) != dev
)
1076 struct logical_volume
*find_pvmove_lv_from_pvname(struct cmd_context
*cmd
,
1077 struct volume_group
*vg
,
1079 const char *uuid
__attribute((unused
)),
1082 struct physical_volume
*pv
;
1084 if (!(pv
= find_pv_by_name(cmd
, name
)))
1087 return find_pvmove_lv(vg
, pv
->dev
, lv_type
);
1090 struct dm_list
*lvs_using_lv(struct cmd_context
*cmd
, struct volume_group
*vg
,
1091 struct logical_volume
*lv
)
1093 struct dm_list
*lvs
;
1094 struct logical_volume
*lv1
;
1095 struct lv_list
*lvl
, *lvl1
;
1096 struct lv_segment
*seg
;
1099 if (!(lvs
= dm_pool_alloc(cmd
->mem
, sizeof(*lvs
)))) {
1100 log_error("lvs list alloc failed");
1106 /* Loop through all LVs except the one supplied */
1107 dm_list_iterate_items(lvl1
, &vg
->lvs
) {
1112 /* Find whether any segment points at the supplied LV */
1113 dm_list_iterate_items(seg
, &lv1
->segments
) {
1114 for (s
= 0; s
< seg
->area_count
; s
++) {
1115 if (seg_type(seg
, s
) != AREA_LV
||
1116 seg_lv(seg
, s
) != lv
)
1118 if (!(lvl
= dm_pool_alloc(cmd
->mem
, sizeof(*lvl
)))) {
1119 log_error("lv_list alloc failed");
1123 dm_list_add(lvs
, &lvl
->list
);
1134 float copy_percent(struct logical_volume
*lv_mirr
,
1135 percent_range_t
*percent_range
)
1137 uint32_t numerator
= 0u, denominator
= 0u;
1138 struct lv_segment
*seg
;
1140 dm_list_iterate_items(seg
, &lv_mirr
->segments
) {
1141 denominator
+= seg
->area_len
;
1143 if (seg_is_mirrored(seg
) && seg
->area_count
> 1)
1144 numerator
+= seg
->extents_copied
;
1146 numerator
+= seg
->area_len
;
1149 if (!denominator
|| (numerator
== denominator
))
1150 *percent_range
= PERCENT_100
;
1151 else if (numerator
== 0)
1152 *percent_range
= PERCENT_0
;
1154 *percent_range
= PERCENT_0_TO_100
;
1156 return denominator
? (float) numerator
*100 / denominator
: 100.0;
1160 * Fixup mirror pointers after single-pass segment import
1162 int fixup_imported_mirrors(struct volume_group
*vg
)
1164 struct lv_list
*lvl
;
1165 struct lv_segment
*seg
;
1167 dm_list_iterate_items(lvl
, &vg
->lvs
) {
1168 dm_list_iterate_items(seg
, &lvl
->lv
->segments
) {
1170 get_segtype_from_string(vg
->cmd
, "mirror"))
1173 if (seg
->log_lv
&& !add_seg_to_segs_using_this_lv(seg
->log_lv
, seg
))
1182 * Add mirrors to "linear" or "mirror" segments
1184 int add_mirrors_to_segments(struct cmd_context
*cmd
, struct logical_volume
*lv
,
1185 uint32_t mirrors
, uint32_t region_size
,
1186 struct dm_list
*allocatable_pvs
, alloc_policy_t alloc
)
1188 struct alloc_handle
*ah
;
1189 const struct segment_type
*segtype
;
1190 struct dm_list
*parallel_areas
;
1191 uint32_t adjusted_region_size
;
1194 if (!(parallel_areas
= build_parallel_areas_from_lv(cmd
, lv
)))
1197 if (!(segtype
= get_segtype_from_string(cmd
, "mirror")))
1200 adjusted_region_size
= adjusted_mirror_region_size(lv
->vg
->extent_size
,
1204 if (!(ah
= allocate_extents(lv
->vg
, NULL
, segtype
, 1, mirrors
, 0, 0,
1205 lv
->le_count
, allocatable_pvs
, alloc
,
1207 log_error("Unable to allocate mirror extents for %s.", lv
->name
);
1211 if (!lv_add_mirror_areas(ah
, lv
, 0, adjusted_region_size
)) {
1212 log_error("Failed to add mirror areas to %s", lv
->name
);
1221 * Convert mirror log
1223 * FIXME: Can't handle segment-by-segment mirror (like pvmove)
1225 int remove_mirror_log(struct cmd_context
*cmd
,
1226 struct logical_volume
*lv
,
1227 struct dm_list
*removable_pvs
)
1230 percent_range_t percent_range
= PERCENT_0
;
1232 struct volume_group
*vg
= lv
->vg
;
1234 /* Unimplemented features */
1235 if (dm_list_size(&lv
->segments
) != 1) {
1236 log_error("Multiple-segment mirror is not supported");
1240 /* Had disk log, switch to core. */
1241 if (lv_info(cmd
, lv
, &info
, 0, 0) && info
.exists
) {
1242 if (!lv_mirror_percent(cmd
, lv
, 0, &sync_percent
,
1243 &percent_range
, NULL
)) {
1244 log_error("Unable to determine mirror sync status.");
1247 } else if (vg_is_clustered(vg
)) {
1248 log_error("Unable to convert the log of an inactive "
1249 "cluster mirror, %s", lv
->name
);
1251 } else if (yes_no_prompt("Full resync required to convert "
1252 "inactive mirror %s to core log. "
1253 "Proceed? [y/n]: ") == 'y')
1258 if (percent_range
== PERCENT_100
)
1259 init_mirror_in_sync(1);
1261 /* A full resync will take place */
1262 lv
->status
&= ~MIRROR_NOTSYNCED
;
1263 init_mirror_in_sync(0);
1266 if (!remove_mirror_images(lv
, lv_mirror_count(lv
),
1273 static struct logical_volume
*_create_mirror_log(struct logical_volume
*lv
,
1274 struct alloc_handle
*ah
,
1275 alloc_policy_t alloc
,
1276 const char *lv_name
,
1279 struct logical_volume
*log_lv
;
1283 len
= strlen(lv_name
) + 32;
1284 if (!(log_name
= alloca(len
))) {
1285 log_error("log_name allocation failed.");
1289 if (dm_snprintf(log_name
, len
, "%s%s", lv_name
, suffix
) < 0) {
1290 log_error("log_name allocation failed.");
1294 if (!(log_lv
= lv_create_empty(log_name
, NULL
,
1295 VISIBLE_LV
| LVM_READ
| LVM_WRITE
,
1299 if (!lv_add_log_segment(ah
, log_lv
))
1305 static struct logical_volume
*_set_up_mirror_log(struct cmd_context
*cmd
,
1306 struct alloc_handle
*ah
,
1307 struct logical_volume
*lv
,
1309 uint32_t region_size
__attribute((unused
)),
1310 alloc_policy_t alloc
,
1313 struct logical_volume
*log_lv
;
1314 const char *suffix
, *c
;
1317 struct lv_segment
*seg
;
1319 init_mirror_in_sync(in_sync
);
1321 if (log_count
!= 1) {
1322 log_error("log_count != 1 is not supported.");
1326 /* Mirror log name is lv_name + suffix, determined as the following:
1328 * o "_mlog" for the original mirror LV.
1329 * o "_mlogtmp_%d" for temporary mirror LV,
1331 * o lv->name, if the log is temporary
1332 * o otherwise, the top-level LV name
1334 seg
= first_seg(lv
);
1335 if (seg_type(seg
, 0) == AREA_LV
&&
1336 strstr(seg_lv(seg
, 0)->name
, MIRROR_SYNC_LAYER
)) {
1338 suffix
= "_mlogtmp_%d";
1339 } else if ((c
= strstr(lv
->name
, MIRROR_SYNC_LAYER
))) {
1340 len
= c
- lv
->name
+ 1;
1341 if (!(lv_name
= alloca(len
)) ||
1342 !dm_snprintf(lv_name
, len
, "%s", lv
->name
)) {
1343 log_error("mirror log name allocation failed");
1352 if (!(log_lv
= _create_mirror_log(lv
, ah
, alloc
,
1353 (const char *) lv_name
, suffix
))) {
1354 log_error("Failed to create mirror log.");
1358 if (!_init_mirror_log(cmd
, log_lv
, in_sync
, &lv
->tags
, 1)) {
1359 log_error("Failed to create mirror log.");
1366 int attach_mirror_log(struct lv_segment
*seg
, struct logical_volume
*log_lv
)
1368 seg
->log_lv
= log_lv
;
1369 log_lv
->status
|= MIRROR_LOG
;
1370 lv_set_hidden(log_lv
);
1371 return add_seg_to_segs_using_this_lv(log_lv
, seg
);
1374 int add_mirror_log(struct cmd_context
*cmd
, struct logical_volume
*lv
,
1375 uint32_t log_count
, uint32_t region_size
,
1376 struct dm_list
*allocatable_pvs
, alloc_policy_t alloc
)
1378 struct alloc_handle
*ah
;
1379 const struct segment_type
*segtype
;
1380 struct dm_list
*parallel_areas
;
1382 percent_range_t percent_range
;
1384 struct logical_volume
*log_lv
;
1388 /* Unimplemented features */
1389 if (log_count
> 1) {
1390 log_error("log_count > 1 is not supported");
1394 if (dm_list_size(&lv
->segments
) != 1) {
1395 log_error("Multiple-segment mirror is not supported");
1400 * We are unable to convert the log of inactive cluster mirrors
1401 * due to the inability to detect whether the mirror is active
1402 * on remote nodes (even though it is inactive on this node)
1404 if (vg_is_clustered(lv
->vg
) &&
1405 !(lv_info(cmd
, lv
, &info
, 0, 0) && info
.exists
)) {
1406 log_error("Unable to convert the log of inactive "
1407 "cluster mirror %s", lv
->name
);
1411 if (!(parallel_areas
= build_parallel_areas_from_lv(cmd
, lv
)))
1414 if (!(segtype
= get_segtype_from_string(cmd
, "mirror")))
1417 if (activation() && segtype
->ops
->target_present
&&
1418 !segtype
->ops
->target_present(cmd
, NULL
, NULL
)) {
1419 log_error("%s: Required device-mapper target(s) not "
1420 "detected in your kernel", segtype
->name
);
1424 /* allocate destination extents */
1425 ah
= allocate_extents(lv
->vg
, NULL
, segtype
,
1426 0, 0, log_count
, region_size
, 0,
1427 allocatable_pvs
, alloc
, parallel_areas
);
1429 log_error("Unable to allocate extents for mirror log.");
1433 /* check sync status */
1434 if (lv_mirror_percent(cmd
, lv
, 0, &sync_percent
, &percent_range
,
1436 (percent_range
== PERCENT_100
))
1441 if (!(log_lv
= _set_up_mirror_log(cmd
, ah
, lv
, log_count
,
1442 region_size
, alloc
, in_sync
)))
1445 if (!attach_mirror_log(first_seg(lv
), log_lv
))
1455 * Convert "linear" LV to "mirror".
1457 int add_mirror_images(struct cmd_context
*cmd
, struct logical_volume
*lv
,
1458 uint32_t mirrors
, uint32_t stripes
, uint32_t region_size
,
1459 struct dm_list
*allocatable_pvs
, alloc_policy_t alloc
,
1462 struct alloc_handle
*ah
;
1463 const struct segment_type
*segtype
;
1464 struct dm_list
*parallel_areas
;
1465 struct logical_volume
**img_lvs
;
1466 struct logical_volume
*log_lv
= NULL
;
1469 log_error("stripes > 1 is not supported");
1474 * allocate destination extents
1477 if (!(parallel_areas
= build_parallel_areas_from_lv(cmd
, lv
)))
1480 if (!(segtype
= get_segtype_from_string(cmd
, "mirror")))
1483 ah
= allocate_extents(lv
->vg
, NULL
, segtype
,
1484 stripes
, mirrors
, log_count
, region_size
, lv
->le_count
,
1485 allocatable_pvs
, alloc
, parallel_areas
);
1487 log_error("Unable to allocate extents for mirror(s).");
1492 * create and initialize mirror log
1495 !(log_lv
= _set_up_mirror_log(cmd
, ah
, lv
, log_count
, region_size
,
1496 alloc
, mirror_in_sync()))) {
1498 goto out_remove_images
;
1501 /* The log initialization involves vg metadata commit.
1502 So from here on, if failure occurs, the log must be explicitly
1503 removed and the updated vg metadata should be committed. */
1506 * insert a mirror layer
1508 if (dm_list_size(&lv
->segments
) != 1 ||
1509 seg_type(first_seg(lv
), 0) != AREA_LV
)
1510 if (!insert_layer_for_lv(cmd
, lv
, 0, "_mimage_%d"))
1511 goto out_remove_log
;
1514 * create mirror image LVs
1516 if (!(img_lvs
= alloca(sizeof(*img_lvs
) * mirrors
))) {
1517 log_error("img_lvs allocation failed. "
1518 "Remove new LV and retry.");
1519 goto out_remove_log
;
1522 if (!_create_mimage_lvs(ah
, mirrors
, lv
, img_lvs
))
1523 goto out_remove_log
;
1525 if (!lv_add_mirror_lvs(lv
, img_lvs
, mirrors
,
1526 MIRROR_IMAGE
| (lv
->status
& LOCKED
),
1528 log_error("Aborting. Failed to add mirror segment. "
1529 "Remove new LV and retry.");
1530 goto out_remove_images
;
1533 if (log_count
&& !attach_mirror_log(first_seg(lv
), log_lv
))
1541 if (!lv_remove(log_lv
) ||
1542 !vg_write(log_lv
->vg
) ||
1543 !vg_commit(log_lv
->vg
))
1544 log_error("Manual intervention may be required to remove "
1545 "abandoned log LV before retrying.");
1555 * Generic interface for adding mirror and/or mirror log.
1556 * 'mirror' is the number of mirrors to be added.
1557 * 'pvs' is either allocatable pvs.
1559 int lv_add_mirrors(struct cmd_context
*cmd
, struct logical_volume
*lv
,
1560 uint32_t mirrors
, uint32_t stripes
,
1561 uint32_t region_size
, uint32_t log_count
,
1562 struct dm_list
*pvs
, alloc_policy_t alloc
, uint32_t flags
)
1564 if (!mirrors
&& !log_count
) {
1565 log_error("No conversion is requested");
1569 /* For corelog mirror, activation code depends on
1570 * the global mirror_in_sync status. As we are adding
1571 * a new mirror, it should be set as 'out-of-sync'
1572 * so that the sync starts. */
1573 /* However, MIRROR_SKIP_INIT_SYNC even overrides it. */
1574 if (flags
& MIRROR_SKIP_INIT_SYNC
)
1575 init_mirror_in_sync(1);
1576 else if (!log_count
)
1577 init_mirror_in_sync(0);
1579 if (flags
& MIRROR_BY_SEG
) {
1581 log_error("Persistent log is not supported on "
1582 "segment-by-segment mirroring");
1586 log_error("Striped-mirroring is not supported on "
1587 "segment-by-segment mirroring");
1591 return add_mirrors_to_segments(cmd
, lv
, mirrors
,
1592 region_size
, pvs
, alloc
);
1593 } else if (flags
& MIRROR_BY_LV
) {
1595 return add_mirror_log(cmd
, lv
, log_count
,
1596 region_size
, pvs
, alloc
);
1597 return add_mirror_images(cmd
, lv
, mirrors
,
1598 stripes
, region_size
,
1599 pvs
, alloc
, log_count
);
1602 log_error("Unsupported mirror conversion type");
1607 * Generic interface for removing mirror and/or mirror log.
1608 * 'mirror' is the number of mirrors to be removed.
1609 * 'pvs' is removable pvs.
1611 int lv_remove_mirrors(struct cmd_context
*cmd
__attribute((unused
)),
1612 struct logical_volume
*lv
,
1613 uint32_t mirrors
, uint32_t log_count
, struct dm_list
*pvs
,
1614 uint32_t status_mask
)
1616 uint32_t new_mirrors
;
1617 struct lv_segment
*seg
;
1619 if (!mirrors
&& !log_count
) {
1620 log_error("No conversion is requested");
1624 seg
= first_seg(lv
);
1625 if (!seg_is_mirrored(seg
)) {
1626 log_error("Not a mirror segment");
1630 if (lv_mirror_count(lv
) <= mirrors
) {
1631 log_error("Removing more than existing: %d <= %d",
1632 seg
->area_count
, mirrors
);
1635 new_mirrors
= lv_mirror_count(lv
) - mirrors
- 1;
1638 if (seg_type(seg
, 0) == AREA_LV
&&
1639 seg_lv(seg
, 0)->status
& MIRROR_IMAGE
)
1640 return remove_mirror_images(lv
, new_mirrors
+ 1,
1641 pvs
, log_count
? 1U : 0);
1645 log_error("Persistent log is not supported on "
1646 "segment-by-segment mirroring");
1649 return remove_mirrors_from_segments(lv
, new_mirrors
, status_mask
);