]> sourceware.org Git - lvm2.git/blob - tools/lvconvert.c
Reinstate accidentally-deleted line.
[lvm2.git] / tools / lvconvert.c
1 /*
2 * Copyright (C) 2005-2007 Red Hat, Inc. All rights reserved.
3 *
4 * This file is part of LVM2.
5 *
6 * This copyrighted material is made available to anyone wishing to use,
7 * modify, copy, or redistribute it subject to the terms and conditions
8 * of the GNU Lesser General Public License v.2.1.
9 *
10 * You should have received a copy of the GNU Lesser General Public License
11 * along with this program; if not, write to the Free Software Foundation,
12 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
13 */
14
15 #include "tools.h"
16 #include "polldaemon.h"
17 #include "lv_alloc.h"
18 #include "metadata.h"
19
20 struct lvconvert_params {
21 int snapshot;
22 int merge;
23 int merge_mirror;
24 int zero;
25
26 const char *origin;
27 const char *lv_name;
28 const char *lv_split_name;
29 const char *lv_name_full;
30 const char *vg_name;
31 int wait_completion;
32 int need_polling;
33
34 uint32_t chunk_size;
35 uint32_t region_size;
36
37 uint32_t mirrors;
38 sign_t mirrors_sign;
39 uint32_t keep_mimages;
40 uint32_t stripes;
41 uint32_t stripe_size;
42
43 const struct segment_type *segtype;
44
45 alloc_policy_t alloc;
46
47 int pv_count;
48 char **pvs;
49 struct dm_list *pvh;
50
51 int replace_pv_count;
52 char **replace_pvs;
53 struct dm_list *replace_pvh;
54
55 struct logical_volume *lv_to_poll;
56 };
57
58 static int _lvconvert_name_params(struct lvconvert_params *lp,
59 struct cmd_context *cmd,
60 int *pargc, char ***pargv)
61 {
62 char *ptr;
63 const char *vg_name = NULL;
64
65 if (lp->merge)
66 return 1;
67
68 if (lp->snapshot) {
69 if (!*pargc) {
70 log_error("Please specify a logical volume to act as "
71 "the snapshot origin.");
72 return 0;
73 }
74
75 lp->origin = *pargv[0];
76 (*pargv)++, (*pargc)--;
77 if (!(lp->vg_name = extract_vgname(cmd, lp->origin))) {
78 log_error("The origin name should include the "
79 "volume group.");
80 return 0;
81 }
82
83 /* Strip the volume group from the origin */
84 if ((ptr = strrchr(lp->origin, (int) '/')))
85 lp->origin = ptr + 1;
86 }
87
88 if (!*pargc) {
89 log_error("Please provide logical volume path");
90 return 0;
91 }
92
93 lp->lv_name = lp->lv_name_full = (*pargv)[0];
94 (*pargv)++, (*pargc)--;
95
96 if (strchr(lp->lv_name_full, '/') &&
97 (vg_name = extract_vgname(cmd, lp->lv_name_full)) &&
98 lp->vg_name && strcmp(vg_name, lp->vg_name)) {
99 log_error("Please use a single volume group name "
100 "(\"%s\" or \"%s\")", vg_name, lp->vg_name);
101 return 0;
102 }
103
104 if (!lp->vg_name)
105 lp->vg_name = vg_name;
106
107 if (!validate_name(lp->vg_name)) {
108 log_error("Please provide a valid volume group name");
109 return 0;
110 }
111
112 if ((ptr = strrchr(lp->lv_name_full, '/')))
113 lp->lv_name = ptr + 1;
114
115 if (!lp->merge_mirror && !apply_lvname_restrictions(lp->lv_name))
116 return_0;
117
118 if (*pargc && lp->snapshot) {
119 log_error("Too many arguments provided for snapshots");
120 return 0;
121 }
122
123 return 1;
124 }
125
126 static int _read_params(struct lvconvert_params *lp, struct cmd_context *cmd,
127 int argc, char **argv)
128 {
129 int i;
130 const char *tmp_str;
131 struct arg_value_group_list *group;
132 int region_size;
133 int pagesize = lvm_getpagesize();
134
135 memset(lp, 0, sizeof(*lp));
136
137 if ((arg_count(cmd, snapshot_ARG) || arg_count(cmd, merge_ARG)) &&
138 (arg_count(cmd, mirrorlog_ARG) || arg_count(cmd, mirrors_ARG) ||
139 arg_count(cmd, repair_ARG))) {
140 log_error("--snapshot or --merge argument cannot be mixed "
141 "with --mirrors, --repair or --mirrorlog");
142 return 0;
143 }
144
145 if (!arg_count(cmd, background_ARG))
146 lp->wait_completion = 1;
147
148 if (arg_count(cmd, snapshot_ARG))
149 lp->snapshot = 1;
150
151 if (arg_count(cmd, snapshot_ARG) && arg_count(cmd, merge_ARG)) {
152 log_error("--snapshot and --merge are mutually exclusive");
153 return 0;
154 }
155
156 if (arg_count(cmd, splitmirrors_ARG) && arg_count(cmd, mirrors_ARG)) {
157 log_error("--mirrors and --splitmirrors are "
158 "mutually exclusive");
159 return 0;
160 }
161
162 /*
163 * The '--splitmirrors n' argument is equivalent to '--mirrors -n'
164 * (note the minus sign), except that it signifies the additional
165 * intent to keep the mimage that is detached, rather than
166 * discarding it.
167 */
168 if (arg_count(cmd, splitmirrors_ARG)) {
169 if (!arg_count(cmd, name_ARG) &&
170 !arg_count(cmd, trackchanges_ARG)) {
171 log_error("Please name the new logical volume using '--name'");
172 return 0;
173 }
174
175 lp->lv_split_name = arg_value(cmd, name_ARG);
176 if (lp->lv_split_name &&
177 !apply_lvname_restrictions(lp->lv_split_name))
178 return_0;
179
180 lp->keep_mimages = 1;
181 lp->mirrors = arg_uint_value(cmd, splitmirrors_ARG, 0);
182 lp->mirrors_sign = SIGN_MINUS;
183 } else if (arg_count(cmd, name_ARG)) {
184 log_error("The 'name' argument is only valid"
185 " with --splitmirrors");
186 return 0;
187 }
188
189 if (arg_count(cmd, merge_ARG)) {
190 if ((argc == 1) && strstr(argv[0], "_rimage_"))
191 lp->merge_mirror = 1;
192 else
193 lp->merge = 1;
194 }
195
196 if (arg_count(cmd, mirrors_ARG)) {
197 /*
198 * --splitmirrors has been chosen as the mechanism for
199 * specifying the intent of detaching and keeping a mimage
200 * versus an additional qualifying argument being added here.
201 */
202 lp->mirrors = arg_uint_value(cmd, mirrors_ARG, 0);
203 lp->mirrors_sign = arg_sign_value(cmd, mirrors_ARG, SIGN_NONE);
204 }
205
206 lp->alloc = (alloc_policy_t) arg_uint_value(cmd, alloc_ARG, ALLOC_INHERIT);
207
208 /* There are three types of lvconvert. */
209 if (lp->merge) { /* Snapshot merge */
210 if (arg_count(cmd, regionsize_ARG) || arg_count(cmd, chunksize_ARG) ||
211 arg_count(cmd, zero_ARG) || arg_count(cmd, regionsize_ARG) ||
212 arg_count(cmd, stripes_long_ARG) || arg_count(cmd, stripesize_ARG)) {
213 log_error("Only --background and --interval are valid "
214 "arguments for snapshot merge");
215 return 0;
216 }
217
218 if (!(lp->segtype = get_segtype_from_string(cmd, "snapshot")))
219 return_0;
220
221 } else if (lp->snapshot) { /* Snapshot creation from pre-existing cow */
222 if (arg_count(cmd, regionsize_ARG)) {
223 log_error("--regionsize is only available with mirrors");
224 return 0;
225 }
226
227 if (arg_count(cmd, stripesize_ARG) || arg_count(cmd, stripes_long_ARG)) {
228 log_error("--stripes and --stripesize are only available with striped mirrors");
229 return 0;
230 }
231
232 if (arg_sign_value(cmd, chunksize_ARG, SIGN_NONE) == SIGN_MINUS) {
233 log_error("Negative chunk size is invalid");
234 return 0;
235 }
236 lp->chunk_size = arg_uint_value(cmd, chunksize_ARG, 8);
237 if (lp->chunk_size < 8 || lp->chunk_size > 1024 ||
238 (lp->chunk_size & (lp->chunk_size - 1))) {
239 log_error("Chunk size must be a power of 2 in the "
240 "range 4K to 512K");
241 return 0;
242 }
243 log_verbose("Setting chunksize to %d sectors.", lp->chunk_size);
244
245 if (!(lp->segtype = get_segtype_from_string(cmd, "snapshot")))
246 return_0;
247
248 lp->zero = strcmp(arg_str_value(cmd, zero_ARG,
249 (lp->segtype->flags &
250 SEG_CANNOT_BE_ZEROED) ?
251 "n" : "y"), "n");
252
253 } else if (arg_count(cmd, replace_ARG)) { /* RAID device replacement */
254 lp->replace_pv_count = arg_count(cmd, replace_ARG);
255 lp->replace_pvs = dm_pool_alloc(cmd->mem, sizeof(char *) * lp->replace_pv_count);
256 if (!lp->replace_pvs)
257 return_0;
258
259 i = 0;
260 dm_list_iterate_items(group, &cmd->arg_value_groups) {
261 if (!grouped_arg_is_set(group->arg_values, replace_ARG))
262 continue;
263 if (!(tmp_str = grouped_arg_str_value(group->arg_values,
264 replace_ARG,
265 NULL))) {
266 log_error("Failed to get '--replace' argument");
267 return 0;
268 }
269 if (!(lp->replace_pvs[i++] = dm_pool_strdup(cmd->mem,
270 tmp_str)))
271 return_0;
272 }
273 } else { /* Mirrors (and some RAID functions) */
274 if (arg_count(cmd, chunksize_ARG)) {
275 log_error("--chunksize is only available with "
276 "snapshots");
277 return 0;
278 }
279
280 if (arg_count(cmd, zero_ARG)) {
281 log_error("--zero is only available with snapshots");
282 return 0;
283 }
284
285 /*
286 * --regionsize is only valid if converting an LV into a mirror.
287 * Checked when we know the state of the LV being converted.
288 */
289
290 if (arg_count(cmd, regionsize_ARG)) {
291 if (arg_sign_value(cmd, regionsize_ARG, SIGN_NONE) ==
292 SIGN_MINUS) {
293 log_error("Negative regionsize is invalid");
294 return 0;
295 }
296 lp->region_size = arg_uint_value(cmd, regionsize_ARG, 0);
297 } else {
298 region_size = 2 * find_config_tree_int(cmd,
299 "activation/mirror_region_size",
300 DEFAULT_MIRROR_REGION_SIZE);
301 if (region_size < 0) {
302 log_error("Negative regionsize in "
303 "configuration file is invalid");
304 return 0;
305 }
306 lp->region_size = region_size;
307 }
308
309 if (lp->region_size % (pagesize >> SECTOR_SHIFT)) {
310 log_error("Region size (%" PRIu32 ") must be "
311 "a multiple of machine memory "
312 "page size (%d)",
313 lp->region_size, pagesize >> SECTOR_SHIFT);
314 return 0;
315 }
316
317 if (lp->region_size & (lp->region_size - 1)) {
318 log_error("Region size (%" PRIu32
319 ") must be a power of 2", lp->region_size);
320 return 0;
321 }
322
323 if (!lp->region_size) {
324 log_error("Non-zero region size must be supplied.");
325 return 0;
326 }
327
328 /* Default is never striped, regardless of existing LV configuration. */
329 if (!get_stripe_params(cmd, &lp->stripes, &lp->stripe_size)) {
330 stack;
331 return 0;
332 }
333
334 lp->segtype = get_segtype_from_string(cmd, arg_str_value(cmd, type_ARG, "mirror"));
335 if (!lp->segtype)
336 return_0;
337 }
338
339 if (activation() && lp->segtype && lp->segtype->ops->target_present &&
340 !lp->segtype->ops->target_present(cmd, NULL, NULL)) {
341 log_error("%s: Required device-mapper target(s) not "
342 "detected in your kernel", lp->segtype->name);
343 return 0;
344 }
345
346 if (!_lvconvert_name_params(lp, cmd, &argc, &argv))
347 return_0;
348
349 lp->pv_count = argc;
350 lp->pvs = argv;
351
352 return 1;
353 }
354
355 static struct volume_group *_get_lvconvert_vg(struct cmd_context *cmd,
356 const char *name,
357 const char *uuid __attribute__((unused)))
358 {
359 dev_close_all();
360
361 if (name && !strchr(name, '/'))
362 return vg_read_for_update(cmd, name, NULL, 0);
363
364 /* 'name' is the full LV name; must extract_vgname() */
365 return vg_read_for_update(cmd, extract_vgname(cmd, name),
366 NULL, 0);
367 }
368
369 static struct logical_volume *_get_lvconvert_lv(struct cmd_context *cmd __attribute__((unused)),
370 struct volume_group *vg,
371 const char *name,
372 const char *uuid,
373 uint64_t lv_type __attribute__((unused)))
374 {
375 struct logical_volume *lv = find_lv(vg, name);
376
377 if (!lv || (uuid && strcmp(uuid, (char *)&lv->lvid)))
378 return NULL;
379
380 return lv;
381 }
382
383 static int _finish_lvconvert_mirror(struct cmd_context *cmd,
384 struct volume_group *vg,
385 struct logical_volume *lv,
386 struct dm_list *lvs_changed __attribute__((unused)))
387 {
388 int r = 0;
389
390 if (!(lv->status & CONVERTING))
391 return 1;
392
393 if (!collapse_mirrored_lv(lv)) {
394 log_error("Failed to remove temporary sync layer.");
395 return 0;
396 }
397
398 lv->status &= ~CONVERTING;
399
400 log_very_verbose("Updating logical volume \"%s\" on disk(s)", lv->name);
401
402 if (!vg_write(vg))
403 return_0;
404
405 if (!suspend_lv(cmd, lv)) {
406 log_error("Failed to lock %s", lv->name);
407 vg_revert(vg);
408 goto out;
409 }
410
411 if (!vg_commit(vg)) {
412 resume_lv(cmd, lv);
413 goto_out;
414 }
415
416 log_very_verbose("Updating \"%s\" in kernel", lv->name);
417
418 if (!resume_lv(cmd, lv)) {
419 log_error("Problem reactivating %s", lv->name);
420 goto out;
421 }
422
423 r = 1;
424 log_print("Logical volume %s converted.", lv->name);
425 out:
426 backup(vg);
427 return r;
428 }
429
430 static int _finish_lvconvert_merge(struct cmd_context *cmd,
431 struct volume_group *vg,
432 struct logical_volume *lv,
433 struct dm_list *lvs_changed __attribute__((unused)))
434 {
435 struct lv_segment *snap_seg = find_merging_cow(lv);
436 if (!snap_seg) {
437 log_error("Logical volume %s has no merging snapshot.", lv->name);
438 return 0;
439 }
440
441 log_print("Merge of snapshot into logical volume %s has finished.", lv->name);
442 if (!lv_remove_single(cmd, snap_seg->cow, DONT_PROMPT)) {
443 log_error("Could not remove snapshot %s merged into %s.",
444 snap_seg->cow->name, lv->name);
445 return 0;
446 }
447
448 return 1;
449 }
450
451 static progress_t _poll_merge_progress(struct cmd_context *cmd,
452 struct logical_volume *lv,
453 const char *name __attribute__((unused)),
454 struct daemon_parms *parms)
455 {
456 percent_t percent = PERCENT_0;
457
458 if (!lv_snapshot_percent(lv, &percent)) {
459 log_error("%s: Failed query for merging percentage. Aborting merge.", lv->name);
460 return PROGRESS_CHECK_FAILED;
461 } else if (percent == PERCENT_INVALID) {
462 log_error("%s: Merging snapshot invalidated. Aborting merge.", lv->name);
463 return PROGRESS_CHECK_FAILED;
464 } else if (percent == PERCENT_MERGE_FAILED) {
465 log_error("%s: Merge failed. Retry merge or inspect manually.", lv->name);
466 return PROGRESS_CHECK_FAILED;
467 }
468
469 if (parms->progress_display)
470 log_print("%s: %s: %.1f%%", lv->name, parms->progress_title,
471 percent_to_float(percent));
472 else
473 log_verbose("%s: %s: %.1f%%", lv->name, parms->progress_title,
474 percent_to_float(percent));
475
476 if (percent == PERCENT_0)
477 return PROGRESS_FINISHED_ALL;
478
479 return PROGRESS_UNFINISHED;
480 }
481
482 static struct poll_functions _lvconvert_mirror_fns = {
483 .get_copy_vg = _get_lvconvert_vg,
484 .get_copy_lv = _get_lvconvert_lv,
485 .poll_progress = poll_mirror_progress,
486 .finish_copy = _finish_lvconvert_mirror,
487 };
488
489 static struct poll_functions _lvconvert_merge_fns = {
490 .get_copy_vg = _get_lvconvert_vg,
491 .get_copy_lv = _get_lvconvert_lv,
492 .poll_progress = _poll_merge_progress,
493 .finish_copy = _finish_lvconvert_merge,
494 };
495
496 int lvconvert_poll(struct cmd_context *cmd, struct logical_volume *lv,
497 unsigned background)
498 {
499 /*
500 * FIXME allocate an "object key" structure with split
501 * out members (vg_name, lv_name, uuid, etc) and pass that
502 * around the lvconvert and polldaemon code
503 * - will avoid needless work, e.g. extract_vgname()
504 * - unfortunately there are enough overloaded "name" dragons in
505 * the polldaemon, lvconvert, pvmove code that a comprehensive
506 * audit/rework is needed
507 */
508 int len = strlen(lv->vg->name) + strlen(lv->name) + 2;
509 char *uuid = alloca(sizeof(lv->lvid));
510 char *lv_full_name = alloca(len);
511
512 if (!uuid || !lv_full_name)
513 return_0;
514
515 if (dm_snprintf(lv_full_name, len, "%s/%s", lv->vg->name, lv->name) < 0)
516 return_0;
517
518 memcpy(uuid, &lv->lvid, sizeof(lv->lvid));
519
520 if (!lv_is_merging_origin(lv))
521 return poll_daemon(cmd, lv_full_name, uuid, background, 0,
522 &_lvconvert_mirror_fns, "Converted");
523 else
524 return poll_daemon(cmd, lv_full_name, uuid, background, 0,
525 &_lvconvert_merge_fns, "Merged");
526 }
527
528 static int _insert_lvconvert_layer(struct cmd_context *cmd,
529 struct logical_volume *lv)
530 {
531 char *format, *layer_name;
532 size_t len;
533 int i;
534
535 /*
536 * We would like to give the same number for this layer
537 * and the newly added mimage.
538 * However, LV name of newly added mimage is determined *after*
539 * the LV name of this layer is determined.
540 *
541 * So, use generate_lv_name() to generate mimage name first
542 * and take the number from it.
543 */
544
545 len = strlen(lv->name) + 32;
546 if (!(format = alloca(len)) ||
547 !(layer_name = alloca(len)) ||
548 dm_snprintf(format, len, "%s_mimage_%%d", lv->name) < 0) {
549 log_error("lvconvert: layer name allocation failed.");
550 return 0;
551 }
552
553 if (!generate_lv_name(lv->vg, format, layer_name, len) ||
554 sscanf(layer_name, format, &i) != 1) {
555 log_error("lvconvert: layer name generation failed.");
556 return 0;
557 }
558
559 if (dm_snprintf(layer_name, len, MIRROR_SYNC_LAYER "_%d", i) < 0) {
560 log_error("layer name allocation failed.");
561 return 0;
562 }
563
564 if (!insert_layer_for_lv(cmd, lv, 0, layer_name)) {
565 log_error("Failed to insert resync layer");
566 return 0;
567 }
568
569 return 1;
570 }
571
572 static int _failed_mirrors_count(struct logical_volume *lv)
573 {
574 struct lv_segment *lvseg;
575 int ret = 0;
576 unsigned s;
577
578 dm_list_iterate_items(lvseg, &lv->segments) {
579 if (!seg_is_mirrored(lvseg))
580 return -1;
581 for (s = 0; s < lvseg->area_count; s++) {
582 if (seg_type(lvseg, s) == AREA_LV) {
583 if (is_temporary_mirror_layer(seg_lv(lvseg, s)))
584 ret += _failed_mirrors_count(seg_lv(lvseg, s));
585 else if (seg_lv(lvseg, s)->status & PARTIAL_LV)
586 ++ ret;
587 else if (seg_type(lvseg, s) == AREA_PV &&
588 is_missing_pv(seg_pv(lvseg, s)))
589 ++ret;
590 }
591 }
592 }
593
594 return ret;
595 }
596
597 static int _failed_logs_count(struct logical_volume *lv)
598 {
599 int ret = 0;
600 unsigned s;
601 struct logical_volume *log_lv = first_seg(lv)->log_lv;
602 if (log_lv && (log_lv->status & PARTIAL_LV)) {
603 if (log_lv->status & MIRRORED)
604 ret += _failed_mirrors_count(log_lv);
605 else
606 ret += 1;
607 }
608 for (s = 0; s < first_seg(lv)->area_count; s++) {
609 if (seg_type(first_seg(lv), s) == AREA_LV &&
610 is_temporary_mirror_layer(seg_lv(first_seg(lv), s)))
611 ret += _failed_logs_count(seg_lv(first_seg(lv), s));
612 }
613 return ret;
614 }
615
616
617 static struct dm_list *_failed_pv_list(struct volume_group *vg)
618 {
619 struct dm_list *failed_pvs;
620 struct pv_list *pvl, *new_pvl;
621
622 if (!(failed_pvs = dm_pool_alloc(vg->vgmem, sizeof(*failed_pvs)))) {
623 log_error("Allocation of list of failed_pvs failed.");
624 return_NULL;
625 }
626
627 dm_list_init(failed_pvs);
628
629 dm_list_iterate_items(pvl, &vg->pvs) {
630 if (!is_missing_pv(pvl->pv))
631 continue;
632
633 /*
634 * Finally, --repair will remove empty PVs.
635 * But we only want remove these which are output of repair,
636 * Do not count these which are already empty here.
637 * FIXME: code should traverse PV in LV not in whole VG.
638 * FIXME: layer violation? should it depend on vgreduce --removemising?
639 */
640 if (pvl->pv->pe_alloc_count == 0)
641 continue;
642
643 if (!(new_pvl = dm_pool_alloc(vg->vgmem, sizeof(*new_pvl)))) {
644 log_error("Allocation of failed_pvs list entry failed.");
645 return_NULL;
646 }
647 new_pvl->pv = pvl->pv;
648 dm_list_add(failed_pvs, &new_pvl->list);
649 }
650
651 return failed_pvs;
652 }
653
654 static int _is_partial_lv(struct logical_volume *lv,
655 void *baton __attribute__((unused)))
656 {
657 return lv->status & PARTIAL_LV;
658 }
659
660 /*
661 * Walk down the stacked mirror LV to the original mirror LV.
662 */
663 static struct logical_volume *_original_lv(struct logical_volume *lv)
664 {
665 struct logical_volume *next_lv = lv, *tmp_lv;
666
667 while ((tmp_lv = find_temporary_mirror(next_lv)))
668 next_lv = tmp_lv;
669
670 return next_lv;
671 }
672
673 static void _lvconvert_mirrors_repair_ask(struct cmd_context *cmd,
674 int failed_log, int failed_mirrors,
675 int *replace_log, int *replace_mirrors)
676 {
677 const char *leg_policy = NULL, *log_policy = NULL;
678
679 int force = arg_count(cmd, force_ARG);
680 int yes = arg_count(cmd, yes_ARG);
681
682 *replace_log = *replace_mirrors = 1;
683
684 if (arg_count(cmd, use_policies_ARG)) {
685 leg_policy = find_config_tree_str(cmd,
686 "activation/mirror_image_fault_policy", NULL);
687 if (!leg_policy)
688 leg_policy = find_config_tree_str(cmd,
689 "activation/mirror_device_fault_policy",
690 DEFAULT_MIRROR_DEVICE_FAULT_POLICY);
691 log_policy = find_config_tree_str(cmd,
692 "activation/mirror_log_fault_policy",
693 DEFAULT_MIRROR_LOG_FAULT_POLICY);
694 *replace_mirrors = strcmp(leg_policy, "remove");
695 *replace_log = strcmp(log_policy, "remove");
696 return;
697 }
698
699 if (yes)
700 return;
701
702 if (force != PROMPT) {
703 *replace_log = *replace_mirrors = 0;
704 return;
705 }
706
707 if (failed_log &&
708 yes_no_prompt("Attempt to replace failed mirror log? [y/n]: ") == 'n') {
709 *replace_log = 0;
710 }
711
712 if (failed_mirrors &&
713 yes_no_prompt("Attempt to replace failed mirror images "
714 "(requires full device resync)? [y/n]: ") == 'n') {
715 *replace_mirrors = 0;
716 }
717 }
718
719 /*
720 * _get_log_count
721 * @lv: the mirror LV
722 *
723 * Get the number of on-disk copies of the log.
724 * 0 = 'core'
725 * 1 = 'disk'
726 * 2+ = 'mirrored'
727 */
728 static int _get_log_count(struct logical_volume *lv)
729 {
730 struct logical_volume *log_lv;
731
732 log_lv = first_seg(_original_lv(lv))->log_lv;
733 if (log_lv)
734 return lv_mirror_count(log_lv);
735
736 return 0;
737 }
738
739 static int _lv_update_mirrored_log(struct logical_volume *lv,
740 struct dm_list *operable_pvs,
741 int log_count)
742 {
743 int old_log_count;
744 struct logical_volume *log_lv;
745
746 /*
747 * When log_count is 0, mirrored log doesn't need to be
748 * updated here but it will be removed later.
749 */
750 if (!log_count)
751 return 1;
752
753 log_lv = first_seg(_original_lv(lv))->log_lv;
754 if (!log_lv || !(log_lv->status & MIRRORED))
755 return 1;
756
757 old_log_count = _get_log_count(lv);
758 if (old_log_count == log_count)
759 return 1;
760
761 /* Reducing redundancy of the log */
762 return remove_mirror_images(log_lv, log_count,
763 is_mirror_image_removable,
764 operable_pvs, 0U);
765 }
766
767 static int _reload_lv(struct cmd_context *cmd, struct logical_volume *lv);
768 static int _lv_update_log_type(struct cmd_context *cmd,
769 struct lvconvert_params *lp,
770 struct logical_volume *lv,
771 struct dm_list *operable_pvs,
772 int log_count)
773 {
774 int old_log_count;
775 uint32_t region_size = (lp) ? lp->region_size :
776 first_seg(lv)->region_size;
777 alloc_policy_t alloc = (lp) ? lp->alloc : lv->alloc;
778 struct logical_volume *original_lv;
779 struct logical_volume *log_lv;
780
781 old_log_count = _get_log_count(lv);
782 if (old_log_count == log_count)
783 return 1;
784
785 original_lv = _original_lv(lv);
786 /* Remove an existing log completely */
787 if (!log_count) {
788 if (!remove_mirror_log(cmd, original_lv, operable_pvs,
789 arg_count(cmd, yes_ARG) ||
790 arg_count(cmd, force_ARG)))
791 return_0;
792 return 1;
793 }
794
795 log_lv = first_seg(original_lv)->log_lv;
796
797 /* Adding redundancy to the log */
798 if (old_log_count < log_count) {
799 region_size = adjusted_mirror_region_size(lv->vg->extent_size,
800 lv->le_count,
801 region_size);
802
803 if (!add_mirror_log(cmd, original_lv, log_count,
804 region_size, operable_pvs, alloc))
805 return_0;
806 /*
807 * FIXME: This simple approach won't work in cluster mirrors,
808 * but it doesn't matter because we don't support
809 * mirrored logs in cluster mirrors.
810 */
811 if (old_log_count)
812 return _reload_lv(cmd, log_lv);
813 return 1;
814 }
815
816 /* Reducing redundancy of the log */
817 return remove_mirror_images(log_lv, log_count,
818 is_mirror_image_removable, operable_pvs, 1U);
819 }
820
821 /*
822 * Reomove missing and empty PVs from VG, if are also in provided list
823 */
824 static void _remove_missing_empty_pv(struct volume_group *vg, struct dm_list *remove_pvs)
825 {
826 struct pv_list *pvl, *pvl_vg, *pvlt;
827 int removed = 0;
828
829 if (!remove_pvs)
830 return;
831
832 dm_list_iterate_items(pvl, remove_pvs) {
833 dm_list_iterate_items_safe(pvl_vg, pvlt, &vg->pvs) {
834 if (!id_equal(&pvl->pv->id, &pvl_vg->pv->id) ||
835 !is_missing_pv(pvl_vg->pv) ||
836 pvl_vg->pv->pe_alloc_count != 0)
837 continue;
838
839 /* FIXME: duplication of vgreduce code, move this to library */
840 vg->free_count -= pvl_vg->pv->pe_count;
841 vg->extent_count -= pvl_vg->pv->pe_count;
842 del_pvl_from_vgs(vg, pvl_vg);
843 free_pv_fid(pvl_vg->pv);
844
845 removed++;
846 }
847 }
848
849 if (removed) {
850 if (!vg_write(vg) || !vg_commit(vg)) {
851 stack;
852 return;
853 }
854 log_warn("%d missing and now unallocated Physical Volumes removed from VG.", removed);
855 }
856 }
857
858 /*
859 * _lvconvert_mirrors_parse_params
860 *
861 * This function performs the following:
862 * 1) Gets the old values of mimage and log counts
863 * 2) Parses the CLI args to find the new desired values
864 * 3) Adjusts 'lp->mirrors' to the appropriate absolute value.
865 * (Remember, 'lp->mirrors' is specified in terms of the number of "copies"
866 * vs. the number of mimages. It can also be a relative value.)
867 * 4) Sets 'lp->need_polling' if collapsing
868 * 5) Validates other mirror params
869 *
870 * Returns: 1 on success, 0 on error
871 */
872 static int _lvconvert_mirrors_parse_params(struct cmd_context *cmd,
873 struct logical_volume *lv,
874 struct lvconvert_params *lp,
875 uint32_t *old_mimage_count,
876 uint32_t *old_log_count,
877 uint32_t *new_mimage_count,
878 uint32_t *new_log_count)
879 {
880 int repair = arg_count(cmd, repair_ARG);
881 const char *mirrorlog;
882 *old_mimage_count = lv_mirror_count(lv);
883 *old_log_count = _get_log_count(lv);
884
885 /*
886 * Collapsing a stack of mirrors:
887 *
888 * If called with no argument, try collapsing the resync layers
889 */
890 if (!arg_count(cmd, mirrors_ARG) && !arg_count(cmd, mirrorlog_ARG) &&
891 !arg_count(cmd, corelog_ARG) && !arg_count(cmd, regionsize_ARG) &&
892 !arg_count(cmd, splitmirrors_ARG) && !repair) {
893 *new_mimage_count = *old_mimage_count;
894 *new_log_count = *old_log_count;
895
896 if (find_temporary_mirror(lv) || (lv->status & CONVERTING))
897 lp->need_polling = 1;
898 return 1;
899 }
900
901 if ((arg_count(cmd, mirrors_ARG) && repair) ||
902 (arg_count(cmd, mirrorlog_ARG) && repair) ||
903 (arg_count(cmd, corelog_ARG) && repair)) {
904 log_error("--repair cannot be used with --mirrors, --mirrorlog,"
905 " or --corelog");
906 return 0;
907 }
908
909 if (arg_count(cmd, mirrorlog_ARG) && arg_count(cmd, corelog_ARG)) {
910 log_error("--mirrorlog and --corelog are incompatible");
911 return 0;
912 }
913
914 /*
915 * Adjusting mimage count?
916 */
917 if (!arg_count(cmd, mirrors_ARG) && !arg_count(cmd, splitmirrors_ARG))
918 lp->mirrors = *old_mimage_count;
919 else if (lp->mirrors_sign == SIGN_PLUS)
920 lp->mirrors = *old_mimage_count + lp->mirrors;
921 else if (lp->mirrors_sign == SIGN_MINUS)
922 lp->mirrors = (*old_mimage_count > lp->mirrors) ?
923 *old_mimage_count - lp->mirrors: 0;
924 else
925 lp->mirrors += 1;
926
927 *new_mimage_count = lp->mirrors;
928
929 /* Too many mimages? */
930 if (lp->mirrors > DEFAULT_MIRROR_MAX_IMAGES) {
931 log_error("Only up to %d images in mirror supported currently.",
932 DEFAULT_MIRROR_MAX_IMAGES);
933 return 0;
934 }
935
936 /* Did the user try to subtract more legs than available? */
937 if (lp->mirrors < 1) {
938 log_error("Unable to reduce images by specified amount - only %d in %s",
939 *old_mimage_count, lv->name);
940 return 0;
941 }
942
943 /*
944 * FIXME: It would be nice to say what we are adjusting to, but
945 * I really don't know whether to specify the # of copies or mimages.
946 */
947 if (*old_mimage_count != *new_mimage_count)
948 log_verbose("Adjusting mirror image count of %s", lv->name);
949
950 /*
951 * Adjust log type
952 *
953 * If we are converting from a mirror to another mirror or simply
954 * changing the log type, we start by assuming they want the log
955 * type the same and then parse the given args. OTOH, If we are
956 * converting from linear to mirror, then we start from the default
957 * position that the user would like a 'disk' log.
958 */
959 *new_log_count = (*old_mimage_count > 1) ? *old_log_count : 1;
960 if (!arg_count(cmd, corelog_ARG) && !arg_count(cmd, mirrorlog_ARG))
961 return 1;
962
963 if (arg_count(cmd, corelog_ARG))
964 *new_log_count = 0;
965
966 mirrorlog = arg_str_value(cmd, mirrorlog_ARG,
967 !*new_log_count ? "core" : DEFAULT_MIRRORLOG);
968
969 if (!strcmp("mirrored", mirrorlog))
970 *new_log_count = 2;
971 else if (!strcmp("disk", mirrorlog))
972 *new_log_count = 1;
973 else if (!strcmp("core", mirrorlog))
974 *new_log_count = 0;
975 else {
976 log_error("Unknown mirrorlog type: %s", mirrorlog);
977 return 0;
978 }
979
980 /*
981 * No mirrored logs for cluster mirrors until
982 * log daemon is multi-threaded.
983 */
984 if ((*new_log_count == 2) && vg_is_clustered(lv->vg)) {
985 log_error("Log type, \"mirrored\", is unavailable to cluster mirrors");
986 return 0;
987 }
988
989 log_verbose("Setting logging type to %s", mirrorlog);
990
991 /*
992 * Region size must not change on existing mirrors
993 */
994 if (arg_count(cmd, regionsize_ARG) && (lv->status & MIRRORED) &&
995 (lp->region_size != first_seg(lv)->region_size)) {
996 log_error("Mirror log region size cannot be changed on "
997 "an existing mirror.");
998 return 0;
999 }
1000
1001 /*
1002 * For the most part, we cannot handle multi-segment mirrors. Bail out
1003 * early if we have encountered one.
1004 */
1005 if ((lv->status & MIRRORED) && dm_list_size(&lv->segments) != 1) {
1006 log_error("Logical volume %s has multiple "
1007 "mirror segments.", lv->name);
1008 return 0;
1009 }
1010
1011 return 1;
1012 }
1013
1014 static int _reload_lv(struct cmd_context *cmd, struct logical_volume *lv)
1015 {
1016 log_very_verbose("Updating logical volume \"%s\" on disk(s)", lv->name);
1017
1018 if (!vg_write(lv->vg))
1019 return_0;
1020
1021 if (!suspend_lv(cmd, lv)) {
1022 log_error("Failed to lock %s", lv->name);
1023 vg_revert(lv->vg);
1024 return 0;
1025 }
1026
1027 if (!vg_commit(lv->vg)) {
1028 if (!resume_lv(cmd, lv))
1029 stack;
1030 return_0;
1031 }
1032
1033 log_very_verbose("Updating \"%s\" in kernel", lv->name);
1034
1035 if (!resume_lv(cmd, lv)) {
1036 log_error("Problem reactivating %s", lv->name);
1037 return 0;
1038 }
1039 return 1;
1040 }
1041
1042 /*
1043 * _lvconvert_mirrors_aux
1044 *
1045 * Add/remove mirror images and adjust log type. 'operable_pvs'
1046 * are the set of PVs open to removal or allocation - depending
1047 * on the operation being performed.
1048 */
1049 static int _lvconvert_mirrors_aux(struct cmd_context *cmd,
1050 struct logical_volume *lv,
1051 struct lvconvert_params *lp,
1052 struct dm_list *operable_pvs,
1053 uint32_t new_mimage_count,
1054 uint32_t new_log_count)
1055 {
1056 uint32_t region_size;
1057 struct lv_segment *seg;
1058 struct logical_volume *layer_lv;
1059 uint32_t old_mimage_count = lv_mirror_count(lv);
1060 uint32_t old_log_count = _get_log_count(lv);
1061
1062 if ((lp->mirrors == 1) && !(lv->status & MIRRORED)) {
1063 log_error("Logical volume %s is already not mirrored.",
1064 lv->name);
1065 return 1;
1066 }
1067
1068 region_size = adjusted_mirror_region_size(lv->vg->extent_size,
1069 lv->le_count,
1070 lp->region_size);
1071
1072 if (!operable_pvs)
1073 operable_pvs = lp->pvh;
1074
1075 seg = first_seg(lv);
1076
1077 /*
1078 * Up-convert from linear to mirror
1079 */
1080 if (!(lv->status & MIRRORED)) {
1081 /* FIXME Share code with lvcreate */
1082
1083 /*
1084 * FIXME should we give not only lp->pvh, but also all PVs
1085 * currently taken by the mirror? Would make more sense from
1086 * user perspective.
1087 */
1088 if (!lv_add_mirrors(cmd, lv, new_mimage_count - 1, lp->stripes,
1089 lp->stripe_size, region_size, new_log_count, operable_pvs,
1090 lp->alloc, MIRROR_BY_LV))
1091 return_0;
1092
1093 if (lp->wait_completion)
1094 lp->need_polling = 1;
1095
1096 goto out;
1097 }
1098
1099 /*
1100 * Up-convert m-way mirror to n-way mirror
1101 */
1102 if (new_mimage_count > old_mimage_count) {
1103 if (lv->status & LV_NOTSYNCED) {
1104 log_error("Can't add mirror to out-of-sync mirrored "
1105 "LV: use lvchange --resync first.");
1106 return 0;
1107 }
1108
1109 /*
1110 * We allow snapshots of mirrors, but for now, we
1111 * do not allow up converting mirrors that are under
1112 * snapshots. The layering logic is somewhat complex,
1113 * and preliminary test show that the conversion can't
1114 * seem to get the correct %'age of completion.
1115 */
1116 if (lv_is_origin(lv)) {
1117 log_error("Can't add additional mirror images to "
1118 "mirrors that are under snapshots");
1119 return 0;
1120 }
1121
1122 /*
1123 * Is there already a convert in progress? We do not
1124 * currently allow more than one.
1125 */
1126 if (find_temporary_mirror(lv) || (lv->status & CONVERTING)) {
1127 log_error("%s is already being converted. Unable to start another conversion.",
1128 lv->name);
1129 return 0;
1130 }
1131
1132 /*
1133 * Log addition/removal should be done before the layer
1134 * insertion to make the end result consistent with
1135 * linear-to-mirror conversion.
1136 */
1137 if (!_lv_update_log_type(cmd, lp, lv,
1138 operable_pvs, new_log_count)) {
1139 stack;
1140 return 0;
1141 }
1142
1143 /* Insert a temporary layer for syncing,
1144 * only if the original lv is using disk log. */
1145 if (seg->log_lv && !_insert_lvconvert_layer(cmd, lv)) {
1146 log_error("Failed to insert resync layer");
1147 return 0;
1148 }
1149
1150 /* FIXME: can't have multiple mlogs. force corelog. */
1151 if (!lv_add_mirrors(cmd, lv,
1152 new_mimage_count - old_mimage_count,
1153 lp->stripes, lp->stripe_size,
1154 region_size, 0U, operable_pvs, lp->alloc,
1155 MIRROR_BY_LV)) {
1156 layer_lv = seg_lv(first_seg(lv), 0);
1157 if (!remove_layer_from_lv(lv, layer_lv) ||
1158 !deactivate_lv(cmd, layer_lv) ||
1159 !lv_remove(layer_lv) || !vg_write(lv->vg) ||
1160 !vg_commit(lv->vg)) {
1161 log_error("ABORTING: Failed to remove "
1162 "temporary mirror layer %s.",
1163 layer_lv->name);
1164 log_error("Manual cleanup with vgcfgrestore "
1165 "and dmsetup may be required.");
1166 return 0;
1167 }
1168 stack;
1169 return 0;
1170 }
1171 if (seg->log_lv)
1172 lv->status |= CONVERTING;
1173 lp->need_polling = 1;
1174
1175 goto out_skip_log_convert;
1176 }
1177
1178 /*
1179 * Down-convert (reduce # of mimages).
1180 */
1181 if (new_mimage_count < old_mimage_count) {
1182 uint32_t nmc = old_mimage_count - new_mimage_count;
1183 uint32_t nlc = (!new_log_count || lp->mirrors == 1) ? 1U : 0U;
1184
1185 /* FIXME: Why did nlc used to be calculated that way? */
1186
1187 /* Reduce number of mirrors */
1188 if (lp->keep_mimages) {
1189 if (arg_count(cmd, trackchanges_ARG)) {
1190 log_error("--trackchanges is not available "
1191 "to 'mirror' segment type");
1192 return 0;
1193 }
1194 if (!lv_split_mirror_images(lv, lp->lv_split_name,
1195 nmc, operable_pvs))
1196 return 0;
1197 } else if (!lv_remove_mirrors(cmd, lv, nmc, nlc,
1198 is_mirror_image_removable, operable_pvs, 0))
1199 return_0;
1200
1201 goto out; /* Just in case someone puts code between */
1202 }
1203
1204 out:
1205 /*
1206 * Converting the log type
1207 */
1208 if ((lv->status & MIRRORED) && (old_log_count != new_log_count)) {
1209 if (!_lv_update_log_type(cmd, lp, lv,
1210 operable_pvs, new_log_count)) {
1211 stack;
1212 return 0;
1213 }
1214 }
1215
1216 out_skip_log_convert:
1217
1218 if (!_reload_lv(cmd, lv))
1219 return 0;
1220
1221 return 1;
1222 }
1223
1224 int mirror_remove_missing(struct cmd_context *cmd,
1225 struct logical_volume *lv, int force)
1226 {
1227 struct dm_list *failed_pvs;
1228 int log_count = _get_log_count(lv) - _failed_logs_count(lv);
1229
1230 if (!(failed_pvs = _failed_pv_list(lv->vg)))
1231 return_0;
1232
1233 /* No point in keeping a log if the result is not a mirror */
1234 if (_failed_mirrors_count(lv) + 1 >= lv_mirror_count(lv))
1235 log_count = 0;
1236
1237 if (force && _failed_mirrors_count(lv) == lv_mirror_count(lv)) {
1238 log_error("No usable images left in %s.", lv->name);
1239 return lv_remove_with_dependencies(cmd, lv, DONT_PROMPT, 0);
1240 }
1241
1242 /*
1243 * We must adjust the log first, or the entire mirror
1244 * will get stuck during a suspend.
1245 */
1246 if (!_lv_update_mirrored_log(lv, failed_pvs, log_count))
1247 return 0;
1248
1249 if (_failed_mirrors_count(lv) > 0 &&
1250 !lv_remove_mirrors(cmd, lv, _failed_mirrors_count(lv),
1251 log_count ? 0U : 1U,
1252 _is_partial_lv, NULL, 0))
1253 return 0;
1254
1255 if (!_lv_update_log_type(cmd, NULL, lv, failed_pvs,
1256 log_count))
1257 return 0;
1258
1259 if (!_reload_lv(cmd, lv))
1260 return 0;
1261
1262 return 1;
1263 }
1264
1265 /*
1266 * _lvconvert_mirrors_repair
1267 *
1268 * This function operates in two phases. First, all of the bad
1269 * devices are removed from the mirror. Then, if desired by the
1270 * user, the devices are replaced.
1271 *
1272 * 'old_mimage_count' and 'old_log_count' are there so we know
1273 * what to convert to after the removal of devices.
1274 */
1275 static int _lvconvert_mirrors_repair(struct cmd_context *cmd,
1276 struct logical_volume *lv,
1277 struct lvconvert_params *lp)
1278 {
1279 int failed_logs = 0;
1280 int failed_mimages = 0;
1281 int replace_logs = 0;
1282 int replace_mimages = 0;
1283 uint32_t log_count;
1284
1285 uint32_t original_mimages = lv_mirror_count(lv);
1286 uint32_t original_logs = _get_log_count(lv);
1287
1288 cmd->handles_missing_pvs = 1;
1289 cmd->partial_activation = 1;
1290 lp->need_polling = 0;
1291
1292 lv_check_transient(lv); /* TODO check this in lib for all commands? */
1293
1294 if (!(lv->status & PARTIAL_LV)) {
1295 log_error("%s is consistent. Nothing to repair.", lv->name);
1296 return 1;
1297 }
1298
1299 failed_mimages = _failed_mirrors_count(lv);
1300 failed_logs = _failed_logs_count(lv);
1301
1302 mirror_remove_missing(cmd, lv, 0);
1303
1304 if (failed_mimages)
1305 log_error("Mirror status: %d of %d images failed.",
1306 failed_mimages, original_mimages);
1307
1308 /*
1309 * Count the failed log devices
1310 */
1311 if (failed_logs)
1312 log_error("Mirror log status: %d of %d images failed.",
1313 failed_logs, original_logs);
1314
1315 /*
1316 * Find out our policies
1317 */
1318 _lvconvert_mirrors_repair_ask(cmd, failed_logs, failed_mimages,
1319 &replace_logs, &replace_mimages);
1320
1321 /*
1322 * Second phase - replace faulty devices
1323 */
1324 lp->mirrors = replace_mimages ? original_mimages : (original_mimages - failed_mimages);
1325
1326 /*
1327 * It does not make sense to replace the log if the volume is no longer
1328 * a mirror.
1329 */
1330 if (lp->mirrors == 1)
1331 replace_logs = 0;
1332
1333 log_count = replace_logs ? original_logs : (original_logs - failed_logs);
1334
1335 while (replace_mimages || replace_logs) {
1336 log_warn("Trying to up-convert to %d images, %d logs.", lp->mirrors, log_count);
1337 if (_lvconvert_mirrors_aux(cmd, lv, lp, NULL,
1338 lp->mirrors, log_count))
1339 break;
1340 else {
1341 if (lp->mirrors > 2)
1342 -- lp->mirrors;
1343 else if (log_count > 0)
1344 -- log_count;
1345 else
1346 break; /* nowhere to go, anymore... */
1347 }
1348 }
1349
1350 if (replace_mimages && lv_mirror_count(lv) != original_mimages)
1351 log_warn("WARNING: Failed to replace %d of %d images in volume %s",
1352 original_mimages - lv_mirror_count(lv), original_mimages, lv->name);
1353 if (replace_logs && _get_log_count(lv) != original_logs)
1354 log_warn("WARNING: Failed to replace %d of %d logs in volume %s",
1355 original_logs - _get_log_count(lv), original_logs, lv->name);
1356
1357 /* if (!arg_count(cmd, use_policies_ARG) && (lp->mirrors != old_mimage_count
1358 || log_count != old_log_count))
1359 return 0; */
1360
1361 return 1;
1362 }
1363
1364 /*
1365 * _lvconvert_mirrors
1366 *
1367 * Determine what is being done. Are we doing a conversion, repair, or
1368 * collapsing a stack? Once determined, call helper functions.
1369 */
1370 static int _lvconvert_mirrors(struct cmd_context *cmd,
1371 struct logical_volume *lv,
1372 struct lvconvert_params *lp)
1373 {
1374 int repair = arg_count(cmd, repair_ARG);
1375 uint32_t old_mimage_count;
1376 uint32_t old_log_count;
1377 uint32_t new_mimage_count;
1378 uint32_t new_log_count;
1379
1380 if (lp->merge_mirror) {
1381 log_error("Unable to merge mirror images"
1382 "of segment type 'mirror'");
1383 return 0;
1384 }
1385
1386 /* Adjust mimage and/or log count */
1387 if (!_lvconvert_mirrors_parse_params(cmd, lv, lp,
1388 &old_mimage_count, &old_log_count,
1389 &new_mimage_count, &new_log_count))
1390 return 0;
1391
1392 if (((old_mimage_count < new_mimage_count && old_log_count > new_log_count) ||
1393 (old_mimage_count > new_mimage_count && old_log_count < new_log_count)) &&
1394 lp->pv_count) {
1395 log_error("Cannot both allocate and free extents when "
1396 "specifying physical volumes to use.");
1397 log_error("Please specify the operation in two steps.");
1398 return 0;
1399 }
1400
1401 /* Nothing to do? (Probably finishing collapse.) */
1402 if ((old_mimage_count == new_mimage_count) &&
1403 (old_log_count == new_log_count) && !repair)
1404 return 1;
1405
1406 if (repair)
1407 return _lvconvert_mirrors_repair(cmd, lv, lp);
1408
1409 if (!_lvconvert_mirrors_aux(cmd, lv, lp, NULL,
1410 new_mimage_count, new_log_count))
1411 return 0;
1412
1413 if (!lp->need_polling)
1414 log_print("Logical volume %s converted.", lv->name);
1415
1416 backup(lv->vg);
1417 return 1;
1418 }
1419
1420 static int is_valid_raid_conversion(const struct segment_type *from_segtype,
1421 const struct segment_type *to_segtype)
1422 {
1423 if (from_segtype == to_segtype)
1424 return 1;
1425
1426 if (!segtype_is_raid(from_segtype) && !segtype_is_raid(to_segtype))
1427 return_0; /* Not converting to or from RAID? */
1428
1429 return 1;
1430 }
1431
1432 static void _lvconvert_raid_repair_ask(struct cmd_context *cmd, int *replace_dev)
1433 {
1434 const char *dev_policy = NULL;
1435
1436 int force = arg_count(cmd, force_ARG);
1437 int yes = arg_count(cmd, yes_ARG);
1438
1439 *replace_dev = 0;
1440
1441 if (arg_count(cmd, use_policies_ARG)) {
1442 dev_policy = find_config_tree_str(cmd, "activation/raid_fault_policy", DEFAULT_RAID_FAULT_POLICY);
1443
1444 if (!strcmp(dev_policy, "allocate") ||
1445 !strcmp(dev_policy, "replace"))
1446 *replace_dev = 1;
1447 /* else if (!strcmp(dev_policy, "anything_else")) -- ignore */
1448 return;
1449 }
1450
1451 if (yes) {
1452 *replace_dev = 1;
1453 return;
1454 }
1455
1456 if (force != PROMPT)
1457 return;
1458
1459 if (yes_no_prompt("Attempt to replace failed RAID images "
1460 "(requires full device resync)? [y/n]: ") == 'y') {
1461 *replace_dev = 1;
1462 }
1463 }
1464
1465 static int lvconvert_raid(struct logical_volume *lv, struct lvconvert_params *lp)
1466 {
1467 int replace = 0;
1468 int uninitialized_var(image_count);
1469 struct dm_list *failed_pvs;
1470 struct cmd_context *cmd = lv->vg->cmd;
1471 struct lv_segment *seg = first_seg(lv);
1472
1473 if (!arg_count(cmd, type_ARG))
1474 lp->segtype = seg->segtype;
1475
1476 /* Can only change image count for raid1 and linear */
1477 if (arg_count(cmd, mirrors_ARG) &&
1478 !seg_is_mirrored(seg) && !seg_is_linear(seg)) {
1479 log_error("'--mirrors/-m' is not compatible with %s",
1480 seg->segtype->name);
1481 return 0;
1482 }
1483
1484 if (!is_valid_raid_conversion(seg->segtype, lp->segtype)) {
1485 log_error("Unable to convert %s/%s from %s to %s",
1486 lv->vg->name, lv->name,
1487 seg->segtype->name, lp->segtype->name);
1488 return 0;
1489 }
1490
1491 /* Change number of RAID1 images */
1492 if (arg_count(cmd, mirrors_ARG) || arg_count(cmd, splitmirrors_ARG)) {
1493 image_count = lv_raid_image_count(lv);
1494 if (lp->mirrors_sign == SIGN_PLUS)
1495 image_count += lp->mirrors;
1496 else if (lp->mirrors_sign == SIGN_MINUS)
1497 image_count -= lp->mirrors;
1498 else
1499 image_count = lp->mirrors + 1;
1500
1501 if (image_count < 1) {
1502 log_error("Unable to %s images by specified amount",
1503 arg_count(cmd, splitmirrors_ARG) ?
1504 "split" : "reduce");
1505 return 0;
1506 }
1507 }
1508
1509 if (lp->merge_mirror)
1510 return lv_raid_merge(lv);
1511
1512 if (arg_count(cmd, trackchanges_ARG))
1513 return lv_raid_split_and_track(lv, lp->pvh);
1514
1515 if (arg_count(cmd, splitmirrors_ARG))
1516 return lv_raid_split(lv, lp->lv_split_name,
1517 image_count, lp->pvh);
1518
1519 if (arg_count(cmd, mirrors_ARG))
1520 return lv_raid_change_image_count(lv, image_count, lp->pvh);
1521
1522 if (arg_count(cmd, type_ARG))
1523 return lv_raid_reshape(lv, lp->segtype);
1524
1525 if (arg_count(cmd, replace_ARG))
1526 return lv_raid_replace(lv, lp->replace_pvh, lp->pvh);
1527
1528 if (arg_count(cmd, repair_ARG)) {
1529 _lvconvert_raid_repair_ask(cmd, &replace);
1530
1531 if (replace) {
1532 if (!(failed_pvs = _failed_pv_list(lv->vg)))
1533 return_0;
1534
1535 if (!lv_raid_replace(lv, failed_pvs, lp->pvh)) {
1536 log_error("Failed to replace faulty devices in"
1537 " %s/%s.", lv->vg->name, lv->name);
1538 return 0;
1539 }
1540
1541 log_print("Faulty devices in %s/%s successfully"
1542 " replaced.", lv->vg->name, lv->name);
1543 return 1;
1544 }
1545
1546 /* "warn" if policy not set to replace */
1547 if (arg_count(cmd, use_policies_ARG))
1548 log_error("Use 'lvconvert --repair %s/%s' to "
1549 "replace failed device",
1550 lv->vg->name, lv->name);
1551 return 1;
1552 }
1553
1554 log_error("Conversion operation not yet supported.");
1555 return 0;
1556 }
1557
1558 static int lvconvert_snapshot(struct cmd_context *cmd,
1559 struct logical_volume *lv,
1560 struct lvconvert_params *lp)
1561 {
1562 struct logical_volume *org;
1563 int r = 0;
1564
1565 if (!(org = find_lv(lv->vg, lp->origin))) {
1566 log_error("Couldn't find origin volume '%s'.", lp->origin);
1567 return 0;
1568 }
1569
1570 if (org == lv) {
1571 log_error("Unable to use \"%s\" as both snapshot and origin.",
1572 lv->name);
1573 return 0;
1574 }
1575
1576 if (org->status & (LOCKED|PVMOVE|MIRRORED) || lv_is_cow(org)) {
1577 log_error("Unable to convert an LV into a snapshot of a %s LV.",
1578 org->status & LOCKED ? "locked" :
1579 org->status & PVMOVE ? "pvmove" :
1580 org->status & MIRRORED ? "mirrored" :
1581 "snapshot");
1582 return 0;
1583 }
1584
1585 if (!lp->zero || !(lv->status & LVM_WRITE))
1586 log_warn("WARNING: \"%s\" not zeroed", lv->name);
1587 else if (!set_lv(cmd, lv, UINT64_C(0), 0)) {
1588 log_error("Aborting. Failed to wipe snapshot "
1589 "exception store.");
1590 return 0;
1591 }
1592
1593 if (!deactivate_lv(cmd, lv)) {
1594 log_error("Couldn't deactivate LV %s.", lv->name);
1595 return 0;
1596 }
1597
1598 if (!vg_add_snapshot(org, lv, NULL, org->le_count, lp->chunk_size)) {
1599 log_error("Couldn't create snapshot.");
1600 return 0;
1601 }
1602
1603 /* store vg on disk(s) */
1604 if (!vg_write(lv->vg))
1605 return_0;
1606
1607 if (!suspend_lv(cmd, org)) {
1608 log_error("Failed to suspend origin %s", org->name);
1609 vg_revert(lv->vg);
1610 goto out;
1611 }
1612
1613 if (!vg_commit(lv->vg))
1614 goto_out;
1615
1616 if (!resume_lv(cmd, org)) {
1617 log_error("Problem reactivating origin %s", org->name);
1618 goto out;
1619 }
1620
1621 log_print("Logical volume %s converted to snapshot.", lv->name);
1622 r = 1;
1623 out:
1624 backup(lv->vg);
1625 return r;
1626 }
1627
1628 static int lvconvert_merge(struct cmd_context *cmd,
1629 struct logical_volume *lv,
1630 struct lvconvert_params *lp)
1631 {
1632 int r = 0;
1633 int merge_on_activate = 0;
1634 struct logical_volume *origin = origin_from_cow(lv);
1635 struct lv_segment *cow_seg = find_cow(lv);
1636 struct lvinfo info;
1637
1638 /* Check if merge is possible */
1639 if (lv_is_merging_cow(lv)) {
1640 log_error("Snapshot %s is already merging", lv->name);
1641 return 0;
1642 }
1643 if (lv_is_merging_origin(origin)) {
1644 log_error("Snapshot %s is already merging into the origin",
1645 find_merging_cow(origin)->cow->name);
1646 return 0;
1647 }
1648
1649 /*
1650 * Prevent merge with open device(s) as it would likely lead
1651 * to application/filesystem failure. Merge on origin's next
1652 * activation if either the origin or snapshot LV are currently
1653 * open.
1654 *
1655 * FIXME testing open_count is racey; snapshot-merge target's
1656 * constructor and DM should prevent appropriate devices from
1657 * being open.
1658 */
1659 if (lv_info(cmd, origin, 0, &info, 1, 0)) {
1660 if (info.open_count) {
1661 log_error("Can't merge over open origin volume");
1662 merge_on_activate = 1;
1663 }
1664 }
1665 if (lv_info(cmd, lv, 0, &info, 1, 0)) {
1666 if (info.open_count) {
1667 log_print("Can't merge when snapshot is open");
1668 merge_on_activate = 1;
1669 }
1670 }
1671
1672 init_snapshot_merge(cow_seg, origin);
1673
1674 /* store vg on disk(s) */
1675 if (!vg_write(lv->vg))
1676 return_0;
1677
1678 if (merge_on_activate) {
1679 /* commit vg but skip starting the merge */
1680 if (!vg_commit(lv->vg))
1681 return_0;
1682 r = 1;
1683 log_print("Merging of snapshot %s will start "
1684 "next activation.", lv->name);
1685 goto out;
1686 }
1687
1688 /* Perform merge */
1689 if (!suspend_lv(cmd, origin)) {
1690 log_error("Failed to suspend origin %s", origin->name);
1691 vg_revert(lv->vg);
1692 goto out;
1693 }
1694
1695 if (!vg_commit(lv->vg)) {
1696 if (!resume_lv(cmd, origin))
1697 stack;
1698 goto_out;
1699 }
1700
1701 if (!resume_lv(cmd, origin)) {
1702 log_error("Failed to reactivate origin %s", origin->name);
1703 goto out;
1704 }
1705
1706 lp->need_polling = 1;
1707 lp->lv_to_poll = origin;
1708
1709 r = 1;
1710 log_print("Merging of volume %s started.", lv->name);
1711 out:
1712 backup(lv->vg);
1713 return r;
1714 }
1715
1716 static int _lvconvert_single(struct cmd_context *cmd, struct logical_volume *lv,
1717 void *handle)
1718 {
1719 struct lvconvert_params *lp = handle;
1720 struct dm_list *failed_pvs;
1721 struct lvinfo info;
1722 percent_t snap_percent;
1723
1724 if (lv->status & LOCKED) {
1725 log_error("Cannot convert locked LV %s", lv->name);
1726 return ECMD_FAILED;
1727 }
1728
1729 if (lv_is_cow(lv) && !lp->merge) {
1730 log_error("Can't convert snapshot logical volume \"%s\"",
1731 lv->name);
1732 return ECMD_FAILED;
1733 }
1734
1735 if (lv->status & PVMOVE) {
1736 log_error("Unable to convert pvmove LV %s", lv->name);
1737 return ECMD_FAILED;
1738 }
1739
1740 if (arg_count(cmd, repair_ARG) &&
1741 !(lv->status & MIRRORED) && !(lv->status & RAID)) {
1742 if (arg_count(cmd, use_policies_ARG))
1743 return ECMD_PROCESSED; /* nothing to be done here */
1744 log_error("Can't repair non-mirrored LV \"%s\".", lv->name);
1745 return ECMD_FAILED;
1746 }
1747
1748 if (!lp->segtype)
1749 lp->segtype = first_seg(lv)->segtype;
1750
1751 if (lp->merge) {
1752 if (!lv_is_cow(lv)) {
1753 log_error("Logical volume \"%s\" is not a snapshot",
1754 lv->name);
1755 return ECMD_FAILED;
1756 }
1757 if (lv_info(lv->vg->cmd, lv, 0, &info, 1, 0)
1758 && info.exists && info.live_table &&
1759 (!lv_snapshot_percent(lv, &snap_percent) ||
1760 snap_percent == PERCENT_INVALID)) {
1761 log_error("Unable to merge invalidated snapshot LV \"%s\"", lv->name);
1762 return ECMD_FAILED;
1763 }
1764 if (!archive(lv->vg)) {
1765 stack;
1766 return ECMD_FAILED;
1767 }
1768 if (!lvconvert_merge(cmd, lv, lp)) {
1769 log_error("Unable to merge LV \"%s\" into its origin.", lv->name);
1770 return ECMD_FAILED;
1771 }
1772 } else if (lp->snapshot) {
1773 if (lv->status & MIRRORED) {
1774 log_error("Unable to convert mirrored LV \"%s\" into a snapshot.", lv->name);
1775 return ECMD_FAILED;
1776 }
1777 if (!archive(lv->vg)) {
1778 stack;
1779 return ECMD_FAILED;
1780 }
1781 if (!lvconvert_snapshot(cmd, lv, lp)) {
1782 stack;
1783 return ECMD_FAILED;
1784 }
1785 } else if (segtype_is_raid(lp->segtype) ||
1786 (lv->status & RAID) || lp->merge_mirror) {
1787 if (!archive(lv->vg)) {
1788 stack;
1789 return ECMD_FAILED;
1790 }
1791 if (!lvconvert_raid(lv, lp)) {
1792 stack;
1793 return ECMD_FAILED;
1794 }
1795
1796 if (!(failed_pvs = _failed_pv_list(lv->vg))) {
1797 stack;
1798 return ECMD_FAILED;
1799 }
1800
1801 /* If repairing and using policies, remove missing PVs from VG */
1802 if (arg_count(cmd, repair_ARG) && arg_count(cmd, use_policies_ARG))
1803 _remove_missing_empty_pv(lv->vg, failed_pvs);
1804 } else if (arg_count(cmd, mirrors_ARG) ||
1805 arg_count(cmd, splitmirrors_ARG) ||
1806 (lv->status & MIRRORED)) {
1807 if (!archive(lv->vg)) {
1808 stack;
1809 return ECMD_FAILED;
1810 }
1811 if (!_lvconvert_mirrors(cmd, lv, lp)) {
1812 stack;
1813 return ECMD_FAILED;
1814 }
1815
1816 if (!(failed_pvs = _failed_pv_list(lv->vg))) {
1817 stack;
1818 return ECMD_FAILED;
1819 }
1820
1821 /* If repairing and using policies, remove missing PVs from VG */
1822 if (arg_count(cmd, repair_ARG) && arg_count(cmd, use_policies_ARG))
1823 _remove_missing_empty_pv(lv->vg, failed_pvs);
1824 }
1825
1826 return ECMD_PROCESSED;
1827 }
1828
1829 /*
1830 * FIXME move to toollib along with the rest of the drop/reacquire
1831 * VG locking that is used by lvconvert_merge_single()
1832 */
1833 static struct logical_volume *get_vg_lock_and_logical_volume(struct cmd_context *cmd,
1834 const char *vg_name,
1835 const char *lv_name)
1836 {
1837 /*
1838 * Returns NULL if the requested LV doesn't exist;
1839 * otherwise the caller must release_vg(lv->vg)
1840 * - it is also up to the caller to unlock_vg() as needed
1841 */
1842 struct volume_group *vg;
1843 struct logical_volume* lv = NULL;
1844
1845 vg = _get_lvconvert_vg(cmd, vg_name, NULL);
1846 if (vg_read_error(vg)) {
1847 release_vg(vg);
1848 return_NULL;
1849 }
1850
1851 if (!(lv = _get_lvconvert_lv(cmd, vg, lv_name, NULL, 0))) {
1852 log_error("Can't find LV %s in VG %s", lv_name, vg_name);
1853 unlock_and_release_vg(cmd, vg, vg_name);
1854 return NULL;
1855 }
1856
1857 return lv;
1858 }
1859
1860 static int poll_logical_volume(struct cmd_context *cmd, struct logical_volume *lv,
1861 int wait_completion)
1862 {
1863 struct lvinfo info;
1864
1865 if (!lv_info(cmd, lv, 0, &info, 0, 0) || !info.exists) {
1866 log_print("Conversion starts after activation.");
1867 return ECMD_PROCESSED;
1868 }
1869 return lvconvert_poll(cmd, lv, wait_completion ? 0 : 1U);
1870 }
1871
1872 static int lvconvert_single(struct cmd_context *cmd, struct lvconvert_params *lp)
1873 {
1874 struct logical_volume *lv = NULL;
1875 int ret = ECMD_FAILED;
1876 int saved_ignore_suspended_devices = ignore_suspended_devices();
1877
1878 if (arg_count(cmd, repair_ARG)) {
1879 init_ignore_suspended_devices(1);
1880 cmd->handles_missing_pvs = 1;
1881 }
1882
1883 lv = get_vg_lock_and_logical_volume(cmd, lp->vg_name, lp->lv_name);
1884 if (!lv)
1885 goto_out;
1886
1887 /*
1888 * lp->pvh holds the list of PVs available for allocation or removal
1889 */
1890 if (lp->pv_count) {
1891 if (!(lp->pvh = create_pv_list(cmd->mem, lv->vg, lp->pv_count,
1892 lp->pvs, 0)))
1893 goto_bad;
1894 } else
1895 lp->pvh = &lv->vg->pvs;
1896
1897 if (lp->replace_pv_count &&
1898 !(lp->replace_pvh = create_pv_list(cmd->mem, lv->vg,
1899 lp->replace_pv_count,
1900 lp->replace_pvs, 0)))
1901 goto_bad;
1902
1903 lp->lv_to_poll = lv;
1904 ret = _lvconvert_single(cmd, lv, lp);
1905 bad:
1906 unlock_vg(cmd, lp->vg_name);
1907
1908 if (ret == ECMD_PROCESSED && lp->need_polling)
1909 ret = poll_logical_volume(cmd, lp->lv_to_poll,
1910 lp->wait_completion);
1911
1912 release_vg(lv->vg);
1913 out:
1914 init_ignore_suspended_devices(saved_ignore_suspended_devices);
1915 return ret;
1916 }
1917
1918 static int lvconvert_merge_single(struct cmd_context *cmd, struct logical_volume *lv,
1919 void *handle)
1920 {
1921 struct lvconvert_params *lp = handle;
1922 const char *vg_name = NULL;
1923 struct logical_volume *refreshed_lv = NULL;
1924 int ret;
1925
1926 /*
1927 * FIXME can't trust lv's VG to be current given that caller
1928 * is process_each_lv() -- poll_logical_volume() may have
1929 * already updated the VG's metadata in an earlier iteration.
1930 * - preemptively drop the VG lock, as is needed for
1931 * poll_logical_volume(), refresh LV (and VG in the process).
1932 */
1933 vg_name = lv->vg->name;
1934 unlock_vg(cmd, vg_name);
1935 refreshed_lv = get_vg_lock_and_logical_volume(cmd, vg_name, lv->name);
1936 if (!refreshed_lv) {
1937 log_error("ABORTING: Can't reread LV %s/%s", vg_name, lv->name);
1938 return ECMD_FAILED;
1939 }
1940
1941 lp->lv_to_poll = refreshed_lv;
1942 ret = _lvconvert_single(cmd, refreshed_lv, lp);
1943
1944 if (ret == ECMD_PROCESSED && lp->need_polling) {
1945 /*
1946 * Must drop VG lock, because lvconvert_poll() needs it,
1947 * then reacquire it after polling completes
1948 */
1949 unlock_vg(cmd, vg_name);
1950
1951 ret = poll_logical_volume(cmd, lp->lv_to_poll,
1952 lp->wait_completion);
1953
1954 /* use LCK_VG_WRITE to match lvconvert()'s READ_FOR_UPDATE */
1955 if (!lock_vol(cmd, vg_name, LCK_VG_WRITE)) {
1956 log_error("ABORTING: Can't relock VG for %s "
1957 "after polling finished", vg_name);
1958 ret = ECMD_FAILED;
1959 }
1960 }
1961
1962 release_vg(refreshed_lv->vg);
1963
1964 return ret;
1965 }
1966
1967 int lvconvert(struct cmd_context * cmd, int argc, char **argv)
1968 {
1969 struct lvconvert_params lp;
1970
1971 if (!_read_params(&lp, cmd, argc, argv)) {
1972 stack;
1973 return EINVALID_CMD_LINE;
1974 }
1975
1976 if (lp.merge) {
1977 if (!argc) {
1978 log_error("Please provide logical volume path");
1979 return EINVALID_CMD_LINE;
1980 }
1981 return process_each_lv(cmd, argc, argv, READ_FOR_UPDATE, &lp,
1982 &lvconvert_merge_single);
1983 }
1984
1985 return lvconvert_single(cmd, &lp);
1986 }
This page took 0.127502 seconds and 5 git commands to generate.