-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmirror.c
2207 lines (1874 loc) · 58.2 KB
/
mirror.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) 2003-2004 Sistina Software, Inc. All rights reserved.
* Copyright (C) 2004-2008 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "lib.h"
#include "metadata.h"
#include "toolcontext.h"
#include "segtype.h"
#include "display.h"
#include "archiver.h"
#include "activate.h"
#include "lv_alloc.h"
#include "lvm-string.h"
#include "str_list.h"
#include "locking.h" /* FIXME Should not be used in this file */
#include "memlock.h"
#include "defaults.h" /* FIXME: should this be defaults.h? */
/* These are necessary for _write_log_header() */
#include "xlate.h"
#define MIRROR_MAGIC 0x4D695272
#define MIRROR_DISK_VERSION 2
/* These are the flags that represent the mirror failure restoration policies */
#define MIRROR_REMOVE 0
#define MIRROR_ALLOCATE 1
#define MIRROR_ALLOCATE_ANYWHERE 2
/*
* Returns true if the lv is temporary mirror layer for resync
*/
int is_temporary_mirror_layer(const struct logical_volume *lv)
{
if (lv->status & MIRROR_IMAGE
&& lv->status & MIRRORED
&& !(lv->status & LOCKED))
return 1;
return 0;
}
/*
* Return a temporary LV for resyncing added mirror image.
* Add other mirror legs to lvs list.
*/
struct logical_volume *find_temporary_mirror(const struct logical_volume *lv)
{
struct lv_segment *seg;
if (!(lv->status & MIRRORED))
return NULL;
seg = first_seg(lv);
/* Temporary mirror is always area_num == 0 */
if (seg_type(seg, 0) == AREA_LV &&
is_temporary_mirror_layer(seg_lv(seg, 0)))
return seg_lv(seg, 0);
return NULL;
}
/*
* cluster_mirror_is_available
*
* Check if the proper kernel module and log daemon are running.
* Caller should check for 'vg_is_clustered(lv->vg)' before making
* this call.
*
* Returns: 1 if available, 0 otherwise
*/
static int _cluster_mirror_is_available(struct logical_volume *lv)
{
unsigned attr = 0;
struct cmd_context *cmd = lv->vg->cmd;
const struct segment_type *segtype;
if (!(segtype = get_segtype_from_string(cmd, "mirror")))
return_0;
if (!segtype->ops->target_present)
return_0;
if (!segtype->ops->target_present(lv->vg->cmd, NULL, &attr))
return_0;
if (!(attr & MIRROR_LOG_CLUSTERED))
return 0;
return 1;
}
/*
* Returns the number of mirrors of the LV
*/
uint32_t lv_mirror_count(const struct logical_volume *lv)
{
struct lv_segment *seg;
uint32_t s, mirrors;
if (!(lv->status & MIRRORED))
return 1;
seg = first_seg(lv);
if (lv->status & PVMOVE)
return seg->area_count;
mirrors = 0;
for (s = 0; s < seg->area_count; s++) {
if (seg_type(seg, s) != AREA_LV)
continue;
if (is_temporary_mirror_layer(seg_lv(seg, s)))
mirrors += lv_mirror_count(seg_lv(seg, s));
else
mirrors++;
}
return mirrors ? mirrors : 1;
}
struct lv_segment *find_mirror_seg(struct lv_segment *seg)
{
struct lv_segment *mirror_seg;
mirror_seg = get_only_segment_using_this_lv(seg->lv);
if (!mirror_seg) {
log_error("Failed to find mirror_seg for %s", seg->lv->name);
return NULL;
}
if (!seg_is_mirrored(mirror_seg)) {
log_error("%s on %s is not a mirror segments",
mirror_seg->lv->name, seg->lv->name);
return NULL;
}
return mirror_seg;
}
/*
* Reduce the region size if necessary to ensure
* the volume size is a multiple of the region size.
*/
uint32_t adjusted_mirror_region_size(uint32_t extent_size, uint32_t extents,
uint32_t region_size)
{
uint64_t region_max;
region_max = (1 << (ffs((int)extents) - 1)) * (uint64_t) extent_size;
if (region_max < UINT32_MAX && region_size > region_max) {
region_size = (uint32_t) region_max;
log_print("Using reduced mirror region size of %" PRIu32
" sectors", region_size);
}
return region_size;
}
/*
* shift_mirror_images
* @mirrored_seg
* @mimage: The position (index) of the image to move to the end
*
* When dealing with removal of legs, we often move a 'removable leg'
* to the back of the 'areas' array. It is critically important not
* to simply swap it for the last area in the array. This would have
* the affect of reordering the remaining legs - altering position of
* the primary. So, we must shuffle all of the areas in the array
* to maintain their relative position before moving the 'removable
* leg' to the end.
*
* Short illustration of the problem:
* - Mirror consists of legs A, B, C and we want to remove A
* - We swap A and C and then remove A, leaving C, B
* This scenario is problematic in failure cases where A dies, because
* B becomes the primary. If the above happens, we effectively throw
* away any changes made between the time of failure and the time of
* restructuring the mirror.
*
* So, any time we want to move areas to the end to be removed, use
* this function.
*/
int shift_mirror_images(struct lv_segment *mirrored_seg, unsigned mimage)
{
unsigned i;
struct lv_segment_area area;
if (mimage >= mirrored_seg->area_count) {
log_error("Invalid index (%u) of mirror image supplied "
"to shift_mirror_images()", mimage);
return 0;
}
area = mirrored_seg->areas[mimage];
/* Shift remaining images down to fill the hole */
for (i = mimage + 1; i < mirrored_seg->area_count; i++)
mirrored_seg->areas[i-1] = mirrored_seg->areas[i];
/* Place this one at the end */
mirrored_seg->areas[i-1] = area;
return 1;
}
/*
* This function writes a new header to the mirror log header to the lv
*
* Returns: 1 on success, 0 on failure
*/
static int _write_log_header(struct cmd_context *cmd, struct logical_volume *lv)
{
struct device *dev;
char *name;
struct { /* The mirror log header */
uint32_t magic;
uint32_t version;
uint64_t nr_regions;
} log_header;
log_header.magic = xlate32(MIRROR_MAGIC);
log_header.version = xlate32(MIRROR_DISK_VERSION);
log_header.nr_regions = xlate64((uint64_t)-1);
if (!(name = dm_pool_alloc(cmd->mem, PATH_MAX))) {
log_error("Name allocation failed - log header not written (%s)",
lv->name);
return 0;
}
if (dm_snprintf(name, PATH_MAX, "%s%s/%s", cmd->dev_dir,
lv->vg->name, lv->name) < 0) {
log_error("Name too long - log header not written (%s)", lv->name);
return 0;
}
log_verbose("Writing log header to device, %s", lv->name);
if (!(dev = dev_cache_get(name, NULL))) {
log_error("%s: not found: log header not written", name);
return 0;
}
if (!dev_open_quiet(dev))
return 0;
if (!dev_write(dev, UINT64_C(0), sizeof(log_header), &log_header)) {
log_error("Failed to write log header to %s", name);
dev_close_immediate(dev);
return 0;
}
dev_close_immediate(dev);
return 1;
}
/*
* Initialize mirror log contents
*/
static int _init_mirror_log(struct cmd_context *cmd,
struct logical_volume *log_lv, int in_sync,
struct dm_list *tags, int remove_on_failure)
{
struct str_list *sl;
struct lvinfo info;
uint64_t orig_status = log_lv->status;
int was_active = 0;
if (test_mode()) {
log_verbose("Test mode: Skipping mirror log initialisation.");
return 1;
}
if (!activation() && in_sync) {
log_error("Aborting. Unable to create in-sync mirror log "
"while activation is disabled.");
return 0;
}
/* If the LV is active, deactivate it first. */
if (lv_info(cmd, log_lv, 0, &info, 0, 0) && info.exists) {
(void)deactivate_lv(cmd, log_lv);
/*
* FIXME: workaround to fail early
* Ensure that log is really deactivated because deactivate_lv
* on cluster do not fail if there is log_lv with different UUID.
*/
if (lv_info(cmd, log_lv, 0, &info, 0, 0) && info.exists) {
log_error("Aborting. Unable to deactivate mirror log.");
goto revert_new_lv;
}
was_active = 1;
}
/* Temporary make it visible for set_lv() */
lv_set_visible(log_lv);
/* Temporary tag mirror log for activation */
dm_list_iterate_items(sl, tags)
if (!str_list_add(cmd->mem, &log_lv->tags, sl->str)) {
log_error("Aborting. Unable to tag mirror log.");
goto activate_lv;
}
/* store mirror log on disk(s) */
if (!vg_write(log_lv->vg) || !vg_commit(log_lv->vg))
goto activate_lv;
backup(log_lv->vg);
/* Wait for events following any deactivation before reactivating */
sync_local_dev_names(cmd);
if (!activate_lv(cmd, log_lv)) {
log_error("Aborting. Failed to activate mirror log.");
goto revert_new_lv;
}
/* Remove the temporary tags */
dm_list_iterate_items(sl, tags)
str_list_del(&log_lv->tags, sl->str);
if (activation() && !set_lv(cmd, log_lv, log_lv->size,
in_sync ? -1 : 0)) {
log_error("Aborting. Failed to wipe mirror log.");
goto deactivate_and_revert_new_lv;
}
if (activation() && !_write_log_header(cmd, log_lv)) {
log_error("Aborting. Failed to write mirror log header.");
goto deactivate_and_revert_new_lv;
}
if (!deactivate_lv(cmd, log_lv)) {
log_error("Aborting. Failed to deactivate mirror log. "
"Manual intervention required.");
return 0;
}
lv_set_hidden(log_lv);
if (was_active && !activate_lv(cmd, log_lv))
return_0;
return 1;
deactivate_and_revert_new_lv:
if (!deactivate_lv(cmd, log_lv)) {
log_error("Unable to deactivate mirror log LV. "
"Manual intervention required.");
return 0;
}
revert_new_lv:
log_lv->status = orig_status;
dm_list_iterate_items(sl, tags)
str_list_del(&log_lv->tags, sl->str);
if (remove_on_failure && !lv_remove(log_lv)) {
log_error("Manual intervention may be required to remove "
"abandoned log LV before retrying.");
return 0;
}
if (!vg_write(log_lv->vg) || !vg_commit(log_lv->vg))
log_error("Manual intervention may be required to "
"remove/restore abandoned log LV before retrying.");
else
backup(log_lv->vg);
activate_lv:
if (was_active && !remove_on_failure && !activate_lv(cmd, log_lv))
return_0;
return 0;
}
/*
* Activate an LV similarly (i.e. SH or EX) to a given "model" LV
*/
static int _activate_lv_like_model(struct logical_volume *model,
struct logical_volume *lv)
{
if (lv_is_active_exclusive(model)) {
if (!activate_lv_excl(lv->vg->cmd, lv))
return_0;
} else {
if (!activate_lv(lv->vg->cmd, lv))
return_0;
}
return 1;
}
/*
* Delete independent/orphan LV, it must acquire lock.
*/
static int _delete_lv(struct logical_volume *mirror_lv, struct logical_volume *lv)
{
struct cmd_context *cmd = mirror_lv->vg->cmd;
struct str_list *sl;
/* Inherit tags - maybe needed for activation */
if (!str_list_match_list(&mirror_lv->tags, &lv->tags, NULL)) {
dm_list_iterate_items(sl, &mirror_lv->tags)
if (!str_list_add(cmd->mem, &lv->tags, sl->str)) {
log_error("Aborting. Unable to tag.");
return 0;
}
if (!vg_write(mirror_lv->vg) ||
!vg_commit(mirror_lv->vg)) {
log_error("Intermediate VG commit for orphan volume failed.");
return 0;
}
}
// FIXME: shouldn't the activation type be based on mirror_lv, not lv?
if (!_activate_lv_like_model(lv, lv))
return_0;
/* FIXME Is this superfluous now? */
sync_local_dev_names(cmd);
if (!deactivate_lv(cmd, lv))
return_0;
if (!lv_remove(lv))
return_0;
return 1;
}
static int _merge_mirror_images(struct logical_volume *lv,
const struct dm_list *mimages)
{
uint32_t addition = dm_list_size(mimages);
struct logical_volume **img_lvs;
struct lv_list *lvl;
int i = 0;
if (!addition)
return 1;
if (!(img_lvs = alloca(sizeof(*img_lvs) * addition)))
return_0;
dm_list_iterate_items(lvl, mimages)
img_lvs[i++] = lvl->lv;
return lv_add_mirror_lvs(lv, img_lvs, addition,
MIRROR_IMAGE, first_seg(lv)->region_size);
}
/* Unlink the relationship between the segment and its log_lv */
struct logical_volume *detach_mirror_log(struct lv_segment *mirrored_seg)
{
struct logical_volume *log_lv;
if (!mirrored_seg->log_lv)
return NULL;
log_lv = mirrored_seg->log_lv;
mirrored_seg->log_lv = NULL;
lv_set_visible(log_lv);
log_lv->status &= ~MIRROR_LOG;
remove_seg_from_segs_using_this_lv(log_lv, mirrored_seg);
return log_lv;
}
/* Check if mirror image LV is removable with regard to given removable_pvs */
int is_mirror_image_removable(struct logical_volume *mimage_lv, void *baton)
{
struct physical_volume *pv;
struct lv_segment *seg;
int pv_found;
struct pv_list *pvl;
uint32_t s;
struct dm_list *removable_pvs = baton;
if (!baton || dm_list_empty(removable_pvs))
return 1;
dm_list_iterate_items(seg, &mimage_lv->segments) {
for (s = 0; s < seg->area_count; s++) {
if (seg_type(seg, s) != AREA_PV) {
/* FIXME Recurse for AREA_LV? */
/* Structure of seg_lv is unknown.
* Not removing this LV for safety. */
return 0;
}
pv = seg_pv(seg, s);
pv_found = 0;
dm_list_iterate_items(pvl, removable_pvs) {
if (id_equal(&pv->id, &pvl->pv->id)) {
pv_found = 1;
break;
}
if (pvl->pv->dev && pv->dev &&
pv->dev->dev == pvl->pv->dev->dev) {
pv_found = 1;
break;
}
}
if (!pv_found)
return 0;
}
}
return 1;
}
/*
* _move_removable_mimages_to_end
*
* We always detach mimage LVs from the end of the areas array.
* This function will push 'count' mimages to the end of the array
* based on if their PVs are removable.
*
* This is an all or nothing function. Either the user specifies
* enough removable PVs to satisfy count, or they don't specify
* any removable_pvs at all (in which case all PVs in the mirror
* are considered removable).
*/
static int _move_removable_mimages_to_end(struct logical_volume *lv,
uint32_t count,
struct dm_list *removable_pvs)
{
int i;
struct logical_volume *sub_lv;
struct lv_segment *mirrored_seg = first_seg(lv);
if (!removable_pvs)
return 1;
for (i = mirrored_seg->area_count - 1; (i >= 0) && count; i--) {
sub_lv = seg_lv(mirrored_seg, i);
if (!is_temporary_mirror_layer(sub_lv) &&
is_mirror_image_removable(sub_lv, removable_pvs)) {
if (!shift_mirror_images(mirrored_seg, i))
return_0;
count--;
}
}
return !count;
}
static int _mirrored_lv_in_sync(struct logical_volume *lv)
{
percent_t sync_percent;
if (!lv_mirror_percent(lv->vg->cmd, lv, 0, &sync_percent,
NULL)) {
if (lv_is_active_but_not_locally(lv))
log_error("Unable to determine mirror sync status of"
" remotely active LV, %s/%s",
lv->vg->name, lv->name);
else
log_error("Unable to determine mirror "
"sync status of %s/%s.",
lv->vg->name, lv->name);
return 0;
}
return (sync_percent == PERCENT_100) ? 1 : 0;
}
/*
* Split off 'split_count' legs from a mirror
*
* Returns: 0 on error, 1 on success
*/
static int _split_mirror_images(struct logical_volume *lv,
const char *split_name,
uint32_t split_count,
struct dm_list *removable_pvs)
{
uint32_t i;
struct logical_volume *sub_lv = NULL;
struct logical_volume *new_lv = NULL;
struct logical_volume *detached_log_lv = NULL;
struct lv_segment *mirrored_seg = first_seg(lv);
struct dm_list split_images;
struct lv_list *lvl;
struct cmd_context *cmd = lv->vg->cmd;
if (!(lv->status & MIRRORED)) {
log_error("Unable to split non-mirrored LV, %s",
lv->name);
return 0;
}
if (!split_count) {
log_error(INTERNAL_ERROR "split_count is zero!");
return 0;
}
log_verbose("Detaching %d images from mirror, %s",
split_count, lv->name);
if (!_move_removable_mimages_to_end(lv, split_count, removable_pvs)) {
/*
* FIXME: Allow incomplete specification of removable PVs?
*
* I am forcing the user to either specify no
* removable PVs or all of them. Should we allow
* them to just specify some - making us pick the rest?
*/
log_error("Insufficient removable PVs given"
" to satisfy request");
return 0;
}
/*
* Step 1:
* Remove the images from the mirror.
* Make them visible, independent LVs (don't change names yet).
* Track them in a list for later instantiation.
*/
dm_list_init(&split_images);
for (i = 0; i < split_count; i++) {
mirrored_seg->area_count--;
sub_lv = seg_lv(mirrored_seg, mirrored_seg->area_count);
sub_lv->status &= ~MIRROR_IMAGE;
release_lv_segment_area(mirrored_seg, mirrored_seg->area_count,
mirrored_seg->area_len);
log_very_verbose("%s assigned to be split", sub_lv->name);
if (!new_lv) {
lv_set_visible(sub_lv);
new_lv = sub_lv;
continue;
}
/* If there is more than one image being split, add to list */
lvl = dm_pool_alloc(lv->vg->vgmem, sizeof(*lvl));
if (!lvl) {
log_error("lv_list alloc failed");
return 0;
}
lvl->lv = sub_lv;
dm_list_add(&split_images, &lvl->list);
}
new_lv->name = dm_pool_strdup(lv->vg->vgmem, split_name);
if (!new_lv->name) {
log_error("Unable to rename newly split LV");
return 0;
}
if (!dm_list_empty(&split_images)) {
size_t len = strlen(new_lv->name) + 32;
char *layer_name, format[len];
/*
* A number of images have been split and
* a new mirror layer must be formed
*/
if (!insert_layer_for_lv(cmd, new_lv, 0, "_mimage_%d")) {
log_error("Failed to build new mirror, %s",
new_lv->name);
return 0;
}
first_seg(new_lv)->region_size = mirrored_seg->region_size;
dm_list_iterate_items(lvl, &split_images) {
sub_lv = lvl->lv;
if (dm_snprintf(format, len, "%s_mimage_%%d",
new_lv->name) < 0) {
log_error("Failed to build new image name.");
return 0;
}
layer_name = dm_pool_alloc(lv->vg->vgmem, len);
if (!layer_name) {
log_error("Unable to allocate memory");
return 0;
}
if (!generate_lv_name(lv->vg, format, layer_name, len)||
sscanf(layer_name, format, &i) != 1) {
log_error("Failed to generate new image names");
return 0;
}
sub_lv->name = layer_name;
}
if (!_merge_mirror_images(new_lv, &split_images)) {
log_error("Failed to group split "
"images into new mirror");
return 0;
}
/*
* We don't allow splitting a mirror that is not in-sync,
* so we can bring the newly split mirror up without a
* resync. (It will be a 'core' log mirror after all.)
*/
init_mirror_in_sync(1);
}
sub_lv = NULL;
/*
* If no more mirrors, remove mirror layer.
* The sub_lv is removed entirely later - leaving
* only the top-level (now linear) LV.
*/
if (mirrored_seg->area_count == 1) {
sub_lv = seg_lv(mirrored_seg, 0);
sub_lv->status &= ~MIRROR_IMAGE;
lv_set_visible(sub_lv);
detached_log_lv = detach_mirror_log(mirrored_seg);
if (!remove_layer_from_lv(lv, sub_lv))
return_0;
lv->status &= ~MIRRORED;
lv->status &= ~LV_NOTSYNCED;
}
if (!vg_write(mirrored_seg->lv->vg)) {
log_error("Intermediate VG metadata write failed.");
return 0;
}
/*
* Suspend the mirror - this includes all the sub-LVs and
* soon-to-be-split sub-LVs
*/
if (!suspend_lv(cmd, mirrored_seg->lv)) {
log_error("Failed to lock %s", mirrored_seg->lv->name);
vg_revert(mirrored_seg->lv->vg);
return 0;
}
if (!vg_commit(mirrored_seg->lv->vg)) {
resume_lv(cmd, mirrored_seg->lv);
return 0;
}
log_very_verbose("Updating \"%s\" in kernel", mirrored_seg->lv->name);
/*
* Resume the mirror - this also activates the visible, independent
* soon-to-be-split sub-LVs
*/
if (!resume_lv(cmd, mirrored_seg->lv)) {
log_error("Problem resuming %s", mirrored_seg->lv->name);
return 0;
}
/*
* Recycle newly split LV so it is properly renamed.
* Cluster requires the extra deactivate/activate calls.
*/
if (vg_is_clustered(lv->vg) &&
(!deactivate_lv(cmd, new_lv) ||
!_activate_lv_like_model(lv, new_lv))) {
log_error("Failed to rename newly split LV in the kernel");
return 0;
}
if (!suspend_lv(cmd, new_lv) || !resume_lv(cmd, new_lv)) {
log_error("Failed to rename newly split LV in the kernel");
return 0;
}
/* Remove original mirror layer if it has been converted to linear */
if (sub_lv && !_delete_lv(lv, sub_lv))
return_0;
/* Remove the log if it has been converted to linear */
if (detached_log_lv && !_delete_lv(lv, detached_log_lv))
return_0;
return 1;
}
/*
* Remove num_removed images from mirrored_seg
*
* Arguments:
* num_removed: the requested (maximum) number of mirrors to be removed
* removable_pvs: if not NULL and list not empty, only mirrors using PVs
* in this list will be removed
* remove_log: if non-zero, log_lv will be removed
* (even if it's 0, log_lv will be removed if there is no
* mirror remaining after the removal)
* collapse: if non-zero, instead of removing, remove the temporary
* mirror layer and merge mirrors to the original LV.
* removable_pvs should be NULL and num_removed should be
* seg->area_count - 1.
* removed: if non NULL, the number of removed mirror images is set
* as a result
*
* If collapse is non-zero, <removed> is guaranteed to be equal to num_removed.
*
* Return values:
* Failure (0) means something unexpected has happend and
* the caller should abort.
* Even if no mirror was removed (e.g. no LV matches to 'removable_pvs'),
* returns success (1).
*/
static int _remove_mirror_images(struct logical_volume *lv,
uint32_t num_removed,
int (*is_removable)(struct logical_volume *, void *),
void *removable_baton,
unsigned remove_log, unsigned collapse,
uint32_t *removed, int preferred_only)
{
uint32_t m;
int32_t s;
struct logical_volume *sub_lv;
struct logical_volume *detached_log_lv = NULL;
struct logical_volume *temp_layer_lv = NULL;
struct lv_segment *mirrored_seg = first_seg(lv);
uint32_t old_area_count = mirrored_seg->area_count;
uint32_t new_area_count = mirrored_seg->area_count;
struct lv_list *lvl;
struct dm_list tmp_orphan_lvs;
int orig_removed = num_removed;
if (removed)
*removed = 0;
log_very_verbose("Reducing mirror set %s from %" PRIu32 " to %"
PRIu32 " image(s)%s.", lv->name,
old_area_count, old_area_count - num_removed,
remove_log ? " and no log volume" : "");
if (collapse && (old_area_count - num_removed != 1)) {
log_error("Incompatible parameters to _remove_mirror_images");
return 0;
}
num_removed = 0;
/* Move removable_pvs to end of array */
for (s = mirrored_seg->area_count - 1;
s >= 0 && old_area_count - new_area_count < orig_removed;
s--) {
sub_lv = seg_lv(mirrored_seg, s);
if (!(is_temporary_mirror_layer(sub_lv) && lv_mirror_count(sub_lv) != 1) &&
is_removable(sub_lv, removable_baton)) {
/*
* Check if the user is trying to pull the
* primary mirror image when the mirror is
* not in-sync.
*/
if ((s == 0) && !_mirrored_lv_in_sync(lv) &&
!(lv->status & PARTIAL_LV)) {
log_error("Unable to remove primary mirror image while mirror is not in-sync");
return 0;
}
if (!shift_mirror_images(mirrored_seg, s))
return_0;
--new_area_count;
++num_removed;
}
}
if (!preferred_only)
num_removed = orig_removed;
/*
* If removable_pvs were specified, then they have been shifted
* to the end to ensure they are removed. The remaining balance
* of images left to remove will be taken from the unspecified.
*/
new_area_count = old_area_count - num_removed;
if (num_removed && old_area_count == new_area_count)
return 1;
/* Remove mimage LVs from the segment */
dm_list_init(&tmp_orphan_lvs);
for (m = new_area_count; m < mirrored_seg->area_count; m++) {
seg_lv(mirrored_seg, m)->status &= ~MIRROR_IMAGE;
lv_set_visible(seg_lv(mirrored_seg, m));
if (!(lvl = dm_pool_alloc(lv->vg->cmd->mem, sizeof(*lvl)))) {
log_error("lv_list alloc failed");
return 0;
}
lvl->lv = seg_lv(mirrored_seg, m);
dm_list_add(&tmp_orphan_lvs, &lvl->list);
release_lv_segment_area(mirrored_seg, m, mirrored_seg->area_len);
}
mirrored_seg->area_count = new_area_count;
/* If no more mirrors, remove mirror layer */
/* As an exceptional case, if the lv is temporary layer,
* leave the LV as mirrored and let the lvconvert completion
* to remove the layer. */
if (new_area_count == 1 && !is_temporary_mirror_layer(lv)) {
temp_layer_lv = seg_lv(mirrored_seg, 0);
temp_layer_lv->status &= ~MIRROR_IMAGE;
lv_set_visible(temp_layer_lv);
detached_log_lv = detach_mirror_log(mirrored_seg);
if (!remove_layer_from_lv(lv, temp_layer_lv))
return_0;
if (collapse && !_merge_mirror_images(lv, &tmp_orphan_lvs)) {
log_error("Failed to add mirror images");
return 0;
}
/*
* No longer a mirror? Even though new_area_count was 1,
* _merge_mirror_images may have resulted into lv being still a
* mirror. Fix up the flags if we only have one image left.
*/
if (lv_mirror_count(lv) == 1) {
lv->status &= ~MIRRORED;
lv->status &= ~LV_NOTSYNCED;
}
mirrored_seg = first_seg(lv);
if (remove_log && !detached_log_lv)
detached_log_lv = detach_mirror_log(mirrored_seg);
} else if (new_area_count == 0) {
log_very_verbose("All mimages of %s are gone", lv->name);
/* All mirror images are gone.
* It can happen for vgreduce --removemissing. */
detached_log_lv = detach_mirror_log(mirrored_seg);
lv->status &= ~MIRRORED;
lv->status &= ~LV_NOTSYNCED;
if (!replace_lv_with_error_segment(lv))
return_0;
} else if (remove_log)
detached_log_lv = detach_mirror_log(mirrored_seg);
/*
* The log may be removed due to repair. If the log
* happens to be a mirrored log, then there is a special
* case we need to consider. One of the images of a
* mirrored log can fail followed shortly afterwards by
* a failure of the second. This means that the top-level
* mirror is waiting for writes to the log to finish, but
* they never will unless the mirrored log can be repaired
* or replaced with an error target. Since both the devices
* have failed, we must replace with error target - it is
* the only way to release the pending writes.
*/
if (detached_log_lv && lv_is_mirrored(detached_log_lv) &&
(detached_log_lv->status & PARTIAL_LV)) {
struct lv_segment *seg = first_seg(detached_log_lv);
log_very_verbose("%s being removed due to failures",
detached_log_lv->name);
/*
* We are going to replace the mirror with an
* error segment, but before we do, we must remember
* all of the LVs that must be deleted later (i.e.
* the sub-lv's)
*/
for (m = 0; m < seg->area_count; m++) {
seg_lv(seg, m)->status &= ~MIRROR_IMAGE;
lv_set_visible(seg_lv(seg, m));
if (!(lvl = dm_pool_alloc(lv->vg->cmd->mem,
sizeof(*lvl)))) {
log_error("dm_pool_alloc failed");
return 0;
}
lvl->lv = seg_lv(seg, m);
dm_list_add(&tmp_orphan_lvs, &lvl->list);
}
if (!replace_lv_with_error_segment(detached_log_lv)) {
log_error("Failed error target substitution for %s",
detached_log_lv->name);
return 0;
}
if (!vg_write(detached_log_lv->vg)) {
log_error("intermediate VG write failed.");
return 0;
}