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