~brightbox/ubuntu/raring/lvm2/fix-for-1076304

« back to all changes in this revision

Viewing changes to lib/metadata/raid_manip.c

  • Committer: Package Import Robot
  • Author(s): Dmitrijs Ledkovs
  • Date: 2012-08-14 14:35:57 UTC
  • mfrom: (3.1.25 sid)
  • Revision ID: package-import@ubuntu.com-20120814143557-93aill2tp3kf3o30
Tags: 2.02.95-4ubuntu1
* Merge from Debian unstable, remaining changes:
  - debian/patches/avoid-dev-block.patch: Prefer any other device name over
    names in /dev/block/ since lvm.conf won't handle this.
  - debian/rules:
    - copy .po file to .pot file for Rosetta (Ubuntu specific).
  - debian/{dmsetup,lvm2}-udeb.install:
    - install initramfs and udev hooks in udebs (Debian bug 504341).
  - auto-start VGs as their PVs are discovered (Ubuntu specific):
    - add debian/tree/lvm2/lib/udev/rules.d/85-lvm2.rules: use watershed plus
      the sledgehammer of vgscan/vgchange to turn on VGs as they come online.
    - debian/tree/lvm2/usr/share/initramfs-tools/scripts/hooks/lvm2:
      - add 85-lvm2.rules to the list of udev rules to copy.
      - depend on udev.
    - debian/control:
      - add versioned Depend on watershed in lvm2 for udev rules.
      - add Depends on watershed-udeb in lvm2-udeb for udev rules.
      - add versioned Depend/Breaks on udev in dmsetup for udev rules.
      - add Depend on initramfs-tools in dmsetup so system is not potentially
        rendered unbootable by out-of-order dpkg configuration.
    - debian/rules:
      - do not install local-top scripts since Ubuntu mounts root using udev.
      - do not install init scripts for lvm2, since udev starts LVM.
    - debian/lvm2.postinst: handle missing lvm2 init script.
    - debian/tree/dmsetup/lib/udev/rules.d/60-persistent-storage-dm.rules:
      watch dm devices for changes with inotify
  - add mountroot failure hooks to help fix bad boots (Debian bug 468115):
    - debian/tree/lvm2/usr/share/initramfs-tools/scripts/init-premount/lvm2
  - remaining changes to upstream event manager packages (Debian bug 514706):
    - debian/rules:
      - enable dmeventd during configure.
    - debian/dmeventd.{8,manpages}: install dmeventd files.
  - rename debian/clvm.defaults to debian/clvm.default so it is installed
    correctly.
  - debian/control: add dmsetup-udeb to libdevmapper1.02.1-udeb recommends.
  - debian/rules: make sure dmsetup and lvm2 initramfs-tools scripts are
    executable.  When the Ubuntu-specific ones are added with a patch,
    they may lose their executable bit.
  - Add and install clvmd resource agent
  - Add dependency on libudev-dev to libdevmapper-dev so that the .pc file
    works.
  - debian/{clvmd.ra,clvm.init}:
    - create /run/lvm if it doesn't exist.
  - debian/clvm.init:
    - exit 3 if not running on status action.
  - Call dh_installman so that our dmeventd manpage actually gets installed
  - Install the missing fsadm manpage.

 * libdevmapper-dev:
  - move .so symlinks and pkgconfig files to multiarched locations.
  - mark libdevmapper-dev M-A: same

 * libdevmapper-event1.02.1:
  - Add Breaks: dmeventd (<< 2.02.95-4ubuntu1) due to debian symbol rename

 * debian/lvm2.{preinst,postinst,postrm}:
  - Implement removal of obsolete /etc/init.d/lvm2 conffile, which
    should not have been re-introduced in Quantal.

 * Dropped Changes, included in Debian:
  - Mostly included packages for upstream event manager (Debian bug 514706).
  - debian/patches/rules-subdir.patch: removed as reordering will cause
    build failure with dmeventd.
  - debian/patches/libdm-event-static.patch: removed as other static libs
    aren't being built anymore either.
  - Update symbols for libdevmapper-event.
  - Update libdevmapper-event, dmeventd descriptions to match Debian
    boilerplate.

 * Disappeared Changes:
  - Don't install documentation in udebs. No diff found, but no docs are
    installed into udebs either.

 * Resurected Changes:
  - corrected dropping the wrong init script. Now clvm.init is shipped
    and lvm2.init is dropped in favor of udev rules as per original
    intention (LP: #1037033).

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
#include "str_list.h"
25
25
#include "memlock.h"
26
26
 
 
27
#define RAID_REGION_SIZE 1024
 
28
 
 
29
static int _lv_is_raid_with_tracking(const struct logical_volume *lv,
 
30
                                     struct logical_volume **tracking)
 
31
{
 
32
        uint32_t s;
 
33
        struct lv_segment *seg;
 
34
 
 
35
        *tracking = NULL;
 
36
        seg = first_seg(lv);
 
37
 
 
38
        if (!(lv->status & RAID))
 
39
                return 0;
 
40
 
 
41
        for (s = 0; s < seg->area_count; s++)
 
42
                if (lv_is_visible(seg_lv(seg, s)) &&
 
43
                    !(seg_lv(seg, s)->status & LVM_WRITE))
 
44
                        *tracking = seg_lv(seg, s);
 
45
 
 
46
 
 
47
        return *tracking ? 1 : 0;
 
48
}
 
49
 
 
50
int lv_is_raid_with_tracking(const struct logical_volume *lv)
 
51
{
 
52
        struct logical_volume *tracking;
 
53
 
 
54
        return _lv_is_raid_with_tracking(lv, &tracking);
 
55
}
 
56
 
27
57
uint32_t lv_raid_image_count(const struct logical_volume *lv)
28
58
{
29
59
        struct lv_segment *seg = first_seg(lv);
125
155
        return 0;
126
156
}
127
157
 
 
158
static int _get_pv_list_for_lv(struct logical_volume *lv, struct dm_list *pvs)
 
159
{
 
160
        uint32_t s;
 
161
        struct pv_list *pvl;
 
162
        struct lv_segment *seg = first_seg(lv);
 
163
 
 
164
        if (!seg_is_linear(seg)) {
 
165
                log_error(INTERNAL_ERROR
 
166
                          "_get_pv_list_for_lv only handles linear volumes");
 
167
                return 0;
 
168
        }
 
169
 
 
170
        log_debug("Getting list of PVs that %s/%s is on:",
 
171
                  lv->vg->name, lv->name);
 
172
 
 
173
        dm_list_iterate_items(seg, &lv->segments) {
 
174
                for (s = 0; s < seg->area_count; s++) {
 
175
                        if (seg_type(seg, s) != AREA_PV) {
 
176
                                log_error(INTERNAL_ERROR
 
177
                                          "Linear seg_type should be AREA_PV");
 
178
                                return 0;
 
179
                        }
 
180
 
 
181
                        if (!(pvl = dm_pool_zalloc(lv->vg->cmd->mem,
 
182
                                                   sizeof(*pvl)))) {
 
183
                                log_error("Failed to allocate memory");
 
184
                                return 0;
 
185
                        }
 
186
 
 
187
                        pvl->pv = seg_pv(seg, s);
 
188
                        log_debug("  %s/%s is on %s", lv->vg->name, lv->name,
 
189
                                  pv_dev_name(pvl->pv));
 
190
                        dm_list_add(pvs, &pvl->list);
 
191
                }
 
192
        }
 
193
 
 
194
        return 1;
 
195
}
 
196
 
 
197
/*
 
198
 * _raid_in_sync
 
199
 * @lv
 
200
 *
 
201
 * _raid_in_sync works for all types of RAID segtypes, as well
 
202
 * as 'mirror' segtype.  (This is because 'lv_raid_percent' is
 
203
 * simply a wrapper around 'lv_mirror_percent'.
 
204
 *
 
205
 * Returns: 1 if in-sync, 0 otherwise.
 
206
 */
128
207
static int _raid_in_sync(struct logical_volume *lv)
129
208
{
130
209
        percent_t sync_percent;
358
437
 * This function does not make metadata changes.
359
438
 */
360
439
static int _alloc_image_component(struct logical_volume *lv,
 
440
                                  const char *alt_base_name,
361
441
                                  struct alloc_handle *ah, uint32_t first_area,
362
 
                                  uint32_t type, struct logical_volume **new_lv)
 
442
                                  uint64_t type, struct logical_volume **new_lv)
363
443
{
364
444
        uint64_t status;
365
445
        size_t len = strlen(lv->name) + 32;
366
446
        char img_name[len];
 
447
        const char *base_name = (alt_base_name) ? alt_base_name : lv->name;
367
448
        struct logical_volume *tmp_lv;
368
449
        const struct segment_type *segtype;
369
450
 
370
451
        if (type == RAID_META) {
371
 
                if (dm_snprintf(img_name, len, "%s_rmeta_%%d", lv->name) < 0)
 
452
                if (dm_snprintf(img_name, len, "%s_rmeta_%%d", base_name) < 0)
372
453
                        return_0;
373
454
        } else if (type == RAID_IMAGE) {
374
 
                if (dm_snprintf(img_name, len, "%s_rimage_%%d", lv->name) < 0)
 
455
                if (dm_snprintf(img_name, len, "%s_rimage_%%d", base_name) < 0)
375
456
                        return_0;
376
457
        } else {
377
458
                log_error(INTERNAL_ERROR
387
468
                return 0;
388
469
        }
389
470
 
390
 
        status = LVM_READ | LVM_WRITE | LV_NOTSYNCED | type;
 
471
        status = LVM_READ | LVM_WRITE | LV_REBUILD | type;
391
472
        tmp_lv = lv_create_empty(img_name, NULL, status, ALLOC_INHERIT, lv->vg);
392
473
        if (!tmp_lv) {
393
474
                log_error("Failed to allocate new raid component, %s", img_name);
411
492
                                   struct dm_list *new_data_lvs)
412
493
{
413
494
        uint32_t s;
 
495
        uint32_t region_size;
 
496
        uint32_t extents;
414
497
        struct lv_segment *seg = first_seg(lv);
 
498
        const struct segment_type *segtype;
415
499
        struct alloc_handle *ah;
416
500
        struct dm_list *parallel_areas;
417
501
        struct logical_volume *tmp_lv;
425
509
        if (!(parallel_areas = build_parallel_areas_from_lv(lv, 0)))
426
510
                return_0;
427
511
 
428
 
        if (!(ah = allocate_extents(lv->vg, NULL, seg->segtype, 0, count, count,
429
 
                                    seg->region_size, lv->le_count, pvs,
 
512
        if (seg_is_linear(seg))
 
513
                region_size = RAID_REGION_SIZE;
 
514
        else
 
515
                region_size = seg->region_size;
 
516
 
 
517
        if (seg_is_raid(seg))
 
518
                segtype = seg->segtype;
 
519
        else if (!(segtype = get_segtype_from_string(lv->vg->cmd, "raid1")))
 
520
                return_0;
 
521
 
 
522
        /*
 
523
         * The number of extents is based on the RAID type.  For RAID1,
 
524
         * each of the rimages is the same size - 'le_count'.  However
 
525
         * for RAID 4/5/6, the stripes add together (NOT including the parity
 
526
         * devices) to equal 'le_count'.  Thus, when we are allocating
 
527
         * individual devies, we must specify how large the individual device
 
528
         * is along with the number we want ('count').
 
529
         */
 
530
        extents = (segtype->parity_devs) ?
 
531
                (lv->le_count / (seg->area_count - segtype->parity_devs)) :
 
532
                lv->le_count;
 
533
 
 
534
        if (!(ah = allocate_extents(lv->vg, NULL, segtype, 0, count, count,
 
535
                                    region_size, extents, pvs,
430
536
                                    lv->alloc, parallel_areas)))
431
537
                return_0;
432
538
 
437
543
                 * allocated areas.  Thus, the metadata areas are pulled
438
544
                 * from 's + count'.
439
545
                 */
440
 
                if (!_alloc_image_component(lv, ah, s + count,
 
546
                if (!_alloc_image_component(lv, NULL, ah, s + count,
441
547
                                            RAID_META, &tmp_lv))
442
548
                        return_0;
443
549
                lvl_array[s + count].lv = tmp_lv;
444
550
                dm_list_add(new_meta_lvs, &(lvl_array[s + count].list));
445
551
 
446
 
                if (!_alloc_image_component(lv, ah, s, RAID_IMAGE, &tmp_lv))
 
552
                if (!_alloc_image_component(lv, NULL, ah, s,
 
553
                                            RAID_IMAGE, &tmp_lv))
447
554
                        return_0;
448
555
                lvl_array[s].lv = tmp_lv;
449
556
                dm_list_add(new_data_lvs, &(lvl_array[s].list));
452
559
        return 1;
453
560
}
454
561
 
 
562
/*
 
563
 * _alloc_rmeta_for_lv
 
564
 * @lv
 
565
 *
 
566
 * Allocate a RAID metadata device for the given LV (which is or will
 
567
 * be the associated RAID data device).  The new metadata device must
 
568
 * be allocated from the same PV(s) as the data device.
 
569
 */
 
570
static int _alloc_rmeta_for_lv(struct logical_volume *data_lv,
 
571
                               struct logical_volume **meta_lv)
 
572
{
 
573
        struct dm_list allocatable_pvs;
 
574
        struct alloc_handle *ah;
 
575
        struct lv_segment *seg = first_seg(data_lv);
 
576
        char *p, base_name[strlen(data_lv->name) + 1];
 
577
 
 
578
        dm_list_init(&allocatable_pvs);
 
579
 
 
580
        if (!seg_is_linear(seg)) {
 
581
                log_error(INTERNAL_ERROR "Unable to allocate RAID metadata "
 
582
                          "area for non-linear LV, %s", data_lv->name);
 
583
                return 0;
 
584
        }
 
585
 
 
586
        sprintf(base_name, "%s", data_lv->name);
 
587
        if ((p = strstr(base_name, "_mimage_")))
 
588
                *p = '\0';
 
589
 
 
590
        if (!_get_pv_list_for_lv(data_lv, &allocatable_pvs)) {
 
591
                log_error("Failed to build list of PVs for %s/%s",
 
592
                          data_lv->vg->name, data_lv->name);
 
593
                return 0;
 
594
        }
 
595
 
 
596
        if (!(ah = allocate_extents(data_lv->vg, NULL, seg->segtype, 0, 1, 0,
 
597
                                    seg->region_size,
 
598
                                    1 /*RAID_METADATA_AREA_LEN*/,
 
599
                                    &allocatable_pvs, data_lv->alloc, NULL)))
 
600
                return_0;
 
601
 
 
602
        if (!_alloc_image_component(data_lv, base_name, ah, 0,
 
603
                                    RAID_META, meta_lv))
 
604
                return_0;
 
605
 
 
606
        alloc_destroy(ah);
 
607
        return 1;
 
608
}
 
609
 
455
610
static int _raid_add_images(struct logical_volume *lv,
456
611
                            uint32_t new_count, struct dm_list *pvs)
457
612
{
 
613
        int rebuild_flag_cleared = 0;
458
614
        uint32_t s;
459
615
        uint32_t old_count = lv_raid_image_count(lv);
460
616
        uint32_t count = new_count - old_count;
 
617
        uint64_t status_mask = -1;
461
618
        struct cmd_context *cmd = lv->vg->cmd;
462
619
        struct lv_segment *seg = first_seg(lv);
463
620
        struct dm_list meta_lvs, data_lvs;
467
624
        dm_list_init(&meta_lvs); /* For image addition */
468
625
        dm_list_init(&data_lvs); /* For image addition */
469
626
 
470
 
        if (!seg_is_raid(seg)) {
 
627
        /*
 
628
         * If the segtype is linear, then we must allocate a metadata
 
629
         * LV to accompany it.
 
630
         */
 
631
        if (seg_is_linear(seg)) {
 
632
                /* A complete resync will be done, no need to mark each sub-lv */
 
633
                status_mask = ~(LV_REBUILD);
 
634
 
 
635
                if (!(lvl = dm_pool_alloc(lv->vg->vgmem, sizeof(*lvl)))) {
 
636
                        log_error("Memory allocation failed");
 
637
                        return 0;
 
638
                }
 
639
 
 
640
                if (!_alloc_rmeta_for_lv(lv, &lvl->lv))
 
641
                        return_0;
 
642
 
 
643
                dm_list_add(&meta_lvs, &lvl->list);
 
644
        } else if (!seg_is_raid(seg)) {
471
645
                log_error("Unable to add RAID images to %s of segment type %s",
472
646
                          lv->name, seg->segtype->name);
473
647
                return 0;
478
652
                return 0;
479
653
        }
480
654
 
 
655
        /*
 
656
         * If linear, we must correct data LV names.  They are off-by-one
 
657
         * because the linear volume hasn't taken its proper name of "_rimage_0"
 
658
         * yet.  This action must be done before '_clear_lvs' because it
 
659
         * commits the LVM metadata before clearing the LVs.
 
660
         */
 
661
        if (seg_is_linear(seg)) {
 
662
                char *name;
 
663
                size_t len;
 
664
                struct dm_list *l;
 
665
                struct lv_list *lvl_tmp;
 
666
 
 
667
                dm_list_iterate(l, &data_lvs) {
 
668
                        if (l == dm_list_last(&data_lvs)) {
 
669
                                lvl = dm_list_item(l, struct lv_list);
 
670
                                len = strlen(lv->name) + strlen("_rimage_XXX");
 
671
                                if (!(name = dm_pool_alloc(lv->vg->vgmem, len))) {
 
672
                                        log_error("Failed to allocate rimage name.");
 
673
                                        return 0;
 
674
                                }
 
675
                                sprintf(name, "%s_rimage_%u", lv->name, count);
 
676
                                lvl->lv->name = name;
 
677
                                continue;
 
678
                        }
 
679
                        lvl = dm_list_item(l, struct lv_list);
 
680
                        lvl_tmp = dm_list_item(l->n, struct lv_list);
 
681
                        lvl->lv->name = lvl_tmp->lv->name;
 
682
                }
 
683
        }
 
684
 
481
685
        /* Metadata LVs must be cleared before being added to the array */
482
686
        if (!_clear_lvs(&meta_lvs))
483
687
                goto fail;
484
688
 
 
689
        if (seg_is_linear(seg)) {
 
690
                first_seg(lv)->status |= RAID_IMAGE;
 
691
                if (!insert_layer_for_lv(lv->vg->cmd, lv,
 
692
                                         RAID | LVM_READ | LVM_WRITE,
 
693
                                         "_rimage_0"))
 
694
                        return_0;
 
695
 
 
696
                lv->status |= RAID;
 
697
                seg = first_seg(lv);
 
698
                seg_lv(seg, 0)->status |= RAID_IMAGE | LVM_READ | LVM_WRITE;
 
699
                seg->region_size = RAID_REGION_SIZE;
 
700
                seg->segtype = get_segtype_from_string(lv->vg->cmd, "raid1");
 
701
                if (!seg->segtype)
 
702
                        return_0;
 
703
        }
485
704
/*
486
705
FIXME: It would be proper to activate the new LVs here, instead of having
487
706
them activated by the suspend.  However, this causes residual device nodes
499
718
                goto fail;
500
719
        memcpy(new_areas, seg->areas, seg->area_count * sizeof(*seg->areas));
501
720
        seg->areas = new_areas;
502
 
        seg->area_count = new_count;
503
721
 
504
722
        /* Expand meta_areas array */
505
723
        if (!(new_areas = dm_pool_zalloc(lv->vg->cmd->mem,
506
724
                                         new_count * sizeof(*new_areas))))
507
725
                goto fail;
508
 
        memcpy(new_areas, seg->meta_areas,
509
 
               seg->area_count * sizeof(*seg->meta_areas));
 
726
        if (seg->meta_areas)
 
727
                memcpy(new_areas, seg->meta_areas,
 
728
                       seg->area_count * sizeof(*seg->meta_areas));
510
729
        seg->meta_areas = new_areas;
 
730
        seg->area_count = new_count;
 
731
 
 
732
        /* Add extra meta area when converting from linear */
 
733
        s = (old_count == 1) ? 0 : old_count;
511
734
 
512
735
        /* Set segment areas for metadata sub_lvs */
513
 
        s = old_count;
514
736
        dm_list_iterate_items(lvl, &meta_lvs) {
515
737
                log_debug("Adding %s to %s",
516
738
                          lvl->lv->name, lv->name);
 
739
                lvl->lv->status &= status_mask;
 
740
                first_seg(lvl->lv)->status &= status_mask;
517
741
                if (!set_lv_segment_area_lv(seg, s, lvl->lv, 0,
518
742
                                            lvl->lv->status)) {
519
743
                        log_error("Failed to add %s to %s",
523
747
                s++;
524
748
        }
525
749
 
 
750
        s = old_count;
 
751
 
526
752
        /* Set segment areas for data sub_lvs */
527
 
        s = old_count;
528
753
        dm_list_iterate_items(lvl, &data_lvs) {
529
754
                log_debug("Adding %s to %s",
530
755
                          lvl->lv->name, lv->name);
 
756
                lvl->lv->status &= status_mask;
 
757
                first_seg(lvl->lv)->status &= status_mask;
531
758
                if (!set_lv_segment_area_lv(seg, s, lvl->lv, 0,
532
759
                                            lvl->lv->status)) {
533
760
                        log_error("Failed to add %s to %s",
551
778
                return 0;
552
779
        }
553
780
 
554
 
        if (!suspend_lv(cmd, lv)) {
 
781
        if (!suspend_lv_origin(cmd, lv)) {
555
782
                log_error("Failed to suspend %s/%s before committing changes",
556
783
                          lv->vg->name, lv->name);
557
784
                return 0;
563
790
                return 0;
564
791
        }
565
792
 
566
 
        if (!resume_lv(cmd, lv)) {
 
793
        if (!resume_lv_origin(cmd, lv)) {
567
794
                log_error("Failed to resume %s/%s after committing changes",
568
795
                          lv->vg->name, lv->name);
569
796
                return 0;
570
797
        }
571
798
 
 
799
        /*
 
800
         * Now that the 'REBUILD' has made its way to the kernel, we must
 
801
         * remove the flag so that the individual devices are not rebuilt
 
802
         * upon every activation.
 
803
         */
 
804
        seg = first_seg(lv);
 
805
        for (s = 0; s < seg->area_count; s++) {
 
806
                if ((seg_lv(seg, s)->status & LV_REBUILD) ||
 
807
                    (seg_metalv(seg, s)->status & LV_REBUILD)) {
 
808
                        seg_metalv(seg, s)->status &= ~LV_REBUILD;
 
809
                        seg_lv(seg, s)->status &= ~LV_REBUILD;
 
810
                        rebuild_flag_cleared = 1;
 
811
                }
 
812
        }
 
813
        if (rebuild_flag_cleared &&
 
814
            (!vg_write(lv->vg) || !vg_commit(lv->vg))) {
 
815
                log_error("Failed to clear REBUILD flag for %s/%s components",
 
816
                          lv->vg->name, lv->name);
 
817
                return 0;
 
818
        }
 
819
 
572
820
        return 1;
573
821
 
574
822
fail:
810
1058
                               uint32_t new_count, struct dm_list *pvs)
811
1059
{
812
1060
        uint32_t old_count = lv_raid_image_count(lv);
813
 
        struct lv_segment *seg = first_seg(lv);
814
 
 
815
 
        if (!seg_is_mirrored(seg)) {
816
 
                log_error("Unable to change image count of non-mirrored RAID.");
817
 
                return 0;
818
 
        }
819
1061
 
820
1062
        if (old_count == new_count) {
821
1063
                log_error("%s/%s already has image count of %d",
837
1079
        struct dm_list removal_list, data_list;
838
1080
        struct cmd_context *cmd = lv->vg->cmd;
839
1081
        uint32_t old_count = lv_raid_image_count(lv);
 
1082
        struct logical_volume *tracking;
 
1083
        struct dm_list tracking_pvs;
840
1084
 
841
1085
        dm_list_init(&removal_list);
842
1086
        dm_list_init(&data_list);
865
1109
                return 0;
866
1110
        }
867
1111
 
 
1112
        /*
 
1113
         * We only allow a split while there is tracking if it is to
 
1114
         * complete the split of the tracking sub-LV
 
1115
         */
 
1116
        if (_lv_is_raid_with_tracking(lv, &tracking)) {
 
1117
                if (!_lv_is_on_pvs(tracking, splittable_pvs)) {
 
1118
                        log_error("Unable to split additional image from %s "
 
1119
                                  "while tracking changes for %s",
 
1120
                                  lv->name, tracking->name);
 
1121
                        return 0;
 
1122
                } else {
 
1123
                        /* Ensure we only split the tracking image */
 
1124
                        dm_list_init(&tracking_pvs);
 
1125
                        splittable_pvs = &tracking_pvs;
 
1126
                        if (!_get_pv_list_for_lv(tracking, splittable_pvs))
 
1127
                                return_0;
 
1128
                }
 
1129
        }
 
1130
 
868
1131
        if (!_raid_extract_images(lv, new_count, splittable_pvs, 1,
869
1132
                                 &removal_list, &data_list)) {
870
1133
                log_error("Failed to extract images from %s/%s",
967
1230
                return 0;
968
1231
        }
969
1232
 
 
1233
        /* Cannot track two split images at once */
 
1234
        if (lv_is_raid_with_tracking(lv)) {
 
1235
                log_error("Cannot track more than one split image at a time");
 
1236
                return 0;
 
1237
        }
 
1238
 
970
1239
        for (s = seg->area_count - 1; s >= 0; s--) {
971
1240
                if (!_lv_is_on_pvs(seg_lv(seg, s), splittable_pvs))
972
1241
                        continue;
1102
1371
                  vg->name, lv->name);
1103
1372
        return 1;
1104
1373
}
 
1374
 
 
1375
static int _convert_mirror_to_raid1(struct logical_volume *lv,
 
1376
                                    const struct segment_type *new_segtype)
 
1377
{
 
1378
        uint32_t s;
 
1379
        struct lv_segment *seg = first_seg(lv);
 
1380
        struct lv_list lvl_array[seg->area_count], *lvl;
 
1381
        struct dm_list meta_lvs;
 
1382
        struct lv_segment_area *meta_areas;
 
1383
 
 
1384
        dm_list_init(&meta_lvs);
 
1385
 
 
1386
        if (!_raid_in_sync(lv)) {
 
1387
                log_error("Unable to convert %s/%s while it is not in-sync",
 
1388
                          lv->vg->name, lv->name);
 
1389
                return 0;
 
1390
        }
 
1391
 
 
1392
        meta_areas = dm_pool_zalloc(lv->vg->vgmem,
 
1393
                                    lv_mirror_count(lv) * sizeof(*meta_areas));
 
1394
        if (!meta_areas) {
 
1395
                log_error("Failed to allocate memory");
 
1396
                return 0;
 
1397
        }
 
1398
 
 
1399
        for (s = 0; s < seg->area_count; s++) {
 
1400
                log_debug("Allocating new metadata LV for %s",
 
1401
                          seg_lv(seg, s)->name);
 
1402
                if (!_alloc_rmeta_for_lv(seg_lv(seg, s), &(lvl_array[s].lv))) {
 
1403
                        log_error("Failed to allocate metadata LV for %s in %s",
 
1404
                                  seg_lv(seg, s)->name, lv->name);
 
1405
                        return 0;
 
1406
                }
 
1407
                dm_list_add(&meta_lvs, &(lvl_array[s].list));
 
1408
        }
 
1409
 
 
1410
        log_debug("Clearing newly allocated metadata LVs");
 
1411
        if (!_clear_lvs(&meta_lvs)) {
 
1412
                log_error("Failed to initialize metadata LVs");
 
1413
                return 0;
 
1414
        }
 
1415
 
 
1416
        if (seg->log_lv) {
 
1417
                log_debug("Removing mirror log, %s", seg->log_lv->name);
 
1418
                if (!remove_mirror_log(lv->vg->cmd, lv, NULL, 0)) {
 
1419
                        log_error("Failed to remove mirror log");
 
1420
                        return 0;
 
1421
                }
 
1422
        }
 
1423
 
 
1424
        seg->meta_areas = meta_areas;
 
1425
        s = 0;
 
1426
 
 
1427
        dm_list_iterate_items(lvl, &meta_lvs) {
 
1428
                log_debug("Adding %s to %s", lvl->lv->name, lv->name);
 
1429
 
 
1430
                /* Images are known to be in-sync */
 
1431
                lvl->lv->status &= ~LV_REBUILD;
 
1432
                first_seg(lvl->lv)->status &= ~LV_REBUILD;
 
1433
                lv_set_hidden(lvl->lv);
 
1434
 
 
1435
                if (!set_lv_segment_area_lv(seg, s, lvl->lv, 0,
 
1436
                                            lvl->lv->status)) {
 
1437
                        log_error("Failed to add %s to %s",
 
1438
                                  lvl->lv->name, lv->name);
 
1439
                        return 0;
 
1440
                }
 
1441
                s++;
 
1442
        }
 
1443
 
 
1444
        for (s = 0; s < seg->area_count; s++) {
 
1445
                char *new_name;
 
1446
 
 
1447
                new_name = dm_pool_zalloc(lv->vg->vgmem,
 
1448
                                          strlen(lv->name) +
 
1449
                                          strlen("_rimage_XXn"));
 
1450
                if (!new_name) {
 
1451
                        log_error("Failed to rename mirror images");
 
1452
                        return 0;
 
1453
                }
 
1454
 
 
1455
                sprintf(new_name, "%s_rimage_%u", lv->name, s);
 
1456
                log_debug("Renaming %s to %s", seg_lv(seg, s)->name, new_name);
 
1457
                seg_lv(seg, s)->name = new_name;
 
1458
                seg_lv(seg, s)->status &= ~MIRROR_IMAGE;
 
1459
                seg_lv(seg, s)->status |= RAID_IMAGE;
 
1460
        }
 
1461
        init_mirror_in_sync(1);
 
1462
 
 
1463
        log_debug("Setting new segtype for %s", lv->name);
 
1464
        seg->segtype = new_segtype;
 
1465
        lv->status &= ~MIRRORED;
 
1466
        lv->status |= RAID;
 
1467
        seg->status |= RAID;
 
1468
 
 
1469
        if (!vg_write(lv->vg)) {
 
1470
                log_error("Failed to write changes to %s in %s",
 
1471
                          lv->name, lv->vg->name);
 
1472
                return 0;
 
1473
        }
 
1474
 
 
1475
        if (!suspend_lv(lv->vg->cmd, lv)) {
 
1476
                log_error("Failed to suspend %s/%s before committing changes",
 
1477
                          lv->vg->name, lv->name);
 
1478
                return 0;
 
1479
        }
 
1480
 
 
1481
        if (!vg_commit(lv->vg)) {
 
1482
                log_error("Failed to commit changes to %s in %s",
 
1483
                          lv->name, lv->vg->name);
 
1484
                return 0;
 
1485
        }
 
1486
 
 
1487
        if (!resume_lv(lv->vg->cmd, lv)) {
 
1488
                log_error("Failed to resume %s/%s after committing changes",
 
1489
                          lv->vg->name, lv->name);
 
1490
                return 0;
 
1491
        }
 
1492
 
 
1493
        return 1;
 
1494
}
 
1495
 
 
1496
/*
 
1497
 * lv_raid_reshape
 
1498
 * @lv
 
1499
 * @new_segtype
 
1500
 *
 
1501
 * Convert an LV from one RAID type (or 'mirror' segtype) to another.
 
1502
 *
 
1503
 * Returns: 1 on success, 0 on failure
 
1504
 */
 
1505
int lv_raid_reshape(struct logical_volume *lv,
 
1506
                    const struct segment_type *new_segtype)
 
1507
{
 
1508
        struct lv_segment *seg = first_seg(lv);
 
1509
 
 
1510
        if (!new_segtype) {
 
1511
                log_error(INTERNAL_ERROR "New segtype not specified");
 
1512
                return 0;
 
1513
        }
 
1514
 
 
1515
        if (!strcmp(seg->segtype->name, "mirror") &&
 
1516
            (!strcmp(new_segtype->name, "raid1")))
 
1517
            return _convert_mirror_to_raid1(lv, new_segtype);
 
1518
 
 
1519
        log_error("Converting the segment type for %s/%s from %s to %s"
 
1520
                  " is not yet supported.", lv->vg->name, lv->name,
 
1521
                  seg->segtype->name, new_segtype->name);
 
1522
        return 0;
 
1523
}
 
1524
 
 
1525
/*
 
1526
 * lv_raid_replace
 
1527
 * @lv
 
1528
 * @replace_pvs
 
1529
 * @allocatable_pvs
 
1530
 *
 
1531
 * Replace the specified PVs.
 
1532
 */
 
1533
int lv_raid_replace(struct logical_volume *lv,
 
1534
                    struct dm_list *remove_pvs,
 
1535
                    struct dm_list *allocate_pvs)
 
1536
{
 
1537
        uint32_t s, sd, match_count = 0;
 
1538
        struct dm_list old_meta_lvs, old_data_lvs;
 
1539
        struct dm_list new_meta_lvs, new_data_lvs;
 
1540
        struct lv_segment *raid_seg = first_seg(lv);
 
1541
        struct lv_list *lvl;
 
1542
        char *tmp_names[raid_seg->area_count * 2];
 
1543
 
 
1544
        dm_list_init(&old_meta_lvs);
 
1545
        dm_list_init(&old_data_lvs);
 
1546
        dm_list_init(&new_meta_lvs);
 
1547
        dm_list_init(&new_data_lvs);
 
1548
 
 
1549
        /*
 
1550
         * How many sub-LVs are being removed?
 
1551
         */
 
1552
        for (s = 0; s < raid_seg->area_count; s++) {
 
1553
                if ((seg_type(raid_seg, s) == AREA_UNASSIGNED) ||
 
1554
                    (seg_metatype(raid_seg, s) == AREA_UNASSIGNED)) {
 
1555
                        log_error("Unable to replace RAID images while the "
 
1556
                                  "array has unassigned areas");
 
1557
                        return 0;
 
1558
                }
 
1559
 
 
1560
                if (_lv_is_on_pvs(seg_lv(raid_seg, s), remove_pvs) ||
 
1561
                    _lv_is_on_pvs(seg_metalv(raid_seg, s), remove_pvs))
 
1562
                        match_count++;
 
1563
        }
 
1564
 
 
1565
        if (!match_count) {
 
1566
                log_verbose("%s/%s does not contain devices specified"
 
1567
                            " for replacement", lv->vg->name, lv->name);
 
1568
                return 1;
 
1569
        } else if (match_count == raid_seg->area_count) {
 
1570
                log_error("Unable to remove all PVs from %s/%s at once.",
 
1571
                          lv->vg->name, lv->name);
 
1572
                return 0;
 
1573
        } else if (raid_seg->segtype->parity_devs &&
 
1574
                   (match_count > raid_seg->segtype->parity_devs)) {
 
1575
                log_error("Unable to replace more than %u PVs from (%s) %s/%s",
 
1576
                          raid_seg->segtype->parity_devs,
 
1577
                          raid_seg->segtype->name, lv->vg->name, lv->name);
 
1578
                return 0;
 
1579
        }
 
1580
 
 
1581
        /*
 
1582
         * Allocate the new image components first
 
1583
         * - This makes it easy to avoid all currently used devs
 
1584
         * - We can immediately tell if there is enough space
 
1585
         *
 
1586
         * - We need to change the LV names when we insert them.
 
1587
         */
 
1588
        if (!_alloc_image_components(lv, allocate_pvs, match_count,
 
1589
                                     &new_meta_lvs, &new_data_lvs)) {
 
1590
                log_error("Failed to allocate replacement images for %s/%s",
 
1591
                          lv->vg->name, lv->name);
 
1592
                return 0;
 
1593
        }
 
1594
 
 
1595
        /*
 
1596
         * Remove the old images
 
1597
         * - If we did this before the allocate, we wouldn't have to rename
 
1598
         *   the allocated images, but it'd be much harder to avoid the right
 
1599
         *   PVs during allocation.
 
1600
         */
 
1601
        if (!_raid_extract_images(lv, raid_seg->area_count - match_count,
 
1602
                                  remove_pvs, 0,
 
1603
                                  &old_meta_lvs, &old_data_lvs)) {
 
1604
                log_error("Failed to remove the specified images from %s/%s",
 
1605
                          lv->vg->name, lv->name);
 
1606
                return 0;
 
1607
        }
 
1608
 
 
1609
        /*
 
1610
         * Skip metadata operation normally done to clear the metadata sub-LVs.
 
1611
         *
 
1612
         * The LV_REBUILD flag is set on the new sub-LVs,
 
1613
         * so they will be rebuilt and we don't need to clear the metadata dev.
 
1614
         */
 
1615
 
 
1616
        for (s = 0; s < raid_seg->area_count; s++) {
 
1617
                tmp_names[s] = NULL;
 
1618
                sd = s + raid_seg->area_count;
 
1619
                tmp_names[sd] = NULL;
 
1620
 
 
1621
                if ((seg_type(raid_seg, s) == AREA_UNASSIGNED) &&
 
1622
                    (seg_metatype(raid_seg, s) == AREA_UNASSIGNED)) {
 
1623
                        /* Adjust the new metadata LV name */
 
1624
                        lvl = dm_list_item(dm_list_first(&new_meta_lvs),
 
1625
                                           struct lv_list);
 
1626
                        dm_list_del(&lvl->list);
 
1627
                        tmp_names[s] = dm_pool_alloc(lv->vg->vgmem,
 
1628
                                                    strlen(lvl->lv->name) + 1);
 
1629
                        if (!tmp_names[s])
 
1630
                                return_0;
 
1631
                        if (dm_snprintf(tmp_names[s], strlen(lvl->lv->name) + 1,
 
1632
                                        "%s_rmeta_%u", lv->name, s) < 0)
 
1633
                                return_0;
 
1634
                        if (!set_lv_segment_area_lv(raid_seg, s, lvl->lv, 0,
 
1635
                                                    lvl->lv->status)) {
 
1636
                                log_error("Failed to add %s to %s",
 
1637
                                          lvl->lv->name, lv->name);
 
1638
                                return 0;
 
1639
                        }
 
1640
                        lv_set_hidden(lvl->lv);
 
1641
 
 
1642
                        /* Adjust the new data LV name */
 
1643
                        lvl = dm_list_item(dm_list_first(&new_data_lvs),
 
1644
                                           struct lv_list);
 
1645
                        dm_list_del(&lvl->list);
 
1646
                        tmp_names[sd] = dm_pool_alloc(lv->vg->vgmem,
 
1647
                                                     strlen(lvl->lv->name) + 1);
 
1648
                        if (!tmp_names[sd])
 
1649
                                return_0;
 
1650
                        if (dm_snprintf(tmp_names[sd], strlen(lvl->lv->name) + 1,
 
1651
                                        "%s_rimage_%u", lv->name, s) < 0)
 
1652
                                return_0;
 
1653
                        if (!set_lv_segment_area_lv(raid_seg, s, lvl->lv, 0,
 
1654
                                                    lvl->lv->status)) {
 
1655
                                log_error("Failed to add %s to %s",
 
1656
                                          lvl->lv->name, lv->name);
 
1657
                                return 0;
 
1658
                        }
 
1659
                        lv_set_hidden(lvl->lv);
 
1660
                }
 
1661
        }
 
1662
 
 
1663
        if (!vg_write(lv->vg)) {
 
1664
                log_error("Failed to write changes to %s in %s",
 
1665
                          lv->name, lv->vg->name);
 
1666
                return 0;
 
1667
        }
 
1668
 
 
1669
        if (!suspend_lv(lv->vg->cmd, lv)) {
 
1670
                log_error("Failed to suspend %s/%s before committing changes",
 
1671
                          lv->vg->name, lv->name);
 
1672
                return 0;
 
1673
        }
 
1674
 
 
1675
        if (!vg_commit(lv->vg)) {
 
1676
                log_error("Failed to commit changes to %s in %s",
 
1677
                          lv->name, lv->vg->name);
 
1678
                return 0;
 
1679
        }
 
1680
 
 
1681
        if (!resume_lv(lv->vg->cmd, lv)) {
 
1682
                log_error("Failed to resume %s/%s after committing changes",
 
1683
                          lv->vg->name, lv->name);
 
1684
                return 0;
 
1685
        }
 
1686
 
 
1687
        dm_list_iterate_items(lvl, &old_meta_lvs) {
 
1688
                if (!deactivate_lv(lv->vg->cmd, lvl->lv))
 
1689
                        return_0;
 
1690
                if (!lv_remove(lvl->lv))
 
1691
                        return_0;
 
1692
        }
 
1693
        dm_list_iterate_items(lvl, &old_data_lvs) {
 
1694
                if (!deactivate_lv(lv->vg->cmd, lvl->lv))
 
1695
                        return_0;
 
1696
                if (!lv_remove(lvl->lv))
 
1697
                        return_0;
 
1698
        }
 
1699
 
 
1700
        /* Update new sub-LVs to correct name and clear REBUILD flag */
 
1701
        for (s = 0; s < raid_seg->area_count; s++) {
 
1702
                sd = s + raid_seg->area_count;
 
1703
                if (tmp_names[s] && tmp_names[sd]) {
 
1704
                        seg_metalv(raid_seg, s)->name = tmp_names[s];
 
1705
                        seg_lv(raid_seg, s)->name = tmp_names[sd];
 
1706
                        seg_metalv(raid_seg, s)->status &= ~LV_REBUILD;
 
1707
                        seg_lv(raid_seg, s)->status &= ~LV_REBUILD;
 
1708
                }
 
1709
        }
 
1710
 
 
1711
        if (!vg_write(lv->vg)) {
 
1712
                log_error("Failed to write changes to %s in %s",
 
1713
                          lv->name, lv->vg->name);
 
1714
                return 0;
 
1715
        }
 
1716
 
 
1717
        if (!suspend_lv(lv->vg->cmd, lv)) {
 
1718
                log_error("Failed to suspend %s/%s before committing changes",
 
1719
                          lv->vg->name, lv->name);
 
1720
                return 0;
 
1721
        }
 
1722
 
 
1723
        if (!vg_commit(lv->vg)) {
 
1724
                log_error("Failed to commit changes to %s in %s",
 
1725
                          lv->name, lv->vg->name);
 
1726
                return 0;
 
1727
        }
 
1728
 
 
1729
        if (!resume_lv(lv->vg->cmd, lv)) {
 
1730
                log_error("Failed to resume %s/%s after committing changes",
 
1731
                          lv->vg->name, lv->name);
 
1732
                return 0;
 
1733
        }
 
1734
 
 
1735
        return 1;
 
1736
}