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_io.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/5.3/sys/geom/geom_io.c 136754 2004-10-21 17:49:45Z phk $");
   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 
   45 #include <sys/errno.h>
   46 #include <geom/geom.h>
   47 #include <geom/geom_int.h>
   48 #include <sys/devicestat.h>
   49 
   50 #include <vm/uma.h>
   51 
   52 static struct g_bioq g_bio_run_down;
   53 static struct g_bioq g_bio_run_up;
   54 static struct g_bioq g_bio_run_task;
   55 
   56 static u_int pace;
   57 static uma_zone_t       biozone;
   58 
   59 #include <machine/atomic.h>
   60 
   61 static void
   62 g_bioq_lock(struct g_bioq *bq)
   63 {
   64 
   65         mtx_lock(&bq->bio_queue_lock);
   66 }
   67 
   68 static void
   69 g_bioq_unlock(struct g_bioq *bq)
   70 {
   71 
   72         mtx_unlock(&bq->bio_queue_lock);
   73 }
   74 
   75 #if 0
   76 static void
   77 g_bioq_destroy(struct g_bioq *bq)
   78 {
   79 
   80         mtx_destroy(&bq->bio_queue_lock);
   81 }
   82 #endif
   83 
   84 static void
   85 g_bioq_init(struct g_bioq *bq)
   86 {
   87 
   88         TAILQ_INIT(&bq->bio_queue);
   89         mtx_init(&bq->bio_queue_lock, "bio queue", NULL, MTX_DEF);
   90 }
   91 
   92 static struct bio *
   93 g_bioq_first(struct g_bioq *bq)
   94 {
   95         struct bio *bp;
   96 
   97         bp = TAILQ_FIRST(&bq->bio_queue);
   98         if (bp != NULL) {
   99                 KASSERT((bp->bio_flags & BIO_ONQUEUE),
  100                     ("Bio not on queue bp=%p target %p", bp, bq));
  101                 bp->bio_flags &= ~BIO_ONQUEUE;
  102                 TAILQ_REMOVE(&bq->bio_queue, bp, bio_queue);
  103                 bq->bio_queue_length--;
  104         }
  105         return (bp);
  106 }
  107 
  108 struct bio *
  109 g_new_bio(void)
  110 {
  111         struct bio *bp;
  112 
  113         bp = uma_zalloc(biozone, M_NOWAIT | M_ZERO);
  114         return (bp);
  115 }
  116 
  117 struct bio *
  118 g_alloc_bio(void)
  119 {
  120         struct bio *bp;
  121 
  122         bp = uma_zalloc(biozone, M_WAITOK | M_ZERO);
  123         return (bp);
  124 }
  125 
  126 void
  127 g_destroy_bio(struct bio *bp)
  128 {
  129 
  130         uma_zfree(biozone, bp);
  131 }
  132 
  133 struct bio *
  134 g_clone_bio(struct bio *bp)
  135 {
  136         struct bio *bp2;
  137 
  138         bp2 = uma_zalloc(biozone, M_NOWAIT | M_ZERO);
  139         if (bp2 != NULL) {
  140                 bp2->bio_parent = bp;
  141                 bp2->bio_cmd = bp->bio_cmd;
  142                 bp2->bio_length = bp->bio_length;
  143                 bp2->bio_offset = bp->bio_offset;
  144                 bp2->bio_data = bp->bio_data;
  145                 bp2->bio_attribute = bp->bio_attribute;
  146                 bp->bio_children++;
  147         }
  148         return(bp2);
  149 }
  150 
  151 void
  152 g_io_init()
  153 {
  154 
  155         g_bioq_init(&g_bio_run_down);
  156         g_bioq_init(&g_bio_run_up);
  157         g_bioq_init(&g_bio_run_task);
  158         biozone = uma_zcreate("g_bio", sizeof (struct bio),
  159             NULL, NULL,
  160             NULL, NULL,
  161             0, 0);
  162 }
  163 
  164 int
  165 g_io_getattr(const char *attr, struct g_consumer *cp, int *len, void *ptr)
  166 {
  167         struct bio *bp;
  168         int error;
  169 
  170         g_trace(G_T_BIO, "bio_getattr(%s)", attr);
  171         bp = g_alloc_bio();
  172         bp->bio_cmd = BIO_GETATTR;
  173         bp->bio_done = NULL;
  174         bp->bio_attribute = attr;
  175         bp->bio_length = *len;
  176         bp->bio_data = ptr;
  177         g_io_request(bp, cp);
  178         error = biowait(bp, "ggetattr");
  179         *len = bp->bio_completed;
  180         g_destroy_bio(bp);
  181         return (error);
  182 }
  183 
  184 static int
  185 g_io_check(struct bio *bp)
  186 {
  187         struct g_consumer *cp;
  188         struct g_provider *pp;
  189 
  190         cp = bp->bio_from;
  191         pp = bp->bio_to;
  192 
  193         /* Fail if access counters dont allow the operation */
  194         switch(bp->bio_cmd) {
  195         case BIO_READ:
  196         case BIO_GETATTR:
  197                 if (cp->acr == 0)
  198                         return (EPERM);
  199                 break;
  200         case BIO_WRITE:
  201         case BIO_DELETE:
  202                 if (cp->acw == 0)
  203                         return (EPERM);
  204                 break;
  205         default:
  206                 return (EPERM);
  207         }
  208         /* if provider is marked for error, don't disturb. */
  209         if (pp->error)
  210                 return (pp->error);
  211 
  212         switch(bp->bio_cmd) {
  213         case BIO_READ:
  214         case BIO_WRITE:
  215         case BIO_DELETE:
  216                 /* Zero sectorsize is a probably lack of media */
  217                 if (pp->sectorsize == 0)
  218                         return (ENXIO);
  219                 /* Reject I/O not on sector boundary */
  220                 if (bp->bio_offset % pp->sectorsize)
  221                         return (EINVAL);
  222                 /* Reject I/O not integral sector long */
  223                 if (bp->bio_length % pp->sectorsize)
  224                         return (EINVAL);
  225                 /* Reject requests before or past the end of media. */
  226                 if (bp->bio_offset < 0)
  227                         return (EIO);
  228                 if (bp->bio_offset > pp->mediasize)
  229                         return (EIO);
  230                 break;
  231         default:
  232                 break;
  233         }
  234         return (0);
  235 }
  236 
  237 void
  238 g_io_request(struct bio *bp, struct g_consumer *cp)
  239 {
  240         struct g_provider *pp;
  241 
  242         KASSERT(cp != NULL, ("NULL cp in g_io_request"));
  243         KASSERT(bp != NULL, ("NULL bp in g_io_request"));
  244         KASSERT(bp->bio_data != NULL, ("NULL bp->data in g_io_request"));
  245         pp = cp->provider;
  246         KASSERT(pp != NULL, ("consumer not attached in g_io_request"));
  247 
  248 #ifdef DIAGNOSTIC
  249         if (bp->bio_cmd & (BIO_READ|BIO_WRITE|BIO_DELETE)) {
  250                 KASSERT(bp->bio_offset % cp->provider->sectorsize == 0,
  251                     ("wrong offset %jd for sectorsize %u",
  252                     bp->bio_offset, cp->provider->sectorsize));
  253                 KASSERT(bp->bio_length % cp->provider->sectorsize == 0,
  254                     ("wrong length %jd for sectorsize %u",
  255                     bp->bio_length, cp->provider->sectorsize));
  256         }
  257 #endif /* DIAGNOSTIC */
  258 
  259         g_trace(G_T_BIO, "bio_request(%p) from %p(%s) to %p(%s) cmd %d",
  260             bp, cp, cp->geom->name, pp, pp->name, bp->bio_cmd);
  261 
  262         bp->bio_from = cp;
  263         bp->bio_to = pp;
  264         bp->bio_error = 0;
  265         bp->bio_completed = 0;
  266 
  267         KASSERT(!(bp->bio_flags & BIO_ONQUEUE),
  268             ("Bio already on queue bp=%p", bp));
  269         bp->bio_flags |= BIO_ONQUEUE;
  270 
  271         binuptime(&bp->bio_t0);
  272         if (g_collectstats & 4)
  273                 g_bioq_lock(&g_bio_run_down);
  274         if (g_collectstats & 1)
  275                 devstat_start_transaction(pp->stat, &bp->bio_t0);
  276         if (g_collectstats & 2)
  277                 devstat_start_transaction(cp->stat, &bp->bio_t0);
  278 
  279         if (!(g_collectstats & 4))
  280                 g_bioq_lock(&g_bio_run_down);
  281         pp->nstart++;
  282         cp->nstart++;
  283         TAILQ_INSERT_TAIL(&g_bio_run_down.bio_queue, bp, bio_queue);
  284         g_bio_run_down.bio_queue_length++;
  285         g_bioq_unlock(&g_bio_run_down);
  286 
  287         /* Pass it on down. */
  288         wakeup(&g_wait_down);
  289 }
  290 
  291 void
  292 g_io_deliver(struct bio *bp, int error)
  293 {
  294         struct g_consumer *cp;
  295         struct g_provider *pp;
  296 
  297         KASSERT(bp != NULL, ("NULL bp in g_io_deliver"));
  298         pp = bp->bio_to;
  299         KASSERT(pp != NULL, ("NULL bio_to in g_io_deliver"));
  300         cp = bp->bio_from;
  301         if (cp == NULL) {
  302                 bp->bio_error = error;
  303                 bp->bio_done(bp);
  304                 return;
  305         }
  306         KASSERT(cp != NULL, ("NULL bio_from in g_io_deliver"));
  307         KASSERT(cp->geom != NULL, ("NULL bio_from->geom in g_io_deliver"));
  308         KASSERT(bp->bio_completed >= 0, ("bio_completed can't be less than 0"));
  309         KASSERT(bp->bio_completed <= bp->bio_length,
  310             ("bio_completed can't be greater than bio_length"));
  311 
  312         g_trace(G_T_BIO,
  313 "g_io_deliver(%p) from %p(%s) to %p(%s) cmd %d error %d off %jd len %jd",
  314             bp, cp, cp->geom->name, pp, pp->name, bp->bio_cmd, error,
  315             (intmax_t)bp->bio_offset, (intmax_t)bp->bio_length);
  316 
  317         KASSERT(!(bp->bio_flags & BIO_ONQUEUE),
  318             ("Bio already on queue bp=%p", bp));
  319 
  320         /*
  321          * XXX: next two doesn't belong here
  322          */
  323         bp->bio_bcount = bp->bio_length;
  324         bp->bio_resid = bp->bio_bcount - bp->bio_completed;
  325 
  326         if (g_collectstats & 4)
  327                 g_bioq_lock(&g_bio_run_up);
  328         if (g_collectstats & 1)
  329                 devstat_end_transaction_bio(pp->stat, bp);
  330         if (g_collectstats & 2)
  331                 devstat_end_transaction_bio(cp->stat, bp);
  332         if (!(g_collectstats & 4))
  333                 g_bioq_lock(&g_bio_run_up);
  334         cp->nend++;
  335         pp->nend++;
  336         if (error != ENOMEM) {
  337                 bp->bio_error = error;
  338                 TAILQ_INSERT_TAIL(&g_bio_run_up.bio_queue, bp, bio_queue);
  339                 bp->bio_flags |= BIO_ONQUEUE;
  340                 g_bio_run_up.bio_queue_length++;
  341                 g_bioq_unlock(&g_bio_run_up);
  342                 wakeup(&g_wait_up);
  343                 return;
  344         }
  345         g_bioq_unlock(&g_bio_run_up);
  346 
  347         if (bootverbose)
  348                 printf("ENOMEM %p on %p(%s)\n", bp, pp, pp->name);
  349         bp->bio_children = 0;
  350         bp->bio_inbed = 0;
  351         g_io_request(bp, cp);
  352         pace++;
  353         return;
  354 }
  355 
  356 void
  357 g_io_schedule_down(struct thread *tp __unused)
  358 {
  359         struct bio *bp;
  360         off_t excess;
  361         int error;
  362 #ifdef WITNESS
  363         struct mtx mymutex;
  364  
  365         bzero(&mymutex, sizeof mymutex);
  366         mtx_init(&mymutex, "g_xdown", NULL, MTX_DEF);
  367 #endif
  368 
  369         for(;;) {
  370                 g_bioq_lock(&g_bio_run_down);
  371                 bp = g_bioq_first(&g_bio_run_down);
  372                 if (bp == NULL) {
  373                         msleep(&g_wait_down, &g_bio_run_down.bio_queue_lock,
  374                             PRIBIO | PDROP, "-", hz/10);
  375                         continue;
  376                 }
  377                 g_bioq_unlock(&g_bio_run_down);
  378                 if (pace > 0) {
  379                         msleep(&error, NULL, PRIBIO, "g_down", hz/10);
  380                         pace--;
  381                 }
  382                 error = g_io_check(bp);
  383                 if (error) {
  384                         g_io_deliver(bp, error);
  385                         continue;
  386                 }
  387                 switch (bp->bio_cmd) {
  388                 case BIO_READ:
  389                 case BIO_WRITE:
  390                 case BIO_DELETE:
  391                         /* Truncate requests to the end of providers media. */
  392                         excess = bp->bio_offset + bp->bio_length;
  393                         if (excess > bp->bio_to->mediasize) {
  394                                 excess -= bp->bio_to->mediasize;
  395                                 bp->bio_length -= excess;
  396                         }
  397                         /* Deliver zero length transfers right here. */
  398                         if (bp->bio_length == 0) {
  399                                 g_io_deliver(bp, 0);
  400                                 continue;
  401                         }
  402                         break;
  403                 default:
  404                         break;
  405                 }
  406 #ifdef WITNESS
  407                 mtx_lock(&mymutex);
  408 #endif
  409                 bp->bio_to->geom->start(bp);
  410 #ifdef WITNESS
  411                 mtx_unlock(&mymutex);
  412 #endif
  413         }
  414 }
  415 
  416 void
  417 bio_taskqueue(struct bio *bp, bio_task_t *func, void *arg)
  418 {
  419         bp->bio_task = func;
  420         bp->bio_task_arg = arg;
  421         /*
  422          * The taskqueue is actually just a second queue off the "up"
  423          * queue, so we use the same lock.
  424          */
  425         g_bioq_lock(&g_bio_run_up);
  426         KASSERT(!(bp->bio_flags & BIO_ONQUEUE),
  427             ("Bio already on queue bp=%p target taskq", bp));
  428         bp->bio_flags |= BIO_ONQUEUE;
  429         TAILQ_INSERT_TAIL(&g_bio_run_task.bio_queue, bp, bio_queue);
  430         g_bio_run_task.bio_queue_length++;
  431         wakeup(&g_wait_up);
  432         g_bioq_unlock(&g_bio_run_up);
  433 }
  434 
  435 
  436 void
  437 g_io_schedule_up(struct thread *tp __unused)
  438 {
  439         struct bio *bp;
  440 #ifdef WITNESS
  441         struct mtx mymutex;
  442  
  443         bzero(&mymutex, sizeof mymutex);
  444         mtx_init(&mymutex, "g_xup", NULL, MTX_DEF);
  445 #endif
  446         for(;;) {
  447                 g_bioq_lock(&g_bio_run_up);
  448                 bp = g_bioq_first(&g_bio_run_task);
  449                 if (bp != NULL) {
  450                         g_bioq_unlock(&g_bio_run_up);
  451 #ifdef WITNESS
  452                         mtx_lock(&mymutex);
  453 #endif
  454                         bp->bio_task(bp->bio_task_arg);
  455 #ifdef WITNESS
  456                         mtx_unlock(&mymutex);
  457 #endif
  458                         continue;
  459                 }
  460                 bp = g_bioq_first(&g_bio_run_up);
  461                 if (bp != NULL) {
  462                         g_bioq_unlock(&g_bio_run_up);
  463 #ifdef WITNESS
  464                         mtx_lock(&mymutex);
  465 #endif
  466                         biodone(bp);
  467 #ifdef WITNESS
  468                         mtx_unlock(&mymutex);
  469 #endif
  470                         continue;
  471                 }
  472                 msleep(&g_wait_up, &g_bio_run_up.bio_queue_lock,
  473                     PRIBIO | PDROP, "-", hz/10);
  474         }
  475 }
  476 
  477 void *
  478 g_read_data(struct g_consumer *cp, off_t offset, off_t length, int *error)
  479 {
  480         struct bio *bp;
  481         void *ptr;
  482         int errorc;
  483 
  484         KASSERT(length > 0 && length >= cp->provider->sectorsize &&
  485             length <= MAXPHYS, ("g_read_data(): invalid length %jd",
  486             (intmax_t)length));
  487 
  488         bp = g_alloc_bio();
  489         bp->bio_cmd = BIO_READ;
  490         bp->bio_done = NULL;
  491         bp->bio_offset = offset;
  492         bp->bio_length = length;
  493         ptr = g_malloc(length, M_WAITOK);
  494         bp->bio_data = ptr;
  495         g_io_request(bp, cp);
  496         errorc = biowait(bp, "gread");
  497         if (error != NULL)
  498                 *error = errorc;
  499         g_destroy_bio(bp);
  500         if (errorc) {
  501                 g_free(ptr);
  502                 ptr = NULL;
  503         }
  504         return (ptr);
  505 }
  506 
  507 int
  508 g_write_data(struct g_consumer *cp, off_t offset, void *ptr, off_t length)
  509 {
  510         struct bio *bp;
  511         int error;
  512 
  513         KASSERT(length > 0 && length >= cp->provider->sectorsize &&
  514             length <= MAXPHYS, ("g_write_data(): invalid length %jd",
  515             (intmax_t)length));
  516 
  517         bp = g_alloc_bio();
  518         bp->bio_cmd = BIO_WRITE;
  519         bp->bio_done = NULL;
  520         bp->bio_offset = offset;
  521         bp->bio_length = length;
  522         bp->bio_data = ptr;
  523         g_io_request(bp, cp);
  524         error = biowait(bp, "gwrite");
  525         g_destroy_bio(bp);
  526         return (error);
  527 }
  528 
  529 void
  530 g_print_bio(struct bio *bp)
  531 {
  532         const char *pname, *cmd = NULL;
  533 
  534         if (bp->bio_to != NULL)
  535                 pname = bp->bio_to->name;
  536         else
  537                 pname = "[unknown]";
  538 
  539         switch (bp->bio_cmd) {
  540         case BIO_GETATTR:
  541                 cmd = "GETATTR";
  542                 printf("%s[%s(attr=%s)]", pname, cmd, bp->bio_attribute);
  543                 return;
  544         case BIO_READ:
  545                 cmd = "READ";
  546         case BIO_WRITE:
  547                 if (cmd == NULL)
  548                         cmd = "WRITE";
  549         case BIO_DELETE:
  550                 if (cmd == NULL)
  551                         cmd = "DELETE";
  552                 printf("%s[%s(offset=%jd, length=%jd)]", pname, cmd,
  553                     (intmax_t)bp->bio_offset, (intmax_t)bp->bio_length);
  554                 return;
  555         default:
  556                 cmd = "UNKNOWN";
  557                 printf("%s[%s()]", pname, cmd);
  558                 return;
  559         }
  560         /* NOTREACHED */
  561 }

Cache object: f85de7a58ed7060dc1352272d2cdb85c


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