~ubuntu-branches/debian/sid/lvm2/sid

« back to all changes in this revision

Viewing changes to lib/metadata/raid_manip.c

  • Committer: Package Import Robot
  • Author(s): Bastian Blank
  • Date: 2013-03-03 12:33:47 UTC
  • mfrom: (1.1.15)
  • Revision ID: package-import@ubuntu.com-20130303123347-smfwei6dodkdth55
Tags: 2.02.98-1
New upstream version.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
#include "toolcontext.h"
18
18
#include "segtype.h"
19
19
#include "display.h"
20
 
#include "archiver.h"
21
20
#include "activate.h"
22
21
#include "lv_alloc.h"
23
22
#include "lvm-string.h"
24
 
#include "str_list.h"
25
 
#include "memlock.h"
26
23
 
27
24
#define RAID_REGION_SIZE 1024
28
25
 
64
61
        return seg->area_count;
65
62
}
66
63
 
 
64
/*
 
65
 * Resume sub-LVs first, then top-level LV
 
66
 */
 
67
static int _bottom_up_resume(struct logical_volume *lv)
 
68
{
 
69
        uint32_t s;
 
70
        struct lv_segment *seg = first_seg(lv);
 
71
 
 
72
        if (seg_is_raid(seg) && (seg->area_count > 1)) {
 
73
                for (s = 0; s < seg->area_count; s++)
 
74
                        if (!resume_lv(lv->vg->cmd, seg_lv(seg, s)) ||
 
75
                            !resume_lv(lv->vg->cmd, seg_metalv(seg, s)))
 
76
                                return_0;
 
77
        }
 
78
 
 
79
        return resume_lv(lv->vg->cmd, lv);
 
80
}
 
81
 
67
82
static int _activate_sublv_preserving_excl(struct logical_volume *top_lv,
68
83
                                           struct logical_volume *sub_lv)
69
84
{
237
252
        if (!seg_is_mirrored(seg)) {
238
253
                log_error(INTERNAL_ERROR
239
254
                          "Unable to remove RAID layer from segment type %s",
240
 
                          seg->segtype->name);
 
255
                          seg->segtype->ops->name(seg));
241
256
                return 0;
242
257
        }
243
258
 
289
304
{
290
305
        int was_active = lv_is_active(lv);
291
306
 
 
307
        if (test_mode())
 
308
                return 1;
 
309
 
292
310
        if (!was_active && !activate_lv(lv->vg->cmd, lv)) {
293
311
                log_error("Failed to activate %s for clearing",
294
312
                          lv->name);
621
639
        struct lv_list *lvl;
622
640
        struct lv_segment_area *new_areas;
623
641
 
 
642
        if (lv->status & LV_NOTSYNCED) {
 
643
                log_error("Can't add image to out-of-sync RAID LV:"
 
644
                          " use 'lvchange --resync' first.");
 
645
                return 0;
 
646
        }
 
647
 
 
648
        if (!_raid_in_sync(lv)) {
 
649
                log_error("Can't add image to RAID LV that"
 
650
                          " is still initializing.");
 
651
                return 0;
 
652
        }
 
653
 
624
654
        dm_list_init(&meta_lvs); /* For image addition */
625
655
        dm_list_init(&data_lvs); /* For image addition */
626
656
 
643
673
                dm_list_add(&meta_lvs, &lvl->list);
644
674
        } else if (!seg_is_raid(seg)) {
645
675
                log_error("Unable to add RAID images to %s of segment type %s",
646
 
                          lv->name, seg->segtype->name);
 
676
                          lv->name, seg->segtype->ops->name(seg));
 
677
                return 0;
 
678
        } else if (!_raid_in_sync(lv)) {
 
679
                log_error("Unable to add RAID images until %s is in-sync",
 
680
                          lv->name);
647
681
                return 0;
648
682
        }
649
683
 
697
731
                seg = first_seg(lv);
698
732
                seg_lv(seg, 0)->status |= RAID_IMAGE | LVM_READ | LVM_WRITE;
699
733
                seg->region_size = RAID_REGION_SIZE;
 
734
                /* MD's bitmap is limited to tracking 2^21 regions */
 
