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
18 #include "import-export.h"
20 #include "toolcontext.h"
25 #include "text_import.h"
28 typedef int (*section_fn) (struct format_instance * fid, struct dm_pool * mem,
29 struct volume_group * vg, const struct config_node * pvn,
30 const struct config_node * vgn,
31 struct dm_hash_table * pv_hash,
32 struct dm_hash_table * lv_hash,
33 unsigned *scan_done_once,
34 unsigned report_missing_devices);
36 #define _read_int32(root, path, result) \
37 get_config_uint32(root, path, (uint32_t *) result)
39 #define _read_uint32(root, path, result) \
40 get_config_uint32(root, path, result)
42 #define _read_int64(root, path, result) \
43 get_config_uint64(root, path, result)
46 * Logs an attempt to read an invalid format file.
48 static void _invalid_format(const char *str)
50 log_error("Can't process text format file - %s.", str);
54 * Checks that the config file contains vg metadata, and that it
55 * we recognise the version number,
57 static int _check_version(const struct config_tree *cft)
59 const struct config_node *cn;
60 const struct config_value *cv;
63 * Check the contents field.
65 if (!(cn = find_config_node(cft->root, CONTENTS_FIELD))) {
66 _invalid_format("missing contents field");
71 if (!cv || cv->type != CFG_STRING || strcmp(cv->v.str, CONTENTS_VALUE)) {
72 _invalid_format("unrecognised contents field");
77 * Check the version number.
79 if (!(cn = find_config_node(cft->root, FORMAT_VERSION_FIELD))) {
80 _invalid_format("missing version number");
85 if (!cv || cv->type != CFG_INT || cv->v.i != FORMAT_VERSION_VALUE) {
86 _invalid_format("unrecognised version number");
93 static int _is_converting(struct logical_volume *lv)
95 struct lv_segment *seg;
97 if (lv->status & MIRRORED) {
99 /* Can't use is_temporary_mirror() because the metadata for
100 * seg_lv may not be read in and flags may not be set yet. */
101 if (seg_type(seg, 0) == AREA_LV &&
102 strstr(seg_lv(seg, 0)->name, MIRROR_SYNC_LAYER))
109 static int _read_id(struct id *id, const struct config_node *cn, const char *path)
111 const struct config_value *cv;
113 if (!(cn = find_config_node(cn, path))) {
114 log_error("Couldn't find uuid.");
119 if (!cv || !cv->v.str) {
120 log_error("uuid must be a string.");
124 if (!id_read_format(id, cv->v.str)) {
125 log_error("Invalid uuid.");
132 static int _read_flag_config(const struct config_node *n, uint64_t *status, int type)
134 const struct config_node *cn;
137 if (!(cn = find_config_node(n, "status"))) {
138 log_error("Could not find status flags.");
142 if (!(read_flags(status, type | STATUS_FLAG, cn->v))) {
143 log_error("Could not read status flags.");
147 if ((cn = find_config_node(n, "flags"))) {
148 if (!(read_flags(status, type, cn->v))) {
149 log_error("Could not read flags.");
157 static int _read_pv(struct format_instance *fid, struct dm_pool *mem,
158 struct volume_group *vg, const struct config_node *pvn,
159 const struct config_node *vgn __attribute__((unused)),
160 struct dm_hash_table *pv_hash,
161 struct dm_hash_table *lv_hash __attribute__((unused)),
162 unsigned *scan_done_once,
163 unsigned report_missing_devices)
165 struct physical_volume *pv;
167 const struct config_node *cn;
170 if (!(pvl = dm_pool_zalloc(mem, sizeof(*pvl))) ||
171 !(pvl->pv = dm_pool_zalloc(mem, sizeof(*pvl->pv))))
177 * Add the pv to the pv hash for quick lookup when we read
180 if (!dm_hash_insert(pv_hash, pvn->key, pv))
183 if (!(pvn = pvn->child)) {
184 log_error("Empty pv section.");
188 if (!_read_id(&pv->id, pvn, "id")) {
189 log_error("Couldn't read uuid for physical volume.");
194 * Convert the uuid into a device.
196 if (!(pv->dev = device_from_pvid(fid->fmt->cmd, &pv->id, scan_done_once))) {
197 char buffer[64] __attribute__((aligned(8)));
199 if (!id_write_format(&pv->id, buffer, sizeof(buffer)))
201 if (report_missing_devices)
202 log_error_once("Couldn't find device with uuid %s.", buffer);
204 log_very_verbose("Couldn't find device with uuid %s.", buffer);
207 if (!(pv->vg_name = dm_pool_strdup(mem, vg->name)))
210 memcpy(&pv->vgid, &vg->id, sizeof(vg->id));
212 if (!_read_flag_config(pvn, &pv->status, PV_FLAGS)) {
213 log_error("Couldn't read status flags for physical volume.");
218 pv->status |= MISSING_PV;
221 _read_int64(pvn, "dev_size", &pv->size);
223 if (!_read_int64(pvn, "pe_start", &pv->pe_start)) {
224 log_error("Couldn't read extent size for physical volume.");
228 if (!_read_int32(pvn, "pe_count", &pv->pe_count)) {
229 log_error("Couldn't find extent count (pe_count) for "
234 dm_list_init(&pv->tags);
235 dm_list_init(&pv->segments);
238 if ((cn = find_config_node(pvn, "tags")) &&
239 !(read_tags(mem, &pv->tags, cn->v))) {
240 log_error("Couldn't read tags for physical volume %s in %s.",
241 pv_dev_name(pv), vg->name);
245 pv->pe_size = vg->extent_size;
247 pv->pe_alloc_count = 0;
251 /* Fix up pv size if missing or impossibly large */
252 if ((!pv->size || pv->size > (1ULL << 62)) && pv->dev) {
253 if (!dev_get_size(pv->dev, &pv->size)) {
254 log_error("%s: Couldn't get size.", pv_dev_name(pv));
257 log_verbose("Fixing up missing size (%s) "
258 "for PV %s", display_size(fid->fmt->cmd, pv->size),
261 size = pv->pe_count * (uint64_t) vg->extent_size +
264 log_warn("WARNING: Physical Volume %s is too "
265 "large for underlying device",
270 if (!alloc_pv_segment_whole_pv(mem, pv))
273 vg->extent_count += pv->pe_count;
274 vg->free_count += pv->pe_count;
275 add_pvl_to_vgs(vg, pvl);
280 static void _insert_segment(struct logical_volume *lv, struct lv_segment *seg)
282 struct lv_segment *comp;
284 dm_list_iterate_items(comp, &lv->segments) {
285 if (comp->le > seg->le) {
286 dm_list_add(&comp->list, &seg->list);
291 lv->le_count += seg->len;
292 dm_list_add(&lv->segments, &seg->list);
295 static int _read_segment(struct dm_pool *mem, struct volume_group *vg,
296 struct logical_volume *lv, const struct config_node *sn,
297 struct dm_hash_table *pv_hash)
299 uint32_t area_count = 0u;
300 struct lv_segment *seg;
301 const struct config_node *cn, *sn_child = sn->child;
302 const struct config_value *cv;
303 uint32_t start_extent, extent_count;
304 struct segment_type *segtype;
305 const char *segtype_str;
308 log_error("Empty segment section.");
312 if (!_read_int32(sn_child, "start_extent", &start_extent)) {
313 log_error("Couldn't read 'start_extent' for segment '%s' "
314 "of logical volume %s.", sn->key, lv->name);
318 if (!_read_int32(sn_child, "extent_count", &extent_count)) {
319 log_error("Couldn't read 'extent_count' for segment '%s' "
320 "of logical volume %s.", sn->key, lv->name);
324 segtype_str = "striped";
326 if ((cn = find_config_node(sn_child, "type"))) {
328 if (!cv || !cv->v.str) {
329 log_error("Segment type must be a string.");
332 segtype_str = cv->v.str;
335 if (!(segtype = get_segtype_from_string(vg->cmd, segtype_str)))
338 if (segtype->ops->text_import_area_count &&
339 !segtype->ops->text_import_area_count(sn_child, &area_count))
342 if (!(seg = alloc_lv_segment(mem, segtype, lv, start_extent,
343 extent_count, 0, 0, NULL, area_count,
344 extent_count, 0, 0, 0, NULL))) {
345 log_error("Segment allocation failed");
349 if (seg->segtype->ops->text_import &&
350 !seg->segtype->ops->text_import(seg, sn_child, pv_hash))
354 if ((cn = find_config_node(sn_child, "tags")) &&
355 !(read_tags(mem, &seg->tags, cn->v))) {
356 log_error("Couldn't read tags for a segment of %s/%s.",
362 * Insert into correct part of segment list.
364 _insert_segment(lv, seg);
366 if (seg_is_mirrored(seg))
367 lv->status |= MIRRORED;
369 if (seg_is_virtual(seg))
370 lv->status |= VIRTUAL;
372 if (_is_converting(lv))
373 lv->status |= CONVERTING;
378 int text_import_areas(struct lv_segment *seg, const struct config_node *sn,
379 const struct config_node *cn, struct dm_hash_table *pv_hash,
383 const struct config_value *cv;
384 struct logical_volume *lv1;
385 struct physical_volume *pv;
386 const char *seg_name = config_parent_name(sn);
388 if (!seg->area_count) {
389 log_error("Zero areas not allowed for segment %s", seg_name);
393 for (cv = cn->v, s = 0; cv && s < seg->area_count; s++, cv = cv->next) {
395 /* first we read the pv */
396 if (cv->type != CFG_STRING) {
397 log_error("Bad volume name in areas array for segment %s.", seg_name);
402 log_error("Missing offset in areas array for segment %s.", seg_name);
406 if (cv->next->type != CFG_INT) {
407 log_error("Bad offset in areas array for segment %s.", seg_name);
411 /* FIXME Cope if LV not yet read in */
412 if ((pv = dm_hash_lookup(pv_hash, cv->v.str))) {
413 if (!set_lv_segment_area_pv(seg, s, pv, (uint32_t) cv->next->v.i))
415 } else if ((lv1 = find_lv(seg->lv->vg, cv->v.str))) {
416 if (!set_lv_segment_area_lv(seg, s, lv1,
417 (uint32_t) cv->next->v.i,
421 log_error("Couldn't find volume '%s' "
423 cv->v.str ? : "NULL", seg_name);
431 * Check we read the correct number of stripes.
433 if (cv || (s < seg->area_count)) {
434 log_error("Incorrect number of areas in area array "
435 "for segment '%s'.", seg_name);
442 static int _read_segments(struct dm_pool *mem, struct volume_group *vg,
443 struct logical_volume *lv, const struct config_node *lvn,
444 struct dm_hash_table *pv_hash)
446 const struct config_node *sn;
447 int count = 0, seg_count;
449 for (sn = lvn; sn; sn = sn->sib) {
452 * All sub-sections are assumed to be segments.
455 if (!_read_segment(mem, vg, lv, sn, pv_hash))
460 /* FIXME Remove this restriction */
461 if ((lv->status & SNAPSHOT) && count > 1) {
462 log_error("Only one segment permitted for snapshot");
467 if (!_read_int32(lvn, "segment_count", &seg_count)) {
468 log_error("Couldn't read segment count for logical volume %s.",
473 if (seg_count != count) {
474 log_error("segment_count and actual number of segments "
475 "disagree for logical volume %s.", lv->name);
480 * Check there are no gaps or overlaps in the lv.
482 if (!check_lv_segments(lv, 0))
486 * Merge segments in case someones been editing things by hand.
488 if (!lv_merge_segments(lv))
494 static int _read_lvnames(struct format_instance *fid __attribute__((unused)),
496 struct volume_group *vg, const struct config_node *lvn,
497 const struct config_node *vgn __attribute__((unused)),
498 struct dm_hash_table *pv_hash __attribute__((unused)),
499 struct dm_hash_table *lv_hash,
500 unsigned *scan_done_once __attribute__((unused)),
501 unsigned report_missing_devices __attribute__((unused)))
503 struct logical_volume *lv;
504 const struct config_node *cn;
506 if (!(lv = alloc_lv(mem)))
509 if (!(lv->name = dm_pool_strdup(mem, lvn->key)))
512 if (!(lvn = lvn->child)) {
513 log_error("Empty logical volume section.");
517 if (!_read_flag_config(lvn, &lv->status, LV_FLAGS)) {
518 log_error("Couldn't read status flags for logical volume %s.",
523 lv->alloc = ALLOC_INHERIT;
524 if ((cn = find_config_node(lvn, "allocation_policy"))) {
525 const struct config_value *cv = cn->v;
526 if (!cv || !cv->v.str) {
527 log_error("allocation_policy must be a string.");
531 lv->alloc = get_alloc_from_string(cv->v.str);
532 if (lv->alloc == ALLOC_INVALID) {
533 log_warn("WARNING: Ignoring unrecognised allocation policy %s for LV %s", cv->v.str, lv->name);
534 lv->alloc = ALLOC_INHERIT;
538 if (!_read_int32(lvn, "read_ahead", &lv->read_ahead))
539 /* If not present, choice of auto or none is configurable */
540 lv->read_ahead = vg->cmd->default_settings.read_ahead;
542 switch (lv->read_ahead) {
544 lv->read_ahead = DM_READ_AHEAD_AUTO;
547 lv->read_ahead = DM_READ_AHEAD_NONE;
555 if ((cn = find_config_node(lvn, "tags")) &&
556 !(read_tags(mem, &lv->tags, cn->v))) {
557 log_error("Couldn't read tags for logical volume %s/%s.",
562 if (!dm_hash_insert(lv_hash, lv->name, lv))
565 return link_lv_to_vg(vg, lv);
568 static int _read_lvsegs(struct format_instance *fid __attribute__((unused)),
570 struct volume_group *vg, const struct config_node *lvn,
571 const struct config_node *vgn __attribute__((unused)),
572 struct dm_hash_table *pv_hash,
573 struct dm_hash_table *lv_hash,
574 unsigned *scan_done_once __attribute__((unused)),
575 unsigned report_missing_devices __attribute__((unused)))
577 struct logical_volume *lv;
579 if (!(lv = dm_hash_lookup(lv_hash, lvn->key))) {
580 log_error("Lost logical volume reference %s", lvn->key);
584 if (!(lvn = lvn->child)) {
585 log_error("Empty logical volume section.");
589 /* FIXME: read full lvid */
590 if (!_read_id(&lv->lvid.id[1], lvn, "id")) {
591 log_error("Couldn't read uuid for logical volume %s.",
596 memcpy(&lv->lvid.id[0], &lv->vg->id, sizeof(lv->lvid.id[0]));
598 if (!_read_segments(mem, vg, lv, lvn, pv_hash))
601 lv->size = (uint64_t) lv->le_count * (uint64_t) vg->extent_size;
604 if ((lv->status & FIXED_MINOR) &&
605 !_read_int32(lvn, "minor", &lv->minor)) {
606 log_error("Couldn't read minor number for logical "
607 "volume %s.", lv->name);
612 if ((lv->status & FIXED_MINOR) &&
613 !_read_int32(lvn, "major", &lv->major)) {
614 log_error("Couldn't read major number for logical "
615 "volume %s.", lv->name);
621 static int _read_sections(struct format_instance *fid,
622 const char *section, section_fn fn,
624 struct volume_group *vg, const struct config_node *vgn,
625 struct dm_hash_table *pv_hash,
626 struct dm_hash_table *lv_hash,
628 unsigned *scan_done_once)
630 const struct config_node *n;
631 /* Only report missing devices when doing a scan */
632 unsigned report_missing_devices = scan_done_once ? !*scan_done_once : 1;
634 if (!(n = find_config_node(vgn, section))) {
636 log_error("Couldn't find section '%s'.", section);
643 for (n = n->child; n; n = n->sib) {
644 if (!fn(fid, mem, vg, n, vgn, pv_hash, lv_hash,
645 scan_done_once, report_missing_devices))
652 static struct volume_group *_read_vg(struct format_instance *fid,
653 const struct config_tree *cft,
654 unsigned use_cached_pvs)
656 const struct config_node *vgn, *cn;
657 struct volume_group *vg;
658 struct dm_hash_table *pv_hash = NULL, *lv_hash = NULL;
659 struct dm_pool *mem = dm_pool_create("lvm2 vg_read", VG_MEMPOOL_CHUNK);
660 unsigned scan_done_once = use_cached_pvs;
665 /* skip any top-level values */
666 for (vgn = cft->root; (vgn && vgn->v); vgn = vgn->sib)
670 log_error("Couldn't find volume group in file.");
674 if (!(vg = dm_pool_zalloc(mem, sizeof(*vg))))
678 vg->cmd = fid->fmt->cmd;
680 /* FIXME Determine format type from file contents */
681 /* eg Set to instance of fmt1 here if reading a format1 backup? */
684 if (!(vg->name = dm_pool_strdup(mem, vgn->key)))
687 if (!(vg->system_id = dm_pool_zalloc(mem, NAME_LEN)))
692 if ((cn = find_config_node(vgn, "system_id")) && cn->v) {
694 log_error("system_id must be a string");
697 strncpy(vg->system_id, cn->v->v.str, NAME_LEN);
700 if (!_read_id(&vg->id, vgn, "id")) {
701 log_error("Couldn't read uuid for volume group %s.", vg->name);
705 if (!_read_int32(vgn, "seqno", &vg->seqno)) {
706 log_error("Couldn't read 'seqno' for volume group %s.",
711 if (!_read_flag_config(vgn, &vg->status, VG_FLAGS)) {
712 log_error("Error reading flags of volume group %s.",
717 if (!_read_int32(vgn, "extent_size", &vg->extent_size)) {
718 log_error("Couldn't read extent size for volume group %s.",
724 * 'extent_count' and 'free_count' get filled in
725 * implicitly when reading in the pv's and lv's.
728 if (!_read_int32(vgn, "max_lv", &vg->max_lv)) {
729 log_error("Couldn't read 'max_lv' for volume group %s.",
734 if (!_read_int32(vgn, "max_pv", &vg->max_pv)) {
735 log_error("Couldn't read 'max_pv' for volume group %s.",
740 vg->alloc = ALLOC_NORMAL;
741 if ((cn = find_config_node(vgn, "allocation_policy"))) {
742 const struct config_value *cv = cn->v;
743 if (!cv || !cv->v.str) {
744 log_error("allocation_policy must be a string.");
748 vg->alloc = get_alloc_from_string(cv->v.str);
749 if (vg->alloc == ALLOC_INVALID) {
750 log_warn("WARNING: Ignoring unrecognised allocation policy %s for VG %s", cv->v.str, vg->name);
751 vg->alloc = ALLOC_NORMAL;
755 if (!_read_uint32(vgn, "metadata_copies", &vg->mda_copies)) {
756 vg->mda_copies = DEFAULT_VGMETADATACOPIES;
760 * The pv hash memorises the pv section names -> pv
763 if (!(pv_hash = dm_hash_create(32))) {
764 log_error("Couldn't create hash table.");
768 dm_list_init(&vg->pvs);
769 if (!_read_sections(fid, "physical_volumes", _read_pv, mem, vg,
770 vgn, pv_hash, lv_hash, 0, &scan_done_once)) {
771 log_error("Couldn't find all physical volumes for volume "
772 "group %s.", vg->name);
776 dm_list_init(&vg->lvs);
777 dm_list_init(&vg->tags);
778 dm_list_init(&vg->removed_pvs);
781 if ((cn = find_config_node(vgn, "tags")) &&
782 !(read_tags(mem, &vg->tags, cn->v))) {
783 log_error("Couldn't read tags for volume group %s.", vg->name);
788 * The lv hash memorises the lv section names -> lv
791 if (!(lv_hash = dm_hash_create(32))) {
792 log_error("Couldn't create hash table.");
796 if (!_read_sections(fid, "logical_volumes", _read_lvnames, mem, vg,
797 vgn, pv_hash, lv_hash, 1, NULL)) {
798 log_error("Couldn't read all logical volume names for volume "
799 "group %s.", vg->name);
803 if (!_read_sections(fid, "logical_volumes", _read_lvsegs, mem, vg,
804 vgn, pv_hash, lv_hash, 1, NULL)) {
805 log_error("Couldn't read all logical volumes for "
806 "volume group %s.", vg->name);
810 if (!fixup_imported_mirrors(vg)) {
811 log_error("Failed to fixup mirror pointers after import for "
812 "volume group %s.", vg->name);
816 dm_hash_destroy(pv_hash);
817 dm_hash_destroy(lv_hash);
826 dm_hash_destroy(pv_hash);
829 dm_hash_destroy(lv_hash);
831 dm_pool_destroy(mem);
835 static void _read_desc(struct dm_pool *mem,
836 const struct config_tree *cft, time_t *when, char **desc)
842 old_suppress = log_suppress(1);
843 d = find_config_str(cft->root, "description", "");
844 log_suppress(old_suppress);
845 *desc = dm_pool_strdup(mem, d);
847 get_config_uint32(cft->root, "creation_time", &u);
851 static const char *_read_vgname(const struct format_type *fmt,
852 const struct config_tree *cft, struct id *vgid,
853 uint64_t *vgstatus, char **creation_host)
855 const struct config_node *vgn;
856 struct dm_pool *mem = fmt->cmd->mem;
860 old_suppress = log_suppress(2);
861 *creation_host = dm_pool_strdup(mem,
862 find_config_str(cft->root,
863 "creation_host", ""));
864 log_suppress(old_suppress);
866 /* skip any top-level values */
867 for (vgn = cft->root; (vgn && vgn->v); vgn = vgn->sib) ;
870 log_error("Couldn't find volume group in file.");
874 if (!(vgname = dm_pool_strdup(mem, vgn->key)))
879 if (!_read_id(vgid, vgn, "id")) {
880 log_error("Couldn't read uuid for volume group %s.", vgname);
884 if (!_read_flag_config(vgn, vgstatus, VG_FLAGS)) {
885 log_error("Couldn't find status flags for volume group %s.",
893 static struct text_vg_version_ops _vsn1_ops = {
894 .check_version = _check_version,
896 .read_desc = _read_desc,
897 .read_vgname = _read_vgname,
900 struct text_vg_version_ops *text_vg_vsn1_init(void)