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/6.4/sys/geom/geom_bsd.c 174738 2007-12-18 01:32:55Z 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  * any magic bits which indicate that we should automatically put a BSD
  455  * geom on it.
  456  *
  457  * There may be cases where the operator would like to put a BSD-geom on
  458  * providers which do not meet all of the requirements.  This can be done
  459  * by instead passing the G_TF_INSIST flag, which will override these
  460  * checks.
  461  *
  462  * The final flags value is G_TF_TRANSPARENT, which instructs the method
  463  * to put a geom on top of the provider and configure it to be as transparent
  464  * as possible.  This is not really relevant to the BSD method and therefore
  465  * not implemented here.
  466  */
  467 
  468 static struct uuid freebsd_slice = GPT_ENT_TYPE_FREEBSD;
  469 
  470 static struct g_geom *
  471 g_bsd_taste(struct g_class *mp, struct g_provider *pp, int flags)
  472 {
  473         struct g_geom *gp;
  474         struct g_consumer *cp;
  475         int error, i;
  476         struct g_bsd_softc *ms;
  477         u_int secsize;
  478         struct g_slicer *gsp;
  479         u_char hash[16];
  480         MD5_CTX md5sum;
  481         struct uuid uuid;
  482 
  483         g_trace(G_T_TOPOLOGY, "bsd_taste(%s,%s)", mp->name, pp->name);
  484         g_topology_assert();
  485 
  486         /* We don't implement transparent inserts. */
  487         if (flags == G_TF_TRANSPARENT)
  488                 return (NULL);
  489 
  490         /*
  491          * BSD labels are a subclass of the general "slicing" topology so
  492          * a lot of the work can be done by the common "slice" code.
  493          * Create a geom with space for MAXPARTITIONS providers, one consumer
  494          * and a softc structure for us.  Specify the provider to attach
  495          * the consumer to and our "start" routine for special requests.
  496          * The provider is opened with mode (1,0,0) so we can do reads
  497          * from it.
  498          */
  499         gp = g_slice_new(mp, MAXPARTITIONS, pp, &cp, &ms,
  500              sizeof(*ms), g_bsd_start);
  501         if (gp == NULL)
  502                 return (NULL);
  503 
  504         /* Get the geom_slicer softc from the geom. */
  505         gsp = gp->softc;
  506 
  507         /*
  508          * The do...while loop here allows us to have multiple escapes
  509          * using a simple "break".  This improves code clarity without
  510          * ending up in deep nesting and without using goto or come from.
  511          */
  512         do {
  513                 /*
  514                  * If the provider is an MBR we will only auto attach
  515                  * to type 165 slices in the G_TF_NORMAL case.  We will
  516                  * attach to any other type.
  517                  */
  518                 error = g_getattr("MBR::type", cp, &i);
  519                 if (!error) {
  520                         if (i != 165 && flags == G_TF_NORMAL)
  521                                 break;
  522                         error = g_getattr("MBR::offset", cp, &ms->mbroffset);
  523                         if (error)
  524                                 break;
  525                 }
  526 
  527                 /* Same thing if we are inside a PC98 */
  528                 error = g_getattr("PC98::type", cp, &i);
  529                 if (!error) {
  530                         if (i != 0xc494 && flags == G_TF_NORMAL)
  531                                 break;
  532                         error = g_getattr("PC98::offset", cp, &ms->mbroffset);
  533                         if (error)
  534                                 break;
  535                 }
  536 
  537                 /* Same thing if we are inside a GPT */
  538                 error = g_getattr("GPT::type", cp, &uuid);
  539                 if (!error) {
  540                         if (memcmp(&uuid, &freebsd_slice, sizeof(uuid)) != 0 &&
  541                             flags == G_TF_NORMAL)
  542                                 break;
  543                 }
  544 
  545                 /* Get sector size, we need it to read data. */
  546                 secsize = cp->provider->sectorsize;
  547                 if (secsize < 512)
  548                         break;
  549 
  550                 /* First look for a label at the start of the second sector. */
  551                 error = g_bsd_try(gp, gsp, cp, secsize, ms, secsize);
  552 
  553                 /*
  554                  * If sector size is not 512 the label still can be at
  555                  * offset 512, not at the start of the second sector. At least
  556                  * it's true for labels created by the FreeBSD's bsdlabel(8).
  557                  */
  558                 if (error && secsize != HISTORIC_LABEL_OFFSET)
  559                         error = g_bsd_try(gp, gsp, cp, secsize, ms,
  560                             HISTORIC_LABEL_OFFSET);
  561 
  562                 /* Next, look for alpha labels */
  563                 if (error)
  564                         error = g_bsd_try(gp, gsp, cp, secsize, ms,
  565                             ALPHA_LABEL_OFFSET);
  566 
  567                 /* If we didn't find a label, punt. */
  568                 if (error)
  569                         break;
  570 
  571                 /*
  572                  * In order to avoid recursively attaching to the same
  573                  * on-disk label (it's usually visible through the 'c'
  574                  * partition) we calculate an MD5 and ask if other BSD's
  575                  * below us love that label.  If they do, we don't.
  576                  */
  577                 MD5Init(&md5sum);
  578                 MD5Update(&md5sum, ms->label, sizeof(ms->label));
  579                 MD5Final(ms->labelsum, &md5sum);
  580 
  581                 error = g_getattr("BSD::labelsum", cp, &hash);
  582                 if (!error && !bcmp(ms->labelsum, hash, sizeof(hash)))
  583                         break;
  584 
  585                 /*
  586                  * Process the found disklabel, and modify our "slice"
  587                  * instance to match it, if possible.
  588                  */
  589                 error = g_bsd_modify(gp, ms->label);
  590         } while (0);
  591 
  592         /* Success or failure, we can close our provider now. */
  593         g_access(cp, -1, 0, 0);
  594 
  595         /* If we have configured any providers, return the new geom. */
  596         if (gsp->nprovider > 0) {
  597                 g_slice_conf_hot(gp, 0, ms->labeloffset, LABELSIZE,
  598                     G_SLICE_HOT_ALLOW, G_SLICE_HOT_DENY, G_SLICE_HOT_CALL);
  599                 gsp->hot = g_bsd_hotwrite;
  600                 return (gp);
  601         }
  602         /*
  603          * ...else push the "self-destruct" button, by spoiling our own
  604          * consumer.  This triggers a call to g_slice_spoiled which will
  605          * dismantle what was setup.
  606          */
  607         g_slice_spoiled(cp);
  608         return (NULL);
  609 }
  610 
  611 struct h0h0 {
  612         struct g_geom *gp;
  613         struct g_bsd_softc *ms;
  614         u_char *label;
  615         int error;
  616 };
  617 
  618 static void
  619 g_bsd_callconfig(void *arg, int flag)
  620 {
  621         struct h0h0 *hp;
  622 
  623         hp = arg;
  624         hp->error = g_bsd_modify(hp->gp, hp->label);
  625         if (!hp->error)
  626                 hp->error = g_bsd_writelabel(hp->gp, NULL);
  627 }
  628 
  629 /*
  630  * NB! curthread is user process which GCTL'ed.
  631  */
  632 static void
  633 g_bsd_config(struct gctl_req *req, struct g_class *mp, char const *verb)
  634 {
  635         u_char *label;
  636         int error;
  637         struct h0h0 h0h0;
  638         struct g_geom *gp;
  639         struct g_slicer *gsp;
  640         struct g_consumer *cp;
  641         struct g_bsd_softc *ms;
  642 
  643         g_topology_assert();
  644         gp = gctl_get_geom(req, mp, "geom");
  645         if (gp == NULL)
  646                 return;
  647         cp = LIST_FIRST(&gp->consumer);
  648         gsp = gp->softc;
  649         ms = gsp->softc;
  650         if (!strcmp(verb, "read mbroffset")) {
  651                 gctl_set_param(req, "mbroffset",
  652                     &ms->mbroffset, sizeof(ms->mbroffset));
  653                 return;
  654         } else if (!strcmp(verb, "write label")) {
  655                 label = gctl_get_paraml(req, "label", LABELSIZE);
  656                 if (label == NULL)
  657                         return;
  658                 h0h0.gp = gp;
  659                 h0h0.ms = gsp->softc;
  660                 h0h0.label = label;
  661                 h0h0.error = -1;
  662                 /* XXX: Does this reference register with our selfdestruct code ? */
  663                 error = g_access(cp, 1, 1, 1);
  664                 if (error) {
  665                         gctl_error(req, "could not access consumer");
  666                         return;
  667                 }
  668                 g_bsd_callconfig(&h0h0, 0);
  669                 error = h0h0.error;
  670                 g_access(cp, -1, -1, -1);
  671         } else if (!strcmp(verb, "write bootcode")) {
  672                 label = gctl_get_paraml(req, "bootcode", BBSIZE);
  673                 if (label == NULL)
  674                         return;
  675                 /* XXX: Does this reference register with our selfdestruct code ? */
  676                 error = g_access(cp, 1, 1, 1);
  677                 if (error) {
  678                         gctl_error(req, "could not access consumer");
  679                         return;
  680                 }
  681                 error = g_bsd_writelabel(gp, label);
  682                 g_access(cp, -1, -1, -1);
  683         } else {
  684                 gctl_error(req, "Unknown verb parameter");
  685         }
  686 
  687         return;
  688 }
  689 
  690 /* Finally, register with GEOM infrastructure. */
  691 static struct g_class g_bsd_class = {
  692         .name = BSD_CLASS_NAME,
  693         .version = G_VERSION,
  694         .taste = g_bsd_taste,
  695         .ctlreq = g_bsd_config,
  696         .dumpconf = g_bsd_dumpconf,
  697         .ioctl = g_bsd_ioctl,
  698 };
  699 
  700 DECLARE_GEOM_CLASS(g_bsd_class, g_bsd);

Cache object: ec6147caa9eacc73a426898bbc078e21


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