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/5.3/sys/geom/geom_disk.c 136588 2004-10-16 08:43:07Z cvs2svn $");
   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/conf.h>
   47 #include <sys/fcntl.h>
   48 #include <sys/malloc.h>
   49 #include <sys/sysctl.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 static struct mtx g_disk_done_mtx;
   60 
   61 static g_access_t g_disk_access;
   62 static g_init_t g_disk_init;
   63 static g_fini_t g_disk_fini;
   64 static g_start_t g_disk_start;
   65 static g_ioctl_t g_disk_ioctl;
   66 static g_dumpconf_t g_disk_dumpconf;
   67 
   68 struct g_class g_disk_class = {
   69         .name = "DISK",
   70         .version = G_VERSION,
   71         .init = g_disk_init,
   72         .fini = g_disk_fini,
   73         .start = g_disk_start,
   74         .access = g_disk_access,
   75         .ioctl = g_disk_ioctl,
   76         .dumpconf = g_disk_dumpconf,
   77 };
   78 
   79 static void
   80 g_disk_init(struct g_class *mp __unused)
   81 {
   82 
   83         mtx_init(&g_disk_done_mtx, "g_disk_done", NULL, MTX_DEF);
   84 }
   85 
   86 static void
   87 g_disk_fini(struct g_class *mp __unused)
   88 {
   89 
   90         mtx_destroy(&g_disk_done_mtx);
   91 }
   92 
   93 DECLARE_GEOM_CLASS(g_disk_class, g_disk);
   94 
   95 static void __inline
   96 g_disk_lock_giant(struct disk *dp)
   97 {
   98         if (dp->d_flags & DISKFLAG_NEEDSGIANT)
   99                 mtx_lock(&Giant);
  100 }
  101 
  102 static void __inline
  103 g_disk_unlock_giant(struct disk *dp)
  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         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         dp = pp->geom->softc;
  119         if (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 (error != 0)
  137                                 printf("Opened disk %s -> %d\n",
  138                                     pp->name, error);
  139                         g_disk_unlock_giant(dp);
  140                 }
  141                 pp->mediasize = dp->d_mediasize;
  142                 pp->sectorsize = dp->d_sectorsize;
  143                 dp->d_flags |= DISKFLAG_OPEN;
  144                 if (dp->d_maxsize == 0) {
  145                         printf("WARNING: Disk drive %s%d has no d_maxsize\n",
  146                             dp->d_name, dp->d_unit);
  147                         dp->d_maxsize = DFLTPHYS;
  148                 }
  149         } else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) {
  150                 if (dp->d_close != NULL) {
  151                         g_disk_lock_giant(dp);
  152                         error = dp->d_close(dp);
  153                         if (error != 0)
  154                                 printf("Closed disk %s -> %d\n",
  155                                     pp->name, error);
  156                         g_disk_unlock_giant(dp);
  157                 }
  158                 dp->d_flags &= ~DISKFLAG_OPEN;
  159         }
  160         return (error);
  161 }
  162 
  163 static void
  164 g_disk_kerneldump(struct bio *bp, struct disk *dp)
  165 { 
  166         int error;
  167         struct g_kerneldump *gkd;
  168         struct dumperinfo di;
  169         struct g_geom *gp;
  170 
  171         gkd = (struct g_kerneldump*)bp->bio_data;
  172         gp = bp->bio_to->geom;
  173         g_trace(G_T_TOPOLOGY, "g_disk_kernedump(%s, %jd, %jd)",
  174                 gp->name, (intmax_t)gkd->offset, (intmax_t)gkd->length);
  175         if (dp->d_dump == NULL) {
  176                 g_io_deliver(bp, ENODEV);
  177                 return;
  178         }
  179         di.dumper = dp->d_dump;
  180         di.priv = dp;
  181         di.blocksize = dp->d_sectorsize;
  182         di.mediaoffset = gkd->offset;
  183         di.mediasize = gkd->length;
  184         error = set_dumper(&di);
  185         g_io_deliver(bp, error);
  186 }
  187 
  188 static void
  189 g_disk_done(struct bio *bp)
  190 {
  191         struct bio *bp2;
  192         struct disk *dp;
  193 
  194         /* See "notes" for why we need a mutex here */
  195         /* XXX: will witness accept a mix of Giant/unGiant drivers here ? */
  196         mtx_lock(&g_disk_done_mtx);
  197         bp->bio_completed = bp->bio_length - bp->bio_resid;
  198 
  199         bp2 = bp->bio_parent;
  200         if (bp2->bio_error == 0)
  201                 bp2->bio_error = bp->bio_error;
  202         bp2->bio_completed += bp->bio_completed;
  203         g_destroy_bio(bp);
  204         bp2->bio_inbed++;
  205         if (bp2->bio_children == bp2->bio_inbed) {
  206                 bp2->bio_resid = bp2->bio_bcount - bp2->bio_completed;
  207                 if ((dp = bp2->bio_to->geom->softc))
  208                         devstat_end_transaction_bio(dp->d_devstat, bp2);
  209                 g_io_deliver(bp2, bp2->bio_error);
  210         }
  211         mtx_unlock(&g_disk_done_mtx);
  212 }
  213 
  214 static int
  215 g_disk_ioctl(struct g_provider *pp, u_long cmd, void * data, struct thread *td)
  216 {
  217         struct g_geom *gp;
  218         struct disk *dp;
  219         int error;
  220 
  221         gp = pp->geom;
  222         dp = gp->softc;
  223 
  224         if (dp->d_ioctl == NULL)
  225                 return (ENOIOCTL);
  226         g_disk_lock_giant(dp);
  227         error = dp->d_ioctl(dp, cmd, data, 0, td);
  228         g_disk_unlock_giant(dp);
  229         return(error);
  230 }
  231 
  232 static void
  233 g_disk_start(struct bio *bp)
  234 {
  235         struct bio *bp2, *bp3;
  236         struct disk *dp;
  237         int error;
  238         off_t off;
  239 
  240         dp = bp->bio_to->geom->softc;
  241         if (dp == NULL || dp->d_destroyed)
  242                 g_io_deliver(bp, ENXIO);
  243         error = EJUSTRETURN;
  244         switch(bp->bio_cmd) {
  245         case BIO_DELETE:
  246                 if (!(dp->d_flags & DISKFLAG_CANDELETE)) {
  247                         error = 0;
  248                         break;
  249                 }
  250                 /* fall-through */
  251         case BIO_READ:
  252         case BIO_WRITE:
  253                 off = 0;
  254                 bp3 = NULL;
  255                 bp2 = g_clone_bio(bp);
  256                 if (bp2 == NULL) {
  257                         error = ENOMEM;
  258                         break;
  259                 }
  260                 devstat_start_transaction_bio(dp->d_devstat, bp);
  261                 do {
  262                         bp2->bio_offset += off;
  263                         bp2->bio_length -= off;
  264                         bp2->bio_data += off;
  265                         if (bp2->bio_length > dp->d_maxsize) {
  266                                 /*
  267                                  * XXX: If we have a stripesize we should really
  268                                  * use it here.
  269                                  */
  270                                 bp2->bio_length = dp->d_maxsize;
  271                                 off += dp->d_maxsize;
  272                                 /*
  273                                  * To avoid a race, we need to grab the next bio
  274                                  * before we schedule this one.  See "notes".
  275                                  */
  276                                 bp3 = g_clone_bio(bp);
  277                                 if (bp3 == NULL)
  278                                         bp->bio_error = ENOMEM;
  279                         }
  280                         bp2->bio_done = g_disk_done;
  281                         bp2->bio_pblkno = bp2->bio_offset / dp->d_sectorsize;
  282                         bp2->bio_bcount = bp2->bio_length;
  283                         bp2->bio_disk = dp;
  284                         g_disk_lock_giant(dp);
  285                         dp->d_strategy(bp2);
  286                         g_disk_unlock_giant(dp);
  287                         bp2 = bp3;
  288                         bp3 = NULL;
  289                 } while (bp2 != NULL);
  290                 break;
  291         case BIO_GETATTR:
  292                 if (g_handleattr_int(bp, "GEOM::fwsectors", dp->d_fwsectors))
  293                         break;
  294                 else if (g_handleattr_int(bp, "GEOM::fwheads", dp->d_fwheads))
  295                         break;
  296                 else if (g_handleattr_off_t(bp, "GEOM::frontstuff", 0))
  297                         break;
  298                 else if (!strcmp(bp->bio_attribute, "GEOM::kerneldump"))
  299                         g_disk_kerneldump(bp, dp);
  300                 else 
  301                         error = ENOIOCTL;
  302                 break;
  303         default:
  304                 error = EOPNOTSUPP;
  305                 break;
  306         }
  307         if (error != EJUSTRETURN)
  308                 g_io_deliver(bp, error);
  309         return;
  310 }
  311 
  312 static void
  313 g_disk_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp)
  314 {
  315         struct disk *dp;
  316 
  317         dp = gp->softc;
  318         if (dp == NULL)
  319                 return;
  320         if (indent == NULL) {
  321                 sbuf_printf(sb, " hd %u", dp->d_fwheads);
  322                 sbuf_printf(sb, " sc %u", dp->d_fwsectors);
  323                 return;
  324         }
  325         if (pp != NULL) {
  326                 sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n",
  327                     indent, dp->d_fwheads);
  328                 sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n",
  329                     indent, dp->d_fwsectors);
  330         }
  331 }
  332 
  333 static void
  334 g_disk_create(void *arg, int flag)
  335 {
  336         struct g_geom *gp;
  337         struct g_provider *pp;
  338         struct disk *dp;
  339 
  340         if (flag == EV_CANCEL)
  341                 return;
  342         g_topology_assert();
  343         dp = arg;
  344         gp = g_new_geomf(&g_disk_class, "%s%d", dp->d_name, dp->d_unit);
  345         gp->softc = dp;
  346         pp = g_new_providerf(gp, "%s", gp->name);
  347         pp->mediasize = dp->d_mediasize;
  348         pp->sectorsize = dp->d_sectorsize;
  349         if (dp->d_flags & DISKFLAG_CANDELETE)
  350                 pp->flags |= G_PF_CANDELETE;
  351         pp->stripeoffset = dp->d_stripeoffset;
  352         pp->stripesize = dp->d_stripesize;
  353         if (bootverbose)
  354                 printf("GEOM: new disk %s\n", gp->name);
  355         dp->d_geom = gp;
  356         g_error_provider(pp, 0);
  357 }
  358 
  359 static void
  360 g_disk_destroy(void *ptr, int flag)
  361 {
  362         struct disk *dp;
  363         struct g_geom *gp;
  364 
  365         g_topology_assert();
  366         dp = ptr;
  367         gp = dp->d_geom;
  368         gp->softc = NULL;
  369         g_wither_geom(gp, ENXIO);
  370         g_free(dp);
  371 }
  372 
  373 struct disk *
  374 disk_alloc()
  375 {
  376         struct disk *dp;
  377 
  378         dp = g_malloc(sizeof *dp, M_WAITOK | M_ZERO);
  379         return (dp);
  380 }
  381 
  382 void
  383 disk_create(struct disk *dp, int version)
  384 {
  385         if (version != DISK_VERSION_00) {
  386                 printf("WARNING: Attempt to add disk %s%d %s",
  387                     dp->d_name, dp->d_unit,
  388                     " using incompatible ABI version of disk(9)\n");
  389                 printf("WARNING: Ignoring disk %s%d\n",
  390                     dp->d_name, dp->d_unit);
  391                 return;
  392         }
  393         KASSERT(dp->d_strategy != NULL, ("disk_create need d_strategy"));
  394         KASSERT(dp->d_name != NULL, ("disk_create need d_name"));
  395         KASSERT(*dp->d_name != 0, ("disk_create need d_name"));
  396         KASSERT(strlen(dp->d_name) < SPECNAMELEN - 4, ("disk name too long"));
  397         if (dp->d_devstat == NULL)
  398                 dp->d_devstat = devstat_new_entry(dp->d_name, dp->d_unit,
  399                     dp->d_sectorsize, DEVSTAT_ALL_SUPPORTED,
  400                     DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
  401         dp->d_geom = NULL;
  402         g_post_event(g_disk_create, dp, M_WAITOK, dp, NULL);
  403 }
  404 
  405 void
  406 disk_destroy(struct disk *dp)
  407 {
  408 
  409         g_cancel_event(dp);
  410         dp->d_destroyed = 1;
  411         if (dp->d_devstat != NULL)
  412                 devstat_remove_entry(dp->d_devstat);
  413         g_post_event(g_disk_destroy, dp, M_WAITOK, NULL);
  414 }
  415 
  416 static void
  417 g_kern_disks(void *p, int flag __unused)
  418 {
  419         struct sbuf *sb;
  420         struct g_geom *gp;
  421         char *sp;
  422 
  423         sb = p;
  424         sp = "";
  425         g_topology_assert();
  426         LIST_FOREACH(gp, &g_disk_class.geom, geom) {
  427                 sbuf_printf(sb, "%s%s", sp, gp->name);
  428                 sp = " ";
  429         }
  430         sbuf_finish(sb);
  431 }
  432 
  433 static int
  434 sysctl_disks(SYSCTL_HANDLER_ARGS)
  435 {
  436         int error;
  437         struct sbuf *sb;
  438 
  439         sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
  440         g_waitfor_event(g_kern_disks, sb, M_WAITOK, NULL);
  441         error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
  442         sbuf_delete(sb);
  443         return error;
  444 }
  445  
  446 SYSCTL_PROC(_kern, OID_AUTO, disks, CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NOLOCK, 0, 0, 
  447     sysctl_disks, "A", "names of available disks");
  448 

Cache object: ce83ec25a553af533632452aeafed64b


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