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/vfs_cache.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) 1989, 1993, 1995
    3  *      The Regents of the University of California.  All rights reserved.
    4  *
    5  * This code is derived from software contributed to Berkeley by
    6  * Poul-Henning Kamp of the FreeBSD Project.
    7  *
    8  * Redistribution and use in source and binary forms, with or without
    9  * modification, are permitted provided that the following conditions
   10  * are met:
   11  * 1. Redistributions of source code must retain the above copyright
   12  *    notice, this list of conditions and the following disclaimer.
   13  * 2. Redistributions in binary form must reproduce the above copyright
   14  *    notice, this list of conditions and the following disclaimer in the
   15  *    documentation and/or other materials provided with the distribution.
   16  * 4. Neither the name of the University nor the names of its contributors
   17  *    may be used to endorse or promote products derived from this software
   18  *    without specific prior written permission.
   19  *
   20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   30  * SUCH DAMAGE.
   31  *
   32  *      @(#)vfs_cache.c 8.5 (Berkeley) 3/22/95
   33  */
   34 
   35 #include <sys/cdefs.h>
   36 __FBSDID("$FreeBSD: releng/9.1/sys/kern/vfs_cache.c 233285 2012-03-21 20:50:15Z jhb $");
   37 
   38 #include "opt_kdtrace.h"
   39 #include "opt_ktrace.h"
   40 
   41 #include <sys/param.h>
   42 #include <sys/systm.h>
   43 #include <sys/filedesc.h>
   44 #include <sys/fnv_hash.h>
   45 #include <sys/kernel.h>
   46 #include <sys/lock.h>
   47 #include <sys/malloc.h>
   48 #include <sys/fcntl.h>
   49 #include <sys/mount.h>
   50 #include <sys/namei.h>
   51 #include <sys/proc.h>
   52 #include <sys/rwlock.h>
   53 #include <sys/sdt.h>
   54 #include <sys/syscallsubr.h>
   55 #include <sys/sysctl.h>
   56 #include <sys/sysproto.h>
   57 #include <sys/vnode.h>
   58 #ifdef KTRACE
   59 #include <sys/ktrace.h>
   60 #endif
   61 
   62 #include <vm/uma.h>
   63 
   64 SDT_PROVIDER_DECLARE(vfs);
   65 SDT_PROBE_DEFINE3(vfs, namecache, enter, done, done, "struct vnode *", "char *",
   66     "struct vnode *");
   67 SDT_PROBE_DEFINE2(vfs, namecache, enter_negative, done, done, "struct vnode *",
   68     "char *");
   69 SDT_PROBE_DEFINE1(vfs, namecache, fullpath, entry, entry, "struct vnode *");
   70 SDT_PROBE_DEFINE3(vfs, namecache, fullpath, hit, hit, "struct vnode *",
   71     "struct char *", "struct vnode *");
   72 SDT_PROBE_DEFINE1(vfs, namecache, fullpath, miss, miss, "struct vnode *");
   73 SDT_PROBE_DEFINE3(vfs, namecache, fullpath, return, return, "int",
   74     "struct vnode *", "struct char *");
   75 SDT_PROBE_DEFINE3(vfs, namecache, lookup, hit, hit, "struct vnode *", "char *",
   76     "struct vnode *");
   77 SDT_PROBE_DEFINE2(vfs, namecache, lookup, hit_negative, hit-negative,
   78     "struct vnode *", "char *");
   79 SDT_PROBE_DEFINE2(vfs, namecache, lookup, miss, miss, "struct vnode *",
   80     "char *");
   81 SDT_PROBE_DEFINE1(vfs, namecache, purge, done, done, "struct vnode *");
   82 SDT_PROBE_DEFINE1(vfs, namecache, purge_negative, done, done, "struct vnode *");
   83 SDT_PROBE_DEFINE1(vfs, namecache, purgevfs, done, done, "struct mount *");
   84 SDT_PROBE_DEFINE3(vfs, namecache, zap, done, done, "struct vnode *", "char *",
   85     "struct vnode *");
   86 SDT_PROBE_DEFINE2(vfs, namecache, zap_negative, done, done, "struct vnode *",
   87     "char *");
   88 
   89 /*
   90  * This structure describes the elements in the cache of recent
   91  * names looked up by namei.
   92  */
   93 
   94 struct  namecache {
   95         LIST_ENTRY(namecache) nc_hash;  /* hash chain */
   96         LIST_ENTRY(namecache) nc_src;   /* source vnode list */
   97         TAILQ_ENTRY(namecache) nc_dst;  /* destination vnode list */
   98         struct  vnode *nc_dvp;          /* vnode of parent of name */
   99         struct  vnode *nc_vp;           /* vnode the name refers to */
  100         u_char  nc_flag;                /* flag bits */
  101         u_char  nc_nlen;                /* length of name */
  102         char    nc_name[0];             /* segment name + nul */
  103 };
  104 
  105 /*
  106  * struct namecache_ts repeats struct namecache layout up to the
  107  * nc_nlen member.
  108  * struct namecache_ts is used in place of struct namecache when time(s) need
  109  * to be stored.  The nc_dotdottime field is used when a cache entry is mapping
  110  * both a non-dotdot directory name plus dotdot for the directory's
  111  * parent.
  112  */
  113 struct  namecache_ts {
  114         LIST_ENTRY(namecache) nc_hash;  /* hash chain */
  115         LIST_ENTRY(namecache) nc_src;   /* source vnode list */
  116         TAILQ_ENTRY(namecache) nc_dst;  /* destination vnode list */
  117         struct  vnode *nc_dvp;          /* vnode of parent of name */
  118         struct  vnode *nc_vp;           /* vnode the name refers to */
  119         u_char  nc_flag;                /* flag bits */
  120         u_char  nc_nlen;                /* length of name */
  121         struct  timespec nc_time;       /* timespec provided by fs */
  122         struct  timespec nc_dotdottime; /* dotdot timespec provided by fs */
  123         int     nc_ticks;               /* ticks value when entry was added */
  124         char    nc_name[0];             /* segment name + nul */
  125 };
  126 
  127 /*
  128  * Flags in namecache.nc_flag
  129  */
  130 #define NCF_WHITE       0x01
  131 #define NCF_ISDOTDOT    0x02
  132 #define NCF_TS          0x04
  133 #define NCF_DTS         0x08
  134 
  135 /*
  136  * Name caching works as follows:
  137  *
  138  * Names found by directory scans are retained in a cache
  139  * for future reference.  It is managed LRU, so frequently
  140  * used names will hang around.  Cache is indexed by hash value
  141  * obtained from (vp, name) where vp refers to the directory
  142  * containing name.
  143  *
  144  * If it is a "negative" entry, (i.e. for a name that is known NOT to
  145  * exist) the vnode pointer will be NULL.
  146  *
  147  * Upon reaching the last segment of a path, if the reference
  148  * is for DELETE, or NOCACHE is set (rewrite), and the
  149  * name is located in the cache, it will be dropped.
  150  */
  151 
  152 /*
  153  * Structures associated with name cacheing.
  154  */
  155 #define NCHHASH(hash) \
  156         (&nchashtbl[(hash) & nchash])
  157 static LIST_HEAD(nchashhead, namecache) *nchashtbl;     /* Hash Table */
  158 static TAILQ_HEAD(, namecache) ncneg;   /* Hash Table */
  159 static u_long   nchash;                 /* size of hash table */
  160 SYSCTL_ULONG(_debug, OID_AUTO, nchash, CTLFLAG_RD, &nchash, 0,
  161     "Size of namecache hash table");
  162 static u_long   ncnegfactor = 16;       /* ratio of negative entries */
  163 SYSCTL_ULONG(_vfs, OID_AUTO, ncnegfactor, CTLFLAG_RW, &ncnegfactor, 0,
  164     "Ratio of negative namecache entries");
  165 static u_long   numneg;                 /* number of negative entries allocated */
  166 SYSCTL_ULONG(_debug, OID_AUTO, numneg, CTLFLAG_RD, &numneg, 0,
  167     "Number of negative entries in namecache");
  168 static u_long   numcache;               /* number of cache entries allocated */
  169 SYSCTL_ULONG(_debug, OID_AUTO, numcache, CTLFLAG_RD, &numcache, 0,
  170     "Number of namecache entries");
  171 static u_long   numcachehv;             /* number of cache entries with vnodes held */
  172 SYSCTL_ULONG(_debug, OID_AUTO, numcachehv, CTLFLAG_RD, &numcachehv, 0,
  173     "Number of namecache entries with vnodes held");
  174 static u_int    ncsizefactor = 2;
  175 SYSCTL_UINT(_vfs, OID_AUTO, ncsizefactor, CTLFLAG_RW, &ncsizefactor, 0,
  176     "Size factor for namecache");
  177 
  178 struct nchstats nchstats;               /* cache effectiveness statistics */
  179 
  180 static struct rwlock cache_lock;
  181 RW_SYSINIT(vfscache, &cache_lock, "Name Cache");
  182 
  183 #define CACHE_UPGRADE_LOCK()    rw_try_upgrade(&cache_lock)
  184 #define CACHE_RLOCK()           rw_rlock(&cache_lock)
  185 #define CACHE_RUNLOCK()         rw_runlock(&cache_lock)
  186 #define CACHE_WLOCK()           rw_wlock(&cache_lock)
  187 #define CACHE_WUNLOCK()         rw_wunlock(&cache_lock)
  188 
  189 /*
  190  * UMA zones for the VFS cache.
  191  *
  192  * The small cache is used for entries with short names, which are the
  193  * most common.  The large cache is used for entries which are too big to
  194  * fit in the small cache.
  195  */
  196 static uma_zone_t cache_zone_small;
  197 static uma_zone_t cache_zone_small_ts;
  198 static uma_zone_t cache_zone_large;
  199 static uma_zone_t cache_zone_large_ts;
  200 
  201 #define CACHE_PATH_CUTOFF       35
  202 
  203 static struct namecache *
  204 cache_alloc(int len, int ts)
  205 {
  206 
  207         if (len > CACHE_PATH_CUTOFF) {
  208                 if (ts)
  209                         return (uma_zalloc(cache_zone_large_ts, M_WAITOK));
  210                 else
  211                         return (uma_zalloc(cache_zone_large, M_WAITOK));
  212         }
  213         if (ts)
  214                 return (uma_zalloc(cache_zone_small_ts, M_WAITOK));
  215         else
  216                 return (uma_zalloc(cache_zone_small, M_WAITOK));
  217 }
  218 
  219 static void
  220 cache_free(struct namecache *ncp)
  221 {
  222         int ts;
  223 
  224         if (ncp == NULL)
  225                 return;
  226         ts = ncp->nc_flag & NCF_TS;
  227         if (ncp->nc_nlen <= CACHE_PATH_CUTOFF) {
  228                 if (ts)
  229                         uma_zfree(cache_zone_small_ts, ncp);
  230                 else
  231                         uma_zfree(cache_zone_small, ncp);
  232         } else if (ts)
  233                 uma_zfree(cache_zone_large_ts, ncp);
  234         else
  235                 uma_zfree(cache_zone_large, ncp);
  236 }
  237 
  238 static char *
  239 nc_get_name(struct namecache *ncp)
  240 {
  241         struct namecache_ts *ncp_ts;
  242 
  243         if ((ncp->nc_flag & NCF_TS) == 0)
  244                 return (ncp->nc_name);
  245         ncp_ts = (struct namecache_ts *)ncp;
  246         return (ncp_ts->nc_name);
  247 }
  248 
  249 static void
  250 cache_out_ts(struct namecache *ncp, struct timespec *tsp, int *ticksp)
  251 {
  252 
  253         KASSERT((ncp->nc_flag & NCF_TS) != 0 ||
  254             (tsp == NULL && ticksp == NULL),
  255             ("No NCF_TS"));
  256 
  257         if (tsp != NULL)
  258                 *tsp = ((struct namecache_ts *)ncp)->nc_time;
  259         if (ticksp != NULL)
  260                 *ticksp = ((struct namecache_ts *)ncp)->nc_ticks;
  261 }
  262 
  263 static int      doingcache = 1;         /* 1 => enable the cache */
  264 SYSCTL_INT(_debug, OID_AUTO, vfscache, CTLFLAG_RW, &doingcache, 0,
  265     "VFS namecache enabled");
  266 
  267 /* Export size information to userland */
  268 SYSCTL_INT(_debug_sizeof, OID_AUTO, namecache, CTLFLAG_RD, 0,
  269     sizeof(struct namecache), "sizeof(struct namecache)");
  270 
  271 /*
  272  * The new name cache statistics
  273  */
  274 static SYSCTL_NODE(_vfs, OID_AUTO, cache, CTLFLAG_RW, 0,
  275     "Name cache statistics");
  276 #define STATNODE(mode, name, var, descr) \
  277         SYSCTL_ULONG(_vfs_cache, OID_AUTO, name, mode, var, 0, descr);
  278 STATNODE(CTLFLAG_RD, numneg, &numneg, "Number of negative cache entries");
  279 STATNODE(CTLFLAG_RD, numcache, &numcache, "Number of cache entries");
  280 static u_long numcalls; STATNODE(CTLFLAG_RD, numcalls, &numcalls,
  281     "Number of cache lookups");
  282 static u_long dothits; STATNODE(CTLFLAG_RD, dothits, &dothits,
  283     "Number of '.' hits");
  284 static u_long dotdothits; STATNODE(CTLFLAG_RD, dotdothits, &dotdothits,
  285     "Number of '..' hits");
  286 static u_long numchecks; STATNODE(CTLFLAG_RD, numchecks, &numchecks,
  287     "Number of checks in lookup");
  288 static u_long nummiss; STATNODE(CTLFLAG_RD, nummiss, &nummiss,
  289     "Number of cache misses");
  290 static u_long nummisszap; STATNODE(CTLFLAG_RD, nummisszap, &nummisszap,
  291     "Number of cache misses we do not want to cache");
  292 static u_long numposzaps; STATNODE(CTLFLAG_RD, numposzaps, &numposzaps, 
  293     "Number of cache hits (positive) we do not want to cache");
  294 static u_long numposhits; STATNODE(CTLFLAG_RD, numposhits, &numposhits,
  295     "Number of cache hits (positive)");
  296 static u_long numnegzaps; STATNODE(CTLFLAG_RD, numnegzaps, &numnegzaps,
  297     "Number of cache hits (negative) we do not want to cache");
  298 static u_long numneghits; STATNODE(CTLFLAG_RD, numneghits, &numneghits,
  299     "Number of cache hits (negative)");
  300 static u_long numupgrades; STATNODE(CTLFLAG_RD, numupgrades, &numupgrades,
  301     "Number of updates of the cache after lookup (write lock + retry)");
  302 
  303 SYSCTL_OPAQUE(_vfs_cache, OID_AUTO, nchstats, CTLFLAG_RD | CTLFLAG_MPSAFE,
  304     &nchstats, sizeof(nchstats), "LU",
  305     "VFS cache effectiveness statistics");
  306 
  307 
  308 
  309 static void cache_zap(struct namecache *ncp);
  310 static int vn_vptocnp_locked(struct vnode **vp, struct ucred *cred, char *buf,
  311     u_int *buflen);
  312 static int vn_fullpath1(struct thread *td, struct vnode *vp, struct vnode *rdir,
  313     char *buf, char **retbuf, u_int buflen);
  314 
  315 static MALLOC_DEFINE(M_VFSCACHE, "vfscache", "VFS name cache entries");
  316 
  317 #ifdef DIAGNOSTIC
  318 /*
  319  * Grab an atomic snapshot of the name cache hash chain lengths
  320  */
  321 SYSCTL_NODE(_debug, OID_AUTO, hashstat, CTLFLAG_RW, NULL, "hash table stats");
  322 
  323 static int
  324 sysctl_debug_hashstat_rawnchash(SYSCTL_HANDLER_ARGS)
  325 {
  326         int error;
  327         struct nchashhead *ncpp;
  328         struct namecache *ncp;
  329         int n_nchash;
  330         int count;
  331 
  332         n_nchash = nchash + 1;  /* nchash is max index, not count */
  333         if (!req->oldptr)
  334                 return SYSCTL_OUT(req, 0, n_nchash * sizeof(int));
  335 
  336         /* Scan hash tables for applicable entries */
  337         for (ncpp = nchashtbl; n_nchash > 0; n_nchash--, ncpp++) {
  338                 CACHE_RLOCK();
  339                 count = 0;
  340                 LIST_FOREACH(ncp, ncpp, nc_hash) {
  341                         count++;
  342                 }
  343                 CACHE_RUNLOCK();
  344                 error = SYSCTL_OUT(req, &count, sizeof(count));
  345                 if (error)
  346                         return (error);
  347         }
  348         return (0);
  349 }
  350 SYSCTL_PROC(_debug_hashstat, OID_AUTO, rawnchash, CTLTYPE_INT|CTLFLAG_RD|
  351     CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_rawnchash, "S,int",
  352     "nchash chain lengths");
  353 
  354 static int
  355 sysctl_debug_hashstat_nchash(SYSCTL_HANDLER_ARGS)
  356 {
  357         int error;
  358         struct nchashhead *ncpp;
  359         struct namecache *ncp;
  360         int n_nchash;
  361         int count, maxlength, used, pct;
  362 
  363         if (!req->oldptr)
  364                 return SYSCTL_OUT(req, 0, 4 * sizeof(int));
  365 
  366         n_nchash = nchash + 1;  /* nchash is max index, not count */
  367         used = 0;
  368         maxlength = 0;
  369 
  370         /* Scan hash tables for applicable entries */
  371         for (ncpp = nchashtbl; n_nchash > 0; n_nchash--, ncpp++) {
  372                 count = 0;
  373                 CACHE_RLOCK();
  374                 LIST_FOREACH(ncp, ncpp, nc_hash) {
  375                         count++;
  376                 }
  377                 CACHE_RUNLOCK();
  378                 if (count)
  379                         used++;
  380                 if (maxlength < count)
  381                         maxlength = count;
  382         }
  383         n_nchash = nchash + 1;
  384         pct = (used * 100 * 100) / n_nchash;
  385         error = SYSCTL_OUT(req, &n_nchash, sizeof(n_nchash));
  386         if (error)
  387                 return (error);
  388         error = SYSCTL_OUT(req, &used, sizeof(used));
  389         if (error)
  390                 return (error);
  391         error = SYSCTL_OUT(req, &maxlength, sizeof(maxlength));
  392         if (error)
  393                 return (error);
  394         error = SYSCTL_OUT(req, &pct, sizeof(pct));
  395         if (error)
  396                 return (error);
  397         return (0);
  398 }
  399 SYSCTL_PROC(_debug_hashstat, OID_AUTO, nchash, CTLTYPE_INT|CTLFLAG_RD|
  400     CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_nchash, "I",
  401     "nchash chain lengths");
  402 #endif
  403 
  404 /*
  405  * cache_zap():
  406  *
  407  *   Removes a namecache entry from cache, whether it contains an actual
  408  *   pointer to a vnode or if it is just a negative cache entry.
  409  */
  410 static void
  411 cache_zap(ncp)
  412         struct namecache *ncp;
  413 {
  414         struct vnode *vp;
  415 
  416         rw_assert(&cache_lock, RA_WLOCKED);
  417         CTR2(KTR_VFS, "cache_zap(%p) vp %p", ncp, ncp->nc_vp);
  418 #ifdef KDTRACE_HOOKS
  419         if (ncp->nc_vp != NULL) {
  420                 SDT_PROBE(vfs, namecache, zap, done, ncp->nc_dvp,
  421                     nc_get_name(ncp), ncp->nc_vp, 0, 0);
  422         } else {
  423                 SDT_PROBE(vfs, namecache, zap_negative, done, ncp->nc_dvp,
  424                     nc_get_name(ncp), 0, 0, 0);
  425         }
  426 #endif
  427         vp = NULL;
  428         LIST_REMOVE(ncp, nc_hash);
  429         if (ncp->nc_flag & NCF_ISDOTDOT) {
  430                 if (ncp == ncp->nc_dvp->v_cache_dd)
  431                         ncp->nc_dvp->v_cache_dd = NULL;
  432         } else {
  433                 LIST_REMOVE(ncp, nc_src);
  434                 if (LIST_EMPTY(&ncp->nc_dvp->v_cache_src)) {
  435                         vp = ncp->nc_dvp;
  436                         numcachehv--;
  437                 }
  438         }
  439         if (ncp->nc_vp) {
  440                 TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst, ncp, nc_dst);
  441                 if (ncp == ncp->nc_vp->v_cache_dd)
  442                         ncp->nc_vp->v_cache_dd = NULL;
  443         } else {
  444                 TAILQ_REMOVE(&ncneg, ncp, nc_dst);
  445                 numneg--;
  446         }
  447         numcache--;
  448         cache_free(ncp);
  449         if (vp)
  450                 vdrop(vp);
  451 }
  452 
  453 /*
  454  * Lookup an entry in the cache
  455  *
  456  * Lookup is called with dvp pointing to the directory to search,
  457  * cnp pointing to the name of the entry being sought. If the lookup
  458  * succeeds, the vnode is returned in *vpp, and a status of -1 is
  459  * returned. If the lookup determines that the name does not exist
  460  * (negative cacheing), a status of ENOENT is returned. If the lookup
  461  * fails, a status of zero is returned.  If the directory vnode is
  462  * recycled out from under us due to a forced unmount, a status of
  463  * ENOENT is returned.
  464  *
  465  * vpp is locked and ref'd on return.  If we're looking up DOTDOT, dvp is
  466  * unlocked.  If we're looking up . an extra ref is taken, but the lock is
  467  * not recursively acquired.
  468  */
  469 
  470 int
  471 cache_lookup_times(dvp, vpp, cnp, tsp, ticksp)
  472         struct vnode *dvp;
  473         struct vnode **vpp;
  474         struct componentname *cnp;
  475         struct timespec *tsp;
  476         int *ticksp;
  477 {
  478         struct namecache *ncp;
  479         uint32_t hash;
  480         int error, ltype, wlocked;
  481 
  482         if (!doingcache) {
  483                 cnp->cn_flags &= ~MAKEENTRY;
  484                 return (0);
  485         }
  486 retry:
  487         CACHE_RLOCK();
  488         wlocked = 0;
  489         numcalls++;
  490         error = 0;
  491 
  492 retry_wlocked:
  493         if (cnp->cn_nameptr[0] == '.') {
  494                 if (cnp->cn_namelen == 1) {
  495                         *vpp = dvp;
  496                         CTR2(KTR_VFS, "cache_lookup(%p, %s) found via .",
  497                             dvp, cnp->cn_nameptr);
  498                         dothits++;
  499                         SDT_PROBE(vfs, namecache, lookup, hit, dvp, ".",
  500                             *vpp, 0, 0);
  501                         if (tsp != NULL)
  502                                 timespecclear(tsp);
  503                         if (ticksp != NULL)
  504                                 *ticksp = ticks;
  505                         goto success;
  506                 }
  507                 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
  508                         dotdothits++;
  509                         if (dvp->v_cache_dd == NULL) {
  510                                 SDT_PROBE(vfs, namecache, lookup, miss, dvp,
  511                                     "..", NULL, 0, 0);
  512                                 goto unlock;
  513                         }
  514                         if ((cnp->cn_flags & MAKEENTRY) == 0) {
  515                                 if (!wlocked && !CACHE_UPGRADE_LOCK())
  516                                         goto wlock;
  517                                 if (dvp->v_cache_dd->nc_flag & NCF_ISDOTDOT)
  518                                         cache_zap(dvp->v_cache_dd);
  519                                 dvp->v_cache_dd = NULL;
  520                                 CACHE_WUNLOCK();
  521                                 return (0);
  522                         }
  523                         ncp = dvp->v_cache_dd;
  524                         if (ncp->nc_flag & NCF_ISDOTDOT)
  525                                 *vpp = ncp->nc_vp;
  526                         else
  527                                 *vpp = ncp->nc_dvp;
  528                         /* Return failure if negative entry was found. */
  529                         if (*vpp == NULL)
  530                                 goto negative_success;
  531                         CTR3(KTR_VFS, "cache_lookup(%p, %s) found %p via ..",
  532                             dvp, cnp->cn_nameptr, *vpp);
  533                         SDT_PROBE(vfs, namecache, lookup, hit, dvp, "..",
  534                             *vpp, 0, 0);
  535                         cache_out_ts(ncp, tsp, ticksp);
  536                         if ((ncp->nc_flag & (NCF_ISDOTDOT | NCF_DTS)) ==
  537                             NCF_DTS && tsp != NULL)
  538                                 *tsp = ((struct namecache_ts *)ncp)->
  539                                     nc_dotdottime;
  540                         goto success;
  541                 }
  542         }
  543 
  544         hash = fnv_32_buf(cnp->cn_nameptr, cnp->cn_namelen, FNV1_32_INIT);
  545         hash = fnv_32_buf(&dvp, sizeof(dvp), hash);
  546         LIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
  547                 numchecks++;
  548                 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
  549                     !bcmp(nc_get_name(ncp), cnp->cn_nameptr, ncp->nc_nlen))
  550                         break;
  551         }
  552 
  553         /* We failed to find an entry */
  554         if (ncp == NULL) {
  555                 SDT_PROBE(vfs, namecache, lookup, miss, dvp, cnp->cn_nameptr,
  556                     NULL, 0, 0);
  557                 if ((cnp->cn_flags & MAKEENTRY) == 0) {
  558                         nummisszap++;
  559                 } else {
  560                         nummiss++;
  561                 }
  562                 nchstats.ncs_miss++;
  563                 goto unlock;
  564         }
  565 
  566         /* We don't want to have an entry, so dump it */
  567         if ((cnp->cn_flags & MAKEENTRY) == 0) {
  568                 numposzaps++;
  569                 nchstats.ncs_badhits++;
  570                 if (!wlocked && !CACHE_UPGRADE_LOCK())
  571                         goto wlock;
  572                 cache_zap(ncp);
  573                 CACHE_WUNLOCK();
  574                 return (0);
  575         }
  576 
  577         /* We found a "positive" match, return the vnode */
  578         if (ncp->nc_vp) {
  579                 numposhits++;
  580                 nchstats.ncs_goodhits++;
  581                 *vpp = ncp->nc_vp;
  582                 CTR4(KTR_VFS, "cache_lookup(%p, %s) found %p via ncp %p",
  583                     dvp, cnp->cn_nameptr, *vpp, ncp);
  584                 SDT_PROBE(vfs, namecache, lookup, hit, dvp, nc_get_name(ncp),
  585                     *vpp, 0, 0);
  586                 cache_out_ts(ncp, tsp, ticksp);
  587                 goto success;
  588         }
  589 
  590 negative_success:
  591         /* We found a negative match, and want to create it, so purge */
  592         if (cnp->cn_nameiop == CREATE) {
  593                 numnegzaps++;
  594                 nchstats.ncs_badhits++;
  595                 if (!wlocked && !CACHE_UPGRADE_LOCK())
  596                         goto wlock;
  597                 cache_zap(ncp);
  598                 CACHE_WUNLOCK();
  599                 return (0);
  600         }
  601 
  602         if (!wlocked && !CACHE_UPGRADE_LOCK())
  603                 goto wlock;
  604         numneghits++;
  605         /*
  606          * We found a "negative" match, so we shift it to the end of
  607          * the "negative" cache entries queue to satisfy LRU.  Also,
  608          * check to see if the entry is a whiteout; indicate this to
  609          * the componentname, if so.
  610          */
  611         TAILQ_REMOVE(&ncneg, ncp, nc_dst);
  612         TAILQ_INSERT_TAIL(&ncneg, ncp, nc_dst);
  613         nchstats.ncs_neghits++;
  614         if (ncp->nc_flag & NCF_WHITE)
  615                 cnp->cn_flags |= ISWHITEOUT;
  616         SDT_PROBE(vfs, namecache, lookup, hit_negative, dvp, nc_get_name(ncp),
  617             0, 0, 0);
  618         cache_out_ts(ncp, tsp, ticksp);
  619         CACHE_WUNLOCK();
  620         return (ENOENT);
  621 
  622 wlock:
  623         /*
  624          * We need to update the cache after our lookup, so upgrade to
  625          * a write lock and retry the operation.
  626          */
  627         CACHE_RUNLOCK();
  628         CACHE_WLOCK();
  629         numupgrades++;
  630         wlocked = 1;
  631         goto retry_wlocked;
  632 
  633 success:
  634         /*
  635          * On success we return a locked and ref'd vnode as per the lookup
  636          * protocol.
  637          */
  638         if (dvp == *vpp) {   /* lookup on "." */
  639                 VREF(*vpp);
  640                 if (wlocked)
  641                         CACHE_WUNLOCK();
  642                 else
  643                         CACHE_RUNLOCK();
  644                 /*
  645                  * When we lookup "." we still can be asked to lock it
  646                  * differently...
  647                  */
  648                 ltype = cnp->cn_lkflags & LK_TYPE_MASK;
  649                 if (ltype != VOP_ISLOCKED(*vpp)) {
  650                         if (ltype == LK_EXCLUSIVE) {
  651                                 vn_lock(*vpp, LK_UPGRADE | LK_RETRY);
  652                                 if ((*vpp)->v_iflag & VI_DOOMED) {
  653                                         /* forced unmount */
  654                                         vrele(*vpp);
  655                                         *vpp = NULL;
  656                                         return (ENOENT);
  657                                 }
  658                         } else
  659                                 vn_lock(*vpp, LK_DOWNGRADE | LK_RETRY);
  660                 }
  661                 return (-1);
  662         }
  663         ltype = 0;      /* silence gcc warning */
  664         if (cnp->cn_flags & ISDOTDOT) {
  665                 ltype = VOP_ISLOCKED(dvp);
  666                 VOP_UNLOCK(dvp, 0);
  667         }
  668         VI_LOCK(*vpp);
  669         if (wlocked)
  670                 CACHE_WUNLOCK();
  671         else
  672                 CACHE_RUNLOCK();
  673         error = vget(*vpp, cnp->cn_lkflags | LK_INTERLOCK, cnp->cn_thread);
  674         if (cnp->cn_flags & ISDOTDOT) {
  675                 vn_lock(dvp, ltype | LK_RETRY);
  676                 if (dvp->v_iflag & VI_DOOMED) {
  677                         if (error == 0)
  678                                 vput(*vpp);
  679                         *vpp = NULL;
  680                         return (ENOENT);
  681                 }
  682         }
  683         if (error) {
  684                 *vpp = NULL;
  685                 goto retry;
  686         }
  687         if ((cnp->cn_flags & ISLASTCN) &&
  688             (cnp->cn_lkflags & LK_TYPE_MASK) == LK_EXCLUSIVE) {
  689                 ASSERT_VOP_ELOCKED(*vpp, "cache_lookup");
  690         }
  691         return (-1);
  692 
  693 unlock:
  694         if (wlocked)
  695                 CACHE_WUNLOCK();
  696         else
  697                 CACHE_RUNLOCK();
  698         return (0);
  699 }
  700 
  701 /*
  702  * Add an entry to the cache.
  703  */
  704 void
  705 cache_enter_time(dvp, vp, cnp, tsp, dtsp)
  706         struct vnode *dvp;
  707         struct vnode *vp;
  708         struct componentname *cnp;
  709         struct timespec *tsp;
  710         struct timespec *dtsp;
  711 {
  712         struct namecache *ncp, *n2;
  713         struct namecache_ts *n3;
  714         struct nchashhead *ncpp;
  715         uint32_t hash;
  716         int flag;
  717         int hold;
  718         int zap;
  719         int len;
  720 
  721         CTR3(KTR_VFS, "cache_enter(%p, %p, %s)", dvp, vp, cnp->cn_nameptr);
  722         VNASSERT(vp == NULL || (vp->v_iflag & VI_DOOMED) == 0, vp,
  723             ("cache_enter: Adding a doomed vnode"));
  724         VNASSERT(dvp == NULL || (dvp->v_iflag & VI_DOOMED) == 0, dvp,
  725             ("cache_enter: Doomed vnode used as src"));
  726 
  727         if (!doingcache)
  728                 return;
  729 
  730         /*
  731          * Avoid blowout in namecache entries.
  732          */
  733         if (numcache >= desiredvnodes * ncsizefactor)
  734                 return;
  735 
  736         flag = 0;
  737         if (cnp->cn_nameptr[0] == '.') {
  738                 if (cnp->cn_namelen == 1)
  739                         return;
  740                 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
  741                         CACHE_WLOCK();
  742                         /*
  743                          * If dotdot entry already exists, just retarget it
  744                          * to new parent vnode, otherwise continue with new
  745                          * namecache entry allocation.
  746                          */
  747                         if ((ncp = dvp->v_cache_dd) != NULL &&
  748                             ncp->nc_flag & NCF_ISDOTDOT) {
  749                                 KASSERT(ncp->nc_dvp == dvp,
  750                                     ("wrong isdotdot parent"));
  751                                 if (ncp->nc_vp != NULL)
  752                                         TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst,
  753                                             ncp, nc_dst);
  754                                 else
  755                                         TAILQ_REMOVE(&ncneg, ncp, nc_dst);
  756                                 if (vp != NULL)
  757                                         TAILQ_INSERT_HEAD(&vp->v_cache_dst,
  758                                             ncp, nc_dst);
  759                                 else
  760                                         TAILQ_INSERT_TAIL(&ncneg, ncp, nc_dst);
  761                                 ncp->nc_vp = vp;
  762                                 CACHE_WUNLOCK();
  763                                 return;
  764                         }
  765                         dvp->v_cache_dd = NULL;
  766                         SDT_PROBE(vfs, namecache, enter, done, dvp, "..", vp,
  767                             0, 0);
  768                         CACHE_WUNLOCK();
  769                         flag = NCF_ISDOTDOT;
  770                 }
  771         }
  772 
  773         hold = 0;
  774         zap = 0;
  775 
  776         /*
  777          * Calculate the hash key and setup as much of the new
  778          * namecache entry as possible before acquiring the lock.
  779          */
  780         ncp = cache_alloc(cnp->cn_namelen, tsp != NULL);
  781         ncp->nc_vp = vp;
  782         ncp->nc_dvp = dvp;
  783         ncp->nc_flag = flag;
  784         if (tsp != NULL) {
  785                 n3 = (struct namecache_ts *)ncp;
  786                 n3->nc_time = *tsp;
  787                 n3->nc_ticks = ticks;
  788                 n3->nc_flag |= NCF_TS;
  789                 if (dtsp != NULL) {
  790                         n3->nc_dotdottime = *dtsp;
  791                         n3->nc_flag |= NCF_DTS;
  792                 }
  793         }
  794         len = ncp->nc_nlen = cnp->cn_namelen;
  795         hash = fnv_32_buf(cnp->cn_nameptr, len, FNV1_32_INIT);
  796         strlcpy(nc_get_name(ncp), cnp->cn_nameptr, len + 1);
  797         hash = fnv_32_buf(&dvp, sizeof(dvp), hash);
  798         CACHE_WLOCK();
  799 
  800         /*
  801          * See if this vnode or negative entry is already in the cache
  802          * with this name.  This can happen with concurrent lookups of
  803          * the same path name.
  804          */
  805         ncpp = NCHHASH(hash);
  806         LIST_FOREACH(n2, ncpp, nc_hash) {
  807                 if (n2->nc_dvp == dvp &&
  808                     n2->nc_nlen == cnp->cn_namelen &&
  809                     !bcmp(nc_get_name(n2), cnp->cn_nameptr, n2->nc_nlen)) {
  810                         if (tsp != NULL) {
  811                                 KASSERT((n2->nc_flag & NCF_TS) != 0,
  812                                     ("no NCF_TS"));
  813                                 n3 = (struct namecache_ts *)n2;
  814                                 n3->nc_time =
  815                                     ((struct namecache_ts *)ncp)->nc_time;
  816                                 n3->nc_ticks =
  817                                     ((struct namecache_ts *)ncp)->nc_ticks;
  818                                 if (dtsp != NULL) {
  819                                         n3->nc_dotdottime =
  820                                             ((struct namecache_ts *)ncp)->
  821                                             nc_dotdottime;
  822                                         n3->nc_flag |= NCF_DTS;
  823                                 }
  824                         }
  825                         CACHE_WUNLOCK();
  826                         cache_free(ncp);
  827                         return;
  828                 }
  829         }
  830 
  831         if (flag == NCF_ISDOTDOT) {
  832                 /*
  833                  * See if we are trying to add .. entry, but some other lookup
  834                  * has populated v_cache_dd pointer already.
  835                  */
  836                 if (dvp->v_cache_dd != NULL) {
  837                     CACHE_WUNLOCK();
  838                     cache_free(ncp);
  839                     return;
  840                 }
  841                 KASSERT(vp == NULL || vp->v_type == VDIR,
  842                     ("wrong vnode type %p", vp));
  843                 dvp->v_cache_dd = ncp;
  844         }
  845 
  846         numcache++;
  847         if (!vp) {
  848                 numneg++;
  849                 if (cnp->cn_flags & ISWHITEOUT)
  850                         ncp->nc_flag |= NCF_WHITE;
  851         } else if (vp->v_type == VDIR) {
  852                 if (flag != NCF_ISDOTDOT) {
  853                         /*
  854                          * For this case, the cache entry maps both the
  855                          * directory name in it and the name ".." for the
  856                          * directory's parent.
  857                          */
  858                         if ((n2 = vp->v_cache_dd) != NULL &&
  859                             (n2->nc_flag & NCF_ISDOTDOT) != 0)
  860                                 cache_zap(n2);
  861                         vp->v_cache_dd = ncp;
  862                 }
  863         } else {
  864                 vp->v_cache_dd = NULL;
  865         }
  866 
  867         /*
  868          * Insert the new namecache entry into the appropriate chain
  869          * within the cache entries table.
  870          */
  871         LIST_INSERT_HEAD(ncpp, ncp, nc_hash);
  872         if (flag != NCF_ISDOTDOT) {
  873                 if (LIST_EMPTY(&dvp->v_cache_src)) {
  874                         hold = 1;
  875                         numcachehv++;
  876                 }
  877                 LIST_INSERT_HEAD(&dvp->v_cache_src, ncp, nc_src);
  878         }
  879 
  880         /*
  881          * If the entry is "negative", we place it into the
  882          * "negative" cache queue, otherwise, we place it into the
  883          * destination vnode's cache entries queue.
  884          */
  885         if (vp) {
  886                 TAILQ_INSERT_HEAD(&vp->v_cache_dst, ncp, nc_dst);
  887                 SDT_PROBE(vfs, namecache, enter, done, dvp, nc_get_name(ncp),
  888                     vp, 0, 0);
  889         } else {
  890                 TAILQ_INSERT_TAIL(&ncneg, ncp, nc_dst);
  891                 SDT_PROBE(vfs, namecache, enter_negative, done, dvp,
  892                     nc_get_name(ncp), 0, 0, 0);
  893         }
  894         if (numneg * ncnegfactor > numcache) {
  895                 ncp = TAILQ_FIRST(&ncneg);
  896                 zap = 1;
  897         }
  898         if (hold)
  899                 vhold(dvp);
  900         if (zap)
  901                 cache_zap(ncp);
  902         CACHE_WUNLOCK();
  903 }
  904 
  905 /*
  906  * Name cache initialization, from vfs_init() when we are booting
  907  */
  908 static void
  909 nchinit(void *dummy __unused)
  910 {
  911 
  912         TAILQ_INIT(&ncneg);
  913 
  914         cache_zone_small = uma_zcreate("S VFS Cache",
  915             sizeof(struct namecache) + CACHE_PATH_CUTOFF + 1,
  916             NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
  917         cache_zone_small_ts = uma_zcreate("STS VFS Cache",
  918             sizeof(struct namecache_ts) + CACHE_PATH_CUTOFF + 1,
  919             NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
  920         cache_zone_large = uma_zcreate("L VFS Cache",
  921             sizeof(struct namecache) + NAME_MAX + 1,
  922             NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
  923         cache_zone_large_ts = uma_zcreate("LTS VFS Cache",
  924             sizeof(struct namecache_ts) + NAME_MAX + 1,
  925             NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
  926 
  927         nchashtbl = hashinit(desiredvnodes * 2, M_VFSCACHE, &nchash);
  928 }
  929 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nchinit, NULL);
  930 
  931 
  932 /*
  933  * Invalidate all entries to a particular vnode.
  934  */
  935 void
  936 cache_purge(vp)
  937         struct vnode *vp;
  938 {
  939 
  940         CTR1(KTR_VFS, "cache_purge(%p)", vp);
  941         SDT_PROBE(vfs, namecache, purge, done, vp, 0, 0, 0, 0);
  942         CACHE_WLOCK();
  943         while (!LIST_EMPTY(&vp->v_cache_src))
  944                 cache_zap(LIST_FIRST(&vp->v_cache_src));
  945         while (!TAILQ_EMPTY(&vp->v_cache_dst))
  946                 cache_zap(TAILQ_FIRST(&vp->v_cache_dst));
  947         if (vp->v_cache_dd != NULL) {
  948                 KASSERT(vp->v_cache_dd->nc_flag & NCF_ISDOTDOT,
  949                    ("lost dotdot link"));
  950                 cache_zap(vp->v_cache_dd);
  951         }
  952         KASSERT(vp->v_cache_dd == NULL, ("incomplete purge"));
  953         CACHE_WUNLOCK();
  954 }
  955 
  956 /*
  957  * Invalidate all negative entries for a particular directory vnode.
  958  */
  959 void
  960 cache_purge_negative(vp)
  961         struct vnode *vp;
  962 {
  963         struct namecache *cp, *ncp;
  964 
  965         CTR1(KTR_VFS, "cache_purge_negative(%p)", vp);
  966         SDT_PROBE(vfs, namecache, purge_negative, done, vp, 0, 0, 0, 0);
  967         CACHE_WLOCK();
  968         LIST_FOREACH_SAFE(cp, &vp->v_cache_src, nc_src, ncp) {
  969                 if (cp->nc_vp == NULL)
  970                         cache_zap(cp);
  971         }
  972         CACHE_WUNLOCK();
  973 }
  974 
  975 /*
  976  * Flush all entries referencing a particular filesystem.
  977  */
  978 void
  979 cache_purgevfs(mp)
  980         struct mount *mp;
  981 {
  982         struct nchashhead *ncpp;
  983         struct namecache *ncp, *nnp;
  984 
  985         /* Scan hash tables for applicable entries */
  986         SDT_PROBE(vfs, namecache, purgevfs, done, mp, 0, 0, 0, 0);
  987         CACHE_WLOCK();
  988         for (ncpp = &nchashtbl[nchash]; ncpp >= nchashtbl; ncpp--) {
  989                 LIST_FOREACH_SAFE(ncp, ncpp, nc_hash, nnp) {
  990                         if (ncp->nc_dvp->v_mount == mp)
  991                                 cache_zap(ncp);
  992                 }
  993         }
  994         CACHE_WUNLOCK();
  995 }
  996 
  997 /*
  998  * Perform canonical checks and cache lookup and pass on to filesystem
  999  * through the vop_cachedlookup only if needed.
 1000  */
 1001 
 1002 int
 1003 vfs_cache_lookup(ap)
 1004         struct vop_lookup_args /* {
 1005                 struct vnode *a_dvp;
 1006                 struct vnode **a_vpp;
 1007                 struct componentname *a_cnp;
 1008         } */ *ap;
 1009 {
 1010         struct vnode *dvp;
 1011         int error;
 1012         struct vnode **vpp = ap->a_vpp;
 1013         struct componentname *cnp = ap->a_cnp;
 1014         struct ucred *cred = cnp->cn_cred;
 1015         int flags = cnp->cn_flags;
 1016         struct thread *td = cnp->cn_thread;
 1017 
 1018         *vpp = NULL;
 1019         dvp = ap->a_dvp;
 1020 
 1021         if (dvp->v_type != VDIR)
 1022                 return (ENOTDIR);
 1023 
 1024         if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
 1025             (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
 1026                 return (EROFS);
 1027 
 1028         error = VOP_ACCESS(dvp, VEXEC, cred, td);
 1029         if (error)
 1030                 return (error);
 1031 
 1032         error = cache_lookup(dvp, vpp, cnp);
 1033         if (error == 0)
 1034                 return (VOP_CACHEDLOOKUP(dvp, vpp, cnp));
 1035         if (error == -1)
 1036                 return (0);
 1037         return (error);
 1038 }
 1039 
 1040 
 1041 #ifndef _SYS_SYSPROTO_H_
 1042 struct  __getcwd_args {
 1043         u_char  *buf;
 1044         u_int   buflen;
 1045 };
 1046 #endif
 1047 
 1048 /*
 1049  * XXX All of these sysctls would probably be more productive dead.
 1050  */
 1051 static int disablecwd;
 1052 SYSCTL_INT(_debug, OID_AUTO, disablecwd, CTLFLAG_RW, &disablecwd, 0,
 1053    "Disable the getcwd syscall");
 1054 
 1055 /* Implementation of the getcwd syscall. */
 1056 int
 1057 sys___getcwd(td, uap)
 1058         struct thread *td;
 1059         struct __getcwd_args *uap;
 1060 {
 1061 
 1062         return (kern___getcwd(td, uap->buf, UIO_USERSPACE, uap->buflen));
 1063 }
 1064 
 1065 int
 1066 kern___getcwd(struct thread *td, u_char *buf, enum uio_seg bufseg, u_int buflen)
 1067 {
 1068         char *bp, *tmpbuf;
 1069         struct filedesc *fdp;
 1070         struct vnode *cdir, *rdir;
 1071         int error, vfslocked;
 1072 
 1073         if (disablecwd)
 1074                 return (ENODEV);
 1075         if (buflen < 2)
 1076                 return (EINVAL);
 1077         if (buflen > MAXPATHLEN)
 1078                 buflen = MAXPATHLEN;
 1079 
 1080         tmpbuf = malloc(buflen, M_TEMP, M_WAITOK);
 1081         fdp = td->td_proc->p_fd;
 1082         FILEDESC_SLOCK(fdp);
 1083         cdir = fdp->fd_cdir;
 1084         VREF(cdir);
 1085         rdir = fdp->fd_rdir;
 1086         VREF(rdir);
 1087         FILEDESC_SUNLOCK(fdp);
 1088         error = vn_fullpath1(td, cdir, rdir, tmpbuf, &bp, buflen);
 1089         vfslocked = VFS_LOCK_GIANT(rdir->v_mount);
 1090         vrele(rdir);
 1091         VFS_UNLOCK_GIANT(vfslocked);
 1092         vfslocked = VFS_LOCK_GIANT(cdir->v_mount);
 1093         vrele(cdir);
 1094         VFS_UNLOCK_GIANT(vfslocked);
 1095 
 1096         if (!error) {
 1097                 if (bufseg == UIO_SYSSPACE)
 1098                         bcopy(bp, buf, strlen(bp) + 1);
 1099                 else
 1100                         error = copyout(bp, buf, strlen(bp) + 1);
 1101 #ifdef KTRACE
 1102         if (KTRPOINT(curthread, KTR_NAMEI))
 1103                 ktrnamei(bp);
 1104 #endif
 1105         }
 1106         free(tmpbuf, M_TEMP);
 1107         return (error);
 1108 }
 1109 
 1110 /*
 1111  * Thus begins the fullpath magic.
 1112  */
 1113 
 1114 #undef STATNODE
 1115 #define STATNODE(name, descr)                                           \
 1116         static u_int name;                                              \
 1117         SYSCTL_UINT(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, 0, descr)
 1118 
 1119 static int disablefullpath;
 1120 SYSCTL_INT(_debug, OID_AUTO, disablefullpath, CTLFLAG_RW, &disablefullpath, 0,
 1121     "Disable the vn_fullpath function");
 1122 
 1123 /* These count for kern___getcwd(), too. */
 1124 STATNODE(numfullpathcalls, "Number of fullpath search calls");
 1125 STATNODE(numfullpathfail1, "Number of fullpath search errors (ENOTDIR)");
 1126 STATNODE(numfullpathfail2,
 1127     "Number of fullpath search errors (VOP_VPTOCNP failures)");
 1128 STATNODE(numfullpathfail4, "Number of fullpath search errors (ENOMEM)");
 1129 STATNODE(numfullpathfound, "Number of successful fullpath calls");
 1130 
 1131 /*
 1132  * Retrieve the full filesystem path that correspond to a vnode from the name
 1133  * cache (if available)
 1134  */
 1135 int
 1136 vn_fullpath(struct thread *td, struct vnode *vn, char **retbuf, char **freebuf)
 1137 {
 1138         char *buf;
 1139         struct filedesc *fdp;
 1140         struct vnode *rdir;
 1141         int error, vfslocked;
 1142 
 1143         if (disablefullpath)
 1144                 return (ENODEV);
 1145         if (vn == NULL)
 1146                 return (EINVAL);
 1147 
 1148         buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
 1149         fdp = td->td_proc->p_fd;
 1150         FILEDESC_SLOCK(fdp);
 1151         rdir = fdp->fd_rdir;
 1152         VREF(rdir);
 1153         FILEDESC_SUNLOCK(fdp);
 1154         error = vn_fullpath1(td, vn, rdir, buf, retbuf, MAXPATHLEN);
 1155         vfslocked = VFS_LOCK_GIANT(rdir->v_mount);
 1156         vrele(rdir);
 1157         VFS_UNLOCK_GIANT(vfslocked);
 1158 
 1159         if (!error)
 1160                 *freebuf = buf;
 1161         else
 1162                 free(buf, M_TEMP);
 1163         return (error);
 1164 }
 1165 
 1166 /*
 1167  * This function is similar to vn_fullpath, but it attempts to lookup the
 1168  * pathname relative to the global root mount point.  This is required for the
 1169  * auditing sub-system, as audited pathnames must be absolute, relative to the
 1170  * global root mount point.
 1171  */
 1172 int
 1173 vn_fullpath_global(struct thread *td, struct vnode *vn,
 1174     char **retbuf, char **freebuf)
 1175 {
 1176         char *buf;
 1177         int error;
 1178 
 1179         if (disablefullpath)
 1180                 return (ENODEV);
 1181         if (vn == NULL)
 1182                 return (EINVAL);
 1183         buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
 1184         error = vn_fullpath1(td, vn, rootvnode, buf, retbuf, MAXPATHLEN);
 1185         if (!error)
 1186                 *freebuf = buf;
 1187         else
 1188                 free(buf, M_TEMP);
 1189         return (error);
 1190 }
 1191 
 1192 int
 1193 vn_vptocnp(struct vnode **vp, struct ucred *cred, char *buf, u_int *buflen)
 1194 {
 1195         int error;
 1196 
 1197         CACHE_RLOCK();
 1198         error = vn_vptocnp_locked(vp, cred, buf, buflen);
 1199         if (error == 0)
 1200                 CACHE_RUNLOCK();
 1201         return (error);
 1202 }
 1203 
 1204 static int
 1205 vn_vptocnp_locked(struct vnode **vp, struct ucred *cred, char *buf,
 1206     u_int *buflen)
 1207 {
 1208         struct vnode *dvp;
 1209         struct namecache *ncp;
 1210         int error, vfslocked;
 1211 
 1212         TAILQ_FOREACH(ncp, &((*vp)->v_cache_dst), nc_dst) {
 1213                 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
 1214                         break;
 1215         }
 1216         if (ncp != NULL) {
 1217                 if (*buflen < ncp->nc_nlen) {
 1218                         CACHE_RUNLOCK();
 1219                         vfslocked = VFS_LOCK_GIANT((*vp)->v_mount);
 1220                         vrele(*vp);
 1221                         VFS_UNLOCK_GIANT(vfslocked);
 1222                         numfullpathfail4++;
 1223                         error = ENOMEM;
 1224                         SDT_PROBE(vfs, namecache, fullpath, return, error,
 1225                             vp, NULL, 0, 0);
 1226                         return (error);
 1227                 }
 1228                 *buflen -= ncp->nc_nlen;
 1229                 memcpy(buf + *buflen, nc_get_name(ncp), ncp->nc_nlen);
 1230                 SDT_PROBE(vfs, namecache, fullpath, hit, ncp->nc_dvp,
 1231                     nc_get_name(ncp), vp, 0, 0);
 1232                 dvp = *vp;
 1233                 *vp = ncp->nc_dvp;
 1234                 vref(*vp);
 1235                 CACHE_RUNLOCK();
 1236                 vfslocked = VFS_LOCK_GIANT(dvp->v_mount);
 1237                 vrele(dvp);
 1238                 VFS_UNLOCK_GIANT(vfslocked);
 1239                 CACHE_RLOCK();
 1240                 return (0);
 1241         }
 1242         SDT_PROBE(vfs, namecache, fullpath, miss, vp, 0, 0, 0, 0);
 1243 
 1244         CACHE_RUNLOCK();
 1245         vfslocked = VFS_LOCK_GIANT((*vp)->v_mount);
 1246         vn_lock(*vp, LK_SHARED | LK_RETRY);
 1247         error = VOP_VPTOCNP(*vp, &dvp, cred, buf, buflen);
 1248         vput(*vp);
 1249         VFS_UNLOCK_GIANT(vfslocked);
 1250         if (error) {
 1251                 numfullpathfail2++;
 1252                 SDT_PROBE(vfs, namecache, fullpath, return,  error, vp,
 1253                     NULL, 0, 0);
 1254                 return (error);
 1255         }
 1256 
 1257         *vp = dvp;
 1258         CACHE_RLOCK();
 1259         if (dvp->v_iflag & VI_DOOMED) {
 1260                 /* forced unmount */
 1261                 CACHE_RUNLOCK();
 1262                 vfslocked = VFS_LOCK_GIANT(dvp->v_mount);
 1263                 vrele(dvp);
 1264                 VFS_UNLOCK_GIANT(vfslocked);
 1265                 error = ENOENT;
 1266                 SDT_PROBE(vfs, namecache, fullpath, return, error, vp,
 1267                     NULL, 0, 0);
 1268                 return (error);
 1269         }
 1270         /*
 1271          * *vp has its use count incremented still.
 1272          */
 1273 
 1274         return (0);
 1275 }
 1276 
 1277 /*
 1278  * The magic behind kern___getcwd() and vn_fullpath().
 1279  */
 1280 static int
 1281 vn_fullpath1(struct thread *td, struct vnode *vp, struct vnode *rdir,
 1282     char *buf, char **retbuf, u_int buflen)
 1283 {
 1284         int error, slash_prefixed, vfslocked;
 1285 #ifdef KDTRACE_HOOKS
 1286         struct vnode *startvp = vp;
 1287 #endif
 1288         struct vnode *vp1;
 1289 
 1290         buflen--;
 1291         buf[buflen] = '\0';
 1292         error = 0;
 1293         slash_prefixed = 0;
 1294 
 1295         SDT_PROBE(vfs, namecache, fullpath, entry, vp, 0, 0, 0, 0);
 1296         numfullpathcalls++;
 1297         vref(vp);
 1298         CACHE_RLOCK();
 1299         if (vp->v_type != VDIR) {
 1300                 error = vn_vptocnp_locked(&vp, td->td_ucred, buf, &buflen);
 1301                 if (error)
 1302                         return (error);
 1303                 if (buflen == 0) {
 1304                         CACHE_RUNLOCK();
 1305                         vfslocked = VFS_LOCK_GIANT(vp->v_mount);
 1306                         vrele(vp);
 1307                         VFS_UNLOCK_GIANT(vfslocked);
 1308                         return (ENOMEM);
 1309                 }
 1310                 buf[--buflen] = '/';
 1311                 slash_prefixed = 1;
 1312         }
 1313         while (vp != rdir && vp != rootvnode) {
 1314                 if (vp->v_vflag & VV_ROOT) {
 1315                         if (vp->v_iflag & VI_DOOMED) {  /* forced unmount */
 1316                                 CACHE_RUNLOCK();
 1317                                 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
 1318                                 vrele(vp);
 1319                                 VFS_UNLOCK_GIANT(vfslocked);
 1320                                 error = ENOENT;
 1321                                 SDT_PROBE(vfs, namecache, fullpath, return,
 1322                                     error, vp, NULL, 0, 0);
 1323                                 break;
 1324                         }
 1325                         vp1 = vp->v_mount->mnt_vnodecovered;
 1326                         vref(vp1);
 1327                         CACHE_RUNLOCK();
 1328                         vfslocked = VFS_LOCK_GIANT(vp->v_mount);
 1329                         vrele(vp);
 1330                         VFS_UNLOCK_GIANT(vfslocked);
 1331                         vp = vp1;
 1332                         CACHE_RLOCK();
 1333                         continue;
 1334                 }
 1335                 if (vp->v_type != VDIR) {
 1336                         CACHE_RUNLOCK();
 1337                         vfslocked = VFS_LOCK_GIANT(vp->v_mount);
 1338                         vrele(vp);
 1339                         VFS_UNLOCK_GIANT(vfslocked);
 1340                         numfullpathfail1++;
 1341                         error = ENOTDIR;
 1342                         SDT_PROBE(vfs, namecache, fullpath, return,
 1343                             error, vp, NULL, 0, 0);
 1344                         break;
 1345                 }
 1346                 error = vn_vptocnp_locked(&vp, td->td_ucred, buf, &buflen);
 1347                 if (error)
 1348                         break;
 1349                 if (buflen == 0) {
 1350                         CACHE_RUNLOCK();
 1351                         vfslocked = VFS_LOCK_GIANT(vp->v_mount);
 1352                         vrele(vp);
 1353                         VFS_UNLOCK_GIANT(vfslocked);
 1354                         error = ENOMEM;
 1355                         SDT_PROBE(vfs, namecache, fullpath, return, error,
 1356                             startvp, NULL, 0, 0);
 1357                         break;
 1358                 }
 1359                 buf[--buflen] = '/';
 1360                 slash_prefixed = 1;
 1361         }
 1362         if (error)
 1363                 return (error);
 1364         if (!slash_prefixed) {
 1365                 if (buflen == 0) {
 1366                         CACHE_RUNLOCK();
 1367                         vfslocked = VFS_LOCK_GIANT(vp->v_mount);
 1368                         vrele(vp);
 1369                         VFS_UNLOCK_GIANT(vfslocked);
 1370                         numfullpathfail4++;
 1371                         SDT_PROBE(vfs, namecache, fullpath, return, ENOMEM,
 1372                             startvp, NULL, 0, 0);
 1373                         return (ENOMEM);
 1374                 }
 1375                 buf[--buflen] = '/';
 1376         }
 1377         numfullpathfound++;
 1378         CACHE_RUNLOCK();
 1379         vfslocked = VFS_LOCK_GIANT(vp->v_mount);
 1380         vrele(vp);
 1381         VFS_UNLOCK_GIANT(vfslocked);
 1382 
 1383         SDT_PROBE(vfs, namecache, fullpath, return, 0, startvp, buf + buflen,
 1384             0, 0);
 1385         *retbuf = buf + buflen;
 1386         return (0);
 1387 }
 1388 
 1389 int
 1390 vn_commname(struct vnode *vp, char *buf, u_int buflen)
 1391 {
 1392         struct namecache *ncp;
 1393         int l;
 1394 
 1395         CACHE_RLOCK();
 1396         TAILQ_FOREACH(ncp, &vp->v_cache_dst, nc_dst)
 1397                 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
 1398                         break;
 1399         if (ncp == NULL) {
 1400                 CACHE_RUNLOCK();
 1401                 return (ENOENT);
 1402         }
 1403         l = min(ncp->nc_nlen, buflen - 1);
 1404         memcpy(buf, nc_get_name(ncp), l);
 1405         CACHE_RUNLOCK();
 1406         buf[l] = '\0';
 1407         return (0);
 1408 }
 1409 
 1410 /* ABI compat shims for old kernel modules. */
 1411 #undef cache_enter
 1412 #undef cache_lookup
 1413 
 1414 void    cache_enter(struct vnode *dvp, struct vnode *vp,
 1415             struct componentname *cnp);
 1416 int     cache_lookup(struct vnode *dvp, struct vnode **vpp,
 1417             struct componentname *cnp);
 1418 
 1419 void
 1420 cache_enter(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
 1421 {
 1422 
 1423         cache_enter_time(dvp, vp, cnp, NULL, NULL);
 1424 }
 1425 
 1426 int
 1427 cache_lookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
 1428 {
 1429 
 1430         return (cache_lookup_times(dvp, vpp, cnp, NULL, NULL));
 1431 }
 1432 
 1433 /*
 1434  * This function updates path string to vnode's full global path
 1435  * and checks the size of the new path string against the pathlen argument.
 1436  *
 1437  * Requires a locked, referenced vnode and GIANT lock held.
 1438  * Vnode is re-locked on success or ENODEV, otherwise unlocked.
 1439  *
 1440  * If sysctl debug.disablefullpath is set, ENODEV is returned,
 1441  * vnode is left locked and path remain untouched.
 1442  *
 1443  * If vp is a directory, the call to vn_fullpath_global() always succeeds
 1444  * because it falls back to the ".." lookup if the namecache lookup fails.
 1445  */
 1446 int
 1447 vn_path_to_global_path(struct thread *td, struct vnode *vp, char *path,
 1448     u_int pathlen)
 1449 {
 1450         struct nameidata nd;
 1451         struct vnode *vp1;
 1452         char *rpath, *fbuf;
 1453         int error, vfslocked;
 1454 
 1455         VFS_ASSERT_GIANT(vp->v_mount);
 1456         ASSERT_VOP_ELOCKED(vp, __func__);
 1457 
 1458         /* Return ENODEV if sysctl debug.disablefullpath==1 */
 1459         if (disablefullpath)
 1460                 return (ENODEV);
 1461 
 1462         /* Construct global filesystem path from vp. */
 1463         VOP_UNLOCK(vp, 0);
 1464         error = vn_fullpath_global(td, vp, &rpath, &fbuf);
 1465 
 1466         if (error != 0) {
 1467                 vrele(vp);
 1468                 return (error);
 1469         }
 1470 
 1471         if (strlen(rpath) >= pathlen) {
 1472                 vrele(vp);
 1473                 error = ENAMETOOLONG;
 1474                 goto out;
 1475         }
 1476 
 1477         /*
 1478          * Re-lookup the vnode by path to detect a possible rename.
 1479          * As a side effect, the vnode is relocked.
 1480          * If vnode was renamed, return ENOENT.
 1481          */
 1482         NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
 1483             UIO_SYSSPACE, path, td);
 1484         error = namei(&nd);
 1485         if (error != 0) {
 1486                 vrele(vp);
 1487                 goto out;
 1488         }
 1489         vfslocked = NDHASGIANT(&nd);
 1490         NDFREE(&nd, NDF_ONLY_PNBUF);
 1491         vp1 = nd.ni_vp;
 1492         vrele(vp);
 1493         if (vp1 == vp)
 1494                 strcpy(path, rpath);
 1495         else {
 1496                 vput(vp1);
 1497                 error = ENOENT;
 1498         }
 1499         VFS_UNLOCK_GIANT(vfslocked);
 1500 
 1501 out:
 1502         free(fbuf, M_TEMP);
 1503         return (error);
 1504 }

Cache object: 8c7b02ef4b2f7e3b88205a6598628ba9


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