Tizen 2.1 base
[external/device-mapper.git] / lib / metadata / lv_manip.c
1 /*
2  * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
3  * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
4  *
5  * This file is part of LVM2.
6  *
7  * This copyrighted material is made available to anyone wishing to use,
8  * modify, copy, or redistribute it subject to the terms and conditions
9  * of the GNU Lesser General Public License v.2.1.
10  *
11  * You should have received a copy of the GNU Lesser General Public License
12  * along with this program; if not, write to the Free Software Foundation,
13  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
14  */
15
16 #include "lib.h"
17 #include "metadata.h"
18 #include "locking.h"
19 #include "pv_map.h"
20 #include "lvm-string.h"
21 #include "toolcontext.h"
22 #include "lv_alloc.h"
23 #include "pv_alloc.h"
24 #include "display.h"
25 #include "segtype.h"
26 #include "archiver.h"
27 #include "activate.h"
28 #include "str_list.h"
29
30 struct lv_names {
31         const char *old;
32         const char *new;
33 };
34
35 int add_seg_to_segs_using_this_lv(struct logical_volume *lv,
36                                   struct lv_segment *seg)
37 {
38         struct seg_list *sl;
39
40         dm_list_iterate_items(sl, &lv->segs_using_this_lv) {
41                 if (sl->seg == seg) {
42                         sl->count++;
43                         return 1;
44                 }
45         }
46
47         log_very_verbose("Adding %s:%" PRIu32 " as an user of %s",
48                          seg->lv->name, seg->le, lv->name);
49
50         if (!(sl = dm_pool_zalloc(lv->vg->vgmem, sizeof(*sl)))) {
51                 log_error("Failed to allocate segment list");
52                 return 0;
53         }
54
55         sl->count = 1;
56         sl->seg = seg;
57         dm_list_add(&lv->segs_using_this_lv, &sl->list);
58
59         return 1;
60 }
61
62 int remove_seg_from_segs_using_this_lv(struct logical_volume *lv,
63                                        struct lv_segment *seg)
64 {
65         struct seg_list *sl;
66
67         dm_list_iterate_items(sl, &lv->segs_using_this_lv) {
68                 if (sl->seg != seg)
69                         continue;
70                 if (sl->count > 1)
71                         sl->count--;
72                 else {
73                         log_very_verbose("%s:%" PRIu32 " is no longer a user "
74                                          "of %s", seg->lv->name, seg->le,
75                                          lv->name);
76                         dm_list_del(&sl->list);
77                 }
78                 return 1;
79         }
80
81         return 0;
82 }
83
84 /*
85  * This is a function specialized for the common case where there is
86  * only one segment which uses the LV.
87  * e.g. the LV is a layer inserted by insert_layer_for_lv().
88  *
89  * In general, walk through lv->segs_using_this_lv.
90  */
91 struct lv_segment *get_only_segment_using_this_lv(struct logical_volume *lv)
92 {
93         struct seg_list *sl;
94
95         if (dm_list_size(&lv->segs_using_this_lv) != 1) {
96                 log_error("%s is expected to have only one segment using it, "
97                           "while it has %d", lv->name,
98                           dm_list_size(&lv->segs_using_this_lv));
99                 return NULL;
100         }
101
102         sl = dm_list_item(dm_list_first(&lv->segs_using_this_lv), struct seg_list);
103
104         if (sl->count != 1) {
105                 log_error("%s is expected to have only one segment using it, "
106                           "while %s:%" PRIu32 " uses it %d times",
107                           lv->name, sl->seg->lv->name, sl->seg->le, sl->count);
108                 return NULL;
109         }
110
111         return sl->seg;
112 }
113
114 /*
115  * PVs used by a segment of an LV
116  */
117 struct seg_pvs {
118         struct dm_list list;
119
120         struct dm_list pvs;     /* struct pv_list */
121
122         uint32_t le;
123         uint32_t len;
124 };
125
126 static struct seg_pvs *_find_seg_pvs_by_le(struct dm_list *list, uint32_t le)
127 {
128         struct seg_pvs *spvs;
129
130         dm_list_iterate_items(spvs, list)
131                 if (le >= spvs->le && le < spvs->le + spvs->len)
132                         return spvs;
133
134         return NULL;
135 }
136
137 /*
138  * Find first unused LV number.
139  */
140 uint32_t find_free_lvnum(struct logical_volume *lv)
141 {
142         int lvnum_used[MAX_RESTRICTED_LVS + 1];
143         uint32_t i = 0;
144         struct lv_list *lvl;
145         int lvnum;
146
147         memset(&lvnum_used, 0, sizeof(lvnum_used));
148
149         dm_list_iterate_items(lvl, &lv->vg->lvs) {
150                 lvnum = lvnum_from_lvid(&lvl->lv->lvid);
151                 if (lvnum <= MAX_RESTRICTED_LVS)
152                         lvnum_used[lvnum] = 1;
153         }
154
155         while (lvnum_used[i])
156                 i++;
157
158         /* FIXME What if none are free? */
159
160         return i;
161 }
162
163 /*
164  * All lv_segments get created here.
165  */
166 struct lv_segment *alloc_lv_segment(struct dm_pool *mem,
167                                     const struct segment_type *segtype,
168                                     struct logical_volume *lv,
169                                     uint32_t le, uint32_t len,
170                                     uint64_t status,
171                                     uint32_t stripe_size,
172                                     struct logical_volume *log_lv,
173                                     uint32_t area_count,
174                                     uint32_t area_len,
175                                     uint32_t chunk_size,
176                                     uint32_t region_size,
177                                     uint32_t extents_copied,
178                                     struct lv_segment *pvmove_source_seg)
179 {
180         struct lv_segment *seg;
181         uint32_t areas_sz = area_count * sizeof(*seg->areas);
182
183         if (!(seg = dm_pool_zalloc(mem, sizeof(*seg))))
184                 return_NULL;
185
186         if (!(seg->areas = dm_pool_zalloc(mem, areas_sz))) {
187                 dm_pool_free(mem, seg);
188                 return_NULL;
189         }
190
191         if (!segtype) {
192                 log_error("alloc_lv_segment: Missing segtype.");
193                 return NULL;
194         }
195
196         seg->segtype = segtype;
197         seg->lv = lv;
198         seg->le = le;
199         seg->len = len;
200         seg->status = status;
201         seg->stripe_size = stripe_size;
202         seg->area_count = area_count;
203         seg->area_len = area_len;
204         seg->chunk_size = chunk_size;
205         seg->region_size = region_size;
206         seg->extents_copied = extents_copied;
207         seg->log_lv = log_lv;
208         seg->pvmove_source_seg = pvmove_source_seg;
209         dm_list_init(&seg->tags);
210
211         if (log_lv && !attach_mirror_log(seg, log_lv))
212                 return_NULL;
213
214         return seg;
215 }
216
217 struct lv_segment *alloc_snapshot_seg(struct logical_volume *lv,
218                                       uint64_t status, uint32_t old_le_count)
219 {
220         struct lv_segment *seg;
221         const struct segment_type *segtype;
222
223         segtype = get_segtype_from_string(lv->vg->cmd, "snapshot");
224         if (!segtype) {
225                 log_error("Failed to find snapshot segtype");
226                 return NULL;
227         }
228
229         if (!(seg = alloc_lv_segment(lv->vg->cmd->mem, segtype, lv, old_le_count,
230                                      lv->le_count - old_le_count, status, 0,
231                                      NULL, 0, lv->le_count - old_le_count,
232                                      0, 0, 0, NULL))) {
233                 log_error("Couldn't allocate new snapshot segment.");
234                 return NULL;
235         }
236
237         dm_list_add(&lv->segments, &seg->list);
238         lv->status |= VIRTUAL;
239
240         return seg;
241 }
242
243 void release_lv_segment_area(struct lv_segment *seg, uint32_t s,
244                              uint32_t area_reduction)
245 {
246         if (seg_type(seg, s) == AREA_UNASSIGNED)
247                 return;
248
249         if (seg_type(seg, s) == AREA_PV) {
250                 if (release_pv_segment(seg_pvseg(seg, s), area_reduction) &&
251                     seg->area_len == area_reduction)
252                         seg_type(seg, s) = AREA_UNASSIGNED;
253                 return;
254         }
255
256         if (seg_lv(seg, s)->status & MIRROR_IMAGE) {
257                 lv_reduce(seg_lv(seg, s), area_reduction);
258                 return;
259         }
260
261         if (area_reduction == seg->area_len) {
262                 log_very_verbose("Remove %s:%" PRIu32 "[%" PRIu32 "] from "
263                                  "the top of LV %s:%" PRIu32,
264                                  seg->lv->name, seg->le, s,
265                                  seg_lv(seg, s)->name, seg_le(seg, s));
266
267                 remove_seg_from_segs_using_this_lv(seg_lv(seg, s), seg);
268                 seg_lv(seg, s) = NULL;
269                 seg_le(seg, s) = 0;
270                 seg_type(seg, s) = AREA_UNASSIGNED;
271         }
272 }
273
274 /*
275  * Move a segment area from one segment to another
276  */
277 int move_lv_segment_area(struct lv_segment *seg_to, uint32_t area_to,
278                          struct lv_segment *seg_from, uint32_t area_from)
279 {
280         struct physical_volume *pv;
281         struct logical_volume *lv;
282         uint32_t pe, le;
283
284         switch (seg_type(seg_from, area_from)) {
285         case AREA_PV:
286                 pv = seg_pv(seg_from, area_from);
287                 pe = seg_pe(seg_from, area_from);
288
289                 release_lv_segment_area(seg_from, area_from,
290                                         seg_from->area_len);
291                 release_lv_segment_area(seg_to, area_to, seg_to->area_len);
292
293                 if (!set_lv_segment_area_pv(seg_to, area_to, pv, pe))
294                         return_0;
295
296                 break;
297
298         case AREA_LV:
299                 lv = seg_lv(seg_from, area_from);
300                 le = seg_le(seg_from, area_from);
301
302                 release_lv_segment_area(seg_from, area_from,
303                                         seg_from->area_len);
304                 release_lv_segment_area(seg_to, area_to, seg_to->area_len);
305
306                 if (!set_lv_segment_area_lv(seg_to, area_to, lv, le, 0))
307                         return_0;
308
309                 break;
310
311         case AREA_UNASSIGNED:
312                 release_lv_segment_area(seg_to, area_to, seg_to->area_len);
313         }
314
315         return 1;
316 }
317
318 /*
319  * Link part of a PV to an LV segment.
320  */
321 int set_lv_segment_area_pv(struct lv_segment *seg, uint32_t area_num,
322                            struct physical_volume *pv, uint32_t pe)
323 {
324         seg->areas[area_num].type = AREA_PV;
325
326         if (!(seg_pvseg(seg, area_num) =
327               assign_peg_to_lvseg(pv, pe, seg->area_len, seg, area_num)))
328                 return_0;
329
330         return 1;
331 }
332
333 /*
334  * Link one LV segment to another.  Assumes sizes already match.
335  */
336 int set_lv_segment_area_lv(struct lv_segment *seg, uint32_t area_num,
337                            struct logical_volume *lv, uint32_t le,
338                            uint64_t status)
339 {
340         log_very_verbose("Stack %s:%" PRIu32 "[%" PRIu32 "] on LV %s:%" PRIu32,
341                          seg->lv->name, seg->le, area_num, lv->name, le);
342
343         seg->areas[area_num].type = AREA_LV;
344         seg_lv(seg, area_num) = lv;
345         seg_le(seg, area_num) = le;
346         lv->status |= status;
347
348         if (!add_seg_to_segs_using_this_lv(lv, seg))
349                 return_0;
350
351         return 1;
352 }
353
354 /*
355  * Prepare for adding parallel areas to an existing segment.
356  */
357 static int _lv_segment_add_areas(struct logical_volume *lv,
358                                  struct lv_segment *seg,
359                                  uint32_t new_area_count)
360 {
361         struct lv_segment_area *newareas;
362         uint32_t areas_sz = new_area_count * sizeof(*newareas);
363
364         if (!(newareas = dm_pool_zalloc(lv->vg->cmd->mem, areas_sz)))
365                 return_0;
366
367         memcpy(newareas, seg->areas, seg->area_count * sizeof(*seg->areas));
368
369         seg->areas = newareas;
370         seg->area_count = new_area_count;
371
372         return 1;
373 }
374
375 /*
376  * Reduce the size of an lv_segment.  New size can be zero.
377  */
378 static int _lv_segment_reduce(struct lv_segment *seg, uint32_t reduction)
379 {
380         uint32_t area_reduction, s;
381
382         /* Caller must ensure exact divisibility */
383         if (seg_is_striped(seg)) {
384                 if (reduction % seg->area_count) {
385                         log_error("Segment extent reduction %" PRIu32
386                                   "not divisible by #stripes %" PRIu32,
387                                   reduction, seg->area_count);
388                         return 0;
389                 }
390                 area_reduction = (reduction / seg->area_count);
391         } else
392                 area_reduction = reduction;
393
394         for (s = 0; s < seg->area_count; s++)
395                 release_lv_segment_area(seg, s, area_reduction);
396
397         seg->len -= reduction;
398         seg->area_len -= area_reduction;
399
400         return 1;
401 }
402
403 /*
404  * Entry point for all LV reductions in size.
405  */
406 static int _lv_reduce(struct logical_volume *lv, uint32_t extents, int delete)
407 {
408         struct lv_segment *seg;
409         uint32_t count = extents;
410         uint32_t reduction;
411
412         dm_list_iterate_back_items(seg, &lv->segments) {
413                 if (!count)
414                         break;
415
416                 if (seg->len <= count) {
417                         /* remove this segment completely */
418                         /* FIXME Check this is safe */
419                         if (seg->log_lv && !lv_remove(seg->log_lv))
420                                 return_0;
421                         dm_list_del(&seg->list);
422                         reduction = seg->len;
423                 } else
424                         reduction = count;
425
426                 if (!_lv_segment_reduce(seg, reduction))
427                         return_0;
428                 count -= reduction;
429         }
430
431         lv->le_count -= extents;
432         lv->size = (uint64_t) lv->le_count * lv->vg->extent_size;
433
434         if (!delete)
435                 return 1;
436
437         /* Remove the LV if it is now empty */
438         if (!lv->le_count && !unlink_lv_from_vg(lv))
439                 return_0;
440         else if (lv->vg->fid->fmt->ops->lv_setup &&
441                    !lv->vg->fid->fmt->ops->lv_setup(lv->vg->fid, lv))
442                 return_0;
443
444         return 1;
445 }
446
447 /*
448  * Empty an LV.
449  */
450 int lv_empty(struct logical_volume *lv)
451 {
452         return _lv_reduce(lv, lv->le_count, 0);
453 }
454
455 /*
456  * Empty an LV and add error segment.
457  */
458 int replace_lv_with_error_segment(struct logical_volume *lv)
459 {
460         uint32_t len = lv->le_count;
461
462         if (!lv_empty(lv))
463                 return_0;
464
465         /*
466          * Since we are replacing the whatever-was-there with
467          * an error segment, we should also clear any flags
468          * that suggest it is anything other than "error".
469          */
470         lv->status &= ~MIRRORED;
471
472         /* FIXME: Should we bug if we find a log_lv attached? */
473
474         if (!lv_add_virtual_segment(lv, 0, len,
475                                     get_segtype_from_string(lv->vg->cmd,
476                                                             "error")))
477                 return_0;
478
479         return 1;
480 }
481
482 /*
483  * Remove given number of extents from LV.
484  */
485 int lv_reduce(struct logical_volume *lv, uint32_t extents)
486 {
487         return _lv_reduce(lv, extents, 1);
488 }
489
490 /*
491  * Completely remove an LV.
492  */
493 int lv_remove(struct logical_volume *lv)
494 {
495
496         if (!lv_reduce(lv, lv->le_count))
497                 return_0;
498
499         return 1;
500 }
501
502 /*
503  * A set of contiguous physical extents allocated
504  */
505 struct alloced_area {
506         struct dm_list list;
507
508         struct physical_volume *pv;
509         uint32_t pe;
510         uint32_t len;
511 };
512
513 /*
514  * Details of an allocation attempt
515  */
516 struct alloc_handle {
517         struct cmd_context *cmd;
518         struct dm_pool *mem;
519
520         alloc_policy_t alloc;           /* Overall policy */
521         uint32_t new_extents;           /* Number of new extents required */
522         uint32_t area_count;            /* Number of parallel areas */
523         uint32_t area_multiple;         /* seg->len = area_len * area_multiple */
524         uint32_t log_area_count;        /* Number of parallel logs */
525         uint32_t log_len;               /* Length of log */
526         uint32_t region_size;           /* Mirror region size */
527         uint32_t total_area_len;        /* Total number of parallel extents */
528
529         const struct config_node *cling_tag_list_cn;
530
531         struct dm_list *parallel_areas; /* PVs to avoid */
532
533         /*
534          * Contains area_count lists of areas allocated to data stripes
535          * followed by log_area_count lists of areas allocated to log stripes.
536          */
537         struct dm_list alloced_areas[0];
538 };
539
540 static uint32_t _calc_area_multiple(const struct segment_type *segtype,
541                                     const uint32_t area_count, const uint32_t stripes)
542 {
543         if (!area_count)
544                 return 1;
545
546         /* Striped */
547         if (segtype_is_striped(segtype))
548                 return area_count;
549
550         /* Mirrored stripes */
551         if (stripes)
552                 return stripes;
553
554         /* Mirrored */
555         return 1;
556 }
557
558 /*
559  * Returns log device size in extents, algorithm from kernel code
560  */
561 #define BYTE_SHIFT 3
562 static uint32_t mirror_log_extents(uint32_t region_size, uint32_t pe_size, uint32_t area_len)
563 {
564         size_t area_size, bitset_size, log_size, region_count;
565
566         area_size = area_len * pe_size;
567         region_count = dm_div_up(area_size, region_size);
568
569         /* Work out how many "unsigned long"s we need to hold the bitset. */
570         bitset_size = dm_round_up(region_count, sizeof(uint32_t) << BYTE_SHIFT);
571         bitset_size >>= BYTE_SHIFT;
572
573         /* Log device holds both header and bitset. */
574         log_size = dm_round_up((MIRROR_LOG_OFFSET << SECTOR_SHIFT) + bitset_size, 1 << SECTOR_SHIFT);
575         log_size >>= SECTOR_SHIFT;
576
577         return dm_div_up(log_size, pe_size);
578 }
579
580 /*
581  * Preparation for a specific allocation attempt
582  * stripes and mirrors refer to the parallel areas used for data.
583  * If log_area_count > 1 it is always mirrored (not striped).
584  */
585 static struct alloc_handle *_alloc_init(struct cmd_context *cmd,
586                                         struct dm_pool *mem,
587                                         const struct segment_type *segtype,
588                                         alloc_policy_t alloc,
589                                         uint32_t new_extents,
590                                         uint32_t mirrors,
591                                         uint32_t stripes,
592                                         uint32_t log_area_count,
593                                         uint32_t extent_size,
594                                         uint32_t region_size,
595                                         struct dm_list *parallel_areas)
596 {
597         struct alloc_handle *ah;
598         uint32_t s, area_count;
599
600         /* FIXME Caller should ensure this */
601         if (mirrors && !stripes)
602                 stripes = 1;
603
604         if (segtype_is_virtual(segtype))
605                 area_count = 0;
606         else if (mirrors > 1)
607                 area_count = mirrors * stripes;
608         else
609                 area_count = stripes;
610
611         if (!(ah = dm_pool_zalloc(mem, sizeof(*ah) + sizeof(ah->alloced_areas[0]) * (area_count + log_area_count)))) {
612                 log_error("allocation handle allocation failed");
613                 return NULL;
614         }
615
616         ah->cmd = cmd;
617
618         if (segtype_is_virtual(segtype))
619                 return ah;
620
621         if (!(area_count + log_area_count)) {
622                 log_error(INTERNAL_ERROR "_alloc_init called for non-virtual segment with no disk space.");
623                 return NULL;
624         }
625
626         if (!(ah->mem = dm_pool_create("allocation", 1024))) {
627                 log_error("allocation pool creation failed");
628                 return NULL;
629         }
630
631         ah->new_extents = new_extents;
632         ah->area_count = area_count;
633         ah->log_area_count = log_area_count;
634         ah->region_size = region_size;
635         ah->alloc = alloc;
636         ah->area_multiple = _calc_area_multiple(segtype, area_count, stripes);
637
638         ah->log_len = log_area_count ? mirror_log_extents(ah->region_size, extent_size, ah->new_extents / ah->area_multiple) : 0;
639
640         for (s = 0; s < ah->area_count + ah->log_area_count; s++)
641                 dm_list_init(&ah->alloced_areas[s]);
642
643         ah->parallel_areas = parallel_areas;
644
645         ah->cling_tag_list_cn = find_config_tree_node(cmd, "allocation/cling_tag_list");
646
647         return ah;
648 }
649
650 void alloc_destroy(struct alloc_handle *ah)
651 {
652         if (ah->mem)
653                 dm_pool_destroy(ah->mem);
654 }
655
656 static int _log_parallel_areas(struct dm_pool *mem, struct dm_list *parallel_areas)
657 {
658         struct seg_pvs *spvs;
659         struct pv_list *pvl;
660         char *pvnames;
661
662         if (!parallel_areas)
663                 return 1;
664
665         dm_list_iterate_items(spvs, parallel_areas) {
666                 if (!dm_pool_begin_object(mem, 256)) {
667                         log_error("dm_pool_begin_object failed");
668                         return 0;
669                 }
670
671                 dm_list_iterate_items(pvl, &spvs->pvs) {
672                         if (!dm_pool_grow_object(mem, pv_dev_name(pvl->pv), strlen(pv_dev_name(pvl->pv)))) {
673                                 log_error("dm_pool_grow_object failed");
674                                 dm_pool_abandon_object(mem);
675                                 return 0;
676                         }
677                         if (!dm_pool_grow_object(mem, " ", 1)) {
678                                 log_error("dm_pool_grow_object failed");
679                                 dm_pool_abandon_object(mem);
680                                 return 0;
681                         }
682                 }
683
684                 if (!dm_pool_grow_object(mem, "\0", 1)) {
685                         log_error("dm_pool_grow_object failed");
686                         dm_pool_abandon_object(mem);
687                         return 0;
688                 }
689
690                 pvnames = dm_pool_end_object(mem);
691                 log_debug("Parallel PVs at LE %" PRIu32 " length %" PRIu32 ": %s",
692                           spvs->le, spvs->len, pvnames);
693                 dm_pool_free(mem, pvnames);
694         }
695
696         return 1;
697 }
698
699 static int _setup_alloced_segment(struct logical_volume *lv, uint64_t status,
700                                   uint32_t area_count,
701                                   uint32_t stripe_size,
702                                   const struct segment_type *segtype,
703                                   struct alloced_area *aa,
704                                   uint32_t region_size)
705 {
706         uint32_t s, extents, area_multiple;
707         struct lv_segment *seg;
708
709         area_multiple = _calc_area_multiple(segtype, area_count, 0);
710
711         if (!(seg = alloc_lv_segment(lv->vg->cmd->mem, segtype, lv,
712                                      lv->le_count,
713                                      aa[0].len * area_multiple,
714                                      status, stripe_size, NULL,
715                                      area_count,
716                                      aa[0].len, 0u, region_size, 0u, NULL))) {
717                 log_error("Couldn't allocate new LV segment.");
718                 return 0;
719         }
720
721         for (s = 0; s < area_count; s++)
722                 if (!set_lv_segment_area_pv(seg, s, aa[s].pv, aa[s].pe))
723                         return_0;
724
725         dm_list_add(&lv->segments, &seg->list);
726
727         extents = aa[0].len * area_multiple;
728         lv->le_count += extents;
729         lv->size += (uint64_t) extents *lv->vg->extent_size;
730
731         if (segtype_is_mirrored(segtype))
732                 lv->status |= MIRRORED;
733
734         return 1;
735 }
736
737 static int _setup_alloced_segments(struct logical_volume *lv,
738                                    struct dm_list *alloced_areas,
739                                    uint32_t area_count,
740                                    uint64_t status,
741                                    uint32_t stripe_size,
742                                    const struct segment_type *segtype,
743                                    uint32_t region_size)
744 {
745         struct alloced_area *aa;
746
747         dm_list_iterate_items(aa, &alloced_areas[0]) {
748                 if (!_setup_alloced_segment(lv, status, area_count,
749                                             stripe_size, segtype, aa,
750                                             region_size))
751                         return_0;
752         }
753
754         return 1;
755 }
756
757 /*
758  * This function takes a list of pv_areas and adds them to allocated_areas.
759  * If the complete area is not needed then it gets split.
760  * The part used is removed from the pv_map so it can't be allocated twice.
761  */
762 static int _alloc_parallel_area(struct alloc_handle *ah, uint32_t needed,
763                                 struct pv_area_used *areas, uint32_t *allocated,
764                                 unsigned log_needs_allocating, uint32_t ix_log_offset)
765 {
766         uint32_t area_len, len, remaining;
767         uint32_t s;
768         uint32_t ix_log_skip = 0; /* How many areas to skip in middle of array to reach log areas */
769         uint32_t total_area_count = ah->area_count + (log_needs_allocating ? ah->log_area_count : 0);
770         struct alloced_area *aa;
771
772         if (!total_area_count) {
773                 log_error(INTERNAL_ERROR "_alloc_parallel_area called without any allocation to do.");
774                 return 1;
775         }
776
777         remaining = needed - *allocated;
778         area_len = remaining / ah->area_multiple;
779
780         /* Reduce area_len to the smallest of the areas */
781         for (s = 0; s < ah->area_count; s++)
782                 if (area_len > areas[s].used)
783                         area_len = areas[s].used;
784
785         if (!(aa = dm_pool_alloc(ah->mem, sizeof(*aa) * total_area_count))) {
786                 log_error("alloced_area allocation failed");
787                 return 0;
788         }
789
790         /*
791          * Areas consists of area_count areas for data stripes, then
792          * ix_log_skip areas to skip, then log_area_count areas to use for the
793          * log, then some areas too small for the log.
794          */
795         len = area_len;
796         for (s = 0; s < total_area_count; s++) {
797                 if (s == ah->area_count) {
798                         ix_log_skip = ix_log_offset - ah->area_count;
799                         len = ah->log_len;
800                 }
801
802                 aa[s].pv = areas[s + ix_log_skip].pva->map->pv;
803                 aa[s].pe = areas[s + ix_log_skip].pva->start;
804                 aa[s].len = len;
805
806                 log_debug("Allocating parallel area %" PRIu32
807                           " on %s start PE %" PRIu32 " length %" PRIu32 ".",
808                           s, dev_name(aa[s].pv->dev), aa[s].pe, len);
809
810                 consume_pv_area(areas[s + ix_log_skip].pva, len);
811
812                 dm_list_add(&ah->alloced_areas[s], &aa[s].list);
813         }
814
815         ah->total_area_len += area_len;
816
817         *allocated += area_len * ah->area_multiple;
818
819         return 1;
820 }
821
822 /* For striped mirrors, all the areas are counted, through the mirror layer */
823 static uint32_t _stripes_per_mimage(struct lv_segment *seg)
824 {
825         struct lv_segment *last_lvseg;
826
827         if (seg_is_mirrored(seg) && seg->area_count && seg_type(seg, 0) == AREA_LV) {
828                 last_lvseg = dm_list_item(dm_list_last(&seg_lv(seg, 0)->segments), struct lv_segment);
829                 if (seg_is_striped(last_lvseg))
830                         return last_lvseg->area_count;
831         }
832
833         return 1;
834 }
835
836 /*
837  * Call fn for each AREA_PV used by the LV segment at lv:le of length *max_seg_len.
838  * If any constituent area contains more than one segment, max_seg_len is
839  * reduced to cover only the first.
840  * fn should return 0 on error, 1 to continue scanning or >1 to terminate without error.
841  * In the last case, this function passes on the return code.
842  */
843 static int _for_each_pv(struct cmd_context *cmd, struct logical_volume *lv,
844                         uint32_t le, uint32_t len, struct lv_segment *seg,
845                         uint32_t *max_seg_len,
846                         uint32_t first_area, uint32_t max_areas,
847                         int top_level_area_index,
848                         int only_single_area_segments,
849                         int (*fn)(struct cmd_context *cmd,
850                                   struct pv_segment *peg, uint32_t s,
851                                   void *data),
852                         void *data)
853 {
854         uint32_t s;
855         uint32_t remaining_seg_len, area_len, area_multiple;
856         uint32_t stripes_per_mimage = 1;
857         int r = 1;
858
859         if (!seg && !(seg = find_seg_by_le(lv, le))) {
860                 log_error("Failed to find segment for %s extent %" PRIu32,
861                           lv->name, le);
862                 return 0;
863         }
864
865         /* Remaining logical length of segment */
866         remaining_seg_len = seg->len - (le - seg->le);
867
868         if (remaining_seg_len > len)
869                 remaining_seg_len = len;
870
871         if (max_seg_len && *max_seg_len > remaining_seg_len)
872                 *max_seg_len = remaining_seg_len;
873
874         area_multiple = _calc_area_multiple(seg->segtype, seg->area_count, 0);
875         area_len = remaining_seg_len / area_multiple ? : 1;
876
877         /* For striped mirrors, all the areas are counted, through the mirror layer */
878         if (top_level_area_index == -1)
879                 stripes_per_mimage = _stripes_per_mimage(seg);
880
881         for (s = first_area;
882              s < seg->area_count && (!max_areas || s <= max_areas);
883              s++) {
884                 if (seg_type(seg, s) == AREA_LV) {
885                         if (!(r = _for_each_pv(cmd, seg_lv(seg, s),
886                                                seg_le(seg, s) +
887                                                (le - seg->le) / area_multiple,
888                                                area_len, NULL, max_seg_len, 0,
889                                                (stripes_per_mimage == 1) && only_single_area_segments ? 1U : 0U,
890                                                top_level_area_index != -1 ? top_level_area_index : (int) s * stripes_per_mimage,
891                                                only_single_area_segments, fn,
892                                                data)))
893                                 stack;
894                 } else if (seg_type(seg, s) == AREA_PV)
895                         if (!(r = fn(cmd, seg_pvseg(seg, s), top_level_area_index != -1 ? (uint32_t) top_level_area_index + s : s, data)))
896                                 stack;
897                 if (r != 1)
898                         return r;
899         }
900
901         /* FIXME only_single_area_segments used as workaround to skip log LV - needs new param? */
902         if (!only_single_area_segments && seg_is_mirrored(seg) && seg->log_lv) {
903                 if (!(r = _for_each_pv(cmd, seg->log_lv, 0, seg->log_lv->le_count, NULL,
904                                        NULL, 0, 0, 0, only_single_area_segments,
905                                        fn, data)))
906                         stack;
907                 if (r != 1)
908                         return r;
909         }
910
911         /* FIXME Add snapshot cow LVs etc. */
912
913         return 1;
914 }
915
916 static int _comp_area(const void *l, const void *r)
917 {
918         const struct pv_area_used *lhs = (const struct pv_area_used *) l;
919         const struct pv_area_used *rhs = (const struct pv_area_used *) r;
920
921         if (lhs->used < rhs->used)
922                 return 1;
923
924         else if (lhs->used > rhs->used)
925                 return -1;
926
927         return 0;
928 }
929
930 /*
931  * Search for pvseg that matches condition
932  */
933 struct pv_match {
934         int (*condition)(struct pv_match *pvmatch, struct pv_segment *pvseg, struct pv_area *pva);
935
936         struct pv_area_used *areas;
937         struct pv_area *pva;
938         uint32_t areas_size;
939         const struct config_node *cling_tag_list_cn;
940         int s;  /* Area index of match */
941 };
942
943 /*
944  * Is PV area on the same PV?
945  */
946 static int _is_same_pv(struct pv_match *pvmatch __attribute((unused)), struct pv_segment *pvseg, struct pv_area *pva)
947 {
948         if (pvseg->pv != pva->map->pv)
949                 return 0;
950
951         return 1;
952 }
953
954 /*
955  * Does PV area have a tag listed in allocation/cling_tag_list that 
956  * matches a tag of the PV of the existing segment?
957  */
958 static int _has_matching_pv_tag(struct pv_match *pvmatch, struct pv_segment *pvseg, struct pv_area *pva)
959 {
960         const struct config_value *cv;
961         const char *str;
962         const char *tag_matched;
963
964         for (cv = pvmatch->cling_tag_list_cn->v; cv; cv = cv->next) {
965                 if (cv->type != CFG_STRING) {
966                         log_error("Ignoring invalid string in config file entry "
967                                   "allocation/cling_tag_list");
968                         continue;
969                 }
970                 str = cv->v.str;
971                 if (!*str) {
972                         log_error("Ignoring empty string in config file entry "
973                                   "allocation/cling_tag_list");
974                         continue;
975                 }
976
977                 if (*str != '@') {
978                         log_error("Ignoring string not starting with @ in config file entry "
979                                   "allocation/cling_tag_list: %s", str);
980                         continue;
981                 }
982
983                 str++;
984
985                 if (!*str) {
986                         log_error("Ignoring empty tag in config file entry "
987                                   "allocation/cling_tag_list");
988                         continue;
989                 }
990
991                 /* Wildcard matches any tag against any tag. */
992                 if (!strcmp(str, "*")) {
993                         if (!str_list_match_list(&pvseg->pv->tags, &pva->map->pv->tags, &tag_matched))
994                                 continue;
995                         else {
996                                 log_debug("Matched allocation PV tag %s on existing %s with free space on %s.",
997                                           tag_matched, pv_dev_name(pvseg->pv), pv_dev_name(pva->map->pv));
998                                 return 1;
999                         }
1000                 }
1001
1002                 if (!str_list_match_item(&pvseg->pv->tags, str) ||
1003                     !str_list_match_item(&pva->map->pv->tags, str))
1004                         continue;
1005                 else {
1006                         log_debug("Matched allocation PV tag %s on existing %s with free space on %s.",
1007                                   str, pv_dev_name(pvseg->pv), pv_dev_name(pva->map->pv));
1008                         return 1;
1009                 }
1010         }
1011
1012         return 0;
1013 }
1014
1015 /*
1016  * Is PV area contiguous to PV segment?
1017  */
1018 static int _is_contiguous(struct pv_match *pvmatch __attribute((unused)), struct pv_segment *pvseg, struct pv_area *pva)
1019 {
1020         if (pvseg->pv != pva->map->pv)
1021                 return 0;
1022
1023         if (pvseg->pe + pvseg->len != pva->start)
1024                 return 0;
1025
1026         return 1;
1027 }
1028
1029 static int _is_condition(struct cmd_context *cmd __attribute__((unused)),
1030                          struct pv_segment *pvseg, uint32_t s,
1031                          void *data)
1032 {
1033         struct pv_match *pvmatch = data;
1034
1035         if (!pvmatch->condition(pvmatch, pvseg, pvmatch->pva))
1036                 return 1;       /* Continue */
1037
1038         if (s >= pvmatch->areas_size)
1039                 return 1;
1040
1041         /*
1042          * Only used for cling and contiguous policies so it's safe to say all
1043          * the available space is used.
1044          */
1045         pvmatch->areas[s].pva = pvmatch->pva;
1046         pvmatch->areas[s].used = pvmatch->pva->count;
1047
1048         log_debug("Trying allocation area %" PRIu32 " on %s start PE %" PRIu32
1049                   " length %" PRIu32 ".",
1050                   s, dev_name(pvmatch->pva->map->pv->dev), pvmatch->pva->start, 
1051                   pvmatch->pva->count);
1052
1053         return 2;       /* Finished */
1054 }
1055
1056 /*
1057  * Is pva on same PV as any existing areas?
1058  */
1059 static int _check_cling(struct cmd_context *cmd,
1060                         const struct config_node *cling_tag_list_cn,
1061                         struct lv_segment *prev_lvseg, struct pv_area *pva,
1062                         struct pv_area_used *areas, uint32_t areas_size)
1063 {
1064         struct pv_match pvmatch;
1065         int r;
1066
1067         pvmatch.condition = cling_tag_list_cn ? _has_matching_pv_tag : _is_same_pv;
1068         pvmatch.areas = areas;
1069         pvmatch.areas_size = areas_size;
1070         pvmatch.pva = pva;
1071         pvmatch.cling_tag_list_cn = cling_tag_list_cn;
1072
1073         /* FIXME Cope with stacks by flattening */
1074         if (!(r = _for_each_pv(cmd, prev_lvseg->lv,
1075                                prev_lvseg->le + prev_lvseg->len - 1, 1, NULL, NULL,
1076                                0, 0, -1, 1,
1077                                _is_condition, &pvmatch)))
1078                 stack;
1079
1080         if (r != 2)
1081                 return 0;
1082
1083         return 1;
1084 }
1085
1086 /*
1087  * Is pva contiguous to any existing areas or on the same PV?
1088  */
1089 static int _check_contiguous(struct cmd_context *cmd,
1090                              struct lv_segment *prev_lvseg, struct pv_area *pva,
1091                              struct pv_area_used *areas, uint32_t areas_size)
1092 {
1093         struct pv_match pvmatch;
1094         int r;
1095
1096         pvmatch.condition = _is_contiguous;
1097         pvmatch.areas = areas;
1098         pvmatch.areas_size = areas_size;
1099         pvmatch.pva = pva;
1100         pvmatch.cling_tag_list_cn = NULL;
1101
1102         /* FIXME Cope with stacks by flattening */
1103         if (!(r = _for_each_pv(cmd, prev_lvseg->lv,
1104                                prev_lvseg->le + prev_lvseg->len - 1, 1, NULL, NULL,
1105                                0, 0, -1, 1,
1106                                _is_condition, &pvmatch)))
1107                 stack;
1108
1109         if (r != 2)
1110                 return 0;
1111
1112         return 1;
1113 }
1114
1115 /*
1116  * Choose sets of parallel areas to use, respecting any constraints.
1117  */
1118 static int _find_parallel_space(struct alloc_handle *ah, alloc_policy_t alloc,
1119                                 struct dm_list *pvms, struct pv_area_used **areas_ptr,
1120                                 uint32_t *areas_size_ptr, unsigned can_split,
1121                                 struct lv_segment *prev_lvseg,
1122                                 uint32_t *allocated, uint32_t *log_needs_allocating, uint32_t needed)
1123 {
1124         struct pv_map *pvm;
1125         struct pv_area *pva;
1126         struct pv_list *pvl;
1127         unsigned already_found_one = 0;
1128         unsigned contiguous = 0, cling = 0, use_cling_tags = 0, preferred_count = 0;
1129         unsigned ix, last_ix;
1130         unsigned ix_offset = 0; /* Offset for non-preferred allocations */
1131         unsigned ix_log_offset; /* Offset to start of areas to use for log */
1132         unsigned too_small_for_log_count; /* How many too small for log? */
1133         uint32_t max_parallel;  /* Maximum extents to allocate */
1134         uint32_t next_le;
1135         uint32_t required;      /* Extents we're trying to obtain from a given area */
1136         struct seg_pvs *spvs;
1137         struct dm_list *parallel_pvs;
1138         uint32_t free_pes;
1139         struct alloced_area *aa;
1140         uint32_t s;
1141         uint32_t total_extents_needed = (needed - *allocated) * ah->area_count / ah->area_multiple;
1142
1143         /* Is there enough total space? */
1144         free_pes = pv_maps_size(pvms);
1145         if (total_extents_needed > free_pes) {
1146                 log_error("Insufficient free space: %" PRIu32 " extents needed,"
1147                           " but only %" PRIu32 " available",
1148                           total_extents_needed, free_pes);
1149                 return 0;
1150         }
1151
1152         /* FIXME Select log PV appropriately if there isn't one yet */
1153
1154         /* Are there any preceding segments we must follow on from? */
1155         if (prev_lvseg) {
1156                 ix_offset = _stripes_per_mimage(prev_lvseg) * prev_lvseg->area_count;
1157                 if ((alloc == ALLOC_CONTIGUOUS))
1158                         contiguous = 1;
1159                 else if ((alloc == ALLOC_CLING))
1160                         cling = 1;
1161                 else if ((alloc == ALLOC_CLING_BY_TAGS)) {
1162                         cling = 1;
1163                         use_cling_tags = 1;
1164                 } else
1165                         ix_offset = 0;
1166         }
1167
1168         /* FIXME This algorithm needs a lot of cleaning up! */
1169         /* FIXME anywhere doesn't find all space yet */
1170         /* ix_offset holds the number of allocations that must be contiguous */
1171         /* ix holds the number of areas found on other PVs */
1172         do {
1173                 ix = 0;
1174                 preferred_count = 0;
1175
1176                 parallel_pvs = NULL;
1177                 max_parallel = needed;
1178
1179                 /*
1180                  * If there are existing parallel PVs, avoid them and reduce
1181                  * the maximum we can allocate in one go accordingly.
1182                  */
1183                 if (ah->parallel_areas) {
1184                         next_le = (prev_lvseg ? prev_lvseg->le + prev_lvseg->len : 0) + *allocated / ah->area_multiple;
1185                         dm_list_iterate_items(spvs, ah->parallel_areas) {
1186                                 if (next_le >= spvs->le + spvs->len)
1187                                         continue;
1188
1189                                 if (max_parallel > (spvs->le + spvs->len) * ah->area_multiple)
1190                                         max_parallel = (spvs->le + spvs->len) * ah->area_multiple;
1191                                 parallel_pvs = &spvs->pvs;
1192                                 break;
1193                         }
1194                 }
1195
1196                 do {
1197                         /*
1198                          * Provide for escape from the loop if no progress is made.
1199                          * This should not happen: ALLOC_ANYWHERE should be able to use
1200                          * all available space. (If there aren't enough extents, the code
1201                          * should not reach this point.)
1202                          */
1203                         last_ix = ix;
1204
1205                         /*
1206                          * Put the smallest area of each PV that is at least the
1207                          * size we need into areas array.  If there isn't one
1208                          * that fits completely and we're allowed more than one
1209                          * LV segment, then take the largest remaining instead.
1210                          */
1211                         dm_list_iterate_items(pvm, pvms) {
1212                                 if (dm_list_empty(&pvm->areas))
1213                                         continue;       /* Next PV */
1214
1215                                 if (alloc != ALLOC_ANYWHERE) {
1216                                         /* Don't allocate onto the log pv */
1217                                         if (ah->log_area_count)
1218                                                 dm_list_iterate_items(aa, &ah->alloced_areas[ah->area_count])
1219                                                         for (s = 0; s < ah->log_area_count; s++)
1220                                                                 if (!aa[s].pv)
1221                                                                         goto next_pv;
1222
1223                                         /* Avoid PVs used by existing parallel areas */
1224                                         if (parallel_pvs)
1225                                                 dm_list_iterate_items(pvl, parallel_pvs)
1226                                                         if (pvm->pv == pvl->pv)
1227                                                                 goto next_pv;
1228                                 }
1229
1230                                 already_found_one = 0;
1231                                 /* First area in each list is the largest */
1232                                 dm_list_iterate_items(pva, &pvm->areas) {
1233                                         /* Skip fully-reserved areas (which are not currently removed from the list). */
1234                                         if (!pva->unreserved)
1235                                                 continue;
1236                                         if (contiguous) {
1237                                                 if (prev_lvseg &&
1238                                                     _check_contiguous(ah->cmd,
1239                                                                       prev_lvseg,
1240                                                                       pva, *areas_ptr,
1241                                                                       *areas_size_ptr)) {
1242                                                         preferred_count++;
1243                                                         goto next_pv;
1244                                                 }
1245                                                 continue;
1246                                         }
1247
1248                                         if (cling) {
1249                                                 if (prev_lvseg &&
1250                                                     _check_cling(ah->cmd,
1251                                                                  use_cling_tags ? ah->cling_tag_list_cn : NULL,
1252                                                                  prev_lvseg,
1253                                                                  pva, *areas_ptr,
1254                                                                  *areas_size_ptr)) {
1255                                                         preferred_count++;
1256                                                 }
1257                                                 goto next_pv;
1258                                         }
1259
1260                                         /* Is it big enough on its own? */
1261                                         if (pva->unreserved * ah->area_multiple <
1262                                             max_parallel - *allocated &&
1263                                             ((!can_split && !ah->log_area_count) ||
1264                                              (already_found_one &&
1265                                               !(alloc == ALLOC_ANYWHERE))))
1266                                                 goto next_pv;
1267
1268                                         /*
1269                                          * Except with ALLOC_ANYWHERE, replace first area with this
1270                                          * one which is smaller but still big enough.
1271                                          */
1272                                         if (!already_found_one ||
1273                                             alloc == ALLOC_ANYWHERE) {
1274                                                 ix++;
1275                                                 already_found_one = 1;
1276                                         }
1277
1278                                         required = (max_parallel - *allocated) / ah->area_multiple;
1279
1280                                         if (alloc == ALLOC_ANYWHERE) {
1281                                                 /*
1282                                                  * Update amount unreserved - effectively splitting an area 
1283                                                  * into two or more parts.  If the whole stripe doesn't fit,
1284                                                  * reduce amount we're looking for.
1285                                                  */
1286                                                 if (ix + ix_offset - 1 >= ah->area_count)
1287                                                         required = ah->log_len;
1288                                                 if (required >= pva->unreserved) {
1289                                                         required = pva->unreserved;
1290                                                         pva->unreserved = 0;
1291                                                 } else {
1292                                                         pva->unreserved -= required;
1293                                                         reinsert_reduced_pv_area(pva);
1294                                                 }
1295                                         } else {
1296                                                 if (required < ah->log_len)
1297                                                         required = ah->log_len;
1298                                                 if (required > pva->count)
1299                                                         required = pva->count;
1300                                         }
1301
1302                                         /* Expand areas array if needed after an area was split. */
1303                                         if (ix + ix_offset > *areas_size_ptr) {
1304                                                 *areas_size_ptr *= 2;
1305                                                 if (!(*areas_ptr = dm_realloc(*areas_ptr,
1306                                                                              sizeof(**areas_ptr) *
1307                                                                              (*areas_size_ptr)))) {
1308                                                         log_error("Memory reallocation for parallel areas failed.");
1309                                                         return 0;
1310                                                 }
1311                                         }
1312                                         (*areas_ptr)[ix + ix_offset - 1].pva = pva;
1313                                                 (*areas_ptr)[ix + ix_offset - 1].used = required;
1314                                         log_debug("Trying allocation area %" PRIu32 " on %s start PE %" PRIu32
1315                                                   " length %" PRIu32 " leaving %" PRIu32 ".",
1316                                                   ix + ix_offset - 1, dev_name(pva->map->pv->dev), pva->start, required,
1317                                                   (alloc == ALLOC_ANYWHERE) ? pva->unreserved : pva->count - required);
1318                                 }
1319                         next_pv:
1320                                 /* With ALLOC_ANYWHERE we ignore further PVs once we have at least enough areas */
1321                                 /* With cling and contiguous we stop if we found a match for *all* the areas */
1322                                 /* FIXME Rename these variables! */
1323                                 if ((alloc == ALLOC_ANYWHERE &&
1324                                     ix + ix_offset >= ah->area_count + (*log_needs_allocating ? ah->log_area_count : 0)) ||
1325                                     (preferred_count == ix_offset &&
1326                                      (ix_offset == ah->area_count + (*log_needs_allocating ? ah->log_area_count : 0))))
1327                                         break;
1328                         }
1329                 } while (alloc == ALLOC_ANYWHERE && last_ix != ix && ix < ah->area_count + (*log_needs_allocating ? ah->log_area_count : 0));
1330
1331                 if (preferred_count < ix_offset)
1332                         break;
1333
1334                 if (ix + ix_offset < ah->area_count +
1335                    (*log_needs_allocating ? ah->log_area_count : 0))
1336                         break;
1337
1338                 /* Sort the areas so we allocate from the biggest */
1339                 if (ix > 1)
1340                         qsort((*areas_ptr) + ix_offset, ix, sizeof(**areas_ptr),
1341                               _comp_area);
1342
1343                 /*
1344                  * First time around, if there's a log, allocate it on the
1345                  * smallest device that has space for it.
1346                  */
1347                 too_small_for_log_count = 0;
1348                 ix_log_offset = 0;
1349
1350                 /* FIXME This logic is due to its heritage and can be simplified! */
1351                 if (*log_needs_allocating) {
1352                         /* How many areas are too small for the log? */
1353                         while (too_small_for_log_count < ix_offset + ix &&
1354                                (*((*areas_ptr) + ix_offset + ix - 1 -
1355                                   too_small_for_log_count)).used < ah->log_len)
1356                                 too_small_for_log_count++;
1357                         ix_log_offset = ix_offset + ix - too_small_for_log_count - ah->log_area_count;
1358                 }
1359
1360                 if (ix + ix_offset < ah->area_count +
1361                     (*log_needs_allocating ? ah->log_area_count +
1362                                             too_small_for_log_count : 0))
1363                         break;
1364
1365                 if (!_alloc_parallel_area(ah, max_parallel, *areas_ptr, allocated,
1366                                           *log_needs_allocating, ix_log_offset))
1367                         return_0;
1368
1369                 *log_needs_allocating = 0;
1370
1371         } while ((alloc != ALLOC_CONTIGUOUS) && *allocated != needed && can_split);
1372
1373         return 1;
1374 }
1375
1376 /*
1377  * Allocate several segments, each the same size, in parallel.
1378  * If mirrored_pv and mirrored_pe are supplied, it is used as
1379  * the first area, and additional areas are allocated parallel to it.
1380  */
1381 static int _allocate(struct alloc_handle *ah,
1382                      struct volume_group *vg,
1383                      struct logical_volume *lv,
1384                      unsigned can_split,
1385                      struct dm_list *allocatable_pvs)
1386 {
1387         struct pv_area_used *areas;
1388         uint32_t allocated = lv ? lv->le_count : 0;
1389         uint32_t old_allocated;
1390         struct lv_segment *prev_lvseg = NULL;
1391         int r = 0;
1392         struct dm_list *pvms;
1393         uint32_t areas_size;
1394         alloc_policy_t alloc;
1395         unsigned log_needs_allocating = 0;
1396
1397         if (allocated >= ah->new_extents && !ah->log_area_count) {
1398                 log_error("_allocate called with no work to do!");
1399                 return 1;
1400         }
1401
1402         if (ah->log_area_count)
1403                 log_needs_allocating = 1;
1404
1405         if (ah->alloc == ALLOC_CONTIGUOUS)
1406                 can_split = 0;
1407
1408         if (lv && !dm_list_empty(&lv->segments))
1409                 prev_lvseg = dm_list_item(dm_list_last(&lv->segments),
1410                                        struct lv_segment);
1411         /*
1412          * Build the sets of available areas on the pv's.
1413          */
1414         if (!(pvms = create_pv_maps(ah->mem, vg, allocatable_pvs)))
1415                 return_0;
1416
1417         if (!_log_parallel_areas(ah->mem, ah->parallel_areas))
1418                 stack;
1419
1420         areas_size = dm_list_size(pvms);
1421         if (areas_size && areas_size < (ah->area_count + ah->log_area_count)) {
1422                 if (ah->alloc != ALLOC_ANYWHERE) {
1423                         log_error("Not enough PVs with free space available "
1424                                   "for parallel allocation.");
1425                         log_error("Consider --alloc anywhere if desperate.");
1426                         return 0;
1427                 }
1428                 areas_size = ah->area_count + ah->log_area_count;
1429         }
1430
1431         /* Upper bound if none of the PVs in prev_lvseg is in pvms */
1432         /* FIXME Work size out properly */
1433         if (prev_lvseg)
1434                 areas_size += _stripes_per_mimage(prev_lvseg) * prev_lvseg->area_count;
1435
1436         /* Allocate an array of pv_areas to hold the largest space on each PV */
1437         if (!(areas = dm_malloc(sizeof(*areas) * areas_size))) {
1438                 log_error("Couldn't allocate areas array.");
1439                 return 0;
1440         }
1441
1442         /*
1443          * cling includes implicit cling_by_tags
1444          * but it does nothing unless the lvm.conf setting is present.
1445          */
1446         if (ah->alloc == ALLOC_CLING)
1447                 ah->alloc = ALLOC_CLING_BY_TAGS;
1448
1449         /* Attempt each defined allocation policy in turn */
1450         for (alloc = ALLOC_CONTIGUOUS; alloc < ALLOC_INHERIT; alloc++) {
1451                 /* Skip cling_by_tags if no list defined */
1452                 if (alloc == ALLOC_CLING_BY_TAGS && !ah->cling_tag_list_cn)
1453                         continue;
1454                 old_allocated = allocated;
1455                 log_debug("Trying allocation using %s policy.  "
1456                           "Need %" PRIu32 " extents for %" PRIu32 " parallel areas and %" PRIu32 " log areas of %" PRIu32 " extents. "
1457                           "(Total %" PRIu32 " extents.)",
1458                           get_alloc_string(alloc),
1459                           (ah->new_extents - allocated) / ah->area_multiple,
1460                           ah->area_count, log_needs_allocating ? ah->log_area_count : 0,
1461                           log_needs_allocating ? ah->log_len : 0,
1462                           (ah->new_extents - allocated) * ah->area_count / ah->area_multiple +
1463                                 (log_needs_allocating ? ah->log_area_count * ah->log_len : 0));
1464                 if (!_find_parallel_space(ah, alloc, pvms, &areas,
1465                                           &areas_size, can_split,
1466                                           prev_lvseg, &allocated, &log_needs_allocating, ah->new_extents))
1467                         goto_out;
1468                 if ((allocated == ah->new_extents && !log_needs_allocating) || (ah->alloc == alloc) ||
1469                     (!can_split && (allocated != old_allocated)))
1470                         break;
1471         }
1472
1473         if (allocated != ah->new_extents) {
1474                 log_error("Insufficient suitable %sallocatable extents "
1475                           "for logical volume %s: %u more required",
1476                           can_split ? "" : "contiguous ",
1477                           lv ? lv->name : "",
1478                           (ah->new_extents - allocated) * ah->area_count
1479                           / ah->area_multiple);
1480                 goto out;
1481         }
1482
1483         if (log_needs_allocating) {
1484                 log_error("Insufficient extents for log allocation "
1485                           "for logical volume %s.",
1486                           lv ? lv->name : "");
1487                 goto out;
1488         }
1489
1490         r = 1;
1491
1492       out:
1493         dm_free(areas);
1494         return r;
1495 }
1496
1497 int lv_add_virtual_segment(struct logical_volume *lv, uint64_t status,
1498                            uint32_t extents, const struct segment_type *segtype)
1499 {
1500         struct lv_segment *seg;
1501
1502         if (!(seg = alloc_lv_segment(lv->vg->cmd->mem, segtype, lv,
1503                                      lv->le_count, extents, status, 0,
1504                                      NULL, 0, extents, 0, 0, 0, NULL))) {
1505                 log_error("Couldn't allocate new zero segment.");
1506                 return 0;
1507         }
1508
1509         dm_list_add(&lv->segments, &seg->list);
1510
1511         lv->le_count += extents;
1512         lv->size += (uint64_t) extents *lv->vg->extent_size;
1513
1514         lv->status |= VIRTUAL;
1515
1516         return 1;
1517 }
1518
1519 /*
1520  * Entry point for all extent allocations.
1521  */
1522 struct alloc_handle *allocate_extents(struct volume_group *vg,
1523                                       struct logical_volume *lv,
1524                                       const struct segment_type *segtype,
1525                                       uint32_t stripes,
1526                                       uint32_t mirrors, uint32_t log_count,
1527                                       uint32_t region_size, uint32_t extents,
1528                                       struct dm_list *allocatable_pvs,
1529                                       alloc_policy_t alloc,
1530                                       struct dm_list *parallel_areas)
1531 {
1532         struct alloc_handle *ah;
1533         uint32_t new_extents;
1534
1535         if (segtype_is_virtual(segtype)) {
1536                 log_error("allocate_extents does not handle virtual segments");
1537                 return NULL;
1538         }
1539
1540         if (vg->fid->fmt->ops->segtype_supported &&
1541             !vg->fid->fmt->ops->segtype_supported(vg->fid, segtype)) {
1542                 log_error("Metadata format (%s) does not support required "
1543                           "LV segment type (%s).", vg->fid->fmt->name,
1544                           segtype->name);
1545                 log_error("Consider changing the metadata format by running "
1546                           "vgconvert.");
1547                 return NULL;
1548         }
1549
1550         if (alloc == ALLOC_INHERIT)
1551                 alloc = vg->alloc;
1552
1553         new_extents = (lv ? lv->le_count : 0) + extents;
1554         if (!(ah = _alloc_init(vg->cmd, vg->cmd->mem, segtype, alloc,
1555                                new_extents, mirrors, stripes, log_count,
1556                                vg->extent_size, region_size,
1557                                parallel_areas)))
1558                 return_NULL;
1559
1560         if (!segtype_is_virtual(segtype) &&
1561             !_allocate(ah, vg, lv, 1, allocatable_pvs)) {
1562                 alloc_destroy(ah);
1563                 return_NULL;
1564         }
1565
1566         return ah;
1567 }
1568
1569 /*
1570  * Add new segments to an LV from supplied list of areas.
1571  */
1572 int lv_add_segment(struct alloc_handle *ah,
1573                    uint32_t first_area, uint32_t num_areas,
1574                    struct logical_volume *lv,
1575                    const struct segment_type *segtype,
1576                    uint32_t stripe_size,
1577                    uint64_t status,
1578                    uint32_t region_size)
1579 {
1580         if (!segtype) {
1581                 log_error("Missing segtype in lv_add_segment().");
1582                 return 0;
1583         }
1584
1585         if (segtype_is_virtual(segtype)) {
1586                 log_error("lv_add_segment cannot handle virtual segments");
1587                 return 0;
1588         }
1589
1590         if ((status & MIRROR_LOG) && dm_list_size(&lv->segments)) {
1591                 log_error("Log segments can only be added to an empty LV");
1592                 return 0;
1593         }
1594
1595         if (!_setup_alloced_segments(lv, &ah->alloced_areas[first_area],
1596                                      num_areas, status,
1597                                      stripe_size, segtype,
1598                                      region_size))
1599                 return_0;
1600
1601         if ((segtype->flags & SEG_CAN_SPLIT) && !lv_merge_segments(lv)) {
1602                 log_error("Couldn't merge segments after extending "
1603                           "logical volume.");
1604                 return 0;
1605         }
1606
1607         if (lv->vg->fid->fmt->ops->lv_setup &&
1608             !lv->vg->fid->fmt->ops->lv_setup(lv->vg->fid, lv))
1609                 return_0;
1610
1611         return 1;
1612 }
1613
1614 /*
1615  * "mirror" segment type doesn't support split.
1616  * So, when adding mirrors to linear LV segment, first split it,
1617  * then convert it to "mirror" and add areas.
1618  */
1619 static struct lv_segment *_convert_seg_to_mirror(struct lv_segment *seg,
1620                                                  uint32_t region_size,
1621                                                  struct logical_volume *log_lv)
1622 {
1623         struct lv_segment *newseg;
1624         uint32_t s;
1625
1626         if (!seg_is_striped(seg)) {
1627                 log_error("Can't convert non-striped segment to mirrored.");
1628                 return NULL;
1629         }
1630
1631         if (seg->area_count > 1) {
1632                 log_error("Can't convert striped segment with multiple areas "
1633                           "to mirrored.");
1634                 return NULL;
1635         }
1636
1637         if (!(newseg = alloc_lv_segment(seg->lv->vg->cmd->mem,
1638                                         get_segtype_from_string(seg->lv->vg->cmd, "mirror"),
1639                                         seg->lv, seg->le, seg->len,
1640                                         seg->status, seg->stripe_size,
1641                                         log_lv,
1642                                         seg->area_count, seg->area_len,
1643                                         seg->chunk_size, region_size,
1644                                         seg->extents_copied, NULL))) {
1645                 log_error("Couldn't allocate converted LV segment");
1646                 return NULL;
1647         }
1648
1649         for (s = 0; s < seg->area_count; s++)
1650                 if (!move_lv_segment_area(newseg, s, seg, s))
1651                         return_NULL;
1652
1653         seg->pvmove_source_seg = NULL; /* Not maintained after allocation */
1654
1655         dm_list_add(&seg->list, &newseg->list);
1656         dm_list_del(&seg->list);
1657
1658         return newseg;
1659 }
1660
1661 /*
1662  * Add new areas to mirrored segments
1663  */
1664 int lv_add_mirror_areas(struct alloc_handle *ah,
1665                         struct logical_volume *lv, uint32_t le,
1666                         uint32_t region_size)
1667 {
1668         struct alloced_area *aa;
1669         struct lv_segment *seg;
1670         uint32_t current_le = le;
1671         uint32_t s, old_area_count, new_area_count;
1672
1673         dm_list_iterate_items(aa, &ah->alloced_areas[0]) {
1674                 if (!(seg = find_seg_by_le(lv, current_le))) {
1675                         log_error("Failed to find segment for %s extent %"
1676                                   PRIu32, lv->name, current_le);
1677                         return 0;
1678                 }
1679
1680                 /* Allocator assures aa[0].len <= seg->area_len */
1681                 if (aa[0].len < seg->area_len) {
1682                         if (!lv_split_segment(lv, seg->le + aa[0].len)) {
1683                                 log_error("Failed to split segment at %s "
1684                                           "extent %" PRIu32, lv->name, le);
1685                                 return 0;
1686                         }
1687                 }
1688
1689                 if (!seg_is_mirrored(seg) &&
1690                     (!(seg = _convert_seg_to_mirror(seg, region_size, NULL))))
1691                         return_0;
1692
1693                 old_area_count = seg->area_count;
1694                 new_area_count = old_area_count + ah->area_count;
1695
1696                 if (!_lv_segment_add_areas(lv, seg, new_area_count))
1697                         return_0;
1698
1699                 for (s = 0; s < ah->area_count; s++) {
1700                         if (!set_lv_segment_area_pv(seg, s + old_area_count,
1701                                                     aa[s].pv, aa[s].pe))
1702                                 return_0;
1703                 }
1704
1705                 current_le += seg->area_len;
1706         }
1707
1708         lv->status |= MIRRORED;
1709
1710         if (lv->vg->fid->fmt->ops->lv_setup &&
1711             !lv->vg->fid->fmt->ops->lv_setup(lv->vg->fid, lv))
1712                 return_0;
1713
1714         return 1;
1715 }
1716
1717 /*
1718  * Add mirror image LVs to mirrored segments
1719  */
1720 int lv_add_mirror_lvs(struct logical_volume *lv,
1721                       struct logical_volume **sub_lvs,
1722                       uint32_t num_extra_areas,
1723                       uint64_t status, uint32_t region_size)
1724 {
1725         struct lv_segment *seg;
1726         uint32_t old_area_count, new_area_count;
1727         uint32_t m;
1728         struct segment_type *mirror_segtype;
1729
1730         seg = first_seg(lv);
1731
1732         if (dm_list_size(&lv->segments) != 1 || seg_type(seg, 0) != AREA_LV) {
1733                 log_error("Mirror layer must be inserted before adding mirrors");
1734                 return_0;
1735         }
1736
1737         mirror_segtype = get_segtype_from_string(lv->vg->cmd, "mirror");
1738         if (seg->segtype != mirror_segtype)
1739                 if (!(seg = _convert_seg_to_mirror(seg, region_size, NULL)))
1740                         return_0;
1741
1742         if (region_size && region_size != seg->region_size) {
1743                 log_error("Conflicting region_size");
1744                 return 0;
1745         }
1746
1747         old_area_count = seg->area_count;
1748         new_area_count = old_area_count + num_extra_areas;
1749
1750         if (!_lv_segment_add_areas(lv, seg, new_area_count)) {
1751                 log_error("Failed to allocate widened LV segment for %s.",
1752                           lv->name);
1753                 return 0;
1754         }
1755
1756         for (m = 0; m < old_area_count; m++)
1757                 seg_lv(seg, m)->status |= status;
1758
1759         for (m = old_area_count; m < new_area_count; m++) {
1760                 if (!set_lv_segment_area_lv(seg, m, sub_lvs[m - old_area_count],
1761                                             0, status))
1762                         return_0;
1763                 lv_set_hidden(sub_lvs[m - old_area_count]);
1764         }
1765
1766         lv->status |= MIRRORED;
1767
1768         return 1;
1769 }
1770
1771 /*
1772  * Turn an empty LV into a mirror log.
1773  *
1774  * FIXME: Mirrored logs are built inefficiently.
1775  * A mirrored log currently uses the same layout that a mirror
1776  * LV uses.  The mirror layer sits on top of AREA_LVs which form the
1777  * legs, rather on AREA_PVs.  This is done to allow re-use of the
1778  * various mirror functions to also handle the mirrored LV that makes
1779  * up the log.
1780  *
1781  * If we used AREA_PVs under the mirror layer of a log, we could
1782  * assemble it all at once by calling 'lv_add_segment' with the
1783  * appropriate segtype (mirror/stripe), like this:
1784  *      lv_add_segment(ah, ah->area_count, ah->log_area_count,
1785  *                     log_lv, segtype, 0, MIRROR_LOG, 0);
1786  *
1787  * For now, we use the same mechanism to build a mirrored log as we
1788  * do for building a mirrored LV: 1) create initial LV, 2) add a
1789  * mirror layer, and 3) add the remaining copy LVs
1790  */
1791 int lv_add_log_segment(struct alloc_handle *ah, uint32_t first_area,
1792                        struct logical_volume *log_lv, uint64_t status)
1793 {
1794
1795         return lv_add_segment(ah, ah->area_count + first_area, 1, log_lv,
1796                               get_segtype_from_string(log_lv->vg->cmd,
1797                                                       "striped"),
1798                               0, status, 0);
1799 }
1800
1801 static int _lv_extend_mirror(struct alloc_handle *ah,
1802                              struct logical_volume *lv,
1803                              uint32_t extents, uint32_t first_area,
1804                              uint32_t stripes, uint32_t stripe_size)
1805 {
1806         struct lv_segment *seg;
1807         uint32_t m, s;
1808
1809         seg = first_seg(lv);
1810         for (m = first_area, s = 0; s < seg->area_count; s++) {
1811                 if (is_temporary_mirror_layer(seg_lv(seg, s))) {
1812                         if (!_lv_extend_mirror(ah, seg_lv(seg, s), extents, m, stripes, stripe_size))
1813                                 return_0;
1814                         m += lv_mirror_count(seg_lv(seg, s));
1815                         continue;
1816                 }
1817
1818                 if (!lv_add_segment(ah, m, stripes, seg_lv(seg, s),
1819                                     get_segtype_from_string(lv->vg->cmd,
1820                                                             "striped"),
1821                                     stripe_size, 0, 0)) {
1822                         log_error("Aborting. Failed to extend %s.",
1823                                   seg_lv(seg, s)->name);
1824                         return 0;
1825                 }
1826                 m += stripes;
1827         }
1828         seg->area_len += extents;
1829         seg->len += extents;
1830         lv->le_count += extents;
1831         lv->size += (uint64_t) extents *lv->vg->extent_size;
1832
1833         return 1;
1834 }
1835
1836 /*
1837  * Entry point for single-step LV allocation + extension.
1838  */
1839 int lv_extend(struct logical_volume *lv,
1840               const struct segment_type *segtype,
1841               uint32_t stripes, uint32_t stripe_size,
1842               uint32_t mirrors, uint32_t extents,
1843               struct physical_volume *mirrored_pv __attribute__((unused)),
1844               uint32_t mirrored_pe __attribute__((unused)),
1845               uint64_t status, struct dm_list *allocatable_pvs,
1846               alloc_policy_t alloc)
1847 {
1848         int r = 1;
1849         struct alloc_handle *ah;
1850
1851         if (segtype_is_virtual(segtype))
1852                 return lv_add_virtual_segment(lv, status, extents, segtype);
1853
1854         if (!(ah = allocate_extents(lv->vg, lv, segtype, stripes, mirrors, 0, 0,
1855                                     extents, allocatable_pvs, alloc, NULL)))
1856                 return_0;
1857
1858         if (mirrors < 2)
1859                 r = lv_add_segment(ah, 0, ah->area_count, lv, segtype,
1860                                    stripe_size, status, 0);
1861         else
1862                 r = _lv_extend_mirror(ah, lv, extents, 0, stripes, stripe_size);
1863
1864         alloc_destroy(ah);
1865         return r;
1866 }
1867
1868 /*
1869  * Minimal LV renaming function.
1870  * Metadata transaction should be made by caller.
1871  * Assumes new_name is allocated from cmd->mem pool.
1872  */
1873 static int _rename_single_lv(struct logical_volume *lv, char *new_name)
1874 {
1875         struct volume_group *vg = lv->vg;
1876
1877         if (find_lv_in_vg(vg, new_name)) {
1878                 log_error("Logical volume \"%s\" already exists in "
1879                           "volume group \"%s\"", new_name, vg->name);
1880                 return 0;
1881         }
1882
1883         if (lv->status & LOCKED) {
1884                 log_error("Cannot rename locked LV %s", lv->name);
1885                 return 0;
1886         }
1887
1888         lv->name = new_name;
1889
1890         return 1;
1891 }
1892
1893 /*
1894  * Rename sub LV.
1895  * 'lv_name_old' and 'lv_name_new' are old and new names of the main LV.
1896  */
1897 static int _rename_sub_lv(struct cmd_context *cmd,
1898                           struct logical_volume *lv,
1899                           const char *lv_name_old, const char *lv_name_new)
1900 {
1901         char *suffix, *new_name;
1902         size_t len;
1903
1904         /*
1905          * A sub LV name starts with lv_name_old + '_'.
1906          * The suffix follows lv_name_old and includes '_'.
1907          */
1908         len = strlen(lv_name_old);
1909         if (strncmp(lv->name, lv_name_old, len) || lv->name[len] != '_') {
1910                 log_error("Cannot rename \"%s\": name format not recognized "
1911                           "for internal LV \"%s\"",
1912                           lv_name_old, lv->name);
1913                 return 0;
1914         }
1915         suffix = lv->name + len;
1916
1917         /*
1918          * Compose a new name for sub lv:
1919          *   e.g. new name is "lvol1_mlog"
1920          *      if the sub LV is "lvol0_mlog" and
1921          *      a new name for main LV is "lvol1"
1922          */
1923         len = strlen(lv_name_new) + strlen(suffix) + 1;
1924         new_name = dm_pool_alloc(cmd->mem, len);
1925         if (!new_name) {
1926                 log_error("Failed to allocate space for new name");
1927                 return 0;
1928         }
1929         if (!dm_snprintf(new_name, len, "%s%s", lv_name_new, suffix)) {
1930                 log_error("Failed to create new name");
1931                 return 0;
1932         }
1933
1934         /* Rename it */
1935         return _rename_single_lv(lv, new_name);
1936 }
1937
1938 /* Callback for _for_each_sub_lv */
1939 static int _rename_cb(struct cmd_context *cmd, struct logical_volume *lv,
1940                       void *data)
1941 {
1942         struct lv_names *lv_names = (struct lv_names *) data;
1943
1944         return _rename_sub_lv(cmd, lv, lv_names->old, lv_names->new);
1945 }
1946
1947 /*
1948  * Loop down sub LVs and call "func" for each.
1949  * "func" is responsible to log necessary information on failure.
1950  */
1951 static int _for_each_sub_lv(struct cmd_context *cmd, struct logical_volume *lv,
1952                             int (*func)(struct cmd_context *cmd,
1953                                         struct logical_volume *lv,
1954                                         void *data),
1955                             void *data)
1956 {
1957         struct logical_volume *org;
1958         struct lv_segment *seg;
1959         uint32_t s;
1960
1961         if (lv_is_cow(lv) && lv_is_virtual_origin(org = origin_from_cow(lv)))
1962                 if (!func(cmd, org, data))
1963                         return_0;
1964
1965         dm_list_iterate_items(seg, &lv->segments) {
1966                 if (seg->log_lv && !func(cmd, seg->log_lv, data))
1967                         return_0;
1968                 for (s = 0; s < seg->area_count; s++) {
1969                         if (seg_type(seg, s) != AREA_LV)
1970                                 continue;
1971                         if (!func(cmd, seg_lv(seg, s), data))
1972                                 return_0;
1973                         if (!_for_each_sub_lv(cmd, seg_lv(seg, s), func, data))
1974                                 return_0;
1975                 }
1976         }
1977
1978         return 1;
1979 }
1980
1981
1982 /*
1983  * Core of LV renaming routine.
1984  * VG must be locked by caller.
1985  */
1986 int lv_rename(struct cmd_context *cmd, struct logical_volume *lv,
1987               const char *new_name)
1988 {
1989         struct volume_group *vg = lv->vg;
1990         struct lv_names lv_names;
1991         DM_LIST_INIT(lvs_changed);
1992         struct lv_list lvl, lvl2, *lvlp;
1993         int r = 0;
1994
1995         /* rename is not allowed on sub LVs */
1996         if (!lv_is_visible(lv)) {
1997                 log_error("Cannot rename internal LV \"%s\".", lv->name);
1998                 return 0;
1999         }
2000
2001         if (find_lv_in_vg(vg, new_name)) {
2002                 log_error("Logical volume \"%s\" already exists in "
2003                           "volume group \"%s\"", new_name, vg->name);
2004                 return 0;
2005         }
2006
2007         if (lv->status & LOCKED) {
2008                 log_error("Cannot rename locked LV %s", lv->name);
2009                 return 0;
2010         }
2011
2012         if (!archive(vg))
2013                 return 0;
2014
2015         /* rename sub LVs */
2016         lv_names.old = lv->name;
2017         lv_names.new = new_name;
2018         if (!_for_each_sub_lv(cmd, lv, _rename_cb, (void *) &lv_names))
2019                 return 0;
2020
2021         /* rename main LV */
2022         if (!(lv->name = dm_pool_strdup(cmd->mem, new_name))) {
2023                 log_error("Failed to allocate space for new name");
2024                 return 0;
2025         }
2026
2027         lvl.lv = lv;
2028         dm_list_add(&lvs_changed, &lvl.list);
2029
2030         /* rename active virtual origin too */
2031         if (lv_is_cow(lv) && lv_is_virtual_origin(lvl2.lv = origin_from_cow(lv)))
2032                 dm_list_add_h(&lvs_changed, &lvl2.list);
2033
2034         log_verbose("Writing out updated volume group");
2035         if (!vg_write(vg))
2036                 return 0;
2037
2038
2039         if (!suspend_lvs(cmd, &lvs_changed)) {
2040                 vg_revert(vg);
2041                 goto_out;
2042         }
2043
2044         if (!(r = vg_commit(vg)))
2045                 stack;
2046
2047         /*
2048          * FIXME: resume LVs in reverse order to prevent memory
2049          * lock imbalance when resuming virtual snapshot origin
2050          * (resume of snapshot resumes origin too)
2051          */
2052         dm_list_iterate_back_items(lvlp, &lvs_changed)
2053                 if (!resume_lv(cmd, lvlp->lv))
2054                         stack;
2055 out:
2056         backup(vg);
2057         return r;
2058 }
2059
2060 char *generate_lv_name(struct volume_group *vg, const char *format,
2061                        char *buffer, size_t len)
2062 {
2063         struct lv_list *lvl;
2064         int high = -1, i;
2065
2066         dm_list_iterate_items(lvl, &vg->lvs) {
2067                 if (sscanf(lvl->lv->name, format, &i) != 1)
2068                         continue;
2069
2070                 if (i > high)
2071                         high = i;
2072         }
2073
2074         if (dm_snprintf(buffer, len, format, high + 1) < 0)
2075                 return NULL;
2076
2077         return buffer;
2078 }
2079
2080 int vg_max_lv_reached(struct volume_group *vg)
2081 {
2082         if (!vg->max_lv)
2083                 return 0;
2084
2085         if (vg->max_lv > vg_visible_lvs(vg))
2086                 return 0;
2087
2088         log_verbose("Maximum number of logical volumes (%u) reached "
2089                     "in volume group %s", vg->max_lv, vg->name);
2090
2091         return 1;
2092 }
2093
2094 struct logical_volume *alloc_lv(struct dm_pool *mem)
2095 {
2096         struct logical_volume *lv;
2097
2098         if (!(lv = dm_pool_zalloc(mem, sizeof(*lv)))) {
2099                 log_error("Unable to allocate logical volume structure");
2100                 return NULL;
2101         }
2102
2103         lv->snapshot = NULL;
2104         dm_list_init(&lv->snapshot_segs);
2105         dm_list_init(&lv->segments);
2106         dm_list_init(&lv->tags);
2107         dm_list_init(&lv->segs_using_this_lv);
2108         dm_list_init(&lv->rsites);
2109
2110         return lv;
2111 }
2112
2113 /*
2114  * Create a new empty LV.
2115  */
2116 struct logical_volume *lv_create_empty(const char *name,
2117                                        union lvid *lvid,
2118                                        uint64_t status,
2119                                        alloc_policy_t alloc,
2120                                        struct volume_group *vg)
2121 {
2122         struct format_instance *fi = vg->fid;
2123         struct logical_volume *lv;
2124         char dname[NAME_LEN];
2125
2126         if (vg_max_lv_reached(vg))
2127                 stack;
2128
2129         if (strstr(name, "%d") &&
2130             !(name = generate_lv_name(vg, name, dname, sizeof(dname)))) {
2131                 log_error("Failed to generate unique name for the new "
2132                           "logical volume");
2133                 return NULL;
2134         } else if (find_lv_in_vg(vg, name)) {
2135                 log_error("Unable to create LV %s in Volume Group %s: "
2136                           "name already in use.", name, vg->name);
2137                 return NULL;
2138         }
2139
2140         log_verbose("Creating logical volume %s", name);
2141
2142         if (!(lv = alloc_lv(vg->vgmem)))
2143                 return_NULL;
2144
2145         if (!(lv->name = dm_pool_strdup(vg->vgmem, name)))
2146                 goto_bad;
2147
2148         lv->status = status;
2149         lv->alloc = alloc;
2150         lv->read_ahead = vg->cmd->default_settings.read_ahead;
2151         lv->major = -1;
2152         lv->minor = -1;
2153         lv->size = UINT64_C(0);
2154         lv->le_count = 0;
2155
2156         if (lvid)
2157                 lv->lvid = *lvid;
2158
2159         if (!link_lv_to_vg(vg, lv))
2160                 goto_bad;
2161  
2162         if (fi->fmt->ops->lv_setup && !fi->fmt->ops->lv_setup(fi, lv))
2163                 goto_bad;
2164  
2165         return lv;
2166 bad:
2167         dm_pool_free(vg->vgmem, lv);
2168         return NULL;
2169 }
2170
2171 static int _add_pvs(struct cmd_context *cmd, struct pv_segment *peg,
2172                     uint32_t s __attribute__((unused)), void *data)
2173 {
2174         struct seg_pvs *spvs = (struct seg_pvs *) data;
2175         struct pv_list *pvl;
2176
2177         /* Don't add again if it's already on list. */
2178         if (find_pv_in_pv_list(&spvs->pvs, peg->pv))
2179                         return 1;
2180
2181         if (!(pvl = dm_pool_alloc(cmd->mem, sizeof(*pvl)))) {
2182                 log_error("pv_list allocation failed");
2183                 return 0;
2184         }
2185
2186         pvl->pv = peg->pv;
2187
2188         dm_list_add(&spvs->pvs, &pvl->list);
2189
2190         return 1;
2191 }
2192
2193 /*
2194  * Construct dm_list of segments of LVs showing which PVs they use.
2195  * For pvmove we use the *parent* LV so we can pick up stripes & existing mirrors etc.
2196  */
2197 struct dm_list *build_parallel_areas_from_lv(struct cmd_context *cmd,
2198                                              struct logical_volume *lv,
2199                                              unsigned use_pvmove_parent_lv)
2200 {
2201         struct dm_list *parallel_areas;
2202         struct seg_pvs *spvs;
2203         uint32_t current_le = 0;
2204         struct lv_segment * uninitialized_var(seg);
2205
2206         if (!(parallel_areas = dm_pool_alloc(cmd->mem, sizeof(*parallel_areas)))) {
2207                 log_error("parallel_areas allocation failed");
2208                 return NULL;
2209         }
2210
2211         dm_list_init(parallel_areas);
2212
2213         do {
2214                 if (!(spvs = dm_pool_zalloc(cmd->mem, sizeof(*spvs)))) {
2215                         log_error("allocation failed");
2216                         return NULL;
2217                 }
2218
2219                 dm_list_init(&spvs->pvs);
2220
2221                 spvs->le = current_le;
2222                 spvs->len = lv->le_count - current_le;
2223
2224                 dm_list_add(parallel_areas, &spvs->list);
2225
2226                 if (use_pvmove_parent_lv && !(seg = find_seg_by_le(lv, current_le))) {
2227                         log_error("Failed to find segment for %s extent %" PRIu32,
2228                                   lv->name, current_le);
2229                         return 0;
2230                 }
2231
2232                 /* Find next segment end */
2233                 /* FIXME Unnecessary nesting! */
2234                 if (!_for_each_pv(cmd, use_pvmove_parent_lv ? seg->pvmove_source_seg->lv : lv,
2235                                   use_pvmove_parent_lv ? seg->pvmove_source_seg->le : current_le,
2236                                   use_pvmove_parent_lv ? spvs->len * _calc_area_multiple(seg->pvmove_source_seg->segtype, seg->pvmove_source_seg->area_count, 0) : spvs->len,
2237                                   use_pvmove_parent_lv ? seg->pvmove_source_seg : NULL,
2238                                   &spvs->len,
2239                                   0, 0, -1, 0, _add_pvs, (void *) spvs))
2240                         return_NULL;
2241
2242                 current_le = spvs->le + spvs->len;
2243         } while (current_le < lv->le_count);
2244
2245         /* FIXME Merge adjacent segments with identical PV lists (avoids need for contiguous allocation attempts between successful allocations) */
2246
2247         return parallel_areas;
2248 }
2249
2250 int link_lv_to_vg(struct volume_group *vg, struct logical_volume *lv)
2251 {
2252         struct lv_list *lvl;
2253
2254         if (vg_max_lv_reached(vg))
2255                 stack;
2256
2257         if (!(lvl = dm_pool_zalloc(vg->vgmem, sizeof(*lvl))))
2258                 return_0;
2259
2260         lvl->lv = lv;
2261         lv->vg = vg;
2262         dm_list_add(&vg->lvs, &lvl->list);
2263
2264         return 1;
2265 }
2266
2267 int unlink_lv_from_vg(struct logical_volume *lv)
2268 {
2269         struct lv_list *lvl;
2270
2271         if (!(lvl = find_lv_in_vg(lv->vg, lv->name)))
2272                 return_0;
2273
2274         dm_list_del(&lvl->list);
2275
2276         return 1;
2277 }
2278
2279 void lv_set_visible(struct logical_volume *lv)
2280 {
2281         if (lv_is_visible(lv))
2282                 return;
2283
2284         lv->status |= VISIBLE_LV;
2285
2286         log_debug("LV %s in VG %s is now visible.",  lv->name, lv->vg->name);
2287 }
2288
2289 void lv_set_hidden(struct logical_volume *lv)
2290 {
2291         if (!lv_is_visible(lv))
2292                 return;
2293
2294         lv->status &= ~VISIBLE_LV;
2295
2296         log_debug("LV %s in VG %s is now hidden.",  lv->name, lv->vg->name);
2297 }
2298
2299 int lv_remove_single(struct cmd_context *cmd, struct logical_volume *lv,
2300                      const force_t force)
2301 {
2302         struct volume_group *vg;
2303         struct lvinfo info;
2304         struct logical_volume *origin = NULL;
2305         int was_merging = 0;
2306
2307         vg = lv->vg;
2308
2309         if (!vg_check_status(vg, LVM_WRITE))
2310                 return 0;
2311
2312         if (lv_is_origin(lv)) {
2313                 log_error("Can't remove logical volume \"%s\" under snapshot",
2314                           lv->name);
2315                 return 0;
2316         }
2317
2318         if (lv->status & MIRROR_IMAGE) {
2319                 log_error("Can't remove logical volume %s used by a mirror",
2320                           lv->name);
2321                 return 0;
2322         }
2323
2324         if (lv->status & MIRROR_LOG) {
2325                 log_error("Can't remove logical volume %s used as mirror log",
2326                           lv->name);
2327                 return 0;
2328         }
2329
2330         if (lv->status & LOCKED) {
2331                 log_error("Can't remove locked LV %s", lv->name);
2332                 return 0;
2333         }
2334
2335         /* FIXME Ensure not referred to by another existing LVs */
2336
2337         if (lv_info(cmd, lv, 0, &info, 1, 0)) {
2338                 if (info.open_count) {
2339                         log_error("Can't remove open logical volume \"%s\"",
2340                                   lv->name);
2341                         return 0;
2342                 }
2343
2344                 if (lv_is_active(lv) && (force == PROMPT) &&
2345                     lv_is_visible(lv) &&
2346                     yes_no_prompt("Do you really want to remove active "
2347                                   "%slogical volume %s? [y/n]: ",
2348                                   vg_is_clustered(vg) ? "clustered " : "",
2349                                   lv->name) == 'n') {
2350                         log_error("Logical volume %s not removed", lv->name);
2351                         return 0;
2352                 }
2353         }
2354
2355         if (!archive(vg))
2356                 return 0;
2357
2358         if (lv_is_cow(lv)) {
2359                 origin = origin_from_cow(lv);
2360                 was_merging = lv_is_merging_origin(origin);
2361                 log_verbose("Removing snapshot %s", lv->name);
2362                 /* vg_remove_snapshot() will preload origin if it was merging */
2363                 if (!vg_remove_snapshot(lv))
2364                         return_0;
2365         }
2366
2367         if (!deactivate_lv(cmd, lv)) {
2368                 log_error("Unable to deactivate logical volume \"%s\"",
2369                           lv->name);
2370                 return 0;
2371         }
2372
2373         log_verbose("Releasing logical volume \"%s\"", lv->name);
2374         if (!lv_remove(lv)) {
2375                 log_error("Error releasing logical volume \"%s\"", lv->name);
2376                 return 0;
2377         }
2378
2379         /* store it on disks */
2380         if (!vg_write(vg) || !vg_commit(vg))
2381                 return_0;
2382
2383         /* If no snapshots left, and was not merging, reload without -real. */
2384         if (origin && (!lv_is_origin(origin) && !was_merging)) {
2385                 if (!suspend_lv(cmd, origin)) {
2386                         log_error("Failed to refresh %s without snapshot.", origin->name);
2387                         return 0;
2388                 }
2389                 if (!resume_lv(cmd, origin)) {
2390                         log_error("Failed to resume %s.", origin->name);
2391                         return 0;
2392                 }
2393         }
2394
2395         backup(vg);
2396
2397         if (lv_is_visible(lv))
2398                 log_print("Logical volume \"%s\" successfully removed", lv->name);
2399
2400         return 1;
2401 }
2402
2403 /*
2404  * remove LVs with its dependencies - LV leaf nodes should be removed first
2405  */
2406 int lv_remove_with_dependencies(struct cmd_context *cmd, struct logical_volume *lv,
2407                                 const force_t force, unsigned level)
2408 {
2409         struct dm_list *snh, *snht;
2410
2411         if (lv_is_cow(lv)) {
2412                 /* A merging snapshot cannot be removed directly */
2413                 if (lv_is_merging_cow(lv) && !level) {
2414                         log_error("Can't remove merging snapshot logical volume \"%s\"",
2415                                   lv->name);
2416                         return 0;
2417                 }
2418         }
2419
2420         if (lv_is_origin(lv)) {
2421                 /* remove snapshot LVs first */
2422                 dm_list_iterate_safe(snh, snht, &lv->snapshot_segs) {
2423                         if (!lv_remove_with_dependencies(cmd, dm_list_struct_base(snh, struct lv_segment,
2424                                                                                   origin_list)->cow,
2425                                                          force, level + 1))
2426                                 return 0;
2427                 }
2428         }
2429
2430         return lv_remove_single(cmd, lv, force);
2431 }
2432
2433 /*
2434  * insert_layer_for_segments_on_pv() inserts a layer segment for a segment area.
2435  * However, layer modification could split the underlying layer segment.
2436  * This function splits the parent area according to keep the 1:1 relationship
2437  * between the parent area and the underlying layer segment.
2438  * Since the layer LV might have other layers below, build_parallel_areas()
2439  * is used to find the lowest-level segment boundaries.
2440  */
2441 static int _split_parent_area(struct lv_segment *seg, uint32_t s,
2442                               struct dm_list *layer_seg_pvs)
2443 {
2444         uint32_t parent_area_len, parent_le, layer_le;
2445         uint32_t area_multiple;
2446         struct seg_pvs *spvs;
2447
2448         if (seg_is_striped(seg))
2449                 area_multiple = seg->area_count;
2450         else
2451                 area_multiple = 1;
2452
2453         parent_area_len = seg->area_len;
2454         parent_le = seg->le;
2455         layer_le = seg_le(seg, s);
2456
2457         while (parent_area_len > 0) {
2458                 /* Find the layer segment pointed at */
2459                 if (!(spvs = _find_seg_pvs_by_le(layer_seg_pvs, layer_le))) {
2460                         log_error("layer segment for %s:%" PRIu32 " not found",
2461                                   seg->lv->name, parent_le);
2462                         return 0;
2463                 }
2464
2465                 if (spvs->le != layer_le) {
2466                         log_error("Incompatible layer boundary: "
2467                                   "%s:%" PRIu32 "[%" PRIu32 "] on %s:%" PRIu32,
2468                                   seg->lv->name, parent_le, s,
2469                                   seg_lv(seg, s)->name, layer_le);
2470                         return 0;
2471                 }
2472
2473                 if (spvs->len < parent_area_len) {
2474                         parent_le += spvs->len * area_multiple;
2475                         if (!lv_split_segment(seg->lv, parent_le))
2476                                 return_0;
2477                 }
2478
2479                 parent_area_len -= spvs->len;
2480                 layer_le += spvs->len;
2481         }
2482
2483         return 1;
2484 }
2485
2486 /*
2487  * Split the parent LV segments if the layer LV below it is splitted.
2488  */
2489 int split_parent_segments_for_layer(struct cmd_context *cmd,
2490                                     struct logical_volume *layer_lv)
2491 {
2492         struct lv_list *lvl;
2493         struct logical_volume *parent_lv;
2494         struct lv_segment *seg;
2495         uint32_t s;
2496         struct dm_list *parallel_areas;
2497
2498         if (!(parallel_areas = build_parallel_areas_from_lv(cmd, layer_lv, 0)))
2499                 return_0;
2500
2501         /* Loop through all LVs except itself */
2502         dm_list_iterate_items(lvl, &layer_lv->vg->lvs) {
2503                 parent_lv = lvl->lv;
2504                 if (parent_lv == layer_lv)
2505                         continue;
2506
2507                 /* Find all segments that point at the layer LV */
2508                 dm_list_iterate_items(seg, &parent_lv->segments) {
2509                         for (s = 0; s < seg->area_count; s++) {
2510                                 if (seg_type(seg, s) != AREA_LV ||
2511                                     seg_lv(seg, s) != layer_lv)
2512                                         continue;
2513
2514                                 if (!_split_parent_area(seg, s, parallel_areas))
2515                                         return_0;
2516                         }
2517                 }
2518         }
2519
2520         return 1;
2521 }
2522
2523 /* Remove a layer from the LV */
2524 int remove_layers_for_segments(struct cmd_context *cmd,
2525                                struct logical_volume *lv,
2526                                struct logical_volume *layer_lv,
2527                                uint64_t status_mask, struct dm_list *lvs_changed)
2528 {
2529         struct lv_segment *seg, *lseg;
2530         uint32_t s;
2531         int lv_changed = 0;
2532         struct lv_list *lvl;
2533
2534         log_very_verbose("Removing layer %s for segments of %s",
2535                          layer_lv->name, lv->name);
2536
2537         /* Find all segments that point at the temporary mirror */
2538         dm_list_iterate_items(seg, &lv->segments) {
2539                 for (s = 0; s < seg->area_count; s++) {
2540                         if (seg_type(seg, s) != AREA_LV ||
2541                             seg_lv(seg, s) != layer_lv)
2542                                 continue;
2543
2544                         /* Find the layer segment pointed at */
2545                         if (!(lseg = find_seg_by_le(layer_lv, seg_le(seg, s)))) {
2546                                 log_error("Layer segment found: %s:%" PRIu32,
2547                                           layer_lv->name, seg_le(seg, s));
2548                                 return 0;
2549                         }
2550
2551                         /* Check the segment params are compatible */
2552                         if (!seg_is_striped(lseg) || lseg->area_count != 1) {
2553                                 log_error("Layer is not linear: %s:%" PRIu32,
2554                                           layer_lv->name, lseg->le);
2555                                 return 0;
2556                         }
2557                         if ((lseg->status & status_mask) != status_mask) {
2558                                 log_error("Layer status does not match: "
2559                                           "%s:%" PRIu32 " status: 0x%" PRIx64 "/0x%" PRIx64,
2560                                           layer_lv->name, lseg->le,
2561                                           lseg->status, status_mask);
2562                                 return 0;
2563                         }
2564                         if (lseg->le != seg_le(seg, s) ||
2565                             lseg->area_len != seg->area_len) {
2566                                 log_error("Layer boundary mismatch: "
2567                                           "%s:%" PRIu32 "-%" PRIu32 " on "
2568                                           "%s:%" PRIu32 " / "
2569                                           "%" PRIu32 "-%" PRIu32 " / ",
2570                                           lv->name, seg->le, seg->area_len,
2571                                           layer_lv->name, seg_le(seg, s),
2572                                           lseg->le, lseg->area_len);
2573                                 return 0;
2574                         }
2575
2576                         if (!move_lv_segment_area(seg, s, lseg, 0))
2577                                 return_0;
2578
2579                         /* Replace mirror with error segment */
2580                         if (!(lseg->segtype =
2581                               get_segtype_from_string(lv->vg->cmd, "error"))) {
2582                                 log_error("Missing error segtype");
2583                                 return 0;
2584                         }
2585                         lseg->area_count = 0;
2586
2587                         /* First time, add LV to list of LVs affected */
2588                         if (!lv_changed && lvs_changed) {
2589                                 if (!(lvl = dm_pool_alloc(cmd->mem, sizeof(*lvl)))) {
2590                                         log_error("lv_list alloc failed");
2591                                         return 0;
2592                                 }
2593                                 lvl->lv = lv;
2594                                 dm_list_add(lvs_changed, &lvl->list);
2595                                 lv_changed = 1;
2596                         }
2597                 }
2598         }
2599         if (lv_changed && !lv_merge_segments(lv))
2600                 stack;
2601
2602         return 1;
2603 }
2604
2605 /* Remove a layer */
2606 int remove_layers_for_segments_all(struct cmd_context *cmd,
2607                                    struct logical_volume *layer_lv,
2608                                    uint64_t status_mask,
2609                                    struct dm_list *lvs_changed)
2610 {
2611         struct lv_list *lvl;
2612         struct logical_volume *lv1;
2613
2614         /* Loop through all LVs except the temporary mirror */
2615         dm_list_iterate_items(lvl, &layer_lv->vg->lvs) {
2616                 lv1 = lvl->lv;
2617                 if (lv1 == layer_lv)
2618                         continue;
2619
2620                 if (!remove_layers_for_segments(cmd, lv1, layer_lv,
2621                                                 status_mask, lvs_changed))
2622                         return_0;
2623         }
2624
2625         if (!lv_empty(layer_lv))
2626                 return_0;
2627
2628         return 1;
2629 }
2630
2631 static int _move_lv_segments(struct logical_volume *lv_to,
2632                              struct logical_volume *lv_from,
2633                              uint64_t set_status, uint64_t reset_status)
2634 {
2635         struct lv_segment *seg;
2636
2637         dm_list_iterate_items(seg, &lv_to->segments) {
2638                 if (seg->origin) {
2639                         log_error("Can't move snapshot segment");
2640                         return 0;
2641                 }
2642         }
2643
2644         lv_to->segments = lv_from->segments;
2645         lv_to->segments.n->p = &lv_to->segments;
2646         lv_to->segments.p->n = &lv_to->segments;
2647
2648         dm_list_iterate_items(seg, &lv_to->segments) {
2649                 seg->lv = lv_to;
2650                 seg->status &= ~reset_status;
2651                 seg->status |= set_status;
2652         }
2653
2654         dm_list_init(&lv_from->segments);
2655
2656         lv_to->le_count = lv_from->le_count;
2657         lv_to->size = lv_from->size;
2658
2659         lv_from->le_count = 0;
2660         lv_from->size = 0;
2661
2662         return 1;
2663 }
2664
2665 /* Remove a layer from the LV */
2666 int remove_layer_from_lv(struct logical_volume *lv,
2667                          struct logical_volume *layer_lv)
2668 {
2669         struct logical_volume *parent;
2670         struct lv_segment *parent_seg;
2671         struct segment_type *segtype;
2672
2673         log_very_verbose("Removing layer %s for %s", layer_lv->name, lv->name);
2674
2675         if (!(parent_seg = get_only_segment_using_this_lv(layer_lv))) {
2676                 log_error("Failed to find layer %s in %s",
2677                 layer_lv->name, lv->name);
2678                 return 0;
2679         }
2680         parent = parent_seg->lv;
2681
2682         /*
2683          * Before removal, the layer should be cleaned up,
2684          * i.e. additional segments and areas should have been removed.
2685          */
2686         if (dm_list_size(&parent->segments) != 1 ||
2687             parent_seg->area_count != 1 ||
2688             seg_type(parent_seg, 0) != AREA_LV ||
2689             layer_lv != seg_lv(parent_seg, 0) ||
2690             parent->le_count != layer_lv->le_count)
2691                 return_0;
2692
2693         if (!lv_empty(parent))
2694                 return_0;
2695
2696         if (!_move_lv_segments(parent, layer_lv, 0, 0))
2697                 return_0;
2698
2699         /* Replace the empty layer with error segment */
2700         segtype = get_segtype_from_string(lv->vg->cmd, "error");
2701         if (!lv_add_virtual_segment(layer_lv, 0, parent->le_count, segtype))
2702                 return_0;
2703
2704         return 1;
2705 }
2706
2707 /*
2708  * Create and insert a linear LV "above" lv_where.
2709  * After the insertion, a new LV named lv_where->name + suffix is created
2710  * and all segments of lv_where is moved to the new LV.
2711  * lv_where will have a single segment which maps linearly to the new LV.
2712  */
2713 struct logical_volume *insert_layer_for_lv(struct cmd_context *cmd,
2714                                            struct logical_volume *lv_where,
2715                                            uint64_t status,
2716                                            const char *layer_suffix)
2717 {
2718         struct logical_volume *layer_lv;
2719         char *name;
2720         size_t len;
2721         struct segment_type *segtype;
2722         struct lv_segment *mapseg;
2723
2724         /* create an empty layer LV */
2725         len = strlen(lv_where->name) + 32;
2726         if (!(name = alloca(len))) {
2727                 log_error("layer name allocation failed. "
2728                           "Remove new LV and retry.");
2729                 return NULL;
2730         }
2731
2732         if (dm_snprintf(name, len, "%s%s", lv_where->name, layer_suffix) < 0) {
2733                 log_error("layer name allocation failed. "
2734                           "Remove new LV and retry.");
2735                 return NULL;
2736         }
2737
2738         if (!(layer_lv = lv_create_empty(name, NULL, LVM_READ | LVM_WRITE,
2739                                          ALLOC_INHERIT, lv_where->vg))) {
2740                 log_error("Creation of layer LV failed");
2741                 return NULL;
2742         }
2743
2744         if (lv_is_active(lv_where) && strstr(name, "_mimagetmp")) {
2745                 log_very_verbose("Creating transient LV %s for mirror conversion in VG %s.", name, lv_where->vg->name);
2746
2747                 segtype = get_segtype_from_string(cmd, "error");
2748
2749                 if (!lv_add_virtual_segment(layer_lv, 0, lv_where->le_count, segtype)) {
2750                         log_error("Creation of transient LV %s for mirror conversion in VG %s failed.", name, lv_where->vg->name);
2751                         return NULL;
2752                 }
2753
2754                 if (!vg_write(lv_where->vg)) {
2755                         log_error("Failed to write intermediate VG %s metadata for mirror conversion.", lv_where->vg->name);
2756                         return NULL;
2757                 }
2758
2759                 if (!vg_commit(lv_where->vg)) {
2760                         log_error("Failed to commit intermediate VG %s metadata for mirror conversion.", lv_where->vg->name);
2761                         vg_revert(lv_where->vg);
2762                         return NULL;
2763                 }
2764
2765                 if (!activate_lv(cmd, layer_lv)) {
2766                         log_error("Failed to resume transient error LV %s for mirror conversion in VG %s.", name, lv_where->vg->name);
2767                         return NULL;
2768                 }
2769         }
2770
2771         log_very_verbose("Inserting layer %s for %s",
2772                          layer_lv->name, lv_where->name);
2773
2774         if (!_move_lv_segments(layer_lv, lv_where, 0, 0))
2775                 return_NULL;
2776
2777         if (!(segtype = get_segtype_from_string(cmd, "striped")))
2778                 return_NULL;
2779
2780         /* allocate a new linear segment */
2781         if (!(mapseg = alloc_lv_segment(cmd->mem, segtype,
2782                                         lv_where, 0, layer_lv->le_count,
2783                                         status, 0, NULL, 1, layer_lv->le_count,
2784                                         0, 0, 0, NULL)))
2785                 return_NULL;
2786
2787         /* map the new segment to the original underlying are */
2788         if (!set_lv_segment_area_lv(mapseg, 0, layer_lv, 0, 0))
2789                 return_NULL;
2790
2791         /* add the new segment to the layer LV */
2792         dm_list_add(&lv_where->segments, &mapseg->list);
2793         lv_where->le_count = layer_lv->le_count;
2794         lv_where->size = lv_where->le_count * lv_where->vg->extent_size;
2795
2796         return layer_lv;
2797 }
2798
2799 /*
2800  * Extend and insert a linear layer LV beneath the source segment area.
2801  */
2802 static int _extend_layer_lv_for_segment(struct logical_volume *layer_lv,
2803                                         struct lv_segment *seg, uint32_t s,
2804                                         uint64_t status)
2805 {
2806         struct lv_segment *mapseg;
2807         struct segment_type *segtype;
2808         struct physical_volume *src_pv = seg_pv(seg, s);
2809         uint32_t src_pe = seg_pe(seg, s);
2810
2811         if (seg_type(seg, s) != AREA_PV && seg_type(seg, s) != AREA_LV)
2812                 return_0;
2813
2814         if (!(segtype = get_segtype_from_string(layer_lv->vg->cmd, "striped")))
2815                 return_0;
2816
2817         /* FIXME Incomplete message? Needs more context */
2818         log_very_verbose("Inserting %s:%" PRIu32 "-%" PRIu32 " of %s/%s",
2819                          pv_dev_name(src_pv),
2820                          src_pe, src_pe + seg->area_len - 1,
2821                          seg->lv->vg->name, seg->lv->name);
2822
2823         /* allocate a new segment */
2824         if (!(mapseg = alloc_lv_segment(layer_lv->vg->cmd->mem, segtype,
2825                                         layer_lv, layer_lv->le_count,
2826                                         seg->area_len, status, 0,
2827                                         NULL, 1, seg->area_len, 0, 0, 0, seg)))
2828                 return_0;
2829
2830         /* map the new segment to the original underlying are */
2831         if (!move_lv_segment_area(mapseg, 0, seg, s))
2832                 return_0;
2833
2834         /* add the new segment to the layer LV */
2835         dm_list_add(&layer_lv->segments, &mapseg->list);
2836         layer_lv->le_count += seg->area_len;
2837         layer_lv->size += seg->area_len * layer_lv->vg->extent_size;
2838
2839         /* map the original area to the new segment */
2840         if (!set_lv_segment_area_lv(seg, s, layer_lv, mapseg->le, 0))
2841                 return_0;
2842
2843         return 1;
2844 }
2845
2846 /*
2847  * Match the segment area to PEs in the pvl
2848  * (the segment area boundary should be aligned to PE ranges by
2849  *  _adjust_layer_segments() so that there is no partial overlap.)
2850  */
2851 static int _match_seg_area_to_pe_range(struct lv_segment *seg, uint32_t s,
2852                                        struct pv_list *pvl)
2853 {
2854         struct pe_range *per;
2855         uint32_t pe_start, per_end;
2856
2857         if (!pvl)
2858                 return 1;
2859
2860         if (seg_type(seg, s) != AREA_PV || seg_dev(seg, s) != pvl->pv->dev)
2861                 return 0;
2862
2863         pe_start = seg_pe(seg, s);
2864
2865         /* Do these PEs match to any of the PEs in pvl? */
2866         dm_list_iterate_items(per, pvl->pe_ranges) {
2867                 per_end = per->start + per->count - 1;
2868
2869                 if ((pe_start < per->start) || (pe_start > per_end))
2870                         continue;
2871
2872                 /* FIXME Missing context in this message - add LV/seg details */
2873                 log_debug("Matched PE range %s:%" PRIu32 "-%" PRIu32 " against "
2874                           "%s %" PRIu32 " len %" PRIu32, dev_name(pvl->pv->dev),
2875                           per->start, per_end, dev_name(seg_dev(seg, s)),
2876                           seg_pe(seg, s), seg->area_len);
2877
2878                 return 1;
2879         }
2880
2881         return 0;
2882 }
2883
2884 /*
2885  * For each segment in lv_where that uses a PV in pvl directly,
2886  * split the segment if it spans more than one underlying PV.
2887  */
2888 static int _align_segment_boundary_to_pe_range(struct logical_volume *lv_where,
2889                                                struct pv_list *pvl)
2890 {
2891         struct lv_segment *seg;
2892         struct pe_range *per;
2893         uint32_t pe_start, pe_end, per_end, stripe_multiplier, s;
2894
2895         if (!pvl)
2896                 return 1;
2897
2898         /* Split LV segments to match PE ranges */
2899         dm_list_iterate_items(seg, &lv_where->segments) {
2900                 for (s = 0; s < seg->area_count; s++) {
2901                         if (seg_type(seg, s) != AREA_PV ||
2902                             seg_dev(seg, s) != pvl->pv->dev)
2903                                 continue;
2904
2905                         /* Do these PEs match with the condition? */
2906                         dm_list_iterate_items(per, pvl->pe_ranges) {
2907                                 pe_start = seg_pe(seg, s);
2908                                 pe_end = pe_start + seg->area_len - 1;
2909                                 per_end = per->start + per->count - 1;
2910
2911                                 /* No overlap? */
2912                                 if ((pe_end < per->start) ||
2913                                     (pe_start > per_end))
2914                                         continue;
2915
2916                                 if (seg_is_striped(seg))
2917                                         stripe_multiplier = seg->area_count;
2918                                 else
2919                                         stripe_multiplier = 1;
2920
2921                                 if ((per->start != pe_start &&
2922                                      per->start > pe_start) &&
2923                                     !lv_split_segment(lv_where, seg->le +
2924                                                       (per->start - pe_start) *
2925                                                       stripe_multiplier))
2926                                         return_0;
2927
2928                                 if ((per_end != pe_end &&
2929                                      per_end < pe_end) &&
2930                                     !lv_split_segment(lv_where, seg->le +
2931                                                       (per_end - pe_start + 1) *
2932                                                       stripe_multiplier))
2933                                         return_0;
2934                         }
2935                 }
2936         }
2937
2938         return 1;
2939 }
2940
2941 /*
2942  * Scan lv_where for segments on a PV in pvl, and for each one found
2943  * append a linear segment to lv_layer and insert it between the two.
2944  *
2945  * If pvl is empty, a layer is placed under the whole of lv_where.
2946  * If the layer is inserted, lv_where is added to lvs_changed.
2947  */
2948 int insert_layer_for_segments_on_pv(struct cmd_context *cmd,
2949                                     struct logical_volume *lv_where,
2950                                     struct logical_volume *layer_lv,
2951                                     uint64_t status,
2952                                     struct pv_list *pvl,
2953                                     struct dm_list *lvs_changed)
2954 {
2955         struct lv_segment *seg;
2956         struct lv_list *lvl;
2957         int lv_used = 0;
2958         uint32_t s;
2959
2960         log_very_verbose("Inserting layer %s for segments of %s on %s",
2961                          layer_lv->name, lv_where->name,
2962                          pvl ? pv_dev_name(pvl->pv) : "any");
2963
2964         if (!_align_segment_boundary_to_pe_range(lv_where, pvl))
2965                 return_0;
2966
2967         /* Work through all segments on the supplied PV */
2968         dm_list_iterate_items(seg, &lv_where->segments) {
2969                 for (s = 0; s < seg->area_count; s++) {
2970                         if (!_match_seg_area_to_pe_range(seg, s, pvl))
2971                                 continue;
2972
2973                         /* First time, add LV to list of LVs affected */
2974                         if (!lv_used && lvs_changed) {
2975                                 if (!(lvl = dm_pool_alloc(cmd->mem, sizeof(*lvl)))) {
2976                                         log_error("lv_list alloc failed");
2977                                         return 0;
2978                                 }
2979                                 lvl->lv = lv_where;
2980                                 dm_list_add(lvs_changed, &lvl->list);
2981                                 lv_used = 1;
2982                         }
2983
2984                         if (!_extend_layer_lv_for_segment(layer_lv, seg, s,
2985                                                           status)) {
2986                                 log_error("Failed to insert segment in layer "
2987                                           "LV %s under %s:%" PRIu32 "-%" PRIu32,
2988                                           layer_lv->name, lv_where->name,
2989                                           seg->le, seg->le + seg->len);
2990                                 return 0;
2991                         }
2992                 }
2993         }
2994
2995         return 1;
2996 }
2997
2998 /*
2999  * Initialize the LV with 'value'.
3000  */
3001 int set_lv(struct cmd_context *cmd, struct logical_volume *lv,
3002            uint64_t sectors, int value)
3003 {
3004         struct device *dev;
3005         char *name;
3006
3007         /*
3008          * FIXME:
3009          * <clausen> also, more than 4k
3010          * <clausen> say, reiserfs puts it's superblock 32k in, IIRC
3011          * <ejt_> k, I'll drop a fixme to that effect
3012          *         (I know the device is at least 4k, but not 32k)
3013          */
3014         if (!(name = dm_pool_alloc(cmd->mem, PATH_MAX))) {
3015                 log_error("Name allocation failed - device not cleared");
3016                 return 0;
3017         }
3018
3019         if (dm_snprintf(name, PATH_MAX, "%s%s/%s", cmd->dev_dir,
3020                         lv->vg->name, lv->name) < 0) {
3021                 log_error("Name too long - device not cleared (%s)", lv->name);
3022                 return 0;
3023         }
3024
3025         log_verbose("Clearing start of logical volume \"%s\"", lv->name);
3026
3027         if (!(dev = dev_cache_get(name, NULL))) {
3028                 log_error("%s: not found: device not cleared", name);
3029                 return 0;
3030         }
3031
3032         if (!dev_open_quiet(dev))
3033                 return_0;
3034
3035         if (!sectors)
3036                 sectors = UINT64_C(4096) >> SECTOR_SHIFT;
3037
3038         if (sectors > lv->size)
3039                 sectors = lv->size;
3040
3041         if (!dev_set(dev, UINT64_C(0), (size_t) sectors << SECTOR_SHIFT, value))
3042                 stack;
3043
3044         dev_flush(dev);
3045
3046         if (!dev_close_immediate(dev))
3047                 stack;
3048
3049         return 1;
3050 }
3051
3052
3053 static struct logical_volume *_create_virtual_origin(struct cmd_context *cmd,
3054                                                      struct volume_group *vg,
3055                                                      const char *lv_name,
3056                                                      uint32_t permission,
3057                                                      uint64_t voriginextents)
3058 {
3059         const struct segment_type *segtype;
3060         size_t len;
3061         char *vorigin_name;
3062         struct logical_volume *lv;
3063
3064         if (!(segtype = get_segtype_from_string(cmd, "zero"))) {
3065                 log_error("Zero segment type for virtual origin not found");
3066                 return NULL;
3067         }
3068
3069         len = strlen(lv_name) + 32;
3070         if (!(vorigin_name = alloca(len)) ||
3071             dm_snprintf(vorigin_name, len, "%s_vorigin", lv_name) < 0) {
3072                 log_error("Virtual origin name allocation failed.");
3073                 return NULL;
3074         }
3075
3076         if (!(lv = lv_create_empty(vorigin_name, NULL, permission,
3077                                    ALLOC_INHERIT, vg)))
3078                 return_NULL;
3079
3080         if (!lv_extend(lv, segtype, 1, 0, 1, voriginextents, NULL, 0u, 0u,
3081                        NULL, ALLOC_INHERIT))
3082                 return_NULL;
3083
3084         /* store vg on disk(s) */
3085         if (!vg_write(vg) || !vg_commit(vg))
3086                 return_NULL;
3087
3088         backup(vg);
3089
3090         return lv;
3091 }
3092
3093 int lv_create_single(struct volume_group *vg,
3094                      struct lvcreate_params *lp)
3095 {
3096         struct cmd_context *cmd = vg->cmd;
3097         uint32_t size_rest;
3098         uint64_t status = UINT64_C(0);
3099         struct logical_volume *lv, *org = NULL;
3100         int origin_active = 0;
3101         struct lvinfo info;
3102
3103         if (lp->lv_name && find_lv_in_vg(vg, lp->lv_name)) {
3104                 log_error("Logical volume \"%s\" already exists in "
3105                           "volume group \"%s\"", lp->lv_name, lp->vg_name);
3106                 return 0;
3107         }
3108
3109         if (vg_max_lv_reached(vg)) {
3110                 log_error("Maximum number of logical volumes (%u) reached "
3111                           "in volume group %s", vg->max_lv, vg->name);
3112                 return 0;
3113         }
3114
3115         if (lp->mirrors > 1 && !(vg->fid->fmt->features & FMT_SEGMENTS)) {
3116                 log_error("Metadata does not support mirroring.");
3117                 return 0;
3118         }
3119
3120         if (lp->read_ahead != DM_READ_AHEAD_AUTO &&
3121             lp->read_ahead != DM_READ_AHEAD_NONE &&
3122             (vg->fid->fmt->features & FMT_RESTRICTED_READAHEAD) &&
3123             (lp->read_ahead < 2 || lp->read_ahead > 120)) {
3124                 log_error("Metadata only supports readahead values between 2 and 120.");
3125                 return 0;
3126         }
3127
3128         if (lp->stripe_size > vg->extent_size) {
3129                 log_error("Reducing requested stripe size %s to maximum, "
3130                           "physical extent size %s",
3131                           display_size(cmd, (uint64_t) lp->stripe_size),
3132                           display_size(cmd, (uint64_t) vg->extent_size));
3133                 lp->stripe_size = vg->extent_size;
3134         }
3135
3136         /* Need to check the vg's format to verify this - the cmd format isn't setup properly yet */
3137         if (lp->stripes > 1 &&
3138             !(vg->fid->fmt->features & FMT_UNLIMITED_STRIPESIZE) &&
3139             (lp->stripe_size > STRIPE_SIZE_MAX)) {
3140                 log_error("Stripe size may not exceed %s",
3141                           display_size(cmd, (uint64_t) STRIPE_SIZE_MAX));
3142                 return 0;
3143         }
3144
3145         if ((size_rest = lp->extents % lp->stripes)) {
3146                 log_print("Rounding size (%d extents) up to stripe boundary "
3147                           "size (%d extents)", lp->extents,
3148                           lp->extents - size_rest + lp->stripes);
3149                 lp->extents = lp->extents - size_rest + lp->stripes;
3150         }
3151
3152         if (lp->zero && !activation()) {
3153                 log_error("Can't wipe start of new LV without using "
3154                           "device-mapper kernel driver");
3155                 return 0;
3156         }
3157
3158         status |= lp->permission | VISIBLE_LV;
3159
3160         if (lp->snapshot) {
3161                 if (!activation()) {
3162                         log_error("Can't create snapshot without using "
3163                                   "device-mapper kernel driver");
3164                         return 0;
3165                 }
3166                 /* FIXME Allow exclusive activation. */
3167                 if (vg_is_clustered(vg)) {
3168                         log_error("Clustered snapshots are not yet supported.");
3169                         return 0;
3170                 }
3171
3172                 /* Must zero cow */
3173                 status |= LVM_WRITE;
3174
3175                 if (lp->voriginsize)
3176                         origin_active = 1;
3177                 else {
3178
3179                         if (!(org = find_lv(vg, lp->origin))) {
3180                                 log_error("Couldn't find origin volume '%s'.",
3181                                           lp->origin);
3182                                 return 0;
3183                         }
3184                         if (lv_is_virtual_origin(org)) {
3185                                 log_error("Can't share virtual origins. "
3186                                           "Use --virtualsize.");
3187                                 return 0;
3188                         }
3189                         if (lv_is_cow(org)) {
3190                                 log_error("Snapshots of snapshots are not "
3191                                           "supported yet.");
3192                                 return 0;
3193                         }
3194                         if (org->status & LOCKED) {
3195                                 log_error("Snapshots of locked devices are not "
3196                                           "supported yet");
3197                                 return 0;
3198                         }
3199                         if (lv_is_merging_origin(org)) {
3200                                 log_error("Snapshots of an origin that has a "
3201                                           "merging snapshot is not supported");
3202                                 return 0;
3203                         }
3204                         if ((org->status & MIRROR_IMAGE) ||
3205                             (org->status & MIRROR_LOG)) {
3206                                 log_error("Snapshots of mirror %ss "
3207                                           "are not supported",
3208                                           (org->status & MIRROR_LOG) ?
3209                                           "log" : "image");
3210                                 return 0;
3211                         }
3212
3213                         if (!lv_info(cmd, org, 0, &info, 0, 0)) {
3214                                 log_error("Check for existence of snapshot "
3215                                           "origin '%s' failed.", org->name);
3216                                 return 0;
3217                         }
3218                         origin_active = info.exists;
3219                 }
3220         }
3221
3222         if (!lp->extents) {
3223                 log_error("Unable to create new logical volume with no extents");
3224                 return 0;
3225         }
3226
3227         if (lp->snapshot && (lp->extents * vg->extent_size < 2 * lp->chunk_size)) {
3228                 log_error("Unable to create a snapshot smaller than 2 chunks.");
3229                 return 0;
3230         }
3231
3232         if (!seg_is_virtual(lp) &&
3233             vg->free_count < lp->extents) {
3234                 log_error("Insufficient free extents (%u) in volume group %s: "
3235                           "%u required", vg->free_count, vg->name, lp->extents);
3236                 return 0;
3237         }
3238
3239         if (lp->stripes > dm_list_size(lp->pvh) && lp->alloc != ALLOC_ANYWHERE) {
3240                 log_error("Number of stripes (%u) must not exceed "
3241                           "number of physical volumes (%d)", lp->stripes,
3242                           dm_list_size(lp->pvh));
3243                 return 0;
3244         }
3245
3246         if (lp->mirrors > 1 && !activation()) {
3247                 log_error("Can't create mirror without using "
3248                           "device-mapper kernel driver.");
3249                 return 0;
3250         }
3251
3252         /* The snapshot segment gets created later */
3253         if (lp->snapshot &&
3254             !(lp->segtype = get_segtype_from_string(cmd, "striped")))
3255                 return_0;
3256
3257         if (!archive(vg))
3258                 return 0;
3259
3260         if (!dm_list_empty(&lp->tags)) {
3261                 if (!(vg->fid->fmt->features & FMT_TAGS)) {
3262                         log_error("Volume group %s does not support tags",
3263                                   vg->name);
3264                         return 0;
3265                 }
3266         }
3267
3268         if (lp->mirrors > 1) {
3269                 init_mirror_in_sync(lp->nosync);
3270
3271                 if (lp->nosync) {
3272                         log_warn("WARNING: New mirror won't be synchronised. "
3273                                   "Don't read what you didn't write!");
3274                         status |= MIRROR_NOTSYNCED;
3275                 }
3276         }
3277
3278         if (!(lv = lv_create_empty(lp->lv_name ? lp->lv_name : "lvol%d", NULL,
3279                                    status, lp->alloc, vg)))
3280                 return_0;
3281
3282         if (lp->read_ahead != lv->read_ahead) {
3283                 log_verbose("Setting read ahead sectors");
3284                 lv->read_ahead = lp->read_ahead;
3285         }
3286
3287         if (lp->minor >= 0) {
3288                 lv->major = lp->major;
3289                 lv->minor = lp->minor;
3290                 lv->status |= FIXED_MINOR;
3291                 log_verbose("Setting device number to (%d, %d)", lv->major,
3292                             lv->minor);
3293         }
3294
3295         if (!dm_list_empty(&lp->tags))
3296                 dm_list_splice(&lv->tags, &lp->tags);
3297
3298         if (!lv_extend(lv, lp->segtype, lp->stripes, lp->stripe_size,
3299                        1, lp->extents, NULL, 0u, 0u, lp->pvh, lp->alloc))
3300                 return_0;
3301
3302         if (lp->mirrors > 1) {
3303                 if (!lv_add_mirrors(cmd, lv, lp->mirrors - 1, lp->stripes,
3304                                     lp->stripe_size,
3305                                     adjusted_mirror_region_size(
3306                                                 vg->extent_size,
3307                                                 lv->le_count,
3308                                                 lp->region_size),
3309                                     lp->log_count, lp->pvh, lp->alloc,
3310                                     MIRROR_BY_LV |
3311                                     (lp->nosync ? MIRROR_SKIP_INIT_SYNC : 0))) {
3312                         stack;
3313                         goto revert_new_lv;
3314                 }
3315         }
3316
3317         /* store vg on disk(s) */
3318         if (!vg_write(vg) || !vg_commit(vg))
3319                 return_0;
3320
3321         backup(vg);
3322
3323         init_dmeventd_monitor(lp->activation_monitoring);
3324
3325         if (lp->snapshot) {
3326                 if (!activate_lv_excl(cmd, lv)) {
3327                         log_error("Aborting. Failed to activate snapshot "
3328                                   "exception store.");
3329                         goto revert_new_lv;
3330                 }
3331         } else if (!activate_lv(cmd, lv)) {
3332                 log_error("Failed to activate new LV.");
3333                 if (lp->zero)
3334                         goto deactivate_and_revert_new_lv;
3335                 return 0;
3336         }
3337
3338         if (!lp->zero && !lp->snapshot)
3339                 log_warn("WARNING: \"%s\" not zeroed", lv->name);
3340         else if (!set_lv(cmd, lv, UINT64_C(0), 0)) {
3341                 log_error("Aborting. Failed to wipe %s.",
3342                           lp->snapshot ? "snapshot exception store" :
3343                                          "start of new LV");
3344                 goto deactivate_and_revert_new_lv;
3345         }
3346
3347         if (lp->snapshot) {
3348                 /* Reset permission after zeroing */
3349                 if (!(lp->permission & LVM_WRITE))
3350                         lv->status &= ~LVM_WRITE;
3351
3352                 /* COW area must be deactivated if origin is not active */
3353                 if (!origin_active && !deactivate_lv(cmd, lv)) {
3354                         log_error("Aborting. Couldn't deactivate snapshot "
3355                                   "COW area. Manual intervention required.");
3356                         return 0;
3357                 }
3358
3359                 /* A virtual origin must be activated explicitly. */
3360                 if (lp->voriginsize &&
3361                     (!(org = _create_virtual_origin(cmd, vg, lv->name,
3362                                                     lp->permission,
3363                                                     lp->voriginextents)) ||
3364                      !activate_lv(cmd, org))) {
3365                         log_error("Couldn't create virtual origin for LV %s",
3366                                   lv->name);
3367                         if (org && !lv_remove(org))
3368                                 stack;
3369                         goto deactivate_and_revert_new_lv;
3370                 }
3371
3372                 /* cow LV remains active and becomes snapshot LV */
3373
3374                 if (!vg_add_snapshot(org, lv, NULL,
3375                                      org->le_count, lp->chunk_size)) {
3376                         log_error("Couldn't create snapshot.");
3377                         goto deactivate_and_revert_new_lv;
3378                 }
3379
3380                 /* store vg on disk(s) */
3381                 if (!vg_write(vg))
3382                         return_0;
3383
3384                 if (!suspend_lv(cmd, org)) {
3385                         log_error("Failed to suspend origin %s", org->name);
3386                         vg_revert(vg);
3387                         return 0;
3388                 }
3389
3390                 if (!vg_commit(vg))
3391                         return_0;
3392
3393                 if (!resume_lv(cmd, org)) {
3394                         log_error("Problem reactivating origin %s", org->name);
3395                         return 0;
3396                 }
3397         }
3398         /* FIXME out of sequence */
3399         backup(vg);
3400
3401         log_print("Logical volume \"%s\" created", lv->name);
3402
3403         /*
3404          * FIXME: as a sanity check we could try reading the
3405          * last block of the device ?
3406          */
3407
3408         return 1;
3409
3410 deactivate_and_revert_new_lv:
3411         if (!deactivate_lv(cmd, lv)) {
3412                 log_error("Unable to deactivate failed new LV. "
3413                           "Manual intervention required.");
3414                 return 0;
3415         }
3416
3417 revert_new_lv:
3418         /* FIXME Better to revert to backup of metadata? */
3419         if (!lv_remove(lv) || !vg_write(vg) || !vg_commit(vg))
3420                 log_error("Manual intervention may be required to remove "
3421                           "abandoned LV(s) before retrying.");
3422         else
3423                 backup(vg);
3424
3425         return 0;
3426 }
3427