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