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