The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/kern_conf.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
    3  *
    4  * Copyright (c) 1999-2002 Poul-Henning Kamp
    5  * All rights reserved.
    6  *
    7  * Redistribution and use in source and binary forms, with or without
    8  * modification, are permitted provided that the following conditions
    9  * are met:
   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.
   15  *
   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
   26  * SUCH DAMAGE.
   27  */
   28 
   29 #include <sys/cdefs.h>
   30 __FBSDID("$FreeBSD$");
   31 
   32 #include <sys/param.h>
   33 #include <sys/kernel.h>
   34 #include <sys/systm.h>
   35 #include <sys/bus.h>
   36 #include <sys/bio.h>
   37 #include <sys/lock.h>
   38 #include <sys/mutex.h>
   39 #include <sys/module.h>
   40 #include <sys/malloc.h>
   41 #include <sys/conf.h>
   42 #include <sys/vnode.h>
   43 #include <sys/queue.h>
   44 #include <sys/poll.h>
   45 #include <sys/sx.h>
   46 #include <sys/ctype.h>
   47 #include <sys/ucred.h>
   48 #include <sys/taskqueue.h>
   49 #include <machine/stdarg.h>
   50 
   51 #include <fs/devfs/devfs_int.h>
   52 #include <vm/vm.h>
   53 
   54 static MALLOC_DEFINE(M_DEVT, "cdev", "cdev storage");
   55 
   56 struct mtx devmtx;
   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,
   63     va_list ap);
   64 
   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);
   69 
   70 void
   71 dev_lock(void)
   72 {
   73 
   74         mtx_lock(&devmtx);
   75 }
   76 
   77 /*
   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
   81  * dropped.
   82  */
   83 static void
   84 dev_unlock_and_free(void)
   85 {
   86         struct cdev_priv_list cdp_free;
   87         struct free_cdevsw csw_free;
   88         struct cdev_priv *cdp;
   89         struct cdevsw *csw;
   90 
   91         mtx_assert(&devmtx, MA_OWNED);
   92 
   93         /*
   94          * Make the local copy of the list heads while the dev_mtx is
   95          * held. Free it later.
   96          */
   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);
  101 
  102         mtx_unlock(&devmtx);
  103 
  104         while ((cdp = TAILQ_FIRST(&cdp_free)) != NULL) {
  105                 TAILQ_REMOVE(&cdp_free, cdp, cdp_list);
  106                 devfs_free(&cdp->cdp_c);
  107         }
  108         while ((csw = SLIST_FIRST(&csw_free)) != NULL) {
  109                 SLIST_REMOVE_HEAD(&csw_free, d_postfree_list);
  110                 free(csw, M_DEVT);
  111         }
  112 }
  113 
  114 static void
  115 dev_free_devlocked(struct cdev *cdev)
  116 {
  117         struct cdev_priv *cdp;
  118 
  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);
  124 }
  125 
  126 static void
  127 cdevsw_free_devlocked(struct cdevsw *csw)
  128 {
  129 
  130         mtx_assert(&devmtx, MA_OWNED);
  131         SLIST_INSERT_HEAD(&cdevsw_gt_post_list, csw, d_postfree_list);
  132 }
  133 
  134 void
  135 dev_unlock(void)
  136 {
  137 
  138         mtx_unlock(&devmtx);
  139 }
  140 
  141 void
  142 dev_ref(struct cdev *dev)
  143 {
  144 
  145         mtx_assert(&devmtx, MA_NOTOWNED);
  146         mtx_lock(&devmtx);
  147         dev->si_refcount++;
  148         mtx_unlock(&devmtx);
  149 }
  150 
  151 void
  152 dev_refl(struct cdev *dev)
  153 {
  154 
  155         mtx_assert(&devmtx, MA_OWNED);
  156         dev->si_refcount++;
  157 }
  158 
  159 void
  160 dev_rel(struct cdev *dev)
  161 {
  162         int flag = 0;
  163 
  164         mtx_assert(&devmtx, MA_NOTOWNED);
  165         dev_lock();
  166         dev->si_refcount--;
  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);
  171                 flag = 1;
  172         }
  173         dev_unlock();
  174         if (flag)
  175                 devfs_free(dev);
  176 }
  177 
  178 struct cdevsw *
  179 dev_refthread(struct cdev *dev, int *ref)
  180 {
  181         struct cdevsw *csw;
  182         struct cdev_priv *cdp;
  183 
  184         mtx_assert(&devmtx, MA_NOTOWNED);
  185         if ((dev->si_flags & SI_ETERNAL) != 0) {
  186                 *ref = 0;
  187                 return (dev->si_devsw);
  188         }
  189         cdp = cdev2priv(dev);
  190         mtx_lock(&cdp->cdp_threadlock);
  191         csw = dev->si_devsw;
  192         if (csw != NULL) {
  193                 if ((cdp->cdp_flags & CDP_SCHED_DTR) == 0)
  194                         atomic_add_long(&dev->si_threadcount, 1);
  195                 else
  196                         csw = NULL;
  197         }
  198         mtx_unlock(&cdp->cdp_threadlock);
  199         *ref = 1;
  200         return (csw);
  201 }
  202 
  203 struct cdevsw *
  204 devvn_refthread(struct vnode *vp, struct cdev **devp, int *ref)
  205 {
  206         struct cdevsw *csw;
  207         struct cdev_priv *cdp;
  208         struct cdev *dev;
  209 
  210         mtx_assert(&devmtx, MA_NOTOWNED);
  211         if ((vp->v_vflag & VV_ETERNALDEV) != 0) {
  212                 dev = vp->v_rdev;
  213                 if (dev == NULL)
  214                         return (NULL);
  215                 KASSERT((dev->si_flags & SI_ETERNAL) != 0,
  216                     ("Not eternal cdev"));
  217                 *ref = 0;
  218                 csw = dev->si_devsw;
  219                 KASSERT(csw != NULL, ("Eternal cdev is destroyed"));
  220                 *devp = dev;
  221                 return (csw);
  222         }
  223 
  224         csw = NULL;
  225         VI_LOCK(vp);
  226         dev = vp->v_rdev;
  227         if (dev == NULL) {
  228                 VI_UNLOCK(vp);
  229                 return (NULL);
  230         }
  231         cdp = cdev2priv(dev);
  232         mtx_lock(&cdp->cdp_threadlock);
  233         if ((cdp->cdp_flags & CDP_SCHED_DTR) == 0) {
  234                 csw = dev->si_devsw;
  235                 if (csw != NULL)
  236                         atomic_add_long(&dev->si_threadcount, 1);
  237         }
  238         mtx_unlock(&cdp->cdp_threadlock);
  239         VI_UNLOCK(vp);
  240         if (csw != NULL) {
  241                 *devp = dev;
  242                 *ref = 1;
  243         }
  244         return (csw);
  245 }
  246 
  247 void    
  248 dev_relthread(struct cdev *dev, int ref)
  249 {
  250 
  251         mtx_assert(&devmtx, MA_NOTOWNED);
  252         if (!ref)
  253                 return;
  254         KASSERT(dev->si_threadcount > 0,
  255             ("%s threadcount is wrong", dev->si_name));
  256         atomic_subtract_rel_long(&dev->si_threadcount, 1);
  257 }
  258 
  259 int
  260 nullop(void)
  261 {
  262 
  263         return (0);
  264 }
  265 
  266 int
  267 eopnotsupp(void)
  268 {
  269 
  270         return (EOPNOTSUPP);
  271 }
  272 
  273 static int
  274 enxio(void)
  275 {
  276         return (ENXIO);
  277 }
  278 
  279 static int
  280 enodev(void)
  281 {
  282         return (ENODEV);
  283 }
  284 
  285 /* Define a dead_cdevsw for use when devices leave unexpectedly. */
  286 
  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
  294 
  295 static void
  296 dead_strategy(struct bio *bp)
  297 {
  298 
  299         biofinish(bp, NULL, ENXIO);
  300 }
  301 
  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
  305 
  306 static struct cdevsw dead_cdevsw = {
  307         .d_version =    D_VERSION,
  308         .d_open =       dead_open,
  309         .d_close =      dead_close,
  310         .d_read =       dead_read,
  311         .d_write =      dead_write,
  312         .d_ioctl =      dead_ioctl,
  313         .d_poll =       dead_poll,
  314         .d_mmap =       dead_mmap,
  315         .d_strategy =   dead_strategy,
  316         .d_name =       "dead",
  317         .d_dump =       dead_dump,
  318         .d_kqfilter =   dead_kqfilter,
  319         .d_mmap_single = dead_mmap_single
  320 };
  321 
  322 /* Default methods if driver does not specify method */
  323 
  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
  332 
  333 static void
  334 no_strategy(struct bio *bp)
  335 {
  336 
  337         biofinish(bp, NULL, ENODEV);
  338 }
  339 
  340 static int
  341 no_poll(struct cdev *dev __unused, int events, struct thread *td __unused)
  342 {
  343 
  344         return (poll_no_poll(events));
  345 }
  346 
  347 #define no_dump         (dumper_t *)enodev
  348 
  349 static int
  350 giant_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
  351 {
  352         struct cdevsw *dsw;
  353         int ref, retval;
  354 
  355         dsw = dev_refthread(dev, &ref);
  356         if (dsw == NULL)
  357                 return (ENXIO);
  358         mtx_lock(&Giant);
  359         retval = dsw->d_gianttrick->d_open(dev, oflags, devtype, td);
  360         mtx_unlock(&Giant);
  361         dev_relthread(dev, ref);
  362         return (retval);
  363 }
  364 
  365 static int
  366 giant_fdopen(struct cdev *dev, int oflags, struct thread *td, struct file *fp)
  367 {
  368         struct cdevsw *dsw;
  369         int ref, retval;
  370 
  371         dsw = dev_refthread(dev, &ref);
  372         if (dsw == NULL)
  373                 return (ENXIO);
  374         mtx_lock(&Giant);
  375         retval = dsw->d_gianttrick->d_fdopen(dev, oflags, td, fp);
  376         mtx_unlock(&Giant);
  377         dev_relthread(dev, ref);
  378         return (retval);
  379 }
  380 
  381 static int
  382 giant_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
  383 {
  384         struct cdevsw *dsw;
  385         int ref, retval;
  386 
  387         dsw = dev_refthread(dev, &ref);
  388         if (dsw == NULL)
  389                 return (ENXIO);
  390         mtx_lock(&Giant);
  391         retval = dsw->d_gianttrick->d_close(dev, fflag, devtype, td);
  392         mtx_unlock(&Giant);
  393         dev_relthread(dev, ref);
  394         return (retval);
  395 }
  396 
  397 static void
  398 giant_strategy(struct bio *bp)
  399 {
  400         struct cdevsw *dsw;
  401         struct cdev *dev;
  402         int ref;
  403 
  404         dev = bp->bio_dev;
  405         dsw = dev_refthread(dev, &ref);
  406         if (dsw == NULL) {
  407                 biofinish(bp, NULL, ENXIO);
  408                 return;
  409         }
  410         mtx_lock(&Giant);
  411         dsw->d_gianttrick->d_strategy(bp);
  412         mtx_unlock(&Giant);
  413         dev_relthread(dev, ref);
  414 }
  415 
  416 static int
  417 giant_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
  418 {
  419         struct cdevsw *dsw;
  420         int ref, retval;
  421 
  422         dsw = dev_refthread(dev, &ref);
  423         if (dsw == NULL)
  424                 return (ENXIO);
  425         mtx_lock(&Giant);
  426         retval = dsw->d_gianttrick->d_ioctl(dev, cmd, data, fflag, td);
  427         mtx_unlock(&Giant);
  428         dev_relthread(dev, ref);
  429         return (retval);
  430 }
  431   
  432 static int
  433 giant_read(struct cdev *dev, struct uio *uio, int ioflag)
  434 {
  435         struct cdevsw *dsw;
  436         int ref, retval;
  437 
  438         dsw = dev_refthread(dev, &ref);
  439         if (dsw == NULL)
  440                 return (ENXIO);
  441         mtx_lock(&Giant);
  442         retval = dsw->d_gianttrick->d_read(dev, uio, ioflag);
  443         mtx_unlock(&Giant);
  444         dev_relthread(dev, ref);
  445         return (retval);
  446 }
  447 
  448 static int
  449 giant_write(struct cdev *dev, struct uio *uio, int ioflag)
  450 {
  451         struct cdevsw *dsw;
  452         int ref, retval;
  453 
  454         dsw = dev_refthread(dev, &ref);
  455         if (dsw == NULL)
  456                 return (ENXIO);
  457         mtx_lock(&Giant);
  458         retval = dsw->d_gianttrick->d_write(dev, uio, ioflag);
  459         mtx_unlock(&Giant);
  460         dev_relthread(dev, ref);
  461         return (retval);
  462 }
  463 
  464 static int
  465 giant_poll(struct cdev *dev, int events, struct thread *td)
  466 {
  467         struct cdevsw *dsw;
  468         int ref, retval;
  469 
  470         dsw = dev_refthread(dev, &ref);
  471         if (dsw == NULL)
  472                 return (ENXIO);
  473         mtx_lock(&Giant);
  474         retval = dsw->d_gianttrick->d_poll(dev, events, td);
  475         mtx_unlock(&Giant);
  476         dev_relthread(dev, ref);
  477         return (retval);
  478 }
  479 
  480 static int
  481 giant_kqfilter(struct cdev *dev, struct knote *kn)
  482 {
  483         struct cdevsw *dsw;
  484         int ref, retval;
  485 
  486         dsw = dev_refthread(dev, &ref);
  487         if (dsw == NULL)
  488                 return (ENXIO);
  489         mtx_lock(&Giant);
  490         retval = dsw->d_gianttrick->d_kqfilter(dev, kn);
  491         mtx_unlock(&Giant);
  492         dev_relthread(dev, ref);
  493         return (retval);
  494 }
  495 
  496 static int
  497 giant_mmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr, int nprot,
  498     vm_memattr_t *memattr)
  499 {
  500         struct cdevsw *dsw;
  501         int ref, retval;
  502 
  503         dsw = dev_refthread(dev, &ref);
  504         if (dsw == NULL)
  505                 return (ENXIO);
  506         mtx_lock(&Giant);
  507         retval = dsw->d_gianttrick->d_mmap(dev, offset, paddr, nprot,
  508             memattr);
  509         mtx_unlock(&Giant);
  510         dev_relthread(dev, ref);
  511         return (retval);
  512 }
  513 
  514 static int
  515 giant_mmap_single(struct cdev *dev, vm_ooffset_t *offset, vm_size_t size,
  516     vm_object_t *object, int nprot)
  517 {
  518         struct cdevsw *dsw;
  519         int ref, retval;
  520 
  521         dsw = dev_refthread(dev, &ref);
  522         if (dsw == NULL)
  523                 return (ENXIO);
  524         mtx_lock(&Giant);
  525         retval = dsw->d_gianttrick->d_mmap_single(dev, offset, size, object,
  526             nprot);
  527         mtx_unlock(&Giant);
  528         dev_relthread(dev, ref);
  529         return (retval);
  530 }
  531 
  532 static void
  533 notify(struct cdev *dev, const char *ev, int flags)
  534 {
  535         static const char prefix[] = "cdev=";
  536         char *data;
  537         int namelen, mflags;
  538 
  539         if (cold)
  540                 return;
  541         mflags = (flags & MAKEDEV_NOWAIT) ? M_NOWAIT : M_WAITOK;
  542         namelen = strlen(dev->si_name);
  543         data = malloc(namelen + sizeof(prefix), M_TEMP, mflags);
  544         if (data == NULL)
  545                 return;
  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);
  549         free(data, M_TEMP);
  550 }
  551 
  552 static void
  553 notify_create(struct cdev *dev, int flags)
  554 {
  555 
  556         notify(dev, "CREATE", flags);
  557 }
  558 
  559 static void
  560 notify_destroy(struct cdev *dev)
  561 {
  562 
  563         notify(dev, "DESTROY", MAKEDEV_WAITOK);
  564 }
  565 
  566 static struct cdev *
  567 newdev(struct make_dev_args *args, struct cdev *si)
  568 {
  569         struct cdev *si2;
  570         struct cdevsw *csw;
  571 
  572         mtx_assert(&devmtx, MA_OWNED);
  573         csw = args->mda_devsw;
  574         si2 = NULL;
  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);
  580                                 si = si2;
  581                                 break;
  582                         }
  583                 }
  584 
  585                 /*
  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
  591                  * to do so.
  592                  */
  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)));
  596         }
  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. */
  601         if (si2 == NULL) {
  602                 si->si_devsw = csw;
  603                 LIST_INSERT_HEAD(&csw->d_devs, si, si_list);
  604         } else {
  605                 KASSERT(si->si_devsw == csw,
  606                     ("%s: inconsistent devsw between clone_create() and make_dev()",
  607                     __func__));
  608         }
  609         return (si);
  610 }
  611 
  612 static void
  613 fini_cdevsw(struct cdevsw *devsw)
  614 {
  615         struct cdevsw *gt;
  616 
  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;
  622         }
  623         devsw->d_flags &= ~D_INIT;
  624 }
  625 
  626 static int
  627 prep_cdevsw(struct cdevsw *devsw, int flags)
  628 {
  629         struct cdevsw *dsw2;
  630 
  631         mtx_assert(&devmtx, MA_OWNED);
  632         if (devsw->d_flags & D_INIT)
  633                 return (0);
  634         if (devsw->d_flags & D_NEEDGIANT) {
  635                 dev_unlock();
  636                 dsw2 = malloc(sizeof *dsw2, M_DEVT,
  637                      (flags & MAKEDEV_NOWAIT) ? M_NOWAIT : M_WAITOK);
  638                 dev_lock();
  639                 if (dsw2 == NULL && !(devsw->d_flags & D_INIT))
  640                         return (ENOMEM);
  641         } else
  642                 dsw2 = NULL;
  643         if (devsw->d_flags & D_INIT) {
  644                 if (dsw2 != NULL)
  645                         cdevsw_free_devlocked(dsw2);
  646                 return (0);
  647         }
  648 
  649         if (devsw->d_version != D_VERSION_03) {
  650                 printf(
  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;
  665         }
  666         
  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;
  674                         dsw2 = NULL;
  675                 }
  676         }
  677 
  678 #define FIXUP(member, noop, giant)                              \
  679         do {                                                    \
  680                 if (devsw->member == NULL) {                    \
  681                         devsw->member = noop;                   \
  682                 } else if (devsw->d_flags & D_NEEDGIANT)        \
  683                         devsw->member = giant;                  \
  684                 }                                               \
  685         while (0)
  686 
  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);
  698 
  699         if (devsw->d_dump == NULL)      devsw->d_dump = no_dump;
  700 
  701         LIST_INIT(&devsw->d_devs);
  702 
  703         devsw->d_flags |= D_INIT;
  704 
  705         if (dsw2 != NULL)
  706                 cdevsw_free_devlocked(dsw2);
  707         return (0);
  708 }
  709 
  710 static int
  711 prep_devname(struct cdev *dev, const char *fmt, va_list ap)
  712 {
  713         int len;
  714         char *from, *q, *s, *to;
  715 
  716         mtx_assert(&devmtx, MA_OWNED);
  717 
  718         len = vsnrprintf(dev->si_name, sizeof(dev->si_name), 32, fmt, ap);
  719         if (len > sizeof(dev->si_name) - 1)
  720                 return (ENAMETOOLONG);
  721 
  722         /* Strip leading slashes. */
  723         for (from = dev->si_name; *from == '/'; from++)
  724                 ;
  725 
  726         for (to = dev->si_name; *from != '\0'; from++, to++) {
  727                 /*
  728                  * Spaces and double quotation marks cause
  729                  * problems for the devctl(4) protocol.
  730                  * Reject names containing those characters.
  731                  */
  732                 if (isspace(*from) || *from == '"')
  733                         return (EINVAL);
  734                 /* Treat multiple sequential slashes as single. */
  735                 while (from[0] == '/' && from[1] == '/')
  736                         from++;
  737                 /* Trailing slash is considered invalid. */
  738                 if (from[0] == '/' && from[1] == '\0')
  739                         return (EINVAL);
  740                 *to = *from;
  741         }
  742         *to = '\0';
  743 
  744         if (dev->si_name[0] == '\0')
  745                 return (EINVAL);
  746 
  747         /* Disallow "." and ".." components. */
  748         for (s = dev->si_name;;) {
  749                 for (q = s; *q != '/' && *q != '\0'; q++)
  750                         ;
  751                 if (q - s == 1 && s[0] == '.')
  752                         return (EINVAL);
  753                 if (q - s == 2 && s[0] == '.' && s[1] == '.')
  754                         return (EINVAL);
  755                 if (*q != '/')
  756                         break;
  757                 s = q + 1;
  758         }
  759 
  760         if (devfs_dev_exists(dev->si_name) != 0)
  761                 return (EEXIST);
  762 
  763         return (0);
  764 }
  765 
  766 void
  767 make_dev_args_init_impl(struct make_dev_args *args, size_t sz)
  768 {
  769 
  770         bzero(args, sz);
  771         args->mda_size = sz;
  772 }
  773 
  774 static int
  775 make_dev_sv(struct make_dev_args *args1, struct cdev **dres,
  776     const char *fmt, va_list ap)
  777 {
  778         struct cdev *dev, *dev_new;
  779         struct make_dev_args args;
  780         int res;
  781 
  782         bzero(&args, sizeof(args));
  783         if (sizeof(args) < args1->mda_size)
  784                 return (EINVAL);
  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);
  790         if (dev_new == NULL)
  791                 return (ENOMEM);
  792         dev_lock();
  793         res = prep_cdevsw(args.mda_devsw, args.mda_flags);
  794         if (res != 0) {
  795                 dev_unlock();
  796                 devfs_free(dev_new);
  797                 return (res);
  798         }
  799         dev = newdev(&args, dev_new);
  800         if ((dev->si_flags & SI_NAMED) == 0) {
  801                 res = prep_devname(dev, fmt, ap);
  802                 if (res != 0) {
  803                         if ((args.mda_flags & MAKEDEV_CHECKNAME) == 0) {
  804                                 panic(
  805                         "make_dev_sv: bad si_name (error=%d, si_name=%s)",
  806                                     res, dev->si_name);
  807                         }
  808                         if (dev == dev_new) {
  809                                 LIST_REMOVE(dev, si_list);
  810                                 dev_unlock();
  811                                 devfs_free(dev);
  812                         } else
  813                                 dev_unlock();
  814                         return (res);
  815                 }
  816         }
  817         if ((args.mda_flags & MAKEDEV_REF) != 0)
  818                 dev_refl(dev);
  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;
  830 
  831         devfs_create(dev);
  832         clean_unrhdrl(devfs_inos);
  833         dev_unlock_and_free();
  834 
  835         notify_create(dev, args.mda_flags);
  836 
  837         *dres = dev;
  838         return (0);
  839 }
  840 
  841 int
  842 make_dev_s(struct make_dev_args *args, struct cdev **dres,
  843     const char *fmt, ...)
  844 {
  845         va_list ap;
  846         int res;
  847 
  848         va_start(ap, fmt);
  849         res = make_dev_sv(args, dres, fmt, ap);
  850         va_end(ap);
  851         return (res);
  852 }
  853 
  854 static int
  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,
  857     va_list ap)
  858 {
  859         struct make_dev_args args;
  860 
  861         make_dev_args_init(&args);
  862         args.mda_flags = flags;
  863         args.mda_devsw = devsw;
  864         args.mda_cr = cr;
  865         args.mda_uid = uid;
  866         args.mda_gid = gid;
  867         args.mda_mode = mode;
  868         args.mda_unit = unit;
  869         return (make_dev_sv(&args, dres, fmt, ap));
  870 }
  871 
  872 struct cdev *
  873 make_dev(struct cdevsw *devsw, int unit, uid_t uid, gid_t gid, int mode,
  874     const char *fmt, ...)
  875 {
  876         struct cdev *dev;
  877         va_list ap;
  878         int res __unused;
  879 
  880         va_start(ap, fmt);
  881         res = make_dev_credv(0, &dev, devsw, unit, NULL, uid, gid, mode, fmt,
  882                       ap);
  883         va_end(ap);
  884         KASSERT(res == 0 && dev != NULL,
  885             ("make_dev: failed make_dev_credv (error=%d)", res));
  886         return (dev);
  887 }
  888 
  889 struct cdev *
  890 make_dev_cred(struct cdevsw *devsw, int unit, struct ucred *cr, uid_t uid,
  891     gid_t gid, int mode, const char *fmt, ...)
  892 {
  893         struct cdev *dev;
  894         va_list ap;
  895         int res __unused;
  896 
  897         va_start(ap, fmt);
  898         res = make_dev_credv(0, &dev, devsw, unit, cr, uid, gid, mode, fmt, ap);
  899         va_end(ap);
  900 
  901         KASSERT(res == 0 && dev != NULL,
  902             ("make_dev_cred: failed make_dev_credv (error=%d)", res));
  903         return (dev);
  904 }
  905 
  906 struct cdev *
  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, ...)
  909 {
  910         struct cdev *dev;
  911         va_list ap;
  912         int res;
  913 
  914         va_start(ap, fmt);
  915         res = make_dev_credv(flags, &dev, devsw, unit, cr, uid, gid, mode,
  916             fmt, ap);
  917         va_end(ap);
  918 
  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);
  923 }
  924 
  925 int
  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, ...)
  928 {
  929         va_list ap;
  930         int res;
  931 
  932         va_start(ap, fmt);
  933         res = make_dev_credv(flags, cdev, devsw, 0, cr, uid, gid, mode,
  934             fmt, ap);
  935         va_end(ap);
  936 
  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));
  940         return (res);
  941 }
  942 
  943 static void
  944 dev_dependsl(struct cdev *pdev, struct cdev *cdev)
  945 {
  946 
  947         cdev->si_parent = pdev;
  948         cdev->si_flags |= SI_CHILD;
  949         LIST_INSERT_HEAD(&pdev->si_children, cdev, si_siblings);
  950 }
  951 
  952 
  953 void
  954 dev_depends(struct cdev *pdev, struct cdev *cdev)
  955 {
  956 
  957         dev_lock();
  958         dev_dependsl(pdev, cdev);
  959         dev_unlock();
  960 }
  961 
  962 static int
  963 make_dev_alias_v(int flags, struct cdev **cdev, struct cdev *pdev,
  964     const char *fmt, va_list ap)
  965 {
  966         struct cdev *dev;
  967         int error;
  968 
  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));
  975 
  976         dev = devfs_alloc(flags);
  977         if (dev == NULL)
  978                 return (ENOMEM);
  979         dev_lock();
  980         dev->si_flags |= SI_ALIAS;
  981         error = prep_devname(dev, fmt, ap);
  982         if (error != 0) {
  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);
  986                 }
  987                 dev_unlock();
  988                 devfs_free(dev);
  989                 return (error);
  990         }
  991         dev->si_flags |= SI_NAMED;
  992         devfs_create(dev);
  993         dev_dependsl(pdev, dev);
  994         clean_unrhdrl(devfs_inos);
  995         dev_unlock();
  996 
  997         notify_create(dev, flags);
  998         *cdev = dev;
  999 
 1000         return (0);
 1001 }
 1002 
 1003 struct cdev *
 1004 make_dev_alias(struct cdev *pdev, const char *fmt, ...)
 1005 {
 1006         struct cdev *dev;
 1007         va_list ap;
 1008         int res __unused;
 1009 
 1010         va_start(ap, fmt);
 1011         res = make_dev_alias_v(MAKEDEV_WAITOK, &dev, pdev, fmt, ap);
 1012         va_end(ap);
 1013 
 1014         KASSERT(res == 0 && dev != NULL,
 1015             ("make_dev_alias: failed make_dev_alias_v (error=%d)", res));
 1016         return (dev);
 1017 }
 1018 
 1019 int
 1020 make_dev_alias_p(int flags, struct cdev **cdev, struct cdev *pdev,
 1021     const char *fmt, ...)
 1022 {
 1023         va_list ap;
 1024         int res;
 1025 
 1026         va_start(ap, fmt);
 1027         res = make_dev_alias_v(flags, cdev, pdev, fmt, ap);
 1028         va_end(ap);
 1029         return (res);
 1030 }
 1031 
 1032 int
 1033 make_dev_physpath_alias(int flags, struct cdev **cdev, struct cdev *pdev, 
 1034     struct cdev *old_alias, const char *physpath)
 1035 {
 1036         char *devfspath;
 1037         int physpath_len;
 1038         int max_parentpath_len;
 1039         int parentpath_len;
 1040         int devfspathbuf_len;
 1041         int mflags;
 1042         int ret;
 1043 
 1044         *cdev = NULL;
 1045         devfspath = NULL;
 1046         physpath_len = strlen(physpath);
 1047         ret = EINVAL;
 1048         if (physpath_len == 0)
 1049                 goto out;
 1050 
 1051         if (strncmp("id1,", physpath, 4) == 0) {
 1052                 physpath += 4;
 1053                 physpath_len -= 4;
 1054                 if (physpath_len == 0)
 1055                         goto out;
 1056         }
 1057 
 1058         max_parentpath_len = SPECNAMELEN - physpath_len - /*/*/1;
 1059         parentpath_len = strlen(pdev->si_name);
 1060         if (max_parentpath_len < parentpath_len) {
 1061                 if (bootverbose)
 1062                         printf("WARNING: Unable to alias %s "
 1063                             "to %s/%s - path too long\n",
 1064                             pdev->si_name, physpath, pdev->si_name);
 1065                 ret = ENAMETOOLONG;
 1066                 goto out;
 1067         }
 1068 
 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) {
 1073                 ret = ENOMEM;
 1074                 goto out;
 1075         }
 1076 
 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. */
 1080                 *cdev = old_alias;
 1081                 old_alias = NULL;
 1082                 ret = 0;
 1083         } else {
 1084                 ret = make_dev_alias_p(flags, cdev, pdev, "%s", devfspath);
 1085         }
 1086 out:
 1087         if (old_alias != NULL)  
 1088                 destroy_dev(old_alias);
 1089         if (devfspath != NULL)
 1090                 free(devfspath, M_DEVBUF);
 1091         return (ret);
 1092 }
 1093 
 1094 static void
 1095 destroy_devl(struct cdev *dev)
 1096 {
 1097         struct cdevsw *csw;
 1098         struct cdev_privdata *p;
 1099         struct cdev_priv *cdp;
 1100 
 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",
 1106              dev2unit(dev)));
 1107 
 1108         cdp = cdev2priv(dev);
 1109         if ((cdp->cdp_flags & CDP_UNREF_DTR) == 0) {
 1110                 /*
 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().
 1115                  */
 1116                 dev_refl(dev);
 1117 
 1118                 devfs_destroy(dev);
 1119         }
 1120 
 1121         /* Remove name marking */
 1122         dev->si_flags &= ~SI_NAMED;
 1123 
 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;
 1128         }
 1129 
 1130         /* Kill our children */
 1131         while (!LIST_EMPTY(&dev->si_children))
 1132                 destroy_devl(LIST_FIRST(&dev->si_children));
 1133 
 1134         /* Remove from clone list */
 1135         if (dev->si_flags & SI_CLONELIST) {
 1136                 LIST_REMOVE(dev, si_clone);
 1137                 dev->si_flags &= ~SI_CLONELIST;
 1138         }
 1139 
 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) {
 1144                 csw->d_purge(dev);
 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));
 1151         }
 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);
 1157         }
 1158 
 1159         mtx_unlock(&cdp->cdp_threadlock);
 1160         dev_unlock();
 1161         if ((cdp->cdp_flags & CDP_UNREF_DTR) == 0) {
 1162                 /* avoid out of order notify events */
 1163                 notify_destroy(dev);
 1164         }
 1165         mtx_lock(&cdevpriv_mtx);
 1166         while ((p = LIST_FIRST(&cdp->cdp_fdpriv)) != NULL) {
 1167                 devfs_destroy_cdevpriv(p);
 1168                 mtx_lock(&cdevpriv_mtx);
 1169         }
 1170         mtx_unlock(&cdevpriv_mtx);
 1171         dev_lock();
 1172 
 1173         dev->si_drv1 = 0;
 1174         dev->si_drv2 = 0;
 1175         bzero(&dev->__si_u, sizeof(dev->__si_u));
 1176 
 1177         if (!(dev->si_flags & SI_ALIAS)) {
 1178                 /* Remove from cdevsw list */
 1179                 LIST_REMOVE(dev, si_list);
 1180 
 1181                 /* If cdevsw has no more struct cdev *'s, clean it */
 1182                 if (LIST_EMPTY(&csw->d_devs)) {
 1183                         fini_cdevsw(csw);
 1184                         wakeup(&csw->d_devs);
 1185                 }
 1186         }
 1187         dev->si_flags &= ~SI_ALIAS;
 1188         cdp->cdp_flags &= ~CDP_UNREF_DTR;
 1189         dev->si_refcount--;
 1190 
 1191         if (dev->si_refcount > 0)
 1192                 LIST_INSERT_HEAD(&dead_cdevsw.d_devs, dev, si_list);
 1193         else
 1194                 dev_free_devlocked(dev);
 1195 }
 1196 
 1197 static void
 1198 delist_dev_locked(struct cdev *dev)
 1199 {
 1200         struct cdev_priv *cdp;
 1201         struct cdev *child;
 1202 
 1203         mtx_assert(&devmtx, MA_OWNED);
 1204         cdp = cdev2priv(dev);
 1205         if ((cdp->cdp_flags & CDP_UNREF_DTR) != 0)
 1206                 return;
 1207         cdp->cdp_flags |= CDP_UNREF_DTR;
 1208         dev_refl(dev);
 1209         devfs_destroy(dev);
 1210         LIST_FOREACH(child, &dev->si_children, si_siblings)
 1211                 delist_dev_locked(child);
 1212         dev_unlock();   
 1213         /* ensure the destroy event is queued in order */
 1214         notify_destroy(dev);
 1215         dev_lock();
 1216 }
 1217 
 1218 /*
 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.
 1225  */
 1226 void
 1227 delist_dev(struct cdev *dev)
 1228 {
 1229 
 1230         WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, "delist_dev");
 1231         dev_lock();
 1232         delist_dev_locked(dev);
 1233         dev_unlock();
 1234 }
 1235 
 1236 void
 1237 destroy_dev(struct cdev *dev)
 1238 {
 1239 
 1240         WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, "destroy_dev");
 1241         dev_lock();
 1242         destroy_devl(dev);
 1243         dev_unlock_and_free();
 1244 }
 1245 
 1246 const char *
 1247 devtoname(struct cdev *dev)
 1248 {
 1249 
 1250         return (dev->si_name);
 1251 }
 1252 
 1253 int
 1254 dev_stdclone(char *name, char **namep, const char *stem, int *unit)
 1255 {
 1256         int u, i;
 1257 
 1258         i = strlen(stem);
 1259         if (strncmp(stem, name, i) != 0)
 1260                 return (0);
 1261         if (!isdigit(name[i]))
 1262                 return (0);
 1263         u = 0;
 1264         if (name[i] == '' && isdigit(name[i+1]))
 1265                 return (0);
 1266         while (isdigit(name[i])) {
 1267                 u *= 10;
 1268                 u += name[i++] - '';
 1269         }
 1270         if (u > 0xffffff)
 1271                 return (0);
 1272         *unit = u;
 1273         if (namep)
 1274                 *namep = &name[i];
 1275         if (name[i]) 
 1276                 return (2);
 1277         return (1);
 1278 }
 1279 
 1280 /*
 1281  * Helper functions for cloning device drivers.
 1282  *
 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.
 1287  *
 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.
 1291  *
 1292  * In addition these routines support paired devices (pty, nmdm and similar)
 1293  * by respecting a number of "flag" bits in the minor number.
 1294  *
 1295  */
 1296 
 1297 struct clonedevs {
 1298         LIST_HEAD(,cdev)        head;
 1299 };
 1300 
 1301 void
 1302 clone_setup(struct clonedevs **cdp)
 1303 {
 1304 
 1305         *cdp = malloc(sizeof **cdp, M_DEVBUF, M_WAITOK | M_ZERO);
 1306         LIST_INIT(&(*cdp)->head);
 1307 }
 1308 
 1309 int
 1310 clone_create(struct clonedevs **cdp, struct cdevsw *csw, int *up,
 1311     struct cdev **dp, int extra)
 1312 {
 1313         struct clonedevs *cd;
 1314         struct cdev *dev, *ndev, *dl, *de;
 1315         struct make_dev_args args;
 1316         int unit, low, u;
 1317 
 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"));
 1326 
 1327 
 1328         /*
 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.
 1335          */
 1336         unit = *up;
 1337         ndev = devfs_alloc(MAKEDEV_WAITOK);
 1338         dev_lock();
 1339         prep_cdevsw(csw, MAKEDEV_WAITOK);
 1340         low = extra;
 1341         de = dl = NULL;
 1342         cd = *cdp;
 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));
 1346                 u = dev2unit(dev);
 1347                 if (u == (unit | extra)) {
 1348                         *dp = dev;
 1349                         dev_unlock();
 1350                         devfs_free(ndev);
 1351                         return (0);
 1352                 }
 1353                 if (unit == -1 && u == low) {
 1354                         low++;
 1355                         de = dev;
 1356                         continue;
 1357                 } else if (u < (unit | extra)) {
 1358                         de = dev;
 1359                         continue;
 1360                 } else if (u > (unit | extra)) {
 1361                         dl = dev;
 1362                         break;
 1363                 }
 1364         }
 1365         if (unit == -1)
 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);
 1376                 }
 1377                 panic("foo");
 1378         }
 1379         KASSERT(!(dev->si_flags & SI_CLONELIST),
 1380             ("Dev %p(%s) should not be on clonelist", dev, dev->si_name));
 1381         if (dl != NULL)
 1382                 LIST_INSERT_BEFORE(dl, dev, si_clone);
 1383         else if (de != NULL)
 1384                 LIST_INSERT_AFTER(de, dev, si_clone);
 1385         else
 1386                 LIST_INSERT_HEAD(&cd->head, dev, si_clone);
 1387         dev->si_flags |= SI_CLONELIST;
 1388         *up = unit;
 1389         dev_unlock_and_free();
 1390         return (1);
 1391 }
 1392 
 1393 /*
 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.
 1396  */
 1397 void
 1398 clone_cleanup(struct clonedevs **cdp)
 1399 {
 1400         struct cdev *dev;
 1401         struct cdev_priv *cp;
 1402         struct clonedevs *cd;
 1403         
 1404         cd = *cdp;
 1405         if (cd == NULL)
 1406                 return;
 1407         dev_lock();
 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)));
 1420                         destroy_devl(dev);
 1421                 }
 1422         }
 1423         dev_unlock_and_free();
 1424         free(cd, M_DEVBUF);
 1425         *cdp = NULL;
 1426 }
 1427 
 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);
 1431 
 1432 static void
 1433 destroy_dev_tq(void *ctx, int pending)
 1434 {
 1435         struct cdev_priv *cp;
 1436         struct cdev *dev;
 1437         void (*cb)(void *);
 1438         void *cb_arg;
 1439 
 1440         dev_lock();
 1441         while (!TAILQ_EMPTY(&dev_ddtr)) {
 1442                 cp = TAILQ_FIRST(&dev_ddtr);
 1443                 dev = &cp->cdp_c;
 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;
 1449                 destroy_devl(dev);
 1450                 dev_unlock_and_free();
 1451                 dev_rel(dev);
 1452                 if (cb != NULL)
 1453                         cb(cb_arg);
 1454                 dev_lock();
 1455         }
 1456         dev_unlock();
 1457 }
 1458 
 1459 /*
 1460  * devmtx shall be locked on entry. devmtx will be unlocked after
 1461  * function return.
 1462  */
 1463 static int
 1464 destroy_dev_sched_cbl(struct cdev *dev, void (*cb)(void *), void *arg)
 1465 {
 1466         struct cdev_priv *cp;
 1467 
 1468         mtx_assert(&devmtx, MA_OWNED);
 1469         cp = cdev2priv(dev);
 1470         if (cp->cdp_flags & CDP_SCHED_DTR) {
 1471                 dev_unlock();
 1472                 return (0);
 1473         }
 1474         dev_refl(dev);
 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);
 1479         dev_unlock();
 1480         taskqueue_enqueue(taskqueue_swi_giant, &dev_dtr_task);
 1481         return (1);
 1482 }
 1483 
 1484 int
 1485 destroy_dev_sched_cb(struct cdev *dev, void (*cb)(void *), void *arg)
 1486 {
 1487 
 1488         dev_lock();
 1489         return (destroy_dev_sched_cbl(dev, cb, arg));
 1490 }
 1491 
 1492 int
 1493 destroy_dev_sched(struct cdev *dev)
 1494 {
 1495 
 1496         return (destroy_dev_sched_cb(dev, NULL, NULL));
 1497 }
 1498 
 1499 void
 1500 destroy_dev_drain(struct cdevsw *csw)
 1501 {
 1502 
 1503         dev_lock();
 1504         while (!LIST_EMPTY(&csw->d_devs)) {
 1505                 msleep(&csw->d_devs, &devmtx, PRIBIO, "devscd", hz/10);
 1506         }
 1507         dev_unlock();
 1508 }
 1509 
 1510 void
 1511 drain_dev_clone_events(void)
 1512 {
 1513 
 1514         sx_xlock(&clone_drain_lock);
 1515         sx_xunlock(&clone_drain_lock);
 1516 }
 1517 
 1518 #include "opt_ddb.h"
 1519 #ifdef DDB
 1520 #include <sys/kernel.h>
 1521 
 1522 #include <ddb/ddb.h>
 1523 
 1524 DB_SHOW_COMMAND(cdev, db_show_cdev)
 1525 {
 1526         struct cdev_priv *cdp;
 1527         struct cdev *dev;
 1528         u_int flags;
 1529         char buf[512];
 1530 
 1531         if (!have_addr) {
 1532                 TAILQ_FOREACH(cdp, &cdevp_list, cdp_list) {
 1533                         dev = &cdp->cdp_c;
 1534                         db_printf("%s %p\n", dev->si_name, dev);
 1535                         if (db_pager_quit)
 1536                                 break;
 1537                 }
 1538                 return;
 1539         }
 1540 
 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));                 \
 1554                 flags &= ~(flag);                                       \
 1555         }                                                               \
 1556 } while (0)
 1557         buf[0] = '\0';
 1558         SI_FLAG(SI_ETERNAL);
 1559         SI_FLAG(SI_ALIAS);
 1560         SI_FLAG(SI_NAMED);
 1561         SI_FLAG(SI_CHILD);
 1562         SI_FLAG(SI_DUMPDEV);
 1563         SI_FLAG(SI_CLONELIST);
 1564         db_printf("si_flags %s\n", buf);
 1565 
 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));                 \
 1572                 flags &= ~(flag);                                       \
 1573         }                                                               \
 1574 } while (0)
 1575         buf[0] = '\0';
 1576         CDP_FLAG(CDP_ACTIVE);
 1577         CDP_FLAG(CDP_SCHED_DTR);
 1578         db_printf("cdp_flags %s\n", buf);
 1579 }
 1580 #endif

Cache object: 3a2c88ede7caa7fec95b8ae94c18c034


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.