cgd.c revision 1.13 1 1.13 yamt /* $NetBSD: cgd.c,v 1.13 2004/01/10 14:39:50 yamt 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 * 3. All advertising materials mentioning features or use of this software
19 1.1 elric * must display the following acknowledgement:
20 1.1 elric * This product includes software developed by the NetBSD
21 1.1 elric * Foundation, Inc. and its contributors.
22 1.1 elric * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 elric * contributors may be used to endorse or promote products derived
24 1.1 elric * from this software without specific prior written permission.
25 1.1 elric *
26 1.1 elric * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 elric * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 elric * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 elric * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 elric * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 elric * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 elric * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 elric * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 elric * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 elric * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 elric * POSSIBILITY OF SUCH DAMAGE.
37 1.1 elric */
38 1.1 elric
39 1.1 elric #include <sys/cdefs.h>
40 1.13 yamt __KERNEL_RCSID(0, "$NetBSD: cgd.c,v 1.13 2004/01/10 14:39:50 yamt Exp $");
41 1.1 elric
42 1.1 elric #include <sys/types.h>
43 1.1 elric #include <sys/param.h>
44 1.1 elric #include <sys/systm.h>
45 1.1 elric #include <sys/proc.h>
46 1.1 elric #include <sys/errno.h>
47 1.1 elric #include <sys/buf.h>
48 1.1 elric #include <sys/malloc.h>
49 1.1 elric #include <sys/pool.h>
50 1.1 elric #include <sys/ioctl.h>
51 1.1 elric #include <sys/device.h>
52 1.1 elric #include <sys/disk.h>
53 1.1 elric #include <sys/disklabel.h>
54 1.1 elric #include <sys/fcntl.h>
55 1.1 elric #include <sys/vnode.h>
56 1.1 elric #include <sys/lock.h>
57 1.1 elric #include <sys/conf.h>
58 1.1 elric
59 1.1 elric #include <dev/dkvar.h>
60 1.1 elric #include <dev/cgdvar.h>
61 1.1 elric
62 1.1 elric /* Entry Point Functions */
63 1.1 elric
64 1.1 elric void cgdattach(int);
65 1.1 elric
66 1.1 elric dev_type_open(cgdopen);
67 1.1 elric dev_type_close(cgdclose);
68 1.1 elric dev_type_read(cgdread);
69 1.1 elric dev_type_write(cgdwrite);
70 1.1 elric dev_type_ioctl(cgdioctl);
71 1.1 elric dev_type_strategy(cgdstrategy);
72 1.1 elric dev_type_dump(cgddump);
73 1.1 elric dev_type_size(cgdsize);
74 1.1 elric
75 1.1 elric const struct bdevsw cgd_bdevsw = {
76 1.1 elric cgdopen, cgdclose, cgdstrategy, cgdioctl,
77 1.1 elric cgddump, cgdsize, D_DISK
78 1.1 elric };
79 1.1 elric
80 1.1 elric const struct cdevsw cgd_cdevsw = {
81 1.1 elric cgdopen, cgdclose, cgdread, cgdwrite, cgdioctl,
82 1.4 jdolecek nostop, notty, nopoll, nommap, nokqfilter, D_DISK
83 1.1 elric };
84 1.1 elric
85 1.1 elric /* Internal Functions */
86 1.1 elric
87 1.1 elric static void cgdstart(struct dk_softc *, struct buf *);
88 1.1 elric static void cgdiodone(struct buf *);
89 1.1 elric
90 1.12 fvdl static int cgd_ioctl_set(struct cgd_softc *, void *, struct proc *);
91 1.12 fvdl static int cgd_ioctl_clr(struct cgd_softc *, void *, struct proc *);
92 1.1 elric static int cgdinit(struct cgd_softc *, char *, struct vnode *,
93 1.12 fvdl struct proc *);
94 1.1 elric static void cgd_cipher(struct cgd_softc *, caddr_t, caddr_t,
95 1.1 elric size_t, daddr_t, size_t, int);
96 1.1 elric
97 1.1 elric /* Pseudo-disk Interface */
98 1.1 elric
99 1.1 elric static struct dk_intf the_dkintf = {
100 1.1 elric DTYPE_CGD,
101 1.1 elric "cgd",
102 1.1 elric cgdopen,
103 1.1 elric cgdclose,
104 1.1 elric cgdstrategy,
105 1.1 elric cgdstart,
106 1.1 elric };
107 1.1 elric static struct dk_intf *di = &the_dkintf;
108 1.1 elric
109 1.1 elric /* DIAGNOSTIC and DEBUG definitions */
110 1.1 elric
111 1.1 elric #if defined(CGDDEBUG) && !defined(DEBUG)
112 1.1 elric #define DEBUG
113 1.1 elric #endif
114 1.1 elric
115 1.1 elric #ifdef DEBUG
116 1.1 elric int cgddebug = 0;
117 1.1 elric
118 1.1 elric #define CGDB_FOLLOW 0x1
119 1.1 elric #define CGDB_IO 0x2
120 1.1 elric #define CGDB_CRYPTO 0x4
121 1.1 elric
122 1.1 elric #define IFDEBUG(x,y) if (cgddebug & (x)) y
123 1.1 elric #define DPRINTF(x,y) IFDEBUG(x, printf y)
124 1.1 elric #define DPRINTF_FOLLOW(y) DPRINTF(CGDB_FOLLOW, y)
125 1.1 elric
126 1.1 elric static void hexprint(char *, void *, int);
127 1.1 elric
128 1.1 elric #else
129 1.1 elric #define IFDEBUG(x,y)
130 1.1 elric #define DPRINTF(x,y)
131 1.1 elric #define DPRINTF_FOLLOW(y)
132 1.1 elric #endif
133 1.1 elric
134 1.1 elric #ifdef DIAGNOSTIC
135 1.1 elric #define DIAGPANIC(x) panic x
136 1.1 elric #define DIAGCONDPANIC(x,y) if (x) panic y
137 1.1 elric #else
138 1.1 elric #define DIAGPANIC(x)
139 1.1 elric #define DIAGCONDPANIC(x,y)
140 1.1 elric #endif
141 1.1 elric
142 1.1 elric /* Component Buffer Pool structures and macros */
143 1.1 elric
144 1.1 elric struct cgdbuf {
145 1.1 elric struct buf cb_buf; /* new I/O buf */
146 1.1 elric struct buf *cb_obp; /* ptr. to original I/O buf */
147 1.1 elric struct cgd_softc *cb_sc; /* pointer to cgd softc */
148 1.1 elric };
149 1.1 elric
150 1.1 elric struct pool cgd_cbufpool;
151 1.1 elric
152 1.1 elric #define CGD_GETBUF() pool_get(&cgd_cbufpool, PR_NOWAIT)
153 1.1 elric #define CGD_PUTBUF(cbp) pool_put(&cgd_cbufpool, cbp)
154 1.1 elric
155 1.1 elric /* Global variables */
156 1.1 elric
157 1.1 elric struct cgd_softc *cgd_softc;
158 1.1 elric int numcgd = 0;
159 1.1 elric
160 1.1 elric /* Utility Functions */
161 1.1 elric
162 1.1 elric #define CGDUNIT(x) DISKUNIT(x)
163 1.1 elric #define GETCGD_SOFTC(_cs, x) if (!((_cs) = getcgd_softc(x))) return ENXIO
164 1.1 elric
165 1.1 elric static struct cgd_softc *
166 1.1 elric getcgd_softc(dev_t dev)
167 1.1 elric {
168 1.1 elric int unit = CGDUNIT(dev);
169 1.1 elric
170 1.1 elric DPRINTF_FOLLOW(("getcgd_softc(0x%x): unit = %d\n", dev, unit));
171 1.1 elric if (unit >= numcgd)
172 1.1 elric return NULL;
173 1.1 elric return &cgd_softc[unit];
174 1.1 elric }
175 1.1 elric
176 1.1 elric /* The code */
177 1.1 elric
178 1.1 elric static void
179 1.1 elric cgdsoftc_init(struct cgd_softc *cs, int num)
180 1.1 elric {
181 1.1 elric char buf[DK_XNAME_SIZE];
182 1.1 elric
183 1.1 elric memset(cs, 0x0, sizeof(*cs));
184 1.1 elric snprintf(buf, DK_XNAME_SIZE, "cgd%d", num);
185 1.1 elric dk_sc_init(&cs->sc_dksc, cs, buf);
186 1.1 elric }
187 1.1 elric
188 1.1 elric void
189 1.1 elric cgdattach(int num)
190 1.1 elric {
191 1.1 elric int i;
192 1.1 elric
193 1.1 elric DPRINTF_FOLLOW(("cgdattach(%d)\n", num));
194 1.1 elric if (num <= 0) {
195 1.1 elric DIAGPANIC(("cgdattach: count <= 0"));
196 1.1 elric return;
197 1.1 elric }
198 1.1 elric
199 1.10 agc cgd_softc = (void *)malloc(num * sizeof(*cgd_softc), M_DEVBUF, M_NOWAIT);
200 1.10 agc if (!cgd_softc) {
201 1.1 elric printf("WARNING: unable to malloc(9) memory for crypt disks\n");
202 1.1 elric DIAGPANIC(("cgdattach: cannot malloc(9) enough memory"));
203 1.1 elric return;
204 1.1 elric }
205 1.1 elric
206 1.1 elric numcgd = num;
207 1.1 elric for (i=0; i<num; i++)
208 1.1 elric cgdsoftc_init(&cgd_softc[i], i);
209 1.1 elric
210 1.1 elric /* Init component buffer pool. XXX, can we put this in dksubr.c? */
211 1.1 elric pool_init(&cgd_cbufpool, sizeof(struct cgdbuf), 0, 0, 0,
212 1.1 elric "cgdpl", NULL);
213 1.1 elric }
214 1.1 elric
215 1.1 elric int
216 1.12 fvdl cgdopen(dev_t dev, int flags, int fmt, struct proc *p)
217 1.1 elric {
218 1.1 elric struct cgd_softc *cs;
219 1.1 elric
220 1.1 elric DPRINTF_FOLLOW(("cgdopen(%d, %d)\n", dev, flags));
221 1.1 elric GETCGD_SOFTC(cs, dev);
222 1.12 fvdl return dk_open(di, &cs->sc_dksc, dev, flags, fmt, p);
223 1.1 elric }
224 1.1 elric
225 1.1 elric int
226 1.12 fvdl cgdclose(dev_t dev, int flags, int fmt, struct proc *p)
227 1.1 elric {
228 1.1 elric struct cgd_softc *cs;
229 1.1 elric
230 1.1 elric DPRINTF_FOLLOW(("cgdclose(%d, %d)\n", dev, flags));
231 1.1 elric GETCGD_SOFTC(cs, dev);
232 1.12 fvdl return dk_close(di, &cs->sc_dksc, dev, flags, fmt, p);
233 1.1 elric }
234 1.1 elric
235 1.1 elric void
236 1.1 elric cgdstrategy(struct buf *bp)
237 1.1 elric {
238 1.1 elric struct cgd_softc *cs = getcgd_softc(bp->b_dev);
239 1.1 elric
240 1.1 elric DPRINTF_FOLLOW(("cgdstrategy(%p): b_bcount = %ld\n", bp,
241 1.1 elric (long)bp->b_bcount));
242 1.1 elric /* XXXrcd: Should we test for (cs != NULL)? */
243 1.1 elric dk_strategy(di, &cs->sc_dksc, bp);
244 1.1 elric return;
245 1.1 elric }
246 1.1 elric
247 1.1 elric int
248 1.1 elric cgdsize(dev_t dev)
249 1.1 elric {
250 1.1 elric struct cgd_softc *cs = getcgd_softc(dev);
251 1.1 elric
252 1.1 elric DPRINTF_FOLLOW(("cgdsize(%d)\n", dev));
253 1.1 elric if (!cs)
254 1.1 elric return -1;
255 1.1 elric return dk_size(di, &cs->sc_dksc, dev);
256 1.1 elric }
257 1.1 elric
258 1.1 elric static void
259 1.1 elric cgdstart(struct dk_softc *dksc, struct buf *bp)
260 1.1 elric {
261 1.1 elric struct cgd_softc *cs = dksc->sc_osc;
262 1.1 elric struct cgdbuf *cbp;
263 1.1 elric struct partition *pp;
264 1.1 elric caddr_t addr;
265 1.1 elric caddr_t newaddr;
266 1.1 elric daddr_t bn;
267 1.1 elric
268 1.1 elric DPRINTF_FOLLOW(("cgdstart(%p, %p)\n", dksc, bp));
269 1.1 elric disk_busy(&dksc->sc_dkdev); /* XXX: put in dksubr.c */
270 1.1 elric
271 1.1 elric /* XXXrcd:
272 1.1 elric * Translate partition relative blocks to absolute blocks,
273 1.1 elric * this probably belongs (somehow) in dksubr.c, since it
274 1.1 elric * is independant of the underlying code... This will require
275 1.1 elric * that the interface be expanded slightly, though.
276 1.1 elric */
277 1.1 elric bn = bp->b_blkno;
278 1.1 elric if (DISKPART(bp->b_dev) != RAW_PART) {
279 1.1 elric pp = &cs->sc_dksc.sc_dkdev.dk_label->d_partitions[DISKPART(bp->b_dev)];
280 1.1 elric bn += pp->p_offset;
281 1.1 elric }
282 1.1 elric
283 1.1 elric /*
284 1.1 elric * If we are writing, then we need to encrypt the outgoing
285 1.1 elric * block. In the best case scenario, we are able to allocate
286 1.1 elric * enough memory to encrypt the data in a new block, otherwise
287 1.1 elric * we encrypt it in place (noting we'll have to decrypt it after
288 1.1 elric * the write.)
289 1.1 elric */
290 1.1 elric newaddr = addr = bp->b_data;
291 1.1 elric if ((bp->b_flags & B_READ) == 0) {
292 1.1 elric newaddr = malloc(bp->b_bcount, M_DEVBUF, 0);
293 1.1 elric if (!newaddr)
294 1.1 elric newaddr = addr;
295 1.1 elric cgd_cipher(cs, newaddr, addr, bp->b_bcount, bn,
296 1.1 elric DEV_BSIZE, CGD_CIPHER_ENCRYPT);
297 1.1 elric }
298 1.1 elric
299 1.1 elric cbp = CGD_GETBUF();
300 1.1 elric if (cbp == NULL) {
301 1.1 elric bp->b_error = ENOMEM;
302 1.1 elric bp->b_flags |= B_ERROR;
303 1.1 elric if (newaddr != addr)
304 1.1 elric free(newaddr, M_DEVBUF);
305 1.1 elric biodone(bp);
306 1.5 mrg disk_unbusy(&dksc->sc_dkdev, 0, (bp->b_flags & B_READ));
307 1.1 elric return;
308 1.1 elric }
309 1.8 thorpej BUF_INIT(&cbp->cb_buf);
310 1.1 elric cbp->cb_buf.b_data = newaddr;
311 1.1 elric cbp->cb_buf.b_flags = bp->b_flags | B_CALL;
312 1.1 elric cbp->cb_buf.b_iodone = cgdiodone;
313 1.1 elric cbp->cb_buf.b_proc = bp->b_proc;
314 1.1 elric cbp->cb_buf.b_dev = cs->sc_tdev;
315 1.1 elric cbp->cb_buf.b_blkno = bn;
316 1.1 elric cbp->cb_buf.b_vp = cs->sc_tvn;
317 1.1 elric cbp->cb_buf.b_bcount = bp->b_bcount;
318 1.1 elric
319 1.1 elric /* context for cgdiodone */
320 1.1 elric cbp->cb_obp = bp;
321 1.1 elric cbp->cb_sc = cs;
322 1.1 elric
323 1.13 yamt BIO_COPYPRIO(&cbp->cb_buf, bp);
324 1.13 yamt
325 1.1 elric if ((cbp->cb_buf.b_flags & B_READ) == 0)
326 1.1 elric cbp->cb_buf.b_vp->v_numoutput++;
327 1.1 elric VOP_STRATEGY(&cbp->cb_buf);
328 1.1 elric }
329 1.1 elric
330 1.1 elric void
331 1.1 elric cgdiodone(struct buf *vbp)
332 1.1 elric {
333 1.1 elric struct cgdbuf *cbp = (struct cgdbuf *)vbp;
334 1.1 elric struct buf *obp = cbp->cb_obp;
335 1.1 elric struct buf *nbp = &cbp->cb_buf;
336 1.1 elric struct cgd_softc *cs = cbp->cb_sc;
337 1.1 elric struct dk_softc *dksc = &cs->sc_dksc;
338 1.1 elric int s;
339 1.1 elric
340 1.1 elric DPRINTF_FOLLOW(("cgdiodone(%p)\n", vbp));
341 1.1 elric DPRINTF(CGDB_IO, ("cgdiodone: bp %p bcount %ld resid %ld\n",
342 1.1 elric obp, obp->b_bcount, obp->b_resid));
343 1.6 bouyer DPRINTF(CGDB_IO, (" dev 0x%x, cbp %p bn %" PRId64 " addr %p bcnt %ld\n",
344 1.1 elric cbp->cb_buf.b_dev, cbp, cbp->cb_buf.b_blkno, cbp->cb_buf.b_data,
345 1.1 elric cbp->cb_buf.b_bcount));
346 1.1 elric s = splbio();
347 1.1 elric if (nbp->b_flags & B_ERROR) {
348 1.1 elric obp->b_flags |= B_ERROR;
349 1.1 elric obp->b_error = nbp->b_error ? nbp->b_error : EIO;
350 1.1 elric
351 1.1 elric printf("%s: error %d\n", dksc->sc_xname, obp->b_error);
352 1.1 elric }
353 1.1 elric
354 1.1 elric /* Perform the decryption if we need to:
355 1.1 elric * o if we are reading, or
356 1.1 elric * o we wrote and couldn't allocate memory.
357 1.1 elric *
358 1.1 elric * Note: use the blocknumber from nbp, since it is what
359 1.1 elric * we used to encrypt the blocks.
360 1.1 elric */
361 1.1 elric
362 1.1 elric if (nbp->b_flags & B_READ || nbp->b_data == obp->b_data)
363 1.1 elric cgd_cipher(cs, obp->b_data, obp->b_data, obp->b_bcount,
364 1.1 elric nbp->b_blkno, DEV_BSIZE, CGD_CIPHER_DECRYPT);
365 1.1 elric
366 1.1 elric /* If we managed to allocate memory, free it now... */
367 1.1 elric if (nbp->b_data != obp->b_data)
368 1.1 elric free(nbp->b_data, M_DEVBUF);
369 1.1 elric
370 1.1 elric CGD_PUTBUF(cbp);
371 1.1 elric
372 1.1 elric /* Request is complete for whatever reason */
373 1.1 elric obp->b_resid = 0;
374 1.1 elric if (obp->b_flags & B_ERROR)
375 1.1 elric obp->b_resid = obp->b_bcount;
376 1.5 mrg disk_unbusy(&dksc->sc_dkdev, obp->b_bcount - obp->b_resid,
377 1.5 mrg (obp->b_flags & B_READ));
378 1.1 elric biodone(obp);
379 1.1 elric splx(s);
380 1.1 elric }
381 1.1 elric
382 1.1 elric /* XXX: we should probably put these into dksubr.c, mostly */
383 1.1 elric int
384 1.1 elric cgdread(dev_t dev, struct uio *uio, int flags)
385 1.1 elric {
386 1.1 elric struct cgd_softc *cs;
387 1.1 elric struct dk_softc *dksc;
388 1.1 elric
389 1.1 elric DPRINTF_FOLLOW(("cgdread(%d, %p, %d)\n", dev, uio, flags));
390 1.1 elric GETCGD_SOFTC(cs, dev);
391 1.1 elric dksc = &cs->sc_dksc;
392 1.1 elric if ((dksc->sc_flags & DKF_INITED) == 0)
393 1.1 elric return ENXIO;
394 1.1 elric /* XXX see the comments about minphys in ccd.c */
395 1.1 elric return physio(cgdstrategy, NULL, dev, B_READ, minphys, uio);
396 1.1 elric }
397 1.1 elric
398 1.1 elric /* XXX: we should probably put these into dksubr.c, mostly */
399 1.1 elric int
400 1.1 elric cgdwrite(dev_t dev, struct uio *uio, int flags)
401 1.1 elric {
402 1.1 elric struct cgd_softc *cs;
403 1.1 elric struct dk_softc *dksc;
404 1.1 elric
405 1.1 elric DPRINTF_FOLLOW(("cgdwrite(%d, %p, %d)\n", dev, uio, flags));
406 1.1 elric GETCGD_SOFTC(cs, dev);
407 1.1 elric dksc = &cs->sc_dksc;
408 1.1 elric if ((dksc->sc_flags & DKF_INITED) == 0)
409 1.1 elric return ENXIO;
410 1.1 elric /* XXX see the comments about minphys in ccd.c */
411 1.1 elric return physio(cgdstrategy, NULL, dev, B_WRITE, minphys, uio);
412 1.1 elric }
413 1.1 elric
414 1.1 elric int
415 1.12 fvdl cgdioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
416 1.1 elric {
417 1.1 elric struct cgd_softc *cs;
418 1.1 elric struct dk_softc *dksc;
419 1.1 elric int ret;
420 1.1 elric int part = DISKPART(dev);
421 1.1 elric int pmask = 1 << part;
422 1.1 elric
423 1.1 elric DPRINTF_FOLLOW(("cgdioctl(%d, %ld, %p, %d, %p)\n",
424 1.12 fvdl dev, cmd, data, flag, p));
425 1.1 elric GETCGD_SOFTC(cs, dev);
426 1.1 elric dksc = &cs->sc_dksc;
427 1.1 elric switch (cmd) {
428 1.1 elric case CGDIOCSET:
429 1.1 elric case CGDIOCCLR:
430 1.1 elric if ((flag & FWRITE) == 0)
431 1.1 elric return EBADF;
432 1.1 elric }
433 1.1 elric
434 1.1 elric if ((ret = lockmgr(&dksc->sc_lock, LK_EXCLUSIVE, NULL)) != 0)
435 1.1 elric return ret;
436 1.1 elric
437 1.1 elric switch (cmd) {
438 1.1 elric case CGDIOCSET:
439 1.1 elric if (dksc->sc_flags & DKF_INITED)
440 1.1 elric ret = EBUSY;
441 1.1 elric else
442 1.12 fvdl ret = cgd_ioctl_set(cs, data, p);
443 1.1 elric break;
444 1.1 elric case CGDIOCCLR:
445 1.1 elric if (!(dksc->sc_flags & DKF_INITED)) {
446 1.1 elric ret = ENXIO;
447 1.1 elric break;
448 1.1 elric }
449 1.1 elric if (DK_BUSY(&cs->sc_dksc, pmask)) {
450 1.1 elric ret = EBUSY;
451 1.1 elric break;
452 1.1 elric }
453 1.12 fvdl ret = cgd_ioctl_clr(cs, data, p);
454 1.1 elric break;
455 1.1 elric default:
456 1.12 fvdl ret = dk_ioctl(di, dksc, dev, cmd, data, flag, p);
457 1.1 elric break;
458 1.1 elric }
459 1.1 elric
460 1.1 elric lockmgr(&dksc->sc_lock, LK_RELEASE, NULL);
461 1.1 elric return ret;
462 1.1 elric }
463 1.1 elric
464 1.1 elric int
465 1.1 elric cgddump(dev_t dev, daddr_t blkno, caddr_t va, size_t size)
466 1.1 elric {
467 1.1 elric struct cgd_softc *cs;
468 1.1 elric
469 1.6 bouyer DPRINTF_FOLLOW(("cgddump(%d, %" PRId64 ", %p, %lu)\n", dev, blkno, va,
470 1.2 elric (unsigned long)size));
471 1.1 elric GETCGD_SOFTC(cs, dev);
472 1.1 elric return dk_dump(di, &cs->sc_dksc, dev, blkno, va, size);
473 1.1 elric }
474 1.1 elric
475 1.1 elric /*
476 1.1 elric * XXXrcd:
477 1.1 elric * for now we hardcode the maximum key length.
478 1.1 elric */
479 1.1 elric #define MAX_KEYSIZE 1024
480 1.1 elric
481 1.1 elric /* ARGSUSED */
482 1.1 elric static int
483 1.12 fvdl cgd_ioctl_set(struct cgd_softc *cs, void *data, struct proc *p)
484 1.1 elric {
485 1.1 elric struct cgd_ioctl *ci = data;
486 1.1 elric struct vnode *vp;
487 1.1 elric int ret;
488 1.1 elric char *cp;
489 1.1 elric char inbuf[MAX_KEYSIZE];
490 1.1 elric
491 1.1 elric cp = ci->ci_disk;
492 1.12 fvdl if ((ret = dk_lookup(cp, p, &vp)) != 0)
493 1.1 elric return ret;
494 1.1 elric
495 1.12 fvdl if ((ret = cgdinit(cs, cp, vp, p)) != 0)
496 1.1 elric goto bail;
497 1.1 elric
498 1.1 elric memset(inbuf, 0x0, sizeof(inbuf));
499 1.1 elric ret = copyinstr(ci->ci_alg, inbuf, 256, NULL);
500 1.1 elric if (ret)
501 1.1 elric goto bail;
502 1.1 elric cs->sc_cfuncs = cryptfuncs_find(inbuf);
503 1.1 elric if (!cs->sc_cfuncs) {
504 1.1 elric ret = EINVAL;
505 1.1 elric goto bail;
506 1.1 elric }
507 1.1 elric
508 1.1 elric /* right now we only support encblkno, so hard-code it */
509 1.1 elric memset(inbuf, 0x0, sizeof(inbuf));
510 1.1 elric ret = copyinstr(ci->ci_ivmethod, inbuf, sizeof(inbuf), NULL);
511 1.1 elric if (ret)
512 1.1 elric goto bail;
513 1.1 elric if (strcmp("encblkno", inbuf)) {
514 1.1 elric ret = EINVAL;
515 1.1 elric goto bail;
516 1.1 elric }
517 1.1 elric
518 1.1 elric if (ci->ci_keylen > MAX_KEYSIZE) {
519 1.1 elric ret = EINVAL;
520 1.1 elric goto bail;
521 1.1 elric }
522 1.1 elric memset(inbuf, 0x0, sizeof(inbuf));
523 1.1 elric ret = copyin(ci->ci_key, inbuf, ci->ci_keylen);
524 1.1 elric if (ret)
525 1.1 elric goto bail;
526 1.1 elric
527 1.1 elric cs->sc_cdata.cf_blocksize = ci->ci_blocksize;
528 1.1 elric cs->sc_cdata.cf_mode = CGD_CIPHER_CBC_ENCBLKNO;
529 1.1 elric cs->sc_cdata.cf_priv = cs->sc_cfuncs->cf_init(ci->ci_keylen, inbuf,
530 1.1 elric &cs->sc_cdata.cf_blocksize);
531 1.1 elric memset(inbuf, 0x0, sizeof(inbuf));
532 1.1 elric if (!cs->sc_cdata.cf_priv) {
533 1.1 elric printf("cgd: unable to initialize cipher\n");
534 1.1 elric ret = EINVAL; /* XXX is this the right error? */
535 1.1 elric goto bail;
536 1.1 elric }
537 1.1 elric
538 1.1 elric cs->sc_dksc.sc_flags |= DKF_INITED;
539 1.1 elric
540 1.1 elric /* Attach the disk. */
541 1.1 elric disk_attach(&cs->sc_dksc.sc_dkdev);
542 1.1 elric
543 1.1 elric /* Try and read the disklabel. */
544 1.1 elric dk_getdisklabel(di, &cs->sc_dksc, 0 /* XXX ? */);
545 1.1 elric
546 1.1 elric return 0;
547 1.1 elric
548 1.1 elric bail:
549 1.12 fvdl (void)vn_close(vp, FREAD|FWRITE, p->p_ucred, p);
550 1.1 elric return ret;
551 1.1 elric }
552 1.1 elric
553 1.1 elric /* ARGSUSED */
554 1.1 elric static int
555 1.12 fvdl cgd_ioctl_clr(struct cgd_softc *cs, void *data, struct proc *p)
556 1.1 elric {
557 1.1 elric
558 1.12 fvdl (void)vn_close(cs->sc_tvn, FREAD|FWRITE, p->p_ucred, p);
559 1.1 elric cs->sc_cfuncs->cf_destroy(cs->sc_cdata.cf_priv);
560 1.1 elric free(cs->sc_tpath, M_DEVBUF);
561 1.1 elric cs->sc_dksc.sc_flags &= ~DKF_INITED;
562 1.1 elric disk_detach(&cs->sc_dksc.sc_dkdev);
563 1.1 elric
564 1.1 elric return 0;
565 1.1 elric }
566 1.1 elric
567 1.1 elric static int
568 1.1 elric cgdinit(struct cgd_softc *cs, char *cpath, struct vnode *vp,
569 1.12 fvdl struct proc *p)
570 1.1 elric {
571 1.1 elric struct dk_geom *pdg;
572 1.1 elric struct partinfo dpart;
573 1.1 elric struct vattr va;
574 1.1 elric size_t size;
575 1.1 elric int maxsecsize = 0;
576 1.1 elric int ret;
577 1.1 elric char tmppath[MAXPATHLEN];
578 1.1 elric
579 1.1 elric cs->sc_dksc.sc_size = 0;
580 1.1 elric cs->sc_tvn = vp;
581 1.1 elric
582 1.1 elric memset(tmppath, 0x0, sizeof(tmppath));
583 1.1 elric ret = copyinstr(cpath, tmppath, MAXPATHLEN, &cs->sc_tpathlen);
584 1.1 elric if (ret)
585 1.1 elric goto bail;
586 1.1 elric cs->sc_tpath = malloc(cs->sc_tpathlen, M_DEVBUF, M_WAITOK);
587 1.1 elric memcpy(cs->sc_tpath, tmppath, cs->sc_tpathlen);
588 1.1 elric
589 1.12 fvdl if ((ret = VOP_GETATTR(vp, &va, p->p_ucred, p)) != 0)
590 1.1 elric goto bail;
591 1.1 elric
592 1.1 elric cs->sc_tdev = va.va_rdev;
593 1.1 elric
594 1.12 fvdl ret = VOP_IOCTL(vp, DIOCGPART, &dpart, FREAD, p->p_ucred, p);
595 1.1 elric if (ret)
596 1.1 elric goto bail;
597 1.1 elric
598 1.1 elric maxsecsize =
599 1.1 elric ((dpart.disklab->d_secsize > maxsecsize) ?
600 1.1 elric dpart.disklab->d_secsize : maxsecsize);
601 1.1 elric size = dpart.part->p_size;
602 1.1 elric
603 1.1 elric if (!size) {
604 1.1 elric ret = ENODEV;
605 1.1 elric goto bail;
606 1.1 elric }
607 1.1 elric
608 1.1 elric cs->sc_dksc.sc_size = size;
609 1.1 elric
610 1.1 elric /*
611 1.1 elric * XXX here we should probe the underlying device. If we
612 1.1 elric * are accessing a partition of type RAW_PART, then
613 1.1 elric * we should populate our initial geometry with the
614 1.1 elric * geometry that we discover from the device.
615 1.1 elric */
616 1.1 elric pdg = &cs->sc_dksc.sc_geom;
617 1.1 elric pdg->pdg_secsize = DEV_BSIZE;
618 1.1 elric pdg->pdg_ntracks = 1;
619 1.1 elric pdg->pdg_nsectors = 1024 * (1024 / pdg->pdg_secsize);
620 1.1 elric pdg->pdg_ncylinders = cs->sc_dksc.sc_size / pdg->pdg_nsectors;
621 1.1 elric
622 1.1 elric bail:
623 1.1 elric if (ret && cs->sc_tpath)
624 1.1 elric free(cs->sc_tpath, M_DEVBUF);
625 1.1 elric return ret;
626 1.1 elric }
627 1.1 elric
628 1.1 elric /*
629 1.1 elric * Our generic cipher entry point. This takes care of the
630 1.1 elric * IV mode and passes off the work to the specific cipher.
631 1.1 elric * We implement here the IV method ``encrypted block
632 1.1 elric * number''.
633 1.1 elric *
634 1.1 elric * For the encryption case, we accomplish this by setting
635 1.1 elric * up a struct uio where the first iovec of the source is
636 1.1 elric * the blocknumber and the first iovec of the dest is a
637 1.1 elric * sink. We then call the cipher with an IV of zero, and
638 1.1 elric * the right thing happens.
639 1.1 elric *
640 1.1 elric * For the decryption case, we use the same basic mechanism
641 1.1 elric * for symmetry, but we encrypt the block number in the
642 1.1 elric * first iovec.
643 1.1 elric *
644 1.1 elric * We mainly do this to avoid requiring the definition of
645 1.1 elric * an ECB mode.
646 1.1 elric *
647 1.1 elric * XXXrcd: for now we rely on our own crypto framework defined
648 1.1 elric * in dev/cgd_crypto.c. This will change when we
649 1.1 elric * get a generic kernel crypto framework.
650 1.1 elric */
651 1.1 elric
652 1.1 elric static void
653 1.1 elric blkno2blkno_buf(char *buf, daddr_t blkno)
654 1.1 elric {
655 1.1 elric int i;
656 1.1 elric
657 1.1 elric /* Set up the blkno in blkno_buf, here we do not care much
658 1.1 elric * about the final layout of the information as long as we
659 1.1 elric * can guarantee that each sector will have a different IV
660 1.1 elric * and that the endianness of the machine will not affect
661 1.1 elric * the representation that we have chosen.
662 1.1 elric *
663 1.1 elric * We choose this representation, because it does not rely
664 1.1 elric * on the size of buf (which is the blocksize of the cipher),
665 1.1 elric * but allows daddr_t to grow without breaking existing
666 1.1 elric * disks.
667 1.1 elric *
668 1.1 elric * Note that blkno2blkno_buf does not take a size as input,
669 1.1 elric * and hence must be called on a pre-zeroed buffer of length
670 1.1 elric * greater than or equal to sizeof(daddr_t).
671 1.1 elric */
672 1.1 elric for (i=0; i < sizeof(daddr_t); i++) {
673 1.1 elric *buf++ = blkno & 0xff;
674 1.1 elric blkno >>= 8;
675 1.1 elric }
676 1.1 elric }
677 1.1 elric
678 1.1 elric static void
679 1.1 elric cgd_cipher(struct cgd_softc *cs, caddr_t dst, caddr_t src,
680 1.1 elric size_t len, daddr_t blkno, size_t secsize, int dir)
681 1.1 elric {
682 1.1 elric cfunc_cipher *cipher = cs->sc_cfuncs->cf_cipher;
683 1.1 elric struct uio dstuio;
684 1.1 elric struct uio srcuio;
685 1.1 elric struct iovec dstiov[2];
686 1.1 elric struct iovec srciov[2];
687 1.1 elric int blocksize = cs->sc_cdata.cf_blocksize;
688 1.1 elric char sink[blocksize];
689 1.1 elric char zero_iv[blocksize];
690 1.1 elric char blkno_buf[blocksize];
691 1.1 elric
692 1.1 elric DPRINTF_FOLLOW(("cgd_cipher() dir=%d\n", dir));
693 1.1 elric
694 1.1 elric DIAGCONDPANIC(len % blocksize != 0,
695 1.1 elric ("cgd_cipher: len %% blocksize != 0"));
696 1.1 elric
697 1.1 elric /* ensure that sizeof(daddr_t) <= blocksize (for encblkno IVing) */
698 1.1 elric DIAGCONDPANIC(sizeof(daddr_t) > blocksize,
699 1.1 elric ("cgd_cipher: sizeof(daddr_t) > blocksize"));
700 1.1 elric
701 1.1 elric memset(zero_iv, 0x0, sizeof(zero_iv));
702 1.1 elric
703 1.1 elric dstuio.uio_iov = dstiov;
704 1.1 elric dstuio.uio_iovcnt = 2;
705 1.1 elric
706 1.1 elric srcuio.uio_iov = srciov;
707 1.1 elric srcuio.uio_iovcnt = 2;
708 1.1 elric
709 1.1 elric dstiov[0].iov_base = sink;
710 1.1 elric dstiov[0].iov_len = blocksize;
711 1.1 elric srciov[0].iov_base = blkno_buf;
712 1.1 elric srciov[0].iov_len = blocksize;
713 1.1 elric dstiov[1].iov_len = secsize;
714 1.1 elric srciov[1].iov_len = secsize;
715 1.1 elric
716 1.1 elric for (; len > 0; len -= secsize) {
717 1.1 elric dstiov[1].iov_base = dst;
718 1.1 elric srciov[1].iov_base = src;
719 1.1 elric
720 1.1 elric memset(blkno_buf, 0x0, sizeof(blkno_buf));
721 1.1 elric blkno2blkno_buf(blkno_buf, blkno);
722 1.1 elric if (dir == CGD_CIPHER_DECRYPT) {
723 1.1 elric dstuio.uio_iovcnt = 1;
724 1.1 elric srcuio.uio_iovcnt = 1;
725 1.1 elric IFDEBUG(CGDB_CRYPTO, hexprint("step 0: blkno_buf",
726 1.1 elric blkno_buf, sizeof(blkno_buf)));
727 1.1 elric cipher(cs->sc_cdata.cf_priv, &dstuio, &srcuio,
728 1.1 elric zero_iv, CGD_CIPHER_ENCRYPT);
729 1.1 elric memcpy(blkno_buf, sink, blocksize);
730 1.1 elric dstuio.uio_iovcnt = 2;
731 1.1 elric srcuio.uio_iovcnt = 2;
732 1.1 elric }
733 1.1 elric
734 1.1 elric IFDEBUG(CGDB_CRYPTO, hexprint("step 1: blkno_buf",
735 1.1 elric blkno_buf, sizeof(blkno_buf)));
736 1.1 elric cipher(cs->sc_cdata.cf_priv, &dstuio, &srcuio, zero_iv, dir);
737 1.1 elric IFDEBUG(CGDB_CRYPTO, hexprint("step 2: sink",
738 1.1 elric sink, sizeof(sink)));
739 1.1 elric
740 1.1 elric dst += secsize;
741 1.1 elric src += secsize;
742 1.1 elric blkno++;
743 1.1 elric }
744 1.1 elric }
745 1.1 elric
746 1.1 elric #ifdef DEBUG
747 1.1 elric static void
748 1.1 elric hexprint(char *start, void *buf, int len)
749 1.1 elric {
750 1.1 elric char *c = buf;
751 1.1 elric
752 1.1 elric DIAGCONDPANIC(len < 0, ("hexprint: called with len < 0"));
753 1.1 elric printf("%s: len=%06d 0x", start, len);
754 1.1 elric while (len--)
755 1.1 elric printf("%02x", (unsigned) *c++);
756 1.1 elric }
757 1.1 elric #endif
758