geom_dev.c revision 116196
192108Sphk/*- 292108Sphk * Copyright (c) 2002 Poul-Henning Kamp 392108Sphk * Copyright (c) 2002 Networks Associates Technology, Inc. 492108Sphk * All rights reserved. 592108Sphk * 692108Sphk * This software was developed for the FreeBSD Project by Poul-Henning Kamp 792108Sphk * and NAI Labs, the Security Research Division of Network Associates, Inc. 892108Sphk * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the 992108Sphk * DARPA CHATS research program. 1092108Sphk * 1192108Sphk * Redistribution and use in source and binary forms, with or without 1292108Sphk * modification, are permitted provided that the following conditions 1392108Sphk * are met: 1492108Sphk * 1. Redistributions of source code must retain the above copyright 1592108Sphk * notice, this list of conditions and the following disclaimer. 1692108Sphk * 2. Redistributions in binary form must reproduce the above copyright 1792108Sphk * notice, this list of conditions and the following disclaimer in the 1892108Sphk * documentation and/or other materials provided with the distribution. 1992108Sphk * 3. The names of the authors may not be used to endorse or promote 2092108Sphk * products derived from this software without specific prior written 2192108Sphk * permission. 2292108Sphk * 2392108Sphk * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 2492108Sphk * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2592108Sphk * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2692108Sphk * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2792108Sphk * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2892108Sphk * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2992108Sphk * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3092108Sphk * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3192108Sphk * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3292108Sphk * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3392108Sphk * SUCH DAMAGE. 3492108Sphk */ 3592108Sphk 36116196Sobrien#include <sys/cdefs.h> 37116196Sobrien__FBSDID("$FreeBSD: head/sys/geom/geom_dev.c 116196 2003-06-11 06:49:16Z obrien $"); 38116196Sobrien 3992108Sphk#include <sys/param.h> 4092108Sphk#include <sys/systm.h> 4192108Sphk#include <sys/malloc.h> 4292108Sphk#include <sys/kernel.h> 4392108Sphk#include <sys/conf.h> 4492108Sphk#include <sys/bio.h> 4592108Sphk#include <sys/lock.h> 4692108Sphk#include <sys/mutex.h> 4792108Sphk#include <sys/errno.h> 4892108Sphk#include <sys/time.h> 4992108Sphk#include <sys/disk.h> 5092108Sphk#include <sys/fcntl.h> 51114216Skan#include <sys/limits.h> 5292108Sphk#include <geom/geom.h> 5395323Sphk#include <geom/geom_int.h> 5492108Sphk 5592108Sphkstatic d_open_t g_dev_open; 5692108Sphkstatic d_close_t g_dev_close; 5792108Sphkstatic d_strategy_t g_dev_strategy; 5892108Sphkstatic d_ioctl_t g_dev_ioctl; 5992108Sphk 6092108Sphkstatic struct cdevsw g_dev_cdevsw = { 61111815Sphk .d_open = g_dev_open, 62111815Sphk .d_close = g_dev_close, 63111815Sphk .d_read = physread, 64111815Sphk .d_write = physwrite, 65111815Sphk .d_ioctl = g_dev_ioctl, 66111815Sphk .d_strategy = g_dev_strategy, 67111815Sphk .d_name = "g_dev", 68111815Sphk .d_maj = GEOM_MAJOR, 69111815Sphk .d_flags = D_DISK | D_TRACKCLOSE, 7092108Sphk}; 7192108Sphk 7292108Sphkstatic g_taste_t g_dev_taste; 7392108Sphkstatic g_orphan_t g_dev_orphan; 7492108Sphk 7593248Sphkstatic struct g_class g_dev_class = { 76112552Sphk .name = "DEV", 77112552Sphk .taste = g_dev_taste, 7892108Sphk}; 7992108Sphk 80115960Sphkvoid 81105947Sphkg_dev_print(void) 82105947Sphk{ 83105947Sphk struct g_geom *gp; 84115960Sphk char const *p = ""; 85105947Sphk 86115960Sphk LIST_FOREACH(gp, &g_dev_class.geom, geom) { 87115960Sphk printf("%s%s", p, gp->name); 88115960Sphk p = " "; 89115960Sphk } 90105947Sphk printf("\n"); 91105947Sphk} 92105947Sphk 93108294Sphk/* 94108294Sphk * XXX: This is disgusting and wrong in every way imaginable: The only reason 95108294Sphk * XXX: we have a clone function is because of the root-mount hack we currently 96108294Sphk * XXX: employ. An improvment would be to unregister this cloner once we know 97108294Sphk * XXX: we no longer need it. Ideally, root-fs would be mounted through DEVFS 98108294Sphk * XXX: eliminating the need for this hack. 99108294Sphk */ 10092108Sphkstatic void 101104087Sphkg_dev_clone(void *arg __unused, char *name, int namelen __unused, dev_t *dev) 10292108Sphk{ 10392108Sphk struct g_geom *gp; 10492108Sphk 10592108Sphk if (*dev != NODEV) 10692108Sphk return; 10792108Sphk 10898066Sphk g_waitidle(); 10992108Sphk 11092108Sphk /* g_topology_lock(); */ 11193248Sphk LIST_FOREACH(gp, &g_dev_class.geom, geom) { 11292108Sphk if (strcmp(gp->name, name)) 11392108Sphk continue; 11492108Sphk *dev = gp->softc; 11592108Sphk g_trace(G_T_TOPOLOGY, "g_dev_clone(%s) = %p", name, *dev); 11692108Sphk return; 11792108Sphk } 11892108Sphk /* g_topology_unlock(); */ 11992108Sphk return; 12092108Sphk} 12192108Sphk 12292108Sphkstatic void 12392108Sphkg_dev_register_cloner(void *foo __unused) 12492108Sphk{ 12592108Sphk static int once; 12692108Sphk 12796987Sphk /* XXX: why would this happen more than once ?? */ 12892108Sphk if (!once) { 12996987Sphk EVENTHANDLER_REGISTER(dev_clone, g_dev_clone, 0, 1000); 13092108Sphk once++; 13192108Sphk } 13292108Sphk} 13392108Sphk 13492108SphkSYSINIT(geomdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE,g_dev_register_cloner,NULL); 13592108Sphk 13692108Sphkstatic struct g_geom * 13793250Sphkg_dev_taste(struct g_class *mp, struct g_provider *pp, int insist __unused) 13892108Sphk{ 13992108Sphk struct g_geom *gp; 14092108Sphk struct g_consumer *cp; 141110540Sphk static int unit = GEOM_MINOR_PROVIDERS; 14296987Sphk int error; 14392108Sphk dev_t dev; 14492108Sphk 14592108Sphk g_trace(G_T_TOPOLOGY, "dev_taste(%s,%s)", mp->name, pp->name); 14692108Sphk g_topology_assert(); 14792108Sphk LIST_FOREACH(cp, &pp->consumers, consumers) 14893248Sphk if (cp->geom->class == mp) 14992108Sphk return (NULL); 15092108Sphk gp = g_new_geomf(mp, pp->name); 15193776Sphk gp->orphan = g_dev_orphan; 15292108Sphk cp = g_new_consumer(gp); 15396987Sphk error = g_attach(cp, pp); 15496987Sphk KASSERT(error == 0, 15596987Sphk ("g_dev_taste(%s) failed to g_attach, err=%d", pp->name, error)); 15696987Sphk /* 15796987Sphk * XXX: I'm not 100% sure we can call make_dev(9) without Giant 15896987Sphk * yet. Once we can, we don't need to drop topology here either. 15996987Sphk */ 16092108Sphk g_topology_unlock(); 16192108Sphk mtx_lock(&Giant); 16296987Sphk dev = make_dev(&g_dev_cdevsw, unit2minor(unit++), 163104316Sphk UID_ROOT, GID_OPERATOR, 0640, gp->name); 164110700Sphk if (pp->flags & G_PF_CANDELETE) 165110700Sphk dev->si_flags |= SI_CANDELETE; 16696987Sphk mtx_unlock(&Giant); 16796987Sphk g_topology_lock(); 168110728Sphk dev->si_iosize_max = MAXPHYS; 169110710Sphk dev->si_stripesize = pp->stripesize; 170110710Sphk dev->si_stripeoffset = pp->stripeoffset; 17192108Sphk gp->softc = dev; 17292108Sphk dev->si_drv1 = gp; 17392108Sphk dev->si_drv2 = cp; 17492108Sphk return (gp); 17592108Sphk} 17692108Sphk 17792108Sphkstatic int 17892108Sphkg_dev_open(dev_t dev, int flags, int fmt, struct thread *td) 17992108Sphk{ 18092108Sphk struct g_geom *gp; 18192108Sphk struct g_consumer *cp; 18292108Sphk int error, r, w, e; 18392108Sphk 18492108Sphk gp = dev->si_drv1; 18592108Sphk cp = dev->si_drv2; 186112978Sphk if (gp == NULL || cp == NULL || gp->softc != dev) 187112978Sphk return(ENXIO); /* g_dev_taste() not done yet */ 188112978Sphk 18992108Sphk g_trace(G_T_ACCESS, "g_dev_open(%s, %d, %d, %p)", 19092108Sphk gp->name, flags, fmt, td); 19192108Sphk r = flags & FREAD ? 1 : 0; 19292108Sphk w = flags & FWRITE ? 1 : 0; 193103004Sphk#ifdef notyet 19492108Sphk e = flags & O_EXCL ? 1 : 0; 195103004Sphk#else 196103004Sphk e = 0; 197103004Sphk#endif 198112978Sphk DROP_GIANT(); 199112978Sphk g_topology_lock(); 200112978Sphk if (dev->si_devsw == NULL) 201112978Sphk error = ENXIO; /* We were orphaned */ 202112978Sphk else 203112978Sphk error = g_access_rel(cp, r, w, e); 20492108Sphk g_topology_unlock(); 20592479Sphk PICKUP_GIANT(); 20698066Sphk g_waitidle(); 207112978Sphk if (!error) 208112978Sphk dev->si_bsize_phys = cp->provider->sectorsize; 20992108Sphk return(error); 21092108Sphk} 21192108Sphk 21292108Sphkstatic int 21392108Sphkg_dev_close(dev_t dev, int flags, int fmt, struct thread *td) 21492108Sphk{ 21592108Sphk struct g_geom *gp; 21692108Sphk struct g_consumer *cp; 217114864Sphk int error, r, w, e, i; 21892108Sphk 21992108Sphk gp = dev->si_drv1; 22092108Sphk cp = dev->si_drv2; 22192108Sphk if (gp == NULL || cp == NULL) 22292108Sphk return(ENXIO); 22392108Sphk g_trace(G_T_ACCESS, "g_dev_close(%s, %d, %d, %p)", 22492108Sphk gp->name, flags, fmt, td); 22592108Sphk r = flags & FREAD ? -1 : 0; 22692108Sphk w = flags & FWRITE ? -1 : 0; 227103004Sphk#ifdef notyet 22892108Sphk e = flags & O_EXCL ? -1 : 0; 229103004Sphk#else 230103004Sphk e = 0; 231103004Sphk#endif 232112978Sphk DROP_GIANT(); 233112978Sphk g_topology_lock(); 234112978Sphk if (dev->si_devsw == NULL) 235112978Sphk error = ENXIO; /* We were orphaned */ 236112978Sphk else 237112978Sphk error = g_access_rel(cp, r, w, e); 238114864Sphk for (i = 0; i < 10 * hz;) { 239114864Sphk if (cp->acr != 0 || cp->acw != 0) 240114864Sphk break; 241114864Sphk if (cp->nstart == cp->nend) 242114864Sphk break; 243114864Sphk tsleep(&i, PRIBIO, "gdevwclose", hz / 10); 244114864Sphk i += hz / 10; 245114864Sphk } 246114864Sphk if (cp->acr == 0 && cp->acw == 0 && cp->nstart != cp->nend) { 247114864Sphk printf("WARNING: Final close of geom_dev(%s) %s %s", 248114864Sphk gp->name, 249114864Sphk "still has outstanding I/O after 10 seconds.", 250114864Sphk "Completing close anyway, panic may happen later."); 251114864Sphk } 25292108Sphk g_topology_unlock(); 25392479Sphk PICKUP_GIANT(); 25498066Sphk g_waitidle(); 25592108Sphk return (error); 25692108Sphk} 25792108Sphk 258112978Sphk/* 259112978Sphk * XXX: Until we have unmessed the ioctl situation, there is a race against 260112978Sphk * XXX: a concurrent orphanization. We cannot close it by holding topology 261112978Sphk * XXX: since that would prevent us from doing our job, and stalling events 262112978Sphk * XXX: will break (actually: stall) the BSD disklabel hacks. 263112978Sphk */ 26492108Sphkstatic int 26592108Sphkg_dev_ioctl(dev_t dev, u_long cmd, caddr_t data, int fflag, struct thread *td) 26692108Sphk{ 267115515Sphk struct g_geom *gp; 26892108Sphk struct g_consumer *cp; 26995038Sphk struct g_kerneldump kd; 27092108Sphk int i, error; 27195038Sphk u_int u; 27292403Sphk struct g_ioctl *gio; 27392108Sphk 27492108Sphk gp = dev->si_drv1; 27592108Sphk cp = dev->si_drv2; 276104602Sphk gio = NULL; 27792108Sphk 27892108Sphk error = 0; 279112978Sphk KASSERT(cp->acr || cp->acw, 280112978Sphk ("Consumer with zero access count in g_dev_ioctl")); 28192479Sphk DROP_GIANT(); 28292403Sphk 283114511Sphk gio = NULL; 28492698Sphk i = IOCPARM_LEN(cmd); 28592698Sphk switch (cmd) { 28692698Sphk case DIOCGSECTORSIZE: 287105551Sphk *(u_int *)data = cp->provider->sectorsize; 288105551Sphk if (*(u_int *)data == 0) 289105180Snjl error = ENOENT; 29092698Sphk break; 29192698Sphk case DIOCGMEDIASIZE: 292105551Sphk *(off_t *)data = cp->provider->mediasize; 293105551Sphk if (*(off_t *)data == 0) 294105180Snjl error = ENOENT; 29592698Sphk break; 29692698Sphk case DIOCGFWSECTORS: 29793250Sphk error = g_io_getattr("GEOM::fwsectors", cp, &i, data); 298105180Snjl if (error == 0 && *(u_int *)data == 0) 299105180Snjl error = ENOENT; 30092698Sphk break; 30192698Sphk case DIOCGFWHEADS: 30293250Sphk error = g_io_getattr("GEOM::fwheads", cp, &i, data); 303105180Snjl if (error == 0 && *(u_int *)data == 0) 304105180Snjl error = ENOENT; 30592698Sphk break; 30694287Sphk case DIOCGFRONTSTUFF: 30794287Sphk error = g_io_getattr("GEOM::frontstuff", cp, &i, data); 30894287Sphk break; 30995038Sphk case DIOCSKERNELDUMP: 31095038Sphk u = *((u_int *)data); 31195038Sphk if (!u) { 31295038Sphk set_dumper(NULL); 31395038Sphk error = 0; 31495038Sphk break; 31595038Sphk } 31695038Sphk kd.offset = 0; 31795038Sphk kd.length = OFF_MAX; 31895038Sphk i = sizeof kd; 31995038Sphk error = g_io_getattr("GEOM::kerneldump", cp, &i, &kd); 32095038Sphk if (!error) 32195038Sphk dev->si_flags |= SI_DUMPDEV; 32295038Sphk break; 323104602Sphk 32492698Sphk default: 325114511Sphk gio = g_malloc(sizeof *gio, M_WAITOK | M_ZERO); 32692698Sphk gio->cmd = cmd; 32792698Sphk gio->data = data; 32892698Sphk gio->fflag = fflag; 32992698Sphk gio->td = td; 33092698Sphk i = sizeof *gio; 331109176Sphk /* 332109176Sphk * We always issue ioctls as getattr since the direction of data 333109176Sphk * movement in ioctl is no indication of the ioctl being a "set" 334109176Sphk * or "get" type ioctl or if such simplistic terms even apply 335109176Sphk */ 336109176Sphk error = g_io_getattr("GEOM::ioctl", cp, &i, gio); 33792698Sphk break; 33892698Sphk } 33992403Sphk 34092479Sphk PICKUP_GIANT(); 341104602Sphk if (error == EDIRIOCTL) { 342104602Sphk KASSERT(gio != NULL, ("NULL gio but EDIRIOCTL")); 343104602Sphk KASSERT(gio->func != NULL, ("NULL function but EDIRIOCTL")); 344104602Sphk error = (gio->func)(gio->dev, cmd, data, fflag, td); 345104602Sphk } 34698066Sphk g_waitidle(); 347109253Sphk if (gio != NULL && (error == EOPNOTSUPP || error == ENOIOCTL)) { 348104357Sphk if (g_debugflags & G_T_TOPOLOGY) { 349104357Sphk i = IOCGROUP(cmd); 350104357Sphk printf("IOCTL(0x%lx) \"%s\"", cmd, gp->name); 351104357Sphk if (i > ' ' && i <= '~') 352104357Sphk printf(" '%c'", (int)IOCGROUP(cmd)); 353104357Sphk else 354104357Sphk printf(" 0x%lx", IOCGROUP(cmd)); 355104357Sphk printf("/%ld ", cmd & 0xff); 356104357Sphk if (cmd & IOC_IN) 357104357Sphk printf("I"); 358104357Sphk if (cmd & IOC_OUT) 359104357Sphk printf("O"); 360104357Sphk printf("(%ld) = ENOIOCTL\n", IOCPARM_LEN(cmd)); 361104357Sphk } 36292403Sphk error = ENOTTY; 36392108Sphk } 364109253Sphk if (gio != NULL) 365114511Sphk g_free(gio); 36692108Sphk return (error); 36792108Sphk} 36892108Sphk 36992108Sphkstatic void 37092108Sphkg_dev_done(struct bio *bp2) 37192108Sphk{ 37292108Sphk struct bio *bp; 37392108Sphk 374110517Sphk bp = bp2->bio_parent; 37592108Sphk bp->bio_error = bp2->bio_error; 37692108Sphk if (bp->bio_error != 0) { 37792108Sphk g_trace(G_T_BIO, "g_dev_done(%p) had error %d", 37892108Sphk bp2, bp->bio_error); 37992108Sphk bp->bio_flags |= BIO_ERROR; 38092108Sphk } else { 381105540Sphk g_trace(G_T_BIO, "g_dev_done(%p/%p) resid %ld completed %jd", 382105540Sphk bp2, bp, bp->bio_resid, (intmax_t)bp2->bio_completed); 38392108Sphk } 38492108Sphk bp->bio_resid = bp->bio_bcount - bp2->bio_completed; 38592108Sphk g_destroy_bio(bp2); 38692108Sphk mtx_lock(&Giant); 38792108Sphk biodone(bp); 38892108Sphk mtx_unlock(&Giant); 38992108Sphk} 39092108Sphk 39192108Sphkstatic void 39292108Sphkg_dev_strategy(struct bio *bp) 39392108Sphk{ 39492108Sphk struct g_consumer *cp; 39592108Sphk struct bio *bp2; 39692108Sphk dev_t dev; 39792108Sphk 398106300Sphk KASSERT(bp->bio_cmd == BIO_READ || 399106300Sphk bp->bio_cmd == BIO_WRITE || 400106300Sphk bp->bio_cmd == BIO_DELETE, 401106300Sphk ("Wrong bio_cmd bio=%p cmd=%d", bp, bp->bio_cmd)); 40292108Sphk dev = bp->bio_dev; 40392108Sphk cp = dev->si_drv2; 404112978Sphk KASSERT(cp->acr || cp->acw, 405112978Sphk ("Consumer with zero access count in g_dev_strategy")); 406112978Sphk 40792108Sphk bp2 = g_clone_bio(bp); 408107834Sphk KASSERT(bp2 != NULL, ("XXX: ENOMEM in a bad place")); 40992108Sphk bp2->bio_offset = (off_t)bp->bio_blkno << DEV_BSHIFT; 410107834Sphk KASSERT(bp2->bio_offset >= 0, 411107834Sphk ("Negative bio_offset (%jd) on bio %p", 412107834Sphk (intmax_t)bp2->bio_offset, bp)); 41392108Sphk bp2->bio_length = (off_t)bp->bio_bcount; 41492108Sphk bp2->bio_done = g_dev_done; 41592108Sphk g_trace(G_T_BIO, 416105540Sphk "g_dev_strategy(%p/%p) offset %jd length %jd data %p cmd %d", 417105540Sphk bp, bp2, (intmax_t)bp->bio_offset, (intmax_t)bp2->bio_length, 418105540Sphk bp2->bio_data, bp2->bio_cmd); 41992108Sphk g_io_request(bp2, cp); 420112978Sphk KASSERT(cp->acr || cp->acw, 421112978Sphk ("g_dev_strategy raced with g_dev_close and lost")); 422112978Sphk 42392108Sphk} 42492108Sphk 42596987Sphk/* 42696987Sphk * g_dev_orphan() 42796987Sphk * 428112024Sphk * Called from below when the provider orphaned us. 429112024Sphk * - Clear any dump settings. 430112024Sphk * - Destroy the dev_t to prevent any more request from coming in. The 431112024Sphk * provider is already marked with an error, so anything which comes in 432112024Sphk * in the interrim will be returned immediately. 433112024Sphk * - Wait for any outstanding I/O to finish. 434112024Sphk * - Set our access counts to zero, whatever they were. 435112024Sphk * - Detach and self-destruct. 43696987Sphk */ 43792108Sphk 43892108Sphkstatic void 43993250Sphkg_dev_orphan(struct g_consumer *cp) 44092108Sphk{ 44192108Sphk struct g_geom *gp; 44292108Sphk dev_t dev; 44392108Sphk 444112024Sphk g_topology_assert(); 44592108Sphk gp = cp->geom; 446112024Sphk dev = gp->softc; 44792108Sphk g_trace(G_T_TOPOLOGY, "g_dev_orphan(%p(%s))", cp, gp->name); 448112024Sphk 449112024Sphk /* Reset any dump-area set on this device */ 45095038Sphk if (dev->si_flags & SI_DUMPDEV) 45195038Sphk set_dumper(NULL); 452112024Sphk 453112024Sphk /* Destroy the dev_t so we get no more requests */ 45492108Sphk destroy_dev(dev); 455112024Sphk 456112024Sphk /* Wait for the cows to come home */ 457112024Sphk while (cp->nstart != cp->nend) 458112978Sphk msleep(&dev, NULL, PRIBIO, "gdevorphan", hz / 10); 459112024Sphk 46092108Sphk if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0) 46192108Sphk g_access_rel(cp, -cp->acr, -cp->acw, -cp->ace); 462112024Sphk 46398066Sphk g_detach(cp); 46492108Sphk g_destroy_consumer(cp); 46592108Sphk g_destroy_geom(gp); 46692108Sphk} 46792108Sphk 46896987SphkDECLARE_GEOM_CLASS(g_dev_class, g_dev); 469