2 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
3 * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
5 * This file is part of LVM2.
7 * This copyrighted material is made available to anyone wishing to use,
8 * modify, copy, or redistribute it subject to the terms and conditions
9 * of the GNU Lesser General Public License v.2.1.
11 * You should have received a copy of the GNU Lesser General Public License
12 * along with this program; if not, write to the Free Software Foundation,
13 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include "toolcontext.h"
21 #include "lvm1-label.h"
26 /* VG consistency checks */
27 static int _check_vgs(struct dm_list *pvs, struct volume_group *vg)
29 struct dm_list *pvh, *t;
30 struct disk_list *dl = NULL;
31 struct disk_list *first = NULL;
33 uint32_t pv_count = 0;
34 uint32_t exported = 0;
38 * If there are exported and unexported PVs, ignore exported ones.
39 * This means an active VG won't be affected if disks are inserted
40 * bearing an exported VG with the same name.
42 dm_list_iterate_items(dl, pvs) {
44 exported = dl->pvd.pv_status & VG_EXPORTED;
49 if (exported != (dl->pvd.pv_status & VG_EXPORTED)) {
50 /* Remove exported PVs */
51 dm_list_iterate_safe(pvh, t, pvs) {
52 dl = dm_list_item(pvh, struct disk_list);
53 if (dl->pvd.pv_status & VG_EXPORTED)
60 /* Remove any PVs with VG structs that differ from the first */
61 dm_list_iterate_safe(pvh, t, pvs) {
62 dl = dm_list_item(pvh, struct disk_list);
67 else if (memcmp(&first->vgd, &dl->vgd, sizeof(first->vgd))) {
68 log_error("VG data differs between PVs %s and %s",
69 dev_name(first->dev), dev_name(dl->dev));
70 log_debug("VG data on %s: %s %s %" PRIu32 " %" PRIu32
71 " %" PRIu32 " %" PRIu32 " %" PRIu32 " %"
72 PRIu32 " %" PRIu32 " %" PRIu32 " %" PRIu32
73 " %" PRIu32 " %" PRIu32 " %" PRIu32 " %"
74 PRIu32 " %" PRIu32 " %" PRIu32,
75 dev_name(first->dev), first->vgd.vg_uuid,
76 first->vgd.vg_name_dummy,
77 first->vgd.vg_number, first->vgd.vg_access,
78 first->vgd.vg_status, first->vgd.lv_max,
79 first->vgd.lv_cur, first->vgd.lv_open,
80 first->vgd.pv_max, first->vgd.pv_cur,
81 first->vgd.pv_act, first->vgd.dummy,
82 first->vgd.vgda, first->vgd.pe_size,
83 first->vgd.pe_total, first->vgd.pe_allocated,
84 first->vgd.pvg_total);
85 log_debug("VG data on %s: %s %s %" PRIu32 " %" PRIu32
86 " %" PRIu32 " %" PRIu32 " %" PRIu32 " %"
87 PRIu32 " %" PRIu32 " %" PRIu32 " %" PRIu32
88 " %" PRIu32 " %" PRIu32 " %" PRIu32 " %"
89 PRIu32 " %" PRIu32 " %" PRIu32,
90 dev_name(dl->dev), dl->vgd.vg_uuid,
91 dl->vgd.vg_name_dummy, dl->vgd.vg_number,
92 dl->vgd.vg_access, dl->vgd.vg_status,
93 dl->vgd.lv_max, dl->vgd.lv_cur,
94 dl->vgd.lv_open, dl->vgd.pv_max,
95 dl->vgd.pv_cur, dl->vgd.pv_act, dl->vgd.dummy,
96 dl->vgd.vgda, dl->vgd.pe_size,
97 dl->vgd.pe_total, dl->vgd.pe_allocated,
105 /* On entry to fn, list known to be non-empty */
106 if (pv_count != first->vgd.pv_cur) {
107 log_error("%d PV(s) found for VG %s: expected %d",
108 pv_count, first->pvd.vg_name, first->vgd.pv_cur);
109 vg->status |= PARTIAL_VG;
115 static int _fix_partial_vg(struct volume_group *vg, struct dm_list *pvs)
117 uint32_t extent_count = 0;
118 struct disk_list *dl;
122 struct lv_segment *seg;
125 * FIXME: code should remap missing segments to error segment.
126 * Also current mapping code allocates 1 segment per missing extent.
127 * For now bail out completely - allocated structures are not complete
129 dm_list_iterate_items(ll, &vg->lvs)
130 dm_list_iterate_items(seg, &ll->lv->segments) {
132 /* area_count is always 1 here, s == 0 */
133 if (seg_type(seg, 0) != AREA_PV)
139 log_error("Partial mode support for missing lvm1 PVs and "
140 "partially available LVs is currently not implemented.");
144 dm_list_iterate(pvh, pvs) {
145 dl = dm_list_item(pvh, struct disk_list);
146 extent_count += dl->pvd.pe_total;
149 /* FIXME: move this to one place to pv_manip */
150 if (!(pvl = dm_pool_zalloc(vg->vgmem, sizeof(*pvl))) ||
151 !(pvl->pv = dm_pool_zalloc(vg->vgmem, sizeof(*pvl->pv))))
154 /* Use vg uuid with replaced first chars to "missing" as missing PV UUID */
155 memcpy(&pvl->pv->id.uuid, vg->id.uuid, sizeof(pvl->pv->id.uuid));
156 memcpy(&pvl->pv->id.uuid, "missing", 7);
158 if (!(pvl->pv->vg_name = dm_pool_strdup(vg->vgmem, vg->name)))
160 memcpy(&pvl->pv->vgid, &vg->id, sizeof(vg->id));
161 pvl->pv->status |= MISSING_PV;
162 dm_list_init(&pvl->pv->tags);
163 dm_list_init(&pvl->pv->segments);
165 pvl->pv->pe_size = vg->extent_size;
166 pvl->pv->pe_count = vg->extent_count - extent_count;
167 if (!alloc_pv_segment_whole_pv(vg->vgmem, pvl->pv))
170 add_pvl_to_vgs(vg, pvl);
171 log_debug("%s: partial VG, allocated missing PV using %d extents.",
172 vg->name, pvl->pv->pe_count);
176 dm_pool_free(vg->vgmem, pvl);
180 static struct volume_group *_build_vg(struct format_instance *fid,
184 struct volume_group *vg = dm_pool_zalloc(mem, sizeof(*vg));
185 struct disk_list *dl;
190 if (dm_list_empty(pvs))
193 vg->cmd = fid->fmt->cmd;
197 dm_list_init(&vg->pvs);
198 dm_list_init(&vg->lvs);
199 dm_list_init(&vg->tags);
200 dm_list_init(&vg->removed_pvs);
202 if (!_check_vgs(pvs, vg))
205 dl = dm_list_item(pvs->n, struct disk_list);
207 if (!import_vg(mem, vg, dl))
210 if (!import_pvs(fid->fmt, mem, vg, pvs))
213 if (!import_lvs(mem, vg, pvs))
216 if (!import_extents(fid->fmt->cmd, vg, pvs))
219 if (!import_snapshots(mem, vg, pvs))
222 /* Fix extents counts by adding missing PV if partial VG */
223 if ((vg->status & PARTIAL_VG) && !_fix_partial_vg(vg, pvs))
229 dm_pool_free(mem, vg);
233 static struct volume_group *_format1_vg_read(struct format_instance *fid,
235 struct metadata_area *mda __attribute__((unused)))
237 struct dm_pool *mem = dm_pool_create("lvm1 vg_read", VG_MEMPOOL_CHUNK);
239 struct volume_group *vg = NULL;
245 /* Strip dev_dir if present */
246 vg_name = strip_dir(vg_name, fid->fmt->cmd->dev_dir);
249 (fid->fmt, vg_name, fid->fmt->cmd->filter, mem, &pvs))
252 if (!(vg = _build_vg(fid, &pvs, mem)))
257 dm_pool_destroy(mem);
261 static struct disk_list *_flatten_pv(struct format_instance *fid,
262 struct dm_pool *mem, struct volume_group *vg,
263 struct physical_volume *pv,
266 struct disk_list *dl = dm_pool_alloc(mem, sizeof(*dl));
274 dm_list_init(&dl->uuids);
275 dm_list_init(&dl->lvds);
277 if (!export_pv(fid->fmt->cmd, mem, vg, &dl->pvd, pv) ||
278 !export_vg(&dl->vgd, vg) ||
279 !export_uuids(dl, vg) ||
280 !export_lvs(dl, vg, pv, dev_dir) || !calculate_layout(dl)) {
281 dm_pool_free(mem, dl);
288 static int _flatten_vg(struct format_instance *fid, struct dm_pool *mem,
289 struct volume_group *vg,
290 struct dm_list *pvds, const char *dev_dir,
291 struct dev_filter *filter)
294 struct disk_list *data;
296 dm_list_iterate_items(pvl, &vg->pvs) {
297 if (!(data = _flatten_pv(fid, mem, vg, pvl->pv, dev_dir)))
300 dm_list_add(pvds, &data->list);
303 export_numbers(pvds, vg);
306 if (!export_vg_number(fid, pvds, vg->name, filter))
312 static int _format1_vg_write(struct format_instance *fid, struct volume_group *vg,
313 struct metadata_area *mda __attribute__((unused)))
315 struct dm_pool *mem = dm_pool_create("lvm1 vg_write", VG_MEMPOOL_CHUNK);
324 r = (_flatten_vg(fid, mem, vg, &pvds, fid->fmt->cmd->dev_dir,
325 fid->fmt->cmd->filter) &&
326 write_disks(fid->fmt, &pvds));
328 lvmcache_update_vg(vg, 0);
329 dm_pool_destroy(mem);
333 static int _format1_pv_read(const struct format_type *fmt, const char *pv_name,
334 struct physical_volume *pv, struct dm_list *mdas __attribute__((unused)),
335 int scan_label_only __attribute__((unused)))
337 struct dm_pool *mem = dm_pool_create("lvm1 pv_read", 1024);
338 struct disk_list *dl;
342 log_very_verbose("Reading physical volume data %s from disk", pv_name);
347 if (!(dev = dev_cache_get(pv_name, fmt->cmd->filter)))
350 if (!(dl = read_disk(fmt, dev, mem, NULL)))
353 if (!import_pv(fmt, fmt->cmd->mem, dl->dev, NULL, pv, &dl->pvd, &dl->vgd))
361 dm_pool_destroy(mem);
365 static int _format1_pv_setup(const struct format_type *fmt,
366 uint64_t pe_start, uint32_t extent_count,
367 uint32_t extent_size,
368 unsigned long data_alignment __attribute__((unused)),
369 unsigned long data_alignment_offset __attribute__((unused)),
370 int pvmetadatacopies __attribute__((unused)),
371 uint64_t pvmetadatasize __attribute__((unused)),
372 unsigned metadataignore __attribute__((unused)),
373 struct dm_list *mdas __attribute__((unused)),
374 struct physical_volume *pv, struct volume_group *vg __attribute__((unused)))
376 if (pv->size > MAX_PV_SIZE)
378 if (pv->size > MAX_PV_SIZE) {
379 log_error("Physical volumes cannot be bigger than %s",
380 display_size(fmt->cmd, (uint64_t) MAX_PV_SIZE));
384 /* Nothing more to do if extent size isn't provided */
389 * This works out pe_start and pe_count.
391 if (!calculate_extent_count(pv, extent_size, extent_count, pe_start))
394 /* Retain existing extent locations exactly */
395 if (((pe_start || extent_count) && (pe_start != pv->pe_start)) ||
396 (extent_count && (extent_count != pv->pe_count))) {
397 log_error("Metadata would overwrite physical extents");
404 static int _format1_lv_setup(struct format_instance *fid, struct logical_volume *lv)
406 uint64_t max_size = UINT_MAX;
409 lvid_from_lvnum(&lv->lvid, &lv->vg->id, find_free_lvnum(lv));
411 if (lv->le_count > MAX_LE_TOTAL) {
412 log_error("logical volumes cannot contain more than "
413 "%d extents.", MAX_LE_TOTAL);
416 if (lv->size > max_size) {
417 log_error("logical volumes cannot be larger than %s",
418 display_size(fid->fmt->cmd, max_size));
425 static int _format1_pv_write(const struct format_type *fmt, struct physical_volume *pv,
426 struct dm_list *mdas __attribute__((unused)), int64_t sector __attribute__((unused)))
429 struct disk_list *dl;
431 struct lvmcache_info *info;
433 if (!(info = lvmcache_add(fmt->labeller, (char *) &pv->id, pv->dev,
434 pv->vg_name, NULL, 0)))
437 info->device_size = pv->size << SECTOR_SHIFT;
440 dm_list_init(&info->mdas);
444 /* Ensure any residual PE structure is gone */
445 pv->pe_size = pv->pe_count = 0;
446 pv->pe_start = LVM1_PE_ALIGN;
448 if (!(mem = dm_pool_create("lvm1 pv_write", 1024)))
451 if (!(dl = dm_pool_alloc(mem, sizeof(*dl))))
457 if (!export_pv(fmt->cmd, mem, NULL, &dl->pvd, pv))
460 /* must be set to be able to zero gap after PV structure in
461 dev_write in order to make other disk tools happy */
462 dl->pvd.pv_on_disk.base = METADATA_BASE;
463 dl->pvd.pv_on_disk.size = PV_SIZE;
464 dl->pvd.pe_on_disk.base = LVM1_PE_ALIGN << SECTOR_SHIFT;
466 dm_list_add(&pvs, &dl->list);
467 if (!write_disks(fmt, &pvs))
470 dm_pool_destroy(mem);
474 dm_pool_destroy(mem);
478 static int _format1_vg_setup(struct format_instance *fid, struct volume_group *vg)
480 /* just check max_pv and max_lv */
481 if (!vg->max_lv || vg->max_lv >= MAX_LV)
482 vg->max_lv = MAX_LV - 1;
484 if (!vg->max_pv || vg->max_pv >= MAX_PV)
485 vg->max_pv = MAX_PV - 1;
487 if (vg->extent_size > MAX_PE_SIZE || vg->extent_size < MIN_PE_SIZE) {
488 log_error("Extent size must be between %s and %s",
489 display_size(fid->fmt->cmd, (uint64_t) MIN_PE_SIZE),
490 display_size(fid->fmt->cmd, (uint64_t) MAX_PE_SIZE));
495 if (vg->extent_size % MIN_PE_SIZE) {
496 log_error("Extent size must be multiple of %s",
497 display_size(fid->fmt->cmd, (uint64_t) MIN_PE_SIZE));
502 if (vg->extent_size & (vg->extent_size - 1)) {
503 log_error("Extent size must be power of 2");
510 static int _format1_segtype_supported(struct format_instance *fid __attribute__((unused)),
511 const struct segment_type *segtype)
513 if (!(segtype->flags & SEG_FORMAT1_SUPPORT))
519 static struct metadata_area_ops _metadata_format1_ops = {
520 .vg_read = _format1_vg_read,
521 .vg_write = _format1_vg_write,
524 static struct format_instance *_format1_create_instance(const struct format_type *fmt,
525 const char *vgname __attribute__((unused)),
526 const char *vgid __attribute__((unused)),
527 void *private __attribute__((unused)))
529 struct format_instance *fid;
530 struct metadata_area *mda;
532 if (!(fid = dm_pool_alloc(fmt->cmd->mem, sizeof(*fid))))
536 dm_list_init(&fid->metadata_areas_in_use);
537 dm_list_init(&fid->metadata_areas_ignored);
539 /* Define a NULL metadata area */
540 if (!(mda = dm_pool_zalloc(fmt->cmd->mem, sizeof(*mda)))) {
541 dm_pool_free(fmt->cmd->mem, fid);
545 mda->ops = &_metadata_format1_ops;
546 mda->metadata_locn = NULL;
548 dm_list_add(&fid->metadata_areas_in_use, &mda->list);
553 static void _format1_destroy_instance(struct format_instance *fid __attribute__((unused)))
557 static void _format1_destroy(struct format_type *fmt)
562 static struct format_handler _format1_ops = {
563 .pv_read = _format1_pv_read,
564 .pv_setup = _format1_pv_setup,
565 .pv_write = _format1_pv_write,
566 .lv_setup = _format1_lv_setup,
567 .vg_setup = _format1_vg_setup,
568 .segtype_supported = _format1_segtype_supported,
569 .create_instance = _format1_create_instance,
570 .destroy_instance = _format1_destroy_instance,
571 .destroy = _format1_destroy,
575 struct format_type *init_lvm1_format(struct cmd_context *cmd)
577 struct format_type *init_format(struct cmd_context *cmd);
578 struct format_type *init_format(struct cmd_context *cmd)
581 struct format_type *fmt = dm_malloc(sizeof(*fmt));
587 fmt->ops = &_format1_ops;
588 fmt->name = FMT_LVM1_NAME;
590 fmt->orphan_vg_name = FMT_LVM1_ORPHAN_VG_NAME;
591 fmt->features = FMT_RESTRICTED_LVIDS | FMT_ORPHAN_ALLOCATABLE |
592 FMT_RESTRICTED_READAHEAD;
595 if (!(fmt->labeller = lvm1_labeller_create(fmt))) {
596 log_error("Couldn't create lvm1 label handler.");
600 if (!(label_register_handler(FMT_LVM1_NAME, fmt->labeller))) {
601 log_error("Couldn't register lvm1 label handler.");
605 log_very_verbose("Initialised format: %s", fmt->name);