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$"); 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", 82254389Sken .d_flags = D_DISK | D_TRACKCLOSE, 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 } 240254389Sken dev->si_flags |= SI_UNMAPPED; 241248679Smav sc->sc_dev = dev; 242221071Smav 243221071Smav /* Search for device alias name and create it if found. */ 244221071Smav adev = NULL; 245221071Smav for (len = MIN(strlen(gp->name), sizeof(buf) - 15); len > 0; len--) { 246221071Smav snprintf(buf, sizeof(buf), "kern.devalias.%s", gp->name); 247221071Smav buf[14 + len] = 0; 248221071Smav val = getenv(buf); 249221071Smav if (val != NULL) { 250221071Smav snprintf(buf, sizeof(buf), "%s%s", 251221071Smav val, gp->name + len); 252221071Smav freeenv(val); 253221400Smav make_dev_alias_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK, 254221400Smav &adev, dev, "%s", buf); 255254389Sken adev->si_flags |= SI_UNMAPPED; 256221071Smav break; 257221071Smav } 258221071Smav } 259221071Smav 260110728Sphk dev->si_iosize_max = MAXPHYS; 26192108Sphk dev->si_drv2 = cp; 262221071Smav if (adev != NULL) { 263221071Smav adev->si_iosize_max = MAXPHYS; 264221071Smav adev->si_drv2 = cp; 265221071Smav } 266223089Sgibbs 267223089Sgibbs g_dev_attrchanged(cp, "GEOM::physpath"); 268223089Sgibbs 26992108Sphk return (gp); 27092108Sphk} 27192108Sphk 27292108Sphkstatic int 273130585Sphkg_dev_open(struct cdev *dev, int flags, int fmt, struct thread *td) 27492108Sphk{ 27592108Sphk struct g_consumer *cp; 276248679Smav struct g_dev_softc *sc; 27792108Sphk int error, r, w, e; 27892108Sphk 27992108Sphk cp = dev->si_drv2; 280248679Smav if (cp == NULL) 281112978Sphk return(ENXIO); /* g_dev_taste() not done yet */ 28292108Sphk g_trace(G_T_ACCESS, "g_dev_open(%s, %d, %d, %p)", 283248679Smav cp->geom->name, flags, fmt, td); 284130712Sphk 28592108Sphk r = flags & FREAD ? 1 : 0; 28692108Sphk w = flags & FWRITE ? 1 : 0; 287103004Sphk#ifdef notyet 28892108Sphk e = flags & O_EXCL ? 1 : 0; 289103004Sphk#else 290103004Sphk e = 0; 291103004Sphk#endif 292130712Sphk if (w) { 293130712Sphk /* 294130712Sphk * When running in very secure mode, do not allow 295130712Sphk * opens for writing of any disks. 296130712Sphk */ 297130712Sphk error = securelevel_ge(td->td_ucred, 2); 298130712Sphk if (error) 299130712Sphk return (error); 300130712Sphk } 301112978Sphk g_topology_lock(); 302248679Smav error = g_access(cp, r, w, e); 30392108Sphk g_topology_unlock(); 304248679Smav if (error == 0) { 305248679Smav sc = cp->private; 306248679Smav mtx_lock(&sc->sc_mtx); 307248679Smav if (sc->sc_open == 0 && sc->sc_active != 0) 308248679Smav wakeup(&sc->sc_active); 309248679Smav sc->sc_open += r + w + e; 310248679Smav mtx_unlock(&sc->sc_mtx); 311248679Smav } 31292108Sphk return(error); 31392108Sphk} 31492108Sphk 31592108Sphkstatic int 316130585Sphkg_dev_close(struct cdev *dev, int flags, int fmt, struct thread *td) 31792108Sphk{ 31892108Sphk struct g_consumer *cp; 319248679Smav struct g_dev_softc *sc; 320248679Smav int error, r, w, e; 32192108Sphk 32292108Sphk cp = dev->si_drv2; 323248679Smav if (cp == NULL) 32492108Sphk return(ENXIO); 32592108Sphk g_trace(G_T_ACCESS, "g_dev_close(%s, %d, %d, %p)", 326248679Smav cp->geom->name, flags, fmt, td); 327248679Smav 32892108Sphk r = flags & FREAD ? -1 : 0; 32992108Sphk w = flags & FWRITE ? -1 : 0; 330103004Sphk#ifdef notyet 33192108Sphk e = flags & O_EXCL ? -1 : 0; 332103004Sphk#else 333103004Sphk e = 0; 334103004Sphk#endif 335248679Smav sc = cp->private; 336248679Smav mtx_lock(&sc->sc_mtx); 337248679Smav sc->sc_open += r + w + e; 338248679Smav while (sc->sc_open == 0 && sc->sc_active != 0) 339248679Smav msleep(&sc->sc_active, &sc->sc_mtx, 0, "PRIBIO", 0); 340248679Smav mtx_unlock(&sc->sc_mtx); 341112978Sphk g_topology_lock(); 342248679Smav error = g_access(cp, r, w, e); 34392108Sphk g_topology_unlock(); 34492108Sphk return (error); 34592108Sphk} 34692108Sphk 347112978Sphk/* 348112978Sphk * XXX: Until we have unmessed the ioctl situation, there is a race against 349112978Sphk * XXX: a concurrent orphanization. We cannot close it by holding topology 350112978Sphk * XXX: since that would prevent us from doing our job, and stalling events 351112978Sphk * XXX: will break (actually: stall) the BSD disklabel hacks. 352112978Sphk */ 35392108Sphkstatic int 354130585Sphkg_dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td) 35592108Sphk{ 35692108Sphk struct g_consumer *cp; 357182843Slulf struct g_provider *pp; 35895038Sphk struct g_kerneldump kd; 359174674Sphk off_t offset, length, chunk; 36092108Sphk int i, error; 36195038Sphk u_int u; 36292108Sphk 36392108Sphk cp = dev->si_drv2; 364182843Slulf pp = cp->provider; 36592108Sphk 36692108Sphk error = 0; 367112978Sphk KASSERT(cp->acr || cp->acw, 368112978Sphk ("Consumer with zero access count in g_dev_ioctl")); 36992403Sphk 37092698Sphk i = IOCPARM_LEN(cmd); 37192698Sphk switch (cmd) { 37292698Sphk case DIOCGSECTORSIZE: 373105551Sphk *(u_int *)data = cp->provider->sectorsize; 374105551Sphk if (*(u_int *)data == 0) 375105180Snjl error = ENOENT; 37692698Sphk break; 37792698Sphk case DIOCGMEDIASIZE: 378105551Sphk *(off_t *)data = cp->provider->mediasize; 379105551Sphk if (*(off_t *)data == 0) 380105180Snjl error = ENOENT; 38192698Sphk break; 38292698Sphk case DIOCGFWSECTORS: 38393250Sphk error = g_io_getattr("GEOM::fwsectors", cp, &i, data); 384105180Snjl if (error == 0 && *(u_int *)data == 0) 385105180Snjl error = ENOENT; 38692698Sphk break; 38792698Sphk case DIOCGFWHEADS: 38893250Sphk error = g_io_getattr("GEOM::fwheads", cp, &i, data); 389105180Snjl if (error == 0 && *(u_int *)data == 0) 390105180Snjl error = ENOENT; 39192698Sphk break; 39294287Sphk case DIOCGFRONTSTUFF: 39394287Sphk error = g_io_getattr("GEOM::frontstuff", cp, &i, data); 39494287Sphk break; 39595038Sphk case DIOCSKERNELDUMP: 39695038Sphk u = *((u_int *)data); 39795038Sphk if (!u) { 398242439Salfred set_dumper(NULL, NULL); 39995038Sphk error = 0; 40095038Sphk break; 40195038Sphk } 40295038Sphk kd.offset = 0; 40395038Sphk kd.length = OFF_MAX; 40495038Sphk i = sizeof kd; 40595038Sphk error = g_io_getattr("GEOM::kerneldump", cp, &i, &kd); 406219950Smav if (!error) { 407242439Salfred error = set_dumper(&kd.di, devtoname(dev)); 408219950Smav if (!error) 409219950Smav dev->si_flags |= SI_DUMPDEV; 410219950Smav } 41195038Sphk break; 412169284Spjd case DIOCGFLUSH: 413169284Spjd error = g_io_flush(cp); 414169284Spjd break; 415169284Spjd case DIOCGDELETE: 416169284Spjd offset = ((off_t *)data)[0]; 417169284Spjd length = ((off_t *)data)[1]; 418169284Spjd if ((offset % cp->provider->sectorsize) != 0 || 419174669Sphk (length % cp->provider->sectorsize) != 0 || length <= 0) { 420169284Spjd printf("%s: offset=%jd length=%jd\n", __func__, offset, 421169284Spjd length); 422169284Spjd error = EINVAL; 423169284Spjd break; 424169284Spjd } 425174674Sphk while (length > 0) { 426174674Sphk chunk = length; 427249930Ssmh if (g_dev_del_max_sectors != 0 && chunk > 428249930Ssmh g_dev_del_max_sectors * cp->provider->sectorsize) { 429249930Ssmh chunk = g_dev_del_max_sectors * 430249930Ssmh cp->provider->sectorsize; 431249930Ssmh } 432174674Sphk error = g_delete_data(cp, offset, chunk); 433174674Sphk length -= chunk; 434174674Sphk offset += chunk; 435174674Sphk if (error) 436174674Sphk break; 437174674Sphk /* 438249930Ssmh * Since the request size can be large, the service 439249930Ssmh * time can be is likewise. We make this ioctl 440249930Ssmh * interruptible by checking for signals for each bio. 441174674Sphk */ 442174674Sphk if (SIGPENDING(td)) 443174674Sphk break; 444174674Sphk } 445169284Spjd break; 446169284Spjd case DIOCGIDENT: 447169284Spjd error = g_io_getattr("GEOM::ident", cp, &i, data); 448169284Spjd break; 449182843Slulf case DIOCGPROVIDERNAME: 450182843Slulf if (pp == NULL) 451182843Slulf return (ENOENT); 452182843Slulf strlcpy(data, pp->name, i); 453182843Slulf break; 454200934Smav case DIOCGSTRIPESIZE: 455200934Smav *(off_t *)data = cp->provider->stripesize; 456200934Smav break; 457200934Smav case DIOCGSTRIPEOFFSET: 458200934Smav *(off_t *)data = cp->provider->stripeoffset; 459200934Smav break; 460223089Sgibbs case DIOCGPHYSPATH: 461223089Sgibbs error = g_io_getattr("GEOM::physpath", cp, &i, data); 462223089Sgibbs if (error == 0 && *(char *)data == '\0') 463223089Sgibbs error = ENOENT; 464223089Sgibbs break; 46592698Sphk default: 466119660Sphk if (cp->provider->geom->ioctl != NULL) { 467138732Sphk error = cp->provider->geom->ioctl(cp->provider, cmd, data, fflag, td); 468119749Sphk } else { 469119749Sphk error = ENOIOCTL; 470119660Sphk } 47192698Sphk } 47292403Sphk 47392108Sphk return (error); 47492108Sphk} 47592108Sphk 47692108Sphkstatic void 47792108Sphkg_dev_done(struct bio *bp2) 47892108Sphk{ 479248679Smav struct g_consumer *cp; 480248679Smav struct g_dev_softc *sc; 48192108Sphk struct bio *bp; 482248679Smav int destroy; 48392108Sphk 484248679Smav cp = bp2->bio_from; 485248679Smav sc = cp->private; 486110517Sphk bp = bp2->bio_parent; 48792108Sphk bp->bio_error = bp2->bio_error; 48892108Sphk if (bp->bio_error != 0) { 48992108Sphk g_trace(G_T_BIO, "g_dev_done(%p) had error %d", 49092108Sphk bp2, bp->bio_error); 49192108Sphk bp->bio_flags |= BIO_ERROR; 49292108Sphk } else { 493105540Sphk g_trace(G_T_BIO, "g_dev_done(%p/%p) resid %ld completed %jd", 494105540Sphk bp2, bp, bp->bio_resid, (intmax_t)bp2->bio_completed); 49592108Sphk } 496137029Sphk bp->bio_resid = bp->bio_length - bp2->bio_completed; 497137029Sphk bp->bio_completed = bp2->bio_completed; 49892108Sphk g_destroy_bio(bp2); 499248679Smav destroy = 0; 500248679Smav mtx_lock(&sc->sc_mtx); 501248679Smav if ((--sc->sc_active) == 0) { 502248679Smav if (sc->sc_open == 0) 503248679Smav wakeup(&sc->sc_active); 504248679Smav if (sc->sc_dev == NULL) 505248679Smav destroy = 1; 506248679Smav } 507248679Smav mtx_unlock(&sc->sc_mtx); 508248679Smav if (destroy) 509248679Smav g_post_event(g_dev_destroy, cp, M_WAITOK, NULL); 51092108Sphk biodone(bp); 51192108Sphk} 51292108Sphk 51392108Sphkstatic void 51492108Sphkg_dev_strategy(struct bio *bp) 51592108Sphk{ 51692108Sphk struct g_consumer *cp; 51792108Sphk struct bio *bp2; 518130585Sphk struct cdev *dev; 519248679Smav struct g_dev_softc *sc; 52092108Sphk 521106300Sphk KASSERT(bp->bio_cmd == BIO_READ || 522106300Sphk bp->bio_cmd == BIO_WRITE || 523249193Strasz bp->bio_cmd == BIO_DELETE || 524249193Strasz bp->bio_cmd == BIO_FLUSH, 525106300Sphk ("Wrong bio_cmd bio=%p cmd=%d", bp, bp->bio_cmd)); 52692108Sphk dev = bp->bio_dev; 52792108Sphk cp = dev->si_drv2; 528248679Smav sc = cp->private; 529112978Sphk KASSERT(cp->acr || cp->acw, 530112978Sphk ("Consumer with zero access count in g_dev_strategy")); 531196964Smav#ifdef INVARIANTS 532135865Spjd if ((bp->bio_offset % cp->provider->sectorsize) != 0 || 533135865Spjd (bp->bio_bcount % cp->provider->sectorsize) != 0) { 534159756Ssimon bp->bio_resid = bp->bio_bcount; 535135865Spjd biofinish(bp, NULL, EINVAL); 536135865Spjd return; 537135865Spjd } 538196964Smav#endif 539248679Smav mtx_lock(&sc->sc_mtx); 540248679Smav KASSERT(sc->sc_open > 0, ("Closed device in g_dev_strategy")); 541248679Smav sc->sc_active++; 542248679Smav mtx_unlock(&sc->sc_mtx); 543248679Smav 544118869Sphk for (;;) { 545118869Sphk /* 546118869Sphk * XXX: This is not an ideal solution, but I belive it to 547118869Sphk * XXX: deadlock safe, all things considered. 548118869Sphk */ 549118869Sphk bp2 = g_clone_bio(bp); 550118869Sphk if (bp2 != NULL) 551118869Sphk break; 552167086Sjhb pause("gdstrat", hz / 10); 553118869Sphk } 554107834Sphk KASSERT(bp2 != NULL, ("XXX: ENOMEM in a bad place")); 55592108Sphk bp2->bio_done = g_dev_done; 55692108Sphk g_trace(G_T_BIO, 557105540Sphk "g_dev_strategy(%p/%p) offset %jd length %jd data %p cmd %d", 558105540Sphk bp, bp2, (intmax_t)bp->bio_offset, (intmax_t)bp2->bio_length, 559105540Sphk bp2->bio_data, bp2->bio_cmd); 56092108Sphk g_io_request(bp2, cp); 561112978Sphk KASSERT(cp->acr || cp->acw, 562112978Sphk ("g_dev_strategy raced with g_dev_close and lost")); 563112978Sphk 56492108Sphk} 56592108Sphk 56696987Sphk/* 567248679Smav * g_dev_callback() 568248679Smav * 569248679Smav * Called by devfs when asynchronous device destruction is completed. 570248679Smav * - Mark that we have no attached device any more. 571248679Smav * - If there are no outstanding requests, schedule geom destruction. 572248679Smav * Otherwise destruction will be scheduled later by g_dev_done(). 573248679Smav */ 574248679Smav 575248679Smavstatic void 576248679Smavg_dev_callback(void *arg) 577248679Smav{ 578248679Smav struct g_consumer *cp; 579248679Smav struct g_dev_softc *sc; 580248679Smav int destroy; 581248679Smav 582248679Smav cp = arg; 583248679Smav sc = cp->private; 584248679Smav g_trace(G_T_TOPOLOGY, "g_dev_callback(%p(%s))", cp, cp->geom->name); 585248679Smav 586248679Smav mtx_lock(&sc->sc_mtx); 587248679Smav sc->sc_dev = NULL; 588248679Smav sc->sc_alias = NULL; 589248679Smav destroy = (sc->sc_active == 0); 590248679Smav mtx_unlock(&sc->sc_mtx); 591248679Smav if (destroy) 592248679Smav g_post_event(g_dev_destroy, cp, M_WAITOK, NULL); 593248679Smav} 594248679Smav 595248679Smav/* 59696987Sphk * g_dev_orphan() 59796987Sphk * 598112024Sphk * Called from below when the provider orphaned us. 599112024Sphk * - Clear any dump settings. 600248679Smav * - Request asynchronous device destruction to prevent any more requests 601248679Smav * from coming in. The provider is already marked with an error, so 602248679Smav * anything which comes in in the interrim will be returned immediately. 60396987Sphk */ 60492108Sphk 60592108Sphkstatic void 60693250Sphkg_dev_orphan(struct g_consumer *cp) 60792108Sphk{ 608130585Sphk struct cdev *dev; 609248679Smav struct g_dev_softc *sc; 61092108Sphk 611112024Sphk g_topology_assert(); 612248679Smav sc = cp->private; 613248679Smav dev = sc->sc_dev; 614248679Smav g_trace(G_T_TOPOLOGY, "g_dev_orphan(%p(%s))", cp, cp->geom->name); 615112024Sphk 616112024Sphk /* Reset any dump-area set on this device */ 61795038Sphk if (dev->si_flags & SI_DUMPDEV) 618242439Salfred set_dumper(NULL, NULL); 619112024Sphk 620130585Sphk /* Destroy the struct cdev *so we get no more requests */ 621248679Smav destroy_dev_sched_cb(dev, g_dev_callback, cp); 62292108Sphk} 62392108Sphk 62496987SphkDECLARE_GEOM_CLASS(g_dev_class, g_dev); 625