cgd.c revision 1.81 1 1.81 martin /* $NetBSD: cgd.c,v 1.81 2013/05/30 08:28:13 martin Exp $ */
2 1.1 elric
3 1.1 elric /*-
4 1.1 elric * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 1.1 elric * All rights reserved.
6 1.1 elric *
7 1.1 elric * This code is derived from software contributed to The NetBSD Foundation
8 1.1 elric * by Roland C. Dowdeswell.
9 1.1 elric *
10 1.1 elric * Redistribution and use in source and binary forms, with or without
11 1.1 elric * modification, are permitted provided that the following conditions
12 1.1 elric * are met:
13 1.1 elric * 1. Redistributions of source code must retain the above copyright
14 1.1 elric * notice, this list of conditions and the following disclaimer.
15 1.1 elric * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 elric * notice, this list of conditions and the following disclaimer in the
17 1.1 elric * documentation and/or other materials provided with the distribution.
18 1.1 elric *
19 1.1 elric * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 elric * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 elric * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 elric * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 elric * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 elric * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 elric * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 elric * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 elric * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 elric * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 elric * POSSIBILITY OF SUCH DAMAGE.
30 1.1 elric */
31 1.1 elric
32 1.1 elric #include <sys/cdefs.h>
33 1.81 martin __KERNEL_RCSID(0, "$NetBSD: cgd.c,v 1.81 2013/05/30 08:28:13 martin Exp $");
34 1.1 elric
35 1.1 elric #include <sys/types.h>
36 1.1 elric #include <sys/param.h>
37 1.1 elric #include <sys/systm.h>
38 1.1 elric #include <sys/proc.h>
39 1.1 elric #include <sys/errno.h>
40 1.1 elric #include <sys/buf.h>
41 1.21 yamt #include <sys/bufq.h>
42 1.1 elric #include <sys/malloc.h>
43 1.74 jruoho #include <sys/module.h>
44 1.1 elric #include <sys/pool.h>
45 1.1 elric #include <sys/ioctl.h>
46 1.1 elric #include <sys/device.h>
47 1.1 elric #include <sys/disk.h>
48 1.1 elric #include <sys/disklabel.h>
49 1.1 elric #include <sys/fcntl.h>
50 1.71 dholland #include <sys/namei.h> /* for pathbuf */
51 1.1 elric #include <sys/vnode.h>
52 1.1 elric #include <sys/conf.h>
53 1.62 christos #include <sys/syslog.h>
54 1.1 elric
55 1.1 elric #include <dev/dkvar.h>
56 1.1 elric #include <dev/cgdvar.h>
57 1.1 elric
58 1.1 elric /* Entry Point Functions */
59 1.1 elric
60 1.1 elric void cgdattach(int);
61 1.1 elric
62 1.18 thorpej static dev_type_open(cgdopen);
63 1.18 thorpej static dev_type_close(cgdclose);
64 1.18 thorpej static dev_type_read(cgdread);
65 1.18 thorpej static dev_type_write(cgdwrite);
66 1.18 thorpej static dev_type_ioctl(cgdioctl);
67 1.18 thorpej static dev_type_strategy(cgdstrategy);
68 1.18 thorpej static dev_type_dump(cgddump);
69 1.18 thorpej static dev_type_size(cgdsize);
70 1.1 elric
71 1.1 elric const struct bdevsw cgd_bdevsw = {
72 1.1 elric cgdopen, cgdclose, cgdstrategy, cgdioctl,
73 1.1 elric cgddump, cgdsize, D_DISK
74 1.1 elric };
75 1.1 elric
76 1.1 elric const struct cdevsw cgd_cdevsw = {
77 1.1 elric cgdopen, cgdclose, cgdread, cgdwrite, cgdioctl,
78 1.4 jdolecek nostop, notty, nopoll, nommap, nokqfilter, D_DISK
79 1.1 elric };
80 1.1 elric
81 1.65 dyoung static int cgd_match(device_t, cfdata_t, void *);
82 1.65 dyoung static void cgd_attach(device_t, device_t, void *);
83 1.65 dyoung static int cgd_detach(device_t, int);
84 1.65 dyoung static struct cgd_softc *cgd_spawn(int);
85 1.65 dyoung static int cgd_destroy(device_t);
86 1.65 dyoung
87 1.1 elric /* Internal Functions */
88 1.1 elric
89 1.16 elric static int cgdstart(struct dk_softc *, struct buf *);
90 1.1 elric static void cgdiodone(struct buf *);
91 1.1 elric
92 1.32 christos static int cgd_ioctl_set(struct cgd_softc *, void *, struct lwp *);
93 1.65 dyoung static int cgd_ioctl_clr(struct cgd_softc *, struct lwp *);
94 1.78 christos static int cgd_ioctl_get(dev_t, void *, struct lwp *);
95 1.27 drochner static int cgdinit(struct cgd_softc *, const char *, struct vnode *,
96 1.32 christos struct lwp *);
97 1.44 christos static void cgd_cipher(struct cgd_softc *, void *, void *,
98 1.1 elric size_t, daddr_t, size_t, int);
99 1.1 elric
100 1.1 elric /* Pseudo-disk Interface */
101 1.1 elric
102 1.1 elric static struct dk_intf the_dkintf = {
103 1.1 elric DTYPE_CGD,
104 1.1 elric "cgd",
105 1.1 elric cgdopen,
106 1.1 elric cgdclose,
107 1.1 elric cgdstrategy,
108 1.1 elric cgdstart,
109 1.1 elric };
110 1.1 elric static struct dk_intf *di = &the_dkintf;
111 1.1 elric
112 1.29 yamt static struct dkdriver cgddkdriver = {
113 1.29 yamt .d_strategy = cgdstrategy,
114 1.29 yamt .d_minphys = minphys,
115 1.29 yamt };
116 1.29 yamt
117 1.65 dyoung CFATTACH_DECL3_NEW(cgd, sizeof(struct cgd_softc),
118 1.65 dyoung cgd_match, cgd_attach, cgd_detach, NULL, NULL, NULL, DVF_DETACH_SHUTDOWN);
119 1.65 dyoung extern struct cfdriver cgd_cd;
120 1.65 dyoung
121 1.1 elric /* DIAGNOSTIC and DEBUG definitions */
122 1.1 elric
123 1.1 elric #if defined(CGDDEBUG) && !defined(DEBUG)
124 1.1 elric #define DEBUG
125 1.1 elric #endif
126 1.1 elric
127 1.1 elric #ifdef DEBUG
128 1.1 elric int cgddebug = 0;
129 1.1 elric
130 1.1 elric #define CGDB_FOLLOW 0x1
131 1.1 elric #define CGDB_IO 0x2
132 1.1 elric #define CGDB_CRYPTO 0x4
133 1.1 elric
134 1.1 elric #define IFDEBUG(x,y) if (cgddebug & (x)) y
135 1.1 elric #define DPRINTF(x,y) IFDEBUG(x, printf y)
136 1.1 elric #define DPRINTF_FOLLOW(y) DPRINTF(CGDB_FOLLOW, y)
137 1.1 elric
138 1.26 drochner static void hexprint(const char *, void *, int);
139 1.1 elric
140 1.1 elric #else
141 1.1 elric #define IFDEBUG(x,y)
142 1.1 elric #define DPRINTF(x,y)
143 1.1 elric #define DPRINTF_FOLLOW(y)
144 1.1 elric #endif
145 1.1 elric
146 1.1 elric #ifdef DIAGNOSTIC
147 1.22 perry #define DIAGPANIC(x) panic x
148 1.1 elric #define DIAGCONDPANIC(x,y) if (x) panic y
149 1.1 elric #else
150 1.1 elric #define DIAGPANIC(x)
151 1.1 elric #define DIAGCONDPANIC(x,y)
152 1.1 elric #endif
153 1.1 elric
154 1.1 elric /* Global variables */
155 1.1 elric
156 1.1 elric /* Utility Functions */
157 1.1 elric
158 1.1 elric #define CGDUNIT(x) DISKUNIT(x)
159 1.1 elric #define GETCGD_SOFTC(_cs, x) if (!((_cs) = getcgd_softc(x))) return ENXIO
160 1.1 elric
161 1.65 dyoung /* The code */
162 1.65 dyoung
163 1.1 elric static struct cgd_softc *
164 1.1 elric getcgd_softc(dev_t dev)
165 1.1 elric {
166 1.1 elric int unit = CGDUNIT(dev);
167 1.65 dyoung struct cgd_softc *sc;
168 1.1 elric
169 1.56 cegger DPRINTF_FOLLOW(("getcgd_softc(0x%"PRIx64"): unit = %d\n", dev, unit));
170 1.65 dyoung
171 1.65 dyoung sc = device_lookup_private(&cgd_cd, unit);
172 1.65 dyoung if (sc == NULL)
173 1.65 dyoung sc = cgd_spawn(unit);
174 1.65 dyoung return sc;
175 1.1 elric }
176 1.1 elric
177 1.65 dyoung static int
178 1.65 dyoung cgd_match(device_t self, cfdata_t cfdata, void *aux)
179 1.65 dyoung {
180 1.65 dyoung
181 1.65 dyoung return 1;
182 1.65 dyoung }
183 1.1 elric
184 1.1 elric static void
185 1.65 dyoung cgd_attach(device_t parent, device_t self, void *aux)
186 1.1 elric {
187 1.65 dyoung struct cgd_softc *sc = device_private(self);
188 1.1 elric
189 1.65 dyoung simple_lock_init(&sc->sc_slock);
190 1.77 elric dk_sc_init(&sc->sc_dksc, device_xname(self));
191 1.77 elric sc->sc_dksc.sc_dev = self;
192 1.65 dyoung disk_init(&sc->sc_dksc.sc_dkdev, sc->sc_dksc.sc_xname, &cgddkdriver);
193 1.70 joerg
194 1.70 joerg if (!pmf_device_register(self, NULL, NULL))
195 1.70 joerg aprint_error_dev(self, "unable to register power management hooks\n");
196 1.65 dyoung }
197 1.65 dyoung
198 1.65 dyoung
199 1.65 dyoung static int
200 1.65 dyoung cgd_detach(device_t self, int flags)
201 1.65 dyoung {
202 1.67 dyoung int ret;
203 1.67 dyoung const int pmask = 1 << RAW_PART;
204 1.65 dyoung struct cgd_softc *sc = device_private(self);
205 1.67 dyoung struct dk_softc *dksc = &sc->sc_dksc;
206 1.67 dyoung
207 1.67 dyoung if (DK_BUSY(dksc, pmask))
208 1.67 dyoung return EBUSY;
209 1.65 dyoung
210 1.67 dyoung if ((dksc->sc_flags & DKF_INITED) != 0 &&
211 1.67 dyoung (ret = cgd_ioctl_clr(sc, curlwp)) != 0)
212 1.67 dyoung return ret;
213 1.65 dyoung
214 1.67 dyoung disk_destroy(&dksc->sc_dkdev);
215 1.65 dyoung
216 1.67 dyoung return 0;
217 1.1 elric }
218 1.1 elric
219 1.1 elric void
220 1.1 elric cgdattach(int num)
221 1.1 elric {
222 1.65 dyoung int error;
223 1.65 dyoung
224 1.65 dyoung error = config_cfattach_attach(cgd_cd.cd_name, &cgd_ca);
225 1.65 dyoung if (error != 0)
226 1.65 dyoung aprint_error("%s: unable to register cfattach\n",
227 1.65 dyoung cgd_cd.cd_name);
228 1.65 dyoung }
229 1.65 dyoung
230 1.65 dyoung static struct cgd_softc *
231 1.65 dyoung cgd_spawn(int unit)
232 1.65 dyoung {
233 1.65 dyoung cfdata_t cf;
234 1.65 dyoung
235 1.65 dyoung cf = malloc(sizeof(*cf), M_DEVBUF, M_WAITOK);
236 1.65 dyoung cf->cf_name = cgd_cd.cd_name;
237 1.65 dyoung cf->cf_atname = cgd_cd.cd_name;
238 1.65 dyoung cf->cf_unit = unit;
239 1.65 dyoung cf->cf_fstate = FSTATE_STAR;
240 1.65 dyoung
241 1.65 dyoung return device_private(config_attach_pseudo(cf));
242 1.65 dyoung }
243 1.65 dyoung
244 1.65 dyoung static int
245 1.65 dyoung cgd_destroy(device_t dev)
246 1.65 dyoung {
247 1.65 dyoung int error;
248 1.65 dyoung cfdata_t cf;
249 1.1 elric
250 1.65 dyoung cf = device_cfdata(dev);
251 1.65 dyoung error = config_detach(dev, DETACH_QUIET);
252 1.65 dyoung if (error)
253 1.65 dyoung return error;
254 1.65 dyoung free(cf, M_DEVBUF);
255 1.65 dyoung return 0;
256 1.1 elric }
257 1.1 elric
258 1.18 thorpej static int
259 1.32 christos cgdopen(dev_t dev, int flags, int fmt, struct lwp *l)
260 1.1 elric {
261 1.1 elric struct cgd_softc *cs;
262 1.1 elric
263 1.56 cegger DPRINTF_FOLLOW(("cgdopen(0x%"PRIx64", %d)\n", dev, flags));
264 1.1 elric GETCGD_SOFTC(cs, dev);
265 1.32 christos return dk_open(di, &cs->sc_dksc, dev, flags, fmt, l);
266 1.1 elric }
267 1.1 elric
268 1.18 thorpej static int
269 1.32 christos cgdclose(dev_t dev, int flags, int fmt, struct lwp *l)
270 1.1 elric {
271 1.65 dyoung int error;
272 1.1 elric struct cgd_softc *cs;
273 1.65 dyoung struct dk_softc *dksc;
274 1.1 elric
275 1.56 cegger DPRINTF_FOLLOW(("cgdclose(0x%"PRIx64", %d)\n", dev, flags));
276 1.1 elric GETCGD_SOFTC(cs, dev);
277 1.65 dyoung dksc = &cs->sc_dksc;
278 1.65 dyoung if ((error = dk_close(di, dksc, dev, flags, fmt, l)) != 0)
279 1.65 dyoung return error;
280 1.65 dyoung
281 1.65 dyoung if ((dksc->sc_flags & DKF_INITED) == 0) {
282 1.77 elric if ((error = cgd_destroy(cs->sc_dksc.sc_dev)) != 0) {
283 1.77 elric aprint_error_dev(dksc->sc_dev,
284 1.65 dyoung "unable to detach instance\n");
285 1.65 dyoung return error;
286 1.65 dyoung }
287 1.65 dyoung }
288 1.65 dyoung return 0;
289 1.1 elric }
290 1.1 elric
291 1.18 thorpej static void
292 1.1 elric cgdstrategy(struct buf *bp)
293 1.1 elric {
294 1.1 elric struct cgd_softc *cs = getcgd_softc(bp->b_dev);
295 1.1 elric
296 1.1 elric DPRINTF_FOLLOW(("cgdstrategy(%p): b_bcount = %ld\n", bp,
297 1.1 elric (long)bp->b_bcount));
298 1.72 riastrad
299 1.72 riastrad /*
300 1.72 riastrad * Reject unaligned writes. We can encrypt and decrypt only
301 1.72 riastrad * complete disk sectors, and we let the ciphers require their
302 1.72 riastrad * buffers to be aligned to 32-bit boundaries.
303 1.72 riastrad */
304 1.72 riastrad if (bp->b_blkno < 0 ||
305 1.72 riastrad (bp->b_bcount % DEV_BSIZE) != 0 ||
306 1.72 riastrad ((uintptr_t)bp->b_data & 3) != 0) {
307 1.72 riastrad bp->b_error = EINVAL;
308 1.72 riastrad bp->b_resid = bp->b_bcount;
309 1.72 riastrad biodone(bp);
310 1.72 riastrad return;
311 1.72 riastrad }
312 1.72 riastrad
313 1.1 elric /* XXXrcd: Should we test for (cs != NULL)? */
314 1.1 elric dk_strategy(di, &cs->sc_dksc, bp);
315 1.1 elric return;
316 1.1 elric }
317 1.1 elric
318 1.18 thorpej static int
319 1.1 elric cgdsize(dev_t dev)
320 1.1 elric {
321 1.1 elric struct cgd_softc *cs = getcgd_softc(dev);
322 1.1 elric
323 1.56 cegger DPRINTF_FOLLOW(("cgdsize(0x%"PRIx64")\n", dev));
324 1.1 elric if (!cs)
325 1.1 elric return -1;
326 1.1 elric return dk_size(di, &cs->sc_dksc, dev);
327 1.1 elric }
328 1.1 elric
329 1.16 elric /*
330 1.16 elric * cgd_{get,put}data are functions that deal with getting a buffer
331 1.16 elric * for the new encrypted data. We have a buffer per device so that
332 1.16 elric * we can ensure that we can always have a transaction in flight.
333 1.16 elric * We use this buffer first so that we have one less piece of
334 1.16 elric * malloc'ed data at any given point.
335 1.16 elric */
336 1.16 elric
337 1.16 elric static void *
338 1.16 elric cgd_getdata(struct dk_softc *dksc, unsigned long size)
339 1.16 elric {
340 1.77 elric struct cgd_softc *cs = (struct cgd_softc *)dksc;
341 1.44 christos void * data = NULL;
342 1.16 elric
343 1.16 elric simple_lock(&cs->sc_slock);
344 1.16 elric if (cs->sc_data_used == 0) {
345 1.16 elric cs->sc_data_used = 1;
346 1.16 elric data = cs->sc_data;
347 1.16 elric }
348 1.16 elric simple_unlock(&cs->sc_slock);
349 1.16 elric
350 1.16 elric if (data)
351 1.16 elric return data;
352 1.16 elric
353 1.16 elric return malloc(size, M_DEVBUF, M_NOWAIT);
354 1.16 elric }
355 1.16 elric
356 1.1 elric static void
357 1.44 christos cgd_putdata(struct dk_softc *dksc, void *data)
358 1.16 elric {
359 1.77 elric struct cgd_softc *cs = (struct cgd_softc *)dksc;
360 1.16 elric
361 1.16 elric if (data == cs->sc_data) {
362 1.16 elric simple_lock(&cs->sc_slock);
363 1.16 elric cs->sc_data_used = 0;
364 1.16 elric simple_unlock(&cs->sc_slock);
365 1.16 elric } else {
366 1.16 elric free(data, M_DEVBUF);
367 1.16 elric }
368 1.16 elric }
369 1.16 elric
370 1.16 elric static int
371 1.1 elric cgdstart(struct dk_softc *dksc, struct buf *bp)
372 1.1 elric {
373 1.77 elric struct cgd_softc *cs = (struct cgd_softc *)dksc;
374 1.17 dbj struct buf *nbp;
375 1.44 christos void * addr;
376 1.44 christos void * newaddr;
377 1.1 elric daddr_t bn;
378 1.49 ad struct vnode *vp;
379 1.1 elric
380 1.1 elric DPRINTF_FOLLOW(("cgdstart(%p, %p)\n", dksc, bp));
381 1.1 elric disk_busy(&dksc->sc_dkdev); /* XXX: put in dksubr.c */
382 1.1 elric
383 1.31 yamt bn = bp->b_rawblkno;
384 1.1 elric
385 1.1 elric /*
386 1.16 elric * We attempt to allocate all of our resources up front, so that
387 1.16 elric * we can fail quickly if they are unavailable.
388 1.16 elric */
389 1.22 perry
390 1.49 ad nbp = getiobuf(cs->sc_tvn, false);
391 1.17 dbj if (nbp == NULL) {
392 1.16 elric disk_unbusy(&dksc->sc_dkdev, 0, (bp->b_flags & B_READ));
393 1.16 elric return -1;
394 1.16 elric }
395 1.16 elric
396 1.16 elric /*
397 1.1 elric * If we are writing, then we need to encrypt the outgoing
398 1.16 elric * block into a new block of memory. If we fail, then we
399 1.16 elric * return an error and let the dksubr framework deal with it.
400 1.1 elric */
401 1.1 elric newaddr = addr = bp->b_data;
402 1.1 elric if ((bp->b_flags & B_READ) == 0) {
403 1.16 elric newaddr = cgd_getdata(dksc, bp->b_bcount);
404 1.16 elric if (!newaddr) {
405 1.33 yamt putiobuf(nbp);
406 1.16 elric disk_unbusy(&dksc->sc_dkdev, 0, (bp->b_flags & B_READ));
407 1.16 elric return -1;
408 1.16 elric }
409 1.1 elric cgd_cipher(cs, newaddr, addr, bp->b_bcount, bn,
410 1.1 elric DEV_BSIZE, CGD_CIPHER_ENCRYPT);
411 1.1 elric }
412 1.1 elric
413 1.17 dbj nbp->b_data = newaddr;
414 1.49 ad nbp->b_flags = bp->b_flags;
415 1.49 ad nbp->b_oflags = bp->b_oflags;
416 1.49 ad nbp->b_cflags = bp->b_cflags;
417 1.17 dbj nbp->b_iodone = cgdiodone;
418 1.17 dbj nbp->b_proc = bp->b_proc;
419 1.17 dbj nbp->b_blkno = bn;
420 1.17 dbj nbp->b_bcount = bp->b_bcount;
421 1.17 dbj nbp->b_private = bp;
422 1.17 dbj
423 1.17 dbj BIO_COPYPRIO(nbp, bp);
424 1.17 dbj
425 1.17 dbj if ((nbp->b_flags & B_READ) == 0) {
426 1.49 ad vp = nbp->b_vp;
427 1.73 rmind mutex_enter(vp->v_interlock);
428 1.49 ad vp->v_numoutput++;
429 1.73 rmind mutex_exit(vp->v_interlock);
430 1.17 dbj }
431 1.17 dbj VOP_STRATEGY(cs->sc_tvn, nbp);
432 1.16 elric return 0;
433 1.1 elric }
434 1.1 elric
435 1.18 thorpej static void
436 1.17 dbj cgdiodone(struct buf *nbp)
437 1.1 elric {
438 1.17 dbj struct buf *obp = nbp->b_private;
439 1.17 dbj struct cgd_softc *cs = getcgd_softc(obp->b_dev);
440 1.1 elric struct dk_softc *dksc = &cs->sc_dksc;
441 1.69 bouyer int s;
442 1.22 perry
443 1.17 dbj KDASSERT(cs);
444 1.1 elric
445 1.17 dbj DPRINTF_FOLLOW(("cgdiodone(%p)\n", nbp));
446 1.20 yamt DPRINTF(CGDB_IO, ("cgdiodone: bp %p bcount %d resid %d\n",
447 1.1 elric obp, obp->b_bcount, obp->b_resid));
448 1.56 cegger DPRINTF(CGDB_IO, (" dev 0x%"PRIx64", nbp %p bn %" PRId64 " addr %p bcnt %d\n",
449 1.17 dbj nbp->b_dev, nbp, nbp->b_blkno, nbp->b_data,
450 1.17 dbj nbp->b_bcount));
451 1.46 ad if (nbp->b_error != 0) {
452 1.46 ad obp->b_error = nbp->b_error;
453 1.62 christos DPRINTF(CGDB_IO, ("%s: error %d\n", dksc->sc_xname,
454 1.62 christos obp->b_error));
455 1.1 elric }
456 1.1 elric
457 1.16 elric /* Perform the decryption if we are reading.
458 1.1 elric *
459 1.1 elric * Note: use the blocknumber from nbp, since it is what
460 1.1 elric * we used to encrypt the blocks.
461 1.1 elric */
462 1.1 elric
463 1.16 elric if (nbp->b_flags & B_READ)
464 1.1 elric cgd_cipher(cs, obp->b_data, obp->b_data, obp->b_bcount,
465 1.1 elric nbp->b_blkno, DEV_BSIZE, CGD_CIPHER_DECRYPT);
466 1.1 elric
467 1.16 elric /* If we allocated memory, free it now... */
468 1.1 elric if (nbp->b_data != obp->b_data)
469 1.16 elric cgd_putdata(dksc, nbp->b_data);
470 1.1 elric
471 1.33 yamt putiobuf(nbp);
472 1.1 elric
473 1.1 elric /* Request is complete for whatever reason */
474 1.1 elric obp->b_resid = 0;
475 1.46 ad if (obp->b_error != 0)
476 1.1 elric obp->b_resid = obp->b_bcount;
477 1.69 bouyer s = splbio();
478 1.5 mrg disk_unbusy(&dksc->sc_dkdev, obp->b_bcount - obp->b_resid,
479 1.5 mrg (obp->b_flags & B_READ));
480 1.1 elric biodone(obp);
481 1.16 elric dk_iodone(di, dksc);
482 1.69 bouyer splx(s);
483 1.1 elric }
484 1.1 elric
485 1.1 elric /* XXX: we should probably put these into dksubr.c, mostly */
486 1.18 thorpej static int
487 1.40 christos cgdread(dev_t dev, struct uio *uio, int flags)
488 1.1 elric {
489 1.1 elric struct cgd_softc *cs;
490 1.1 elric struct dk_softc *dksc;
491 1.1 elric
492 1.56 cegger DPRINTF_FOLLOW(("cgdread(0x%llx, %p, %d)\n",
493 1.56 cegger (unsigned long long)dev, uio, flags));
494 1.1 elric GETCGD_SOFTC(cs, dev);
495 1.1 elric dksc = &cs->sc_dksc;
496 1.1 elric if ((dksc->sc_flags & DKF_INITED) == 0)
497 1.1 elric return ENXIO;
498 1.1 elric return physio(cgdstrategy, NULL, dev, B_READ, minphys, uio);
499 1.1 elric }
500 1.1 elric
501 1.1 elric /* XXX: we should probably put these into dksubr.c, mostly */
502 1.18 thorpej static int
503 1.40 christos cgdwrite(dev_t dev, struct uio *uio, int flags)
504 1.1 elric {
505 1.1 elric struct cgd_softc *cs;
506 1.1 elric struct dk_softc *dksc;
507 1.1 elric
508 1.56 cegger DPRINTF_FOLLOW(("cgdwrite(0x%"PRIx64", %p, %d)\n", dev, uio, flags));
509 1.1 elric GETCGD_SOFTC(cs, dev);
510 1.1 elric dksc = &cs->sc_dksc;
511 1.1 elric if ((dksc->sc_flags & DKF_INITED) == 0)
512 1.1 elric return ENXIO;
513 1.1 elric return physio(cgdstrategy, NULL, dev, B_WRITE, minphys, uio);
514 1.1 elric }
515 1.1 elric
516 1.18 thorpej static int
517 1.44 christos cgdioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
518 1.1 elric {
519 1.1 elric struct cgd_softc *cs;
520 1.1 elric struct dk_softc *dksc;
521 1.29 yamt struct disk *dk;
522 1.1 elric int part = DISKPART(dev);
523 1.1 elric int pmask = 1 << part;
524 1.1 elric
525 1.56 cegger DPRINTF_FOLLOW(("cgdioctl(0x%"PRIx64", %ld, %p, %d, %p)\n",
526 1.32 christos dev, cmd, data, flag, l));
527 1.78 christos
528 1.1 elric switch (cmd) {
529 1.78 christos case CGDIOCGET: /* don't call cgd_spawn() if the device isn't there */
530 1.78 christos cs = NULL;
531 1.78 christos dksc = NULL;
532 1.78 christos dk = NULL;
533 1.78 christos break;
534 1.1 elric case CGDIOCSET:
535 1.1 elric case CGDIOCCLR:
536 1.1 elric if ((flag & FWRITE) == 0)
537 1.1 elric return EBADF;
538 1.78 christos /* FALLTHROUGH */
539 1.78 christos default:
540 1.78 christos GETCGD_SOFTC(cs, dev);
541 1.78 christos dksc = &cs->sc_dksc;
542 1.78 christos dk = &dksc->sc_dkdev;
543 1.78 christos break;
544 1.1 elric }
545 1.1 elric
546 1.1 elric switch (cmd) {
547 1.1 elric case CGDIOCSET:
548 1.1 elric if (dksc->sc_flags & DKF_INITED)
549 1.68 dyoung return EBUSY;
550 1.68 dyoung return cgd_ioctl_set(cs, data, l);
551 1.1 elric case CGDIOCCLR:
552 1.65 dyoung if (DK_BUSY(&cs->sc_dksc, pmask))
553 1.68 dyoung return EBUSY;
554 1.68 dyoung return cgd_ioctl_clr(cs, l);
555 1.78 christos case CGDIOCGET:
556 1.78 christos return cgd_ioctl_get(dev, data, l);
557 1.57 apb case DIOCCACHESYNC:
558 1.57 apb /*
559 1.57 apb * XXX Do we really need to care about having a writable
560 1.57 apb * file descriptor here?
561 1.57 apb */
562 1.57 apb if ((flag & FWRITE) == 0)
563 1.57 apb return (EBADF);
564 1.57 apb
565 1.57 apb /*
566 1.57 apb * We pass this call down to the underlying disk.
567 1.57 apb */
568 1.68 dyoung return VOP_IOCTL(cs->sc_tvn, cmd, data, flag, l->l_cred);
569 1.1 elric default:
570 1.68 dyoung return dk_ioctl(di, dksc, dev, cmd, data, flag, l);
571 1.1 elric }
572 1.1 elric }
573 1.1 elric
574 1.18 thorpej static int
575 1.44 christos cgddump(dev_t dev, daddr_t blkno, void *va, size_t size)
576 1.1 elric {
577 1.1 elric struct cgd_softc *cs;
578 1.1 elric
579 1.56 cegger DPRINTF_FOLLOW(("cgddump(0x%"PRIx64", %" PRId64 ", %p, %lu)\n",
580 1.56 cegger dev, blkno, va, (unsigned long)size));
581 1.1 elric GETCGD_SOFTC(cs, dev);
582 1.1 elric return dk_dump(di, &cs->sc_dksc, dev, blkno, va, size);
583 1.1 elric }
584 1.1 elric
585 1.1 elric /*
586 1.1 elric * XXXrcd:
587 1.1 elric * for now we hardcode the maximum key length.
588 1.1 elric */
589 1.1 elric #define MAX_KEYSIZE 1024
590 1.1 elric
591 1.53 christos static const struct {
592 1.53 christos const char *n;
593 1.53 christos int v;
594 1.53 christos int d;
595 1.53 christos } encblkno[] = {
596 1.53 christos { "encblkno", CGD_CIPHER_CBC_ENCBLKNO8, 1 },
597 1.53 christos { "encblkno8", CGD_CIPHER_CBC_ENCBLKNO8, 1 },
598 1.53 christos { "encblkno1", CGD_CIPHER_CBC_ENCBLKNO1, 8 },
599 1.53 christos };
600 1.53 christos
601 1.1 elric /* ARGSUSED */
602 1.1 elric static int
603 1.32 christos cgd_ioctl_set(struct cgd_softc *cs, void *data, struct lwp *l)
604 1.1 elric {
605 1.1 elric struct cgd_ioctl *ci = data;
606 1.1 elric struct vnode *vp;
607 1.1 elric int ret;
608 1.53 christos size_t i;
609 1.43 cbiere size_t keybytes; /* key length in bytes */
610 1.27 drochner const char *cp;
611 1.71 dholland struct pathbuf *pb;
612 1.36 christos char *inbuf;
613 1.80 christos struct dk_softc *dksc = &cs->sc_dksc;
614 1.1 elric
615 1.1 elric cp = ci->ci_disk;
616 1.71 dholland
617 1.71 dholland ret = pathbuf_copyin(ci->ci_disk, &pb);
618 1.71 dholland if (ret != 0) {
619 1.71 dholland return ret;
620 1.71 dholland }
621 1.71 dholland ret = dk_lookup(pb, l, &vp);
622 1.71 dholland pathbuf_destroy(pb);
623 1.71 dholland if (ret != 0) {
624 1.1 elric return ret;
625 1.71 dholland }
626 1.1 elric
627 1.36 christos inbuf = malloc(MAX_KEYSIZE, M_TEMP, M_WAITOK);
628 1.36 christos
629 1.32 christos if ((ret = cgdinit(cs, cp, vp, l)) != 0)
630 1.1 elric goto bail;
631 1.1 elric
632 1.36 christos (void)memset(inbuf, 0, MAX_KEYSIZE);
633 1.1 elric ret = copyinstr(ci->ci_alg, inbuf, 256, NULL);
634 1.1 elric if (ret)
635 1.1 elric goto bail;
636 1.1 elric cs->sc_cfuncs = cryptfuncs_find(inbuf);
637 1.1 elric if (!cs->sc_cfuncs) {
638 1.1 elric ret = EINVAL;
639 1.1 elric goto bail;
640 1.1 elric }
641 1.1 elric
642 1.43 cbiere (void)memset(inbuf, 0, MAX_KEYSIZE);
643 1.36 christos ret = copyinstr(ci->ci_ivmethod, inbuf, MAX_KEYSIZE, NULL);
644 1.1 elric if (ret)
645 1.1 elric goto bail;
646 1.53 christos
647 1.53 christos for (i = 0; i < __arraycount(encblkno); i++)
648 1.53 christos if (strcmp(encblkno[i].n, inbuf) == 0)
649 1.53 christos break;
650 1.53 christos
651 1.53 christos if (i == __arraycount(encblkno)) {
652 1.1 elric ret = EINVAL;
653 1.1 elric goto bail;
654 1.1 elric }
655 1.1 elric
656 1.15 dan keybytes = ci->ci_keylen / 8 + 1;
657 1.15 dan if (keybytes > MAX_KEYSIZE) {
658 1.1 elric ret = EINVAL;
659 1.1 elric goto bail;
660 1.1 elric }
661 1.53 christos
662 1.36 christos (void)memset(inbuf, 0, MAX_KEYSIZE);
663 1.15 dan ret = copyin(ci->ci_key, inbuf, keybytes);
664 1.1 elric if (ret)
665 1.1 elric goto bail;
666 1.1 elric
667 1.1 elric cs->sc_cdata.cf_blocksize = ci->ci_blocksize;
668 1.53 christos cs->sc_cdata.cf_mode = encblkno[i].v;
669 1.78 christos cs->sc_cdata.cf_keylen = ci->ci_keylen;
670 1.1 elric cs->sc_cdata.cf_priv = cs->sc_cfuncs->cf_init(ci->ci_keylen, inbuf,
671 1.1 elric &cs->sc_cdata.cf_blocksize);
672 1.62 christos if (cs->sc_cdata.cf_blocksize > CGD_MAXBLOCKSIZE) {
673 1.62 christos log(LOG_WARNING, "cgd: Disallowed cipher with blocksize %zu > %u\n",
674 1.63 christos cs->sc_cdata.cf_blocksize, CGD_MAXBLOCKSIZE);
675 1.62 christos cs->sc_cdata.cf_priv = NULL;
676 1.62 christos }
677 1.78 christos
678 1.53 christos /*
679 1.53 christos * The blocksize is supposed to be in bytes. Unfortunately originally
680 1.53 christos * it was expressed in bits. For compatibility we maintain encblkno
681 1.53 christos * and encblkno8.
682 1.53 christos */
683 1.53 christos cs->sc_cdata.cf_blocksize /= encblkno[i].d;
684 1.36 christos (void)memset(inbuf, 0, MAX_KEYSIZE);
685 1.1 elric if (!cs->sc_cdata.cf_priv) {
686 1.1 elric ret = EINVAL; /* XXX is this the right error? */
687 1.1 elric goto bail;
688 1.1 elric }
689 1.36 christos free(inbuf, M_TEMP);
690 1.1 elric
691 1.80 christos bufq_alloc(&dksc->sc_bufq, "fcfs", 0);
692 1.16 elric
693 1.16 elric cs->sc_data = malloc(MAXPHYS, M_DEVBUF, M_WAITOK);
694 1.16 elric cs->sc_data_used = 0;
695 1.16 elric
696 1.80 christos dksc->sc_flags |= DKF_INITED;
697 1.1 elric
698 1.80 christos disk_set_info(dksc->sc_dev, &dksc->sc_dkdev, NULL);
699 1.77 elric
700 1.1 elric /* Attach the disk. */
701 1.80 christos disk_attach(&dksc->sc_dkdev);
702 1.1 elric
703 1.1 elric /* Try and read the disklabel. */
704 1.80 christos dk_getdisklabel(di, dksc, 0 /* XXX ? (cause of PR 41704) */);
705 1.1 elric
706 1.29 yamt /* Discover wedges on this disk. */
707 1.80 christos dkwedge_discover(&dksc->sc_dkdev);
708 1.29 yamt
709 1.1 elric return 0;
710 1.1 elric
711 1.1 elric bail:
712 1.36 christos free(inbuf, M_TEMP);
713 1.51 ad (void)vn_close(vp, FREAD|FWRITE, l->l_cred);
714 1.1 elric return ret;
715 1.1 elric }
716 1.1 elric
717 1.1 elric /* ARGSUSED */
718 1.1 elric static int
719 1.65 dyoung cgd_ioctl_clr(struct cgd_softc *cs, struct lwp *l)
720 1.1 elric {
721 1.16 elric int s;
722 1.80 christos struct dk_softc *dksc = &cs->sc_dksc;
723 1.65 dyoung
724 1.65 dyoung if ((dksc->sc_flags & DKF_INITED) == 0)
725 1.65 dyoung return ENXIO;
726 1.16 elric
727 1.29 yamt /* Delete all of our wedges. */
728 1.80 christos dkwedge_delall(&dksc->sc_dkdev);
729 1.29 yamt
730 1.16 elric /* Kill off any queued buffers. */
731 1.16 elric s = splbio();
732 1.80 christos bufq_drain(dksc->sc_bufq);
733 1.16 elric splx(s);
734 1.80 christos bufq_free(dksc->sc_bufq);
735 1.1 elric
736 1.51 ad (void)vn_close(cs->sc_tvn, FREAD|FWRITE, l->l_cred);
737 1.1 elric cs->sc_cfuncs->cf_destroy(cs->sc_cdata.cf_priv);
738 1.1 elric free(cs->sc_tpath, M_DEVBUF);
739 1.16 elric free(cs->sc_data, M_DEVBUF);
740 1.16 elric cs->sc_data_used = 0;
741 1.80 christos dksc->sc_flags &= ~DKF_INITED;
742 1.80 christos disk_detach(&dksc->sc_dkdev);
743 1.1 elric
744 1.1 elric return 0;
745 1.1 elric }
746 1.1 elric
747 1.1 elric static int
748 1.78 christos cgd_ioctl_get(dev_t dev, void *data, struct lwp *l)
749 1.78 christos {
750 1.81 martin struct cgd_softc *cs = getcgd_softc(dev);
751 1.78 christos struct cgd_user *cgu;
752 1.78 christos int unit;
753 1.80 christos struct dk_softc *dksc = &cs->sc_dksc;
754 1.78 christos
755 1.78 christos unit = CGDUNIT(dev);
756 1.78 christos cgu = (struct cgd_user *)data;
757 1.78 christos
758 1.78 christos DPRINTF_FOLLOW(("cgd_ioctl_get(0x%"PRIx64", %d, %p, %p)\n",
759 1.78 christos dev, unit, data, l));
760 1.78 christos
761 1.78 christos if (cgu->cgu_unit == -1)
762 1.78 christos cgu->cgu_unit = unit;
763 1.78 christos
764 1.78 christos if (cgu->cgu_unit < 0)
765 1.78 christos return EINVAL; /* XXX: should this be ENXIO? */
766 1.78 christos
767 1.78 christos cs = device_lookup_private(&cgd_cd, unit);
768 1.80 christos if (cs == NULL || (dksc->sc_flags & DKF_INITED) == 0) {
769 1.78 christos cgu->cgu_dev = 0;
770 1.78 christos cgu->cgu_alg[0] = '\0';
771 1.78 christos cgu->cgu_blocksize = 0;
772 1.78 christos cgu->cgu_mode = 0;
773 1.78 christos cgu->cgu_keylen = 0;
774 1.78 christos }
775 1.78 christos else {
776 1.78 christos cgu->cgu_dev = cs->sc_tdev;
777 1.78 christos strlcpy(cgu->cgu_alg, cs->sc_cfuncs->cf_name,
778 1.78 christos sizeof(cgu->cgu_alg));
779 1.78 christos cgu->cgu_blocksize = cs->sc_cdata.cf_blocksize;
780 1.78 christos cgu->cgu_mode = cs->sc_cdata.cf_mode;
781 1.78 christos cgu->cgu_keylen = cs->sc_cdata.cf_keylen;
782 1.78 christos }
783 1.78 christos return 0;
784 1.78 christos }
785 1.78 christos
786 1.78 christos static int
787 1.27 drochner cgdinit(struct cgd_softc *cs, const char *cpath, struct vnode *vp,
788 1.32 christos struct lwp *l)
789 1.1 elric {
790 1.80 christos struct disk_geom *dg;
791 1.1 elric struct vattr va;
792 1.1 elric int ret;
793 1.36 christos char *tmppath;
794 1.76 christos uint64_t psize;
795 1.76 christos unsigned secsize;
796 1.80 christos struct dk_softc *dksc = &cs->sc_dksc;
797 1.1 elric
798 1.1 elric cs->sc_tvn = vp;
799 1.36 christos cs->sc_tpath = NULL;
800 1.1 elric
801 1.36 christos tmppath = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
802 1.1 elric ret = copyinstr(cpath, tmppath, MAXPATHLEN, &cs->sc_tpathlen);
803 1.1 elric if (ret)
804 1.1 elric goto bail;
805 1.1 elric cs->sc_tpath = malloc(cs->sc_tpathlen, M_DEVBUF, M_WAITOK);
806 1.1 elric memcpy(cs->sc_tpath, tmppath, cs->sc_tpathlen);
807 1.1 elric
808 1.75 hannken vn_lock(vp, LK_SHARED | LK_RETRY);
809 1.75 hannken ret = VOP_GETATTR(vp, &va, l->l_cred);
810 1.75 hannken VOP_UNLOCK(vp);
811 1.75 hannken if (ret != 0)
812 1.1 elric goto bail;
813 1.1 elric
814 1.1 elric cs->sc_tdev = va.va_rdev;
815 1.1 elric
816 1.76 christos if ((ret = getdisksize(vp, &psize, &secsize)) != 0)
817 1.1 elric goto bail;
818 1.1 elric
819 1.76 christos if (psize == 0) {
820 1.1 elric ret = ENODEV;
821 1.1 elric goto bail;
822 1.1 elric }
823 1.1 elric
824 1.1 elric /*
825 1.1 elric * XXX here we should probe the underlying device. If we
826 1.1 elric * are accessing a partition of type RAW_PART, then
827 1.1 elric * we should populate our initial geometry with the
828 1.1 elric * geometry that we discover from the device.
829 1.1 elric */
830 1.80 christos dg = &dksc->sc_dkdev.dk_geom;
831 1.80 christos memset(dg, 0, sizeof(*dg));
832 1.80 christos dg->dg_secperunit = psize;
833 1.80 christos // XXX: Inherit?
834 1.80 christos dg->dg_secsize = DEV_BSIZE;
835 1.80 christos dg->dg_ntracks = 1;
836 1.80 christos dg->dg_nsectors = 1024 * (1024 / dg->dg_secsize);
837 1.80 christos dg->dg_ncylinders = dg->dg_secperunit / dg->dg_nsectors;
838 1.1 elric
839 1.1 elric bail:
840 1.36 christos free(tmppath, M_TEMP);
841 1.1 elric if (ret && cs->sc_tpath)
842 1.1 elric free(cs->sc_tpath, M_DEVBUF);
843 1.1 elric return ret;
844 1.1 elric }
845 1.1 elric
846 1.1 elric /*
847 1.1 elric * Our generic cipher entry point. This takes care of the
848 1.1 elric * IV mode and passes off the work to the specific cipher.
849 1.1 elric * We implement here the IV method ``encrypted block
850 1.1 elric * number''.
851 1.22 perry *
852 1.1 elric * For the encryption case, we accomplish this by setting
853 1.1 elric * up a struct uio where the first iovec of the source is
854 1.1 elric * the blocknumber and the first iovec of the dest is a
855 1.1 elric * sink. We then call the cipher with an IV of zero, and
856 1.1 elric * the right thing happens.
857 1.22 perry *
858 1.1 elric * For the decryption case, we use the same basic mechanism
859 1.1 elric * for symmetry, but we encrypt the block number in the
860 1.1 elric * first iovec.
861 1.1 elric *
862 1.1 elric * We mainly do this to avoid requiring the definition of
863 1.1 elric * an ECB mode.
864 1.1 elric *
865 1.1 elric * XXXrcd: for now we rely on our own crypto framework defined
866 1.1 elric * in dev/cgd_crypto.c. This will change when we
867 1.1 elric * get a generic kernel crypto framework.
868 1.1 elric */
869 1.1 elric
870 1.1 elric static void
871 1.25 xtraeme blkno2blkno_buf(char *sbuf, daddr_t blkno)
872 1.1 elric {
873 1.1 elric int i;
874 1.1 elric
875 1.1 elric /* Set up the blkno in blkno_buf, here we do not care much
876 1.1 elric * about the final layout of the information as long as we
877 1.1 elric * can guarantee that each sector will have a different IV
878 1.1 elric * and that the endianness of the machine will not affect
879 1.1 elric * the representation that we have chosen.
880 1.1 elric *
881 1.1 elric * We choose this representation, because it does not rely
882 1.1 elric * on the size of buf (which is the blocksize of the cipher),
883 1.1 elric * but allows daddr_t to grow without breaking existing
884 1.1 elric * disks.
885 1.1 elric *
886 1.1 elric * Note that blkno2blkno_buf does not take a size as input,
887 1.1 elric * and hence must be called on a pre-zeroed buffer of length
888 1.1 elric * greater than or equal to sizeof(daddr_t).
889 1.1 elric */
890 1.1 elric for (i=0; i < sizeof(daddr_t); i++) {
891 1.25 xtraeme *sbuf++ = blkno & 0xff;
892 1.1 elric blkno >>= 8;
893 1.1 elric }
894 1.1 elric }
895 1.1 elric
896 1.1 elric static void
897 1.44 christos cgd_cipher(struct cgd_softc *cs, void *dstv, void *srcv,
898 1.44 christos size_t len, daddr_t blkno, size_t secsize, int dir)
899 1.1 elric {
900 1.44 christos char *dst = dstv;
901 1.44 christos char *src = srcv;
902 1.1 elric cfunc_cipher *cipher = cs->sc_cfuncs->cf_cipher;
903 1.1 elric struct uio dstuio;
904 1.1 elric struct uio srcuio;
905 1.1 elric struct iovec dstiov[2];
906 1.1 elric struct iovec srciov[2];
907 1.42 christos size_t blocksize = cs->sc_cdata.cf_blocksize;
908 1.62 christos char sink[CGD_MAXBLOCKSIZE];
909 1.62 christos char zero_iv[CGD_MAXBLOCKSIZE];
910 1.62 christos char blkno_buf[CGD_MAXBLOCKSIZE];
911 1.1 elric
912 1.1 elric DPRINTF_FOLLOW(("cgd_cipher() dir=%d\n", dir));
913 1.1 elric
914 1.22 perry DIAGCONDPANIC(len % blocksize != 0,
915 1.1 elric ("cgd_cipher: len %% blocksize != 0"));
916 1.1 elric
917 1.1 elric /* ensure that sizeof(daddr_t) <= blocksize (for encblkno IVing) */
918 1.1 elric DIAGCONDPANIC(sizeof(daddr_t) > blocksize,
919 1.1 elric ("cgd_cipher: sizeof(daddr_t) > blocksize"));
920 1.1 elric
921 1.64 christos memset(zero_iv, 0x0, blocksize);
922 1.1 elric
923 1.1 elric dstuio.uio_iov = dstiov;
924 1.1 elric dstuio.uio_iovcnt = 2;
925 1.1 elric
926 1.1 elric srcuio.uio_iov = srciov;
927 1.1 elric srcuio.uio_iovcnt = 2;
928 1.1 elric
929 1.1 elric dstiov[0].iov_base = sink;
930 1.1 elric dstiov[0].iov_len = blocksize;
931 1.1 elric srciov[0].iov_base = blkno_buf;
932 1.1 elric srciov[0].iov_len = blocksize;
933 1.1 elric dstiov[1].iov_len = secsize;
934 1.1 elric srciov[1].iov_len = secsize;
935 1.1 elric
936 1.1 elric for (; len > 0; len -= secsize) {
937 1.1 elric dstiov[1].iov_base = dst;
938 1.1 elric srciov[1].iov_base = src;
939 1.1 elric
940 1.64 christos memset(blkno_buf, 0x0, blocksize);
941 1.1 elric blkno2blkno_buf(blkno_buf, blkno);
942 1.1 elric if (dir == CGD_CIPHER_DECRYPT) {
943 1.1 elric dstuio.uio_iovcnt = 1;
944 1.1 elric srcuio.uio_iovcnt = 1;
945 1.1 elric IFDEBUG(CGDB_CRYPTO, hexprint("step 0: blkno_buf",
946 1.64 christos blkno_buf, blocksize));
947 1.1 elric cipher(cs->sc_cdata.cf_priv, &dstuio, &srcuio,
948 1.1 elric zero_iv, CGD_CIPHER_ENCRYPT);
949 1.1 elric memcpy(blkno_buf, sink, blocksize);
950 1.1 elric dstuio.uio_iovcnt = 2;
951 1.1 elric srcuio.uio_iovcnt = 2;
952 1.1 elric }
953 1.1 elric
954 1.1 elric IFDEBUG(CGDB_CRYPTO, hexprint("step 1: blkno_buf",
955 1.64 christos blkno_buf, blocksize));
956 1.1 elric cipher(cs->sc_cdata.cf_priv, &dstuio, &srcuio, zero_iv, dir);
957 1.1 elric IFDEBUG(CGDB_CRYPTO, hexprint("step 2: sink",
958 1.64 christos sink, blocksize));
959 1.1 elric
960 1.1 elric dst += secsize;
961 1.1 elric src += secsize;
962 1.1 elric blkno++;
963 1.1 elric }
964 1.1 elric }
965 1.1 elric
966 1.1 elric #ifdef DEBUG
967 1.1 elric static void
968 1.26 drochner hexprint(const char *start, void *buf, int len)
969 1.1 elric {
970 1.1 elric char *c = buf;
971 1.1 elric
972 1.1 elric DIAGCONDPANIC(len < 0, ("hexprint: called with len < 0"));
973 1.1 elric printf("%s: len=%06d 0x", start, len);
974 1.1 elric while (len--)
975 1.43 cbiere printf("%02x", (unsigned char) *c++);
976 1.1 elric }
977 1.1 elric #endif
978 1.58 haad
979 1.74 jruoho MODULE(MODULE_CLASS_DRIVER, cgd, NULL);
980 1.74 jruoho
981 1.58 haad #ifdef _MODULE
982 1.66 dyoung CFDRIVER_DECL(cgd, DV_DISK, NULL);
983 1.74 jruoho #endif
984 1.58 haad
985 1.58 haad static int
986 1.58 haad cgd_modcmd(modcmd_t cmd, void *arg)
987 1.58 haad {
988 1.74 jruoho int bmajor, cmajor, error = 0;
989 1.74 jruoho
990 1.74 jruoho bmajor = cmajor = -1;
991 1.74 jruoho
992 1.58 haad switch (cmd) {
993 1.58 haad case MODULE_CMD_INIT:
994 1.74 jruoho #ifdef _MODULE
995 1.66 dyoung error = config_cfdriver_attach(&cgd_cd);
996 1.66 dyoung if (error)
997 1.66 dyoung break;
998 1.66 dyoung
999 1.66 dyoung error = config_cfattach_attach(cgd_cd.cd_name, &cgd_ca);
1000 1.66 dyoung if (error) {
1001 1.66 dyoung config_cfdriver_detach(&cgd_cd);
1002 1.66 dyoung aprint_error("%s: unable to register cfattach\n",
1003 1.66 dyoung cgd_cd.cd_name);
1004 1.66 dyoung break;
1005 1.66 dyoung }
1006 1.74 jruoho
1007 1.66 dyoung error = devsw_attach("cgd", &cgd_bdevsw, &bmajor,
1008 1.58 haad &cgd_cdevsw, &cmajor);
1009 1.66 dyoung if (error) {
1010 1.66 dyoung config_cfattach_detach(cgd_cd.cd_name, &cgd_ca);
1011 1.66 dyoung config_cfdriver_detach(&cgd_cd);
1012 1.66 dyoung break;
1013 1.66 dyoung }
1014 1.74 jruoho #endif
1015 1.58 haad break;
1016 1.58 haad
1017 1.58 haad case MODULE_CMD_FINI:
1018 1.74 jruoho #ifdef _MODULE
1019 1.66 dyoung error = config_cfattach_detach(cgd_cd.cd_name, &cgd_ca);
1020 1.66 dyoung if (error)
1021 1.66 dyoung break;
1022 1.66 dyoung config_cfdriver_detach(&cgd_cd);
1023 1.66 dyoung devsw_detach(&cgd_bdevsw, &cgd_cdevsw);
1024 1.74 jruoho #endif
1025 1.58 haad break;
1026 1.58 haad
1027 1.58 haad case MODULE_CMD_STAT:
1028 1.58 haad return ENOTTY;
1029 1.58 haad
1030 1.58 haad default:
1031 1.58 haad return ENOTTY;
1032 1.58 haad }
1033 1.58 haad
1034 1.58 haad return error;
1035 1.58 haad }
1036