]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/vfs_cache.c
[mips] make UMTX_CHAINS configurable at compile time.
[FreeBSD/FreeBSD.git] / sys / kern / vfs_cache.c
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  * 3. 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$");
37
38 #include "opt_ktrace.h"
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/counter.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/smp.h>
55 #include <sys/syscallsubr.h>
56 #include <sys/sysctl.h>
57 #include <sys/sysproto.h>
58 #include <sys/vnode.h>
59 #ifdef KTRACE
60 #include <sys/ktrace.h>
61 #endif
62
63 #include <vm/uma.h>
64
65 SDT_PROVIDER_DECLARE(vfs);
66 SDT_PROBE_DEFINE3(vfs, namecache, enter, done, "struct vnode *", "char *",
67     "struct vnode *");
68 SDT_PROBE_DEFINE2(vfs, namecache, enter_negative, done, "struct vnode *",
69     "char *");
70 SDT_PROBE_DEFINE1(vfs, namecache, fullpath, entry, "struct vnode *");
71 SDT_PROBE_DEFINE3(vfs, namecache, fullpath, hit, "struct vnode *",
72     "char *", "struct vnode *");
73 SDT_PROBE_DEFINE1(vfs, namecache, fullpath, miss, "struct vnode *");
74 SDT_PROBE_DEFINE3(vfs, namecache, fullpath, return, "int",
75     "struct vnode *", "char *");
76 SDT_PROBE_DEFINE3(vfs, namecache, lookup, hit, "struct vnode *", "char *",
77     "struct vnode *");
78 SDT_PROBE_DEFINE2(vfs, namecache, lookup, hit__negative,
79     "struct vnode *", "char *");
80 SDT_PROBE_DEFINE2(vfs, namecache, lookup, miss, "struct vnode *",
81     "char *");
82 SDT_PROBE_DEFINE1(vfs, namecache, purge, done, "struct vnode *");
83 SDT_PROBE_DEFINE1(vfs, namecache, purge_negative, done, "struct vnode *");
84 SDT_PROBE_DEFINE1(vfs, namecache, purgevfs, done, "struct mount *");
85 SDT_PROBE_DEFINE3(vfs, namecache, zap, done, "struct vnode *", "char *",
86     "struct vnode *");
87 SDT_PROBE_DEFINE3(vfs, namecache, zap_negative, done, "struct vnode *",
88     "char *", "int");
89 SDT_PROBE_DEFINE3(vfs, namecache, shrink_negative, done, "struct vnode *",
90     "char *", "int");
91
92 /*
93  * This structure describes the elements in the cache of recent
94  * names looked up by namei.
95  */
96
97 struct  namecache {
98         LIST_ENTRY(namecache) nc_hash;  /* hash chain */
99         LIST_ENTRY(namecache) nc_src;   /* source vnode list */
100         TAILQ_ENTRY(namecache) nc_dst;  /* destination vnode list */
101         struct  vnode *nc_dvp;          /* vnode of parent of name */
102         union {
103                 struct  vnode *nu_vp;   /* vnode the name refers to */
104                 u_int   nu_neghits;     /* negative entry hits */
105         } n_un;
106         u_char  nc_flag;                /* flag bits */
107         u_char  nc_nlen;                /* length of name */
108         char    nc_name[0];             /* segment name + nul */
109 };
110
111 /*
112  * struct namecache_ts repeats struct namecache layout up to the
113  * nc_nlen member.
114  * struct namecache_ts is used in place of struct namecache when time(s) need
115  * to be stored.  The nc_dotdottime field is used when a cache entry is mapping
116  * both a non-dotdot directory name plus dotdot for the directory's
117  * parent.
118  */
119 struct  namecache_ts {
120         LIST_ENTRY(namecache) nc_hash;  /* hash chain */
121         LIST_ENTRY(namecache) nc_src;   /* source vnode list */
122         TAILQ_ENTRY(namecache) nc_dst;  /* destination vnode list */
123         struct  vnode *nc_dvp;          /* vnode of parent of name */
124         union {
125                 struct  vnode *nu_vp;   /* vnode the name refers to */
126                 u_int   nu_neghits;     /* negative entry hits */
127         } n_un;
128         u_char  nc_flag;                /* flag bits */
129         u_char  nc_nlen;                /* length of name */
130         struct  timespec nc_time;       /* timespec provided by fs */
131         struct  timespec nc_dotdottime; /* dotdot timespec provided by fs */
132         int     nc_ticks;               /* ticks value when entry was added */
133         char    nc_name[0];             /* segment name + nul */
134 };
135
136 #define nc_vp           n_un.nu_vp
137 #define nc_neghits      n_un.nu_neghits
138
139 /*
140  * Flags in namecache.nc_flag
141  */
142 #define NCF_WHITE       0x01
143 #define NCF_ISDOTDOT    0x02
144 #define NCF_TS          0x04
145 #define NCF_DTS         0x08
146 #define NCF_DVDROP      0x10
147 #define NCF_NEGATIVE    0x20
148 #define NCF_HOTNEGATIVE 0x40
149
150 /*
151  * Name caching works as follows:
152  *
153  * Names found by directory scans are retained in a cache
154  * for future reference.  It is managed LRU, so frequently
155  * used names will hang around.  Cache is indexed by hash value
156  * obtained from (vp, name) where vp refers to the directory
157  * containing name.
158  *
159  * If it is a "negative" entry, (i.e. for a name that is known NOT to
160  * exist) the vnode pointer will be NULL.
161  *
162  * Upon reaching the last segment of a path, if the reference
163  * is for DELETE, or NOCACHE is set (rewrite), and the
164  * name is located in the cache, it will be dropped.
165  *
166  * These locks are used (in the order in which they can be taken):
167  * NAME         TYPE    ROLE
168  * vnodelock    mtx     vnode lists and v_cache_dd field protection
169  * bucketlock   rwlock  for access to given set of hash buckets
170  * neglist      mtx     negative entry LRU management
171  *
172  * Additionally, ncneg_shrink_lock mtx is used to have at most one thread
173  * shrinking the LRU list.
174  *
175  * It is legal to take multiple vnodelock and bucketlock locks. The locking
176  * order is lower address first. Both are recursive.
177  *
178  * "." lookups are lockless.
179  *
180  * ".." and vnode -> name lookups require vnodelock.
181  *
182  * name -> vnode lookup requires the relevant bucketlock to be held for reading.
183  *
184  * Insertions and removals of entries require involved vnodes and bucketlocks
185  * to be write-locked to prevent other threads from seeing the entry.
186  *
187  * Some lookups result in removal of the found entry (e.g. getting rid of a
188  * negative entry with the intent to create a positive one), which poses a
189  * problem when multiple threads reach the state. Similarly, two different
190  * threads can purge two different vnodes and try to remove the same name.
191  *
192  * If the already held vnode lock is lower than the second required lock, we
193  * can just take the other lock. However, in the opposite case, this could
194  * deadlock. As such, this is resolved by trylocking and if that fails unlocking
195  * the first node, locking everything in order and revalidating the state.
196  */
197
198 /*
199  * Structures associated with name caching.
200  */
201 #define NCHHASH(hash) \
202         (&nchashtbl[(hash) & nchash])
203 static LIST_HEAD(nchashhead, namecache) *nchashtbl;     /* Hash Table */
204 static u_long   nchash;                 /* size of hash table */
205 SYSCTL_ULONG(_debug, OID_AUTO, nchash, CTLFLAG_RD, &nchash, 0,
206     "Size of namecache hash table");
207 static u_long   ncnegfactor = 16;       /* ratio of negative entries */
208 SYSCTL_ULONG(_vfs, OID_AUTO, ncnegfactor, CTLFLAG_RW, &ncnegfactor, 0,
209     "Ratio of negative namecache entries");
210 static u_long   numneg;                 /* number of negative entries allocated */
211 SYSCTL_ULONG(_debug, OID_AUTO, numneg, CTLFLAG_RD, &numneg, 0,
212     "Number of negative entries in namecache");
213 static u_long   numcache;               /* number of cache entries allocated */
214 SYSCTL_ULONG(_debug, OID_AUTO, numcache, CTLFLAG_RD, &numcache, 0,
215     "Number of namecache entries");
216 static u_long   numcachehv;             /* number of cache entries with vnodes held */
217 SYSCTL_ULONG(_debug, OID_AUTO, numcachehv, CTLFLAG_RD, &numcachehv, 0,
218     "Number of namecache entries with vnodes held");
219 u_int   ncsizefactor = 2;
220 SYSCTL_UINT(_vfs, OID_AUTO, ncsizefactor, CTLFLAG_RW, &ncsizefactor, 0,
221     "Size factor for namecache");
222 static u_int    ncpurgeminvnodes;
223 SYSCTL_UINT(_vfs, OID_AUTO, ncpurgeminvnodes, CTLFLAG_RW, &ncpurgeminvnodes, 0,
224     "Number of vnodes below which purgevfs ignores the request");
225 static u_int    ncneghitsrequeue = 8;
226 SYSCTL_UINT(_vfs, OID_AUTO, ncneghitsrequeue, CTLFLAG_RW, &ncneghitsrequeue, 0,
227     "Number of hits to requeue a negative entry in the LRU list");
228
229 struct nchstats nchstats;               /* cache effectiveness statistics */
230
231 static struct mtx       ncneg_shrink_lock;
232 MTX_SYSINIT(vfscache_shrink_neg, &ncneg_shrink_lock, "Name Cache shrink neg",
233     MTX_DEF);
234
235 struct neglist {
236         struct mtx              nl_lock;
237         TAILQ_HEAD(, namecache) nl_list;
238 } __aligned(CACHE_LINE_SIZE);
239
240 static struct neglist *neglists;
241 static struct neglist ncneg_hot;
242
243 static int      shrink_list_turn;
244
245 static u_int    numneglists;
246 static inline struct neglist *
247 NCP2NEGLIST(struct namecache *ncp)
248 {
249
250         return (&neglists[(((uintptr_t)(ncp) >> 8) % numneglists)]);
251 }
252
253 static u_int   numbucketlocks;
254 static struct rwlock_padalign  *bucketlocks;
255 #define HASH2BUCKETLOCK(hash) \
256         ((struct rwlock *)(&bucketlocks[((hash) % numbucketlocks)]))
257
258 static u_int   numvnodelocks;
259 static struct mtx *vnodelocks;
260 static inline struct mtx *
261 VP2VNODELOCK(struct vnode *vp)
262 {
263         struct mtx *vlp;
264
265         if (vp == NULL)
266                 return (NULL);
267         vlp = &vnodelocks[(((uintptr_t)(vp) >> 8) % numvnodelocks)];
268         return (vlp);
269 }
270
271 /*
272  * UMA zones for the VFS cache.
273  *
274  * The small cache is used for entries with short names, which are the
275  * most common.  The large cache is used for entries which are too big to
276  * fit in the small cache.
277  */
278 static uma_zone_t cache_zone_small;
279 static uma_zone_t cache_zone_small_ts;
280 static uma_zone_t cache_zone_large;
281 static uma_zone_t cache_zone_large_ts;
282
283 #define CACHE_PATH_CUTOFF       35
284
285 static struct namecache *
286 cache_alloc(int len, int ts)
287 {
288
289         if (len > CACHE_PATH_CUTOFF) {
290                 if (ts)
291                         return (uma_zalloc(cache_zone_large_ts, M_WAITOK));
292                 else
293                         return (uma_zalloc(cache_zone_large, M_WAITOK));
294         }
295         if (ts)
296                 return (uma_zalloc(cache_zone_small_ts, M_WAITOK));
297         else
298                 return (uma_zalloc(cache_zone_small, M_WAITOK));
299 }
300
301 static void
302 cache_free(struct namecache *ncp)
303 {
304         int ts;
305
306         if (ncp == NULL)
307                 return;
308         ts = ncp->nc_flag & NCF_TS;
309         if ((ncp->nc_flag & NCF_DVDROP) != 0)
310                 vdrop(ncp->nc_dvp);
311         if (ncp->nc_nlen <= CACHE_PATH_CUTOFF) {
312                 if (ts)
313                         uma_zfree(cache_zone_small_ts, ncp);
314                 else
315                         uma_zfree(cache_zone_small, ncp);
316         } else if (ts)
317                 uma_zfree(cache_zone_large_ts, ncp);
318         else
319                 uma_zfree(cache_zone_large, ncp);
320 }
321
322 static char *
323 nc_get_name(struct namecache *ncp)
324 {
325         struct namecache_ts *ncp_ts;
326
327         if ((ncp->nc_flag & NCF_TS) == 0)
328                 return (ncp->nc_name);
329         ncp_ts = (struct namecache_ts *)ncp;
330         return (ncp_ts->nc_name);
331 }
332
333 static void
334 cache_out_ts(struct namecache *ncp, struct timespec *tsp, int *ticksp)
335 {
336
337         KASSERT((ncp->nc_flag & NCF_TS) != 0 ||
338             (tsp == NULL && ticksp == NULL),
339             ("No NCF_TS"));
340
341         if (tsp != NULL)
342                 *tsp = ((struct namecache_ts *)ncp)->nc_time;
343         if (ticksp != NULL)
344                 *ticksp = ((struct namecache_ts *)ncp)->nc_ticks;
345 }
346
347 static int      doingcache = 1;         /* 1 => enable the cache */
348 SYSCTL_INT(_debug, OID_AUTO, vfscache, CTLFLAG_RW, &doingcache, 0,
349     "VFS namecache enabled");
350
351 /* Export size information to userland */
352 SYSCTL_INT(_debug_sizeof, OID_AUTO, namecache, CTLFLAG_RD, SYSCTL_NULL_INT_PTR,
353     sizeof(struct namecache), "sizeof(struct namecache)");
354
355 /*
356  * The new name cache statistics
357  */
358 static SYSCTL_NODE(_vfs, OID_AUTO, cache, CTLFLAG_RW, 0,
359     "Name cache statistics");
360 #define STATNODE_ULONG(name, descr)     \
361         SYSCTL_ULONG(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, 0, descr);
362 #define STATNODE_COUNTER(name, descr)   \
363         static counter_u64_t name;      \
364         SYSCTL_COUNTER_U64(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, descr);
365 STATNODE_ULONG(numneg, "Number of negative cache entries");
366 STATNODE_ULONG(numcache, "Number of cache entries");
367 STATNODE_COUNTER(numcalls, "Number of cache lookups");
368 STATNODE_COUNTER(dothits, "Number of '.' hits");
369 STATNODE_COUNTER(dotdothits, "Number of '..' hits");
370 STATNODE_COUNTER(numchecks, "Number of checks in lookup");
371 STATNODE_COUNTER(nummiss, "Number of cache misses");
372 STATNODE_COUNTER(nummisszap, "Number of cache misses we do not want to cache");
373 STATNODE_COUNTER(numposzaps,
374     "Number of cache hits (positive) we do not want to cache");
375 STATNODE_COUNTER(numposhits, "Number of cache hits (positive)");
376 STATNODE_COUNTER(numnegzaps,
377     "Number of cache hits (negative) we do not want to cache");
378 STATNODE_COUNTER(numneghits, "Number of cache hits (negative)");
379 /* These count for kern___getcwd(), too. */
380 STATNODE_COUNTER(numfullpathcalls, "Number of fullpath search calls");
381 STATNODE_COUNTER(numfullpathfail1, "Number of fullpath search errors (ENOTDIR)");
382 STATNODE_COUNTER(numfullpathfail2,
383     "Number of fullpath search errors (VOP_VPTOCNP failures)");
384 STATNODE_COUNTER(numfullpathfail4, "Number of fullpath search errors (ENOMEM)");
385 STATNODE_COUNTER(numfullpathfound, "Number of successful fullpath calls");
386 static long zap_and_exit_bucket_fail; STATNODE_ULONG(zap_and_exit_bucket_fail,
387     "Number of times zap_and_exit failed to lock");
388 static long cache_lock_vnodes_cel_3_failures;
389 STATNODE_ULONG(cache_lock_vnodes_cel_3_failures,
390     "Number of times 3-way vnode locking failed");
391
392 static void cache_zap_locked(struct namecache *ncp, bool neg_locked);
393 static int vn_fullpath1(struct thread *td, struct vnode *vp, struct vnode *rdir,
394     char *buf, char **retbuf, u_int buflen);
395
396 static MALLOC_DEFINE(M_VFSCACHE, "vfscache", "VFS name cache entries");
397
398 static int cache_yield;
399 SYSCTL_INT(_vfs_cache, OID_AUTO, yield, CTLFLAG_RD, &cache_yield, 0,
400     "Number of times cache called yield");
401
402 static void
403 cache_maybe_yield(void)
404 {
405
406         if (should_yield()) {
407                 cache_yield++;
408                 kern_yield(PRI_USER);
409         }
410 }
411
412 static inline void
413 cache_assert_vlp_locked(struct mtx *vlp)
414 {
415
416         if (vlp != NULL)
417                 mtx_assert(vlp, MA_OWNED);
418 }
419
420 static inline void
421 cache_assert_vnode_locked(struct vnode *vp)
422 {
423         struct mtx *vlp;
424
425         vlp = VP2VNODELOCK(vp);
426         cache_assert_vlp_locked(vlp);
427 }
428
429 static uint32_t
430 cache_get_hash(char *name, u_char len, struct vnode *dvp)
431 {
432         uint32_t hash;
433
434         hash = fnv_32_buf(name, len, FNV1_32_INIT);
435         hash = fnv_32_buf(&dvp, sizeof(dvp), hash);
436         return (hash);
437 }
438
439 static inline struct rwlock *
440 NCP2BUCKETLOCK(struct namecache *ncp)
441 {
442         uint32_t hash;
443
444         hash = cache_get_hash(nc_get_name(ncp), ncp->nc_nlen, ncp->nc_dvp);
445         return (HASH2BUCKETLOCK(hash));
446 }
447
448 #ifdef INVARIANTS
449 static void
450 cache_assert_bucket_locked(struct namecache *ncp, int mode)
451 {
452         struct rwlock *blp;
453
454         blp = NCP2BUCKETLOCK(ncp);
455         rw_assert(blp, mode);
456 }
457 #else
458 #define cache_assert_bucket_locked(x, y) do { } while (0)
459 #endif
460
461 #define cache_sort(x, y)        _cache_sort((void **)(x), (void **)(y))
462 static void
463 _cache_sort(void **p1, void **p2)
464 {
465         void *tmp;
466
467         if (*p1 > *p2) {
468                 tmp = *p2;
469                 *p2 = *p1;
470                 *p1 = tmp;
471         }
472 }
473
474 static void
475 cache_lock_all_buckets(void)
476 {
477         u_int i;
478
479         for (i = 0; i < numbucketlocks; i++)
480                 rw_wlock(&bucketlocks[i]);
481 }
482
483 static void
484 cache_unlock_all_buckets(void)
485 {
486         u_int i;
487
488         for (i = 0; i < numbucketlocks; i++)
489                 rw_wunlock(&bucketlocks[i]);
490 }
491
492 static void
493 cache_lock_all_vnodes(void)
494 {
495         u_int i;
496
497         for (i = 0; i < numvnodelocks; i++)
498                 mtx_lock(&vnodelocks[i]);
499 }
500
501 static void
502 cache_unlock_all_vnodes(void)
503 {
504         u_int i;
505
506         for (i = 0; i < numvnodelocks; i++)
507                 mtx_unlock(&vnodelocks[i]);
508 }
509
510 static int
511 cache_trylock_vnodes(struct mtx *vlp1, struct mtx *vlp2)
512 {
513
514         cache_sort(&vlp1, &vlp2);
515         MPASS(vlp2 != NULL);
516
517         if (vlp1 != NULL) {
518                 if (!mtx_trylock(vlp1))
519                         return (EAGAIN);
520         }
521         if (!mtx_trylock(vlp2)) {
522                 if (vlp1 != NULL)
523                         mtx_unlock(vlp1);
524                 return (EAGAIN);
525         }
526
527         return (0);
528 }
529
530 static void
531 cache_unlock_vnodes(struct mtx *vlp1, struct mtx *vlp2)
532 {
533
534         MPASS(vlp1 != NULL || vlp2 != NULL);
535
536         if (vlp1 != NULL)
537                 mtx_unlock(vlp1);
538         if (vlp2 != NULL)
539                 mtx_unlock(vlp2);
540 }
541
542 static int
543 sysctl_nchstats(SYSCTL_HANDLER_ARGS)
544 {
545         struct nchstats snap;
546
547         if (req->oldptr == NULL)
548                 return (SYSCTL_OUT(req, 0, sizeof(snap)));
549
550         snap = nchstats;
551         snap.ncs_goodhits = counter_u64_fetch(numposhits);
552         snap.ncs_neghits = counter_u64_fetch(numneghits);
553         snap.ncs_badhits = counter_u64_fetch(numposzaps) +
554             counter_u64_fetch(numnegzaps);
555         snap.ncs_miss = counter_u64_fetch(nummisszap) +
556             counter_u64_fetch(nummiss);
557
558         return (SYSCTL_OUT(req, &snap, sizeof(snap)));
559 }
560 SYSCTL_PROC(_vfs_cache, OID_AUTO, nchstats, CTLTYPE_OPAQUE | CTLFLAG_RD |
561     CTLFLAG_MPSAFE, 0, 0, sysctl_nchstats, "LU",
562     "VFS cache effectiveness statistics");
563
564 #ifdef DIAGNOSTIC
565 /*
566  * Grab an atomic snapshot of the name cache hash chain lengths
567  */
568 static SYSCTL_NODE(_debug, OID_AUTO, hashstat, CTLFLAG_RW, NULL,
569     "hash table stats");
570
571 static int
572 sysctl_debug_hashstat_rawnchash(SYSCTL_HANDLER_ARGS)
573 {
574         struct nchashhead *ncpp;
575         struct namecache *ncp;
576         int i, error, n_nchash, *cntbuf;
577
578 retry:
579         n_nchash = nchash + 1;  /* nchash is max index, not count */
580         if (req->oldptr == NULL)
581                 return SYSCTL_OUT(req, 0, n_nchash * sizeof(int));
582         cntbuf = malloc(n_nchash * sizeof(int), M_TEMP, M_ZERO | M_WAITOK);
583         cache_lock_all_buckets();
584         if (n_nchash != nchash + 1) {
585                 cache_unlock_all_buckets();
586                 free(cntbuf, M_TEMP);
587                 goto retry;
588         }
589         /* Scan hash tables counting entries */
590         for (ncpp = nchashtbl, i = 0; i < n_nchash; ncpp++, i++)
591                 LIST_FOREACH(ncp, ncpp, nc_hash)
592                         cntbuf[i]++;
593         cache_unlock_all_buckets();
594         for (error = 0, i = 0; i < n_nchash; i++)
595                 if ((error = SYSCTL_OUT(req, &cntbuf[i], sizeof(int))) != 0)
596                         break;
597         free(cntbuf, M_TEMP);
598         return (error);
599 }
600 SYSCTL_PROC(_debug_hashstat, OID_AUTO, rawnchash, CTLTYPE_INT|CTLFLAG_RD|
601     CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_rawnchash, "S,int",
602     "nchash chain lengths");
603
604 static int
605 sysctl_debug_hashstat_nchash(SYSCTL_HANDLER_ARGS)
606 {
607         int error;
608         struct nchashhead *ncpp;
609         struct namecache *ncp;
610         int n_nchash;
611         int count, maxlength, used, pct;
612
613         if (!req->oldptr)
614                 return SYSCTL_OUT(req, 0, 4 * sizeof(int));
615
616         cache_lock_all_buckets();
617         n_nchash = nchash + 1;  /* nchash is max index, not count */
618         used = 0;
619         maxlength = 0;
620
621         /* Scan hash tables for applicable entries */
622         for (ncpp = nchashtbl; n_nchash > 0; n_nchash--, ncpp++) {
623                 count = 0;
624                 LIST_FOREACH(ncp, ncpp, nc_hash) {
625                         count++;
626                 }
627                 if (count)
628                         used++;
629                 if (maxlength < count)
630                         maxlength = count;
631         }
632         n_nchash = nchash + 1;
633         cache_unlock_all_buckets();
634         pct = (used * 100) / (n_nchash / 100);
635         error = SYSCTL_OUT(req, &n_nchash, sizeof(n_nchash));
636         if (error)
637                 return (error);
638         error = SYSCTL_OUT(req, &used, sizeof(used));
639         if (error)
640                 return (error);
641         error = SYSCTL_OUT(req, &maxlength, sizeof(maxlength));
642         if (error)
643                 return (error);
644         error = SYSCTL_OUT(req, &pct, sizeof(pct));
645         if (error)
646                 return (error);
647         return (0);
648 }
649 SYSCTL_PROC(_debug_hashstat, OID_AUTO, nchash, CTLTYPE_INT|CTLFLAG_RD|
650     CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_nchash, "I",
651     "nchash statistics (number of total/used buckets, maximum chain length, usage percentage)");
652 #endif
653
654 /*
655  * Negative entries management
656  *
657  * A variation of LRU scheme is used. New entries are hashed into one of
658  * numneglists cold lists. Entries get promoted to the hot list on first hit.
659  * Partial LRU for the hot list is maintained by requeueing them every
660  * ncneghitsrequeue hits.
661  *
662  * The shrinker will demote hot list head and evict from the cold list in a
663  * round-robin manner.
664  */
665 static void
666 cache_negative_hit(struct namecache *ncp)
667 {
668         struct neglist *neglist;
669         u_int hits;
670
671         MPASS(ncp->nc_flag & NCF_NEGATIVE);
672         hits = atomic_fetchadd_int(&ncp->nc_neghits, 1);
673         if (ncp->nc_flag & NCF_HOTNEGATIVE) {
674                 if ((hits % ncneghitsrequeue) != 0)
675                         return;
676                 mtx_lock(&ncneg_hot.nl_lock);
677                 if (ncp->nc_flag & NCF_HOTNEGATIVE) {
678                         TAILQ_REMOVE(&ncneg_hot.nl_list, ncp, nc_dst);
679                         TAILQ_INSERT_TAIL(&ncneg_hot.nl_list, ncp, nc_dst);
680                         mtx_unlock(&ncneg_hot.nl_lock);
681                         return;
682                 }
683                 /*
684                  * The shrinker cleared the flag and removed the entry from
685                  * the hot list. Put it back.
686                  */
687         } else {
688                 mtx_lock(&ncneg_hot.nl_lock);
689         }
690         neglist = NCP2NEGLIST(ncp);
691         mtx_lock(&neglist->nl_lock);
692         if (!(ncp->nc_flag & NCF_HOTNEGATIVE)) {
693                 TAILQ_REMOVE(&neglist->nl_list, ncp, nc_dst);
694                 TAILQ_INSERT_TAIL(&ncneg_hot.nl_list, ncp, nc_dst);
695                 ncp->nc_flag |= NCF_HOTNEGATIVE;
696         }
697         mtx_unlock(&neglist->nl_lock);
698         mtx_unlock(&ncneg_hot.nl_lock);
699 }
700
701 static void
702 cache_negative_insert(struct namecache *ncp, bool neg_locked)
703 {
704         struct neglist *neglist;
705
706         MPASS(ncp->nc_flag & NCF_NEGATIVE);
707         cache_assert_bucket_locked(ncp, RA_WLOCKED);
708         neglist = NCP2NEGLIST(ncp);
709         if (!neg_locked) {
710                 mtx_lock(&neglist->nl_lock);
711         } else {
712                 mtx_assert(&neglist->nl_lock, MA_OWNED);
713         }
714         TAILQ_INSERT_TAIL(&neglist->nl_list, ncp, nc_dst);
715         if (!neg_locked)
716                 mtx_unlock(&neglist->nl_lock);
717         atomic_add_rel_long(&numneg, 1);
718 }
719
720 static void
721 cache_negative_remove(struct namecache *ncp, bool neg_locked)
722 {
723         struct neglist *neglist;
724         bool hot_locked = false;
725         bool list_locked = false;
726
727         MPASS(ncp->nc_flag & NCF_NEGATIVE);
728         cache_assert_bucket_locked(ncp, RA_WLOCKED);
729         neglist = NCP2NEGLIST(ncp);
730         if (!neg_locked) {
731                 if (ncp->nc_flag & NCF_HOTNEGATIVE) {
732                         hot_locked = true;
733                         mtx_lock(&ncneg_hot.nl_lock);
734                         if (!(ncp->nc_flag & NCF_HOTNEGATIVE)) {
735                                 list_locked = true;
736                                 mtx_lock(&neglist->nl_lock);
737                         }
738                 } else {
739                         list_locked = true;
740                         mtx_lock(&neglist->nl_lock);
741                 }
742         } else {
743                 mtx_assert(&neglist->nl_lock, MA_OWNED);
744                 mtx_assert(&ncneg_hot.nl_lock, MA_OWNED);
745         }
746         if (ncp->nc_flag & NCF_HOTNEGATIVE) {
747                 TAILQ_REMOVE(&ncneg_hot.nl_list, ncp, nc_dst);
748         } else {
749                 TAILQ_REMOVE(&neglist->nl_list, ncp, nc_dst);
750         }
751         if (list_locked)
752                 mtx_unlock(&neglist->nl_lock);
753         if (hot_locked)
754                 mtx_unlock(&ncneg_hot.nl_lock);
755         atomic_subtract_rel_long(&numneg, 1);
756 }
757
758 static void
759 cache_negative_shrink_select(int start, struct namecache **ncpp,
760     struct neglist **neglistpp)
761 {
762         struct neglist *neglist;
763         struct namecache *ncp;
764         int i;
765
766         *ncpp = ncp = NULL;
767
768         for (i = start; i < numneglists; i++) {
769                 neglist = &neglists[i];
770                 if (TAILQ_FIRST(&neglist->nl_list) == NULL)
771                         continue;
772                 mtx_lock(&neglist->nl_lock);
773                 ncp = TAILQ_FIRST(&neglist->nl_list);
774                 if (ncp != NULL)
775                         break;
776                 mtx_unlock(&neglist->nl_lock);
777         }
778
779         *neglistpp = neglist;
780         *ncpp = ncp;
781 }
782
783 static void
784 cache_negative_zap_one(void)
785 {
786         struct namecache *ncp, *ncp2, *ncpc;
787         struct neglist *neglist;
788         struct mtx *dvlp;
789         struct rwlock *blp;
790
791         if (!mtx_trylock(&ncneg_shrink_lock))
792                 return;
793
794         ncpc = NULL;
795         mtx_lock(&ncneg_hot.nl_lock);
796         ncp = TAILQ_FIRST(&ncneg_hot.nl_list);
797         if (ncp != NULL) {
798                 neglist = NCP2NEGLIST(ncp);
799                 mtx_lock(&neglist->nl_lock);
800                 TAILQ_REMOVE(&ncneg_hot.nl_list, ncp, nc_dst);
801                 TAILQ_INSERT_TAIL(&neglist->nl_list, ncp, nc_dst);
802                 ncp->nc_flag &= ~NCF_HOTNEGATIVE;
803                 mtx_unlock(&neglist->nl_lock);
804         }
805
806         cache_negative_shrink_select(shrink_list_turn, &ncp, &neglist);
807         shrink_list_turn++;
808         if (shrink_list_turn == numneglists)
809                 shrink_list_turn = 0;
810         if (ncp == NULL && shrink_list_turn == 0)
811                 cache_negative_shrink_select(shrink_list_turn, &ncp, &neglist);
812         if (ncp == NULL) {
813                 mtx_unlock(&ncneg_hot.nl_lock);
814                 goto out;
815         }
816
817         MPASS(ncp->nc_flag & NCF_NEGATIVE);
818         dvlp = VP2VNODELOCK(ncp->nc_dvp);
819         blp = NCP2BUCKETLOCK(ncp);
820         mtx_unlock(&neglist->nl_lock);
821         mtx_unlock(&ncneg_hot.nl_lock);
822         mtx_lock(dvlp);
823         rw_wlock(blp);
824         mtx_lock(&ncneg_hot.nl_lock);
825         mtx_lock(&neglist->nl_lock);
826         ncp2 = TAILQ_FIRST(&neglist->nl_list);
827         if (ncp != ncp2 || dvlp != VP2VNODELOCK(ncp2->nc_dvp) ||
828             blp != NCP2BUCKETLOCK(ncp2) || !(ncp2->nc_flag & NCF_NEGATIVE)) {
829                 ncp = NULL;
830                 goto out_unlock_all;
831         }
832         SDT_PROBE3(vfs, namecache, shrink_negative, done, ncp->nc_dvp,
833             nc_get_name(ncp), ncp->nc_neghits);
834
835         cache_zap_locked(ncp, true);
836 out_unlock_all:
837         mtx_unlock(&neglist->nl_lock);
838         mtx_unlock(&ncneg_hot.nl_lock);
839         rw_wunlock(blp);
840         mtx_unlock(dvlp);
841 out:
842         mtx_unlock(&ncneg_shrink_lock);
843         cache_free(ncp);
844 }
845
846 /*
847  * cache_zap_locked():
848  *
849  *   Removes a namecache entry from cache, whether it contains an actual
850  *   pointer to a vnode or if it is just a negative cache entry.
851  */
852 static void
853 cache_zap_locked(struct namecache *ncp, bool neg_locked)
854 {
855
856         if (!(ncp->nc_flag & NCF_NEGATIVE))
857                 cache_assert_vnode_locked(ncp->nc_vp);
858         cache_assert_vnode_locked(ncp->nc_dvp);
859         cache_assert_bucket_locked(ncp, RA_WLOCKED);
860
861         CTR2(KTR_VFS, "cache_zap(%p) vp %p", ncp,
862             (ncp->nc_flag & NCF_NEGATIVE) ? NULL : ncp->nc_vp);
863         if (!(ncp->nc_flag & NCF_NEGATIVE)) {
864                 SDT_PROBE3(vfs, namecache, zap, done, ncp->nc_dvp,
865                     nc_get_name(ncp), ncp->nc_vp);
866         } else {
867                 SDT_PROBE3(vfs, namecache, zap_negative, done, ncp->nc_dvp,
868                     nc_get_name(ncp), ncp->nc_neghits);
869         }
870         LIST_REMOVE(ncp, nc_hash);
871         if (ncp->nc_flag & NCF_ISDOTDOT) {
872                 if (ncp == ncp->nc_dvp->v_cache_dd)
873                         ncp->nc_dvp->v_cache_dd = NULL;
874         } else {
875                 LIST_REMOVE(ncp, nc_src);
876                 if (LIST_EMPTY(&ncp->nc_dvp->v_cache_src)) {
877                         ncp->nc_flag |= NCF_DVDROP;
878                         atomic_subtract_rel_long(&numcachehv, 1);
879                 }
880         }
881         if (!(ncp->nc_flag & NCF_NEGATIVE)) {
882                 TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst, ncp, nc_dst);
883                 if (ncp == ncp->nc_vp->v_cache_dd)
884                         ncp->nc_vp->v_cache_dd = NULL;
885         } else {
886                 cache_negative_remove(ncp, neg_locked);
887         }
888         atomic_subtract_rel_long(&numcache, 1);
889 }
890
891 static void
892 cache_zap_negative_locked_vnode_kl(struct namecache *ncp, struct vnode *vp)
893 {
894         struct rwlock *blp;
895
896         MPASS(ncp->nc_dvp == vp);
897         MPASS(ncp->nc_flag & NCF_NEGATIVE);
898         cache_assert_vnode_locked(vp);
899
900         blp = NCP2BUCKETLOCK(ncp);
901         rw_wlock(blp);
902         cache_zap_locked(ncp, false);
903         rw_wunlock(blp);
904 }
905
906 static bool
907 cache_zap_locked_vnode_kl2(struct namecache *ncp, struct vnode *vp,
908     struct mtx **vlpp)
909 {
910         struct mtx *pvlp, *vlp1, *vlp2, *to_unlock;
911         struct rwlock *blp;
912
913         MPASS(vp == ncp->nc_dvp || vp == ncp->nc_vp);
914         cache_assert_vnode_locked(vp);
915
916         if (ncp->nc_flag & NCF_NEGATIVE) {
917                 if (*vlpp != NULL) {
918                         mtx_unlock(*vlpp);
919                         *vlpp = NULL;
920                 }
921                 cache_zap_negative_locked_vnode_kl(ncp, vp);
922                 return (true);
923         }
924
925         pvlp = VP2VNODELOCK(vp);
926         blp = NCP2BUCKETLOCK(ncp);
927         vlp1 = VP2VNODELOCK(ncp->nc_dvp);
928         vlp2 = VP2VNODELOCK(ncp->nc_vp);
929
930         if (*vlpp == vlp1 || *vlpp == vlp2) {
931                 to_unlock = *vlpp;
932                 *vlpp = NULL;
933         } else {
934                 if (*vlpp != NULL) {
935                         mtx_unlock(*vlpp);
936                         *vlpp = NULL;
937                 }
938                 cache_sort(&vlp1, &vlp2);
939                 if (vlp1 == pvlp) {
940                         mtx_lock(vlp2);
941                         to_unlock = vlp2;
942                 } else {
943                         if (!mtx_trylock(vlp1))
944                                 goto out_relock;
945                         to_unlock = vlp1;
946                 }
947         }
948         rw_wlock(blp);
949         cache_zap_locked(ncp, false);
950         rw_wunlock(blp);
951         if (to_unlock != NULL)
952                 mtx_unlock(to_unlock);
953         return (true);
954
955 out_relock:
956         mtx_unlock(vlp2);
957         mtx_lock(vlp1);
958         mtx_lock(vlp2);
959         MPASS(*vlpp == NULL);
960         *vlpp = vlp1;
961         return (false);
962 }
963
964 static int
965 cache_zap_locked_vnode(struct namecache *ncp, struct vnode *vp)
966 {
967         struct mtx *pvlp, *vlp1, *vlp2, *to_unlock;
968         struct rwlock *blp;
969         int error = 0;
970
971         MPASS(vp == ncp->nc_dvp || vp == ncp->nc_vp);
972         cache_assert_vnode_locked(vp);
973
974         pvlp = VP2VNODELOCK(vp);
975         if (ncp->nc_flag & NCF_NEGATIVE) {
976                 cache_zap_negative_locked_vnode_kl(ncp, vp);
977                 goto out;
978         }
979
980         blp = NCP2BUCKETLOCK(ncp);
981         vlp1 = VP2VNODELOCK(ncp->nc_dvp);
982         vlp2 = VP2VNODELOCK(ncp->nc_vp);
983         cache_sort(&vlp1, &vlp2);
984         if (vlp1 == pvlp) {
985                 mtx_lock(vlp2);
986                 to_unlock = vlp2;
987         } else {
988                 if (!mtx_trylock(vlp1)) {
989                         error = EAGAIN;
990                         goto out;
991                 }
992                 to_unlock = vlp1;
993         }
994         rw_wlock(blp);
995         cache_zap_locked(ncp, false);
996         rw_wunlock(blp);
997         mtx_unlock(to_unlock);
998 out:
999         mtx_unlock(pvlp);
1000         return (error);
1001 }
1002
1003 static int
1004 cache_zap_rlocked_bucket(struct namecache *ncp, struct rwlock *blp)
1005 {
1006         struct mtx *dvlp, *vlp;
1007
1008         cache_assert_bucket_locked(ncp, RA_RLOCKED);
1009
1010         dvlp = VP2VNODELOCK(ncp->nc_dvp);
1011         vlp = NULL;
1012         if (!(ncp->nc_flag & NCF_NEGATIVE))
1013                 vlp = VP2VNODELOCK(ncp->nc_vp);
1014         if (cache_trylock_vnodes(dvlp, vlp) == 0) {
1015                 rw_runlock(blp);
1016                 rw_wlock(blp);
1017                 cache_zap_locked(ncp, false);
1018                 rw_wunlock(blp);
1019                 cache_unlock_vnodes(dvlp, vlp);
1020                 return (0);
1021         }
1022
1023         rw_runlock(blp);
1024         return (EAGAIN);
1025 }
1026
1027 static int
1028 cache_zap_wlocked_bucket_kl(struct namecache *ncp, struct rwlock *blp,
1029     struct mtx **vlpp1, struct mtx **vlpp2)
1030 {
1031         struct mtx *dvlp, *vlp;
1032
1033         cache_assert_bucket_locked(ncp, RA_WLOCKED);
1034
1035         dvlp = VP2VNODELOCK(ncp->nc_dvp);
1036         vlp = NULL;
1037         if (!(ncp->nc_flag & NCF_NEGATIVE))
1038                 vlp = VP2VNODELOCK(ncp->nc_vp);
1039         cache_sort(&dvlp, &vlp);
1040
1041         if (*vlpp1 == dvlp && *vlpp2 == vlp) {
1042                 cache_zap_locked(ncp, false);
1043                 cache_unlock_vnodes(dvlp, vlp);
1044                 *vlpp1 = NULL;
1045                 *vlpp2 = NULL;
1046                 return (0);
1047         }
1048
1049         if (*vlpp1 != NULL)
1050                 mtx_unlock(*vlpp1);
1051         if (*vlpp2 != NULL)
1052                 mtx_unlock(*vlpp2);
1053         *vlpp1 = NULL;
1054         *vlpp2 = NULL;
1055
1056         if (cache_trylock_vnodes(dvlp, vlp) == 0) {
1057                 cache_zap_locked(ncp, false);
1058                 cache_unlock_vnodes(dvlp, vlp);
1059                 return (0);
1060         }
1061
1062         rw_wunlock(blp);
1063         *vlpp1 = dvlp;
1064         *vlpp2 = vlp;
1065         if (*vlpp1 != NULL)
1066                 mtx_lock(*vlpp1);
1067         mtx_lock(*vlpp2);
1068         rw_wlock(blp);
1069         return (EAGAIN);
1070 }
1071
1072 static void
1073 cache_lookup_unlock(struct rwlock *blp, struct mtx *vlp)
1074 {
1075
1076         if (blp != NULL) {
1077                 rw_runlock(blp);
1078                 mtx_assert(vlp, MA_NOTOWNED);
1079         } else {
1080                 mtx_unlock(vlp);
1081         }
1082 }
1083
1084 /*
1085  * Lookup an entry in the cache
1086  *
1087  * Lookup is called with dvp pointing to the directory to search,
1088  * cnp pointing to the name of the entry being sought. If the lookup
1089  * succeeds, the vnode is returned in *vpp, and a status of -1 is
1090  * returned. If the lookup determines that the name does not exist
1091  * (negative caching), a status of ENOENT is returned. If the lookup
1092  * fails, a status of zero is returned.  If the directory vnode is
1093  * recycled out from under us due to a forced unmount, a status of
1094  * ENOENT is returned.
1095  *
1096  * vpp is locked and ref'd on return.  If we're looking up DOTDOT, dvp is
1097  * unlocked.  If we're looking up . an extra ref is taken, but the lock is
1098  * not recursively acquired.
1099  */
1100
1101 int
1102 cache_lookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1103     struct timespec *tsp, int *ticksp)
1104 {
1105         struct namecache *ncp;
1106         struct rwlock *blp;
1107         struct mtx *dvlp, *dvlp2;
1108         uint32_t hash;
1109         int error, ltype;
1110
1111         if (!doingcache) {
1112                 cnp->cn_flags &= ~MAKEENTRY;
1113                 return (0);
1114         }
1115 retry:
1116         blp = NULL;
1117         dvlp = VP2VNODELOCK(dvp);
1118         error = 0;
1119         counter_u64_add(numcalls, 1);
1120
1121         if (cnp->cn_nameptr[0] == '.') {
1122                 if (cnp->cn_namelen == 1) {
1123                         *vpp = dvp;
1124                         CTR2(KTR_VFS, "cache_lookup(%p, %s) found via .",
1125                             dvp, cnp->cn_nameptr);
1126                         counter_u64_add(dothits, 1);
1127                         SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ".", *vpp);
1128                         if (tsp != NULL)
1129                                 timespecclear(tsp);
1130                         if (ticksp != NULL)
1131                                 *ticksp = ticks;
1132                         VREF(*vpp);
1133                         /*
1134                          * When we lookup "." we still can be asked to lock it
1135                          * differently...
1136                          */
1137                         ltype = cnp->cn_lkflags & LK_TYPE_MASK;
1138                         if (ltype != VOP_ISLOCKED(*vpp)) {
1139                                 if (ltype == LK_EXCLUSIVE) {
1140                                         vn_lock(*vpp, LK_UPGRADE | LK_RETRY);
1141                                         if ((*vpp)->v_iflag & VI_DOOMED) {
1142                                                 /* forced unmount */
1143                                                 vrele(*vpp);
1144                                                 *vpp = NULL;
1145                                                 return (ENOENT);
1146                                         }
1147                                 } else
1148                                         vn_lock(*vpp, LK_DOWNGRADE | LK_RETRY);
1149                         }
1150                         return (-1);
1151                 }
1152                 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
1153                         counter_u64_add(dotdothits, 1);
1154                         dvlp2 = NULL;
1155                         mtx_lock(dvlp);
1156 retry_dotdot:
1157                         ncp = dvp->v_cache_dd;
1158                         if (ncp == NULL) {
1159                                 SDT_PROBE3(vfs, namecache, lookup, miss, dvp,
1160                                     "..", NULL);
1161                                 mtx_unlock(dvlp);
1162                                 return (0);
1163                         }
1164                         if ((cnp->cn_flags & MAKEENTRY) == 0) {
1165                                 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0) {
1166                                         if (ncp->nc_dvp != dvp)
1167                                                 panic("dvp %p v_cache_dd %p\n", dvp, ncp);
1168                                         if (!cache_zap_locked_vnode_kl2(ncp,
1169                                             dvp, &dvlp2))
1170                                                 goto retry_dotdot;
1171                                         MPASS(dvp->v_cache_dd == NULL);
1172                                         mtx_unlock(dvlp);
1173                                         if (dvlp2 != NULL)
1174                                                 mtx_unlock(dvlp2);
1175                                         cache_free(ncp);
1176                                 } else {
1177                                         dvp->v_cache_dd = NULL;
1178                                         mtx_unlock(dvlp);
1179                                         if (dvlp2 != NULL)
1180                                                 mtx_unlock(dvlp2);
1181                                 }
1182                                 return (0);
1183                         }
1184                         if ((ncp->nc_flag & NCF_ISDOTDOT) != 0) {
1185                                 if (ncp->nc_flag & NCF_NEGATIVE)
1186                                         *vpp = NULL;
1187                                 else
1188                                         *vpp = ncp->nc_vp;
1189                         } else
1190                                 *vpp = ncp->nc_dvp;
1191                         /* Return failure if negative entry was found. */
1192                         if (*vpp == NULL)
1193                                 goto negative_success;
1194                         CTR3(KTR_VFS, "cache_lookup(%p, %s) found %p via ..",
1195                             dvp, cnp->cn_nameptr, *vpp);
1196                         SDT_PROBE3(vfs, namecache, lookup, hit, dvp, "..",
1197                             *vpp);
1198                         cache_out_ts(ncp, tsp, ticksp);
1199                         if ((ncp->nc_flag & (NCF_ISDOTDOT | NCF_DTS)) ==
1200                             NCF_DTS && tsp != NULL)
1201                                 *tsp = ((struct namecache_ts *)ncp)->
1202                                     nc_dotdottime;
1203                         goto success;
1204                 }
1205         }
1206
1207         hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp);
1208         blp = HASH2BUCKETLOCK(hash);
1209         rw_rlock(blp);
1210
1211         LIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
1212                 counter_u64_add(numchecks, 1);
1213                 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
1214                     !bcmp(nc_get_name(ncp), cnp->cn_nameptr, ncp->nc_nlen))
1215                         break;
1216         }
1217
1218         /* We failed to find an entry */
1219         if (ncp == NULL) {
1220                 SDT_PROBE3(vfs, namecache, lookup, miss, dvp, cnp->cn_nameptr,
1221                     NULL);
1222                 if ((cnp->cn_flags & MAKEENTRY) == 0) {
1223                         counter_u64_add(nummisszap, 1);
1224                 } else {
1225                         counter_u64_add(nummiss, 1);
1226                 }
1227                 goto unlock;
1228         }
1229
1230         /* We don't want to have an entry, so dump it */
1231         if ((cnp->cn_flags & MAKEENTRY) == 0) {
1232                 counter_u64_add(numposzaps, 1);
1233                 goto zap_and_exit;
1234         }
1235
1236         /* We found a "positive" match, return the vnode */
1237         if (!(ncp->nc_flag & NCF_NEGATIVE)) {
1238                 counter_u64_add(numposhits, 1);
1239                 *vpp = ncp->nc_vp;
1240                 CTR4(KTR_VFS, "cache_lookup(%p, %s) found %p via ncp %p",
1241                     dvp, cnp->cn_nameptr, *vpp, ncp);
1242                 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, nc_get_name(ncp),
1243                     *vpp);
1244                 cache_out_ts(ncp, tsp, ticksp);
1245                 goto success;
1246         }
1247
1248 negative_success:
1249         /* We found a negative match, and want to create it, so purge */
1250         if (cnp->cn_nameiop == CREATE) {
1251                 counter_u64_add(numnegzaps, 1);
1252                 goto zap_and_exit;
1253         }
1254
1255         counter_u64_add(numneghits, 1);
1256         cache_negative_hit(ncp);
1257         if (ncp->nc_flag & NCF_WHITE)
1258                 cnp->cn_flags |= ISWHITEOUT;
1259         SDT_PROBE2(vfs, namecache, lookup, hit__negative, dvp,
1260             nc_get_name(ncp));
1261         cache_out_ts(ncp, tsp, ticksp);
1262         cache_lookup_unlock(blp, dvlp);
1263         return (ENOENT);
1264
1265 success:
1266         /*
1267          * On success we return a locked and ref'd vnode as per the lookup
1268          * protocol.
1269          */
1270         MPASS(dvp != *vpp);
1271         ltype = 0;      /* silence gcc warning */
1272         if (cnp->cn_flags & ISDOTDOT) {
1273                 ltype = VOP_ISLOCKED(dvp);
1274                 VOP_UNLOCK(dvp, 0);
1275         }
1276         vhold(*vpp);
1277         cache_lookup_unlock(blp, dvlp);
1278         error = vget(*vpp, cnp->cn_lkflags | LK_VNHELD, cnp->cn_thread);
1279         if (cnp->cn_flags & ISDOTDOT) {
1280                 vn_lock(dvp, ltype | LK_RETRY);
1281                 if (dvp->v_iflag & VI_DOOMED) {
1282                         if (error == 0)
1283                                 vput(*vpp);
1284                         *vpp = NULL;
1285                         return (ENOENT);
1286                 }
1287         }
1288         if (error) {
1289                 *vpp = NULL;
1290                 goto retry;
1291         }
1292         if ((cnp->cn_flags & ISLASTCN) &&
1293             (cnp->cn_lkflags & LK_TYPE_MASK) == LK_EXCLUSIVE) {
1294                 ASSERT_VOP_ELOCKED(*vpp, "cache_lookup");
1295         }
1296         return (-1);
1297
1298 unlock:
1299         cache_lookup_unlock(blp, dvlp);
1300         return (0);
1301
1302 zap_and_exit:
1303         if (blp != NULL)
1304                 error = cache_zap_rlocked_bucket(ncp, blp);
1305         else
1306                 error = cache_zap_locked_vnode(ncp, dvp);
1307         if (error != 0) {
1308                 zap_and_exit_bucket_fail++;
1309                 cache_maybe_yield();
1310                 goto retry;
1311         }
1312         cache_free(ncp);
1313         return (0);
1314 }
1315
1316 struct celockstate {
1317         struct mtx *vlp[3];
1318         struct rwlock *blp[2];
1319 };
1320 CTASSERT((nitems(((struct celockstate *)0)->vlp) == 3));
1321 CTASSERT((nitems(((struct celockstate *)0)->blp) == 2));
1322
1323 static inline void
1324 cache_celockstate_init(struct celockstate *cel)
1325 {
1326
1327         bzero(cel, sizeof(*cel));
1328 }
1329
1330 static void
1331 cache_lock_vnodes_cel(struct celockstate *cel, struct vnode *vp,
1332     struct vnode *dvp)
1333 {
1334         struct mtx *vlp1, *vlp2;
1335
1336         MPASS(cel->vlp[0] == NULL);
1337         MPASS(cel->vlp[1] == NULL);
1338         MPASS(cel->vlp[2] == NULL);
1339
1340         MPASS(vp != NULL || dvp != NULL);
1341
1342         vlp1 = VP2VNODELOCK(vp);
1343         vlp2 = VP2VNODELOCK(dvp);
1344         cache_sort(&vlp1, &vlp2);
1345
1346         if (vlp1 != NULL) {
1347                 mtx_lock(vlp1);
1348                 cel->vlp[0] = vlp1;
1349         }
1350         mtx_lock(vlp2);
1351         cel->vlp[1] = vlp2;
1352 }
1353
1354 static void
1355 cache_unlock_vnodes_cel(struct celockstate *cel)
1356 {
1357
1358         MPASS(cel->vlp[0] != NULL || cel->vlp[1] != NULL);
1359
1360         if (cel->vlp[0] != NULL)
1361                 mtx_unlock(cel->vlp[0]);
1362         if (cel->vlp[1] != NULL)
1363                 mtx_unlock(cel->vlp[1]);
1364         if (cel->vlp[2] != NULL)
1365                 mtx_unlock(cel->vlp[2]);
1366 }
1367
1368 static bool
1369 cache_lock_vnodes_cel_3(struct celockstate *cel, struct vnode *vp)
1370 {
1371         struct mtx *vlp;
1372         bool ret;
1373
1374         cache_assert_vlp_locked(cel->vlp[0]);
1375         cache_assert_vlp_locked(cel->vlp[1]);
1376         MPASS(cel->vlp[2] == NULL);
1377
1378         vlp = VP2VNODELOCK(vp);
1379         MPASS(vlp != NULL);
1380
1381         ret = true;
1382         if (vlp >= cel->vlp[1]) {
1383                 mtx_lock(vlp);
1384         } else {
1385                 if (mtx_trylock(vlp))
1386                         goto out;
1387                 cache_lock_vnodes_cel_3_failures++;
1388                 cache_unlock_vnodes_cel(cel);
1389                 if (vlp < cel->vlp[0]) {
1390                         mtx_lock(vlp);
1391                         mtx_lock(cel->vlp[0]);
1392                         mtx_lock(cel->vlp[1]);
1393                 } else {
1394                         if (cel->vlp[0] != NULL)
1395                                 mtx_lock(cel->vlp[0]);
1396                         mtx_lock(vlp);
1397                         mtx_lock(cel->vlp[1]);
1398                 }
1399                 ret = false;
1400         }
1401 out:
1402         cel->vlp[2] = vlp;
1403         return (ret);
1404 }
1405
1406 static void
1407 cache_lock_buckets_cel(struct celockstate *cel, struct rwlock *blp1,
1408     struct rwlock *blp2)
1409 {
1410
1411         MPASS(cel->blp[0] == NULL);
1412         MPASS(cel->blp[1] == NULL);
1413
1414         cache_sort(&blp1, &blp2);
1415
1416         if (blp1 != NULL) {
1417                 rw_wlock(blp1);
1418                 cel->blp[0] = blp1;
1419         }
1420         rw_wlock(blp2);
1421         cel->blp[1] = blp2;
1422 }
1423
1424 static void
1425 cache_unlock_buckets_cel(struct celockstate *cel)
1426 {
1427
1428         if (cel->blp[0] != NULL)
1429                 rw_wunlock(cel->blp[0]);
1430         rw_wunlock(cel->blp[1]);
1431 }
1432
1433 /*
1434  * Lock part of the cache affected by the insertion.
1435  *
1436  * This means vnodelocks for dvp, vp and the relevant bucketlock.
1437  * However, insertion can result in removal of an old entry. In this
1438  * case we have an additional vnode and bucketlock pair to lock. If the
1439  * entry is negative, ncelock is locked instead of the vnode.
1440  *
1441  * That is, in the worst case we have to lock 3 vnodes and 2 bucketlocks, while
1442  * preserving the locking order (smaller address first).
1443  */
1444 static void
1445 cache_enter_lock(struct celockstate *cel, struct vnode *dvp, struct vnode *vp,
1446     uint32_t hash)
1447 {
1448         struct namecache *ncp;
1449         struct rwlock *blps[2];
1450
1451         blps[0] = HASH2BUCKETLOCK(hash);
1452         for (;;) {
1453                 blps[1] = NULL;
1454                 cache_lock_vnodes_cel(cel, dvp, vp);
1455                 if (vp == NULL || vp->v_type != VDIR)
1456                         break;
1457                 ncp = vp->v_cache_dd;
1458                 if (ncp == NULL)
1459                         break;
1460                 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1461                         break;
1462                 MPASS(ncp->nc_dvp == vp);
1463                 blps[1] = NCP2BUCKETLOCK(ncp);
1464                 if (ncp->nc_flag & NCF_NEGATIVE)
1465                         break;
1466                 if (cache_lock_vnodes_cel_3(cel, ncp->nc_vp))
1467                         break;
1468                 /*
1469                  * All vnodes got re-locked. Re-validate the state and if
1470                  * nothing changed we are done. Otherwise restart.
1471                  */
1472                 if (ncp == vp->v_cache_dd &&
1473                     (ncp->nc_flag & NCF_ISDOTDOT) != 0 &&
1474                     blps[1] == NCP2BUCKETLOCK(ncp) &&
1475                     VP2VNODELOCK(ncp->nc_vp) == cel->vlp[2])
1476                         break;
1477                 cache_unlock_vnodes_cel(cel);
1478                 cel->vlp[0] = NULL;
1479                 cel->vlp[1] = NULL;
1480                 cel->vlp[2] = NULL;
1481         }
1482         cache_lock_buckets_cel(cel, blps[0], blps[1]);
1483 }
1484
1485 static void
1486 cache_enter_lock_dd(struct celockstate *cel, struct vnode *dvp, struct vnode *vp,
1487     uint32_t hash)
1488 {
1489         struct namecache *ncp;
1490         struct rwlock *blps[2];
1491
1492         blps[0] = HASH2BUCKETLOCK(hash);
1493         for (;;) {
1494                 blps[1] = NULL;
1495                 cache_lock_vnodes_cel(cel, dvp, vp);
1496                 ncp = dvp->v_cache_dd;
1497                 if (ncp == NULL)
1498                         break;
1499                 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1500                         break;
1501                 MPASS(ncp->nc_dvp == dvp);
1502                 blps[1] = NCP2BUCKETLOCK(ncp);
1503                 if (ncp->nc_flag & NCF_NEGATIVE)
1504                         break;
1505                 if (cache_lock_vnodes_cel_3(cel, ncp->nc_vp))
1506                         break;
1507                 if (ncp == dvp->v_cache_dd &&
1508                     (ncp->nc_flag & NCF_ISDOTDOT) != 0 &&
1509                     blps[1] == NCP2BUCKETLOCK(ncp) &&
1510                     VP2VNODELOCK(ncp->nc_vp) == cel->vlp[2])
1511                         break;
1512                 cache_unlock_vnodes_cel(cel);
1513                 cel->vlp[0] = NULL;
1514                 cel->vlp[1] = NULL;
1515                 cel->vlp[2] = NULL;
1516         }
1517         cache_lock_buckets_cel(cel, blps[0], blps[1]);
1518 }
1519
1520 static void
1521 cache_enter_unlock(struct celockstate *cel)
1522 {
1523
1524         cache_unlock_buckets_cel(cel);
1525         cache_unlock_vnodes_cel(cel);
1526 }
1527
1528 /*
1529  * Add an entry to the cache.
1530  */
1531 void
1532 cache_enter_time(struct vnode *dvp, struct vnode *vp, struct componentname *cnp,
1533     struct timespec *tsp, struct timespec *dtsp)
1534 {
1535         struct celockstate cel;
1536         struct namecache *ncp, *n2, *ndd;
1537         struct namecache_ts *n3;
1538         struct nchashhead *ncpp;
1539         struct neglist *neglist;
1540         uint32_t hash;
1541         int flag;
1542         int len;
1543         bool neg_locked;
1544
1545         CTR3(KTR_VFS, "cache_enter(%p, %p, %s)", dvp, vp, cnp->cn_nameptr);
1546         VNASSERT(vp == NULL || (vp->v_iflag & VI_DOOMED) == 0, vp,
1547             ("cache_enter: Adding a doomed vnode"));
1548         VNASSERT(dvp == NULL || (dvp->v_iflag & VI_DOOMED) == 0, dvp,
1549             ("cache_enter: Doomed vnode used as src"));
1550
1551         if (!doingcache)
1552                 return;
1553
1554         /*
1555          * Avoid blowout in namecache entries.
1556          */
1557         if (numcache >= desiredvnodes * ncsizefactor)
1558                 return;
1559
1560         cache_celockstate_init(&cel);
1561         ndd = NULL;
1562         flag = 0;
1563         if (cnp->cn_nameptr[0] == '.') {
1564                 if (cnp->cn_namelen == 1)
1565                         return;
1566                 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
1567                         len = cnp->cn_namelen;
1568                         hash = cache_get_hash(cnp->cn_nameptr, len, dvp);
1569                         cache_enter_lock_dd(&cel, dvp, vp, hash);
1570                         /*
1571                          * If dotdot entry already exists, just retarget it
1572                          * to new parent vnode, otherwise continue with new
1573                          * namecache entry allocation.
1574                          */
1575                         if ((ncp = dvp->v_cache_dd) != NULL &&
1576                             ncp->nc_flag & NCF_ISDOTDOT) {
1577                                 KASSERT(ncp->nc_dvp == dvp,
1578                                     ("wrong isdotdot parent"));
1579                                 neg_locked = false;
1580                                 if (ncp->nc_flag & NCF_NEGATIVE || vp == NULL) {
1581                                         neglist = NCP2NEGLIST(ncp);
1582                                         mtx_lock(&ncneg_hot.nl_lock);
1583                                         mtx_lock(&neglist->nl_lock);
1584                                         neg_locked = true;
1585                                 }
1586                                 if (!(ncp->nc_flag & NCF_NEGATIVE)) {
1587                                         TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst,
1588                                             ncp, nc_dst);
1589                                 } else {
1590                                         cache_negative_remove(ncp, true);
1591                                 }
1592                                 if (vp != NULL) {
1593                                         TAILQ_INSERT_HEAD(&vp->v_cache_dst,
1594                                             ncp, nc_dst);
1595                                         ncp->nc_flag &= ~(NCF_NEGATIVE|NCF_HOTNEGATIVE);
1596                                 } else {
1597                                         ncp->nc_flag &= ~(NCF_HOTNEGATIVE);
1598                                         ncp->nc_flag |= NCF_NEGATIVE;
1599                                         cache_negative_insert(ncp, true);
1600                                 }
1601                                 if (neg_locked) {
1602                                         mtx_unlock(&neglist->nl_lock);
1603                                         mtx_unlock(&ncneg_hot.nl_lock);
1604                                 }
1605                                 ncp->nc_vp = vp;
1606                                 cache_enter_unlock(&cel);
1607                                 return;
1608                         }
1609                         dvp->v_cache_dd = NULL;
1610                         cache_enter_unlock(&cel);
1611                         cache_celockstate_init(&cel);
1612                         SDT_PROBE3(vfs, namecache, enter, done, dvp, "..", vp);
1613                         flag = NCF_ISDOTDOT;
1614                 }
1615         }
1616
1617         /*
1618          * Calculate the hash key and setup as much of the new
1619          * namecache entry as possible before acquiring the lock.
1620          */
1621         ncp = cache_alloc(cnp->cn_namelen, tsp != NULL);
1622         ncp->nc_flag = flag;
1623         ncp->nc_vp = vp;
1624         if (vp == NULL)
1625                 ncp->nc_flag |= NCF_NEGATIVE;
1626         ncp->nc_dvp = dvp;
1627         if (tsp != NULL) {
1628                 n3 = (struct namecache_ts *)ncp;
1629                 n3->nc_time = *tsp;
1630                 n3->nc_ticks = ticks;
1631                 n3->nc_flag |= NCF_TS;
1632                 if (dtsp != NULL) {
1633                         n3->nc_dotdottime = *dtsp;
1634                         n3->nc_flag |= NCF_DTS;
1635                 }
1636         }
1637         len = ncp->nc_nlen = cnp->cn_namelen;
1638         hash = cache_get_hash(cnp->cn_nameptr, len, dvp);
1639         strlcpy(nc_get_name(ncp), cnp->cn_nameptr, len + 1);
1640         cache_enter_lock(&cel, dvp, vp, hash);
1641
1642         /*
1643          * See if this vnode or negative entry is already in the cache
1644          * with this name.  This can happen with concurrent lookups of
1645          * the same path name.
1646          */
1647         ncpp = NCHHASH(hash);
1648         LIST_FOREACH(n2, ncpp, nc_hash) {
1649                 if (n2->nc_dvp == dvp &&
1650                     n2->nc_nlen == cnp->cn_namelen &&
1651                     !bcmp(nc_get_name(n2), cnp->cn_nameptr, n2->nc_nlen)) {
1652                         if (tsp != NULL) {
1653                                 KASSERT((n2->nc_flag & NCF_TS) != 0,
1654                                     ("no NCF_TS"));
1655                                 n3 = (struct namecache_ts *)n2;
1656                                 n3->nc_time =
1657                                     ((struct namecache_ts *)ncp)->nc_time;
1658                                 n3->nc_ticks =
1659                                     ((struct namecache_ts *)ncp)->nc_ticks;
1660                                 if (dtsp != NULL) {
1661                                         n3->nc_dotdottime =
1662                                             ((struct namecache_ts *)ncp)->
1663                                             nc_dotdottime;
1664                                         if (ncp->nc_flag & NCF_NEGATIVE)
1665                                                 mtx_lock(&ncneg_hot.nl_lock);
1666                                         n3->nc_flag |= NCF_DTS;
1667                                         if (ncp->nc_flag & NCF_NEGATIVE)
1668                                                 mtx_unlock(&ncneg_hot.nl_lock);
1669                                 }
1670                         }
1671                         goto out_unlock_free;
1672                 }
1673         }
1674
1675         if (flag == NCF_ISDOTDOT) {
1676                 /*
1677                  * See if we are trying to add .. entry, but some other lookup
1678                  * has populated v_cache_dd pointer already.
1679                  */
1680                 if (dvp->v_cache_dd != NULL)
1681                         goto out_unlock_free;
1682                 KASSERT(vp == NULL || vp->v_type == VDIR,
1683                     ("wrong vnode type %p", vp));
1684                 dvp->v_cache_dd = ncp;
1685         }
1686
1687         atomic_add_rel_long(&numcache, 1);
1688         if (vp != NULL) {
1689                 if (vp->v_type == VDIR) {
1690                         if (flag != NCF_ISDOTDOT) {
1691                                 /*
1692                                  * For this case, the cache entry maps both the
1693                                  * directory name in it and the name ".." for the
1694                                  * directory's parent.
1695                                  */
1696                                 if ((ndd = vp->v_cache_dd) != NULL) {
1697                                         if ((ndd->nc_flag & NCF_ISDOTDOT) != 0)
1698                                                 cache_zap_locked(ndd, false);
1699                                         else
1700                                                 ndd = NULL;
1701                                 }
1702                                 vp->v_cache_dd = ncp;
1703                         }
1704                 } else {
1705                         vp->v_cache_dd = NULL;
1706                 }
1707         }
1708
1709         if (flag != NCF_ISDOTDOT) {
1710                 if (LIST_EMPTY(&dvp->v_cache_src)) {
1711                         vhold(dvp);
1712                         atomic_add_rel_long(&numcachehv, 1);
1713                 }
1714                 LIST_INSERT_HEAD(&dvp->v_cache_src, ncp, nc_src);
1715         }
1716
1717         /*
1718          * Insert the new namecache entry into the appropriate chain
1719          * within the cache entries table.
1720          */
1721         LIST_INSERT_HEAD(ncpp, ncp, nc_hash);
1722
1723         /*
1724          * If the entry is "negative", we place it into the
1725          * "negative" cache queue, otherwise, we place it into the
1726          * destination vnode's cache entries queue.
1727          */
1728         if (vp != NULL) {
1729                 TAILQ_INSERT_HEAD(&vp->v_cache_dst, ncp, nc_dst);
1730                 SDT_PROBE3(vfs, namecache, enter, done, dvp, nc_get_name(ncp),
1731                     vp);
1732         } else {
1733                 if (cnp->cn_flags & ISWHITEOUT)
1734                         ncp->nc_flag |= NCF_WHITE;
1735                 cache_negative_insert(ncp, false);
1736                 SDT_PROBE2(vfs, namecache, enter_negative, done, dvp,
1737                     nc_get_name(ncp));
1738         }
1739         cache_enter_unlock(&cel);
1740         if (numneg * ncnegfactor > numcache)
1741                 cache_negative_zap_one();
1742         cache_free(ndd);
1743         return;
1744 out_unlock_free:
1745         cache_enter_unlock(&cel);
1746         cache_free(ncp);
1747         return;
1748 }
1749
1750 static u_int
1751 cache_roundup_2(u_int val)
1752 {
1753         u_int res;
1754
1755         for (res = 1; res <= val; res <<= 1)
1756                 continue;
1757
1758         return (res);
1759 }
1760
1761 /*
1762  * Name cache initialization, from vfs_init() when we are booting
1763  */
1764 static void
1765 nchinit(void *dummy __unused)
1766 {
1767         u_int i;
1768
1769         cache_zone_small = uma_zcreate("S VFS Cache",
1770             sizeof(struct namecache) + CACHE_PATH_CUTOFF + 1,
1771             NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
1772         cache_zone_small_ts = uma_zcreate("STS VFS Cache",
1773             sizeof(struct namecache_ts) + CACHE_PATH_CUTOFF + 1,
1774             NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
1775         cache_zone_large = uma_zcreate("L VFS Cache",
1776             sizeof(struct namecache) + NAME_MAX + 1,
1777             NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
1778         cache_zone_large_ts = uma_zcreate("LTS VFS Cache",
1779             sizeof(struct namecache_ts) + NAME_MAX + 1,
1780             NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
1781
1782         nchashtbl = hashinit(desiredvnodes * 2, M_VFSCACHE, &nchash);
1783         numbucketlocks = cache_roundup_2(mp_ncpus * 64);
1784         bucketlocks = malloc(sizeof(*bucketlocks) * numbucketlocks, M_VFSCACHE,
1785             M_WAITOK | M_ZERO);
1786         for (i = 0; i < numbucketlocks; i++)
1787                 rw_init_flags(&bucketlocks[i], "ncbuc", RW_DUPOK | RW_RECURSE);
1788         numvnodelocks = cache_roundup_2(mp_ncpus * 64);
1789         vnodelocks = malloc(sizeof(*vnodelocks) * numvnodelocks, M_VFSCACHE,
1790             M_WAITOK | M_ZERO);
1791         for (i = 0; i < numvnodelocks; i++)
1792                 mtx_init(&vnodelocks[i], "ncvn", NULL, MTX_DUPOK | MTX_RECURSE);
1793         ncpurgeminvnodes = numbucketlocks;
1794
1795         numneglists = 4;
1796         neglists = malloc(sizeof(*neglists) * numneglists, M_VFSCACHE,
1797             M_WAITOK | M_ZERO);
1798         for (i = 0; i < numneglists; i++) {
1799                 mtx_init(&neglists[i].nl_lock, "ncnegl", NULL, MTX_DEF);
1800                 TAILQ_INIT(&neglists[i].nl_list);
1801         }
1802         mtx_init(&ncneg_hot.nl_lock, "ncneglh", NULL, MTX_DEF);
1803         TAILQ_INIT(&ncneg_hot.nl_list);
1804
1805         numcalls = counter_u64_alloc(M_WAITOK);
1806         dothits = counter_u64_alloc(M_WAITOK);
1807         dotdothits = counter_u64_alloc(M_WAITOK);
1808         numchecks = counter_u64_alloc(M_WAITOK);
1809         nummiss = counter_u64_alloc(M_WAITOK);
1810         nummisszap = counter_u64_alloc(M_WAITOK);
1811         numposzaps = counter_u64_alloc(M_WAITOK);
1812         numposhits = counter_u64_alloc(M_WAITOK);
1813         numnegzaps = counter_u64_alloc(M_WAITOK);
1814         numneghits = counter_u64_alloc(M_WAITOK);
1815         numfullpathcalls = counter_u64_alloc(M_WAITOK);
1816         numfullpathfail1 = counter_u64_alloc(M_WAITOK);
1817         numfullpathfail2 = counter_u64_alloc(M_WAITOK);
1818         numfullpathfail4 = counter_u64_alloc(M_WAITOK);
1819         numfullpathfound = counter_u64_alloc(M_WAITOK);
1820 }
1821 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nchinit, NULL);
1822
1823 void
1824 cache_changesize(int newmaxvnodes)
1825 {
1826         struct nchashhead *new_nchashtbl, *old_nchashtbl;
1827         u_long new_nchash, old_nchash;
1828         struct namecache *ncp;
1829         uint32_t hash;
1830         int i;
1831
1832         new_nchashtbl = hashinit(newmaxvnodes * 2, M_VFSCACHE, &new_nchash);
1833         /* If same hash table size, nothing to do */
1834         if (nchash == new_nchash) {
1835                 free(new_nchashtbl, M_VFSCACHE);
1836                 return;
1837         }
1838         /*
1839          * Move everything from the old hash table to the new table.
1840          * None of the namecache entries in the table can be removed
1841          * because to do so, they have to be removed from the hash table.
1842          */
1843         cache_lock_all_vnodes();
1844         cache_lock_all_buckets();
1845         old_nchashtbl = nchashtbl;
1846         old_nchash = nchash;
1847         nchashtbl = new_nchashtbl;
1848         nchash = new_nchash;
1849         for (i = 0; i <= old_nchash; i++) {
1850                 while ((ncp = LIST_FIRST(&old_nchashtbl[i])) != NULL) {
1851                         hash = cache_get_hash(nc_get_name(ncp), ncp->nc_nlen,
1852                             ncp->nc_dvp);
1853                         LIST_REMOVE(ncp, nc_hash);
1854                         LIST_INSERT_HEAD(NCHHASH(hash), ncp, nc_hash);
1855                 }
1856         }
1857         cache_unlock_all_buckets();
1858         cache_unlock_all_vnodes();
1859         free(old_nchashtbl, M_VFSCACHE);
1860 }
1861
1862 /*
1863  * Invalidate all entries to a particular vnode.
1864  */
1865 void
1866 cache_purge(struct vnode *vp)
1867 {
1868         TAILQ_HEAD(, namecache) ncps;
1869         struct namecache *ncp, *nnp;
1870         struct mtx *vlp, *vlp2;
1871
1872         CTR1(KTR_VFS, "cache_purge(%p)", vp);
1873         SDT_PROBE1(vfs, namecache, purge, done, vp);
1874         if (LIST_EMPTY(&vp->v_cache_src) && TAILQ_EMPTY(&vp->v_cache_dst) &&
1875             vp->v_cache_dd == NULL)
1876                 return;
1877         TAILQ_INIT(&ncps);
1878         vlp = VP2VNODELOCK(vp);
1879         vlp2 = NULL;
1880         mtx_lock(vlp);
1881 retry:
1882         while (!LIST_EMPTY(&vp->v_cache_src)) {
1883                 ncp = LIST_FIRST(&vp->v_cache_src);
1884                 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
1885                         goto retry;
1886                 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
1887         }
1888         while (!TAILQ_EMPTY(&vp->v_cache_dst)) {
1889                 ncp = TAILQ_FIRST(&vp->v_cache_dst);
1890                 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
1891                         goto retry;
1892                 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
1893         }
1894         ncp = vp->v_cache_dd;
1895         if (ncp != NULL) {
1896                 KASSERT(ncp->nc_flag & NCF_ISDOTDOT,
1897                    ("lost dotdot link"));
1898                 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
1899                         goto retry;
1900                 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
1901         }
1902         KASSERT(vp->v_cache_dd == NULL, ("incomplete purge"));
1903         mtx_unlock(vlp);
1904         if (vlp2 != NULL)
1905                 mtx_unlock(vlp2);
1906         TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) {
1907                 cache_free(ncp);
1908         }
1909 }
1910
1911 /*
1912  * Invalidate all negative entries for a particular directory vnode.
1913  */
1914 void
1915 cache_purge_negative(struct vnode *vp)
1916 {
1917         TAILQ_HEAD(, namecache) ncps;
1918         struct namecache *ncp, *nnp;
1919         struct mtx *vlp;
1920
1921         CTR1(KTR_VFS, "cache_purge_negative(%p)", vp);
1922         SDT_PROBE1(vfs, namecache, purge_negative, done, vp);
1923         TAILQ_INIT(&ncps);
1924         vlp = VP2VNODELOCK(vp);
1925         mtx_lock(vlp);
1926         LIST_FOREACH_SAFE(ncp, &vp->v_cache_src, nc_src, nnp) {
1927                 if (!(ncp->nc_flag & NCF_NEGATIVE))
1928                         continue;
1929                 cache_zap_negative_locked_vnode_kl(ncp, vp);
1930                 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
1931         }
1932         mtx_unlock(vlp);
1933         TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) {
1934                 cache_free(ncp);
1935         }
1936 }
1937
1938 /*
1939  * Flush all entries referencing a particular filesystem.
1940  */
1941 void
1942 cache_purgevfs(struct mount *mp, bool force)
1943 {
1944         TAILQ_HEAD(, namecache) ncps;
1945         struct mtx *vlp1, *vlp2;
1946         struct rwlock *blp;
1947         struct nchashhead *bucket;
1948         struct namecache *ncp, *nnp;
1949         u_long i, j, n_nchash;
1950         int error;
1951
1952         /* Scan hash tables for applicable entries */
1953         SDT_PROBE1(vfs, namecache, purgevfs, done, mp);
1954         if (!force && mp->mnt_nvnodelistsize <= ncpurgeminvnodes)
1955                 return;
1956         TAILQ_INIT(&ncps);
1957         n_nchash = nchash + 1;
1958         vlp1 = vlp2 = NULL;
1959         for (i = 0; i < numbucketlocks; i++) {
1960                 blp = (struct rwlock *)&bucketlocks[i];
1961                 rw_wlock(blp);
1962                 for (j = i; j < n_nchash; j += numbucketlocks) {
1963 retry:
1964                         bucket = &nchashtbl[j];
1965                         LIST_FOREACH_SAFE(ncp, bucket, nc_hash, nnp) {
1966                                 cache_assert_bucket_locked(ncp, RA_WLOCKED);
1967                                 if (ncp->nc_dvp->v_mount != mp)
1968                                         continue;
1969                                 error = cache_zap_wlocked_bucket_kl(ncp, blp,
1970                                     &vlp1, &vlp2);
1971                                 if (error != 0)
1972                                         goto retry;
1973                                 TAILQ_INSERT_HEAD(&ncps, ncp, nc_dst);
1974                         }
1975                 }
1976                 rw_wunlock(blp);
1977                 if (vlp1 == NULL && vlp2 == NULL)
1978                         cache_maybe_yield();
1979         }
1980         if (vlp1 != NULL)
1981                 mtx_unlock(vlp1);
1982         if (vlp2 != NULL)
1983                 mtx_unlock(vlp2);
1984
1985         TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) {
1986                 cache_free(ncp);
1987         }
1988 }
1989
1990 /*
1991  * Perform canonical checks and cache lookup and pass on to filesystem
1992  * through the vop_cachedlookup only if needed.
1993  */
1994
1995 int
1996 vfs_cache_lookup(struct vop_lookup_args *ap)
1997 {
1998         struct vnode *dvp;
1999         int error;
2000         struct vnode **vpp = ap->a_vpp;
2001         struct componentname *cnp = ap->a_cnp;
2002         struct ucred *cred = cnp->cn_cred;
2003         int flags = cnp->cn_flags;
2004         struct thread *td = cnp->cn_thread;
2005
2006         *vpp = NULL;
2007         dvp = ap->a_dvp;
2008
2009         if (dvp->v_type != VDIR)
2010                 return (ENOTDIR);
2011
2012         if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
2013             (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
2014                 return (EROFS);
2015
2016         error = VOP_ACCESS(dvp, VEXEC, cred, td);
2017         if (error)
2018                 return (error);
2019
2020         error = cache_lookup(dvp, vpp, cnp, NULL, NULL);
2021         if (error == 0)
2022                 return (VOP_CACHEDLOOKUP(dvp, vpp, cnp));
2023         if (error == -1)
2024                 return (0);
2025         return (error);
2026 }
2027
2028 /*
2029  * XXX All of these sysctls would probably be more productive dead.
2030  */
2031 static int disablecwd;
2032 SYSCTL_INT(_debug, OID_AUTO, disablecwd, CTLFLAG_RW, &disablecwd, 0,
2033    "Disable the getcwd syscall");
2034
2035 /* Implementation of the getcwd syscall. */
2036 int
2037 sys___getcwd(struct thread *td, struct __getcwd_args *uap)
2038 {
2039
2040         return (kern___getcwd(td, uap->buf, UIO_USERSPACE, uap->buflen,
2041             MAXPATHLEN));
2042 }
2043
2044 int
2045 kern___getcwd(struct thread *td, char *buf, enum uio_seg bufseg, u_int buflen,
2046     u_int path_max)
2047 {
2048         char *bp, *tmpbuf;
2049         struct filedesc *fdp;
2050         struct vnode *cdir, *rdir;
2051         int error;
2052
2053         if (disablecwd)
2054                 return (ENODEV);
2055         if (buflen < 2)
2056                 return (EINVAL);
2057         if (buflen > path_max)
2058                 buflen = path_max;
2059
2060         tmpbuf = malloc(buflen, M_TEMP, M_WAITOK);
2061         fdp = td->td_proc->p_fd;
2062         FILEDESC_SLOCK(fdp);
2063         cdir = fdp->fd_cdir;
2064         VREF(cdir);
2065         rdir = fdp->fd_rdir;
2066         VREF(rdir);
2067         FILEDESC_SUNLOCK(fdp);
2068         error = vn_fullpath1(td, cdir, rdir, tmpbuf, &bp, buflen);
2069         vrele(rdir);
2070         vrele(cdir);
2071
2072         if (!error) {
2073                 if (bufseg == UIO_SYSSPACE)
2074                         bcopy(bp, buf, strlen(bp) + 1);
2075                 else
2076                         error = copyout(bp, buf, strlen(bp) + 1);
2077 #ifdef KTRACE
2078         if (KTRPOINT(curthread, KTR_NAMEI))
2079                 ktrnamei(bp);
2080 #endif
2081         }
2082         free(tmpbuf, M_TEMP);
2083         return (error);
2084 }
2085
2086 /*
2087  * Thus begins the fullpath magic.
2088  */
2089
2090 static int disablefullpath;
2091 SYSCTL_INT(_debug, OID_AUTO, disablefullpath, CTLFLAG_RW, &disablefullpath, 0,
2092     "Disable the vn_fullpath function");
2093
2094 /*
2095  * Retrieve the full filesystem path that correspond to a vnode from the name
2096  * cache (if available)
2097  */
2098 int
2099 vn_fullpath(struct thread *td, struct vnode *vn, char **retbuf, char **freebuf)
2100 {
2101         char *buf;
2102         struct filedesc *fdp;
2103         struct vnode *rdir;
2104         int error;
2105
2106         if (disablefullpath)
2107                 return (ENODEV);
2108         if (vn == NULL)
2109                 return (EINVAL);
2110
2111         buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
2112         fdp = td->td_proc->p_fd;
2113         FILEDESC_SLOCK(fdp);
2114         rdir = fdp->fd_rdir;
2115         VREF(rdir);
2116         FILEDESC_SUNLOCK(fdp);
2117         error = vn_fullpath1(td, vn, rdir, buf, retbuf, MAXPATHLEN);
2118         vrele(rdir);
2119
2120         if (!error)
2121                 *freebuf = buf;
2122         else
2123                 free(buf, M_TEMP);
2124         return (error);
2125 }
2126
2127 /*
2128  * This function is similar to vn_fullpath, but it attempts to lookup the
2129  * pathname relative to the global root mount point.  This is required for the
2130  * auditing sub-system, as audited pathnames must be absolute, relative to the
2131  * global root mount point.
2132  */
2133 int
2134 vn_fullpath_global(struct thread *td, struct vnode *vn,
2135     char **retbuf, char **freebuf)
2136 {
2137         char *buf;
2138         int error;
2139
2140         if (disablefullpath)
2141                 return (ENODEV);
2142         if (vn == NULL)
2143                 return (EINVAL);
2144         buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
2145         error = vn_fullpath1(td, vn, rootvnode, buf, retbuf, MAXPATHLEN);
2146         if (!error)
2147                 *freebuf = buf;
2148         else
2149                 free(buf, M_TEMP);
2150         return (error);
2151 }
2152
2153 int
2154 vn_vptocnp(struct vnode **vp, struct ucred *cred, char *buf, u_int *buflen)
2155 {
2156         struct vnode *dvp;
2157         struct namecache *ncp;
2158         struct mtx *vlp;
2159         int error;
2160
2161         vlp = VP2VNODELOCK(*vp);
2162         mtx_lock(vlp);
2163         TAILQ_FOREACH(ncp, &((*vp)->v_cache_dst), nc_dst) {
2164                 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
2165                         break;
2166         }
2167         if (ncp != NULL) {
2168                 if (*buflen < ncp->nc_nlen) {
2169                         mtx_unlock(vlp);
2170                         vrele(*vp);
2171                         counter_u64_add(numfullpathfail4, 1);
2172                         error = ENOMEM;
2173                         SDT_PROBE3(vfs, namecache, fullpath, return, error,
2174                             vp, NULL);
2175                         return (error);
2176                 }
2177                 *buflen -= ncp->nc_nlen;
2178                 memcpy(buf + *buflen, nc_get_name(ncp), ncp->nc_nlen);
2179                 SDT_PROBE3(vfs, namecache, fullpath, hit, ncp->nc_dvp,
2180                     nc_get_name(ncp), vp);
2181                 dvp = *vp;
2182                 *vp = ncp->nc_dvp;
2183                 vref(*vp);
2184                 mtx_unlock(vlp);
2185                 vrele(dvp);
2186                 return (0);
2187         }
2188         SDT_PROBE1(vfs, namecache, fullpath, miss, vp);
2189
2190         mtx_unlock(vlp);
2191         vn_lock(*vp, LK_SHARED | LK_RETRY);
2192         error = VOP_VPTOCNP(*vp, &dvp, cred, buf, buflen);
2193         vput(*vp);
2194         if (error) {
2195                 counter_u64_add(numfullpathfail2, 1);
2196                 SDT_PROBE3(vfs, namecache, fullpath, return,  error, vp, NULL);
2197                 return (error);
2198         }
2199
2200         *vp = dvp;
2201         if (dvp->v_iflag & VI_DOOMED) {
2202                 /* forced unmount */
2203                 vrele(dvp);
2204                 error = ENOENT;
2205                 SDT_PROBE3(vfs, namecache, fullpath, return, error, vp, NULL);
2206                 return (error);
2207         }
2208         /*
2209          * *vp has its use count incremented still.
2210          */
2211
2212         return (0);
2213 }
2214
2215 /*
2216  * The magic behind kern___getcwd() and vn_fullpath().
2217  */
2218 static int
2219 vn_fullpath1(struct thread *td, struct vnode *vp, struct vnode *rdir,
2220     char *buf, char **retbuf, u_int buflen)
2221 {
2222         int error, slash_prefixed;
2223 #ifdef KDTRACE_HOOKS
2224         struct vnode *startvp = vp;
2225 #endif
2226         struct vnode *vp1;
2227
2228         buflen--;
2229         buf[buflen] = '\0';
2230         error = 0;
2231         slash_prefixed = 0;
2232
2233         SDT_PROBE1(vfs, namecache, fullpath, entry, vp);
2234         counter_u64_add(numfullpathcalls, 1);
2235         vref(vp);
2236         if (vp->v_type != VDIR) {
2237                 error = vn_vptocnp(&vp, td->td_ucred, buf, &buflen);
2238                 if (error)
2239                         return (error);
2240                 if (buflen == 0) {
2241                         vrele(vp);
2242                         return (ENOMEM);
2243                 }
2244                 buf[--buflen] = '/';
2245                 slash_prefixed = 1;
2246         }
2247         while (vp != rdir && vp != rootvnode) {
2248                 /*
2249                  * The vp vnode must be already fully constructed,
2250                  * since it is either found in namecache or obtained
2251                  * from VOP_VPTOCNP().  We may test for VV_ROOT safely
2252                  * without obtaining the vnode lock.
2253                  */
2254                 if ((vp->v_vflag & VV_ROOT) != 0) {
2255                         vn_lock(vp, LK_RETRY | LK_SHARED);
2256
2257                         /*
2258                          * With the vnode locked, check for races with
2259                          * unmount, forced or not.  Note that we
2260                          * already verified that vp is not equal to
2261                          * the root vnode, which means that
2262                          * mnt_vnodecovered can be NULL only for the
2263                          * case of unmount.
2264                          */
2265                         if ((vp->v_iflag & VI_DOOMED) != 0 ||
2266                             (vp1 = vp->v_mount->mnt_vnodecovered) == NULL ||
2267                             vp1->v_mountedhere != vp->v_mount) {
2268                                 vput(vp);
2269                                 error = ENOENT;
2270                                 SDT_PROBE3(vfs, namecache, fullpath, return,
2271                                     error, vp, NULL);
2272                                 break;
2273                         }
2274
2275                         vref(vp1);
2276                         vput(vp);
2277                         vp = vp1;
2278                         continue;
2279                 }
2280                 if (vp->v_type != VDIR) {
2281                         vrele(vp);
2282                         counter_u64_add(numfullpathfail1, 1);
2283                         error = ENOTDIR;
2284                         SDT_PROBE3(vfs, namecache, fullpath, return,
2285                             error, vp, NULL);
2286                         break;
2287                 }
2288                 error = vn_vptocnp(&vp, td->td_ucred, buf, &buflen);
2289                 if (error)
2290                         break;
2291                 if (buflen == 0) {
2292                         vrele(vp);
2293                         error = ENOMEM;
2294                         SDT_PROBE3(vfs, namecache, fullpath, return, error,
2295                             startvp, NULL);
2296                         break;
2297                 }
2298                 buf[--buflen] = '/';
2299                 slash_prefixed = 1;
2300         }
2301         if (error)
2302                 return (error);
2303         if (!slash_prefixed) {
2304                 if (buflen == 0) {
2305                         vrele(vp);
2306                         counter_u64_add(numfullpathfail4, 1);
2307                         SDT_PROBE3(vfs, namecache, fullpath, return, ENOMEM,
2308                             startvp, NULL);
2309                         return (ENOMEM);
2310                 }
2311                 buf[--buflen] = '/';
2312         }
2313         counter_u64_add(numfullpathfound, 1);
2314         vrele(vp);
2315
2316         SDT_PROBE3(vfs, namecache, fullpath, return, 0, startvp, buf + buflen);
2317         *retbuf = buf + buflen;
2318         return (0);
2319 }
2320
2321 struct vnode *
2322 vn_dir_dd_ino(struct vnode *vp)
2323 {
2324         struct namecache *ncp;
2325         struct vnode *ddvp;
2326         struct mtx *vlp;
2327
2328         ASSERT_VOP_LOCKED(vp, "vn_dir_dd_ino");
2329         vlp = VP2VNODELOCK(vp);
2330         mtx_lock(vlp);
2331         TAILQ_FOREACH(ncp, &(vp->v_cache_dst), nc_dst) {
2332                 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0)
2333                         continue;
2334                 ddvp = ncp->nc_dvp;
2335                 vhold(ddvp);
2336                 mtx_unlock(vlp);
2337                 if (vget(ddvp, LK_SHARED | LK_NOWAIT | LK_VNHELD, curthread))
2338                         return (NULL);
2339                 return (ddvp);
2340         }
2341         mtx_unlock(vlp);
2342         return (NULL);
2343 }
2344
2345 int
2346 vn_commname(struct vnode *vp, char *buf, u_int buflen)
2347 {
2348         struct namecache *ncp;
2349         struct mtx *vlp;
2350         int l;
2351
2352         vlp = VP2VNODELOCK(vp);
2353         mtx_lock(vlp);
2354         TAILQ_FOREACH(ncp, &vp->v_cache_dst, nc_dst)
2355                 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
2356                         break;
2357         if (ncp == NULL) {
2358                 mtx_unlock(vlp);
2359                 return (ENOENT);
2360         }
2361         l = min(ncp->nc_nlen, buflen - 1);
2362         memcpy(buf, nc_get_name(ncp), l);
2363         mtx_unlock(vlp);
2364         buf[l] = '\0';
2365         return (0);
2366 }
2367
2368 /* ABI compat shims for old kernel modules. */
2369 #undef cache_enter
2370
2371 void    cache_enter(struct vnode *dvp, struct vnode *vp,
2372             struct componentname *cnp);
2373
2374 void
2375 cache_enter(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2376 {
2377
2378         cache_enter_time(dvp, vp, cnp, NULL, NULL);
2379 }
2380
2381 /*
2382  * This function updates path string to vnode's full global path
2383  * and checks the size of the new path string against the pathlen argument.
2384  *
2385  * Requires a locked, referenced vnode.
2386  * Vnode is re-locked on success or ENODEV, otherwise unlocked.
2387  *
2388  * If sysctl debug.disablefullpath is set, ENODEV is returned,
2389  * vnode is left locked and path remain untouched.
2390  *
2391  * If vp is a directory, the call to vn_fullpath_global() always succeeds
2392  * because it falls back to the ".." lookup if the namecache lookup fails.
2393  */
2394 int
2395 vn_path_to_global_path(struct thread *td, struct vnode *vp, char *path,
2396     u_int pathlen)
2397 {
2398         struct nameidata nd;
2399         struct vnode *vp1;
2400         char *rpath, *fbuf;
2401         int error;
2402
2403         ASSERT_VOP_ELOCKED(vp, __func__);
2404
2405         /* Return ENODEV if sysctl debug.disablefullpath==1 */
2406         if (disablefullpath)
2407                 return (ENODEV);
2408
2409         /* Construct global filesystem path from vp. */
2410         VOP_UNLOCK(vp, 0);
2411         error = vn_fullpath_global(td, vp, &rpath, &fbuf);
2412
2413         if (error != 0) {
2414                 vrele(vp);
2415                 return (error);
2416         }
2417
2418         if (strlen(rpath) >= pathlen) {
2419                 vrele(vp);
2420                 error = ENAMETOOLONG;
2421                 goto out;
2422         }
2423
2424         /*
2425          * Re-lookup the vnode by path to detect a possible rename.
2426          * As a side effect, the vnode is relocked.
2427          * If vnode was renamed, return ENOENT.
2428          */
2429         NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1,
2430             UIO_SYSSPACE, path, td);
2431         error = namei(&nd);
2432         if (error != 0) {
2433                 vrele(vp);
2434                 goto out;
2435         }
2436         NDFREE(&nd, NDF_ONLY_PNBUF);
2437         vp1 = nd.ni_vp;
2438         vrele(vp);
2439         if (vp1 == vp)
2440                 strcpy(path, rpath);
2441         else {
2442                 vput(vp1);
2443                 error = ENOENT;
2444         }
2445
2446 out:
2447         free(fbuf, M_TEMP);
2448         return (error);
2449 }