xref: /freebsd/sys/geom/eli/g_eli.c (revision 53a6215c83e7f2849bb07b57d955d17e47e65bc6)
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>
35c58794deSPawel Jakub Dawidek #include <sys/kernel.h>
369af2131bSPawel Jakub Dawidek #include <sys/linker.h>
37c58794deSPawel Jakub Dawidek #include <sys/module.h>
38c58794deSPawel Jakub Dawidek #include <sys/lock.h>
39c58794deSPawel Jakub Dawidek #include <sys/mutex.h>
40c58794deSPawel Jakub Dawidek #include <sys/bio.h>
415d807a0eSAndrey V. Elsukov #include <sys/sbuf.h>
42c58794deSPawel Jakub Dawidek #include <sys/sysctl.h>
43c58794deSPawel Jakub Dawidek #include <sys/malloc.h>
44c5d387d0SPawel Jakub Dawidek #include <sys/eventhandler.h>
45c58794deSPawel Jakub Dawidek #include <sys/kthread.h>
46c58794deSPawel Jakub Dawidek #include <sys/proc.h>
47c58794deSPawel Jakub Dawidek #include <sys/sched.h>
48dd549194SPawel Jakub Dawidek #include <sys/smp.h>
49c58794deSPawel Jakub Dawidek #include <sys/uio.h>
50a80f82a4SPawel Jakub Dawidek #include <sys/vnode.h>
51c58794deSPawel Jakub Dawidek 
52c58794deSPawel Jakub Dawidek #include <vm/uma.h>
53c58794deSPawel Jakub Dawidek 
54c58794deSPawel Jakub Dawidek #include <geom/geom.h>
55ac03832eSConrad Meyer #include <geom/geom_dbg.h>
56c58794deSPawel Jakub Dawidek #include <geom/eli/g_eli.h>
57c58794deSPawel Jakub Dawidek #include <geom/eli/pkcs5v2.h>
58c58794deSPawel Jakub Dawidek 
59ec5c0e5bSAllan Jude #include <crypto/intake.h>
60ec5c0e5bSAllan Jude 
61cb08c2ccSAlexander Leidinger FEATURE(geom_eli, "GEOM crypto module");
62c58794deSPawel Jakub Dawidek 
63c58794deSPawel Jakub Dawidek MALLOC_DEFINE(M_ELI, "eli data", "GEOM_ELI Data");
64c58794deSPawel Jakub Dawidek 
65c58794deSPawel Jakub Dawidek SYSCTL_DECL(_kern_geom);
66*53a6215cSPawel Biernacki SYSCTL_NODE(_kern_geom, OID_AUTO, eli, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
67*53a6215cSPawel Biernacki     "GEOM_ELI stuff");
68a1f4a8c4SPawel Jakub Dawidek static int g_eli_version = G_ELI_VERSION;
69a1f4a8c4SPawel Jakub Dawidek SYSCTL_INT(_kern_geom_eli, OID_AUTO, version, CTLFLAG_RD, &g_eli_version, 0,
70a1f4a8c4SPawel Jakub Dawidek     "GELI version");
71f95168e0SPawel Jakub Dawidek int g_eli_debug = 0;
72af3b2549SHans Petter Selasky SYSCTL_INT(_kern_geom_eli, OID_AUTO, debug, CTLFLAG_RWTUN, &g_eli_debug, 0,
73c58794deSPawel Jakub Dawidek     "Debug level");
74c58794deSPawel Jakub Dawidek static u_int g_eli_tries = 3;
75af3b2549SHans Petter Selasky SYSCTL_UINT(_kern_geom_eli, OID_AUTO, tries, CTLFLAG_RWTUN, &g_eli_tries, 0,
7698645006SChristian Brueffer     "Number of tries for entering the passphrase");
77eb4c31fdSEd Schouten static u_int g_eli_visible_passphrase = GETS_NOECHO;
78af3b2549SHans Petter Selasky SYSCTL_UINT(_kern_geom_eli, OID_AUTO, visible_passphrase, CTLFLAG_RWTUN,
79c58794deSPawel Jakub Dawidek     &g_eli_visible_passphrase, 0,
80eb4c31fdSEd Schouten     "Visibility of passphrase prompt (0 = invisible, 1 = visible, 2 = asterisk)");
81b35bfe7eSPawel Jakub Dawidek u_int g_eli_overwrites = G_ELI_OVERWRITES;
82af3b2549SHans Petter Selasky SYSCTL_UINT(_kern_geom_eli, OID_AUTO, overwrites, CTLFLAG_RWTUN, &g_eli_overwrites,
8398645006SChristian Brueffer     0, "Number of times on-disk keys should be overwritten when destroying them");
84dd549194SPawel Jakub Dawidek static u_int g_eli_threads = 0;
85af3b2549SHans Petter Selasky SYSCTL_UINT(_kern_geom_eli, OID_AUTO, threads, CTLFLAG_RWTUN, &g_eli_threads, 0,
86c58794deSPawel Jakub Dawidek     "Number of threads doing crypto work");
87eaa3b919SPawel Jakub Dawidek u_int g_eli_batch = 0;
88af3b2549SHans Petter Selasky SYSCTL_UINT(_kern_geom_eli, OID_AUTO, batch, CTLFLAG_RWTUN, &g_eli_batch, 0,
89eaa3b919SPawel Jakub Dawidek     "Use crypto operations batching");
90c58794deSPawel Jakub Dawidek 
91835c4dd4SColin Percival /*
92835c4dd4SColin Percival  * Passphrase cached during boot, in order to be more user-friendly if
93835c4dd4SColin Percival  * there are multiple providers using the same passphrase.
94835c4dd4SColin Percival  */
95835c4dd4SColin Percival static char cached_passphrase[256];
96835c4dd4SColin Percival static u_int g_eli_boot_passcache = 1;
97835c4dd4SColin Percival TUNABLE_INT("kern.geom.eli.boot_passcache", &g_eli_boot_passcache);
98835c4dd4SColin Percival SYSCTL_UINT(_kern_geom_eli, OID_AUTO, boot_passcache, CTLFLAG_RD,
99835c4dd4SColin Percival     &g_eli_boot_passcache, 0,
100835c4dd4SColin Percival     "Passphrases are cached during boot process for possible reuse");
101835c4dd4SColin Percival static void
10266427784SColin Percival fetch_loader_passphrase(void * dummy)
10366427784SColin Percival {
10466427784SColin Percival 	char * env_passphrase;
10566427784SColin Percival 
10666427784SColin Percival 	KASSERT(dynamic_kenv, ("need dynamic kenv"));
10766427784SColin Percival 
10866427784SColin Percival 	if ((env_passphrase = kern_getenv("kern.geom.eli.passphrase")) != NULL) {
10966427784SColin Percival 		/* Extract passphrase from the environment. */
11066427784SColin Percival 		strlcpy(cached_passphrase, env_passphrase,
11166427784SColin Percival 		    sizeof(cached_passphrase));
11266427784SColin Percival 		freeenv(env_passphrase);
11366427784SColin Percival 
11466427784SColin Percival 		/* Wipe the passphrase from the environment. */
11566427784SColin Percival 		kern_unsetenv("kern.geom.eli.passphrase");
11666427784SColin Percival 	}
11766427784SColin Percival }
11866427784SColin Percival SYSINIT(geli_fetch_loader_passphrase, SI_SUB_KMEM + 1, SI_ORDER_ANY,
11966427784SColin Percival     fetch_loader_passphrase, NULL);
120ec5c0e5bSAllan Jude 
12166427784SColin Percival static void
122ec5c0e5bSAllan Jude zero_boot_passcache(void)
123835c4dd4SColin Percival {
124835c4dd4SColin Percival 
125ec5c0e5bSAllan Jude         explicit_bzero(cached_passphrase, sizeof(cached_passphrase));
126835c4dd4SColin Percival }
127ec5c0e5bSAllan Jude 
128ec5c0e5bSAllan Jude static void
129ec5c0e5bSAllan Jude zero_geli_intake_keys(void)
130ec5c0e5bSAllan Jude {
131ec5c0e5bSAllan Jude         struct keybuf *keybuf;
132ec5c0e5bSAllan Jude         int i;
133ec5c0e5bSAllan Jude 
134ec5c0e5bSAllan Jude         if ((keybuf = get_keybuf()) != NULL) {
135ec5c0e5bSAllan Jude                 /* Scan the key buffer, clear all GELI keys. */
136ec5c0e5bSAllan Jude                 for (i = 0; i < keybuf->kb_nents; i++) {
137ec5c0e5bSAllan Jude                          if (keybuf->kb_ents[i].ke_type == KEYBUF_TYPE_GELI) {
138ec5c0e5bSAllan Jude                                  explicit_bzero(keybuf->kb_ents[i].ke_data,
139ec5c0e5bSAllan Jude                                      sizeof(keybuf->kb_ents[i].ke_data));
140ec5c0e5bSAllan Jude                                  keybuf->kb_ents[i].ke_type = KEYBUF_TYPE_NONE;
141ec5c0e5bSAllan Jude                          }
142ec5c0e5bSAllan Jude                 }
143ec5c0e5bSAllan Jude         }
144ec5c0e5bSAllan Jude }
145ec5c0e5bSAllan Jude 
146ec5c0e5bSAllan Jude static void
147ec5c0e5bSAllan Jude zero_intake_passcache(void *dummy)
148ec5c0e5bSAllan Jude {
149ec5c0e5bSAllan Jude         zero_boot_passcache();
150ec5c0e5bSAllan Jude         zero_geli_intake_keys();
151ec5c0e5bSAllan Jude }
152ec5c0e5bSAllan Jude EVENTHANDLER_DEFINE(mountroot, zero_intake_passcache, NULL, 0);
153835c4dd4SColin Percival 
154c5d387d0SPawel Jakub Dawidek static eventhandler_tag g_eli_pre_sync = NULL;
155c5d387d0SPawel Jakub Dawidek 
1562f07cdf8SPawel Jakub Dawidek static int g_eli_read_metadata_offset(struct g_class *mp, struct g_provider *pp,
1572f07cdf8SPawel Jakub Dawidek     off_t offset, struct g_eli_metadata *md);
1582f07cdf8SPawel Jakub Dawidek 
159c58794deSPawel Jakub Dawidek static int g_eli_destroy_geom(struct gctl_req *req, struct g_class *mp,
160c58794deSPawel Jakub Dawidek     struct g_geom *gp);
161c5d387d0SPawel Jakub Dawidek static void g_eli_init(struct g_class *mp);
162c5d387d0SPawel Jakub Dawidek static void g_eli_fini(struct g_class *mp);
163c58794deSPawel Jakub Dawidek 
164c58794deSPawel Jakub Dawidek static g_taste_t g_eli_taste;
165c58794deSPawel Jakub Dawidek static g_dumpconf_t g_eli_dumpconf;
166c58794deSPawel Jakub Dawidek 
167c58794deSPawel Jakub Dawidek struct g_class g_eli_class = {
168c58794deSPawel Jakub Dawidek 	.name = G_ELI_CLASS_NAME,
169c58794deSPawel Jakub Dawidek 	.version = G_VERSION,
170c58794deSPawel Jakub Dawidek 	.ctlreq = g_eli_config,
171c58794deSPawel Jakub Dawidek 	.taste = g_eli_taste,
172c5d387d0SPawel Jakub Dawidek 	.destroy_geom = g_eli_destroy_geom,
173c5d387d0SPawel Jakub Dawidek 	.init = g_eli_init,
174c5d387d0SPawel Jakub Dawidek 	.fini = g_eli_fini
175c58794deSPawel Jakub Dawidek };
176c58794deSPawel Jakub Dawidek 
177c58794deSPawel Jakub Dawidek 
178c58794deSPawel Jakub Dawidek /*
179c58794deSPawel Jakub Dawidek  * Code paths:
180c58794deSPawel Jakub Dawidek  * BIO_READ:
1815ad4a7c7SPawel 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
182c58794deSPawel Jakub Dawidek  * BIO_WRITE:
183c58794deSPawel Jakub Dawidek  *	g_eli_start -> g_eli_crypto_run -> g_eli_crypto_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver
184c58794deSPawel Jakub Dawidek  */
185c58794deSPawel Jakub Dawidek 
186c58794deSPawel Jakub Dawidek 
187c58794deSPawel Jakub Dawidek /*
188c58794deSPawel Jakub Dawidek  * EAGAIN from crypto(9) means, that we were probably balanced to another crypto
189c58794deSPawel Jakub Dawidek  * accelerator or something like this.
190c58794deSPawel Jakub Dawidek  * The function updates the SID and rerun the operation.
191c58794deSPawel Jakub Dawidek  */
192eaa3b919SPawel Jakub Dawidek int
193c58794deSPawel Jakub Dawidek g_eli_crypto_rerun(struct cryptop *crp)
194c58794deSPawel Jakub Dawidek {
195c58794deSPawel Jakub Dawidek 	struct g_eli_softc *sc;
196c58794deSPawel Jakub Dawidek 	struct g_eli_worker *wr;
197c58794deSPawel Jakub Dawidek 	struct bio *bp;
198c58794deSPawel Jakub Dawidek 	int error;
199c58794deSPawel Jakub Dawidek 
200c58794deSPawel Jakub Dawidek 	bp = (struct bio *)crp->crp_opaque;
201c58794deSPawel Jakub Dawidek 	sc = bp->bio_to->geom->softc;
202c58794deSPawel Jakub Dawidek 	LIST_FOREACH(wr, &sc->sc_workers, w_next) {
203c58794deSPawel Jakub Dawidek 		if (wr->w_number == bp->bio_pflags)
204c58794deSPawel Jakub Dawidek 			break;
205c58794deSPawel Jakub Dawidek 	}
206c58794deSPawel Jakub Dawidek 	KASSERT(wr != NULL, ("Invalid worker (%u).", bp->bio_pflags));
2071b0909d5SConrad Meyer 	G_ELI_DEBUG(1, "Rerunning crypto %s request (sid: %p -> %p).",
2081b0909d5SConrad Meyer 	    bp->bio_cmd == BIO_READ ? "READ" : "WRITE", wr->w_sid,
2091b0909d5SConrad Meyer 	    crp->crp_session);
2101b0909d5SConrad Meyer 	wr->w_sid = crp->crp_session;
211c58794deSPawel Jakub Dawidek 	crp->crp_etype = 0;
212c58794deSPawel Jakub Dawidek 	error = crypto_dispatch(crp);
213c58794deSPawel Jakub Dawidek 	if (error == 0)
214c58794deSPawel Jakub Dawidek 		return (0);
215c58794deSPawel Jakub Dawidek 	G_ELI_DEBUG(1, "%s: crypto_dispatch() returned %d.", __func__, error);
216c58794deSPawel Jakub Dawidek 	crp->crp_etype = error;
217c58794deSPawel Jakub Dawidek 	return (error);
218c58794deSPawel Jakub Dawidek }
219c58794deSPawel Jakub Dawidek 
2200bab7fa8SAlan Somers static void
2210bab7fa8SAlan Somers g_eli_getattr_done(struct bio *bp)
2220bab7fa8SAlan Somers {
2230bab7fa8SAlan Somers 	if (bp->bio_error == 0 &&
2240bab7fa8SAlan Somers 	    !strcmp(bp->bio_attribute, "GEOM::physpath")) {
2250bab7fa8SAlan Somers 		strlcat(bp->bio_data, "/eli", bp->bio_length);
2260bab7fa8SAlan Somers 	}
2270bab7fa8SAlan Somers 	g_std_done(bp);
2280bab7fa8SAlan Somers }
2290bab7fa8SAlan Somers 
230c58794deSPawel Jakub Dawidek /*
231c58794deSPawel Jakub Dawidek  * The function is called afer reading encrypted data from the provider.
232bb30fea6SPawel Jakub Dawidek  *
2335ad4a7c7SPawel 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
234c58794deSPawel Jakub Dawidek  */
235eaa3b919SPawel Jakub Dawidek void
236c58794deSPawel Jakub Dawidek g_eli_read_done(struct bio *bp)
237c58794deSPawel Jakub Dawidek {
238c58794deSPawel Jakub Dawidek 	struct g_eli_softc *sc;
239c58794deSPawel Jakub Dawidek 	struct bio *pbp;
240c58794deSPawel Jakub Dawidek 
241c58794deSPawel Jakub Dawidek 	G_ELI_LOGREQ(2, bp, "Request done.");
242c58794deSPawel Jakub Dawidek 	pbp = bp->bio_parent;
2432dc7e36bSSteven Hartland 	if (pbp->bio_error == 0 && bp->bio_error != 0)
244c58794deSPawel Jakub Dawidek 		pbp->bio_error = bp->bio_error;
24590574b0aSMikolaj Golub 	g_destroy_bio(bp);
246eaa3b919SPawel Jakub Dawidek 	/*
247eaa3b919SPawel Jakub Dawidek 	 * Do we have all sectors already?
248eaa3b919SPawel Jakub Dawidek 	 */
249eaa3b919SPawel Jakub Dawidek 	pbp->bio_inbed++;
250eaa3b919SPawel Jakub Dawidek 	if (pbp->bio_inbed < pbp->bio_children)
251eaa3b919SPawel Jakub Dawidek 		return;
2525ad4a7c7SPawel Jakub Dawidek 	sc = pbp->bio_to->geom->softc;
253c58794deSPawel Jakub Dawidek 	if (pbp->bio_error != 0) {
2542dc7e36bSSteven Hartland 		G_ELI_LOGREQ(0, pbp, "%s() failed (error=%d)", __func__,
2552dc7e36bSSteven Hartland 		    pbp->bio_error);
256c58794deSPawel Jakub Dawidek 		pbp->bio_completed = 0;
257eaa3b919SPawel Jakub Dawidek 		if (pbp->bio_driver2 != NULL) {
258eaa3b919SPawel Jakub Dawidek 			free(pbp->bio_driver2, M_ELI);
259eaa3b919SPawel Jakub Dawidek 			pbp->bio_driver2 = NULL;
260eaa3b919SPawel Jakub Dawidek 		}
261c58794deSPawel Jakub Dawidek 		g_io_deliver(pbp, pbp->bio_error);
26278f79a9aSMariusz Zaborski 		if (sc != NULL)
2635ad4a7c7SPawel Jakub Dawidek 			atomic_subtract_int(&sc->sc_inflight, 1);
264c58794deSPawel Jakub Dawidek 		return;
265c58794deSPawel Jakub Dawidek 	}
266c58794deSPawel Jakub Dawidek 	mtx_lock(&sc->sc_queue_mtx);
267c58794deSPawel Jakub Dawidek 	bioq_insert_tail(&sc->sc_queue, pbp);
268c58794deSPawel Jakub Dawidek 	mtx_unlock(&sc->sc_queue_mtx);
269c58794deSPawel Jakub Dawidek 	wakeup(sc);
270c58794deSPawel Jakub Dawidek }
271c58794deSPawel Jakub Dawidek 
272c58794deSPawel Jakub Dawidek /*
273c58794deSPawel Jakub Dawidek  * The function is called after we encrypt and write data.
274bb30fea6SPawel Jakub Dawidek  *
275bb30fea6SPawel Jakub Dawidek  * g_eli_start -> g_eli_crypto_run -> g_eli_crypto_write_done -> g_io_request -> G_ELI_WRITE_DONE -> g_io_deliver
276c58794deSPawel Jakub Dawidek  */
277eaa3b919SPawel Jakub Dawidek void
278c58794deSPawel Jakub Dawidek g_eli_write_done(struct bio *bp)
279c58794deSPawel Jakub Dawidek {
2805ad4a7c7SPawel Jakub Dawidek 	struct g_eli_softc *sc;
281c58794deSPawel Jakub Dawidek 	struct bio *pbp;
282c58794deSPawel Jakub Dawidek 
283c58794deSPawel Jakub Dawidek 	G_ELI_LOGREQ(2, bp, "Request done.");
284c58794deSPawel Jakub Dawidek 	pbp = bp->bio_parent;
2852dc7e36bSSteven Hartland 	if (pbp->bio_error == 0 && bp->bio_error != 0)
286c58794deSPawel Jakub Dawidek 		pbp->bio_error = bp->bio_error;
28790574b0aSMikolaj Golub 	g_destroy_bio(bp);
288eaa3b919SPawel Jakub Dawidek 	/*
289eaa3b919SPawel Jakub Dawidek 	 * Do we have all sectors already?
290eaa3b919SPawel Jakub Dawidek 	 */
291eaa3b919SPawel Jakub Dawidek 	pbp->bio_inbed++;
292eaa3b919SPawel Jakub Dawidek 	if (pbp->bio_inbed < pbp->bio_children)
293eaa3b919SPawel Jakub Dawidek 		return;
294c58794deSPawel Jakub Dawidek 	free(pbp->bio_driver2, M_ELI);
295c58794deSPawel Jakub Dawidek 	pbp->bio_driver2 = NULL;
296eaa3b919SPawel Jakub Dawidek 	if (pbp->bio_error != 0) {
2972dc7e36bSSteven Hartland 		G_ELI_LOGREQ(0, pbp, "%s() failed (error=%d)", __func__,
298c58794deSPawel Jakub Dawidek 		    pbp->bio_error);
299c58794deSPawel Jakub Dawidek 		pbp->bio_completed = 0;
3002dc7e36bSSteven Hartland 	} else
3012dc7e36bSSteven Hartland 		pbp->bio_completed = pbp->bio_length;
3022dc7e36bSSteven Hartland 
303c58794deSPawel Jakub Dawidek 	/*
304c58794deSPawel Jakub Dawidek 	 * Write is finished, send it up.
305c58794deSPawel Jakub Dawidek 	 */
3065ad4a7c7SPawel Jakub Dawidek 	sc = pbp->bio_to->geom->softc;
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 }
311c58794deSPawel Jakub Dawidek 
312c58794deSPawel Jakub Dawidek /*
313c58794deSPawel Jakub Dawidek  * This function should never be called, but GEOM made as it set ->orphan()
314c58794deSPawel Jakub Dawidek  * method for every geom.
315c58794deSPawel Jakub Dawidek  */
316c58794deSPawel Jakub Dawidek static void
317c58794deSPawel Jakub Dawidek g_eli_orphan_spoil_assert(struct g_consumer *cp)
318c58794deSPawel Jakub Dawidek {
319c58794deSPawel Jakub Dawidek 
320c58794deSPawel Jakub Dawidek 	panic("Function %s() called for %s.", __func__, cp->geom->name);
321c58794deSPawel Jakub Dawidek }
322c58794deSPawel Jakub Dawidek 
323c58794deSPawel Jakub Dawidek static void
324c58794deSPawel Jakub Dawidek g_eli_orphan(struct g_consumer *cp)
325c58794deSPawel Jakub Dawidek {
326c58794deSPawel Jakub Dawidek 	struct g_eli_softc *sc;
327c58794deSPawel Jakub Dawidek 
328c58794deSPawel Jakub Dawidek 	g_topology_assert();
329c58794deSPawel Jakub Dawidek 	sc = cp->geom->softc;
330c58794deSPawel Jakub Dawidek 	if (sc == NULL)
331c58794deSPawel Jakub Dawidek 		return;
3325ad4a7c7SPawel Jakub Dawidek 	g_eli_destroy(sc, TRUE);
333c58794deSPawel Jakub Dawidek }
334c58794deSPawel Jakub Dawidek 
3352f07cdf8SPawel Jakub Dawidek static void
3362f07cdf8SPawel Jakub Dawidek g_eli_resize(struct g_consumer *cp)
3372f07cdf8SPawel Jakub Dawidek {
3382f07cdf8SPawel Jakub Dawidek 	struct g_eli_softc *sc;
3392f07cdf8SPawel Jakub Dawidek 	struct g_provider *epp, *pp;
3402f07cdf8SPawel Jakub Dawidek 	off_t oldsize;
3412f07cdf8SPawel Jakub Dawidek 
3422f07cdf8SPawel Jakub Dawidek 	g_topology_assert();
3432f07cdf8SPawel Jakub Dawidek 	sc = cp->geom->softc;
3442f07cdf8SPawel Jakub Dawidek 	if (sc == NULL)
3452f07cdf8SPawel Jakub Dawidek 		return;
3462f07cdf8SPawel Jakub Dawidek 
3472f07cdf8SPawel Jakub Dawidek 	if ((sc->sc_flags & G_ELI_FLAG_AUTORESIZE) == 0) {
3482f07cdf8SPawel Jakub Dawidek 		G_ELI_DEBUG(0, "Autoresize is turned off, old size: %jd.",
3492f07cdf8SPawel Jakub Dawidek 		    (intmax_t)sc->sc_provsize);
3502f07cdf8SPawel Jakub Dawidek 		return;
3512f07cdf8SPawel Jakub Dawidek 	}
3522f07cdf8SPawel Jakub Dawidek 
3532f07cdf8SPawel Jakub Dawidek 	pp = cp->provider;
3542f07cdf8SPawel Jakub Dawidek 
3552f07cdf8SPawel Jakub Dawidek 	if ((sc->sc_flags & G_ELI_FLAG_ONETIME) == 0) {
3562f07cdf8SPawel Jakub Dawidek 		struct g_eli_metadata md;
3572f07cdf8SPawel Jakub Dawidek 		u_char *sector;
3582f07cdf8SPawel Jakub Dawidek 		int error;
3592f07cdf8SPawel Jakub Dawidek 
3602f07cdf8SPawel Jakub Dawidek 		sector = NULL;
3612f07cdf8SPawel Jakub Dawidek 
3622f07cdf8SPawel Jakub Dawidek 		error = g_eli_read_metadata_offset(cp->geom->class, pp,
3632f07cdf8SPawel Jakub Dawidek 		    sc->sc_provsize - pp->sectorsize, &md);
3642f07cdf8SPawel Jakub Dawidek 		if (error != 0) {
3652f07cdf8SPawel Jakub Dawidek 			G_ELI_DEBUG(0, "Cannot read metadata from %s (error=%d).",
3662f07cdf8SPawel Jakub Dawidek 			    pp->name, error);
3672f07cdf8SPawel Jakub Dawidek 			goto iofail;
3682f07cdf8SPawel Jakub Dawidek 		}
3692f07cdf8SPawel Jakub Dawidek 
3702f07cdf8SPawel Jakub Dawidek 		md.md_provsize = pp->mediasize;
3712f07cdf8SPawel Jakub Dawidek 
3722f07cdf8SPawel Jakub Dawidek 		sector = malloc(pp->sectorsize, M_ELI, M_WAITOK | M_ZERO);
3732f07cdf8SPawel Jakub Dawidek 		eli_metadata_encode(&md, sector);
3742f07cdf8SPawel Jakub Dawidek 		error = g_write_data(cp, pp->mediasize - pp->sectorsize, sector,
3752f07cdf8SPawel Jakub Dawidek 		    pp->sectorsize);
3762f07cdf8SPawel Jakub Dawidek 		if (error != 0) {
3772f07cdf8SPawel Jakub Dawidek 			G_ELI_DEBUG(0, "Cannot store metadata on %s (error=%d).",
3782f07cdf8SPawel Jakub Dawidek 			    pp->name, error);
3792f07cdf8SPawel Jakub Dawidek 			goto iofail;
3802f07cdf8SPawel Jakub Dawidek 		}
3812f07cdf8SPawel Jakub Dawidek 		explicit_bzero(sector, pp->sectorsize);
3822f07cdf8SPawel Jakub Dawidek 		error = g_write_data(cp, sc->sc_provsize - pp->sectorsize,
3832f07cdf8SPawel Jakub Dawidek 		    sector, pp->sectorsize);
3842f07cdf8SPawel Jakub Dawidek 		if (error != 0) {
3852f07cdf8SPawel Jakub Dawidek 			G_ELI_DEBUG(0, "Cannot clear old metadata from %s (error=%d).",
3862f07cdf8SPawel Jakub Dawidek 			    pp->name, error);
3872f07cdf8SPawel Jakub Dawidek 			goto iofail;
3882f07cdf8SPawel Jakub Dawidek 		}
3892f07cdf8SPawel Jakub Dawidek iofail:
3902f07cdf8SPawel Jakub Dawidek 		explicit_bzero(&md, sizeof(md));
3912f07cdf8SPawel Jakub Dawidek 		if (sector != NULL) {
3922f07cdf8SPawel Jakub Dawidek 			explicit_bzero(sector, pp->sectorsize);
3932f07cdf8SPawel Jakub Dawidek 			free(sector, M_ELI);
3942f07cdf8SPawel Jakub Dawidek 		}
3952f07cdf8SPawel Jakub Dawidek 	}
3962f07cdf8SPawel Jakub Dawidek 
3972f07cdf8SPawel Jakub Dawidek 	oldsize = sc->sc_mediasize;
3982f07cdf8SPawel Jakub Dawidek 	sc->sc_mediasize = eli_mediasize(sc, pp->mediasize, pp->sectorsize);
3992f07cdf8SPawel Jakub Dawidek 	g_eli_key_resize(sc);
4002f07cdf8SPawel Jakub Dawidek 	sc->sc_provsize = pp->mediasize;
4012f07cdf8SPawel Jakub Dawidek 
4022f07cdf8SPawel Jakub Dawidek 	epp = LIST_FIRST(&sc->sc_geom->provider);
4032f07cdf8SPawel Jakub Dawidek 	g_resize_provider(epp, sc->sc_mediasize);
4042f07cdf8SPawel Jakub Dawidek 	G_ELI_DEBUG(0, "Device %s size changed from %jd to %jd.", epp->name,
4052f07cdf8SPawel Jakub Dawidek 	    (intmax_t)oldsize, (intmax_t)sc->sc_mediasize);
4062f07cdf8SPawel Jakub Dawidek }
4072f07cdf8SPawel Jakub Dawidek 
408bb30fea6SPawel Jakub Dawidek /*
409056638c4SPawel Jakub Dawidek  * BIO_READ:
4105ad4a7c7SPawel 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
411056638c4SPawel Jakub Dawidek  * BIO_WRITE:
412056638c4SPawel Jakub Dawidek  *	G_ELI_START -> g_eli_crypto_run -> g_eli_crypto_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver
413bb30fea6SPawel Jakub Dawidek  */
414c58794deSPawel Jakub Dawidek static void
415c58794deSPawel Jakub Dawidek g_eli_start(struct bio *bp)
416c58794deSPawel Jakub Dawidek {
417c58794deSPawel Jakub Dawidek 	struct g_eli_softc *sc;
418d3a1be90SPawel Jakub Dawidek 	struct g_consumer *cp;
419c58794deSPawel Jakub Dawidek 	struct bio *cbp;
420c58794deSPawel Jakub Dawidek 
421c58794deSPawel Jakub Dawidek 	sc = bp->bio_to->geom->softc;
422c58794deSPawel Jakub Dawidek 	KASSERT(sc != NULL,
423c58794deSPawel Jakub Dawidek 	    ("Provider's error should be set (error=%d)(device=%s).",
424c58794deSPawel Jakub Dawidek 	    bp->bio_to->error, bp->bio_to->name));
425c58794deSPawel Jakub Dawidek 	G_ELI_LOGREQ(2, bp, "Request received.");
426c58794deSPawel Jakub Dawidek 
427c58794deSPawel Jakub Dawidek 	switch (bp->bio_cmd) {
428c58794deSPawel Jakub Dawidek 	case BIO_READ:
429c58794deSPawel Jakub Dawidek 	case BIO_WRITE:
430d3a1be90SPawel Jakub Dawidek 	case BIO_GETATTR:
43142461fbaSPawel Jakub Dawidek 	case BIO_FLUSH:
4329a6844d5SKenneth D. Merry 	case BIO_ZONE:
4338b522bdaSWarner Losh 	case BIO_SPEEDUP:
434c58794deSPawel Jakub Dawidek 		break;
435c58794deSPawel Jakub Dawidek 	case BIO_DELETE:
436c58794deSPawel Jakub Dawidek 		/*
43746e34470SPawel Jakub Dawidek 		 * If the user hasn't set the NODELETE flag, we just pass
43846e34470SPawel Jakub Dawidek 		 * it down the stack and let the layers beneath us do (or
43946e34470SPawel Jakub Dawidek 		 * not) whatever they do with it.  If they have, we
44046e34470SPawel Jakub Dawidek 		 * reject it.  A possible extension would be an
44146e34470SPawel Jakub Dawidek 		 * additional flag to take it as a hint to shred the data
44246e34470SPawel Jakub Dawidek 		 * with [multiple?] overwrites.
443c58794deSPawel Jakub Dawidek 		 */
44446e34470SPawel Jakub Dawidek 		if (!(sc->sc_flags & G_ELI_FLAG_NODELETE))
44546e34470SPawel Jakub Dawidek 			break;
446c58794deSPawel Jakub Dawidek 	default:
447c58794deSPawel Jakub Dawidek 		g_io_deliver(bp, EOPNOTSUPP);
448c58794deSPawel Jakub Dawidek 		return;
449c58794deSPawel Jakub Dawidek 	}
450c58794deSPawel Jakub Dawidek 	cbp = g_clone_bio(bp);
451c58794deSPawel Jakub Dawidek 	if (cbp == NULL) {
452c58794deSPawel Jakub Dawidek 		g_io_deliver(bp, ENOMEM);
453c58794deSPawel Jakub Dawidek 		return;
454c58794deSPawel Jakub Dawidek 	}
4555ad4a7c7SPawel Jakub Dawidek 	bp->bio_driver1 = cbp;
4565ad4a7c7SPawel Jakub Dawidek 	bp->bio_pflags = G_ELI_NEW_BIO;
457d3a1be90SPawel Jakub Dawidek 	switch (bp->bio_cmd) {
458d3a1be90SPawel Jakub Dawidek 	case BIO_READ:
459eaa3b919SPawel Jakub Dawidek 		if (!(sc->sc_flags & G_ELI_FLAG_AUTH)) {
4605ad4a7c7SPawel Jakub Dawidek 			g_eli_crypto_read(sc, bp, 0);
461d3a1be90SPawel Jakub Dawidek 			break;
462eaa3b919SPawel Jakub Dawidek 		}
463eaa3b919SPawel Jakub Dawidek 		/* FALLTHROUGH */
464d3a1be90SPawel Jakub Dawidek 	case BIO_WRITE:
465c58794deSPawel Jakub Dawidek 		mtx_lock(&sc->sc_queue_mtx);
466c58794deSPawel Jakub Dawidek 		bioq_insert_tail(&sc->sc_queue, bp);
467c58794deSPawel Jakub Dawidek 		mtx_unlock(&sc->sc_queue_mtx);
468c58794deSPawel Jakub Dawidek 		wakeup(sc);
469d3a1be90SPawel Jakub Dawidek 		break;
470d3a1be90SPawel Jakub Dawidek 	case BIO_GETATTR:
47142461fbaSPawel Jakub Dawidek 	case BIO_FLUSH:
47246e34470SPawel Jakub Dawidek 	case BIO_DELETE:
4738b522bdaSWarner Losh 	case BIO_SPEEDUP:
4749a6844d5SKenneth D. Merry 	case BIO_ZONE:
4750bab7fa8SAlan Somers 		if (bp->bio_cmd == BIO_GETATTR)
4760bab7fa8SAlan Somers 			cbp->bio_done = g_eli_getattr_done;
4770bab7fa8SAlan Somers 		else
478d3a1be90SPawel Jakub Dawidek 			cbp->bio_done = g_std_done;
479d3a1be90SPawel Jakub Dawidek 		cp = LIST_FIRST(&sc->sc_geom->consumer);
480d3a1be90SPawel Jakub Dawidek 		cbp->bio_to = cp->provider;
481cd0d707eSPawel Jakub Dawidek 		G_ELI_LOGREQ(2, cbp, "Sending request.");
482d3a1be90SPawel Jakub Dawidek 		g_io_request(cbp, cp);
483d3a1be90SPawel Jakub Dawidek 		break;
484c58794deSPawel Jakub Dawidek 	}
485c58794deSPawel Jakub Dawidek }
486c58794deSPawel Jakub Dawidek 
4873ac01bc2SPawel Jakub Dawidek static int
4883ac01bc2SPawel Jakub Dawidek g_eli_newsession(struct g_eli_worker *wr)
4893ac01bc2SPawel Jakub Dawidek {
4903ac01bc2SPawel Jakub Dawidek 	struct g_eli_softc *sc;
4913ac01bc2SPawel Jakub Dawidek 	struct cryptoini crie, cria;
4923ac01bc2SPawel Jakub Dawidek 	int error;
4933ac01bc2SPawel Jakub Dawidek 
4943ac01bc2SPawel Jakub Dawidek 	sc = wr->w_softc;
4953ac01bc2SPawel Jakub Dawidek 
4963ac01bc2SPawel Jakub Dawidek 	bzero(&crie, sizeof(crie));
4973ac01bc2SPawel Jakub Dawidek 	crie.cri_alg = sc->sc_ealgo;
4983ac01bc2SPawel Jakub Dawidek 	crie.cri_klen = sc->sc_ekeylen;
4993ac01bc2SPawel Jakub Dawidek 	if (sc->sc_ealgo == CRYPTO_AES_XTS)
5003ac01bc2SPawel Jakub Dawidek 		crie.cri_klen <<= 1;
501ad0a5236SPawel Jakub Dawidek 	if ((sc->sc_flags & G_ELI_FLAG_FIRST_KEY) != 0) {
502ad0a5236SPawel Jakub Dawidek 		crie.cri_key = g_eli_key_hold(sc, 0,
503ad0a5236SPawel Jakub Dawidek 		    LIST_FIRST(&sc->sc_geom->consumer)->provider->sectorsize);
504ad0a5236SPawel Jakub Dawidek 	} else {
5051e09ff3dSPawel Jakub Dawidek 		crie.cri_key = sc->sc_ekey;
506ad0a5236SPawel Jakub Dawidek 	}
5073ac01bc2SPawel Jakub Dawidek 	if (sc->sc_flags & G_ELI_FLAG_AUTH) {
5083ac01bc2SPawel Jakub Dawidek 		bzero(&cria, sizeof(cria));
5093ac01bc2SPawel Jakub Dawidek 		cria.cri_alg = sc->sc_aalgo;
5103ac01bc2SPawel Jakub Dawidek 		cria.cri_klen = sc->sc_akeylen;
5113ac01bc2SPawel Jakub Dawidek 		cria.cri_key = sc->sc_akey;
5123ac01bc2SPawel Jakub Dawidek 		crie.cri_next = &cria;
5133ac01bc2SPawel Jakub Dawidek 	}
5143ac01bc2SPawel Jakub Dawidek 
5153ac01bc2SPawel Jakub Dawidek 	switch (sc->sc_crypto) {
5163ac01bc2SPawel Jakub Dawidek 	case G_ELI_CRYPTO_SW:
5173ac01bc2SPawel Jakub Dawidek 		error = crypto_newsession(&wr->w_sid, &crie,
5183ac01bc2SPawel Jakub Dawidek 		    CRYPTOCAP_F_SOFTWARE);
5193ac01bc2SPawel Jakub Dawidek 		break;
5203ac01bc2SPawel Jakub Dawidek 	case G_ELI_CRYPTO_HW:
5213ac01bc2SPawel Jakub Dawidek 		error = crypto_newsession(&wr->w_sid, &crie,
5223ac01bc2SPawel Jakub Dawidek 		    CRYPTOCAP_F_HARDWARE);
5233ac01bc2SPawel Jakub Dawidek 		break;
5243ac01bc2SPawel Jakub Dawidek 	case G_ELI_CRYPTO_UNKNOWN:
5253ac01bc2SPawel Jakub Dawidek 		error = crypto_newsession(&wr->w_sid, &crie,
5263ac01bc2SPawel Jakub Dawidek 		    CRYPTOCAP_F_HARDWARE);
5273ac01bc2SPawel Jakub Dawidek 		if (error == 0) {
5283ac01bc2SPawel Jakub Dawidek 			mtx_lock(&sc->sc_queue_mtx);
5293ac01bc2SPawel Jakub Dawidek 			if (sc->sc_crypto == G_ELI_CRYPTO_UNKNOWN)
5303ac01bc2SPawel Jakub Dawidek 				sc->sc_crypto = G_ELI_CRYPTO_HW;
5313ac01bc2SPawel Jakub Dawidek 			mtx_unlock(&sc->sc_queue_mtx);
5323ac01bc2SPawel Jakub Dawidek 		} else {
5333ac01bc2SPawel Jakub Dawidek 			error = crypto_newsession(&wr->w_sid, &crie,
5343ac01bc2SPawel Jakub Dawidek 			    CRYPTOCAP_F_SOFTWARE);
5353ac01bc2SPawel Jakub Dawidek 			mtx_lock(&sc->sc_queue_mtx);
5363ac01bc2SPawel Jakub Dawidek 			if (sc->sc_crypto == G_ELI_CRYPTO_UNKNOWN)
5373ac01bc2SPawel Jakub Dawidek 				sc->sc_crypto = G_ELI_CRYPTO_SW;
5383ac01bc2SPawel Jakub Dawidek 			mtx_unlock(&sc->sc_queue_mtx);
5393ac01bc2SPawel Jakub Dawidek 		}
5403ac01bc2SPawel Jakub Dawidek 		break;
5413ac01bc2SPawel Jakub Dawidek 	default:
5423ac01bc2SPawel Jakub Dawidek 		panic("%s: invalid condition", __func__);
5433ac01bc2SPawel Jakub Dawidek 	}
5443ac01bc2SPawel Jakub Dawidek 
545ad0a5236SPawel Jakub Dawidek 	if ((sc->sc_flags & G_ELI_FLAG_FIRST_KEY) != 0)
546ad0a5236SPawel Jakub Dawidek 		g_eli_key_drop(sc, crie.cri_key);
547ad0a5236SPawel Jakub Dawidek 
5483ac01bc2SPawel Jakub Dawidek 	return (error);
5493ac01bc2SPawel Jakub Dawidek }
5503ac01bc2SPawel Jakub Dawidek 
5513ac01bc2SPawel Jakub Dawidek static void
5523ac01bc2SPawel Jakub Dawidek g_eli_freesession(struct g_eli_worker *wr)
5533ac01bc2SPawel Jakub Dawidek {
5543ac01bc2SPawel Jakub Dawidek 
5553ac01bc2SPawel Jakub Dawidek 	crypto_freesession(wr->w_sid);
5563ac01bc2SPawel Jakub Dawidek }
5573ac01bc2SPawel Jakub Dawidek 
5585ad4a7c7SPawel Jakub Dawidek static void
5595ad4a7c7SPawel Jakub Dawidek g_eli_cancel(struct g_eli_softc *sc)
5605ad4a7c7SPawel Jakub Dawidek {
5615ad4a7c7SPawel Jakub Dawidek 	struct bio *bp;
5625ad4a7c7SPawel Jakub Dawidek 
5635ad4a7c7SPawel Jakub Dawidek 	mtx_assert(&sc->sc_queue_mtx, MA_OWNED);
5645ad4a7c7SPawel Jakub Dawidek 
5655ad4a7c7SPawel Jakub Dawidek 	while ((bp = bioq_takefirst(&sc->sc_queue)) != NULL) {
5665ad4a7c7SPawel Jakub Dawidek 		KASSERT(bp->bio_pflags == G_ELI_NEW_BIO,
5675ad4a7c7SPawel Jakub Dawidek 		    ("Not new bio when canceling (bp=%p).", bp));
5685ad4a7c7SPawel Jakub Dawidek 		g_io_deliver(bp, ENXIO);
5695ad4a7c7SPawel Jakub Dawidek 	}
5705ad4a7c7SPawel Jakub Dawidek }
5715ad4a7c7SPawel Jakub Dawidek 
5725ad4a7c7SPawel Jakub Dawidek static struct bio *
5735ad4a7c7SPawel Jakub Dawidek g_eli_takefirst(struct g_eli_softc *sc)
5745ad4a7c7SPawel Jakub Dawidek {
5755ad4a7c7SPawel Jakub Dawidek 	struct bio *bp;
5765ad4a7c7SPawel Jakub Dawidek 
5775ad4a7c7SPawel Jakub Dawidek 	mtx_assert(&sc->sc_queue_mtx, MA_OWNED);
5785ad4a7c7SPawel Jakub Dawidek 
5795ad4a7c7SPawel Jakub Dawidek 	if (!(sc->sc_flags & G_ELI_FLAG_SUSPEND))
5805ad4a7c7SPawel Jakub Dawidek 		return (bioq_takefirst(&sc->sc_queue));
5815ad4a7c7SPawel Jakub Dawidek 	/*
5825ad4a7c7SPawel Jakub Dawidek 	 * Device suspended, so we skip new I/O requests.
5835ad4a7c7SPawel Jakub Dawidek 	 */
5845ad4a7c7SPawel Jakub Dawidek 	TAILQ_FOREACH(bp, &sc->sc_queue.queue, bio_queue) {
5855ad4a7c7SPawel Jakub Dawidek 		if (bp->bio_pflags != G_ELI_NEW_BIO)
5865ad4a7c7SPawel Jakub Dawidek 			break;
5875ad4a7c7SPawel Jakub Dawidek 	}
5885ad4a7c7SPawel Jakub Dawidek 	if (bp != NULL)
5895ad4a7c7SPawel Jakub Dawidek 		bioq_remove(&sc->sc_queue, bp);
5905ad4a7c7SPawel Jakub Dawidek 	return (bp);
5915ad4a7c7SPawel Jakub Dawidek }
5925ad4a7c7SPawel Jakub Dawidek 
593c58794deSPawel Jakub Dawidek /*
594c58794deSPawel Jakub Dawidek  * This is the main function for kernel worker thread when we don't have
595c58794deSPawel Jakub Dawidek  * hardware acceleration and we have to do cryptography in software.
596c58794deSPawel Jakub Dawidek  * Dedicated thread is needed, so we don't slow down g_up/g_down GEOM
597c58794deSPawel Jakub Dawidek  * threads with crypto work.
598c58794deSPawel Jakub Dawidek  */
599c58794deSPawel Jakub Dawidek static void
600c58794deSPawel Jakub Dawidek g_eli_worker(void *arg)
601c58794deSPawel Jakub Dawidek {
602c58794deSPawel Jakub Dawidek 	struct g_eli_softc *sc;
603c58794deSPawel Jakub Dawidek 	struct g_eli_worker *wr;
604c58794deSPawel Jakub Dawidek 	struct bio *bp;
6053ac01bc2SPawel Jakub Dawidek 	int error;
606c58794deSPawel Jakub Dawidek 
607c58794deSPawel Jakub Dawidek 	wr = arg;
608c58794deSPawel Jakub Dawidek 	sc = wr->w_softc;
609fdce57a0SJohn Baldwin #ifdef EARLY_AP_STARTUP
610fdce57a0SJohn Baldwin 	MPASS(!sc->sc_cpubind || smp_started);
611fdce57a0SJohn Baldwin #elif defined(SMP)
612a1ea1a22SPawel Jakub Dawidek 	/* Before sched_bind() to a CPU, wait for all CPUs to go on-line. */
6130c879bd9SPawel Jakub Dawidek 	if (sc->sc_cpubind) {
614a1ea1a22SPawel Jakub Dawidek 		while (!smp_started)
615a1ea1a22SPawel Jakub Dawidek 			tsleep(wr, 0, "geli:smp", hz / 4);
616a1ea1a22SPawel Jakub Dawidek 	}
617a1ea1a22SPawel Jakub Dawidek #endif
618982d11f8SJeff Roberson 	thread_lock(curthread);
61931c4cef7SPawel Jakub Dawidek 	sched_prio(curthread, PUSER);
6200c879bd9SPawel Jakub Dawidek 	if (sc->sc_cpubind)
6210c879bd9SPawel Jakub Dawidek 		sched_bind(curthread, wr->w_number % mp_ncpus);
622982d11f8SJeff Roberson 	thread_unlock(curthread);
623c58794deSPawel Jakub Dawidek 
624c58794deSPawel Jakub Dawidek 	G_ELI_DEBUG(1, "Thread %s started.", curthread->td_proc->p_comm);
625c58794deSPawel Jakub Dawidek 
626c58794deSPawel Jakub Dawidek 	for (;;) {
627c58794deSPawel Jakub Dawidek 		mtx_lock(&sc->sc_queue_mtx);
6285ad4a7c7SPawel Jakub Dawidek again:
6295ad4a7c7SPawel Jakub Dawidek 		bp = g_eli_takefirst(sc);
630c58794deSPawel Jakub Dawidek 		if (bp == NULL) {
631eaa3b919SPawel Jakub Dawidek 			if (sc->sc_flags & G_ELI_FLAG_DESTROY) {
6325ad4a7c7SPawel Jakub Dawidek 				g_eli_cancel(sc);
633c58794deSPawel Jakub Dawidek 				LIST_REMOVE(wr, w_next);
6343ac01bc2SPawel Jakub Dawidek 				g_eli_freesession(wr);
635c58794deSPawel Jakub Dawidek 				free(wr, M_ELI);
636c58794deSPawel Jakub Dawidek 				G_ELI_DEBUG(1, "Thread %s exiting.",
637c58794deSPawel Jakub Dawidek 				    curthread->td_proc->p_comm);
638c58794deSPawel Jakub Dawidek 				wakeup(&sc->sc_workers);
639c58794deSPawel Jakub Dawidek 				mtx_unlock(&sc->sc_queue_mtx);
6403745c395SJulian Elischer 				kproc_exit(0);
641c58794deSPawel Jakub Dawidek 			}
6425ad4a7c7SPawel Jakub Dawidek 			while (sc->sc_flags & G_ELI_FLAG_SUSPEND) {
6435ad4a7c7SPawel Jakub Dawidek 				if (sc->sc_inflight > 0) {
644038c55adSPawel Jakub Dawidek 					G_ELI_DEBUG(0, "inflight=%d",
645038c55adSPawel Jakub Dawidek 					    sc->sc_inflight);
6465ad4a7c7SPawel Jakub Dawidek 					/*
6475ad4a7c7SPawel Jakub Dawidek 					 * We still have inflight BIOs, so
6485ad4a7c7SPawel Jakub Dawidek 					 * sleep and retry.
6495ad4a7c7SPawel Jakub Dawidek 					 */
6505ad4a7c7SPawel Jakub Dawidek 					msleep(sc, &sc->sc_queue_mtx, PRIBIO,
6515ad4a7c7SPawel Jakub Dawidek 					    "geli:inf", hz / 5);
6525ad4a7c7SPawel Jakub Dawidek 					goto again;
6535ad4a7c7SPawel Jakub Dawidek 				}
6545ad4a7c7SPawel Jakub Dawidek 				/*
6555ad4a7c7SPawel Jakub Dawidek 				 * Suspend requested, mark the worker as
6565ad4a7c7SPawel Jakub Dawidek 				 * suspended and go to sleep.
6575ad4a7c7SPawel Jakub Dawidek 				 */
6583ac01bc2SPawel Jakub Dawidek 				if (wr->w_active) {
6593ac01bc2SPawel Jakub Dawidek 					g_eli_freesession(wr);
6603ac01bc2SPawel Jakub Dawidek 					wr->w_active = FALSE;
6613ac01bc2SPawel Jakub Dawidek 				}
6625ad4a7c7SPawel Jakub Dawidek 				wakeup(&sc->sc_workers);
6635ad4a7c7SPawel Jakub Dawidek 				msleep(sc, &sc->sc_queue_mtx, PRIBIO,
6645ad4a7c7SPawel Jakub Dawidek 				    "geli:suspend", 0);
6653ac01bc2SPawel Jakub Dawidek 				if (!wr->w_active &&
6663ac01bc2SPawel Jakub Dawidek 				    !(sc->sc_flags & G_ELI_FLAG_SUSPEND)) {
6673ac01bc2SPawel Jakub Dawidek 					error = g_eli_newsession(wr);
6683ac01bc2SPawel Jakub Dawidek 					KASSERT(error == 0,
6693ac01bc2SPawel Jakub Dawidek 					    ("g_eli_newsession() failed on resume (error=%d)",
6703ac01bc2SPawel Jakub Dawidek 					    error));
6713ac01bc2SPawel Jakub Dawidek 					wr->w_active = TRUE;
6723ac01bc2SPawel Jakub Dawidek 				}
6735ad4a7c7SPawel Jakub Dawidek 				goto again;
6745ad4a7c7SPawel Jakub Dawidek 			}
67531c4cef7SPawel Jakub Dawidek 			msleep(sc, &sc->sc_queue_mtx, PDROP, "geli:w", 0);
676c58794deSPawel Jakub Dawidek 			continue;
677c58794deSPawel Jakub Dawidek 		}
6785ad4a7c7SPawel Jakub Dawidek 		if (bp->bio_pflags == G_ELI_NEW_BIO)
6795ad4a7c7SPawel Jakub Dawidek 			atomic_add_int(&sc->sc_inflight, 1);
680c58794deSPawel Jakub Dawidek 		mtx_unlock(&sc->sc_queue_mtx);
6815ad4a7c7SPawel Jakub Dawidek 		if (bp->bio_pflags == G_ELI_NEW_BIO) {
6825ad4a7c7SPawel Jakub Dawidek 			bp->bio_pflags = 0;
6835ad4a7c7SPawel Jakub Dawidek 			if (sc->sc_flags & G_ELI_FLAG_AUTH) {
6845ad4a7c7SPawel Jakub Dawidek 				if (bp->bio_cmd == BIO_READ)
685eaa3b919SPawel Jakub Dawidek 					g_eli_auth_read(sc, bp);
6865ad4a7c7SPawel Jakub Dawidek 				else
6875ad4a7c7SPawel Jakub Dawidek 					g_eli_auth_run(wr, bp);
6885ad4a7c7SPawel Jakub Dawidek 			} else {
6895ad4a7c7SPawel Jakub Dawidek 				if (bp->bio_cmd == BIO_READ)
6905ad4a7c7SPawel Jakub Dawidek 					g_eli_crypto_read(sc, bp, 1);
6915ad4a7c7SPawel Jakub Dawidek 				else
6925ad4a7c7SPawel Jakub Dawidek 					g_eli_crypto_run(wr, bp);
6935ad4a7c7SPawel Jakub Dawidek 			}
6945ad4a7c7SPawel Jakub Dawidek 		} else {
6955ad4a7c7SPawel Jakub Dawidek 			if (sc->sc_flags & G_ELI_FLAG_AUTH)
696eaa3b919SPawel Jakub Dawidek 				g_eli_auth_run(wr, bp);
697eaa3b919SPawel Jakub Dawidek 			else
698dddd1d53SPawel Jakub Dawidek 				g_eli_crypto_run(wr, bp);
699c58794deSPawel Jakub Dawidek 		}
700c58794deSPawel Jakub Dawidek 	}
7015ad4a7c7SPawel Jakub Dawidek }
702c58794deSPawel Jakub Dawidek 
7032f07cdf8SPawel Jakub Dawidek static int
7042f07cdf8SPawel Jakub Dawidek g_eli_read_metadata_offset(struct g_class *mp, struct g_provider *pp,
7052f07cdf8SPawel Jakub Dawidek     off_t offset, struct g_eli_metadata *md)
706c58794deSPawel Jakub Dawidek {
707c58794deSPawel Jakub Dawidek 	struct g_geom *gp;
708c58794deSPawel Jakub Dawidek 	struct g_consumer *cp;
709c58794deSPawel Jakub Dawidek 	u_char *buf = NULL;
710c58794deSPawel Jakub Dawidek 	int error;
711c58794deSPawel Jakub Dawidek 
712c58794deSPawel Jakub Dawidek 	g_topology_assert();
713c58794deSPawel Jakub Dawidek 
714c58794deSPawel Jakub Dawidek 	gp = g_new_geomf(mp, "eli:taste");
715c58794deSPawel Jakub Dawidek 	gp->start = g_eli_start;
716c58794deSPawel Jakub Dawidek 	gp->access = g_std_access;
717c58794deSPawel Jakub Dawidek 	/*
718c58794deSPawel Jakub Dawidek 	 * g_eli_read_metadata() is always called from the event thread.
719c58794deSPawel Jakub Dawidek 	 * Our geom is created and destroyed in the same event, so there
720c58794deSPawel Jakub Dawidek 	 * could be no orphan nor spoil event in the meantime.
721c58794deSPawel Jakub Dawidek 	 */
722c58794deSPawel Jakub Dawidek 	gp->orphan = g_eli_orphan_spoil_assert;
723c58794deSPawel Jakub Dawidek 	gp->spoiled = g_eli_orphan_spoil_assert;
724c58794deSPawel Jakub Dawidek 	cp = g_new_consumer(gp);
725c58794deSPawel Jakub Dawidek 	error = g_attach(cp, pp);
726c58794deSPawel Jakub Dawidek 	if (error != 0)
727c58794deSPawel Jakub Dawidek 		goto end;
728c58794deSPawel Jakub Dawidek 	error = g_access(cp, 1, 0, 0);
729c58794deSPawel Jakub Dawidek 	if (error != 0)
730c58794deSPawel Jakub Dawidek 		goto end;
731c58794deSPawel Jakub Dawidek 	g_topology_unlock();
7322f07cdf8SPawel Jakub Dawidek 	buf = g_read_data(cp, offset, pp->sectorsize, &error);
733c58794deSPawel Jakub Dawidek 	g_topology_lock();
7348a4a44b5SMaxim Sobolev 	if (buf == NULL)
735c58794deSPawel Jakub Dawidek 		goto end;
736fefb6a14SPawel Jakub Dawidek 	error = eli_metadata_decode(buf, md);
737fefb6a14SPawel Jakub Dawidek 	if (error != 0)
738fefb6a14SPawel Jakub Dawidek 		goto end;
739fefb6a14SPawel Jakub Dawidek 	/* Metadata was read and decoded successfully. */
740c58794deSPawel Jakub Dawidek end:
741c58794deSPawel Jakub Dawidek 	if (buf != NULL)
742c58794deSPawel Jakub Dawidek 		g_free(buf);
743c58794deSPawel Jakub Dawidek 	if (cp->provider != NULL) {
744c58794deSPawel Jakub Dawidek 		if (cp->acr == 1)
745c58794deSPawel Jakub Dawidek 			g_access(cp, -1, 0, 0);
746c58794deSPawel Jakub Dawidek 		g_detach(cp);
747c58794deSPawel Jakub Dawidek 	}
748c58794deSPawel Jakub Dawidek 	g_destroy_consumer(cp);
749c58794deSPawel Jakub Dawidek 	g_destroy_geom(gp);
750c58794deSPawel Jakub Dawidek 	return (error);
751c58794deSPawel Jakub Dawidek }
752c58794deSPawel Jakub Dawidek 
7532f07cdf8SPawel Jakub Dawidek int
7542f07cdf8SPawel Jakub Dawidek g_eli_read_metadata(struct g_class *mp, struct g_provider *pp,
7552f07cdf8SPawel Jakub Dawidek     struct g_eli_metadata *md)
7562f07cdf8SPawel Jakub Dawidek {
7572f07cdf8SPawel Jakub Dawidek 
7582f07cdf8SPawel Jakub Dawidek 	return (g_eli_read_metadata_offset(mp, pp,
7592f07cdf8SPawel Jakub Dawidek 	    pp->mediasize - pp->sectorsize, md));
7602f07cdf8SPawel Jakub Dawidek }
7612f07cdf8SPawel Jakub Dawidek 
762c58794deSPawel Jakub Dawidek /*
763c58794deSPawel Jakub Dawidek  * The function is called when we had last close on provider and user requested
764c58794deSPawel Jakub Dawidek  * to close it when this situation occur.
765c58794deSPawel Jakub Dawidek  */
766c58794deSPawel Jakub Dawidek static void
76719351a14SAlexander Motin g_eli_last_close(void *arg, int flags __unused)
768c58794deSPawel Jakub Dawidek {
769c58794deSPawel Jakub Dawidek 	struct g_geom *gp;
77019351a14SAlexander Motin 	char gpname[64];
771c58794deSPawel Jakub Dawidek 	int error;
772c58794deSPawel Jakub Dawidek 
773c58794deSPawel Jakub Dawidek 	g_topology_assert();
77419351a14SAlexander Motin 	gp = arg;
77519351a14SAlexander Motin 	strlcpy(gpname, gp->name, sizeof(gpname));
77619351a14SAlexander Motin 	error = g_eli_destroy(gp->softc, TRUE);
777c58794deSPawel Jakub Dawidek 	KASSERT(error == 0, ("Cannot detach %s on last close (error=%d).",
77819351a14SAlexander Motin 	    gpname, error));
77919351a14SAlexander Motin 	G_ELI_DEBUG(0, "Detached %s on last close.", gpname);
780c58794deSPawel Jakub Dawidek }
781c58794deSPawel Jakub Dawidek 
782c58794deSPawel Jakub Dawidek int
783c58794deSPawel Jakub Dawidek g_eli_access(struct g_provider *pp, int dr, int dw, int de)
784c58794deSPawel Jakub Dawidek {
785c58794deSPawel Jakub Dawidek 	struct g_eli_softc *sc;
786c58794deSPawel Jakub Dawidek 	struct g_geom *gp;
787c58794deSPawel Jakub Dawidek 
788c58794deSPawel Jakub Dawidek 	gp = pp->geom;
789c58794deSPawel Jakub Dawidek 	sc = gp->softc;
790c58794deSPawel Jakub Dawidek 
791c58794deSPawel Jakub Dawidek 	if (dw > 0) {
79285059016SPawel Jakub Dawidek 		if (sc->sc_flags & G_ELI_FLAG_RO) {
79385059016SPawel Jakub Dawidek 			/* Deny write attempts. */
79485059016SPawel Jakub Dawidek 			return (EROFS);
79585059016SPawel Jakub Dawidek 		}
796c58794deSPawel Jakub Dawidek 		/* Someone is opening us for write, we need to remember that. */
797c58794deSPawel Jakub Dawidek 		sc->sc_flags |= G_ELI_FLAG_WOPEN;
798c58794deSPawel Jakub Dawidek 		return (0);
799c58794deSPawel Jakub Dawidek 	}
800c58794deSPawel Jakub Dawidek 	/* Is this the last close? */
801c58794deSPawel Jakub Dawidek 	if (pp->acr + dr > 0 || pp->acw + dw > 0 || pp->ace + de > 0)
802c58794deSPawel Jakub Dawidek 		return (0);
803c58794deSPawel Jakub Dawidek 
804c58794deSPawel Jakub Dawidek 	/*
805c58794deSPawel Jakub Dawidek 	 * Automatically detach on last close if requested.
806c58794deSPawel Jakub Dawidek 	 */
807c58794deSPawel Jakub Dawidek 	if ((sc->sc_flags & G_ELI_FLAG_RW_DETACH) ||
808c58794deSPawel Jakub Dawidek 	    (sc->sc_flags & G_ELI_FLAG_WOPEN)) {
80919351a14SAlexander Motin 		g_post_event(g_eli_last_close, gp, M_WAITOK, NULL);
810c58794deSPawel Jakub Dawidek 	}
811c58794deSPawel Jakub Dawidek 	return (0);
812c58794deSPawel Jakub Dawidek }
813c58794deSPawel Jakub Dawidek 
814eba8f137SPawel Jakub Dawidek static int
815eba8f137SPawel Jakub Dawidek g_eli_cpu_is_disabled(int cpu)
816eba8f137SPawel Jakub Dawidek {
817eba8f137SPawel Jakub Dawidek #ifdef SMP
81871a19bdcSAttilio Rao 	return (CPU_ISSET(cpu, &hlt_cpus_mask));
819eba8f137SPawel Jakub Dawidek #else
820eba8f137SPawel Jakub Dawidek 	return (0);
821eba8f137SPawel Jakub Dawidek #endif
822eba8f137SPawel Jakub Dawidek }
823eba8f137SPawel Jakub Dawidek 
824c58794deSPawel Jakub Dawidek struct g_geom *
825c58794deSPawel Jakub Dawidek g_eli_create(struct gctl_req *req, struct g_class *mp, struct g_provider *bpp,
826c58794deSPawel Jakub Dawidek     const struct g_eli_metadata *md, const u_char *mkey, int nkey)
827c58794deSPawel Jakub Dawidek {
828c58794deSPawel Jakub Dawidek 	struct g_eli_softc *sc;
829c58794deSPawel Jakub Dawidek 	struct g_eli_worker *wr;
830c58794deSPawel Jakub Dawidek 	struct g_geom *gp;
831c58794deSPawel Jakub Dawidek 	struct g_provider *pp;
832c58794deSPawel Jakub Dawidek 	struct g_consumer *cp;
833c58794deSPawel Jakub Dawidek 	u_int i, threads;
8343bb6e0f0SRyan Libby 	int dcw, error;
835c58794deSPawel Jakub Dawidek 
836c58794deSPawel Jakub Dawidek 	G_ELI_DEBUG(1, "Creating device %s%s.", bpp->name, G_ELI_SUFFIX);
837c58794deSPawel Jakub Dawidek 
838c58794deSPawel Jakub Dawidek 	gp = g_new_geomf(mp, "%s%s", bpp->name, G_ELI_SUFFIX);
839c58794deSPawel Jakub Dawidek 	sc = malloc(sizeof(*sc), M_ELI, M_WAITOK | M_ZERO);
840c58794deSPawel Jakub Dawidek 	gp->start = g_eli_start;
841c58794deSPawel Jakub Dawidek 	/*
8424273d412SPawel Jakub Dawidek 	 * Spoiling can happen even though we have the provider open
8434273d412SPawel Jakub Dawidek 	 * exclusively, e.g. through media change events.
844c58794deSPawel Jakub Dawidek 	 */
8454273d412SPawel Jakub Dawidek 	gp->spoiled = g_eli_orphan;
846c58794deSPawel Jakub Dawidek 	gp->orphan = g_eli_orphan;
8472f07cdf8SPawel Jakub Dawidek 	gp->resize = g_eli_resize;
84885059016SPawel Jakub Dawidek 	gp->dumpconf = g_eli_dumpconf;
849c58794deSPawel Jakub Dawidek 	/*
85085059016SPawel Jakub Dawidek 	 * If detach-on-last-close feature is not enabled and we don't operate
85185059016SPawel Jakub Dawidek 	 * on read-only provider, we can simply use g_std_access().
852c58794deSPawel Jakub Dawidek 	 */
85385059016SPawel Jakub Dawidek 	if (md->md_flags & (G_ELI_FLAG_WO_DETACH | G_ELI_FLAG_RO))
854c58794deSPawel Jakub Dawidek 		gp->access = g_eli_access;
855c58794deSPawel Jakub Dawidek 	else
856c58794deSPawel Jakub Dawidek 		gp->access = g_std_access;
857c58794deSPawel Jakub Dawidek 
8584332fecaSAllan Jude 	eli_metadata_softc(sc, md, bpp->sectorsize, bpp->mediasize);
859c58794deSPawel Jakub Dawidek 	sc->sc_nkey = nkey;
860eaa3b919SPawel Jakub Dawidek 
861c58794deSPawel Jakub Dawidek 	gp->softc = sc;
862c58794deSPawel Jakub Dawidek 	sc->sc_geom = gp;
863c58794deSPawel Jakub Dawidek 
864c58794deSPawel Jakub Dawidek 	bioq_init(&sc->sc_queue);
865c58794deSPawel Jakub Dawidek 	mtx_init(&sc->sc_queue_mtx, "geli:queue", NULL, MTX_DEF);
8661e09ff3dSPawel Jakub Dawidek 	mtx_init(&sc->sc_ekeys_lock, "geli:ekeys", NULL, MTX_DEF);
867c58794deSPawel Jakub Dawidek 
868c58794deSPawel Jakub Dawidek 	pp = NULL;
869c58794deSPawel Jakub Dawidek 	cp = g_new_consumer(gp);
870c58794deSPawel Jakub Dawidek 	error = g_attach(cp, bpp);
871c58794deSPawel Jakub Dawidek 	if (error != 0) {
872c58794deSPawel Jakub Dawidek 		if (req != NULL) {
873c58794deSPawel Jakub Dawidek 			gctl_error(req, "Cannot attach to %s (error=%d).",
874c58794deSPawel Jakub Dawidek 			    bpp->name, error);
875c58794deSPawel Jakub Dawidek 		} else {
876c58794deSPawel Jakub Dawidek 			G_ELI_DEBUG(1, "Cannot attach to %s (error=%d).",
877c58794deSPawel Jakub Dawidek 			    bpp->name, error);
878c58794deSPawel Jakub Dawidek 		}
879c58794deSPawel Jakub Dawidek 		goto failed;
880c58794deSPawel Jakub Dawidek 	}
881c58794deSPawel Jakub Dawidek 	/*
882c58794deSPawel Jakub Dawidek 	 * Keep provider open all the time, so we can run critical tasks,
883c58794deSPawel Jakub Dawidek 	 * like Master Keys deletion, without wondering if we can open
884c58794deSPawel Jakub Dawidek 	 * provider or not.
88585059016SPawel Jakub Dawidek 	 * We don't open provider for writing only when user requested read-only
88685059016SPawel Jakub Dawidek 	 * access.
887c58794deSPawel Jakub Dawidek 	 */
8883bb6e0f0SRyan Libby 	dcw = (sc->sc_flags & G_ELI_FLAG_RO) ? 0 : 1;
8893bb6e0f0SRyan Libby 	error = g_access(cp, 1, dcw, 1);
890c58794deSPawel Jakub Dawidek 	if (error != 0) {
891c58794deSPawel Jakub Dawidek 		if (req != NULL) {
892c58794deSPawel Jakub Dawidek 			gctl_error(req, "Cannot access %s (error=%d).",
893c58794deSPawel Jakub Dawidek 			    bpp->name, error);
894c58794deSPawel Jakub Dawidek 		} else {
895c58794deSPawel Jakub Dawidek 			G_ELI_DEBUG(1, "Cannot access %s (error=%d).",
896c58794deSPawel Jakub Dawidek 			    bpp->name, error);
897c58794deSPawel Jakub Dawidek 		}
898c58794deSPawel Jakub Dawidek 		goto failed;
899c58794deSPawel Jakub Dawidek 	}
900c58794deSPawel Jakub Dawidek 
901c6a26d4cSPawel Jakub Dawidek 	/*
902c6a26d4cSPawel Jakub Dawidek 	 * Remember the keys in our softc structure.
903c6a26d4cSPawel Jakub Dawidek 	 */
904c6a26d4cSPawel Jakub Dawidek 	g_eli_mkey_propagate(sc, mkey);
905c6a26d4cSPawel Jakub Dawidek 
906c58794deSPawel Jakub Dawidek 	LIST_INIT(&sc->sc_workers);
907c58794deSPawel Jakub Dawidek 
908c58794deSPawel Jakub Dawidek 	threads = g_eli_threads;
909dd549194SPawel Jakub Dawidek 	if (threads == 0)
910dd549194SPawel Jakub Dawidek 		threads = mp_ncpus;
9110c879bd9SPawel Jakub Dawidek 	sc->sc_cpubind = (mp_ncpus > 1 && threads == mp_ncpus);
912c58794deSPawel Jakub Dawidek 	for (i = 0; i < threads; i++) {
913eba8f137SPawel Jakub Dawidek 		if (g_eli_cpu_is_disabled(i)) {
914eba8f137SPawel Jakub Dawidek 			G_ELI_DEBUG(1, "%s: CPU %u disabled, skipping.",
9151506db21SPawel Jakub Dawidek 			    bpp->name, i);
916eba8f137SPawel Jakub Dawidek 			continue;
917eba8f137SPawel Jakub Dawidek 		}
918c58794deSPawel Jakub Dawidek 		wr = malloc(sizeof(*wr), M_ELI, M_WAITOK | M_ZERO);
919c58794deSPawel Jakub Dawidek 		wr->w_softc = sc;
920c58794deSPawel Jakub Dawidek 		wr->w_number = i;
9215ad4a7c7SPawel Jakub Dawidek 		wr->w_active = TRUE;
922c58794deSPawel Jakub Dawidek 
9233ac01bc2SPawel Jakub Dawidek 		error = g_eli_newsession(wr);
924c58794deSPawel Jakub Dawidek 		if (error != 0) {
925c58794deSPawel Jakub Dawidek 			free(wr, M_ELI);
926c58794deSPawel Jakub Dawidek 			if (req != NULL) {
927c58794deSPawel Jakub Dawidek 				gctl_error(req, "Cannot set up crypto session "
928c58794deSPawel Jakub Dawidek 				    "for %s (error=%d).", bpp->name, error);
929c58794deSPawel Jakub Dawidek 			} else {
930c58794deSPawel Jakub Dawidek 				G_ELI_DEBUG(1, "Cannot set up crypto session "
931c58794deSPawel Jakub Dawidek 				    "for %s (error=%d).", bpp->name, error);
932c58794deSPawel Jakub Dawidek 			}
933c58794deSPawel Jakub Dawidek 			goto failed;
934c58794deSPawel Jakub Dawidek 		}
935c58794deSPawel Jakub Dawidek 
9363745c395SJulian Elischer 		error = kproc_create(g_eli_worker, wr, &wr->w_proc, 0, 0,
937c58794deSPawel Jakub Dawidek 		    "g_eli[%u] %s", i, bpp->name);
938c58794deSPawel Jakub Dawidek 		if (error != 0) {
9393ac01bc2SPawel Jakub Dawidek 			g_eli_freesession(wr);
940c58794deSPawel Jakub Dawidek 			free(wr, M_ELI);
941c58794deSPawel Jakub Dawidek 			if (req != NULL) {
942dddd1d53SPawel Jakub Dawidek 				gctl_error(req, "Cannot create kernel thread "
943dddd1d53SPawel Jakub Dawidek 				    "for %s (error=%d).", bpp->name, error);
944c58794deSPawel Jakub Dawidek 			} else {
945dddd1d53SPawel Jakub Dawidek 				G_ELI_DEBUG(1, "Cannot create kernel thread "
946dddd1d53SPawel Jakub Dawidek 				    "for %s (error=%d).", bpp->name, error);
947c58794deSPawel Jakub Dawidek 			}
948c58794deSPawel Jakub Dawidek 			goto failed;
949c58794deSPawel Jakub Dawidek 		}
950c58794deSPawel Jakub Dawidek 		LIST_INSERT_HEAD(&sc->sc_workers, wr, w_next);
951c58794deSPawel Jakub Dawidek 	}
952c58794deSPawel Jakub Dawidek 
953c58794deSPawel Jakub Dawidek 	/*
954c58794deSPawel Jakub Dawidek 	 * Create decrypted provider.
955c58794deSPawel Jakub Dawidek 	 */
956c58794deSPawel Jakub Dawidek 	pp = g_new_providerf(gp, "%s%s", bpp->name, G_ELI_SUFFIX);
957c6a26d4cSPawel Jakub Dawidek 	pp->mediasize = sc->sc_mediasize;
958c6a26d4cSPawel Jakub Dawidek 	pp->sectorsize = sc->sc_sectorsize;
959eaa3b919SPawel Jakub Dawidek 
960c58794deSPawel Jakub Dawidek 	g_error_provider(pp, 0);
961c58794deSPawel Jakub Dawidek 
962c58794deSPawel Jakub Dawidek 	G_ELI_DEBUG(0, "Device %s created.", pp->name);
963eaa3b919SPawel Jakub Dawidek 	G_ELI_DEBUG(0, "Encryption: %s %u", g_eli_algo2str(sc->sc_ealgo),
964eaa3b919SPawel Jakub Dawidek 	    sc->sc_ekeylen);
9655c420aaeSJohn Baldwin 	switch (sc->sc_ealgo) {
9665c420aaeSJohn Baldwin 	case CRYPTO_3DES_CBC:
9675c420aaeSJohn Baldwin 		gone_in(13,
9685c420aaeSJohn Baldwin 		    "support for GEOM_ELI volumes encrypted with 3des");
9695c420aaeSJohn Baldwin 		break;
9705c420aaeSJohn Baldwin 	case CRYPTO_BLF_CBC:
9715c420aaeSJohn Baldwin 		gone_in(13,
9725c420aaeSJohn Baldwin 		    "support for GEOM_ELI volumes encrypted with blowfish");
9735c420aaeSJohn Baldwin 		break;
9745c420aaeSJohn Baldwin 	}
9755c420aaeSJohn Baldwin 	if (sc->sc_flags & G_ELI_FLAG_AUTH) {
976eaa3b919SPawel Jakub Dawidek 		G_ELI_DEBUG(0, " Integrity: %s", g_eli_algo2str(sc->sc_aalgo));
9775c420aaeSJohn Baldwin 		switch (sc->sc_aalgo) {
9785c420aaeSJohn Baldwin 		case CRYPTO_MD5_HMAC:
9795c420aaeSJohn Baldwin 			gone_in(13,
9805c420aaeSJohn Baldwin 		    "support for GEOM_ELI volumes authenticated with hmac/md5");
9815c420aaeSJohn Baldwin 			break;
9825c420aaeSJohn Baldwin 		}
9835c420aaeSJohn Baldwin 	}
984c58794deSPawel Jakub Dawidek 	G_ELI_DEBUG(0, "    Crypto: %s",
985c58794deSPawel Jakub Dawidek 	    sc->sc_crypto == G_ELI_CRYPTO_SW ? "software" : "hardware");
986c58794deSPawel Jakub Dawidek 	return (gp);
987c58794deSPawel Jakub Dawidek failed:
988c58794deSPawel Jakub Dawidek 	mtx_lock(&sc->sc_queue_mtx);
989c58794deSPawel Jakub Dawidek 	sc->sc_flags |= G_ELI_FLAG_DESTROY;
990c58794deSPawel Jakub Dawidek 	wakeup(sc);
991c58794deSPawel Jakub Dawidek 	/*
992c58794deSPawel Jakub Dawidek 	 * Wait for kernel threads self destruction.
993c58794deSPawel Jakub Dawidek 	 */
994c58794deSPawel Jakub Dawidek 	while (!LIST_EMPTY(&sc->sc_workers)) {
995c58794deSPawel Jakub Dawidek 		msleep(&sc->sc_workers, &sc->sc_queue_mtx, PRIBIO,
996c58794deSPawel Jakub Dawidek 		    "geli:destroy", 0);
997c58794deSPawel Jakub Dawidek 	}
998c58794deSPawel Jakub Dawidek 	mtx_destroy(&sc->sc_queue_mtx);
999c58794deSPawel Jakub Dawidek 	if (cp->provider != NULL) {
1000c58794deSPawel Jakub Dawidek 		if (cp->acr == 1)
10013bb6e0f0SRyan Libby 			g_access(cp, -1, -dcw, -1);
1002c58794deSPawel Jakub Dawidek 		g_detach(cp);
1003c58794deSPawel Jakub Dawidek 	}
1004c58794deSPawel Jakub Dawidek 	g_destroy_consumer(cp);
1005c58794deSPawel Jakub Dawidek 	g_destroy_geom(gp);
10061e09ff3dSPawel Jakub Dawidek 	g_eli_key_destroy(sc);
1007c58794deSPawel Jakub Dawidek 	bzero(sc, sizeof(*sc));
1008c58794deSPawel Jakub Dawidek 	free(sc, M_ELI);
1009c58794deSPawel Jakub Dawidek 	return (NULL);
1010c58794deSPawel Jakub Dawidek }
1011c58794deSPawel Jakub Dawidek 
1012c58794deSPawel Jakub Dawidek int
1013c58794deSPawel Jakub Dawidek g_eli_destroy(struct g_eli_softc *sc, boolean_t force)
1014c58794deSPawel Jakub Dawidek {
1015c58794deSPawel Jakub Dawidek 	struct g_geom *gp;
1016c58794deSPawel Jakub Dawidek 	struct g_provider *pp;
1017c58794deSPawel Jakub Dawidek 
1018c58794deSPawel Jakub Dawidek 	g_topology_assert();
1019c58794deSPawel Jakub Dawidek 
1020c58794deSPawel Jakub Dawidek 	if (sc == NULL)
1021c58794deSPawel Jakub Dawidek 		return (ENXIO);
1022c58794deSPawel Jakub Dawidek 
1023c58794deSPawel Jakub Dawidek 	gp = sc->sc_geom;
1024c58794deSPawel Jakub Dawidek 	pp = LIST_FIRST(&gp->provider);
1025c58794deSPawel Jakub Dawidek 	if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
1026c58794deSPawel Jakub Dawidek 		if (force) {
1027c58794deSPawel Jakub Dawidek 			G_ELI_DEBUG(1, "Device %s is still open, so it "
102898645006SChristian Brueffer 			    "cannot be definitely removed.", pp->name);
102919351a14SAlexander Motin 			sc->sc_flags |= G_ELI_FLAG_RW_DETACH;
103019351a14SAlexander Motin 			gp->access = g_eli_access;
103119351a14SAlexander Motin 			g_wither_provider(pp, ENXIO);
103219351a14SAlexander Motin 			return (EBUSY);
1033c58794deSPawel Jakub Dawidek 		} else {
1034c58794deSPawel Jakub Dawidek 			G_ELI_DEBUG(1,
1035c58794deSPawel Jakub Dawidek 			    "Device %s is still open (r%dw%de%d).", pp->name,
1036c58794deSPawel Jakub Dawidek 			    pp->acr, pp->acw, pp->ace);
1037c58794deSPawel Jakub Dawidek 			return (EBUSY);
1038c58794deSPawel Jakub Dawidek 		}
1039c58794deSPawel Jakub Dawidek 	}
1040c58794deSPawel Jakub Dawidek 
1041c58794deSPawel Jakub Dawidek 	mtx_lock(&sc->sc_queue_mtx);
1042c58794deSPawel Jakub Dawidek 	sc->sc_flags |= G_ELI_FLAG_DESTROY;
1043c58794deSPawel Jakub Dawidek 	wakeup(sc);
1044c58794deSPawel Jakub Dawidek 	while (!LIST_EMPTY(&sc->sc_workers)) {
1045c58794deSPawel Jakub Dawidek 		msleep(&sc->sc_workers, &sc->sc_queue_mtx, PRIBIO,
1046c58794deSPawel Jakub Dawidek 		    "geli:destroy", 0);
1047c58794deSPawel Jakub Dawidek 	}
1048c58794deSPawel Jakub Dawidek 	mtx_destroy(&sc->sc_queue_mtx);
1049c58794deSPawel Jakub Dawidek 	gp->softc = NULL;
10501e09ff3dSPawel Jakub Dawidek 	g_eli_key_destroy(sc);
1051c58794deSPawel Jakub Dawidek 	bzero(sc, sizeof(*sc));
1052c58794deSPawel Jakub Dawidek 	free(sc, M_ELI);
1053c58794deSPawel Jakub Dawidek 
1054c58794deSPawel Jakub Dawidek 	G_ELI_DEBUG(0, "Device %s destroyed.", gp->name);
1055c58794deSPawel Jakub Dawidek 	g_wither_geom_close(gp, ENXIO);
1056c58794deSPawel Jakub Dawidek 
1057c58794deSPawel Jakub Dawidek 	return (0);
1058c58794deSPawel Jakub Dawidek }
1059c58794deSPawel Jakub Dawidek 
1060c58794deSPawel Jakub Dawidek static int
1061c58794deSPawel Jakub Dawidek g_eli_destroy_geom(struct gctl_req *req __unused,
1062c58794deSPawel Jakub Dawidek     struct g_class *mp __unused, struct g_geom *gp)
1063c58794deSPawel Jakub Dawidek {
1064c58794deSPawel Jakub Dawidek 	struct g_eli_softc *sc;
1065c58794deSPawel Jakub Dawidek 
1066c58794deSPawel Jakub Dawidek 	sc = gp->softc;
10675ad4a7c7SPawel Jakub Dawidek 	return (g_eli_destroy(sc, FALSE));
1068c58794deSPawel Jakub Dawidek }
1069c58794deSPawel Jakub Dawidek 
10709af2131bSPawel Jakub Dawidek static int
10719af2131bSPawel Jakub Dawidek g_eli_keyfiles_load(struct hmac_ctx *ctx, const char *provider)
10729af2131bSPawel Jakub Dawidek {
10731e189c08SMarcel Moolenaar 	u_char *keyfile, *data;
10749af2131bSPawel Jakub Dawidek 	char *file, name[64];
10751e189c08SMarcel Moolenaar 	size_t size;
10769af2131bSPawel Jakub Dawidek 	int i;
10779af2131bSPawel Jakub Dawidek 
10789af2131bSPawel Jakub Dawidek 	for (i = 0; ; i++) {
10799af2131bSPawel Jakub Dawidek 		snprintf(name, sizeof(name), "%s:geli_keyfile%d", provider, i);
10809af2131bSPawel Jakub Dawidek 		keyfile = preload_search_by_type(name);
1081edaa9008SPawel Jakub Dawidek 		if (keyfile == NULL && i == 0) {
1082edaa9008SPawel Jakub Dawidek 			/*
1083edaa9008SPawel Jakub Dawidek 			 * If there is only one keyfile, allow simpler name.
1084edaa9008SPawel Jakub Dawidek 			 */
1085edaa9008SPawel Jakub Dawidek 			snprintf(name, sizeof(name), "%s:geli_keyfile", provider);
1086edaa9008SPawel Jakub Dawidek 			keyfile = preload_search_by_type(name);
1087edaa9008SPawel Jakub Dawidek 		}
10889af2131bSPawel Jakub Dawidek 		if (keyfile == NULL)
10899af2131bSPawel Jakub Dawidek 			return (i);	/* Return number of loaded keyfiles. */
10901e189c08SMarcel Moolenaar 		data = preload_fetch_addr(keyfile);
10919af2131bSPawel Jakub Dawidek 		if (data == NULL) {
10929af2131bSPawel Jakub Dawidek 			G_ELI_DEBUG(0, "Cannot find key file data for %s.",
10939af2131bSPawel Jakub Dawidek 			    name);
10949af2131bSPawel Jakub Dawidek 			return (0);
10959af2131bSPawel Jakub Dawidek 		}
10961e189c08SMarcel Moolenaar 		size = preload_fetch_size(keyfile);
10971e189c08SMarcel Moolenaar 		if (size == 0) {
10989af2131bSPawel Jakub Dawidek 			G_ELI_DEBUG(0, "Cannot find key file size for %s.",
10999af2131bSPawel Jakub Dawidek 			    name);
11009af2131bSPawel Jakub Dawidek 			return (0);
11019af2131bSPawel Jakub Dawidek 		}
11029af2131bSPawel Jakub Dawidek 		file = preload_search_info(keyfile, MODINFO_NAME);
11039af2131bSPawel Jakub Dawidek 		if (file == NULL) {
11049af2131bSPawel Jakub Dawidek 			G_ELI_DEBUG(0, "Cannot find key file name for %s.",
11059af2131bSPawel Jakub Dawidek 			    name);
11069af2131bSPawel Jakub Dawidek 			return (0);
11079af2131bSPawel Jakub Dawidek 		}
11089af2131bSPawel Jakub Dawidek 		G_ELI_DEBUG(1, "Loaded keyfile %s for %s (type: %s).", file,
11099af2131bSPawel Jakub Dawidek 		    provider, name);
11101e189c08SMarcel Moolenaar 		g_eli_crypto_hmac_update(ctx, data, size);
11119af2131bSPawel Jakub Dawidek 	}
11129af2131bSPawel Jakub Dawidek }
11139af2131bSPawel Jakub Dawidek 
11149af2131bSPawel Jakub Dawidek static void
11159af2131bSPawel Jakub Dawidek g_eli_keyfiles_clear(const char *provider)
11169af2131bSPawel Jakub Dawidek {
11171e189c08SMarcel Moolenaar 	u_char *keyfile, *data;
11189af2131bSPawel Jakub Dawidek 	char name[64];
11191e189c08SMarcel Moolenaar 	size_t size;
11209af2131bSPawel Jakub Dawidek 	int i;
11219af2131bSPawel Jakub Dawidek 
11229af2131bSPawel Jakub Dawidek 	for (i = 0; ; i++) {
11239af2131bSPawel Jakub Dawidek 		snprintf(name, sizeof(name), "%s:geli_keyfile%d", provider, i);
11249af2131bSPawel Jakub Dawidek 		keyfile = preload_search_by_type(name);
11259af2131bSPawel Jakub Dawidek 		if (keyfile == NULL)
11269af2131bSPawel Jakub Dawidek 			return;
11271e189c08SMarcel Moolenaar 		data = preload_fetch_addr(keyfile);
11281e189c08SMarcel Moolenaar 		size = preload_fetch_size(keyfile);
11291e189c08SMarcel Moolenaar 		if (data != NULL && size != 0)
11301e189c08SMarcel Moolenaar 			bzero(data, size);
11319af2131bSPawel Jakub Dawidek 	}
11329af2131bSPawel Jakub Dawidek }
11339af2131bSPawel Jakub Dawidek 
1134c58794deSPawel Jakub Dawidek /*
1135c58794deSPawel Jakub Dawidek  * Tasting is only made on boot.
1136c58794deSPawel Jakub Dawidek  * We detect providers which should be attached before root is mounted.
1137c58794deSPawel Jakub Dawidek  */
1138c58794deSPawel Jakub Dawidek static struct g_geom *
1139c58794deSPawel Jakub Dawidek g_eli_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
1140c58794deSPawel Jakub Dawidek {
1141c58794deSPawel Jakub Dawidek 	struct g_eli_metadata md;
1142c58794deSPawel Jakub Dawidek 	struct g_geom *gp;
1143c58794deSPawel Jakub Dawidek 	struct hmac_ctx ctx;
1144c58794deSPawel Jakub Dawidek 	char passphrase[256];
1145c58794deSPawel Jakub Dawidek 	u_char key[G_ELI_USERKEYLEN], mkey[G_ELI_DATAIVKEYLEN];
11463453dc72SMariusz Zaborski 	u_int i, nkey, nkeyfiles, tries, showpass;
1147c58794deSPawel Jakub Dawidek 	int error;
1148ec5c0e5bSAllan Jude         struct keybuf *keybuf;
1149c58794deSPawel Jakub Dawidek 
1150c58794deSPawel Jakub Dawidek 	g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
1151c58794deSPawel Jakub Dawidek 	g_topology_assert();
1152c58794deSPawel Jakub Dawidek 
1153df3aed4fSPawel Jakub Dawidek 	if (root_mounted() || g_eli_tries == 0)
1154c58794deSPawel Jakub Dawidek 		return (NULL);
1155c58794deSPawel Jakub Dawidek 
1156c58794deSPawel Jakub Dawidek 	G_ELI_DEBUG(3, "Tasting %s.", pp->name);
1157c58794deSPawel Jakub Dawidek 
1158c58794deSPawel Jakub Dawidek 	error = g_eli_read_metadata(mp, pp, &md);
1159c58794deSPawel Jakub Dawidek 	if (error != 0)
1160c58794deSPawel Jakub Dawidek 		return (NULL);
1161c58794deSPawel Jakub Dawidek 	gp = NULL;
1162c58794deSPawel Jakub Dawidek 
1163c58794deSPawel Jakub Dawidek 	if (strcmp(md.md_magic, G_ELI_MAGIC) != 0)
1164c58794deSPawel Jakub Dawidek 		return (NULL);
1165c58794deSPawel Jakub Dawidek 	if (md.md_version > G_ELI_VERSION) {
1166c58794deSPawel Jakub Dawidek 		printf("geom_eli.ko module is too old to handle %s.\n",
1167c58794deSPawel Jakub Dawidek 		    pp->name);
1168c58794deSPawel Jakub Dawidek 		return (NULL);
1169c58794deSPawel Jakub Dawidek 	}
1170c58794deSPawel Jakub Dawidek 	if (md.md_provsize != pp->mediasize)
1171c58794deSPawel Jakub Dawidek 		return (NULL);
1172c58794deSPawel Jakub Dawidek 	/* Should we attach it on boot? */
1173c81929d3SKyle Evans 	if (!(md.md_flags & G_ELI_FLAG_BOOT) &&
1174c81929d3SKyle Evans 	    !(md.md_flags & G_ELI_FLAG_GELIBOOT))
1175c58794deSPawel Jakub Dawidek 		return (NULL);
1176c58794deSPawel Jakub Dawidek 	if (md.md_keys == 0x00) {
1177c58794deSPawel Jakub Dawidek 		G_ELI_DEBUG(0, "No valid keys on %s.", pp->name);
1178c58794deSPawel Jakub Dawidek 		return (NULL);
1179c58794deSPawel Jakub Dawidek 	}
11809af2131bSPawel Jakub Dawidek 	if (md.md_iterations == -1) {
11819af2131bSPawel Jakub Dawidek 		/* If there is no passphrase, we try only once. */
11829af2131bSPawel Jakub Dawidek 		tries = 1;
11839af2131bSPawel Jakub Dawidek 	} else {
11849af2131bSPawel Jakub Dawidek 		/* Ask for the passphrase no more than g_eli_tries times. */
11859af2131bSPawel Jakub Dawidek 		tries = g_eli_tries;
11869af2131bSPawel Jakub Dawidek 	}
11879af2131bSPawel Jakub Dawidek 
1188ec5c0e5bSAllan Jude         if ((keybuf = get_keybuf()) != NULL) {
1189ec5c0e5bSAllan Jude                 /* Scan the key buffer, try all GELI keys. */
1190ec5c0e5bSAllan Jude                 for (i = 0; i < keybuf->kb_nents; i++) {
1191ec5c0e5bSAllan Jude                          if (keybuf->kb_ents[i].ke_type == KEYBUF_TYPE_GELI) {
1192ec5c0e5bSAllan Jude                                  memcpy(key, keybuf->kb_ents[i].ke_data,
1193ec5c0e5bSAllan Jude                                      sizeof(key));
1194ec5c0e5bSAllan Jude 
119531f7586dSMariusz Zaborski                                  if (g_eli_mkey_decrypt_any(&md, key,
1196ec5c0e5bSAllan Jude                                      mkey, &nkey) == 0 ) {
1197ec5c0e5bSAllan Jude                                          explicit_bzero(key, sizeof(key));
1198ec5c0e5bSAllan Jude                                          goto have_key;
1199ec5c0e5bSAllan Jude                                  }
1200ec5c0e5bSAllan Jude                          }
1201ec5c0e5bSAllan Jude                 }
1202ec5c0e5bSAllan Jude         }
1203ec5c0e5bSAllan Jude 
1204835c4dd4SColin Percival         for (i = 0; i <= tries; i++) {
12059af2131bSPawel Jakub Dawidek                 g_eli_crypto_hmac_init(&ctx, NULL, 0);
1206c58794deSPawel Jakub Dawidek 
1207c58794deSPawel Jakub Dawidek                 /*
12089af2131bSPawel Jakub Dawidek                  * Load all key files.
1209c58794deSPawel Jakub Dawidek                  */
12109af2131bSPawel Jakub Dawidek                 nkeyfiles = g_eli_keyfiles_load(&ctx, pp->name);
12119af2131bSPawel Jakub Dawidek 
12129af2131bSPawel Jakub Dawidek                 if (nkeyfiles == 0 && md.md_iterations == -1) {
12139af2131bSPawel Jakub Dawidek                         /*
12149af2131bSPawel Jakub Dawidek                          * No key files and no passphrase, something is
12159af2131bSPawel Jakub Dawidek                          * definitely wrong here.
12169af2131bSPawel Jakub Dawidek                          * geli(8) doesn't allow for such situation, so assume
12179af2131bSPawel Jakub Dawidek                          * that there was really no passphrase and in that case
12189af2131bSPawel Jakub Dawidek                          * key files are no properly defined in loader.conf.
12199af2131bSPawel Jakub Dawidek                          */
12209af2131bSPawel Jakub Dawidek                         G_ELI_DEBUG(0,
12219af2131bSPawel Jakub Dawidek                             "Found no key files in loader.conf for %s.",
12229af2131bSPawel Jakub Dawidek                             pp->name);
12239af2131bSPawel Jakub Dawidek                         return (NULL);
12249af2131bSPawel Jakub Dawidek                 }
12259af2131bSPawel Jakub Dawidek 
12269af2131bSPawel Jakub Dawidek                 /* Ask for the passphrase if defined. */
12279af2131bSPawel Jakub Dawidek                 if (md.md_iterations >= 0) {
1228835c4dd4SColin Percival                         /* Try first with cached passphrase. */
1229835c4dd4SColin Percival                         if (i == 0) {
1230835c4dd4SColin Percival                                 if (!g_eli_boot_passcache)
1231835c4dd4SColin Percival                                         continue;
1232835c4dd4SColin Percival                                 memcpy(passphrase, cached_passphrase,
1233835c4dd4SColin Percival                                     sizeof(passphrase));
1234835c4dd4SColin Percival                         } else {
1235c58794deSPawel Jakub Dawidek                                 printf("Enter passphrase for %s: ", pp->name);
12363453dc72SMariusz Zaborski 				showpass = g_eli_visible_passphrase;
12373453dc72SMariusz Zaborski 				if ((md.md_flags & G_ELI_FLAG_GELIDISPLAYPASS) != 0)
12383453dc72SMariusz Zaborski 					showpass = GETS_ECHOPASS;
1239f6ce353eSAndriy Gapon                                 cngets(passphrase, sizeof(passphrase),
12403453dc72SMariusz Zaborski 				    showpass);
1241835c4dd4SColin Percival                                 memcpy(cached_passphrase, passphrase,
1242835c4dd4SColin Percival                                     sizeof(passphrase));
1243835c4dd4SColin Percival                         }
12449af2131bSPawel Jakub Dawidek                 }
12459af2131bSPawel Jakub Dawidek 
1246c58794deSPawel Jakub Dawidek                 /*
1247c58794deSPawel Jakub Dawidek                  * Prepare Derived-Key from the user passphrase.
1248c58794deSPawel Jakub Dawidek                  */
1249c58794deSPawel Jakub Dawidek                 if (md.md_iterations == 0) {
1250c58794deSPawel Jakub Dawidek                         g_eli_crypto_hmac_update(&ctx, md.md_salt,
1251c58794deSPawel Jakub Dawidek                             sizeof(md.md_salt));
1252c58794deSPawel Jakub Dawidek                         g_eli_crypto_hmac_update(&ctx, passphrase,
1253c58794deSPawel Jakub Dawidek                             strlen(passphrase));
1254ec5c0e5bSAllan Jude                         explicit_bzero(passphrase, sizeof(passphrase));
12559af2131bSPawel Jakub Dawidek                 } else if (md.md_iterations > 0) {
1256c58794deSPawel Jakub Dawidek                         u_char dkey[G_ELI_USERKEYLEN];
1257c58794deSPawel Jakub Dawidek 
1258c58794deSPawel Jakub Dawidek                         pkcs5v2_genkey(dkey, sizeof(dkey), md.md_salt,
1259c58794deSPawel Jakub Dawidek                             sizeof(md.md_salt), passphrase, md.md_iterations);
12605527ecd9SPawel Jakub Dawidek                         bzero(passphrase, sizeof(passphrase));
1261c58794deSPawel Jakub Dawidek                         g_eli_crypto_hmac_update(&ctx, dkey, sizeof(dkey));
1262ec5c0e5bSAllan Jude                         explicit_bzero(dkey, sizeof(dkey));
1263c58794deSPawel Jakub Dawidek                 }
12649af2131bSPawel Jakub Dawidek 
1265c58794deSPawel Jakub Dawidek                 g_eli_crypto_hmac_final(&ctx, key, 0);
1266c58794deSPawel Jakub Dawidek 
1267c58794deSPawel Jakub Dawidek                 /*
1268c58794deSPawel Jakub Dawidek                  * Decrypt Master-Key.
1269c58794deSPawel Jakub Dawidek                  */
127031f7586dSMariusz Zaborski                 error = g_eli_mkey_decrypt_any(&md, key, mkey, &nkey);
1271c58794deSPawel Jakub Dawidek                 bzero(key, sizeof(key));
1272c58794deSPawel Jakub Dawidek                 if (error == -1) {
1273835c4dd4SColin Percival                         if (i == tries) {
12749af2131bSPawel Jakub Dawidek                                 G_ELI_DEBUG(0,
12759af2131bSPawel Jakub Dawidek                                     "Wrong key for %s. No tries left.",
12769af2131bSPawel Jakub Dawidek                                     pp->name);
12779af2131bSPawel Jakub Dawidek                                 g_eli_keyfiles_clear(pp->name);
12789af2131bSPawel Jakub Dawidek                                 return (NULL);
1279c58794deSPawel Jakub Dawidek                         }
1280835c4dd4SColin Percival                         if (i > 0) {
1281835c4dd4SColin Percival                                 G_ELI_DEBUG(0,
1282835c4dd4SColin Percival                                     "Wrong key for %s. Tries left: %u.",
1283835c4dd4SColin Percival                                     pp->name, tries - i);
1284835c4dd4SColin Percival                         }
1285c58794deSPawel Jakub Dawidek                         /* Try again. */
1286c58794deSPawel Jakub Dawidek                         continue;
1287c58794deSPawel Jakub Dawidek                 } else if (error > 0) {
1288038c55adSPawel Jakub Dawidek                         G_ELI_DEBUG(0,
1289038c55adSPawel Jakub Dawidek                             "Cannot decrypt Master Key for %s (error=%d).",
1290c58794deSPawel Jakub Dawidek                             pp->name, error);
12919af2131bSPawel Jakub Dawidek                         g_eli_keyfiles_clear(pp->name);
1292c58794deSPawel Jakub Dawidek                         return (NULL);
1293c58794deSPawel Jakub Dawidek                 }
1294ebd05adaSBrad Davis                 g_eli_keyfiles_clear(pp->name);
1295c58794deSPawel Jakub Dawidek                 G_ELI_DEBUG(1, "Using Master Key %u for %s.", nkey, pp->name);
1296c58794deSPawel Jakub Dawidek                 break;
1297c58794deSPawel Jakub Dawidek         }
1298ec5c0e5bSAllan Jude have_key:
1299c58794deSPawel Jakub Dawidek 
1300c58794deSPawel Jakub Dawidek 	/*
1301c58794deSPawel Jakub Dawidek 	 * We have correct key, let's attach provider.
1302c58794deSPawel Jakub Dawidek 	 */
1303c58794deSPawel Jakub Dawidek 	gp = g_eli_create(NULL, mp, pp, &md, mkey, nkey);
1304c58794deSPawel Jakub Dawidek 	bzero(mkey, sizeof(mkey));
1305c58794deSPawel Jakub Dawidek 	bzero(&md, sizeof(md));
1306c58794deSPawel Jakub Dawidek 	if (gp == NULL) {
1307c58794deSPawel Jakub Dawidek 		G_ELI_DEBUG(0, "Cannot create device %s%s.", pp->name,
1308c58794deSPawel Jakub Dawidek 		    G_ELI_SUFFIX);
1309c58794deSPawel Jakub Dawidek 		return (NULL);
1310c58794deSPawel Jakub Dawidek 	}
1311c58794deSPawel Jakub Dawidek 	return (gp);
1312c58794deSPawel Jakub Dawidek }
1313c58794deSPawel Jakub Dawidek 
1314c58794deSPawel Jakub Dawidek static void
1315c58794deSPawel Jakub Dawidek g_eli_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
1316c58794deSPawel Jakub Dawidek     struct g_consumer *cp, struct g_provider *pp)
1317c58794deSPawel Jakub Dawidek {
1318c58794deSPawel Jakub Dawidek 	struct g_eli_softc *sc;
1319c58794deSPawel Jakub Dawidek 
1320c58794deSPawel Jakub Dawidek 	g_topology_assert();
1321c58794deSPawel Jakub Dawidek 	sc = gp->softc;
1322c58794deSPawel Jakub Dawidek 	if (sc == NULL)
1323c58794deSPawel Jakub Dawidek 		return;
1324c58794deSPawel Jakub Dawidek 	if (pp != NULL || cp != NULL)
1325c58794deSPawel Jakub Dawidek 		return;	/* Nothing here. */
13261e09ff3dSPawel Jakub Dawidek 
1327743437c4SAndrey V. Elsukov 	sbuf_printf(sb, "%s<KeysTotal>%ju</KeysTotal>\n", indent,
13281e09ff3dSPawel Jakub Dawidek 	    (uintmax_t)sc->sc_ekeys_total);
1329743437c4SAndrey V. Elsukov 	sbuf_printf(sb, "%s<KeysAllocated>%ju</KeysAllocated>\n", indent,
13301e09ff3dSPawel Jakub Dawidek 	    (uintmax_t)sc->sc_ekeys_allocated);
1331ea35a2ecSPawel Jakub Dawidek 	sbuf_printf(sb, "%s<Flags>", indent);
1332ea35a2ecSPawel Jakub Dawidek 	if (sc->sc_flags == 0)
133349ee0fceSAlexander Motin 		sbuf_cat(sb, "NONE");
1334ea35a2ecSPawel Jakub Dawidek 	else {
1335ea35a2ecSPawel Jakub Dawidek 		int first = 1;
1336ea35a2ecSPawel Jakub Dawidek 
1337ea35a2ecSPawel Jakub Dawidek #define ADD_FLAG(flag, name)	do {					\
1338eaa3b919SPawel Jakub Dawidek 	if (sc->sc_flags & (flag)) {					\
1339ea35a2ecSPawel Jakub Dawidek 		if (!first)						\
134049ee0fceSAlexander Motin 			sbuf_cat(sb, ", ");				\
1341ea35a2ecSPawel Jakub Dawidek 		else							\
1342ea35a2ecSPawel Jakub Dawidek 			first = 0;					\
134349ee0fceSAlexander Motin 		sbuf_cat(sb, name);					\
1344ea35a2ecSPawel Jakub Dawidek 	}								\
1345ea35a2ecSPawel Jakub Dawidek } while (0)
13465ad4a7c7SPawel Jakub Dawidek 		ADD_FLAG(G_ELI_FLAG_SUSPEND, "SUSPEND");
1347c6a26d4cSPawel Jakub Dawidek 		ADD_FLAG(G_ELI_FLAG_SINGLE_KEY, "SINGLE-KEY");
13482bd4ade6SPawel Jakub Dawidek 		ADD_FLAG(G_ELI_FLAG_NATIVE_BYTE_ORDER, "NATIVE-BYTE-ORDER");
1349ea35a2ecSPawel Jakub Dawidek 		ADD_FLAG(G_ELI_FLAG_ONETIME, "ONETIME");
1350ea35a2ecSPawel Jakub Dawidek 		ADD_FLAG(G_ELI_FLAG_BOOT, "BOOT");
1351ea35a2ecSPawel Jakub Dawidek 		ADD_FLAG(G_ELI_FLAG_WO_DETACH, "W-DETACH");
1352ea35a2ecSPawel Jakub Dawidek 		ADD_FLAG(G_ELI_FLAG_RW_DETACH, "RW-DETACH");
1353eaa3b919SPawel Jakub Dawidek 		ADD_FLAG(G_ELI_FLAG_AUTH, "AUTH");
1354ea35a2ecSPawel Jakub Dawidek 		ADD_FLAG(G_ELI_FLAG_WOPEN, "W-OPEN");
1355ea35a2ecSPawel Jakub Dawidek 		ADD_FLAG(G_ELI_FLAG_DESTROY, "DESTROY");
135685059016SPawel Jakub Dawidek 		ADD_FLAG(G_ELI_FLAG_RO, "READ-ONLY");
135746e34470SPawel Jakub Dawidek 		ADD_FLAG(G_ELI_FLAG_NODELETE, "NODELETE");
1358d8736625SAllan Jude 		ADD_FLAG(G_ELI_FLAG_GELIBOOT, "GELIBOOT");
13593453dc72SMariusz Zaborski 		ADD_FLAG(G_ELI_FLAG_GELIDISPLAYPASS, "GELIDISPLAYPASS");
13602f07cdf8SPawel Jakub Dawidek 		ADD_FLAG(G_ELI_FLAG_AUTORESIZE, "AUTORESIZE");
1361ea35a2ecSPawel Jakub Dawidek #undef  ADD_FLAG
1362ea35a2ecSPawel Jakub Dawidek 	}
136349ee0fceSAlexander Motin 	sbuf_cat(sb, "</Flags>\n");
1364ea35a2ecSPawel Jakub Dawidek 
1365eaa3b919SPawel Jakub Dawidek 	if (!(sc->sc_flags & G_ELI_FLAG_ONETIME)) {
1366ea35a2ecSPawel Jakub Dawidek 		sbuf_printf(sb, "%s<UsedKey>%u</UsedKey>\n", indent,
1367ea35a2ecSPawel Jakub Dawidek 		    sc->sc_nkey);
1368ea35a2ecSPawel Jakub Dawidek 	}
13691f8c92e6SPawel Jakub Dawidek 	sbuf_printf(sb, "%s<Version>%u</Version>\n", indent, sc->sc_version);
1370c58794deSPawel Jakub Dawidek 	sbuf_printf(sb, "%s<Crypto>", indent);
1371c58794deSPawel Jakub Dawidek 	switch (sc->sc_crypto) {
1372c58794deSPawel Jakub Dawidek 	case G_ELI_CRYPTO_HW:
137349ee0fceSAlexander Motin 		sbuf_cat(sb, "hardware");
1374c58794deSPawel Jakub Dawidek 		break;
1375c58794deSPawel Jakub Dawidek 	case G_ELI_CRYPTO_SW:
137649ee0fceSAlexander Motin 		sbuf_cat(sb, "software");
1377c58794deSPawel Jakub Dawidek 		break;
1378c58794deSPawel Jakub Dawidek 	default:
137949ee0fceSAlexander Motin 		sbuf_cat(sb, "UNKNOWN");
1380c58794deSPawel Jakub Dawidek 		break;
1381c58794deSPawel Jakub Dawidek 	}
138249ee0fceSAlexander Motin 	sbuf_cat(sb, "</Crypto>\n");
1383eaa3b919SPawel Jakub Dawidek 	if (sc->sc_flags & G_ELI_FLAG_AUTH) {
1384eaa3b919SPawel Jakub Dawidek 		sbuf_printf(sb,
1385eaa3b919SPawel Jakub Dawidek 		    "%s<AuthenticationAlgorithm>%s</AuthenticationAlgorithm>\n",
1386eaa3b919SPawel Jakub Dawidek 		    indent, g_eli_algo2str(sc->sc_aalgo));
1387eaa3b919SPawel Jakub Dawidek 	}
1388eaa3b919SPawel Jakub Dawidek 	sbuf_printf(sb, "%s<KeyLength>%u</KeyLength>\n", indent,
1389eaa3b919SPawel Jakub Dawidek 	    sc->sc_ekeylen);
1390038c55adSPawel Jakub Dawidek 	sbuf_printf(sb, "%s<EncryptionAlgorithm>%s</EncryptionAlgorithm>\n",
1391038c55adSPawel Jakub Dawidek 	    indent, g_eli_algo2str(sc->sc_ealgo));
1392d8d61ef8SPawel Jakub Dawidek 	sbuf_printf(sb, "%s<State>%s</State>\n", indent,
1393d8d61ef8SPawel Jakub Dawidek 	    (sc->sc_flags & G_ELI_FLAG_SUSPEND) ? "SUSPENDED" : "ACTIVE");
1394c58794deSPawel Jakub Dawidek }
1395c58794deSPawel Jakub Dawidek 
1396c5d387d0SPawel Jakub Dawidek static void
1397c5d387d0SPawel Jakub Dawidek g_eli_shutdown_pre_sync(void *arg, int howto)
1398c5d387d0SPawel Jakub Dawidek {
1399c5d387d0SPawel Jakub Dawidek 	struct g_class *mp;
1400c5d387d0SPawel Jakub Dawidek 	struct g_geom *gp, *gp2;
1401c5d387d0SPawel Jakub Dawidek 	struct g_provider *pp;
1402c5d387d0SPawel Jakub Dawidek 	struct g_eli_softc *sc;
1403c5d387d0SPawel Jakub Dawidek 	int error;
1404c5d387d0SPawel Jakub Dawidek 
1405c5d387d0SPawel Jakub Dawidek 	mp = arg;
1406c5d387d0SPawel Jakub Dawidek 	g_topology_lock();
1407c5d387d0SPawel Jakub Dawidek 	LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) {
1408c5d387d0SPawel Jakub Dawidek 		sc = gp->softc;
1409c5d387d0SPawel Jakub Dawidek 		if (sc == NULL)
1410c5d387d0SPawel Jakub Dawidek 			continue;
1411c5d387d0SPawel Jakub Dawidek 		pp = LIST_FIRST(&gp->provider);
1412c5d387d0SPawel Jakub Dawidek 		KASSERT(pp != NULL, ("No provider? gp=%p (%s)", gp, gp->name));
1413c5d387d0SPawel Jakub Dawidek 		if (pp->acr + pp->acw + pp->ace == 0)
14145ad4a7c7SPawel Jakub Dawidek 			error = g_eli_destroy(sc, TRUE);
1415c5d387d0SPawel Jakub Dawidek 		else {
1416c5d387d0SPawel Jakub Dawidek 			sc->sc_flags |= G_ELI_FLAG_RW_DETACH;
1417c5d387d0SPawel Jakub Dawidek 			gp->access = g_eli_access;
1418c5d387d0SPawel Jakub Dawidek 		}
1419c5d387d0SPawel Jakub Dawidek 	}
1420c5d387d0SPawel Jakub Dawidek 	g_topology_unlock();
1421c5d387d0SPawel Jakub Dawidek }
1422c5d387d0SPawel Jakub Dawidek 
1423c5d387d0SPawel Jakub Dawidek static void
1424c5d387d0SPawel Jakub Dawidek g_eli_init(struct g_class *mp)
1425c5d387d0SPawel Jakub Dawidek {
1426c5d387d0SPawel Jakub Dawidek 
1427c5d387d0SPawel Jakub Dawidek 	g_eli_pre_sync = EVENTHANDLER_REGISTER(shutdown_pre_sync,
1428c5d387d0SPawel Jakub Dawidek 	    g_eli_shutdown_pre_sync, mp, SHUTDOWN_PRI_FIRST);
1429c5d387d0SPawel Jakub Dawidek 	if (g_eli_pre_sync == NULL)
1430c5d387d0SPawel Jakub Dawidek 		G_ELI_DEBUG(0, "Warning! Cannot register shutdown event.");
1431c5d387d0SPawel Jakub Dawidek }
1432c5d387d0SPawel Jakub Dawidek 
1433c5d387d0SPawel Jakub Dawidek static void
1434c5d387d0SPawel Jakub Dawidek g_eli_fini(struct g_class *mp)
1435c5d387d0SPawel Jakub Dawidek {
1436c5d387d0SPawel Jakub Dawidek 
1437c5d387d0SPawel Jakub Dawidek 	if (g_eli_pre_sync != NULL)
1438c5d387d0SPawel Jakub Dawidek 		EVENTHANDLER_DEREGISTER(shutdown_pre_sync, g_eli_pre_sync);
1439c5d387d0SPawel Jakub Dawidek }
1440c5d387d0SPawel Jakub Dawidek 
1441c58794deSPawel Jakub Dawidek DECLARE_GEOM_CLASS(g_eli_class, g_eli);
1442f6829a05SYaroslav Tykhiy MODULE_DEPEND(g_eli, crypto, 1, 1, 1);
144374d6c131SKyle Evans MODULE_VERSION(geom_eli, 0);
1444