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