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/pseudofs/pseudofs_vncache.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) 2001 Dag-Erling Coïdan Smørgrav
    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  *    in this position and unchanged.
   11  * 2. Redistributions in binary form must reproduce the above copyright
   12  *    notice, this list of conditions and the following disclaimer in the
   13  *    documentation and/or other materials provided with the distribution.
   14  * 3. The name of the author may not be used to endorse or promote products
   15  *    derived from this software without specific prior written permission.
   16  *
   17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   27  */
   28 
   29 #include <sys/cdefs.h>
   30 __FBSDID("$FreeBSD$");
   31 
   32 #include "opt_pseudofs.h"
   33 
   34 #include <sys/param.h>
   35 #include <sys/kernel.h>
   36 #include <sys/systm.h>
   37 #include <sys/eventhandler.h>
   38 #include <sys/lock.h>
   39 #include <sys/malloc.h>
   40 #include <sys/mutex.h>
   41 #include <sys/proc.h>
   42 #include <sys/sysctl.h>
   43 #include <sys/vnode.h>
   44 
   45 #include <fs/pseudofs/pseudofs.h>
   46 #include <fs/pseudofs/pseudofs_internal.h>
   47 
   48 static MALLOC_DEFINE(M_PFSVNCACHE, "pfs_vncache", "pseudofs vnode cache");
   49 
   50 static struct mtx pfs_vncache_mutex;
   51 static struct pfs_vdata *pfs_vncache;
   52 static eventhandler_tag pfs_exit_tag;
   53 static void pfs_exit(void *arg, struct proc *p);
   54 
   55 SYSCTL_NODE(_vfs_pfs, OID_AUTO, vncache, CTLFLAG_RW, 0,
   56     "pseudofs vnode cache");
   57 
   58 static int pfs_vncache_entries;
   59 SYSCTL_INT(_vfs_pfs_vncache, OID_AUTO, entries, CTLFLAG_RD,
   60     &pfs_vncache_entries, 0,
   61     "number of entries in the vnode cache");
   62 
   63 static int pfs_vncache_maxentries;
   64 SYSCTL_INT(_vfs_pfs_vncache, OID_AUTO, maxentries, CTLFLAG_RD,
   65     &pfs_vncache_maxentries, 0,
   66     "highest number of entries in the vnode cache");
   67 
   68 static int pfs_vncache_hits;
   69 SYSCTL_INT(_vfs_pfs_vncache, OID_AUTO, hits, CTLFLAG_RD,
   70     &pfs_vncache_hits, 0,
   71     "number of cache hits since initialization");
   72 
   73 static int pfs_vncache_misses;
   74 SYSCTL_INT(_vfs_pfs_vncache, OID_AUTO, misses, CTLFLAG_RD,
   75     &pfs_vncache_misses, 0,
   76     "number of cache misses since initialization");
   77 
   78 extern struct vop_vector pfs_vnodeops;  /* XXX -> .h file */
   79 
   80 /*
   81  * Initialize vnode cache
   82  */
   83 void
   84 pfs_vncache_load(void)
   85 {
   86 
   87         mtx_assert(&Giant, MA_OWNED);
   88         mtx_init(&pfs_vncache_mutex, "pfs_vncache", NULL, MTX_DEF);
   89         pfs_exit_tag = EVENTHANDLER_REGISTER(process_exit, pfs_exit, NULL,
   90             EVENTHANDLER_PRI_ANY);
   91 }
   92 
   93 /*
   94  * Tear down vnode cache
   95  */
   96 void
   97 pfs_vncache_unload(void)
   98 {
   99 
  100         mtx_assert(&Giant, MA_OWNED);
  101         EVENTHANDLER_DEREGISTER(process_exit, pfs_exit_tag);
  102         KASSERT(pfs_vncache_entries == 0,
  103             ("%d vncache entries remaining", pfs_vncache_entries));
  104         mtx_destroy(&pfs_vncache_mutex);
  105 }
  106 
  107 /*
  108  * Allocate a vnode
  109  */
  110 int
  111 pfs_vncache_alloc(struct mount *mp, struct vnode **vpp,
  112                   struct pfs_node *pn, pid_t pid)
  113 {
  114         struct pfs_vdata *pvd, *pvd2;
  115         struct vnode *vp;
  116         int error;
  117 
  118         /*
  119          * See if the vnode is in the cache.
  120          * XXX linear search is not very efficient.
  121          */
  122 retry:
  123         mtx_lock(&pfs_vncache_mutex);
  124         for (pvd = pfs_vncache; pvd; pvd = pvd->pvd_next) {
  125                 if (pvd->pvd_pn == pn && pvd->pvd_pid == pid &&
  126                     pvd->pvd_vnode->v_mount == mp) {
  127                         vp = pvd->pvd_vnode;
  128                         VI_LOCK(vp);
  129                         mtx_unlock(&pfs_vncache_mutex);
  130                         if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, curthread) == 0) {
  131                                 ++pfs_vncache_hits;
  132                                 *vpp = vp;
  133                                 /*
  134                                  * Some callers cache_enter(vp) later, so
  135                                  * we have to make sure it's not in the
  136                                  * VFS cache so it doesn't get entered
  137                                  * twice.  A better solution would be to
  138                                  * make pfs_vncache_alloc() responsible
  139                                  * for entering the vnode in the VFS
  140                                  * cache.
  141                                  */
  142                                 cache_purge(vp);
  143                                 return (0);
  144                         }
  145                         goto retry;
  146                 }
  147         }
  148         mtx_unlock(&pfs_vncache_mutex);
  149 
  150         /* nope, get a new one */
  151         MALLOC(pvd, struct pfs_vdata *, sizeof *pvd, M_PFSVNCACHE, M_WAITOK);
  152         pvd->pvd_next = pvd->pvd_prev = NULL;
  153         error = getnewvnode("pseudofs", mp, &pfs_vnodeops, vpp);
  154         if (error) {
  155                 FREE(pvd, M_PFSVNCACHE);
  156                 return (error);
  157         }
  158         pvd->pvd_pn = pn;
  159         pvd->pvd_pid = pid;
  160         (*vpp)->v_data = pvd;
  161         switch (pn->pn_type) {
  162         case pfstype_root:
  163                 (*vpp)->v_vflag = VV_ROOT;
  164 #if 0
  165                 printf("root vnode allocated\n");
  166 #endif
  167                 /* fall through */
  168         case pfstype_dir:
  169         case pfstype_this:
  170         case pfstype_parent:
  171         case pfstype_procdir:
  172                 (*vpp)->v_type = VDIR;
  173                 break;
  174         case pfstype_file:
  175                 (*vpp)->v_type = VREG;
  176                 break;
  177         case pfstype_symlink:
  178                 (*vpp)->v_type = VLNK;
  179                 break;
  180         case pfstype_none:
  181                 KASSERT(0, ("pfs_vncache_alloc called for null node\n"));
  182         default:
  183                 panic("%s has unexpected type: %d", pn->pn_name, pn->pn_type);
  184         }
  185         /*
  186          * Propagate flag through to vnode so users know it can change
  187          * if the process changes (i.e. execve)
  188          */
  189         if ((pn->pn_flags & PFS_PROCDEP) != 0)
  190                 (*vpp)->v_vflag |= VV_PROCDEP;
  191         pvd->pvd_vnode = *vpp;
  192         (*vpp)->v_vnlock->lk_flags |= LK_CANRECURSE;
  193         vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY, curthread);
  194         error = insmntque(*vpp, mp);
  195         if (error != 0) {
  196                 *vpp = NULLVP;
  197                 return (error);
  198         }
  199 retry2:
  200         mtx_lock(&pfs_vncache_mutex);
  201         /*
  202          * Other thread may race with us, creating the entry we are
  203          * going to insert into the cache. Recheck after
  204          * pfs_vncache_mutex is reacquired.
  205          */
  206         for (pvd2 = pfs_vncache; pvd2; pvd2 = pvd2->pvd_next) {
  207                 if (pvd2->pvd_pn == pn && pvd2->pvd_pid == pid &&
  208                     pvd2->pvd_vnode->v_mount == mp) {
  209                         vp = pvd2->pvd_vnode;
  210                         VI_LOCK(vp);
  211                         mtx_unlock(&pfs_vncache_mutex);
  212                         if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, curthread) == 0) {
  213                                 ++pfs_vncache_hits;
  214                                 vgone(*vpp);
  215                                 vput(*vpp);
  216                                 *vpp = vp;
  217                                 cache_purge(vp);
  218                                 return (0);
  219                         }
  220                         goto retry2;
  221                 }
  222         }
  223         ++pfs_vncache_misses;
  224         if (++pfs_vncache_entries > pfs_vncache_maxentries)
  225                 pfs_vncache_maxentries = pfs_vncache_entries;
  226         pvd->pvd_prev = NULL;
  227         pvd->pvd_next = pfs_vncache;
  228         if (pvd->pvd_next)
  229                 pvd->pvd_next->pvd_prev = pvd;
  230         pfs_vncache = pvd;
  231         mtx_unlock(&pfs_vncache_mutex);
  232         return (0);
  233 }
  234 
  235 /*
  236  * Free a vnode
  237  */
  238 int
  239 pfs_vncache_free(struct vnode *vp)
  240 {
  241         struct pfs_vdata *pvd;
  242 
  243         mtx_lock(&pfs_vncache_mutex);
  244         pvd = (struct pfs_vdata *)vp->v_data;
  245         KASSERT(pvd != NULL, ("pfs_vncache_free(): no vnode data\n"));
  246         if (pvd->pvd_next)
  247                 pvd->pvd_next->pvd_prev = pvd->pvd_prev;
  248         if (pvd->pvd_prev)
  249                 pvd->pvd_prev->pvd_next = pvd->pvd_next;
  250         else if (pfs_vncache == pvd)
  251                 pfs_vncache = pvd->pvd_next;
  252         --pfs_vncache_entries;
  253         mtx_unlock(&pfs_vncache_mutex);
  254 
  255         FREE(pvd, M_PFSVNCACHE);
  256         vp->v_data = NULL;
  257         return (0);
  258 }
  259 
  260 /*
  261  * Purge the cache of dead entries
  262  *
  263  * This is extremely inefficient due to the fact that vgone() not only
  264  * indirectly modifies the vnode cache, but may also sleep.  We can
  265  * neither hold pfs_vncache_mutex across a vgone() call, nor make any
  266  * assumptions about the state of the cache after vgone() returns.  In
  267  * consequence, we must start over after every vgone() call, and keep
  268  * trying until we manage to traverse the entire cache.
  269  *
  270  * The only way to improve this situation is to change the data structure
  271  * used to implement the cache.
  272  */
  273 void
  274 pfs_purge(struct pfs_node *pn)
  275 {
  276         struct pfs_vdata *pvd;
  277         struct vnode *vnp;
  278 
  279         mtx_lock(&pfs_vncache_mutex);
  280         pvd = pfs_vncache;
  281         while (pvd != NULL) {
  282                 if (pvd->pvd_dead || (pn != NULL && pvd->pvd_pn == pn)) {
  283                         vnp = pvd->pvd_vnode;
  284                         vhold(vnp);
  285                         mtx_unlock(&pfs_vncache_mutex);
  286                         VOP_LOCK(vnp, LK_EXCLUSIVE, curthread);
  287                         vgone(vnp);
  288                         VOP_UNLOCK(vnp, 0, curthread);
  289                         vdrop(vnp);
  290                         mtx_lock(&pfs_vncache_mutex);
  291                         pvd = pfs_vncache;
  292                 } else {
  293                         pvd = pvd->pvd_next;
  294                 }
  295         }
  296         mtx_unlock(&pfs_vncache_mutex);
  297 }
  298 
  299 /*
  300  * Free all vnodes associated with a defunct process
  301  *
  302  * XXXRW: It is unfortunate that pfs_exit() always acquires and releases two
  303  * mutexes (one of which is Giant) for every process exit, even if procfs
  304  * isn't mounted.
  305  */
  306 static void
  307 pfs_exit(void *arg, struct proc *p)
  308 {
  309         struct pfs_vdata *pvd;
  310         int dead;
  311 
  312         if (pfs_vncache == NULL)
  313                 return;
  314         mtx_lock(&Giant);
  315         mtx_lock(&pfs_vncache_mutex);
  316         for (pvd = pfs_vncache, dead = 0; pvd != NULL; pvd = pvd->pvd_next)
  317                 if (pvd->pvd_pid == p->p_pid)
  318                         dead = pvd->pvd_dead = 1;
  319         mtx_unlock(&pfs_vncache_mutex);
  320         if (dead)
  321                 pfs_purge(NULL);
  322         mtx_unlock(&Giant);
  323 }

Cache object: 328a6c948b541430d1354d6477706494


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