xref: /freebsd/sys/geom/geom_dev.c (revision f7856fe81df2df3c4355e674d34a4c15a095a33c)
1dd84a43cSPoul-Henning Kamp /*-
23728855aSPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
33728855aSPedro F. Giffuni  *
4dd84a43cSPoul-Henning Kamp  * Copyright (c) 2002 Poul-Henning Kamp
5dd84a43cSPoul-Henning Kamp  * Copyright (c) 2002 Networks Associates Technology, Inc.
6dd84a43cSPoul-Henning Kamp  * All rights reserved.
7dd84a43cSPoul-Henning Kamp  *
8dd84a43cSPoul-Henning Kamp  * This software was developed for the FreeBSD Project by Poul-Henning Kamp
9dd84a43cSPoul-Henning Kamp  * and NAI Labs, the Security Research Division of Network Associates, Inc.
10dd84a43cSPoul-Henning Kamp  * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
11dd84a43cSPoul-Henning Kamp  * DARPA CHATS research program.
12dd84a43cSPoul-Henning Kamp  *
13dd84a43cSPoul-Henning Kamp  * Redistribution and use in source and binary forms, with or without
14dd84a43cSPoul-Henning Kamp  * modification, are permitted provided that the following conditions
15dd84a43cSPoul-Henning Kamp  * are met:
16dd84a43cSPoul-Henning Kamp  * 1. Redistributions of source code must retain the above copyright
17dd84a43cSPoul-Henning Kamp  *    notice, this list of conditions and the following disclaimer.
18dd84a43cSPoul-Henning Kamp  * 2. Redistributions in binary form must reproduce the above copyright
19dd84a43cSPoul-Henning Kamp  *    notice, this list of conditions and the following disclaimer in the
20dd84a43cSPoul-Henning Kamp  *    documentation and/or other materials provided with the distribution.
21dd84a43cSPoul-Henning Kamp  * 3. The names of the authors may not be used to endorse or promote
22dd84a43cSPoul-Henning Kamp  *    products derived from this software without specific prior written
23dd84a43cSPoul-Henning Kamp  *    permission.
24dd84a43cSPoul-Henning Kamp  *
25dd84a43cSPoul-Henning Kamp  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
26dd84a43cSPoul-Henning Kamp  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27dd84a43cSPoul-Henning Kamp  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28dd84a43cSPoul-Henning Kamp  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
29dd84a43cSPoul-Henning Kamp  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30dd84a43cSPoul-Henning Kamp  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31dd84a43cSPoul-Henning Kamp  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32dd84a43cSPoul-Henning Kamp  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33dd84a43cSPoul-Henning Kamp  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34dd84a43cSPoul-Henning Kamp  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35dd84a43cSPoul-Henning Kamp  * SUCH DAMAGE.
36dd84a43cSPoul-Henning Kamp  */
37dd84a43cSPoul-Henning Kamp 
38dd84a43cSPoul-Henning Kamp #include <sys/param.h>
39dd84a43cSPoul-Henning Kamp #include <sys/systm.h>
40dd84a43cSPoul-Henning Kamp #include <sys/malloc.h>
41dd84a43cSPoul-Henning Kamp #include <sys/kernel.h>
42dd84a43cSPoul-Henning Kamp #include <sys/conf.h>
43416494d7SJustin T. Gibbs #include <sys/ctype.h>
44dd84a43cSPoul-Henning Kamp #include <sys/bio.h>
45773e541eSWarner Losh #include <sys/devctl.h>
46dd84a43cSPoul-Henning Kamp #include <sys/lock.h>
47dd84a43cSPoul-Henning Kamp #include <sys/mutex.h>
48fc6c63b4SPoul-Henning Kamp #include <sys/proc.h>
49dd84a43cSPoul-Henning Kamp #include <sys/errno.h>
50dd84a43cSPoul-Henning Kamp #include <sys/time.h>
51dd84a43cSPoul-Henning Kamp #include <sys/disk.h>
52dd84a43cSPoul-Henning Kamp #include <sys/fcntl.h>
53104a9b7eSAlexander Kabaev #include <sys/limits.h>
54a50e92ccSRobert Wing #include <sys/selinfo.h>
556f926c0bSSteven Hartland #include <sys/sysctl.h>
56dd84a43cSPoul-Henning Kamp #include <geom/geom.h>
5753705e35SPoul-Henning Kamp #include <geom/geom_int.h>
58416494d7SJustin T. Gibbs #include <machine/stdarg.h>
59416494d7SJustin T. Gibbs 
603c330affSAlexander Motin struct g_dev_softc {
613c330affSAlexander Motin 	struct mtx	 sc_mtx;
623c330affSAlexander Motin 	struct cdev	*sc_dev;
633c330affSAlexander Motin 	struct cdev	*sc_alias;
643c330affSAlexander Motin 	int		 sc_open;
6502a99230SAlexander Motin 	u_int		 sc_active;
66a50e92ccSRobert Wing 	struct selinfo	 sc_selinfo;
6702a99230SAlexander Motin #define	SC_A_DESTROY	(1 << 31)
6802a99230SAlexander Motin #define	SC_A_OPEN	(1 << 30)
6902a99230SAlexander Motin #define	SC_A_ACTIVE	(SC_A_OPEN - 1)
703c330affSAlexander Motin };
71dd84a43cSPoul-Henning Kamp 
72dd84a43cSPoul-Henning Kamp static d_open_t		g_dev_open;
73dd84a43cSPoul-Henning Kamp static d_close_t	g_dev_close;
74dd84a43cSPoul-Henning Kamp static d_strategy_t	g_dev_strategy;
75dd84a43cSPoul-Henning Kamp static d_ioctl_t	g_dev_ioctl;
76a50e92ccSRobert Wing static d_kqfilter_t	g_dev_kqfilter;
77a50e92ccSRobert Wing 
78a50e92ccSRobert Wing static void		gdev_filter_detach(struct knote *kn);
79a50e92ccSRobert Wing static int		gdev_filter_vnode(struct knote *kn, long hint);
80a50e92ccSRobert Wing 
81ef9ffb85SMark Johnston static const struct filterops gdev_filterops_vnode = {
82a50e92ccSRobert Wing 	.f_isfd = 1,
83a50e92ccSRobert Wing 	.f_detach = gdev_filter_detach,
84a50e92ccSRobert Wing 	.f_event = gdev_filter_vnode,
85a50e92ccSRobert Wing };
86dd84a43cSPoul-Henning Kamp 
87dd84a43cSPoul-Henning Kamp static struct cdevsw g_dev_cdevsw = {
88dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
897ac40f5fSPoul-Henning Kamp 	.d_open =	g_dev_open,
907ac40f5fSPoul-Henning Kamp 	.d_close =	g_dev_close,
917ac40f5fSPoul-Henning Kamp 	.d_read =	physread,
927ac40f5fSPoul-Henning Kamp 	.d_write =	physwrite,
937ac40f5fSPoul-Henning Kamp 	.d_ioctl =	g_dev_ioctl,
947ac40f5fSPoul-Henning Kamp 	.d_strategy =	g_dev_strategy,
957ac40f5fSPoul-Henning Kamp 	.d_name =	"g_dev",
96ce625ec7SKenneth D. Merry 	.d_flags =	D_DISK | D_TRACKCLOSE,
97a50e92ccSRobert Wing 	.d_kqfilter =	g_dev_kqfilter,
98dd84a43cSPoul-Henning Kamp };
99dd84a43cSPoul-Henning Kamp 
1000478dc0cSAndrey V. Elsukov static g_init_t g_dev_init;
1010478dc0cSAndrey V. Elsukov static g_fini_t g_dev_fini;
102dd84a43cSPoul-Henning Kamp static g_taste_t g_dev_taste;
103dd84a43cSPoul-Henning Kamp static g_orphan_t g_dev_orphan;
104416494d7SJustin T. Gibbs static g_attrchanged_t g_dev_attrchanged;
105e6b0d5ebSPawel Jakub Dawidek static g_resize_t g_dev_resize;
106dd84a43cSPoul-Henning Kamp 
107e805e8f0SPoul-Henning Kamp static struct g_class g_dev_class	= {
108c138fec0SPoul-Henning Kamp 	.name = "DEV",
1095721c9c7SPoul-Henning Kamp 	.version = G_VERSION,
1100478dc0cSAndrey V. Elsukov 	.init = g_dev_init,
1110478dc0cSAndrey V. Elsukov 	.fini = g_dev_fini,
112c138fec0SPoul-Henning Kamp 	.taste = g_dev_taste,
113650ee351SPoul-Henning Kamp 	.orphan = g_dev_orphan,
114e6b0d5ebSPawel Jakub Dawidek 	.attrchanged = g_dev_attrchanged,
115e6b0d5ebSPawel Jakub Dawidek 	.resize = g_dev_resize
116dd84a43cSPoul-Henning Kamp };
117dd84a43cSPoul-Henning Kamp 
1186f926c0bSSteven Hartland /*
1196f926c0bSSteven Hartland  * We target 262144 (8 x 32768) sectors by default as this significantly
1206f926c0bSSteven Hartland  * increases the throughput on commonly used SSD's with a marginal
1216f926c0bSSteven Hartland  * increase in non-interruptible request latency.
1226f926c0bSSteven Hartland  */
1236f926c0bSSteven Hartland static uint64_t g_dev_del_max_sectors = 262144;
1246f926c0bSSteven Hartland SYSCTL_DECL(_kern_geom);
1257029da5cSPawel Biernacki SYSCTL_NODE(_kern_geom, OID_AUTO, dev, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1267029da5cSPawel Biernacki     "GEOM_DEV stuff");
1276f926c0bSSteven Hartland SYSCTL_QUAD(_kern_geom_dev, OID_AUTO, delete_max_sectors, CTLFLAG_RW,
1286f926c0bSSteven Hartland     &g_dev_del_max_sectors, 0, "Maximum number of sectors in a single "
1296f926c0bSSteven Hartland     "delete request sent to the provider. Larger requests are chunked "
1306f926c0bSSteven Hartland     "so they can be interrupted. (0 = disable chunking)");
1316f926c0bSSteven Hartland 
1320478dc0cSAndrey V. Elsukov static char *dumpdev = NULL;
1330478dc0cSAndrey V. Elsukov static void
g_dev_init(struct g_class * mp)1340478dc0cSAndrey V. Elsukov g_dev_init(struct g_class *mp)
1350478dc0cSAndrey V. Elsukov {
1360478dc0cSAndrey V. Elsukov 
1372be111bfSDavide Italiano 	dumpdev = kern_getenv("dumpdev");
1380478dc0cSAndrey V. Elsukov }
1390478dc0cSAndrey V. Elsukov 
1400478dc0cSAndrey V. Elsukov static void
g_dev_fini(struct g_class * mp)1410478dc0cSAndrey V. Elsukov g_dev_fini(struct g_class *mp)
1420478dc0cSAndrey V. Elsukov {
1430478dc0cSAndrey V. Elsukov 
1440478dc0cSAndrey V. Elsukov 	freeenv(dumpdev);
145b4d72907SConrad Meyer 	dumpdev = NULL;
1460478dc0cSAndrey V. Elsukov }
1470478dc0cSAndrey V. Elsukov 
1480478dc0cSAndrey V. Elsukov static int
g_dev_setdumpdev(struct cdev * dev,struct diocskerneldump_arg * kda)1496b6e2954SConrad Meyer g_dev_setdumpdev(struct cdev *dev, struct diocskerneldump_arg *kda)
1500478dc0cSAndrey V. Elsukov {
1510478dc0cSAndrey V. Elsukov 	struct g_kerneldump kd;
1520478dc0cSAndrey V. Elsukov 	struct g_consumer *cp;
1530478dc0cSAndrey V. Elsukov 	int error, len;
1540478dc0cSAndrey V. Elsukov 
1556b6e2954SConrad Meyer 	MPASS(dev != NULL && kda != NULL);
1566b6e2954SConrad Meyer 	MPASS(kda->kda_index != KDA_REMOVE);
1570478dc0cSAndrey V. Elsukov 
1580478dc0cSAndrey V. Elsukov 	cp = dev->si_drv2;
1590478dc0cSAndrey V. Elsukov 	len = sizeof(kd);
160bd92e6b6SMark Johnston 	memset(&kd, 0, len);
1610478dc0cSAndrey V. Elsukov 	kd.offset = 0;
1620478dc0cSAndrey V. Elsukov 	kd.length = OFF_MAX;
1630478dc0cSAndrey V. Elsukov 	error = g_io_getattr("GEOM::kerneldump", cp, &len, &kd);
164480f31c2SKonrad Witaszczyk 	if (error != 0)
165480f31c2SKonrad Witaszczyk 		return (error);
166480f31c2SKonrad Witaszczyk 
1676b6e2954SConrad Meyer 	error = dumper_insert(&kd.di, devtoname(dev), kda);
1680478dc0cSAndrey V. Elsukov 	if (error == 0)
1690478dc0cSAndrey V. Elsukov 		dev->si_flags |= SI_DUMPDEV;
170480f31c2SKonrad Witaszczyk 
1710478dc0cSAndrey V. Elsukov 	return (error);
1720478dc0cSAndrey V. Elsukov }
1730478dc0cSAndrey V. Elsukov 
17486787e8dSSteven Hartland static int
init_dumpdev(struct cdev * dev)1750478dc0cSAndrey V. Elsukov init_dumpdev(struct cdev *dev)
1760478dc0cSAndrey V. Elsukov {
177480f31c2SKonrad Witaszczyk 	struct diocskerneldump_arg kda;
17886787e8dSSteven Hartland 	struct g_consumer *cp;
1798510f61aSXin LI 	const char *devprefix = _PATH_DEV, *devname;
18086787e8dSSteven Hartland 	int error;
181b4d72907SConrad Meyer 	size_t len;
1820478dc0cSAndrey V. Elsukov 
183480f31c2SKonrad Witaszczyk 	bzero(&kda, sizeof(kda));
1846b6e2954SConrad Meyer 	kda.kda_index = KDA_APPEND;
185480f31c2SKonrad Witaszczyk 
1860478dc0cSAndrey V. Elsukov 	if (dumpdev == NULL)
18786787e8dSSteven Hartland 		return (0);
18886787e8dSSteven Hartland 
189b4d72907SConrad Meyer 	len = strlen(devprefix);
190b4d72907SConrad Meyer 	devname = devtoname(dev);
191b4d72907SConrad Meyer 	if (strcmp(devname, dumpdev) != 0 &&
192b4d72907SConrad Meyer 	   (strncmp(dumpdev, devprefix, len) != 0 ||
193b4d72907SConrad Meyer 	    strcmp(devname, dumpdev + len) != 0))
19486787e8dSSteven Hartland 		return (0);
19586787e8dSSteven Hartland 
19686787e8dSSteven Hartland 	cp = (struct g_consumer *)dev->si_drv2;
19786787e8dSSteven Hartland 	error = g_access(cp, 1, 0, 0);
19886787e8dSSteven Hartland 	if (error != 0)
19986787e8dSSteven Hartland 		return (error);
20086787e8dSSteven Hartland 
2016b6e2954SConrad Meyer 	error = g_dev_setdumpdev(dev, &kda);
20286787e8dSSteven Hartland 	if (error == 0) {
2030478dc0cSAndrey V. Elsukov 		freeenv(dumpdev);
2040478dc0cSAndrey V. Elsukov 		dumpdev = NULL;
2050478dc0cSAndrey V. Elsukov 	}
20686787e8dSSteven Hartland 
20786787e8dSSteven Hartland 	(void)g_access(cp, -1, 0, 0);
20886787e8dSSteven Hartland 
20986787e8dSSteven Hartland 	return (error);
2100478dc0cSAndrey V. Elsukov }
2110478dc0cSAndrey V. Elsukov 
2123c330affSAlexander Motin static void
g_dev_destroy(void * arg,int flags __unused)2133c330affSAlexander Motin g_dev_destroy(void *arg, int flags __unused)
2143c330affSAlexander Motin {
2153c330affSAlexander Motin 	struct g_consumer *cp;
2163c330affSAlexander Motin 	struct g_geom *gp;
2173c330affSAlexander Motin 	struct g_dev_softc *sc;
21801de1a06SEdward Tomasz Napierala 	char buf[SPECNAMELEN + 6];
2193c330affSAlexander Motin 
2203c330affSAlexander Motin 	g_topology_assert();
2213c330affSAlexander Motin 	cp = arg;
2223c330affSAlexander Motin 	gp = cp->geom;
2233c330affSAlexander Motin 	sc = cp->private;
2243c330affSAlexander Motin 	g_trace(G_T_TOPOLOGY, "g_dev_destroy(%p(%s))", cp, gp->name);
22501de1a06SEdward Tomasz Napierala 	snprintf(buf, sizeof(buf), "cdev=%s", gp->name);
226887611b1SWarner Losh 	devctl_notify("GEOM", "DEV", "DESTROY", buf);
227a50e92ccSRobert Wing 	knlist_clear(&sc->sc_selinfo.si_note, 0);
228a50e92ccSRobert Wing 	knlist_destroy(&sc->sc_selinfo.si_note);
2293c330affSAlexander Motin 	if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
2303c330affSAlexander Motin 		g_access(cp, -cp->acr, -cp->acw, -cp->ace);
2313c330affSAlexander Motin 	g_detach(cp);
2323c330affSAlexander Motin 	g_destroy_consumer(cp);
2333c330affSAlexander Motin 	g_destroy_geom(gp);
2343c330affSAlexander Motin 	mtx_destroy(&sc->sc_mtx);
2353c330affSAlexander Motin 	g_free(sc);
2363c330affSAlexander Motin }
2373c330affSAlexander Motin 
23884c080a8SPoul-Henning Kamp void
g_dev_print(void)239ce225127SPoul-Henning Kamp g_dev_print(void)
240ce225127SPoul-Henning Kamp {
241ce225127SPoul-Henning Kamp 	struct g_geom *gp;
24284c080a8SPoul-Henning Kamp 	char const *p = "";
243ce225127SPoul-Henning Kamp 
24484c080a8SPoul-Henning Kamp 	LIST_FOREACH(gp, &g_dev_class.geom, geom) {
24584c080a8SPoul-Henning Kamp 		printf("%s%s", p, gp->name);
24684c080a8SPoul-Henning Kamp 		p = " ";
24784c080a8SPoul-Henning Kamp 	}
248ce225127SPoul-Henning Kamp 	printf("\n");
249ce225127SPoul-Henning Kamp }
250ce225127SPoul-Henning Kamp 
251416494d7SJustin T. Gibbs static void
g_dev_set_physpath(struct g_consumer * cp)252151746b2SAlan Somers g_dev_set_physpath(struct g_consumer *cp)
253151746b2SAlan Somers {
254151746b2SAlan Somers 	struct g_dev_softc *sc;
255151746b2SAlan Somers 	char *physpath;
256151746b2SAlan Somers 	int error, physpath_len;
257151746b2SAlan Somers 
258151746b2SAlan Somers 	if (g_access(cp, 1, 0, 0) != 0)
259151746b2SAlan Somers 		return;
260151746b2SAlan Somers 
261151746b2SAlan Somers 	sc = cp->private;
262151746b2SAlan Somers 	physpath_len = MAXPATHLEN;
263151746b2SAlan Somers 	physpath = g_malloc(physpath_len, M_WAITOK|M_ZERO);
264151746b2SAlan Somers 	error = g_io_getattr("GEOM::physpath", cp, &physpath_len, physpath);
265151746b2SAlan Somers 	g_access(cp, -1, 0, 0);
266151746b2SAlan Somers 	if (error == 0 && strlen(physpath) != 0) {
267151746b2SAlan Somers 		struct cdev *dev, *old_alias_dev;
268151746b2SAlan Somers 		struct cdev **alias_devp;
269151746b2SAlan Somers 
270151746b2SAlan Somers 		dev = sc->sc_dev;
271151746b2SAlan Somers 		old_alias_dev = sc->sc_alias;
272151746b2SAlan Somers 		alias_devp = (struct cdev **)&sc->sc_alias;
2735f438dd3SAlan Somers 		make_dev_physpath_alias(MAKEDEV_WAITOK | MAKEDEV_CHECKNAME,
2745f438dd3SAlan Somers 		    alias_devp, dev, old_alias_dev, physpath);
275151746b2SAlan Somers 	} else if (sc->sc_alias) {
276151746b2SAlan Somers 		destroy_dev((struct cdev *)sc->sc_alias);
277151746b2SAlan Somers 		sc->sc_alias = NULL;
278151746b2SAlan Somers 	}
279151746b2SAlan Somers 	g_free(physpath);
280151746b2SAlan Somers }
281151746b2SAlan Somers 
282151746b2SAlan Somers static void
g_dev_set_media(struct g_consumer * cp)283151746b2SAlan Somers g_dev_set_media(struct g_consumer *cp)
284416494d7SJustin T. Gibbs {
2853c330affSAlexander Motin 	struct g_dev_softc *sc;
2863631c638SAlexander Motin 	struct cdev *dev;
2873631c638SAlexander Motin 	char buf[SPECNAMELEN + 6];
2883631c638SAlexander Motin 
2893c330affSAlexander Motin 	sc = cp->private;
2903c330affSAlexander Motin 	dev = sc->sc_dev;
2913631c638SAlexander Motin 	snprintf(buf, sizeof(buf), "cdev=%s", dev->si_name);
292887611b1SWarner Losh 	devctl_notify("DEVFS", "CDEV", "MEDIACHANGE", buf);
293887611b1SWarner Losh 	devctl_notify("GEOM", "DEV", "MEDIACHANGE", buf);
2943c330affSAlexander Motin 	dev = sc->sc_alias;
2953631c638SAlexander Motin 	if (dev != NULL) {
2963631c638SAlexander Motin 		snprintf(buf, sizeof(buf), "cdev=%s", dev->si_name);
297887611b1SWarner Losh 		devctl_notify("DEVFS", "CDEV", "MEDIACHANGE", buf);
298887611b1SWarner Losh 		devctl_notify("GEOM", "DEV", "MEDIACHANGE", buf);
2993631c638SAlexander Motin 	}
300151746b2SAlan Somers }
301151746b2SAlan Somers 
302151746b2SAlan Somers static void
g_dev_attrchanged(struct g_consumer * cp,const char * attr)303151746b2SAlan Somers g_dev_attrchanged(struct g_consumer *cp, const char *attr)
304151746b2SAlan Somers {
305151746b2SAlan Somers 
306151746b2SAlan Somers 	if (strcmp(attr, "GEOM::media") == 0) {
307151746b2SAlan Somers 		g_dev_set_media(cp);
3083631c638SAlexander Motin 		return;
3093631c638SAlexander Motin 	}
310416494d7SJustin T. Gibbs 
311151746b2SAlan Somers 	if (strcmp(attr, "GEOM::physpath") == 0) {
312151746b2SAlan Somers 		g_dev_set_physpath(cp);
313416494d7SJustin T. Gibbs 		return;
314416494d7SJustin T. Gibbs 	}
315416494d7SJustin T. Gibbs }
316416494d7SJustin T. Gibbs 
317e6b0d5ebSPawel Jakub Dawidek static void
g_dev_resize(struct g_consumer * cp)318e6b0d5ebSPawel Jakub Dawidek g_dev_resize(struct g_consumer *cp)
319e6b0d5ebSPawel Jakub Dawidek {
320a50e92ccSRobert Wing 	struct g_dev_softc *sc;
321e6b0d5ebSPawel Jakub Dawidek 	char buf[SPECNAMELEN + 6];
322e6b0d5ebSPawel Jakub Dawidek 
323a50e92ccSRobert Wing 	sc = cp->private;
324a50e92ccSRobert Wing 	KNOTE_UNLOCKED(&sc->sc_selinfo.si_note, NOTE_ATTRIB);
325a50e92ccSRobert Wing 
326e6b0d5ebSPawel Jakub Dawidek 	snprintf(buf, sizeof(buf), "cdev=%s", cp->provider->name);
327887611b1SWarner Losh 	devctl_notify("GEOM", "DEV", "SIZECHANGE", buf);
328e6b0d5ebSPawel Jakub Dawidek }
329e6b0d5ebSPawel Jakub Dawidek 
330bff1e299SPoul-Henning Kamp struct g_provider *
g_dev_getprovider(struct cdev * dev)33189c9c53dSPoul-Henning Kamp g_dev_getprovider(struct cdev *dev)
332bff1e299SPoul-Henning Kamp {
333bff1e299SPoul-Henning Kamp 	struct g_consumer *cp;
334bff1e299SPoul-Henning Kamp 
335a7830346SPoul-Henning Kamp 	g_topology_assert();
336bff1e299SPoul-Henning Kamp 	if (dev == NULL)
337bff1e299SPoul-Henning Kamp 		return (NULL);
338a7830346SPoul-Henning Kamp 	if (dev->si_devsw != &g_dev_cdevsw)
339b3b21113SPoul-Henning Kamp 		return (NULL);
340bff1e299SPoul-Henning Kamp 	cp = dev->si_drv2;
341bff1e299SPoul-Henning Kamp 	return (cp->provider);
342bff1e299SPoul-Henning Kamp }
343bff1e299SPoul-Henning Kamp 
344dd84a43cSPoul-Henning Kamp static struct g_geom *
g_dev_taste(struct g_class * mp,struct g_provider * pp,int insist __unused)345b1876192SPoul-Henning Kamp g_dev_taste(struct g_class *mp, struct g_provider *pp, int insist __unused)
346dd84a43cSPoul-Henning Kamp {
347dd84a43cSPoul-Henning Kamp 	struct g_geom *gp;
348c624eb25SWarner Losh 	struct g_geom_alias *gap;
349dd84a43cSPoul-Henning Kamp 	struct g_consumer *cp;
3503c330affSAlexander Motin 	struct g_dev_softc *sc;
3514a3760baSAlexander Motin 	int error;
352c624eb25SWarner Losh 	struct cdev *dev, *adev;
3534a3760baSAlexander Motin 	char buf[SPECNAMELEN + 6];
354e61ed798SAlexander Motin 	struct make_dev_args args;
355dd84a43cSPoul-Henning Kamp 
356dd84a43cSPoul-Henning Kamp 	g_trace(G_T_TOPOLOGY, "dev_taste(%s,%s)", mp->name, pp->name);
357dd84a43cSPoul-Henning Kamp 	g_topology_assert();
35802c62349SJaakko Heinonen 	gp = g_new_geomf(mp, "%s", pp->name);
3593c330affSAlexander Motin 	sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
3603c330affSAlexander Motin 	mtx_init(&sc->sc_mtx, "g_dev", NULL, MTX_DEF);
361dd84a43cSPoul-Henning Kamp 	cp = g_new_consumer(gp);
3623c330affSAlexander Motin 	cp->private = sc;
36340ea77a0SAlexander Motin 	cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
3644b8374a7SPoul-Henning Kamp 	error = g_attach(cp, pp);
3653001e97dSEdward Tomasz Napierala 	if (error != 0) {
3663001e97dSEdward Tomasz Napierala 		printf("%s: g_dev_taste(%s) failed to g_attach, error=%d\n",
3673001e97dSEdward Tomasz Napierala 		    __func__, pp->name, error);
3683001e97dSEdward Tomasz Napierala 		g_destroy_consumer(cp);
3693001e97dSEdward Tomasz Napierala 		g_destroy_geom(gp);
3703001e97dSEdward Tomasz Napierala 		mtx_destroy(&sc->sc_mtx);
3713001e97dSEdward Tomasz Napierala 		g_free(sc);
3723001e97dSEdward Tomasz Napierala 		return (NULL);
3733001e97dSEdward Tomasz Napierala 	}
374e61ed798SAlexander Motin 	make_dev_args_init(&args);
375e61ed798SAlexander Motin 	args.mda_flags = MAKEDEV_CHECKNAME | MAKEDEV_WAITOK;
376e61ed798SAlexander Motin 	args.mda_devsw = &g_dev_cdevsw;
377e61ed798SAlexander Motin 	args.mda_cr = NULL;
378e61ed798SAlexander Motin 	args.mda_uid = UID_ROOT;
379e61ed798SAlexander Motin 	args.mda_gid = GID_OPERATOR;
380e61ed798SAlexander Motin 	args.mda_mode = 0640;
381e61ed798SAlexander Motin 	args.mda_si_drv1 = sc;
382e61ed798SAlexander Motin 	args.mda_si_drv2 = cp;
383e61ed798SAlexander Motin 	error = make_dev_s(&args, &sc->sc_dev, "%s", gp->name);
384bc2589f5SJaakko Heinonen 	if (error != 0) {
385bc2589f5SJaakko Heinonen 		printf("%s: make_dev_p() failed (gp->name=%s, error=%d)\n",
386bc2589f5SJaakko Heinonen 		    __func__, gp->name, error);
387bc2589f5SJaakko Heinonen 		g_detach(cp);
388bc2589f5SJaakko Heinonen 		g_destroy_consumer(cp);
389bc2589f5SJaakko Heinonen 		g_destroy_geom(gp);
3903c330affSAlexander Motin 		mtx_destroy(&sc->sc_mtx);
3913c330affSAlexander Motin 		g_free(sc);
392bc2589f5SJaakko Heinonen 		return (NULL);
393bc2589f5SJaakko Heinonen 	}
394e61ed798SAlexander Motin 	dev = sc->sc_dev;
395ce625ec7SKenneth D. Merry 	dev->si_flags |= SI_UNMAPPED;
396cd853791SKonstantin Belousov 	dev->si_iosize_max = maxphys;
397a50e92ccSRobert Wing 	knlist_init_mtx(&sc->sc_selinfo.si_note, &sc->sc_mtx);
39886787e8dSSteven Hartland 	error = init_dumpdev(dev);
39986787e8dSSteven Hartland 	if (error != 0)
40086787e8dSSteven Hartland 		printf("%s: init_dumpdev() failed (gp->name=%s, error=%d)\n",
40186787e8dSSteven Hartland 		    __func__, gp->name, error);
402416494d7SJustin T. Gibbs 
403416494d7SJustin T. Gibbs 	g_dev_attrchanged(cp, "GEOM::physpath");
40401de1a06SEdward Tomasz Napierala 	snprintf(buf, sizeof(buf), "cdev=%s", gp->name);
405887611b1SWarner Losh 	devctl_notify("GEOM", "DEV", "CREATE", buf);
406c624eb25SWarner Losh 	/*
407c624eb25SWarner Losh 	 * Now add all the aliases for this drive
408c624eb25SWarner Losh 	 */
409ae1cce52SWarner Losh 	LIST_FOREACH(gap, &pp->aliases, ga_next) {
410c624eb25SWarner Losh 		error = make_dev_alias_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK, &adev, dev,
411c624eb25SWarner Losh 		    "%s", gap->ga_alias);
412c624eb25SWarner Losh 		if (error) {
413a9ca503bSConrad Meyer 			printf("%s: make_dev_alias_p() failed (name=%s, error=%d)\n",
414a9ca503bSConrad Meyer 			    __func__, gap->ga_alias, error);
415c624eb25SWarner Losh 			continue;
416c624eb25SWarner Losh 		}
417c624eb25SWarner Losh 		snprintf(buf, sizeof(buf), "cdev=%s", gap->ga_alias);
418887611b1SWarner Losh 		devctl_notify("GEOM", "DEV", "CREATE", buf);
419c624eb25SWarner Losh 	}
420416494d7SJustin T. Gibbs 
421dd84a43cSPoul-Henning Kamp 	return (gp);
422dd84a43cSPoul-Henning Kamp }
423dd84a43cSPoul-Henning Kamp 
424dd84a43cSPoul-Henning Kamp static int
g_dev_open(struct cdev * dev,int flags,int fmt,struct thread * td)42589c9c53dSPoul-Henning Kamp g_dev_open(struct cdev *dev, int flags, int fmt, struct thread *td)
426dd84a43cSPoul-Henning Kamp {
427dd84a43cSPoul-Henning Kamp 	struct g_consumer *cp;
4283c330affSAlexander Motin 	struct g_dev_softc *sc;
429dd84a43cSPoul-Henning Kamp 	int error, r, w, e;
430dd84a43cSPoul-Henning Kamp 
431dd84a43cSPoul-Henning Kamp 	cp = dev->si_drv2;
432dd84a43cSPoul-Henning Kamp 	g_trace(G_T_ACCESS, "g_dev_open(%s, %d, %d, %p)",
4333c330affSAlexander Motin 	    cp->geom->name, flags, fmt, td);
434fc6c63b4SPoul-Henning Kamp 
435dd84a43cSPoul-Henning Kamp 	r = flags & FREAD ? 1 : 0;
436dd84a43cSPoul-Henning Kamp 	w = flags & FWRITE ? 1 : 0;
43702945fefSPoul-Henning Kamp #ifdef notyet
438dd84a43cSPoul-Henning Kamp 	e = flags & O_EXCL ? 1 : 0;
43902945fefSPoul-Henning Kamp #else
44002945fefSPoul-Henning Kamp 	e = 0;
44102945fefSPoul-Henning Kamp #endif
44272800098SEdward Tomasz Napierala 
44372800098SEdward Tomasz Napierala 	/*
44472800098SEdward Tomasz Napierala 	 * This happens on attempt to open a device node with O_EXEC.
44572800098SEdward Tomasz Napierala 	 */
44672800098SEdward Tomasz Napierala 	if (r + w + e == 0)
44772800098SEdward Tomasz Napierala 		return (EINVAL);
44872800098SEdward Tomasz Napierala 
449fc6c63b4SPoul-Henning Kamp 	if (w) {
450fc6c63b4SPoul-Henning Kamp 		/*
451fc6c63b4SPoul-Henning Kamp 		 * When running in very secure mode, do not allow
452fc6c63b4SPoul-Henning Kamp 		 * opens for writing of any disks.
453fc6c63b4SPoul-Henning Kamp 		 */
454fc6c63b4SPoul-Henning Kamp 		error = securelevel_ge(td->td_ucred, 2);
455fc6c63b4SPoul-Henning Kamp 		if (error)
456fc6c63b4SPoul-Henning Kamp 			return (error);
457fc6c63b4SPoul-Henning Kamp 	}
458c7e1925cSPoul-Henning Kamp 	g_topology_lock();
459d2bae332SPoul-Henning Kamp 	error = g_access(cp, r, w, e);
460dd84a43cSPoul-Henning Kamp 	g_topology_unlock();
4613c330affSAlexander Motin 	if (error == 0) {
462e61ed798SAlexander Motin 		sc = dev->si_drv1;
4633c330affSAlexander Motin 		mtx_lock(&sc->sc_mtx);
46402a99230SAlexander Motin 		if (sc->sc_open == 0 && (sc->sc_active & SC_A_ACTIVE) != 0)
4653c330affSAlexander Motin 			wakeup(&sc->sc_active);
4663c330affSAlexander Motin 		sc->sc_open += r + w + e;
46702a99230SAlexander Motin 		if (sc->sc_open == 0)
46802a99230SAlexander Motin 			atomic_clear_int(&sc->sc_active, SC_A_OPEN);
46902a99230SAlexander Motin 		else
47002a99230SAlexander Motin 			atomic_set_int(&sc->sc_active, SC_A_OPEN);
47165c87a6cSRobert Wing 		KNOTE_LOCKED(&sc->sc_selinfo.si_note, NOTE_OPEN);
4723c330affSAlexander Motin 		mtx_unlock(&sc->sc_mtx);
4733c330affSAlexander Motin 	}
474dd84a43cSPoul-Henning Kamp 	return (error);
475dd84a43cSPoul-Henning Kamp }
476dd84a43cSPoul-Henning Kamp 
477dd84a43cSPoul-Henning Kamp static int
g_dev_close(struct cdev * dev,int flags,int fmt,struct thread * td)47889c9c53dSPoul-Henning Kamp g_dev_close(struct cdev *dev, int flags, int fmt, struct thread *td)
479dd84a43cSPoul-Henning Kamp {
480dd84a43cSPoul-Henning Kamp 	struct g_consumer *cp;
4813c330affSAlexander Motin 	struct g_dev_softc *sc;
4823c330affSAlexander Motin 	int error, r, w, e;
483dd84a43cSPoul-Henning Kamp 
484dd84a43cSPoul-Henning Kamp 	cp = dev->si_drv2;
485dd84a43cSPoul-Henning Kamp 	g_trace(G_T_ACCESS, "g_dev_close(%s, %d, %d, %p)",
4863c330affSAlexander Motin 	    cp->geom->name, flags, fmt, td);
4873c330affSAlexander Motin 
488dd84a43cSPoul-Henning Kamp 	r = flags & FREAD ? -1 : 0;
489dd84a43cSPoul-Henning Kamp 	w = flags & FWRITE ? -1 : 0;
49002945fefSPoul-Henning Kamp #ifdef notyet
491dd84a43cSPoul-Henning Kamp 	e = flags & O_EXCL ? -1 : 0;
49202945fefSPoul-Henning Kamp #else
49302945fefSPoul-Henning Kamp 	e = 0;
49402945fefSPoul-Henning Kamp #endif
495d6cc35b2SEdward Tomasz Napierala 
496d6cc35b2SEdward Tomasz Napierala 	/*
497d6cc35b2SEdward Tomasz Napierala 	 * The vgonel(9) - caused by eg. forced unmount of devfs - calls
498d6cc35b2SEdward Tomasz Napierala 	 * VOP_CLOSE(9) on devfs vnode without any FREAD or FWRITE flags,
499d6cc35b2SEdward Tomasz Napierala 	 * which would result in zero deltas, which in turn would cause
500d6cc35b2SEdward Tomasz Napierala 	 * panic in g_access(9).
501d6cc35b2SEdward Tomasz Napierala 	 *
502d6cc35b2SEdward Tomasz Napierala 	 * Note that we cannot zero the counters (ie. do "r = cp->acr"
503d6cc35b2SEdward Tomasz Napierala 	 * etc) instead, because the consumer might be opened in another
504d6cc35b2SEdward Tomasz Napierala 	 * devfs instance.
505d6cc35b2SEdward Tomasz Napierala 	 */
506d6cc35b2SEdward Tomasz Napierala 	if (r + w + e == 0)
507d6cc35b2SEdward Tomasz Napierala 		return (EINVAL);
508d6cc35b2SEdward Tomasz Napierala 
509e61ed798SAlexander Motin 	sc = dev->si_drv1;
5103c330affSAlexander Motin 	mtx_lock(&sc->sc_mtx);
5113c330affSAlexander Motin 	sc->sc_open += r + w + e;
51202a99230SAlexander Motin 	if (sc->sc_open == 0)
51302a99230SAlexander Motin 		atomic_clear_int(&sc->sc_active, SC_A_OPEN);
51402a99230SAlexander Motin 	else
51502a99230SAlexander Motin 		atomic_set_int(&sc->sc_active, SC_A_OPEN);
51602a99230SAlexander Motin 	while (sc->sc_open == 0 && (sc->sc_active & SC_A_ACTIVE) != 0)
51702a99230SAlexander Motin 		msleep(&sc->sc_active, &sc->sc_mtx, 0, "g_dev_close", hz / 10);
51865c87a6cSRobert Wing 	KNOTE_LOCKED(&sc->sc_selinfo.si_note, NOTE_CLOSE | (w ? NOTE_CLOSE_WRITE : 0));
5193c330affSAlexander Motin 	mtx_unlock(&sc->sc_mtx);
520c7e1925cSPoul-Henning Kamp 	g_topology_lock();
521d2bae332SPoul-Henning Kamp 	error = g_access(cp, r, w, e);
522dd84a43cSPoul-Henning Kamp 	g_topology_unlock();
523dd84a43cSPoul-Henning Kamp 	return (error);
524dd84a43cSPoul-Henning Kamp }
525dd84a43cSPoul-Henning Kamp 
526dd84a43cSPoul-Henning Kamp static int
g_dev_ioctl(struct cdev * dev,u_long cmd,caddr_t data,int fflag,struct thread * td)52789c9c53dSPoul-Henning Kamp g_dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
528dd84a43cSPoul-Henning Kamp {
529dd84a43cSPoul-Henning Kamp 	struct g_consumer *cp;
530f805f204SUlf Lilleengen 	struct g_provider *pp;
5314d5832bcSAlexander Motin 	off_t offset, length, chunk, odd;
532dd84a43cSPoul-Henning Kamp 	int i, error;
533dd84a43cSPoul-Henning Kamp 
534dd84a43cSPoul-Henning Kamp 	cp = dev->si_drv2;
535f805f204SUlf Lilleengen 	pp = cp->provider;
536dd84a43cSPoul-Henning Kamp 
53719cfcf25SAlexander Motin 	/* If consumer or provider is dying, don't disturb. */
53819cfcf25SAlexander Motin 	if (cp->flags & G_CF_ORPHAN)
53919cfcf25SAlexander Motin 		return (ENXIO);
54019cfcf25SAlexander Motin 	if (pp->error)
54119cfcf25SAlexander Motin 		return (pp->error);
54219cfcf25SAlexander Motin 
543dd84a43cSPoul-Henning Kamp 	error = 0;
544c7e1925cSPoul-Henning Kamp 	KASSERT(cp->acr || cp->acw,
545c7e1925cSPoul-Henning Kamp 	    ("Consumer with zero access count in g_dev_ioctl"));
546a5b2e75dSPoul-Henning Kamp 
54700dcdc8dSPoul-Henning Kamp 	i = IOCPARM_LEN(cmd);
54800dcdc8dSPoul-Henning Kamp 	switch (cmd) {
54900dcdc8dSPoul-Henning Kamp 	case DIOCGSECTORSIZE:
5506b3c68bfSAlexander Motin 		*(u_int *)data = pp->sectorsize;
5513f12caa1SPoul-Henning Kamp 		if (*(u_int *)data == 0)
55202fcfac0SNate Lawson 			error = ENOENT;
55300dcdc8dSPoul-Henning Kamp 		break;
55400dcdc8dSPoul-Henning Kamp 	case DIOCGMEDIASIZE:
5556b3c68bfSAlexander Motin 		*(off_t *)data = pp->mediasize;
5563f12caa1SPoul-Henning Kamp 		if (*(off_t *)data == 0)
55702fcfac0SNate Lawson 			error = ENOENT;
55800dcdc8dSPoul-Henning Kamp 		break;
55900dcdc8dSPoul-Henning Kamp 	case DIOCGFWSECTORS:
560b1876192SPoul-Henning Kamp 		error = g_io_getattr("GEOM::fwsectors", cp, &i, data);
56102fcfac0SNate Lawson 		if (error == 0 && *(u_int *)data == 0)
56202fcfac0SNate Lawson 			error = ENOENT;
56300dcdc8dSPoul-Henning Kamp 		break;
56400dcdc8dSPoul-Henning Kamp 	case DIOCGFWHEADS:
565b1876192SPoul-Henning Kamp 		error = g_io_getattr("GEOM::fwheads", cp, &i, data);
56602fcfac0SNate Lawson 		if (error == 0 && *(u_int *)data == 0)
56702fcfac0SNate Lawson 			error = ENOENT;
56800dcdc8dSPoul-Henning Kamp 		break;
569480f31c2SKonrad Witaszczyk 	case DIOCSKERNELDUMP:
570480f31c2SKonrad Witaszczyk 	    {
571480f31c2SKonrad Witaszczyk 		struct diocskerneldump_arg *kda;
572480f31c2SKonrad Witaszczyk 		uint8_t *encryptedkey;
573480f31c2SKonrad Witaszczyk 
574480f31c2SKonrad Witaszczyk 		kda = (struct diocskerneldump_arg *)data;
5756b6e2954SConrad Meyer 		if (kda->kda_index == KDA_REMOVE_ALL ||
5766b6e2954SConrad Meyer 		    kda->kda_index == KDA_REMOVE_DEV ||
5776b6e2954SConrad Meyer 		    kda->kda_index == KDA_REMOVE) {
5786b6e2954SConrad Meyer 			error = dumper_remove(devtoname(dev), kda);
5796b6e2954SConrad Meyer 			explicit_bzero(kda, sizeof(*kda));
580480f31c2SKonrad Witaszczyk 			break;
581480f31c2SKonrad Witaszczyk 		}
582480f31c2SKonrad Witaszczyk 
583480f31c2SKonrad Witaszczyk 		if (kda->kda_encryption != KERNELDUMP_ENC_NONE) {
5846b6e2954SConrad Meyer 			if (kda->kda_encryptedkeysize == 0 ||
585480f31c2SKonrad Witaszczyk 			    kda->kda_encryptedkeysize >
586480f31c2SKonrad Witaszczyk 			    KERNELDUMP_ENCKEY_MAX_SIZE) {
5876b6e2954SConrad Meyer 				explicit_bzero(kda, sizeof(*kda));
588480f31c2SKonrad Witaszczyk 				return (EINVAL);
589480f31c2SKonrad Witaszczyk 			}
590480f31c2SKonrad Witaszczyk 			encryptedkey = malloc(kda->kda_encryptedkeysize, M_TEMP,
591480f31c2SKonrad Witaszczyk 			    M_WAITOK);
592480f31c2SKonrad Witaszczyk 			error = copyin(kda->kda_encryptedkey, encryptedkey,
593480f31c2SKonrad Witaszczyk 			    kda->kda_encryptedkeysize);
594480f31c2SKonrad Witaszczyk 		} else {
595480f31c2SKonrad Witaszczyk 			encryptedkey = NULL;
596480f31c2SKonrad Witaszczyk 		}
597480f31c2SKonrad Witaszczyk 		if (error == 0) {
598480f31c2SKonrad Witaszczyk 			kda->kda_encryptedkey = encryptedkey;
5996b6e2954SConrad Meyer 			error = g_dev_setdumpdev(dev, kda);
600480f31c2SKonrad Witaszczyk 		}
6014a711b8dSJohn Baldwin 		zfree(encryptedkey, M_TEMP);
602480f31c2SKonrad Witaszczyk 		explicit_bzero(kda, sizeof(*kda));
603480f31c2SKonrad Witaszczyk 		break;
604480f31c2SKonrad Witaszczyk 	    }
6050589353aSPawel Jakub Dawidek 	case DIOCGFLUSH:
6060589353aSPawel Jakub Dawidek 		error = g_io_flush(cp);
6070589353aSPawel Jakub Dawidek 		break;
6080589353aSPawel Jakub Dawidek 	case DIOCGDELETE:
6090589353aSPawel Jakub Dawidek 		offset = ((off_t *)data)[0];
6100589353aSPawel Jakub Dawidek 		length = ((off_t *)data)[1];
6116b3c68bfSAlexander Motin 		if ((offset % pp->sectorsize) != 0 ||
6126b3c68bfSAlexander Motin 		    (length % pp->sectorsize) != 0 || length <= 0) {
6130589353aSPawel Jakub Dawidek 			printf("%s: offset=%jd length=%jd\n", __func__, offset,
6140589353aSPawel Jakub Dawidek 			    length);
6150589353aSPawel Jakub Dawidek 			error = EINVAL;
6160589353aSPawel Jakub Dawidek 			break;
6170589353aSPawel Jakub Dawidek 		}
618015a11e6SPoul-Henning Kamp 		while (length > 0) {
619015a11e6SPoul-Henning Kamp 			chunk = length;
6206b3c68bfSAlexander Motin 			if (g_dev_del_max_sectors != 0 &&
6216b3c68bfSAlexander Motin 			    chunk > g_dev_del_max_sectors * pp->sectorsize) {
6226b3c68bfSAlexander Motin 				chunk = g_dev_del_max_sectors * pp->sectorsize;
6236b3c68bfSAlexander Motin 				if (pp->stripesize > 0) {
6244d5832bcSAlexander Motin 					odd = (offset + chunk +
6256b3c68bfSAlexander Motin 					    pp->stripeoffset) % pp->stripesize;
6264d5832bcSAlexander Motin 					if (chunk > odd)
6274d5832bcSAlexander Motin 						chunk -= odd;
6284d5832bcSAlexander Motin 				}
6296f926c0bSSteven Hartland 			}
630015a11e6SPoul-Henning Kamp 			error = g_delete_data(cp, offset, chunk);
631015a11e6SPoul-Henning Kamp 			length -= chunk;
632015a11e6SPoul-Henning Kamp 			offset += chunk;
633015a11e6SPoul-Henning Kamp 			if (error)
634015a11e6SPoul-Henning Kamp 				break;
635015a11e6SPoul-Henning Kamp 			/*
6366f926c0bSSteven Hartland 			 * Since the request size can be large, the service
6376f926c0bSSteven Hartland 			 * time can be is likewise.  We make this ioctl
6386f926c0bSSteven Hartland 			 * interruptible by checking for signals for each bio.
639015a11e6SPoul-Henning Kamp 			 */
640015a11e6SPoul-Henning Kamp 			if (SIGPENDING(td))
641015a11e6SPoul-Henning Kamp 				break;
642015a11e6SPoul-Henning Kamp 		}
6430589353aSPawel Jakub Dawidek 		break;
6440589353aSPawel Jakub Dawidek 	case DIOCGIDENT:
6450589353aSPawel Jakub Dawidek 		error = g_io_getattr("GEOM::ident", cp, &i, data);
6460589353aSPawel Jakub Dawidek 		break;
647f805f204SUlf Lilleengen 	case DIOCGPROVIDERNAME:
648f805f204SUlf Lilleengen 		strlcpy(data, pp->name, i);
649f805f204SUlf Lilleengen 		break;
6508b303238SAlexander Motin 	case DIOCGSTRIPESIZE:
6516b3c68bfSAlexander Motin 		*(off_t *)data = pp->stripesize;
6528b303238SAlexander Motin 		break;
6538b303238SAlexander Motin 	case DIOCGSTRIPEOFFSET:
6546b3c68bfSAlexander Motin 		*(off_t *)data = pp->stripeoffset;
6558b303238SAlexander Motin 		break;
656416494d7SJustin T. Gibbs 	case DIOCGPHYSPATH:
657416494d7SJustin T. Gibbs 		error = g_io_getattr("GEOM::physpath", cp, &i, data);
658416494d7SJustin T. Gibbs 		if (error == 0 && *(char *)data == '\0')
659416494d7SJustin T. Gibbs 			error = ENOENT;
660416494d7SJustin T. Gibbs 		break;
661c3e7ba3eSAlexander Motin 	case DIOCGATTR: {
662c3e7ba3eSAlexander Motin 		struct diocgattr_arg *arg = (struct diocgattr_arg *)data;
663c3e7ba3eSAlexander Motin 
664c3e7ba3eSAlexander Motin 		if (arg->len > sizeof(arg->value)) {
665c3e7ba3eSAlexander Motin 			error = EINVAL;
666c3e7ba3eSAlexander Motin 			break;
667c3e7ba3eSAlexander Motin 		}
668c3e7ba3eSAlexander Motin 		error = g_io_getattr(arg->name, cp, &arg->len, &arg->value);
669c3e7ba3eSAlexander Motin 		break;
670c3e7ba3eSAlexander Motin 	}
6719a6844d5SKenneth D. Merry 	case DIOCZONECMD: {
6729a6844d5SKenneth D. Merry 		struct disk_zone_args *zone_args =(struct disk_zone_args *)data;
6739a6844d5SKenneth D. Merry 		struct disk_zone_rep_entry *new_entries, *old_entries;
6749a6844d5SKenneth D. Merry 		struct disk_zone_report *rep;
6759a6844d5SKenneth D. Merry 		size_t alloc_size;
6769a6844d5SKenneth D. Merry 
6779a6844d5SKenneth D. Merry 		old_entries = NULL;
6789a6844d5SKenneth D. Merry 		new_entries = NULL;
6799a6844d5SKenneth D. Merry 		rep = NULL;
6809a6844d5SKenneth D. Merry 		alloc_size = 0;
6819a6844d5SKenneth D. Merry 
6829a6844d5SKenneth D. Merry 		if (zone_args->zone_cmd == DISK_ZONE_REPORT_ZONES) {
6839a6844d5SKenneth D. Merry 			rep = &zone_args->zone_params.report;
684cd853791SKonstantin Belousov #define	MAXENTRIES	(maxphys / sizeof(struct disk_zone_rep_entry))
685d4fbe32cSMark Johnston 			if (rep->entries_allocated > MAXENTRIES)
686d4fbe32cSMark Johnston 				rep->entries_allocated = MAXENTRIES;
6879a6844d5SKenneth D. Merry 			alloc_size = rep->entries_allocated *
6889a6844d5SKenneth D. Merry 			    sizeof(struct disk_zone_rep_entry);
6899a6844d5SKenneth D. Merry 			if (alloc_size != 0)
6909a6844d5SKenneth D. Merry 				new_entries = g_malloc(alloc_size,
6919a6844d5SKenneth D. Merry 				    M_WAITOK | M_ZERO);
6929a6844d5SKenneth D. Merry 			old_entries = rep->entries;
6939a6844d5SKenneth D. Merry 			rep->entries = new_entries;
6949a6844d5SKenneth D. Merry 		}
6959a6844d5SKenneth D. Merry 		error = g_io_zonecmd(zone_args, cp);
696d4fbe32cSMark Johnston 		if (zone_args->zone_cmd == DISK_ZONE_REPORT_ZONES &&
697d4fbe32cSMark Johnston 		    alloc_size != 0 && error == 0)
6989a6844d5SKenneth D. Merry 			error = copyout(new_entries, old_entries, alloc_size);
699d4fbe32cSMark Johnston 		if (old_entries != NULL && rep != NULL)
7009a6844d5SKenneth D. Merry 			rep->entries = old_entries;
7019a6844d5SKenneth D. Merry 		g_free(new_entries);
7029a6844d5SKenneth D. Merry 		break;
7039a6844d5SKenneth D. Merry 	}
70400dcdc8dSPoul-Henning Kamp 	default:
7056b3c68bfSAlexander Motin 		if (pp->geom->ioctl != NULL) {
7066b3c68bfSAlexander Motin 			error = pp->geom->ioctl(pp, cmd, data, fflag, td);
707f03bec94SPoul-Henning Kamp 		} else {
708f03bec94SPoul-Henning Kamp 			error = ENOIOCTL;
709497c3347SPoul-Henning Kamp 		}
71000dcdc8dSPoul-Henning Kamp 	}
711a5b2e75dSPoul-Henning Kamp 
712dd84a43cSPoul-Henning Kamp 	return (error);
713dd84a43cSPoul-Henning Kamp }
714dd84a43cSPoul-Henning Kamp 
715dd84a43cSPoul-Henning Kamp static void
g_dev_done(struct bio * bp2)716dd84a43cSPoul-Henning Kamp g_dev_done(struct bio *bp2)
717dd84a43cSPoul-Henning Kamp {
7183c330affSAlexander Motin 	struct g_consumer *cp;
7193c330affSAlexander Motin 	struct g_dev_softc *sc;
720dd84a43cSPoul-Henning Kamp 	struct bio *bp;
72102a99230SAlexander Motin 	int active;
722dd84a43cSPoul-Henning Kamp 
7233c330affSAlexander Motin 	cp = bp2->bio_from;
7243c330affSAlexander Motin 	sc = cp->private;
725936cc461SPoul-Henning Kamp 	bp = bp2->bio_parent;
726dd84a43cSPoul-Henning Kamp 	bp->bio_error = bp2->bio_error;
72721d0712cSAlexander Motin 	bp->bio_completed = bp2->bio_completed;
7282634da8cSAlexander Motin 	bp->bio_resid = bp->bio_length - bp2->bio_completed;
7299a6844d5SKenneth D. Merry 	if (bp2->bio_cmd == BIO_ZONE)
7309a6844d5SKenneth D. Merry 		bcopy(&bp2->bio_zone, &bp->bio_zone, sizeof(bp->bio_zone));
7319a6844d5SKenneth D. Merry 
73221d0712cSAlexander Motin 	if (bp2->bio_error != 0) {
733dd84a43cSPoul-Henning Kamp 		g_trace(G_T_BIO, "g_dev_done(%p) had error %d",
73421d0712cSAlexander Motin 		    bp2, bp2->bio_error);
735dd84a43cSPoul-Henning Kamp 		bp->bio_flags |= BIO_ERROR;
736dd84a43cSPoul-Henning Kamp 	} else {
73765c87a6cSRobert Wing 		if (bp->bio_cmd == BIO_READ)
73865c87a6cSRobert Wing 			KNOTE_UNLOCKED(&sc->sc_selinfo.si_note, NOTE_READ);
73965c87a6cSRobert Wing 		if (bp->bio_cmd == BIO_WRITE)
74065c87a6cSRobert Wing 			KNOTE_UNLOCKED(&sc->sc_selinfo.si_note, NOTE_WRITE);
74114ac6812SPoul-Henning Kamp 		g_trace(G_T_BIO, "g_dev_done(%p/%p) resid %ld completed %jd",
74221d0712cSAlexander Motin 		    bp2, bp, bp2->bio_resid, (intmax_t)bp2->bio_completed);
743dd84a43cSPoul-Henning Kamp 	}
744dd84a43cSPoul-Henning Kamp 	g_destroy_bio(bp2);
74502a99230SAlexander Motin 	active = atomic_fetchadd_int(&sc->sc_active, -1) - 1;
74602a99230SAlexander Motin 	if ((active & SC_A_ACTIVE) == 0) {
74702a99230SAlexander Motin 		if ((active & SC_A_OPEN) == 0)
7483c330affSAlexander Motin 			wakeup(&sc->sc_active);
74902a99230SAlexander Motin 		if (active & SC_A_DESTROY)
7500ada3afcSAlexander Motin 			g_post_event(g_dev_destroy, cp, M_NOWAIT, NULL);
75102a99230SAlexander Motin 	}
752dd84a43cSPoul-Henning Kamp 	biodone(bp);
753dd84a43cSPoul-Henning Kamp }
754dd84a43cSPoul-Henning Kamp 
755dd84a43cSPoul-Henning Kamp static void
g_dev_strategy(struct bio * bp)756dd84a43cSPoul-Henning Kamp g_dev_strategy(struct bio *bp)
757dd84a43cSPoul-Henning Kamp {
758dd84a43cSPoul-Henning Kamp 	struct g_consumer *cp;
759dd84a43cSPoul-Henning Kamp 	struct bio *bp2;
76089c9c53dSPoul-Henning Kamp 	struct cdev *dev;
7613c330affSAlexander Motin 	struct g_dev_softc *sc;
762dd84a43cSPoul-Henning Kamp 
763b630d83fSPoul-Henning Kamp 	KASSERT(bp->bio_cmd == BIO_READ ||
764b630d83fSPoul-Henning Kamp 	        bp->bio_cmd == BIO_WRITE ||
76516fac6c9SEdward Tomasz Napierala 	        bp->bio_cmd == BIO_DELETE ||
7669a6844d5SKenneth D. Merry 		bp->bio_cmd == BIO_FLUSH ||
7679a6844d5SKenneth D. Merry 		bp->bio_cmd == BIO_ZONE,
768b630d83fSPoul-Henning Kamp 		("Wrong bio_cmd bio=%p cmd=%d", bp, bp->bio_cmd));
769dd84a43cSPoul-Henning Kamp 	dev = bp->bio_dev;
770dd84a43cSPoul-Henning Kamp 	cp = dev->si_drv2;
771c7e1925cSPoul-Henning Kamp 	KASSERT(cp->acr || cp->acw,
772c7e1925cSPoul-Henning Kamp 	    ("Consumer with zero access count in g_dev_strategy"));
7738532d381SConrad Meyer 	biotrack(bp, __func__);
77418e42503SAlexander Motin #ifdef INVARIANTS
7756c252337SPawel Jakub Dawidek 	if ((bp->bio_offset % cp->provider->sectorsize) != 0 ||
7766c252337SPawel Jakub Dawidek 	    (bp->bio_bcount % cp->provider->sectorsize) != 0) {
777274ede62SSimon L. B. Nielsen 		bp->bio_resid = bp->bio_bcount;
7786c252337SPawel Jakub Dawidek 		biofinish(bp, NULL, EINVAL);
7796c252337SPawel Jakub Dawidek 		return;
7806c252337SPawel Jakub Dawidek 	}
78118e42503SAlexander Motin #endif
782e61ed798SAlexander Motin 	sc = dev->si_drv1;
7833c330affSAlexander Motin 	KASSERT(sc->sc_open > 0, ("Closed device in g_dev_strategy"));
78402a99230SAlexander Motin 	atomic_add_int(&sc->sc_active, 1);
7853c330affSAlexander Motin 
7864ba5a129SPoul-Henning Kamp 	for (;;) {
7874ba5a129SPoul-Henning Kamp 		/*
788e8d57122SPedro F. Giffuni 		 * XXX: This is not an ideal solution, but I believe it to
789e8d57122SPedro F. Giffuni 		 * XXX: deadlock safely, all things considered.
7904ba5a129SPoul-Henning Kamp 		 */
791dd84a43cSPoul-Henning Kamp 		bp2 = g_clone_bio(bp);
7924ba5a129SPoul-Henning Kamp 		if (bp2 != NULL)
7934ba5a129SPoul-Henning Kamp 			break;
7944d70511aSJohn Baldwin 		pause("gdstrat", hz / 10);
7954ba5a129SPoul-Henning Kamp 	}
796cc0163a3SPoul-Henning Kamp 	KASSERT(bp2 != NULL, ("XXX: ENOMEM in a bad place"));
797dd84a43cSPoul-Henning Kamp 	bp2->bio_done = g_dev_done;
798dd84a43cSPoul-Henning Kamp 	g_trace(G_T_BIO,
79914ac6812SPoul-Henning Kamp 	    "g_dev_strategy(%p/%p) offset %jd length %jd data %p cmd %d",
80014ac6812SPoul-Henning Kamp 	    bp, bp2, (intmax_t)bp->bio_offset, (intmax_t)bp2->bio_length,
80114ac6812SPoul-Henning Kamp 	    bp2->bio_data, bp2->bio_cmd);
802dd84a43cSPoul-Henning Kamp 	g_io_request(bp2, cp);
803c7e1925cSPoul-Henning Kamp 	KASSERT(cp->acr || cp->acw,
804c7e1925cSPoul-Henning Kamp 	    ("g_dev_strategy raced with g_dev_close and lost"));
805c7e1925cSPoul-Henning Kamp 
806dd84a43cSPoul-Henning Kamp }
807dd84a43cSPoul-Henning Kamp 
8084b8374a7SPoul-Henning Kamp /*
8093c330affSAlexander Motin  * g_dev_callback()
8103c330affSAlexander Motin  *
8113c330affSAlexander Motin  * Called by devfs when asynchronous device destruction is completed.
8123c330affSAlexander Motin  * - Mark that we have no attached device any more.
8133c330affSAlexander Motin  * - If there are no outstanding requests, schedule geom destruction.
8143c330affSAlexander Motin  *   Otherwise destruction will be scheduled later by g_dev_done().
8153c330affSAlexander Motin  */
8163c330affSAlexander Motin 
8173c330affSAlexander Motin static void
g_dev_callback(void * arg)8183c330affSAlexander Motin g_dev_callback(void *arg)
8193c330affSAlexander Motin {
8203c330affSAlexander Motin 	struct g_consumer *cp;
8213c330affSAlexander Motin 	struct g_dev_softc *sc;
82202a99230SAlexander Motin 	int active;
8233c330affSAlexander Motin 
8243c330affSAlexander Motin 	cp = arg;
8253c330affSAlexander Motin 	sc = cp->private;
8263c330affSAlexander Motin 	g_trace(G_T_TOPOLOGY, "g_dev_callback(%p(%s))", cp, cp->geom->name);
8273c330affSAlexander Motin 
8283c330affSAlexander Motin 	sc->sc_dev = NULL;
8293c330affSAlexander Motin 	sc->sc_alias = NULL;
83002a99230SAlexander Motin 	active = atomic_fetchadd_int(&sc->sc_active, SC_A_DESTROY);
83102a99230SAlexander Motin 	if ((active & SC_A_ACTIVE) == 0)
8323c330affSAlexander Motin 		g_post_event(g_dev_destroy, cp, M_WAITOK, NULL);
8333c330affSAlexander Motin }
8343c330affSAlexander Motin 
8353c330affSAlexander Motin /*
8364b8374a7SPoul-Henning Kamp  * g_dev_orphan()
8374b8374a7SPoul-Henning Kamp  *
838df6c9fe9SPoul-Henning Kamp  * Called from below when the provider orphaned us.
839df6c9fe9SPoul-Henning Kamp  * - Clear any dump settings.
8403c330affSAlexander Motin  * - Request asynchronous device destruction to prevent any more requests
8413c330affSAlexander Motin  *   from coming in.  The provider is already marked with an error, so
842e8d57122SPedro F. Giffuni  *   anything which comes in the interim will be returned immediately.
8434b8374a7SPoul-Henning Kamp  */
844dd84a43cSPoul-Henning Kamp 
845dd84a43cSPoul-Henning Kamp static void
g_dev_orphan(struct g_consumer * cp)846b1876192SPoul-Henning Kamp g_dev_orphan(struct g_consumer *cp)
847dd84a43cSPoul-Henning Kamp {
84889c9c53dSPoul-Henning Kamp 	struct cdev *dev;
8493c330affSAlexander Motin 	struct g_dev_softc *sc;
850dd84a43cSPoul-Henning Kamp 
851dd84a43cSPoul-Henning Kamp 	g_topology_assert();
8523c330affSAlexander Motin 	sc = cp->private;
8533c330affSAlexander Motin 	dev = sc->sc_dev;
8543c330affSAlexander Motin 	g_trace(G_T_TOPOLOGY, "g_dev_orphan(%p(%s))", cp, cp->geom->name);
855df6c9fe9SPoul-Henning Kamp 
856*f7856fe8SFabian Keil 	if (dev == NULL)
857*f7856fe8SFabian Keil 		return;
858*f7856fe8SFabian Keil 
859df6c9fe9SPoul-Henning Kamp 	/* Reset any dump-area set on this device */
8606b6e2954SConrad Meyer 	if (dev->si_flags & SI_DUMPDEV) {
8616b6e2954SConrad Meyer 		struct diocskerneldump_arg kda;
8626b6e2954SConrad Meyer 
8636b6e2954SConrad Meyer 		bzero(&kda, sizeof(kda));
8646b6e2954SConrad Meyer 		kda.kda_index = KDA_REMOVE_DEV;
8656b6e2954SConrad Meyer 		(void)dumper_remove(devtoname(dev), &kda);
8666b6e2954SConrad Meyer 	}
867df6c9fe9SPoul-Henning Kamp 
86889c9c53dSPoul-Henning Kamp 	/* Destroy the struct cdev *so we get no more requests */
8699c498bd5SAlexander Motin 	delist_dev(dev);
8703c330affSAlexander Motin 	destroy_dev_sched_cb(dev, g_dev_callback, cp);
871dd84a43cSPoul-Henning Kamp }
872dd84a43cSPoul-Henning Kamp 
873a50e92ccSRobert Wing static void
gdev_filter_detach(struct knote * kn)874a50e92ccSRobert Wing gdev_filter_detach(struct knote *kn)
875a50e92ccSRobert Wing {
876a50e92ccSRobert Wing 	struct g_dev_softc *sc;
877a50e92ccSRobert Wing 
878a50e92ccSRobert Wing 	sc = kn->kn_hook;
879a50e92ccSRobert Wing 
880a50e92ccSRobert Wing 	knlist_remove(&sc->sc_selinfo.si_note, kn, 0);
881a50e92ccSRobert Wing }
882a50e92ccSRobert Wing 
883a50e92ccSRobert Wing static int
gdev_filter_vnode(struct knote * kn,long hint)884a50e92ccSRobert Wing gdev_filter_vnode(struct knote *kn, long hint)
885a50e92ccSRobert Wing {
886a50e92ccSRobert Wing 	kn->kn_fflags |= kn->kn_sfflags & hint;
887a50e92ccSRobert Wing 
888a50e92ccSRobert Wing 	return (kn->kn_fflags != 0);
889a50e92ccSRobert Wing }
890a50e92ccSRobert Wing 
891a50e92ccSRobert Wing static int
g_dev_kqfilter(struct cdev * dev,struct knote * kn)892a50e92ccSRobert Wing g_dev_kqfilter(struct cdev *dev, struct knote *kn)
893a50e92ccSRobert Wing {
894a50e92ccSRobert Wing 	struct g_dev_softc *sc;
895a50e92ccSRobert Wing 
896a50e92ccSRobert Wing 	sc = dev->si_drv1;
897a50e92ccSRobert Wing 
898a50e92ccSRobert Wing 	if (kn->kn_filter != EVFILT_VNODE)
899a50e92ccSRobert Wing 		return (EINVAL);
900a50e92ccSRobert Wing 
90165c87a6cSRobert Wing #define SUPPORTED_EVENTS (NOTE_ATTRIB | NOTE_OPEN | NOTE_CLOSE | \
90265c87a6cSRobert Wing     NOTE_CLOSE_WRITE | NOTE_READ | NOTE_WRITE)
90365c87a6cSRobert Wing 	if (kn->kn_sfflags & ~SUPPORTED_EVENTS)
90465c87a6cSRobert Wing 		return (EOPNOTSUPP);
905a50e92ccSRobert Wing 
906a50e92ccSRobert Wing 	kn->kn_fop = &gdev_filterops_vnode;
907a50e92ccSRobert Wing 	kn->kn_hook = sc;
908a50e92ccSRobert Wing 	knlist_add(&sc->sc_selinfo.si_note, kn, 0);
909a50e92ccSRobert Wing 
910a50e92ccSRobert Wing 	return (0);
911a50e92ccSRobert Wing }
912a50e92ccSRobert Wing 
9134b8374a7SPoul-Henning Kamp DECLARE_GEOM_CLASS(g_dev_class, g_dev);
914