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

Cache object: 33a668c69f3602ed68045d40c8c50a24


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