735
                while (seg->region_size < (lv->size / (1 << 21))) {
 
736
                        seg->region_size *= 2;
 
737
                        log_very_verbose("Setting RAID1 region_size to %uS",
 
738
                                         seg->region_size);
 
739
                }
700
740
                seg->segtype = get_segtype_from_string(lv->vg->cmd, "raid1");
701
741
                if (!seg->segtype)
702
742
                        return_0;
988
1028
        }
989
1029
 
990
1030
        /* Convert to linear? */
991
 
        if ((new_count == 1) && !_raid_remove_top_layer(lv, &removal_list)) {
992
 
                log_error("Failed to remove RAID layer after linear conversion");
993
 
                return 0;
 
1031
        if (new_count == 1) {
 
1032
                if (!_raid_remove_top_layer(lv, &removal_list)) {
 
1033
                        log_error("Failed to remove RAID layer"
 
1034
                                  " after linear conversion");
 
1035
                        return 0;
 
1036
                }
 
1037
                lv->status &= ~LV_NOTSYNCED;
994
1038
        }
995
1039
 
996
1040
        if (!vg_write(lv->vg)) {
1012
1056
        }
1013
1057
 
1014
1058
        /*
1015
 
         * Resume original LV
1016
 
         * This also resumes all other sub-lvs (including the extracted)
1017
 
         */
1018
 
        if (!resume_lv(lv->vg->cmd, lv)) {
 
1059
         * We resume the extracted sub-LVs first so they are renamed
 
1060
         * and won't conflict with the remaining (possibly shifted)
 
1061
         * sub-LVs.
 
1062
         */
 
1063
        dm_list_iterate_items(lvl, &removal_list) {
 
1064
                if (!resume_lv(lv->vg->cmd, lvl->lv)) {
 
1065
                        log_error("Failed to resume extracted LVs");
 
1066
                        return 0;
 
1067
                }
 
1068
        }
 
1069
 
 
1070
        /*
 
1071
         * Resume the remaining LVs
 
1072
         * We must start by resuming the sub-LVs first (which would
 
1073
         * otherwise be handled automatically) because the shifting
 
1074
         * of positions could otherwise cause name collisions.  For
 
1075
         * example, if position 0 of a 3-way array is removed, position
 
1076
         * 1 and 2 must be shifted and renamed 0 and 1.  If position 2
 
1077
         * tries to rename first, it will collide with the existing
 
1078
         * position 1.
 
1079
         */
 
1080
        if (!_bottom_up_resume(lv)) {
1019
1081
                log_error("Failed to resume %s/%s after committing changes",
1020
1082
                          lv->vg->name, lv->name);
1021
1083
                return 0;
1074
1136
int lv_raid_split(struct logical_volume *lv, const char *split_name,
1075
1137
                  uint32_t new_count, struct dm_list *splittable_pvs)
1076
1138
{
1077
 
        const char *old_name;
1078
1139
        struct lv_list *lvl;
1079
1140
        struct dm_list removal_list, data_list;
1080
1141
        struct cmd_context *cmd = lv->vg->cmd;
1093
1154
 
1094
1155
        if (!seg_is_mirrored(first_seg(lv))) {
1095
1156
                log_error("Unable to split logical volume of segment type, %s",
1096
 
                          first_seg(lv)->segtype->name);
 
1157
                          first_seg(lv)->segtype->ops->name(first_seg(lv)));
1097
1158
                return 0;
1098
1159
        }
1099
1160
 
1145
1206
        dm_list_iterate_items(lvl, &data_list)
1146
1207
                break;
1147
1208
 
1148
 
        old_name = lvl->lv->name;
1149
1209
        lvl->lv->name = split_name;
1150
1210
 
1151
1211
        if (!vg_write(lv->vg)) {
1167
1227
        }
1168
1228
 
1169
1229
        /*
1170
 
         * Resume original LV
1171
 
         * This also resumes all other sub-lvs (including the extracted)
1172
 
         */
1173
 
        if (!resume_lv(cmd, lv)) {
 
1230
         * First resume the newly split LV and LVs on the removal list.
 
1231
         * This is necessary so that there are no name collisions due to
 
1232
         * the original RAID LV having possibly had sub-LVs that have been
 
1233
         * shifted and renamed.
 
1234
         */
 
1235
        if (!resume_lv(cmd, lvl->lv))
 
1236
                return_0;
 
1237
        dm_list_iterate_items(lvl, &removal_list)
 
1238
                if (!resume_lv(cmd, lvl->lv))
 
1239
                        return_0;
 
1240
 
 
1241
        /*
 
1242
         * Resume the remaining LVs
 
1243
         * We must start by resuming the sub-LVs first (which would
 
1244
         * otherwise be handled automatically) because the shifting
 
1245
         * of positions could otherwise cause name collisions.  For
 
1246
         * example, if position 0 of a 3-way array is split, position
 
1247
         * 1 and 2 must be shifted and renamed 0 and 1.  If position 2
 
1248
         * tries to rename first, it will collide with the existing
 
1249
         * position 1.
 
1250
         */
 
1251
        if (!_bottom_up_resume(lv)) {
1174
1252
                log_error("Failed to resume %s/%s after committing changes",
1175
1253
                          lv->vg->name, lv->name);
1176
1254
                return 0;
1177
1255
        }
1178
1256
 
1179
 
        /* Recycle newly split LV so it is properly renamed */
1180
 
        if (!suspend_lv(cmd, lvl->lv) || !resume_lv(cmd, lvl->lv)) {
1181
 
                log_error("Failed to rename %s to %s after committing changes",
1182
 
                          old_name, split_name);
1183
 
                return 0;
1184
 
        }
1185
 
 
1186
1257
        /*
1187
1258
         * Eliminate the residual LVs
1188
1259
         */
1244
1315
                break;
1245
1316
        }
1246
1317
 
1247
 
        if (s >= seg->area_count) {
 
1318
        if (s >= (int) seg->area_count) {
1248
1319
                log_error("Unable to find image to satisfy request");
1249
1320
                return 0;
1250
1321
        }
1267
1338
                return 0;
1268
1339
        }
1269
1340
 
1270
 
        log_print("%s split from %s for read-only purposes.",
1271
 
                  seg_lv(seg, s)->name, lv->name);
 
1341
        log_print_unless_silent("%s split from %s for read-only purposes.",
 
1342
                                seg_lv(seg, s)->name, lv->name);
1272
1343
 
1273
1344
        /* Resume original LV */
1274
1345
        if (!resume_lv(lv->vg->cmd, lv)) {
1281
1352
        if (!_activate_sublv_preserving_excl(lv, seg_lv(seg, s)))
1282
1353
                return 0;
1283
1354
 
1284
 
        log_print("Use 'lvconvert --merge %s/%s' to merge back into %s",
1285
 
                  lv->vg->name, seg_lv(seg, s)->name, lv->name);
 
1355
        log_print_unless_silent("Use 'lvconvert --merge %s/%s' to merge back into %s",
 
1356
                                lv->vg->name, seg_lv(seg, s)->name, lv->name);
1286
1357
        return 1;
1287
1358
}
1288
1359
 
1366
1437
                return 0;
1367
1438
        }
1368
1439
 
1369
 
        log_print("%s/%s successfully merged back into %s/%s",
1370
 
                  vg->name, image_lv->name,
1371
 
                  vg->name, lv->name);
 
1440
        log_print_unless_silent("%s/%s successfully merged back into %s/%s",
 
1441
                                vg->name, image_lv->name, vg->name, lv->name);
1372
1442
        return 1;
1373
1443
}
1374
1444
 
1518
1588
 
1519
1589
        log_error("Converting the segment type for %s/%s from %s to %s"
1520
1590
                  " is not yet supported.", lv->vg->name, lv->name,
1521
 
                  seg->segtype->name, new_segtype->name);
 
1591
                  seg->segtype->ops->name(seg), new_segtype->name);
