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

« back to all changes in this revision

Viewing changes to lib/metadata/lv_manip.c

  • Committer: Package Import Robot
  • Author(s): Bastian Blank
  • Date: 2014-08-19 15:37:06 UTC
  • mfrom: (1.1.18)
  • Revision ID: package-import@ubuntu.com-20140819153706-i1gaio8lg534dara
Tags: 2.02.109-1
New upstream version.

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
#include "str_list.h"
29
29
#include "defaults.h"
30
30
#include "lvm-exec.h"
 
31
#include "lvm-signal.h"
31
32
 
32
33
typedef enum {
33
34
        PREFERRED,
49
50
#define A_CAN_SPLIT             0x10
50
51
#define A_AREA_COUNT_MATCHES    0x20    /* Existing lvseg has same number of areas as new segment */
51
52
 
 
53
#define A_POSITIONAL_FILL       0x40    /* Slots are positional and filled using PREFERRED */
 
54
 
52
55
/*
53
56
 * Constant parameters during a single allocation attempt.
54
57
 */
63
66
 * Holds varying state of each allocation attempt.
64
67
 */
65
68
struct alloc_state {
 
69
        const struct alloc_parms *alloc_parms;
66
70
        struct pv_area_used *areas;
67
71
        uint32_t areas_size;
68
72
        uint32_t log_area_count_still_needed;   /* Number of areas still needing to be allocated for the log */
78
82
        struct physical_volume *pv;
79
83
        int *i;
80
84
};
 
85
 
 
86
typedef enum {
 
87
        LV_TYPE_UNKNOWN,
 
88
        LV_TYPE_PVMOVE,
 
89
        LV_TYPE_ORIGIN,
 
90
        LV_TYPE_EXTERNAL_ORIGIN,
 
91
        LV_TYPE_SNAPSHOT,
 
92
        LV_TYPE_THIN,
 
93
        LV_TYPE_THIN_SNAPSHOT,
 
94
        LV_TYPE_THIN_POOL,
 
95
        LV_TYPE_THIN_POOL_DATA,
 
96
        LV_TYPE_THIN_POOL_METADATA,
 
97
        LV_TYPE_CACHE,
 
98
        LV_TYPE_CACHE_POOL,
 
99
        LV_TYPE_CACHE_POOL_DATA,
 
100
        LV_TYPE_CACHE_POOL_METADATA,
 
101
        LV_TYPE_POOL_METADATA_SPARE,
 
102
        LV_TYPE_VIRTUAL,
 
103
        LV_TYPE_RAID,
 
104
        LV_TYPE_RAID_IMAGE,
 
105
        LV_TYPE_RAID_METADATA,
 
106
        LV_TYPE_MIRROR,
 
107
        LV_TYPE_MIRROR_IMAGE,
 
108
        LV_TYPE_MIRROR_LOG,
 
109
        LV_TYPE_LINEAR,
 
110
        LV_TYPE_STRIPED
 
111
} lv_type_t;
 
112
 
 
113
static const char *_lv_type_names[] = {
 
114
        [LV_TYPE_UNKNOWN] =                     "unknown",
 
115
        [LV_TYPE_PVMOVE] =                      "pvmove",
 
116
        [LV_TYPE_ORIGIN] =                      "origin",
 
117
        [LV_TYPE_EXTERNAL_ORIGIN] =             "external-origin",
 
118
        [LV_TYPE_SNAPSHOT] =                    "snapshot",
 
119
        [LV_TYPE_THIN] =                        "thin",
 
120
        [LV_TYPE_THIN_SNAPSHOT] =               "thin-snapshot",
 
121
        [LV_TYPE_THIN_POOL] =                   "thin-pool",
 
122
        [LV_TYPE_THIN_POOL_DATA] =              "thin-pool-data",
 
123
        [LV_TYPE_THIN_POOL_METADATA] =          "thin-pool-metadata",
 
124
        [LV_TYPE_CACHE] =                       "cache",
 
125
        [LV_TYPE_CACHE_POOL] =                  "cache-pool",
 
126
        [LV_TYPE_CACHE_POOL_DATA] =             "cache-pool-data",
 
127
        [LV_TYPE_CACHE_POOL_METADATA] =         "cache-pool-metadata",
 
128
        [LV_TYPE_POOL_METADATA_SPARE] =         "pool-metadata-spare",
 
129
        [LV_TYPE_VIRTUAL] =                     "virtual",
 
130
        [LV_TYPE_RAID] =                        "raid",
 
131
        [LV_TYPE_RAID_IMAGE] =                  "raid-image",
 
132
        [LV_TYPE_RAID_METADATA] =               "raid-metadata",
 
133
        [LV_TYPE_MIRROR] =                      "mirror",
 
134
        [LV_TYPE_MIRROR_IMAGE] =                "mirror-image",
 
135
        [LV_TYPE_MIRROR_LOG] =                  "mirror-log",
 
136
        [LV_TYPE_LINEAR] =                      "linear",
 
137
        [LV_TYPE_STRIPED] =                     "striped"
 
138
};
 
139
 
 
140
static lv_type_t _get_lv_type(const struct logical_volume *lv)
 
141
{
 
142
        lv_type_t type = LV_TYPE_UNKNOWN;
 
143
        struct lv_segment *seg;
 
144
 
 
145
        if (lv->status & PVMOVE)
 
146
                type = LV_TYPE_PVMOVE;
 
147
        else if (lv_is_origin(lv))
 
148
                type = LV_TYPE_ORIGIN;
 
149
        else if (lv_is_external_origin(lv))
 
150
                type = LV_TYPE_EXTERNAL_ORIGIN;
 
151
        else if (lv_is_cow(lv))
 
152
                type = LV_TYPE_SNAPSHOT;
 
153
        else if (lv_is_thin_volume(lv))
 
154
                type = first_seg(lv)->origin ? LV_TYPE_THIN_SNAPSHOT : LV_TYPE_THIN;
 
155
        else if (lv_is_thin_pool(lv))
 
156
                type = LV_TYPE_THIN_POOL;
 
157
        else if (lv_is_thin_pool_data(lv))
 
158
                type = LV_TYPE_THIN_POOL_DATA;
 
159
        else if (lv_is_thin_pool_metadata(lv))
 
160
                type = LV_TYPE_THIN_POOL_METADATA;
 
161
        else if (lv_is_pool_metadata_spare(lv))
 
162
                type = LV_TYPE_POOL_METADATA_SPARE;
 
163
        else if (lv_is_cache(lv))
 
164
                type = LV_TYPE_CACHE;
 
165
        else if (lv_is_cache_pool(lv))
 
166
                type = LV_TYPE_CACHE_POOL;
 
167
        else if (lv_is_cache_pool_data(lv))
 
168
                type = LV_TYPE_CACHE_POOL_DATA;
 
169
        else if (lv_is_cache_pool_metadata(lv))
 
170
                type = LV_TYPE_CACHE_POOL_METADATA;
 
171
        else if (lv_is_virtual(lv))
 
172
                type = LV_TYPE_VIRTUAL;
 
173
        else if (lv_is_raid(lv))
 
174
                type = LV_TYPE_RAID;
 
175
        else if (lv_is_raid_image(lv))
 
176
                type = LV_TYPE_RAID_IMAGE;
 
177
        else if (lv_is_raid_metadata(lv))
 
178
                type = LV_TYPE_RAID_METADATA;
 
179
        else if (lv_is_mirrored(lv))
 
180
                type = LV_TYPE_MIRROR;
 
181
        else if (lv_is_mirror_image(lv))
 
182
                type = LV_TYPE_MIRROR_IMAGE;
 
183
        else if (lv_is_mirror_log(lv))
 
184
                type = LV_TYPE_MIRROR_LOG;
 
185
 
 
186
        /* none of the above, check linear... */
 
187
        if (type == LV_TYPE_UNKNOWN) {
 
188
                type = LV_TYPE_LINEAR;
 
189
                dm_list_iterate_items(seg, &lv->segments) {
 
190
                        if (!seg_is_linear(seg)) {
 
191
                                type = LV_TYPE_UNKNOWN;
 
192
                                break;
 
193
                        }
 
194
                }
 
195
        }
 
196
 
 
197
        /* ...if not even linear, check striped... */
 
198
        if (type == LV_TYPE_UNKNOWN) {
 
199
                type = LV_TYPE_STRIPED;
 
200
                dm_list_iterate_items(seg, &lv->segments) {
 
201
                        if (!seg_is_striped(seg)) {
 
202
                                type = LV_TYPE_UNKNOWN;
 
203
                                break;
 
204
                        }
 
205
                }
 
206
        }
 
207
 
 
208
        return type;
 
209
}
 
210
 
 
211
const char *lv_type_name(const struct logical_volume *lv) {
 
212
        lv_type_t type = _get_lv_type(lv);
 
213
        return _lv_type_names[type];
 
214
}
 
215
 
 
216
int lv_is_linear(const struct logical_volume *lv)
 
217
{
 
218
        lv_type_t type = _get_lv_type(lv);
 
219
        return type == LV_TYPE_LINEAR;
 
220
}
 
221
 
 
222
int lv_is_striped(const struct logical_volume *lv)
 
223
{
 
224
        lv_type_t type = _get_lv_type(lv);
 
225
        return type == LV_TYPE_STRIPED;
 
226
}
 
227
 
81
228
static int _lv_is_on_pv(struct logical_volume *lv, void *data)
82
229
{
83
230
        int *is_on_pv = ((struct pv_and_int *)data)->i;
338
485
 *
339
486
 * In general, walk through lv->segs_using_this_lv.
340
487
 */
341
 
struct lv_segment *get_only_segment_using_this_lv(struct logical_volume *lv)
 
488
struct lv_segment *get_only_segment_using_this_lv(const struct logical_volume *lv)
342
489
{
343
490
        struct seg_list *sl;
344
491
 
414
561
        return i;
415
562
}
416
563
 
417
 
percent_t copy_percent(const struct logical_volume *lv)
 
564
dm_percent_t copy_percent(const struct logical_volume *lv)
418
565
{
419
566
        uint32_t numerator = 0u, denominator = 0u;
420
567
        struct lv_segment *seg;
430
577
                        numerator += seg->area_len;
431
578
        }
432
579
 
433
 
        return denominator ? make_percent( numerator, denominator ) : 100.0;
 
580
        return denominator ? dm_make_percent( numerator, denominator ) : 100.0;
434
581
}
435
582
 
436
583
/*
873
1020
         * an error segment, we should also clear any flags
874
1021
         * that suggest it is anything other than "error".
875
1022
         */
876
 
        lv->status &= ~(MIRRORED|PVMOVE);
 
1023
        lv->status &= ~(MIRRORED|PVMOVE|LOCKED);
877
1024
 
878
1025
        /* FIXME: Should we bug if we find a log_lv attached? */
879
1026
 
1273
1420
         * areas if the number of areas matches.
1274
1421
         */
1275
1422
        if (alloc_parms->prev_lvseg &&
1276
 
            ((ah->area_count + ah->parity_count) == prev_lvseg->area_count))
 
1423
            ((ah->area_count + ah->parity_count) == prev_lvseg->area_count)) {
1277
1424
                alloc_parms->flags |= A_AREA_COUNT_MATCHES;
1278
1425
 
1279
 
        /* Are there any preceding segments we must follow on from? */
1280
 
        if (alloc_parms->prev_lvseg &&
1281
 
            (alloc_parms->flags & A_AREA_COUNT_MATCHES)) {
1282
 
                if (alloc_parms->alloc == ALLOC_CONTIGUOUS)
 
1426
                /* Are there any preceding segments we must follow on from? */
 
1427
                if (alloc_parms->alloc == ALLOC_CONTIGUOUS) {
1283
1428
                        alloc_parms->flags |= A_CONTIGUOUS_TO_LVSEG;
1284
 
                else if ((alloc_parms->alloc == ALLOC_CLING) ||
1285
 
                         (alloc_parms->alloc == ALLOC_CLING_BY_TAGS))
 
1429
                        alloc_parms->flags |= A_POSITIONAL_FILL;
 
1430
                } else if ((alloc_parms->alloc == ALLOC_CLING) ||
 
1431
                           (alloc_parms->alloc == ALLOC_CLING_BY_TAGS)) {
1286
1432
                        alloc_parms->flags |= A_CLING_TO_LVSEG;
 
1433
                        alloc_parms->flags |= A_POSITIONAL_FILL;
 
1434
                }
1287
1435
        } else
1288
1436
                /*
1289
1437
                 * A cling allocation that follows a successful contiguous
1290
1438
                 * allocation must use the same PVs (or else fail).
1291
1439
                 */
1292
1440
                if ((alloc_parms->alloc == ALLOC_CLING) ||
1293
 
                    (alloc_parms->alloc == ALLOC_CLING_BY_TAGS))
 
1441
                    (alloc_parms->alloc == ALLOC_CLING_BY_TAGS)) {
1294
1442
                        alloc_parms->flags |= A_CLING_TO_ALLOCED;
 
1443
                        alloc_parms->flags |= A_POSITIONAL_FILL;
 
1444
                }
