xref: /freebsd/sys/geom/geom_dev.c (revision a3e8fd0b7f663db7eafff527d5c3ca3bcfa8a537)
1 /*-
2  * Copyright (c) 2002 Poul-Henning Kamp
3  * Copyright (c) 2002 Networks Associates Technology, Inc.
4  * All rights reserved.
5  *
6  * This software was developed for the FreeBSD Project by Poul-Henning Kamp
7  * and NAI Labs, the Security Research Division of Network Associates, Inc.
8  * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
9  * DARPA CHATS research program.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. The names of the authors may not be used to endorse or promote
20  *    products derived from this software without specific prior written
21  *    permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  * $FreeBSD$
36  */
37 
38 #include <sys/param.h>
39 #include <sys/stdint.h>
40 #include <sys/systm.h>
41 #include <sys/malloc.h>
42 #include <sys/kernel.h>
43 #include <sys/conf.h>
44 #include <sys/bio.h>
45 #include <sys/lock.h>
46 #include <sys/mutex.h>
47 #include <sys/errno.h>
48 #include <sys/time.h>
49 #include <sys/disk.h>
50 #include <sys/fcntl.h>
51 #include <geom/geom.h>
52 #include <geom/geom_int.h>
53 #include <machine/limits.h>
54 
55 #define CDEV_MAJOR	4
56 
57 static d_open_t		g_dev_open;
58 static d_close_t	g_dev_close;
59 static d_strategy_t	g_dev_strategy;
60 static d_ioctl_t	g_dev_ioctl;
61 static d_psize_t	g_dev_psize;
62 
63 static struct cdevsw g_dev_cdevsw = {
64 	/* open */      g_dev_open,
65 	/* close */     g_dev_close,
66 	/* read */      physread,
67 	/* write */     physwrite,
68 	/* ioctl */     g_dev_ioctl,
69 	/* poll */      nopoll,
70 	/* mmap */      nommap,
71 	/* strategy */  g_dev_strategy,
72 	/* name */      "g_dev",
73 	/* maj */       CDEV_MAJOR,
74 	/* dump */      nodump,
75 	/* psize */     g_dev_psize,
76 	/* flags */     D_DISK | D_CANFREE | D_TRACKCLOSE,
77 	/* kqfilter */	nokqfilter
78 };
79 
80 static g_taste_t g_dev_taste;
81 static g_orphan_t g_dev_orphan;
82 
83 static struct g_class g_dev_class	= {
84 	"DEV",
85 	g_dev_taste,
86 	NULL,
87 	G_CLASS_INITIALIZER
88 };
89 
90 static void
91 g_dev_clone(void *arg __unused, char *name, int namelen __unused, dev_t *dev)
92 {
93 	struct g_geom *gp;
94 
95 	if (*dev != NODEV)
96 		return;
97 
98 	g_trace(G_T_TOPOLOGY, "g_dev_clone(%s)", name);
99 	g_waitidle();
100 
101 	/* XXX: can I drop Giant here ??? */
102 	/* g_topology_lock(); */
103 	LIST_FOREACH(gp, &g_dev_class.geom, geom) {
104 		if (strcmp(gp->name, name))
105 			continue;
106 		*dev = gp->softc;
107 		g_trace(G_T_TOPOLOGY, "g_dev_clone(%s) = %p", name, *dev);
108 		return;
109 	}
110 	/* g_topology_unlock(); */
111 	return;
112 }
113 
114 static void
115 g_dev_register_cloner(void *foo __unused)
116 {
117 	static int once;
118 
119 	/* XXX: why would this happen more than once ?? */
120 	if (!once) {
121 		EVENTHANDLER_REGISTER(dev_clone, g_dev_clone, 0, 1000);
122 		once++;
123 	}
124 }
125 
126 SYSINIT(geomdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE,g_dev_register_cloner,NULL);
127 
128 static struct g_geom *
129 g_dev_taste(struct g_class *mp, struct g_provider *pp, int insist __unused)
130 {
131 	struct g_geom *gp;
132 	struct g_consumer *cp;
133 	static int unit;
134 	int error;
135 	dev_t dev;
136 
137 	g_trace(G_T_TOPOLOGY, "dev_taste(%s,%s)", mp->name, pp->name);
138 	g_topology_assert();
139 	LIST_FOREACH(cp, &pp->consumers, consumers)
140 		if (cp->geom->class == mp)
141 			return (NULL);
142 	gp = g_new_geomf(mp, pp->name);
143 	gp->orphan = g_dev_orphan;
144 	cp = g_new_consumer(gp);
145 	error = g_attach(cp, pp);
146 	KASSERT(error == 0,
147 	    ("g_dev_taste(%s) failed to g_attach, err=%d", pp->name, error));
148 	/*
149 	 * XXX: I'm not 100% sure we can call make_dev(9) without Giant
150 	 * yet.  Once we can, we don't need to drop topology here either.
151 	 */
152 	g_topology_unlock();
153 	mtx_lock(&Giant);
154 	dev = make_dev(&g_dev_cdevsw, unit2minor(unit++),
155 	    UID_ROOT, GID_OPERATOR, 0640, gp->name);
156 	mtx_unlock(&Giant);
157 	g_topology_lock();
158 
159 	gp->softc = dev;
160 	dev->si_drv1 = gp;
161 	dev->si_drv2 = cp;
162 	return (gp);
163 }
164 
165 static int
166 g_dev_open(dev_t dev, int flags, int fmt, struct thread *td)
167 {
168 	struct g_geom *gp;
169 	struct g_consumer *cp;
170 	int error, r, w, e;
171 
172 	gp = dev->si_drv1;
173 	cp = dev->si_drv2;
174 	if (gp == NULL || cp == NULL)
175 		return(ENXIO);
176 	g_trace(G_T_ACCESS, "g_dev_open(%s, %d, %d, %p)",
177 	    gp->name, flags, fmt, td);
178 	DROP_GIANT();
179 	g_topology_lock();
180 	g_silence();
181 	r = flags & FREAD ? 1 : 0;
182 	w = flags & FWRITE ? 1 : 0;
183 #ifdef notyet
184 	e = flags & O_EXCL ? 1 : 0;
185 #else
186 	e = 0;
187 #endif
188 	error = g_access_rel(cp, r, w, e);
189 	g_topology_unlock();
190 	PICKUP_GIANT();
191 	g_waitidle();
192 	return(error);
193 }
194 
195 static int
196 g_dev_close(dev_t dev, int flags, int fmt, struct thread *td)
197 {
198 	struct g_geom *gp;
199 	struct g_consumer *cp;
200 	int error, r, w, e;
201 
202 	gp = dev->si_drv1;
203 	cp = dev->si_drv2;
204 	if (gp == NULL || cp == NULL)
205 		return(ENXIO);
206 	g_trace(G_T_ACCESS, "g_dev_close(%s, %d, %d, %p)",
207 	    gp->name, flags, fmt, td);
208 	DROP_GIANT();
209 	g_topology_lock();
210 	g_silence();
211 	r = flags & FREAD ? -1 : 0;
212 	w = flags & FWRITE ? -1 : 0;
213 #ifdef notyet
214 	e = flags & O_EXCL ? -1 : 0;
215 #else
216 	e = 0;
217 #endif
218 	error = g_access_rel(cp, r, w, e);
219 	g_topology_unlock();
220 	PICKUP_GIANT();
221 	g_waitidle();
222 	return (error);
223 }
224 
225 static int
226 g_dev_ioctl(dev_t dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
227 {
228 	struct g_geom *gp, *gp2;
229 	struct g_consumer *cp;
230 	struct g_provider *pp2;
231 	struct g_kerneldump kd;
232 	int i, error;
233 	u_int u;
234 	struct g_ioctl *gio;
235 
236 	gp = dev->si_drv1;
237 	cp = dev->si_drv2;
238 	pp2 = cp->provider;
239 	gp2 = pp2->geom;
240 	gio = NULL;
241 
242 	error = 0;
243 	DROP_GIANT();
244 
245 	i = IOCPARM_LEN(cmd);
246 	switch (cmd) {
247 	case DIOCGSECTORSIZE:
248 		*(u_int *)data = cp->provider->sectorsize;
249 		if (*(u_int *)data == 0)
250 			error = ENOENT;
251 		break;
252 	case DIOCGMEDIASIZE:
253 		*(off_t *)data = cp->provider->mediasize;
254 		if (*(off_t *)data == 0)
255 			error = ENOENT;
256 		break;
257 	case DIOCGFWSECTORS:
258 		error = g_io_getattr("GEOM::fwsectors", cp, &i, data);
259 		if (error == 0 && *(u_int *)data == 0)
260 			error = ENOENT;
261 		break;
262 	case DIOCGFWHEADS:
263 		error = g_io_getattr("GEOM::fwheads", cp, &i, data);
264 		if (error == 0 && *(u_int *)data == 0)
265 			error = ENOENT;
266 		break;
267 	case DIOCGFRONTSTUFF:
268 		error = g_io_getattr("GEOM::frontstuff", cp, &i, data);
269 		break;
270 	case DIOCSKERNELDUMP:
271 		u = *((u_int *)data);
272 		if (!u) {
273 			set_dumper(NULL);
274 			error = 0;
275 			break;
276 		}
277 		kd.offset = 0;
278 		kd.length = OFF_MAX;
279 		i = sizeof kd;
280 		error = g_io_getattr("GEOM::kerneldump", cp, &i, &kd);
281 		if (!error)
282 			dev->si_flags |= SI_DUMPDEV;
283 		break;
284 
285 	default:
286 		gio = g_malloc(sizeof *gio, M_WAITOK | M_ZERO);
287 		gio->cmd = cmd;
288 		gio->data = data;
289 		gio->fflag = fflag;
290 		gio->td = td;
291 		i = sizeof *gio;
292 		if (cmd & IOC_IN)
293 			error = g_io_setattr("GEOM::ioctl", cp, i, gio);
294 		else
295 			error = g_io_getattr("GEOM::ioctl", cp, &i, gio);
296 		break;
297 	}
298 
299 	PICKUP_GIANT();
300 	if (error == EDIRIOCTL) {
301 		KASSERT(gio != NULL, ("NULL gio but EDIRIOCTL"));
302 		KASSERT(gio->func != NULL, ("NULL function but EDIRIOCTL"));
303 		error = (gio->func)(gio->dev, cmd, data, fflag, td);
304 	}
305 	if (gio != NULL)
306 		g_free(gio);
307 	g_waitidle();
308 	if (error == ENOIOCTL) {
309 		if (g_debugflags & G_T_TOPOLOGY) {
310 			i = IOCGROUP(cmd);
311 			printf("IOCTL(0x%lx) \"%s\"", cmd, gp->name);
312 			if (i > ' ' && i <= '~')
313 				printf(" '%c'", (int)IOCGROUP(cmd));
314 			else
315 				printf(" 0x%lx", IOCGROUP(cmd));
316 			printf("/%ld ", cmd & 0xff);
317 			if (cmd & IOC_IN)
318 				printf("I");
319 			if (cmd & IOC_OUT)
320 				printf("O");
321 			printf("(%ld) = ENOIOCTL\n", IOCPARM_LEN(cmd));
322 		}
323 		error = ENOTTY;
324 	}
325 	return (error);
326 }
327 
328 static int
329 g_dev_psize(dev_t dev)
330 {
331 	struct g_consumer *cp;
332 	off_t mediasize;
333 
334 	cp = dev->si_drv2;
335 
336 	mediasize = cp->provider->mediasize;
337 	return (mediasize >> DEV_BSHIFT);
338 }
339 
340 static void
341 g_dev_done(struct bio *bp2)
342 {
343 	struct bio *bp;
344 
345 	bp = bp2->bio_linkage;
346 	bp->bio_error = bp2->bio_error;
347 	if (bp->bio_error != 0) {
348 		g_trace(G_T_BIO, "g_dev_done(%p) had error %d",
349 		    bp2, bp->bio_error);
350 		bp->bio_flags |= BIO_ERROR;
351 	} else {
352 		g_trace(G_T_BIO, "g_dev_done(%p/%p) resid %ld completed %jd",
353 		    bp2, bp, bp->bio_resid, (intmax_t)bp2->bio_completed);
354 	}
355 	bp->bio_resid = bp->bio_bcount - bp2->bio_completed;
356 	g_destroy_bio(bp2);
357 	mtx_lock(&Giant);
358 	biodone(bp);
359 	mtx_unlock(&Giant);
360 }
361 
362 static void
363 g_dev_strategy(struct bio *bp)
364 {
365 	struct g_geom *gp;
366 	struct g_consumer *cp;
367 	struct bio *bp2;
368 	dev_t dev;
369 
370 	dev = bp->bio_dev;
371 	gp = dev->si_drv1;
372 	cp = dev->si_drv2;
373 	bp2 = g_clone_bio(bp);
374 	bp2->bio_offset = (off_t)bp->bio_blkno << DEV_BSHIFT;
375 	bp2->bio_length = (off_t)bp->bio_bcount;
376 	bp2->bio_done = g_dev_done;
377 	g_trace(G_T_BIO,
378 	    "g_dev_strategy(%p/%p) offset %jd length %jd data %p cmd %d",
379 	    bp, bp2, (intmax_t)bp->bio_offset, (intmax_t)bp2->bio_length,
380 	    bp2->bio_data, bp2->bio_cmd);
381 	g_io_request(bp2, cp);
382 }
383 
384 /*
385  * g_dev_orphan()
386  *
387  * Called from below when the provider orphaned us.  It is our responsibility
388  * to get the access counts back to zero, until we do so the stack below will
389  * not unravel.  We must clear the kernel-dump settings, if this is the
390  * current dumpdev.  We call destroy_dev(9) to send our dev_t the way of
391  * punched cards and if we have non-zero access counts, we call down with
392  * them negated before we detattch and selfdestruct.
393  */
394 
395 static void
396 g_dev_orphan(struct g_consumer *cp)
397 {
398 	struct g_geom *gp;
399 	dev_t dev;
400 
401 	gp = cp->geom;
402 	g_trace(G_T_TOPOLOGY, "g_dev_orphan(%p(%s))", cp, gp->name);
403 	g_topology_assert();
404 	if (cp->biocount > 0)
405 		return;
406 	dev = gp->softc;
407 	if (dev->si_flags & SI_DUMPDEV)
408 		set_dumper(NULL);
409 	/* XXX: we may need Giant for now */
410 	destroy_dev(dev);
411 	if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
412 		g_access_rel(cp, -cp->acr, -cp->acw, -cp->ace);
413 	g_detach(cp);
414 	g_destroy_consumer(cp);
415 	g_destroy_geom(gp);
416 }
417 
418 DECLARE_GEOM_CLASS(g_dev_class, g_dev);
419