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  * 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  * $FreeBSD: releng/5.1/sys/kern/kern_conf.c 113441 2003-04-13 15:27:49Z phk $
   27  */
   28 
   29 #include <sys/param.h>
   30 #include <sys/kernel.h>
   31 #include <sys/systm.h>
   32 #include <sys/bio.h>
   33 #include <sys/lock.h>
   34 #include <sys/mutex.h>
   35 #include <sys/sysctl.h>
   36 #include <sys/module.h>
   37 #include <sys/malloc.h>
   38 #include <sys/conf.h>
   39 #include <sys/vnode.h>
   40 #include <sys/queue.h>
   41 #include <sys/ctype.h>
   42 #include <machine/stdarg.h>
   43 
   44 static MALLOC_DEFINE(M_DEVT, "dev_t", "dev_t storage");
   45 
   46 /* Built at compile time from sys/conf/majors */
   47 extern unsigned char reserved_majors[256];
   48 
   49 /*
   50  * This is the number of hash-buckets.  Experiements with 'real-life'
   51  * udev_t's show that a prime halfway between two powers of two works
   52  * best.
   53  */
   54 #define DEVT_HASH 83
   55 
   56 /* The number of dev_t's we can create before malloc(9) kick in.  */
   57 #define DEVT_STASH 50
   58 
   59 static struct cdev devt_stash[DEVT_STASH];
   60 
   61 static LIST_HEAD(, cdev) dev_hash[DEVT_HASH];
   62 
   63 static LIST_HEAD(, cdev) dev_free;
   64 
   65 static int ready_for_devs;
   66 
   67 static int free_devt;
   68 SYSCTL_INT(_debug, OID_AUTO, free_devt, CTLFLAG_RW, &free_devt, 0, "");
   69 
   70 /* Define a dead_cdevsw for use when devices leave unexpectedly. */
   71 
   72 static int
   73 enxio(void)
   74 {
   75         return (ENXIO);
   76 }
   77 
   78 #define dead_open       (d_open_t *)enxio
   79 #define dead_close      (d_close_t *)enxio
   80 #define dead_read       (d_read_t *)enxio
   81 #define dead_write      (d_write_t *)enxio
   82 #define dead_ioctl      (d_ioctl_t *)enxio
   83 #define dead_poll       nopoll
   84 #define dead_mmap       nommap
   85 
   86 static void
   87 dead_strategy(struct bio *bp)
   88 {
   89 
   90         biofinish(bp, NULL, ENXIO);
   91 }
   92 
   93 #define dead_dump       (dumper_t *)enxio
   94 
   95 #define dead_kqfilter   (d_kqfilter_t *)enxio
   96 
   97 static struct cdevsw dead_cdevsw = {
   98         .d_open =       dead_open,
   99         .d_close =      dead_close,
  100         .d_read =       dead_read,
  101         .d_write =      dead_write,
  102         .d_ioctl =      dead_ioctl,
  103         .d_poll =       dead_poll,
  104         .d_mmap =       dead_mmap,
  105         .d_strategy =   dead_strategy,
  106         .d_name =       "dead",
  107         .d_maj =        255,
  108         .d_dump =       dead_dump,
  109         .d_kqfilter =   dead_kqfilter
  110 };
  111 
  112 
  113 struct cdevsw *
  114 devsw(dev_t dev)
  115 {
  116         if (dev->si_devsw)
  117                 return (dev->si_devsw);
  118         return (&dead_cdevsw);
  119 }
  120 
  121 /*
  122  * dev_t and u_dev_t primitives
  123  */
  124 
  125 int
  126 major(dev_t x)
  127 {
  128         if (x == NODEV)
  129                 return NOUDEV;
  130         return((x->si_udev >> 8) & 0xff);
  131 }
  132 
  133 int
  134 minor(dev_t x)
  135 {
  136         if (x == NODEV)
  137                 return NOUDEV;
  138         return(x->si_udev & 0xffff00ff);
  139 }
  140 
  141 int
  142 dev2unit(dev_t x)
  143 {
  144         int i;
  145 
  146         if (x == NODEV)
  147                 return NOUDEV;
  148         i = minor(x);
  149         return ((i & 0xff) | (i >> 8));
  150 }
  151 
  152 int
  153 unit2minor(int unit)
  154 {
  155 
  156         KASSERT(unit <= 0xffffff, ("Invalid unit (%d) in unit2minor", unit));
  157         return ((unit & 0xff) | ((unit << 8) & ~0xffff));
  158 }
  159 
  160 static dev_t
  161 allocdev(void)
  162 {
  163         static int stashed;
  164         struct cdev *si;
  165 
  166         if (LIST_FIRST(&dev_free)) {
  167                 si = LIST_FIRST(&dev_free);
  168                 LIST_REMOVE(si, si_hash);
  169         } else if (stashed >= DEVT_STASH) {
  170                 MALLOC(si, struct cdev *, sizeof(*si), M_DEVT,
  171                     M_USE_RESERVE | M_ZERO | M_WAITOK);
  172         } else {
  173                 si = devt_stash + stashed++;
  174                 bzero(si, sizeof *si);
  175                 si->si_flags |= SI_STASHED;
  176         }
  177         si->__si_namebuf[0] = '\0';
  178         si->si_name = si->__si_namebuf;
  179         LIST_INIT(&si->si_children);
  180         TAILQ_INIT(&si->si_snapshots);
  181         return (si);
  182 }
  183 
  184 dev_t
  185 makedev(int x, int y)
  186 {
  187         struct cdev *si;
  188         udev_t  udev;
  189         int hash;
  190 
  191         if (x == umajor(NOUDEV) && y == uminor(NOUDEV))
  192                 panic("makedev of NOUDEV");
  193         udev = (x << 8) | y;
  194         hash = udev % DEVT_HASH;
  195         LIST_FOREACH(si, &dev_hash[hash], si_hash) {
  196                 if (si->si_udev == udev)
  197                         return (si);
  198         }
  199         si = allocdev();
  200         si->si_udev = udev;
  201         LIST_INSERT_HEAD(&dev_hash[hash], si, si_hash);
  202         return (si);
  203 }
  204 
  205 void
  206 freedev(dev_t dev)
  207 {
  208 
  209         if (!free_devt)
  210                 return;
  211         if (SLIST_FIRST(&dev->si_hlist))
  212                 return;
  213         if (dev->si_devsw || dev->si_drv1 || dev->si_drv2)
  214                 return;
  215         LIST_REMOVE(dev, si_hash);
  216         if (dev->si_flags & SI_STASHED) {
  217                 bzero(dev, sizeof(*dev));
  218                 dev->si_flags |= SI_STASHED;
  219                 LIST_INSERT_HEAD(&dev_free, dev, si_hash);
  220         } else {
  221                 FREE(dev, M_DEVT);
  222         }
  223 }
  224 
  225 udev_t
  226 dev2udev(dev_t x)
  227 {
  228         if (x == NODEV)
  229                 return NOUDEV;
  230         return (x->si_udev);
  231 }
  232 
  233 dev_t
  234 udev2dev(udev_t x, int b)
  235 {
  236 
  237         if (x == NOUDEV)
  238                 return (NODEV);
  239         switch (b) {
  240                 case 0:
  241                         return makedev(umajor(x), uminor(x));
  242                 case 1:
  243                         return (NODEV);
  244                 default:
  245                         Debugger("udev2dev(...,X)");
  246                         return NODEV;
  247         }
  248 }
  249 
  250 int
  251 uminor(udev_t dev)
  252 {
  253         return(dev & 0xffff00ff);
  254 }
  255 
  256 int
  257 umajor(udev_t dev)
  258 {
  259         return((dev & 0xff00) >> 8);
  260 }
  261 
  262 udev_t
  263 makeudev(int x, int y)
  264 {
  265         return ((x << 8) | y);
  266 }
  267 
  268 dev_t
  269 make_dev(struct cdevsw *devsw, int minor, uid_t uid, gid_t gid, int perms, const char *fmt, ...)
  270 {
  271         dev_t   dev;
  272         va_list ap;
  273         int i;
  274 
  275         KASSERT((minor & ~0xffff00ff) == 0,
  276             ("Invalid minor (0x%x) in make_dev", minor));
  277 
  278         if (devsw->d_open == NULL)      devsw->d_open = noopen;
  279         if (devsw->d_close == NULL)     devsw->d_close = noclose;
  280         if (devsw->d_read == NULL)      devsw->d_read = noread;
  281         if (devsw->d_write == NULL)     devsw->d_write = nowrite;
  282         if (devsw->d_ioctl == NULL)     devsw->d_ioctl = noioctl;
  283         if (devsw->d_poll == NULL)      devsw->d_poll = nopoll;
  284         if (devsw->d_mmap == NULL)      devsw->d_mmap = nommap;
  285         if (devsw->d_strategy == NULL)  devsw->d_strategy = nostrategy;
  286         if (devsw->d_dump == NULL)      devsw->d_dump = nodump;
  287         if (devsw->d_kqfilter == NULL)  devsw->d_kqfilter = nokqfilter;
  288 
  289         if (devsw->d_maj == MAJOR_AUTO) {
  290                 for (i = NUMCDEVSW - 1; i > 0; i--)
  291                         if (reserved_majors[i] != i)
  292                                 break;
  293                 KASSERT(i > 0, ("Out of major numbers (%s)", devsw->d_name));
  294                 devsw->d_maj = i;
  295                 reserved_majors[i] = i;
  296         } else {
  297                 if (devsw->d_maj == 256)        /* XXX: tty_cons.c is magic */
  298                         devsw->d_maj = 0;       
  299                 KASSERT(devsw->d_maj >= 0 && devsw->d_maj < 256,
  300                     ("Invalid major (%d) in make_dev", devsw->d_maj));
  301                 if (reserved_majors[devsw->d_maj] != devsw->d_maj) {
  302                         printf("WARNING: driver \"%s\" used %s %d\n",
  303                             devsw->d_name, "unreserved major device number",
  304                             devsw->d_maj);
  305                         reserved_majors[devsw->d_maj] = devsw->d_maj;
  306                 }
  307         }
  308 
  309         if (!ready_for_devs) {
  310                 printf("WARNING: Driver mistake: make_dev(%s) called before SI_SUB_DRIVERS\n",
  311                        fmt);
  312                 /* XXX panic here once drivers are cleaned up */
  313         }
  314 
  315         dev = makedev(devsw->d_maj, minor);
  316         if (dev->si_flags & SI_NAMED) {
  317                 printf( "WARNING: Driver mistake: repeat make_dev(\"%s\")\n",
  318                     dev->si_name);
  319                 panic("don't do that");
  320                 return (dev);
  321         }
  322         va_start(ap, fmt);
  323         i = vsnrprintf(dev->__si_namebuf, sizeof dev->__si_namebuf, 32, fmt, ap);
  324         if (i > (sizeof dev->__si_namebuf - 1)) {
  325                 printf("WARNING: Device name truncated! (%s)", 
  326                     dev->__si_namebuf);
  327         }
  328         va_end(ap);
  329         dev->si_devsw = devsw;
  330         dev->si_uid = uid;
  331         dev->si_gid = gid;
  332         dev->si_mode = perms;
  333         dev->si_flags |= SI_NAMED;
  334 
  335         devfs_create(dev);
  336         return (dev);
  337 }
  338 
  339 int
  340 dev_named(dev_t pdev, const char *name)
  341 {
  342         dev_t cdev;
  343 
  344         if (strcmp(devtoname(pdev), name) == 0)
  345                 return (1);
  346         LIST_FOREACH(cdev, &pdev->si_children, si_siblings)
  347                 if (strcmp(devtoname(cdev), name) == 0)
  348                         return (1);
  349         return (0);
  350 }
  351 
  352 void
  353 dev_depends(dev_t pdev, dev_t cdev)
  354 {
  355 
  356         cdev->si_parent = pdev;
  357         cdev->si_flags |= SI_CHILD;
  358         LIST_INSERT_HEAD(&pdev->si_children, cdev, si_siblings);
  359 }
  360 
  361 dev_t
  362 make_dev_alias(dev_t pdev, const char *fmt, ...)
  363 {
  364         dev_t   dev;
  365         va_list ap;
  366         int i;
  367 
  368         dev = allocdev();
  369         dev->si_flags |= SI_ALIAS;
  370         dev->si_flags |= SI_NAMED;
  371         dev_depends(pdev, dev);
  372         va_start(ap, fmt);
  373         i = vsnrprintf(dev->__si_namebuf, sizeof dev->__si_namebuf, 32, fmt, ap);
  374         if (i > (sizeof dev->__si_namebuf - 1)) {
  375                 printf("WARNING: Device name truncated! (%s)", 
  376                     dev->__si_namebuf);
  377         }
  378         va_end(ap);
  379 
  380         devfs_create(dev);
  381         return (dev);
  382 }
  383 
  384 void
  385 revoke_and_destroy_dev(dev_t dev)
  386 {
  387         struct vnode *vp;
  388 
  389         GIANT_REQUIRED;
  390 
  391         vp = SLIST_FIRST(&dev->si_hlist);
  392         if (vp != NULL)
  393                 VOP_REVOKE(vp, REVOKEALL);
  394         destroy_dev(dev);
  395 }
  396 
  397 void
  398 destroy_dev(dev_t dev)
  399 {
  400         
  401         if (!(dev->si_flags & SI_NAMED)) {
  402                 printf( "WARNING: Driver mistake: destroy_dev on %d/%d\n",
  403                     major(dev), minor(dev));
  404                 panic("don't do that");
  405                 return;
  406         }
  407                 
  408         devfs_destroy(dev);
  409         if (dev->si_flags & SI_CHILD) {
  410                 LIST_REMOVE(dev, si_siblings);
  411                 dev->si_flags &= ~SI_CHILD;
  412         }
  413         while (!LIST_EMPTY(&dev->si_children))
  414                 destroy_dev(LIST_FIRST(&dev->si_children));
  415         dev->si_drv1 = 0;
  416         dev->si_drv2 = 0;
  417         dev->si_devsw = 0;
  418         bzero(&dev->__si_u, sizeof(dev->__si_u));
  419         dev->si_flags &= ~SI_NAMED;
  420         dev->si_flags &= ~SI_ALIAS;
  421         freedev(dev);
  422 }
  423 
  424 const char *
  425 devtoname(dev_t dev)
  426 {
  427         char *p;
  428         int mynor;
  429 
  430         if (dev->si_name[0] == '#' || dev->si_name[0] == '\0') {
  431                 p = dev->si_name;
  432                 if (devsw(dev))
  433                         sprintf(p, "#%s/", devsw(dev)->d_name);
  434                 else
  435                         sprintf(p, "#%d/", major(dev));
  436                 p += strlen(p);
  437                 mynor = minor(dev);
  438                 if (mynor < 0 || mynor > 255)
  439                         sprintf(p, "%#x", (u_int)mynor);
  440                 else
  441                         sprintf(p, "%d", mynor);
  442         }
  443         return (dev->si_name);
  444 }
  445 
  446 int
  447 dev_stdclone(char *name, char **namep, const char *stem, int *unit)
  448 {
  449         int u, i;
  450 
  451         i = strlen(stem);
  452         if (bcmp(stem, name, i) != 0)
  453                 return (0);
  454         if (!isdigit(name[i]))
  455                 return (0);
  456         u = 0;
  457         if (name[i] == '' && isdigit(name[i+1]))
  458                 return (0);
  459         while (isdigit(name[i])) {
  460                 u *= 10;
  461                 u += name[i++] - '';
  462         }
  463         if (u > 0xffffff)
  464                 return (0);
  465         *unit = u;
  466         if (namep)
  467                 *namep = &name[i];
  468         if (name[i]) 
  469                 return (2);
  470         return (1);
  471 }
  472 
  473 /*
  474  * Helper sysctl for devname(3).  We're given a {u}dev_t and return
  475  * the name, if any, registered by the device driver.
  476  */
  477 static int
  478 sysctl_devname(SYSCTL_HANDLER_ARGS)
  479 {
  480         int error;
  481         udev_t ud;
  482         dev_t dev;
  483 
  484         error = SYSCTL_IN(req, &ud, sizeof (ud));
  485         if (error)
  486                 return (error);
  487         if (ud == NOUDEV)
  488                 return(EINVAL);
  489         dev = makedev(umajor(ud), uminor(ud));
  490         if (dev->si_name[0] == '\0')
  491                 error = ENOENT;
  492         else
  493                 error = SYSCTL_OUT(req, dev->si_name, strlen(dev->si_name) + 1);
  494         freedev(dev);
  495         return (error);
  496 }
  497 
  498 SYSCTL_PROC(_kern, OID_AUTO, devname, CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_ANYBODY,
  499         NULL, 0, sysctl_devname, "", "devname(3) handler");
  500 
  501 /*
  502  * Set ready_for_devs; prior to this point, device creation is not allowed.
  503  */     
  504 static void
  505 dev_set_ready(void *junk)
  506 {
  507         ready_for_devs = 1;
  508 }
  509 
  510 SYSINIT(dev_ready, SI_SUB_DEVFS, SI_ORDER_FIRST, dev_set_ready, NULL);

Cache object: b84693901432ca67159a6815ce8483c6


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