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