2 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
3 * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
5 * This file is part of LVM2.
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.
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
20 #include "lvm-string.h"
21 #include "toolcontext.h"
35 int add_seg_to_segs_using_this_lv(struct logical_volume *lv,
36 struct lv_segment *seg)
40 dm_list_iterate_items(sl, &lv->segs_using_this_lv) {
47 log_very_verbose("Adding %s:%" PRIu32 " as an user of %s",
48 seg->lv->name, seg->le, lv->name);
50 if (!(sl = dm_pool_zalloc(lv->vg->vgmem, sizeof(*sl)))) {
51 log_error("Failed to allocate segment list");
57 dm_list_add(&lv->segs_using_this_lv, &sl->list);
62 int remove_seg_from_segs_using_this_lv(struct logical_volume *lv,
63 struct lv_segment *seg)
67 dm_list_iterate_items(sl, &lv->segs_using_this_lv) {
73 log_very_verbose("%s:%" PRIu32 " is no longer a user "
74 "of %s", seg->lv->name, seg->le,
76 dm_list_del(&sl->list);
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().
89 * In general, walk through lv->segs_using_this_lv.
91 struct lv_segment *get_only_segment_using_this_lv(struct logical_volume *lv)
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));
102 sl = dm_list_item(dm_list_first(&lv->segs_using_this_lv), struct seg_list);
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);
115 * PVs used by a segment of an LV
120 struct dm_list pvs; /* struct pv_list */
126 static struct seg_pvs *_find_seg_pvs_by_le(struct dm_list *list, uint32_t le)
128 struct seg_pvs *spvs;
130 dm_list_iterate_items(spvs, list)
131 if (le >= spvs->le && le < spvs->le + spvs->len)
138 * Find first unused LV number.
140 uint32_t find_free_lvnum(struct logical_volume *lv)
142 int lvnum_used[MAX_RESTRICTED_LVS + 1];
147 memset(&lvnum_used, 0, sizeof(lvnum_used));
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;
155 while (lvnum_used[i])
158 /* FIXME What if none are free? */
164 * All lv_segments get created here.
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,
171 uint32_t stripe_size,
172 struct logical_volume *log_lv,
176 uint32_t region_size,
177 uint32_t extents_copied,
178 struct lv_segment *pvmove_source_seg)
180 struct lv_segment *seg;
181 uint32_t areas_sz = area_count * sizeof(*seg->areas);
183 if (!(seg = dm_pool_zalloc(mem, sizeof(*seg))))
186 if (!(seg->areas = dm_pool_zalloc(mem, areas_sz))) {
187 dm_pool_free(mem, seg);
192 log_error("alloc_lv_segment: Missing segtype.");
196 seg->segtype = segtype;
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);
211 if (log_lv && !attach_mirror_log(seg, log_lv))
217 struct lv_segment *alloc_snapshot_seg(struct logical_volume *lv,
218 uint64_t status, uint32_t old_le_count)
220 struct lv_segment *seg;
221 const struct segment_type *segtype;
223 segtype = get_segtype_from_string(lv->vg->cmd, "snapshot");
225 log_error("Failed to find snapshot segtype");
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,
233 log_error("Couldn't allocate new snapshot segment.");
237 dm_list_add(&lv->segments, &seg->list);
238 lv->status |= VIRTUAL;
243 void release_lv_segment_area(struct lv_segment *seg, uint32_t s,
244 uint32_t area_reduction)
246 if (seg_type(seg, s) == AREA_UNASSIGNED)
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;
256 if (seg_lv(seg, s)->status & MIRROR_IMAGE) {
257 lv_reduce(seg_lv(seg, s), area_reduction);
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));
267 remove_seg_from_segs_using_this_lv(seg_lv(seg, s), seg);
268 seg_lv(seg, s) = NULL;
270 seg_type(seg, s) = AREA_UNASSIGNED;
275 * Move a segment area from one segment to another
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)
280 struct physical_volume *pv;
281 struct logical_volume *lv;
284 switch (seg_type(seg_from, area_from)) {
286 pv = seg_pv(seg_from, area_from);
287 pe = seg_pe(seg_from, area_from);
289 release_lv_segment_area(seg_from, area_from,
291 release_lv_segment_area(seg_to, area_to, seg_to->area_len);
293 if (!set_lv_segment_area_pv(seg_to, area_to, pv, pe))
299 lv = seg_lv(seg_from, area_from);
300 le = seg_le(seg_from, area_from);
302 release_lv_segment_area(seg_from, area_from,
304 release_lv_segment_area(seg_to, area_to, seg_to->area_len);
306 if (!set_lv_segment_area_lv(seg_to, area_to, lv, le, 0))
311 case AREA_UNASSIGNED:
312 release_lv_segment_area(seg_to, area_to, seg_to->area_len);
319 * Link part of a PV to an LV segment.
321 int set_lv_segment_area_pv(struct lv_segment *seg, uint32_t area_num,
322 struct physical_volume *pv, uint32_t pe)
324 seg->areas[area_num].type = AREA_PV;
326 if (!(seg_pvseg(seg, area_num) =
327 assign_peg_to_lvseg(pv, pe, seg->area_len, seg, area_num)))
334 * Link one LV segment to another. Assumes sizes already match.
336 int set_lv_segment_area_lv(struct lv_segment *seg, uint32_t area_num,
337 struct logical_volume *lv, uint32_t le,
340 log_very_verbose("Stack %s:%" PRIu32 "[%" PRIu32 "] on LV %s:%" PRIu32,
341 seg->lv->name, seg->le, area_num, lv->name, le);
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;
348 if (!add_seg_to_segs_using_this_lv(lv, seg))
355 * Prepare for adding parallel areas to an existing segment.
357 static int _lv_segment_add_areas(struct logical_volume *lv,
358 struct lv_segment *seg,
359 uint32_t new_area_count)
361 struct lv_segment_area *newareas;
362 uint32_t areas_sz = new_area_count * sizeof(*newareas);
364 if (!(newareas = dm_pool_zalloc(lv->vg->cmd->mem, areas_sz)))
367 memcpy(newareas, seg->areas, seg->area_count * sizeof(*seg->areas));
369 seg->areas = newareas;
370 seg->area_count = new_area_count;
376 * Reduce the size of an lv_segment. New size can be zero.
378 static int _lv_segment_reduce(struct lv_segment *seg, uint32_t reduction)
380 uint32_t area_reduction, s;
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);
390 area_reduction = (reduction / seg->area_count);
392 area_reduction = reduction;
394 for (s = 0; s < seg->area_count; s++)
395 release_lv_segment_area(seg, s, area_reduction);
397 seg->len -= reduction;
398 seg->area_len -= area_reduction;
404 * Entry point for all LV reductions in size.
406 static int _lv_reduce(struct logical_volume *lv, uint32_t extents, int delete)
408 struct lv_segment *seg;
409 uint32_t count = extents;
412 dm_list_iterate_back_items(seg, &lv->segments) {
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))
421 dm_list_del(&seg->list);
422 reduction = seg->len;
426 if (!_lv_segment_reduce(seg, reduction))
431 lv->le_count -= extents;
432 lv->size = (uint64_t) lv->le_count * lv->vg->extent_size;
437 /* Remove the LV if it is now empty */
438 if (!lv->le_count && !unlink_lv_from_vg(lv))
440 else if (lv->vg->fid->fmt->ops->lv_setup &&
441 !lv->vg->fid->fmt->ops->lv_setup(lv->vg->fid, lv))
450 int lv_empty(struct logical_volume *lv)
452 return _lv_reduce(lv, lv->le_count, 0);
456 * Empty an LV and add error segment.
458 int replace_lv_with_error_segment(struct logical_volume *lv)
460 uint32_t len = lv->le_count;
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".
470 lv->status &= ~MIRRORED;
472 /* FIXME: Should we bug if we find a log_lv attached? */
474 if (!lv_add_virtual_segment(lv, 0, len,
475 get_segtype_from_string(lv->vg->cmd,
483 * Remove given number of extents from LV.
485 int lv_reduce(struct logical_volume *lv, uint32_t extents)
487 return _lv_reduce(lv, extents, 1);
491 * Completely remove an LV.
493 int lv_remove(struct logical_volume *lv)
496 if (!lv_reduce(lv, lv->le_count))
503 * A set of contiguous physical extents allocated
505 struct alloced_area {
508 struct physical_volume *pv;
514 * Details of an allocation attempt
516 struct alloc_handle {
517 struct cmd_context *cmd;
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 */
529 const struct config_node *cling_tag_list_cn;
531 struct dm_list *parallel_areas; /* PVs to avoid */
534 * Contains area_count lists of areas allocated to data stripes
535 * followed by log_area_count lists of areas allocated to log stripes.
537 struct dm_list alloced_areas[0];
540 static uint32_t _calc_area_multiple(const struct segment_type *segtype,
541 const uint32_t area_count, const uint32_t stripes)
547 if (segtype_is_striped(segtype))
550 /* Mirrored stripes */
559 * Returns log device size in extents, algorithm from kernel code
562 static uint32_t mirror_log_extents(uint32_t region_size, uint32_t pe_size, uint32_t area_len)
564 size_t area_size, bitset_size, log_size, region_count;
566 area_size = area_len * pe_size;
567 region_count = dm_div_up(area_size, region_size);
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;
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;
577 return dm_div_up(log_size, pe_size);
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).
585 static struct alloc_handle *_alloc_init(struct cmd_context *cmd,
587 const struct segment_type *segtype,
588 alloc_policy_t alloc,
589 uint32_t new_extents,
592 uint32_t log_area_count,
593 uint32_t extent_size,
594 uint32_t region_size,
595 struct dm_list *parallel_areas)
597 struct alloc_handle *ah;
598 uint32_t s, area_count;
600 /* FIXME Caller should ensure this */
601 if (mirrors && !stripes)
604 if (segtype_is_virtual(segtype))
606 else if (mirrors > 1)
607 area_count = mirrors * stripes;
609 area_count = stripes;
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");
618 if (segtype_is_virtual(segtype))
621 if (!(area_count + log_area_count)) {
622 log_error(INTERNAL_ERROR "_alloc_init called for non-virtual segment with no disk space.");
626 if (!(ah->mem = dm_pool_create("allocation", 1024))) {
627 log_error("allocation pool creation failed");
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;
636 ah->area_multiple = _calc_area_multiple(segtype, area_count, stripes);
638 ah->log_len = log_area_count ? mirror_log_extents(ah->region_size, extent_size, ah->new_extents / ah->area_multiple) : 0;
640 for (s = 0; s < ah->area_count + ah->log_area_count; s++)
641 dm_list_init(&ah->alloced_areas[s]);
643 ah->parallel_areas = parallel_areas;
645 ah->cling_tag_list_cn = find_config_tree_node(cmd, "allocation/cling_tag_list");
650 void alloc_destroy(struct alloc_handle *ah)
653 dm_pool_destroy(ah->mem);
656 static int _log_parallel_areas(struct dm_pool *mem, struct dm_list *parallel_areas)
658 struct seg_pvs *spvs;
665 dm_list_iterate_items(spvs, parallel_areas) {
666 if (!dm_pool_begin_object(mem, 256)) {
667 log_error("dm_pool_begin_object failed");
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);
677 if (!dm_pool_grow_object(mem, " ", 1)) {
678 log_error("dm_pool_grow_object failed");
679 dm_pool_abandon_object(mem);
684 if (!dm_pool_grow_object(mem, "\0", 1)) {
685 log_error("dm_pool_grow_object failed");
686 dm_pool_abandon_object(mem);
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);
699 static int _setup_alloced_segment(struct logical_volume *lv, uint64_t status,
701 uint32_t stripe_size,
702 const struct segment_type *segtype,
703 struct alloced_area *aa,
704 uint32_t region_size)
706 uint32_t s, extents, area_multiple;
707 struct lv_segment *seg;
709 area_multiple = _calc_area_multiple(segtype, area_count, 0);
711 if (!(seg = alloc_lv_segment(lv->vg->cmd->mem, segtype, lv,
713 aa[0].len * area_multiple,
714 status, stripe_size, NULL,
716 aa[0].len, 0u, region_size, 0u, NULL))) {
717 log_error("Couldn't allocate new LV segment.");
721 for (s = 0; s < area_count; s++)
722 if (!set_lv_segment_area_pv(seg, s, aa[s].pv, aa[s].pe))
725 dm_list_add(&lv->segments, &seg->list);
727 extents = aa[0].len * area_multiple;
728 lv->le_count += extents;
729 lv->size += (uint64_t) extents *lv->vg->extent_size;
731 if (segtype_is_mirrored(segtype))
732 lv->status |= MIRRORED;
737 static int _setup_alloced_segments(struct logical_volume *lv,
738 struct dm_list *alloced_areas,
741 uint32_t stripe_size,
742 const struct segment_type *segtype,
743 uint32_t region_size)
745 struct alloced_area *aa;
747 dm_list_iterate_items(aa, &alloced_areas[0]) {
748 if (!_setup_alloced_segment(lv, status, area_count,
749 stripe_size, segtype, aa,
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.
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)
766 uint32_t area_len, len, remaining;
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;
772 if (!total_area_count) {
773 log_error(INTERNAL_ERROR "_alloc_parallel_area called without any allocation to do.");
777 remaining = needed - *allocated;
778 area_len = remaining / ah->area_multiple;
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;
785 if (!(aa = dm_pool_alloc(ah->mem, sizeof(*aa) * total_area_count))) {
786 log_error("alloced_area allocation failed");
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.
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;
802 aa[s].pv = areas[s + ix_log_skip].pva->map->pv;
803 aa[s].pe = areas[s + ix_log_skip].pva->start;
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);
810 consume_pv_area(areas[s + ix_log_skip].pva, len);
812 dm_list_add(&ah->alloced_areas[s], &aa[s].list);
815 ah->total_area_len += area_len;
817 *allocated += area_len * ah->area_multiple;
822 /* For striped mirrors, all the areas are counted, through the mirror layer */
823 static uint32_t _stripes_per_mimage(struct lv_segment *seg)
825 struct lv_segment *last_lvseg;
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;
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.
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,
855 uint32_t remaining_seg_len, area_len, area_multiple;
856 uint32_t stripes_per_mimage = 1;
859 if (!seg && !(seg = find_seg_by_le(lv, le))) {
860 log_error("Failed to find segment for %s extent %" PRIu32,
865 /* Remaining logical length of segment */
866 remaining_seg_len = seg->len - (le - seg->le);
868 if (remaining_seg_len > len)
869 remaining_seg_len = len;
871 if (max_seg_len && *max_seg_len > remaining_seg_len)
872 *max_seg_len = remaining_seg_len;
874 area_multiple = _calc_area_multiple(seg->segtype, seg->area_count, 0);
875 area_len = remaining_seg_len / area_multiple ? : 1;
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);
882 s < seg->area_count && (!max_areas || s <= max_areas);
884 if (seg_type(seg, s) == AREA_LV) {
885 if (!(r = _for_each_pv(cmd, seg_lv(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,
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)))
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,
911 /* FIXME Add snapshot cow LVs etc. */
916 static int _comp_area(const void *l, const void *r)
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;
921 if (lhs->used < rhs->used)
924 else if (lhs->used > rhs->used)
931 * Search for pvseg that matches condition
934 int (*condition)(struct pv_match *pvmatch, struct pv_segment *pvseg, struct pv_area *pva);
936 struct pv_area_used *areas;
939 const struct config_node *cling_tag_list_cn;
940 int s; /* Area index of match */
944 * Is PV area on the same PV?
946 static int _is_same_pv(struct pv_match *pvmatch __attribute((unused)), struct pv_segment *pvseg, struct pv_area *pva)
948 if (pvseg->pv != pva->map->pv)
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?
958 static int _has_matching_pv_tag(struct pv_match *pvmatch, struct pv_segment *pvseg, struct pv_area *pva)
960 const struct config_value *cv;
962 const char *tag_matched;
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");
972 log_error("Ignoring empty string in config file entry "
973 "allocation/cling_tag_list");
978 log_error("Ignoring string not starting with @ in config file entry "
979 "allocation/cling_tag_list: %s", str);
986 log_error("Ignoring empty tag in config file entry "
987 "allocation/cling_tag_list");
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))
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));
1002 if (!str_list_match_item(&pvseg->pv->tags, str) ||
1003 !str_list_match_item(&pva->map->pv->tags, str))
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));
1016 * Is PV area contiguous to PV segment?
1018 static int _is_contiguous(struct pv_match *pvmatch __attribute((unused)), struct pv_segment *pvseg, struct pv_area *pva)
1020 if (pvseg->pv != pva->map->pv)
1023 if (pvseg->pe + pvseg->len != pva->start)
1029 static int _is_condition(struct cmd_context *cmd __attribute__((unused)),
1030 struct pv_segment *pvseg, uint32_t s,
1033 struct pv_match *pvmatch = data;
1035 if (!pvmatch->condition(pvmatch, pvseg, pvmatch->pva))
1036 return 1; /* Continue */
1038 if (s >= pvmatch->areas_size)
1042 * Only used for cling and contiguous policies so it's safe to say all
1043 * the available space is used.
1045 pvmatch->areas[s].pva = pvmatch->pva;
1046 pvmatch->areas[s].used = pvmatch->pva->count;
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);
1053 return 2; /* Finished */
1057 * Is pva on same PV as any existing areas?
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)
1064 struct pv_match pvmatch;
1067 pvmatch.condition = cling_tag_list_cn ? _has_matching_pv_tag : _is_same_pv;
1068 pvmatch.areas = areas;
1069 pvmatch.areas_size = areas_size;
1071 pvmatch.cling_tag_list_cn = cling_tag_list_cn;
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,
1077 _is_condition, &pvmatch)))
1087 * Is pva contiguous to any existing areas or on the same PV?
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)
1093 struct pv_match pvmatch;
1096 pvmatch.condition = _is_contiguous;
1097 pvmatch.areas = areas;
1098 pvmatch.areas_size = areas_size;
1100 pvmatch.cling_tag_list_cn = NULL;
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,
1106 _is_condition, &pvmatch)))
1116 * Choose sets of parallel areas to use, respecting any constraints.
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)
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 */
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;
1139 struct alloced_area *aa;
1141 uint32_t total_extents_needed = (needed - *allocated) * ah->area_count / ah->area_multiple;
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);
1152 /* FIXME Select log PV appropriately if there isn't one yet */
1154 /* Are there any preceding segments we must follow on from? */
1156 ix_offset = _stripes_per_mimage(prev_lvseg) * prev_lvseg->area_count;
1157 if ((alloc == ALLOC_CONTIGUOUS))
1159 else if ((alloc == ALLOC_CLING))
1161 else if ((alloc == ALLOC_CLING_BY_TAGS)) {
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 */
1174 preferred_count = 0;
1176 parallel_pvs = NULL;
1177 max_parallel = needed;
1180 * If there are existing parallel PVs, avoid them and reduce
1181 * the maximum we can allocate in one go accordingly.
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)
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;
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.)
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.
1211 dm_list_iterate_items(pvm, pvms) {
1212 if (dm_list_empty(&pvm->areas))
1213 continue; /* Next PV */
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++)
1223 /* Avoid PVs used by existing parallel areas */
1225 dm_list_iterate_items(pvl, parallel_pvs)
1226 if (pvm->pv == pvl->pv)
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)
1238 _check_contiguous(ah->cmd,
1250 _check_cling(ah->cmd,
1251 use_cling_tags ? ah->cling_tag_list_cn : NULL,
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))))
1269 * Except with ALLOC_ANYWHERE, replace first area with this
1270 * one which is smaller but still big enough.
1272 if (!already_found_one ||
1273 alloc == ALLOC_ANYWHERE) {
1275 already_found_one = 1;
1278 required = (max_parallel - *allocated) / ah->area_multiple;
1280 if (alloc == ALLOC_ANYWHERE) {
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.
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;
1292 pva->unreserved -= required;
1293 reinsert_reduced_pv_area(pva);
1296 if (required < ah->log_len)
1297 required = ah->log_len;
1298 if (required > pva->count)
1299 required = pva->count;
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.");
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);
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))))
1329 } while (alloc == ALLOC_ANYWHERE && last_ix != ix && ix < ah->area_count + (*log_needs_allocating ? ah->log_area_count : 0));
1331 if (preferred_count < ix_offset)
1334 if (ix + ix_offset < ah->area_count +
1335 (*log_needs_allocating ? ah->log_area_count : 0))
1338 /* Sort the areas so we allocate from the biggest */
1340 qsort((*areas_ptr) + ix_offset, ix, sizeof(**areas_ptr),
1344 * First time around, if there's a log, allocate it on the
1345 * smallest device that has space for it.
1347 too_small_for_log_count = 0;
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;
1360 if (ix + ix_offset < ah->area_count +
1361 (*log_needs_allocating ? ah->log_area_count +
1362 too_small_for_log_count : 0))
1365 if (!_alloc_parallel_area(ah, max_parallel, *areas_ptr, allocated,
1366 *log_needs_allocating, ix_log_offset))
1369 *log_needs_allocating = 0;
1371 } while ((alloc != ALLOC_CONTIGUOUS) && *allocated != needed && can_split);
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.
1381 static int _allocate(struct alloc_handle *ah,
1382 struct volume_group *vg,
1383 struct logical_volume *lv,
1385 struct dm_list *allocatable_pvs)
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;
1392 struct dm_list *pvms;
1393 uint32_t areas_size;
1394 alloc_policy_t alloc;
1395 unsigned log_needs_allocating = 0;
1397 if (allocated >= ah->new_extents && !ah->log_area_count) {
1398 log_error("_allocate called with no work to do!");
1402 if (ah->log_area_count)
1403 log_needs_allocating = 1;
1405 if (ah->alloc == ALLOC_CONTIGUOUS)
1408 if (lv && !dm_list_empty(&lv->segments))
1409 prev_lvseg = dm_list_item(dm_list_last(&lv->segments),
1412 * Build the sets of available areas on the pv's.
1414 if (!(pvms = create_pv_maps(ah->mem, vg, allocatable_pvs)))
1417 if (!_log_parallel_areas(ah->mem, ah->parallel_areas))
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.");
1428 areas_size = ah->area_count + ah->log_area_count;
1431 /* Upper bound if none of the PVs in prev_lvseg is in pvms */
1432 /* FIXME Work size out properly */
1434 areas_size += _stripes_per_mimage(prev_lvseg) * prev_lvseg->area_count;
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.");
1443 * cling includes implicit cling_by_tags
1444 * but it does nothing unless the lvm.conf setting is present.
1446 if (ah->alloc == ALLOC_CLING)
1447 ah->alloc = ALLOC_CLING_BY_TAGS;
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)
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))
1468 if ((allocated == ah->new_extents && !log_needs_allocating) || (ah->alloc == alloc) ||
1469 (!can_split && (allocated != old_allocated)))
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 ",
1478 (ah->new_extents - allocated) * ah->area_count
1479 / ah->area_multiple);
1483 if (log_needs_allocating) {
1484 log_error("Insufficient extents for log allocation "
1485 "for logical volume %s.",
1486 lv ? lv->name : "");
1497 int lv_add_virtual_segment(struct logical_volume *lv, uint64_t status,
1498 uint32_t extents, const struct segment_type *segtype)
1500 struct lv_segment *seg;
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.");
1509 dm_list_add(&lv->segments, &seg->list);
1511 lv->le_count += extents;
1512 lv->size += (uint64_t) extents *lv->vg->extent_size;
1514 lv->status |= VIRTUAL;
1520 * Entry point for all extent allocations.
1522 struct alloc_handle *allocate_extents(struct volume_group *vg,
1523 struct logical_volume *lv,
1524 const struct segment_type *segtype,
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)
1532 struct alloc_handle *ah;
1533 uint32_t new_extents;
1535 if (segtype_is_virtual(segtype)) {
1536 log_error("allocate_extents does not handle virtual segments");
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,
1545 log_error("Consider changing the metadata format by running "
1550 if (alloc == ALLOC_INHERIT)
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,
1560 if (!segtype_is_virtual(segtype) &&
1561 !_allocate(ah, vg, lv, 1, allocatable_pvs)) {
1570 * Add new segments to an LV from supplied list of areas.
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,
1578 uint32_t region_size)
1581 log_error("Missing segtype in lv_add_segment().");
1585 if (segtype_is_virtual(segtype)) {
1586 log_error("lv_add_segment cannot handle virtual segments");
1590 if ((status & MIRROR_LOG) && dm_list_size(&lv->segments)) {
1591 log_error("Log segments can only be added to an empty LV");
1595 if (!_setup_alloced_segments(lv, &ah->alloced_areas[first_area],
1597 stripe_size, segtype,
1601 if ((segtype->flags & SEG_CAN_SPLIT) && !lv_merge_segments(lv)) {
1602 log_error("Couldn't merge segments after extending "
1607 if (lv->vg->fid->fmt->ops->lv_setup &&
1608 !lv->vg->fid->fmt->ops->lv_setup(lv->vg->fid, lv))
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.
1619 static struct lv_segment *_convert_seg_to_mirror(struct lv_segment *seg,
1620 uint32_t region_size,
1621 struct logical_volume *log_lv)
1623 struct lv_segment *newseg;
1626 if (!seg_is_striped(seg)) {
1627 log_error("Can't convert non-striped segment to mirrored.");
1631 if (seg->area_count > 1) {
1632 log_error("Can't convert striped segment with multiple areas "
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,
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");
1649 for (s = 0; s < seg->area_count; s++)
1650 if (!move_lv_segment_area(newseg, s, seg, s))
1653 seg->pvmove_source_seg = NULL; /* Not maintained after allocation */
1655 dm_list_add(&seg->list, &newseg->list);
1656 dm_list_del(&seg->list);
1662 * Add new areas to mirrored segments
1664 int lv_add_mirror_areas(struct alloc_handle *ah,
1665 struct logical_volume *lv, uint32_t le,
1666 uint32_t region_size)
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;
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);
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);
1689 if (!seg_is_mirrored(seg) &&
1690 (!(seg = _convert_seg_to_mirror(seg, region_size, NULL))))
1693 old_area_count = seg->area_count;
1694 new_area_count = old_area_count + ah->area_count;
1696 if (!_lv_segment_add_areas(lv, seg, new_area_count))
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))
1705 current_le += seg->area_len;
1708 lv->status |= MIRRORED;
1710 if (lv->vg->fid->fmt->ops->lv_setup &&
1711 !lv->vg->fid->fmt->ops->lv_setup(lv->vg->fid, lv))
1718 * Add mirror image LVs to mirrored segments
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)
1725 struct lv_segment *seg;
1726 uint32_t old_area_count, new_area_count;
1728 struct segment_type *mirror_segtype;
1730 seg = first_seg(lv);
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");
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)))
1742 if (region_size && region_size != seg->region_size) {
1743 log_error("Conflicting region_size");
1747 old_area_count = seg->area_count;
1748 new_area_count = old_area_count + num_extra_areas;
1750 if (!_lv_segment_add_areas(lv, seg, new_area_count)) {
1751 log_error("Failed to allocate widened LV segment for %s.",
1756 for (m = 0; m < old_area_count; m++)
1757 seg_lv(seg, m)->status |= status;
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],
1763 lv_set_hidden(sub_lvs[m - old_area_count]);
1766 lv->status |= MIRRORED;
1772 * Turn an empty LV into a mirror log.
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
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);
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
1791 int lv_add_log_segment(struct alloc_handle *ah, uint32_t first_area,
1792 struct logical_volume *log_lv, uint64_t status)
1795 return lv_add_segment(ah, ah->area_count + first_area, 1, log_lv,
1796 get_segtype_from_string(log_lv->vg->cmd,
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)
1806 struct lv_segment *seg;
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))
1814 m += lv_mirror_count(seg_lv(seg, s));
1818 if (!lv_add_segment(ah, m, stripes, seg_lv(seg, s),
1819 get_segtype_from_string(lv->vg->cmd,
1821 stripe_size, 0, 0)) {
1822 log_error("Aborting. Failed to extend %s.",
1823 seg_lv(seg, s)->name);
1828 seg->area_len += extents;
1829 seg->len += extents;
1830 lv->le_count += extents;
1831 lv->size += (uint64_t) extents *lv->vg->extent_size;
1837 * Entry point for single-step LV allocation + extension.
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)
1849 struct alloc_handle *ah;
1851 if (segtype_is_virtual(segtype))
1852 return lv_add_virtual_segment(lv, status, extents, segtype);
1854 if (!(ah = allocate_extents(lv->vg, lv, segtype, stripes, mirrors, 0, 0,
1855 extents, allocatable_pvs, alloc, NULL)))
1859 r = lv_add_segment(ah, 0, ah->area_count, lv, segtype,
1860 stripe_size, status, 0);
1862 r = _lv_extend_mirror(ah, lv, extents, 0, stripes, stripe_size);
1869 * Minimal LV renaming function.
1870 * Metadata transaction should be made by caller.
1871 * Assumes new_name is allocated from cmd->mem pool.
1873 static int _rename_single_lv(struct logical_volume *lv, char *new_name)
1875 struct volume_group *vg = lv->vg;
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);
1883 if (lv->status & LOCKED) {
1884 log_error("Cannot rename locked LV %s", lv->name);
1888 lv->name = new_name;
1895 * 'lv_name_old' and 'lv_name_new' are old and new names of the main LV.
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)
1901 char *suffix, *new_name;
1905 * A sub LV name starts with lv_name_old + '_'.
1906 * The suffix follows lv_name_old and includes '_'.
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);
1915 suffix = lv->name + len;
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"
1923 len = strlen(lv_name_new) + strlen(suffix) + 1;
1924 new_name = dm_pool_alloc(cmd->mem, len);
1926 log_error("Failed to allocate space for new name");
1929 if (!dm_snprintf(new_name, len, "%s%s", lv_name_new, suffix)) {
1930 log_error("Failed to create new name");
1935 return _rename_single_lv(lv, new_name);
1938 /* Callback for _for_each_sub_lv */
1939 static int _rename_cb(struct cmd_context *cmd, struct logical_volume *lv,
1942 struct lv_names *lv_names = (struct lv_names *) data;
1944 return _rename_sub_lv(cmd, lv, lv_names->old, lv_names->new);
1948 * Loop down sub LVs and call "func" for each.
1949 * "func" is responsible to log necessary information on failure.
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,
1957 struct logical_volume *org;
1958 struct lv_segment *seg;
1961 if (lv_is_cow(lv) && lv_is_virtual_origin(org = origin_from_cow(lv)))
1962 if (!func(cmd, org, data))
1965 dm_list_iterate_items(seg, &lv->segments) {
1966 if (seg->log_lv && !func(cmd, seg->log_lv, data))
1968 for (s = 0; s < seg->area_count; s++) {
1969 if (seg_type(seg, s) != AREA_LV)
1971 if (!func(cmd, seg_lv(seg, s), data))
1973 if (!_for_each_sub_lv(cmd, seg_lv(seg, s), func, data))
1983 * Core of LV renaming routine.
1984 * VG must be locked by caller.
1986 int lv_rename(struct cmd_context *cmd, struct logical_volume *lv,
1987 const char *new_name)
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;
1995 /* rename is not allowed on sub LVs */
1996 if (!lv_is_visible(lv)) {
1997 log_error("Cannot rename internal LV \"%s\".", lv->name);
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);
2007 if (lv->status & LOCKED) {
2008 log_error("Cannot rename locked LV %s", lv->name);
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))
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");
2028 dm_list_add(&lvs_changed, &lvl.list);
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);
2034 log_verbose("Writing out updated volume group");
2039 if (!suspend_lvs(cmd, &lvs_changed)) {
2044 if (!(r = vg_commit(vg)))
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)
2052 dm_list_iterate_back_items(lvlp, &lvs_changed)
2053 if (!resume_lv(cmd, lvlp->lv))
2060 char *generate_lv_name(struct volume_group *vg, const char *format,
2061 char *buffer, size_t len)
2063 struct lv_list *lvl;
2066 dm_list_iterate_items(lvl, &vg->lvs) {
2067 if (sscanf(lvl->lv->name, format, &i) != 1)
2074 if (dm_snprintf(buffer, len, format, high + 1) < 0)
2080 int vg_max_lv_reached(struct volume_group *vg)
2085 if (vg->max_lv > vg_visible_lvs(vg))
2088 log_verbose("Maximum number of logical volumes (%u) reached "
2089 "in volume group %s", vg->max_lv, vg->name);
2094 struct logical_volume *alloc_lv(struct dm_pool *mem)
2096 struct logical_volume *lv;
2098 if (!(lv = dm_pool_zalloc(mem, sizeof(*lv)))) {
2099 log_error("Unable to allocate logical volume structure");
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);
2114 * Create a new empty LV.
2116 struct logical_volume *lv_create_empty(const char *name,
2119 alloc_policy_t alloc,
2120 struct volume_group *vg)
2122 struct format_instance *fi = vg->fid;
2123 struct logical_volume *lv;
2124 char dname[NAME_LEN];
2126 if (vg_max_lv_reached(vg))
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 "
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);
2140 log_verbose("Creating logical volume %s", name);
2142 if (!(lv = alloc_lv(vg->vgmem)))
2145 if (!(lv->name = dm_pool_strdup(vg->vgmem, name)))
2148 lv->status = status;
2150 lv->read_ahead = vg->cmd->default_settings.read_ahead;
2153 lv->size = UINT64_C(0);
2159 if (!link_lv_to_vg(vg, lv))
2162 if (fi->fmt->ops->lv_setup && !fi->fmt->ops->lv_setup(fi, lv))
2167 dm_pool_free(vg->vgmem, lv);
2171 static int _add_pvs(struct cmd_context *cmd, struct pv_segment *peg,
2172 uint32_t s __attribute__((unused)), void *data)
2174 struct seg_pvs *spvs = (struct seg_pvs *) data;
2175 struct pv_list *pvl;
2177 /* Don't add again if it's already on list. */
2178 if (find_pv_in_pv_list(&spvs->pvs, peg->pv))
2181 if (!(pvl = dm_pool_alloc(cmd->mem, sizeof(*pvl)))) {
2182 log_error("pv_list allocation failed");
2188 dm_list_add(&spvs->pvs, &pvl->list);
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.
2197 struct dm_list *build_parallel_areas_from_lv(struct cmd_context *cmd,
2198 struct logical_volume *lv,
2199 unsigned use_pvmove_parent_lv)
2201 struct dm_list *parallel_areas;
2202 struct seg_pvs *spvs;
2203 uint32_t current_le = 0;
2204 struct lv_segment * uninitialized_var(seg);
2206 if (!(parallel_areas = dm_pool_alloc(cmd->mem, sizeof(*parallel_areas)))) {
2207 log_error("parallel_areas allocation failed");
2211 dm_list_init(parallel_areas);
2214 if (!(spvs = dm_pool_zalloc(cmd->mem, sizeof(*spvs)))) {
2215 log_error("allocation failed");
2219 dm_list_init(&spvs->pvs);
2221 spvs->le = current_le;
2222 spvs->len = lv->le_count - current_le;
2224 dm_list_add(parallel_areas, &spvs->list);
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);
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,
2239 0, 0, -1, 0, _add_pvs, (void *) spvs))
2242 current_le = spvs->le + spvs->len;
2243 } while (current_le < lv->le_count);
2245 /* FIXME Merge adjacent segments with identical PV lists (avoids need for contiguous allocation attempts between successful allocations) */
2247 return parallel_areas;
2250 int link_lv_to_vg(struct volume_group *vg, struct logical_volume *lv)
2252 struct lv_list *lvl;
2254 if (vg_max_lv_reached(vg))
2257 if (!(lvl = dm_pool_zalloc(vg->vgmem, sizeof(*lvl))))
2262 dm_list_add(&vg->lvs, &lvl->list);
2267 int unlink_lv_from_vg(struct logical_volume *lv)
2269 struct lv_list *lvl;
2271 if (!(lvl = find_lv_in_vg(lv->vg, lv->name)))
2274 dm_list_del(&lvl->list);
2279 void lv_set_visible(struct logical_volume *lv)
2281 if (lv_is_visible(lv))
2284 lv->status |= VISIBLE_LV;
2286 log_debug("LV %s in VG %s is now visible.", lv->name, lv->vg->name);
2289 void lv_set_hidden(struct logical_volume *lv)
2291 if (!lv_is_visible(lv))
2294 lv->status &= ~VISIBLE_LV;
2296 log_debug("LV %s in VG %s is now hidden.", lv->name, lv->vg->name);
2299 int lv_remove_single(struct cmd_context *cmd, struct logical_volume *lv,
2300 const force_t force)
2302 struct volume_group *vg;
2304 struct logical_volume *origin = NULL;
2305 int was_merging = 0;
2309 if (!vg_check_status(vg, LVM_WRITE))
2312 if (lv_is_origin(lv)) {
2313 log_error("Can't remove logical volume \"%s\" under snapshot",
2318 if (lv->status & MIRROR_IMAGE) {
2319 log_error("Can't remove logical volume %s used by a mirror",
2324 if (lv->status & MIRROR_LOG) {
2325 log_error("Can't remove logical volume %s used as mirror log",
2330 if (lv->status & LOCKED) {
2331 log_error("Can't remove locked LV %s", lv->name);
2335 /* FIXME Ensure not referred to by another existing LVs */
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\"",
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 " : "",
2350 log_error("Logical volume %s not removed", lv->name);
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))
2367 if (!deactivate_lv(cmd, lv)) {
2368 log_error("Unable to deactivate logical volume \"%s\"",
2373 log_verbose("Releasing logical volume \"%s\"", lv->name);
2374 if (!lv_remove(lv)) {
2375 log_error("Error releasing logical volume \"%s\"", lv->name);
2379 /* store it on disks */
2380 if (!vg_write(vg) || !vg_commit(vg))
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);
2389 if (!resume_lv(cmd, origin)) {
2390 log_error("Failed to resume %s.", origin->name);
2397 if (lv_is_visible(lv))
2398 log_print("Logical volume \"%s\" successfully removed", lv->name);
2404 * remove LVs with its dependencies - LV leaf nodes should be removed first
2406 int lv_remove_with_dependencies(struct cmd_context *cmd, struct logical_volume *lv,
2407 const force_t force, unsigned level)
2409 struct dm_list *snh, *snht;
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\"",
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,
2430 return lv_remove_single(cmd, lv, force);
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.
2441 static int _split_parent_area(struct lv_segment *seg, uint32_t s,
2442 struct dm_list *layer_seg_pvs)
2444 uint32_t parent_area_len, parent_le, layer_le;
2445 uint32_t area_multiple;
2446 struct seg_pvs *spvs;
2448 if (seg_is_striped(seg))
2449 area_multiple = seg->area_count;
2453 parent_area_len = seg->area_len;
2454 parent_le = seg->le;
2455 layer_le = seg_le(seg, s);
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);
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);
2473 if (spvs->len < parent_area_len) {
2474 parent_le += spvs->len * area_multiple;
2475 if (!lv_split_segment(seg->lv, parent_le))
2479 parent_area_len -= spvs->len;
2480 layer_le += spvs->len;
2487 * Split the parent LV segments if the layer LV below it is splitted.
2489 int split_parent_segments_for_layer(struct cmd_context *cmd,
2490 struct logical_volume *layer_lv)
2492 struct lv_list *lvl;
2493 struct logical_volume *parent_lv;
2494 struct lv_segment *seg;
2496 struct dm_list *parallel_areas;
2498 if (!(parallel_areas = build_parallel_areas_from_lv(cmd, layer_lv, 0)))
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)
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)
2514 if (!_split_parent_area(seg, s, parallel_areas))
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)
2529 struct lv_segment *seg, *lseg;
2532 struct lv_list *lvl;
2534 log_very_verbose("Removing layer %s for segments of %s",
2535 layer_lv->name, lv->name);
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)
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));
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);
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);
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 "
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);
2576 if (!move_lv_segment_area(seg, s, lseg, 0))
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");
2585 lseg->area_count = 0;
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");
2594 dm_list_add(lvs_changed, &lvl->list);
2599 if (lv_changed && !lv_merge_segments(lv))
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)
2611 struct lv_list *lvl;
2612 struct logical_volume *lv1;
2614 /* Loop through all LVs except the temporary mirror */
2615 dm_list_iterate_items(lvl, &layer_lv->vg->lvs) {
2617 if (lv1 == layer_lv)
2620 if (!remove_layers_for_segments(cmd, lv1, layer_lv,
2621 status_mask, lvs_changed))
2625 if (!lv_empty(layer_lv))
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)
2635 struct lv_segment *seg;
2637 dm_list_iterate_items(seg, &lv_to->segments) {
2639 log_error("Can't move snapshot segment");
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;
2648 dm_list_iterate_items(seg, &lv_to->segments) {
2650 seg->status &= ~reset_status;
2651 seg->status |= set_status;
2654 dm_list_init(&lv_from->segments);
2656 lv_to->le_count = lv_from->le_count;
2657 lv_to->size = lv_from->size;
2659 lv_from->le_count = 0;
2665 /* Remove a layer from the LV */
2666 int remove_layer_from_lv(struct logical_volume *lv,
2667 struct logical_volume *layer_lv)
2669 struct logical_volume *parent;
2670 struct lv_segment *parent_seg;
2671 struct segment_type *segtype;
2673 log_very_verbose("Removing layer %s for %s", layer_lv->name, lv->name);
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);
2680 parent = parent_seg->lv;
2683 * Before removal, the layer should be cleaned up,
2684 * i.e. additional segments and areas should have been removed.
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)
2693 if (!lv_empty(parent))
2696 if (!_move_lv_segments(parent, layer_lv, 0, 0))
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))
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.
2713 struct logical_volume *insert_layer_for_lv(struct cmd_context *cmd,
2714 struct logical_volume *lv_where,
2716 const char *layer_suffix)
2718 struct logical_volume *layer_lv;
2721 struct segment_type *segtype;
2722 struct lv_segment *mapseg;
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.");
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.");
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");
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);
2747 segtype = get_segtype_from_string(cmd, "error");
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);
2754 if (!vg_write(lv_where->vg)) {
2755 log_error("Failed to write intermediate VG %s metadata for mirror conversion.", lv_where->vg->name);
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);
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);
2771 log_very_verbose("Inserting layer %s for %s",
2772 layer_lv->name, lv_where->name);
2774 if (!_move_lv_segments(layer_lv, lv_where, 0, 0))
2777 if (!(segtype = get_segtype_from_string(cmd, "striped")))
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,
2787 /* map the new segment to the original underlying are */
2788 if (!set_lv_segment_area_lv(mapseg, 0, layer_lv, 0, 0))
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;
2800 * Extend and insert a linear layer LV beneath the source segment area.
2802 static int _extend_layer_lv_for_segment(struct logical_volume *layer_lv,
2803 struct lv_segment *seg, uint32_t s,
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);
2811 if (seg_type(seg, s) != AREA_PV && seg_type(seg, s) != AREA_LV)
2814 if (!(segtype = get_segtype_from_string(layer_lv->vg->cmd, "striped")))
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);
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)))
2830 /* map the new segment to the original underlying are */
2831 if (!move_lv_segment_area(mapseg, 0, seg, s))
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;
2839 /* map the original area to the new segment */
2840 if (!set_lv_segment_area_lv(seg, s, layer_lv, mapseg->le, 0))
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.)
2851 static int _match_seg_area_to_pe_range(struct lv_segment *seg, uint32_t s,
2852 struct pv_list *pvl)
2854 struct pe_range *per;
2855 uint32_t pe_start, per_end;
2860 if (seg_type(seg, s) != AREA_PV || seg_dev(seg, s) != pvl->pv->dev)
2863 pe_start = seg_pe(seg, s);
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;
2869 if ((pe_start < per->start) || (pe_start > per_end))
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);
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.
2888 static int _align_segment_boundary_to_pe_range(struct logical_volume *lv_where,
2889 struct pv_list *pvl)
2891 struct lv_segment *seg;
2892 struct pe_range *per;
2893 uint32_t pe_start, pe_end, per_end, stripe_multiplier, s;
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)
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;
2912 if ((pe_end < per->start) ||
2913 (pe_start > per_end))
2916 if (seg_is_striped(seg))
2917 stripe_multiplier = seg->area_count;
2919 stripe_multiplier = 1;
2921 if ((per->start != pe_start &&
2922 per->start > pe_start) &&
2923 !lv_split_segment(lv_where, seg->le +
2924 (per->start - pe_start) *
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) *
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.
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.
2948 int insert_layer_for_segments_on_pv(struct cmd_context *cmd,
2949 struct logical_volume *lv_where,
2950 struct logical_volume *layer_lv,
2952 struct pv_list *pvl,
2953 struct dm_list *lvs_changed)
2955 struct lv_segment *seg;
2956 struct lv_list *lvl;
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");
2964 if (!_align_segment_boundary_to_pe_range(lv_where, pvl))
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))
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");
2980 dm_list_add(lvs_changed, &lvl->list);
2984 if (!_extend_layer_lv_for_segment(layer_lv, seg, s,
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);
2999 * Initialize the LV with 'value'.
3001 int set_lv(struct cmd_context *cmd, struct logical_volume *lv,
3002 uint64_t sectors, int value)
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)
3014 if (!(name = dm_pool_alloc(cmd->mem, PATH_MAX))) {
3015 log_error("Name allocation failed - device not cleared");
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);
3025 log_verbose("Clearing start of logical volume \"%s\"", lv->name);
3027 if (!(dev = dev_cache_get(name, NULL))) {
3028 log_error("%s: not found: device not cleared", name);
3032 if (!dev_open_quiet(dev))
3036 sectors = UINT64_C(4096) >> SECTOR_SHIFT;
3038 if (sectors > lv->size)
3041 if (!dev_set(dev, UINT64_C(0), (size_t) sectors << SECTOR_SHIFT, value))
3046 if (!dev_close_immediate(dev))
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)
3059 const struct segment_type *segtype;
3062 struct logical_volume *lv;
3064 if (!(segtype = get_segtype_from_string(cmd, "zero"))) {
3065 log_error("Zero segment type for virtual origin not found");
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.");
3076 if (!(lv = lv_create_empty(vorigin_name, NULL, permission,
3077 ALLOC_INHERIT, vg)))
3080 if (!lv_extend(lv, segtype, 1, 0, 1, voriginextents, NULL, 0u, 0u,
3081 NULL, ALLOC_INHERIT))
3084 /* store vg on disk(s) */
3085 if (!vg_write(vg) || !vg_commit(vg))
3093 int lv_create_single(struct volume_group *vg,
3094 struct lvcreate_params *lp)
3096 struct cmd_context *cmd = vg->cmd;
3098 uint64_t status = UINT64_C(0);
3099 struct logical_volume *lv, *org = NULL;
3100 int origin_active = 0;
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);
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);
3115 if (lp->mirrors > 1 && !(vg->fid->fmt->features & FMT_SEGMENTS)) {
3116 log_error("Metadata does not support mirroring.");
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.");
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;
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));
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;
3152 if (lp->zero && !activation()) {
3153 log_error("Can't wipe start of new LV without using "
3154 "device-mapper kernel driver");
3158 status |= lp->permission | VISIBLE_LV;
3161 if (!activation()) {
3162 log_error("Can't create snapshot without using "
3163 "device-mapper kernel driver");
3166 /* FIXME Allow exclusive activation. */
3167 if (vg_is_clustered(vg)) {
3168 log_error("Clustered snapshots are not yet supported.");
3173 status |= LVM_WRITE;
3175 if (lp->voriginsize)
3179 if (!(org = find_lv(vg, lp->origin))) {
3180 log_error("Couldn't find origin volume '%s'.",
3184 if (lv_is_virtual_origin(org)) {
3185 log_error("Can't share virtual origins. "
3186 "Use --virtualsize.");
3189 if (lv_is_cow(org)) {
3190 log_error("Snapshots of snapshots are not "
3194 if (org->status & LOCKED) {
3195 log_error("Snapshots of locked devices are not "
3199 if (lv_is_merging_origin(org)) {
3200 log_error("Snapshots of an origin that has a "
3201 "merging snapshot is not supported");
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) ?
3213 if (!lv_info(cmd, org, 0, &info, 0, 0)) {
3214 log_error("Check for existence of snapshot "
3215 "origin '%s' failed.", org->name);
3218 origin_active = info.exists;
3223 log_error("Unable to create new logical volume with no extents");
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.");
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);
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));
3246 if (lp->mirrors > 1 && !activation()) {
3247 log_error("Can't create mirror without using "
3248 "device-mapper kernel driver.");
3252 /* The snapshot segment gets created later */
3254 !(lp->segtype = get_segtype_from_string(cmd, "striped")))
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",
3268 if (lp->mirrors > 1) {
3269 init_mirror_in_sync(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;
3278 if (!(lv = lv_create_empty(lp->lv_name ? lp->lv_name : "lvol%d", NULL,
3279 status, lp->alloc, vg)))
3282 if (lp->read_ahead != lv->read_ahead) {
3283 log_verbose("Setting read ahead sectors");
3284 lv->read_ahead = lp->read_ahead;
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,
3295 if (!dm_list_empty(&lp->tags))
3296 dm_list_splice(&lv->tags, &lp->tags);
3298 if (!lv_extend(lv, lp->segtype, lp->stripes, lp->stripe_size,
3299 1, lp->extents, NULL, 0u, 0u, lp->pvh, lp->alloc))
3302 if (lp->mirrors > 1) {
3303 if (!lv_add_mirrors(cmd, lv, lp->mirrors - 1, lp->stripes,
3305 adjusted_mirror_region_size(
3309 lp->log_count, lp->pvh, lp->alloc,
3311 (lp->nosync ? MIRROR_SKIP_INIT_SYNC : 0))) {
3317 /* store vg on disk(s) */
3318 if (!vg_write(vg) || !vg_commit(vg))
3323 init_dmeventd_monitor(lp->activation_monitoring);
3326 if (!activate_lv_excl(cmd, lv)) {
3327 log_error("Aborting. Failed to activate snapshot "
3328 "exception store.");
3331 } else if (!activate_lv(cmd, lv)) {
3332 log_error("Failed to activate new LV.");
3334 goto deactivate_and_revert_new_lv;
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" :
3344 goto deactivate_and_revert_new_lv;
3348 /* Reset permission after zeroing */
3349 if (!(lp->permission & LVM_WRITE))
3350 lv->status &= ~LVM_WRITE;
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.");
3359 /* A virtual origin must be activated explicitly. */
3360 if (lp->voriginsize &&
3361 (!(org = _create_virtual_origin(cmd, vg, lv->name,
3363 lp->voriginextents)) ||
3364 !activate_lv(cmd, org))) {
3365 log_error("Couldn't create virtual origin for LV %s",
3367 if (org && !lv_remove(org))
3369 goto deactivate_and_revert_new_lv;
3372 /* cow LV remains active and becomes snapshot LV */
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;
3380 /* store vg on disk(s) */
3384 if (!suspend_lv(cmd, org)) {
3385 log_error("Failed to suspend origin %s", org->name);
3393 if (!resume_lv(cmd, org)) {
3394 log_error("Problem reactivating origin %s", org->name);
3398 /* FIXME out of sequence */
3401 log_print("Logical volume \"%s\" created", lv->name);
3404 * FIXME: as a sanity check we could try reading the
3405 * last block of the device ?
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.");
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.");