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