1 /* $NetBSD: mirror.c,v 1.1.1.3 2009/12/02 00:26:41 haad 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 #include "memlock.h" 30 31 #include "defaults.h" /* FIXME: should this be defaults.h? */ 32 33 /* These are necessary for _write_log_header() */ 34 #include "xlate.h" 35 #define MIRROR_MAGIC 0x4D695272 36 #define MIRROR_DISK_VERSION 2 37 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 42 43 /* 44 * Returns true if the lv is temporary mirror layer for resync 45 */ 46 int is_temporary_mirror_layer(const struct logical_volume *lv) 47 { 48 if (lv->status & MIRROR_IMAGE 49 && lv->status & MIRRORED 50 && !(lv->status & LOCKED)) 51 return 1; 52 53 return 0; 54 } 55 56 /* 57 * Return a temporary LV for resyncing added mirror image. 58 * Add other mirror legs to lvs list. 59 */ 60 struct logical_volume *find_temporary_mirror(const struct logical_volume *lv) 61 { 62 struct lv_segment *seg; 63 64 if (!(lv->status & MIRRORED)) 65 return NULL; 66 67 seg = first_seg(lv); 68 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); 73 74 return NULL; 75 } 76 77 /* 78 * Returns the number of mirrors of the LV 79 */ 80 uint32_t lv_mirror_count(const struct logical_volume *lv) 81 { 82 struct lv_segment *seg; 83 uint32_t s, mirrors; 84 85 if (!(lv->status & MIRRORED)) 86 return 1; 87 88 seg = first_seg(lv); 89 mirrors = seg->area_count; 90 91 for (s = 0; s < seg->area_count; s++) { 92 if (seg_type(seg, s) != AREA_LV) 93 continue; 94 if (is_temporary_mirror_layer(seg_lv(seg, s))) 95 mirrors += lv_mirror_count(seg_lv(seg, s)) - 1; 96 } 97 98 return mirrors; 99 } 100 101 struct lv_segment *find_mirror_seg(struct lv_segment *seg) 102 { 103 struct lv_segment *mirror_seg; 104 105 mirror_seg = get_only_segment_using_this_lv(seg->lv); 106 107 if (!mirror_seg) { 108 log_error("Failed to find mirror_seg for %s", seg->lv->name); 109 return NULL; 110 } 111 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); 115 return NULL; 116 } 117 118 return mirror_seg; 119 } 120 121 /* 122 * Reduce the region size if necessary to ensure 123 * the volume size is a multiple of the region size. 124 */ 125 uint32_t adjusted_mirror_region_size(uint32_t extent_size, uint32_t extents, 126 uint32_t region_size) 127 { 128 uint64_t region_max; 129 130 region_max = (1 << (ffs((int)extents) - 1)) * (uint64_t) extent_size; 131 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); 136 } 137 138 return region_size; 139 } 140 141 /* 142 * shift_mirror_images 143 * @mirrored_seg 144 * @mimage: The position (index) of the image to move to the end 145 * 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 152 * leg' to the end. 153 * 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. 161 * 162 * So, any time we want to move areas to the end to be removed, use 163 * this function. 164 */ 165 int shift_mirror_images(struct lv_segment *mirrored_seg, unsigned mimage) 166 { 167 int i; 168 struct lv_segment_area area; 169 170 if (mimage >= mirrored_seg->area_count) { 171 log_error("Invalid index (%u) of mirror image supplied " 172 "to shift_mirror_images()", mimage); 173 return 0; 174 } 175 176 area = mirrored_seg->areas[mimage]; 177 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]; 181 182 /* Place this one at the end */ 183 mirrored_seg->areas[i-1] = area; 184 185 return 1; 186 } 187 188 /* 189 * This function writes a new header to the mirror log header to the lv 190 * 191 * Returns: 1 on success, 0 on failure 192 */ 193 static int _write_log_header(struct cmd_context *cmd, struct logical_volume *lv) 194 { 195 struct device *dev; 196 char *name; 197 struct { /* The mirror log header */ 198 uint32_t magic; 199 uint32_t version; 200 uint64_t nr_regions; 201 } log_header; 202 203 log_header.magic = xlate32(MIRROR_MAGIC); 204 log_header.version = xlate32(MIRROR_DISK_VERSION); 205 log_header.nr_regions = xlate64((uint64_t)-1); 206 207 if (!(name = dm_pool_alloc(cmd->mem, PATH_MAX))) { 208 log_error("Name allocation failed - log header not written (%s)", 209 lv->name); 210 return 0; 211 } 212 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); 216 return 0; 217 } 218 219 log_verbose("Writing log header to device, %s", lv->name); 220 221 if (!(dev = dev_cache_get(name, NULL))) { 222 log_error("%s: not found: log header not written", name); 223 return 0; 224 } 225 226 if (!dev_open_quiet(dev)) 227 return 0; 228 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); 232 return 0; 233 } 234 235 dev_close_immediate(dev); 236 237 return 1; 238 } 239 240 /* 241 * Initialize mirror log contents 242 */ 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) 246 { 247 struct str_list *sl; 248 struct lvinfo info; 249 uint32_t orig_status = log_lv->status; 250 int was_active = 0; 251 252 if (!activation() && in_sync) { 253 log_error("Aborting. Unable to create in-sync mirror log " 254 "while activation is disabled."); 255 return 0; 256 } 257 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)) 261 return_0; 262 was_active = 1; 263 } 264 265 /* Temporary make it visible for set_lv() */ 266 lv_set_visible(log_lv); 267 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."); 272 goto activate_lv; 273 } 274 275 /* store mirror log on disk(s) */ 276 if (!vg_write(log_lv->vg) || !vg_commit(log_lv->vg)) 277 goto activate_lv; 278 279 backup(log_lv->vg); 280 281 if (!activate_lv(cmd, log_lv)) { 282 log_error("Aborting. Failed to activate mirror log."); 283 goto revert_new_lv; 284 } 285 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.", 290 sl->str); 291 292 if (activation() && !set_lv(cmd, log_lv, log_lv->size, 293 in_sync ? -1 : 0)) { 294 log_error("Aborting. Failed to wipe mirror log."); 295 goto deactivate_and_revert_new_lv; 296 } 297 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; 301 } 302 303 if (!deactivate_lv(cmd, log_lv)) { 304 log_error("Aborting. Failed to deactivate mirror log. " 305 "Manual intervention required."); 306 return 0; 307 } 308 309 lv_set_hidden(log_lv); 310 311 if (was_active && !activate_lv(cmd, log_lv)) 312 return_0; 313 314 return 1; 315 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."); 320 return 0; 321 } 322 323 revert_new_lv: 324 log_lv->status = orig_status; 325 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.", 329 sl->str); 330 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."); 334 return 0; 335 } 336 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."); 340 else 341 backup(log_lv->vg); 342 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 lv_set_visible(log_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 (id_equal(&pv->id, &pvl->pv->id)) { 447 pv_found = 1; 448 break; 449 } 450 if (pvl->pv->dev && pv->dev && 451 pv->dev->dev == pvl->pv->dev->dev) { 452 pv_found = 1; 453 break; 454 } 455 } 456 if (!pv_found) 457 return 0; 458 } 459 } 460 461 return 1; 462 } 463 464 /* 465 * Remove num_removed images from mirrored_seg 466 * 467 * Arguments: 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 470 * will be removed 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 479 * as a result 480 * 481 * If collapse is non-zero, <removed> is guaranteed to be equal to num_removed. 482 * 483 * Return values: 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). 488 */ 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, 493 uint32_t *removed) 494 { 495 uint32_t m; 496 uint32_t s; 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; 503 struct lv_list *lvl; 504 struct dm_list tmp_orphan_lvs; 505 506 if (removed) 507 *removed = 0; 508 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" : ""); 513 514 if (collapse && 515 (removable_pvs || (old_area_count - num_removed != 1))) { 516 log_error("Incompatible parameters to _remove_mirror_images"); 517 return 0; 518 } 519 520 /* Move removable_pvs to end of array */ 521 if (removable_pvs) { 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); 525 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)) 529 return_0; 530 new_area_count--; 531 } 532 } 533 if (num_removed && old_area_count == new_area_count) 534 return 1; 535 } else 536 new_area_count = old_area_count - num_removed; 537 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"); 545 return 0; 546 } 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); 550 } 551 mirrored_seg->area_count = new_area_count; 552 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; 560 lv_set_visible(lv1); 561 detached_log_lv = detach_mirror_log(mirrored_seg); 562 if (!remove_layer_from_lv(lv, lv1)) 563 return_0; 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"); 568 return 0; 569 } 570 } else if (new_area_count == 0) { 571 log_very_verbose("All mimages of %s are gone", lv->name); 572 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)) 579 return_0; 580 } else if (remove_log) 581 detached_log_lv = detach_mirror_log(mirrored_seg); 582 583 /* 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. 587 */ 588 589 if (!vg_write(mirrored_seg->lv->vg)) { 590 log_error("intermediate VG write failed."); 591 return 0; 592 } 593 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); 597 return 0; 598 } 599 600 if (!vg_commit(mirrored_seg->lv->vg)) { 601 resume_lv(mirrored_seg->lv->vg->cmd, mirrored_seg->lv); 602 return 0; 603 } 604 605 log_very_verbose("Updating \"%s\" in kernel", mirrored_seg->lv->name); 606 607 /* 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 611 * explicitly. 612 */ 613 if (lv1) { 614 if (!resume_lv(lv1->vg->cmd, lv1)) { 615 log_error("Problem resuming temporary LV, %s", lv1->name); 616 return 0; 617 } 618 619 /* 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. 628 */ 629 memlock_inc(); 630 } 631 632 if (!resume_lv(mirrored_seg->lv->vg->cmd, mirrored_seg->lv)) { 633 log_error("Problem reactivating %s", mirrored_seg->lv->name); 634 return 0; 635 } 636 637 /* Save or delete the 'orphan' LVs */ 638 if (!collapse) { 639 dm_list_iterate_items(lvl, &tmp_orphan_lvs) 640 if (!_delete_lv(lv, lvl->lv)) 641 return_0; 642 } 643 644 if (lv1 && !_delete_lv(lv, lv1)) 645 return_0; 646 647 if (detached_log_lv && !_delete_lv(lv, detached_log_lv)) 648 return_0; 649 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, 654 1, &lv->tags, 0)) { 655 /* As a result, unnecessary sync may run after 656 * collapsing. But safe.*/ 657 log_error("Failed to initialize log device"); 658 return_0; 659 } 660 } 661 662 if (removed) 663 *removed = old_area_count - new_area_count; 664 665 log_very_verbose("%" PRIu32 " image(s) removed from %s", 666 old_area_count - num_removed, lv->name); 667 668 return 1; 669 } 670 671 /* 672 * Remove the number of mirror images from the LV 673 */ 674 int remove_mirror_images(struct logical_volume *lv, uint32_t num_mirrors, 675 struct dm_list *removable_pvs, unsigned remove_log) 676 { 677 uint32_t num_removed, removed_once, r; 678 uint32_t existing_mirrors = lv_mirror_count(lv); 679 struct logical_volume *next_lv = lv; 680 681 num_removed = existing_mirrors - num_mirrors; 682 683 /* num_removed can be 0 if the function is called just to remove log */ 684 do { 685 if (num_removed < first_seg(next_lv)->area_count) 686 removed_once = num_removed; 687 else 688 removed_once = first_seg(next_lv)->area_count - 1; 689 690 if (!_remove_mirror_images(next_lv, removed_once, 691 removable_pvs, remove_log, 0, &r)) 692 return_0; 693 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); 699 } 700 701 num_removed -= r; 702 } while (next_lv && num_removed); 703 704 if (num_removed) { 705 if (num_removed == existing_mirrors - num_mirrors) 706 log_error("No mirror images found using specified PVs."); 707 else { 708 log_error("%u images are removed out of requested %u.", 709 existing_mirrors - lv_mirror_count(lv), 710 existing_mirrors - num_mirrors); 711 } 712 return 0; 713 } 714 715 return 1; 716 } 717 718 static int _mirrored_lv_in_sync(struct logical_volume *lv) 719 { 720 float sync_percent; 721 percent_range_t percent_range; 722 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); 727 return 0; 728 } 729 730 return (percent_range == PERCENT_100) ? 1 : 0; 731 } 732 733 /* 734 * Collapsing temporary mirror layers. 735 * 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. 740 * 741 * The function is destructive: to remove intermediate mirror layers, 742 * VG metadata commits and suspend/resume are necessary. 743 */ 744 int collapse_mirrored_lv(struct logical_volume *lv) 745 { 746 struct logical_volume *tmp_lv; 747 struct lv_segment *mirror_seg; 748 749 while ((tmp_lv = find_temporary_mirror(lv))) { 750 mirror_seg = find_mirror_seg(first_seg(tmp_lv)); 751 if (!mirror_seg) { 752 log_error("Failed to find mirrored LV for %s", 753 tmp_lv->name); 754 return 0; 755 } 756 757 if (!_mirrored_lv_in_sync(mirror_seg->lv)) { 758 log_verbose("Not collapsing %s: out-of-sync", 759 mirror_seg->lv->name); 760 return 1; 761 } 762 763 if (!_remove_mirror_images(mirror_seg->lv, 764 mirror_seg->area_count - 1, 765 NULL, 1, 1, NULL)) { 766 log_error("Failed to release mirror images"); 767 return 0; 768 } 769 } 770 771 return 1; 772 } 773 774 static int get_mirror_fault_policy(struct cmd_context *cmd __attribute((unused)), 775 int log_policy) 776 { 777 const char *policy; 778 779 if (log_policy) 780 policy = find_config_str(NULL, "activation/mirror_log_fault_policy", 781 DEFAULT_MIRROR_LOG_FAULT_POLICY); 782 else 783 policy = find_config_str(NULL, "activation/mirror_device_fault_policy", 784 DEFAULT_MIRROR_DEV_FAULT_POLICY); 785 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; 792 793 if (log_policy) 794 log_error("Bad activation/mirror_log_fault_policy"); 795 else 796 log_error("Bad activation/mirror_device_fault_policy"); 797 798 return MIRROR_REMOVE; 799 } 800 801 static int get_mirror_log_fault_policy(struct cmd_context *cmd) 802 { 803 return get_mirror_fault_policy(cmd, 1); 804 } 805 806 static int get_mirror_device_fault_policy(struct cmd_context *cmd) 807 { 808 return get_mirror_fault_policy(cmd, 0); 809 } 810 811 /* 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? 817 * 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) 820 * is being restored. 821 * 822 * Returns: 0 on failure, 1 on reconfig, -1 if no reconfig done 823 */ 824 static int replace_mirror_images(struct lv_segment *mirrored_seg, 825 uint32_t num_mirrors, 826 int log_policy, int in_sync) 827 { 828 int r = -1; 829 struct logical_volume *lv = mirrored_seg->lv; 830 831 /* FIXME: Use lvconvert rather than duplicating its code */ 832 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); 836 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); 840 else 841 log_error("WARNING: Use 'lvconvert -m %d %s/%s' to replace failed devices", 842 num_mirrors - 1, lv->vg->name, lv->name); 843 r = 0; 844 845 /* REMEMBER/FIXME: set in_sync to 0 if a new mirror device was added */ 846 in_sync = 0; 847 } 848 849 /* 850 * FIXME: right now, we ignore the allocation policy specified to 851 * allocate the new log. 852 */ 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); 857 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); 860 r = 0; 861 } 862 863 return r; 864 } 865 866 int reconfigure_mirror_images(struct lv_segment *mirrored_seg, uint32_t num_mirrors, 867 struct dm_list *removable_pvs, unsigned remove_log) 868 { 869 int r; 870 int in_sync; 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; 874 875 /* was the mirror in-sync before problems? */ 876 in_sync = _mirrored_lv_in_sync(mirrored_seg->lv); 877 878 /* 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 882 */ 883 init_mirror_in_sync(in_sync); 884 885 r = _remove_mirror_images(mirrored_seg->lv, old_num_mirrors - num_mirrors, 886 removable_pvs, remove_log, 0, NULL); 887 if (!r) 888 /* Unable to remove bad devices */ 889 return 0; 890 891 log_warn("WARNING: Bad device removed from mirror volume, %s/%s", 892 mirrored_seg->lv->vg->name, mirrored_seg->lv->name); 893 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); 896 897 r = replace_mirror_images(mirrored_seg, 898 (dev_policy != MIRROR_REMOVE) ? 899 old_num_mirrors : num_mirrors, 900 log_policy, in_sync); 901 902 if (!r) 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); 906 else if (r > 0) 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); 910 else 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); 918 } 919 /* 920 * If we made it here, we at least removed the bad device. 921 * Consider this success. 922 */ 923 return 1; 924 } 925 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) 930 { 931 uint32_t m; 932 char *img_name; 933 size_t len; 934 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."); 939 return 0; 940 } 941 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."); 945 return 0; 946 } 947 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."); 954 return 0; 955 } 956 957 if (!lv_add_segment(ah, m, 1, img_lvs[m], 958 get_segtype_from_string(lv->vg->cmd, 959 "striped"), 960 0, 0, 0, NULL)) { 961 log_error("Aborting. Failed to add mirror image segment " 962 "to %s. Remove new LV and retry.", 963 img_lvs[m]->name); 964 return 0; 965 } 966 } 967 968 return 1; 969 } 970 971 /* 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. 976 */ 977 int remove_mirrors_from_segments(struct logical_volume *lv, 978 uint32_t new_mirrors, uint32_t status_mask) 979 { 980 struct lv_segment *seg; 981 uint32_t s; 982 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, 987 lv->name, seg->le); 988 return 0; 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); 993 return 0; 994 } 995 } 996 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)) 1001 return_0; 1002 } 1003 1004 for (s = new_mirrors + 1; s < seg->area_count; s++) 1005 release_lv_segment_area(seg, s, seg->area_len); 1006 1007 seg->area_count = new_mirrors + 1; 1008 1009 if (!new_mirrors) 1010 seg->segtype = get_segtype_from_string(lv->vg->cmd, 1011 "striped"); 1012 } 1013 1014 return 1; 1015 } 1016 1017 const char *get_pvmove_pvname_from_lv_mirr(struct logical_volume *lv_mirr) 1018 { 1019 struct lv_segment *seg; 1020 1021 dm_list_iterate_items(seg, &lv_mirr->segments) { 1022 if (!seg_is_mirrored(seg)) 1023 continue; 1024 if (seg_type(seg, 0) != AREA_PV) 1025 continue; 1026 return dev_name(seg_dev(seg, 0)); 1027 } 1028 1029 return NULL; 1030 } 1031 1032 const char *get_pvmove_pvname_from_lv(struct logical_volume *lv) 1033 { 1034 struct lv_segment *seg; 1035 uint32_t s; 1036 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) 1040 continue; 1041 return get_pvmove_pvname_from_lv_mirr(seg_lv(seg, s)); 1042 } 1043 } 1044 1045 return NULL; 1046 } 1047 1048 struct logical_volume *find_pvmove_lv(struct volume_group *vg, 1049 struct device *dev, 1050 uint32_t lv_type) 1051 { 1052 struct lv_list *lvl; 1053 struct logical_volume *lv; 1054 struct lv_segment *seg; 1055 1056 /* Loop through all LVs */ 1057 dm_list_iterate_items(lvl, &vg->lvs) { 1058 lv = lvl->lv; 1059 1060 if (!(lv->status & lv_type)) 1061 continue; 1062 1063 /* Check segment origins point to pvname */ 1064 dm_list_iterate_items(seg, &lv->segments) { 1065 if (seg_type(seg, 0) != AREA_PV) 1066 continue; 1067 if (seg_dev(seg, 0) != dev) 1068 continue; 1069 return lv; 1070 } 1071 } 1072 1073 return NULL; 1074 } 1075 1076 struct logical_volume *find_pvmove_lv_from_pvname(struct cmd_context *cmd, 1077 struct volume_group *vg, 1078 const char *name, 1079 const char *uuid __attribute((unused)), 1080 uint32_t lv_type) 1081 { 1082 struct physical_volume *pv; 1083 1084 if (!(pv = find_pv_by_name(cmd, name))) 1085 return_NULL; 1086 1087 return find_pvmove_lv(vg, pv->dev, lv_type); 1088 } 1089 1090 struct dm_list *lvs_using_lv(struct cmd_context *cmd, struct volume_group *vg, 1091 struct logical_volume *lv) 1092 { 1093 struct dm_list *lvs; 1094 struct logical_volume *lv1; 1095 struct lv_list *lvl, *lvl1; 1096 struct lv_segment *seg; 1097 uint32_t s; 1098 1099 if (!(lvs = dm_pool_alloc(cmd->mem, sizeof(*lvs)))) { 1100 log_error("lvs list alloc failed"); 1101 return NULL; 1102 } 1103 1104 dm_list_init(lvs); 1105 1106 /* Loop through all LVs except the one supplied */ 1107 dm_list_iterate_items(lvl1, &vg->lvs) { 1108 lv1 = lvl1->lv; 1109 if (lv1 == lv) 1110 continue; 1111 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) 1117 continue; 1118 if (!(lvl = dm_pool_alloc(cmd->mem, sizeof(*lvl)))) { 1119 log_error("lv_list alloc failed"); 1120 return NULL; 1121 } 1122 lvl->lv = lv1; 1123 dm_list_add(lvs, &lvl->list); 1124 goto next_lv; 1125 } 1126 } 1127 next_lv: 1128 ; 1129 } 1130 1131 return lvs; 1132 } 1133 1134 float copy_percent(struct logical_volume *lv_mirr, 1135 percent_range_t *percent_range) 1136 { 1137 uint32_t numerator = 0u, denominator = 0u; 1138 struct lv_segment *seg; 1139 1140 dm_list_iterate_items(seg, &lv_mirr->segments) { 1141 denominator += seg->area_len; 1142 1143 if (seg_is_mirrored(seg) && seg->area_count > 1) 1144 numerator += seg->extents_copied; 1145 else 1146 numerator += seg->area_len; 1147 } 1148 1149 if (!denominator || (numerator == denominator)) 1150 *percent_range = PERCENT_100; 1151 else if (numerator == 0) 1152 *percent_range = PERCENT_0; 1153 else 1154 *percent_range = PERCENT_0_TO_100; 1155 1156 return denominator ? (float) numerator *100 / denominator : 100.0; 1157 } 1158 1159 /* 1160 * Fixup mirror pointers after single-pass segment import 1161 */ 1162 int fixup_imported_mirrors(struct volume_group *vg) 1163 { 1164 struct lv_list *lvl; 1165 struct lv_segment *seg; 1166 1167 dm_list_iterate_items(lvl, &vg->lvs) { 1168 dm_list_iterate_items(seg, &lvl->lv->segments) { 1169 if (seg->segtype != 1170 get_segtype_from_string(vg->cmd, "mirror")) 1171 continue; 1172 1173 if (seg->log_lv && !add_seg_to_segs_using_this_lv(seg->log_lv, seg)) 1174 return_0; 1175 } 1176 } 1177 1178 return 1; 1179 } 1180 1181 /* 1182 * Add mirrors to "linear" or "mirror" segments 1183 */ 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) 1187 { 1188 struct alloc_handle *ah; 1189 const struct segment_type *segtype; 1190 struct dm_list *parallel_areas; 1191 uint32_t adjusted_region_size; 1192 int r = 1; 1193 1194 if (!(parallel_areas = build_parallel_areas_from_lv(cmd, lv))) 1195 return_0; 1196 1197 if (!(segtype = get_segtype_from_string(cmd, "mirror"))) 1198 return_0; 1199 1200 adjusted_region_size = adjusted_mirror_region_size(lv->vg->extent_size, 1201 lv->le_count, 1202 region_size); 1203 1204 if (!(ah = allocate_extents(lv->vg, NULL, segtype, 1, mirrors, 0, 0, 1205 lv->le_count, allocatable_pvs, alloc, 1206 parallel_areas))) { 1207 log_error("Unable to allocate mirror extents for %s.", lv->name); 1208 return 0; 1209 } 1210 1211 if (!lv_add_mirror_areas(ah, lv, 0, adjusted_region_size)) { 1212 log_error("Failed to add mirror areas to %s", lv->name); 1213 r = 0; 1214 } 1215 1216 alloc_destroy(ah); 1217 return r; 1218 } 1219 1220 /* 1221 * Convert mirror log 1222 * 1223 * FIXME: Can't handle segment-by-segment mirror (like pvmove) 1224 */ 1225 int remove_mirror_log(struct cmd_context *cmd, 1226 struct logical_volume *lv, 1227 struct dm_list *removable_pvs) 1228 { 1229 float sync_percent; 1230 percent_range_t percent_range = PERCENT_0; 1231 struct lvinfo info; 1232 struct volume_group *vg = lv->vg; 1233 1234 /* Unimplemented features */ 1235 if (dm_list_size(&lv->segments) != 1) { 1236 log_error("Multiple-segment mirror is not supported"); 1237 return 0; 1238 } 1239 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."); 1245 return 0; 1246 } 1247 } else if (vg_is_clustered(vg)) { 1248 log_error("Unable to convert the log of an inactive " 1249 "cluster mirror, %s", lv->name); 1250 return 0; 1251 } else if (yes_no_prompt("Full resync required to convert " 1252 "inactive mirror %s to core log. " 1253 "Proceed? [y/n]: ") == 'y') 1254 sync_percent = 0; 1255 else 1256 return 0; 1257 1258 if (percent_range == PERCENT_100) 1259 init_mirror_in_sync(1); 1260 else { 1261 /* A full resync will take place */ 1262 lv->status &= ~MIRROR_NOTSYNCED; 1263 init_mirror_in_sync(0); 1264 } 1265 1266 if (!remove_mirror_images(lv, lv_mirror_count(lv), 1267 removable_pvs, 1U)) 1268 return_0; 1269 1270 return 1; 1271 } 1272 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, 1277 const char *suffix) 1278 { 1279 struct logical_volume *log_lv; 1280 char *log_name; 1281 size_t len; 1282 1283 len = strlen(lv_name) + 32; 1284 if (!(log_name = alloca(len))) { 1285 log_error("log_name allocation failed."); 1286 return NULL; 1287 } 1288 1289 if (dm_snprintf(log_name, len, "%s%s", lv_name, suffix) < 0) { 1290 log_error("log_name allocation failed."); 1291 return NULL; 1292 } 1293 1294 if (!(log_lv = lv_create_empty(log_name, NULL, 1295 VISIBLE_LV | LVM_READ | LVM_WRITE, 1296 alloc, lv->vg))) 1297 return_NULL; 1298 1299 if (!lv_add_log_segment(ah, log_lv)) 1300 return_NULL; 1301 1302 return log_lv; 1303 } 1304 1305 static struct logical_volume *_set_up_mirror_log(struct cmd_context *cmd, 1306 struct alloc_handle *ah, 1307 struct logical_volume *lv, 1308 uint32_t log_count, 1309 uint32_t region_size __attribute((unused)), 1310 alloc_policy_t alloc, 1311 int in_sync) 1312 { 1313 struct logical_volume *log_lv; 1314 const char *suffix, *c; 1315 char *lv_name; 1316 size_t len; 1317 struct lv_segment *seg; 1318 1319 init_mirror_in_sync(in_sync); 1320 1321 if (log_count != 1) { 1322 log_error("log_count != 1 is not supported."); 1323 return NULL; 1324 } 1325 1326 /* Mirror log name is lv_name + suffix, determined as the following: 1327 * 1. suffix is: 1328 * o "_mlog" for the original mirror LV. 1329 * o "_mlogtmp_%d" for temporary mirror LV, 1330 * 2. lv_name is: 1331 * o lv->name, if the log is temporary 1332 * o otherwise, the top-level LV name 1333 */ 1334 seg = first_seg(lv); 1335 if (seg_type(seg, 0) == AREA_LV && 1336 strstr(seg_lv(seg, 0)->name, MIRROR_SYNC_LAYER)) { 1337 lv_name = lv->name; 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"); 1344 return 0; 1345 } 1346 suffix = "_mlog"; 1347 } else { 1348 lv_name = lv->name; 1349 suffix = "_mlog"; 1350 } 1351 1352 if (!(log_lv = _create_mirror_log(lv, ah, alloc, 1353 (const char *) lv_name, suffix))) { 1354 log_error("Failed to create mirror log."); 1355 return NULL; 1356 } 1357 1358 if (!_init_mirror_log(cmd, log_lv, in_sync, &lv->tags, 1)) { 1359 log_error("Failed to create mirror log."); 1360 return NULL; 1361 } 1362 1363 return log_lv; 1364 } 1365 1366 int attach_mirror_log(struct lv_segment *seg, struct logical_volume *log_lv) 1367 { 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); 1372 } 1373 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) 1377 { 1378 struct alloc_handle *ah; 1379 const struct segment_type *segtype; 1380 struct dm_list *parallel_areas; 1381 float sync_percent; 1382 percent_range_t percent_range; 1383 int in_sync; 1384 struct logical_volume *log_lv; 1385 struct lvinfo info; 1386 int r = 0; 1387 1388 /* Unimplemented features */ 1389 if (log_count > 1) { 1390 log_error("log_count > 1 is not supported"); 1391 return 0; 1392 } 1393 1394 if (dm_list_size(&lv->segments) != 1) { 1395 log_error("Multiple-segment mirror is not supported"); 1396 return 0; 1397 } 1398 1399 /* 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) 1403 */ 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); 1408 return 0; 1409 } 1410 1411 if (!(parallel_areas = build_parallel_areas_from_lv(cmd, lv))) 1412 return_0; 1413 1414 if (!(segtype = get_segtype_from_string(cmd, "mirror"))) 1415 return_0; 1416 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); 1421 return 0; 1422 } 1423 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); 1428 if (!ah) { 1429 log_error("Unable to allocate extents for mirror log."); 1430 return 0; 1431 } 1432 1433 /* check sync status */ 1434 if (lv_mirror_percent(cmd, lv, 0, &sync_percent, &percent_range, 1435 NULL) && 1436 (percent_range == PERCENT_100)) 1437 in_sync = 1; 1438 else 1439 in_sync = 0; 1440 1441 if (!(log_lv = _set_up_mirror_log(cmd, ah, lv, log_count, 1442 region_size, alloc, in_sync))) 1443 goto_out; 1444 1445 if (!attach_mirror_log(first_seg(lv), log_lv)) 1446 goto_out; 1447 1448 r = 1; 1449 out: 1450 alloc_destroy(ah); 1451 return r; 1452 } 1453 1454 /* 1455 * Convert "linear" LV to "mirror". 1456 */ 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, 1460 uint32_t log_count) 1461 { 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; 1467 1468 if (stripes > 1) { 1469 log_error("stripes > 1 is not supported"); 1470 return 0; 1471 } 1472 1473 /* 1474 * allocate destination extents 1475 */ 1476 1477 if (!(parallel_areas = build_parallel_areas_from_lv(cmd, lv))) 1478 return_0; 1479 1480 if (!(segtype = get_segtype_from_string(cmd, "mirror"))) 1481 return_0; 1482 1483 ah = allocate_extents(lv->vg, NULL, segtype, 1484 stripes, mirrors, log_count, region_size, lv->le_count, 1485 allocatable_pvs, alloc, parallel_areas); 1486 if (!ah) { 1487 log_error("Unable to allocate extents for mirror(s)."); 1488 return 0; 1489 } 1490 1491 /* 1492 * create and initialize mirror log 1493 */ 1494 if (log_count && 1495 !(log_lv = _set_up_mirror_log(cmd, ah, lv, log_count, region_size, 1496 alloc, mirror_in_sync()))) { 1497 stack; 1498 goto out_remove_images; 1499 } 1500 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. */ 1504 1505 /* 1506 * insert a mirror layer 1507 */ 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; 1512 1513 /* 1514 * create mirror image LVs 1515 */ 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; 1520 } 1521 1522 if (!_create_mimage_lvs(ah, mirrors, lv, img_lvs)) 1523 goto out_remove_log; 1524 1525 if (!lv_add_mirror_lvs(lv, img_lvs, mirrors, 1526 MIRROR_IMAGE | (lv->status & LOCKED), 1527 region_size)) { 1528 log_error("Aborting. Failed to add mirror segment. " 1529 "Remove new LV and retry."); 1530 goto out_remove_images; 1531 } 1532 1533 if (log_count && !attach_mirror_log(first_seg(lv), log_lv)) 1534 stack; 1535 1536 alloc_destroy(ah); 1537 return 1; 1538 1539 out_remove_log: 1540 if (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."); 1546 else 1547 backup(log_lv->vg); 1548 } 1549 out_remove_images: 1550 alloc_destroy(ah); 1551 return 0; 1552 } 1553 1554 /* 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. 1558 */ 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) 1563 { 1564 if (!mirrors && !log_count) { 1565 log_error("No conversion is requested"); 1566 return 0; 1567 } 1568 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); 1578 1579 if (flags & MIRROR_BY_SEG) { 1580 if (log_count) { 1581 log_error("Persistent log is not supported on " 1582 "segment-by-segment mirroring"); 1583 return 0; 1584 } 1585 if (stripes > 1) { 1586 log_error("Striped-mirroring is not supported on " 1587 "segment-by-segment mirroring"); 1588 return 0; 1589 } 1590 1591 return add_mirrors_to_segments(cmd, lv, mirrors, 1592 region_size, pvs, alloc); 1593 } else if (flags & MIRROR_BY_LV) { 1594 if (!mirrors) 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); 1600 } 1601 1602 log_error("Unsupported mirror conversion type"); 1603 return 0; 1604 } 1605 1606 /* 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. 1610 */ 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) 1615 { 1616 uint32_t new_mirrors; 1617 struct lv_segment *seg; 1618 1619 if (!mirrors && !log_count) { 1620 log_error("No conversion is requested"); 1621 return 0; 1622 } 1623 1624 seg = first_seg(lv); 1625 if (!seg_is_mirrored(seg)) { 1626 log_error("Not a mirror segment"); 1627 return 0; 1628 } 1629 1630 if (lv_mirror_count(lv) <= mirrors) { 1631 log_error("Removing more than existing: %d <= %d", 1632 seg->area_count, mirrors); 1633 return 0; 1634 } 1635 new_mirrors = lv_mirror_count(lv) - mirrors - 1; 1636 1637 /* MIRROR_BY_LV */ 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); 1642 1643 /* MIRROR_BY_SEG */ 1644 if (log_count) { 1645 log_error("Persistent log is not supported on " 1646 "segment-by-segment mirroring"); 1647 return 0; 1648 } 1649 return remove_mirrors_from_segments(lv, new_mirrors, status_mask); 1650 } 1651 1652