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);
142 pp->mediasize = dp->d_mediasize;
143 pp->sectorsize = dp->d_sectorsize;
144 if (dp->d_flags & DISKFLAG_CANDELETE)
145 pp->flags |= G_PF_CANDELETE;
147 pp->flags &= ~G_PF_CANDELETE;
148 pp->stripeoffset = dp->d_stripeoffset;
149 pp->stripesize = dp->d_stripesize;
150 dp->d_flags |= DISKFLAG_OPEN;
151 if (dp->d_maxsize == 0) {
152 printf("WARNING: Disk drive %s%d has no d_maxsize\n",
153 dp->d_name, dp->d_unit);
154 dp->d_maxsize = DFLTPHYS;
156 } else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) {
157 if (dp->d_close != NULL) {
158 g_disk_lock_giant(dp);
159 error = dp->d_close(dp);
161 printf("Closed disk %s -> %d\n",
163 g_disk_unlock_giant(dp);
165 sc->state = G_STATE_ACTIVE;
167 led_set(sc->led, "0");
168 dp->d_flags &= ~DISKFLAG_OPEN;
174 g_disk_kerneldump(struct bio *bp, struct disk *dp)
176 struct g_kerneldump *gkd;
179 gkd = (struct g_kerneldump*)bp->bio_data;
180 gp = bp->bio_to->geom;
181 g_trace(G_T_TOPOLOGY, "g_disk_kernedump(%s, %jd, %jd)",
182 gp->name, (intmax_t)gkd->offset, (intmax_t)gkd->length);
183 if (dp->d_dump == NULL) {
184 g_io_deliver(bp, ENODEV);
187 gkd->di.dumper = dp->d_dump;
189 gkd->di.blocksize = dp->d_sectorsize;
190 gkd->di.maxiosize = dp->d_maxsize;
191 gkd->di.mediaoffset = gkd->offset;
192 if ((gkd->offset + gkd->length) > dp->d_mediasize)
193 gkd->length = dp->d_mediasize - gkd->offset;
194 gkd->di.mediasize = gkd->length;
199 g_disk_setstate(struct bio *bp, struct g_disk_softc *sc)
203 memcpy(&sc->state, bp->bio_data, sizeof(sc->state));
204 if (sc->led[0] != 0) {
209 case G_STATE_REBUILD:
219 led_set(sc->led, cmd);
225 g_disk_done(struct bio *bp)
228 struct g_disk_softc *sc;
230 /* See "notes" for why we need a mutex here */
231 /* XXX: will witness accept a mix of Giant/unGiant drivers here ? */
232 bp2 = bp->bio_parent;
233 sc = bp2->bio_to->private;
234 bp->bio_completed = bp->bio_length - bp->bio_resid;
235 mtx_lock(&sc->done_mtx);
236 if (bp2->bio_error == 0)
237 bp2->bio_error = bp->bio_error;
238 bp2->bio_completed += bp->bio_completed;
239 if ((bp->bio_cmd & (BIO_READ|BIO_WRITE|BIO_DELETE)) != 0)
240 devstat_end_transaction_bio(sc->dp->d_devstat, bp);
243 if (bp2->bio_children == bp2->bio_inbed) {
244 bp2->bio_resid = bp2->bio_bcount - bp2->bio_completed;
245 g_io_deliver(bp2, bp2->bio_error);
247 mtx_unlock(&sc->done_mtx);
251 g_disk_ioctl(struct g_provider *pp, u_long cmd, void * data, int fflag, struct thread *td)
254 struct g_disk_softc *sc;
260 if (dp->d_ioctl == NULL)
262 g_disk_lock_giant(dp);
263 error = dp->d_ioctl(dp, cmd, data, fflag, td);
264 g_disk_unlock_giant(dp);
269 g_disk_start(struct bio *bp)
271 struct bio *bp2, *bp3;
273 struct g_disk_softc *sc;
277 sc = bp->bio_to->private;
278 if (sc == NULL || (dp = sc->dp) == NULL || dp->d_destroyed) {
279 g_io_deliver(bp, ENXIO);
283 switch(bp->bio_cmd) {
285 if (!(dp->d_flags & DISKFLAG_CANDELETE)) {
294 bp2 = g_clone_bio(bp);
300 bp2->bio_offset += off;
301 bp2->bio_length -= off;
302 bp2->bio_data += off;
303 if (bp2->bio_length > dp->d_maxsize) {
305 * XXX: If we have a stripesize we should really
308 bp2->bio_length = dp->d_maxsize;
309 off += dp->d_maxsize;
311 * To avoid a race, we need to grab the next bio
312 * before we schedule this one. See "notes".
314 bp3 = g_clone_bio(bp);
316 bp->bio_error = ENOMEM;
318 bp2->bio_done = g_disk_done;
319 bp2->bio_pblkno = bp2->bio_offset / dp->d_sectorsize;
320 bp2->bio_bcount = bp2->bio_length;
322 devstat_start_transaction_bio(dp->d_devstat, bp2);
323 g_disk_lock_giant(dp);
325 g_disk_unlock_giant(dp);
328 } while (bp2 != NULL);
331 /* Give the driver a chance to override */
332 if (dp->d_getattr != NULL) {
333 if (bp->bio_disk == NULL)
335 error = dp->d_getattr(bp);
340 if (g_handleattr_int(bp, "GEOM::candelete",
341 (dp->d_flags & DISKFLAG_CANDELETE) != 0))
343 else if (g_handleattr_int(bp, "GEOM::fwsectors",
346 else if (g_handleattr_int(bp, "GEOM::fwheads", dp->d_fwheads))
348 else if (g_handleattr_off_t(bp, "GEOM::frontstuff", 0))
350 else if (g_handleattr_str(bp, "GEOM::ident", dp->d_ident))
352 else if (g_handleattr(bp, "GEOM::hba_vendor",
353 &dp->d_hba_vendor, 2))
355 else if (g_handleattr(bp, "GEOM::hba_device",
356 &dp->d_hba_device, 2))
358 else if (g_handleattr(bp, "GEOM::hba_subvendor",
359 &dp->d_hba_subvendor, 2))
361 else if (g_handleattr(bp, "GEOM::hba_subdevice",
362 &dp->d_hba_subdevice, 2))
364 else if (!strcmp(bp->bio_attribute, "GEOM::kerneldump"))
365 g_disk_kerneldump(bp, dp);
366 else if (!strcmp(bp->bio_attribute, "GEOM::setstate"))
367 g_disk_setstate(bp, sc);
372 g_trace(G_T_BIO, "g_disk_flushcache(%s)",
374 if (!(dp->d_flags & DISKFLAG_CANFLUSHCACHE)) {
378 bp2 = g_clone_bio(bp);
380 g_io_deliver(bp, ENOMEM);
383 bp2->bio_done = g_disk_done;
385 g_disk_lock_giant(dp);
387 g_disk_unlock_giant(dp);
393 if (error != EJUSTRETURN)
394 g_io_deliver(bp, error);
399 g_disk_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp)
402 struct g_disk_softc *sc;
405 if (sc == NULL || (dp = sc->dp) == NULL)
407 if (indent == NULL) {
408 sbuf_printf(sb, " hd %u", dp->d_fwheads);
409 sbuf_printf(sb, " sc %u", dp->d_fwsectors);
413 sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n",
414 indent, dp->d_fwheads);
415 sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n",
416 indent, dp->d_fwsectors);
417 sbuf_printf(sb, "%s<ident>%s</ident>\n", indent, dp->d_ident);
418 sbuf_printf(sb, "%s<descr>%s</descr>\n", indent, dp->d_descr);
423 g_disk_create(void *arg, int flag)
426 struct g_provider *pp;
428 struct g_disk_softc *sc;
431 if (flag == EV_CANCEL)
435 sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
436 mtx_init(&sc->done_mtx, "g_disk_done", NULL, MTX_DEF);
438 gp = g_new_geomf(&g_disk_class, "%s%d", dp->d_name, dp->d_unit);
440 pp = g_new_providerf(gp, "%s", gp->name);
441 pp->mediasize = dp->d_mediasize;
442 pp->sectorsize = dp->d_sectorsize;
443 if (dp->d_flags & DISKFLAG_CANDELETE)
444 pp->flags |= G_PF_CANDELETE;
445 pp->stripeoffset = dp->d_stripeoffset;
446 pp->stripesize = dp->d_stripesize;
448 printf("GEOM: new disk %s\n", gp->name);
449 sysctl_ctx_init(&sc->sysctl_ctx);
450 snprintf(tmpstr, sizeof(tmpstr), "GEOM disk %s", gp->name);
451 sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
452 SYSCTL_STATIC_CHILDREN(_kern_geom_disk), OID_AUTO, gp->name,
453 CTLFLAG_RD, 0, tmpstr);
454 if (sc->sysctl_tree != NULL) {
455 snprintf(tmpstr, sizeof(tmpstr),
456 "kern.geom.disk.%s.led", gp->name);
457 TUNABLE_STR_FETCH(tmpstr, sc->led, sizeof(sc->led));
458 SYSCTL_ADD_STRING(&sc->sysctl_ctx,
459 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO, "led",
460 CTLFLAG_RW | CTLFLAG_TUN, sc->led, sizeof(sc->led),
465 g_error_provider(pp, 0);
469 * We get this callback after all of the consumers have gone away, and just
470 * before the provider is freed. If the disk driver provided a d_gone
471 * callback, let them know that it is okay to free resources -- they won't
472 * be getting any more accesses from GEOM.
475 g_disk_providergone(struct g_provider *pp)
478 struct g_disk_softc *sc;
480 sc = (struct g_disk_softc *)pp->private;
484 * FreeBSD 9 started with VERSION_01 of the struct disk structure.
485 * However, g_gone was added in the middle of the branch. To
486 * cope with version being missing from struct disk, we set a flag
487 * in g_disk_create for VERSION_01 and avoid touching the d_gone
488 * field for old consumers.
490 if (dp != NULL && (dp->d_flags & DISKFLAG_LACKS_GONE) == 0 &&
493 if (sc->sysctl_tree != NULL) {
494 sysctl_ctx_free(&sc->sysctl_ctx);
495 sc->sysctl_tree = NULL;
497 if (sc->led[0] != 0) {
498 led_set(sc->led, "0");
502 pp->geom->softc = NULL;
503 mtx_destroy(&sc->done_mtx);
508 g_disk_destroy(void *ptr, int flag)
512 struct g_disk_softc *sc;
522 g_wither_geom(gp, ENXIO);
528 * We only allow printable characters in disk ident,
529 * the rest is converted to 'x<HH>'.
532 g_disk_ident_adjust(char *ident, size_t size)
534 char *p, tmp[4], newid[DISK_IDENT_SIZE];
537 for (p = ident; *p != '\0'; p++) {
542 snprintf(tmp, sizeof(tmp), "x%02hhx",
543 *(unsigned char *)p);
545 if (strlcat(newid, tmp, sizeof(newid)) >= sizeof(newid))
549 strlcpy(ident, newid, size);
556 return (g_malloc(sizeof(struct disk), M_WAITOK | M_ZERO));
560 disk_create(struct disk *dp, int version)
563 if (version != DISK_VERSION_02 && version != DISK_VERSION_01) {
564 printf("WARNING: Attempt to add disk %s%d %s",
565 dp->d_name, dp->d_unit,
566 " using incompatible ABI version of disk(9)\n");
567 printf("WARNING: Ignoring disk %s%d\n",
568 dp->d_name, dp->d_unit);
571 if (version == DISK_VERSION_01)
572 dp->d_flags |= DISKFLAG_LACKS_GONE;
573 KASSERT(dp->d_strategy != NULL, ("disk_create need d_strategy"));
574 KASSERT(dp->d_name != NULL, ("disk_create need d_name"));
575 KASSERT(*dp->d_name != 0, ("disk_create need d_name"));
576 KASSERT(strlen(dp->d_name) < SPECNAMELEN - 4, ("disk name too long"));
577 if (dp->d_devstat == NULL)
578 dp->d_devstat = devstat_new_entry(dp->d_name, dp->d_unit,
579 dp->d_sectorsize, DEVSTAT_ALL_SUPPORTED,
580 DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
582 g_disk_ident_adjust(dp->d_ident, sizeof(dp->d_ident));
583 g_post_event(g_disk_create, dp, M_WAITOK, dp, NULL);
587 disk_destroy(struct disk *dp)
592 if (dp->d_devstat != NULL)
593 devstat_remove_entry(dp->d_devstat);
594 g_post_event(g_disk_destroy, dp, M_WAITOK, NULL);
598 disk_gone(struct disk *dp)
601 struct g_provider *pp;
605 pp = LIST_FIRST(&gp->provider);
607 KASSERT(LIST_NEXT(pp, provider) == NULL,
608 ("geom %p has more than one provider", gp));
609 g_wither_provider(pp, ENXIO);
615 disk_attr_changed(struct disk *dp, const char *attr, int flag)
618 struct g_provider *pp;
622 LIST_FOREACH(pp, &gp->provider, provider)
623 (void)g_attr_changed(pp, attr, flag);
627 disk_media_changed(struct disk *dp, int flag)
630 struct g_provider *pp;
634 pp = LIST_FIRST(&gp->provider);
636 KASSERT(LIST_NEXT(pp, provider) == NULL,
637 ("geom %p has more than one provider", gp));
638 g_media_changed(pp, flag);
644 disk_media_gone(struct disk *dp, int flag)
647 struct g_provider *pp;
651 pp = LIST_FIRST(&gp->provider);
653 KASSERT(LIST_NEXT(pp, provider) == NULL,
654 ("geom %p has more than one provider", gp));
655 g_media_gone(pp, flag);
661 g_kern_disks(void *p, int flag __unused)
670 LIST_FOREACH(gp, &g_disk_class.geom, geom) {
671 sbuf_printf(sb, "%s%s", sp, gp->name);
678 sysctl_disks(SYSCTL_HANDLER_ARGS)
683 sb = sbuf_new_auto();
684 g_waitfor_event(g_kern_disks, sb, M_WAITOK, NULL);
685 error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
690 SYSCTL_PROC(_kern, OID_AUTO, disks,
691 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
692 sysctl_disks, "A", "names of available disks");