2 * Copyright (c) 2002 Poul-Henning Kamp
3 * Copyright (c) 2002 Networks Associates Technology, Inc.
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.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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
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
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/sysctl.h>
47 #include <sys/ctype.h>
48 #include <sys/fcntl.h>
49 #include <sys/malloc.h>
51 #include <sys/devicestat.h>
52 #include <machine/md_var.h>
55 #include <sys/mutex.h>
56 #include <geom/geom.h>
57 #include <geom/geom_disk.h>
58 #include <geom/geom_int.h>
60 #include <dev/led/led.h>
62 #include <machine/bus.h>
67 struct sysctl_ctx_list sysctl_ctx;
68 struct sysctl_oid *sysctl_tree;
74 static g_access_t g_disk_access;
75 static g_start_t g_disk_start;
76 static g_ioctl_t g_disk_ioctl;
77 static g_dumpconf_t g_disk_dumpconf;
78 static g_provgone_t g_disk_providergone;
80 static struct g_class g_disk_class = {
81 .name = G_DISK_CLASS_NAME,
83 .start = g_disk_start,
84 .access = g_disk_access,
85 .ioctl = g_disk_ioctl,
86 .providergone = g_disk_providergone,
87 .dumpconf = g_disk_dumpconf,
90 SYSCTL_DECL(_kern_geom);
91 static SYSCTL_NODE(_kern_geom, OID_AUTO, disk, CTLFLAG_RW, 0,
94 DECLARE_GEOM_CLASS(g_disk_class, g_disk);
97 g_disk_access(struct g_provider *pp, int r, int w, int e)
100 struct g_disk_softc *sc;
103 g_trace(G_T_ACCESS, "g_disk_access(%s, %d, %d, %d)",
107 if (sc == NULL || (dp = sc->dp) == NULL || dp->d_destroyed) {
109 * Allow decreasing access count even if disk is not
112 if (r <= 0 && w <= 0 && e <= 0)
120 if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) {
121 if (dp->d_open != NULL) {
122 error = dp->d_open(dp);
123 if (bootverbose && error != 0)
124 printf("Opened disk %s -> %d\n",
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;
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);
140 dp->d_delmaxsize = dp->d_maxsize;
142 pp->stripeoffset = dp->d_stripeoffset;
143 pp->stripesize = dp->d_stripesize;
144 dp->d_flags |= DISKFLAG_OPEN;
146 * Do not invoke resize event when initial size was zero.
147 * Some disks report its size only after first opening.
149 if (pp->mediasize == 0)
150 pp->mediasize = dp->d_mediasize;
152 g_resize_provider(pp, dp->d_mediasize);
153 } else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) {
154 if (dp->d_close != NULL) {
155 error = dp->d_close(dp);
157 printf("Closed disk %s -> %d\n",
160 sc->state = G_STATE_ACTIVE;
162 led_set(sc->led, "0");
163 dp->d_flags &= ~DISKFLAG_OPEN;
169 g_disk_kerneldump(struct bio *bp, struct disk *dp)
171 struct g_kerneldump *gkd;
174 gkd = (struct g_kerneldump*)bp->bio_data;
175 gp = bp->bio_to->geom;
176 g_trace(G_T_TOPOLOGY, "g_disk_kerneldump(%s, %jd, %jd)",
177 gp->name, (intmax_t)gkd->offset, (intmax_t)gkd->length);
178 if (dp->d_dump == NULL) {
179 g_io_deliver(bp, ENODEV);
182 gkd->di.dumper = dp->d_dump;
184 gkd->di.blocksize = dp->d_sectorsize;
185 gkd->di.maxiosize = dp->d_maxsize;
186 gkd->di.mediaoffset = gkd->offset;
187 if ((gkd->offset + gkd->length) > dp->d_mediasize)
188 gkd->length = dp->d_mediasize - gkd->offset;
189 gkd->di.mediasize = gkd->length;
194 g_disk_setstate(struct bio *bp, struct g_disk_softc *sc)
198 memcpy(&sc->state, bp->bio_data, sizeof(sc->state));
199 if (sc->led[0] != 0) {
204 case G_STATE_REBUILD:
214 led_set(sc->led, cmd);
220 g_disk_done(struct bio *bp)
224 struct g_disk_softc *sc;
226 /* See "notes" for why we need a mutex here */
227 /* XXX: will witness accept a mix of Giant/unGiant drivers here ? */
228 bp2 = bp->bio_parent;
229 sc = bp2->bio_to->private;
230 bp->bio_completed = bp->bio_length - bp->bio_resid;
232 mtx_lock(&sc->done_mtx);
233 if (bp2->bio_error == 0)
234 bp2->bio_error = bp->bio_error;
235 bp2->bio_completed += bp->bio_completed;
237 switch (bp->bio_cmd) {
239 bcopy(&bp->bio_zone, &bp2->bio_zone, sizeof(bp->bio_zone));
245 devstat_end_transaction_bio_bt(sc->dp->d_devstat, bp, &now);
251 if (bp2->bio_children == bp2->bio_inbed) {
252 mtx_unlock(&sc->done_mtx);
253 bp2->bio_resid = bp2->bio_bcount - bp2->bio_completed;
254 g_io_deliver(bp2, bp2->bio_error);
256 mtx_unlock(&sc->done_mtx);
261 g_disk_ioctl(struct g_provider *pp, u_long cmd, void * data, int fflag, struct thread *td)
264 struct g_disk_softc *sc;
270 if (dp->d_ioctl == NULL)
272 error = dp->d_ioctl(dp, cmd, data, fflag, td);
277 g_disk_maxsize(struct disk *dp, struct bio *bp)
279 if (bp->bio_cmd == BIO_DELETE)
280 return (dp->d_delmaxsize);
281 return (dp->d_maxsize);
285 g_disk_maxsegs(struct disk *dp, struct bio *bp)
287 return ((g_disk_maxsize(dp, bp) / PAGE_SIZE) + 1);
291 g_disk_advance(struct disk *dp, struct bio *bp, off_t off)
294 bp->bio_offset += off;
295 bp->bio_length -= off;
297 if ((bp->bio_flags & BIO_VLIST) != 0) {
298 bus_dma_segment_t *seg, *end;
300 seg = (bus_dma_segment_t *)bp->bio_data;
301 end = (bus_dma_segment_t *)bp->bio_data + bp->bio_ma_n;
302 off += bp->bio_ma_offset;
303 while (off >= seg->ds_len) {
304 KASSERT((seg != end),
305 ("vlist request runs off the end"));
309 bp->bio_ma_offset = off;
310 bp->bio_ma_n = end - seg;
311 bp->bio_data = (void *)seg;
312 } else if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
313 bp->bio_ma += off / PAGE_SIZE;
314 bp->bio_ma_offset += off;
315 bp->bio_ma_offset %= PAGE_SIZE;
316 bp->bio_ma_n -= off / PAGE_SIZE;
323 g_disk_seg_limit(bus_dma_segment_t *seg, off_t *poffset,
324 off_t *plength, int *ppages)
326 uintptr_t seg_page_base;
327 uintptr_t seg_page_end;
335 if (length > seg->ds_len - offset)
336 length = seg->ds_len - offset;
338 seg_page_base = trunc_page(seg->ds_addr + offset);
339 seg_page_end = round_page(seg->ds_addr + offset + length);
340 seg_pages = (seg_page_end - seg_page_base) >> PAGE_SHIFT;
342 if (seg_pages > *ppages) {
344 length = (seg_page_base + (seg_pages << PAGE_SHIFT)) -
345 (seg->ds_addr + offset);
350 *ppages -= seg_pages;
354 g_disk_vlist_limit(struct disk *dp, struct bio *bp, bus_dma_segment_t **pendseg)
356 bus_dma_segment_t *seg, *end;
361 seg = (bus_dma_segment_t *)bp->bio_data;
362 end = (bus_dma_segment_t *)bp->bio_data + bp->bio_ma_n;
363 residual = bp->bio_length;
364 offset = bp->bio_ma_offset;
365 pages = g_disk_maxsegs(dp, bp);
366 while (residual != 0 && pages != 0) {
367 KASSERT((seg != end),
368 ("vlist limit runs off the end"));
369 g_disk_seg_limit(seg, &offset, &residual, &pages);
378 g_disk_limit(struct disk *dp, struct bio *bp)
380 bool limited = false;
383 maxsz = g_disk_maxsize(dp, bp);
386 * XXX: If we have a stripesize we should really use it here.
387 * Care should be taken in the delete case if this is done
388 * as deletes can be very sensitive to size given how they
391 if (bp->bio_length > maxsz) {
392 bp->bio_length = maxsz;
396 if ((bp->bio_flags & BIO_VLIST) != 0) {
397 bus_dma_segment_t *firstseg, *endseg;
400 firstseg = (bus_dma_segment_t*)bp->bio_data;
401 residual = g_disk_vlist_limit(dp, bp, &endseg);
403 bp->bio_ma_n = endseg - firstseg;
404 bp->bio_length -= residual;
407 } else if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
409 howmany(bp->bio_ma_offset + bp->bio_length, PAGE_SIZE);
416 g_disk_start(struct bio *bp)
418 struct bio *bp2, *bp3;
420 struct g_disk_softc *sc;
424 sc = bp->bio_to->private;
425 if (sc == NULL || (dp = sc->dp) == NULL || dp->d_destroyed) {
426 g_io_deliver(bp, ENXIO);
430 switch(bp->bio_cmd) {
432 if (!(dp->d_flags & DISKFLAG_CANDELETE)) {
439 KASSERT((dp->d_flags & DISKFLAG_UNMAPPED_BIO) != 0 ||
440 (bp->bio_flags & BIO_UNMAPPED) == 0,
441 ("unmapped bio not supported by disk %s", dp->d_name));
444 bp2 = g_clone_bio(bp);
450 if (g_disk_limit(dp, bp2)) {
451 off += bp2->bio_length;
454 * To avoid a race, we need to grab the next bio
455 * before we schedule this one. See "notes".
457 bp3 = g_clone_bio(bp);
459 bp->bio_error = ENOMEM;
461 bp2->bio_done = g_disk_done;
462 bp2->bio_pblkno = bp2->bio_offset / dp->d_sectorsize;
463 bp2->bio_bcount = bp2->bio_length;
465 mtx_lock(&sc->start_mtx);
466 devstat_start_transaction_bio(dp->d_devstat, bp2);
467 mtx_unlock(&sc->start_mtx);
475 g_disk_advance(dp, bp2, off);
479 /* Give the driver a chance to override */
480 if (dp->d_getattr != NULL) {
481 if (bp->bio_disk == NULL)
483 error = dp->d_getattr(bp);
488 if (g_handleattr_int(bp, "GEOM::candelete",
489 (dp->d_flags & DISKFLAG_CANDELETE) != 0))
491 else if (g_handleattr_int(bp, "GEOM::fwsectors",
494 else if (g_handleattr_int(bp, "GEOM::fwheads", dp->d_fwheads))
496 else if (g_handleattr_off_t(bp, "GEOM::frontstuff", 0))
498 else if (g_handleattr_str(bp, "GEOM::ident", dp->d_ident))
500 else if (g_handleattr_uint16_t(bp, "GEOM::hba_vendor",
503 else if (g_handleattr_uint16_t(bp, "GEOM::hba_device",
506 else if (g_handleattr_uint16_t(bp, "GEOM::hba_subvendor",
507 dp->d_hba_subvendor))
509 else if (g_handleattr_uint16_t(bp, "GEOM::hba_subdevice",
510 dp->d_hba_subdevice))
512 else if (!strcmp(bp->bio_attribute, "GEOM::kerneldump"))
513 g_disk_kerneldump(bp, dp);
514 else if (!strcmp(bp->bio_attribute, "GEOM::setstate"))
515 g_disk_setstate(bp, sc);
516 else if (g_handleattr_uint16_t(bp, "GEOM::rotation_rate",
517 dp->d_rotation_rate))
523 g_trace(G_T_BIO, "g_disk_flushcache(%s)",
525 if (!(dp->d_flags & DISKFLAG_CANFLUSHCACHE)) {
531 if (bp->bio_cmd == BIO_ZONE) {
532 if (!(dp->d_flags & DISKFLAG_CANZONE)) {
536 g_trace(G_T_BIO, "g_disk_zone(%s)",
539 bp2 = g_clone_bio(bp);
541 g_io_deliver(bp, ENOMEM);
544 bp2->bio_done = g_disk_done;
546 mtx_lock(&sc->start_mtx);
547 devstat_start_transaction_bio(dp->d_devstat, bp2);
548 mtx_unlock(&sc->start_mtx);
555 if (error != EJUSTRETURN)
556 g_io_deliver(bp, error);
561 g_disk_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp)
565 struct g_disk_softc *sc;
570 if (sc == NULL || (dp = sc->dp) == NULL)
572 if (indent == NULL) {
573 sbuf_printf(sb, " hd %u", dp->d_fwheads);
574 sbuf_printf(sb, " sc %u", dp->d_fwsectors);
578 sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n",
579 indent, dp->d_fwheads);
580 sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n",
581 indent, dp->d_fwsectors);
584 * "rotationrate" is a little complicated, because the value
585 * returned by the drive might not be the RPM; 0 and 1 are
586 * special cases, and there's also a valid range.
588 sbuf_printf(sb, "%s<rotationrate>", indent);
589 if (dp->d_rotation_rate == 0) /* Old drives don't */
590 sbuf_printf(sb, "unknown"); /* report RPM. */
591 else if (dp->d_rotation_rate == 1) /* Since 0 is used */
592 sbuf_printf(sb, "0"); /* above, SSDs use 1. */
593 else if ((dp->d_rotation_rate >= 0x041) &&
594 (dp->d_rotation_rate <= 0xfffe))
595 sbuf_printf(sb, "%u", dp->d_rotation_rate);
597 sbuf_printf(sb, "invalid");
598 sbuf_printf(sb, "</rotationrate>\n");
599 if (dp->d_getattr != NULL) {
600 buf = g_malloc(DISK_IDENT_SIZE, M_WAITOK);
603 bp->bio_attribute = "GEOM::ident";
604 bp->bio_length = DISK_IDENT_SIZE;
606 res = dp->d_getattr(bp);
607 sbuf_printf(sb, "%s<ident>", indent);
608 g_conf_printf_escaped(sb, "%s",
609 res == 0 ? buf: dp->d_ident);
610 sbuf_printf(sb, "</ident>\n");
611 bp->bio_attribute = "GEOM::lunid";
612 bp->bio_length = DISK_IDENT_SIZE;
614 if (dp->d_getattr(bp) == 0) {
615 sbuf_printf(sb, "%s<lunid>", indent);
616 g_conf_printf_escaped(sb, "%s", buf);
617 sbuf_printf(sb, "</lunid>\n");
619 bp->bio_attribute = "GEOM::lunname";
620 bp->bio_length = DISK_IDENT_SIZE;
622 if (dp->d_getattr(bp) == 0) {
623 sbuf_printf(sb, "%s<lunname>", indent);
624 g_conf_printf_escaped(sb, "%s", buf);
625 sbuf_printf(sb, "</lunname>\n");
630 sbuf_printf(sb, "%s<ident>", indent);
631 g_conf_printf_escaped(sb, "%s", dp->d_ident);
632 sbuf_printf(sb, "</ident>\n");
634 sbuf_printf(sb, "%s<descr>", indent);
635 g_conf_printf_escaped(sb, "%s", dp->d_descr);
636 sbuf_printf(sb, "</descr>\n");
641 g_disk_resize(void *ptr, int flag)
645 struct g_provider *pp;
647 if (flag == EV_CANCEL)
654 if (dp->d_destroyed || gp == NULL)
657 LIST_FOREACH(pp, &gp->provider, provider) {
658 if (pp->sectorsize != 0 &&
659 pp->sectorsize != dp->d_sectorsize)
660 g_wither_provider(pp, ENXIO);
662 g_resize_provider(pp, dp->d_mediasize);
667 g_disk_create(void *arg, int flag)
670 struct g_provider *pp;
672 struct g_disk_softc *sc;
675 if (flag == EV_CANCEL)
680 mtx_pool_lock(mtxpool_sleep, dp);
681 dp->d_init_level = DISK_INIT_START;
684 * If the disk has already gone away, we can just stop here and
685 * call the user's callback to tell him we've cleaned things up.
687 if (dp->d_goneflag != 0) {
688 mtx_pool_unlock(mtxpool_sleep, dp);
689 if (dp->d_gone != NULL)
693 mtx_pool_unlock(mtxpool_sleep, dp);
695 sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
696 mtx_init(&sc->start_mtx, "g_disk_start", NULL, MTX_DEF);
697 mtx_init(&sc->done_mtx, "g_disk_done", NULL, MTX_DEF);
699 gp = g_new_geomf(&g_disk_class, "%s%d", dp->d_name, dp->d_unit);
701 pp = g_new_providerf(gp, "%s", gp->name);
702 devstat_remove_entry(pp->stat);
704 dp->d_devstat->id = pp;
705 pp->mediasize = dp->d_mediasize;
706 pp->sectorsize = dp->d_sectorsize;
707 pp->stripeoffset = dp->d_stripeoffset;
708 pp->stripesize = dp->d_stripesize;
709 if ((dp->d_flags & DISKFLAG_UNMAPPED_BIO) != 0)
710 pp->flags |= G_PF_ACCEPT_UNMAPPED;
711 if ((dp->d_flags & DISKFLAG_DIRECT_COMPLETION) != 0)
712 pp->flags |= G_PF_DIRECT_SEND;
713 pp->flags |= G_PF_DIRECT_RECEIVE;
715 printf("GEOM: new disk %s\n", gp->name);
716 sysctl_ctx_init(&sc->sysctl_ctx);
717 snprintf(tmpstr, sizeof(tmpstr), "GEOM disk %s", gp->name);
718 sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
719 SYSCTL_STATIC_CHILDREN(_kern_geom_disk), OID_AUTO, gp->name,
720 CTLFLAG_RD, 0, tmpstr);
721 if (sc->sysctl_tree != NULL) {
722 SYSCTL_ADD_STRING(&sc->sysctl_ctx,
723 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO, "led",
724 CTLFLAG_RWTUN, sc->led, sizeof(sc->led),
729 g_error_provider(pp, 0);
731 mtx_pool_lock(mtxpool_sleep, dp);
732 dp->d_init_level = DISK_INIT_DONE;
735 * If the disk has gone away at this stage, start the withering
738 if (dp->d_goneflag != 0) {
739 mtx_pool_unlock(mtxpool_sleep, dp);
740 g_wither_provider(pp, ENXIO);
743 mtx_pool_unlock(mtxpool_sleep, dp);
748 * We get this callback after all of the consumers have gone away, and just
749 * before the provider is freed. If the disk driver provided a d_gone
750 * callback, let them know that it is okay to free resources -- they won't
751 * be getting any more accesses from GEOM.
754 g_disk_providergone(struct g_provider *pp)
757 struct g_disk_softc *sc;
759 sc = (struct g_disk_softc *)pp->private;
761 if (dp != NULL && dp->d_gone != NULL)
763 if (sc->sysctl_tree != NULL) {
764 sysctl_ctx_free(&sc->sysctl_ctx);
765 sc->sysctl_tree = NULL;
767 if (sc->led[0] != 0) {
768 led_set(sc->led, "0");
772 pp->geom->softc = NULL;
773 mtx_destroy(&sc->done_mtx);
774 mtx_destroy(&sc->start_mtx);
779 g_disk_destroy(void *ptr, int flag)
783 struct g_disk_softc *sc;
793 g_wither_geom(gp, ENXIO);
800 * We only allow printable characters in disk ident,
801 * the rest is converted to 'x<HH>'.
804 g_disk_ident_adjust(char *ident, size_t size)
806 char *p, tmp[4], newid[DISK_IDENT_SIZE];
809 for (p = ident; *p != '\0'; p++) {
814 snprintf(tmp, sizeof(tmp), "x%02hhx",
815 *(unsigned char *)p);
817 if (strlcat(newid, tmp, sizeof(newid)) >= sizeof(newid))
821 strlcpy(ident, newid, size);
828 return (g_malloc(sizeof(struct disk), M_WAITOK | M_ZERO));
832 disk_create(struct disk *dp, int version)
835 if (version != DISK_VERSION) {
836 printf("WARNING: Attempt to add disk %s%d %s",
837 dp->d_name, dp->d_unit,
838 " using incompatible ABI version of disk(9)\n");
839 printf("WARNING: Ignoring disk %s%d\n",
840 dp->d_name, dp->d_unit);
843 if (dp->d_flags & DISKFLAG_RESERVED) {
844 printf("WARNING: Attempt to add non-MPSAFE disk %s%d\n",
845 dp->d_name, dp->d_unit);
846 printf("WARNING: Ignoring disk %s%d\n",
847 dp->d_name, dp->d_unit);
850 KASSERT(dp->d_strategy != NULL, ("disk_create need d_strategy"));
851 KASSERT(dp->d_name != NULL, ("disk_create need d_name"));
852 KASSERT(*dp->d_name != 0, ("disk_create need d_name"));
853 KASSERT(strlen(dp->d_name) < SPECNAMELEN - 4, ("disk name too long"));
854 if (dp->d_devstat == NULL)
855 dp->d_devstat = devstat_new_entry(dp->d_name, dp->d_unit,
856 dp->d_sectorsize, DEVSTAT_ALL_SUPPORTED,
857 DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
860 dp->d_init_level = DISK_INIT_NONE;
862 g_disk_ident_adjust(dp->d_ident, sizeof(dp->d_ident));
863 g_post_event(g_disk_create, dp, M_WAITOK, dp, NULL);
867 disk_destroy(struct disk *dp)
872 if (dp->d_devstat != NULL)
873 devstat_remove_entry(dp->d_devstat);
874 g_post_event(g_disk_destroy, dp, M_WAITOK, NULL);
878 disk_gone(struct disk *dp)
881 struct g_provider *pp;
883 mtx_pool_lock(mtxpool_sleep, dp);
887 * If we're still in the process of creating this disk (the
888 * g_disk_create() function is still queued, or is in
889 * progress), the init level will not yet be DISK_INIT_DONE.
891 * If that is the case, g_disk_create() will see d_goneflag
892 * and take care of cleaning things up.
894 * If the disk has already been created, we default to
895 * withering the provider as usual below.
897 * If the caller has not set a d_gone() callback, he will
898 * not be any worse off by returning here, because the geom
899 * has not been fully setup in any case.
901 if (dp->d_init_level < DISK_INIT_DONE) {
902 mtx_pool_unlock(mtxpool_sleep, dp);
905 mtx_pool_unlock(mtxpool_sleep, dp);
909 pp = LIST_FIRST(&gp->provider);
911 KASSERT(LIST_NEXT(pp, provider) == NULL,
912 ("geom %p has more than one provider", gp));
913 g_wither_provider(pp, ENXIO);
919 disk_attr_changed(struct disk *dp, const char *attr, int flag)
922 struct g_provider *pp;
923 char devnamebuf[128];
927 LIST_FOREACH(pp, &gp->provider, provider)
928 (void)g_attr_changed(pp, attr, flag);
929 snprintf(devnamebuf, sizeof(devnamebuf), "devname=%s%d", dp->d_name,
931 devctl_notify("GEOM", "disk", attr, devnamebuf);
935 disk_media_changed(struct disk *dp, int flag)
938 struct g_provider *pp;
942 pp = LIST_FIRST(&gp->provider);
944 KASSERT(LIST_NEXT(pp, provider) == NULL,
945 ("geom %p has more than one provider", gp));
946 g_media_changed(pp, flag);
952 disk_media_gone(struct disk *dp, int flag)
955 struct g_provider *pp;
959 pp = LIST_FIRST(&gp->provider);
961 KASSERT(LIST_NEXT(pp, provider) == NULL,
962 ("geom %p has more than one provider", gp));
963 g_media_gone(pp, flag);
969 disk_resize(struct disk *dp, int flag)
972 if (dp->d_destroyed || dp->d_geom == NULL)
975 return (g_post_event(g_disk_resize, dp, flag, NULL));
979 g_kern_disks(void *p, int flag __unused)
988 LIST_FOREACH(gp, &g_disk_class.geom, geom) {
989 sbuf_printf(sb, "%s%s", sp, gp->name);
996 sysctl_disks(SYSCTL_HANDLER_ARGS)
1001 sb = sbuf_new_auto();
1002 g_waitfor_event(g_kern_disks, sb, M_WAITOK, NULL);
1003 error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
1008 SYSCTL_PROC(_kern, OID_AUTO, disks,
1009 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
1010 sysctl_disks, "A", "names of available disks");