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-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
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/6.0/sys/geom/geom_disk.c 143791 2005-03-18 07:01:31Z phk $");
   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 static 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         if ((gkd->offset + gkd->length) > dp->d_mediasize)
  184                 gkd->length = dp->d_mediasize - gkd->offset;
  185         di.mediasize = gkd->length;
  186         error = set_dumper(&di);
  187         g_io_deliver(bp, error);
  188 }
  189 
  190 static void
  191 g_disk_done(struct bio *bp)
  192 {
  193         struct bio *bp2;
  194         struct disk *dp;
  195 
  196         /* See "notes" for why we need a mutex here */
  197         /* XXX: will witness accept a mix of Giant/unGiant drivers here ? */
  198         mtx_lock(&g_disk_done_mtx);
  199         bp->bio_completed = bp->bio_length - bp->bio_resid;
  200 
  201         bp2 = bp->bio_parent;
  202         if (bp2->bio_error == 0)
  203                 bp2->bio_error = bp->bio_error;
  204         bp2->bio_completed += bp->bio_completed;
  205         g_destroy_bio(bp);
  206         bp2->bio_inbed++;
  207         if (bp2->bio_children == bp2->bio_inbed) {
  208                 bp2->bio_resid = bp2->bio_bcount - bp2->bio_completed;
  209                 if ((dp = bp2->bio_to->geom->softc))
  210                         devstat_end_transaction_bio(dp->d_devstat, bp2);
  211                 g_io_deliver(bp2, bp2->bio_error);
  212         }
  213         mtx_unlock(&g_disk_done_mtx);
  214 }
  215 
  216 static int
  217 g_disk_ioctl(struct g_provider *pp, u_long cmd, void * data, int fflag, struct thread *td)
  218 {
  219         struct g_geom *gp;
  220         struct disk *dp;
  221         int error;
  222 
  223         gp = pp->geom;
  224         dp = gp->softc;
  225 
  226         if (dp->d_ioctl == NULL)
  227                 return (ENOIOCTL);
  228         g_disk_lock_giant(dp);
  229         error = dp->d_ioctl(dp, cmd, data, fflag, td);
  230         g_disk_unlock_giant(dp);
  231         return(error);
  232 }
  233 
  234 static void
  235 g_disk_start(struct bio *bp)
  236 {
  237         struct bio *bp2, *bp3;
  238         struct disk *dp;
  239         int error;
  240         off_t off;
  241 
  242         dp = bp->bio_to->geom->softc;
  243         if (dp == NULL || dp->d_destroyed) {
  244                 g_io_deliver(bp, ENXIO);
  245                 return;
  246         }
  247         error = EJUSTRETURN;
  248         switch(bp->bio_cmd) {
  249         case BIO_DELETE:
  250                 if (!(dp->d_flags & DISKFLAG_CANDELETE)) {
  251                         error = 0;
  252                         break;
  253                 }
  254                 /* fall-through */
  255         case BIO_READ:
  256         case BIO_WRITE:
  257                 off = 0;
  258                 bp3 = NULL;
  259                 bp2 = g_clone_bio(bp);
  260                 if (bp2 == NULL) {
  261                         error = ENOMEM;
  262                         break;
  263                 }
  264                 devstat_start_transaction_bio(dp->d_devstat, bp);
  265                 do {
  266                         bp2->bio_offset += off;
  267                         bp2->bio_length -= off;
  268                         bp2->bio_data += off;
  269                         if (bp2->bio_length > dp->d_maxsize) {
  270                                 /*
  271                                  * XXX: If we have a stripesize we should really
  272                                  * use it here.
  273                                  */
  274                                 bp2->bio_length = dp->d_maxsize;
  275                                 off += dp->d_maxsize;
  276                                 /*
  277                                  * To avoid a race, we need to grab the next bio
  278                                  * before we schedule this one.  See "notes".
  279                                  */
  280                                 bp3 = g_clone_bio(bp);
  281                                 if (bp3 == NULL)
  282                                         bp->bio_error = ENOMEM;
  283                         }
  284                         bp2->bio_done = g_disk_done;
  285                         bp2->bio_pblkno = bp2->bio_offset / dp->d_sectorsize;
  286                         bp2->bio_bcount = bp2->bio_length;
  287                         bp2->bio_disk = dp;
  288                         g_disk_lock_giant(dp);
  289                         dp->d_strategy(bp2);
  290                         g_disk_unlock_giant(dp);
  291                         bp2 = bp3;
  292                         bp3 = NULL;
  293                 } while (bp2 != NULL);
  294                 break;
  295         case BIO_GETATTR:
  296                 if (g_handleattr_int(bp, "GEOM::fwsectors", dp->d_fwsectors))
  297                         break;
  298                 else if (g_handleattr_int(bp, "GEOM::fwheads", dp->d_fwheads))
  299                         break;
  300                 else if (g_handleattr_off_t(bp, "GEOM::frontstuff", 0))
  301                         break;
  302                 else if (!strcmp(bp->bio_attribute, "GEOM::kerneldump"))
  303                         g_disk_kerneldump(bp, dp);
  304                 else 
  305                         error = ENOIOCTL;
  306                 break;
  307         default:
  308                 error = EOPNOTSUPP;
  309                 break;
  310         }
  311         if (error != EJUSTRETURN)
  312                 g_io_deliver(bp, error);
  313         return;
  314 }
  315 
  316 static void
  317 g_disk_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp)
  318 {
  319         struct disk *dp;
  320 
  321         dp = gp->softc;
  322         if (dp == NULL)
  323                 return;
  324         if (indent == NULL) {
  325                 sbuf_printf(sb, " hd %u", dp->d_fwheads);
  326                 sbuf_printf(sb, " sc %u", dp->d_fwsectors);
  327                 return;
  328         }
  329         if (pp != NULL) {
  330                 sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n",
  331                     indent, dp->d_fwheads);
  332                 sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n",
  333                     indent, dp->d_fwsectors);
  334         }
  335 }
  336 
  337 static void
  338 g_disk_create(void *arg, int flag)
  339 {
  340         struct g_geom *gp;
  341         struct g_provider *pp;
  342         struct disk *dp;
  343 
  344         if (flag == EV_CANCEL)
  345                 return;
  346         g_topology_assert();
  347         dp = arg;
  348         gp = g_new_geomf(&g_disk_class, "%s%d", dp->d_name, dp->d_unit);
  349         gp->softc = dp;
  350         pp = g_new_providerf(gp, "%s", gp->name);
  351         pp->mediasize = dp->d_mediasize;
  352         pp->sectorsize = dp->d_sectorsize;
  353         if (dp->d_flags & DISKFLAG_CANDELETE)
  354                 pp->flags |= G_PF_CANDELETE;
  355         pp->stripeoffset = dp->d_stripeoffset;
  356         pp->stripesize = dp->d_stripesize;
  357         if (bootverbose)
  358                 printf("GEOM: new disk %s\n", gp->name);
  359         dp->d_geom = gp;
  360         g_error_provider(pp, 0);
  361 }
  362 
  363 static void
  364 g_disk_destroy(void *ptr, int flag)
  365 {
  366         struct disk *dp;
  367         struct g_geom *gp;
  368 
  369         g_topology_assert();
  370         dp = ptr;
  371         gp = dp->d_geom;
  372         if (gp != NULL) {
  373                 gp->softc = NULL;
  374                 g_wither_geom(gp, ENXIO);
  375         }
  376         g_free(dp);
  377 }
  378 
  379 struct disk *
  380 disk_alloc()
  381 {
  382         struct disk *dp;
  383 
  384         dp = g_malloc(sizeof *dp, M_WAITOK | M_ZERO);
  385         return (dp);
  386 }
  387 
  388 void
  389 disk_create(struct disk *dp, int version)
  390 {
  391         if (version != DISK_VERSION_00) {
  392                 printf("WARNING: Attempt to add disk %s%d %s",
  393                     dp->d_name, dp->d_unit,
  394                     " using incompatible ABI version of disk(9)\n");
  395                 printf("WARNING: Ignoring disk %s%d\n",
  396                     dp->d_name, dp->d_unit);
  397                 return;
  398         }
  399         KASSERT(dp->d_strategy != NULL, ("disk_create need d_strategy"));
  400         KASSERT(dp->d_name != NULL, ("disk_create need d_name"));
  401         KASSERT(*dp->d_name != 0, ("disk_create need d_name"));
  402         KASSERT(strlen(dp->d_name) < SPECNAMELEN - 4, ("disk name too long"));
  403         if (dp->d_devstat == NULL)
  404                 dp->d_devstat = devstat_new_entry(dp->d_name, dp->d_unit,
  405                     dp->d_sectorsize, DEVSTAT_ALL_SUPPORTED,
  406                     DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
  407         dp->d_geom = NULL;
  408         g_post_event(g_disk_create, dp, M_WAITOK, dp, NULL);
  409 }
  410 
  411 void
  412 disk_destroy(struct disk *dp)
  413 {
  414 
  415         g_cancel_event(dp);
  416         dp->d_destroyed = 1;
  417         if (dp->d_devstat != NULL)
  418                 devstat_remove_entry(dp->d_devstat);
  419         g_post_event(g_disk_destroy, dp, M_WAITOK, NULL);
  420 }
  421 
  422 static void
  423 g_kern_disks(void *p, int flag __unused)
  424 {
  425         struct sbuf *sb;
  426         struct g_geom *gp;
  427         char *sp;
  428 
  429         sb = p;
  430         sp = "";
  431         g_topology_assert();
  432         LIST_FOREACH(gp, &g_disk_class.geom, geom) {
  433                 sbuf_printf(sb, "%s%s", sp, gp->name);
  434                 sp = " ";
  435         }
  436         sbuf_finish(sb);
  437 }
  438 
  439 static int
  440 sysctl_disks(SYSCTL_HANDLER_ARGS)
  441 {
  442         int error;
  443         struct sbuf *sb;
  444 
  445         sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
  446         g_waitfor_event(g_kern_disks, sb, M_WAITOK, NULL);
  447         error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
  448         sbuf_delete(sb);
  449         return error;
  450 }
  451  
  452 SYSCTL_PROC(_kern, OID_AUTO, disks, CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NOLOCK, 0, 0, 
  453     sysctl_disks, "A", "names of available disks");
  454 

Cache object: a9b556db93ab8b080f48deb436c42c19


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