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/fs/devfs/devfs_devs.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) 2000,2004
    3  *      Poul-Henning Kamp.  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. Neither the name of the University nor the names of its contributors
   11  *    may be used to endorse or promote products derived from this software
   12  *    without specific prior written permission.
   13  *
   14  * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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  * From: FreeBSD: src/sys/miscfs/kernfs/kernfs_vfsops.c 1.36
   27  *
   28  * $FreeBSD: releng/6.0/sys/fs/devfs/devfs_devs.c 150569 2005-09-26 14:36:54Z phk $
   29  */
   30 
   31 #include "opt_devfs.h"
   32 #include "opt_mac.h"
   33 
   34 #include <sys/param.h>
   35 #include <sys/systm.h>
   36 #include <sys/conf.h>
   37 #include <sys/dirent.h>
   38 #include <sys/kernel.h>
   39 #include <sys/limits.h>
   40 #include <sys/lock.h>
   41 #include <sys/mac.h>
   42 #include <sys/malloc.h>
   43 #include <sys/proc.h>
   44 #include <sys/sx.h>
   45 #include <sys/sysctl.h>
   46 #include <sys/vnode.h>
   47 
   48 #include <sys/kdb.h>
   49 
   50 #include <fs/devfs/devfs.h>
   51 #include <fs/devfs/devfs_int.h>
   52 
   53 /*
   54  * The one true (but secret) list of active devices in the system.
   55  * Locked by dev_lock()/devmtx
   56  */
   57 static TAILQ_HEAD(,cdev_priv) cdevp_list = TAILQ_HEAD_INITIALIZER(cdevp_list);
   58 
   59 struct unrhdr *devfs_inos;
   60 
   61 
   62 static MALLOC_DEFINE(M_DEVFS2, "DEVFS2", "DEVFS data 2");
   63 static MALLOC_DEFINE(M_DEVFS3, "DEVFS3", "DEVFS data 3");
   64 static MALLOC_DEFINE(M_CDEVP, "DEVFS1", "DEVFS cdev_priv storage");
   65 
   66 static SYSCTL_NODE(_vfs, OID_AUTO, devfs, CTLFLAG_RW, 0, "DEVFS filesystem");
   67 
   68 static unsigned devfs_generation;
   69 SYSCTL_UINT(_vfs_devfs, OID_AUTO, generation, CTLFLAG_RD,
   70         &devfs_generation, 0, "DEVFS generation number");
   71 
   72 unsigned devfs_rule_depth = 1;
   73 SYSCTL_UINT(_vfs_devfs, OID_AUTO, rule_depth, CTLFLAG_RW,
   74         &devfs_rule_depth, 0, "Max depth of ruleset include");
   75 
   76 /*
   77  * Helper sysctl for devname(3).  We're given a struct cdev * and return
   78  * the name, if any, registered by the device driver.
   79  */
   80 static int
   81 sysctl_devname(SYSCTL_HANDLER_ARGS)
   82 {
   83         int error;
   84         dev_t ud;
   85         struct cdev_priv *cdp;
   86 
   87         error = SYSCTL_IN(req, &ud, sizeof (ud));
   88         if (error)
   89                 return (error);
   90         if (ud == NODEV)
   91                 return(EINVAL);
   92 /*
   93         ud ^ devfs_random();
   94 */
   95         dev_lock();
   96         TAILQ_FOREACH(cdp, &cdevp_list, cdp_list)
   97                 if (cdp->cdp_inode == ud)
   98                         break;
   99         dev_unlock();
  100         if (cdp == NULL)
  101                 return(ENOENT);
  102         return(SYSCTL_OUT(req, cdp->cdp_c.si_name, strlen(cdp->cdp_c.si_name) + 1));
  103         return (error);
  104 }
  105 
  106 SYSCTL_PROC(_kern, OID_AUTO, devname, CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_ANYBODY,
  107         NULL, 0, sysctl_devname, "", "devname(3) handler");
  108 
  109 SYSCTL_INT(_debug_sizeof, OID_AUTO, cdev, CTLFLAG_RD,
  110     0, sizeof(struct cdev), "sizeof(struct cdev)");
  111 
  112 SYSCTL_INT(_debug_sizeof, OID_AUTO, cdev_priv, CTLFLAG_RD,
  113     0, sizeof(struct cdev_priv), "sizeof(struct cdev_priv)");
  114 
  115 struct cdev *
  116 devfs_alloc(void)
  117 {
  118         struct cdev_priv *cdp;
  119         struct cdev *cdev;
  120 
  121         cdp = malloc(sizeof *cdp, M_CDEVP, M_USE_RESERVE | M_ZERO | M_WAITOK);
  122 
  123         cdp->cdp_dirents = &cdp->cdp_dirent0;
  124         cdp->cdp_dirent0 = NULL;
  125         cdp->cdp_maxdirent = 0;
  126 
  127         cdev = &cdp->cdp_c;
  128         cdev->si_priv = cdp;
  129 
  130         cdev->si_name = cdev->__si_namebuf;
  131         LIST_INIT(&cdev->si_children);
  132         return (cdev);
  133 }
  134 
  135 void
  136 devfs_free(struct cdev *cdev)
  137 {
  138         struct cdev_priv *cdp;
  139 
  140         cdp = cdev->si_priv;
  141         if (cdev->si_cred != NULL)
  142                 crfree(cdev->si_cred);
  143         if (cdp->cdp_inode > 0)
  144                 free_unr(devfs_inos, cdp->cdp_inode);
  145         if (cdp->cdp_maxdirent > 0) 
  146                 free(cdp->cdp_dirents, M_DEVFS2);
  147         free(cdp, M_CDEVP);
  148 }
  149 
  150 struct devfs_dirent *
  151 devfs_find(struct devfs_dirent *dd, const char *name, int namelen)
  152 {
  153         struct devfs_dirent *de;
  154 
  155         TAILQ_FOREACH(de, &dd->de_dlist, de_list) {
  156                 if (namelen != de->de_dirent->d_namlen)
  157                         continue;
  158                 if (bcmp(name, de->de_dirent->d_name, namelen) != 0)
  159                         continue;
  160                 break;
  161         }
  162         return (de);
  163 }
  164 
  165 struct devfs_dirent *
  166 devfs_newdirent(char *name, int namelen)
  167 {
  168         int i;
  169         struct devfs_dirent *de;
  170         struct dirent d;
  171 
  172         d.d_namlen = namelen;
  173         i = sizeof (*de) + GENERIC_DIRSIZ(&d); 
  174         de = malloc(i, M_DEVFS3, M_WAITOK | M_ZERO);
  175         de->de_dirent = (struct dirent *)(de + 1);
  176         de->de_dirent->d_namlen = namelen;
  177         de->de_dirent->d_reclen = GENERIC_DIRSIZ(&d);
  178         bcopy(name, de->de_dirent->d_name, namelen);
  179         de->de_dirent->d_name[namelen] = '\0';
  180         vfs_timestamp(&de->de_ctime);
  181         de->de_mtime = de->de_atime = de->de_ctime;
  182         de->de_links = 1;
  183 #ifdef MAC
  184         mac_init_devfsdirent(de);
  185 #endif
  186         return (de);
  187 }
  188 
  189 struct devfs_dirent *
  190 devfs_vmkdir(struct devfs_mount *dmp, char *name, int namelen, struct devfs_dirent *dotdot, u_int inode)
  191 {
  192         struct devfs_dirent *dd;
  193         struct devfs_dirent *de;
  194 
  195         /* Create the new directory */
  196         dd = devfs_newdirent(name, namelen);
  197         TAILQ_INIT(&dd->de_dlist);
  198         dd->de_dirent->d_type = DT_DIR;
  199         dd->de_mode = 0555;
  200         dd->de_links = 2;
  201         dd->de_dir = dd;
  202         if (inode != 0)
  203                 dd->de_inode = inode;
  204         else
  205                 dd->de_inode = alloc_unr(devfs_inos);
  206 
  207         /* Create the "." entry in the new directory */
  208         de = devfs_newdirent(".", 1);
  209         de->de_dirent->d_type = DT_DIR;
  210         de->de_flags |= DE_DOT;
  211         TAILQ_INSERT_TAIL(&dd->de_dlist, de, de_list);
  212         de->de_dir = dd;
  213 
  214         /* Create the ".." entry in the new directory */
  215         de = devfs_newdirent("..", 2);
  216         de->de_dirent->d_type = DT_DIR;
  217         de->de_flags |= DE_DOTDOT;
  218         TAILQ_INSERT_TAIL(&dd->de_dlist, de, de_list);
  219         if (dotdot == NULL) {
  220                 de->de_dir = dd;
  221         } else {
  222                 de->de_dir = dotdot;
  223                 TAILQ_INSERT_TAIL(&dotdot->de_dlist, dd, de_list);
  224                 dotdot->de_links++;
  225         }
  226 
  227 #ifdef MAC
  228         mac_create_devfs_directory(dmp->dm_mount, name, namelen, dd);
  229 #endif
  230         return (dd);
  231 }
  232 
  233 void
  234 devfs_delete(struct devfs_mount *dm, struct devfs_dirent *de)
  235 {
  236 
  237         if (de->de_symlink) {
  238                 free(de->de_symlink, M_DEVFS);
  239                 de->de_symlink = NULL;
  240         }
  241         if (de->de_vnode != NULL) {
  242                 de->de_vnode->v_data = NULL;
  243                 vgone(de->de_vnode);
  244                 de->de_vnode = NULL;
  245         }
  246 #ifdef MAC
  247         mac_destroy_devfsdirent(de);
  248 #endif
  249         if (de->de_inode > DEVFS_ROOTINO) {
  250                 free_unr(devfs_inos, de->de_inode);
  251                 de->de_inode = 0;
  252         }
  253         free(de, M_DEVFS3);
  254 }
  255 
  256 /*
  257  * Called on unmount.
  258  * Recursively removes the entire tree
  259  */
  260 
  261 static void
  262 devfs_purge(struct devfs_mount *dm, struct devfs_dirent *dd)
  263 {
  264         struct devfs_dirent *de;
  265 
  266         sx_assert(&dm->dm_lock, SX_XLOCKED);
  267         for (;;) {
  268                 de = TAILQ_FIRST(&dd->de_dlist);
  269                 if (de == NULL)
  270                         break;
  271                 TAILQ_REMOVE(&dd->de_dlist, de, de_list);
  272                 if (de->de_flags & (DE_DOT|DE_DOTDOT))
  273                         devfs_delete(dm, de);
  274                 else if (de->de_dirent->d_type == DT_DIR)
  275                         devfs_purge(dm, de);
  276                 else 
  277                         devfs_delete(dm, de);
  278         }
  279         devfs_delete(dm, dd);
  280 }
  281 
  282 /*
  283  * Each cdev_priv has an array of pointers to devfs_dirent which is indexed
  284  * by the mount points dm_idx.
  285  * This function extends the array when necessary, taking into account that
  286  * the default array is 1 element and not malloc'ed.
  287  */
  288 static void
  289 devfs_metoo(struct cdev_priv *cdp, struct devfs_mount *dm)
  290 {
  291         struct devfs_dirent **dep;
  292         int siz;
  293 
  294         siz = (dm->dm_idx + 1) * sizeof *dep;
  295         dep = malloc(siz, M_DEVFS2, M_WAITOK | M_ZERO);
  296         dev_lock();
  297         if (dm->dm_idx <= cdp->cdp_maxdirent) {
  298                 /* We got raced */
  299                 dev_unlock();
  300                 free(dep, M_DEVFS2);
  301                 return;
  302         } 
  303         memcpy(dep, cdp->cdp_dirents, (cdp->cdp_maxdirent + 1) * sizeof *dep);
  304         if (cdp->cdp_maxdirent > 0)
  305                 free(cdp->cdp_dirents, M_DEVFS2);
  306         cdp->cdp_dirents = dep;
  307         /*
  308          * XXX: if malloc told us how much we actually got this could
  309          * XXX: be optimized.
  310          */
  311         cdp->cdp_maxdirent = dm->dm_idx;
  312         dev_unlock();
  313 }
  314 
  315 static int
  316 devfs_populate_loop(struct devfs_mount *dm, int cleanup)
  317 {
  318         struct cdev_priv *cdp;
  319         struct devfs_dirent *de;
  320         struct devfs_dirent *dd;
  321         struct cdev *pdev;
  322         int j;
  323         char *q, *s;
  324 
  325         sx_assert(&dm->dm_lock, SX_XLOCKED);
  326         dev_lock();
  327         TAILQ_FOREACH(cdp, &cdevp_list, cdp_list) {
  328 
  329                 KASSERT(cdp->cdp_dirents != NULL, ("NULL cdp_dirents"));
  330 
  331                 /*
  332                  * If we are unmounting, or the device has been destroyed,
  333                  * clean up our dirent.
  334                  */
  335                 if ((cleanup || !(cdp->cdp_flags & CDP_ACTIVE)) &&
  336                     dm->dm_idx <= cdp->cdp_maxdirent &&
  337                     cdp->cdp_dirents[dm->dm_idx] != NULL) {
  338                         de = cdp->cdp_dirents[dm->dm_idx];
  339                         cdp->cdp_dirents[dm->dm_idx] = NULL;
  340                         cdp->cdp_inuse--;
  341                         KASSERT(cdp == de->de_cdp,
  342                             ("%s %d %s %p %p", __func__, __LINE__,
  343                             cdp->cdp_c.si_name, cdp, de->de_cdp));
  344                         KASSERT(de->de_dir != NULL, ("Null de->de_dir"));
  345                         dev_unlock();
  346 
  347                         TAILQ_REMOVE(&de->de_dir->de_dlist, de, de_list);
  348                         de->de_cdp = NULL;
  349                         de->de_inode = 0;
  350                         devfs_delete(dm, de);
  351                         return (1);
  352                 }
  353                 /*
  354                  * GC any lingering devices
  355                  */
  356                 if (!(cdp->cdp_flags & CDP_ACTIVE)) {
  357                         if (cdp->cdp_inuse > 0)
  358                                 continue;
  359                         TAILQ_REMOVE(&cdevp_list, cdp, cdp_list);
  360                         dev_unlock();
  361                         dev_rel(&cdp->cdp_c);
  362                         return (1);
  363                 }
  364                 /*
  365                  * Don't create any new dirents if we are unmounting
  366                  */
  367                 if (cleanup)
  368                         continue;
  369                 KASSERT((cdp->cdp_flags & CDP_ACTIVE), ("Bogons, I tell ya'!"));
  370 
  371                 if (dm->dm_idx <= cdp->cdp_maxdirent &&
  372                     cdp->cdp_dirents[dm->dm_idx] != NULL) {
  373                         de = cdp->cdp_dirents[dm->dm_idx];
  374                         KASSERT(cdp == de->de_cdp, ("inconsistent cdp"));
  375                         continue;
  376                 }
  377 
  378 
  379                 cdp->cdp_inuse++;
  380                 dev_unlock();
  381 
  382                 if (dm->dm_idx > cdp->cdp_maxdirent)
  383                         devfs_metoo(cdp, dm);
  384 
  385                 dd = dm->dm_rootdir;
  386                 s = cdp->cdp_c.si_name;
  387                 for (;;) {
  388                         for (q = s; *q != '/' && *q != '\0'; q++)
  389                                 continue;
  390                         if (*q != '/')
  391                                 break;
  392                         de = devfs_find(dd, s, q - s);
  393                         if (de == NULL)
  394                                 de = devfs_vmkdir(dm, s, q - s, dd, 0);
  395                         s = q + 1;
  396                         dd = de;
  397                 }
  398 
  399                 de = devfs_newdirent(s, q - s);
  400                 if (cdp->cdp_c.si_flags & SI_ALIAS) {
  401                         de->de_uid = 0;
  402                         de->de_gid = 0;
  403                         de->de_mode = 0755;
  404                         de->de_dirent->d_type = DT_LNK;
  405                         pdev = cdp->cdp_c.si_parent;
  406                         j = strlen(pdev->si_name) + 1;
  407                         de->de_symlink = malloc(j, M_DEVFS, M_WAITOK);
  408                         bcopy(pdev->si_name, de->de_symlink, j);
  409                 } else {
  410                         de->de_uid = cdp->cdp_c.si_uid;
  411                         de->de_gid = cdp->cdp_c.si_gid;
  412                         de->de_mode = cdp->cdp_c.si_mode;
  413                         de->de_dirent->d_type = DT_CHR;
  414                 }
  415                 de->de_inode = cdp->cdp_inode;
  416                 de->de_cdp = cdp;
  417 #ifdef MAC
  418                 mac_create_devfs_device(cdp->cdp_c.si_cred, dm->dm_mount,
  419                     &cdp->cdp_c, de);
  420 #endif
  421                 de->de_dir = dd;
  422                 TAILQ_INSERT_TAIL(&dd->de_dlist, de, de_list);
  423                 devfs_rules_apply(dm, de);
  424                 dev_lock();
  425                 /* XXX: could check that cdp is still active here */
  426                 KASSERT(cdp->cdp_dirents[dm->dm_idx] == NULL,
  427                     ("%s %d\n", __func__, __LINE__));
  428                 cdp->cdp_dirents[dm->dm_idx] = de;
  429                 KASSERT(de->de_cdp != (void *)0xdeadc0de,
  430                     ("%s %d\n", __func__, __LINE__));
  431                 dev_unlock();
  432                 return (1);
  433         }
  434         dev_unlock();
  435         return (0);
  436 }
  437 
  438 void
  439 devfs_populate(struct devfs_mount *dm)
  440 {
  441 
  442         sx_assert(&dm->dm_lock, SX_XLOCKED);
  443         if (dm->dm_generation == devfs_generation)
  444                 return;
  445         while (devfs_populate_loop(dm, 0))
  446                 continue;
  447         dm->dm_generation = devfs_generation;
  448 }
  449 
  450 void
  451 devfs_cleanup(struct devfs_mount *dm)
  452 {
  453 
  454         sx_assert(&dm->dm_lock, SX_XLOCKED);
  455         while (devfs_populate_loop(dm, 1))
  456                 continue;
  457         devfs_purge(dm, dm->dm_rootdir);
  458 }
  459 
  460 /*
  461  * devfs_create() and devfs_destroy() are called from kern_conf.c and
  462  * in both cases the devlock() mutex is held, so no further locking
  463  * is necesary and no sleeping allowed.
  464  */
  465 
  466 void
  467 devfs_create(struct cdev *dev)
  468 {
  469         struct cdev_priv *cdp;
  470 
  471         mtx_assert(&devmtx, MA_OWNED);
  472         cdp = dev->si_priv;
  473         cdp->cdp_flags |= CDP_ACTIVE;
  474         cdp->cdp_inode = alloc_unrl(devfs_inos);
  475         dev_refl(dev);
  476         TAILQ_INSERT_TAIL(&cdevp_list, cdp, cdp_list);
  477         devfs_generation++;
  478 }
  479 
  480 void
  481 devfs_destroy(struct cdev *dev)
  482 {
  483         struct cdev_priv *cdp;
  484 
  485         mtx_assert(&devmtx, MA_OWNED);
  486         cdp = dev->si_priv;
  487         cdp->cdp_flags &= ~CDP_ACTIVE;
  488         devfs_generation++;
  489 }
  490 
  491 static void
  492 devfs_devs_init(void *junk __unused)
  493 {
  494 
  495         devfs_inos = new_unrhdr(DEVFS_ROOTINO + 1, INT_MAX, &devmtx);
  496 }
  497 
  498 SYSINIT(devfs_devs, SI_SUB_DEVFS, SI_ORDER_FIRST, devfs_devs_init, NULL);

Cache object: c1f3cde8d1d4b38310ef5805d4341468


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