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_slice.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: releng/6.2/sys/geom/geom_slice.c 156163 2006-03-01 17:57:11Z pjd $");
   38 
   39 #include <sys/param.h>
   40 #include <sys/systm.h>
   41 #include <sys/kernel.h>
   42 #include <sys/malloc.h>
   43 #include <sys/bio.h>
   44 #include <sys/sysctl.h>
   45 #include <sys/proc.h>
   46 #include <sys/kthread.h>
   47 #include <sys/lock.h>
   48 #include <sys/mutex.h>
   49 #include <sys/errno.h>
   50 #include <sys/sbuf.h>
   51 #include <geom/geom.h>
   52 #include <geom/geom_slice.h>
   53 #include <machine/stdarg.h>
   54 
   55 static g_access_t g_slice_access;
   56 static g_start_t g_slice_start;
   57 
   58 static struct g_slicer *
   59 g_slice_alloc(unsigned nslice, unsigned scsize)
   60 {
   61         struct g_slicer *gsp;
   62 
   63         gsp = g_malloc(sizeof *gsp, M_WAITOK | M_ZERO);
   64         if (scsize > 0)
   65                 gsp->softc = g_malloc(scsize, M_WAITOK | M_ZERO);
   66         else
   67                 gsp->softc = NULL;
   68         gsp->slices = g_malloc(nslice * sizeof(struct g_slice),
   69             M_WAITOK | M_ZERO);
   70         gsp->nslice = nslice;
   71         return (gsp);
   72 }
   73 
   74 static void
   75 g_slice_free(struct g_slicer *gsp)
   76 {
   77 
   78         if (gsp == NULL)        /* XXX: phk thinks about this */
   79                 return;
   80         g_free(gsp->slices);
   81         if (gsp->hotspot != NULL)
   82                 g_free(gsp->hotspot);
   83         if (gsp->softc != NULL)
   84                 g_free(gsp->softc);
   85         g_free(gsp);
   86 }
   87 
   88 static int
   89 g_slice_access(struct g_provider *pp, int dr, int dw, int de)
   90 {
   91         int error;
   92         u_int u;
   93         struct g_geom *gp;
   94         struct g_consumer *cp;
   95         struct g_provider *pp2;
   96         struct g_slicer *gsp;
   97         struct g_slice *gsl, *gsl2;
   98 
   99         gp = pp->geom;
  100         cp = LIST_FIRST(&gp->consumer);
  101         KASSERT (cp != NULL, ("g_slice_access but no consumer"));
  102         gsp = gp->softc;
  103         if (dr > 0 || dw > 0 || de > 0) {
  104                 gsl = &gsp->slices[pp->index];
  105                 for (u = 0; u < gsp->nslice; u++) {
  106                         gsl2 = &gsp->slices[u];
  107                         if (gsl2->length == 0)
  108                                 continue;
  109                         if (u == pp->index)
  110                                 continue;
  111                         if (gsl->offset + gsl->length <= gsl2->offset)
  112                                 continue;
  113                         if (gsl2->offset + gsl2->length <= gsl->offset)
  114                                 continue;
  115                         /* overlap */
  116                         pp2 = gsl2->provider;
  117                         if ((pp->acw + dw) > 0 && pp2->ace > 0)
  118                                 return (EPERM);
  119                         if ((pp->ace + de) > 0 && pp2->acw > 0)
  120                                 return (EPERM);
  121                 }
  122         }
  123         /* On first open, grab an extra "exclusive" bit */
  124         if (cp->acr == 0 && cp->acw == 0 && cp->ace == 0)
  125                 de++;
  126         /* ... and let go of it on last close */
  127         if ((cp->acr + dr) == 0 && (cp->acw + dw) == 0 && (cp->ace + de) == 1)
  128                 de--;
  129         error = g_access(cp, dr, dw, de);
  130         return (error);
  131 }
  132 
  133 /*
  134  * XXX: It should be possible to specify here if we should finish all of the
  135  * XXX: bio, or only the non-hot bits.  This would get messy if there were
  136  * XXX: two hot spots in the same bio, so for now we simply finish off the
  137  * XXX: entire bio.  Modifying hot data on the way to disk is frowned on
  138  * XXX: so making that considerably harder is not a bad idea anyway.
  139  */
  140 void
  141 g_slice_finish_hot(struct bio *bp)
  142 {
  143         struct bio *bp2;
  144         struct g_geom *gp;
  145         struct g_consumer *cp;
  146         struct g_slicer *gsp;
  147         struct g_slice *gsl;
  148         int idx;
  149 
  150         KASSERT(bp->bio_to != NULL,
  151             ("NULL bio_to in g_slice_finish_hot(%p)", bp));
  152         KASSERT(bp->bio_from != NULL,
  153             ("NULL bio_from in g_slice_finish_hot(%p)", bp));
  154         gp = bp->bio_to->geom;
  155         gsp = gp->softc;
  156         cp = LIST_FIRST(&gp->consumer);
  157         KASSERT(cp != NULL, ("NULL consumer in g_slice_finish_hot(%p)", bp));
  158         idx = bp->bio_to->index;
  159         gsl = &gsp->slices[idx];
  160 
  161         bp2 = g_clone_bio(bp);
  162         if (bp2 == NULL) {
  163                 g_io_deliver(bp, ENOMEM);
  164                 return;
  165         }
  166         if (bp2->bio_offset + bp2->bio_length > gsl->length)
  167                 bp2->bio_length = gsl->length - bp2->bio_offset;
  168         bp2->bio_done = g_std_done;
  169         bp2->bio_offset += gsl->offset;
  170         g_io_request(bp2, cp);
  171         return;
  172 }
  173 
  174 static void
  175 g_slice_start(struct bio *bp)
  176 {
  177         struct bio *bp2;
  178         struct g_provider *pp;
  179         struct g_geom *gp;
  180         struct g_consumer *cp;
  181         struct g_slicer *gsp;
  182         struct g_slice *gsl;
  183         struct g_slice_hot *ghp;
  184         int idx, error;
  185         u_int m_index;
  186         off_t t;
  187 
  188         pp = bp->bio_to;
  189         gp = pp->geom;
  190         gsp = gp->softc;
  191         cp = LIST_FIRST(&gp->consumer);
  192         idx = pp->index;
  193         gsl = &gsp->slices[idx];
  194         switch(bp->bio_cmd) {
  195         case BIO_READ:
  196         case BIO_WRITE:
  197         case BIO_DELETE:
  198                 if (bp->bio_offset > gsl->length) {
  199                         g_io_deliver(bp, EINVAL); /* XXX: EWHAT ? */
  200                         return;
  201                 }
  202                 /*
  203                  * Check if we collide with any hot spaces, and call the
  204                  * method once if so.
  205                  */
  206                 t = bp->bio_offset + gsl->offset;
  207                 for (m_index = 0; m_index < gsp->nhotspot; m_index++) {
  208                         ghp = &gsp->hotspot[m_index];
  209                         if (t >= ghp->offset + ghp->length)
  210                                 continue;
  211                         if (t + bp->bio_length <= ghp->offset)
  212                                 continue;
  213                         switch(bp->bio_cmd) {
  214                         case BIO_READ:          idx = ghp->ract; break;
  215                         case BIO_WRITE:         idx = ghp->wact; break;
  216                         case BIO_DELETE:        idx = ghp->dact; break;
  217                         }
  218                         switch(idx) {
  219                         case G_SLICE_HOT_ALLOW:
  220                                 /* Fall out and continue normal processing */
  221                                 continue;
  222                         case G_SLICE_HOT_DENY:
  223                                 g_io_deliver(bp, EROFS);
  224                                 return;
  225                         case G_SLICE_HOT_START:
  226                                 error = gsp->start(bp);
  227                                 if (error && error != EJUSTRETURN)
  228                                         g_io_deliver(bp, error);
  229                                 return;
  230                         case G_SLICE_HOT_CALL:
  231                                 error = g_post_event(gsp->hot, bp, M_NOWAIT,
  232                                     gp, NULL);
  233                                 if (error)
  234                                         g_io_deliver(bp, error);
  235                                 return;
  236                         }
  237                         break;
  238                 }
  239                 bp2 = g_clone_bio(bp);
  240                 if (bp2 == NULL) {
  241                         g_io_deliver(bp, ENOMEM);
  242                         return;
  243                 }
  244                 if (bp2->bio_offset + bp2->bio_length > gsl->length)
  245                         bp2->bio_length = gsl->length - bp2->bio_offset;
  246                 bp2->bio_done = g_std_done;
  247                 bp2->bio_offset += gsl->offset;
  248                 g_io_request(bp2, cp);
  249                 return;
  250         case BIO_GETATTR:
  251                 /* Give the real method a chance to override */
  252                 if (gsp->start != NULL && gsp->start(bp))
  253                         return;
  254                 if (!strcmp("GEOM::kerneldump", bp->bio_attribute)) {
  255                         struct g_kerneldump *gkd;
  256 
  257                         gkd = (struct g_kerneldump *)bp->bio_data;
  258                         gkd->offset += gsp->slices[idx].offset;
  259                         if (gkd->length > gsp->slices[idx].length)
  260                                 gkd->length = gsp->slices[idx].length;
  261                         /* now, pass it on downwards... */
  262                 }
  263                 bp2 = g_clone_bio(bp);
  264                 if (bp2 == NULL) {
  265                         g_io_deliver(bp, ENOMEM);
  266                         return;
  267                 }
  268                 bp2->bio_done = g_std_done;
  269                 g_io_request(bp2, cp);
  270                 break;
  271         default:
  272                 g_io_deliver(bp, EOPNOTSUPP);
  273                 return;
  274         }
  275 }
  276 
  277 void
  278 g_slice_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp)
  279 {
  280         struct g_slicer *gsp;
  281 
  282         gsp = gp->softc;
  283         if (indent == NULL) {
  284                 sbuf_printf(sb, " i %u", pp->index);
  285                 sbuf_printf(sb, " o %ju", 
  286                     (uintmax_t)gsp->slices[pp->index].offset);
  287                 return;
  288         }
  289         if (pp != NULL) {
  290                 sbuf_printf(sb, "%s<index>%u</index>\n", indent, pp->index);
  291                 sbuf_printf(sb, "%s<length>%ju</length>\n",
  292                     indent, (uintmax_t)gsp->slices[pp->index].length);
  293                 sbuf_printf(sb, "%s<seclength>%ju</seclength>\n", indent,
  294                     (uintmax_t)gsp->slices[pp->index].length / 512);
  295                 sbuf_printf(sb, "%s<offset>%ju</offset>\n", indent,
  296                     (uintmax_t)gsp->slices[pp->index].offset);
  297                 sbuf_printf(sb, "%s<secoffset>%ju</secoffset>\n", indent,
  298                     (uintmax_t)gsp->slices[pp->index].offset / 512);
  299         }
  300 }
  301 
  302 int
  303 g_slice_config(struct g_geom *gp, u_int idx, int how, off_t offset, off_t length, u_int sectorsize, const char *fmt, ...)
  304 {
  305         struct g_provider *pp, *pp2;
  306         struct g_slicer *gsp;
  307         struct g_slice *gsl;
  308         va_list ap;
  309         struct sbuf *sb;
  310         int acc;
  311 
  312         g_trace(G_T_TOPOLOGY, "g_slice_config(%s, %d, %d)",
  313              gp->name, idx, how);
  314         g_topology_assert();
  315         gsp = gp->softc;
  316         if (idx >= gsp->nslice)
  317                 return(EINVAL);
  318         gsl = &gsp->slices[idx];
  319         pp = gsl->provider;
  320         if (pp != NULL)
  321                 acc = pp->acr + pp->acw + pp->ace;
  322         else
  323                 acc = 0;
  324         if (acc != 0 && how != G_SLICE_CONFIG_FORCE) {
  325                 if (length < gsl->length)
  326                         return(EBUSY);
  327                 if (offset != gsl->offset)
  328                         return(EBUSY);
  329         }
  330         /* XXX: check offset + length <= MEDIASIZE */
  331         if (how == G_SLICE_CONFIG_CHECK)
  332                 return (0);
  333         gsl->length = length;
  334         gsl->offset = offset;
  335         gsl->sectorsize = sectorsize;
  336         if (length == 0) {
  337                 if (pp == NULL)
  338                         return (0);
  339                 if (bootverbose)
  340                         printf("GEOM: Deconfigure %s\n", pp->name);
  341                 pp->flags |= G_PF_WITHER;
  342                 g_orphan_provider(pp, ENXIO);
  343                 gsl->provider = NULL;
  344                 gsp->nprovider--;
  345                 return (0);
  346         }
  347         if (pp != NULL) {
  348                 if (bootverbose)
  349                         printf("GEOM: Reconfigure %s, start %jd length %jd end %jd\n",
  350                             pp->name, (intmax_t)offset, (intmax_t)length,
  351                             (intmax_t)(offset + length - 1));
  352                 pp->mediasize = gsl->length;
  353                 return (0);
  354         }
  355         sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
  356         va_start(ap, fmt);
  357         sbuf_vprintf(sb, fmt, ap);
  358         va_end(ap);
  359         sbuf_finish(sb);
  360         pp = g_new_providerf(gp, sbuf_data(sb));
  361         pp2 = LIST_FIRST(&gp->consumer)->provider;
  362         pp->flags = pp2->flags & G_PF_CANDELETE;
  363         if (pp2->stripesize > 0) {
  364                 pp->stripesize = pp2->stripesize;
  365                 pp->stripeoffset = (pp2->stripeoffset + offset) % pp->stripesize;
  366         }
  367         if (0 && bootverbose)
  368                 printf("GEOM: Configure %s, start %jd length %jd end %jd\n",
  369                     pp->name, (intmax_t)offset, (intmax_t)length,
  370                     (intmax_t)(offset + length - 1));
  371         pp->index = idx;
  372         pp->mediasize = gsl->length;
  373         pp->sectorsize = gsl->sectorsize;
  374         gsl->provider = pp;
  375         gsp->nprovider++;
  376         g_error_provider(pp, 0);
  377         sbuf_delete(sb);
  378         return(0);
  379 }
  380 
  381 /*
  382  * Configure "hotspots".  A hotspot is a piece of the parent device which
  383  * this particular slicer cares about for some reason.  Typically because
  384  * it contains meta-data used to configure the slicer.
  385  * A hotspot is identified by its index number. The offset and length are
  386  * relative to the parent device, and the three "?act" fields specify
  387  * what action to take on BIO_READ, BIO_DELETE and BIO_WRITE.
  388  *
  389  * XXX: There may be a race relative to g_slice_start() here, if an existing
  390  * XXX: hotspot is changed wile I/O is happening.  Should this become a problem
  391  * XXX: we can protect the hotspot stuff with a mutex.
  392  */
  393 
  394 int
  395 g_slice_conf_hot(struct g_geom *gp, u_int idx, off_t offset, off_t length, int ract, int dact, int wact)
  396 {
  397         struct g_slicer *gsp;
  398         struct g_slice_hot *gsl, *gsl2;
  399 
  400         g_trace(G_T_TOPOLOGY, "g_slice_conf_hot(%s, idx: %d, off: %jd, len: %jd)",
  401             gp->name, idx, (intmax_t)offset, (intmax_t)length);
  402         g_topology_assert();
  403         gsp = gp->softc;
  404         gsl = gsp->hotspot;
  405         if(idx >= gsp->nhotspot) {
  406                 gsl2 = g_malloc((idx + 1) * sizeof *gsl2, M_WAITOK | M_ZERO);
  407                 if (gsp->hotspot != NULL)
  408                         bcopy(gsp->hotspot, gsl2, gsp->nhotspot * sizeof *gsl2);
  409                 gsp->hotspot = gsl2;
  410                 if (gsp->hotspot != NULL)
  411                         g_free(gsl);
  412                 gsl = gsl2;
  413                 gsp->nhotspot = idx + 1;
  414         }
  415         gsl[idx].offset = offset;
  416         gsl[idx].length = length;
  417         KASSERT(!((ract | dact | wact) & G_SLICE_HOT_START)
  418             || gsp->start != NULL, ("G_SLICE_HOT_START but no slice->start"));
  419         /* XXX: check that we _have_ a start function if HOT_START specified */
  420         gsl[idx].ract = ract;
  421         gsl[idx].dact = dact;
  422         gsl[idx].wact = wact;
  423         return (0);
  424 }
  425 
  426 void
  427 g_slice_spoiled(struct g_consumer *cp)
  428 {
  429         struct g_geom *gp;
  430         struct g_slicer *gsp;
  431 
  432         g_topology_assert();
  433         gp = cp->geom;
  434         g_trace(G_T_TOPOLOGY, "g_slice_spoiled(%p/%s)", cp, gp->name);
  435         gsp = gp->softc;
  436         gp->softc = NULL;
  437         g_slice_free(gsp);
  438         g_wither_geom(gp, ENXIO);
  439 }
  440 
  441 int
  442 g_slice_destroy_geom(struct gctl_req *req, struct g_class *mp, struct g_geom *gp)
  443 {
  444 
  445         g_slice_spoiled(LIST_FIRST(&gp->consumer));
  446         return (0);
  447 }
  448 
  449 struct g_geom *
  450 g_slice_new(struct g_class *mp, u_int slices, struct g_provider *pp, struct g_consumer **cpp, void *extrap, int extra, g_slice_start_t *start)
  451 {
  452         struct g_geom *gp;
  453         struct g_slicer *gsp;
  454         struct g_consumer *cp;
  455         void **vp;
  456         int error;
  457 
  458         g_topology_assert();
  459         vp = (void **)extrap;
  460         gp = g_new_geomf(mp, "%s", pp->name);
  461         gsp = g_slice_alloc(slices, extra);
  462         gsp->start = start;
  463         gp->access = g_slice_access;
  464         gp->orphan = g_slice_orphan;
  465         gp->softc = gsp;
  466         gp->start = g_slice_start;
  467         gp->spoiled = g_slice_spoiled;
  468         if (gp->dumpconf == NULL)
  469                 gp->dumpconf = g_slice_dumpconf;
  470         if (gp->class->destroy_geom == NULL)
  471                 gp->class->destroy_geom = g_slice_destroy_geom;
  472         cp = g_new_consumer(gp);
  473         error = g_attach(cp, pp);
  474         if (error == 0)
  475                 error = g_access(cp, 1, 0, 0);
  476         if (error) {
  477                 g_wither_geom(gp, ENXIO);
  478                 return (NULL);
  479         }
  480         if (extrap != NULL)
  481                 *vp = gsp->softc;
  482         *cpp = cp;
  483         return (gp);
  484 }
  485 
  486 void
  487 g_slice_orphan(struct g_consumer *cp)
  488 {
  489 
  490         g_trace(G_T_TOPOLOGY, "g_slice_orphan(%p/%s)", cp, cp->provider->name);
  491         g_topology_assert();
  492         KASSERT(cp->provider->error != 0,
  493             ("g_slice_orphan with error == 0"));
  494 
  495         /* XXX: Not good enough we leak the softc and its suballocations */
  496         g_slice_free(cp->geom->softc);
  497         g_wither_geom(cp->geom, cp->provider->error);
  498 }

Cache object: 1b4109516eaaf1999fa882a6d0f66ed1


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