Tizen 2.1 base
[external/device-mapper.git] / lib / format_text / import_vsn1.c
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 "import-export.h"
19 #include "display.h"
20 #include "toolcontext.h"
21 #include "lvmcache.h"
22 #include "lv_alloc.h"
23 #include "pv_alloc.h"
24 #include "segtype.h"
25 #include "text_import.h"
26 #include "defaults.h"
27
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);
35
36 #define _read_int32(root, path, result) \
37         get_config_uint32(root, path, (uint32_t *) result)
38
39 #define _read_uint32(root, path, result) \
40         get_config_uint32(root, path, result)
41
42 #define _read_int64(root, path, result) \
43         get_config_uint64(root, path, result)
44
45 /*
46  * Logs an attempt to read an invalid format file.
47  */
48 static void _invalid_format(const char *str)
49 {
50         log_error("Can't process text format file - %s.", str);
51 }
52
53 /*
54  * Checks that the config file contains vg metadata, and that it
55  * we recognise the version number,
56  */
57 static int _check_version(const struct config_tree *cft)
58 {
59         const struct config_node *cn;
60         const struct config_value *cv;
61
62         /*
63          * Check the contents field.
64          */
65         if (!(cn = find_config_node(cft->root, CONTENTS_FIELD))) {
66                 _invalid_format("missing contents field");
67                 return 0;
68         }
69
70         cv = cn->v;
71         if (!cv || cv->type != CFG_STRING || strcmp(cv->v.str, CONTENTS_VALUE)) {
72                 _invalid_format("unrecognised contents field");
73                 return 0;
74         }
75
76         /*
77          * Check the version number.
78          */
79         if (!(cn = find_config_node(cft->root, FORMAT_VERSION_FIELD))) {
80                 _invalid_format("missing version number");
81                 return 0;
82         }
83
84         cv = cn->v;
85         if (!cv || cv->type != CFG_INT || cv->v.i != FORMAT_VERSION_VALUE) {
86                 _invalid_format("unrecognised version number");
87                 return 0;
88         }
89
90         return 1;
91 }
92
93 static int _is_converting(struct logical_volume *lv)
94 {
95         struct lv_segment *seg;
96
97         if (lv->status & MIRRORED) {
98                 seg = first_seg(lv);
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))
103                         return 1;
104         }
105
106         return 0;
107 }
108
109 static int _read_id(struct id *id, const struct config_node *cn, const char *path)
110 {
111         const struct config_value *cv;
112
113         if (!(cn = find_config_node(cn, path))) {
114                 log_error("Couldn't find uuid.");
115                 return 0;
116         }
117
118         cv = cn->v;
119         if (!cv || !cv->v.str) {
120                 log_error("uuid must be a string.");
121                 return 0;
122         }
123
124         if (!id_read_format(id, cv->v.str)) {
125                 log_error("Invalid uuid.");
126                 return 0;
127         }
128
129         return 1;
130 }
131
132 static int _read_flag_config(const struct config_node *n, uint64_t *status, int type)
133 {
134         const struct config_node *cn;
135         *status = 0;
136
137         if (!(cn = find_config_node(n, "status"))) {
138                 log_error("Could not find status flags.");
139                 return 0;
140         }
141
142         if (!(read_flags(status, type | STATUS_FLAG, cn->v))) {
143                 log_error("Could not read status flags.");
144                 return 0;
145         }
146
147         if ((cn = find_config_node(n, "flags"))) {
148                 if (!(read_flags(status, type, cn->v))) {
149                         log_error("Could not read flags.");
150                         return 0;
151                 }
152         }
153
154         return 1;
155 }
156
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)
164 {
165         struct physical_volume *pv;
166         struct pv_list *pvl;
167         const struct config_node *cn;
168         uint64_t size;
169
170         if (!(pvl = dm_pool_zalloc(mem, sizeof(*pvl))) ||
171             !(pvl->pv = dm_pool_zalloc(mem, sizeof(*pvl->pv))))
172                 return_0;
173
174         pv = pvl->pv;
175
176         /*
177          * Add the pv to the pv hash for quick lookup when we read
178          * the lv segments.
179          */
180         if (!dm_hash_insert(pv_hash, pvn->key, pv))
181                 return_0;
182
183         if (!(pvn = pvn->child)) {
184                 log_error("Empty pv section.");
185                 return 0;
186         }
187
188         if (!_read_id(&pv->id, pvn, "id")) {
189                 log_error("Couldn't read uuid for physical volume.");
190                 return 0;
191         }
192
193         /*
194          * Convert the uuid into a device.
195          */
196         if (!(pv->dev = device_from_pvid(fid->fmt->cmd, &pv->id, scan_done_once))) {
197                 char buffer[64] __attribute__((aligned(8)));
198
199                 if (!id_write_format(&pv->id, buffer, sizeof(buffer)))
200                         buffer[0] = '\0';
201                 if (report_missing_devices)
202                         log_error_once("Couldn't find device with uuid %s.", buffer);
203                 else
204                         log_very_verbose("Couldn't find device with uuid %s.", buffer);
205         }
206
207         if (!(pv->vg_name = dm_pool_strdup(mem, vg->name)))
208                 return_0;
209
210         memcpy(&pv->vgid, &vg->id, sizeof(vg->id));
211
212         if (!_read_flag_config(pvn, &pv->status, PV_FLAGS)) {
213                 log_error("Couldn't read status flags for physical volume.");
214                 return 0;
215         }
216
217         if (!pv->dev)
218                 pv->status |= MISSING_PV;
219
220         /* Late addition */
221         _read_int64(pvn, "dev_size", &pv->size);
222
223         if (!_read_int64(pvn, "pe_start", &pv->pe_start)) {
224                 log_error("Couldn't read extent size for physical volume.");
225                 return 0;
226         }
227
228         if (!_read_int32(pvn, "pe_count", &pv->pe_count)) {
229                 log_error("Couldn't find extent count (pe_count) for "
230                           "physical volume.");
231                 return 0;
232         }
233
234         dm_list_init(&pv->tags);
235         dm_list_init(&pv->segments);
236
237         /* Optional tags */
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);
242                 return 0;
243         }
244
245         pv->pe_size = vg->extent_size;
246
247         pv->pe_alloc_count = 0;
248         pv->pe_align = 0;
249         pv->fmt = fid->fmt;
250
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));
255                         return 0;
256                 }
257                 log_verbose("Fixing up missing size (%s) "
258                             "for PV %s", display_size(fid->fmt->cmd, pv->size),
259                             pv_dev_name(pv));
260                 if (vg) {
261                         size = pv->pe_count * (uint64_t) vg->extent_size +
262                                pv->pe_start;
263                         if (size > pv->size)
264                                 log_warn("WARNING: Physical Volume %s is too "
265                                          "large for underlying device",
266                                          pv_dev_name(pv));
267                 }
268         }
269
270         if (!alloc_pv_segment_whole_pv(mem, pv))
271                 return_0;
272
273         vg->extent_count += pv->pe_count;
274         vg->free_count += pv->pe_count;
275         add_pvl_to_vgs(vg, pvl);
276
277         return 1;
278 }
279
280 static void _insert_segment(struct logical_volume *lv, struct lv_segment *seg)
281 {
282         struct lv_segment *comp;
283
284         dm_list_iterate_items(comp, &lv->segments) {
285                 if (comp->le > seg->le) {
286                         dm_list_add(&comp->list, &seg->list);
287                         return;
288                 }
289         }
290
291         lv->le_count += seg->len;
292         dm_list_add(&lv->segments, &seg->list);
293 }
294
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)
298 {
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;
306
307         if (!sn_child) {
308                 log_error("Empty segment section.");
309                 return 0;
310         }
311
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);
315                 return 0;
316         }
317
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);
321                 return 0;
322         }
323
324         segtype_str = "striped";
325
326         if ((cn = find_config_node(sn_child, "type"))) {
327                 cv = cn->v;
328                 if (!cv || !cv->v.str) {
329                         log_error("Segment type must be a string.");
330                         return 0;
331                 }
332                 segtype_str = cv->v.str;
333         }
334
335         if (!(segtype = get_segtype_from_string(vg->cmd, segtype_str)))
336                 return_0;
337
338         if (segtype->ops->text_import_area_count &&
339             !segtype->ops->text_import_area_count(sn_child, &area_count))
340                 return_0;
341
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");
346                 return 0;
347         }
348
349         if (seg->segtype->ops->text_import &&
350             !seg->segtype->ops->text_import(seg, sn_child, pv_hash))
351                 return_0;
352
353         /* Optional tags */
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.",
357                           vg->name, lv->name);
358                 return 0;
359         }
360
361         /*
362          * Insert into correct part of segment list.
363          */
364         _insert_segment(lv, seg);
365
366         if (seg_is_mirrored(seg))
367                 lv->status |= MIRRORED;
368
369         if (seg_is_virtual(seg))
370                 lv->status |= VIRTUAL;
371
372         if (_is_converting(lv))
373                 lv->status |= CONVERTING;
374
375         return 1;
376 }
377
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,
380                       uint64_t status)
381 {
382         unsigned int s;
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);
387
388         if (!seg->area_count) {
389                 log_error("Zero areas not allowed for segment %s", seg_name);
390                 return 0;
391         }
392
393         for (cv = cn->v, s = 0; cv && s < seg->area_count; s++, cv = cv->next) {
394
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);
398                         return 0;
399                 }
400
401                 if (!cv->next) {
402                         log_error("Missing offset in areas array for segment %s.", seg_name);
403                         return 0;
404                 }
405
406                 if (cv->next->type != CFG_INT) {
407                         log_error("Bad offset in areas array for segment %s.", seg_name);
408                         return 0;
409                 }
410
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))
414                                 return_0;
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,
418                                                     status))
419                                 return_0;
420                 } else {
421                         log_error("Couldn't find volume '%s' "
422                                   "for segment '%s'.",
423                                   cv->v.str ? : "NULL", seg_name);
424                         return 0;
425                 }
426
427                 cv = cv->next;
428         }
429
430         /*
431          * Check we read the correct number of stripes.
432          */
433         if (cv || (s < seg->area_count)) {
434                 log_error("Incorrect number of areas in area array "
435                           "for segment '%s'.", seg_name);
436                 return 0;
437         }
438
439         return 1;
440 }
441
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)
445 {
446         const struct config_node *sn;
447         int count = 0, seg_count;
448
449         for (sn = lvn; sn; sn = sn->sib) {
450
451                 /*
452                  * All sub-sections are assumed to be segments.
453                  */
454                 if (!sn->v) {
455                         if (!_read_segment(mem, vg, lv, sn, pv_hash))
456                                 return_0;
457
458                         count++;
459                 }
460                 /* FIXME Remove this restriction */
461                 if ((lv->status & SNAPSHOT) && count > 1) {
462                         log_error("Only one segment permitted for snapshot");
463                         return 0;
464                 }
465         }
466
467         if (!_read_int32(lvn, "segment_count", &seg_count)) {
468                 log_error("Couldn't read segment count for logical volume %s.",
469                           lv->name);
470                 return 0;
471         }
472
473         if (seg_count != count) {
474                 log_error("segment_count and actual number of segments "
475                           "disagree for logical volume %s.", lv->name);
476                 return 0;
477         }
478
479         /*
480          * Check there are no gaps or overlaps in the lv.
481          */
482         if (!check_lv_segments(lv, 0))
483                 return_0;
484
485         /*
486          * Merge segments in case someones been editing things by hand.
487          */
488         if (!lv_merge_segments(lv))
489                 return_0;
490
491         return 1;
492 }
493
494 static int _read_lvnames(struct format_instance *fid __attribute__((unused)),
495                          struct dm_pool *mem,
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)))
502 {
503         struct logical_volume *lv;
504         const struct config_node *cn;
505
506         if (!(lv = alloc_lv(mem)))
507                 return_0;
508
509         if (!(lv->name = dm_pool_strdup(mem, lvn->key)))
510                 return_0;
511
512         if (!(lvn = lvn->child)) {
513                 log_error("Empty logical volume section.");
514                 return 0;
515         }
516
517         if (!_read_flag_config(lvn, &lv->status, LV_FLAGS)) {
518                 log_error("Couldn't read status flags for logical volume %s.",
519                           lv->name);
520                 return 0;
521         }
522
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.");
528                         return 0;
529                 }
530
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;
535                 }
536         }
537
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;
541         else {
542                 switch (lv->read_ahead) {
543                 case 0:
544                         lv->read_ahead = DM_READ_AHEAD_AUTO;
545                         break;
546                 case (uint32_t) -1:
547                         lv->read_ahead = DM_READ_AHEAD_NONE;
548                         break;
549                 default:
550                         ;
551                 }
552         }
553
554         /* Optional tags */
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.",
558                           vg->name, lv->name);
559                 return 0;
560         }
561
562         if (!dm_hash_insert(lv_hash, lv->name, lv))
563                 return_0;
564
565         return link_lv_to_vg(vg, lv);
566 }
567
568 static int _read_lvsegs(struct format_instance *fid __attribute__((unused)),
569                         struct dm_pool *mem,
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)))
576 {
577         struct logical_volume *lv;
578
579         if (!(lv = dm_hash_lookup(lv_hash, lvn->key))) {
580                 log_error("Lost logical volume reference %s", lvn->key);
581                 return 0;
582         }
583
584         if (!(lvn = lvn->child)) {
585                 log_error("Empty logical volume section.");
586                 return 0;
587         }
588
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.",
592                           lv->name);
593                 return 0;
594         }
595
596         memcpy(&lv->lvid.id[0], &lv->vg->id, sizeof(lv->lvid.id[0]));
597
598         if (!_read_segments(mem, vg, lv, lvn, pv_hash))
599                 return_0;
600
601         lv->size = (uint64_t) lv->le_count * (uint64_t) vg->extent_size;
602
603         lv->minor = -1;
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);
608                 return 0;
609         }
610
611         lv->major = -1;
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);
616         }
617
618         return 1;
619 }
620
621 static int _read_sections(struct format_instance *fid,
622                           const char *section, section_fn fn,
623                           struct dm_pool *mem,
624                           struct volume_group *vg, const struct config_node *vgn,
625                           struct dm_hash_table *pv_hash,
626                           struct dm_hash_table *lv_hash,
627                           int optional,
628                           unsigned *scan_done_once)
629 {
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;
633
634         if (!(n = find_config_node(vgn, section))) {
635                 if (!optional) {
636                         log_error("Couldn't find section '%s'.", section);
637                         return 0;
638                 }
639
640                 return 1;
641         }
642
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))
646                         return_0;
647         }
648
649         return 1;
650 }
651
652 static struct volume_group *_read_vg(struct format_instance *fid,
653                                      const struct config_tree *cft,
654                                      unsigned use_cached_pvs)
655 {
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;
661
662         if (!mem)
663                 return_NULL;
664
665         /* skip any top-level values */
666         for (vgn = cft->root; (vgn && vgn->v); vgn = vgn->sib)
667                 ;
668
669         if (!vgn) {
670                 log_error("Couldn't find volume group in file.");
671                 goto bad;
672         }
673
674         if (!(vg = dm_pool_zalloc(mem, sizeof(*vg))))
675                 goto_bad;
676
677         vg->vgmem = mem;
678         vg->cmd = fid->fmt->cmd;
679
680         /* FIXME Determine format type from file contents */
681         /* eg Set to instance of fmt1 here if reading a format1 backup? */
682         vg->fid = fid;
683
684         if (!(vg->name = dm_pool_strdup(mem, vgn->key)))
685                 goto_bad;
686
687         if (!(vg->system_id = dm_pool_zalloc(mem, NAME_LEN)))
688                 goto_bad;
689
690         vgn = vgn->child;
691
692         if ((cn = find_config_node(vgn, "system_id")) && cn->v) {
693                 if (!cn->v->v.str) {
694                         log_error("system_id must be a string");
695                         goto bad;
696                 }
697                 strncpy(vg->system_id, cn->v->v.str, NAME_LEN);
698         }
699
700         if (!_read_id(&vg->id, vgn, "id")) {
701                 log_error("Couldn't read uuid for volume group %s.", vg->name);
702                 goto bad;
703         }
704
705         if (!_read_int32(vgn, "seqno", &vg->seqno)) {
706                 log_error("Couldn't read 'seqno' for volume group %s.",
707                           vg->name);
708                 goto bad;
709         }
710
711         if (!_read_flag_config(vgn, &vg->status, VG_FLAGS)) {
712                 log_error("Error reading flags of volume group %s.",
713                           vg->name);
714                 goto bad;
715         }
716
717         if (!_read_int32(vgn, "extent_size", &vg->extent_size)) {
718                 log_error("Couldn't read extent size for volume group %s.",
719                           vg->name);
720                 goto bad;
721         }
722
723         /*
724          * 'extent_count' and 'free_count' get filled in
725          * implicitly when reading in the pv's and lv's.
726          */
727
728         if (!_read_int32(vgn, "max_lv", &vg->max_lv)) {
729                 log_error("Couldn't read 'max_lv' for volume group %s.",
730                           vg->name);
731                 goto bad;
732         }
733
734         if (!_read_int32(vgn, "max_pv", &vg->max_pv)) {
735                 log_error("Couldn't read 'max_pv' for volume group %s.",
736                           vg->name);
737                 goto bad;
738         }
739
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.");
745                         goto bad;
746                 }
747
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;
752                 }
753         }
754
755         if (!_read_uint32(vgn, "metadata_copies", &vg->mda_copies)) {
756                 vg->mda_copies = DEFAULT_VGMETADATACOPIES;
757         }
758
759         /*
760          * The pv hash memorises the pv section names -> pv
761          * structures.
762          */
763         if (!(pv_hash = dm_hash_create(32))) {
764                 log_error("Couldn't create hash table.");
765                 goto bad;
766         }
767
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);
773                 goto bad;
774         }
775
776         dm_list_init(&vg->lvs);
777         dm_list_init(&vg->tags);
778         dm_list_init(&vg->removed_pvs);
779
780         /* Optional tags */
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);
784                 goto bad;
785         }
786
787         /*
788          * The lv hash memorises the lv section names -> lv
789          * structures.
790          */
791         if (!(lv_hash = dm_hash_create(32))) {
792                 log_error("Couldn't create hash table.");
793                 goto bad;
794         }
795
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);
800                 goto bad;
801         }
802
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);
807                 goto bad;
808         }
809
810         if (!fixup_imported_mirrors(vg)) {
811                 log_error("Failed to fixup mirror pointers after import for "
812                           "volume group %s.", vg->name);
813                 goto bad;
814         }
815
816         dm_hash_destroy(pv_hash);
817         dm_hash_destroy(lv_hash);
818
819         /*
820          * Finished.
821          */
822         return vg;
823
824       bad:
825         if (pv_hash)
826                 dm_hash_destroy(pv_hash);
827
828         if (lv_hash)
829                 dm_hash_destroy(lv_hash);
830
831         dm_pool_destroy(mem);
832         return NULL;
833 }
834
835 static void _read_desc(struct dm_pool *mem,
836                        const struct config_tree *cft, time_t *when, char **desc)
837 {
838         const char *d;
839         unsigned int u = 0u;
840         int old_suppress;
841
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);
846
847         get_config_uint32(cft->root, "creation_time", &u);
848         *when = u;
849 }
850
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)
854 {
855         const struct config_node *vgn;
856         struct dm_pool *mem = fmt->cmd->mem;
857         char *vgname;
858         int old_suppress;
859
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);
865
866         /* skip any top-level values */
867         for (vgn = cft->root; (vgn && vgn->v); vgn = vgn->sib) ;
868
869         if (!vgn) {
870                 log_error("Couldn't find volume group in file.");
871                 return 0;
872         }
873
874         if (!(vgname = dm_pool_strdup(mem, vgn->key)))
875                 return_0;
876
877         vgn = vgn->child;
878
879         if (!_read_id(vgid, vgn, "id")) {
880                 log_error("Couldn't read uuid for volume group %s.", vgname);
881                 return 0;
882         }
883
884         if (!_read_flag_config(vgn, vgstatus, VG_FLAGS)) {
885                 log_error("Couldn't find status flags for volume group %s.",
886                           vgname);
887                 return 0;
888         }
889
890         return vgname;
891 }
892
893 static struct text_vg_version_ops _vsn1_ops = {
894         .check_version = _check_version,
895         .read_vg = _read_vg,
896         .read_desc = _read_desc,
897         .read_vgname = _read_vgname,
898 };
899
900 struct text_vg_version_ops *text_vg_vsn1_init(void)
901 {
902         return &_vsn1_ops;
903 }