summaryrefslogtreecommitdiffstats
path: root/lib/format_text/import.c
blob: 6066ae05089f6905fe6b6915b92bd8b509010409 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
/*
 * Copyright (C) 2001 Sistina Software (UK) Limited.
 *
 * This file is released under the LGPL.
 */

#include "metadata.h"
#include "import-export.h"
#include "pool.h"
#include "log.h"
#include "uuid.h"
#include "hash.h"


typedef int (*section_fn)(struct pool *mem,
			  struct volume_group *vg, struct config_node *pvn,
			  struct config_node *vgn, struct hash_table *pv_hash,
			  struct uuid_map *um);

#define _read_int32(root, path, result) \
	get_config_uint32(root, path, '/', result)

#define _read_int64(root, path, result) \
	get_config_uint64(root, path, '/', result)

static int _read_id(struct id *id, struct config_node *cn, const char *path)
{
	struct config_value *cv;

	if (!(cn = find_config_node(cn, path, '/'))) {
		log_err("Couldn't find uuid.");
		return 0;
	}

	cv = cn->v;
	if (!cv || !cv->v.str) {
		log_err("uuid must be a string.");
		return 0;
	}

	if (!id_read_format(id, cv->v.str)) {
		log_err("Invalid uuid.");
		return 0;
	}

	return 1;
}

static int _read_pv(struct pool *mem,
		    struct volume_group *vg, struct config_node *pvn,
		    struct config_node *vgn,
		    struct hash_table *pv_hash,
		    struct uuid_map *um)
{
	struct physical_volume *pv;
	struct pv_list *pvl;
	struct config_node *cn;

	if (!(pvl = pool_zalloc(mem, sizeof(*pvl))) ||
	    !(pvl->pv = pool_zalloc(mem, sizeof(*pvl->pv)))) {
		stack;
		return 0;
	}

	pv = pvl->pv;

	/*
	 * Add the pv to the pv hash for quick lookup when we read
	 * the lv segments.
	 */
	if (!hash_insert(pv_hash, pvn->key, pv)) {
		stack;
		return 0;
	}

	if (!(pvn = pvn->child)) {
		log_err("Empty pv section.");
		return 0;
	}

	if (!_read_id(&pv->id, pvn, "id")) {
		log_err("Couldn't read uuid for volume group.");
		return 0;
	}

	/*
	 * Use the uuid map to convert the uuid into a device.
	 */
	if (!(pv->dev = uuid_map_lookup(um, &pv->id))) {
		char buffer[64];

		if (!id_write_format(&pv->id, buffer, sizeof(buffer))) {
			log_err("Couldn't find device.");
			return 0;
		}

		log_err("Couldn't find device with uuid '%s'.", buffer);
		return 0;
	}

	if (!(pv->vg_name = pool_strdup(mem, vg->name))) {
		stack;
		return 0;
	}

	if (!(cn = find_config_node(pvn, "status", '/'))) {
		log_err("Couldn't find status flags for physical volume.");
		return 0;
	}

	if (!(read_flags(&pv->status, PV_FLAGS, cn->v))) {
		log_err("Couldn't read status flags for physical volume.");
		return 0;
	}

	if (!_read_int64(pvn, "pe_start", &pv->pe_start)) {
		log_err("Couldn't read extent size for volume group.");
		return 0;
	}

	if (!_read_int32(pvn, "pe_count", &pv->pe_count)) {
		log_err("Couldn't find extent count (pe_count) for "
			"physical volume.");
		return 0;
	}

	/* adjust the volume group. */
	vg->extent_count += pv->pe_count;
	vg->free_count += pv->pe_count;

	pv->pe_size = vg->extent_size;
	pv->size = pv->pe_size * (uint64_t) pv->pe_count;
	pv->pe_allocated = 0;

	vg->pv_count++;
	list_add(&vg->pvs, &pvl->list);

	return 1;
}

static void _insert_segment(struct logical_volume *lv,
			    struct stripe_segment *seg)
{
	struct list *segh;
	struct stripe_segment *comp;

	list_iterate (segh, &lv->segments) {
		comp = list_item(segh, struct stripe_segment);

		if (comp->le > seg->le) {
			list_add(&comp->list, &seg->list);
			return;
		}
	}

	lv->le_count += seg->len;
	list_add(&lv->segments, &seg->list);
}

static int _read_segment(struct pool *mem, struct volume_group *vg,
			 struct logical_volume *lv, struct config_node *sn,
			 struct hash_table *pv_hash)
{
	int s;
	uint32_t stripes;
	struct stripe_segment *seg;
	struct config_node *cn;
	struct config_value *cv;
	const char *seg_name = sn->key;

	if (!(sn = sn->child)) {
		log_err("Empty segment section.");
		return 0;
	}

