Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/sys/dev The CryptoGraphic Disk Driver.
details: https://anonhg.NetBSD.org/src/rev/73c897127975
branches: trunk
changeset: 537682:73c897127975
user: elric <elric%NetBSD.org@localhost>
date: Fri Oct 04 18:22:35 2002 +0000
description:
The CryptoGraphic Disk Driver.
diffstat:
sys/dev/cgd.c | 754 +++++++++++++++++++++++++++++++++++++++++++++++++++
sys/dev/cgd_crypto.c | 460 +++++++++++++++++++++++++++++++
sys/dev/cgd_crypto.h | 56 +++
sys/dev/cgdvar.h | 82 +++++
4 files changed, 1352 insertions(+), 0 deletions(-)
diffs (truncated from 1368 to 300 lines):
diff -r 2f541ae88f0d -r 73c897127975 sys/dev/cgd.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/dev/cgd.c Fri Oct 04 18:22:35 2002 +0000
@@ -0,0 +1,754 @@
+/* $NetBSD: cgd.c,v 1.1 2002/10/04 18:22:35 elric Exp $ */
+
+/*-
+ * Copyright (c) 2002 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Roland C. Dowdeswell.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by the NetBSD
+ * Foundation, Inc. and its contributors.
+ * 4. Neither the name of The NetBSD Foundation nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: cgd.c,v 1.1 2002/10/04 18:22:35 elric Exp $");
+
+#include <sys/types.h>
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/proc.h>
+#include <sys/errno.h>
+#include <sys/buf.h>
+#include <sys/malloc.h>
+#include <sys/pool.h>
+#include <sys/ioctl.h>
+#include <sys/device.h>
+#include <sys/disk.h>
+#include <sys/disklabel.h>
+#include <sys/fcntl.h>
+#include <sys/vnode.h>
+#include <sys/lock.h>
+#include <sys/conf.h>
+
+#include <dev/dkvar.h>
+#include <dev/cgdvar.h>
+
+/* Entry Point Functions */
+
+void cgdattach(int);
+
+dev_type_open(cgdopen);
+dev_type_close(cgdclose);
+dev_type_read(cgdread);
+dev_type_write(cgdwrite);
+dev_type_ioctl(cgdioctl);
+dev_type_strategy(cgdstrategy);
+dev_type_dump(cgddump);
+dev_type_size(cgdsize);
+
+const struct bdevsw cgd_bdevsw = {
+ cgdopen, cgdclose, cgdstrategy, cgdioctl,
+ cgddump, cgdsize, D_DISK
+};
+
+const struct cdevsw cgd_cdevsw = {
+ cgdopen, cgdclose, cgdread, cgdwrite, cgdioctl,
+ nostop, notty, nopoll, nommap, D_DISK
+};
+
+/* Internal Functions */
+
+static void cgdstart(struct dk_softc *, struct buf *);
+static void cgdiodone(struct buf *);
+
+static int cgd_ioctl_set(struct cgd_softc *, void *, struct proc *);
+static int cgd_ioctl_clr(struct cgd_softc *, void *, struct proc *);
+static int cgdinit(struct cgd_softc *, char *, struct vnode *,
+ struct proc *);
+static void cgd_cipher(struct cgd_softc *, caddr_t, caddr_t,
+ size_t, daddr_t, size_t, int);
+
+/* Pseudo-disk Interface */
+
+static struct dk_intf the_dkintf = {
+ DTYPE_CGD,
+ "cgd",
+ cgdopen,
+ cgdclose,
+ cgdstrategy,
+ cgdstart,
+};
+static struct dk_intf *di = &the_dkintf;
+
+/* DIAGNOSTIC and DEBUG definitions */
+
+#if defined(CGDDEBUG) && !defined(DEBUG)
+#define DEBUG
+#endif
+
+#ifdef DEBUG
+int cgddebug = 0;
+
+#define CGDB_FOLLOW 0x1
+#define CGDB_IO 0x2
+#define CGDB_CRYPTO 0x4
+
+#define IFDEBUG(x,y) if (cgddebug & (x)) y
+#define DPRINTF(x,y) IFDEBUG(x, printf y)
+#define DPRINTF_FOLLOW(y) DPRINTF(CGDB_FOLLOW, y)
+
+static void hexprint(char *, void *, int);
+
+#else
+#define IFDEBUG(x,y)
+#define DPRINTF(x,y)
+#define DPRINTF_FOLLOW(y)
+#endif
+
+#ifdef DIAGNOSTIC
+#define DIAGPANIC(x) panic x
+#define DIAGCONDPANIC(x,y) if (x) panic y
+#else
+#define DIAGPANIC(x)
+#define DIAGCONDPANIC(x,y)
+#endif
+
+/* Component Buffer Pool structures and macros */
+
+struct cgdbuf {
+ struct buf cb_buf; /* new I/O buf */
+ struct buf *cb_obp; /* ptr. to original I/O buf */
+ struct cgd_softc *cb_sc; /* pointer to cgd softc */
+};
+
+struct pool cgd_cbufpool;
+
+#define CGD_GETBUF() pool_get(&cgd_cbufpool, PR_NOWAIT)
+#define CGD_PUTBUF(cbp) pool_put(&cgd_cbufpool, cbp)
+
+/* Global variables */
+
+struct cgd_softc *cgd_softc;
+int numcgd = 0;
+
+/* Utility Functions */
+
+#define CGDUNIT(x) DISKUNIT(x)
+#define GETCGD_SOFTC(_cs, x) if (!((_cs) = getcgd_softc(x))) return ENXIO
+
+static struct cgd_softc *
+getcgd_softc(dev_t dev)
+{
+ int unit = CGDUNIT(dev);
+
+ DPRINTF_FOLLOW(("getcgd_softc(0x%x): unit = %d\n", dev, unit));
+ if (unit >= numcgd)
+ return NULL;
+ return &cgd_softc[unit];
+}
+
+/* The code */
+
+static void
+cgdsoftc_init(struct cgd_softc *cs, int num)
+{
+ char buf[DK_XNAME_SIZE];
+
+ memset(cs, 0x0, sizeof(*cs));
+ snprintf(buf, DK_XNAME_SIZE, "cgd%d", num);
+ dk_sc_init(&cs->sc_dksc, cs, buf);
+}
+
+void
+cgdattach(int num)
+{
+ struct cgd_softc *cs;
+ int i;
+
+ DPRINTF_FOLLOW(("cgdattach(%d)\n", num));
+ if (num <= 0) {
+ DIAGPANIC(("cgdattach: count <= 0"));
+ return;
+ }
+
+ cgd_softc = (void *)malloc(num * sizeof(*cs), M_DEVBUF, M_NOWAIT);
+ if (!cs) {
+ printf("WARNING: unable to malloc(9) memory for crypt disks\n");
+ DIAGPANIC(("cgdattach: cannot malloc(9) enough memory"));
+ return;
+ }
+
+ numcgd = num;
+ for (i=0; i<num; i++)
+ cgdsoftc_init(&cgd_softc[i], i);
+
+ /* Init component buffer pool. XXX, can we put this in dksubr.c? */
+ pool_init(&cgd_cbufpool, sizeof(struct cgdbuf), 0, 0, 0,
+ "cgdpl", NULL);
+}
+
+int
+cgdopen(dev_t dev, int flags, int fmt, struct proc *p)
+{
+ struct cgd_softc *cs;
+
+ DPRINTF_FOLLOW(("cgdopen(%d, %d)\n", dev, flags));
+ GETCGD_SOFTC(cs, dev);
+ return dk_open(di, &cs->sc_dksc, dev, flags, fmt, p);
+}
+
+int
+cgdclose(dev_t dev, int flags, int fmt, struct proc *p)
+{
+ struct cgd_softc *cs;
+
+ DPRINTF_FOLLOW(("cgdclose(%d, %d)\n", dev, flags));
+ GETCGD_SOFTC(cs, dev);
+ return dk_close(di, &cs->sc_dksc, dev, flags, fmt, p);
+}
+
+void
+cgdstrategy(struct buf *bp)
+{
+ struct cgd_softc *cs = getcgd_softc(bp->b_dev);
+
+ DPRINTF_FOLLOW(("cgdstrategy(%p): b_bcount = %ld\n", bp,
+ (long)bp->b_bcount));
+ /* XXXrcd: Should we test for (cs != NULL)? */
+ dk_strategy(di, &cs->sc_dksc, bp);
+ return;
+}
+
+int
+cgdsize(dev_t dev)
+{
+ struct cgd_softc *cs = getcgd_softc(dev);
+
+ DPRINTF_FOLLOW(("cgdsize(%d)\n", dev));
+ if (!cs)
+ return -1;
+ return dk_size(di, &cs->sc_dksc, dev);
+}
+
+static void
+cgdstart(struct dk_softc *dksc, struct buf *bp)
+{
+ struct cgd_softc *cs = dksc->sc_osc;
+ struct cgdbuf *cbp;
+ struct partition *pp;
+ caddr_t addr;
+ caddr_t newaddr;
+ daddr_t bn;
+
+ DPRINTF_FOLLOW(("cgdstart(%p, %p)\n", dksc, bp));
+ disk_busy(&dksc->sc_dkdev); /* XXX: put in dksubr.c */
+
+ /* XXXrcd:
+ * Translate partition relative blocks to absolute blocks,
+ * this probably belongs (somehow) in dksubr.c, since it
+ * is independant of the underlying code... This will require
+ * that the interface be expanded slightly, though.
+ */
+ bn = bp->b_blkno;
+ if (DISKPART(bp->b_dev) != RAW_PART) {
+ pp = &cs->sc_dksc.sc_dkdev.dk_label->d_partitions[DISKPART(bp->b_dev)];
+ bn += pp->p_offset;
+ }
+
+ /*
+ * If we are writing, then we need to encrypt the outgoing
+ * block. In the best case scenario, we are able to allocate
+ * enough memory to encrypt the data in a new block, otherwise
+ * we encrypt it in place (noting we'll have to decrypt it after
+ * the write.)
+ */
+ newaddr = addr = bp->b_data;
+ if ((bp->b_flags & B_READ) == 0) {
+ newaddr = malloc(bp->b_bcount, M_DEVBUF, 0);
+ if (!newaddr)
+ newaddr = addr;
+ cgd_cipher(cs, newaddr, addr, bp->b_bcount, bn,
Home |
Main Index |
Thread Index |
Old Index