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