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>
46249152Smav#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>
55251306Ssmh#include <sys/sysctl.h>
5692108Sphk#include <geom/geom.h>
5795323Sphk#include <geom/geom_int.h>
58223089Sgibbs#include <machine/stdarg.h>
5992108Sphk
60249620Smavstruct g_dev_softc {
61249620Smav	struct mtx	 sc_mtx;
62249620Smav	struct cdev	*sc_dev;
63249620Smav	struct cdev	*sc_alias;
64249620Smav	int		 sc_open;
65249620Smav	int		 sc_active;
66249620Smav};
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",
82254979Sken	.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
97251306Ssmh/*
98251306Ssmh * We target 262144 (8 x 32768) sectors by default as this significantly
99251306Ssmh * increases the throughput on commonly used SSD's with a marginal
100251306Ssmh * increase in non-interruptible request latency.
101251306Ssmh */
102251306Ssmhstatic uint64_t g_dev_del_max_sectors = 262144;
103251306SsmhSYSCTL_DECL(_kern_geom);
104251306SsmhSYSCTL_NODE(_kern_geom, OID_AUTO, dev, CTLFLAG_RW, 0, "GEOM_DEV stuff");
105251306SsmhSYSCTL_QUAD(_kern_geom_dev, OID_AUTO, delete_max_sectors, CTLFLAG_RW,
106251306Ssmh    &g_dev_del_max_sectors, 0, "Maximum number of sectors in a single "
107251306Ssmh    "delete request sent to the provider. Larger requests are chunked "
108251306Ssmh    "so they can be interrupted. (0 = disable chunking)");
109251306Ssmh
110249620Smavstatic void
111249620Smavg_dev_destroy(void *arg, int flags __unused)
112249620Smav{
113249620Smav	struct g_consumer *cp;
114249620Smav	struct g_geom *gp;
115249620Smav	struct g_dev_softc *sc;
116249620Smav
117249620Smav	g_topology_assert();
118249620Smav	cp = arg;
119249620Smav	gp = cp->geom;
120249620Smav	sc = cp->private;
121249620Smav	g_trace(G_T_TOPOLOGY, "g_dev_destroy(%p(%s))", cp, gp->name);
122249620Smav	if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
123249620Smav		g_access(cp, -cp->acr, -cp->acw, -cp->ace);
124249620Smav	g_detach(cp);
125249620Smav	g_destroy_consumer(cp);
126249620Smav	g_destroy_geom(gp);
127249620Smav	mtx_destroy(&sc->sc_mtx);
128249620Smav	g_free(sc);
129249620Smav}
130249620Smav
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{
147249620Smav	struct g_dev_softc *sc;
148249152Smav	struct cdev *dev;
149249152Smav	char buf[SPECNAMELEN + 6];
150223089Sgibbs
151249620Smav	sc = cp->private;
152249152Smav	if (strcmp(attr, "GEOM::media") == 0) {
153249620Smav		dev = sc->sc_dev;
154249152Smav		snprintf(buf, sizeof(buf), "cdev=%s", dev->si_name);
155249152Smav		devctl_notify_f("DEVFS", "CDEV", "MEDIACHANGE", buf, M_WAITOK);
156249620Smav		dev = sc->sc_alias;
157249152Smav		if (dev != NULL) {
158249152Smav			snprintf(buf, sizeof(buf), "cdev=%s", dev->si_name);
159249152Smav			devctl_notify_f("DEVFS", "CDEV", "MEDIACHANGE", buf,
160249152Smav			    M_WAITOK);
161249152Smav		}
162249152Smav		return;
163249152Smav	}
164249152Smav
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
181249620Smav			dev = sc->sc_dev;
182249620Smav			old_alias_dev = sc->sc_alias;
183249620Smav			alias_devp = (struct cdev **)&sc->sc_alias;
184223089Sgibbs			make_dev_physpath_alias(MAKEDEV_WAITOK, alias_devp,
185223089Sgibbs			    dev, old_alias_dev, physpath);
186249620Smav		} else if (sc->sc_alias) {
187249620Smav			destroy_dev((struct cdev *)sc->sc_alias);
188249620Smav			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;
213249620Smav	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();
220244547Sjh	gp = g_new_geomf(mp, "%s", pp->name);
221249620Smav	sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
222249620Smav	mtx_init(&sc->sc_mtx, "g_dev", NULL, MTX_DEF);
22392108Sphk	cp = g_new_consumer(gp);
224249620Smav	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);
236249620Smav		mtx_destroy(&sc->sc_mtx);
237249620Smav		g_free(sc);
238214063Sjh		return (NULL);
239214063Sjh	}
240254979Sken	dev->si_flags |= SI_UNMAPPED;
241249620Smav	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);
255254979Sken			adev->si_flags |= SI_UNMAPPED;
256221071Smav			break;
257221071Smav		}
258221071Smav	}
259221071Smav
260110700Sphk	if (pp->flags & G_PF_CANDELETE)
261110700Sphk		dev->si_flags |= SI_CANDELETE;
262110728Sphk	dev->si_iosize_max = MAXPHYS;
26392108Sphk	dev->si_drv2 = cp;
264221071Smav	if (adev != NULL) {
265221071Smav		if (pp->flags & G_PF_CANDELETE)
266221071Smav			adev->si_flags |= SI_CANDELETE;
267221071Smav		adev->si_iosize_max = MAXPHYS;
268221071Smav		adev->si_drv2 = cp;
269221071Smav	}
270223089Sgibbs
271223089Sgibbs	g_dev_attrchanged(cp, "GEOM::physpath");
272223089Sgibbs
27392108Sphk	return (gp);
27492108Sphk}
27592108Sphk
27692108Sphkstatic int
277130585Sphkg_dev_open(struct cdev *dev, int flags, int fmt, struct thread *td)
27892108Sphk{
27992108Sphk	struct g_consumer *cp;
280249620Smav	struct g_dev_softc *sc;
28192108Sphk	int error, r, w, e;
28292108Sphk
28392108Sphk	cp = dev->si_drv2;
284249620Smav	if (cp == NULL)
285112978Sphk		return(ENXIO);		/* g_dev_taste() not done yet */
28692108Sphk	g_trace(G_T_ACCESS, "g_dev_open(%s, %d, %d, %p)",
287249620Smav	    cp->geom->name, flags, fmt, td);
288130712Sphk
28992108Sphk	r = flags & FREAD ? 1 : 0;
29092108Sphk	w = flags & FWRITE ? 1 : 0;
291103004Sphk#ifdef notyet
29292108Sphk	e = flags & O_EXCL ? 1 : 0;
293103004Sphk#else
294103004Sphk	e = 0;
295103004Sphk#endif
296130712Sphk	if (w) {
297130712Sphk		/*
298130712Sphk		 * When running in very secure mode, do not allow
299130712Sphk		 * opens for writing of any disks.
300130712Sphk		 */
301130712Sphk		error = securelevel_ge(td->td_ucred, 2);
302130712Sphk		if (error)
303130712Sphk			return (error);
304130712Sphk	}
305112978Sphk	g_topology_lock();
306249620Smav	error = g_access(cp, r, w, e);
30792108Sphk	g_topology_unlock();
308249620Smav	if (error == 0) {
309249620Smav		sc = cp->private;
310249620Smav		mtx_lock(&sc->sc_mtx);
311249620Smav		if (sc->sc_open == 0 && sc->sc_active != 0)
312249620Smav			wakeup(&sc->sc_active);
313249620Smav		sc->sc_open += r + w + e;
314249620Smav		mtx_unlock(&sc->sc_mtx);
315249620Smav	}
31692108Sphk	return(error);
31792108Sphk}
31892108Sphk
31992108Sphkstatic int
320130585Sphkg_dev_close(struct cdev *dev, int flags, int fmt, struct thread *td)
32192108Sphk{
32292108Sphk	struct g_consumer *cp;
323249620Smav	struct g_dev_softc *sc;
324249620Smav	int error, r, w, e;
32592108Sphk
32692108Sphk	cp = dev->si_drv2;
327249620Smav	if (cp == NULL)
32892108Sphk		return(ENXIO);
32992108Sphk	g_trace(G_T_ACCESS, "g_dev_close(%s, %d, %d, %p)",
330249620Smav	    cp->geom->name, flags, fmt, td);
331249620Smav
33292108Sphk	r = flags & FREAD ? -1 : 0;
33392108Sphk	w = flags & FWRITE ? -1 : 0;
334103004Sphk#ifdef notyet
33592108Sphk	e = flags & O_EXCL ? -1 : 0;
336103004Sphk#else
337103004Sphk	e = 0;
338103004Sphk#endif
339249620Smav	sc = cp->private;
340249620Smav	mtx_lock(&sc->sc_mtx);
341249620Smav	sc->sc_open += r + w + e;
342249620Smav	while (sc->sc_open == 0 && sc->sc_active != 0)
343249620Smav		msleep(&sc->sc_active, &sc->sc_mtx, 0, "PRIBIO", 0);
344249620Smav	mtx_unlock(&sc->sc_mtx);
345112978Sphk	g_topology_lock();
346249620Smav	error = g_access(cp, r, w, e);
34792108Sphk	g_topology_unlock();
34892108Sphk	return (error);
34992108Sphk}
35092108Sphk
351112978Sphk/*
352112978Sphk * XXX: Until we have unmessed the ioctl situation, there is a race against
353112978Sphk * XXX: a concurrent orphanization.  We cannot close it by holding topology
354112978Sphk * XXX: since that would prevent us from doing our job, and stalling events
355112978Sphk * XXX: will break (actually: stall) the BSD disklabel hacks.
356112978Sphk */
35792108Sphkstatic int
358130585Sphkg_dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
35992108Sphk{
36092108Sphk	struct g_consumer *cp;
361182843Slulf	struct g_provider *pp;
36295038Sphk	struct g_kerneldump kd;
363174674Sphk	off_t offset, length, chunk;
36492108Sphk	int i, error;
36595038Sphk	u_int u;
36692108Sphk
36792108Sphk	cp = dev->si_drv2;
368182843Slulf	pp = cp->provider;
36992108Sphk
37092108Sphk	error = 0;
371112978Sphk	KASSERT(cp->acr || cp->acw,
372112978Sphk	    ("Consumer with zero access count in g_dev_ioctl"));
37392403Sphk
37492698Sphk	i = IOCPARM_LEN(cmd);
37592698Sphk	switch (cmd) {
37692698Sphk	case DIOCGSECTORSIZE:
377105551Sphk		*(u_int *)data = cp->provider->sectorsize;
378105551Sphk		if (*(u_int *)data == 0)
379105180Snjl			error = ENOENT;
38092698Sphk		break;
38192698Sphk	case DIOCGMEDIASIZE:
382105551Sphk		*(off_t *)data = cp->provider->mediasize;
383105551Sphk		if (*(off_t *)data == 0)
384105180Snjl			error = ENOENT;
38592698Sphk		break;
38692698Sphk	case DIOCGFWSECTORS:
38793250Sphk		error = g_io_getattr("GEOM::fwsectors", cp, &i, data);
388105180Snjl		if (error == 0 && *(u_int *)data == 0)
389105180Snjl			error = ENOENT;
39092698Sphk		break;
39192698Sphk	case DIOCGFWHEADS:
39293250Sphk		error = g_io_getattr("GEOM::fwheads", cp, &i, data);
393105180Snjl		if (error == 0 && *(u_int *)data == 0)
394105180Snjl			error = ENOENT;
39592698Sphk		break;
39694287Sphk	case DIOCGFRONTSTUFF:
39794287Sphk		error = g_io_getattr("GEOM::frontstuff", cp, &i, data);
39894287Sphk		break;
39995038Sphk	case DIOCSKERNELDUMP:
40095038Sphk		u = *((u_int *)data);
40195038Sphk		if (!u) {
40295038Sphk			set_dumper(NULL);
40395038Sphk			error = 0;
40495038Sphk			break;
40595038Sphk		}
40695038Sphk		kd.offset = 0;
40795038Sphk		kd.length = OFF_MAX;
40895038Sphk		i = sizeof kd;
40995038Sphk		error = g_io_getattr("GEOM::kerneldump", cp, &i, &kd);
410219950Smav		if (!error) {
411219950Smav			error = set_dumper(&kd.di);
412219950Smav			if (!error)
413219950Smav				dev->si_flags |= SI_DUMPDEV;
414219950Smav		}
41595038Sphk		break;
416169284Spjd	case DIOCGFLUSH:
417169284Spjd		error = g_io_flush(cp);
418169284Spjd		break;
419169284Spjd	case DIOCGDELETE:
420169284Spjd		offset = ((off_t *)data)[0];
421169284Spjd		length = ((off_t *)data)[1];
422169284Spjd		if ((offset % cp->provider->sectorsize) != 0 ||
423174669Sphk		    (length % cp->provider->sectorsize) != 0 || length <= 0) {
424169284Spjd			printf("%s: offset=%jd length=%jd\n", __func__, offset,
425169284Spjd			    length);
426169284Spjd			error = EINVAL;
427169284Spjd			break;
428169284Spjd		}
429174674Sphk		while (length > 0) {
430174674Sphk			chunk = length;
431251306Ssmh			if (g_dev_del_max_sectors != 0 && chunk >
432251306Ssmh			    g_dev_del_max_sectors * cp->provider->sectorsize) {
433251306Ssmh				chunk = g_dev_del_max_sectors *
434251306Ssmh				    cp->provider->sectorsize;
435251306Ssmh			}
436174674Sphk			error = g_delete_data(cp, offset, chunk);
437174674Sphk			length -= chunk;
438174674Sphk			offset += chunk;
439174674Sphk			if (error)
440174674Sphk				break;
441174674Sphk			/*
442251306Ssmh			 * Since the request size can be large, the service
443251306Ssmh			 * time can be is likewise.  We make this ioctl
444251306Ssmh			 * interruptible by checking for signals for each bio.
445174674Sphk			 */
446174674Sphk			if (SIGPENDING(td))
447174674Sphk				break;
448174674Sphk		}
449169284Spjd		break;
450169284Spjd	case DIOCGIDENT:
451169284Spjd		error = g_io_getattr("GEOM::ident", cp, &i, data);
452169284Spjd		break;
453182843Slulf	case DIOCGPROVIDERNAME:
454182843Slulf		if (pp == NULL)
455182843Slulf			return (ENOENT);
456182843Slulf		strlcpy(data, pp->name, i);
457182843Slulf		break;
458200934Smav	case DIOCGSTRIPESIZE:
459200934Smav		*(off_t *)data = cp->provider->stripesize;
460200934Smav		break;
461200934Smav	case DIOCGSTRIPEOFFSET:
462200934Smav		*(off_t *)data = cp->provider->stripeoffset;
463200934Smav		break;
464223089Sgibbs	case DIOCGPHYSPATH:
465223089Sgibbs		error = g_io_getattr("GEOM::physpath", cp, &i, data);
466223089Sgibbs		if (error == 0 && *(char *)data == '\0')
467223089Sgibbs			error = ENOENT;
468223089Sgibbs		break;
46992698Sphk	default:
470119660Sphk		if (cp->provider->geom->ioctl != NULL) {
471138732Sphk			error = cp->provider->geom->ioctl(cp->provider, cmd, data, fflag, td);
472119749Sphk		} else {
473119749Sphk			error = ENOIOCTL;
474119660Sphk		}
47592698Sphk	}
47692403Sphk
47792108Sphk	return (error);
47892108Sphk}
47992108Sphk
48092108Sphkstatic void
48192108Sphkg_dev_done(struct bio *bp2)
48292108Sphk{
483249620Smav	struct g_consumer *cp;
484249620Smav	struct g_dev_softc *sc;
48592108Sphk	struct bio *bp;
486249620Smav	int destroy;
48792108Sphk
488249620Smav	cp = bp2->bio_from;
489249620Smav	sc = cp->private;
490110517Sphk	bp = bp2->bio_parent;
49192108Sphk	bp->bio_error = bp2->bio_error;
492265674Smav	bp->bio_completed = bp2->bio_completed;
493265674Smav	bp->bio_resid = bp->bio_length - bp2->bio_completed;
494265674Smav	if (bp2->bio_error != 0) {
49592108Sphk		g_trace(G_T_BIO, "g_dev_done(%p) had error %d",
496265674Smav		    bp2, bp2->bio_error);
49792108Sphk		bp->bio_flags |= BIO_ERROR;
49892108Sphk	} else {
499105540Sphk		g_trace(G_T_BIO, "g_dev_done(%p/%p) resid %ld completed %jd",
500265674Smav		    bp2, bp, bp2->bio_resid, (intmax_t)bp2->bio_completed);
50192108Sphk	}
50292108Sphk	g_destroy_bio(bp2);
503249620Smav	destroy = 0;
504249620Smav	mtx_lock(&sc->sc_mtx);
505249620Smav	if ((--sc->sc_active) == 0) {
506249620Smav		if (sc->sc_open == 0)
507249620Smav			wakeup(&sc->sc_active);
508249620Smav		if (sc->sc_dev == NULL)
509249620Smav			destroy = 1;
510249620Smav	}
511249620Smav	mtx_unlock(&sc->sc_mtx);
512249620Smav	if (destroy)
513249620Smav		g_post_event(g_dev_destroy, cp, M_WAITOK, NULL);
51492108Sphk	biodone(bp);
51592108Sphk}
51692108Sphk
51792108Sphkstatic void
51892108Sphkg_dev_strategy(struct bio *bp)
51992108Sphk{
52092108Sphk	struct g_consumer *cp;
52192108Sphk	struct bio *bp2;
522130585Sphk	struct cdev *dev;
523249620Smav	struct g_dev_softc *sc;
52492108Sphk
525106300Sphk	KASSERT(bp->bio_cmd == BIO_READ ||
526106300Sphk	        bp->bio_cmd == BIO_WRITE ||
527251927Smav	        bp->bio_cmd == BIO_DELETE ||
528251927Smav		bp->bio_cmd == BIO_FLUSH,
529106300Sphk		("Wrong bio_cmd bio=%p cmd=%d", bp, bp->bio_cmd));
53092108Sphk	dev = bp->bio_dev;
53192108Sphk	cp = dev->si_drv2;
532249620Smav	sc = cp->private;
533112978Sphk	KASSERT(cp->acr || cp->acw,
534112978Sphk	    ("Consumer with zero access count in g_dev_strategy"));
535196964Smav#ifdef INVARIANTS
536135865Spjd	if ((bp->bio_offset % cp->provider->sectorsize) != 0 ||
537135865Spjd	    (bp->bio_bcount % cp->provider->sectorsize) != 0) {
538159756Ssimon		bp->bio_resid = bp->bio_bcount;
539135865Spjd		biofinish(bp, NULL, EINVAL);
540135865Spjd		return;
541135865Spjd	}
542196964Smav#endif
543249620Smav	mtx_lock(&sc->sc_mtx);
544249620Smav	KASSERT(sc->sc_open > 0, ("Closed device in g_dev_strategy"));
545249620Smav	sc->sc_active++;
546249620Smav	mtx_unlock(&sc->sc_mtx);
547249620Smav
548118869Sphk	for (;;) {
549118869Sphk		/*
550118869Sphk		 * XXX: This is not an ideal solution, but I belive it to
551118869Sphk		 * XXX: deadlock safe, all things considered.
552118869Sphk		 */
553118869Sphk		bp2 = g_clone_bio(bp);
554118869Sphk		if (bp2 != NULL)
555118869Sphk			break;
556167086Sjhb		pause("gdstrat", hz / 10);
557118869Sphk	}
558107834Sphk	KASSERT(bp2 != NULL, ("XXX: ENOMEM in a bad place"));
55992108Sphk	bp2->bio_done = g_dev_done;
56092108Sphk	g_trace(G_T_BIO,
561105540Sphk	    "g_dev_strategy(%p/%p) offset %jd length %jd data %p cmd %d",
562105540Sphk	    bp, bp2, (intmax_t)bp->bio_offset, (intmax_t)bp2->bio_length,
563105540Sphk	    bp2->bio_data, bp2->bio_cmd);
56492108Sphk	g_io_request(bp2, cp);
565112978Sphk	KASSERT(cp->acr || cp->acw,
566112978Sphk	    ("g_dev_strategy raced with g_dev_close and lost"));
567112978Sphk
56892108Sphk}
56992108Sphk
57096987Sphk/*
571249620Smav * g_dev_callback()
572249620Smav *
573249620Smav * Called by devfs when asynchronous device destruction is completed.
574249620Smav * - Mark that we have no attached device any more.
575249620Smav * - If there are no outstanding requests, schedule geom destruction.
576249620Smav *   Otherwise destruction will be scheduled later by g_dev_done().
577249620Smav */
578249620Smav
579249620Smavstatic void
580249620Smavg_dev_callback(void *arg)
581249620Smav{
582249620Smav	struct g_consumer *cp;
583249620Smav	struct g_dev_softc *sc;
584249620Smav	int destroy;
585249620Smav
586249620Smav	cp = arg;
587249620Smav	sc = cp->private;
588249620Smav	g_trace(G_T_TOPOLOGY, "g_dev_callback(%p(%s))", cp, cp->geom->name);
589249620Smav
590249620Smav	mtx_lock(&sc->sc_mtx);
591249620Smav	sc->sc_dev = NULL;
592249620Smav	sc->sc_alias = NULL;
593249620Smav	destroy = (sc->sc_active == 0);
594249620Smav	mtx_unlock(&sc->sc_mtx);
595249620Smav	if (destroy)
596249620Smav		g_post_event(g_dev_destroy, cp, M_WAITOK, NULL);
597249620Smav}
598249620Smav
599249620Smav/*
60096987Sphk * g_dev_orphan()
60196987Sphk *
602112024Sphk * Called from below when the provider orphaned us.
603112024Sphk * - Clear any dump settings.
604249620Smav * - Request asynchronous device destruction to prevent any more requests
605249620Smav *   from coming in.  The provider is already marked with an error, so
606249620Smav *   anything which comes in in the interrim will be returned immediately.
60796987Sphk */
60892108Sphk
60992108Sphkstatic void
61093250Sphkg_dev_orphan(struct g_consumer *cp)
61192108Sphk{
612130585Sphk	struct cdev *dev;
613249620Smav	struct g_dev_softc *sc;
61492108Sphk
615112024Sphk	g_topology_assert();
616249620Smav	sc = cp->private;
617249620Smav	dev = sc->sc_dev;
618249620Smav	g_trace(G_T_TOPOLOGY, "g_dev_orphan(%p(%s))", cp, cp->geom->name);
619112024Sphk
620112024Sphk	/* Reset any dump-area set on this device */
62195038Sphk	if (dev->si_flags & SI_DUMPDEV)
62295038Sphk		set_dumper(NULL);
623112024Sphk
624130585Sphk	/* Destroy the struct cdev *so we get no more requests */
625249620Smav	destroy_dev_sched_cb(dev, g_dev_callback, cp);
62692108Sphk}
62792108Sphk
62896987SphkDECLARE_GEOM_CLASS(g_dev_class, g_dev);
629