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

« back to all changes in this revision

Viewing changes to lib/metadata/lv_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:
1
1
/*
2
2
 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
3
 
 * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
 
3
 * Copyright (C) 2004-2012 Red Hat, Inc. All rights reserved.
4
4
 *
5
5
 * This file is part of LVM2.
6
6
 *
137
137
                return NULL;
138
138
        }
139
139
 
140
 
        sl = dm_list_item(dm_list_first(&lv->segs_using_this_lv), struct seg_list);
 
140
        dm_list_iterate_items(sl, &lv->segs_using_this_lv)
 
141
                break; /* first item */
141
142
 
142
143
        if (sl->count != 1) {
143
144
                log_error("%s is expected to have only one segment using it, "
201
202
/*
202
203
 * All lv_segments get created here.
203
204
 */
204
 
struct lv_segment *alloc_lv_segment(struct dm_pool *mem,
205
 
                                    const struct segment_type *segtype,
 
205
struct lv_segment *alloc_lv_segment(const struct segment_type *segtype,
206
206
                                    struct logical_volume *lv,
207
207
                                    uint32_t le, uint32_t len,
208
208
                                    uint64_t status,
209
209
                                    uint32_t stripe_size,
210
210
                                    struct logical_volume *log_lv,
 
211
                                    struct logical_volume *thin_pool_lv,
211
212
                                    uint32_t area_count,
212
213
                                    uint32_t area_len,
213
214
                                    uint32_t chunk_size,
216
217
                                    struct lv_segment *pvmove_source_seg)
217
218
{
218
219
        struct lv_segment *seg;
 
220
        struct dm_pool *mem = lv->vg->vgmem;
219
221
        uint32_t areas_sz = area_count * sizeof(*seg->areas);
220
222
 
221
223
        if (!segtype) {
248
250
        seg->chunk_size = chunk_size;
249
251
        seg->region_size = region_size;
250
252
        seg->extents_copied = extents_copied;
251
 
        seg->log_lv = log_lv;
252
253
        seg->pvmove_source_seg = pvmove_source_seg;
253
254
        dm_list_init(&seg->tags);
 
255
        dm_list_init(&seg->thin_messages);
 
256
 
 
257
        if (thin_pool_lv) {
 
258
                /* If this thin volume, thin snapshot is being created */
 
259
                if (lv_is_thin_volume(thin_pool_lv)) {
 
260
                        seg->transaction_id = first_seg(first_seg(thin_pool_lv)->pool_lv)->transaction_id;
 
261
                        if (!attach_pool_lv(seg, first_seg(thin_pool_lv)->pool_lv, thin_pool_lv))
 
262
                                return_NULL;
 
263
                } else {
 
264
                        seg->transaction_id = first_seg(thin_pool_lv)->transaction_id;
 
265
                        if (!attach_pool_lv(seg, thin_pool_lv, NULL))
 
266
                                return_NULL;
 
267
                }
 
268
        }
254
269
 
255
270
        if (log_lv && !attach_mirror_log(seg, log_lv))
256
271
                return_NULL;
270
285
                return NULL;
271
286
        }
272
287
 
273
 
        if (!(seg = alloc_lv_segment(lv->vg->cmd->mem, segtype, lv, old_le_count,
 
288
        if (!(seg = alloc_lv_segment(segtype, lv, old_le_count,
274
289
                                     lv->le_count - old_le_count, status, 0,
275
 
                                     NULL, 0, lv->le_count - old_le_count,
 
290
                                     NULL, NULL, 0, lv->le_count - old_le_count,
276
291
                                     0, 0, 0, NULL))) {
277
292
                log_error("Couldn't allocate new snapshot segment.");
278
293
                return NULL;
297
312
                return;
298
313
        }
299
314
 
300
 
        if (seg_lv(seg, s)->status & MIRROR_IMAGE) {
301
 
                lv_reduce(seg_lv(seg, s), area_reduction);
 
315
        if ((seg_lv(seg, s)->status & MIRROR_IMAGE) ||
 
316
            (seg_lv(seg, s)->status & THIN_POOL_DATA)) {
 
317
                if (!lv_reduce(seg_lv(seg, s), area_reduction))
 
318
                        stack; /* FIXME: any upper level reporting */
302
319
                return;
303
320
        }
304
321
 
502
519
                        /* FIXME Check this is safe */
503
520
                        if (seg->log_lv && !lv_remove(seg->log_lv))
504
521
                                return_0;
 
522
 
 
523
                        if (seg->metadata_lv && !lv_remove(seg->metadata_lv))
 
524
                                return_0;
 
525
 
 
526
                        if (seg->pool_lv) {
 
527
                                if (!detach_pool_lv(seg))
 
528
                                        return_0;
 
529
                        }
 
530
 
505
531
                        dm_list_del(&seg->list);
506
532
                        reduction = seg->len;
507
533
                } else
559
585
 
560
586
        /* FIXME: Should we bug if we find a log_lv attached? */
561
587
 
562
 
        if (!lv_add_virtual_segment(lv, 0, len,
563
 
                                    get_segtype_from_string(lv->vg->cmd,
564
 
                                                            "error")))
 
588
        if (!lv_add_virtual_segment(lv, 0, len, get_segtype_from_string(lv->vg->cmd, "error"), NULL))
565
589
                return_0;
566
590
 
567
591
        return 1;
628
652
         */
629
653
        unsigned alloc_and_split_meta;
630
654
 
631
 
        const struct config_node *cling_tag_list_cn;
 
655
        const struct dm_config_node *cling_tag_list_cn;
632
656
 
633
657
        struct dm_list *parallel_areas; /* PVs to avoid */
634
658
 
675
699
        /* Log device holds both header and bitset. */
676
700
        log_size = dm_round_up((MIRROR_LOG_OFFSET << SECTOR_SHIFT) + bitset_size, 1 << SECTOR_SHIFT);
677
701
        log_size >>= SECTOR_SHIFT;
 
702
        log_size = dm_div_up(log_size, pe_size);
678
703
 
679
 
        return dm_div_up(log_size, pe_size);
 
704
        /*
 
705
         * Kernel requires a mirror to be at least 1 region large.  So,
 
706
         * if our mirror log is itself a mirror, it must be at least
 
707
         * 1 region large.  This restriction may not be necessary for
 
708
         * non-mirrored logs, but we apply the rule anyway.
 
709
         *
 
710
         * (The other option is to make the region size of the log
 
711
         * mirror smaller than the mirror it is acting as a log for,
 
712
         * but that really complicates things.  It's much easier to
 
713
         * keep the region_size the same for both.)
 
714
         */
 
715
        return (log_size > (region_size / pe_size)) ? log_size :
 
716
                (region_size / pe_size);
680
717
}
681
718
 
682
719
/*
697
734
                                        struct dm_list *parallel_areas)
698
735
{
699
736
        struct alloc_handle *ah;
700
 
        uint32_t s, area_count, alloc_count;
 
737
        uint32_t s, area_count, alloc_count, parity_count;
701
738
        size_t size = 0;
702
739
 
703
740
        /* FIXME Caller should ensure this */
712
749
                area_count = stripes;
713
750
 
714
751
        size = sizeof(*ah);
715
 
        alloc_count = area_count + segtype->parity_devs;
 
752
 
 
753
        /*
 
754
         * It is a requirement that RAID 4/5/6 are created with a number of
 
755
         * stripes that is greater than the number of parity devices.  (e.g
 
756
         * RAID4/5 must have at least 2 stripes and RAID6 must have at least
 
757
         * 3.)  It is also a constraint that, when replacing individual devices
 
758
         * in a RAID 4/5/6 array, no more devices can be replaced than
 
759
         * there are parity devices.  (Otherwise, there would not be enough
 
760
         * redundancy to maintain the array.)  Understanding these two
 
761
         * constraints allows us to infer whether the caller of this function
 
762
         * is intending to allocate an entire array or just replacement
 
763
         * component devices.  In the former case, we must account for the
 
764
         * necessary parity_count.  In the later case, we do not need to
 
765
         * account for the extra parity devices because the array already
 
766
         * exists and they only want replacement drives.
 
767
         */
 
768
        parity_count = (area_count <= segtype->parity_devs) ? 0 :
 
769
                segtype->parity_devs;
 
770
        alloc_count = area_count + parity_count;
716
771
        if (segtype_is_raid(segtype) && metadata_area_count)
717
772
                /* RAID has a meta area for each device */
718
773
                alloc_count *= 2;
742
797
                return NULL;
743
798
        }
744
799
 
745
 
        ah->new_extents = new_extents;
 
800
        if (mirrors || stripes)
 
801
                ah->new_extents = new_extents;
 
802
        else
 
803
                ah->new_extents = 0;
746
804
        ah->area_count = area_count;
747
 
        ah->parity_count = segtype->parity_devs;
 
805
        ah->parity_count = parity_count;
748
806
        ah->region_size = region_size;
749
807
        ah->alloc = alloc;
750
808
        ah->area_multiple = _calc_area_multiple(segtype, area_count, stripes);
 
809
        ah->mirror_logs_separate = find_config_tree_bool(cmd, "allocation/mirror_logs_require_separate_pvs",
 
810
                                                         DEFAULT_MIRROR_LOGS_REQUIRE_SEPARATE_PVS);
751
811
 
752
812
        if (segtype_is_raid(segtype)) {
753
813
                if (metadata_area_count) {
764
824
                         * RAID device's metadata_area
765
825
                         */
766
826
                        ah->new_extents += (ah->log_len * ah->area_multiple);
 
827
                } else {
 
828
                        ah->log_area_count = 0;
 
829
                        ah->log_len = 0;
767
830
                }
 
831
        } else if (segtype_is_thin_pool(segtype)) {
 
832
                ah->log_area_count = metadata_area_count;
 
833
                /* thin_pool uses region_size to pass metadata size in extents */
 
834
                ah->log_len = ah->region_size;
 
835
                ah->region_size = 0;
 
836
                ah->mirror_logs_separate =
 
837
                        find_config_tree_bool(cmd, "allocation/thin_pool_metadata_require_separate_pvs",
 
838
                                              DEFAULT_THIN_POOL_METADATA_REQUIRE_SEPARATE_PVS);
768
839
        } else {
769
840
                ah->log_area_count = metadata_area_count;
770
841
                ah->log_len = !metadata_area_count ? 0 :
771
842
                        mirror_log_extents(ah->region_size, extent_size,
772
 
                                           ah->new_extents / ah->area_multiple);
 
843
                                           new_extents / ah->area_multiple);
773
844
        }
774
845
 
775
846
        for (s = 0; s < alloc_count; s++)
781
852
 
782
853
        ah->maximise_cling = find_config_tree_bool(cmd, "allocation/maximise_cling", DEFAULT_MAXIMISE_CLING);
783
854
 
784
 
        ah->mirror_logs_separate = find_config_tree_bool(cmd, "allocation/mirror_logs_require_separate_pvs", DEFAULT_MIRROR_LOGS_REQUIRE_SEPARATE_PVS);
785
 
 
786
855
        return ah;
787
856
}
788
857
 
914
983
 
915
984
        area_multiple = _calc_area_multiple(segtype, area_count, 0);
916
985
 
917
 
        if (!(seg = alloc_lv_segment(lv->vg->cmd->mem, segtype, lv,
918
 
                                     lv->le_count,
 
986
        if (!(seg = alloc_lv_segment(segtype, lv, lv->le_count,
919
987
                                     aa[0].len * area_multiple,
920
 
                                     status, stripe_size, NULL,
 
988
                                     status, stripe_size, NULL, NULL,
921
989
                                     area_count,
922
990
                                     aa[0].len, 0u, region_size, 0u, NULL))) {
923
991
                log_error("Couldn't allocate new LV segment.");
1161
1229
        struct pv_area_used *areas;
1162
1230
        struct pv_area *pva;
1163
1231
        uint32_t areas_size;
1164
 
        const struct config_node *cling_tag_list_cn;
 
1232
        const struct dm_config_node *cling_tag_list_cn;
1165
1233
        int s;  /* Area index of match */
1166
1234
};
1167
1235
 
1182
1250
 */
1183
1251
static int _has_matching_pv_tag(struct pv_match *pvmatch, struct pv_segment *pvseg, struct pv_area *pva)
1184
1252
{
1185
 
        const struct config_value *cv;
 
1253
        const struct dm_config_value *cv;
1186
1254
        const char *str;
1187
1255
        const char *tag_matched;
1188
1256
 
1189
1257
        for (cv = pvmatch->cling_tag_list_cn->v; cv; cv = cv->next) {
1190
 
                if (cv->type != CFG_STRING) {
 
1258
                if (cv->type != DM_CFG_STRING) {
1191
1259
                        log_error("Ignoring invalid string in config file entry "
1192
1260
                                  "allocation/cling_tag_list");
1193
1261
                        continue;
1292
1360
 * Is pva on same PV as any existing areas?
1293
1361
 */
1294
1362
static int _check_cling(struct alloc_handle *ah,
1295
 
                        const struct config_node *cling_tag_list_cn,
 
1363
                        const struct dm_config_node *cling_tag_list_cn,
1296
1364
                        struct lv_segment *prev_lvseg, struct pv_area *pva,
1297
1365
                        struct alloc_state *alloc_state)
1298
1366
{
1411
1479
        if (!pva->unreserved)
1412
1480
                return NEXT_AREA;
1413
1481
 
1414
 
        if (iteration_count + log_iteration_count) {
 
1482
        /* FIXME Should this test be removed? */
 
1483
        if (iteration_count)
1415
1484
                /*
1416
 
                 * Don't use an area twice.
1417
 
                 * Only ALLOC_ANYWHERE currently supports that, by destroying the data structures,
1418
 
                 * which is OK because they are not needed again afterwards.
1419
 
                 */
 
1485
                * Don't use an area twice.
 
1486
                */
1420
1487
                for (s = 0; s < alloc_state->areas_size; s++)
1421
1488
                        if (alloc_state->areas[s].pva == pva)
1422
1489
                                return NEXT_AREA;
1423
 
        }
1424
1490
 
1425
1491
        /* If maximise_cling is set, perform several checks, otherwise perform exactly one. */
1426
1492
        if (!iteration_count && !log_iteration_count && alloc_parms->flags & (A_CONTIGUOUS | A_CLING | A_CLING_TO_ALLOCED)) {
1478
1544
{
1479
1545
        uint32_t required = max_to_allocate / ah->area_multiple;
1480
1546
 
1481
 
        /* FIXME Maintain unreserved all the time, so other policies can split areas too. */
1482
 
 
 
1547
        /*
 
1548
         * Update amount unreserved - effectively splitting an area 
 
1549
         * into two or more parts.  If the whole stripe doesn't fit,
 
1550
         * reduce amount we're looking for.
 
1551
         */
1483
1552
        if (alloc == ALLOC_ANYWHERE) {
1484
 
                /*
1485
 
                 * Update amount unreserved - effectively splitting an area 
1486
 
                 * into two or more parts.  If the whole stripe doesn't fit,
1487
 
                 * reduce amount we're looking for.
1488
 
                 */
1489
1553
                if (ix_pva - 1 >= ah->area_count)
1490
1554
                        required = ah->log_len;
1491
 
                if (required >= pva->unreserved) {
1492
 
                        required = pva->unreserved;
1493
 
                        pva->unreserved = 0;
1494
 
                } else {
1495
 
                        pva->unreserved -= required;
1496
 
                        reinsert_reduced_pv_area(pva);
1497
 
                }
 
1555
        } else if (required < ah->log_len)
 
1556
                required = ah->log_len;
 
1557
 
 
1558
        if (required >= pva->unreserved) {
 
1559
                required = pva->unreserved;
 
1560
                pva->unreserved = 0;
1498
1561
        } else {
1499
 
                if (required < ah->log_len)
1500
 
                        required = ah->log_len;
1501
 
                if (required > pva->count)
1502
 
                        required = pva->count;
 
1562
                pva->unreserved -= required;
 
1563
                reinsert_changed_pv_area(pva);
1503
1564
        }
1504
1565
 
1505
1566
        return required;
1523
1584
                        alloc_state->areas[s].pva = NULL;
1524
1585
        }
1525
1586
 
1526
 
        _reserve_area(&alloc_state->areas[ix_pva - 1], pva, required, ix_pva, 
1527
 
                  (alloc == ALLOC_ANYWHERE) ? pva->unreserved : pva->count - required);
 
1587
        _reserve_area(&alloc_state->areas[ix_pva - 1], pva, required, ix_pva, pva->unreserved);
1528
1588
 
1529
1589
        return 1;
1530
1590
}
1537
1597
                alloc_state->areas[s].pva = NULL;
1538
1598
}
1539
1599
 
 
1600
static void _reset_unreserved(struct dm_list *pvms)
 
1601
{
 
1602
        struct pv_map *pvm;
 
1603
        struct pv_area *pva;
 
1604
 
 
1605
        dm_list_iterate_items(pvm, pvms)
 
1606
                dm_list_iterate_items(pva, &pvm->areas)
 
1607
                        if (pva->unreserved != pva->count) {
 
1608
                                pva->unreserved = pva->count;
 
1609
                                reinsert_changed_pv_area(pva);
 
1610
                        }
 
1611
}
 
1612
 
1540
1613
static void _report_needed_allocation_space(struct alloc_handle *ah,
1541
1614
                                            struct alloc_state *alloc_state)
1542
1615
{
1600
1673
                          alloc_parms->flags & A_CLING_TO_ALLOCED ? "" : "not ");
1601
1674
 
1602
1675
        _clear_areas(alloc_state);
 
1676
        _reset_unreserved(pvms);
1603
1677
 
1604
1678
        _report_needed_allocation_space(ah, alloc_state);
1605
1679
 
1653
1727
                                                if (alloc_state->areas[s].pva && alloc_state->areas[s].pva->map->pv == pvm->pv)
1654
1728
                                                        goto next_pv;
1655
1729
                                /* On a second pass, avoid PVs already used in an uncommitted area */
1656
 
                                } else if (iteration_count)
 
1730
                                } else if (iteration_count)
1657
1731
                                        for (s = 0; s < devices_needed; s++)
1658
1732
                                                if (alloc_state->areas[s].pva && alloc_state->areas[s].pva->map->pv == pvm->pv)
1659
1733
                                                        goto next_pv;
1886
1960
        }
1887
1961
 
1888
1962
        if (ah->area_multiple > 1 &&
1889
 
            (ah->new_extents - alloc_state.allocated) % ah->area_count) {
 
1963
            (ah->new_extents - alloc_state.allocated) % ah->area_multiple) {
1890
1964
                log_error("Number of extents requested (%d) needs to be divisible by %d.",
1891
 
                          ah->new_extents - alloc_state.allocated, ah->area_count);
 
1965
                          ah->new_extents - alloc_state.allocated,
 
1966
                          ah->area_multiple);
1892
1967
                return 0;
1893
1968
        }
1894
1969
 
1987
2062
}
1988
2063
 
1989
2064
int lv_add_virtual_segment(struct logical_volume *lv, uint64_t status,
1990
 
                           uint32_t extents, const struct segment_type *segtype)
 
2065
                           uint32_t extents, const struct segment_type *segtype,
 
2066
                           const char *thin_pool_name)
1991
2067
{
1992
2068
        struct lv_segment *seg;
1993
 
 
1994
 
        if (!(seg = alloc_lv_segment(lv->vg->cmd->mem, segtype, lv,
1995
 
                                     lv->le_count, extents, status, 0,
1996
 
                                     NULL, 0, extents, 0, 0, 0, NULL))) {
1997
 
                log_error("Couldn't allocate new zero segment.");
1998
 
                return 0;
1999
 
        }
2000
 
 
2001
 
        dm_list_add(&lv->segments, &seg->list);
 
2069
        struct logical_volume *thin_pool_lv = NULL;
 
2070
        struct lv_list *lvl;
 
2071
        uint32_t size;
 
2072
 
 
2073
        if (thin_pool_name) {
 
2074
                if (!(lvl = find_lv_in_vg(lv->vg, thin_pool_name))) {
 
2075
                        log_error("Unable to find existing pool LV %s in VG %s.",
 
2076
                                  thin_pool_name, lv->vg->name);
 
2077
                        return 0;
 
2078
                }
 
2079
                thin_pool_lv = lvl->lv;
 
2080
                size = first_seg(thin_pool_lv)->chunk_size;
 
2081
                if (lv->vg->extent_size < size) {
 
2082
                        /* Align extents on chunk boundary size */
 
2083
                        size = ((uint64_t)lv->vg->extent_size * extents + size - 1) /
 
2084
                                size * size / lv->vg->extent_size;
 
2085
                        if (size != extents) {
 
2086
                                log_print("Rounding size (%d extents) up to chunk boundary "
 
2087
                                          "size (%d extents).", extents, size);
 
2088
                                extents = size;
 
2089
                        }
 
2090
                }
 
2091
        }
 
2092
 
 
2093
        if (!dm_list_empty(&lv->segments) &&
 
2094
            (seg = last_seg(lv)) && (seg->segtype == segtype)) {
 
2095
                seg->area_len += extents;
 
2096
                seg->len += extents;
 
2097
        } else {
 
2098
                if (!(seg = alloc_lv_segment(segtype, lv, lv->le_count, extents,
 
2099
                                             status, 0, NULL, thin_pool_lv, 0,
 
2100
                                             extents, 0, 0, 0, NULL))) {
 
2101
                        log_error("Couldn't allocate new zero segment.");
 
2102
                        return 0;
 
2103
                }
 
2104
                lv->status |= VIRTUAL;
 
2105
                dm_list_add(&lv->segments, &seg->list);
 
2106
        }
2002
2107
 
2003
2108
        lv->le_count += extents;
2004
2109
        lv->size += (uint64_t) extents *lv->vg->extent_size;
2005
2110
 
2006
 
        lv->status |= VIRTUAL;
2007
 
 
2008
2111
        return 1;
2009
2112
}
2010
2113
 
2029
2132
                return NULL;
2030
2133
        }
2031
2134
 
 
2135
        if (!allocatable_pvs) {
 
2136
                log_error(INTERNAL_ERROR "Missing allocatable pvs.");
 
2137
                return NULL;
 
2138
        }
 
2139
 
2032
2140
        if (vg->fid->fmt->ops->segtype_supported &&
2033
2141
            !vg->fid->fmt->ops->segtype_supported(vg->fid, segtype)) {
2034
2142
                log_error("Metadata format (%s) does not support required "
2125
2233
                return NULL;
2126
2234
        }
2127
2235
 
2128
 
        if (!(newseg = alloc_lv_segment(seg->lv->vg->cmd->mem,
2129
 
                                        get_segtype_from_string(seg->lv->vg->cmd, "mirror"),
 
2236
        if (!(newseg = alloc_lv_segment(get_segtype_from_string(seg->lv->vg->cmd, "mirror"),
2130
2237
                                        seg->lv, seg->le, seg->len,
2131
2238
                                        seg->status, seg->stripe_size,
2132
 
                                        log_lv,
 
2239
                                        log_lv, NULL,
2133
2240
                                        seg->area_count, seg->area_len,
2134
2241
                                        seg->chunk_size, region_size,
2135
2242
                                        seg->extents_copied, NULL))) {
2222
2329
 
2223
2330
        if (dm_list_size(&lv->segments) != 1 || seg_type(seg, 0) != AREA_LV) {
2224
2331
                log_error("Mirror layer must be inserted before adding mirrors");
2225
 
                return_0;
 
2332
                return 0;
2226
2333
        }
2227
2334
 
2228
2335
        mirror_segtype = get_segtype_from_string(lv->vg->cmd, "mirror");
2272
2379
 * If we used AREA_PVs under the mirror layer of a log, we could
2273
2380
 * assemble it all at once by calling 'lv_add_segment' with the
2274
2381
 * appropriate segtype (mirror/stripe), like this:
2275
 
 *      lv_add_segment(ah, ah->area_count, ah->log_area_count,
 
2382
 *      lv_add_segment(ah, ah->area_count, ah->log_area_count,
2276
2383
 *                     log_lv, segtype, 0, MIRROR_LOG, 0);
2277
2384
 *
2278
2385
 * For now, we use the same mechanism to build a mirrored log as we
2296
2403
{
2297
2404
        struct logical_volume *sub_lv;
2298
2405
        uint32_t i;
2299
 
        uint64_t status = 0;
 
2406
        uint64_t sub_lv_status = 0;
2300
2407
        const char *layer_name;
2301
2408
        size_t len = strlen(lv->name) + 32;
2302
2409
        char img_name[len];
2303
2410
        struct lv_segment *mapseg;
2304
2411
 
2305
 
        if (lv->le_count || first_seg(lv)) {
 
2412
        if (lv->le_count || !dm_list_empty(&lv->segments)) {
2306
2413
                log_error(INTERNAL_ERROR
2307
2414
                          "Non-empty LV passed to _lv_insert_empty_sublv");
2308
2415
                return 0;
2310
2417
 
2311
2418
        if (segtype_is_raid(segtype)) {
2312
2419
                lv->status |= RAID;
2313
 
                status = RAID_IMAGE;
 
2420
                sub_lv_status = RAID_IMAGE;
2314
2421
                layer_name = "rimage";
2315
2422
        } else if (segtype_is_mirrored(segtype)) {
2316
2423
                lv->status |= MIRRORED;
2317
 
                status = MIRROR_IMAGE;
 
2424
                sub_lv_status = MIRROR_IMAGE;
2318
2425
                layer_name = "mimage";
2319
2426
        } else
2320
2427
                return_0;
2322
2429
        /*
2323
2430
         * First, create our top-level segment for our top-level LV
2324
2431
         */
2325
 
        if (!(mapseg = alloc_lv_segment(lv->vg->cmd->mem, segtype,
2326
 
                                        lv, 0, 0, lv->status, stripe_size, NULL,
 
2432
        if (!(mapseg = alloc_lv_segment(segtype, lv, 0, 0, lv->status,
 
2433
                                        stripe_size, NULL, NULL,
2327
2434
                                        devices, 0, 0, region_size, 0, NULL))) {
2328
2435
                log_error("Failed to create mapping segment for %s", lv->name);
2329
2436
                return 0;
2334
2441
         */
2335
2442
        for (i = 0; i < devices; i++) {
2336
2443
                /* Data LVs */
2337
 
                if (dm_snprintf(img_name, len, "%s_%s_%u",
2338
 
                                lv->name, layer_name, i) < 0)
2339
 
                        return_0;
2340
 
 
2341
 
                sub_lv = lv_create_empty(img_name, NULL,
2342
 
                                         LVM_READ | LVM_WRITE | status,
2343
 
                                         lv->alloc, lv->vg);
2344
 
 
2345
 
                if (!sub_lv)
2346
 
                        return_0;
2347
 
                if (!set_lv_segment_area_lv(mapseg, i, sub_lv, 0, status))
2348
 
                        return_0;
2349
 
                if (!segtype_is_raid(segtype))
 
2444
                if (devices > 1) {
 
2445
                        if (dm_snprintf(img_name, len, "%s_%s_%u",
 
2446
                                        lv->name, layer_name, i) < 0)
 
2447
                                return_0;
 
2448
                } else {
 
2449
                        if (dm_snprintf(img_name, len, "%s_%s",
 
2450
                                        lv->name, layer_name) < 0)
 
2451
                                return_0;
 
2452
                }
 
2453
 
 
2454
                /* FIXME Should use ALLOC_INHERIT here and inherit from parent LV */
 
2455
                if (!(sub_lv = lv_create_empty(img_name, NULL,
 
2456
                                         LVM_READ | LVM_WRITE,
 
2457
                                         lv->alloc, lv->vg)))
 
2458
                        return_0;
 
2459
 
 
2460
                if (!set_lv_segment_area_lv(mapseg, i, sub_lv, 0, sub_lv_status))
 
2461
                        return_0;
 
2462
 
 
2463
                /* Metadata LVs for raid */
 
2464
                if (segtype_is_raid(segtype)) {
 
2465
                        if (dm_snprintf(img_name, len, "%s_rmeta_%u", lv->name, i) < 0)
 
2466
                                return_0;
 
2467
                } else
2350
2468
                        continue;
2351
2469
 
2352
 
                /* RAID meta LVs */
2353
 
                if (dm_snprintf(img_name, len, "%s_rmeta_%u", lv->name, i) < 0)
 
2470
                /* FIXME Should use ALLOC_INHERIT here and inherit from parent LV */
 
2471
                if (!(sub_lv = lv_create_empty(img_name, NULL,
 
2472
                                               LVM_READ | LVM_WRITE,
 
2473
                                               lv->alloc, lv->vg)))
2354
2474
                        return_0;
2355
2475
 
2356
 
                sub_lv = lv_create_empty(img_name, NULL,
2357
 
                                         LVM_READ | LVM_WRITE | RAID_META,
2358
 
                                         lv->alloc, lv->vg);
2359
 
                if (!sub_lv)
2360
 
                        return_0;
2361
2476
                if (!set_lv_segment_area_lv(mapseg, i, sub_lv, 0, RAID_META))
2362
 
                        return_0;
 
2477
                                return_0;
2363
2478
        }
 
2479
 
2364
2480
        dm_list_add(&lv->segments, &mapseg->list);
2365
2481
 
2366
2482
        return 1;
2481
2597
              const struct segment_type *segtype,
2482
2598
              uint32_t stripes, uint32_t stripe_size,
2483
2599
              uint32_t mirrors, uint32_t region_size,
2484
 
              uint32_t extents,
 
2600
              uint32_t extents, const char *thin_pool_name,
2485
2601
              struct dm_list *allocatable_pvs, alloc_policy_t alloc)
2486
2602
{
2487
2603
        int r = 1;
2488
 
        int raid_logs = 0;
 
2604
        int log_count = 0;
2489
2605
        struct alloc_handle *ah;
2490
 
        uint32_t dev_count = mirrors * stripes + segtype->parity_devs;
 
2606
        uint32_t sub_lv_count;
2491
2607
 
2492
2608
        log_very_verbose("Extending segment type, %s", segtype->name);
2493
2609
 
2494
2610
        if (segtype_is_virtual(segtype))
2495
 
                return lv_add_virtual_segment(lv, 0u, extents, segtype);
 
2611
                return lv_add_virtual_segment(lv, 0u, extents, segtype, thin_pool_name);
2496
2612
 
2497
 
        if (segtype_is_raid(segtype) && !lv->le_count)
2498
 
                raid_logs = mirrors * stripes;
 
2613
        if (!lv->le_count && segtype_is_thin_pool(segtype)) {
 
2614
                /* Thin pool allocation treats its metadata device like a mirror log. */
 
2615
                /* FIXME Allow pool and data on same device with NORMAL */
 
2616
                /* FIXME Support striped metadata pool */
 
2617
                log_count = 1;
 
2618
        } else if (segtype_is_raid(segtype) && !lv->le_count)
 
2619
                log_count = mirrors * stripes;
 
2620
        /* FIXME log_count should be 1 for mirrors */
2499
2621
 
2500
2622
        if (!(ah = allocate_extents(lv->vg, lv, segtype, stripes, mirrors,
2501
 
                                    raid_logs, region_size, extents,
 
2623
                                    log_count, region_size, extents,
2502
2624
                                    allocatable_pvs, alloc, NULL)))
2503
2625
                return_0;
2504
2626
 
2505
 
        if (!segtype_is_mirrored(segtype) && !segtype_is_raid(segtype))
2506
 
                r = lv_add_segment(ah, 0, ah->area_count, lv, segtype,
2507
 
                                   stripe_size, 0u, 0);
2508
 
        else {
 
2627
        if (segtype_is_thin_pool(segtype)) {
 
2628
                if (!lv->le_count) {
 
2629
                        if (!(r = extend_pool(lv, segtype, ah, stripes, stripe_size)))
 
2630
                                stack;
 
2631
                } else if (!(r = _lv_extend_layered_lv(ah, lv, extents, 0,
 
2632
                                                       stripes, stripe_size)))
 
2633
                        stack;
 
2634
        } else if (!segtype_is_mirrored(segtype) && !segtype_is_raid(segtype)) {
 
2635
                if (!(r = lv_add_segment(ah, 0, ah->area_count, lv, segtype,
 
2636
                                         stripe_size, 0u, 0)))
 
2637
                        stack;
 
2638
        } else {
2509
2639
                /*
2510
2640
                 * For RAID, all the devices are AREA_LV.
2511
2641
                 * However, for 'mirror on stripe' using non-RAID targets,
2512
2642
                 * the mirror legs are AREA_LV while the stripes underneath
2513
 
                 * are AREA_PV.  So if this is not RAID, reset dev_count to
2514
 
                 * just 'mirrors' - the necessary sub_lv count.
 
2643
                 * are AREA_PV.
2515
2644
                 */
2516
 
                if (!segtype_is_raid(segtype))
2517
 
                        dev_count = mirrors;
 
2645
                if (segtype_is_raid(segtype))
 
2646
                        sub_lv_count = mirrors * stripes + segtype->parity_devs;
 
2647
                else
 
2648
                        sub_lv_count = mirrors;
2518
2649
 
2519
2650
                if (!lv->le_count &&
2520
 
                    !_lv_insert_empty_sublvs(lv, segtype, stripe_size,
2521
 
                                             region_size, dev_count)) {
 
2651
                    !(r = _lv_insert_empty_sublvs(lv, segtype, stripe_size,
 
2652
                                                  region_size, sub_lv_count))) {
2522
2653
                        log_error("Failed to insert layer for %s", lv->name);
2523
 
                        alloc_destroy(ah);
2524
 
                        return 0;
2525
 
                }
2526
 
 
2527
 
                r = _lv_extend_layered_lv(ah, lv, extents, 0,
2528
 
                                          stripes, stripe_size);
 
2654
                        goto out;
 
2655
                }
 
2656
 
 
2657
                if (!(r = _lv_extend_layered_lv(ah, lv, extents, 0,
 
2658
                                                stripes, stripe_size)))
 
2659
                        goto_out;
 
2660
 
 
2661
                /*
 
2662
                 * If we are expanding an existing mirror, we can skip the
 
2663
                 * resync of the extension if the LV is currently in-sync
 
2664
                 * and the LV has the LV_NOTSYNCED flag set.
 
2665
                 */
 
2666
                if ((lv->le_count != extents) &&
 
2667
                    segtype_is_mirrored(segtype) &&
 
2668
                    (lv->status & LV_NOTSYNCED)) {
 
2669
                        percent_t sync_percent = PERCENT_INVALID;
 
2670
 
 
2671
                        if (!lv_is_active(lv)) {
 
2672
                                log_print("%s/%s is not active."
 
2673
                                          "  Unable to get sync percent.",
 
2674
                                          lv->vg->name, lv->name);
 
2675
                                if (yes_no_prompt("Do full resync of extended "
 
2676
                                                  "portion of %s/%s?  [y/n]: ",
 
2677
                                                  lv->vg->name, lv->name) == 'y')
 
2678
                                        goto out;
 
2679
                                r = 0;
 
2680
                                goto out;
 
2681
                        }
 
2682
 
 
2683
                        if (!(r = lv_mirror_percent(lv->vg->cmd, lv, 0,
 
2684
                                                    &sync_percent, NULL))) {
 
2685
                                log_error("Failed to get sync percent for %s/%s",
 
2686
                                          lv->vg->name, lv->name);
 
2687
                                goto out;
 
2688
                        } else if (sync_percent == PERCENT_100) {
 
2689
                                log_verbose("Skipping initial resync for "
 
2690
                                            "extended portion of %s/%s",
 
2691
                                            lv->vg->name, lv->name);
 
2692
                                init_mirror_in_sync(1);
 
2693
                                lv->status |= LV_NOTSYNCED;
 
2694
                        } else {
 
2695
                                log_error("%s/%s cannot be extended while"
 
2696
                                          " it is recovering.",
 
2697
                                          lv->vg->name, lv->name);
 
2698
                                r = 0;
 
2699
                                goto out;
 
2700
                        }
 
2701
                }
2529
2702
        }
 
2703
 
 
2704
out:
2530
2705
        alloc_destroy(ah);
2531
2706
        return r;
2532
2707
}
2624
2799
        struct lv_segment *seg;
2625
2800
        uint32_t s;
2626
2801
 
2627
 
        if (lv_is_cow(lv) && lv_is_virtual_origin(org = origin_from_cow(lv)))
 
2802
        if (lv_is_cow(lv) && lv_is_virtual_origin(org = origin_from_cow(lv))) {
2628
2803
                if (!fn(cmd, org, data))
2629
2804
                        return_0;
 
2805
                if (!for_each_sub_lv(cmd, org, fn, data))
 
2806
                        return_0;
 
2807
        }
2630
2808
 
2631
2809
        dm_list_iterate_items(seg, &lv->segments) {
2632
 
                if (seg->log_lv && !fn(cmd, seg->log_lv, data))
2633
 
                        return_0;
 
2810
                if (seg->log_lv) {
 
2811
                        if (!fn(cmd, seg->log_lv, data))
 
2812
                                return_0;
 
2813
                        if (!for_each_sub_lv(cmd, seg->log_lv, fn, data))
 
2814
                                return_0;
 
2815
                }
 
2816
 
 
2817
                if (seg->pool_lv) {
 
2818
                        if (!fn(cmd, seg->pool_lv, data))
 
2819
                                return_0;
 
2820
                        if (!for_each_sub_lv(cmd, seg->pool_lv, fn, data))
 
2821
                                return_0;
 
2822
                }
 
2823
 
 
2824
                if (seg->metadata_lv) {
 
2825
                        if (!fn(cmd, seg->metadata_lv, data))
 
2826
                                return_0;
 
2827
                        if (!for_each_sub_lv(cmd, seg->metadata_lv, fn, data))
 
2828
                                return_0;
 
2829
                }
 
2830
 
2634
2831
                for (s = 0; s < seg->area_count; s++) {
2635
2832
                        if (seg_type(seg, s) != AREA_LV)
2636
2833
                                continue;
2714
2911
        if (!vg_write(vg))
2715
2912
                return 0;
2716
2913
 
2717
 
 
2718
 
        if (!suspend_lvs(cmd, &lvs_changed)) {
2719
 
                vg_revert(vg);
 
2914
        if (!suspend_lvs(cmd, &lvs_changed, vg))
2720
2915
                goto_out;
2721
 
        }
2722
2916
 
2723
2917
        if (!(r = vg_commit(vg)))
2724
2918
                stack;
2837
3031
 
2838
3032
        if (!link_lv_to_vg(vg, lv))
2839
3033
                goto_bad;
 
3034
 
 
3035
        if (!lv_set_creation(lv, NULL, 0))
 
3036
                goto_bad;
2840
3037
 
2841
3038
        if (fi->fmt->ops->lv_setup && !fi->fmt->ops->lv_setup(fi, lv))
2842
3039
                goto_bad;
2880
3077
        struct dm_list *parallel_areas;
2881
3078
        struct seg_pvs *spvs;
2882
3079
        uint32_t current_le = 0;
2883
 
        struct lv_segment * uninitialized_var(seg);
 
3080
        uint32_t raid_multiple;
 
3081
        struct lv_segment *seg = first_seg(lv);
2884
3082
 
2885
3083
        if (!(parallel_areas = dm_pool_alloc(cmd->mem, sizeof(*parallel_areas)))) {
2886
3084
                log_error("parallel_areas allocation failed");
2919
3117
                        return_NULL;
2920
3118
 
2921
3119
                current_le = spvs->le + spvs->len;
2922
 
        } while (current_le < lv->le_count);
 
3120
                raid_multiple = (seg->segtype->parity_devs) ?
 
3121
                        seg->area_count - seg->segtype->parity_devs : 1;
 
3122
        } while ((current_le * raid_multiple) < lv->le_count);
2923
3123
 
2924
3124
        /* FIXME Merge adjacent segments with identical PV lists (avoids need for contiguous allocation attempts between successful allocations) */
2925
3125
 
2980
3180
{
2981
3181
        struct volume_group *vg;
2982
3182
        struct lvinfo info;
 
3183
        struct logical_volume *format1_origin = NULL;
 
3184
        int format1_reload_required = 0;
 
3185
        int visible;
 
3186
        struct logical_volume *pool_lv = NULL;
2983
3187
 
2984
3188
        vg = lv->vg;
2985
3189
 
2986
3190
        if (!vg_check_status(vg, LVM_WRITE))
2987
 
                return 0;
 
3191
                return_0;
2988
3192
 
2989
3193
        if (lv_is_origin(lv)) {
2990
3194
                log_error("Can't remove logical volume \"%s\" under snapshot",
3010
3214
                return 0;
3011
3215
        }
3012
3216
 
 
3217
        if (lv_is_thin_pool_data(lv) || lv_is_thin_pool_metadata(lv)) {
 
3218
                log_error("Can't remove logical volume %s used by a thin pool.",
 
3219
                          lv->name);
 
3220
                return 0;
 
3221
        } else if (lv_is_thin_volume(lv))
 
3222
                pool_lv = first_seg(lv)->pool_lv;
 
3223
 
3013
3224
        if (lv->status & LOCKED) {
3014
3225
                log_error("Can't remove locked LV %s", lv->name);
3015
3226
                return 0;
3018
3229
        /* FIXME Ensure not referred to by another existing LVs */
3019
3230
 
3020
3231
        if (lv_info(cmd, lv, 0, &info, 1, 0)) {
3021
 
                if (info.open_count) {
3022
 
                        log_error("Can't remove open logical volume \"%s\"",
3023
 
                                  lv->name);
3024
 
                        return 0;
3025
 
                }
 
3232
                if (!lv_check_not_in_use(cmd, lv, &info))
 
3233
                        return_0;
3026
3234
 
3027
 
                if (lv_is_active(lv) && (force == PROMPT) &&
 
3235
                if ((force == PROMPT) &&
3028
3236
                    lv_is_visible(lv) &&
 
3237
                    lv_is_active(lv) &&
3029
3238
                    yes_no_prompt("Do you really want to remove active "
3030
3239
                                  "%slogical volume %s? [y/n]: ",
3031
3240
                                  vg_is_clustered(vg) ? "clustered " : "",
3039
3248
                return 0;
3040
3249
 
3041
3250
        if (lv_is_cow(lv)) {
 
3251
                /* Old format1 code */
 
3252
                if (!(lv->vg->fid->fmt->features & FMT_MDAS))
 
3253
                        format1_origin = origin_from_cow(lv);
 
3254
 
3042
3255
                log_verbose("Removing snapshot %s", lv->name);
3043
3256
                /* vg_remove_snapshot() will preload origin/former snapshots */
3044
3257
                if (!vg_remove_snapshot(lv))
3052
3265
                return 0;
3053
3266
        }
3054
3267
 
 
3268
        /* Clear thin pool stacked messages */
 
3269
        if (pool_lv && !pool_has_message(first_seg(pool_lv), lv, 0) &&
 
3270
            !update_pool_lv(pool_lv, 1)) {
 
3271
                log_error("Failed to update thin pool %s.", pool_lv->name);
 
3272
                return 0;
 
3273
        }
 
3274
 
 
3275
        visible = lv_is_visible(lv);
 
3276
 
3055
3277
        log_verbose("Releasing logical volume \"%s\"", lv->name);
3056
3278
        if (!lv_remove(lv)) {
3057
3279
                log_error("Error releasing logical volume \"%s\"", lv->name);
3058
3280
                return 0;
3059
3281
        }
3060
3282
 
 
3283
        /*
 
3284
         * Old format1 code: If no snapshots left reload without -real.
 
3285
         */
 
3286
        if (format1_origin && !lv_is_origin(format1_origin)) {
 
3287
                log_warn("WARNING: Support for snapshots with old LVM1-style metadata is deprecated.");
 
3288
                log_warn("WARNING: Please use lvconvert to update to lvm2 metadata at your convenience.");
 
3289
                format1_reload_required = 1;
 
3290
        }
 
3291
 
3061
3292
        /* store it on disks */
3062
3293
        if (!vg_write(vg))
3063
3294
                return_0;
3064
3295
 
 
3296
        /* format1 */
 
3297
        if (format1_reload_required && !suspend_lv(cmd, format1_origin))
 
3298
                log_error("Failed to refresh %s without snapshot.", format1_origin->name);
 
3299
 
3065
3300
        if (!vg_commit(vg))
3066
3301
                return_0;
 
3302
 
 
3303
        /* format1 */
 
3304
        if (format1_reload_required && !resume_lv(cmd, format1_origin)) {
 
3305
                log_error("Failed to resume %s.", format1_origin->name);
 
3306
                return 0;
 
3307
        }
 
3308
 
 
3309
        /* Release unneeded blocks in thin pool */
 
3310
        /* TODO: defer when multiple LVs relased at once */
 
3311
        if (pool_lv && !update_pool_lv(pool_lv, 1)) {
 
3312
                log_error("Failed to update thin pool %s.", pool_lv->name);
 
3313
                return 0;
 
3314
        }
3067
3315
 
3068
3316
        backup(vg);
3069
3317
 
3070
 
        if (lv_is_visible(lv))
 
3318
        if (visible)
3071
3319
                log_print("Logical volume \"%s\" successfully removed", lv->name);
3072
3320
 
3073
3321
        return 1;
3079
3327
int lv_remove_with_dependencies(struct cmd_context *cmd, struct logical_volume *lv,
3080
3328
                                const force_t force, unsigned level)
3081
3329
{
 
3330
        percent_t snap_percent;
3082
3331
        struct dm_list *snh, *snht;
 
3332
        struct seg_list *sl, *tsl;
 
3333
        struct lvinfo info;
3083
3334
 
3084
3335
        if (lv_is_cow(lv)) {
3085
 
                /* A merging snapshot cannot be removed directly */
 
3336
                /*
 
3337
                 * A merging snapshot cannot be removed directly unless
 
3338
                 * it has been invalidated or failed merge removal is requested.
 
3339
                 */
3086
3340
                if (lv_is_merging_cow(lv) && !level) {
3087
 
                        log_error("Can't remove merging snapshot logical volume \"%s\"",
3088
 
                                  lv->name);
3089
 
                        return 0;
 
3341
                        if (lv_info(lv->vg->cmd, lv, 0, &info, 1, 0) &&
 
3342
                            info.exists && info.live_table) {
 
3343
                                if (!lv_snapshot_percent(lv, &snap_percent)) {
 
3344
                                        log_error("Failed to obtain merging snapshot progress percentage for logical volume %s.",
 
3345
                                                  lv->name);
 
3346
                                        return 0;
 
3347
                                }
 
3348
                                if ((snap_percent != PERCENT_INVALID) &&
 
3349
                                     (snap_percent != PERCENT_MERGE_FAILED)) {
 
3350
                                        log_error("Can't remove merging snapshot logical volume \"%s\"",
 
3351
                                                  lv->name);
 
3352
                                        return 0;
 
3353
                                } else if ((snap_percent == PERCENT_MERGE_FAILED) &&
 
3354
                                         (force == PROMPT) &&
 
3355
                                         yes_no_prompt("Removing snapshot \"%s\" that failed to merge may leave origin \"%s\" inconsistent. "
 
3356
                                                       "Proceed? [y/n]: ", lv->name, origin_from_cow(lv)->name) == 'n') {
 
3357
                                        log_error("Logical volume %s not removed.", lv->name);
 
3358
                                        return 0;
 
3359
                                }
 
3360
                        }
3090
3361
                }
3091
3362
        }
3092
3363
 
3093
3364
        if (lv_is_origin(lv)) {
3094
 
                /* remove snapshot LVs first */
3095
 
                dm_list_iterate_safe(snh, snht, &lv->snapshot_segs) {
 
3365
                /* Remove snapshot LVs first */
 
3366
                if ((force == PROMPT) &&
 
3367
                    /* Active snapshot already needs to confirm each active LV */
 
3368
                    !lv_is_active(lv) &&
 
3369
                    yes_no_prompt("Removing origin %s will also remove %u "
 
3370
                                  "snapshots(s). Proceed? [y/n]: ",
 
3371
                                  lv->name, lv->origin_count) == 'n') {
 
3372
                        log_error("Logical volume %s not removed.", lv->name);
 
3373
                        return 0;
 
3374
                }
 
3375
 
 
3376
                dm_list_iterate_safe(snh, snht, &lv->snapshot_segs)
3096
3377
                        if (!lv_remove_with_dependencies(cmd, dm_list_struct_base(snh, struct lv_segment,
3097
3378
                                                                                  origin_list)->cow,
3098
3379
                                                         force, level + 1))
3099
 
                                return 0;
 
3380
                                return_0;
 
3381
        }
 
3382
 
 
3383
        if (lv_is_used_thin_pool(lv)) {
 
3384
                /* Remove thin LVs first */
 
3385
                if ((force == PROMPT) &&
 
3386
                    yes_no_prompt("Removing pool %s will also remove %u "
 
3387
                                  "thin volume(s). OK? [y/n]: ", lv->name,
 
3388
                                  /* Note: Snaphosts not included */
 
3389
                                  dm_list_size(&lv->segs_using_this_lv)) == 'n') {
 
3390
                        log_error("Logical volume %s not removed.", lv->name);
 
3391
                        return 0;
3100
3392
                }
 
3393
 
 
3394
                dm_list_iterate_items_safe(sl, tsl, &lv->segs_using_this_lv)
 
3395
                        if (!lv_remove_with_dependencies(cmd, sl->seg->lv,
 
3396
                                                         force, level + 1))
 
3397
                                return_0;
3101
3398
        }
3102
3399
 
3103
3400
        return lv_remove_single(cmd, lv, force);
3301
3598
        return 1;
3302
3599
}
3303
3600
 
3304
 
static int _move_lv_segments(struct logical_volume *lv_to,
3305
 
                             struct logical_volume *lv_from,
3306
 
                             uint64_t set_status, uint64_t reset_status)
 
3601
int move_lv_segments(struct logical_volume *lv_to,
 
3602
                     struct logical_volume *lv_from,
 
3603
                     uint64_t set_status, uint64_t reset_status)
3307
3604
{
3308
3605
        struct lv_segment *seg;
3309
3606
 
3310
 
        dm_list_iterate_items(seg, &lv_to->segments) {
 
3607
        dm_list_iterate_items(seg, &lv_to->segments)
3311
3608
                if (seg->origin) {
3312
 
                        log_error("Can't move snapshot segment");
 
3609
                        log_error("Can't move snapshot segment.");
3313
3610
                        return 0;
3314
3611
                }
3315
 
        }
3316
3612
 
3317
 
        if (!dm_list_empty(&lv_from->segments))
3318
 
                lv_to->segments = lv_from->segments;
3319
 
        lv_to->segments.n->p = &lv_to->segments;
3320
 
        lv_to->segments.p->n = &lv_to->segments;
 
3613
        dm_list_init(&lv_to->segments);
 
3614
        dm_list_splice(&lv_to->segments, &lv_from->segments);
3321
3615
 
3322
3616
        dm_list_iterate_items(seg, &lv_to->segments) {
3323
3617
                seg->lv = lv_to;
3325
3619
                seg->status |= set_status;
3326
3620
        }
3327
3621
 
3328
 
        dm_list_init(&lv_from->segments);
3329
 
 
3330
3622
        lv_to->le_count = lv_from->le_count;
3331
3623
        lv_to->size = lv_from->size;
3332
3624
 
3367
3659
        if (!lv_empty(parent))
3368
3660
                return_0;
3369
3661
 
3370
 
        if (!_move_lv_segments(parent, layer_lv, 0, 0))
 
3662
        if (!move_lv_segments(parent, layer_lv, 0, 0))
3371
3663
                return_0;
3372
3664
 
3373
3665
        /* Replace the empty layer with error segment */
3374
3666
        segtype = get_segtype_from_string(lv->vg->cmd, "error");
3375
 
        if (!lv_add_virtual_segment(layer_lv, 0, parent->le_count, segtype))
 
3667
        if (!lv_add_virtual_segment(layer_lv, 0, parent->le_count, segtype, NULL))
3376
3668
                return_0;
3377
3669
 
3378
3670
        return 1;
3425
3717
 
3426
3718
                segtype = get_segtype_from_string(cmd, "error");
3427
3719
 
3428
 
                if (!lv_add_virtual_segment(layer_lv, 0, lv_where->le_count, segtype)) {
 
3720
                if (!lv_add_virtual_segment(layer_lv, 0, lv_where->le_count, segtype, NULL)) {
3429
3721
                        log_error("Creation of transient LV %s for mirror conversion in VG %s failed.", name, lv_where->vg->name);
3430
3722
                        return NULL;
3431
3723
                }
3457
3749
        log_very_verbose("Inserting layer %s for %s",
3458
3750
                         layer_lv->name, lv_where->name);
3459
3751
 
3460
 
        if (!_move_lv_segments(layer_lv, lv_where, 0, 0))
 
3752
        if (!move_lv_segments(layer_lv, lv_where, 0, 0))
3461
3753
                return_NULL;
3462
3754
 
3463
3755
        if (!(segtype = get_segtype_from_string(cmd, "striped")))
3464
3756
                return_NULL;
3465
3757
 
3466
3758
        /* allocate a new linear segment */
3467
 
        if (!(mapseg = alloc_lv_segment(cmd->mem, segtype,
3468
 
                                        lv_where, 0, layer_lv->le_count,
3469
 
                                        status, 0, NULL, 1, layer_lv->le_count,
 
3759
        if (!(mapseg = alloc_lv_segment(segtype, lv_where, 0, layer_lv->le_count,
 
3760
                                        status, 0, NULL, NULL, 1, layer_lv->le_count,
3470
3761
                                        0, 0, 0, NULL)))
3471
3762
                return_NULL;
3472
3763
 
3477
3768
        /* add the new segment to the layer LV */
3478
3769
        dm_list_add(&lv_where->segments, &mapseg->list);
3479
3770
        lv_where->le_count = layer_lv->le_count;
3480
 
        lv_where->size = lv_where->le_count * lv_where->vg->extent_size;
 
3771
        lv_where->size = (uint64_t) lv_where->le_count * lv_where->vg->extent_size;
3481
3772
 
3482
3773
        return layer_lv;
3483
3774
}
3507
3798
                         seg->lv->vg->name, seg->lv->name);
3508
3799
 
3509
3800
        /* allocate a new segment */
3510
 
        if (!(mapseg = alloc_lv_segment(layer_lv->vg->cmd->mem, segtype,
3511
 
                                        layer_lv, layer_lv->le_count,
 
3801
        if (!(mapseg = alloc_lv_segment(segtype, layer_lv, layer_lv->le_count,
3512
3802
                                        seg->area_len, status, 0,
3513
 
                                        NULL, 1, seg->area_len, 0, 0, 0, seg)))
 
3803
                                        NULL, NULL, 1, seg->area_len, 0, 0, 0, seg)))
3514
3804
                return_0;
3515
3805
 
3516
3806
        /* map the new segment to the original underlying are */
3520
3810
        /* add the new segment to the layer LV */
3521
3811
        dm_list_add(&layer_lv->segments, &mapseg->list);
3522
3812
        layer_lv->le_count += seg->area_len;
3523
 
        layer_lv->size += seg->area_len * layer_lv->vg->extent_size;
 
3813
        layer_lv->size += (uint64_t) seg->area_len * layer_lv->vg->extent_size;
3524
3814
 
3525
3815
        /* map the original area to the new segment */
3526
3816
        if (!set_lv_segment_area_lv(seg, s, layer_lv, mapseg->le, 0))
3732
4022
        dev_flush(dev);
3733
4023
 
3734
4024
        if (!dev_close_immediate(dev))
3735
 
                stack;
 
4025
                stack;
3736
4026
 
3737
4027
        return 1;
3738
4028
}
3739
4029
 
3740
 
 
3741
4030
static struct logical_volume *_create_virtual_origin(struct cmd_context *cmd,
3742
4031
                                                     struct volume_group *vg,
3743
4032
                                                     const char *lv_name,
3766
4055
                return_NULL;
3767
4056
 
3768
4057
        if (!lv_extend(lv, segtype, 1, 0, 1, 0, voriginextents,
3769
 
                       NULL, ALLOC_INHERIT))
 
4058
                       NULL, NULL, ALLOC_INHERIT))
3770
4059
                return_NULL;
3771
4060
 
3772
4061
        /* store vg on disk(s) */
3778
4067
        return lv;
3779
4068
}
3780
4069
 
3781
 
int lv_create_single(struct volume_group *vg,
3782
 
                     struct lvcreate_params *lp)
 
4070
/* Thin notes:
 
4071
 * If lp->thin OR lp->activate is AY*, activate the pool if not already active.
 
4072
 * If lp->thin, create thin LV within the pool - as a snapshot if lp->snapshot.
 
4073
 *   If lp->activate is AY*, activate it.
 
4074
 *   If lp->activate was AN* and the pool was originally inactive, deactivate it.
 
4075
 */
 
4076
static struct logical_volume *_lv_create_an_lv(struct volume_group *vg, struct lvcreate_params *lp,
 
4077
                                               const char *new_lv_name)
3783
4078
{
3784
4079
        struct cmd_context *cmd = vg->cmd;
3785
4080
        uint32_t size_rest;
3786
4081
        uint64_t status = UINT64_C(0);
3787
4082
        struct logical_volume *lv, *org = NULL;
 
4083
        struct logical_volume *pool_lv;
 
4084
        struct lv_list *lvl;
3788
4085
        int origin_active = 0;
3789
4086
        struct lvinfo info;
3790
4087
 
3791
 
        if (lp->lv_name && find_lv_in_vg(vg, lp->lv_name)) {
 
4088
        if (new_lv_name && find_lv_in_vg(vg, new_lv_name)) {
3792
4089
                log_error("Logical volume \"%s\" already exists in "
3793
 
                          "volume group \"%s\"", lp->lv_name, lp->vg_name);
3794
 
                return 0;
 
4090
                          "volume group \"%s\"", new_lv_name, lp->vg_name);
 
4091
                return NULL;
3795
4092
        }
3796
4093
 
3797
4094
        if (vg_max_lv_reached(vg)) {
3798
4095
                log_error("Maximum number of logical volumes (%u) reached "
3799
4096
                          "in volume group %s", vg->max_lv, vg->name);
3800
 
                return 0;
 
4097
                return NULL;
3801
4098
        }
3802
4099
 
3803
4100
        if ((segtype_is_mirrored(lp->segtype) ||
3804
 
             segtype_is_raid(lp->segtype)) &&
 
4101
             segtype_is_raid(lp->segtype) || segtype_is_thin(lp->segtype)) &&
3805
4102
            !(vg->fid->fmt->features & FMT_SEGMENTS)) {
3806
 
                log_error("Metadata does not support %s.",
3807
 
                          segtype_is_raid(lp->segtype) ? "RAID" : "mirroring");
3808
 
                return 0;
 
4103
                log_error("Metadata does not support %s segments.",
 
4104
                          lp->segtype->name);
 
4105
                return NULL;
3809
4106
        }
3810
4107
 
3811
4108
        if (lp->read_ahead != DM_READ_AHEAD_AUTO &&
3813
4110
            (vg->fid->fmt->features & FMT_RESTRICTED_READAHEAD) &&
3814
4111
            (lp->read_ahead < 2 || lp->read_ahead > 120)) {
3815
4112
                log_error("Metadata only supports readahead values between 2 and 120.");
3816
 
                return 0;
 
4113
                return NULL;
3817
4114
        }
3818
4115
 
3819
4116
        if (lp->stripe_size > vg->extent_size) {
3830
4127
            (lp->stripe_size > STRIPE_SIZE_MAX)) {
3831
4128
                log_error("Stripe size may not exceed %s",
3832
4129
                          display_size(cmd, (uint64_t) STRIPE_SIZE_MAX));
3833
 
                return 0;
 
4130
                return NULL;
3834
4131
        }
3835
4132
 
3836
4133
        if ((size_rest = lp->extents % lp->stripes)) {
3840
4137
                lp->extents = lp->extents - size_rest + lp->stripes;
3841
4138
        }
3842
4139
 
3843
 
        if (lp->zero && !activation()) {
 
4140
        /* Does LV need to be zeroed?  Thin handles this as a per-pool in-kernel setting. */
 
4141
        if (lp->zero && !segtype_is_thin(lp->segtype) && !activation()) {
3844
4142
                log_error("Can't wipe start of new LV without using "
3845
4143
                          "device-mapper kernel driver");
3846
 
                return 0;
 
4144
                return NULL;
3847
4145
        }
3848
4146
 
3849
4147
        status |= lp->permission | VISIBLE_LV;
3850
4148
 
3851
 
        if (lp->snapshot) {
 
4149
        if (lp->snapshot && lp->thin) {
 
4150
                if (!(org = find_lv(vg, lp->origin))) {
 
4151
                        log_error("Couldn't find origin volume '%s'.",
 
4152
                                  lp->origin);
 
4153
                        return NULL;
 
4154
                }
 
4155
 
 
4156
                if (org->status & LOCKED) {
 
4157
                        log_error("Snapshots of locked devices are not supported.");
 
4158
                        return NULL;
 
4159
                }
 
4160
 
 
4161
                lp->voriginextents = org->le_count;
 
4162
        } else if (lp->snapshot) {
3852
4163
                if (!activation()) {
3853
4164
                        log_error("Can't create snapshot without using "
3854
4165
                                  "device-mapper kernel driver");
3855
 
                        return 0;
 
4166
                        return NULL;
3856
4167
                }
3857
4168
 
3858
4169
                /* Must zero cow */
3865
4176
                        if (!(org = find_lv(vg, lp->origin))) {
3866
4177
                                log_error("Couldn't find origin volume '%s'.",
3867
4178
                                          lp->origin);
3868
 
                                return 0;
 
4179
                                return NULL;
3869
4180
                        }
3870
4181
                        if (lv_is_virtual_origin(org)) {
3871
4182
                                log_error("Can't share virtual origins. "
3872
4183
                                          "Use --virtualsize.");
3873
 
                                return 0;
 
4184
                                return NULL;
3874
4185
                        }
3875
4186
                        if (lv_is_cow(org)) {
3876
4187
                                log_error("Snapshots of snapshots are not "
3877
4188
                                          "supported yet.");
3878
 
                                return 0;
 
4189
                                return NULL;
3879
4190
                        }
3880
4191
                        if (org->status & LOCKED) {
3881
4192
                                log_error("Snapshots of locked devices are not "
3882
4193
                                          "supported yet");
3883
 
                                return 0;
 
4194
                                return NULL;
3884
4195
                        }
3885
4196
                        if (lv_is_merging_origin(org)) {
3886
4197
                                log_error("Snapshots of an origin that has a "
3887
4198
                                          "merging snapshot is not supported");
3888
 
                                return 0;
3889
 
                        }
 
4199
                                return NULL;
 
4200
                        }
 
4201
 
 
4202
                        if (lv_is_thin_type(org) && !lv_is_thin_volume(org)) {
 
4203
                                log_error("Snapshots of thin pool %sdevices "
 
4204
                                          "are not supported.",
 
4205
                                          lv_is_thin_pool_data(org) ? "data " :
 
4206
                                          lv_is_thin_pool_metadata(org) ?
 
4207
                                          "metadata " : "");
 
4208
                                return NULL;
 
4209
                        }
 
4210
 
3890
4211
                        if ((org->status & MIRROR_IMAGE) ||
3891
4212
                            (org->status & MIRROR_LOG)) {
3892
4213
                                log_error("Snapshots of mirror %ss "
3893
4214
                                          "are not supported",
3894
4215
                                          (org->status & MIRROR_LOG) ?
3895
4216
                                          "log" : "image");
3896
 
                                return 0;
 
4217
                                return NULL;
3897
4218
                        }
3898
4219
 
3899
4220
                        if (!lv_info(cmd, org, 0, &info, 0, 0)) {
3900
 
                                log_error("Check for existence of snapshot "
 
4221
                                log_error("Check for existence of active snapshot "
3901
4222
                                          "origin '%s' failed.", org->name);
3902
 
                                return 0;
 
4223
                                return NULL;
3903
4224
                        }
3904
4225
                        origin_active = info.exists;
3905
4226
 
3907
4228
                            !lv_is_active_exclusive_locally(org)) {
3908
4229
                                log_error("%s must be active exclusively to"
3909
4230
                                          " create snapshot", org->name);
3910
 
                                return 0;
 
4231
                                return NULL;
3911
4232
                        }
3912
4233
                }
3913
4234
        }
3914
4235
 
3915
 
        if (!lp->extents) {
 
4236
        if (!seg_is_thin_volume(lp) && !lp->extents) {
3916
4237
                log_error("Unable to create new logical volume with no extents");
3917
 
                return 0;
3918
 
        }
3919
 
 
3920
 
        if (lp->snapshot && (lp->extents * vg->extent_size < 2 * lp->chunk_size)) {
 
4238
                return NULL;
 
4239
        }
 
4240
 
 
4241
        if (seg_is_thin_pool(lp) &&
 
4242
            ((uint64_t)lp->extents * vg->extent_size < lp->chunk_size)) {
 
4243
                log_error("Unable to create thin pool smaller than 1 chunk.");
 
4244
                return NULL;
 
4245
        }
 
4246
 
 
4247
        if (lp->snapshot && !lp->thin && ((uint64_t)lp->extents * vg->extent_size < 2 * lp->chunk_size)) {
3921
4248
                log_error("Unable to create a snapshot smaller than 2 chunks.");
3922
 
                return 0;
 
4249
                return NULL;
3923
4250
        }
3924
4251
 
3925
4252
        if (!seg_is_virtual(lp) &&
3927
4254
                log_error("Volume group \"%s\" has insufficient free space "
3928
4255
                          "(%u extents): %u required.",
3929
4256
                          vg->name, vg->free_count, lp->extents);
3930
 
                return 0;
 
4257
                return NULL;
3931
4258
        }
3932
4259
 
3933
4260
        if (lp->stripes > dm_list_size(lp->pvh) && lp->alloc != ALLOC_ANYWHERE) {
3934
4261
                log_error("Number of stripes (%u) must not exceed "
3935
4262
                          "number of physical volumes (%d)", lp->stripes,
3936
4263
                          dm_list_size(lp->pvh));
3937
 
                return 0;
 
4264
                return NULL;
3938
4265
        }
3939
4266
 
3940
 
        if ((segtype_is_mirrored(lp->segtype) ||
3941
 
             segtype_is_raid(lp->segtype)) && !activation()) {
 
4267
        if (!activation() &&
 
4268
            (seg_is_mirrored(lp) ||
 
4269
             seg_is_raid(lp) ||
 
4270
             seg_is_thin_pool(lp))) {
 
4271
                /*
 
4272
                 * FIXME: For thin pool add some code to allow delayed
 
4273
                 * initialization of empty thin pool volume.
 
4274
                 * i.e. using some LV flag, fake message,...
 
4275
                 * and testing for metadata pool header signature?
 
4276
                 */
3942
4277
                log_error("Can't create %s without using "
3943
4278
                          "device-mapper kernel driver.",
3944
4279
                          segtype_is_raid(lp->segtype) ? lp->segtype->name :
3945
 
                          "mirror");
3946
 
                return 0;
 
4280
                          segtype_is_mirrored(lp->segtype) ?  "mirror" :
 
4281
                          "thin pool volume");
 
4282
                return NULL;
3947
4283
        }
3948
4284
 
3949
4285
        /* The snapshot segment gets created later */
3950
 
        if (lp->snapshot &&
 
4286
        if (lp->snapshot && !lp->thin &&
3951
4287
            !(lp->segtype = get_segtype_from_string(cmd, "striped")))
3952
 
                return_0;
 
4288
                return_NULL;
3953
4289
 
3954
4290
        if (!archive(vg))
3955
 
                return 0;
 
4291
                return_NULL;
3956
4292
 
3957
4293
        if (!dm_list_empty(&lp->tags)) {
3958
4294
                if (!(vg->fid->fmt->features & FMT_TAGS)) {
3959
4295
                        log_error("Volume group %s does not support tags",
3960
4296
                                  vg->name);
 
4297
                        return NULL;
 
4298
                }
 
4299
        }
 
4300
 
 
4301
        if (seg_is_thin_volume(lp) &&
 
4302
            ((lp->activate == CHANGE_AY) ||
 
4303
             (lp->activate == CHANGE_AE) ||
 
4304
             (lp->activate == CHANGE_ALY))) {
 
4305
                /* Ensure all stacked messages are submitted */
 
4306
                if (!(lvl = find_lv_in_vg(vg, lp->pool))) {
 
4307
                        log_error("Unable to find existing pool LV %s in VG %s.",
 
4308
                                  lp->pool, vg->name);
3961
4309
                        return 0;
3962
4310
                }
 
4311
                if (!update_pool_lv(lvl->lv, 1))
 
4312
                        return_0;
3963
4313
        }
3964
4314
 
3965
4315
        if (segtype_is_mirrored(lp->segtype) || segtype_is_raid(lp->segtype)) {
3971
4321
                                 lp->segtype->name);
3972
4322
                        status |= LV_NOTSYNCED;
3973
4323
                }
 
4324
 
 
4325
                lp->region_size = adjusted_mirror_region_size(vg->extent_size,
 
4326
                                                              lp->extents,
 
4327
                                                              lp->region_size);
3974
4328
        }
3975
4329
 
3976
 
        if (!(lv = lv_create_empty(lp->lv_name ? lp->lv_name : "lvol%d", NULL,
 
4330
        if (!(lv = lv_create_empty(new_lv_name ? : "lvol%d", NULL,
3977
4331
                                   status, lp->alloc, vg)))
3978
 
                return_0;
 
4332
                return_NULL;
3979
4333
 
3980
4334
        if (lp->read_ahead != lv->read_ahead) {
3981
4335
                log_verbose("Setting read ahead sectors");
3982
4336
                lv->read_ahead = lp->read_ahead;
3983
4337
        }
3984
4338
 
3985
 
        if (lp->minor >= 0) {
 
4339
        if (!seg_is_thin_pool(lp) && lp->minor >= 0) {
3986
4340
                lv->major = lp->major;
3987
4341
                lv->minor = lp->minor;
3988
4342
                lv->status |= FIXED_MINOR;
3990
4344
                            lv->minor);
3991
4345
        }
3992
4346
 
3993
 
        if (!dm_list_empty(&lp->tags))
3994
 
                dm_list_splice(&lv->tags, &lp->tags);
3995
 
 
3996
 
        lp->region_size = adjusted_mirror_region_size(vg->extent_size,
3997
 
                                                      lp->extents,
3998
 
                                                      lp->region_size);
 
4347
        dm_list_splice(&lv->tags, &lp->tags);
3999
4348
 
4000
4349
        if (!lv_extend(lv, lp->segtype,
4001
4350
                       lp->stripes, lp->stripe_size,
4002
 
                       lp->mirrors, lp->region_size,
4003
 
                       lp->extents, lp->pvh, lp->alloc))
4004
 
                return_0;
4005
 
 
 
4351
                       lp->mirrors,
 
4352
                       seg_is_thin_pool(lp) ? lp->poolmetadataextents : lp->region_size,
 
4353
                       seg_is_thin_volume(lp) ? lp->voriginextents : lp->extents,
 
4354
                       seg_is_thin_volume(lp) ? (org ? org->name : lp->pool) : NULL, lp->pvh, lp->alloc))
 
4355
                return_NULL;
 
4356
 
 
4357
        if (seg_is_thin_pool(lp)) {
 
4358
                first_seg(lv)->zero_new_blocks = lp->zero ? 1 : 0;
 
4359
                first_seg(lv)->chunk_size = lp->chunk_size;
 
4360
                /* FIXME: use lowwatermark  via lvm.conf global for all thinpools ? */
 
4361
                first_seg(lv)->low_water_mark = 0;
 
4362
        } else if (seg_is_thin_volume(lp)) {
 
4363
                pool_lv = first_seg(lv)->pool_lv;
 
4364
 
 
4365
                if (!(first_seg(lv)->device_id =
 
4366
                      get_free_pool_device_id(first_seg(pool_lv)))) {
 
4367
                        stack;
 
4368
                        goto revert_new_lv;
 
4369
                }
 
4370
 
 
4371
                if (!attach_pool_message(first_seg(pool_lv),
 
4372
                                         DM_THIN_MESSAGE_CREATE_THIN, lv, 0, 0)) {
 
4373
                        stack;
 
4374
                        goto revert_new_lv;
 
4375
                }
 
4376
        }
 
4377
 
 
4378
        /* FIXME Log allocation and attachment should have happened inside lv_extend. */
4006
4379
        if (lp->log_count &&
4007
4380
            !seg_is_raid(first_seg(lv)) && seg_is_mirrored(first_seg(lv))) {
4008
4381
                if (!add_mirror_log(cmd, lv, lp->log_count,
4015
4388
 
4016
4389
        /* store vg on disk(s) */
4017
4390
        if (!vg_write(vg) || !vg_commit(vg))
4018
 
                return_0;
 
4391
                return_NULL;
4019
4392
 
4020
4393
        backup(vg);
4021
4394
 
4024
4397
                goto out;
4025
4398
        }
4026
4399
 
4027
 
        init_dmeventd_monitor(lp->activation_monitoring);
4028
 
 
4029
 
        if (lp->snapshot) {
 
4400
        if (seg_is_thin(lp)) {
 
4401
                /* For snapshot, suspend active thin origin first */
 
4402
                if (org && lv_is_active(org)) {
 
4403
                        if (!pool_below_threshold(first_seg(first_seg(org)->pool_lv))) {
 
4404
                                log_error("Cannot create thin snapshot. Pool %s/%s is filled "
 
4405
                                          "over the autoextend threshold.",
 
4406
                                          org->vg->name, first_seg(org)->pool_lv->name);
 
4407
                                goto revert_new_lv;
 
4408
                        }
 
4409
                        if (!suspend_lv_origin(cmd, org)) {
 
4410
                                log_error("Failed to suspend thin snapshot origin %s/%s.",
 
4411
                                          org->vg->name, org->name);
 
4412
                                goto revert_new_lv;
 
4413
                        }
 
4414
                        if (!resume_lv_origin(cmd, org)) { /* deptree updates thin-pool */
 
4415
                                log_error("Failed to resume thin snapshot origin %s/%s.",
 
4416
                                          org->vg->name, org->name);
 
4417
                                goto revert_new_lv;
 
4418
                        }
 
4419
                        /* At this point remove pool messages, snapshot is active */
 
4420
                        if (!update_pool_lv(first_seg(org)->pool_lv, 0)) {
 
4421
                                stack;
 
4422
                                goto deactivate_and_revert_new_lv;
 
4423
                        }
 
4424
                }
 
4425
                if (((lp->activate == CHANGE_AY) ||
 
4426
                     (lp->activate == CHANGE_AE) ||
 
4427
                     (lp->activate == CHANGE_ALY))) {
 
4428
                        /* At this point send message to kernel thin mda */
 
4429
                        pool_lv = lv_is_thin_pool(lv) ? lv : first_seg(lv)->pool_lv;
 
4430
                        if (!update_pool_lv(pool_lv, 1)) {
 
4431
                                stack;
 
4432
                                goto deactivate_and_revert_new_lv;
 
4433
                        }
 
4434
                        if (!activate_lv_excl(cmd, lv)) {
 
4435
                                log_error("Aborting. Failed to activate thin %s.",
 
4436
                                          lv->name);
 
4437
                                goto deactivate_and_revert_new_lv;
 
4438
                        }
 
4439
                }
 
4440
        } else if (lp->snapshot) {
4030
4441
                if (!activate_lv_excl(cmd, lv)) {
4031
4442
                        log_error("Aborting. Failed to activate snapshot "
4032
4443
                                  "exception store.");
4038
4449
                log_error("Failed to activate new LV.");
4039
4450
                if (lp->zero)
4040
4451
                        goto deactivate_and_revert_new_lv;
4041
 
                return 0;
 
4452
                return NULL;
4042
4453
        }
4043
4454
 
4044
 
        if (!lp->zero && !lp->snapshot)
 
4455
        if (!seg_is_thin(lp) && !lp->zero && !lp->snapshot)
4045
4456
                log_warn("WARNING: \"%s\" not zeroed", lv->name);
4046
 
        else if (!set_lv(cmd, lv, UINT64_C(0), 0)) {
 
4457
        else if ((!seg_is_thin(lp) ||
 
4458
                  (lv_is_thin_volume(lv) &&
 
4459
                   !first_seg(first_seg(lv)->pool_lv)->zero_new_blocks)) &&
 
4460
                 !set_lv(cmd, lv, UINT64_C(0), 0)) {
4047
4461
                log_error("Aborting. Failed to wipe %s.",
4048
4462
                          lp->snapshot ? "snapshot exception store" :
4049
4463
                                         "start of new LV");
4050
4464
                goto deactivate_and_revert_new_lv;
4051
4465
        }
4052
4466
 
4053
 
        if (lp->snapshot) {
 
4467
        if (lp->snapshot && !lp->thin) {
4054
4468
                /* Reset permission after zeroing */
4055
4469
                if (!(lp->permission & LVM_WRITE))
4056
4470
                        lv->status &= ~LVM_WRITE;
4059
4473
                if (!origin_active && !deactivate_lv(cmd, lv)) {
4060
4474
                        log_error("Aborting. Couldn't deactivate snapshot "
4061
4475
                                  "COW area. Manual intervention required.");
4062
 
                        return 0;
 
4476
                        return NULL;
4063
4477
                }
4064
4478
 
4065
4479
                /* A virtual origin must be activated explicitly. */
4067
4481
                    (!(org = _create_virtual_origin(cmd, vg, lv->name,
4068
4482
                                                    lp->permission,
4069
4483
                                                    lp->voriginextents)) ||
4070
 
                     !activate_lv(cmd, org))) {
 
4484
                     !activate_lv_excl(cmd, org))) {
4071
4485
                        log_error("Couldn't create virtual origin for LV %s",
4072
4486
                                  lv->name);
4073
4487
                        if (org && !lv_remove(org))
4085
4499
 
4086
4500
                /* store vg on disk(s) */
4087
4501
                if (!vg_write(vg))
4088
 
                        return_0;
 
4502
                        return_NULL;
4089
4503
 
4090
4504
                if (!suspend_lv(cmd, org)) {
4091
4505
                        log_error("Failed to suspend origin %s", org->name);
4092
4506
                        vg_revert(vg);
4093
 
                        return 0;
 
4507
                        return NULL;
4094
4508
                }
4095
4509
 
4096
4510
                if (!vg_commit(vg))
4097
 
                        return_0;
 
4511
                        return_NULL;
4098
4512
 
4099
4513
                if (!resume_lv(cmd, org)) {
4100
4514
                        log_error("Problem reactivating origin %s", org->name);
4101
 
                        return 0;
 
4515
                        return NULL;
4102
4516
                }
4103
4517
        }
4104
4518
        /* FIXME out of sequence */
4105
4519
        backup(vg);
4106
4520
 
4107
4521
out:
4108
 
        log_print("Logical volume \"%s\" created", lv->name);
4109
 
 
4110
 
        /*
4111
 
         * FIXME: as a sanity check we could try reading the
4112
 
         * last block of the device ?
4113
 
         */
4114
 
 
4115
 
        return 1;
 
4522
        return lv;
4116
4523
 
4117
4524
deactivate_and_revert_new_lv:
4118
4525
        if (!deactivate_lv(cmd, lv)) {
4119
4526
                log_error("Unable to deactivate failed new LV. "
4120
4527
                          "Manual intervention required.");
4121
 
                return 0;
 
4528
                return NULL;
4122
4529
        }
4123
4530
 
4124
4531
revert_new_lv:
4129
4536
        else
4130
4537
                backup(vg);
4131
4538
 
4132
 
        return 0;
4133
 
}
4134
 
 
 
4539
        return NULL;
 
4540
}
 
4541
 
 
4542
int lv_create_single(struct volume_group *vg,
 
4543
                     struct lvcreate_params *lp)
 
4544
{
 
4545
        struct logical_volume *lv;
 
4546
 
 
4547
        /* Create thin pool first if necessary */
 
4548
        if (lp->create_thin_pool) {
 
4549
                if (!seg_is_thin_pool(lp) &&
 
4550
                    !(lp->segtype = get_segtype_from_string(vg->cmd, "thin-pool")))
 
4551
                        return_0;
 
4552
 
 
4553
                if (!(lv = _lv_create_an_lv(vg, lp, lp->pool)))
 
4554
                        return_0;
 
4555
 
 
4556
                if (!lp->thin)
 
4557
                        goto out;
 
4558
 
 
4559
                lp->pool = lv->name;
 
4560
 
 
4561
                if (!(lp->segtype = get_segtype_from_string(vg->cmd, "thin")))
 
4562
                        return_0;
 
4563
        }
 
4564
 
 
4565
        if (!(lv = _lv_create_an_lv(vg, lp, lp->lv_name)))
 
4566
                return_0;
 
4567
 
 
4568
out:
 
4569
        log_print("Logical volume \"%s\" created", lv->name);
 
4570
 
 
4571
        return 1;
 
4572
}