The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/geom/bde/g_bde.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    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  *
   20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   30  * SUCH DAMAGE.
   31  *
   32  * $FreeBSD: releng/5.1/sys/geom/bde/g_bde.c 114720 2003-05-05 08:58:12Z phk $
   33  *
   34  */
   35 
   36 #include <sys/param.h>
   37 #include <sys/bio.h>
   38 #include <sys/lock.h>
   39 #include <sys/mutex.h>
   40 #include <sys/malloc.h>
   41 #include <sys/systm.h>
   42 #include <sys/kernel.h>
   43 #include <sys/kthread.h>
   44 
   45 #include <crypto/rijndael/rijndael.h>
   46 #include <crypto/sha2/sha2.h>
   47 #include <geom/geom.h>
   48 #include <geom/bde/g_bde.h>
   49 #define BDE_CLASS_NAME "BDE"
   50 
   51 static void
   52 g_bde_start(struct bio *bp)
   53 {
   54         struct g_geom *gp;
   55         struct g_consumer *cp;
   56         struct g_bde_softc *sc;
   57 
   58         gp = bp->bio_to->geom;
   59         cp = LIST_FIRST(&gp->consumer);
   60         sc = gp->softc;
   61         switch (bp->bio_cmd) {
   62         case BIO_DELETE:
   63         case BIO_READ:
   64         case BIO_WRITE:
   65                 g_bde_start1(bp);
   66                 break;
   67         case BIO_GETATTR:
   68                 g_io_deliver(bp, EOPNOTSUPP);
   69                 break;
   70         default:
   71                 g_io_deliver(bp, EOPNOTSUPP);
   72                 return;
   73         }
   74         return;
   75 }
   76 
   77 static void
   78 g_bde_orphan(struct g_consumer *cp)
   79 {
   80         struct g_geom *gp;
   81         struct g_provider *pp;
   82         struct g_bde_softc *sc;
   83         int error;
   84 
   85         g_trace(G_T_TOPOLOGY, "g_bde_orphan(%p/%s)", cp, cp->provider->name);
   86         g_topology_assert();
   87         KASSERT(cp->provider->error != 0,
   88                 ("g_bde_orphan with error == 0"));
   89 
   90         gp = cp->geom;
   91         sc = gp->softc;
   92         gp->flags |= G_GEOM_WITHER;
   93         error = cp->provider->error;
   94         LIST_FOREACH(pp, &gp->provider, provider)
   95                 g_orphan_provider(pp, error);
   96         bzero(sc, sizeof(struct g_bde_softc));  /* destroy evidence */
   97         return;
   98 }
   99 
  100 static int
  101 g_bde_access(struct g_provider *pp, int dr, int dw, int de)
  102 {
  103         struct g_geom *gp;
  104         struct g_consumer *cp;
  105 
  106         gp = pp->geom;
  107         cp = LIST_FIRST(&gp->consumer);
  108         if (cp->acr == 0 && cp->acw == 0 && cp->ace == 0) {
  109                 de++;
  110                 dr++;
  111         }
  112         /* ... and let go of it on last close */
  113         if ((cp->acr + dr) == 0 && (cp->acw + dw) == 0 && (cp->ace + de) == 1) {
  114                 de--;
  115                 dr--;
  116         }
  117         return (g_access_rel(cp, dr, dw, de));
  118 }
  119 
  120 static int
  121 g_bde_create_geom(struct gctl_req *req, struct g_class *mp, struct g_provider *pp)
  122 {
  123         struct g_geom *gp;
  124         struct g_consumer *cp;
  125         struct g_bde_key *kp;
  126         int error, i;
  127         u_int sectorsize;
  128         off_t mediasize;
  129         struct g_bde_softc *sc;
  130         void *pass;
  131         void *key;
  132 
  133         if (pp == NULL)
  134                 return (gctl_error(req, "Provider needed"));
  135         g_trace(G_T_TOPOLOGY, "g_bde_create_geom(%s, %s)", mp->name, pp->name);
  136         g_topology_assert();
  137         gp = NULL;
  138 
  139 
  140         gp = g_new_geomf(mp, "%s.bde", pp->name);
  141         gp->start = g_bde_start;
  142         gp->orphan = g_bde_orphan;
  143         gp->access = g_bde_access;
  144         gp->spoiled = g_std_spoiled;
  145         cp = g_new_consumer(gp);
  146         g_attach(cp, pp);
  147         error = g_access_rel(cp, 1, 1, 1);
  148         if (error) {
  149                 g_detach(cp);
  150                 g_destroy_consumer(cp);
  151                 g_destroy_geom(gp);
  152                 return (error);
  153         }
  154         g_topology_unlock();
  155         g_waitidle();
  156         pass = NULL;
  157         key = NULL;
  158         do {
  159                 pass = gctl_get_param(req, "pass", &i);
  160                 if (pass == NULL || i != SHA512_DIGEST_LENGTH) {
  161                         error = gctl_error(req, "No usable key presented");
  162                         break;
  163                 }
  164                 key = gctl_get_param(req, "key", &i);
  165                 if (key != NULL && i != 16) {
  166                         error = gctl_error(req, "Invalid key presented");
  167                         break;
  168                 }
  169                 sectorsize = cp->provider->sectorsize;
  170                 mediasize = cp->provider->mediasize;
  171                 sc = g_malloc(sizeof(struct g_bde_softc), M_WAITOK | M_ZERO);
  172                 gp->softc = sc;
  173                 sc->geom = gp;
  174                 sc->consumer = cp;
  175 
  176                 error = g_bde_decrypt_lock(sc, pass, key,
  177                     mediasize, sectorsize, NULL);
  178                 bzero(sc->sha2, sizeof sc->sha2);
  179                 if (error)
  180                         break;
  181                 kp = &sc->key;
  182 
  183                 /* Initialize helper-fields */
  184                 kp->keys_per_sector = kp->sectorsize / G_BDE_SKEYLEN;
  185                 kp->zone_cont = kp->keys_per_sector * kp->sectorsize;
  186                 kp->zone_width = kp->zone_cont + kp->sectorsize;
  187                 kp->media_width = kp->sectorN - kp->sector0 -
  188                     G_BDE_MAXKEYS * kp->sectorsize;
  189 
  190                 /* Our external parameters */
  191                 sc->zone_cont = kp->zone_cont;
  192                 sc->mediasize = g_bde_max_sector(kp);
  193                 sc->sectorsize = kp->sectorsize;
  194 
  195                 TAILQ_INIT(&sc->freelist);
  196                 TAILQ_INIT(&sc->worklist);
  197                 mtx_init(&sc->worklist_mutex, "g_bde_worklist", NULL, MTX_DEF);
  198                 mtx_lock(&Giant);
  199                 /* XXX: error check */
  200                 kthread_create(g_bde_worker, gp, &sc->thread, 0, 0,
  201                         "g_bde %s", gp->name);
  202                 mtx_unlock(&Giant);
  203                 g_topology_lock();
  204                 pp = g_new_providerf(gp, gp->name);
  205 #if 0
  206                 /*
  207                  * XXX: Disable this for now.  Appearantly UFS no longer
  208                  * XXX: issues BIO_DELETE requests correctly, with the obvious
  209                  * XXX: outcome that userdata is trashed.
  210                  */
  211                 pp->flags |= G_PF_CANDELETE;
  212 #endif
  213                 pp->stripesize = kp->zone_cont;
  214                 pp->stripeoffset = 0;
  215                 pp->mediasize = sc->mediasize;
  216                 pp->sectorsize = sc->sectorsize;
  217                 g_error_provider(pp, 0);
  218                 g_topology_unlock();
  219                 break;
  220         } while (0);
  221         if (pass != NULL) {
  222                 bzero(pass, SHA512_DIGEST_LENGTH);
  223                 g_free(pass);
  224         }
  225         if (key != NULL) {
  226                 bzero(key, 16);
  227                 g_free(key);
  228         }
  229         g_topology_lock();
  230         if (error == 0) {
  231                 return (0);
  232         }
  233         g_access_rel(cp, -1, -1, -1);
  234         g_detach(cp);
  235         g_destroy_consumer(cp);
  236         if (gp->softc != NULL)
  237                 g_free(gp->softc);
  238         g_destroy_geom(gp);
  239         return (error);
  240 }
  241 
  242 
  243 static int
  244 g_bde_destroy_geom(struct gctl_req *req, struct g_class *mp, struct g_geom *gp)
  245 {
  246         struct g_consumer *cp;
  247         struct g_provider *pp;
  248         int error;
  249         struct g_bde_softc *sc;
  250 
  251         g_trace(G_T_TOPOLOGY, "g_bde_destroy_geom(%s, %s)", mp->name, gp->name);
  252         g_topology_assert();
  253         /*
  254          * Orderly detachment.
  255          */
  256         KASSERT(gp != NULL, ("NULL geom"));
  257         pp = LIST_FIRST(&gp->provider);
  258         KASSERT(pp != NULL, ("NULL provider"));
  259         if (pp->acr > 0 || pp->acw > 0 || pp->ace > 0)
  260                 return (EBUSY);
  261         g_orphan_provider(pp, ENXIO);
  262         sc = gp->softc;
  263         cp = LIST_FIRST(&gp->consumer);
  264         KASSERT(cp != NULL, ("NULL consumer"));
  265         sc->dead = 1;
  266         wakeup(sc);
  267         error = g_access_rel(cp, -1, -1, -1);
  268         KASSERT(error == 0, ("error on close"));
  269         g_detach(cp);
  270         g_destroy_consumer(cp);
  271         g_topology_unlock();
  272         while (sc->dead != 2 && !LIST_EMPTY(&pp->consumers))
  273                 tsleep(sc, PRIBIO, "g_bdedie", hz);
  274         g_waitidle();
  275         g_topology_lock();
  276         g_destroy_provider(pp);
  277         mtx_destroy(&sc->worklist_mutex);
  278         bzero(&sc->key, sizeof sc->key);
  279         g_free(sc);
  280         g_destroy_geom(gp);
  281         return (0);
  282 }
  283 
  284 static struct g_class g_bde_class       = {
  285         .name = BDE_CLASS_NAME,
  286         .create_geom = g_bde_create_geom,
  287         .destroy_geom = g_bde_destroy_geom,
  288         G_CLASS_INITIALIZER
  289 };
  290 
  291 DECLARE_GEOM_CLASS(g_bde_class, g_bde);

Cache object: afc0333dec2916a4631d7719beed5cd6


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.