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