1522
1592
        return 0;
1523
1593
}
1524
1594
 
1574
1644
                   (match_count > raid_seg->segtype->parity_devs)) {
1575
1645
                log_error("Unable to replace more than %u PVs from (%s) %s/%s",
1576
1646
                          raid_seg->segtype->parity_devs,
1577
 
                          raid_seg->segtype->name, lv->vg->name, lv->name);
 
1647
                          raid_seg->segtype->ops->name(raid_seg),
 
1648
                          lv->vg->name, lv->name);
1578
1649
                return 0;
 
1650
        } else if (!strcmp(raid_seg->segtype->name, "raid10")) {
 
1651
                uint32_t i, rebuilds_per_group = 0;
 
1652
                /* FIXME: We only support 2-way mirrors in RAID10 currently */
 
1653
                uint32_t copies = 2;
 
1654
 
 
1655
                for (i = 0; i < raid_seg->area_count * copies; i++) {
 
1656
                        s = i % raid_seg->area_count;
 
1657
                        if (!(i % copies))
 
1658
                                rebuilds_per_group = 0;
 
1659
                        if (_lv_is_on_pvs(seg_lv(raid_seg, s), remove_pvs) ||
 
1660
                            _lv_is_on_pvs(seg_metalv(raid_seg, s), remove_pvs))
 
1661
                                rebuilds_per_group++;
 
1662
                        if (rebuilds_per_group >= copies) {
 
1663
                                log_error("Unable to replace all the devices "
 
1664
                                          "in a RAID10 mirror group.");
 
1665
                                return 0;
 
1666
                        }
 
1667
                }
1579
1668
        }
