g_bde.c revision 112552
1105464Sphk/*- 2105464Sphk * Copyright (c) 2002 Poul-Henning Kamp 3105464Sphk * Copyright (c) 2002 Networks Associates Technology, Inc. 4105464Sphk * All rights reserved. 5105464Sphk * 6105464Sphk * This software was developed for the FreeBSD Project by Poul-Henning Kamp 7105464Sphk * and NAI Labs, the Security Research Division of Network Associates, Inc. 8105464Sphk * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the 9105464Sphk * DARPA CHATS research program. 10105464Sphk * 11105464Sphk * Redistribution and use in source and binary forms, with or without 12105464Sphk * modification, are permitted provided that the following conditions 13105464Sphk * are met: 14105464Sphk * 1. Redistributions of source code must retain the above copyright 15105464Sphk * notice, this list of conditions and the following disclaimer. 16105464Sphk * 2. Redistributions in binary form must reproduce the above copyright 17105464Sphk * notice, this list of conditions and the following disclaimer in the 18105464Sphk * documentation and/or other materials provided with the distribution. 19105464Sphk * 20105464Sphk * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 21105464Sphk * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22105464Sphk * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23105464Sphk * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 24105464Sphk * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25105464Sphk * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26105464Sphk * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27105464Sphk * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28105464Sphk * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29105464Sphk * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30105464Sphk * SUCH DAMAGE. 31105464Sphk * 32105464Sphk * $FreeBSD: head/sys/geom/bde/g_bde.c 112552 2003-03-24 19:30:15Z phk $ 33105464Sphk * 34105464Sphk */ 35105464Sphk 36105464Sphk#include <sys/param.h> 37105464Sphk#include <sys/bio.h> 38105464Sphk#include <sys/lock.h> 39105464Sphk#include <sys/mutex.h> 40105464Sphk#include <sys/malloc.h> 41105464Sphk#include <sys/systm.h> 42105464Sphk#include <sys/kernel.h> 43105464Sphk#include <sys/kthread.h> 44105464Sphk 45106407Sphk#include <crypto/rijndael/rijndael.h> 46106407Sphk#include <crypto/sha2/sha2.h> 47106407Sphk#include <geom/geom.h> 48106407Sphk#include <geom/bde/g_bde.h> 49105464Sphk#define BDE_CLASS_NAME "BDE" 50105464Sphk 51105464Sphkstatic void 52105464Sphkg_bde_start(struct bio *bp) 53105464Sphk{ 54105464Sphk struct g_geom *gp; 55105464Sphk struct g_consumer *cp; 56105464Sphk struct g_bde_softc *sc; 57105464Sphk 58105464Sphk gp = bp->bio_to->geom; 59105464Sphk cp = LIST_FIRST(&gp->consumer); 60105464Sphk sc = gp->softc; 61105464Sphk switch (bp->bio_cmd) { 62105464Sphk case BIO_DELETE: 63105464Sphk case BIO_READ: 64105464Sphk case BIO_WRITE: 65105464Sphk g_bde_start1(bp); 66105464Sphk break; 67105464Sphk case BIO_GETATTR: 68105464Sphk case BIO_SETATTR: 69105464Sphk g_io_deliver(bp, EOPNOTSUPP); 70105464Sphk break; 71105464Sphk default: 72105464Sphk g_io_deliver(bp, EOPNOTSUPP); 73105464Sphk return; 74105464Sphk } 75105464Sphk return; 76105464Sphk} 77105464Sphk 78105464Sphkstatic void 79105464Sphkg_bde_orphan(struct g_consumer *cp) 80105464Sphk{ 81105464Sphk struct g_geom *gp; 82105464Sphk struct g_provider *pp; 83105464Sphk struct g_bde_softc *sc; 84105464Sphk int error; 85105464Sphk 86105464Sphk g_trace(G_T_TOPOLOGY, "g_bde_orphan(%p/%s)", cp, cp->provider->name); 87105464Sphk g_topology_assert(); 88105464Sphk KASSERT(cp->provider->error != 0, 89105464Sphk ("g_bde_orphan with error == 0")); 90105464Sphk 91105464Sphk gp = cp->geom; 92105464Sphk sc = gp->softc; 93105464Sphk gp->flags |= G_GEOM_WITHER; 94105464Sphk error = cp->provider->error; 95105464Sphk LIST_FOREACH(pp, &gp->provider, provider) 96105464Sphk g_orphan_provider(pp, error); 97105464Sphk bzero(sc, sizeof(struct g_bde_softc)); /* destroy evidence */ 98105464Sphk return; 99105464Sphk} 100105464Sphk 101105464Sphkstatic int 102105464Sphkg_bde_access(struct g_provider *pp, int dr, int dw, int de) 103105464Sphk{ 104105464Sphk struct g_geom *gp; 105105464Sphk struct g_consumer *cp; 106105464Sphk 107105464Sphk gp = pp->geom; 108105464Sphk cp = LIST_FIRST(&gp->consumer); 109105464Sphk if (cp->acr == 0 && cp->acw == 0 && cp->ace == 0) { 110105464Sphk de++; 111105464Sphk dr++; 112105464Sphk } 113105464Sphk /* ... and let go of it on last close */ 114105464Sphk if ((cp->acr + dr) == 0 && (cp->acw + dw) == 0 && (cp->ace + de) == 1) { 115105464Sphk de--; 116105464Sphk dr--; 117105464Sphk } 118105464Sphk return (g_access_rel(cp, dr, dw, de)); 119105464Sphk} 120105464Sphk 121105464Sphkstatic int 122106518Sphkg_bde_config(struct g_configargs *ga) 123105464Sphk{ 124105464Sphk struct g_geom *gp; 125105464Sphk struct g_consumer *cp; 126105464Sphk struct g_provider *pp; 127105464Sphk struct g_bde_key *kp; 128105464Sphk int error; 129105464Sphk u_int sectorsize; 130105464Sphk off_t mediasize; 131105464Sphk struct g_bde_softc *sc; 132105464Sphk 133106518Sphk g_trace(G_T_TOPOLOGY, "g_bde_config(%d)", ga->flag); 134105464Sphk g_topology_assert(); 135105465Sphk gp = NULL; 136106518Sphk if (ga->flag == GCFG_DISMANTLE) { 137105464Sphk /* 138107831Sphk * Orderly detachment. 139105464Sphk */ 140105464Sphk if (ga->geom != NULL) { 141105464Sphk gp = ga->geom; 142105464Sphk } else if (ga->provider != NULL) { 143105464Sphk if (ga->provider->geom->class == ga->class) { 144105464Sphk gp = ga->provider->geom; 145105464Sphk } else { 146105464Sphk LIST_FOREACH(cp, &ga->provider->consumers, 147105464Sphk consumers) { 148105464Sphk if (cp->geom->class == ga->class) { 149105464Sphk gp = cp->geom; 150105464Sphk break; 151105464Sphk } 152105464Sphk } 153105464Sphk } 154105464Sphk if (gp == NULL) 155105464Sphk return (EINVAL); 156105464Sphk } else { 157105464Sphk return (EINVAL); 158105464Sphk } 159105464Sphk KASSERT(gp != NULL, ("NULL geom")); 160105464Sphk pp = LIST_FIRST(&gp->provider); 161105464Sphk KASSERT(pp != NULL, ("NULL provider")); 162105464Sphk if (pp->acr > 0 || pp->acw > 0 || pp->ace > 0) 163105464Sphk return (EBUSY); 164105464Sphk g_orphan_provider(pp, ENXIO); 165105464Sphk sc = gp->softc; 166105464Sphk cp = LIST_FIRST(&gp->consumer); 167105464Sphk KASSERT(cp != NULL, ("NULL consumer")); 168105464Sphk sc->dead = 1; 169105464Sphk wakeup(sc); 170105464Sphk error = g_access_rel(cp, -1, -1, -1); 171105464Sphk KASSERT(error == 0, ("error on close")); 172105464Sphk g_detach(cp); 173105464Sphk g_destroy_consumer(cp); 174105464Sphk g_topology_unlock(); 175105464Sphk while (sc->dead != 2 && !LIST_EMPTY(&pp->consumers)) 176105464Sphk tsleep(sc, PRIBIO, "g_bdedie", hz); 177106407Sphk g_waitidle(); 178105464Sphk g_topology_lock(); 179105464Sphk g_destroy_provider(pp); 180105464Sphk mtx_destroy(&sc->worklist_mutex); 181105464Sphk bzero(&sc->key, sizeof sc->key); 182105464Sphk g_free(sc); 183105464Sphk g_destroy_geom(gp); 184105464Sphk return (0); 185105464Sphk } 186105464Sphk 187106518Sphk if (ga->flag != GCFG_CREATE) 188105464Sphk return (EOPNOTSUPP); 189105464Sphk 190105464Sphk if (ga->provider == NULL) 191105464Sphk return (EINVAL); 192105464Sphk /* 193105464Sphk * Attach 194105464Sphk */ 195105464Sphk gp = g_new_geomf(ga->class, "%s.bde", ga->provider->name); 196105464Sphk gp->start = g_bde_start; 197105464Sphk gp->orphan = g_bde_orphan; 198105464Sphk gp->access = g_bde_access; 199105464Sphk gp->spoiled = g_std_spoiled; 200105464Sphk cp = g_new_consumer(gp); 201105464Sphk g_attach(cp, ga->provider); 202105464Sphk error = g_access_rel(cp, 1, 1, 1); 203105464Sphk if (error) { 204105464Sphk g_detach(cp); 205105464Sphk g_destroy_consumer(cp); 206105464Sphk g_destroy_geom(gp); 207105464Sphk return (error); 208105464Sphk } 209105464Sphk g_topology_unlock(); 210106407Sphk g_waitidle(); 211105464Sphk while (1) { 212105551Sphk sectorsize = cp->provider->sectorsize; 213105551Sphk mediasize = cp->provider->mediasize; 214111119Simp sc = g_malloc(sizeof(struct g_bde_softc), M_WAITOK | M_ZERO); 215105464Sphk gp->softc = sc; 216105464Sphk sc->geom = gp; 217105464Sphk sc->consumer = cp; 218105464Sphk 219105464Sphk error = g_bde_decrypt_lock(sc, ga->ptr, 220106407Sphk (u_char *)ga->ptr + (sizeof sc->sha2), 221106407Sphk mediasize, sectorsize, NULL); 222106407Sphk bzero(sc->sha2, sizeof sc->sha2); 223105464Sphk if (error) 224105464Sphk break; 225105464Sphk kp = &sc->key; 226105464Sphk 227105464Sphk /* Initialize helper-fields */ 228105464Sphk kp->keys_per_sector = kp->sectorsize / G_BDE_SKEYLEN; 229105464Sphk kp->zone_cont = kp->keys_per_sector * kp->sectorsize; 230105464Sphk kp->zone_width = kp->zone_cont + kp->sectorsize; 231105464Sphk kp->media_width = kp->sectorN - kp->sector0 - 232105464Sphk G_BDE_MAXKEYS * kp->sectorsize; 233105464Sphk 234105464Sphk /* Our external parameters */ 235105464Sphk sc->zone_cont = kp->zone_cont; 236105464Sphk sc->mediasize = g_bde_max_sector(kp); 237105464Sphk sc->sectorsize = kp->sectorsize; 238105464Sphk 239105464Sphk TAILQ_INIT(&sc->freelist); 240105464Sphk TAILQ_INIT(&sc->worklist); 241105464Sphk mtx_init(&sc->worklist_mutex, "g_bde_worklist", NULL, MTX_DEF); 242105464Sphk mtx_lock(&Giant); 243105464Sphk /* XXX: error check */ 244105464Sphk kthread_create(g_bde_worker, gp, &sc->thread, 0, 0, 245105464Sphk "g_bde %s", gp->name); 246105464Sphk mtx_unlock(&Giant); 247105464Sphk g_topology_lock(); 248105464Sphk pp = g_new_providerf(gp, gp->name); 249110697Sphk pp->flags |= G_PF_CANDELETE; 250110712Sphk pp->stripesize = kp->zone_cont; 251110712Sphk pp->stripeoffset = 0; 252105464Sphk pp->mediasize = sc->mediasize; 253105542Sphk pp->sectorsize = sc->sectorsize; 254105464Sphk g_error_provider(pp, 0); 255105464Sphk g_topology_unlock(); 256105464Sphk break; 257105464Sphk } 258105464Sphk g_topology_lock(); 259105464Sphk if (error == 0) { 260105464Sphk ga->geom = gp; 261105464Sphk return (0); 262105464Sphk } else { 263105464Sphk g_access_rel(cp, -1, -1, -1); 264105464Sphk } 265105464Sphk g_detach(cp); 266105464Sphk g_destroy_consumer(cp); 267105464Sphk if (gp->softc != NULL) 268105464Sphk g_free(gp->softc); 269105464Sphk g_destroy_geom(gp); 270105464Sphk return (error); 271105464Sphk} 272105464Sphk 273105464Sphkstatic struct g_class g_bde_class = { 274112552Sphk .name = BDE_CLASS_NAME, 275112552Sphk .config = g_bde_config, 276105464Sphk G_CLASS_INITIALIZER 277105464Sphk}; 278105464Sphk 279105464SphkDECLARE_GEOM_CLASS(g_bde_class, g_bde); 280