1295
1445
 
1296
1446
        if (alloc_parms->alloc == ALLOC_CLING_BY_TAGS)
1297
1447
                alloc_parms->flags |= A_CLING_BY_TAGS;
1419
1569
                                struct alloc_state *alloc_state, uint32_t ix_log_offset)
1420
1570
{
1421
1571
        uint32_t area_len, len;
1422
 
        uint32_t s;
 
1572
        uint32_t s, smeta;
1423
1573
        uint32_t ix_log_skip = 0; /* How many areas to skip in middle of array to reach log areas */
1424
1574
        uint32_t total_area_count;
1425
1575
        struct alloced_area *aa;
1426
1576
        struct pv_area *pva;
1427
1577
 
1428
 
        total_area_count = ah->area_count + alloc_state->log_area_count_still_needed;
1429
 
        total_area_count += ah->parity_count;
 
1578
        total_area_count = ah->area_count + ah->parity_count + alloc_state->log_area_count_still_needed;
1430
1579
        if (!total_area_count) {
1431
1580
                log_warn(INTERNAL_ERROR "_alloc_parallel_area called without any allocation to do.");
1432
1581
                return 1;
1469
1618
                         * allocation, we store the images at the beginning
1470
1619
                         * of the areas array and the metadata at the end.
1471
1620
                         */
1472
 
                        s += ah->area_count + ah->parity_count;
1473
 
                        aa[s].pv = pva->map->pv;
1474
 
                        aa[s].pe = pva->start;
1475
 
                        aa[s].len = ah->log_len;
 
1621
                        smeta = s + ah->area_count + ah->parity_count;
 
1622
                        aa[smeta].pv = pva->map->pv;
 
1623
                        aa[smeta].pe = pva->start;
 
1624
                        aa[smeta].len = ah->log_len;
1476
1625
 
1477
1626
                        log_debug_alloc("Allocating parallel metadata area %" PRIu32
1478
1627
                                        " on %s start PE %" PRIu32
1479
1628
                                        " length %" PRIu32 ".",
1480
 
                                        (s - (ah->area_count + ah->parity_count)),
1481
 
                                        pv_dev_name(aa[s].pv), aa[s].pe,
 
1629
                                        (smeta - (ah->area_count + ah->parity_count)),
 
1630
                                        pv_dev_name(aa[smeta].pv), aa[smeta].pe,
1482
1631
                                        ah->log_len);
1483
1632
 
1484
1633
                        consume_pv_area(pva, ah->log_len);
1485
 
                        dm_list_add(&ah->alloced_areas[s], &aa[s].list);
1486
 
                        s -= ah->area_count + ah->parity_count;
 
1634
                        dm_list_add(&ah->alloced_areas[smeta], &aa[smeta].list);
1487
1635
                }
1488
1636
                aa[s].len = (ah->alloc_and_split_meta && !ah->split_metadata_is_allocated) ? len - ah->log_len : len;
1489
1637
                /* Skip empty allocations */
1613
1761
struct pv_match {
1614
1762
        int (*condition)(struct pv_match *pvmatch, struct pv_segment *pvseg, struct pv_area *pva);
1615
1763
 
1616
 
        struct pv_area_used *areas;
 
1764
        struct alloc_state *alloc_state;
1617
1765
        struct pv_area *pva;
1618
 
        uint32_t areas_size;
1619
1766
        const struct dm_config_node *cling_tag_list_cn;
1620
1767
        int s;  /* Area index of match */
1621
1768
};
1711
1858
        return 1;
1712
1859
}
1713
1860
 
1714
 
