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_disk.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$");
   38 
   39 #include "opt_geom.h"
   40 
   41 #include <sys/param.h>
   42 #include <sys/systm.h>
   43 #include <sys/kernel.h>
   44 #include <sys/sysctl.h>
   45 #include <sys/bio.h>
   46 #include <sys/bus.h>
   47 #include <sys/ctype.h>
   48 #include <sys/fcntl.h>
   49 #include <sys/malloc.h>
   50 #include <sys/sbuf.h>
   51 #include <sys/devicestat.h>
   52 #include <machine/md_var.h>
   53 
   54 #include <sys/lock.h>
   55 #include <sys/mutex.h>
   56 #include <geom/geom.h>
   57 #include <geom/geom_disk.h>
   58 #include <geom/geom_int.h>
   59 
   60 #include <dev/led/led.h>
   61 
   62 #include <machine/bus.h>
   63 
   64 struct g_disk_softc {
   65         struct mtx               done_mtx;
   66         struct disk             *dp;
   67         struct sysctl_ctx_list  sysctl_ctx;
   68         struct sysctl_oid       *sysctl_tree;
   69         char                    led[64];
   70         uint32_t                state;
   71         struct mtx               start_mtx;
   72 };
   73 
   74 static g_access_t g_disk_access;
   75 static g_start_t g_disk_start;
   76 static g_ioctl_t g_disk_ioctl;
   77 static g_dumpconf_t g_disk_dumpconf;
   78 static g_provgone_t g_disk_providergone;
   79 
   80 static struct g_class g_disk_class = {
   81         .name = G_DISK_CLASS_NAME,
   82         .version = G_VERSION,
   83         .start = g_disk_start,
   84         .access = g_disk_access,
   85         .ioctl = g_disk_ioctl,
   86         .providergone = g_disk_providergone,
   87         .dumpconf = g_disk_dumpconf,
   88 };
   89 
   90 SYSCTL_DECL(_kern_geom);
   91 static SYSCTL_NODE(_kern_geom, OID_AUTO, disk, CTLFLAG_RW, 0,
   92     "GEOM_DISK stuff");
   93 
   94 DECLARE_GEOM_CLASS(g_disk_class, g_disk);
   95 
   96 static void __inline
   97 g_disk_lock_giant(struct disk *dp)
   98 {
   99 
  100         if (dp->d_flags & DISKFLAG_NEEDSGIANT)
  101                 mtx_lock(&Giant);
  102 }
  103 
  104 static void __inline
  105 g_disk_unlock_giant(struct disk *dp)
  106 {
  107 
  108         if (dp->d_flags & DISKFLAG_NEEDSGIANT)
  109                 mtx_unlock(&Giant);
  110 }
  111 
  112 static int
  113 g_disk_access(struct g_provider *pp, int r, int w, int e)
  114 {
  115         struct disk *dp;
  116         struct g_disk_softc *sc;
  117         int error;
  118 
  119         g_trace(G_T_ACCESS, "g_disk_access(%s, %d, %d, %d)",
  120             pp->name, r, w, e);
  121         g_topology_assert();
  122         sc = pp->private;
  123         if (sc == NULL || (dp = sc->dp) == NULL || dp->d_destroyed) {
  124                 /*
  125                  * Allow decreasing access count even if disk is not
  126                  * avaliable anymore.
  127                  */
  128                 if (r <= 0 && w <= 0 && e <= 0)
  129                         return (0);
  130                 return (ENXIO);
  131         }
  132         r += pp->acr;
  133         w += pp->acw;
  134         e += pp->ace;
  135         error = 0;
  136         if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) {
  137                 /*
  138                  * It would be better to defer this decision to d_open if
  139                  * it was able to take flags.
  140                  */
  141                 if (w > 0 && (dp->d_flags & DISKFLAG_WRITE_PROTECT) != 0)
  142                         error = EROFS;
  143                 if (error == 0 && dp->d_open != NULL) {
  144                         g_disk_lock_giant(dp);
  145                         error = dp->d_open(dp);
  146                         g_disk_unlock_giant(dp);
  147                 }
  148                 if (bootverbose && error != 0)
  149                         printf("Opened disk %s -> %d\n", pp->name, error);
  150                 if (error != 0)
  151                         return (error);
  152                 pp->mediasize = dp->d_mediasize;
  153                 pp->sectorsize = dp->d_sectorsize;
  154                 if (dp->d_maxsize == 0) {
  155                         printf("WARNING: Disk drive %s%d has no d_maxsize\n",
  156                             dp->d_name, dp->d_unit);
  157                         dp->d_maxsize = DFLTPHYS;
  158                 }
  159                 if (dp->d_delmaxsize == 0) {
  160                         if (bootverbose && dp->d_flags & DISKFLAG_CANDELETE) {
  161                                 printf("WARNING: Disk drive %s%d has no "
  162                                     "d_delmaxsize\n", dp->d_name, dp->d_unit);
  163                         }
  164                         dp->d_delmaxsize = dp->d_maxsize;
  165                 }
  166                 pp->stripeoffset = dp->d_stripeoffset;
  167                 pp->stripesize = dp->d_stripesize;
  168                 dp->d_flags |= DISKFLAG_OPEN;
  169         } else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) {
  170                 if (dp->d_close != NULL) {
  171                         g_disk_lock_giant(dp);
  172                         error = dp->d_close(dp);
  173                         if (error != 0)
  174                                 printf("Closed disk %s -> %d\n",
  175                                     pp->name, error);
  176                         g_disk_unlock_giant(dp);
  177                 }
  178                 sc->state = G_STATE_ACTIVE;
  179                 if (sc->led[0] != 0)
  180                         led_set(sc->led, "");
  181                 dp->d_flags &= ~DISKFLAG_OPEN;
  182         }
  183         return (error);
  184 }
  185 
  186 static void
  187 g_disk_kerneldump(struct bio *bp, struct disk *dp)
  188 {
  189         struct g_kerneldump *gkd;
  190         struct g_geom *gp;
  191 
  192         gkd = (struct g_kerneldump*)bp->bio_data;
  193         gp = bp->bio_to->geom;
  194         g_trace(G_T_TOPOLOGY, "g_disk_kerneldump(%s, %jd, %jd)",
  195                 gp->name, (intmax_t)gkd->offset, (intmax_t)gkd->length);
  196         if (dp->d_dump == NULL) {
  197                 g_io_deliver(bp, ENODEV);
  198                 return;
  199         }
  200         gkd->di.dumper = dp->d_dump;
  201         gkd->di.priv = dp;
  202         gkd->di.blocksize = dp->d_sectorsize;
  203         gkd->di.maxiosize = dp->d_maxsize;
  204         gkd->di.mediaoffset = gkd->offset;
  205         if ((gkd->offset + gkd->length) > dp->d_mediasize)
  206                 gkd->length = dp->d_mediasize - gkd->offset;
  207         gkd->di.mediasize = gkd->length;
  208         g_io_deliver(bp, 0);
  209 }
  210 
  211 static void
  212 g_disk_setstate(struct bio *bp, struct g_disk_softc *sc)
  213 {
  214         const char *cmd;
  215 
  216         memcpy(&sc->state, bp->bio_data, sizeof(sc->state));
  217         if (sc->led[0] != 0) {
  218                 switch (sc->state) {
  219                 case G_STATE_FAILED:
  220                         cmd = "1";
  221                         break;
  222                 case G_STATE_REBUILD:
  223                         cmd = "f5";
  224                         break;
  225                 case G_STATE_RESYNC:
  226                         cmd = "f1";
  227                         break;
  228                 default:
  229                         cmd = "";
  230                         break;
  231                 }
  232                 led_set(sc->led, cmd);
  233         }
  234         g_io_deliver(bp, 0);
  235 }
  236 
  237 static void
  238 g_disk_done(struct bio *bp)
  239 {
  240         struct bintime now;
  241         struct bio *bp2;
  242         struct g_disk_softc *sc;
  243 
  244         /* See "notes" for why we need a mutex here */
  245         /* XXX: will witness accept a mix of Giant/unGiant drivers here ? */
  246         bp2 = bp->bio_parent;
  247         sc = bp2->bio_to->private;
  248         bp->bio_completed = bp->bio_length - bp->bio_resid;
  249         binuptime(&now);
  250         mtx_lock(&sc->done_mtx);
  251         if (bp2->bio_error == 0)
  252                 bp2->bio_error = bp->bio_error;
  253         bp2->bio_completed += bp->bio_completed;
  254         if ((bp->bio_cmd & (BIO_READ|BIO_WRITE|BIO_DELETE|BIO_FLUSH)) != 0)
  255                 devstat_end_transaction_bio_bt(sc->dp->d_devstat, bp, &now);
  256         bp2->bio_inbed++;
  257         if (bp2->bio_children == bp2->bio_inbed) {
  258                 mtx_unlock(&sc->done_mtx);
  259                 bp2->bio_resid = bp2->bio_bcount - bp2->bio_completed;
  260                 g_io_deliver(bp2, bp2->bio_error);
  261         } else
  262                 mtx_unlock(&sc->done_mtx);
  263         g_destroy_bio(bp);
  264 }
  265 
  266 static int
  267 g_disk_ioctl(struct g_provider *pp, u_long cmd, void * data, int fflag, struct thread *td)
  268 {
  269         struct disk *dp;
  270         struct g_disk_softc *sc;
  271         int error;
  272 
  273         sc = pp->private;
  274         dp = sc->dp;
  275 
  276         if (dp->d_ioctl == NULL)
  277                 return (ENOIOCTL);
  278         g_disk_lock_giant(dp);
  279         error = dp->d_ioctl(dp, cmd, data, fflag, td);
  280         g_disk_unlock_giant(dp);
  281         return (error);
  282 }
  283 
  284 static off_t
  285 g_disk_maxsize(struct disk *dp, struct bio *bp)
  286 {
  287         if (bp->bio_cmd == BIO_DELETE)
  288                 return (dp->d_delmaxsize);
  289         return (dp->d_maxsize);
  290 }
  291 
  292 static int
  293 g_disk_maxsegs(struct disk *dp, struct bio *bp)
  294 {
  295         return ((g_disk_maxsize(dp, bp) / PAGE_SIZE) + 1);
  296 }
  297 
  298 static void
  299 g_disk_advance(struct disk *dp, struct bio *bp, off_t off)
  300 {
  301 
  302         bp->bio_offset += off;
  303         bp->bio_length -= off;
  304 
  305         if ((bp->bio_flags & BIO_VLIST) != 0) {
  306                 bus_dma_segment_t *seg, *end;
  307 
  308                 seg = (bus_dma_segment_t *)bp->bio_data;
  309                 end = (bus_dma_segment_t *)bp->bio_data + bp->bio_ma_n;
  310                 off += bp->bio_ma_offset;
  311                 while (off >= seg->ds_len) {
  312                         KASSERT((seg != end),
  313                             ("vlist request runs off the end"));
  314                         off -= seg->ds_len;
  315                         seg++;
  316                 }
  317                 bp->bio_ma_offset = off;
  318                 bp->bio_ma_n = end - seg;
  319                 bp->bio_data = (void *)seg;
  320         } else if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
  321                 bp->bio_ma += off / PAGE_SIZE;
  322                 bp->bio_ma_offset += off;
  323                 bp->bio_ma_offset %= PAGE_SIZE;
  324                 bp->bio_ma_n -= off / PAGE_SIZE;
  325         } else {
  326                 bp->bio_data += off;
  327         }
  328 }
  329 
  330 static void
  331 g_disk_seg_limit(bus_dma_segment_t *seg, off_t *poffset,
  332     off_t *plength, int *ppages)
  333 {
  334         uintptr_t seg_page_base;
  335         uintptr_t seg_page_end;
  336         off_t offset;
  337         off_t length;
  338         int seg_pages;
  339 
  340         offset = *poffset;
  341         length = *plength;
  342 
  343         if (length > seg->ds_len - offset)
  344                 length = seg->ds_len - offset;
  345 
  346         seg_page_base = trunc_page(seg->ds_addr + offset);
  347         seg_page_end  = round_page(seg->ds_addr + offset + length);
  348         seg_pages = (seg_page_end - seg_page_base) >> PAGE_SHIFT;
  349 
  350         if (seg_pages > *ppages) {
  351                 seg_pages = *ppages;
  352                 length = (seg_page_base + (seg_pages << PAGE_SHIFT)) -
  353                     (seg->ds_addr + offset);
  354         }
  355 
  356         *poffset = 0;
  357         *plength -= length;
  358         *ppages -= seg_pages;
  359 }
  360 
  361 static off_t
  362 g_disk_vlist_limit(struct disk *dp, struct bio *bp, bus_dma_segment_t **pendseg)
  363 {
  364         bus_dma_segment_t *seg, *end;
  365         off_t residual;
  366         off_t offset;
  367         int pages;
  368 
  369         seg = (bus_dma_segment_t *)bp->bio_data;
  370         end = (bus_dma_segment_t *)bp->bio_data + bp->bio_ma_n;
  371         residual = bp->bio_length;
  372         offset = bp->bio_ma_offset;
  373         pages = g_disk_maxsegs(dp, bp);
  374         while (residual != 0 && pages != 0) {
  375                 KASSERT((seg != end),
  376                     ("vlist limit runs off the end"));
  377                 g_disk_seg_limit(seg, &offset, &residual, &pages);
  378                 seg++;
  379         }
  380         if (pendseg != NULL)
  381                 *pendseg = seg;
  382         return (residual);
  383 }
  384 
  385 static bool
  386 g_disk_limit(struct disk *dp, struct bio *bp)
  387 {
  388         bool limited = false;
  389         off_t maxsz;
  390 
  391         maxsz = g_disk_maxsize(dp, bp);
  392 
  393         /*
  394          * XXX: If we have a stripesize we should really use it here.
  395          *      Care should be taken in the delete case if this is done
  396          *      as deletes can be very sensitive to size given how they
  397          *      are processed.
  398          */
  399         if (bp->bio_length > maxsz) {
  400                 bp->bio_length = maxsz;
  401                 limited = true;
  402         }
  403 
  404         if ((bp->bio_flags & BIO_VLIST) != 0) {
  405                 bus_dma_segment_t *firstseg, *endseg;
  406                 off_t residual;
  407 
  408                 firstseg = (bus_dma_segment_t*)bp->bio_data;
  409                 residual = g_disk_vlist_limit(dp, bp, &endseg);
  410                 if (residual != 0) {
  411                         bp->bio_ma_n = endseg - firstseg;
  412                         bp->bio_length -= residual;
  413                         limited = true;
  414                 }
  415         } else if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
  416                 bp->bio_ma_n =
  417                     howmany(bp->bio_ma_offset + bp->bio_length, PAGE_SIZE);
  418         }
  419 
  420         return (limited);
  421 }
  422 
  423 static void
  424 g_disk_start(struct bio *bp)
  425 {
  426         struct bio *bp2, *bp3;
  427         struct disk *dp;
  428         struct g_disk_softc *sc;
  429         int error;
  430         off_t off;
  431 
  432         sc = bp->bio_to->private;
  433         if (sc == NULL || (dp = sc->dp) == NULL || dp->d_destroyed) {
  434                 g_io_deliver(bp, ENXIO);
  435                 return;
  436         }
  437         error = EJUSTRETURN;
  438         switch(bp->bio_cmd) {
  439         case BIO_DELETE:
  440                 if (!(dp->d_flags & DISKFLAG_CANDELETE)) {
  441                         error = EOPNOTSUPP;
  442                         break;
  443                 }
  444                 /* fall-through */
  445         case BIO_READ:
  446         case BIO_WRITE:
  447                 KASSERT((dp->d_flags & DISKFLAG_UNMAPPED_BIO) != 0 ||
  448                     (bp->bio_flags & BIO_UNMAPPED) == 0,
  449                     ("unmapped bio not supported by disk %s", dp->d_name));
  450                 off = 0;
  451                 bp3 = NULL;
  452                 bp2 = g_clone_bio(bp);
  453                 if (bp2 == NULL) {
  454                         error = ENOMEM;
  455                         break;
  456                 }
  457                 for (;;) {
  458                         if (g_disk_limit(dp, bp2)) {
  459                                 off += bp2->bio_length;
  460 
  461                                 /*
  462                                  * To avoid a race, we need to grab the next bio
  463                                  * before we schedule this one.  See "notes".
  464                                  */
  465                                 bp3 = g_clone_bio(bp);
  466                                 if (bp3 == NULL)
  467                                         bp->bio_error = ENOMEM;
  468                         }
  469                         bp2->bio_done = g_disk_done;
  470                         bp2->bio_pblkno = bp2->bio_offset / dp->d_sectorsize;
  471                         bp2->bio_bcount = bp2->bio_length;
  472                         bp2->bio_disk = dp;
  473                         mtx_lock(&sc->start_mtx); 
  474                         devstat_start_transaction_bio(dp->d_devstat, bp2);
  475                         mtx_unlock(&sc->start_mtx); 
  476                         g_disk_lock_giant(dp);
  477                         dp->d_strategy(bp2);
  478                         g_disk_unlock_giant(dp);
  479 
  480                         if (bp3 == NULL)
  481                                 break;
  482 
  483                         bp2 = bp3;
  484                         bp3 = NULL;
  485                         g_disk_advance(dp, bp2, off);
  486                 }
  487                 break;
  488         case BIO_GETATTR:
  489                 /* Give the driver a chance to override */
  490                 if (dp->d_getattr != NULL) {
  491                         if (bp->bio_disk == NULL)
  492                                 bp->bio_disk = dp;
  493                         error = dp->d_getattr(bp);
  494                         if (error != -1)
  495                                 break;
  496                         error = EJUSTRETURN;
  497                 }
  498                 if (g_handleattr_int(bp, "GEOM::candelete",
  499                     (dp->d_flags & DISKFLAG_CANDELETE) != 0))
  500                         break;
  501                 else if (g_handleattr_int(bp, "GEOM::fwsectors",
  502                     dp->d_fwsectors))
  503                         break;
  504                 else if (g_handleattr_int(bp, "GEOM::fwheads", dp->d_fwheads))
  505                         break;
  506                 else if (g_handleattr_off_t(bp, "GEOM::frontstuff", 0))
  507                         break;
  508                 else if (g_handleattr_str(bp, "GEOM::ident", dp->d_ident))
  509                         break;
  510                 else if (g_handleattr_str(bp, "GEOM::descr", dp->d_descr))
  511                         break;
  512                 else if (g_handleattr_uint16_t(bp, "GEOM::hba_vendor",
  513                     dp->d_hba_vendor))
  514                         break;
  515                 else if (g_handleattr_uint16_t(bp, "GEOM::hba_device",
  516                     dp->d_hba_device))
  517                         break;
  518                 else if (g_handleattr_uint16_t(bp, "GEOM::hba_subvendor",
  519                     dp->d_hba_subvendor))
  520                         break;
  521                 else if (g_handleattr_uint16_t(bp, "GEOM::hba_subdevice",
  522                     dp->d_hba_subdevice))
  523                         break;
  524                 else if (!strcmp(bp->bio_attribute, "GEOM::kerneldump"))
  525                         g_disk_kerneldump(bp, dp);
  526                 else if (!strcmp(bp->bio_attribute, "GEOM::setstate"))
  527                         g_disk_setstate(bp, sc);
  528                 else if (!strcmp(bp->bio_attribute, "GEOM::rotation_rate")) {
  529                         uint64_t v;
  530 
  531                         if ((dp->d_flags & DISKFLAG_LACKS_ROTRATE) == 0)
  532                                 v = dp->d_rotation_rate;
  533                         else
  534                                 v = 0; /* rate unknown */
  535                         g_handleattr_uint16_t(bp, "GEOM::rotation_rate", v);
  536                         break;
  537                 } else 
  538                         error = ENOIOCTL;
  539                 break;
  540         case BIO_FLUSH:
  541                 g_trace(G_T_BIO, "g_disk_flushcache(%s)",
  542                     bp->bio_to->name);
  543                 if (!(dp->d_flags & DISKFLAG_CANFLUSHCACHE)) {
  544                         error = EOPNOTSUPP;
  545                         break;
  546                 }
  547                 bp2 = g_clone_bio(bp);
  548                 if (bp2 == NULL) {
  549                         g_io_deliver(bp, ENOMEM);
  550                         return;
  551                 }
  552                 bp2->bio_done = g_disk_done;
  553                 bp2->bio_disk = dp;
  554                 mtx_lock(&sc->start_mtx);
  555                 devstat_start_transaction_bio(dp->d_devstat, bp2);
  556                 mtx_unlock(&sc->start_mtx);
  557                 g_disk_lock_giant(dp);
  558                 dp->d_strategy(bp2);
  559                 g_disk_unlock_giant(dp);
  560                 break;
  561         default:
  562                 error = EOPNOTSUPP;
  563                 break;
  564         }
  565         if (error != EJUSTRETURN)
  566                 g_io_deliver(bp, error);
  567         return;
  568 }
  569 
  570 static void
  571 g_disk_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp)
  572 {
  573         struct bio *bp;
  574         struct disk *dp;
  575         struct g_disk_softc *sc;
  576         char *buf;
  577         int res = 0;
  578 
  579         sc = gp->softc;
  580         if (sc == NULL || (dp = sc->dp) == NULL)
  581                 return;
  582         if (indent == NULL) {
  583                 sbuf_printf(sb, " hd %u", dp->d_fwheads);
  584                 sbuf_printf(sb, " sc %u", dp->d_fwsectors);
  585                 return;
  586         }
  587         if (pp != NULL) {
  588                 sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n",
  589                     indent, dp->d_fwheads);
  590                 sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n",
  591                     indent, dp->d_fwsectors);
  592 
  593                 /*
  594                  * "rotationrate" is a little complicated, because the value
  595                  * returned by the drive might not be the RPM; 0 and 1 are
  596                  * special cases, and there's also a valid range.
  597                  */
  598                 sbuf_printf(sb, "%s<rotationrate>", indent);
  599                 if (dp->d_rotation_rate == DISK_RR_UNKNOWN) /* Old drives */
  600                         sbuf_printf(sb, "unknown");     /* don't report RPM. */
  601                 else if (dp->d_rotation_rate == DISK_RR_NON_ROTATING)
  602                         sbuf_printf(sb, "");
  603                 else if ((dp->d_rotation_rate >= DISK_RR_MIN) &&
  604                     (dp->d_rotation_rate <= DISK_RR_MAX))
  605                         sbuf_printf(sb, "%u", dp->d_rotation_rate);
  606                 else
  607                         sbuf_printf(sb, "invalid");
  608                 sbuf_printf(sb, "</rotationrate>\n");
  609                 if (dp->d_getattr != NULL) {
  610                         buf = g_malloc(DISK_IDENT_SIZE, M_WAITOK);
  611                         bp = g_alloc_bio();
  612                         bp->bio_disk = dp;
  613                         bp->bio_attribute = "GEOM::ident";
  614                         bp->bio_length = DISK_IDENT_SIZE;
  615                         bp->bio_data = buf;
  616                         res = dp->d_getattr(bp);
  617                         sbuf_printf(sb, "%s<ident>", indent);
  618                         g_conf_printf_escaped(sb, "%s",
  619                             res == 0 ? buf: dp->d_ident);
  620                         sbuf_printf(sb, "</ident>\n");
  621                         bp->bio_attribute = "GEOM::lunid";
  622                         bp->bio_length = DISK_IDENT_SIZE;
  623                         bp->bio_data = buf;
  624                         if (dp->d_getattr(bp) == 0) {
  625                                 sbuf_printf(sb, "%s<lunid>", indent);
  626                                 g_conf_printf_escaped(sb, "%s", buf);
  627                                 sbuf_printf(sb, "</lunid>\n");
  628                         }
  629                         bp->bio_attribute = "GEOM::lunname";
  630                         bp->bio_length = DISK_IDENT_SIZE;
  631                         bp->bio_data = buf;
  632                         if (dp->d_getattr(bp) == 0) {
  633                                 sbuf_printf(sb, "%s<lunname>", indent);
  634                                 g_conf_printf_escaped(sb, "%s", buf);
  635                                 sbuf_printf(sb, "</lunname>\n");
  636                         }
  637                         g_destroy_bio(bp);
  638                         g_free(buf);
  639                 } else {
  640                         sbuf_printf(sb, "%s<ident>", indent);
  641                         g_conf_printf_escaped(sb, "%s", dp->d_ident);
  642                         sbuf_printf(sb, "</ident>\n");
  643                 }
  644                 sbuf_printf(sb, "%s<descr>", indent);
  645                 g_conf_printf_escaped(sb, "%s", dp->d_descr);
  646                 sbuf_printf(sb, "</descr>\n");
  647         }
  648 }
  649 
  650 static void
  651 g_disk_resize(void *ptr, int flag)
  652 {
  653         struct disk *dp;
  654         struct g_geom *gp;
  655         struct g_provider *pp;
  656 
  657         if (flag == EV_CANCEL)
  658                 return;
  659         g_topology_assert();
  660 
  661         dp = ptr;
  662         gp = dp->d_geom;
  663 
  664         if (dp->d_destroyed || gp == NULL)
  665                 return;
  666 
  667         LIST_FOREACH(pp, &gp->provider, provider) {
  668                 if (pp->sectorsize != 0 &&
  669                     pp->sectorsize != dp->d_sectorsize)
  670                         g_wither_provider(pp, ENXIO);
  671                 else
  672                         g_resize_provider(pp, dp->d_mediasize);
  673         }
  674 }
  675 
  676 static void
  677 g_disk_create(void *arg, int flag)
  678 {
  679         struct g_geom *gp;
  680         struct g_provider *pp;
  681         struct disk *dp;
  682         struct g_disk_softc *sc;
  683         char tmpstr[80];
  684 
  685         if (flag == EV_CANCEL)
  686                 return;
  687         g_topology_assert();
  688         dp = arg;
  689         sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
  690         mtx_init(&sc->start_mtx, "g_disk_start", NULL, MTX_DEF);
  691         mtx_init(&sc->done_mtx, "g_disk_done", NULL, MTX_DEF);
  692         sc->dp = dp;
  693         gp = g_new_geomf(&g_disk_class, "%s%d", dp->d_name, dp->d_unit);
  694         gp->softc = sc;
  695         pp = g_new_providerf(gp, "%s", gp->name);
  696         devstat_remove_entry(pp->stat);
  697         pp->stat = NULL;
  698         dp->d_devstat->id = pp;
  699         pp->mediasize = dp->d_mediasize;
  700         pp->sectorsize = dp->d_sectorsize;
  701         pp->stripeoffset = dp->d_stripeoffset;
  702         pp->stripesize = dp->d_stripesize;
  703         if ((dp->d_flags & DISKFLAG_UNMAPPED_BIO) != 0)
  704                 pp->flags |= G_PF_ACCEPT_UNMAPPED;
  705         if ((dp->d_flags & DISKFLAG_DIRECT_COMPLETION) != 0)
  706                 pp->flags |= G_PF_DIRECT_SEND;
  707         pp->flags |= G_PF_DIRECT_RECEIVE;
  708         if (bootverbose)
  709                 printf("GEOM: new disk %s\n", gp->name);
  710         sysctl_ctx_init(&sc->sysctl_ctx);
  711         snprintf(tmpstr, sizeof(tmpstr), "GEOM disk %s", gp->name);
  712         sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
  713                 SYSCTL_STATIC_CHILDREN(_kern_geom_disk), OID_AUTO, gp->name,
  714                 CTLFLAG_RD, 0, tmpstr);
  715         if (sc->sysctl_tree != NULL) {
  716                 snprintf(tmpstr, sizeof(tmpstr),
  717                     "kern.geom.disk.%s.led", gp->name);
  718                 TUNABLE_STR_FETCH(tmpstr, sc->led, sizeof(sc->led));
  719                 SYSCTL_ADD_STRING(&sc->sysctl_ctx,
  720                     SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO, "led",
  721                     CTLFLAG_RW | CTLFLAG_TUN, sc->led, sizeof(sc->led),
  722                     "LED name");
  723         }
  724         pp->private = sc;
  725         dp->d_geom = gp;
  726         g_error_provider(pp, 0);
  727 }
  728 
  729 /*
  730  * We get this callback after all of the consumers have gone away, and just
  731  * before the provider is freed.  If the disk driver provided a d_gone
  732  * callback, let them know that it is okay to free resources -- they won't
  733  * be getting any more accesses from GEOM.
  734  */
  735 static void
  736 g_disk_providergone(struct g_provider *pp)
  737 {
  738         struct disk *dp;
  739         struct g_disk_softc *sc;
  740 
  741         sc = (struct g_disk_softc *)pp->private;
  742         dp = sc->dp;
  743         if (dp != NULL && dp->d_gone != NULL)
  744                 dp->d_gone(dp);
  745         if (sc->sysctl_tree != NULL) {
  746                 sysctl_ctx_free(&sc->sysctl_ctx);
  747                 sc->sysctl_tree = NULL;
  748         }
  749         if (sc->led[0] != 0) {
  750                 led_set(sc->led, "");
  751                 sc->led[0] = 0;
  752         }
  753         pp->private = NULL;
  754         pp->geom->softc = NULL;
  755         mtx_destroy(&sc->done_mtx);
  756         mtx_destroy(&sc->start_mtx);
  757         g_free(sc);
  758 }
  759 
  760 static void
  761 g_disk_destroy(void *ptr, int flag)
  762 {
  763         struct disk *dp;
  764         struct g_geom *gp;
  765         struct g_disk_softc *sc;
  766 
  767         g_topology_assert();
  768         dp = ptr;
  769         gp = dp->d_geom;
  770         if (gp != NULL) {
  771                 sc = gp->softc;
  772                 if (sc != NULL)
  773                         sc->dp = NULL;
  774                 dp->d_geom = NULL;
  775                 g_wither_geom(gp, ENXIO);
  776         }
  777         g_free(dp);
  778 }
  779 
  780 /*
  781  * We only allow printable characters in disk ident,
  782  * the rest is converted to 'x<HH>'.
  783  */
  784 static void
  785 g_disk_ident_adjust(char *ident, size_t size)
  786 {
  787         char *p, tmp[4], newid[DISK_IDENT_SIZE];
  788 
  789         newid[0] = '\0';
  790         for (p = ident; *p != '\0'; p++) {
  791                 if (isprint(*p)) {
  792                         tmp[0] = *p;
  793                         tmp[1] = '\0';
  794                 } else {
  795                         snprintf(tmp, sizeof(tmp), "x%02hhx",
  796                             *(unsigned char *)p);
  797                 }
  798                 if (strlcat(newid, tmp, sizeof(newid)) >= sizeof(newid))
  799                         break;
  800         }
  801         bzero(ident, size);
  802         strlcpy(ident, newid, size);
  803 }
  804 
  805 struct disk *
  806 disk_alloc(void)
  807 {
  808 
  809         return (g_malloc(sizeof(struct disk), M_WAITOK | M_ZERO));
  810 }
  811 
  812 void
  813 disk_create(struct disk *dp, int version)
  814 {
  815 
  816         if (version != DISK_VERSION) {
  817                 printf("WARNING: Attempt to add disk %s%d %s",
  818                     dp->d_name, dp->d_unit,
  819                     " using incompatible ABI version of disk(9)\n");
  820                 printf("WARNING: Ignoring disk %s%d\n",
  821                     dp->d_name, dp->d_unit);
  822                 return;
  823         }
  824         if (version < DISK_VERSION_04)
  825                 dp->d_flags |= DISKFLAG_LACKS_ROTRATE;
  826         KASSERT(dp->d_strategy != NULL, ("disk_create need d_strategy"));
  827         KASSERT(dp->d_name != NULL, ("disk_create need d_name"));
  828         KASSERT(*dp->d_name != 0, ("disk_create need d_name"));
  829         KASSERT(strlen(dp->d_name) < SPECNAMELEN - 4, ("disk name too long"));
  830         if (dp->d_devstat == NULL)
  831                 dp->d_devstat = devstat_new_entry(dp->d_name, dp->d_unit,
  832                     dp->d_sectorsize, DEVSTAT_ALL_SUPPORTED,
  833                     DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
  834         dp->d_geom = NULL;
  835         g_disk_ident_adjust(dp->d_ident, sizeof(dp->d_ident));
  836         g_post_event(g_disk_create, dp, M_WAITOK, dp, NULL);
  837 }
  838 
  839 void
  840 disk_destroy(struct disk *dp)
  841 {
  842 
  843         g_cancel_event(dp);
  844         dp->d_destroyed = 1;
  845         if (dp->d_devstat != NULL)
  846                 devstat_remove_entry(dp->d_devstat);
  847         g_post_event(g_disk_destroy, dp, M_WAITOK, NULL);
  848 }
  849 
  850 void
  851 disk_gone(struct disk *dp)
  852 {
  853         struct g_geom *gp;
  854         struct g_provider *pp;
  855 
  856         gp = dp->d_geom;
  857         if (gp != NULL) {
  858                 pp = LIST_FIRST(&gp->provider);
  859                 if (pp != NULL) {
  860                         KASSERT(LIST_NEXT(pp, provider) == NULL,
  861                             ("geom %p has more than one provider", gp));
  862                         g_wither_provider(pp, ENXIO);
  863                 }
  864         }
  865 }
  866 
  867 void
  868 disk_attr_changed(struct disk *dp, const char *attr, int flag)
  869 {
  870         struct g_geom *gp;
  871         struct g_provider *pp;
  872         char devnamebuf[128];
  873 
  874         gp = dp->d_geom;
  875         if (gp != NULL)
  876                 LIST_FOREACH(pp, &gp->provider, provider)
  877                         (void)g_attr_changed(pp, attr, flag);
  878         snprintf(devnamebuf, sizeof(devnamebuf), "devname=%s%d", dp->d_name,
  879             dp->d_unit);
  880         devctl_notify("GEOM", "disk", attr, devnamebuf);
  881 }
  882 
  883 void
  884 disk_media_changed(struct disk *dp, int flag)
  885 {
  886         struct g_geom *gp;
  887         struct g_provider *pp;
  888 
  889         gp = dp->d_geom;
  890         if (gp != NULL) {
  891                 pp = LIST_FIRST(&gp->provider);
  892                 if (pp != NULL) {
  893                         KASSERT(LIST_NEXT(pp, provider) == NULL,
  894                             ("geom %p has more than one provider", gp));
  895                         g_media_changed(pp, flag);
  896                 }
  897         }
  898 }
  899 
  900 void
  901 disk_media_gone(struct disk *dp, int flag)
  902 {
  903         struct g_geom *gp;
  904         struct g_provider *pp;
  905 
  906         gp = dp->d_geom;
  907         if (gp != NULL) {
  908                 pp = LIST_FIRST(&gp->provider);
  909                 if (pp != NULL) {
  910                         KASSERT(LIST_NEXT(pp, provider) == NULL,
  911                             ("geom %p has more than one provider", gp));
  912                         g_media_gone(pp, flag);
  913                 }
  914         }
  915 }
  916 
  917 int
  918 disk_resize(struct disk *dp, int flag)
  919 {
  920 
  921         if (dp->d_destroyed || dp->d_geom == NULL)
  922                 return (0);
  923 
  924         return (g_post_event(g_disk_resize, dp, flag, NULL));
  925 }
  926 
  927 static void
  928 g_kern_disks(void *p, int flag __unused)
  929 {
  930         struct sbuf *sb;
  931         struct g_geom *gp;
  932         char *sp;
  933 
  934         sb = p;
  935         sp = "";
  936         g_topology_assert();
  937         LIST_FOREACH(gp, &g_disk_class.geom, geom) {
  938                 sbuf_printf(sb, "%s%s", sp, gp->name);
  939                 sp = " ";
  940         }
  941         sbuf_finish(sb);
  942 }
  943 
  944 static int
  945 sysctl_disks(SYSCTL_HANDLER_ARGS)
  946 {
  947         int error;
  948         struct sbuf *sb;
  949 
  950         sb = sbuf_new_auto();
  951         g_waitfor_event(g_kern_disks, sb, M_WAITOK, NULL);
  952         error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
  953         sbuf_delete(sb);
  954         return error;
  955 }
  956  
  957 SYSCTL_PROC(_kern, OID_AUTO, disks,
  958     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
  959     sysctl_disks, "A", "names of available disks");

Cache object: 332bb0c9ca77c142062f8eacebf11058


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