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>
46 #include <sys/ctype.h>
47 #include <sys/fcntl.h>
48 #include <sys/malloc.h>
50 #include <sys/sysctl.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>
65 struct sysctl_ctx_list sysctl_ctx;
66 struct sysctl_oid *sysctl_tree;
71 static g_access_t g_disk_access;
72 static g_start_t g_disk_start;
73 static g_ioctl_t g_disk_ioctl;
74 static g_dumpconf_t g_disk_dumpconf;
75 static g_provgone_t g_disk_providergone;
77 static struct g_class g_disk_class = {
78 .name = G_DISK_CLASS_NAME,
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,
87 SYSCTL_DECL(_kern_geom);
88 static SYSCTL_NODE(_kern_geom, OID_AUTO, disk, CTLFLAG_RW, 0,
91 DECLARE_GEOM_CLASS(g_disk_class, g_disk);
94 g_disk_lock_giant(struct disk *dp)
97 if (dp->d_flags & DISKFLAG_NEEDSGIANT)
102 g_disk_unlock_giant(struct disk *dp)
105 if (dp->d_flags & DISKFLAG_NEEDSGIANT)
110 g_disk_access(struct g_provider *pp, int r, int w, int e)
113 struct g_disk_softc *sc;
116 g_trace(G_T_ACCESS, "g_disk_access(%s, %d, %d, %d)",
120 if (sc == NULL || (dp = sc->dp) == NULL || dp->d_destroyed) {
122 * Allow decreasing access count even if disk is not
125 if (r <= 0 && w <= 0 && e <= 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",
140 g_disk_unlock_giant(dp);
142 pp->mediasize = dp->d_mediasize;
143 pp->sectorsize = dp->d_sectorsize;
144 pp->stripeoffset = dp->d_stripeoffset;
145 pp->stripesize = dp->d_stripesize;
146 dp->d_flags |= DISKFLAG_OPEN;
147 if (dp->d_maxsize == 0) {
148 printf("WARNING: Disk drive %s%d has no d_maxsize\n",
149 dp->d_name, dp->d_unit);
150 dp->d_maxsize = DFLTPHYS;
152 } else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) {
153 if (dp->d_close != NULL) {
154 g_disk_lock_giant(dp);
155 error = dp->d_close(dp);
157 printf("Closed disk %s -> %d\n",
159 g_disk_unlock_giant(dp);
161 sc->state = G_STATE_ACTIVE;
163 led_set(sc->led, "0");
164 dp->d_flags &= ~DISKFLAG_OPEN;
170 g_disk_kerneldump(struct bio *bp, struct disk *dp)
172 struct g_kerneldump *gkd;
175 gkd = (struct g_kerneldump*)bp->bio_data;
176 gp = bp->bio_to->geom;
177 g_trace(G_T_TOPOLOGY, "g_disk_kernedump(%s, %jd, %jd)",
178 gp->name, (intmax_t)gkd->offset, (intmax_t)gkd->length);
179 if (dp->d_dump == NULL) {
180 g_io_deliver(bp, ENODEV);
183 gkd->di.dumper = dp->d_dump;
185 gkd->di.blocksize = dp->d_sectorsize;
186 gkd->di.maxiosize = dp->d_maxsize;
187 gkd->di.mediaoffset = gkd->offset;
188 if ((gkd->offset + gkd->length) > dp->d_mediasize)
189 gkd->length = dp->d_mediasize - gkd->offset;
190 gkd->di.mediasize = gkd->length;
195 g_disk_setstate(struct bio *bp, struct g_disk_softc *sc)
199 memcpy(&sc->state, bp->bio_data, sizeof(sc->state));
200 if (sc->led[0] != 0) {
205 case G_STATE_REBUILD:
215 led_set(sc->led, cmd);
221 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;
231 mtx_lock(&sc->done_mtx);
232 if (bp2->bio_error == 0)
233 bp2->bio_error = bp->bio_error;
234 bp2->bio_completed += bp->bio_completed;
235 if ((bp->bio_cmd & (BIO_READ|BIO_WRITE|BIO_DELETE)) != 0)
236 devstat_end_transaction_bio(sc->dp->d_devstat, bp);
239 if (bp2->bio_children == bp2->bio_inbed) {
240 bp2->bio_resid = bp2->bio_bcount - bp2->bio_completed;
241 g_io_deliver(bp2, bp2->bio_error);
243 mtx_unlock(&sc->done_mtx);
247 g_disk_ioctl(struct g_provider *pp, u_long cmd, void * data, int fflag, struct thread *td)
250 struct g_disk_softc *sc;
256 if (dp->d_ioctl == NULL)
258 g_disk_lock_giant(dp);
259 error = dp->d_ioctl(dp, cmd, data, fflag, td);
260 g_disk_unlock_giant(dp);
265 g_disk_start(struct bio *bp)
267 struct bio *bp2, *bp3;
269 struct g_disk_softc *sc;
273 sc = bp->bio_to->private;
274 if (sc == NULL || (dp = sc->dp) == NULL || dp->d_destroyed) {
275 g_io_deliver(bp, ENXIO);
279 switch(bp->bio_cmd) {
281 if (!(dp->d_flags & DISKFLAG_CANDELETE)) {
290 bp2 = g_clone_bio(bp);
296 bp2->bio_offset += off;
297 bp2->bio_length -= off;
298 if ((bp->bio_flags & BIO_UNMAPPED) == 0) {
299 bp2->bio_data += off;
301 KASSERT((dp->d_flags & DISKFLAG_UNMAPPED_BIO)
303 ("unmapped bio not supported by disk %s",
305 bp2->bio_ma += off / PAGE_SIZE;
306 bp2->bio_ma_offset += off;
307 bp2->bio_ma_offset %= PAGE_SIZE;
308 bp2->bio_ma_n -= off / PAGE_SIZE;
310 if (bp2->bio_length > dp->d_maxsize) {
312 * XXX: If we have a stripesize we should really
315 bp2->bio_length = dp->d_maxsize;
316 if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
317 bp2->bio_ma_n = howmany(
319 bp2->bio_length, PAGE_SIZE);
321 off += dp->d_maxsize;
323 * To avoid a race, we need to grab the next bio
324 * before we schedule this one. See "notes".
326 bp3 = g_clone_bio(bp);
328 bp->bio_error = ENOMEM;
330 bp2->bio_done = g_disk_done;
331 bp2->bio_pblkno = bp2->bio_offset / dp->d_sectorsize;
332 bp2->bio_bcount = bp2->bio_length;
334 devstat_start_transaction_bio(dp->d_devstat, bp2);
335 g_disk_lock_giant(dp);
337 g_disk_unlock_giant(dp);
340 } while (bp2 != NULL);
343 /* Give the driver a chance to override */
344 if (dp->d_getattr != NULL) {
345 if (bp->bio_disk == NULL)
347 error = dp->d_getattr(bp);
352 if (g_handleattr_int(bp, "GEOM::candelete",
353 (dp->d_flags & DISKFLAG_CANDELETE) != 0))
355 else if (g_handleattr_int(bp, "GEOM::fwsectors",
358 else if (g_handleattr_int(bp, "GEOM::fwheads", dp->d_fwheads))
360 else if (g_handleattr_off_t(bp, "GEOM::frontstuff", 0))
362 else if (g_handleattr_str(bp, "GEOM::ident", dp->d_ident))
364 else if (g_handleattr(bp, "GEOM::hba_vendor",
365 &dp->d_hba_vendor, 2))
367 else if (g_handleattr(bp, "GEOM::hba_device",
368 &dp->d_hba_device, 2))
370 else if (g_handleattr(bp, "GEOM::hba_subvendor",
371 &dp->d_hba_subvendor, 2))
373 else if (g_handleattr(bp, "GEOM::hba_subdevice",
374 &dp->d_hba_subdevice, 2))
376 else if (!strcmp(bp->bio_attribute, "GEOM::kerneldump"))
377 g_disk_kerneldump(bp, dp);
378 else if (!strcmp(bp->bio_attribute, "GEOM::setstate"))
379 g_disk_setstate(bp, sc);
384 g_trace(G_T_BIO, "g_disk_flushcache(%s)",
386 if (!(dp->d_flags & DISKFLAG_CANFLUSHCACHE)) {
390 bp2 = g_clone_bio(bp);
392 g_io_deliver(bp, ENOMEM);
395 bp2->bio_done = g_disk_done;
397 g_disk_lock_giant(dp);
399 g_disk_unlock_giant(dp);
405 if (error != EJUSTRETURN)
406 g_io_deliver(bp, error);
411 g_disk_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp)
414 struct g_disk_softc *sc;
417 if (sc == NULL || (dp = sc->dp) == NULL)
419 if (indent == NULL) {
420 sbuf_printf(sb, " hd %u", dp->d_fwheads);
421 sbuf_printf(sb, " sc %u", dp->d_fwsectors);
425 sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n",
426 indent, dp->d_fwheads);
427 sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n",
428 indent, dp->d_fwsectors);
429 sbuf_printf(sb, "%s<ident>%s</ident>\n", indent, dp->d_ident);
430 sbuf_printf(sb, "%s<descr>%s</descr>\n", indent, dp->d_descr);
435 g_disk_resize(void *ptr, int flag)
439 struct g_provider *pp;
441 if (flag == EV_CANCEL)
448 if (dp->d_destroyed || gp == NULL)
451 LIST_FOREACH(pp, &gp->provider, provider) {
452 if (pp->sectorsize != 0 &&
453 pp->sectorsize != dp->d_sectorsize)
454 g_wither_provider(pp, ENXIO);
456 g_resize_provider(pp, dp->d_mediasize);
461 g_disk_create(void *arg, int flag)
464 struct g_provider *pp;
466 struct g_disk_softc *sc;
469 if (flag == EV_CANCEL)
473 sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
474 mtx_init(&sc->done_mtx, "g_disk_done", NULL, MTX_DEF);
476 gp = g_new_geomf(&g_disk_class, "%s%d", dp->d_name, dp->d_unit);
478 pp = g_new_providerf(gp, "%s", gp->name);
479 pp->mediasize = dp->d_mediasize;
480 pp->sectorsize = dp->d_sectorsize;
481 pp->stripeoffset = dp->d_stripeoffset;
482 pp->stripesize = dp->d_stripesize;
483 if ((dp->d_flags & DISKFLAG_UNMAPPED_BIO) != 0)
484 pp->flags |= G_PF_ACCEPT_UNMAPPED;
486 printf("GEOM: new disk %s\n", gp->name);
487 sysctl_ctx_init(&sc->sysctl_ctx);
488 snprintf(tmpstr, sizeof(tmpstr), "GEOM disk %s", gp->name);
489 sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
490 SYSCTL_STATIC_CHILDREN(_kern_geom_disk), OID_AUTO, gp->name,
491 CTLFLAG_RD, 0, tmpstr);
492 if (sc->sysctl_tree != NULL) {
493 snprintf(tmpstr, sizeof(tmpstr),
494 "kern.geom.disk.%s.led", gp->name);
495 TUNABLE_STR_FETCH(tmpstr, sc->led, sizeof(sc->led));
496 SYSCTL_ADD_STRING(&sc->sysctl_ctx,
497 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO, "led",
498 CTLFLAG_RW | CTLFLAG_TUN, sc->led, sizeof(sc->led),
503 g_error_provider(pp, 0);
507 * We get this callback after all of the consumers have gone away, and just
508 * before the provider is freed. If the disk driver provided a d_gone
509 * callback, let them know that it is okay to free resources -- they won't
510 * be getting any more accesses from GEOM.
513 g_disk_providergone(struct g_provider *pp)
516 struct g_disk_softc *sc;
518 sc = (struct g_disk_softc *)pp->private;
520 if (dp != NULL && dp->d_gone != NULL)
522 if (sc->sysctl_tree != NULL) {
523 sysctl_ctx_free(&sc->sysctl_ctx);
524 sc->sysctl_tree = NULL;
526 if (sc->led[0] != 0) {
527 led_set(sc->led, "0");
531 pp->geom->softc = NULL;
532 mtx_destroy(&sc->done_mtx);
537 g_disk_destroy(void *ptr, int flag)
541 struct g_disk_softc *sc;
551 g_wither_geom(gp, ENXIO);
557 * We only allow printable characters in disk ident,
558 * the rest is converted to 'x<HH>'.
561 g_disk_ident_adjust(char *ident, size_t size)
563 char *p, tmp[4], newid[DISK_IDENT_SIZE];
566 for (p = ident; *p != '\0'; p++) {
571 snprintf(tmp, sizeof(tmp), "x%02hhx",
572 *(unsigned char *)p);
574 if (strlcat(newid, tmp, sizeof(newid)) >= sizeof(newid))
578 strlcpy(ident, newid, size);
585 return (g_malloc(sizeof(struct disk), M_WAITOK | M_ZERO));
589 disk_create(struct disk *dp, int version)
592 if (version != DISK_VERSION_02) {
593 printf("WARNING: Attempt to add disk %s%d %s",
594 dp->d_name, dp->d_unit,
595 " using incompatible ABI version of disk(9)\n");
596 printf("WARNING: Ignoring disk %s%d\n",
597 dp->d_name, dp->d_unit);
600 KASSERT(dp->d_strategy != NULL, ("disk_create need d_strategy"));
601 KASSERT(dp->d_name != NULL, ("disk_create need d_name"));
602 KASSERT(*dp->d_name != 0, ("disk_create need d_name"));
603 KASSERT(strlen(dp->d_name) < SPECNAMELEN - 4, ("disk name too long"));
604 if (dp->d_devstat == NULL)
605 dp->d_devstat = devstat_new_entry(dp->d_name, dp->d_unit,
606 dp->d_sectorsize, DEVSTAT_ALL_SUPPORTED,
607 DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
609 g_disk_ident_adjust(dp->d_ident, sizeof(dp->d_ident));
610 g_post_event(g_disk_create, dp, M_WAITOK, dp, NULL);
614 disk_destroy(struct disk *dp)
619 if (dp->d_devstat != NULL)
620 devstat_remove_entry(dp->d_devstat);
621 g_post_event(g_disk_destroy, dp, M_WAITOK, NULL);
625 disk_gone(struct disk *dp)
628 struct g_provider *pp;
632 pp = LIST_FIRST(&gp->provider);
634 KASSERT(LIST_NEXT(pp, provider) == NULL,
635 ("geom %p has more than one provider", gp));
636 g_wither_provider(pp, ENXIO);
642 disk_attr_changed(struct disk *dp, const char *attr, int flag)
645 struct g_provider *pp;
649 LIST_FOREACH(pp, &gp->provider, provider)
650 (void)g_attr_changed(pp, attr, flag);
654 disk_media_changed(struct disk *dp, int flag)
657 struct g_provider *pp;
661 pp = LIST_FIRST(&gp->provider);
663 KASSERT(LIST_NEXT(pp, provider) == NULL,
664 ("geom %p has more than one provider", gp));
665 g_media_changed(pp, flag);
671 disk_media_gone(struct disk *dp, int flag)
674 struct g_provider *pp;
678 pp = LIST_FIRST(&gp->provider);
680 KASSERT(LIST_NEXT(pp, provider) == NULL,
681 ("geom %p has more than one provider", gp));
682 g_media_gone(pp, flag);
688 disk_resize(struct disk *dp, int flag)
691 if (dp->d_destroyed || dp->d_geom == NULL)
694 return (g_post_event(g_disk_resize, dp, flag, NULL));
698 g_kern_disks(void *p, int flag __unused)
707 LIST_FOREACH(gp, &g_disk_class.geom, geom) {
708 sbuf_printf(sb, "%s%s", sp, gp->name);
715 sysctl_disks(SYSCTL_HANDLER_ARGS)
720 sb = sbuf_new_auto();
721 g_waitfor_event(g_kern_disks, sb, M_WAITOK, NULL);
722 error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
727 SYSCTL_PROC(_kern, OID_AUTO, disks,
728 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
729 sysctl_disks, "A", "names of available disks");