1580
1669
 
1581
1670
        /*
1585
1674
         *
1586
1675
         * - We need to change the LV names when we insert them.
1587
1676
         */
 
1677
try_again:
1588
1678
        if (!_alloc_image_components(lv, allocate_pvs, match_count,
1589
1679
                                     &new_meta_lvs, &new_data_lvs)) {
1590
1680
                log_error("Failed to allocate replacement images for %s/%s",
1591
1681
                          lv->vg->name, lv->name);
 
1682
 
 
1683
                /*
 
1684
                 * If this is a repair, then try to
 
1685
                 * do better than all-or-nothing
 
1686
                 */
 
1687
                if (match_count > 1) {
 
1688
                        log_error("Attempting replacement of %u devices"
 
1689
                                  " instead of %u", match_count - 1, match_count);
 
1690
                        match_count--;
 
1691
 
 
1692
                        /*
 
1693
                         * Since we are replacing some but not all of the bad
 
1694
                         * devices, we must set partial_activation
 
1695
                         */
 
1696
                        lv->vg->cmd->partial_activation = 1;
 
1697
                        goto try_again;
 
1698
                }
1592
1699
                return 0;
1593
1700
        }
1594
1701
 
1666
1773
                return 0;
1667
1774
        }
1668
1775
 
1669
 
        if (!suspend_lv(lv->vg->cmd, lv)) {
 
1776
        if (!suspend_lv_origin(lv->vg->cmd, lv)) {
1670
1777
                log_error("Failed to suspend %s/%s before committing changes",
1671
1778
                          lv->vg->name, lv->name);
1672
1779
                return 0;
1678
1785
                return 0;
1679
1786
        }
1680
1787
 
1681
 
        if (!resume_lv(lv->vg->cmd, lv)) {
 
1788
        if (!resume_lv_origin(lv->vg->cmd, lv)) {
1682
1789
                log_error("Failed to resume %s/%s after committing changes",
1683
1790
                          lv->vg->name, lv->name);
1684
1791
                return 0;
1714
1821
                return 0;
1715
1822
        }
1716
1823
 
1717
 
        if (!suspend_lv(lv->vg->cmd, lv)) {
 
1824
        if (!suspend_lv_origin(lv->vg->cmd, lv)) {
1718
1825
                log_error("Failed to suspend %s/%s before committing changes",
1719
1826
                          lv->vg->name, lv->name);
1720
1827
                return 0;
1726
1833
                return 0;
1727
1834
        }
1728
1835
 
1729
 
        if (!resume_lv(lv->vg->cmd, lv)) {
 
1836
        if (!resume_lv_origin(lv->vg->cmd, lv)) {
1730
1837
                log_error("Failed to resume %s/%s after committing changes",
1731
1838
                          lv->vg->name, lv->name);
1732
1839
                return 0;