1c58794deSPawel Jakub Dawidek /*- 24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 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/param.h> 30c58794deSPawel Jakub Dawidek #include <sys/systm.h> 31f6ce353eSAndriy Gapon #include <sys/cons.h> 32bc683a89SWarner Losh #include <sys/kenv.h> 33c58794deSPawel Jakub Dawidek #include <sys/kernel.h> 349af2131bSPawel Jakub Dawidek #include <sys/linker.h> 35c58794deSPawel Jakub Dawidek #include <sys/module.h> 36c58794deSPawel Jakub Dawidek #include <sys/lock.h> 37c58794deSPawel Jakub Dawidek #include <sys/mutex.h> 38c58794deSPawel Jakub Dawidek #include <sys/bio.h> 395d807a0eSAndrey V. Elsukov #include <sys/sbuf.h> 40c58794deSPawel Jakub Dawidek #include <sys/sysctl.h> 41c58794deSPawel Jakub Dawidek #include <sys/malloc.h> 42c5d387d0SPawel Jakub Dawidek #include <sys/eventhandler.h> 43c58794deSPawel Jakub Dawidek #include <sys/kthread.h> 44c58794deSPawel Jakub Dawidek #include <sys/proc.h> 45c58794deSPawel Jakub Dawidek #include <sys/sched.h> 46dd549194SPawel Jakub Dawidek #include <sys/smp.h> 47c58794deSPawel Jakub Dawidek #include <sys/uio.h> 48a80f82a4SPawel Jakub Dawidek #include <sys/vnode.h> 49c58794deSPawel Jakub Dawidek 507d874f0fSAlan Somers #include <machine/vmparam.h> 517d874f0fSAlan Somers 52c58794deSPawel Jakub Dawidek #include <vm/uma.h> 532dbc9a38SGleb Smirnoff #include <vm/vm.h> 542dbc9a38SGleb Smirnoff #include <vm/swap_pager.h> 55c58794deSPawel Jakub Dawidek 56c58794deSPawel Jakub Dawidek #include <geom/geom.h> 57ac03832eSConrad Meyer #include <geom/geom_dbg.h> 58c58794deSPawel Jakub Dawidek #include <geom/eli/g_eli.h> 59c58794deSPawel Jakub Dawidek #include <geom/eli/pkcs5v2.h> 60c58794deSPawel Jakub Dawidek 61ec5c0e5bSAllan Jude #include <crypto/intake.h> 62ec5c0e5bSAllan Jude 63cb08c2ccSAlexander Leidinger FEATURE(geom_eli, "GEOM crypto module"); 64c58794deSPawel Jakub Dawidek 65627d5d19SMateusz Guzik MALLOC_DEFINE(M_ELI, "eli_data", "GEOM_ELI Data"); 66c58794deSPawel Jakub Dawidek 67c58794deSPawel Jakub Dawidek SYSCTL_DECL(_kern_geom); 6853a6215cSPawel Biernacki SYSCTL_NODE(_kern_geom, OID_AUTO, eli, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 6953a6215cSPawel Biernacki "GEOM_ELI stuff"); 70a1f4a8c4SPawel Jakub Dawidek static int g_eli_version = G_ELI_VERSION; 71a1f4a8c4SPawel Jakub Dawidek SYSCTL_INT(_kern_geom_eli, OID_AUTO, version, CTLFLAG_RD, &g_eli_version, 0, 72a1f4a8c4SPawel Jakub Dawidek "GELI version"); 73f95168e0SPawel Jakub Dawidek int g_eli_debug = 0; 74af3b2549SHans Petter Selasky SYSCTL_INT(_kern_geom_eli, OID_AUTO, debug, CTLFLAG_RWTUN, &g_eli_debug, 0, 75c58794deSPawel Jakub Dawidek "Debug level"); 76c58794deSPawel Jakub Dawidek static u_int g_eli_tries = 3; 77af3b2549SHans Petter Selasky SYSCTL_UINT(_kern_geom_eli, OID_AUTO, tries, CTLFLAG_RWTUN, &g_eli_tries, 0, 7898645006SChristian Brueffer "Number of tries for entering the passphrase"); 79eb4c31fdSEd Schouten static u_int g_eli_visible_passphrase = GETS_NOECHO; 80af3b2549SHans Petter Selasky SYSCTL_UINT(_kern_geom_eli, OID_AUTO, visible_passphrase, CTLFLAG_RWTUN, 81c58794deSPawel Jakub Dawidek &g_eli_visible_passphrase, 0, 82eb4c31fdSEd Schouten "Visibility of passphrase prompt (0 = invisible, 1 = visible, 2 = asterisk)"); 83b35bfe7eSPawel Jakub Dawidek u_int g_eli_overwrites = G_ELI_OVERWRITES; 84af3b2549SHans Petter Selasky SYSCTL_UINT(_kern_geom_eli, OID_AUTO, overwrites, CTLFLAG_RWTUN, &g_eli_overwrites, 8598645006SChristian Brueffer 0, "Number of times on-disk keys should be overwritten when destroying them"); 86dd549194SPawel Jakub Dawidek static u_int g_eli_threads = 0; 87af3b2549SHans Petter Selasky SYSCTL_UINT(_kern_geom_eli, OID_AUTO, threads, CTLFLAG_RWTUN, &g_eli_threads, 0, 88c58794deSPawel Jakub Dawidek "Number of threads doing crypto work"); 89eaa3b919SPawel Jakub Dawidek u_int g_eli_batch = 0; 90af3b2549SHans Petter Selasky SYSCTL_UINT(_kern_geom_eli, OID_AUTO, batch, CTLFLAG_RWTUN, &g_eli_batch, 0, 91eaa3b919SPawel Jakub Dawidek "Use crypto operations batching"); 922dbc9a38SGleb Smirnoff static u_int g_eli_minbufs = 16; 932dbc9a38SGleb Smirnoff static int sysctl_g_eli_minbufs(SYSCTL_HANDLER_ARGS); 942dbc9a38SGleb Smirnoff SYSCTL_PROC(_kern_geom_eli, OID_AUTO, minbufs, CTLTYPE_UINT | CTLFLAG_RW | 952dbc9a38SGleb Smirnoff CTLFLAG_MPSAFE, NULL, 0, sysctl_g_eli_minbufs, "IU", 962dbc9a38SGleb Smirnoff "Number of GELI bufs reserved for swap transactions"); 97081b4452SMark Johnston static bool g_eli_blocking_malloc = false; 98081b4452SMark Johnston SYSCTL_BOOL(_kern_geom_eli, OID_AUTO, blocking_malloc, CTLFLAG_RWTUN, 99081b4452SMark Johnston &g_eli_blocking_malloc, 0, "Use blocking malloc calls for GELI buffers"); 100081b4452SMark Johnston static bool g_eli_unmapped_io = true; 101081b4452SMark Johnston SYSCTL_BOOL(_kern_geom_eli, OID_AUTO, unmapped_io, CTLFLAG_RDTUN, 102081b4452SMark Johnston &g_eli_unmapped_io, 0, "Enable support for unmapped I/O"); 103*3acf3feaSAlan Somers static int g_eli_alloc_sz; 104*3acf3feaSAlan Somers SYSCTL_UINT(_kern_geom_eli, OID_AUTO, use_uma_bytes, CTLFLAG_RD, 105*3acf3feaSAlan Somers &g_eli_alloc_sz, 0, "Use uma(9) for allocations of this size or smaller."); 106081b4452SMark Johnston 1072dbc9a38SGleb Smirnoff static struct sx g_eli_umalock; /* Controls changes to UMA zone. */ 1082dbc9a38SGleb Smirnoff SX_SYSINIT(g_eli_umalock, &g_eli_umalock, "GELI UMA"); 1092dbc9a38SGleb Smirnoff static uma_zone_t g_eli_uma = NULL; 1102dbc9a38SGleb Smirnoff static volatile int g_eli_umaoutstanding; 1112dbc9a38SGleb Smirnoff static volatile int g_eli_devs; 1122dbc9a38SGleb Smirnoff 1132dbc9a38SGleb Smirnoff /* 1142dbc9a38SGleb Smirnoff * Control the number of reserved entries in the GELI zone. 1152dbc9a38SGleb Smirnoff * If the GELI zone has already been allocated, update the zone. Otherwise, 1162dbc9a38SGleb Smirnoff * simply update the variable for use the next time the zone is created. 1172dbc9a38SGleb Smirnoff */ 1182dbc9a38SGleb Smirnoff static int 1192dbc9a38SGleb Smirnoff sysctl_g_eli_minbufs(SYSCTL_HANDLER_ARGS) 1202dbc9a38SGleb Smirnoff { 1212dbc9a38SGleb Smirnoff int error; 1222dbc9a38SGleb Smirnoff u_int new; 1232dbc9a38SGleb Smirnoff 1242dbc9a38SGleb Smirnoff new = g_eli_minbufs; 1252dbc9a38SGleb Smirnoff error = sysctl_handle_int(oidp, &new, 0, req); 1262dbc9a38SGleb Smirnoff if (error != 0 || req->newptr == NULL) 1272dbc9a38SGleb Smirnoff return (error); 1282dbc9a38SGleb Smirnoff sx_xlock(&g_eli_umalock); 1292dbc9a38SGleb Smirnoff if (g_eli_uma != NULL) { 1302dbc9a38SGleb Smirnoff if (new != g_eli_minbufs) 1312dbc9a38SGleb Smirnoff uma_zone_reserve(g_eli_uma, new); 1322dbc9a38SGleb Smirnoff if (new > g_eli_minbufs) 1332dbc9a38SGleb Smirnoff uma_prealloc(g_eli_uma, new - g_eli_minbufs); 1342dbc9a38SGleb Smirnoff } 1352dbc9a38SGleb Smirnoff if (new != g_eli_minbufs) 1362dbc9a38SGleb Smirnoff g_eli_minbufs = new; 1372dbc9a38SGleb Smirnoff sx_xunlock(&g_eli_umalock); 1382dbc9a38SGleb Smirnoff return (0); 1392dbc9a38SGleb Smirnoff } 140c58794deSPawel Jakub Dawidek 141835c4dd4SColin Percival /* 142835c4dd4SColin Percival * Passphrase cached during boot, in order to be more user-friendly if 143835c4dd4SColin Percival * there are multiple providers using the same passphrase. 144835c4dd4SColin Percival */ 145835c4dd4SColin Percival static char cached_passphrase[256]; 146835c4dd4SColin Percival static u_int g_eli_boot_passcache = 1; 147835c4dd4SColin Percival TUNABLE_INT("kern.geom.eli.boot_passcache", &g_eli_boot_passcache); 148835c4dd4SColin Percival SYSCTL_UINT(_kern_geom_eli, OID_AUTO, boot_passcache, CTLFLAG_RD, 149835c4dd4SColin Percival &g_eli_boot_passcache, 0, 150835c4dd4SColin Percival "Passphrases are cached during boot process for possible reuse"); 151835c4dd4SColin Percival static void 15266427784SColin Percival fetch_loader_passphrase(void * dummy) 15366427784SColin Percival { 15466427784SColin Percival char * env_passphrase; 15566427784SColin Percival 15666427784SColin Percival KASSERT(dynamic_kenv, ("need dynamic kenv")); 15766427784SColin Percival 15866427784SColin Percival if ((env_passphrase = kern_getenv("kern.geom.eli.passphrase")) != NULL) { 15966427784SColin Percival /* Extract passphrase from the environment. */ 16066427784SColin Percival strlcpy(cached_passphrase, env_passphrase, 16166427784SColin Percival sizeof(cached_passphrase)); 16266427784SColin Percival freeenv(env_passphrase); 16366427784SColin Percival 16466427784SColin Percival /* Wipe the passphrase from the environment. */ 16566427784SColin Percival kern_unsetenv("kern.geom.eli.passphrase"); 16666427784SColin Percival } 16766427784SColin Percival } 16866427784SColin Percival SYSINIT(geli_fetch_loader_passphrase, SI_SUB_KMEM + 1, SI_ORDER_ANY, 16966427784SColin Percival fetch_loader_passphrase, NULL); 170ec5c0e5bSAllan Jude 17166427784SColin Percival static void 172ec5c0e5bSAllan Jude zero_boot_passcache(void) 173835c4dd4SColin Percival { 174835c4dd4SColin Percival 175ec5c0e5bSAllan Jude explicit_bzero(cached_passphrase, sizeof(cached_passphrase)); 176835c4dd4SColin Percival } 177ec5c0e5bSAllan Jude 178ec5c0e5bSAllan Jude static void 179ec5c0e5bSAllan Jude zero_geli_intake_keys(void) 180ec5c0e5bSAllan Jude { 181ec5c0e5bSAllan Jude struct keybuf *keybuf; 182ec5c0e5bSAllan Jude int i; 183ec5c0e5bSAllan Jude 184ec5c0e5bSAllan Jude if ((keybuf = get_keybuf()) != NULL) { 185ec5c0e5bSAllan Jude /* Scan the key buffer, clear all GELI keys. */ 186ec5c0e5bSAllan Jude for (i = 0; i < keybuf->kb_nents; i++) { 187ec5c0e5bSAllan Jude if (keybuf->kb_ents[i].ke_type == KEYBUF_TYPE_GELI) { 188ec5c0e5bSAllan Jude explicit_bzero(keybuf->kb_ents[i].ke_data, 189ec5c0e5bSAllan Jude sizeof(keybuf->kb_ents[i].ke_data)); 190ec5c0e5bSAllan Jude keybuf->kb_ents[i].ke_type = KEYBUF_TYPE_NONE; 191ec5c0e5bSAllan Jude } 192ec5c0e5bSAllan Jude } 193ec5c0e5bSAllan Jude } 194ec5c0e5bSAllan Jude } 195ec5c0e5bSAllan Jude 196ec5c0e5bSAllan Jude static void 197ec5c0e5bSAllan Jude zero_intake_passcache(void *dummy) 198ec5c0e5bSAllan Jude { 199ec5c0e5bSAllan Jude zero_boot_passcache(); 200ec5c0e5bSAllan Jude zero_geli_intake_keys(); 201ec5c0e5bSAllan Jude } 202ec5c0e5bSAllan Jude EVENTHANDLER_DEFINE(mountroot, zero_intake_passcache, NULL, 0); 203835c4dd4SColin Percival 204c5d387d0SPawel Jakub Dawidek static eventhandler_tag g_eli_pre_sync = NULL; 205c5d387d0SPawel Jakub Dawidek 2062f07cdf8SPawel Jakub Dawidek static int g_eli_read_metadata_offset(struct g_class *mp, struct g_provider *pp, 2072f07cdf8SPawel Jakub Dawidek off_t offset, struct g_eli_metadata *md); 2082f07cdf8SPawel Jakub Dawidek 209c58794deSPawel Jakub Dawidek static int g_eli_destroy_geom(struct gctl_req *req, struct g_class *mp, 210c58794deSPawel Jakub Dawidek struct g_geom *gp); 211c5d387d0SPawel Jakub Dawidek static void g_eli_init(struct g_class *mp); 212c5d387d0SPawel Jakub Dawidek static void g_eli_fini(struct g_class *mp); 213c58794deSPawel Jakub Dawidek 214c58794deSPawel Jakub Dawidek static g_taste_t g_eli_taste; 215c58794deSPawel Jakub Dawidek static g_dumpconf_t g_eli_dumpconf; 216c58794deSPawel Jakub Dawidek 217c58794deSPawel Jakub Dawidek struct g_class g_eli_class = { 218c58794deSPawel Jakub Dawidek .name = G_ELI_CLASS_NAME, 219c58794deSPawel Jakub Dawidek .version = G_VERSION, 220c58794deSPawel Jakub Dawidek .ctlreq = g_eli_config, 221c58794deSPawel Jakub Dawidek .taste = g_eli_taste, 222c5d387d0SPawel Jakub Dawidek .destroy_geom = g_eli_destroy_geom, 223c5d387d0SPawel Jakub Dawidek .init = g_eli_init, 224c5d387d0SPawel Jakub Dawidek .fini = g_eli_fini 225c58794deSPawel Jakub Dawidek }; 226c58794deSPawel Jakub Dawidek 227c58794deSPawel Jakub Dawidek /* 228c58794deSPawel Jakub Dawidek * Code paths: 229c58794deSPawel Jakub Dawidek * BIO_READ: 2305ad4a7c7SPawel 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 231c58794deSPawel Jakub Dawidek * BIO_WRITE: 232c58794deSPawel Jakub Dawidek * g_eli_start -> g_eli_crypto_run -> g_eli_crypto_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver 233c58794deSPawel Jakub Dawidek */ 234c58794deSPawel Jakub Dawidek 235c58794deSPawel Jakub Dawidek /* 236c58794deSPawel Jakub Dawidek * EAGAIN from crypto(9) means, that we were probably balanced to another crypto 237c58794deSPawel Jakub Dawidek * accelerator or something like this. 238c58794deSPawel Jakub Dawidek * The function updates the SID and rerun the operation. 239c58794deSPawel Jakub Dawidek */ 240eaa3b919SPawel Jakub Dawidek int 241c58794deSPawel Jakub Dawidek g_eli_crypto_rerun(struct cryptop *crp) 242c58794deSPawel Jakub Dawidek { 243c58794deSPawel Jakub Dawidek struct g_eli_softc *sc; 244c58794deSPawel Jakub Dawidek struct g_eli_worker *wr; 245c58794deSPawel Jakub Dawidek struct bio *bp; 246c58794deSPawel Jakub Dawidek int error; 247c58794deSPawel Jakub Dawidek 248c58794deSPawel Jakub Dawidek bp = (struct bio *)crp->crp_opaque; 249c58794deSPawel Jakub Dawidek sc = bp->bio_to->geom->softc; 250c58794deSPawel Jakub Dawidek LIST_FOREACH(wr, &sc->sc_workers, w_next) { 2512dbc9a38SGleb Smirnoff if (wr->w_number == G_ELI_WORKER(bp->bio_pflags)) 252c58794deSPawel Jakub Dawidek break; 253c58794deSPawel Jakub Dawidek } 2542dbc9a38SGleb Smirnoff KASSERT(wr != NULL, ("Invalid worker (%u).", 2552dbc9a38SGleb Smirnoff G_ELI_WORKER(bp->bio_pflags))); 2561b0909d5SConrad Meyer G_ELI_DEBUG(1, "Rerunning crypto %s request (sid: %p -> %p).", 2571b0909d5SConrad Meyer bp->bio_cmd == BIO_READ ? "READ" : "WRITE", wr->w_sid, 2581b0909d5SConrad Meyer crp->crp_session); 2591b0909d5SConrad Meyer wr->w_sid = crp->crp_session; 260c58794deSPawel Jakub Dawidek crp->crp_etype = 0; 261c58794deSPawel Jakub Dawidek error = crypto_dispatch(crp); 262c58794deSPawel Jakub Dawidek if (error == 0) 263c58794deSPawel Jakub Dawidek return (0); 264c58794deSPawel Jakub Dawidek G_ELI_DEBUG(1, "%s: crypto_dispatch() returned %d.", __func__, error); 265c58794deSPawel Jakub Dawidek crp->crp_etype = error; 266c58794deSPawel Jakub Dawidek return (error); 267c58794deSPawel Jakub Dawidek } 268c58794deSPawel Jakub Dawidek 2690bab7fa8SAlan Somers static void 2700bab7fa8SAlan Somers g_eli_getattr_done(struct bio *bp) 2710bab7fa8SAlan Somers { 2720bab7fa8SAlan Somers if (bp->bio_error == 0 && 2730bab7fa8SAlan Somers !strcmp(bp->bio_attribute, "GEOM::physpath")) { 2740bab7fa8SAlan Somers strlcat(bp->bio_data, "/eli", bp->bio_length); 2750bab7fa8SAlan Somers } 2760bab7fa8SAlan Somers g_std_done(bp); 2770bab7fa8SAlan Somers } 2780bab7fa8SAlan Somers 279c58794deSPawel Jakub Dawidek /* 280c58794deSPawel Jakub Dawidek * The function is called afer reading encrypted data from the provider. 281bb30fea6SPawel Jakub Dawidek * 2825ad4a7c7SPawel 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 283c58794deSPawel Jakub Dawidek */ 284eaa3b919SPawel Jakub Dawidek void 285c58794deSPawel Jakub Dawidek g_eli_read_done(struct bio *bp) 286c58794deSPawel Jakub Dawidek { 287c58794deSPawel Jakub Dawidek struct g_eli_softc *sc; 288c58794deSPawel Jakub Dawidek struct bio *pbp; 289c58794deSPawel Jakub Dawidek 290c58794deSPawel Jakub Dawidek G_ELI_LOGREQ(2, bp, "Request done."); 291c58794deSPawel Jakub Dawidek pbp = bp->bio_parent; 2922dc7e36bSSteven Hartland if (pbp->bio_error == 0 && bp->bio_error != 0) 293c58794deSPawel Jakub Dawidek pbp->bio_error = bp->bio_error; 29490574b0aSMikolaj Golub g_destroy_bio(bp); 295eaa3b919SPawel Jakub Dawidek /* 296eaa3b919SPawel Jakub Dawidek * Do we have all sectors already? 297eaa3b919SPawel Jakub Dawidek */ 298eaa3b919SPawel Jakub Dawidek pbp->bio_inbed++; 299eaa3b919SPawel Jakub Dawidek if (pbp->bio_inbed < pbp->bio_children) 300eaa3b919SPawel Jakub Dawidek return; 3015ad4a7c7SPawel Jakub Dawidek sc = pbp->bio_to->geom->softc; 302c58794deSPawel Jakub Dawidek if (pbp->bio_error != 0) { 3032dc7e36bSSteven Hartland G_ELI_LOGREQ(0, pbp, "%s() failed (error=%d)", __func__, 3042dc7e36bSSteven Hartland pbp->bio_error); 305c58794deSPawel Jakub Dawidek pbp->bio_completed = 0; 3062dbc9a38SGleb Smirnoff g_eli_free_data(pbp); 307c58794deSPawel Jakub Dawidek g_io_deliver(pbp, pbp->bio_error); 30878f79a9aSMariusz Zaborski if (sc != NULL) 3095ad4a7c7SPawel Jakub Dawidek atomic_subtract_int(&sc->sc_inflight, 1); 310c58794deSPawel Jakub Dawidek return; 311c58794deSPawel Jakub Dawidek } 312c58794deSPawel Jakub Dawidek mtx_lock(&sc->sc_queue_mtx); 313c58794deSPawel Jakub Dawidek bioq_insert_tail(&sc->sc_queue, pbp); 314c58794deSPawel Jakub Dawidek mtx_unlock(&sc->sc_queue_mtx); 315c58794deSPawel Jakub Dawidek wakeup(sc); 316c58794deSPawel Jakub Dawidek } 317c58794deSPawel Jakub Dawidek 318c58794deSPawel Jakub Dawidek /* 319c58794deSPawel Jakub Dawidek * The function is called after we encrypt and write data. 320bb30fea6SPawel Jakub Dawidek * 321bb30fea6SPawel Jakub Dawidek * g_eli_start -> g_eli_crypto_run -> g_eli_crypto_write_done -> g_io_request -> G_ELI_WRITE_DONE -> g_io_deliver 322c58794deSPawel Jakub Dawidek */ 323eaa3b919SPawel Jakub Dawidek void 324c58794deSPawel Jakub Dawidek g_eli_write_done(struct bio *bp) 325c58794deSPawel Jakub Dawidek { 3265ad4a7c7SPawel Jakub Dawidek struct g_eli_softc *sc; 327c58794deSPawel Jakub Dawidek struct bio *pbp; 328c58794deSPawel Jakub Dawidek 329c58794deSPawel Jakub Dawidek G_ELI_LOGREQ(2, bp, "Request done."); 330c58794deSPawel Jakub Dawidek pbp = bp->bio_parent; 3312dc7e36bSSteven Hartland if (pbp->bio_error == 0 && bp->bio_error != 0) 332c58794deSPawel Jakub Dawidek pbp->bio_error = bp->bio_error; 33390574b0aSMikolaj Golub g_destroy_bio(bp); 334eaa3b919SPawel Jakub Dawidek /* 335eaa3b919SPawel Jakub Dawidek * Do we have all sectors already? 336eaa3b919SPawel Jakub Dawidek */ 337eaa3b919SPawel Jakub Dawidek pbp->bio_inbed++; 338eaa3b919SPawel Jakub Dawidek if (pbp->bio_inbed < pbp->bio_children) 339eaa3b919SPawel Jakub Dawidek return; 3402dbc9a38SGleb Smirnoff sc = pbp->bio_to->geom->softc; 3412dbc9a38SGleb Smirnoff g_eli_free_data(pbp); 342eaa3b919SPawel Jakub Dawidek if (pbp->bio_error != 0) { 3432dc7e36bSSteven Hartland G_ELI_LOGREQ(0, pbp, "%s() failed (error=%d)", __func__, 344c58794deSPawel Jakub Dawidek pbp->bio_error); 345c58794deSPawel Jakub Dawidek pbp->bio_completed = 0; 3462dc7e36bSSteven Hartland } else 3472dc7e36bSSteven Hartland pbp->bio_completed = pbp->bio_length; 3482dc7e36bSSteven Hartland 349c58794deSPawel Jakub Dawidek /* 350c58794deSPawel Jakub Dawidek * Write is finished, send it up. 351c58794deSPawel Jakub Dawidek */ 352c58794deSPawel Jakub Dawidek g_io_deliver(pbp, pbp->bio_error); 35378f79a9aSMariusz Zaborski if (sc != NULL) 3545ad4a7c7SPawel Jakub Dawidek atomic_subtract_int(&sc->sc_inflight, 1); 355c58794deSPawel Jakub Dawidek } 356c58794deSPawel Jakub Dawidek 357c58794deSPawel Jakub Dawidek /* 358c58794deSPawel Jakub Dawidek * This function should never be called, but GEOM made as it set ->orphan() 359c58794deSPawel Jakub Dawidek * method for every geom. 360c58794deSPawel Jakub Dawidek */ 361c58794deSPawel Jakub Dawidek static void 362c58794deSPawel Jakub Dawidek g_eli_orphan_spoil_assert(struct g_consumer *cp) 363c58794deSPawel Jakub Dawidek { 364c58794deSPawel Jakub Dawidek 365c58794deSPawel Jakub Dawidek panic("Function %s() called for %s.", __func__, cp->geom->name); 366c58794deSPawel Jakub Dawidek } 367c58794deSPawel Jakub Dawidek 368c58794deSPawel Jakub Dawidek static void 369c58794deSPawel Jakub Dawidek g_eli_orphan(struct g_consumer *cp) 370c58794deSPawel Jakub Dawidek { 371c58794deSPawel Jakub Dawidek struct g_eli_softc *sc; 372c58794deSPawel Jakub Dawidek 373c58794deSPawel Jakub Dawidek g_topology_assert(); 374c58794deSPawel Jakub Dawidek sc = cp->geom->softc; 375c58794deSPawel Jakub Dawidek if (sc == NULL) 376c58794deSPawel Jakub Dawidek return; 3775ad4a7c7SPawel Jakub Dawidek g_eli_destroy(sc, TRUE); 378c58794deSPawel Jakub Dawidek } 379c58794deSPawel Jakub Dawidek 3802f07cdf8SPawel Jakub Dawidek static void 3812f07cdf8SPawel Jakub Dawidek g_eli_resize(struct g_consumer *cp) 3822f07cdf8SPawel Jakub Dawidek { 3832f07cdf8SPawel Jakub Dawidek struct g_eli_softc *sc; 3842f07cdf8SPawel Jakub Dawidek struct g_provider *epp, *pp; 3852f07cdf8SPawel Jakub Dawidek off_t oldsize; 3862f07cdf8SPawel Jakub Dawidek 3872f07cdf8SPawel Jakub Dawidek g_topology_assert(); 3882f07cdf8SPawel Jakub Dawidek sc = cp->geom->softc; 3892f07cdf8SPawel Jakub Dawidek if (sc == NULL) 3902f07cdf8SPawel Jakub Dawidek return; 3912f07cdf8SPawel Jakub Dawidek 3922f07cdf8SPawel Jakub Dawidek if ((sc->sc_flags & G_ELI_FLAG_AUTORESIZE) == 0) { 3932f07cdf8SPawel Jakub Dawidek G_ELI_DEBUG(0, "Autoresize is turned off, old size: %jd.", 3942f07cdf8SPawel Jakub Dawidek (intmax_t)sc->sc_provsize); 3952f07cdf8SPawel Jakub Dawidek return; 3962f07cdf8SPawel Jakub Dawidek } 3972f07cdf8SPawel Jakub Dawidek 3982f07cdf8SPawel Jakub Dawidek pp = cp->provider; 3992f07cdf8SPawel Jakub Dawidek 4002f07cdf8SPawel Jakub Dawidek if ((sc->sc_flags & G_ELI_FLAG_ONETIME) == 0) { 4012f07cdf8SPawel Jakub Dawidek struct g_eli_metadata md; 4022f07cdf8SPawel Jakub Dawidek u_char *sector; 4032f07cdf8SPawel Jakub Dawidek int error; 4042f07cdf8SPawel Jakub Dawidek 4052f07cdf8SPawel Jakub Dawidek sector = NULL; 4062f07cdf8SPawel Jakub Dawidek 4072f07cdf8SPawel Jakub Dawidek error = g_eli_read_metadata_offset(cp->geom->class, pp, 4082f07cdf8SPawel Jakub Dawidek sc->sc_provsize - pp->sectorsize, &md); 4092f07cdf8SPawel Jakub Dawidek if (error != 0) { 4102f07cdf8SPawel Jakub Dawidek G_ELI_DEBUG(0, "Cannot read metadata from %s (error=%d).", 4112f07cdf8SPawel Jakub Dawidek pp->name, error); 4122f07cdf8SPawel Jakub Dawidek goto iofail; 4132f07cdf8SPawel Jakub Dawidek } 4142f07cdf8SPawel Jakub Dawidek 4152f07cdf8SPawel Jakub Dawidek md.md_provsize = pp->mediasize; 4162f07cdf8SPawel Jakub Dawidek 4172f07cdf8SPawel Jakub Dawidek sector = malloc(pp->sectorsize, M_ELI, M_WAITOK | M_ZERO); 4182f07cdf8SPawel Jakub Dawidek eli_metadata_encode(&md, sector); 4192f07cdf8SPawel Jakub Dawidek error = g_write_data(cp, pp->mediasize - pp->sectorsize, sector, 4202f07cdf8SPawel Jakub Dawidek pp->sectorsize); 4212f07cdf8SPawel Jakub Dawidek if (error != 0) { 4222f07cdf8SPawel Jakub Dawidek G_ELI_DEBUG(0, "Cannot store metadata on %s (error=%d).", 4232f07cdf8SPawel Jakub Dawidek pp->name, error); 4242f07cdf8SPawel Jakub Dawidek goto iofail; 4252f07cdf8SPawel Jakub Dawidek } 4262f07cdf8SPawel Jakub Dawidek explicit_bzero(sector, pp->sectorsize); 4272f07cdf8SPawel Jakub Dawidek error = g_write_data(cp, sc->sc_provsize - pp->sectorsize, 4282f07cdf8SPawel Jakub Dawidek sector, pp->sectorsize); 4292f07cdf8SPawel Jakub Dawidek if (error != 0) { 4302f07cdf8SPawel Jakub Dawidek G_ELI_DEBUG(0, "Cannot clear old metadata from %s (error=%d).", 4312f07cdf8SPawel Jakub Dawidek pp->name, error); 4322f07cdf8SPawel Jakub Dawidek goto iofail; 4332f07cdf8SPawel Jakub Dawidek } 4342f07cdf8SPawel Jakub Dawidek iofail: 4352f07cdf8SPawel Jakub Dawidek explicit_bzero(&md, sizeof(md)); 4364a711b8dSJohn Baldwin zfree(sector, M_ELI); 4372f07cdf8SPawel Jakub Dawidek } 4382f07cdf8SPawel Jakub Dawidek 4392f07cdf8SPawel Jakub Dawidek oldsize = sc->sc_mediasize; 4402f07cdf8SPawel Jakub Dawidek sc->sc_mediasize = eli_mediasize(sc, pp->mediasize, pp->sectorsize); 4412f07cdf8SPawel Jakub Dawidek g_eli_key_resize(sc); 4422f07cdf8SPawel Jakub Dawidek sc->sc_provsize = pp->mediasize; 4432f07cdf8SPawel Jakub Dawidek 4442f07cdf8SPawel Jakub Dawidek epp = LIST_FIRST(&sc->sc_geom->provider); 4452f07cdf8SPawel Jakub Dawidek g_resize_provider(epp, sc->sc_mediasize); 4462f07cdf8SPawel Jakub Dawidek G_ELI_DEBUG(0, "Device %s size changed from %jd to %jd.", epp->name, 4472f07cdf8SPawel Jakub Dawidek (intmax_t)oldsize, (intmax_t)sc->sc_mediasize); 4482f07cdf8SPawel Jakub Dawidek } 4492f07cdf8SPawel Jakub Dawidek 450bb30fea6SPawel Jakub Dawidek /* 451056638c4SPawel Jakub Dawidek * BIO_READ: 4525ad4a7c7SPawel 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 453056638c4SPawel Jakub Dawidek * BIO_WRITE: 454056638c4SPawel Jakub Dawidek * G_ELI_START -> g_eli_crypto_run -> g_eli_crypto_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver 455bb30fea6SPawel Jakub Dawidek */ 456c58794deSPawel Jakub Dawidek static void 457c58794deSPawel Jakub Dawidek g_eli_start(struct bio *bp) 458c58794deSPawel Jakub Dawidek { 459c58794deSPawel Jakub Dawidek struct g_eli_softc *sc; 460d3a1be90SPawel Jakub Dawidek struct g_consumer *cp; 461c58794deSPawel Jakub Dawidek struct bio *cbp; 462c58794deSPawel Jakub Dawidek 463c58794deSPawel Jakub Dawidek sc = bp->bio_to->geom->softc; 464c58794deSPawel Jakub Dawidek KASSERT(sc != NULL, 465c58794deSPawel Jakub Dawidek ("Provider's error should be set (error=%d)(device=%s).", 466c58794deSPawel Jakub Dawidek bp->bio_to->error, bp->bio_to->name)); 467c58794deSPawel Jakub Dawidek G_ELI_LOGREQ(2, bp, "Request received."); 468c58794deSPawel Jakub Dawidek 469c58794deSPawel Jakub Dawidek switch (bp->bio_cmd) { 470c58794deSPawel Jakub Dawidek case BIO_READ: 471c58794deSPawel Jakub Dawidek case BIO_WRITE: 472d3a1be90SPawel Jakub Dawidek case BIO_GETATTR: 47342461fbaSPawel Jakub Dawidek case BIO_FLUSH: 4749a6844d5SKenneth D. Merry case BIO_ZONE: 4758b522bdaSWarner Losh case BIO_SPEEDUP: 476c58794deSPawel Jakub Dawidek break; 477c58794deSPawel Jakub Dawidek case BIO_DELETE: 478c58794deSPawel Jakub Dawidek /* 47946e34470SPawel Jakub Dawidek * If the user hasn't set the NODELETE flag, we just pass 48046e34470SPawel Jakub Dawidek * it down the stack and let the layers beneath us do (or 48146e34470SPawel Jakub Dawidek * not) whatever they do with it. If they have, we 48246e34470SPawel Jakub Dawidek * reject it. A possible extension would be an 48346e34470SPawel Jakub Dawidek * additional flag to take it as a hint to shred the data 48446e34470SPawel Jakub Dawidek * with [multiple?] overwrites. 485c58794deSPawel Jakub Dawidek */ 48646e34470SPawel Jakub Dawidek if (!(sc->sc_flags & G_ELI_FLAG_NODELETE)) 48746e34470SPawel Jakub Dawidek break; 488c58794deSPawel Jakub Dawidek default: 489c58794deSPawel Jakub Dawidek g_io_deliver(bp, EOPNOTSUPP); 490c58794deSPawel Jakub Dawidek return; 491c58794deSPawel Jakub Dawidek } 492c58794deSPawel Jakub Dawidek cbp = g_clone_bio(bp); 493c58794deSPawel Jakub Dawidek if (cbp == NULL) { 494c58794deSPawel Jakub Dawidek g_io_deliver(bp, ENOMEM); 495c58794deSPawel Jakub Dawidek return; 496c58794deSPawel Jakub Dawidek } 4975ad4a7c7SPawel Jakub Dawidek bp->bio_driver1 = cbp; 4982dbc9a38SGleb Smirnoff bp->bio_pflags = 0; 4992dbc9a38SGleb Smirnoff G_ELI_SET_NEW_BIO(bp->bio_pflags); 500d3a1be90SPawel Jakub Dawidek switch (bp->bio_cmd) { 501d3a1be90SPawel Jakub Dawidek case BIO_READ: 502eaa3b919SPawel Jakub Dawidek if (!(sc->sc_flags & G_ELI_FLAG_AUTH)) { 5035ad4a7c7SPawel Jakub Dawidek g_eli_crypto_read(sc, bp, 0); 504d3a1be90SPawel Jakub Dawidek break; 505eaa3b919SPawel Jakub Dawidek } 506eaa3b919SPawel Jakub Dawidek /* FALLTHROUGH */ 507d3a1be90SPawel Jakub Dawidek case BIO_WRITE: 508c58794deSPawel Jakub Dawidek mtx_lock(&sc->sc_queue_mtx); 509c58794deSPawel Jakub Dawidek bioq_insert_tail(&sc->sc_queue, bp); 510c58794deSPawel Jakub Dawidek mtx_unlock(&sc->sc_queue_mtx); 511c58794deSPawel Jakub Dawidek wakeup(sc); 512d3a1be90SPawel Jakub Dawidek break; 513d3a1be90SPawel Jakub Dawidek case BIO_GETATTR: 51442461fbaSPawel Jakub Dawidek case BIO_FLUSH: 51546e34470SPawel Jakub Dawidek case BIO_DELETE: 5168b522bdaSWarner Losh case BIO_SPEEDUP: 5179a6844d5SKenneth D. Merry case BIO_ZONE: 5180bab7fa8SAlan Somers if (bp->bio_cmd == BIO_GETATTR) 5190bab7fa8SAlan Somers cbp->bio_done = g_eli_getattr_done; 5200bab7fa8SAlan Somers else 521d3a1be90SPawel Jakub Dawidek cbp->bio_done = g_std_done; 522d3a1be90SPawel Jakub Dawidek cp = LIST_FIRST(&sc->sc_geom->consumer); 523d3a1be90SPawel Jakub Dawidek cbp->bio_to = cp->provider; 524cd0d707eSPawel Jakub Dawidek G_ELI_LOGREQ(2, cbp, "Sending request."); 525d3a1be90SPawel Jakub Dawidek g_io_request(cbp, cp); 526d3a1be90SPawel Jakub Dawidek break; 527c58794deSPawel Jakub Dawidek } 528c58794deSPawel Jakub Dawidek } 529c58794deSPawel Jakub Dawidek 5303ac01bc2SPawel Jakub Dawidek static int 5313ac01bc2SPawel Jakub Dawidek g_eli_newsession(struct g_eli_worker *wr) 5323ac01bc2SPawel Jakub Dawidek { 5333ac01bc2SPawel Jakub Dawidek struct g_eli_softc *sc; 534c0341432SJohn Baldwin struct crypto_session_params csp; 535a3d565a1SJohn Baldwin uint32_t caps; 536a3d565a1SJohn Baldwin int error, new_crypto; 537c0341432SJohn Baldwin void *key; 5383ac01bc2SPawel Jakub Dawidek 5393ac01bc2SPawel Jakub Dawidek sc = wr->w_softc; 5403ac01bc2SPawel Jakub Dawidek 541c0341432SJohn Baldwin memset(&csp, 0, sizeof(csp)); 542c0341432SJohn Baldwin csp.csp_mode = CSP_MODE_CIPHER; 543c0341432SJohn Baldwin csp.csp_cipher_alg = sc->sc_ealgo; 544c0341432SJohn Baldwin csp.csp_ivlen = g_eli_ivlen(sc->sc_ealgo); 545c0341432SJohn Baldwin csp.csp_cipher_klen = sc->sc_ekeylen / 8; 5463ac01bc2SPawel Jakub Dawidek if (sc->sc_ealgo == CRYPTO_AES_XTS) 547c0341432SJohn Baldwin csp.csp_cipher_klen <<= 1; 548ad0a5236SPawel Jakub Dawidek if ((sc->sc_flags & G_ELI_FLAG_FIRST_KEY) != 0) { 549c0341432SJohn Baldwin key = g_eli_key_hold(sc, 0, 550ad0a5236SPawel Jakub Dawidek LIST_FIRST(&sc->sc_geom->consumer)->provider->sectorsize); 551c0341432SJohn Baldwin csp.csp_cipher_key = key; 552ad0a5236SPawel Jakub Dawidek } else { 553c0341432SJohn Baldwin key = NULL; 554c0341432SJohn Baldwin csp.csp_cipher_key = sc->sc_ekey; 555ad0a5236SPawel Jakub Dawidek } 5563ac01bc2SPawel Jakub Dawidek if (sc->sc_flags & G_ELI_FLAG_AUTH) { 557c0341432SJohn Baldwin csp.csp_mode = CSP_MODE_ETA; 558c0341432SJohn Baldwin csp.csp_auth_alg = sc->sc_aalgo; 559c0341432SJohn Baldwin csp.csp_auth_klen = G_ELI_AUTH_SECKEYLEN; 5603ac01bc2SPawel Jakub Dawidek } 5613ac01bc2SPawel Jakub Dawidek 5623ac01bc2SPawel Jakub Dawidek switch (sc->sc_crypto) { 563a3d565a1SJohn Baldwin case G_ELI_CRYPTO_SW_ACCEL: 5643ac01bc2SPawel Jakub Dawidek case G_ELI_CRYPTO_SW: 565c0341432SJohn Baldwin error = crypto_newsession(&wr->w_sid, &csp, 5663ac01bc2SPawel Jakub Dawidek CRYPTOCAP_F_SOFTWARE); 5673ac01bc2SPawel Jakub Dawidek break; 5683ac01bc2SPawel Jakub Dawidek case G_ELI_CRYPTO_HW: 569c0341432SJohn Baldwin error = crypto_newsession(&wr->w_sid, &csp, 5703ac01bc2SPawel Jakub Dawidek CRYPTOCAP_F_HARDWARE); 5713ac01bc2SPawel Jakub Dawidek break; 5723ac01bc2SPawel Jakub Dawidek case G_ELI_CRYPTO_UNKNOWN: 573c0341432SJohn Baldwin error = crypto_newsession(&wr->w_sid, &csp, 574a3d565a1SJohn Baldwin CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE); 5753ac01bc2SPawel Jakub Dawidek if (error == 0) { 576a3d565a1SJohn Baldwin caps = crypto_ses2caps(wr->w_sid); 577a3d565a1SJohn Baldwin if (caps & CRYPTOCAP_F_HARDWARE) 578a3d565a1SJohn Baldwin new_crypto = G_ELI_CRYPTO_HW; 579a3d565a1SJohn Baldwin else if (caps & CRYPTOCAP_F_ACCEL_SOFTWARE) 580a3d565a1SJohn Baldwin new_crypto = G_ELI_CRYPTO_SW_ACCEL; 581a3d565a1SJohn Baldwin else 582a3d565a1SJohn Baldwin new_crypto = G_ELI_CRYPTO_SW; 5833ac01bc2SPawel Jakub Dawidek mtx_lock(&sc->sc_queue_mtx); 5843ac01bc2SPawel Jakub Dawidek if (sc->sc_crypto == G_ELI_CRYPTO_UNKNOWN) 585a3d565a1SJohn Baldwin sc->sc_crypto = new_crypto; 5863ac01bc2SPawel Jakub Dawidek mtx_unlock(&sc->sc_queue_mtx); 5873ac01bc2SPawel Jakub Dawidek } 5883ac01bc2SPawel Jakub Dawidek break; 5893ac01bc2SPawel Jakub Dawidek default: 5903ac01bc2SPawel Jakub Dawidek panic("%s: invalid condition", __func__); 5913ac01bc2SPawel Jakub Dawidek } 5923ac01bc2SPawel Jakub Dawidek 593c0341432SJohn Baldwin if ((sc->sc_flags & G_ELI_FLAG_FIRST_KEY) != 0) { 594c0341432SJohn Baldwin if (error) 595c0341432SJohn Baldwin g_eli_key_drop(sc, key); 596c0341432SJohn Baldwin else 597c0341432SJohn Baldwin wr->w_first_key = key; 598c0341432SJohn Baldwin } 599ad0a5236SPawel Jakub Dawidek 6003ac01bc2SPawel Jakub Dawidek return (error); 6013ac01bc2SPawel Jakub Dawidek } 6023ac01bc2SPawel Jakub Dawidek 6033ac01bc2SPawel Jakub Dawidek static void 6043ac01bc2SPawel Jakub Dawidek g_eli_freesession(struct g_eli_worker *wr) 6053ac01bc2SPawel Jakub Dawidek { 606c0341432SJohn Baldwin struct g_eli_softc *sc; 6073ac01bc2SPawel Jakub Dawidek 6083ac01bc2SPawel Jakub Dawidek crypto_freesession(wr->w_sid); 609c0341432SJohn Baldwin if (wr->w_first_key != NULL) { 610c0341432SJohn Baldwin sc = wr->w_softc; 611c0341432SJohn Baldwin g_eli_key_drop(sc, wr->w_first_key); 612c0341432SJohn Baldwin wr->w_first_key = NULL; 613c0341432SJohn Baldwin } 6143ac01bc2SPawel Jakub Dawidek } 6153ac01bc2SPawel Jakub Dawidek 6165ad4a7c7SPawel Jakub Dawidek static void 6175ad4a7c7SPawel Jakub Dawidek g_eli_cancel(struct g_eli_softc *sc) 6185ad4a7c7SPawel Jakub Dawidek { 6195ad4a7c7SPawel Jakub Dawidek struct bio *bp; 6205ad4a7c7SPawel Jakub Dawidek 6215ad4a7c7SPawel Jakub Dawidek mtx_assert(&sc->sc_queue_mtx, MA_OWNED); 6225ad4a7c7SPawel Jakub Dawidek 6235ad4a7c7SPawel Jakub Dawidek while ((bp = bioq_takefirst(&sc->sc_queue)) != NULL) { 6242dbc9a38SGleb Smirnoff KASSERT(G_ELI_IS_NEW_BIO(bp->bio_pflags), 6255ad4a7c7SPawel Jakub Dawidek ("Not new bio when canceling (bp=%p).", bp)); 6265ad4a7c7SPawel Jakub Dawidek g_io_deliver(bp, ENXIO); 6275ad4a7c7SPawel Jakub Dawidek } 6285ad4a7c7SPawel Jakub Dawidek } 6295ad4a7c7SPawel Jakub Dawidek 6305ad4a7c7SPawel Jakub Dawidek static struct bio * 6315ad4a7c7SPawel Jakub Dawidek g_eli_takefirst(struct g_eli_softc *sc) 6325ad4a7c7SPawel Jakub Dawidek { 6335ad4a7c7SPawel Jakub Dawidek struct bio *bp; 6345ad4a7c7SPawel Jakub Dawidek 6355ad4a7c7SPawel Jakub Dawidek mtx_assert(&sc->sc_queue_mtx, MA_OWNED); 6365ad4a7c7SPawel Jakub Dawidek 6375ad4a7c7SPawel Jakub Dawidek if (!(sc->sc_flags & G_ELI_FLAG_SUSPEND)) 6385ad4a7c7SPawel Jakub Dawidek return (bioq_takefirst(&sc->sc_queue)); 6395ad4a7c7SPawel Jakub Dawidek /* 6405ad4a7c7SPawel Jakub Dawidek * Device suspended, so we skip new I/O requests. 6415ad4a7c7SPawel Jakub Dawidek */ 6425ad4a7c7SPawel Jakub Dawidek TAILQ_FOREACH(bp, &sc->sc_queue.queue, bio_queue) { 6432dbc9a38SGleb Smirnoff if (!G_ELI_IS_NEW_BIO(bp->bio_pflags)) 6445ad4a7c7SPawel Jakub Dawidek break; 6455ad4a7c7SPawel Jakub Dawidek } 6465ad4a7c7SPawel Jakub Dawidek if (bp != NULL) 6475ad4a7c7SPawel Jakub Dawidek bioq_remove(&sc->sc_queue, bp); 6485ad4a7c7SPawel Jakub Dawidek return (bp); 6495ad4a7c7SPawel Jakub Dawidek } 6505ad4a7c7SPawel Jakub Dawidek 651c58794deSPawel Jakub Dawidek /* 652c58794deSPawel Jakub Dawidek * This is the main function for kernel worker thread when we don't have 653c58794deSPawel Jakub Dawidek * hardware acceleration and we have to do cryptography in software. 654c58794deSPawel Jakub Dawidek * Dedicated thread is needed, so we don't slow down g_up/g_down GEOM 655c58794deSPawel Jakub Dawidek * threads with crypto work. 656c58794deSPawel Jakub Dawidek */ 657c58794deSPawel Jakub Dawidek static void 658c58794deSPawel Jakub Dawidek g_eli_worker(void *arg) 659c58794deSPawel Jakub Dawidek { 660c58794deSPawel Jakub Dawidek struct g_eli_softc *sc; 661c58794deSPawel Jakub Dawidek struct g_eli_worker *wr; 662c58794deSPawel Jakub Dawidek struct bio *bp; 663c9048120SMateusz Guzik int error __diagused; 664c58794deSPawel Jakub Dawidek 665c58794deSPawel Jakub Dawidek wr = arg; 666c58794deSPawel Jakub Dawidek sc = wr->w_softc; 667fdce57a0SJohn Baldwin #ifdef EARLY_AP_STARTUP 668fdce57a0SJohn Baldwin MPASS(!sc->sc_cpubind || smp_started); 669fdce57a0SJohn Baldwin #elif defined(SMP) 670a1ea1a22SPawel Jakub Dawidek /* Before sched_bind() to a CPU, wait for all CPUs to go on-line. */ 6710c879bd9SPawel Jakub Dawidek if (sc->sc_cpubind) { 672a1ea1a22SPawel Jakub Dawidek while (!smp_started) 673a1ea1a22SPawel Jakub Dawidek tsleep(wr, 0, "geli:smp", hz / 4); 674a1ea1a22SPawel Jakub Dawidek } 675a1ea1a22SPawel Jakub Dawidek #endif 676982d11f8SJeff Roberson thread_lock(curthread); 67731c4cef7SPawel Jakub Dawidek sched_prio(curthread, PUSER); 6780c879bd9SPawel Jakub Dawidek if (sc->sc_cpubind) 6790c879bd9SPawel Jakub Dawidek sched_bind(curthread, wr->w_number % mp_ncpus); 680982d11f8SJeff Roberson thread_unlock(curthread); 681c58794deSPawel Jakub Dawidek 682c58794deSPawel Jakub Dawidek G_ELI_DEBUG(1, "Thread %s started.", curthread->td_proc->p_comm); 683c58794deSPawel Jakub Dawidek 684c58794deSPawel Jakub Dawidek for (;;) { 685c58794deSPawel Jakub Dawidek mtx_lock(&sc->sc_queue_mtx); 6865ad4a7c7SPawel Jakub Dawidek again: 6875ad4a7c7SPawel Jakub Dawidek bp = g_eli_takefirst(sc); 688c58794deSPawel Jakub Dawidek if (bp == NULL) { 689eaa3b919SPawel Jakub Dawidek if (sc->sc_flags & G_ELI_FLAG_DESTROY) { 6905ad4a7c7SPawel Jakub Dawidek g_eli_cancel(sc); 691c58794deSPawel Jakub Dawidek LIST_REMOVE(wr, w_next); 6923ac01bc2SPawel Jakub Dawidek g_eli_freesession(wr); 693c58794deSPawel Jakub Dawidek free(wr, M_ELI); 694c58794deSPawel Jakub Dawidek G_ELI_DEBUG(1, "Thread %s exiting.", 695c58794deSPawel Jakub Dawidek curthread->td_proc->p_comm); 696c58794deSPawel Jakub Dawidek wakeup(&sc->sc_workers); 697c58794deSPawel Jakub Dawidek mtx_unlock(&sc->sc_queue_mtx); 6983745c395SJulian Elischer kproc_exit(0); 699c58794deSPawel Jakub Dawidek } 7005ad4a7c7SPawel Jakub Dawidek while (sc->sc_flags & G_ELI_FLAG_SUSPEND) { 7015ad4a7c7SPawel Jakub Dawidek if (sc->sc_inflight > 0) { 702038c55adSPawel Jakub Dawidek G_ELI_DEBUG(0, "inflight=%d", 703038c55adSPawel Jakub Dawidek sc->sc_inflight); 7045ad4a7c7SPawel Jakub Dawidek /* 7055ad4a7c7SPawel Jakub Dawidek * We still have inflight BIOs, so 7065ad4a7c7SPawel Jakub Dawidek * sleep and retry. 7075ad4a7c7SPawel Jakub Dawidek */ 7085ad4a7c7SPawel Jakub Dawidek msleep(sc, &sc->sc_queue_mtx, PRIBIO, 7095ad4a7c7SPawel Jakub Dawidek "geli:inf", hz / 5); 7105ad4a7c7SPawel Jakub Dawidek goto again; 7115ad4a7c7SPawel Jakub Dawidek } 7125ad4a7c7SPawel Jakub Dawidek /* 7135ad4a7c7SPawel Jakub Dawidek * Suspend requested, mark the worker as 7145ad4a7c7SPawel Jakub Dawidek * suspended and go to sleep. 7155ad4a7c7SPawel Jakub Dawidek */ 7163ac01bc2SPawel Jakub Dawidek if (wr->w_active) { 7173ac01bc2SPawel Jakub Dawidek g_eli_freesession(wr); 7183ac01bc2SPawel Jakub Dawidek wr->w_active = FALSE; 7193ac01bc2SPawel Jakub Dawidek } 7205ad4a7c7SPawel Jakub Dawidek wakeup(&sc->sc_workers); 7215ad4a7c7SPawel Jakub Dawidek msleep(sc, &sc->sc_queue_mtx, PRIBIO, 7225ad4a7c7SPawel Jakub Dawidek "geli:suspend", 0); 7233ac01bc2SPawel Jakub Dawidek if (!wr->w_active && 7243ac01bc2SPawel Jakub Dawidek !(sc->sc_flags & G_ELI_FLAG_SUSPEND)) { 7253ac01bc2SPawel Jakub Dawidek error = g_eli_newsession(wr); 7263ac01bc2SPawel Jakub Dawidek KASSERT(error == 0, 7273ac01bc2SPawel Jakub Dawidek ("g_eli_newsession() failed on resume (error=%d)", 7283ac01bc2SPawel Jakub Dawidek error)); 7293ac01bc2SPawel Jakub Dawidek wr->w_active = TRUE; 7303ac01bc2SPawel Jakub Dawidek } 7315ad4a7c7SPawel Jakub Dawidek goto again; 7325ad4a7c7SPawel Jakub Dawidek } 73331c4cef7SPawel Jakub Dawidek msleep(sc, &sc->sc_queue_mtx, PDROP, "geli:w", 0); 734c58794deSPawel Jakub Dawidek continue; 735c58794deSPawel Jakub Dawidek } 7362dbc9a38SGleb Smirnoff if (G_ELI_IS_NEW_BIO(bp->bio_pflags)) 7375ad4a7c7SPawel Jakub Dawidek atomic_add_int(&sc->sc_inflight, 1); 738c58794deSPawel Jakub Dawidek mtx_unlock(&sc->sc_queue_mtx); 7392dbc9a38SGleb Smirnoff if (G_ELI_IS_NEW_BIO(bp->bio_pflags)) { 7402dbc9a38SGleb Smirnoff G_ELI_SETWORKER(bp->bio_pflags, 0); 7415ad4a7c7SPawel Jakub Dawidek if (sc->sc_flags & G_ELI_FLAG_AUTH) { 7425ad4a7c7SPawel Jakub Dawidek if (bp->bio_cmd == BIO_READ) 743eaa3b919SPawel Jakub Dawidek g_eli_auth_read(sc, bp); 7445ad4a7c7SPawel Jakub Dawidek else 7455ad4a7c7SPawel Jakub Dawidek g_eli_auth_run(wr, bp); 7465ad4a7c7SPawel Jakub Dawidek } else { 7475ad4a7c7SPawel Jakub Dawidek if (bp->bio_cmd == BIO_READ) 7485ad4a7c7SPawel Jakub Dawidek g_eli_crypto_read(sc, bp, 1); 7495ad4a7c7SPawel Jakub Dawidek else 7505ad4a7c7SPawel Jakub Dawidek g_eli_crypto_run(wr, bp); 7515ad4a7c7SPawel Jakub Dawidek } 7525ad4a7c7SPawel Jakub Dawidek } else { 7535ad4a7c7SPawel Jakub Dawidek if (sc->sc_flags & G_ELI_FLAG_AUTH) 754eaa3b919SPawel Jakub Dawidek g_eli_auth_run(wr, bp); 755eaa3b919SPawel Jakub Dawidek else 756dddd1d53SPawel Jakub Dawidek g_eli_crypto_run(wr, bp); 757c58794deSPawel Jakub Dawidek } 758c58794deSPawel Jakub Dawidek } 7595ad4a7c7SPawel Jakub Dawidek } 760c58794deSPawel Jakub Dawidek 7612f07cdf8SPawel Jakub Dawidek static int 7622f07cdf8SPawel Jakub Dawidek g_eli_read_metadata_offset(struct g_class *mp, struct g_provider *pp, 7632f07cdf8SPawel Jakub Dawidek off_t offset, struct g_eli_metadata *md) 764c58794deSPawel Jakub Dawidek { 765c58794deSPawel Jakub Dawidek struct g_geom *gp; 766c58794deSPawel Jakub Dawidek struct g_consumer *cp; 767c58794deSPawel Jakub Dawidek u_char *buf = NULL; 768c58794deSPawel Jakub Dawidek int error; 769c58794deSPawel Jakub Dawidek 770c58794deSPawel Jakub Dawidek g_topology_assert(); 771c58794deSPawel Jakub Dawidek 772c58794deSPawel Jakub Dawidek gp = g_new_geomf(mp, "eli:taste"); 773c58794deSPawel Jakub Dawidek gp->start = g_eli_start; 774c58794deSPawel Jakub Dawidek gp->access = g_std_access; 775c58794deSPawel Jakub Dawidek /* 776c58794deSPawel Jakub Dawidek * g_eli_read_metadata() is always called from the event thread. 777c58794deSPawel Jakub Dawidek * Our geom is created and destroyed in the same event, so there 778c58794deSPawel Jakub Dawidek * could be no orphan nor spoil event in the meantime. 779c58794deSPawel Jakub Dawidek */ 780c58794deSPawel Jakub Dawidek gp->orphan = g_eli_orphan_spoil_assert; 781c58794deSPawel Jakub Dawidek gp->spoiled = g_eli_orphan_spoil_assert; 782c58794deSPawel Jakub Dawidek cp = g_new_consumer(gp); 7836f818c1fSAlan Somers cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE; 784c58794deSPawel Jakub Dawidek error = g_attach(cp, pp); 785c58794deSPawel Jakub Dawidek if (error != 0) 786c58794deSPawel Jakub Dawidek goto end; 787c58794deSPawel Jakub Dawidek error = g_access(cp, 1, 0, 0); 788c58794deSPawel Jakub Dawidek if (error != 0) 789c58794deSPawel Jakub Dawidek goto end; 790c58794deSPawel Jakub Dawidek g_topology_unlock(); 7912f07cdf8SPawel Jakub Dawidek buf = g_read_data(cp, offset, pp->sectorsize, &error); 792c58794deSPawel Jakub Dawidek g_topology_lock(); 7938a4a44b5SMaxim Sobolev if (buf == NULL) 794c58794deSPawel Jakub Dawidek goto end; 795fefb6a14SPawel Jakub Dawidek error = eli_metadata_decode(buf, md); 796fefb6a14SPawel Jakub Dawidek if (error != 0) 797fefb6a14SPawel Jakub Dawidek goto end; 798fefb6a14SPawel Jakub Dawidek /* Metadata was read and decoded successfully. */ 799c58794deSPawel Jakub Dawidek end: 800c58794deSPawel Jakub Dawidek g_free(buf); 801c58794deSPawel Jakub Dawidek if (cp->provider != NULL) { 802c58794deSPawel Jakub Dawidek if (cp->acr == 1) 803c58794deSPawel Jakub Dawidek g_access(cp, -1, 0, 0); 804c58794deSPawel Jakub Dawidek g_detach(cp); 805c58794deSPawel Jakub Dawidek } 806c58794deSPawel Jakub Dawidek g_destroy_consumer(cp); 807c58794deSPawel Jakub Dawidek g_destroy_geom(gp); 808c58794deSPawel Jakub Dawidek return (error); 809c58794deSPawel Jakub Dawidek } 810c58794deSPawel Jakub Dawidek 8112f07cdf8SPawel Jakub Dawidek int 8122f07cdf8SPawel Jakub Dawidek g_eli_read_metadata(struct g_class *mp, struct g_provider *pp, 8132f07cdf8SPawel Jakub Dawidek struct g_eli_metadata *md) 8142f07cdf8SPawel Jakub Dawidek { 8152f07cdf8SPawel Jakub Dawidek 8162f07cdf8SPawel Jakub Dawidek return (g_eli_read_metadata_offset(mp, pp, 8172f07cdf8SPawel Jakub Dawidek pp->mediasize - pp->sectorsize, md)); 8182f07cdf8SPawel Jakub Dawidek } 8192f07cdf8SPawel Jakub Dawidek 820c58794deSPawel Jakub Dawidek /* 821c58794deSPawel Jakub Dawidek * The function is called when we had last close on provider and user requested 822c58794deSPawel Jakub Dawidek * to close it when this situation occur. 823c58794deSPawel Jakub Dawidek */ 824c58794deSPawel Jakub Dawidek static void 82519351a14SAlexander Motin g_eli_last_close(void *arg, int flags __unused) 826c58794deSPawel Jakub Dawidek { 827c58794deSPawel Jakub Dawidek struct g_geom *gp; 82819351a14SAlexander Motin char gpname[64]; 829c9048120SMateusz Guzik int error __diagused; 830c58794deSPawel Jakub Dawidek 831c58794deSPawel Jakub Dawidek g_topology_assert(); 83219351a14SAlexander Motin gp = arg; 83319351a14SAlexander Motin strlcpy(gpname, gp->name, sizeof(gpname)); 83419351a14SAlexander Motin error = g_eli_destroy(gp->softc, TRUE); 835c58794deSPawel Jakub Dawidek KASSERT(error == 0, ("Cannot detach %s on last close (error=%d).", 83619351a14SAlexander Motin gpname, error)); 83719351a14SAlexander Motin G_ELI_DEBUG(0, "Detached %s on last close.", gpname); 838c58794deSPawel Jakub Dawidek } 839c58794deSPawel Jakub Dawidek 840c58794deSPawel Jakub Dawidek int 841c58794deSPawel Jakub Dawidek g_eli_access(struct g_provider *pp, int dr, int dw, int de) 842c58794deSPawel Jakub Dawidek { 843c58794deSPawel Jakub Dawidek struct g_eli_softc *sc; 844c58794deSPawel Jakub Dawidek struct g_geom *gp; 845c58794deSPawel Jakub Dawidek 846c58794deSPawel Jakub Dawidek gp = pp->geom; 847c58794deSPawel Jakub Dawidek sc = gp->softc; 848c58794deSPawel Jakub Dawidek 849c58794deSPawel Jakub Dawidek if (dw > 0) { 85085059016SPawel Jakub Dawidek if (sc->sc_flags & G_ELI_FLAG_RO) { 85185059016SPawel Jakub Dawidek /* Deny write attempts. */ 85285059016SPawel Jakub Dawidek return (EROFS); 85385059016SPawel Jakub Dawidek } 854c58794deSPawel Jakub Dawidek /* Someone is opening us for write, we need to remember that. */ 855c58794deSPawel Jakub Dawidek sc->sc_flags |= G_ELI_FLAG_WOPEN; 856c58794deSPawel Jakub Dawidek return (0); 857c58794deSPawel Jakub Dawidek } 858c58794deSPawel Jakub Dawidek /* Is this the last close? */ 859c58794deSPawel Jakub Dawidek if (pp->acr + dr > 0 || pp->acw + dw > 0 || pp->ace + de > 0) 860c58794deSPawel Jakub Dawidek return (0); 861c58794deSPawel Jakub Dawidek 862c58794deSPawel Jakub Dawidek /* 863c58794deSPawel Jakub Dawidek * Automatically detach on last close if requested. 864c58794deSPawel Jakub Dawidek */ 865c58794deSPawel Jakub Dawidek if ((sc->sc_flags & G_ELI_FLAG_RW_DETACH) || 866c58794deSPawel Jakub Dawidek (sc->sc_flags & G_ELI_FLAG_WOPEN)) { 86719351a14SAlexander Motin g_post_event(g_eli_last_close, gp, M_WAITOK, NULL); 868c58794deSPawel Jakub Dawidek } 869c58794deSPawel Jakub Dawidek return (0); 870c58794deSPawel Jakub Dawidek } 871c58794deSPawel Jakub Dawidek 872eba8f137SPawel Jakub Dawidek static int 873eba8f137SPawel Jakub Dawidek g_eli_cpu_is_disabled(int cpu) 874eba8f137SPawel Jakub Dawidek { 875eba8f137SPawel Jakub Dawidek #ifdef SMP 87671a19bdcSAttilio Rao return (CPU_ISSET(cpu, &hlt_cpus_mask)); 877eba8f137SPawel Jakub Dawidek #else 878eba8f137SPawel Jakub Dawidek return (0); 879eba8f137SPawel Jakub Dawidek #endif 880eba8f137SPawel Jakub Dawidek } 881eba8f137SPawel Jakub Dawidek 8822dbc9a38SGleb Smirnoff static void 8832dbc9a38SGleb Smirnoff g_eli_init_uma(void) 8842dbc9a38SGleb Smirnoff { 8852dbc9a38SGleb Smirnoff 8862dbc9a38SGleb Smirnoff atomic_add_int(&g_eli_devs, 1); 8872dbc9a38SGleb Smirnoff sx_xlock(&g_eli_umalock); 8882dbc9a38SGleb Smirnoff if (g_eli_uma == NULL) { 8892dbc9a38SGleb Smirnoff /* 8902dbc9a38SGleb Smirnoff * Calculate the maximum-sized swap buffer we are 8912dbc9a38SGleb Smirnoff * likely to see. 8922dbc9a38SGleb Smirnoff */ 8932dbc9a38SGleb Smirnoff g_eli_alloc_sz = roundup2((PAGE_SIZE + sizeof(int) + 8942dbc9a38SGleb Smirnoff G_ELI_AUTH_SECKEYLEN) * nsw_cluster_max + 8952dbc9a38SGleb Smirnoff sizeof(uintptr_t), PAGE_SIZE); 8962dbc9a38SGleb Smirnoff 8972dbc9a38SGleb Smirnoff g_eli_uma = uma_zcreate("GELI buffers", g_eli_alloc_sz, 898b984d153SGleb Smirnoff NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 8992dbc9a38SGleb Smirnoff 9002dbc9a38SGleb Smirnoff /* Reserve and pre-allocate pages, as appropriate. */ 9012dbc9a38SGleb Smirnoff uma_zone_reserve(g_eli_uma, g_eli_minbufs); 9022dbc9a38SGleb Smirnoff uma_prealloc(g_eli_uma, g_eli_minbufs); 9032dbc9a38SGleb Smirnoff } 9042dbc9a38SGleb Smirnoff sx_xunlock(&g_eli_umalock); 9052dbc9a38SGleb Smirnoff } 9062dbc9a38SGleb Smirnoff 9072dbc9a38SGleb Smirnoff /* 9082dbc9a38SGleb Smirnoff * Try to destroy the UMA pool. This will do nothing if there are existing 9092dbc9a38SGleb Smirnoff * GELI devices or existing UMA allocations. 9102dbc9a38SGleb Smirnoff */ 9112dbc9a38SGleb Smirnoff static void 9122dbc9a38SGleb Smirnoff g_eli_destroy_uma(void) 9132dbc9a38SGleb Smirnoff { 9142dbc9a38SGleb Smirnoff uma_zone_t oldzone; 9152dbc9a38SGleb Smirnoff 9162dbc9a38SGleb Smirnoff sx_xlock(&g_eli_umalock); 9172dbc9a38SGleb Smirnoff /* Ensure we really should be destroying this. */ 9182dbc9a38SGleb Smirnoff if (atomic_load_int(&g_eli_devs) == 0 && 9192dbc9a38SGleb Smirnoff atomic_load_int(&g_eli_umaoutstanding) == 0) { 9202dbc9a38SGleb Smirnoff oldzone = g_eli_uma; 9212dbc9a38SGleb Smirnoff g_eli_uma = NULL; 9222dbc9a38SGleb Smirnoff } else 9232dbc9a38SGleb Smirnoff oldzone = NULL; 9242dbc9a38SGleb Smirnoff sx_xunlock(&g_eli_umalock); 9252dbc9a38SGleb Smirnoff 9262dbc9a38SGleb Smirnoff if (oldzone != NULL) 9272dbc9a38SGleb Smirnoff uma_zdestroy(oldzone); 9282dbc9a38SGleb Smirnoff } 9292dbc9a38SGleb Smirnoff 9302dbc9a38SGleb Smirnoff static void 9312dbc9a38SGleb Smirnoff g_eli_fini_uma(void) 9322dbc9a38SGleb Smirnoff { 9332dbc9a38SGleb Smirnoff 9342dbc9a38SGleb Smirnoff /* 9352dbc9a38SGleb Smirnoff * If this is the last outstanding GELI device, try to 9362dbc9a38SGleb Smirnoff * destroy the UMA pool. 9372dbc9a38SGleb Smirnoff */ 9382dbc9a38SGleb Smirnoff if (atomic_fetchadd_int(&g_eli_devs, -1) == 1) 9392dbc9a38SGleb Smirnoff g_eli_destroy_uma(); 9402dbc9a38SGleb Smirnoff } 9412dbc9a38SGleb Smirnoff 9422dbc9a38SGleb Smirnoff /* 9432dbc9a38SGleb Smirnoff * Allocate a data buffer. If the size fits within our swap-sized buffers, 9442dbc9a38SGleb Smirnoff * try to allocate a swap-sized buffer from the UMA pool. Otherwise, fall 9452dbc9a38SGleb Smirnoff * back to using malloc. 9462dbc9a38SGleb Smirnoff * 9472dbc9a38SGleb Smirnoff * Swap-related requests are special: they can only use the UMA pool, they 9482dbc9a38SGleb Smirnoff * use M_USE_RESERVE to let them dip farther into system resources, and 9492dbc9a38SGleb Smirnoff * they always use M_NOWAIT to prevent swap operations from deadlocking. 9502dbc9a38SGleb Smirnoff */ 9512dbc9a38SGleb Smirnoff bool 9522dbc9a38SGleb Smirnoff g_eli_alloc_data(struct bio *bp, int sz) 9532dbc9a38SGleb Smirnoff { 9542dbc9a38SGleb Smirnoff 9552dbc9a38SGleb Smirnoff KASSERT(sz <= g_eli_alloc_sz || (bp->bio_flags & BIO_SWAP) == 0, 9562dbc9a38SGleb Smirnoff ("BIO_SWAP request for %d bytes exceeds the precalculated buffer" 9572dbc9a38SGleb Smirnoff " size (%d)", sz, g_eli_alloc_sz)); 9582dbc9a38SGleb Smirnoff if (sz <= g_eli_alloc_sz) { 9592dbc9a38SGleb Smirnoff bp->bio_driver2 = uma_zalloc(g_eli_uma, M_NOWAIT | 9602dbc9a38SGleb Smirnoff ((bp->bio_flags & BIO_SWAP) != 0 ? M_USE_RESERVE : 0)); 9612dbc9a38SGleb Smirnoff if (bp->bio_driver2 != NULL) { 9622dbc9a38SGleb Smirnoff bp->bio_pflags |= G_ELI_UMA_ALLOC; 9632dbc9a38SGleb Smirnoff atomic_add_int(&g_eli_umaoutstanding, 1); 9642dbc9a38SGleb Smirnoff } 9652dbc9a38SGleb Smirnoff if (bp->bio_driver2 != NULL || (bp->bio_flags & BIO_SWAP) != 0) 9662dbc9a38SGleb Smirnoff return (bp->bio_driver2 != NULL); 9672dbc9a38SGleb Smirnoff } 9682dbc9a38SGleb Smirnoff bp->bio_pflags &= ~(G_ELI_UMA_ALLOC); 9692dbc9a38SGleb Smirnoff bp->bio_driver2 = malloc(sz, M_ELI, g_eli_blocking_malloc ? M_WAITOK : 9702dbc9a38SGleb Smirnoff M_NOWAIT); 9712dbc9a38SGleb Smirnoff return (bp->bio_driver2 != NULL); 9722dbc9a38SGleb Smirnoff } 9732dbc9a38SGleb Smirnoff 9742dbc9a38SGleb Smirnoff /* 9752dbc9a38SGleb Smirnoff * Free a buffer from bp->bio_driver2 which was allocated with 9762dbc9a38SGleb Smirnoff * g_eli_alloc_data(). This function makes sure that the memory is freed 9772dbc9a38SGleb Smirnoff * to the correct place. 9782dbc9a38SGleb Smirnoff * 9792dbc9a38SGleb Smirnoff * Additionally, if this function frees the last outstanding UMA request 9802dbc9a38SGleb Smirnoff * and there are no open GELI devices, this will destroy the UMA pool. 9812dbc9a38SGleb Smirnoff */ 9822dbc9a38SGleb Smirnoff void 9832dbc9a38SGleb Smirnoff g_eli_free_data(struct bio *bp) 9842dbc9a38SGleb Smirnoff { 9852dbc9a38SGleb Smirnoff 9862dbc9a38SGleb Smirnoff /* 9872dbc9a38SGleb Smirnoff * Mimic the free(9) behavior of allowing a NULL pointer to be 9882dbc9a38SGleb Smirnoff * freed. 9892dbc9a38SGleb Smirnoff */ 9902dbc9a38SGleb Smirnoff if (bp->bio_driver2 == NULL) 9912dbc9a38SGleb Smirnoff return; 9922dbc9a38SGleb Smirnoff 9932dbc9a38SGleb Smirnoff if ((bp->bio_pflags & G_ELI_UMA_ALLOC) != 0) { 9942dbc9a38SGleb Smirnoff uma_zfree(g_eli_uma, bp->bio_driver2); 9952dbc9a38SGleb Smirnoff if (atomic_fetchadd_int(&g_eli_umaoutstanding, -1) == 1 && 9962dbc9a38SGleb Smirnoff atomic_load_int(&g_eli_devs) == 0) 9972dbc9a38SGleb Smirnoff g_eli_destroy_uma(); 9982dbc9a38SGleb Smirnoff } else 9992dbc9a38SGleb Smirnoff free(bp->bio_driver2, M_ELI); 10002dbc9a38SGleb Smirnoff bp->bio_driver2 = NULL; 10012dbc9a38SGleb Smirnoff } 10022dbc9a38SGleb Smirnoff 1003c58794deSPawel Jakub Dawidek struct g_geom * 1004c58794deSPawel Jakub Dawidek g_eli_create(struct gctl_req *req, struct g_class *mp, struct g_provider *bpp, 1005c58794deSPawel Jakub Dawidek const struct g_eli_metadata *md, const u_char *mkey, int nkey) 1006c58794deSPawel Jakub Dawidek { 1007c58794deSPawel Jakub Dawidek struct g_eli_softc *sc; 1008c58794deSPawel Jakub Dawidek struct g_eli_worker *wr; 1009c58794deSPawel Jakub Dawidek struct g_geom *gp; 1010c58794deSPawel Jakub Dawidek struct g_provider *pp; 1011c58794deSPawel Jakub Dawidek struct g_consumer *cp; 1012ae1cce52SWarner Losh struct g_geom_alias *gap; 1013c58794deSPawel Jakub Dawidek u_int i, threads; 10143bb6e0f0SRyan Libby int dcw, error; 1015c58794deSPawel Jakub Dawidek 1016c58794deSPawel Jakub Dawidek G_ELI_DEBUG(1, "Creating device %s%s.", bpp->name, G_ELI_SUFFIX); 1017e2b99193SJohn Baldwin KASSERT(eli_metadata_crypto_supported(md), 1018e2b99193SJohn Baldwin ("%s: unsupported crypto for %s", __func__, bpp->name)); 1019c58794deSPawel Jakub Dawidek 1020c58794deSPawel Jakub Dawidek gp = g_new_geomf(mp, "%s%s", bpp->name, G_ELI_SUFFIX); 1021c58794deSPawel Jakub Dawidek sc = malloc(sizeof(*sc), M_ELI, M_WAITOK | M_ZERO); 1022c58794deSPawel Jakub Dawidek gp->start = g_eli_start; 1023c58794deSPawel Jakub Dawidek /* 10244273d412SPawel Jakub Dawidek * Spoiling can happen even though we have the provider open 10254273d412SPawel Jakub Dawidek * exclusively, e.g. through media change events. 1026c58794deSPawel Jakub Dawidek */ 10274273d412SPawel Jakub Dawidek gp->spoiled = g_eli_orphan; 1028c58794deSPawel Jakub Dawidek gp->orphan = g_eli_orphan; 10292f07cdf8SPawel Jakub Dawidek gp->resize = g_eli_resize; 103085059016SPawel Jakub Dawidek gp->dumpconf = g_eli_dumpconf; 1031c58794deSPawel Jakub Dawidek /* 103285059016SPawel Jakub Dawidek * If detach-on-last-close feature is not enabled and we don't operate 103385059016SPawel Jakub Dawidek * on read-only provider, we can simply use g_std_access(). 1034c58794deSPawel Jakub Dawidek */ 103585059016SPawel Jakub Dawidek if (md->md_flags & (G_ELI_FLAG_WO_DETACH | G_ELI_FLAG_RO)) 1036c58794deSPawel Jakub Dawidek gp->access = g_eli_access; 1037c58794deSPawel Jakub Dawidek else 1038c58794deSPawel Jakub Dawidek gp->access = g_std_access; 1039c58794deSPawel Jakub Dawidek 10404332fecaSAllan Jude eli_metadata_softc(sc, md, bpp->sectorsize, bpp->mediasize); 1041c58794deSPawel Jakub Dawidek sc->sc_nkey = nkey; 1042eaa3b919SPawel Jakub Dawidek 1043c58794deSPawel Jakub Dawidek gp->softc = sc; 1044c58794deSPawel Jakub Dawidek sc->sc_geom = gp; 1045c58794deSPawel Jakub Dawidek 1046c58794deSPawel Jakub Dawidek bioq_init(&sc->sc_queue); 1047c58794deSPawel Jakub Dawidek mtx_init(&sc->sc_queue_mtx, "geli:queue", NULL, MTX_DEF); 10481e09ff3dSPawel Jakub Dawidek mtx_init(&sc->sc_ekeys_lock, "geli:ekeys", NULL, MTX_DEF); 1049c58794deSPawel Jakub Dawidek 1050c58794deSPawel Jakub Dawidek pp = NULL; 1051c58794deSPawel Jakub Dawidek cp = g_new_consumer(gp); 10526f818c1fSAlan Somers cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE; 1053c58794deSPawel Jakub Dawidek error = g_attach(cp, bpp); 1054c58794deSPawel Jakub Dawidek if (error != 0) { 1055c58794deSPawel Jakub Dawidek if (req != NULL) { 1056c58794deSPawel Jakub Dawidek gctl_error(req, "Cannot attach to %s (error=%d).", 1057c58794deSPawel Jakub Dawidek bpp->name, error); 1058c58794deSPawel Jakub Dawidek } else { 1059c58794deSPawel Jakub Dawidek G_ELI_DEBUG(1, "Cannot attach to %s (error=%d).", 1060c58794deSPawel Jakub Dawidek bpp->name, error); 1061c58794deSPawel Jakub Dawidek } 1062c58794deSPawel Jakub Dawidek goto failed; 1063c58794deSPawel Jakub Dawidek } 1064c58794deSPawel Jakub Dawidek /* 1065c58794deSPawel Jakub Dawidek * Keep provider open all the time, so we can run critical tasks, 1066c58794deSPawel Jakub Dawidek * like Master Keys deletion, without wondering if we can open 1067c58794deSPawel Jakub Dawidek * provider or not. 106885059016SPawel Jakub Dawidek * We don't open provider for writing only when user requested read-only 106985059016SPawel Jakub Dawidek * access. 1070c58794deSPawel Jakub Dawidek */ 10713bb6e0f0SRyan Libby dcw = (sc->sc_flags & G_ELI_FLAG_RO) ? 0 : 1; 10723bb6e0f0SRyan Libby error = g_access(cp, 1, dcw, 1); 1073c58794deSPawel Jakub Dawidek if (error != 0) { 1074c58794deSPawel Jakub Dawidek if (req != NULL) { 1075c58794deSPawel Jakub Dawidek gctl_error(req, "Cannot access %s (error=%d).", 1076c58794deSPawel Jakub Dawidek bpp->name, error); 1077c58794deSPawel Jakub Dawidek } else { 1078c58794deSPawel Jakub Dawidek G_ELI_DEBUG(1, "Cannot access %s (error=%d).", 1079c58794deSPawel Jakub Dawidek bpp->name, error); 1080c58794deSPawel Jakub Dawidek } 1081c58794deSPawel Jakub Dawidek goto failed; 1082c58794deSPawel Jakub Dawidek } 1083c58794deSPawel Jakub Dawidek 1084c6a26d4cSPawel Jakub Dawidek /* 1085c6a26d4cSPawel Jakub Dawidek * Remember the keys in our softc structure. 1086c6a26d4cSPawel Jakub Dawidek */ 1087c6a26d4cSPawel Jakub Dawidek g_eli_mkey_propagate(sc, mkey); 1088c6a26d4cSPawel Jakub Dawidek 1089c58794deSPawel Jakub Dawidek LIST_INIT(&sc->sc_workers); 1090c58794deSPawel Jakub Dawidek 1091c58794deSPawel Jakub Dawidek threads = g_eli_threads; 1092dd549194SPawel Jakub Dawidek if (threads == 0) 1093dd549194SPawel Jakub Dawidek threads = mp_ncpus; 10940c879bd9SPawel Jakub Dawidek sc->sc_cpubind = (mp_ncpus > 1 && threads == mp_ncpus); 10952dbc9a38SGleb Smirnoff g_eli_init_uma(); 1096c58794deSPawel Jakub Dawidek for (i = 0; i < threads; i++) { 1097eba8f137SPawel Jakub Dawidek if (g_eli_cpu_is_disabled(i)) { 1098eba8f137SPawel Jakub Dawidek G_ELI_DEBUG(1, "%s: CPU %u disabled, skipping.", 10991506db21SPawel Jakub Dawidek bpp->name, i); 1100eba8f137SPawel Jakub Dawidek continue; 1101eba8f137SPawel Jakub Dawidek } 1102c58794deSPawel Jakub Dawidek wr = malloc(sizeof(*wr), M_ELI, M_WAITOK | M_ZERO); 1103c58794deSPawel Jakub Dawidek wr->w_softc = sc; 1104c58794deSPawel Jakub Dawidek wr->w_number = i; 11055ad4a7c7SPawel Jakub Dawidek wr->w_active = TRUE; 1106c58794deSPawel Jakub Dawidek 11073ac01bc2SPawel Jakub Dawidek error = g_eli_newsession(wr); 1108c58794deSPawel Jakub Dawidek if (error != 0) { 1109c58794deSPawel Jakub Dawidek free(wr, M_ELI); 1110c58794deSPawel Jakub Dawidek if (req != NULL) { 1111c58794deSPawel Jakub Dawidek gctl_error(req, "Cannot set up crypto session " 1112c58794deSPawel Jakub Dawidek "for %s (error=%d).", bpp->name, error); 1113c58794deSPawel Jakub Dawidek } else { 1114c58794deSPawel Jakub Dawidek G_ELI_DEBUG(1, "Cannot set up crypto session " 1115c58794deSPawel Jakub Dawidek "for %s (error=%d).", bpp->name, error); 1116c58794deSPawel Jakub Dawidek } 1117c58794deSPawel Jakub Dawidek goto failed; 1118c58794deSPawel Jakub Dawidek } 1119c58794deSPawel Jakub Dawidek 11203745c395SJulian Elischer error = kproc_create(g_eli_worker, wr, &wr->w_proc, 0, 0, 1121c58794deSPawel Jakub Dawidek "g_eli[%u] %s", i, bpp->name); 1122c58794deSPawel Jakub Dawidek if (error != 0) { 11233ac01bc2SPawel Jakub Dawidek g_eli_freesession(wr); 1124c58794deSPawel Jakub Dawidek free(wr, M_ELI); 1125c58794deSPawel Jakub Dawidek if (req != NULL) { 1126dddd1d53SPawel Jakub Dawidek gctl_error(req, "Cannot create kernel thread " 1127dddd1d53SPawel Jakub Dawidek "for %s (error=%d).", bpp->name, error); 1128c58794deSPawel Jakub Dawidek } else { 1129dddd1d53SPawel Jakub Dawidek G_ELI_DEBUG(1, "Cannot create kernel thread " 1130dddd1d53SPawel Jakub Dawidek "for %s (error=%d).", bpp->name, error); 1131c58794deSPawel Jakub Dawidek } 1132c58794deSPawel Jakub Dawidek goto failed; 1133c58794deSPawel Jakub Dawidek } 1134c58794deSPawel Jakub Dawidek LIST_INSERT_HEAD(&sc->sc_workers, wr, w_next); 1135c58794deSPawel Jakub Dawidek } 1136c58794deSPawel Jakub Dawidek 1137c58794deSPawel Jakub Dawidek /* 1138c58794deSPawel Jakub Dawidek * Create decrypted provider. 1139c58794deSPawel Jakub Dawidek */ 1140c58794deSPawel Jakub Dawidek pp = g_new_providerf(gp, "%s%s", bpp->name, G_ELI_SUFFIX); 11416f818c1fSAlan Somers pp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE; 1142081b4452SMark Johnston if (g_eli_unmapped_io && CRYPTO_HAS_VMPAGE) { 11437d874f0fSAlan Somers /* 11447d874f0fSAlan Somers * On DMAP architectures we can use unmapped I/O. But don't 11457d874f0fSAlan Somers * use it with data integrity verification. That code hasn't 11467d874f0fSAlan Somers * been written yet. 11477d874f0fSAlan Somers */ 11487d874f0fSAlan Somers if ((sc->sc_flags & G_ELI_FLAG_AUTH) == 0) 11497d874f0fSAlan Somers pp->flags |= G_PF_ACCEPT_UNMAPPED; 11507d874f0fSAlan Somers } 1151c6a26d4cSPawel Jakub Dawidek pp->mediasize = sc->sc_mediasize; 1152c6a26d4cSPawel Jakub Dawidek pp->sectorsize = sc->sc_sectorsize; 1153ae1cce52SWarner Losh LIST_FOREACH(gap, &bpp->aliases, ga_next) 1154ae1cce52SWarner Losh g_provider_add_alias(pp, "%s%s", gap->ga_alias, G_ELI_SUFFIX); 1155eaa3b919SPawel Jakub Dawidek 1156c58794deSPawel Jakub Dawidek g_error_provider(pp, 0); 1157c58794deSPawel Jakub Dawidek 1158c58794deSPawel Jakub Dawidek G_ELI_DEBUG(0, "Device %s created.", pp->name); 1159eaa3b919SPawel Jakub Dawidek G_ELI_DEBUG(0, "Encryption: %s %u", g_eli_algo2str(sc->sc_ealgo), 1160eaa3b919SPawel Jakub Dawidek sc->sc_ekeylen); 1161e2b99193SJohn Baldwin if (sc->sc_flags & G_ELI_FLAG_AUTH) 1162eaa3b919SPawel Jakub Dawidek G_ELI_DEBUG(0, " Integrity: %s", g_eli_algo2str(sc->sc_aalgo)); 1163c58794deSPawel Jakub Dawidek G_ELI_DEBUG(0, " Crypto: %s", 1164a3d565a1SJohn Baldwin sc->sc_crypto == G_ELI_CRYPTO_SW_ACCEL ? "accelerated software" : 1165c58794deSPawel Jakub Dawidek sc->sc_crypto == G_ELI_CRYPTO_SW ? "software" : "hardware"); 1166c58794deSPawel Jakub Dawidek return (gp); 1167c58794deSPawel Jakub Dawidek failed: 1168c58794deSPawel Jakub Dawidek mtx_lock(&sc->sc_queue_mtx); 1169c58794deSPawel Jakub Dawidek sc->sc_flags |= G_ELI_FLAG_DESTROY; 1170c58794deSPawel Jakub Dawidek wakeup(sc); 1171c58794deSPawel Jakub Dawidek /* 1172c58794deSPawel Jakub Dawidek * Wait for kernel threads self destruction. 1173c58794deSPawel Jakub Dawidek */ 1174c58794deSPawel Jakub Dawidek while (!LIST_EMPTY(&sc->sc_workers)) { 1175c58794deSPawel Jakub Dawidek msleep(&sc->sc_workers, &sc->sc_queue_mtx, PRIBIO, 1176c58794deSPawel Jakub Dawidek "geli:destroy", 0); 1177c58794deSPawel Jakub Dawidek } 1178c58794deSPawel Jakub Dawidek mtx_destroy(&sc->sc_queue_mtx); 1179c58794deSPawel Jakub Dawidek if (cp->provider != NULL) { 1180c58794deSPawel Jakub Dawidek if (cp->acr == 1) 11813bb6e0f0SRyan Libby g_access(cp, -1, -dcw, -1); 1182c58794deSPawel Jakub Dawidek g_detach(cp); 1183c58794deSPawel Jakub Dawidek } 1184c58794deSPawel Jakub Dawidek g_destroy_consumer(cp); 1185c58794deSPawel Jakub Dawidek g_destroy_geom(gp); 11861e09ff3dSPawel Jakub Dawidek g_eli_key_destroy(sc); 11872dbc9a38SGleb Smirnoff g_eli_fini_uma(); 1188b172f23dSJohn Baldwin zfree(sc, M_ELI); 1189c58794deSPawel Jakub Dawidek return (NULL); 1190c58794deSPawel Jakub Dawidek } 1191c58794deSPawel Jakub Dawidek 1192c58794deSPawel Jakub Dawidek int 1193c58794deSPawel Jakub Dawidek g_eli_destroy(struct g_eli_softc *sc, boolean_t force) 1194c58794deSPawel Jakub Dawidek { 1195c58794deSPawel Jakub Dawidek struct g_geom *gp; 1196c58794deSPawel Jakub Dawidek struct g_provider *pp; 1197c58794deSPawel Jakub Dawidek 1198c58794deSPawel Jakub Dawidek g_topology_assert(); 1199c58794deSPawel Jakub Dawidek 1200c58794deSPawel Jakub Dawidek if (sc == NULL) 1201c58794deSPawel Jakub Dawidek return (ENXIO); 1202c58794deSPawel Jakub Dawidek 1203c58794deSPawel Jakub Dawidek gp = sc->sc_geom; 1204c58794deSPawel Jakub Dawidek pp = LIST_FIRST(&gp->provider); 1205c58794deSPawel Jakub Dawidek if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) { 1206c58794deSPawel Jakub Dawidek if (force) { 1207c58794deSPawel Jakub Dawidek G_ELI_DEBUG(1, "Device %s is still open, so it " 120898645006SChristian Brueffer "cannot be definitely removed.", pp->name); 120919351a14SAlexander Motin sc->sc_flags |= G_ELI_FLAG_RW_DETACH; 121019351a14SAlexander Motin gp->access = g_eli_access; 121119351a14SAlexander Motin g_wither_provider(pp, ENXIO); 121219351a14SAlexander Motin return (EBUSY); 1213c58794deSPawel Jakub Dawidek } else { 1214c58794deSPawel Jakub Dawidek G_ELI_DEBUG(1, 1215c58794deSPawel Jakub Dawidek "Device %s is still open (r%dw%de%d).", pp->name, 1216c58794deSPawel Jakub Dawidek pp->acr, pp->acw, pp->ace); 1217c58794deSPawel Jakub Dawidek return (EBUSY); 1218c58794deSPawel Jakub Dawidek } 1219c58794deSPawel Jakub Dawidek } 1220c58794deSPawel Jakub Dawidek 1221c58794deSPawel Jakub Dawidek mtx_lock(&sc->sc_queue_mtx); 1222c58794deSPawel Jakub Dawidek sc->sc_flags |= G_ELI_FLAG_DESTROY; 1223c58794deSPawel Jakub Dawidek wakeup(sc); 1224c58794deSPawel Jakub Dawidek while (!LIST_EMPTY(&sc->sc_workers)) { 1225c58794deSPawel Jakub Dawidek msleep(&sc->sc_workers, &sc->sc_queue_mtx, PRIBIO, 1226c58794deSPawel Jakub Dawidek "geli:destroy", 0); 1227c58794deSPawel Jakub Dawidek } 1228c58794deSPawel Jakub Dawidek mtx_destroy(&sc->sc_queue_mtx); 1229c58794deSPawel Jakub Dawidek gp->softc = NULL; 12301e09ff3dSPawel Jakub Dawidek g_eli_key_destroy(sc); 12312dbc9a38SGleb Smirnoff g_eli_fini_uma(); 1232b172f23dSJohn Baldwin zfree(sc, M_ELI); 1233c58794deSPawel Jakub Dawidek 1234c58794deSPawel Jakub Dawidek G_ELI_DEBUG(0, "Device %s destroyed.", gp->name); 1235c58794deSPawel Jakub Dawidek g_wither_geom_close(gp, ENXIO); 1236c58794deSPawel Jakub Dawidek 1237c58794deSPawel Jakub Dawidek return (0); 1238c58794deSPawel Jakub Dawidek } 1239c58794deSPawel Jakub Dawidek 1240c58794deSPawel Jakub Dawidek static int 1241c58794deSPawel Jakub Dawidek g_eli_destroy_geom(struct gctl_req *req __unused, 1242c58794deSPawel Jakub Dawidek struct g_class *mp __unused, struct g_geom *gp) 1243c58794deSPawel Jakub Dawidek { 1244c58794deSPawel Jakub Dawidek struct g_eli_softc *sc; 1245c58794deSPawel Jakub Dawidek 1246c58794deSPawel Jakub Dawidek sc = gp->softc; 12475ad4a7c7SPawel Jakub Dawidek return (g_eli_destroy(sc, FALSE)); 1248c58794deSPawel Jakub Dawidek } 1249c58794deSPawel Jakub Dawidek 12509af2131bSPawel Jakub Dawidek static int 12519af2131bSPawel Jakub Dawidek g_eli_keyfiles_load(struct hmac_ctx *ctx, const char *provider) 12529af2131bSPawel Jakub Dawidek { 12531e189c08SMarcel Moolenaar u_char *keyfile, *data; 12549af2131bSPawel Jakub Dawidek char *file, name[64]; 12551e189c08SMarcel Moolenaar size_t size; 12569af2131bSPawel Jakub Dawidek int i; 12579af2131bSPawel Jakub Dawidek 12589af2131bSPawel Jakub Dawidek for (i = 0; ; i++) { 12599af2131bSPawel Jakub Dawidek snprintf(name, sizeof(name), "%s:geli_keyfile%d", provider, i); 12609af2131bSPawel Jakub Dawidek keyfile = preload_search_by_type(name); 1261edaa9008SPawel Jakub Dawidek if (keyfile == NULL && i == 0) { 1262edaa9008SPawel Jakub Dawidek /* 1263edaa9008SPawel Jakub Dawidek * If there is only one keyfile, allow simpler name. 1264edaa9008SPawel Jakub Dawidek */ 1265edaa9008SPawel Jakub Dawidek snprintf(name, sizeof(name), "%s:geli_keyfile", provider); 1266edaa9008SPawel Jakub Dawidek keyfile = preload_search_by_type(name); 1267edaa9008SPawel Jakub Dawidek } 12689af2131bSPawel Jakub Dawidek if (keyfile == NULL) 12699af2131bSPawel Jakub Dawidek return (i); /* Return number of loaded keyfiles. */ 12701e189c08SMarcel Moolenaar data = preload_fetch_addr(keyfile); 12719af2131bSPawel Jakub Dawidek if (data == NULL) { 12729af2131bSPawel Jakub Dawidek G_ELI_DEBUG(0, "Cannot find key file data for %s.", 12739af2131bSPawel Jakub Dawidek name); 12749af2131bSPawel Jakub Dawidek return (0); 12759af2131bSPawel Jakub Dawidek } 12761e189c08SMarcel Moolenaar size = preload_fetch_size(keyfile); 12771e189c08SMarcel Moolenaar if (size == 0) { 12789af2131bSPawel Jakub Dawidek G_ELI_DEBUG(0, "Cannot find key file size for %s.", 12799af2131bSPawel Jakub Dawidek name); 12809af2131bSPawel Jakub Dawidek return (0); 12819af2131bSPawel Jakub Dawidek } 12829af2131bSPawel Jakub Dawidek file = preload_search_info(keyfile, MODINFO_NAME); 12839af2131bSPawel Jakub Dawidek if (file == NULL) { 12849af2131bSPawel Jakub Dawidek G_ELI_DEBUG(0, "Cannot find key file name for %s.", 12859af2131bSPawel Jakub Dawidek name); 12869af2131bSPawel Jakub Dawidek return (0); 12879af2131bSPawel Jakub Dawidek } 12889af2131bSPawel Jakub Dawidek G_ELI_DEBUG(1, "Loaded keyfile %s for %s (type: %s).", file, 12899af2131bSPawel Jakub Dawidek provider, name); 12901e189c08SMarcel Moolenaar g_eli_crypto_hmac_update(ctx, data, size); 12919af2131bSPawel Jakub Dawidek } 12929af2131bSPawel Jakub Dawidek } 12939af2131bSPawel Jakub Dawidek 12949af2131bSPawel Jakub Dawidek static void 12959af2131bSPawel Jakub Dawidek g_eli_keyfiles_clear(const char *provider) 12969af2131bSPawel Jakub Dawidek { 12971e189c08SMarcel Moolenaar u_char *keyfile, *data; 12989af2131bSPawel Jakub Dawidek char name[64]; 12991e189c08SMarcel Moolenaar size_t size; 13009af2131bSPawel Jakub Dawidek int i; 13019af2131bSPawel Jakub Dawidek 13029af2131bSPawel Jakub Dawidek for (i = 0; ; i++) { 13039af2131bSPawel Jakub Dawidek snprintf(name, sizeof(name), "%s:geli_keyfile%d", provider, i); 13049af2131bSPawel Jakub Dawidek keyfile = preload_search_by_type(name); 13059af2131bSPawel Jakub Dawidek if (keyfile == NULL) 13069af2131bSPawel Jakub Dawidek return; 13071e189c08SMarcel Moolenaar data = preload_fetch_addr(keyfile); 13081e189c08SMarcel Moolenaar size = preload_fetch_size(keyfile); 13091e189c08SMarcel Moolenaar if (data != NULL && size != 0) 13106572e5ffSJohn Baldwin explicit_bzero(data, size); 13119af2131bSPawel Jakub Dawidek } 13129af2131bSPawel Jakub Dawidek } 13139af2131bSPawel Jakub Dawidek 1314c58794deSPawel Jakub Dawidek /* 1315c58794deSPawel Jakub Dawidek * Tasting is only made on boot. 1316c58794deSPawel Jakub Dawidek * We detect providers which should be attached before root is mounted. 1317c58794deSPawel Jakub Dawidek */ 1318c58794deSPawel Jakub Dawidek static struct g_geom * 1319c58794deSPawel Jakub Dawidek g_eli_taste(struct g_class *mp, struct g_provider *pp, int flags __unused) 1320c58794deSPawel Jakub Dawidek { 1321c58794deSPawel Jakub Dawidek struct g_eli_metadata md; 1322c58794deSPawel Jakub Dawidek struct g_geom *gp; 1323c58794deSPawel Jakub Dawidek struct hmac_ctx ctx; 1324c58794deSPawel Jakub Dawidek char passphrase[256]; 1325c58794deSPawel Jakub Dawidek u_char key[G_ELI_USERKEYLEN], mkey[G_ELI_DATAIVKEYLEN]; 13263453dc72SMariusz Zaborski u_int i, nkey, nkeyfiles, tries, showpass; 1327c58794deSPawel Jakub Dawidek int error; 1328ec5c0e5bSAllan Jude struct keybuf *keybuf; 1329c58794deSPawel Jakub Dawidek 1330c58794deSPawel Jakub Dawidek g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name); 1331c58794deSPawel Jakub Dawidek g_topology_assert(); 1332c58794deSPawel Jakub Dawidek 1333df3aed4fSPawel Jakub Dawidek if (root_mounted() || g_eli_tries == 0) 1334c58794deSPawel Jakub Dawidek return (NULL); 1335c58794deSPawel Jakub Dawidek 1336c58794deSPawel Jakub Dawidek G_ELI_DEBUG(3, "Tasting %s.", pp->name); 1337c58794deSPawel Jakub Dawidek 1338c58794deSPawel Jakub Dawidek error = g_eli_read_metadata(mp, pp, &md); 1339c58794deSPawel Jakub Dawidek if (error != 0) 1340c58794deSPawel Jakub Dawidek return (NULL); 1341c58794deSPawel Jakub Dawidek gp = NULL; 1342c58794deSPawel Jakub Dawidek 1343c58794deSPawel Jakub Dawidek if (strcmp(md.md_magic, G_ELI_MAGIC) != 0) 1344c58794deSPawel Jakub Dawidek return (NULL); 1345c58794deSPawel Jakub Dawidek if (md.md_version > G_ELI_VERSION) { 1346c58794deSPawel Jakub Dawidek printf("geom_eli.ko module is too old to handle %s.\n", 1347c58794deSPawel Jakub Dawidek pp->name); 1348c58794deSPawel Jakub Dawidek return (NULL); 1349c58794deSPawel Jakub Dawidek } 1350c58794deSPawel Jakub Dawidek if (md.md_provsize != pp->mediasize) 1351c58794deSPawel Jakub Dawidek return (NULL); 1352c58794deSPawel Jakub Dawidek /* Should we attach it on boot? */ 1353c81929d3SKyle Evans if (!(md.md_flags & G_ELI_FLAG_BOOT) && 1354c81929d3SKyle Evans !(md.md_flags & G_ELI_FLAG_GELIBOOT)) 1355c58794deSPawel Jakub Dawidek return (NULL); 1356c58794deSPawel Jakub Dawidek if (md.md_keys == 0x00) { 1357c58794deSPawel Jakub Dawidek G_ELI_DEBUG(0, "No valid keys on %s.", pp->name); 1358c58794deSPawel Jakub Dawidek return (NULL); 1359c58794deSPawel Jakub Dawidek } 1360e2b99193SJohn Baldwin if (!eli_metadata_crypto_supported(&md)) { 1361e2b99193SJohn Baldwin G_ELI_DEBUG(0, "%s uses invalid or unsupported algorithms\n", 1362e2b99193SJohn Baldwin pp->name); 1363e2b99193SJohn Baldwin return (NULL); 1364e2b99193SJohn Baldwin } 13659af2131bSPawel Jakub Dawidek if (md.md_iterations == -1) { 13669af2131bSPawel Jakub Dawidek /* If there is no passphrase, we try only once. */ 13679af2131bSPawel Jakub Dawidek tries = 1; 13689af2131bSPawel Jakub Dawidek } else { 13699af2131bSPawel Jakub Dawidek /* Ask for the passphrase no more than g_eli_tries times. */ 13709af2131bSPawel Jakub Dawidek tries = g_eli_tries; 13719af2131bSPawel Jakub Dawidek } 13729af2131bSPawel Jakub Dawidek 1373ec5c0e5bSAllan Jude if ((keybuf = get_keybuf()) != NULL) { 1374ec5c0e5bSAllan Jude /* Scan the key buffer, try all GELI keys. */ 1375ec5c0e5bSAllan Jude for (i = 0; i < keybuf->kb_nents; i++) { 1376ec5c0e5bSAllan Jude if (keybuf->kb_ents[i].ke_type == KEYBUF_TYPE_GELI) { 1377ec5c0e5bSAllan Jude memcpy(key, keybuf->kb_ents[i].ke_data, 1378ec5c0e5bSAllan Jude sizeof(key)); 1379ec5c0e5bSAllan Jude 138031f7586dSMariusz Zaborski if (g_eli_mkey_decrypt_any(&md, key, 1381ec5c0e5bSAllan Jude mkey, &nkey) == 0 ) { 1382ec5c0e5bSAllan Jude explicit_bzero(key, sizeof(key)); 1383ec5c0e5bSAllan Jude goto have_key; 1384ec5c0e5bSAllan Jude } 1385ec5c0e5bSAllan Jude } 1386ec5c0e5bSAllan Jude } 1387ec5c0e5bSAllan Jude } 1388ec5c0e5bSAllan Jude 1389835c4dd4SColin Percival for (i = 0; i <= tries; i++) { 13909af2131bSPawel Jakub Dawidek g_eli_crypto_hmac_init(&ctx, NULL, 0); 1391c58794deSPawel Jakub Dawidek 1392c58794deSPawel Jakub Dawidek /* 13939af2131bSPawel Jakub Dawidek * Load all key files. 1394c58794deSPawel Jakub Dawidek */ 13959af2131bSPawel Jakub Dawidek nkeyfiles = g_eli_keyfiles_load(&ctx, pp->name); 13969af2131bSPawel Jakub Dawidek 13979af2131bSPawel Jakub Dawidek if (nkeyfiles == 0 && md.md_iterations == -1) { 13989af2131bSPawel Jakub Dawidek /* 13999af2131bSPawel Jakub Dawidek * No key files and no passphrase, something is 14009af2131bSPawel Jakub Dawidek * definitely wrong here. 14019af2131bSPawel Jakub Dawidek * geli(8) doesn't allow for such situation, so assume 14029af2131bSPawel Jakub Dawidek * that there was really no passphrase and in that case 14039af2131bSPawel Jakub Dawidek * key files are no properly defined in loader.conf. 14049af2131bSPawel Jakub Dawidek */ 14059af2131bSPawel Jakub Dawidek G_ELI_DEBUG(0, 14069af2131bSPawel Jakub Dawidek "Found no key files in loader.conf for %s.", 14079af2131bSPawel Jakub Dawidek pp->name); 14089af2131bSPawel Jakub Dawidek return (NULL); 14099af2131bSPawel Jakub Dawidek } 14109af2131bSPawel Jakub Dawidek 14119af2131bSPawel Jakub Dawidek /* Ask for the passphrase if defined. */ 14129af2131bSPawel Jakub Dawidek if (md.md_iterations >= 0) { 1413835c4dd4SColin Percival /* Try first with cached passphrase. */ 1414835c4dd4SColin Percival if (i == 0) { 1415835c4dd4SColin Percival if (!g_eli_boot_passcache) 1416835c4dd4SColin Percival continue; 1417835c4dd4SColin Percival memcpy(passphrase, cached_passphrase, 1418835c4dd4SColin Percival sizeof(passphrase)); 1419835c4dd4SColin Percival } else { 1420c58794deSPawel Jakub Dawidek printf("Enter passphrase for %s: ", pp->name); 14213453dc72SMariusz Zaborski showpass = g_eli_visible_passphrase; 14223453dc72SMariusz Zaborski if ((md.md_flags & G_ELI_FLAG_GELIDISPLAYPASS) != 0) 14233453dc72SMariusz Zaborski showpass = GETS_ECHOPASS; 1424f6ce353eSAndriy Gapon cngets(passphrase, sizeof(passphrase), 14253453dc72SMariusz Zaborski showpass); 1426835c4dd4SColin Percival memcpy(cached_passphrase, passphrase, 1427835c4dd4SColin Percival sizeof(passphrase)); 1428835c4dd4SColin Percival } 14299af2131bSPawel Jakub Dawidek } 14309af2131bSPawel Jakub Dawidek 1431c58794deSPawel Jakub Dawidek /* 1432c58794deSPawel Jakub Dawidek * Prepare Derived-Key from the user passphrase. 1433c58794deSPawel Jakub Dawidek */ 1434c58794deSPawel Jakub Dawidek if (md.md_iterations == 0) { 1435c58794deSPawel Jakub Dawidek g_eli_crypto_hmac_update(&ctx, md.md_salt, 1436c58794deSPawel Jakub Dawidek sizeof(md.md_salt)); 1437c58794deSPawel Jakub Dawidek g_eli_crypto_hmac_update(&ctx, passphrase, 1438c58794deSPawel Jakub Dawidek strlen(passphrase)); 1439ec5c0e5bSAllan Jude explicit_bzero(passphrase, sizeof(passphrase)); 14409af2131bSPawel Jakub Dawidek } else if (md.md_iterations > 0) { 1441c58794deSPawel Jakub Dawidek u_char dkey[G_ELI_USERKEYLEN]; 1442c58794deSPawel Jakub Dawidek 1443c58794deSPawel Jakub Dawidek pkcs5v2_genkey(dkey, sizeof(dkey), md.md_salt, 1444c58794deSPawel Jakub Dawidek sizeof(md.md_salt), passphrase, md.md_iterations); 14456572e5ffSJohn Baldwin explicit_bzero(passphrase, sizeof(passphrase)); 1446c58794deSPawel Jakub Dawidek g_eli_crypto_hmac_update(&ctx, dkey, sizeof(dkey)); 1447ec5c0e5bSAllan Jude explicit_bzero(dkey, sizeof(dkey)); 1448c58794deSPawel Jakub Dawidek } 14499af2131bSPawel Jakub Dawidek 1450c58794deSPawel Jakub Dawidek g_eli_crypto_hmac_final(&ctx, key, 0); 1451c58794deSPawel Jakub Dawidek 1452c58794deSPawel Jakub Dawidek /* 1453c58794deSPawel Jakub Dawidek * Decrypt Master-Key. 1454c58794deSPawel Jakub Dawidek */ 145531f7586dSMariusz Zaborski error = g_eli_mkey_decrypt_any(&md, key, mkey, &nkey); 14566572e5ffSJohn Baldwin explicit_bzero(key, sizeof(key)); 1457c58794deSPawel Jakub Dawidek if (error == -1) { 1458835c4dd4SColin Percival if (i == tries) { 14599af2131bSPawel Jakub Dawidek G_ELI_DEBUG(0, 14609af2131bSPawel Jakub Dawidek "Wrong key for %s. No tries left.", 14619af2131bSPawel Jakub Dawidek pp->name); 14629af2131bSPawel Jakub Dawidek g_eli_keyfiles_clear(pp->name); 14639af2131bSPawel Jakub Dawidek return (NULL); 1464c58794deSPawel Jakub Dawidek } 1465835c4dd4SColin Percival if (i > 0) { 1466835c4dd4SColin Percival G_ELI_DEBUG(0, 1467835c4dd4SColin Percival "Wrong key for %s. Tries left: %u.", 1468835c4dd4SColin Percival pp->name, tries - i); 1469835c4dd4SColin Percival } 1470c58794deSPawel Jakub Dawidek /* Try again. */ 1471c58794deSPawel Jakub Dawidek continue; 1472c58794deSPawel Jakub Dawidek } else if (error > 0) { 1473038c55adSPawel Jakub Dawidek G_ELI_DEBUG(0, 1474038c55adSPawel Jakub Dawidek "Cannot decrypt Master Key for %s (error=%d).", 1475c58794deSPawel Jakub Dawidek pp->name, error); 14769af2131bSPawel Jakub Dawidek g_eli_keyfiles_clear(pp->name); 1477c58794deSPawel Jakub Dawidek return (NULL); 1478c58794deSPawel Jakub Dawidek } 1479ebd05adaSBrad Davis g_eli_keyfiles_clear(pp->name); 1480c58794deSPawel Jakub Dawidek G_ELI_DEBUG(1, "Using Master Key %u for %s.", nkey, pp->name); 1481c58794deSPawel Jakub Dawidek break; 1482c58794deSPawel Jakub Dawidek } 1483ec5c0e5bSAllan Jude have_key: 1484c58794deSPawel Jakub Dawidek 1485c58794deSPawel Jakub Dawidek /* 1486c58794deSPawel Jakub Dawidek * We have correct key, let's attach provider. 1487c58794deSPawel Jakub Dawidek */ 1488c58794deSPawel Jakub Dawidek gp = g_eli_create(NULL, mp, pp, &md, mkey, nkey); 14896572e5ffSJohn Baldwin explicit_bzero(mkey, sizeof(mkey)); 14906572e5ffSJohn Baldwin explicit_bzero(&md, sizeof(md)); 1491c58794deSPawel Jakub Dawidek if (gp == NULL) { 1492c58794deSPawel Jakub Dawidek G_ELI_DEBUG(0, "Cannot create device %s%s.", pp->name, 1493c58794deSPawel Jakub Dawidek G_ELI_SUFFIX); 1494c58794deSPawel Jakub Dawidek return (NULL); 1495c58794deSPawel Jakub Dawidek } 1496c58794deSPawel Jakub Dawidek return (gp); 1497c58794deSPawel Jakub Dawidek } 1498c58794deSPawel Jakub Dawidek 1499c58794deSPawel Jakub Dawidek static void 1500c58794deSPawel Jakub Dawidek g_eli_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, 1501c58794deSPawel Jakub Dawidek struct g_consumer *cp, struct g_provider *pp) 1502c58794deSPawel Jakub Dawidek { 1503c58794deSPawel Jakub Dawidek struct g_eli_softc *sc; 1504c58794deSPawel Jakub Dawidek 1505c58794deSPawel Jakub Dawidek g_topology_assert(); 1506c58794deSPawel Jakub Dawidek sc = gp->softc; 1507c58794deSPawel Jakub Dawidek if (sc == NULL) 1508c58794deSPawel Jakub Dawidek return; 1509c58794deSPawel Jakub Dawidek if (pp != NULL || cp != NULL) 1510c58794deSPawel Jakub Dawidek return; /* Nothing here. */ 15111e09ff3dSPawel Jakub Dawidek 1512743437c4SAndrey V. Elsukov sbuf_printf(sb, "%s<KeysTotal>%ju</KeysTotal>\n", indent, 15131e09ff3dSPawel Jakub Dawidek (uintmax_t)sc->sc_ekeys_total); 1514743437c4SAndrey V. Elsukov sbuf_printf(sb, "%s<KeysAllocated>%ju</KeysAllocated>\n", indent, 15151e09ff3dSPawel Jakub Dawidek (uintmax_t)sc->sc_ekeys_allocated); 1516ea35a2ecSPawel Jakub Dawidek sbuf_printf(sb, "%s<Flags>", indent); 1517ea35a2ecSPawel Jakub Dawidek if (sc->sc_flags == 0) 151849ee0fceSAlexander Motin sbuf_cat(sb, "NONE"); 1519ea35a2ecSPawel Jakub Dawidek else { 1520ea35a2ecSPawel Jakub Dawidek int first = 1; 1521ea35a2ecSPawel Jakub Dawidek 1522ea35a2ecSPawel Jakub Dawidek #define ADD_FLAG(flag, name) do { \ 1523eaa3b919SPawel Jakub Dawidek if (sc->sc_flags & (flag)) { \ 1524ea35a2ecSPawel Jakub Dawidek if (!first) \ 152549ee0fceSAlexander Motin sbuf_cat(sb, ", "); \ 1526ea35a2ecSPawel Jakub Dawidek else \ 1527ea35a2ecSPawel Jakub Dawidek first = 0; \ 152849ee0fceSAlexander Motin sbuf_cat(sb, name); \ 1529ea35a2ecSPawel Jakub Dawidek } \ 1530ea35a2ecSPawel Jakub Dawidek } while (0) 15315ad4a7c7SPawel Jakub Dawidek ADD_FLAG(G_ELI_FLAG_SUSPEND, "SUSPEND"); 1532c6a26d4cSPawel Jakub Dawidek ADD_FLAG(G_ELI_FLAG_SINGLE_KEY, "SINGLE-KEY"); 15332bd4ade6SPawel Jakub Dawidek ADD_FLAG(G_ELI_FLAG_NATIVE_BYTE_ORDER, "NATIVE-BYTE-ORDER"); 1534ea35a2ecSPawel Jakub Dawidek ADD_FLAG(G_ELI_FLAG_ONETIME, "ONETIME"); 1535ea35a2ecSPawel Jakub Dawidek ADD_FLAG(G_ELI_FLAG_BOOT, "BOOT"); 1536ea35a2ecSPawel Jakub Dawidek ADD_FLAG(G_ELI_FLAG_WO_DETACH, "W-DETACH"); 1537ea35a2ecSPawel Jakub Dawidek ADD_FLAG(G_ELI_FLAG_RW_DETACH, "RW-DETACH"); 1538eaa3b919SPawel Jakub Dawidek ADD_FLAG(G_ELI_FLAG_AUTH, "AUTH"); 1539ea35a2ecSPawel Jakub Dawidek ADD_FLAG(G_ELI_FLAG_WOPEN, "W-OPEN"); 1540ea35a2ecSPawel Jakub Dawidek ADD_FLAG(G_ELI_FLAG_DESTROY, "DESTROY"); 154185059016SPawel Jakub Dawidek ADD_FLAG(G_ELI_FLAG_RO, "READ-ONLY"); 154246e34470SPawel Jakub Dawidek ADD_FLAG(G_ELI_FLAG_NODELETE, "NODELETE"); 1543d8736625SAllan Jude ADD_FLAG(G_ELI_FLAG_GELIBOOT, "GELIBOOT"); 15443453dc72SMariusz Zaborski ADD_FLAG(G_ELI_FLAG_GELIDISPLAYPASS, "GELIDISPLAYPASS"); 15452f07cdf8SPawel Jakub Dawidek ADD_FLAG(G_ELI_FLAG_AUTORESIZE, "AUTORESIZE"); 1546ea35a2ecSPawel Jakub Dawidek #undef ADD_FLAG 1547ea35a2ecSPawel Jakub Dawidek } 154849ee0fceSAlexander Motin sbuf_cat(sb, "</Flags>\n"); 1549ea35a2ecSPawel Jakub Dawidek 1550eaa3b919SPawel Jakub Dawidek if (!(sc->sc_flags & G_ELI_FLAG_ONETIME)) { 1551ea35a2ecSPawel Jakub Dawidek sbuf_printf(sb, "%s<UsedKey>%u</UsedKey>\n", indent, 1552ea35a2ecSPawel Jakub Dawidek sc->sc_nkey); 1553ea35a2ecSPawel Jakub Dawidek } 15541f8c92e6SPawel Jakub Dawidek sbuf_printf(sb, "%s<Version>%u</Version>\n", indent, sc->sc_version); 1555c58794deSPawel Jakub Dawidek sbuf_printf(sb, "%s<Crypto>", indent); 1556c58794deSPawel Jakub Dawidek switch (sc->sc_crypto) { 1557c58794deSPawel Jakub Dawidek case G_ELI_CRYPTO_HW: 155849ee0fceSAlexander Motin sbuf_cat(sb, "hardware"); 1559c58794deSPawel Jakub Dawidek break; 1560c58794deSPawel Jakub Dawidek case G_ELI_CRYPTO_SW: 156149ee0fceSAlexander Motin sbuf_cat(sb, "software"); 1562c58794deSPawel Jakub Dawidek break; 1563a3d565a1SJohn Baldwin case G_ELI_CRYPTO_SW_ACCEL: 1564a3d565a1SJohn Baldwin sbuf_cat(sb, "accelerated software"); 1565a3d565a1SJohn Baldwin break; 1566c58794deSPawel Jakub Dawidek default: 156749ee0fceSAlexander Motin sbuf_cat(sb, "UNKNOWN"); 1568c58794deSPawel Jakub Dawidek break; 1569c58794deSPawel Jakub Dawidek } 157049ee0fceSAlexander Motin sbuf_cat(sb, "</Crypto>\n"); 1571eaa3b919SPawel Jakub Dawidek if (sc->sc_flags & G_ELI_FLAG_AUTH) { 1572eaa3b919SPawel Jakub Dawidek sbuf_printf(sb, 1573eaa3b919SPawel Jakub Dawidek "%s<AuthenticationAlgorithm>%s</AuthenticationAlgorithm>\n", 1574eaa3b919SPawel Jakub Dawidek indent, g_eli_algo2str(sc->sc_aalgo)); 1575eaa3b919SPawel Jakub Dawidek } 1576eaa3b919SPawel Jakub Dawidek sbuf_printf(sb, "%s<KeyLength>%u</KeyLength>\n", indent, 1577eaa3b919SPawel Jakub Dawidek sc->sc_ekeylen); 1578038c55adSPawel Jakub Dawidek sbuf_printf(sb, "%s<EncryptionAlgorithm>%s</EncryptionAlgorithm>\n", 1579038c55adSPawel Jakub Dawidek indent, g_eli_algo2str(sc->sc_ealgo)); 1580d8d61ef8SPawel Jakub Dawidek sbuf_printf(sb, "%s<State>%s</State>\n", indent, 1581d8d61ef8SPawel Jakub Dawidek (sc->sc_flags & G_ELI_FLAG_SUSPEND) ? "SUSPENDED" : "ACTIVE"); 1582c58794deSPawel Jakub Dawidek } 1583c58794deSPawel Jakub Dawidek 1584c5d387d0SPawel Jakub Dawidek static void 1585c5d387d0SPawel Jakub Dawidek g_eli_shutdown_pre_sync(void *arg, int howto) 1586c5d387d0SPawel Jakub Dawidek { 1587c5d387d0SPawel Jakub Dawidek struct g_class *mp; 1588c5d387d0SPawel Jakub Dawidek struct g_geom *gp, *gp2; 1589c5d387d0SPawel Jakub Dawidek struct g_provider *pp; 1590c5d387d0SPawel Jakub Dawidek struct g_eli_softc *sc; 1591c5d387d0SPawel Jakub Dawidek 1592c5d387d0SPawel Jakub Dawidek mp = arg; 1593c5d387d0SPawel Jakub Dawidek g_topology_lock(); 1594c5d387d0SPawel Jakub Dawidek LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) { 1595c5d387d0SPawel Jakub Dawidek sc = gp->softc; 1596c5d387d0SPawel Jakub Dawidek if (sc == NULL) 1597c5d387d0SPawel Jakub Dawidek continue; 1598c5d387d0SPawel Jakub Dawidek pp = LIST_FIRST(&gp->provider); 1599c5d387d0SPawel Jakub Dawidek KASSERT(pp != NULL, ("No provider? gp=%p (%s)", gp, gp->name)); 16002a230609SAlan Somers if (pp->acr != 0 || pp->acw != 0 || pp->ace != 0 || 16012a230609SAlan Somers SCHEDULER_STOPPED()) 16022a230609SAlan Somers { 1603c5d387d0SPawel Jakub Dawidek sc->sc_flags |= G_ELI_FLAG_RW_DETACH; 1604c5d387d0SPawel Jakub Dawidek gp->access = g_eli_access; 16052a230609SAlan Somers } else { 16068f7878e3SRobert Wing (void) g_eli_destroy(sc, TRUE); 1607c5d387d0SPawel Jakub Dawidek } 1608c5d387d0SPawel Jakub Dawidek } 1609c5d387d0SPawel Jakub Dawidek g_topology_unlock(); 1610c5d387d0SPawel Jakub Dawidek } 1611c5d387d0SPawel Jakub Dawidek 1612c5d387d0SPawel Jakub Dawidek static void 1613c5d387d0SPawel Jakub Dawidek g_eli_init(struct g_class *mp) 1614c5d387d0SPawel Jakub Dawidek { 1615c5d387d0SPawel Jakub Dawidek 1616c5d387d0SPawel Jakub Dawidek g_eli_pre_sync = EVENTHANDLER_REGISTER(shutdown_pre_sync, 1617c5d387d0SPawel Jakub Dawidek g_eli_shutdown_pre_sync, mp, SHUTDOWN_PRI_FIRST); 1618c5d387d0SPawel Jakub Dawidek if (g_eli_pre_sync == NULL) 1619c5d387d0SPawel Jakub Dawidek G_ELI_DEBUG(0, "Warning! Cannot register shutdown event."); 1620c5d387d0SPawel Jakub Dawidek } 1621c5d387d0SPawel Jakub Dawidek 1622c5d387d0SPawel Jakub Dawidek static void 1623c5d387d0SPawel Jakub Dawidek g_eli_fini(struct g_class *mp) 1624c5d387d0SPawel Jakub Dawidek { 1625c5d387d0SPawel Jakub Dawidek 1626c5d387d0SPawel Jakub Dawidek if (g_eli_pre_sync != NULL) 1627c5d387d0SPawel Jakub Dawidek EVENTHANDLER_DEREGISTER(shutdown_pre_sync, g_eli_pre_sync); 1628c5d387d0SPawel Jakub Dawidek } 1629c5d387d0SPawel Jakub Dawidek 1630c58794deSPawel Jakub Dawidek DECLARE_GEOM_CLASS(g_eli_class, g_eli); 1631f6829a05SYaroslav Tykhiy MODULE_DEPEND(g_eli, crypto, 1, 1, 1); 163274d6c131SKyle Evans MODULE_VERSION(geom_eli, 0); 1633