xref: /freebsd/sys/geom/geom_dev.c (revision 27c9070e7e726d96ce4995cfc3f64a284fdaff2c)
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/ctype.h>
45 #include <sys/bio.h>
46 #include <sys/bus.h>
47 #include <sys/lock.h>
48 #include <sys/mutex.h>
49 #include <sys/proc.h>
50 #include <sys/errno.h>
51 #include <sys/time.h>
52 #include <sys/disk.h>
53 #include <sys/fcntl.h>
54 #include <sys/limits.h>
55 #include <geom/geom.h>
56 #include <geom/geom_int.h>
57 #include <machine/stdarg.h>
58 
59 /*
60  * Use the consumer private field to reference a physdev alias (if any).
61  */
62 #define cp_alias_dev	private
63 
64 static d_open_t		g_dev_open;
65 static d_close_t	g_dev_close;
66 static d_strategy_t	g_dev_strategy;
67 static d_ioctl_t	g_dev_ioctl;
68 
69 static struct cdevsw g_dev_cdevsw = {
70 	.d_version =	D_VERSION,
71 	.d_open =	g_dev_open,
72 	.d_close =	g_dev_close,
73 	.d_read =	physread,
74 	.d_write =	physwrite,
75 	.d_ioctl =	g_dev_ioctl,
76 	.d_strategy =	g_dev_strategy,
77 	.d_name =	"g_dev",
78 	.d_flags =	D_DISK | D_TRACKCLOSE,
79 };
80 
81 static g_taste_t g_dev_taste;
82 static g_orphan_t g_dev_orphan;
83 static g_attrchanged_t g_dev_attrchanged;
84 
85 static struct g_class g_dev_class	= {
86 	.name = "DEV",
87 	.version = G_VERSION,
88 	.taste = g_dev_taste,
89 	.orphan = g_dev_orphan,
90 	.attrchanged = g_dev_attrchanged
91 };
92 
93 void
94 g_dev_print(void)
95 {
96 	struct g_geom *gp;
97 	char const *p = "";
98 
99 	LIST_FOREACH(gp, &g_dev_class.geom, geom) {
100 		printf("%s%s", p, gp->name);
101 		p = " ";
102 	}
103 	printf("\n");
104 }
105 
106 static void
107 g_dev_attrchanged(struct g_consumer *cp, const char *attr)
108 {
109 	struct cdev *dev;
110 	char buf[SPECNAMELEN + 6];
111 
112 	if (strcmp(attr, "GEOM::media") == 0) {
113 		dev = cp->geom->softc;
114 		snprintf(buf, sizeof(buf), "cdev=%s", dev->si_name);
115 		devctl_notify_f("DEVFS", "CDEV", "MEDIACHANGE", buf, M_WAITOK);
116 		dev = cp->cp_alias_dev;
117 		if (dev != NULL) {
118 			snprintf(buf, sizeof(buf), "cdev=%s", dev->si_name);
119 			devctl_notify_f("DEVFS", "CDEV", "MEDIACHANGE", buf,
120 			    M_WAITOK);
121 		}
122 		return;
123 	}
124 
125 	if (strcmp(attr, "GEOM::physpath") != 0)
126 		return;
127 
128 	if (g_access(cp, 1, 0, 0) == 0) {
129 		char *physpath;
130 		int error, physpath_len;
131 
132 		physpath_len = MAXPATHLEN;
133 		physpath = g_malloc(physpath_len, M_WAITOK|M_ZERO);
134 		error =
135 		    g_io_getattr("GEOM::physpath", cp, &physpath_len, physpath);
136 		g_access(cp, -1, 0, 0);
137 		if (error == 0 && strlen(physpath) != 0) {
138 			struct cdev *old_alias_dev;
139 			struct cdev **alias_devp;
140 
141 			dev = cp->geom->softc;
142 			old_alias_dev = cp->cp_alias_dev;
143 			alias_devp = (struct cdev **)&cp->cp_alias_dev;
144 			make_dev_physpath_alias(MAKEDEV_WAITOK, alias_devp,
145 			    dev, old_alias_dev, physpath);
146 		} else if (cp->cp_alias_dev) {
147 			destroy_dev((struct cdev *)cp->cp_alias_dev);
148 			cp->cp_alias_dev = NULL;
149 		}
150 		g_free(physpath);
151 	}
152 }
153 
154 struct g_provider *
155 g_dev_getprovider(struct cdev *dev)
156 {
157 	struct g_consumer *cp;
158 
159 	g_topology_assert();
160 	if (dev == NULL)
161 		return (NULL);
162 	if (dev->si_devsw != &g_dev_cdevsw)
163 		return (NULL);
164 	cp = dev->si_drv2;
165 	return (cp->provider);
166 }
167 
168 static struct g_geom *
169 g_dev_taste(struct g_class *mp, struct g_provider *pp, int insist __unused)
170 {
171 	struct g_geom *gp;
172 	struct g_consumer *cp;
173 	int error, len;
174 	struct cdev *dev, *adev;
175 	char buf[64], *val;
176 
177 	g_trace(G_T_TOPOLOGY, "dev_taste(%s,%s)", mp->name, pp->name);
178 	g_topology_assert();
179 	gp = g_new_geomf(mp, pp->name);
180 	cp = g_new_consumer(gp);
181 	error = g_attach(cp, pp);
182 	KASSERT(error == 0,
183 	    ("g_dev_taste(%s) failed to g_attach, err=%d", pp->name, error));
184 	error = make_dev_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK, &dev,
185 	    &g_dev_cdevsw, NULL, UID_ROOT, GID_OPERATOR, 0640, "%s", gp->name);
186 	if (error != 0) {
187 		printf("%s: make_dev_p() failed (gp->name=%s, error=%d)\n",
188 		    __func__, gp->name, error);
189 		g_detach(cp);
190 		g_destroy_consumer(cp);
191 		g_destroy_geom(gp);
192 		return (NULL);
193 	}
194 
195 	/* Search for device alias name and create it if found. */
196 	adev = NULL;
197 	for (len = MIN(strlen(gp->name), sizeof(buf) - 15); len > 0; len--) {
198 		snprintf(buf, sizeof(buf), "kern.devalias.%s", gp->name);
199 		buf[14 + len] = 0;
200 		val = getenv(buf);
201 		if (val != NULL) {
202 			snprintf(buf, sizeof(buf), "%s%s",
203 			    val, gp->name + len);
204 			freeenv(val);
205 			make_dev_alias_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK,
206 			    &adev, dev, "%s", buf);
207 			break;
208 		}
209 	}
210 
211 	if (pp->flags & G_PF_CANDELETE)
212 		dev->si_flags |= SI_CANDELETE;
213 	dev->si_iosize_max = MAXPHYS;
214 	gp->softc = dev;
215 	dev->si_drv1 = gp;
216 	dev->si_drv2 = cp;
217 	if (adev != NULL) {
218 		if (pp->flags & G_PF_CANDELETE)
219 			adev->si_flags |= SI_CANDELETE;
220 		adev->si_iosize_max = MAXPHYS;
221 		adev->si_drv1 = gp;
222 		adev->si_drv2 = cp;
223 	}
224 
225 	g_dev_attrchanged(cp, "GEOM::physpath");
226 
227 	return (gp);
228 }
229 
230 static int
231 g_dev_open(struct cdev *dev, int flags, int fmt, struct thread *td)
232 {
233 	struct g_geom *gp;
234 	struct g_consumer *cp;
235 	int error, r, w, e;
236 
237 	gp = dev->si_drv1;
238 	cp = dev->si_drv2;
239 	if (gp == NULL || cp == NULL || gp->softc != dev)
240 		return(ENXIO);		/* g_dev_taste() not done yet */
241 
242 	g_trace(G_T_ACCESS, "g_dev_open(%s, %d, %d, %p)",
243 	    gp->name, flags, fmt, td);
244 
245 	r = flags & FREAD ? 1 : 0;
246 	w = flags & FWRITE ? 1 : 0;
247 #ifdef notyet
248 	e = flags & O_EXCL ? 1 : 0;
249 #else
250 	e = 0;
251 #endif
252 	if (w) {
253 		/*
254 		 * When running in very secure mode, do not allow
255 		 * opens for writing of any disks.
256 		 */
257 		error = securelevel_ge(td->td_ucred, 2);
258 		if (error)
259 			return (error);
260 	}
261 	g_topology_lock();
262 	if (dev->si_devsw == NULL)
263 		error = ENXIO;		/* We were orphaned */
264 	else
265 		error = g_access(cp, r, w, e);
266 	g_topology_unlock();
267 	return(error);
268 }
269 
270 static int
271 g_dev_close(struct cdev *dev, int flags, int fmt, struct thread *td)
272 {
273 	struct g_geom *gp;
274 	struct g_consumer *cp;
275 	int error, r, w, e, i;
276 
277 	gp = dev->si_drv1;
278 	cp = dev->si_drv2;
279 	if (gp == NULL || cp == NULL)
280 		return(ENXIO);
281 	g_trace(G_T_ACCESS, "g_dev_close(%s, %d, %d, %p)",
282 	    gp->name, flags, fmt, td);
283 	r = flags & FREAD ? -1 : 0;
284 	w = flags & FWRITE ? -1 : 0;
285 #ifdef notyet
286 	e = flags & O_EXCL ? -1 : 0;
287 #else
288 	e = 0;
289 #endif
290 	g_topology_lock();
291 	if (dev->si_devsw == NULL)
292 		error = ENXIO;		/* We were orphaned */
293 	else
294 		error = g_access(cp, r, w, e);
295 	for (i = 0; i < 10 * hz;) {
296 		if (cp->acr != 0 || cp->acw != 0)
297 			break;
298  		if (cp->nstart == cp->nend)
299 			break;
300 		pause("gdevwclose", hz / 10);
301 		i += hz / 10;
302 	}
303 	if (cp->acr == 0 && cp->acw == 0 && cp->nstart != cp->nend) {
304 		printf("WARNING: Final close of geom_dev(%s) %s %s\n",
305 		    gp->name,
306 		    "still has outstanding I/O after 10 seconds.",
307 		    "Completing close anyway, panic may happen later.");
308 	}
309 	g_topology_unlock();
310 	return (error);
311 }
312 
313 /*
314  * XXX: Until we have unmessed the ioctl situation, there is a race against
315  * XXX: a concurrent orphanization.  We cannot close it by holding topology
316  * XXX: since that would prevent us from doing our job, and stalling events
317  * XXX: will break (actually: stall) the BSD disklabel hacks.
318  */
319 static int
320 g_dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
321 {
322 	struct g_geom *gp;
323 	struct g_consumer *cp;
324 	struct g_provider *pp;
325 	struct g_kerneldump kd;
326 	off_t offset, length, chunk;
327 	int i, error;
328 	u_int u;
329 
330 	gp = dev->si_drv1;
331 	cp = dev->si_drv2;
332 	pp = cp->provider;
333 
334 	error = 0;
335 	KASSERT(cp->acr || cp->acw,
336 	    ("Consumer with zero access count in g_dev_ioctl"));
337 
338 	i = IOCPARM_LEN(cmd);
339 	switch (cmd) {
340 	case DIOCGSECTORSIZE:
341 		*(u_int *)data = cp->provider->sectorsize;
342 		if (*(u_int *)data == 0)
343 			error = ENOENT;
344 		break;
345 	case DIOCGMEDIASIZE:
346 		*(off_t *)data = cp->provider->mediasize;
347 		if (*(off_t *)data == 0)
348 			error = ENOENT;
349 		break;
350 	case DIOCGFWSECTORS:
351 		error = g_io_getattr("GEOM::fwsectors", cp, &i, data);
352 		if (error == 0 && *(u_int *)data == 0)
353 			error = ENOENT;
354 		break;
355 	case DIOCGFWHEADS:
356 		error = g_io_getattr("GEOM::fwheads", cp, &i, data);
357 		if (error == 0 && *(u_int *)data == 0)
358 			error = ENOENT;
359 		break;
360 	case DIOCGFRONTSTUFF:
361 		error = g_io_getattr("GEOM::frontstuff", cp, &i, data);
362 		break;
363 	case DIOCSKERNELDUMP:
364 		u = *((u_int *)data);
365 		if (!u) {
366 			set_dumper(NULL);
367 			error = 0;
368 			break;
369 		}
370 		kd.offset = 0;
371 		kd.length = OFF_MAX;
372 		i = sizeof kd;
373 		error = g_io_getattr("GEOM::kerneldump", cp, &i, &kd);
374 		if (!error) {
375 			error = set_dumper(&kd.di);
376 			if (!error)
377 				dev->si_flags |= SI_DUMPDEV;
378 		}
379 		break;
380 	case DIOCGFLUSH:
381 		error = g_io_flush(cp);
382 		break;
383 	case DIOCGDELETE:
384 		offset = ((off_t *)data)[0];
385 		length = ((off_t *)data)[1];
386 		if ((offset % cp->provider->sectorsize) != 0 ||
387 		    (length % cp->provider->sectorsize) != 0 || length <= 0) {
388 			printf("%s: offset=%jd length=%jd\n", __func__, offset,
389 			    length);
390 			error = EINVAL;
391 			break;
392 		}
393 		while (length > 0) {
394 			chunk = length;
395 			if (chunk > 65536 * cp->provider->sectorsize)
396 				chunk = 65536 * cp->provider->sectorsize;
397 			error = g_delete_data(cp, offset, chunk);
398 			length -= chunk;
399 			offset += chunk;
400 			if (error)
401 				break;
402 			/*
403 			 * Since the request size is unbounded, the service
404 			 * time is likewise.  We make this ioctl interruptible
405 			 * by checking for signals for each bio.
406 			 */
407 			if (SIGPENDING(td))
408 				break;
409 		}
410 		break;
411 	case DIOCGIDENT:
412 		error = g_io_getattr("GEOM::ident", cp, &i, data);
413 		break;
414 	case DIOCGPROVIDERNAME:
415 		if (pp == NULL)
416 			return (ENOENT);
417 		strlcpy(data, pp->name, i);
418 		break;
419 	case DIOCGSTRIPESIZE:
420 		*(off_t *)data = cp->provider->stripesize;
421 		break;
422 	case DIOCGSTRIPEOFFSET:
423 		*(off_t *)data = cp->provider->stripeoffset;
424 		break;
425 	case DIOCGPHYSPATH:
426 		error = g_io_getattr("GEOM::physpath", cp, &i, data);
427 		if (error == 0 && *(char *)data == '\0')
428 			error = ENOENT;
429 		break;
430 	default:
431 		if (cp->provider->geom->ioctl != NULL) {
432 			error = cp->provider->geom->ioctl(cp->provider, cmd, data, fflag, td);
433 		} else {
434 			error = ENOIOCTL;
435 		}
436 	}
437 
438 	return (error);
439 }
440 
441 static void
442 g_dev_done(struct bio *bp2)
443 {
444 	struct bio *bp;
445 
446 	bp = bp2->bio_parent;
447 	bp->bio_error = bp2->bio_error;
448 	if (bp->bio_error != 0) {
449 		g_trace(G_T_BIO, "g_dev_done(%p) had error %d",
450 		    bp2, bp->bio_error);
451 		bp->bio_flags |= BIO_ERROR;
452 	} else {
453 		g_trace(G_T_BIO, "g_dev_done(%p/%p) resid %ld completed %jd",
454 		    bp2, bp, bp->bio_resid, (intmax_t)bp2->bio_completed);
455 	}
456 	bp->bio_resid = bp->bio_length - bp2->bio_completed;
457 	bp->bio_completed = bp2->bio_completed;
458 	g_destroy_bio(bp2);
459 	biodone(bp);
460 }
461 
462 static void
463 g_dev_strategy(struct bio *bp)
464 {
465 	struct g_consumer *cp;
466 	struct bio *bp2;
467 	struct cdev *dev;
468 
469 	KASSERT(bp->bio_cmd == BIO_READ ||
470 	        bp->bio_cmd == BIO_WRITE ||
471 	        bp->bio_cmd == BIO_DELETE,
472 		("Wrong bio_cmd bio=%p cmd=%d", bp, bp->bio_cmd));
473 	dev = bp->bio_dev;
474 	cp = dev->si_drv2;
475 	KASSERT(cp->acr || cp->acw,
476 	    ("Consumer with zero access count in g_dev_strategy"));
477 #ifdef INVARIANTS
478 	if ((bp->bio_offset % cp->provider->sectorsize) != 0 ||
479 	    (bp->bio_bcount % cp->provider->sectorsize) != 0) {
480 		bp->bio_resid = bp->bio_bcount;
481 		biofinish(bp, NULL, EINVAL);
482 		return;
483 	}
484 #endif
485 	for (;;) {
486 		/*
487 		 * XXX: This is not an ideal solution, but I belive it to
488 		 * XXX: deadlock safe, all things considered.
489 		 */
490 		bp2 = g_clone_bio(bp);
491 		if (bp2 != NULL)
492 			break;
493 		pause("gdstrat", hz / 10);
494 	}
495 	KASSERT(bp2 != NULL, ("XXX: ENOMEM in a bad place"));
496 	bp2->bio_done = g_dev_done;
497 	g_trace(G_T_BIO,
498 	    "g_dev_strategy(%p/%p) offset %jd length %jd data %p cmd %d",
499 	    bp, bp2, (intmax_t)bp->bio_offset, (intmax_t)bp2->bio_length,
500 	    bp2->bio_data, bp2->bio_cmd);
501 	g_io_request(bp2, cp);
502 	KASSERT(cp->acr || cp->acw,
503 	    ("g_dev_strategy raced with g_dev_close and lost"));
504 
505 }
506 
507 /*
508  * g_dev_orphan()
509  *
510  * Called from below when the provider orphaned us.
511  * - Clear any dump settings.
512  * - Destroy the struct cdev to prevent any more request from coming in.  The
513  *   provider is already marked with an error, so anything which comes in
514  *   in the interrim will be returned immediately.
515  * - Wait for any outstanding I/O to finish.
516  * - Set our access counts to zero, whatever they were.
517  * - Detach and self-destruct.
518  */
519 
520 static void
521 g_dev_orphan(struct g_consumer *cp)
522 {
523 	struct g_geom *gp;
524 	struct cdev *dev;
525 
526 	g_topology_assert();
527 	gp = cp->geom;
528 	dev = gp->softc;
529 	g_trace(G_T_TOPOLOGY, "g_dev_orphan(%p(%s))", cp, gp->name);
530 
531 	/* Reset any dump-area set on this device */
532 	if (dev->si_flags & SI_DUMPDEV)
533 		set_dumper(NULL);
534 
535 	/* Destroy the struct cdev *so we get no more requests */
536 	destroy_dev(dev);
537 
538 	/* Wait for the cows to come home */
539 	while (cp->nstart != cp->nend)
540 		pause("gdevorphan", hz / 10);
541 
542 	if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
543 		g_access(cp, -cp->acr, -cp->acw, -cp->ace);
544 
545 	g_detach(cp);
546 	g_destroy_consumer(cp);
547 	g_destroy_geom(gp);
548 }
549 
550 DECLARE_GEOM_CLASS(g_dev_class, g_dev);
551