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: releng/10.3/sys/geom/geom_dev.c 291548 2015-12-01 00:53:03Z smh $");
38116196Sobrien
3992108Sphk#include <sys/param.h>
4092108Sphk#include <sys/systm.h>
4192108Sphk#include <sys/malloc.h>
4292108Sphk#include <sys/kernel.h>
4392108Sphk#include <sys/conf.h>
44223089Sgibbs#include <sys/ctype.h>
4592108Sphk#include <sys/bio.h>
46238886Smav#include <sys/bus.h>
4792108Sphk#include <sys/lock.h>
4892108Sphk#include <sys/mutex.h>
49130712Sphk#include <sys/proc.h>
5092108Sphk#include <sys/errno.h>
5192108Sphk#include <sys/time.h>
5292108Sphk#include <sys/disk.h>
5392108Sphk#include <sys/fcntl.h>
54114216Skan#include <sys/limits.h>
55249930Ssmh#include <sys/sysctl.h>
5692108Sphk#include <geom/geom.h>
5795323Sphk#include <geom/geom_int.h>
58223089Sgibbs#include <machine/stdarg.h>
5992108Sphk
60248679Smavstruct g_dev_softc {
61248679Smav	struct mtx	 sc_mtx;
62248679Smav	struct cdev	*sc_dev;
63248679Smav	struct cdev	*sc_alias;
64248679Smav	int		 sc_open;
65248679Smav	int		 sc_active;
66248679Smav};
67223089Sgibbs
6892108Sphkstatic d_open_t		g_dev_open;
6992108Sphkstatic d_close_t	g_dev_close;
7092108Sphkstatic d_strategy_t	g_dev_strategy;
7192108Sphkstatic d_ioctl_t	g_dev_ioctl;
7292108Sphk
7392108Sphkstatic struct cdevsw g_dev_cdevsw = {
74126080Sphk	.d_version =	D_VERSION,
75111815Sphk	.d_open =	g_dev_open,
76111815Sphk	.d_close =	g_dev_close,
77111815Sphk	.d_read =	physread,
78111815Sphk	.d_write =	physwrite,
79111815Sphk	.d_ioctl =	g_dev_ioctl,
80111815Sphk	.d_strategy =	g_dev_strategy,
81111815Sphk	.d_name =	"g_dev",
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
225223089Sgibbsg_dev_attrchanged(struct g_consumer *cp, const char *attr)
226223089Sgibbs{
227248679Smav	struct g_dev_softc *sc;
228238886Smav	struct cdev *dev;
229238886Smav	char buf[SPECNAMELEN + 6];
230223089Sgibbs
231248679Smav	sc = cp->private;
232238886Smav	if (strcmp(attr, "GEOM::media") == 0) {
233248679Smav		dev = sc->sc_dev;
234238886Smav		snprintf(buf, sizeof(buf), "cdev=%s", dev->si_name);
235238886Smav		devctl_notify_f("DEVFS", "CDEV", "MEDIACHANGE", buf, M_WAITOK);
236282953Strasz		devctl_notify_f("GEOM", "DEV", "MEDIACHANGE", buf, M_WAITOK);
237248679Smav		dev = sc->sc_alias;
238238886Smav		if (dev != NULL) {
239238886Smav			snprintf(buf, sizeof(buf), "cdev=%s", dev->si_name);
240238886Smav			devctl_notify_f("DEVFS", "CDEV", "MEDIACHANGE", buf,
241238886Smav			    M_WAITOK);
242282953Strasz			devctl_notify_f("GEOM", "DEV", "MEDIACHANGE", buf,
243282953Strasz			    M_WAITOK);
244238886Smav		}
245238886Smav		return;
246238886Smav	}
247238886Smav
248223089Sgibbs	if (strcmp(attr, "GEOM::physpath") != 0)
249223089Sgibbs		return;
250223089Sgibbs
251223089Sgibbs	if (g_access(cp, 1, 0, 0) == 0) {
252223089Sgibbs		char *physpath;
253223089Sgibbs		int error, physpath_len;
254223089Sgibbs
255223089Sgibbs		physpath_len = MAXPATHLEN;
256223089Sgibbs		physpath = g_malloc(physpath_len, M_WAITOK|M_ZERO);
257223089Sgibbs		error =
258223089Sgibbs		    g_io_getattr("GEOM::physpath", cp, &physpath_len, physpath);
259223089Sgibbs		g_access(cp, -1, 0, 0);
260223089Sgibbs		if (error == 0 && strlen(physpath) != 0) {
261223089Sgibbs			struct cdev *old_alias_dev;
262223089Sgibbs			struct cdev **alias_devp;
263223089Sgibbs
264248679Smav			dev = sc->sc_dev;
265248679Smav			old_alias_dev = sc->sc_alias;
266248679Smav			alias_devp = (struct cdev **)&sc->sc_alias;
267223089Sgibbs			make_dev_physpath_alias(MAKEDEV_WAITOK, alias_devp,
268223089Sgibbs			    dev, old_alias_dev, physpath);
269248679Smav		} else if (sc->sc_alias) {
270248679Smav			destroy_dev((struct cdev *)sc->sc_alias);
271248679Smav			sc->sc_alias = NULL;
272223089Sgibbs		}
273223089Sgibbs		g_free(physpath);
274223089Sgibbs	}
275223089Sgibbs}
276223089Sgibbs
277119593Sphkstruct g_provider *
278130585Sphkg_dev_getprovider(struct cdev *dev)
279119593Sphk{
280119593Sphk	struct g_consumer *cp;
281119593Sphk
282135716Sphk	g_topology_assert();
283119593Sphk	if (dev == NULL)
284119593Sphk		return (NULL);
285135716Sphk	if (dev->si_devsw != &g_dev_cdevsw)
286143790Sphk		return (NULL);
287143790Sphk	cp = dev->si_drv2;
288119593Sphk	return (cp->provider);
289119593Sphk}
290119593Sphk
29192108Sphkstatic struct g_geom *
29293250Sphkg_dev_taste(struct g_class *mp, struct g_provider *pp, int insist __unused)
29392108Sphk{
29492108Sphk	struct g_geom *gp;
29592108Sphk	struct g_consumer *cp;
296248679Smav	struct g_dev_softc *sc;
297221071Smav	int error, len;
298221071Smav	struct cdev *dev, *adev;
299282953Strasz	char buf[SPECNAMELEN + 6], *val;
30092108Sphk
30192108Sphk	g_trace(G_T_TOPOLOGY, "dev_taste(%s,%s)", mp->name, pp->name);
30292108Sphk	g_topology_assert();
303243333Sjh	gp = g_new_geomf(mp, "%s", pp->name);
304248679Smav	sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
305248679Smav	mtx_init(&sc->sc_mtx, "g_dev", NULL, MTX_DEF);
30692108Sphk	cp = g_new_consumer(gp);
307248679Smav	cp->private = sc;
308260385Sscottl	cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
30996987Sphk	error = g_attach(cp, pp);
31096987Sphk	KASSERT(error == 0,
31196987Sphk	    ("g_dev_taste(%s) failed to g_attach, err=%d", pp->name, error));
312214063Sjh	error = make_dev_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK, &dev,
313214063Sjh	    &g_dev_cdevsw, NULL, UID_ROOT, GID_OPERATOR, 0640, "%s", gp->name);
314214063Sjh	if (error != 0) {
315214063Sjh		printf("%s: make_dev_p() failed (gp->name=%s, error=%d)\n",
316214063Sjh		    __func__, gp->name, error);
317214063Sjh		g_detach(cp);
318214063Sjh		g_destroy_consumer(cp);
319214063Sjh		g_destroy_geom(gp);
320248679Smav		mtx_destroy(&sc->sc_mtx);
321248679Smav		g_free(sc);
322214063Sjh		return (NULL);
323214063Sjh	}
324254389Sken	dev->si_flags |= SI_UNMAPPED;
325248679Smav	sc->sc_dev = dev;
326221071Smav
327221071Smav	/* Search for device alias name and create it if found. */
328221071Smav	adev = NULL;
329221071Smav	for (len = MIN(strlen(gp->name), sizeof(buf) - 15); len > 0; len--) {
330221071Smav		snprintf(buf, sizeof(buf), "kern.devalias.%s", gp->name);
331221071Smav		buf[14 + len] = 0;
332221071Smav		val = getenv(buf);
333221071Smav		if (val != NULL) {
334221071Smav			snprintf(buf, sizeof(buf), "%s%s",
335221071Smav			    val, gp->name + len);
336221071Smav			freeenv(val);
337221400Smav			make_dev_alias_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK,
338221400Smav			    &adev, dev, "%s", buf);
339254389Sken			adev->si_flags |= SI_UNMAPPED;
340221071Smav			break;
341221071Smav		}
342221071Smav	}
343221071Smav
344110728Sphk	dev->si_iosize_max = MAXPHYS;
34592108Sphk	dev->si_drv2 = cp;
346291548Ssmh	error = init_dumpdev(dev);
347291548Ssmh	if (error != 0)
348291548Ssmh		printf("%s: init_dumpdev() failed (gp->name=%s, error=%d)\n",
349291548Ssmh		    __func__, gp->name, error);
350221071Smav	if (adev != NULL) {
351221071Smav		adev->si_iosize_max = MAXPHYS;
352221071Smav		adev->si_drv2 = cp;
353273817Sae		init_dumpdev(adev);
354221071Smav	}
355223089Sgibbs
356223089Sgibbs	g_dev_attrchanged(cp, "GEOM::physpath");
357282953Strasz	snprintf(buf, sizeof(buf), "cdev=%s", gp->name);
358282953Strasz	devctl_notify_f("GEOM", "DEV", "CREATE", buf, M_WAITOK);
359223089Sgibbs
36092108Sphk	return (gp);
36192108Sphk}
36292108Sphk
36392108Sphkstatic int
364130585Sphkg_dev_open(struct cdev *dev, int flags, int fmt, struct thread *td)
36592108Sphk{
36692108Sphk	struct g_consumer *cp;
367248679Smav	struct g_dev_softc *sc;
36892108Sphk	int error, r, w, e;
36992108Sphk
37092108Sphk	cp = dev->si_drv2;
371248679Smav	if (cp == NULL)
372112978Sphk		return(ENXIO);		/* g_dev_taste() not done yet */
37392108Sphk	g_trace(G_T_ACCESS, "g_dev_open(%s, %d, %d, %p)",
374248679Smav	    cp->geom->name, flags, fmt, td);
375130712Sphk
37692108Sphk	r = flags & FREAD ? 1 : 0;
37792108Sphk	w = flags & FWRITE ? 1 : 0;
378103004Sphk#ifdef notyet
37992108Sphk	e = flags & O_EXCL ? 1 : 0;
380103004Sphk#else
381103004Sphk	e = 0;
382103004Sphk#endif
383289512Strasz
384289512Strasz	/*
385289512Strasz	 * This happens on attempt to open a device node with O_EXEC.
386289512Strasz	 */
387289512Strasz	if (r + w + e == 0)
388289512Strasz		return (EINVAL);
389289512Strasz
390130712Sphk	if (w) {
391130712Sphk		/*
392130712Sphk		 * When running in very secure mode, do not allow
393130712Sphk		 * opens for writing of any disks.
394130712Sphk		 */
395130712Sphk		error = securelevel_ge(td->td_ucred, 2);
396130712Sphk		if (error)
397130712Sphk			return (error);
398130712Sphk	}
399112978Sphk	g_topology_lock();
400248679Smav	error = g_access(cp, r, w, e);
40192108Sphk	g_topology_unlock();
402248679Smav	if (error == 0) {
403248679Smav		sc = cp->private;
404248679Smav		mtx_lock(&sc->sc_mtx);
405248679Smav		if (sc->sc_open == 0 && sc->sc_active != 0)
406248679Smav			wakeup(&sc->sc_active);
407248679Smav		sc->sc_open += r + w + e;
408248679Smav		mtx_unlock(&sc->sc_mtx);
409248679Smav	}
41092108Sphk	return(error);
41192108Sphk}
41292108Sphk
41392108Sphkstatic int
414130585Sphkg_dev_close(struct cdev *dev, int flags, int fmt, struct thread *td)
41592108Sphk{
41692108Sphk	struct g_consumer *cp;
417248679Smav	struct g_dev_softc *sc;
418248679Smav	int error, r, w, e;
41992108Sphk
42092108Sphk	cp = dev->si_drv2;
421248679Smav	if (cp == NULL)
42292108Sphk		return(ENXIO);
42392108Sphk	g_trace(G_T_ACCESS, "g_dev_close(%s, %d, %d, %p)",
424248679Smav	    cp->geom->name, flags, fmt, td);
425248679Smav
42692108Sphk	r = flags & FREAD ? -1 : 0;
42792108Sphk	w = flags & FWRITE ? -1 : 0;
428103004Sphk#ifdef notyet
42992108Sphk	e = flags & O_EXCL ? -1 : 0;
430103004Sphk#else
431103004Sphk	e = 0;
432103004Sphk#endif
433289511Strasz
434289511Strasz	/*
435289511Strasz	 * The vgonel(9) - caused by eg. forced unmount of devfs - calls
436289511Strasz	 * VOP_CLOSE(9) on devfs vnode without any FREAD or FWRITE flags,
437289511Strasz	 * which would result in zero deltas, which in turn would cause
438289511Strasz	 * panic in g_access(9).
439289511Strasz	 *
440289511Strasz	 * Note that we cannot zero the counters (ie. do "r = cp->acr"
441289511Strasz	 * etc) instead, because the consumer might be opened in another
442289511Strasz	 * devfs instance.
443289511Strasz	 */
444289511Strasz	if (r + w + e == 0)
445289511Strasz		return (EINVAL);
446289511Strasz
447248679Smav	sc = cp->private;
448248679Smav	mtx_lock(&sc->sc_mtx);
449248679Smav	sc->sc_open += r + w + e;
450248679Smav	while (sc->sc_open == 0 && sc->sc_active != 0)
451248679Smav		msleep(&sc->sc_active, &sc->sc_mtx, 0, "PRIBIO", 0);
452248679Smav	mtx_unlock(&sc->sc_mtx);
453112978Sphk	g_topology_lock();
454248679Smav	error = g_access(cp, r, w, e);
45592108Sphk	g_topology_unlock();
45692108Sphk	return (error);
45792108Sphk}
45892108Sphk
459112978Sphk/*
460112978Sphk * XXX: Until we have unmessed the ioctl situation, there is a race against
461112978Sphk * XXX: a concurrent orphanization.  We cannot close it by holding topology
462112978Sphk * XXX: since that would prevent us from doing our job, and stalling events
463112978Sphk * XXX: will break (actually: stall) the BSD disklabel hacks.
464112978Sphk */
46592108Sphkstatic int
466130585Sphkg_dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
46792108Sphk{
46892108Sphk	struct g_consumer *cp;
469182843Slulf	struct g_provider *pp;
470174674Sphk	off_t offset, length, chunk;
47192108Sphk	int i, error;
47292108Sphk
47392108Sphk	cp = dev->si_drv2;
474182843Slulf	pp = cp->provider;
47592108Sphk
47692108Sphk	error = 0;
477112978Sphk	KASSERT(cp->acr || cp->acw,
478112978Sphk	    ("Consumer with zero access count in g_dev_ioctl"));
47992403Sphk
48092698Sphk	i = IOCPARM_LEN(cmd);
48192698Sphk	switch (cmd) {
48292698Sphk	case DIOCGSECTORSIZE:
483105551Sphk		*(u_int *)data = cp->provider->sectorsize;
484105551Sphk		if (*(u_int *)data == 0)
485105180Snjl			error = ENOENT;
48692698Sphk		break;
48792698Sphk	case DIOCGMEDIASIZE:
488105551Sphk		*(off_t *)data = cp->provider->mediasize;
489105551Sphk		if (*(off_t *)data == 0)
490105180Snjl			error = ENOENT;
49192698Sphk		break;
49292698Sphk	case DIOCGFWSECTORS:
49393250Sphk		error = g_io_getattr("GEOM::fwsectors", cp, &i, data);
494105180Snjl		if (error == 0 && *(u_int *)data == 0)
495105180Snjl			error = ENOENT;
49692698Sphk		break;
49792698Sphk	case DIOCGFWHEADS:
49893250Sphk		error = g_io_getattr("GEOM::fwheads", cp, &i, data);
499105180Snjl		if (error == 0 && *(u_int *)data == 0)
500105180Snjl			error = ENOENT;
50192698Sphk		break;
50294287Sphk	case DIOCGFRONTSTUFF:
50394287Sphk		error = g_io_getattr("GEOM::frontstuff", cp, &i, data);
50494287Sphk		break;
50595038Sphk	case DIOCSKERNELDUMP:
506273817Sae		if (*(u_int *)data == 0)
507291215Ssmh			error = g_dev_setdumpdev(NULL, td);
508273817Sae		else
509291215Ssmh			error = g_dev_setdumpdev(dev, td);
51095038Sphk		break;
511169284Spjd	case DIOCGFLUSH:
512169284Spjd		error = g_io_flush(cp);
513169284Spjd		break;
514169284Spjd	case DIOCGDELETE:
515169284Spjd		offset = ((off_t *)data)[0];
516169284Spjd		length = ((off_t *)data)[1];
517169284Spjd		if ((offset % cp->provider->sectorsize) != 0 ||
518174669Sphk		    (length % cp->provider->sectorsize) != 0 || length <= 0) {
519169284Spjd			printf("%s: offset=%jd length=%jd\n", __func__, offset,
520169284Spjd			    length);
521169284Spjd			error = EINVAL;
522169284Spjd			break;
523169284Spjd		}
524174674Sphk		while (length > 0) {
525174674Sphk			chunk = length;
526249930Ssmh			if (g_dev_del_max_sectors != 0 && chunk >
527249930Ssmh			    g_dev_del_max_sectors * cp->provider->sectorsize) {
528249930Ssmh				chunk = g_dev_del_max_sectors *
529249930Ssmh				    cp->provider->sectorsize;
530249930Ssmh			}
531174674Sphk			error = g_delete_data(cp, offset, chunk);
532174674Sphk			length -= chunk;
533174674Sphk			offset += chunk;
534174674Sphk			if (error)
535174674Sphk				break;
536174674Sphk			/*
537249930Ssmh			 * Since the request size can be large, the service
538249930Ssmh			 * time can be is likewise.  We make this ioctl
539249930Ssmh			 * interruptible by checking for signals for each bio.
540174674Sphk			 */
541174674Sphk			if (SIGPENDING(td))
542174674Sphk				break;
543174674Sphk		}
544169284Spjd		break;
545169284Spjd	case DIOCGIDENT:
546169284Spjd		error = g_io_getattr("GEOM::ident", cp, &i, data);
547169284Spjd		break;
548182843Slulf	case DIOCGPROVIDERNAME:
549182843Slulf		if (pp == NULL)
550182843Slulf			return (ENOENT);
551182843Slulf		strlcpy(data, pp->name, i);
552182843Slulf		break;
553200934Smav	case DIOCGSTRIPESIZE:
554200934Smav		*(off_t *)data = cp->provider->stripesize;
555200934Smav		break;
556200934Smav	case DIOCGSTRIPEOFFSET:
557200934Smav		*(off_t *)data = cp->provider->stripeoffset;
558200934Smav		break;
559223089Sgibbs	case DIOCGPHYSPATH:
560223089Sgibbs		error = g_io_getattr("GEOM::physpath", cp, &i, data);
561223089Sgibbs		if (error == 0 && *(char *)data == '\0')
562223089Sgibbs			error = ENOENT;
563223089Sgibbs		break;
564274732Smav	case DIOCGATTR: {
565274732Smav		struct diocgattr_arg *arg = (struct diocgattr_arg *)data;
566274732Smav
567274732Smav		if (arg->len > sizeof(arg->value)) {
568274732Smav			error = EINVAL;
569274732Smav			break;
570274732Smav		}
571274732Smav		error = g_io_getattr(arg->name, cp, &arg->len, &arg->value);
572274732Smav		break;
573274732Smav	}
57492698Sphk	default:
575119660Sphk		if (cp->provider->geom->ioctl != NULL) {
576138732Sphk			error = cp->provider->geom->ioctl(cp->provider, cmd, data, fflag, td);
577119749Sphk		} else {
578119749Sphk			error = ENOIOCTL;
579119660Sphk		}
58092698Sphk	}
58192403Sphk
58292108Sphk	return (error);
58392108Sphk}
58492108Sphk
58592108Sphkstatic void
58692108Sphkg_dev_done(struct bio *bp2)
58792108Sphk{
588248679Smav	struct g_consumer *cp;
589248679Smav	struct g_dev_softc *sc;
59092108Sphk	struct bio *bp;
591248679Smav	int destroy;
59292108Sphk
593248679Smav	cp = bp2->bio_from;
594248679Smav	sc = cp->private;
595110517Sphk	bp = bp2->bio_parent;
59692108Sphk	bp->bio_error = bp2->bio_error;
597260385Sscottl	bp->bio_completed = bp2->bio_completed;
598260385Sscottl	bp->bio_resid = bp->bio_length - bp2->bio_completed;
599260385Sscottl	if (bp2->bio_error != 0) {
60092108Sphk		g_trace(G_T_BIO, "g_dev_done(%p) had error %d",
601260385Sscottl		    bp2, bp2->bio_error);
60292108Sphk		bp->bio_flags |= BIO_ERROR;
60392108Sphk	} else {
604105540Sphk		g_trace(G_T_BIO, "g_dev_done(%p/%p) resid %ld completed %jd",
605260385Sscottl		    bp2, bp, bp2->bio_resid, (intmax_t)bp2->bio_completed);
60692108Sphk	}
60792108Sphk	g_destroy_bio(bp2);
608248679Smav	destroy = 0;
609248679Smav	mtx_lock(&sc->sc_mtx);
610248679Smav	if ((--sc->sc_active) == 0) {
611248679Smav		if (sc->sc_open == 0)
612248679Smav			wakeup(&sc->sc_active);
613248679Smav		if (sc->sc_dev == NULL)
614248679Smav			destroy = 1;
615248679Smav	}
616248679Smav	mtx_unlock(&sc->sc_mtx);
617248679Smav	if (destroy)
618287850Simp		g_post_event(g_dev_destroy, cp, M_NOWAIT, NULL);
61992108Sphk	biodone(bp);
62092108Sphk}
62192108Sphk
62292108Sphkstatic void
62392108Sphkg_dev_strategy(struct bio *bp)
62492108Sphk{
62592108Sphk	struct g_consumer *cp;
62692108Sphk	struct bio *bp2;
627130585Sphk	struct cdev *dev;
628248679Smav	struct g_dev_softc *sc;
62992108Sphk
630106300Sphk	KASSERT(bp->bio_cmd == BIO_READ ||
631106300Sphk	        bp->bio_cmd == BIO_WRITE ||
632249193Strasz	        bp->bio_cmd == BIO_DELETE ||
633249193Strasz		bp->bio_cmd == BIO_FLUSH,
634106300Sphk		("Wrong bio_cmd bio=%p cmd=%d", bp, bp->bio_cmd));
63592108Sphk	dev = bp->bio_dev;
63692108Sphk	cp = dev->si_drv2;
637248679Smav	sc = cp->private;
638112978Sphk	KASSERT(cp->acr || cp->acw,
639112978Sphk	    ("Consumer with zero access count in g_dev_strategy"));
640196964Smav#ifdef INVARIANTS
641135865Spjd	if ((bp->bio_offset % cp->provider->sectorsize) != 0 ||
642135865Spjd	    (bp->bio_bcount % cp->provider->sectorsize) != 0) {
643159756Ssimon		bp->bio_resid = bp->bio_bcount;
644135865Spjd		biofinish(bp, NULL, EINVAL);
645135865Spjd		return;
646135865Spjd	}
647196964Smav#endif
648248679Smav	mtx_lock(&sc->sc_mtx);
649248679Smav	KASSERT(sc->sc_open > 0, ("Closed device in g_dev_strategy"));
650248679Smav	sc->sc_active++;
651248679Smav	mtx_unlock(&sc->sc_mtx);
652248679Smav
653118869Sphk	for (;;) {
654118869Sphk		/*
655118869Sphk		 * XXX: This is not an ideal solution, but I belive it to
656118869Sphk		 * XXX: deadlock safe, all things considered.
657118869Sphk		 */
658118869Sphk		bp2 = g_clone_bio(bp);
659118869Sphk		if (bp2 != NULL)
660118869Sphk			break;
661167086Sjhb		pause("gdstrat", hz / 10);
662118869Sphk	}
663107834Sphk	KASSERT(bp2 != NULL, ("XXX: ENOMEM in a bad place"));
66492108Sphk	bp2->bio_done = g_dev_done;
66592108Sphk	g_trace(G_T_BIO,
666105540Sphk	    "g_dev_strategy(%p/%p) offset %jd length %jd data %p cmd %d",
667105540Sphk	    bp, bp2, (intmax_t)bp->bio_offset, (intmax_t)bp2->bio_length,
668105540Sphk	    bp2->bio_data, bp2->bio_cmd);
66992108Sphk	g_io_request(bp2, cp);
670112978Sphk	KASSERT(cp->acr || cp->acw,
671112978Sphk	    ("g_dev_strategy raced with g_dev_close and lost"));
672112978Sphk
67392108Sphk}
67492108Sphk
67596987Sphk/*
676248679Smav * g_dev_callback()
677248679Smav *
678248679Smav * Called by devfs when asynchronous device destruction is completed.
679248679Smav * - Mark that we have no attached device any more.
680248679Smav * - If there are no outstanding requests, schedule geom destruction.
681248679Smav *   Otherwise destruction will be scheduled later by g_dev_done().
682248679Smav */
683248679Smav
684248679Smavstatic void
685248679Smavg_dev_callback(void *arg)
686248679Smav{
687248679Smav	struct g_consumer *cp;
688248679Smav	struct g_dev_softc *sc;
689248679Smav	int destroy;
690248679Smav
691248679Smav	cp = arg;
692248679Smav	sc = cp->private;
693248679Smav	g_trace(G_T_TOPOLOGY, "g_dev_callback(%p(%s))", cp, cp->geom->name);
694248679Smav
695248679Smav	mtx_lock(&sc->sc_mtx);
696248679Smav	sc->sc_dev = NULL;
697248679Smav	sc->sc_alias = NULL;
698248679Smav	destroy = (sc->sc_active == 0);
699248679Smav	mtx_unlock(&sc->sc_mtx);
700248679Smav	if (destroy)
701248679Smav		g_post_event(g_dev_destroy, cp, M_WAITOK, NULL);
702248679Smav}
703248679Smav
704248679Smav/*
70596987Sphk * g_dev_orphan()
70696987Sphk *
707112024Sphk * Called from below when the provider orphaned us.
708112024Sphk * - Clear any dump settings.
709248679Smav * - Request asynchronous device destruction to prevent any more requests
710248679Smav *   from coming in.  The provider is already marked with an error, so
711248679Smav *   anything which comes in in the interrim will be returned immediately.
71296987Sphk */
71392108Sphk
71492108Sphkstatic void
71593250Sphkg_dev_orphan(struct g_consumer *cp)
71692108Sphk{
717130585Sphk	struct cdev *dev;
718248679Smav	struct g_dev_softc *sc;
71992108Sphk
720112024Sphk	g_topology_assert();
721248679Smav	sc = cp->private;
722248679Smav	dev = sc->sc_dev;
723248679Smav	g_trace(G_T_TOPOLOGY, "g_dev_orphan(%p(%s))", cp, cp->geom->name);
724112024Sphk
725112024Sphk	/* Reset any dump-area set on this device */
72695038Sphk	if (dev->si_flags & SI_DUMPDEV)
727291215Ssmh		(void)set_dumper(NULL, NULL, curthread);
728112024Sphk
729130585Sphk	/* Destroy the struct cdev *so we get no more requests */
730248679Smav	destroy_dev_sched_cb(dev, g_dev_callback, cp);
73192108Sphk}
73292108Sphk
73396987SphkDECLARE_GEOM_CLASS(g_dev_class, g_dev);
734