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