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 "opt_compat.h"
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/malloc.h>
46 #include <sys/kernel.h>
48 #include <sys/ctype.h>
52 #include <sys/mutex.h>
54 #include <sys/errno.h>
57 #include <sys/fcntl.h>
58 #include <sys/limits.h>
59 #include <sys/sysctl.h>
60 #include <geom/geom.h>
61 #include <geom/geom_int.h>
62 #include <machine/stdarg.h>
67 struct cdev *sc_alias;
72 static d_open_t g_dev_open;
73 static d_close_t g_dev_close;
74 static d_strategy_t g_dev_strategy;
75 static d_ioctl_t g_dev_ioctl;
77 static struct cdevsw g_dev_cdevsw = {
78 .d_version = D_VERSION,
80 .d_close = g_dev_close,
83 .d_ioctl = g_dev_ioctl,
84 .d_strategy = g_dev_strategy,
86 .d_flags = D_DISK | D_TRACKCLOSE,
89 static g_init_t g_dev_init;
90 static g_fini_t g_dev_fini;
91 static g_taste_t g_dev_taste;
92 static g_orphan_t g_dev_orphan;
93 static g_attrchanged_t g_dev_attrchanged;
95 static struct g_class g_dev_class = {
100 .taste = g_dev_taste,
101 .orphan = g_dev_orphan,
102 .attrchanged = g_dev_attrchanged
106 * We target 262144 (8 x 32768) sectors by default as this significantly
107 * increases the throughput on commonly used SSD's with a marginal
108 * increase in non-interruptible request latency.
110 static uint64_t g_dev_del_max_sectors = 262144;
111 SYSCTL_DECL(_kern_geom);
112 SYSCTL_NODE(_kern_geom, OID_AUTO, dev, CTLFLAG_RW, 0, "GEOM_DEV stuff");
113 SYSCTL_QUAD(_kern_geom_dev, OID_AUTO, delete_max_sectors, CTLFLAG_RW,
114 &g_dev_del_max_sectors, 0, "Maximum number of sectors in a single "
115 "delete request sent to the provider. Larger requests are chunked "
116 "so they can be interrupted. (0 = disable chunking)");
118 static char *dumpdev = NULL;
120 g_dev_init(struct g_class *mp)
123 dumpdev = kern_getenv("dumpdev");
127 g_dev_fini(struct g_class *mp)
135 g_dev_setdumpdev(struct cdev *dev, struct diocskerneldump_arg *kda,
138 struct g_kerneldump kd;
139 struct g_consumer *cp;
142 if (dev == NULL || kda == NULL)
143 return (set_dumper(NULL, NULL, td, 0, 0, NULL, 0, NULL));
149 error = g_io_getattr("GEOM::kerneldump", cp, &len, &kd);
153 error = set_dumper(&kd.di, devtoname(dev), td, kda->kda_compression,
154 kda->kda_encryption, kda->kda_key, kda->kda_encryptedkeysize,
155 kda->kda_encryptedkey);
157 dev->si_flags |= SI_DUMPDEV;
163 init_dumpdev(struct cdev *dev)
165 struct diocskerneldump_arg kda;
166 struct g_consumer *cp;
167 const char *devprefix = "/dev/", *devname;
171 bzero(&kda, sizeof(kda));
177 len = strlen(devprefix);
178 devname = devtoname(dev);
179 if (strcmp(devname, dumpdev) != 0 &&
180 (strncmp(dumpdev, devprefix, len) != 0 ||
181 strcmp(devname, dumpdev + len) != 0))
184 cp = (struct g_consumer *)dev->si_drv2;
185 error = g_access(cp, 1, 0, 0);
189 error = g_dev_setdumpdev(dev, &kda, curthread);
195 (void)g_access(cp, -1, 0, 0);
201 g_dev_destroy(void *arg, int flags __unused)
203 struct g_consumer *cp;
205 struct g_dev_softc *sc;
206 char buf[SPECNAMELEN + 6];
212 g_trace(G_T_TOPOLOGY, "g_dev_destroy(%p(%s))", cp, gp->name);
213 snprintf(buf, sizeof(buf), "cdev=%s", gp->name);
214 devctl_notify_f("GEOM", "DEV", "DESTROY", buf, M_WAITOK);
215 if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
216 g_access(cp, -cp->acr, -cp->acw, -cp->ace);
218 g_destroy_consumer(cp);
220 mtx_destroy(&sc->sc_mtx);
230 LIST_FOREACH(gp, &g_dev_class.geom, geom) {
231 printf("%s%s", p, gp->name);
238 g_dev_set_physpath(struct g_consumer *cp)
240 struct g_dev_softc *sc;
242 int error, physpath_len;
244 if (g_access(cp, 1, 0, 0) != 0)
248 physpath_len = MAXPATHLEN;
249 physpath = g_malloc(physpath_len, M_WAITOK|M_ZERO);
250 error = g_io_getattr("GEOM::physpath", cp, &physpath_len, physpath);
251 g_access(cp, -1, 0, 0);
252 if (error == 0 && strlen(physpath) != 0) {
253 struct cdev *dev, *old_alias_dev;
254 struct cdev **alias_devp;
257 old_alias_dev = sc->sc_alias;
258 alias_devp = (struct cdev **)&sc->sc_alias;
259 make_dev_physpath_alias(MAKEDEV_WAITOK, alias_devp, dev,
260 old_alias_dev, physpath);
261 } else if (sc->sc_alias) {
262 destroy_dev((struct cdev *)sc->sc_alias);
269 g_dev_set_media(struct g_consumer *cp)
271 struct g_dev_softc *sc;
273 char buf[SPECNAMELEN + 6];
277 snprintf(buf, sizeof(buf), "cdev=%s", dev->si_name);
278 devctl_notify_f("DEVFS", "CDEV", "MEDIACHANGE", buf, M_WAITOK);
279 devctl_notify_f("GEOM", "DEV", "MEDIACHANGE", buf, M_WAITOK);
282 snprintf(buf, sizeof(buf), "cdev=%s", dev->si_name);
283 devctl_notify_f("DEVFS", "CDEV", "MEDIACHANGE", buf, M_WAITOK);
284 devctl_notify_f("GEOM", "DEV", "MEDIACHANGE", buf, M_WAITOK);
289 g_dev_attrchanged(struct g_consumer *cp, const char *attr)
292 if (strcmp(attr, "GEOM::media") == 0) {
297 if (strcmp(attr, "GEOM::physpath") == 0) {
298 g_dev_set_physpath(cp);
304 g_dev_getprovider(struct cdev *dev)
306 struct g_consumer *cp;
311 if (dev->si_devsw != &g_dev_cdevsw)
314 return (cp->provider);
317 static struct g_geom *
318 g_dev_taste(struct g_class *mp, struct g_provider *pp, int insist __unused)
321 struct g_geom_alias *gap;
322 struct g_consumer *cp;
323 struct g_dev_softc *sc;
325 struct cdev *dev, *adev;
326 char buf[SPECNAMELEN + 6];
328 g_trace(G_T_TOPOLOGY, "dev_taste(%s,%s)", mp->name, pp->name);
330 gp = g_new_geomf(mp, "%s", pp->name);
331 sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
332 mtx_init(&sc->sc_mtx, "g_dev", NULL, MTX_DEF);
333 cp = g_new_consumer(gp);
335 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
336 error = g_attach(cp, pp);
338 ("g_dev_taste(%s) failed to g_attach, err=%d", pp->name, error));
339 error = make_dev_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK, &dev,
340 &g_dev_cdevsw, NULL, UID_ROOT, GID_OPERATOR, 0640, "%s", gp->name);
342 printf("%s: make_dev_p() failed (gp->name=%s, error=%d)\n",
343 __func__, gp->name, error);
345 g_destroy_consumer(cp);
347 mtx_destroy(&sc->sc_mtx);
351 dev->si_flags |= SI_UNMAPPED;
354 dev->si_iosize_max = MAXPHYS;
356 error = init_dumpdev(dev);
358 printf("%s: init_dumpdev() failed (gp->name=%s, error=%d)\n",
359 __func__, gp->name, error);
361 g_dev_attrchanged(cp, "GEOM::physpath");
362 snprintf(buf, sizeof(buf), "cdev=%s", gp->name);
363 devctl_notify_f("GEOM", "DEV", "CREATE", buf, M_WAITOK);
365 * Now add all the aliases for this drive
367 LIST_FOREACH(gap, &pp->geom->aliases, ga_next) {
368 error = make_dev_alias_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK, &adev, dev,
369 "%s", gap->ga_alias);
371 printf("%s: make_dev_alias_p() failed (name=%s, error=%d)\n",
372 __func__, gap->ga_alias, error);
375 snprintf(buf, sizeof(buf), "cdev=%s", gap->ga_alias);
376 devctl_notify_f("GEOM", "DEV", "CREATE", buf, M_WAITOK);
383 g_dev_open(struct cdev *dev, int flags, int fmt, struct thread *td)
385 struct g_consumer *cp;
386 struct g_dev_softc *sc;
391 return (ENXIO); /* g_dev_taste() not done yet */
392 g_trace(G_T_ACCESS, "g_dev_open(%s, %d, %d, %p)",
393 cp->geom->name, flags, fmt, td);
395 r = flags & FREAD ? 1 : 0;
396 w = flags & FWRITE ? 1 : 0;
398 e = flags & O_EXCL ? 1 : 0;
404 * This happens on attempt to open a device node with O_EXEC.
411 * When running in very secure mode, do not allow
412 * opens for writing of any disks.
414 error = securelevel_ge(td->td_ucred, 2);
419 error = g_access(cp, r, w, e);
423 mtx_lock(&sc->sc_mtx);
424 if (sc->sc_open == 0 && sc->sc_active != 0)
425 wakeup(&sc->sc_active);
426 sc->sc_open += r + w + e;
427 mtx_unlock(&sc->sc_mtx);
433 g_dev_close(struct cdev *dev, int flags, int fmt, struct thread *td)
435 struct g_consumer *cp;
436 struct g_dev_softc *sc;
442 g_trace(G_T_ACCESS, "g_dev_close(%s, %d, %d, %p)",
443 cp->geom->name, flags, fmt, td);
445 r = flags & FREAD ? -1 : 0;
446 w = flags & FWRITE ? -1 : 0;
448 e = flags & O_EXCL ? -1 : 0;
454 * The vgonel(9) - caused by eg. forced unmount of devfs - calls
455 * VOP_CLOSE(9) on devfs vnode without any FREAD or FWRITE flags,
456 * which would result in zero deltas, which in turn would cause
457 * panic in g_access(9).
459 * Note that we cannot zero the counters (ie. do "r = cp->acr"
460 * etc) instead, because the consumer might be opened in another
467 mtx_lock(&sc->sc_mtx);
468 sc->sc_open += r + w + e;
469 while (sc->sc_open == 0 && sc->sc_active != 0)
470 msleep(&sc->sc_active, &sc->sc_mtx, 0, "PRIBIO", 0);
471 mtx_unlock(&sc->sc_mtx);
473 error = g_access(cp, r, w, e);
479 * XXX: Until we have unmessed the ioctl situation, there is a race against
480 * XXX: a concurrent orphanization. We cannot close it by holding topology
481 * XXX: since that would prevent us from doing our job, and stalling events
482 * XXX: will break (actually: stall) the BSD disklabel hacks.
485 g_dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
487 struct g_consumer *cp;
488 struct g_provider *pp;
489 off_t offset, length, chunk, odd;
496 KASSERT(cp->acr || cp->acw,
497 ("Consumer with zero access count in g_dev_ioctl"));
499 i = IOCPARM_LEN(cmd);
501 case DIOCGSECTORSIZE:
502 *(u_int *)data = cp->provider->sectorsize;
503 if (*(u_int *)data == 0)
507 *(off_t *)data = cp->provider->mediasize;
508 if (*(off_t *)data == 0)
512 error = g_io_getattr("GEOM::fwsectors", cp, &i, data);
513 if (error == 0 && *(u_int *)data == 0)
517 error = g_io_getattr("GEOM::fwheads", cp, &i, data);
518 if (error == 0 && *(u_int *)data == 0)
521 case DIOCGFRONTSTUFF:
522 error = g_io_getattr("GEOM::frontstuff", cp, &i, data);
524 #ifdef COMPAT_FREEBSD11
525 case DIOCSKERNELDUMP_FREEBSD11:
527 struct diocskerneldump_arg kda;
529 bzero(&kda, sizeof(kda));
530 kda.kda_encryption = KERNELDUMP_ENC_NONE;
531 kda.kda_enable = (uint8_t)*(u_int *)data;
532 if (kda.kda_enable == 0)
533 error = g_dev_setdumpdev(NULL, NULL, td);
535 error = g_dev_setdumpdev(dev, &kda, td);
539 case DIOCSKERNELDUMP:
541 struct diocskerneldump_arg *kda;
542 uint8_t *encryptedkey;
544 kda = (struct diocskerneldump_arg *)data;
545 if (kda->kda_enable == 0) {
546 error = g_dev_setdumpdev(NULL, NULL, td);
550 if (kda->kda_encryption != KERNELDUMP_ENC_NONE) {
551 if (kda->kda_encryptedkeysize <= 0 ||
552 kda->kda_encryptedkeysize >
553 KERNELDUMP_ENCKEY_MAX_SIZE) {
556 encryptedkey = malloc(kda->kda_encryptedkeysize, M_TEMP,
558 error = copyin(kda->kda_encryptedkey, encryptedkey,
559 kda->kda_encryptedkeysize);
564 kda->kda_encryptedkey = encryptedkey;
565 error = g_dev_setdumpdev(dev, kda, td);
567 if (encryptedkey != NULL) {
568 explicit_bzero(encryptedkey, kda->kda_encryptedkeysize);
569 free(encryptedkey, M_TEMP);
571 explicit_bzero(kda, sizeof(*kda));
575 error = g_io_flush(cp);
578 offset = ((off_t *)data)[0];
579 length = ((off_t *)data)[1];
580 if ((offset % cp->provider->sectorsize) != 0 ||
581 (length % cp->provider->sectorsize) != 0 || length <= 0) {
582 printf("%s: offset=%jd length=%jd\n", __func__, offset,
589 if (g_dev_del_max_sectors != 0 && chunk >
590 g_dev_del_max_sectors * cp->provider->sectorsize) {
591 chunk = g_dev_del_max_sectors *
592 cp->provider->sectorsize;
593 if (cp->provider->stripesize > 0) {
594 odd = (offset + chunk +
595 cp->provider->stripeoffset) %
596 cp->provider->stripesize;
601 error = g_delete_data(cp, offset, chunk);
607 * Since the request size can be large, the service
608 * time can be is likewise. We make this ioctl
609 * interruptible by checking for signals for each bio.
616 error = g_io_getattr("GEOM::ident", cp, &i, data);
618 case DIOCGPROVIDERNAME:
621 strlcpy(data, pp->name, i);
623 case DIOCGSTRIPESIZE:
624 *(off_t *)data = cp->provider->stripesize;
626 case DIOCGSTRIPEOFFSET:
627 *(off_t *)data = cp->provider->stripeoffset;
630 error = g_io_getattr("GEOM::physpath", cp, &i, data);
631 if (error == 0 && *(char *)data == '\0')
635 struct diocgattr_arg *arg = (struct diocgattr_arg *)data;
637 if (arg->len > sizeof(arg->value)) {
641 error = g_io_getattr(arg->name, cp, &arg->len, &arg->value);
645 struct disk_zone_args *zone_args =(struct disk_zone_args *)data;
646 struct disk_zone_rep_entry *new_entries, *old_entries;
647 struct disk_zone_report *rep;
655 if (zone_args->zone_cmd == DISK_ZONE_REPORT_ZONES) {
657 rep = &zone_args->zone_params.report;
658 alloc_size = rep->entries_allocated *
659 sizeof(struct disk_zone_rep_entry);
661 new_entries = g_malloc(alloc_size,
663 old_entries = rep->entries;
664 rep->entries = new_entries;
666 error = g_io_zonecmd(zone_args, cp);
667 if ((zone_args->zone_cmd == DISK_ZONE_REPORT_ZONES)
670 error = copyout(new_entries, old_entries, alloc_size);
672 if ((old_entries != NULL)
674 rep->entries = old_entries;
676 if (new_entries != NULL)
681 if (cp->provider->geom->ioctl != NULL) {
682 error = cp->provider->geom->ioctl(cp->provider, cmd, data, fflag, td);
692 g_dev_done(struct bio *bp2)
694 struct g_consumer *cp;
695 struct g_dev_softc *sc;
701 bp = bp2->bio_parent;
702 bp->bio_error = bp2->bio_error;
703 bp->bio_completed = bp2->bio_completed;
704 bp->bio_resid = bp->bio_length - bp2->bio_completed;
705 if (bp2->bio_cmd == BIO_ZONE)
706 bcopy(&bp2->bio_zone, &bp->bio_zone, sizeof(bp->bio_zone));
708 if (bp2->bio_error != 0) {
709 g_trace(G_T_BIO, "g_dev_done(%p) had error %d",
710 bp2, bp2->bio_error);
711 bp->bio_flags |= BIO_ERROR;
713 g_trace(G_T_BIO, "g_dev_done(%p/%p) resid %ld completed %jd",
714 bp2, bp, bp2->bio_resid, (intmax_t)bp2->bio_completed);
718 mtx_lock(&sc->sc_mtx);
719 if ((--sc->sc_active) == 0) {
720 if (sc->sc_open == 0)
721 wakeup(&sc->sc_active);
722 if (sc->sc_dev == NULL)
725 mtx_unlock(&sc->sc_mtx);
727 g_post_event(g_dev_destroy, cp, M_NOWAIT, NULL);
732 g_dev_strategy(struct bio *bp)
734 struct g_consumer *cp;
737 struct g_dev_softc *sc;
739 KASSERT(bp->bio_cmd == BIO_READ ||
740 bp->bio_cmd == BIO_WRITE ||
741 bp->bio_cmd == BIO_DELETE ||
742 bp->bio_cmd == BIO_FLUSH ||
743 bp->bio_cmd == BIO_ZONE,
744 ("Wrong bio_cmd bio=%p cmd=%d", bp, bp->bio_cmd));
748 KASSERT(cp->acr || cp->acw,
749 ("Consumer with zero access count in g_dev_strategy"));
750 biotrack(bp, __func__);
752 if ((bp->bio_offset % cp->provider->sectorsize) != 0 ||
753 (bp->bio_bcount % cp->provider->sectorsize) != 0) {
754 bp->bio_resid = bp->bio_bcount;
755 biofinish(bp, NULL, EINVAL);
759 mtx_lock(&sc->sc_mtx);
760 KASSERT(sc->sc_open > 0, ("Closed device in g_dev_strategy"));
762 mtx_unlock(&sc->sc_mtx);
766 * XXX: This is not an ideal solution, but I believe it to
767 * XXX: deadlock safely, all things considered.
769 bp2 = g_clone_bio(bp);
772 pause("gdstrat", hz / 10);
774 KASSERT(bp2 != NULL, ("XXX: ENOMEM in a bad place"));
775 bp2->bio_done = g_dev_done;
777 "g_dev_strategy(%p/%p) offset %jd length %jd data %p cmd %d",
778 bp, bp2, (intmax_t)bp->bio_offset, (intmax_t)bp2->bio_length,
779 bp2->bio_data, bp2->bio_cmd);
780 g_io_request(bp2, cp);
781 KASSERT(cp->acr || cp->acw,
782 ("g_dev_strategy raced with g_dev_close and lost"));
789 * Called by devfs when asynchronous device destruction is completed.
790 * - Mark that we have no attached device any more.
791 * - If there are no outstanding requests, schedule geom destruction.
792 * Otherwise destruction will be scheduled later by g_dev_done().
796 g_dev_callback(void *arg)
798 struct g_consumer *cp;
799 struct g_dev_softc *sc;
804 g_trace(G_T_TOPOLOGY, "g_dev_callback(%p(%s))", cp, cp->geom->name);
806 mtx_lock(&sc->sc_mtx);
809 destroy = (sc->sc_active == 0);
810 mtx_unlock(&sc->sc_mtx);
812 g_post_event(g_dev_destroy, cp, M_WAITOK, NULL);
818 * Called from below when the provider orphaned us.
819 * - Clear any dump settings.
820 * - Request asynchronous device destruction to prevent any more requests
821 * from coming in. The provider is already marked with an error, so
822 * anything which comes in the interim will be returned immediately.
826 g_dev_orphan(struct g_consumer *cp)
829 struct g_dev_softc *sc;
834 g_trace(G_T_TOPOLOGY, "g_dev_orphan(%p(%s))", cp, cp->geom->name);
836 /* Reset any dump-area set on this device */
837 if (dev->si_flags & SI_DUMPDEV)
838 (void)set_dumper(NULL, NULL, curthread, 0, 0, NULL, 0, NULL);
840 /* Destroy the struct cdev *so we get no more requests */
841 destroy_dev_sched_cb(dev, g_dev_callback, cp);
844 DECLARE_GEOM_CLASS(g_dev_class, g_dev);