static void _reserve_area(struct pv_area_used *area_used, struct pv_area *pva, uint32_t required,
 
1861
static void _reserve_area(struct alloc_state *alloc_state, struct pv_area *pva, uint32_t required,
1715
1862
                          uint32_t ix_pva, uint32_t unreserved)
1716
1863
{
 
1864
        struct pv_area_used *area_used = &alloc_state->areas[ix_pva];
 
1865
 
1717
1866
        log_debug_alloc("%s allocation area %" PRIu32 " %s %s start PE %" PRIu32
1718
1867
                        " length %" PRIu32 " leaving %" PRIu32 ".",
1719
1868
                        area_used->pva ? "Changing   " : "Considering", 
1720
 
                        ix_pva - 1, area_used->pva ? "to" : "as", 
 
1869
                        ix_pva, area_used->pva ? "to" : "as", 
1721
1870
                        dev_name(pva->map->pv->dev), pva->start, required, unreserved);
1722
1871
 
1723
1872
        area_used->pva = pva;
1724
1873
        area_used->used = required;
1725
1874
}
1726
1875
 
 
1876
static int _reserve_required_area(struct alloc_state *alloc_state, struct pv_area *pva, uint32_t required,
 
1877
                                  uint32_t ix_pva, uint32_t unreserved)
 
1878
{
 
1879
        uint32_t s;
 
1880
 
 
1881
        /* Expand areas array if needed after an area was split. */
 
1882
        if (ix_pva >= alloc_state->areas_size) {
 
1883
                alloc_state->areas_size *= 2;
 
1884
                if (!(alloc_state->areas = dm_realloc(alloc_state->areas, sizeof(*alloc_state->areas) * (alloc_state->areas_size)))) {
 
1885
                        log_error("Memory reallocation for parallel areas failed.");
 
1886
                        return 0;
 
1887
                }
 
1888
                for (s = alloc_state->areas_size / 2; s < alloc_state->areas_size; s++)
 
1889
                        alloc_state->areas[s].pva = NULL;
 
1890
        }
 
1891
 
 
1892
        _reserve_area(alloc_state, pva, required, ix_pva, unreserved);
 
1893
 
 
1894
        return 1;
 
1895
}
 
1896
 
1727
1897
static int _is_condition(struct cmd_context *cmd __attribute__((unused)),
1728
1898
                         struct pv_segment *pvseg, uint32_t s,
1729
1899
                         void *data)
1730
1900
{
1731
1901
        struct pv_match *pvmatch = data;
 
1902
        int positional = pvmatch->alloc_state->alloc_parms->flags & A_POSITIONAL_FILL;
1732
1903
 
1733
 
        if (pvmatch->areas[s].pva)
 
1904
        if (positional && pvmatch->alloc_state->areas[s].pva)
1734
1905
                return 1;       /* Area already assigned */
1735
1906
 
1736
1907
        if (!pvmatch->condition(pvmatch, pvseg, pvmatch->pva))
1737
1908
                return 1;       /* Continue */
1738
1909
 
1739
 
        if (s >= pvmatch->areas_size)
 
1910
        if (positional && (s >= pvmatch->alloc_state->areas_size))
1740
1911
                return 1;
1741
1912
 
1742
1913
        /*
1743
1914
         * Only used for cling and contiguous policies (which only make one allocation per PV)
1744
1915
         * so it's safe to say all the available space is used.
1745
1916
         */
1746
 
        _reserve_area(&pvmatch->areas[s], pvmatch->pva, pvmatch->pva->count, s + 1, 0);
 
1917
        if (positional)
 
1918
                _reserve_required_area(pvmatch->alloc_state, pvmatch->pva, pvmatch->pva->count, s, 0);
1747
1919
 
1748
1920
        return 2;       /* Finished */
1749
1921
}
1761
1933
        uint32_t le, len;
1762
1934
 
1763
1935
        pvmatch.condition = cling_tag_list_cn ? _has_matching_pv_tag : _is_same_pv;
1764
 
        pvmatch.areas = alloc_state->areas;
1765
 
        pvmatch.areas_size = alloc_state->areas_size;
 
1936
        pvmatch.alloc_state = alloc_state;
1766
1937
        pvmatch.pva = pva;
1767
1938
        pvmatch.cling_tag_list_cn = cling_tag_list_cn;
1768
1939
 
1799
1970
        int r;
1800
1971
 
1801
1972
        pvmatch.condition = _is_contiguous;
1802
 
        pvmatch.areas = alloc_state->areas;
1803
 
        pvmatch.areas_size = alloc_state->areas_size;
 
1973
        pvmatch.alloc_state = alloc_state;
1804
1974
        pvmatch.pva = pva;
1805
1975
        pvmatch.cling_tag_list_cn = NULL;
1806
1976
 
1825
1995
{
1826
1996
        unsigned s;
1827
1997
        struct alloced_area *aa;
 
1998
        int positional = alloc_state->alloc_parms->flags & A_POSITIONAL_FILL;
1828
1999
 
1829
2000
        /*
1830
2001
         * Ignore log areas.  They are always allocated whole as part of the
1834
2005
                return 0;
1835
2006
 
1836
2007
        for (s = 0; s < ah->area_count; s++) {
1837
 
                if (alloc_state->areas[s].pva)
 
2008
                if (positional && alloc_state->areas[s].pva)
1838
2009
                        continue;       /* Area already assigned */
1839
2010
                dm_list_iterate_items(aa, &ah->alloced_areas[s]) {
1840
2011
                        if ((!cling_tag_list_cn && (pva->map->pv == aa[0].pv)) ||
1841
2012
                            (cling_tag_list_cn && _pvs_have_matching_tag(cling_tag_list_cn, pva->map->pv, aa[0].pv))) {
1842
 
                                _reserve_area(&alloc_state->areas[s], pva, pva->count, s + 1, 0);
 
2013
                                if (positional)
 
2014
                                        _reserve_required_area(alloc_state, pva, pva->count, s, 0);
1843
2015
                                return 1;
1844
2016
                        }
1845
2017
                }
1864
2036
 * alloc_state->areas may get modified.
1865
2037
 */
1866
2038
static area_use_t _check_pva(struct alloc_handle *ah, struct pv_area *pva, uint32_t still_needed,
1867
 
                             const struct alloc_parms *alloc_parms, struct alloc_state *alloc_state,
 
2039
                             struct alloc_state *alloc_state,
1868
2040
                             unsigned already_found_one, unsigned iteration_count, unsigned log_iteration_count)
1869
2041
{
 
2042
        const struct alloc_parms *alloc_parms = alloc_state->alloc_parms;
1870
2043
        unsigned s;
1871
2044
 
1872
2045
        /* Skip fully-reserved areas (which are not currently removed from the list). */
1886
2059
        if (!iteration_count && !log_iteration_count && alloc_parms->flags & (A_CONTIGUOUS_TO_LVSEG | A_CLING_TO_LVSEG | A_CLING_TO_ALLOCED)) {
1887
2060
                /* Contiguous? */
1888
2061
                if (((alloc_parms->flags & A_CONTIGUOUS_TO_LVSEG) ||
1889
 
                     (ah->maximise_cling && alloc_parms->prev_lvseg && (alloc_parms->flags & A_AREA_COUNT_MATCHES))) &&
 
2062
                     (ah->maximise_cling && (alloc_parms->flags & A_AREA_COUNT_MATCHES))) &&
1890
2063
                    _check_contiguous(ah->cmd, alloc_parms->prev_lvseg, pva, alloc_state))
1891
 
                        return PREFERRED;
 
2064
                        goto found;
1892
2065
 
1893
2066
                /* Try next area on same PV if looking for contiguous space */
1894
2067
                if (alloc_parms->flags & A_CONTIGUOUS_TO_LVSEG)
1896
2069
 
1897
2070
                /* Cling to prev_lvseg? */
1898
2071
                if (((alloc_parms->flags & A_CLING_TO_LVSEG) ||
1899
 
                     (ah->maximise_cling && alloc_parms->prev_lvseg && (alloc_parms->flags & A_AREA_COUNT_MATCHES))) &&
 
2072
                     (ah->maximise_cling && (alloc_parms->flags & A_AREA_COUNT_MATCHES))) &&
1900
2073
                    _check_cling(ah, NULL, alloc_parms->prev_lvseg, pva, alloc_state))
1901
2074
                        /* If this PV is suitable, use this first area */
1902
 
                        return PREFERRED;
 
2075
                        goto found;
1903
2076
 
1904
2077
                /* Cling_to_alloced? */
1905
2078
                if ((alloc_parms->flags & A_CLING_TO_ALLOCED) &&
1906
2079
                    _check_cling_to_alloced(ah, NULL, pva, alloc_state))
1907
 
                        return PREFERRED;
 
2080
                        goto found;
1908
2081
 
1909
2082
                /* Cling_by_tags? */
1910
2083
                if (!(alloc_parms->flags & A_CLING_BY_TAGS) || !ah->cling_tag_list_cn)
1911
2084
                        return NEXT_PV;
1912
2085
 
1913
 
                if (alloc_parms->prev_lvseg && (alloc_parms->flags & A_AREA_COUNT_MATCHES)) {
 
2086
                if ((alloc_parms->flags & A_AREA_COUNT_MATCHES)) {
1914
2087
                        if (_check_cling(ah, ah->cling_tag_list_cn, alloc_parms->prev_lvseg, pva, alloc_state))
1915
 
                                return PREFERRED;
 
2088
                                goto found;
1916
2089
                } else if (_check_cling_to_alloced(ah, ah->cling_tag_list_cn, pva, alloc_state))
1917
 
                        return PREFERRED;
 
2090
                        goto found;
1918
2091
 
1919
2092
                /* All areas on this PV give same result so pointless checking more */
1920
2093
                return NEXT_PV;
1928
2101
             (already_found_one && alloc_parms->alloc != ALLOC_ANYWHERE)))
1929
2102
                return NEXT_PV;
1930
2103
 
 
2104
found:
 
2105
        if (alloc_parms->flags & A_POSITIONAL_FILL)
 
2106
                return PREFERRED;
 
2107
 
1931
2108
        return USE_AREA;
1932
2109
}
1933
2110
 