	if (!_read_int32(sn, "stripes", &stripes)) {
		log_err("Couldn't read 'stripes' for segment '%s'.",
			sn->key);
		return 0;
	}

	if (!(seg = pool_zalloc(mem, sizeof(*seg) +
				(sizeof(seg->area[0]) * stripes)))) {
		stack;
		return 0;
	}
	seg->stripes = stripes;

	if (!_read_int32(sn, "start_extent", &seg->le)) {
		log_err("Couldn't read 'start_extent' for segment '%s'.",
			sn->key);
		return 0;
	}

	if (!_read_int32(sn, "extent_count", &seg->len)) {
		log_err("Couldn't read 'extent_count' for segment '%s'.",
			sn->key);
		return 0;
	}

	if (seg->stripes == 0) {
		log_err("Zero stripes is *not* allowed for segment '%s'.",
			sn->key);
		return 0;
	}

	if ((seg->stripes != 1) &&
	    !_read_int32(sn, "stripe_size", &seg->stripe_size)) {
		log_err("Couldn't read 'stripe_size' for segment '%s'.",
			sn->key);
		return 0;
	}

	if (!(cn = find_config_node(sn, "areas", '/'))) {
		log_err("Couldn't find 'areas' array for segment '%s'.",
			sn->key);
		return 0;
	}

	/*
	 * Read the stripes from the 'areas' array.
	 * FIXME: we could move this to a separate function.
	 */
	for (cv = cn->v, s = 0; cv && s < seg->stripes; s++, cv = cv->next) {

		/* first we read the pv */
		const char *bad = "Badly formed areas array for segment '%s'.";
		struct physical_volume *pv;
		uint32_t allocated;

		if (cv->type != CFG_STRING) {
			log_err(bad, sn->key);
			return 0;
		}

		if (!(pv = hash_lookup(pv_hash, cv->v.str))) {
			log_err("Couldn't find physical volume '%s' for "
				"segment '%s'.",
				cn->v->v.str ? cn->v->v.str : "NULL",
				seg_name);
				return 0;
		}

		seg->area[s].pv = pv;

		if (!(cv = cv->next)) {
			log_err(bad, sn->key);
			return 0;
		}

		if (cv->type != CFG_INT) {
			log_err(bad, sn->key);
			return 0;
		}

		seg->area[s].pe = cv->v.i;

		/*
		 * Adjust the extent counts in the pv and vg.
		 */
		allocated = seg->len / seg->stripes;
		pv->pe_allocated += allocated;
		vg->free_count -= allocated;
	}

	/*
	 * Check we read the correct number of stripes.
	 */
	if (cv || (s < seg->stripes)) {
		log_err("Incorrect number of stripes in 'area' array "
			"for segment '%s'.", seg_name);
		return 0;
	}

	/*
	 * Insert into correct part of segment list.
	 */
	_insert_segment(lv, seg);
	return 1;
}

static int _read_segments(struct pool *mem, struct volume_group *vg,
			  struct logical_volume *lv, struct config_node *lvn,
			  struct hash_table *pv_hash)
{
	struct config_node *sn;
	int count = 0, seg_count;

	for (sn = lvn; sn; sn = sn->sib) {

		/*
		 * All sub-sections are assumed to be segments.
		 */
		if (!sn->v) {
			if (!_read_segment(mem, vg, lv, sn, pv_hash)) {
				stack;
				return 0;
			}

			count++;
		}
	}

	if (!_read_int32(lvn, "segment_count", &seg_count)) {
		log_err("Couldn't read segment count for logical volume.");
		return 0;
	}

	if (seg_count != count) {
		log_err("segment_count and actual number of segments "
			"disagree.");
		return 0;
	}

	/*
	 * Check there are no gaps or overlaps in the lv.
	 */
	if (!lv_check_segments(lv)) {
		stack;
		return 0;
	}

	/*
	 * Merge segments in case someones been editing things by hand.
	 */
	if (!lv_merge_segments(lv)) {
		stack;
		return 0;
	}

	return 1;
}

static int _read_lv(struct pool *mem,
		    struct volume_group *vg, struct config_node *lvn,
		    struct config_node *vgn, struct hash_table *pv_hash,
		    struct uuid_map *um)
{
	struct logical_volume *lv;
	struct lv_list *lvl;
	struct config_node *cn;

	if (!(lvl = pool_zalloc(mem, sizeof(*lvl))) ||
	    !(lvl->lv = pool_zalloc(mem, sizeof(*lvl->lv)))) {
		stack;
		return 0;
	}

	lv = lvl->lv;

	if (!(lv->name = pool_strdup(mem, lvn->key))) {
		stack;
		return 0;
	}

	if (!(lvn = lvn->child)) {
		log_err("Empty logical volume section.");
		return 0;
	}

	lv->vg = vg;


