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