mirror.c revision 1.1.1.1.2.1 1 /* $NetBSD: mirror.c,v 1.1.1.1.2.1 2009/05/13 18:52:43 jym Exp $ */
2
3 /*
4 * Copyright (C) 2003-2004 Sistina Software, Inc. All rights reserved.
5 * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
6 *
7 * This file is part of LVM2.
8 *
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.
12 *
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
16 */
17
18 #include "lib.h"
19 #include "metadata.h"
20 #include "toolcontext.h"
21 #include "segtype.h"
22 #include "display.h"
23 #include "archiver.h"
24 #include "activate.h"
25 #include "lv_alloc.h"
26 #include "lvm-string.h"
27 #include "str_list.h"
28 #include "locking.h" /* FIXME Should not be used in this file */
29
30 #include "defaults.h" /* FIXME: should this be defaults.h? */
31
32 /* These are necessary for _write_log_header() */
33 #include "xlate.h"
34 #define MIRROR_MAGIC 0x4D695272
35 #define MIRROR_DISK_VERSION 2
36
37 /* These are the flags that represent the mirror failure restoration policies */
38 #define MIRROR_REMOVE 0
39 #define MIRROR_ALLOCATE 1
40 #define MIRROR_ALLOCATE_ANYWHERE 2
41
42 /*
43 * Returns true if the lv is temporary mirror layer for resync
44 */
45 int is_temporary_mirror_layer(const struct logical_volume *lv)
46 {
47 if (lv->status & MIRROR_IMAGE
48 && lv->status & MIRRORED
49 && !(lv->status & LOCKED))
50 return 1;
51
52 return 0;
53 }
54
55 /*
56 * Return a temporary LV for resyncing added mirror image.
57 * Add other mirror legs to lvs list.
58 */
59 struct logical_volume *find_temporary_mirror(const struct logical_volume *lv)
60 {
61 struct lv_segment *seg;
62
63 if (!(lv->status & MIRRORED))
64 return NULL;
65
66 seg = first_seg(lv);
67
68 /* Temporary mirror is always area_num == 0 */
69 if (seg_type(seg, 0) == AREA_LV &&
70 is_temporary_mirror_layer(seg_lv(seg, 0)))
71 return seg_lv(seg, 0);
72
73 return NULL;
74 }
75
76 /*
77 * Returns the number of mirrors of the LV
78 */
79 uint32_t lv_mirror_count(const struct logical_volume *lv)
80 {
81 struct lv_segment *seg;
82 uint32_t s, mirrors;
83
84 if (!(lv->status & MIRRORED))
85 return 1;
86
87 seg = first_seg(lv);
88 mirrors = seg->area_count;
89
90 for (s = 0; s < seg->area_count; s++) {
91 if (seg_type(seg, s) != AREA_LV)
92 continue;
93 if (is_temporary_mirror_layer(seg_lv(seg, s)))
94 mirrors += lv_mirror_count(seg_lv(seg, s)) - 1;
95 }
96
97 return mirrors;
98 }
99
100 struct lv_segment *find_mirror_seg(struct lv_segment *seg)
101 {
102 struct lv_segment *mirror_seg;
103
104 mirror_seg = get_only_segment_using_this_lv(seg->lv);
105
106 if (!mirror_seg) {
107 log_error("Failed to find mirror_seg for %s", seg->lv->name);
108 return NULL;
109 }
110
111 if (!seg_is_mirrored(mirror_seg)) {
112 log_error("%s on %s is not a mirror segments",
113 mirror_seg->lv->name, seg->lv->name);
114 return NULL;
115 }
116
117 return mirror_seg;
118 }
119
120 /*
121 * Reduce the region size if necessary to ensure
122 * the volume size is a multiple of the region size.
123 */
124 uint32_t adjusted_mirror_region_size(uint32_t extent_size, uint32_t extents,
125 uint32_t region_size)
126 {
127 uint64_t region_max;
128
129 region_max = (1 << (ffs((int)extents) - 1)) * (uint64_t) extent_size;
130
131 if (region_max < UINT32_MAX && region_size > region_max) {
132 region_size = (uint32_t) region_max;
133 log_print("Using reduced mirror region size of %" PRIu32
134 " sectors", region_size);
135 }
136
137 return region_size;
138 }
139
140 /*
141 * shift_mirror_images
142 * @mirrored_seg
143 * @mimage: The position (index) of the image to move to the end
144 *
145 * When dealing with removal of legs, we often move a 'removable leg'
146 * to the back of the 'areas' array. It is critically important not
147 * to simply swap it for the last area in the array. This would have
148 * the affect of reordering the remaining legs - altering position of
149 * the primary. So, we must shuffle all of the areas in the array
150 * to maintain their relative position before moving the 'removable
151 * leg' to the end.
152 *
153 * Short illustration of the problem:
154 * - Mirror consists of legs A, B, C and we want to remove A
155 * - We swap A and C and then remove A, leaving C, B
156 * This scenario is problematic in failure cases where A dies, because
157 * B becomes the primary. If the above happens, we effectively throw
158 * away any changes made between the time of failure and the time of
159 * restructuring the mirror.
160 *
161 * So, any time we want to move areas to the end to be removed, use
162 * this function.
163 */
164 int shift_mirror_images(struct lv_segment *mirrored_seg, unsigned mimage)
165 {
166 int i;
167 struct lv_segment_area area;
168
169 if (mimage >= mirrored_seg->area_count) {
170 log_error("Invalid index (%u) of mirror image supplied "
171 "to shift_mirror_images()", mimage);
172 return 0;
173 }
174
175 area = mirrored_seg->areas[mimage];
176
177 /* Shift remaining images down to fill the hole */
178 for (i = mimage + 1; i < mirrored_seg->area_count; i++)
179 mirrored_seg->areas[i-1] = mirrored_seg->areas[i];
180
181 /* Place this one at the end */
182 mirrored_seg->areas[i-1] = area;
183
184 return 1;
185 }
186
187 /*
188 * This function writes a new header to the mirror log header to the lv
189 *
190 * Returns: 1 on success, 0 on failure
191 */
192 static int _write_log_header(struct cmd_context *cmd, struct logical_volume *lv)
193 {
194 struct device *dev;
195 char *name;
196 struct { /* The mirror log header */
197 uint32_t magic;
198 uint32_t version;
199 uint64_t nr_regions;
200 } log_header;
201
202 log_header.magic = xlate32(MIRROR_MAGIC);
203 log_header.version = xlate32(MIRROR_DISK_VERSION);
204 log_header.nr_regions = xlate64((uint64_t)-1);
205
206 if (!(name = dm_pool_alloc(cmd->mem, PATH_MAX))) {
207 log_error("Name allocation failed - log header not written (%s)",
208 lv->name);
209 return 0;
210 }
211
212 if (dm_snprintf(name, PATH_MAX, "%s%s/%s", cmd->dev_dir,
213 lv->vg->name, lv->name) < 0) {
214 log_error("Name too long - log header not written (%s)", lv->name);
215 return 0;
216 }
217
218 log_verbose("Writing log header to device, %s", lv->name);
219
220 if (!(dev = dev_cache_get(name, NULL))) {
221 log_error("%s: not found: log header not written", name);
222 return 0;
223 }
224
225 if (!dev_open_quiet(dev))
226 return 0;
227
228 if (!dev_write(dev, UINT64_C(0), sizeof(log_header), &log_header)) {
229 log_error("Failed to write log header to %s", name);
230 dev_close_immediate(dev);
231 return 0;
232 }
233
234 dev_close_immediate(dev);
235
236 return 1;
237 }
238
239 /*
240 * Initialize mirror log contents
241 */
242 static int _init_mirror_log(struct cmd_context *cmd,
243 struct logical_volume *log_lv, int in_sync,
244 struct dm_list *tags, int remove_on_failure)
245 {
246 struct str_list *sl;
247 struct lvinfo info;
248 uint32_t orig_status = log_lv->status;
249 int was_active = 0;
250
251 if (!activation() && in_sync) {
252 log_error("Aborting. Unable to create in-sync mirror log "
253 "while activation is disabled.");
254 return 0;
255 }
256
257 /* If the LV is active, deactivate it first. */
258 if (lv_info(cmd, log_lv, &info, 0, 0) && info.exists) {
259 if (!deactivate_lv(cmd, log_lv))
260 return_0;
261 was_active = 1;
262 }
263
264 /* Temporary make it visible for set_lv() */
265 log_lv->status |= VISIBLE_LV;
266
267 /* Temporary tag mirror log for activation */
268 dm_list_iterate_items(sl, tags)
269 if (!str_list_add(cmd->mem, &log_lv->tags, sl->str)) {
270 log_error("Aborting. Unable to tag mirror log.");
271 goto activate_lv;
272 }
273
274 /* store mirror log on disk(s) */
275 if (!vg_write(log_lv->vg))
276 goto activate_lv;
277
278 backup(log_lv->vg);
279
280 if (!vg_commit(log_lv->vg))
281 goto activate_lv;
282
283 if (!activate_lv(cmd, log_lv)) {
284 log_error("Aborting. Failed to activate mirror log.");
285 goto revert_new_lv;
286 }
287
288 /* Remove the temporary tags */
289 dm_list_iterate_items(sl, tags)
290 if (!str_list_del(&log_lv->tags, sl->str))
291 log_error("Failed to remove tag %s from mirror log.",
292 sl->str);
293
294 if (activation() && !set_lv(cmd, log_lv, log_lv->size,
295 in_sync ? -1 : 0)) {
296 log_error("Aborting. Failed to wipe mirror log.");
297 goto deactivate_and_revert_new_lv;
298 }
299
300 if (activation() && !_write_log_header(cmd, log_lv)) {
301 log_error("Aborting. Failed to write mirror log header.");
302 goto deactivate_and_revert_new_lv;
303 }
304
305 if (!deactivate_lv(cmd, log_lv)) {
306 log_error("Aborting. Failed to deactivate mirror log. "
307 "Manual intervention required.");
308 return 0;
309 }
310
311 log_lv->status &= ~VISIBLE_LV;
312
313 if (was_active && !activate_lv(cmd, log_lv))
314 return_0;
315
316 return 1;
317
318 deactivate_and_revert_new_lv:
319 if (!deactivate_lv(cmd, log_lv)) {
320 log_error("Unable to deactivate mirror log LV. "
321 "Manual intervention required.");
322 return 0;
323 }
324
325 revert_new_lv:
326 log_lv->status = orig_status;
327
328 dm_list_iterate_items(sl, tags)
329 if (!str_list_del(&log_lv->tags, sl->str))
330 log_error("Failed to remove tag %s from mirror log.",
331 sl->str);
332
333 if (remove_on_failure && !lv_remove(log_lv)) {
334 log_error("Manual intervention may be required to remove "
335 "abandoned log LV before retrying.");
336 return 0;
337 }
338
339 if (!vg_write(log_lv->vg) ||
340 (backup(log_lv->vg), !vg_commit(log_lv->vg)))
341 log_error("Manual intervention may be required to "
342 "remove/restore abandoned log LV before retrying.");
343 activate_lv:
344 if (was_active && !remove_on_failure && !activate_lv(cmd, log_lv))
345 return_0;
346
347 return 0;
348 }
349
350 /*
351 * Delete independent/orphan LV, it must acquire lock.
352 */
353 static int _delete_lv(struct logical_volume *mirror_lv, struct logical_volume *lv)
354 {
355 struct cmd_context *cmd = mirror_lv->vg->cmd;
356 struct str_list *sl;
357
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.");
363 return 0;
364 }
365
366 if (!vg_write(mirror_lv->vg) ||
367 !vg_commit(mirror_lv->vg)) {
368 log_error("Intermediate VG commit for orphan volume failed.");
369 return 0;
370 }
371 }
372
373 if (!activate_lv(cmd, lv))
374 return_0;
375
376 if (!deactivate_lv(cmd, lv))
377 return_0;
378
379 if (!lv_remove(lv))
380 return_0;
381
382 return 1;
383 }
384
385 static int _merge_mirror_images(struct logical_volume *lv,
386 const struct dm_list *mimages)
387 {
388 uint32_t addition = dm_list_size(mimages);
389 struct logical_volume **img_lvs;
390 struct lv_list *lvl;
391 int i = 0;
392
393 if (!addition)
394 return 1;
395
396 if (!(img_lvs = alloca(sizeof(*img_lvs) * addition)))
397 return_0;
398
399 dm_list_iterate_items(lvl, mimages)
400 img_lvs[i++] = lvl->lv;
401
402 return lv_add_mirror_lvs(lv, img_lvs, addition,
403 MIRROR_IMAGE, first_seg(lv)->region_size);
404 }
405
406 /* Unlink the relationship between the segment and its log_lv */
407 struct logical_volume *detach_mirror_log(struct lv_segment *mirrored_seg)
408 {
409 struct logical_volume *log_lv;
410
411 if (!mirrored_seg->log_lv)
412 return NULL;
413
414 log_lv = mirrored_seg->log_lv;
415 mirrored_seg->log_lv = NULL;
416 log_lv->status |= VISIBLE_LV;
417 log_lv->status &= ~MIRROR_LOG;
418 remove_seg_from_segs_using_this_lv(log_lv, mirrored_seg);
419
420 return log_lv;
421 }
422
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)
426 {
427 struct physical_volume *pv;
428 struct lv_segment *seg;
429 int pv_found;
430 struct pv_list *pvl;
431 uint32_t s;
432
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. */
439 return 0;
440 }
441
442 pv = seg_pv(seg, s);
443
444 pv_found = 0;
445 dm_list_iterate_items(pvl, removable_pvs) {
446 if (pv->dev->dev == pvl->pv->dev->dev) {
447 pv_found = 1;
448 break;
449 }
450 }
451 if (!pv_found)
452 return 0;
453 }
454 }
455
456 return 1;
457 }
458
459 /*
460 * Remove num_removed images from mirrored_seg
461 *
462 * Arguments:
463 * num_removed: the requested (maximum) number of mirrors to be removed
464 * removable_pvs: if not NULL, only mirrors using PVs in this list
465 * will be removed
466 * remove_log: if non-zero, log_lv will be removed
467 * (even if it's 0, log_lv will be removed if there is no
468 * mirror remaining after the removal)
469 * collapse: if non-zero, instead of removing, remove the temporary
470 * mirror layer and merge mirrors to the original LV.
471 * removable_pvs should be NULL and num_removed should be
472 * seg->area_count - 1.
473 * removed: if non NULL, the number of removed mirror images is set
474 * as a result
475 *
476 * If collapse is non-zero, <removed> is guaranteed to be equal to num_removed.
477 *
478 * Return values:
479 * Failure (0) means something unexpected has happend and
480 * the caller should abort.
481 * Even if no mirror was removed (e.g. no LV matches to 'removable_pvs'),
482 * returns success (1).
483 */
484 static int _remove_mirror_images(struct logical_volume *lv,
485 uint32_t num_removed,
486 struct dm_list *removable_pvs,
487 unsigned remove_log, unsigned collapse,
488 uint32_t *removed)
489 {
490 uint32_t m;
491 uint32_t s;
492 struct logical_volume *sub_lv;
493 struct logical_volume *detached_log_lv = NULL;
494 struct logical_volume *lv1 = NULL;
495 struct lv_segment *mirrored_seg = first_seg(lv);
496 uint32_t old_area_count = mirrored_seg->area_count;
497 uint32_t new_area_count = mirrored_seg->area_count;
498 struct lv_list *lvl;
499 struct dm_list tmp_orphan_lvs;
500
501 if (removed)
502 *removed = 0;
503
504 log_very_verbose("Reducing mirror set from %" PRIu32 " to %"
505 PRIu32 " image(s)%s.",
506 old_area_count, old_area_count - num_removed,
507 remove_log ? " and no log volume" : "");
508
509 if (collapse &&
510 (removable_pvs || (old_area_count - num_removed != 1))) {
511 log_error("Incompatible parameters to _remove_mirror_images");
512 return 0;
513 }
514
515 /* Move removable_pvs to end of array */
516 if (removable_pvs) {
517 for (s = 0; s < mirrored_seg->area_count &&
518 old_area_count - new_area_count < num_removed; s++) {
519 sub_lv = seg_lv(mirrored_seg, s);
520
521 if (!is_temporary_mirror_layer(sub_lv) &&
522 _is_mirror_image_removable(sub_lv, removable_pvs)) {
523 if (!shift_mirror_images(mirrored_seg, s))
524 return_0;
525 new_area_count--;
526 }
527 }
528 if (num_removed && old_area_count == new_area_count)
529 return 1;
530 } else
531 new_area_count = old_area_count - num_removed;
532
533 /* Remove mimage LVs from the segment */
534 dm_list_init(&tmp_orphan_lvs);
535 for (m = new_area_count; m < mirrored_seg->area_count; m++) {
536 seg_lv(mirrored_seg, m)->status &= ~MIRROR_IMAGE;
537 seg_lv(mirrored_seg, m)->status |= VISIBLE_LV;
538 if (!(lvl = dm_pool_alloc(lv->vg->cmd->mem, sizeof(*lvl)))) {
539 log_error("lv_list alloc failed");
540 return 0;
541 }
542 lvl->lv = seg_lv(mirrored_seg, m);
543 dm_list_add(&tmp_orphan_lvs, &lvl->list);
544 release_lv_segment_area(mirrored_seg, m, mirrored_seg->area_len);
545 }
546 mirrored_seg->area_count = new_area_count;
547
548 /* If no more mirrors, remove mirror layer */
549 /* As an exceptional case, if the lv is temporary layer,
550 * leave the LV as mirrored and let the lvconvert completion
551 * to remove the layer. */
552 if (new_area_count == 1 && !is_temporary_mirror_layer(lv)) {
553 lv1 = seg_lv(mirrored_seg, 0);
554 lv1->status &= ~MIRROR_IMAGE;
555 lv1->status |= VISIBLE_LV;
556 detached_log_lv = detach_mirror_log(mirrored_seg);
557 if (!remove_layer_from_lv(lv, lv1))
558 return_0;
559 lv->status &= ~MIRRORED;
560 lv->status &= ~MIRROR_NOTSYNCED;
561 if (collapse && !_merge_mirror_images(lv, &tmp_orphan_lvs)) {
562 log_error("Failed to add mirror images");
563 return 0;
564 }
565 } else if (new_area_count == 0) {
566 log_very_verbose("All mimages of %s are gone", lv->name);
567
568 /* All mirror images are gone.
569 * It can happen for vgreduce --removemissing. */
570 detached_log_lv = detach_mirror_log(mirrored_seg);
571 lv->status &= ~MIRRORED;
572 lv->status &= ~MIRROR_NOTSYNCED;
573 if (!replace_lv_with_error_segment(lv))
574 return_0;
575 } else if (remove_log)
576 detached_log_lv = detach_mirror_log(mirrored_seg);
577
578 /*
579 * To successfully remove these unwanted LVs we need to
580 * remove the LVs from the mirror set, commit that metadata
581 * then deactivate and remove them fully.
582 */
583
584 if (!vg_write(mirrored_seg->lv->vg)) {
585 log_error("intermediate VG write failed.");
586 return 0;
587 }
588
589 if (!suspend_lv(mirrored_seg->lv->vg->cmd, mirrored_seg->lv)) {
590 log_error("Failed to lock %s", mirrored_seg->lv->name);
591 vg_revert(mirrored_seg->lv->vg);
592 return 0;
593 }
594
595 if (!vg_commit(mirrored_seg->lv->vg)) {
596 resume_lv(mirrored_seg->lv->vg->cmd, mirrored_seg->lv);
597 return 0;
598 }
599
600 log_very_verbose("Updating \"%s\" in kernel", mirrored_seg->lv->name);
601
602 /*
603 * Avoid having same mirror target loaded twice simultaneously by first
604 * resuming the removed LV which now contains an error segment.
605 * As it's now detached from mirrored_seg->lv we must resume it
606 * explicitly.
607 */
608 if (lv1 && !resume_lv(lv1->vg->cmd, lv1)) {
609 log_error("Problem resuming temporary LV, %s", lv1->name);
610 return 0;
611 }
612
613 if (!resume_lv(mirrored_seg->lv->vg->cmd, mirrored_seg->lv)) {
614 log_error("Problem reactivating %s", mirrored_seg->lv->name);
615 return 0;
616 }
617
618 /* Save or delete the 'orphan' LVs */
619 if (!collapse) {
620 dm_list_iterate_items(lvl, &tmp_orphan_lvs)
621 if (!_delete_lv(lv, lvl->lv))
622 return_0;
623 }
624
625 if (lv1 && !_delete_lv(lv, lv1))
626 return_0;
627
628 if (detached_log_lv && !_delete_lv(lv, detached_log_lv))
629 return_0;
630
631 /* Mirror with only 1 area is 'in sync'. */
632 if (new_area_count == 1 && is_temporary_mirror_layer(lv)) {
633 if (first_seg(lv)->log_lv &&
634 !_init_mirror_log(lv->vg->cmd, first_seg(lv)->log_lv,
635 1, &lv->tags, 0)) {
636 /* As a result, unnecessary sync may run after
637 * collapsing. But safe.*/
638 log_error("Failed to initialize log device");
639 return_0;
640 }
641 }
642
643 if (removed)
644 *removed = old_area_count - new_area_count;
645
646 log_very_verbose("%" PRIu32 " image(s) removed from %s",
647 old_area_count - num_removed, lv->name);
648
649 return 1;
650 }
651
652 /*
653 * Remove the number of mirror images from the LV
654 */
655 int remove_mirror_images(struct logical_volume *lv, uint32_t num_mirrors,
656 struct dm_list *removable_pvs, unsigned remove_log)
657 {
658 uint32_t num_removed, removed_once, r;
659 uint32_t existing_mirrors = lv_mirror_count(lv);
660 struct logical_volume *next_lv = lv;
661
662 num_removed = existing_mirrors - num_mirrors;
663
664 /* num_removed can be 0 if the function is called just to remove log */
665 do {
666 if (num_removed < first_seg(next_lv)->area_count)
667 removed_once = num_removed;
668 else
669 removed_once = first_seg(next_lv)->area_count - 1;
670
671 if (!_remove_mirror_images(next_lv, removed_once,
672 removable_pvs, remove_log, 0, &r))
673 return_0;
674
675 if (r < removed_once) {
676 /* Some mirrors are removed from the temporary mirror,
677 * but the temporary layer still exists.
678 * Down the stack and retry for remainder. */
679 next_lv = find_temporary_mirror(next_lv);
680 }
681
682 num_removed -= r;
683 } while (next_lv && num_removed);
684
685 if (num_removed) {
686 if (num_removed == existing_mirrors - num_mirrors)
687 log_error("No mirror images found using specified PVs.");
688 else {
689 log_error("%u images are removed out of requested %u.",
690 existing_mirrors - lv_mirror_count(lv),
691 existing_mirrors - num_mirrors);
692 }
693 return 0;
694 }
695
696 return 1;
697 }
698
699 static int _mirrored_lv_in_sync(struct logical_volume *lv)
700 {
701 float sync_percent;
702
703 if (!lv_mirror_percent(lv->vg->cmd, lv, 0, &sync_percent, NULL)) {
704 log_error("Unable to determine mirror sync status of %s/%s.",
705 lv->vg->name, lv->name);
706 return 0;
707 }
708
709 if (sync_percent >= 100.0)
710 return 1;
711
712 return 0;
713 }
714
715 /*
716 * Collapsing temporary mirror layers.
717 *
718 * When mirrors are added to already-mirrored LV, a temporary mirror layer
719 * is inserted at the top of the stack to reduce resync work.
720 * The function will remove the intermediate layer and collapse the stack
721 * as far as mirrors are in-sync.
722 *
723 * The function is destructive: to remove intermediate mirror layers,
724 * VG metadata commits and suspend/resume are necessary.
725 */
726 int collapse_mirrored_lv(struct logical_volume *lv)
727 {
728 struct logical_volume *tmp_lv;
729 struct lv_segment *mirror_seg;
730
731 while ((tmp_lv = find_temporary_mirror(lv))) {
732 mirror_seg = find_mirror_seg(first_seg(tmp_lv));
733 if (!mirror_seg) {
734 log_error("Failed to find mirrored LV for %s",
735 tmp_lv->name);
736 return 0;
737 }
738
739 if (!_mirrored_lv_in_sync(mirror_seg->lv)) {
740 log_verbose("Not collapsing %s: out-of-sync",
741 mirror_seg->lv->name);
742 return 1;
743 }
744
745 if (!_remove_mirror_images(mirror_seg->lv,
746 mirror_seg->area_count - 1,
747 NULL, 1, 1, NULL)) {
748 log_error("Failed to release mirror images");
749 return 0;
750 }
751 }
752
753 return 1;
754 }
755
756 static int get_mirror_fault_policy(struct cmd_context *cmd __attribute((unused)),
757 int log_policy)
758 {
759 const char *policy;
760
761 if (log_policy)
762 policy = find_config_str(NULL, "activation/mirror_log_fault_policy",
763 DEFAULT_MIRROR_LOG_FAULT_POLICY);
764 else
765 policy = find_config_str(NULL, "activation/mirror_device_fault_policy",
766 DEFAULT_MIRROR_DEV_FAULT_POLICY);
767
768 if (!strcmp(policy, "remove"))
769 return MIRROR_REMOVE;
770 else if (!strcmp(policy, "allocate"))
771 return MIRROR_ALLOCATE;
772 else if (!strcmp(policy, "allocate_anywhere"))
773 return MIRROR_ALLOCATE_ANYWHERE;
774
775 if (log_policy)
776 log_error("Bad activation/mirror_log_fault_policy");
777 else
778 log_error("Bad activation/mirror_device_fault_policy");
779
780 return MIRROR_REMOVE;
781 }
782
783 static int get_mirror_log_fault_policy(struct cmd_context *cmd)
784 {
785 return get_mirror_fault_policy(cmd, 1);
786 }
787
788 static int get_mirror_device_fault_policy(struct cmd_context *cmd)
789 {
790 return get_mirror_fault_policy(cmd, 0);
791 }
792
793 /*
794 * replace_mirror_images
795 * @mirrored_seg: segment (which may be linear now) to restore
796 * @num_mirrors: number of copies we should end up with
797 * @replace_log: replace log if not present
798 * @in_sync: was the original mirror in-sync?
799 *
800 * in_sync will be set to 0 if new mirror devices are being added
801 * In other words, it is only useful if the log (and only the log)
802 * is being restored.
803 *
804 * Returns: 0 on failure, 1 on reconfig, -1 if no reconfig done
805 */
806 static int replace_mirror_images(struct lv_segment *mirrored_seg,
807 uint32_t num_mirrors,
808 int log_policy, int in_sync)
809 {
810 int r = -1;
811 struct logical_volume *lv = mirrored_seg->lv;
812
813 /* FIXME: Use lvconvert rather than duplicating its code */
814
815 if (mirrored_seg->area_count < num_mirrors) {
816 log_error("WARNING: Failed to replace mirror device in %s/%s",
817 mirrored_seg->lv->vg->name, mirrored_seg->lv->name);
818
819 if ((mirrored_seg->area_count > 1) && !mirrored_seg->log_lv)
820 log_error("WARNING: Use 'lvconvert -m %d %s/%s --corelog' to replace failed devices",
821 num_mirrors - 1, lv->vg->name, lv->name);
822 else
823 log_error("WARNING: Use 'lvconvert -m %d %s/%s' to replace failed devices",
824 num_mirrors - 1, lv->vg->name, lv->name);
825 r = 0;
826
827 /* REMEMBER/FIXME: set in_sync to 0 if a new mirror device was added */
828 in_sync = 0;
829 }
830
831 /*
832 * FIXME: right now, we ignore the allocation policy specified to
833 * allocate the new log.
834 */
835 if ((mirrored_seg->area_count > 1) && !mirrored_seg->log_lv &&
836 (log_policy != MIRROR_REMOVE)) {
837 log_error("WARNING: Failed to replace mirror log device in %s/%s",
838 lv->vg->name, lv->name);
839
840 log_error("WARNING: Use 'lvconvert -m %d %s/%s' to replace failed devices",
841 mirrored_seg->area_count - 1 , lv->vg->name, lv->name);
842 r = 0;
843 }
844
845 return r;
846 }
847
848 int reconfigure_mirror_images(struct lv_segment *mirrored_seg, uint32_t num_mirrors,
849 struct dm_list *removable_pvs, unsigned remove_log)
850 {
851 int r;
852 int in_sync;
853 int log_policy, dev_policy;
854 uint32_t old_num_mirrors = mirrored_seg->area_count;
855 int had_log = (mirrored_seg->log_lv) ? 1 : 0;
856
857 /* was the mirror in-sync before problems? */
858 in_sync = _mirrored_lv_in_sync(mirrored_seg->lv);
859
860 /*
861 * While we are only removing devices, we can have sync set.
862 * Setting this is only useful if we are moving to core log
863 * otherwise the disk log will contain the sync information
864 */
865 init_mirror_in_sync(in_sync);
866
867 r = _remove_mirror_images(mirrored_seg->lv, old_num_mirrors - num_mirrors,
868 removable_pvs, remove_log, 0, NULL);
869 if (!r)
870 /* Unable to remove bad devices */
871 return 0;
872
873 log_warn("WARNING: Bad device removed from mirror volume, %s/%s",
874 mirrored_seg->lv->vg->name, mirrored_seg->lv->name);
875
876 log_policy = get_mirror_log_fault_policy(mirrored_seg->lv->vg->cmd);
877 dev_policy = get_mirror_device_fault_policy(mirrored_seg->lv->vg->cmd);
878
879 r = replace_mirror_images(mirrored_seg,
880 (dev_policy != MIRROR_REMOVE) ?
881 old_num_mirrors : num_mirrors,
882 log_policy, in_sync);
883
884 if (!r)
885 /* Failed to replace device(s) */
886 log_error("WARNING: Unable to find substitute device for mirror volume, %s/%s",
887 mirrored_seg->lv->vg->name, mirrored_seg->lv->name);
888 else if (r > 0)
889 /* Success in replacing device(s) */
890 log_warn("WARNING: Mirror volume, %s/%s restored - substitute for failed device found.",
891 mirrored_seg->lv->vg->name, mirrored_seg->lv->name);
892 else
893 /* Bad device removed, but not replaced because of policy */
894 if (mirrored_seg->area_count == 1) {
895 log_warn("WARNING: Mirror volume, %s/%s converted to linear due to device failure.",
896 mirrored_seg->lv->vg->name, mirrored_seg->lv->name);
897 } else if (had_log && !mirrored_seg->log_lv) {
898 log_warn("WARNING: Mirror volume, %s/%s disk log removed due to device failure.",
899 mirrored_seg->lv->vg->name, mirrored_seg->lv->name);
900 }
901 /*
902 * If we made it here, we at least removed the bad device.
903 * Consider this success.
904 */
905 return 1;
906 }
907
908 static int _create_mimage_lvs(struct alloc_handle *ah,
909 uint32_t num_mirrors,
910 struct logical_volume *lv,
911 struct logical_volume **img_lvs)
912 {
913 uint32_t m;
914 char *img_name;
915 size_t len;
916
917 len = strlen(lv->name) + 32;
918 if (!(img_name = alloca(len))) {
919 log_error("img_name allocation failed. "
920 "Remove new LV and retry.");
921 return 0;
922 }
923
924 if (dm_snprintf(img_name, len, "%s_mimage_%%d", lv->name) < 0) {
925 log_error("img_name allocation failed. "
926 "Remove new LV and retry.");
927 return 0;
928 }
929
930 for (m = 0; m < num_mirrors; m++) {
931 if (!(img_lvs[m] = lv_create_empty(img_name,
932 NULL, LVM_READ | LVM_WRITE,
933 ALLOC_INHERIT, 0, lv->vg))) {
934 log_error("Aborting. Failed to create mirror image LV. "
935 "Remove new LV and retry.");
936 return 0;
937 }
938
939 if (!lv_add_segment(ah, m, 1, img_lvs[m],
940 get_segtype_from_string(lv->vg->cmd,
941 "striped"),
942 0, 0, 0, NULL)) {
943 log_error("Aborting. Failed to add mirror image segment "
944 "to %s. Remove new LV and retry.",
945 img_lvs[m]->name);
946 return 0;
947 }
948 }
949
950 return 1;
951 }
952
953 /*
954 * Remove mirrors from each segment.
955 * 'new_mirrors' is the number of mirrors after the removal. '0' for linear.
956 * If 'status_mask' is non-zero, the removal happens only when all segments
957 * has the status bits on.
958 */
959 int remove_mirrors_from_segments(struct logical_volume *lv,
960 uint32_t new_mirrors, uint32_t status_mask)
961 {
962 struct lv_segment *seg;
963 uint32_t s;
964
965 /* Check the segment params are compatible */
966 dm_list_iterate_items(seg, &lv->segments) {
967 if (!seg_is_mirrored(seg)) {
968 log_error("Segment is not mirrored: %s:%" PRIu32,
969 lv->name, seg->le);
970 return 0;
971 } if ((seg->status & status_mask) != status_mask) {
972 log_error("Segment status does not match: %s:%" PRIu32
973 " status:0x%x/0x%x", lv->name, seg->le,
974 seg->status, status_mask);
975 return 0;
976 }
977 }
978
979 /* Convert the segments */
980 dm_list_iterate_items(seg, &lv->segments) {
981 if (!new_mirrors && seg->extents_copied == seg->area_len) {
982 if (!move_lv_segment_area(seg, 0, seg, 1))
983 return_0;
984 }
985
986 for (s = new_mirrors + 1; s < seg->area_count; s++)
987 release_lv_segment_area(seg, s, seg->area_len);
988
989 seg->area_count = new_mirrors + 1;
990
991 if (!new_mirrors)
992 seg->segtype = get_segtype_from_string(lv->vg->cmd,
993 "striped");
994 }
995
996 return 1;
997 }
998
999 const char *get_pvmove_pvname_from_lv_mirr(struct logical_volume *lv_mirr)
1000 {
1001 struct lv_segment *seg;
1002
1003 dm_list_iterate_items(seg, &lv_mirr->segments) {
1004 if (!seg_is_mirrored(seg))
1005 continue;
1006 if (seg_type(seg, 0) != AREA_PV)
1007 continue;
1008 return dev_name(seg_dev(seg, 0));
1009 }
1010
1011 return NULL;
1012 }
1013
1014 const char *get_pvmove_pvname_from_lv(struct logical_volume *lv)
1015 {
1016 struct lv_segment *seg;
1017 uint32_t s;
1018
1019 dm_list_iterate_items(seg, &lv->segments) {
1020 for (s = 0; s < seg->area_count; s++) {
1021 if (seg_type(seg, s) != AREA_LV)
1022 continue;
1023 return get_pvmove_pvname_from_lv_mirr(seg_lv(seg, s));
1024 }
1025 }
1026
1027 return NULL;
1028 }
1029
1030 struct logical_volume *find_pvmove_lv(struct volume_group *vg,
1031 struct device *dev,
1032 uint32_t lv_type)
1033 {
1034 struct lv_list *lvl;
1035 struct logical_volume *lv;
1036 struct lv_segment *seg;
1037
1038 /* Loop through all LVs */
1039 dm_list_iterate_items(lvl, &vg->lvs) {
1040 lv = lvl->lv;
1041
1042 if (!(lv->status & lv_type))
1043 continue;
1044
1045 /* Check segment origins point to pvname */
1046 dm_list_iterate_items(seg, &lv->segments) {
1047 if (seg_type(seg, 0) != AREA_PV)
1048 continue;
1049 if (seg_dev(seg, 0) != dev)
1050 continue;
1051 return lv;
1052 }
1053 }
1054
1055 return NULL;
1056 }
1057
1058 struct logical_volume *find_pvmove_lv_from_pvname(struct cmd_context *cmd,
1059 struct volume_group *vg,
1060 const char *name,
1061 uint32_t lv_type)
1062 {
1063 struct physical_volume *pv;
1064
1065 if (!(pv = find_pv_by_name(cmd, name)))
1066 return_NULL;
1067
1068 return find_pvmove_lv(vg, pv->dev, lv_type);
1069 }
1070
1071 struct dm_list *lvs_using_lv(struct cmd_context *cmd, struct volume_group *vg,
1072 struct logical_volume *lv)
1073 {
1074 struct dm_list *lvs;
1075 struct logical_volume *lv1;
1076 struct lv_list *lvl, *lvl1;
1077 struct lv_segment *seg;
1078 uint32_t s;
1079
1080 if (!(lvs = dm_pool_alloc(cmd->mem, sizeof(*lvs)))) {
1081 log_error("lvs list alloc failed");
1082 return NULL;
1083 }
1084
1085 dm_list_init(lvs);
1086
1087 /* Loop through all LVs except the one supplied */
1088 dm_list_iterate_items(lvl1, &vg->lvs) {
1089 lv1 = lvl1->lv;
1090 if (lv1 == lv)
1091 continue;
1092
1093 /* Find whether any segment points at the supplied LV */
1094 dm_list_iterate_items(seg, &lv1->segments) {
1095 for (s = 0; s < seg->area_count; s++) {
1096 if (seg_type(seg, s) != AREA_LV ||
1097 seg_lv(seg, s) != lv)
1098 continue;
1099 if (!(lvl = dm_pool_alloc(cmd->mem, sizeof(*lvl)))) {
1100 log_error("lv_list alloc failed");
1101 return NULL;
1102 }
1103 lvl->lv = lv1;
1104 dm_list_add(lvs, &lvl->list);
1105 goto next_lv;
1106 }
1107 }
1108 next_lv:
1109 ;
1110 }
1111
1112 return lvs;
1113 }
1114
1115 float copy_percent(struct logical_volume *lv_mirr)
1116 {
1117 uint32_t numerator = 0u, denominator = 0u;
1118 struct lv_segment *seg;
1119
1120 dm_list_iterate_items(seg, &lv_mirr->segments) {
1121 denominator += seg->area_len;
1122
1123 if (seg_is_mirrored(seg) && seg->area_count > 1)
1124 numerator += seg->extents_copied;
1125 else
1126 numerator += seg->area_len;
1127 }
1128
1129 return denominator ? (float) numerator *100 / denominator : 100.0;
1130 }
1131
1132 /*
1133 * Fixup mirror pointers after single-pass segment import
1134 */
1135 int fixup_imported_mirrors(struct volume_group *vg)
1136 {
1137 struct lv_list *lvl;
1138 struct lv_segment *seg;
1139
1140 dm_list_iterate_items(lvl, &vg->lvs) {
1141 dm_list_iterate_items(seg, &lvl->lv->segments) {
1142 if (seg->segtype !=
1143 get_segtype_from_string(vg->cmd, "mirror"))
1144 continue;
1145
1146 if (seg->log_lv && !add_seg_to_segs_using_this_lv(seg->log_lv, seg))
1147 return_0;
1148 }
1149 }
1150
1151 return 1;
1152 }
1153
1154 /*
1155 * Add mirrors to "linear" or "mirror" segments
1156 */
1157 int add_mirrors_to_segments(struct cmd_context *cmd, struct logical_volume *lv,
1158 uint32_t mirrors, uint32_t region_size,
1159 struct dm_list *allocatable_pvs, alloc_policy_t alloc)
1160 {
1161 struct alloc_handle *ah;
1162 const struct segment_type *segtype;
1163 struct dm_list *parallel_areas;
1164 uint32_t adjusted_region_size;
1165
1166 if (!(parallel_areas = build_parallel_areas_from_lv(cmd, lv)))
1167 return_0;
1168
1169 if (!(segtype = get_segtype_from_string(cmd, "mirror")))
1170 return_0;
1171
1172 adjusted_region_size = adjusted_mirror_region_size(lv->vg->extent_size,
1173 lv->le_count,
1174 region_size);
1175
1176 if (!(ah = allocate_extents(lv->vg, NULL, segtype, 1, mirrors, 0, 0,
1177 lv->le_count, allocatable_pvs, alloc,
1178 parallel_areas))) {
1179 log_error("Unable to allocate mirror extents for %s.", lv->name);
1180 return 0;
1181 }
1182
1183 if (!lv_add_mirror_areas(ah, lv, 0, adjusted_region_size)) {
1184 log_error("Failed to add mirror areas to %s", lv->name);
1185 return 0;
1186 }
1187
1188 return 1;
1189 }
1190
1191 /*
1192 * Convert mirror log
1193 *
1194 * FIXME: Can't handle segment-by-segment mirror (like pvmove)
1195 */
1196 int remove_mirror_log(struct cmd_context *cmd,
1197 struct logical_volume *lv,
1198 struct dm_list *removable_pvs)
1199 {
1200 float sync_percent;
1201 struct lvinfo info;
1202 struct volume_group *vg = lv->vg;
1203
1204 /* Unimplemented features */
1205 if (dm_list_size(&lv->segments) != 1) {
1206 log_error("Multiple-segment mirror is not supported");
1207 return 0;
1208 }
1209
1210 /* Had disk log, switch to core. */
1211 if (lv_info(cmd, lv, &info, 0, 0) && info.exists) {
1212 if (!lv_mirror_percent(cmd, lv, 0, &sync_percent, NULL)) {
1213 log_error("Unable to determine mirror sync status.");
1214 return 0;
1215 }
1216 } else if (vg_is_clustered(vg)) {
1217 log_error("Unable to convert the log of inactive "
1218 "cluster mirror %s", lv->name);
1219 return 0;
1220 } else if (yes_no_prompt("Full resync required to convert "
1221 "inactive mirror %s to core log. "
1222 "Proceed? [y/n]: "))
1223 sync_percent = 0;
1224 else
1225 return 0;
1226
1227 if (sync_percent >= 100.0)
1228 init_mirror_in_sync(1);
1229 else {
1230 /* A full resync will take place */
1231 lv->status &= ~MIRROR_NOTSYNCED;
1232 init_mirror_in_sync(0);
1233 }
1234
1235 if (!remove_mirror_images(lv, lv_mirror_count(lv),
1236 removable_pvs, 1U))
1237 return_0;
1238
1239 return 1;
1240 }
1241
1242 static struct logical_volume *_create_mirror_log(struct logical_volume *lv,
1243 struct alloc_handle *ah,
1244 alloc_policy_t alloc,
1245 const char *lv_name,
1246 const char *suffix)
1247 {
1248 struct logical_volume *log_lv;
1249 char *log_name;
1250 size_t len;
1251
1252 len = strlen(lv_name) + 32;
1253 if (!(log_name = alloca(len))) {
1254 log_error("log_name allocation failed.");
1255 return NULL;
1256 }
1257
1258 if (dm_snprintf(log_name, len, "%s%s", lv_name, suffix) < 0) {
1259 log_error("log_name allocation failed.");
1260 return NULL;
1261 }
1262
1263 if (!(log_lv = lv_create_empty(log_name, NULL,
1264 VISIBLE_LV | LVM_READ | LVM_WRITE,
1265 alloc, 0, lv->vg)))
1266 return_NULL;
1267
1268 if (!lv_add_log_segment(ah, log_lv))
1269 return_NULL;
1270
1271 return log_lv;
1272 }
1273
1274 static struct logical_volume *_set_up_mirror_log(struct cmd_context *cmd,
1275 struct alloc_handle *ah,
1276 struct logical_volume *lv,
1277 uint32_t log_count,
1278 uint32_t region_size __attribute((unused)),
1279 alloc_policy_t alloc,
1280 int in_sync)
1281 {
1282 struct logical_volume *log_lv;
1283 const char *suffix, *c;
1284 char *lv_name;
1285 size_t len;
1286 struct lv_segment *seg;
1287
1288 init_mirror_in_sync(in_sync);
1289
1290 if (log_count != 1) {
1291 log_error("log_count != 1 is not supported.");
1292 return NULL;
1293 }
1294
1295 /* Mirror log name is lv_name + suffix, determined as the following:
1296 * 1. suffix is:
1297 * o "_mlog" for the original mirror LV.
1298 * o "_mlogtmp_%d" for temporary mirror LV,
1299 * 2. lv_name is:
1300 * o lv->name, if the log is temporary
1301 * o otherwise, the top-level LV name
1302 */
1303 seg = first_seg(lv);
1304 if (seg_type(seg, 0) == AREA_LV &&
1305 strstr(seg_lv(seg, 0)->name, MIRROR_SYNC_LAYER)) {
1306 lv_name = lv->name;
1307 suffix = "_mlogtmp_%d";
1308 } else if ((c = strstr(lv->name, MIRROR_SYNC_LAYER))) {
1309 len = c - lv->name + 1;
1310 if (!(lv_name = alloca(len)) ||
1311 !dm_snprintf(lv_name, len, "%s", lv->name)) {
1312 log_error("mirror log name allocation failed");
1313 return 0;
1314 }
1315 suffix = "_mlog";
1316 } else {
1317 lv_name = lv->name;
1318 suffix = "_mlog";
1319 }
1320
1321 if (!(log_lv = _create_mirror_log(lv, ah, alloc,
1322 (const char *) lv_name, suffix))) {
1323 log_error("Failed to create mirror log.");
1324 return NULL;
1325 }
1326
1327 if (!_init_mirror_log(cmd, log_lv, in_sync, &lv->tags, 1)) {
1328 log_error("Failed to create mirror log.");
1329 return NULL;
1330 }
1331
1332 return log_lv;
1333 }
1334
1335 int attach_mirror_log(struct lv_segment *seg, struct logical_volume *log_lv)
1336 {
1337 seg->log_lv = log_lv;
1338 log_lv->status |= MIRROR_LOG;
1339 log_lv->status &= ~VISIBLE_LV;
1340 return add_seg_to_segs_using_this_lv(log_lv, seg);
1341 }
1342
1343 int add_mirror_log(struct cmd_context *cmd, struct logical_volume *lv,
1344 uint32_t log_count, uint32_t region_size,
1345 struct dm_list *allocatable_pvs, alloc_policy_t alloc)
1346 {
1347 struct alloc_handle *ah;
1348 const struct segment_type *segtype;
1349 struct dm_list *parallel_areas;
1350 float sync_percent;
1351 int in_sync;
1352 struct logical_volume *log_lv;
1353 struct lvinfo info;
1354
1355 /* Unimplemented features */
1356 if (log_count > 1) {
1357 log_error("log_count > 1 is not supported");
1358 return 0;
1359 }
1360
1361 if (dm_list_size(&lv->segments) != 1) {
1362 log_error("Multiple-segment mirror is not supported");
1363 return 0;
1364 }
1365
1366 /*
1367 * We are unable to convert the log of inactive cluster mirrors
1368 * due to the inability to detect whether the mirror is active
1369 * on remote nodes (even though it is inactive on this node)
1370 */
1371 if (vg_is_clustered(lv->vg) &&
1372 !(lv_info(cmd, lv, &info, 0, 0) && info.exists)) {
1373 log_error("Unable to convert the log of inactive "
1374 "cluster mirror %s", lv->name);
1375 return 0;
1376 }
1377
1378 if (!(parallel_areas = build_parallel_areas_from_lv(cmd, lv)))
1379 return_0;
1380
1381 if (!(segtype = get_segtype_from_string(cmd, "mirror")))
1382 return_0;
1383
1384 if (activation() && segtype->ops->target_present &&
1385 !segtype->ops->target_present(NULL, NULL)) {
1386 log_error("%s: Required device-mapper target(s) not "
1387 "detected in your kernel", segtype->name);
1388 return 0;
1389 }
1390
1391 /* allocate destination extents */
1392 ah = allocate_extents(lv->vg, NULL, segtype,
1393 0, 0, log_count, region_size, 0,
1394 allocatable_pvs, alloc, parallel_areas);
1395 if (!ah) {
1396 log_error("Unable to allocate extents for mirror log.");
1397 return 0;
1398 }
1399
1400 /* check sync status */
1401 if (lv_mirror_percent(cmd, lv, 0, &sync_percent, NULL) &&
1402 sync_percent >= 100.0)
1403 in_sync = 1;
1404 else
1405 in_sync = 0;
1406
1407 if (!(log_lv = _set_up_mirror_log(cmd, ah, lv, log_count,
1408 region_size, alloc, in_sync)))
1409 return_0;
1410
1411 if (!attach_mirror_log(first_seg(lv), log_lv))
1412 return_0;
1413
1414 alloc_destroy(ah);
1415 return 1;
1416 }
1417
1418 /*
1419 * Convert "linear" LV to "mirror".
1420 */
1421 int add_mirror_images(struct cmd_context *cmd, struct logical_volume *lv,
1422 uint32_t mirrors, uint32_t stripes, uint32_t region_size,
1423 struct dm_list *allocatable_pvs, alloc_policy_t alloc,
1424 uint32_t log_count)
1425 {
1426 struct alloc_handle *ah;
1427 const struct segment_type *segtype;
1428 struct dm_list *parallel_areas;
1429 struct logical_volume **img_lvs;
1430 struct logical_volume *log_lv = NULL;
1431
1432 if (stripes > 1) {
1433 log_error("stripes > 1 is not supported");
1434 return 0;
1435 }
1436
1437 /*
1438 * allocate destination extents
1439 */
1440
1441 if (!(parallel_areas = build_parallel_areas_from_lv(cmd, lv)))
1442 return_0;
1443
1444 if (!(segtype = get_segtype_from_string(cmd, "mirror")))
1445 return_0;
1446
1447 ah = allocate_extents(lv->vg, NULL, segtype,
1448 stripes, mirrors, log_count, region_size, lv->le_count,
1449 allocatable_pvs, alloc, parallel_areas);
1450 if (!ah) {
1451 log_error("Unable to allocate extents for mirror(s).");
1452 return 0;
1453 }
1454
1455 /*
1456 * create and initialize mirror log
1457 */
1458 if (log_count &&
1459 !(log_lv = _set_up_mirror_log(cmd, ah, lv, log_count, region_size,
1460 alloc, mirror_in_sync())))
1461 return_0;
1462
1463 /* The log initialization involves vg metadata commit.
1464 So from here on, if failure occurs, the log must be explicitly
1465 removed and the updated vg metadata should be committed. */
1466
1467 /*
1468 * insert a mirror layer
1469 */
1470 if (dm_list_size(&lv->segments) != 1 ||
1471 seg_type(first_seg(lv), 0) != AREA_LV)
1472 if (!insert_layer_for_lv(cmd, lv, 0, "_mimage_%d"))
1473 goto out_remove_log;
1474
1475 /*
1476 * create mirror image LVs
1477 */
1478 if (!(img_lvs = alloca(sizeof(*img_lvs) * mirrors))) {
1479 log_error("img_lvs allocation failed. "
1480 "Remove new LV and retry.");
1481 goto out_remove_log;
1482 }
1483
1484 if (!_create_mimage_lvs(ah, mirrors, lv, img_lvs))
1485 goto out_remove_log;
1486
1487 if (!lv_add_mirror_lvs(lv, img_lvs, mirrors,
1488 MIRROR_IMAGE | (lv->status & LOCKED),
1489 region_size)) {
1490 log_error("Aborting. Failed to add mirror segment. "
1491 "Remove new LV and retry.");
1492 goto out_remove_imgs;
1493 }
1494
1495 if (log_count && !attach_mirror_log(first_seg(lv), log_lv))
1496 stack;
1497
1498 alloc_destroy(ah);
1499 return 1;
1500
1501 out_remove_log:
1502 if (log_lv && (!lv_remove(log_lv) || !vg_write(log_lv->vg) ||
1503 (backup(log_lv->vg), !vg_commit(log_lv->vg))))
1504 log_error("Manual intervention may be required to remove "
1505 "abandoned log LV before retrying.");
1506
1507 out_remove_imgs:
1508 return 0;
1509 }
1510
1511 /*
1512 * Generic interface for adding mirror and/or mirror log.
1513 * 'mirror' is the number of mirrors to be added.
1514 * 'pvs' is either allocatable pvs.
1515 */
1516 int lv_add_mirrors(struct cmd_context *cmd, struct logical_volume *lv,
1517 uint32_t mirrors, uint32_t stripes,
1518 uint32_t region_size, uint32_t log_count,
1519 struct dm_list *pvs, alloc_policy_t alloc, uint32_t flags)
1520 {
1521 if (!mirrors && !log_count) {
1522 log_error("No conversion is requested");
1523 return 0;
1524 }
1525
1526 /* For corelog mirror, activation code depends on
1527 * the global mirror_in_sync status. As we are adding
1528 * a new mirror, it should be set as 'out-of-sync'
1529 * so that the sync starts. */
1530 /* However, MIRROR_SKIP_INIT_SYNC even overrides it. */
1531 if (flags & MIRROR_SKIP_INIT_SYNC)
1532 init_mirror_in_sync(1);
1533 else if (!log_count)
1534 init_mirror_in_sync(0);
1535
1536 if (flags & MIRROR_BY_SEG) {
1537 if (log_count) {
1538 log_error("Persistent log is not supported on "
1539 "segment-by-segment mirroring");
1540 return 0;
1541 }
1542 if (stripes > 1) {
1543 log_error("Striped-mirroring is not supported on "
1544 "segment-by-segment mirroring");
1545 return 0;
1546 }
1547
1548 return add_mirrors_to_segments(cmd, lv, mirrors,
1549 region_size, pvs, alloc);
1550 } else if (flags & MIRROR_BY_LV) {
1551 if (!mirrors)
1552 return add_mirror_log(cmd, lv, log_count,
1553 region_size, pvs, alloc);
1554 return add_mirror_images(cmd, lv, mirrors,
1555 stripes, region_size,
1556 pvs, alloc, log_count);
1557 }
1558
1559 log_error("Unsupported mirror conversion type");
1560 return 0;
1561 }
1562
1563 /*
1564 * Generic interface for removing mirror and/or mirror log.
1565 * 'mirror' is the number of mirrors to be removed.
1566 * 'pvs' is removable pvs.
1567 */
1568 int lv_remove_mirrors(struct cmd_context *cmd __attribute((unused)),
1569 struct logical_volume *lv,
1570 uint32_t mirrors, uint32_t log_count, struct dm_list *pvs,
1571 uint32_t status_mask)
1572 {
1573 uint32_t new_mirrors;
1574 struct lv_segment *seg;
1575
1576 if (!mirrors && !log_count) {
1577 log_error("No conversion is requested");
1578 return 0;
1579 }
1580
1581 seg = first_seg(lv);
1582 if (!seg_is_mirrored(seg)) {
1583 log_error("Not a mirror segment");
1584 return 0;
1585 }
1586
1587 if (lv_mirror_count(lv) <= mirrors) {
1588 log_error("Removing more than existing: %d <= %d",
1589 seg->area_count, mirrors);
1590 return 0;
1591 }
1592 new_mirrors = lv_mirror_count(lv) - mirrors - 1;
1593
1594 /* MIRROR_BY_LV */
1595 if (seg_type(seg, 0) == AREA_LV &&
1596 seg_lv(seg, 0)->status & MIRROR_IMAGE)
1597 return remove_mirror_images(lv, new_mirrors + 1,
1598 pvs, log_count ? 1U : 0);
1599
1600 /* MIRROR_BY_SEG */
1601 if (log_count) {
1602 log_error("Persistent log is not supported on "
1603 "segment-by-segment mirroring");
1604 return 0;
1605 }
1606 return remove_mirrors_from_segments(lv, new_mirrors, status_mask);
1607 }
1608
1609