2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1999-2002 Poul-Henning Kamp
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/systm.h>
38 #include <sys/mutex.h>
39 #include <sys/module.h>
40 #include <sys/malloc.h>
42 #include <sys/vnode.h>
43 #include <sys/queue.h>
46 #include <sys/ctype.h>
47 #include <sys/ucred.h>
48 #include <sys/taskqueue.h>
49 #include <machine/stdarg.h>
51 #include <fs/devfs/devfs_int.h>
54 static MALLOC_DEFINE(M_DEVT, "cdev", "cdev storage");
57 static void destroy_devl(struct cdev *dev);
58 static int destroy_dev_sched_cbl(struct cdev *dev,
59 void (*cb)(void *), void *arg);
60 static void destroy_dev_tq(void *ctx, int pending);
61 static int make_dev_credv(int flags, struct cdev **dres, struct cdevsw *devsw,
62 int unit, struct ucred *cr, uid_t uid, gid_t gid, int mode, const char *fmt,
65 static struct cdev_priv_list cdevp_free_list =
66 TAILQ_HEAD_INITIALIZER(cdevp_free_list);
67 static SLIST_HEAD(free_cdevsw, cdevsw) cdevsw_gt_post_list =
68 SLIST_HEAD_INITIALIZER(cdevsw_gt_post_list);
78 * Free all the memory collected while the cdev mutex was
79 * locked. Since devmtx is after the system map mutex, free() cannot
80 * be called immediately and is postponed until cdev mutex can be
84 dev_unlock_and_free(void)
86 struct cdev_priv_list cdp_free;
87 struct free_cdevsw csw_free;
88 struct cdev_priv *cdp;
91 mtx_assert(&devmtx, MA_OWNED);
94 * Make the local copy of the list heads while the dev_mtx is
95 * held. Free it later.
97 TAILQ_INIT(&cdp_free);
98 TAILQ_CONCAT(&cdp_free, &cdevp_free_list, cdp_list);
99 csw_free = cdevsw_gt_post_list;
100 SLIST_INIT(&cdevsw_gt_post_list);
104 while ((cdp = TAILQ_FIRST(&cdp_free)) != NULL) {
105 TAILQ_REMOVE(&cdp_free, cdp, cdp_list);
106 devfs_free(&cdp->cdp_c);
108 while ((csw = SLIST_FIRST(&csw_free)) != NULL) {
109 SLIST_REMOVE_HEAD(&csw_free, d_postfree_list);
115 dev_free_devlocked(struct cdev *cdev)
117 struct cdev_priv *cdp;
119 mtx_assert(&devmtx, MA_OWNED);
120 cdp = cdev2priv(cdev);
121 KASSERT((cdp->cdp_flags & CDP_UNREF_DTR) == 0,
122 ("destroy_dev() was not called after delist_dev(%p)", cdev));
123 TAILQ_INSERT_HEAD(&cdevp_free_list, cdp, cdp_list);
127 cdevsw_free_devlocked(struct cdevsw *csw)
130 mtx_assert(&devmtx, MA_OWNED);
131 SLIST_INSERT_HEAD(&cdevsw_gt_post_list, csw, d_postfree_list);
142 dev_ref(struct cdev *dev)
145 mtx_assert(&devmtx, MA_NOTOWNED);
152 dev_refl(struct cdev *dev)
155 mtx_assert(&devmtx, MA_OWNED);
160 dev_rel(struct cdev *dev)
164 mtx_assert(&devmtx, MA_NOTOWNED);
167 KASSERT(dev->si_refcount >= 0,
168 ("dev_rel(%s) gave negative count", devtoname(dev)));
169 if (dev->si_devsw == NULL && dev->si_refcount == 0) {
170 LIST_REMOVE(dev, si_list);
179 dev_refthread(struct cdev *dev, int *ref)
182 struct cdev_priv *cdp;
184 mtx_assert(&devmtx, MA_NOTOWNED);
185 if ((dev->si_flags & SI_ETERNAL) != 0) {
187 return (dev->si_devsw);
189 cdp = cdev2priv(dev);
190 mtx_lock(&cdp->cdp_threadlock);
193 if ((cdp->cdp_flags & CDP_SCHED_DTR) == 0)
194 atomic_add_long(&dev->si_threadcount, 1);
198 mtx_unlock(&cdp->cdp_threadlock);
204 devvn_refthread(struct vnode *vp, struct cdev **devp, int *ref)
207 struct cdev_priv *cdp;
210 mtx_assert(&devmtx, MA_NOTOWNED);
211 if ((vp->v_vflag & VV_ETERNALDEV) != 0) {
215 KASSERT((dev->si_flags & SI_ETERNAL) != 0,
216 ("Not eternal cdev"));
219 KASSERT(csw != NULL, ("Eternal cdev is destroyed"));
231 cdp = cdev2priv(dev);
232 mtx_lock(&cdp->cdp_threadlock);
233 if ((cdp->cdp_flags & CDP_SCHED_DTR) == 0) {
236 atomic_add_long(&dev->si_threadcount, 1);
238 mtx_unlock(&cdp->cdp_threadlock);
248 dev_relthread(struct cdev *dev, int ref)
251 mtx_assert(&devmtx, MA_NOTOWNED);
254 KASSERT(dev->si_threadcount > 0,
255 ("%s threadcount is wrong", dev->si_name));
256 atomic_subtract_rel_long(&dev->si_threadcount, 1);
285 /* Define a dead_cdevsw for use when devices leave unexpectedly. */
287 #define dead_open (d_open_t *)enxio
288 #define dead_close (d_close_t *)enxio
289 #define dead_read (d_read_t *)enxio
290 #define dead_write (d_write_t *)enxio
291 #define dead_ioctl (d_ioctl_t *)enxio
292 #define dead_poll (d_poll_t *)enodev
293 #define dead_mmap (d_mmap_t *)enodev
296 dead_strategy(struct bio *bp)
299 biofinish(bp, NULL, ENXIO);
302 #define dead_dump (dumper_t *)enxio
303 #define dead_kqfilter (d_kqfilter_t *)enxio
304 #define dead_mmap_single (d_mmap_single_t *)enodev
306 static struct cdevsw dead_cdevsw = {
307 .d_version = D_VERSION,
309 .d_close = dead_close,
311 .d_write = dead_write,
312 .d_ioctl = dead_ioctl,
315 .d_strategy = dead_strategy,
318 .d_kqfilter = dead_kqfilter,
319 .d_mmap_single = dead_mmap_single
322 /* Default methods if driver does not specify method */
324 #define null_open (d_open_t *)nullop
325 #define null_close (d_close_t *)nullop
326 #define no_read (d_read_t *)enodev
327 #define no_write (d_write_t *)enodev
328 #define no_ioctl (d_ioctl_t *)enodev
329 #define no_mmap (d_mmap_t *)enodev
330 #define no_kqfilter (d_kqfilter_t *)enodev
331 #define no_mmap_single (d_mmap_single_t *)enodev
334 no_strategy(struct bio *bp)
337 biofinish(bp, NULL, ENODEV);
341 no_poll(struct cdev *dev __unused, int events, struct thread *td __unused)
344 return (poll_no_poll(events));
347 #define no_dump (dumper_t *)enodev
350 giant_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
355 dsw = dev_refthread(dev, &ref);
359 retval = dsw->d_gianttrick->d_open(dev, oflags, devtype, td);
361 dev_relthread(dev, ref);
366 giant_fdopen(struct cdev *dev, int oflags, struct thread *td, struct file *fp)
371 dsw = dev_refthread(dev, &ref);
375 retval = dsw->d_gianttrick->d_fdopen(dev, oflags, td, fp);
377 dev_relthread(dev, ref);
382 giant_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
387 dsw = dev_refthread(dev, &ref);
391 retval = dsw->d_gianttrick->d_close(dev, fflag, devtype, td);
393 dev_relthread(dev, ref);
398 giant_strategy(struct bio *bp)
405 dsw = dev_refthread(dev, &ref);
407 biofinish(bp, NULL, ENXIO);
411 dsw->d_gianttrick->d_strategy(bp);
413 dev_relthread(dev, ref);
417 giant_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
422 dsw = dev_refthread(dev, &ref);
426 retval = dsw->d_gianttrick->d_ioctl(dev, cmd, data, fflag, td);
428 dev_relthread(dev, ref);
433 giant_read(struct cdev *dev, struct uio *uio, int ioflag)
438 dsw = dev_refthread(dev, &ref);
442 retval = dsw->d_gianttrick->d_read(dev, uio, ioflag);
444 dev_relthread(dev, ref);
449 giant_write(struct cdev *dev, struct uio *uio, int ioflag)
454 dsw = dev_refthread(dev, &ref);
458 retval = dsw->d_gianttrick->d_write(dev, uio, ioflag);
460 dev_relthread(dev, ref);
465 giant_poll(struct cdev *dev, int events, struct thread *td)
470 dsw = dev_refthread(dev, &ref);
474 retval = dsw->d_gianttrick->d_poll(dev, events, td);
476 dev_relthread(dev, ref);
481 giant_kqfilter(struct cdev *dev, struct knote *kn)
486 dsw = dev_refthread(dev, &ref);
490 retval = dsw->d_gianttrick->d_kqfilter(dev, kn);
492 dev_relthread(dev, ref);
497 giant_mmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr, int nprot,
498 vm_memattr_t *memattr)
503 dsw = dev_refthread(dev, &ref);
507 retval = dsw->d_gianttrick->d_mmap(dev, offset, paddr, nprot,
510 dev_relthread(dev, ref);
515 giant_mmap_single(struct cdev *dev, vm_ooffset_t *offset, vm_size_t size,
516 vm_object_t *object, int nprot)
521 dsw = dev_refthread(dev, &ref);
525 retval = dsw->d_gianttrick->d_mmap_single(dev, offset, size, object,
528 dev_relthread(dev, ref);
533 notify(struct cdev *dev, const char *ev, int flags)
535 static const char prefix[] = "cdev=";
541 mflags = (flags & MAKEDEV_NOWAIT) ? M_NOWAIT : M_WAITOK;
542 namelen = strlen(dev->si_name);
543 data = malloc(namelen + sizeof(prefix), M_TEMP, mflags);
546 memcpy(data, prefix, sizeof(prefix) - 1);
547 memcpy(data + sizeof(prefix) - 1, dev->si_name, namelen + 1);
548 devctl_notify_f("DEVFS", "CDEV", ev, data, mflags);
553 notify_create(struct cdev *dev, int flags)
556 notify(dev, "CREATE", flags);
560 notify_destroy(struct cdev *dev)
563 notify(dev, "DESTROY", MAKEDEV_WAITOK);
567 newdev(struct make_dev_args *args, struct cdev *si)
572 mtx_assert(&devmtx, MA_OWNED);
573 csw = args->mda_devsw;
575 if (csw->d_flags & D_NEEDMINOR) {
576 /* We may want to return an existing device */
577 LIST_FOREACH(si2, &csw->d_devs, si_list) {
578 if (dev2unit(si2) == args->mda_unit) {
579 dev_free_devlocked(si);
586 * If we're returning an existing device, we should make sure
587 * it isn't already initialized. This would have been caught
588 * in consumers anyways, but it's good to catch such a case
589 * early. We still need to complete initialization of the
590 * device, and we'll use whatever make_dev_args were passed in
593 KASSERT(si2 == NULL || (si2->si_flags & SI_NAMED) == 0,
594 ("make_dev() by driver %s on pre-existing device (min=%x, name=%s)",
595 args->mda_devsw->d_name, dev2unit(si2), devtoname(si2)));
597 si->si_drv0 = args->mda_unit;
598 si->si_drv1 = args->mda_si_drv1;
599 si->si_drv2 = args->mda_si_drv2;
600 /* Only push to csw->d_devs if it's not a cloned device. */
603 LIST_INSERT_HEAD(&csw->d_devs, si, si_list);
605 KASSERT(si->si_devsw == csw,
606 ("%s: inconsistent devsw between clone_create() and make_dev()",
613 fini_cdevsw(struct cdevsw *devsw)
617 if (devsw->d_gianttrick != NULL) {
618 gt = devsw->d_gianttrick;
619 memcpy(devsw, gt, sizeof *devsw);
620 cdevsw_free_devlocked(gt);
621 devsw->d_gianttrick = NULL;
623 devsw->d_flags &= ~D_INIT;
627 prep_cdevsw(struct cdevsw *devsw, int flags)
631 mtx_assert(&devmtx, MA_OWNED);
632 if (devsw->d_flags & D_INIT)
634 if (devsw->d_flags & D_NEEDGIANT) {
636 dsw2 = malloc(sizeof *dsw2, M_DEVT,
637 (flags & MAKEDEV_NOWAIT) ? M_NOWAIT : M_WAITOK);
639 if (dsw2 == NULL && !(devsw->d_flags & D_INIT))
643 if (devsw->d_flags & D_INIT) {
645 cdevsw_free_devlocked(dsw2);
649 if (devsw->d_version != D_VERSION_03) {
651 "WARNING: Device driver \"%s\" has wrong version %s\n",
652 devsw->d_name == NULL ? "???" : devsw->d_name,
653 "and is disabled. Recompile KLD module.");
654 devsw->d_open = dead_open;
655 devsw->d_close = dead_close;
656 devsw->d_read = dead_read;
657 devsw->d_write = dead_write;
658 devsw->d_ioctl = dead_ioctl;
659 devsw->d_poll = dead_poll;
660 devsw->d_mmap = dead_mmap;
661 devsw->d_mmap_single = dead_mmap_single;
662 devsw->d_strategy = dead_strategy;
663 devsw->d_dump = dead_dump;
664 devsw->d_kqfilter = dead_kqfilter;
667 if (devsw->d_flags & D_NEEDGIANT) {
668 printf("WARNING: Device \"%s\" is Giant locked and may be "
669 "deleted before FreeBSD 14.0.\n",
670 devsw->d_name == NULL ? "???" : devsw->d_name);
671 if (devsw->d_gianttrick == NULL) {
672 memcpy(dsw2, devsw, sizeof *dsw2);
673 devsw->d_gianttrick = dsw2;
678 #define FIXUP(member, noop, giant) \
680 if (devsw->member == NULL) { \
681 devsw->member = noop; \
682 } else if (devsw->d_flags & D_NEEDGIANT) \
683 devsw->member = giant; \
687 FIXUP(d_open, null_open, giant_open);
688 FIXUP(d_fdopen, NULL, giant_fdopen);
689 FIXUP(d_close, null_close, giant_close);
690 FIXUP(d_read, no_read, giant_read);
691 FIXUP(d_write, no_write, giant_write);
692 FIXUP(d_ioctl, no_ioctl, giant_ioctl);
693 FIXUP(d_poll, no_poll, giant_poll);
694 FIXUP(d_mmap, no_mmap, giant_mmap);
695 FIXUP(d_strategy, no_strategy, giant_strategy);
696 FIXUP(d_kqfilter, no_kqfilter, giant_kqfilter);
697 FIXUP(d_mmap_single, no_mmap_single, giant_mmap_single);
699 if (devsw->d_dump == NULL) devsw->d_dump = no_dump;
701 LIST_INIT(&devsw->d_devs);
703 devsw->d_flags |= D_INIT;
706 cdevsw_free_devlocked(dsw2);
711 prep_devname(struct cdev *dev, const char *fmt, va_list ap)
714 char *from, *q, *s, *to;
716 mtx_assert(&devmtx, MA_OWNED);
718 len = vsnrprintf(dev->si_name, sizeof(dev->si_name), 32, fmt, ap);
719 if (len > sizeof(dev->si_name) - 1)
720 return (ENAMETOOLONG);
722 /* Strip leading slashes. */
723 for (from = dev->si_name; *from == '/'; from++)
726 for (to = dev->si_name; *from != '\0'; from++, to++) {
728 * Spaces and double quotation marks cause
729 * problems for the devctl(4) protocol.
730 * Reject names containing those characters.
732 if (isspace(*from) || *from == '"')
734 /* Treat multiple sequential slashes as single. */
735 while (from[0] == '/' && from[1] == '/')
737 /* Trailing slash is considered invalid. */
738 if (from[0] == '/' && from[1] == '\0')
744 if (dev->si_name[0] == '\0')
747 /* Disallow "." and ".." components. */
748 for (s = dev->si_name;;) {
749 for (q = s; *q != '/' && *q != '\0'; q++)
751 if (q - s == 1 && s[0] == '.')
753 if (q - s == 2 && s[0] == '.' && s[1] == '.')
760 if (devfs_dev_exists(dev->si_name) != 0)
767 make_dev_args_init_impl(struct make_dev_args *args, size_t sz)
775 make_dev_sv(struct make_dev_args *args1, struct cdev **dres,
776 const char *fmt, va_list ap)
778 struct cdev *dev, *dev_new;
779 struct make_dev_args args;
782 bzero(&args, sizeof(args));
783 if (sizeof(args) < args1->mda_size)
785 bcopy(args1, &args, args1->mda_size);
786 KASSERT((args.mda_flags & MAKEDEV_WAITOK) == 0 ||
787 (args.mda_flags & MAKEDEV_NOWAIT) == 0,
788 ("make_dev_sv: both WAITOK and NOWAIT specified"));
789 dev_new = devfs_alloc(args.mda_flags);
793 res = prep_cdevsw(args.mda_devsw, args.mda_flags);
799 dev = newdev(&args, dev_new);
800 if ((dev->si_flags & SI_NAMED) == 0) {
801 res = prep_devname(dev, fmt, ap);
803 if ((args.mda_flags & MAKEDEV_CHECKNAME) == 0) {
805 "make_dev_sv: bad si_name (error=%d, si_name=%s)",
808 if (dev == dev_new) {
809 LIST_REMOVE(dev, si_list);
817 if ((args.mda_flags & MAKEDEV_REF) != 0)
819 if ((args.mda_flags & MAKEDEV_ETERNAL) != 0)
820 dev->si_flags |= SI_ETERNAL;
821 KASSERT(!(dev->si_flags & SI_NAMED),
822 ("make_dev() by driver %s on pre-existing device (min=%x, name=%s)",
823 args.mda_devsw->d_name, dev2unit(dev), devtoname(dev)));
824 dev->si_flags |= SI_NAMED;
825 if (args.mda_cr != NULL)
826 dev->si_cred = crhold(args.mda_cr);
827 dev->si_uid = args.mda_uid;
828 dev->si_gid = args.mda_gid;
829 dev->si_mode = args.mda_mode;
832 clean_unrhdrl(devfs_inos);
833 dev_unlock_and_free();
835 notify_create(dev, args.mda_flags);
842 make_dev_s(struct make_dev_args *args, struct cdev **dres,
843 const char *fmt, ...)
849 res = make_dev_sv(args, dres, fmt, ap);
855 make_dev_credv(int flags, struct cdev **dres, struct cdevsw *devsw, int unit,
856 struct ucred *cr, uid_t uid, gid_t gid, int mode, const char *fmt,
859 struct make_dev_args args;
861 make_dev_args_init(&args);
862 args.mda_flags = flags;
863 args.mda_devsw = devsw;
867 args.mda_mode = mode;
868 args.mda_unit = unit;
869 return (make_dev_sv(&args, dres, fmt, ap));
873 make_dev(struct cdevsw *devsw, int unit, uid_t uid, gid_t gid, int mode,
874 const char *fmt, ...)
881 res = make_dev_credv(0, &dev, devsw, unit, NULL, uid, gid, mode, fmt,
884 KASSERT(res == 0 && dev != NULL,
885 ("make_dev: failed make_dev_credv (error=%d)", res));
890 make_dev_cred(struct cdevsw *devsw, int unit, struct ucred *cr, uid_t uid,
891 gid_t gid, int mode, const char *fmt, ...)
898 res = make_dev_credv(0, &dev, devsw, unit, cr, uid, gid, mode, fmt, ap);
901 KASSERT(res == 0 && dev != NULL,
902 ("make_dev_cred: failed make_dev_credv (error=%d)", res));
907 make_dev_credf(int flags, struct cdevsw *devsw, int unit, struct ucred *cr,
908 uid_t uid, gid_t gid, int mode, const char *fmt, ...)
915 res = make_dev_credv(flags, &dev, devsw, unit, cr, uid, gid, mode,
919 KASSERT(((flags & MAKEDEV_NOWAIT) != 0 && res == ENOMEM) ||
920 ((flags & MAKEDEV_CHECKNAME) != 0 && res != ENOMEM) || res == 0,
921 ("make_dev_credf: failed make_dev_credv (error=%d)", res));
922 return (res == 0 ? dev : NULL);
926 make_dev_p(int flags, struct cdev **cdev, struct cdevsw *devsw,
927 struct ucred *cr, uid_t uid, gid_t gid, int mode, const char *fmt, ...)
933 res = make_dev_credv(flags, cdev, devsw, 0, cr, uid, gid, mode,
937 KASSERT(((flags & MAKEDEV_NOWAIT) != 0 && res == ENOMEM) ||
938 ((flags & MAKEDEV_CHECKNAME) != 0 && res != ENOMEM) || res == 0,
939 ("make_dev_p: failed make_dev_credv (error=%d)", res));
944 dev_dependsl(struct cdev *pdev, struct cdev *cdev)
947 cdev->si_parent = pdev;
948 cdev->si_flags |= SI_CHILD;
949 LIST_INSERT_HEAD(&pdev->si_children, cdev, si_siblings);
954 dev_depends(struct cdev *pdev, struct cdev *cdev)
958 dev_dependsl(pdev, cdev);
963 make_dev_alias_v(int flags, struct cdev **cdev, struct cdev *pdev,
964 const char *fmt, va_list ap)
969 KASSERT(pdev != NULL, ("make_dev_alias_v: pdev is NULL"));
970 KASSERT((flags & MAKEDEV_WAITOK) == 0 || (flags & MAKEDEV_NOWAIT) == 0,
971 ("make_dev_alias_v: both WAITOK and NOWAIT specified"));
972 KASSERT((flags & ~(MAKEDEV_WAITOK | MAKEDEV_NOWAIT |
973 MAKEDEV_CHECKNAME)) == 0,
974 ("make_dev_alias_v: invalid flags specified (flags=%02x)", flags));
976 dev = devfs_alloc(flags);
980 dev->si_flags |= SI_ALIAS;
981 error = prep_devname(dev, fmt, ap);
983 if ((flags & MAKEDEV_CHECKNAME) == 0) {
984 panic("make_dev_alias_v: bad si_name "
985 "(error=%d, si_name=%s)", error, dev->si_name);
991 dev->si_flags |= SI_NAMED;
993 dev_dependsl(pdev, dev);
994 clean_unrhdrl(devfs_inos);
997 notify_create(dev, flags);
1004 make_dev_alias(struct cdev *pdev, const char *fmt, ...)
1011 res = make_dev_alias_v(MAKEDEV_WAITOK, &dev, pdev, fmt, ap);
1014 KASSERT(res == 0 && dev != NULL,
1015 ("make_dev_alias: failed make_dev_alias_v (error=%d)", res));
1020 make_dev_alias_p(int flags, struct cdev **cdev, struct cdev *pdev,
1021 const char *fmt, ...)
1027 res = make_dev_alias_v(flags, cdev, pdev, fmt, ap);
1033 make_dev_physpath_alias(int flags, struct cdev **cdev, struct cdev *pdev,
1034 struct cdev *old_alias, const char *physpath)
1038 int max_parentpath_len;
1040 int devfspathbuf_len;
1046 physpath_len = strlen(physpath);
1048 if (physpath_len == 0)
1051 if (strncmp("id1,", physpath, 4) == 0) {
1054 if (physpath_len == 0)
1058 max_parentpath_len = SPECNAMELEN - physpath_len - /*/*/1;
1059 parentpath_len = strlen(pdev->si_name);
1060 if (max_parentpath_len < parentpath_len) {
1062 printf("WARNING: Unable to alias %s "
1063 "to %s/%s - path too long\n",
1064 pdev->si_name, physpath, pdev->si_name);
1069 mflags = (flags & MAKEDEV_NOWAIT) ? M_NOWAIT : M_WAITOK;
1070 devfspathbuf_len = physpath_len + /*/*/1 + parentpath_len + /*NUL*/1;
1071 devfspath = malloc(devfspathbuf_len, M_DEVBUF, mflags);
1072 if (devfspath == NULL) {
1077 sprintf(devfspath, "%s/%s", physpath, pdev->si_name);
1078 if (old_alias != NULL && strcmp(old_alias->si_name, devfspath) == 0) {
1079 /* Retain the existing alias. */
1084 ret = make_dev_alias_p(flags, cdev, pdev, "%s", devfspath);
1087 if (old_alias != NULL)
1088 destroy_dev(old_alias);
1089 if (devfspath != NULL)
1090 free(devfspath, M_DEVBUF);
1095 destroy_devl(struct cdev *dev)
1098 struct cdev_privdata *p;
1099 struct cdev_priv *cdp;
1101 mtx_assert(&devmtx, MA_OWNED);
1102 KASSERT(dev->si_flags & SI_NAMED,
1103 ("WARNING: Driver mistake: destroy_dev on %d\n", dev2unit(dev)));
1104 KASSERT((dev->si_flags & SI_ETERNAL) == 0,
1105 ("WARNING: Driver mistake: destroy_dev on eternal %d\n",
1108 cdp = cdev2priv(dev);
1109 if ((cdp->cdp_flags & CDP_UNREF_DTR) == 0) {
1111 * Avoid race with dev_rel(), e.g. from the populate
1112 * loop. If CDP_UNREF_DTR flag is set, the reference
1113 * to be dropped at the end of destroy_devl() was
1114 * already taken by delist_dev_locked().
1121 /* Remove name marking */
1122 dev->si_flags &= ~SI_NAMED;
1124 /* If we are a child, remove us from the parents list */
1125 if (dev->si_flags & SI_CHILD) {
1126 LIST_REMOVE(dev, si_siblings);
1127 dev->si_flags &= ~SI_CHILD;
1130 /* Kill our children */
1131 while (!LIST_EMPTY(&dev->si_children))
1132 destroy_devl(LIST_FIRST(&dev->si_children));
1134 /* Remove from clone list */
1135 if (dev->si_flags & SI_CLONELIST) {
1136 LIST_REMOVE(dev, si_clone);
1137 dev->si_flags &= ~SI_CLONELIST;
1140 mtx_lock(&cdp->cdp_threadlock);
1141 csw = dev->si_devsw;
1142 dev->si_devsw = NULL; /* already NULL for SI_ALIAS */
1143 while (csw != NULL && csw->d_purge != NULL && dev->si_threadcount) {
1145 mtx_unlock(&cdp->cdp_threadlock);
1146 msleep(csw, &devmtx, PRIBIO, "devprg", hz/10);
1147 mtx_lock(&cdp->cdp_threadlock);
1148 if (dev->si_threadcount)
1149 printf("Still %lu threads in %s\n",
1150 dev->si_threadcount, devtoname(dev));
1152 while (dev->si_threadcount != 0) {
1153 /* Use unique dummy wait ident */
1154 mtx_unlock(&cdp->cdp_threadlock);
1155 msleep(&csw, &devmtx, PRIBIO, "devdrn", hz / 10);
1156 mtx_lock(&cdp->cdp_threadlock);
1159 mtx_unlock(&cdp->cdp_threadlock);
1161 if ((cdp->cdp_flags & CDP_UNREF_DTR) == 0) {
1162 /* avoid out of order notify events */
1163 notify_destroy(dev);
1165 mtx_lock(&cdevpriv_mtx);
1166 while ((p = LIST_FIRST(&cdp->cdp_fdpriv)) != NULL) {
1167 devfs_destroy_cdevpriv(p);
1168 mtx_lock(&cdevpriv_mtx);
1170 mtx_unlock(&cdevpriv_mtx);
1175 bzero(&dev->__si_u, sizeof(dev->__si_u));
1177 if (!(dev->si_flags & SI_ALIAS)) {
1178 /* Remove from cdevsw list */
1179 LIST_REMOVE(dev, si_list);
1181 /* If cdevsw has no more struct cdev *'s, clean it */
1182 if (LIST_EMPTY(&csw->d_devs)) {
1184 wakeup(&csw->d_devs);
1187 dev->si_flags &= ~SI_ALIAS;
1188 cdp->cdp_flags &= ~CDP_UNREF_DTR;
1191 if (dev->si_refcount > 0)
1192 LIST_INSERT_HEAD(&dead_cdevsw.d_devs, dev, si_list);
1194 dev_free_devlocked(dev);
1198 delist_dev_locked(struct cdev *dev)
1200 struct cdev_priv *cdp;
1203 mtx_assert(&devmtx, MA_OWNED);
1204 cdp = cdev2priv(dev);
1205 if ((cdp->cdp_flags & CDP_UNREF_DTR) != 0)
1207 cdp->cdp_flags |= CDP_UNREF_DTR;
1210 LIST_FOREACH(child, &dev->si_children, si_siblings)
1211 delist_dev_locked(child);
1213 /* ensure the destroy event is queued in order */
1214 notify_destroy(dev);
1219 * This function will delist a character device and its children from
1220 * the directory listing and create a destroy event without waiting
1221 * for all character device references to go away. At some later point
1222 * destroy_dev() must be called to complete the character device
1223 * destruction. After calling this function the character device name
1224 * can instantly be re-used.
1227 delist_dev(struct cdev *dev)
1230 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, "delist_dev");
1232 delist_dev_locked(dev);
1237 destroy_dev(struct cdev *dev)
1240 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, "destroy_dev");
1243 dev_unlock_and_free();
1247 devtoname(struct cdev *dev)
1250 return (dev->si_name);
1254 dev_stdclone(char *name, char **namep, const char *stem, int *unit)
1259 if (strncmp(stem, name, i) != 0)
1261 if (!isdigit(name[i]))
1264 if (name[i] == '0' && isdigit(name[i+1]))
1266 while (isdigit(name[i])) {
1268 u += name[i++] - '0';
1281 * Helper functions for cloning device drivers.
1283 * The objective here is to make it unnecessary for the device drivers to
1284 * use rman or similar to manage their unit number space. Due to the way
1285 * we do "on-demand" devices, using rman or other "private" methods
1286 * will be very tricky to lock down properly once we lock down this file.
1288 * Instead we give the drivers these routines which puts the struct cdev *'s
1289 * that are to be managed on their own list, and gives the driver the ability
1290 * to ask for the first free unit number or a given specified unit number.
1292 * In addition these routines support paired devices (pty, nmdm and similar)
1293 * by respecting a number of "flag" bits in the minor number.
1298 LIST_HEAD(,cdev) head;
1302 clone_setup(struct clonedevs **cdp)
1305 *cdp = malloc(sizeof **cdp, M_DEVBUF, M_WAITOK | M_ZERO);
1306 LIST_INIT(&(*cdp)->head);
1310 clone_create(struct clonedevs **cdp, struct cdevsw *csw, int *up,
1311 struct cdev **dp, int extra)
1313 struct clonedevs *cd;
1314 struct cdev *dev, *ndev, *dl, *de;
1315 struct make_dev_args args;
1318 KASSERT(*cdp != NULL,
1319 ("clone_setup() not called in driver \"%s\"", csw->d_name));
1320 KASSERT(!(extra & CLONE_UNITMASK),
1321 ("Illegal extra bits (0x%x) in clone_create", extra));
1322 KASSERT(*up <= CLONE_UNITMASK,
1323 ("Too high unit (0x%x) in clone_create", *up));
1324 KASSERT(csw->d_flags & D_NEEDMINOR,
1325 ("clone_create() on cdevsw without minor numbers"));
1329 * Search the list for a lot of things in one go:
1330 * A preexisting match is returned immediately.
1331 * The lowest free unit number if we are passed -1, and the place
1332 * in the list where we should insert that new element.
1333 * The place to insert a specified unit number, if applicable
1334 * the end of the list.
1337 ndev = devfs_alloc(MAKEDEV_WAITOK);
1339 prep_cdevsw(csw, MAKEDEV_WAITOK);
1343 LIST_FOREACH(dev, &cd->head, si_clone) {
1344 KASSERT(dev->si_flags & SI_CLONELIST,
1345 ("Dev %p(%s) should be on clonelist", dev, dev->si_name));
1347 if (u == (unit | extra)) {
1353 if (unit == -1 && u == low) {
1357 } else if (u < (unit | extra)) {
1360 } else if (u > (unit | extra)) {
1366 unit = low & CLONE_UNITMASK;
1367 make_dev_args_init(&args);
1368 args.mda_unit = unit | extra;
1369 args.mda_devsw = csw;
1370 dev = newdev(&args, ndev);
1371 if (dev->si_flags & SI_CLONELIST) {
1372 printf("dev %p (%s) is on clonelist\n", dev, dev->si_name);
1373 printf("unit=%d, low=%d, extra=0x%x\n", unit, low, extra);
1374 LIST_FOREACH(dev, &cd->head, si_clone) {
1375 printf("\t%p %s\n", dev, dev->si_name);
1379 KASSERT(!(dev->si_flags & SI_CLONELIST),
1380 ("Dev %p(%s) should not be on clonelist", dev, dev->si_name));
1382 LIST_INSERT_BEFORE(dl, dev, si_clone);
1383 else if (de != NULL)
1384 LIST_INSERT_AFTER(de, dev, si_clone);
1386 LIST_INSERT_HEAD(&cd->head, dev, si_clone);
1387 dev->si_flags |= SI_CLONELIST;
1389 dev_unlock_and_free();
1394 * Kill everything still on the list. The driver should already have
1395 * disposed of any softc hung of the struct cdev *'s at this time.
1398 clone_cleanup(struct clonedevs **cdp)
1401 struct cdev_priv *cp;
1402 struct clonedevs *cd;
1408 while (!LIST_EMPTY(&cd->head)) {
1409 dev = LIST_FIRST(&cd->head);
1410 LIST_REMOVE(dev, si_clone);
1411 KASSERT(dev->si_flags & SI_CLONELIST,
1412 ("Dev %p(%s) should be on clonelist", dev, dev->si_name));
1413 dev->si_flags &= ~SI_CLONELIST;
1414 cp = cdev2priv(dev);
1415 if (!(cp->cdp_flags & CDP_SCHED_DTR)) {
1416 cp->cdp_flags |= CDP_SCHED_DTR;
1417 KASSERT(dev->si_flags & SI_NAMED,
1418 ("Driver has goofed in cloning underways udev %jx unit %x",
1419 (uintmax_t)dev2udev(dev), dev2unit(dev)));
1423 dev_unlock_and_free();
1428 static TAILQ_HEAD(, cdev_priv) dev_ddtr =
1429 TAILQ_HEAD_INITIALIZER(dev_ddtr);
1430 static struct task dev_dtr_task = TASK_INITIALIZER(0, destroy_dev_tq, NULL);
1433 destroy_dev_tq(void *ctx, int pending)
1435 struct cdev_priv *cp;
1441 while (!TAILQ_EMPTY(&dev_ddtr)) {
1442 cp = TAILQ_FIRST(&dev_ddtr);
1444 KASSERT(cp->cdp_flags & CDP_SCHED_DTR,
1445 ("cdev %p in dev_destroy_tq without CDP_SCHED_DTR", cp));
1446 TAILQ_REMOVE(&dev_ddtr, cp, cdp_dtr_list);
1447 cb = cp->cdp_dtr_cb;
1448 cb_arg = cp->cdp_dtr_cb_arg;
1450 dev_unlock_and_free();
1460 * devmtx shall be locked on entry. devmtx will be unlocked after
1464 destroy_dev_sched_cbl(struct cdev *dev, void (*cb)(void *), void *arg)
1466 struct cdev_priv *cp;
1468 mtx_assert(&devmtx, MA_OWNED);
1469 cp = cdev2priv(dev);
1470 if (cp->cdp_flags & CDP_SCHED_DTR) {
1475 cp->cdp_flags |= CDP_SCHED_DTR;
1476 cp->cdp_dtr_cb = cb;
1477 cp->cdp_dtr_cb_arg = arg;
1478 TAILQ_INSERT_TAIL(&dev_ddtr, cp, cdp_dtr_list);
1480 taskqueue_enqueue(taskqueue_swi_giant, &dev_dtr_task);
1485 destroy_dev_sched_cb(struct cdev *dev, void (*cb)(void *), void *arg)
1489 return (destroy_dev_sched_cbl(dev, cb, arg));
1493 destroy_dev_sched(struct cdev *dev)
1496 return (destroy_dev_sched_cb(dev, NULL, NULL));
1500 destroy_dev_drain(struct cdevsw *csw)
1504 while (!LIST_EMPTY(&csw->d_devs)) {
1505 msleep(&csw->d_devs, &devmtx, PRIBIO, "devscd", hz/10);
1511 drain_dev_clone_events(void)
1514 sx_xlock(&clone_drain_lock);
1515 sx_xunlock(&clone_drain_lock);
1518 #include "opt_ddb.h"
1520 #include <sys/kernel.h>
1522 #include <ddb/ddb.h>
1524 DB_SHOW_COMMAND(cdev, db_show_cdev)
1526 struct cdev_priv *cdp;
1532 TAILQ_FOREACH(cdp, &cdevp_list, cdp_list) {
1534 db_printf("%s %p\n", dev->si_name, dev);
1541 dev = (struct cdev *)addr;
1542 cdp = cdev2priv(dev);
1543 db_printf("dev %s ref %d use %ld thr %ld inuse %u fdpriv %p\n",
1544 dev->si_name, dev->si_refcount, dev->si_usecount,
1545 dev->si_threadcount, cdp->cdp_inuse, cdp->cdp_fdpriv.lh_first);
1546 db_printf("devsw %p si_drv0 %d si_drv1 %p si_drv2 %p\n",
1547 dev->si_devsw, dev->si_drv0, dev->si_drv1, dev->si_drv2);
1548 flags = dev->si_flags;
1549 #define SI_FLAG(flag) do { \
1550 if (flags & (flag)) { \
1551 if (buf[0] != '\0') \
1552 strlcat(buf, ", ", sizeof(buf)); \
1553 strlcat(buf, (#flag) + 3, sizeof(buf)); \
1558 SI_FLAG(SI_ETERNAL);
1562 SI_FLAG(SI_DUMPDEV);
1563 SI_FLAG(SI_CLONELIST);
1564 db_printf("si_flags %s\n", buf);
1566 flags = cdp->cdp_flags;
1567 #define CDP_FLAG(flag) do { \
1568 if (flags & (flag)) { \
1569 if (buf[0] != '\0') \
1570 strlcat(buf, ", ", sizeof(buf)); \
1571 strlcat(buf, (#flag) + 4, sizeof(buf)); \
1576 CDP_FLAG(CDP_ACTIVE);
1577 CDP_FLAG(CDP_SCHED_DTR);
1578 db_printf("cdp_flags %s\n", buf);