Home | History | Annotate | Line # | Download | only in dev
cgd.c revision 1.108.2.9
      1  1.108.2.9  pgoyette /* $NetBSD: cgd.c,v 1.108.2.9 2016/07/22 06:32:54 pgoyette 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.108.2.9  pgoyette __KERNEL_RCSID(0, "$NetBSD: cgd.c,v 1.108.2.9 2016/07/22 06:32:54 pgoyette 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.74    jruoho #include <sys/module.h>
     44        1.1     elric #include <sys/pool.h>
     45        1.1     elric #include <sys/ioctl.h>
     46        1.1     elric #include <sys/device.h>
     47        1.1     elric #include <sys/disk.h>
     48        1.1     elric #include <sys/disklabel.h>
     49        1.1     elric #include <sys/fcntl.h>
     50       1.71  dholland #include <sys/namei.h> /* for pathbuf */
     51        1.1     elric #include <sys/vnode.h>
     52        1.1     elric #include <sys/conf.h>
     53       1.62  christos #include <sys/syslog.h>
     54  1.108.2.1  pgoyette #include <sys/localcount.h>
     55        1.1     elric 
     56        1.1     elric #include <dev/dkvar.h>
     57        1.1     elric #include <dev/cgdvar.h>
     58        1.1     elric 
     59       1.88   hannken #include <miscfs/specfs/specdev.h> /* for v_rdev */
     60       1.88   hannken 
     61      1.102  christos #include "ioconf.h"
     62      1.102  christos 
     63        1.1     elric /* Entry Point Functions */
     64        1.1     elric 
     65       1.18   thorpej static dev_type_open(cgdopen);
     66       1.18   thorpej static dev_type_close(cgdclose);
     67       1.18   thorpej static dev_type_read(cgdread);
     68       1.18   thorpej static dev_type_write(cgdwrite);
     69       1.18   thorpej static dev_type_ioctl(cgdioctl);
     70       1.18   thorpej static dev_type_strategy(cgdstrategy);
     71       1.18   thorpej static dev_type_dump(cgddump);
     72       1.18   thorpej static dev_type_size(cgdsize);
     73        1.1     elric 
     74        1.1     elric const struct bdevsw cgd_bdevsw = {
     75  1.108.2.2  pgoyette 	LOCALCOUNT_INITIALIZER
     76       1.84  dholland 	.d_open = cgdopen,
     77       1.84  dholland 	.d_close = cgdclose,
     78       1.84  dholland 	.d_strategy = cgdstrategy,
     79       1.84  dholland 	.d_ioctl = cgdioctl,
     80       1.84  dholland 	.d_dump = cgddump,
     81       1.84  dholland 	.d_psize = cgdsize,
     82       1.89  dholland 	.d_discard = nodiscard,
     83       1.84  dholland 	.d_flag = D_DISK
     84        1.1     elric };
     85        1.1     elric 
     86        1.1     elric const struct cdevsw cgd_cdevsw = {
     87  1.108.2.2  pgoyette 	LOCALCOUNT_INITIALIZER
     88       1.84  dholland 	.d_open = cgdopen,
     89       1.84  dholland 	.d_close = cgdclose,
     90       1.84  dholland 	.d_read = cgdread,
     91       1.84  dholland 	.d_write = cgdwrite,
     92       1.84  dholland 	.d_ioctl = cgdioctl,
     93       1.84  dholland 	.d_stop = nostop,
     94       1.84  dholland 	.d_tty = notty,
     95       1.84  dholland 	.d_poll = nopoll,
     96       1.84  dholland 	.d_mmap = nommap,
     97       1.84  dholland 	.d_kqfilter = nokqfilter,
     98       1.90  dholland 	.d_discard = nodiscard,
     99       1.84  dholland 	.d_flag = D_DISK
    100        1.1     elric };
    101        1.1     elric 
    102       1.65    dyoung static int cgd_match(device_t, cfdata_t, void *);
    103       1.65    dyoung static void cgd_attach(device_t, device_t, void *);
    104       1.65    dyoung static int cgd_detach(device_t, int);
    105  1.108.2.6  pgoyette static struct cgd_softc	*cgd_spawn(int, device_t *);
    106       1.65    dyoung static int cgd_destroy(device_t);
    107       1.65    dyoung 
    108        1.1     elric /* Internal Functions */
    109        1.1     elric 
    110       1.99   mlelstv static int	cgd_diskstart(device_t, struct buf *);
    111        1.1     elric static void	cgdiodone(struct buf *);
    112      1.108  riastrad static int	cgd_dumpblocks(device_t, void *, daddr_t, int);
    113        1.1     elric 
    114       1.32  christos static int	cgd_ioctl_set(struct cgd_softc *, void *, struct lwp *);
    115       1.65    dyoung static int	cgd_ioctl_clr(struct cgd_softc *, struct lwp *);
    116       1.78  christos static int	cgd_ioctl_get(dev_t, void *, struct lwp *);
    117       1.27  drochner static int	cgdinit(struct cgd_softc *, const char *, struct vnode *,
    118       1.32  christos 			struct lwp *);
    119       1.44  christos static void	cgd_cipher(struct cgd_softc *, void *, void *,
    120        1.1     elric 			   size_t, daddr_t, size_t, int);
    121        1.1     elric 
    122       1.29      yamt static struct dkdriver cgddkdriver = {
    123       1.98   mlelstv         .d_minphys  = minphys,
    124       1.98   mlelstv         .d_open = cgdopen,
    125       1.98   mlelstv         .d_close = cgdclose,
    126       1.98   mlelstv         .d_strategy = cgdstrategy,
    127       1.98   mlelstv         .d_iosize = NULL,
    128       1.99   mlelstv         .d_diskstart = cgd_diskstart,
    129      1.108  riastrad         .d_dumpblocks = cgd_dumpblocks,
    130       1.98   mlelstv         .d_lastclose = NULL
    131       1.29      yamt };
    132       1.29      yamt 
    133       1.65    dyoung CFATTACH_DECL3_NEW(cgd, sizeof(struct cgd_softc),
    134       1.65    dyoung     cgd_match, cgd_attach, cgd_detach, NULL, NULL, NULL, DVF_DETACH_SHUTDOWN);
    135       1.65    dyoung extern struct cfdriver cgd_cd;
    136       1.65    dyoung 
    137        1.1     elric /* DIAGNOSTIC and DEBUG definitions */
    138        1.1     elric 
    139        1.1     elric #if defined(CGDDEBUG) && !defined(DEBUG)
    140        1.1     elric #define DEBUG
    141        1.1     elric #endif
    142        1.1     elric 
    143        1.1     elric #ifdef DEBUG
    144        1.1     elric int cgddebug = 0;
    145        1.1     elric 
    146        1.1     elric #define CGDB_FOLLOW	0x1
    147        1.1     elric #define CGDB_IO	0x2
    148        1.1     elric #define CGDB_CRYPTO	0x4
    149        1.1     elric 
    150        1.1     elric #define IFDEBUG(x,y)		if (cgddebug & (x)) y
    151        1.1     elric #define DPRINTF(x,y)		IFDEBUG(x, printf y)
    152        1.1     elric #define DPRINTF_FOLLOW(y)	DPRINTF(CGDB_FOLLOW, y)
    153        1.1     elric 
    154       1.26  drochner static void	hexprint(const char *, void *, int);
    155        1.1     elric 
    156        1.1     elric #else
    157        1.1     elric #define IFDEBUG(x,y)
    158        1.1     elric #define DPRINTF(x,y)
    159        1.1     elric #define DPRINTF_FOLLOW(y)
    160        1.1     elric #endif
    161        1.1     elric 
    162        1.1     elric #ifdef DIAGNOSTIC
    163       1.22     perry #define DIAGPANIC(x)		panic x
    164        1.1     elric #define DIAGCONDPANIC(x,y)	if (x) panic y
    165        1.1     elric #else
    166        1.1     elric #define DIAGPANIC(x)
    167        1.1     elric #define DIAGCONDPANIC(x,y)
    168        1.1     elric #endif
    169        1.1     elric 
    170        1.1     elric /* Global variables */
    171        1.1     elric 
    172        1.1     elric /* Utility Functions */
    173        1.1     elric 
    174        1.1     elric #define CGDUNIT(x)		DISKUNIT(x)
    175  1.108.2.5  pgoyette #define GETCGD_SOFTC(_cs, x, _dv)				\
    176  1.108.2.5  pgoyette 	if (!((_cs) = getcgd_softc(x, &_dv))) return ENXIO;
    177        1.1     elric 
    178       1.65    dyoung /* The code */
    179       1.65    dyoung 
    180  1.108.2.4  pgoyette static void
    181  1.108.2.4  pgoyette cgd_release(dev_t dev)
    182  1.108.2.4  pgoyette {
    183  1.108.2.4  pgoyette 	int unit = CGDUNIT(dev);
    184  1.108.2.4  pgoyette 	device_t self;
    185  1.108.2.4  pgoyette 
    186  1.108.2.4  pgoyette 	self = device_lookup_acquire(&cgd_cd, unit);
    187  1.108.2.4  pgoyette 	if (self != NULL)
    188  1.108.2.4  pgoyette 		device_release(self);
    189  1.108.2.4  pgoyette }
    190  1.108.2.4  pgoyette 
    191        1.1     elric static struct cgd_softc *
    192  1.108.2.5  pgoyette getcgd_softc(dev_t dev, device_t *self)
    193        1.1     elric {
    194        1.1     elric 	int	unit = CGDUNIT(dev);
    195       1.65    dyoung 	struct cgd_softc *sc;
    196        1.1     elric 
    197       1.56    cegger 	DPRINTF_FOLLOW(("getcgd_softc(0x%"PRIx64"): unit = %d\n", dev, unit));
    198       1.65    dyoung 
    199  1.108.2.5  pgoyette 	*self = device_lookup_acquire(&cgd_cd, unit);
    200  1.108.2.8  pgoyette 
    201  1.108.2.9  pgoyette 	if (*self == NULL) {
    202  1.108.2.7  pgoyette 		sc = cgd_spawn(unit, self);
    203  1.108.2.9  pgoyette 	} else {
    204  1.108.2.8  pgoyette 		sc = device_private(*self);
    205  1.108.2.9  pgoyette 	}
    206  1.108.2.8  pgoyette 
    207       1.65    dyoung 	return sc;
    208        1.1     elric }
    209        1.1     elric 
    210       1.65    dyoung static int
    211       1.65    dyoung cgd_match(device_t self, cfdata_t cfdata, void *aux)
    212       1.65    dyoung {
    213       1.65    dyoung 
    214       1.65    dyoung 	return 1;
    215       1.65    dyoung }
    216        1.1     elric 
    217        1.1     elric static void
    218       1.65    dyoung cgd_attach(device_t parent, device_t self, void *aux)
    219        1.1     elric {
    220       1.65    dyoung 	struct cgd_softc *sc = device_private(self);
    221        1.1     elric 
    222       1.85     skrll 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_BIO);
    223       1.98   mlelstv 	dk_init(&sc->sc_dksc, self, DKTYPE_CGD);
    224       1.65    dyoung 	disk_init(&sc->sc_dksc.sc_dkdev, sc->sc_dksc.sc_xname, &cgddkdriver);
    225       1.70     joerg 
    226       1.98   mlelstv 	if (!pmf_device_register(self, NULL, NULL))
    227      1.107   msaitoh 		aprint_error_dev(self,
    228      1.107   msaitoh 		    "unable to register power management hooks\n");
    229       1.65    dyoung }
    230       1.65    dyoung 
    231       1.65    dyoung 
    232       1.65    dyoung static int
    233       1.65    dyoung cgd_detach(device_t self, int flags)
    234       1.65    dyoung {
    235       1.67    dyoung 	int ret;
    236       1.67    dyoung 	const int pmask = 1 << RAW_PART;
    237       1.65    dyoung 	struct cgd_softc *sc = device_private(self);
    238       1.67    dyoung 	struct dk_softc *dksc = &sc->sc_dksc;
    239       1.67    dyoung 
    240       1.67    dyoung 	if (DK_BUSY(dksc, pmask))
    241       1.67    dyoung 		return EBUSY;
    242       1.65    dyoung 
    243       1.98   mlelstv 	if (DK_ATTACHED(dksc) &&
    244       1.67    dyoung 	    (ret = cgd_ioctl_clr(sc, curlwp)) != 0)
    245       1.67    dyoung 		return ret;
    246       1.65    dyoung 
    247       1.67    dyoung 	disk_destroy(&dksc->sc_dkdev);
    248       1.86  christos 	mutex_destroy(&sc->sc_lock);
    249       1.65    dyoung 
    250       1.67    dyoung 	return 0;
    251        1.1     elric }
    252        1.1     elric 
    253        1.1     elric void
    254        1.1     elric cgdattach(int num)
    255        1.1     elric {
    256       1.65    dyoung 	int error;
    257       1.65    dyoung 
    258       1.65    dyoung 	error = config_cfattach_attach(cgd_cd.cd_name, &cgd_ca);
    259       1.65    dyoung 	if (error != 0)
    260       1.65    dyoung 		aprint_error("%s: unable to register cfattach\n",
    261       1.65    dyoung 		    cgd_cd.cd_name);
    262       1.65    dyoung }
    263       1.65    dyoung 
    264       1.65    dyoung static struct cgd_softc *
    265  1.108.2.5  pgoyette cgd_spawn(int unit, device_t *self)
    266       1.65    dyoung {
    267       1.65    dyoung 	cfdata_t cf;
    268       1.65    dyoung 
    269       1.65    dyoung 	cf = malloc(sizeof(*cf), M_DEVBUF, M_WAITOK);
    270       1.65    dyoung 	cf->cf_name = cgd_cd.cd_name;
    271       1.65    dyoung 	cf->cf_atname = cgd_cd.cd_name;
    272       1.65    dyoung 	cf->cf_unit = unit;
    273       1.65    dyoung 	cf->cf_fstate = FSTATE_STAR;
    274       1.65    dyoung 
    275  1.108.2.4  pgoyette 	if (config_attach_pseudo(cf) == NULL)
    276  1.108.2.8  pgoyette { printf("%s: config_attach_pseudo() failed\n", __func__);
    277  1.108.2.4  pgoyette 		return NULL;
    278  1.108.2.8  pgoyette }
    279  1.108.2.4  pgoyette 
    280  1.108.2.5  pgoyette 	*self = device_lookup_acquire(&cgd_cd, unit);
    281  1.108.2.4  pgoyette 	if (self == NULL)
    282  1.108.2.4  pgoyette 		return NULL;
    283  1.108.2.4  pgoyette 	else
    284  1.108.2.4  pgoyette 		/*
    285  1.108.2.5  pgoyette 		 * Note that we return while still holding a reference
    286  1.108.2.5  pgoyette 		 * to the device!
    287  1.108.2.4  pgoyette 		 */
    288  1.108.2.5  pgoyette 		return device_private(*self);
    289       1.65    dyoung }
    290       1.65    dyoung 
    291       1.65    dyoung static int
    292       1.65    dyoung cgd_destroy(device_t dev)
    293       1.65    dyoung {
    294       1.65    dyoung 	int error;
    295       1.65    dyoung 	cfdata_t cf;
    296        1.1     elric 
    297       1.65    dyoung 	cf = device_cfdata(dev);
    298       1.65    dyoung 	error = config_detach(dev, DETACH_QUIET);
    299       1.65    dyoung 	if (error)
    300       1.65    dyoung 		return error;
    301       1.65    dyoung 	free(cf, M_DEVBUF);
    302       1.65    dyoung 	return 0;
    303        1.1     elric }
    304        1.1     elric 
    305       1.18   thorpej static int
    306       1.32  christos cgdopen(dev_t dev, int flags, int fmt, struct lwp *l)
    307        1.1     elric {
    308  1.108.2.5  pgoyette 	device_t self;
    309  1.108.2.5  pgoyette 	int	error;
    310        1.1     elric 	struct	cgd_softc *cs;
    311        1.1     elric 
    312       1.56    cegger 	DPRINTF_FOLLOW(("cgdopen(0x%"PRIx64", %d)\n", dev, flags));
    313  1.108.2.5  pgoyette 	GETCGD_SOFTC(cs, dev, self);
    314  1.108.2.5  pgoyette 	error = dk_open(&cs->sc_dksc, dev, flags, fmt, l);
    315  1.108.2.5  pgoyette 	device_release(self);
    316  1.108.2.5  pgoyette 	return error;
    317        1.1     elric }
    318        1.1     elric 
    319       1.18   thorpej static int
    320       1.32  christos cgdclose(dev_t dev, int flags, int fmt, struct lwp *l)
    321        1.1     elric {
    322       1.65    dyoung 	int error;
    323  1.108.2.5  pgoyette 	device_t self;
    324        1.1     elric 	struct	cgd_softc *cs;
    325       1.65    dyoung 	struct	dk_softc *dksc;
    326        1.1     elric 
    327       1.56    cegger 	DPRINTF_FOLLOW(("cgdclose(0x%"PRIx64", %d)\n", dev, flags));
    328  1.108.2.5  pgoyette 	GETCGD_SOFTC(cs, dev, self);
    329       1.65    dyoung 	dksc = &cs->sc_dksc;
    330  1.108.2.5  pgoyette 	if ((error =  dk_close(dksc, dev, flags, fmt, l)) != 0) {
    331  1.108.2.5  pgoyette 		device_release(self);
    332       1.65    dyoung 		return error;
    333  1.108.2.5  pgoyette 	}
    334       1.65    dyoung 
    335       1.98   mlelstv 	if (!DK_ATTACHED(dksc)) {
    336       1.77     elric 		if ((error = cgd_destroy(cs->sc_dksc.sc_dev)) != 0) {
    337       1.77     elric 			aprint_error_dev(dksc->sc_dev,
    338       1.65    dyoung 			    "unable to detach instance\n");
    339  1.108.2.5  pgoyette 			device_release(self);
    340       1.65    dyoung 			return error;
    341       1.65    dyoung 		}
    342       1.65    dyoung 	}
    343  1.108.2.5  pgoyette 	device_release(self);
    344  1.108.2.5  pgoyette 	return error;
    345        1.1     elric }
    346        1.1     elric 
    347       1.18   thorpej static void
    348        1.1     elric cgdstrategy(struct buf *bp)
    349        1.1     elric {
    350  1.108.2.5  pgoyette 	device_t self;
    351  1.108.2.5  pgoyette 	struct	cgd_softc *cs = getcgd_softc(bp->b_dev, &self);
    352      1.105   mlelstv 	struct	dk_softc *dksc = &cs->sc_dksc;
    353      1.105   mlelstv 	struct	disk_geom *dg = &dksc->sc_dkdev.dk_geom;
    354        1.1     elric 
    355        1.1     elric 	DPRINTF_FOLLOW(("cgdstrategy(%p): b_bcount = %ld\n", bp,
    356        1.1     elric 	    (long)bp->b_bcount));
    357       1.72  riastrad 
    358       1.72  riastrad 	/*
    359       1.72  riastrad 	 * Reject unaligned writes.  We can encrypt and decrypt only
    360       1.72  riastrad 	 * complete disk sectors, and we let the ciphers require their
    361       1.72  riastrad 	 * buffers to be aligned to 32-bit boundaries.
    362       1.72  riastrad 	 */
    363       1.72  riastrad 	if (bp->b_blkno < 0 ||
    364      1.105   mlelstv 	    (bp->b_bcount % dg->dg_secsize) != 0 ||
    365       1.72  riastrad 	    ((uintptr_t)bp->b_data & 3) != 0) {
    366       1.72  riastrad 		bp->b_error = EINVAL;
    367       1.72  riastrad 		bp->b_resid = bp->b_bcount;
    368       1.72  riastrad 		biodone(bp);
    369  1.108.2.4  pgoyette 		cgd_release(bp->b_dev);
    370  1.108.2.5  pgoyette 		device_release(self);
    371       1.72  riastrad 		return;
    372       1.72  riastrad 	}
    373       1.72  riastrad 
    374        1.1     elric 	/* XXXrcd: Should we test for (cs != NULL)? */
    375       1.98   mlelstv 	dk_strategy(&cs->sc_dksc, bp);
    376  1.108.2.4  pgoyette 	cgd_release(bp->b_dev);
    377  1.108.2.5  pgoyette 	device_release(self);
    378        1.1     elric 	return;
    379        1.1     elric }
    380        1.1     elric 
    381       1.18   thorpej static int
    382        1.1     elric cgdsize(dev_t dev)
    383        1.1     elric {
    384  1.108.2.4  pgoyette 	int retval;
    385  1.108.2.5  pgoyette 	device_t self;
    386  1.108.2.5  pgoyette 	struct cgd_softc *cs = getcgd_softc(dev, &self);
    387        1.1     elric 
    388       1.56    cegger 	DPRINTF_FOLLOW(("cgdsize(0x%"PRIx64")\n", dev));
    389        1.1     elric 	if (!cs)
    390  1.108.2.4  pgoyette 		retval = -1;
    391  1.108.2.4  pgoyette 	else
    392  1.108.2.4  pgoyette 		retval = dk_size(&cs->sc_dksc, dev);
    393  1.108.2.4  pgoyette 
    394  1.108.2.4  pgoyette 	cgd_release(dev);
    395  1.108.2.5  pgoyette 	device_release(self);
    396  1.108.2.4  pgoyette 	return retval;
    397        1.1     elric }
    398        1.1     elric 
    399       1.16     elric /*
    400       1.16     elric  * cgd_{get,put}data are functions that deal with getting a buffer
    401       1.16     elric  * for the new encrypted data.  We have a buffer per device so that
    402       1.16     elric  * we can ensure that we can always have a transaction in flight.
    403       1.16     elric  * We use this buffer first so that we have one less piece of
    404       1.16     elric  * malloc'ed data at any given point.
    405       1.16     elric  */
    406       1.16     elric 
    407       1.16     elric static void *
    408       1.16     elric cgd_getdata(struct dk_softc *dksc, unsigned long size)
    409       1.16     elric {
    410       1.77     elric 	struct	cgd_softc *cs = (struct cgd_softc *)dksc;
    411       1.44  christos 	void *	data = NULL;
    412       1.16     elric 
    413       1.85     skrll 	mutex_enter(&cs->sc_lock);
    414       1.16     elric 	if (cs->sc_data_used == 0) {
    415       1.16     elric 		cs->sc_data_used = 1;
    416       1.16     elric 		data = cs->sc_data;
    417       1.16     elric 	}
    418       1.85     skrll 	mutex_exit(&cs->sc_lock);
    419       1.16     elric 
    420       1.16     elric 	if (data)
    421       1.16     elric 		return data;
    422       1.16     elric 
    423       1.16     elric 	return malloc(size, M_DEVBUF, M_NOWAIT);
    424       1.16     elric }
    425       1.16     elric 
    426        1.1     elric static void
    427       1.44  christos cgd_putdata(struct dk_softc *dksc, void *data)
    428       1.16     elric {
    429       1.77     elric 	struct	cgd_softc *cs = (struct cgd_softc *)dksc;
    430       1.16     elric 
    431       1.16     elric 	if (data == cs->sc_data) {
    432       1.85     skrll 		mutex_enter(&cs->sc_lock);
    433       1.16     elric 		cs->sc_data_used = 0;
    434       1.85     skrll 		mutex_exit(&cs->sc_lock);
    435       1.16     elric 	} else {
    436       1.16     elric 		free(data, M_DEVBUF);
    437       1.16     elric 	}
    438       1.16     elric }
    439       1.16     elric 
    440       1.99   mlelstv static int
    441       1.99   mlelstv cgd_diskstart(device_t dev, struct buf *bp)
    442        1.1     elric {
    443       1.98   mlelstv 	struct	cgd_softc *cs = device_private(dev);
    444       1.98   mlelstv 	struct	dk_softc *dksc = &cs->sc_dksc;
    445      1.105   mlelstv 	struct	disk_geom *dg = &dksc->sc_dkdev.dk_geom;
    446       1.99   mlelstv 	struct	buf *nbp;
    447       1.44  christos 	void *	addr;
    448       1.44  christos 	void *	newaddr;
    449        1.1     elric 	daddr_t	bn;
    450       1.49        ad 	struct	vnode *vp;
    451        1.1     elric 
    452       1.99   mlelstv 	DPRINTF_FOLLOW(("cgd_diskstart(%p, %p)\n", dksc, bp));
    453        1.1     elric 
    454       1.99   mlelstv 	bn = bp->b_rawblkno;
    455       1.22     perry 
    456       1.99   mlelstv 	/*
    457       1.99   mlelstv 	 * We attempt to allocate all of our resources up front, so that
    458       1.99   mlelstv 	 * we can fail quickly if they are unavailable.
    459       1.99   mlelstv 	 */
    460       1.99   mlelstv 	nbp = getiobuf(cs->sc_tvn, false);
    461       1.99   mlelstv 	if (nbp == NULL)
    462       1.99   mlelstv 		return EAGAIN;
    463       1.16     elric 
    464       1.99   mlelstv 	/*
    465       1.99   mlelstv 	 * If we are writing, then we need to encrypt the outgoing
    466       1.99   mlelstv 	 * block into a new block of memory.
    467       1.99   mlelstv 	 */
    468       1.99   mlelstv 	newaddr = addr = bp->b_data;
    469       1.99   mlelstv 	if ((bp->b_flags & B_READ) == 0) {
    470       1.99   mlelstv 		newaddr = cgd_getdata(dksc, bp->b_bcount);
    471       1.99   mlelstv 		if (!newaddr) {
    472       1.99   mlelstv 			putiobuf(nbp);
    473       1.99   mlelstv 			return EAGAIN;
    474       1.16     elric 		}
    475       1.99   mlelstv 		cgd_cipher(cs, newaddr, addr, bp->b_bcount, bn,
    476      1.105   mlelstv 		    dg->dg_secsize, CGD_CIPHER_ENCRYPT);
    477       1.99   mlelstv 	}
    478        1.1     elric 
    479       1.99   mlelstv 	nbp->b_data = newaddr;
    480       1.99   mlelstv 	nbp->b_flags = bp->b_flags;
    481       1.99   mlelstv 	nbp->b_oflags = bp->b_oflags;
    482       1.99   mlelstv 	nbp->b_cflags = bp->b_cflags;
    483       1.99   mlelstv 	nbp->b_iodone = cgdiodone;
    484       1.99   mlelstv 	nbp->b_proc = bp->b_proc;
    485      1.105   mlelstv 	nbp->b_blkno = btodb(bn * dg->dg_secsize);
    486       1.99   mlelstv 	nbp->b_bcount = bp->b_bcount;
    487       1.99   mlelstv 	nbp->b_private = bp;
    488       1.99   mlelstv 
    489       1.99   mlelstv 	BIO_COPYPRIO(nbp, bp);
    490       1.99   mlelstv 
    491       1.99   mlelstv 	if ((nbp->b_flags & B_READ) == 0) {
    492       1.99   mlelstv 		vp = nbp->b_vp;
    493       1.99   mlelstv 		mutex_enter(vp->v_interlock);
    494       1.99   mlelstv 		vp->v_numoutput++;
    495       1.99   mlelstv 		mutex_exit(vp->v_interlock);
    496       1.17       dbj 	}
    497       1.99   mlelstv 	VOP_STRATEGY(cs->sc_tvn, nbp);
    498       1.99   mlelstv 
    499       1.99   mlelstv 	return 0;
    500        1.1     elric }
    501        1.1     elric 
    502       1.18   thorpej static void
    503       1.17       dbj cgdiodone(struct buf *nbp)
    504        1.1     elric {
    505  1.108.2.4  pgoyette 	dev_t dev;
    506  1.108.2.5  pgoyette 	device_t self;
    507       1.17       dbj 	struct	buf *obp = nbp->b_private;
    508  1.108.2.5  pgoyette 	struct	cgd_softc *cs = getcgd_softc(obp->b_dev, &self);
    509        1.1     elric 	struct	dk_softc *dksc = &cs->sc_dksc;
    510      1.105   mlelstv 	struct	disk_geom *dg = &dksc->sc_dkdev.dk_geom;
    511      1.105   mlelstv 	daddr_t	bn;
    512       1.22     perry 
    513       1.17       dbj 	KDASSERT(cs);
    514        1.1     elric 
    515       1.17       dbj 	DPRINTF_FOLLOW(("cgdiodone(%p)\n", nbp));
    516       1.20      yamt 	DPRINTF(CGDB_IO, ("cgdiodone: bp %p bcount %d resid %d\n",
    517        1.1     elric 	    obp, obp->b_bcount, obp->b_resid));
    518      1.107   msaitoh 	DPRINTF(CGDB_IO, (" dev 0x%"PRIx64", nbp %p bn %" PRId64
    519      1.107   msaitoh 	    " addr %p bcnt %d\n", nbp->b_dev, nbp, nbp->b_blkno, nbp->b_data,
    520      1.107   msaitoh 		nbp->b_bcount));
    521       1.46        ad 	if (nbp->b_error != 0) {
    522       1.46        ad 		obp->b_error = nbp->b_error;
    523       1.62  christos 		DPRINTF(CGDB_IO, ("%s: error %d\n", dksc->sc_xname,
    524       1.62  christos 		    obp->b_error));
    525        1.1     elric 	}
    526        1.1     elric 
    527       1.16     elric 	/* Perform the decryption if we are reading.
    528        1.1     elric 	 *
    529        1.1     elric 	 * Note: use the blocknumber from nbp, since it is what
    530        1.1     elric 	 *       we used to encrypt the blocks.
    531        1.1     elric 	 */
    532        1.1     elric 
    533      1.105   mlelstv 	if (nbp->b_flags & B_READ) {
    534      1.105   mlelstv 		bn = dbtob(nbp->b_blkno) / dg->dg_secsize;
    535        1.1     elric 		cgd_cipher(cs, obp->b_data, obp->b_data, obp->b_bcount,
    536      1.105   mlelstv 		    bn, dg->dg_secsize, CGD_CIPHER_DECRYPT);
    537      1.105   mlelstv 	}
    538        1.1     elric 
    539       1.16     elric 	/* If we allocated memory, free it now... */
    540        1.1     elric 	if (nbp->b_data != obp->b_data)
    541       1.16     elric 		cgd_putdata(dksc, nbp->b_data);
    542        1.1     elric 
    543       1.33      yamt 	putiobuf(nbp);
    544        1.1     elric 
    545      1.100   mlelstv 	/* Request is complete for whatever reason */
    546      1.100   mlelstv 	obp->b_resid = 0;
    547      1.100   mlelstv 	if (obp->b_error != 0)
    548      1.100   mlelstv 		obp->b_resid = obp->b_bcount;
    549      1.100   mlelstv 
    550  1.108.2.4  pgoyette 	/*
    551  1.108.2.4  pgoyette 	 * copy the dev_t, finish the disk operation, and release the
    552  1.108.2.8  pgoyette 	 * reference we're holding on to (from getcgd_softc() earlier)
    553  1.108.2.4  pgoyette 	 */
    554  1.108.2.4  pgoyette 	dev = obp->b_dev;
    555       1.99   mlelstv 	dk_done(dksc, obp);
    556  1.108.2.4  pgoyette 	cgd_release(dev);
    557  1.108.2.5  pgoyette 	device_release(self);
    558  1.108.2.4  pgoyette 
    559      1.101   mlelstv 	dk_start(dksc, NULL);
    560        1.1     elric }
    561        1.1     elric 
    562      1.108  riastrad static int
    563      1.108  riastrad cgd_dumpblocks(device_t dev, void *va, daddr_t blkno, int nblk)
    564      1.108  riastrad {
    565      1.108  riastrad 	struct cgd_softc *sc = device_private(dev);
    566      1.108  riastrad 	struct dk_softc *dksc = &sc->sc_dksc;
    567      1.108  riastrad 	struct disk_geom *dg = &dksc->sc_dkdev.dk_geom;
    568      1.108  riastrad 	size_t nbytes, blksize;
    569      1.108  riastrad 	void *buf;
    570      1.108  riastrad 	int error;
    571      1.108  riastrad 
    572      1.108  riastrad 	/*
    573      1.108  riastrad 	 * dk_dump gives us units of disklabel sectors.  Everything
    574      1.108  riastrad 	 * else in cgd uses units of diskgeom sectors.  These had
    575      1.108  riastrad 	 * better agree; otherwise we need to figure out how to convert
    576      1.108  riastrad 	 * between them.
    577      1.108  riastrad 	 */
    578      1.108  riastrad 	KASSERTMSG((dg->dg_secsize == dksc->sc_dkdev.dk_label->d_secsize),
    579      1.108  riastrad 	    "diskgeom secsize %"PRIu32" != disklabel secsize %"PRIu32,
    580      1.108  riastrad 	    dg->dg_secsize, dksc->sc_dkdev.dk_label->d_secsize);
    581      1.108  riastrad 	blksize = dg->dg_secsize;
    582      1.108  riastrad 
    583      1.108  riastrad 	/*
    584      1.108  riastrad 	 * Compute the number of bytes in this request, which dk_dump
    585      1.108  riastrad 	 * has `helpfully' converted to a number of blocks for us.
    586      1.108  riastrad 	 */
    587      1.108  riastrad 	nbytes = nblk*blksize;
    588      1.108  riastrad 
    589      1.108  riastrad 	/* Try to acquire a buffer to store the ciphertext.  */
    590      1.108  riastrad 	buf = cgd_getdata(dksc, nbytes);
    591      1.108  riastrad 	if (buf == NULL)
    592      1.108  riastrad 		/* Out of memory: give up.  */
    593      1.108  riastrad 		return ENOMEM;
    594      1.108  riastrad 
    595      1.108  riastrad 	/* Encrypt the caller's data into the temporary buffer.  */
    596      1.108  riastrad 	cgd_cipher(sc, buf, va, nbytes, blkno, blksize, CGD_CIPHER_ENCRYPT);
    597      1.108  riastrad 
    598      1.108  riastrad 	/* Pass it on to the underlying disk device.  */
    599      1.108  riastrad 	error = bdev_dump(sc->sc_tdev, blkno, buf, nbytes);
    600      1.108  riastrad 
    601      1.108  riastrad 	/* Release the buffer.  */
    602      1.108  riastrad 	cgd_putdata(dksc, buf);
    603      1.108  riastrad 
    604      1.108  riastrad 	/* Return any error from the underlying disk device.  */
    605      1.108  riastrad 	return error;
    606      1.108  riastrad }
    607      1.108  riastrad 
    608        1.1     elric /* XXX: we should probably put these into dksubr.c, mostly */
    609       1.18   thorpej static int
    610       1.40  christos cgdread(dev_t dev, struct uio *uio, int flags)
    611        1.1     elric {
    612  1.108.2.5  pgoyette 	device_t self;
    613  1.108.2.5  pgoyette 	int	error;
    614        1.1     elric 	struct	cgd_softc *cs;
    615        1.1     elric 	struct	dk_softc *dksc;
    616        1.1     elric 
    617       1.56    cegger 	DPRINTF_FOLLOW(("cgdread(0x%llx, %p, %d)\n",
    618       1.56    cegger 	    (unsigned long long)dev, uio, flags));
    619  1.108.2.5  pgoyette 	GETCGD_SOFTC(cs, dev, self);
    620        1.1     elric 	dksc = &cs->sc_dksc;
    621       1.98   mlelstv 	if (!DK_ATTACHED(dksc))
    622        1.1     elric 		return ENXIO;
    623  1.108.2.5  pgoyette 	error = physio(cgdstrategy, NULL, dev, B_READ, minphys, uio);
    624  1.108.2.5  pgoyette 	device_release(self);
    625  1.108.2.5  pgoyette 	return error;
    626        1.1     elric }
    627        1.1     elric 
    628        1.1     elric /* XXX: we should probably put these into dksubr.c, mostly */
    629       1.18   thorpej static int
    630       1.40  christos cgdwrite(dev_t dev, struct uio *uio, int flags)
    631        1.1     elric {
    632  1.108.2.5  pgoyette 	device_t self;
    633  1.108.2.5  pgoyette 	int	error;
    634        1.1     elric 	struct	cgd_softc *cs;
    635        1.1     elric 	struct	dk_softc *dksc;
    636        1.1     elric 
    637       1.56    cegger 	DPRINTF_FOLLOW(("cgdwrite(0x%"PRIx64", %p, %d)\n", dev, uio, flags));
    638  1.108.2.5  pgoyette 	GETCGD_SOFTC(cs, dev, self);
    639        1.1     elric 	dksc = &cs->sc_dksc;
    640  1.108.2.5  pgoyette 	if (!DK_ATTACHED(dksc)) {
    641  1.108.2.5  pgoyette 		device_release(self);
    642        1.1     elric 		return ENXIO;
    643  1.108.2.5  pgoyette 	}
    644  1.108.2.5  pgoyette 	error = physio(cgdstrategy, NULL, dev, B_WRITE, minphys, uio);
    645  1.108.2.5  pgoyette 	device_release(self);
    646  1.108.2.5  pgoyette 	return error;
    647        1.1     elric }
    648        1.1     elric 
    649       1.18   thorpej static int
    650       1.44  christos cgdioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    651        1.1     elric {
    652  1.108.2.5  pgoyette 	device_t self;
    653        1.1     elric 	struct	cgd_softc *cs;
    654        1.1     elric 	struct	dk_softc *dksc;
    655        1.1     elric 	int	part = DISKPART(dev);
    656        1.1     elric 	int	pmask = 1 << part;
    657  1.108.2.5  pgoyette 	int	error = 0;
    658        1.1     elric 
    659       1.56    cegger 	DPRINTF_FOLLOW(("cgdioctl(0x%"PRIx64", %ld, %p, %d, %p)\n",
    660       1.32  christos 	    dev, cmd, data, flag, l));
    661       1.78  christos 
    662        1.1     elric 	switch (cmd) {
    663       1.93  christos 	case CGDIOCGET:
    664       1.93  christos 		return cgd_ioctl_get(dev, data, l);
    665        1.1     elric 	case CGDIOCSET:
    666        1.1     elric 	case CGDIOCCLR:
    667        1.1     elric 		if ((flag & FWRITE) == 0)
    668        1.1     elric 			return EBADF;
    669       1.78  christos 		/* FALLTHROUGH */
    670       1.78  christos 	default:
    671  1.108.2.5  pgoyette 		GETCGD_SOFTC(cs, dev, self);
    672       1.78  christos 		dksc = &cs->sc_dksc;
    673       1.78  christos 		break;
    674        1.1     elric 	}
    675        1.1     elric 
    676        1.1     elric 	switch (cmd) {
    677        1.1     elric 	case CGDIOCSET:
    678       1.98   mlelstv 		if (DK_ATTACHED(dksc))
    679  1.108.2.5  pgoyette 			error = EBUSY;
    680  1.108.2.5  pgoyette 		else
    681  1.108.2.5  pgoyette 			cgd_ioctl_set(cs, data, l);
    682  1.108.2.5  pgoyette 		break;
    683        1.1     elric 	case CGDIOCCLR:
    684       1.65    dyoung 		if (DK_BUSY(&cs->sc_dksc, pmask))
    685  1.108.2.5  pgoyette 			error = EBUSY;
    686  1.108.2.5  pgoyette 		else
    687  1.108.2.5  pgoyette 			cgd_ioctl_clr(cs, l);
    688  1.108.2.5  pgoyette 		break;
    689       1.57       apb 	case DIOCCACHESYNC:
    690       1.57       apb 		/*
    691       1.57       apb 		 * XXX Do we really need to care about having a writable
    692       1.57       apb 		 * file descriptor here?
    693       1.57       apb 		 */
    694       1.57       apb 		if ((flag & FWRITE) == 0)
    695  1.108.2.5  pgoyette 			error = (EBADF);
    696       1.57       apb 
    697       1.57       apb 		/*
    698       1.57       apb 		 * We pass this call down to the underlying disk.
    699       1.57       apb 		 */
    700  1.108.2.5  pgoyette 		else
    701  1.108.2.5  pgoyette 			error = VOP_IOCTL(cs->sc_tvn, cmd, data, flag,
    702  1.108.2.5  pgoyette 			    l->l_cred);
    703  1.108.2.5  pgoyette 		break;
    704      1.103  christos 	case DIOCGSTRATEGY:
    705      1.103  christos 	case DIOCSSTRATEGY:
    706      1.104   mlelstv 		if (!DK_ATTACHED(dksc))
    707  1.108.2.5  pgoyette 			error = ENOENT;
    708      1.103  christos 		/*FALLTHROUGH*/
    709        1.1     elric 	default:
    710  1.108.2.5  pgoyette 		 if (error == 0)
    711  1.108.2.5  pgoyette 			error = dk_ioctl(dksc, dev, cmd, data, flag, l);
    712  1.108.2.5  pgoyette 		break;
    713       1.93  christos 	case CGDIOCGET:
    714       1.93  christos 		KASSERT(0);
    715  1.108.2.5  pgoyette 		error = EINVAL;
    716        1.1     elric 	}
    717  1.108.2.5  pgoyette 	device_release(self);
    718  1.108.2.5  pgoyette 	return error;
    719        1.1     elric }
    720        1.1     elric 
    721       1.18   thorpej static int
    722       1.44  christos cgddump(dev_t dev, daddr_t blkno, void *va, size_t size)
    723        1.1     elric {
    724  1.108.2.5  pgoyette 	device_t self;
    725  1.108.2.5  pgoyette 	int	error;
    726        1.1     elric 	struct	cgd_softc *cs;
    727        1.1     elric 
    728       1.56    cegger 	DPRINTF_FOLLOW(("cgddump(0x%"PRIx64", %" PRId64 ", %p, %lu)\n",
    729       1.56    cegger 	    dev, blkno, va, (unsigned long)size));
    730  1.108.2.5  pgoyette 	GETCGD_SOFTC(cs, dev, self);
    731  1.108.2.5  pgoyette 	error = dk_dump(&cs->sc_dksc, dev, blkno, va, size);
    732  1.108.2.5  pgoyette 	device_release(self);
    733  1.108.2.5  pgoyette 	return error;
    734        1.1     elric }
    735        1.1     elric 
    736        1.1     elric /*
    737        1.1     elric  * XXXrcd:
    738        1.1     elric  *  for now we hardcode the maximum key length.
    739        1.1     elric  */
    740        1.1     elric #define MAX_KEYSIZE	1024
    741        1.1     elric 
    742       1.53  christos static const struct {
    743       1.53  christos 	const char *n;
    744       1.53  christos 	int v;
    745       1.53  christos 	int d;
    746       1.53  christos } encblkno[] = {
    747       1.53  christos 	{ "encblkno",  CGD_CIPHER_CBC_ENCBLKNO8, 1 },
    748       1.53  christos 	{ "encblkno8", CGD_CIPHER_CBC_ENCBLKNO8, 1 },
    749       1.53  christos 	{ "encblkno1", CGD_CIPHER_CBC_ENCBLKNO1, 8 },
    750       1.53  christos };
    751       1.53  christos 
    752        1.1     elric /* ARGSUSED */
    753        1.1     elric static int
    754       1.32  christos cgd_ioctl_set(struct cgd_softc *cs, void *data, struct lwp *l)
    755        1.1     elric {
    756        1.1     elric 	struct	 cgd_ioctl *ci = data;
    757        1.1     elric 	struct	 vnode *vp;
    758        1.1     elric 	int	 ret;
    759       1.53  christos 	size_t	 i;
    760       1.43    cbiere 	size_t	 keybytes;			/* key length in bytes */
    761       1.27  drochner 	const char *cp;
    762       1.71  dholland 	struct pathbuf *pb;
    763       1.36  christos 	char	 *inbuf;
    764       1.80  christos 	struct dk_softc *dksc = &cs->sc_dksc;
    765        1.1     elric 
    766        1.1     elric 	cp = ci->ci_disk;
    767       1.71  dholland 
    768       1.71  dholland 	ret = pathbuf_copyin(ci->ci_disk, &pb);
    769       1.71  dholland 	if (ret != 0) {
    770       1.71  dholland 		return ret;
    771       1.71  dholland 	}
    772       1.71  dholland 	ret = dk_lookup(pb, l, &vp);
    773       1.71  dholland 	pathbuf_destroy(pb);
    774       1.71  dholland 	if (ret != 0) {
    775        1.1     elric 		return ret;
    776       1.71  dholland 	}
    777        1.1     elric 
    778       1.36  christos 	inbuf = malloc(MAX_KEYSIZE, M_TEMP, M_WAITOK);
    779       1.36  christos 
    780       1.32  christos 	if ((ret = cgdinit(cs, cp, vp, l)) != 0)
    781        1.1     elric 		goto bail;
    782        1.1     elric 
    783       1.36  christos 	(void)memset(inbuf, 0, MAX_KEYSIZE);
    784        1.1     elric 	ret = copyinstr(ci->ci_alg, inbuf, 256, NULL);
    785        1.1     elric 	if (ret)
    786        1.1     elric 		goto bail;
    787        1.1     elric 	cs->sc_cfuncs = cryptfuncs_find(inbuf);
    788        1.1     elric 	if (!cs->sc_cfuncs) {
    789        1.1     elric 		ret = EINVAL;
    790        1.1     elric 		goto bail;
    791        1.1     elric 	}
    792        1.1     elric 
    793       1.43    cbiere 	(void)memset(inbuf, 0, MAX_KEYSIZE);
    794       1.36  christos 	ret = copyinstr(ci->ci_ivmethod, inbuf, MAX_KEYSIZE, NULL);
    795        1.1     elric 	if (ret)
    796        1.1     elric 		goto bail;
    797       1.53  christos 
    798       1.53  christos 	for (i = 0; i < __arraycount(encblkno); i++)
    799       1.53  christos 		if (strcmp(encblkno[i].n, inbuf) == 0)
    800       1.53  christos 			break;
    801       1.53  christos 
    802       1.53  christos 	if (i == __arraycount(encblkno)) {
    803        1.1     elric 		ret = EINVAL;
    804        1.1     elric 		goto bail;
    805        1.1     elric 	}
    806        1.1     elric 
    807       1.15       dan 	keybytes = ci->ci_keylen / 8 + 1;
    808       1.15       dan 	if (keybytes > MAX_KEYSIZE) {
    809        1.1     elric 		ret = EINVAL;
    810        1.1     elric 		goto bail;
    811        1.1     elric 	}
    812       1.53  christos 
    813       1.36  christos 	(void)memset(inbuf, 0, MAX_KEYSIZE);
    814       1.15       dan 	ret = copyin(ci->ci_key, inbuf, keybytes);
    815        1.1     elric 	if (ret)
    816        1.1     elric 		goto bail;
    817        1.1     elric 
    818        1.1     elric 	cs->sc_cdata.cf_blocksize = ci->ci_blocksize;
    819       1.53  christos 	cs->sc_cdata.cf_mode = encblkno[i].v;
    820       1.78  christos 	cs->sc_cdata.cf_keylen = ci->ci_keylen;
    821        1.1     elric 	cs->sc_cdata.cf_priv = cs->sc_cfuncs->cf_init(ci->ci_keylen, inbuf,
    822        1.1     elric 	    &cs->sc_cdata.cf_blocksize);
    823       1.62  christos 	if (cs->sc_cdata.cf_blocksize > CGD_MAXBLOCKSIZE) {
    824       1.62  christos 	    log(LOG_WARNING, "cgd: Disallowed cipher with blocksize %zu > %u\n",
    825       1.63  christos 		cs->sc_cdata.cf_blocksize, CGD_MAXBLOCKSIZE);
    826       1.62  christos 	    cs->sc_cdata.cf_priv = NULL;
    827       1.62  christos 	}
    828       1.78  christos 
    829       1.53  christos 	/*
    830       1.53  christos 	 * The blocksize is supposed to be in bytes. Unfortunately originally
    831       1.53  christos 	 * it was expressed in bits. For compatibility we maintain encblkno
    832       1.53  christos 	 * and encblkno8.
    833       1.53  christos 	 */
    834       1.53  christos 	cs->sc_cdata.cf_blocksize /= encblkno[i].d;
    835       1.97  riastrad 	(void)explicit_memset(inbuf, 0, MAX_KEYSIZE);
    836        1.1     elric 	if (!cs->sc_cdata.cf_priv) {
    837        1.1     elric 		ret = EINVAL;		/* XXX is this the right error? */
    838        1.1     elric 		goto bail;
    839        1.1     elric 	}
    840       1.36  christos 	free(inbuf, M_TEMP);
    841        1.1     elric 
    842       1.80  christos 	bufq_alloc(&dksc->sc_bufq, "fcfs", 0);
    843       1.16     elric 
    844       1.16     elric 	cs->sc_data = malloc(MAXPHYS, M_DEVBUF, M_WAITOK);
    845       1.16     elric 	cs->sc_data_used = 0;
    846       1.16     elric 
    847       1.98   mlelstv 	/* Attach the disk. */
    848       1.98   mlelstv 	dk_attach(dksc);
    849       1.98   mlelstv 	disk_attach(&dksc->sc_dkdev);
    850        1.1     elric 
    851       1.80  christos 	disk_set_info(dksc->sc_dev, &dksc->sc_dkdev, NULL);
    852       1.77     elric 
    853       1.29      yamt 	/* Discover wedges on this disk. */
    854       1.80  christos 	dkwedge_discover(&dksc->sc_dkdev);
    855       1.29      yamt 
    856        1.1     elric 	return 0;
    857        1.1     elric 
    858        1.1     elric bail:
    859       1.36  christos 	free(inbuf, M_TEMP);
    860       1.51        ad 	(void)vn_close(vp, FREAD|FWRITE, l->l_cred);
    861        1.1     elric 	return ret;
    862        1.1     elric }
    863        1.1     elric 
    864        1.1     elric /* ARGSUSED */
    865        1.1     elric static int
    866       1.65    dyoung cgd_ioctl_clr(struct cgd_softc *cs, struct lwp *l)
    867        1.1     elric {
    868       1.80  christos 	struct	dk_softc *dksc = &cs->sc_dksc;
    869       1.65    dyoung 
    870       1.98   mlelstv 	if (!DK_ATTACHED(dksc))
    871       1.65    dyoung 		return ENXIO;
    872       1.16     elric 
    873       1.29      yamt 	/* Delete all of our wedges. */
    874       1.80  christos 	dkwedge_delall(&dksc->sc_dkdev);
    875       1.29      yamt 
    876       1.16     elric 	/* Kill off any queued buffers. */
    877      1.104   mlelstv 	dk_drain(dksc);
    878       1.80  christos 	bufq_free(dksc->sc_bufq);
    879        1.1     elric 
    880       1.51        ad 	(void)vn_close(cs->sc_tvn, FREAD|FWRITE, l->l_cred);
    881        1.1     elric 	cs->sc_cfuncs->cf_destroy(cs->sc_cdata.cf_priv);
    882        1.1     elric 	free(cs->sc_tpath, M_DEVBUF);
    883       1.16     elric 	free(cs->sc_data, M_DEVBUF);
    884       1.16     elric 	cs->sc_data_used = 0;
    885       1.98   mlelstv 	dk_detach(dksc);
    886       1.80  christos 	disk_detach(&dksc->sc_dkdev);
    887        1.1     elric 
    888        1.1     elric 	return 0;
    889        1.1     elric }
    890        1.1     elric 
    891        1.1     elric static int
    892       1.78  christos cgd_ioctl_get(dev_t dev, void *data, struct lwp *l)
    893       1.78  christos {
    894  1.108.2.5  pgoyette 	device_t self;
    895  1.108.2.5  pgoyette 	struct cgd_softc *cs = getcgd_softc(dev, &self);
    896       1.78  christos 	struct cgd_user *cgu;
    897       1.78  christos 	int unit;
    898       1.80  christos 	struct	dk_softc *dksc = &cs->sc_dksc;
    899       1.78  christos 
    900       1.78  christos 	unit = CGDUNIT(dev);
    901       1.78  christos 	cgu = (struct cgd_user *)data;
    902       1.78  christos 
    903       1.78  christos 	DPRINTF_FOLLOW(("cgd_ioctl_get(0x%"PRIx64", %d, %p, %p)\n",
    904       1.78  christos 			   dev, unit, data, l));
    905       1.78  christos 
    906       1.78  christos 	if (cgu->cgu_unit == -1)
    907       1.78  christos 		cgu->cgu_unit = unit;
    908       1.78  christos 
    909  1.108.2.4  pgoyette 	if (cgu->cgu_unit < 0) {
    910  1.108.2.4  pgoyette 		cgd_release(dev);
    911  1.108.2.5  pgoyette 		device_release(self);
    912       1.78  christos 		return EINVAL;	/* XXX: should this be ENXIO? */
    913  1.108.2.4  pgoyette 	}
    914       1.78  christos 
    915       1.78  christos 	cs = device_lookup_private(&cgd_cd, unit);
    916       1.98   mlelstv 	if (cs == NULL || !DK_ATTACHED(dksc)) {
    917       1.78  christos 		cgu->cgu_dev = 0;
    918       1.78  christos 		cgu->cgu_alg[0] = '\0';
    919       1.78  christos 		cgu->cgu_blocksize = 0;
    920       1.78  christos 		cgu->cgu_mode = 0;
    921       1.78  christos 		cgu->cgu_keylen = 0;
    922       1.78  christos 	}
    923       1.78  christos 	else {
    924       1.78  christos 		cgu->cgu_dev = cs->sc_tdev;
    925       1.78  christos 		strlcpy(cgu->cgu_alg, cs->sc_cfuncs->cf_name,
    926       1.78  christos 		    sizeof(cgu->cgu_alg));
    927       1.78  christos 		cgu->cgu_blocksize = cs->sc_cdata.cf_blocksize;
    928       1.78  christos 		cgu->cgu_mode = cs->sc_cdata.cf_mode;
    929       1.78  christos 		cgu->cgu_keylen = cs->sc_cdata.cf_keylen;
    930       1.78  christos 	}
    931  1.108.2.4  pgoyette 	cgd_release(dev);
    932  1.108.2.5  pgoyette 	device_release(self);
    933       1.78  christos 	return 0;
    934       1.78  christos }
    935       1.78  christos 
    936       1.78  christos static int
    937       1.27  drochner cgdinit(struct cgd_softc *cs, const char *cpath, struct vnode *vp,
    938       1.32  christos 	struct lwp *l)
    939        1.1     elric {
    940       1.80  christos 	struct	disk_geom *dg;
    941        1.1     elric 	int	ret;
    942       1.36  christos 	char	*tmppath;
    943       1.76  christos 	uint64_t psize;
    944       1.76  christos 	unsigned secsize;
    945       1.80  christos 	struct dk_softc *dksc = &cs->sc_dksc;
    946        1.1     elric 
    947        1.1     elric 	cs->sc_tvn = vp;
    948       1.36  christos 	cs->sc_tpath = NULL;
    949        1.1     elric 
    950       1.36  christos 	tmppath = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
    951        1.1     elric 	ret = copyinstr(cpath, tmppath, MAXPATHLEN, &cs->sc_tpathlen);
    952        1.1     elric 	if (ret)
    953        1.1     elric 		goto bail;
    954        1.1     elric 	cs->sc_tpath = malloc(cs->sc_tpathlen, M_DEVBUF, M_WAITOK);
    955        1.1     elric 	memcpy(cs->sc_tpath, tmppath, cs->sc_tpathlen);
    956        1.1     elric 
    957       1.88   hannken 	cs->sc_tdev = vp->v_rdev;
    958        1.1     elric 
    959       1.76  christos 	if ((ret = getdisksize(vp, &psize, &secsize)) != 0)
    960        1.1     elric 		goto bail;
    961        1.1     elric 
    962       1.76  christos 	if (psize == 0) {
    963        1.1     elric 		ret = ENODEV;
    964        1.1     elric 		goto bail;
    965        1.1     elric 	}
    966        1.1     elric 
    967        1.1     elric 	/*
    968        1.1     elric 	 * XXX here we should probe the underlying device.  If we
    969        1.1     elric 	 *     are accessing a partition of type RAW_PART, then
    970        1.1     elric 	 *     we should populate our initial geometry with the
    971        1.1     elric 	 *     geometry that we discover from the device.
    972        1.1     elric 	 */
    973       1.80  christos 	dg = &dksc->sc_dkdev.dk_geom;
    974       1.80  christos 	memset(dg, 0, sizeof(*dg));
    975       1.80  christos 	dg->dg_secperunit = psize;
    976      1.105   mlelstv 	dg->dg_secsize = secsize;
    977       1.80  christos 	dg->dg_ntracks = 1;
    978      1.105   mlelstv 	dg->dg_nsectors = 1024 * 1024 / dg->dg_secsize;
    979       1.80  christos 	dg->dg_ncylinders = dg->dg_secperunit / dg->dg_nsectors;
    980        1.1     elric 
    981        1.1     elric bail:
    982       1.36  christos 	free(tmppath, M_TEMP);
    983        1.1     elric 	if (ret && cs->sc_tpath)
    984        1.1     elric 		free(cs->sc_tpath, M_DEVBUF);
    985        1.1     elric 	return ret;
    986        1.1     elric }
    987        1.1     elric 
    988        1.1     elric /*
    989        1.1     elric  * Our generic cipher entry point.  This takes care of the
    990        1.1     elric  * IV mode and passes off the work to the specific cipher.
    991        1.1     elric  * We implement here the IV method ``encrypted block
    992        1.1     elric  * number''.
    993       1.22     perry  *
    994        1.1     elric  * For the encryption case, we accomplish this by setting
    995        1.1     elric  * up a struct uio where the first iovec of the source is
    996        1.1     elric  * the blocknumber and the first iovec of the dest is a
    997        1.1     elric  * sink.  We then call the cipher with an IV of zero, and
    998        1.1     elric  * the right thing happens.
    999       1.22     perry  *
   1000        1.1     elric  * For the decryption case, we use the same basic mechanism
   1001        1.1     elric  * for symmetry, but we encrypt the block number in the
   1002        1.1     elric  * first iovec.
   1003        1.1     elric  *
   1004        1.1     elric  * We mainly do this to avoid requiring the definition of
   1005        1.1     elric  * an ECB mode.
   1006        1.1     elric  *
   1007        1.1     elric  * XXXrcd: for now we rely on our own crypto framework defined
   1008        1.1     elric  *         in dev/cgd_crypto.c.  This will change when we
   1009        1.1     elric  *         get a generic kernel crypto framework.
   1010        1.1     elric  */
   1011        1.1     elric 
   1012        1.1     elric static void
   1013       1.25   xtraeme blkno2blkno_buf(char *sbuf, daddr_t blkno)
   1014        1.1     elric {
   1015        1.1     elric 	int	i;
   1016        1.1     elric 
   1017        1.1     elric 	/* Set up the blkno in blkno_buf, here we do not care much
   1018        1.1     elric 	 * about the final layout of the information as long as we
   1019        1.1     elric 	 * can guarantee that each sector will have a different IV
   1020        1.1     elric 	 * and that the endianness of the machine will not affect
   1021        1.1     elric 	 * the representation that we have chosen.
   1022        1.1     elric 	 *
   1023        1.1     elric 	 * We choose this representation, because it does not rely
   1024        1.1     elric 	 * on the size of buf (which is the blocksize of the cipher),
   1025        1.1     elric 	 * but allows daddr_t to grow without breaking existing
   1026        1.1     elric 	 * disks.
   1027        1.1     elric 	 *
   1028        1.1     elric 	 * Note that blkno2blkno_buf does not take a size as input,
   1029        1.1     elric 	 * and hence must be called on a pre-zeroed buffer of length
   1030        1.1     elric 	 * greater than or equal to sizeof(daddr_t).
   1031        1.1     elric 	 */
   1032        1.1     elric 	for (i=0; i < sizeof(daddr_t); i++) {
   1033       1.25   xtraeme 		*sbuf++ = blkno & 0xff;
   1034        1.1     elric 		blkno >>= 8;
   1035        1.1     elric 	}
   1036        1.1     elric }
   1037        1.1     elric 
   1038        1.1     elric static void
   1039       1.44  christos cgd_cipher(struct cgd_softc *cs, void *dstv, void *srcv,
   1040       1.44  christos     size_t len, daddr_t blkno, size_t secsize, int dir)
   1041        1.1     elric {
   1042       1.44  christos 	char		*dst = dstv;
   1043       1.44  christos 	char 		*src = srcv;
   1044        1.1     elric 	cfunc_cipher	*cipher = cs->sc_cfuncs->cf_cipher;
   1045        1.1     elric 	struct uio	dstuio;
   1046        1.1     elric 	struct uio	srcuio;
   1047        1.1     elric 	struct iovec	dstiov[2];
   1048        1.1     elric 	struct iovec	srciov[2];
   1049       1.42  christos 	size_t		blocksize = cs->sc_cdata.cf_blocksize;
   1050      1.105   mlelstv 	size_t		todo;
   1051       1.62  christos 	char		sink[CGD_MAXBLOCKSIZE];
   1052       1.62  christos 	char		zero_iv[CGD_MAXBLOCKSIZE];
   1053       1.62  christos 	char		blkno_buf[CGD_MAXBLOCKSIZE];
   1054        1.1     elric 
   1055        1.1     elric 	DPRINTF_FOLLOW(("cgd_cipher() dir=%d\n", dir));
   1056        1.1     elric 
   1057       1.22     perry 	DIAGCONDPANIC(len % blocksize != 0,
   1058        1.1     elric 	    ("cgd_cipher: len %% blocksize != 0"));
   1059        1.1     elric 
   1060        1.1     elric 	/* ensure that sizeof(daddr_t) <= blocksize (for encblkno IVing) */
   1061        1.1     elric 	DIAGCONDPANIC(sizeof(daddr_t) > blocksize,
   1062        1.1     elric 	    ("cgd_cipher: sizeof(daddr_t) > blocksize"));
   1063        1.1     elric 
   1064       1.64  christos 	memset(zero_iv, 0x0, blocksize);
   1065        1.1     elric 
   1066        1.1     elric 	dstuio.uio_iov = dstiov;
   1067        1.1     elric 	dstuio.uio_iovcnt = 2;
   1068        1.1     elric 
   1069        1.1     elric 	srcuio.uio_iov = srciov;
   1070        1.1     elric 	srcuio.uio_iovcnt = 2;
   1071        1.1     elric 
   1072        1.1     elric 	dstiov[0].iov_base = sink;
   1073        1.1     elric 	dstiov[0].iov_len  = blocksize;
   1074        1.1     elric 	srciov[0].iov_base = blkno_buf;
   1075        1.1     elric 	srciov[0].iov_len  = blocksize;
   1076        1.1     elric 
   1077      1.105   mlelstv 	for (; len > 0; len -= todo) {
   1078      1.105   mlelstv 		todo = MIN(len, secsize);
   1079      1.105   mlelstv 
   1080        1.1     elric 		dstiov[1].iov_base = dst;
   1081        1.1     elric 		srciov[1].iov_base = src;
   1082      1.105   mlelstv 		dstiov[1].iov_len  = todo;
   1083      1.105   mlelstv 		srciov[1].iov_len  = todo;
   1084        1.1     elric 
   1085       1.64  christos 		memset(blkno_buf, 0x0, blocksize);
   1086        1.1     elric 		blkno2blkno_buf(blkno_buf, blkno);
   1087        1.1     elric 		if (dir == CGD_CIPHER_DECRYPT) {
   1088        1.1     elric 			dstuio.uio_iovcnt = 1;
   1089        1.1     elric 			srcuio.uio_iovcnt = 1;
   1090        1.1     elric 			IFDEBUG(CGDB_CRYPTO, hexprint("step 0: blkno_buf",
   1091       1.64  christos 			    blkno_buf, blocksize));
   1092        1.1     elric 			cipher(cs->sc_cdata.cf_priv, &dstuio, &srcuio,
   1093        1.1     elric 			    zero_iv, CGD_CIPHER_ENCRYPT);
   1094        1.1     elric 			memcpy(blkno_buf, sink, blocksize);
   1095        1.1     elric 			dstuio.uio_iovcnt = 2;
   1096        1.1     elric 			srcuio.uio_iovcnt = 2;
   1097        1.1     elric 		}
   1098        1.1     elric 
   1099        1.1     elric 		IFDEBUG(CGDB_CRYPTO, hexprint("step 1: blkno_buf",
   1100       1.64  christos 		    blkno_buf, blocksize));
   1101        1.1     elric 		cipher(cs->sc_cdata.cf_priv, &dstuio, &srcuio, zero_iv, dir);
   1102        1.1     elric 		IFDEBUG(CGDB_CRYPTO, hexprint("step 2: sink",
   1103       1.64  christos 		    sink, blocksize));
   1104        1.1     elric 
   1105      1.105   mlelstv 		dst += todo;
   1106      1.105   mlelstv 		src += todo;
   1107        1.1     elric 		blkno++;
   1108        1.1     elric 	}
   1109        1.1     elric }
   1110        1.1     elric 
   1111        1.1     elric #ifdef DEBUG
   1112        1.1     elric static void
   1113       1.26  drochner hexprint(const char *start, void *buf, int len)
   1114        1.1     elric {
   1115        1.1     elric 	char	*c = buf;
   1116        1.1     elric 
   1117        1.1     elric 	DIAGCONDPANIC(len < 0, ("hexprint: called with len < 0"));
   1118        1.1     elric 	printf("%s: len=%06d 0x", start, len);
   1119        1.1     elric 	while (len--)
   1120       1.43    cbiere 		printf("%02x", (unsigned char) *c++);
   1121        1.1     elric }
   1122        1.1     elric #endif
   1123       1.58      haad 
   1124       1.83  pgoyette MODULE(MODULE_CLASS_DRIVER, cgd, "dk_subr");
   1125       1.74    jruoho 
   1126       1.58      haad #ifdef _MODULE
   1127  1.108.2.4  pgoyette #include "ioconf.c"
   1128       1.74    jruoho #endif
   1129       1.58      haad 
   1130       1.58      haad static int
   1131       1.58      haad cgd_modcmd(modcmd_t cmd, void *arg)
   1132       1.58      haad {
   1133       1.82    martin 	int error = 0;
   1134       1.74    jruoho 
   1135       1.82    martin #ifdef _MODULE
   1136       1.91    justin 	devmajor_t bmajor = -1, cmajor = -1;
   1137       1.82    martin #endif
   1138       1.74    jruoho 
   1139       1.58      haad 	switch (cmd) {
   1140       1.58      haad 	case MODULE_CMD_INIT:
   1141       1.74    jruoho #ifdef _MODULE
   1142  1.108.2.3  pgoyette 		/*
   1143  1.108.2.3  pgoyette 		 * Insert the driver into the autoconf database
   1144  1.108.2.3  pgoyette 		 */
   1145  1.108.2.3  pgoyette 		error = config_init_component(cfdriver_ioconf_cgd,
   1146  1.108.2.3  pgoyette                     cfattach_ioconf_cgd, cfdata_ioconf_cgd);
   1147  1.108.2.3  pgoyette 		if (error) {
   1148  1.108.2.4  pgoyette 			aprint_error("%s: unable to init component"
   1149  1.108.2.4  pgoyette 			    ", error %d", cgd_cd.cd_name, error);
   1150       1.66    dyoung 			break;
   1151       1.66    dyoung 		}
   1152       1.74    jruoho 
   1153  1.108.2.3  pgoyette 		/*
   1154  1.108.2.3  pgoyette 		 * Attach the {b,c}devsw's
   1155  1.108.2.3  pgoyette 		 */
   1156       1.66    dyoung 		error = devsw_attach("cgd", &cgd_bdevsw, &bmajor,
   1157       1.58      haad 		    &cgd_cdevsw, &cmajor);
   1158  1.108.2.3  pgoyette 
   1159  1.108.2.3  pgoyette 		/*
   1160  1.108.2.3  pgoyette 		 * If devsw_attach fails, remove from autoconf database
   1161  1.108.2.3  pgoyette 		 */
   1162       1.66    dyoung 		if (error) {
   1163  1.108.2.3  pgoyette 			config_fini_component(cfdriver_ioconf_cgd,
   1164  1.108.2.3  pgoyette 			    cfattach_ioconf_cgd, cfdata_ioconf_cgd);
   1165  1.108.2.4  pgoyette 			aprint_error("%s: unable to attach devsw"
   1166  1.108.2.4  pgoyette 				    ", error %d", cgd_cd.cd_name, error);
   1167       1.66    dyoung 		}
   1168       1.74    jruoho #endif
   1169       1.58      haad 		break;
   1170       1.58      haad 
   1171       1.58      haad 	case MODULE_CMD_FINI:
   1172       1.74    jruoho #ifdef _MODULE
   1173  1.108.2.3  pgoyette 		/*
   1174  1.108.2.3  pgoyette 		 * Remove {b,c}devsw's
   1175  1.108.2.3  pgoyette 		 */
   1176       1.66    dyoung 		devsw_detach(&cgd_bdevsw, &cgd_cdevsw);
   1177  1.108.2.3  pgoyette 
   1178  1.108.2.3  pgoyette 		/*
   1179  1.108.2.3  pgoyette 		 * Now remove device from autoconf database
   1180  1.108.2.3  pgoyette 		 */
   1181  1.108.2.3  pgoyette 		error = config_fini_component(cfdriver_ioconf_cgd,
   1182  1.108.2.3  pgoyette 		    cfattach_ioconf_cgd, cfdata_ioconf_cgd);
   1183  1.108.2.3  pgoyette 
   1184  1.108.2.3  pgoyette 		/*
   1185  1.108.2.3  pgoyette 		 * If removal fails, re-attach our {b,c}devsw's
   1186  1.108.2.3  pgoyette 		 */
   1187  1.108.2.4  pgoyette 		if (error) {
   1188  1.108.2.4  pgoyette 			aprint_error("%s: failed to remove from autoconf"
   1189  1.108.2.4  pgoyette 			    ", error %d", cgd_cd.cd_name, error);
   1190  1.108.2.3  pgoyette 			devsw_attach("cgd", &cgd_bdevsw, &bmajor,
   1191  1.108.2.3  pgoyette 			    &cgd_cdevsw, &cmajor);
   1192  1.108.2.4  pgoyette 		}
   1193       1.74    jruoho #endif
   1194       1.58      haad 		break;
   1195       1.58      haad 
   1196       1.58      haad 	case MODULE_CMD_STAT:
   1197  1.108.2.5  pgoyette 		error = ENOTTY;
   1198  1.108.2.5  pgoyette 		break;
   1199       1.58      haad 	default:
   1200  1.108.2.5  pgoyette 		error = ENOTTY;
   1201  1.108.2.5  pgoyette 		break;
   1202       1.58      haad 	}
   1203       1.58      haad 
   1204       1.58      haad 	return error;
   1205       1.58      haad }
   1206