forked from lvmteam/lvm2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlvconvert.c
6476 lines (5326 loc) · 182 KB
/
lvconvert.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2005-2016 Red Hat, Inc. All rights reserved.
*
* This file is part of LVM2.
*
* This copyrighted material is made available to anyone wishing to use,
* modify, copy, or redistribute it subject to the terms and conditions
* of the GNU Lesser General Public License v.2.1.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "tools.h"
#include "lib/lvmpolld/polldaemon.h"
#include "lib/metadata/lv_alloc.h"
#include "lib/metadata/metadata.h"
#include "lvconvert_poll.h"
#define MAX_PDATA_ARGS 10 /* Max number of accepted args for d-m-p-d tools */
typedef enum {
/* Split:
* For a mirrored or raid LV, split mirror into two mirrors, optionally tracking
* future changes to the main mirror to allow future recombination.
*/
CONV_SPLIT_MIRRORS = 2,
/* Every other segment type or mirror log conversion we haven't separated out */
CONV_OTHER = 3,
} conversion_type_t;
struct lvconvert_params {
/* Exactly one of these 12 command categories is determined */
int keep_mimages; /* 2 */ /* --splitmirrors */
/* other */ /* 3 */
/* FIXME Eliminate all cases where more than one of the above are set then use conv_type instead */
conversion_type_t conv_type;
int track_changes; /* CONV_SPLIT_MIRRORS is set */
int corelog; /* Equivalent to --mirrorlog core */
int mirrorlog; /* Only one of corelog and mirrorlog may be set */
int mirrors_supplied; /* When type_str is not set, this may be set with keep_mimages for --splitmirrors */
const char *type_str; /* When this is set, mirrors_supplied may optionally also be set */
/* Holds what you asked for based on --type or other arguments, else "" */
const struct segment_type *segtype; /* Holds what segment type you will get */
int force;
int yes;
int zero;
const char *lv_name;
const char *lv_split_name;
const char *lv_name_full;
const char *vg_name;
int wait_completion;
int need_polling;
uint32_t region_size;
unsigned region_size_supplied;
uint32_t mirrors;
sign_t mirrors_sign;
uint32_t stripes;
uint32_t stripe_size;
unsigned stripes_supplied;
unsigned stripe_size_supplied;
uint32_t read_ahead;
unsigned target_attr;
alloc_policy_t alloc;
int pv_count;
char **pvs;
struct dm_list *pvh;
struct logical_volume *lv_to_poll;
struct dm_list idls;
const char *origin_name;
};
struct convert_poll_id_list {
struct dm_list list;
struct poll_operation_id *id;
unsigned is_merging_origin:1;
unsigned is_merging_origin_thin:1;
};
/* FIXME Temporary function until the enum replaces the separate variables */
static void _set_conv_type(struct lvconvert_params *lp, conversion_type_t conv_type)
{
if (lp->conv_type != CONV_OTHER)
log_error(INTERNAL_ERROR "Changing conv_type from %d to %d.", lp->conv_type, conv_type);
lp->conv_type = conv_type;
}
static int _raid0_type_requested(const char *type_str)
{
return (!strcmp(type_str, SEG_TYPE_NAME_RAID0) || !strcmp(type_str, SEG_TYPE_NAME_RAID0_META));
}
/* mirror/raid* (1,10,4,5,6 and their variants) reshape */
static int _mirror_or_raid_type_requested(struct cmd_context *cmd, const char *type_str)
{
return (arg_is_set(cmd, mirrors_ARG) || !strcmp(type_str, SEG_TYPE_NAME_MIRROR) ||
(!strncmp(type_str, SEG_TYPE_NAME_RAID, 4) && !_raid0_type_requested(type_str)));
}
static int _linear_type_requested(const char *type_str)
{
return (!strcmp(type_str, SEG_TYPE_NAME_LINEAR));
}
static int _striped_type_requested(const char *type_str)
{
return (!strcmp(type_str, SEG_TYPE_NAME_STRIPED) || _linear_type_requested(type_str));
}
static int _read_conversion_type(struct cmd_context *cmd,
struct lvconvert_params *lp)
{
const char *type_str = arg_str_value(cmd, type_ARG, "");
lp->type_str = type_str;
if (!lp->type_str[0])
return 1;
/* FIXME: Check thin-pool and thin more thoroughly! */
if (!strcmp(type_str, SEG_TYPE_NAME_SNAPSHOT) || _striped_type_requested(type_str) ||
!strncmp(type_str, SEG_TYPE_NAME_RAID, 4) || !strcmp(type_str, SEG_TYPE_NAME_MIRROR) ||
!strcmp(type_str, SEG_TYPE_NAME_CACHE_POOL) || !strcmp(type_str, SEG_TYPE_NAME_CACHE) ||
!strcmp(type_str, SEG_TYPE_NAME_THIN_POOL) || !strcmp(type_str, SEG_TYPE_NAME_THIN))
return 1;
log_error("Conversion using --type %s is not supported.", type_str);
return 0;
}
static int _read_params(struct cmd_context *cmd, struct lvconvert_params *lp)
{
const char *vg_name = NULL;
if (!_read_conversion_type(cmd, lp))
return_0;
if (!arg_is_set(cmd, background_ARG))
lp->wait_completion = 1;
if (arg_is_set(cmd, corelog_ARG))
lp->corelog = 1;
if (arg_is_set(cmd, mirrorlog_ARG)) {
if (lp->corelog) {
log_error("--mirrorlog and --corelog are incompatible.");
return 0;
}
lp->mirrorlog = 1;
}
if (arg_is_set(cmd, trackchanges_ARG))
lp->track_changes = 1;
/*
* The '--splitmirrors n' argument is equivalent to '--mirrors -n'
* (note the minus sign), except that it signifies the additional
* intent to keep the mimage that is detached, rather than
* discarding it.
*/
if (arg_is_set(cmd, splitmirrors_ARG)) {
if ((lp->lv_split_name = arg_str_value(cmd, name_ARG, NULL))) {
if (!validate_restricted_lvname_param(cmd, &vg_name, &lp->lv_split_name))
return_0;
}
if (_mirror_or_raid_type_requested(cmd, lp->type_str)) {
log_error("--mirrors/--type mirror/--type raid* and --splitmirrors are "
"mutually exclusive.");
return 0;
}
if (!arg_is_set(cmd, name_ARG) && !lp->track_changes) {
log_error("Please name the new logical volume using '--name'");
return 0;
}
if ((lp->lv_split_name = arg_str_value(cmd, name_ARG, NULL))) {
if (!validate_restricted_lvname_param(cmd, &vg_name, &lp->lv_split_name))
return_0;
}
lp->keep_mimages = 1;
_set_conv_type(lp, CONV_SPLIT_MIRRORS);
lp->mirrors = arg_uint_value(cmd, splitmirrors_ARG, 0);
lp->mirrors_sign = SIGN_MINUS;
}
/* If no other case was identified, then use of --stripes means --type striped */
if (!arg_is_set(cmd, type_ARG) && !*lp->type_str &&
!lp->mirrorlog && !lp->corelog &&
(arg_is_set(cmd, stripes_long_ARG) || arg_is_set(cmd, stripesize_ARG)))
lp->type_str = SEG_TYPE_NAME_STRIPED;
if ((arg_is_set(cmd, stripes_long_ARG) || arg_is_set(cmd, stripesize_ARG)) &&
!(_mirror_or_raid_type_requested(cmd, lp->type_str) || _striped_type_requested(lp->type_str) ||
_raid0_type_requested(lp->type_str) || arg_is_set(cmd, thinpool_ARG))) {
log_error("--stripes or --stripesize argument is only valid "
"with --mirrors/--type mirror/--type raid*/--type striped/--type linear, --repair and --thinpool");
return 0;
}
if (arg_is_set(cmd, mirrors_ARG)) {
/* --splitmirrors is the mechanism for detaching and keeping a mimage */
lp->mirrors_supplied = 1;
lp->mirrors = arg_uint_value(cmd, mirrors_ARG, 0);
lp->mirrors_sign = arg_sign_value(cmd, mirrors_ARG, SIGN_NONE);
}
lp->alloc = (alloc_policy_t) arg_uint_value(cmd, alloc_ARG, ALLOC_INHERIT);
/*
* Final checking of each case:
* lp->keep_mimages
* --type mirror|raid lp->mirrorlog lp->corelog
* --type raid0|striped
*/
switch(lp->conv_type) {
case CONV_SPLIT_MIRRORS:
break;
case CONV_OTHER:
if (arg_is_set(cmd, regionsize_ARG)) {
lp->region_size = arg_uint_value(cmd, regionsize_ARG, 0);
lp->region_size_supplied = 1;
} else {
lp->region_size = get_default_region_size(cmd);
lp->region_size_supplied = 0;
}
if (_mirror_or_raid_type_requested(cmd, lp->type_str) ||
lp->mirrorlog || lp->corelog) { /* Mirrors (and some RAID functions) */
if (arg_is_set(cmd, chunksize_ARG)) {
log_error("--chunksize is only available with snapshots or pools.");
return 0;
}
if (arg_is_set(cmd, zero_ARG)) {
log_error("--zero is only available with snapshots or pools.");
return 0;
}
/* FIXME man page says in one place that --type and --mirrors can't be mixed */
if (lp->mirrors_supplied && !lp->mirrors)
/* down-converting to linear/stripe? */
lp->type_str = SEG_TYPE_NAME_STRIPED;
} else if (_raid0_type_requested(lp->type_str) || _striped_type_requested(lp->type_str)) { /* striped or linear or raid0 */
if (arg_from_list_is_set(cmd, "cannot be used with --type raid0 or --type striped or --type linear",
chunksize_ARG, corelog_ARG, mirrors_ARG, mirrorlog_ARG, zero_ARG,
-1))
return_0;
} /* else segtype will default to current type */
}
lp->force = arg_count(cmd, force_ARG);
lp->yes = arg_count(cmd, yes_ARG);
return 1;
}
static struct poll_functions _lvconvert_mirror_fns = {
.poll_progress = poll_mirror_progress,
.finish_copy = lvconvert_mirror_finish,
};
static struct poll_functions _lvconvert_merge_fns = {
.poll_progress = poll_merge_progress,
.finish_copy = lvconvert_merge_finish,
};
static struct poll_functions _lvconvert_thin_merge_fns = {
.poll_progress = poll_thin_merge_progress,
.finish_copy = lvconvert_merge_finish,
};
static struct poll_operation_id *_create_id(struct cmd_context *cmd,
const char *vg_name,
const char *lv_name,
const char *uuid)
{
struct poll_operation_id *id;
char lv_full_name[NAME_LEN];
if (!vg_name || !lv_name || !uuid) {
log_error(INTERNAL_ERROR "Wrong params for lvconvert _create_id.");
return NULL;
}
if (dm_snprintf(lv_full_name, sizeof(lv_full_name), "%s/%s", vg_name, lv_name) < 0) {
log_error(INTERNAL_ERROR "Name \"%s/%s\" is too long.", vg_name, lv_name);
return NULL;
}
if (!(id = dm_pool_alloc(cmd->mem, sizeof(*id)))) {
log_error("Poll operation ID allocation failed.");
return NULL;
}
if (!(id->display_name = dm_pool_strdup(cmd->mem, lv_full_name)) ||
!(id->lv_name = strchr(id->display_name, '/')) ||
!(id->vg_name = dm_pool_strdup(cmd->mem, vg_name)) ||
!(id->uuid = dm_pool_strdup(cmd->mem, uuid))) {
log_error("Failed to copy one or more poll operation ID members.");
dm_pool_free(cmd->mem, id);
return NULL;
}
id->lv_name++; /* skip over '/' */
return id;
}
static int _lvconvert_poll_by_id(struct cmd_context *cmd, struct poll_operation_id *id,
unsigned background,
int is_merging_origin,
int is_merging_origin_thin)
{
if (test_mode())
return ECMD_PROCESSED;
if (is_merging_origin)
return poll_daemon(cmd, background,
(MERGING | (is_merging_origin_thin ? THIN_VOLUME : SNAPSHOT)),
is_merging_origin_thin ? &_lvconvert_thin_merge_fns : &_lvconvert_merge_fns,
"Merged", id);
return poll_daemon(cmd, background, CONVERTING,
&_lvconvert_mirror_fns, "Converted", id);
}
int lvconvert_poll(struct cmd_context *cmd, struct logical_volume *lv,
unsigned background)
{
int r;
struct poll_operation_id *id = _create_id(cmd, lv->vg->name, lv->name, lv->lvid.s);
int is_merging_origin = 0;
int is_merging_origin_thin = 0;
if (!id) {
log_error("Failed to allocate poll identifier for lvconvert.");
return ECMD_FAILED;
}
/* FIXME: check this in polling instead */
if (lv_is_merging_origin(lv)) {
is_merging_origin = 1;
is_merging_origin_thin = seg_is_thin_volume(find_snapshot(lv));
}
r = _lvconvert_poll_by_id(cmd, id, background, is_merging_origin, is_merging_origin_thin);
return r;
}
static int _insert_lvconvert_layer(struct cmd_context *cmd,
struct logical_volume *lv)
{
char format[NAME_LEN], layer_name[NAME_LEN];
int i;
/*
* We would like to give the same number for this layer
* and the newly added mimage.
* However, LV name of newly added mimage is determined *after*
* the LV name of this layer is determined.
*
* So, use generate_lv_name() to generate mimage name first
* and take the number from it.
*/
if (dm_snprintf(format, sizeof(format), "%s_mimage_%%d", lv->name) < 0) {
log_error("lvconvert: layer name creation failed.");
return 0;
}
if (!generate_lv_name(lv->vg, format, layer_name, sizeof(layer_name)) ||
sscanf(layer_name, format, &i) != 1) {
log_error("lvconvert: layer name generation failed.");
return 0;
}
if (dm_snprintf(layer_name, sizeof(layer_name), MIRROR_SYNC_LAYER "_%d", i) < 0) {
log_error("layer name creation failed.");
return 0;
}
if (!insert_layer_for_lv(cmd, lv, 0, layer_name)) {
log_error("Failed to insert resync layer");
return 0;
}
return 1;
}
static int _failed_mirrors_count(struct logical_volume *lv)
{
struct lv_segment *lvseg;
int ret = 0;
unsigned s;
dm_list_iterate_items(lvseg, &lv->segments) {
if (!seg_is_mirrored(lvseg))
return -1;
for (s = 0; s < lvseg->area_count; s++) {
if (seg_type(lvseg, s) == AREA_LV) {
if (is_temporary_mirror_layer(seg_lv(lvseg, s)))
ret += _failed_mirrors_count(seg_lv(lvseg, s));
else if (lv_is_partial(seg_lv(lvseg, s)))
++ ret;
}
else if (seg_type(lvseg, s) == AREA_PV &&
is_missing_pv(seg_pv(lvseg, s)))
++ret;
}
}
return ret;
}
static int _failed_logs_count(struct logical_volume *lv)
{
int ret = 0;
unsigned s;
struct logical_volume *log_lv = first_seg(lv)->log_lv;
if (log_lv && lv_is_partial(log_lv)) {
if (lv_is_mirrored(log_lv))
ret += _failed_mirrors_count(log_lv);
else
ret += 1;
}
for (s = 0; s < first_seg(lv)->area_count; s++) {
if (seg_type(first_seg(lv), s) == AREA_LV &&
is_temporary_mirror_layer(seg_lv(first_seg(lv), s)))
ret += _failed_logs_count(seg_lv(first_seg(lv), s));
}
return ret;
}
static struct dm_list *_failed_pv_list(struct volume_group *vg)
{
struct dm_list *failed_pvs;
struct pv_list *pvl, *new_pvl;
if (!(failed_pvs = dm_pool_alloc(vg->vgmem, sizeof(*failed_pvs)))) {
log_error("Allocation of list of failed_pvs failed.");
return NULL;
}
dm_list_init(failed_pvs);
dm_list_iterate_items(pvl, &vg->pvs) {
if (!is_missing_pv(pvl->pv))
continue;
/*
* Finally, --repair will remove empty PVs.
* But we only want remove these which are output of repair,
* Do not count these which are already empty here.
* FIXME: code should traverse PV in LV not in whole VG.
* FIXME: layer violation? should it depend on vgreduce --removemising?
*/
if (pvl->pv->pe_alloc_count == 0)
continue;
if (!(new_pvl = dm_pool_zalloc(vg->vgmem, sizeof(*new_pvl)))) {
log_error("Allocation of failed_pvs list entry failed.");
return NULL;
}
new_pvl->pv = pvl->pv;
dm_list_add(failed_pvs, &new_pvl->list);
}
return failed_pvs;
}
static int _is_partial_lv(struct logical_volume *lv,
void *baton __attribute__((unused)))
{
return lv_is_partial(lv);
}
/*
* Walk down the stacked mirror LV to the original mirror LV.
*/
static struct logical_volume *_original_lv(struct logical_volume *lv)
{
struct logical_volume *next_lv = lv, *tmp_lv;
while ((tmp_lv = find_temporary_mirror(next_lv)))
next_lv = tmp_lv;
return next_lv;
}
static void _lvconvert_mirrors_repair_ask(struct cmd_context *cmd,
int failed_log, int failed_mirrors,
int *replace_log, int *replace_mirrors)
{
const char *leg_policy, *log_policy;
int force = arg_count(cmd, force_ARG);
int yes = arg_count(cmd, yes_ARG);
if (arg_is_set(cmd, usepolicies_ARG)) {
leg_policy = find_config_tree_str(cmd, activation_mirror_image_fault_policy_CFG, NULL);
log_policy = find_config_tree_str(cmd, activation_mirror_log_fault_policy_CFG, NULL);
*replace_mirrors = strcmp(leg_policy, "remove");
*replace_log = strcmp(log_policy, "remove");
return;
}
if (force != PROMPT) {
*replace_log = *replace_mirrors = 0;
return;
}
*replace_log = *replace_mirrors = 1;
if (yes)
return;
if (failed_log &&
yes_no_prompt("Attempt to replace failed mirror log? [y/n]: ") == 'n')
*replace_log = 0;
if (failed_mirrors &&
yes_no_prompt("Attempt to replace failed mirror images "
"(requires full device resync)? [y/n]: ") == 'n')
*replace_mirrors = 0;
}
/*
* _get_log_count
* @lv: the mirror LV
*
* Get the number of on-disk copies of the log.
* 0 = 'core'
* 1 = 'disk'
* 2+ = 'mirrored'
*/
static uint32_t _get_log_count(struct logical_volume *lv)
{
struct logical_volume *log_lv;
log_lv = first_seg(_original_lv(lv))->log_lv;
if (log_lv)
return lv_mirror_count(log_lv);
return 0;
}
static int _lv_update_mirrored_log(struct logical_volume *lv,
struct dm_list *operable_pvs,
int log_count)
{
int old_log_count;
struct logical_volume *log_lv;
/*
* When log_count is 0, mirrored log doesn't need to be
* updated here but it will be removed later.
*/
if (!log_count)
return 1;
log_lv = first_seg(_original_lv(lv))->log_lv;
if (!log_lv || !lv_is_mirrored(log_lv))
return 1;
old_log_count = _get_log_count(lv);
if (old_log_count == log_count)
return 1;
/* Reducing redundancy of the log */
return remove_mirror_images(log_lv, log_count,
is_mirror_image_removable,
operable_pvs, 0U);
}
static int _lv_update_log_type(struct cmd_context *cmd,
struct lvconvert_params *lp,
struct logical_volume *lv,
struct dm_list *operable_pvs,
int log_count)
{
int old_log_count;
uint32_t region_size = (lp) ? lp->region_size :
first_seg(lv)->region_size;
alloc_policy_t alloc = (lp) ? lp->alloc : lv->alloc;
struct logical_volume *original_lv;
struct logical_volume *log_lv;
old_log_count = _get_log_count(lv);
if (old_log_count == log_count)
return 1;
original_lv = _original_lv(lv);
/* Remove an existing log completely */
if (!log_count) {
if (!remove_mirror_log(cmd, original_lv, operable_pvs,
arg_count(cmd, yes_ARG) ||
arg_count(cmd, force_ARG)))
return_0;
return 1;
}
log_lv = first_seg(original_lv)->log_lv;
/* Adding redundancy to the log */
if (old_log_count < log_count) {
if (!(region_size = adjusted_mirror_region_size(cmd, lv->vg->extent_size,
lv->le_count,
region_size, 0,
vg_is_clustered(lv->vg))))
return_0;
if (!add_mirror_log(cmd, original_lv, log_count,
region_size, operable_pvs, alloc))
return_0;
/*
* FIXME: This simple approach won't work in cluster mirrors,
* but it doesn't matter because we don't support
* mirrored logs in cluster mirrors.
*/
if (old_log_count &&
!lv_update_and_reload(log_lv))
return_0;
return 1;
}
/* Reducing redundancy of the log */
return remove_mirror_images(log_lv, log_count,
is_mirror_image_removable, operable_pvs, 1U);
}
/*
* Reomove missing and empty PVs from VG, if are also in provided list
*/
static void _remove_missing_empty_pv(struct volume_group *vg, struct dm_list *remove_pvs)
{
struct pv_list *pvl, *pvl_vg, *pvlt;
int removed = 0;
if (!remove_pvs)
return;
dm_list_iterate_items(pvl, remove_pvs) {
dm_list_iterate_items_safe(pvl_vg, pvlt, &vg->pvs) {
if (!id_equal(&pvl->pv->id, &pvl_vg->pv->id) ||
!is_missing_pv(pvl_vg->pv) ||
pvl_vg->pv->pe_alloc_count != 0)
continue;
/* FIXME: duplication of vgreduce code, move this to library */
vg->free_count -= pvl_vg->pv->pe_count;
vg->extent_count -= pvl_vg->pv->pe_count;
del_pvl_from_vgs(vg, pvl_vg);
free_pv_fid(pvl_vg->pv);
removed++;
}
}
if (removed) {
if (!vg_write(vg) || !vg_commit(vg)) {
stack;
return;
}
log_warn("WARNING: %d missing and now unallocated Physical Volumes removed from VG.", removed);
}
}
/*
* _lvconvert_mirrors_parse_params
*
* This function performs the following:
* 1) Gets the old values of mimage and log counts
* 2) Parses the CLI args to find the new desired values
* 3) Adjusts 'lp->mirrors' to the appropriate absolute value.
* (Remember, 'lp->mirrors' is specified in terms of the number of "copies"
* vs. the number of mimages. It can also be a relative value.)
* 4) Sets 'lp->need_polling' if collapsing
* 5) Validates other mirror params
*
* Returns: 1 on success, 0 on error
*/
static int _lvconvert_mirrors_parse_params(struct cmd_context *cmd,
struct logical_volume *lv,
struct lvconvert_params *lp,
uint32_t *old_mimage_count,
uint32_t *old_log_count,
uint32_t *new_mimage_count,
uint32_t *new_log_count)
{
*old_mimage_count = lv_mirror_count(lv);
*old_log_count = _get_log_count(lv);
if (lv->vg->lock_type && !strcmp(lv->vg->lock_type, "sanlock") && lp->keep_mimages) {
/* FIXME: we need to create a sanlock lock on disk for the new LV. */
log_error("Unable to split mirrors in VG with lock_type %s", lv->vg->lock_type);
return 0;
}
/*
* Adjusting mimage count?
*/
if (!lp->mirrors_supplied && !lp->keep_mimages)
lp->mirrors = *old_mimage_count;
else if (lp->mirrors_sign == SIGN_PLUS)
lp->mirrors = *old_mimage_count + lp->mirrors;
else if (lp->mirrors_sign == SIGN_MINUS)
lp->mirrors = (*old_mimage_count > lp->mirrors) ?
*old_mimage_count - lp->mirrors: 0;
else
lp->mirrors += 1;
*new_mimage_count = lp->mirrors;
/* Too many mimages? */
if (lp->mirrors > DEFAULT_MIRROR_MAX_IMAGES) {
log_error("Only up to %d images in mirror supported currently.",
DEFAULT_MIRROR_MAX_IMAGES);
return 0;
}
/* Did the user try to subtract more legs than available? */
if (lp->mirrors < 1) {
log_error("Unable to reduce images by specified amount - only %d in %s",
*old_mimage_count, lv->name);
return 0;
}
/*
* FIXME: It would be nice to say what we are adjusting to, but
* I really don't know whether to specify the # of copies or mimages.
*/
if (*old_mimage_count != *new_mimage_count)
log_verbose("Adjusting mirror image count of %s", lv->name);
/* If region size is not given by user - use value from mirror */
if (lv_is_mirrored(lv) && !lp->region_size_supplied) {
lp->region_size = first_seg(lv)->region_size;
log_debug("Copying region size %s from existing mirror.",
display_size(lv->vg->cmd, lp->region_size));
}
/*
* Adjust log type
*
* If we are converting from a mirror to another mirror or simply
* changing the log type, we start by assuming they want the log
* type the same and then parse the given args. OTOH, If we are
* converting from linear to mirror, then we start from the default
* position that the user would like a 'disk' log.
*/
*new_log_count = (*old_mimage_count > 1) ? *old_log_count : 1;
if (!lp->corelog && !lp->mirrorlog)
return 1;
*new_log_count = arg_int_value(cmd, mirrorlog_ARG, lp->corelog ? MIRROR_LOG_CORE : DEFAULT_MIRRORLOG);
log_verbose("Setting logging type to %s.", get_mirror_log_name(*new_log_count));
/*
* Region size must not change on existing mirrors
*/
if (arg_is_set(cmd, regionsize_ARG) && lv_is_mirrored(lv) &&
(lp->region_size != first_seg(lv)->region_size)) {
log_error("Mirror log region size cannot be changed on "
"an existing mirror.");
return 0;
}
/*
* For the most part, we cannot handle multi-segment mirrors. Bail out
* early if we have encountered one.
*/
if (lv_is_mirrored(lv) && dm_list_size(&lv->segments) != 1) {
log_error("Logical volume %s has multiple "
"mirror segments.", display_lvname(lv));
return 0;
}
return 1;
}
/*
* _lvconvert_mirrors_aux
*
* Add/remove mirror images and adjust log type. 'operable_pvs'
* are the set of PVs open to removal or allocation - depending
* on the operation being performed.
*/
static int _lvconvert_mirrors_aux(struct cmd_context *cmd,
struct logical_volume *lv,
struct lvconvert_params *lp,
struct dm_list *operable_pvs,
uint32_t new_mimage_count,
uint32_t new_log_count,
struct dm_list *pvh)
{
uint32_t region_size;
struct lv_segment *seg = first_seg(lv);
struct logical_volume *layer_lv;
uint32_t old_mimage_count = lv_mirror_count(lv);
uint32_t old_log_count = _get_log_count(lv);
if ((lp->mirrors == 1) && !lv_is_mirrored(lv)) {
log_warn("WARNING: Logical volume %s is already not mirrored.",
display_lvname(lv));
return 1;
}
if (!(region_size = adjusted_mirror_region_size(cmd, lv->vg->extent_size,
lv->le_count,
lp->region_size ? : seg->region_size, 0,
vg_is_clustered(lv->vg))))
return_0;
if (lv_component_is_active(lv)) {
log_error("Cannot convert logical volume %s with active component LV(s).",
display_lvname(lv));
return 0;
}
if (!operable_pvs)
operable_pvs = pvh;
/*
* Up-convert from linear to mirror
*/
if (!lv_is_mirrored(lv)) {
/* FIXME Share code with lvcreate */
/*
* FIXME should we give not only pvh, but also all PVs
* currently taken by the mirror? Would make more sense from
* user perspective.
*/
if (!lv_add_mirrors(cmd, lv, new_mimage_count - 1, lp->stripes,
lp->stripe_size, region_size, new_log_count, operable_pvs,
lp->alloc, MIRROR_BY_LV))
return_0;
if (!arg_is_set(cmd, background_ARG))
lp->need_polling = 1;
goto out;
}
/*
* Up-convert m-way mirror to n-way mirror
*/
if (new_mimage_count > old_mimage_count) {
if (lv_is_not_synced(lv)) {
log_error("Can't add mirror to out-of-sync mirrored "
"LV: use lvchange --resync first.");
return 0;
}
/*
* We allow snapshots of mirrors, but for now, we
* do not allow up converting mirrors that are under
* snapshots. The layering logic is somewhat complex,
* and preliminary test show that the conversion can't
* seem to get the correct %'age of completion.
*/
if (lv_is_origin(lv)) {
log_error("Can't add additional mirror images to "
"mirror %s which is under snapshots.",
display_lvname(lv));
return 0;
}
/*
* Is there already a convert in progress? We do not
* currently allow more than one.
*/
if (find_temporary_mirror(lv) || lv_is_converting(lv)) {
log_error("%s is already being converted. Unable to start another conversion.",
display_lvname(lv));
return 0;
}
/*
* Log addition/removal should be done before the layer
* insertion to make the end result consistent with
* linear-to-mirror conversion.
*/
if (!_lv_update_log_type(cmd, lp, lv,
operable_pvs, new_log_count))
return_0;
/* Insert a temporary layer for syncing,
* only if the original lv is using disk log. */
if (seg->log_lv && !_insert_lvconvert_layer(cmd, lv)) {
log_error("Failed to insert resync layer.");
return 0;
}
/* FIXME: can't have multiple mlogs. force corelog. */
if (!lv_add_mirrors(cmd, lv,
new_mimage_count - old_mimage_count,
lp->stripes, lp->stripe_size,
region_size, 0U, operable_pvs, lp->alloc,
MIRROR_BY_LV)) {
/* FIXME: failure inside library -> move error path processing into library. */
layer_lv = seg_lv(first_seg(lv), 0);
if (!remove_layer_from_lv(lv, layer_lv) ||
(lv_is_active(lv) && !deactivate_lv(cmd, layer_lv)) ||
!lv_remove(layer_lv) ||
!vg_write(lv->vg) || !vg_commit(lv->vg)) {
log_error("ABORTING: Failed to remove "
"temporary mirror layer %s.",
display_lvname(layer_lv));
log_error("Manual cleanup with vgcfgrestore "
"and dmsetup may be required.");
return 0;
}
return_0;
}
if (seg->log_lv)
lv->status |= CONVERTING;
lp->need_polling = 1;
goto out_skip_log_convert;
}
/*
* Down-convert (reduce # of mimages).
*/
if (new_mimage_count < old_mimage_count) {
uint32_t nmc = old_mimage_count - new_mimage_count;
uint32_t nlc = (!new_log_count || lp->mirrors == 1) ? 1U : 0U;
/* FIXME: Why did nlc used to be calculated that way? */
/* Reduce number of mirrors */
if (lp->keep_mimages) {
if (lp->track_changes) {
log_error("--trackchanges is not available "
"to 'mirror' segment type.");
return 0;
}
if (!lv_split_mirror_images(lv, lp->lv_split_name,
nmc, operable_pvs))
return_0;
} else if (!lv_remove_mirrors(cmd, lv, nmc, nlc,
is_mirror_image_removable, operable_pvs, 0))
return_0;
goto out; /* Just in case someone puts code between */
}
out:
/*
* Converting the log type
*/
if (lv_is_mirrored(lv) && (old_log_count != new_log_count)) {
if (!_lv_update_log_type(cmd, lp, lv,
operable_pvs, new_log_count))
return_0;
}
out_skip_log_convert:
if (!lv_update_and_reload(lv))
return_0;
return 1;
}
int mirror_remove_missing(struct cmd_context *cmd,
struct logical_volume *lv, int force)
{
struct dm_list *failed_pvs;
int log_count = _get_log_count(lv) - _failed_logs_count(lv);