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 = {
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);
144 pp->mediasize = dp->d_mediasize;
145 pp->sectorsize = dp->d_sectorsize;
146 if (dp->d_flags & DISKFLAG_CANDELETE)
147 pp->flags |= G_PF_CANDELETE;
149 pp->flags &= ~G_PF_CANDELETE;
150 pp->stripeoffset = dp->d_stripeoffset;
151 pp->stripesize = dp->d_stripesize;
152 dp->d_flags |= DISKFLAG_OPEN;
153 if (dp->d_maxsize == 0) {
154 printf("WARNING: Disk drive %s%d has no d_maxsize\n",
155 dp->d_name, dp->d_unit);
156 dp->d_maxsize = DFLTPHYS;
158 if (dp->d_delmaxsize == 0) {
159 if (bootverbose && dp->d_flags & DISKFLAG_CANDELETE) {
160 printf("WARNING: Disk drive %s%d has no "
161 "d_delmaxsize\n", dp->d_name, dp->d_unit);
163 dp->d_delmaxsize = dp->d_maxsize;
165 } else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) {
166 if (dp->d_close != NULL) {
167 g_disk_lock_giant(dp);
168 error = dp->d_close(dp);
170 printf("Closed disk %s -> %d\n",
172 g_disk_unlock_giant(dp);
174 sc->state = G_STATE_ACTIVE;
176 led_set(sc->led, "0");
177 dp->d_flags &= ~DISKFLAG_OPEN;
183 g_disk_kerneldump(struct bio *bp, struct disk *dp)
185 struct g_kerneldump *gkd;
188 gkd = (struct g_kerneldump*)bp->bio_data;
189 gp = bp->bio_to->geom;
190 g_trace(G_T_TOPOLOGY, "g_disk_kerneldump(%s, %jd, %jd)",
191 gp->name, (intmax_t)gkd->offset, (intmax_t)gkd->length);
192 if (dp->d_dump == NULL) {
193 g_io_deliver(bp, ENODEV);
196 gkd->di.dumper = dp->d_dump;
198 gkd->di.blocksize = dp->d_sectorsize;
199 gkd->di.maxiosize = dp->d_maxsize;
200 gkd->di.mediaoffset = gkd->offset;
201 if ((gkd->offset + gkd->length) > dp->d_mediasize)
202 gkd->length = dp->d_mediasize - gkd->offset;
203 gkd->di.mediasize = gkd->length;
208 g_disk_setstate(struct bio *bp, struct g_disk_softc *sc)
212 memcpy(&sc->state, bp->bio_data, sizeof(sc->state));
213 if (sc->led[0] != 0) {
218 case G_STATE_REBUILD:
228 led_set(sc->led, cmd);
234 g_disk_done(struct bio *bp)
238 struct g_disk_softc *sc;
240 /* See "notes" for why we need a mutex here */
241 /* XXX: will witness accept a mix of Giant/unGiant drivers here ? */
242 bp2 = bp->bio_parent;
243 sc = bp2->bio_to->private;
244 bp->bio_completed = bp->bio_length - bp->bio_resid;
246 mtx_lock(&sc->done_mtx);
247 if (bp2->bio_error == 0)
248 bp2->bio_error = bp->bio_error;
249 bp2->bio_completed += bp->bio_completed;
250 if ((bp->bio_cmd & (BIO_READ|BIO_WRITE|BIO_DELETE)) != 0)
251 devstat_end_transaction_bio_bt(sc->dp->d_devstat, bp, &now);
253 if (bp2->bio_children == bp2->bio_inbed) {
254 mtx_unlock(&sc->done_mtx);
255 bp2->bio_resid = bp2->bio_bcount - bp2->bio_completed;
256 g_io_deliver(bp2, bp2->bio_error);
258 mtx_unlock(&sc->done_mtx);
263 g_disk_ioctl(struct g_provider *pp, u_long cmd, void * data, int fflag, struct thread *td)
266 struct g_disk_softc *sc;
272 if (dp->d_ioctl == NULL)
274 g_disk_lock_giant(dp);
275 error = dp->d_ioctl(dp, cmd, data, fflag, td);
276 g_disk_unlock_giant(dp);
281 g_disk_start(struct bio *bp)
283 struct bio *bp2, *bp3;
285 struct g_disk_softc *sc;
289 sc = bp->bio_to->private;
290 if (sc == NULL || (dp = sc->dp) == NULL || dp->d_destroyed) {
291 g_io_deliver(bp, ENXIO);
295 switch(bp->bio_cmd) {
297 if (!(dp->d_flags & DISKFLAG_CANDELETE)) {
306 bp2 = g_clone_bio(bp);
314 d_maxsize = (bp->bio_cmd == BIO_DELETE &&
315 (dp->d_flags & DISKFLAG_LACKS_DELMAX) == 0) ?
316 dp->d_delmaxsize : dp->d_maxsize;
318 bp2->bio_offset += off;
319 bp2->bio_length -= off;
320 if ((bp->bio_flags & BIO_UNMAPPED) == 0) {
321 bp2->bio_data += off;
323 KASSERT((dp->d_flags & DISKFLAG_UNMAPPED_BIO)
325 ("unmapped bio not supported by disk %s",
327 bp2->bio_ma += off / PAGE_SIZE;
328 bp2->bio_ma_offset += off;
329 bp2->bio_ma_offset %= PAGE_SIZE;
330 bp2->bio_ma_n -= off / PAGE_SIZE;
332 if (bp2->bio_length > d_maxsize) {
334 * XXX: If we have a stripesize we should really
335 * use it here. Care should be taken in the delete
336 * case if this is done as deletes can be very
337 * sensitive to size given how they are processed.
339 bp2->bio_length = d_maxsize;
340 if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
341 bp2->bio_ma_n = howmany(
343 bp2->bio_length, PAGE_SIZE);
347 * To avoid a race, we need to grab the next bio
348 * before we schedule this one. See "notes".
350 bp3 = g_clone_bio(bp);
352 bp->bio_error = ENOMEM;
354 bp2->bio_done = g_disk_done;
355 bp2->bio_pblkno = bp2->bio_offset / dp->d_sectorsize;
356 bp2->bio_bcount = bp2->bio_length;
358 devstat_start_transaction_bio(dp->d_devstat, bp2);
359 g_disk_lock_giant(dp);
361 g_disk_unlock_giant(dp);
364 } while (bp2 != NULL);
367 /* Give the driver a chance to override */
368 if (dp->d_getattr != NULL) {
369 if (bp->bio_disk == NULL)
371 error = dp->d_getattr(bp);
376 if (g_handleattr_int(bp, "GEOM::candelete",
377 (dp->d_flags & DISKFLAG_CANDELETE) != 0))
379 else if (g_handleattr_int(bp, "GEOM::fwsectors",
382 else if (g_handleattr_int(bp, "GEOM::fwheads", dp->d_fwheads))
384 else if (g_handleattr_off_t(bp, "GEOM::frontstuff", 0))
386 else if (g_handleattr_str(bp, "GEOM::ident", dp->d_ident))
388 else if (g_handleattr(bp, "GEOM::hba_vendor",
389 &dp->d_hba_vendor, 2))
391 else if (g_handleattr(bp, "GEOM::hba_device",
392 &dp->d_hba_device, 2))
394 else if (g_handleattr(bp, "GEOM::hba_subvendor",
395 &dp->d_hba_subvendor, 2))
397 else if (g_handleattr(bp, "GEOM::hba_subdevice",
398 &dp->d_hba_subdevice, 2))
400 else if (!strcmp(bp->bio_attribute, "GEOM::kerneldump"))
401 g_disk_kerneldump(bp, dp);
402 else if (!strcmp(bp->bio_attribute, "GEOM::setstate"))
403 g_disk_setstate(bp, sc);
408 g_trace(G_T_BIO, "g_disk_flushcache(%s)",
410 if (!(dp->d_flags & DISKFLAG_CANFLUSHCACHE)) {
414 bp2 = g_clone_bio(bp);
416 g_io_deliver(bp, ENOMEM);
419 bp2->bio_done = g_disk_done;
421 g_disk_lock_giant(dp);
423 g_disk_unlock_giant(dp);
429 if (error != EJUSTRETURN)
430 g_io_deliver(bp, error);
435 g_disk_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp)
439 struct g_disk_softc *sc;
444 if (sc == NULL || (dp = sc->dp) == NULL)
446 if (indent == NULL) {
447 sbuf_printf(sb, " hd %u", dp->d_fwheads);
448 sbuf_printf(sb, " sc %u", dp->d_fwsectors);
452 sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n",
453 indent, dp->d_fwheads);
454 sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n",
455 indent, dp->d_fwsectors);
456 if (dp->d_getattr != NULL) {
457 buf = g_malloc(DISK_IDENT_SIZE, M_WAITOK);
460 bp->bio_attribute = "GEOM::ident";
461 bp->bio_length = DISK_IDENT_SIZE;
463 res = dp->d_getattr(bp);
464 sbuf_printf(sb, "%s<ident>", indent);
465 g_conf_printf_escaped(sb, "%s",
466 res == 0 ? buf: dp->d_ident);
467 sbuf_printf(sb, "</ident>\n");
468 bp->bio_attribute = "GEOM::lunid";
469 bp->bio_length = DISK_IDENT_SIZE;
471 if (dp->d_getattr(bp) == 0) {
472 sbuf_printf(sb, "%s<lunid>", indent);
473 g_conf_printf_escaped(sb, "%s", buf);
474 sbuf_printf(sb, "</lunid>\n");
476 bp->bio_attribute = "GEOM::lunname";
477 bp->bio_length = DISK_IDENT_SIZE;
479 if (dp->d_getattr(bp) == 0) {
480 sbuf_printf(sb, "%s<lunname>", indent);
481 g_conf_printf_escaped(sb, "%s", buf);
482 sbuf_printf(sb, "</lunname>\n");
487 sbuf_printf(sb, "%s<ident>", indent);
488 g_conf_printf_escaped(sb, "%s", dp->d_ident);
489 sbuf_printf(sb, "</ident>\n");
491 sbuf_printf(sb, "%s<descr>", indent);
492 g_conf_printf_escaped(sb, "%s", dp->d_descr);
493 sbuf_printf(sb, "</descr>\n");
498 g_disk_create(void *arg, int flag)
501 struct g_provider *pp;
503 struct g_disk_softc *sc;
506 if (flag == EV_CANCEL)
510 sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
511 mtx_init(&sc->done_mtx, "g_disk_done", NULL, MTX_DEF);
513 gp = g_new_geomf(&g_disk_class, "%s%d", dp->d_name, dp->d_unit);
515 pp = g_new_providerf(gp, "%s", gp->name);
516 pp->mediasize = dp->d_mediasize;
517 pp->sectorsize = dp->d_sectorsize;
518 if (dp->d_flags & DISKFLAG_CANDELETE)
519 pp->flags |= G_PF_CANDELETE;
520 pp->stripeoffset = dp->d_stripeoffset;
521 pp->stripesize = dp->d_stripesize;
522 if ((dp->d_flags & DISKFLAG_UNMAPPED_BIO) != 0)
523 pp->flags |= G_PF_ACCEPT_UNMAPPED;
525 printf("GEOM: new disk %s\n", gp->name);
526 sysctl_ctx_init(&sc->sysctl_ctx);
527 snprintf(tmpstr, sizeof(tmpstr), "GEOM disk %s", gp->name);
528 sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
529 SYSCTL_STATIC_CHILDREN(_kern_geom_disk), OID_AUTO, gp->name,
530 CTLFLAG_RD, 0, tmpstr);
531 if (sc->sysctl_tree != NULL) {
532 snprintf(tmpstr, sizeof(tmpstr),
533 "kern.geom.disk.%s.led", gp->name);
534 TUNABLE_STR_FETCH(tmpstr, sc->led, sizeof(sc->led));
535 SYSCTL_ADD_STRING(&sc->sysctl_ctx,
536 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO, "led",
537 CTLFLAG_RW | CTLFLAG_TUN, sc->led, sizeof(sc->led),
542 g_error_provider(pp, 0);
546 * We get this callback after all of the consumers have gone away, and just
547 * before the provider is freed. If the disk driver provided a d_gone
548 * callback, let them know that it is okay to free resources -- they won't
549 * be getting any more accesses from GEOM.
552 g_disk_providergone(struct g_provider *pp)
555 struct g_disk_softc *sc;
557 sc = (struct g_disk_softc *)pp->private;
561 * FreeBSD 9 started with VERSION_01 of the struct disk structure.
562 * However, g_gone was added in the middle of the branch. To
563 * cope with version being missing from struct disk, we set a flag
564 * in g_disk_create for VERSION_01 and avoid touching the d_gone
565 * field for old consumers.
567 if (dp != NULL && (dp->d_flags & DISKFLAG_LACKS_GONE) == 0 &&
570 if (sc->sysctl_tree != NULL) {
571 sysctl_ctx_free(&sc->sysctl_ctx);
572 sc->sysctl_tree = NULL;
574 if (sc->led[0] != 0) {
575 led_set(sc->led, "0");
579 pp->geom->softc = NULL;
580 mtx_destroy(&sc->done_mtx);
585 g_disk_destroy(void *ptr, int flag)
589 struct g_disk_softc *sc;
599 g_wither_geom(gp, ENXIO);
605 * We only allow printable characters in disk ident,
606 * the rest is converted to 'x<HH>'.
609 g_disk_ident_adjust(char *ident, size_t size)
611 char *p, tmp[4], newid[DISK_IDENT_SIZE];
614 for (p = ident; *p != '\0'; p++) {
619 snprintf(tmp, sizeof(tmp), "x%02hhx",
620 *(unsigned char *)p);
622 if (strlcat(newid, tmp, sizeof(newid)) >= sizeof(newid))
626 strlcpy(ident, newid, size);
633 return (g_malloc(sizeof(struct disk), M_WAITOK | M_ZERO));
637 disk_create(struct disk *dp, int version)
640 if (version != DISK_VERSION_03 && version != DISK_VERSION_02 &&
641 version != DISK_VERSION_01) {
642 printf("WARNING: Attempt to add disk %s%d %s",
643 dp->d_name, dp->d_unit,
644 " using incompatible ABI version of disk(9)\n");
645 printf("WARNING: Ignoring disk %s%d\n",
646 dp->d_name, dp->d_unit);
649 if (version == DISK_VERSION_01)
650 dp->d_flags |= DISKFLAG_LACKS_GONE;
651 if (version < DISK_VERSION_03)
652 dp->d_flags |= DISKFLAG_LACKS_DELMAX;
653 KASSERT(dp->d_strategy != NULL, ("disk_create need d_strategy"));
654 KASSERT(dp->d_name != NULL, ("disk_create need d_name"));
655 KASSERT(*dp->d_name != 0, ("disk_create need d_name"));
656 KASSERT(strlen(dp->d_name) < SPECNAMELEN - 4, ("disk name too long"));
657 if (dp->d_devstat == NULL)
658 dp->d_devstat = devstat_new_entry(dp->d_name, dp->d_unit,
659 dp->d_sectorsize, DEVSTAT_ALL_SUPPORTED,
660 DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
662 g_disk_ident_adjust(dp->d_ident, sizeof(dp->d_ident));
663 g_post_event(g_disk_create, dp, M_WAITOK, dp, NULL);
667 disk_destroy(struct disk *dp)
672 if (dp->d_devstat != NULL)
673 devstat_remove_entry(dp->d_devstat);
674 g_post_event(g_disk_destroy, dp, M_WAITOK, NULL);
678 disk_gone(struct disk *dp)
681 struct g_provider *pp;
685 pp = LIST_FIRST(&gp->provider);
687 KASSERT(LIST_NEXT(pp, provider) == NULL,
688 ("geom %p has more than one provider", gp));
689 g_wither_provider(pp, ENXIO);
695 disk_attr_changed(struct disk *dp, const char *attr, int flag)
698 struct g_provider *pp;
702 LIST_FOREACH(pp, &gp->provider, provider)
703 (void)g_attr_changed(pp, attr, flag);
707 disk_media_changed(struct disk *dp, int flag)
710 struct g_provider *pp;
714 pp = LIST_FIRST(&gp->provider);
716 KASSERT(LIST_NEXT(pp, provider) == NULL,
717 ("geom %p has more than one provider", gp));
718 g_media_changed(pp, flag);
724 disk_media_gone(struct disk *dp, int flag)
727 struct g_provider *pp;
731 pp = LIST_FIRST(&gp->provider);
733 KASSERT(LIST_NEXT(pp, provider) == NULL,
734 ("geom %p has more than one provider", gp));
735 g_media_gone(pp, flag);
741 g_kern_disks(void *p, int flag __unused)
750 LIST_FOREACH(gp, &g_disk_class.geom, geom) {
751 sbuf_printf(sb, "%s%s", sp, gp->name);
758 sysctl_disks(SYSCTL_HANDLER_ARGS)
763 sb = sbuf_new_auto();
764 g_waitfor_event(g_kern_disks, sb, M_WAITOK, NULL);
765 error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
770 SYSCTL_PROC(_kern, OID_AUTO, disks,
771 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
772 sysctl_disks, "A", "names of available disks");