1c58794deSPawel Jakub Dawidek /*- 23728855aSPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 33728855aSPedro F. Giffuni * 42f07cdf8SPawel Jakub Dawidek * Copyright (c) 2005-2019 Pawel Jakub Dawidek <pawel@dawidek.net> 5c58794deSPawel Jakub Dawidek * All rights reserved. 6c58794deSPawel Jakub Dawidek * 7c58794deSPawel Jakub Dawidek * Redistribution and use in source and binary forms, with or without 8c58794deSPawel Jakub Dawidek * modification, are permitted provided that the following conditions 9c58794deSPawel Jakub Dawidek * are met: 10c58794deSPawel Jakub Dawidek * 1. Redistributions of source code must retain the above copyright 11c58794deSPawel Jakub Dawidek * notice, this list of conditions and the following disclaimer. 12c58794deSPawel Jakub Dawidek * 2. Redistributions in binary form must reproduce the above copyright 13c58794deSPawel Jakub Dawidek * notice, this list of conditions and the following disclaimer in the 14c58794deSPawel Jakub Dawidek * documentation and/or other materials provided with the distribution. 15c58794deSPawel Jakub Dawidek * 16c58794deSPawel Jakub Dawidek * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 17c58794deSPawel Jakub Dawidek * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18c58794deSPawel Jakub Dawidek * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19c58794deSPawel Jakub Dawidek * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 20c58794deSPawel Jakub Dawidek * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21c58794deSPawel Jakub Dawidek * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22c58794deSPawel Jakub Dawidek * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23c58794deSPawel Jakub Dawidek * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24c58794deSPawel Jakub Dawidek * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25c58794deSPawel Jakub Dawidek * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26c58794deSPawel Jakub Dawidek * SUCH DAMAGE. 27c58794deSPawel Jakub Dawidek */ 28c58794deSPawel Jakub Dawidek 29c58794deSPawel Jakub Dawidek #include <sys/cdefs.h> 30c58794deSPawel Jakub Dawidek __FBSDID("$FreeBSD$"); 31c58794deSPawel Jakub Dawidek 32c58794deSPawel Jakub Dawidek #include <sys/param.h> 33c58794deSPawel Jakub Dawidek #include <sys/systm.h> 34f6ce353eSAndriy Gapon #include <sys/cons.h> 35bc683a89SWarner Losh #include <sys/kenv.h> 36c58794deSPawel Jakub Dawidek #include <sys/kernel.h> 379af2131bSPawel Jakub Dawidek #include <sys/linker.h> 38c58794deSPawel Jakub Dawidek #include <sys/module.h> 39c58794deSPawel Jakub Dawidek #include <sys/lock.h> 40c58794deSPawel Jakub Dawidek #include <sys/mutex.h> 41c58794deSPawel Jakub Dawidek #include <sys/bio.h> 425d807a0eSAndrey V. Elsukov #include <sys/sbuf.h> 43c58794deSPawel Jakub Dawidek #include <sys/sysctl.h> 44c58794deSPawel Jakub Dawidek #include <sys/malloc.h> 45c5d387d0SPawel Jakub Dawidek #include <sys/eventhandler.h> 46c58794deSPawel Jakub Dawidek #include <sys/kthread.h> 47c58794deSPawel Jakub Dawidek #include <sys/proc.h> 48c58794deSPawel Jakub Dawidek #include <sys/sched.h> 49dd549194SPawel Jakub Dawidek #include <sys/smp.h> 50c58794deSPawel Jakub Dawidek #include <sys/uio.h> 51a80f82a4SPawel Jakub Dawidek #include <sys/vnode.h> 52c58794deSPawel Jakub Dawidek 537d874f0fSAlan Somers #include <machine/vmparam.h> 547d874f0fSAlan Somers 55c58794deSPawel Jakub Dawidek #include <vm/uma.h> 56*2dbc9a38SGleb Smirnoff #include <vm/vm.h> 57*2dbc9a38SGleb Smirnoff #include <vm/swap_pager.h> 58c58794deSPawel Jakub Dawidek 59c58794deSPawel Jakub Dawidek #include <geom/geom.h> 60ac03832eSConrad Meyer #include <geom/geom_dbg.h> 61c58794deSPawel Jakub Dawidek #include <geom/eli/g_eli.h> 62c58794deSPawel Jakub Dawidek #include <geom/eli/pkcs5v2.h> 63c58794deSPawel Jakub Dawidek 64ec5c0e5bSAllan Jude #include <crypto/intake.h> 65ec5c0e5bSAllan Jude 66cb08c2ccSAlexander Leidinger FEATURE(geom_eli, "GEOM crypto module"); 67c58794deSPawel Jakub Dawidek 68c58794deSPawel Jakub Dawidek MALLOC_DEFINE(M_ELI, "eli data", "GEOM_ELI Data"); 69c58794deSPawel Jakub Dawidek 70c58794deSPawel Jakub Dawidek SYSCTL_DECL(_kern_geom); 7153a6215cSPawel Biernacki SYSCTL_NODE(_kern_geom, OID_AUTO, eli, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 7253a6215cSPawel Biernacki "GEOM_ELI stuff"); 73a1f4a8c4SPawel Jakub Dawidek static int g_eli_version = G_ELI_VERSION; 74a1f4a8c4SPawel Jakub Dawidek SYSCTL_INT(_kern_geom_eli, OID_AUTO, version, CTLFLAG_RD, &g_eli_version, 0, 75a1f4a8c4SPawel Jakub Dawidek "GELI version"); 76f95168e0SPawel Jakub Dawidek int g_eli_debug = 0; 77af3b2549SHans Petter Selasky SYSCTL_INT(_kern_geom_eli, OID_AUTO, debug, CTLFLAG_RWTUN, &g_eli_debug, 0, 78c58794deSPawel Jakub Dawidek "Debug level"); 79c58794deSPawel Jakub Dawidek static u_int g_eli_tries = 3; 80af3b2549SHans Petter Selasky SYSCTL_UINT(_kern_geom_eli, OID_AUTO, tries, CTLFLAG_RWTUN, &g_eli_tries, 0, 8198645006SChristian Brueffer "Number of tries for entering the passphrase"); 82eb4c31fdSEd Schouten static u_int g_eli_visible_passphrase = GETS_NOECHO; 83af3b2549SHans Petter Selasky SYSCTL_UINT(_kern_geom_eli, OID_AUTO, visible_passphrase, CTLFLAG_RWTUN, 84c58794deSPawel Jakub Dawidek &g_eli_visible_passphrase, 0, 85eb4c31fdSEd Schouten "Visibility of passphrase prompt (0 = invisible, 1 = visible, 2 = asterisk)"); 86b35bfe7eSPawel Jakub Dawidek u_int g_eli_overwrites = G_ELI_OVERWRITES; 87af3b2549SHans Petter Selasky SYSCTL_UINT(_kern_geom_eli, OID_AUTO, overwrites, CTLFLAG_RWTUN, &g_eli_overwrites, 8898645006SChristian Brueffer 0, "Number of times on-disk keys should be overwritten when destroying them"); 89dd549194SPawel Jakub Dawidek static u_int g_eli_threads = 0; 90af3b2549SHans Petter Selasky SYSCTL_UINT(_kern_geom_eli, OID_AUTO, threads, CTLFLAG_RWTUN, &g_eli_threads, 0, 91c58794deSPawel Jakub Dawidek "Number of threads doing crypto work"); 92eaa3b919SPawel Jakub Dawidek u_int g_eli_batch = 0; 93af3b2549SHans Petter Selasky SYSCTL_UINT(_kern_geom_eli, OID_AUTO, batch, CTLFLAG_RWTUN, &g_eli_batch, 0, 94eaa3b919SPawel Jakub Dawidek "Use crypto operations batching"); 95*2dbc9a38SGleb Smirnoff static u_int g_eli_minbufs = 16; 96*2dbc9a38SGleb Smirnoff static int sysctl_g_eli_minbufs(SYSCTL_HANDLER_ARGS); 97*2dbc9a38SGleb Smirnoff SYSCTL_PROC(_kern_geom_eli, OID_AUTO, minbufs, CTLTYPE_UINT | CTLFLAG_RW | 98*2dbc9a38SGleb Smirnoff CTLFLAG_MPSAFE, NULL, 0, sysctl_g_eli_minbufs, "IU", 99*2dbc9a38SGleb Smirnoff "Number of GELI bufs reserved for swap transactions"); 100*2dbc9a38SGleb Smirnoff static struct sx g_eli_umalock; /* Controls changes to UMA zone. */ 101*2dbc9a38SGleb Smirnoff SX_SYSINIT(g_eli_umalock, &g_eli_umalock, "GELI UMA"); 102*2dbc9a38SGleb Smirnoff static uma_zone_t g_eli_uma = NULL; 103*2dbc9a38SGleb Smirnoff static int g_eli_alloc_sz; 104*2dbc9a38SGleb Smirnoff static volatile int g_eli_umaoutstanding; 105*2dbc9a38SGleb Smirnoff static volatile int g_eli_devs; 106*2dbc9a38SGleb Smirnoff static bool g_eli_blocking_malloc = false; 107*2dbc9a38SGleb Smirnoff SYSCTL_BOOL(_kern_geom_eli, OID_AUTO, blocking_malloc, CTLFLAG_RWTUN, 108*2dbc9a38SGleb Smirnoff &g_eli_blocking_malloc, 0, "Use blocking malloc calls for GELI buffers"); 109*2dbc9a38SGleb Smirnoff 110*2dbc9a38SGleb Smirnoff /* 111*2dbc9a38SGleb Smirnoff * Control the number of reserved entries in the GELI zone. 112*2dbc9a38SGleb Smirnoff * If the GELI zone has already been allocated, update the zone. Otherwise, 113*2dbc9a38SGleb Smirnoff * simply update the variable for use the next time the zone is created. 114*2dbc9a38SGleb Smirnoff */ 115*2dbc9a38SGleb Smirnoff static int 116*2dbc9a38SGleb Smirnoff sysctl_g_eli_minbufs(SYSCTL_HANDLER_ARGS) 117*2dbc9a38SGleb Smirnoff { 118*2dbc9a38SGleb Smirnoff int error; 119*2dbc9a38SGleb Smirnoff u_int new; 120*2dbc9a38SGleb Smirnoff 121*2dbc9a38SGleb Smirnoff new = g_eli_minbufs; 122*2dbc9a38SGleb Smirnoff error = sysctl_handle_int(oidp, &new, 0, req); 123*2dbc9a38SGleb Smirnoff if (error != 0 || req->newptr == NULL) 124*2dbc9a38SGleb Smirnoff return (error); 125*2dbc9a38SGleb Smirnoff sx_xlock(&g_eli_umalock); 126*2dbc9a38SGleb Smirnoff if (g_eli_uma != NULL) { 127*2dbc9a38SGleb Smirnoff if (new != g_eli_minbufs) 128*2dbc9a38SGleb Smirnoff uma_zone_reserve(g_eli_uma, new); 129*2dbc9a38SGleb Smirnoff if (new > g_eli_minbufs) 130*2dbc9a38SGleb Smirnoff uma_prealloc(g_eli_uma, new - g_eli_minbufs); 131*2dbc9a38SGleb Smirnoff } 132*2dbc9a38SGleb Smirnoff if (new != g_eli_minbufs) 133*2dbc9a38SGleb Smirnoff g_eli_minbufs = new; 134*2dbc9a38SGleb Smirnoff sx_xunlock(&g_eli_umalock); 135*2dbc9a38SGleb Smirnoff return (0); 136*2dbc9a38SGleb Smirnoff } 137c58794deSPawel Jakub Dawidek 138835c4dd4SColin Percival /* 139835c4dd4SColin Percival * Passphrase cached during boot, in order to be more user-friendly if 140835c4dd4SColin Percival * there are multiple providers using the same passphrase. 141835c4dd4SColin Percival */ 142835c4dd4SColin Percival static char cached_passphrase[256]; 143835c4dd4SColin Percival static u_int g_eli_boot_passcache = 1; 144835c4dd4SColin Percival TUNABLE_INT("kern.geom.eli.boot_passcache", &g_eli_boot_passcache); 145835c4dd4SColin Percival SYSCTL_UINT(_kern_geom_eli, OID_AUTO, boot_passcache, CTLFLAG_RD, 146835c4dd4SColin Percival &g_eli_boot_passcache, 0, 147835c4dd4SColin Percival "Passphrases are cached during boot process for possible reuse"); 148835c4dd4SColin Percival static void 14966427784SColin Percival fetch_loader_passphrase(void * dummy) 15066427784SColin Percival { 15166427784SColin Percival char * env_passphrase; 15266427784SColin Percival 15366427784SColin Percival KASSERT(dynamic_kenv, ("need dynamic kenv")); 15466427784SColin Percival 15566427784SColin Percival if ((env_passphrase = kern_getenv("kern.geom.eli.passphrase")) != NULL) { 15666427784SColin Percival /* Extract passphrase from the environment. */ 15766427784SColin Percival strlcpy(cached_passphrase, env_passphrase, 15866427784SColin Percival sizeof(cached_passphrase)); 15966427784SColin Percival freeenv(env_passphrase); 16066427784SColin Percival 16166427784SColin Percival /* Wipe the passphrase from the environment. */ 16266427784SColin Percival kern_unsetenv("kern.geom.eli.passphrase"); 16366427784SColin Percival } 16466427784SColin Percival } 16566427784SColin Percival SYSINIT(geli_fetch_loader_passphrase, SI_SUB_KMEM + 1, SI_ORDER_ANY, 16666427784SColin Percival fetch_loader_passphrase, NULL); 167ec5c0e5bSAllan Jude 16866427784SColin Percival static void 169ec5c0e5bSAllan Jude zero_boot_passcache(void) 170835c4dd4SColin Percival { 171835c4dd4SColin Percival 172ec5c0e5bSAllan Jude explicit_bzero(cached_passphrase, sizeof(cached_passphrase)); 173835c4dd4SColin Percival } 174ec5c0e5bSAllan Jude 175ec5c0e5bSAllan Jude static void 176ec5c0e5bSAllan Jude zero_geli_intake_keys(void) 177ec5c0e5bSAllan Jude { 178ec5c0e5bSAllan Jude struct keybuf *keybuf; 179ec5c0e5bSAllan Jude int i; 180ec5c0e5bSAllan Jude 181ec5c0e5bSAllan Jude if ((keybuf = get_keybuf()) != NULL) { 182ec5c0e5bSAllan Jude /* Scan the key buffer, clear all GELI keys. */ 183ec5c0e5bSAllan Jude for (i = 0; i < keybuf->kb_nents; i++) { 184ec5c0e5bSAllan Jude if (keybuf->kb_ents[i].ke_type == KEYBUF_TYPE_GELI) { 185ec5c0e5bSAllan Jude explicit_bzero(keybuf->kb_ents[i].ke_data, 186ec5c0e5bSAllan Jude sizeof(keybuf->kb_ents[i].ke_data)); 187ec5c0e5bSAllan Jude keybuf->kb_ents[i].ke_type = KEYBUF_TYPE_NONE; 188ec5c0e5bSAllan Jude } 189ec5c0e5bSAllan Jude } 190ec5c0e5bSAllan Jude } 191ec5c0e5bSAllan Jude } 192ec5c0e5bSAllan Jude 193ec5c0e5bSAllan Jude static void 194ec5c0e5bSAllan Jude zero_intake_passcache(void *dummy) 195ec5c0e5bSAllan Jude { 196ec5c0e5bSAllan Jude zero_boot_passcache(); 197ec5c0e5bSAllan Jude zero_geli_intake_keys(); 198ec5c0e5bSAllan Jude } 199ec5c0e5bSAllan Jude EVENTHANDLER_DEFINE(mountroot, zero_intake_passcache, NULL, 0); 200835c4dd4SColin Percival 201c5d387d0SPawel Jakub Dawidek static eventhandler_tag g_eli_pre_sync = NULL; 202c5d387d0SPawel Jakub Dawidek 2032f07cdf8SPawel Jakub Dawidek static int g_eli_read_metadata_offset(struct g_class *mp, struct g_provider *pp, 2042f07cdf8SPawel Jakub Dawidek off_t offset, struct g_eli_metadata *md); 2052f07cdf8SPawel Jakub Dawidek 206c58794deSPawel Jakub Dawidek static int g_eli_destroy_geom(struct gctl_req *req, struct g_class *mp, 207c58794deSPawel Jakub Dawidek struct g_geom *gp); 208c5d387d0SPawel Jakub Dawidek static void g_eli_init(struct g_class *mp); 209c5d387d0SPawel Jakub Dawidek static void g_eli_fini(struct g_class *mp); 210c58794deSPawel Jakub Dawidek 211c58794deSPawel Jakub Dawidek static g_taste_t g_eli_taste; 212c58794deSPawel Jakub Dawidek static g_dumpconf_t g_eli_dumpconf; 213c58794deSPawel Jakub Dawidek 214c58794deSPawel Jakub Dawidek struct g_class g_eli_class = { 215c58794deSPawel Jakub Dawidek .name = G_ELI_CLASS_NAME, 216c58794deSPawel Jakub Dawidek .version = G_VERSION, 217c58794deSPawel Jakub Dawidek .ctlreq = g_eli_config, 218c58794deSPawel Jakub Dawidek .taste = g_eli_taste, 219c5d387d0SPawel Jakub Dawidek .destroy_geom = g_eli_destroy_geom, 220c5d387d0SPawel Jakub Dawidek .init = g_eli_init, 221c5d387d0SPawel Jakub Dawidek .fini = g_eli_fini 222c58794deSPawel Jakub Dawidek }; 223c58794deSPawel Jakub Dawidek 224c58794deSPawel Jakub Dawidek /* 225c58794deSPawel Jakub Dawidek * Code paths: 226c58794deSPawel Jakub Dawidek * BIO_READ: 2275ad4a7c7SPawel Jakub Dawidek * 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 228c58794deSPawel Jakub Dawidek * BIO_WRITE: 229c58794deSPawel Jakub Dawidek * g_eli_start -> g_eli_crypto_run -> g_eli_crypto_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver 230c58794deSPawel Jakub Dawidek */ 231c58794deSPawel Jakub Dawidek 232c58794deSPawel Jakub Dawidek /* 233c58794deSPawel Jakub Dawidek * EAGAIN from crypto(9) means, that we were probably balanced to another crypto 234c58794deSPawel Jakub Dawidek * accelerator or something like this. 235c58794deSPawel Jakub Dawidek * The function updates the SID and rerun the operation. 236c58794deSPawel Jakub Dawidek */ 237eaa3b919SPawel Jakub Dawidek int 238c58794deSPawel Jakub Dawidek g_eli_crypto_rerun(struct cryptop *crp) 239c58794deSPawel Jakub Dawidek { 240c58794deSPawel Jakub Dawidek struct g_eli_softc *sc; 241c58794deSPawel Jakub Dawidek struct g_eli_worker *wr; 242c58794deSPawel Jakub Dawidek struct bio *bp; 243c58794deSPawel Jakub Dawidek int error; 244c58794deSPawel Jakub Dawidek 245c58794deSPawel Jakub Dawidek bp = (struct bio *)crp->crp_opaque; 246c58794deSPawel Jakub Dawidek sc = bp->bio_to->geom->softc; 247c58794deSPawel Jakub Dawidek LIST_FOREACH(wr, &sc->sc_workers, w_next) { 248*2dbc9a38SGleb Smirnoff if (wr->w_number == G_ELI_WORKER(bp->bio_pflags)) 249c58794deSPawel Jakub Dawidek break; 250c58794deSPawel Jakub Dawidek } 251*2dbc9a38SGleb Smirnoff KASSERT(wr != NULL, ("Invalid worker (%u).", 252*2dbc9a38SGleb Smirnoff G_ELI_WORKER(bp->bio_pflags))); 2531b0909d5SConrad Meyer G_ELI_DEBUG(1, "Rerunning crypto %s request (sid: %p -> %p).", 2541b0909d5SConrad Meyer bp->bio_cmd == BIO_READ ? "READ" : "WRITE", wr->w_sid, 2551b0909d5SConrad Meyer crp->crp_session); 2561b0909d5SConrad Meyer wr->w_sid = crp->crp_session; 257c58794deSPawel Jakub Dawidek crp->crp_etype = 0; 258c58794deSPawel Jakub Dawidek error = crypto_dispatch(crp); 259c58794deSPawel Jakub Dawidek if (error == 0) 260c58794deSPawel Jakub Dawidek return (0); 261c58794deSPawel Jakub Dawidek G_ELI_DEBUG(1, "%s: crypto_dispatch() returned %d.", __func__, error); 262c58794deSPawel Jakub Dawidek crp->crp_etype = error; 263c58794deSPawel Jakub Dawidek return (error); 264c58794deSPawel Jakub Dawidek } 265c58794deSPawel Jakub Dawidek 2660bab7fa8SAlan Somers static void 2670bab7fa8SAlan Somers g_eli_getattr_done(struct bio *bp) 2680bab7fa8SAlan Somers { 2690bab7fa8SAlan Somers if (bp->bio_error == 0 && 2700bab7fa8SAlan Somers !strcmp(bp->bio_attribute, "GEOM::physpath")) { 2710bab7fa8SAlan Somers strlcat(bp->bio_data, "/eli", bp->bio_length); 2720bab7fa8SAlan Somers } 2730bab7fa8SAlan Somers g_std_done(bp); 2740bab7fa8SAlan Somers } 2750bab7fa8SAlan Somers 276c58794deSPawel Jakub Dawidek /* 277c58794deSPawel Jakub Dawidek * The function is called afer reading encrypted data from the provider. 278bb30fea6SPawel Jakub Dawidek * 2795ad4a7c7SPawel Jakub Dawidek * 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 280c58794deSPawel Jakub Dawidek */ 281eaa3b919SPawel Jakub Dawidek void 282c58794deSPawel Jakub Dawidek g_eli_read_done(struct bio *bp) 283c58794deSPawel Jakub Dawidek { 284c58794deSPawel Jakub Dawidek struct g_eli_softc *sc; 285c58794deSPawel Jakub Dawidek struct bio *pbp; 286c58794deSPawel Jakub Dawidek 287c58794deSPawel Jakub Dawidek G_ELI_LOGREQ(2, bp, "Request done."); 288c58794deSPawel Jakub Dawidek pbp = bp->bio_parent; 2892dc7e36bSSteven Hartland if (pbp->bio_error == 0 && bp->bio_error != 0) 290c58794deSPawel Jakub Dawidek pbp->bio_error = bp->bio_error; 29190574b0aSMikolaj Golub g_destroy_bio(bp); 292eaa3b919SPawel Jakub Dawidek /* 293eaa3b919SPawel Jakub Dawidek * Do we have all sectors already? 294eaa3b919SPawel Jakub Dawidek */ 295eaa3b919SPawel Jakub Dawidek pbp->bio_inbed++; 296eaa3b919SPawel Jakub Dawidek if (pbp->bio_inbed < pbp->bio_children) 297eaa3b919SPawel Jakub Dawidek return; 2985ad4a7c7SPawel Jakub Dawidek sc = pbp->bio_to->geom->softc; 299c58794deSPawel Jakub Dawidek if (pbp->bio_error != 0) { 3002dc7e36bSSteven Hartland G_ELI_LOGREQ(0, pbp, "%s() failed (error=%d)", __func__, 3012dc7e36bSSteven Hartland pbp->bio_error); 302c58794deSPawel Jakub Dawidek pbp->bio_completed = 0; 303*2dbc9a38SGleb Smirnoff g_eli_free_data(pbp); 304c58794deSPawel Jakub Dawidek g_io_deliver(pbp, pbp->bio_error); 30578f79a9aSMariusz Zaborski if (sc != NULL) 3065ad4a7c7SPawel Jakub Dawidek atomic_subtract_int(&sc->sc_inflight, 1); 307c58794deSPawel Jakub Dawidek return; 308c58794deSPawel Jakub Dawidek } 309c58794deSPawel Jakub Dawidek mtx_lock(&sc->sc_queue_mtx); 310c58794deSPawel Jakub Dawidek bioq_insert_tail(&sc->sc_queue, pbp); 311c58794deSPawel Jakub Dawidek mtx_unlock(&sc->sc_queue_mtx); 312c58794deSPawel Jakub Dawidek wakeup(sc); 313c58794deSPawel Jakub Dawidek } 314c58794deSPawel Jakub Dawidek 315c58794deSPawel Jakub Dawidek /* 316c58794deSPawel Jakub Dawidek * The function is called after we encrypt and write data. 317bb30fea6SPawel Jakub Dawidek * 318bb30fea6SPawel Jakub Dawidek * g_eli_start -> g_eli_crypto_run -> g_eli_crypto_write_done -> g_io_request -> G_ELI_WRITE_DONE -> g_io_deliver 319c58794deSPawel Jakub Dawidek */ 320eaa3b919SPawel Jakub Dawidek void 321c58794deSPawel Jakub Dawidek g_eli_write_done(struct bio *bp) 322c58794deSPawel Jakub Dawidek { 3235ad4a7c7SPawel Jakub Dawidek struct g_eli_softc *sc; 324c58794deSPawel Jakub Dawidek struct bio *pbp; 325c58794deSPawel Jakub Dawidek 326c58794deSPawel Jakub Dawidek G_ELI_LOGREQ(2, bp, "Request done."); 327c58794deSPawel Jakub Dawidek pbp = bp->bio_parent; 3282dc7e36bSSteven Hartland if (pbp->bio_error == 0 && bp->bio_error != 0) 329c58794deSPawel Jakub Dawidek pbp->bio_error = bp->bio_error; 33090574b0aSMikolaj Golub g_destroy_bio(bp); 331eaa3b919SPawel Jakub Dawidek /* 332eaa3b919SPawel Jakub Dawidek * Do we have all sectors already? 333eaa3b919SPawel Jakub Dawidek */ 334eaa3b919SPawel Jakub Dawidek pbp->bio_inbed++; 335eaa3b919SPawel Jakub Dawidek if (pbp->bio_inbed < pbp->bio_children) 336eaa3b919SPawel Jakub Dawidek return; 337*2dbc9a38SGleb Smirnoff sc = pbp->bio_to->geom->softc; 338*2dbc9a38SGleb Smirnoff g_eli_free_data(pbp); 339eaa3b919SPawel Jakub Dawidek if (pbp->bio_error != 0) { 3402dc7e36bSSteven Hartland G_ELI_LOGREQ(0, pbp, "%s() failed (error=%d)", __func__, 341c58794deSPawel Jakub Dawidek pbp->bio_error); 342c58794deSPawel Jakub Dawidek pbp->bio_completed = 0; 3432dc7e36bSSteven Hartland } else 3442dc7e36bSSteven Hartland pbp->bio_completed = pbp->bio_length; 3452dc7e36bSSteven Hartland 346c58794deSPawel Jakub Dawidek /* 347c58794deSPawel Jakub Dawidek * Write is finished, send it up. 348c58794deSPawel Jakub Dawidek */ 349c58794deSPawel Jakub Dawidek g_io_deliver(pbp, pbp->bio_error); 35078f79a9aSMariusz Zaborski if (sc != NULL) 3515ad4a7c7SPawel Jakub Dawidek atomic_subtract_int(&sc->sc_inflight, 1); 352c58794deSPawel Jakub Dawidek } 353c58794deSPawel Jakub Dawidek 354c58794deSPawel Jakub Dawidek /* 355c58794deSPawel Jakub Dawidek * This function should never be called, but GEOM made as it set ->orphan() 356c58794deSPawel Jakub Dawidek * method for every geom. 357c58794deSPawel Jakub Dawidek */ 358c58794deSPawel Jakub Dawidek static void 359c58794deSPawel Jakub Dawidek g_eli_orphan_spoil_assert(struct g_consumer *cp) 360c58794deSPawel Jakub Dawidek { 361c58794deSPawel Jakub Dawidek 362c58794deSPawel Jakub Dawidek panic("Function %s() called for %s.", __func__, cp->geom->name); 363c58794deSPawel Jakub Dawidek } 364c58794deSPawel Jakub Dawidek 365c58794deSPawel Jakub Dawidek static void 366c58794deSPawel Jakub Dawidek g_eli_orphan(struct g_consumer *cp) 367c58794deSPawel Jakub Dawidek { 368c58794deSPawel Jakub Dawidek struct g_eli_softc *sc; 369c58794deSPawel Jakub Dawidek 370c58794deSPawel Jakub Dawidek g_topology_assert(); 371c58794deSPawel Jakub Dawidek sc = cp->geom->softc; 372c58794deSPawel Jakub Dawidek if (sc == NULL) 373c58794deSPawel Jakub Dawidek return; 3745ad4a7c7SPawel Jakub Dawidek g_eli_destroy(sc, TRUE); 375c58794deSPawel Jakub Dawidek } 376c58794deSPawel Jakub Dawidek 3772f07cdf8SPawel Jakub Dawidek static void 3782f07cdf8SPawel Jakub Dawidek g_eli_resize(struct g_consumer *cp) 3792f07cdf8SPawel Jakub Dawidek { 3802f07cdf8SPawel Jakub Dawidek struct g_eli_softc *sc; 3812f07cdf8SPawel Jakub Dawidek struct g_provider *epp, *pp; 3822f07cdf8SPawel Jakub Dawidek off_t oldsize; 3832f07cdf8SPawel Jakub Dawidek 3842f07cdf8SPawel Jakub Dawidek g_topology_assert(); 3852f07cdf8SPawel Jakub Dawidek sc = cp->geom->softc; 3862f07cdf8SPawel Jakub Dawidek if (sc == NULL) 3872f07cdf8SPawel Jakub Dawidek return; 3882f07cdf8SPawel Jakub Dawidek 3892f07cdf8SPawel Jakub Dawidek if ((sc->sc_flags & G_ELI_FLAG_AUTORESIZE) == 0) { 3902f07cdf8SPawel Jakub Dawidek G_ELI_DEBUG(0, "Autoresize is turned off, old size: %jd.", 3912f07cdf8SPawel Jakub Dawidek (intmax_t)sc->sc_provsize); 3922f07cdf8SPawel Jakub Dawidek return; 3932f07cdf8SPawel Jakub Dawidek } 3942f07cdf8SPawel Jakub Dawidek 3952f07cdf8SPawel Jakub Dawidek pp = cp->provider; 3962f07cdf8SPawel Jakub Dawidek 3972f07cdf8SPawel Jakub Dawidek if ((sc->sc_flags & G_ELI_FLAG_ONETIME) == 0) { 3982f07cdf8SPawel Jakub Dawidek struct g_eli_metadata md; 3992f07cdf8SPawel Jakub Dawidek u_char *sector; 4002f07cdf8SPawel Jakub Dawidek int error; 4012f07cdf8SPawel Jakub Dawidek 4022f07cdf8SPawel Jakub Dawidek sector = NULL; 4032f07cdf8SPawel Jakub Dawidek 4042f07cdf8SPawel Jakub Dawidek error = g_eli_read_metadata_offset(cp->geom->class, pp, 4052f07cdf8SPawel Jakub Dawidek sc->sc_provsize - pp->sectorsize, &md); 4062f07cdf8SPawel Jakub Dawidek if (error != 0) { 4072f07cdf8SPawel Jakub Dawidek G_ELI_DEBUG(0, "Cannot read metadata from %s (error=%d).", 4082f07cdf8SPawel Jakub Dawidek pp->name, error); 4092f07cdf8SPawel Jakub Dawidek goto iofail; 4102f07cdf8SPawel Jakub Dawidek } 4112f07cdf8SPawel Jakub Dawidek 4122f07cdf8SPawel Jakub Dawidek md.md_provsize = pp->mediasize; 4132f07cdf8SPawel Jakub Dawidek 4142f07cdf8SPawel Jakub Dawidek sector = malloc(pp->sectorsize, M_ELI, M_WAITOK | M_ZERO); 4152f07cdf8SPawel Jakub Dawidek eli_metadata_encode(&md, sector); 4162f07cdf8SPawel Jakub Dawidek error = g_write_data(cp, pp->mediasize - pp->sectorsize, sector, 4172f07cdf8SPawel Jakub Dawidek pp->sectorsize); 4182f07cdf8SPawel Jakub Dawidek if (error != 0) { 4192f07cdf8SPawel Jakub Dawidek G_ELI_DEBUG(0, "Cannot store metadata on %s (error=%d).", 4202f07cdf8SPawel Jakub Dawidek pp->name, error); 4212f07cdf8SPawel Jakub Dawidek goto iofail; 4222f07cdf8SPawel Jakub Dawidek } 4232f07cdf8SPawel Jakub Dawidek explicit_bzero(sector, pp->sectorsize); 4242f07cdf8SPawel Jakub Dawidek error = g_write_data(cp, sc->sc_provsize - pp->sectorsize, 4252f07cdf8SPawel Jakub Dawidek sector, pp->sectorsize); 4262f07cdf8SPawel Jakub Dawidek if (error != 0) { 4272f07cdf8SPawel Jakub Dawidek G_ELI_DEBUG(0, "Cannot clear old metadata from %s (error=%d).", 4282f07cdf8SPawel Jakub Dawidek pp->name, error); 4292f07cdf8SPawel Jakub Dawidek goto iofail; 4302f07cdf8SPawel Jakub Dawidek } 4312f07cdf8SPawel Jakub Dawidek iofail: 4322f07cdf8SPawel Jakub Dawidek explicit_bzero(&md, sizeof(md)); 4334a711b8dSJohn Baldwin zfree(sector, M_ELI); 4342f07cdf8SPawel Jakub Dawidek } 4352f07cdf8SPawel Jakub Dawidek 4362f07cdf8SPawel Jakub Dawidek oldsize = sc->sc_mediasize; 4372f07cdf8SPawel Jakub Dawidek sc->sc_mediasize = eli_mediasize(sc, pp->mediasize, pp->sectorsize); 4382f07cdf8SPawel Jakub Dawidek g_eli_key_resize(sc); 4392f07cdf8SPawel Jakub Dawidek sc->sc_provsize = pp->mediasize; 4402f07cdf8SPawel Jakub Dawidek 4412f07cdf8SPawel Jakub Dawidek epp = LIST_FIRST(&sc->sc_geom->provider); 4422f07cdf8SPawel Jakub Dawidek g_resize_provider(epp, sc->sc_mediasize); 4432f07cdf8SPawel Jakub Dawidek G_ELI_DEBUG(0, "Device %s size changed from %jd to %jd.", epp->name, 4442f07cdf8SPawel Jakub Dawidek (intmax_t)oldsize, (intmax_t)sc->sc_mediasize); 4452f07cdf8SPawel Jakub Dawidek } 4462f07cdf8SPawel Jakub Dawidek 447bb30fea6SPawel Jakub Dawidek /* 448056638c4SPawel Jakub Dawidek * BIO_READ: 4495ad4a7c7SPawel Jakub Dawidek * 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 450056638c4SPawel Jakub Dawidek * BIO_WRITE: 451056638c4SPawel Jakub Dawidek * G_ELI_START -> g_eli_crypto_run -> g_eli_crypto_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver 452bb30fea6SPawel Jakub Dawidek */ 453c58794deSPawel Jakub Dawidek static void 454c58794deSPawel Jakub Dawidek g_eli_start(struct bio *bp) 455c58794deSPawel Jakub Dawidek { 456c58794deSPawel Jakub Dawidek struct g_eli_softc *sc; 457d3a1be90SPawel Jakub Dawidek struct g_consumer *cp; 458c58794deSPawel Jakub Dawidek struct bio *cbp; 459c58794deSPawel Jakub Dawidek 460c58794deSPawel Jakub Dawidek sc = bp->bio_to->geom->softc; 461c58794deSPawel Jakub Dawidek KASSERT(sc != NULL, 462c58794deSPawel Jakub Dawidek ("Provider's error should be set (error=%d)(device=%s).", 463c58794deSPawel Jakub Dawidek bp->bio_to->error, bp->bio_to->name)); 464c58794deSPawel Jakub Dawidek G_ELI_LOGREQ(2, bp, "Request received."); 465c58794deSPawel Jakub Dawidek 466c58794deSPawel Jakub Dawidek switch (bp->bio_cmd) { 467c58794deSPawel Jakub Dawidek case BIO_READ: 468c58794deSPawel Jakub Dawidek case BIO_WRITE: 469d3a1be90SPawel Jakub Dawidek case BIO_GETATTR: 47042461fbaSPawel Jakub Dawidek case BIO_FLUSH: 4719a6844d5SKenneth D. Merry case BIO_ZONE: 4728b522bdaSWarner Losh case BIO_SPEEDUP: 473c58794deSPawel Jakub Dawidek break; 474c58794deSPawel Jakub Dawidek case BIO_DELETE: 475c58794deSPawel Jakub Dawidek /* 47646e34470SPawel Jakub Dawidek * If the user hasn't set the NODELETE flag, we just pass 47746e34470SPawel Jakub Dawidek * it down the stack and let the layers beneath us do (or 47846e34470SPawel Jakub Dawidek * not) whatever they do with it. If they have, we 47946e34470SPawel Jakub Dawidek * reject it. A possible extension would be an 48046e34470SPawel Jakub Dawidek * additional flag to take it as a hint to shred the data 48146e34470SPawel Jakub Dawidek * with [multiple?] overwrites. 482c58794deSPawel Jakub Dawidek */ 48346e34470SPawel Jakub Dawidek if (!(sc->sc_flags & G_ELI_FLAG_NODELETE)) 48446e34470SPawel Jakub Dawidek break; 485c58794deSPawel Jakub Dawidek default: 486c58794deSPawel Jakub Dawidek g_io_deliver(bp, EOPNOTSUPP); 487c58794deSPawel Jakub Dawidek return; 488c58794deSPawel Jakub Dawidek } 489c58794deSPawel Jakub Dawidek cbp = g_clone_bio(bp); 490c58794deSPawel Jakub Dawidek if (cbp == NULL) { 491c58794deSPawel Jakub Dawidek g_io_deliver(bp, ENOMEM); 492c58794deSPawel Jakub Dawidek return; 493c58794deSPawel Jakub Dawidek } 4945ad4a7c7SPawel Jakub Dawidek bp->bio_driver1 = cbp; 495*2dbc9a38SGleb Smirnoff bp->bio_pflags = 0; 496*2dbc9a38SGleb Smirnoff G_ELI_SET_NEW_BIO(bp->bio_pflags); 497d3a1be90SPawel Jakub Dawidek switch (bp->bio_cmd) { 498d3a1be90SPawel Jakub Dawidek case BIO_READ: 499eaa3b919SPawel Jakub Dawidek if (!(sc->sc_flags & G_ELI_FLAG_AUTH)) { 5005ad4a7c7SPawel Jakub Dawidek g_eli_crypto_read(sc, bp, 0); 501d3a1be90SPawel Jakub Dawidek break; 502eaa3b919SPawel Jakub Dawidek } 503eaa3b919SPawel Jakub Dawidek /* FALLTHROUGH */ 504d3a1be90SPawel Jakub Dawidek case BIO_WRITE: 505c58794deSPawel Jakub Dawidek mtx_lock(&sc->sc_queue_mtx); 506c58794deSPawel Jakub Dawidek bioq_insert_tail(&sc->sc_queue, bp); 507c58794deSPawel Jakub Dawidek mtx_unlock(&sc->sc_queue_mtx); 508c58794deSPawel Jakub Dawidek wakeup(sc); 509d3a1be90SPawel Jakub Dawidek break; 510d3a1be90SPawel Jakub Dawidek case BIO_GETATTR: 51142461fbaSPawel Jakub Dawidek case BIO_FLUSH: 51246e34470SPawel Jakub Dawidek case BIO_DELETE: 5138b522bdaSWarner Losh case BIO_SPEEDUP: 5149a6844d5SKenneth D. Merry case BIO_ZONE: 5150bab7fa8SAlan Somers if (bp->bio_cmd == BIO_GETATTR) 5160bab7fa8SAlan Somers cbp->bio_done = g_eli_getattr_done; 5170bab7fa8SAlan Somers else 518d3a1be90SPawel Jakub Dawidek cbp->bio_done = g_std_done; 519d3a1be90SPawel Jakub Dawidek cp = LIST_FIRST(&sc->sc_geom->consumer); 520d3a1be90SPawel Jakub Dawidek cbp->bio_to = cp->provider; 521cd0d707eSPawel Jakub Dawidek G_ELI_LOGREQ(2, cbp, "Sending request."); 522d3a1be90SPawel Jakub Dawidek g_io_request(cbp, cp); 523d3a1be90SPawel Jakub Dawidek break; 524c58794deSPawel Jakub Dawidek } 525c58794deSPawel Jakub Dawidek } 526c58794deSPawel Jakub Dawidek 5273ac01bc2SPawel Jakub Dawidek static int 5283ac01bc2SPawel Jakub Dawidek g_eli_newsession(struct g_eli_worker *wr) 5293ac01bc2SPawel Jakub Dawidek { 5303ac01bc2SPawel Jakub Dawidek struct g_eli_softc *sc; 531c0341432SJohn Baldwin struct crypto_session_params csp; 532a3d565a1SJohn Baldwin uint32_t caps; 533a3d565a1SJohn Baldwin int error, new_crypto; 534c0341432SJohn Baldwin void *key; 5353ac01bc2SPawel Jakub Dawidek 5363ac01bc2SPawel Jakub Dawidek sc = wr->w_softc; 5373ac01bc2SPawel Jakub Dawidek 538c0341432SJohn Baldwin memset(&csp, 0, sizeof(csp)); 539c0341432SJohn Baldwin csp.csp_mode = CSP_MODE_CIPHER; 540c0341432SJohn Baldwin csp.csp_cipher_alg = sc->sc_ealgo; 541c0341432SJohn Baldwin csp.csp_ivlen = g_eli_ivlen(sc->sc_ealgo); 542c0341432SJohn Baldwin csp.csp_cipher_klen = sc->sc_ekeylen / 8; 5433ac01bc2SPawel Jakub Dawidek if (sc->sc_ealgo == CRYPTO_AES_XTS) 544c0341432SJohn Baldwin csp.csp_cipher_klen <<= 1; 545ad0a5236SPawel Jakub Dawidek if ((sc->sc_flags & G_ELI_FLAG_FIRST_KEY) != 0) { 546c0341432SJohn Baldwin key = g_eli_key_hold(sc, 0, 547ad0a5236SPawel Jakub Dawidek LIST_FIRST(&sc->sc_geom->consumer)->provider->sectorsize); 548c0341432SJohn Baldwin csp.csp_cipher_key = key; 549ad0a5236SPawel Jakub Dawidek } else { 550c0341432SJohn Baldwin key = NULL; 551c0341432SJohn Baldwin csp.csp_cipher_key = sc->sc_ekey; 552ad0a5236SPawel Jakub Dawidek } 5533ac01bc2SPawel Jakub Dawidek if (sc->sc_flags & G_ELI_FLAG_AUTH) { 554c0341432SJohn Baldwin csp.csp_mode = CSP_MODE_ETA; 555c0341432SJohn Baldwin csp.csp_auth_alg = sc->sc_aalgo; 556c0341432SJohn Baldwin csp.csp_auth_klen = G_ELI_AUTH_SECKEYLEN; 5573ac01bc2SPawel Jakub Dawidek } 5583ac01bc2SPawel Jakub Dawidek 5593ac01bc2SPawel Jakub Dawidek switch (sc->sc_crypto) { 560a3d565a1SJohn Baldwin case G_ELI_CRYPTO_SW_ACCEL: 5613ac01bc2SPawel Jakub Dawidek case G_ELI_CRYPTO_SW: 562c0341432SJohn Baldwin error = crypto_newsession(&wr->w_sid, &csp, 5633ac01bc2SPawel Jakub Dawidek CRYPTOCAP_F_SOFTWARE); 5643ac01bc2SPawel Jakub Dawidek break; 5653ac01bc2SPawel Jakub Dawidek case G_ELI_CRYPTO_HW: 566c0341432SJohn Baldwin error = crypto_newsession(&wr->w_sid, &csp, 5673ac01bc2SPawel Jakub Dawidek CRYPTOCAP_F_HARDWARE); 5683ac01bc2SPawel Jakub Dawidek break; 5693ac01bc2SPawel Jakub Dawidek case G_ELI_CRYPTO_UNKNOWN: 570c0341432SJohn Baldwin error = crypto_newsession(&wr->w_sid, &csp, 571a3d565a1SJohn Baldwin CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE); 5723ac01bc2SPawel Jakub Dawidek if (error == 0) { 573a3d565a1SJohn Baldwin caps = crypto_ses2caps(wr->w_sid); 574a3d565a1SJohn Baldwin if (caps & CRYPTOCAP_F_HARDWARE) 575a3d565a1SJohn Baldwin new_crypto = G_ELI_CRYPTO_HW; 576a3d565a1SJohn Baldwin else if (caps & CRYPTOCAP_F_ACCEL_SOFTWARE) 577a3d565a1SJohn Baldwin new_crypto = G_ELI_CRYPTO_SW_ACCEL; 578a3d565a1SJohn Baldwin else 579a3d565a1SJohn Baldwin new_crypto = G_ELI_CRYPTO_SW; 5803ac01bc2SPawel Jakub Dawidek mtx_lock(&sc->sc_queue_mtx); 5813ac01bc2SPawel Jakub Dawidek if (sc->sc_crypto == G_ELI_CRYPTO_UNKNOWN) 582a3d565a1SJohn Baldwin sc->sc_crypto = new_crypto; 5833ac01bc2SPawel Jakub Dawidek mtx_unlock(&sc->sc_queue_mtx); 5843ac01bc2SPawel Jakub Dawidek } 5853ac01bc2SPawel Jakub Dawidek break; 5863ac01bc2SPawel Jakub Dawidek default: 5873ac01bc2SPawel Jakub Dawidek panic("%s: invalid condition", __func__); 5883ac01bc2SPawel Jakub Dawidek } 5893ac01bc2SPawel Jakub Dawidek 590c0341432SJohn Baldwin if ((sc->sc_flags & G_ELI_FLAG_FIRST_KEY) != 0) { 591c0341432SJohn Baldwin if (error) 592c0341432SJohn Baldwin g_eli_key_drop(sc, key); 593c0341432SJohn Baldwin else 594c0341432SJohn Baldwin wr->w_first_key = key; 595c0341432SJohn Baldwin } 596ad0a5236SPawel Jakub Dawidek 5973ac01bc2SPawel Jakub Dawidek return (error); 5983ac01bc2SPawel Jakub Dawidek } 5993ac01bc2SPawel Jakub Dawidek 6003ac01bc2SPawel Jakub Dawidek static void 6013ac01bc2SPawel Jakub Dawidek g_eli_freesession(struct g_eli_worker *wr) 6023ac01bc2SPawel Jakub Dawidek { 603c0341432SJohn Baldwin struct g_eli_softc *sc; 6043ac01bc2SPawel Jakub Dawidek 6053ac01bc2SPawel Jakub Dawidek crypto_freesession(wr->w_sid); 606c0341432SJohn Baldwin if (wr->w_first_key != NULL) { 607c0341432SJohn Baldwin sc = wr->w_softc; 608c0341432SJohn Baldwin g_eli_key_drop(sc, wr->w_first_key); 609c0341432SJohn Baldwin wr->w_first_key = NULL; 610c0341432SJohn Baldwin } 6113ac01bc2SPawel Jakub Dawidek } 6123ac01bc2SPawel Jakub Dawidek 6135ad4a7c7SPawel Jakub Dawidek static void 6145ad4a7c7SPawel Jakub Dawidek g_eli_cancel(struct g_eli_softc *sc) 6155ad4a7c7SPawel Jakub Dawidek { 6165ad4a7c7SPawel Jakub Dawidek struct bio *bp; 6175ad4a7c7SPawel Jakub Dawidek 6185ad4a7c7SPawel Jakub Dawidek mtx_assert(&sc->sc_queue_mtx, MA_OWNED); 6195ad4a7c7SPawel Jakub Dawidek 6205ad4a7c7SPawel Jakub Dawidek while ((bp = bioq_takefirst(&sc->sc_queue)) != NULL) { 621*2dbc9a38SGleb Smirnoff KASSERT(G_ELI_IS_NEW_BIO(bp->bio_pflags), 6225ad4a7c7SPawel Jakub Dawidek ("Not new bio when canceling (bp=%p).", bp)); 6235ad4a7c7SPawel Jakub Dawidek g_io_deliver(bp, ENXIO); 6245ad4a7c7SPawel Jakub Dawidek } 6255ad4a7c7SPawel Jakub Dawidek } 6265ad4a7c7SPawel Jakub Dawidek 6275ad4a7c7SPawel Jakub Dawidek static struct bio * 6285ad4a7c7SPawel Jakub Dawidek g_eli_takefirst(struct g_eli_softc *sc) 6295ad4a7c7SPawel Jakub Dawidek { 6305ad4a7c7SPawel Jakub Dawidek struct bio *bp; 6315ad4a7c7SPawel Jakub Dawidek 6325ad4a7c7SPawel Jakub Dawidek mtx_assert(&sc->sc_queue_mtx, MA_OWNED); 6335ad4a7c7SPawel Jakub Dawidek 6345ad4a7c7SPawel Jakub Dawidek if (!(sc->sc_flags & G_ELI_FLAG_SUSPEND)) 6355ad4a7c7SPawel Jakub Dawidek return (bioq_takefirst(&sc->sc_queue)); 6365ad4a7c7SPawel Jakub Dawidek /* 6375ad4a7c7SPawel Jakub Dawidek * Device suspended, so we skip new I/O requests. 6385ad4a7c7SPawel Jakub Dawidek */ 6395ad4a7c7SPawel Jakub Dawidek TAILQ_FOREACH(bp, &sc->sc_queue.queue, bio_queue) { 640*2dbc9a38SGleb Smirnoff if (!G_ELI_IS_NEW_BIO(bp->bio_pflags)) 6415ad4a7c7SPawel Jakub Dawidek break; 6425ad4a7c7SPawel Jakub Dawidek } 6435ad4a7c7SPawel Jakub Dawidek if (bp != NULL) 6445ad4a7c7SPawel Jakub Dawidek bioq_remove(&sc->sc_queue, bp); 6455ad4a7c7SPawel Jakub Dawidek return (bp); 6465ad4a7c7SPawel Jakub Dawidek } 6475ad4a7c7SPawel Jakub Dawidek 648c58794deSPawel Jakub Dawidek /* 649c58794deSPawel Jakub Dawidek * This is the main function for kernel worker thread when we don't have 650c58794deSPawel Jakub Dawidek * hardware acceleration and we have to do cryptography in software. 651c58794deSPawel Jakub Dawidek * Dedicated thread is needed, so we don't slow down g_up/g_down GEOM 652c58794deSPawel Jakub Dawidek * threads with crypto work. 653c58794deSPawel Jakub Dawidek */ 654c58794deSPawel Jakub Dawidek static void 655c58794deSPawel Jakub Dawidek g_eli_worker(void *arg) 656c58794deSPawel Jakub Dawidek { 657c58794deSPawel Jakub Dawidek struct g_eli_softc *sc; 658c58794deSPawel Jakub Dawidek struct g_eli_worker *wr; 659c58794deSPawel Jakub Dawidek struct bio *bp; 6603ac01bc2SPawel Jakub Dawidek int error; 661c58794deSPawel Jakub Dawidek 662c58794deSPawel Jakub Dawidek wr = arg; 663c58794deSPawel Jakub Dawidek sc = wr->w_softc; 664fdce57a0SJohn Baldwin #ifdef EARLY_AP_STARTUP 665fdce57a0SJohn Baldwin MPASS(!sc->sc_cpubind || smp_started); 666fdce57a0SJohn Baldwin #elif defined(SMP) 667a1ea1a22SPawel Jakub Dawidek /* Before sched_bind() to a CPU, wait for all CPUs to go on-line. */ 6680c879bd9SPawel Jakub Dawidek if (sc->sc_cpubind) { 669a1ea1a22SPawel Jakub Dawidek while (!smp_started) 670a1ea1a22SPawel Jakub Dawidek tsleep(wr, 0, "geli:smp", hz / 4); 671a1ea1a22SPawel Jakub Dawidek } 672a1ea1a22SPawel Jakub Dawidek #endif 673982d11f8SJeff Roberson thread_lock(curthread); 67431c4cef7SPawel Jakub Dawidek sched_prio(curthread, PUSER); 6750c879bd9SPawel Jakub Dawidek if (sc->sc_cpubind) 6760c879bd9SPawel Jakub Dawidek sched_bind(curthread, wr->w_number % mp_ncpus); 677982d11f8SJeff Roberson thread_unlock(curthread); 678c58794deSPawel Jakub Dawidek 679c58794deSPawel Jakub Dawidek G_ELI_DEBUG(1, "Thread %s started.", curthread->td_proc->p_comm); 680c58794deSPawel Jakub Dawidek 681c58794deSPawel Jakub Dawidek for (;;) { 682c58794deSPawel Jakub Dawidek mtx_lock(&sc->sc_queue_mtx); 6835ad4a7c7SPawel Jakub Dawidek again: 6845ad4a7c7SPawel Jakub Dawidek bp = g_eli_takefirst(sc); 685c58794deSPawel Jakub Dawidek if (bp == NULL) { 686eaa3b919SPawel Jakub Dawidek if (sc->sc_flags & G_ELI_FLAG_DESTROY) { 6875ad4a7c7SPawel Jakub Dawidek g_eli_cancel(sc); 688c58794deSPawel Jakub Dawidek LIST_REMOVE(wr, w_next); 6893ac01bc2SPawel Jakub Dawidek g_eli_freesession(wr); 690c58794deSPawel Jakub Dawidek free(wr, M_ELI); 691c58794deSPawel Jakub Dawidek G_ELI_DEBUG(1, "Thread %s exiting.", 692c58794deSPawel Jakub Dawidek curthread->td_proc->p_comm); 693c58794deSPawel Jakub Dawidek wakeup(&sc->sc_workers); 694c58794deSPawel Jakub Dawidek mtx_unlock(&sc->sc_queue_mtx); 6953745c395SJulian Elischer kproc_exit(0); 696c58794deSPawel Jakub Dawidek } 6975ad4a7c7SPawel Jakub Dawidek while (sc->sc_flags & G_ELI_FLAG_SUSPEND) { 6985ad4a7c7SPawel Jakub Dawidek if (sc->sc_inflight > 0) { 699038c55adSPawel Jakub Dawidek G_ELI_DEBUG(0, "inflight=%d", 700038c55adSPawel Jakub Dawidek sc->sc_inflight); 7015ad4a7c7SPawel Jakub Dawidek /* 7025ad4a7c7SPawel Jakub Dawidek * We still have inflight BIOs, so 7035ad4a7c7SPawel Jakub Dawidek * sleep and retry. 7045ad4a7c7SPawel Jakub Dawidek */ 7055ad4a7c7SPawel Jakub Dawidek msleep(sc, &sc->sc_queue_mtx, PRIBIO, 7065ad4a7c7SPawel Jakub Dawidek "geli:inf", hz / 5); 7075ad4a7c7SPawel Jakub Dawidek goto again; 7085ad4a7c7SPawel Jakub Dawidek } 7095ad4a7c7SPawel Jakub Dawidek /* 7105ad4a7c7SPawel Jakub Dawidek * Suspend requested, mark the worker as 7115ad4a7c7SPawel Jakub Dawidek * suspended and go to sleep. 7125ad4a7c7SPawel Jakub Dawidek */ 7133ac01bc2SPawel Jakub Dawidek if (wr->w_active) { 7143ac01bc2SPawel Jakub Dawidek g_eli_freesession(wr); 7153ac01bc2SPawel Jakub Dawidek wr->w_active = FALSE; 7163ac01bc2SPawel Jakub Dawidek } 7175ad4a7c7SPawel Jakub Dawidek wakeup(&sc->sc_workers); 7185ad4a7c7SPawel Jakub Dawidek msleep(sc, &sc->sc_queue_mtx, PRIBIO, 7195ad4a7c7SPawel Jakub Dawidek "geli:suspend", 0); 7203ac01bc2SPawel Jakub Dawidek if (!wr->w_active && 7213ac01bc2SPawel Jakub Dawidek !(sc->sc_flags & G_ELI_FLAG_SUSPEND)) { 7223ac01bc2SPawel Jakub Dawidek error = g_eli_newsession(wr); 7233ac01bc2SPawel Jakub Dawidek KASSERT(error == 0, 7243ac01bc2SPawel Jakub Dawidek ("g_eli_newsession() failed on resume (error=%d)", 7253ac01bc2SPawel Jakub Dawidek error)); 7263ac01bc2SPawel Jakub Dawidek wr->w_active = TRUE; 7273ac01bc2SPawel Jakub Dawidek } 7285ad4a7c7SPawel Jakub Dawidek goto again; 7295ad4a7c7SPawel Jakub Dawidek } 73031c4cef7SPawel Jakub Dawidek msleep(sc, &sc->sc_queue_mtx, PDROP, "geli:w", 0); 731c58794deSPawel Jakub Dawidek continue; 732c58794deSPawel Jakub Dawidek } 733*2dbc9a38SGleb Smirnoff if (G_ELI_IS_NEW_BIO(bp->bio_pflags)) 7345ad4a7c7SPawel Jakub Dawidek atomic_add_int(&sc->sc_inflight, 1); 735c58794deSPawel Jakub Dawidek mtx_unlock(&sc->sc_queue_mtx); 736*2dbc9a38SGleb Smirnoff if (G_ELI_IS_NEW_BIO(bp->bio_pflags)) { 737*2dbc9a38SGleb Smirnoff G_ELI_SETWORKER(bp->bio_pflags, 0); 7385ad4a7c7SPawel Jakub Dawidek if (sc->sc_flags & G_ELI_FLAG_AUTH) { 7395ad4a7c7SPawel Jakub Dawidek if (bp->bio_cmd == BIO_READ) 740eaa3b919SPawel Jakub Dawidek g_eli_auth_read(sc, bp); 7415ad4a7c7SPawel Jakub Dawidek else 7425ad4a7c7SPawel Jakub Dawidek g_eli_auth_run(wr, bp); 7435ad4a7c7SPawel Jakub Dawidek } else { 7445ad4a7c7SPawel Jakub Dawidek if (bp->bio_cmd == BIO_READ) 7455ad4a7c7SPawel Jakub Dawidek g_eli_crypto_read(sc, bp, 1); 7465ad4a7c7SPawel Jakub Dawidek else 7475ad4a7c7SPawel Jakub Dawidek g_eli_crypto_run(wr, bp); 7485ad4a7c7SPawel Jakub Dawidek } 7495ad4a7c7SPawel Jakub Dawidek } else { 7505ad4a7c7SPawel Jakub Dawidek if (sc->sc_flags & G_ELI_FLAG_AUTH) 751eaa3b919SPawel Jakub Dawidek g_eli_auth_run(wr, bp); 752eaa3b919SPawel Jakub Dawidek else 753dddd1d53SPawel Jakub Dawidek g_eli_crypto_run(wr, bp); 754c58794deSPawel Jakub Dawidek } 755c58794deSPawel Jakub Dawidek } 7565ad4a7c7SPawel Jakub Dawidek } 757c58794deSPawel Jakub Dawidek 7582f07cdf8SPawel Jakub Dawidek static int 7592f07cdf8SPawel Jakub Dawidek g_eli_read_metadata_offset(struct g_class *mp, struct g_provider *pp, 7602f07cdf8SPawel Jakub Dawidek off_t offset, struct g_eli_metadata *md) 761c58794deSPawel Jakub Dawidek { 762c58794deSPawel Jakub Dawidek struct g_geom *gp; 763c58794deSPawel Jakub Dawidek struct g_consumer *cp; 764c58794deSPawel Jakub Dawidek u_char *buf = NULL; 765c58794deSPawel Jakub Dawidek int error; 766c58794deSPawel Jakub Dawidek 767c58794deSPawel Jakub Dawidek g_topology_assert(); 768c58794deSPawel Jakub Dawidek 769c58794deSPawel Jakub Dawidek gp = g_new_geomf(mp, "eli:taste"); 770c58794deSPawel Jakub Dawidek gp->start = g_eli_start; 771c58794deSPawel Jakub Dawidek gp->access = g_std_access; 772c58794deSPawel Jakub Dawidek /* 773c58794deSPawel Jakub Dawidek * g_eli_read_metadata() is always called from the event thread. 774c58794deSPawel Jakub Dawidek * Our geom is created and destroyed in the same event, so there 775c58794deSPawel Jakub Dawidek * could be no orphan nor spoil event in the meantime. 776c58794deSPawel Jakub Dawidek */ 777c58794deSPawel Jakub Dawidek gp->orphan = g_eli_orphan_spoil_assert; 778c58794deSPawel Jakub Dawidek gp->spoiled = g_eli_orphan_spoil_assert; 779c58794deSPawel Jakub Dawidek cp = g_new_consumer(gp); 7806f818c1fSAlan Somers cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE; 781c58794deSPawel Jakub Dawidek error = g_attach(cp, pp); 782c58794deSPawel Jakub Dawidek if (error != 0) 783c58794deSPawel Jakub Dawidek goto end; 784c58794deSPawel Jakub Dawidek error = g_access(cp, 1, 0, 0); 785c58794deSPawel Jakub Dawidek if (error != 0) 786c58794deSPawel Jakub Dawidek goto end; 787c58794deSPawel Jakub Dawidek g_topology_unlock(); 7882f07cdf8SPawel Jakub Dawidek buf = g_read_data(cp, offset, pp->sectorsize, &error); 789c58794deSPawel Jakub Dawidek g_topology_lock(); 7908a4a44b5SMaxim Sobolev if (buf == NULL) 791c58794deSPawel Jakub Dawidek goto end; 792fefb6a14SPawel Jakub Dawidek error = eli_metadata_decode(buf, md); 793fefb6a14SPawel Jakub Dawidek if (error != 0) 794fefb6a14SPawel Jakub Dawidek goto end; 795fefb6a14SPawel Jakub Dawidek /* Metadata was read and decoded successfully. */ 796c58794deSPawel Jakub Dawidek end: 797c58794deSPawel Jakub Dawidek if (buf != NULL) 798c58794deSPawel Jakub Dawidek g_free(buf); 799c58794deSPawel Jakub Dawidek if (cp->provider != NULL) { 800c58794deSPawel Jakub Dawidek if (cp->acr == 1) 801c58794deSPawel Jakub Dawidek g_access(cp, -1, 0, 0); 802c58794deSPawel Jakub Dawidek g_detach(cp); 803c58794deSPawel Jakub Dawidek } 804c58794deSPawel Jakub Dawidek g_destroy_consumer(cp); 805c58794deSPawel Jakub Dawidek g_destroy_geom(gp); 806c58794deSPawel Jakub Dawidek return (error); 807c58794deSPawel Jakub Dawidek } 808c58794deSPawel Jakub Dawidek 8092f07cdf8SPawel Jakub Dawidek int 8102f07cdf8SPawel Jakub Dawidek g_eli_read_metadata(struct g_class *mp, struct g_provider *pp, 8112f07cdf8SPawel Jakub Dawidek struct g_eli_metadata *md) 8122f07cdf8SPawel Jakub Dawidek { 8132f07cdf8SPawel Jakub Dawidek 8142f07cdf8SPawel Jakub Dawidek return (g_eli_read_metadata_offset(mp, pp, 8152f07cdf8SPawel Jakub Dawidek pp->mediasize - pp->sectorsize, md)); 8162f07cdf8SPawel Jakub Dawidek } 8172f07cdf8SPawel Jakub Dawidek 818c58794deSPawel Jakub Dawidek /* 819c58794deSPawel Jakub Dawidek * The function is called when we had last close on provider and user requested 820c58794deSPawel Jakub Dawidek * to close it when this situation occur. 821c58794deSPawel Jakub Dawidek */ 822c58794deSPawel Jakub Dawidek static void 82319351a14SAlexander Motin g_eli_last_close(void *arg, int flags __unused) 824c58794deSPawel Jakub Dawidek { 825c58794deSPawel Jakub Dawidek struct g_geom *gp; 82619351a14SAlexander Motin char gpname[64]; 827c58794deSPawel Jakub Dawidek int error; 828c58794deSPawel Jakub Dawidek 829c58794deSPawel Jakub Dawidek g_topology_assert(); 83019351a14SAlexander Motin gp = arg; 83119351a14SAlexander Motin strlcpy(gpname, gp->name, sizeof(gpname)); 83219351a14SAlexander Motin error = g_eli_destroy(gp->softc, TRUE); 833c58794deSPawel Jakub Dawidek KASSERT(error == 0, ("Cannot detach %s on last close (error=%d).", 83419351a14SAlexander Motin gpname, error)); 83519351a14SAlexander Motin G_ELI_DEBUG(0, "Detached %s on last close.", gpname); 836c58794deSPawel Jakub Dawidek } 837c58794deSPawel Jakub Dawidek 838c58794deSPawel Jakub Dawidek int 839c58794deSPawel Jakub Dawidek g_eli_access(struct g_provider *pp, int dr, int dw, int de) 840c58794deSPawel Jakub Dawidek { 841c58794deSPawel Jakub Dawidek struct g_eli_softc *sc; 842c58794deSPawel Jakub Dawidek struct g_geom *gp; 843c58794deSPawel Jakub Dawidek 844c58794deSPawel Jakub Dawidek gp = pp->geom; 845c58794deSPawel Jakub Dawidek sc = gp->softc; 846c58794deSPawel Jakub Dawidek 847c58794deSPawel Jakub Dawidek if (dw > 0) { 84885059016SPawel Jakub Dawidek if (sc->sc_flags & G_ELI_FLAG_RO) { 84985059016SPawel Jakub Dawidek /* Deny write attempts. */ 85085059016SPawel Jakub Dawidek return (EROFS); 85185059016SPawel Jakub Dawidek } 852c58794deSPawel Jakub Dawidek /* Someone is opening us for write, we need to remember that. */ 853c58794deSPawel Jakub Dawidek sc->sc_flags |= G_ELI_FLAG_WOPEN; 854c58794deSPawel Jakub Dawidek return (0); 855c58794deSPawel Jakub Dawidek } 856c58794deSPawel Jakub Dawidek /* Is this the last close? */ 857c58794deSPawel Jakub Dawidek if (pp->acr + dr > 0 || pp->acw + dw > 0 || pp->ace + de > 0) 858c58794deSPawel Jakub Dawidek return (0); 859c58794deSPawel Jakub Dawidek 860c58794deSPawel Jakub Dawidek /* 861c58794deSPawel Jakub Dawidek * Automatically detach on last close if requested. 862c58794deSPawel Jakub Dawidek */ 863c58794deSPawel Jakub Dawidek if ((sc->sc_flags & G_ELI_FLAG_RW_DETACH) || 864c58794deSPawel Jakub Dawidek (sc->sc_flags & G_ELI_FLAG_WOPEN)) { 86519351a14SAlexander Motin g_post_event(g_eli_last_close, gp, M_WAITOK, NULL); 866c58794deSPawel Jakub Dawidek } 867c58794deSPawel Jakub Dawidek return (0); 868c58794deSPawel Jakub Dawidek } 869c58794deSPawel Jakub Dawidek 870eba8f137SPawel Jakub Dawidek static int 871eba8f137SPawel Jakub Dawidek g_eli_cpu_is_disabled(int cpu) 872eba8f137SPawel Jakub Dawidek { 873eba8f137SPawel Jakub Dawidek #ifdef SMP 87471a19bdcSAttilio Rao return (CPU_ISSET(cpu, &hlt_cpus_mask)); 875eba8f137SPawel Jakub Dawidek #else 876eba8f137SPawel Jakub Dawidek return (0); 877eba8f137SPawel Jakub Dawidek #endif 878eba8f137SPawel Jakub Dawidek } 879eba8f137SPawel Jakub Dawidek 880*2dbc9a38SGleb Smirnoff static void 881*2dbc9a38SGleb Smirnoff g_eli_init_uma(void) 882*2dbc9a38SGleb Smirnoff { 883*2dbc9a38SGleb Smirnoff 884*2dbc9a38SGleb Smirnoff atomic_add_int(&g_eli_devs, 1); 885*2dbc9a38SGleb Smirnoff sx_xlock(&g_eli_umalock); 886*2dbc9a38SGleb Smirnoff if (g_eli_uma == NULL) { 887*2dbc9a38SGleb Smirnoff /* 888*2dbc9a38SGleb Smirnoff * Calculate the maximum-sized swap buffer we are 889*2dbc9a38SGleb Smirnoff * likely to see. 890*2dbc9a38SGleb Smirnoff */ 891*2dbc9a38SGleb Smirnoff g_eli_alloc_sz = roundup2((PAGE_SIZE + sizeof(int) + 892*2dbc9a38SGleb Smirnoff G_ELI_AUTH_SECKEYLEN) * nsw_cluster_max + 893*2dbc9a38SGleb Smirnoff sizeof(uintptr_t), PAGE_SIZE); 894*2dbc9a38SGleb Smirnoff 895*2dbc9a38SGleb Smirnoff /* 896*2dbc9a38SGleb Smirnoff * Create the zone, setting UMA_ZONE_NOFREE so we won't 897*2dbc9a38SGleb Smirnoff * drain the zone in a memory shortage. 898*2dbc9a38SGleb Smirnoff */ 899*2dbc9a38SGleb Smirnoff g_eli_uma = uma_zcreate("GELI buffers", g_eli_alloc_sz, 900*2dbc9a38SGleb Smirnoff NULL, NULL, NULL, NULL, 901*2dbc9a38SGleb Smirnoff UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 902*2dbc9a38SGleb Smirnoff 903*2dbc9a38SGleb Smirnoff /* Reserve and pre-allocate pages, as appropriate. */ 904*2dbc9a38SGleb Smirnoff uma_zone_reserve(g_eli_uma, g_eli_minbufs); 905*2dbc9a38SGleb Smirnoff uma_prealloc(g_eli_uma, g_eli_minbufs); 906*2dbc9a38SGleb Smirnoff } 907*2dbc9a38SGleb Smirnoff sx_xunlock(&g_eli_umalock); 908*2dbc9a38SGleb Smirnoff } 909*2dbc9a38SGleb Smirnoff 910*2dbc9a38SGleb Smirnoff /* 911*2dbc9a38SGleb Smirnoff * Try to destroy the UMA pool. This will do nothing if there are existing 912*2dbc9a38SGleb Smirnoff * GELI devices or existing UMA allocations. 913*2dbc9a38SGleb Smirnoff */ 914*2dbc9a38SGleb Smirnoff static void 915*2dbc9a38SGleb Smirnoff g_eli_destroy_uma(void) 916*2dbc9a38SGleb Smirnoff { 917*2dbc9a38SGleb Smirnoff uma_zone_t oldzone; 918*2dbc9a38SGleb Smirnoff 919*2dbc9a38SGleb Smirnoff sx_xlock(&g_eli_umalock); 920*2dbc9a38SGleb Smirnoff /* Ensure we really should be destroying this. */ 921*2dbc9a38SGleb Smirnoff if (atomic_load_int(&g_eli_devs) == 0 && 922*2dbc9a38SGleb Smirnoff atomic_load_int(&g_eli_umaoutstanding) == 0) { 923*2dbc9a38SGleb Smirnoff oldzone = g_eli_uma; 924*2dbc9a38SGleb Smirnoff g_eli_uma = NULL; 925*2dbc9a38SGleb Smirnoff } else 926*2dbc9a38SGleb Smirnoff oldzone = NULL; 927*2dbc9a38SGleb Smirnoff sx_xunlock(&g_eli_umalock); 928*2dbc9a38SGleb Smirnoff 929*2dbc9a38SGleb Smirnoff if (oldzone != NULL) 930*2dbc9a38SGleb Smirnoff uma_zdestroy(oldzone); 931*2dbc9a38SGleb Smirnoff } 932*2dbc9a38SGleb Smirnoff 933*2dbc9a38SGleb Smirnoff static void 934*2dbc9a38SGleb Smirnoff g_eli_fini_uma(void) 935*2dbc9a38SGleb Smirnoff { 936*2dbc9a38SGleb Smirnoff 937*2dbc9a38SGleb Smirnoff /* 938*2dbc9a38SGleb Smirnoff * If this is the last outstanding GELI device, try to 939*2dbc9a38SGleb Smirnoff * destroy the UMA pool. 940*2dbc9a38SGleb Smirnoff */ 941*2dbc9a38SGleb Smirnoff if (atomic_fetchadd_int(&g_eli_devs, -1) == 1) 942*2dbc9a38SGleb Smirnoff g_eli_destroy_uma(); 943*2dbc9a38SGleb Smirnoff } 944*2dbc9a38SGleb Smirnoff 945*2dbc9a38SGleb Smirnoff /* 946*2dbc9a38SGleb Smirnoff * Allocate a data buffer. If the size fits within our swap-sized buffers, 947*2dbc9a38SGleb Smirnoff * try to allocate a swap-sized buffer from the UMA pool. Otherwise, fall 948*2dbc9a38SGleb Smirnoff * back to using malloc. 949*2dbc9a38SGleb Smirnoff * 950*2dbc9a38SGleb Smirnoff * Swap-related requests are special: they can only use the UMA pool, they 951*2dbc9a38SGleb Smirnoff * use M_USE_RESERVE to let them dip farther into system resources, and 952*2dbc9a38SGleb Smirnoff * they always use M_NOWAIT to prevent swap operations from deadlocking. 953*2dbc9a38SGleb Smirnoff */ 954*2dbc9a38SGleb Smirnoff bool 955*2dbc9a38SGleb Smirnoff g_eli_alloc_data(struct bio *bp, int sz) 956*2dbc9a38SGleb Smirnoff { 957*2dbc9a38SGleb Smirnoff 958*2dbc9a38SGleb Smirnoff KASSERT(sz <= g_eli_alloc_sz || (bp->bio_flags & BIO_SWAP) == 0, 959*2dbc9a38SGleb Smirnoff ("BIO_SWAP request for %d bytes exceeds the precalculated buffer" 960*2dbc9a38SGleb Smirnoff " size (%d)", sz, g_eli_alloc_sz)); 961*2dbc9a38SGleb Smirnoff if (sz <= g_eli_alloc_sz) { 962*2dbc9a38SGleb Smirnoff bp->bio_driver2 = uma_zalloc(g_eli_uma, M_NOWAIT | 963*2dbc9a38SGleb Smirnoff ((bp->bio_flags & BIO_SWAP) != 0 ? M_USE_RESERVE : 0)); 964*2dbc9a38SGleb Smirnoff if (bp->bio_driver2 != NULL) { 965*2dbc9a38SGleb Smirnoff bp->bio_pflags |= G_ELI_UMA_ALLOC; 966*2dbc9a38SGleb Smirnoff atomic_add_int(&g_eli_umaoutstanding, 1); 967*2dbc9a38SGleb Smirnoff } 968*2dbc9a38SGleb Smirnoff if (bp->bio_driver2 != NULL || (bp->bio_flags & BIO_SWAP) != 0) 969*2dbc9a38SGleb Smirnoff return (bp->bio_driver2 != NULL); 970*2dbc9a38SGleb Smirnoff } 971*2dbc9a38SGleb Smirnoff bp->bio_pflags &= ~(G_ELI_UMA_ALLOC); 972*2dbc9a38SGleb Smirnoff bp->bio_driver2 = malloc(sz, M_ELI, g_eli_blocking_malloc ? M_WAITOK : 973*2dbc9a38SGleb Smirnoff M_NOWAIT); 974*2dbc9a38SGleb Smirnoff return (bp->bio_driver2 != NULL); 975*2dbc9a38SGleb Smirnoff } 976*2dbc9a38SGleb Smirnoff 977*2dbc9a38SGleb Smirnoff /* 978*2dbc9a38SGleb Smirnoff * Free a buffer from bp->bio_driver2 which was allocated with 979*2dbc9a38SGleb Smirnoff * g_eli_alloc_data(). This function makes sure that the memory is freed 980*2dbc9a38SGleb Smirnoff * to the correct place. 981*2dbc9a38SGleb Smirnoff * 982*2dbc9a38SGleb Smirnoff * Additionally, if this function frees the last outstanding UMA request 983*2dbc9a38SGleb Smirnoff * and there are no open GELI devices, this will destroy the UMA pool. 984*2dbc9a38SGleb Smirnoff */ 985*2dbc9a38SGleb Smirnoff void 986*2dbc9a38SGleb Smirnoff g_eli_free_data(struct bio *bp) 987*2dbc9a38SGleb Smirnoff { 988*2dbc9a38SGleb Smirnoff 989*2dbc9a38SGleb Smirnoff /* 990*2dbc9a38SGleb Smirnoff * Mimic the free(9) behavior of allowing a NULL pointer to be 991*2dbc9a38SGleb Smirnoff * freed. 992*2dbc9a38SGleb Smirnoff */ 993*2dbc9a38SGleb Smirnoff if (bp->bio_driver2 == NULL) 994*2dbc9a38SGleb Smirnoff return; 995*2dbc9a38SGleb Smirnoff 996*2dbc9a38SGleb Smirnoff if ((bp->bio_pflags & G_ELI_UMA_ALLOC) != 0) { 997*2dbc9a38SGleb Smirnoff uma_zfree(g_eli_uma, bp->bio_driver2); 998*2dbc9a38SGleb Smirnoff if (atomic_fetchadd_int(&g_eli_umaoutstanding, -1) == 1 && 999*2dbc9a38SGleb Smirnoff atomic_load_int(&g_eli_devs) == 0) 1000*2dbc9a38SGleb Smirnoff g_eli_destroy_uma(); 1001*2dbc9a38SGleb Smirnoff } else 1002*2dbc9a38SGleb Smirnoff free(bp->bio_driver2, M_ELI); 1003*2dbc9a38SGleb Smirnoff bp->bio_driver2 = NULL; 1004*2dbc9a38SGleb Smirnoff } 1005*2dbc9a38SGleb Smirnoff 1006c58794deSPawel Jakub Dawidek struct g_geom * 1007c58794deSPawel Jakub Dawidek g_eli_create(struct gctl_req *req, struct g_class *mp, struct g_provider *bpp, 1008c58794deSPawel Jakub Dawidek const struct g_eli_metadata *md, const u_char *mkey, int nkey) 1009c58794deSPawel Jakub Dawidek { 1010c58794deSPawel Jakub Dawidek struct g_eli_softc *sc; 1011c58794deSPawel Jakub Dawidek struct g_eli_worker *wr; 1012c58794deSPawel Jakub Dawidek struct g_geom *gp; 1013c58794deSPawel Jakub Dawidek struct g_provider *pp; 1014c58794deSPawel Jakub Dawidek struct g_consumer *cp; 1015ae1cce52SWarner Losh struct g_geom_alias *gap; 1016c58794deSPawel Jakub Dawidek u_int i, threads; 10173bb6e0f0SRyan Libby int dcw, error; 1018c58794deSPawel Jakub Dawidek 1019c58794deSPawel Jakub Dawidek G_ELI_DEBUG(1, "Creating device %s%s.", bpp->name, G_ELI_SUFFIX); 1020e2b99193SJohn Baldwin KASSERT(eli_metadata_crypto_supported(md), 1021e2b99193SJohn Baldwin ("%s: unsupported crypto for %s", __func__, bpp->name)); 1022c58794deSPawel Jakub Dawidek 1023c58794deSPawel Jakub Dawidek gp = g_new_geomf(mp, "%s%s", bpp->name, G_ELI_SUFFIX); 1024c58794deSPawel Jakub Dawidek sc = malloc(sizeof(*sc), M_ELI, M_WAITOK | M_ZERO); 1025c58794deSPawel Jakub Dawidek gp->start = g_eli_start; 1026c58794deSPawel Jakub Dawidek /* 10274273d412SPawel Jakub Dawidek * Spoiling can happen even though we have the provider open 10284273d412SPawel Jakub Dawidek * exclusively, e.g. through media change events. 1029c58794deSPawel Jakub Dawidek */ 10304273d412SPawel Jakub Dawidek gp->spoiled = g_eli_orphan; 1031c58794deSPawel Jakub Dawidek gp->orphan = g_eli_orphan; 10322f07cdf8SPawel Jakub Dawidek gp->resize = g_eli_resize; 103385059016SPawel Jakub Dawidek gp->dumpconf = g_eli_dumpconf; 1034c58794deSPawel Jakub Dawidek /* 103585059016SPawel Jakub Dawidek * If detach-on-last-close feature is not enabled and we don't operate 103685059016SPawel Jakub Dawidek * on read-only provider, we can simply use g_std_access(). 1037c58794deSPawel Jakub Dawidek */ 103885059016SPawel Jakub Dawidek if (md->md_flags & (G_ELI_FLAG_WO_DETACH | G_ELI_FLAG_RO)) 1039c58794deSPawel Jakub Dawidek gp->access = g_eli_access; 1040c58794deSPawel Jakub Dawidek else 1041c58794deSPawel Jakub Dawidek gp->access = g_std_access; 1042c58794deSPawel Jakub Dawidek 10434332fecaSAllan Jude eli_metadata_softc(sc, md, bpp->sectorsize, bpp->mediasize); 1044c58794deSPawel Jakub Dawidek sc->sc_nkey = nkey; 1045eaa3b919SPawel Jakub Dawidek 1046c58794deSPawel Jakub Dawidek gp->softc = sc; 1047c58794deSPawel Jakub Dawidek sc->sc_geom = gp; 1048c58794deSPawel Jakub Dawidek 1049c58794deSPawel Jakub Dawidek bioq_init(&sc->sc_queue); 1050c58794deSPawel Jakub Dawidek mtx_init(&sc->sc_queue_mtx, "geli:queue", NULL, MTX_DEF); 10511e09ff3dSPawel Jakub Dawidek mtx_init(&sc->sc_ekeys_lock, "geli:ekeys", NULL, MTX_DEF); 1052c58794deSPawel Jakub Dawidek 1053c58794deSPawel Jakub Dawidek pp = NULL; 1054c58794deSPawel Jakub Dawidek cp = g_new_consumer(gp); 10556f818c1fSAlan Somers cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE; 1056c58794deSPawel Jakub Dawidek error = g_attach(cp, bpp); 1057c58794deSPawel Jakub Dawidek if (error != 0) { 1058c58794deSPawel Jakub Dawidek if (req != NULL) { 1059c58794deSPawel Jakub Dawidek gctl_error(req, "Cannot attach to %s (error=%d).", 1060c58794deSPawel Jakub Dawidek bpp->name, error); 1061c58794deSPawel Jakub Dawidek } else { 1062c58794deSPawel Jakub Dawidek G_ELI_DEBUG(1, "Cannot attach to %s (error=%d).", 1063c58794deSPawel Jakub Dawidek bpp->name, error); 1064c58794deSPawel Jakub Dawidek } 1065c58794deSPawel Jakub Dawidek goto failed; 1066c58794deSPawel Jakub Dawidek } 1067c58794deSPawel Jakub Dawidek /* 1068c58794deSPawel Jakub Dawidek * Keep provider open all the time, so we can run critical tasks, 1069c58794deSPawel Jakub Dawidek * like Master Keys deletion, without wondering if we can open 1070c58794deSPawel Jakub Dawidek * provider or not. 107185059016SPawel Jakub Dawidek * We don't open provider for writing only when user requested read-only 107285059016SPawel Jakub Dawidek * access. 1073c58794deSPawel Jakub Dawidek */ 10743bb6e0f0SRyan Libby dcw = (sc->sc_flags & G_ELI_FLAG_RO) ? 0 : 1; 10753bb6e0f0SRyan Libby error = g_access(cp, 1, dcw, 1); 1076c58794deSPawel Jakub Dawidek if (error != 0) { 1077c58794deSPawel Jakub Dawidek if (req != NULL) { 1078c58794deSPawel Jakub Dawidek gctl_error(req, "Cannot access %s (error=%d).", 1079c58794deSPawel Jakub Dawidek bpp->name, error); 1080c58794deSPawel Jakub Dawidek } else { 1081c58794deSPawel Jakub Dawidek G_ELI_DEBUG(1, "Cannot access %s (error=%d).", 1082c58794deSPawel Jakub Dawidek bpp->name, error); 1083c58794deSPawel Jakub Dawidek } 1084c58794deSPawel Jakub Dawidek goto failed; 1085c58794deSPawel Jakub Dawidek } 1086c58794deSPawel Jakub Dawidek 1087c6a26d4cSPawel Jakub Dawidek /* 1088c6a26d4cSPawel Jakub Dawidek * Remember the keys in our softc structure. 1089c6a26d4cSPawel Jakub Dawidek */ 1090c6a26d4cSPawel Jakub Dawidek g_eli_mkey_propagate(sc, mkey); 1091c6a26d4cSPawel Jakub Dawidek 1092c58794deSPawel Jakub Dawidek LIST_INIT(&sc->sc_workers); 1093c58794deSPawel Jakub Dawidek 1094c58794deSPawel Jakub Dawidek threads = g_eli_threads; 1095dd549194SPawel Jakub Dawidek if (threads == 0) 1096dd549194SPawel Jakub Dawidek threads = mp_ncpus; 10970c879bd9SPawel Jakub Dawidek sc->sc_cpubind = (mp_ncpus > 1 && threads == mp_ncpus); 1098*2dbc9a38SGleb Smirnoff g_eli_init_uma(); 1099c58794deSPawel Jakub Dawidek for (i = 0; i < threads; i++) { 1100eba8f137SPawel Jakub Dawidek if (g_eli_cpu_is_disabled(i)) { 1101eba8f137SPawel Jakub Dawidek G_ELI_DEBUG(1, "%s: CPU %u disabled, skipping.", 11021506db21SPawel Jakub Dawidek bpp->name, i); 1103eba8f137SPawel Jakub Dawidek continue; 1104eba8f137SPawel Jakub Dawidek } 1105c58794deSPawel Jakub Dawidek wr = malloc(sizeof(*wr), M_ELI, M_WAITOK | M_ZERO); 1106c58794deSPawel Jakub Dawidek wr->w_softc = sc; 1107c58794deSPawel Jakub Dawidek wr->w_number = i; 11085ad4a7c7SPawel Jakub Dawidek wr->w_active = TRUE; 1109c58794deSPawel Jakub Dawidek 11103ac01bc2SPawel Jakub Dawidek error = g_eli_newsession(wr); 1111c58794deSPawel Jakub Dawidek if (error != 0) { 1112c58794deSPawel Jakub Dawidek free(wr, M_ELI); 1113c58794deSPawel Jakub Dawidek if (req != NULL) { 1114c58794deSPawel Jakub Dawidek gctl_error(req, "Cannot set up crypto session " 1115c58794deSPawel Jakub Dawidek "for %s (error=%d).", bpp->name, error); 1116c58794deSPawel Jakub Dawidek } else { 1117c58794deSPawel Jakub Dawidek G_ELI_DEBUG(1, "Cannot set up crypto session " 1118c58794deSPawel Jakub Dawidek "for %s (error=%d).", bpp->name, error); 1119c58794deSPawel Jakub Dawidek } 1120c58794deSPawel Jakub Dawidek goto failed; 1121c58794deSPawel Jakub Dawidek } 1122c58794deSPawel Jakub Dawidek 11233745c395SJulian Elischer error = kproc_create(g_eli_worker, wr, &wr->w_proc, 0, 0, 1124c58794deSPawel Jakub Dawidek "g_eli[%u] %s", i, bpp->name); 1125c58794deSPawel Jakub Dawidek if (error != 0) { 11263ac01bc2SPawel Jakub Dawidek g_eli_freesession(wr); 1127c58794deSPawel Jakub Dawidek free(wr, M_ELI); 1128c58794deSPawel Jakub Dawidek if (req != NULL) { 1129dddd1d53SPawel Jakub Dawidek gctl_error(req, "Cannot create kernel thread " 1130dddd1d53SPawel Jakub Dawidek "for %s (error=%d).", bpp->name, error); 1131c58794deSPawel Jakub Dawidek } else { 1132dddd1d53SPawel Jakub Dawidek G_ELI_DEBUG(1, "Cannot create kernel thread " 1133dddd1d53SPawel Jakub Dawidek "for %s (error=%d).", bpp->name, error); 1134c58794deSPawel Jakub Dawidek } 1135c58794deSPawel Jakub Dawidek goto failed; 1136c58794deSPawel Jakub Dawidek } 1137c58794deSPawel Jakub Dawidek LIST_INSERT_HEAD(&sc->sc_workers, wr, w_next); 1138c58794deSPawel Jakub Dawidek } 1139c58794deSPawel Jakub Dawidek 1140c58794deSPawel Jakub Dawidek /* 1141c58794deSPawel Jakub Dawidek * Create decrypted provider. 1142c58794deSPawel Jakub Dawidek */ 1143c58794deSPawel Jakub Dawidek pp = g_new_providerf(gp, "%s%s", bpp->name, G_ELI_SUFFIX); 11446f818c1fSAlan Somers pp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE; 11457d874f0fSAlan Somers if (CRYPTO_HAS_VMPAGE) { 11467d874f0fSAlan Somers /* 11477d874f0fSAlan Somers * On DMAP architectures we can use unmapped I/O. But don't 11487d874f0fSAlan Somers * use it with data integrity verification. That code hasn't 11497d874f0fSAlan Somers * been written yet. 11507d874f0fSAlan Somers */ 11517d874f0fSAlan Somers if ((sc->sc_flags & G_ELI_FLAG_AUTH) == 0) 11527d874f0fSAlan Somers pp->flags |= G_PF_ACCEPT_UNMAPPED; 11537d874f0fSAlan Somers } 1154c6a26d4cSPawel Jakub Dawidek pp->mediasize = sc->sc_mediasize; 1155c6a26d4cSPawel Jakub Dawidek pp->sectorsize = sc->sc_sectorsize; 1156ae1cce52SWarner Losh LIST_FOREACH(gap, &bpp->aliases, ga_next) 1157ae1cce52SWarner Losh g_provider_add_alias(pp, "%s%s", gap->ga_alias, G_ELI_SUFFIX); 1158eaa3b919SPawel Jakub Dawidek 1159c58794deSPawel Jakub Dawidek g_error_provider(pp, 0); 1160c58794deSPawel Jakub Dawidek 1161c58794deSPawel Jakub Dawidek G_ELI_DEBUG(0, "Device %s created.", pp->name); 1162eaa3b919SPawel Jakub Dawidek G_ELI_DEBUG(0, "Encryption: %s %u", g_eli_algo2str(sc->sc_ealgo), 1163eaa3b919SPawel Jakub Dawidek sc->sc_ekeylen); 1164e2b99193SJohn Baldwin if (sc->sc_flags & G_ELI_FLAG_AUTH) 1165eaa3b919SPawel Jakub Dawidek G_ELI_DEBUG(0, " Integrity: %s", g_eli_algo2str(sc->sc_aalgo)); 1166c58794deSPawel Jakub Dawidek G_ELI_DEBUG(0, " Crypto: %s", 1167a3d565a1SJohn Baldwin sc->sc_crypto == G_ELI_CRYPTO_SW_ACCEL ? "accelerated software" : 1168c58794deSPawel Jakub Dawidek sc->sc_crypto == G_ELI_CRYPTO_SW ? "software" : "hardware"); 1169c58794deSPawel Jakub Dawidek return (gp); 1170c58794deSPawel Jakub Dawidek failed: 1171c58794deSPawel Jakub Dawidek mtx_lock(&sc->sc_queue_mtx); 1172c58794deSPawel Jakub Dawidek sc->sc_flags |= G_ELI_FLAG_DESTROY; 1173c58794deSPawel Jakub Dawidek wakeup(sc); 1174c58794deSPawel Jakub Dawidek /* 1175c58794deSPawel Jakub Dawidek * Wait for kernel threads self destruction. 1176c58794deSPawel Jakub Dawidek */ 1177c58794deSPawel Jakub Dawidek while (!LIST_EMPTY(&sc->sc_workers)) { 1178c58794deSPawel Jakub Dawidek msleep(&sc->sc_workers, &sc->sc_queue_mtx, PRIBIO, 1179c58794deSPawel Jakub Dawidek "geli:destroy", 0); 1180c58794deSPawel Jakub Dawidek } 1181c58794deSPawel Jakub Dawidek mtx_destroy(&sc->sc_queue_mtx); 1182c58794deSPawel Jakub Dawidek if (cp->provider != NULL) { 1183c58794deSPawel Jakub Dawidek if (cp->acr == 1) 11843bb6e0f0SRyan Libby g_access(cp, -1, -dcw, -1); 1185c58794deSPawel Jakub Dawidek g_detach(cp); 1186c58794deSPawel Jakub Dawidek } 1187c58794deSPawel Jakub Dawidek g_destroy_consumer(cp); 1188c58794deSPawel Jakub Dawidek g_destroy_geom(gp); 11891e09ff3dSPawel Jakub Dawidek g_eli_key_destroy(sc); 1190*2dbc9a38SGleb Smirnoff g_eli_fini_uma(); 1191b172f23dSJohn Baldwin zfree(sc, M_ELI); 1192c58794deSPawel Jakub Dawidek return (NULL); 1193c58794deSPawel Jakub Dawidek } 1194c58794deSPawel Jakub Dawidek 1195c58794deSPawel Jakub Dawidek int 1196c58794deSPawel Jakub Dawidek g_eli_destroy(struct g_eli_softc *sc, boolean_t force) 1197c58794deSPawel Jakub Dawidek { 1198c58794deSPawel Jakub Dawidek struct g_geom *gp; 1199c58794deSPawel Jakub Dawidek struct g_provider *pp; 1200c58794deSPawel Jakub Dawidek 1201c58794deSPawel Jakub Dawidek g_topology_assert(); 1202c58794deSPawel Jakub Dawidek 1203c58794deSPawel Jakub Dawidek if (sc == NULL) 1204c58794deSPawel Jakub Dawidek return (ENXIO); 1205c58794deSPawel Jakub Dawidek 1206c58794deSPawel Jakub Dawidek gp = sc->sc_geom; 1207c58794deSPawel Jakub Dawidek pp = LIST_FIRST(&gp->provider); 1208c58794deSPawel Jakub Dawidek if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) { 1209c58794deSPawel Jakub Dawidek if (force) { 1210c58794deSPawel Jakub Dawidek G_ELI_DEBUG(1, "Device %s is still open, so it " 121198645006SChristian Brueffer "cannot be definitely removed.", pp->name); 121219351a14SAlexander Motin sc->sc_flags |= G_ELI_FLAG_RW_DETACH; 121319351a14SAlexander Motin gp->access = g_eli_access; 121419351a14SAlexander Motin g_wither_provider(pp, ENXIO); 121519351a14SAlexander Motin return (EBUSY); 1216c58794deSPawel Jakub Dawidek } else { 1217c58794deSPawel Jakub Dawidek G_ELI_DEBUG(1, 1218c58794deSPawel Jakub Dawidek "Device %s is still open (r%dw%de%d).", pp->name, 1219c58794deSPawel Jakub Dawidek pp->acr, pp->acw, pp->ace); 1220c58794deSPawel Jakub Dawidek return (EBUSY); 1221c58794deSPawel Jakub Dawidek } 1222c58794deSPawel Jakub Dawidek } 1223c58794deSPawel Jakub Dawidek 1224c58794deSPawel Jakub Dawidek mtx_lock(&sc->sc_queue_mtx); 1225c58794deSPawel Jakub Dawidek sc->sc_flags |= G_ELI_FLAG_DESTROY; 1226c58794deSPawel Jakub Dawidek wakeup(sc); 1227c58794deSPawel Jakub Dawidek while (!LIST_EMPTY(&sc->sc_workers)) { 1228c58794deSPawel Jakub Dawidek msleep(&sc->sc_workers, &sc->sc_queue_mtx, PRIBIO, 1229c58794deSPawel Jakub Dawidek "geli:destroy", 0); 1230c58794deSPawel Jakub Dawidek } 1231c58794deSPawel Jakub Dawidek mtx_destroy(&sc->sc_queue_mtx); 1232c58794deSPawel Jakub Dawidek gp->softc = NULL; 12331e09ff3dSPawel Jakub Dawidek g_eli_key_destroy(sc); 1234*2dbc9a38SGleb Smirnoff g_eli_fini_uma(); 1235b172f23dSJohn Baldwin zfree(sc, M_ELI); 1236c58794deSPawel Jakub Dawidek 1237c58794deSPawel Jakub Dawidek G_ELI_DEBUG(0, "Device %s destroyed.", gp->name); 1238c58794deSPawel Jakub Dawidek g_wither_geom_close(gp, ENXIO); 1239c58794deSPawel Jakub Dawidek 1240c58794deSPawel Jakub Dawidek return (0); 1241c58794deSPawel Jakub Dawidek } 1242c58794deSPawel Jakub Dawidek 1243c58794deSPawel Jakub Dawidek static int 1244c58794deSPawel Jakub Dawidek g_eli_destroy_geom(struct gctl_req *req __unused, 1245c58794deSPawel Jakub Dawidek struct g_class *mp __unused, struct g_geom *gp) 1246c58794deSPawel Jakub Dawidek { 1247c58794deSPawel Jakub Dawidek struct g_eli_softc *sc; 1248c58794deSPawel Jakub Dawidek 1249c58794deSPawel Jakub Dawidek sc = gp->softc; 12505ad4a7c7SPawel Jakub Dawidek return (g_eli_destroy(sc, FALSE)); 1251c58794deSPawel Jakub Dawidek } 1252c58794deSPawel Jakub Dawidek 12539af2131bSPawel Jakub Dawidek static int 12549af2131bSPawel Jakub Dawidek g_eli_keyfiles_load(struct hmac_ctx *ctx, const char *provider) 12559af2131bSPawel Jakub Dawidek { 12561e189c08SMarcel Moolenaar u_char *keyfile, *data; 12579af2131bSPawel Jakub Dawidek char *file, name[64]; 12581e189c08SMarcel Moolenaar size_t size; 12599af2131bSPawel Jakub Dawidek int i; 12609af2131bSPawel Jakub Dawidek 12619af2131bSPawel Jakub Dawidek for (i = 0; ; i++) { 12629af2131bSPawel Jakub Dawidek snprintf(name, sizeof(name), "%s:geli_keyfile%d", provider, i); 12639af2131bSPawel Jakub Dawidek keyfile = preload_search_by_type(name); 1264edaa9008SPawel Jakub Dawidek if (keyfile == NULL && i == 0) { 1265edaa9008SPawel Jakub Dawidek /* 1266edaa9008SPawel Jakub Dawidek * If there is only one keyfile, allow simpler name. 1267edaa9008SPawel Jakub Dawidek */ 1268edaa9008SPawel Jakub Dawidek snprintf(name, sizeof(name), "%s:geli_keyfile", provider); 1269edaa9008SPawel Jakub Dawidek keyfile = preload_search_by_type(name); 1270edaa9008SPawel Jakub Dawidek } 12719af2131bSPawel Jakub Dawidek if (keyfile == NULL) 12729af2131bSPawel Jakub Dawidek return (i); /* Return number of loaded keyfiles. */ 12731e189c08SMarcel Moolenaar data = preload_fetch_addr(keyfile); 12749af2131bSPawel Jakub Dawidek if (data == NULL) { 12759af2131bSPawel Jakub Dawidek G_ELI_DEBUG(0, "Cannot find key file data for %s.", 12769af2131bSPawel Jakub Dawidek name); 12779af2131bSPawel Jakub Dawidek return (0); 12789af2131bSPawel Jakub Dawidek } 12791e189c08SMarcel Moolenaar size = preload_fetch_size(keyfile); 12801e189c08SMarcel Moolenaar if (size == 0) { 12819af2131bSPawel Jakub Dawidek G_ELI_DEBUG(0, "Cannot find key file size for %s.", 12829af2131bSPawel Jakub Dawidek name); 12839af2131bSPawel Jakub Dawidek return (0); 12849af2131bSPawel Jakub Dawidek } 12859af2131bSPawel Jakub Dawidek file = preload_search_info(keyfile, MODINFO_NAME); 12869af2131bSPawel Jakub Dawidek if (file == NULL) { 12879af2131bSPawel Jakub Dawidek G_ELI_DEBUG(0, "Cannot find key file name for %s.", 12889af2131bSPawel Jakub Dawidek name); 12899af2131bSPawel Jakub Dawidek return (0); 12909af2131bSPawel Jakub Dawidek } 12919af2131bSPawel Jakub Dawidek G_ELI_DEBUG(1, "Loaded keyfile %s for %s (type: %s).", file, 12929af2131bSPawel Jakub Dawidek provider, name); 12931e189c08SMarcel Moolenaar g_eli_crypto_hmac_update(ctx, data, size); 12949af2131bSPawel Jakub Dawidek } 12959af2131bSPawel Jakub Dawidek } 12969af2131bSPawel Jakub Dawidek 12979af2131bSPawel Jakub Dawidek static void 12989af2131bSPawel Jakub Dawidek g_eli_keyfiles_clear(const char *provider) 12999af2131bSPawel Jakub Dawidek { 13001e189c08SMarcel Moolenaar u_char *keyfile, *data; 13019af2131bSPawel Jakub Dawidek char name[64]; 13021e189c08SMarcel Moolenaar size_t size; 13039af2131bSPawel Jakub Dawidek int i; 13049af2131bSPawel Jakub Dawidek 13059af2131bSPawel Jakub Dawidek for (i = 0; ; i++) { 13069af2131bSPawel Jakub Dawidek snprintf(name, sizeof(name), "%s:geli_keyfile%d", provider, i); 13079af2131bSPawel Jakub Dawidek keyfile = preload_search_by_type(name); 13089af2131bSPawel Jakub Dawidek if (keyfile == NULL) 13099af2131bSPawel Jakub Dawidek return; 13101e189c08SMarcel Moolenaar data = preload_fetch_addr(keyfile); 13111e189c08SMarcel Moolenaar size = preload_fetch_size(keyfile); 13121e189c08SMarcel Moolenaar if (data != NULL && size != 0) 13136572e5ffSJohn Baldwin explicit_bzero(data, size); 13149af2131bSPawel Jakub Dawidek } 13159af2131bSPawel Jakub Dawidek } 13169af2131bSPawel Jakub Dawidek 1317c58794deSPawel Jakub Dawidek /* 1318c58794deSPawel Jakub Dawidek * Tasting is only made on boot. 1319c58794deSPawel Jakub Dawidek * We detect providers which should be attached before root is mounted. 1320c58794deSPawel Jakub Dawidek */ 1321c58794deSPawel Jakub Dawidek static struct g_geom * 1322c58794deSPawel Jakub Dawidek g_eli_taste(struct g_class *mp, struct g_provider *pp, int flags __unused) 1323c58794deSPawel Jakub Dawidek { 1324c58794deSPawel Jakub Dawidek struct g_eli_metadata md; 1325c58794deSPawel Jakub Dawidek struct g_geom *gp; 1326c58794deSPawel Jakub Dawidek struct hmac_ctx ctx; 1327c58794deSPawel Jakub Dawidek char passphrase[256]; 1328c58794deSPawel Jakub Dawidek u_char key[G_ELI_USERKEYLEN], mkey[G_ELI_DATAIVKEYLEN]; 13293453dc72SMariusz Zaborski u_int i, nkey, nkeyfiles, tries, showpass; 1330c58794deSPawel Jakub Dawidek int error; 1331ec5c0e5bSAllan Jude struct keybuf *keybuf; 1332c58794deSPawel Jakub Dawidek 1333c58794deSPawel Jakub Dawidek g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name); 1334c58794deSPawel Jakub Dawidek g_topology_assert(); 1335c58794deSPawel Jakub Dawidek 1336df3aed4fSPawel Jakub Dawidek if (root_mounted() || g_eli_tries == 0) 1337c58794deSPawel Jakub Dawidek return (NULL); 1338c58794deSPawel Jakub Dawidek 1339c58794deSPawel Jakub Dawidek G_ELI_DEBUG(3, "Tasting %s.", pp->name); 1340c58794deSPawel Jakub Dawidek 1341c58794deSPawel Jakub Dawidek error = g_eli_read_metadata(mp, pp, &md); 1342c58794deSPawel Jakub Dawidek if (error != 0) 1343c58794deSPawel Jakub Dawidek return (NULL); 1344c58794deSPawel Jakub Dawidek gp = NULL; 1345c58794deSPawel Jakub Dawidek 1346c58794deSPawel Jakub Dawidek if (strcmp(md.md_magic, G_ELI_MAGIC) != 0) 1347c58794deSPawel Jakub Dawidek return (NULL); 1348c58794deSPawel Jakub Dawidek if (md.md_version > G_ELI_VERSION) { 1349c58794deSPawel Jakub Dawidek printf("geom_eli.ko module is too old to handle %s.\n", 1350c58794deSPawel Jakub Dawidek pp->name); 1351c58794deSPawel Jakub Dawidek return (NULL); 1352c58794deSPawel Jakub Dawidek } 1353c58794deSPawel Jakub Dawidek if (md.md_provsize != pp->mediasize) 1354c58794deSPawel Jakub Dawidek return (NULL); 1355c58794deSPawel Jakub Dawidek /* Should we attach it on boot? */ 1356c81929d3SKyle Evans if (!(md.md_flags & G_ELI_FLAG_BOOT) && 1357c81929d3SKyle Evans !(md.md_flags & G_ELI_FLAG_GELIBOOT)) 1358c58794deSPawel Jakub Dawidek return (NULL); 1359c58794deSPawel Jakub Dawidek if (md.md_keys == 0x00) { 1360c58794deSPawel Jakub Dawidek G_ELI_DEBUG(0, "No valid keys on %s.", pp->name); 1361c58794deSPawel Jakub Dawidek return (NULL); 1362c58794deSPawel Jakub Dawidek } 1363e2b99193SJohn Baldwin if (!eli_metadata_crypto_supported(&md)) { 1364e2b99193SJohn Baldwin G_ELI_DEBUG(0, "%s uses invalid or unsupported algorithms\n", 1365e2b99193SJohn Baldwin pp->name); 1366e2b99193SJohn Baldwin return (NULL); 1367e2b99193SJohn Baldwin } 13689af2131bSPawel Jakub Dawidek if (md.md_iterations == -1) { 13699af2131bSPawel Jakub Dawidek /* If there is no passphrase, we try only once. */ 13709af2131bSPawel Jakub Dawidek tries = 1; 13719af2131bSPawel Jakub Dawidek } else { 13729af2131bSPawel Jakub Dawidek /* Ask for the passphrase no more than g_eli_tries times. */ 13739af2131bSPawel Jakub Dawidek tries = g_eli_tries; 13749af2131bSPawel Jakub Dawidek } 13759af2131bSPawel Jakub Dawidek 1376ec5c0e5bSAllan Jude if ((keybuf = get_keybuf()) != NULL) { 1377ec5c0e5bSAllan Jude /* Scan the key buffer, try all GELI keys. */ 1378ec5c0e5bSAllan Jude for (i = 0; i < keybuf->kb_nents; i++) { 1379ec5c0e5bSAllan Jude if (keybuf->kb_ents[i].ke_type == KEYBUF_TYPE_GELI) { 1380ec5c0e5bSAllan Jude memcpy(key, keybuf->kb_ents[i].ke_data, 1381ec5c0e5bSAllan Jude sizeof(key)); 1382ec5c0e5bSAllan Jude 138331f7586dSMariusz Zaborski if (g_eli_mkey_decrypt_any(&md, key, 1384ec5c0e5bSAllan Jude mkey, &nkey) == 0 ) { 1385ec5c0e5bSAllan Jude explicit_bzero(key, sizeof(key)); 1386ec5c0e5bSAllan Jude goto have_key; 1387ec5c0e5bSAllan Jude } 1388ec5c0e5bSAllan Jude } 1389ec5c0e5bSAllan Jude } 1390ec5c0e5bSAllan Jude } 1391ec5c0e5bSAllan Jude 1392835c4dd4SColin Percival for (i = 0; i <= tries; i++) { 13939af2131bSPawel Jakub Dawidek g_eli_crypto_hmac_init(&ctx, NULL, 0); 1394c58794deSPawel Jakub Dawidek 1395c58794deSPawel Jakub Dawidek /* 13969af2131bSPawel Jakub Dawidek * Load all key files. 1397c58794deSPawel Jakub Dawidek */ 13989af2131bSPawel Jakub Dawidek nkeyfiles = g_eli_keyfiles_load(&ctx, pp->name); 13999af2131bSPawel Jakub Dawidek 14009af2131bSPawel Jakub Dawidek if (nkeyfiles == 0 && md.md_iterations == -1) { 14019af2131bSPawel Jakub Dawidek /* 14029af2131bSPawel Jakub Dawidek * No key files and no passphrase, something is 14039af2131bSPawel Jakub Dawidek * definitely wrong here. 14049af2131bSPawel Jakub Dawidek * geli(8) doesn't allow for such situation, so assume 14059af2131bSPawel Jakub Dawidek * that there was really no passphrase and in that case 14069af2131bSPawel Jakub Dawidek * key files are no properly defined in loader.conf. 14079af2131bSPawel Jakub Dawidek */ 14089af2131bSPawel Jakub Dawidek G_ELI_DEBUG(0, 14099af2131bSPawel Jakub Dawidek "Found no key files in loader.conf for %s.", 14109af2131bSPawel Jakub Dawidek pp->name); 14119af2131bSPawel Jakub Dawidek return (NULL); 14129af2131bSPawel Jakub Dawidek } 14139af2131bSPawel Jakub Dawidek 14149af2131bSPawel Jakub Dawidek /* Ask for the passphrase if defined. */ 14159af2131bSPawel Jakub Dawidek if (md.md_iterations >= 0) { 1416835c4dd4SColin Percival /* Try first with cached passphrase. */ 1417835c4dd4SColin Percival if (i == 0) { 1418835c4dd4SColin Percival if (!g_eli_boot_passcache) 1419835c4dd4SColin Percival continue; 1420835c4dd4SColin Percival memcpy(passphrase, cached_passphrase, 1421835c4dd4SColin Percival sizeof(passphrase)); 1422835c4dd4SColin Percival } else { 1423c58794deSPawel Jakub Dawidek printf("Enter passphrase for %s: ", pp->name); 14243453dc72SMariusz Zaborski showpass = g_eli_visible_passphrase; 14253453dc72SMariusz Zaborski if ((md.md_flags & G_ELI_FLAG_GELIDISPLAYPASS) != 0) 14263453dc72SMariusz Zaborski showpass = GETS_ECHOPASS; 1427f6ce353eSAndriy Gapon cngets(passphrase, sizeof(passphrase), 14283453dc72SMariusz Zaborski showpass); 1429835c4dd4SColin Percival memcpy(cached_passphrase, passphrase, 1430835c4dd4SColin Percival sizeof(passphrase)); 1431835c4dd4SColin Percival } 14329af2131bSPawel Jakub Dawidek } 14339af2131bSPawel Jakub Dawidek 1434c58794deSPawel Jakub Dawidek /* 1435c58794deSPawel Jakub Dawidek * Prepare Derived-Key from the user passphrase. 1436c58794deSPawel Jakub Dawidek */ 1437c58794deSPawel Jakub Dawidek if (md.md_iterations == 0) { 1438c58794deSPawel Jakub Dawidek g_eli_crypto_hmac_update(&ctx, md.md_salt, 1439c58794deSPawel Jakub Dawidek sizeof(md.md_salt)); 1440c58794deSPawel Jakub Dawidek g_eli_crypto_hmac_update(&ctx, passphrase, 1441c58794deSPawel Jakub Dawidek strlen(passphrase)); 1442ec5c0e5bSAllan Jude explicit_bzero(passphrase, sizeof(passphrase)); 14439af2131bSPawel Jakub Dawidek } else if (md.md_iterations > 0) { 1444c58794deSPawel Jakub Dawidek u_char dkey[G_ELI_USERKEYLEN]; 1445c58794deSPawel Jakub Dawidek 1446c58794deSPawel Jakub Dawidek pkcs5v2_genkey(dkey, sizeof(dkey), md.md_salt, 1447c58794deSPawel Jakub Dawidek sizeof(md.md_salt), passphrase, md.md_iterations); 14486572e5ffSJohn Baldwin explicit_bzero(passphrase, sizeof(passphrase)); 1449c58794deSPawel Jakub Dawidek g_eli_crypto_hmac_update(&ctx, dkey, sizeof(dkey)); 1450ec5c0e5bSAllan Jude explicit_bzero(dkey, sizeof(dkey)); 1451c58794deSPawel Jakub Dawidek } 14529af2131bSPawel Jakub Dawidek 1453c58794deSPawel Jakub Dawidek g_eli_crypto_hmac_final(&ctx, key, 0); 1454c58794deSPawel Jakub Dawidek 1455c58794deSPawel Jakub Dawidek /* 1456c58794deSPawel Jakub Dawidek * Decrypt Master-Key. 1457c58794deSPawel Jakub Dawidek */ 145831f7586dSMariusz Zaborski error = g_eli_mkey_decrypt_any(&md, key, mkey, &nkey); 14596572e5ffSJohn Baldwin explicit_bzero(key, sizeof(key)); 1460c58794deSPawel Jakub Dawidek if (error == -1) { 1461835c4dd4SColin Percival if (i == tries) { 14629af2131bSPawel Jakub Dawidek G_ELI_DEBUG(0, 14639af2131bSPawel Jakub Dawidek "Wrong key for %s. No tries left.", 14649af2131bSPawel Jakub Dawidek pp->name); 14659af2131bSPawel Jakub Dawidek g_eli_keyfiles_clear(pp->name); 14669af2131bSPawel Jakub Dawidek return (NULL); 1467c58794deSPawel Jakub Dawidek } 1468835c4dd4SColin Percival if (i > 0) { 1469835c4dd4SColin Percival G_ELI_DEBUG(0, 1470835c4dd4SColin Percival "Wrong key for %s. Tries left: %u.", 1471835c4dd4SColin Percival pp->name, tries - i); 1472835c4dd4SColin Percival } 1473c58794deSPawel Jakub Dawidek /* Try again. */ 1474c58794deSPawel Jakub Dawidek continue; 1475c58794deSPawel Jakub Dawidek } else if (error > 0) { 1476038c55adSPawel Jakub Dawidek G_ELI_DEBUG(0, 1477038c55adSPawel Jakub Dawidek "Cannot decrypt Master Key for %s (error=%d).", 1478c58794deSPawel Jakub Dawidek pp->name, error); 14799af2131bSPawel Jakub Dawidek g_eli_keyfiles_clear(pp->name); 1480c58794deSPawel Jakub Dawidek return (NULL); 1481c58794deSPawel Jakub Dawidek } 1482ebd05adaSBrad Davis g_eli_keyfiles_clear(pp->name); 1483c58794deSPawel Jakub Dawidek G_ELI_DEBUG(1, "Using Master Key %u for %s.", nkey, pp->name); 1484c58794deSPawel Jakub Dawidek break; 1485c58794deSPawel Jakub Dawidek } 1486ec5c0e5bSAllan Jude have_key: 1487c58794deSPawel Jakub Dawidek 1488c58794deSPawel Jakub Dawidek /* 1489c58794deSPawel Jakub Dawidek * We have correct key, let's attach provider. 1490c58794deSPawel Jakub Dawidek */ 1491c58794deSPawel Jakub Dawidek gp = g_eli_create(NULL, mp, pp, &md, mkey, nkey); 14926572e5ffSJohn Baldwin explicit_bzero(mkey, sizeof(mkey)); 14936572e5ffSJohn Baldwin explicit_bzero(&md, sizeof(md)); 1494c58794deSPawel Jakub Dawidek if (gp == NULL) { 1495c58794deSPawel Jakub Dawidek G_ELI_DEBUG(0, "Cannot create device %s%s.", pp->name, 1496c58794deSPawel Jakub Dawidek G_ELI_SUFFIX); 1497c58794deSPawel Jakub Dawidek return (NULL); 1498c58794deSPawel Jakub Dawidek } 1499c58794deSPawel Jakub Dawidek return (gp); 1500c58794deSPawel Jakub Dawidek } 1501c58794deSPawel Jakub Dawidek 1502c58794deSPawel Jakub Dawidek static void 1503c58794deSPawel Jakub Dawidek g_eli_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, 1504c58794deSPawel Jakub Dawidek struct g_consumer *cp, struct g_provider *pp) 1505c58794deSPawel Jakub Dawidek { 1506c58794deSPawel Jakub Dawidek struct g_eli_softc *sc; 1507c58794deSPawel Jakub Dawidek 1508c58794deSPawel Jakub Dawidek g_topology_assert(); 1509c58794deSPawel Jakub Dawidek sc = gp->softc; 1510c58794deSPawel Jakub Dawidek if (sc == NULL) 1511c58794deSPawel Jakub Dawidek return; 1512c58794deSPawel Jakub Dawidek if (pp != NULL || cp != NULL) 1513c58794deSPawel Jakub Dawidek return; /* Nothing here. */ 15141e09ff3dSPawel Jakub Dawidek 1515743437c4SAndrey V. Elsukov sbuf_printf(sb, "%s<KeysTotal>%ju</KeysTotal>\n", indent, 15161e09ff3dSPawel Jakub Dawidek (uintmax_t)sc->sc_ekeys_total); 1517743437c4SAndrey V. Elsukov sbuf_printf(sb, "%s<KeysAllocated>%ju</KeysAllocated>\n", indent, 15181e09ff3dSPawel Jakub Dawidek (uintmax_t)sc->sc_ekeys_allocated); 1519ea35a2ecSPawel Jakub Dawidek sbuf_printf(sb, "%s<Flags>", indent); 1520ea35a2ecSPawel Jakub Dawidek if (sc->sc_flags == 0) 152149ee0fceSAlexander Motin sbuf_cat(sb, "NONE"); 1522ea35a2ecSPawel Jakub Dawidek else { 1523ea35a2ecSPawel Jakub Dawidek int first = 1; 1524ea35a2ecSPawel Jakub Dawidek 1525ea35a2ecSPawel Jakub Dawidek #define ADD_FLAG(flag, name) do { \ 1526eaa3b919SPawel Jakub Dawidek if (sc->sc_flags & (flag)) { \ 1527ea35a2ecSPawel Jakub Dawidek if (!first) \ 152849ee0fceSAlexander Motin sbuf_cat(sb, ", "); \ 1529ea35a2ecSPawel Jakub Dawidek else \ 1530ea35a2ecSPawel Jakub Dawidek first = 0; \ 153149ee0fceSAlexander Motin sbuf_cat(sb, name); \ 1532ea35a2ecSPawel Jakub Dawidek } \ 1533ea35a2ecSPawel Jakub Dawidek } while (0) 15345ad4a7c7SPawel Jakub Dawidek ADD_FLAG(G_ELI_FLAG_SUSPEND, "SUSPEND"); 1535c6a26d4cSPawel Jakub Dawidek ADD_FLAG(G_ELI_FLAG_SINGLE_KEY, "SINGLE-KEY"); 15362bd4ade6SPawel Jakub Dawidek ADD_FLAG(G_ELI_FLAG_NATIVE_BYTE_ORDER, "NATIVE-BYTE-ORDER"); 1537ea35a2ecSPawel Jakub Dawidek ADD_FLAG(G_ELI_FLAG_ONETIME, "ONETIME"); 1538ea35a2ecSPawel Jakub Dawidek ADD_FLAG(G_ELI_FLAG_BOOT, "BOOT"); 1539ea35a2ecSPawel Jakub Dawidek ADD_FLAG(G_ELI_FLAG_WO_DETACH, "W-DETACH"); 1540ea35a2ecSPawel Jakub Dawidek ADD_FLAG(G_ELI_FLAG_RW_DETACH, "RW-DETACH"); 1541eaa3b919SPawel Jakub Dawidek ADD_FLAG(G_ELI_FLAG_AUTH, "AUTH"); 1542ea35a2ecSPawel Jakub Dawidek ADD_FLAG(G_ELI_FLAG_WOPEN, "W-OPEN"); 1543ea35a2ecSPawel Jakub Dawidek ADD_FLAG(G_ELI_FLAG_DESTROY, "DESTROY"); 154485059016SPawel Jakub Dawidek ADD_FLAG(G_ELI_FLAG_RO, "READ-ONLY"); 154546e34470SPawel Jakub Dawidek ADD_FLAG(G_ELI_FLAG_NODELETE, "NODELETE"); 1546d8736625SAllan Jude ADD_FLAG(G_ELI_FLAG_GELIBOOT, "GELIBOOT"); 15473453dc72SMariusz Zaborski ADD_FLAG(G_ELI_FLAG_GELIDISPLAYPASS, "GELIDISPLAYPASS"); 15482f07cdf8SPawel Jakub Dawidek ADD_FLAG(G_ELI_FLAG_AUTORESIZE, "AUTORESIZE"); 1549ea35a2ecSPawel Jakub Dawidek #undef ADD_FLAG 1550ea35a2ecSPawel Jakub Dawidek } 155149ee0fceSAlexander Motin sbuf_cat(sb, "</Flags>\n"); 1552ea35a2ecSPawel Jakub Dawidek 1553eaa3b919SPawel Jakub Dawidek if (!(sc->sc_flags & G_ELI_FLAG_ONETIME)) { 1554ea35a2ecSPawel Jakub Dawidek sbuf_printf(sb, "%s<UsedKey>%u</UsedKey>\n", indent, 1555ea35a2ecSPawel Jakub Dawidek sc->sc_nkey); 1556ea35a2ecSPawel Jakub Dawidek } 15571f8c92e6SPawel Jakub Dawidek sbuf_printf(sb, "%s<Version>%u</Version>\n", indent, sc->sc_version); 1558c58794deSPawel Jakub Dawidek sbuf_printf(sb, "%s<Crypto>", indent); 1559c58794deSPawel Jakub Dawidek switch (sc->sc_crypto) { 1560c58794deSPawel Jakub Dawidek case G_ELI_CRYPTO_HW: 156149ee0fceSAlexander Motin sbuf_cat(sb, "hardware"); 1562c58794deSPawel Jakub Dawidek break; 1563c58794deSPawel Jakub Dawidek case G_ELI_CRYPTO_SW: 156449ee0fceSAlexander Motin sbuf_cat(sb, "software"); 1565c58794deSPawel Jakub Dawidek break; 1566a3d565a1SJohn Baldwin case G_ELI_CRYPTO_SW_ACCEL: 1567a3d565a1SJohn Baldwin sbuf_cat(sb, "accelerated software"); 1568a3d565a1SJohn Baldwin break; 1569c58794deSPawel Jakub Dawidek default: 157049ee0fceSAlexander Motin sbuf_cat(sb, "UNKNOWN"); 1571c58794deSPawel Jakub Dawidek break; 1572c58794deSPawel Jakub Dawidek } 157349ee0fceSAlexander Motin sbuf_cat(sb, "</Crypto>\n"); 1574eaa3b919SPawel Jakub Dawidek if (sc->sc_flags & G_ELI_FLAG_AUTH) { 1575eaa3b919SPawel Jakub Dawidek sbuf_printf(sb, 1576eaa3b919SPawel Jakub Dawidek "%s<AuthenticationAlgorithm>%s</AuthenticationAlgorithm>\n", 1577eaa3b919SPawel Jakub Dawidek indent, g_eli_algo2str(sc->sc_aalgo)); 1578eaa3b919SPawel Jakub Dawidek } 1579eaa3b919SPawel Jakub Dawidek sbuf_printf(sb, "%s<KeyLength>%u</KeyLength>\n", indent, 1580eaa3b919SPawel Jakub Dawidek sc->sc_ekeylen); 1581038c55adSPawel Jakub Dawidek sbuf_printf(sb, "%s<EncryptionAlgorithm>%s</EncryptionAlgorithm>\n", 1582038c55adSPawel Jakub Dawidek indent, g_eli_algo2str(sc->sc_ealgo)); 1583d8d61ef8SPawel Jakub Dawidek sbuf_printf(sb, "%s<State>%s</State>\n", indent, 1584d8d61ef8SPawel Jakub Dawidek (sc->sc_flags & G_ELI_FLAG_SUSPEND) ? "SUSPENDED" : "ACTIVE"); 1585c58794deSPawel Jakub Dawidek } 1586c58794deSPawel Jakub Dawidek 1587c5d387d0SPawel Jakub Dawidek static void 1588c5d387d0SPawel Jakub Dawidek g_eli_shutdown_pre_sync(void *arg, int howto) 1589c5d387d0SPawel Jakub Dawidek { 1590c5d387d0SPawel Jakub Dawidek struct g_class *mp; 1591c5d387d0SPawel Jakub Dawidek struct g_geom *gp, *gp2; 1592c5d387d0SPawel Jakub Dawidek struct g_provider *pp; 1593c5d387d0SPawel Jakub Dawidek struct g_eli_softc *sc; 1594c5d387d0SPawel Jakub Dawidek int error; 1595c5d387d0SPawel Jakub Dawidek 1596c5d387d0SPawel Jakub Dawidek mp = arg; 1597c5d387d0SPawel Jakub Dawidek g_topology_lock(); 1598c5d387d0SPawel Jakub Dawidek LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) { 1599c5d387d0SPawel Jakub Dawidek sc = gp->softc; 1600c5d387d0SPawel Jakub Dawidek if (sc == NULL) 1601c5d387d0SPawel Jakub Dawidek continue; 1602c5d387d0SPawel Jakub Dawidek pp = LIST_FIRST(&gp->provider); 1603c5d387d0SPawel Jakub Dawidek KASSERT(pp != NULL, ("No provider? gp=%p (%s)", gp, gp->name)); 16042a230609SAlan Somers if (pp->acr != 0 || pp->acw != 0 || pp->ace != 0 || 16052a230609SAlan Somers SCHEDULER_STOPPED()) 16062a230609SAlan Somers { 1607c5d387d0SPawel Jakub Dawidek sc->sc_flags |= G_ELI_FLAG_RW_DETACH; 1608c5d387d0SPawel Jakub Dawidek gp->access = g_eli_access; 16092a230609SAlan Somers } else { 16102a230609SAlan Somers error = g_eli_destroy(sc, TRUE); 1611c5d387d0SPawel Jakub Dawidek } 1612c5d387d0SPawel Jakub Dawidek } 1613c5d387d0SPawel Jakub Dawidek g_topology_unlock(); 1614c5d387d0SPawel Jakub Dawidek } 1615c5d387d0SPawel Jakub Dawidek 1616c5d387d0SPawel Jakub Dawidek static void 1617c5d387d0SPawel Jakub Dawidek g_eli_init(struct g_class *mp) 1618c5d387d0SPawel Jakub Dawidek { 1619c5d387d0SPawel Jakub Dawidek 1620c5d387d0SPawel Jakub Dawidek g_eli_pre_sync = EVENTHANDLER_REGISTER(shutdown_pre_sync, 1621c5d387d0SPawel Jakub Dawidek g_eli_shutdown_pre_sync, mp, SHUTDOWN_PRI_FIRST); 1622c5d387d0SPawel Jakub Dawidek if (g_eli_pre_sync == NULL) 1623c5d387d0SPawel Jakub Dawidek G_ELI_DEBUG(0, "Warning! Cannot register shutdown event."); 1624c5d387d0SPawel Jakub Dawidek } 1625c5d387d0SPawel Jakub Dawidek 1626c5d387d0SPawel Jakub Dawidek static void 1627c5d387d0SPawel Jakub Dawidek g_eli_fini(struct g_class *mp) 1628c5d387d0SPawel Jakub Dawidek { 1629c5d387d0SPawel Jakub Dawidek 1630c5d387d0SPawel Jakub Dawidek if (g_eli_pre_sync != NULL) 1631c5d387d0SPawel Jakub Dawidek EVENTHANDLER_DEREGISTER(shutdown_pre_sync, g_eli_pre_sync); 1632c5d387d0SPawel Jakub Dawidek } 1633c5d387d0SPawel Jakub Dawidek 1634c58794deSPawel Jakub Dawidek DECLARE_GEOM_CLASS(g_eli_class, g_eli); 1635f6829a05SYaroslav Tykhiy MODULE_DEPEND(g_eli, crypto, 1, 1, 1); 163674d6c131SKyle Evans MODULE_VERSION(geom_eli, 0); 1637