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_dev.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/8.4/sys/geom/geom_dev.c 234912 2012-05-02 06:52:00Z mav $");
   38 
   39 #include <sys/param.h>
   40 #include <sys/systm.h>
   41 #include <sys/malloc.h>
   42 #include <sys/kernel.h>
   43 #include <sys/conf.h>
   44 #include <sys/bio.h>
   45 #include <sys/lock.h>
   46 #include <sys/mutex.h>
   47 #include <sys/proc.h>
   48 #include <sys/errno.h>
   49 #include <sys/time.h>
   50 #include <sys/disk.h>
   51 #include <sys/fcntl.h>
   52 #include <sys/limits.h>
   53 #include <geom/geom.h>
   54 #include <geom/geom_int.h>
   55 
   56 static d_open_t         g_dev_open;
   57 static d_close_t        g_dev_close;
   58 static d_strategy_t     g_dev_strategy;
   59 static d_ioctl_t        g_dev_ioctl;
   60 
   61 static struct cdevsw g_dev_cdevsw = {
   62         .d_version =    D_VERSION,
   63         .d_open =       g_dev_open,
   64         .d_close =      g_dev_close,
   65         .d_read =       physread,
   66         .d_write =      physwrite,
   67         .d_ioctl =      g_dev_ioctl,
   68         .d_strategy =   g_dev_strategy,
   69         .d_name =       "g_dev",
   70         .d_flags =      D_DISK | D_TRACKCLOSE,
   71 };
   72 
   73 static g_taste_t g_dev_taste;
   74 static g_orphan_t g_dev_orphan;
   75 
   76 static struct g_class g_dev_class       = {
   77         .name = "DEV",
   78         .version = G_VERSION,
   79         .taste = g_dev_taste,
   80         .orphan = g_dev_orphan,
   81 };
   82 
   83 void
   84 g_dev_print(void)
   85 {
   86         struct g_geom *gp;
   87         char const *p = "";
   88 
   89         LIST_FOREACH(gp, &g_dev_class.geom, geom) {
   90                 printf("%s%s", p, gp->name);
   91                 p = " ";
   92         }
   93         printf("\n");
   94 }
   95 
   96 struct g_provider *
   97 g_dev_getprovider(struct cdev *dev)
   98 {
   99         struct g_consumer *cp;
  100 
  101         g_topology_assert();
  102         if (dev == NULL)
  103                 return (NULL);
  104         if (dev->si_devsw != &g_dev_cdevsw)
  105                 return (NULL);
  106         cp = dev->si_drv2;
  107         return (cp->provider);
  108 }
  109 
  110 
  111 static struct g_geom *
  112 g_dev_taste(struct g_class *mp, struct g_provider *pp, int insist __unused)
  113 {
  114         struct g_geom *gp;
  115         struct g_consumer *cp;
  116         int error, len;
  117         struct cdev *dev, *adev;
  118         char buf[64], *val;
  119 
  120         g_trace(G_T_TOPOLOGY, "dev_taste(%s,%s)", mp->name, pp->name);
  121         g_topology_assert();
  122         LIST_FOREACH(cp, &pp->consumers, consumers)
  123                 if (cp->geom->class == mp)
  124                         return (NULL);
  125         gp = g_new_geomf(mp, pp->name);
  126         cp = g_new_consumer(gp);
  127         error = g_attach(cp, pp);
  128         KASSERT(error == 0,
  129             ("g_dev_taste(%s) failed to g_attach, err=%d", pp->name, error));
  130         dev = make_dev(&g_dev_cdevsw, 0,
  131             UID_ROOT, GID_OPERATOR, 0640, "%s", gp->name);
  132 
  133         /* Search for device alias name and create it if found. */
  134         adev = NULL;
  135         for (len = MIN(strlen(gp->name), sizeof(buf) - 15); len > 0; len--) {
  136                 snprintf(buf, sizeof(buf), "kern.devalias.%s", gp->name);
  137                 buf[14 + len] = 0;
  138                 val = getenv(buf);
  139                 if (val != NULL) {
  140                         snprintf(buf, sizeof(buf), "%s%s",
  141                             val, gp->name + len);
  142                         freeenv(val);
  143                         adev = make_dev_alias(dev, buf);
  144                         break;
  145                 }
  146         }
  147 
  148         if (pp->flags & G_PF_CANDELETE)
  149                 dev->si_flags |= SI_CANDELETE;
  150         dev->si_iosize_max = MAXPHYS;
  151         gp->softc = dev;
  152         dev->si_drv1 = gp;
  153         dev->si_drv2 = cp;
  154         if (adev != NULL) {
  155                 if (pp->flags & G_PF_CANDELETE)
  156                         adev->si_flags |= SI_CANDELETE;
  157                 adev->si_iosize_max = MAXPHYS;
  158                 adev->si_drv1 = gp;
  159                 adev->si_drv2 = cp;
  160         }
  161         return (gp);
  162 }
  163 
  164 static int
  165 g_dev_open(struct cdev *dev, int flags, int fmt, struct thread *td)
  166 {
  167         struct g_geom *gp;
  168         struct g_consumer *cp;
  169         int error, r, w, e;
  170 
  171         gp = dev->si_drv1;
  172         cp = dev->si_drv2;
  173         if (gp == NULL || cp == NULL || gp->softc != dev)
  174                 return(ENXIO);          /* g_dev_taste() not done yet */
  175 
  176         g_trace(G_T_ACCESS, "g_dev_open(%s, %d, %d, %p)",
  177             gp->name, flags, fmt, td);
  178 
  179         r = flags & FREAD ? 1 : 0;
  180         w = flags & FWRITE ? 1 : 0;
  181 #ifdef notyet
  182         e = flags & O_EXCL ? 1 : 0;
  183 #else
  184         e = 0;
  185 #endif
  186         if (w) {
  187                 /*
  188                  * When running in very secure mode, do not allow
  189                  * opens for writing of any disks.
  190                  */
  191                 error = securelevel_ge(td->td_ucred, 2);
  192                 if (error)
  193                         return (error);
  194         }
  195         g_topology_lock();
  196         if (dev->si_devsw == NULL)
  197                 error = ENXIO;          /* We were orphaned */
  198         else
  199                 error = g_access(cp, r, w, e);
  200         g_topology_unlock();
  201         return(error);
  202 }
  203 
  204 static int
  205 g_dev_close(struct cdev *dev, int flags, int fmt, struct thread *td)
  206 {
  207         struct g_geom *gp;
  208         struct g_consumer *cp;
  209         int error, r, w, e, i;
  210 
  211         gp = dev->si_drv1;
  212         cp = dev->si_drv2;
  213         if (gp == NULL || cp == NULL)
  214                 return(ENXIO);
  215         g_trace(G_T_ACCESS, "g_dev_close(%s, %d, %d, %p)",
  216             gp->name, flags, fmt, td);
  217         r = flags & FREAD ? -1 : 0;
  218         w = flags & FWRITE ? -1 : 0;
  219 #ifdef notyet
  220         e = flags & O_EXCL ? -1 : 0;
  221 #else
  222         e = 0;
  223 #endif
  224         g_topology_lock();
  225         if (dev->si_devsw == NULL)
  226                 error = ENXIO;          /* We were orphaned */
  227         else
  228                 error = g_access(cp, r, w, e);
  229         for (i = 0; i < 10 * hz;) {
  230                 if (cp->acr != 0 || cp->acw != 0)
  231                         break;
  232                 if (cp->nstart == cp->nend)
  233                         break;
  234                 pause("gdevwclose", hz / 10);
  235                 i += hz / 10;
  236         }
  237         if (cp->acr == 0 && cp->acw == 0 && cp->nstart != cp->nend) {
  238                 printf("WARNING: Final close of geom_dev(%s) %s %s\n",
  239                     gp->name,
  240                     "still has outstanding I/O after 10 seconds.",
  241                     "Completing close anyway, panic may happen later.");
  242         }
  243         g_topology_unlock();
  244         return (error);
  245 }
  246 
  247 /*
  248  * XXX: Until we have unmessed the ioctl situation, there is a race against
  249  * XXX: a concurrent orphanization.  We cannot close it by holding topology
  250  * XXX: since that would prevent us from doing our job, and stalling events
  251  * XXX: will break (actually: stall) the BSD disklabel hacks.
  252  */
  253 static int
  254 g_dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
  255 {
  256         struct g_geom *gp;
  257         struct g_consumer *cp;
  258         struct g_provider *pp;
  259         struct g_kerneldump kd;
  260         off_t offset, length, chunk;
  261         int i, error;
  262         u_int u;
  263 
  264         gp = dev->si_drv1;
  265         cp = dev->si_drv2;
  266         pp = cp->provider;
  267 
  268         error = 0;
  269         KASSERT(cp->acr || cp->acw,
  270             ("Consumer with zero access count in g_dev_ioctl"));
  271 
  272         i = IOCPARM_LEN(cmd);
  273         switch (cmd) {
  274         case DIOCGSECTORSIZE:
  275                 *(u_int *)data = cp->provider->sectorsize;
  276                 if (*(u_int *)data == 0)
  277                         error = ENOENT;
  278                 break;
  279         case DIOCGMEDIASIZE:
  280                 *(off_t *)data = cp->provider->mediasize;
  281                 if (*(off_t *)data == 0)
  282                         error = ENOENT;
  283                 break;
  284         case DIOCGFWSECTORS:
  285                 error = g_io_getattr("GEOM::fwsectors", cp, &i, data);
  286                 if (error == 0 && *(u_int *)data == 0)
  287                         error = ENOENT;
  288                 break;
  289         case DIOCGFWHEADS:
  290                 error = g_io_getattr("GEOM::fwheads", cp, &i, data);
  291                 if (error == 0 && *(u_int *)data == 0)
  292                         error = ENOENT;
  293                 break;
  294         case DIOCGFRONTSTUFF:
  295                 error = g_io_getattr("GEOM::frontstuff", cp, &i, data);
  296                 break;
  297         case DIOCSKERNELDUMP:
  298                 u = *((u_int *)data);
  299                 if (!u) {
  300                         set_dumper(NULL);
  301                         error = 0;
  302                         break;
  303                 }
  304                 kd.offset = 0;
  305                 kd.length = OFF_MAX;
  306                 i = sizeof kd;
  307                 error = g_io_getattr("GEOM::kerneldump", cp, &i, &kd);
  308                 if (!error) {
  309                         error = set_dumper(&kd.di);
  310                         if (!error)
  311                                 dev->si_flags |= SI_DUMPDEV;
  312                 }
  313                 break;
  314         case DIOCGFLUSH:
  315                 error = g_io_flush(cp);
  316                 break;
  317         case DIOCGDELETE:
  318                 offset = ((off_t *)data)[0];
  319                 length = ((off_t *)data)[1];
  320                 if ((offset % cp->provider->sectorsize) != 0 ||
  321                     (length % cp->provider->sectorsize) != 0 || length <= 0) {
  322                         printf("%s: offset=%jd length=%jd\n", __func__, offset,
  323                             length);
  324                         error = EINVAL;
  325                         break;
  326                 }
  327                 while (length > 0) { 
  328                         chunk = length;
  329                         if (chunk > 65536 * cp->provider->sectorsize)
  330                                 chunk = 65536 * cp->provider->sectorsize;
  331                         error = g_delete_data(cp, offset, chunk);
  332                         length -= chunk;
  333                         offset += chunk;
  334                         if (error)
  335                                 break;
  336                         /*
  337                          * Since the request size is unbounded, the service
  338                          * time is likewise.  We make this ioctl interruptible
  339                          * by checking for signals for each bio.
  340                          */
  341                         if (SIGPENDING(td))
  342                                 break;
  343                 }
  344                 break;
  345         case DIOCGIDENT:
  346                 error = g_io_getattr("GEOM::ident", cp, &i, data);
  347                 break;
  348         case DIOCGPROVIDERNAME:
  349                 if (pp == NULL)
  350                         return (ENOENT);
  351                 strlcpy(data, pp->name, i);
  352                 break;
  353         case DIOCGSTRIPESIZE:
  354                 *(off_t *)data = cp->provider->stripesize;
  355                 break;
  356         case DIOCGSTRIPEOFFSET:
  357                 *(off_t *)data = cp->provider->stripeoffset;
  358                 break;
  359         default:
  360                 if (cp->provider->geom->ioctl != NULL) {
  361                         error = cp->provider->geom->ioctl(cp->provider, cmd, data, fflag, td);
  362                 } else {
  363                         error = ENOIOCTL;
  364                 }
  365         }
  366 
  367         return (error);
  368 }
  369 
  370 static void
  371 g_dev_done(struct bio *bp2)
  372 {
  373         struct bio *bp;
  374 
  375         bp = bp2->bio_parent;
  376         bp->bio_error = bp2->bio_error;
  377         if (bp->bio_error != 0) {
  378                 g_trace(G_T_BIO, "g_dev_done(%p) had error %d",
  379                     bp2, bp->bio_error);
  380                 bp->bio_flags |= BIO_ERROR;
  381         } else {
  382                 g_trace(G_T_BIO, "g_dev_done(%p/%p) resid %ld completed %jd",
  383                     bp2, bp, bp->bio_resid, (intmax_t)bp2->bio_completed);
  384         }
  385         bp->bio_resid = bp->bio_length - bp2->bio_completed;
  386         bp->bio_completed = bp2->bio_completed;
  387         g_destroy_bio(bp2);
  388         biodone(bp);
  389 }
  390 
  391 static void
  392 g_dev_strategy(struct bio *bp)
  393 {
  394         struct g_consumer *cp;
  395         struct bio *bp2;
  396         struct cdev *dev;
  397 
  398         KASSERT(bp->bio_cmd == BIO_READ ||
  399                 bp->bio_cmd == BIO_WRITE ||
  400                 bp->bio_cmd == BIO_DELETE,
  401                 ("Wrong bio_cmd bio=%p cmd=%d", bp, bp->bio_cmd));
  402         dev = bp->bio_dev;
  403         cp = dev->si_drv2;
  404         KASSERT(cp->acr || cp->acw,
  405             ("Consumer with zero access count in g_dev_strategy"));
  406 #ifdef INVARIANTS
  407         if ((bp->bio_offset % cp->provider->sectorsize) != 0 ||
  408             (bp->bio_bcount % cp->provider->sectorsize) != 0) {
  409                 bp->bio_resid = bp->bio_bcount;
  410                 biofinish(bp, NULL, EINVAL);
  411                 return;
  412         }
  413 #endif
  414         for (;;) {
  415                 /*
  416                  * XXX: This is not an ideal solution, but I belive it to
  417                  * XXX: deadlock safe, all things considered.
  418                  */
  419                 bp2 = g_clone_bio(bp);
  420                 if (bp2 != NULL)
  421                         break;
  422                 pause("gdstrat", hz / 10);
  423         }
  424         KASSERT(bp2 != NULL, ("XXX: ENOMEM in a bad place"));
  425         bp2->bio_done = g_dev_done;
  426         g_trace(G_T_BIO,
  427             "g_dev_strategy(%p/%p) offset %jd length %jd data %p cmd %d",
  428             bp, bp2, (intmax_t)bp->bio_offset, (intmax_t)bp2->bio_length,
  429             bp2->bio_data, bp2->bio_cmd);
  430         g_io_request(bp2, cp);
  431         KASSERT(cp->acr || cp->acw,
  432             ("g_dev_strategy raced with g_dev_close and lost"));
  433 
  434 }
  435 
  436 /*
  437  * g_dev_orphan()
  438  *
  439  * Called from below when the provider orphaned us.
  440  * - Clear any dump settings.
  441  * - Destroy the struct cdev *to prevent any more request from coming in.  The
  442  *   provider is already marked with an error, so anything which comes in
  443  *   in the interrim will be returned immediately.
  444  * - Wait for any outstanding I/O to finish.
  445  * - Set our access counts to zero, whatever they were.
  446  * - Detach and self-destruct.
  447  */
  448 
  449 static void
  450 g_dev_orphan(struct g_consumer *cp)
  451 {
  452         struct g_geom *gp;
  453         struct cdev *dev;
  454 
  455         g_topology_assert();
  456         gp = cp->geom;
  457         dev = gp->softc;
  458         g_trace(G_T_TOPOLOGY, "g_dev_orphan(%p(%s))", cp, gp->name);
  459 
  460         /* Reset any dump-area set on this device */
  461         if (dev->si_flags & SI_DUMPDEV)
  462                 set_dumper(NULL);
  463 
  464         /* Destroy the struct cdev *so we get no more requests */
  465         destroy_dev(dev);
  466 
  467         /* Wait for the cows to come home */
  468         while (cp->nstart != cp->nend)
  469                 pause("gdevorphan", hz / 10);
  470 
  471         if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
  472                 g_access(cp, -cp->acr, -cp->acw, -cp->ace);
  473 
  474         g_detach(cp);
  475         g_destroy_consumer(cp);
  476         g_destroy_geom(gp);
  477 }
  478 
  479 DECLARE_GEOM_CLASS(g_dev_class, g_dev);

Cache object: f46eb7ff2068799d2d4b5941b4a2d440


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