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