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