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;
225260385Sscottl	cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
22696987Sphk	error = g_attach(cp, pp);
22796987Sphk	KASSERT(error == 0,
22896987Sphk	    ("g_dev_taste(%s) failed to g_attach, err=%d", pp->name, error));
229214063Sjh	error = make_dev_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK, &dev,
230214063Sjh	    &g_dev_cdevsw, NULL, UID_ROOT, GID_OPERATOR, 0640, "%s", gp->name);
231214063Sjh	if (error != 0) {
232214063Sjh		printf("%s: make_dev_p() failed (gp->name=%s, error=%d)\n",
233214063Sjh		    __func__, gp->name, error);
234214063Sjh		g_detach(cp);
235214063Sjh		g_destroy_consumer(cp);
236214063Sjh		g_destroy_geom(gp);
237248679Smav		mtx_destroy(&sc->sc_mtx);
238248679Smav		g_free(sc);
239214063Sjh		return (NULL);
240214063Sjh	}
241254389Sken	dev->si_flags |= SI_UNMAPPED;
242248679Smav	sc->sc_dev = dev;
243221071Smav
244221071Smav	/* Search for device alias name and create it if found. */
245221071Smav	adev = NULL;
246221071Smav	for (len = MIN(strlen(gp->name), sizeof(buf) - 15); len > 0; len--) {
247221071Smav		snprintf(buf, sizeof(buf), "kern.devalias.%s", gp->name);
248221071Smav		buf[14 + len] = 0;
249221071Smav		val = getenv(buf);
250221071Smav		if (val != NULL) {
251221071Smav			snprintf(buf, sizeof(buf), "%s%s",
252221071Smav			    val, gp->name + len);
253221071Smav			freeenv(val);
254221400Smav			make_dev_alias_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK,
255221400Smav			    &adev, dev, "%s", buf);
256254389Sken			adev->si_flags |= SI_UNMAPPED;
257221071Smav			break;
258221071Smav		}
259221071Smav	}
260221071Smav
261110728Sphk	dev->si_iosize_max = MAXPHYS;
26292108Sphk	dev->si_drv2 = cp;
263221071Smav	if (adev != NULL) {
264221071Smav		adev->si_iosize_max = MAXPHYS;
265221071Smav		adev->si_drv2 = cp;
266221071Smav	}
267223089Sgibbs
268223089Sgibbs	g_dev_attrchanged(cp, "GEOM::physpath");
269223089Sgibbs
27092108Sphk	return (gp);
27192108Sphk}
27292108Sphk
27392108Sphkstatic int
274130585Sphkg_dev_open(struct cdev *dev, int flags, int fmt, struct thread *td)
27592108Sphk{
27692108Sphk	struct g_consumer *cp;
277248679Smav	struct g_dev_softc *sc;
27892108Sphk	int error, r, w, e;
27992108Sphk
28092108Sphk	cp = dev->si_drv2;
281248679Smav	if (cp == NULL)
282112978Sphk		return(ENXIO);		/* g_dev_taste() not done yet */
28392108Sphk	g_trace(G_T_ACCESS, "g_dev_open(%s, %d, %d, %p)",
284248679Smav	    cp->geom->name, flags, fmt, td);
285130712Sphk
28692108Sphk	r = flags & FREAD ? 1 : 0;
28792108Sphk	w = flags & FWRITE ? 1 : 0;
288103004Sphk#ifdef notyet
28992108Sphk	e = flags & O_EXCL ? 1 : 0;
290103004Sphk#else
291103004Sphk	e = 0;
292103004Sphk#endif
293130712Sphk	if (w) {
294130712Sphk		/*
295130712Sphk		 * When running in very secure mode, do not allow
296130712Sphk		 * opens for writing of any disks.
297130712Sphk		 */
298130712Sphk		error = securelevel_ge(td->td_ucred, 2);
299130712Sphk		if (error)
300130712Sphk			return (error);
301130712Sphk	}
302112978Sphk	g_topology_lock();
303248679Smav	error = g_access(cp, r, w, e);
30492108Sphk	g_topology_unlock();
305248679Smav	if (error == 0) {
306248679Smav		sc = cp->private;
307248679Smav		mtx_lock(&sc->sc_mtx);
308248679Smav		if (sc->sc_open == 0 && sc->sc_active != 0)
309248679Smav			wakeup(&sc->sc_active);
310248679Smav		sc->sc_open += r + w + e;
311248679Smav		mtx_unlock(&sc->sc_mtx);
312248679Smav	}
31392108Sphk	return(error);
31492108Sphk}
31592108Sphk
31692108Sphkstatic int
317130585Sphkg_dev_close(struct cdev *dev, int flags, int fmt, struct thread *td)
31892108Sphk{
31992108Sphk	struct g_consumer *cp;
320248679Smav	struct g_dev_softc *sc;
321248679Smav	int error, r, w, e;
32292108Sphk
32392108Sphk	cp = dev->si_drv2;
324248679Smav	if (cp == NULL)
32592108Sphk		return(ENXIO);
32692108Sphk	g_trace(G_T_ACCESS, "g_dev_close(%s, %d, %d, %p)",
327248679Smav	    cp->geom->name, flags, fmt, td);
328248679Smav
32992108Sphk	r = flags & FREAD ? -1 : 0;
33092108Sphk	w = flags & FWRITE ? -1 : 0;
331103004Sphk#ifdef notyet
33292108Sphk	e = flags & O_EXCL ? -1 : 0;
333103004Sphk#else
334103004Sphk	e = 0;
335103004Sphk#endif
336248679Smav	sc = cp->private;
337248679Smav	mtx_lock(&sc->sc_mtx);
338248679Smav	sc->sc_open += r + w + e;
339248679Smav	while (sc->sc_open == 0 && sc->sc_active != 0)
340248679Smav		msleep(&sc->sc_active, &sc->sc_mtx, 0, "PRIBIO", 0);
341248679Smav	mtx_unlock(&sc->sc_mtx);
342112978Sphk	g_topology_lock();
343248679Smav	error = g_access(cp, r, w, e);
34492108Sphk	g_topology_unlock();
34592108Sphk	return (error);
34692108Sphk}
34792108Sphk
348112978Sphk/*
349112978Sphk * XXX: Until we have unmessed the ioctl situation, there is a race against
350112978Sphk * XXX: a concurrent orphanization.  We cannot close it by holding topology
351112978Sphk * XXX: since that would prevent us from doing our job, and stalling events
352112978Sphk * XXX: will break (actually: stall) the BSD disklabel hacks.
353112978Sphk */
35492108Sphkstatic int
355130585Sphkg_dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
35692108Sphk{
35792108Sphk	struct g_consumer *cp;
358182843Slulf	struct g_provider *pp;
35995038Sphk	struct g_kerneldump kd;
360174674Sphk	off_t offset, length, chunk;
36192108Sphk	int i, error;
36295038Sphk	u_int u;
36392108Sphk
36492108Sphk	cp = dev->si_drv2;
365182843Slulf	pp = cp->provider;
36692108Sphk
36792108Sphk	error = 0;
368112978Sphk	KASSERT(cp->acr || cp->acw,
369112978Sphk	    ("Consumer with zero access count in g_dev_ioctl"));
37092403Sphk
37192698Sphk	i = IOCPARM_LEN(cmd);
37292698Sphk	switch (cmd) {
37392698Sphk	case DIOCGSECTORSIZE:
374105551Sphk		*(u_int *)data = cp->provider->sectorsize;
375105551Sphk		if (*(u_int *)data == 0)
376105180Snjl			error = ENOENT;
37792698Sphk		break;
37892698Sphk	case DIOCGMEDIASIZE:
379105551Sphk		*(off_t *)data = cp->provider->mediasize;
380105551Sphk		if (*(off_t *)data == 0)
381105180Snjl			error = ENOENT;
38292698Sphk		break;
38392698Sphk	case DIOCGFWSECTORS:
38493250Sphk		error = g_io_getattr("GEOM::fwsectors", cp, &i, data);
385105180Snjl		if (error == 0 && *(u_int *)data == 0)
386105180Snjl			error = ENOENT;
38792698Sphk		break;
38892698Sphk	case DIOCGFWHEADS:
38993250Sphk		error = g_io_getattr("GEOM::fwheads", cp, &i, data);
390105180Snjl		if (error == 0 && *(u_int *)data == 0)
391105180Snjl			error = ENOENT;
39292698Sphk		break;
39394287Sphk	case DIOCGFRONTSTUFF:
39494287Sphk		error = g_io_getattr("GEOM::frontstuff", cp, &i, data);
39594287Sphk		break;
39695038Sphk	case DIOCSKERNELDUMP:
39795038Sphk		u = *((u_int *)data);
39895038Sphk		if (!u) {
399242439Salfred			set_dumper(NULL, NULL);
40095038Sphk			error = 0;
40195038Sphk			break;
40295038Sphk		}
40395038Sphk		kd.offset = 0;
40495038Sphk		kd.length = OFF_MAX;
40595038Sphk		i = sizeof kd;
40695038Sphk		error = g_io_getattr("GEOM::kerneldump", cp, &i, &kd);
407219950Smav		if (!error) {
408242439Salfred			error = set_dumper(&kd.di, devtoname(dev));
409219950Smav			if (!error)
410219950Smav				dev->si_flags |= SI_DUMPDEV;
411219950Smav		}
41295038Sphk		break;
413169284Spjd	case DIOCGFLUSH:
414169284Spjd		error = g_io_flush(cp);
415169284Spjd		break;
416169284Spjd	case DIOCGDELETE:
417169284Spjd		offset = ((off_t *)data)[0];
418169284Spjd		length = ((off_t *)data)[1];
419169284Spjd		if ((offset % cp->provider->sectorsize) != 0 ||
420174669Sphk		    (length % cp->provider->sectorsize) != 0 || length <= 0) {
421169284Spjd			printf("%s: offset=%jd length=%jd\n", __func__, offset,
422169284Spjd			    length);
423169284Spjd			error = EINVAL;
424169284Spjd			break;
425169284Spjd		}
426174674Sphk		while (length > 0) {
427174674Sphk			chunk = length;
428249930Ssmh			if (g_dev_del_max_sectors != 0 && chunk >
429249930Ssmh			    g_dev_del_max_sectors * cp->provider->sectorsize) {
430249930Ssmh				chunk = g_dev_del_max_sectors *
431249930Ssmh				    cp->provider->sectorsize;
432249930Ssmh			}
433174674Sphk			error = g_delete_data(cp, offset, chunk);
434174674Sphk			length -= chunk;
435174674Sphk			offset += chunk;
436174674Sphk			if (error)
437174674Sphk				break;
438174674Sphk			/*
439249930Ssmh			 * Since the request size can be large, the service
440249930Ssmh			 * time can be is likewise.  We make this ioctl
441249930Ssmh			 * interruptible by checking for signals for each bio.
442174674Sphk			 */
443174674Sphk			if (SIGPENDING(td))
444174674Sphk				break;
445174674Sphk		}
446169284Spjd		break;
447169284Spjd	case DIOCGIDENT:
448169284Spjd		error = g_io_getattr("GEOM::ident", cp, &i, data);
449169284Spjd		break;
450182843Slulf	case DIOCGPROVIDERNAME:
451182843Slulf		if (pp == NULL)
452182843Slulf			return (ENOENT);
453182843Slulf		strlcpy(data, pp->name, i);
454182843Slulf		break;
455200934Smav	case DIOCGSTRIPESIZE:
456200934Smav		*(off_t *)data = cp->provider->stripesize;
457200934Smav		break;
458200934Smav	case DIOCGSTRIPEOFFSET:
459200934Smav		*(off_t *)data = cp->provider->stripeoffset;
460200934Smav		break;
461223089Sgibbs	case DIOCGPHYSPATH:
462223089Sgibbs		error = g_io_getattr("GEOM::physpath", cp, &i, data);
463223089Sgibbs		if (error == 0 && *(char *)data == '\0')
464223089Sgibbs			error = ENOENT;
465223089Sgibbs		break;
46692698Sphk	default:
467119660Sphk		if (cp->provider->geom->ioctl != NULL) {
468138732Sphk			error = cp->provider->geom->ioctl(cp->provider, cmd, data, fflag, td);
469119749Sphk		} else {
470119749Sphk			error = ENOIOCTL;
471119660Sphk		}
47292698Sphk	}
47392403Sphk
47492108Sphk	return (error);
47592108Sphk}
47692108Sphk
47792108Sphkstatic void
47892108Sphkg_dev_done(struct bio *bp2)
47992108Sphk{
480248679Smav	struct g_consumer *cp;
481248679Smav	struct g_dev_softc *sc;
48292108Sphk	struct bio *bp;
483248679Smav	int destroy;
48492108Sphk
485248679Smav	cp = bp2->bio_from;
486248679Smav	sc = cp->private;
487110517Sphk	bp = bp2->bio_parent;
48892108Sphk	bp->bio_error = bp2->bio_error;
489260385Sscottl	bp->bio_completed = bp2->bio_completed;
490260385Sscottl	bp->bio_resid = bp->bio_length - bp2->bio_completed;
491260385Sscottl	if (bp2->bio_error != 0) {
49292108Sphk		g_trace(G_T_BIO, "g_dev_done(%p) had error %d",
493260385Sscottl		    bp2, bp2->bio_error);
49492108Sphk		bp->bio_flags |= BIO_ERROR;
49592108Sphk	} else {
496105540Sphk		g_trace(G_T_BIO, "g_dev_done(%p/%p) resid %ld completed %jd",
497260385Sscottl		    bp2, bp, bp2->bio_resid, (intmax_t)bp2->bio_completed);
49892108Sphk	}
49992108Sphk	g_destroy_bio(bp2);
500248679Smav	destroy = 0;
501248679Smav	mtx_lock(&sc->sc_mtx);
502248679Smav	if ((--sc->sc_active) == 0) {
503248679Smav		if (sc->sc_open == 0)
504248679Smav			wakeup(&sc->sc_active);
505248679Smav		if (sc->sc_dev == NULL)
506248679Smav			destroy = 1;
507248679Smav	}
508248679Smav	mtx_unlock(&sc->sc_mtx);
509248679Smav	if (destroy)
510248679Smav		g_post_event(g_dev_destroy, cp, M_WAITOK, NULL);
51192108Sphk	biodone(bp);
51292108Sphk}
51392108Sphk
51492108Sphkstatic void
51592108Sphkg_dev_strategy(struct bio *bp)
51692108Sphk{
51792108Sphk	struct g_consumer *cp;
51892108Sphk	struct bio *bp2;
519130585Sphk	struct cdev *dev;
520248679Smav	struct g_dev_softc *sc;
52192108Sphk
522106300Sphk	KASSERT(bp->bio_cmd == BIO_READ ||
523106300Sphk	        bp->bio_cmd == BIO_WRITE ||
524249193Strasz	        bp->bio_cmd == BIO_DELETE ||
525249193Strasz		bp->bio_cmd == BIO_FLUSH,
526106300Sphk		("Wrong bio_cmd bio=%p cmd=%d", bp, bp->bio_cmd));
52792108Sphk	dev = bp->bio_dev;
52892108Sphk	cp = dev->si_drv2;
529248679Smav	sc = cp->private;
530112978Sphk	KASSERT(cp->acr || cp->acw,
531112978Sphk	    ("Consumer with zero access count in g_dev_strategy"));
532196964Smav#ifdef INVARIANTS
533135865Spjd	if ((bp->bio_offset % cp->provider->sectorsize) != 0 ||
534135865Spjd	    (bp->bio_bcount % cp->provider->sectorsize) != 0) {
535159756Ssimon		bp->bio_resid = bp->bio_bcount;
536135865Spjd		biofinish(bp, NULL, EINVAL);
537135865Spjd		return;
538135865Spjd	}
539196964Smav#endif
540248679Smav	mtx_lock(&sc->sc_mtx);
541248679Smav	KASSERT(sc->sc_open > 0, ("Closed device in g_dev_strategy"));
542248679Smav	sc->sc_active++;
543248679Smav	mtx_unlock(&sc->sc_mtx);
544248679Smav
545118869Sphk	for (;;) {
546118869Sphk		/*
547118869Sphk		 * XXX: This is not an ideal solution, but I belive it to
548118869Sphk		 * XXX: deadlock safe, all things considered.
549118869Sphk		 */
550118869Sphk		bp2 = g_clone_bio(bp);
551118869Sphk		if (bp2 != NULL)
552118869Sphk			break;
553167086Sjhb		pause("gdstrat", hz / 10);
554118869Sphk	}
555107834Sphk	KASSERT(bp2 != NULL, ("XXX: ENOMEM in a bad place"));
55692108Sphk	bp2->bio_done = g_dev_done;
55792108Sphk	g_trace(G_T_BIO,
558105540Sphk	    "g_dev_strategy(%p/%p) offset %jd length %jd data %p cmd %d",
559105540Sphk	    bp, bp2, (intmax_t)bp->bio_offset, (intmax_t)bp2->bio_length,
560105540Sphk	    bp2->bio_data, bp2->bio_cmd);
56192108Sphk	g_io_request(bp2, cp);
562112978Sphk	KASSERT(cp->acr || cp->acw,
563112978Sphk	    ("g_dev_strategy raced with g_dev_close and lost"));
564112978Sphk
56592108Sphk}
56692108Sphk
56796987Sphk/*
568248679Smav * g_dev_callback()
569248679Smav *
570248679Smav * Called by devfs when asynchronous device destruction is completed.
571248679Smav * - Mark that we have no attached device any more.
572248679Smav * - If there are no outstanding requests, schedule geom destruction.
573248679Smav *   Otherwise destruction will be scheduled later by g_dev_done().
574248679Smav */
575248679Smav
576248679Smavstatic void
577248679Smavg_dev_callback(void *arg)
578248679Smav{
579248679Smav	struct g_consumer *cp;
580248679Smav	struct g_dev_softc *sc;
581248679Smav	int destroy;
582248679Smav
583248679Smav	cp = arg;
584248679Smav	sc = cp->private;
585248679Smav	g_trace(G_T_TOPOLOGY, "g_dev_callback(%p(%s))", cp, cp->geom->name);
586248679Smav
587248679Smav	mtx_lock(&sc->sc_mtx);
588248679Smav	sc->sc_dev = NULL;
589248679Smav	sc->sc_alias = NULL;
590248679Smav	destroy = (sc->sc_active == 0);
591248679Smav	mtx_unlock(&sc->sc_mtx);
592248679Smav	if (destroy)
593248679Smav		g_post_event(g_dev_destroy, cp, M_WAITOK, NULL);
594248679Smav}
595248679Smav
596248679Smav/*
59796987Sphk * g_dev_orphan()
59896987Sphk *
599112024Sphk * Called from below when the provider orphaned us.
600112024Sphk * - Clear any dump settings.
601248679Smav * - Request asynchronous device destruction to prevent any more requests
602248679Smav *   from coming in.  The provider is already marked with an error, so
603248679Smav *   anything which comes in in the interrim will be returned immediately.
60496987Sphk */
60592108Sphk
60692108Sphkstatic void
60793250Sphkg_dev_orphan(struct g_consumer *cp)
60892108Sphk{
609130585Sphk	struct cdev *dev;
610248679Smav	struct g_dev_softc *sc;
61192108Sphk
612112024Sphk	g_topology_assert();
613248679Smav	sc = cp->private;
614248679Smav	dev = sc->sc_dev;
615248679Smav	g_trace(G_T_TOPOLOGY, "g_dev_orphan(%p(%s))", cp, cp->geom->name);
616112024Sphk
617112024Sphk	/* Reset any dump-area set on this device */
61895038Sphk	if (dev->si_flags & SI_DUMPDEV)
619242439Salfred		set_dumper(NULL, NULL);
620112024Sphk
621130585Sphk	/* Destroy the struct cdev *so we get no more requests */
622248679Smav	destroy_dev_sched_cb(dev, g_dev_callback, cp);
62392108Sphk}
62492108Sphk
62596987SphkDECLARE_GEOM_CLASS(g_dev_class, g_dev);
626