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