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_lock_giant(struct disk *dp)
100 if (dp->d_flags & DISKFLAG_NEEDSGIANT)
105 g_disk_unlock_giant(struct disk *dp)
108 if (dp->d_flags & DISKFLAG_NEEDSGIANT)
113 g_disk_access(struct g_provider *pp, int r, int w, int e)
116 struct g_disk_softc *sc;
119 g_trace(G_T_ACCESS, "g_disk_access(%s, %d, %d, %d)",
123 if (sc == NULL || (dp = sc->dp) == NULL || dp->d_destroyed) {
125 * Allow decreasing access count even if disk is not
128 if (r <= 0 && w <= 0 && e <= 0)
136 if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) {
138 * It would be better to defer this decision to d_open if
139 * it was able to take flags.
141 if (w > 0 && (dp->d_flags & DISKFLAG_WRITE_PROTECT) != 0)
143 if (error == 0 && dp->d_open != NULL) {
144 g_disk_lock_giant(dp);
145 error = dp->d_open(dp);
146 g_disk_unlock_giant(dp);
148 if (bootverbose && error != 0)
149 printf("Opened disk %s -> %d\n", pp->name, error);
152 pp->mediasize = dp->d_mediasize;
153 pp->sectorsize = dp->d_sectorsize;
154 if (dp->d_maxsize == 0) {
155 printf("WARNING: Disk drive %s%d has no d_maxsize\n",
156 dp->d_name, dp->d_unit);
157 dp->d_maxsize = DFLTPHYS;
159 if (dp->d_delmaxsize == 0) {
160 if (bootverbose && dp->d_flags & DISKFLAG_CANDELETE) {
161 printf("WARNING: Disk drive %s%d has no "
162 "d_delmaxsize\n", dp->d_name, dp->d_unit);
164 dp->d_delmaxsize = dp->d_maxsize;
166 pp->stripeoffset = dp->d_stripeoffset;
167 pp->stripesize = dp->d_stripesize;
168 dp->d_flags |= DISKFLAG_OPEN;
169 } else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) {
170 if (dp->d_close != NULL) {
171 g_disk_lock_giant(dp);
172 error = dp->d_close(dp);
174 printf("Closed disk %s -> %d\n",
176 g_disk_unlock_giant(dp);
178 sc->state = G_STATE_ACTIVE;
180 led_set(sc->led, "0");
181 dp->d_flags &= ~DISKFLAG_OPEN;
187 g_disk_kerneldump(struct bio *bp, struct disk *dp)
189 struct g_kerneldump *gkd;
192 gkd = (struct g_kerneldump*)bp->bio_data;
193 gp = bp->bio_to->geom;
194 g_trace(G_T_TOPOLOGY, "g_disk_kerneldump(%s, %jd, %jd)",
195 gp->name, (intmax_t)gkd->offset, (intmax_t)gkd->length);
196 if (dp->d_dump == NULL) {
197 g_io_deliver(bp, ENODEV);
200 gkd->di.dumper = dp->d_dump;
202 gkd->di.blocksize = dp->d_sectorsize;
203 gkd->di.maxiosize = dp->d_maxsize;
204 gkd->di.mediaoffset = gkd->offset;
205 if ((gkd->offset + gkd->length) > dp->d_mediasize)
206 gkd->length = dp->d_mediasize - gkd->offset;
207 gkd->di.mediasize = gkd->length;
212 g_disk_setstate(struct bio *bp, struct g_disk_softc *sc)
216 memcpy(&sc->state, bp->bio_data, sizeof(sc->state));
217 if (sc->led[0] != 0) {
222 case G_STATE_REBUILD:
232 led_set(sc->led, cmd);
238 g_disk_done(struct bio *bp)
242 struct g_disk_softc *sc;
244 /* See "notes" for why we need a mutex here */
245 /* XXX: will witness accept a mix of Giant/unGiant drivers here ? */
246 bp2 = bp->bio_parent;
247 sc = bp2->bio_to->private;
248 bp->bio_completed = bp->bio_length - bp->bio_resid;
250 mtx_lock(&sc->done_mtx);
251 if (bp2->bio_error == 0)
252 bp2->bio_error = bp->bio_error;
253 bp2->bio_completed += bp->bio_completed;
254 if ((bp->bio_cmd & (BIO_READ|BIO_WRITE|BIO_DELETE|BIO_FLUSH)) != 0)
255 devstat_end_transaction_bio_bt(sc->dp->d_devstat, bp, &now);
257 if (bp2->bio_children == bp2->bio_inbed) {
258 mtx_unlock(&sc->done_mtx);
259 bp2->bio_resid = bp2->bio_bcount - bp2->bio_completed;
260 g_io_deliver(bp2, bp2->bio_error);
262 mtx_unlock(&sc->done_mtx);
267 g_disk_ioctl(struct g_provider *pp, u_long cmd, void * data, int fflag, struct thread *td)
270 struct g_disk_softc *sc;
276 if (dp->d_ioctl == NULL)
278 g_disk_lock_giant(dp);
279 error = dp->d_ioctl(dp, cmd, data, fflag, td);
280 g_disk_unlock_giant(dp);
285 g_disk_maxsize(struct disk *dp, struct bio *bp)
287 if (bp->bio_cmd == BIO_DELETE)
288 return (dp->d_delmaxsize);
289 return (dp->d_maxsize);
293 g_disk_maxsegs(struct disk *dp, struct bio *bp)
295 return ((g_disk_maxsize(dp, bp) / PAGE_SIZE) + 1);
299 g_disk_advance(struct disk *dp, struct bio *bp, off_t off)
302 bp->bio_offset += off;
303 bp->bio_length -= off;
305 if ((bp->bio_flags & BIO_VLIST) != 0) {
306 bus_dma_segment_t *seg, *end;
308 seg = (bus_dma_segment_t *)bp->bio_data;
309 end = (bus_dma_segment_t *)bp->bio_data + bp->bio_ma_n;
310 off += bp->bio_ma_offset;
311 while (off >= seg->ds_len) {
312 KASSERT((seg != end),
313 ("vlist request runs off the end"));
317 bp->bio_ma_offset = off;
318 bp->bio_ma_n = end - seg;
319 bp->bio_data = (void *)seg;
320 } else if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
321 bp->bio_ma += off / PAGE_SIZE;
322 bp->bio_ma_offset += off;
323 bp->bio_ma_offset %= PAGE_SIZE;
324 bp->bio_ma_n -= off / PAGE_SIZE;
331 g_disk_seg_limit(bus_dma_segment_t *seg, off_t *poffset,
332 off_t *plength, int *ppages)
334 uintptr_t seg_page_base;
335 uintptr_t seg_page_end;
343 if (length > seg->ds_len - offset)
344 length = seg->ds_len - offset;
346 seg_page_base = trunc_page(seg->ds_addr + offset);
347 seg_page_end = round_page(seg->ds_addr + offset + length);
348 seg_pages = (seg_page_end - seg_page_base) >> PAGE_SHIFT;
350 if (seg_pages > *ppages) {
352 length = (seg_page_base + (seg_pages << PAGE_SHIFT)) -
353 (seg->ds_addr + offset);
358 *ppages -= seg_pages;
362 g_disk_vlist_limit(struct disk *dp, struct bio *bp, bus_dma_segment_t **pendseg)
364 bus_dma_segment_t *seg, *end;
369 seg = (bus_dma_segment_t *)bp->bio_data;
370 end = (bus_dma_segment_t *)bp->bio_data + bp->bio_ma_n;
371 residual = bp->bio_length;
372 offset = bp->bio_ma_offset;
373 pages = g_disk_maxsegs(dp, bp);
374 while (residual != 0 && pages != 0) {
375 KASSERT((seg != end),
376 ("vlist limit runs off the end"));
377 g_disk_seg_limit(seg, &offset, &residual, &pages);
386 g_disk_limit(struct disk *dp, struct bio *bp)
388 bool limited = false;
391 maxsz = g_disk_maxsize(dp, bp);
394 * XXX: If we have a stripesize we should really use it here.
395 * Care should be taken in the delete case if this is done
396 * as deletes can be very sensitive to size given how they
399 if (bp->bio_length > maxsz) {
400 bp->bio_length = maxsz;
404 if ((bp->bio_flags & BIO_VLIST) != 0) {
405 bus_dma_segment_t *firstseg, *endseg;
408 firstseg = (bus_dma_segment_t*)bp->bio_data;
409 residual = g_disk_vlist_limit(dp, bp, &endseg);
411 bp->bio_ma_n = endseg - firstseg;
412 bp->bio_length -= residual;
415 } else if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
417 howmany(bp->bio_ma_offset + bp->bio_length, PAGE_SIZE);
424 g_disk_start(struct bio *bp)
426 struct bio *bp2, *bp3;
428 struct g_disk_softc *sc;
432 sc = bp->bio_to->private;
433 if (sc == NULL || (dp = sc->dp) == NULL || dp->d_destroyed) {
434 g_io_deliver(bp, ENXIO);
438 switch(bp->bio_cmd) {
440 if (!(dp->d_flags & DISKFLAG_CANDELETE)) {
447 KASSERT((dp->d_flags & DISKFLAG_UNMAPPED_BIO) != 0 ||
448 (bp->bio_flags & BIO_UNMAPPED) == 0,
449 ("unmapped bio not supported by disk %s", dp->d_name));
452 bp2 = g_clone_bio(bp);
458 if (g_disk_limit(dp, bp2)) {
459 off += bp2->bio_length;
462 * To avoid a race, we need to grab the next bio
463 * before we schedule this one. See "notes".
465 bp3 = g_clone_bio(bp);
467 bp->bio_error = ENOMEM;
469 bp2->bio_done = g_disk_done;
470 bp2->bio_pblkno = bp2->bio_offset / dp->d_sectorsize;
471 bp2->bio_bcount = bp2->bio_length;
473 mtx_lock(&sc->start_mtx);
474 devstat_start_transaction_bio(dp->d_devstat, bp2);
475 mtx_unlock(&sc->start_mtx);
476 g_disk_lock_giant(dp);
478 g_disk_unlock_giant(dp);
485 g_disk_advance(dp, bp2, off);
489 /* Give the driver a chance to override */
490 if (dp->d_getattr != NULL) {
491 if (bp->bio_disk == NULL)
493 error = dp->d_getattr(bp);
498 if (g_handleattr_int(bp, "GEOM::candelete",
499 (dp->d_flags & DISKFLAG_CANDELETE) != 0))
501 else if (g_handleattr_int(bp, "GEOM::fwsectors",
504 else if (g_handleattr_int(bp, "GEOM::fwheads", dp->d_fwheads))
506 else if (g_handleattr_off_t(bp, "GEOM::frontstuff", 0))
508 else if (g_handleattr_str(bp, "GEOM::ident", dp->d_ident))
510 else if (g_handleattr_str(bp, "GEOM::descr", dp->d_descr))
512 else if (g_handleattr_uint16_t(bp, "GEOM::hba_vendor",
515 else if (g_handleattr_uint16_t(bp, "GEOM::hba_device",
518 else if (g_handleattr_uint16_t(bp, "GEOM::hba_subvendor",
519 dp->d_hba_subvendor))
521 else if (g_handleattr_uint16_t(bp, "GEOM::hba_subdevice",
522 dp->d_hba_subdevice))
524 else if (!strcmp(bp->bio_attribute, "GEOM::kerneldump"))
525 g_disk_kerneldump(bp, dp);
526 else if (!strcmp(bp->bio_attribute, "GEOM::setstate"))
527 g_disk_setstate(bp, sc);
528 else if (!strcmp(bp->bio_attribute, "GEOM::rotation_rate")) {
531 if ((dp->d_flags & DISKFLAG_LACKS_ROTRATE) == 0)
532 v = dp->d_rotation_rate;
534 v = 0; /* rate unknown */
535 g_handleattr_uint16_t(bp, "GEOM::rotation_rate", v);
541 g_trace(G_T_BIO, "g_disk_flushcache(%s)",
543 if (!(dp->d_flags & DISKFLAG_CANFLUSHCACHE)) {
547 bp2 = g_clone_bio(bp);
549 g_io_deliver(bp, ENOMEM);
552 bp2->bio_done = g_disk_done;
554 mtx_lock(&sc->start_mtx);
555 devstat_start_transaction_bio(dp->d_devstat, bp2);
556 mtx_unlock(&sc->start_mtx);
557 g_disk_lock_giant(dp);
559 g_disk_unlock_giant(dp);
565 if (error != EJUSTRETURN)
566 g_io_deliver(bp, error);
571 g_disk_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp)
575 struct g_disk_softc *sc;
580 if (sc == NULL || (dp = sc->dp) == NULL)
582 if (indent == NULL) {
583 sbuf_printf(sb, " hd %u", dp->d_fwheads);
584 sbuf_printf(sb, " sc %u", dp->d_fwsectors);
588 sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n",
589 indent, dp->d_fwheads);
590 sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n",
591 indent, dp->d_fwsectors);
594 * "rotationrate" is a little complicated, because the value
595 * returned by the drive might not be the RPM; 0 and 1 are
596 * special cases, and there's also a valid range.
598 sbuf_printf(sb, "%s<rotationrate>", indent);
599 if (dp->d_rotation_rate == DISK_RR_UNKNOWN) /* Old drives */
600 sbuf_printf(sb, "unknown"); /* don't report RPM. */
601 else if (dp->d_rotation_rate == DISK_RR_NON_ROTATING)
602 sbuf_printf(sb, "0");
603 else if ((dp->d_rotation_rate >= DISK_RR_MIN) &&
604 (dp->d_rotation_rate <= DISK_RR_MAX))
605 sbuf_printf(sb, "%u", dp->d_rotation_rate);
607 sbuf_printf(sb, "invalid");
608 sbuf_printf(sb, "</rotationrate>\n");
609 if (dp->d_getattr != NULL) {
610 buf = g_malloc(DISK_IDENT_SIZE, M_WAITOK);
613 bp->bio_attribute = "GEOM::ident";
614 bp->bio_length = DISK_IDENT_SIZE;
616 res = dp->d_getattr(bp);
617 sbuf_printf(sb, "%s<ident>", indent);
618 g_conf_printf_escaped(sb, "%s",
619 res == 0 ? buf: dp->d_ident);
620 sbuf_printf(sb, "</ident>\n");
621 bp->bio_attribute = "GEOM::lunid";
622 bp->bio_length = DISK_IDENT_SIZE;
624 if (dp->d_getattr(bp) == 0) {
625 sbuf_printf(sb, "%s<lunid>", indent);
626 g_conf_printf_escaped(sb, "%s", buf);
627 sbuf_printf(sb, "</lunid>\n");
629 bp->bio_attribute = "GEOM::lunname";
630 bp->bio_length = DISK_IDENT_SIZE;
632 if (dp->d_getattr(bp) == 0) {
633 sbuf_printf(sb, "%s<lunname>", indent);
634 g_conf_printf_escaped(sb, "%s", buf);
635 sbuf_printf(sb, "</lunname>\n");
640 sbuf_printf(sb, "%s<ident>", indent);
641 g_conf_printf_escaped(sb, "%s", dp->d_ident);
642 sbuf_printf(sb, "</ident>\n");
644 sbuf_printf(sb, "%s<descr>", indent);
645 g_conf_printf_escaped(sb, "%s", dp->d_descr);
646 sbuf_printf(sb, "</descr>\n");
651 g_disk_resize(void *ptr, int flag)
655 struct g_provider *pp;
657 if (flag == EV_CANCEL)
664 if (dp->d_destroyed || gp == NULL)
667 LIST_FOREACH(pp, &gp->provider, provider) {
668 if (pp->sectorsize != 0 &&
669 pp->sectorsize != dp->d_sectorsize)
670 g_wither_provider(pp, ENXIO);
672 g_resize_provider(pp, dp->d_mediasize);
677 g_disk_create(void *arg, int flag)
680 struct g_provider *pp;
682 struct g_disk_softc *sc;
685 if (flag == EV_CANCEL)
689 sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
690 mtx_init(&sc->start_mtx, "g_disk_start", NULL, MTX_DEF);
691 mtx_init(&sc->done_mtx, "g_disk_done", NULL, MTX_DEF);
693 gp = g_new_geomf(&g_disk_class, "%s%d", dp->d_name, dp->d_unit);
695 pp = g_new_providerf(gp, "%s", gp->name);
696 devstat_remove_entry(pp->stat);
698 dp->d_devstat->id = pp;
699 pp->mediasize = dp->d_mediasize;
700 pp->sectorsize = dp->d_sectorsize;
701 pp->stripeoffset = dp->d_stripeoffset;
702 pp->stripesize = dp->d_stripesize;
703 if ((dp->d_flags & DISKFLAG_UNMAPPED_BIO) != 0)
704 pp->flags |= G_PF_ACCEPT_UNMAPPED;
705 if ((dp->d_flags & DISKFLAG_DIRECT_COMPLETION) != 0)
706 pp->flags |= G_PF_DIRECT_SEND;
707 pp->flags |= G_PF_DIRECT_RECEIVE;
709 printf("GEOM: new disk %s\n", gp->name);
710 sysctl_ctx_init(&sc->sysctl_ctx);
711 snprintf(tmpstr, sizeof(tmpstr), "GEOM disk %s", gp->name);
712 sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
713 SYSCTL_STATIC_CHILDREN(_kern_geom_disk), OID_AUTO, gp->name,
714 CTLFLAG_RD, 0, tmpstr);
715 if (sc->sysctl_tree != NULL) {
716 snprintf(tmpstr, sizeof(tmpstr),
717 "kern.geom.disk.%s.led", gp->name);
718 TUNABLE_STR_FETCH(tmpstr, sc->led, sizeof(sc->led));
719 SYSCTL_ADD_STRING(&sc->sysctl_ctx,
720 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO, "led",
721 CTLFLAG_RW | CTLFLAG_TUN, sc->led, sizeof(sc->led),
726 g_error_provider(pp, 0);
730 * We get this callback after all of the consumers have gone away, and just
731 * before the provider is freed. If the disk driver provided a d_gone
732 * callback, let them know that it is okay to free resources -- they won't
733 * be getting any more accesses from GEOM.
736 g_disk_providergone(struct g_provider *pp)
739 struct g_disk_softc *sc;
741 sc = (struct g_disk_softc *)pp->private;
743 if (dp != NULL && dp->d_gone != NULL)
745 if (sc->sysctl_tree != NULL) {
746 sysctl_ctx_free(&sc->sysctl_ctx);
747 sc->sysctl_tree = NULL;
749 if (sc->led[0] != 0) {
750 led_set(sc->led, "0");
754 pp->geom->softc = NULL;
755 mtx_destroy(&sc->done_mtx);
756 mtx_destroy(&sc->start_mtx);
761 g_disk_destroy(void *ptr, int flag)
765 struct g_disk_softc *sc;
775 g_wither_geom(gp, ENXIO);
781 * We only allow printable characters in disk ident,
782 * the rest is converted to 'x<HH>'.
785 g_disk_ident_adjust(char *ident, size_t size)
787 char *p, tmp[4], newid[DISK_IDENT_SIZE];
790 for (p = ident; *p != '\0'; p++) {
795 snprintf(tmp, sizeof(tmp), "x%02hhx",
796 *(unsigned char *)p);
798 if (strlcat(newid, tmp, sizeof(newid)) >= sizeof(newid))
802 strlcpy(ident, newid, size);
809 return (g_malloc(sizeof(struct disk), M_WAITOK | M_ZERO));
813 disk_create(struct disk *dp, int version)
816 if (version != DISK_VERSION) {
817 printf("WARNING: Attempt to add disk %s%d %s",
818 dp->d_name, dp->d_unit,
819 " using incompatible ABI version of disk(9)\n");
820 printf("WARNING: Ignoring disk %s%d\n",
821 dp->d_name, dp->d_unit);
824 if (version < DISK_VERSION_04)
825 dp->d_flags |= DISKFLAG_LACKS_ROTRATE;
826 KASSERT(dp->d_strategy != NULL, ("disk_create need d_strategy"));
827 KASSERT(dp->d_name != NULL, ("disk_create need d_name"));
828 KASSERT(*dp->d_name != 0, ("disk_create need d_name"));
829 KASSERT(strlen(dp->d_name) < SPECNAMELEN - 4, ("disk name too long"));
830 if (dp->d_devstat == NULL)
831 dp->d_devstat = devstat_new_entry(dp->d_name, dp->d_unit,
832 dp->d_sectorsize, DEVSTAT_ALL_SUPPORTED,
833 DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
835 g_disk_ident_adjust(dp->d_ident, sizeof(dp->d_ident));
836 g_post_event(g_disk_create, dp, M_WAITOK, dp, NULL);
840 disk_destroy(struct disk *dp)
845 if (dp->d_devstat != NULL)
846 devstat_remove_entry(dp->d_devstat);
847 g_post_event(g_disk_destroy, dp, M_WAITOK, NULL);
851 disk_gone(struct disk *dp)
854 struct g_provider *pp;
858 pp = LIST_FIRST(&gp->provider);
860 KASSERT(LIST_NEXT(pp, provider) == NULL,
861 ("geom %p has more than one provider", gp));
862 g_wither_provider(pp, ENXIO);
868 disk_attr_changed(struct disk *dp, const char *attr, int flag)
871 struct g_provider *pp;
872 char devnamebuf[128];
876 LIST_FOREACH(pp, &gp->provider, provider)
877 (void)g_attr_changed(pp, attr, flag);
878 snprintf(devnamebuf, sizeof(devnamebuf), "devname=%s%d", dp->d_name,
880 devctl_notify("GEOM", "disk", attr, devnamebuf);
884 disk_media_changed(struct disk *dp, int flag)
887 struct g_provider *pp;
891 pp = LIST_FIRST(&gp->provider);
893 KASSERT(LIST_NEXT(pp, provider) == NULL,
894 ("geom %p has more than one provider", gp));
895 g_media_changed(pp, flag);
901 disk_media_gone(struct disk *dp, int flag)
904 struct g_provider *pp;
908 pp = LIST_FIRST(&gp->provider);
910 KASSERT(LIST_NEXT(pp, provider) == NULL,
911 ("geom %p has more than one provider", gp));
912 g_media_gone(pp, flag);
918 disk_resize(struct disk *dp, int flag)
921 if (dp->d_destroyed || dp->d_geom == NULL)
924 return (g_post_event(g_disk_resize, dp, flag, NULL));
928 g_kern_disks(void *p, int flag __unused)
937 LIST_FOREACH(gp, &g_disk_class.geom, geom) {
938 sbuf_printf(sb, "%s%s", sp, gp->name);
945 sysctl_disks(SYSCTL_HANDLER_ARGS)
950 sb = sbuf_new_auto();
951 g_waitfor_event(g_kern_disks, sb, M_WAITOK, NULL);
952 error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
957 SYSCTL_PROC(_kern, OID_AUTO, disks,
958 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
959 sysctl_disks, "A", "names of available disks");