g_bde.c revision 105464
1/*- 2 * Copyright (c) 2002 Poul-Henning Kamp 3 * Copyright (c) 2002 Networks Associates Technology, Inc. 4 * All rights reserved. 5 * 6 * This software was developed for the FreeBSD Project by Poul-Henning Kamp 7 * and NAI Labs, the Security Research Division of Network Associates, Inc. 8 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the 9 * DARPA CHATS research program. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. The names of the authors may not be used to endorse or promote 20 * products derived from this software without specific prior written 21 * permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * $FreeBSD: head/sys/geom/bde/g_bde.c 105464 2002-10-19 17:02:17Z phk $ 36 * 37 */ 38 39#include <sys/param.h> 40#include <sys/stdint.h> 41#include <sys/bio.h> 42#include <sys/lock.h> 43#include <sys/mutex.h> 44#include <sys/malloc.h> 45#include <geom/geom.h> 46#include <geom/bde/g_bde.h> 47#include <sys/systm.h> 48#include <sys/kernel.h> 49#include <sys/kthread.h> 50 51#define BDE_CLASS_NAME "BDE" 52 53static void 54g_bde_start(struct bio *bp) 55{ 56 struct g_geom *gp; 57 struct g_consumer *cp; 58 struct g_bde_softc *sc; 59 60 gp = bp->bio_to->geom; 61 cp = LIST_FIRST(&gp->consumer); 62 sc = gp->softc; 63 switch (bp->bio_cmd) { 64 case BIO_DELETE: 65 case BIO_READ: 66 case BIO_WRITE: 67 g_bde_start1(bp); 68 break; 69 case BIO_GETATTR: 70 case BIO_SETATTR: 71 if (g_handleattr_off_t(bp, "GEOM::mediasize", sc->mediasize)) 72 return; 73 if (g_handleattr_int(bp, "GEOM::sectorsize", sc->sectorsize)) 74 return; 75 g_io_deliver(bp, EOPNOTSUPP); 76 break; 77 default: 78 g_io_deliver(bp, EOPNOTSUPP); 79 return; 80 } 81 return; 82} 83 84static void 85g_bde_orphan(struct g_consumer *cp) 86{ 87 struct g_geom *gp; 88 struct g_provider *pp; 89 struct g_bde_softc *sc; 90 int error; 91 92 g_trace(G_T_TOPOLOGY, "g_bde_orphan(%p/%s)", cp, cp->provider->name); 93 g_topology_assert(); 94 KASSERT(cp->provider->error != 0, 95 ("g_bde_orphan with error == 0")); 96 97 gp = cp->geom; 98 sc = gp->softc; 99 gp->flags |= G_GEOM_WITHER; 100 error = cp->provider->error; 101 LIST_FOREACH(pp, &gp->provider, provider) 102 g_orphan_provider(pp, error); 103 bzero(sc, sizeof(struct g_bde_softc)); /* destroy evidence */ 104 return; 105} 106 107static int 108g_bde_access(struct g_provider *pp, int dr, int dw, int de) 109{ 110 struct g_geom *gp; 111 struct g_consumer *cp; 112 113 gp = pp->geom; 114 cp = LIST_FIRST(&gp->consumer); 115 if (cp->acr == 0 && cp->acw == 0 && cp->ace == 0) { 116 de++; 117 dr++; 118 } 119 /* ... and let go of it on last close */ 120 if ((cp->acr + dr) == 0 && (cp->acw + dw) == 0 && (cp->ace + de) == 1) { 121 de--; 122 dr--; 123 } 124 return (g_access_rel(cp, dr, dw, de)); 125} 126 127static int 128g_bde_create(struct g_createargs *ga) 129{ 130 struct g_geom *gp; 131 struct g_consumer *cp; 132 struct g_provider *pp; 133 struct g_bde_key *kp; 134 int error; 135 u_int sectorsize; 136 off_t mediasize; 137 struct g_bde_softc *sc; 138 139 g_trace(G_T_TOPOLOGY, "g_bde_create(%d)", ga->flag); 140 g_topology_assert(); 141 if (ga->flag == 1) { 142 /* 143 * Orderly dettachment. 144 */ 145 if (ga->geom != NULL) { 146 gp = ga->geom; 147 } else if (ga->provider != NULL) { 148 if (ga->provider->geom->class == ga->class) { 149 gp = ga->provider->geom; 150 } else { 151 LIST_FOREACH(cp, &ga->provider->consumers, 152 consumers) { 153 if (cp->geom->class == ga->class) { 154 gp = cp->geom; 155 break; 156 } 157 } 158 } 159 if (gp == NULL) 160 return (EINVAL); 161 } else { 162 return (EINVAL); 163 } 164 KASSERT(gp != NULL, ("NULL geom")); 165 pp = LIST_FIRST(&gp->provider); 166 KASSERT(pp != NULL, ("NULL provider")); 167 if (pp->acr > 0 || pp->acw > 0 || pp->ace > 0) 168 return (EBUSY); 169 g_orphan_provider(pp, ENXIO); 170 sc = gp->softc; 171 cp = LIST_FIRST(&gp->consumer); 172 KASSERT(cp != NULL, ("NULL consumer")); 173 sc->dead = 1; 174 wakeup(sc); 175 error = g_access_rel(cp, -1, -1, -1); 176 KASSERT(error == 0, ("error on close")); 177 g_detach(cp); 178 g_destroy_consumer(cp); 179 g_topology_unlock(); 180 while (sc->dead != 2 && !LIST_EMPTY(&pp->consumers)) 181 tsleep(sc, PRIBIO, "g_bdedie", hz); 182 g_topology_lock(); 183 g_destroy_provider(pp); 184 mtx_destroy(&sc->worklist_mutex); 185 bzero(&sc->key, sizeof sc->key); 186 g_free(sc); 187 g_destroy_geom(gp); 188 return (0); 189 } 190 191 if (ga->flag != 0) 192 return (EOPNOTSUPP); 193 194 if (ga->provider == NULL) 195 return (EINVAL); 196 /* 197 * Attach 198 */ 199 gp = g_new_geomf(ga->class, "%s.bde", ga->provider->name); 200 gp->start = g_bde_start; 201 gp->orphan = g_bde_orphan; 202 gp->access = g_bde_access; 203 gp->spoiled = g_std_spoiled; 204 cp = g_new_consumer(gp); 205 g_attach(cp, ga->provider); 206 error = g_access_rel(cp, 1, 1, 1); 207 if (error) { 208 g_detach(cp); 209 g_destroy_consumer(cp); 210 g_destroy_geom(gp); 211 return (error); 212 } 213 g_topology_unlock(); 214 while (1) { 215 error = g_getattr("GEOM::sectorsize", cp, §orsize); 216 if (error) 217 break; 218 error = g_getattr("GEOM::mediasize", cp, &mediasize); 219 if (error) 220 break; 221 sc = g_malloc(sizeof(struct g_bde_softc), M_WAITOK | M_ZERO); 222 gp->softc = sc; 223 sc->geom = gp; 224 sc->consumer = cp; 225 226 error = g_bde_decrypt_lock(sc, ga->ptr, 227 (u_char *)ga->ptr + 256, mediasize, sectorsize, NULL); 228 bzero(sc->arc4_sbox, sizeof sc->arc4_sbox); 229 if (error) 230 break; 231 kp = &sc->key; 232 233 /* Initialize helper-fields */ 234 kp->keys_per_sector = kp->sectorsize / G_BDE_SKEYLEN; 235 kp->zone_cont = kp->keys_per_sector * kp->sectorsize; 236 kp->zone_width = kp->zone_cont + kp->sectorsize; 237 kp->media_width = kp->sectorN - kp->sector0 - 238 G_BDE_MAXKEYS * kp->sectorsize; 239 240 /* Our external parameters */ 241 sc->zone_cont = kp->zone_cont; 242 sc->mediasize = g_bde_max_sector(kp); 243 sc->sectorsize = kp->sectorsize; 244 245 TAILQ_INIT(&sc->freelist); 246 TAILQ_INIT(&sc->worklist); 247 mtx_init(&sc->worklist_mutex, "g_bde_worklist", NULL, MTX_DEF); 248 mtx_lock(&Giant); 249 /* XXX: error check */ 250 kthread_create(g_bde_worker, gp, &sc->thread, 0, 0, 251 "g_bde %s", gp->name); 252 mtx_unlock(&Giant); 253 g_topology_lock(); 254 pp = g_new_providerf(gp, gp->name); 255 pp->mediasize = sc->mediasize; 256 g_error_provider(pp, 0); 257 g_topology_unlock(); 258 break; 259 } 260 g_topology_lock(); 261 if (error == 0) { 262 ga->geom = gp; 263 return (0); 264 } else { 265 g_access_rel(cp, -1, -1, -1); 266 } 267 g_detach(cp); 268 g_destroy_consumer(cp); 269 if (gp->softc != NULL) 270 g_free(gp->softc); 271 g_destroy_geom(gp); 272 return (error); 273} 274 275static struct g_class g_bde_class = { 276 BDE_CLASS_NAME, 277 NULL, 278 g_bde_create, 279 G_CLASS_INITIALIZER 280}; 281 282DECLARE_GEOM_CLASS(g_bde_class, g_bde); 283