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