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