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>
64 struct sysctl_ctx_list sysctl_ctx;
65 struct sysctl_oid *sysctl_tree;
70 static struct mtx g_disk_done_mtx;
72 static g_access_t g_disk_access;
73 static g_init_t g_disk_init;
74 static g_fini_t g_disk_fini;
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 = {
85 .start = g_disk_start,
86 .access = g_disk_access,
87 .ioctl = g_disk_ioctl,
88 .providergone = g_disk_providergone,
89 .dumpconf = g_disk_dumpconf,
92 SYSCTL_DECL(_kern_geom);
93 static SYSCTL_NODE(_kern_geom, OID_AUTO, disk, CTLFLAG_RW, 0,
97 g_disk_init(struct g_class *mp __unused)
100 mtx_init(&g_disk_done_mtx, "g_disk_done", NULL, MTX_DEF);
104 g_disk_fini(struct g_class *mp __unused)
107 mtx_destroy(&g_disk_done_mtx);
110 DECLARE_GEOM_CLASS(g_disk_class, g_disk);
113 g_disk_lock_giant(struct disk *dp)
116 if (dp->d_flags & DISKFLAG_NEEDSGIANT)
121 g_disk_unlock_giant(struct disk *dp)
124 if (dp->d_flags & DISKFLAG_NEEDSGIANT)
129 g_disk_access(struct g_provider *pp, int r, int w, int e)
132 struct g_disk_softc *sc;
135 g_trace(G_T_ACCESS, "g_disk_access(%s, %d, %d, %d)",
138 sc = pp->geom->softc;
139 if (sc == NULL || (dp = sc->dp) == NULL || dp->d_destroyed) {
141 * Allow decreasing access count even if disk is not
144 if (r <= 0 && w <= 0 && e <= 0)
152 if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) {
153 if (dp->d_open != NULL) {
154 g_disk_lock_giant(dp);
155 error = dp->d_open(dp);
156 if (bootverbose && error != 0)
157 printf("Opened disk %s -> %d\n",
159 g_disk_unlock_giant(dp);
161 pp->mediasize = dp->d_mediasize;
162 pp->sectorsize = dp->d_sectorsize;
163 if (dp->d_flags & DISKFLAG_CANDELETE)
164 pp->flags |= G_PF_CANDELETE;
166 pp->flags &= ~G_PF_CANDELETE;
167 pp->stripeoffset = dp->d_stripeoffset;
168 pp->stripesize = dp->d_stripesize;
169 dp->d_flags |= DISKFLAG_OPEN;
170 if (dp->d_maxsize == 0) {
171 printf("WARNING: Disk drive %s%d has no d_maxsize\n",
172 dp->d_name, dp->d_unit);
173 dp->d_maxsize = DFLTPHYS;
175 } else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) {
176 if (dp->d_close != NULL) {
177 g_disk_lock_giant(dp);
178 error = dp->d_close(dp);
180 printf("Closed disk %s -> %d\n",
182 g_disk_unlock_giant(dp);
184 sc->state = G_STATE_ACTIVE;
186 led_set(sc->led, "0");
187 dp->d_flags &= ~DISKFLAG_OPEN;
193 g_disk_kerneldump(struct bio *bp, struct disk *dp)
195 struct g_kerneldump *gkd;
198 gkd = (struct g_kerneldump*)bp->bio_data;
199 gp = bp->bio_to->geom;
200 g_trace(G_T_TOPOLOGY, "g_disk_kernedump(%s, %jd, %jd)",
201 gp->name, (intmax_t)gkd->offset, (intmax_t)gkd->length);
202 if (dp->d_dump == NULL) {
203 g_io_deliver(bp, ENODEV);
206 gkd->di.dumper = dp->d_dump;
208 gkd->di.blocksize = dp->d_sectorsize;
209 gkd->di.maxiosize = dp->d_maxsize;
210 gkd->di.mediaoffset = gkd->offset;
211 if ((gkd->offset + gkd->length) > dp->d_mediasize)
212 gkd->length = dp->d_mediasize - gkd->offset;
213 gkd->di.mediasize = gkd->length;
218 g_disk_setstate(struct bio *bp, struct g_disk_softc *sc)
222 memcpy(&sc->state, bp->bio_data, sizeof(sc->state));
223 if (sc->led[0] != 0) {
228 case G_STATE_REBUILD:
238 led_set(sc->led, cmd);
244 g_disk_done(struct bio *bp)
248 struct g_disk_softc *sc;
250 /* See "notes" for why we need a mutex here */
251 /* XXX: will witness accept a mix of Giant/unGiant drivers here ? */
252 mtx_lock(&g_disk_done_mtx);
253 bp->bio_completed = bp->bio_length - bp->bio_resid;
255 bp2 = bp->bio_parent;
256 if (bp2->bio_error == 0)
257 bp2->bio_error = bp->bio_error;
258 bp2->bio_completed += bp->bio_completed;
259 if ((bp->bio_cmd & (BIO_READ|BIO_WRITE|BIO_DELETE)) != 0 &&
260 (sc = bp2->bio_to->geom->softc) != NULL &&
261 (dp = sc->dp) != NULL) {
262 devstat_end_transaction_bio(dp->d_devstat, bp);
266 if (bp2->bio_children == bp2->bio_inbed) {
267 bp2->bio_resid = bp2->bio_bcount - bp2->bio_completed;
268 g_io_deliver(bp2, bp2->bio_error);
270 mtx_unlock(&g_disk_done_mtx);
274 g_disk_ioctl(struct g_provider *pp, u_long cmd, void * data, int fflag, struct thread *td)
278 struct g_disk_softc *sc;
285 if (dp->d_ioctl == NULL)
287 g_disk_lock_giant(dp);
288 error = dp->d_ioctl(dp, cmd, data, fflag, td);
289 g_disk_unlock_giant(dp);
294 g_disk_start(struct bio *bp)
296 struct bio *bp2, *bp3;
298 struct g_disk_softc *sc;
302 sc = bp->bio_to->geom->softc;
303 if (sc == NULL || (dp = sc->dp) == NULL || dp->d_destroyed) {
304 g_io_deliver(bp, ENXIO);
308 switch(bp->bio_cmd) {
310 if (!(dp->d_flags & DISKFLAG_CANDELETE)) {
319 bp2 = g_clone_bio(bp);
325 bp2->bio_offset += off;
326 bp2->bio_length -= off;
327 bp2->bio_data += off;
328 if (bp2->bio_length > dp->d_maxsize) {
330 * XXX: If we have a stripesize we should really
333 bp2->bio_length = dp->d_maxsize;
334 off += dp->d_maxsize;
336 * To avoid a race, we need to grab the next bio
337 * before we schedule this one. See "notes".
339 bp3 = g_clone_bio(bp);
341 bp->bio_error = ENOMEM;
343 bp2->bio_done = g_disk_done;
344 bp2->bio_pblkno = bp2->bio_offset / dp->d_sectorsize;
345 bp2->bio_bcount = bp2->bio_length;
347 devstat_start_transaction_bio(dp->d_devstat, bp2);
348 g_disk_lock_giant(dp);
350 g_disk_unlock_giant(dp);
353 } while (bp2 != NULL);
356 /* Give the driver a chance to override */
357 if (dp->d_getattr != NULL) {
358 if (bp->bio_disk == NULL)
360 error = dp->d_getattr(bp);
365 if (g_handleattr_int(bp, "GEOM::candelete",
366 (dp->d_flags & DISKFLAG_CANDELETE) != 0))
368 else if (g_handleattr_int(bp, "GEOM::fwsectors",
371 else if (g_handleattr_int(bp, "GEOM::fwheads", dp->d_fwheads))
373 else if (g_handleattr_off_t(bp, "GEOM::frontstuff", 0))
375 else if (g_handleattr_str(bp, "GEOM::ident", dp->d_ident))
377 else if (g_handleattr(bp, "GEOM::hba_vendor",
378 &dp->d_hba_vendor, 2))
380 else if (g_handleattr(bp, "GEOM::hba_device",
381 &dp->d_hba_device, 2))
383 else if (g_handleattr(bp, "GEOM::hba_subvendor",
384 &dp->d_hba_subvendor, 2))
386 else if (g_handleattr(bp, "GEOM::hba_subdevice",
387 &dp->d_hba_subdevice, 2))
389 else if (!strcmp(bp->bio_attribute, "GEOM::kerneldump"))
390 g_disk_kerneldump(bp, dp);
391 else if (!strcmp(bp->bio_attribute, "GEOM::setstate"))
392 g_disk_setstate(bp, sc);
397 g_trace(G_T_BIO, "g_disk_flushcache(%s)",
399 if (!(dp->d_flags & DISKFLAG_CANFLUSHCACHE)) {
403 bp2 = g_clone_bio(bp);
405 g_io_deliver(bp, ENOMEM);
408 bp2->bio_done = g_disk_done;
410 g_disk_lock_giant(dp);
412 g_disk_unlock_giant(dp);
418 if (error != EJUSTRETURN)
419 g_io_deliver(bp, error);
424 g_disk_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp)
427 struct g_disk_softc *sc;
430 if (sc == NULL || (dp = sc->dp) == NULL)
432 if (indent == NULL) {
433 sbuf_printf(sb, " hd %u", dp->d_fwheads);
434 sbuf_printf(sb, " sc %u", dp->d_fwsectors);
438 sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n",
439 indent, dp->d_fwheads);
440 sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n",
441 indent, dp->d_fwsectors);
442 sbuf_printf(sb, "%s<ident>%s</ident>\n", indent, dp->d_ident);
443 sbuf_printf(sb, "%s<descr>%s</descr>\n", indent, dp->d_descr);
448 g_disk_create(void *arg, int flag)
451 struct g_provider *pp;
453 struct g_disk_softc *sc;
456 if (flag == EV_CANCEL)
460 sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
462 gp = g_new_geomf(&g_disk_class, "%s%d", dp->d_name, dp->d_unit);
464 pp = g_new_providerf(gp, "%s", gp->name);
465 pp->mediasize = dp->d_mediasize;
466 pp->sectorsize = dp->d_sectorsize;
467 if (dp->d_flags & DISKFLAG_CANDELETE)
468 pp->flags |= G_PF_CANDELETE;
469 pp->stripeoffset = dp->d_stripeoffset;
470 pp->stripesize = dp->d_stripesize;
472 printf("GEOM: new disk %s\n", gp->name);
473 sysctl_ctx_init(&sc->sysctl_ctx);
474 snprintf(tmpstr, sizeof(tmpstr), "GEOM disk %s", gp->name);
475 sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
476 SYSCTL_STATIC_CHILDREN(_kern_geom_disk), OID_AUTO, gp->name,
477 CTLFLAG_RD, 0, tmpstr);
478 if (sc->sysctl_tree != NULL) {
479 snprintf(tmpstr, sizeof(tmpstr),
480 "kern.geom.disk.%s.led", gp->name);
481 TUNABLE_STR_FETCH(tmpstr, sc->led, sizeof(sc->led));
482 SYSCTL_ADD_STRING(&sc->sysctl_ctx,
483 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO, "led",
484 CTLFLAG_RW | CTLFLAG_TUN, sc->led, sizeof(sc->led),
489 g_error_provider(pp, 0);
493 * We get this callback after all of the consumers have gone away, and just
494 * before the provider is freed. If the disk driver provided a d_gone
495 * callback, let them know that it is okay to free resources -- they won't
496 * be getting any more accesses from GEOM.
499 g_disk_providergone(struct g_provider *pp)
502 struct g_disk_softc *sc;
504 sc = (struct g_disk_softc *)pp->geom->softc;
507 * If the softc is already NULL, then we've probably been through
508 * g_disk_destroy already; there is nothing for us to do anyway.
516 * FreeBSD 9 started with VERSION_01 of the struct disk structure.
517 * However, g_gone was added in the middle of the branch. To
518 * cope with version being missing from struct disk, we set a flag
519 * in g_disk_create for VERSION_01 and avoid touching the d_gone
520 * field for old consumers.
522 if (!(dp->d_flags & DISKFLAG_LACKS_GONE) && dp->d_gone != NULL)
527 g_disk_destroy(void *ptr, int flag)
531 struct g_disk_softc *sc;
538 if (sc->sysctl_tree != NULL) {
539 sysctl_ctx_free(&sc->sysctl_ctx);
540 sc->sysctl_tree = NULL;
542 if (sc->led[0] != 0) {
543 led_set(sc->led, "0");
548 g_wither_geom(gp, ENXIO);
554 * We only allow printable characters in disk ident,
555 * the rest is converted to 'x<HH>'.
558 g_disk_ident_adjust(char *ident, size_t size)
560 char *p, tmp[4], newid[DISK_IDENT_SIZE];
563 for (p = ident; *p != '\0'; p++) {
568 snprintf(tmp, sizeof(tmp), "x%02hhx",
569 *(unsigned char *)p);
571 if (strlcat(newid, tmp, sizeof(newid)) >= sizeof(newid))
575 strlcpy(ident, newid, size);
582 return (g_malloc(sizeof(struct disk), M_WAITOK | M_ZERO));
586 disk_create(struct disk *dp, int version)
589 if (version != DISK_VERSION_02 && version != DISK_VERSION_01) {
590 printf("WARNING: Attempt to add disk %s%d %s",
591 dp->d_name, dp->d_unit,
592 " using incompatible ABI version of disk(9)\n");
593 printf("WARNING: Ignoring disk %s%d\n",
594 dp->d_name, dp->d_unit);
597 if (version == DISK_VERSION_01)
598 dp->d_flags |= DISKFLAG_LACKS_GONE;
599 KASSERT(dp->d_strategy != NULL, ("disk_create need d_strategy"));
600 KASSERT(dp->d_name != NULL, ("disk_create need d_name"));
601 KASSERT(*dp->d_name != 0, ("disk_create need d_name"));
602 KASSERT(strlen(dp->d_name) < SPECNAMELEN - 4, ("disk name too long"));
603 if (dp->d_devstat == NULL)
604 dp->d_devstat = devstat_new_entry(dp->d_name, dp->d_unit,
605 dp->d_sectorsize, DEVSTAT_ALL_SUPPORTED,
606 DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
608 g_disk_ident_adjust(dp->d_ident, sizeof(dp->d_ident));
609 g_post_event(g_disk_create, dp, M_WAITOK, dp, NULL);
613 disk_destroy(struct disk *dp)
618 if (dp->d_devstat != NULL)
619 devstat_remove_entry(dp->d_devstat);
620 g_post_event(g_disk_destroy, dp, M_WAITOK, NULL);
624 disk_gone(struct disk *dp)
627 struct g_provider *pp;
631 pp = LIST_FIRST(&gp->provider);
633 KASSERT(LIST_NEXT(pp, provider) == NULL,
634 ("geom %p has more than one provider", gp));
635 g_wither_provider(pp, ENXIO);
641 disk_attr_changed(struct disk *dp, const char *attr, int flag)
644 struct g_provider *pp;
648 LIST_FOREACH(pp, &gp->provider, provider)
649 (void)g_attr_changed(pp, attr, flag);
653 disk_media_changed(struct disk *dp, int flag)
656 struct g_provider *pp;
660 pp = LIST_FIRST(&gp->provider);
662 KASSERT(LIST_NEXT(pp, provider) == NULL,
663 ("geom %p has more than one provider", gp));
664 g_media_changed(pp, flag);
670 disk_media_gone(struct disk *dp, int flag)
673 struct g_provider *pp;
677 pp = LIST_FIRST(&gp->provider);
679 KASSERT(LIST_NEXT(pp, provider) == NULL,
680 ("geom %p has more than one provider", gp));
681 g_media_gone(pp, flag);
687 g_kern_disks(void *p, int flag __unused)
696 LIST_FOREACH(gp, &g_disk_class.geom, geom) {
697 sbuf_printf(sb, "%s%s", sp, gp->name);
704 sysctl_disks(SYSCTL_HANDLER_ARGS)
709 sb = sbuf_new_auto();
710 g_waitfor_event(g_kern_disks, sb, M_WAITOK, NULL);
711 error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
716 SYSCTL_PROC(_kern, OID_AUTO, disks,
717 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
718 sysctl_disks, "A", "names of available disks");