g_linux_lvm.c revision 194924
1/*-
2 * Copyright (c) 2008 Andrew Thompson <thompsa@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/geom/linux_lvm/g_linux_lvm.c 194924 2009-06-24 22:09:30Z lulf $");
29
30#include <sys/ctype.h>
31#include <sys/param.h>
32#include <sys/bio.h>
33#include <sys/kernel.h>
34#include <sys/limits.h>
35#include <sys/malloc.h>
36#include <sys/queue.h>
37#include <sys/sysctl.h>
38#include <sys/systm.h>
39
40#include <geom/geom.h>
41#include <sys/endian.h>
42
43#include <geom/linux_lvm/g_linux_lvm.h>
44
45/* Declare malloc(9) label */
46static MALLOC_DEFINE(M_GLLVM, "gllvm", "GEOM_LINUX_LVM Data");
47
48/* GEOM class methods */
49static g_access_t g_llvm_access;
50static g_init_t g_llvm_init;
51static g_orphan_t g_llvm_orphan;
52static g_orphan_t g_llvm_taste_orphan;
53static g_start_t g_llvm_start;
54static g_taste_t g_llvm_taste;
55static g_ctl_destroy_geom_t g_llvm_destroy_geom;
56
57static void	g_llvm_done(struct bio *);
58static void	g_llvm_remove_disk(struct g_llvm_vg *, struct g_consumer *);
59static int	g_llvm_activate_lv(struct g_llvm_vg *, struct g_llvm_lv *);
60static int	g_llvm_add_disk(struct g_llvm_vg *, struct g_provider *, char *);
61static void	g_llvm_free_vg(struct g_llvm_vg *);
62static int	g_llvm_destroy(struct g_llvm_vg *, int);
63static int	g_llvm_read_label(struct g_consumer *, struct g_llvm_label *);
64static int	g_llvm_read_md(struct g_consumer *, struct g_llvm_metadata *,
65		    struct g_llvm_label *);
66
67static int	llvm_label_decode(const u_char *, struct g_llvm_label *, int);
68static int	llvm_md_decode(const u_char *, struct g_llvm_metadata *,
69		    struct g_llvm_label *);
70static int	llvm_textconf_decode(u_char *, int,
71		    struct g_llvm_metadata *);
72static int	llvm_textconf_decode_pv(char **, char *, struct g_llvm_vg *);
73static int	llvm_textconf_decode_lv(char **, char *, struct g_llvm_vg *);
74static int	llvm_textconf_decode_sg(char **, char *, struct g_llvm_lv *);
75
76SYSCTL_DECL(_kern_geom);
77SYSCTL_NODE(_kern_geom, OID_AUTO, linux_lvm, CTLFLAG_RW, 0,
78    "GEOM_LINUX_LVM stuff");
79static u_int g_llvm_debug = 0;
80TUNABLE_INT("kern.geom.linux_lvm.debug", &g_llvm_debug);
81SYSCTL_UINT(_kern_geom_linux_lvm, OID_AUTO, debug, CTLFLAG_RW, &g_llvm_debug, 0,
82    "Debug level");
83
84LIST_HEAD(, g_llvm_vg) vg_list;
85
86/*
87 * Called to notify geom when it's been opened, and for what intent
88 */
89static int
90g_llvm_access(struct g_provider *pp, int dr, int dw, int de)
91{
92	struct g_consumer *c;
93	struct g_llvm_vg *vg;
94	struct g_geom *gp;
95	int error;
96
97	KASSERT(pp != NULL, ("%s: NULL provider", __func__));
98	gp = pp->geom;
99	KASSERT(gp != NULL, ("%s: NULL geom", __func__));
100	vg = gp->softc;
101
102	if (vg == NULL) {
103		/* It seems that .access can be called with negative dr,dw,dx
104		 * in this case but I want to check for myself */
105		G_LLVM_DEBUG(0, "access(%d, %d, %d) for %s",
106		    dr, dw, de, pp->name);
107
108		/* This should only happen when geom is withered so
109		 * allow only negative requests */
110		KASSERT(dr <= 0 && dw <= 0 && de <= 0,
111		    ("%s: Positive access for %s", __func__, pp->name));
112		if (pp->acr + dr == 0 && pp->acw + dw == 0 && pp->ace + de == 0)
113			G_LLVM_DEBUG(0,
114			    "Device %s definitely destroyed", pp->name);
115		return (0);
116	}
117
118	/* Grab an exclusive bit to propagate on our consumers on first open */
119	if (pp->acr == 0 && pp->acw == 0 && pp->ace == 0)
120		de++;
121	/* ... drop it on close */
122	if (pp->acr + dr == 0 && pp->acw + dw == 0 && pp->ace + de == 0)
123		de--;
124
125	error = ENXIO;
126	LIST_FOREACH(c, &gp->consumer, consumer) {
127		KASSERT(c != NULL, ("%s: consumer is NULL", __func__));
128		error = g_access(c, dr, dw, de);
129		if (error != 0) {
130			struct g_consumer *c2;
131
132			/* Backout earlier changes */
133			LIST_FOREACH(c2, &gp->consumer, consumer) {
134				if (c2 == c) /* all eariler components fixed */
135					return (error);
136				g_access(c2, -dr, -dw, -de);
137			}
138		}
139	}
140
141	return (error);
142}
143
144/*
145 * Dismantle bio_queue and destroy its components
146 */
147static void
148bioq_dismantle(struct bio_queue_head *bq)
149{
150	struct bio *b;
151
152	for (b = bioq_first(bq); b != NULL; b = bioq_first(bq)) {
153		bioq_remove(bq, b);
154		g_destroy_bio(b);
155	}
156}
157
158/*
159 * GEOM .done handler
160 * Can't use standard handler because one requested IO may
161 * fork into additional data IOs
162 */
163static void
164g_llvm_done(struct bio *b)
165{
166	struct bio *parent_b;
167
168	parent_b = b->bio_parent;
169
170	if (b->bio_error != 0) {
171		G_LLVM_DEBUG(0, "Error %d for offset=%ju, length=%ju on %s",
172		    b->bio_error, b->bio_offset, b->bio_length,
173		    b->bio_to->name);
174		if (parent_b->bio_error == 0)
175			parent_b->bio_error = b->bio_error;
176	}
177
178	parent_b->bio_inbed++;
179	parent_b->bio_completed += b->bio_completed;
180
181	if (parent_b->bio_children == parent_b->bio_inbed) {
182		parent_b->bio_completed = parent_b->bio_length;
183		g_io_deliver(parent_b, parent_b->bio_error);
184	}
185	g_destroy_bio(b);
186}
187
188static void
189g_llvm_start(struct bio *bp)
190{
191	struct g_provider *pp;
192	struct g_llvm_vg *vg;
193	struct g_llvm_pv *pv;
194	struct g_llvm_lv *lv;
195	struct g_llvm_segment *sg;
196	struct bio *cb;
197	struct bio_queue_head bq;
198	size_t chunk_size;
199	off_t offset, length;
200	char *addr;
201	u_int count;
202
203	pp = bp->bio_to;
204	lv = pp->private;
205	vg = pp->geom->softc;
206
207	switch (bp->bio_cmd) {
208	case BIO_READ:
209	case BIO_WRITE:
210	case BIO_DELETE:
211	/* XXX BIO_GETATTR allowed? */
212		break;
213	default:
214		g_io_deliver(bp, EOPNOTSUPP);
215		return;
216	}
217
218	bioq_init(&bq);
219
220	chunk_size = vg->vg_extentsize;
221	addr = bp->bio_data;
222	offset = bp->bio_offset;	/* virtual offset and length */
223	length = bp->bio_length;
224
225	while (length > 0) {
226		size_t chunk_index, in_chunk_offset, in_chunk_length;
227
228		pv = NULL;
229		cb = g_clone_bio(bp);
230		if (cb == NULL) {
231			bioq_dismantle(&bq);
232			if (bp->bio_error == 0)
233				bp->bio_error = ENOMEM;
234			g_io_deliver(bp, bp->bio_error);
235			return;
236		}
237
238		/* get the segment and the pv */
239		if (lv->lv_sgcount == 1) {
240			/* skip much of the calculations for a single sg */
241			chunk_index = 0;
242			in_chunk_offset = 0;
243			in_chunk_length = length;
244			sg = lv->lv_firstsg;
245			pv = sg->sg_pv;
246			cb->bio_offset = offset + sg->sg_pvoffset;
247		} else {
248			chunk_index = offset / chunk_size; /* round downwards */
249			in_chunk_offset = offset % chunk_size;
250			in_chunk_length =
251			    min(length, chunk_size - in_chunk_offset);
252
253			/* XXX could be faster */
254			LIST_FOREACH(sg, &lv->lv_segs, sg_next) {
255				if (chunk_index >= sg->sg_start &&
256				    chunk_index <= sg->sg_end) {
257					/* adjust chunk index for sg start */
258					chunk_index -= sg->sg_start;
259					pv = sg->sg_pv;
260					break;
261				}
262			}
263			cb->bio_offset =
264			    (off_t)chunk_index * (off_t)chunk_size
265			    + in_chunk_offset + sg->sg_pvoffset;
266		}
267
268		KASSERT(pv != NULL, ("Can't find PV for chunk %zu",
269		    chunk_index));
270
271		cb->bio_to = pv->pv_gprov;
272		cb->bio_done = g_llvm_done;
273		cb->bio_length = in_chunk_length;
274		cb->bio_data = addr;
275		cb->bio_caller1 = pv;
276		bioq_disksort(&bq, cb);
277
278		G_LLVM_DEBUG(5,
279		    "Mapped %s(%ju, %ju) on %s to %zu(%zu,%zu) @ %s:%ju",
280		    bp->bio_cmd == BIO_READ ? "R" : "W",
281		    offset, length, lv->lv_name,
282		    chunk_index, in_chunk_offset, in_chunk_length,
283		    pv->pv_name, cb->bio_offset);
284
285		addr += in_chunk_length;
286		length -= in_chunk_length;
287		offset += in_chunk_length;
288	}
289
290	/* Fire off bio's here */
291	count = 0;
292	for (cb = bioq_first(&bq); cb != NULL; cb = bioq_first(&bq)) {
293		bioq_remove(&bq, cb);
294		pv = cb->bio_caller1;
295		cb->bio_caller1 = NULL;
296		G_LLVM_DEBUG(6, "firing bio to %s, offset=%ju, length=%ju",
297		    cb->bio_to->name, cb->bio_offset, cb->bio_length);
298		g_io_request(cb, pv->pv_gcons);
299		count++;
300	}
301	if (count == 0) { /* We handled everything locally */
302		bp->bio_completed = bp->bio_length;
303		g_io_deliver(bp, 0);
304	}
305}
306
307static void
308g_llvm_remove_disk(struct g_llvm_vg *vg, struct g_consumer *cp)
309{
310	struct g_llvm_pv *pv;
311	struct g_llvm_lv *lv;
312	struct g_llvm_segment *sg;
313	int found;
314
315	KASSERT(cp != NULL, ("Non-valid disk in %s.", __func__));
316	pv = (struct g_llvm_pv *)cp->private;
317
318	G_LLVM_DEBUG(0, "Disk %s removed from %s.", cp->provider->name,
319	    pv->pv_name);
320
321	LIST_FOREACH(lv, &vg->vg_lvs, lv_next) {
322		/* Find segments that map to this disk */
323		found = 0;
324		LIST_FOREACH(sg, &lv->lv_segs, sg_next) {
325			if (sg->sg_pv == pv) {
326				sg->sg_pv = NULL;
327				lv->lv_sgactive--;
328				found = 1;
329				break;
330			}
331		}
332		if (found) {
333			G_LLVM_DEBUG(0, "Device %s removed.",
334			    lv->lv_gprov->name);
335			g_orphan_provider(lv->lv_gprov, ENXIO);
336			lv->lv_gprov = NULL;
337		}
338	}
339
340	if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
341		g_access(cp, -cp->acr, -cp->acw, -cp->ace);
342	g_detach(cp);
343	g_destroy_consumer(cp);
344}
345
346static void
347g_llvm_orphan(struct g_consumer *cp)
348{
349	struct g_llvm_vg *vg;
350	struct g_geom *gp;
351
352	g_topology_assert();
353	gp = cp->geom;
354	vg = gp->softc;
355	if (vg == NULL)
356		return;
357
358	g_llvm_remove_disk(vg, cp);
359	g_llvm_destroy(vg, 1);
360}
361
362static int
363g_llvm_activate_lv(struct g_llvm_vg *vg, struct g_llvm_lv *lv)
364{
365	struct g_geom *gp;
366	struct g_provider *pp;
367
368	g_topology_assert();
369
370	KASSERT(lv->lv_sgactive == lv->lv_sgcount, ("segment missing"));
371
372	gp = vg->vg_geom;
373	pp = g_new_providerf(gp, "linux_lvm/%s-%s", vg->vg_name, lv->lv_name);
374	pp->mediasize = vg->vg_extentsize * (off_t)lv->lv_extentcount;
375	pp->sectorsize = vg->vg_sectorsize;
376	g_error_provider(pp, 0);
377	lv->lv_gprov = pp;
378	pp->private = lv;
379
380	G_LLVM_DEBUG(1, "Created %s, %juM", pp->name,
381	    pp->mediasize / (1024*1024));
382
383	return (0);
384}
385
386static int
387g_llvm_add_disk(struct g_llvm_vg *vg, struct g_provider *pp, char *uuid)
388{
389	struct g_geom *gp;
390	struct g_consumer *cp, *fcp;
391	struct g_llvm_pv *pv;
392	struct g_llvm_lv *lv;
393	struct g_llvm_segment *sg;
394	int error;
395
396	g_topology_assert();
397
398	LIST_FOREACH(pv, &vg->vg_pvs, pv_next) {
399		if (strcmp(pv->pv_uuid, uuid) == 0)
400			break;	/* found it */
401	}
402	if (pv == NULL) {
403		G_LLVM_DEBUG(3, "uuid %s not found in pv list", uuid);
404		return (ENOENT);
405	}
406	if (pv->pv_gprov != NULL) {
407		G_LLVM_DEBUG(0, "disk %s already initialised in %s",
408		    pv->pv_name, vg->vg_name);
409		return (EEXIST);
410	}
411
412	pv->pv_start *= vg->vg_sectorsize;
413	gp = vg->vg_geom;
414	fcp = LIST_FIRST(&gp->consumer);
415
416	cp = g_new_consumer(gp);
417	error = g_attach(cp, pp);
418	G_LLVM_DEBUG(1, "Attached %s to %s at offset %ju",
419	    pp->name, pv->pv_name, pv->pv_start);
420
421	if (error != 0) {
422		G_LLVM_DEBUG(0, "cannot attach %s to %s",
423		    pp->name, vg->vg_name);
424		g_destroy_consumer(cp);
425		return (error);
426	}
427
428	if (fcp != NULL) {
429		if (fcp->provider->sectorsize != pp->sectorsize) {
430			G_LLVM_DEBUG(0, "Provider %s of %s has invalid "
431			    "sector size (%d)", pp->name, vg->vg_name,
432			    pp->sectorsize);
433			return (EINVAL);
434		}
435		if (fcp->acr > 0 || fcp->acw || fcp->ace > 0) {
436			/* Replicate access permissions from first "live"
437			 * consumer to the new one */
438			error = g_access(cp, fcp->acr, fcp->acw, fcp->ace);
439			if (error != 0) {
440				g_detach(cp);
441				g_destroy_consumer(cp);
442				return (error);
443			}
444		}
445	}
446
447	cp->private = pv;
448	pv->pv_gcons = cp;
449	pv->pv_gprov = pp;
450
451	LIST_FOREACH(lv, &vg->vg_lvs, lv_next) {
452		/* Find segments that map to this disk */
453		LIST_FOREACH(sg, &lv->lv_segs, sg_next) {
454			if (strcmp(sg->sg_pvname, pv->pv_name) == 0) {
455				/* avtivate the segment */
456				KASSERT(sg->sg_pv == NULL,
457				    ("segment already mapped"));
458				sg->sg_pvoffset =
459				    (off_t)sg->sg_pvstart * vg->vg_extentsize
460				    + pv->pv_start;
461				sg->sg_pv = pv;
462				lv->lv_sgactive++;
463
464				G_LLVM_DEBUG(2, "%s: %d to %d @ %s:%d"
465				    " offset %ju sector %ju",
466				    lv->lv_name, sg->sg_start, sg->sg_end,
467				    sg->sg_pvname, sg->sg_pvstart,
468				    sg->sg_pvoffset,
469				    sg->sg_pvoffset / vg->vg_sectorsize);
470			}
471		}
472		/* Activate any lvs waiting on this disk */
473		if (lv->lv_gprov == NULL && lv->lv_sgactive == lv->lv_sgcount) {
474			error = g_llvm_activate_lv(vg, lv);
475			if (error)
476				break;
477		}
478	}
479	return (error);
480}
481
482static void
483g_llvm_init(struct g_class *mp)
484{
485	LIST_INIT(&vg_list);
486}
487
488static void
489g_llvm_free_vg(struct g_llvm_vg *vg)
490{
491	struct g_llvm_pv *pv;
492	struct g_llvm_lv *lv;
493	struct g_llvm_segment *sg;
494
495	/* Free all the structures */
496	while ((pv = LIST_FIRST(&vg->vg_pvs)) != NULL) {
497		LIST_REMOVE(pv, pv_next);
498		free(pv, M_GLLVM);
499	}
500	while ((lv = LIST_FIRST(&vg->vg_lvs)) != NULL) {
501		while ((sg = LIST_FIRST(&lv->lv_segs)) != NULL) {
502			LIST_REMOVE(sg, sg_next);
503			free(sg, M_GLLVM);
504		}
505		LIST_REMOVE(lv, lv_next);
506		free(lv, M_GLLVM);
507	}
508	LIST_REMOVE(vg, vg_next);
509	free(vg, M_GLLVM);
510}
511
512static void
513g_llvm_taste_orphan(struct g_consumer *cp)
514{
515
516	KASSERT(1 == 0, ("%s called while tasting %s.", __func__,
517	    cp->provider->name));
518}
519
520static struct g_geom *
521g_llvm_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
522{
523	struct g_consumer *cp;
524	struct g_geom *gp;
525	struct g_llvm_label ll;
526	struct g_llvm_metadata md;
527	struct g_llvm_vg *vg;
528	int error;
529
530	bzero(&md, sizeof(md));
531
532	g_topology_assert();
533	g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
534	gp = g_new_geomf(mp, "linux_lvm:taste");
535	/* This orphan function should be never called. */
536	gp->orphan = g_llvm_taste_orphan;
537	cp = g_new_consumer(gp);
538	g_attach(cp, pp);
539	error = g_llvm_read_label(cp, &ll);
540	if (!error)
541		error = g_llvm_read_md(cp, &md, &ll);
542	g_detach(cp);
543	g_destroy_consumer(cp);
544	g_destroy_geom(gp);
545	if (error != 0)
546		return (NULL);
547
548	vg = md.md_vg;
549	if (vg->vg_geom == NULL) {
550		/* new volume group */
551		gp = g_new_geomf(mp, "%s", vg->vg_name);
552		gp->start = g_llvm_start;
553		gp->spoiled = g_llvm_orphan;
554		gp->orphan = g_llvm_orphan;
555		gp->access = g_llvm_access;
556		vg->vg_sectorsize = pp->sectorsize;
557		vg->vg_extentsize *= vg->vg_sectorsize;
558		vg->vg_geom = gp;
559		gp->softc = vg;
560		G_LLVM_DEBUG(1, "Created volume %s, extent size %zuK",
561		    vg->vg_name, vg->vg_extentsize / 1024);
562	}
563
564	/* initialise this disk in the volume group */
565	g_llvm_add_disk(vg, pp, ll.ll_uuid);
566	return (vg->vg_geom);
567}
568
569static int
570g_llvm_destroy(struct g_llvm_vg *vg, int force)
571{
572	struct g_provider *pp;
573	struct g_geom *gp;
574
575	g_topology_assert();
576	if (vg == NULL)
577		return (ENXIO);
578	gp = vg->vg_geom;
579
580	LIST_FOREACH(pp, &gp->provider, provider) {
581		if (pp->acr != 0 || pp->acw != 0 || pp->ace != 0) {
582			G_LLVM_DEBUG(1, "Device %s is still open (r%dw%de%d)",
583			    pp->name, pp->acr, pp->acw, pp->ace);
584			if (!force)
585				return (EBUSY);
586		}
587	}
588
589	g_llvm_free_vg(gp->softc);
590	gp->softc = NULL;
591	g_wither_geom(gp, ENXIO);
592	return (0);
593}
594
595static int
596g_llvm_destroy_geom(struct gctl_req *req __unused, struct g_class *mp __unused,
597    struct g_geom *gp)
598{
599	struct g_llvm_vg *vg;
600
601	vg = gp->softc;
602	return (g_llvm_destroy(vg, 0));
603}
604
605int
606g_llvm_read_label(struct g_consumer *cp, struct g_llvm_label *ll)
607{
608	struct g_provider *pp;
609	u_char *buf;
610	int i, error = 0;
611
612	g_topology_assert();
613
614	/* The LVM label is stored on the first four sectors */
615	error = g_access(cp, 1, 0, 0);
616	if (error != 0)
617		return (error);
618	pp = cp->provider;
619	g_topology_unlock();
620	buf = g_read_data(cp, 0, pp->sectorsize * 4, &error);
621	g_topology_lock();
622	g_access(cp, -1, 0, 0);
623	if (buf == NULL) {
624		G_LLVM_DEBUG(1, "Cannot read metadata from %s (error=%d)",
625		    pp->name, error);
626		return (error);
627	}
628
629	/* Search the four sectors for the LVM label. */
630	for (i = 0; i < 4; i++) {
631		error = llvm_label_decode(&buf[i * pp->sectorsize], ll, i);
632		if (error == 0)
633			break;	/* found it */
634	}
635	g_free(buf);
636	return (error);
637}
638
639int
640g_llvm_read_md(struct g_consumer *cp, struct g_llvm_metadata *md,
641    struct g_llvm_label *ll)
642{
643	struct g_provider *pp;
644	u_char *buf;
645	int error;
646	int size;
647
648	g_topology_assert();
649
650	error = g_access(cp, 1, 0, 0);
651	if (error != 0)
652		return (error);
653	pp = cp->provider;
654	g_topology_unlock();
655	buf = g_read_data(cp, ll->ll_md_offset, pp->sectorsize, &error);
656	g_topology_lock();
657	g_access(cp, -1, 0, 0);
658	if (buf == NULL) {
659		G_LLVM_DEBUG(0, "Cannot read metadata from %s (error=%d)",
660		    cp->provider->name, error);
661		return (error);
662	}
663
664	error = llvm_md_decode(buf, md, ll);
665	g_free(buf);
666	if (error != 0) {
667		return (error);
668	}
669
670	G_LLVM_DEBUG(1, "reading LVM2 config @ %s:%ju", pp->name,
671		    ll->ll_md_offset + md->md_reloffset);
672	error = g_access(cp, 1, 0, 0);
673	if (error != 0)
674		return (error);
675	pp = cp->provider;
676	g_topology_unlock();
677	/* round up to the nearest sector */
678	size = md->md_relsize +
679	    (pp->sectorsize - md->md_relsize % pp->sectorsize);
680	buf = g_read_data(cp, ll->ll_md_offset + md->md_reloffset, size, &error);
681	g_topology_lock();
682	g_access(cp, -1, 0, 0);
683	if (buf == NULL) {
684		G_LLVM_DEBUG(0, "Cannot read LVM2 config from %s (error=%d)",
685		    pp->name, error);
686		return (error);
687	}
688	buf[md->md_relsize] = '\0';
689	G_LLVM_DEBUG(10, "LVM config:\n%s\n", buf);
690	error = llvm_textconf_decode(buf, md->md_relsize, md);
691	g_free(buf);
692
693	return (error);
694}
695
696static int
697llvm_label_decode(const u_char *data, struct g_llvm_label *ll, int sector)
698{
699	uint64_t off;
700	char *uuid;
701
702	/* Magic string */
703	if (bcmp("LABELONE", data , 8) != 0)
704		return (EINVAL);
705
706	/* We only support LVM2 text format */
707	if (bcmp("LVM2 001", data + 24, 8) != 0) {
708		G_LLVM_DEBUG(0, "Unsupported LVM format");
709		return (EINVAL);
710	}
711
712	ll->ll_sector = le64dec(data + 8);
713	ll->ll_crc = le32dec(data + 16);
714	ll->ll_offset = le32dec(data + 20);
715
716	if (ll->ll_sector != sector) {
717		G_LLVM_DEBUG(0, "Expected sector %ju, found at %d",
718		    ll->ll_sector, sector);
719		return (EINVAL);
720	}
721
722	off = ll->ll_offset;
723	/*
724	 * convert the binary uuid to string format, the format is
725	 * xxxxxx-xxxx-xxxx-xxxx-xxxx-xxxx-xxxxxx (6-4-4-4-4-4-6)
726	 */
727	uuid = ll->ll_uuid;
728	bcopy(data + off, uuid, 6);
729	off += 6;
730	uuid += 6;
731	*uuid++ = '-';
732	for (int i = 0; i < 5; i++) {
733		bcopy(data + off, uuid, 4);
734		off += 4;
735		uuid += 4;
736		*uuid++ = '-';
737	}
738	bcopy(data + off, uuid, 6);
739	off += 6;
740	uuid += 6;
741	*uuid++ = '\0';
742
743	ll->ll_size = le64dec(data + off);
744	off += 8;
745	ll->ll_pestart = le64dec(data + off);
746	off += 16;
747
748	/* Only one data section is supported */
749	if (le64dec(data + off) != 0) {
750		G_LLVM_DEBUG(0, "Only one data section supported");
751		return (EINVAL);
752	}
753
754	off += 16;
755	ll->ll_md_offset = le64dec(data + off);
756	off += 8;
757	ll->ll_md_size = le64dec(data + off);
758	off += 8;
759
760	G_LLVM_DEBUG(1, "LVM metadata: offset=%ju, size=%ju", ll->ll_md_offset,
761	    ll->ll_md_size);
762
763	/* Only one data section is supported */
764	if (le64dec(data + off) != 0) {
765		G_LLVM_DEBUG(0, "Only one metadata section supported");
766		return (EINVAL);
767	}
768
769	G_LLVM_DEBUG(2, "label uuid=%s", ll->ll_uuid);
770	G_LLVM_DEBUG(2, "sector=%ju, crc=%u, offset=%u, size=%ju, pestart=%ju",
771	    ll->ll_sector, ll->ll_crc, ll->ll_offset, ll->ll_size,
772	    ll->ll_pestart);
773
774	return (0);
775}
776
777static int
778llvm_md_decode(const u_char *data, struct g_llvm_metadata *md,
779    struct g_llvm_label *ll)
780{
781	uint64_t off;
782	char magic[16];
783
784	off = 0;
785	md->md_csum = le32dec(data + off);
786	off += 4;
787	bcopy(data + off, magic, 16);
788	off += 16;
789	md->md_version = le32dec(data + off);
790	off += 4;
791	md->md_start = le64dec(data + off);
792	off += 8;
793	md->md_size = le64dec(data + off);
794	off += 8;
795
796	if (bcmp(G_LLVM_MAGIC, magic, 16) != 0) {
797		G_LLVM_DEBUG(0, "Incorrect md magic number");
798		return (EINVAL);
799	}
800	if (md->md_version != 1) {
801		G_LLVM_DEBUG(0, "Incorrect md version number (%u)",
802		    md->md_version);
803		return (EINVAL);
804	}
805	if (md->md_start != ll->ll_md_offset) {
806		G_LLVM_DEBUG(0, "Incorrect md offset (%ju)", md->md_start);
807		return (EINVAL);
808	}
809
810	/* Aparently only one is ever returned */
811	md->md_reloffset = le64dec(data + off);
812	off += 8;
813	md->md_relsize = le64dec(data + off);
814	off += 16;	/* XXX skipped checksum */
815
816	if (le64dec(data + off) != 0) {
817		G_LLVM_DEBUG(0, "Only one reloc supported");
818		return (EINVAL);
819	}
820
821	G_LLVM_DEBUG(3, "reloc: offset=%ju, size=%ju",
822	    md->md_reloffset, md->md_relsize);
823	G_LLVM_DEBUG(3, "md: version=%u, start=%ju, size=%ju",
824	    md->md_version, md->md_start, md->md_size);
825
826	return (0);
827}
828
829#define	GRAB_INT(key, tok1, tok2, v)					\
830	if (tok1 && tok2 && strncmp(tok1, key, sizeof(key)) == 0) {	\
831		v = strtol(tok2, &tok1, 10);				\
832		if (tok1 == tok2)					\
833			/* strtol did not eat any of the buffer */	\
834			goto bad;					\
835		continue;						\
836	}
837
838#define	GRAB_STR(key, tok1, tok2, v, len)				\
839	if (tok1 && tok2 && strncmp(tok1, key, sizeof(key)) == 0) {	\
840		strsep(&tok2, "\"");					\
841		if (tok2 == NULL)					\
842			continue;					\
843		tok1 = strsep(&tok2, "\"");				\
844		if (tok2 == NULL)					\
845			continue;					\
846		strncpy(v, tok1, len);					\
847		continue;						\
848	}
849
850#define	SPLIT(key, value, str)						\
851	key = strsep(&value, str);					\
852	/* strip trailing whitespace on the key */			\
853	for (char *t = key; *t != '\0'; t++)				\
854		if (isspace(*t)) {					\
855			*t = '\0';					\
856			break;						\
857		}
858
859static size_t
860llvm_grab_name(char *name, const char *tok)
861{
862	size_t len;
863
864	len = 0;
865	if (tok == NULL)
866		return (0);
867	if (tok[0] == '-')
868		return (0);
869	if (strcmp(tok, ".") == 0 || strcmp(tok, "..") == 0)
870		return (0);
871	while (tok[len] && (isalpha(tok[len]) || isdigit(tok[len]) ||
872	    tok[len] == '.' || tok[len] == '_' || tok[len] == '-' ||
873	    tok[len] == '+') && len < G_LLVM_NAMELEN - 1)
874		len++;
875	bcopy(tok, name, len);
876	name[len] = '\0';
877	return (len);
878}
879
880static int
881llvm_textconf_decode(u_char *data, int buflen, struct g_llvm_metadata *md)
882{
883	struct g_llvm_vg	*vg;
884	char *buf = data;
885	char *tok, *v;
886	char name[G_LLVM_NAMELEN];
887	char uuid[G_LLVM_UUIDLEN];
888	size_t len;
889
890	if (buf == NULL || *buf == '\0')
891		return (EINVAL);
892
893	tok = strsep(&buf, "\n");
894	if (tok == NULL)
895		return (EINVAL);
896	len = llvm_grab_name(name, tok);
897	if (len == 0)
898		return (EINVAL);
899
900	/* check too see if the vg has already been loaded off another disk */
901	LIST_FOREACH(vg, &vg_list, vg_next) {
902		if (strcmp(vg->vg_name, name) == 0) {
903			uuid[0] = '\0';
904			/* grab the volume group uuid */
905			while ((tok = strsep(&buf, "\n")) != NULL) {
906				if (strstr(tok, "{"))
907					break;
908				if (strstr(tok, "=")) {
909					SPLIT(v, tok, "=");
910					GRAB_STR("id", v, tok, uuid,
911					    sizeof(uuid));
912				}
913			}
914			if (strcmp(vg->vg_uuid, uuid) == 0) {
915				/* existing vg */
916				md->md_vg = vg;
917				return (0);
918			}
919			/* XXX different volume group with name clash! */
920			G_LLVM_DEBUG(0,
921			    "%s already exists, volume group not loaded", name);
922			return (EINVAL);
923		}
924	}
925
926	vg = malloc(sizeof(*vg), M_GLLVM, M_NOWAIT|M_ZERO);
927	if (vg == NULL)
928		return (ENOMEM);
929
930	strncpy(vg->vg_name, name, sizeof(vg->vg_name));
931	LIST_INIT(&vg->vg_pvs);
932	LIST_INIT(&vg->vg_lvs);
933
934#define	VOL_FOREACH(func, tok, buf, p)					\
935	while ((tok = strsep(buf, "\n")) != NULL) {			\
936		if (strstr(tok, "{")) {					\
937			func(buf, tok, p);				\
938			continue;					\
939		}							\
940		if (strstr(tok, "}"))					\
941			break;						\
942	}
943
944	while ((tok = strsep(&buf, "\n")) != NULL) {
945		if (strcmp(tok, "physical_volumes {") == 0) {
946			VOL_FOREACH(llvm_textconf_decode_pv, tok, &buf, vg);
947			continue;
948		}
949		if (strcmp(tok, "logical_volumes {") == 0) {
950			VOL_FOREACH(llvm_textconf_decode_lv, tok, &buf, vg);
951			continue;
952		}
953		if (strstr(tok, "{")) {
954			G_LLVM_DEBUG(2, "unknown section %s", tok);
955			continue;
956		}
957
958		/* parse 'key = value' lines */
959		if (strstr(tok, "=")) {
960			SPLIT(v, tok, "=");
961			GRAB_STR("id", v, tok, vg->vg_uuid, sizeof(vg->vg_uuid));
962			GRAB_INT("extent_size", v, tok, vg->vg_extentsize);
963			continue;
964		}
965	}
966	/* basic checking */
967	if (vg->vg_extentsize == 0)
968		goto bad;
969
970	md->md_vg = vg;
971	LIST_INSERT_HEAD(&vg_list, vg, vg_next);
972	G_LLVM_DEBUG(3, "vg: name=%s uuid=%s", vg->vg_name, vg->vg_uuid);
973	return(0);
974
975bad:
976	g_llvm_free_vg(vg);
977	return (-1);
978}
979#undef	VOL_FOREACH
980
981static int
982llvm_textconf_decode_pv(char **buf, char *tok, struct g_llvm_vg *vg)
983{
984	struct g_llvm_pv	*pv;
985	char *v;
986	size_t len;
987
988	if (*buf == NULL || **buf == '\0')
989		return (EINVAL);
990
991	pv = malloc(sizeof(*pv), M_GLLVM, M_NOWAIT|M_ZERO);
992	if (pv == NULL)
993		return (ENOMEM);
994
995	pv->pv_vg = vg;
996	len = 0;
997	if (tok == NULL)
998		goto bad;
999	len = llvm_grab_name(pv->pv_name, tok);
1000	if (len == 0)
1001		goto bad;
1002
1003	while ((tok = strsep(buf, "\n")) != NULL) {
1004		if (strstr(tok, "{"))
1005			goto bad;
1006
1007		if (strstr(tok, "}"))
1008			break;
1009
1010		/* parse 'key = value' lines */
1011		if (strstr(tok, "=")) {
1012			SPLIT(v, tok, "=");
1013			GRAB_STR("id", v, tok, pv->pv_uuid, sizeof(pv->pv_uuid));
1014			GRAB_INT("pe_start", v, tok, pv->pv_start);
1015			GRAB_INT("pe_count", v, tok, pv->pv_count);
1016			continue;
1017		}
1018	}
1019	if (tok == NULL)
1020		goto bad;
1021	/* basic checking */
1022	if (pv->pv_count == 0)
1023		goto bad;
1024
1025	LIST_INSERT_HEAD(&vg->vg_pvs, pv, pv_next);
1026	G_LLVM_DEBUG(3, "pv: name=%s uuid=%s", pv->pv_name, pv->pv_uuid);
1027
1028	return (0);
1029bad:
1030	free(pv, M_GLLVM);
1031	return (-1);
1032}
1033
1034static int
1035llvm_textconf_decode_lv(char **buf, char *tok, struct g_llvm_vg *vg)
1036{
1037	struct g_llvm_lv	*lv;
1038	struct g_llvm_segment *sg;
1039	char *v;
1040	size_t len;
1041
1042	if (*buf == NULL || **buf == '\0')
1043		return (EINVAL);
1044
1045	lv = malloc(sizeof(*lv), M_GLLVM, M_NOWAIT|M_ZERO);
1046	if (lv == NULL)
1047		return (ENOMEM);
1048
1049	lv->lv_vg = vg;
1050	LIST_INIT(&lv->lv_segs);
1051
1052	if (tok == NULL)
1053		goto bad;
1054	len = llvm_grab_name(lv->lv_name, tok);
1055	if (len == 0)
1056		goto bad;
1057
1058	while ((tok = strsep(buf, "\n")) != NULL) {
1059		if (strstr(tok, "{")) {
1060			if (strstr(tok, "segment")) {
1061				llvm_textconf_decode_sg(buf, tok, lv);
1062				continue;
1063			} else
1064				/* unexpected section */
1065				goto bad;
1066		}
1067
1068		if (strstr(tok, "}"))
1069			break;
1070
1071		/* parse 'key = value' lines */
1072		if (strstr(tok, "=")) {
1073			SPLIT(v, tok, "=");
1074			GRAB_STR("id", v, tok, lv->lv_uuid, sizeof(lv->lv_uuid));
1075			GRAB_INT("segment_count", v, tok, lv->lv_sgcount);
1076			continue;
1077		}
1078	}
1079	if (tok == NULL)
1080		goto bad;
1081	if (lv->lv_sgcount == 0 || lv->lv_sgcount != lv->lv_numsegs)
1082		/* zero or incomplete segment list */
1083		goto bad;
1084
1085	/* Optimize for only one segment on the pv */
1086	lv->lv_firstsg = LIST_FIRST(&lv->lv_segs);
1087	LIST_INSERT_HEAD(&vg->vg_lvs, lv, lv_next);
1088	G_LLVM_DEBUG(3, "lv: name=%s uuid=%s", lv->lv_name, lv->lv_uuid);
1089
1090	return (0);
1091bad:
1092	while ((sg = LIST_FIRST(&lv->lv_segs)) != NULL) {
1093		LIST_REMOVE(sg, sg_next);
1094		free(sg, M_GLLVM);
1095	}
1096	free(lv, M_GLLVM);
1097	return (-1);
1098}
1099
1100static int
1101llvm_textconf_decode_sg(char **buf, char *tok, struct g_llvm_lv *lv)
1102{
1103	struct g_llvm_segment *sg;
1104	char *v;
1105	int count = 0;
1106
1107	if (*buf == NULL || **buf == '\0')
1108		return (EINVAL);
1109
1110	sg = malloc(sizeof(*sg), M_GLLVM, M_NOWAIT|M_ZERO);
1111	if (sg == NULL)
1112		return (ENOMEM);
1113
1114	while ((tok = strsep(buf, "\n")) != NULL) {
1115		/* only a single linear stripe is supported */
1116		if (strstr(tok, "stripe_count")) {
1117			SPLIT(v, tok, "=");
1118			GRAB_INT("stripe_count", v, tok, count);
1119			if (count != 1)
1120				goto bad;
1121		}
1122
1123		if (strstr(tok, "{"))
1124			goto bad;
1125
1126		if (strstr(tok, "}"))
1127			break;
1128
1129		if (strcmp(tok, "stripes = [") == 0) {
1130			tok = strsep(buf, "\n");
1131			if (tok == NULL)
1132				goto bad;
1133
1134			strsep(&tok, "\"");
1135			if (tok == NULL)
1136				goto bad;	/* missing open quotes */
1137			v = strsep(&tok, "\"");
1138			if (tok == NULL)
1139				goto bad;	/* missing close quotes */
1140			strncpy(sg->sg_pvname, v, sizeof(sg->sg_pvname));
1141			if (*tok != ',')
1142				goto bad;	/* missing comma for stripe */
1143			tok++;
1144
1145			sg->sg_pvstart = strtol(tok, &v, 10);
1146			if (v == tok)
1147				/* strtol did not eat any of the buffer */
1148				goto bad;
1149
1150			continue;
1151		}
1152
1153		/* parse 'key = value' lines */
1154		if (strstr(tok, "=")) {
1155			SPLIT(v, tok, "=");
1156			GRAB_INT("start_extent", v, tok, sg->sg_start);
1157			GRAB_INT("extent_count", v, tok, sg->sg_count);
1158			continue;
1159		}
1160	}
1161	if (tok == NULL)
1162		goto bad;
1163	/* basic checking */
1164	if (count != 1 || sg->sg_count == 0)
1165		goto bad;
1166
1167	sg->sg_end = sg->sg_start + sg->sg_count - 1;
1168	lv->lv_numsegs++;
1169	lv->lv_extentcount += sg->sg_count;
1170	LIST_INSERT_HEAD(&lv->lv_segs, sg, sg_next);
1171
1172	return (0);
1173bad:
1174	free(sg, M_GLLVM);
1175	return (-1);
1176}
1177#undef	GRAB_INT
1178#undef	GRAB_STR
1179#undef	SPLIT
1180
1181static struct g_class g_llvm_class = {
1182	.name = G_LLVM_CLASS_NAME,
1183	.version = G_VERSION,
1184	.init = g_llvm_init,
1185	.taste = g_llvm_taste,
1186	.destroy_geom = g_llvm_destroy_geom
1187};
1188
1189DECLARE_GEOM_CLASS(g_llvm_class, g_linux_lvm);
1190