4 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
5 * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
7 * This file is part of LVM2.
9 * This copyrighted material is made available to anyone wishing to use,
10 * modify, copy, or redistribute it subject to the terms and conditions
11 * of the GNU Lesser General Public License v.2.1.
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program; if not, write to the Free Software Foundation,
15 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include "import-export.h"
22 #include "toolcontext.h"
27 #include "text_import.h"
29 typedef int (*section_fn
) (struct format_instance
* fid
, struct dm_pool
* mem
,
30 struct volume_group
* vg
, struct config_node
* pvn
,
31 struct config_node
* vgn
,
32 struct dm_hash_table
* pv_hash
);
34 #define _read_int32(root, path, result) \
35 get_config_uint32(root, path, (uint32_t *) result)
37 #define _read_uint32(root, path, result) \
38 get_config_uint32(root, path, result)
40 #define _read_int64(root, path, result) \
41 get_config_uint64(root, path, result)
44 * Logs an attempt to read an invalid format file.
46 static void _invalid_format(const char *str
)
48 log_error("Can't process text format file - %s.", str
);
52 * Checks that the config file contains vg metadata, and that it
53 * we recognise the version number,
55 static int _check_version(struct config_tree
*cft
)
57 struct config_node
*cn
;
58 struct config_value
*cv
;
61 * Check the contents field.
63 if (!(cn
= find_config_node(cft
->root
, CONTENTS_FIELD
))) {
64 _invalid_format("missing contents field");
69 if (!cv
|| cv
->type
!= CFG_STRING
|| strcmp(cv
->v
.str
, CONTENTS_VALUE
)) {
70 _invalid_format("unrecognised contents field");
75 * Check the version number.
77 if (!(cn
= find_config_node(cft
->root
, FORMAT_VERSION_FIELD
))) {
78 _invalid_format("missing version number");
83 if (!cv
|| cv
->type
!= CFG_INT
|| cv
->v
.i
!= FORMAT_VERSION_VALUE
) {
84 _invalid_format("unrecognised version number");
91 static int _is_converting(struct logical_volume
*lv
)
93 struct lv_segment
*seg
;
95 if (lv
->status
& MIRRORED
) {
97 /* Can't use is_temporary_mirror() because the metadata for
98 * seg_lv may not be read in and flags may not be set yet. */
99 if (seg_type(seg
, 0) == AREA_LV
&&
100 strstr(seg_lv(seg
, 0)->name
, MIRROR_SYNC_LAYER
))
107 static int _read_id(struct id
*id
, struct config_node
*cn
, const char *path
)
109 struct config_value
*cv
;
111 if (!(cn
= find_config_node(cn
, path
))) {
112 log_error("Couldn't find uuid.");
117 if (!cv
|| !cv
->v
.str
) {
118 log_error("uuid must be a string.");
122 if (!id_read_format(id
, cv
->v
.str
)) {
123 log_error("Invalid uuid.");
130 static int _read_flag_config(struct config_node
*n
, uint32_t *status
, int type
)
132 struct config_node
*cn
;
135 if (!(cn
= find_config_node(n
, "status"))) {
136 log_error("Could not find status flags.");
140 if (!(read_flags(status
, type
| STATUS_FLAG
, cn
->v
))) {
141 log_error("Could not read status flags.");
145 if ((cn
= find_config_node(n
, "flags"))) {
146 if (!(read_flags(status
, type
, cn
->v
))) {
147 log_error("Could not read flags.");
155 static int _read_pv(struct format_instance
*fid
, struct dm_pool
*mem
,
156 struct volume_group
*vg
, struct config_node
*pvn
,
157 struct config_node
*vgn
__attribute((unused
)),
158 struct dm_hash_table
*pv_hash
)
160 struct physical_volume
*pv
;
162 struct config_node
*cn
;
165 if (!(pvl
= dm_pool_zalloc(mem
, sizeof(*pvl
))) ||
166 !(pvl
->pv
= dm_pool_zalloc(mem
, sizeof(*pvl
->pv
))))
172 * Add the pv to the pv hash for quick lookup when we read
175 if (!dm_hash_insert(pv_hash
, pvn
->key
, pv
))
178 if (!(pvn
= pvn
->child
)) {
179 log_error("Empty pv section.");
183 if (!_read_id(&pv
->id
, pvn
, "id")) {
184 log_error("Couldn't read uuid for physical volume.");
189 * Convert the uuid into a device.
191 if (!(pv
->dev
= device_from_pvid(fid
->fmt
->cmd
, &pv
->id
))) {
192 char buffer
[64] __attribute((aligned(8)));
194 if (!id_write_format(&pv
->id
, buffer
, sizeof(buffer
)))
195 log_error("Couldn't find device.");
197 log_error("Couldn't find device with uuid '%s'.",
201 if (!(pv
->vg_name
= dm_pool_strdup(mem
, vg
->name
)))
204 memcpy(&pv
->vgid
, &vg
->id
, sizeof(vg
->id
));
206 if (!_read_flag_config(pvn
, &pv
->status
, PV_FLAGS
)) {
207 log_error("Couldn't read status flags for physical volume.");
212 pv
->status
|= MISSING_PV
;
215 _read_int64(pvn
, "dev_size", &pv
->size
);
217 if (!_read_int64(pvn
, "pe_start", &pv
->pe_start
)) {
218 log_error("Couldn't read extent size for physical volume.");
222 if (!_read_int32(pvn
, "pe_count", &pv
->pe_count
)) {
223 log_error("Couldn't find extent count (pe_count) for "
228 dm_list_init(&pv
->tags
);
229 dm_list_init(&pv
->segments
);
232 if ((cn
= find_config_node(pvn
, "tags")) &&
233 !(read_tags(mem
, &pv
->tags
, cn
->v
))) {
234 log_error("Couldn't read tags for physical volume %s in %s.",
235 pv_dev_name(pv
), vg
->name
);
239 /* adjust the volume group. */
240 vg
->extent_count
+= pv
->pe_count
;
241 vg
->free_count
+= pv
->pe_count
;
243 pv
->pe_size
= vg
->extent_size
;
245 pv
->pe_alloc_count
= 0;
249 /* Fix up pv size if missing or impossibly large */
250 if ((!pv
->size
|| pv
->size
> (1ULL << 62)) && pv
->dev
) {
251 if (!dev_get_size(pv
->dev
, &pv
->size
)) {
252 log_error("%s: Couldn't get size.", pv_dev_name(pv
));
255 log_verbose("Fixing up missing size (%s) "
256 "for PV %s", display_size(fid
->fmt
->cmd
, pv
->size
),
259 size
= pv
->pe_count
* (uint64_t) vg
->extent_size
+
262 log_error("WARNING: Physical Volume %s is too "
263 "large for underlying device",
268 if (!alloc_pv_segment_whole_pv(mem
, pv
))
272 dm_list_add(&vg
->pvs
, &pvl
->list
);
277 static void _insert_segment(struct logical_volume
*lv
, struct lv_segment
*seg
)
279 struct lv_segment
*comp
;
281 dm_list_iterate_items(comp
, &lv
->segments
) {
282 if (comp
->le
> seg
->le
) {
283 dm_list_add(&comp
->list
, &seg
->list
);
288 lv
->le_count
+= seg
->len
;
289 dm_list_add(&lv
->segments
, &seg
->list
);
292 static int _read_segment(struct dm_pool
*mem
, struct volume_group
*vg
,
293 struct logical_volume
*lv
, struct config_node
*sn
,
294 struct dm_hash_table
*pv_hash
)
296 uint32_t area_count
= 0u;
297 struct lv_segment
*seg
;
298 struct config_node
*cn
, *sn_child
= sn
->child
;
299 struct config_value
*cv
;
300 uint32_t start_extent
, extent_count
;
301 struct segment_type
*segtype
;
302 const char *segtype_str
;
305 log_error("Empty segment section.");
309 if (!_read_int32(sn_child
, "start_extent", &start_extent
)) {
310 log_error("Couldn't read 'start_extent' for segment '%s' "
311 "of logical volume %s.", sn
->key
, lv
->name
);
315 if (!_read_int32(sn_child
, "extent_count", &extent_count
)) {
316 log_error("Couldn't read 'extent_count' for segment '%s' "
317 "of logical volume %s.", sn
->key
, lv
->name
);
321 segtype_str
= "striped";
323 if ((cn
= find_config_node(sn_child
, "type"))) {
325 if (!cv
|| !cv
->v
.str
) {
326 log_error("Segment type must be a string.");
329 segtype_str
= cv
->v
.str
;
332 if (!(segtype
= get_segtype_from_string(vg
->cmd
, segtype_str
)))
335 if (segtype
->ops
->text_import_area_count
&&
336 !segtype
->ops
->text_import_area_count(sn_child
, &area_count
))
339 if (!(seg
= alloc_lv_segment(mem
, segtype
, lv
, start_extent
,
340 extent_count
, 0, 0, NULL
, area_count
,
341 extent_count
, 0, 0, 0))) {
342 log_error("Segment allocation failed");
346 if (seg
->segtype
->ops
->text_import
&&
347 !seg
->segtype
->ops
->text_import(seg
, sn_child
, pv_hash
))
351 if ((cn
= find_config_node(sn_child
, "tags")) &&
352 !(read_tags(mem
, &seg
->tags
, cn
->v
))) {
353 log_error("Couldn't read tags for a segment of %s/%s.",
359 * Insert into correct part of segment list.
361 _insert_segment(lv
, seg
);
363 if (seg_is_mirrored(seg
))
364 lv
->status
|= MIRRORED
;
366 if (seg_is_virtual(seg
))
367 lv
->status
|= VIRTUAL
;
369 if (_is_converting(lv
))
370 lv
->status
|= CONVERTING
;
375 int text_import_areas(struct lv_segment
*seg
, const struct config_node
*sn
,
376 const struct config_node
*cn
, struct dm_hash_table
*pv_hash
,
380 struct config_value
*cv
;
381 struct logical_volume
*lv1
;
382 struct physical_volume
*pv
;
383 const char *seg_name
= config_parent_name(sn
);
385 if (!seg
->area_count
) {
386 log_error("Zero areas not allowed for segment %s", seg_name
);
390 for (cv
= cn
->v
, s
= 0; cv
&& s
< seg
->area_count
; s
++, cv
= cv
->next
) {
392 /* first we read the pv */
393 if (cv
->type
!= CFG_STRING
) {
394 log_error("Bad volume name in areas array for segment %s.", seg_name
);
399 log_error("Missing offset in areas array for segment %s.", seg_name
);
403 if (cv
->next
->type
!= CFG_INT
) {
404 log_error("Bad offset in areas array for segment %s.", seg_name
);
408 /* FIXME Cope if LV not yet read in */
409 if ((pv
= dm_hash_lookup(pv_hash
, cv
->v
.str
))) {
410 if (!set_lv_segment_area_pv(seg
, s
, pv
, (uint32_t) cv
->next
->v
.i
))
412 } else if ((lv1
= find_lv(seg
->lv
->vg
, cv
->v
.str
))) {
413 if (!set_lv_segment_area_lv(seg
, s
, lv1
,
414 (uint32_t) cv
->next
->v
.i
,
418 log_error("Couldn't find volume '%s' "
420 cv
->v
.str
? : "NULL", seg_name
);
428 * Check we read the correct number of stripes.
430 if (cv
|| (s
< seg
->area_count
)) {
431 log_error("Incorrect number of areas in area array "
432 "for segment '%s'.", seg_name
);
439 static int _read_segments(struct dm_pool
*mem
, struct volume_group
*vg
,
440 struct logical_volume
*lv
, struct config_node
*lvn
,
441 struct dm_hash_table
*pv_hash
)
443 struct config_node
*sn
;
444 int count
= 0, seg_count
;
446 for (sn
= lvn
; sn
; sn
= sn
->sib
) {
449 * All sub-sections are assumed to be segments.
452 if (!_read_segment(mem
, vg
, lv
, sn
, pv_hash
))
457 /* FIXME Remove this restriction */
458 if ((lv
->status
& SNAPSHOT
) && count
> 1) {
459 log_error("Only one segment permitted for snapshot");
464 if (!_read_int32(lvn
, "segment_count", &seg_count
)) {
465 log_error("Couldn't read segment count for logical volume %s.",
470 if (seg_count
!= count
) {
471 log_error("segment_count and actual number of segments "
472 "disagree for logical volume %s.", lv
->name
);
477 * Check there are no gaps or overlaps in the lv.
479 if (!check_lv_segments(lv
, 0))
483 * Merge segments in case someones been editing things by hand.
485 if (!lv_merge_segments(lv
))
491 static int _read_lvnames(struct format_instance
*fid
__attribute((unused
)),
493 struct volume_group
*vg
, struct config_node
*lvn
,
494 struct config_node
*vgn
__attribute((unused
)),
495 struct dm_hash_table
*pv_hash
__attribute((unused
)))
497 struct logical_volume
*lv
;
498 struct config_node
*cn
;
500 if (!(lv
= alloc_lv(mem
)))
503 if (!(lv
->name
= dm_pool_strdup(mem
, lvn
->key
)))
506 if (!(lvn
= lvn
->child
)) {
507 log_error("Empty logical volume section.");
511 if (!_read_flag_config(lvn
, &lv
->status
, LV_FLAGS
)) {
512 log_error("Couldn't read status flags for logical volume %s.",
517 lv
->alloc
= ALLOC_INHERIT
;
518 if ((cn
= find_config_node(lvn
, "allocation_policy"))) {
519 struct config_value
*cv
= cn
->v
;
520 if (!cv
|| !cv
->v
.str
) {
521 log_error("allocation_policy must be a string.");
525 lv
->alloc
= get_alloc_from_string(cv
->v
.str
);
526 if (lv
->alloc
== ALLOC_INVALID
)
530 if (!_read_int32(lvn
, "read_ahead", &lv
->read_ahead
))
531 /* If not present, choice of auto or none is configurable */
532 lv
->read_ahead
= vg
->cmd
->default_settings
.read_ahead
;
534 switch (lv
->read_ahead
) {
536 lv
->read_ahead
= DM_READ_AHEAD_AUTO
;
539 lv
->read_ahead
= DM_READ_AHEAD_NONE
;
547 if ((cn
= find_config_node(lvn
, "tags")) &&
548 !(read_tags(mem
, &lv
->tags
, cn
->v
))) {
549 log_error("Couldn't read tags for logical volume %s/%s.",
554 return link_lv_to_vg(vg
, lv
);
557 static int _read_lvsegs(struct format_instance
*fid
__attribute((unused
)),
559 struct volume_group
*vg
, struct config_node
*lvn
,
560 struct config_node
*vgn
__attribute((unused
)),
561 struct dm_hash_table
*pv_hash
)
563 struct logical_volume
*lv
;
566 if (!(lvl
= find_lv_in_vg(vg
, lvn
->key
))) {
567 log_error("Lost logical volume reference %s", lvn
->key
);
573 if (!(lvn
= lvn
->child
)) {
574 log_error("Empty logical volume section.");
578 /* FIXME: read full lvid */
579 if (!_read_id(&lv
->lvid
.id
[1], lvn
, "id")) {
580 log_error("Couldn't read uuid for logical volume %s.",
585 memcpy(&lv
->lvid
.id
[0], &lv
->vg
->id
, sizeof(lv
->lvid
.id
[0]));
587 if (!_read_segments(mem
, vg
, lv
, lvn
, pv_hash
))
590 lv
->size
= (uint64_t) lv
->le_count
* (uint64_t) vg
->extent_size
;
593 if ((lv
->status
& FIXED_MINOR
) &&
594 !_read_int32(lvn
, "minor", &lv
->minor
)) {
595 log_error("Couldn't read minor number for logical "
596 "volume %s.", lv
->name
);
601 if ((lv
->status
& FIXED_MINOR
) &&
602 !_read_int32(lvn
, "major", &lv
->major
)) {
603 log_error("Couldn't read major number for logical "
604 "volume %s.", lv
->name
);
610 static int _read_sections(struct format_instance
*fid
,
611 const char *section
, section_fn fn
,
613 struct volume_group
*vg
, struct config_node
*vgn
,
614 struct dm_hash_table
*pv_hash
, int optional
)
616 struct config_node
*n
;
618 if (!(n
= find_config_node(vgn
, section
))) {
620 log_error("Couldn't find section '%s'.", section
);
627 for (n
= n
->child
; n
; n
= n
->sib
) {
628 if (!fn(fid
, mem
, vg
, n
, vgn
, pv_hash
))
635 static struct volume_group
*_read_vg(struct format_instance
*fid
,
636 struct config_tree
*cft
)
638 struct config_node
*vgn
, *cn
;
639 struct volume_group
*vg
;
640 struct dm_hash_table
*pv_hash
= NULL
;
641 struct dm_pool
*mem
= dm_pool_create("lvm2 vg_read", VG_MEMPOOL_CHUNK
);
646 /* skip any top-level values */
647 for (vgn
= cft
->root
; (vgn
&& vgn
->v
); vgn
= vgn
->sib
) ;
650 log_error("Couldn't find volume group in file.");
654 if (!(vg
= dm_pool_zalloc(mem
, sizeof(*vg
))))
658 vg
->cmd
= fid
->fmt
->cmd
;
660 /* FIXME Determine format type from file contents */
661 /* eg Set to instance of fmt1 here if reading a format1 backup? */
664 if (!(vg
->name
= dm_pool_strdup(mem
, vgn
->key
)))
667 if (!(vg
->system_id
= dm_pool_zalloc(mem
, NAME_LEN
)))
672 if ((cn
= find_config_node(vgn
, "system_id")) && cn
->v
) {
674 log_error("system_id must be a string");
677 strncpy(vg
->system_id
, cn
->v
->v
.str
, NAME_LEN
);
680 if (!_read_id(&vg
->id
, vgn
, "id")) {
681 log_error("Couldn't read uuid for volume group %s.", vg
->name
);
685 if (!_read_int32(vgn
, "seqno", &vg
->seqno
)) {
686 log_error("Couldn't read 'seqno' for volume group %s.",
691 if (!_read_flag_config(vgn
, &vg
->status
, VG_FLAGS
)) {
692 log_error("Error reading flags of volume group %s.",
697 if (!_read_int32(vgn
, "extent_size", &vg
->extent_size
)) {
698 log_error("Couldn't read extent size for volume group %s.",
704 * 'extent_count' and 'free_count' get filled in
705 * implicitly when reading in the pv's and lv's.
708 if (!_read_int32(vgn
, "max_lv", &vg
->max_lv
)) {
709 log_error("Couldn't read 'max_lv' for volume group %s.",
714 if (!_read_int32(vgn
, "max_pv", &vg
->max_pv
)) {
715 log_error("Couldn't read 'max_pv' for volume group %s.",
720 vg
->alloc
= ALLOC_NORMAL
;
721 if ((cn
= find_config_node(vgn
, "allocation_policy"))) {
722 struct config_value
*cv
= cn
->v
;
723 if (!cv
|| !cv
->v
.str
) {
724 log_error("allocation_policy must be a string.");
728 vg
->alloc
= get_alloc_from_string(cv
->v
.str
);
729 if (vg
->alloc
== ALLOC_INVALID
)
734 * The pv hash memoises the pv section names -> pv
737 if (!(pv_hash
= dm_hash_create(32))) {
738 log_error("Couldn't create hash table.");
742 dm_list_init(&vg
->pvs
);
743 if (!_read_sections(fid
, "physical_volumes", _read_pv
, mem
, vg
,
745 log_error("Couldn't find all physical volumes for volume "
746 "group %s.", vg
->name
);
750 dm_list_init(&vg
->lvs
);
751 dm_list_init(&vg
->tags
);
752 dm_list_init(&vg
->removed_pvs
);
755 if ((cn
= find_config_node(vgn
, "tags")) &&
756 !(read_tags(mem
, &vg
->tags
, cn
->v
))) {
757 log_error("Couldn't read tags for volume group %s.", vg
->name
);
761 if (!_read_sections(fid
, "logical_volumes", _read_lvnames
, mem
, vg
,
763 log_error("Couldn't read all logical volume names for volume "
764 "group %s.", vg
->name
);
768 if (!_read_sections(fid
, "logical_volumes", _read_lvsegs
, mem
, vg
,
770 log_error("Couldn't read all logical volumes for "
771 "volume group %s.", vg
->name
);
775 if (!fixup_imported_mirrors(vg
)) {
776 log_error("Failed to fixup mirror pointers after import for "
777 "volume group %s.", vg
->name
);
781 dm_hash_destroy(pv_hash
);
790 dm_hash_destroy(pv_hash
);
792 dm_pool_destroy(mem
);
796 static void _read_desc(struct dm_pool
*mem
,
797 struct config_tree
*cft
, time_t *when
, char **desc
)
803 old_suppress
= log_suppress(1);
804 d
= find_config_str(cft
->root
, "description", "");
805 log_suppress(old_suppress
);
806 *desc
= dm_pool_strdup(mem
, d
);
808 get_config_uint32(cft
->root
, "creation_time", &u
);
812 static const char *_read_vgname(const struct format_type
*fmt
,
813 struct config_tree
*cft
, struct id
*vgid
,
814 uint32_t *vgstatus
, char **creation_host
)
816 struct config_node
*vgn
;
817 struct dm_pool
*mem
= fmt
->cmd
->mem
;
821 old_suppress
= log_suppress(2);
822 *creation_host
= dm_pool_strdup(mem
,
823 find_config_str(cft
->root
,
824 "creation_host", ""));
825 log_suppress(old_suppress
);
827 /* skip any top-level values */
828 for (vgn
= cft
->root
; (vgn
&& vgn
->v
); vgn
= vgn
->sib
) ;
831 log_error("Couldn't find volume group in file.");
835 if (!(vgname
= dm_pool_strdup(mem
, vgn
->key
)))
840 if (!_read_id(vgid
, vgn
, "id")) {
841 log_error("Couldn't read uuid for volume group %s.", vgname
);
845 if (!_read_flag_config(vgn
, vgstatus
, VG_FLAGS
)) {
846 log_error("Couldn't find status flags for volume group %s.",
854 static struct text_vg_version_ops _vsn1_ops
= {
855 .check_version
= _check_version
,
857 .read_desc
= _read_desc
,
858 .read_vgname
= _read_vgname
,
861 struct text_vg_version_ops
*text_vg_vsn1_init(void)