1 /*- 2 * Copyright (c) 2005-2011 Pawel Jakub Dawidek <pawel@dawidek.net> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include <sys/param.h> 31 #include <sys/systm.h> 32 #include <sys/cons.h> 33 #include <sys/kernel.h> 34 #include <sys/linker.h> 35 #include <sys/module.h> 36 #include <sys/lock.h> 37 #include <sys/mutex.h> 38 #include <sys/bio.h> 39 #include <sys/sbuf.h> 40 #include <sys/sysctl.h> 41 #include <sys/malloc.h> 42 #include <sys/eventhandler.h> 43 #include <sys/kthread.h> 44 #include <sys/proc.h> 45 #include <sys/sched.h> 46 #include <sys/smp.h> 47 #include <sys/uio.h> 48 #include <sys/vnode.h> 49 50 #include <vm/uma.h> 51 52 #include <geom/geom.h> 53 #include <geom/eli/g_eli.h> 54 #include <geom/eli/pkcs5v2.h> 55 56 FEATURE(geom_eli, "GEOM crypto module"); 57 58 MALLOC_DEFINE(M_ELI, "eli data", "GEOM_ELI Data"); 59 60 SYSCTL_DECL(_kern_geom); 61 SYSCTL_NODE(_kern_geom, OID_AUTO, eli, CTLFLAG_RW, 0, "GEOM_ELI stuff"); 62 static int g_eli_version = G_ELI_VERSION; 63 SYSCTL_INT(_kern_geom_eli, OID_AUTO, version, CTLFLAG_RD, &g_eli_version, 0, 64 "GELI version"); 65 int g_eli_debug = 0; 66 SYSCTL_INT(_kern_geom_eli, OID_AUTO, debug, CTLFLAG_RWTUN, &g_eli_debug, 0, 67 "Debug level"); 68 static u_int g_eli_tries = 3; 69 SYSCTL_UINT(_kern_geom_eli, OID_AUTO, tries, CTLFLAG_RWTUN, &g_eli_tries, 0, 70 "Number of tries for entering the passphrase"); 71 static u_int g_eli_visible_passphrase = GETS_NOECHO; 72 SYSCTL_UINT(_kern_geom_eli, OID_AUTO, visible_passphrase, CTLFLAG_RWTUN, 73 &g_eli_visible_passphrase, 0, 74 "Visibility of passphrase prompt (0 = invisible, 1 = visible, 2 = asterisk)"); 75 u_int g_eli_overwrites = G_ELI_OVERWRITES; 76 SYSCTL_UINT(_kern_geom_eli, OID_AUTO, overwrites, CTLFLAG_RWTUN, &g_eli_overwrites, 77 0, "Number of times on-disk keys should be overwritten when destroying them"); 78 static u_int g_eli_threads = 0; 79 SYSCTL_UINT(_kern_geom_eli, OID_AUTO, threads, CTLFLAG_RWTUN, &g_eli_threads, 0, 80 "Number of threads doing crypto work"); 81 u_int g_eli_batch = 0; 82 SYSCTL_UINT(_kern_geom_eli, OID_AUTO, batch, CTLFLAG_RWTUN, &g_eli_batch, 0, 83 "Use crypto operations batching"); 84 85 static eventhandler_tag g_eli_pre_sync = NULL; 86 87 static int g_eli_destroy_geom(struct gctl_req *req, struct g_class *mp, 88 struct g_geom *gp); 89 static void g_eli_init(struct g_class *mp); 90 static void g_eli_fini(struct g_class *mp); 91 92 static g_taste_t g_eli_taste; 93 static g_dumpconf_t g_eli_dumpconf; 94 95 struct g_class g_eli_class = { 96 .name = G_ELI_CLASS_NAME, 97 .version = G_VERSION, 98 .ctlreq = g_eli_config, 99 .taste = g_eli_taste, 100 .destroy_geom = g_eli_destroy_geom, 101 .init = g_eli_init, 102 .fini = g_eli_fini 103 }; 104 105 106 /* 107 * Code paths: 108 * BIO_READ: 109 * g_eli_start -> g_eli_crypto_read -> g_io_request -> g_eli_read_done -> g_eli_crypto_run -> g_eli_crypto_read_done -> g_io_deliver 110 * BIO_WRITE: 111 * g_eli_start -> g_eli_crypto_run -> g_eli_crypto_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver 112 */ 113 114 115 /* 116 * EAGAIN from crypto(9) means, that we were probably balanced to another crypto 117 * accelerator or something like this. 118 * The function updates the SID and rerun the operation. 119 */ 120 int 121 g_eli_crypto_rerun(struct cryptop *crp) 122 { 123 struct g_eli_softc *sc; 124 struct g_eli_worker *wr; 125 struct bio *bp; 126 int error; 127 128 bp = (struct bio *)crp->crp_opaque; 129 sc = bp->bio_to->geom->softc; 130 LIST_FOREACH(wr, &sc->sc_workers, w_next) { 131 if (wr->w_number == bp->bio_pflags) 132 break; 133 } 134 KASSERT(wr != NULL, ("Invalid worker (%u).", bp->bio_pflags)); 135 G_ELI_DEBUG(1, "Rerunning crypto %s request (sid: %ju -> %ju).", 136 bp->bio_cmd == BIO_READ ? "READ" : "WRITE", (uintmax_t)wr->w_sid, 137 (uintmax_t)crp->crp_sid); 138 wr->w_sid = crp->crp_sid; 139 crp->crp_etype = 0; 140 error = crypto_dispatch(crp); 141 if (error == 0) 142 return (0); 143 G_ELI_DEBUG(1, "%s: crypto_dispatch() returned %d.", __func__, error); 144 crp->crp_etype = error; 145 return (error); 146 } 147 148 /* 149 * The function is called afer reading encrypted data from the provider. 150 * 151 * g_eli_start -> g_eli_crypto_read -> g_io_request -> G_ELI_READ_DONE -> g_eli_crypto_run -> g_eli_crypto_read_done -> g_io_deliver 152 */ 153 void 154 g_eli_read_done(struct bio *bp) 155 { 156 struct g_eli_softc *sc; 157 struct bio *pbp; 158 159 G_ELI_LOGREQ(2, bp, "Request done."); 160 pbp = bp->bio_parent; 161 if (pbp->bio_error == 0) 162 pbp->bio_error = bp->bio_error; 163 g_destroy_bio(bp); 164 /* 165 * Do we have all sectors already? 166 */ 167 pbp->bio_inbed++; 168 if (pbp->bio_inbed < pbp->bio_children) 169 return; 170 sc = pbp->bio_to->geom->softc; 171 if (pbp->bio_error != 0) { 172 G_ELI_LOGREQ(0, pbp, "%s() failed", __func__); 173 pbp->bio_completed = 0; 174 if (pbp->bio_driver2 != NULL) { 175 free(pbp->bio_driver2, M_ELI); 176 pbp->bio_driver2 = NULL; 177 } 178 g_io_deliver(pbp, pbp->bio_error); 179 atomic_subtract_int(&sc->sc_inflight, 1); 180 return; 181 } 182 mtx_lock(&sc->sc_queue_mtx); 183 bioq_insert_tail(&sc->sc_queue, pbp); 184 mtx_unlock(&sc->sc_queue_mtx); 185 wakeup(sc); 186 } 187 188 /* 189 * The function is called after we encrypt and write data. 190 * 191 * g_eli_start -> g_eli_crypto_run -> g_eli_crypto_write_done -> g_io_request -> G_ELI_WRITE_DONE -> g_io_deliver 192 */ 193 void 194 g_eli_write_done(struct bio *bp) 195 { 196 struct g_eli_softc *sc; 197 struct bio *pbp; 198 199 G_ELI_LOGREQ(2, bp, "Request done."); 200 pbp = bp->bio_parent; 201 if (pbp->bio_error == 0) { 202 if (bp->bio_error != 0) 203 pbp->bio_error = bp->bio_error; 204 } 205 g_destroy_bio(bp); 206 /* 207 * Do we have all sectors already? 208 */ 209 pbp->bio_inbed++; 210 if (pbp->bio_inbed < pbp->bio_children) 211 return; 212 free(pbp->bio_driver2, M_ELI); 213 pbp->bio_driver2 = NULL; 214 if (pbp->bio_error != 0) { 215 G_ELI_LOGREQ(0, pbp, "Crypto WRITE request failed (error=%d).", 216 pbp->bio_error); 217 pbp->bio_completed = 0; 218 } 219 /* 220 * Write is finished, send it up. 221 */ 222 pbp->bio_completed = pbp->bio_length; 223 sc = pbp->bio_to->geom->softc; 224 g_io_deliver(pbp, pbp->bio_error); 225 atomic_subtract_int(&sc->sc_inflight, 1); 226 } 227 228 /* 229 * This function should never be called, but GEOM made as it set ->orphan() 230 * method for every geom. 231 */ 232 static void 233 g_eli_orphan_spoil_assert(struct g_consumer *cp) 234 { 235 236 panic("Function %s() called for %s.", __func__, cp->geom->name); 237 } 238 239 static void 240 g_eli_orphan(struct g_consumer *cp) 241 { 242 struct g_eli_softc *sc; 243 244 g_topology_assert(); 245 sc = cp->geom->softc; 246 if (sc == NULL) 247 return; 248 g_eli_destroy(sc, TRUE); 249 } 250 251 /* 252 * BIO_READ: 253 * G_ELI_START -> g_eli_crypto_read -> g_io_request -> g_eli_read_done -> g_eli_crypto_run -> g_eli_crypto_read_done -> g_io_deliver 254 * BIO_WRITE: 255 * G_ELI_START -> g_eli_crypto_run -> g_eli_crypto_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver 256 */ 257 static void 258 g_eli_start(struct bio *bp) 259 { 260 struct g_eli_softc *sc; 261 struct g_consumer *cp; 262 struct bio *cbp; 263 264 sc = bp->bio_to->geom->softc; 265 KASSERT(sc != NULL, 266 ("Provider's error should be set (error=%d)(device=%s).", 267 bp->bio_to->error, bp->bio_to->name)); 268 G_ELI_LOGREQ(2, bp, "Request received."); 269 270 switch (bp->bio_cmd) { 271 case BIO_READ: 272 case BIO_WRITE: 273 case BIO_GETATTR: 274 case BIO_FLUSH: 275 break; 276 case BIO_DELETE: 277 /* 278 * We could eventually support BIO_DELETE request. 279 * It could be done by overwritting requested sector with 280 * random data g_eli_overwrites number of times. 281 */ 282 default: 283 g_io_deliver(bp, EOPNOTSUPP); 284 return; 285 } 286 cbp = g_clone_bio(bp); 287 if (cbp == NULL) { 288 g_io_deliver(bp, ENOMEM); 289 return; 290 } 291 bp->bio_driver1 = cbp; 292 bp->bio_pflags = G_ELI_NEW_BIO; 293 switch (bp->bio_cmd) { 294 case BIO_READ: 295 if (!(sc->sc_flags & G_ELI_FLAG_AUTH)) { 296 g_eli_crypto_read(sc, bp, 0); 297 break; 298 } 299 /* FALLTHROUGH */ 300 case BIO_WRITE: 301 mtx_lock(&sc->sc_queue_mtx); 302 bioq_insert_tail(&sc->sc_queue, bp); 303 mtx_unlock(&sc->sc_queue_mtx); 304 wakeup(sc); 305 break; 306 case BIO_GETATTR: 307 case BIO_FLUSH: 308 cbp->bio_done = g_std_done; 309 cp = LIST_FIRST(&sc->sc_geom->consumer); 310 cbp->bio_to = cp->provider; 311 G_ELI_LOGREQ(2, cbp, "Sending request."); 312 g_io_request(cbp, cp); 313 break; 314 } 315 } 316 317 static int 318 g_eli_newsession(struct g_eli_worker *wr) 319 { 320 struct g_eli_softc *sc; 321 struct cryptoini crie, cria; 322 int error; 323 324 sc = wr->w_softc; 325 326 bzero(&crie, sizeof(crie)); 327 crie.cri_alg = sc->sc_ealgo; 328 crie.cri_klen = sc->sc_ekeylen; 329 if (sc->sc_ealgo == CRYPTO_AES_XTS) 330 crie.cri_klen <<= 1; 331 if ((sc->sc_flags & G_ELI_FLAG_FIRST_KEY) != 0) { 332 crie.cri_key = g_eli_key_hold(sc, 0, 333 LIST_FIRST(&sc->sc_geom->consumer)->provider->sectorsize); 334 } else { 335 crie.cri_key = sc->sc_ekey; 336 } 337 if (sc->sc_flags & G_ELI_FLAG_AUTH) { 338 bzero(&cria, sizeof(cria)); 339 cria.cri_alg = sc->sc_aalgo; 340 cria.cri_klen = sc->sc_akeylen; 341 cria.cri_key = sc->sc_akey; 342 crie.cri_next = &cria; 343 } 344 345 switch (sc->sc_crypto) { 346 case G_ELI_CRYPTO_SW: 347 error = crypto_newsession(&wr->w_sid, &crie, 348 CRYPTOCAP_F_SOFTWARE); 349 break; 350 case G_ELI_CRYPTO_HW: 351 error = crypto_newsession(&wr->w_sid, &crie, 352 CRYPTOCAP_F_HARDWARE); 353 break; 354 case G_ELI_CRYPTO_UNKNOWN: 355 error = crypto_newsession(&wr->w_sid, &crie, 356 CRYPTOCAP_F_HARDWARE); 357 if (error == 0) { 358 mtx_lock(&sc->sc_queue_mtx); 359 if (sc->sc_crypto == G_ELI_CRYPTO_UNKNOWN) 360 sc->sc_crypto = G_ELI_CRYPTO_HW; 361 mtx_unlock(&sc->sc_queue_mtx); 362 } else { 363 error = crypto_newsession(&wr->w_sid, &crie, 364 CRYPTOCAP_F_SOFTWARE); 365 mtx_lock(&sc->sc_queue_mtx); 366 if (sc->sc_crypto == G_ELI_CRYPTO_UNKNOWN) 367 sc->sc_crypto = G_ELI_CRYPTO_SW; 368 mtx_unlock(&sc->sc_queue_mtx); 369 } 370 break; 371 default: 372 panic("%s: invalid condition", __func__); 373 } 374 375 if ((sc->sc_flags & G_ELI_FLAG_FIRST_KEY) != 0) 376 g_eli_key_drop(sc, crie.cri_key); 377 378 return (error); 379 } 380 381 static void 382 g_eli_freesession(struct g_eli_worker *wr) 383 { 384 385 crypto_freesession(wr->w_sid); 386 } 387 388 static void 389 g_eli_cancel(struct g_eli_softc *sc) 390 { 391 struct bio *bp; 392 393 mtx_assert(&sc->sc_queue_mtx, MA_OWNED); 394 395 while ((bp = bioq_takefirst(&sc->sc_queue)) != NULL) { 396 KASSERT(bp->bio_pflags == G_ELI_NEW_BIO, 397 ("Not new bio when canceling (bp=%p).", bp)); 398 g_io_deliver(bp, ENXIO); 399 } 400 } 401 402 static struct bio * 403 g_eli_takefirst(struct g_eli_softc *sc) 404 { 405 struct bio *bp; 406 407 mtx_assert(&sc->sc_queue_mtx, MA_OWNED); 408 409 if (!(sc->sc_flags & G_ELI_FLAG_SUSPEND)) 410 return (bioq_takefirst(&sc->sc_queue)); 411 /* 412 * Device suspended, so we skip new I/O requests. 413 */ 414 TAILQ_FOREACH(bp, &sc->sc_queue.queue, bio_queue) { 415 if (bp->bio_pflags != G_ELI_NEW_BIO) 416 break; 417 } 418 if (bp != NULL) 419 bioq_remove(&sc->sc_queue, bp); 420 return (bp); 421 } 422 423 /* 424 * This is the main function for kernel worker thread when we don't have 425 * hardware acceleration and we have to do cryptography in software. 426 * Dedicated thread is needed, so we don't slow down g_up/g_down GEOM 427 * threads with crypto work. 428 */ 429 static void 430 g_eli_worker(void *arg) 431 { 432 struct g_eli_softc *sc; 433 struct g_eli_worker *wr; 434 struct bio *bp; 435 int error; 436 437 wr = arg; 438 sc = wr->w_softc; 439 #ifdef SMP 440 /* Before sched_bind() to a CPU, wait for all CPUs to go on-line. */ 441 if (sc->sc_cpubind) { 442 while (!smp_started) 443 tsleep(wr, 0, "geli:smp", hz / 4); 444 } 445 #endif 446 thread_lock(curthread); 447 sched_prio(curthread, PUSER); 448 if (sc->sc_cpubind) 449 sched_bind(curthread, wr->w_number % mp_ncpus); 450 thread_unlock(curthread); 451 452 G_ELI_DEBUG(1, "Thread %s started.", curthread->td_proc->p_comm); 453 454 for (;;) { 455 mtx_lock(&sc->sc_queue_mtx); 456 again: 457 bp = g_eli_takefirst(sc); 458 if (bp == NULL) { 459 if (sc->sc_flags & G_ELI_FLAG_DESTROY) { 460 g_eli_cancel(sc); 461 LIST_REMOVE(wr, w_next); 462 g_eli_freesession(wr); 463 free(wr, M_ELI); 464 G_ELI_DEBUG(1, "Thread %s exiting.", 465 curthread->td_proc->p_comm); 466 wakeup(&sc->sc_workers); 467 mtx_unlock(&sc->sc_queue_mtx); 468 kproc_exit(0); 469 } 470 while (sc->sc_flags & G_ELI_FLAG_SUSPEND) { 471 if (sc->sc_inflight > 0) { 472 G_ELI_DEBUG(0, "inflight=%d", 473 sc->sc_inflight); 474 /* 475 * We still have inflight BIOs, so 476 * sleep and retry. 477 */ 478 msleep(sc, &sc->sc_queue_mtx, PRIBIO, 479 "geli:inf", hz / 5); 480 goto again; 481 } 482 /* 483 * Suspend requested, mark the worker as 484 * suspended and go to sleep. 485 */ 486 if (wr->w_active) { 487 g_eli_freesession(wr); 488 wr->w_active = FALSE; 489 } 490 wakeup(&sc->sc_workers); 491 msleep(sc, &sc->sc_queue_mtx, PRIBIO, 492 "geli:suspend", 0); 493 if (!wr->w_active && 494 !(sc->sc_flags & G_ELI_FLAG_SUSPEND)) { 495 error = g_eli_newsession(wr); 496 KASSERT(error == 0, 497 ("g_eli_newsession() failed on resume (error=%d)", 498 error)); 499 wr->w_active = TRUE; 500 } 501 goto again; 502 } 503 msleep(sc, &sc->sc_queue_mtx, PDROP, "geli:w", 0); 504 continue; 505 } 506 if (bp->bio_pflags == G_ELI_NEW_BIO) 507 atomic_add_int(&sc->sc_inflight, 1); 508 mtx_unlock(&sc->sc_queue_mtx); 509 if (bp->bio_pflags == G_ELI_NEW_BIO) { 510 bp->bio_pflags = 0; 511 if (sc->sc_flags & G_ELI_FLAG_AUTH) { 512 if (bp->bio_cmd == BIO_READ) 513 g_eli_auth_read(sc, bp); 514 else 515 g_eli_auth_run(wr, bp); 516 } else { 517 if (bp->bio_cmd == BIO_READ) 518 g_eli_crypto_read(sc, bp, 1); 519 else 520 g_eli_crypto_run(wr, bp); 521 } 522 } else { 523 if (sc->sc_flags & G_ELI_FLAG_AUTH) 524 g_eli_auth_run(wr, bp); 525 else 526 g_eli_crypto_run(wr, bp); 527 } 528 } 529 } 530 531 /* 532 * Here we generate IV. It is unique for every sector. 533 */ 534 void 535 g_eli_crypto_ivgen(struct g_eli_softc *sc, off_t offset, u_char *iv, 536 size_t size) 537 { 538 uint8_t off[8]; 539 540 if ((sc->sc_flags & G_ELI_FLAG_NATIVE_BYTE_ORDER) != 0) 541 bcopy(&offset, off, sizeof(off)); 542 else 543 le64enc(off, (uint64_t)offset); 544 545 switch (sc->sc_ealgo) { 546 case CRYPTO_AES_XTS: 547 bcopy(off, iv, sizeof(off)); 548 bzero(iv + sizeof(off), size - sizeof(off)); 549 break; 550 default: 551 { 552 u_char hash[SHA256_DIGEST_LENGTH]; 553 SHA256_CTX ctx; 554 555 /* Copy precalculated SHA256 context for IV-Key. */ 556 bcopy(&sc->sc_ivctx, &ctx, sizeof(ctx)); 557 SHA256_Update(&ctx, off, sizeof(off)); 558 SHA256_Final(hash, &ctx); 559 bcopy(hash, iv, MIN(sizeof(hash), size)); 560 break; 561 } 562 } 563 } 564 565 int 566 g_eli_read_metadata(struct g_class *mp, struct g_provider *pp, 567 struct g_eli_metadata *md) 568 { 569 struct g_geom *gp; 570 struct g_consumer *cp; 571 u_char *buf = NULL; 572 int error; 573 574 g_topology_assert(); 575 576 gp = g_new_geomf(mp, "eli:taste"); 577 gp->start = g_eli_start; 578 gp->access = g_std_access; 579 /* 580 * g_eli_read_metadata() is always called from the event thread. 581 * Our geom is created and destroyed in the same event, so there 582 * could be no orphan nor spoil event in the meantime. 583 */ 584 gp->orphan = g_eli_orphan_spoil_assert; 585 gp->spoiled = g_eli_orphan_spoil_assert; 586 cp = g_new_consumer(gp); 587 error = g_attach(cp, pp); 588 if (error != 0) 589 goto end; 590 error = g_access(cp, 1, 0, 0); 591 if (error != 0) 592 goto end; 593 g_topology_unlock(); 594 buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize, 595 &error); 596 g_topology_lock(); 597 if (buf == NULL) 598 goto end; 599 eli_metadata_decode(buf, md); 600 end: 601 if (buf != NULL) 602 g_free(buf); 603 if (cp->provider != NULL) { 604 if (cp->acr == 1) 605 g_access(cp, -1, 0, 0); 606 g_detach(cp); 607 } 608 g_destroy_consumer(cp); 609 g_destroy_geom(gp); 610 return (error); 611 } 612 613 /* 614 * The function is called when we had last close on provider and user requested 615 * to close it when this situation occur. 616 */ 617 static void 618 g_eli_last_close(void *arg, int flags __unused) 619 { 620 struct g_geom *gp; 621 char gpname[64]; 622 int error; 623 624 g_topology_assert(); 625 gp = arg; 626 strlcpy(gpname, gp->name, sizeof(gpname)); 627 error = g_eli_destroy(gp->softc, TRUE); 628 KASSERT(error == 0, ("Cannot detach %s on last close (error=%d).", 629 gpname, error)); 630 G_ELI_DEBUG(0, "Detached %s on last close.", gpname); 631 } 632 633 int 634 g_eli_access(struct g_provider *pp, int dr, int dw, int de) 635 { 636 struct g_eli_softc *sc; 637 struct g_geom *gp; 638 639 gp = pp->geom; 640 sc = gp->softc; 641 642 if (dw > 0) { 643 if (sc->sc_flags & G_ELI_FLAG_RO) { 644 /* Deny write attempts. */ 645 return (EROFS); 646 } 647 /* Someone is opening us for write, we need to remember that. */ 648 sc->sc_flags |= G_ELI_FLAG_WOPEN; 649 return (0); 650 } 651 /* Is this the last close? */ 652 if (pp->acr + dr > 0 || pp->acw + dw > 0 || pp->ace + de > 0) 653 return (0); 654 655 /* 656 * Automatically detach on last close if requested. 657 */ 658 if ((sc->sc_flags & G_ELI_FLAG_RW_DETACH) || 659 (sc->sc_flags & G_ELI_FLAG_WOPEN)) { 660 g_post_event(g_eli_last_close, gp, M_WAITOK, NULL); 661 } 662 return (0); 663 } 664 665 static int 666 g_eli_cpu_is_disabled(int cpu) 667 { 668 #ifdef SMP 669 return (CPU_ISSET(cpu, &hlt_cpus_mask)); 670 #else 671 return (0); 672 #endif 673 } 674 675 struct g_geom * 676 g_eli_create(struct gctl_req *req, struct g_class *mp, struct g_provider *bpp, 677 const struct g_eli_metadata *md, const u_char *mkey, int nkey) 678 { 679 struct g_eli_softc *sc; 680 struct g_eli_worker *wr; 681 struct g_geom *gp; 682 struct g_provider *pp; 683 struct g_consumer *cp; 684 u_int i, threads; 685 int error; 686 687 G_ELI_DEBUG(1, "Creating device %s%s.", bpp->name, G_ELI_SUFFIX); 688 689 gp = g_new_geomf(mp, "%s%s", bpp->name, G_ELI_SUFFIX); 690 sc = malloc(sizeof(*sc), M_ELI, M_WAITOK | M_ZERO); 691 gp->start = g_eli_start; 692 /* 693 * Spoiling cannot happen actually, because we keep provider open for 694 * writing all the time or provider is read-only. 695 */ 696 gp->spoiled = g_eli_orphan_spoil_assert; 697 gp->orphan = g_eli_orphan; 698 gp->dumpconf = g_eli_dumpconf; 699 /* 700 * If detach-on-last-close feature is not enabled and we don't operate 701 * on read-only provider, we can simply use g_std_access(). 702 */ 703 if (md->md_flags & (G_ELI_FLAG_WO_DETACH | G_ELI_FLAG_RO)) 704 gp->access = g_eli_access; 705 else 706 gp->access = g_std_access; 707 708 sc->sc_version = md->md_version; 709 sc->sc_inflight = 0; 710 sc->sc_crypto = G_ELI_CRYPTO_UNKNOWN; 711 sc->sc_flags = md->md_flags; 712 /* Backward compatibility. */ 713 if (md->md_version < G_ELI_VERSION_04) 714 sc->sc_flags |= G_ELI_FLAG_NATIVE_BYTE_ORDER; 715 if (md->md_version < G_ELI_VERSION_05) 716 sc->sc_flags |= G_ELI_FLAG_SINGLE_KEY; 717 if (md->md_version < G_ELI_VERSION_06 && 718 (sc->sc_flags & G_ELI_FLAG_AUTH) != 0) { 719 sc->sc_flags |= G_ELI_FLAG_FIRST_KEY; 720 } 721 if (md->md_version < G_ELI_VERSION_07) 722 sc->sc_flags |= G_ELI_FLAG_ENC_IVKEY; 723 sc->sc_ealgo = md->md_ealgo; 724 sc->sc_nkey = nkey; 725 726 if (sc->sc_flags & G_ELI_FLAG_AUTH) { 727 sc->sc_akeylen = sizeof(sc->sc_akey) * 8; 728 sc->sc_aalgo = md->md_aalgo; 729 sc->sc_alen = g_eli_hashlen(sc->sc_aalgo); 730 731 sc->sc_data_per_sector = bpp->sectorsize - sc->sc_alen; 732 /* 733 * Some hash functions (like SHA1 and RIPEMD160) generates hash 734 * which length is not multiple of 128 bits, but we want data 735 * length to be multiple of 128, so we can encrypt without 736 * padding. The line below rounds down data length to multiple 737 * of 128 bits. 738 */ 739 sc->sc_data_per_sector -= sc->sc_data_per_sector % 16; 740 741 sc->sc_bytes_per_sector = 742 (md->md_sectorsize - 1) / sc->sc_data_per_sector + 1; 743 sc->sc_bytes_per_sector *= bpp->sectorsize; 744 } 745 746 gp->softc = sc; 747 sc->sc_geom = gp; 748 749 bioq_init(&sc->sc_queue); 750 mtx_init(&sc->sc_queue_mtx, "geli:queue", NULL, MTX_DEF); 751 mtx_init(&sc->sc_ekeys_lock, "geli:ekeys", NULL, MTX_DEF); 752 753 pp = NULL; 754 cp = g_new_consumer(gp); 755 error = g_attach(cp, bpp); 756 if (error != 0) { 757 if (req != NULL) { 758 gctl_error(req, "Cannot attach to %s (error=%d).", 759 bpp->name, error); 760 } else { 761 G_ELI_DEBUG(1, "Cannot attach to %s (error=%d).", 762 bpp->name, error); 763 } 764 goto failed; 765 } 766 /* 767 * Keep provider open all the time, so we can run critical tasks, 768 * like Master Keys deletion, without wondering if we can open 769 * provider or not. 770 * We don't open provider for writing only when user requested read-only 771 * access. 772 */ 773 if (sc->sc_flags & G_ELI_FLAG_RO) 774 error = g_access(cp, 1, 0, 1); 775 else 776 error = g_access(cp, 1, 1, 1); 777 if (error != 0) { 778 if (req != NULL) { 779 gctl_error(req, "Cannot access %s (error=%d).", 780 bpp->name, error); 781 } else { 782 G_ELI_DEBUG(1, "Cannot access %s (error=%d).", 783 bpp->name, error); 784 } 785 goto failed; 786 } 787 788 sc->sc_sectorsize = md->md_sectorsize; 789 sc->sc_mediasize = bpp->mediasize; 790 if (!(sc->sc_flags & G_ELI_FLAG_ONETIME)) 791 sc->sc_mediasize -= bpp->sectorsize; 792 if (!(sc->sc_flags & G_ELI_FLAG_AUTH)) 793 sc->sc_mediasize -= (sc->sc_mediasize % sc->sc_sectorsize); 794 else { 795 sc->sc_mediasize /= sc->sc_bytes_per_sector; 796 sc->sc_mediasize *= sc->sc_sectorsize; 797 } 798 799 /* 800 * Remember the keys in our softc structure. 801 */ 802 g_eli_mkey_propagate(sc, mkey); 803 sc->sc_ekeylen = md->md_keylen; 804 805 LIST_INIT(&sc->sc_workers); 806 807 threads = g_eli_threads; 808 if (threads == 0) 809 threads = mp_ncpus; 810 sc->sc_cpubind = (mp_ncpus > 1 && threads == mp_ncpus); 811 for (i = 0; i < threads; i++) { 812 if (g_eli_cpu_is_disabled(i)) { 813 G_ELI_DEBUG(1, "%s: CPU %u disabled, skipping.", 814 bpp->name, i); 815 continue; 816 } 817 wr = malloc(sizeof(*wr), M_ELI, M_WAITOK | M_ZERO); 818 wr->w_softc = sc; 819 wr->w_number = i; 820 wr->w_active = TRUE; 821 822 error = g_eli_newsession(wr); 823 if (error != 0) { 824 free(wr, M_ELI); 825 if (req != NULL) { 826 gctl_error(req, "Cannot set up crypto session " 827 "for %s (error=%d).", bpp->name, error); 828 } else { 829 G_ELI_DEBUG(1, "Cannot set up crypto session " 830 "for %s (error=%d).", bpp->name, error); 831 } 832 goto failed; 833 } 834 835 error = kproc_create(g_eli_worker, wr, &wr->w_proc, 0, 0, 836 "g_eli[%u] %s", i, bpp->name); 837 if (error != 0) { 838 g_eli_freesession(wr); 839 free(wr, M_ELI); 840 if (req != NULL) { 841 gctl_error(req, "Cannot create kernel thread " 842 "for %s (error=%d).", bpp->name, error); 843 } else { 844 G_ELI_DEBUG(1, "Cannot create kernel thread " 845 "for %s (error=%d).", bpp->name, error); 846 } 847 goto failed; 848 } 849 LIST_INSERT_HEAD(&sc->sc_workers, wr, w_next); 850 } 851 852 /* 853 * Create decrypted provider. 854 */ 855 pp = g_new_providerf(gp, "%s%s", bpp->name, G_ELI_SUFFIX); 856 pp->mediasize = sc->sc_mediasize; 857 pp->sectorsize = sc->sc_sectorsize; 858 859 g_error_provider(pp, 0); 860 861 G_ELI_DEBUG(0, "Device %s created.", pp->name); 862 G_ELI_DEBUG(0, "Encryption: %s %u", g_eli_algo2str(sc->sc_ealgo), 863 sc->sc_ekeylen); 864 if (sc->sc_flags & G_ELI_FLAG_AUTH) 865 G_ELI_DEBUG(0, " Integrity: %s", g_eli_algo2str(sc->sc_aalgo)); 866 G_ELI_DEBUG(0, " Crypto: %s", 867 sc->sc_crypto == G_ELI_CRYPTO_SW ? "software" : "hardware"); 868 return (gp); 869 failed: 870 mtx_lock(&sc->sc_queue_mtx); 871 sc->sc_flags |= G_ELI_FLAG_DESTROY; 872 wakeup(sc); 873 /* 874 * Wait for kernel threads self destruction. 875 */ 876 while (!LIST_EMPTY(&sc->sc_workers)) { 877 msleep(&sc->sc_workers, &sc->sc_queue_mtx, PRIBIO, 878 "geli:destroy", 0); 879 } 880 mtx_destroy(&sc->sc_queue_mtx); 881 if (cp->provider != NULL) { 882 if (cp->acr == 1) 883 g_access(cp, -1, -1, -1); 884 g_detach(cp); 885 } 886 g_destroy_consumer(cp); 887 g_destroy_geom(gp); 888 g_eli_key_destroy(sc); 889 bzero(sc, sizeof(*sc)); 890 free(sc, M_ELI); 891 return (NULL); 892 } 893 894 int 895 g_eli_destroy(struct g_eli_softc *sc, boolean_t force) 896 { 897 struct g_geom *gp; 898 struct g_provider *pp; 899 900 g_topology_assert(); 901 902 if (sc == NULL) 903 return (ENXIO); 904 905 gp = sc->sc_geom; 906 pp = LIST_FIRST(&gp->provider); 907 if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) { 908 if (force) { 909 G_ELI_DEBUG(1, "Device %s is still open, so it " 910 "cannot be definitely removed.", pp->name); 911 sc->sc_flags |= G_ELI_FLAG_RW_DETACH; 912 gp->access = g_eli_access; 913 g_wither_provider(pp, ENXIO); 914 return (EBUSY); 915 } else { 916 G_ELI_DEBUG(1, 917 "Device %s is still open (r%dw%de%d).", pp->name, 918 pp->acr, pp->acw, pp->ace); 919 return (EBUSY); 920 } 921 } 922 923 mtx_lock(&sc->sc_queue_mtx); 924 sc->sc_flags |= G_ELI_FLAG_DESTROY; 925 wakeup(sc); 926 while (!LIST_EMPTY(&sc->sc_workers)) { 927 msleep(&sc->sc_workers, &sc->sc_queue_mtx, PRIBIO, 928 "geli:destroy", 0); 929 } 930 mtx_destroy(&sc->sc_queue_mtx); 931 gp->softc = NULL; 932 g_eli_key_destroy(sc); 933 bzero(sc, sizeof(*sc)); 934 free(sc, M_ELI); 935 936 if (pp == NULL || (pp->acr == 0 && pp->acw == 0 && pp->ace == 0)) 937 G_ELI_DEBUG(0, "Device %s destroyed.", gp->name); 938 g_wither_geom_close(gp, ENXIO); 939 940 return (0); 941 } 942 943 static int 944 g_eli_destroy_geom(struct gctl_req *req __unused, 945 struct g_class *mp __unused, struct g_geom *gp) 946 { 947 struct g_eli_softc *sc; 948 949 sc = gp->softc; 950 return (g_eli_destroy(sc, FALSE)); 951 } 952 953 static int 954 g_eli_keyfiles_load(struct hmac_ctx *ctx, const char *provider) 955 { 956 u_char *keyfile, *data; 957 char *file, name[64]; 958 size_t size; 959 int i; 960 961 for (i = 0; ; i++) { 962 snprintf(name, sizeof(name), "%s:geli_keyfile%d", provider, i); 963 keyfile = preload_search_by_type(name); 964 if (keyfile == NULL) 965 return (i); /* Return number of loaded keyfiles. */ 966 data = preload_fetch_addr(keyfile); 967 if (data == NULL) { 968 G_ELI_DEBUG(0, "Cannot find key file data for %s.", 969 name); 970 return (0); 971 } 972 size = preload_fetch_size(keyfile); 973 if (size == 0) { 974 G_ELI_DEBUG(0, "Cannot find key file size for %s.", 975 name); 976 return (0); 977 } 978 file = preload_search_info(keyfile, MODINFO_NAME); 979 if (file == NULL) { 980 G_ELI_DEBUG(0, "Cannot find key file name for %s.", 981 name); 982 return (0); 983 } 984 G_ELI_DEBUG(1, "Loaded keyfile %s for %s (type: %s).", file, 985 provider, name); 986 g_eli_crypto_hmac_update(ctx, data, size); 987 } 988 } 989 990 static void 991 g_eli_keyfiles_clear(const char *provider) 992 { 993 u_char *keyfile, *data; 994 char name[64]; 995 size_t size; 996 int i; 997 998 for (i = 0; ; i++) { 999 snprintf(name, sizeof(name), "%s:geli_keyfile%d", provider, i); 1000 keyfile = preload_search_by_type(name); 1001 if (keyfile == NULL) 1002 return; 1003 data = preload_fetch_addr(keyfile); 1004 size = preload_fetch_size(keyfile); 1005 if (data != NULL && size != 0) 1006 bzero(data, size); 1007 } 1008 } 1009 1010 /* 1011 * Tasting is only made on boot. 1012 * We detect providers which should be attached before root is mounted. 1013 */ 1014 static struct g_geom * 1015 g_eli_taste(struct g_class *mp, struct g_provider *pp, int flags __unused) 1016 { 1017 struct g_eli_metadata md; 1018 struct g_geom *gp; 1019 struct hmac_ctx ctx; 1020 char passphrase[256]; 1021 u_char key[G_ELI_USERKEYLEN], mkey[G_ELI_DATAIVKEYLEN]; 1022 u_int i, nkey, nkeyfiles, tries; 1023 int error; 1024 1025 g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name); 1026 g_topology_assert(); 1027 1028 if (root_mounted() || g_eli_tries == 0) 1029 return (NULL); 1030 1031 G_ELI_DEBUG(3, "Tasting %s.", pp->name); 1032 1033 error = g_eli_read_metadata(mp, pp, &md); 1034 if (error != 0) 1035 return (NULL); 1036 gp = NULL; 1037 1038 if (strcmp(md.md_magic, G_ELI_MAGIC) != 0) 1039 return (NULL); 1040 if (md.md_version > G_ELI_VERSION) { 1041 printf("geom_eli.ko module is too old to handle %s.\n", 1042 pp->name); 1043 return (NULL); 1044 } 1045 if (md.md_provsize != pp->mediasize) 1046 return (NULL); 1047 /* Should we attach it on boot? */ 1048 if (!(md.md_flags & G_ELI_FLAG_BOOT)) 1049 return (NULL); 1050 if (md.md_keys == 0x00) { 1051 G_ELI_DEBUG(0, "No valid keys on %s.", pp->name); 1052 return (NULL); 1053 } 1054 if (md.md_iterations == -1) { 1055 /* If there is no passphrase, we try only once. */ 1056 tries = 1; 1057 } else { 1058 /* Ask for the passphrase no more than g_eli_tries times. */ 1059 tries = g_eli_tries; 1060 } 1061 1062 for (i = 0; i < tries; i++) { 1063 g_eli_crypto_hmac_init(&ctx, NULL, 0); 1064 1065 /* 1066 * Load all key files. 1067 */ 1068 nkeyfiles = g_eli_keyfiles_load(&ctx, pp->name); 1069 1070 if (nkeyfiles == 0 && md.md_iterations == -1) { 1071 /* 1072 * No key files and no passphrase, something is 1073 * definitely wrong here. 1074 * geli(8) doesn't allow for such situation, so assume 1075 * that there was really no passphrase and in that case 1076 * key files are no properly defined in loader.conf. 1077 */ 1078 G_ELI_DEBUG(0, 1079 "Found no key files in loader.conf for %s.", 1080 pp->name); 1081 return (NULL); 1082 } 1083 1084 /* Ask for the passphrase if defined. */ 1085 if (md.md_iterations >= 0) { 1086 printf("Enter passphrase for %s: ", pp->name); 1087 cngets(passphrase, sizeof(passphrase), 1088 g_eli_visible_passphrase); 1089 } 1090 1091 /* 1092 * Prepare Derived-Key from the user passphrase. 1093 */ 1094 if (md.md_iterations == 0) { 1095 g_eli_crypto_hmac_update(&ctx, md.md_salt, 1096 sizeof(md.md_salt)); 1097 g_eli_crypto_hmac_update(&ctx, passphrase, 1098 strlen(passphrase)); 1099 bzero(passphrase, sizeof(passphrase)); 1100 } else if (md.md_iterations > 0) { 1101 u_char dkey[G_ELI_USERKEYLEN]; 1102 1103 pkcs5v2_genkey(dkey, sizeof(dkey), md.md_salt, 1104 sizeof(md.md_salt), passphrase, md.md_iterations); 1105 bzero(passphrase, sizeof(passphrase)); 1106 g_eli_crypto_hmac_update(&ctx, dkey, sizeof(dkey)); 1107 bzero(dkey, sizeof(dkey)); 1108 } 1109 1110 g_eli_crypto_hmac_final(&ctx, key, 0); 1111 1112 /* 1113 * Decrypt Master-Key. 1114 */ 1115 error = g_eli_mkey_decrypt(&md, key, mkey, &nkey); 1116 bzero(key, sizeof(key)); 1117 if (error == -1) { 1118 if (i == tries - 1) { 1119 G_ELI_DEBUG(0, 1120 "Wrong key for %s. No tries left.", 1121 pp->name); 1122 g_eli_keyfiles_clear(pp->name); 1123 return (NULL); 1124 } 1125 G_ELI_DEBUG(0, "Wrong key for %s. Tries left: %u.", 1126 pp->name, tries - i - 1); 1127 /* Try again. */ 1128 continue; 1129 } else if (error > 0) { 1130 G_ELI_DEBUG(0, 1131 "Cannot decrypt Master Key for %s (error=%d).", 1132 pp->name, error); 1133 g_eli_keyfiles_clear(pp->name); 1134 return (NULL); 1135 } 1136 g_eli_keyfiles_clear(pp->name); 1137 G_ELI_DEBUG(1, "Using Master Key %u for %s.", nkey, pp->name); 1138 break; 1139 } 1140 1141 /* 1142 * We have correct key, let's attach provider. 1143 */ 1144 gp = g_eli_create(NULL, mp, pp, &md, mkey, nkey); 1145 bzero(mkey, sizeof(mkey)); 1146 bzero(&md, sizeof(md)); 1147 if (gp == NULL) { 1148 G_ELI_DEBUG(0, "Cannot create device %s%s.", pp->name, 1149 G_ELI_SUFFIX); 1150 return (NULL); 1151 } 1152 return (gp); 1153 } 1154 1155 static void 1156 g_eli_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, 1157 struct g_consumer *cp, struct g_provider *pp) 1158 { 1159 struct g_eli_softc *sc; 1160 1161 g_topology_assert(); 1162 sc = gp->softc; 1163 if (sc == NULL) 1164 return; 1165 if (pp != NULL || cp != NULL) 1166 return; /* Nothing here. */ 1167 1168 sbuf_printf(sb, "%s<KeysTotal>%ju</KeysTotal>\n", indent, 1169 (uintmax_t)sc->sc_ekeys_total); 1170 sbuf_printf(sb, "%s<KeysAllocated>%ju</KeysAllocated>\n", indent, 1171 (uintmax_t)sc->sc_ekeys_allocated); 1172 sbuf_printf(sb, "%s<Flags>", indent); 1173 if (sc->sc_flags == 0) 1174 sbuf_printf(sb, "NONE"); 1175 else { 1176 int first = 1; 1177 1178 #define ADD_FLAG(flag, name) do { \ 1179 if (sc->sc_flags & (flag)) { \ 1180 if (!first) \ 1181 sbuf_printf(sb, ", "); \ 1182 else \ 1183 first = 0; \ 1184 sbuf_printf(sb, name); \ 1185 } \ 1186 } while (0) 1187 ADD_FLAG(G_ELI_FLAG_SUSPEND, "SUSPEND"); 1188 ADD_FLAG(G_ELI_FLAG_SINGLE_KEY, "SINGLE-KEY"); 1189 ADD_FLAG(G_ELI_FLAG_NATIVE_BYTE_ORDER, "NATIVE-BYTE-ORDER"); 1190 ADD_FLAG(G_ELI_FLAG_ONETIME, "ONETIME"); 1191 ADD_FLAG(G_ELI_FLAG_BOOT, "BOOT"); 1192 ADD_FLAG(G_ELI_FLAG_WO_DETACH, "W-DETACH"); 1193 ADD_FLAG(G_ELI_FLAG_RW_DETACH, "RW-DETACH"); 1194 ADD_FLAG(G_ELI_FLAG_AUTH, "AUTH"); 1195 ADD_FLAG(G_ELI_FLAG_WOPEN, "W-OPEN"); 1196 ADD_FLAG(G_ELI_FLAG_DESTROY, "DESTROY"); 1197 ADD_FLAG(G_ELI_FLAG_RO, "READ-ONLY"); 1198 #undef ADD_FLAG 1199 } 1200 sbuf_printf(sb, "</Flags>\n"); 1201 1202 if (!(sc->sc_flags & G_ELI_FLAG_ONETIME)) { 1203 sbuf_printf(sb, "%s<UsedKey>%u</UsedKey>\n", indent, 1204 sc->sc_nkey); 1205 } 1206 sbuf_printf(sb, "%s<Version>%u</Version>\n", indent, sc->sc_version); 1207 sbuf_printf(sb, "%s<Crypto>", indent); 1208 switch (sc->sc_crypto) { 1209 case G_ELI_CRYPTO_HW: 1210 sbuf_printf(sb, "hardware"); 1211 break; 1212 case G_ELI_CRYPTO_SW: 1213 sbuf_printf(sb, "software"); 1214 break; 1215 default: 1216 sbuf_printf(sb, "UNKNOWN"); 1217 break; 1218 } 1219 sbuf_printf(sb, "</Crypto>\n"); 1220 if (sc->sc_flags & G_ELI_FLAG_AUTH) { 1221 sbuf_printf(sb, 1222 "%s<AuthenticationAlgorithm>%s</AuthenticationAlgorithm>\n", 1223 indent, g_eli_algo2str(sc->sc_aalgo)); 1224 } 1225 sbuf_printf(sb, "%s<KeyLength>%u</KeyLength>\n", indent, 1226 sc->sc_ekeylen); 1227 sbuf_printf(sb, "%s<EncryptionAlgorithm>%s</EncryptionAlgorithm>\n", 1228 indent, g_eli_algo2str(sc->sc_ealgo)); 1229 sbuf_printf(sb, "%s<State>%s</State>\n", indent, 1230 (sc->sc_flags & G_ELI_FLAG_SUSPEND) ? "SUSPENDED" : "ACTIVE"); 1231 } 1232 1233 static void 1234 g_eli_shutdown_pre_sync(void *arg, int howto) 1235 { 1236 struct g_class *mp; 1237 struct g_geom *gp, *gp2; 1238 struct g_provider *pp; 1239 struct g_eli_softc *sc; 1240 int error; 1241 1242 mp = arg; 1243 DROP_GIANT(); 1244 g_topology_lock(); 1245 LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) { 1246 sc = gp->softc; 1247 if (sc == NULL) 1248 continue; 1249 pp = LIST_FIRST(&gp->provider); 1250 KASSERT(pp != NULL, ("No provider? gp=%p (%s)", gp, gp->name)); 1251 if (pp->acr + pp->acw + pp->ace == 0) 1252 error = g_eli_destroy(sc, TRUE); 1253 else { 1254 sc->sc_flags |= G_ELI_FLAG_RW_DETACH; 1255 gp->access = g_eli_access; 1256 } 1257 } 1258 g_topology_unlock(); 1259 PICKUP_GIANT(); 1260 } 1261 1262 static void 1263 g_eli_init(struct g_class *mp) 1264 { 1265 1266 g_eli_pre_sync = EVENTHANDLER_REGISTER(shutdown_pre_sync, 1267 g_eli_shutdown_pre_sync, mp, SHUTDOWN_PRI_FIRST); 1268 if (g_eli_pre_sync == NULL) 1269 G_ELI_DEBUG(0, "Warning! Cannot register shutdown event."); 1270 } 1271 1272 static void 1273 g_eli_fini(struct g_class *mp) 1274 { 1275 1276 if (g_eli_pre_sync != NULL) 1277 EVENTHANDLER_DEREGISTER(shutdown_pre_sync, g_eli_pre_sync); 1278 } 1279 1280 DECLARE_GEOM_CLASS(g_eli_class, g_eli); 1281 MODULE_DEPEND(g_eli, crypto, 1, 1, 1); 1282