geom_dev.c revision 182843
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 182843 2008-09-07 13:54:57Z lulf $"); 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 91179413Sed unithdr = new_unrhdr(0, INT_MAX, 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)); 141136946Sphk unit = alloc_unr(unithdr); 142136946Sphk dev = make_dev(&g_dev_cdevsw, unit2minor(unit), 143104316Sphk UID_ROOT, GID_OPERATOR, 0640, gp->name); 144110700Sphk if (pp->flags & G_PF_CANDELETE) 145110700Sphk dev->si_flags |= SI_CANDELETE; 146110728Sphk dev->si_iosize_max = MAXPHYS; 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(); 19092108Sphk return(error); 19192108Sphk} 19292108Sphk 19392108Sphkstatic int 194130585Sphkg_dev_close(struct cdev *dev, int flags, int fmt, struct thread *td) 19592108Sphk{ 19692108Sphk struct g_geom *gp; 19792108Sphk struct g_consumer *cp; 198114864Sphk int error, r, w, e, i; 19992108Sphk 20092108Sphk gp = dev->si_drv1; 20192108Sphk cp = dev->si_drv2; 20292108Sphk if (gp == NULL || cp == NULL) 20392108Sphk return(ENXIO); 20492108Sphk g_trace(G_T_ACCESS, "g_dev_close(%s, %d, %d, %p)", 20592108Sphk gp->name, flags, fmt, td); 20692108Sphk r = flags & FREAD ? -1 : 0; 20792108Sphk w = flags & FWRITE ? -1 : 0; 208103004Sphk#ifdef notyet 20992108Sphk e = flags & O_EXCL ? -1 : 0; 210103004Sphk#else 211103004Sphk e = 0; 212103004Sphk#endif 213112978Sphk g_topology_lock(); 214112978Sphk if (dev->si_devsw == NULL) 215112978Sphk error = ENXIO; /* We were orphaned */ 216112978Sphk else 217125755Sphk error = g_access(cp, r, w, e); 218114864Sphk for (i = 0; i < 10 * hz;) { 219114864Sphk if (cp->acr != 0 || cp->acw != 0) 220114864Sphk break; 221114864Sphk if (cp->nstart == cp->nend) 222114864Sphk break; 223167086Sjhb pause("gdevwclose", hz / 10); 224114864Sphk i += hz / 10; 225114864Sphk } 226114864Sphk if (cp->acr == 0 && cp->acw == 0 && cp->nstart != cp->nend) { 227124880Sphk printf("WARNING: Final close of geom_dev(%s) %s %s\n", 228114864Sphk gp->name, 229114864Sphk "still has outstanding I/O after 10 seconds.", 230114864Sphk "Completing close anyway, panic may happen later."); 231114864Sphk } 23292108Sphk g_topology_unlock(); 23392108Sphk return (error); 23492108Sphk} 23592108Sphk 236112978Sphk/* 237112978Sphk * XXX: Until we have unmessed the ioctl situation, there is a race against 238112978Sphk * XXX: a concurrent orphanization. We cannot close it by holding topology 239112978Sphk * XXX: since that would prevent us from doing our job, and stalling events 240112978Sphk * XXX: will break (actually: stall) the BSD disklabel hacks. 241112978Sphk */ 24292108Sphkstatic int 243130585Sphkg_dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td) 24492108Sphk{ 245115515Sphk struct g_geom *gp; 24692108Sphk struct g_consumer *cp; 247182843Slulf struct g_provider *pp; 24895038Sphk struct g_kerneldump kd; 249174674Sphk off_t offset, length, chunk; 25092108Sphk int i, error; 25195038Sphk u_int u; 25292108Sphk 25392108Sphk gp = dev->si_drv1; 25492108Sphk cp = dev->si_drv2; 255182843Slulf pp = cp->provider; 25692108Sphk 25792108Sphk error = 0; 258112978Sphk KASSERT(cp->acr || cp->acw, 259112978Sphk ("Consumer with zero access count in g_dev_ioctl")); 26092403Sphk 26192698Sphk i = IOCPARM_LEN(cmd); 26292698Sphk switch (cmd) { 26392698Sphk case DIOCGSECTORSIZE: 264105551Sphk *(u_int *)data = cp->provider->sectorsize; 265105551Sphk if (*(u_int *)data == 0) 266105180Snjl error = ENOENT; 26792698Sphk break; 26892698Sphk case DIOCGMEDIASIZE: 269105551Sphk *(off_t *)data = cp->provider->mediasize; 270105551Sphk if (*(off_t *)data == 0) 271105180Snjl error = ENOENT; 27292698Sphk break; 27392698Sphk case DIOCGFWSECTORS: 27493250Sphk error = g_io_getattr("GEOM::fwsectors", cp, &i, data); 275105180Snjl if (error == 0 && *(u_int *)data == 0) 276105180Snjl error = ENOENT; 27792698Sphk break; 27892698Sphk case DIOCGFWHEADS: 27993250Sphk error = g_io_getattr("GEOM::fwheads", cp, &i, data); 280105180Snjl if (error == 0 && *(u_int *)data == 0) 281105180Snjl error = ENOENT; 28292698Sphk break; 28394287Sphk case DIOCGFRONTSTUFF: 28494287Sphk error = g_io_getattr("GEOM::frontstuff", cp, &i, data); 28594287Sphk break; 28695038Sphk case DIOCSKERNELDUMP: 28795038Sphk u = *((u_int *)data); 28895038Sphk if (!u) { 28995038Sphk set_dumper(NULL); 29095038Sphk error = 0; 29195038Sphk break; 29295038Sphk } 29395038Sphk kd.offset = 0; 29495038Sphk kd.length = OFF_MAX; 29595038Sphk i = sizeof kd; 29695038Sphk error = g_io_getattr("GEOM::kerneldump", cp, &i, &kd); 29795038Sphk if (!error) 29895038Sphk dev->si_flags |= SI_DUMPDEV; 29995038Sphk break; 300169284Spjd case DIOCGFLUSH: 301169284Spjd error = g_io_flush(cp); 302169284Spjd break; 303169284Spjd case DIOCGDELETE: 304169284Spjd offset = ((off_t *)data)[0]; 305169284Spjd length = ((off_t *)data)[1]; 306169284Spjd if ((offset % cp->provider->sectorsize) != 0 || 307174669Sphk (length % cp->provider->sectorsize) != 0 || length <= 0) { 308169284Spjd printf("%s: offset=%jd length=%jd\n", __func__, offset, 309169284Spjd length); 310169284Spjd error = EINVAL; 311169284Spjd break; 312169284Spjd } 313174674Sphk while (length > 0) { 314174674Sphk chunk = length; 315174674Sphk if (chunk > 1024 * cp->provider->sectorsize) 316174674Sphk chunk = 1024 * cp->provider->sectorsize; 317174674Sphk error = g_delete_data(cp, offset, chunk); 318174674Sphk length -= chunk; 319174674Sphk offset += chunk; 320174674Sphk if (error) 321174674Sphk break; 322174674Sphk /* 323174674Sphk * Since the request size is unbounded, the service 324174674Sphk * time is likewise. We make this ioctl interruptible 325174674Sphk * by checking for signals for each bio. 326174674Sphk */ 327174674Sphk if (SIGPENDING(td)) 328174674Sphk break; 329174674Sphk } 330169284Spjd break; 331169284Spjd case DIOCGIDENT: 332169284Spjd error = g_io_getattr("GEOM::ident", cp, &i, data); 333169284Spjd break; 334182843Slulf case DIOCGPROVIDERNAME: 335182843Slulf if (pp == NULL) 336182843Slulf return (ENOENT); 337182843Slulf strlcpy(data, pp->name, i); 338182843Slulf break; 339104602Sphk 34092698Sphk default: 341119660Sphk if (cp->provider->geom->ioctl != NULL) { 342138732Sphk error = cp->provider->geom->ioctl(cp->provider, cmd, data, fflag, td); 343119749Sphk } else { 344119749Sphk error = ENOIOCTL; 345119660Sphk } 34692698Sphk } 34792403Sphk 34892108Sphk return (error); 34992108Sphk} 35092108Sphk 35192108Sphkstatic void 35292108Sphkg_dev_done(struct bio *bp2) 35392108Sphk{ 35492108Sphk struct bio *bp; 35592108Sphk 356110517Sphk bp = bp2->bio_parent; 35792108Sphk bp->bio_error = bp2->bio_error; 35892108Sphk if (bp->bio_error != 0) { 35992108Sphk g_trace(G_T_BIO, "g_dev_done(%p) had error %d", 36092108Sphk bp2, bp->bio_error); 36192108Sphk bp->bio_flags |= BIO_ERROR; 36292108Sphk } else { 363105540Sphk g_trace(G_T_BIO, "g_dev_done(%p/%p) resid %ld completed %jd", 364105540Sphk bp2, bp, bp->bio_resid, (intmax_t)bp2->bio_completed); 36592108Sphk } 366137029Sphk bp->bio_resid = bp->bio_length - bp2->bio_completed; 367137029Sphk bp->bio_completed = bp2->bio_completed; 36892108Sphk g_destroy_bio(bp2); 36992108Sphk biodone(bp); 37092108Sphk} 37192108Sphk 37292108Sphkstatic void 37392108Sphkg_dev_strategy(struct bio *bp) 37492108Sphk{ 37592108Sphk struct g_consumer *cp; 37692108Sphk struct bio *bp2; 377130585Sphk struct cdev *dev; 37892108Sphk 379106300Sphk KASSERT(bp->bio_cmd == BIO_READ || 380106300Sphk bp->bio_cmd == BIO_WRITE || 381106300Sphk bp->bio_cmd == BIO_DELETE, 382106300Sphk ("Wrong bio_cmd bio=%p cmd=%d", bp, bp->bio_cmd)); 38392108Sphk dev = bp->bio_dev; 38492108Sphk cp = dev->si_drv2; 385112978Sphk KASSERT(cp->acr || cp->acw, 386112978Sphk ("Consumer with zero access count in g_dev_strategy")); 387112978Sphk 388135865Spjd if ((bp->bio_offset % cp->provider->sectorsize) != 0 || 389135865Spjd (bp->bio_bcount % cp->provider->sectorsize) != 0) { 390159756Ssimon bp->bio_resid = bp->bio_bcount; 391135865Spjd biofinish(bp, NULL, EINVAL); 392135865Spjd return; 393135865Spjd } 394135865Spjd 395118869Sphk for (;;) { 396118869Sphk /* 397118869Sphk * XXX: This is not an ideal solution, but I belive it to 398118869Sphk * XXX: deadlock safe, all things considered. 399118869Sphk */ 400118869Sphk bp2 = g_clone_bio(bp); 401118869Sphk if (bp2 != NULL) 402118869Sphk break; 403167086Sjhb pause("gdstrat", hz / 10); 404118869Sphk } 405107834Sphk KASSERT(bp2 != NULL, ("XXX: ENOMEM in a bad place")); 40692108Sphk bp2->bio_done = g_dev_done; 40792108Sphk g_trace(G_T_BIO, 408105540Sphk "g_dev_strategy(%p/%p) offset %jd length %jd data %p cmd %d", 409105540Sphk bp, bp2, (intmax_t)bp->bio_offset, (intmax_t)bp2->bio_length, 410105540Sphk bp2->bio_data, bp2->bio_cmd); 41192108Sphk g_io_request(bp2, cp); 412112978Sphk KASSERT(cp->acr || cp->acw, 413112978Sphk ("g_dev_strategy raced with g_dev_close and lost")); 414112978Sphk 41592108Sphk} 41692108Sphk 41796987Sphk/* 41896987Sphk * g_dev_orphan() 41996987Sphk * 420112024Sphk * Called from below when the provider orphaned us. 421112024Sphk * - Clear any dump settings. 422130585Sphk * - Destroy the struct cdev *to prevent any more request from coming in. The 423112024Sphk * provider is already marked with an error, so anything which comes in 424112024Sphk * in the interrim will be returned immediately. 425112024Sphk * - Wait for any outstanding I/O to finish. 426112024Sphk * - Set our access counts to zero, whatever they were. 427112024Sphk * - Detach and self-destruct. 42896987Sphk */ 42992108Sphk 43092108Sphkstatic void 43193250Sphkg_dev_orphan(struct g_consumer *cp) 43292108Sphk{ 43392108Sphk struct g_geom *gp; 434130585Sphk struct cdev *dev; 435136946Sphk u_int unit; 43692108Sphk 437112024Sphk g_topology_assert(); 43892108Sphk gp = cp->geom; 439112024Sphk dev = gp->softc; 44092108Sphk g_trace(G_T_TOPOLOGY, "g_dev_orphan(%p(%s))", cp, gp->name); 441112024Sphk 442112024Sphk /* Reset any dump-area set on this device */ 44395038Sphk if (dev->si_flags & SI_DUMPDEV) 44495038Sphk set_dumper(NULL); 445112024Sphk 446130585Sphk /* Destroy the struct cdev *so we get no more requests */ 447136946Sphk unit = dev2unit(dev); 44892108Sphk destroy_dev(dev); 449136946Sphk free_unr(unithdr, unit); 450112024Sphk 451112024Sphk /* Wait for the cows to come home */ 452112024Sphk while (cp->nstart != cp->nend) 453167086Sjhb pause("gdevorphan", hz / 10); 454112024Sphk 45592108Sphk if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0) 456125755Sphk g_access(cp, -cp->acr, -cp->acw, -cp->ace); 457112024Sphk 45898066Sphk g_detach(cp); 45992108Sphk g_destroy_consumer(cp); 46092108Sphk g_destroy_geom(gp); 46192108Sphk} 46292108Sphk 46396987SphkDECLARE_GEOM_CLASS(g_dev_class, g_dev); 464