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: releng/9.2/sys/geom/geom_disk.c 252730 2013-07-04 21:57:09Z smh $");
   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/ctype.h>
   47 #include <sys/fcntl.h>
   48 #include <sys/malloc.h>
   49 #include <sys/sbuf.h>
   50 #include <sys/sysctl.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 struct g_disk_softc {
   63         struct mtx               done_mtx;
   64         struct disk             *dp;
   65         struct sysctl_ctx_list  sysctl_ctx;
   66         struct sysctl_oid       *sysctl_tree;
   67         char                    led[64];
   68         uint32_t                state;
   69 };
   70 
   71 static g_access_t g_disk_access;
   72 static g_start_t g_disk_start;
   73 static g_ioctl_t g_disk_ioctl;
   74 static g_dumpconf_t g_disk_dumpconf;
   75 static g_provgone_t g_disk_providergone;
   76 
   77 static struct g_class g_disk_class = {
   78         .name = "DISK",
   79         .version = G_VERSION,
   80         .start = g_disk_start,
   81         .access = g_disk_access,
   82         .ioctl = g_disk_ioctl,
   83         .providergone = g_disk_providergone,
   84         .dumpconf = g_disk_dumpconf,
   85 };
   86 
   87 SYSCTL_DECL(_kern_geom);
   88 static SYSCTL_NODE(_kern_geom, OID_AUTO, disk, CTLFLAG_RW, 0,
   89     "GEOM_DISK stuff");
   90 
   91 DECLARE_GEOM_CLASS(g_disk_class, g_disk);
   92 
   93 static void __inline
   94 g_disk_lock_giant(struct disk *dp)
   95 {
   96 
   97         if (dp->d_flags & DISKFLAG_NEEDSGIANT)
   98                 mtx_lock(&Giant);
   99 }
  100 
  101 static void __inline
  102 g_disk_unlock_giant(struct disk *dp)
  103 {
  104 
  105         if (dp->d_flags & DISKFLAG_NEEDSGIANT)
  106                 mtx_unlock(&Giant);
  107 }
  108 
  109 static int
  110 g_disk_access(struct g_provider *pp, int r, int w, int e)
  111 {
  112         struct disk *dp;
  113         struct g_disk_softc *sc;
  114         int error;
  115 
  116         g_trace(G_T_ACCESS, "g_disk_access(%s, %d, %d, %d)",
  117             pp->name, r, w, e);
  118         g_topology_assert();
  119         sc = pp->private;
  120         if (sc == NULL || (dp = sc->dp) == NULL || dp->d_destroyed) {
  121                 /*
  122                  * Allow decreasing access count even if disk is not
  123                  * avaliable anymore.
  124                  */
  125                 if (r <= 0 && w <= 0 && e <= 0)
  126                         return (0);
  127                 return (ENXIO);
  128         }
  129         r += pp->acr;
  130         w += pp->acw;
  131         e += pp->ace;
  132         error = 0;
  133         if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) {
  134                 if (dp->d_open != NULL) {
  135                         g_disk_lock_giant(dp);
  136                         error = dp->d_open(dp);
  137                         if (bootverbose && error != 0)
  138                                 printf("Opened disk %s -> %d\n",
  139                                     pp->name, error);
  140                         g_disk_unlock_giant(dp);
  141                         if (error != 0)
  142                                 return (error);
  143                 }
  144                 pp->mediasize = dp->d_mediasize;
  145                 pp->sectorsize = dp->d_sectorsize;
  146                 if (dp->d_flags & DISKFLAG_CANDELETE)
  147                         pp->flags |= G_PF_CANDELETE;
  148                 else
  149                         pp->flags &= ~G_PF_CANDELETE;
  150                 pp->stripeoffset = dp->d_stripeoffset;
  151                 pp->stripesize = dp->d_stripesize;
  152                 dp->d_flags |= DISKFLAG_OPEN;
  153                 if (dp->d_maxsize == 0) {
  154                         printf("WARNING: Disk drive %s%d has no d_maxsize\n",
  155                             dp->d_name, dp->d_unit);
  156                         dp->d_maxsize = DFLTPHYS;
  157                 }
  158                 if (dp->d_delmaxsize == 0) {
  159                         if (bootverbose && dp->d_flags & DISKFLAG_CANDELETE) {
  160                                 printf("WARNING: Disk drive %s%d has no "
  161                                     "d_delmaxsize\n", dp->d_name, dp->d_unit);
  162                         }
  163                         dp->d_delmaxsize = dp->d_maxsize;
  164                 }
  165         } else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) {
  166                 if (dp->d_close != NULL) {
  167                         g_disk_lock_giant(dp);
  168                         error = dp->d_close(dp);
  169                         if (error != 0)
  170                                 printf("Closed disk %s -> %d\n",
  171                                     pp->name, error);
  172                         g_disk_unlock_giant(dp);
  173                 }
  174                 sc->state = G_STATE_ACTIVE;
  175                 if (sc->led[0] != 0)
  176                         led_set(sc->led, "");
  177                 dp->d_flags &= ~DISKFLAG_OPEN;
  178         }
  179         return (error);
  180 }
  181 
  182 static void
  183 g_disk_kerneldump(struct bio *bp, struct disk *dp)
  184 {
  185         struct g_kerneldump *gkd;
  186         struct g_geom *gp;
  187 
  188         gkd = (struct g_kerneldump*)bp->bio_data;
  189         gp = bp->bio_to->geom;
  190         g_trace(G_T_TOPOLOGY, "g_disk_kernedump(%s, %jd, %jd)",
  191                 gp->name, (intmax_t)gkd->offset, (intmax_t)gkd->length);
  192         if (dp->d_dump == NULL) {
  193                 g_io_deliver(bp, ENODEV);
  194                 return;
  195         }
  196         gkd->di.dumper = dp->d_dump;
  197         gkd->di.priv = dp;
  198         gkd->di.blocksize = dp->d_sectorsize;
  199         gkd->di.maxiosize = dp->d_maxsize;
  200         gkd->di.mediaoffset = gkd->offset;
  201         if ((gkd->offset + gkd->length) > dp->d_mediasize)
  202                 gkd->length = dp->d_mediasize - gkd->offset;
  203         gkd->di.mediasize = gkd->length;
  204         g_io_deliver(bp, 0);
  205 }
  206 
  207 static void
  208 g_disk_setstate(struct bio *bp, struct g_disk_softc *sc)
  209 {
  210         const char *cmd;
  211 
  212         memcpy(&sc->state, bp->bio_data, sizeof(sc->state));
  213         if (sc->led[0] != 0) {
  214                 switch (sc->state) {
  215                 case G_STATE_FAILED:
  216                         cmd = "1";
  217                         break;
  218                 case G_STATE_REBUILD:
  219                         cmd = "f5";
  220                         break;
  221                 case G_STATE_RESYNC:
  222                         cmd = "f1";
  223                         break;
  224                 default:
  225                         cmd = "";
  226                         break;
  227                 }
  228                 led_set(sc->led, cmd);
  229         }
  230         g_io_deliver(bp, 0);
  231 }
  232 
  233 static void
  234 g_disk_done(struct bio *bp)
  235 {
  236         struct bio *bp2;
  237         struct g_disk_softc *sc;
  238 
  239         /* See "notes" for why we need a mutex here */
  240         /* XXX: will witness accept a mix of Giant/unGiant drivers here ? */
  241         bp2 = bp->bio_parent;
  242         sc = bp2->bio_to->private;
  243         bp->bio_completed = bp->bio_length - bp->bio_resid;
  244         mtx_lock(&sc->done_mtx);
  245         if (bp2->bio_error == 0)
  246                 bp2->bio_error = bp->bio_error;
  247         bp2->bio_completed += bp->bio_completed;
  248         if ((bp->bio_cmd & (BIO_READ|BIO_WRITE|BIO_DELETE)) != 0)
  249                 devstat_end_transaction_bio(sc->dp->d_devstat, bp);
  250         g_destroy_bio(bp);
  251         bp2->bio_inbed++;
  252         if (bp2->bio_children == bp2->bio_inbed) {
  253                 bp2->bio_resid = bp2->bio_bcount - bp2->bio_completed;
  254                 g_io_deliver(bp2, bp2->bio_error);
  255         }
  256         mtx_unlock(&sc->done_mtx);
  257 }
  258 
  259 static int
  260 g_disk_ioctl(struct g_provider *pp, u_long cmd, void * data, int fflag, struct thread *td)
  261 {
  262         struct disk *dp;
  263         struct g_disk_softc *sc;
  264         int error;
  265 
  266         sc = pp->private;
  267         dp = sc->dp;
  268 
  269         if (dp->d_ioctl == NULL)
  270                 return (ENOIOCTL);
  271         g_disk_lock_giant(dp);
  272         error = dp->d_ioctl(dp, cmd, data, fflag, td);
  273         g_disk_unlock_giant(dp);
  274         return (error);
  275 }
  276 
  277 static void
  278 g_disk_start(struct bio *bp)
  279 {
  280         struct bio *bp2, *bp3;
  281         struct disk *dp;
  282         struct g_disk_softc *sc;
  283         int error;
  284         off_t off;
  285 
  286         sc = bp->bio_to->private;
  287         if (sc == NULL || (dp = sc->dp) == NULL || dp->d_destroyed) {
  288                 g_io_deliver(bp, ENXIO);
  289                 return;
  290         }
  291         error = EJUSTRETURN;
  292         switch(bp->bio_cmd) {
  293         case BIO_DELETE:
  294                 if (!(dp->d_flags & DISKFLAG_CANDELETE)) {
  295                         error = EOPNOTSUPP;
  296                         break;
  297                 }
  298                 /* fall-through */
  299         case BIO_READ:
  300         case BIO_WRITE:
  301                 off = 0;
  302                 bp3 = NULL;
  303                 bp2 = g_clone_bio(bp);
  304                 if (bp2 == NULL) {
  305                         error = ENOMEM;
  306                         break;
  307                 }
  308                 do {
  309                         off_t d_maxsize;
  310 
  311                         d_maxsize = (bp->bio_cmd == BIO_DELETE &&
  312                             (dp->d_flags & DISKFLAG_LACKS_DELMAX) == 0) ?
  313                             dp->d_delmaxsize : dp->d_maxsize;
  314 
  315                         bp2->bio_offset += off;
  316                         bp2->bio_length -= off;
  317                         if ((bp->bio_flags & BIO_UNMAPPED) == 0) {
  318                                 bp2->bio_data += off;
  319                         } else {
  320                                 KASSERT((dp->d_flags & DISKFLAG_UNMAPPED_BIO)
  321                                     != 0,
  322                                     ("unmapped bio not supported by disk %s",
  323                                     dp->d_name));
  324                                 bp2->bio_ma += off / PAGE_SIZE;
  325                                 bp2->bio_ma_offset += off;
  326                                 bp2->bio_ma_offset %= PAGE_SIZE;
  327                                 bp2->bio_ma_n -= off / PAGE_SIZE;
  328                         }
  329                         if (bp2->bio_length > d_maxsize) {
  330                                 /*
  331                                  * XXX: If we have a stripesize we should really
  332                                  * use it here. Care should be taken in the delete
  333                                  * case if this is done as deletes can be very 
  334                                  * sensitive to size given how they are processed.
  335                                  */
  336                                 bp2->bio_length = d_maxsize;
  337                                 if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
  338                                         bp2->bio_ma_n = howmany(
  339                                             bp2->bio_ma_offset +
  340                                             bp2->bio_length, PAGE_SIZE);
  341                                 }
  342                                 off += d_maxsize;
  343                                 /*
  344                                  * To avoid a race, we need to grab the next bio
  345                                  * before we schedule this one.  See "notes".
  346                                  */
  347                                 bp3 = g_clone_bio(bp);
  348                                 if (bp3 == NULL)
  349                                         bp->bio_error = ENOMEM;
  350                         }
  351                         bp2->bio_done = g_disk_done;
  352                         bp2->bio_pblkno = bp2->bio_offset / dp->d_sectorsize;
  353                         bp2->bio_bcount = bp2->bio_length;
  354                         bp2->bio_disk = dp;
  355                         devstat_start_transaction_bio(dp->d_devstat, bp2);
  356                         g_disk_lock_giant(dp);
  357                         dp->d_strategy(bp2);
  358                         g_disk_unlock_giant(dp);
  359                         bp2 = bp3;
  360                         bp3 = NULL;
  361                 } while (bp2 != NULL);
  362                 break;
  363         case BIO_GETATTR:
  364                 /* Give the driver a chance to override */
  365                 if (dp->d_getattr != NULL) {
  366                         if (bp->bio_disk == NULL)
  367                                 bp->bio_disk = dp;
  368                         error = dp->d_getattr(bp);
  369                         if (error != -1)
  370                                 break;
  371                         error = EJUSTRETURN;
  372                 }
  373                 if (g_handleattr_int(bp, "GEOM::candelete",
  374                     (dp->d_flags & DISKFLAG_CANDELETE) != 0))
  375                         break;
  376                 else if (g_handleattr_int(bp, "GEOM::fwsectors",
  377                     dp->d_fwsectors))
  378                         break;
  379                 else if (g_handleattr_int(bp, "GEOM::fwheads", dp->d_fwheads))
  380                         break;
  381                 else if (g_handleattr_off_t(bp, "GEOM::frontstuff", 0))
  382                         break;
  383                 else if (g_handleattr_str(bp, "GEOM::ident", dp->d_ident))
  384                         break;
  385                 else if (g_handleattr(bp, "GEOM::hba_vendor",
  386                     &dp->d_hba_vendor, 2))
  387                         break;
  388                 else if (g_handleattr(bp, "GEOM::hba_device",
  389                     &dp->d_hba_device, 2))
  390                         break;
  391                 else if (g_handleattr(bp, "GEOM::hba_subvendor",
  392                     &dp->d_hba_subvendor, 2))
  393                         break;
  394                 else if (g_handleattr(bp, "GEOM::hba_subdevice",
  395                     &dp->d_hba_subdevice, 2))
  396                         break;
  397                 else if (!strcmp(bp->bio_attribute, "GEOM::kerneldump"))
  398                         g_disk_kerneldump(bp, dp);
  399                 else if (!strcmp(bp->bio_attribute, "GEOM::setstate"))
  400                         g_disk_setstate(bp, sc);
  401                 else 
  402                         error = ENOIOCTL;
  403                 break;
  404         case BIO_FLUSH:
  405                 g_trace(G_T_BIO, "g_disk_flushcache(%s)",
  406                     bp->bio_to->name);
  407                 if (!(dp->d_flags & DISKFLAG_CANFLUSHCACHE)) {
  408                         error = EOPNOTSUPP;
  409                         break;
  410                 }
  411                 bp2 = g_clone_bio(bp);
  412                 if (bp2 == NULL) {
  413                         g_io_deliver(bp, ENOMEM);
  414                         return;
  415                 }
  416                 bp2->bio_done = g_disk_done;
  417                 bp2->bio_disk = dp;
  418                 g_disk_lock_giant(dp);
  419                 dp->d_strategy(bp2);
  420                 g_disk_unlock_giant(dp);
  421                 break;
  422         default:
  423                 error = EOPNOTSUPP;
  424                 break;
  425         }
  426         if (error != EJUSTRETURN)
  427                 g_io_deliver(bp, error);
  428         return;
  429 }
  430 
  431 static void
  432 g_disk_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp)
  433 {
  434         struct bio *bp;
  435         struct disk *dp;
  436         struct g_disk_softc *sc;
  437         char *buf;
  438         int res = 0;
  439 
  440         sc = gp->softc;
  441         if (sc == NULL || (dp = sc->dp) == NULL)
  442                 return;
  443         if (indent == NULL) {
  444                 sbuf_printf(sb, " hd %u", dp->d_fwheads);
  445                 sbuf_printf(sb, " sc %u", dp->d_fwsectors);
  446                 return;
  447         }
  448         if (pp != NULL) {
  449                 sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n",
  450                     indent, dp->d_fwheads);
  451                 sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n",
  452                     indent, dp->d_fwsectors);
  453                 if (dp->d_getattr != NULL) {
  454                         buf = g_malloc(DISK_IDENT_SIZE, M_WAITOK);
  455                         bp = g_alloc_bio();
  456                         bp->bio_disk = dp;
  457                         bp->bio_attribute = "GEOM::ident";
  458                         bp->bio_length = DISK_IDENT_SIZE;
  459                         bp->bio_data = buf;
  460                         res = dp->d_getattr(bp);
  461                         sbuf_printf(sb, "%s<ident>%s</ident>\n", indent,
  462                             res == 0 ? buf: dp->d_ident);
  463                         bp->bio_attribute = "GEOM::lunid";
  464                         bp->bio_length = DISK_IDENT_SIZE;
  465                         bp->bio_data = buf;
  466                         if (dp->d_getattr(bp) == 0)
  467                                 sbuf_printf(sb, "%s<lunid>%s</lunid>\n",
  468                                     indent, buf);
  469                         g_destroy_bio(bp);
  470                         g_free(buf);
  471                 } else
  472                         sbuf_printf(sb, "%s<ident>%s</ident>\n", indent,
  473                             dp->d_ident);
  474                 sbuf_printf(sb, "%s<descr>%s</descr>\n", indent, dp->d_descr);
  475         }
  476 }
  477 
  478 static void
  479 g_disk_create(void *arg, int flag)
  480 {
  481         struct g_geom *gp;
  482         struct g_provider *pp;
  483         struct disk *dp;
  484         struct g_disk_softc *sc;
  485         char tmpstr[80];
  486 
  487         if (flag == EV_CANCEL)
  488                 return;
  489         g_topology_assert();
  490         dp = arg;
  491         sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
  492         mtx_init(&sc->done_mtx, "g_disk_done", NULL, MTX_DEF);
  493         sc->dp = dp;
  494         gp = g_new_geomf(&g_disk_class, "%s%d", dp->d_name, dp->d_unit);
  495         gp->softc = sc;
  496         pp = g_new_providerf(gp, "%s", gp->name);
  497         pp->mediasize = dp->d_mediasize;
  498         pp->sectorsize = dp->d_sectorsize;
  499         if (dp->d_flags & DISKFLAG_CANDELETE)
  500                 pp->flags |= G_PF_CANDELETE;
  501         pp->stripeoffset = dp->d_stripeoffset;
  502         pp->stripesize = dp->d_stripesize;
  503         if ((dp->d_flags & DISKFLAG_UNMAPPED_BIO) != 0)
  504                 pp->flags |= G_PF_ACCEPT_UNMAPPED;
  505         if (bootverbose)
  506                 printf("GEOM: new disk %s\n", gp->name);
  507         sysctl_ctx_init(&sc->sysctl_ctx);
  508         snprintf(tmpstr, sizeof(tmpstr), "GEOM disk %s", gp->name);
  509         sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
  510                 SYSCTL_STATIC_CHILDREN(_kern_geom_disk), OID_AUTO, gp->name,
  511                 CTLFLAG_RD, 0, tmpstr);
  512         if (sc->sysctl_tree != NULL) {
  513                 snprintf(tmpstr, sizeof(tmpstr),
  514                     "kern.geom.disk.%s.led", gp->name);
  515                 TUNABLE_STR_FETCH(tmpstr, sc->led, sizeof(sc->led));
  516                 SYSCTL_ADD_STRING(&sc->sysctl_ctx,
  517                     SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO, "led",
  518                     CTLFLAG_RW | CTLFLAG_TUN, sc->led, sizeof(sc->led),
  519                     "LED name");
  520         }
  521         pp->private = sc;
  522         dp->d_geom = gp;
  523         g_error_provider(pp, 0);
  524 }
  525 
  526 /*
  527  * We get this callback after all of the consumers have gone away, and just
  528  * before the provider is freed.  If the disk driver provided a d_gone
  529  * callback, let them know that it is okay to free resources -- they won't
  530  * be getting any more accesses from GEOM.
  531  */
  532 static void
  533 g_disk_providergone(struct g_provider *pp)
  534 {
  535         struct disk *dp;
  536         struct g_disk_softc *sc;
  537 
  538         sc = (struct g_disk_softc *)pp->private;
  539         dp = sc->dp;
  540 
  541         /*
  542          * FreeBSD 9 started with VERSION_01 of the struct disk structure.
  543          * However, g_gone was added in the middle of the branch.  To
  544          * cope with version being missing from struct disk, we set a flag
  545          * in g_disk_create for VERSION_01 and avoid touching the d_gone
  546          * field for old consumers.
  547          */
  548         if (dp != NULL && (dp->d_flags & DISKFLAG_LACKS_GONE) == 0 &&
  549             dp->d_gone != NULL)
  550                 dp->d_gone(dp);
  551         if (sc->sysctl_tree != NULL) {
  552                 sysctl_ctx_free(&sc->sysctl_ctx);
  553                 sc->sysctl_tree = NULL;
  554         }
  555         if (sc->led[0] != 0) {
  556                 led_set(sc->led, "");
  557                 sc->led[0] = 0;
  558         }
  559         pp->private = NULL;
  560         pp->geom->softc = NULL;
  561         mtx_destroy(&sc->done_mtx);
  562         g_free(sc);
  563 }
  564 
  565 static void
  566 g_disk_destroy(void *ptr, int flag)
  567 {
  568         struct disk *dp;
  569         struct g_geom *gp;
  570         struct g_disk_softc *sc;
  571 
  572         g_topology_assert();
  573         dp = ptr;
  574         gp = dp->d_geom;
  575         if (gp != NULL) {
  576                 sc = gp->softc;
  577                 if (sc != NULL)
  578                         sc->dp = NULL;
  579                 dp->d_geom = NULL;
  580                 g_wither_geom(gp, ENXIO);
  581         }
  582         g_free(dp);
  583 }
  584 
  585 /*
  586  * We only allow printable characters in disk ident,
  587  * the rest is converted to 'x<HH>'.
  588  */
  589 static void
  590 g_disk_ident_adjust(char *ident, size_t size)
  591 {
  592         char *p, tmp[4], newid[DISK_IDENT_SIZE];
  593 
  594         newid[0] = '\0';
  595         for (p = ident; *p != '\0'; p++) {
  596                 if (isprint(*p)) {
  597                         tmp[0] = *p;
  598                         tmp[1] = '\0';
  599                 } else {
  600                         snprintf(tmp, sizeof(tmp), "x%02hhx",
  601                             *(unsigned char *)p);
  602                 }
  603                 if (strlcat(newid, tmp, sizeof(newid)) >= sizeof(newid))
  604                         break;
  605         }
  606         bzero(ident, size);
  607         strlcpy(ident, newid, size);
  608 }
  609 
  610 struct disk *
  611 disk_alloc(void)
  612 {
  613 
  614         return (g_malloc(sizeof(struct disk), M_WAITOK | M_ZERO));
  615 }
  616 
  617 void
  618 disk_create(struct disk *dp, int version)
  619 {
  620 
  621         if (version != DISK_VERSION_03 && version != DISK_VERSION_02 &&
  622             version != DISK_VERSION_01) {
  623                 printf("WARNING: Attempt to add disk %s%d %s",
  624                     dp->d_name, dp->d_unit,
  625                     " using incompatible ABI version of disk(9)\n");
  626                 printf("WARNING: Ignoring disk %s%d\n",
  627                     dp->d_name, dp->d_unit);
  628                 return;
  629         }
  630         if (version == DISK_VERSION_01)
  631                 dp->d_flags |= DISKFLAG_LACKS_GONE;
  632         if (version < DISK_VERSION_03)
  633                 dp->d_flags |= DISKFLAG_LACKS_DELMAX;
  634         KASSERT(dp->d_strategy != NULL, ("disk_create need d_strategy"));
  635         KASSERT(dp->d_name != NULL, ("disk_create need d_name"));
  636         KASSERT(*dp->d_name != 0, ("disk_create need d_name"));
  637         KASSERT(strlen(dp->d_name) < SPECNAMELEN - 4, ("disk name too long"));
  638         if (dp->d_devstat == NULL)
  639                 dp->d_devstat = devstat_new_entry(dp->d_name, dp->d_unit,
  640                     dp->d_sectorsize, DEVSTAT_ALL_SUPPORTED,
  641                     DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
  642         dp->d_geom = NULL;
  643         g_disk_ident_adjust(dp->d_ident, sizeof(dp->d_ident));
  644         g_post_event(g_disk_create, dp, M_WAITOK, dp, NULL);
  645 }
  646 
  647 void
  648 disk_destroy(struct disk *dp)
  649 {
  650 
  651         g_cancel_event(dp);
  652         dp->d_destroyed = 1;
  653         if (dp->d_devstat != NULL)
  654                 devstat_remove_entry(dp->d_devstat);
  655         g_post_event(g_disk_destroy, dp, M_WAITOK, NULL);
  656 }
  657 
  658 void
  659 disk_gone(struct disk *dp)
  660 {
  661         struct g_geom *gp;
  662         struct g_provider *pp;
  663 
  664         gp = dp->d_geom;
  665         if (gp != NULL) {
  666                 pp = LIST_FIRST(&gp->provider);
  667                 if (pp != NULL) {
  668                         KASSERT(LIST_NEXT(pp, provider) == NULL,
  669                             ("geom %p has more than one provider", gp));
  670                         g_wither_provider(pp, ENXIO);
  671                 }
  672         }
  673 }
  674 
  675 void
  676 disk_attr_changed(struct disk *dp, const char *attr, int flag)
  677 {
  678         struct g_geom *gp;
  679         struct g_provider *pp;
  680 
  681         gp = dp->d_geom;
  682         if (gp != NULL)
  683                 LIST_FOREACH(pp, &gp->provider, provider)
  684                         (void)g_attr_changed(pp, attr, flag);
  685 }
  686 
  687 void
  688 disk_media_changed(struct disk *dp, int flag)
  689 {
  690         struct g_geom *gp;
  691         struct g_provider *pp;
  692 
  693         gp = dp->d_geom;
  694         if (gp != NULL) {
  695                 pp = LIST_FIRST(&gp->provider);
  696                 if (pp != NULL) {
  697                         KASSERT(LIST_NEXT(pp, provider) == NULL,
  698                             ("geom %p has more than one provider", gp));
  699                         g_media_changed(pp, flag);
  700                 }
  701         }
  702 }
  703 
  704 void
  705 disk_media_gone(struct disk *dp, int flag)
  706 {
  707         struct g_geom *gp;
  708         struct g_provider *pp;
  709 
  710         gp = dp->d_geom;
  711         if (gp != NULL) {
  712                 pp = LIST_FIRST(&gp->provider);
  713                 if (pp != NULL) {
  714                         KASSERT(LIST_NEXT(pp, provider) == NULL,
  715                             ("geom %p has more than one provider", gp));
  716                         g_media_gone(pp, flag);
  717                 }
  718         }
  719 }
  720 
  721 static void
  722 g_kern_disks(void *p, int flag __unused)
  723 {
  724         struct sbuf *sb;
  725         struct g_geom *gp;
  726         char *sp;
  727 
  728         sb = p;
  729         sp = "";
  730         g_topology_assert();
  731         LIST_FOREACH(gp, &g_disk_class.geom, geom) {
  732                 sbuf_printf(sb, "%s%s", sp, gp->name);
  733                 sp = " ";
  734         }
  735         sbuf_finish(sb);
  736 }
  737 
  738 static int
  739 sysctl_disks(SYSCTL_HANDLER_ARGS)
  740 {
  741         int error;
  742         struct sbuf *sb;
  743 
  744         sb = sbuf_new_auto();
  745         g_waitfor_event(g_kern_disks, sb, M_WAITOK, NULL);
  746         error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
  747         sbuf_delete(sb);
  748         return error;
  749 }
  750  
  751 SYSCTL_PROC(_kern, OID_AUTO, disks,
  752     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
  753     sysctl_disks, "A", "names of available disks");

Cache object: c8fd7da91b58b03397d781e1d6e13cc2


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