xref: /freebsd/sys/geom/geom_disk.c (revision 5608fd23c27fa1e8ee595d7b678cbfd35d657fbe)
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 "opt_geom.h"
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/sysctl.h>
45 #include <sys/bio.h>
46 #include <sys/ctype.h>
47 #include <sys/fcntl.h>
48 #include <sys/malloc.h>
49 #include <sys/sbuf.h>
50 #include <sys/devicestat.h>
51 #include <machine/md_var.h>
52 
53 #include <sys/lock.h>
54 #include <sys/mutex.h>
55 #include <geom/geom.h>
56 #include <geom/geom_disk.h>
57 #include <geom/geom_int.h>
58 
59 #include <dev/led/led.h>
60 
61 struct g_disk_softc {
62 	struct mtx		 done_mtx;
63 	struct disk		*dp;
64 	struct sysctl_ctx_list	sysctl_ctx;
65 	struct sysctl_oid	*sysctl_tree;
66 	char			led[64];
67 	uint32_t		state;
68 	struct mtx		 start_mtx;
69 };
70 
71 static g_access_t g_disk_access;
72 static g_start_t g_disk_start;
73 static g_ioctl_t g_disk_ioctl;
74 static g_dumpconf_t g_disk_dumpconf;
75 static g_provgone_t g_disk_providergone;
76 
77 static struct g_class g_disk_class = {
78 	.name = G_DISK_CLASS_NAME,
79 	.version = G_VERSION,
80 	.start = g_disk_start,
81 	.access = g_disk_access,
82 	.ioctl = g_disk_ioctl,
83 	.providergone = g_disk_providergone,
84 	.dumpconf = g_disk_dumpconf,
85 };
86 
87 SYSCTL_DECL(_kern_geom);
88 static SYSCTL_NODE(_kern_geom, OID_AUTO, disk, CTLFLAG_RW, 0,
89     "GEOM_DISK stuff");
90 
91 DECLARE_GEOM_CLASS(g_disk_class, g_disk);
92 
93 static int
94 g_disk_access(struct g_provider *pp, int r, int w, int e)
95 {
96 	struct disk *dp;
97 	struct g_disk_softc *sc;
98 	int error;
99 
100 	g_trace(G_T_ACCESS, "g_disk_access(%s, %d, %d, %d)",
101 	    pp->name, r, w, e);
102 	g_topology_assert();
103 	sc = pp->private;
104 	if (sc == NULL || (dp = sc->dp) == NULL || dp->d_destroyed) {
105 		/*
106 		 * Allow decreasing access count even if disk is not
107 		 * avaliable anymore.
108 		 */
109 		if (r <= 0 && w <= 0 && e <= 0)
110 			return (0);
111 		return (ENXIO);
112 	}
113 	r += pp->acr;
114 	w += pp->acw;
115 	e += pp->ace;
116 	error = 0;
117 	if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) {
118 		if (dp->d_open != NULL) {
119 			error = dp->d_open(dp);
120 			if (bootverbose && error != 0)
121 				printf("Opened disk %s -> %d\n",
122 				    pp->name, error);
123 			if (error != 0)
124 				return (error);
125 		}
126 		pp->mediasize = dp->d_mediasize;
127 		pp->sectorsize = dp->d_sectorsize;
128 		if (dp->d_maxsize == 0) {
129 			printf("WARNING: Disk drive %s%d has no d_maxsize\n",
130 			    dp->d_name, dp->d_unit);
131 			dp->d_maxsize = DFLTPHYS;
132 		}
133 		if (dp->d_delmaxsize == 0) {
134 			if (bootverbose && dp->d_flags & DISKFLAG_CANDELETE) {
135 				printf("WARNING: Disk drive %s%d has no "
136 				    "d_delmaxsize\n", dp->d_name, dp->d_unit);
137 			}
138 			dp->d_delmaxsize = dp->d_maxsize;
139 		}
140 		pp->stripeoffset = dp->d_stripeoffset;
141 		pp->stripesize = dp->d_stripesize;
142 		dp->d_flags |= DISKFLAG_OPEN;
143 	} else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) {
144 		if (dp->d_close != NULL) {
145 			error = dp->d_close(dp);
146 			if (error != 0)
147 				printf("Closed disk %s -> %d\n",
148 				    pp->name, error);
149 		}
150 		sc->state = G_STATE_ACTIVE;
151 		if (sc->led[0] != 0)
152 			led_set(sc->led, "0");
153 		dp->d_flags &= ~DISKFLAG_OPEN;
154 	}
155 	return (error);
156 }
157 
158 static void
159 g_disk_kerneldump(struct bio *bp, struct disk *dp)
160 {
161 	struct g_kerneldump *gkd;
162 	struct g_geom *gp;
163 
164 	gkd = (struct g_kerneldump*)bp->bio_data;
165 	gp = bp->bio_to->geom;
166 	g_trace(G_T_TOPOLOGY, "g_disk_kerneldump(%s, %jd, %jd)",
167 		gp->name, (intmax_t)gkd->offset, (intmax_t)gkd->length);
168 	if (dp->d_dump == NULL) {
169 		g_io_deliver(bp, ENODEV);
170 		return;
171 	}
172 	gkd->di.dumper = dp->d_dump;
173 	gkd->di.priv = dp;
174 	gkd->di.blocksize = dp->d_sectorsize;
175 	gkd->di.maxiosize = dp->d_maxsize;
176 	gkd->di.mediaoffset = gkd->offset;
177 	if ((gkd->offset + gkd->length) > dp->d_mediasize)
178 		gkd->length = dp->d_mediasize - gkd->offset;
179 	gkd->di.mediasize = gkd->length;
180 	g_io_deliver(bp, 0);
181 }
182 
183 static void
184 g_disk_setstate(struct bio *bp, struct g_disk_softc *sc)
185 {
186 	const char *cmd;
187 
188 	memcpy(&sc->state, bp->bio_data, sizeof(sc->state));
189 	if (sc->led[0] != 0) {
190 		switch (sc->state) {
191 		case G_STATE_FAILED:
192 			cmd = "1";
193 			break;
194 		case G_STATE_REBUILD:
195 			cmd = "f5";
196 			break;
197 		case G_STATE_RESYNC:
198 			cmd = "f1";
199 			break;
200 		default:
201 			cmd = "0";
202 			break;
203 		}
204 		led_set(sc->led, cmd);
205 	}
206 	g_io_deliver(bp, 0);
207 }
208 
209 static void
210 g_disk_done(struct bio *bp)
211 {
212 	struct bintime now;
213 	struct bio *bp2;
214 	struct g_disk_softc *sc;
215 
216 	/* See "notes" for why we need a mutex here */
217 	/* XXX: will witness accept a mix of Giant/unGiant drivers here ? */
218 	bp2 = bp->bio_parent;
219 	sc = bp2->bio_to->private;
220 	bp->bio_completed = bp->bio_length - bp->bio_resid;
221 	binuptime(&now);
222 	mtx_lock(&sc->done_mtx);
223 	if (bp2->bio_error == 0)
224 		bp2->bio_error = bp->bio_error;
225 	bp2->bio_completed += bp->bio_completed;
226 	if ((bp->bio_cmd & (BIO_READ|BIO_WRITE|BIO_DELETE|BIO_FLUSH)) != 0)
227 		devstat_end_transaction_bio_bt(sc->dp->d_devstat, bp, &now);
228 	bp2->bio_inbed++;
229 	if (bp2->bio_children == bp2->bio_inbed) {
230 		mtx_unlock(&sc->done_mtx);
231 		bp2->bio_resid = bp2->bio_bcount - bp2->bio_completed;
232 		g_io_deliver(bp2, bp2->bio_error);
233 	} else
234 		mtx_unlock(&sc->done_mtx);
235 	g_destroy_bio(bp);
236 }
237 
238 static void
239 g_disk_done_single(struct bio *bp)
240 {
241 	struct bintime now;
242 	struct g_disk_softc *sc;
243 
244 	bp->bio_completed = bp->bio_length - bp->bio_resid;
245 	bp->bio_done = (void *)bp->bio_to;
246 	bp->bio_to = LIST_FIRST(&bp->bio_disk->d_geom->provider);
247 	if ((bp->bio_cmd & (BIO_READ|BIO_WRITE|BIO_DELETE|BIO_FLUSH)) != 0) {
248 		binuptime(&now);
249 		sc = bp->bio_to->private;
250 		mtx_lock(&sc->done_mtx);
251 		devstat_end_transaction_bio_bt(sc->dp->d_devstat, bp, &now);
252 		mtx_unlock(&sc->done_mtx);
253 	}
254 	g_io_deliver(bp, bp->bio_error);
255 }
256 
257 static int
258 g_disk_ioctl(struct g_provider *pp, u_long cmd, void * data, int fflag, struct thread *td)
259 {
260 	struct disk *dp;
261 	struct g_disk_softc *sc;
262 	int error;
263 
264 	sc = pp->private;
265 	dp = sc->dp;
266 
267 	if (dp->d_ioctl == NULL)
268 		return (ENOIOCTL);
269 	error = dp->d_ioctl(dp, cmd, data, fflag, td);
270 	return (error);
271 }
272 
273 static void
274 g_disk_start(struct bio *bp)
275 {
276 	struct bio *bp2, *bp3;
277 	struct disk *dp;
278 	struct g_disk_softc *sc;
279 	int error;
280 	off_t d_maxsize, off;
281 
282 	sc = bp->bio_to->private;
283 	if (sc == NULL || (dp = sc->dp) == NULL || dp->d_destroyed) {
284 		g_io_deliver(bp, ENXIO);
285 		return;
286 	}
287 	error = EJUSTRETURN;
288 	switch(bp->bio_cmd) {
289 	case BIO_DELETE:
290 		if (!(dp->d_flags & DISKFLAG_CANDELETE)) {
291 			error = EOPNOTSUPP;
292 			break;
293 		}
294 		/* fall-through */
295 	case BIO_READ:
296 	case BIO_WRITE:
297 		d_maxsize = (bp->bio_cmd == BIO_DELETE) ?
298 		    dp->d_delmaxsize : dp->d_maxsize;
299 		if (bp->bio_length <= d_maxsize) {
300 			bp->bio_disk = dp;
301 			bp->bio_to = (void *)bp->bio_done;
302 			bp->bio_done = g_disk_done_single;
303 			bp->bio_pblkno = bp->bio_offset / dp->d_sectorsize;
304 			bp->bio_bcount = bp->bio_length;
305 			mtx_lock(&sc->start_mtx);
306 			devstat_start_transaction_bio(dp->d_devstat, bp);
307 			mtx_unlock(&sc->start_mtx);
308 			dp->d_strategy(bp);
309 			break;
310 		}
311 		off = 0;
312 		bp3 = NULL;
313 		bp2 = g_clone_bio(bp);
314 		if (bp2 == NULL) {
315 			error = ENOMEM;
316 			break;
317 		}
318 		do {
319 			bp2->bio_offset += off;
320 			bp2->bio_length -= off;
321 			if ((bp->bio_flags & BIO_UNMAPPED) == 0) {
322 				bp2->bio_data += off;
323 			} else {
324 				KASSERT((dp->d_flags & DISKFLAG_UNMAPPED_BIO)
325 				    != 0,
326 				    ("unmapped bio not supported by disk %s",
327 				    dp->d_name));
328 				bp2->bio_ma += off / PAGE_SIZE;
329 				bp2->bio_ma_offset += off;
330 				bp2->bio_ma_offset %= PAGE_SIZE;
331 				bp2->bio_ma_n -= off / PAGE_SIZE;
332 			}
333 			if (bp2->bio_length > d_maxsize) {
334 				/*
335 				 * XXX: If we have a stripesize we should really
336 				 * use it here. Care should be taken in the delete
337 				 * case if this is done as deletes can be very
338 				 * sensitive to size given how they are processed.
339 				 */
340 				bp2->bio_length = d_maxsize;
341 				if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
342 					bp2->bio_ma_n = howmany(
343 					    bp2->bio_ma_offset +
344 					    bp2->bio_length, PAGE_SIZE);
345 				}
346 				off += d_maxsize;
347 				/*
348 				 * To avoid a race, we need to grab the next bio
349 				 * before we schedule this one.  See "notes".
350 				 */
351 				bp3 = g_clone_bio(bp);
352 				if (bp3 == NULL)
353 					bp->bio_error = ENOMEM;
354 			}
355 			bp2->bio_done = g_disk_done;
356 			bp2->bio_pblkno = bp2->bio_offset / dp->d_sectorsize;
357 			bp2->bio_bcount = bp2->bio_length;
358 			bp2->bio_disk = dp;
359 			mtx_lock(&sc->start_mtx);
360 			devstat_start_transaction_bio(dp->d_devstat, bp2);
361 			mtx_unlock(&sc->start_mtx);
362 			dp->d_strategy(bp2);
363 			bp2 = bp3;
364 			bp3 = NULL;
365 		} while (bp2 != NULL);
366 		break;
367 	case BIO_GETATTR:
368 		/* Give the driver a chance to override */
369 		if (dp->d_getattr != NULL) {
370 			if (bp->bio_disk == NULL)
371 				bp->bio_disk = dp;
372 			error = dp->d_getattr(bp);
373 			if (error != -1)
374 				break;
375 			error = EJUSTRETURN;
376 		}
377 		if (g_handleattr_int(bp, "GEOM::candelete",
378 		    (dp->d_flags & DISKFLAG_CANDELETE) != 0))
379 			break;
380 		else if (g_handleattr_int(bp, "GEOM::fwsectors",
381 		    dp->d_fwsectors))
382 			break;
383 		else if (g_handleattr_int(bp, "GEOM::fwheads", dp->d_fwheads))
384 			break;
385 		else if (g_handleattr_off_t(bp, "GEOM::frontstuff", 0))
386 			break;
387 		else if (g_handleattr_str(bp, "GEOM::ident", dp->d_ident))
388 			break;
389 		else if (g_handleattr_uint16_t(bp, "GEOM::hba_vendor",
390 		    dp->d_hba_vendor))
391 			break;
392 		else if (g_handleattr_uint16_t(bp, "GEOM::hba_device",
393 		    dp->d_hba_device))
394 			break;
395 		else if (g_handleattr_uint16_t(bp, "GEOM::hba_subvendor",
396 		    dp->d_hba_subvendor))
397 			break;
398 		else if (g_handleattr_uint16_t(bp, "GEOM::hba_subdevice",
399 		    dp->d_hba_subdevice))
400 			break;
401 		else if (!strcmp(bp->bio_attribute, "GEOM::kerneldump"))
402 			g_disk_kerneldump(bp, dp);
403 		else if (!strcmp(bp->bio_attribute, "GEOM::setstate"))
404 			g_disk_setstate(bp, sc);
405 		else if (g_handleattr_uint16_t(bp, "GEOM::rotation_rate",
406 		    dp->d_rotation_rate))
407 			break;
408 		else
409 			error = ENOIOCTL;
410 		break;
411 	case BIO_FLUSH:
412 		g_trace(G_T_BIO, "g_disk_flushcache(%s)",
413 		    bp->bio_to->name);
414 		if (!(dp->d_flags & DISKFLAG_CANFLUSHCACHE)) {
415 			error = EOPNOTSUPP;
416 			break;
417 		}
418 		bp->bio_disk = dp;
419 		bp->bio_to = (void *)bp->bio_done;
420 		bp->bio_done = g_disk_done_single;
421 		mtx_lock(&sc->start_mtx);
422 		devstat_start_transaction_bio(dp->d_devstat, bp);
423 		mtx_unlock(&sc->start_mtx);
424 		dp->d_strategy(bp);
425 		break;
426 	default:
427 		error = EOPNOTSUPP;
428 		break;
429 	}
430 	if (error != EJUSTRETURN)
431 		g_io_deliver(bp, error);
432 	return;
433 }
434 
435 static void
436 g_disk_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp)
437 {
438 	struct bio *bp;
439 	struct disk *dp;
440 	struct g_disk_softc *sc;
441 	char *buf;
442 	int res = 0;
443 
444 	sc = gp->softc;
445 	if (sc == NULL || (dp = sc->dp) == NULL)
446 		return;
447 	if (indent == NULL) {
448 		sbuf_printf(sb, " hd %u", dp->d_fwheads);
449 		sbuf_printf(sb, " sc %u", dp->d_fwsectors);
450 		return;
451 	}
452 	if (pp != NULL) {
453 		sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n",
454 		    indent, dp->d_fwheads);
455 		sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n",
456 		    indent, dp->d_fwsectors);
457 		if (dp->d_getattr != NULL) {
458 			buf = g_malloc(DISK_IDENT_SIZE, M_WAITOK);
459 			bp = g_alloc_bio();
460 			bp->bio_disk = dp;
461 			bp->bio_attribute = "GEOM::ident";
462 			bp->bio_length = DISK_IDENT_SIZE;
463 			bp->bio_data = buf;
464 			res = dp->d_getattr(bp);
465 			sbuf_printf(sb, "%s<ident>", indent);
466 			g_conf_printf_escaped(sb, "%s",
467 			    res == 0 ? buf: dp->d_ident);
468 			sbuf_printf(sb, "</ident>\n");
469 			bp->bio_attribute = "GEOM::lunid";
470 			bp->bio_length = DISK_IDENT_SIZE;
471 			bp->bio_data = buf;
472 			if (dp->d_getattr(bp) == 0) {
473 				sbuf_printf(sb, "%s<lunid>", indent);
474 				g_conf_printf_escaped(sb, "%s", buf);
475 				sbuf_printf(sb, "</lunid>\n");
476 			}
477 			bp->bio_attribute = "GEOM::lunname";
478 			bp->bio_length = DISK_IDENT_SIZE;
479 			bp->bio_data = buf;
480 			if (dp->d_getattr(bp) == 0) {
481 				sbuf_printf(sb, "%s<lunname>", indent);
482 				g_conf_printf_escaped(sb, "%s", buf);
483 				sbuf_printf(sb, "</lunname>\n");
484 			}
485 			g_destroy_bio(bp);
486 			g_free(buf);
487 		} else {
488 			sbuf_printf(sb, "%s<ident>", indent);
489 			g_conf_printf_escaped(sb, "%s", dp->d_ident);
490 			sbuf_printf(sb, "</ident>\n");
491 		}
492 		sbuf_printf(sb, "%s<descr>", indent);
493 		g_conf_printf_escaped(sb, "%s", dp->d_descr);
494 		sbuf_printf(sb, "</descr>\n");
495 	}
496 }
497 
498 static void
499 g_disk_resize(void *ptr, int flag)
500 {
501 	struct disk *dp;
502 	struct g_geom *gp;
503 	struct g_provider *pp;
504 
505 	if (flag == EV_CANCEL)
506 		return;
507 	g_topology_assert();
508 
509 	dp = ptr;
510 	gp = dp->d_geom;
511 
512 	if (dp->d_destroyed || gp == NULL)
513 		return;
514 
515 	LIST_FOREACH(pp, &gp->provider, provider) {
516 		if (pp->sectorsize != 0 &&
517 		    pp->sectorsize != dp->d_sectorsize)
518 			g_wither_provider(pp, ENXIO);
519 		else
520 			g_resize_provider(pp, dp->d_mediasize);
521 	}
522 }
523 
524 static void
525 g_disk_create(void *arg, int flag)
526 {
527 	struct g_geom *gp;
528 	struct g_provider *pp;
529 	struct disk *dp;
530 	struct g_disk_softc *sc;
531 	char tmpstr[80];
532 
533 	if (flag == EV_CANCEL)
534 		return;
535 	g_topology_assert();
536 	dp = arg;
537 	sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
538 	mtx_init(&sc->start_mtx, "g_disk_start", NULL, MTX_DEF);
539 	mtx_init(&sc->done_mtx, "g_disk_done", NULL, MTX_DEF);
540 	sc->dp = dp;
541 	gp = g_new_geomf(&g_disk_class, "%s%d", dp->d_name, dp->d_unit);
542 	gp->softc = sc;
543 	pp = g_new_providerf(gp, "%s", gp->name);
544 	devstat_remove_entry(pp->stat);
545 	pp->stat = NULL;
546 	dp->d_devstat->id = pp;
547 	pp->mediasize = dp->d_mediasize;
548 	pp->sectorsize = dp->d_sectorsize;
549 	pp->stripeoffset = dp->d_stripeoffset;
550 	pp->stripesize = dp->d_stripesize;
551 	if ((dp->d_flags & DISKFLAG_UNMAPPED_BIO) != 0)
552 		pp->flags |= G_PF_ACCEPT_UNMAPPED;
553 	if ((dp->d_flags & DISKFLAG_DIRECT_COMPLETION) != 0)
554 		pp->flags |= G_PF_DIRECT_SEND;
555 	pp->flags |= G_PF_DIRECT_RECEIVE;
556 	if (bootverbose)
557 		printf("GEOM: new disk %s\n", gp->name);
558 	sysctl_ctx_init(&sc->sysctl_ctx);
559 	snprintf(tmpstr, sizeof(tmpstr), "GEOM disk %s", gp->name);
560 	sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
561 		SYSCTL_STATIC_CHILDREN(_kern_geom_disk), OID_AUTO, gp->name,
562 		CTLFLAG_RD, 0, tmpstr);
563 	if (sc->sysctl_tree != NULL) {
564 		SYSCTL_ADD_STRING(&sc->sysctl_ctx,
565 		    SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO, "led",
566 		    CTLFLAG_RWTUN, sc->led, sizeof(sc->led),
567 		    "LED name");
568 	}
569 	pp->private = sc;
570 	dp->d_geom = gp;
571 	g_error_provider(pp, 0);
572 }
573 
574 /*
575  * We get this callback after all of the consumers have gone away, and just
576  * before the provider is freed.  If the disk driver provided a d_gone
577  * callback, let them know that it is okay to free resources -- they won't
578  * be getting any more accesses from GEOM.
579  */
580 static void
581 g_disk_providergone(struct g_provider *pp)
582 {
583 	struct disk *dp;
584 	struct g_disk_softc *sc;
585 
586 	sc = (struct g_disk_softc *)pp->private;
587 	dp = sc->dp;
588 	if (dp != NULL && dp->d_gone != NULL)
589 		dp->d_gone(dp);
590 	if (sc->sysctl_tree != NULL) {
591 		sysctl_ctx_free(&sc->sysctl_ctx);
592 		sc->sysctl_tree = NULL;
593 	}
594 	if (sc->led[0] != 0) {
595 		led_set(sc->led, "0");
596 		sc->led[0] = 0;
597 	}
598 	pp->private = NULL;
599 	pp->geom->softc = NULL;
600 	mtx_destroy(&sc->done_mtx);
601 	mtx_destroy(&sc->start_mtx);
602 	g_free(sc);
603 }
604 
605 static void
606 g_disk_destroy(void *ptr, int flag)
607 {
608 	struct disk *dp;
609 	struct g_geom *gp;
610 	struct g_disk_softc *sc;
611 
612 	g_topology_assert();
613 	dp = ptr;
614 	gp = dp->d_geom;
615 	if (gp != NULL) {
616 		sc = gp->softc;
617 		if (sc != NULL)
618 			sc->dp = NULL;
619 		dp->d_geom = NULL;
620 		g_wither_geom(gp, ENXIO);
621 	}
622 	g_free(dp);
623 }
624 
625 /*
626  * We only allow printable characters in disk ident,
627  * the rest is converted to 'x<HH>'.
628  */
629 static void
630 g_disk_ident_adjust(char *ident, size_t size)
631 {
632 	char *p, tmp[4], newid[DISK_IDENT_SIZE];
633 
634 	newid[0] = '\0';
635 	for (p = ident; *p != '\0'; p++) {
636 		if (isprint(*p)) {
637 			tmp[0] = *p;
638 			tmp[1] = '\0';
639 		} else {
640 			snprintf(tmp, sizeof(tmp), "x%02hhx",
641 			    *(unsigned char *)p);
642 		}
643 		if (strlcat(newid, tmp, sizeof(newid)) >= sizeof(newid))
644 			break;
645 	}
646 	bzero(ident, size);
647 	strlcpy(ident, newid, size);
648 }
649 
650 struct disk *
651 disk_alloc(void)
652 {
653 
654 	return (g_malloc(sizeof(struct disk), M_WAITOK | M_ZERO));
655 }
656 
657 void
658 disk_create(struct disk *dp, int version)
659 {
660 
661 	if (version != DISK_VERSION) {
662 		printf("WARNING: Attempt to add disk %s%d %s",
663 		    dp->d_name, dp->d_unit,
664 		    " using incompatible ABI version of disk(9)\n");
665 		printf("WARNING: Ignoring disk %s%d\n",
666 		    dp->d_name, dp->d_unit);
667 		return;
668 	}
669 	if (dp->d_flags & DISKFLAG_RESERVED) {
670 		printf("WARNING: Attempt to add non-MPSAFE disk %s%d\n",
671 		    dp->d_name, dp->d_unit);
672 		printf("WARNING: Ignoring disk %s%d\n",
673 		    dp->d_name, dp->d_unit);
674 		return;
675 	}
676 	KASSERT(dp->d_strategy != NULL, ("disk_create need d_strategy"));
677 	KASSERT(dp->d_name != NULL, ("disk_create need d_name"));
678 	KASSERT(*dp->d_name != 0, ("disk_create need d_name"));
679 	KASSERT(strlen(dp->d_name) < SPECNAMELEN - 4, ("disk name too long"));
680 	if (dp->d_devstat == NULL)
681 		dp->d_devstat = devstat_new_entry(dp->d_name, dp->d_unit,
682 		    dp->d_sectorsize, DEVSTAT_ALL_SUPPORTED,
683 		    DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
684 	dp->d_geom = NULL;
685 	g_disk_ident_adjust(dp->d_ident, sizeof(dp->d_ident));
686 	g_post_event(g_disk_create, dp, M_WAITOK, dp, NULL);
687 }
688 
689 void
690 disk_destroy(struct disk *dp)
691 {
692 
693 	g_cancel_event(dp);
694 	dp->d_destroyed = 1;
695 	if (dp->d_devstat != NULL)
696 		devstat_remove_entry(dp->d_devstat);
697 	g_post_event(g_disk_destroy, dp, M_WAITOK, NULL);
698 }
699 
700 void
701 disk_gone(struct disk *dp)
702 {
703 	struct g_geom *gp;
704 	struct g_provider *pp;
705 
706 	gp = dp->d_geom;
707 	if (gp != NULL) {
708 		pp = LIST_FIRST(&gp->provider);
709 		if (pp != NULL) {
710 			KASSERT(LIST_NEXT(pp, provider) == NULL,
711 			    ("geom %p has more than one provider", gp));
712 			g_wither_provider(pp, ENXIO);
713 		}
714 	}
715 }
716 
717 void
718 disk_attr_changed(struct disk *dp, const char *attr, int flag)
719 {
720 	struct g_geom *gp;
721 	struct g_provider *pp;
722 
723 	gp = dp->d_geom;
724 	if (gp != NULL)
725 		LIST_FOREACH(pp, &gp->provider, provider)
726 			(void)g_attr_changed(pp, attr, flag);
727 }
728 
729 void
730 disk_media_changed(struct disk *dp, int flag)
731 {
732 	struct g_geom *gp;
733 	struct g_provider *pp;
734 
735 	gp = dp->d_geom;
736 	if (gp != NULL) {
737 		pp = LIST_FIRST(&gp->provider);
738 		if (pp != NULL) {
739 			KASSERT(LIST_NEXT(pp, provider) == NULL,
740 			    ("geom %p has more than one provider", gp));
741 			g_media_changed(pp, flag);
742 		}
743 	}
744 }
745 
746 void
747 disk_media_gone(struct disk *dp, int flag)
748 {
749 	struct g_geom *gp;
750 	struct g_provider *pp;
751 
752 	gp = dp->d_geom;
753 	if (gp != NULL) {
754 		pp = LIST_FIRST(&gp->provider);
755 		if (pp != NULL) {
756 			KASSERT(LIST_NEXT(pp, provider) == NULL,
757 			    ("geom %p has more than one provider", gp));
758 			g_media_gone(pp, flag);
759 		}
760 	}
761 }
762 
763 int
764 disk_resize(struct disk *dp, int flag)
765 {
766 
767 	if (dp->d_destroyed || dp->d_geom == NULL)
768 		return (0);
769 
770 	return (g_post_event(g_disk_resize, dp, flag, NULL));
771 }
772 
773 static void
774 g_kern_disks(void *p, int flag __unused)
775 {
776 	struct sbuf *sb;
777 	struct g_geom *gp;
778 	char *sp;
779 
780 	sb = p;
781 	sp = "";
782 	g_topology_assert();
783 	LIST_FOREACH(gp, &g_disk_class.geom, geom) {
784 		sbuf_printf(sb, "%s%s", sp, gp->name);
785 		sp = " ";
786 	}
787 	sbuf_finish(sb);
788 }
789 
790 static int
791 sysctl_disks(SYSCTL_HANDLER_ARGS)
792 {
793 	int error;
794 	struct sbuf *sb;
795 
796 	sb = sbuf_new_auto();
797 	g_waitfor_event(g_kern_disks, sb, M_WAITOK, NULL);
798 	error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
799 	sbuf_delete(sb);
800 	return error;
801 }
802 
803 SYSCTL_PROC(_kern, OID_AUTO, disks,
804     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
805     sysctl_disks, "A", "names of available disks");
806