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: stable/10/sys/geom/geom_dev.c 315705 2017-03-22 07:54:29Z mav $");
38116196Sobrien
3992108Sphk#include <sys/param.h>
4092108Sphk#include <sys/systm.h>
4192108Sphk#include <sys/malloc.h>
4292108Sphk#include <sys/kernel.h>
4392108Sphk#include <sys/conf.h>
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
85273817Saestatic g_init_t g_dev_init;
86273817Saestatic g_fini_t g_dev_fini;
8792108Sphkstatic g_taste_t g_dev_taste;
8892108Sphkstatic g_orphan_t g_dev_orphan;
89223089Sgibbsstatic g_attrchanged_t g_dev_attrchanged;
9092108Sphk
9193248Sphkstatic struct g_class g_dev_class	= {
92112552Sphk	.name = "DEV",
93133318Sphk	.version = G_VERSION,
94273817Sae	.init = g_dev_init,
95273817Sae	.fini = g_dev_fini,
96112552Sphk	.taste = g_dev_taste,
97133314Sphk	.orphan = g_dev_orphan,
98223089Sgibbs	.attrchanged = g_dev_attrchanged
9992108Sphk};
10092108Sphk
101249930Ssmh/*
102249930Ssmh * We target 262144 (8 x 32768) sectors by default as this significantly
103249930Ssmh * increases the throughput on commonly used SSD's with a marginal
104249930Ssmh * increase in non-interruptible request latency.
105249930Ssmh */
106249930Ssmhstatic uint64_t g_dev_del_max_sectors = 262144;
107249930SsmhSYSCTL_DECL(_kern_geom);
108249930SsmhSYSCTL_NODE(_kern_geom, OID_AUTO, dev, CTLFLAG_RW, 0, "GEOM_DEV stuff");
109249930SsmhSYSCTL_QUAD(_kern_geom_dev, OID_AUTO, delete_max_sectors, CTLFLAG_RW,
110249930Ssmh    &g_dev_del_max_sectors, 0, "Maximum number of sectors in a single "
111249930Ssmh    "delete request sent to the provider. Larger requests are chunked "
112249930Ssmh    "so they can be interrupted. (0 = disable chunking)");
113249930Ssmh
114273817Saestatic char *dumpdev = NULL;
115248679Smavstatic void
116273817Saeg_dev_init(struct g_class *mp)
117273817Sae{
118273817Sae
119273817Sae	dumpdev = getenv("dumpdev");
120273817Sae}
121273817Sae
122273817Saestatic void
123273817Saeg_dev_fini(struct g_class *mp)
124273817Sae{
125273817Sae
126273817Sae	freeenv(dumpdev);
127291547Ssmh	dumpdev = NULL;
128273817Sae}
129273817Sae
130273817Saestatic int
131291215Ssmhg_dev_setdumpdev(struct cdev *dev, struct thread *td)
132273817Sae{
133273817Sae	struct g_kerneldump kd;
134273817Sae	struct g_consumer *cp;
135273817Sae	int error, len;
136273817Sae
137273817Sae	if (dev == NULL)
138291215Ssmh		return (set_dumper(NULL, NULL, td));
139273817Sae
140273817Sae	cp = dev->si_drv2;
141273817Sae	len = sizeof(kd);
142273817Sae	kd.offset = 0;
143273817Sae	kd.length = OFF_MAX;
144273817Sae	error = g_io_getattr("GEOM::kerneldump", cp, &len, &kd);
145273817Sae	if (error == 0) {
146291215Ssmh		error = set_dumper(&kd.di, devtoname(dev), td);
147273817Sae		if (error == 0)
148273817Sae			dev->si_flags |= SI_DUMPDEV;
149273817Sae	}
150273817Sae	return (error);
151273817Sae}
152273817Sae
153291548Ssmhstatic int
154273817Saeinit_dumpdev(struct cdev *dev)
155273817Sae{
156291548Ssmh	struct g_consumer *cp;
157291547Ssmh	const char *devprefix = "/dev/", *devname;
158291548Ssmh	int error;
159291547Ssmh	size_t len;
160273817Sae
161273817Sae	if (dumpdev == NULL)
162291548Ssmh		return (0);
163291548Ssmh
164291547Ssmh	len = strlen(devprefix);
165291547Ssmh	devname = devtoname(dev);
166291547Ssmh	if (strcmp(devname, dumpdev) != 0 &&
167291547Ssmh	   (strncmp(dumpdev, devprefix, len) != 0 ||
168291547Ssmh	    strcmp(devname, dumpdev + len) != 0))
169291548Ssmh		return (0);
170291548Ssmh
171291548Ssmh	cp = (struct g_consumer *)dev->si_drv2;
172291548Ssmh	error = g_access(cp, 1, 0, 0);
173291548Ssmh	if (error != 0)
174291548Ssmh		return (error);
175291548Ssmh
176291548Ssmh	error = g_dev_setdumpdev(dev, curthread);
177291548Ssmh	if (error == 0) {
178273817Sae		freeenv(dumpdev);
179273817Sae		dumpdev = NULL;
180273817Sae	}
181291548Ssmh
182291548Ssmh	(void)g_access(cp, -1, 0, 0);
183291548Ssmh
184291548Ssmh	return (error);
185273817Sae}
186273817Sae
187273817Saestatic void
188248679Smavg_dev_destroy(void *arg, int flags __unused)
189248679Smav{
190248679Smav	struct g_consumer *cp;
191248679Smav	struct g_geom *gp;
192248679Smav	struct g_dev_softc *sc;
193282953Strasz	char buf[SPECNAMELEN + 6];
194248679Smav
195248679Smav	g_topology_assert();
196248679Smav	cp = arg;
197248679Smav	gp = cp->geom;
198248679Smav	sc = cp->private;
199248679Smav	g_trace(G_T_TOPOLOGY, "g_dev_destroy(%p(%s))", cp, gp->name);
200282953Strasz	snprintf(buf, sizeof(buf), "cdev=%s", gp->name);
201282953Strasz	devctl_notify_f("GEOM", "DEV", "DESTROY", buf, M_WAITOK);
202248679Smav	if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
203248679Smav		g_access(cp, -cp->acr, -cp->acw, -cp->ace);
204248679Smav	g_detach(cp);
205248679Smav	g_destroy_consumer(cp);
206248679Smav	g_destroy_geom(gp);
207248679Smav	mtx_destroy(&sc->sc_mtx);
208248679Smav	g_free(sc);
209248679Smav}
210248679Smav
211115960Sphkvoid
212105947Sphkg_dev_print(void)
213105947Sphk{
214105947Sphk	struct g_geom *gp;
215115960Sphk	char const *p = "";
216105947Sphk
217115960Sphk	LIST_FOREACH(gp, &g_dev_class.geom, geom) {
218115960Sphk		printf("%s%s", p, gp->name);
219115960Sphk		p = " ";
220115960Sphk	}
221105947Sphk	printf("\n");
222105947Sphk}
223105947Sphk
224223089Sgibbsstatic void
225308061Smavg_dev_set_physpath(struct g_consumer *cp)
226223089Sgibbs{
227248679Smav	struct g_dev_softc *sc;
228308061Smav	char *physpath;
229308061Smav	int error, physpath_len;
230308061Smav
231308061Smav	if (g_access(cp, 1, 0, 0) != 0)
232308061Smav		return;
233308061Smav
234308061Smav	sc = cp->private;
235308061Smav	physpath_len = MAXPATHLEN;
236308061Smav	physpath = g_malloc(physpath_len, M_WAITOK|M_ZERO);
237308061Smav	error = g_io_getattr("GEOM::physpath", cp, &physpath_len, physpath);
238308061Smav	g_access(cp, -1, 0, 0);
239308061Smav	if (error == 0 && strlen(physpath) != 0) {
240308061Smav		struct cdev *dev, *old_alias_dev;
241308061Smav		struct cdev **alias_devp;
242308061Smav
243308061Smav		dev = sc->sc_dev;
244308061Smav		old_alias_dev = sc->sc_alias;
245308061Smav		alias_devp = (struct cdev **)&sc->sc_alias;
246308061Smav		make_dev_physpath_alias(MAKEDEV_WAITOK, alias_devp, dev,
247308061Smav		    old_alias_dev, physpath);
248308061Smav	} else if (sc->sc_alias) {
249308061Smav		destroy_dev((struct cdev *)sc->sc_alias);
250308061Smav		sc->sc_alias = NULL;
251308061Smav	}
252308061Smav	g_free(physpath);
253308061Smav}
254308061Smav
255308061Smavstatic void
256308061Smavg_dev_set_media(struct g_consumer *cp)
257308061Smav{
258308061Smav	struct g_dev_softc *sc;
259238886Smav	struct cdev *dev;
260238886Smav	char buf[SPECNAMELEN + 6];
261223089Sgibbs
262248679Smav	sc = cp->private;
263308061Smav	dev = sc->sc_dev;
264308061Smav	snprintf(buf, sizeof(buf), "cdev=%s", dev->si_name);
265308061Smav	devctl_notify_f("DEVFS", "CDEV", "MEDIACHANGE", buf, M_WAITOK);
266308061Smav	devctl_notify_f("GEOM", "DEV", "MEDIACHANGE", buf, M_WAITOK);
267308061Smav	dev = sc->sc_alias;
268308061Smav	if (dev != NULL) {
269238886Smav		snprintf(buf, sizeof(buf), "cdev=%s", dev->si_name);
270238886Smav		devctl_notify_f("DEVFS", "CDEV", "MEDIACHANGE", buf, M_WAITOK);
271282953Strasz		devctl_notify_f("GEOM", "DEV", "MEDIACHANGE", buf, M_WAITOK);
272308061Smav	}
273308061Smav}
274308061Smav
275308061Smavstatic void
276308061Smavg_dev_attrchanged(struct g_consumer *cp, const char *attr)
277308061Smav{
278308061Smav
279308061Smav	if (strcmp(attr, "GEOM::media") == 0) {
280308061Smav		g_dev_set_media(cp);
281238886Smav		return;
282238886Smav	}
283238886Smav
284308061Smav	if (strcmp(attr, "GEOM::physpath") == 0) {
285308061Smav		g_dev_set_physpath(cp);
286223089Sgibbs		return;
287223089Sgibbs	}
288223089Sgibbs}
289223089Sgibbs
290119593Sphkstruct g_provider *
291130585Sphkg_dev_getprovider(struct cdev *dev)
292119593Sphk{
293119593Sphk	struct g_consumer *cp;
294119593Sphk
295135716Sphk	g_topology_assert();
296119593Sphk	if (dev == NULL)
297119593Sphk		return (NULL);
298135716Sphk	if (dev->si_devsw != &g_dev_cdevsw)
299143790Sphk		return (NULL);
300143790Sphk	cp = dev->si_drv2;
301119593Sphk	return (cp->provider);
302119593Sphk}
303119593Sphk
30492108Sphkstatic struct g_geom *
30593250Sphkg_dev_taste(struct g_class *mp, struct g_provider *pp, int insist __unused)
30692108Sphk{
30792108Sphk	struct g_geom *gp;
30892108Sphk	struct g_consumer *cp;
309248679Smav	struct g_dev_softc *sc;
310221071Smav	int error, len;
311221071Smav	struct cdev *dev, *adev;
312282953Strasz	char buf[SPECNAMELEN + 6], *val;
31392108Sphk
31492108Sphk	g_trace(G_T_TOPOLOGY, "dev_taste(%s,%s)", mp->name, pp->name);
31592108Sphk	g_topology_assert();
316243333Sjh	gp = g_new_geomf(mp, "%s", pp->name);
317248679Smav	sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
318248679Smav	mtx_init(&sc->sc_mtx, "g_dev", NULL, MTX_DEF);
31992108Sphk	cp = g_new_consumer(gp);
320248679Smav	cp->private = sc;
321260385Sscottl	cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
32296987Sphk	error = g_attach(cp, pp);
32396987Sphk	KASSERT(error == 0,
32496987Sphk	    ("g_dev_taste(%s) failed to g_attach, err=%d", pp->name, error));
325214063Sjh	error = make_dev_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK, &dev,
326214063Sjh	    &g_dev_cdevsw, NULL, UID_ROOT, GID_OPERATOR, 0640, "%s", gp->name);
327214063Sjh	if (error != 0) {
328214063Sjh		printf("%s: make_dev_p() failed (gp->name=%s, error=%d)\n",
329214063Sjh		    __func__, gp->name, error);
330214063Sjh		g_detach(cp);
331214063Sjh		g_destroy_consumer(cp);
332214063Sjh		g_destroy_geom(gp);
333248679Smav		mtx_destroy(&sc->sc_mtx);
334248679Smav		g_free(sc);
335214063Sjh		return (NULL);
336214063Sjh	}
337254389Sken	dev->si_flags |= SI_UNMAPPED;
338248679Smav	sc->sc_dev = dev;
339221071Smav
340221071Smav	/* Search for device alias name and create it if found. */
341221071Smav	adev = NULL;
342221071Smav	for (len = MIN(strlen(gp->name), sizeof(buf) - 15); len > 0; len--) {
343221071Smav		snprintf(buf, sizeof(buf), "kern.devalias.%s", gp->name);
344221071Smav		buf[14 + len] = 0;
345221071Smav		val = getenv(buf);
346221071Smav		if (val != NULL) {
347221071Smav			snprintf(buf, sizeof(buf), "%s%s",
348221071Smav			    val, gp->name + len);
349221071Smav			freeenv(val);
350221400Smav			make_dev_alias_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK,
351221400Smav			    &adev, dev, "%s", buf);
352254389Sken			adev->si_flags |= SI_UNMAPPED;
353221071Smav			break;
354221071Smav		}
355221071Smav	}
356221071Smav
357110728Sphk	dev->si_iosize_max = MAXPHYS;
35892108Sphk	dev->si_drv2 = cp;
359291548Ssmh	error = init_dumpdev(dev);
360291548Ssmh	if (error != 0)
361291548Ssmh		printf("%s: init_dumpdev() failed (gp->name=%s, error=%d)\n",
362291548Ssmh		    __func__, gp->name, error);
363221071Smav	if (adev != NULL) {
364221071Smav		adev->si_iosize_max = MAXPHYS;
365221071Smav		adev->si_drv2 = cp;
366273817Sae		init_dumpdev(adev);
367221071Smav	}
368223089Sgibbs
369223089Sgibbs	g_dev_attrchanged(cp, "GEOM::physpath");
370282953Strasz	snprintf(buf, sizeof(buf), "cdev=%s", gp->name);
371282953Strasz	devctl_notify_f("GEOM", "DEV", "CREATE", buf, M_WAITOK);
372223089Sgibbs
37392108Sphk	return (gp);
37492108Sphk}
37592108Sphk
37692108Sphkstatic int
377130585Sphkg_dev_open(struct cdev *dev, int flags, int fmt, struct thread *td)
37892108Sphk{
37992108Sphk	struct g_consumer *cp;
380248679Smav	struct g_dev_softc *sc;
38192108Sphk	int error, r, w, e;
38292108Sphk
38392108Sphk	cp = dev->si_drv2;
384248679Smav	if (cp == NULL)
385112978Sphk		return(ENXIO);		/* g_dev_taste() not done yet */
38692108Sphk	g_trace(G_T_ACCESS, "g_dev_open(%s, %d, %d, %p)",
387248679Smav	    cp->geom->name, flags, fmt, td);
388130712Sphk
38992108Sphk	r = flags & FREAD ? 1 : 0;
39092108Sphk	w = flags & FWRITE ? 1 : 0;
391103004Sphk#ifdef notyet
39292108Sphk	e = flags & O_EXCL ? 1 : 0;
393103004Sphk#else
394103004Sphk	e = 0;
395103004Sphk#endif
396289512Strasz
397289512Strasz	/*
398289512Strasz	 * This happens on attempt to open a device node with O_EXEC.
399289512Strasz	 */
400289512Strasz	if (r + w + e == 0)
401289512Strasz		return (EINVAL);
402289512Strasz
403130712Sphk	if (w) {
404130712Sphk		/*
405130712Sphk		 * When running in very secure mode, do not allow
406130712Sphk		 * opens for writing of any disks.
407130712Sphk		 */
408130712Sphk		error = securelevel_ge(td->td_ucred, 2);
409130712Sphk		if (error)
410130712Sphk			return (error);
411130712Sphk	}
412112978Sphk	g_topology_lock();
413248679Smav	error = g_access(cp, r, w, e);
41492108Sphk	g_topology_unlock();
415248679Smav	if (error == 0) {
416248679Smav		sc = cp->private;
417248679Smav		mtx_lock(&sc->sc_mtx);
418248679Smav		if (sc->sc_open == 0 && sc->sc_active != 0)
419248679Smav			wakeup(&sc->sc_active);
420248679Smav		sc->sc_open += r + w + e;
421248679Smav		mtx_unlock(&sc->sc_mtx);
422248679Smav	}
42392108Sphk	return(error);
42492108Sphk}
42592108Sphk
42692108Sphkstatic int
427130585Sphkg_dev_close(struct cdev *dev, int flags, int fmt, struct thread *td)
42892108Sphk{
42992108Sphk	struct g_consumer *cp;
430248679Smav	struct g_dev_softc *sc;
431248679Smav	int error, r, w, e;
43292108Sphk
43392108Sphk	cp = dev->si_drv2;
434248679Smav	if (cp == NULL)
43592108Sphk		return(ENXIO);
43692108Sphk	g_trace(G_T_ACCESS, "g_dev_close(%s, %d, %d, %p)",
437248679Smav	    cp->geom->name, flags, fmt, td);
438248679Smav
43992108Sphk	r = flags & FREAD ? -1 : 0;
44092108Sphk	w = flags & FWRITE ? -1 : 0;
441103004Sphk#ifdef notyet
44292108Sphk	e = flags & O_EXCL ? -1 : 0;
443103004Sphk#else
444103004Sphk	e = 0;
445103004Sphk#endif
446289511Strasz
447289511Strasz	/*
448289511Strasz	 * The vgonel(9) - caused by eg. forced unmount of devfs - calls
449289511Strasz	 * VOP_CLOSE(9) on devfs vnode without any FREAD or FWRITE flags,
450289511Strasz	 * which would result in zero deltas, which in turn would cause
451289511Strasz	 * panic in g_access(9).
452289511Strasz	 *
453289511Strasz	 * Note that we cannot zero the counters (ie. do "r = cp->acr"
454289511Strasz	 * etc) instead, because the consumer might be opened in another
455289511Strasz	 * devfs instance.
456289511Strasz	 */
457289511Strasz	if (r + w + e == 0)
458289511Strasz		return (EINVAL);
459289511Strasz
460248679Smav	sc = cp->private;
461248679Smav	mtx_lock(&sc->sc_mtx);
462248679Smav	sc->sc_open += r + w + e;
463248679Smav	while (sc->sc_open == 0 && sc->sc_active != 0)
464248679Smav		msleep(&sc->sc_active, &sc->sc_mtx, 0, "PRIBIO", 0);
465248679Smav	mtx_unlock(&sc->sc_mtx);
466112978Sphk	g_topology_lock();
467248679Smav	error = g_access(cp, r, w, e);
46892108Sphk	g_topology_unlock();
46992108Sphk	return (error);
47092108Sphk}
47192108Sphk
472112978Sphk/*
473112978Sphk * XXX: Until we have unmessed the ioctl situation, there is a race against
474112978Sphk * XXX: a concurrent orphanization.  We cannot close it by holding topology
475112978Sphk * XXX: since that would prevent us from doing our job, and stalling events
476112978Sphk * XXX: will break (actually: stall) the BSD disklabel hacks.
477112978Sphk */
47892108Sphkstatic int
479130585Sphkg_dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
48092108Sphk{
48192108Sphk	struct g_consumer *cp;
482182843Slulf	struct g_provider *pp;
483315705Smav	off_t offset, length, chunk, odd;
48492108Sphk	int i, error;
48592108Sphk
48692108Sphk	cp = dev->si_drv2;
487182843Slulf	pp = cp->provider;
48892108Sphk
48992108Sphk	error = 0;
490112978Sphk	KASSERT(cp->acr || cp->acw,
491112978Sphk	    ("Consumer with zero access count in g_dev_ioctl"));
49292403Sphk
49392698Sphk	i = IOCPARM_LEN(cmd);
49492698Sphk	switch (cmd) {
49592698Sphk	case DIOCGSECTORSIZE:
496105551Sphk		*(u_int *)data = cp->provider->sectorsize;
497105551Sphk		if (*(u_int *)data == 0)
498105180Snjl			error = ENOENT;
49992698Sphk		break;
50092698Sphk	case DIOCGMEDIASIZE:
501105551Sphk		*(off_t *)data = cp->provider->mediasize;
502105551Sphk		if (*(off_t *)data == 0)
503105180Snjl			error = ENOENT;
50492698Sphk		break;
50592698Sphk	case DIOCGFWSECTORS:
50693250Sphk		error = g_io_getattr("GEOM::fwsectors", cp, &i, data);
507105180Snjl		if (error == 0 && *(u_int *)data == 0)
508105180Snjl			error = ENOENT;
50992698Sphk		break;
51092698Sphk	case DIOCGFWHEADS:
51193250Sphk		error = g_io_getattr("GEOM::fwheads", cp, &i, data);
512105180Snjl		if (error == 0 && *(u_int *)data == 0)
513105180Snjl			error = ENOENT;
51492698Sphk		break;
51594287Sphk	case DIOCGFRONTSTUFF:
51694287Sphk		error = g_io_getattr("GEOM::frontstuff", cp, &i, data);
51794287Sphk		break;
51895038Sphk	case DIOCSKERNELDUMP:
519273817Sae		if (*(u_int *)data == 0)
520291215Ssmh			error = g_dev_setdumpdev(NULL, td);
521273817Sae		else
522291215Ssmh			error = g_dev_setdumpdev(dev, td);
52395038Sphk		break;
524169284Spjd	case DIOCGFLUSH:
525169284Spjd		error = g_io_flush(cp);
526169284Spjd		break;
527169284Spjd	case DIOCGDELETE:
528169284Spjd		offset = ((off_t *)data)[0];
529169284Spjd		length = ((off_t *)data)[1];
530169284Spjd		if ((offset % cp->provider->sectorsize) != 0 ||
531174669Sphk		    (length % cp->provider->sectorsize) != 0 || length <= 0) {
532169284Spjd			printf("%s: offset=%jd length=%jd\n", __func__, offset,
533169284Spjd			    length);
534169284Spjd			error = EINVAL;
535169284Spjd			break;
536169284Spjd		}
537174674Sphk		while (length > 0) {
538174674Sphk			chunk = length;
539249930Ssmh			if (g_dev_del_max_sectors != 0 && chunk >
540249930Ssmh			    g_dev_del_max_sectors * cp->provider->sectorsize) {
541249930Ssmh				chunk = g_dev_del_max_sectors *
542249930Ssmh				    cp->provider->sectorsize;
543315705Smav				if (cp->provider->stripesize > 0) {
544315705Smav					odd = (offset + chunk +
545315705Smav					    cp->provider->stripeoffset) %
546315705Smav					    cp->provider->stripesize;
547315705Smav					if (chunk > odd)
548315705Smav						chunk -= odd;
549315705Smav				}
550249930Ssmh			}
551174674Sphk			error = g_delete_data(cp, offset, chunk);
552174674Sphk			length -= chunk;
553174674Sphk			offset += chunk;
554174674Sphk			if (error)
555174674Sphk				break;
556174674Sphk			/*
557249930Ssmh			 * Since the request size can be large, the service
558249930Ssmh			 * time can be is likewise.  We make this ioctl
559249930Ssmh			 * interruptible by checking for signals for each bio.
560174674Sphk			 */
561174674Sphk			if (SIGPENDING(td))
562174674Sphk				break;
563174674Sphk		}
564169284Spjd		break;
565169284Spjd	case DIOCGIDENT:
566169284Spjd		error = g_io_getattr("GEOM::ident", cp, &i, data);
567169284Spjd		break;
568182843Slulf	case DIOCGPROVIDERNAME:
569182843Slulf		if (pp == NULL)
570182843Slulf			return (ENOENT);
571182843Slulf		strlcpy(data, pp->name, i);
572182843Slulf		break;
573200934Smav	case DIOCGSTRIPESIZE:
574200934Smav		*(off_t *)data = cp->provider->stripesize;
575200934Smav		break;
576200934Smav	case DIOCGSTRIPEOFFSET:
577200934Smav		*(off_t *)data = cp->provider->stripeoffset;
578200934Smav		break;
579223089Sgibbs	case DIOCGPHYSPATH:
580223089Sgibbs		error = g_io_getattr("GEOM::physpath", cp, &i, data);
581223089Sgibbs		if (error == 0 && *(char *)data == '\0')
582223089Sgibbs			error = ENOENT;
583223089Sgibbs		break;
584274732Smav	case DIOCGATTR: {
585274732Smav		struct diocgattr_arg *arg = (struct diocgattr_arg *)data;
586274732Smav
587274732Smav		if (arg->len > sizeof(arg->value)) {
588274732Smav			error = EINVAL;
589274732Smav			break;
590274732Smav		}
591274732Smav		error = g_io_getattr(arg->name, cp, &arg->len, &arg->value);
592274732Smav		break;
593274732Smav	}
59492698Sphk	default:
595119660Sphk		if (cp->provider->geom->ioctl != NULL) {
596138732Sphk			error = cp->provider->geom->ioctl(cp->provider, cmd, data, fflag, td);
597119749Sphk		} else {
598119749Sphk			error = ENOIOCTL;
599119660Sphk		}
60092698Sphk	}
60192403Sphk
60292108Sphk	return (error);
60392108Sphk}
60492108Sphk
60592108Sphkstatic void
60692108Sphkg_dev_done(struct bio *bp2)
60792108Sphk{
608248679Smav	struct g_consumer *cp;
609248679Smav	struct g_dev_softc *sc;
61092108Sphk	struct bio *bp;
611248679Smav	int destroy;
61292108Sphk
613248679Smav	cp = bp2->bio_from;
614248679Smav	sc = cp->private;
615110517Sphk	bp = bp2->bio_parent;
61692108Sphk	bp->bio_error = bp2->bio_error;
617260385Sscottl	bp->bio_completed = bp2->bio_completed;
618260385Sscottl	bp->bio_resid = bp->bio_length - bp2->bio_completed;
619260385Sscottl	if (bp2->bio_error != 0) {
62092108Sphk		g_trace(G_T_BIO, "g_dev_done(%p) had error %d",
621260385Sscottl		    bp2, bp2->bio_error);
62292108Sphk		bp->bio_flags |= BIO_ERROR;
62392108Sphk	} else {
624105540Sphk		g_trace(G_T_BIO, "g_dev_done(%p/%p) resid %ld completed %jd",
625260385Sscottl		    bp2, bp, bp2->bio_resid, (intmax_t)bp2->bio_completed);
62692108Sphk	}
62792108Sphk	g_destroy_bio(bp2);
628248679Smav	destroy = 0;
629248679Smav	mtx_lock(&sc->sc_mtx);
630248679Smav	if ((--sc->sc_active) == 0) {
631248679Smav		if (sc->sc_open == 0)
632248679Smav			wakeup(&sc->sc_active);
633248679Smav		if (sc->sc_dev == NULL)
634248679Smav			destroy = 1;
635248679Smav	}
636248679Smav	mtx_unlock(&sc->sc_mtx);
637248679Smav	if (destroy)
638287850Simp		g_post_event(g_dev_destroy, cp, M_NOWAIT, NULL);
63992108Sphk	biodone(bp);
64092108Sphk}
64192108Sphk
64292108Sphkstatic void
64392108Sphkg_dev_strategy(struct bio *bp)
64492108Sphk{
64592108Sphk	struct g_consumer *cp;
64692108Sphk	struct bio *bp2;
647130585Sphk	struct cdev *dev;
648248679Smav	struct g_dev_softc *sc;
64992108Sphk
650106300Sphk	KASSERT(bp->bio_cmd == BIO_READ ||
651106300Sphk	        bp->bio_cmd == BIO_WRITE ||
652249193Strasz	        bp->bio_cmd == BIO_DELETE ||
653249193Strasz		bp->bio_cmd == BIO_FLUSH,
654106300Sphk		("Wrong bio_cmd bio=%p cmd=%d", bp, bp->bio_cmd));
65592108Sphk	dev = bp->bio_dev;
65692108Sphk	cp = dev->si_drv2;
657248679Smav	sc = cp->private;
658112978Sphk	KASSERT(cp->acr || cp->acw,
659112978Sphk	    ("Consumer with zero access count in g_dev_strategy"));
660196964Smav#ifdef INVARIANTS
661135865Spjd	if ((bp->bio_offset % cp->provider->sectorsize) != 0 ||
662135865Spjd	    (bp->bio_bcount % cp->provider->sectorsize) != 0) {
663159756Ssimon		bp->bio_resid = bp->bio_bcount;
664135865Spjd		biofinish(bp, NULL, EINVAL);
665135865Spjd		return;
666135865Spjd	}
667196964Smav#endif
668248679Smav	mtx_lock(&sc->sc_mtx);
669248679Smav	KASSERT(sc->sc_open > 0, ("Closed device in g_dev_strategy"));
670248679Smav	sc->sc_active++;
671248679Smav	mtx_unlock(&sc->sc_mtx);
672248679Smav
673118869Sphk	for (;;) {
674118869Sphk		/*
675118869Sphk		 * XXX: This is not an ideal solution, but I belive it to
676118869Sphk		 * XXX: deadlock safe, all things considered.
677118869Sphk		 */
678118869Sphk		bp2 = g_clone_bio(bp);
679118869Sphk		if (bp2 != NULL)
680118869Sphk			break;
681167086Sjhb		pause("gdstrat", hz / 10);
682118869Sphk	}
683107834Sphk	KASSERT(bp2 != NULL, ("XXX: ENOMEM in a bad place"));
68492108Sphk	bp2->bio_done = g_dev_done;
68592108Sphk	g_trace(G_T_BIO,
686105540Sphk	    "g_dev_strategy(%p/%p) offset %jd length %jd data %p cmd %d",
687105540Sphk	    bp, bp2, (intmax_t)bp->bio_offset, (intmax_t)bp2->bio_length,
688105540Sphk	    bp2->bio_data, bp2->bio_cmd);
68992108Sphk	g_io_request(bp2, cp);
690112978Sphk	KASSERT(cp->acr || cp->acw,
691112978Sphk	    ("g_dev_strategy raced with g_dev_close and lost"));
692112978Sphk
69392108Sphk}
69492108Sphk
69596987Sphk/*
696248679Smav * g_dev_callback()
697248679Smav *
698248679Smav * Called by devfs when asynchronous device destruction is completed.
699248679Smav * - Mark that we have no attached device any more.
700248679Smav * - If there are no outstanding requests, schedule geom destruction.
701248679Smav *   Otherwise destruction will be scheduled later by g_dev_done().
702248679Smav */
703248679Smav
704248679Smavstatic void
705248679Smavg_dev_callback(void *arg)
706248679Smav{
707248679Smav	struct g_consumer *cp;
708248679Smav	struct g_dev_softc *sc;
709248679Smav	int destroy;
710248679Smav
711248679Smav	cp = arg;
712248679Smav	sc = cp->private;
713248679Smav	g_trace(G_T_TOPOLOGY, "g_dev_callback(%p(%s))", cp, cp->geom->name);
714248679Smav
715248679Smav	mtx_lock(&sc->sc_mtx);
716248679Smav	sc->sc_dev = NULL;
717248679Smav	sc->sc_alias = NULL;
718248679Smav	destroy = (sc->sc_active == 0);
719248679Smav	mtx_unlock(&sc->sc_mtx);
720248679Smav	if (destroy)
721248679Smav		g_post_event(g_dev_destroy, cp, M_WAITOK, NULL);
722248679Smav}
723248679Smav
724248679Smav/*
72596987Sphk * g_dev_orphan()
72696987Sphk *
727112024Sphk * Called from below when the provider orphaned us.
728112024Sphk * - Clear any dump settings.
729248679Smav * - Request asynchronous device destruction to prevent any more requests
730248679Smav *   from coming in.  The provider is already marked with an error, so
731248679Smav *   anything which comes in in the interrim will be returned immediately.
73296987Sphk */
73392108Sphk
73492108Sphkstatic void
73593250Sphkg_dev_orphan(struct g_consumer *cp)
73692108Sphk{
737130585Sphk	struct cdev *dev;
738248679Smav	struct g_dev_softc *sc;
73992108Sphk
740112024Sphk	g_topology_assert();
741248679Smav	sc = cp->private;
742248679Smav	dev = sc->sc_dev;
743248679Smav	g_trace(G_T_TOPOLOGY, "g_dev_orphan(%p(%s))", cp, cp->geom->name);
744112024Sphk
745112024Sphk	/* Reset any dump-area set on this device */
74695038Sphk	if (dev->si_flags & SI_DUMPDEV)
747291215Ssmh		(void)set_dumper(NULL, NULL, curthread);
748112024Sphk
749130585Sphk	/* Destroy the struct cdev *so we get no more requests */
750248679Smav	destroy_dev_sched_cb(dev, g_dev_callback, cp);
75192108Sphk}
75292108Sphk
75396987SphkDECLARE_GEOM_CLASS(g_dev_class, g_dev);
754