2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2008 Andrew Thompson <thompsa@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/ctype.h>
33 #include <sys/param.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>
42 #include <geom/geom.h>
43 #include <geom/geom_dbg.h>
44 #include <sys/endian.h>
46 #include <geom/linux_lvm/g_linux_lvm.h>
48 FEATURE(geom_linux_lvm, "GEOM Linux LVM partitioning support");
50 /* Declare malloc(9) label */
51 static MALLOC_DEFINE(M_GLLVM, "gllvm", "GEOM_LINUX_LVM Data");
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;
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 *);
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 *);
81 SYSCTL_DECL(_kern_geom);
82 SYSCTL_NODE(_kern_geom, OID_AUTO, linux_lvm, CTLFLAG_RW | CTLFLAG_MPSAFE, 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,
88 LIST_HEAD(, g_llvm_vg) vg_list;
91 * Called to notify geom when it's been opened, and for what intent
94 g_llvm_access(struct g_provider *pp, int dr, int dw, int de)
101 KASSERT(pp != NULL, ("%s: NULL provider", __func__));
103 KASSERT(gp != NULL, ("%s: NULL geom", __func__));
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);
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)
118 "Device %s definitely destroyed", pp->name);
122 /* Grab an exclusive bit to propagate on our consumers on first open */
123 if (pp->acr == 0 && pp->acw == 0 && pp->ace == 0)
125 /* ... drop it on close */
126 if (pp->acr + dr == 0 && pp->acw + dw == 0 && pp->ace + de == 0)
130 LIST_FOREACH(c, &gp->consumer, consumer) {
131 KASSERT(c != NULL, ("%s: consumer is NULL", __func__));
132 error = g_access(c, dr, dw, de);
134 struct g_consumer *c2;
136 /* Backout earlier changes */
137 LIST_FOREACH(c2, &gp->consumer, consumer) {
138 if (c2 == c) /* all eariler components fixed */
140 g_access(c2, -dr, -dw, -de);
149 * Dismantle bio_queue and destroy its components
152 bioq_dismantle(struct bio_queue_head *bq)
156 for (b = bioq_first(bq); b != NULL; b = bioq_first(bq)) {
164 * Can't use standard handler because one requested IO may
165 * fork into additional data IOs
168 g_llvm_done(struct bio *b)
170 struct bio *parent_b;
172 parent_b = b->bio_parent;
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,
178 if (parent_b->bio_error == 0)
179 parent_b->bio_error = b->bio_error;
182 parent_b->bio_inbed++;
183 parent_b->bio_completed += b->bio_completed;
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);
193 g_llvm_start(struct bio *bp)
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;
201 struct bio_queue_head bq;
203 off_t offset, length;
209 vg = pp->geom->softc;
211 switch (bp->bio_cmd) {
215 /* XXX BIO_GETATTR allowed? */
219 * BIO_SPEEDUP and BIO_FLUSH should pass through to all sg
220 * elements, but aren't.
222 g_io_deliver(bp, EOPNOTSUPP);
228 chunk_size = vg->vg_extentsize;
230 offset = bp->bio_offset; /* virtual offset and length */
231 length = bp->bio_length;
234 size_t chunk_index, in_chunk_offset, in_chunk_length;
237 cb = g_clone_bio(bp);
240 if (bp->bio_error == 0)
241 bp->bio_error = ENOMEM;
242 g_io_deliver(bp, bp->bio_error);
246 /* get the segment and the pv */
247 if (lv->lv_sgcount == 1) {
248 /* skip much of the calculations for a single sg */
251 in_chunk_length = length;
254 cb->bio_offset = offset + sg->sg_pvoffset;
256 chunk_index = offset / chunk_size; /* round downwards */
257 in_chunk_offset = offset % chunk_size;
259 min(length, chunk_size - in_chunk_offset);
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;
272 (off_t)chunk_index * (off_t)chunk_size
273 + in_chunk_offset + sg->sg_pvoffset;
276 KASSERT(pv != NULL, ("Can't find PV for chunk %zu",
279 cb->bio_to = pv->pv_gprov;
280 cb->bio_done = g_llvm_done;
281 cb->bio_length = in_chunk_length;
283 cb->bio_caller1 = pv;
284 bioq_disksort(&bq, cb);
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);
293 addr += in_chunk_length;
294 length -= in_chunk_length;
295 offset += in_chunk_length;
298 /* Fire off bio's here */
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);
309 if (count == 0) { /* We handled everything locally */
310 bp->bio_completed = bp->bio_length;
316 g_llvm_remove_disk(struct g_llvm_vg *vg, struct g_consumer *cp)
318 struct g_llvm_pv *pv;
319 struct g_llvm_lv *lv;
320 struct g_llvm_segment *sg;
323 KASSERT(cp != NULL, ("Non-valid disk in %s.", __func__));
324 pv = (struct g_llvm_pv *)cp->private;
326 G_LLVM_DEBUG(0, "Disk %s removed from %s.", cp->provider->name,
329 LIST_FOREACH(lv, &vg->vg_lvs, lv_next) {
330 /* Find segments that map to this disk */
332 LIST_FOREACH(sg, &lv->lv_segs, sg_next) {
333 if (sg->sg_pv == pv) {
341 G_LLVM_DEBUG(0, "Device %s removed.",
343 g_wither_provider(lv->lv_gprov, ENXIO);
348 if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
349 g_access(cp, -cp->acr, -cp->acw, -cp->ace);
351 g_destroy_consumer(cp);
355 g_llvm_orphan(struct g_consumer *cp)
357 struct g_llvm_vg *vg;
366 g_llvm_remove_disk(vg, cp);
367 g_llvm_destroy(vg, 1);
371 g_llvm_activate_lv(struct g_llvm_vg *vg, struct g_llvm_lv *lv)
374 struct g_provider *pp;
378 KASSERT(lv->lv_sgactive == lv->lv_sgcount, ("segment missing"));
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);
388 G_LLVM_DEBUG(1, "Created %s, %juM", pp->name,
389 pp->mediasize / (1024*1024));
395 g_llvm_add_disk(struct g_llvm_vg *vg, struct g_provider *pp, char *uuid)
398 struct g_consumer *cp, *fcp;
399 struct g_llvm_pv *pv;
400 struct g_llvm_lv *lv;
401 struct g_llvm_segment *sg;
406 LIST_FOREACH(pv, &vg->vg_pvs, pv_next) {
407 if (strcmp(pv->pv_uuid, uuid) == 0)
408 break; /* found it */
411 G_LLVM_DEBUG(3, "uuid %s not found in pv list", uuid);
414 if (pv->pv_gprov != NULL) {
415 G_LLVM_DEBUG(0, "disk %s already initialised in %s",
416 pv->pv_name, vg->vg_name);
420 pv->pv_start *= vg->vg_sectorsize;
422 fcp = LIST_FIRST(&gp->consumer);
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);
430 G_LLVM_DEBUG(0, "cannot attach %s to %s",
431 pp->name, vg->vg_name);
432 g_destroy_consumer(cp);
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,
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);
449 g_destroy_consumer(cp);
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"));
467 (off_t)sg->sg_pvstart * vg->vg_extentsize
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,
477 sg->sg_pvoffset / vg->vg_sectorsize);
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);
491 g_llvm_init(struct g_class *mp)
497 g_llvm_free_vg(struct g_llvm_vg *vg)
499 struct g_llvm_pv *pv;
500 struct g_llvm_lv *lv;
501 struct g_llvm_segment *sg;
503 /* Free all the structures */
504 while ((pv = LIST_FIRST(&vg->vg_pvs)) != NULL) {
505 LIST_REMOVE(pv, pv_next);
508 while ((lv = LIST_FIRST(&vg->vg_lvs)) != NULL) {
509 while ((sg = LIST_FIRST(&lv->lv_segs)) != NULL) {
510 LIST_REMOVE(sg, sg_next);
513 LIST_REMOVE(lv, lv_next);
516 LIST_REMOVE(vg, vg_next);
521 g_llvm_taste_orphan(struct g_consumer *cp)
524 KASSERT(1 == 0, ("%s called while tasting %s.", __func__,
525 cp->provider->name));
528 static struct g_geom *
529 g_llvm_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
531 struct g_consumer *cp;
533 struct g_llvm_label ll;
534 struct g_llvm_metadata md;
535 struct g_llvm_vg *vg;
538 bzero(&md, sizeof(md));
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 error = g_attach(cp, pp);
548 error = g_llvm_read_label(cp, &ll);
550 error = g_llvm_read_md(cp, &md, &ll);
553 g_destroy_consumer(cp);
559 if (vg->vg_geom == NULL) {
560 /* new volume group */
561 gp = g_new_geomf(mp, "%s", vg->vg_name);
562 gp->start = g_llvm_start;
563 gp->spoiled = g_llvm_orphan;
564 gp->orphan = g_llvm_orphan;
565 gp->access = g_llvm_access;
566 vg->vg_sectorsize = pp->sectorsize;
567 vg->vg_extentsize *= vg->vg_sectorsize;
570 G_LLVM_DEBUG(1, "Created volume %s, extent size %zuK",
571 vg->vg_name, vg->vg_extentsize / 1024);
574 /* initialise this disk in the volume group */
575 g_llvm_add_disk(vg, pp, ll.ll_uuid);
576 return (vg->vg_geom);
580 g_llvm_destroy(struct g_llvm_vg *vg, int force)
582 struct g_provider *pp;
590 LIST_FOREACH(pp, &gp->provider, provider) {
591 if (pp->acr != 0 || pp->acw != 0 || pp->ace != 0) {
592 G_LLVM_DEBUG(1, "Device %s is still open (r%dw%de%d)",
593 pp->name, pp->acr, pp->acw, pp->ace);
599 g_llvm_free_vg(gp->softc);
601 g_wither_geom(gp, ENXIO);
606 g_llvm_destroy_geom(struct gctl_req *req __unused, struct g_class *mp __unused,
609 struct g_llvm_vg *vg;
612 return (g_llvm_destroy(vg, 0));
616 g_llvm_read_label(struct g_consumer *cp, struct g_llvm_label *ll)
618 struct g_provider *pp;
624 /* The LVM label is stored on the first four sectors */
625 error = g_access(cp, 1, 0, 0);
630 buf = g_read_data(cp, 0, pp->sectorsize * 4, &error);
632 g_access(cp, -1, 0, 0);
634 G_LLVM_DEBUG(1, "Cannot read metadata from %s (error=%d)",
639 /* Search the four sectors for the LVM label. */
640 for (i = 0; i < 4; i++) {
641 error = llvm_label_decode(&buf[i * pp->sectorsize], ll, i);
643 break; /* found it */
650 g_llvm_read_md(struct g_consumer *cp, struct g_llvm_metadata *md,
651 struct g_llvm_label *ll)
653 struct g_provider *pp;
660 error = g_access(cp, 1, 0, 0);
665 buf = g_read_data(cp, ll->ll_md_offset, pp->sectorsize, &error);
667 g_access(cp, -1, 0, 0);
669 G_LLVM_DEBUG(0, "Cannot read metadata from %s (error=%d)",
670 cp->provider->name, error);
674 error = llvm_md_decode(buf, md, ll);
680 G_LLVM_DEBUG(1, "reading LVM2 config @ %s:%ju", pp->name,
681 ll->ll_md_offset + md->md_reloffset);
682 error = g_access(cp, 1, 0, 0);
687 /* round up to the nearest sector */
688 size = md->md_relsize +
689 (pp->sectorsize - md->md_relsize % pp->sectorsize);
690 buf = g_read_data(cp, ll->ll_md_offset + md->md_reloffset, size, &error);
692 g_access(cp, -1, 0, 0);
694 G_LLVM_DEBUG(0, "Cannot read LVM2 config from %s (error=%d)",
698 buf[md->md_relsize] = '\0';
699 G_LLVM_DEBUG(10, "LVM config:\n%s\n", buf);
700 error = llvm_textconf_decode(buf, md->md_relsize, md);
707 llvm_label_decode(const u_char *data, struct g_llvm_label *ll, int sector)
713 if (bcmp("LABELONE", data , 8) != 0)
716 /* We only support LVM2 text format */
717 if (bcmp("LVM2 001", data + 24, 8) != 0) {
718 G_LLVM_DEBUG(0, "Unsupported LVM format");
722 ll->ll_sector = le64dec(data + 8);
723 ll->ll_crc = le32dec(data + 16);
724 ll->ll_offset = le32dec(data + 20);
726 if (ll->ll_sector != sector) {
727 G_LLVM_DEBUG(0, "Expected sector %ju, found at %d",
728 ll->ll_sector, sector);
734 * convert the binary uuid to string format, the format is
735 * xxxxxx-xxxx-xxxx-xxxx-xxxx-xxxx-xxxxxx (6-4-4-4-4-4-6)
738 bcopy(data + off, uuid, 6);
742 for (int i = 0; i < 5; i++) {
743 bcopy(data + off, uuid, 4);
748 bcopy(data + off, uuid, 6);
753 ll->ll_size = le64dec(data + off);
755 ll->ll_pestart = le64dec(data + off);
758 /* Only one data section is supported */
759 if (le64dec(data + off) != 0) {
760 G_LLVM_DEBUG(0, "Only one data section supported");
765 ll->ll_md_offset = le64dec(data + off);
767 ll->ll_md_size = le64dec(data + off);
770 G_LLVM_DEBUG(1, "LVM metadata: offset=%ju, size=%ju", ll->ll_md_offset,
773 /* Only one data section is supported */
774 if (le64dec(data + off) != 0) {
775 G_LLVM_DEBUG(0, "Only one metadata section supported");
779 G_LLVM_DEBUG(2, "label uuid=%s", ll->ll_uuid);
780 G_LLVM_DEBUG(2, "sector=%ju, crc=%u, offset=%u, size=%ju, pestart=%ju",
781 ll->ll_sector, ll->ll_crc, ll->ll_offset, ll->ll_size,
788 llvm_md_decode(const u_char *data, struct g_llvm_metadata *md,
789 struct g_llvm_label *ll)
795 md->md_csum = le32dec(data + off);
797 bcopy(data + off, magic, 16);
799 md->md_version = le32dec(data + off);
801 md->md_start = le64dec(data + off);
803 md->md_size = le64dec(data + off);
806 if (bcmp(G_LLVM_MAGIC, magic, 16) != 0) {
807 G_LLVM_DEBUG(0, "Incorrect md magic number");
810 if (md->md_version != 1) {
811 G_LLVM_DEBUG(0, "Incorrect md version number (%u)",
815 if (md->md_start != ll->ll_md_offset) {
816 G_LLVM_DEBUG(0, "Incorrect md offset (%ju)", md->md_start);
820 /* Aparently only one is ever returned */
821 md->md_reloffset = le64dec(data + off);
823 md->md_relsize = le64dec(data + off);
824 off += 16; /* XXX skipped checksum */
826 if (le64dec(data + off) != 0) {
827 G_LLVM_DEBUG(0, "Only one reloc supported");
831 G_LLVM_DEBUG(3, "reloc: offset=%ju, size=%ju",
832 md->md_reloffset, md->md_relsize);
833 G_LLVM_DEBUG(3, "md: version=%u, start=%ju, size=%ju",
834 md->md_version, md->md_start, md->md_size);
839 #define GRAB_INT(key, tok1, tok2, v) \
840 if (tok1 && tok2 && strncmp(tok1, key, sizeof(key)) == 0) { \
841 v = strtol(tok2, &tok1, 10); \
843 /* strtol did not eat any of the buffer */ \
848 #define GRAB_STR(key, tok1, tok2, v, len) \
849 if (tok1 && tok2 && strncmp(tok1, key, sizeof(key)) == 0) { \
850 strsep(&tok2, "\""); \
853 tok1 = strsep(&tok2, "\""); \
856 strncpy(v, tok1, len); \
860 #define SPLIT(key, value, str) \
861 key = strsep(&value, str); \
862 /* strip trailing whitespace on the key */ \
863 for (char *t = key; *t != '\0'; t++) \
870 llvm_grab_name(char *name, const char *tok)
879 if (strcmp(tok, ".") == 0 || strcmp(tok, "..") == 0)
881 while (tok[len] && (isalpha(tok[len]) || isdigit(tok[len]) ||
882 tok[len] == '.' || tok[len] == '_' || tok[len] == '-' ||
883 tok[len] == '+') && len < G_LLVM_NAMELEN - 1)
885 bcopy(tok, name, len);
891 llvm_textconf_decode(u_char *data, int buflen, struct g_llvm_metadata *md)
893 struct g_llvm_vg *vg;
896 char name[G_LLVM_NAMELEN];
897 char uuid[G_LLVM_UUIDLEN];
900 if (buf == NULL || *buf == '\0')
903 tok = strsep(&buf, "\n");
906 len = llvm_grab_name(name, tok);
910 /* check too see if the vg has already been loaded off another disk */
911 LIST_FOREACH(vg, &vg_list, vg_next) {
912 if (strcmp(vg->vg_name, name) == 0) {
914 /* grab the volume group uuid */
915 while ((tok = strsep(&buf, "\n")) != NULL) {
916 if (strstr(tok, "{"))
918 if (strstr(tok, "=")) {
920 GRAB_STR("id", v, tok, uuid,
924 if (strcmp(vg->vg_uuid, uuid) == 0) {
929 /* XXX different volume group with name clash! */
931 "%s already exists, volume group not loaded", name);
936 vg = malloc(sizeof(*vg), M_GLLVM, M_NOWAIT|M_ZERO);
940 strncpy(vg->vg_name, name, sizeof(vg->vg_name));
941 LIST_INIT(&vg->vg_pvs);
942 LIST_INIT(&vg->vg_lvs);
944 #define VOL_FOREACH(func, tok, buf, p) \
945 while ((tok = strsep(buf, "\n")) != NULL) { \
946 if (strstr(tok, "{")) { \
950 if (strstr(tok, "}")) \
954 while ((tok = strsep(&buf, "\n")) != NULL) {
955 if (strcmp(tok, "physical_volumes {") == 0) {
956 VOL_FOREACH(llvm_textconf_decode_pv, tok, &buf, vg);
959 if (strcmp(tok, "logical_volumes {") == 0) {
960 VOL_FOREACH(llvm_textconf_decode_lv, tok, &buf, vg);
963 if (strstr(tok, "{")) {
964 G_LLVM_DEBUG(2, "unknown section %s", tok);
968 /* parse 'key = value' lines */
969 if (strstr(tok, "=")) {
971 GRAB_STR("id", v, tok, vg->vg_uuid, sizeof(vg->vg_uuid));
972 GRAB_INT("extent_size", v, tok, vg->vg_extentsize);
977 if (vg->vg_extentsize == 0)
981 LIST_INSERT_HEAD(&vg_list, vg, vg_next);
982 G_LLVM_DEBUG(3, "vg: name=%s uuid=%s", vg->vg_name, vg->vg_uuid);
992 llvm_textconf_decode_pv(char **buf, char *tok, struct g_llvm_vg *vg)
994 struct g_llvm_pv *pv;
998 if (*buf == NULL || **buf == '\0')
1001 pv = malloc(sizeof(*pv), M_GLLVM, M_NOWAIT|M_ZERO);
1009 len = llvm_grab_name(pv->pv_name, tok);
1013 while ((tok = strsep(buf, "\n")) != NULL) {
1014 if (strstr(tok, "{"))
1017 if (strstr(tok, "}"))
1020 /* parse 'key = value' lines */
1021 if (strstr(tok, "=")) {
1023 GRAB_STR("id", v, tok, pv->pv_uuid, sizeof(pv->pv_uuid));
1024 GRAB_INT("pe_start", v, tok, pv->pv_start);
1025 GRAB_INT("pe_count", v, tok, pv->pv_count);
1031 /* basic checking */
1032 if (pv->pv_count == 0)
1035 LIST_INSERT_HEAD(&vg->vg_pvs, pv, pv_next);
1036 G_LLVM_DEBUG(3, "pv: name=%s uuid=%s", pv->pv_name, pv->pv_uuid);
1045 llvm_textconf_decode_lv(char **buf, char *tok, struct g_llvm_vg *vg)
1047 struct g_llvm_lv *lv;
1048 struct g_llvm_segment *sg;
1052 if (*buf == NULL || **buf == '\0')
1055 lv = malloc(sizeof(*lv), M_GLLVM, M_NOWAIT|M_ZERO);
1060 LIST_INIT(&lv->lv_segs);
1064 len = llvm_grab_name(lv->lv_name, tok);
1068 while ((tok = strsep(buf, "\n")) != NULL) {
1069 if (strstr(tok, "{")) {
1070 if (strstr(tok, "segment")) {
1071 llvm_textconf_decode_sg(buf, tok, lv);
1074 /* unexpected section */
1078 if (strstr(tok, "}"))
1081 /* parse 'key = value' lines */
1082 if (strstr(tok, "=")) {
1084 GRAB_STR("id", v, tok, lv->lv_uuid, sizeof(lv->lv_uuid));
1085 GRAB_INT("segment_count", v, tok, lv->lv_sgcount);
1091 if (lv->lv_sgcount == 0 || lv->lv_sgcount != lv->lv_numsegs)
1092 /* zero or incomplete segment list */
1095 /* Optimize for only one segment on the pv */
1096 lv->lv_firstsg = LIST_FIRST(&lv->lv_segs);
1097 LIST_INSERT_HEAD(&vg->vg_lvs, lv, lv_next);
1098 G_LLVM_DEBUG(3, "lv: name=%s uuid=%s", lv->lv_name, lv->lv_uuid);
1102 while ((sg = LIST_FIRST(&lv->lv_segs)) != NULL) {
1103 LIST_REMOVE(sg, sg_next);
1111 llvm_textconf_decode_sg(char **buf, char *tok, struct g_llvm_lv *lv)
1113 struct g_llvm_segment *sg;
1117 if (*buf == NULL || **buf == '\0')
1120 sg = malloc(sizeof(*sg), M_GLLVM, M_NOWAIT|M_ZERO);
1124 while ((tok = strsep(buf, "\n")) != NULL) {
1125 /* only a single linear stripe is supported */
1126 if (strstr(tok, "stripe_count")) {
1128 GRAB_INT("stripe_count", v, tok, count);
1133 if (strstr(tok, "{"))
1136 if (strstr(tok, "}"))
1139 if (strcmp(tok, "stripes = [") == 0) {
1140 tok = strsep(buf, "\n");
1146 goto bad; /* missing open quotes */
1147 v = strsep(&tok, "\"");
1149 goto bad; /* missing close quotes */
1150 strncpy(sg->sg_pvname, v, sizeof(sg->sg_pvname));
1152 goto bad; /* missing comma for stripe */
1155 sg->sg_pvstart = strtol(tok, &v, 10);
1157 /* strtol did not eat any of the buffer */
1163 /* parse 'key = value' lines */
1164 if (strstr(tok, "=")) {
1166 GRAB_INT("start_extent", v, tok, sg->sg_start);
1167 GRAB_INT("extent_count", v, tok, sg->sg_count);
1173 /* basic checking */
1174 if (count != 1 || sg->sg_count == 0)
1177 sg->sg_end = sg->sg_start + sg->sg_count - 1;
1179 lv->lv_extentcount += sg->sg_count;
1180 LIST_INSERT_HEAD(&lv->lv_segs, sg, sg_next);
1191 static struct g_class g_llvm_class = {
1192 .name = G_LLVM_CLASS_NAME,
1193 .version = G_VERSION,
1194 .init = g_llvm_init,
1195 .taste = g_llvm_taste,
1196 .destroy_geom = g_llvm_destroy_geom
1199 DECLARE_GEOM_CLASS(g_llvm_class, g_linux_lvm);
1200 MODULE_VERSION(geom_linux_lvm, 0);