geom_dev.c revision 135865
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 135865 2004-09-27 22:10:01Z pjd $"); 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> 47130712Sphk#include <sys/proc.h> 4892108Sphk#include <sys/errno.h> 4992108Sphk#include <sys/time.h> 5092108Sphk#include <sys/disk.h> 5192108Sphk#include <sys/fcntl.h> 52114216Skan#include <sys/limits.h> 5392108Sphk#include <geom/geom.h> 5495323Sphk#include <geom/geom_int.h> 5592108Sphk 5692108Sphkstatic d_open_t g_dev_open; 5792108Sphkstatic d_close_t g_dev_close; 5892108Sphkstatic d_strategy_t g_dev_strategy; 5992108Sphkstatic d_ioctl_t g_dev_ioctl; 6092108Sphk 6192108Sphkstatic struct cdevsw g_dev_cdevsw = { 62126080Sphk .d_version = D_VERSION, 63111815Sphk .d_open = g_dev_open, 64111815Sphk .d_close = g_dev_close, 65111815Sphk .d_read = physread, 66111815Sphk .d_write = physwrite, 67111815Sphk .d_ioctl = g_dev_ioctl, 68111815Sphk .d_strategy = g_dev_strategy, 69111815Sphk .d_name = "g_dev", 70111815Sphk .d_maj = GEOM_MAJOR, 71126080Sphk .d_flags = D_DISK | D_TRACKCLOSE, 7292108Sphk}; 7392108Sphk 7492108Sphkstatic g_taste_t g_dev_taste; 7592108Sphkstatic g_orphan_t g_dev_orphan; 7692108Sphk 7793248Sphkstatic struct g_class g_dev_class = { 78112552Sphk .name = "DEV", 79133318Sphk .version = G_VERSION, 80112552Sphk .taste = g_dev_taste, 81133314Sphk .orphan = g_dev_orphan, 8292108Sphk}; 8392108Sphk 84115960Sphkvoid 85105947Sphkg_dev_print(void) 86105947Sphk{ 87105947Sphk struct g_geom *gp; 88115960Sphk char const *p = ""; 89105947Sphk 90115960Sphk LIST_FOREACH(gp, &g_dev_class.geom, geom) { 91115960Sphk printf("%s%s", p, gp->name); 92115960Sphk p = " "; 93115960Sphk } 94105947Sphk printf("\n"); 95105947Sphk} 96105947Sphk 97119593Sphkstruct g_provider * 98130585Sphkg_dev_getprovider(struct cdev *dev) 99119593Sphk{ 100119593Sphk struct g_consumer *cp; 101119593Sphk 102135716Sphk g_topology_assert(); 103119593Sphk if (dev == NULL) 104119593Sphk return (NULL); 105135716Sphk if (dev->si_devsw != &g_dev_cdevsw) 106135716Sphk cp = NULL; 107135716Sphk else 108135716Sphk cp = dev->si_drv2; 109119593Sphk return (cp->provider); 110119593Sphk} 111119593Sphk 112119593Sphk 11392108Sphkstatic struct g_geom * 11493250Sphkg_dev_taste(struct g_class *mp, struct g_provider *pp, int insist __unused) 11592108Sphk{ 11692108Sphk struct g_geom *gp; 11792108Sphk struct g_consumer *cp; 118110540Sphk static int unit = GEOM_MINOR_PROVIDERS; 11996987Sphk int error; 120130585Sphk struct cdev *dev; 12192108Sphk 12292108Sphk g_trace(G_T_TOPOLOGY, "dev_taste(%s,%s)", mp->name, pp->name); 12392108Sphk g_topology_assert(); 12492108Sphk LIST_FOREACH(cp, &pp->consumers, consumers) 12593248Sphk if (cp->geom->class == mp) 12692108Sphk return (NULL); 12792108Sphk gp = g_new_geomf(mp, pp->name); 12892108Sphk cp = g_new_consumer(gp); 12996987Sphk error = g_attach(cp, pp); 13096987Sphk KASSERT(error == 0, 13196987Sphk ("g_dev_taste(%s) failed to g_attach, err=%d", pp->name, error)); 13296987Sphk /* 13396987Sphk * XXX: I'm not 100% sure we can call make_dev(9) without Giant 13496987Sphk * yet. Once we can, we don't need to drop topology here either. 13596987Sphk */ 13692108Sphk g_topology_unlock(); 13792108Sphk mtx_lock(&Giant); 13896987Sphk dev = make_dev(&g_dev_cdevsw, unit2minor(unit++), 139104316Sphk UID_ROOT, GID_OPERATOR, 0640, gp->name); 140110700Sphk if (pp->flags & G_PF_CANDELETE) 141110700Sphk dev->si_flags |= SI_CANDELETE; 14296987Sphk mtx_unlock(&Giant); 14396987Sphk g_topology_lock(); 144110728Sphk dev->si_iosize_max = MAXPHYS; 145110710Sphk dev->si_stripesize = pp->stripesize; 146110710Sphk dev->si_stripeoffset = pp->stripeoffset; 14792108Sphk gp->softc = dev; 14892108Sphk dev->si_drv1 = gp; 14992108Sphk dev->si_drv2 = cp; 15092108Sphk return (gp); 15192108Sphk} 15292108Sphk 15392108Sphkstatic int 154130585Sphkg_dev_open(struct cdev *dev, int flags, int fmt, struct thread *td) 15592108Sphk{ 15692108Sphk struct g_geom *gp; 15792108Sphk struct g_consumer *cp; 15892108Sphk int error, r, w, e; 15992108Sphk 16092108Sphk gp = dev->si_drv1; 16192108Sphk cp = dev->si_drv2; 162112978Sphk if (gp == NULL || cp == NULL || gp->softc != dev) 163112978Sphk return(ENXIO); /* g_dev_taste() not done yet */ 164112978Sphk 16592108Sphk g_trace(G_T_ACCESS, "g_dev_open(%s, %d, %d, %p)", 16692108Sphk gp->name, flags, fmt, td); 167130712Sphk 16892108Sphk r = flags & FREAD ? 1 : 0; 16992108Sphk w = flags & FWRITE ? 1 : 0; 170103004Sphk#ifdef notyet 17192108Sphk e = flags & O_EXCL ? 1 : 0; 172103004Sphk#else 173103004Sphk e = 0; 174103004Sphk#endif 175130712Sphk if (w) { 176130712Sphk /* 177130712Sphk * When running in very secure mode, do not allow 178130712Sphk * opens for writing of any disks. 179130712Sphk */ 180130712Sphk error = securelevel_ge(td->td_ucred, 2); 181130712Sphk if (error) 182130712Sphk return (error); 183130712Sphk } 184112978Sphk g_topology_lock(); 185112978Sphk if (dev->si_devsw == NULL) 186112978Sphk error = ENXIO; /* We were orphaned */ 187112978Sphk else 188125755Sphk error = g_access(cp, r, w, e); 18992108Sphk g_topology_unlock(); 19098066Sphk g_waitidle(); 191112978Sphk if (!error) 192112978Sphk dev->si_bsize_phys = cp->provider->sectorsize; 19392108Sphk return(error); 19492108Sphk} 19592108Sphk 19692108Sphkstatic int 197130585Sphkg_dev_close(struct cdev *dev, int flags, int fmt, struct thread *td) 19892108Sphk{ 19992108Sphk struct g_geom *gp; 20092108Sphk struct g_consumer *cp; 201114864Sphk int error, r, w, e, i; 20292108Sphk 20392108Sphk gp = dev->si_drv1; 20492108Sphk cp = dev->si_drv2; 20592108Sphk if (gp == NULL || cp == NULL) 20692108Sphk return(ENXIO); 20792108Sphk g_trace(G_T_ACCESS, "g_dev_close(%s, %d, %d, %p)", 20892108Sphk gp->name, flags, fmt, td); 20992108Sphk r = flags & FREAD ? -1 : 0; 21092108Sphk w = flags & FWRITE ? -1 : 0; 211103004Sphk#ifdef notyet 21292108Sphk e = flags & O_EXCL ? -1 : 0; 213103004Sphk#else 214103004Sphk e = 0; 215103004Sphk#endif 216112978Sphk g_topology_lock(); 217112978Sphk if (dev->si_devsw == NULL) 218112978Sphk error = ENXIO; /* We were orphaned */ 219112978Sphk else 220125755Sphk error = g_access(cp, r, w, e); 221114864Sphk for (i = 0; i < 10 * hz;) { 222114864Sphk if (cp->acr != 0 || cp->acw != 0) 223114864Sphk break; 224114864Sphk if (cp->nstart == cp->nend) 225114864Sphk break; 226114864Sphk tsleep(&i, PRIBIO, "gdevwclose", hz / 10); 227114864Sphk i += hz / 10; 228114864Sphk } 229114864Sphk if (cp->acr == 0 && cp->acw == 0 && cp->nstart != cp->nend) { 230124880Sphk printf("WARNING: Final close of geom_dev(%s) %s %s\n", 231114864Sphk gp->name, 232114864Sphk "still has outstanding I/O after 10 seconds.", 233114864Sphk "Completing close anyway, panic may happen later."); 234114864Sphk } 23592108Sphk g_topology_unlock(); 23698066Sphk g_waitidle(); 23792108Sphk return (error); 23892108Sphk} 23992108Sphk 240112978Sphk/* 241112978Sphk * XXX: Until we have unmessed the ioctl situation, there is a race against 242112978Sphk * XXX: a concurrent orphanization. We cannot close it by holding topology 243112978Sphk * XXX: since that would prevent us from doing our job, and stalling events 244112978Sphk * XXX: will break (actually: stall) the BSD disklabel hacks. 245112978Sphk */ 24692108Sphkstatic int 247130585Sphkg_dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td) 24892108Sphk{ 249115515Sphk struct g_geom *gp; 25092108Sphk struct g_consumer *cp; 25195038Sphk struct g_kerneldump kd; 25292108Sphk int i, error; 25395038Sphk u_int u; 25492108Sphk 25592108Sphk gp = dev->si_drv1; 25692108Sphk cp = dev->si_drv2; 25792108Sphk 25892108Sphk error = 0; 259112978Sphk KASSERT(cp->acr || cp->acw, 260112978Sphk ("Consumer with zero access count in g_dev_ioctl")); 26192403Sphk 26292698Sphk i = IOCPARM_LEN(cmd); 26392698Sphk switch (cmd) { 26492698Sphk case DIOCGSECTORSIZE: 265105551Sphk *(u_int *)data = cp->provider->sectorsize; 266105551Sphk if (*(u_int *)data == 0) 267105180Snjl error = ENOENT; 26892698Sphk break; 26992698Sphk case DIOCGMEDIASIZE: 270105551Sphk *(off_t *)data = cp->provider->mediasize; 271105551Sphk if (*(off_t *)data == 0) 272105180Snjl error = ENOENT; 27392698Sphk break; 27492698Sphk case DIOCGFWSECTORS: 27593250Sphk error = g_io_getattr("GEOM::fwsectors", cp, &i, data); 276105180Snjl if (error == 0 && *(u_int *)data == 0) 277105180Snjl error = ENOENT; 27892698Sphk break; 27992698Sphk case DIOCGFWHEADS: 28093250Sphk error = g_io_getattr("GEOM::fwheads", cp, &i, data); 281105180Snjl if (error == 0 && *(u_int *)data == 0) 282105180Snjl error = ENOENT; 28392698Sphk break; 28494287Sphk case DIOCGFRONTSTUFF: 28594287Sphk error = g_io_getattr("GEOM::frontstuff", cp, &i, data); 28694287Sphk break; 28795038Sphk case DIOCSKERNELDUMP: 28895038Sphk u = *((u_int *)data); 28995038Sphk if (!u) { 29095038Sphk set_dumper(NULL); 29195038Sphk error = 0; 29295038Sphk break; 29395038Sphk } 29495038Sphk kd.offset = 0; 29595038Sphk kd.length = OFF_MAX; 29695038Sphk i = sizeof kd; 29795038Sphk error = g_io_getattr("GEOM::kerneldump", cp, &i, &kd); 29895038Sphk if (!error) 29995038Sphk dev->si_flags |= SI_DUMPDEV; 30095038Sphk break; 301104602Sphk 30292698Sphk default: 303119660Sphk if (cp->provider->geom->ioctl != NULL) { 304119660Sphk error = cp->provider->geom->ioctl(cp->provider, cmd, data, td); 305119749Sphk } else { 306119749Sphk error = ENOIOCTL; 307119660Sphk } 30892698Sphk } 30992403Sphk 31098066Sphk g_waitidle(); 31192108Sphk return (error); 31292108Sphk} 31392108Sphk 31492108Sphkstatic void 31592108Sphkg_dev_done(struct bio *bp2) 31692108Sphk{ 31792108Sphk struct bio *bp; 31892108Sphk 319110517Sphk bp = bp2->bio_parent; 32092108Sphk bp->bio_error = bp2->bio_error; 32192108Sphk if (bp->bio_error != 0) { 32292108Sphk g_trace(G_T_BIO, "g_dev_done(%p) had error %d", 32392108Sphk bp2, bp->bio_error); 32492108Sphk bp->bio_flags |= BIO_ERROR; 32592108Sphk } else { 326105540Sphk g_trace(G_T_BIO, "g_dev_done(%p/%p) resid %ld completed %jd", 327105540Sphk bp2, bp, bp->bio_resid, (intmax_t)bp2->bio_completed); 32892108Sphk } 32992108Sphk bp->bio_resid = bp->bio_bcount - bp2->bio_completed; 33092108Sphk g_destroy_bio(bp2); 33192108Sphk biodone(bp); 33292108Sphk} 33392108Sphk 33492108Sphkstatic void 33592108Sphkg_dev_strategy(struct bio *bp) 33692108Sphk{ 33792108Sphk struct g_consumer *cp; 33892108Sphk struct bio *bp2; 339130585Sphk struct cdev *dev; 34092108Sphk 341106300Sphk KASSERT(bp->bio_cmd == BIO_READ || 342106300Sphk bp->bio_cmd == BIO_WRITE || 343106300Sphk bp->bio_cmd == BIO_DELETE, 344106300Sphk ("Wrong bio_cmd bio=%p cmd=%d", bp, bp->bio_cmd)); 34592108Sphk dev = bp->bio_dev; 34692108Sphk cp = dev->si_drv2; 347112978Sphk KASSERT(cp->acr || cp->acw, 348112978Sphk ("Consumer with zero access count in g_dev_strategy")); 349112978Sphk 350135865Spjd if ((bp->bio_offset % cp->provider->sectorsize) != 0 || 351135865Spjd (bp->bio_bcount % cp->provider->sectorsize) != 0) { 352135865Spjd biofinish(bp, NULL, EINVAL); 353135865Spjd return; 354135865Spjd } 355135865Spjd 356118869Sphk for (;;) { 357118869Sphk /* 358118869Sphk * XXX: This is not an ideal solution, but I belive it to 359118869Sphk * XXX: deadlock safe, all things considered. 360118869Sphk */ 361118869Sphk bp2 = g_clone_bio(bp); 362118869Sphk if (bp2 != NULL) 363118869Sphk break; 364118869Sphk tsleep(&bp, PRIBIO, "gdstrat", hz / 10); 365118869Sphk } 366107834Sphk KASSERT(bp2 != NULL, ("XXX: ENOMEM in a bad place")); 36792108Sphk bp2->bio_length = (off_t)bp->bio_bcount; 36892108Sphk bp2->bio_done = g_dev_done; 36992108Sphk g_trace(G_T_BIO, 370105540Sphk "g_dev_strategy(%p/%p) offset %jd length %jd data %p cmd %d", 371105540Sphk bp, bp2, (intmax_t)bp->bio_offset, (intmax_t)bp2->bio_length, 372105540Sphk bp2->bio_data, bp2->bio_cmd); 37392108Sphk g_io_request(bp2, cp); 374112978Sphk KASSERT(cp->acr || cp->acw, 375112978Sphk ("g_dev_strategy raced with g_dev_close and lost")); 376112978Sphk 37792108Sphk} 37892108Sphk 37996987Sphk/* 38096987Sphk * g_dev_orphan() 38196987Sphk * 382112024Sphk * Called from below when the provider orphaned us. 383112024Sphk * - Clear any dump settings. 384130585Sphk * - Destroy the struct cdev *to prevent any more request from coming in. The 385112024Sphk * provider is already marked with an error, so anything which comes in 386112024Sphk * in the interrim will be returned immediately. 387112024Sphk * - Wait for any outstanding I/O to finish. 388112024Sphk * - Set our access counts to zero, whatever they were. 389112024Sphk * - Detach and self-destruct. 39096987Sphk */ 39192108Sphk 39292108Sphkstatic void 39393250Sphkg_dev_orphan(struct g_consumer *cp) 39492108Sphk{ 39592108Sphk struct g_geom *gp; 396130585Sphk struct cdev *dev; 39792108Sphk 398112024Sphk g_topology_assert(); 39992108Sphk gp = cp->geom; 400112024Sphk dev = gp->softc; 40192108Sphk g_trace(G_T_TOPOLOGY, "g_dev_orphan(%p(%s))", cp, gp->name); 402112024Sphk 403112024Sphk /* Reset any dump-area set on this device */ 40495038Sphk if (dev->si_flags & SI_DUMPDEV) 40595038Sphk set_dumper(NULL); 406112024Sphk 407130585Sphk /* Destroy the struct cdev *so we get no more requests */ 40892108Sphk destroy_dev(dev); 409112024Sphk 410112024Sphk /* Wait for the cows to come home */ 411112024Sphk while (cp->nstart != cp->nend) 412112978Sphk msleep(&dev, NULL, PRIBIO, "gdevorphan", hz / 10); 413112024Sphk 41492108Sphk if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0) 415125755Sphk g_access(cp, -cp->acr, -cp->acw, -cp->ace); 416112024Sphk 41798066Sphk g_detach(cp); 41892108Sphk g_destroy_consumer(cp); 41992108Sphk g_destroy_geom(gp); 42092108Sphk} 42192108Sphk 42296987SphkDECLARE_GEOM_CLASS(g_dev_class, g_dev); 423