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_kernedump(%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>%s</ident>\n", indent,
489 res == 0 ? buf: dp->d_ident);
490 bp->bio_attribute = "GEOM::lunid";
491 bp->bio_length = DISK_IDENT_SIZE;
493 if (dp->d_getattr(bp) == 0)
494 sbuf_printf(sb, "%s<lunid>%s</lunid>\n",
496 bp->bio_attribute = "GEOM::lunname";
497 bp->bio_length = DISK_IDENT_SIZE;
499 if (dp->d_getattr(bp) == 0)
500 sbuf_printf(sb, "%s<lunname>%s</lunname>\n",
505 sbuf_printf(sb, "%s<ident>%s</ident>\n", indent,
507 sbuf_printf(sb, "%s<descr>%s</descr>\n", indent, dp->d_descr);
512 g_disk_resize(void *ptr, int flag)
516 struct g_provider *pp;
518 if (flag == EV_CANCEL)
525 if (dp->d_destroyed || gp == NULL)
528 LIST_FOREACH(pp, &gp->provider, provider) {
529 if (pp->sectorsize != 0 &&
530 pp->sectorsize != dp->d_sectorsize)
531 g_wither_provider(pp, ENXIO);
533 g_resize_provider(pp, dp->d_mediasize);
538 g_disk_create(void *arg, int flag)
541 struct g_provider *pp;
543 struct g_disk_softc *sc;
546 if (flag == EV_CANCEL)
550 sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
551 mtx_init(&sc->start_mtx, "g_disk_start", NULL, MTX_DEF);
552 mtx_init(&sc->done_mtx, "g_disk_done", NULL, MTX_DEF);
554 gp = g_new_geomf(&g_disk_class, "%s%d", dp->d_name, dp->d_unit);
556 pp = g_new_providerf(gp, "%s", gp->name);
557 devstat_remove_entry(pp->stat);
559 dp->d_devstat->id = pp;
560 pp->mediasize = dp->d_mediasize;
561 pp->sectorsize = dp->d_sectorsize;
562 pp->stripeoffset = dp->d_stripeoffset;
563 pp->stripesize = dp->d_stripesize;
564 if ((dp->d_flags & DISKFLAG_UNMAPPED_BIO) != 0)
565 pp->flags |= G_PF_ACCEPT_UNMAPPED;
566 if ((dp->d_flags & DISKFLAG_DIRECT_COMPLETION) != 0)
567 pp->flags |= G_PF_DIRECT_SEND;
568 pp->flags |= G_PF_DIRECT_RECEIVE;
570 printf("GEOM: new disk %s\n", gp->name);
571 sysctl_ctx_init(&sc->sysctl_ctx);
572 snprintf(tmpstr, sizeof(tmpstr), "GEOM disk %s", gp->name);
573 sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
574 SYSCTL_STATIC_CHILDREN(_kern_geom_disk), OID_AUTO, gp->name,
575 CTLFLAG_RD, 0, tmpstr);
576 if (sc->sysctl_tree != NULL) {
577 snprintf(tmpstr, sizeof(tmpstr),
578 "kern.geom.disk.%s.led", gp->name);
579 TUNABLE_STR_FETCH(tmpstr, sc->led, sizeof(sc->led));
580 SYSCTL_ADD_STRING(&sc->sysctl_ctx,
581 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO, "led",
582 CTLFLAG_RW | CTLFLAG_TUN, sc->led, sizeof(sc->led),
587 g_error_provider(pp, 0);
591 * We get this callback after all of the consumers have gone away, and just
592 * before the provider is freed. If the disk driver provided a d_gone
593 * callback, let them know that it is okay to free resources -- they won't
594 * be getting any more accesses from GEOM.
597 g_disk_providergone(struct g_provider *pp)
600 struct g_disk_softc *sc;
602 sc = (struct g_disk_softc *)pp->private;
604 if (dp != NULL && dp->d_gone != NULL)
606 if (sc->sysctl_tree != NULL) {
607 sysctl_ctx_free(&sc->sysctl_ctx);
608 sc->sysctl_tree = NULL;
610 if (sc->led[0] != 0) {
611 led_set(sc->led, "0");
615 pp->geom->softc = NULL;
616 mtx_destroy(&sc->done_mtx);
617 mtx_destroy(&sc->start_mtx);
622 g_disk_destroy(void *ptr, int flag)
626 struct g_disk_softc *sc;
636 g_wither_geom(gp, ENXIO);
642 * We only allow printable characters in disk ident,
643 * the rest is converted to 'x<HH>'.
646 g_disk_ident_adjust(char *ident, size_t size)
648 char *p, tmp[4], newid[DISK_IDENT_SIZE];
651 for (p = ident; *p != '\0'; p++) {
656 snprintf(tmp, sizeof(tmp), "x%02hhx",
657 *(unsigned char *)p);
659 if (strlcat(newid, tmp, sizeof(newid)) >= sizeof(newid))
663 strlcpy(ident, newid, size);
670 return (g_malloc(sizeof(struct disk), M_WAITOK | M_ZERO));
674 disk_create(struct disk *dp, int version)
677 if (version != DISK_VERSION) {
678 printf("WARNING: Attempt to add disk %s%d %s",
679 dp->d_name, dp->d_unit,
680 " using incompatible ABI version of disk(9)\n");
681 printf("WARNING: Ignoring disk %s%d\n",
682 dp->d_name, dp->d_unit);
685 KASSERT(dp->d_strategy != NULL, ("disk_create need d_strategy"));
686 KASSERT(dp->d_name != NULL, ("disk_create need d_name"));
687 KASSERT(*dp->d_name != 0, ("disk_create need d_name"));
688 KASSERT(strlen(dp->d_name) < SPECNAMELEN - 4, ("disk name too long"));
689 if (dp->d_devstat == NULL)
690 dp->d_devstat = devstat_new_entry(dp->d_name, dp->d_unit,
691 dp->d_sectorsize, DEVSTAT_ALL_SUPPORTED,
692 DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
694 g_disk_ident_adjust(dp->d_ident, sizeof(dp->d_ident));
695 g_post_event(g_disk_create, dp, M_WAITOK, dp, NULL);
699 disk_destroy(struct disk *dp)
704 if (dp->d_devstat != NULL)
705 devstat_remove_entry(dp->d_devstat);
706 g_post_event(g_disk_destroy, dp, M_WAITOK, NULL);
710 disk_gone(struct disk *dp)
713 struct g_provider *pp;
717 pp = LIST_FIRST(&gp->provider);
719 KASSERT(LIST_NEXT(pp, provider) == NULL,
720 ("geom %p has more than one provider", gp));
721 g_wither_provider(pp, ENXIO);
727 disk_attr_changed(struct disk *dp, const char *attr, int flag)
730 struct g_provider *pp;
734 LIST_FOREACH(pp, &gp->provider, provider)
735 (void)g_attr_changed(pp, attr, flag);
739 disk_media_changed(struct disk *dp, int flag)
742 struct g_provider *pp;
746 pp = LIST_FIRST(&gp->provider);
748 KASSERT(LIST_NEXT(pp, provider) == NULL,
749 ("geom %p has more than one provider", gp));
750 g_media_changed(pp, flag);
756 disk_media_gone(struct disk *dp, int flag)
759 struct g_provider *pp;
763 pp = LIST_FIRST(&gp->provider);
765 KASSERT(LIST_NEXT(pp, provider) == NULL,
766 ("geom %p has more than one provider", gp));
767 g_media_gone(pp, flag);
773 disk_resize(struct disk *dp, int flag)
776 if (dp->d_destroyed || dp->d_geom == NULL)
779 return (g_post_event(g_disk_resize, dp, flag, NULL));
783 g_kern_disks(void *p, int flag __unused)
792 LIST_FOREACH(gp, &g_disk_class.geom, geom) {
793 sbuf_printf(sb, "%s%s", sp, gp->name);
800 sysctl_disks(SYSCTL_HANDLER_ARGS)
805 sb = sbuf_new_auto();
806 g_waitfor_event(g_kern_disks, sb, M_WAITOK, NULL);
807 error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
812 SYSCTL_PROC(_kern, OID_AUTO, disks,
813 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
814 sysctl_disks, "A", "names of available disks");