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;
79 static struct g_class g_disk_class = {
84 .start = g_disk_start,
85 .access = g_disk_access,
86 .ioctl = g_disk_ioctl,
87 .dumpconf = g_disk_dumpconf,
90 SYSCTL_DECL(_kern_geom);
91 SYSCTL_NODE(_kern_geom, OID_AUTO, disk, CTLFLAG_RW, 0, "GEOM_DISK stuff");
94 g_disk_init(struct g_class *mp __unused)
97 mtx_init(&g_disk_done_mtx, "g_disk_done", NULL, MTX_DEF);
101 g_disk_fini(struct g_class *mp __unused)
104 mtx_destroy(&g_disk_done_mtx);
107 DECLARE_GEOM_CLASS(g_disk_class, g_disk);
110 g_disk_lock_giant(struct disk *dp)
112 if (dp->d_flags & DISKFLAG_NEEDSGIANT)
117 g_disk_unlock_giant(struct disk *dp)
119 if (dp->d_flags & DISKFLAG_NEEDSGIANT)
124 g_disk_access(struct g_provider *pp, int r, int w, int e)
127 struct g_disk_softc *sc;
130 g_trace(G_T_ACCESS, "g_disk_access(%s, %d, %d, %d)",
133 sc = pp->geom->softc;
134 if (sc == NULL || (dp = sc->dp) == NULL || dp->d_destroyed) {
136 * Allow decreasing access count even if disk is not
139 if (r <= 0 && w <= 0 && e <= 0)
147 if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) {
148 if (dp->d_open != NULL) {
149 g_disk_lock_giant(dp);
150 error = dp->d_open(dp);
151 if (bootverbose && error != 0)
152 printf("Opened disk %s -> %d\n",
154 g_disk_unlock_giant(dp);
156 pp->mediasize = dp->d_mediasize;
157 pp->sectorsize = dp->d_sectorsize;
158 if (dp->d_flags & DISKFLAG_CANDELETE)
159 pp->flags |= G_PF_CANDELETE;
161 pp->flags &= ~G_PF_CANDELETE;
162 pp->stripeoffset = dp->d_stripeoffset;
163 pp->stripesize = dp->d_stripesize;
164 dp->d_flags |= DISKFLAG_OPEN;
165 if (dp->d_maxsize == 0) {
166 printf("WARNING: Disk drive %s%d has no d_maxsize\n",
167 dp->d_name, dp->d_unit);
168 dp->d_maxsize = DFLTPHYS;
170 } else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) {
171 if (dp->d_close != NULL) {
172 g_disk_lock_giant(dp);
173 error = dp->d_close(dp);
175 printf("Closed disk %s -> %d\n",
177 g_disk_unlock_giant(dp);
179 sc->state = G_STATE_ACTIVE;
181 led_set(sc->led, "0");
182 dp->d_flags &= ~DISKFLAG_OPEN;
188 g_disk_kerneldump(struct bio *bp, struct disk *dp)
190 struct g_kerneldump *gkd;
193 gkd = (struct g_kerneldump*)bp->bio_data;
194 gp = bp->bio_to->geom;
195 g_trace(G_T_TOPOLOGY, "g_disk_kernedump(%s, %jd, %jd)",
196 gp->name, (intmax_t)gkd->offset, (intmax_t)gkd->length);
197 if (dp->d_dump == NULL) {
198 g_io_deliver(bp, ENODEV);
201 gkd->di.dumper = dp->d_dump;
203 gkd->di.blocksize = dp->d_sectorsize;
204 gkd->di.maxiosize = dp->d_maxsize;
205 gkd->di.mediaoffset = gkd->offset;
206 if ((gkd->offset + gkd->length) > dp->d_mediasize)
207 gkd->length = dp->d_mediasize - gkd->offset;
208 gkd->di.mediasize = gkd->length;
213 g_disk_setstate(struct bio *bp, struct g_disk_softc *sc)
217 memcpy(&sc->state, bp->bio_data, sizeof(sc->state));
218 if (sc->led[0] != 0) {
223 case G_STATE_REBUILD:
233 led_set(sc->led, cmd);
239 g_disk_done(struct bio *bp)
243 struct g_disk_softc *sc;
245 /* See "notes" for why we need a mutex here */
246 /* XXX: will witness accept a mix of Giant/unGiant drivers here ? */
247 mtx_lock(&g_disk_done_mtx);
248 bp->bio_completed = bp->bio_length - bp->bio_resid;
250 bp2 = bp->bio_parent;
251 if (bp2->bio_error == 0)
252 bp2->bio_error = bp->bio_error;
253 bp2->bio_completed += bp->bio_completed;
254 if ((bp->bio_cmd & (BIO_READ|BIO_WRITE|BIO_DELETE)) &&
255 (sc = bp2->bio_to->geom->softc) &&
257 devstat_end_transaction_bio(dp->d_devstat, bp);
261 if (bp2->bio_children == bp2->bio_inbed) {
262 bp2->bio_resid = bp2->bio_bcount - bp2->bio_completed;
263 g_io_deliver(bp2, bp2->bio_error);
265 mtx_unlock(&g_disk_done_mtx);
269 g_disk_ioctl(struct g_provider *pp, u_long cmd, void * data, int fflag, struct thread *td)
273 struct g_disk_softc *sc;
280 if (dp->d_ioctl == NULL)
282 g_disk_lock_giant(dp);
283 error = dp->d_ioctl(dp, cmd, data, fflag, td);
284 g_disk_unlock_giant(dp);
289 g_disk_start(struct bio *bp)
291 struct bio *bp2, *bp3;
293 struct g_disk_softc *sc;
297 sc = bp->bio_to->geom->softc;
298 if (sc == NULL || (dp = sc->dp) == NULL || dp->d_destroyed) {
299 g_io_deliver(bp, ENXIO);
303 switch(bp->bio_cmd) {
305 if (!(dp->d_flags & DISKFLAG_CANDELETE)) {
314 bp2 = g_clone_bio(bp);
320 bp2->bio_offset += off;
321 bp2->bio_length -= off;
322 bp2->bio_data += off;
323 if (bp2->bio_length > dp->d_maxsize) {
325 * XXX: If we have a stripesize we should really
328 bp2->bio_length = dp->d_maxsize;
329 off += dp->d_maxsize;
331 * To avoid a race, we need to grab the next bio
332 * before we schedule this one. See "notes".
334 bp3 = g_clone_bio(bp);
336 bp->bio_error = ENOMEM;
338 bp2->bio_done = g_disk_done;
339 bp2->bio_pblkno = bp2->bio_offset / dp->d_sectorsize;
340 bp2->bio_bcount = bp2->bio_length;
342 devstat_start_transaction_bio(dp->d_devstat, bp2);
343 g_disk_lock_giant(dp);
345 g_disk_unlock_giant(dp);
348 } while (bp2 != NULL);
351 /* Give the driver a chance to override */
352 if (dp->d_getattr != NULL) {
353 if (bp->bio_disk == NULL)
355 error = dp->d_getattr(bp);
360 if (g_handleattr_int(bp, "GEOM::candelete",
361 (dp->d_flags & DISKFLAG_CANDELETE) != 0))
363 else if (g_handleattr_int(bp, "GEOM::fwsectors",
366 else if (g_handleattr_int(bp, "GEOM::fwheads", dp->d_fwheads))
368 else if (g_handleattr_off_t(bp, "GEOM::frontstuff", 0))
370 else if (g_handleattr_str(bp, "GEOM::ident", dp->d_ident))
372 else if (g_handleattr(bp, "GEOM::hba_vendor",
373 &dp->d_hba_vendor, 2))
375 else if (g_handleattr(bp, "GEOM::hba_device",
376 &dp->d_hba_device, 2))
378 else if (g_handleattr(bp, "GEOM::hba_subvendor",
379 &dp->d_hba_subvendor, 2))
381 else if (g_handleattr(bp, "GEOM::hba_subdevice",
382 &dp->d_hba_subdevice, 2))
384 else if (!strcmp(bp->bio_attribute, "GEOM::kerneldump"))
385 g_disk_kerneldump(bp, dp);
386 else if (!strcmp(bp->bio_attribute, "GEOM::setstate"))
387 g_disk_setstate(bp, sc);
392 g_trace(G_T_TOPOLOGY, "g_disk_flushcache(%s)",
394 if (!(dp->d_flags & DISKFLAG_CANFLUSHCACHE)) {
395 g_io_deliver(bp, ENODEV);
398 bp2 = g_clone_bio(bp);
400 g_io_deliver(bp, ENOMEM);
403 bp2->bio_done = g_disk_done;
405 g_disk_lock_giant(dp);
407 g_disk_unlock_giant(dp);
413 if (error != EJUSTRETURN)
414 g_io_deliver(bp, error);
419 g_disk_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp)
422 struct g_disk_softc *sc;
425 if (sc == NULL || (dp = sc->dp) == NULL)
427 if (indent == NULL) {
428 sbuf_printf(sb, " hd %u", dp->d_fwheads);
429 sbuf_printf(sb, " sc %u", dp->d_fwsectors);
433 sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n",
434 indent, dp->d_fwheads);
435 sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n",
436 indent, dp->d_fwsectors);
437 sbuf_printf(sb, "%s<ident>%s</ident>\n", indent, dp->d_ident);
438 sbuf_printf(sb, "%s<descr>%s</descr>\n", indent, dp->d_descr);
443 g_disk_create(void *arg, int flag)
446 struct g_provider *pp;
448 struct g_disk_softc *sc;
451 if (flag == EV_CANCEL)
455 sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
457 gp = g_new_geomf(&g_disk_class, "%s%d", dp->d_name, dp->d_unit);
459 pp = g_new_providerf(gp, "%s", gp->name);
460 pp->mediasize = dp->d_mediasize;
461 pp->sectorsize = dp->d_sectorsize;
462 if (dp->d_flags & DISKFLAG_CANDELETE)
463 pp->flags |= G_PF_CANDELETE;
464 pp->stripeoffset = dp->d_stripeoffset;
465 pp->stripesize = dp->d_stripesize;
467 printf("GEOM: new disk %s\n", gp->name);
468 sysctl_ctx_init(&sc->sysctl_ctx);
469 snprintf(tmpstr, sizeof(tmpstr), "GEOM disk %s", gp->name);
470 sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
471 SYSCTL_STATIC_CHILDREN(_kern_geom_disk), OID_AUTO, gp->name,
472 CTLFLAG_RD, 0, tmpstr);
473 if (sc->sysctl_tree != NULL) {
474 snprintf(tmpstr, sizeof(tmpstr),
475 "kern.geom.disk.%s.led", gp->name);
476 TUNABLE_STR_FETCH(tmpstr, sc->led, sizeof(sc->led));
477 SYSCTL_ADD_STRING(&sc->sysctl_ctx,
478 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO, "led",
479 CTLFLAG_RW | CTLFLAG_TUN, sc->led, sizeof(sc->led),
484 g_error_provider(pp, 0);
488 g_disk_destroy(void *ptr, int flag)
492 struct g_disk_softc *sc;
499 if (sc->sysctl_tree != NULL) {
500 sysctl_ctx_free(&sc->sysctl_ctx);
501 sc->sysctl_tree = NULL;
503 if (sc->led[0] != 0) {
504 led_set(sc->led, "0");
509 g_wither_geom(gp, ENXIO);
515 * We only allow printable characters in disk ident,
516 * the rest is converted to 'x<HH>'.
519 g_disk_ident_adjust(char *ident, size_t size)
521 char *p, tmp[4], newid[DISK_IDENT_SIZE];
524 for (p = ident; *p != '\0'; p++) {
529 snprintf(tmp, sizeof(tmp), "x%02hhx",
530 *(unsigned char *)p);
532 if (strlcat(newid, tmp, sizeof(newid)) >= sizeof(newid))
536 strlcpy(ident, newid, size);
544 dp = g_malloc(sizeof *dp, M_WAITOK | M_ZERO);
549 disk_create(struct disk *dp, int version)
551 if (version != DISK_VERSION_00 && version != DISK_VERSION_01) {
552 printf("WARNING: Attempt to add disk %s%d %s",
553 dp->d_name, dp->d_unit,
554 " using incompatible ABI version of disk(9)\n");
555 printf("WARNING: Ignoring disk %s%d\n",
556 dp->d_name, dp->d_unit);
559 KASSERT(dp->d_strategy != NULL, ("disk_create need d_strategy"));
560 KASSERT(dp->d_name != NULL, ("disk_create need d_name"));
561 KASSERT(*dp->d_name != 0, ("disk_create need d_name"));
562 KASSERT(strlen(dp->d_name) < SPECNAMELEN - 4, ("disk name too long"));
563 if (dp->d_devstat == NULL)
564 dp->d_devstat = devstat_new_entry(dp->d_name, dp->d_unit,
565 dp->d_sectorsize, DEVSTAT_ALL_SUPPORTED,
566 DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
568 g_disk_ident_adjust(dp->d_ident, sizeof(dp->d_ident));
569 g_post_event(g_disk_create, dp, M_WAITOK, dp, NULL);
573 disk_destroy(struct disk *dp)
578 if (dp->d_devstat != NULL)
579 devstat_remove_entry(dp->d_devstat);
580 g_post_event(g_disk_destroy, dp, M_WAITOK, NULL);
584 disk_gone(struct disk *dp)
587 struct g_provider *pp;
591 LIST_FOREACH(pp, &gp->provider, provider)
592 g_wither_provider(pp, ENXIO);
596 disk_attr_changed(struct disk *dp, const char *attr, int flag)
599 struct g_provider *pp;
603 LIST_FOREACH(pp, &gp->provider, provider)
604 (void)g_attr_changed(pp, attr, flag);
608 g_kern_disks(void *p, int flag __unused)
617 LIST_FOREACH(gp, &g_disk_class.geom, geom) {
618 sbuf_printf(sb, "%s%s", sp, gp->name);
625 sysctl_disks(SYSCTL_HANDLER_ARGS)
630 sb = sbuf_new_auto();
631 g_waitfor_event(g_kern_disks, sb, M_WAITOK, NULL);
632 error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
637 SYSCTL_PROC(_kern, OID_AUTO, disks,
638 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
639 sysctl_disks, "A", "names of available disks");