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_bsd.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 /*
   37  * This is the method for dealing with BSD disklabels.  It has been
   38  * extensively (by my standards at least) commented, in the vain hope that
   39  * it will serve as the source in future copy&paste operations.
   40  */
   41 
   42 #include <sys/cdefs.h>
   43 __FBSDID("$FreeBSD: releng/7.3/sys/geom/geom_bsd.c 174737 2007-12-18 01:24:27Z jhb $");
   44 
   45 #include <sys/param.h>
   46 #include <sys/endian.h>
   47 #include <sys/systm.h>
   48 #include <sys/kernel.h>
   49 #include <sys/fcntl.h>
   50 #include <sys/conf.h>
   51 #include <sys/bio.h>
   52 #include <sys/malloc.h>
   53 #include <sys/lock.h>
   54 #include <sys/mutex.h>
   55 #include <sys/md5.h>
   56 #include <sys/errno.h>
   57 #include <sys/disklabel.h>
   58 #include <sys/gpt.h>
   59 #include <sys/uuid.h>
   60 #include <geom/geom.h>
   61 #include <geom/geom_slice.h>
   62 
   63 #define BSD_CLASS_NAME "BSD"
   64 
   65 #define ALPHA_LABEL_OFFSET      64
   66 #define HISTORIC_LABEL_OFFSET   512
   67 
   68 #define LABELSIZE (148 + 16 * MAXPARTITIONS)
   69 
   70 static void g_bsd_hotwrite(void *arg, int flag);
   71 /*
   72  * Our private data about one instance.  All the rest is handled by the
   73  * slice code and stored in its softc, so this is just the stuff
   74  * specific to BSD disklabels.
   75  */
   76 struct g_bsd_softc {
   77         off_t   labeloffset;
   78         off_t   mbroffset;
   79         off_t   rawoffset;
   80         struct disklabel ondisk;
   81         u_char  label[LABELSIZE];
   82         u_char  labelsum[16];
   83 };
   84 
   85 /*
   86  * Modify our slicer to match proposed disklabel, if possible.
   87  * This is where we make sure we don't do something stupid.
   88  */
   89 static int
   90 g_bsd_modify(struct g_geom *gp, u_char *label)
   91 {
   92         int i, error;
   93         struct partition *ppp;
   94         struct g_slicer *gsp;
   95         struct g_consumer *cp;
   96         struct g_bsd_softc *ms;
   97         u_int secsize, u;
   98         off_t rawoffset, o;
   99         struct disklabel dl;
  100         MD5_CTX md5sum;
  101 
  102         g_topology_assert();
  103         gsp = gp->softc;
  104         ms = gsp->softc;
  105 
  106         error = bsd_disklabel_le_dec(label, &dl, MAXPARTITIONS);
  107         if (error) {
  108                 return (error);
  109         }
  110 
  111         /* Get dimensions of our device. */
  112         cp = LIST_FIRST(&gp->consumer);
  113         secsize = cp->provider->sectorsize;
  114 
  115         /* ... or a smaller sector size. */
  116         if (dl.d_secsize < secsize) {
  117                 return (EINVAL);
  118         }
  119 
  120         /* ... or a non-multiple sector size. */
  121         if (dl.d_secsize % secsize != 0) {
  122                 return (EINVAL);
  123         }
  124 
  125         /* Historical braindamage... */
  126         rawoffset = (off_t)dl.d_partitions[RAW_PART].p_offset * dl.d_secsize;
  127 
  128         for (i = 0; i < dl.d_npartitions; i++) {
  129                 ppp = &dl.d_partitions[i];
  130                 if (ppp->p_size == 0)
  131                         continue;
  132                 o = (off_t)ppp->p_offset * dl.d_secsize;
  133 
  134                 if (o < rawoffset)
  135                         rawoffset = 0;
  136         }
  137         
  138         if (rawoffset != 0 && (off_t)rawoffset != ms->mbroffset)
  139                 printf("WARNING: Expected rawoffset %jd, found %jd\n",
  140                     (intmax_t)ms->mbroffset/dl.d_secsize,
  141                     (intmax_t)rawoffset/dl.d_secsize);
  142 
  143         /* Don't munge open partitions. */
  144         for (i = 0; i < dl.d_npartitions; i++) {
  145                 ppp = &dl.d_partitions[i];
  146 
  147                 o = (off_t)ppp->p_offset * dl.d_secsize;
  148                 if (o == 0)
  149                         o = rawoffset;
  150                 error = g_slice_config(gp, i, G_SLICE_CONFIG_CHECK,
  151                     o - rawoffset,
  152                     (off_t)ppp->p_size * dl.d_secsize,
  153                      dl.d_secsize,
  154                     "%s%c", gp->name, 'a' + i);
  155                 if (error)
  156                         return (error);
  157         }
  158 
  159         /* Look good, go for it... */
  160         for (u = 0; u < gsp->nslice; u++) {
  161                 ppp = &dl.d_partitions[u];
  162                 o = (off_t)ppp->p_offset * dl.d_secsize;
  163                 if (o == 0)
  164                         o = rawoffset;
  165                 g_slice_config(gp, u, G_SLICE_CONFIG_SET,
  166                     o - rawoffset,
  167                     (off_t)ppp->p_size * dl.d_secsize,
  168                      dl.d_secsize,
  169                     "%s%c", gp->name, 'a' + u);
  170         }
  171 
  172         /* Update our softc */
  173         ms->ondisk = dl;
  174         if (label != ms->label)
  175                 bcopy(label, ms->label, LABELSIZE);
  176         ms->rawoffset = rawoffset;
  177 
  178         /*
  179          * In order to avoid recursively attaching to the same
  180          * on-disk label (it's usually visible through the 'c'
  181          * partition) we calculate an MD5 and ask if other BSD's
  182          * below us love that label.  If they do, we don't.
  183          */
  184         MD5Init(&md5sum);
  185         MD5Update(&md5sum, ms->label, sizeof(ms->label));
  186         MD5Final(ms->labelsum, &md5sum);
  187 
  188         return (0);
  189 }
  190 
  191 /*
  192  * This is an internal helper function, called multiple times from the taste
  193  * function to try to locate a disklabel on the disk.  More civilized formats
  194  * will not need this, as there is only one possible place on disk to look
  195  * for the magic spot.
  196  */
  197 
  198 static int
  199 g_bsd_try(struct g_geom *gp, struct g_slicer *gsp, struct g_consumer *cp, int secsize, struct g_bsd_softc *ms, off_t offset)
  200 {
  201         int error;
  202         u_char *buf;
  203         struct disklabel *dl;
  204         off_t secoff;
  205 
  206         /*
  207          * We need to read entire aligned sectors, and we assume that the
  208          * disklabel does not span sectors, so one sector is enough.
  209          */
  210         secoff = offset % secsize;
  211         buf = g_read_data(cp, offset - secoff, secsize, NULL);
  212         if (buf == NULL)
  213                 return (ENOENT);
  214 
  215         /* Decode into our native format. */
  216         dl = &ms->ondisk;
  217         error = bsd_disklabel_le_dec(buf + secoff, dl, MAXPARTITIONS);
  218         if (!error)
  219                 bcopy(buf + secoff, ms->label, LABELSIZE);
  220 
  221         /* Remember to free the buffer g_read_data() gave us. */
  222         g_free(buf);
  223 
  224         ms->labeloffset = offset;
  225         return (error);
  226 }
  227 
  228 /*
  229  * This function writes the current label to disk, possibly updating
  230  * the alpha SRM checksum.
  231  */
  232 
  233 static int
  234 g_bsd_writelabel(struct g_geom *gp, u_char *bootcode)
  235 {
  236         off_t secoff;
  237         u_int secsize;
  238         struct g_consumer *cp;
  239         struct g_slicer *gsp;
  240         struct g_bsd_softc *ms;
  241         u_char *buf;
  242         uint64_t sum;
  243         int error, i;
  244 
  245         gsp = gp->softc;
  246         ms = gsp->softc;
  247         cp = LIST_FIRST(&gp->consumer);
  248         /* Get sector size, we need it to read data. */
  249         secsize = cp->provider->sectorsize;
  250         secoff = ms->labeloffset % secsize;
  251         if (bootcode == NULL) {
  252                 buf = g_read_data(cp, ms->labeloffset - secoff, secsize, &error);
  253                 if (buf == NULL)
  254                         return (error);
  255                 bcopy(ms->label, buf + secoff, sizeof(ms->label));
  256         } else {
  257                 buf = bootcode;
  258                 bcopy(ms->label, buf + ms->labeloffset, sizeof(ms->label));
  259         }
  260         if (ms->labeloffset == ALPHA_LABEL_OFFSET) {
  261                 sum = 0;
  262                 for (i = 0; i < 63; i++)
  263                         sum += le64dec(buf + i * 8);
  264                 le64enc(buf + 504, sum);
  265         }
  266         if (bootcode == NULL) {
  267                 error = g_write_data(cp, ms->labeloffset - secoff, buf, secsize);
  268                 g_free(buf);
  269         } else {
  270                 error = g_write_data(cp, 0, bootcode, BBSIZE);
  271         }
  272         return(error);
  273 }
  274 
  275 /*
  276  * If the user tries to overwrite our disklabel through an open partition
  277  * or via a magicwrite config call, we end up here and try to prevent
  278  * footshooting as best we can.
  279  */
  280 static void
  281 g_bsd_hotwrite(void *arg, int flag)
  282 {
  283         struct bio *bp;
  284         struct g_geom *gp;
  285         struct g_slicer *gsp;
  286         struct g_slice *gsl;
  287         struct g_bsd_softc *ms;
  288         u_char *p;
  289         int error;
  290         
  291         g_topology_assert();
  292         /*
  293          * We should never get canceled, because that would amount to a removal
  294          * of the geom while there was outstanding I/O requests.
  295          */
  296         KASSERT(flag != EV_CANCEL, ("g_bsd_hotwrite cancelled"));
  297         bp = arg;
  298         gp = bp->bio_to->geom;
  299         gsp = gp->softc;
  300         ms = gsp->softc;
  301         gsl = &gsp->slices[bp->bio_to->index];
  302         p = (u_char*)bp->bio_data + ms->labeloffset 
  303             - (bp->bio_offset + gsl->offset);
  304         error = g_bsd_modify(gp, p);
  305         if (error) {
  306                 g_io_deliver(bp, EPERM);
  307                 return;
  308         }
  309         g_slice_finish_hot(bp);
  310 }
  311 
  312 /*-
  313  * This start routine is only called for non-trivial requests, all the
  314  * trivial ones are handled autonomously by the slice code.
  315  * For requests we handle here, we must call the g_io_deliver() on the
  316  * bio, and return non-zero to indicate to the slice code that we did so.
  317  * This code executes in the "DOWN" I/O path, this means:
  318  *    * No sleeping.
  319  *    * Don't grab the topology lock.
  320  *    * Don't call biowait, g_getattr(), g_setattr() or g_read_data()
  321  */
  322 static int
  323 g_bsd_ioctl(struct g_provider *pp, u_long cmd, void *data, int fflag, struct thread *td)
  324 {
  325         struct g_geom *gp;
  326         struct g_bsd_softc *ms;
  327         struct g_slicer *gsp;
  328         u_char *label;
  329         int error;
  330 
  331         gp = pp->geom;
  332         gsp = gp->softc;
  333         ms = gsp->softc;
  334 
  335         switch(cmd) {
  336         case DIOCGDINFO:
  337                 /* Return a copy of the disklabel to userland. */
  338                 bsd_disklabel_le_dec(ms->label, data, MAXPARTITIONS);
  339                 return(0);
  340         case DIOCBSDBB: {
  341                 struct g_consumer *cp;
  342                 u_char *buf;
  343                 void *p;
  344                 int error, i;
  345                 uint64_t sum;
  346 
  347                 if (!(fflag & FWRITE))
  348                         return (EPERM);
  349                 /* The disklabel to set is the ioctl argument. */
  350                 buf = g_malloc(BBSIZE, M_WAITOK);
  351                 p = *(void **)data;
  352                 error = copyin(p, buf, BBSIZE);
  353                 if (!error) {
  354                         /* XXX: Rude, but supposedly safe */
  355                         DROP_GIANT();
  356                         g_topology_lock();
  357                         /* Validate and modify our slice instance to match. */
  358                         error = g_bsd_modify(gp, buf + ms->labeloffset);
  359                         if (!error) {
  360                                 cp = LIST_FIRST(&gp->consumer);
  361                                 if (ms->labeloffset == ALPHA_LABEL_OFFSET) {
  362                                         sum = 0;
  363                                         for (i = 0; i < 63; i++)
  364                                                 sum += le64dec(buf + i * 8);
  365                                         le64enc(buf + 504, sum);
  366                                 }
  367                                 error = g_write_data(cp, 0, buf, BBSIZE);
  368                         }
  369                         g_topology_unlock();
  370                         PICKUP_GIANT();
  371                 }
  372                 g_free(buf);
  373                 return (error);
  374         }
  375         case DIOCSDINFO:
  376         case DIOCWDINFO: {
  377                 if (!(fflag & FWRITE))
  378                         return (EPERM);
  379                 label = g_malloc(LABELSIZE, M_WAITOK);
  380                 /* The disklabel to set is the ioctl argument. */
  381                 bsd_disklabel_le_enc(label, data);
  382 
  383                 DROP_GIANT();
  384                 g_topology_lock();
  385                 /* Validate and modify our slice instance to match. */
  386                 error = g_bsd_modify(gp, label);
  387                 if (error == 0 && cmd == DIOCWDINFO)
  388                         error = g_bsd_writelabel(gp, NULL);
  389                 g_topology_unlock();
  390                 PICKUP_GIANT();
  391                 g_free(label);
  392                 return(error);
  393         }
  394         default:
  395                 return (ENOIOCTL);
  396         }
  397 }
  398 
  399 static int
  400 g_bsd_start(struct bio *bp)
  401 {
  402         struct g_geom *gp;
  403         struct g_bsd_softc *ms;
  404         struct g_slicer *gsp;
  405 
  406         gp = bp->bio_to->geom;
  407         gsp = gp->softc;
  408         ms = gsp->softc;
  409         if (bp->bio_cmd == BIO_GETATTR) {
  410                 if (g_handleattr(bp, "BSD::labelsum", ms->labelsum,
  411                     sizeof(ms->labelsum)))
  412                         return (1);
  413         }
  414         return (0);
  415 }
  416 
  417 /*
  418  * Dump configuration information in XML format.
  419  * Notice that the function is called once for the geom and once for each
  420  * consumer and provider.  We let g_slice_dumpconf() do most of the work.
  421  */
  422 static void
  423 g_bsd_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp)
  424 {
  425         struct g_bsd_softc *ms;
  426         struct g_slicer *gsp;
  427 
  428         gsp = gp->softc;
  429         ms = gsp->softc;
  430         g_slice_dumpconf(sb, indent, gp, cp, pp);
  431         if (indent != NULL && pp == NULL && cp == NULL) {
  432                 sbuf_printf(sb, "%s<labeloffset>%jd</labeloffset>\n",
  433                     indent, (intmax_t)ms->labeloffset);
  434                 sbuf_printf(sb, "%s<rawoffset>%jd</rawoffset>\n",
  435                     indent, (intmax_t)ms->rawoffset);
  436                 sbuf_printf(sb, "%s<mbroffset>%jd</mbroffset>\n",
  437                     indent, (intmax_t)ms->mbroffset);
  438         } else if (pp != NULL) {
  439                 if (indent == NULL)
  440                         sbuf_printf(sb, " ty %d",
  441                             ms->ondisk.d_partitions[pp->index].p_fstype);
  442                 else
  443                         sbuf_printf(sb, "%s<type>%d</type>\n", indent,
  444                             ms->ondisk.d_partitions[pp->index].p_fstype);
  445         }
  446 }
  447 
  448 /*
  449  * The taste function is called from the event-handler, with the topology
  450  * lock already held and a provider to examine.  The flags are unused.
  451  *
  452  * If flags == G_TF_NORMAL, the idea is to take a bite of the provider and
  453  * if we find valid, consistent magic on it, build a geom on it.
  454  *
  455  * There may be cases where the operator would like to put a BSD-geom on
  456  * providers which do not meet all of the requirements.  This can be done
  457  * by instead passing the G_TF_INSIST flag, which will override these
  458  * checks.
  459  *
  460  * The final flags value is G_TF_TRANSPARENT, which instructs the method
  461  * to put a geom on top of the provider and configure it to be as transparent
  462  * as possible.  This is not really relevant to the BSD method and therefore
  463  * not implemented here.
  464  */
  465 
  466 static struct uuid freebsd_slice = GPT_ENT_TYPE_FREEBSD;
  467 
  468 static struct g_geom *
  469 g_bsd_taste(struct g_class *mp, struct g_provider *pp, int flags)
  470 {
  471         struct g_geom *gp;
  472         struct g_consumer *cp;
  473         int error, i;
  474         struct g_bsd_softc *ms;
  475         u_int secsize;
  476         struct g_slicer *gsp;
  477         u_char hash[16];
  478         MD5_CTX md5sum;
  479         struct uuid uuid;
  480 
  481         g_trace(G_T_TOPOLOGY, "bsd_taste(%s,%s)", mp->name, pp->name);
  482         g_topology_assert();
  483 
  484         /* We don't implement transparent inserts. */
  485         if (flags == G_TF_TRANSPARENT)
  486                 return (NULL);
  487 
  488         /*
  489          * BSD labels are a subclass of the general "slicing" topology so
  490          * a lot of the work can be done by the common "slice" code.
  491          * Create a geom with space for MAXPARTITIONS providers, one consumer
  492          * and a softc structure for us.  Specify the provider to attach
  493          * the consumer to and our "start" routine for special requests.
  494          * The provider is opened with mode (1,0,0) so we can do reads
  495          * from it.
  496          */
  497         gp = g_slice_new(mp, MAXPARTITIONS, pp, &cp, &ms,
  498              sizeof(*ms), g_bsd_start);
  499         if (gp == NULL)
  500                 return (NULL);
  501 
  502         /* Get the geom_slicer softc from the geom. */
  503         gsp = gp->softc;
  504 
  505         /*
  506          * The do...while loop here allows us to have multiple escapes
  507          * using a simple "break".  This improves code clarity without
  508          * ending up in deep nesting and without using goto or come from.
  509          */
  510         do {
  511                 /*
  512                  * If the provider is an MBR we will only auto attach
  513                  * to type 165 slices in the G_TF_NORMAL case.  We will
  514                  * attach to any other type.
  515                  */
  516                 error = g_getattr("MBR::type", cp, &i);
  517                 if (!error) {
  518                         if (i != 165 && flags == G_TF_NORMAL)
  519                                 break;
  520                         error = g_getattr("MBR::offset", cp, &ms->mbroffset);
  521                         if (error)
  522                                 break;
  523                 }
  524 
  525                 /* Same thing if we are inside a PC98 */
  526                 error = g_getattr("PC98::type", cp, &i);
  527                 if (!error) {
  528                         if (i != 0xc494 && flags == G_TF_NORMAL)
  529                                 break;
  530                         error = g_getattr("PC98::offset", cp, &ms->mbroffset);
  531                         if (error)
  532                                 break;
  533                 }
  534 
  535                 /* Same thing if we are inside a GPT */
  536                 error = g_getattr("GPT::type", cp, &uuid);
  537                 if (!error) {
  538                         if (memcmp(&uuid, &freebsd_slice, sizeof(uuid)) != 0 &&
  539                             flags == G_TF_NORMAL)
  540                                 break;
  541                 }
  542 
  543                 /* Get sector size, we need it to read data. */
  544                 secsize = cp->provider->sectorsize;
  545                 if (secsize < 512)
  546                         break;
  547 
  548                 /* First look for a label at the start of the second sector. */
  549                 error = g_bsd_try(gp, gsp, cp, secsize, ms, secsize);
  550 
  551                 /*
  552                  * If sector size is not 512 the label still can be at
  553                  * offset 512, not at the start of the second sector. At least
  554                  * it's true for labels created by the FreeBSD's bsdlabel(8).
  555                  */
  556                 if (error && secsize != HISTORIC_LABEL_OFFSET)
  557                         error = g_bsd_try(gp, gsp, cp, secsize, ms,
  558                             HISTORIC_LABEL_OFFSET);
  559 
  560                 /* Next, look for alpha labels */
  561                 if (error)
  562                         error = g_bsd_try(gp, gsp, cp, secsize, ms,
  563                             ALPHA_LABEL_OFFSET);
  564 
  565                 /* If we didn't find a label, punt. */
  566                 if (error)
  567                         break;
  568 
  569                 /*
  570                  * In order to avoid recursively attaching to the same
  571                  * on-disk label (it's usually visible through the 'c'
  572                  * partition) we calculate an MD5 and ask if other BSD's
  573                  * below us love that label.  If they do, we don't.
  574                  */
  575                 MD5Init(&md5sum);
  576                 MD5Update(&md5sum, ms->label, sizeof(ms->label));
  577                 MD5Final(ms->labelsum, &md5sum);
  578 
  579                 error = g_getattr("BSD::labelsum", cp, &hash);
  580                 if (!error && !bcmp(ms->labelsum, hash, sizeof(hash)))
  581                         break;
  582 
  583                 /*
  584                  * Process the found disklabel, and modify our "slice"
  585                  * instance to match it, if possible.
  586                  */
  587                 error = g_bsd_modify(gp, ms->label);
  588         } while (0);
  589 
  590         /* Success or failure, we can close our provider now. */
  591         g_access(cp, -1, 0, 0);
  592 
  593         /* If we have configured any providers, return the new geom. */
  594         if (gsp->nprovider > 0) {
  595                 g_slice_conf_hot(gp, 0, ms->labeloffset, LABELSIZE,
  596                     G_SLICE_HOT_ALLOW, G_SLICE_HOT_DENY, G_SLICE_HOT_CALL);
  597                 gsp->hot = g_bsd_hotwrite;
  598                 return (gp);
  599         }
  600         /*
  601          * ...else push the "self-destruct" button, by spoiling our own
  602          * consumer.  This triggers a call to g_slice_spoiled which will
  603          * dismantle what was setup.
  604          */
  605         g_slice_spoiled(cp);
  606         return (NULL);
  607 }
  608 
  609 struct h0h0 {
  610         struct g_geom *gp;
  611         struct g_bsd_softc *ms;
  612         u_char *label;
  613         int error;
  614 };
  615 
  616 static void
  617 g_bsd_callconfig(void *arg, int flag)
  618 {
  619         struct h0h0 *hp;
  620 
  621         hp = arg;
  622         hp->error = g_bsd_modify(hp->gp, hp->label);
  623         if (!hp->error)
  624                 hp->error = g_bsd_writelabel(hp->gp, NULL);
  625 }
  626 
  627 /*
  628  * NB! curthread is user process which GCTL'ed.
  629  */
  630 static void
  631 g_bsd_config(struct gctl_req *req, struct g_class *mp, char const *verb)
  632 {
  633         u_char *label;
  634         int error;
  635         struct h0h0 h0h0;
  636         struct g_geom *gp;
  637         struct g_slicer *gsp;
  638         struct g_consumer *cp;
  639         struct g_bsd_softc *ms;
  640 
  641         g_topology_assert();
  642         gp = gctl_get_geom(req, mp, "geom");
  643         if (gp == NULL)
  644                 return;
  645         cp = LIST_FIRST(&gp->consumer);
  646         gsp = gp->softc;
  647         ms = gsp->softc;
  648         if (!strcmp(verb, "read mbroffset")) {
  649                 gctl_set_param_err(req, "mbroffset", &ms->mbroffset,
  650                     sizeof(ms->mbroffset));
  651                 return;
  652         } else if (!strcmp(verb, "write label")) {
  653                 label = gctl_get_paraml(req, "label", LABELSIZE);
  654                 if (label == NULL)
  655                         return;
  656                 h0h0.gp = gp;
  657                 h0h0.ms = gsp->softc;
  658                 h0h0.label = label;
  659                 h0h0.error = -1;
  660                 /* XXX: Does this reference register with our selfdestruct code ? */
  661                 error = g_access(cp, 1, 1, 1);
  662                 if (error) {
  663                         gctl_error(req, "could not access consumer");
  664                         return;
  665                 }
  666                 g_bsd_callconfig(&h0h0, 0);
  667                 error = h0h0.error;
  668                 g_access(cp, -1, -1, -1);
  669         } else if (!strcmp(verb, "write bootcode")) {
  670                 label = gctl_get_paraml(req, "bootcode", BBSIZE);
  671                 if (label == NULL)
  672                         return;
  673                 /* XXX: Does this reference register with our selfdestruct code ? */
  674                 error = g_access(cp, 1, 1, 1);
  675                 if (error) {
  676                         gctl_error(req, "could not access consumer");
  677                         return;
  678                 }
  679                 error = g_bsd_writelabel(gp, label);
  680                 g_access(cp, -1, -1, -1);
  681         } else {
  682                 gctl_error(req, "Unknown verb parameter");
  683         }
  684 
  685         return;
  686 }
  687 
  688 /* Finally, register with GEOM infrastructure. */
  689 static struct g_class g_bsd_class = {
  690         .name = BSD_CLASS_NAME,
  691         .version = G_VERSION,
  692         .taste = g_bsd_taste,
  693         .ctlreq = g_bsd_config,
  694         .dumpconf = g_bsd_dumpconf,
  695         .ioctl = g_bsd_ioctl,
  696 };
  697 
  698 DECLARE_GEOM_CLASS(g_bsd_class, g_bsd);

Cache object: b75e1f6621a17b8c379e3f8b4fb7bf91


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