xref: /freebsd/sys/geom/eli/g_eli.c (revision fdce57a04219d7a36c6646950fde6c8bcd97c044)
1c58794deSPawel Jakub Dawidek /*-
21e09ff3dSPawel Jakub Dawidek  * Copyright (c) 2005-2011 Pawel Jakub Dawidek <pawel@dawidek.net>
3c58794deSPawel Jakub Dawidek  * All rights reserved.
4c58794deSPawel Jakub Dawidek  *
5c58794deSPawel Jakub Dawidek  * Redistribution and use in source and binary forms, with or without
6c58794deSPawel Jakub Dawidek  * modification, are permitted provided that the following conditions
7c58794deSPawel Jakub Dawidek  * are met:
8c58794deSPawel Jakub Dawidek  * 1. Redistributions of source code must retain the above copyright
9c58794deSPawel Jakub Dawidek  *    notice, this list of conditions and the following disclaimer.
10c58794deSPawel Jakub Dawidek  * 2. Redistributions in binary form must reproduce the above copyright
11c58794deSPawel Jakub Dawidek  *    notice, this list of conditions and the following disclaimer in the
12c58794deSPawel Jakub Dawidek  *    documentation and/or other materials provided with the distribution.
13c58794deSPawel Jakub Dawidek  *
14c58794deSPawel Jakub Dawidek  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15c58794deSPawel Jakub Dawidek  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16c58794deSPawel Jakub Dawidek  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17c58794deSPawel Jakub Dawidek  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18c58794deSPawel Jakub Dawidek  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19c58794deSPawel Jakub Dawidek  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20c58794deSPawel Jakub Dawidek  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21c58794deSPawel Jakub Dawidek  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22c58794deSPawel Jakub Dawidek  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23c58794deSPawel Jakub Dawidek  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24c58794deSPawel Jakub Dawidek  * SUCH DAMAGE.
25c58794deSPawel Jakub Dawidek  */
26c58794deSPawel Jakub Dawidek 
27c58794deSPawel Jakub Dawidek #include <sys/cdefs.h>
28c58794deSPawel Jakub Dawidek __FBSDID("$FreeBSD$");
29c58794deSPawel Jakub Dawidek 
30c58794deSPawel Jakub Dawidek #include <sys/param.h>
31c58794deSPawel Jakub Dawidek #include <sys/systm.h>
32f6ce353eSAndriy Gapon #include <sys/cons.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 
50c58794deSPawel Jakub Dawidek #include <vm/uma.h>
51c58794deSPawel Jakub Dawidek 
52c58794deSPawel Jakub Dawidek #include <geom/geom.h>
53c58794deSPawel Jakub Dawidek #include <geom/eli/g_eli.h>
54c58794deSPawel Jakub Dawidek #include <geom/eli/pkcs5v2.h>
55c58794deSPawel Jakub Dawidek 
56cb08c2ccSAlexander Leidinger FEATURE(geom_eli, "GEOM crypto module");
57c58794deSPawel Jakub Dawidek 
58c58794deSPawel Jakub Dawidek MALLOC_DEFINE(M_ELI, "eli data", "GEOM_ELI Data");
59c58794deSPawel Jakub Dawidek 
60c58794deSPawel Jakub Dawidek SYSCTL_DECL(_kern_geom);
61c58794deSPawel Jakub Dawidek SYSCTL_NODE(_kern_geom, OID_AUTO, eli, CTLFLAG_RW, 0, "GEOM_ELI stuff");
62a1f4a8c4SPawel Jakub Dawidek static int g_eli_version = G_ELI_VERSION;
63a1f4a8c4SPawel Jakub Dawidek SYSCTL_INT(_kern_geom_eli, OID_AUTO, version, CTLFLAG_RD, &g_eli_version, 0,
64a1f4a8c4SPawel Jakub Dawidek     "GELI version");
65f95168e0SPawel Jakub Dawidek int g_eli_debug = 0;
66af3b2549SHans Petter Selasky SYSCTL_INT(_kern_geom_eli, OID_AUTO, debug, CTLFLAG_RWTUN, &g_eli_debug, 0,
67c58794deSPawel Jakub Dawidek     "Debug level");
68c58794deSPawel Jakub Dawidek static u_int g_eli_tries = 3;
69af3b2549SHans Petter Selasky SYSCTL_UINT(_kern_geom_eli, OID_AUTO, tries, CTLFLAG_RWTUN, &g_eli_tries, 0,
7098645006SChristian Brueffer     "Number of tries for entering the passphrase");
71eb4c31fdSEd Schouten static u_int g_eli_visible_passphrase = GETS_NOECHO;
72af3b2549SHans Petter Selasky SYSCTL_UINT(_kern_geom_eli, OID_AUTO, visible_passphrase, CTLFLAG_RWTUN,
73c58794deSPawel Jakub Dawidek     &g_eli_visible_passphrase, 0,
74eb4c31fdSEd Schouten     "Visibility of passphrase prompt (0 = invisible, 1 = visible, 2 = asterisk)");
75b35bfe7eSPawel Jakub Dawidek u_int g_eli_overwrites = G_ELI_OVERWRITES;
76af3b2549SHans Petter Selasky SYSCTL_UINT(_kern_geom_eli, OID_AUTO, overwrites, CTLFLAG_RWTUN, &g_eli_overwrites,
7798645006SChristian Brueffer     0, "Number of times on-disk keys should be overwritten when destroying them");
78dd549194SPawel Jakub Dawidek static u_int g_eli_threads = 0;
79af3b2549SHans Petter Selasky SYSCTL_UINT(_kern_geom_eli, OID_AUTO, threads, CTLFLAG_RWTUN, &g_eli_threads, 0,
80c58794deSPawel Jakub Dawidek     "Number of threads doing crypto work");
81eaa3b919SPawel Jakub Dawidek u_int g_eli_batch = 0;
82af3b2549SHans Petter Selasky SYSCTL_UINT(_kern_geom_eli, OID_AUTO, batch, CTLFLAG_RWTUN, &g_eli_batch, 0,
83eaa3b919SPawel Jakub Dawidek     "Use crypto operations batching");
84c58794deSPawel Jakub Dawidek 
85835c4dd4SColin Percival /*
86835c4dd4SColin Percival  * Passphrase cached during boot, in order to be more user-friendly if
87835c4dd4SColin Percival  * there are multiple providers using the same passphrase.
88835c4dd4SColin Percival  */
89835c4dd4SColin Percival static char cached_passphrase[256];
90835c4dd4SColin Percival static u_int g_eli_boot_passcache = 1;
91835c4dd4SColin Percival TUNABLE_INT("kern.geom.eli.boot_passcache", &g_eli_boot_passcache);
92835c4dd4SColin Percival SYSCTL_UINT(_kern_geom_eli, OID_AUTO, boot_passcache, CTLFLAG_RD,
93835c4dd4SColin Percival     &g_eli_boot_passcache, 0,
94835c4dd4SColin Percival     "Passphrases are cached during boot process for possible reuse");
95835c4dd4SColin Percival static void
9666427784SColin Percival fetch_loader_passphrase(void * dummy)
9766427784SColin Percival {
9866427784SColin Percival 	char * env_passphrase;
9966427784SColin Percival 
10066427784SColin Percival 	KASSERT(dynamic_kenv, ("need dynamic kenv"));
10166427784SColin Percival 
10266427784SColin Percival 	if ((env_passphrase = kern_getenv("kern.geom.eli.passphrase")) != NULL) {
10366427784SColin Percival 		/* Extract passphrase from the environment. */
10466427784SColin Percival 		strlcpy(cached_passphrase, env_passphrase,
10566427784SColin Percival 		    sizeof(cached_passphrase));
10666427784SColin Percival 		freeenv(env_passphrase);
10766427784SColin Percival 
10866427784SColin Percival 		/* Wipe the passphrase from the environment. */
10966427784SColin Percival 		kern_unsetenv("kern.geom.eli.passphrase");
11066427784SColin Percival 	}
11166427784SColin Percival }
11266427784SColin Percival SYSINIT(geli_fetch_loader_passphrase, SI_SUB_KMEM + 1, SI_ORDER_ANY,
11366427784SColin Percival     fetch_loader_passphrase, NULL);
11466427784SColin Percival static void
115835c4dd4SColin Percival zero_boot_passcache(void * dummy)
116835c4dd4SColin Percival {
117835c4dd4SColin Percival 
118835c4dd4SColin Percival 	memset(cached_passphrase, 0, sizeof(cached_passphrase));
119835c4dd4SColin Percival }
120835c4dd4SColin Percival EVENTHANDLER_DEFINE(mountroot, zero_boot_passcache, NULL, 0);
121835c4dd4SColin Percival 
122c5d387d0SPawel Jakub Dawidek static eventhandler_tag g_eli_pre_sync = NULL;
123c5d387d0SPawel Jakub Dawidek 
124c58794deSPawel Jakub Dawidek static int g_eli_destroy_geom(struct gctl_req *req, struct g_class *mp,
125c58794deSPawel Jakub Dawidek     struct g_geom *gp);
126c5d387d0SPawel Jakub Dawidek static void g_eli_init(struct g_class *mp);
127c5d387d0SPawel Jakub Dawidek static void g_eli_fini(struct g_class *mp);
128c58794deSPawel Jakub Dawidek 
129c58794deSPawel Jakub Dawidek static g_taste_t g_eli_taste;
130c58794deSPawel Jakub Dawidek static g_dumpconf_t g_eli_dumpconf;
131c58794deSPawel Jakub Dawidek 
132c58794deSPawel Jakub Dawidek struct g_class g_eli_class = {
133c58794deSPawel Jakub Dawidek 	.name = G_ELI_CLASS_NAME,
134c58794deSPawel Jakub Dawidek 	.version = G_VERSION,
135c58794deSPawel Jakub Dawidek 	.ctlreq = g_eli_config,
136c58794deSPawel Jakub Dawidek 	.taste = g_eli_taste,
137c5d387d0SPawel Jakub Dawidek 	.destroy_geom = g_eli_destroy_geom,
138c5d387d0SPawel Jakub Dawidek 	.init = g_eli_init,
139c5d387d0SPawel Jakub Dawidek 	.fini = g_eli_fini
140c58794deSPawel Jakub Dawidek };
141c58794deSPawel Jakub Dawidek 
142c58794deSPawel Jakub Dawidek 
143c58794deSPawel Jakub Dawidek /*
144c58794deSPawel Jakub Dawidek  * Code paths:
145c58794deSPawel Jakub Dawidek  * BIO_READ:
1465ad4a7c7SPawel 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
147c58794deSPawel Jakub Dawidek  * BIO_WRITE:
148c58794deSPawel Jakub Dawidek  *	g_eli_start -> g_eli_crypto_run -> g_eli_crypto_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver
149c58794deSPawel Jakub Dawidek  */
150c58794deSPawel Jakub Dawidek 
151c58794deSPawel Jakub Dawidek 
152c58794deSPawel Jakub Dawidek /*
153c58794deSPawel Jakub Dawidek  * EAGAIN from crypto(9) means, that we were probably balanced to another crypto
154c58794deSPawel Jakub Dawidek  * accelerator or something like this.
155c58794deSPawel Jakub Dawidek  * The function updates the SID and rerun the operation.
156c58794deSPawel Jakub Dawidek  */
157eaa3b919SPawel Jakub Dawidek int
158c58794deSPawel Jakub Dawidek g_eli_crypto_rerun(struct cryptop *crp)
159c58794deSPawel Jakub Dawidek {
160c58794deSPawel Jakub Dawidek 	struct g_eli_softc *sc;
161c58794deSPawel Jakub Dawidek 	struct g_eli_worker *wr;
162c58794deSPawel Jakub Dawidek 	struct bio *bp;
163c58794deSPawel Jakub Dawidek 	int error;
164c58794deSPawel Jakub Dawidek 
165c58794deSPawel Jakub Dawidek 	bp = (struct bio *)crp->crp_opaque;
166c58794deSPawel Jakub Dawidek 	sc = bp->bio_to->geom->softc;
167c58794deSPawel Jakub Dawidek 	LIST_FOREACH(wr, &sc->sc_workers, w_next) {
168c58794deSPawel Jakub Dawidek 		if (wr->w_number == bp->bio_pflags)
169c58794deSPawel Jakub Dawidek 			break;
170c58794deSPawel Jakub Dawidek 	}
171c58794deSPawel Jakub Dawidek 	KASSERT(wr != NULL, ("Invalid worker (%u).", bp->bio_pflags));
17298645006SChristian Brueffer 	G_ELI_DEBUG(1, "Rerunning crypto %s request (sid: %ju -> %ju).",
173c58794deSPawel Jakub Dawidek 	    bp->bio_cmd == BIO_READ ? "READ" : "WRITE", (uintmax_t)wr->w_sid,
174c58794deSPawel Jakub Dawidek 	    (uintmax_t)crp->crp_sid);
175c58794deSPawel Jakub Dawidek 	wr->w_sid = crp->crp_sid;
176c58794deSPawel Jakub Dawidek 	crp->crp_etype = 0;
177c58794deSPawel Jakub Dawidek 	error = crypto_dispatch(crp);
178c58794deSPawel Jakub Dawidek 	if (error == 0)
179c58794deSPawel Jakub Dawidek 		return (0);
180c58794deSPawel Jakub Dawidek 	G_ELI_DEBUG(1, "%s: crypto_dispatch() returned %d.", __func__, error);
181c58794deSPawel Jakub Dawidek 	crp->crp_etype = error;
182c58794deSPawel Jakub Dawidek 	return (error);
183c58794deSPawel Jakub Dawidek }
184c58794deSPawel Jakub Dawidek 
185c58794deSPawel Jakub Dawidek /*
186c58794deSPawel Jakub Dawidek  * The function is called afer reading encrypted data from the provider.
187bb30fea6SPawel Jakub Dawidek  *
1885ad4a7c7SPawel 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
189c58794deSPawel Jakub Dawidek  */
190eaa3b919SPawel Jakub Dawidek void
191c58794deSPawel Jakub Dawidek g_eli_read_done(struct bio *bp)
192c58794deSPawel Jakub Dawidek {
193c58794deSPawel Jakub Dawidek 	struct g_eli_softc *sc;
194c58794deSPawel Jakub Dawidek 	struct bio *pbp;
195c58794deSPawel Jakub Dawidek 
196c58794deSPawel Jakub Dawidek 	G_ELI_LOGREQ(2, bp, "Request done.");
197c58794deSPawel Jakub Dawidek 	pbp = bp->bio_parent;
1982dc7e36bSSteven Hartland 	if (pbp->bio_error == 0 && bp->bio_error != 0)
199c58794deSPawel Jakub Dawidek 		pbp->bio_error = bp->bio_error;
20090574b0aSMikolaj Golub 	g_destroy_bio(bp);
201eaa3b919SPawel Jakub Dawidek 	/*
202eaa3b919SPawel Jakub Dawidek 	 * Do we have all sectors already?
203eaa3b919SPawel Jakub Dawidek 	 */
204eaa3b919SPawel Jakub Dawidek 	pbp->bio_inbed++;
205eaa3b919SPawel Jakub Dawidek 	if (pbp->bio_inbed < pbp->bio_children)
206eaa3b919SPawel Jakub Dawidek 		return;
2075ad4a7c7SPawel Jakub Dawidek 	sc = pbp->bio_to->geom->softc;
208c58794deSPawel Jakub Dawidek 	if (pbp->bio_error != 0) {
2092dc7e36bSSteven Hartland 		G_ELI_LOGREQ(0, pbp, "%s() failed (error=%d)", __func__,
2102dc7e36bSSteven Hartland 		    pbp->bio_error);
211c58794deSPawel Jakub Dawidek 		pbp->bio_completed = 0;
212eaa3b919SPawel Jakub Dawidek 		if (pbp->bio_driver2 != NULL) {
213eaa3b919SPawel Jakub Dawidek 			free(pbp->bio_driver2, M_ELI);
214eaa3b919SPawel Jakub Dawidek 			pbp->bio_driver2 = NULL;
215eaa3b919SPawel Jakub Dawidek 		}
216c58794deSPawel Jakub Dawidek 		g_io_deliver(pbp, pbp->bio_error);
2175ad4a7c7SPawel Jakub Dawidek 		atomic_subtract_int(&sc->sc_inflight, 1);
218c58794deSPawel Jakub Dawidek 		return;
219c58794deSPawel Jakub Dawidek 	}
220c58794deSPawel Jakub Dawidek 	mtx_lock(&sc->sc_queue_mtx);
221c58794deSPawel Jakub Dawidek 	bioq_insert_tail(&sc->sc_queue, pbp);
222c58794deSPawel Jakub Dawidek 	mtx_unlock(&sc->sc_queue_mtx);
223c58794deSPawel Jakub Dawidek 	wakeup(sc);
224c58794deSPawel Jakub Dawidek }
225c58794deSPawel Jakub Dawidek 
226c58794deSPawel Jakub Dawidek /*
227c58794deSPawel Jakub Dawidek  * The function is called after we encrypt and write data.
228bb30fea6SPawel Jakub Dawidek  *
229bb30fea6SPawel Jakub Dawidek  * g_eli_start -> g_eli_crypto_run -> g_eli_crypto_write_done -> g_io_request -> G_ELI_WRITE_DONE -> g_io_deliver
230c58794deSPawel Jakub Dawidek  */
231eaa3b919SPawel Jakub Dawidek void
232c58794deSPawel Jakub Dawidek g_eli_write_done(struct bio *bp)
233c58794deSPawel Jakub Dawidek {
2345ad4a7c7SPawel Jakub Dawidek 	struct g_eli_softc *sc;
235c58794deSPawel Jakub Dawidek 	struct bio *pbp;
236c58794deSPawel Jakub Dawidek 
237c58794deSPawel Jakub Dawidek 	G_ELI_LOGREQ(2, bp, "Request done.");
238c58794deSPawel Jakub Dawidek 	pbp = bp->bio_parent;
2392dc7e36bSSteven Hartland 	if (pbp->bio_error == 0 && bp->bio_error != 0)
240c58794deSPawel Jakub Dawidek 		pbp->bio_error = bp->bio_error;
24190574b0aSMikolaj Golub 	g_destroy_bio(bp);
242eaa3b919SPawel Jakub Dawidek 	/*
243eaa3b919SPawel Jakub Dawidek 	 * Do we have all sectors already?
244eaa3b919SPawel Jakub Dawidek 	 */
245eaa3b919SPawel Jakub Dawidek 	pbp->bio_inbed++;
246eaa3b919SPawel Jakub Dawidek 	if (pbp->bio_inbed < pbp->bio_children)
247eaa3b919SPawel Jakub Dawidek 		return;
248c58794deSPawel Jakub Dawidek 	free(pbp->bio_driver2, M_ELI);
249c58794deSPawel Jakub Dawidek 	pbp->bio_driver2 = NULL;
250eaa3b919SPawel Jakub Dawidek 	if (pbp->bio_error != 0) {
2512dc7e36bSSteven Hartland 		G_ELI_LOGREQ(0, pbp, "%s() failed (error=%d)", __func__,
252c58794deSPawel Jakub Dawidek 		    pbp->bio_error);
253c58794deSPawel Jakub Dawidek 		pbp->bio_completed = 0;
2542dc7e36bSSteven Hartland 	} else
2552dc7e36bSSteven Hartland 		pbp->bio_completed = pbp->bio_length;
2562dc7e36bSSteven Hartland 
257c58794deSPawel Jakub Dawidek 	/*
258c58794deSPawel Jakub Dawidek 	 * Write is finished, send it up.
259c58794deSPawel Jakub Dawidek 	 */
2605ad4a7c7SPawel Jakub Dawidek 	sc = pbp->bio_to->geom->softc;
261c58794deSPawel Jakub Dawidek 	g_io_deliver(pbp, pbp->bio_error);
2625ad4a7c7SPawel Jakub Dawidek 	atomic_subtract_int(&sc->sc_inflight, 1);
263c58794deSPawel Jakub Dawidek }
264c58794deSPawel Jakub Dawidek 
265c58794deSPawel Jakub Dawidek /*
266c58794deSPawel Jakub Dawidek  * This function should never be called, but GEOM made as it set ->orphan()
267c58794deSPawel Jakub Dawidek  * method for every geom.
268c58794deSPawel Jakub Dawidek  */
269c58794deSPawel Jakub Dawidek static void
270c58794deSPawel Jakub Dawidek g_eli_orphan_spoil_assert(struct g_consumer *cp)
271c58794deSPawel Jakub Dawidek {
272c58794deSPawel Jakub Dawidek 
273c58794deSPawel Jakub Dawidek 	panic("Function %s() called for %s.", __func__, cp->geom->name);
274c58794deSPawel Jakub Dawidek }
275c58794deSPawel Jakub Dawidek 
276c58794deSPawel Jakub Dawidek static void
277c58794deSPawel Jakub Dawidek g_eli_orphan(struct g_consumer *cp)
278c58794deSPawel Jakub Dawidek {
279c58794deSPawel Jakub Dawidek 	struct g_eli_softc *sc;
280c58794deSPawel Jakub Dawidek 
281c58794deSPawel Jakub Dawidek 	g_topology_assert();
282c58794deSPawel Jakub Dawidek 	sc = cp->geom->softc;
283c58794deSPawel Jakub Dawidek 	if (sc == NULL)
284c58794deSPawel Jakub Dawidek 		return;
2855ad4a7c7SPawel Jakub Dawidek 	g_eli_destroy(sc, TRUE);
286c58794deSPawel Jakub Dawidek }
287c58794deSPawel Jakub Dawidek 
288bb30fea6SPawel Jakub Dawidek /*
289056638c4SPawel Jakub Dawidek  * BIO_READ:
2905ad4a7c7SPawel 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
291056638c4SPawel Jakub Dawidek  * BIO_WRITE:
292056638c4SPawel Jakub Dawidek  *	G_ELI_START -> g_eli_crypto_run -> g_eli_crypto_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver
293bb30fea6SPawel Jakub Dawidek  */
294c58794deSPawel Jakub Dawidek static void
295c58794deSPawel Jakub Dawidek g_eli_start(struct bio *bp)
296c58794deSPawel Jakub Dawidek {
297c58794deSPawel Jakub Dawidek 	struct g_eli_softc *sc;
298d3a1be90SPawel Jakub Dawidek 	struct g_consumer *cp;
299c58794deSPawel Jakub Dawidek 	struct bio *cbp;
300c58794deSPawel Jakub Dawidek 
301c58794deSPawel Jakub Dawidek 	sc = bp->bio_to->geom->softc;
302c58794deSPawel Jakub Dawidek 	KASSERT(sc != NULL,
303c58794deSPawel Jakub Dawidek 	    ("Provider's error should be set (error=%d)(device=%s).",
304c58794deSPawel Jakub Dawidek 	    bp->bio_to->error, bp->bio_to->name));
305c58794deSPawel Jakub Dawidek 	G_ELI_LOGREQ(2, bp, "Request received.");
306c58794deSPawel Jakub Dawidek 
307c58794deSPawel Jakub Dawidek 	switch (bp->bio_cmd) {
308c58794deSPawel Jakub Dawidek 	case BIO_READ:
309c58794deSPawel Jakub Dawidek 	case BIO_WRITE:
310d3a1be90SPawel Jakub Dawidek 	case BIO_GETATTR:
31142461fbaSPawel Jakub Dawidek 	case BIO_FLUSH:
312c58794deSPawel Jakub Dawidek 		break;
313c58794deSPawel Jakub Dawidek 	case BIO_DELETE:
314c58794deSPawel Jakub Dawidek 		/*
31546e34470SPawel Jakub Dawidek 		 * If the user hasn't set the NODELETE flag, we just pass
31646e34470SPawel Jakub Dawidek 		 * it down the stack and let the layers beneath us do (or
31746e34470SPawel Jakub Dawidek 		 * not) whatever they do with it.  If they have, we
31846e34470SPawel Jakub Dawidek 		 * reject it.  A possible extension would be an
31946e34470SPawel Jakub Dawidek 		 * additional flag to take it as a hint to shred the data
32046e34470SPawel Jakub Dawidek 		 * with [multiple?] overwrites.
321c58794deSPawel Jakub Dawidek 		 */
32246e34470SPawel Jakub Dawidek 		if (!(sc->sc_flags & G_ELI_FLAG_NODELETE))
32346e34470SPawel Jakub Dawidek 			break;
324c58794deSPawel Jakub Dawidek 	default:
325c58794deSPawel Jakub Dawidek 		g_io_deliver(bp, EOPNOTSUPP);
326c58794deSPawel Jakub Dawidek 		return;
327c58794deSPawel Jakub Dawidek 	}
328c58794deSPawel Jakub Dawidek 	cbp = g_clone_bio(bp);
329c58794deSPawel Jakub Dawidek 	if (cbp == NULL) {
330c58794deSPawel Jakub Dawidek 		g_io_deliver(bp, ENOMEM);
331c58794deSPawel Jakub Dawidek 		return;
332c58794deSPawel Jakub Dawidek 	}
3335ad4a7c7SPawel Jakub Dawidek 	bp->bio_driver1 = cbp;
3345ad4a7c7SPawel Jakub Dawidek 	bp->bio_pflags = G_ELI_NEW_BIO;
335d3a1be90SPawel Jakub Dawidek 	switch (bp->bio_cmd) {
336d3a1be90SPawel Jakub Dawidek 	case BIO_READ:
337eaa3b919SPawel Jakub Dawidek 		if (!(sc->sc_flags & G_ELI_FLAG_AUTH)) {
3385ad4a7c7SPawel Jakub Dawidek 			g_eli_crypto_read(sc, bp, 0);
339d3a1be90SPawel Jakub Dawidek 			break;
340eaa3b919SPawel Jakub Dawidek 		}
341eaa3b919SPawel Jakub Dawidek 		/* FALLTHROUGH */
342d3a1be90SPawel Jakub Dawidek 	case BIO_WRITE:
343c58794deSPawel Jakub Dawidek 		mtx_lock(&sc->sc_queue_mtx);
344c58794deSPawel Jakub Dawidek 		bioq_insert_tail(&sc->sc_queue, bp);
345c58794deSPawel Jakub Dawidek 		mtx_unlock(&sc->sc_queue_mtx);
346c58794deSPawel Jakub Dawidek 		wakeup(sc);
347d3a1be90SPawel Jakub Dawidek 		break;
348d3a1be90SPawel Jakub Dawidek 	case BIO_GETATTR:
34942461fbaSPawel Jakub Dawidek 	case BIO_FLUSH:
35046e34470SPawel Jakub Dawidek 	case BIO_DELETE:
351d3a1be90SPawel Jakub Dawidek 		cbp->bio_done = g_std_done;
352d3a1be90SPawel Jakub Dawidek 		cp = LIST_FIRST(&sc->sc_geom->consumer);
353d3a1be90SPawel Jakub Dawidek 		cbp->bio_to = cp->provider;
354cd0d707eSPawel Jakub Dawidek 		G_ELI_LOGREQ(2, cbp, "Sending request.");
355d3a1be90SPawel Jakub Dawidek 		g_io_request(cbp, cp);
356d3a1be90SPawel Jakub Dawidek 		break;
357c58794deSPawel Jakub Dawidek 	}
358c58794deSPawel Jakub Dawidek }
359c58794deSPawel Jakub Dawidek 
3603ac01bc2SPawel Jakub Dawidek static int
3613ac01bc2SPawel Jakub Dawidek g_eli_newsession(struct g_eli_worker *wr)
3623ac01bc2SPawel Jakub Dawidek {
3633ac01bc2SPawel Jakub Dawidek 	struct g_eli_softc *sc;
3643ac01bc2SPawel Jakub Dawidek 	struct cryptoini crie, cria;
3653ac01bc2SPawel Jakub Dawidek 	int error;
3663ac01bc2SPawel Jakub Dawidek 
3673ac01bc2SPawel Jakub Dawidek 	sc = wr->w_softc;
3683ac01bc2SPawel Jakub Dawidek 
3693ac01bc2SPawel Jakub Dawidek 	bzero(&crie, sizeof(crie));
3703ac01bc2SPawel Jakub Dawidek 	crie.cri_alg = sc->sc_ealgo;
3713ac01bc2SPawel Jakub Dawidek 	crie.cri_klen = sc->sc_ekeylen;
3723ac01bc2SPawel Jakub Dawidek 	if (sc->sc_ealgo == CRYPTO_AES_XTS)
3733ac01bc2SPawel Jakub Dawidek 		crie.cri_klen <<= 1;
374ad0a5236SPawel Jakub Dawidek 	if ((sc->sc_flags & G_ELI_FLAG_FIRST_KEY) != 0) {
375ad0a5236SPawel Jakub Dawidek 		crie.cri_key = g_eli_key_hold(sc, 0,
376ad0a5236SPawel Jakub Dawidek 		    LIST_FIRST(&sc->sc_geom->consumer)->provider->sectorsize);
377ad0a5236SPawel Jakub Dawidek 	} else {
3781e09ff3dSPawel Jakub Dawidek 		crie.cri_key = sc->sc_ekey;
379ad0a5236SPawel Jakub Dawidek 	}
3803ac01bc2SPawel Jakub Dawidek 	if (sc->sc_flags & G_ELI_FLAG_AUTH) {
3813ac01bc2SPawel Jakub Dawidek 		bzero(&cria, sizeof(cria));
3823ac01bc2SPawel Jakub Dawidek 		cria.cri_alg = sc->sc_aalgo;
3833ac01bc2SPawel Jakub Dawidek 		cria.cri_klen = sc->sc_akeylen;
3843ac01bc2SPawel Jakub Dawidek 		cria.cri_key = sc->sc_akey;
3853ac01bc2SPawel Jakub Dawidek 		crie.cri_next = &cria;
3863ac01bc2SPawel Jakub Dawidek 	}
3873ac01bc2SPawel Jakub Dawidek 
3883ac01bc2SPawel Jakub Dawidek 	switch (sc->sc_crypto) {
3893ac01bc2SPawel Jakub Dawidek 	case G_ELI_CRYPTO_SW:
3903ac01bc2SPawel Jakub Dawidek 		error = crypto_newsession(&wr->w_sid, &crie,
3913ac01bc2SPawel Jakub Dawidek 		    CRYPTOCAP_F_SOFTWARE);
3923ac01bc2SPawel Jakub Dawidek 		break;
3933ac01bc2SPawel Jakub Dawidek 	case G_ELI_CRYPTO_HW:
3943ac01bc2SPawel Jakub Dawidek 		error = crypto_newsession(&wr->w_sid, &crie,
3953ac01bc2SPawel Jakub Dawidek 		    CRYPTOCAP_F_HARDWARE);
3963ac01bc2SPawel Jakub Dawidek 		break;
3973ac01bc2SPawel Jakub Dawidek 	case G_ELI_CRYPTO_UNKNOWN:
3983ac01bc2SPawel Jakub Dawidek 		error = crypto_newsession(&wr->w_sid, &crie,
3993ac01bc2SPawel Jakub Dawidek 		    CRYPTOCAP_F_HARDWARE);
4003ac01bc2SPawel Jakub Dawidek 		if (error == 0) {
4013ac01bc2SPawel Jakub Dawidek 			mtx_lock(&sc->sc_queue_mtx);
4023ac01bc2SPawel Jakub Dawidek 			if (sc->sc_crypto == G_ELI_CRYPTO_UNKNOWN)
4033ac01bc2SPawel Jakub Dawidek 				sc->sc_crypto = G_ELI_CRYPTO_HW;
4043ac01bc2SPawel Jakub Dawidek 			mtx_unlock(&sc->sc_queue_mtx);
4053ac01bc2SPawel Jakub Dawidek 		} else {
4063ac01bc2SPawel Jakub Dawidek 			error = crypto_newsession(&wr->w_sid, &crie,
4073ac01bc2SPawel Jakub Dawidek 			    CRYPTOCAP_F_SOFTWARE);
4083ac01bc2SPawel Jakub Dawidek 			mtx_lock(&sc->sc_queue_mtx);
4093ac01bc2SPawel Jakub Dawidek 			if (sc->sc_crypto == G_ELI_CRYPTO_UNKNOWN)
4103ac01bc2SPawel Jakub Dawidek 				sc->sc_crypto = G_ELI_CRYPTO_SW;
4113ac01bc2SPawel Jakub Dawidek 			mtx_unlock(&sc->sc_queue_mtx);
4123ac01bc2SPawel Jakub Dawidek 		}
4133ac01bc2SPawel Jakub Dawidek 		break;
4143ac01bc2SPawel Jakub Dawidek 	default:
4153ac01bc2SPawel Jakub Dawidek 		panic("%s: invalid condition", __func__);
4163ac01bc2SPawel Jakub Dawidek 	}
4173ac01bc2SPawel Jakub Dawidek 
418ad0a5236SPawel Jakub Dawidek 	if ((sc->sc_flags & G_ELI_FLAG_FIRST_KEY) != 0)
419ad0a5236SPawel Jakub Dawidek 		g_eli_key_drop(sc, crie.cri_key);
420ad0a5236SPawel Jakub Dawidek 
4213ac01bc2SPawel Jakub Dawidek 	return (error);
4223ac01bc2SPawel Jakub Dawidek }
4233ac01bc2SPawel Jakub Dawidek 
4243ac01bc2SPawel Jakub Dawidek static void
4253ac01bc2SPawel Jakub Dawidek g_eli_freesession(struct g_eli_worker *wr)
4263ac01bc2SPawel Jakub Dawidek {
4273ac01bc2SPawel Jakub Dawidek 
4283ac01bc2SPawel Jakub Dawidek 	crypto_freesession(wr->w_sid);
4293ac01bc2SPawel Jakub Dawidek }
4303ac01bc2SPawel Jakub Dawidek 
4315ad4a7c7SPawel Jakub Dawidek static void
4325ad4a7c7SPawel Jakub Dawidek g_eli_cancel(struct g_eli_softc *sc)
4335ad4a7c7SPawel Jakub Dawidek {
4345ad4a7c7SPawel Jakub Dawidek 	struct bio *bp;
4355ad4a7c7SPawel Jakub Dawidek 
4365ad4a7c7SPawel Jakub Dawidek 	mtx_assert(&sc->sc_queue_mtx, MA_OWNED);
4375ad4a7c7SPawel Jakub Dawidek 
4385ad4a7c7SPawel Jakub Dawidek 	while ((bp = bioq_takefirst(&sc->sc_queue)) != NULL) {
4395ad4a7c7SPawel Jakub Dawidek 		KASSERT(bp->bio_pflags == G_ELI_NEW_BIO,
4405ad4a7c7SPawel Jakub Dawidek 		    ("Not new bio when canceling (bp=%p).", bp));
4415ad4a7c7SPawel Jakub Dawidek 		g_io_deliver(bp, ENXIO);
4425ad4a7c7SPawel Jakub Dawidek 	}
4435ad4a7c7SPawel Jakub Dawidek }
4445ad4a7c7SPawel Jakub Dawidek 
4455ad4a7c7SPawel Jakub Dawidek static struct bio *
4465ad4a7c7SPawel Jakub Dawidek g_eli_takefirst(struct g_eli_softc *sc)
4475ad4a7c7SPawel Jakub Dawidek {
4485ad4a7c7SPawel Jakub Dawidek 	struct bio *bp;
4495ad4a7c7SPawel Jakub Dawidek 
4505ad4a7c7SPawel Jakub Dawidek 	mtx_assert(&sc->sc_queue_mtx, MA_OWNED);
4515ad4a7c7SPawel Jakub Dawidek 
4525ad4a7c7SPawel Jakub Dawidek 	if (!(sc->sc_flags & G_ELI_FLAG_SUSPEND))
4535ad4a7c7SPawel Jakub Dawidek 		return (bioq_takefirst(&sc->sc_queue));
4545ad4a7c7SPawel Jakub Dawidek 	/*
4555ad4a7c7SPawel Jakub Dawidek 	 * Device suspended, so we skip new I/O requests.
4565ad4a7c7SPawel Jakub Dawidek 	 */
4575ad4a7c7SPawel Jakub Dawidek 	TAILQ_FOREACH(bp, &sc->sc_queue.queue, bio_queue) {
4585ad4a7c7SPawel Jakub Dawidek 		if (bp->bio_pflags != G_ELI_NEW_BIO)
4595ad4a7c7SPawel Jakub Dawidek 			break;
4605ad4a7c7SPawel Jakub Dawidek 	}
4615ad4a7c7SPawel Jakub Dawidek 	if (bp != NULL)
4625ad4a7c7SPawel Jakub Dawidek 		bioq_remove(&sc->sc_queue, bp);
4635ad4a7c7SPawel Jakub Dawidek 	return (bp);
4645ad4a7c7SPawel Jakub Dawidek }
4655ad4a7c7SPawel Jakub Dawidek 
466c58794deSPawel Jakub Dawidek /*
467c58794deSPawel Jakub Dawidek  * This is the main function for kernel worker thread when we don't have
468c58794deSPawel Jakub Dawidek  * hardware acceleration and we have to do cryptography in software.
469c58794deSPawel Jakub Dawidek  * Dedicated thread is needed, so we don't slow down g_up/g_down GEOM
470c58794deSPawel Jakub Dawidek  * threads with crypto work.
471c58794deSPawel Jakub Dawidek  */
472c58794deSPawel Jakub Dawidek static void
473c58794deSPawel Jakub Dawidek g_eli_worker(void *arg)
474c58794deSPawel Jakub Dawidek {
475c58794deSPawel Jakub Dawidek 	struct g_eli_softc *sc;
476c58794deSPawel Jakub Dawidek 	struct g_eli_worker *wr;
477c58794deSPawel Jakub Dawidek 	struct bio *bp;
4783ac01bc2SPawel Jakub Dawidek 	int error;
479c58794deSPawel Jakub Dawidek 
480c58794deSPawel Jakub Dawidek 	wr = arg;
481c58794deSPawel Jakub Dawidek 	sc = wr->w_softc;
482*fdce57a0SJohn Baldwin #ifdef EARLY_AP_STARTUP
483*fdce57a0SJohn Baldwin 	MPASS(!sc->sc_cpubind || smp_started);
484*fdce57a0SJohn Baldwin #elif defined(SMP)
485a1ea1a22SPawel Jakub Dawidek 	/* Before sched_bind() to a CPU, wait for all CPUs to go on-line. */
4860c879bd9SPawel Jakub Dawidek 	if (sc->sc_cpubind) {
487a1ea1a22SPawel Jakub Dawidek 		while (!smp_started)
488a1ea1a22SPawel Jakub Dawidek 			tsleep(wr, 0, "geli:smp", hz / 4);
489a1ea1a22SPawel Jakub Dawidek 	}
490a1ea1a22SPawel Jakub Dawidek #endif
491982d11f8SJeff Roberson 	thread_lock(curthread);
49231c4cef7SPawel Jakub Dawidek 	sched_prio(curthread, PUSER);
4930c879bd9SPawel Jakub Dawidek 	if (sc->sc_cpubind)
4940c879bd9SPawel Jakub Dawidek 		sched_bind(curthread, wr->w_number % mp_ncpus);
495982d11f8SJeff Roberson 	thread_unlock(curthread);
496c58794deSPawel Jakub Dawidek 
497c58794deSPawel Jakub Dawidek 	G_ELI_DEBUG(1, "Thread %s started.", curthread->td_proc->p_comm);
498c58794deSPawel Jakub Dawidek 
499c58794deSPawel Jakub Dawidek 	for (;;) {
500c58794deSPawel Jakub Dawidek 		mtx_lock(&sc->sc_queue_mtx);
5015ad4a7c7SPawel Jakub Dawidek again:
5025ad4a7c7SPawel Jakub Dawidek 		bp = g_eli_takefirst(sc);
503c58794deSPawel Jakub Dawidek 		if (bp == NULL) {
504eaa3b919SPawel Jakub Dawidek 			if (sc->sc_flags & G_ELI_FLAG_DESTROY) {
5055ad4a7c7SPawel Jakub Dawidek 				g_eli_cancel(sc);
506c58794deSPawel Jakub Dawidek 				LIST_REMOVE(wr, w_next);
5073ac01bc2SPawel Jakub Dawidek 				g_eli_freesession(wr);
508c58794deSPawel Jakub Dawidek 				free(wr, M_ELI);
509c58794deSPawel Jakub Dawidek 				G_ELI_DEBUG(1, "Thread %s exiting.",
510c58794deSPawel Jakub Dawidek 				    curthread->td_proc->p_comm);
511c58794deSPawel Jakub Dawidek 				wakeup(&sc->sc_workers);
512c58794deSPawel Jakub Dawidek 				mtx_unlock(&sc->sc_queue_mtx);
5133745c395SJulian Elischer 				kproc_exit(0);
514c58794deSPawel Jakub Dawidek 			}
5155ad4a7c7SPawel Jakub Dawidek 			while (sc->sc_flags & G_ELI_FLAG_SUSPEND) {
5165ad4a7c7SPawel Jakub Dawidek 				if (sc->sc_inflight > 0) {
517038c55adSPawel Jakub Dawidek 					G_ELI_DEBUG(0, "inflight=%d",
518038c55adSPawel Jakub Dawidek 					    sc->sc_inflight);
5195ad4a7c7SPawel Jakub Dawidek 					/*
5205ad4a7c7SPawel Jakub Dawidek 					 * We still have inflight BIOs, so
5215ad4a7c7SPawel Jakub Dawidek 					 * sleep and retry.
5225ad4a7c7SPawel Jakub Dawidek 					 */
5235ad4a7c7SPawel Jakub Dawidek 					msleep(sc, &sc->sc_queue_mtx, PRIBIO,
5245ad4a7c7SPawel Jakub Dawidek 					    "geli:inf", hz / 5);
5255ad4a7c7SPawel Jakub Dawidek 					goto again;
5265ad4a7c7SPawel Jakub Dawidek 				}
5275ad4a7c7SPawel Jakub Dawidek 				/*
5285ad4a7c7SPawel Jakub Dawidek 				 * Suspend requested, mark the worker as
5295ad4a7c7SPawel Jakub Dawidek 				 * suspended and go to sleep.
5305ad4a7c7SPawel Jakub Dawidek 				 */
5313ac01bc2SPawel Jakub Dawidek 				if (wr->w_active) {
5323ac01bc2SPawel Jakub Dawidek 					g_eli_freesession(wr);
5333ac01bc2SPawel Jakub Dawidek 					wr->w_active = FALSE;
5343ac01bc2SPawel Jakub Dawidek 				}
5355ad4a7c7SPawel Jakub Dawidek 				wakeup(&sc->sc_workers);
5365ad4a7c7SPawel Jakub Dawidek 				msleep(sc, &sc->sc_queue_mtx, PRIBIO,
5375ad4a7c7SPawel Jakub Dawidek 				    "geli:suspend", 0);
5383ac01bc2SPawel Jakub Dawidek 				if (!wr->w_active &&
5393ac01bc2SPawel Jakub Dawidek 				    !(sc->sc_flags & G_ELI_FLAG_SUSPEND)) {
5403ac01bc2SPawel Jakub Dawidek 					error = g_eli_newsession(wr);
5413ac01bc2SPawel Jakub Dawidek 					KASSERT(error == 0,
5423ac01bc2SPawel Jakub Dawidek 					    ("g_eli_newsession() failed on resume (error=%d)",
5433ac01bc2SPawel Jakub Dawidek 					    error));
5443ac01bc2SPawel Jakub Dawidek 					wr->w_active = TRUE;
5453ac01bc2SPawel Jakub Dawidek 				}
5465ad4a7c7SPawel Jakub Dawidek 				goto again;
5475ad4a7c7SPawel Jakub Dawidek 			}
54831c4cef7SPawel Jakub Dawidek 			msleep(sc, &sc->sc_queue_mtx, PDROP, "geli:w", 0);
549c58794deSPawel Jakub Dawidek 			continue;
550c58794deSPawel Jakub Dawidek 		}
5515ad4a7c7SPawel Jakub Dawidek 		if (bp->bio_pflags == G_ELI_NEW_BIO)
5525ad4a7c7SPawel Jakub Dawidek 			atomic_add_int(&sc->sc_inflight, 1);
553c58794deSPawel Jakub Dawidek 		mtx_unlock(&sc->sc_queue_mtx);
5545ad4a7c7SPawel Jakub Dawidek 		if (bp->bio_pflags == G_ELI_NEW_BIO) {
5555ad4a7c7SPawel Jakub Dawidek 			bp->bio_pflags = 0;
5565ad4a7c7SPawel Jakub Dawidek 			if (sc->sc_flags & G_ELI_FLAG_AUTH) {
5575ad4a7c7SPawel Jakub Dawidek 				if (bp->bio_cmd == BIO_READ)
558eaa3b919SPawel Jakub Dawidek 					g_eli_auth_read(sc, bp);
5595ad4a7c7SPawel Jakub Dawidek 				else
5605ad4a7c7SPawel Jakub Dawidek 					g_eli_auth_run(wr, bp);
5615ad4a7c7SPawel Jakub Dawidek 			} else {
5625ad4a7c7SPawel Jakub Dawidek 				if (bp->bio_cmd == BIO_READ)
5635ad4a7c7SPawel Jakub Dawidek 					g_eli_crypto_read(sc, bp, 1);
5645ad4a7c7SPawel Jakub Dawidek 				else
5655ad4a7c7SPawel Jakub Dawidek 					g_eli_crypto_run(wr, bp);
5665ad4a7c7SPawel Jakub Dawidek 			}
5675ad4a7c7SPawel Jakub Dawidek 		} else {
5685ad4a7c7SPawel Jakub Dawidek 			if (sc->sc_flags & G_ELI_FLAG_AUTH)
569eaa3b919SPawel Jakub Dawidek 				g_eli_auth_run(wr, bp);
570eaa3b919SPawel Jakub Dawidek 			else
571dddd1d53SPawel Jakub Dawidek 				g_eli_crypto_run(wr, bp);
572c58794deSPawel Jakub Dawidek 		}
573c58794deSPawel Jakub Dawidek 	}
5745ad4a7c7SPawel Jakub Dawidek }
575c58794deSPawel Jakub Dawidek 
576c58794deSPawel Jakub Dawidek int
577c58794deSPawel Jakub Dawidek g_eli_read_metadata(struct g_class *mp, struct g_provider *pp,
578c58794deSPawel Jakub Dawidek     struct g_eli_metadata *md)
579c58794deSPawel Jakub Dawidek {
580c58794deSPawel Jakub Dawidek 	struct g_geom *gp;
581c58794deSPawel Jakub Dawidek 	struct g_consumer *cp;
582c58794deSPawel Jakub Dawidek 	u_char *buf = NULL;
583c58794deSPawel Jakub Dawidek 	int error;
584c58794deSPawel Jakub Dawidek 
585c58794deSPawel Jakub Dawidek 	g_topology_assert();
586c58794deSPawel Jakub Dawidek 
587c58794deSPawel Jakub Dawidek 	gp = g_new_geomf(mp, "eli:taste");
588c58794deSPawel Jakub Dawidek 	gp->start = g_eli_start;
589c58794deSPawel Jakub Dawidek 	gp->access = g_std_access;
590c58794deSPawel Jakub Dawidek 	/*
591c58794deSPawel Jakub Dawidek 	 * g_eli_read_metadata() is always called from the event thread.
592c58794deSPawel Jakub Dawidek 	 * Our geom is created and destroyed in the same event, so there
593c58794deSPawel Jakub Dawidek 	 * could be no orphan nor spoil event in the meantime.
594c58794deSPawel Jakub Dawidek 	 */
595c58794deSPawel Jakub Dawidek 	gp->orphan = g_eli_orphan_spoil_assert;
596c58794deSPawel Jakub Dawidek 	gp->spoiled = g_eli_orphan_spoil_assert;
597c58794deSPawel Jakub Dawidek 	cp = g_new_consumer(gp);
598c58794deSPawel Jakub Dawidek 	error = g_attach(cp, pp);
599c58794deSPawel Jakub Dawidek 	if (error != 0)
600c58794deSPawel Jakub Dawidek 		goto end;
601c58794deSPawel Jakub Dawidek 	error = g_access(cp, 1, 0, 0);
602c58794deSPawel Jakub Dawidek 	if (error != 0)
603c58794deSPawel Jakub Dawidek 		goto end;
604c58794deSPawel Jakub Dawidek 	g_topology_unlock();
605c58794deSPawel Jakub Dawidek 	buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
606c58794deSPawel Jakub Dawidek 	    &error);
607c58794deSPawel Jakub Dawidek 	g_topology_lock();
6088a4a44b5SMaxim Sobolev 	if (buf == NULL)
609c58794deSPawel Jakub Dawidek 		goto end;
610fefb6a14SPawel Jakub Dawidek 	error = eli_metadata_decode(buf, md);
611fefb6a14SPawel Jakub Dawidek 	if (error != 0)
612fefb6a14SPawel Jakub Dawidek 		goto end;
613fefb6a14SPawel Jakub Dawidek 	/* Metadata was read and decoded successfully. */
614c58794deSPawel Jakub Dawidek end:
615c58794deSPawel Jakub Dawidek 	if (buf != NULL)
616c58794deSPawel Jakub Dawidek 		g_free(buf);
617c58794deSPawel Jakub Dawidek 	if (cp->provider != NULL) {
618c58794deSPawel Jakub Dawidek 		if (cp->acr == 1)
619c58794deSPawel Jakub Dawidek 			g_access(cp, -1, 0, 0);
620c58794deSPawel Jakub Dawidek 		g_detach(cp);
621c58794deSPawel Jakub Dawidek 	}
622c58794deSPawel Jakub Dawidek 	g_destroy_consumer(cp);
623c58794deSPawel Jakub Dawidek 	g_destroy_geom(gp);
624c58794deSPawel Jakub Dawidek 	return (error);
625c58794deSPawel Jakub Dawidek }
626c58794deSPawel Jakub Dawidek 
627c58794deSPawel Jakub Dawidek /*
628c58794deSPawel Jakub Dawidek  * The function is called when we had last close on provider and user requested
629c58794deSPawel Jakub Dawidek  * to close it when this situation occur.
630c58794deSPawel Jakub Dawidek  */
631c58794deSPawel Jakub Dawidek static void
63219351a14SAlexander Motin g_eli_last_close(void *arg, int flags __unused)
633c58794deSPawel Jakub Dawidek {
634c58794deSPawel Jakub Dawidek 	struct g_geom *gp;
63519351a14SAlexander Motin 	char gpname[64];
636c58794deSPawel Jakub Dawidek 	int error;
637c58794deSPawel Jakub Dawidek 
638c58794deSPawel Jakub Dawidek 	g_topology_assert();
63919351a14SAlexander Motin 	gp = arg;
64019351a14SAlexander Motin 	strlcpy(gpname, gp->name, sizeof(gpname));
64119351a14SAlexander Motin 	error = g_eli_destroy(gp->softc, TRUE);
642c58794deSPawel Jakub Dawidek 	KASSERT(error == 0, ("Cannot detach %s on last close (error=%d).",
64319351a14SAlexander Motin 	    gpname, error));
64419351a14SAlexander Motin 	G_ELI_DEBUG(0, "Detached %s on last close.", gpname);
645c58794deSPawel Jakub Dawidek }
646c58794deSPawel Jakub Dawidek 
647c58794deSPawel Jakub Dawidek int
648c58794deSPawel Jakub Dawidek g_eli_access(struct g_provider *pp, int dr, int dw, int de)
649c58794deSPawel Jakub Dawidek {
650c58794deSPawel Jakub Dawidek 	struct g_eli_softc *sc;
651c58794deSPawel Jakub Dawidek 	struct g_geom *gp;
652c58794deSPawel Jakub Dawidek 
653c58794deSPawel Jakub Dawidek 	gp = pp->geom;
654c58794deSPawel Jakub Dawidek 	sc = gp->softc;
655c58794deSPawel Jakub Dawidek 
656c58794deSPawel Jakub Dawidek 	if (dw > 0) {
65785059016SPawel Jakub Dawidek 		if (sc->sc_flags & G_ELI_FLAG_RO) {
65885059016SPawel Jakub Dawidek 			/* Deny write attempts. */
65985059016SPawel Jakub Dawidek 			return (EROFS);
66085059016SPawel Jakub Dawidek 		}
661c58794deSPawel Jakub Dawidek 		/* Someone is opening us for write, we need to remember that. */
662c58794deSPawel Jakub Dawidek 		sc->sc_flags |= G_ELI_FLAG_WOPEN;
663c58794deSPawel Jakub Dawidek 		return (0);
664c58794deSPawel Jakub Dawidek 	}
665c58794deSPawel Jakub Dawidek 	/* Is this the last close? */
666c58794deSPawel Jakub Dawidek 	if (pp->acr + dr > 0 || pp->acw + dw > 0 || pp->ace + de > 0)
667c58794deSPawel Jakub Dawidek 		return (0);
668c58794deSPawel Jakub Dawidek 
669c58794deSPawel Jakub Dawidek 	/*
670c58794deSPawel Jakub Dawidek 	 * Automatically detach on last close if requested.
671c58794deSPawel Jakub Dawidek 	 */
672c58794deSPawel Jakub Dawidek 	if ((sc->sc_flags & G_ELI_FLAG_RW_DETACH) ||
673c58794deSPawel Jakub Dawidek 	    (sc->sc_flags & G_ELI_FLAG_WOPEN)) {
67419351a14SAlexander Motin 		g_post_event(g_eli_last_close, gp, M_WAITOK, NULL);
675c58794deSPawel Jakub Dawidek 	}
676c58794deSPawel Jakub Dawidek 	return (0);
677c58794deSPawel Jakub Dawidek }
678c58794deSPawel Jakub Dawidek 
679eba8f137SPawel Jakub Dawidek static int
680eba8f137SPawel Jakub Dawidek g_eli_cpu_is_disabled(int cpu)
681eba8f137SPawel Jakub Dawidek {
682eba8f137SPawel Jakub Dawidek #ifdef SMP
68371a19bdcSAttilio Rao 	return (CPU_ISSET(cpu, &hlt_cpus_mask));
684eba8f137SPawel Jakub Dawidek #else
685eba8f137SPawel Jakub Dawidek 	return (0);
686eba8f137SPawel Jakub Dawidek #endif
687eba8f137SPawel Jakub Dawidek }
688eba8f137SPawel Jakub Dawidek 
689c58794deSPawel Jakub Dawidek struct g_geom *
690c58794deSPawel Jakub Dawidek g_eli_create(struct gctl_req *req, struct g_class *mp, struct g_provider *bpp,
691c58794deSPawel Jakub Dawidek     const struct g_eli_metadata *md, const u_char *mkey, int nkey)
692c58794deSPawel Jakub Dawidek {
693c58794deSPawel Jakub Dawidek 	struct g_eli_softc *sc;
694c58794deSPawel Jakub Dawidek 	struct g_eli_worker *wr;
695c58794deSPawel Jakub Dawidek 	struct g_geom *gp;
696c58794deSPawel Jakub Dawidek 	struct g_provider *pp;
697c58794deSPawel Jakub Dawidek 	struct g_consumer *cp;
698c58794deSPawel Jakub Dawidek 	u_int i, threads;
699c58794deSPawel Jakub Dawidek 	int error;
700c58794deSPawel Jakub Dawidek 
701c58794deSPawel Jakub Dawidek 	G_ELI_DEBUG(1, "Creating device %s%s.", bpp->name, G_ELI_SUFFIX);
702c58794deSPawel Jakub Dawidek 
703c58794deSPawel Jakub Dawidek 	gp = g_new_geomf(mp, "%s%s", bpp->name, G_ELI_SUFFIX);
704c58794deSPawel Jakub Dawidek 	sc = malloc(sizeof(*sc), M_ELI, M_WAITOK | M_ZERO);
705c58794deSPawel Jakub Dawidek 	gp->start = g_eli_start;
706c58794deSPawel Jakub Dawidek 	/*
7074273d412SPawel Jakub Dawidek 	 * Spoiling can happen even though we have the provider open
7084273d412SPawel Jakub Dawidek 	 * exclusively, e.g. through media change events.
709c58794deSPawel Jakub Dawidek 	 */
7104273d412SPawel Jakub Dawidek 	gp->spoiled = g_eli_orphan;
711c58794deSPawel Jakub Dawidek 	gp->orphan = g_eli_orphan;
71285059016SPawel Jakub Dawidek 	gp->dumpconf = g_eli_dumpconf;
713c58794deSPawel Jakub Dawidek 	/*
71485059016SPawel Jakub Dawidek 	 * If detach-on-last-close feature is not enabled and we don't operate
71585059016SPawel Jakub Dawidek 	 * on read-only provider, we can simply use g_std_access().
716c58794deSPawel Jakub Dawidek 	 */
71785059016SPawel Jakub Dawidek 	if (md->md_flags & (G_ELI_FLAG_WO_DETACH | G_ELI_FLAG_RO))
718c58794deSPawel Jakub Dawidek 		gp->access = g_eli_access;
719c58794deSPawel Jakub Dawidek 	else
720c58794deSPawel Jakub Dawidek 		gp->access = g_std_access;
721c58794deSPawel Jakub Dawidek 
7224332fecaSAllan Jude 	eli_metadata_softc(sc, md, bpp->sectorsize, bpp->mediasize);
723c58794deSPawel Jakub Dawidek 	sc->sc_nkey = nkey;
724eaa3b919SPawel Jakub Dawidek 
725c58794deSPawel Jakub Dawidek 	gp->softc = sc;
726c58794deSPawel Jakub Dawidek 	sc->sc_geom = gp;
727c58794deSPawel Jakub Dawidek 
728c58794deSPawel Jakub Dawidek 	bioq_init(&sc->sc_queue);
729c58794deSPawel Jakub Dawidek 	mtx_init(&sc->sc_queue_mtx, "geli:queue", NULL, MTX_DEF);
7301e09ff3dSPawel Jakub Dawidek 	mtx_init(&sc->sc_ekeys_lock, "geli:ekeys", NULL, MTX_DEF);
731c58794deSPawel Jakub Dawidek 
732c58794deSPawel Jakub Dawidek 	pp = NULL;
733c58794deSPawel Jakub Dawidek 	cp = g_new_consumer(gp);
734c58794deSPawel Jakub Dawidek 	error = g_attach(cp, bpp);
735c58794deSPawel Jakub Dawidek 	if (error != 0) {
736c58794deSPawel Jakub Dawidek 		if (req != NULL) {
737c58794deSPawel Jakub Dawidek 			gctl_error(req, "Cannot attach to %s (error=%d).",
738c58794deSPawel Jakub Dawidek 			    bpp->name, error);
739c58794deSPawel Jakub Dawidek 		} else {
740c58794deSPawel Jakub Dawidek 			G_ELI_DEBUG(1, "Cannot attach to %s (error=%d).",
741c58794deSPawel Jakub Dawidek 			    bpp->name, error);
742c58794deSPawel Jakub Dawidek 		}
743c58794deSPawel Jakub Dawidek 		goto failed;
744c58794deSPawel Jakub Dawidek 	}
745c58794deSPawel Jakub Dawidek 	/*
746c58794deSPawel Jakub Dawidek 	 * Keep provider open all the time, so we can run critical tasks,
747c58794deSPawel Jakub Dawidek 	 * like Master Keys deletion, without wondering if we can open
748c58794deSPawel Jakub Dawidek 	 * provider or not.
74985059016SPawel Jakub Dawidek 	 * We don't open provider for writing only when user requested read-only
75085059016SPawel Jakub Dawidek 	 * access.
751c58794deSPawel Jakub Dawidek 	 */
75285059016SPawel Jakub Dawidek 	if (sc->sc_flags & G_ELI_FLAG_RO)
75385059016SPawel Jakub Dawidek 		error = g_access(cp, 1, 0, 1);
75485059016SPawel Jakub Dawidek 	else
755c58794deSPawel Jakub Dawidek 		error = g_access(cp, 1, 1, 1);
756c58794deSPawel Jakub Dawidek 	if (error != 0) {
757c58794deSPawel Jakub Dawidek 		if (req != NULL) {
758c58794deSPawel Jakub Dawidek 			gctl_error(req, "Cannot access %s (error=%d).",
759c58794deSPawel Jakub Dawidek 			    bpp->name, error);
760c58794deSPawel Jakub Dawidek 		} else {
761c58794deSPawel Jakub Dawidek 			G_ELI_DEBUG(1, "Cannot access %s (error=%d).",
762c58794deSPawel Jakub Dawidek 			    bpp->name, error);
763c58794deSPawel Jakub Dawidek 		}
764c58794deSPawel Jakub Dawidek 		goto failed;
765c58794deSPawel Jakub Dawidek 	}
766c58794deSPawel Jakub Dawidek 
767c6a26d4cSPawel Jakub Dawidek 	/*
768c6a26d4cSPawel Jakub Dawidek 	 * Remember the keys in our softc structure.
769c6a26d4cSPawel Jakub Dawidek 	 */
770c6a26d4cSPawel Jakub Dawidek 	g_eli_mkey_propagate(sc, mkey);
771c6a26d4cSPawel Jakub Dawidek 
772c58794deSPawel Jakub Dawidek 	LIST_INIT(&sc->sc_workers);
773c58794deSPawel Jakub Dawidek 
774c58794deSPawel Jakub Dawidek 	threads = g_eli_threads;
775dd549194SPawel Jakub Dawidek 	if (threads == 0)
776dd549194SPawel Jakub Dawidek 		threads = mp_ncpus;
7770c879bd9SPawel Jakub Dawidek 	sc->sc_cpubind = (mp_ncpus > 1 && threads == mp_ncpus);
778c58794deSPawel Jakub Dawidek 	for (i = 0; i < threads; i++) {
779eba8f137SPawel Jakub Dawidek 		if (g_eli_cpu_is_disabled(i)) {
780eba8f137SPawel Jakub Dawidek 			G_ELI_DEBUG(1, "%s: CPU %u disabled, skipping.",
7811506db21SPawel Jakub Dawidek 			    bpp->name, i);
782eba8f137SPawel Jakub Dawidek 			continue;
783eba8f137SPawel Jakub Dawidek 		}
784c58794deSPawel Jakub Dawidek 		wr = malloc(sizeof(*wr), M_ELI, M_WAITOK | M_ZERO);
785c58794deSPawel Jakub Dawidek 		wr->w_softc = sc;
786c58794deSPawel Jakub Dawidek 		wr->w_number = i;
7875ad4a7c7SPawel Jakub Dawidek 		wr->w_active = TRUE;
788c58794deSPawel Jakub Dawidek 
7893ac01bc2SPawel Jakub Dawidek 		error = g_eli_newsession(wr);
790c58794deSPawel Jakub Dawidek 		if (error != 0) {
791c58794deSPawel Jakub Dawidek 			free(wr, M_ELI);
792c58794deSPawel Jakub Dawidek 			if (req != NULL) {
793c58794deSPawel Jakub Dawidek 				gctl_error(req, "Cannot set up crypto session "
794c58794deSPawel Jakub Dawidek 				    "for %s (error=%d).", bpp->name, error);
795c58794deSPawel Jakub Dawidek 			} else {
796c58794deSPawel Jakub Dawidek 				G_ELI_DEBUG(1, "Cannot set up crypto session "
797c58794deSPawel Jakub Dawidek 				    "for %s (error=%d).", bpp->name, error);
798c58794deSPawel Jakub Dawidek 			}
799c58794deSPawel Jakub Dawidek 			goto failed;
800c58794deSPawel Jakub Dawidek 		}
801c58794deSPawel Jakub Dawidek 
8023745c395SJulian Elischer 		error = kproc_create(g_eli_worker, wr, &wr->w_proc, 0, 0,
803c58794deSPawel Jakub Dawidek 		    "g_eli[%u] %s", i, bpp->name);
804c58794deSPawel Jakub Dawidek 		if (error != 0) {
8053ac01bc2SPawel Jakub Dawidek 			g_eli_freesession(wr);
806c58794deSPawel Jakub Dawidek 			free(wr, M_ELI);
807c58794deSPawel Jakub Dawidek 			if (req != NULL) {
808dddd1d53SPawel Jakub Dawidek 				gctl_error(req, "Cannot create kernel thread "
809dddd1d53SPawel Jakub Dawidek 				    "for %s (error=%d).", bpp->name, error);
810c58794deSPawel Jakub Dawidek 			} else {
811dddd1d53SPawel Jakub Dawidek 				G_ELI_DEBUG(1, "Cannot create kernel thread "
812dddd1d53SPawel Jakub Dawidek 				    "for %s (error=%d).", bpp->name, error);
813c58794deSPawel Jakub Dawidek 			}
814c58794deSPawel Jakub Dawidek 			goto failed;
815c58794deSPawel Jakub Dawidek 		}
816c58794deSPawel Jakub Dawidek 		LIST_INSERT_HEAD(&sc->sc_workers, wr, w_next);
817c58794deSPawel Jakub Dawidek 	}
818c58794deSPawel Jakub Dawidek 
819c58794deSPawel Jakub Dawidek 	/*
820c58794deSPawel Jakub Dawidek 	 * Create decrypted provider.
821c58794deSPawel Jakub Dawidek 	 */
822c58794deSPawel Jakub Dawidek 	pp = g_new_providerf(gp, "%s%s", bpp->name, G_ELI_SUFFIX);
823c6a26d4cSPawel Jakub Dawidek 	pp->mediasize = sc->sc_mediasize;
824c6a26d4cSPawel Jakub Dawidek 	pp->sectorsize = sc->sc_sectorsize;
825eaa3b919SPawel Jakub Dawidek 
826c58794deSPawel Jakub Dawidek 	g_error_provider(pp, 0);
827c58794deSPawel Jakub Dawidek 
828c58794deSPawel Jakub Dawidek 	G_ELI_DEBUG(0, "Device %s created.", pp->name);
829eaa3b919SPawel Jakub Dawidek 	G_ELI_DEBUG(0, "Encryption: %s %u", g_eli_algo2str(sc->sc_ealgo),
830eaa3b919SPawel Jakub Dawidek 	    sc->sc_ekeylen);
831eaa3b919SPawel Jakub Dawidek 	if (sc->sc_flags & G_ELI_FLAG_AUTH)
832eaa3b919SPawel Jakub Dawidek 		G_ELI_DEBUG(0, " Integrity: %s", g_eli_algo2str(sc->sc_aalgo));
833c58794deSPawel Jakub Dawidek 	G_ELI_DEBUG(0, "    Crypto: %s",
834c58794deSPawel Jakub Dawidek 	    sc->sc_crypto == G_ELI_CRYPTO_SW ? "software" : "hardware");
835c58794deSPawel Jakub Dawidek 	return (gp);
836c58794deSPawel Jakub Dawidek failed:
837c58794deSPawel Jakub Dawidek 	mtx_lock(&sc->sc_queue_mtx);
838c58794deSPawel Jakub Dawidek 	sc->sc_flags |= G_ELI_FLAG_DESTROY;
839c58794deSPawel Jakub Dawidek 	wakeup(sc);
840c58794deSPawel Jakub Dawidek 	/*
841c58794deSPawel Jakub Dawidek 	 * Wait for kernel threads self destruction.
842c58794deSPawel Jakub Dawidek 	 */
843c58794deSPawel Jakub Dawidek 	while (!LIST_EMPTY(&sc->sc_workers)) {
844c58794deSPawel Jakub Dawidek 		msleep(&sc->sc_workers, &sc->sc_queue_mtx, PRIBIO,
845c58794deSPawel Jakub Dawidek 		    "geli:destroy", 0);
846c58794deSPawel Jakub Dawidek 	}
847c58794deSPawel Jakub Dawidek 	mtx_destroy(&sc->sc_queue_mtx);
848c58794deSPawel Jakub Dawidek 	if (cp->provider != NULL) {
849c58794deSPawel Jakub Dawidek 		if (cp->acr == 1)
850c58794deSPawel Jakub Dawidek 			g_access(cp, -1, -1, -1);
851c58794deSPawel Jakub Dawidek 		g_detach(cp);
852c58794deSPawel Jakub Dawidek 	}
853c58794deSPawel Jakub Dawidek 	g_destroy_consumer(cp);
854c58794deSPawel Jakub Dawidek 	g_destroy_geom(gp);
8551e09ff3dSPawel Jakub Dawidek 	g_eli_key_destroy(sc);
856c58794deSPawel Jakub Dawidek 	bzero(sc, sizeof(*sc));
857c58794deSPawel Jakub Dawidek 	free(sc, M_ELI);
858c58794deSPawel Jakub Dawidek 	return (NULL);
859c58794deSPawel Jakub Dawidek }
860c58794deSPawel Jakub Dawidek 
861c58794deSPawel Jakub Dawidek int
862c58794deSPawel Jakub Dawidek g_eli_destroy(struct g_eli_softc *sc, boolean_t force)
863c58794deSPawel Jakub Dawidek {
864c58794deSPawel Jakub Dawidek 	struct g_geom *gp;
865c58794deSPawel Jakub Dawidek 	struct g_provider *pp;
866c58794deSPawel Jakub Dawidek 
867c58794deSPawel Jakub Dawidek 	g_topology_assert();
868c58794deSPawel Jakub Dawidek 
869c58794deSPawel Jakub Dawidek 	if (sc == NULL)
870c58794deSPawel Jakub Dawidek 		return (ENXIO);
871c58794deSPawel Jakub Dawidek 
872c58794deSPawel Jakub Dawidek 	gp = sc->sc_geom;
873c58794deSPawel Jakub Dawidek 	pp = LIST_FIRST(&gp->provider);
874c58794deSPawel Jakub Dawidek 	if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
875c58794deSPawel Jakub Dawidek 		if (force) {
876c58794deSPawel Jakub Dawidek 			G_ELI_DEBUG(1, "Device %s is still open, so it "
87798645006SChristian Brueffer 			    "cannot be definitely removed.", pp->name);
87819351a14SAlexander Motin 			sc->sc_flags |= G_ELI_FLAG_RW_DETACH;
87919351a14SAlexander Motin 			gp->access = g_eli_access;
88019351a14SAlexander Motin 			g_wither_provider(pp, ENXIO);
88119351a14SAlexander Motin 			return (EBUSY);
882c58794deSPawel Jakub Dawidek 		} else {
883c58794deSPawel Jakub Dawidek 			G_ELI_DEBUG(1,
884c58794deSPawel Jakub Dawidek 			    "Device %s is still open (r%dw%de%d).", pp->name,
885c58794deSPawel Jakub Dawidek 			    pp->acr, pp->acw, pp->ace);
886c58794deSPawel Jakub Dawidek 			return (EBUSY);
887c58794deSPawel Jakub Dawidek 		}
888c58794deSPawel Jakub Dawidek 	}
889c58794deSPawel Jakub Dawidek 
890c58794deSPawel Jakub Dawidek 	mtx_lock(&sc->sc_queue_mtx);
891c58794deSPawel Jakub Dawidek 	sc->sc_flags |= G_ELI_FLAG_DESTROY;
892c58794deSPawel Jakub Dawidek 	wakeup(sc);
893c58794deSPawel Jakub Dawidek 	while (!LIST_EMPTY(&sc->sc_workers)) {
894c58794deSPawel Jakub Dawidek 		msleep(&sc->sc_workers, &sc->sc_queue_mtx, PRIBIO,
895c58794deSPawel Jakub Dawidek 		    "geli:destroy", 0);
896c58794deSPawel Jakub Dawidek 	}
897c58794deSPawel Jakub Dawidek 	mtx_destroy(&sc->sc_queue_mtx);
898c58794deSPawel Jakub Dawidek 	gp->softc = NULL;
8991e09ff3dSPawel Jakub Dawidek 	g_eli_key_destroy(sc);
900c58794deSPawel Jakub Dawidek 	bzero(sc, sizeof(*sc));
901c58794deSPawel Jakub Dawidek 	free(sc, M_ELI);
902c58794deSPawel Jakub Dawidek 
903c58794deSPawel Jakub Dawidek 	if (pp == NULL || (pp->acr == 0 && pp->acw == 0 && pp->ace == 0))
904c58794deSPawel Jakub Dawidek 		G_ELI_DEBUG(0, "Device %s destroyed.", gp->name);
905c58794deSPawel Jakub Dawidek 	g_wither_geom_close(gp, ENXIO);
906c58794deSPawel Jakub Dawidek 
907c58794deSPawel Jakub Dawidek 	return (0);
908c58794deSPawel Jakub Dawidek }
909c58794deSPawel Jakub Dawidek 
910c58794deSPawel Jakub Dawidek static int
911c58794deSPawel Jakub Dawidek g_eli_destroy_geom(struct gctl_req *req __unused,
912c58794deSPawel Jakub Dawidek     struct g_class *mp __unused, struct g_geom *gp)
913c58794deSPawel Jakub Dawidek {
914c58794deSPawel Jakub Dawidek 	struct g_eli_softc *sc;
915c58794deSPawel Jakub Dawidek 
916c58794deSPawel Jakub Dawidek 	sc = gp->softc;
9175ad4a7c7SPawel Jakub Dawidek 	return (g_eli_destroy(sc, FALSE));
918c58794deSPawel Jakub Dawidek }
919c58794deSPawel Jakub Dawidek 
9209af2131bSPawel Jakub Dawidek static int
9219af2131bSPawel Jakub Dawidek g_eli_keyfiles_load(struct hmac_ctx *ctx, const char *provider)
9229af2131bSPawel Jakub Dawidek {
9231e189c08SMarcel Moolenaar 	u_char *keyfile, *data;
9249af2131bSPawel Jakub Dawidek 	char *file, name[64];
9251e189c08SMarcel Moolenaar 	size_t size;
9269af2131bSPawel Jakub Dawidek 	int i;
9279af2131bSPawel Jakub Dawidek 
9289af2131bSPawel Jakub Dawidek 	for (i = 0; ; i++) {
9299af2131bSPawel Jakub Dawidek 		snprintf(name, sizeof(name), "%s:geli_keyfile%d", provider, i);
9309af2131bSPawel Jakub Dawidek 		keyfile = preload_search_by_type(name);
931edaa9008SPawel Jakub Dawidek 		if (keyfile == NULL && i == 0) {
932edaa9008SPawel Jakub Dawidek 			/*
933edaa9008SPawel Jakub Dawidek 			 * If there is only one keyfile, allow simpler name.
934edaa9008SPawel Jakub Dawidek 			 */
935edaa9008SPawel Jakub Dawidek 			snprintf(name, sizeof(name), "%s:geli_keyfile", provider);
936edaa9008SPawel Jakub Dawidek 			keyfile = preload_search_by_type(name);
937edaa9008SPawel Jakub Dawidek 		}
9389af2131bSPawel Jakub Dawidek 		if (keyfile == NULL)
9399af2131bSPawel Jakub Dawidek 			return (i);	/* Return number of loaded keyfiles. */
9401e189c08SMarcel Moolenaar 		data = preload_fetch_addr(keyfile);
9419af2131bSPawel Jakub Dawidek 		if (data == NULL) {
9429af2131bSPawel Jakub Dawidek 			G_ELI_DEBUG(0, "Cannot find key file data for %s.",
9439af2131bSPawel Jakub Dawidek 			    name);
9449af2131bSPawel Jakub Dawidek 			return (0);
9459af2131bSPawel Jakub Dawidek 		}
9461e189c08SMarcel Moolenaar 		size = preload_fetch_size(keyfile);
9471e189c08SMarcel Moolenaar 		if (size == 0) {
9489af2131bSPawel Jakub Dawidek 			G_ELI_DEBUG(0, "Cannot find key file size for %s.",
9499af2131bSPawel Jakub Dawidek 			    name);
9509af2131bSPawel Jakub Dawidek 			return (0);
9519af2131bSPawel Jakub Dawidek 		}
9529af2131bSPawel Jakub Dawidek 		file = preload_search_info(keyfile, MODINFO_NAME);
9539af2131bSPawel Jakub Dawidek 		if (file == NULL) {
9549af2131bSPawel Jakub Dawidek 			G_ELI_DEBUG(0, "Cannot find key file name for %s.",
9559af2131bSPawel Jakub Dawidek 			    name);
9569af2131bSPawel Jakub Dawidek 			return (0);
9579af2131bSPawel Jakub Dawidek 		}
9589af2131bSPawel Jakub Dawidek 		G_ELI_DEBUG(1, "Loaded keyfile %s for %s (type: %s).", file,
9599af2131bSPawel Jakub Dawidek 		    provider, name);
9601e189c08SMarcel Moolenaar 		g_eli_crypto_hmac_update(ctx, data, size);
9619af2131bSPawel Jakub Dawidek 	}
9629af2131bSPawel Jakub Dawidek }
9639af2131bSPawel Jakub Dawidek 
9649af2131bSPawel Jakub Dawidek static void
9659af2131bSPawel Jakub Dawidek g_eli_keyfiles_clear(const char *provider)
9669af2131bSPawel Jakub Dawidek {
9671e189c08SMarcel Moolenaar 	u_char *keyfile, *data;
9689af2131bSPawel Jakub Dawidek 	char name[64];
9691e189c08SMarcel Moolenaar 	size_t size;
9709af2131bSPawel Jakub Dawidek 	int i;
9719af2131bSPawel Jakub Dawidek 
9729af2131bSPawel Jakub Dawidek 	for (i = 0; ; i++) {
9739af2131bSPawel Jakub Dawidek 		snprintf(name, sizeof(name), "%s:geli_keyfile%d", provider, i);
9749af2131bSPawel Jakub Dawidek 		keyfile = preload_search_by_type(name);
9759af2131bSPawel Jakub Dawidek 		if (keyfile == NULL)
9769af2131bSPawel Jakub Dawidek 			return;
9771e189c08SMarcel Moolenaar 		data = preload_fetch_addr(keyfile);
9781e189c08SMarcel Moolenaar 		size = preload_fetch_size(keyfile);
9791e189c08SMarcel Moolenaar 		if (data != NULL && size != 0)
9801e189c08SMarcel Moolenaar 			bzero(data, size);
9819af2131bSPawel Jakub Dawidek 	}
9829af2131bSPawel Jakub Dawidek }
9839af2131bSPawel Jakub Dawidek 
984c58794deSPawel Jakub Dawidek /*
985c58794deSPawel Jakub Dawidek  * Tasting is only made on boot.
986c58794deSPawel Jakub Dawidek  * We detect providers which should be attached before root is mounted.
987c58794deSPawel Jakub Dawidek  */
988c58794deSPawel Jakub Dawidek static struct g_geom *
989c58794deSPawel Jakub Dawidek g_eli_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
990c58794deSPawel Jakub Dawidek {
991c58794deSPawel Jakub Dawidek 	struct g_eli_metadata md;
992c58794deSPawel Jakub Dawidek 	struct g_geom *gp;
993c58794deSPawel Jakub Dawidek 	struct hmac_ctx ctx;
994c58794deSPawel Jakub Dawidek 	char passphrase[256];
995c58794deSPawel Jakub Dawidek 	u_char key[G_ELI_USERKEYLEN], mkey[G_ELI_DATAIVKEYLEN];
9969af2131bSPawel Jakub Dawidek 	u_int i, nkey, nkeyfiles, tries;
997c58794deSPawel Jakub Dawidek 	int error;
998c58794deSPawel Jakub Dawidek 
999c58794deSPawel Jakub Dawidek 	g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
1000c58794deSPawel Jakub Dawidek 	g_topology_assert();
1001c58794deSPawel Jakub Dawidek 
1002df3aed4fSPawel Jakub Dawidek 	if (root_mounted() || g_eli_tries == 0)
1003c58794deSPawel Jakub Dawidek 		return (NULL);
1004c58794deSPawel Jakub Dawidek 
1005c58794deSPawel Jakub Dawidek 	G_ELI_DEBUG(3, "Tasting %s.", pp->name);
1006c58794deSPawel Jakub Dawidek 
1007c58794deSPawel Jakub Dawidek 	error = g_eli_read_metadata(mp, pp, &md);
1008c58794deSPawel Jakub Dawidek 	if (error != 0)
1009c58794deSPawel Jakub Dawidek 		return (NULL);
1010c58794deSPawel Jakub Dawidek 	gp = NULL;
1011c58794deSPawel Jakub Dawidek 
1012c58794deSPawel Jakub Dawidek 	if (strcmp(md.md_magic, G_ELI_MAGIC) != 0)
1013c58794deSPawel Jakub Dawidek 		return (NULL);
1014c58794deSPawel Jakub Dawidek 	if (md.md_version > G_ELI_VERSION) {
1015c58794deSPawel Jakub Dawidek 		printf("geom_eli.ko module is too old to handle %s.\n",
1016c58794deSPawel Jakub Dawidek 		    pp->name);
1017c58794deSPawel Jakub Dawidek 		return (NULL);
1018c58794deSPawel Jakub Dawidek 	}
1019c58794deSPawel Jakub Dawidek 	if (md.md_provsize != pp->mediasize)
1020c58794deSPawel Jakub Dawidek 		return (NULL);
1021c58794deSPawel Jakub Dawidek 	/* Should we attach it on boot? */
1022eaa3b919SPawel Jakub Dawidek 	if (!(md.md_flags & G_ELI_FLAG_BOOT))
1023c58794deSPawel Jakub Dawidek 		return (NULL);
1024c58794deSPawel Jakub Dawidek 	if (md.md_keys == 0x00) {
1025c58794deSPawel Jakub Dawidek 		G_ELI_DEBUG(0, "No valid keys on %s.", pp->name);
1026c58794deSPawel Jakub Dawidek 		return (NULL);
1027c58794deSPawel Jakub Dawidek 	}
10289af2131bSPawel Jakub Dawidek 	if (md.md_iterations == -1) {
10299af2131bSPawel Jakub Dawidek 		/* If there is no passphrase, we try only once. */
10309af2131bSPawel Jakub Dawidek 		tries = 1;
10319af2131bSPawel Jakub Dawidek 	} else {
10329af2131bSPawel Jakub Dawidek 		/* Ask for the passphrase no more than g_eli_tries times. */
10339af2131bSPawel Jakub Dawidek 		tries = g_eli_tries;
10349af2131bSPawel Jakub Dawidek 	}
10359af2131bSPawel Jakub Dawidek 
1036835c4dd4SColin Percival 	for (i = 0; i <= tries; i++) {
10379af2131bSPawel Jakub Dawidek 		g_eli_crypto_hmac_init(&ctx, NULL, 0);
1038c58794deSPawel Jakub Dawidek 
1039c58794deSPawel Jakub Dawidek 		/*
10409af2131bSPawel Jakub Dawidek 		 * Load all key files.
1041c58794deSPawel Jakub Dawidek 		 */
10429af2131bSPawel Jakub Dawidek 		nkeyfiles = g_eli_keyfiles_load(&ctx, pp->name);
10439af2131bSPawel Jakub Dawidek 
10449af2131bSPawel Jakub Dawidek 		if (nkeyfiles == 0 && md.md_iterations == -1) {
10459af2131bSPawel Jakub Dawidek 			/*
10469af2131bSPawel Jakub Dawidek 			 * No key files and no passphrase, something is
10479af2131bSPawel Jakub Dawidek 			 * definitely wrong here.
10489af2131bSPawel Jakub Dawidek 			 * geli(8) doesn't allow for such situation, so assume
10499af2131bSPawel Jakub Dawidek 			 * that there was really no passphrase and in that case
10509af2131bSPawel Jakub Dawidek 			 * key files are no properly defined in loader.conf.
10519af2131bSPawel Jakub Dawidek 			 */
10529af2131bSPawel Jakub Dawidek 			G_ELI_DEBUG(0,
10539af2131bSPawel Jakub Dawidek 			    "Found no key files in loader.conf for %s.",
10549af2131bSPawel Jakub Dawidek 			    pp->name);
10559af2131bSPawel Jakub Dawidek 			return (NULL);
10569af2131bSPawel Jakub Dawidek 		}
10579af2131bSPawel Jakub Dawidek 
10589af2131bSPawel Jakub Dawidek 		/* Ask for the passphrase if defined. */
10599af2131bSPawel Jakub Dawidek 		if (md.md_iterations >= 0) {
1060835c4dd4SColin Percival 			/* Try first with cached passphrase. */
1061835c4dd4SColin Percival 			if (i == 0) {
1062835c4dd4SColin Percival 				if (!g_eli_boot_passcache)
1063835c4dd4SColin Percival 					continue;
1064835c4dd4SColin Percival 				memcpy(passphrase, cached_passphrase,
1065835c4dd4SColin Percival 				    sizeof(passphrase));
1066835c4dd4SColin Percival 			} else {
1067c58794deSPawel Jakub Dawidek 				printf("Enter passphrase for %s: ", pp->name);
1068f6ce353eSAndriy Gapon 				cngets(passphrase, sizeof(passphrase),
10699af2131bSPawel Jakub Dawidek 				    g_eli_visible_passphrase);
1070835c4dd4SColin Percival 				memcpy(cached_passphrase, passphrase,
1071835c4dd4SColin Percival 				    sizeof(passphrase));
1072835c4dd4SColin Percival 			}
10739af2131bSPawel Jakub Dawidek 		}
10749af2131bSPawel Jakub Dawidek 
1075c58794deSPawel Jakub Dawidek 		/*
1076c58794deSPawel Jakub Dawidek 		 * Prepare Derived-Key from the user passphrase.
1077c58794deSPawel Jakub Dawidek 		 */
1078c58794deSPawel Jakub Dawidek 		if (md.md_iterations == 0) {
1079c58794deSPawel Jakub Dawidek 			g_eli_crypto_hmac_update(&ctx, md.md_salt,
1080c58794deSPawel Jakub Dawidek 			    sizeof(md.md_salt));
1081c58794deSPawel Jakub Dawidek 			g_eli_crypto_hmac_update(&ctx, passphrase,
1082c58794deSPawel Jakub Dawidek 			    strlen(passphrase));
10835527ecd9SPawel Jakub Dawidek 			bzero(passphrase, sizeof(passphrase));
10849af2131bSPawel Jakub Dawidek 		} else if (md.md_iterations > 0) {
1085c58794deSPawel Jakub Dawidek 			u_char dkey[G_ELI_USERKEYLEN];
1086c58794deSPawel Jakub Dawidek 
1087c58794deSPawel Jakub Dawidek 			pkcs5v2_genkey(dkey, sizeof(dkey), md.md_salt,
1088c58794deSPawel Jakub Dawidek 			    sizeof(md.md_salt), passphrase, md.md_iterations);
10895527ecd9SPawel Jakub Dawidek 			bzero(passphrase, sizeof(passphrase));
1090c58794deSPawel Jakub Dawidek 			g_eli_crypto_hmac_update(&ctx, dkey, sizeof(dkey));
1091c58794deSPawel Jakub Dawidek 			bzero(dkey, sizeof(dkey));
1092c58794deSPawel Jakub Dawidek 		}
10939af2131bSPawel Jakub Dawidek 
1094c58794deSPawel Jakub Dawidek 		g_eli_crypto_hmac_final(&ctx, key, 0);
1095c58794deSPawel Jakub Dawidek 
1096c58794deSPawel Jakub Dawidek 		/*
1097c58794deSPawel Jakub Dawidek 		 * Decrypt Master-Key.
1098c58794deSPawel Jakub Dawidek 		 */
1099c58794deSPawel Jakub Dawidek 		error = g_eli_mkey_decrypt(&md, key, mkey, &nkey);
1100c58794deSPawel Jakub Dawidek 		bzero(key, sizeof(key));
1101c58794deSPawel Jakub Dawidek 		if (error == -1) {
1102835c4dd4SColin Percival 			if (i == tries) {
11039af2131bSPawel Jakub Dawidek 				G_ELI_DEBUG(0,
11049af2131bSPawel Jakub Dawidek 				    "Wrong key for %s. No tries left.",
11059af2131bSPawel Jakub Dawidek 				    pp->name);
11069af2131bSPawel Jakub Dawidek 				g_eli_keyfiles_clear(pp->name);
11079af2131bSPawel Jakub Dawidek 				return (NULL);
1108c58794deSPawel Jakub Dawidek 			}
1109835c4dd4SColin Percival 			if (i > 0) {
1110835c4dd4SColin Percival 				G_ELI_DEBUG(0,
1111835c4dd4SColin Percival 				    "Wrong key for %s. Tries left: %u.",
1112835c4dd4SColin Percival 				    pp->name, tries - i);
1113835c4dd4SColin Percival 			}
1114c58794deSPawel Jakub Dawidek 			/* Try again. */
1115c58794deSPawel Jakub Dawidek 			continue;
1116c58794deSPawel Jakub Dawidek 		} else if (error > 0) {
1117038c55adSPawel Jakub Dawidek 			G_ELI_DEBUG(0,
1118038c55adSPawel Jakub Dawidek 			    "Cannot decrypt Master Key for %s (error=%d).",
1119c58794deSPawel Jakub Dawidek 			    pp->name, error);
11209af2131bSPawel Jakub Dawidek 			g_eli_keyfiles_clear(pp->name);
1121c58794deSPawel Jakub Dawidek 			return (NULL);
1122c58794deSPawel Jakub Dawidek 		}
1123ebd05adaSBrad Davis 		g_eli_keyfiles_clear(pp->name);
1124c58794deSPawel Jakub Dawidek 		G_ELI_DEBUG(1, "Using Master Key %u for %s.", nkey, pp->name);
1125c58794deSPawel Jakub Dawidek 		break;
1126c58794deSPawel Jakub Dawidek 	}
1127c58794deSPawel Jakub Dawidek 
1128c58794deSPawel Jakub Dawidek 	/*
1129c58794deSPawel Jakub Dawidek 	 * We have correct key, let's attach provider.
1130c58794deSPawel Jakub Dawidek 	 */
1131c58794deSPawel Jakub Dawidek 	gp = g_eli_create(NULL, mp, pp, &md, mkey, nkey);
1132c58794deSPawel Jakub Dawidek 	bzero(mkey, sizeof(mkey));
1133c58794deSPawel Jakub Dawidek 	bzero(&md, sizeof(md));
1134c58794deSPawel Jakub Dawidek 	if (gp == NULL) {
1135c58794deSPawel Jakub Dawidek 		G_ELI_DEBUG(0, "Cannot create device %s%s.", pp->name,
1136c58794deSPawel Jakub Dawidek 		    G_ELI_SUFFIX);
1137c58794deSPawel Jakub Dawidek 		return (NULL);
1138c58794deSPawel Jakub Dawidek 	}
1139c58794deSPawel Jakub Dawidek 	return (gp);
1140c58794deSPawel Jakub Dawidek }
1141c58794deSPawel Jakub Dawidek 
1142c58794deSPawel Jakub Dawidek static void
1143c58794deSPawel Jakub Dawidek g_eli_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
1144c58794deSPawel Jakub Dawidek     struct g_consumer *cp, struct g_provider *pp)
1145c58794deSPawel Jakub Dawidek {
1146c58794deSPawel Jakub Dawidek 	struct g_eli_softc *sc;
1147c58794deSPawel Jakub Dawidek 
1148c58794deSPawel Jakub Dawidek 	g_topology_assert();
1149c58794deSPawel Jakub Dawidek 	sc = gp->softc;
1150c58794deSPawel Jakub Dawidek 	if (sc == NULL)
1151c58794deSPawel Jakub Dawidek 		return;
1152c58794deSPawel Jakub Dawidek 	if (pp != NULL || cp != NULL)
1153c58794deSPawel Jakub Dawidek 		return;	/* Nothing here. */
11541e09ff3dSPawel Jakub Dawidek 
1155743437c4SAndrey V. Elsukov 	sbuf_printf(sb, "%s<KeysTotal>%ju</KeysTotal>\n", indent,
11561e09ff3dSPawel Jakub Dawidek 	    (uintmax_t)sc->sc_ekeys_total);
1157743437c4SAndrey V. Elsukov 	sbuf_printf(sb, "%s<KeysAllocated>%ju</KeysAllocated>\n", indent,
11581e09ff3dSPawel Jakub Dawidek 	    (uintmax_t)sc->sc_ekeys_allocated);
1159ea35a2ecSPawel Jakub Dawidek 	sbuf_printf(sb, "%s<Flags>", indent);
1160ea35a2ecSPawel Jakub Dawidek 	if (sc->sc_flags == 0)
1161ea35a2ecSPawel Jakub Dawidek 		sbuf_printf(sb, "NONE");
1162ea35a2ecSPawel Jakub Dawidek 	else {
1163ea35a2ecSPawel Jakub Dawidek 		int first = 1;
1164ea35a2ecSPawel Jakub Dawidek 
1165ea35a2ecSPawel Jakub Dawidek #define ADD_FLAG(flag, name)	do {					\
1166eaa3b919SPawel Jakub Dawidek 	if (sc->sc_flags & (flag)) {					\
1167ea35a2ecSPawel Jakub Dawidek 		if (!first)						\
1168ea35a2ecSPawel Jakub Dawidek 			sbuf_printf(sb, ", ");				\
1169ea35a2ecSPawel Jakub Dawidek 		else							\
1170ea35a2ecSPawel Jakub Dawidek 			first = 0;					\
1171ea35a2ecSPawel Jakub Dawidek 		sbuf_printf(sb, name);					\
1172ea35a2ecSPawel Jakub Dawidek 	}								\
1173ea35a2ecSPawel Jakub Dawidek } while (0)
11745ad4a7c7SPawel Jakub Dawidek 		ADD_FLAG(G_ELI_FLAG_SUSPEND, "SUSPEND");
1175c6a26d4cSPawel Jakub Dawidek 		ADD_FLAG(G_ELI_FLAG_SINGLE_KEY, "SINGLE-KEY");
11762bd4ade6SPawel Jakub Dawidek 		ADD_FLAG(G_ELI_FLAG_NATIVE_BYTE_ORDER, "NATIVE-BYTE-ORDER");
1177ea35a2ecSPawel Jakub Dawidek 		ADD_FLAG(G_ELI_FLAG_ONETIME, "ONETIME");
1178ea35a2ecSPawel Jakub Dawidek 		ADD_FLAG(G_ELI_FLAG_BOOT, "BOOT");
1179ea35a2ecSPawel Jakub Dawidek 		ADD_FLAG(G_ELI_FLAG_WO_DETACH, "W-DETACH");
1180ea35a2ecSPawel Jakub Dawidek 		ADD_FLAG(G_ELI_FLAG_RW_DETACH, "RW-DETACH");
1181eaa3b919SPawel Jakub Dawidek 		ADD_FLAG(G_ELI_FLAG_AUTH, "AUTH");
1182ea35a2ecSPawel Jakub Dawidek 		ADD_FLAG(G_ELI_FLAG_WOPEN, "W-OPEN");
1183ea35a2ecSPawel Jakub Dawidek 		ADD_FLAG(G_ELI_FLAG_DESTROY, "DESTROY");
118485059016SPawel Jakub Dawidek 		ADD_FLAG(G_ELI_FLAG_RO, "READ-ONLY");
118546e34470SPawel Jakub Dawidek 		ADD_FLAG(G_ELI_FLAG_NODELETE, "NODELETE");
1186d8736625SAllan Jude 		ADD_FLAG(G_ELI_FLAG_GELIBOOT, "GELIBOOT");
1187ea35a2ecSPawel Jakub Dawidek #undef  ADD_FLAG
1188ea35a2ecSPawel Jakub Dawidek 	}
1189ea35a2ecSPawel Jakub Dawidek 	sbuf_printf(sb, "</Flags>\n");
1190ea35a2ecSPawel Jakub Dawidek 
1191eaa3b919SPawel Jakub Dawidek 	if (!(sc->sc_flags & G_ELI_FLAG_ONETIME)) {
1192ea35a2ecSPawel Jakub Dawidek 		sbuf_printf(sb, "%s<UsedKey>%u</UsedKey>\n", indent,
1193ea35a2ecSPawel Jakub Dawidek 		    sc->sc_nkey);
1194ea35a2ecSPawel Jakub Dawidek 	}
11951f8c92e6SPawel Jakub Dawidek 	sbuf_printf(sb, "%s<Version>%u</Version>\n", indent, sc->sc_version);
1196c58794deSPawel Jakub Dawidek 	sbuf_printf(sb, "%s<Crypto>", indent);
1197c58794deSPawel Jakub Dawidek 	switch (sc->sc_crypto) {
1198c58794deSPawel Jakub Dawidek 	case G_ELI_CRYPTO_HW:
1199c58794deSPawel Jakub Dawidek 		sbuf_printf(sb, "hardware");
1200c58794deSPawel Jakub Dawidek 		break;
1201c58794deSPawel Jakub Dawidek 	case G_ELI_CRYPTO_SW:
1202c58794deSPawel Jakub Dawidek 		sbuf_printf(sb, "software");
1203c58794deSPawel Jakub Dawidek 		break;
1204c58794deSPawel Jakub Dawidek 	default:
1205c58794deSPawel Jakub Dawidek 		sbuf_printf(sb, "UNKNOWN");
1206c58794deSPawel Jakub Dawidek 		break;
1207c58794deSPawel Jakub Dawidek 	}
1208c58794deSPawel Jakub Dawidek 	sbuf_printf(sb, "</Crypto>\n");
1209eaa3b919SPawel Jakub Dawidek 	if (sc->sc_flags & G_ELI_FLAG_AUTH) {
1210eaa3b919SPawel Jakub Dawidek 		sbuf_printf(sb,
1211eaa3b919SPawel Jakub Dawidek 		    "%s<AuthenticationAlgorithm>%s</AuthenticationAlgorithm>\n",
1212eaa3b919SPawel Jakub Dawidek 		    indent, g_eli_algo2str(sc->sc_aalgo));
1213eaa3b919SPawel Jakub Dawidek 	}
1214eaa3b919SPawel Jakub Dawidek 	sbuf_printf(sb, "%s<KeyLength>%u</KeyLength>\n", indent,
1215eaa3b919SPawel Jakub Dawidek 	    sc->sc_ekeylen);
1216038c55adSPawel Jakub Dawidek 	sbuf_printf(sb, "%s<EncryptionAlgorithm>%s</EncryptionAlgorithm>\n",
1217038c55adSPawel Jakub Dawidek 	    indent, g_eli_algo2str(sc->sc_ealgo));
1218d8d61ef8SPawel Jakub Dawidek 	sbuf_printf(sb, "%s<State>%s</State>\n", indent,
1219d8d61ef8SPawel Jakub Dawidek 	    (sc->sc_flags & G_ELI_FLAG_SUSPEND) ? "SUSPENDED" : "ACTIVE");
1220c58794deSPawel Jakub Dawidek }
1221c58794deSPawel Jakub Dawidek 
1222c5d387d0SPawel Jakub Dawidek static void
1223c5d387d0SPawel Jakub Dawidek g_eli_shutdown_pre_sync(void *arg, int howto)
1224c5d387d0SPawel Jakub Dawidek {
1225c5d387d0SPawel Jakub Dawidek 	struct g_class *mp;
1226c5d387d0SPawel Jakub Dawidek 	struct g_geom *gp, *gp2;
1227c5d387d0SPawel Jakub Dawidek 	struct g_provider *pp;
1228c5d387d0SPawel Jakub Dawidek 	struct g_eli_softc *sc;
1229c5d387d0SPawel Jakub Dawidek 	int error;
1230c5d387d0SPawel Jakub Dawidek 
1231c5d387d0SPawel Jakub Dawidek 	mp = arg;
1232c5d387d0SPawel Jakub Dawidek 	DROP_GIANT();
1233c5d387d0SPawel Jakub Dawidek 	g_topology_lock();
1234c5d387d0SPawel Jakub Dawidek 	LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) {
1235c5d387d0SPawel Jakub Dawidek 		sc = gp->softc;
1236c5d387d0SPawel Jakub Dawidek 		if (sc == NULL)
1237c5d387d0SPawel Jakub Dawidek 			continue;
1238c5d387d0SPawel Jakub Dawidek 		pp = LIST_FIRST(&gp->provider);
1239c5d387d0SPawel Jakub Dawidek 		KASSERT(pp != NULL, ("No provider? gp=%p (%s)", gp, gp->name));
1240c5d387d0SPawel Jakub Dawidek 		if (pp->acr + pp->acw + pp->ace == 0)
12415ad4a7c7SPawel Jakub Dawidek 			error = g_eli_destroy(sc, TRUE);
1242c5d387d0SPawel Jakub Dawidek 		else {
1243c5d387d0SPawel Jakub Dawidek 			sc->sc_flags |= G_ELI_FLAG_RW_DETACH;
1244c5d387d0SPawel Jakub Dawidek 			gp->access = g_eli_access;
1245c5d387d0SPawel Jakub Dawidek 		}
1246c5d387d0SPawel Jakub Dawidek 	}
1247c5d387d0SPawel Jakub Dawidek 	g_topology_unlock();
1248c5d387d0SPawel Jakub Dawidek 	PICKUP_GIANT();
1249c5d387d0SPawel Jakub Dawidek }
1250c5d387d0SPawel Jakub Dawidek 
1251c5d387d0SPawel Jakub Dawidek static void
1252c5d387d0SPawel Jakub Dawidek g_eli_init(struct g_class *mp)
1253c5d387d0SPawel Jakub Dawidek {
1254c5d387d0SPawel Jakub Dawidek 
1255c5d387d0SPawel Jakub Dawidek 	g_eli_pre_sync = EVENTHANDLER_REGISTER(shutdown_pre_sync,
1256c5d387d0SPawel Jakub Dawidek 	    g_eli_shutdown_pre_sync, mp, SHUTDOWN_PRI_FIRST);
1257c5d387d0SPawel Jakub Dawidek 	if (g_eli_pre_sync == NULL)
1258c5d387d0SPawel Jakub Dawidek 		G_ELI_DEBUG(0, "Warning! Cannot register shutdown event.");
1259c5d387d0SPawel Jakub Dawidek }
1260c5d387d0SPawel Jakub Dawidek 
1261c5d387d0SPawel Jakub Dawidek static void
1262c5d387d0SPawel Jakub Dawidek g_eli_fini(struct g_class *mp)
1263c5d387d0SPawel Jakub Dawidek {
1264c5d387d0SPawel Jakub Dawidek 
1265c5d387d0SPawel Jakub Dawidek 	if (g_eli_pre_sync != NULL)
1266c5d387d0SPawel Jakub Dawidek 		EVENTHANDLER_DEREGISTER(shutdown_pre_sync, g_eli_pre_sync);
1267c5d387d0SPawel Jakub Dawidek }
1268c5d387d0SPawel Jakub Dawidek 
1269c58794deSPawel Jakub Dawidek DECLARE_GEOM_CLASS(g_eli_class, g_eli);
1270f6829a05SYaroslav Tykhiy MODULE_DEPEND(g_eli, crypto, 1, 1, 1);
1271