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;
68 #define SC_A_DESTROY (1 << 31)
69 #define SC_A_OPEN (1 << 30)
70 #define SC_A_ACTIVE (SC_A_OPEN - 1)
73 static d_open_t g_dev_open;
74 static d_close_t g_dev_close;
75 static d_strategy_t g_dev_strategy;
76 static d_ioctl_t g_dev_ioctl;
78 static struct cdevsw g_dev_cdevsw = {
79 .d_version = D_VERSION,
81 .d_close = g_dev_close,
84 .d_ioctl = g_dev_ioctl,
85 .d_strategy = g_dev_strategy,
87 .d_flags = D_DISK | D_TRACKCLOSE,
90 static g_init_t g_dev_init;
91 static g_fini_t g_dev_fini;
92 static g_taste_t g_dev_taste;
93 static g_orphan_t g_dev_orphan;
94 static g_attrchanged_t g_dev_attrchanged;
95 static g_resize_t g_dev_resize;
97 static struct g_class g_dev_class = {
102 .taste = g_dev_taste,
103 .orphan = g_dev_orphan,
104 .attrchanged = g_dev_attrchanged,
105 .resize = g_dev_resize
109 * We target 262144 (8 x 32768) sectors by default as this significantly
110 * increases the throughput on commonly used SSD's with a marginal
111 * increase in non-interruptible request latency.
113 static uint64_t g_dev_del_max_sectors = 262144;
114 SYSCTL_DECL(_kern_geom);
115 SYSCTL_NODE(_kern_geom, OID_AUTO, dev, CTLFLAG_RW, 0, "GEOM_DEV stuff");
116 SYSCTL_QUAD(_kern_geom_dev, OID_AUTO, delete_max_sectors, CTLFLAG_RW,
117 &g_dev_del_max_sectors, 0, "Maximum number of sectors in a single "
118 "delete request sent to the provider. Larger requests are chunked "
119 "so they can be interrupted. (0 = disable chunking)");
121 static char *dumpdev = NULL;
123 g_dev_init(struct g_class *mp)
126 dumpdev = kern_getenv("dumpdev");
130 g_dev_fini(struct g_class *mp)
138 g_dev_setdumpdev(struct cdev *dev, struct diocskerneldump_arg *kda)
140 struct g_kerneldump kd;
141 struct g_consumer *cp;
144 MPASS(dev != NULL && kda != NULL);
145 MPASS(kda->kda_index != KDA_REMOVE);
152 error = g_io_getattr("GEOM::kerneldump", cp, &len, &kd);
156 error = dumper_insert(&kd.di, devtoname(dev), kda);
158 dev->si_flags |= SI_DUMPDEV;
164 init_dumpdev(struct cdev *dev)
166 struct diocskerneldump_arg kda;
167 struct g_consumer *cp;
168 const char *devprefix = "/dev/", *devname;
172 bzero(&kda, sizeof(kda));
173 kda.kda_index = KDA_APPEND;
178 len = strlen(devprefix);
179 devname = devtoname(dev);
180 if (strcmp(devname, dumpdev) != 0 &&
181 (strncmp(dumpdev, devprefix, len) != 0 ||
182 strcmp(devname, dumpdev + len) != 0))
185 cp = (struct g_consumer *)dev->si_drv2;
186 error = g_access(cp, 1, 0, 0);
190 error = g_dev_setdumpdev(dev, &kda);
196 (void)g_access(cp, -1, 0, 0);
202 g_dev_destroy(void *arg, int flags __unused)
204 struct g_consumer *cp;
206 struct g_dev_softc *sc;
207 char buf[SPECNAMELEN + 6];
213 g_trace(G_T_TOPOLOGY, "g_dev_destroy(%p(%s))", cp, gp->name);
214 snprintf(buf, sizeof(buf), "cdev=%s", gp->name);
215 devctl_notify_f("GEOM", "DEV", "DESTROY", buf, M_WAITOK);
216 if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
217 g_access(cp, -cp->acr, -cp->acw, -cp->ace);
219 g_destroy_consumer(cp);
221 mtx_destroy(&sc->sc_mtx);
231 LIST_FOREACH(gp, &g_dev_class.geom, geom) {
232 printf("%s%s", p, gp->name);
239 g_dev_set_physpath(struct g_consumer *cp)
241 struct g_dev_softc *sc;
243 int error, physpath_len;
245 if (g_access(cp, 1, 0, 0) != 0)
249 physpath_len = MAXPATHLEN;
250 physpath = g_malloc(physpath_len, M_WAITOK|M_ZERO);
251 error = g_io_getattr("GEOM::physpath", cp, &physpath_len, physpath);
252 g_access(cp, -1, 0, 0);
253 if (error == 0 && strlen(physpath) != 0) {
254 struct cdev *dev, *old_alias_dev;
255 struct cdev **alias_devp;
258 old_alias_dev = sc->sc_alias;
259 alias_devp = (struct cdev **)&sc->sc_alias;
260 make_dev_physpath_alias(MAKEDEV_WAITOK, alias_devp, dev,
261 old_alias_dev, physpath);
262 } else if (sc->sc_alias) {
263 destroy_dev((struct cdev *)sc->sc_alias);
270 g_dev_set_media(struct g_consumer *cp)
272 struct g_dev_softc *sc;
274 char buf[SPECNAMELEN + 6];
278 snprintf(buf, sizeof(buf), "cdev=%s", dev->si_name);
279 devctl_notify_f("DEVFS", "CDEV", "MEDIACHANGE", buf, M_WAITOK);
280 devctl_notify_f("GEOM", "DEV", "MEDIACHANGE", buf, M_WAITOK);
283 snprintf(buf, sizeof(buf), "cdev=%s", dev->si_name);
284 devctl_notify_f("DEVFS", "CDEV", "MEDIACHANGE", buf, M_WAITOK);
285 devctl_notify_f("GEOM", "DEV", "MEDIACHANGE", buf, M_WAITOK);
290 g_dev_attrchanged(struct g_consumer *cp, const char *attr)
293 if (strcmp(attr, "GEOM::media") == 0) {
298 if (strcmp(attr, "GEOM::physpath") == 0) {
299 g_dev_set_physpath(cp);
305 g_dev_resize(struct g_consumer *cp)
307 char buf[SPECNAMELEN + 6];
309 snprintf(buf, sizeof(buf), "cdev=%s", cp->provider->name);
310 devctl_notify_f("GEOM", "DEV", "SIZECHANGE", buf, M_WAITOK);
314 g_dev_getprovider(struct cdev *dev)
316 struct g_consumer *cp;
321 if (dev->si_devsw != &g_dev_cdevsw)
324 return (cp->provider);
327 static struct g_geom *
328 g_dev_taste(struct g_class *mp, struct g_provider *pp, int insist __unused)
331 struct g_geom_alias *gap;
332 struct g_consumer *cp;
333 struct g_dev_softc *sc;
335 struct cdev *dev, *adev;
336 char buf[SPECNAMELEN + 6];
338 g_trace(G_T_TOPOLOGY, "dev_taste(%s,%s)", mp->name, pp->name);
340 gp = g_new_geomf(mp, "%s", pp->name);
341 sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
342 mtx_init(&sc->sc_mtx, "g_dev", NULL, MTX_DEF);
343 cp = g_new_consumer(gp);
345 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
346 error = g_attach(cp, pp);
348 ("g_dev_taste(%s) failed to g_attach, err=%d", pp->name, error));
349 error = make_dev_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK, &dev,
350 &g_dev_cdevsw, NULL, UID_ROOT, GID_OPERATOR, 0640, "%s", gp->name);
352 printf("%s: make_dev_p() failed (gp->name=%s, error=%d)\n",
353 __func__, gp->name, error);
355 g_destroy_consumer(cp);
357 mtx_destroy(&sc->sc_mtx);
361 dev->si_flags |= SI_UNMAPPED;
364 dev->si_iosize_max = MAXPHYS;
366 error = init_dumpdev(dev);
368 printf("%s: init_dumpdev() failed (gp->name=%s, error=%d)\n",
369 __func__, gp->name, error);
371 g_dev_attrchanged(cp, "GEOM::physpath");
372 snprintf(buf, sizeof(buf), "cdev=%s", gp->name);
373 devctl_notify_f("GEOM", "DEV", "CREATE", buf, M_WAITOK);
375 * Now add all the aliases for this drive
377 LIST_FOREACH(gap, &pp->geom->aliases, ga_next) {
378 error = make_dev_alias_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK, &adev, dev,
379 "%s", gap->ga_alias);
381 printf("%s: make_dev_alias_p() failed (name=%s, error=%d)\n",
382 __func__, gap->ga_alias, error);
385 snprintf(buf, sizeof(buf), "cdev=%s", gap->ga_alias);
386 devctl_notify_f("GEOM", "DEV", "CREATE", buf, M_WAITOK);
393 g_dev_open(struct cdev *dev, int flags, int fmt, struct thread *td)
395 struct g_consumer *cp;
396 struct g_dev_softc *sc;
401 return (ENXIO); /* g_dev_taste() not done yet */
402 g_trace(G_T_ACCESS, "g_dev_open(%s, %d, %d, %p)",
403 cp->geom->name, flags, fmt, td);
405 r = flags & FREAD ? 1 : 0;
406 w = flags & FWRITE ? 1 : 0;
408 e = flags & O_EXCL ? 1 : 0;
414 * This happens on attempt to open a device node with O_EXEC.
421 * When running in very secure mode, do not allow
422 * opens for writing of any disks.
424 error = securelevel_ge(td->td_ucred, 2);
429 error = g_access(cp, r, w, e);
433 mtx_lock(&sc->sc_mtx);
434 if (sc->sc_open == 0 && (sc->sc_active & SC_A_ACTIVE) != 0)
435 wakeup(&sc->sc_active);
436 sc->sc_open += r + w + e;
437 if (sc->sc_open == 0)
438 atomic_clear_int(&sc->sc_active, SC_A_OPEN);
440 atomic_set_int(&sc->sc_active, SC_A_OPEN);
441 mtx_unlock(&sc->sc_mtx);
447 g_dev_close(struct cdev *dev, int flags, int fmt, struct thread *td)
449 struct g_consumer *cp;
450 struct g_dev_softc *sc;
456 g_trace(G_T_ACCESS, "g_dev_close(%s, %d, %d, %p)",
457 cp->geom->name, flags, fmt, td);
459 r = flags & FREAD ? -1 : 0;
460 w = flags & FWRITE ? -1 : 0;
462 e = flags & O_EXCL ? -1 : 0;
468 * The vgonel(9) - caused by eg. forced unmount of devfs - calls
469 * VOP_CLOSE(9) on devfs vnode without any FREAD or FWRITE flags,
470 * which would result in zero deltas, which in turn would cause
471 * panic in g_access(9).
473 * Note that we cannot zero the counters (ie. do "r = cp->acr"
474 * etc) instead, because the consumer might be opened in another
481 mtx_lock(&sc->sc_mtx);
482 sc->sc_open += r + w + e;
483 if (sc->sc_open == 0)
484 atomic_clear_int(&sc->sc_active, SC_A_OPEN);
486 atomic_set_int(&sc->sc_active, SC_A_OPEN);
487 while (sc->sc_open == 0 && (sc->sc_active & SC_A_ACTIVE) != 0)
488 msleep(&sc->sc_active, &sc->sc_mtx, 0, "g_dev_close", hz / 10);
489 mtx_unlock(&sc->sc_mtx);
491 error = g_access(cp, r, w, e);
497 * XXX: Until we have unmessed the ioctl situation, there is a race against
498 * XXX: a concurrent orphanization. We cannot close it by holding topology
499 * XXX: since that would prevent us from doing our job, and stalling events
500 * XXX: will break (actually: stall) the BSD disklabel hacks.
503 g_dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
505 struct g_consumer *cp;
506 struct g_provider *pp;
507 off_t offset, length, chunk, odd;
509 #ifdef COMPAT_FREEBSD12
510 struct diocskerneldump_arg kda_copy;
517 KASSERT(cp->acr || cp->acw,
518 ("Consumer with zero access count in g_dev_ioctl"));
520 i = IOCPARM_LEN(cmd);
522 case DIOCGSECTORSIZE:
523 *(u_int *)data = cp->provider->sectorsize;
524 if (*(u_int *)data == 0)
528 *(off_t *)data = cp->provider->mediasize;
529 if (*(off_t *)data == 0)
533 error = g_io_getattr("GEOM::fwsectors", cp, &i, data);
534 if (error == 0 && *(u_int *)data == 0)
538 error = g_io_getattr("GEOM::fwheads", cp, &i, data);
539 if (error == 0 && *(u_int *)data == 0)
542 case DIOCGFRONTSTUFF:
543 error = g_io_getattr("GEOM::frontstuff", cp, &i, data);
545 #ifdef COMPAT_FREEBSD11
546 case DIOCSKERNELDUMP_FREEBSD11:
548 struct diocskerneldump_arg kda;
550 gone_in(13, "FreeBSD 11.x ABI compat");
552 bzero(&kda, sizeof(kda));
553 kda.kda_encryption = KERNELDUMP_ENC_NONE;
554 kda.kda_index = (*(u_int *)data ? 0 : KDA_REMOVE_ALL);
555 if (kda.kda_index == KDA_REMOVE_ALL)
556 error = dumper_remove(devtoname(dev), &kda);
558 error = g_dev_setdumpdev(dev, &kda);
562 #ifdef COMPAT_FREEBSD12
563 case DIOCSKERNELDUMP_FREEBSD12:
565 struct diocskerneldump_arg_freebsd12 *kda12;
567 gone_in(14, "FreeBSD 12.x ABI compat");
569 kda12 = (void *)data;
570 memcpy(&kda_copy, kda12, sizeof(kda_copy));
571 kda_copy.kda_index = (kda12->kda12_enable ?
574 explicit_bzero(kda12, sizeof(*kda12));
575 /* Kludge to pass kda_copy to kda in fallthrough. */
576 data = (void *)&kda_copy;
580 case DIOCSKERNELDUMP:
582 struct diocskerneldump_arg *kda;
583 uint8_t *encryptedkey;
585 kda = (struct diocskerneldump_arg *)data;
586 if (kda->kda_index == KDA_REMOVE_ALL ||
587 kda->kda_index == KDA_REMOVE_DEV ||
588 kda->kda_index == KDA_REMOVE) {
589 error = dumper_remove(devtoname(dev), kda);
590 explicit_bzero(kda, sizeof(*kda));
594 if (kda->kda_encryption != KERNELDUMP_ENC_NONE) {
595 if (kda->kda_encryptedkeysize == 0 ||
596 kda->kda_encryptedkeysize >
597 KERNELDUMP_ENCKEY_MAX_SIZE) {
598 explicit_bzero(kda, sizeof(*kda));
601 encryptedkey = malloc(kda->kda_encryptedkeysize, M_TEMP,
603 error = copyin(kda->kda_encryptedkey, encryptedkey,
604 kda->kda_encryptedkeysize);
609 kda->kda_encryptedkey = encryptedkey;
610 error = g_dev_setdumpdev(dev, kda);
612 if (encryptedkey != NULL) {
613 explicit_bzero(encryptedkey, kda->kda_encryptedkeysize);
614 free(encryptedkey, M_TEMP);
616 explicit_bzero(kda, sizeof(*kda));
620 error = g_io_flush(cp);
623 offset = ((off_t *)data)[0];
624 length = ((off_t *)data)[1];
625 if ((offset % cp->provider->sectorsize) != 0 ||
626 (length % cp->provider->sectorsize) != 0 || length <= 0) {
627 printf("%s: offset=%jd length=%jd\n", __func__, offset,
632 if ((cp->provider->mediasize > 0) &&
633 (offset >= cp->provider->mediasize)) {
635 * Catch out-of-bounds requests here. The problem is
636 * that due to historical GEOM I/O implementation
637 * peculatities, g_delete_data() would always return
638 * success for requests starting just the next byte
639 * after providers media boundary. Condition check on
640 * non-zero media size, since that condition would
641 * (most likely) cause ENXIO instead.
648 if (g_dev_del_max_sectors != 0 && chunk >
649 g_dev_del_max_sectors * cp->provider->sectorsize) {
650 chunk = g_dev_del_max_sectors *
651 cp->provider->sectorsize;
652 if (cp->provider->stripesize > 0) {
653 odd = (offset + chunk +
654 cp->provider->stripeoffset) %
655 cp->provider->stripesize;
660 error = g_delete_data(cp, offset, chunk);
666 * Since the request size can be large, the service
667 * time can be is likewise. We make this ioctl
668 * interruptible by checking for signals for each bio.
675 error = g_io_getattr("GEOM::ident", cp, &i, data);
677 case DIOCGPROVIDERNAME:
680 strlcpy(data, pp->name, i);
682 case DIOCGSTRIPESIZE:
683 *(off_t *)data = cp->provider->stripesize;
685 case DIOCGSTRIPEOFFSET:
686 *(off_t *)data = cp->provider->stripeoffset;
689 error = g_io_getattr("GEOM::physpath", cp, &i, data);
690 if (error == 0 && *(char *)data == '\0')
694 struct diocgattr_arg *arg = (struct diocgattr_arg *)data;
696 if (arg->len > sizeof(arg->value)) {
700 error = g_io_getattr(arg->name, cp, &arg->len, &arg->value);
704 struct disk_zone_args *zone_args =(struct disk_zone_args *)data;
705 struct disk_zone_rep_entry *new_entries, *old_entries;
706 struct disk_zone_report *rep;
714 if (zone_args->zone_cmd == DISK_ZONE_REPORT_ZONES) {
715 rep = &zone_args->zone_params.report;
716 #define MAXENTRIES (MAXPHYS / sizeof(struct disk_zone_rep_entry))
717 if (rep->entries_allocated > MAXENTRIES)
718 rep->entries_allocated = MAXENTRIES;
719 alloc_size = rep->entries_allocated *
720 sizeof(struct disk_zone_rep_entry);
722 new_entries = g_malloc(alloc_size,
724 old_entries = rep->entries;
725 rep->entries = new_entries;
727 error = g_io_zonecmd(zone_args, cp);
728 if (zone_args->zone_cmd == DISK_ZONE_REPORT_ZONES &&
729 alloc_size != 0 && error == 0)
730 error = copyout(new_entries, old_entries, alloc_size);
731 if (old_entries != NULL && rep != NULL)
732 rep->entries = old_entries;
733 if (new_entries != NULL)
738 if (cp->provider->geom->ioctl != NULL) {
739 error = cp->provider->geom->ioctl(cp->provider, cmd, data, fflag, td);
749 g_dev_done(struct bio *bp2)
751 struct g_consumer *cp;
752 struct g_dev_softc *sc;
758 bp = bp2->bio_parent;
759 bp->bio_error = bp2->bio_error;
760 bp->bio_completed = bp2->bio_completed;
761 bp->bio_resid = bp->bio_length - bp2->bio_completed;
762 if (bp2->bio_cmd == BIO_ZONE)
763 bcopy(&bp2->bio_zone, &bp->bio_zone, sizeof(bp->bio_zone));
765 if (bp2->bio_error != 0) {
766 g_trace(G_T_BIO, "g_dev_done(%p) had error %d",
767 bp2, bp2->bio_error);
768 bp->bio_flags |= BIO_ERROR;
770 g_trace(G_T_BIO, "g_dev_done(%p/%p) resid %ld completed %jd",
771 bp2, bp, bp2->bio_resid, (intmax_t)bp2->bio_completed);
774 active = atomic_fetchadd_int(&sc->sc_active, -1) - 1;
775 if ((active & SC_A_ACTIVE) == 0) {
776 if ((active & SC_A_OPEN) == 0)
777 wakeup(&sc->sc_active);
778 if (active & SC_A_DESTROY)
779 g_post_event(g_dev_destroy, cp, M_NOWAIT, NULL);
785 g_dev_strategy(struct bio *bp)
787 struct g_consumer *cp;
790 struct g_dev_softc *sc;
792 KASSERT(bp->bio_cmd == BIO_READ ||
793 bp->bio_cmd == BIO_WRITE ||
794 bp->bio_cmd == BIO_DELETE ||
795 bp->bio_cmd == BIO_FLUSH ||
796 bp->bio_cmd == BIO_ZONE,
797 ("Wrong bio_cmd bio=%p cmd=%d", bp, bp->bio_cmd));
801 KASSERT(cp->acr || cp->acw,
802 ("Consumer with zero access count in g_dev_strategy"));
803 biotrack(bp, __func__);
805 if ((bp->bio_offset % cp->provider->sectorsize) != 0 ||
806 (bp->bio_bcount % cp->provider->sectorsize) != 0) {
807 bp->bio_resid = bp->bio_bcount;
808 biofinish(bp, NULL, EINVAL);
812 KASSERT(sc->sc_open > 0, ("Closed device in g_dev_strategy"));
813 atomic_add_int(&sc->sc_active, 1);
817 * XXX: This is not an ideal solution, but I believe it to
818 * XXX: deadlock safely, all things considered.
820 bp2 = g_clone_bio(bp);
823 pause("gdstrat", hz / 10);
825 KASSERT(bp2 != NULL, ("XXX: ENOMEM in a bad place"));
826 bp2->bio_done = g_dev_done;
828 "g_dev_strategy(%p/%p) offset %jd length %jd data %p cmd %d",
829 bp, bp2, (intmax_t)bp->bio_offset, (intmax_t)bp2->bio_length,
830 bp2->bio_data, bp2->bio_cmd);
831 g_io_request(bp2, cp);
832 KASSERT(cp->acr || cp->acw,
833 ("g_dev_strategy raced with g_dev_close and lost"));
840 * Called by devfs when asynchronous device destruction is completed.
841 * - Mark that we have no attached device any more.
842 * - If there are no outstanding requests, schedule geom destruction.
843 * Otherwise destruction will be scheduled later by g_dev_done().
847 g_dev_callback(void *arg)
849 struct g_consumer *cp;
850 struct g_dev_softc *sc;
855 g_trace(G_T_TOPOLOGY, "g_dev_callback(%p(%s))", cp, cp->geom->name);
859 active = atomic_fetchadd_int(&sc->sc_active, SC_A_DESTROY);
860 if ((active & SC_A_ACTIVE) == 0)
861 g_post_event(g_dev_destroy, cp, M_WAITOK, NULL);
867 * Called from below when the provider orphaned us.
868 * - Clear any dump settings.
869 * - Request asynchronous device destruction to prevent any more requests
870 * from coming in. The provider is already marked with an error, so
871 * anything which comes in the interim will be returned immediately.
875 g_dev_orphan(struct g_consumer *cp)
878 struct g_dev_softc *sc;
883 g_trace(G_T_TOPOLOGY, "g_dev_orphan(%p(%s))", cp, cp->geom->name);
885 /* Reset any dump-area set on this device */
886 if (dev->si_flags & SI_DUMPDEV) {
887 struct diocskerneldump_arg kda;
889 bzero(&kda, sizeof(kda));
890 kda.kda_index = KDA_REMOVE_DEV;
891 (void)dumper_remove(devtoname(dev), &kda);
894 /* Destroy the struct cdev *so we get no more requests */
896 destroy_dev_sched_cb(dev, g_dev_callback, cp);
899 DECLARE_GEOM_CLASS(g_dev_class, g_dev);