1 /*- 2 * Copyright (c) 2010 Edward Tomasz Napierala <trasz@FreeBSD.org> 3 * Copyright (c) 2004-2006 Pawel Jakub Dawidek <pjd@FreeBSD.org> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 #include <sys/param.h> 32 #include <sys/systm.h> 33 #include <sys/kernel.h> 34 #include <sys/module.h> 35 #include <sys/lock.h> 36 #include <sys/mutex.h> 37 #include <sys/bio.h> 38 #include <sys/disk.h> 39 #include <sys/proc.h> 40 #include <sys/sbuf.h> 41 #include <sys/sysctl.h> 42 #include <sys/malloc.h> 43 #include <sys/eventhandler.h> 44 #include <geom/geom.h> 45 #include <geom/mountver/g_mountver.h> 46 47 48 SYSCTL_DECL(_kern_geom); 49 static SYSCTL_NODE(_kern_geom, OID_AUTO, mountver, CTLFLAG_RW, 50 0, "GEOM_MOUNTVER stuff"); 51 static u_int g_mountver_debug = 0; 52 static u_int g_mountver_check_ident = 1; 53 SYSCTL_UINT(_kern_geom_mountver, OID_AUTO, debug, CTLFLAG_RW, 54 &g_mountver_debug, 0, "Debug level"); 55 SYSCTL_UINT(_kern_geom_mountver, OID_AUTO, check_ident, CTLFLAG_RW, 56 &g_mountver_check_ident, 0, "Check disk ident when reattaching"); 57 58 static eventhandler_tag g_mountver_pre_sync = NULL; 59 60 static void g_mountver_queue(struct bio *bp); 61 static void g_mountver_orphan(struct g_consumer *cp); 62 static void g_mountver_resize(struct g_consumer *cp); 63 static int g_mountver_destroy(struct g_geom *gp, boolean_t force); 64 static g_taste_t g_mountver_taste; 65 static int g_mountver_destroy_geom(struct gctl_req *req, struct g_class *mp, 66 struct g_geom *gp); 67 static void g_mountver_config(struct gctl_req *req, struct g_class *mp, 68 const char *verb); 69 static void g_mountver_dumpconf(struct sbuf *sb, const char *indent, 70 struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp); 71 static void g_mountver_init(struct g_class *mp); 72 static void g_mountver_fini(struct g_class *mp); 73 74 struct g_class g_mountver_class = { 75 .name = G_MOUNTVER_CLASS_NAME, 76 .version = G_VERSION, 77 .ctlreq = g_mountver_config, 78 .taste = g_mountver_taste, 79 .destroy_geom = g_mountver_destroy_geom, 80 .init = g_mountver_init, 81 .fini = g_mountver_fini 82 }; 83 84 static void 85 g_mountver_done(struct bio *bp) 86 { 87 struct g_geom *gp; 88 struct bio *pbp; 89 90 if (bp->bio_error != ENXIO) { 91 g_std_done(bp); 92 return; 93 } 94 95 /* 96 * When the device goes away, it's possible that few requests 97 * will be completed with ENXIO before g_mountver_orphan() 98 * gets called. To work around that, we have to queue requests 99 * that failed with ENXIO, in order to send them later. 100 */ 101 gp = bp->bio_from->geom; 102 103 pbp = bp->bio_parent; 104 KASSERT(pbp->bio_to == LIST_FIRST(&gp->provider), 105 ("parent request was for someone else")); 106 g_destroy_bio(bp); 107 pbp->bio_inbed++; 108 g_mountver_queue(pbp); 109 } 110 111 static void 112 g_mountver_send(struct bio *bp) 113 { 114 struct g_geom *gp; 115 struct bio *cbp; 116 117 gp = bp->bio_to->geom; 118 119 cbp = g_clone_bio(bp); 120 if (cbp == NULL) { 121 g_io_deliver(bp, ENOMEM); 122 return; 123 } 124 125 cbp->bio_done = g_mountver_done; 126 g_io_request(cbp, LIST_FIRST(&gp->consumer)); 127 } 128 129 static void 130 g_mountver_queue(struct bio *bp) 131 { 132 struct g_mountver_softc *sc; 133 struct g_geom *gp; 134 135 gp = bp->bio_to->geom; 136 sc = gp->softc; 137 138 mtx_lock(&sc->sc_mtx); 139 TAILQ_INSERT_TAIL(&sc->sc_queue, bp, bio_queue); 140 mtx_unlock(&sc->sc_mtx); 141 } 142 143 static void 144 g_mountver_send_queued(struct g_geom *gp) 145 { 146 struct g_mountver_softc *sc; 147 struct bio *bp; 148 149 sc = gp->softc; 150 151 mtx_lock(&sc->sc_mtx); 152 while ((bp = TAILQ_FIRST(&sc->sc_queue)) != NULL) { 153 TAILQ_REMOVE(&sc->sc_queue, bp, bio_queue); 154 G_MOUNTVER_LOGREQ(bp, "Sending queued request."); 155 g_mountver_send(bp); 156 } 157 mtx_unlock(&sc->sc_mtx); 158 } 159 160 static void 161 g_mountver_discard_queued(struct g_geom *gp) 162 { 163 struct g_mountver_softc *sc; 164 struct bio *bp; 165 166 sc = gp->softc; 167 168 mtx_lock(&sc->sc_mtx); 169 while ((bp = TAILQ_FIRST(&sc->sc_queue)) != NULL) { 170 TAILQ_REMOVE(&sc->sc_queue, bp, bio_queue); 171 G_MOUNTVER_LOGREQ(bp, "Discarding queued request."); 172 g_io_deliver(bp, ENXIO); 173 } 174 mtx_unlock(&sc->sc_mtx); 175 } 176 177 static void 178 g_mountver_start(struct bio *bp) 179 { 180 struct g_mountver_softc *sc; 181 struct g_geom *gp; 182 183 gp = bp->bio_to->geom; 184 sc = gp->softc; 185 G_MOUNTVER_LOGREQ(bp, "Request received."); 186 187 /* 188 * It is possible that some bios were returned with ENXIO, even though 189 * orphaning didn't happen yet. In that case, queue all subsequent 190 * requests in order to maintain ordering. 191 */ 192 if (sc->sc_orphaned || !TAILQ_EMPTY(&sc->sc_queue)) { 193 if (sc->sc_shutting_down) { 194 G_MOUNTVER_LOGREQ(bp, "Discarding request due to shutdown."); 195 g_io_deliver(bp, ENXIO); 196 return; 197 } 198 G_MOUNTVER_LOGREQ(bp, "Queueing request."); 199 g_mountver_queue(bp); 200 if (!sc->sc_orphaned) 201 g_mountver_send_queued(gp); 202 } else { 203 G_MOUNTVER_LOGREQ(bp, "Sending request."); 204 g_mountver_send(bp); 205 } 206 } 207 208 static int 209 g_mountver_access(struct g_provider *pp, int dr, int dw, int de) 210 { 211 struct g_mountver_softc *sc; 212 struct g_geom *gp; 213 struct g_consumer *cp; 214 215 g_topology_assert(); 216 217 gp = pp->geom; 218 cp = LIST_FIRST(&gp->consumer); 219 sc = gp->softc; 220 if (sc == NULL && dr <= 0 && dw <= 0 && de <= 0) 221 return (0); 222 KASSERT(sc != NULL, ("Trying to access withered provider \"%s\".", pp->name)); 223 224 sc->sc_access_r += dr; 225 sc->sc_access_w += dw; 226 sc->sc_access_e += de; 227 228 if (sc->sc_orphaned) 229 return (0); 230 231 return (g_access(cp, dr, dw, de)); 232 } 233 234 static int 235 g_mountver_create(struct gctl_req *req, struct g_class *mp, struct g_provider *pp) 236 { 237 struct g_mountver_softc *sc; 238 struct g_geom *gp; 239 struct g_provider *newpp; 240 struct g_consumer *cp; 241 char name[64]; 242 int error; 243 int identsize = DISK_IDENT_SIZE; 244 245 g_topology_assert(); 246 247 gp = NULL; 248 newpp = NULL; 249 cp = NULL; 250 251 snprintf(name, sizeof(name), "%s%s", pp->name, G_MOUNTVER_SUFFIX); 252 LIST_FOREACH(gp, &mp->geom, geom) { 253 if (strcmp(gp->name, name) == 0) { 254 gctl_error(req, "Provider %s already exists.", name); 255 return (EEXIST); 256 } 257 } 258 gp = g_new_geomf(mp, "%s", name); 259 sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO); 260 mtx_init(&sc->sc_mtx, "gmountver", NULL, MTX_DEF | MTX_RECURSE); 261 TAILQ_INIT(&sc->sc_queue); 262 sc->sc_provider_name = strdup(pp->name, M_GEOM); 263 gp->softc = sc; 264 gp->start = g_mountver_start; 265 gp->orphan = g_mountver_orphan; 266 gp->resize = g_mountver_resize; 267 gp->access = g_mountver_access; 268 gp->dumpconf = g_mountver_dumpconf; 269 270 newpp = g_new_providerf(gp, "%s", gp->name); 271 newpp->mediasize = pp->mediasize; 272 newpp->sectorsize = pp->sectorsize; 273 newpp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE; 274 275 if ((pp->flags & G_PF_ACCEPT_UNMAPPED) != 0) { 276 G_MOUNTVER_DEBUG(0, "Unmapped supported for %s.", gp->name); 277 newpp->flags |= G_PF_ACCEPT_UNMAPPED; 278 } else { 279 G_MOUNTVER_DEBUG(0, "Unmapped unsupported for %s.", gp->name); 280 newpp->flags &= ~G_PF_ACCEPT_UNMAPPED; 281 } 282 283 cp = g_new_consumer(gp); 284 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE; 285 error = g_attach(cp, pp); 286 if (error != 0) { 287 gctl_error(req, "Cannot attach to provider %s.", pp->name); 288 goto fail; 289 } 290 error = g_access(cp, 1, 0, 0); 291 if (error != 0) { 292 gctl_error(req, "Cannot access provider %s.", pp->name); 293 goto fail; 294 } 295 error = g_io_getattr("GEOM::ident", cp, &identsize, sc->sc_ident); 296 g_access(cp, -1, 0, 0); 297 if (error != 0) { 298 if (g_mountver_check_ident) { 299 gctl_error(req, "Cannot get disk ident from %s; error = %d.", pp->name, error); 300 goto fail; 301 } 302 303 G_MOUNTVER_DEBUG(0, "Cannot get disk ident from %s; error = %d.", pp->name, error); 304 sc->sc_ident[0] = '\0'; 305 } 306 307 g_error_provider(newpp, 0); 308 G_MOUNTVER_DEBUG(0, "Device %s created.", gp->name); 309 return (0); 310 fail: 311 g_free(sc->sc_provider_name); 312 if (cp->provider != NULL) 313 g_detach(cp); 314 g_destroy_consumer(cp); 315 g_destroy_provider(newpp); 316 g_free(gp->softc); 317 g_destroy_geom(gp); 318 return (error); 319 } 320 321 static int 322 g_mountver_destroy(struct g_geom *gp, boolean_t force) 323 { 324 struct g_mountver_softc *sc; 325 struct g_provider *pp; 326 327 g_topology_assert(); 328 if (gp->softc == NULL) 329 return (ENXIO); 330 sc = gp->softc; 331 pp = LIST_FIRST(&gp->provider); 332 if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) { 333 if (force) { 334 G_MOUNTVER_DEBUG(0, "Device %s is still open, so it " 335 "can't be definitely removed.", pp->name); 336 } else { 337 G_MOUNTVER_DEBUG(1, "Device %s is still open (r%dw%de%d).", 338 pp->name, pp->acr, pp->acw, pp->ace); 339 return (EBUSY); 340 } 341 } else { 342 G_MOUNTVER_DEBUG(0, "Device %s removed.", gp->name); 343 } 344 if (pp != NULL) 345 g_wither_provider(pp, ENXIO); 346 g_mountver_discard_queued(gp); 347 g_free(sc->sc_provider_name); 348 g_free(gp->softc); 349 gp->softc = NULL; 350 g_wither_geom(gp, ENXIO); 351 352 return (0); 353 } 354 355 static int 356 g_mountver_destroy_geom(struct gctl_req *req, struct g_class *mp, struct g_geom *gp) 357 { 358 359 return (g_mountver_destroy(gp, 0)); 360 } 361 362 static void 363 g_mountver_ctl_create(struct gctl_req *req, struct g_class *mp) 364 { 365 struct g_provider *pp; 366 const char *name; 367 char param[16]; 368 int i, *nargs; 369 370 g_topology_assert(); 371 372 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 373 if (nargs == NULL) { 374 gctl_error(req, "No '%s' argument", "nargs"); 375 return; 376 } 377 if (*nargs <= 0) { 378 gctl_error(req, "Missing device(s)."); 379 return; 380 } 381 for (i = 0; i < *nargs; i++) { 382 snprintf(param, sizeof(param), "arg%d", i); 383 name = gctl_get_asciiparam(req, param); 384 if (name == NULL) { 385 gctl_error(req, "No 'arg%d' argument", i); 386 return; 387 } 388 if (strncmp(name, "/dev/", strlen("/dev/")) == 0) 389 name += strlen("/dev/"); 390 pp = g_provider_by_name(name); 391 if (pp == NULL) { 392 G_MOUNTVER_DEBUG(1, "Provider %s is invalid.", name); 393 gctl_error(req, "Provider %s is invalid.", name); 394 return; 395 } 396 if (g_mountver_create(req, mp, pp) != 0) 397 return; 398 } 399 } 400 401 static struct g_geom * 402 g_mountver_find_geom(struct g_class *mp, const char *name) 403 { 404 struct g_geom *gp; 405 406 LIST_FOREACH(gp, &mp->geom, geom) { 407 if (strcmp(gp->name, name) == 0) 408 return (gp); 409 } 410 return (NULL); 411 } 412 413 static void 414 g_mountver_ctl_destroy(struct gctl_req *req, struct g_class *mp) 415 { 416 int *nargs, *force, error, i; 417 struct g_geom *gp; 418 const char *name; 419 char param[16]; 420 421 g_topology_assert(); 422 423 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 424 if (nargs == NULL) { 425 gctl_error(req, "No '%s' argument", "nargs"); 426 return; 427 } 428 if (*nargs <= 0) { 429 gctl_error(req, "Missing device(s)."); 430 return; 431 } 432 force = gctl_get_paraml(req, "force", sizeof(*force)); 433 if (force == NULL) { 434 gctl_error(req, "No 'force' argument"); 435 return; 436 } 437 438 for (i = 0; i < *nargs; i++) { 439 snprintf(param, sizeof(param), "arg%d", i); 440 name = gctl_get_asciiparam(req, param); 441 if (name == NULL) { 442 gctl_error(req, "No 'arg%d' argument", i); 443 return; 444 } 445 if (strncmp(name, "/dev/", strlen("/dev/")) == 0) 446 name += strlen("/dev/"); 447 gp = g_mountver_find_geom(mp, name); 448 if (gp == NULL) { 449 G_MOUNTVER_DEBUG(1, "Device %s is invalid.", name); 450 gctl_error(req, "Device %s is invalid.", name); 451 return; 452 } 453 error = g_mountver_destroy(gp, *force); 454 if (error != 0) { 455 gctl_error(req, "Cannot destroy device %s (error=%d).", 456 gp->name, error); 457 return; 458 } 459 } 460 } 461 462 static void 463 g_mountver_orphan(struct g_consumer *cp) 464 { 465 struct g_mountver_softc *sc; 466 467 g_topology_assert(); 468 469 sc = cp->geom->softc; 470 sc->sc_orphaned = 1; 471 if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0) 472 g_access(cp, -cp->acr, -cp->acw, -cp->ace); 473 g_detach(cp); 474 G_MOUNTVER_DEBUG(0, "%s is offline. Mount verification in progress.", sc->sc_provider_name); 475 } 476 477 static void 478 g_mountver_resize(struct g_consumer *cp) 479 { 480 struct g_geom *gp; 481 struct g_provider *pp; 482 483 gp = cp->geom; 484 485 LIST_FOREACH(pp, &gp->provider, provider) 486 g_resize_provider(pp, cp->provider->mediasize); 487 } 488 489 static int 490 g_mountver_ident_matches(struct g_geom *gp) 491 { 492 struct g_consumer *cp; 493 struct g_mountver_softc *sc; 494 char ident[DISK_IDENT_SIZE]; 495 int error, identsize = DISK_IDENT_SIZE; 496 497 sc = gp->softc; 498 cp = LIST_FIRST(&gp->consumer); 499 500 if (g_mountver_check_ident == 0) 501 return (0); 502 503 error = g_access(cp, 1, 0, 0); 504 if (error != 0) { 505 G_MOUNTVER_DEBUG(0, "Cannot access %s; " 506 "not attaching; error = %d.", gp->name, error); 507 return (1); 508 } 509 error = g_io_getattr("GEOM::ident", cp, &identsize, ident); 510 g_access(cp, -1, 0, 0); 511 if (error != 0) { 512 G_MOUNTVER_DEBUG(0, "Cannot get disk ident for %s; " 513 "not attaching; error = %d.", gp->name, error); 514 return (1); 515 } 516 if (strcmp(ident, sc->sc_ident) != 0) { 517 G_MOUNTVER_DEBUG(1, "Disk ident for %s (\"%s\") is different " 518 "from expected \"%s\", not attaching.", gp->name, ident, 519 sc->sc_ident); 520 return (1); 521 } 522 523 return (0); 524 } 525 526 static struct g_geom * 527 g_mountver_taste(struct g_class *mp, struct g_provider *pp, int flags __unused) 528 { 529 struct g_mountver_softc *sc; 530 struct g_consumer *cp; 531 struct g_geom *gp; 532 int error; 533 534 g_topology_assert(); 535 g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name); 536 G_MOUNTVER_DEBUG(2, "Tasting %s.", pp->name); 537 538 /* 539 * Let's check if device already exists. 540 */ 541 LIST_FOREACH(gp, &mp->geom, geom) { 542 sc = gp->softc; 543 if (sc == NULL) 544 continue; 545 546 /* Already attached? */ 547 if (pp == LIST_FIRST(&gp->provider)) 548 return (NULL); 549 550 if (sc->sc_orphaned && strcmp(pp->name, sc->sc_provider_name) == 0) 551 break; 552 } 553 if (gp == NULL) 554 return (NULL); 555 556 cp = LIST_FIRST(&gp->consumer); 557 g_attach(cp, pp); 558 error = g_mountver_ident_matches(gp); 559 if (error != 0) { 560 g_detach(cp); 561 return (NULL); 562 } 563 if (sc->sc_access_r > 0 || sc->sc_access_w > 0 || sc->sc_access_e > 0) { 564 error = g_access(cp, sc->sc_access_r, sc->sc_access_w, sc->sc_access_e); 565 if (error != 0) { 566 G_MOUNTVER_DEBUG(0, "Cannot access %s; error = %d.", pp->name, error); 567 g_detach(cp); 568 return (NULL); 569 } 570 } 571 g_mountver_send_queued(gp); 572 sc->sc_orphaned = 0; 573 G_MOUNTVER_DEBUG(0, "%s has completed mount verification.", sc->sc_provider_name); 574 575 return (gp); 576 } 577 578 static void 579 g_mountver_config(struct gctl_req *req, struct g_class *mp, const char *verb) 580 { 581 uint32_t *version; 582 583 g_topology_assert(); 584 585 version = gctl_get_paraml(req, "version", sizeof(*version)); 586 if (version == NULL) { 587 gctl_error(req, "No '%s' argument.", "version"); 588 return; 589 } 590 if (*version != G_MOUNTVER_VERSION) { 591 gctl_error(req, "Userland and kernel parts are out of sync."); 592 return; 593 } 594 595 if (strcmp(verb, "create") == 0) { 596 g_mountver_ctl_create(req, mp); 597 return; 598 } else if (strcmp(verb, "destroy") == 0) { 599 g_mountver_ctl_destroy(req, mp); 600 return; 601 } 602 603 gctl_error(req, "Unknown verb."); 604 } 605 606 static void 607 g_mountver_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, 608 struct g_consumer *cp, struct g_provider *pp) 609 { 610 struct g_mountver_softc *sc; 611 612 if (pp != NULL || cp != NULL) 613 return; 614 615 sc = gp->softc; 616 sbuf_printf(sb, "%s<State>%s</State>\n", indent, 617 sc->sc_orphaned ? "OFFLINE" : "ONLINE"); 618 sbuf_printf(sb, "%s<Provider-Name>%s</Provider-Name>\n", indent, sc->sc_provider_name); 619 sbuf_printf(sb, "%s<Disk-Ident>%s</Disk-Ident>\n", indent, sc->sc_ident); 620 } 621 622 static void 623 g_mountver_shutdown_pre_sync(void *arg, int howto) 624 { 625 struct g_mountver_softc *sc; 626 struct g_class *mp; 627 struct g_geom *gp, *gp2; 628 629 mp = arg; 630 g_topology_lock(); 631 LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) { 632 if (gp->softc == NULL) 633 continue; 634 sc = gp->softc; 635 sc->sc_shutting_down = 1; 636 if (sc->sc_orphaned) 637 g_mountver_destroy(gp, 1); 638 } 639 g_topology_unlock(); 640 } 641 642 static void 643 g_mountver_init(struct g_class *mp) 644 { 645 646 g_mountver_pre_sync = EVENTHANDLER_REGISTER(shutdown_pre_sync, 647 g_mountver_shutdown_pre_sync, mp, SHUTDOWN_PRI_FIRST); 648 if (g_mountver_pre_sync == NULL) 649 G_MOUNTVER_DEBUG(0, "Warning! Cannot register shutdown event."); 650 } 651 652 static void 653 g_mountver_fini(struct g_class *mp) 654 { 655 656 if (g_mountver_pre_sync != NULL) 657 EVENTHANDLER_DEREGISTER(shutdown_pre_sync, g_mountver_pre_sync); 658 } 659 660 DECLARE_GEOM_CLASS(g_mountver_class, g_mountver); 661