geom_dev.c revision 143790
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 143790 2005-03-18 06:57:58Z phk $"); 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", 70126080Sphk .d_flags = D_DISK | D_TRACKCLOSE, 7192108Sphk}; 7292108Sphk 7392108Sphkstatic g_taste_t g_dev_taste; 7492108Sphkstatic g_orphan_t g_dev_orphan; 75136946Sphkstatic g_init_t g_dev_init; 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, 82136946Sphk .init = g_dev_init, 8392108Sphk}; 8492108Sphk 85136946Sphkstatic struct unrhdr *unithdr; /* Locked by topology */ 86136946Sphk 87136946Sphkstatic void 88136946Sphkg_dev_init(struct g_class *mp) 89136946Sphk{ 90136946Sphk 91143238Sphk unithdr = new_unrhdr(0, minor2unit(MAXMINOR), NULL); 92136946Sphk} 93136946Sphk 94115960Sphkvoid 95105947Sphkg_dev_print(void) 96105947Sphk{ 97105947Sphk struct g_geom *gp; 98115960Sphk char const *p = ""; 99105947Sphk 100115960Sphk LIST_FOREACH(gp, &g_dev_class.geom, geom) { 101115960Sphk printf("%s%s", p, gp->name); 102115960Sphk p = " "; 103115960Sphk } 104105947Sphk printf("\n"); 105105947Sphk} 106105947Sphk 107119593Sphkstruct g_provider * 108130585Sphkg_dev_getprovider(struct cdev *dev) 109119593Sphk{ 110119593Sphk struct g_consumer *cp; 111119593Sphk 112135716Sphk g_topology_assert(); 113119593Sphk if (dev == NULL) 114119593Sphk return (NULL); 115135716Sphk if (dev->si_devsw != &g_dev_cdevsw) 116143790Sphk return (NULL); 117143790Sphk cp = dev->si_drv2; 118119593Sphk return (cp->provider); 119119593Sphk} 120119593Sphk 121119593Sphk 12292108Sphkstatic struct g_geom * 12393250Sphkg_dev_taste(struct g_class *mp, struct g_provider *pp, int insist __unused) 12492108Sphk{ 12592108Sphk struct g_geom *gp; 12692108Sphk struct g_consumer *cp; 12796987Sphk int error; 128130585Sphk struct cdev *dev; 129136946Sphk u_int unit; 13092108Sphk 13192108Sphk g_trace(G_T_TOPOLOGY, "dev_taste(%s,%s)", mp->name, pp->name); 13292108Sphk g_topology_assert(); 13392108Sphk LIST_FOREACH(cp, &pp->consumers, consumers) 13493248Sphk if (cp->geom->class == mp) 13592108Sphk return (NULL); 13692108Sphk gp = g_new_geomf(mp, pp->name); 13792108Sphk cp = g_new_consumer(gp); 13896987Sphk error = g_attach(cp, pp); 13996987Sphk KASSERT(error == 0, 14096987Sphk ("g_dev_taste(%s) failed to g_attach, err=%d", pp->name, error)); 14196987Sphk /* 14296987Sphk * XXX: I'm not 100% sure we can call make_dev(9) without Giant 14396987Sphk * yet. Once we can, we don't need to drop topology here either. 14496987Sphk */ 145136946Sphk unit = alloc_unr(unithdr); 14692108Sphk g_topology_unlock(); 14792108Sphk mtx_lock(&Giant); 148136946Sphk dev = make_dev(&g_dev_cdevsw, unit2minor(unit), 149104316Sphk UID_ROOT, GID_OPERATOR, 0640, gp->name); 150110700Sphk if (pp->flags & G_PF_CANDELETE) 151110700Sphk dev->si_flags |= SI_CANDELETE; 15296987Sphk mtx_unlock(&Giant); 15396987Sphk g_topology_lock(); 154110728Sphk dev->si_iosize_max = MAXPHYS; 15592108Sphk gp->softc = dev; 15692108Sphk dev->si_drv1 = gp; 15792108Sphk dev->si_drv2 = cp; 15892108Sphk return (gp); 15992108Sphk} 16092108Sphk 16192108Sphkstatic int 162130585Sphkg_dev_open(struct cdev *dev, int flags, int fmt, struct thread *td) 16392108Sphk{ 16492108Sphk struct g_geom *gp; 16592108Sphk struct g_consumer *cp; 16692108Sphk int error, r, w, e; 16792108Sphk 16892108Sphk gp = dev->si_drv1; 16992108Sphk cp = dev->si_drv2; 170112978Sphk if (gp == NULL || cp == NULL || gp->softc != dev) 171112978Sphk return(ENXIO); /* g_dev_taste() not done yet */ 172112978Sphk 17392108Sphk g_trace(G_T_ACCESS, "g_dev_open(%s, %d, %d, %p)", 17492108Sphk gp->name, flags, fmt, td); 175130712Sphk 17692108Sphk r = flags & FREAD ? 1 : 0; 17792108Sphk w = flags & FWRITE ? 1 : 0; 178103004Sphk#ifdef notyet 17992108Sphk e = flags & O_EXCL ? 1 : 0; 180103004Sphk#else 181103004Sphk e = 0; 182103004Sphk#endif 183130712Sphk if (w) { 184130712Sphk /* 185130712Sphk * When running in very secure mode, do not allow 186130712Sphk * opens for writing of any disks. 187130712Sphk */ 188130712Sphk error = securelevel_ge(td->td_ucred, 2); 189130712Sphk if (error) 190130712Sphk return (error); 191130712Sphk } 192112978Sphk g_topology_lock(); 193112978Sphk if (dev->si_devsw == NULL) 194112978Sphk error = ENXIO; /* We were orphaned */ 195112978Sphk else 196125755Sphk error = g_access(cp, r, w, e); 19792108Sphk g_topology_unlock(); 19892108Sphk return(error); 19992108Sphk} 20092108Sphk 20192108Sphkstatic int 202130585Sphkg_dev_close(struct cdev *dev, int flags, int fmt, struct thread *td) 20392108Sphk{ 20492108Sphk struct g_geom *gp; 20592108Sphk struct g_consumer *cp; 206114864Sphk int error, r, w, e, i; 20792108Sphk 20892108Sphk gp = dev->si_drv1; 20992108Sphk cp = dev->si_drv2; 21092108Sphk if (gp == NULL || cp == NULL) 21192108Sphk return(ENXIO); 21292108Sphk g_trace(G_T_ACCESS, "g_dev_close(%s, %d, %d, %p)", 21392108Sphk gp->name, flags, fmt, td); 21492108Sphk r = flags & FREAD ? -1 : 0; 21592108Sphk w = flags & FWRITE ? -1 : 0; 216103004Sphk#ifdef notyet 21792108Sphk e = flags & O_EXCL ? -1 : 0; 218103004Sphk#else 219103004Sphk e = 0; 220103004Sphk#endif 221112978Sphk g_topology_lock(); 222112978Sphk if (dev->si_devsw == NULL) 223112978Sphk error = ENXIO; /* We were orphaned */ 224112978Sphk else 225125755Sphk error = g_access(cp, r, w, e); 226114864Sphk for (i = 0; i < 10 * hz;) { 227114864Sphk if (cp->acr != 0 || cp->acw != 0) 228114864Sphk break; 229114864Sphk if (cp->nstart == cp->nend) 230114864Sphk break; 231114864Sphk tsleep(&i, PRIBIO, "gdevwclose", hz / 10); 232114864Sphk i += hz / 10; 233114864Sphk } 234114864Sphk if (cp->acr == 0 && cp->acw == 0 && cp->nstart != cp->nend) { 235124880Sphk printf("WARNING: Final close of geom_dev(%s) %s %s\n", 236114864Sphk gp->name, 237114864Sphk "still has outstanding I/O after 10 seconds.", 238114864Sphk "Completing close anyway, panic may happen later."); 239114864Sphk } 24092108Sphk g_topology_unlock(); 24192108Sphk return (error); 24292108Sphk} 24392108Sphk 244112978Sphk/* 245112978Sphk * XXX: Until we have unmessed the ioctl situation, there is a race against 246112978Sphk * XXX: a concurrent orphanization. We cannot close it by holding topology 247112978Sphk * XXX: since that would prevent us from doing our job, and stalling events 248112978Sphk * XXX: will break (actually: stall) the BSD disklabel hacks. 249112978Sphk */ 25092108Sphkstatic int 251130585Sphkg_dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td) 25292108Sphk{ 253115515Sphk struct g_geom *gp; 25492108Sphk struct g_consumer *cp; 25595038Sphk struct g_kerneldump kd; 25692108Sphk int i, error; 25795038Sphk u_int u; 25892108Sphk 25992108Sphk gp = dev->si_drv1; 26092108Sphk cp = dev->si_drv2; 26192108Sphk 26292108Sphk error = 0; 263112978Sphk KASSERT(cp->acr || cp->acw, 264112978Sphk ("Consumer with zero access count in g_dev_ioctl")); 26592403Sphk 26692698Sphk i = IOCPARM_LEN(cmd); 26792698Sphk switch (cmd) { 26892698Sphk case DIOCGSECTORSIZE: 269105551Sphk *(u_int *)data = cp->provider->sectorsize; 270105551Sphk if (*(u_int *)data == 0) 271105180Snjl error = ENOENT; 27292698Sphk break; 27392698Sphk case DIOCGMEDIASIZE: 274105551Sphk *(off_t *)data = cp->provider->mediasize; 275105551Sphk if (*(off_t *)data == 0) 276105180Snjl error = ENOENT; 27792698Sphk break; 27892698Sphk case DIOCGFWSECTORS: 27993250Sphk error = g_io_getattr("GEOM::fwsectors", cp, &i, data); 280105180Snjl if (error == 0 && *(u_int *)data == 0) 281105180Snjl error = ENOENT; 28292698Sphk break; 28392698Sphk case DIOCGFWHEADS: 28493250Sphk error = g_io_getattr("GEOM::fwheads", cp, &i, data); 285105180Snjl if (error == 0 && *(u_int *)data == 0) 286105180Snjl error = ENOENT; 28792698Sphk break; 28894287Sphk case DIOCGFRONTSTUFF: 28994287Sphk error = g_io_getattr("GEOM::frontstuff", cp, &i, data); 29094287Sphk break; 29195038Sphk case DIOCSKERNELDUMP: 29295038Sphk u = *((u_int *)data); 29395038Sphk if (!u) { 29495038Sphk set_dumper(NULL); 29595038Sphk error = 0; 29695038Sphk break; 29795038Sphk } 29895038Sphk kd.offset = 0; 29995038Sphk kd.length = OFF_MAX; 30095038Sphk i = sizeof kd; 30195038Sphk error = g_io_getattr("GEOM::kerneldump", cp, &i, &kd); 30295038Sphk if (!error) 30395038Sphk dev->si_flags |= SI_DUMPDEV; 30495038Sphk break; 305104602Sphk 30692698Sphk default: 307119660Sphk if (cp->provider->geom->ioctl != NULL) { 308138732Sphk error = cp->provider->geom->ioctl(cp->provider, cmd, data, fflag, td); 309119749Sphk } else { 310119749Sphk error = ENOIOCTL; 311119660Sphk } 31292698Sphk } 31392403Sphk 31492108Sphk return (error); 31592108Sphk} 31692108Sphk 31792108Sphkstatic void 31892108Sphkg_dev_done(struct bio *bp2) 31992108Sphk{ 32092108Sphk struct bio *bp; 32192108Sphk 322110517Sphk bp = bp2->bio_parent; 32392108Sphk bp->bio_error = bp2->bio_error; 32492108Sphk if (bp->bio_error != 0) { 32592108Sphk g_trace(G_T_BIO, "g_dev_done(%p) had error %d", 32692108Sphk bp2, bp->bio_error); 32792108Sphk bp->bio_flags |= BIO_ERROR; 32892108Sphk } else { 329105540Sphk g_trace(G_T_BIO, "g_dev_done(%p/%p) resid %ld completed %jd", 330105540Sphk bp2, bp, bp->bio_resid, (intmax_t)bp2->bio_completed); 33192108Sphk } 332137029Sphk bp->bio_resid = bp->bio_length - bp2->bio_completed; 333137029Sphk bp->bio_completed = bp2->bio_completed; 33492108Sphk g_destroy_bio(bp2); 33592108Sphk biodone(bp); 33692108Sphk} 33792108Sphk 33892108Sphkstatic void 33992108Sphkg_dev_strategy(struct bio *bp) 34092108Sphk{ 34192108Sphk struct g_consumer *cp; 34292108Sphk struct bio *bp2; 343130585Sphk struct cdev *dev; 34492108Sphk 345106300Sphk KASSERT(bp->bio_cmd == BIO_READ || 346106300Sphk bp->bio_cmd == BIO_WRITE || 347106300Sphk bp->bio_cmd == BIO_DELETE, 348106300Sphk ("Wrong bio_cmd bio=%p cmd=%d", bp, bp->bio_cmd)); 34992108Sphk dev = bp->bio_dev; 35092108Sphk cp = dev->si_drv2; 351112978Sphk KASSERT(cp->acr || cp->acw, 352112978Sphk ("Consumer with zero access count in g_dev_strategy")); 353112978Sphk 354135865Spjd if ((bp->bio_offset % cp->provider->sectorsize) != 0 || 355135865Spjd (bp->bio_bcount % cp->provider->sectorsize) != 0) { 356135865Spjd biofinish(bp, NULL, EINVAL); 357135865Spjd return; 358135865Spjd } 359135865Spjd 360118869Sphk for (;;) { 361118869Sphk /* 362118869Sphk * XXX: This is not an ideal solution, but I belive it to 363118869Sphk * XXX: deadlock safe, all things considered. 364118869Sphk */ 365118869Sphk bp2 = g_clone_bio(bp); 366118869Sphk if (bp2 != NULL) 367118869Sphk break; 368118869Sphk tsleep(&bp, PRIBIO, "gdstrat", hz / 10); 369118869Sphk } 370107834Sphk KASSERT(bp2 != NULL, ("XXX: ENOMEM in a bad place")); 37192108Sphk bp2->bio_done = g_dev_done; 37292108Sphk g_trace(G_T_BIO, 373105540Sphk "g_dev_strategy(%p/%p) offset %jd length %jd data %p cmd %d", 374105540Sphk bp, bp2, (intmax_t)bp->bio_offset, (intmax_t)bp2->bio_length, 375105540Sphk bp2->bio_data, bp2->bio_cmd); 37692108Sphk g_io_request(bp2, cp); 377112978Sphk KASSERT(cp->acr || cp->acw, 378112978Sphk ("g_dev_strategy raced with g_dev_close and lost")); 379112978Sphk 38092108Sphk} 38192108Sphk 38296987Sphk/* 38396987Sphk * g_dev_orphan() 38496987Sphk * 385112024Sphk * Called from below when the provider orphaned us. 386112024Sphk * - Clear any dump settings. 387130585Sphk * - Destroy the struct cdev *to prevent any more request from coming in. The 388112024Sphk * provider is already marked with an error, so anything which comes in 389112024Sphk * in the interrim will be returned immediately. 390112024Sphk * - Wait for any outstanding I/O to finish. 391112024Sphk * - Set our access counts to zero, whatever they were. 392112024Sphk * - Detach and self-destruct. 39396987Sphk */ 39492108Sphk 39592108Sphkstatic void 39693250Sphkg_dev_orphan(struct g_consumer *cp) 39792108Sphk{ 39892108Sphk struct g_geom *gp; 399130585Sphk struct cdev *dev; 400136946Sphk u_int unit; 40192108Sphk 402112024Sphk g_topology_assert(); 40392108Sphk gp = cp->geom; 404112024Sphk dev = gp->softc; 40592108Sphk g_trace(G_T_TOPOLOGY, "g_dev_orphan(%p(%s))", cp, gp->name); 406112024Sphk 407112024Sphk /* Reset any dump-area set on this device */ 40895038Sphk if (dev->si_flags & SI_DUMPDEV) 40995038Sphk set_dumper(NULL); 410112024Sphk 411130585Sphk /* Destroy the struct cdev *so we get no more requests */ 412136946Sphk unit = dev2unit(dev); 41392108Sphk destroy_dev(dev); 414136946Sphk free_unr(unithdr, unit); 415112024Sphk 416112024Sphk /* Wait for the cows to come home */ 417112024Sphk while (cp->nstart != cp->nend) 418112978Sphk msleep(&dev, NULL, PRIBIO, "gdevorphan", hz / 10); 419112024Sphk 42092108Sphk if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0) 421125755Sphk g_access(cp, -cp->acr, -cp->acw, -cp->ace); 422112024Sphk 42398066Sphk g_detach(cp); 42492108Sphk g_destroy_consumer(cp); 42592108Sphk g_destroy_geom(gp); 42692108Sphk} 42792108Sphk 42896987SphkDECLARE_GEOM_CLASS(g_dev_class, g_dev); 429