xref: /freebsd/sys/geom/eli/g_eli.c (revision 038c55adcc1b4423ff556c18b1bc9f64e7728cb5)
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>
32c58794deSPawel Jakub Dawidek #include <sys/kernel.h>
339af2131bSPawel Jakub Dawidek #include <sys/linker.h>
34c58794deSPawel Jakub Dawidek #include <sys/module.h>
35c58794deSPawel Jakub Dawidek #include <sys/lock.h>
36c58794deSPawel Jakub Dawidek #include <sys/mutex.h>
37c58794deSPawel Jakub Dawidek #include <sys/bio.h>
385d807a0eSAndrey V. Elsukov #include <sys/sbuf.h>
39c58794deSPawel Jakub Dawidek #include <sys/sysctl.h>
40c58794deSPawel Jakub Dawidek #include <sys/malloc.h>
41c5d387d0SPawel Jakub Dawidek #include <sys/eventhandler.h>
42c58794deSPawel Jakub Dawidek #include <sys/kthread.h>
43c58794deSPawel Jakub Dawidek #include <sys/proc.h>
44c58794deSPawel Jakub Dawidek #include <sys/sched.h>
45dd549194SPawel Jakub Dawidek #include <sys/smp.h>
46c58794deSPawel Jakub Dawidek #include <sys/uio.h>
47a80f82a4SPawel Jakub Dawidek #include <sys/vnode.h>
48c58794deSPawel Jakub Dawidek 
49c58794deSPawel Jakub Dawidek #include <vm/uma.h>
50c58794deSPawel Jakub Dawidek 
51c58794deSPawel Jakub Dawidek #include <geom/geom.h>
52c58794deSPawel Jakub Dawidek #include <geom/eli/g_eli.h>
53c58794deSPawel Jakub Dawidek #include <geom/eli/pkcs5v2.h>
54c58794deSPawel Jakub Dawidek 
55cb08c2ccSAlexander Leidinger FEATURE(geom_eli, "GEOM crypto module");
56c58794deSPawel Jakub Dawidek 
57c58794deSPawel Jakub Dawidek MALLOC_DEFINE(M_ELI, "eli data", "GEOM_ELI Data");
58c58794deSPawel Jakub Dawidek 
59c58794deSPawel Jakub Dawidek SYSCTL_DECL(_kern_geom);
60c58794deSPawel Jakub Dawidek SYSCTL_NODE(_kern_geom, OID_AUTO, eli, CTLFLAG_RW, 0, "GEOM_ELI stuff");
61a1f4a8c4SPawel Jakub Dawidek static int g_eli_version = G_ELI_VERSION;
62a1f4a8c4SPawel Jakub Dawidek SYSCTL_INT(_kern_geom_eli, OID_AUTO, version, CTLFLAG_RD, &g_eli_version, 0,
63a1f4a8c4SPawel Jakub Dawidek     "GELI version");
64f95168e0SPawel Jakub Dawidek int g_eli_debug = 0;
65c58794deSPawel Jakub Dawidek TUNABLE_INT("kern.geom.eli.debug", &g_eli_debug);
66f95168e0SPawel Jakub Dawidek SYSCTL_INT(_kern_geom_eli, OID_AUTO, debug, CTLFLAG_RW, &g_eli_debug, 0,
67c58794deSPawel Jakub Dawidek     "Debug level");
68c58794deSPawel Jakub Dawidek static u_int g_eli_tries = 3;
69c58794deSPawel Jakub Dawidek TUNABLE_INT("kern.geom.eli.tries", &g_eli_tries);
70c58794deSPawel Jakub Dawidek SYSCTL_UINT(_kern_geom_eli, OID_AUTO, tries, CTLFLAG_RW, &g_eli_tries, 0,
7198645006SChristian Brueffer     "Number of tries for entering the passphrase");
72eb4c31fdSEd Schouten static u_int g_eli_visible_passphrase = GETS_NOECHO;
73c58794deSPawel Jakub Dawidek TUNABLE_INT("kern.geom.eli.visible_passphrase", &g_eli_visible_passphrase);
74c58794deSPawel Jakub Dawidek SYSCTL_UINT(_kern_geom_eli, OID_AUTO, visible_passphrase, CTLFLAG_RW,
75c58794deSPawel Jakub Dawidek     &g_eli_visible_passphrase, 0,
76eb4c31fdSEd Schouten     "Visibility of passphrase prompt (0 = invisible, 1 = visible, 2 = asterisk)");
77b35bfe7eSPawel Jakub Dawidek u_int g_eli_overwrites = G_ELI_OVERWRITES;
7805bf5e8aSPawel Jakub Dawidek TUNABLE_INT("kern.geom.eli.overwrites", &g_eli_overwrites);
79c58794deSPawel Jakub Dawidek SYSCTL_UINT(_kern_geom_eli, OID_AUTO, overwrites, CTLFLAG_RW, &g_eli_overwrites,
8098645006SChristian Brueffer     0, "Number of times on-disk keys should be overwritten when destroying them");
81dd549194SPawel Jakub Dawidek static u_int g_eli_threads = 0;
82c58794deSPawel Jakub Dawidek TUNABLE_INT("kern.geom.eli.threads", &g_eli_threads);
83c58794deSPawel Jakub Dawidek SYSCTL_UINT(_kern_geom_eli, OID_AUTO, threads, CTLFLAG_RW, &g_eli_threads, 0,
84c58794deSPawel Jakub Dawidek     "Number of threads doing crypto work");
85eaa3b919SPawel Jakub Dawidek u_int g_eli_batch = 0;
86eaa3b919SPawel Jakub Dawidek TUNABLE_INT("kern.geom.eli.batch", &g_eli_batch);
87eaa3b919SPawel Jakub Dawidek SYSCTL_UINT(_kern_geom_eli, OID_AUTO, batch, CTLFLAG_RW, &g_eli_batch, 0,
88eaa3b919SPawel Jakub Dawidek     "Use crypto operations batching");
89c58794deSPawel Jakub Dawidek 
90c5d387d0SPawel Jakub Dawidek static eventhandler_tag g_eli_pre_sync = NULL;
91c5d387d0SPawel Jakub Dawidek 
92c58794deSPawel Jakub Dawidek static int g_eli_destroy_geom(struct gctl_req *req, struct g_class *mp,
93c58794deSPawel Jakub Dawidek     struct g_geom *gp);
94c5d387d0SPawel Jakub Dawidek static void g_eli_init(struct g_class *mp);
95c5d387d0SPawel Jakub Dawidek static void g_eli_fini(struct g_class *mp);
96c58794deSPawel Jakub Dawidek 
97c58794deSPawel Jakub Dawidek static g_taste_t g_eli_taste;
98c58794deSPawel Jakub Dawidek static g_dumpconf_t g_eli_dumpconf;
99c58794deSPawel Jakub Dawidek 
100c58794deSPawel Jakub Dawidek struct g_class g_eli_class = {
101c58794deSPawel Jakub Dawidek 	.name = G_ELI_CLASS_NAME,
102c58794deSPawel Jakub Dawidek 	.version = G_VERSION,
103c58794deSPawel Jakub Dawidek 	.ctlreq = g_eli_config,
104c58794deSPawel Jakub Dawidek 	.taste = g_eli_taste,
105c5d387d0SPawel Jakub Dawidek 	.destroy_geom = g_eli_destroy_geom,
106c5d387d0SPawel Jakub Dawidek 	.init = g_eli_init,
107c5d387d0SPawel Jakub Dawidek 	.fini = g_eli_fini
108c58794deSPawel Jakub Dawidek };
109c58794deSPawel Jakub Dawidek 
110c58794deSPawel Jakub Dawidek 
111c58794deSPawel Jakub Dawidek /*
112c58794deSPawel Jakub Dawidek  * Code paths:
113c58794deSPawel Jakub Dawidek  * BIO_READ:
1145ad4a7c7SPawel 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
115c58794deSPawel Jakub Dawidek  * BIO_WRITE:
116c58794deSPawel Jakub Dawidek  *	g_eli_start -> g_eli_crypto_run -> g_eli_crypto_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver
117c58794deSPawel Jakub Dawidek  */
118c58794deSPawel Jakub Dawidek 
119c58794deSPawel Jakub Dawidek 
120c58794deSPawel Jakub Dawidek /*
121c58794deSPawel Jakub Dawidek  * EAGAIN from crypto(9) means, that we were probably balanced to another crypto
122c58794deSPawel Jakub Dawidek  * accelerator or something like this.
123c58794deSPawel Jakub Dawidek  * The function updates the SID and rerun the operation.
124c58794deSPawel Jakub Dawidek  */
125eaa3b919SPawel Jakub Dawidek int
126c58794deSPawel Jakub Dawidek g_eli_crypto_rerun(struct cryptop *crp)
127c58794deSPawel Jakub Dawidek {
128c58794deSPawel Jakub Dawidek 	struct g_eli_softc *sc;
129c58794deSPawel Jakub Dawidek 	struct g_eli_worker *wr;
130c58794deSPawel Jakub Dawidek 	struct bio *bp;
131c58794deSPawel Jakub Dawidek 	int error;
132c58794deSPawel Jakub Dawidek 
133c58794deSPawel Jakub Dawidek 	bp = (struct bio *)crp->crp_opaque;
134c58794deSPawel Jakub Dawidek 	sc = bp->bio_to->geom->softc;
135c58794deSPawel Jakub Dawidek 	LIST_FOREACH(wr, &sc->sc_workers, w_next) {
136c58794deSPawel Jakub Dawidek 		if (wr->w_number == bp->bio_pflags)
137c58794deSPawel Jakub Dawidek 			break;
138c58794deSPawel Jakub Dawidek 	}
139c58794deSPawel Jakub Dawidek 	KASSERT(wr != NULL, ("Invalid worker (%u).", bp->bio_pflags));
14098645006SChristian Brueffer 	G_ELI_DEBUG(1, "Rerunning crypto %s request (sid: %ju -> %ju).",
141c58794deSPawel Jakub Dawidek 	    bp->bio_cmd == BIO_READ ? "READ" : "WRITE", (uintmax_t)wr->w_sid,
142c58794deSPawel Jakub Dawidek 	    (uintmax_t)crp->crp_sid);
143c58794deSPawel Jakub Dawidek 	wr->w_sid = crp->crp_sid;
144c58794deSPawel Jakub Dawidek 	crp->crp_etype = 0;
145c58794deSPawel Jakub Dawidek 	error = crypto_dispatch(crp);
146c58794deSPawel Jakub Dawidek 	if (error == 0)
147c58794deSPawel Jakub Dawidek 		return (0);
148c58794deSPawel Jakub Dawidek 	G_ELI_DEBUG(1, "%s: crypto_dispatch() returned %d.", __func__, error);
149c58794deSPawel Jakub Dawidek 	crp->crp_etype = error;
150c58794deSPawel Jakub Dawidek 	return (error);
151c58794deSPawel Jakub Dawidek }
152c58794deSPawel Jakub Dawidek 
153c58794deSPawel Jakub Dawidek /*
154c58794deSPawel Jakub Dawidek  * The function is called afer reading encrypted data from the provider.
155bb30fea6SPawel Jakub Dawidek  *
1565ad4a7c7SPawel 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
157c58794deSPawel Jakub Dawidek  */
158eaa3b919SPawel Jakub Dawidek void
159c58794deSPawel Jakub Dawidek g_eli_read_done(struct bio *bp)
160c58794deSPawel Jakub Dawidek {
161c58794deSPawel Jakub Dawidek 	struct g_eli_softc *sc;
162c58794deSPawel Jakub Dawidek 	struct bio *pbp;
163c58794deSPawel Jakub Dawidek 
164c58794deSPawel Jakub Dawidek 	G_ELI_LOGREQ(2, bp, "Request done.");
165c58794deSPawel Jakub Dawidek 	pbp = bp->bio_parent;
166c58794deSPawel Jakub Dawidek 	if (pbp->bio_error == 0)
167c58794deSPawel Jakub Dawidek 		pbp->bio_error = bp->bio_error;
16890574b0aSMikolaj Golub 	g_destroy_bio(bp);
169eaa3b919SPawel Jakub Dawidek 	/*
170eaa3b919SPawel Jakub Dawidek 	 * Do we have all sectors already?
171eaa3b919SPawel Jakub Dawidek 	 */
172eaa3b919SPawel Jakub Dawidek 	pbp->bio_inbed++;
173eaa3b919SPawel Jakub Dawidek 	if (pbp->bio_inbed < pbp->bio_children)
174eaa3b919SPawel Jakub Dawidek 		return;
1755ad4a7c7SPawel Jakub Dawidek 	sc = pbp->bio_to->geom->softc;
176c58794deSPawel Jakub Dawidek 	if (pbp->bio_error != 0) {
177c58794deSPawel Jakub Dawidek 		G_ELI_LOGREQ(0, pbp, "%s() failed", __func__);
178c58794deSPawel Jakub Dawidek 		pbp->bio_completed = 0;
179eaa3b919SPawel Jakub Dawidek 		if (pbp->bio_driver2 != NULL) {
180eaa3b919SPawel Jakub Dawidek 			free(pbp->bio_driver2, M_ELI);
181eaa3b919SPawel Jakub Dawidek 			pbp->bio_driver2 = NULL;
182eaa3b919SPawel Jakub Dawidek 		}
183c58794deSPawel Jakub Dawidek 		g_io_deliver(pbp, pbp->bio_error);
1845ad4a7c7SPawel Jakub Dawidek 		atomic_subtract_int(&sc->sc_inflight, 1);
185c58794deSPawel Jakub Dawidek 		return;
186c58794deSPawel Jakub Dawidek 	}
187c58794deSPawel Jakub Dawidek 	mtx_lock(&sc->sc_queue_mtx);
188c58794deSPawel Jakub Dawidek 	bioq_insert_tail(&sc->sc_queue, pbp);
189c58794deSPawel Jakub Dawidek 	mtx_unlock(&sc->sc_queue_mtx);
190c58794deSPawel Jakub Dawidek 	wakeup(sc);
191c58794deSPawel Jakub Dawidek }
192c58794deSPawel Jakub Dawidek 
193c58794deSPawel Jakub Dawidek /*
194c58794deSPawel Jakub Dawidek  * The function is called after we encrypt and write data.
195bb30fea6SPawel Jakub Dawidek  *
196bb30fea6SPawel Jakub Dawidek  * g_eli_start -> g_eli_crypto_run -> g_eli_crypto_write_done -> g_io_request -> G_ELI_WRITE_DONE -> g_io_deliver
197c58794deSPawel Jakub Dawidek  */
198eaa3b919SPawel Jakub Dawidek void
199c58794deSPawel Jakub Dawidek g_eli_write_done(struct bio *bp)
200c58794deSPawel Jakub Dawidek {
2015ad4a7c7SPawel Jakub Dawidek 	struct g_eli_softc *sc;
202c58794deSPawel Jakub Dawidek 	struct bio *pbp;
203c58794deSPawel Jakub Dawidek 
204c58794deSPawel Jakub Dawidek 	G_ELI_LOGREQ(2, bp, "Request done.");
205c58794deSPawel Jakub Dawidek 	pbp = bp->bio_parent;
206eaa3b919SPawel Jakub Dawidek 	if (pbp->bio_error == 0) {
207eaa3b919SPawel Jakub Dawidek 		if (bp->bio_error != 0)
208c58794deSPawel Jakub Dawidek 			pbp->bio_error = bp->bio_error;
209eaa3b919SPawel Jakub Dawidek 	}
21090574b0aSMikolaj Golub 	g_destroy_bio(bp);
211eaa3b919SPawel Jakub Dawidek 	/*
212eaa3b919SPawel Jakub Dawidek 	 * Do we have all sectors already?
213eaa3b919SPawel Jakub Dawidek 	 */
214eaa3b919SPawel Jakub Dawidek 	pbp->bio_inbed++;
215eaa3b919SPawel Jakub Dawidek 	if (pbp->bio_inbed < pbp->bio_children)
216eaa3b919SPawel Jakub Dawidek 		return;
217c58794deSPawel Jakub Dawidek 	free(pbp->bio_driver2, M_ELI);
218c58794deSPawel Jakub Dawidek 	pbp->bio_driver2 = NULL;
219eaa3b919SPawel Jakub Dawidek 	if (pbp->bio_error != 0) {
22071270ca6SPawel Jakub Dawidek 		G_ELI_LOGREQ(0, pbp, "Crypto WRITE request failed (error=%d).",
221c58794deSPawel Jakub Dawidek 		    pbp->bio_error);
222c58794deSPawel Jakub Dawidek 		pbp->bio_completed = 0;
223c58794deSPawel Jakub Dawidek 	}
224c58794deSPawel Jakub Dawidek 	/*
225c58794deSPawel Jakub Dawidek 	 * Write is finished, send it up.
226c58794deSPawel Jakub Dawidek 	 */
227eaa3b919SPawel Jakub Dawidek 	pbp->bio_completed = pbp->bio_length;
2285ad4a7c7SPawel Jakub Dawidek 	sc = pbp->bio_to->geom->softc;
229c58794deSPawel Jakub Dawidek 	g_io_deliver(pbp, pbp->bio_error);
2305ad4a7c7SPawel Jakub Dawidek 	atomic_subtract_int(&sc->sc_inflight, 1);
231c58794deSPawel Jakub Dawidek }
232c58794deSPawel Jakub Dawidek 
233c58794deSPawel Jakub Dawidek /*
234c58794deSPawel Jakub Dawidek  * This function should never be called, but GEOM made as it set ->orphan()
235c58794deSPawel Jakub Dawidek  * method for every geom.
236c58794deSPawel Jakub Dawidek  */
237c58794deSPawel Jakub Dawidek static void
238c58794deSPawel Jakub Dawidek g_eli_orphan_spoil_assert(struct g_consumer *cp)
239c58794deSPawel Jakub Dawidek {
240c58794deSPawel Jakub Dawidek 
241c58794deSPawel Jakub Dawidek 	panic("Function %s() called for %s.", __func__, cp->geom->name);
242c58794deSPawel Jakub Dawidek }
243c58794deSPawel Jakub Dawidek 
244c58794deSPawel Jakub Dawidek static void
245c58794deSPawel Jakub Dawidek g_eli_orphan(struct g_consumer *cp)
246c58794deSPawel Jakub Dawidek {
247c58794deSPawel Jakub Dawidek 	struct g_eli_softc *sc;
248c58794deSPawel Jakub Dawidek 
249c58794deSPawel Jakub Dawidek 	g_topology_assert();
250c58794deSPawel Jakub Dawidek 	sc = cp->geom->softc;
251c58794deSPawel Jakub Dawidek 	if (sc == NULL)
252c58794deSPawel Jakub Dawidek 		return;
2535ad4a7c7SPawel Jakub Dawidek 	g_eli_destroy(sc, TRUE);
254c58794deSPawel Jakub Dawidek }
255c58794deSPawel Jakub Dawidek 
256bb30fea6SPawel Jakub Dawidek /*
257056638c4SPawel Jakub Dawidek  * BIO_READ:
2585ad4a7c7SPawel 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
259056638c4SPawel Jakub Dawidek  * BIO_WRITE:
260056638c4SPawel Jakub Dawidek  *	G_ELI_START -> g_eli_crypto_run -> g_eli_crypto_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver
261bb30fea6SPawel Jakub Dawidek  */
262c58794deSPawel Jakub Dawidek static void
263c58794deSPawel Jakub Dawidek g_eli_start(struct bio *bp)
264c58794deSPawel Jakub Dawidek {
265c58794deSPawel Jakub Dawidek 	struct g_eli_softc *sc;
266d3a1be90SPawel Jakub Dawidek 	struct g_consumer *cp;
267c58794deSPawel Jakub Dawidek 	struct bio *cbp;
268c58794deSPawel Jakub Dawidek 
269c58794deSPawel Jakub Dawidek 	sc = bp->bio_to->geom->softc;
270c58794deSPawel Jakub Dawidek 	KASSERT(sc != NULL,
271c58794deSPawel Jakub Dawidek 	    ("Provider's error should be set (error=%d)(device=%s).",
272c58794deSPawel Jakub Dawidek 	    bp->bio_to->error, bp->bio_to->name));
273c58794deSPawel Jakub Dawidek 	G_ELI_LOGREQ(2, bp, "Request received.");
274c58794deSPawel Jakub Dawidek 
275c58794deSPawel Jakub Dawidek 	switch (bp->bio_cmd) {
276c58794deSPawel Jakub Dawidek 	case BIO_READ:
277c58794deSPawel Jakub Dawidek 	case BIO_WRITE:
278d3a1be90SPawel Jakub Dawidek 	case BIO_GETATTR:
27942461fbaSPawel Jakub Dawidek 	case BIO_FLUSH:
280c58794deSPawel Jakub Dawidek 		break;
281c58794deSPawel Jakub Dawidek 	case BIO_DELETE:
282c58794deSPawel Jakub Dawidek 		/*
283c58794deSPawel Jakub Dawidek 		 * We could eventually support BIO_DELETE request.
284c58794deSPawel Jakub Dawidek 		 * It could be done by overwritting requested sector with
285c58794deSPawel Jakub Dawidek 		 * random data g_eli_overwrites number of times.
286c58794deSPawel Jakub Dawidek 		 */
287c58794deSPawel Jakub Dawidek 	default:
288c58794deSPawel Jakub Dawidek 		g_io_deliver(bp, EOPNOTSUPP);
289c58794deSPawel Jakub Dawidek 		return;
290c58794deSPawel Jakub Dawidek 	}
291c58794deSPawel Jakub Dawidek 	cbp = g_clone_bio(bp);
292c58794deSPawel Jakub Dawidek 	if (cbp == NULL) {
293c58794deSPawel Jakub Dawidek 		g_io_deliver(bp, ENOMEM);
294c58794deSPawel Jakub Dawidek 		return;
295c58794deSPawel Jakub Dawidek 	}
2965ad4a7c7SPawel Jakub Dawidek 	bp->bio_driver1 = cbp;
2975ad4a7c7SPawel Jakub Dawidek 	bp->bio_pflags = G_ELI_NEW_BIO;
298d3a1be90SPawel Jakub Dawidek 	switch (bp->bio_cmd) {
299d3a1be90SPawel Jakub Dawidek 	case BIO_READ:
300eaa3b919SPawel Jakub Dawidek 		if (!(sc->sc_flags & G_ELI_FLAG_AUTH)) {
3015ad4a7c7SPawel Jakub Dawidek 			g_eli_crypto_read(sc, bp, 0);
302d3a1be90SPawel Jakub Dawidek 			break;
303eaa3b919SPawel Jakub Dawidek 		}
304eaa3b919SPawel Jakub Dawidek 		/* FALLTHROUGH */
305d3a1be90SPawel Jakub Dawidek 	case BIO_WRITE:
306c58794deSPawel Jakub Dawidek 		mtx_lock(&sc->sc_queue_mtx);
307c58794deSPawel Jakub Dawidek 		bioq_insert_tail(&sc->sc_queue, bp);
308c58794deSPawel Jakub Dawidek 		mtx_unlock(&sc->sc_queue_mtx);
309c58794deSPawel Jakub Dawidek 		wakeup(sc);
310d3a1be90SPawel Jakub Dawidek 		break;
311d3a1be90SPawel Jakub Dawidek 	case BIO_GETATTR:
31242461fbaSPawel Jakub Dawidek 	case BIO_FLUSH:
313d3a1be90SPawel Jakub Dawidek 		cbp->bio_done = g_std_done;
314d3a1be90SPawel Jakub Dawidek 		cp = LIST_FIRST(&sc->sc_geom->consumer);
315d3a1be90SPawel Jakub Dawidek 		cbp->bio_to = cp->provider;
316cd0d707eSPawel Jakub Dawidek 		G_ELI_LOGREQ(2, cbp, "Sending request.");
317d3a1be90SPawel Jakub Dawidek 		g_io_request(cbp, cp);
318d3a1be90SPawel Jakub Dawidek 		break;
319c58794deSPawel Jakub Dawidek 	}
320c58794deSPawel Jakub Dawidek }
321c58794deSPawel Jakub Dawidek 
3223ac01bc2SPawel Jakub Dawidek static int
3233ac01bc2SPawel Jakub Dawidek g_eli_newsession(struct g_eli_worker *wr)
3243ac01bc2SPawel Jakub Dawidek {
3253ac01bc2SPawel Jakub Dawidek 	struct g_eli_softc *sc;
3263ac01bc2SPawel Jakub Dawidek 	struct cryptoini crie, cria;
3273ac01bc2SPawel Jakub Dawidek 	int error;
3283ac01bc2SPawel Jakub Dawidek 
3293ac01bc2SPawel Jakub Dawidek 	sc = wr->w_softc;
3303ac01bc2SPawel Jakub Dawidek 
3313ac01bc2SPawel Jakub Dawidek 	bzero(&crie, sizeof(crie));
3323ac01bc2SPawel Jakub Dawidek 	crie.cri_alg = sc->sc_ealgo;
3333ac01bc2SPawel Jakub Dawidek 	crie.cri_klen = sc->sc_ekeylen;
3343ac01bc2SPawel Jakub Dawidek 	if (sc->sc_ealgo == CRYPTO_AES_XTS)
3353ac01bc2SPawel Jakub Dawidek 		crie.cri_klen <<= 1;
336ad0a5236SPawel Jakub Dawidek 	if ((sc->sc_flags & G_ELI_FLAG_FIRST_KEY) != 0) {
337ad0a5236SPawel Jakub Dawidek 		crie.cri_key = g_eli_key_hold(sc, 0,
338ad0a5236SPawel Jakub Dawidek 		    LIST_FIRST(&sc->sc_geom->consumer)->provider->sectorsize);
339ad0a5236SPawel Jakub Dawidek 	} else {
3401e09ff3dSPawel Jakub Dawidek 		crie.cri_key = sc->sc_ekey;
341ad0a5236SPawel Jakub Dawidek 	}
3423ac01bc2SPawel Jakub Dawidek 	if (sc->sc_flags & G_ELI_FLAG_AUTH) {
3433ac01bc2SPawel Jakub Dawidek 		bzero(&cria, sizeof(cria));
3443ac01bc2SPawel Jakub Dawidek 		cria.cri_alg = sc->sc_aalgo;
3453ac01bc2SPawel Jakub Dawidek 		cria.cri_klen = sc->sc_akeylen;
3463ac01bc2SPawel Jakub Dawidek 		cria.cri_key = sc->sc_akey;
3473ac01bc2SPawel Jakub Dawidek 		crie.cri_next = &cria;
3483ac01bc2SPawel Jakub Dawidek 	}
3493ac01bc2SPawel Jakub Dawidek 
3503ac01bc2SPawel Jakub Dawidek 	switch (sc->sc_crypto) {
3513ac01bc2SPawel Jakub Dawidek 	case G_ELI_CRYPTO_SW:
3523ac01bc2SPawel Jakub Dawidek 		error = crypto_newsession(&wr->w_sid, &crie,
3533ac01bc2SPawel Jakub Dawidek 		    CRYPTOCAP_F_SOFTWARE);
3543ac01bc2SPawel Jakub Dawidek 		break;
3553ac01bc2SPawel Jakub Dawidek 	case G_ELI_CRYPTO_HW:
3563ac01bc2SPawel Jakub Dawidek 		error = crypto_newsession(&wr->w_sid, &crie,
3573ac01bc2SPawel Jakub Dawidek 		    CRYPTOCAP_F_HARDWARE);
3583ac01bc2SPawel Jakub Dawidek 		break;
3593ac01bc2SPawel Jakub Dawidek 	case G_ELI_CRYPTO_UNKNOWN:
3603ac01bc2SPawel Jakub Dawidek 		error = crypto_newsession(&wr->w_sid, &crie,
3613ac01bc2SPawel Jakub Dawidek 		    CRYPTOCAP_F_HARDWARE);
3623ac01bc2SPawel Jakub Dawidek 		if (error == 0) {
3633ac01bc2SPawel Jakub Dawidek 			mtx_lock(&sc->sc_queue_mtx);
3643ac01bc2SPawel Jakub Dawidek 			if (sc->sc_crypto == G_ELI_CRYPTO_UNKNOWN)
3653ac01bc2SPawel Jakub Dawidek 				sc->sc_crypto = G_ELI_CRYPTO_HW;
3663ac01bc2SPawel Jakub Dawidek 			mtx_unlock(&sc->sc_queue_mtx);
3673ac01bc2SPawel Jakub Dawidek 		} else {
3683ac01bc2SPawel Jakub Dawidek 			error = crypto_newsession(&wr->w_sid, &crie,
3693ac01bc2SPawel Jakub Dawidek 			    CRYPTOCAP_F_SOFTWARE);
3703ac01bc2SPawel Jakub Dawidek 			mtx_lock(&sc->sc_queue_mtx);
3713ac01bc2SPawel Jakub Dawidek 			if (sc->sc_crypto == G_ELI_CRYPTO_UNKNOWN)
3723ac01bc2SPawel Jakub Dawidek 				sc->sc_crypto = G_ELI_CRYPTO_SW;
3733ac01bc2SPawel Jakub Dawidek 			mtx_unlock(&sc->sc_queue_mtx);
3743ac01bc2SPawel Jakub Dawidek 		}
3753ac01bc2SPawel Jakub Dawidek 		break;
3763ac01bc2SPawel Jakub Dawidek 	default:
3773ac01bc2SPawel Jakub Dawidek 		panic("%s: invalid condition", __func__);
3783ac01bc2SPawel Jakub Dawidek 	}
3793ac01bc2SPawel Jakub Dawidek 
380ad0a5236SPawel Jakub Dawidek 	if ((sc->sc_flags & G_ELI_FLAG_FIRST_KEY) != 0)
381ad0a5236SPawel Jakub Dawidek 		g_eli_key_drop(sc, crie.cri_key);
382ad0a5236SPawel Jakub Dawidek 
3833ac01bc2SPawel Jakub Dawidek 	return (error);
3843ac01bc2SPawel Jakub Dawidek }
3853ac01bc2SPawel Jakub Dawidek 
3863ac01bc2SPawel Jakub Dawidek static void
3873ac01bc2SPawel Jakub Dawidek g_eli_freesession(struct g_eli_worker *wr)
3883ac01bc2SPawel Jakub Dawidek {
3893ac01bc2SPawel Jakub Dawidek 
3903ac01bc2SPawel Jakub Dawidek 	crypto_freesession(wr->w_sid);
3913ac01bc2SPawel Jakub Dawidek }
3923ac01bc2SPawel Jakub Dawidek 
3935ad4a7c7SPawel Jakub Dawidek static void
3945ad4a7c7SPawel Jakub Dawidek g_eli_cancel(struct g_eli_softc *sc)
3955ad4a7c7SPawel Jakub Dawidek {
3965ad4a7c7SPawel Jakub Dawidek 	struct bio *bp;
3975ad4a7c7SPawel Jakub Dawidek 
3985ad4a7c7SPawel Jakub Dawidek 	mtx_assert(&sc->sc_queue_mtx, MA_OWNED);
3995ad4a7c7SPawel Jakub Dawidek 
4005ad4a7c7SPawel Jakub Dawidek 	while ((bp = bioq_takefirst(&sc->sc_queue)) != NULL) {
4015ad4a7c7SPawel Jakub Dawidek 		KASSERT(bp->bio_pflags == G_ELI_NEW_BIO,
4025ad4a7c7SPawel Jakub Dawidek 		    ("Not new bio when canceling (bp=%p).", bp));
4035ad4a7c7SPawel Jakub Dawidek 		g_io_deliver(bp, ENXIO);
4045ad4a7c7SPawel Jakub Dawidek 	}
4055ad4a7c7SPawel Jakub Dawidek }
4065ad4a7c7SPawel Jakub Dawidek 
4075ad4a7c7SPawel Jakub Dawidek static struct bio *
4085ad4a7c7SPawel Jakub Dawidek g_eli_takefirst(struct g_eli_softc *sc)
4095ad4a7c7SPawel Jakub Dawidek {
4105ad4a7c7SPawel Jakub Dawidek 	struct bio *bp;
4115ad4a7c7SPawel Jakub Dawidek 
4125ad4a7c7SPawel Jakub Dawidek 	mtx_assert(&sc->sc_queue_mtx, MA_OWNED);
4135ad4a7c7SPawel Jakub Dawidek 
4145ad4a7c7SPawel Jakub Dawidek 	if (!(sc->sc_flags & G_ELI_FLAG_SUSPEND))
4155ad4a7c7SPawel Jakub Dawidek 		return (bioq_takefirst(&sc->sc_queue));
4165ad4a7c7SPawel Jakub Dawidek 	/*
4175ad4a7c7SPawel Jakub Dawidek 	 * Device suspended, so we skip new I/O requests.
4185ad4a7c7SPawel Jakub Dawidek 	 */
4195ad4a7c7SPawel Jakub Dawidek 	TAILQ_FOREACH(bp, &sc->sc_queue.queue, bio_queue) {
4205ad4a7c7SPawel Jakub Dawidek 		if (bp->bio_pflags != G_ELI_NEW_BIO)
4215ad4a7c7SPawel Jakub Dawidek 			break;
4225ad4a7c7SPawel Jakub Dawidek 	}
4235ad4a7c7SPawel Jakub Dawidek 	if (bp != NULL)
4245ad4a7c7SPawel Jakub Dawidek 		bioq_remove(&sc->sc_queue, bp);
4255ad4a7c7SPawel Jakub Dawidek 	return (bp);
4265ad4a7c7SPawel Jakub Dawidek }
4275ad4a7c7SPawel Jakub Dawidek 
428c58794deSPawel Jakub Dawidek /*
429c58794deSPawel Jakub Dawidek  * This is the main function for kernel worker thread when we don't have
430c58794deSPawel Jakub Dawidek  * hardware acceleration and we have to do cryptography in software.
431c58794deSPawel Jakub Dawidek  * Dedicated thread is needed, so we don't slow down g_up/g_down GEOM
432c58794deSPawel Jakub Dawidek  * threads with crypto work.
433c58794deSPawel Jakub Dawidek  */
434c58794deSPawel Jakub Dawidek static void
435c58794deSPawel Jakub Dawidek g_eli_worker(void *arg)
436c58794deSPawel Jakub Dawidek {
437c58794deSPawel Jakub Dawidek 	struct g_eli_softc *sc;
438c58794deSPawel Jakub Dawidek 	struct g_eli_worker *wr;
439c58794deSPawel Jakub Dawidek 	struct bio *bp;
4403ac01bc2SPawel Jakub Dawidek 	int error;
441c58794deSPawel Jakub Dawidek 
442c58794deSPawel Jakub Dawidek 	wr = arg;
443c58794deSPawel Jakub Dawidek 	sc = wr->w_softc;
444a1ea1a22SPawel Jakub Dawidek #ifdef SMP
445a1ea1a22SPawel Jakub Dawidek 	/* Before sched_bind() to a CPU, wait for all CPUs to go on-line. */
446b9420939SPawel Jakub Dawidek 	if (mp_ncpus > 1 && sc->sc_crypto == G_ELI_CRYPTO_SW &&
447b9420939SPawel Jakub Dawidek 	    g_eli_threads == 0) {
448a1ea1a22SPawel Jakub Dawidek 		while (!smp_started)
449a1ea1a22SPawel Jakub Dawidek 			tsleep(wr, 0, "geli:smp", hz / 4);
450a1ea1a22SPawel Jakub Dawidek 	}
451a1ea1a22SPawel Jakub Dawidek #endif
452982d11f8SJeff Roberson 	thread_lock(curthread);
45331c4cef7SPawel Jakub Dawidek 	sched_prio(curthread, PUSER);
454dd549194SPawel Jakub Dawidek 	if (sc->sc_crypto == G_ELI_CRYPTO_SW && g_eli_threads == 0)
455dd549194SPawel Jakub Dawidek 		sched_bind(curthread, wr->w_number);
456982d11f8SJeff Roberson 	thread_unlock(curthread);
457c58794deSPawel Jakub Dawidek 
458c58794deSPawel Jakub Dawidek 	G_ELI_DEBUG(1, "Thread %s started.", curthread->td_proc->p_comm);
459c58794deSPawel Jakub Dawidek 
460c58794deSPawel Jakub Dawidek 	for (;;) {
461c58794deSPawel Jakub Dawidek 		mtx_lock(&sc->sc_queue_mtx);
4625ad4a7c7SPawel Jakub Dawidek again:
4635ad4a7c7SPawel Jakub Dawidek 		bp = g_eli_takefirst(sc);
464c58794deSPawel Jakub Dawidek 		if (bp == NULL) {
465eaa3b919SPawel Jakub Dawidek 			if (sc->sc_flags & G_ELI_FLAG_DESTROY) {
4665ad4a7c7SPawel Jakub Dawidek 				g_eli_cancel(sc);
467c58794deSPawel Jakub Dawidek 				LIST_REMOVE(wr, w_next);
4683ac01bc2SPawel Jakub Dawidek 				g_eli_freesession(wr);
469c58794deSPawel Jakub Dawidek 				free(wr, M_ELI);
470c58794deSPawel Jakub Dawidek 				G_ELI_DEBUG(1, "Thread %s exiting.",
471c58794deSPawel Jakub Dawidek 				    curthread->td_proc->p_comm);
472c58794deSPawel Jakub Dawidek 				wakeup(&sc->sc_workers);
473c58794deSPawel Jakub Dawidek 				mtx_unlock(&sc->sc_queue_mtx);
4743745c395SJulian Elischer 				kproc_exit(0);
475c58794deSPawel Jakub Dawidek 			}
4765ad4a7c7SPawel Jakub Dawidek 			while (sc->sc_flags & G_ELI_FLAG_SUSPEND) {
4775ad4a7c7SPawel Jakub Dawidek 				if (sc->sc_inflight > 0) {
478*038c55adSPawel Jakub Dawidek 					G_ELI_DEBUG(0, "inflight=%d",
479*038c55adSPawel Jakub Dawidek 					    sc->sc_inflight);
4805ad4a7c7SPawel Jakub Dawidek 					/*
4815ad4a7c7SPawel Jakub Dawidek 					 * We still have inflight BIOs, so
4825ad4a7c7SPawel Jakub Dawidek 					 * sleep and retry.
4835ad4a7c7SPawel Jakub Dawidek 					 */
4845ad4a7c7SPawel Jakub Dawidek 					msleep(sc, &sc->sc_queue_mtx, PRIBIO,
4855ad4a7c7SPawel Jakub Dawidek 					    "geli:inf", hz / 5);
4865ad4a7c7SPawel Jakub Dawidek 					goto again;
4875ad4a7c7SPawel Jakub Dawidek 				}
4885ad4a7c7SPawel Jakub Dawidek 				/*
4895ad4a7c7SPawel Jakub Dawidek 				 * Suspend requested, mark the worker as
4905ad4a7c7SPawel Jakub Dawidek 				 * suspended and go to sleep.
4915ad4a7c7SPawel Jakub Dawidek 				 */
4923ac01bc2SPawel Jakub Dawidek 				if (wr->w_active) {
4933ac01bc2SPawel Jakub Dawidek 					g_eli_freesession(wr);
4943ac01bc2SPawel Jakub Dawidek 					wr->w_active = FALSE;
4953ac01bc2SPawel Jakub Dawidek 				}
4965ad4a7c7SPawel Jakub Dawidek 				wakeup(&sc->sc_workers);
4975ad4a7c7SPawel Jakub Dawidek 				msleep(sc, &sc->sc_queue_mtx, PRIBIO,
4985ad4a7c7SPawel Jakub Dawidek 				    "geli:suspend", 0);
4993ac01bc2SPawel Jakub Dawidek 				if (!wr->w_active &&
5003ac01bc2SPawel Jakub Dawidek 				    !(sc->sc_flags & G_ELI_FLAG_SUSPEND)) {
5013ac01bc2SPawel Jakub Dawidek 					error = g_eli_newsession(wr);
5023ac01bc2SPawel Jakub Dawidek 					KASSERT(error == 0,
5033ac01bc2SPawel Jakub Dawidek 					    ("g_eli_newsession() failed on resume (error=%d)",
5043ac01bc2SPawel Jakub Dawidek 					    error));
5053ac01bc2SPawel Jakub Dawidek 					wr->w_active = TRUE;
5063ac01bc2SPawel Jakub Dawidek 				}
5075ad4a7c7SPawel Jakub Dawidek 				goto again;
5085ad4a7c7SPawel Jakub Dawidek 			}
50931c4cef7SPawel Jakub Dawidek 			msleep(sc, &sc->sc_queue_mtx, PDROP, "geli:w", 0);
510c58794deSPawel Jakub Dawidek 			continue;
511c58794deSPawel Jakub Dawidek 		}
5125ad4a7c7SPawel Jakub Dawidek 		if (bp->bio_pflags == G_ELI_NEW_BIO)
5135ad4a7c7SPawel Jakub Dawidek 			atomic_add_int(&sc->sc_inflight, 1);
514c58794deSPawel Jakub Dawidek 		mtx_unlock(&sc->sc_queue_mtx);
5155ad4a7c7SPawel Jakub Dawidek 		if (bp->bio_pflags == G_ELI_NEW_BIO) {
5165ad4a7c7SPawel Jakub Dawidek 			bp->bio_pflags = 0;
5175ad4a7c7SPawel Jakub Dawidek 			if (sc->sc_flags & G_ELI_FLAG_AUTH) {
5185ad4a7c7SPawel Jakub Dawidek 				if (bp->bio_cmd == BIO_READ)
519eaa3b919SPawel Jakub Dawidek 					g_eli_auth_read(sc, bp);
5205ad4a7c7SPawel Jakub Dawidek 				else
5215ad4a7c7SPawel Jakub Dawidek 					g_eli_auth_run(wr, bp);
5225ad4a7c7SPawel Jakub Dawidek 			} else {
5235ad4a7c7SPawel Jakub Dawidek 				if (bp->bio_cmd == BIO_READ)
5245ad4a7c7SPawel Jakub Dawidek 					g_eli_crypto_read(sc, bp, 1);
5255ad4a7c7SPawel Jakub Dawidek 				else
5265ad4a7c7SPawel Jakub Dawidek 					g_eli_crypto_run(wr, bp);
5275ad4a7c7SPawel Jakub Dawidek 			}
5285ad4a7c7SPawel Jakub Dawidek 		} else {
5295ad4a7c7SPawel Jakub Dawidek 			if (sc->sc_flags & G_ELI_FLAG_AUTH)
530eaa3b919SPawel Jakub Dawidek 				g_eli_auth_run(wr, bp);
531eaa3b919SPawel Jakub Dawidek 			else
532dddd1d53SPawel Jakub Dawidek 				g_eli_crypto_run(wr, bp);
533c58794deSPawel Jakub Dawidek 		}
534c58794deSPawel Jakub Dawidek 	}
5355ad4a7c7SPawel Jakub Dawidek }
536c58794deSPawel Jakub Dawidek 
537c58794deSPawel Jakub Dawidek /*
538c58794deSPawel Jakub Dawidek  * Here we generate IV. It is unique for every sector.
539c58794deSPawel Jakub Dawidek  */
540eaa3b919SPawel Jakub Dawidek void
541c58794deSPawel Jakub Dawidek g_eli_crypto_ivgen(struct g_eli_softc *sc, off_t offset, u_char *iv,
542c58794deSPawel Jakub Dawidek     size_t size)
543c58794deSPawel Jakub Dawidek {
5449a5a1d1eSPawel Jakub Dawidek 	uint8_t off[8];
545c58794deSPawel Jakub Dawidek 
546efb46508SPawel Jakub Dawidek 	if ((sc->sc_flags & G_ELI_FLAG_NATIVE_BYTE_ORDER) != 0)
547efb46508SPawel Jakub Dawidek 		bcopy(&offset, off, sizeof(off));
548efb46508SPawel Jakub Dawidek 	else
5492bd4ade6SPawel Jakub Dawidek 		le64enc(off, (uint64_t)offset);
5509a5a1d1eSPawel Jakub Dawidek 
5519a5a1d1eSPawel Jakub Dawidek 	switch (sc->sc_ealgo) {
5529a5a1d1eSPawel Jakub Dawidek 	case CRYPTO_AES_XTS:
5539a5a1d1eSPawel Jakub Dawidek 		bcopy(off, iv, sizeof(off));
5549a5a1d1eSPawel Jakub Dawidek 		bzero(iv + sizeof(off), size - sizeof(off));
5559a5a1d1eSPawel Jakub Dawidek 		break;
5569a5a1d1eSPawel Jakub Dawidek 	default:
5579a5a1d1eSPawel Jakub Dawidek 	    {
5589a5a1d1eSPawel Jakub Dawidek 		u_char hash[SHA256_DIGEST_LENGTH];
5599a5a1d1eSPawel Jakub Dawidek 		SHA256_CTX ctx;
5609a5a1d1eSPawel Jakub Dawidek 
561c58794deSPawel Jakub Dawidek 		/* Copy precalculated SHA256 context for IV-Key. */
562c58794deSPawel Jakub Dawidek 		bcopy(&sc->sc_ivctx, &ctx, sizeof(ctx));
563efb46508SPawel Jakub Dawidek 		SHA256_Update(&ctx, off, sizeof(off));
564c58794deSPawel Jakub Dawidek 		SHA256_Final(hash, &ctx);
5659a5a1d1eSPawel Jakub Dawidek 		bcopy(hash, iv, MIN(sizeof(hash), size));
5669a5a1d1eSPawel Jakub Dawidek 		break;
5679a5a1d1eSPawel Jakub Dawidek 	    }
5689a5a1d1eSPawel Jakub Dawidek 	}
569c58794deSPawel Jakub Dawidek }
570c58794deSPawel Jakub Dawidek 
571c58794deSPawel Jakub Dawidek int
572c58794deSPawel Jakub Dawidek g_eli_read_metadata(struct g_class *mp, struct g_provider *pp,
573c58794deSPawel Jakub Dawidek     struct g_eli_metadata *md)
574c58794deSPawel Jakub Dawidek {
575c58794deSPawel Jakub Dawidek 	struct g_geom *gp;
576c58794deSPawel Jakub Dawidek 	struct g_consumer *cp;
577c58794deSPawel Jakub Dawidek 	u_char *buf = NULL;
578c58794deSPawel Jakub Dawidek 	int error;
579c58794deSPawel Jakub Dawidek 
580c58794deSPawel Jakub Dawidek 	g_topology_assert();
581c58794deSPawel Jakub Dawidek 
582c58794deSPawel Jakub Dawidek 	gp = g_new_geomf(mp, "eli:taste");
583c58794deSPawel Jakub Dawidek 	gp->start = g_eli_start;
584c58794deSPawel Jakub Dawidek 	gp->access = g_std_access;
585c58794deSPawel Jakub Dawidek 	/*
586c58794deSPawel Jakub Dawidek 	 * g_eli_read_metadata() is always called from the event thread.
587c58794deSPawel Jakub Dawidek 	 * Our geom is created and destroyed in the same event, so there
588c58794deSPawel Jakub Dawidek 	 * could be no orphan nor spoil event in the meantime.
589c58794deSPawel Jakub Dawidek 	 */
590c58794deSPawel Jakub Dawidek 	gp->orphan = g_eli_orphan_spoil_assert;
591c58794deSPawel Jakub Dawidek 	gp->spoiled = g_eli_orphan_spoil_assert;
592c58794deSPawel Jakub Dawidek 	cp = g_new_consumer(gp);
593c58794deSPawel Jakub Dawidek 	error = g_attach(cp, pp);
594c58794deSPawel Jakub Dawidek 	if (error != 0)
595c58794deSPawel Jakub Dawidek 		goto end;
596c58794deSPawel Jakub Dawidek 	error = g_access(cp, 1, 0, 0);
597c58794deSPawel Jakub Dawidek 	if (error != 0)
598c58794deSPawel Jakub Dawidek 		goto end;
599c58794deSPawel Jakub Dawidek 	g_topology_unlock();
600c58794deSPawel Jakub Dawidek 	buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
601c58794deSPawel Jakub Dawidek 	    &error);
602c58794deSPawel Jakub Dawidek 	g_topology_lock();
6038a4a44b5SMaxim Sobolev 	if (buf == NULL)
604c58794deSPawel Jakub Dawidek 		goto end;
605c58794deSPawel Jakub Dawidek 	eli_metadata_decode(buf, md);
606c58794deSPawel Jakub Dawidek end:
607c58794deSPawel Jakub Dawidek 	if (buf != NULL)
608c58794deSPawel Jakub Dawidek 		g_free(buf);
609c58794deSPawel Jakub Dawidek 	if (cp->provider != NULL) {
610c58794deSPawel Jakub Dawidek 		if (cp->acr == 1)
611c58794deSPawel Jakub Dawidek 			g_access(cp, -1, 0, 0);
612c58794deSPawel Jakub Dawidek 		g_detach(cp);
613c58794deSPawel Jakub Dawidek 	}
614c58794deSPawel Jakub Dawidek 	g_destroy_consumer(cp);
615c58794deSPawel Jakub Dawidek 	g_destroy_geom(gp);
616c58794deSPawel Jakub Dawidek 	return (error);
617c58794deSPawel Jakub Dawidek }
618c58794deSPawel Jakub Dawidek 
619c58794deSPawel Jakub Dawidek /*
620c58794deSPawel Jakub Dawidek  * The function is called when we had last close on provider and user requested
621c58794deSPawel Jakub Dawidek  * to close it when this situation occur.
622c58794deSPawel Jakub Dawidek  */
623c58794deSPawel Jakub Dawidek static void
624c58794deSPawel Jakub Dawidek g_eli_last_close(struct g_eli_softc *sc)
625c58794deSPawel Jakub Dawidek {
626c58794deSPawel Jakub Dawidek 	struct g_geom *gp;
627c58794deSPawel Jakub Dawidek 	struct g_provider *pp;
628c58794deSPawel Jakub Dawidek 	char ppname[64];
629c58794deSPawel Jakub Dawidek 	int error;
630c58794deSPawel Jakub Dawidek 
631c58794deSPawel Jakub Dawidek 	g_topology_assert();
632c58794deSPawel Jakub Dawidek 	gp = sc->sc_geom;
633c58794deSPawel Jakub Dawidek 	pp = LIST_FIRST(&gp->provider);
634c58794deSPawel Jakub Dawidek 	strlcpy(ppname, pp->name, sizeof(ppname));
6355ad4a7c7SPawel Jakub Dawidek 	error = g_eli_destroy(sc, TRUE);
636c58794deSPawel Jakub Dawidek 	KASSERT(error == 0, ("Cannot detach %s on last close (error=%d).",
637c58794deSPawel Jakub Dawidek 	    ppname, error));
638c58794deSPawel Jakub Dawidek 	G_ELI_DEBUG(0, "Detached %s on last close.", ppname);
639c58794deSPawel Jakub Dawidek }
640c58794deSPawel Jakub Dawidek 
641c58794deSPawel Jakub Dawidek int
642c58794deSPawel Jakub Dawidek g_eli_access(struct g_provider *pp, int dr, int dw, int de)
643c58794deSPawel Jakub Dawidek {
644c58794deSPawel Jakub Dawidek 	struct g_eli_softc *sc;
645c58794deSPawel Jakub Dawidek 	struct g_geom *gp;
646c58794deSPawel Jakub Dawidek 
647c58794deSPawel Jakub Dawidek 	gp = pp->geom;
648c58794deSPawel Jakub Dawidek 	sc = gp->softc;
649c58794deSPawel Jakub Dawidek 
650c58794deSPawel Jakub Dawidek 	if (dw > 0) {
65185059016SPawel Jakub Dawidek 		if (sc->sc_flags & G_ELI_FLAG_RO) {
65285059016SPawel Jakub Dawidek 			/* Deny write attempts. */
65385059016SPawel Jakub Dawidek 			return (EROFS);
65485059016SPawel Jakub Dawidek 		}
655c58794deSPawel Jakub Dawidek 		/* Someone is opening us for write, we need to remember that. */
656c58794deSPawel Jakub Dawidek 		sc->sc_flags |= G_ELI_FLAG_WOPEN;
657c58794deSPawel Jakub Dawidek 		return (0);
658c58794deSPawel Jakub Dawidek 	}
659c58794deSPawel Jakub Dawidek 	/* Is this the last close? */
660c58794deSPawel Jakub Dawidek 	if (pp->acr + dr > 0 || pp->acw + dw > 0 || pp->ace + de > 0)
661c58794deSPawel Jakub Dawidek 		return (0);
662c58794deSPawel Jakub Dawidek 
663c58794deSPawel Jakub Dawidek 	/*
664c58794deSPawel Jakub Dawidek 	 * Automatically detach on last close if requested.
665c58794deSPawel Jakub Dawidek 	 */
666c58794deSPawel Jakub Dawidek 	if ((sc->sc_flags & G_ELI_FLAG_RW_DETACH) ||
667c58794deSPawel Jakub Dawidek 	    (sc->sc_flags & G_ELI_FLAG_WOPEN)) {
668c58794deSPawel Jakub Dawidek 		g_eli_last_close(sc);
669c58794deSPawel Jakub Dawidek 	}
670c58794deSPawel Jakub Dawidek 	return (0);
671c58794deSPawel Jakub Dawidek }
672c58794deSPawel Jakub Dawidek 
673eba8f137SPawel Jakub Dawidek static int
674eba8f137SPawel Jakub Dawidek g_eli_cpu_is_disabled(int cpu)
675eba8f137SPawel Jakub Dawidek {
676eba8f137SPawel Jakub Dawidek #ifdef SMP
67771a19bdcSAttilio Rao 	return (CPU_ISSET(cpu, &hlt_cpus_mask));
678eba8f137SPawel Jakub Dawidek #else
679eba8f137SPawel Jakub Dawidek 	return (0);
680eba8f137SPawel Jakub Dawidek #endif
681eba8f137SPawel Jakub Dawidek }
682eba8f137SPawel Jakub Dawidek 
683c58794deSPawel Jakub Dawidek struct g_geom *
684c58794deSPawel Jakub Dawidek g_eli_create(struct gctl_req *req, struct g_class *mp, struct g_provider *bpp,
685c58794deSPawel Jakub Dawidek     const struct g_eli_metadata *md, const u_char *mkey, int nkey)
686c58794deSPawel Jakub Dawidek {
687c58794deSPawel Jakub Dawidek 	struct g_eli_softc *sc;
688c58794deSPawel Jakub Dawidek 	struct g_eli_worker *wr;
689c58794deSPawel Jakub Dawidek 	struct g_geom *gp;
690c58794deSPawel Jakub Dawidek 	struct g_provider *pp;
691c58794deSPawel Jakub Dawidek 	struct g_consumer *cp;
692c58794deSPawel Jakub Dawidek 	u_int i, threads;
693c58794deSPawel Jakub Dawidek 	int error;
694c58794deSPawel Jakub Dawidek 
695c58794deSPawel Jakub Dawidek 	G_ELI_DEBUG(1, "Creating device %s%s.", bpp->name, G_ELI_SUFFIX);
696c58794deSPawel Jakub Dawidek 
697c58794deSPawel Jakub Dawidek 	gp = g_new_geomf(mp, "%s%s", bpp->name, G_ELI_SUFFIX);
698c58794deSPawel Jakub Dawidek 	sc = malloc(sizeof(*sc), M_ELI, M_WAITOK | M_ZERO);
699c58794deSPawel Jakub Dawidek 	gp->start = g_eli_start;
700c58794deSPawel Jakub Dawidek 	/*
701c58794deSPawel Jakub Dawidek 	 * Spoiling cannot happen actually, because we keep provider open for
70285059016SPawel Jakub Dawidek 	 * writing all the time or provider is read-only.
703c58794deSPawel Jakub Dawidek 	 */
704c58794deSPawel Jakub Dawidek 	gp->spoiled = g_eli_orphan_spoil_assert;
705c58794deSPawel Jakub Dawidek 	gp->orphan = g_eli_orphan;
70685059016SPawel Jakub Dawidek 	gp->dumpconf = g_eli_dumpconf;
707c58794deSPawel Jakub Dawidek 	/*
70885059016SPawel Jakub Dawidek 	 * If detach-on-last-close feature is not enabled and we don't operate
70985059016SPawel Jakub Dawidek 	 * on read-only provider, we can simply use g_std_access().
710c58794deSPawel Jakub Dawidek 	 */
71185059016SPawel Jakub Dawidek 	if (md->md_flags & (G_ELI_FLAG_WO_DETACH | G_ELI_FLAG_RO))
712c58794deSPawel Jakub Dawidek 		gp->access = g_eli_access;
713c58794deSPawel Jakub Dawidek 	else
714c58794deSPawel Jakub Dawidek 		gp->access = g_std_access;
715c58794deSPawel Jakub Dawidek 
7165ad4a7c7SPawel Jakub Dawidek 	sc->sc_inflight = 0;
7173ac01bc2SPawel Jakub Dawidek 	sc->sc_crypto = G_ELI_CRYPTO_UNKNOWN;
718c58794deSPawel Jakub Dawidek 	sc->sc_flags = md->md_flags;
7192bd4ade6SPawel Jakub Dawidek 	/* Backward compatibility. */
720efb46508SPawel Jakub Dawidek 	if (md->md_version < 4)
7212bd4ade6SPawel Jakub Dawidek 		sc->sc_flags |= G_ELI_FLAG_NATIVE_BYTE_ORDER;
722c6a26d4cSPawel Jakub Dawidek 	if (md->md_version < 5)
723c6a26d4cSPawel Jakub Dawidek 		sc->sc_flags |= G_ELI_FLAG_SINGLE_KEY;
724ad0a5236SPawel Jakub Dawidek 	if (md->md_version < 6 && (sc->sc_flags & G_ELI_FLAG_AUTH) != 0)
725ad0a5236SPawel Jakub Dawidek 		sc->sc_flags |= G_ELI_FLAG_FIRST_KEY;
726eaa3b919SPawel Jakub Dawidek 	sc->sc_ealgo = md->md_ealgo;
727c58794deSPawel Jakub Dawidek 	sc->sc_nkey = nkey;
728eaa3b919SPawel Jakub Dawidek 
729eaa3b919SPawel Jakub Dawidek 	if (sc->sc_flags & G_ELI_FLAG_AUTH) {
730eaa3b919SPawel Jakub Dawidek 		sc->sc_akeylen = sizeof(sc->sc_akey) * 8;
731eaa3b919SPawel Jakub Dawidek 		sc->sc_aalgo = md->md_aalgo;
732eaa3b919SPawel Jakub Dawidek 		sc->sc_alen = g_eli_hashlen(sc->sc_aalgo);
733eaa3b919SPawel Jakub Dawidek 
734eaa3b919SPawel Jakub Dawidek 		sc->sc_data_per_sector = bpp->sectorsize - sc->sc_alen;
735eaa3b919SPawel Jakub Dawidek 		/*
736eaa3b919SPawel Jakub Dawidek 		 * Some hash functions (like SHA1 and RIPEMD160) generates hash
737eaa3b919SPawel Jakub Dawidek 		 * which length is not multiple of 128 bits, but we want data
738eaa3b919SPawel Jakub Dawidek 		 * length to be multiple of 128, so we can encrypt without
739eaa3b919SPawel Jakub Dawidek 		 * padding. The line below rounds down data length to multiple
740eaa3b919SPawel Jakub Dawidek 		 * of 128 bits.
741eaa3b919SPawel Jakub Dawidek 		 */
742eaa3b919SPawel Jakub Dawidek 		sc->sc_data_per_sector -= sc->sc_data_per_sector % 16;
743eaa3b919SPawel Jakub Dawidek 
744eaa3b919SPawel Jakub Dawidek 		sc->sc_bytes_per_sector =
745eaa3b919SPawel Jakub Dawidek 		    (md->md_sectorsize - 1) / sc->sc_data_per_sector + 1;
746eaa3b919SPawel Jakub Dawidek 		sc->sc_bytes_per_sector *= bpp->sectorsize;
747eaa3b919SPawel Jakub Dawidek 	}
748c58794deSPawel Jakub Dawidek 
749c58794deSPawel Jakub Dawidek 	gp->softc = sc;
750c58794deSPawel Jakub Dawidek 	sc->sc_geom = gp;
751c58794deSPawel Jakub Dawidek 
752c58794deSPawel Jakub Dawidek 	bioq_init(&sc->sc_queue);
753c58794deSPawel Jakub Dawidek 	mtx_init(&sc->sc_queue_mtx, "geli:queue", NULL, MTX_DEF);
7541e09ff3dSPawel Jakub Dawidek 	mtx_init(&sc->sc_ekeys_lock, "geli:ekeys", NULL, MTX_DEF);
755c58794deSPawel Jakub Dawidek 
756c58794deSPawel Jakub Dawidek 	pp = NULL;
757c58794deSPawel Jakub Dawidek 	cp = g_new_consumer(gp);
758c58794deSPawel Jakub Dawidek 	error = g_attach(cp, bpp);
759c58794deSPawel Jakub Dawidek 	if (error != 0) {
760c58794deSPawel Jakub Dawidek 		if (req != NULL) {
761c58794deSPawel Jakub Dawidek 			gctl_error(req, "Cannot attach to %s (error=%d).",
762c58794deSPawel Jakub Dawidek 			    bpp->name, error);
763c58794deSPawel Jakub Dawidek 		} else {
764c58794deSPawel Jakub Dawidek 			G_ELI_DEBUG(1, "Cannot attach to %s (error=%d).",
765c58794deSPawel Jakub Dawidek 			    bpp->name, error);
766c58794deSPawel Jakub Dawidek 		}
767c58794deSPawel Jakub Dawidek 		goto failed;
768c58794deSPawel Jakub Dawidek 	}
769c58794deSPawel Jakub Dawidek 	/*
770c58794deSPawel Jakub Dawidek 	 * Keep provider open all the time, so we can run critical tasks,
771c58794deSPawel Jakub Dawidek 	 * like Master Keys deletion, without wondering if we can open
772c58794deSPawel Jakub Dawidek 	 * provider or not.
77385059016SPawel Jakub Dawidek 	 * We don't open provider for writing only when user requested read-only
77485059016SPawel Jakub Dawidek 	 * access.
775c58794deSPawel Jakub Dawidek 	 */
77685059016SPawel Jakub Dawidek 	if (sc->sc_flags & G_ELI_FLAG_RO)
77785059016SPawel Jakub Dawidek 		error = g_access(cp, 1, 0, 1);
77885059016SPawel Jakub Dawidek 	else
779c58794deSPawel Jakub Dawidek 		error = g_access(cp, 1, 1, 1);
780c58794deSPawel Jakub Dawidek 	if (error != 0) {
781c58794deSPawel Jakub Dawidek 		if (req != NULL) {
782c58794deSPawel Jakub Dawidek 			gctl_error(req, "Cannot access %s (error=%d).",
783c58794deSPawel Jakub Dawidek 			    bpp->name, error);
784c58794deSPawel Jakub Dawidek 		} else {
785c58794deSPawel Jakub Dawidek 			G_ELI_DEBUG(1, "Cannot access %s (error=%d).",
786c58794deSPawel Jakub Dawidek 			    bpp->name, error);
787c58794deSPawel Jakub Dawidek 		}
788c58794deSPawel Jakub Dawidek 		goto failed;
789c58794deSPawel Jakub Dawidek 	}
790c58794deSPawel Jakub Dawidek 
791c6a26d4cSPawel Jakub Dawidek 	sc->sc_sectorsize = md->md_sectorsize;
792c6a26d4cSPawel Jakub Dawidek 	sc->sc_mediasize = bpp->mediasize;
793c6a26d4cSPawel Jakub Dawidek 	if (!(sc->sc_flags & G_ELI_FLAG_ONETIME))
794c6a26d4cSPawel Jakub Dawidek 		sc->sc_mediasize -= bpp->sectorsize;
795c6a26d4cSPawel Jakub Dawidek 	if (!(sc->sc_flags & G_ELI_FLAG_AUTH))
796c6a26d4cSPawel Jakub Dawidek 		sc->sc_mediasize -= (sc->sc_mediasize % sc->sc_sectorsize);
797c6a26d4cSPawel Jakub Dawidek 	else {
798c6a26d4cSPawel Jakub Dawidek 		sc->sc_mediasize /= sc->sc_bytes_per_sector;
799c6a26d4cSPawel Jakub Dawidek 		sc->sc_mediasize *= sc->sc_sectorsize;
800c6a26d4cSPawel Jakub Dawidek 	}
801c6a26d4cSPawel Jakub Dawidek 
802c6a26d4cSPawel Jakub Dawidek 	/*
803c6a26d4cSPawel Jakub Dawidek 	 * Remember the keys in our softc structure.
804c6a26d4cSPawel Jakub Dawidek 	 */
805c6a26d4cSPawel Jakub Dawidek 	g_eli_mkey_propagate(sc, mkey);
806c6a26d4cSPawel Jakub Dawidek 	sc->sc_ekeylen = md->md_keylen;
807c6a26d4cSPawel Jakub Dawidek 
808c58794deSPawel Jakub Dawidek 	LIST_INIT(&sc->sc_workers);
809c58794deSPawel Jakub Dawidek 
810c58794deSPawel Jakub Dawidek 	threads = g_eli_threads;
811dd549194SPawel Jakub Dawidek 	if (threads == 0)
812dd549194SPawel Jakub Dawidek 		threads = mp_ncpus;
813dd549194SPawel Jakub Dawidek 	else if (threads > mp_ncpus) {
814c58794deSPawel Jakub Dawidek 		/* There is really no need for too many worker threads. */
815dd549194SPawel Jakub Dawidek 		threads = mp_ncpus;
816dd549194SPawel Jakub Dawidek 		G_ELI_DEBUG(0, "Reducing number of threads to %u.", threads);
817c58794deSPawel Jakub Dawidek 	}
818c58794deSPawel Jakub Dawidek 	for (i = 0; i < threads; i++) {
819eba8f137SPawel Jakub Dawidek 		if (g_eli_cpu_is_disabled(i)) {
820eba8f137SPawel Jakub Dawidek 			G_ELI_DEBUG(1, "%s: CPU %u disabled, skipping.",
8211506db21SPawel Jakub Dawidek 			    bpp->name, i);
822eba8f137SPawel Jakub Dawidek 			continue;
823eba8f137SPawel Jakub Dawidek 		}
824c58794deSPawel Jakub Dawidek 		wr = malloc(sizeof(*wr), M_ELI, M_WAITOK | M_ZERO);
825c58794deSPawel Jakub Dawidek 		wr->w_softc = sc;
826c58794deSPawel Jakub Dawidek 		wr->w_number = i;
8275ad4a7c7SPawel Jakub Dawidek 		wr->w_active = TRUE;
828c58794deSPawel Jakub Dawidek 
8293ac01bc2SPawel Jakub Dawidek 		error = g_eli_newsession(wr);
830c58794deSPawel Jakub Dawidek 		if (error != 0) {
831c58794deSPawel Jakub Dawidek 			free(wr, M_ELI);
832c58794deSPawel Jakub Dawidek 			if (req != NULL) {
833c58794deSPawel Jakub Dawidek 				gctl_error(req, "Cannot set up crypto session "
834c58794deSPawel Jakub Dawidek 				    "for %s (error=%d).", bpp->name, error);
835c58794deSPawel Jakub Dawidek 			} else {
836c58794deSPawel Jakub Dawidek 				G_ELI_DEBUG(1, "Cannot set up crypto session "
837c58794deSPawel Jakub Dawidek 				    "for %s (error=%d).", bpp->name, error);
838c58794deSPawel Jakub Dawidek 			}
839c58794deSPawel Jakub Dawidek 			goto failed;
840c58794deSPawel Jakub Dawidek 		}
841c58794deSPawel Jakub Dawidek 
8423745c395SJulian Elischer 		error = kproc_create(g_eli_worker, wr, &wr->w_proc, 0, 0,
843c58794deSPawel Jakub Dawidek 		    "g_eli[%u] %s", i, bpp->name);
844c58794deSPawel Jakub Dawidek 		if (error != 0) {
8453ac01bc2SPawel Jakub Dawidek 			g_eli_freesession(wr);
846c58794deSPawel Jakub Dawidek 			free(wr, M_ELI);
847c58794deSPawel Jakub Dawidek 			if (req != NULL) {
848dddd1d53SPawel Jakub Dawidek 				gctl_error(req, "Cannot create kernel thread "
849dddd1d53SPawel Jakub Dawidek 				    "for %s (error=%d).", bpp->name, error);
850c58794deSPawel Jakub Dawidek 			} else {
851dddd1d53SPawel Jakub Dawidek 				G_ELI_DEBUG(1, "Cannot create kernel thread "
852dddd1d53SPawel Jakub Dawidek 				    "for %s (error=%d).", bpp->name, error);
853c58794deSPawel Jakub Dawidek 			}
854c58794deSPawel Jakub Dawidek 			goto failed;
855c58794deSPawel Jakub Dawidek 		}
856c58794deSPawel Jakub Dawidek 		LIST_INSERT_HEAD(&sc->sc_workers, wr, w_next);
857dddd1d53SPawel Jakub Dawidek 		/* If we have hardware support, one thread is enough. */
858dddd1d53SPawel Jakub Dawidek 		if (sc->sc_crypto == G_ELI_CRYPTO_HW)
859dddd1d53SPawel Jakub Dawidek 			break;
860c58794deSPawel Jakub Dawidek 	}
861c58794deSPawel Jakub Dawidek 
862c58794deSPawel Jakub Dawidek 	/*
863c58794deSPawel Jakub Dawidek 	 * Create decrypted provider.
864c58794deSPawel Jakub Dawidek 	 */
865c58794deSPawel Jakub Dawidek 	pp = g_new_providerf(gp, "%s%s", bpp->name, G_ELI_SUFFIX);
866c6a26d4cSPawel Jakub Dawidek 	pp->mediasize = sc->sc_mediasize;
867c6a26d4cSPawel Jakub Dawidek 	pp->sectorsize = sc->sc_sectorsize;
868eaa3b919SPawel Jakub Dawidek 
869c58794deSPawel Jakub Dawidek 	g_error_provider(pp, 0);
870c58794deSPawel Jakub Dawidek 
871c58794deSPawel Jakub Dawidek 	G_ELI_DEBUG(0, "Device %s created.", pp->name);
872eaa3b919SPawel Jakub Dawidek 	G_ELI_DEBUG(0, "Encryption: %s %u", g_eli_algo2str(sc->sc_ealgo),
873eaa3b919SPawel Jakub Dawidek 	    sc->sc_ekeylen);
874eaa3b919SPawel Jakub Dawidek 	if (sc->sc_flags & G_ELI_FLAG_AUTH)
875eaa3b919SPawel Jakub Dawidek 		G_ELI_DEBUG(0, " Integrity: %s", g_eli_algo2str(sc->sc_aalgo));
876c58794deSPawel Jakub Dawidek 	G_ELI_DEBUG(0, "    Crypto: %s",
877c58794deSPawel Jakub Dawidek 	    sc->sc_crypto == G_ELI_CRYPTO_SW ? "software" : "hardware");
878c58794deSPawel Jakub Dawidek 	return (gp);
879c58794deSPawel Jakub Dawidek failed:
880c58794deSPawel Jakub Dawidek 	mtx_lock(&sc->sc_queue_mtx);
881c58794deSPawel Jakub Dawidek 	sc->sc_flags |= G_ELI_FLAG_DESTROY;
882c58794deSPawel Jakub Dawidek 	wakeup(sc);
883c58794deSPawel Jakub Dawidek 	/*
884c58794deSPawel Jakub Dawidek 	 * Wait for kernel threads self destruction.
885c58794deSPawel Jakub Dawidek 	 */
886c58794deSPawel Jakub Dawidek 	while (!LIST_EMPTY(&sc->sc_workers)) {
887c58794deSPawel Jakub Dawidek 		msleep(&sc->sc_workers, &sc->sc_queue_mtx, PRIBIO,
888c58794deSPawel Jakub Dawidek 		    "geli:destroy", 0);
889c58794deSPawel Jakub Dawidek 	}
890c58794deSPawel Jakub Dawidek 	mtx_destroy(&sc->sc_queue_mtx);
891c58794deSPawel Jakub Dawidek 	if (cp->provider != NULL) {
892c58794deSPawel Jakub Dawidek 		if (cp->acr == 1)
893c58794deSPawel Jakub Dawidek 			g_access(cp, -1, -1, -1);
894c58794deSPawel Jakub Dawidek 		g_detach(cp);
895c58794deSPawel Jakub Dawidek 	}
896c58794deSPawel Jakub Dawidek 	g_destroy_consumer(cp);
897c58794deSPawel Jakub Dawidek 	g_destroy_geom(gp);
8981e09ff3dSPawel Jakub Dawidek 	g_eli_key_destroy(sc);
899c58794deSPawel Jakub Dawidek 	bzero(sc, sizeof(*sc));
900c58794deSPawel Jakub Dawidek 	free(sc, M_ELI);
901c58794deSPawel Jakub Dawidek 	return (NULL);
902c58794deSPawel Jakub Dawidek }
903c58794deSPawel Jakub Dawidek 
904c58794deSPawel Jakub Dawidek int
905c58794deSPawel Jakub Dawidek g_eli_destroy(struct g_eli_softc *sc, boolean_t force)
906c58794deSPawel Jakub Dawidek {
907c58794deSPawel Jakub Dawidek 	struct g_geom *gp;
908c58794deSPawel Jakub Dawidek 	struct g_provider *pp;
909c58794deSPawel Jakub Dawidek 
910c58794deSPawel Jakub Dawidek 	g_topology_assert();
911c58794deSPawel Jakub Dawidek 
912c58794deSPawel Jakub Dawidek 	if (sc == NULL)
913c58794deSPawel Jakub Dawidek 		return (ENXIO);
914c58794deSPawel Jakub Dawidek 
915c58794deSPawel Jakub Dawidek 	gp = sc->sc_geom;
916c58794deSPawel Jakub Dawidek 	pp = LIST_FIRST(&gp->provider);
917c58794deSPawel Jakub Dawidek 	if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
918c58794deSPawel Jakub Dawidek 		if (force) {
919c58794deSPawel Jakub Dawidek 			G_ELI_DEBUG(1, "Device %s is still open, so it "
92098645006SChristian Brueffer 			    "cannot be definitely removed.", pp->name);
921c58794deSPawel Jakub Dawidek 		} else {
922c58794deSPawel Jakub Dawidek 			G_ELI_DEBUG(1,
923c58794deSPawel Jakub Dawidek 			    "Device %s is still open (r%dw%de%d).", pp->name,
924c58794deSPawel Jakub Dawidek 			    pp->acr, pp->acw, pp->ace);
925c58794deSPawel Jakub Dawidek 			return (EBUSY);
926c58794deSPawel Jakub Dawidek 		}
927c58794deSPawel Jakub Dawidek 	}
928c58794deSPawel Jakub Dawidek 
929c58794deSPawel Jakub Dawidek 	mtx_lock(&sc->sc_queue_mtx);
930c58794deSPawel Jakub Dawidek 	sc->sc_flags |= G_ELI_FLAG_DESTROY;
931c58794deSPawel Jakub Dawidek 	wakeup(sc);
932c58794deSPawel Jakub Dawidek 	while (!LIST_EMPTY(&sc->sc_workers)) {
933c58794deSPawel Jakub Dawidek 		msleep(&sc->sc_workers, &sc->sc_queue_mtx, PRIBIO,
934c58794deSPawel Jakub Dawidek 		    "geli:destroy", 0);
935c58794deSPawel Jakub Dawidek 	}
936c58794deSPawel Jakub Dawidek 	mtx_destroy(&sc->sc_queue_mtx);
937c58794deSPawel Jakub Dawidek 	gp->softc = NULL;
9381e09ff3dSPawel Jakub Dawidek 	g_eli_key_destroy(sc);
939c58794deSPawel Jakub Dawidek 	bzero(sc, sizeof(*sc));
940c58794deSPawel Jakub Dawidek 	free(sc, M_ELI);
941c58794deSPawel Jakub Dawidek 
942c58794deSPawel Jakub Dawidek 	if (pp == NULL || (pp->acr == 0 && pp->acw == 0 && pp->ace == 0))
943c58794deSPawel Jakub Dawidek 		G_ELI_DEBUG(0, "Device %s destroyed.", gp->name);
944c58794deSPawel Jakub Dawidek 	g_wither_geom_close(gp, ENXIO);
945c58794deSPawel Jakub Dawidek 
946c58794deSPawel Jakub Dawidek 	return (0);
947c58794deSPawel Jakub Dawidek }
948c58794deSPawel Jakub Dawidek 
949c58794deSPawel Jakub Dawidek static int
950c58794deSPawel Jakub Dawidek g_eli_destroy_geom(struct gctl_req *req __unused,
951c58794deSPawel Jakub Dawidek     struct g_class *mp __unused, struct g_geom *gp)
952c58794deSPawel Jakub Dawidek {
953c58794deSPawel Jakub Dawidek 	struct g_eli_softc *sc;
954c58794deSPawel Jakub Dawidek 
955c58794deSPawel Jakub Dawidek 	sc = gp->softc;
9565ad4a7c7SPawel Jakub Dawidek 	return (g_eli_destroy(sc, FALSE));
957c58794deSPawel Jakub Dawidek }
958c58794deSPawel Jakub Dawidek 
9599af2131bSPawel Jakub Dawidek static int
9609af2131bSPawel Jakub Dawidek g_eli_keyfiles_load(struct hmac_ctx *ctx, const char *provider)
9619af2131bSPawel Jakub Dawidek {
9621e189c08SMarcel Moolenaar 	u_char *keyfile, *data;
9639af2131bSPawel Jakub Dawidek 	char *file, name[64];
9641e189c08SMarcel Moolenaar 	size_t size;
9659af2131bSPawel Jakub Dawidek 	int i;
9669af2131bSPawel Jakub Dawidek 
9679af2131bSPawel Jakub Dawidek 	for (i = 0; ; i++) {
9689af2131bSPawel Jakub Dawidek 		snprintf(name, sizeof(name), "%s:geli_keyfile%d", provider, i);
9699af2131bSPawel Jakub Dawidek 		keyfile = preload_search_by_type(name);
9709af2131bSPawel Jakub Dawidek 		if (keyfile == NULL)
9719af2131bSPawel Jakub Dawidek 			return (i);	/* Return number of loaded keyfiles. */
9721e189c08SMarcel Moolenaar 		data = preload_fetch_addr(keyfile);
9739af2131bSPawel Jakub Dawidek 		if (data == NULL) {
9749af2131bSPawel Jakub Dawidek 			G_ELI_DEBUG(0, "Cannot find key file data for %s.",
9759af2131bSPawel Jakub Dawidek 			    name);
9769af2131bSPawel Jakub Dawidek 			return (0);
9779af2131bSPawel Jakub Dawidek 		}
9781e189c08SMarcel Moolenaar 		size = preload_fetch_size(keyfile);
9791e189c08SMarcel Moolenaar 		if (size == 0) {
9809af2131bSPawel Jakub Dawidek 			G_ELI_DEBUG(0, "Cannot find key file size for %s.",
9819af2131bSPawel Jakub Dawidek 			    name);
9829af2131bSPawel Jakub Dawidek 			return (0);
9839af2131bSPawel Jakub Dawidek 		}
9849af2131bSPawel Jakub Dawidek 		file = preload_search_info(keyfile, MODINFO_NAME);
9859af2131bSPawel Jakub Dawidek 		if (file == NULL) {
9869af2131bSPawel Jakub Dawidek 			G_ELI_DEBUG(0, "Cannot find key file name for %s.",
9879af2131bSPawel Jakub Dawidek 			    name);
9889af2131bSPawel Jakub Dawidek 			return (0);
9899af2131bSPawel Jakub Dawidek 		}
9909af2131bSPawel Jakub Dawidek 		G_ELI_DEBUG(1, "Loaded keyfile %s for %s (type: %s).", file,
9919af2131bSPawel Jakub Dawidek 		    provider, name);
9921e189c08SMarcel Moolenaar 		g_eli_crypto_hmac_update(ctx, data, size);
9939af2131bSPawel Jakub Dawidek 	}
9949af2131bSPawel Jakub Dawidek }
9959af2131bSPawel Jakub Dawidek 
9969af2131bSPawel Jakub Dawidek static void
9979af2131bSPawel Jakub Dawidek g_eli_keyfiles_clear(const char *provider)
9989af2131bSPawel Jakub Dawidek {
9991e189c08SMarcel Moolenaar 	u_char *keyfile, *data;
10009af2131bSPawel Jakub Dawidek 	char name[64];
10011e189c08SMarcel Moolenaar 	size_t size;
10029af2131bSPawel Jakub Dawidek 	int i;
10039af2131bSPawel Jakub Dawidek 
10049af2131bSPawel Jakub Dawidek 	for (i = 0; ; i++) {
10059af2131bSPawel Jakub Dawidek 		snprintf(name, sizeof(name), "%s:geli_keyfile%d", provider, i);
10069af2131bSPawel Jakub Dawidek 		keyfile = preload_search_by_type(name);
10079af2131bSPawel Jakub Dawidek 		if (keyfile == NULL)
10089af2131bSPawel Jakub Dawidek 			return;
10091e189c08SMarcel Moolenaar 		data = preload_fetch_addr(keyfile);
10101e189c08SMarcel Moolenaar 		size = preload_fetch_size(keyfile);
10111e189c08SMarcel Moolenaar 		if (data != NULL && size != 0)
10121e189c08SMarcel Moolenaar 			bzero(data, size);
10139af2131bSPawel Jakub Dawidek 	}
10149af2131bSPawel Jakub Dawidek }
10159af2131bSPawel Jakub Dawidek 
1016c58794deSPawel Jakub Dawidek /*
1017c58794deSPawel Jakub Dawidek  * Tasting is only made on boot.
1018c58794deSPawel Jakub Dawidek  * We detect providers which should be attached before root is mounted.
1019c58794deSPawel Jakub Dawidek  */
1020c58794deSPawel Jakub Dawidek static struct g_geom *
1021c58794deSPawel Jakub Dawidek g_eli_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
1022c58794deSPawel Jakub Dawidek {
1023c58794deSPawel Jakub Dawidek 	struct g_eli_metadata md;
1024c58794deSPawel Jakub Dawidek 	struct g_geom *gp;
1025c58794deSPawel Jakub Dawidek 	struct hmac_ctx ctx;
1026c58794deSPawel Jakub Dawidek 	char passphrase[256];
1027c58794deSPawel Jakub Dawidek 	u_char key[G_ELI_USERKEYLEN], mkey[G_ELI_DATAIVKEYLEN];
10289af2131bSPawel Jakub Dawidek 	u_int i, nkey, nkeyfiles, tries;
1029c58794deSPawel Jakub Dawidek 	int error;
1030c58794deSPawel Jakub Dawidek 
1031c58794deSPawel Jakub Dawidek 	g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
1032c58794deSPawel Jakub Dawidek 	g_topology_assert();
1033c58794deSPawel Jakub Dawidek 
1034df3aed4fSPawel Jakub Dawidek 	if (root_mounted() || g_eli_tries == 0)
1035c58794deSPawel Jakub Dawidek 		return (NULL);
1036c58794deSPawel Jakub Dawidek 
1037c58794deSPawel Jakub Dawidek 	G_ELI_DEBUG(3, "Tasting %s.", pp->name);
1038c58794deSPawel Jakub Dawidek 
1039c58794deSPawel Jakub Dawidek 	error = g_eli_read_metadata(mp, pp, &md);
1040c58794deSPawel Jakub Dawidek 	if (error != 0)
1041c58794deSPawel Jakub Dawidek 		return (NULL);
1042c58794deSPawel Jakub Dawidek 	gp = NULL;
1043c58794deSPawel Jakub Dawidek 
1044c58794deSPawel Jakub Dawidek 	if (strcmp(md.md_magic, G_ELI_MAGIC) != 0)
1045c58794deSPawel Jakub Dawidek 		return (NULL);
1046c58794deSPawel Jakub Dawidek 	if (md.md_version > G_ELI_VERSION) {
1047c58794deSPawel Jakub Dawidek 		printf("geom_eli.ko module is too old to handle %s.\n",
1048c58794deSPawel Jakub Dawidek 		    pp->name);
1049c58794deSPawel Jakub Dawidek 		return (NULL);
1050c58794deSPawel Jakub Dawidek 	}
1051c58794deSPawel Jakub Dawidek 	if (md.md_provsize != pp->mediasize)
1052c58794deSPawel Jakub Dawidek 		return (NULL);
1053c58794deSPawel Jakub Dawidek 	/* Should we attach it on boot? */
1054eaa3b919SPawel Jakub Dawidek 	if (!(md.md_flags & G_ELI_FLAG_BOOT))
1055c58794deSPawel Jakub Dawidek 		return (NULL);
1056c58794deSPawel Jakub Dawidek 	if (md.md_keys == 0x00) {
1057c58794deSPawel Jakub Dawidek 		G_ELI_DEBUG(0, "No valid keys on %s.", pp->name);
1058c58794deSPawel Jakub Dawidek 		return (NULL);
1059c58794deSPawel Jakub Dawidek 	}
10609af2131bSPawel Jakub Dawidek 	if (md.md_iterations == -1) {
10619af2131bSPawel Jakub Dawidek 		/* If there is no passphrase, we try only once. */
10629af2131bSPawel Jakub Dawidek 		tries = 1;
10639af2131bSPawel Jakub Dawidek 	} else {
10649af2131bSPawel Jakub Dawidek 		/* Ask for the passphrase no more than g_eli_tries times. */
10659af2131bSPawel Jakub Dawidek 		tries = g_eli_tries;
10669af2131bSPawel Jakub Dawidek 	}
10679af2131bSPawel Jakub Dawidek 
10689af2131bSPawel Jakub Dawidek 	for (i = 0; i < tries; i++) {
10699af2131bSPawel Jakub Dawidek 		g_eli_crypto_hmac_init(&ctx, NULL, 0);
1070c58794deSPawel Jakub Dawidek 
1071c58794deSPawel Jakub Dawidek 		/*
10729af2131bSPawel Jakub Dawidek 		 * Load all key files.
1073c58794deSPawel Jakub Dawidek 		 */
10749af2131bSPawel Jakub Dawidek 		nkeyfiles = g_eli_keyfiles_load(&ctx, pp->name);
10759af2131bSPawel Jakub Dawidek 
10769af2131bSPawel Jakub Dawidek 		if (nkeyfiles == 0 && md.md_iterations == -1) {
10779af2131bSPawel Jakub Dawidek 			/*
10789af2131bSPawel Jakub Dawidek 			 * No key files and no passphrase, something is
10799af2131bSPawel Jakub Dawidek 			 * definitely wrong here.
10809af2131bSPawel Jakub Dawidek 			 * geli(8) doesn't allow for such situation, so assume
10819af2131bSPawel Jakub Dawidek 			 * that there was really no passphrase and in that case
10829af2131bSPawel Jakub Dawidek 			 * key files are no properly defined in loader.conf.
10839af2131bSPawel Jakub Dawidek 			 */
10849af2131bSPawel Jakub Dawidek 			G_ELI_DEBUG(0,
10859af2131bSPawel Jakub Dawidek 			    "Found no key files in loader.conf for %s.",
10869af2131bSPawel Jakub Dawidek 			    pp->name);
10879af2131bSPawel Jakub Dawidek 			return (NULL);
10889af2131bSPawel Jakub Dawidek 		}
10899af2131bSPawel Jakub Dawidek 
10909af2131bSPawel Jakub Dawidek 		/* Ask for the passphrase if defined. */
10919af2131bSPawel Jakub Dawidek 		if (md.md_iterations >= 0) {
1092c58794deSPawel Jakub Dawidek 			printf("Enter passphrase for %s: ", pp->name);
10939af2131bSPawel Jakub Dawidek 			gets(passphrase, sizeof(passphrase),
10949af2131bSPawel Jakub Dawidek 			    g_eli_visible_passphrase);
10959af2131bSPawel Jakub Dawidek 		}
10969af2131bSPawel Jakub Dawidek 
1097c58794deSPawel Jakub Dawidek 		/*
1098c58794deSPawel Jakub Dawidek 		 * Prepare Derived-Key from the user passphrase.
1099c58794deSPawel Jakub Dawidek 		 */
1100c58794deSPawel Jakub Dawidek 		if (md.md_iterations == 0) {
1101c58794deSPawel Jakub Dawidek 			g_eli_crypto_hmac_update(&ctx, md.md_salt,
1102c58794deSPawel Jakub Dawidek 			    sizeof(md.md_salt));
1103c58794deSPawel Jakub Dawidek 			g_eli_crypto_hmac_update(&ctx, passphrase,
1104c58794deSPawel Jakub Dawidek 			    strlen(passphrase));
11055527ecd9SPawel Jakub Dawidek 			bzero(passphrase, sizeof(passphrase));
11069af2131bSPawel Jakub Dawidek 		} else if (md.md_iterations > 0) {
1107c58794deSPawel Jakub Dawidek 			u_char dkey[G_ELI_USERKEYLEN];
1108c58794deSPawel Jakub Dawidek 
1109c58794deSPawel Jakub Dawidek 			pkcs5v2_genkey(dkey, sizeof(dkey), md.md_salt,
1110c58794deSPawel Jakub Dawidek 			    sizeof(md.md_salt), passphrase, md.md_iterations);
11115527ecd9SPawel Jakub Dawidek 			bzero(passphrase, sizeof(passphrase));
1112c58794deSPawel Jakub Dawidek 			g_eli_crypto_hmac_update(&ctx, dkey, sizeof(dkey));
1113c58794deSPawel Jakub Dawidek 			bzero(dkey, sizeof(dkey));
1114c58794deSPawel Jakub Dawidek 		}
11159af2131bSPawel Jakub Dawidek 
1116c58794deSPawel Jakub Dawidek 		g_eli_crypto_hmac_final(&ctx, key, 0);
1117c58794deSPawel Jakub Dawidek 
1118c58794deSPawel Jakub Dawidek 		/*
1119c58794deSPawel Jakub Dawidek 		 * Decrypt Master-Key.
1120c58794deSPawel Jakub Dawidek 		 */
1121c58794deSPawel Jakub Dawidek 		error = g_eli_mkey_decrypt(&md, key, mkey, &nkey);
1122c58794deSPawel Jakub Dawidek 		bzero(key, sizeof(key));
1123c58794deSPawel Jakub Dawidek 		if (error == -1) {
11249af2131bSPawel Jakub Dawidek 			if (i == tries - 1) {
11259af2131bSPawel Jakub Dawidek 				G_ELI_DEBUG(0,
11269af2131bSPawel Jakub Dawidek 				    "Wrong key for %s. No tries left.",
11279af2131bSPawel Jakub Dawidek 				    pp->name);
11289af2131bSPawel Jakub Dawidek 				g_eli_keyfiles_clear(pp->name);
11299af2131bSPawel Jakub Dawidek 				return (NULL);
1130c58794deSPawel Jakub Dawidek 			}
1131c58794deSPawel Jakub Dawidek 			G_ELI_DEBUG(0, "Wrong key for %s. Tries left: %u.",
11329af2131bSPawel Jakub Dawidek 			    pp->name, tries - i - 1);
1133c58794deSPawel Jakub Dawidek 			/* Try again. */
1134c58794deSPawel Jakub Dawidek 			continue;
1135c58794deSPawel Jakub Dawidek 		} else if (error > 0) {
1136*038c55adSPawel Jakub Dawidek 			G_ELI_DEBUG(0,
1137*038c55adSPawel Jakub Dawidek 			    "Cannot decrypt Master Key for %s (error=%d).",
1138c58794deSPawel Jakub Dawidek 			    pp->name, error);
11399af2131bSPawel Jakub Dawidek 			g_eli_keyfiles_clear(pp->name);
1140c58794deSPawel Jakub Dawidek 			return (NULL);
1141c58794deSPawel Jakub Dawidek 		}
1142c58794deSPawel Jakub Dawidek 		G_ELI_DEBUG(1, "Using Master Key %u for %s.", nkey, pp->name);
1143c58794deSPawel Jakub Dawidek 		break;
1144c58794deSPawel Jakub Dawidek 	}
1145c58794deSPawel Jakub Dawidek 
1146c58794deSPawel Jakub Dawidek 	/*
1147c58794deSPawel Jakub Dawidek 	 * We have correct key, let's attach provider.
1148c58794deSPawel Jakub Dawidek 	 */
1149c58794deSPawel Jakub Dawidek 	gp = g_eli_create(NULL, mp, pp, &md, mkey, nkey);
1150c58794deSPawel Jakub Dawidek 	bzero(mkey, sizeof(mkey));
1151c58794deSPawel Jakub Dawidek 	bzero(&md, sizeof(md));
1152c58794deSPawel Jakub Dawidek 	if (gp == NULL) {
1153c58794deSPawel Jakub Dawidek 		G_ELI_DEBUG(0, "Cannot create device %s%s.", pp->name,
1154c58794deSPawel Jakub Dawidek 		    G_ELI_SUFFIX);
1155c58794deSPawel Jakub Dawidek 		return (NULL);
1156c58794deSPawel Jakub Dawidek 	}
1157c58794deSPawel Jakub Dawidek 	return (gp);
1158c58794deSPawel Jakub Dawidek }
1159c58794deSPawel Jakub Dawidek 
1160c58794deSPawel Jakub Dawidek static void
1161c58794deSPawel Jakub Dawidek g_eli_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
1162c58794deSPawel Jakub Dawidek     struct g_consumer *cp, struct g_provider *pp)
1163c58794deSPawel Jakub Dawidek {
1164c58794deSPawel Jakub Dawidek 	struct g_eli_softc *sc;
1165c58794deSPawel Jakub Dawidek 
1166c58794deSPawel Jakub Dawidek 	g_topology_assert();
1167c58794deSPawel Jakub Dawidek 	sc = gp->softc;
1168c58794deSPawel Jakub Dawidek 	if (sc == NULL)
1169c58794deSPawel Jakub Dawidek 		return;
1170c58794deSPawel Jakub Dawidek 	if (pp != NULL || cp != NULL)
1171c58794deSPawel Jakub Dawidek 		return;	/* Nothing here. */
11721e09ff3dSPawel Jakub Dawidek 
11731e09ff3dSPawel Jakub Dawidek 	sbuf_printf(sb, "%s<KeysTotal>%ju</KeysTotal>", indent,
11741e09ff3dSPawel Jakub Dawidek 	    (uintmax_t)sc->sc_ekeys_total);
11751e09ff3dSPawel Jakub Dawidek 	sbuf_printf(sb, "%s<KeysAllocated>%ju</KeysAllocated>", indent,
11761e09ff3dSPawel Jakub Dawidek 	    (uintmax_t)sc->sc_ekeys_allocated);
1177ea35a2ecSPawel Jakub Dawidek 	sbuf_printf(sb, "%s<Flags>", indent);
1178ea35a2ecSPawel Jakub Dawidek 	if (sc->sc_flags == 0)
1179ea35a2ecSPawel Jakub Dawidek 		sbuf_printf(sb, "NONE");
1180ea35a2ecSPawel Jakub Dawidek 	else {
1181ea35a2ecSPawel Jakub Dawidek 		int first = 1;
1182ea35a2ecSPawel Jakub Dawidek 
1183ea35a2ecSPawel Jakub Dawidek #define ADD_FLAG(flag, name)	do {					\
1184eaa3b919SPawel Jakub Dawidek 	if (sc->sc_flags & (flag)) {					\
1185ea35a2ecSPawel Jakub Dawidek 		if (!first)						\
1186ea35a2ecSPawel Jakub Dawidek 			sbuf_printf(sb, ", ");				\
1187ea35a2ecSPawel Jakub Dawidek 		else							\
1188ea35a2ecSPawel Jakub Dawidek 			first = 0;					\
1189ea35a2ecSPawel Jakub Dawidek 		sbuf_printf(sb, name);					\
1190ea35a2ecSPawel Jakub Dawidek 	}								\
1191ea35a2ecSPawel Jakub Dawidek } while (0)
11925ad4a7c7SPawel Jakub Dawidek 		ADD_FLAG(G_ELI_FLAG_SUSPEND, "SUSPEND");
1193c6a26d4cSPawel Jakub Dawidek 		ADD_FLAG(G_ELI_FLAG_SINGLE_KEY, "SINGLE-KEY");
11942bd4ade6SPawel Jakub Dawidek 		ADD_FLAG(G_ELI_FLAG_NATIVE_BYTE_ORDER, "NATIVE-BYTE-ORDER");
1195ea35a2ecSPawel Jakub Dawidek 		ADD_FLAG(G_ELI_FLAG_ONETIME, "ONETIME");
1196ea35a2ecSPawel Jakub Dawidek 		ADD_FLAG(G_ELI_FLAG_BOOT, "BOOT");
1197ea35a2ecSPawel Jakub Dawidek 		ADD_FLAG(G_ELI_FLAG_WO_DETACH, "W-DETACH");
1198ea35a2ecSPawel Jakub Dawidek 		ADD_FLAG(G_ELI_FLAG_RW_DETACH, "RW-DETACH");
1199eaa3b919SPawel Jakub Dawidek 		ADD_FLAG(G_ELI_FLAG_AUTH, "AUTH");
1200ea35a2ecSPawel Jakub Dawidek 		ADD_FLAG(G_ELI_FLAG_WOPEN, "W-OPEN");
1201ea35a2ecSPawel Jakub Dawidek 		ADD_FLAG(G_ELI_FLAG_DESTROY, "DESTROY");
120285059016SPawel Jakub Dawidek 		ADD_FLAG(G_ELI_FLAG_RO, "READ-ONLY");
1203ea35a2ecSPawel Jakub Dawidek #undef  ADD_FLAG
1204ea35a2ecSPawel Jakub Dawidek 	}
1205ea35a2ecSPawel Jakub Dawidek 	sbuf_printf(sb, "</Flags>\n");
1206ea35a2ecSPawel Jakub Dawidek 
1207eaa3b919SPawel Jakub Dawidek 	if (!(sc->sc_flags & G_ELI_FLAG_ONETIME)) {
1208ea35a2ecSPawel Jakub Dawidek 		sbuf_printf(sb, "%s<UsedKey>%u</UsedKey>\n", indent,
1209ea35a2ecSPawel Jakub Dawidek 		    sc->sc_nkey);
1210ea35a2ecSPawel Jakub Dawidek 	}
1211c58794deSPawel Jakub Dawidek 	sbuf_printf(sb, "%s<Crypto>", indent);
1212c58794deSPawel Jakub Dawidek 	switch (sc->sc_crypto) {
1213c58794deSPawel Jakub Dawidek 	case G_ELI_CRYPTO_HW:
1214c58794deSPawel Jakub Dawidek 		sbuf_printf(sb, "hardware");
1215c58794deSPawel Jakub Dawidek 		break;
1216c58794deSPawel Jakub Dawidek 	case G_ELI_CRYPTO_SW:
1217c58794deSPawel Jakub Dawidek 		sbuf_printf(sb, "software");
1218c58794deSPawel Jakub Dawidek 		break;
1219c58794deSPawel Jakub Dawidek 	default:
1220c58794deSPawel Jakub Dawidek 		sbuf_printf(sb, "UNKNOWN");
1221c58794deSPawel Jakub Dawidek 		break;
1222c58794deSPawel Jakub Dawidek 	}
1223c58794deSPawel Jakub Dawidek 	sbuf_printf(sb, "</Crypto>\n");
1224eaa3b919SPawel Jakub Dawidek 	if (sc->sc_flags & G_ELI_FLAG_AUTH) {
1225eaa3b919SPawel Jakub Dawidek 		sbuf_printf(sb,
1226eaa3b919SPawel Jakub Dawidek 		    "%s<AuthenticationAlgorithm>%s</AuthenticationAlgorithm>\n",
1227eaa3b919SPawel Jakub Dawidek 		    indent, g_eli_algo2str(sc->sc_aalgo));
1228eaa3b919SPawel Jakub Dawidek 	}
1229eaa3b919SPawel Jakub Dawidek 	sbuf_printf(sb, "%s<KeyLength>%u</KeyLength>\n", indent,
1230eaa3b919SPawel Jakub Dawidek 	    sc->sc_ekeylen);
1231*038c55adSPawel Jakub Dawidek 	sbuf_printf(sb, "%s<EncryptionAlgorithm>%s</EncryptionAlgorithm>\n",
1232*038c55adSPawel Jakub Dawidek 	    indent, g_eli_algo2str(sc->sc_ealgo));
1233d8d61ef8SPawel Jakub Dawidek 	sbuf_printf(sb, "%s<State>%s</State>\n", indent,
1234d8d61ef8SPawel Jakub Dawidek 	    (sc->sc_flags & G_ELI_FLAG_SUSPEND) ? "SUSPENDED" : "ACTIVE");
1235c58794deSPawel Jakub Dawidek }
1236c58794deSPawel Jakub Dawidek 
1237c5d387d0SPawel Jakub Dawidek static void
1238c5d387d0SPawel Jakub Dawidek g_eli_shutdown_pre_sync(void *arg, int howto)
1239c5d387d0SPawel Jakub Dawidek {
1240c5d387d0SPawel Jakub Dawidek 	struct g_class *mp;
1241c5d387d0SPawel Jakub Dawidek 	struct g_geom *gp, *gp2;
1242c5d387d0SPawel Jakub Dawidek 	struct g_provider *pp;
1243c5d387d0SPawel Jakub Dawidek 	struct g_eli_softc *sc;
1244c5d387d0SPawel Jakub Dawidek 	int error;
1245c5d387d0SPawel Jakub Dawidek 
1246c5d387d0SPawel Jakub Dawidek 	mp = arg;
1247c5d387d0SPawel Jakub Dawidek 	DROP_GIANT();
1248c5d387d0SPawel Jakub Dawidek 	g_topology_lock();
1249c5d387d0SPawel Jakub Dawidek 	LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) {
1250c5d387d0SPawel Jakub Dawidek 		sc = gp->softc;
1251c5d387d0SPawel Jakub Dawidek 		if (sc == NULL)
1252c5d387d0SPawel Jakub Dawidek 			continue;
1253c5d387d0SPawel Jakub Dawidek 		pp = LIST_FIRST(&gp->provider);
1254c5d387d0SPawel Jakub Dawidek 		KASSERT(pp != NULL, ("No provider? gp=%p (%s)", gp, gp->name));
1255c5d387d0SPawel Jakub Dawidek 		if (pp->acr + pp->acw + pp->ace == 0)
12565ad4a7c7SPawel Jakub Dawidek 			error = g_eli_destroy(sc, TRUE);
1257c5d387d0SPawel Jakub Dawidek 		else {
1258c5d387d0SPawel Jakub Dawidek 			sc->sc_flags |= G_ELI_FLAG_RW_DETACH;
1259c5d387d0SPawel Jakub Dawidek 			gp->access = g_eli_access;
1260c5d387d0SPawel Jakub Dawidek 		}
1261c5d387d0SPawel Jakub Dawidek 	}
1262c5d387d0SPawel Jakub Dawidek 	g_topology_unlock();
1263c5d387d0SPawel Jakub Dawidek 	PICKUP_GIANT();
1264c5d387d0SPawel Jakub Dawidek }
1265c5d387d0SPawel Jakub Dawidek 
1266c5d387d0SPawel Jakub Dawidek static void
1267c5d387d0SPawel Jakub Dawidek g_eli_init(struct g_class *mp)
1268c5d387d0SPawel Jakub Dawidek {
1269c5d387d0SPawel Jakub Dawidek 
1270c5d387d0SPawel Jakub Dawidek 	g_eli_pre_sync = EVENTHANDLER_REGISTER(shutdown_pre_sync,
1271c5d387d0SPawel Jakub Dawidek 	    g_eli_shutdown_pre_sync, mp, SHUTDOWN_PRI_FIRST);
1272c5d387d0SPawel Jakub Dawidek 	if (g_eli_pre_sync == NULL)
1273c5d387d0SPawel Jakub Dawidek 		G_ELI_DEBUG(0, "Warning! Cannot register shutdown event.");
1274c5d387d0SPawel Jakub Dawidek }
1275c5d387d0SPawel Jakub Dawidek 
1276c5d387d0SPawel Jakub Dawidek static void
1277c5d387d0SPawel Jakub Dawidek g_eli_fini(struct g_class *mp)
1278c5d387d0SPawel Jakub Dawidek {
1279c5d387d0SPawel Jakub Dawidek 
1280c5d387d0SPawel Jakub Dawidek 	if (g_eli_pre_sync != NULL)
1281c5d387d0SPawel Jakub Dawidek 		EVENTHANDLER_DEREGISTER(shutdown_pre_sync, g_eli_pre_sync);
1282c5d387d0SPawel Jakub Dawidek }
1283c5d387d0SPawel Jakub Dawidek 
1284c58794deSPawel Jakub Dawidek DECLARE_GEOM_CLASS(g_eli_class, g_eli);
1285f6829a05SYaroslav Tykhiy MODULE_DEPEND(g_eli, crypto, 1, 1, 1);
1286