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;
72 static g_access_t g_disk_access;
73 static g_start_t g_disk_start;
74 static g_ioctl_t g_disk_ioctl;
75 static g_dumpconf_t g_disk_dumpconf;
76 static g_provgone_t g_disk_providergone;
78 static struct g_class g_disk_class = {
79 .name = G_DISK_CLASS_NAME,
81 .start = g_disk_start,
82 .access = g_disk_access,
83 .ioctl = g_disk_ioctl,
84 .providergone = g_disk_providergone,
85 .dumpconf = g_disk_dumpconf,
88 SYSCTL_DECL(_kern_geom);
89 static SYSCTL_NODE(_kern_geom, OID_AUTO, disk, CTLFLAG_RW, 0,
92 DECLARE_GEOM_CLASS(g_disk_class, g_disk);
95 g_disk_lock_giant(struct disk *dp)
98 if (dp->d_flags & DISKFLAG_NEEDSGIANT)
103 g_disk_unlock_giant(struct disk *dp)
106 if (dp->d_flags & DISKFLAG_NEEDSGIANT)
111 g_disk_access(struct g_provider *pp, int r, int w, int e)
114 struct g_disk_softc *sc;
117 g_trace(G_T_ACCESS, "g_disk_access(%s, %d, %d, %d)",
121 if (sc == NULL || (dp = sc->dp) == NULL || dp->d_destroyed) {
123 * Allow decreasing access count even if disk is not
126 if (r <= 0 && w <= 0 && e <= 0)
134 if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) {
135 if (dp->d_open != NULL) {
136 g_disk_lock_giant(dp);
137 error = dp->d_open(dp);
138 if (bootverbose && error != 0)
139 printf("Opened disk %s -> %d\n",
141 g_disk_unlock_giant(dp);
145 pp->mediasize = dp->d_mediasize;
146 pp->sectorsize = dp->d_sectorsize;
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 if (dp->d_delmaxsize == 0) {
153 if (bootverbose && dp->d_flags & DISKFLAG_CANDELETE) {
154 printf("WARNING: Disk drive %s%d has no "
155 "d_delmaxsize\n", dp->d_name, dp->d_unit);
157 dp->d_delmaxsize = dp->d_maxsize;
159 pp->stripeoffset = dp->d_stripeoffset;
160 pp->stripesize = dp->d_stripesize;
161 dp->d_flags |= DISKFLAG_OPEN;
162 } else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) {
163 if (dp->d_close != NULL) {
164 g_disk_lock_giant(dp);
165 error = dp->d_close(dp);
167 printf("Closed disk %s -> %d\n",
169 g_disk_unlock_giant(dp);
171 sc->state = G_STATE_ACTIVE;
173 led_set(sc->led, "0");
174 dp->d_flags &= ~DISKFLAG_OPEN;
180 g_disk_kerneldump(struct bio *bp, struct disk *dp)
182 struct g_kerneldump *gkd;
185 gkd = (struct g_kerneldump*)bp->bio_data;
186 gp = bp->bio_to->geom;
187 g_trace(G_T_TOPOLOGY, "g_disk_kerneldump(%s, %jd, %jd)",
188 gp->name, (intmax_t)gkd->offset, (intmax_t)gkd->length);
189 if (dp->d_dump == NULL) {
190 g_io_deliver(bp, ENODEV);
193 gkd->di.dumper = dp->d_dump;
195 gkd->di.blocksize = dp->d_sectorsize;
196 gkd->di.maxiosize = dp->d_maxsize;
197 gkd->di.mediaoffset = gkd->offset;
198 if ((gkd->offset + gkd->length) > dp->d_mediasize)
199 gkd->length = dp->d_mediasize - gkd->offset;
200 gkd->di.mediasize = gkd->length;
205 g_disk_setstate(struct bio *bp, struct g_disk_softc *sc)
209 memcpy(&sc->state, bp->bio_data, sizeof(sc->state));
210 if (sc->led[0] != 0) {
215 case G_STATE_REBUILD:
225 led_set(sc->led, cmd);
231 g_disk_done(struct bio *bp)
235 struct g_disk_softc *sc;
237 /* See "notes" for why we need a mutex here */
238 /* XXX: will witness accept a mix of Giant/unGiant drivers here ? */
239 bp2 = bp->bio_parent;
240 sc = bp2->bio_to->private;
241 bp->bio_completed = bp->bio_length - bp->bio_resid;
243 mtx_lock(&sc->done_mtx);
244 if (bp2->bio_error == 0)
245 bp2->bio_error = bp->bio_error;
246 bp2->bio_completed += bp->bio_completed;
247 if ((bp->bio_cmd & (BIO_READ|BIO_WRITE|BIO_DELETE)) != 0)
248 devstat_end_transaction_bio_bt(sc->dp->d_devstat, bp, &now);
250 if (bp2->bio_children == bp2->bio_inbed) {
251 mtx_unlock(&sc->done_mtx);
252 bp2->bio_resid = bp2->bio_bcount - bp2->bio_completed;
253 g_io_deliver(bp2, bp2->bio_error);
255 mtx_unlock(&sc->done_mtx);
260 g_disk_done_single(struct bio *bp)
263 struct g_disk_softc *sc;
265 bp->bio_completed = bp->bio_length - bp->bio_resid;
266 bp->bio_done = (void *)bp->bio_to;
267 bp->bio_to = LIST_FIRST(&bp->bio_disk->d_geom->provider);
268 if ((bp->bio_cmd & (BIO_READ|BIO_WRITE|BIO_DELETE)) != 0) {
270 sc = bp->bio_to->private;
271 mtx_lock(&sc->done_mtx);
272 devstat_end_transaction_bio_bt(sc->dp->d_devstat, bp, &now);
273 mtx_unlock(&sc->done_mtx);
275 g_io_deliver(bp, bp->bio_error);
279 g_disk_ioctl(struct g_provider *pp, u_long cmd, void * data, int fflag, struct thread *td)
282 struct g_disk_softc *sc;
288 if (dp->d_ioctl == NULL)
290 g_disk_lock_giant(dp);
291 error = dp->d_ioctl(dp, cmd, data, fflag, td);
292 g_disk_unlock_giant(dp);
297 g_disk_start(struct bio *bp)
299 struct bio *bp2, *bp3;
301 struct g_disk_softc *sc;
303 off_t d_maxsize, off;
305 sc = bp->bio_to->private;
306 if (sc == NULL || (dp = sc->dp) == NULL || dp->d_destroyed) {
307 g_io_deliver(bp, ENXIO);
311 switch(bp->bio_cmd) {
313 if (!(dp->d_flags & DISKFLAG_CANDELETE)) {
320 d_maxsize = (bp->bio_cmd == BIO_DELETE) ?
321 dp->d_delmaxsize : dp->d_maxsize;
322 if (bp->bio_length <= d_maxsize) {
324 bp->bio_to = (void *)bp->bio_done;
325 bp->bio_done = g_disk_done_single;
326 bp->bio_pblkno = bp->bio_offset / dp->d_sectorsize;
327 bp->bio_bcount = bp->bio_length;
328 mtx_lock(&sc->start_mtx);
329 devstat_start_transaction_bio(dp->d_devstat, bp);
330 mtx_unlock(&sc->start_mtx);
331 g_disk_lock_giant(dp);
333 g_disk_unlock_giant(dp);
338 bp2 = g_clone_bio(bp);
344 bp2->bio_offset += off;
345 bp2->bio_length -= off;
346 if ((bp->bio_flags & BIO_UNMAPPED) == 0) {
347 bp2->bio_data += off;
349 KASSERT((dp->d_flags & DISKFLAG_UNMAPPED_BIO)
351 ("unmapped bio not supported by disk %s",
353 bp2->bio_ma += off / PAGE_SIZE;
354 bp2->bio_ma_offset += off;
355 bp2->bio_ma_offset %= PAGE_SIZE;
356 bp2->bio_ma_n -= off / PAGE_SIZE;
358 if (bp2->bio_length > d_maxsize) {
360 * XXX: If we have a stripesize we should really
361 * use it here. Care should be taken in the delete
362 * case if this is done as deletes can be very
363 * sensitive to size given how they are processed.
365 bp2->bio_length = d_maxsize;
366 if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
367 bp2->bio_ma_n = howmany(
369 bp2->bio_length, PAGE_SIZE);
373 * To avoid a race, we need to grab the next bio
374 * before we schedule this one. See "notes".
376 bp3 = g_clone_bio(bp);
378 bp->bio_error = ENOMEM;
380 bp2->bio_done = g_disk_done;
381 bp2->bio_pblkno = bp2->bio_offset / dp->d_sectorsize;
382 bp2->bio_bcount = bp2->bio_length;
384 mtx_lock(&sc->start_mtx);
385 devstat_start_transaction_bio(dp->d_devstat, bp2);
386 mtx_unlock(&sc->start_mtx);
387 g_disk_lock_giant(dp);
389 g_disk_unlock_giant(dp);
392 } while (bp2 != NULL);
395 /* Give the driver a chance to override */
396 if (dp->d_getattr != NULL) {
397 if (bp->bio_disk == NULL)
399 error = dp->d_getattr(bp);
404 if (g_handleattr_int(bp, "GEOM::candelete",
405 (dp->d_flags & DISKFLAG_CANDELETE) != 0))
407 else if (g_handleattr_int(bp, "GEOM::fwsectors",
410 else if (g_handleattr_int(bp, "GEOM::fwheads", dp->d_fwheads))
412 else if (g_handleattr_off_t(bp, "GEOM::frontstuff", 0))
414 else if (g_handleattr_str(bp, "GEOM::ident", dp->d_ident))
416 else if (g_handleattr(bp, "GEOM::hba_vendor",
417 &dp->d_hba_vendor, 2))
419 else if (g_handleattr(bp, "GEOM::hba_device",
420 &dp->d_hba_device, 2))
422 else if (g_handleattr(bp, "GEOM::hba_subvendor",
423 &dp->d_hba_subvendor, 2))
425 else if (g_handleattr(bp, "GEOM::hba_subdevice",
426 &dp->d_hba_subdevice, 2))
428 else if (!strcmp(bp->bio_attribute, "GEOM::kerneldump"))
429 g_disk_kerneldump(bp, dp);
430 else if (!strcmp(bp->bio_attribute, "GEOM::setstate"))
431 g_disk_setstate(bp, sc);
436 g_trace(G_T_BIO, "g_disk_flushcache(%s)",
438 if (!(dp->d_flags & DISKFLAG_CANFLUSHCACHE)) {
443 bp->bio_to = (void *)bp->bio_done;
444 bp->bio_done = g_disk_done_single;
445 g_disk_lock_giant(dp);
447 g_disk_unlock_giant(dp);
453 if (error != EJUSTRETURN)
454 g_io_deliver(bp, error);
459 g_disk_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp)
463 struct g_disk_softc *sc;
468 if (sc == NULL || (dp = sc->dp) == NULL)
470 if (indent == NULL) {
471 sbuf_printf(sb, " hd %u", dp->d_fwheads);
472 sbuf_printf(sb, " sc %u", dp->d_fwsectors);
476 sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n",
477 indent, dp->d_fwheads);
478 sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n",
479 indent, dp->d_fwsectors);
480 if (dp->d_getattr != NULL) {
481 buf = g_malloc(DISK_IDENT_SIZE, M_WAITOK);
484 bp->bio_attribute = "GEOM::ident";
485 bp->bio_length = DISK_IDENT_SIZE;
487 res = dp->d_getattr(bp);
488 sbuf_printf(sb, "%s<ident>", indent);
489 g_conf_printf_escaped(sb, "%s",
490 res == 0 ? buf: dp->d_ident);
491 sbuf_printf(sb, "</ident>\n");
492 bp->bio_attribute = "GEOM::lunid";
493 bp->bio_length = DISK_IDENT_SIZE;
495 if (dp->d_getattr(bp) == 0) {
496 sbuf_printf(sb, "%s<lunid>", indent);
497 g_conf_printf_escaped(sb, "%s", buf);
498 sbuf_printf(sb, "</lunid>\n");
500 bp->bio_attribute = "GEOM::lunname";
501 bp->bio_length = DISK_IDENT_SIZE;
503 if (dp->d_getattr(bp) == 0) {
504 sbuf_printf(sb, "%s<lunname>", indent);
505 g_conf_printf_escaped(sb, "%s", buf);
506 sbuf_printf(sb, "</lunname>\n");
511 sbuf_printf(sb, "%s<ident>", indent);
512 g_conf_printf_escaped(sb, "%s", dp->d_ident);
513 sbuf_printf(sb, "</ident>\n");
515 sbuf_printf(sb, "%s<descr>", indent);
516 g_conf_printf_escaped(sb, "%s", dp->d_descr);
517 sbuf_printf(sb, "</descr>\n");
522 g_disk_resize(void *ptr, int flag)
526 struct g_provider *pp;
528 if (flag == EV_CANCEL)
535 if (dp->d_destroyed || gp == NULL)
538 LIST_FOREACH(pp, &gp->provider, provider) {
539 if (pp->sectorsize != 0 &&
540 pp->sectorsize != dp->d_sectorsize)
541 g_wither_provider(pp, ENXIO);
543 g_resize_provider(pp, dp->d_mediasize);
548 g_disk_create(void *arg, int flag)
551 struct g_provider *pp;
553 struct g_disk_softc *sc;
556 if (flag == EV_CANCEL)
560 sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
561 mtx_init(&sc->start_mtx, "g_disk_start", NULL, MTX_DEF);
562 mtx_init(&sc->done_mtx, "g_disk_done", NULL, MTX_DEF);
564 gp = g_new_geomf(&g_disk_class, "%s%d", dp->d_name, dp->d_unit);
566 pp = g_new_providerf(gp, "%s", gp->name);
567 devstat_remove_entry(pp->stat);
569 dp->d_devstat->id = pp;
570 pp->mediasize = dp->d_mediasize;
571 pp->sectorsize = dp->d_sectorsize;
572 pp->stripeoffset = dp->d_stripeoffset;
573 pp->stripesize = dp->d_stripesize;
574 if ((dp->d_flags & DISKFLAG_UNMAPPED_BIO) != 0)
575 pp->flags |= G_PF_ACCEPT_UNMAPPED;
576 if ((dp->d_flags & DISKFLAG_DIRECT_COMPLETION) != 0)
577 pp->flags |= G_PF_DIRECT_SEND;
578 pp->flags |= G_PF_DIRECT_RECEIVE;
580 printf("GEOM: new disk %s\n", gp->name);
581 sysctl_ctx_init(&sc->sysctl_ctx);
582 snprintf(tmpstr, sizeof(tmpstr), "GEOM disk %s", gp->name);
583 sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
584 SYSCTL_STATIC_CHILDREN(_kern_geom_disk), OID_AUTO, gp->name,
585 CTLFLAG_RD, 0, tmpstr);
586 if (sc->sysctl_tree != NULL) {
587 snprintf(tmpstr, sizeof(tmpstr),
588 "kern.geom.disk.%s.led", gp->name);
589 TUNABLE_STR_FETCH(tmpstr, sc->led, sizeof(sc->led));
590 SYSCTL_ADD_STRING(&sc->sysctl_ctx,
591 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO, "led",
592 CTLFLAG_RW | CTLFLAG_TUN, sc->led, sizeof(sc->led),
597 g_error_provider(pp, 0);
601 * We get this callback after all of the consumers have gone away, and just
602 * before the provider is freed. If the disk driver provided a d_gone
603 * callback, let them know that it is okay to free resources -- they won't
604 * be getting any more accesses from GEOM.
607 g_disk_providergone(struct g_provider *pp)
610 struct g_disk_softc *sc;
612 sc = (struct g_disk_softc *)pp->private;
614 if (dp != NULL && dp->d_gone != NULL)
616 if (sc->sysctl_tree != NULL) {
617 sysctl_ctx_free(&sc->sysctl_ctx);
618 sc->sysctl_tree = NULL;
620 if (sc->led[0] != 0) {
621 led_set(sc->led, "0");
625 pp->geom->softc = NULL;
626 mtx_destroy(&sc->done_mtx);
627 mtx_destroy(&sc->start_mtx);
632 g_disk_destroy(void *ptr, int flag)
636 struct g_disk_softc *sc;
646 g_wither_geom(gp, ENXIO);
652 * We only allow printable characters in disk ident,
653 * the rest is converted to 'x<HH>'.
656 g_disk_ident_adjust(char *ident, size_t size)
658 char *p, tmp[4], newid[DISK_IDENT_SIZE];
661 for (p = ident; *p != '\0'; p++) {
666 snprintf(tmp, sizeof(tmp), "x%02hhx",
667 *(unsigned char *)p);
669 if (strlcat(newid, tmp, sizeof(newid)) >= sizeof(newid))
673 strlcpy(ident, newid, size);
680 return (g_malloc(sizeof(struct disk), M_WAITOK | M_ZERO));
684 disk_create(struct disk *dp, int version)
687 if (version != DISK_VERSION) {
688 printf("WARNING: Attempt to add disk %s%d %s",
689 dp->d_name, dp->d_unit,
690 " using incompatible ABI version of disk(9)\n");
691 printf("WARNING: Ignoring disk %s%d\n",
692 dp->d_name, dp->d_unit);
695 KASSERT(dp->d_strategy != NULL, ("disk_create need d_strategy"));
696 KASSERT(dp->d_name != NULL, ("disk_create need d_name"));
697 KASSERT(*dp->d_name != 0, ("disk_create need d_name"));
698 KASSERT(strlen(dp->d_name) < SPECNAMELEN - 4, ("disk name too long"));
699 if (dp->d_devstat == NULL)
700 dp->d_devstat = devstat_new_entry(dp->d_name, dp->d_unit,
701 dp->d_sectorsize, DEVSTAT_ALL_SUPPORTED,
702 DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
704 g_disk_ident_adjust(dp->d_ident, sizeof(dp->d_ident));
705 g_post_event(g_disk_create, dp, M_WAITOK, dp, NULL);
709 disk_destroy(struct disk *dp)
714 if (dp->d_devstat != NULL)
715 devstat_remove_entry(dp->d_devstat);
716 g_post_event(g_disk_destroy, dp, M_WAITOK, NULL);
720 disk_gone(struct disk *dp)
723 struct g_provider *pp;
727 pp = LIST_FIRST(&gp->provider);
729 KASSERT(LIST_NEXT(pp, provider) == NULL,
730 ("geom %p has more than one provider", gp));
731 g_wither_provider(pp, ENXIO);
737 disk_attr_changed(struct disk *dp, const char *attr, int flag)
740 struct g_provider *pp;
744 LIST_FOREACH(pp, &gp->provider, provider)
745 (void)g_attr_changed(pp, attr, flag);
749 disk_media_changed(struct disk *dp, int flag)
752 struct g_provider *pp;
756 pp = LIST_FIRST(&gp->provider);
758 KASSERT(LIST_NEXT(pp, provider) == NULL,
759 ("geom %p has more than one provider", gp));
760 g_media_changed(pp, flag);
766 disk_media_gone(struct disk *dp, int flag)
769 struct g_provider *pp;
773 pp = LIST_FIRST(&gp->provider);
775 KASSERT(LIST_NEXT(pp, provider) == NULL,
776 ("geom %p has more than one provider", gp));
777 g_media_gone(pp, flag);
783 disk_resize(struct disk *dp, int flag)
786 if (dp->d_destroyed || dp->d_geom == NULL)
789 return (g_post_event(g_disk_resize, dp, flag, NULL));
793 g_kern_disks(void *p, int flag __unused)
802 LIST_FOREACH(gp, &g_disk_class.geom, geom) {
803 sbuf_printf(sb, "%s%s", sp, gp->name);
810 sysctl_disks(SYSCTL_HANDLER_ARGS)
815 sb = sbuf_new_auto();
816 g_waitfor_event(g_kern_disks, sb, M_WAITOK, NULL);
817 error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
822 SYSCTL_PROC(_kern, OID_AUTO, disks,
823 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
824 sysctl_disks, "A", "names of available disks");