geom_dev.c revision 200934
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 200934 2009-12-24 11:05:23Z mav $"); 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; 7592108Sphk 7693248Sphkstatic struct g_class g_dev_class = { 77112552Sphk .name = "DEV", 78133318Sphk .version = G_VERSION, 79112552Sphk .taste = g_dev_taste, 80133314Sphk .orphan = g_dev_orphan, 8192108Sphk}; 8292108Sphk 83115960Sphkvoid 84105947Sphkg_dev_print(void) 85105947Sphk{ 86105947Sphk struct g_geom *gp; 87115960Sphk char const *p = ""; 88105947Sphk 89115960Sphk LIST_FOREACH(gp, &g_dev_class.geom, geom) { 90115960Sphk printf("%s%s", p, gp->name); 91115960Sphk p = " "; 92115960Sphk } 93105947Sphk printf("\n"); 94105947Sphk} 95105947Sphk 96119593Sphkstruct g_provider * 97130585Sphkg_dev_getprovider(struct cdev *dev) 98119593Sphk{ 99119593Sphk struct g_consumer *cp; 100119593Sphk 101135716Sphk g_topology_assert(); 102119593Sphk if (dev == NULL) 103119593Sphk return (NULL); 104135716Sphk if (dev->si_devsw != &g_dev_cdevsw) 105143790Sphk return (NULL); 106143790Sphk cp = dev->si_drv2; 107119593Sphk return (cp->provider); 108119593Sphk} 109119593Sphk 110119593Sphk 11192108Sphkstatic struct g_geom * 11293250Sphkg_dev_taste(struct g_class *mp, struct g_provider *pp, int insist __unused) 11392108Sphk{ 11492108Sphk struct g_geom *gp; 11592108Sphk struct g_consumer *cp; 11696987Sphk int error; 117130585Sphk struct cdev *dev; 11892108Sphk 11992108Sphk g_trace(G_T_TOPOLOGY, "dev_taste(%s,%s)", mp->name, pp->name); 12092108Sphk g_topology_assert(); 12192108Sphk LIST_FOREACH(cp, &pp->consumers, consumers) 12293248Sphk if (cp->geom->class == mp) 12392108Sphk return (NULL); 12492108Sphk gp = g_new_geomf(mp, pp->name); 12592108Sphk cp = g_new_consumer(gp); 12696987Sphk error = g_attach(cp, pp); 12796987Sphk KASSERT(error == 0, 12896987Sphk ("g_dev_taste(%s) failed to g_attach, err=%d", pp->name, error)); 129187672Sed dev = make_dev(&g_dev_cdevsw, 0, 130104316Sphk UID_ROOT, GID_OPERATOR, 0640, gp->name); 131110700Sphk if (pp->flags & G_PF_CANDELETE) 132110700Sphk dev->si_flags |= SI_CANDELETE; 133110728Sphk dev->si_iosize_max = MAXPHYS; 13492108Sphk gp->softc = dev; 13592108Sphk dev->si_drv1 = gp; 13692108Sphk dev->si_drv2 = cp; 13792108Sphk return (gp); 13892108Sphk} 13992108Sphk 14092108Sphkstatic int 141130585Sphkg_dev_open(struct cdev *dev, int flags, int fmt, struct thread *td) 14292108Sphk{ 14392108Sphk struct g_geom *gp; 14492108Sphk struct g_consumer *cp; 14592108Sphk int error, r, w, e; 14692108Sphk 14792108Sphk gp = dev->si_drv1; 14892108Sphk cp = dev->si_drv2; 149112978Sphk if (gp == NULL || cp == NULL || gp->softc != dev) 150112978Sphk return(ENXIO); /* g_dev_taste() not done yet */ 151112978Sphk 15292108Sphk g_trace(G_T_ACCESS, "g_dev_open(%s, %d, %d, %p)", 15392108Sphk gp->name, flags, fmt, td); 154130712Sphk 15592108Sphk r = flags & FREAD ? 1 : 0; 15692108Sphk w = flags & FWRITE ? 1 : 0; 157103004Sphk#ifdef notyet 15892108Sphk e = flags & O_EXCL ? 1 : 0; 159103004Sphk#else 160103004Sphk e = 0; 161103004Sphk#endif 162130712Sphk if (w) { 163130712Sphk /* 164130712Sphk * When running in very secure mode, do not allow 165130712Sphk * opens for writing of any disks. 166130712Sphk */ 167130712Sphk error = securelevel_ge(td->td_ucred, 2); 168130712Sphk if (error) 169130712Sphk return (error); 170130712Sphk } 171112978Sphk g_topology_lock(); 172112978Sphk if (dev->si_devsw == NULL) 173112978Sphk error = ENXIO; /* We were orphaned */ 174112978Sphk else 175125755Sphk error = g_access(cp, r, w, e); 17692108Sphk g_topology_unlock(); 17792108Sphk return(error); 17892108Sphk} 17992108Sphk 18092108Sphkstatic int 181130585Sphkg_dev_close(struct cdev *dev, int flags, int fmt, struct thread *td) 18292108Sphk{ 18392108Sphk struct g_geom *gp; 18492108Sphk struct g_consumer *cp; 185114864Sphk int error, r, w, e, i; 18692108Sphk 18792108Sphk gp = dev->si_drv1; 18892108Sphk cp = dev->si_drv2; 18992108Sphk if (gp == NULL || cp == NULL) 19092108Sphk return(ENXIO); 19192108Sphk g_trace(G_T_ACCESS, "g_dev_close(%s, %d, %d, %p)", 19292108Sphk gp->name, flags, fmt, td); 19392108Sphk r = flags & FREAD ? -1 : 0; 19492108Sphk w = flags & FWRITE ? -1 : 0; 195103004Sphk#ifdef notyet 19692108Sphk e = flags & O_EXCL ? -1 : 0; 197103004Sphk#else 198103004Sphk e = 0; 199103004Sphk#endif 200112978Sphk g_topology_lock(); 201112978Sphk if (dev->si_devsw == NULL) 202112978Sphk error = ENXIO; /* We were orphaned */ 203112978Sphk else 204125755Sphk error = g_access(cp, r, w, e); 205114864Sphk for (i = 0; i < 10 * hz;) { 206114864Sphk if (cp->acr != 0 || cp->acw != 0) 207114864Sphk break; 208114864Sphk if (cp->nstart == cp->nend) 209114864Sphk break; 210167086Sjhb pause("gdevwclose", hz / 10); 211114864Sphk i += hz / 10; 212114864Sphk } 213114864Sphk if (cp->acr == 0 && cp->acw == 0 && cp->nstart != cp->nend) { 214124880Sphk printf("WARNING: Final close of geom_dev(%s) %s %s\n", 215114864Sphk gp->name, 216114864Sphk "still has outstanding I/O after 10 seconds.", 217114864Sphk "Completing close anyway, panic may happen later."); 218114864Sphk } 21992108Sphk g_topology_unlock(); 22092108Sphk return (error); 22192108Sphk} 22292108Sphk 223112978Sphk/* 224112978Sphk * XXX: Until we have unmessed the ioctl situation, there is a race against 225112978Sphk * XXX: a concurrent orphanization. We cannot close it by holding topology 226112978Sphk * XXX: since that would prevent us from doing our job, and stalling events 227112978Sphk * XXX: will break (actually: stall) the BSD disklabel hacks. 228112978Sphk */ 22992108Sphkstatic int 230130585Sphkg_dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td) 23192108Sphk{ 232115515Sphk struct g_geom *gp; 23392108Sphk struct g_consumer *cp; 234182843Slulf struct g_provider *pp; 23595038Sphk struct g_kerneldump kd; 236174674Sphk off_t offset, length, chunk; 23792108Sphk int i, error; 23895038Sphk u_int u; 23992108Sphk 24092108Sphk gp = dev->si_drv1; 24192108Sphk cp = dev->si_drv2; 242182843Slulf pp = cp->provider; 24392108Sphk 24492108Sphk error = 0; 245112978Sphk KASSERT(cp->acr || cp->acw, 246112978Sphk ("Consumer with zero access count in g_dev_ioctl")); 24792403Sphk 24892698Sphk i = IOCPARM_LEN(cmd); 24992698Sphk switch (cmd) { 25092698Sphk case DIOCGSECTORSIZE: 251105551Sphk *(u_int *)data = cp->provider->sectorsize; 252105551Sphk if (*(u_int *)data == 0) 253105180Snjl error = ENOENT; 25492698Sphk break; 25592698Sphk case DIOCGMEDIASIZE: 256105551Sphk *(off_t *)data = cp->provider->mediasize; 257105551Sphk if (*(off_t *)data == 0) 258105180Snjl error = ENOENT; 25992698Sphk break; 26092698Sphk case DIOCGFWSECTORS: 26193250Sphk error = g_io_getattr("GEOM::fwsectors", cp, &i, data); 262105180Snjl if (error == 0 && *(u_int *)data == 0) 263105180Snjl error = ENOENT; 26492698Sphk break; 26592698Sphk case DIOCGFWHEADS: 26693250Sphk error = g_io_getattr("GEOM::fwheads", cp, &i, data); 267105180Snjl if (error == 0 && *(u_int *)data == 0) 268105180Snjl error = ENOENT; 26992698Sphk break; 27094287Sphk case DIOCGFRONTSTUFF: 27194287Sphk error = g_io_getattr("GEOM::frontstuff", cp, &i, data); 27294287Sphk break; 27395038Sphk case DIOCSKERNELDUMP: 27495038Sphk u = *((u_int *)data); 27595038Sphk if (!u) { 27695038Sphk set_dumper(NULL); 27795038Sphk error = 0; 27895038Sphk break; 27995038Sphk } 28095038Sphk kd.offset = 0; 28195038Sphk kd.length = OFF_MAX; 28295038Sphk i = sizeof kd; 28395038Sphk error = g_io_getattr("GEOM::kerneldump", cp, &i, &kd); 28495038Sphk if (!error) 28595038Sphk dev->si_flags |= SI_DUMPDEV; 28695038Sphk break; 287169284Spjd case DIOCGFLUSH: 288169284Spjd error = g_io_flush(cp); 289169284Spjd break; 290169284Spjd case DIOCGDELETE: 291169284Spjd offset = ((off_t *)data)[0]; 292169284Spjd length = ((off_t *)data)[1]; 293169284Spjd if ((offset % cp->provider->sectorsize) != 0 || 294174669Sphk (length % cp->provider->sectorsize) != 0 || length <= 0) { 295169284Spjd printf("%s: offset=%jd length=%jd\n", __func__, offset, 296169284Spjd length); 297169284Spjd error = EINVAL; 298169284Spjd break; 299169284Spjd } 300174674Sphk while (length > 0) { 301174674Sphk chunk = length; 302174674Sphk if (chunk > 1024 * cp->provider->sectorsize) 303174674Sphk chunk = 1024 * cp->provider->sectorsize; 304174674Sphk error = g_delete_data(cp, offset, chunk); 305174674Sphk length -= chunk; 306174674Sphk offset += chunk; 307174674Sphk if (error) 308174674Sphk break; 309174674Sphk /* 310174674Sphk * Since the request size is unbounded, the service 311174674Sphk * time is likewise. We make this ioctl interruptible 312174674Sphk * by checking for signals for each bio. 313174674Sphk */ 314174674Sphk if (SIGPENDING(td)) 315174674Sphk break; 316174674Sphk } 317169284Spjd break; 318169284Spjd case DIOCGIDENT: 319169284Spjd error = g_io_getattr("GEOM::ident", cp, &i, data); 320169284Spjd break; 321182843Slulf case DIOCGPROVIDERNAME: 322182843Slulf if (pp == NULL) 323182843Slulf return (ENOENT); 324182843Slulf strlcpy(data, pp->name, i); 325182843Slulf break; 326200934Smav case DIOCGSTRIPESIZE: 327200934Smav *(off_t *)data = cp->provider->stripesize; 328200934Smav break; 329200934Smav case DIOCGSTRIPEOFFSET: 330200934Smav *(off_t *)data = cp->provider->stripeoffset; 331200934Smav break; 33292698Sphk default: 333119660Sphk if (cp->provider->geom->ioctl != NULL) { 334138732Sphk error = cp->provider->geom->ioctl(cp->provider, cmd, data, fflag, td); 335119749Sphk } else { 336119749Sphk error = ENOIOCTL; 337119660Sphk } 33892698Sphk } 33992403Sphk 34092108Sphk return (error); 34192108Sphk} 34292108Sphk 34392108Sphkstatic void 34492108Sphkg_dev_done(struct bio *bp2) 34592108Sphk{ 34692108Sphk struct bio *bp; 34792108Sphk 348110517Sphk bp = bp2->bio_parent; 34992108Sphk bp->bio_error = bp2->bio_error; 35092108Sphk if (bp->bio_error != 0) { 35192108Sphk g_trace(G_T_BIO, "g_dev_done(%p) had error %d", 35292108Sphk bp2, bp->bio_error); 35392108Sphk bp->bio_flags |= BIO_ERROR; 35492108Sphk } else { 355105540Sphk g_trace(G_T_BIO, "g_dev_done(%p/%p) resid %ld completed %jd", 356105540Sphk bp2, bp, bp->bio_resid, (intmax_t)bp2->bio_completed); 35792108Sphk } 358137029Sphk bp->bio_resid = bp->bio_length - bp2->bio_completed; 359137029Sphk bp->bio_completed = bp2->bio_completed; 36092108Sphk g_destroy_bio(bp2); 36192108Sphk biodone(bp); 36292108Sphk} 36392108Sphk 36492108Sphkstatic void 36592108Sphkg_dev_strategy(struct bio *bp) 36692108Sphk{ 36792108Sphk struct g_consumer *cp; 36892108Sphk struct bio *bp2; 369130585Sphk struct cdev *dev; 37092108Sphk 371106300Sphk KASSERT(bp->bio_cmd == BIO_READ || 372106300Sphk bp->bio_cmd == BIO_WRITE || 373106300Sphk bp->bio_cmd == BIO_DELETE, 374106300Sphk ("Wrong bio_cmd bio=%p cmd=%d", bp, bp->bio_cmd)); 37592108Sphk dev = bp->bio_dev; 37692108Sphk cp = dev->si_drv2; 377112978Sphk KASSERT(cp->acr || cp->acw, 378112978Sphk ("Consumer with zero access count in g_dev_strategy")); 379196964Smav#ifdef INVARIANTS 380135865Spjd if ((bp->bio_offset % cp->provider->sectorsize) != 0 || 381135865Spjd (bp->bio_bcount % cp->provider->sectorsize) != 0) { 382159756Ssimon bp->bio_resid = bp->bio_bcount; 383135865Spjd biofinish(bp, NULL, EINVAL); 384135865Spjd return; 385135865Spjd } 386196964Smav#endif 387118869Sphk for (;;) { 388118869Sphk /* 389118869Sphk * XXX: This is not an ideal solution, but I belive it to 390118869Sphk * XXX: deadlock safe, all things considered. 391118869Sphk */ 392118869Sphk bp2 = g_clone_bio(bp); 393118869Sphk if (bp2 != NULL) 394118869Sphk break; 395167086Sjhb pause("gdstrat", hz / 10); 396118869Sphk } 397107834Sphk KASSERT(bp2 != NULL, ("XXX: ENOMEM in a bad place")); 39892108Sphk bp2->bio_done = g_dev_done; 39992108Sphk g_trace(G_T_BIO, 400105540Sphk "g_dev_strategy(%p/%p) offset %jd length %jd data %p cmd %d", 401105540Sphk bp, bp2, (intmax_t)bp->bio_offset, (intmax_t)bp2->bio_length, 402105540Sphk bp2->bio_data, bp2->bio_cmd); 40392108Sphk g_io_request(bp2, cp); 404112978Sphk KASSERT(cp->acr || cp->acw, 405112978Sphk ("g_dev_strategy raced with g_dev_close and lost")); 406112978Sphk 40792108Sphk} 40892108Sphk 40996987Sphk/* 41096987Sphk * g_dev_orphan() 41196987Sphk * 412112024Sphk * Called from below when the provider orphaned us. 413112024Sphk * - Clear any dump settings. 414130585Sphk * - Destroy the struct cdev *to prevent any more request from coming in. The 415112024Sphk * provider is already marked with an error, so anything which comes in 416112024Sphk * in the interrim will be returned immediately. 417112024Sphk * - Wait for any outstanding I/O to finish. 418112024Sphk * - Set our access counts to zero, whatever they were. 419112024Sphk * - Detach and self-destruct. 42096987Sphk */ 42192108Sphk 42292108Sphkstatic void 42393250Sphkg_dev_orphan(struct g_consumer *cp) 42492108Sphk{ 42592108Sphk struct g_geom *gp; 426130585Sphk struct cdev *dev; 42792108Sphk 428112024Sphk g_topology_assert(); 42992108Sphk gp = cp->geom; 430112024Sphk dev = gp->softc; 43192108Sphk g_trace(G_T_TOPOLOGY, "g_dev_orphan(%p(%s))", cp, gp->name); 432112024Sphk 433112024Sphk /* Reset any dump-area set on this device */ 43495038Sphk if (dev->si_flags & SI_DUMPDEV) 43595038Sphk set_dumper(NULL); 436112024Sphk 437130585Sphk /* Destroy the struct cdev *so we get no more requests */ 43892108Sphk destroy_dev(dev); 439112024Sphk 440112024Sphk /* Wait for the cows to come home */ 441112024Sphk while (cp->nstart != cp->nend) 442167086Sjhb pause("gdevorphan", hz / 10); 443112024Sphk 44492108Sphk if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0) 445125755Sphk g_access(cp, -cp->acr, -cp->acw, -cp->ace); 446112024Sphk 44798066Sphk g_detach(cp); 44892108Sphk g_destroy_consumer(cp); 44992108Sphk g_destroy_geom(gp); 45092108Sphk} 45192108Sphk 45296987SphkDECLARE_GEOM_CLASS(g_dev_class, g_dev); 453