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