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