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/geom_sunlabel.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  * 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 
   36 #include <sys/cdefs.h>
   37 __FBSDID("$FreeBSD$");
   38 
   39 #include <sys/param.h>
   40 #include <sys/endian.h>
   41 #include <sys/systm.h>
   42 #include <sys/kernel.h>
   43 #include <sys/conf.h>
   44 #include <sys/bio.h>
   45 #include <sys/malloc.h>
   46 #include <sys/lock.h>
   47 #include <sys/mutex.h>
   48 #include <sys/md5.h>
   49 #include <sys/sun_disklabel.h>
   50 #include <geom/geom.h>
   51 #include <geom/geom_slice.h>
   52 #include <machine/endian.h>
   53 
   54 #define SUNLABEL_CLASS_NAME "SUN"
   55 
   56 struct g_sunlabel_softc {
   57         int sectorsize;
   58         int nheads;
   59         int nsects;
   60         int nalt;
   61         u_char labelsum[16];
   62 };
   63 
   64 static int
   65 g_sunlabel_modify(struct g_geom *gp, struct g_sunlabel_softc *ms, u_char *sec0)
   66 {
   67         int i, error;
   68         u_int u, v, csize;
   69         struct sun_disklabel sl;
   70         MD5_CTX md5sum;
   71 
   72         error = sunlabel_dec(sec0, &sl);
   73         if (error)
   74                 return (error);
   75 
   76         csize = sl.sl_ntracks * sl.sl_nsectors;
   77 
   78         for (i = 0; i < SUN_NPART; i++) {
   79                 v = sl.sl_part[i].sdkp_cyloffset;
   80                 u = sl.sl_part[i].sdkp_nsectors;
   81                 error = g_slice_config(gp, i, G_SLICE_CONFIG_CHECK,
   82                     ((off_t)v * csize) << 9ULL,
   83                     ((off_t)u) << 9ULL,
   84                     ms->sectorsize,
   85                     "%s%c", gp->name, 'a' + i);
   86                 if (error)
   87                         return (error);
   88         }
   89         for (i = 0; i < SUN_NPART; i++) {
   90                 v = sl.sl_part[i].sdkp_cyloffset;
   91                 u = sl.sl_part[i].sdkp_nsectors;
   92                 g_slice_config(gp, i, G_SLICE_CONFIG_SET,
   93                     ((off_t)v * csize) << 9ULL,
   94                     ((off_t)u) << 9ULL,
   95                     ms->sectorsize,
   96                     "%s%c", gp->name, 'a' + i);
   97         }
   98         ms->nalt = sl.sl_acylinders;
   99         ms->nheads = sl.sl_ntracks;
  100         ms->nsects = sl.sl_nsectors;
  101 
  102         /*
  103          * Calculate MD5 from the first sector and use it for avoiding
  104          * recursive labels creation.
  105          */
  106         MD5Init(&md5sum);
  107         MD5Update(&md5sum, sec0, ms->sectorsize);
  108         MD5Final(ms->labelsum, &md5sum);
  109 
  110         return (0);
  111 }
  112 
  113 static void
  114 g_sunlabel_hotwrite(void *arg, int flag)
  115 {
  116         struct bio *bp;
  117         struct g_geom *gp;
  118         struct g_slicer *gsp;
  119         struct g_slice *gsl;
  120         struct g_sunlabel_softc *ms;
  121         u_char *p;
  122         int error;
  123 
  124         KASSERT(flag != EV_CANCEL, ("g_sunlabel_hotwrite cancelled"));
  125         bp = arg;
  126         gp = bp->bio_to->geom;
  127         gsp = gp->softc;
  128         ms = gsp->softc;
  129         gsl = &gsp->slices[bp->bio_to->index];
  130         /*
  131          * XXX: For all practical purposes, this whould be equvivalent to
  132          * XXX: "p = (u_char *)bp->bio_data;" because the label is always
  133          * XXX: in the first sector and we refuse sectors smaller than the
  134          * XXX: label.
  135          */
  136         p = (u_char *)bp->bio_data - (bp->bio_offset + gsl->offset);
  137 
  138         error = g_sunlabel_modify(gp, ms, p);
  139         if (error) {
  140                 g_io_deliver(bp, EPERM);
  141                 return;
  142         }
  143         g_slice_finish_hot(bp);
  144 }
  145 
  146 static void
  147 g_sunlabel_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp)
  148 {
  149         struct g_slicer *gsp;
  150         struct g_sunlabel_softc *ms;
  151 
  152         gsp = gp->softc;
  153         ms = gsp->softc;
  154         g_slice_dumpconf(sb, indent, gp, cp, pp);
  155         if (indent == NULL) {
  156                 sbuf_printf(sb, " sc %u hd %u alt %u",
  157                     ms->nsects, ms->nheads, ms->nalt);
  158         }
  159 }
  160 
  161 struct g_hh01 {
  162         struct g_geom *gp;
  163         struct g_sunlabel_softc *ms;
  164         u_char *label;
  165         int error;
  166 };
  167 
  168 static void
  169 g_sunlabel_callconfig(void *arg, int flag)
  170 {
  171         struct g_hh01 *hp;
  172 
  173         hp = arg;
  174         hp->error = g_sunlabel_modify(hp->gp, hp->ms, hp->label);
  175         if (!hp->error)
  176                 hp->error = g_write_data(LIST_FIRST(&hp->gp->consumer),
  177                     0, hp->label, SUN_SIZE);
  178 }
  179 
  180 /*
  181  * NB! curthread is user process which GCTL'ed.
  182  */
  183 static void
  184 g_sunlabel_config(struct gctl_req *req, struct g_class *mp, const char *verb)
  185 {
  186         u_char *label;
  187         int error, i;
  188         struct g_hh01 h0h0;
  189         struct g_slicer *gsp;
  190         struct g_geom *gp;
  191         struct g_consumer *cp;
  192 
  193         g_topology_assert();
  194         gp = gctl_get_geom(req, mp, "geom");
  195         if (gp == NULL)
  196                 return;
  197         cp = LIST_FIRST(&gp->consumer);
  198         gsp = gp->softc;
  199         if (!strcmp(verb, "write label")) {
  200                 label = gctl_get_paraml(req, "label", SUN_SIZE);
  201                 if (label == NULL)
  202                         return;
  203                 h0h0.gp = gp;
  204                 h0h0.ms = gsp->softc;
  205                 h0h0.label = label;
  206                 h0h0.error = -1;
  207                 /* XXX: Does this reference register with our selfdestruct code ? */
  208                 error = g_access(cp, 1, 1, 1);
  209                 if (error) {
  210                         gctl_error(req, "could not access consumer");
  211                         return;
  212                 }
  213                 g_sunlabel_callconfig(&h0h0, 0);
  214                 g_access(cp, -1, -1, -1);
  215         } else if (!strcmp(verb, "write bootcode")) {
  216                 label = gctl_get_paraml(req, "bootcode", SUN_BOOTSIZE);
  217                 if (label == NULL)
  218                         return;
  219                 /* XXX: Does this reference register with our selfdestruct code ? */
  220                 error = g_access(cp, 1, 1, 1);
  221                 if (error) {
  222                         gctl_error(req, "could not access consumer");
  223                         return;
  224                 }
  225                 for (i = 0; i < SUN_NPART; i++) {
  226                         if (gsp->slices[i].length <= SUN_BOOTSIZE)
  227                                 continue;
  228                         g_write_data(cp,
  229                             gsp->slices[i].offset + SUN_SIZE, label + SUN_SIZE,
  230                             SUN_BOOTSIZE - SUN_SIZE);
  231                 }
  232                 g_access(cp, -1, -1, -1);
  233         } else {
  234                 gctl_error(req, "Unknown verb parameter");
  235         }
  236 }
  237 
  238 static int
  239 g_sunlabel_start(struct bio *bp)
  240 {
  241         struct g_sunlabel_softc *mp;
  242         struct g_slicer *gsp;
  243 
  244         gsp = bp->bio_to->geom->softc;
  245         mp = gsp->softc;
  246         if (bp->bio_cmd == BIO_GETATTR) {
  247                 if (g_handleattr(bp, "SUN::labelsum", mp->labelsum,
  248                     sizeof(mp->labelsum)))
  249                         return (1);
  250         }
  251         return (0);
  252 }
  253 
  254 static struct g_geom *
  255 g_sunlabel_taste(struct g_class *mp, struct g_provider *pp, int flags)
  256 {
  257         struct g_geom *gp;
  258         struct g_consumer *cp;
  259         struct g_sunlabel_softc *ms;
  260         struct g_slicer *gsp;
  261         u_char *buf, hash[16];
  262         MD5_CTX md5sum;
  263         int error;
  264 
  265         g_trace(G_T_TOPOLOGY, "g_sunlabel_taste(%s,%s)", mp->name, pp->name);
  266         g_topology_assert();
  267         if (flags == G_TF_NORMAL &&
  268             !strcmp(pp->geom->class->name, SUNLABEL_CLASS_NAME))
  269                 return (NULL);
  270         gp = g_slice_new(mp, 8, pp, &cp, &ms, sizeof *ms, g_sunlabel_start);
  271         if (gp == NULL)
  272                 return (NULL);
  273         gsp = gp->softc;
  274         do {
  275                 ms->sectorsize = cp->provider->sectorsize;
  276                 if (ms->sectorsize < 512)
  277                         break;
  278                 g_topology_unlock();
  279                 buf = g_read_data(cp, 0, ms->sectorsize, NULL);
  280                 g_topology_lock();
  281                 if (buf == NULL)
  282                         break;
  283 
  284                 /*
  285                  * Calculate MD5 from the first sector and use it for avoiding
  286                  * recursive labels creation.
  287                  */
  288                 MD5Init(&md5sum);
  289                 MD5Update(&md5sum, buf, ms->sectorsize);
  290                 MD5Final(ms->labelsum, &md5sum);
  291  
  292                 error = g_getattr("SUN::labelsum", cp, &hash);
  293                 if (!error && !bcmp(ms->labelsum, hash, sizeof(hash))) {
  294                         g_free(buf);
  295                         break;
  296                 }
  297 
  298                 g_sunlabel_modify(gp, ms, buf);
  299                 g_free(buf);
  300 
  301                 break;
  302         } while (0);
  303         g_access(cp, -1, 0, 0);
  304         if (LIST_EMPTY(&gp->provider)) {
  305                 g_slice_spoiled(cp);
  306                 return (NULL);
  307         }
  308         g_slice_conf_hot(gp, 0, 0, SUN_SIZE,
  309             G_SLICE_HOT_ALLOW, G_SLICE_HOT_DENY, G_SLICE_HOT_CALL);
  310         gsp->hot = g_sunlabel_hotwrite;
  311         return (gp);
  312 }
  313 
  314 static struct g_class g_sunlabel_class = {
  315         .name = SUNLABEL_CLASS_NAME,
  316         .version = G_VERSION,
  317         .taste = g_sunlabel_taste,
  318         .ctlreq = g_sunlabel_config,
  319         .dumpconf = g_sunlabel_dumpconf,
  320 };
  321 
  322 DECLARE_GEOM_CLASS(g_sunlabel_class, g_sunlabel);

Cache object: 4304586d865b77facbcb244178998bc8


[ 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.