xref: /freebsd/sys/geom/geom_dev.c (revision dce6e6518b85561495cff38a3074a69d29d58a55)
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 
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38 
39 #include <sys/param.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 <sys/limits.h>
52 #include <geom/geom.h>
53 #include <geom/geom_int.h>
54 
55 static d_open_t		g_dev_open;
56 static d_close_t	g_dev_close;
57 static d_strategy_t	g_dev_strategy;
58 static d_ioctl_t	g_dev_ioctl;
59 
60 static struct cdevsw g_dev_cdevsw = {
61 	.d_open =	g_dev_open,
62 	.d_close =	g_dev_close,
63 	.d_read =	physread,
64 	.d_write =	physwrite,
65 	.d_ioctl =	g_dev_ioctl,
66 	.d_strategy =	g_dev_strategy,
67 	.d_name =	"g_dev",
68 	.d_maj =	GEOM_MAJOR,
69 	.d_flags =	D_DISK | D_TRACKCLOSE,
70 };
71 
72 static g_taste_t g_dev_taste;
73 static g_orphan_t g_dev_orphan;
74 
75 static struct g_class g_dev_class	= {
76 	.name = "DEV",
77 	.taste = g_dev_taste,
78 };
79 
80 void
81 g_dev_print(void)
82 {
83 	struct g_geom *gp;
84 	char const *p = "";
85 
86 	LIST_FOREACH(gp, &g_dev_class.geom, geom) {
87 		printf("%s%s", p, gp->name);
88 		p = " ";
89 	}
90 	printf("\n");
91 }
92 
93 /*
94  * XXX: This is disgusting and wrong in every way imaginable:  The only reason
95  * XXX: we have a clone function is because of the root-mount hack we currently
96  * XXX: employ.  An improvment would be to unregister this cloner once we know
97  * XXX: we no longer need it.  Ideally, root-fs would be mounted through DEVFS
98  * XXX: eliminating the need for this hack.
99  */
100 static void
101 g_dev_clone(void *arg __unused, char *name, int namelen __unused, dev_t *dev)
102 {
103 	struct g_geom *gp;
104 
105 	if (*dev != NODEV)
106 		return;
107 
108 	g_waitidle();
109 
110 	/* g_topology_lock(); */
111 	LIST_FOREACH(gp, &g_dev_class.geom, geom) {
112 		if (strcmp(gp->name, name))
113 			continue;
114 		*dev = gp->softc;
115 		g_trace(G_T_TOPOLOGY, "g_dev_clone(%s) = %p", name, *dev);
116 		return;
117 	}
118 	/* g_topology_unlock(); */
119 	return;
120 }
121 
122 static void
123 g_dev_register_cloner(void *foo __unused)
124 {
125 	static int once;
126 
127 	/* XXX: why would this happen more than once ?? */
128 	if (!once) {
129 		EVENTHANDLER_REGISTER(dev_clone, g_dev_clone, 0, 1000);
130 		once++;
131 	}
132 }
133 
134 SYSINIT(geomdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE,g_dev_register_cloner,NULL);
135 
136 static struct g_geom *
137 g_dev_taste(struct g_class *mp, struct g_provider *pp, int insist __unused)
138 {
139 	struct g_geom *gp;
140 	struct g_consumer *cp;
141 	static int unit = GEOM_MINOR_PROVIDERS;
142 	int error;
143 	dev_t dev;
144 
145 	g_trace(G_T_TOPOLOGY, "dev_taste(%s,%s)", mp->name, pp->name);
146 	g_topology_assert();
147 	LIST_FOREACH(cp, &pp->consumers, consumers)
148 		if (cp->geom->class == mp)
149 			return (NULL);
150 	gp = g_new_geomf(mp, pp->name);
151 	gp->orphan = g_dev_orphan;
152 	cp = g_new_consumer(gp);
153 	error = g_attach(cp, pp);
154 	KASSERT(error == 0,
155 	    ("g_dev_taste(%s) failed to g_attach, err=%d", pp->name, error));
156 	/*
157 	 * XXX: I'm not 100% sure we can call make_dev(9) without Giant
158 	 * yet.  Once we can, we don't need to drop topology here either.
159 	 */
160 	g_topology_unlock();
161 	mtx_lock(&Giant);
162 	dev = make_dev(&g_dev_cdevsw, unit2minor(unit++),
163 	    UID_ROOT, GID_OPERATOR, 0640, gp->name);
164 	if (pp->flags & G_PF_CANDELETE)
165 		dev->si_flags |= SI_CANDELETE;
166 	mtx_unlock(&Giant);
167 	g_topology_lock();
168 	dev->si_iosize_max = MAXPHYS;
169 	dev->si_stripesize = pp->stripesize;
170 	dev->si_stripeoffset = pp->stripeoffset;
171 	gp->softc = dev;
172 	dev->si_drv1 = gp;
173 	dev->si_drv2 = cp;
174 	return (gp);
175 }
176 
177 static int
178 g_dev_open(dev_t dev, int flags, int fmt, struct thread *td)
179 {
180 	struct g_geom *gp;
181 	struct g_consumer *cp;
182 	int error, r, w, e;
183 
184 	gp = dev->si_drv1;
185 	cp = dev->si_drv2;
186 	if (gp == NULL || cp == NULL || gp->softc != dev)
187 		return(ENXIO);		/* g_dev_taste() not done yet */
188 
189 	g_trace(G_T_ACCESS, "g_dev_open(%s, %d, %d, %p)",
190 	    gp->name, flags, fmt, td);
191 	r = flags & FREAD ? 1 : 0;
192 	w = flags & FWRITE ? 1 : 0;
193 #ifdef notyet
194 	e = flags & O_EXCL ? 1 : 0;
195 #else
196 	e = 0;
197 #endif
198 	DROP_GIANT();
199 	g_topology_lock();
200 	if (dev->si_devsw == NULL)
201 		error = ENXIO;		/* We were orphaned */
202 	else
203 		error = g_access_rel(cp, r, w, e);
204 	g_topology_unlock();
205 	PICKUP_GIANT();
206 	g_waitidle();
207 	if (!error)
208 		dev->si_bsize_phys = cp->provider->sectorsize;
209 	return(error);
210 }
211 
212 static int
213 g_dev_close(dev_t dev, int flags, int fmt, struct thread *td)
214 {
215 	struct g_geom *gp;
216 	struct g_consumer *cp;
217 	int error, r, w, e, i;
218 
219 	gp = dev->si_drv1;
220 	cp = dev->si_drv2;
221 	if (gp == NULL || cp == NULL)
222 		return(ENXIO);
223 	g_trace(G_T_ACCESS, "g_dev_close(%s, %d, %d, %p)",
224 	    gp->name, flags, fmt, td);
225 	r = flags & FREAD ? -1 : 0;
226 	w = flags & FWRITE ? -1 : 0;
227 #ifdef notyet
228 	e = flags & O_EXCL ? -1 : 0;
229 #else
230 	e = 0;
231 #endif
232 	DROP_GIANT();
233 	g_topology_lock();
234 	if (dev->si_devsw == NULL)
235 		error = ENXIO;		/* We were orphaned */
236 	else
237 		error = g_access_rel(cp, r, w, e);
238 	for (i = 0; i < 10 * hz;) {
239 		if (cp->acr != 0 || cp->acw != 0)
240 			break;
241  		if (cp->nstart == cp->nend)
242 			break;
243 		tsleep(&i, PRIBIO, "gdevwclose", hz / 10);
244 		i += hz / 10;
245 	}
246 	if (cp->acr == 0 && cp->acw == 0 && cp->nstart != cp->nend) {
247 		printf("WARNING: Final close of geom_dev(%s) %s %s",
248 		    gp->name,
249 		    "still has outstanding I/O after 10 seconds.",
250 		    "Completing close anyway, panic may happen later.");
251 	}
252 	g_topology_unlock();
253 	PICKUP_GIANT();
254 	g_waitidle();
255 	return (error);
256 }
257 
258 /*
259  * XXX: Until we have unmessed the ioctl situation, there is a race against
260  * XXX: a concurrent orphanization.  We cannot close it by holding topology
261  * XXX: since that would prevent us from doing our job, and stalling events
262  * XXX: will break (actually: stall) the BSD disklabel hacks.
263  */
264 static int
265 g_dev_ioctl(dev_t dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
266 {
267 	struct g_geom *gp;
268 	struct g_consumer *cp;
269 	struct g_kerneldump kd;
270 	int i, error;
271 	u_int u;
272 	struct g_ioctl *gio;
273 
274 	gp = dev->si_drv1;
275 	cp = dev->si_drv2;
276 	gio = NULL;
277 
278 	error = 0;
279 	KASSERT(cp->acr || cp->acw,
280 	    ("Consumer with zero access count in g_dev_ioctl"));
281 	DROP_GIANT();
282 
283 	gio = NULL;
284 	i = IOCPARM_LEN(cmd);
285 	switch (cmd) {
286 	case DIOCGSECTORSIZE:
287 		*(u_int *)data = cp->provider->sectorsize;
288 		if (*(u_int *)data == 0)
289 			error = ENOENT;
290 		break;
291 	case DIOCGMEDIASIZE:
292 		*(off_t *)data = cp->provider->mediasize;
293 		if (*(off_t *)data == 0)
294 			error = ENOENT;
295 		break;
296 	case DIOCGFWSECTORS:
297 		error = g_io_getattr("GEOM::fwsectors", cp, &i, data);
298 		if (error == 0 && *(u_int *)data == 0)
299 			error = ENOENT;
300 		break;
301 	case DIOCGFWHEADS:
302 		error = g_io_getattr("GEOM::fwheads", cp, &i, data);
303 		if (error == 0 && *(u_int *)data == 0)
304 			error = ENOENT;
305 		break;
306 	case DIOCGFRONTSTUFF:
307 		error = g_io_getattr("GEOM::frontstuff", cp, &i, data);
308 		break;
309 	case DIOCSKERNELDUMP:
310 		u = *((u_int *)data);
311 		if (!u) {
312 			set_dumper(NULL);
313 			error = 0;
314 			break;
315 		}
316 		kd.offset = 0;
317 		kd.length = OFF_MAX;
318 		i = sizeof kd;
319 		error = g_io_getattr("GEOM::kerneldump", cp, &i, &kd);
320 		if (!error)
321 			dev->si_flags |= SI_DUMPDEV;
322 		break;
323 
324 	default:
325 		gio = g_malloc(sizeof *gio, M_WAITOK | M_ZERO);
326 		gio->cmd = cmd;
327 		gio->data = data;
328 		gio->fflag = fflag;
329 		gio->td = td;
330 		i = sizeof *gio;
331 		/*
332 		 * We always issue ioctls as getattr since the direction of data
333 		 * movement in ioctl is no indication of the ioctl being a "set"
334 		 * or "get" type ioctl or if such simplistic terms even apply
335 		 */
336 		error = g_io_getattr("GEOM::ioctl", cp, &i, gio);
337 		break;
338 	}
339 
340 	PICKUP_GIANT();
341 	if (error == EDIRIOCTL) {
342 		KASSERT(gio != NULL, ("NULL gio but EDIRIOCTL"));
343 		KASSERT(gio->func != NULL, ("NULL function but EDIRIOCTL"));
344 		error = (gio->func)(gio->dev, cmd, data, fflag, td);
345 	}
346 	g_waitidle();
347 	if (gio != NULL && (error == EOPNOTSUPP || error == ENOIOCTL)) {
348 		if (g_debugflags & G_T_TOPOLOGY) {
349 			i = IOCGROUP(cmd);
350 			printf("IOCTL(0x%lx) \"%s\"", cmd, gp->name);
351 			if (i > ' ' && i <= '~')
352 				printf(" '%c'", (int)IOCGROUP(cmd));
353 			else
354 				printf(" 0x%lx", IOCGROUP(cmd));
355 			printf("/%ld ", cmd & 0xff);
356 			if (cmd & IOC_IN)
357 				printf("I");
358 			if (cmd & IOC_OUT)
359 				printf("O");
360 			printf("(%ld) = ENOIOCTL\n", IOCPARM_LEN(cmd));
361 		}
362 		error = ENOTTY;
363 	}
364 	if (gio != NULL)
365 		g_free(gio);
366 	return (error);
367 }
368 
369 static void
370 g_dev_done(struct bio *bp2)
371 {
372 	struct bio *bp;
373 
374 	bp = bp2->bio_parent;
375 	bp->bio_error = bp2->bio_error;
376 	if (bp->bio_error != 0) {
377 		g_trace(G_T_BIO, "g_dev_done(%p) had error %d",
378 		    bp2, bp->bio_error);
379 		bp->bio_flags |= BIO_ERROR;
380 	} else {
381 		g_trace(G_T_BIO, "g_dev_done(%p/%p) resid %ld completed %jd",
382 		    bp2, bp, bp->bio_resid, (intmax_t)bp2->bio_completed);
383 	}
384 	bp->bio_resid = bp->bio_bcount - bp2->bio_completed;
385 	g_destroy_bio(bp2);
386 	mtx_lock(&Giant);
387 	biodone(bp);
388 	mtx_unlock(&Giant);
389 }
390 
391 static void
392 g_dev_strategy(struct bio *bp)
393 {
394 	struct g_consumer *cp;
395 	struct bio *bp2;
396 	dev_t dev;
397 
398 	KASSERT(bp->bio_cmd == BIO_READ ||
399 	        bp->bio_cmd == BIO_WRITE ||
400 	        bp->bio_cmd == BIO_DELETE,
401 		("Wrong bio_cmd bio=%p cmd=%d", bp, bp->bio_cmd));
402 	dev = bp->bio_dev;
403 	cp = dev->si_drv2;
404 	KASSERT(cp->acr || cp->acw,
405 	    ("Consumer with zero access count in g_dev_strategy"));
406 
407 	bp2 = g_clone_bio(bp);
408 	KASSERT(bp2 != NULL, ("XXX: ENOMEM in a bad place"));
409 	bp2->bio_offset = (off_t)bp->bio_blkno << DEV_BSHIFT;
410 	KASSERT(bp2->bio_offset >= 0,
411 	    ("Negative bio_offset (%jd) on bio %p",
412 	    (intmax_t)bp2->bio_offset, bp));
413 	bp2->bio_length = (off_t)bp->bio_bcount;
414 	bp2->bio_done = g_dev_done;
415 	g_trace(G_T_BIO,
416 	    "g_dev_strategy(%p/%p) offset %jd length %jd data %p cmd %d",
417 	    bp, bp2, (intmax_t)bp->bio_offset, (intmax_t)bp2->bio_length,
418 	    bp2->bio_data, bp2->bio_cmd);
419 	g_io_request(bp2, cp);
420 	KASSERT(cp->acr || cp->acw,
421 	    ("g_dev_strategy raced with g_dev_close and lost"));
422 
423 }
424 
425 /*
426  * g_dev_orphan()
427  *
428  * Called from below when the provider orphaned us.
429  * - Clear any dump settings.
430  * - Destroy the dev_t to prevent any more request from coming in.  The
431  *   provider is already marked with an error, so anything which comes in
432  *   in the interrim will be returned immediately.
433  * - Wait for any outstanding I/O to finish.
434  * - Set our access counts to zero, whatever they were.
435  * - Detach and self-destruct.
436  */
437 
438 static void
439 g_dev_orphan(struct g_consumer *cp)
440 {
441 	struct g_geom *gp;
442 	dev_t dev;
443 
444 	g_topology_assert();
445 	gp = cp->geom;
446 	dev = gp->softc;
447 	g_trace(G_T_TOPOLOGY, "g_dev_orphan(%p(%s))", cp, gp->name);
448 
449 	/* Reset any dump-area set on this device */
450 	if (dev->si_flags & SI_DUMPDEV)
451 		set_dumper(NULL);
452 
453 	/* Destroy the dev_t so we get no more requests */
454 	destroy_dev(dev);
455 
456 	/* Wait for the cows to come home */
457 	while (cp->nstart != cp->nend)
458 		msleep(&dev, NULL, PRIBIO, "gdevorphan", hz / 10);
459 
460 	if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
461 		g_access_rel(cp, -cp->acr, -cp->acw, -cp->ace);
462 
463 	g_detach(cp);
464 	g_destroy_consumer(cp);
465 	g_destroy_geom(gp);
466 }
467 
468 DECLARE_GEOM_CLASS(g_dev_class, g_dev);
469