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