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  * $FreeBSD: releng/5.0/sys/geom/geom_slice.c 108186 2002-12-22 18:00:18Z phk $
   36  */
   37 
   38 
   39 #include <sys/param.h>
   40 #include <sys/stdint.h>
   41 #ifndef _KERNEL
   42 #include <stdio.h>
   43 #include <unistd.h>
   44 #include <stdlib.h>
   45 #include <signal.h>
   46 #include <string.h>
   47 #include <err.h>
   48 #else
   49 #include <sys/systm.h>
   50 #include <sys/kernel.h>
   51 #include <sys/malloc.h>
   52 #include <sys/bio.h>
   53 #include <sys/sysctl.h>
   54 #include <sys/proc.h>
   55 #include <sys/kthread.h>
   56 #include <sys/lock.h>
   57 #include <sys/mutex.h>
   58 #endif
   59 #include <sys/errno.h>
   60 #include <sys/sbuf.h>
   61 #include <geom/geom.h>
   62 #include <geom/geom_slice.h>
   63 #include <machine/stdarg.h>
   64 
   65 static g_orphan_t g_slice_orphan;
   66 static g_access_t g_slice_access;
   67 static g_start_t g_slice_start;
   68 
   69 static struct g_slicer *
   70 g_slice_init(unsigned nslice, unsigned scsize)
   71 {
   72         struct g_slicer *gsp;
   73 
   74         gsp = g_malloc(sizeof *gsp, M_WAITOK | M_ZERO);
   75         gsp->softc = g_malloc(scsize, M_WAITOK | M_ZERO);
   76         gsp->slices = g_malloc(nslice * sizeof(struct g_slice),
   77             M_WAITOK | M_ZERO);
   78         gsp->nslice = nslice;
   79         return (gsp);
   80 }
   81 
   82 static int
   83 g_slice_access(struct g_provider *pp, int dr, int dw, int de)
   84 {
   85         int error;
   86         u_int u;
   87         struct g_geom *gp;
   88         struct g_consumer *cp;
   89         struct g_provider *pp2;
   90         struct g_slicer *gsp;
   91         struct g_slice *gsl, *gsl2;
   92 
   93         gp = pp->geom;
   94         cp = LIST_FIRST(&gp->consumer);
   95         KASSERT (cp != NULL, ("g_slice_access but no consumer"));
   96         gsp = gp->softc;
   97         gsl = &gsp->slices[pp->index];
   98         for (u = 0; u < gsp->nslice; u++) {
   99                 gsl2 = &gsp->slices[u];
  100                 if (gsl2->length == 0)
  101                         continue;
  102                 if (u == pp->index)
  103                         continue;
  104                 if (gsl->offset + gsl->length <= gsl2->offset)
  105                         continue;
  106                 if (gsl2->offset + gsl2->length <= gsl->offset)
  107                         continue;
  108                 /* overlap */
  109                 pp2 = gsl2->provider;
  110                 if ((pp->acw + dw) > 0 && pp2->ace > 0)
  111                         return (EPERM);
  112                 if ((pp->ace + de) > 0 && pp2->acw > 0)
  113                         return (EPERM);
  114         }
  115         /* On first open, grab an extra "exclusive" bit */
  116         if (cp->acr == 0 && cp->acw == 0 && cp->ace == 0)
  117                 de++;
  118         /* ... and let go of it on last close */
  119         if ((cp->acr + dr) == 0 && (cp->acw + dw) == 0 && (cp->ace + de) == 1)
  120                 de--;
  121         error = g_access_rel(cp, dr, dw, de);
  122         return (error);
  123 }
  124 
  125 void
  126 g_slice_finish_hot(struct bio *bp)
  127 {
  128         struct bio *bp2;
  129         struct g_geom *gp;
  130         struct g_consumer *cp;
  131         struct g_slicer *gsp;
  132         struct g_slice *gsl;
  133         int idx;
  134 
  135         KASSERT(bp->bio_to != NULL, ("NULL bio_to in g_slice_finish_hot(%p)", bp));
  136         KASSERT(bp->bio_from != NULL, ("NULL bio_from in g_slice_finish_hot(%p)", bp));
  137         gp = bp->bio_to->geom;
  138         gsp = gp->softc;
  139         cp = LIST_FIRST(&gp->consumer);
  140         KASSERT(cp != NULL, ("NULL consumer in g_slice_finish_hot(%p)", bp));
  141         idx = bp->bio_to->index;
  142         gsl = &gsp->slices[idx];
  143 
  144         bp2 = g_clone_bio(bp);
  145         if (bp2 == NULL) {
  146                 g_io_deliver(bp, ENOMEM);
  147                 return;
  148         }
  149         if (bp2->bio_offset + bp2->bio_length > gsl->length)
  150                 bp2->bio_length = gsl->length - bp2->bio_offset;
  151         bp2->bio_done = g_std_done;
  152         bp2->bio_offset += gsl->offset;
  153         g_io_request(bp2, cp);
  154         return;
  155 }
  156 
  157 static void
  158 g_slice_start(struct bio *bp)
  159 {
  160         struct bio *bp2;
  161         struct g_provider *pp;
  162         struct g_geom *gp;
  163         struct g_consumer *cp;
  164         struct g_slicer *gsp;
  165         struct g_slice *gsl, *gmp;
  166         int idx, error;
  167         u_int m_index;
  168         off_t t;
  169 
  170         pp = bp->bio_to;
  171         gp = pp->geom;
  172         gsp = gp->softc;
  173         cp = LIST_FIRST(&gp->consumer);
  174         idx = pp->index;
  175         gsl = &gsp->slices[idx];
  176         switch(bp->bio_cmd) {
  177         case BIO_READ:
  178         case BIO_WRITE:
  179         case BIO_DELETE:
  180                 if (bp->bio_offset > gsl->length) {
  181                         g_io_deliver(bp, EINVAL); /* XXX: EWHAT ? */
  182                         return;
  183                 }
  184                 /*
  185                  * Check if we collide with any hot spaces, and call the
  186                  * method once if so.
  187                  */
  188                 t = bp->bio_offset + gsl->offset;
  189                 /* .ctl devices may take us negative */
  190                 if (t < 0 || (t + bp->bio_length) < 0) {
  191                         g_io_deliver(bp, EINVAL);
  192                         return;
  193                 }
  194                 for (m_index = 0; m_index < gsp->nhot; m_index++) {
  195                         gmp = &gsp->hot[m_index];
  196                         if (t >= gmp->offset + gmp->length)
  197                                 continue;
  198                         if (t + bp->bio_length <= gmp->offset)
  199                                 continue;
  200                         error = gsp->start(bp);
  201                         if (error == EJUSTRETURN)
  202                                 return;
  203                         else if (error) {
  204                                 g_io_deliver(bp, error);
  205                                 return;
  206                         }
  207                         break;
  208                 }
  209                 bp2 = g_clone_bio(bp);
  210                 if (bp2 == NULL) {
  211                         g_io_deliver(bp, ENOMEM);
  212                         return;
  213                 }
  214                 if (bp2->bio_offset + bp2->bio_length > gsl->length)
  215                         bp2->bio_length = gsl->length - bp2->bio_offset;
  216                 bp2->bio_done = g_std_done;
  217                 bp2->bio_offset += gsl->offset;
  218                 g_io_request(bp2, cp);
  219                 return;
  220         case BIO_GETATTR:
  221         case BIO_SETATTR:
  222                 /* Give the real method a chance to override */
  223                 if (gsp->start(bp))
  224                         return;
  225                 if (!strcmp("GEOM::frontstuff", bp->bio_attribute)) {
  226                         t = gsp->cfrontstuff;
  227                         if (gsp->frontstuff > t)
  228                                 t = gsp->frontstuff;
  229                         t -= gsl->offset;
  230                         if (t < 0)
  231                                 t = 0;
  232                         if (t > gsl->length)
  233                                 t = gsl->length;
  234                         g_handleattr_off_t(bp, "GEOM::frontstuff", t);
  235                         return;
  236                 }
  237 #ifdef _KERNEL
  238                 if (!strcmp("GEOM::kerneldump", bp->bio_attribute)) {
  239                         struct g_kerneldump *gkd;
  240 
  241                         gkd = (struct g_kerneldump *)bp->bio_data;
  242                         gkd->offset += gsp->slices[idx].offset;
  243                         if (gkd->length > gsp->slices[idx].length)
  244                                 gkd->length = gsp->slices[idx].length;
  245                         /* now, pass it on downwards... */
  246                 }
  247 #endif
  248                 bp2 = g_clone_bio(bp);
  249                 if (bp2 == NULL) {
  250                         g_io_deliver(bp, ENOMEM);
  251                         return;
  252                 }
  253                 bp2->bio_done = g_std_done;
  254                 g_io_request(bp2, cp);
  255                 break;
  256         default:
  257                 g_io_deliver(bp, EOPNOTSUPP);
  258                 return;
  259         }
  260 }
  261 
  262 void
  263 g_slice_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp)
  264 {
  265         struct g_slicer *gsp;
  266 
  267         gsp = gp->softc;
  268         if (indent == NULL) {
  269                 sbuf_printf(sb, " i %u", pp->index);
  270                 sbuf_printf(sb, " o %ju", 
  271                     (uintmax_t)gsp->slices[pp->index].offset);
  272                 return;
  273         }
  274         if (gp != NULL && (pp == NULL && cp == NULL)) {
  275                 sbuf_printf(sb, "%s<frontstuff>%ju</frontstuff>\n",
  276                     indent, (intmax_t)gsp->frontstuff);
  277         }
  278         if (pp != NULL) {
  279                 sbuf_printf(sb, "%s<index>%u</index>\n", indent, pp->index);
  280                 sbuf_printf(sb, "%s<length>%ju</length>\n",
  281                     indent, (uintmax_t)gsp->slices[pp->index].length);
  282                 sbuf_printf(sb, "%s<seclength>%ju</seclength>\n", indent,
  283                     (uintmax_t)gsp->slices[pp->index].length / 512);
  284                 sbuf_printf(sb, "%s<offset>%ju</offset>\n", indent,
  285                     (uintmax_t)gsp->slices[pp->index].offset);
  286                 sbuf_printf(sb, "%s<secoffset>%ju</secoffset>\n", indent,
  287                     (uintmax_t)gsp->slices[pp->index].offset / 512);
  288         }
  289 }
  290 
  291 int
  292 g_slice_config(struct g_geom *gp, u_int idx, int how, off_t offset, off_t length, u_int sectorsize, const char *fmt, ...)
  293 {
  294         struct g_provider *pp;
  295         struct g_slicer *gsp;
  296         struct g_slice *gsl;
  297         va_list ap;
  298         struct sbuf *sb;
  299         int error, acc;
  300 
  301         g_trace(G_T_TOPOLOGY, "g_slice_config(%s, %d, %d)",
  302              gp->name, idx, how);
  303         g_topology_assert();
  304         gsp = gp->softc;
  305         error = 0;
  306         if (idx >= gsp->nslice)
  307                 return(EINVAL);
  308         gsl = &gsp->slices[idx];
  309         pp = gsl->provider;
  310         if (pp != NULL)
  311                 acc = pp->acr + pp->acw + pp->ace;
  312         else
  313                 acc = 0;
  314         if (acc != 0 && how != G_SLICE_CONFIG_FORCE) {
  315                 if (length < gsl->length)
  316                         return(EBUSY);
  317                 if (offset != gsl->offset)
  318                         return(EBUSY);
  319         }
  320         /* XXX: check offset + length <= MEDIASIZE */
  321         if (how == G_SLICE_CONFIG_CHECK)
  322                 return (0);
  323         gsl->length = length;
  324         gsl->offset = offset;
  325         gsl->sectorsize = sectorsize;
  326         if (length == 0) {
  327                 if (pp == NULL)
  328                         return (0);
  329                 if (bootverbose)
  330                         printf("GEOM: Deconfigure %s\n", pp->name);
  331                 g_orphan_provider(pp, ENXIO);
  332                 gsl->provider = NULL;
  333                 gsp->nprovider--;
  334                 return (0);
  335         }
  336         if (pp != NULL) {
  337                 if (bootverbose)
  338                         printf("GEOM: Reconfigure %s, start %jd length %jd end %jd\n",
  339                             pp->name, (intmax_t)offset, (intmax_t)length,
  340                             (intmax_t)(offset + length - 1));
  341                 pp->mediasize = gsl->length;
  342                 return (0);
  343         }
  344         va_start(ap, fmt);
  345         sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
  346         sbuf_vprintf(sb, fmt, ap);
  347         sbuf_finish(sb);
  348         pp = g_new_providerf(gp, sbuf_data(sb));
  349         if (bootverbose)
  350                 printf("GEOM: Configure %s, start %jd length %jd end %jd\n",
  351                     pp->name, (intmax_t)offset, (intmax_t)length,
  352                     (intmax_t)(offset + length - 1));
  353         pp->index = idx;
  354         pp->mediasize = gsl->length;
  355         pp->sectorsize = gsl->sectorsize;
  356         gsl->provider = pp;
  357         gsp->nprovider++;
  358         g_error_provider(pp, 0);
  359         sbuf_delete(sb);
  360         return(0);
  361 }
  362 
  363 int
  364 g_slice_conf_hot(struct g_geom *gp, u_int idx, off_t offset, off_t length)
  365 {
  366         struct g_slicer *gsp;
  367         struct g_slice *gsl, *gsl2;
  368 
  369         g_trace(G_T_TOPOLOGY, "g_slice_conf_hot()");
  370         g_topology_assert();
  371         gsp = gp->softc;
  372         gsl = gsp->hot;
  373         if(idx >= gsp->nhot) {
  374                 gsl2 = g_malloc((idx + 1) * sizeof *gsl2, M_WAITOK | M_ZERO);
  375                 if (gsp->hot != NULL)
  376                         bcopy(gsp->hot, gsl2, gsp->nhot * sizeof *gsl2);
  377                 gsp->hot = gsl2;
  378                 if (gsp->hot != NULL)
  379                         g_free(gsl);
  380                 gsl = gsl2;
  381                 gsp->nhot = idx + 1;
  382         }
  383         if (bootverbose)
  384                 printf("GEOM: Add %s hot[%d] start %jd length %jd end %jd\n",
  385                     gp->name, idx, (intmax_t)offset, (intmax_t)length,
  386                     (intmax_t)(offset + length - 1));
  387         gsl[idx].offset = offset;
  388         gsl[idx].length = length;
  389         return (0);
  390 }
  391 
  392 struct g_geom *
  393 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)
  394 {
  395         struct g_geom *gp;
  396         struct g_slicer *gsp;
  397         struct g_consumer *cp;
  398         void **vp;
  399         int error, i;
  400 
  401         g_topology_assert();
  402         vp = (void **)extrap;
  403         gp = g_new_geomf(mp, "%s", pp->name);
  404         gsp = g_slice_init(slices, extra);
  405         gsp->start = start;
  406         gp->access = g_slice_access;
  407         gp->orphan = g_slice_orphan;
  408         gp->softc = gsp;
  409         gp->start = g_slice_start;
  410         gp->spoiled = g_std_spoiled;
  411         gp->dumpconf = g_slice_dumpconf;
  412         cp = g_new_consumer(gp);
  413         error = g_attach(cp, pp);
  414         if (error == 0)
  415                 error = g_access_rel(cp, 1, 0, 0);
  416         if (error) {
  417                 if (cp->provider != NULL)
  418                         g_detach(cp);
  419                 g_destroy_consumer(cp);
  420                 g_free(gsp->slices);
  421                 g_free(gp->softc);
  422                 g_destroy_geom(gp);
  423                 return (NULL);
  424         }
  425         /* Find out if there are any magic bytes on the consumer */
  426         i = sizeof gsp->cfrontstuff;
  427         error = g_io_getattr("GEOM::frontstuff", cp, &i, &gsp->cfrontstuff);
  428         if (error)
  429                 gsp->cfrontstuff = 0;
  430         *vp = gsp->softc;
  431         *cpp = cp;
  432         return (gp);
  433 }
  434 
  435 static void
  436 g_slice_orphan(struct g_consumer *cp)
  437 {
  438         struct g_geom *gp;
  439         struct g_provider *pp;
  440         int error;
  441 
  442         g_trace(G_T_TOPOLOGY, "g_slice_orphan(%p/%s)", cp, cp->provider->name);
  443         g_topology_assert();
  444         KASSERT(cp->provider->error != 0,
  445             ("g_slice_orphan with error == 0"));
  446 
  447         gp = cp->geom;
  448         /* XXX: Not good enough we leak the softc and its suballocations */
  449         gp->flags |= G_GEOM_WITHER;
  450         error = cp->provider->error;
  451         LIST_FOREACH(pp, &gp->provider, provider)
  452                 g_orphan_provider(pp, error);
  453         return;
  454 }

Cache object: 2208849f9e9d30f8ec24a9e689139d64


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