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