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  * SPDX-License-Identifier: BSD-3-Clause
    3  *
    4  * Copyright (c) 2002 Poul-Henning Kamp
    5  * Copyright (c) 2002 Networks Associates Technology, Inc.
    6  * All rights reserved.
    7  *
    8  * This software was developed for the FreeBSD Project by Poul-Henning Kamp
    9  * and NAI Labs, the Security Research Division of Network Associates, Inc.
   10  * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
   11  * DARPA CHATS research program.
   12  *
   13  * Redistribution and use in source and binary forms, with or without
   14  * modification, are permitted provided that the following conditions
   15  * are met:
   16  * 1. Redistributions of source code must retain the above copyright
   17  *    notice, this list of conditions and the following disclaimer.
   18  * 2. Redistributions in binary form must reproduce the above copyright
   19  *    notice, this list of conditions and the following disclaimer in the
   20  *    documentation and/or other materials provided with the distribution.
   21  * 3. The names of the authors may not be used to endorse or promote
   22  *    products derived from this software without specific prior written
   23  *    permission.
   24  *
   25  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   35  * SUCH DAMAGE.
   36  */
   37 
   38 #include <sys/cdefs.h>
   39 __FBSDID("$FreeBSD$");
   40 
   41 #include "opt_geom.h"
   42 
   43 #include <sys/param.h>
   44 #include <sys/systm.h>
   45 #include <sys/kernel.h>
   46 #include <sys/sysctl.h>
   47 #include <sys/bio.h>
   48 #include <sys/bus.h>
   49 #include <sys/ctype.h>
   50 #include <sys/fcntl.h>
   51 #include <sys/malloc.h>
   52 #include <sys/sbuf.h>
   53 #include <sys/devicestat.h>
   54 #include <machine/md_var.h>
   55 
   56 #include <sys/lock.h>
   57 #include <sys/mutex.h>
   58 #include <geom/geom.h>
   59 #include <geom/geom_disk.h>
   60 #include <geom/geom_int.h>
   61 
   62 #include <dev/led/led.h>
   63 
   64 #include <machine/bus.h>
   65 
   66 struct g_disk_softc {
   67         struct mtx               done_mtx;
   68         struct disk             *dp;
   69         struct devstat          *d_devstat;
   70         struct sysctl_ctx_list  sysctl_ctx;
   71         struct sysctl_oid       *sysctl_tree;
   72         char                    led[64];
   73         uint32_t                state;
   74         struct mtx               start_mtx;
   75 };
   76 
   77 static g_access_t g_disk_access;
   78 static g_start_t g_disk_start;
   79 static g_ioctl_t g_disk_ioctl;
   80 static g_dumpconf_t g_disk_dumpconf;
   81 static g_provgone_t g_disk_providergone;
   82 
   83 static int g_disk_sysctl_flags(SYSCTL_HANDLER_ARGS);
   84 
   85 static struct g_class g_disk_class = {
   86         .name = G_DISK_CLASS_NAME,
   87         .version = G_VERSION,
   88         .start = g_disk_start,
   89         .access = g_disk_access,
   90         .ioctl = g_disk_ioctl,
   91         .providergone = g_disk_providergone,
   92         .dumpconf = g_disk_dumpconf,
   93 };
   94 
   95 SYSCTL_DECL(_kern_geom);
   96 static SYSCTL_NODE(_kern_geom, OID_AUTO, disk, CTLFLAG_RW, 0,
   97     "GEOM_DISK stuff");
   98 
   99 DECLARE_GEOM_CLASS(g_disk_class, g_disk);
  100 
  101 static int
  102 g_disk_access(struct g_provider *pp, int r, int w, int e)
  103 {
  104         struct disk *dp;
  105         struct g_disk_softc *sc;
  106         int error;
  107 
  108         g_trace(G_T_ACCESS, "g_disk_access(%s, %d, %d, %d)",
  109             pp->name, r, w, e);
  110         g_topology_assert();
  111         sc = pp->private;
  112         if ((dp = sc->dp) == NULL || dp->d_destroyed) {
  113                 /*
  114                  * Allow decreasing access count even if disk is not
  115                  * available anymore.
  116                  */
  117                 if (r <= 0 && w <= 0 && e <= 0)
  118                         return (0);
  119                 return (ENXIO);
  120         }
  121         r += pp->acr;
  122         w += pp->acw;
  123         e += pp->ace;
  124         error = 0;
  125         if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) {
  126                 /*
  127                  * It would be better to defer this decision to d_open if
  128                  * it was able to take flags.
  129                  */
  130                 if (w > 0 && (dp->d_flags & DISKFLAG_WRITE_PROTECT) != 0)
  131                         error = EROFS;
  132                 if (error == 0 && dp->d_open != NULL)
  133                         error = dp->d_open(dp);
  134                 if (bootverbose && error != 0)
  135                         printf("Opened disk %s -> %d\n", pp->name, error);
  136                 if (error != 0)
  137                         return (error);
  138                 pp->sectorsize = dp->d_sectorsize;
  139                 if (dp->d_maxsize == 0) {
  140                         printf("WARNING: Disk drive %s%d has no d_maxsize\n",
  141                             dp->d_name, dp->d_unit);
  142                         dp->d_maxsize = DFLTPHYS;
  143                 }
  144                 if (dp->d_delmaxsize == 0) {
  145                         if (bootverbose && dp->d_flags & DISKFLAG_CANDELETE) {
  146                                 printf("WARNING: Disk drive %s%d has no "
  147                                     "d_delmaxsize\n", dp->d_name, dp->d_unit);
  148                         }
  149                         dp->d_delmaxsize = dp->d_maxsize;
  150                 }
  151                 pp->stripeoffset = dp->d_stripeoffset;
  152                 pp->stripesize = dp->d_stripesize;
  153                 dp->d_flags |= DISKFLAG_OPEN;
  154                 /*
  155                  * Do not invoke resize event when initial size was zero.
  156                  * Some disks report its size only after first opening.
  157                  */
  158                 if (pp->mediasize == 0)
  159                         pp->mediasize = dp->d_mediasize;
  160                 else
  161                         g_resize_provider(pp, dp->d_mediasize);
  162         } else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) {
  163                 if (dp->d_close != NULL) {
  164                         error = dp->d_close(dp);
  165                         if (error != 0)
  166                                 printf("Closed disk %s -> %d\n",
  167                                     pp->name, error);
  168                 }
  169                 sc->state = G_STATE_ACTIVE;
  170                 if (sc->led[0] != 0)
  171                         led_set(sc->led, "");
  172                 dp->d_flags &= ~DISKFLAG_OPEN;
  173         }
  174         return (error);
  175 }
  176 
  177 static void
  178 g_disk_kerneldump(struct bio *bp, struct disk *dp)
  179 {
  180         struct g_kerneldump *gkd;
  181         struct g_geom *gp;
  182 
  183         gkd = (struct g_kerneldump*)bp->bio_data;
  184         gp = bp->bio_to->geom;
  185         g_trace(G_T_TOPOLOGY, "g_disk_kerneldump(%s, %jd, %jd)",
  186                 gp->name, (intmax_t)gkd->offset, (intmax_t)gkd->length);
  187         if (dp->d_dump == NULL) {
  188                 g_io_deliver(bp, ENODEV);
  189                 return;
  190         }
  191         gkd->di.dumper = dp->d_dump;
  192         gkd->di.priv = dp;
  193         gkd->di.blocksize = dp->d_sectorsize;
  194         gkd->di.maxiosize = dp->d_maxsize;
  195         gkd->di.mediaoffset = gkd->offset;
  196         if ((gkd->offset + gkd->length) > dp->d_mediasize)
  197                 gkd->length = dp->d_mediasize - gkd->offset;
  198         gkd->di.mediasize = gkd->length;
  199         g_io_deliver(bp, 0);
  200 }
  201 
  202 static void
  203 g_disk_setstate(struct bio *bp, struct g_disk_softc *sc)
  204 {
  205         const char *cmd;
  206 
  207         memcpy(&sc->state, bp->bio_data, sizeof(sc->state));
  208         if (sc->led[0] != 0) {
  209                 switch (sc->state) {
  210                 case G_STATE_FAILED:
  211                         cmd = "1";
  212                         break;
  213                 case G_STATE_REBUILD:
  214                         cmd = "f5";
  215                         break;
  216                 case G_STATE_RESYNC:
  217                         cmd = "f1";
  218                         break;
  219                 default:
  220                         cmd = "";
  221                         break;
  222                 }
  223                 led_set(sc->led, cmd);
  224         }
  225         g_io_deliver(bp, 0);
  226 }
  227 
  228 static void
  229 g_disk_done(struct bio *bp)
  230 {
  231         struct bintime now;
  232         struct bio *bp2;
  233         struct g_disk_softc *sc;
  234 
  235         /* See "notes" for why we need a mutex here */
  236         sc = bp->bio_caller1;
  237         bp2 = bp->bio_parent;
  238         binuptime(&now);
  239         mtx_lock(&sc->done_mtx);
  240         if (bp2->bio_error == 0)
  241                 bp2->bio_error = bp->bio_error;
  242         bp2->bio_completed += bp->bio_length - bp->bio_resid;
  243 
  244         switch (bp->bio_cmd) {
  245         case BIO_ZONE:
  246                 bcopy(&bp->bio_zone, &bp2->bio_zone, sizeof(bp->bio_zone));
  247                 /*FALLTHROUGH*/
  248         case BIO_READ:
  249         case BIO_WRITE:
  250         case BIO_DELETE:
  251         case BIO_FLUSH:
  252                 devstat_end_transaction_bio_bt(sc->d_devstat, bp, &now);
  253                 break;
  254         default:
  255                 break;
  256         }
  257         bp2->bio_inbed++;
  258         if (bp2->bio_children == bp2->bio_inbed) {
  259                 mtx_unlock(&sc->done_mtx);
  260                 bp2->bio_resid = bp2->bio_bcount - bp2->bio_completed;
  261                 g_io_deliver(bp2, bp2->bio_error);
  262         } else
  263                 mtx_unlock(&sc->done_mtx);
  264         g_destroy_bio(bp);
  265 }
  266 
  267 static int
  268 g_disk_ioctl(struct g_provider *pp, u_long cmd, void * data, int fflag, struct thread *td)
  269 {
  270         struct disk *dp;
  271         struct g_disk_softc *sc;
  272         int error;
  273 
  274         sc = pp->private;
  275         dp = sc->dp;
  276         KASSERT(dp != NULL && !dp->d_destroyed,
  277             ("g_disk_ioctl(%lx) on destroyed disk %s", cmd, pp->name));
  278 
  279         if (dp->d_ioctl == NULL)
  280                 return (ENOIOCTL);
  281         error = dp->d_ioctl(dp, cmd, data, fflag, td);
  282         return (error);
  283 }
  284 
  285 static off_t
  286 g_disk_maxsize(struct disk *dp, struct bio *bp)
  287 {
  288         if (bp->bio_cmd == BIO_DELETE)
  289                 return (dp->d_delmaxsize);
  290         return (dp->d_maxsize);
  291 }
  292 
  293 static int
  294 g_disk_maxsegs(struct disk *dp, struct bio *bp)
  295 {
  296         return ((g_disk_maxsize(dp, bp) / PAGE_SIZE) + 1);
  297 }
  298 
  299 static void
  300 g_disk_advance(struct disk *dp, struct bio *bp, off_t off)
  301 {
  302 
  303         bp->bio_offset += off;
  304         bp->bio_length -= off;
  305 
  306         if ((bp->bio_flags & BIO_VLIST) != 0) {
  307                 bus_dma_segment_t *seg, *end;
  308 
  309                 seg = (bus_dma_segment_t *)bp->bio_data;
  310                 end = (bus_dma_segment_t *)bp->bio_data + bp->bio_ma_n;
  311                 off += bp->bio_ma_offset;
  312                 while (off >= seg->ds_len) {
  313                         KASSERT((seg != end),
  314                             ("vlist request runs off the end"));
  315                         off -= seg->ds_len;
  316                         seg++;
  317                 }
  318                 bp->bio_ma_offset = off;
  319                 bp->bio_ma_n = end - seg;
  320                 bp->bio_data = (void *)seg;
  321         } else if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
  322                 bp->bio_ma += off / PAGE_SIZE;
  323                 bp->bio_ma_offset += off;
  324                 bp->bio_ma_offset %= PAGE_SIZE;
  325                 bp->bio_ma_n -= off / PAGE_SIZE;
  326         } else {
  327                 bp->bio_data += off;
  328         }
  329 }
  330 
  331 static void
  332 g_disk_seg_limit(bus_dma_segment_t *seg, off_t *poffset,
  333     off_t *plength, int *ppages)
  334 {
  335         uintptr_t seg_page_base;
  336         uintptr_t seg_page_end;
  337         off_t offset;
  338         off_t length;
  339         int seg_pages;
  340 
  341         offset = *poffset;
  342         length = *plength;
  343 
  344         if (length > seg->ds_len - offset)
  345                 length = seg->ds_len - offset;
  346 
  347         seg_page_base = trunc_page(seg->ds_addr + offset);
  348         seg_page_end  = round_page(seg->ds_addr + offset + length);
  349         seg_pages = (seg_page_end - seg_page_base) >> PAGE_SHIFT;
  350 
  351         if (seg_pages > *ppages) {
  352                 seg_pages = *ppages;
  353                 length = (seg_page_base + (seg_pages << PAGE_SHIFT)) -
  354                     (seg->ds_addr + offset);
  355         }
  356 
  357         *poffset = 0;
  358         *plength -= length;
  359         *ppages -= seg_pages;
  360 }
  361 
  362 static off_t
  363 g_disk_vlist_limit(struct disk *dp, struct bio *bp, bus_dma_segment_t **pendseg)
  364 {
  365         bus_dma_segment_t *seg, *end;
  366         off_t residual;
  367         off_t offset;
  368         int pages;
  369 
  370         seg = (bus_dma_segment_t *)bp->bio_data;
  371         end = (bus_dma_segment_t *)bp->bio_data + bp->bio_ma_n;
  372         residual = bp->bio_length;
  373         offset = bp->bio_ma_offset;
  374         pages = g_disk_maxsegs(dp, bp);
  375         while (residual != 0 && pages != 0) {
  376                 KASSERT((seg != end),
  377                     ("vlist limit runs off the end"));
  378                 g_disk_seg_limit(seg, &offset, &residual, &pages);
  379                 seg++;
  380         }
  381         if (pendseg != NULL)
  382                 *pendseg = seg;
  383         return (residual);
  384 }
  385 
  386 static bool
  387 g_disk_limit(struct disk *dp, struct bio *bp)
  388 {
  389         bool limited = false;
  390         off_t maxsz;
  391 
  392         maxsz = g_disk_maxsize(dp, bp);
  393 
  394         /*
  395          * XXX: If we have a stripesize we should really use it here.
  396          *      Care should be taken in the delete case if this is done
  397          *      as deletes can be very sensitive to size given how they
  398          *      are processed.
  399          */
  400         if (bp->bio_length > maxsz) {
  401                 bp->bio_length = maxsz;
  402                 limited = true;
  403         }
  404 
  405         if ((bp->bio_flags & BIO_VLIST) != 0) {
  406                 bus_dma_segment_t *firstseg, *endseg;
  407                 off_t residual;
  408 
  409                 firstseg = (bus_dma_segment_t*)bp->bio_data;
  410                 residual = g_disk_vlist_limit(dp, bp, &endseg);
  411                 if (residual != 0) {
  412                         bp->bio_ma_n = endseg - firstseg;
  413                         bp->bio_length -= residual;
  414                         limited = true;
  415                 }
  416         } else if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
  417                 bp->bio_ma_n =
  418                     howmany(bp->bio_ma_offset + bp->bio_length, PAGE_SIZE);
  419         }
  420 
  421         return (limited);
  422 }
  423 
  424 static void
  425 g_disk_start(struct bio *bp)
  426 {
  427         struct bio *bp2, *bp3;
  428         struct disk *dp;
  429         struct g_disk_softc *sc;
  430         int error;
  431         off_t off;
  432 
  433         biotrack(bp, __func__);
  434 
  435         sc = bp->bio_to->private;
  436         dp = sc->dp;
  437         KASSERT(dp != NULL && !dp->d_destroyed,
  438             ("g_disk_start(%p) on destroyed disk %s", bp, bp->bio_to->name));
  439         error = EJUSTRETURN;
  440         switch(bp->bio_cmd) {
  441         case BIO_DELETE:
  442                 if (!(dp->d_flags & DISKFLAG_CANDELETE)) {
  443                         error = EOPNOTSUPP;
  444                         break;
  445                 }
  446                 /* fall-through */
  447         case BIO_READ:
  448         case BIO_WRITE:
  449                 KASSERT((dp->d_flags & DISKFLAG_UNMAPPED_BIO) != 0 ||
  450                     (bp->bio_flags & BIO_UNMAPPED) == 0,
  451                     ("unmapped bio not supported by disk %s", dp->d_name));
  452                 off = 0;
  453                 bp3 = NULL;
  454                 bp2 = g_clone_bio(bp);
  455                 if (bp2 == NULL) {
  456                         error = ENOMEM;
  457                         break;
  458                 }
  459                 for (;;) {
  460                         if (g_disk_limit(dp, bp2)) {
  461                                 off += bp2->bio_length;
  462 
  463                                 /*
  464                                  * To avoid a race, we need to grab the next bio
  465                                  * before we schedule this one.  See "notes".
  466                                  */
  467                                 bp3 = g_clone_bio(bp);
  468                                 if (bp3 == NULL)
  469                                         bp->bio_error = ENOMEM;
  470                         }
  471                         bp2->bio_done = g_disk_done;
  472                         bp2->bio_caller1 = sc;
  473                         bp2->bio_pblkno = bp2->bio_offset / dp->d_sectorsize;
  474                         bp2->bio_bcount = bp2->bio_length;
  475                         bp2->bio_disk = dp;
  476                         mtx_lock(&sc->start_mtx); 
  477                         devstat_start_transaction_bio(dp->d_devstat, bp2);
  478                         mtx_unlock(&sc->start_mtx); 
  479                         dp->d_strategy(bp2);
  480 
  481                         if (bp3 == NULL)
  482                                 break;
  483 
  484                         bp2 = bp3;
  485                         bp3 = NULL;
  486                         g_disk_advance(dp, bp2, off);
  487                 }
  488                 break;
  489         case BIO_GETATTR:
  490                 /* Give the driver a chance to override */
  491                 if (dp->d_getattr != NULL) {
  492                         if (bp->bio_disk == NULL)
  493                                 bp->bio_disk = dp;
  494                         error = dp->d_getattr(bp);
  495                         if (error != -1)
  496                                 break;
  497                         error = EJUSTRETURN;
  498                 }
  499                 if (g_handleattr_int(bp, "GEOM::candelete",
  500                     (dp->d_flags & DISKFLAG_CANDELETE) != 0))
  501                         break;
  502                 else if (g_handleattr_int(bp, "GEOM::fwsectors",
  503                     dp->d_fwsectors))
  504                         break;
  505                 else if (g_handleattr_int(bp, "GEOM::fwheads", dp->d_fwheads))
  506                         break;
  507                 else if (g_handleattr_off_t(bp, "GEOM::frontstuff", 0))
  508                         break;
  509                 else if (g_handleattr_str(bp, "GEOM::ident", dp->d_ident))
  510                         break;
  511                 else if (g_handleattr_str(bp, "GEOM::descr", dp->d_descr))
  512                         break;
  513                 else if (g_handleattr_uint16_t(bp, "GEOM::hba_vendor",
  514                     dp->d_hba_vendor))
  515                         break;
  516                 else if (g_handleattr_uint16_t(bp, "GEOM::hba_device",
  517                     dp->d_hba_device))
  518                         break;
  519                 else if (g_handleattr_uint16_t(bp, "GEOM::hba_subvendor",
  520                     dp->d_hba_subvendor))
  521                         break;
  522                 else if (g_handleattr_uint16_t(bp, "GEOM::hba_subdevice",
  523                     dp->d_hba_subdevice))
  524                         break;
  525                 else if (!strcmp(bp->bio_attribute, "GEOM::kerneldump"))
  526                         g_disk_kerneldump(bp, dp);
  527                 else if (!strcmp(bp->bio_attribute, "GEOM::setstate"))
  528                         g_disk_setstate(bp, sc);
  529                 else if (g_handleattr_uint16_t(bp, "GEOM::rotation_rate",
  530                     dp->d_rotation_rate))
  531                         break;
  532                 else 
  533                         error = ENOIOCTL;
  534                 break;
  535         case BIO_FLUSH:
  536                 g_trace(G_T_BIO, "g_disk_flushcache(%s)",
  537                     bp->bio_to->name);
  538                 if (!(dp->d_flags & DISKFLAG_CANFLUSHCACHE)) {
  539                         error = EOPNOTSUPP;
  540                         break;
  541                 }
  542                 /*FALLTHROUGH*/
  543         case BIO_ZONE:
  544                 if (bp->bio_cmd == BIO_ZONE) {
  545                         if (!(dp->d_flags & DISKFLAG_CANZONE)) {
  546                                 error = EOPNOTSUPP;
  547                                 break;
  548                         }
  549                         g_trace(G_T_BIO, "g_disk_zone(%s)",
  550                             bp->bio_to->name);
  551                 }
  552                 bp2 = g_clone_bio(bp);
  553                 if (bp2 == NULL) {
  554                         g_io_deliver(bp, ENOMEM);
  555                         return;
  556                 }
  557                 bp2->bio_done = g_disk_done;
  558                 bp2->bio_caller1 = sc;
  559                 bp2->bio_disk = dp;
  560                 mtx_lock(&sc->start_mtx);
  561                 devstat_start_transaction_bio(dp->d_devstat, bp2);
  562                 mtx_unlock(&sc->start_mtx);
  563                 dp->d_strategy(bp2);
  564                 break;
  565         default:
  566                 error = EOPNOTSUPP;
  567                 break;
  568         }
  569         if (error != EJUSTRETURN)
  570                 g_io_deliver(bp, error);
  571         return;
  572 }
  573 
  574 static void
  575 g_disk_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp)
  576 {
  577         struct bio *bp;
  578         struct disk *dp;
  579         struct g_disk_softc *sc;
  580         char *buf;
  581         int res = 0;
  582 
  583         sc = gp->softc;
  584         if (sc == NULL || (dp = sc->dp) == NULL)
  585                 return;
  586         if (indent == NULL) {
  587                 sbuf_printf(sb, " hd %u", dp->d_fwheads);
  588                 sbuf_printf(sb, " sc %u", dp->d_fwsectors);
  589                 return;
  590         }
  591         if (pp != NULL) {
  592                 sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n",
  593                     indent, dp->d_fwheads);
  594                 sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n",
  595                     indent, dp->d_fwsectors);
  596 
  597                 /*
  598                  * "rotationrate" is a little complicated, because the value
  599                  * returned by the drive might not be the RPM; 0 and 1 are
  600                  * special cases, and there's also a valid range.
  601                  */
  602                 sbuf_printf(sb, "%s<rotationrate>", indent);
  603                 if (dp->d_rotation_rate == DISK_RR_UNKNOWN) /* Old drives */
  604                         sbuf_cat(sb, "unknown");        /* don't report RPM. */
  605                 else if (dp->d_rotation_rate == DISK_RR_NON_ROTATING)
  606                         sbuf_cat(sb, "");
  607                 else if ((dp->d_rotation_rate >= DISK_RR_MIN) &&
  608                     (dp->d_rotation_rate <= DISK_RR_MAX))
  609                         sbuf_printf(sb, "%u", dp->d_rotation_rate);
  610                 else
  611                         sbuf_cat(sb, "invalid");
  612                 sbuf_cat(sb, "</rotationrate>\n");
  613                 if (dp->d_getattr != NULL) {
  614                         buf = g_malloc(DISK_IDENT_SIZE, M_WAITOK);
  615                         bp = g_alloc_bio();
  616                         bp->bio_disk = dp;
  617                         bp->bio_attribute = "GEOM::ident";
  618                         bp->bio_length = DISK_IDENT_SIZE;
  619                         bp->bio_data = buf;
  620                         res = dp->d_getattr(bp);
  621                         sbuf_printf(sb, "%s<ident>", indent);
  622                         g_conf_cat_escaped(sb, res == 0 ? buf : dp->d_ident);
  623                         sbuf_cat(sb, "</ident>\n");
  624                         bp->bio_attribute = "GEOM::lunid";
  625                         bp->bio_length = DISK_IDENT_SIZE;
  626                         bp->bio_data = buf;
  627                         if (dp->d_getattr(bp) == 0) {
  628                                 sbuf_printf(sb, "%s<lunid>", indent);
  629                                 g_conf_cat_escaped(sb, buf);
  630                                 sbuf_cat(sb, "</lunid>\n");
  631                         }
  632                         bp->bio_attribute = "GEOM::lunname";
  633                         bp->bio_length = DISK_IDENT_SIZE;
  634                         bp->bio_data = buf;
  635                         if (dp->d_getattr(bp) == 0) {
  636                                 sbuf_printf(sb, "%s<lunname>", indent);
  637                                 g_conf_cat_escaped(sb, buf);
  638                                 sbuf_cat(sb, "</lunname>\n");
  639                         }
  640                         g_destroy_bio(bp);
  641                         g_free(buf);
  642                 } else {
  643                         sbuf_printf(sb, "%s<ident>", indent);
  644                         g_conf_cat_escaped(sb, dp->d_ident);
  645                         sbuf_cat(sb, "</ident>\n");
  646                 }
  647                 sbuf_printf(sb, "%s<descr>", indent);
  648                 g_conf_cat_escaped(sb, dp->d_descr);
  649                 sbuf_cat(sb, "</descr>\n");
  650         }
  651 }
  652 
  653 static void
  654 g_disk_resize(void *ptr, int flag)
  655 {
  656         struct disk *dp;
  657         struct g_geom *gp;
  658         struct g_provider *pp;
  659 
  660         if (flag == EV_CANCEL)
  661                 return;
  662         g_topology_assert();
  663 
  664         dp = ptr;
  665         gp = dp->d_geom;
  666 
  667         if (dp->d_destroyed || gp == NULL)
  668                 return;
  669 
  670         LIST_FOREACH(pp, &gp->provider, provider) {
  671                 if (pp->sectorsize != 0 &&
  672                     pp->sectorsize != dp->d_sectorsize)
  673                         g_wither_provider(pp, ENXIO);
  674                 else
  675                         g_resize_provider(pp, dp->d_mediasize);
  676         }
  677 }
  678 
  679 static void
  680 g_disk_create(void *arg, int flag)
  681 {
  682         struct g_geom *gp;
  683         struct g_provider *pp;
  684         struct disk *dp;
  685         struct g_disk_softc *sc;
  686         struct disk_alias *dap;
  687         char tmpstr[80];
  688 
  689         if (flag == EV_CANCEL)
  690                 return;
  691         g_topology_assert();
  692         dp = arg;
  693 
  694         mtx_pool_lock(mtxpool_sleep, dp);
  695         dp->d_init_level = DISK_INIT_START;
  696 
  697         /*
  698          * If the disk has already gone away, we can just stop here and
  699          * call the user's callback to tell him we've cleaned things up.
  700          */
  701         if (dp->d_goneflag != 0) {
  702                 mtx_pool_unlock(mtxpool_sleep, dp);
  703                 if (dp->d_gone != NULL)
  704                         dp->d_gone(dp);
  705                 return;
  706         }
  707         mtx_pool_unlock(mtxpool_sleep, dp);
  708 
  709         sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
  710         mtx_init(&sc->start_mtx, "g_disk_start", NULL, MTX_DEF);
  711         mtx_init(&sc->done_mtx, "g_disk_done", NULL, MTX_DEF);
  712         sc->dp = dp;
  713         sc->d_devstat = dp->d_devstat;
  714         gp = g_new_geomf(&g_disk_class, "%s%d", dp->d_name, dp->d_unit);
  715         gp->softc = sc;
  716         LIST_FOREACH(dap, &dp->d_aliases, da_next) {
  717                 snprintf(tmpstr, sizeof(tmpstr), "%s%d", dap->da_alias, dp->d_unit);
  718                 g_geom_add_alias(gp, tmpstr);
  719         }
  720         pp = g_new_providerf(gp, "%s", gp->name);
  721         devstat_remove_entry(pp->stat);
  722         pp->stat = NULL;
  723         dp->d_devstat->id = pp;
  724         pp->mediasize = dp->d_mediasize;
  725         pp->sectorsize = dp->d_sectorsize;
  726         pp->stripeoffset = dp->d_stripeoffset;
  727         pp->stripesize = dp->d_stripesize;
  728         if ((dp->d_flags & DISKFLAG_UNMAPPED_BIO) != 0)
  729                 pp->flags |= G_PF_ACCEPT_UNMAPPED;
  730         if ((dp->d_flags & DISKFLAG_DIRECT_COMPLETION) != 0)
  731                 pp->flags |= G_PF_DIRECT_SEND;
  732         pp->flags |= G_PF_DIRECT_RECEIVE;
  733         if (bootverbose)
  734                 printf("GEOM: new disk %s\n", gp->name);
  735         sysctl_ctx_init(&sc->sysctl_ctx);
  736         snprintf(tmpstr, sizeof(tmpstr), "GEOM disk %s", gp->name);
  737         sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
  738                 SYSCTL_STATIC_CHILDREN(_kern_geom_disk), OID_AUTO, gp->name,
  739                 CTLFLAG_RD, 0, tmpstr);
  740         if (sc->sysctl_tree != NULL) {
  741                 SYSCTL_ADD_STRING(&sc->sysctl_ctx,
  742                     SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO, "led",
  743                     CTLFLAG_RWTUN, sc->led, sizeof(sc->led),
  744                     "LED name");
  745                 SYSCTL_ADD_PROC(&sc->sysctl_ctx,
  746                     SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO, "flags",
  747                     CTLTYPE_STRING | CTLFLAG_RD, dp, 0, g_disk_sysctl_flags,
  748                     "A", "Report disk flags");
  749         }
  750         pp->private = sc;
  751         dp->d_geom = gp;
  752         g_error_provider(pp, 0);
  753 
  754         mtx_pool_lock(mtxpool_sleep, dp);
  755         dp->d_init_level = DISK_INIT_DONE;
  756 
  757         /*
  758          * If the disk has gone away at this stage, start the withering
  759          * process for it.
  760          */
  761         if (dp->d_goneflag != 0) {
  762                 mtx_pool_unlock(mtxpool_sleep, dp);
  763                 g_wither_provider(pp, ENXIO);
  764                 return;
  765         }
  766         mtx_pool_unlock(mtxpool_sleep, dp);
  767 
  768 }
  769 
  770 /*
  771  * We get this callback after all of the consumers have gone away, and just
  772  * before the provider is freed.  If the disk driver provided a d_gone
  773  * callback, let them know that it is okay to free resources -- they won't
  774  * be getting any more accesses from GEOM.
  775  */
  776 static void
  777 g_disk_providergone(struct g_provider *pp)
  778 {
  779         struct disk *dp;
  780         struct g_disk_softc *sc;
  781 
  782         sc = (struct g_disk_softc *)pp->private;
  783         dp = sc->dp;
  784         if (dp != NULL && dp->d_gone != NULL)
  785                 dp->d_gone(dp);
  786         if (sc->sysctl_tree != NULL) {
  787                 sysctl_ctx_free(&sc->sysctl_ctx);
  788                 sc->sysctl_tree = NULL;
  789         }
  790         if (sc->led[0] != 0) {
  791                 led_set(sc->led, "");
  792                 sc->led[0] = 0;
  793         }
  794         pp->private = NULL;
  795         pp->geom->softc = NULL;
  796         mtx_destroy(&sc->done_mtx);
  797         mtx_destroy(&sc->start_mtx);
  798         g_free(sc);
  799 }
  800 
  801 static void
  802 g_disk_destroy(void *ptr, int flag)
  803 {
  804         struct disk *dp;
  805         struct g_geom *gp;
  806         struct g_disk_softc *sc;
  807         struct disk_alias *dap, *daptmp;
  808 
  809         g_topology_assert();
  810         dp = ptr;
  811         gp = dp->d_geom;
  812         if (gp != NULL) {
  813                 sc = gp->softc;
  814                 if (sc != NULL)
  815                         sc->dp = NULL;
  816                 dp->d_geom = NULL;
  817                 g_wither_geom(gp, ENXIO);
  818         }
  819         LIST_FOREACH_SAFE(dap, &dp->d_aliases, da_next, daptmp)
  820                 g_free(dap);
  821 
  822         g_free(dp);
  823 }
  824 
  825 /*
  826  * We only allow printable characters in disk ident,
  827  * the rest is converted to 'x<HH>'.
  828  */
  829 static void
  830 g_disk_ident_adjust(char *ident, size_t size)
  831 {
  832         char *p, tmp[4], newid[DISK_IDENT_SIZE];
  833 
  834         newid[0] = '\0';
  835         for (p = ident; *p != '\0'; p++) {
  836                 if (isprint(*p)) {
  837                         tmp[0] = *p;
  838                         tmp[1] = '\0';
  839                 } else {
  840                         snprintf(tmp, sizeof(tmp), "x%02hhx",
  841                             *(unsigned char *)p);
  842                 }
  843                 if (strlcat(newid, tmp, sizeof(newid)) >= sizeof(newid))
  844                         break;
  845         }
  846         bzero(ident, size);
  847         strlcpy(ident, newid, size);
  848 }
  849 
  850 struct disk *
  851 disk_alloc(void)
  852 {
  853         struct disk *dp;
  854 
  855         dp = g_malloc(sizeof(struct disk), M_WAITOK | M_ZERO);
  856         LIST_INIT(&dp->d_aliases);
  857         return (dp);
  858 }
  859 
  860 void
  861 disk_create(struct disk *dp, int version)
  862 {
  863 
  864         if (version != DISK_VERSION) {
  865                 printf("WARNING: Attempt to add disk %s%d %s",
  866                     dp->d_name, dp->d_unit,
  867                     " using incompatible ABI version of disk(9)\n");
  868                 printf("WARNING: Ignoring disk %s%d\n",
  869                     dp->d_name, dp->d_unit);
  870                 return;
  871         }
  872         if (dp->d_flags & DISKFLAG_RESERVED) {
  873                 printf("WARNING: Attempt to add non-MPSAFE disk %s%d\n",
  874                     dp->d_name, dp->d_unit);
  875                 printf("WARNING: Ignoring disk %s%d\n",
  876                     dp->d_name, dp->d_unit);
  877                 return;
  878         }
  879         KASSERT(dp->d_strategy != NULL, ("disk_create need d_strategy"));
  880         KASSERT(dp->d_name != NULL, ("disk_create need d_name"));
  881         KASSERT(*dp->d_name != 0, ("disk_create need d_name"));
  882         KASSERT(strlen(dp->d_name) < SPECNAMELEN - 4, ("disk name too long"));
  883         if (dp->d_devstat == NULL)
  884                 dp->d_devstat = devstat_new_entry(dp->d_name, dp->d_unit,
  885                     dp->d_sectorsize, DEVSTAT_ALL_SUPPORTED,
  886                     DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
  887         dp->d_geom = NULL;
  888 
  889         dp->d_init_level = DISK_INIT_NONE;
  890 
  891         g_disk_ident_adjust(dp->d_ident, sizeof(dp->d_ident));
  892         g_post_event(g_disk_create, dp, M_WAITOK, dp, NULL);
  893 }
  894 
  895 void
  896 disk_destroy(struct disk *dp)
  897 {
  898 
  899         disk_gone(dp);
  900         dp->d_destroyed = 1;
  901         g_cancel_event(dp);
  902         if (dp->d_devstat != NULL)
  903                 devstat_remove_entry(dp->d_devstat);
  904         g_post_event(g_disk_destroy, dp, M_WAITOK, NULL);
  905 }
  906 
  907 void
  908 disk_add_alias(struct disk *dp, const char *name)
  909 {
  910         struct disk_alias *dap;
  911 
  912         dap = (struct disk_alias *)g_malloc(
  913                 sizeof(struct disk_alias) + strlen(name) + 1, M_WAITOK);
  914         strcpy((char *)(dap + 1), name);
  915         dap->da_alias = (const char *)(dap + 1);
  916         LIST_INSERT_HEAD(&dp->d_aliases, dap, da_next);
  917 }
  918 
  919 void
  920 disk_gone(struct disk *dp)
  921 {
  922         struct g_geom *gp;
  923         struct g_provider *pp;
  924 
  925         mtx_pool_lock(mtxpool_sleep, dp);
  926 
  927         /*
  928          * Second wither call makes no sense, plus we can not access the list
  929          * of providers without topology lock after calling wither once.
  930          */
  931         if (dp->d_goneflag != 0) {
  932                 mtx_pool_unlock(mtxpool_sleep, dp);
  933                 return;
  934         }
  935 
  936         dp->d_goneflag = 1;
  937 
  938         /*
  939          * If we're still in the process of creating this disk (the
  940          * g_disk_create() function is still queued, or is in
  941          * progress), the init level will not yet be DISK_INIT_DONE.
  942          *
  943          * If that is the case, g_disk_create() will see d_goneflag
  944          * and take care of cleaning things up.
  945          *
  946          * If the disk has already been created, we default to
  947          * withering the provider as usual below.
  948          *
  949          * If the caller has not set a d_gone() callback, he will
  950          * not be any worse off by returning here, because the geom
  951          * has not been fully setup in any case.
  952          */
  953         if (dp->d_init_level < DISK_INIT_DONE) {
  954                 mtx_pool_unlock(mtxpool_sleep, dp);
  955                 return;
  956         }
  957         mtx_pool_unlock(mtxpool_sleep, dp);
  958 
  959         gp = dp->d_geom;
  960         pp = LIST_FIRST(&gp->provider);
  961         if (pp != NULL) {
  962                 KASSERT(LIST_NEXT(pp, provider) == NULL,
  963                     ("geom %p has more than one provider", gp));
  964                 g_wither_provider(pp, ENXIO);
  965         }
  966 }
  967 
  968 void
  969 disk_attr_changed(struct disk *dp, const char *attr, int flag)
  970 {
  971         struct g_geom *gp;
  972         struct g_provider *pp;
  973         char devnamebuf[128];
  974 
  975         gp = dp->d_geom;
  976         if (gp != NULL)
  977                 LIST_FOREACH(pp, &gp->provider, provider)
  978                         (void)g_attr_changed(pp, attr, flag);
  979         snprintf(devnamebuf, sizeof(devnamebuf), "devname=%s%d", dp->d_name,
  980             dp->d_unit);
  981         devctl_notify("GEOM", "disk", attr, devnamebuf);
  982 }
  983 
  984 void
  985 disk_media_changed(struct disk *dp, int flag)
  986 {
  987         struct g_geom *gp;
  988         struct g_provider *pp;
  989 
  990         gp = dp->d_geom;
  991         if (gp != NULL) {
  992                 pp = LIST_FIRST(&gp->provider);
  993                 if (pp != NULL) {
  994                         KASSERT(LIST_NEXT(pp, provider) == NULL,
  995                             ("geom %p has more than one provider", gp));
  996                         g_media_changed(pp, flag);
  997                 }
  998         }
  999 }
 1000 
 1001 void
 1002 disk_media_gone(struct disk *dp, int flag)
 1003 {
 1004         struct g_geom *gp;
 1005         struct g_provider *pp;
 1006 
 1007         gp = dp->d_geom;
 1008         if (gp != NULL) {
 1009                 pp = LIST_FIRST(&gp->provider);
 1010                 if (pp != NULL) {
 1011                         KASSERT(LIST_NEXT(pp, provider) == NULL,
 1012                             ("geom %p has more than one provider", gp));
 1013                         g_media_gone(pp, flag);
 1014                 }
 1015         }
 1016 }
 1017 
 1018 int
 1019 disk_resize(struct disk *dp, int flag)
 1020 {
 1021 
 1022         if (dp->d_destroyed || dp->d_geom == NULL)
 1023                 return (0);
 1024 
 1025         return (g_post_event(g_disk_resize, dp, flag, NULL));
 1026 }
 1027 
 1028 static void
 1029 g_kern_disks(void *p, int flag __unused)
 1030 {
 1031         struct sbuf *sb;
 1032         struct g_geom *gp;
 1033         char *sp;
 1034 
 1035         sb = p;
 1036         sp = "";
 1037         g_topology_assert();
 1038         LIST_FOREACH(gp, &g_disk_class.geom, geom) {
 1039                 sbuf_printf(sb, "%s%s", sp, gp->name);
 1040                 sp = " ";
 1041         }
 1042         sbuf_finish(sb);
 1043 }
 1044 
 1045 static int
 1046 g_disk_sysctl_flags(SYSCTL_HANDLER_ARGS)
 1047 {
 1048         struct disk *dp;
 1049         struct sbuf *sb;
 1050         int error;
 1051 
 1052         sb = sbuf_new_auto();
 1053         dp = (struct disk *)arg1;
 1054         sbuf_printf(sb, "%b", dp->d_flags,
 1055                 "\2"
 1056                 "\2OPEN"
 1057                 "\3CANDELETE"
 1058                 "\4CANFLUSHCACHE"
 1059                 "\5UNMAPPEDBIO"
 1060                 "\6DIRECTCOMPLETION"
 1061                 "\10CANZONE"
 1062                 "\11WRITEPROTECT");
 1063 
 1064         sbuf_finish(sb);
 1065         error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
 1066         sbuf_delete(sb);
 1067         return (error);
 1068 }
 1069 
 1070 static int
 1071 sysctl_disks(SYSCTL_HANDLER_ARGS)
 1072 {
 1073         int error;
 1074         struct sbuf *sb;
 1075 
 1076         sb = sbuf_new_auto();
 1077         g_waitfor_event(g_kern_disks, sb, M_WAITOK, NULL);
 1078         error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
 1079         sbuf_delete(sb);
 1080         return error;
 1081 }
 1082  
 1083 SYSCTL_PROC(_kern, OID_AUTO, disks,
 1084     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
 1085     sysctl_disks, "A", "names of available disks");

Cache object: d7b16bd028c849b82bceafee2ce42cb9


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