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