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