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-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 1999-2002 Poul-Henning Kamp
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  *
   14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   24  * SUCH DAMAGE.
   25  */
   26 
   27 #include <sys/cdefs.h>
   28 __FBSDID("$FreeBSD: releng/8.3/sys/kern/kern_conf.c 222274 2011-05-25 03:25:14Z kib $");
   29 
   30 #include <sys/param.h>
   31 #include <sys/kernel.h>
   32 #include <sys/systm.h>
   33 #include <sys/bus.h>
   34 #include <sys/bio.h>
   35 #include <sys/lock.h>
   36 #include <sys/mutex.h>
   37 #include <sys/module.h>
   38 #include <sys/malloc.h>
   39 #include <sys/conf.h>
   40 #include <sys/vnode.h>
   41 #include <sys/queue.h>
   42 #include <sys/poll.h>
   43 #include <sys/sx.h>
   44 #include <sys/ctype.h>
   45 #include <sys/ucred.h>
   46 #include <sys/taskqueue.h>
   47 #include <machine/stdarg.h>
   48 
   49 #include <fs/devfs/devfs_int.h>
   50 #include <vm/vm.h>
   51 
   52 static MALLOC_DEFINE(M_DEVT, "cdev", "cdev storage");
   53 
   54 struct mtx devmtx;
   55 static void destroy_devl(struct cdev *dev);
   56 static int destroy_dev_sched_cbl(struct cdev *dev,
   57     void (*cb)(void *), void *arg);
   58 static int make_dev_credv(int flags, struct cdev **dres, struct cdevsw *devsw,
   59     int unit, struct ucred *cr, uid_t uid, gid_t gid, int mode, const char *fmt,
   60     va_list ap);
   61 
   62 static struct cdev_priv_list cdevp_free_list =
   63     TAILQ_HEAD_INITIALIZER(cdevp_free_list);
   64 static SLIST_HEAD(free_cdevsw, cdevsw) cdevsw_gt_post_list =
   65     SLIST_HEAD_INITIALIZER(cdevsw_gt_post_list);
   66 
   67 void
   68 dev_lock(void)
   69 {
   70 
   71         mtx_lock(&devmtx);
   72 }
   73 
   74 /*
   75  * Free all the memory collected while the cdev mutex was
   76  * locked. Since devmtx is after the system map mutex, free() cannot
   77  * be called immediately and is postponed until cdev mutex can be
   78  * dropped.
   79  */
   80 static void
   81 dev_unlock_and_free(void)
   82 {
   83         struct cdev_priv_list cdp_free;
   84         struct free_cdevsw csw_free;
   85         struct cdev_priv *cdp;
   86         struct cdevsw *csw;
   87 
   88         mtx_assert(&devmtx, MA_OWNED);
   89 
   90         /*
   91          * Make the local copy of the list heads while the dev_mtx is
   92          * held. Free it later.
   93          */
   94         TAILQ_INIT(&cdp_free);
   95         TAILQ_CONCAT(&cdp_free, &cdevp_free_list, cdp_list);
   96         csw_free = cdevsw_gt_post_list;
   97         SLIST_INIT(&cdevsw_gt_post_list);
   98 
   99         mtx_unlock(&devmtx);
  100 
  101         while ((cdp = TAILQ_FIRST(&cdp_free)) != NULL) {
  102                 TAILQ_REMOVE(&cdp_free, cdp, cdp_list);
  103                 devfs_free(&cdp->cdp_c);
  104         }
  105         while ((csw = SLIST_FIRST(&csw_free)) != NULL) {
  106                 SLIST_REMOVE_HEAD(&csw_free, d_postfree_list);
  107                 free(csw, M_DEVT);
  108         }
  109 }
  110 
  111 static void
  112 dev_free_devlocked(struct cdev *cdev)
  113 {
  114         struct cdev_priv *cdp;
  115 
  116         mtx_assert(&devmtx, MA_OWNED);
  117         cdp = cdev2priv(cdev);
  118         TAILQ_INSERT_HEAD(&cdevp_free_list, cdp, cdp_list);
  119 }
  120 
  121 static void
  122 cdevsw_free_devlocked(struct cdevsw *csw)
  123 {
  124 
  125         mtx_assert(&devmtx, MA_OWNED);
  126         SLIST_INSERT_HEAD(&cdevsw_gt_post_list, csw, d_postfree_list);
  127 }
  128 
  129 void
  130 dev_unlock(void)
  131 {
  132 
  133         mtx_unlock(&devmtx);
  134 }
  135 
  136 void
  137 dev_ref(struct cdev *dev)
  138 {
  139 
  140         mtx_assert(&devmtx, MA_NOTOWNED);
  141         mtx_lock(&devmtx);
  142         dev->si_refcount++;
  143         mtx_unlock(&devmtx);
  144 }
  145 
  146 void
  147 dev_refl(struct cdev *dev)
  148 {
  149 
  150         mtx_assert(&devmtx, MA_OWNED);
  151         dev->si_refcount++;
  152 }
  153 
  154 void
  155 dev_rel(struct cdev *dev)
  156 {
  157         int flag = 0;
  158 
  159         mtx_assert(&devmtx, MA_NOTOWNED);
  160         dev_lock();
  161         dev->si_refcount--;
  162         KASSERT(dev->si_refcount >= 0,
  163             ("dev_rel(%s) gave negative count", devtoname(dev)));
  164 #if 0
  165         if (dev->si_usecount == 0 &&
  166             (dev->si_flags & SI_CHEAPCLONE) && (dev->si_flags & SI_NAMED))
  167                 ;
  168         else 
  169 #endif
  170         if (dev->si_devsw == NULL && dev->si_refcount == 0) {
  171                 LIST_REMOVE(dev, si_list);
  172                 flag = 1;
  173         }
  174         dev_unlock();
  175         if (flag)
  176                 devfs_free(dev);
  177 }
  178 
  179 struct cdevsw *
  180 dev_refthread(struct cdev *dev, int *ref)
  181 {
  182         struct cdevsw *csw;
  183         struct cdev_priv *cdp;
  184 
  185         mtx_assert(&devmtx, MA_NOTOWNED);
  186         if ((dev->si_flags & SI_ETERNAL) != 0) {
  187                 *ref = 0;
  188                 return (dev->si_devsw);
  189         }
  190         dev_lock();
  191         csw = dev->si_devsw;
  192         if (csw != NULL) {
  193                 cdp = cdev2priv(dev);
  194                 if ((cdp->cdp_flags & CDP_SCHED_DTR) == 0)
  195                         dev->si_threadcount++;
  196                 else
  197                         csw = NULL;
  198         }
  199         dev_unlock();
  200         *ref = 1;
  201         return (csw);
  202 }
  203 
  204 struct cdevsw *
  205 devvn_refthread(struct vnode *vp, struct cdev **devp, int *ref)
  206 {
  207         struct cdevsw *csw;
  208         struct cdev_priv *cdp;
  209         struct cdev *dev;
  210 
  211         mtx_assert(&devmtx, MA_NOTOWNED);
  212         if ((vp->v_vflag & VV_ETERNALDEV) != 0) {
  213                 dev = vp->v_rdev;
  214                 if (dev == NULL)
  215                         return (NULL);
  216                 KASSERT((dev->si_flags & SI_ETERNAL) != 0,
  217                     ("Not eternal cdev"));
  218                 *ref = 0;
  219                 csw = dev->si_devsw;
  220                 KASSERT(csw != NULL, ("Eternal cdev is destroyed"));
  221                 *devp = dev;
  222                 return (csw);
  223         }
  224 
  225         csw = NULL;
  226         dev_lock();
  227         dev = vp->v_rdev;
  228         if (dev == NULL) {
  229                 dev_unlock();
  230                 return (NULL);
  231         }
  232         cdp = cdev2priv(dev);
  233         if ((cdp->cdp_flags & CDP_SCHED_DTR) == 0) {
  234                 csw = dev->si_devsw;
  235                 if (csw != NULL)
  236                         dev->si_threadcount++;
  237         }
  238         dev_unlock();
  239         if (csw != NULL) {
  240                 *devp = dev;
  241                 *ref = 1;
  242         }
  243         return (csw);
  244 }
  245 
  246 void    
  247 dev_relthread(struct cdev *dev, int ref)
  248 {
  249 
  250         mtx_assert(&devmtx, MA_NOTOWNED);
  251         if (!ref)
  252                 return;
  253         dev_lock();
  254         KASSERT(dev->si_threadcount > 0,
  255             ("%s threadcount is wrong", dev->si_name));
  256         dev->si_threadcount--;
  257         dev_unlock();
  258 }
  259 
  260 int
  261 nullop(void)
  262 {
  263 
  264         return (0);
  265 }
  266 
  267 int
  268 eopnotsupp(void)
  269 {
  270 
  271         return (EOPNOTSUPP);
  272 }
  273 
  274 static int
  275 enxio(void)
  276 {
  277         return (ENXIO);
  278 }
  279 
  280 static int
  281 enodev(void)
  282 {
  283         return (ENODEV);
  284 }
  285 
  286 /* Define a dead_cdevsw for use when devices leave unexpectedly. */
  287 
  288 #define dead_open       (d_open_t *)enxio
  289 #define dead_close      (d_close_t *)enxio
  290 #define dead_read       (d_read_t *)enxio
  291 #define dead_write      (d_write_t *)enxio
  292 #define dead_ioctl      (d_ioctl_t *)enxio
  293 #define dead_poll       (d_poll_t *)enodev
  294 #define dead_mmap       (d_mmap_t *)enodev
  295 
  296 static void
  297 dead_strategy(struct bio *bp)
  298 {
  299 
  300         biofinish(bp, NULL, ENXIO);
  301 }
  302 
  303 #define dead_dump       (dumper_t *)enxio
  304 #define dead_kqfilter   (d_kqfilter_t *)enxio
  305 #define dead_mmap_single (d_mmap_single_t *)enodev
  306 
  307 static struct cdevsw dead_cdevsw = {
  308         .d_version =    D_VERSION,
  309         .d_flags =      D_NEEDGIANT, /* XXX: does dead_strategy need this ? */
  310         .d_open =       dead_open,
  311         .d_close =      dead_close,
  312         .d_read =       dead_read,
  313         .d_write =      dead_write,
  314         .d_ioctl =      dead_ioctl,
  315         .d_poll =       dead_poll,
  316         .d_mmap =       dead_mmap,
  317         .d_strategy =   dead_strategy,
  318         .d_name =       "dead",
  319         .d_dump =       dead_dump,
  320         .d_kqfilter =   dead_kqfilter,
  321         .d_mmap_single = dead_mmap_single
  322 };
  323 
  324 /* Default methods if driver does not specify method */
  325 
  326 #define null_open       (d_open_t *)nullop
  327 #define null_close      (d_close_t *)nullop
  328 #define no_read         (d_read_t *)enodev
  329 #define no_write        (d_write_t *)enodev
  330 #define no_ioctl        (d_ioctl_t *)enodev
  331 #define no_mmap         (d_mmap2_t *)enodev
  332 #define no_kqfilter     (d_kqfilter_t *)enodev
  333 #define no_mmap_single  (d_mmap_single_t *)enodev
  334 
  335 static void
  336 no_strategy(struct bio *bp)
  337 {
  338 
  339         biofinish(bp, NULL, ENODEV);
  340 }
  341 
  342 static int
  343 no_poll(struct cdev *dev __unused, int events, struct thread *td __unused)
  344 {
  345 
  346         return (poll_no_poll(events));
  347 }
  348 
  349 #define no_dump         (dumper_t *)enodev
  350 
  351 static int
  352 giant_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
  353 {
  354         struct cdevsw *dsw;
  355         int ref, retval;
  356 
  357         dsw = dev_refthread(dev, &ref);
  358         if (dsw == NULL)
  359                 return (ENXIO);
  360         mtx_lock(&Giant);
  361         retval = dsw->d_gianttrick->d_open(dev, oflags, devtype, td);
  362         mtx_unlock(&Giant);
  363         dev_relthread(dev, ref);
  364         return (retval);
  365 }
  366 
  367 static int
  368 giant_fdopen(struct cdev *dev, int oflags, struct thread *td, struct file *fp)
  369 {
  370         struct cdevsw *dsw;
  371         int ref, retval;
  372 
  373         dsw = dev_refthread(dev, &ref);
  374         if (dsw == NULL)
  375                 return (ENXIO);
  376         mtx_lock(&Giant);
  377         retval = dsw->d_gianttrick->d_fdopen(dev, oflags, td, fp);
  378         mtx_unlock(&Giant);
  379         dev_relthread(dev, ref);
  380         return (retval);
  381 }
  382 
  383 static int
  384 giant_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
  385 {
  386         struct cdevsw *dsw;
  387         int ref, retval;
  388 
  389         dsw = dev_refthread(dev, &ref);
  390         if (dsw == NULL)
  391                 return (ENXIO);
  392         mtx_lock(&Giant);
  393         retval = dsw->d_gianttrick->d_close(dev, fflag, devtype, td);
  394         mtx_unlock(&Giant);
  395         dev_relthread(dev, ref);
  396         return (retval);
  397 }
  398 
  399 static void
  400 giant_strategy(struct bio *bp)
  401 {
  402         struct cdevsw *dsw;
  403         struct cdev *dev;
  404         int ref;
  405 
  406         dev = bp->bio_dev;
  407         dsw = dev_refthread(dev, &ref);
  408         if (dsw == NULL) {
  409                 biofinish(bp, NULL, ENXIO);
  410                 return;
  411         }
  412         mtx_lock(&Giant);
  413         dsw->d_gianttrick->d_strategy(bp);
  414         mtx_unlock(&Giant);
  415         dev_relthread(dev, ref);
  416 }
  417 
  418 static int
  419 giant_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
  420 {
  421         struct cdevsw *dsw;
  422         int ref, retval;
  423 
  424         dsw = dev_refthread(dev, &ref);
  425         if (dsw == NULL)
  426                 return (ENXIO);
  427         mtx_lock(&Giant);
  428         retval = dsw->d_gianttrick->d_ioctl(dev, cmd, data, fflag, td);
  429         mtx_unlock(&Giant);
  430         dev_relthread(dev, ref);
  431         return (retval);
  432 }
  433   
  434 static int
  435 giant_read(struct cdev *dev, struct uio *uio, int ioflag)
  436 {
  437         struct cdevsw *dsw;
  438         int ref, retval;
  439 
  440         dsw = dev_refthread(dev, &ref);
  441         if (dsw == NULL)
  442                 return (ENXIO);
  443         mtx_lock(&Giant);
  444         retval = dsw->d_gianttrick->d_read(dev, uio, ioflag);
  445         mtx_unlock(&Giant);
  446         dev_relthread(dev, ref);
  447         return (retval);
  448 }
  449 
  450 static int
  451 giant_write(struct cdev *dev, struct uio *uio, int ioflag)
  452 {
  453         struct cdevsw *dsw;
  454         int ref, retval;
  455 
  456         dsw = dev_refthread(dev, &ref);
  457         if (dsw == NULL)
  458                 return (ENXIO);
  459         mtx_lock(&Giant);
  460         retval = dsw->d_gianttrick->d_write(dev, uio, ioflag);
  461         mtx_unlock(&Giant);
  462         dev_relthread(dev, ref);
  463         return (retval);
  464 }
  465 
  466 static int
  467 giant_poll(struct cdev *dev, int events, struct thread *td)
  468 {
  469         struct cdevsw *dsw;
  470         int ref, retval;
  471 
  472         dsw = dev_refthread(dev, &ref);
  473         if (dsw == NULL)
  474                 return (ENXIO);
  475         mtx_lock(&Giant);
  476         retval = dsw->d_gianttrick->d_poll(dev, events, td);
  477         mtx_unlock(&Giant);
  478         dev_relthread(dev, ref);
  479         return (retval);
  480 }
  481 
  482 static int
  483 giant_kqfilter(struct cdev *dev, struct knote *kn)
  484 {
  485         struct cdevsw *dsw;
  486         int ref, retval;
  487 
  488         dsw = dev_refthread(dev, &ref);
  489         if (dsw == NULL)
  490                 return (ENXIO);
  491         mtx_lock(&Giant);
  492         retval = dsw->d_gianttrick->d_kqfilter(dev, kn);
  493         mtx_unlock(&Giant);
  494         dev_relthread(dev, ref);
  495         return (retval);
  496 }
  497 
  498 static int
  499 giant_mmap(struct cdev *dev, vm_offset_t offset, vm_paddr_t *paddr, int nprot,
  500     vm_memattr_t *memattr)
  501 {
  502         struct cdevsw *dsw;
  503         int ref, retval;
  504 
  505         dsw = dev_refthread(dev, &ref);
  506         if (dsw == NULL)
  507                 return (ENXIO);
  508         mtx_lock(&Giant);
  509         if (dsw->d_gianttrick->d_flags & D_MMAP2)
  510                 retval = dsw->d_gianttrick->d_mmap2(dev, offset, paddr, nprot,
  511                     memattr);
  512         else
  513                 retval = dsw->d_gianttrick->d_mmap(dev, offset, paddr, nprot);
  514         mtx_unlock(&Giant);
  515         dev_relthread(dev, ref);
  516         return (retval);
  517 }
  518 
  519 static int
  520 giant_mmap_single(struct cdev *dev, vm_ooffset_t *offset, vm_size_t size,
  521     vm_object_t *object, int nprot)
  522 {
  523         struct cdevsw *dsw;
  524         int ref, retval;
  525 
  526         dsw = dev_refthread(dev, &ref);
  527         if (dsw == NULL)
  528                 return (ENXIO);
  529         mtx_lock(&Giant);
  530         retval = dsw->d_gianttrick->d_mmap_single(dev, offset, size, object,
  531             nprot);
  532         mtx_unlock(&Giant);
  533         dev_relthread(dev, ref);
  534         return (retval);
  535 }
  536 
  537 static void
  538 notify(struct cdev *dev, const char *ev, int flags)
  539 {
  540         static const char prefix[] = "cdev=";
  541         char *data;
  542         int namelen, mflags;
  543 
  544         if (cold)
  545                 return;
  546         mflags = (flags & MAKEDEV_NOWAIT) ? M_NOWAIT : M_WAITOK;
  547         namelen = strlen(dev->si_name);
  548         data = malloc(namelen + sizeof(prefix), M_TEMP, mflags);
  549         if (data == NULL)
  550                 return;
  551         memcpy(data, prefix, sizeof(prefix) - 1);
  552         memcpy(data + sizeof(prefix) - 1, dev->si_name, namelen + 1);
  553         devctl_notify_f("DEVFS", "CDEV", ev, data, mflags);
  554         free(data, M_TEMP);
  555 }
  556 
  557 static void
  558 notify_create(struct cdev *dev, int flags)
  559 {
  560 
  561         notify(dev, "CREATE", flags);
  562 }
  563 
  564 static void
  565 notify_destroy(struct cdev *dev)
  566 {
  567 
  568         notify(dev, "DESTROY", MAKEDEV_WAITOK);
  569 }
  570 
  571 static struct cdev *
  572 newdev(struct cdevsw *csw, int unit, struct cdev *si)
  573 {
  574         struct cdev *si2;
  575 
  576         mtx_assert(&devmtx, MA_OWNED);
  577         if (csw->d_flags & D_NEEDMINOR) {
  578                 /* We may want to return an existing device */
  579                 LIST_FOREACH(si2, &csw->d_devs, si_list) {
  580                         if (dev2unit(si2) == unit) {
  581                                 dev_free_devlocked(si);
  582                                 return (si2);
  583                         }
  584                 }
  585         }
  586         si->si_drv0 = unit;
  587         si->si_devsw = csw;
  588         LIST_INSERT_HEAD(&csw->d_devs, si, si_list);
  589         return (si);
  590 }
  591 
  592 static void
  593 fini_cdevsw(struct cdevsw *devsw)
  594 {
  595         struct cdevsw *gt;
  596 
  597         if (devsw->d_gianttrick != NULL) {
  598                 gt = devsw->d_gianttrick;
  599                 memcpy(devsw, gt, sizeof *devsw);
  600                 cdevsw_free_devlocked(gt);
  601                 devsw->d_gianttrick = NULL;
  602         }
  603         devsw->d_flags &= ~D_INIT;
  604 }
  605 
  606 static int
  607 prep_cdevsw(struct cdevsw *devsw, int flags)
  608 {
  609         struct cdevsw *dsw2;
  610 
  611         mtx_assert(&devmtx, MA_OWNED);
  612         if (devsw->d_flags & D_INIT)
  613                 return (0);
  614         if (devsw->d_flags & D_NEEDGIANT) {
  615                 dev_unlock();
  616                 dsw2 = malloc(sizeof *dsw2, M_DEVT,
  617                      (flags & MAKEDEV_NOWAIT) ? M_NOWAIT : M_WAITOK);
  618                 dev_lock();
  619                 if (dsw2 == NULL && !(devsw->d_flags & D_INIT))
  620                         return (ENOMEM);
  621         } else
  622                 dsw2 = NULL;
  623         if (devsw->d_flags & D_INIT) {
  624                 if (dsw2 != NULL)
  625                         cdevsw_free_devlocked(dsw2);
  626                 return (0);
  627         }
  628 
  629         if (devsw->d_version != D_VERSION_01 &&
  630             devsw->d_version != D_VERSION_02) {
  631                 printf(
  632                     "WARNING: Device driver \"%s\" has wrong version %s\n",
  633                     devsw->d_name == NULL ? "???" : devsw->d_name,
  634                     "and is disabled.  Recompile KLD module.");
  635                 devsw->d_open = dead_open;
  636                 devsw->d_close = dead_close;
  637                 devsw->d_read = dead_read;
  638                 devsw->d_write = dead_write;
  639                 devsw->d_ioctl = dead_ioctl;
  640                 devsw->d_poll = dead_poll;
  641                 devsw->d_mmap = dead_mmap;
  642                 devsw->d_strategy = dead_strategy;
  643                 devsw->d_dump = dead_dump;
  644                 devsw->d_kqfilter = dead_kqfilter;
  645         }
  646         if (devsw->d_version == D_VERSION_01)
  647                 devsw->d_mmap_single = NULL;
  648         
  649         if (devsw->d_flags & D_NEEDGIANT) {
  650                 if (devsw->d_gianttrick == NULL) {
  651                         memcpy(dsw2, devsw, sizeof *dsw2);
  652                         devsw->d_gianttrick = dsw2;
  653                         devsw->d_flags |= D_MMAP2;
  654                         dsw2 = NULL;
  655                 }
  656         }
  657 
  658 #define FIXUP(member, noop, giant)                              \
  659         do {                                                    \
  660                 if (devsw->member == NULL) {                    \
  661                         devsw->member = noop;                   \
  662                 } else if (devsw->d_flags & D_NEEDGIANT)        \
  663                         devsw->member = giant;                  \
  664                 }                                               \
  665         while (0)
  666 
  667         FIXUP(d_open,           null_open,      giant_open);
  668         FIXUP(d_fdopen,         NULL,           giant_fdopen);
  669         FIXUP(d_close,          null_close,     giant_close);
  670         FIXUP(d_read,           no_read,        giant_read);
  671         FIXUP(d_write,          no_write,       giant_write);
  672         FIXUP(d_ioctl,          no_ioctl,       giant_ioctl);
  673         FIXUP(d_poll,           no_poll,        giant_poll);
  674         FIXUP(d_mmap2,          no_mmap,        giant_mmap);
  675         FIXUP(d_strategy,       no_strategy,    giant_strategy);
  676         FIXUP(d_kqfilter,       no_kqfilter,    giant_kqfilter);
  677         FIXUP(d_mmap_single,    no_mmap_single, giant_mmap_single);
  678 
  679         if (devsw->d_dump == NULL)      devsw->d_dump = no_dump;
  680 
  681         LIST_INIT(&devsw->d_devs);
  682 
  683         devsw->d_flags |= D_INIT;
  684 
  685         if (dsw2 != NULL)
  686                 cdevsw_free_devlocked(dsw2);
  687         return (0);
  688 }
  689 
  690 static int
  691 make_dev_credv(int flags, struct cdev **dres, struct cdevsw *devsw, int unit,
  692     struct ucred *cr, uid_t uid, gid_t gid, int mode, const char *fmt,
  693     va_list ap)
  694 {
  695         struct cdev *dev;
  696         int i, res;
  697 
  698         KASSERT((flags & MAKEDEV_WAITOK) == 0 || (flags & MAKEDEV_NOWAIT) == 0,
  699             ("make_dev_credv: both WAITOK and NOWAIT specified"));
  700         dev = devfs_alloc(flags);
  701         if (dev == NULL)
  702                 return (ENOMEM);
  703         dev_lock();
  704         res = prep_cdevsw(devsw, flags);
  705         if (res != 0) {
  706                 dev_unlock();
  707                 devfs_free(dev);
  708                 return (res);
  709         }
  710         dev = newdev(devsw, unit, dev);
  711         if (flags & MAKEDEV_REF)
  712                 dev_refl(dev);
  713         if (flags & MAKEDEV_ETERNAL)
  714                 dev->si_flags |= SI_ETERNAL;
  715         if (dev->si_flags & SI_CHEAPCLONE &&
  716             dev->si_flags & SI_NAMED) {
  717                 /*
  718                  * This is allowed as it removes races and generally
  719                  * simplifies cloning devices.
  720                  * XXX: still ??
  721                  */
  722                 dev_unlock_and_free();
  723                 *dres = dev;
  724                 return (0);
  725         }
  726         KASSERT(!(dev->si_flags & SI_NAMED),
  727             ("make_dev() by driver %s on pre-existing device (min=%x, name=%s)",
  728             devsw->d_name, dev2unit(dev), devtoname(dev)));
  729 
  730         i = vsnrprintf(dev->__si_namebuf, sizeof dev->__si_namebuf, 32, fmt, ap);
  731         if (i > (sizeof dev->__si_namebuf - 1)) {
  732                 printf("WARNING: Device name truncated! (%s)\n", 
  733                     dev->__si_namebuf);
  734         }
  735                 
  736         dev->si_flags |= SI_NAMED;
  737         if (cr != NULL)
  738                 dev->si_cred = crhold(cr);
  739         dev->si_uid = uid;
  740         dev->si_gid = gid;
  741         dev->si_mode = mode;
  742 
  743         devfs_create(dev);
  744         clean_unrhdrl(devfs_inos);
  745         dev_unlock_and_free();
  746 
  747         notify_create(dev, flags);
  748 
  749         *dres = dev;
  750         return (0);
  751 }
  752 
  753 struct cdev *
  754 make_dev(struct cdevsw *devsw, int unit, uid_t uid, gid_t gid, int mode,
  755     const char *fmt, ...)
  756 {
  757         struct cdev *dev;
  758         va_list ap;
  759         int res;
  760 
  761         va_start(ap, fmt);
  762         res = make_dev_credv(0, &dev, devsw, unit, NULL, uid, gid, mode, fmt,
  763             ap);
  764         va_end(ap);
  765         KASSERT(res == 0 && dev != NULL, ("make_dev: failed make_dev_credv"));
  766         return (dev);
  767 }
  768 
  769 struct cdev *
  770 make_dev_cred(struct cdevsw *devsw, int unit, struct ucred *cr, uid_t uid,
  771     gid_t gid, int mode, const char *fmt, ...)
  772 {
  773         struct cdev *dev;
  774         va_list ap;
  775         int res;
  776 
  777         va_start(ap, fmt);
  778         res = make_dev_credv(0, &dev, devsw, unit, cr, uid, gid, mode, fmt, ap);
  779         va_end(ap);
  780 
  781         KASSERT(res == 0 && dev != NULL,
  782             ("make_dev_cred: failed make_dev_credv"));
  783         return (dev);
  784 }
  785 
  786 struct cdev *
  787 make_dev_credf(int flags, struct cdevsw *devsw, int unit, struct ucred *cr,
  788     uid_t uid, gid_t gid, int mode, const char *fmt, ...)
  789 {
  790         struct cdev *dev;
  791         va_list ap;
  792         int res;
  793 
  794         va_start(ap, fmt);
  795         res = make_dev_credv(flags, &dev, devsw, unit, cr, uid, gid, mode,
  796             fmt, ap);
  797         va_end(ap);
  798 
  799         KASSERT((flags & MAKEDEV_NOWAIT) != 0 || res == 0,
  800             ("make_dev_credf: failed make_dev_credv"));
  801         return (res == 0 ? dev : NULL);
  802 }
  803 
  804 int
  805 make_dev_p(int flags, struct cdev **cdev, struct cdevsw *devsw,
  806     struct ucred *cr, uid_t uid, gid_t gid, int mode, const char *fmt, ...)
  807 {
  808         va_list ap;
  809         int res;
  810 
  811         va_start(ap, fmt);
  812         res = make_dev_credv(flags, cdev, devsw, 0, cr, uid, gid, mode,
  813             fmt, ap);
  814         va_end(ap);
  815 
  816         KASSERT((flags & MAKEDEV_NOWAIT) != 0 || res == 0,
  817             ("make_dev_p: failed make_dev_credv"));
  818         return (res);
  819 }
  820 
  821 static void
  822 dev_dependsl(struct cdev *pdev, struct cdev *cdev)
  823 {
  824 
  825         cdev->si_parent = pdev;
  826         cdev->si_flags |= SI_CHILD;
  827         LIST_INSERT_HEAD(&pdev->si_children, cdev, si_siblings);
  828 }
  829 
  830 
  831 void
  832 dev_depends(struct cdev *pdev, struct cdev *cdev)
  833 {
  834 
  835         dev_lock();
  836         dev_dependsl(pdev, cdev);
  837         dev_unlock();
  838 }
  839 
  840 struct cdev *
  841 make_dev_alias(struct cdev *pdev, const char *fmt, ...)
  842 {
  843         struct cdev *dev;
  844         va_list ap;
  845         int i;
  846 
  847         KASSERT(pdev != NULL, ("NULL pdev"));
  848         dev = devfs_alloc(MAKEDEV_WAITOK);
  849         dev_lock();
  850         dev->si_flags |= SI_ALIAS;
  851         dev->si_flags |= SI_NAMED;
  852         va_start(ap, fmt);
  853         i = vsnrprintf(dev->__si_namebuf, sizeof dev->__si_namebuf, 32, fmt, ap);
  854         if (i > (sizeof dev->__si_namebuf - 1)) {
  855                 printf("WARNING: Device name truncated! (%s)\n", 
  856                     dev->__si_namebuf);
  857         }
  858         va_end(ap);
  859 
  860         devfs_create(dev);
  861         dev_dependsl(pdev, dev);
  862         clean_unrhdrl(devfs_inos);
  863         dev_unlock();
  864 
  865         notify_create(dev, MAKEDEV_WAITOK);
  866 
  867         return (dev);
  868 }
  869 
  870 static void
  871 destroy_devl(struct cdev *dev)
  872 {
  873         struct cdevsw *csw;
  874         struct cdev_privdata *p;
  875 
  876         mtx_assert(&devmtx, MA_OWNED);
  877         KASSERT(dev->si_flags & SI_NAMED,
  878             ("WARNING: Driver mistake: destroy_dev on %d\n", dev2unit(dev)));
  879         KASSERT((dev->si_flags & SI_ETERNAL) == 0,
  880             ("WARNING: Driver mistake: destroy_dev on eternal %d\n",
  881              dev2unit(dev)));
  882 
  883         devfs_destroy(dev);
  884 
  885         /* Remove name marking */
  886         dev->si_flags &= ~SI_NAMED;
  887 
  888         dev->si_refcount++;     /* Avoid race with dev_rel() */
  889 
  890         /* If we are a child, remove us from the parents list */
  891         if (dev->si_flags & SI_CHILD) {
  892                 LIST_REMOVE(dev, si_siblings);
  893                 dev->si_flags &= ~SI_CHILD;
  894         }
  895 
  896         /* Kill our children */
  897         while (!LIST_EMPTY(&dev->si_children))
  898                 destroy_devl(LIST_FIRST(&dev->si_children));
  899 
  900         /* Remove from clone list */
  901         if (dev->si_flags & SI_CLONELIST) {
  902                 LIST_REMOVE(dev, si_clone);
  903                 dev->si_flags &= ~SI_CLONELIST;
  904         }
  905 
  906         csw = dev->si_devsw;
  907         dev->si_devsw = NULL;   /* already NULL for SI_ALIAS */
  908         while (csw != NULL && csw->d_purge != NULL && dev->si_threadcount) {
  909                 csw->d_purge(dev);
  910                 msleep(csw, &devmtx, PRIBIO, "devprg", hz/10);
  911                 if (dev->si_threadcount)
  912                         printf("Still %lu threads in %s\n",
  913                             dev->si_threadcount, devtoname(dev));
  914         }
  915         while (dev->si_threadcount != 0) {
  916                 /* Use unique dummy wait ident */
  917                 msleep(&csw, &devmtx, PRIBIO, "devdrn", hz / 10);
  918         }
  919 
  920         dev_unlock();
  921         notify_destroy(dev);
  922         mtx_lock(&cdevpriv_mtx);
  923         while ((p = LIST_FIRST(&cdev2priv(dev)->cdp_fdpriv)) != NULL) {
  924                 devfs_destroy_cdevpriv(p);
  925                 mtx_lock(&cdevpriv_mtx);
  926         }
  927         mtx_unlock(&cdevpriv_mtx);
  928         dev_lock();
  929 
  930         dev->si_drv1 = 0;
  931         dev->si_drv2 = 0;
  932         bzero(&dev->__si_u, sizeof(dev->__si_u));
  933 
  934         if (!(dev->si_flags & SI_ALIAS)) {
  935                 /* Remove from cdevsw list */
  936                 LIST_REMOVE(dev, si_list);
  937 
  938                 /* If cdevsw has no more struct cdev *'s, clean it */
  939                 if (LIST_EMPTY(&csw->d_devs)) {
  940                         fini_cdevsw(csw);
  941                         wakeup(&csw->d_devs);
  942                 }
  943         }
  944         dev->si_flags &= ~SI_ALIAS;
  945         dev->si_refcount--;     /* Avoid race with dev_rel() */
  946 
  947         if (dev->si_refcount > 0) {
  948                 LIST_INSERT_HEAD(&dead_cdevsw.d_devs, dev, si_list);
  949         } else {
  950                 dev_free_devlocked(dev);
  951         }
  952 }
  953 
  954 void
  955 destroy_dev(struct cdev *dev)
  956 {
  957 
  958         WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, "destroy_dev");
  959         dev_lock();
  960         destroy_devl(dev);
  961         dev_unlock_and_free();
  962 }
  963 
  964 const char *
  965 devtoname(struct cdev *dev)
  966 {
  967 
  968         return (dev->si_name);
  969 }
  970 
  971 int
  972 dev_stdclone(char *name, char **namep, const char *stem, int *unit)
  973 {
  974         int u, i;
  975 
  976         i = strlen(stem);
  977         if (bcmp(stem, name, i) != 0)
  978                 return (0);
  979         if (!isdigit(name[i]))
  980                 return (0);
  981         u = 0;
  982         if (name[i] == '' && isdigit(name[i+1]))
  983                 return (0);
  984         while (isdigit(name[i])) {
  985                 u *= 10;
  986                 u += name[i++] - '';
  987         }
  988         if (u > 0xffffff)
  989                 return (0);
  990         *unit = u;
  991         if (namep)
  992                 *namep = &name[i];
  993         if (name[i]) 
  994                 return (2);
  995         return (1);
  996 }
  997 
  998 /*
  999  * Helper functions for cloning device drivers.
 1000  *
 1001  * The objective here is to make it unnecessary for the device drivers to
 1002  * use rman or similar to manage their unit number space.  Due to the way
 1003  * we do "on-demand" devices, using rman or other "private" methods 
 1004  * will be very tricky to lock down properly once we lock down this file.
 1005  *
 1006  * Instead we give the drivers these routines which puts the struct cdev *'s
 1007  * that are to be managed on their own list, and gives the driver the ability
 1008  * to ask for the first free unit number or a given specified unit number.
 1009  *
 1010  * In addition these routines support paired devices (pty, nmdm and similar)
 1011  * by respecting a number of "flag" bits in the minor number.
 1012  *
 1013  */
 1014 
 1015 struct clonedevs {
 1016         LIST_HEAD(,cdev)        head;
 1017 };
 1018 
 1019 void
 1020 clone_setup(struct clonedevs **cdp)
 1021 {
 1022 
 1023         *cdp = malloc(sizeof **cdp, M_DEVBUF, M_WAITOK | M_ZERO);
 1024         LIST_INIT(&(*cdp)->head);
 1025 }
 1026 
 1027 int
 1028 clone_create(struct clonedevs **cdp, struct cdevsw *csw, int *up,
 1029     struct cdev **dp, int extra)
 1030 {
 1031         struct clonedevs *cd;
 1032         struct cdev *dev, *ndev, *dl, *de;
 1033         int unit, low, u;
 1034 
 1035         KASSERT(*cdp != NULL,
 1036             ("clone_setup() not called in driver \"%s\"", csw->d_name));
 1037         KASSERT(!(extra & CLONE_UNITMASK),
 1038             ("Illegal extra bits (0x%x) in clone_create", extra));
 1039         KASSERT(*up <= CLONE_UNITMASK,
 1040             ("Too high unit (0x%x) in clone_create", *up));
 1041         KASSERT(csw->d_flags & D_NEEDMINOR,
 1042             ("clone_create() on cdevsw without minor numbers"));
 1043 
 1044 
 1045         /*
 1046          * Search the list for a lot of things in one go:
 1047          *   A preexisting match is returned immediately.
 1048          *   The lowest free unit number if we are passed -1, and the place
 1049          *       in the list where we should insert that new element.
 1050          *   The place to insert a specified unit number, if applicable
 1051          *       the end of the list.
 1052          */
 1053         unit = *up;
 1054         ndev = devfs_alloc(MAKEDEV_WAITOK);
 1055         dev_lock();
 1056         prep_cdevsw(csw, MAKEDEV_WAITOK);
 1057         low = extra;
 1058         de = dl = NULL;
 1059         cd = *cdp;
 1060         LIST_FOREACH(dev, &cd->head, si_clone) {
 1061                 KASSERT(dev->si_flags & SI_CLONELIST,
 1062                     ("Dev %p(%s) should be on clonelist", dev, dev->si_name));
 1063                 u = dev2unit(dev);
 1064                 if (u == (unit | extra)) {
 1065                         *dp = dev;
 1066                         dev_unlock();
 1067                         devfs_free(ndev);
 1068                         return (0);
 1069                 }
 1070                 if (unit == -1 && u == low) {
 1071                         low++;
 1072                         de = dev;
 1073                         continue;
 1074                 } else if (u < (unit | extra)) {
 1075                         de = dev;
 1076                         continue;
 1077                 } else if (u > (unit | extra)) {
 1078                         dl = dev;
 1079                         break;
 1080                 }
 1081         }
 1082         if (unit == -1)
 1083                 unit = low & CLONE_UNITMASK;
 1084         dev = newdev(csw, unit | extra, ndev);
 1085         if (dev->si_flags & SI_CLONELIST) {
 1086                 printf("dev %p (%s) is on clonelist\n", dev, dev->si_name);
 1087                 printf("unit=%d, low=%d, extra=0x%x\n", unit, low, extra);
 1088                 LIST_FOREACH(dev, &cd->head, si_clone) {
 1089                         printf("\t%p %s\n", dev, dev->si_name);
 1090                 }
 1091                 panic("foo");
 1092         }
 1093         KASSERT(!(dev->si_flags & SI_CLONELIST),
 1094             ("Dev %p(%s) should not be on clonelist", dev, dev->si_name));
 1095         if (dl != NULL)
 1096                 LIST_INSERT_BEFORE(dl, dev, si_clone);
 1097         else if (de != NULL)
 1098                 LIST_INSERT_AFTER(de, dev, si_clone);
 1099         else
 1100                 LIST_INSERT_HEAD(&cd->head, dev, si_clone);
 1101         dev->si_flags |= SI_CLONELIST;
 1102         *up = unit;
 1103         dev_unlock_and_free();
 1104         return (1);
 1105 }
 1106 
 1107 /*
 1108  * Kill everything still on the list.  The driver should already have
 1109  * disposed of any softc hung of the struct cdev *'s at this time.
 1110  */
 1111 void
 1112 clone_cleanup(struct clonedevs **cdp)
 1113 {
 1114         struct cdev *dev;
 1115         struct cdev_priv *cp;
 1116         struct clonedevs *cd;
 1117         
 1118         cd = *cdp;
 1119         if (cd == NULL)
 1120                 return;
 1121         dev_lock();
 1122         while (!LIST_EMPTY(&cd->head)) {
 1123                 dev = LIST_FIRST(&cd->head);
 1124                 LIST_REMOVE(dev, si_clone);
 1125                 KASSERT(dev->si_flags & SI_CLONELIST,
 1126                     ("Dev %p(%s) should be on clonelist", dev, dev->si_name));
 1127                 dev->si_flags &= ~SI_CLONELIST;
 1128                 cp = cdev2priv(dev);
 1129                 if (!(cp->cdp_flags & CDP_SCHED_DTR)) {
 1130                         cp->cdp_flags |= CDP_SCHED_DTR;
 1131                         KASSERT(dev->si_flags & SI_NAMED,
 1132                                 ("Driver has goofed in cloning underways udev %x unit %x", dev2udev(dev), dev2unit(dev)));
 1133                         destroy_devl(dev);
 1134                 }
 1135         }
 1136         dev_unlock_and_free();
 1137         free(cd, M_DEVBUF);
 1138         *cdp = NULL;
 1139 }
 1140 
 1141 static TAILQ_HEAD(, cdev_priv) dev_ddtr =
 1142         TAILQ_HEAD_INITIALIZER(dev_ddtr);
 1143 static struct task dev_dtr_task;
 1144 
 1145 static void
 1146 destroy_dev_tq(void *ctx, int pending)
 1147 {
 1148         struct cdev_priv *cp;
 1149         struct cdev *dev;
 1150         void (*cb)(void *);
 1151         void *cb_arg;
 1152 
 1153         dev_lock();
 1154         while (!TAILQ_EMPTY(&dev_ddtr)) {
 1155                 cp = TAILQ_FIRST(&dev_ddtr);
 1156                 dev = &cp->cdp_c;
 1157                 KASSERT(cp->cdp_flags & CDP_SCHED_DTR,
 1158                     ("cdev %p in dev_destroy_tq without CDP_SCHED_DTR", cp));
 1159                 TAILQ_REMOVE(&dev_ddtr, cp, cdp_dtr_list);
 1160                 cb = cp->cdp_dtr_cb;
 1161                 cb_arg = cp->cdp_dtr_cb_arg;
 1162                 destroy_devl(dev);
 1163                 dev_unlock_and_free();
 1164                 dev_rel(dev);
 1165                 if (cb != NULL)
 1166                         cb(cb_arg);
 1167                 dev_lock();
 1168         }
 1169         dev_unlock();
 1170 }
 1171 
 1172 /*
 1173  * devmtx shall be locked on entry. devmtx will be unlocked after
 1174  * function return.
 1175  */
 1176 static int
 1177 destroy_dev_sched_cbl(struct cdev *dev, void (*cb)(void *), void *arg)
 1178 {
 1179         struct cdev_priv *cp;
 1180 
 1181         mtx_assert(&devmtx, MA_OWNED);
 1182         cp = cdev2priv(dev);
 1183         if (cp->cdp_flags & CDP_SCHED_DTR) {
 1184                 dev_unlock();
 1185                 return (0);
 1186         }
 1187         dev_refl(dev);
 1188         cp->cdp_flags |= CDP_SCHED_DTR;
 1189         cp->cdp_dtr_cb = cb;
 1190         cp->cdp_dtr_cb_arg = arg;
 1191         TAILQ_INSERT_TAIL(&dev_ddtr, cp, cdp_dtr_list);
 1192         dev_unlock();
 1193         taskqueue_enqueue(taskqueue_swi_giant, &dev_dtr_task);
 1194         return (1);
 1195 }
 1196 
 1197 int
 1198 destroy_dev_sched_cb(struct cdev *dev, void (*cb)(void *), void *arg)
 1199 {
 1200 
 1201         dev_lock();
 1202         return (destroy_dev_sched_cbl(dev, cb, arg));
 1203 }
 1204 
 1205 int
 1206 destroy_dev_sched(struct cdev *dev)
 1207 {
 1208 
 1209         return (destroy_dev_sched_cb(dev, NULL, NULL));
 1210 }
 1211 
 1212 void
 1213 destroy_dev_drain(struct cdevsw *csw)
 1214 {
 1215 
 1216         dev_lock();
 1217         while (!LIST_EMPTY(&csw->d_devs)) {
 1218                 msleep(&csw->d_devs, &devmtx, PRIBIO, "devscd", hz/10);
 1219         }
 1220         dev_unlock();
 1221 }
 1222 
 1223 void
 1224 drain_dev_clone_events(void)
 1225 {
 1226 
 1227         sx_xlock(&clone_drain_lock);
 1228         sx_xunlock(&clone_drain_lock);
 1229 }
 1230 
 1231 static void
 1232 devdtr_init(void *dummy __unused)
 1233 {
 1234 
 1235         TASK_INIT(&dev_dtr_task, 0, destroy_dev_tq, NULL);
 1236 }
 1237 
 1238 SYSINIT(devdtr, SI_SUB_DEVFS, SI_ORDER_SECOND, devdtr_init, NULL);
 1239 
 1240 #include "opt_ddb.h"
 1241 #ifdef DDB
 1242 #include <sys/kernel.h>
 1243 
 1244 #include <ddb/ddb.h>
 1245 
 1246 DB_SHOW_COMMAND(cdev, db_show_cdev)
 1247 {
 1248         struct cdev_priv *cdp;
 1249         struct cdev *dev;
 1250         u_int flags;
 1251         char buf[512];
 1252 
 1253         if (!have_addr) {
 1254                 TAILQ_FOREACH(cdp, &cdevp_list, cdp_list) {
 1255                         dev = &cdp->cdp_c;
 1256                         db_printf("%s %p\n", dev->si_name, dev);
 1257                         if (db_pager_quit)
 1258                                 break;
 1259                 }
 1260                 return;
 1261         }
 1262 
 1263         dev = (struct cdev *)addr;
 1264         cdp = cdev2priv(dev);
 1265         db_printf("dev %s ref %d use %ld thr %ld inuse %u fdpriv %p\n",
 1266             dev->si_name, dev->si_refcount, dev->si_usecount,
 1267             dev->si_threadcount, cdp->cdp_inuse, cdp->cdp_fdpriv.lh_first);
 1268         db_printf("devsw %p si_drv0 %d si_drv1 %p si_drv2 %p\n",
 1269             dev->si_devsw, dev->si_drv0, dev->si_drv1, dev->si_drv2);
 1270         flags = dev->si_flags;
 1271 #define SI_FLAG(flag)   do {                                            \
 1272         if (flags & (flag)) {                                           \
 1273                 if (buf[0] != '\0')                                     \
 1274                         strlcat(buf, ", ", sizeof(buf));                \
 1275                 strlcat(buf, (#flag) + 3, sizeof(buf));                 \
 1276                 flags &= ~(flag);                                       \
 1277         }                                                               \
 1278 } while (0)
 1279         buf[0] = '\0';
 1280         SI_FLAG(SI_ETERNAL);
 1281         SI_FLAG(SI_ALIAS);
 1282         SI_FLAG(SI_NAMED);
 1283         SI_FLAG(SI_CHEAPCLONE);
 1284         SI_FLAG(SI_CHILD);
 1285         SI_FLAG(SI_DEVOPEN);
 1286         SI_FLAG(SI_CONSOPEN);
 1287         SI_FLAG(SI_DUMPDEV);
 1288         SI_FLAG(SI_CANDELETE);
 1289         SI_FLAG(SI_CLONELIST);
 1290         db_printf("si_flags %s\n", buf);
 1291 
 1292         flags = cdp->cdp_flags;
 1293 #define CDP_FLAG(flag)  do {                                            \
 1294         if (flags & (flag)) {                                           \
 1295                 if (buf[0] != '\0')                                     \
 1296                         strlcat(buf, ", ", sizeof(buf));                \
 1297                 strlcat(buf, (#flag) + 4, sizeof(buf));                 \
 1298                 flags &= ~(flag);                                       \
 1299         }                                                               \
 1300 } while (0)
 1301         buf[0] = '\0';
 1302         CDP_FLAG(CDP_ACTIVE);
 1303         CDP_FLAG(CDP_SCHED_DTR);
 1304         db_printf("cdp_flags %s\n", buf);
 1305 }
 1306 #endif

Cache object: 4986c20923bd0b10a1d836dd6fce3413


[ 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.