1945
2122
         * reduce amount we're looking for.
1946
2123
         */
1947
2124
        if (alloc == ALLOC_ANYWHERE) {
1948
 
                if (ix_pva - 1 >= ah->area_count)
 
2125
                if (ix_pva >= ah->area_count + ah->parity_count)
1949
2126
                        required = ah->log_len;
1950
2127
        } else if (required < ah->log_len)
1951
2128
                required = ah->log_len;
1961
2138
        return required;
1962
2139
}
1963
2140
 
1964
 
static int _reserve_required_area(struct alloc_handle *ah, uint32_t max_to_allocate,
1965
 
                                  unsigned ix_pva, struct pv_area *pva,
1966
 
                                  struct alloc_state *alloc_state, alloc_policy_t alloc)
1967
 
{
1968
 
        uint32_t required = _calc_required_extents(ah, pva, ix_pva, max_to_allocate, alloc);
1969
 
        uint32_t s;
1970
 
 
1971
 
        /* Expand areas array if needed after an area was split. */
1972
 
        if (ix_pva > alloc_state->areas_size) {
1973
 
                alloc_state->areas_size *= 2;
1974
 
                if (!(alloc_state->areas = dm_realloc(alloc_state->areas, sizeof(*alloc_state->areas) * (alloc_state->areas_size)))) {
1975
 
                        log_error("Memory reallocation for parallel areas failed.");
1976
 
                        return 0;
1977
 
                }
1978
 
                for (s = alloc_state->areas_size / 2; s < alloc_state->areas_size; s++)
1979
 
                        alloc_state->areas[s].pva = NULL;
1980
 
        }
1981
 
 
1982
 
        _reserve_area(&alloc_state->areas[ix_pva - 1], pva, required, ix_pva, pva->unreserved);
1983
 
 
1984
 
        return 1;
1985
 
}
1986
 
 
1987
2141
static void _clear_areas(struct alloc_state *alloc_state)
1988
2142
{
1989
2143
        uint32_t s;
2044
2198
/*
2045
2199
 * Returns 1 regardless of whether any space was found, except on error.
2046
2200
 */
2047
 
static int _find_some_parallel_space(struct alloc_handle *ah, const struct alloc_parms *alloc_parms,
 
2201
static int _find_some_parallel_space(struct alloc_handle *ah,
2048
2202
                                     struct dm_list *pvms, struct alloc_state *alloc_state,
2049
2203
                                     struct dm_list *parallel_pvs, uint32_t max_to_allocate)
2050
2204
{
 
2205
        const struct alloc_parms *alloc_parms = alloc_state->alloc_parms;
2051
2206
        unsigned ix = 0;
2052
2207
        unsigned last_ix;
2053
2208
        struct pv_map *pvm;
2062
2217
        struct alloced_area *aa;
2063
2218
        uint32_t s;
2064
2219
        uint32_t devices_needed = ah->area_count + ah->parity_count;
 
2220
        uint32_t required;
2065
2221
 
2066
2222
        /* ix_offset holds the number of parallel allocations that must be contiguous/cling */
2067
2223
        /* At most one of A_CONTIGUOUS_TO_LVSEG, A_CLING_TO_LVSEG or A_CLING_TO_ALLOCED may be set */
2068
 
        if (alloc_parms->flags & (A_CONTIGUOUS_TO_LVSEG | A_CLING_TO_LVSEG))
 
2224
        if (!(alloc_parms->flags & A_POSITIONAL_FILL))
 
2225
                ix_offset = 0;
 
2226
        else if (alloc_parms->flags & (A_CONTIGUOUS_TO_LVSEG | A_CLING_TO_LVSEG))
2069
2227
                ix_offset = _stripes_per_mimage(alloc_parms->prev_lvseg) * alloc_parms->prev_lvseg->area_count;
2070
 
 
2071
 
        if (alloc_parms->flags & A_CLING_TO_ALLOCED)
 
2228
        else if (alloc_parms->flags & A_CLING_TO_ALLOCED)
2072
2229
                ix_offset = ah->area_count;
2073
2230
 
2074
2231
        if (alloc_parms->alloc == ALLOC_NORMAL || (alloc_parms->flags & A_CLING_TO_ALLOCED))
2075
2232
                log_debug_alloc("Cling_to_allocated is %sset",
2076
2233
                                alloc_parms->flags & A_CLING_TO_ALLOCED ? "" : "not ");
2077
2234
 
 
2235
        if (alloc_parms->flags & A_POSITIONAL_FILL)
 
2236
                log_debug_alloc("%u preferred area(s) to be filled positionally.", ix_offset);
 
2237
        else
 
2238
                log_debug_alloc("Areas to be sorted and filled sequentially.");
 
2239
 
2078
2240
        _clear_areas(alloc_state);
2079
2241
        _reset_unreserved(pvms);
2080
2242
 
2143
2305
                                 * There are two types of allocations, which can't be mixed at present:
2144
2306
                                 *
2145
2307
                                 * PREFERRED are stored immediately in a specific parallel slot.
 
2308
                                 *   This is only used if the A_POSITIONAL_FILL flag is set.
2146
2309
                                 *   This requires the number of slots to match, so if comparing with
2147
2310
                                 *   prev_lvseg then A_AREA_COUNT_MATCHES must be set.
2148
2311
                                 *
2149
2312
                                 * USE_AREA are stored for later, then sorted and chosen from.
2150
2313
                                 */
2151
 
                                switch(_check_pva(ah, pva, max_to_allocate, alloc_parms,
 
2314
                                switch(_check_pva(ah, pva, max_to_allocate, 
2152
2315
                                                  alloc_state, already_found_one, iteration_count, log_iteration_count)) {
2153
2316
 
2154
2317
                                case PREFERRED:
2173
2336
                                        }
2174
2337
 
2175
2338
                                        /* Reserve required amount of pva */
2176
 
                                        if (!_reserve_required_area(ah, max_to_allocate, ix + ix_offset,
2177
 
                                                                    pva, alloc_state, alloc_parms->alloc))
 
2339
                                        required = _calc_required_extents(ah, pva, ix + ix_offset - 1, max_to_allocate, alloc_parms->alloc);
 
2340
                                        if (!_reserve_required_area(alloc_state, pva, required, ix + ix_offset - 1, pva->unreserved))
2178
2341
                                                return_0;
2179
2342
                                }
2180
2343
 
2200
2363
                  (ix + preferred_count >= devices_needed) &&
2201
2364
                  (ix + preferred_count < devices_needed + alloc_state->log_area_count_still_needed) && !log_iteration_count++));
2202
2365
 
2203
 
        if (preferred_count < ix_offset && !(alloc_parms->flags & A_CLING_TO_ALLOCED))
 
2366
        /* Non-zero ix means at least one USE_AREA was returned */
 
2367
        if (preferred_count < ix_offset && !(alloc_parms->flags & A_CLING_TO_ALLOCED) && !ix)
2204
2368
                return 1;
2205
2369
 
2206
2370
        if (ix + preferred_count < devices_needed + alloc_state->log_area_count_still_needed)
2279
2443
        struct seg_pvs *spvs;
2280
2444
        struct dm_list *parallel_pvs;
2281
2445
 
 
2446
        alloc_state->alloc_parms = alloc_parms;
 
2447
 
2282
2448
        /* FIXME This algorithm needs a lot of cleaning up! */
2283
2449
        /* FIXME anywhere doesn't find all space yet */
2284
2450
        do {
2316
2482
 
2317
2483
                old_allocated = alloc_state->allocated;
2318
2484
 
2319
 
                if (!_find_some_parallel_space(ah, alloc_parms, pvms, alloc_state, parallel_pvs, max_to_allocate))
 
2485
                if (!_find_some_parallel_space(ah, pvms, alloc_state, parallel_pvs, max_to_allocate))
2320
2486
                        return_0;
2321
2487
 
2322
2488
                /*
 
2489
                 * For ALLOC_CLING, if the number of areas matches and maximise_cling is
 
2490
                 * set we allow two passes, first with A_POSITIONAL_FILL then without.
 
2491
                 *
2323
2492
                 * If we didn't allocate anything this time with ALLOC_NORMAL and had
2324
2493
                 * A_CLING_TO_ALLOCED set, try again without it.
2325
2494
                 *
2328
2497
                 * remain on the same disks where possible.
2329
2498
                 */
2330
2499
                if (old_allocated == alloc_state->allocated) {
2331
 
                        if ((alloc_parms->alloc == ALLOC_NORMAL) && (alloc_parms->flags & A_CLING_TO_ALLOCED))
 
2500
                        if (ah->maximise_cling && ((alloc_parms->alloc == ALLOC_CLING) || (alloc_parms->alloc == ALLOC_CLING_BY_TAGS)) &&
 
2501
                            (alloc_parms->flags & A_CLING_TO_LVSEG) && (alloc_parms->flags & A_POSITIONAL_FILL))
 
2502
                                alloc_parms->flags &= ~A_POSITIONAL_FILL;
 
2503
                        else if ((alloc_parms->alloc == ALLOC_NORMAL) && (alloc_parms->flags & A_CLING_TO_ALLOCED))
2332
2504
                                alloc_parms->flags &= ~A_CLING_TO_ALLOCED;
2333
2505
                        else
2334
2506
                                break;  /* Give up */
2683
2855
/*
2684
2856
 * Add new areas to mirrored segments
2685
2857
 */
 
2858
int lv_add_segmented_mirror_image(struct alloc_handle *ah,
 
2859
                                  struct logical_volume *lv, uint32_t le,
 
2860
                                  uint32_t region_size)
 
2861
{
 
2862
        char *image_name;
 
2863
        struct alloced_area *aa;
 
2864
        struct lv_segment *seg, *new_seg;
 
2865
        uint32_t current_le = le;
 
2866
        uint32_t s;
 
2867
        struct segment_type *segtype;
 
2868
        struct logical_volume *orig_lv, *copy_lv;
 
2869
 
 
2870
        if (!(lv->status & PVMOVE)) {
 
2871
                log_error(INTERNAL_ERROR
 
2872
                          "Non-pvmove LV, %s, passed as argument", lv->name);
 
2873
                return 0;
 
2874
        }
 
2875
 
 
2876
        if (seg_type(first_seg(lv), 0) != AREA_PV) {
 
2877
                log_error(INTERNAL_ERROR
 
2878
                          "Bad segment type for first segment area");
 
2879
                return 0;
 
2880
        }
 
2881
 
 
2882
        /*
 
2883
         * If the allocator provided two or more PV allocations for any
 
2884
         * single segment of the original LV, that LV segment must be
 
2885
         * split up to match.
 
2886
         */
 
2887
        dm_list_iterate_items(aa, &ah->alloced_areas[0]) {
 
2888
                if (!(seg = find_seg_by_le(lv, current_le))) {
 
2889
                        log_error("Failed to find segment for %s extent %"
 
2890
                                  PRIu32, lv->name, current_le);
 
2891
                        return 0;
 
2892
                }
 
2893
 
 
2894
                /* Allocator assures aa[0].len <= seg->area_len */
 
2895
                if (aa[0].len < seg->area_len) {
 
2896
                        if (!lv_split_segment(lv, seg->le + aa[0].len)) {
 
2897
                                log_error("Failed to split segment at %s "
 
2898
                                          "extent %" PRIu32, lv->name, le);
 
2899
                                return 0;
 
2900
                        }
 
2901
                }
 
2902
                current_le += seg->area_len;
 
2903
        }
 
2904
 
 
2905
        current_le = le;
 
2906
 
 
2907
        if (!insert_layer_for_lv(lv->vg->cmd, lv, PVMOVE, "_mimage_0")) {
 
2908
                log_error("Failed to build pvmove LV-type mirror, %s",
 
2909
                          lv->name);
 
2910
                return 0;
 
2911
        }
 
2912
        orig_lv = seg_lv(first_seg(lv), 0);
 
2913
        if (!(image_name = dm_pool_strdup(lv->vg->vgmem, orig_lv->name)))
 
2914
                return_0;
 
2915
        image_name[strlen(image_name) - 1] = '1';
 
2916
 
 
2917
        if (!(copy_lv = lv_create_empty(image_name, NULL,
 
2918
                                        orig_lv->status,
 
2919
                                        ALLOC_INHERIT, lv->vg)))
 
2920
                return_0;
 
2921
 
 
2922
        if (!lv_add_mirror_lvs(lv, &copy_lv, 1, MIRROR_IMAGE, region_size))
 
2923
                return_0;
 
2924
 
 
2925
        if (!(segtype = get_segtype_from_string(lv->vg->cmd, "striped")))
 
2926
                return_0;
 
2927
 
 
2928
        dm_list_iterate_items(aa, &ah->alloced_areas[0]) {
 
2929
                if (!(seg = find_seg_by_le(orig_lv, current_le))) {
 
2930
                        log_error("Failed to find segment for %s extent %"
 
2931
                                  PRIu32, lv->name, current_le);
 
2932
                        return 0;
 
2933
                }
 
2934
 
 
2935
                if (!(new_seg = alloc_lv_segment(segtype, copy_lv,
 
2936
                                                 seg->le, seg->len, PVMOVE, 0,
 
2937
                                                 NULL, NULL, 1, seg->len,
 
2938
                                                 0, 0, 0, NULL)))
 
2939
                        return_0;
 
2940
 
 
2941
                for (s = 0; s < ah->area_count; s++) {
 
2942
                        if (!set_lv_segment_area_pv(new_seg, s,
 
2943
                                                    aa[s].pv, aa[s].pe))
 
2944
                                return_0;
 
2945
                }
 
2946
 
 
2947
                dm_list_add(&copy_lv->segments, &new_seg->list);
 
2948
 
 
2949
                current_le += seg->area_len;
 
2950
                copy_lv->le_count += seg->area_len;
 
2951
        }
 
2952
        lv->status |= MIRRORED;
 
2953
 
 
2954
        /* FIXME: add log */
 
2955
 
 
2956
        if (lv->vg->fid->fmt->ops->lv_setup &&
 
2957
            !lv->vg->fid->fmt->ops->lv_setup(lv->vg->fid, lv))
 
2958
                return_0;
 
2959
 
 
2960
        return 1;
 
2961
}
 
2962
 
 
2963
/*
 
2964
 * Add new areas to mirrored segments
 
2965
 */
2686
2966
int lv_add_mirror_areas(struct alloc_handle *ah,
2687
2967
                        struct logical_volume *lv, uint32_t le,
2688
2968
                        uint32_t region_size)
3128
3408
                if ((lv->le_count != extents) &&
3129
3409
                    segtype_is_mirrored(segtype) &&
3130
3410
                    (lv->status & LV_NOTSYNCED)) {
3131
 
                        percent_t sync_percent = PERCENT_INVALID;
 
3411
                        dm_percent_t sync_percent = DM_PERCENT_INVALID;
3132
3412
 
3133
3413
                        if (!lv_is_active_locally(lv)) {
3134
3414
                                log_error("%s/%s is not active locally."
3137
3417
                                /* FIXME Support --force */
3138
3418
                                if (yes_no_prompt("Do full resync of extended "
3139
3419
                                                  "portion of %s/%s?  [y/n]: ",
3140
 
                                                  lv->vg->name, lv->name) == 'y')
3141
 
                                        goto out;
3142
 
                                r = 0;
 
3420
                                                  lv->vg->name, lv->name) == 'n') {
 
3421
                                        r = 0;
 
3422
                                        goto_out;
 
3423
                                }
3143
3424
                                goto out;
3144
3425
                        }
3145
3426
 
3148
3429
                                log_error("Failed to get sync percent for %s/%s",
3149
3430
                                          lv->vg->name, lv->name);
3150
3431
                                goto out;
3151
 
                        } else if (sync_percent == PERCENT_100) {
 
3432
                        } else if (sync_percent == DM_PERCENT_100) {
3152
3433
                                log_verbose("Skipping initial resync for "
3153
3434
                                            "extended portion of %s/%s",
3154
3435
                                            lv->vg->name, lv->name);
3520
3801
        if (status)
3521
3802
                *status = -1;
3522
3803
 
3523
 
        if (dm_snprintf(lv_path, PATH_MAX, "%s%s/%s", cmd->dev_dir, lp->vg_name,
3524
 
                        lp->lv_name) < 0) {
 
3804
        if (dm_snprintf(lv_path, sizeof(lv_path), "%s%s/%s", cmd->dev_dir,
 
3805
                        vg->name, lp->lv_name) < 0) {
3525
3806
                log_error("Couldn't create LV path for %s", lp->lv_name);
3526
3807
                return 0;
3527
3808
        }
3529
3810
        argv[i++] = lv_path;
3530
3811
 
3531
3812
        if (fcmd == FSADM_CMD_RESIZE) {
3532
 
                if (dm_snprintf(size_buf, SIZE_BUF, "%" PRIu64 "K",
3533
 
                                (uint64_t) lp->extents * vg->extent_size / 2) < 0) {
 
3813
                if (dm_snprintf(size_buf, sizeof(size_buf), "%" PRIu64 "K",
 
3814
                                (uint64_t) lp->extents * (vg->extent_size / 2)) < 0) {
3534
3815
                        log_error("Couldn't generate new LV size string");
3535
3816
                        return 0;
3536
3817
                }
3546
3827
static int _adjust_policy_params(struct cmd_context *cmd,
3547
3828
                                 struct logical_volume *lv, struct lvresize_params *lp)
3548
3829
{
3549
 
        percent_t percent;
 
3830
        dm_percent_t percent;
3550
3831
        int policy_threshold, policy_amount;
3551
3832
 
3552
3833
        if (lv_is_thin_pool(lv)) {
3553
3834
                policy_threshold =
3554
3835
                        find_config_tree_int(cmd, activation_thin_pool_autoextend_threshold_CFG,
3555
 
                                             lv_config_profile(lv)) * PERCENT_1;
 
3836
                                             lv_config_profile(lv)) * DM_PERCENT_1;
3556
3837
                policy_amount =
3557
3838
                        find_config_tree_int(cmd, activation_thin_pool_autoextend_percent_CFG,
3558
3839
                                             lv_config_profile(lv));
3559
 
                if (!policy_amount && policy_threshold < PERCENT_100)
 
3840
                if (!policy_amount && policy_threshold < DM_PERCENT_100)
3560
3841
                        return 0;
3561
3842
        } else {
3562
3843
                policy_threshold =
3563
 
                        find_config_tree_int(cmd, activation_snapshot_autoextend_threshold_CFG, NULL) * PERCENT_1;
 
3844
                        find_config_tree_int(cmd, activation_snapshot_autoextend_threshold_CFG, NULL) * DM_PERCENT_1;
3564
3845
                policy_amount =
3565
3846
                        find_config_tree_int(cmd, activation_snapshot_autoextend_percent_CFG, NULL);
3566
3847
        }
3567
3848
 
3568
 
        if (policy_threshold >= PERCENT_100)
 
3849
        if (policy_threshold >= DM_PERCENT_100)
3569
3850
                return 1; /* nothing to do */
3570
3851
 
3571
3852
        if (lv_is_thin_pool(lv)) {
3572
3853
                if (!lv_thin_pool_percent(lv, 1, &percent))
3573
3854
                        return_0;
3574
 
                if ((PERCENT_0 < percent && percent <= PERCENT_100) &&
 
3855
                if ((DM_PERCENT_0 < percent && percent <= DM_PERCENT_100) &&
3575
3856
                    (percent > policy_threshold)) {
3576
3857
                        if (!thin_pool_feature_supported(lv, THIN_FEATURE_METADATA_RESIZE)) {
3577
3858
                                log_error_once("Online metadata resize for %s/%s is not supported.",
3578
 
                                               lp->vg_name, lp->lv_name);
 
3859
                                               lv->vg->name, lv->name);
3579
3860
                                return 0;
3580
3861
                        }
3581
3862
                        lp->poolmetadatasize = (first_seg(lv)->metadata_lv->size *
3585
3866
 
3586
3867
                if (!lv_thin_pool_percent(lv, 0, &percent))
3587
3868
                        return_0;
3588
 
                if (!(PERCENT_0 < percent && percent <= PERCENT_100) ||
 
3869
                if (!(DM_PERCENT_0 < percent && percent <= DM_PERCENT_100) ||
3589
3870
                    percent <= policy_threshold)
3590
3871
                        return 1;
3591
3872
        } else {
3592
3873
                if (!lv_snapshot_percent(lv, &percent))
3593
3874
                        return_0;
3594
 
                if (!(PERCENT_0 < percent && percent <= PERCENT_100) || percent <= policy_threshold)
3595
 
                        return 1; /* nothing to do */
 
3875
                if (!(DM_PERCENT_0 < percent && percent <= DM_PERCENT_100) || percent <= policy_threshold)
 
3876
                return 1; /* nothing to do */
3596
3877
        }
3597
3878
 
3598
3879
        lp->extents = policy_amount;
3874
4155
                if (lp->sign == SIGN_NONE && (lp->percent != PERCENT_LV && lp->percent != PERCENT_ORIGIN))
3875
4156
                        lp->approx_alloc = 1;
3876
4157
                /* FIXME Adjust for parallel areas here before processing relative allocations */
 
4158
                if (lp->sign == SIGN_PLUS && lp->percent == PERCENT_FREE)
 
4159
                        lp->approx_alloc = 1;
3877
4160
        }
3878
4161
 
3879
4162
        if (lp->sign == SIGN_PLUS) {
4175
4458
        struct volume_group *vg = lv->vg;
4176
4459
        struct logical_volume *lock_lv = NULL;
4177
4460
        struct lv_segment *seg = NULL;
 
4461
        uint32_t old_extents;
4178
4462
        int status;
4179
4463
        alloc_policy_t alloc;
4180
4464
 
4219
4503
        if (!archive(vg))
4220
4504
                return_NULL;
4221
4505
 
4222
 
        log_print_unless_silent("%sing logical volume %s to %s",
4223
 
                                (lp->resize == LV_REDUCE) ? "Reduc" : "Extend",
4224
 
                                lv->name,
4225
 
                                display_size(cmd, (uint64_t) lp->extents * vg->extent_size));
 
4506
        old_extents = lv->le_count;
 
4507
        log_verbose("%sing logical volume %s to %s%s",
 
4508
                    (lp->resize == LV_REDUCE) ? "Reduc" : "Extend",
 
4509
                    display_lvname(lv), lp->approx_alloc ? "up to " : "",
 
4510
                    display_size(cmd, (uint64_t) lp->extents * vg->extent_size));
4226
4511
 
4227
4512
        if (lp->resize == LV_REDUCE) {
4228
4513
                if (!lv_reduce(lv, lv->le_count - lp->extents))
4235
4520
                              pvh, alloc, lp->approx_alloc))
4236
4521
                return_NULL;
4237
4522
 
 
4523
        if (old_extents == lv->le_count)
 
4524
                log_print_unless_silent("Size of logical volume %s unchanged from %s.",
 
4525
                                        display_lvname(lv),
 
4526
                                        display_size(cmd, (uint64_t) old_extents * vg->extent_size));
 
4527
        else
 
4528
                log_print_unless_silent("Size of logical volume %s changed from %s to %s.",
 
4529
                                        display_lvname(lv),
 
4530
                                        display_size(cmd, (uint64_t) old_extents * vg->extent_size),
 
4531
                                        display_size(cmd, (uint64_t) lv->le_count * vg->extent_size));
 
4532
 
4238
4533
        if (lock_lv) {
4239
4534
                /* Update thin pool segment from the layered LV */
4240
4535
                seg->area_len = lv->le_count;
4280
4575
 
4281
4576
        if (lp->poolmetadatasize &&
4282
4577
            !_lvresize_poolmetadata_prepare(cmd, lp, lv))
4283
 
                        return_0;
 
4578
                return_0;
4284
4579
 
4285
4580
        return 1;
4286
4581
}
4495
4790
                goto_bad;
4496
4791
 
4497
4792
        if (vg->fid->fmt->features & FMT_CONFIG_PROFILE)
4498
 
                lv->profile = vg->cmd->profile_params->global_profile;
 
4793
                lv->profile = vg->cmd->profile_params->global_metadata_profile;
4499
4794
 
4500
4795
        return lv;
4501
4796
bad:
4511
4806
 
4512
4807
        /* Don't add again if it's already on list. */
4513
4808
        if (find_pv_in_pv_list(&spvs->pvs, peg->pv))
4514
 
                        return 1;
 
4809
                return 1;
4515
4810
 
4516
4811
        if (!(pvl = dm_pool_alloc(cmd->mem, sizeof(*pvl)))) {
4517
4812
                log_error("pv_list allocation failed");
4526
4821
}
4527
4822
 
4528
4823
/*
4529
 
 * Construct dm_list of segments of LVs showing which PVs they use.
4530
 
 * For pvmove we use the *parent* LV so we can pick up stripes & existing mirrors etc.
 
4824
 * build_parallel_areas_from_lv
 
4825
 * @lv
 
4826
 * @use_pvmove_parent_lv
 
4827
 * @create_single_list
 
4828
 *
 
4829
 * For each segment in an LV, create a list of PVs used by the segment.
 
4830
 * Thus, the returned list is really a list of segments (seg_pvs)
 
4831
 * containing a list of PVs that are in use by that segment.
 
4832
 *
 
4833
 * use_pvmove_parent_lv:  For pvmove we use the *parent* LV so we can
 
4834
 *                        pick up stripes & existing mirrors etc.
 
4835
 * create_single_list  :  Instead of creating a list of segments that
 
4836
 *                        each contain a list of PVs, return a list
 
4837
 *                        containing just one segment (i.e. seg_pvs)
 
4838
 *                        that contains a list of all the PVs used by
 
4839
 *                        the entire LV and all it's segments.
4531
4840
 */
4532
4841
struct dm_list *build_parallel_areas_from_lv(struct logical_volume *lv,
4533
 
                                             unsigned use_pvmove_parent_lv)
 
4842
                                             unsigned use_pvmove_parent_lv,
 
4843
                                             unsigned create_single_list)
4534
4844
{
4535
4845
        struct cmd_context *cmd = lv->vg->cmd;
4536
4846
        struct dm_list *parallel_areas;
4537
 
        struct seg_pvs *spvs;
 
4847
        struct seg_pvs *spvs = NULL;
4538
4848
        uint32_t current_le = 0;
4539
4849
        uint32_t raid_multiple;
4540
4850
        struct lv_segment *seg = first_seg(lv);
4547
4857
        dm_list_init(parallel_areas);
4548
4858
 
4549
4859
        do {
4550
 
                if (!(spvs = dm_pool_zalloc(cmd->mem, sizeof(*spvs)))) {
4551
 
                        log_error("allocation failed");
4552
 
                        return NULL;
 
4860
                if (!spvs || !create_single_list) {
 
4861
                        if (!(spvs = dm_pool_zalloc(cmd->mem, sizeof(*spvs)))) {
 
4862
                                log_error("allocation failed");
 
4863
                                return NULL;
 
4864
                        }
 
4865
 
 
4866
                        dm_list_init(&spvs->pvs);
 
4867
                        dm_list_add(parallel_areas, &spvs->list);
4553
4868
                }
4554
 
 
4555
 
                dm_list_init(&spvs->pvs);
4556
 
 
4557
4869
                spvs->le = current_le;
4558
4870
                spvs->len = lv->le_count - current_le;
4559
4871
 
4560
 
                dm_list_add(parallel_areas, &spvs->list);
4561
 
 
4562
 
                if (use_pvmove_parent_lv && !(seg = find_seg_by_le(lv, current_le))) {
 
4872
                if (use_pvmove_parent_lv &&
 
4873
                    !(seg = find_seg_by_le(lv, current_le))) {
4563
4874
                        log_error("Failed to find segment for %s extent %" PRIu32,
4564
4875
                                  lv->name, current_le);
4565
4876
                        return 0;
4580
4891
                        seg->area_count - seg->segtype->parity_devs : 1;
4581
4892
        } while ((current_le * raid_multiple) < lv->le_count);
4582
4893
 
4583
 
        /* FIXME Merge adjacent segments with identical PV lists (avoids need for contiguous allocation attempts between successful allocations) */
 
4894
        if (create_single_list) {
 
4895
                spvs->le = 0;
 
4896
                spvs->len = lv->le_count;
 
4897
        }
 
4898
 
 
4899
        /*
 
4900
         * FIXME:  Merge adjacent segments with identical PV lists
 
4901
         * (avoids need for contiguous allocation attempts between
 
4902
         * successful allocations)
 
4903
         */
4584
4904
 
4585
4905
        return parallel_areas;
4586
4906
}
4760
5080
        /* Clear thin pool stacked messages */
4761
5081
        if (pool_lv && !pool_has_message(first_seg(pool_lv), lv, 0) &&
4762
5082
            !update_pool_lv(pool_lv, 1)) {
4763
 
                log_error("Failed to update thin pool %s.", pool_lv->name);
4764
 
                return 0;
 
5083
                if (force < DONT_PROMPT_OVERRIDE) {
 
5084
                        log_error("Failed to update thin pool %s.", pool_lv->name);
 
5085
                        return 0;
 
5086
                }
 
5087
                log_warn("WARNING: Forced to ignoring failure of pool metadata update %s.",
 
5088
                         pool_lv->name);
 
5089
                pool_lv = NULL; /* Do not retry */
4765
5090
        }
4766
5091
 
4767
5092
        visible = lv_is_visible(lv);
4846
5171
int lv_remove_with_dependencies(struct cmd_context *cmd, struct logical_volume *lv,
4847
5172
                                const force_t force, unsigned level)
4848
5173
{
4849
 
        percent_t snap_percent;
 
5174
        dm_percent_t snap_percent;
4850
5175
        struct dm_list *snh, *snht;
4851
5176
        struct lv_list *lvl;
4852
5177
        struct lvinfo info;
4866
5191
                                                  lv->name);
4867
5192
                                        return 0;
4868
5193
                                }
4869
 
                                if ((snap_percent != PERCENT_INVALID) &&
4870
 
                                     (snap_percent != PERCENT_MERGE_FAILED)) {
 
5194
                                if ((snap_percent != DM_PERCENT_INVALID) &&
 
5195
                                     (snap_percent != LVM_PERCENT_MERGE_FAILED)) {
4871
5196
                                        log_error("Can't remove merging snapshot logical volume \"%s\"",
4872
5197
                                                  lv->name);
4873
5198
                                        return 0;
4874
 
                                } else if ((snap_percent == PERCENT_MERGE_FAILED) &&
 
5199
                                } else if ((snap_percent == LVM_PERCENT_MERGE_FAILED) &&
4875
5200
                                         (force == PROMPT) &&
4876
5201
                                         yes_no_prompt("Removing snapshot \"%s\" that failed to merge may leave origin \"%s\" inconsistent. "
4877
5202
                                                       "Proceed? [y/n]: ", lv->name, origin_from_cow(lv)->name) == 'n')
5027
5352
        uint32_t s;
5028
5353
        struct dm_list *parallel_areas;
5029
5354
 
5030
 
        if (!(parallel_areas = build_parallel_areas_from_lv(layer_lv, 0)))
 
5355
        if (!(parallel_areas = build_parallel_areas_from_lv(layer_lv, 0, 0)))
5031
5356
                return_0;
5032
5357
 
5033
5358
        /* Loop through all LVs except itself */
5248
5573
                                           uint64_t status,
5249
5574
                                           const char *layer_suffix)
5250
5575
{
 
5576
        static char _suffixes[][8] = { "_tdata", "_cdata", "_corig" };
5251
5577
        int r;
5252
5578
        char *name;
5253
5579
        size_t len;
5254
 
        struct str_list *sl;
 
5580
        struct dm_str_list *sl;
5255
5581
        struct logical_volume *layer_lv;
5256
5582
        struct segment_type *segtype;
5257
5583
        struct lv_segment *mapseg;
5357
5683
         *   currently supported only for thin data layer
5358
5684
         *   FIXME: without strcmp it breaks mirrors....
5359
5685
         */
5360
 
        if (strcmp(layer_suffix, "_tdata") == 0) {
5361
 
                lv_names.old = lv_where->name;
5362
 
                lv_names.new = layer_lv->name;
5363
 
                if (!for_each_sub_lv(layer_lv, _rename_cb, (void *) &lv_names))
5364
 
                        return 0;
5365
 
        }
 
5686
        for (r = 0; r < DM_ARRAY_SIZE(_suffixes); ++r)
 
5687
                if (strcmp(layer_suffix, _suffixes[r]) == 0) {
 
5688
                        lv_names.old = lv_where->name;
 
5689
                        lv_names.new = layer_lv->name;
 
5690
                        if (!for_each_sub_lv(layer_lv, _rename_cb, (void *) &lv_names))
 
5691
                                return 0;
 
5692
                        break;
 
5693
                }
5366
5694
 
5367
5695
        return layer_lv;
5368
5696
}
5721
6049
            override_lv_skip_flag)
5722
6050
                return 0;
5723
6051
 
5724
 
        log_verbose("ACTIVATON_SKIP flag set for LV %s/%s, skipping activation.",
 
6052
        log_verbose("ACTIVATION_SKIP flag set for LV %s/%s, skipping activation.",
5725
6053
                    lv->vg->name, lv->name);
5726
6054
        return 1;
5727
6055
}
5728
6056
 
5729
 
/* Greatest common divisor */
5730
 
static unsigned long _gcd(unsigned long n1, unsigned long n2)
5731
 
{
5732
 
        unsigned long remainder;
5733
 
 
5734
 
        do {
5735
 
                remainder = n1 % n2;
5736
 
                n1 = n2;
5737
 
                n2 = remainder;
5738
 
        } while (n2);
5739
 
 
5740
 
        return n1;
5741
 
}
5742
 
 
5743
 
/* Least common multiple */
5744
 
static unsigned long _lcm(unsigned long n1, unsigned long n2)
5745
 
{
5746
 
        if (!n1 || !n2)
5747
 
                return 0;
5748
 
        return (n1 * n2) / _gcd(n1, n2);
5749
 
}
5750
 
 
5751
 
static int _recalculate_pool_chunk_size_with_dev_hints(struct lvcreate_params *lp,
5752
 
                                                       struct logical_volume *pool_lv)
5753
 
{
5754
 
        struct logical_volume *pool_data_lv;
5755
 
        struct lv_segment *seg;
5756
 
        struct physical_volume *pv;
5757
 
        struct cmd_context *cmd = pool_lv->vg->cmd;
5758
 
        unsigned long previous_hint = 0, hint = 0;
5759
 
        uint32_t chunk_size = lp->chunk_size;
5760
 
        uint32_t default_chunk_size;
5761
 
        uint32_t min_chunk_size, max_chunk_size;
5762
 
 
5763
 
        if (lp->passed_args & PASS_ARG_CHUNK_SIZE)
5764
 
                goto out;
5765
 
 
5766
 
        if (seg_is_thin_pool(lp)) {
5767
 
                if (find_config_tree_int(cmd, allocation_thin_pool_chunk_size_CFG, NULL))
5768
 
                        goto out;
5769
 
 
5770
 
                min_chunk_size = DM_THIN_MIN_DATA_BLOCK_SIZE;
5771
 
                max_chunk_size = DM_THIN_MAX_DATA_BLOCK_SIZE;
5772
 
                default_chunk_size = get_default_allocation_thin_pool_chunk_size_CFG(cmd, NULL) * 2;
5773
 
        } else if (seg_is_cache_pool(lp)) {
5774
 
                if (find_config_tree_int(cmd, allocation_cache_pool_chunk_size_CFG, NULL))
5775
 
                        goto out;
5776
 
                min_chunk_size = DM_CACHE_MIN_DATA_BLOCK_SIZE;
5777
 
                max_chunk_size = DM_CACHE_MAX_DATA_BLOCK_SIZE;
5778
 
                default_chunk_size = get_default_allocation_cache_pool_chunk_size_CFG(cmd, NULL) * 2;
5779
 
        } else {
5780
 
                log_error(INTERNAL_ERROR "%s is not a thin pool or cache pool",
5781
 
                          pool_lv->name);
5782
 
                return 0;
5783
 
        }
5784
 
 
5785
 
        pool_data_lv = seg_lv(first_seg(pool_lv), 0);
5786
 
 
5787
 
        dm_list_iterate_items(seg, &pool_data_lv->segments) {
5788
 
                pv = seg_pv(seg, 0);
5789
 
                if (lp->thin_chunk_size_calc_policy == THIN_CHUNK_SIZE_CALC_METHOD_PERFORMANCE)
5790
 
                        hint = dev_optimal_io_size(cmd->dev_types, pv_dev(pv));
5791
 
                else
5792
 
                        hint = dev_minimum_io_size(cmd->dev_types, pv_dev(pv));
5793
 
 
5794
 
                if (!hint)
5795
 
                        continue;
5796
 
                if (previous_hint)
5797
 
                        hint = _lcm(previous_hint, hint);
5798
 
                previous_hint = hint;
5799
 
        }
5800
 
 
5801
 
        if (!hint) {
5802
 
                log_debug_alloc("No usable device hint found while recalculating"
5803
 
                                " thin pool chunk size for %s.", pool_lv->name);
5804
 
                goto out;
5805
 
        }
5806
 
 
5807
 
        if ((hint < min_chunk_size) || (hint > max_chunk_size)) {
5808
 
                log_debug_alloc("Calculated chunk size value of %ld sectors for"
5809
 
                                " thin pool %s is out of allowed range (%d-%d).",
5810
 
                                hint, pool_lv->name,
5811
 
                                min_chunk_size, max_chunk_size);
5812
 
        } else
5813
 
                chunk_size = (hint >= default_chunk_size) ?
5814
 
                        hint : default_chunk_size;
5815
 
out:
5816
 
        first_seg(pool_lv)->chunk_size = chunk_size;
5817
 
        return 1;
5818
 
}
5819
 
 
5820
6057
static int _should_wipe_lv(struct lvcreate_params *lp, struct logical_volume *lv) {
5821
6058
        int r = lp->zero | lp->wipe_signatures;
5822
6059
 
5849
6086
 
5850
6087
        if (new_lv_name && find_lv_in_vg(vg, new_lv_name)) {
5851
6088
                log_error("Logical volume \"%s\" already exists in "
5852
 
                          "volume group \"%s\"", new_lv_name, lp->vg_name);
 
6089
                          "volume group \"%s\"", new_lv_name, vg->name);
5853
6090
                return NULL;
5854
6091
        }
5855
6092
 
5963
6200
                                                            "cache-pool")))
5964
6201
                        return_0;
5965
6202
        } else if (seg_is_thin(lp) && lp->snapshot) {
 
6203
                if (!lp->origin) {
 
6204
                        log_error(INTERNAL_ERROR "Origin LV is not defined.");
 
6205
                        return 0;
 
6206
                }
5966
6207
                if (!(org = find_lv(vg, lp->origin))) {
5967
6208
                        log_error("Couldn't find origin volume '%s'.",
5968
6209
                                  lp->origin);
6043
6284
                return NULL;
6044
6285
        }
6045
6286
 
6046
 
        if (seg_is_thin_pool(lp) || seg_is_cache_pool(lp)) {
 
6287
        if (seg_is_pool(lp)) {
6047
6288
                if (((uint64_t)lp->extents * vg->extent_size < lp->chunk_size)) {
6048
6289
                        log_error("Unable to create %s smaller than 1 chunk.",
6049
6290
                                  lp->segtype->name);
6073
6314
        if (!activation() &&
6074
6315
            (seg_is_mirrored(lp) ||
6075
6316
             seg_is_raid(lp) ||
6076
 
             seg_is_thin_pool(lp) ||
6077
 
             seg_is_cache_pool(lp))) {
 
6317
             seg_is_pool(lp))) {
6078
6318
                /*
6079
6319
                 * FIXME: For thin pool add some code to allow delayed
6080
6320
                 * initialization of empty thin pool volume.
6083
6323
                 */
6084
6324
                log_error("Can't create %s without using "
6085
6325
                          "device-mapper kernel driver.",
6086
 
                          seg_is_thin_pool(lp) ? "thin pool volume" :
6087
6326
                          lp->segtype->name);
6088
6327
                return NULL;
6089
6328
        }
6106
6345
 
6107
6346
        if (seg_is_thin_volume(lp)) {
6108
6347
                /* Ensure all stacked messages are submitted */
 
6348
                if (!lp->pool) {
 
6349
                        log_error(INTERNAL_ERROR "Undefined pool for thin volume segment.");
 
6350
                        return NULL;
 
6351
                }
 
6352
 
6109
6353
                if (!(lvl = find_lv_in_vg(vg, lp->pool))) {
6110
6354
                        log_error("Unable to find existing pool LV %s in VG %s.",
6111
6355
                                  lp->pool, vg->name);
6169
6413
        if (!lv_extend(lv, lp->segtype,
6170
6414
                       lp->stripes, lp->stripe_size,
6171
6415
                       lp->mirrors,
6172
 
                       (seg_is_thin_pool(lp) || seg_is_cache_pool(lp)) ?
6173
 
                       lp->poolmetadataextents : lp->region_size,
 
6416
                       seg_is_pool(lp) ? lp->poolmetadataextents : lp->region_size,
6174
6417
                       seg_is_thin_volume(lp) ? lp->voriginextents : lp->extents,
6175
6418
                       thin_name, lp->pvh, lp->alloc, lp->approx_alloc))
6176
6419
                return_NULL;
6177
6420
 
6178
6421
        if (seg_is_cache_pool(lp)) {
6179
 
                if (!_recalculate_pool_chunk_size_with_dev_hints(lp, lv))
6180
 
                        return_NULL;
 
6422
                first_seg(lv)->chunk_size = lp->chunk_size;
6181
6423
                first_seg(lv)->feature_flags = lp->feature_flags;
 
6424
                /* TODO: some calc_policy solution for cache ? */
 
6425
                if (!recalculate_pool_chunk_size_with_dev_hints(lv, lp->passed_args,
 
6426
                                                                THIN_CHUNK_SIZE_CALC_METHOD_GENERIC))
 
6427
                        return_NULL;
6182
6428
        } else if (seg_is_thin_pool(lp)) {
6183
 
                if (!_recalculate_pool_chunk_size_with_dev_hints(lp, lv))
6184
 
                        return_NULL;
 
6429
                first_seg(lv)->chunk_size = lp->chunk_size;
6185
6430
                first_seg(lv)->zero_new_blocks = lp->zero ? 1 : 0;
6186
6431
                first_seg(lv)->discards = lp->discards;
6187
6432
                /* FIXME: use lowwatermark  via lvm.conf global for all thinpools ? */
6188
6433
                first_seg(lv)->low_water_mark = 0;
 
6434
                if (!recalculate_pool_chunk_size_with_dev_hints(lv, lp->passed_args,
 
6435
                                                                lp->thin_chunk_size_calc_policy))
 
6436
                        return_NULL;
6189
6437
        } else if (seg_is_thin_volume(lp)) {
6190
6438
                pool_lv = first_seg(lv)->pool_lv;
6191
6439
                if (!(first_seg(lv)->device_id =