1eaa3b919SPawel Jakub Dawidek /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
33728855aSPedro F. Giffuni *
41e09ff3dSPawel Jakub Dawidek * Copyright (c) 2005-2011 Pawel Jakub Dawidek <pawel@dawidek.net>
5eaa3b919SPawel Jakub Dawidek * All rights reserved.
6eaa3b919SPawel Jakub Dawidek *
7eaa3b919SPawel Jakub Dawidek * Redistribution and use in source and binary forms, with or without
8eaa3b919SPawel Jakub Dawidek * modification, are permitted provided that the following conditions
9eaa3b919SPawel Jakub Dawidek * are met:
10eaa3b919SPawel Jakub Dawidek * 1. Redistributions of source code must retain the above copyright
11eaa3b919SPawel Jakub Dawidek * notice, this list of conditions and the following disclaimer.
12eaa3b919SPawel Jakub Dawidek * 2. Redistributions in binary form must reproduce the above copyright
13eaa3b919SPawel Jakub Dawidek * notice, this list of conditions and the following disclaimer in the
14eaa3b919SPawel Jakub Dawidek * documentation and/or other materials provided with the distribution.
15eaa3b919SPawel Jakub Dawidek *
16eaa3b919SPawel Jakub Dawidek * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17eaa3b919SPawel Jakub Dawidek * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18eaa3b919SPawel Jakub Dawidek * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19eaa3b919SPawel Jakub Dawidek * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20eaa3b919SPawel Jakub Dawidek * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21eaa3b919SPawel Jakub Dawidek * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22eaa3b919SPawel Jakub Dawidek * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23eaa3b919SPawel Jakub Dawidek * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24eaa3b919SPawel Jakub Dawidek * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25eaa3b919SPawel Jakub Dawidek * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26eaa3b919SPawel Jakub Dawidek * SUCH DAMAGE.
27eaa3b919SPawel Jakub Dawidek */
28eaa3b919SPawel Jakub Dawidek
29eaa3b919SPawel Jakub Dawidek #include <sys/param.h>
30eaa3b919SPawel Jakub Dawidek #include <sys/systm.h>
31eaa3b919SPawel Jakub Dawidek #include <sys/kernel.h>
32eaa3b919SPawel Jakub Dawidek #include <sys/linker.h>
33eaa3b919SPawel Jakub Dawidek #include <sys/module.h>
34eaa3b919SPawel Jakub Dawidek #include <sys/lock.h>
35eaa3b919SPawel Jakub Dawidek #include <sys/mutex.h>
36eaa3b919SPawel Jakub Dawidek #include <sys/bio.h>
37eaa3b919SPawel Jakub Dawidek #include <sys/sysctl.h>
38eaa3b919SPawel Jakub Dawidek #include <sys/kthread.h>
39eaa3b919SPawel Jakub Dawidek #include <sys/proc.h>
40eaa3b919SPawel Jakub Dawidek #include <sys/sched.h>
41eaa3b919SPawel Jakub Dawidek #include <sys/smp.h>
42eaa3b919SPawel Jakub Dawidek #include <sys/vnode.h>
43eaa3b919SPawel Jakub Dawidek
44eaa3b919SPawel Jakub Dawidek #include <vm/uma.h>
45eaa3b919SPawel Jakub Dawidek
46eaa3b919SPawel Jakub Dawidek #include <geom/geom.h>
47ac03832eSConrad Meyer #include <geom/geom_dbg.h>
48eaa3b919SPawel Jakub Dawidek #include <geom/eli/g_eli.h>
49eaa3b919SPawel Jakub Dawidek #include <geom/eli/pkcs5v2.h>
50eaa3b919SPawel Jakub Dawidek
51eaa3b919SPawel Jakub Dawidek /*
52eaa3b919SPawel Jakub Dawidek * Code paths:
53eaa3b919SPawel Jakub Dawidek * BIO_READ:
545ad4a7c7SPawel 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
55eaa3b919SPawel Jakub Dawidek * BIO_WRITE:
56eaa3b919SPawel Jakub Dawidek * g_eli_start -> g_eli_crypto_run -> g_eli_crypto_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver
57eaa3b919SPawel Jakub Dawidek */
58eaa3b919SPawel Jakub Dawidek
59eaa3b919SPawel Jakub Dawidek /*
607d874f0fSAlan Somers * Copy data from a (potentially unmapped) bio to a kernelspace buffer.
617d874f0fSAlan Somers *
627d874f0fSAlan Somers * The buffer must have at least as much room as bp->bio_length.
637d874f0fSAlan Somers */
647d874f0fSAlan Somers static void
g_eli_bio_copyin(struct bio * bp,void * kaddr)657d874f0fSAlan Somers g_eli_bio_copyin(struct bio *bp, void *kaddr)
667d874f0fSAlan Somers {
677d874f0fSAlan Somers struct uio uio;
687d874f0fSAlan Somers struct iovec iov[1];
697d874f0fSAlan Somers
707d874f0fSAlan Somers iov[0].iov_base = kaddr;
717d874f0fSAlan Somers iov[0].iov_len = bp->bio_length;
727d874f0fSAlan Somers uio.uio_iov = iov;
737d874f0fSAlan Somers uio.uio_iovcnt = 1;
747d874f0fSAlan Somers uio.uio_offset = 0;
757d874f0fSAlan Somers uio.uio_resid = bp->bio_length;
767d874f0fSAlan Somers uio.uio_segflg = UIO_SYSSPACE;
777d874f0fSAlan Somers uio.uio_rw = UIO_READ;
787d874f0fSAlan Somers uiomove_fromphys(bp->bio_ma, bp->bio_ma_offset, bp->bio_length, &uio);
797d874f0fSAlan Somers }
807d874f0fSAlan Somers
817d874f0fSAlan Somers /*
82eaa3b919SPawel Jakub Dawidek * The function is called after we read and decrypt data.
83eaa3b919SPawel Jakub Dawidek *
845ad4a7c7SPawel 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
85eaa3b919SPawel Jakub Dawidek */
86eaa3b919SPawel Jakub Dawidek static int
g_eli_crypto_read_done(struct cryptop * crp)87eaa3b919SPawel Jakub Dawidek g_eli_crypto_read_done(struct cryptop *crp)
88eaa3b919SPawel Jakub Dawidek {
895ad4a7c7SPawel Jakub Dawidek struct g_eli_softc *sc;
90eaa3b919SPawel Jakub Dawidek struct bio *bp;
91eaa3b919SPawel Jakub Dawidek
92eaa3b919SPawel Jakub Dawidek if (crp->crp_etype == EAGAIN) {
93eaa3b919SPawel Jakub Dawidek if (g_eli_crypto_rerun(crp) == 0)
94eaa3b919SPawel Jakub Dawidek return (0);
95eaa3b919SPawel Jakub Dawidek }
96eaa3b919SPawel Jakub Dawidek bp = (struct bio *)crp->crp_opaque;
97eaa3b919SPawel Jakub Dawidek bp->bio_inbed++;
98eaa3b919SPawel Jakub Dawidek if (crp->crp_etype == 0) {
99eaa3b919SPawel Jakub Dawidek G_ELI_DEBUG(3, "Crypto READ request done (%d/%d).",
100eaa3b919SPawel Jakub Dawidek bp->bio_inbed, bp->bio_children);
1019c0e3d3aSJohn Baldwin bp->bio_completed += crp->crp_payload_length;
102eaa3b919SPawel Jakub Dawidek } else {
103eaa3b919SPawel Jakub Dawidek G_ELI_DEBUG(1, "Crypto READ request failed (%d/%d) error=%d.",
104eaa3b919SPawel Jakub Dawidek bp->bio_inbed, bp->bio_children, crp->crp_etype);
105eaa3b919SPawel Jakub Dawidek if (bp->bio_error == 0)
106eaa3b919SPawel Jakub Dawidek bp->bio_error = crp->crp_etype;
107eaa3b919SPawel Jakub Dawidek }
1081e09ff3dSPawel Jakub Dawidek sc = bp->bio_to->geom->softc;
109c0341432SJohn Baldwin if (sc != NULL && crp->crp_cipher_key != NULL)
110c0341432SJohn Baldwin g_eli_key_drop(sc, __DECONST(void *, crp->crp_cipher_key));
111c0341432SJohn Baldwin crypto_freereq(crp);
112eaa3b919SPawel Jakub Dawidek /*
113eaa3b919SPawel Jakub Dawidek * Do we have all sectors already?
114eaa3b919SPawel Jakub Dawidek */
115eaa3b919SPawel Jakub Dawidek if (bp->bio_inbed < bp->bio_children)
116eaa3b919SPawel Jakub Dawidek return (0);
1177d874f0fSAlan Somers
118eaa3b919SPawel Jakub Dawidek if (bp->bio_error != 0) {
119eaa3b919SPawel Jakub Dawidek G_ELI_LOGREQ(0, bp, "Crypto READ request failed (error=%d).",
120eaa3b919SPawel Jakub Dawidek bp->bio_error);
121eaa3b919SPawel Jakub Dawidek bp->bio_completed = 0;
122eaa3b919SPawel Jakub Dawidek }
123eaa3b919SPawel Jakub Dawidek /*
124eaa3b919SPawel Jakub Dawidek * Read is finished, send it up.
125eaa3b919SPawel Jakub Dawidek */
126eaa3b919SPawel Jakub Dawidek g_io_deliver(bp, bp->bio_error);
12778f79a9aSMariusz Zaborski if (sc != NULL)
1285ad4a7c7SPawel Jakub Dawidek atomic_subtract_int(&sc->sc_inflight, 1);
129eaa3b919SPawel Jakub Dawidek return (0);
130eaa3b919SPawel Jakub Dawidek }
131eaa3b919SPawel Jakub Dawidek
132eaa3b919SPawel Jakub Dawidek /*
133eaa3b919SPawel Jakub Dawidek * The function is called after data encryption.
134eaa3b919SPawel Jakub Dawidek *
135eaa3b919SPawel Jakub Dawidek * g_eli_start -> g_eli_crypto_run -> G_ELI_CRYPTO_WRITE_DONE -> g_io_request -> g_eli_write_done -> g_io_deliver
136eaa3b919SPawel Jakub Dawidek */
137eaa3b919SPawel Jakub Dawidek static int
g_eli_crypto_write_done(struct cryptop * crp)138eaa3b919SPawel Jakub Dawidek g_eli_crypto_write_done(struct cryptop *crp)
139eaa3b919SPawel Jakub Dawidek {
1405ad4a7c7SPawel Jakub Dawidek struct g_eli_softc *sc;
141eaa3b919SPawel Jakub Dawidek struct g_geom *gp;
142eaa3b919SPawel Jakub Dawidek struct g_consumer *cp;
143eaa3b919SPawel Jakub Dawidek struct bio *bp, *cbp;
144eaa3b919SPawel Jakub Dawidek
145eaa3b919SPawel Jakub Dawidek if (crp->crp_etype == EAGAIN) {
146eaa3b919SPawel Jakub Dawidek if (g_eli_crypto_rerun(crp) == 0)
147eaa3b919SPawel Jakub Dawidek return (0);
148eaa3b919SPawel Jakub Dawidek }
149eaa3b919SPawel Jakub Dawidek bp = (struct bio *)crp->crp_opaque;
150eaa3b919SPawel Jakub Dawidek bp->bio_inbed++;
151eaa3b919SPawel Jakub Dawidek if (crp->crp_etype == 0) {
152eaa3b919SPawel Jakub Dawidek G_ELI_DEBUG(3, "Crypto WRITE request done (%d/%d).",
153eaa3b919SPawel Jakub Dawidek bp->bio_inbed, bp->bio_children);
154eaa3b919SPawel Jakub Dawidek } else {
155eaa3b919SPawel Jakub Dawidek G_ELI_DEBUG(1, "Crypto WRITE request failed (%d/%d) error=%d.",
156eaa3b919SPawel Jakub Dawidek bp->bio_inbed, bp->bio_children, crp->crp_etype);
157eaa3b919SPawel Jakub Dawidek if (bp->bio_error == 0)
158eaa3b919SPawel Jakub Dawidek bp->bio_error = crp->crp_etype;
159eaa3b919SPawel Jakub Dawidek }
1601e09ff3dSPawel Jakub Dawidek gp = bp->bio_to->geom;
1611e09ff3dSPawel Jakub Dawidek sc = gp->softc;
162c0341432SJohn Baldwin if (crp->crp_cipher_key != NULL)
163c0341432SJohn Baldwin g_eli_key_drop(sc, __DECONST(void *, crp->crp_cipher_key));
164c0341432SJohn Baldwin crypto_freereq(crp);
165eaa3b919SPawel Jakub Dawidek /*
166eaa3b919SPawel Jakub Dawidek * All sectors are already encrypted?
167eaa3b919SPawel Jakub Dawidek */
168eaa3b919SPawel Jakub Dawidek if (bp->bio_inbed < bp->bio_children)
169eaa3b919SPawel Jakub Dawidek return (0);
170eaa3b919SPawel Jakub Dawidek bp->bio_inbed = 0;
171eaa3b919SPawel Jakub Dawidek bp->bio_children = 1;
172eaa3b919SPawel Jakub Dawidek cbp = bp->bio_driver1;
173eaa3b919SPawel Jakub Dawidek bp->bio_driver1 = NULL;
174eaa3b919SPawel Jakub Dawidek if (bp->bio_error != 0) {
175eaa3b919SPawel Jakub Dawidek G_ELI_LOGREQ(0, bp, "Crypto WRITE request failed (error=%d).",
176eaa3b919SPawel Jakub Dawidek bp->bio_error);
1772dbc9a38SGleb Smirnoff g_eli_free_data(bp);
178eaa3b919SPawel Jakub Dawidek g_destroy_bio(cbp);
179eaa3b919SPawel Jakub Dawidek g_io_deliver(bp, bp->bio_error);
1805ad4a7c7SPawel Jakub Dawidek atomic_subtract_int(&sc->sc_inflight, 1);
181eaa3b919SPawel Jakub Dawidek return (0);
182eaa3b919SPawel Jakub Dawidek }
183eaa3b919SPawel Jakub Dawidek cbp->bio_data = bp->bio_driver2;
1847d874f0fSAlan Somers /*
1857d874f0fSAlan Somers * Clear BIO_UNMAPPED, which was inherited from where we cloned the bio
1867d874f0fSAlan Somers * in g_eli_start, because we manually set bio_data
1877d874f0fSAlan Somers */
1887d874f0fSAlan Somers cbp->bio_flags &= ~BIO_UNMAPPED;
189eaa3b919SPawel Jakub Dawidek cbp->bio_done = g_eli_write_done;
190eaa3b919SPawel Jakub Dawidek cp = LIST_FIRST(&gp->consumer);
191eaa3b919SPawel Jakub Dawidek cbp->bio_to = cp->provider;
192eaa3b919SPawel Jakub Dawidek G_ELI_LOGREQ(2, cbp, "Sending request.");
193eaa3b919SPawel Jakub Dawidek /*
194eaa3b919SPawel Jakub Dawidek * Send encrypted data to the provider.
195eaa3b919SPawel Jakub Dawidek */
196eaa3b919SPawel Jakub Dawidek g_io_request(cbp, cp);
197eaa3b919SPawel Jakub Dawidek return (0);
198eaa3b919SPawel Jakub Dawidek }
199eaa3b919SPawel Jakub Dawidek
200eaa3b919SPawel Jakub Dawidek /*
2015ad4a7c7SPawel Jakub Dawidek * The function is called to read encrypted data.
2025ad4a7c7SPawel Jakub Dawidek *
2035ad4a7c7SPawel 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
2045ad4a7c7SPawel Jakub Dawidek */
2055ad4a7c7SPawel Jakub Dawidek void
g_eli_crypto_read(struct g_eli_softc * sc,struct bio * bp,boolean_t fromworker)2065ad4a7c7SPawel Jakub Dawidek g_eli_crypto_read(struct g_eli_softc *sc, struct bio *bp, boolean_t fromworker)
2075ad4a7c7SPawel Jakub Dawidek {
2085ad4a7c7SPawel Jakub Dawidek struct g_consumer *cp;
2095ad4a7c7SPawel Jakub Dawidek struct bio *cbp;
2105ad4a7c7SPawel Jakub Dawidek
2115ad4a7c7SPawel Jakub Dawidek if (!fromworker) {
2125ad4a7c7SPawel Jakub Dawidek /*
2135ad4a7c7SPawel Jakub Dawidek * We are not called from the worker thread, so check if
2145ad4a7c7SPawel Jakub Dawidek * device is suspended.
2155ad4a7c7SPawel Jakub Dawidek */
2165ad4a7c7SPawel Jakub Dawidek mtx_lock(&sc->sc_queue_mtx);
2175ad4a7c7SPawel Jakub Dawidek if (sc->sc_flags & G_ELI_FLAG_SUSPEND) {
2185ad4a7c7SPawel Jakub Dawidek /*
2195ad4a7c7SPawel Jakub Dawidek * If device is suspended, we place the request onto
2205ad4a7c7SPawel Jakub Dawidek * the queue, so it can be handled after resume.
2215ad4a7c7SPawel Jakub Dawidek */
2225ad4a7c7SPawel Jakub Dawidek G_ELI_DEBUG(0, "device suspended, move onto queue");
2235ad4a7c7SPawel Jakub Dawidek bioq_insert_tail(&sc->sc_queue, bp);
2245ad4a7c7SPawel Jakub Dawidek mtx_unlock(&sc->sc_queue_mtx);
2255ad4a7c7SPawel Jakub Dawidek wakeup(sc);
2265ad4a7c7SPawel Jakub Dawidek return;
2275ad4a7c7SPawel Jakub Dawidek }
2285ad4a7c7SPawel Jakub Dawidek atomic_add_int(&sc->sc_inflight, 1);
2295ad4a7c7SPawel Jakub Dawidek mtx_unlock(&sc->sc_queue_mtx);
2305ad4a7c7SPawel Jakub Dawidek }
2312dbc9a38SGleb Smirnoff G_ELI_SETWORKER(bp->bio_pflags, 0);
2325ad4a7c7SPawel Jakub Dawidek bp->bio_driver2 = NULL;
2335ad4a7c7SPawel Jakub Dawidek cbp = bp->bio_driver1;
2345ad4a7c7SPawel Jakub Dawidek cbp->bio_done = g_eli_read_done;
2355ad4a7c7SPawel Jakub Dawidek cp = LIST_FIRST(&sc->sc_geom->consumer);
2365ad4a7c7SPawel Jakub Dawidek cbp->bio_to = cp->provider;
2375ad4a7c7SPawel Jakub Dawidek G_ELI_LOGREQ(2, cbp, "Sending request.");
2385ad4a7c7SPawel Jakub Dawidek /*
2395ad4a7c7SPawel Jakub Dawidek * Read encrypted data from provider.
2405ad4a7c7SPawel Jakub Dawidek */
2415ad4a7c7SPawel Jakub Dawidek g_io_request(cbp, cp);
2425ad4a7c7SPawel Jakub Dawidek }
2435ad4a7c7SPawel Jakub Dawidek
2445ad4a7c7SPawel Jakub Dawidek /*
245eaa3b919SPawel Jakub Dawidek * This is the main function responsible for cryptography (ie. communication
246eaa3b919SPawel Jakub Dawidek * with crypto(9) subsystem).
247056638c4SPawel Jakub Dawidek *
248056638c4SPawel Jakub Dawidek * BIO_READ:
2495ad4a7c7SPawel 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
250056638c4SPawel Jakub Dawidek * BIO_WRITE:
251056638c4SPawel Jakub Dawidek * g_eli_start -> G_ELI_CRYPTO_RUN -> g_eli_crypto_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver
252eaa3b919SPawel Jakub Dawidek */
253eaa3b919SPawel Jakub Dawidek void
g_eli_crypto_run(struct g_eli_worker * wr,struct bio * bp)254eaa3b919SPawel Jakub Dawidek g_eli_crypto_run(struct g_eli_worker *wr, struct bio *bp)
255eaa3b919SPawel Jakub Dawidek {
256eaa3b919SPawel Jakub Dawidek struct g_eli_softc *sc;
25768f6800cSMark Johnston struct cryptopq crpq;
258eaa3b919SPawel Jakub Dawidek struct cryptop *crp;
2597d874f0fSAlan Somers vm_page_t *pages;
2601f0fb66fSPawel Jakub Dawidek u_int i, nsec, secsize;
2611f0fb66fSPawel Jakub Dawidek off_t dstoff;
2627d874f0fSAlan Somers u_char *data = NULL;
263c9048120SMateusz Guzik int error __diagused, pages_offset;
26468f6800cSMark Johnston bool batch;
265eaa3b919SPawel Jakub Dawidek
266eaa3b919SPawel Jakub Dawidek G_ELI_LOGREQ(3, bp, "%s", __func__);
267eaa3b919SPawel Jakub Dawidek
2682dbc9a38SGleb Smirnoff G_ELI_SETWORKER(bp->bio_pflags, wr->w_number);
269eaa3b919SPawel Jakub Dawidek sc = wr->w_softc;
270eaa3b919SPawel Jakub Dawidek secsize = LIST_FIRST(&sc->sc_geom->provider)->sectorsize;
271eaa3b919SPawel Jakub Dawidek nsec = bp->bio_length / secsize;
272eaa3b919SPawel Jakub Dawidek
273c0341432SJohn Baldwin bp->bio_inbed = 0;
274c0341432SJohn Baldwin bp->bio_children = nsec;
275c0341432SJohn Baldwin
276eaa3b919SPawel Jakub Dawidek /*
277eaa3b919SPawel Jakub Dawidek * If we write the data we cannot destroy current bio_data content,
278eaa3b919SPawel Jakub Dawidek * so we need to allocate more memory for encrypted data.
279eaa3b919SPawel Jakub Dawidek */
280c0341432SJohn Baldwin if (bp->bio_cmd == BIO_WRITE) {
2812dbc9a38SGleb Smirnoff if (!g_eli_alloc_data(bp, bp->bio_length)) {
2822dbc9a38SGleb Smirnoff G_ELI_LOGREQ(0, bp, "Crypto request failed (ENOMEM).");
2832dbc9a38SGleb Smirnoff if (bp->bio_driver1 != NULL) {
2842dbc9a38SGleb Smirnoff g_destroy_bio(bp->bio_driver1);
2852dbc9a38SGleb Smirnoff bp->bio_driver1 = NULL;
2862dbc9a38SGleb Smirnoff }
2872dbc9a38SGleb Smirnoff bp->bio_error = ENOMEM;
2882dbc9a38SGleb Smirnoff g_io_deliver(bp, bp->bio_error);
2892dbc9a38SGleb Smirnoff if (sc != NULL)
2902dbc9a38SGleb Smirnoff atomic_subtract_int(&sc->sc_inflight, 1);
2912dbc9a38SGleb Smirnoff return;
2922dbc9a38SGleb Smirnoff }
2932dbc9a38SGleb Smirnoff data = bp->bio_driver2;
2947d874f0fSAlan Somers /*
2957d874f0fSAlan Somers * This copy could be eliminated by using crypto's output
2967d874f0fSAlan Somers * buffer, instead of using a single overwriting buffer.
2977d874f0fSAlan Somers */
2987d874f0fSAlan Somers if ((bp->bio_flags & BIO_UNMAPPED) != 0)
2997d874f0fSAlan Somers g_eli_bio_copyin(bp, data);
3007d874f0fSAlan Somers else
301eaa3b919SPawel Jakub Dawidek bcopy(bp->bio_data, data, bp->bio_length);
3027d874f0fSAlan Somers } else {
3037d874f0fSAlan Somers if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
3047d874f0fSAlan Somers pages = bp->bio_ma;
3057d874f0fSAlan Somers pages_offset = bp->bio_ma_offset;
3067d874f0fSAlan Somers } else {
307c0341432SJohn Baldwin data = bp->bio_data;
3087d874f0fSAlan Somers }
3097d874f0fSAlan Somers }
310eaa3b919SPawel Jakub Dawidek
31168f6800cSMark Johnston TAILQ_INIT(&crpq);
31268f6800cSMark Johnston batch = atomic_load_int(&g_eli_batch) != 0;
31368f6800cSMark Johnston
3141f0fb66fSPawel Jakub Dawidek for (i = 0, dstoff = bp->bio_offset; i < nsec; i++, dstoff += secsize) {
315c0341432SJohn Baldwin crp = crypto_getreq(wr->w_sid, M_WAITOK);
316eaa3b919SPawel Jakub Dawidek
3177d874f0fSAlan Somers if (data) {
3189c0e3d3aSJohn Baldwin crypto_use_buf(crp, data, secsize);
31989fac384SJohn-Mark Gurney data += secsize;
3207d874f0fSAlan Somers } else {
3217d874f0fSAlan Somers MPASS(pages != NULL);
3227d874f0fSAlan Somers crypto_use_vmpage(crp, pages, secsize, pages_offset);
3237d874f0fSAlan Somers pages_offset += secsize;
3247d874f0fSAlan Somers pages += pages_offset >> PAGE_SHIFT;
3257d874f0fSAlan Somers pages_offset &= PAGE_MASK;
3267d874f0fSAlan Somers }
3277d874f0fSAlan Somers crp->crp_opaque = (void *)bp;
328c0341432SJohn Baldwin if (bp->bio_cmd == BIO_WRITE) {
329c0341432SJohn Baldwin crp->crp_op = CRYPTO_OP_ENCRYPT;
330eaa3b919SPawel Jakub Dawidek crp->crp_callback = g_eli_crypto_write_done;
331c0341432SJohn Baldwin } else /* if (bp->bio_cmd == BIO_READ) */ {
332c0341432SJohn Baldwin crp->crp_op = CRYPTO_OP_DECRYPT;
333eaa3b919SPawel Jakub Dawidek crp->crp_callback = g_eli_crypto_read_done;
334c0341432SJohn Baldwin }
33508fca7a5SJohn-Mark Gurney crp->crp_flags = CRYPTO_F_CBIFSYNC;
336c0341432SJohn Baldwin crp->crp_payload_start = 0;
337c0341432SJohn Baldwin crp->crp_payload_length = secsize;
338c0341432SJohn Baldwin if ((sc->sc_flags & G_ELI_FLAG_SINGLE_KEY) == 0) {
339c0341432SJohn Baldwin crp->crp_cipher_key = g_eli_key_hold(sc, dstoff,
340c0341432SJohn Baldwin secsize);
341c0341432SJohn Baldwin }
342aafaa8b7SAlan Somers if (g_eli_ivlen(sc->sc_ealgo) != 0) {
343aafaa8b7SAlan Somers crp->crp_flags |= CRYPTO_F_IV_SEPARATE;
344c0341432SJohn Baldwin g_eli_crypto_ivgen(sc, dstoff, crp->crp_iv,
345c0341432SJohn Baldwin sizeof(crp->crp_iv));
346aafaa8b7SAlan Somers }
347eaa3b919SPawel Jakub Dawidek
34868f6800cSMark Johnston if (batch) {
34968f6800cSMark Johnston TAILQ_INSERT_TAIL(&crpq, crp, crp_next);
35068f6800cSMark Johnston } else {
3515ee9ea19SPawel Jakub Dawidek error = crypto_dispatch(crp);
35268f6800cSMark Johnston KASSERT(error == 0,
35368f6800cSMark Johnston ("crypto_dispatch() failed (error=%d)", error));
354eaa3b919SPawel Jakub Dawidek }
355eaa3b919SPawel Jakub Dawidek }
35668f6800cSMark Johnston
35768f6800cSMark Johnston if (batch)
35868f6800cSMark Johnston crypto_dispatch_batch(&crpq, 0);
35968f6800cSMark Johnston }
360