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