	if (!(cn = find_config_node(lvn, "status", '/'))) {
		log_err("Couldn't find status flags for logical volume.");
		return 0;
	}

	if (!(read_flags(&lv->status, LV_FLAGS, cn->v))) {
		log_err("Couldn't read status flags for logical volume.");
		return 0;
	}

	if (!_read_int32(lvn, "read_ahead", &lv->read_ahead)) {
		log_err("Couldn't read 'read_ahead' value for "
			"logical volume.");
		return 0;
	}

	list_init(&lv->segments);
	if (!_read_segments(mem, vg, lv, lvn, pv_hash)) {
		stack;
		return 0;
	}
	lv->size = (uint64_t) lv->le_count * (uint64_t) vg->extent_size;

	vg->lv_count++;
	list_add(&vg->lvs, &lvl->list);

	return 1;
}

static int _read_sections(const char *section, section_fn fn,
			  struct pool *mem,
			  struct volume_group *vg, struct config_node *vgn,
			  struct hash_table *pv_hash,
			  struct uuid_map *um)
{
	struct config_node *n;

	if (!(n = find_config_node(vgn, section, '/'))) {
		log_err("Couldn't find section '%s'.", section);
		return 0;
	}

	for (n = n->child; n; n = n->sib) {
		if (!fn(mem, vg, n, vgn, pv_hash, um)) {
			stack;
			return 0;
		}
	}

	return 1;
}

static struct volume_group *_read_vg(struct pool *mem, struct config_file *cf,
				     struct uuid_map *um)
{
	struct config_node *vgn = cf->root, *cn;
	struct volume_group *vg;
	struct hash_table *pv_hash = NULL;

	if (!vgn) {
		log_err("Couldn't find volume group.");
		return NULL;
	}

	if (!(vgn = cf->root)) {
		log_err("Couldn't find volume group in file.");
		return NULL;
	}

	if (!(vg = pool_zalloc(mem, sizeof(*vg)))) {
		stack;
		return NULL;
	}

	if (!(vg->name = pool_strdup(mem, vgn->key))) {
		stack;
		goto bad;
	}

	vgn = vgn->child;
	if (!_read_id(&vg->id, vgn, "id")) {
		log_err("Couldn't read uuid for volume group %s.",
			vg->name);
		goto bad;
	}

	if (!(cn = find_config_node(vgn, "status", '/'))) {
		log_err("Couldn't find status flags for volume group %s.",
			vg->name);
		goto bad;
	}

	if (!(read_flags(&vg->status, VG_FLAGS, cn->v))) {
		log_err("Couldn't read status flags for volume group %s.",
			vg->name);
		goto bad;
	}

	if (!_read_int32(vgn, "extent_size", &vg->extent_size)) {
		log_err("Couldn't read extent size for volume group %s.",
			vg->name);
		goto bad;
	}

	/*
	 * 'extent_count' and 'free_count' get filled in
	 * implicitly when reading in the pv's and lv's.
	 */

	if (!_read_int32(vgn, "max_lv", &vg->max_lv)) {
		log_err("Couldn't read 'max_lv' for volume group %s.",
			vg->name);
		goto bad;
	}

	if (!_read_int32(vgn, "max_pv", &vg->max_pv)) {
		log_err("Couldn't read 'max_pv' for volume group %s.",
			vg->name);
		goto bad;
	}

	/*
	 * The pv hash memoises the pv section names -> pv
	 * structures.
	 */
	if (!(pv_hash = hash_create(32))) {
		log_err("Couldn't create hash table.");
		goto bad;
	}

	list_init(&vg->pvs);
	if (!_read_sections("physical_volumes", _read_pv, mem, vg,
			    vgn, pv_hash, um)) {
		log_err("Couldn't find all physical volumes for volume "
			"group %s.", vg->name);
		goto bad;
	}

	list_init(&vg->lvs);
	if (!_read_sections("logical_volumes", _read_lv, mem, vg,
			    vgn, pv_hash, um)) {
		log_err("Couldn't read all logical volumes for volume "
			"group %s.", vg->name);
		goto bad;
	}

	hash_destroy(pv_hash);

	/*
	 * Finished.
	 */
	return vg;

 bad:
	if (pv_hash)
		hash_destroy(pv_hash);

	pool_free(mem, vg);
	return NULL;
}

struct volume_group *text_vg_import(struct cmd_context *cmd,
				    const char *file,
				    struct uuid_map *um)
{
	struct volume_group *vg = NULL;
	struct config_file *cf;

	if (!(cf = create_config_file())) {
		stack;
		goto out;
	}

	if (!read_config(cf, file)) {
		log_error("Couldn't read volume group file.");
		goto out;
	}

	if (!(vg = _read_vg(cmd->mem, cf, um))) {
		stack;
		goto out;
	}

	vg->cmd = cmd;

 out:
	destroy_config_file(cf);
	return vg;
}