geom_dev.c revision 249930
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 249930 2013-04-26 15:43:24Z smh $"); 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> 44223089Sgibbs#include <sys/ctype.h> 4592108Sphk#include <sys/bio.h> 46238886Smav#include <sys/bus.h> 4792108Sphk#include <sys/lock.h> 4892108Sphk#include <sys/mutex.h> 49130712Sphk#include <sys/proc.h> 5092108Sphk#include <sys/errno.h> 5192108Sphk#include <sys/time.h> 5292108Sphk#include <sys/disk.h> 5392108Sphk#include <sys/fcntl.h> 54114216Skan#include <sys/limits.h> 55249930Ssmh#include <sys/sysctl.h> 5692108Sphk#include <geom/geom.h> 5795323Sphk#include <geom/geom_int.h> 58223089Sgibbs#include <machine/stdarg.h> 5992108Sphk 60248679Smavstruct g_dev_softc { 61248679Smav struct mtx sc_mtx; 62248679Smav struct cdev *sc_dev; 63248679Smav struct cdev *sc_alias; 64248679Smav int sc_open; 65248679Smav int sc_active; 66248679Smav}; 67223089Sgibbs 6892108Sphkstatic d_open_t g_dev_open; 6992108Sphkstatic d_close_t g_dev_close; 7092108Sphkstatic d_strategy_t g_dev_strategy; 7192108Sphkstatic d_ioctl_t g_dev_ioctl; 7292108Sphk 7392108Sphkstatic struct cdevsw g_dev_cdevsw = { 74126080Sphk .d_version = D_VERSION, 75111815Sphk .d_open = g_dev_open, 76111815Sphk .d_close = g_dev_close, 77111815Sphk .d_read = physread, 78111815Sphk .d_write = physwrite, 79111815Sphk .d_ioctl = g_dev_ioctl, 80111815Sphk .d_strategy = g_dev_strategy, 81111815Sphk .d_name = "g_dev", 82248712Skan .d_flags = D_DISK | D_TRACKCLOSE | D_UNMAPPED_IO, 8392108Sphk}; 8492108Sphk 8592108Sphkstatic g_taste_t g_dev_taste; 8692108Sphkstatic g_orphan_t g_dev_orphan; 87223089Sgibbsstatic g_attrchanged_t g_dev_attrchanged; 8892108Sphk 8993248Sphkstatic struct g_class g_dev_class = { 90112552Sphk .name = "DEV", 91133318Sphk .version = G_VERSION, 92112552Sphk .taste = g_dev_taste, 93133314Sphk .orphan = g_dev_orphan, 94223089Sgibbs .attrchanged = g_dev_attrchanged 9592108Sphk}; 9692108Sphk 97249930Ssmh/* 98249930Ssmh * We target 262144 (8 x 32768) sectors by default as this significantly 99249930Ssmh * increases the throughput on commonly used SSD's with a marginal 100249930Ssmh * increase in non-interruptible request latency. 101249930Ssmh */ 102249930Ssmhstatic uint64_t g_dev_del_max_sectors = 262144; 103249930SsmhSYSCTL_DECL(_kern_geom); 104249930SsmhSYSCTL_NODE(_kern_geom, OID_AUTO, dev, CTLFLAG_RW, 0, "GEOM_DEV stuff"); 105249930SsmhSYSCTL_QUAD(_kern_geom_dev, OID_AUTO, delete_max_sectors, CTLFLAG_RW, 106249930Ssmh &g_dev_del_max_sectors, 0, "Maximum number of sectors in a single " 107249930Ssmh "delete request sent to the provider. Larger requests are chunked " 108249930Ssmh "so they can be interrupted. (0 = disable chunking)"); 109249930Ssmh 110248679Smavstatic void 111248679Smavg_dev_destroy(void *arg, int flags __unused) 112248679Smav{ 113248679Smav struct g_consumer *cp; 114248679Smav struct g_geom *gp; 115248679Smav struct g_dev_softc *sc; 116248679Smav 117248679Smav g_topology_assert(); 118248679Smav cp = arg; 119248679Smav gp = cp->geom; 120248679Smav sc = cp->private; 121248679Smav g_trace(G_T_TOPOLOGY, "g_dev_destroy(%p(%s))", cp, gp->name); 122248679Smav if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0) 123248679Smav g_access(cp, -cp->acr, -cp->acw, -cp->ace); 124248679Smav g_detach(cp); 125248679Smav g_destroy_consumer(cp); 126248679Smav g_destroy_geom(gp); 127248679Smav mtx_destroy(&sc->sc_mtx); 128248679Smav g_free(sc); 129248679Smav} 130248679Smav 131115960Sphkvoid 132105947Sphkg_dev_print(void) 133105947Sphk{ 134105947Sphk struct g_geom *gp; 135115960Sphk char const *p = ""; 136105947Sphk 137115960Sphk LIST_FOREACH(gp, &g_dev_class.geom, geom) { 138115960Sphk printf("%s%s", p, gp->name); 139115960Sphk p = " "; 140115960Sphk } 141105947Sphk printf("\n"); 142105947Sphk} 143105947Sphk 144223089Sgibbsstatic void 145223089Sgibbsg_dev_attrchanged(struct g_consumer *cp, const char *attr) 146223089Sgibbs{ 147248679Smav struct g_dev_softc *sc; 148238886Smav struct cdev *dev; 149238886Smav char buf[SPECNAMELEN + 6]; 150223089Sgibbs 151248679Smav sc = cp->private; 152238886Smav if (strcmp(attr, "GEOM::media") == 0) { 153248679Smav dev = sc->sc_dev; 154238886Smav snprintf(buf, sizeof(buf), "cdev=%s", dev->si_name); 155238886Smav devctl_notify_f("DEVFS", "CDEV", "MEDIACHANGE", buf, M_WAITOK); 156248679Smav dev = sc->sc_alias; 157238886Smav if (dev != NULL) { 158238886Smav snprintf(buf, sizeof(buf), "cdev=%s", dev->si_name); 159238886Smav devctl_notify_f("DEVFS", "CDEV", "MEDIACHANGE", buf, 160238886Smav M_WAITOK); 161238886Smav } 162238886Smav return; 163238886Smav } 164238886Smav 165223089Sgibbs if (strcmp(attr, "GEOM::physpath") != 0) 166223089Sgibbs return; 167223089Sgibbs 168223089Sgibbs if (g_access(cp, 1, 0, 0) == 0) { 169223089Sgibbs char *physpath; 170223089Sgibbs int error, physpath_len; 171223089Sgibbs 172223089Sgibbs physpath_len = MAXPATHLEN; 173223089Sgibbs physpath = g_malloc(physpath_len, M_WAITOK|M_ZERO); 174223089Sgibbs error = 175223089Sgibbs g_io_getattr("GEOM::physpath", cp, &physpath_len, physpath); 176223089Sgibbs g_access(cp, -1, 0, 0); 177223089Sgibbs if (error == 0 && strlen(physpath) != 0) { 178223089Sgibbs struct cdev *old_alias_dev; 179223089Sgibbs struct cdev **alias_devp; 180223089Sgibbs 181248679Smav dev = sc->sc_dev; 182248679Smav old_alias_dev = sc->sc_alias; 183248679Smav alias_devp = (struct cdev **)&sc->sc_alias; 184223089Sgibbs make_dev_physpath_alias(MAKEDEV_WAITOK, alias_devp, 185223089Sgibbs dev, old_alias_dev, physpath); 186248679Smav } else if (sc->sc_alias) { 187248679Smav destroy_dev((struct cdev *)sc->sc_alias); 188248679Smav sc->sc_alias = NULL; 189223089Sgibbs } 190223089Sgibbs g_free(physpath); 191223089Sgibbs } 192223089Sgibbs} 193223089Sgibbs 194119593Sphkstruct g_provider * 195130585Sphkg_dev_getprovider(struct cdev *dev) 196119593Sphk{ 197119593Sphk struct g_consumer *cp; 198119593Sphk 199135716Sphk g_topology_assert(); 200119593Sphk if (dev == NULL) 201119593Sphk return (NULL); 202135716Sphk if (dev->si_devsw != &g_dev_cdevsw) 203143790Sphk return (NULL); 204143790Sphk cp = dev->si_drv2; 205119593Sphk return (cp->provider); 206119593Sphk} 207119593Sphk 20892108Sphkstatic struct g_geom * 20993250Sphkg_dev_taste(struct g_class *mp, struct g_provider *pp, int insist __unused) 21092108Sphk{ 21192108Sphk struct g_geom *gp; 21292108Sphk struct g_consumer *cp; 213248679Smav struct g_dev_softc *sc; 214221071Smav int error, len; 215221071Smav struct cdev *dev, *adev; 216221071Smav char buf[64], *val; 21792108Sphk 21892108Sphk g_trace(G_T_TOPOLOGY, "dev_taste(%s,%s)", mp->name, pp->name); 21992108Sphk g_topology_assert(); 220243333Sjh gp = g_new_geomf(mp, "%s", pp->name); 221248679Smav sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO); 222248679Smav mtx_init(&sc->sc_mtx, "g_dev", NULL, MTX_DEF); 22392108Sphk cp = g_new_consumer(gp); 224248679Smav cp->private = sc; 22596987Sphk error = g_attach(cp, pp); 22696987Sphk KASSERT(error == 0, 22796987Sphk ("g_dev_taste(%s) failed to g_attach, err=%d", pp->name, error)); 228214063Sjh error = make_dev_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK, &dev, 229214063Sjh &g_dev_cdevsw, NULL, UID_ROOT, GID_OPERATOR, 0640, "%s", gp->name); 230214063Sjh if (error != 0) { 231214063Sjh printf("%s: make_dev_p() failed (gp->name=%s, error=%d)\n", 232214063Sjh __func__, gp->name, error); 233214063Sjh g_detach(cp); 234214063Sjh g_destroy_consumer(cp); 235214063Sjh g_destroy_geom(gp); 236248679Smav mtx_destroy(&sc->sc_mtx); 237248679Smav g_free(sc); 238214063Sjh return (NULL); 239214063Sjh } 240248679Smav sc->sc_dev = dev; 241221071Smav 242221071Smav /* Search for device alias name and create it if found. */ 243221071Smav adev = NULL; 244221071Smav for (len = MIN(strlen(gp->name), sizeof(buf) - 15); len > 0; len--) { 245221071Smav snprintf(buf, sizeof(buf), "kern.devalias.%s", gp->name); 246221071Smav buf[14 + len] = 0; 247221071Smav val = getenv(buf); 248221071Smav if (val != NULL) { 249221071Smav snprintf(buf, sizeof(buf), "%s%s", 250221071Smav val, gp->name + len); 251221071Smav freeenv(val); 252221400Smav make_dev_alias_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK, 253221400Smav &adev, dev, "%s", buf); 254221071Smav break; 255221071Smav } 256221071Smav } 257221071Smav 258110728Sphk dev->si_iosize_max = MAXPHYS; 25992108Sphk dev->si_drv2 = cp; 260221071Smav if (adev != NULL) { 261221071Smav adev->si_iosize_max = MAXPHYS; 262221071Smav adev->si_drv2 = cp; 263221071Smav } 264223089Sgibbs 265223089Sgibbs g_dev_attrchanged(cp, "GEOM::physpath"); 266223089Sgibbs 26792108Sphk return (gp); 26892108Sphk} 26992108Sphk 27092108Sphkstatic int 271130585Sphkg_dev_open(struct cdev *dev, int flags, int fmt, struct thread *td) 27292108Sphk{ 27392108Sphk struct g_consumer *cp; 274248679Smav struct g_dev_softc *sc; 27592108Sphk int error, r, w, e; 27692108Sphk 27792108Sphk cp = dev->si_drv2; 278248679Smav if (cp == NULL) 279112978Sphk return(ENXIO); /* g_dev_taste() not done yet */ 28092108Sphk g_trace(G_T_ACCESS, "g_dev_open(%s, %d, %d, %p)", 281248679Smav cp->geom->name, flags, fmt, td); 282130712Sphk 28392108Sphk r = flags & FREAD ? 1 : 0; 28492108Sphk w = flags & FWRITE ? 1 : 0; 285103004Sphk#ifdef notyet 28692108Sphk e = flags & O_EXCL ? 1 : 0; 287103004Sphk#else 288103004Sphk e = 0; 289103004Sphk#endif 290130712Sphk if (w) { 291130712Sphk /* 292130712Sphk * When running in very secure mode, do not allow 293130712Sphk * opens for writing of any disks. 294130712Sphk */ 295130712Sphk error = securelevel_ge(td->td_ucred, 2); 296130712Sphk if (error) 297130712Sphk return (error); 298130712Sphk } 299112978Sphk g_topology_lock(); 300248679Smav error = g_access(cp, r, w, e); 30192108Sphk g_topology_unlock(); 302248679Smav if (error == 0) { 303248679Smav sc = cp->private; 304248679Smav mtx_lock(&sc->sc_mtx); 305248679Smav if (sc->sc_open == 0 && sc->sc_active != 0) 306248679Smav wakeup(&sc->sc_active); 307248679Smav sc->sc_open += r + w + e; 308248679Smav mtx_unlock(&sc->sc_mtx); 309248679Smav } 31092108Sphk return(error); 31192108Sphk} 31292108Sphk 31392108Sphkstatic int 314130585Sphkg_dev_close(struct cdev *dev, int flags, int fmt, struct thread *td) 31592108Sphk{ 31692108Sphk struct g_consumer *cp; 317248679Smav struct g_dev_softc *sc; 318248679Smav int error, r, w, e; 31992108Sphk 32092108Sphk cp = dev->si_drv2; 321248679Smav if (cp == NULL) 32292108Sphk return(ENXIO); 32392108Sphk g_trace(G_T_ACCESS, "g_dev_close(%s, %d, %d, %p)", 324248679Smav cp->geom->name, flags, fmt, td); 325248679Smav 32692108Sphk r = flags & FREAD ? -1 : 0; 32792108Sphk w = flags & FWRITE ? -1 : 0; 328103004Sphk#ifdef notyet 32992108Sphk e = flags & O_EXCL ? -1 : 0; 330103004Sphk#else 331103004Sphk e = 0; 332103004Sphk#endif 333248679Smav sc = cp->private; 334248679Smav mtx_lock(&sc->sc_mtx); 335248679Smav sc->sc_open += r + w + e; 336248679Smav while (sc->sc_open == 0 && sc->sc_active != 0) 337248679Smav msleep(&sc->sc_active, &sc->sc_mtx, 0, "PRIBIO", 0); 338248679Smav mtx_unlock(&sc->sc_mtx); 339112978Sphk g_topology_lock(); 340248679Smav error = g_access(cp, r, w, e); 34192108Sphk g_topology_unlock(); 34292108Sphk return (error); 34392108Sphk} 34492108Sphk 345112978Sphk/* 346112978Sphk * XXX: Until we have unmessed the ioctl situation, there is a race against 347112978Sphk * XXX: a concurrent orphanization. We cannot close it by holding topology 348112978Sphk * XXX: since that would prevent us from doing our job, and stalling events 349112978Sphk * XXX: will break (actually: stall) the BSD disklabel hacks. 350112978Sphk */ 35192108Sphkstatic int 352130585Sphkg_dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td) 35392108Sphk{ 35492108Sphk struct g_consumer *cp; 355182843Slulf struct g_provider *pp; 35695038Sphk struct g_kerneldump kd; 357174674Sphk off_t offset, length, chunk; 35892108Sphk int i, error; 35995038Sphk u_int u; 36092108Sphk 36192108Sphk cp = dev->si_drv2; 362182843Slulf pp = cp->provider; 36392108Sphk 36492108Sphk error = 0; 365112978Sphk KASSERT(cp->acr || cp->acw, 366112978Sphk ("Consumer with zero access count in g_dev_ioctl")); 36792403Sphk 36892698Sphk i = IOCPARM_LEN(cmd); 36992698Sphk switch (cmd) { 37092698Sphk case DIOCGSECTORSIZE: 371105551Sphk *(u_int *)data = cp->provider->sectorsize; 372105551Sphk if (*(u_int *)data == 0) 373105180Snjl error = ENOENT; 37492698Sphk break; 37592698Sphk case DIOCGMEDIASIZE: 376105551Sphk *(off_t *)data = cp->provider->mediasize; 377105551Sphk if (*(off_t *)data == 0) 378105180Snjl error = ENOENT; 37992698Sphk break; 38092698Sphk case DIOCGFWSECTORS: 38193250Sphk error = g_io_getattr("GEOM::fwsectors", cp, &i, data); 382105180Snjl if (error == 0 && *(u_int *)data == 0) 383105180Snjl error = ENOENT; 38492698Sphk break; 38592698Sphk case DIOCGFWHEADS: 38693250Sphk error = g_io_getattr("GEOM::fwheads", cp, &i, data); 387105180Snjl if (error == 0 && *(u_int *)data == 0) 388105180Snjl error = ENOENT; 38992698Sphk break; 39094287Sphk case DIOCGFRONTSTUFF: 39194287Sphk error = g_io_getattr("GEOM::frontstuff", cp, &i, data); 39294287Sphk break; 39395038Sphk case DIOCSKERNELDUMP: 39495038Sphk u = *((u_int *)data); 39595038Sphk if (!u) { 396242439Salfred set_dumper(NULL, NULL); 39795038Sphk error = 0; 39895038Sphk break; 39995038Sphk } 40095038Sphk kd.offset = 0; 40195038Sphk kd.length = OFF_MAX; 40295038Sphk i = sizeof kd; 40395038Sphk error = g_io_getattr("GEOM::kerneldump", cp, &i, &kd); 404219950Smav if (!error) { 405242439Salfred error = set_dumper(&kd.di, devtoname(dev)); 406219950Smav if (!error) 407219950Smav dev->si_flags |= SI_DUMPDEV; 408219950Smav } 40995038Sphk break; 410169284Spjd case DIOCGFLUSH: 411169284Spjd error = g_io_flush(cp); 412169284Spjd break; 413169284Spjd case DIOCGDELETE: 414169284Spjd offset = ((off_t *)data)[0]; 415169284Spjd length = ((off_t *)data)[1]; 416169284Spjd if ((offset % cp->provider->sectorsize) != 0 || 417174669Sphk (length % cp->provider->sectorsize) != 0 || length <= 0) { 418169284Spjd printf("%s: offset=%jd length=%jd\n", __func__, offset, 419169284Spjd length); 420169284Spjd error = EINVAL; 421169284Spjd break; 422169284Spjd } 423174674Sphk while (length > 0) { 424174674Sphk chunk = length; 425249930Ssmh if (g_dev_del_max_sectors != 0 && chunk > 426249930Ssmh g_dev_del_max_sectors * cp->provider->sectorsize) { 427249930Ssmh chunk = g_dev_del_max_sectors * 428249930Ssmh cp->provider->sectorsize; 429249930Ssmh } 430174674Sphk error = g_delete_data(cp, offset, chunk); 431174674Sphk length -= chunk; 432174674Sphk offset += chunk; 433174674Sphk if (error) 434174674Sphk break; 435174674Sphk /* 436249930Ssmh * Since the request size can be large, the service 437249930Ssmh * time can be is likewise. We make this ioctl 438249930Ssmh * interruptible by checking for signals for each bio. 439174674Sphk */ 440174674Sphk if (SIGPENDING(td)) 441174674Sphk break; 442174674Sphk } 443169284Spjd break; 444169284Spjd case DIOCGIDENT: 445169284Spjd error = g_io_getattr("GEOM::ident", cp, &i, data); 446169284Spjd break; 447182843Slulf case DIOCGPROVIDERNAME: 448182843Slulf if (pp == NULL) 449182843Slulf return (ENOENT); 450182843Slulf strlcpy(data, pp->name, i); 451182843Slulf break; 452200934Smav case DIOCGSTRIPESIZE: 453200934Smav *(off_t *)data = cp->provider->stripesize; 454200934Smav break; 455200934Smav case DIOCGSTRIPEOFFSET: 456200934Smav *(off_t *)data = cp->provider->stripeoffset; 457200934Smav break; 458223089Sgibbs case DIOCGPHYSPATH: 459223089Sgibbs error = g_io_getattr("GEOM::physpath", cp, &i, data); 460223089Sgibbs if (error == 0 && *(char *)data == '\0') 461223089Sgibbs error = ENOENT; 462223089Sgibbs break; 46392698Sphk default: 464119660Sphk if (cp->provider->geom->ioctl != NULL) { 465138732Sphk error = cp->provider->geom->ioctl(cp->provider, cmd, data, fflag, td); 466119749Sphk } else { 467119749Sphk error = ENOIOCTL; 468119660Sphk } 46992698Sphk } 47092403Sphk 47192108Sphk return (error); 47292108Sphk} 47392108Sphk 47492108Sphkstatic void 47592108Sphkg_dev_done(struct bio *bp2) 47692108Sphk{ 477248679Smav struct g_consumer *cp; 478248679Smav struct g_dev_softc *sc; 47992108Sphk struct bio *bp; 480248679Smav int destroy; 48192108Sphk 482248679Smav cp = bp2->bio_from; 483248679Smav sc = cp->private; 484110517Sphk bp = bp2->bio_parent; 48592108Sphk bp->bio_error = bp2->bio_error; 48692108Sphk if (bp->bio_error != 0) { 48792108Sphk g_trace(G_T_BIO, "g_dev_done(%p) had error %d", 48892108Sphk bp2, bp->bio_error); 48992108Sphk bp->bio_flags |= BIO_ERROR; 49092108Sphk } else { 491105540Sphk g_trace(G_T_BIO, "g_dev_done(%p/%p) resid %ld completed %jd", 492105540Sphk bp2, bp, bp->bio_resid, (intmax_t)bp2->bio_completed); 49392108Sphk } 494137029Sphk bp->bio_resid = bp->bio_length - bp2->bio_completed; 495137029Sphk bp->bio_completed = bp2->bio_completed; 49692108Sphk g_destroy_bio(bp2); 497248679Smav destroy = 0; 498248679Smav mtx_lock(&sc->sc_mtx); 499248679Smav if ((--sc->sc_active) == 0) { 500248679Smav if (sc->sc_open == 0) 501248679Smav wakeup(&sc->sc_active); 502248679Smav if (sc->sc_dev == NULL) 503248679Smav destroy = 1; 504248679Smav } 505248679Smav mtx_unlock(&sc->sc_mtx); 506248679Smav if (destroy) 507248679Smav g_post_event(g_dev_destroy, cp, M_WAITOK, NULL); 50892108Sphk biodone(bp); 50992108Sphk} 51092108Sphk 51192108Sphkstatic void 51292108Sphkg_dev_strategy(struct bio *bp) 51392108Sphk{ 51492108Sphk struct g_consumer *cp; 51592108Sphk struct bio *bp2; 516130585Sphk struct cdev *dev; 517248679Smav struct g_dev_softc *sc; 51892108Sphk 519106300Sphk KASSERT(bp->bio_cmd == BIO_READ || 520106300Sphk bp->bio_cmd == BIO_WRITE || 521249193Strasz bp->bio_cmd == BIO_DELETE || 522249193Strasz bp->bio_cmd == BIO_FLUSH, 523106300Sphk ("Wrong bio_cmd bio=%p cmd=%d", bp, bp->bio_cmd)); 52492108Sphk dev = bp->bio_dev; 52592108Sphk cp = dev->si_drv2; 526248679Smav sc = cp->private; 527112978Sphk KASSERT(cp->acr || cp->acw, 528112978Sphk ("Consumer with zero access count in g_dev_strategy")); 529196964Smav#ifdef INVARIANTS 530135865Spjd if ((bp->bio_offset % cp->provider->sectorsize) != 0 || 531135865Spjd (bp->bio_bcount % cp->provider->sectorsize) != 0) { 532159756Ssimon bp->bio_resid = bp->bio_bcount; 533135865Spjd biofinish(bp, NULL, EINVAL); 534135865Spjd return; 535135865Spjd } 536196964Smav#endif 537248679Smav mtx_lock(&sc->sc_mtx); 538248679Smav KASSERT(sc->sc_open > 0, ("Closed device in g_dev_strategy")); 539248679Smav sc->sc_active++; 540248679Smav mtx_unlock(&sc->sc_mtx); 541248679Smav 542118869Sphk for (;;) { 543118869Sphk /* 544118869Sphk * XXX: This is not an ideal solution, but I belive it to 545118869Sphk * XXX: deadlock safe, all things considered. 546118869Sphk */ 547118869Sphk bp2 = g_clone_bio(bp); 548118869Sphk if (bp2 != NULL) 549118869Sphk break; 550167086Sjhb pause("gdstrat", hz / 10); 551118869Sphk } 552107834Sphk KASSERT(bp2 != NULL, ("XXX: ENOMEM in a bad place")); 55392108Sphk bp2->bio_done = g_dev_done; 55492108Sphk g_trace(G_T_BIO, 555105540Sphk "g_dev_strategy(%p/%p) offset %jd length %jd data %p cmd %d", 556105540Sphk bp, bp2, (intmax_t)bp->bio_offset, (intmax_t)bp2->bio_length, 557105540Sphk bp2->bio_data, bp2->bio_cmd); 55892108Sphk g_io_request(bp2, cp); 559112978Sphk KASSERT(cp->acr || cp->acw, 560112978Sphk ("g_dev_strategy raced with g_dev_close and lost")); 561112978Sphk 56292108Sphk} 56392108Sphk 56496987Sphk/* 565248679Smav * g_dev_callback() 566248679Smav * 567248679Smav * Called by devfs when asynchronous device destruction is completed. 568248679Smav * - Mark that we have no attached device any more. 569248679Smav * - If there are no outstanding requests, schedule geom destruction. 570248679Smav * Otherwise destruction will be scheduled later by g_dev_done(). 571248679Smav */ 572248679Smav 573248679Smavstatic void 574248679Smavg_dev_callback(void *arg) 575248679Smav{ 576248679Smav struct g_consumer *cp; 577248679Smav struct g_dev_softc *sc; 578248679Smav int destroy; 579248679Smav 580248679Smav cp = arg; 581248679Smav sc = cp->private; 582248679Smav g_trace(G_T_TOPOLOGY, "g_dev_callback(%p(%s))", cp, cp->geom->name); 583248679Smav 584248679Smav mtx_lock(&sc->sc_mtx); 585248679Smav sc->sc_dev = NULL; 586248679Smav sc->sc_alias = NULL; 587248679Smav destroy = (sc->sc_active == 0); 588248679Smav mtx_unlock(&sc->sc_mtx); 589248679Smav if (destroy) 590248679Smav g_post_event(g_dev_destroy, cp, M_WAITOK, NULL); 591248679Smav} 592248679Smav 593248679Smav/* 59496987Sphk * g_dev_orphan() 59596987Sphk * 596112024Sphk * Called from below when the provider orphaned us. 597112024Sphk * - Clear any dump settings. 598248679Smav * - Request asynchronous device destruction to prevent any more requests 599248679Smav * from coming in. The provider is already marked with an error, so 600248679Smav * anything which comes in in the interrim will be returned immediately. 60196987Sphk */ 60292108Sphk 60392108Sphkstatic void 60493250Sphkg_dev_orphan(struct g_consumer *cp) 60592108Sphk{ 606130585Sphk struct cdev *dev; 607248679Smav struct g_dev_softc *sc; 60892108Sphk 609112024Sphk g_topology_assert(); 610248679Smav sc = cp->private; 611248679Smav dev = sc->sc_dev; 612248679Smav g_trace(G_T_TOPOLOGY, "g_dev_orphan(%p(%s))", cp, cp->geom->name); 613112024Sphk 614112024Sphk /* Reset any dump-area set on this device */ 61595038Sphk if (dev->si_flags & SI_DUMPDEV) 616242439Salfred set_dumper(NULL, NULL); 617112024Sphk 618130585Sphk /* Destroy the struct cdev *so we get no more requests */ 619248679Smav destroy_dev_sched_cb(dev, g_dev_callback, cp); 62092108Sphk} 62192108Sphk 62296987SphkDECLARE_GEOM_CLASS(g_dev_class, g_dev); 623