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