1 /*        $NetBSD: lvconvert.c,v 1.1.1.2 2009/12/02 00:25:50 haad Exp $         */
2 
3 /*
4  * Copyright (C) 2005-2007 Red Hat, Inc. All rights reserved.
5  *
6  * This file is part of LVM2.
7  *
8  * This copyrighted material is made available to anyone wishing to use,
9  * modify, copy, or redistribute it subject to the terms and conditions
10  * of the GNU Lesser General Public License v.2.1.
11  *
12  * You should have received a copy of the GNU Lesser General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15  */
16 
17 #include "tools.h"
18 #include "polldaemon.h"
19 #include "lv_alloc.h"
20 
21 struct lvconvert_params {
22           int snapshot;
23           int zero;
24 
25           const char *origin;
26           const char *lv_name;
27           const char *lv_name_full;
28           const char *vg_name;
29           int wait_completion;
30           int need_polling;
31 
32           uint32_t chunk_size;
33           uint32_t region_size;
34 
35           uint32_t mirrors;
36           sign_t mirrors_sign;
37 
38           struct segment_type *segtype;
39 
40           alloc_policy_t alloc;
41 
42           int pv_count;
43           char **pvs;
44           struct dm_list *pvh;
45 };
46 
_lvconvert_name_params(struct lvconvert_params * lp,struct cmd_context * cmd,int * pargc,char *** pargv)47 static int _lvconvert_name_params(struct lvconvert_params *lp,
48                                           struct cmd_context *cmd,
49                                           int *pargc, char ***pargv)
50 {
51           char *ptr;
52           const char *vg_name = NULL;
53 
54           if (lp->snapshot) {
55                     if (!*pargc) {
56                               log_error("Please specify a logical volume to act as "
57                                           "the snapshot origin.");
58                               return 0;
59                     }
60 
61                     lp->origin = *pargv[0];
62                     (*pargv)++, (*pargc)--;
63                     if (!(lp->vg_name = extract_vgname(cmd, lp->origin))) {
64                               log_error("The origin name should include the "
65                                           "volume group.");
66                               return 0;
67                     }
68 
69                     /* Strip the volume group from the origin */
70                     if ((ptr = strrchr(lp->origin, (int) '/')))
71                               lp->origin = ptr + 1;
72           }
73 
74           if (!*pargc) {
75                     log_error("Please provide logical volume path");
76                     return 0;
77           }
78 
79           lp->lv_name = lp->lv_name_full = (*pargv)[0];
80           (*pargv)++, (*pargc)--;
81 
82           if (strchr(lp->lv_name_full, '/') &&
83               (vg_name = extract_vgname(cmd, lp->lv_name_full)) &&
84               lp->vg_name && strcmp(vg_name, lp->vg_name)) {
85                     log_error("Please use a single volume group name "
86                                 "(\"%s\" or \"%s\")", vg_name, lp->vg_name);
87                     return 0;
88           }
89 
90           if (!lp->vg_name)
91                     lp->vg_name = vg_name;
92 
93           if (!validate_name(lp->vg_name)) {
94                     log_error("Please provide a valid volume group name");
95                     return 0;
96           }
97 
98           if ((ptr = strrchr(lp->lv_name_full, '/')))
99                     lp->lv_name = ptr + 1;
100 
101           if (!apply_lvname_restrictions(lp->lv_name))
102                     return_0;
103 
104           return 1;
105 }
106 
_read_params(struct lvconvert_params * lp,struct cmd_context * cmd,int argc,char ** argv)107 static int _read_params(struct lvconvert_params *lp, struct cmd_context *cmd,
108                               int argc, char **argv)
109 {
110           int region_size;
111           int pagesize = lvm_getpagesize();
112 
113           memset(lp, 0, sizeof(*lp));
114 
115           if (arg_count(cmd, snapshot_ARG) &&
116               (arg_count(cmd, mirrorlog_ARG) || arg_count(cmd, mirrors_ARG) ||
117                arg_count(cmd, repair_ARG))) {
118                     log_error("--snapshot argument cannot be mixed "
119                                 "with --mirrors, --repair or --log");
120                     return 0;
121           }
122 
123           if (!arg_count(cmd, background_ARG))
124                     lp->wait_completion = 1;
125 
126           if (arg_count(cmd, snapshot_ARG))
127                     lp->snapshot = 1;
128 
129           if (arg_count(cmd, mirrors_ARG)) {
130                     lp->mirrors = arg_uint_value(cmd, mirrors_ARG, 0);
131                     lp->mirrors_sign = arg_sign_value(cmd, mirrors_ARG, 0);
132           }
133 
134           lp->alloc = arg_uint_value(cmd, alloc_ARG, ALLOC_INHERIT);
135 
136           if (lp->snapshot) {
137                     if (arg_count(cmd, regionsize_ARG)) {
138                               log_error("--regionsize is only available with mirrors");
139                               return 0;
140                     }
141 
142                     if (arg_sign_value(cmd, chunksize_ARG, 0) == SIGN_MINUS) {
143                               log_error("Negative chunk size is invalid");
144                               return 0;
145                     }
146                     lp->chunk_size = arg_uint_value(cmd, chunksize_ARG, 8);
147                     if (lp->chunk_size < 8 || lp->chunk_size > 1024 ||
148                         (lp->chunk_size & (lp->chunk_size - 1))) {
149                               log_error("Chunk size must be a power of 2 in the "
150                                           "range 4K to 512K");
151                               return 0;
152                     }
153                     log_verbose("Setting chunksize to %d sectors.", lp->chunk_size);
154 
155                     if (!(lp->segtype = get_segtype_from_string(cmd, "snapshot")))
156                               return_0;
157 
158                     lp->zero = strcmp(arg_str_value(cmd, zero_ARG,
159                                                             (lp->segtype->flags &
160                                                              SEG_CANNOT_BE_ZEROED) ?
161                                                             "n" : "y"), "n");
162 
163           } else {  /* Mirrors */
164                     if (arg_count(cmd, chunksize_ARG)) {
165                               log_error("--chunksize is only available with "
166                                           "snapshots");
167                               return 0;
168                     }
169 
170                     if (arg_count(cmd, zero_ARG)) {
171                               log_error("--zero is only available with snapshots");
172                               return 0;
173                     }
174 
175                     /*
176                      * --regionsize is only valid if converting an LV into a mirror.
177                      * Checked when we know the state of the LV being converted.
178                      */
179 
180                     if (arg_count(cmd, regionsize_ARG)) {
181                               if (arg_sign_value(cmd, regionsize_ARG, 0) ==
182                                             SIGN_MINUS) {
183                                         log_error("Negative regionsize is invalid");
184                                         return 0;
185                               }
186                               lp->region_size = arg_uint_value(cmd, regionsize_ARG, 0);
187                     } else {
188                               region_size = 2 * find_config_tree_int(cmd,
189                                                             "activation/mirror_region_size",
190                                                             DEFAULT_MIRROR_REGION_SIZE);
191                               if (region_size < 0) {
192                                         log_error("Negative regionsize in "
193                                                     "configuration file is invalid");
194                                         return 0;
195                               }
196                               lp->region_size = region_size;
197                     }
198 
199                     if (lp->region_size % (pagesize >> SECTOR_SHIFT)) {
200                               log_error("Region size (%" PRIu32 ") must be "
201                                           "a multiple of machine memory "
202                                           "page size (%d)",
203                                           lp->region_size, pagesize >> SECTOR_SHIFT);
204                               return 0;
205                     }
206 
207                     if (lp->region_size & (lp->region_size - 1)) {
208                               log_error("Region size (%" PRIu32
209                                           ") must be a power of 2", lp->region_size);
210                               return 0;
211                     }
212 
213                     if (!lp->region_size) {
214                               log_error("Non-zero region size must be supplied.");
215                               return 0;
216                     }
217 
218                     if (!(lp->segtype = get_segtype_from_string(cmd, "mirror")))
219                               return_0;
220           }
221 
222           if (activation() && lp->segtype->ops->target_present &&
223               !lp->segtype->ops->target_present(cmd, NULL, NULL)) {
224                     log_error("%s: Required device-mapper target(s) not "
225                                 "detected in your kernel", lp->segtype->name);
226                     return 0;
227           }
228 
229           if (!_lvconvert_name_params(lp, cmd, &argc, &argv))
230                     return_0;
231 
232           lp->pv_count = argc;
233           lp->pvs = argv;
234 
235           return 1;
236 }
237 
_get_lvconvert_vg(struct cmd_context * cmd,const char * lv_name,const char * uuid)238 static struct volume_group *_get_lvconvert_vg(struct cmd_context *cmd,
239                                                         const char *lv_name, const char *uuid)
240 {
241           dev_close_all();
242 
243         return vg_read_for_update(cmd, extract_vgname(cmd, lv_name),
244                                           NULL, 0);
245 }
246 
_get_lvconvert_lv(struct cmd_context * cmd __attribute ((unused)),struct volume_group * vg,const char * name,const char * uuid,uint32_t lv_type __attribute ((unused)))247 static struct logical_volume *_get_lvconvert_lv(struct cmd_context *cmd __attribute((unused)),
248                                                             struct volume_group *vg,
249                                                             const char *name,
250                                                             const char *uuid,
251                                                             uint32_t lv_type __attribute((unused)))
252 {
253           struct logical_volume *lv = find_lv(vg, name);
254 
255           if (!lv || (uuid && strcmp(uuid, (char *)&lv->lvid)))
256                     return NULL;
257 
258           return lv;
259 }
260 
_update_lvconvert_mirror(struct cmd_context * cmd __attribute ((unused)),struct volume_group * vg __attribute ((unused)),struct logical_volume * lv __attribute ((unused)),struct dm_list * lvs_changed __attribute ((unused)),unsigned flags __attribute ((unused)))261 static int _update_lvconvert_mirror(struct cmd_context *cmd __attribute((unused)),
262                                             struct volume_group *vg __attribute((unused)),
263                                             struct logical_volume *lv __attribute((unused)),
264                                             struct dm_list *lvs_changed __attribute((unused)),
265                                             unsigned flags __attribute((unused)))
266 {
267           /* lvconvert mirror doesn't require periodical metadata update */
268           return 1;
269 }
270 
_finish_lvconvert_mirror(struct cmd_context * cmd,struct volume_group * vg,struct logical_volume * lv,struct dm_list * lvs_changed __attribute ((unused)))271 static int _finish_lvconvert_mirror(struct cmd_context *cmd,
272                                             struct volume_group *vg,
273                                             struct logical_volume *lv,
274                                             struct dm_list *lvs_changed __attribute((unused)))
275 {
276           int r = 0;
277 
278           if (!collapse_mirrored_lv(lv)) {
279                     log_error("Failed to remove temporary sync layer.");
280                     return 0;
281           }
282 
283           lv->status &= ~CONVERTING;
284 
285           log_very_verbose("Updating logical volume \"%s\" on disk(s)", lv->name);
286 
287           if (!vg_write(vg))
288                     return_0;
289 
290           if (!suspend_lv(cmd, lv)) {
291                     log_error("Failed to lock %s", lv->name);
292                     vg_revert(vg);
293                     goto out;
294           }
295 
296           if (!vg_commit(vg)) {
297                     resume_lv(cmd, lv);
298                     goto_out;
299           }
300 
301           log_very_verbose("Updating \"%s\" in kernel", lv->name);
302 
303           if (!resume_lv(cmd, lv)) {
304                     log_error("Problem reactivating %s", lv->name);
305                     goto out;
306           }
307 
308           r = 1;
309           log_print("Logical volume %s converted.", lv->name);
310 out:
311           backup(vg);
312           return r;
313 }
314 
315 static struct poll_functions _lvconvert_mirror_fns = {
316           .get_copy_vg = _get_lvconvert_vg,
317           .get_copy_lv = _get_lvconvert_lv,
318           .poll_progress = poll_mirror_progress,
319           .update_metadata = _update_lvconvert_mirror,
320           .finish_copy = _finish_lvconvert_mirror,
321 };
322 
lvconvert_poll(struct cmd_context * cmd,struct logical_volume * lv,unsigned background)323 int lvconvert_poll(struct cmd_context *cmd, struct logical_volume *lv,
324                        unsigned background)
325 {
326           int len = strlen(lv->vg->name) + strlen(lv->name) + 2;
327           char *uuid = alloca(sizeof(lv->lvid));
328           char *lv_full_name = alloca(len);
329 
330 
331           if (!uuid || !lv_full_name)
332                     return_0;
333 
334           if (!dm_snprintf(lv_full_name, len, "%s/%s", lv->vg->name, lv->name))
335                     return_0;
336 
337           memcpy(uuid, &lv->lvid, sizeof(lv->lvid));
338 
339           return poll_daemon(cmd, lv_full_name, uuid, background, 0,
340                                  &_lvconvert_mirror_fns, "Converted");
341 }
342 
_insert_lvconvert_layer(struct cmd_context * cmd,struct logical_volume * lv)343 static int _insert_lvconvert_layer(struct cmd_context *cmd,
344                                            struct logical_volume *lv)
345 {
346           char *format, *layer_name;
347           size_t len;
348           int i;
349 
350           /*
351            * We would like to give the same number for this layer
352            * and the newly added mimage.
353            * However, LV name of newly added mimage is determined *after*
354            * the LV name of this layer is determined.
355            *
356            * So, use generate_lv_name() to generate mimage name first
357            * and take the number from it.
358            */
359 
360           len = strlen(lv->name) + 32;
361           if (!(format = alloca(len)) ||
362               !(layer_name = alloca(len)) ||
363               dm_snprintf(format, len, "%s_mimage_%%d", lv->name) < 0) {
364                     log_error("lvconvert: layer name allocation failed.");
365                     return 0;
366           }
367 
368           if (!generate_lv_name(lv->vg, format, layer_name, len) ||
369               sscanf(layer_name, format, &i) != 1) {
370                     log_error("lvconvert: layer name generation failed.");
371                     return 0;
372           }
373 
374           if (dm_snprintf(layer_name, len, MIRROR_SYNC_LAYER "_%d", i) < 0) {
375                     log_error("layer name allocation failed.");
376                     return 0;
377           }
378 
379           if (!insert_layer_for_lv(cmd, lv, 0, layer_name)) {
380                     log_error("Failed to insert resync layer");
381                     return 0;
382           }
383 
384           return 1;
385 }
386 
_area_missing(struct lv_segment * lvseg,int s)387 static int _area_missing(struct lv_segment *lvseg, int s)
388 {
389           if (seg_type(lvseg, s) == AREA_LV) {
390                     if (seg_lv(lvseg, s)->status & PARTIAL_LV)
391                               return 1;
392           } else if ((seg_type(lvseg, s) == AREA_PV) &&
393                        (seg_pv(lvseg, s)->status & MISSING_PV))
394                     return 1;
395 
396           return 0;
397 }
398 
399 /* FIXME we want to handle mirror stacks here... */
_failed_mirrors_count(struct logical_volume * lv)400 static int _failed_mirrors_count(struct logical_volume *lv)
401 {
402           struct lv_segment *lvseg;
403           int ret = 0;
404           int s;
405 
406           dm_list_iterate_items(lvseg, &lv->segments) {
407                     if (!seg_is_mirrored(lvseg))
408                               return -1;
409                     for (s = 0; s < lvseg->area_count; s++)
410                               if (_area_missing(lvseg, s))
411                                         ret++;
412           }
413 
414           return ret;
415 }
416 
_failed_pv_list(struct volume_group * vg)417 static struct dm_list *_failed_pv_list(struct volume_group *vg)
418 {
419           struct dm_list *failed_pvs;
420           struct pv_list *pvl, *new_pvl;
421 
422           if (!(failed_pvs = dm_pool_alloc(vg->vgmem, sizeof(*failed_pvs)))) {
423                     log_error("Allocation of list of failed_pvs failed.");
424                     return_NULL;
425           }
426 
427           dm_list_init(failed_pvs);
428 
429           dm_list_iterate_items(pvl, &vg->pvs) {
430                     if (!(pvl->pv->status & MISSING_PV))
431                               continue;
432 
433                     if (!(new_pvl = dm_pool_alloc(vg->vgmem, sizeof(*new_pvl)))) {
434                               log_error("Allocation of failed_pvs list entry failed.");
435                               return_NULL;
436                     }
437                     new_pvl->pv = pvl->pv;
438                     dm_list_add(failed_pvs, &new_pvl->list);
439           }
440 
441           return failed_pvs;
442 }
443 
444 /*
445  * Walk down the stacked mirror LV to the original mirror LV.
446  */
_original_lv(struct logical_volume * lv)447 static struct logical_volume *_original_lv(struct logical_volume *lv)
448 {
449           struct logical_volume *next_lv = lv, *tmp_lv;
450 
451           while ((tmp_lv = find_temporary_mirror(next_lv)))
452                     next_lv = tmp_lv;
453 
454           return next_lv;
455 }
456 
_lvconvert_mirrors_repair_ask(struct cmd_context * cmd,int failed_log,int failed_mirrors,int * replace_log,int * replace_mirrors)457 static void _lvconvert_mirrors_repair_ask(struct cmd_context *cmd,
458                                                     int failed_log, int failed_mirrors,
459                                                     int *replace_log, int *replace_mirrors)
460 {
461           const char *leg_policy = NULL, *log_policy = NULL;
462 
463           int force = arg_count(cmd, force_ARG);
464           int yes = arg_count(cmd, yes_ARG);
465 
466           *replace_log = *replace_mirrors = 1;
467 
468           if (arg_count(cmd, use_policies_ARG)) {
469                     leg_policy = find_config_tree_str(cmd,
470                                                   "activation/mirror_device_fault_policy",
471                                                   DEFAULT_MIRROR_DEVICE_FAULT_POLICY);
472                     log_policy = find_config_tree_str(cmd,
473                                                   "activation/mirror_log_fault_policy",
474                                                   DEFAULT_MIRROR_LOG_FAULT_POLICY);
475                     *replace_mirrors = strcmp(leg_policy, "remove");
476                     *replace_log = strcmp(log_policy, "remove");
477                     return;
478           }
479 
480           if (yes)
481                     return;
482 
483           if (force != PROMPT) {
484                     *replace_log = *replace_mirrors = 0;
485                     return;
486           }
487 
488           if (failed_log &&
489               yes_no_prompt("Attempt to replace failed mirror log? [y/n]: ") == 'n') {
490                     *replace_log = 0;
491           }
492 
493           if (failed_mirrors &&
494               yes_no_prompt("Attempt to replace failed mirror images "
495                                 "(requires full device resync)? [y/n]: ") == 'n') {
496                     *replace_mirrors = 0;
497           }
498 }
499 
_using_corelog(struct logical_volume * lv)500 static int _using_corelog(struct logical_volume *lv)
501 {
502           return !first_seg(_original_lv(lv))->log_lv;
503 }
504 
_lv_update_log_type(struct cmd_context * cmd,struct lvconvert_params * lp,struct logical_volume * lv,int corelog)505 static int _lv_update_log_type(struct cmd_context *cmd,
506                                      struct lvconvert_params *lp,
507                                      struct logical_volume *lv,
508                                      int corelog)
509 {
510           struct logical_volume *original_lv = _original_lv(lv);
511           if (_using_corelog(lv) && !corelog) {
512                     if (!add_mirror_log(cmd, original_lv, 1,
513                                             adjusted_mirror_region_size(
514                                                   lv->vg->extent_size,
515                                                   lv->le_count,
516                                                   lp->region_size),
517                                             lp->pvh, lp->alloc))
518                               return_0;
519           } else if (!_using_corelog(lv) && corelog) {
520                     if (!remove_mirror_log(cmd, original_lv,
521                                                lp->pv_count ? lp->pvh : NULL))
522                               return_0;
523           }
524           return 1;
525 }
526 
_lvconvert_mirrors(struct cmd_context * cmd,struct logical_volume * lv,struct lvconvert_params * lp)527 static int _lvconvert_mirrors(struct cmd_context *cmd, struct logical_volume *lv,
528                                     struct lvconvert_params *lp)
529 {
530           struct lv_segment *seg;
531           uint32_t existing_mirrors;
532           const char *mirrorlog;
533           unsigned corelog = 0;
534           int r = 0;
535           struct logical_volume *log_lv, *layer_lv;
536           int failed_mirrors = 0, failed_log = 0;
537           struct dm_list *old_pvh = NULL, *remove_pvs = NULL;
538 
539           int repair = arg_count(cmd, repair_ARG);
540           int replace_log = 1, replace_mirrors = 1;
541 
542           seg = first_seg(lv);
543           existing_mirrors = lv_mirror_count(lv);
544 
545           /* If called with no argument, try collapsing the resync layers */
546           if (!arg_count(cmd, mirrors_ARG) && !arg_count(cmd, mirrorlog_ARG) &&
547               !arg_count(cmd, corelog_ARG) && !arg_count(cmd, regionsize_ARG) &&
548               !repair) {
549                     if (find_temporary_mirror(lv) || (lv->status & CONVERTING))
550                               lp->need_polling = 1;
551                     return 1;
552           }
553 
554           if (arg_count(cmd, mirrors_ARG) && repair) {
555                     log_error("You may only use one of --mirrors and --repair.");
556                     return 0;
557           }
558 
559           /*
560            * Adjust required number of mirrors
561            *
562            * We check mirrors_ARG again to see if it
563            * was supplied.  If not, they want the mirror
564            * count to remain the same.  They may be changing
565            * the logging type.
566            */
567           if (!arg_count(cmd, mirrors_ARG))
568                     lp->mirrors = existing_mirrors;
569           else if (lp->mirrors_sign == SIGN_PLUS)
570                     lp->mirrors = existing_mirrors + lp->mirrors;
571           else if (lp->mirrors_sign == SIGN_MINUS)
572                     lp->mirrors = existing_mirrors - lp->mirrors;
573           else
574                     lp->mirrors += 1;
575 
576           if (repair) {
577                     cmd->handles_missing_pvs = 1;
578                     cmd->partial_activation = 1;
579                     lp->need_polling = 0;
580                     if (!(lv->status & PARTIAL_LV)) {
581                               log_error("The mirror is consistent, nothing to repair.");
582                               return 0;
583                     }
584                     if ((failed_mirrors = _failed_mirrors_count(lv)) < 0)
585                               return_0;
586                     lp->mirrors -= failed_mirrors;
587                     log_error("Mirror status: %d of %d images failed.",
588                                 failed_mirrors, existing_mirrors);
589                     old_pvh = lp->pvh;
590                     if (!(lp->pvh = _failed_pv_list(lv->vg)))
591                               return_0;
592                     log_lv=first_seg(lv)->log_lv;
593                     if (!log_lv || log_lv->status & PARTIAL_LV)
594                               failed_log = corelog = 1;
595           } else {
596                     /*
597                      * Did the user try to subtract more legs than available?
598                      */
599                     if (lp->mirrors < 1) {
600                               log_error("Logical volume %s only has %" PRIu32 " mirrors.",
601                                           lv->name, existing_mirrors);
602                               return 0;
603                     }
604 
605                     /*
606                      * Adjust log type
607                      */
608                     if (arg_count(cmd, corelog_ARG))
609                               corelog = 1;
610 
611                     mirrorlog = arg_str_value(cmd, mirrorlog_ARG,
612                                                     corelog ? "core" : DEFAULT_MIRRORLOG);
613                     if (!strcmp("disk", mirrorlog)) {
614                               if (corelog) {
615                                         log_error("--mirrorlog disk and --corelog "
616                                                     "are incompatible");
617                                         return 0;
618                               }
619                               corelog = 0;
620                     } else if (!strcmp("core", mirrorlog))
621                               corelog = 1;
622                     else {
623                               log_error("Unknown mirrorlog type: %s", mirrorlog);
624                               return 0;
625                     }
626 
627                     log_verbose("Setting logging type to %s", mirrorlog);
628           }
629 
630           /*
631            * Region size must not change on existing mirrors
632            */
633           if (arg_count(cmd, regionsize_ARG) && (lv->status & MIRRORED) &&
634               (lp->region_size != seg->region_size)) {
635                     log_error("Mirror log region size cannot be changed on "
636                                 "an existing mirror.");
637                     return 0;
638           }
639 
640           /*
641            * For the most part, we cannot handle multi-segment mirrors. Bail out
642            * early if we have encountered one.
643            */
644           if ((lv->status & MIRRORED) && dm_list_size(&lv->segments) != 1) {
645                     log_error("Logical volume %s has multiple "
646                                 "mirror segments.", lv->name);
647                     return 0;
648           }
649 
650           if (repair)
651                     _lvconvert_mirrors_repair_ask(cmd, failed_log, failed_mirrors,
652                                                         &replace_log, &replace_mirrors);
653 
654  restart:
655           /*
656            * Converting from mirror to linear
657            */
658           if ((lp->mirrors == 1)) {
659                     if (!(lv->status & MIRRORED)) {
660                               log_error("Logical volume %s is already not mirrored.",
661                                           lv->name);
662                               return 1;
663                     }
664           }
665 
666           /*
667            * Downconversion.
668            */
669           if (lp->mirrors < existing_mirrors) {
670                     /* Reduce number of mirrors */
671                     if (repair || lp->pv_count)
672                               remove_pvs = lp->pvh;
673                     if (!lv_remove_mirrors(cmd, lv, existing_mirrors - lp->mirrors,
674                                                (corelog || lp->mirrors == 1) ? 1U : 0U,
675                                                remove_pvs, 0))
676                               return_0;
677                     if (lp->mirrors > 1 &&
678                         !_lv_update_log_type(cmd, lp, lv, corelog))
679                               return_0;
680           } else if (!(lv->status & MIRRORED)) {
681                     /*
682                      * Converting from linear to mirror
683                      */
684 
685                     /* FIXME Share code with lvcreate */
686 
687                     /* FIXME Why is this restriction here?  Fix it! */
688                     dm_list_iterate_items(seg, &lv->segments) {
689                               if (seg_is_striped(seg) && seg->area_count > 1) {
690                                         log_error("Mirrors of striped volumes are not yet supported.");
691                                         return 0;
692                               }
693                     }
694 
695                     /*
696                      * FIXME should we give not only lp->pvh, but also all PVs
697                      * currently taken by the mirror? Would make more sense from
698                      * user perspective.
699                      */
700                     if (!lv_add_mirrors(cmd, lv, lp->mirrors - 1, 1,
701                                             adjusted_mirror_region_size(
702                                                             lv->vg->extent_size,
703                                                             lv->le_count,
704                                                             lp->region_size),
705                                             corelog ? 0U : 1U, lp->pvh, lp->alloc,
706                                             MIRROR_BY_LV))
707                               return_0;
708                     if (lp->wait_completion)
709                               lp->need_polling = 1;
710           } else if (lp->mirrors > existing_mirrors || failed_mirrors) {
711                     if (lv->status & MIRROR_NOTSYNCED) {
712                               log_error("Can't add mirror to out-of-sync mirrored "
713                                           "LV: use lvchange --resync first.");
714                               return 0;
715                     }
716 
717                     /*
718                      * We allow snapshots of mirrors, but for now, we
719                      * do not allow up converting mirrors that are under
720                      * snapshots.  The layering logic is somewhat complex,
721                      * and preliminary test show that the conversion can't
722                      * seem to get the correct %'age of completion.
723                      */
724                     if (lv_is_origin(lv)) {
725                               log_error("Can't add additional mirror images to "
726                                           "mirrors that are under snapshots");
727                               return 0;
728                     }
729 
730                     /*
731                      * Log addition/removal should be done before the layer
732                      * insertion to make the end result consistent with
733                      * linear-to-mirror conversion.
734                      */
735                     if (!_lv_update_log_type(cmd, lp, lv, corelog))
736                               return_0;
737                     /* Insert a temporary layer for syncing,
738                      * only if the original lv is using disk log. */
739                     if (seg->log_lv && !_insert_lvconvert_layer(cmd, lv)) {
740                               log_error("Failed to insert resync layer");
741                               return 0;
742                     }
743                     /* FIXME: can't have multiple mlogs. force corelog. */
744                     if (!lv_add_mirrors(cmd, lv, lp->mirrors - existing_mirrors, 1,
745                                             adjusted_mirror_region_size(
746                                                             lv->vg->extent_size,
747                                                             lv->le_count,
748                                                             lp->region_size),
749                                             0U, lp->pvh, lp->alloc,
750                                             MIRROR_BY_LV)) {
751                               layer_lv = seg_lv(first_seg(lv), 0);
752                               if (!remove_layer_from_lv(lv, layer_lv) ||
753                                   !deactivate_lv(cmd, layer_lv) ||
754                                   !lv_remove(layer_lv) || !vg_write(lv->vg) ||
755                                   !vg_commit(lv->vg)) {
756                                         log_error("ABORTING: Failed to remove "
757                                                     "temporary mirror layer %s.",
758                                                     layer_lv->name);
759                                         log_error("Manual cleanup with vgcfgrestore "
760                                                     "and dmsetup may be required.");
761                                         return 0;
762                               }
763                               return_0;
764                     }
765                     lv->status |= CONVERTING;
766                     lp->need_polling = 1;
767           }
768 
769           if (lp->mirrors == existing_mirrors) {
770                     if (_using_corelog(lv) != corelog) {
771                               if (!_lv_update_log_type(cmd, lp, lv, corelog))
772                                         return_0;
773                     } else {
774                               log_error("Logical volume %s already has %"
775                                           PRIu32 " mirror(s).", lv->name,
776                                           lp->mirrors - 1);
777                               if (lv->status & CONVERTING)
778                                         lp->need_polling = 1;
779                               return 1;
780                     }
781           }
782 
783           log_very_verbose("Updating logical volume \"%s\" on disk(s)", lv->name);
784 
785           if (!vg_write(lv->vg))
786                     return_0;
787 
788           if (!suspend_lv(cmd, lv)) {
789                     log_error("Failed to lock %s", lv->name);
790                     vg_revert(lv->vg);
791                     goto out;
792           }
793 
794           if (!vg_commit(lv->vg)) {
795                     resume_lv(cmd, lv);
796                     goto_out;
797           }
798 
799           log_very_verbose("Updating \"%s\" in kernel", lv->name);
800 
801           if (!resume_lv(cmd, lv)) {
802                     log_error("Problem reactivating %s", lv->name);
803                     goto out;
804           }
805 
806           if (failed_log || failed_mirrors) {
807                     lp->pvh = old_pvh;
808                     if (failed_log && replace_log)
809                               failed_log = corelog = 0;
810                     if (replace_mirrors)
811                               lp->mirrors += failed_mirrors;
812                     failed_mirrors = 0;
813                     existing_mirrors = lv_mirror_count(lv);
814                     /* Now replace missing devices. */
815                     if (replace_log || replace_mirrors)
816                               goto restart;
817           }
818 
819           if (!lp->need_polling)
820                     log_print("Logical volume %s converted.", lv->name);
821 
822           r = 1;
823 out:
824           backup(lv->vg);
825           return r;
826 }
827 
lvconvert_snapshot(struct cmd_context * cmd,struct logical_volume * lv,struct lvconvert_params * lp)828 static int lvconvert_snapshot(struct cmd_context *cmd,
829                                     struct logical_volume *lv,
830                                     struct lvconvert_params *lp)
831 {
832           struct logical_volume *org;
833           int r = 0;
834 
835           if (!(org = find_lv(lv->vg, lp->origin))) {
836                     log_error("Couldn't find origin volume '%s'.", lp->origin);
837                     return 0;
838           }
839 
840           if (org == lv) {
841                     log_error("Unable to use \"%s\" as both snapshot and origin.",
842                                 lv->name);
843                     return 0;
844           }
845 
846           if (org->status & (LOCKED|PVMOVE|MIRRORED) || lv_is_cow(org)) {
847                     log_error("Unable to create a snapshot of a %s LV.",
848                                 org->status & LOCKED ? "locked" :
849                                 org->status & PVMOVE ? "pvmove" :
850                                 org->status & MIRRORED ? "mirrored" :
851                                 "snapshot");
852                     return 0;
853           }
854 
855           if (!lp->zero || !(lv->status & LVM_WRITE))
856                     log_warn("WARNING: \"%s\" not zeroed", lv->name);
857           else if (!set_lv(cmd, lv, UINT64_C(0), 0)) {
858                     log_error("Aborting. Failed to wipe snapshot "
859                                 "exception store.");
860                     return 0;
861           }
862 
863           if (!deactivate_lv(cmd, lv)) {
864                     log_error("Couldn't deactivate LV %s.", lv->name);
865                     return 0;
866           }
867 
868           if (!vg_add_snapshot(org, lv, NULL, org->le_count, lp->chunk_size)) {
869                     log_error("Couldn't create snapshot.");
870                     return 0;
871           }
872 
873           /* store vg on disk(s) */
874           if (!vg_write(lv->vg))
875                     return_0;
876 
877           if (!suspend_lv(cmd, org)) {
878                     log_error("Failed to suspend origin %s", org->name);
879                     vg_revert(lv->vg);
880                     goto out;
881           }
882 
883           if (!vg_commit(lv->vg))
884                     goto_out;
885 
886           if (!resume_lv(cmd, org)) {
887                     log_error("Problem reactivating origin %s", org->name);
888                     goto out;
889           }
890 
891           log_print("Logical volume %s converted to snapshot.", lv->name);
892           r = 1;
893 out:
894           backup(lv->vg);
895           return r;
896 }
897 
lvconvert_single(struct cmd_context * cmd,struct logical_volume * lv,void * handle)898 static int lvconvert_single(struct cmd_context *cmd, struct logical_volume *lv,
899                                   void *handle)
900 {
901           struct lvconvert_params *lp = handle;
902 
903           if (lv->status & LOCKED) {
904                     log_error("Cannot convert locked LV %s", lv->name);
905                     return ECMD_FAILED;
906           }
907 
908           if (lv_is_cow(lv)) {
909                     log_error("Can't convert snapshot logical volume \"%s\"",
910                                 lv->name);
911                     return ECMD_FAILED;
912           }
913 
914           if (lv->status & PVMOVE) {
915                     log_error("Unable to convert pvmove LV %s", lv->name);
916                     return ECMD_FAILED;
917           }
918 
919           if (arg_count(cmd, repair_ARG) && !(lv->status & MIRRORED)) {
920                     log_error("Can't repair non-mirrored LV \"%s\".", lv->name);
921                     return ECMD_FAILED;
922           }
923 
924           if (lp->snapshot) {
925                     if (lv->status & MIRRORED) {
926                               log_error("Unable to convert mirrored LV \"%s\" into a snapshot.", lv->name);
927                               return ECMD_FAILED;
928                     }
929                     if (!archive(lv->vg)) {
930                               stack;
931                               return ECMD_FAILED;
932                     }
933                     if (!lvconvert_snapshot(cmd, lv, lp)) {
934                               stack;
935                               return ECMD_FAILED;
936                     }
937           } else if (arg_count(cmd, mirrors_ARG) || (lv->status & MIRRORED)) {
938                     if (!archive(lv->vg)) {
939                               stack;
940                               return ECMD_FAILED;
941                     }
942                     if (!_lvconvert_mirrors(cmd, lv, lp)) {
943                               stack;
944                               return ECMD_FAILED;
945                     }
946           }
947 
948           return ECMD_PROCESSED;
949 }
950 
lvconvert(struct cmd_context * cmd,int argc,char ** argv)951 int lvconvert(struct cmd_context * cmd, int argc, char **argv)
952 {
953           struct volume_group *vg;
954           struct lv_list *lvl;
955           struct lvconvert_params lp;
956           int ret = ECMD_FAILED;
957           struct lvinfo info;
958           int saved_ignore_suspended_devices = ignore_suspended_devices();
959 
960           if (!_read_params(&lp, cmd, argc, argv)) {
961                     stack;
962                     return EINVALID_CMD_LINE;
963           }
964 
965           if (arg_count(cmd, repair_ARG)) {
966                     init_ignore_suspended_devices(1);
967                     cmd->handles_missing_pvs = 1;
968           }
969 
970           log_verbose("Checking for existing volume group \"%s\"", lp.vg_name);
971 
972           vg = vg_read_for_update(cmd, lp.vg_name, NULL, 0);
973           if (vg_read_error(vg))
974                     goto out;
975 
976           if (!(lvl = find_lv_in_vg(vg, lp.lv_name))) {
977                     log_error("Logical volume \"%s\" not found in "
978                                 "volume group \"%s\"", lp.lv_name, lp.vg_name);
979                     goto bad;
980           }
981 
982           if (lp.pv_count) {
983                     if (!(lp.pvh = create_pv_list(cmd->mem, vg, lp.pv_count,
984                                                         lp.pvs, 0)))
985                               goto_bad;
986           } else
987                     lp.pvh = &vg->pvs;
988 
989           ret = lvconvert_single(cmd, lvl->lv, &lp);
990 
991 bad:
992           unlock_vg(cmd, lp.vg_name);
993 
994           if (ret == ECMD_PROCESSED && lp.need_polling) {
995                     if (!lv_info(cmd, lvl->lv, &info, 1, 0) || !info.exists) {
996                               log_print("Conversion starts after activation");
997                               goto out;
998                     }
999                     ret = lvconvert_poll(cmd, lvl->lv,
1000                                              lp.wait_completion ? 0 : 1U);
1001           }
1002 out:
1003           init_ignore_suspended_devices(saved_ignore_suspended_devices);
1004           vg_release(vg);
1005           return ret;
1006 }
1007