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