2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2002 Poul-Henning Kamp
5 * Copyright (c) 2002 Networks Associates Technology, Inc.
8 * This software was developed for the FreeBSD Project by Poul-Henning Kamp
9 * and NAI Labs, the Security Research Division of Network Associates, Inc.
10 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
11 * DARPA CHATS research program.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. The names of the authors may not be used to endorse or promote
22 * products derived from this software without specific prior written
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/malloc.h>
44 #include <sys/kernel.h>
46 #include <sys/ctype.h>
50 #include <sys/mutex.h>
52 #include <sys/errno.h>
55 #include <sys/fcntl.h>
56 #include <sys/limits.h>
57 #include <sys/sysctl.h>
58 #include <geom/geom.h>
59 #include <geom/geom_int.h>
60 #include <machine/stdarg.h>
65 struct cdev *sc_alias;
70 static d_open_t g_dev_open;
71 static d_close_t g_dev_close;
72 static d_strategy_t g_dev_strategy;
73 static d_ioctl_t g_dev_ioctl;
75 static struct cdevsw g_dev_cdevsw = {
76 .d_version = D_VERSION,
78 .d_close = g_dev_close,
81 .d_ioctl = g_dev_ioctl,
82 .d_strategy = g_dev_strategy,
84 .d_flags = D_DISK | D_TRACKCLOSE,
87 static g_init_t g_dev_init;
88 static g_fini_t g_dev_fini;
89 static g_taste_t g_dev_taste;
90 static g_orphan_t g_dev_orphan;
91 static g_attrchanged_t g_dev_attrchanged;
93 static struct g_class g_dev_class = {
99 .orphan = g_dev_orphan,
100 .attrchanged = g_dev_attrchanged
104 * We target 262144 (8 x 32768) sectors by default as this significantly
105 * increases the throughput on commonly used SSD's with a marginal
106 * increase in non-interruptible request latency.
108 static uint64_t g_dev_del_max_sectors = 262144;
109 SYSCTL_DECL(_kern_geom);
110 SYSCTL_NODE(_kern_geom, OID_AUTO, dev, CTLFLAG_RW, 0, "GEOM_DEV stuff");
111 SYSCTL_QUAD(_kern_geom_dev, OID_AUTO, delete_max_sectors, CTLFLAG_RW,
112 &g_dev_del_max_sectors, 0, "Maximum number of sectors in a single "
113 "delete request sent to the provider. Larger requests are chunked "
114 "so they can be interrupted. (0 = disable chunking)");
116 static char *dumpdev = NULL;
118 g_dev_init(struct g_class *mp)
121 dumpdev = kern_getenv("dumpdev");
125 g_dev_fini(struct g_class *mp)
133 g_dev_setdumpdev(struct cdev *dev, struct diocskerneldump_arg *kda,
136 struct g_kerneldump kd;
137 struct g_consumer *cp;
140 if (dev == NULL || kda == NULL)
141 return (clear_dumper(td));
148 error = g_io_getattr("GEOM::kerneldump", cp, &len, &kd);
152 error = set_dumper(&kd.di, devtoname(dev), td, kda->kda_compression,
153 kda->kda_encryption, kda->kda_key, kda->kda_encryptedkeysize,
154 kda->kda_encryptedkey);
156 dev->si_flags |= SI_DUMPDEV;
162 init_dumpdev(struct cdev *dev)
164 struct diocskerneldump_arg kda;
165 struct g_consumer *cp;
166 const char *devprefix = "/dev/", *devname;
170 bzero(&kda, sizeof(kda));
176 len = strlen(devprefix);
177 devname = devtoname(dev);
178 if (strcmp(devname, dumpdev) != 0 &&
179 (strncmp(dumpdev, devprefix, len) != 0 ||
180 strcmp(devname, dumpdev + len) != 0))
183 cp = (struct g_consumer *)dev->si_drv2;
184 error = g_access(cp, 1, 0, 0);
188 error = g_dev_setdumpdev(dev, &kda, curthread);
194 (void)g_access(cp, -1, 0, 0);
200 g_dev_destroy(void *arg, int flags __unused)
202 struct g_consumer *cp;
204 struct g_dev_softc *sc;
205 char buf[SPECNAMELEN + 6];
211 g_trace(G_T_TOPOLOGY, "g_dev_destroy(%p(%s))", cp, gp->name);
212 snprintf(buf, sizeof(buf), "cdev=%s", gp->name);
213 devctl_notify_f("GEOM", "DEV", "DESTROY", buf, M_WAITOK);
214 if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
215 g_access(cp, -cp->acr, -cp->acw, -cp->ace);
217 g_destroy_consumer(cp);
219 mtx_destroy(&sc->sc_mtx);
229 LIST_FOREACH(gp, &g_dev_class.geom, geom) {
230 printf("%s%s", p, gp->name);
237 g_dev_set_physpath(struct g_consumer *cp)
239 struct g_dev_softc *sc;
241 int error, physpath_len;
243 if (g_access(cp, 1, 0, 0) != 0)
247 physpath_len = MAXPATHLEN;
248 physpath = g_malloc(physpath_len, M_WAITOK|M_ZERO);
249 error = g_io_getattr("GEOM::physpath", cp, &physpath_len, physpath);
250 g_access(cp, -1, 0, 0);
251 if (error == 0 && strlen(physpath) != 0) {
252 struct cdev *dev, *old_alias_dev;
253 struct cdev **alias_devp;
256 old_alias_dev = sc->sc_alias;
257 alias_devp = (struct cdev **)&sc->sc_alias;
258 make_dev_physpath_alias(MAKEDEV_WAITOK, alias_devp, dev,
259 old_alias_dev, physpath);
260 } else if (sc->sc_alias) {
261 destroy_dev((struct cdev *)sc->sc_alias);
268 g_dev_set_media(struct g_consumer *cp)
270 struct g_dev_softc *sc;
272 char buf[SPECNAMELEN + 6];
276 snprintf(buf, sizeof(buf), "cdev=%s", dev->si_name);
277 devctl_notify_f("DEVFS", "CDEV", "MEDIACHANGE", buf, M_WAITOK);
278 devctl_notify_f("GEOM", "DEV", "MEDIACHANGE", buf, M_WAITOK);
281 snprintf(buf, sizeof(buf), "cdev=%s", dev->si_name);
282 devctl_notify_f("DEVFS", "CDEV", "MEDIACHANGE", buf, M_WAITOK);
283 devctl_notify_f("GEOM", "DEV", "MEDIACHANGE", buf, M_WAITOK);
288 g_dev_attrchanged(struct g_consumer *cp, const char *attr)
291 if (strcmp(attr, "GEOM::media") == 0) {
296 if (strcmp(attr, "GEOM::physpath") == 0) {
297 g_dev_set_physpath(cp);
303 g_dev_getprovider(struct cdev *dev)
305 struct g_consumer *cp;
310 if (dev->si_devsw != &g_dev_cdevsw)
313 return (cp->provider);
316 static struct g_geom *
317 g_dev_taste(struct g_class *mp, struct g_provider *pp, int insist __unused)
320 struct g_geom_alias *gap;
321 struct g_consumer *cp;
322 struct g_dev_softc *sc;
324 struct cdev *dev, *adev;
325 char buf[SPECNAMELEN + 6];
327 g_trace(G_T_TOPOLOGY, "dev_taste(%s,%s)", mp->name, pp->name);
329 gp = g_new_geomf(mp, "%s", pp->name);
330 sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
331 mtx_init(&sc->sc_mtx, "g_dev", NULL, MTX_DEF);
332 cp = g_new_consumer(gp);
334 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
335 error = g_attach(cp, pp);
337 ("g_dev_taste(%s) failed to g_attach, err=%d", pp->name, error));
338 error = make_dev_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK, &dev,
339 &g_dev_cdevsw, NULL, UID_ROOT, GID_OPERATOR, 0640, "%s", gp->name);
341 printf("%s: make_dev_p() failed (gp->name=%s, error=%d)\n",
342 __func__, gp->name, error);
344 g_destroy_consumer(cp);
346 mtx_destroy(&sc->sc_mtx);
350 dev->si_flags |= SI_UNMAPPED;
353 dev->si_iosize_max = MAXPHYS;
355 error = init_dumpdev(dev);
357 printf("%s: init_dumpdev() failed (gp->name=%s, error=%d)\n",
358 __func__, gp->name, error);
360 g_dev_attrchanged(cp, "GEOM::physpath");
361 snprintf(buf, sizeof(buf), "cdev=%s", gp->name);
362 devctl_notify_f("GEOM", "DEV", "CREATE", buf, M_WAITOK);
364 * Now add all the aliases for this drive
366 LIST_FOREACH(gap, &pp->geom->aliases, ga_next) {
367 error = make_dev_alias_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK, &adev, dev,
368 "%s", gap->ga_alias);
370 printf("%s: make_dev_alias_p() failed (name=%s, error=%d)\n",
371 __func__, gap->ga_alias, error);
374 snprintf(buf, sizeof(buf), "cdev=%s", gap->ga_alias);
375 devctl_notify_f("GEOM", "DEV", "CREATE", buf, M_WAITOK);
382 g_dev_open(struct cdev *dev, int flags, int fmt, struct thread *td)
384 struct g_consumer *cp;
385 struct g_dev_softc *sc;
390 return (ENXIO); /* g_dev_taste() not done yet */
391 g_trace(G_T_ACCESS, "g_dev_open(%s, %d, %d, %p)",
392 cp->geom->name, flags, fmt, td);
394 r = flags & FREAD ? 1 : 0;
395 w = flags & FWRITE ? 1 : 0;
397 e = flags & O_EXCL ? 1 : 0;
403 * This happens on attempt to open a device node with O_EXEC.
410 * When running in very secure mode, do not allow
411 * opens for writing of any disks.
413 error = securelevel_ge(td->td_ucred, 2);
418 error = g_access(cp, r, w, e);
422 mtx_lock(&sc->sc_mtx);
423 if (sc->sc_open == 0 && sc->sc_active != 0)
424 wakeup(&sc->sc_active);
425 sc->sc_open += r + w + e;
426 mtx_unlock(&sc->sc_mtx);
432 g_dev_close(struct cdev *dev, int flags, int fmt, struct thread *td)
434 struct g_consumer *cp;
435 struct g_dev_softc *sc;
441 g_trace(G_T_ACCESS, "g_dev_close(%s, %d, %d, %p)",
442 cp->geom->name, flags, fmt, td);
444 r = flags & FREAD ? -1 : 0;
445 w = flags & FWRITE ? -1 : 0;
447 e = flags & O_EXCL ? -1 : 0;
453 * The vgonel(9) - caused by eg. forced unmount of devfs - calls
454 * VOP_CLOSE(9) on devfs vnode without any FREAD or FWRITE flags,
455 * which would result in zero deltas, which in turn would cause
456 * panic in g_access(9).
458 * Note that we cannot zero the counters (ie. do "r = cp->acr"
459 * etc) instead, because the consumer might be opened in another
466 mtx_lock(&sc->sc_mtx);
467 sc->sc_open += r + w + e;
468 while (sc->sc_open == 0 && sc->sc_active != 0)
469 msleep(&sc->sc_active, &sc->sc_mtx, 0, "PRIBIO", 0);
470 mtx_unlock(&sc->sc_mtx);
472 error = g_access(cp, r, w, e);
478 * XXX: Until we have unmessed the ioctl situation, there is a race against
479 * XXX: a concurrent orphanization. We cannot close it by holding topology
480 * XXX: since that would prevent us from doing our job, and stalling events
481 * XXX: will break (actually: stall) the BSD disklabel hacks.
484 g_dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
486 struct g_consumer *cp;
487 struct g_provider *pp;
488 off_t offset, length, chunk, odd;
495 KASSERT(cp->acr || cp->acw,
496 ("Consumer with zero access count in g_dev_ioctl"));
498 i = IOCPARM_LEN(cmd);
500 case DIOCGSECTORSIZE:
501 *(u_int *)data = cp->provider->sectorsize;
502 if (*(u_int *)data == 0)
506 *(off_t *)data = cp->provider->mediasize;
507 if (*(off_t *)data == 0)
511 error = g_io_getattr("GEOM::fwsectors", cp, &i, data);
512 if (error == 0 && *(u_int *)data == 0)
516 error = g_io_getattr("GEOM::fwheads", cp, &i, data);
517 if (error == 0 && *(u_int *)data == 0)
520 case DIOCGFRONTSTUFF:
521 error = g_io_getattr("GEOM::frontstuff", cp, &i, data);
523 #ifdef COMPAT_FREEBSD11
524 case DIOCSKERNELDUMP_FREEBSD11:
526 struct diocskerneldump_arg kda;
528 bzero(&kda, sizeof(kda));
529 kda.kda_encryption = KERNELDUMP_ENC_NONE;
530 kda.kda_enable = (uint8_t)*(u_int *)data;
531 if (kda.kda_enable == 0)
532 error = g_dev_setdumpdev(NULL, NULL, td);
534 error = g_dev_setdumpdev(dev, &kda, td);
538 case DIOCSKERNELDUMP:
540 struct diocskerneldump_arg *kda;
541 uint8_t *encryptedkey;
543 kda = (struct diocskerneldump_arg *)data;
544 if (kda->kda_enable == 0) {
545 error = g_dev_setdumpdev(NULL, NULL, td);
549 if (kda->kda_encryption != KERNELDUMP_ENC_NONE) {
550 if (kda->kda_encryptedkeysize <= 0 ||
551 kda->kda_encryptedkeysize >
552 KERNELDUMP_ENCKEY_MAX_SIZE) {
555 encryptedkey = malloc(kda->kda_encryptedkeysize, M_TEMP,
557 error = copyin(kda->kda_encryptedkey, encryptedkey,
558 kda->kda_encryptedkeysize);
563 kda->kda_encryptedkey = encryptedkey;
564 error = g_dev_setdumpdev(dev, kda, td);
566 if (encryptedkey != NULL) {
567 explicit_bzero(encryptedkey, kda->kda_encryptedkeysize);
568 free(encryptedkey, M_TEMP);
570 explicit_bzero(kda, sizeof(*kda));
574 error = g_io_flush(cp);
577 offset = ((off_t *)data)[0];
578 length = ((off_t *)data)[1];
579 if ((offset % cp->provider->sectorsize) != 0 ||
580 (length % cp->provider->sectorsize) != 0 || length <= 0) {
581 printf("%s: offset=%jd length=%jd\n", __func__, offset,
586 if ((cp->provider->mediasize > 0) &&
587 (offset >= cp->provider->mediasize)) {
589 * Catch out-of-bounds requests here. The problem is
590 * that due to historical GEOM I/O implementation
591 * peculatities, g_delete_data() would always return
592 * success for requests starting just the next byte
593 * after providers media boundary. Condition check on
594 * non-zero media size, since that condition would
595 * (most likely) cause ENXIO instead.
602 if (g_dev_del_max_sectors != 0 && chunk >
603 g_dev_del_max_sectors * cp->provider->sectorsize) {
604 chunk = g_dev_del_max_sectors *
605 cp->provider->sectorsize;
606 if (cp->provider->stripesize > 0) {
607 odd = (offset + chunk +
608 cp->provider->stripeoffset) %
609 cp->provider->stripesize;
614 error = g_delete_data(cp, offset, chunk);
620 * Since the request size can be large, the service
621 * time can be is likewise. We make this ioctl
622 * interruptible by checking for signals for each bio.
629 error = g_io_getattr("GEOM::ident", cp, &i, data);
631 case DIOCGPROVIDERNAME:
634 strlcpy(data, pp->name, i);
636 case DIOCGSTRIPESIZE:
637 *(off_t *)data = cp->provider->stripesize;
639 case DIOCGSTRIPEOFFSET:
640 *(off_t *)data = cp->provider->stripeoffset;
643 error = g_io_getattr("GEOM::physpath", cp, &i, data);
644 if (error == 0 && *(char *)data == '\0')
648 struct diocgattr_arg *arg = (struct diocgattr_arg *)data;
650 if (arg->len > sizeof(arg->value)) {
654 error = g_io_getattr(arg->name, cp, &arg->len, &arg->value);
658 struct disk_zone_args *zone_args =(struct disk_zone_args *)data;
659 struct disk_zone_rep_entry *new_entries, *old_entries;
660 struct disk_zone_report *rep;
668 if (zone_args->zone_cmd == DISK_ZONE_REPORT_ZONES) {
670 rep = &zone_args->zone_params.report;
671 alloc_size = rep->entries_allocated *
672 sizeof(struct disk_zone_rep_entry);
674 new_entries = g_malloc(alloc_size,
676 old_entries = rep->entries;
677 rep->entries = new_entries;
679 error = g_io_zonecmd(zone_args, cp);
680 if ((zone_args->zone_cmd == DISK_ZONE_REPORT_ZONES)
683 error = copyout(new_entries, old_entries, alloc_size);
685 if ((old_entries != NULL)
687 rep->entries = old_entries;
689 if (new_entries != NULL)
694 if (cp->provider->geom->ioctl != NULL) {
695 error = cp->provider->geom->ioctl(cp->provider, cmd, data, fflag, td);
705 g_dev_done(struct bio *bp2)
707 struct g_consumer *cp;
708 struct g_dev_softc *sc;
714 bp = bp2->bio_parent;
715 bp->bio_error = bp2->bio_error;
716 bp->bio_completed = bp2->bio_completed;
717 bp->bio_resid = bp->bio_length - bp2->bio_completed;
718 if (bp2->bio_cmd == BIO_ZONE)
719 bcopy(&bp2->bio_zone, &bp->bio_zone, sizeof(bp->bio_zone));
721 if (bp2->bio_error != 0) {
722 g_trace(G_T_BIO, "g_dev_done(%p) had error %d",
723 bp2, bp2->bio_error);
724 bp->bio_flags |= BIO_ERROR;
726 g_trace(G_T_BIO, "g_dev_done(%p/%p) resid %ld completed %jd",
727 bp2, bp, bp2->bio_resid, (intmax_t)bp2->bio_completed);
731 mtx_lock(&sc->sc_mtx);
732 if ((--sc->sc_active) == 0) {
733 if (sc->sc_open == 0)
734 wakeup(&sc->sc_active);
735 if (sc->sc_dev == NULL)
738 mtx_unlock(&sc->sc_mtx);
740 g_post_event(g_dev_destroy, cp, M_NOWAIT, NULL);
745 g_dev_strategy(struct bio *bp)
747 struct g_consumer *cp;
750 struct g_dev_softc *sc;
752 KASSERT(bp->bio_cmd == BIO_READ ||
753 bp->bio_cmd == BIO_WRITE ||
754 bp->bio_cmd == BIO_DELETE ||
755 bp->bio_cmd == BIO_FLUSH ||
756 bp->bio_cmd == BIO_ZONE,
757 ("Wrong bio_cmd bio=%p cmd=%d", bp, bp->bio_cmd));
761 KASSERT(cp->acr || cp->acw,
762 ("Consumer with zero access count in g_dev_strategy"));
763 biotrack(bp, __func__);
765 if ((bp->bio_offset % cp->provider->sectorsize) != 0 ||
766 (bp->bio_bcount % cp->provider->sectorsize) != 0) {
767 bp->bio_resid = bp->bio_bcount;
768 biofinish(bp, NULL, EINVAL);
772 mtx_lock(&sc->sc_mtx);
773 KASSERT(sc->sc_open > 0, ("Closed device in g_dev_strategy"));
775 mtx_unlock(&sc->sc_mtx);
779 * XXX: This is not an ideal solution, but I believe it to
780 * XXX: deadlock safely, all things considered.
782 bp2 = g_clone_bio(bp);
785 pause("gdstrat", hz / 10);
787 KASSERT(bp2 != NULL, ("XXX: ENOMEM in a bad place"));
788 bp2->bio_done = g_dev_done;
790 "g_dev_strategy(%p/%p) offset %jd length %jd data %p cmd %d",
791 bp, bp2, (intmax_t)bp->bio_offset, (intmax_t)bp2->bio_length,
792 bp2->bio_data, bp2->bio_cmd);
793 g_io_request(bp2, cp);
794 KASSERT(cp->acr || cp->acw,
795 ("g_dev_strategy raced with g_dev_close and lost"));
802 * Called by devfs when asynchronous device destruction is completed.
803 * - Mark that we have no attached device any more.
804 * - If there are no outstanding requests, schedule geom destruction.
805 * Otherwise destruction will be scheduled later by g_dev_done().
809 g_dev_callback(void *arg)
811 struct g_consumer *cp;
812 struct g_dev_softc *sc;
817 g_trace(G_T_TOPOLOGY, "g_dev_callback(%p(%s))", cp, cp->geom->name);
819 mtx_lock(&sc->sc_mtx);
822 destroy = (sc->sc_active == 0);
823 mtx_unlock(&sc->sc_mtx);
825 g_post_event(g_dev_destroy, cp, M_WAITOK, NULL);
831 * Called from below when the provider orphaned us.
832 * - Clear any dump settings.
833 * - Request asynchronous device destruction to prevent any more requests
834 * from coming in. The provider is already marked with an error, so
835 * anything which comes in the interim will be returned immediately.
839 g_dev_orphan(struct g_consumer *cp)
842 struct g_dev_softc *sc;
847 g_trace(G_T_TOPOLOGY, "g_dev_orphan(%p(%s))", cp, cp->geom->name);
849 /* Reset any dump-area set on this device */
850 if (dev->si_flags & SI_DUMPDEV)
851 (void)clear_dumper(curthread);
853 /* Destroy the struct cdev *so we get no more requests */
854 destroy_dev_sched_cb(dev, g_dev_callback, cp);
857 DECLARE_GEOM_CLASS(g_dev_class, g_dev);