2 * Copyright (c) 2003,2004 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * Copyright (c) 1989, 1993, 1995
35 * The Regents of the University of California. All rights reserved.
37 * This code is derived from software contributed to Berkeley by
38 * Poul-Henning Kamp of the FreeBSD Project.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. All advertising materials mentioning features or use of this software
49 * must display the following acknowledgement:
50 * This product includes software developed by the University of
51 * California, Berkeley and its contributors.
52 * 4. Neither the name of the University nor the names of its contributors
53 * may be used to endorse or promote products derived from this software
54 * without specific prior written permission.
56 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
57 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
60 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * @(#)vfs_cache.c 8.5 (Berkeley) 3/22/95
69 * $FreeBSD: src/sys/kern/vfs_cache.c,v 1.42.2.6 2001/10/05 20:07:03 dillon Exp $
70 * $DragonFly: src/sys/kern/vfs_cache.c,v 1.91 2008/06/14 05:34:06 dillon Exp $
73 #include <sys/param.h>
74 #include <sys/systm.h>
75 #include <sys/kernel.h>
76 #include <sys/sysctl.h>
77 #include <sys/mount.h>
78 #include <sys/vnode.h>
79 #include <sys/malloc.h>
80 #include <sys/sysproto.h>
81 #include <sys/spinlock.h>
83 #include <sys/namei.h>
84 #include <sys/nlookup.h>
85 #include <sys/filedesc.h>
86 #include <sys/fnv_hash.h>
87 #include <sys/globaldata.h>
88 #include <sys/kern_syscall.h>
89 #include <sys/dirent.h>
92 #include <sys/sysref2.h>
93 #include <sys/spinlock2.h>
94 #include <sys/mplock2.h>
96 #define MAX_RECURSION_DEPTH 64
99 * Random lookups in the cache are accomplished with a hash table using
100 * a hash key of (nc_src_vp, name).
102 * Negative entries may exist and correspond to structures where nc_vp
103 * is NULL. In a negative entry, NCF_WHITEOUT will be set if the entry
104 * corresponds to a whited-out directory entry (verses simply not finding the
107 * Upon reaching the last segment of a path, if the reference is for DELETE,
108 * or NOCACHE is set (rewrite), and the name is located in the cache, it
113 * Structures associated with name cacheing.
115 #define NCHHASH(hash) (&nchashtbl[(hash) & nchash])
118 MALLOC_DEFINE(M_VFSCACHE, "vfscache", "VFS name cache entries");
120 LIST_HEAD(nchash_list, namecache);
123 struct nchash_list list;
124 struct spinlock spin;
127 static struct nchash_head *nchashtbl;
128 static struct namecache_list ncneglist;
129 static struct spinlock ncspin;
130 struct lwkt_token vfs_token;
133 * ncvp_debug - debug cache_fromvp(). This is used by the NFS server
134 * to create the namecache infrastructure leading to a dangling vnode.
136 * 0 Only errors are reported
137 * 1 Successes are reported
138 * 2 Successes + the whole directory scan is reported
139 * 3 Force the directory scan code run as if the parent vnode did not
140 * have a namecache record, even if it does have one.
142 static int ncvp_debug;
143 SYSCTL_INT(_debug, OID_AUTO, ncvp_debug, CTLFLAG_RW, &ncvp_debug, 0, "");
145 static u_long nchash; /* size of hash table */
146 SYSCTL_ULONG(_debug, OID_AUTO, nchash, CTLFLAG_RD, &nchash, 0, "");
148 static int ncnegfactor = 16; /* ratio of negative entries */
149 SYSCTL_INT(_debug, OID_AUTO, ncnegfactor, CTLFLAG_RW, &ncnegfactor, 0, "");
151 static int nclockwarn; /* warn on locked entries in ticks */
152 SYSCTL_INT(_debug, OID_AUTO, nclockwarn, CTLFLAG_RW, &nclockwarn, 0, "");
154 static int numneg; /* number of cache entries allocated */
155 SYSCTL_INT(_debug, OID_AUTO, numneg, CTLFLAG_RD, &numneg, 0, "");
157 static int numcache; /* number of cache entries allocated */
158 SYSCTL_INT(_debug, OID_AUTO, numcache, CTLFLAG_RD, &numcache, 0, "");
160 static int numunres; /* number of unresolved entries */
161 SYSCTL_INT(_debug, OID_AUTO, numunres, CTLFLAG_RD, &numunres, 0, "");
163 SYSCTL_INT(_debug, OID_AUTO, vnsize, CTLFLAG_RD, 0, sizeof(struct vnode), "");
164 SYSCTL_INT(_debug, OID_AUTO, ncsize, CTLFLAG_RD, 0, sizeof(struct namecache), "");
166 static int cache_resolve_mp(struct mount *mp);
167 static struct vnode *cache_dvpref(struct namecache *ncp);
168 static void _cache_rehash(struct namecache *ncp);
169 static void _cache_lock(struct namecache *ncp);
170 static void _cache_setunresolved(struct namecache *ncp);
173 * The new name cache statistics
175 SYSCTL_NODE(_vfs, OID_AUTO, cache, CTLFLAG_RW, 0, "Name cache statistics");
176 #define STATNODE(mode, name, var) \
177 SYSCTL_ULONG(_vfs_cache, OID_AUTO, name, mode, var, 0, "");
178 STATNODE(CTLFLAG_RD, numneg, &numneg);
179 STATNODE(CTLFLAG_RD, numcache, &numcache);
180 static u_long numcalls; STATNODE(CTLFLAG_RD, numcalls, &numcalls);
181 static u_long dothits; STATNODE(CTLFLAG_RD, dothits, &dothits);
182 static u_long dotdothits; STATNODE(CTLFLAG_RD, dotdothits, &dotdothits);
183 static u_long numchecks; STATNODE(CTLFLAG_RD, numchecks, &numchecks);
184 static u_long nummiss; STATNODE(CTLFLAG_RD, nummiss, &nummiss);
185 static u_long nummisszap; STATNODE(CTLFLAG_RD, nummisszap, &nummisszap);
186 static u_long numposzaps; STATNODE(CTLFLAG_RD, numposzaps, &numposzaps);
187 static u_long numposhits; STATNODE(CTLFLAG_RD, numposhits, &numposhits);
188 static u_long numnegzaps; STATNODE(CTLFLAG_RD, numnegzaps, &numnegzaps);
189 static u_long numneghits; STATNODE(CTLFLAG_RD, numneghits, &numneghits);
191 struct nchstats nchstats[SMP_MAXCPU];
193 * Export VFS cache effectiveness statistics to user-land.
195 * The statistics are left for aggregation to user-land so
196 * neat things can be achieved, like observing per-CPU cache
200 sysctl_nchstats(SYSCTL_HANDLER_ARGS)
202 struct globaldata *gd;
206 for (i = 0; i < ncpus; ++i) {
207 gd = globaldata_find(i);
208 if ((error = SYSCTL_OUT(req, (void *)&(*gd->gd_nchstats),
209 sizeof(struct nchstats))))
215 SYSCTL_PROC(_vfs_cache, OID_AUTO, nchstats, CTLTYPE_OPAQUE|CTLFLAG_RD,
216 0, 0, sysctl_nchstats, "S,nchstats", "VFS cache effectiveness statistics");
218 static struct namecache *cache_zap(struct namecache *ncp);
221 * Namespace locking. The caller must already hold a reference to the
222 * namecache structure in order to lock/unlock it. This function prevents
223 * the namespace from being created or destroyed by accessors other then
226 * Note that holding a locked namecache structure prevents other threads
227 * from making namespace changes (e.g. deleting or creating), prevents
228 * vnode association state changes by other threads, and prevents the
229 * namecache entry from being resolved or unresolved by other threads.
231 * The lock owner has full authority to associate/disassociate vnodes
232 * and resolve/unresolve the locked ncp.
234 * WARNING! Holding a locked ncp will prevent a vnode from being destroyed
235 * or recycled, but it does NOT help you if the vnode had already
236 * initiated a recyclement. If this is important, use cache_get()
237 * rather then cache_lock() (and deal with the differences in the
238 * way the refs counter is handled). Or, alternatively, make an
239 * unconditional call to cache_validate() or cache_resolve()
240 * after cache_lock() returns.
244 _cache_lock(struct namecache *ncp)
251 KKASSERT(ncp->nc_refs != 0);
256 xtd = ncp->nc_locktd;
263 if (atomic_cmpset_ptr(&ncp->nc_locktd, NULL, td)) {
264 KKASSERT(ncp->nc_exlocks == 0);
268 * The vp associated with a locked ncp must
269 * be held to prevent it from being recycled.
271 * WARNING! If VRECLAIMED is set the vnode
272 * could already be in the middle of a recycle.
273 * Callers must use cache_vref() or
274 * cache_vget() on the locked ncp to
275 * validate the vp or set the cache entry
279 vhold(ncp->nc_vp); /* MPSAFE */
286 * Memory interlock (XXX)
289 tsleep_interlock(ncp, 0);
291 if (xtd != ncp->nc_locktd)
293 error = tsleep(ncp, PINTERLOCKED, "clock", nclockwarn);
294 if (error == EWOULDBLOCK) {
298 kprintf("[diagnostic] cache_lock: blocked on %p", ncp);
299 kprintf(" \"%*.*s\"\n",
300 ncp->nc_nlen, ncp->nc_nlen, ncp->nc_name);
305 kprintf("[diagnostic] cache_lock: unblocked %*.*s\n",
306 ncp->nc_nlen, ncp->nc_nlen, ncp->nc_name);
312 _cache_lock_nonblock(struct namecache *ncp)
317 KKASSERT(ncp->nc_refs != 0);
321 xtd = ncp->nc_locktd;
328 if (atomic_cmpset_ptr(&ncp->nc_locktd, NULL, td)) {
329 KKASSERT(ncp->nc_exlocks == 0);
333 * The vp associated with a locked ncp must
334 * be held to prevent it from being recycled.
336 * WARNING! If VRECLAIMED is set the vnode
337 * could already be in the middle of a recycle.
338 * Callers must use cache_vref() or
339 * cache_vget() on the locked ncp to
340 * validate the vp or set the cache entry
344 vhold(ncp->nc_vp); /* MPSAFE */
357 * NOTE: nc_refs can be 0 (degenerate case during _cache_drop).
361 _cache_unlock(struct namecache *ncp)
363 thread_t td __debugvar = curthread;
365 KKASSERT(ncp->nc_refs >= 0);
366 KKASSERT(ncp->nc_exlocks > 0);
367 KKASSERT(ncp->nc_locktd == td);
369 if (--ncp->nc_exlocks == 0) {
372 ncp->nc_locktd = NULL;
374 if (ncp->nc_lockreq) {
383 * cache_hold() and cache_drop() prevent the premature deletion of a
384 * namecache entry but do not prevent operations (such as zapping) on
385 * that namecache entry.
387 * This routine may only be called from outside this source module if
388 * nc_refs is already at least 1.
390 * This is a rare case where callers are allowed to hold a spinlock,
391 * so we can't ourselves.
397 _cache_hold(struct namecache *ncp)
399 atomic_add_int(&ncp->nc_refs, 1);
404 * Drop a cache entry, taking care to deal with races.
406 * For potential 1->0 transitions we must hold the ncp lock to safely
407 * test its flags. An unresolved entry with no children must be zapped
410 * The call to cache_zap() itself will handle all remaining races and
411 * will decrement the ncp's refs regardless. If we are resolved or
412 * have children nc_refs can safely be dropped to 0 without having to
415 * NOTE: cache_zap() will re-check nc_refs and nc_list in a MPSAFE fashion.
417 * NOTE: cache_zap() may return a non-NULL referenced parent which must
418 * be dropped in a loop.
422 _cache_drop(struct namecache *ncp)
427 KKASSERT(ncp->nc_refs > 0);
431 if (_cache_lock_nonblock(ncp) == 0) {
432 if ((ncp->nc_flag & NCF_UNRESOLVED) &&
433 TAILQ_EMPTY(&ncp->nc_list)) {
434 ncp = cache_zap(ncp);
437 if (atomic_cmpset_int(&ncp->nc_refs, 1, 0)) {
444 if (atomic_cmpset_int(&ncp->nc_refs, refs, refs - 1))
451 * Link a new namecache entry to its parent. Be careful to avoid races
452 * if vhold() blocks in the future.
454 * MPSAFE - ncp must be locked and vfs_token must be held.
457 _cache_link_parent(struct namecache *ncp, struct namecache *par)
459 KKASSERT(ncp->nc_parent == NULL);
460 ncp->nc_parent = par;
461 if (TAILQ_EMPTY(&par->nc_list)) {
462 TAILQ_INSERT_HEAD(&par->nc_list, ncp, nc_entry);
464 * Any vp associated with an ncp which has children must
465 * be held to prevent it from being recycled.
468 vhold(par->nc_vp); /* MPSAFE */
470 TAILQ_INSERT_HEAD(&par->nc_list, ncp, nc_entry);
475 * Remove the parent association from a namecache structure. If this is
476 * the last child of the parent the cache_drop(par) will attempt to
477 * recursively zap the parent.
479 * MPSAFE - ncp must be locked and vfs_token must be held.
482 _cache_unlink_parent(struct namecache *ncp)
484 struct namecache *par;
485 struct vnode *dropvp;
487 if ((par = ncp->nc_parent) != NULL) {
488 ncp->nc_parent = NULL;
490 TAILQ_REMOVE(&par->nc_list, ncp, nc_entry);
492 if (par->nc_vp && TAILQ_EMPTY(&par->nc_list))
497 * We can only safely vdrop with no spinlocks held.
505 * Allocate a new namecache structure. Most of the code does not require
506 * zero-termination of the string but it makes vop_compat_ncreate() easier.
508 static struct namecache *
509 cache_alloc(int nlen)
511 struct namecache *ncp;
513 ncp = kmalloc(sizeof(*ncp), M_VFSCACHE, M_WAITOK|M_ZERO);
515 ncp->nc_name = kmalloc(nlen + 1, M_VFSCACHE, M_WAITOK);
517 ncp->nc_flag = NCF_UNRESOLVED;
518 ncp->nc_error = ENOTCONN; /* needs to be resolved */
521 TAILQ_INIT(&ncp->nc_list);
527 * Can only be called for the case where the ncp has never been
528 * associated with anything (so no spinlocks are needed).
531 _cache_free(struct namecache *ncp)
533 KKASSERT(ncp->nc_refs == 1 && ncp->nc_exlocks == 1);
535 kfree(ncp->nc_name, M_VFSCACHE);
536 kfree(ncp, M_VFSCACHE);
540 cache_zero(struct nchandle *nch)
547 * Ref and deref a namecache structure.
549 * Warning: caller may hold an unrelated read spinlock, which means we can't
550 * use read spinlocks here.
555 cache_hold(struct nchandle *nch)
557 _cache_hold(nch->ncp);
558 atomic_add_int(&nch->mount->mnt_refs, 1);
563 * Create a copy of a namecache handle for an already-referenced
569 cache_copy(struct nchandle *nch, struct nchandle *target)
572 _cache_hold(target->ncp);
573 atomic_add_int(&nch->mount->mnt_refs, 1);
580 cache_changemount(struct nchandle *nch, struct mount *mp)
582 atomic_add_int(&nch->mount->mnt_refs, -1);
584 atomic_add_int(&nch->mount->mnt_refs, 1);
588 cache_drop(struct nchandle *nch)
590 atomic_add_int(&nch->mount->mnt_refs, -1);
591 _cache_drop(nch->ncp);
597 cache_lock(struct nchandle *nch)
599 _cache_lock(nch->ncp);
603 cache_lock_nonblock(struct nchandle *nch)
605 return(_cache_lock_nonblock(nch->ncp));
610 cache_unlock(struct nchandle *nch)
612 _cache_unlock(nch->ncp);
616 * ref-and-lock, unlock-and-deref functions.
618 * This function is primarily used by nlookup. Even though cache_lock
619 * holds the vnode, it is possible that the vnode may have already
620 * initiated a recyclement.
622 * We want cache_get() to return a definitively usable vnode or a
623 * definitively unresolved ncp.
627 _cache_get(struct namecache *ncp)
631 if (ncp->nc_vp && (ncp->nc_vp->v_flag & VRECLAIMED))
632 _cache_setunresolved(ncp);
637 * This is a special form of _cache_get() which only succeeds if
638 * it can get a pristine, non-recursive lock. The caller must have
639 * already ref'd the ncp.
641 * On success the ncp will be locked, on failure it will not. The
642 * ref count does not change either way.
644 * We want _cache_get_nonblock() (on success) to return a definitively
645 * usable vnode or a definitively unresolved ncp.
648 _cache_get_nonblock(struct namecache *ncp)
650 if (_cache_lock_nonblock(ncp) == 0) {
651 if (ncp->nc_exlocks == 1) {
652 if (ncp->nc_vp && (ncp->nc_vp->v_flag & VRECLAIMED))
653 _cache_setunresolved(ncp);
663 * NOTE: The same nchandle can be passed for both arguments.
666 cache_get(struct nchandle *nch, struct nchandle *target)
668 KKASSERT(nch->ncp->nc_refs > 0);
669 target->mount = nch->mount;
670 target->ncp = _cache_get(nch->ncp);
671 atomic_add_int(&target->mount->mnt_refs, 1);
676 cache_get_nonblock(struct nchandle *nch)
680 if ((error = _cache_get_nonblock(nch->ncp)) == 0)
681 atomic_add_int(&nch->mount->mnt_refs, 1);
688 _cache_put(struct namecache *ncp)
695 cache_put(struct nchandle *nch)
697 atomic_add_int(&nch->mount->mnt_refs, -1);
698 _cache_put(nch->ncp);
704 * Resolve an unresolved ncp by associating a vnode with it. If the
705 * vnode is NULL, a negative cache entry is created.
707 * The ncp should be locked on entry and will remain locked on return.
711 _cache_setvp(struct mount *mp, struct namecache *ncp, struct vnode *vp)
713 KKASSERT(ncp->nc_flag & NCF_UNRESOLVED);
716 * Any vp associated with an ncp which has children must
717 * be held. Any vp associated with a locked ncp must be held.
719 if (!TAILQ_EMPTY(&ncp->nc_list))
721 spin_lock_wr(&vp->v_spinlock);
723 TAILQ_INSERT_HEAD(&vp->v_namecache, ncp, nc_vnode);
724 spin_unlock_wr(&vp->v_spinlock);
729 * Set auxiliary flags
733 ncp->nc_flag |= NCF_ISDIR;
736 ncp->nc_flag |= NCF_ISSYMLINK;
737 /* XXX cache the contents of the symlink */
742 atomic_add_int(&numcache, 1);
746 * When creating a negative cache hit we set the
747 * namecache_gen. A later resolve will clean out the
748 * negative cache hit if the mount point's namecache_gen
749 * has changed. Used by devfs, could also be used by
753 spin_lock_wr(&ncspin);
754 lwkt_token_init(&vfs_token);
755 TAILQ_INSERT_TAIL(&ncneglist, ncp, nc_vnode);
757 spin_unlock_wr(&ncspin);
758 ncp->nc_error = ENOENT;
760 ncp->nc_namecache_gen = mp->mnt_namecache_gen;
762 ncp->nc_flag &= ~NCF_UNRESOLVED;
766 cache_setvp(struct nchandle *nch, struct vnode *vp)
768 _cache_setvp(nch->mount, nch->ncp, vp);
772 cache_settimeout(struct nchandle *nch, int nticks)
774 struct namecache *ncp = nch->ncp;
776 if ((ncp->nc_timeout = ticks + nticks) == 0)
781 * Disassociate the vnode or negative-cache association and mark a
782 * namecache entry as unresolved again. Note that the ncp is still
783 * left in the hash table and still linked to its parent.
785 * The ncp should be locked and refd on entry and will remain locked and refd
788 * This routine is normally never called on a directory containing children.
789 * However, NFS often does just that in its rename() code as a cop-out to
790 * avoid complex namespace operations. This disconnects a directory vnode
791 * from its namecache and can cause the OLDAPI and NEWAPI to get out of
796 _cache_setunresolved(struct namecache *ncp)
800 if ((ncp->nc_flag & NCF_UNRESOLVED) == 0) {
801 ncp->nc_flag |= NCF_UNRESOLVED;
803 ncp->nc_error = ENOTCONN;
804 atomic_add_int(&numunres, 1);
805 if ((vp = ncp->nc_vp) != NULL) {
806 atomic_add_int(&numcache, -1);
807 spin_lock_wr(&vp->v_spinlock);
809 TAILQ_REMOVE(&vp->v_namecache, ncp, nc_vnode);
810 spin_unlock_wr(&vp->v_spinlock);
813 * Any vp associated with an ncp with children is
814 * held by that ncp. Any vp associated with a locked
815 * ncp is held by that ncp. These conditions must be
816 * undone when the vp is cleared out from the ncp.
818 if (!TAILQ_EMPTY(&ncp->nc_list))
823 spin_lock_wr(&ncspin);
824 TAILQ_REMOVE(&ncneglist, ncp, nc_vnode);
826 spin_unlock_wr(&ncspin);
828 ncp->nc_flag &= ~(NCF_WHITEOUT|NCF_ISDIR|NCF_ISSYMLINK);
833 * The cache_nresolve() code calls this function to automatically
834 * set a resolved cache element to unresolved if it has timed out
835 * or if it is a negative cache hit and the mount point namecache_gen
839 _cache_auto_unresolve(struct mount *mp, struct namecache *ncp)
842 * Already in an unresolved state, nothing to do.
844 if (ncp->nc_flag & NCF_UNRESOLVED)
848 * Try to zap entries that have timed out. We have
849 * to be careful here because locked leafs may depend
850 * on the vnode remaining intact in a parent, so only
851 * do this under very specific conditions.
853 if (ncp->nc_timeout && (int)(ncp->nc_timeout - ticks) < 0 &&
854 TAILQ_EMPTY(&ncp->nc_list)) {
855 _cache_setunresolved(ncp);
860 * If a resolved negative cache hit is invalid due to
861 * the mount's namecache generation being bumped, zap it.
863 if (ncp->nc_vp == NULL &&
864 ncp->nc_namecache_gen != mp->mnt_namecache_gen) {
865 _cache_setunresolved(ncp);
871 cache_setunresolved(struct nchandle *nch)
873 _cache_setunresolved(nch->ncp);
877 * Determine if we can clear NCF_ISMOUNTPT by scanning the mountlist
878 * looking for matches. This flag tells the lookup code when it must
879 * check for a mount linkage and also prevents the directories in question
880 * from being deleted or renamed.
884 cache_clrmountpt_callback(struct mount *mp, void *data)
886 struct nchandle *nch = data;
888 if (mp->mnt_ncmounton.ncp == nch->ncp)
890 if (mp->mnt_ncmountpt.ncp == nch->ncp)
896 cache_clrmountpt(struct nchandle *nch)
900 count = mountlist_scan(cache_clrmountpt_callback, nch,
901 MNTSCAN_FORWARD|MNTSCAN_NOBUSY);
903 nch->ncp->nc_flag &= ~NCF_ISMOUNTPT;
907 * Invalidate portions of the namecache topology given a starting entry.
908 * The passed ncp is set to an unresolved state and:
910 * The passed ncp must be locked.
912 * CINV_DESTROY - Set a flag in the passed ncp entry indicating
913 * that the physical underlying nodes have been
914 * destroyed... as in deleted. For example, when
915 * a directory is removed. This will cause record
916 * lookups on the name to no longer be able to find
917 * the record and tells the resolver to return failure
918 * rather then trying to resolve through the parent.
920 * The topology itself, including ncp->nc_name,
923 * This only applies to the passed ncp, if CINV_CHILDREN
924 * is specified the children are not flagged.
926 * CINV_CHILDREN - Set all children (recursively) to an unresolved
929 * Note that this will also have the side effect of
930 * cleaning out any unreferenced nodes in the topology
931 * from the leaves up as the recursion backs out.
933 * Note that the topology for any referenced nodes remains intact.
935 * It is possible for cache_inval() to race a cache_resolve(), meaning that
936 * the namecache entry may not actually be invalidated on return if it was
937 * revalidated while recursing down into its children. This code guarentees
938 * that the node(s) will go through an invalidation cycle, but does not
939 * guarentee that they will remain in an invalidated state.
941 * Returns non-zero if a revalidation was detected during the invalidation
942 * recursion, zero otherwise. Note that since only the original ncp is
943 * locked the revalidation ultimately can only indicate that the original ncp
944 * *MIGHT* no have been reresolved.
946 * DEEP RECURSION HANDLING - If a recursive invalidation recurses deeply we
947 * have to avoid blowing out the kernel stack. We do this by saving the
948 * deep namecache node and aborting the recursion, then re-recursing at that
949 * node using a depth-first algorithm in order to allow multiple deep
950 * recursions to chain through each other, then we restart the invalidation
955 struct namecache *resume_ncp;
959 static int _cache_inval_internal(struct namecache *, int, struct cinvtrack *);
963 _cache_inval(struct namecache *ncp, int flags)
965 struct cinvtrack track;
966 struct namecache *ncp2;
970 track.resume_ncp = NULL;
973 r = _cache_inval_internal(ncp, flags, &track);
974 if (track.resume_ncp == NULL)
976 kprintf("Warning: deep namecache recursion at %s\n",
979 while ((ncp2 = track.resume_ncp) != NULL) {
980 track.resume_ncp = NULL;
982 _cache_inval_internal(ncp2, flags & ~CINV_DESTROY,
992 cache_inval(struct nchandle *nch, int flags)
994 return(_cache_inval(nch->ncp, flags));
998 _cache_inval_internal(struct namecache *ncp, int flags, struct cinvtrack *track)
1000 struct namecache *kid;
1001 struct namecache *nextkid;
1005 KKASSERT(ncp->nc_exlocks);
1007 _cache_setunresolved(ncp);
1008 lwkt_gettoken(&nlock, &vfs_token);
1009 if (flags & CINV_DESTROY)
1010 ncp->nc_flag |= NCF_DESTROYED;
1011 if ((flags & CINV_CHILDREN) &&
1012 (kid = TAILQ_FIRST(&ncp->nc_list)) != NULL
1015 if (++track->depth > MAX_RECURSION_DEPTH) {
1016 track->resume_ncp = ncp;
1022 if (track->resume_ncp) {
1026 if ((nextkid = TAILQ_NEXT(kid, nc_entry)) != NULL)
1027 _cache_hold(nextkid);
1028 if ((kid->nc_flag & NCF_UNRESOLVED) == 0 ||
1029 TAILQ_FIRST(&kid->nc_list)
1032 rcnt += _cache_inval_internal(kid, flags & ~CINV_DESTROY, track);
1041 lwkt_reltoken(&nlock);
1044 * Someone could have gotten in there while ncp was unlocked,
1047 if ((ncp->nc_flag & NCF_UNRESOLVED) == 0)
1053 * Invalidate a vnode's namecache associations. To avoid races against
1054 * the resolver we do not invalidate a node which we previously invalidated
1055 * but which was then re-resolved while we were in the invalidation loop.
1057 * Returns non-zero if any namecache entries remain after the invalidation
1060 * NOTE: Unlike the namecache topology which guarentees that ncp's will not
1061 * be ripped out of the topology while held, the vnode's v_namecache
1062 * list has no such restriction. NCP's can be ripped out of the list
1063 * at virtually any time if not locked, even if held.
1065 * In addition, the v_namecache list itself must be locked via
1066 * the vnode's spinlock.
1069 cache_inval_vp(struct vnode *vp, int flags)
1071 struct namecache *ncp;
1072 struct namecache *next;
1075 spin_lock_wr(&vp->v_spinlock);
1076 ncp = TAILQ_FIRST(&vp->v_namecache);
1080 /* loop entered with ncp held and vp spin-locked */
1081 if ((next = TAILQ_NEXT(ncp, nc_vnode)) != NULL)
1083 spin_unlock_wr(&vp->v_spinlock);
1085 if (ncp->nc_vp != vp) {
1086 kprintf("Warning: cache_inval_vp: race-A detected on "
1087 "%s\n", ncp->nc_name);
1093 _cache_inval(ncp, flags);
1094 _cache_put(ncp); /* also releases reference */
1096 if (ncp && ncp->nc_vp != vp) {
1097 kprintf("Warning: cache_inval_vp: race-B detected on "
1098 "%s\n", ncp->nc_name);
1102 spin_lock_wr(&vp->v_spinlock);
1104 spin_unlock_wr(&vp->v_spinlock);
1105 return(TAILQ_FIRST(&vp->v_namecache) != NULL);
1109 * This routine is used instead of the normal cache_inval_vp() when we
1110 * are trying to recycle otherwise good vnodes.
1112 * Return 0 on success, non-zero if not all namecache records could be
1113 * disassociated from the vnode (for various reasons).
1116 cache_inval_vp_nonblock(struct vnode *vp)
1118 struct namecache *ncp;
1119 struct namecache *next;
1121 spin_lock_wr(&vp->v_spinlock);
1122 ncp = TAILQ_FIRST(&vp->v_namecache);
1126 /* loop entered with ncp held */
1127 if ((next = TAILQ_NEXT(ncp, nc_vnode)) != NULL)
1129 spin_unlock_wr(&vp->v_spinlock);
1130 if (_cache_lock_nonblock(ncp)) {
1136 if (ncp->nc_vp != vp) {
1137 kprintf("Warning: cache_inval_vp: race-A detected on "
1138 "%s\n", ncp->nc_name);
1144 _cache_inval(ncp, 0);
1145 _cache_put(ncp); /* also releases reference */
1147 if (ncp && ncp->nc_vp != vp) {
1148 kprintf("Warning: cache_inval_vp: race-B detected on "
1149 "%s\n", ncp->nc_name);
1153 spin_lock_wr(&vp->v_spinlock);
1155 spin_unlock_wr(&vp->v_spinlock);
1156 return(TAILQ_FIRST(&vp->v_namecache) != NULL);
1160 * The source ncp has been renamed to the target ncp. Both fncp and tncp
1161 * must be locked. The target ncp is destroyed (as a normal rename-over
1162 * would destroy the target file or directory).
1164 * Because there may be references to the source ncp we cannot copy its
1165 * contents to the target. Instead the source ncp is relinked as the target
1166 * and the target ncp is removed from the namecache topology.
1169 cache_rename(struct nchandle *fnch, struct nchandle *tnch)
1171 struct namecache *fncp = fnch->ncp;
1172 struct namecache *tncp = tnch->ncp;
1176 lwkt_gettoken(&nlock, &vfs_token);
1177 _cache_setunresolved(tncp);
1178 _cache_unlink_parent(fncp);
1179 _cache_link_parent(fncp, tncp->nc_parent);
1180 _cache_unlink_parent(tncp);
1181 oname = fncp->nc_name;
1182 fncp->nc_name = tncp->nc_name;
1183 fncp->nc_nlen = tncp->nc_nlen;
1184 tncp->nc_name = NULL;
1187 _cache_rehash(fncp);
1189 _cache_rehash(tncp);
1190 lwkt_reltoken(&nlock);
1193 kfree(oname, M_VFSCACHE);
1197 * vget the vnode associated with the namecache entry. Resolve the namecache
1198 * entry if necessary and deal with namecache/vp races. The passed ncp must
1199 * be referenced and may be locked. The ncp's ref/locking state is not
1200 * effected by this call.
1202 * lk_type may be LK_SHARED, LK_EXCLUSIVE. A ref'd, possibly locked
1203 * (depending on the passed lk_type) will be returned in *vpp with an error
1204 * of 0, or NULL will be returned in *vpp with a non-0 error code. The
1205 * most typical error is ENOENT, meaning that the ncp represents a negative
1206 * cache hit and there is no vnode to retrieve, but other errors can occur
1209 * The main race we have to deal with are namecache zaps. The ncp itself
1210 * will not disappear since it is referenced, and it turns out that the
1211 * validity of the vp pointer can be checked simply by rechecking the
1212 * contents of ncp->nc_vp.
1215 cache_vget(struct nchandle *nch, struct ucred *cred,
1216 int lk_type, struct vnode **vpp)
1218 struct namecache *ncp;
1225 if (ncp->nc_flag & NCF_UNRESOLVED) {
1227 error = cache_resolve(nch, cred);
1232 if (error == 0 && (vp = ncp->nc_vp) != NULL) {
1234 * Accessing the vnode from the namecache is a bit
1235 * dangerous. Because there are no refs on the vnode, it
1236 * could be in the middle of a reclaim.
1238 if (vp->v_flag & VRECLAIMED) {
1239 kprintf("Warning: vnode reclaim race detected in cache_vget on %p (%s)\n", vp, ncp->nc_name);
1241 _cache_setunresolved(ncp);
1245 error = vget(vp, lk_type);
1247 if (vp != ncp->nc_vp)
1250 } else if (vp != ncp->nc_vp) {
1253 } else if (vp->v_flag & VRECLAIMED) {
1254 panic("vget succeeded on a VRECLAIMED node! vp %p", vp);
1257 if (error == 0 && vp == NULL)
1264 cache_vref(struct nchandle *nch, struct ucred *cred, struct vnode **vpp)
1266 struct namecache *ncp;
1274 if (ncp->nc_flag & NCF_UNRESOLVED) {
1276 error = cache_resolve(nch, cred);
1281 if (error == 0 && (vp = ncp->nc_vp) != NULL) {
1283 * Since we did not obtain any locks, a cache zap
1284 * race can occur here if the vnode is in the middle
1285 * of being reclaimed and has not yet been able to
1286 * clean out its cache node. If that case occurs,
1287 * we must lock and unresolve the cache, then loop
1290 if ((error = vget(vp, LK_SHARED)) != 0) {
1291 if (error == ENOENT) {
1292 kprintf("Warning: vnode reclaim race detected on cache_vref %p (%s)\n", vp, ncp->nc_name);
1294 _cache_setunresolved(ncp);
1300 /* caller does not want a lock */
1304 if (error == 0 && vp == NULL)
1311 * Return a referenced vnode representing the parent directory of
1314 * Because the caller has locked the ncp it should not be possible for
1315 * the parent ncp to go away. However, the parent can unresolve its
1316 * dvp at any time so we must be able to acquire a lock on the parent
1317 * to safely access nc_vp.
1319 * We have to leave par unlocked when vget()ing dvp to avoid a deadlock,
1320 * so use vhold()/vdrop() while holding the lock to prevent dvp from
1321 * getting destroyed.
1323 static struct vnode *
1324 cache_dvpref(struct namecache *ncp)
1326 struct namecache *par;
1330 if ((par = ncp->nc_parent) != NULL) {
1332 if (_cache_lock_nonblock(par) == 0) {
1333 if ((par->nc_flag & NCF_UNRESOLVED) == 0) {
1334 if ((dvp = par->nc_vp) != NULL)
1339 if (vget(dvp, LK_SHARED) == 0) {
1342 /* return refd, unlocked dvp */
1355 * Convert a directory vnode to a namecache record without any other
1356 * knowledge of the topology. This ONLY works with directory vnodes and
1357 * is ONLY used by the NFS server. dvp must be refd but unlocked, and the
1358 * returned ncp (if not NULL) will be held and unlocked.
1360 * If 'makeit' is 0 and dvp has no existing namecache record, NULL is returned.
1361 * If 'makeit' is 1 we attempt to track-down and create the namecache topology
1362 * for dvp. This will fail only if the directory has been deleted out from
1365 * Callers must always check for a NULL return no matter the value of 'makeit'.
1367 * To avoid underflowing the kernel stack each recursive call increments
1368 * the makeit variable.
1371 static int cache_inefficient_scan(struct nchandle *nch, struct ucred *cred,
1372 struct vnode *dvp, char *fakename);
1373 static int cache_fromdvp_try(struct vnode *dvp, struct ucred *cred,
1374 struct vnode **saved_dvp);
1377 cache_fromdvp(struct vnode *dvp, struct ucred *cred, int makeit,
1378 struct nchandle *nch)
1380 struct vnode *saved_dvp;
1386 nch->mount = dvp->v_mount;
1391 * Loop until resolution, inside code will break out on error.
1395 * Break out if we successfully acquire a working ncp.
1397 spin_lock_wr(&dvp->v_spinlock);
1398 nch->ncp = TAILQ_FIRST(&dvp->v_namecache);
1401 spin_unlock_wr(&dvp->v_spinlock);
1404 spin_unlock_wr(&dvp->v_spinlock);
1407 * If dvp is the root of its filesystem it should already
1408 * have a namecache pointer associated with it as a side
1409 * effect of the mount, but it may have been disassociated.
1411 if (dvp->v_flag & VROOT) {
1412 nch->ncp = _cache_get(nch->mount->mnt_ncmountpt.ncp);
1413 error = cache_resolve_mp(nch->mount);
1414 _cache_put(nch->ncp);
1416 kprintf("cache_fromdvp: resolve root of mount %p error %d",
1417 dvp->v_mount, error);
1421 kprintf(" failed\n");
1426 kprintf(" succeeded\n");
1431 * If we are recursed too deeply resort to an O(n^2)
1432 * algorithm to resolve the namecache topology. The
1433 * resolved pvp is left referenced in saved_dvp to
1434 * prevent the tree from being destroyed while we loop.
1437 error = cache_fromdvp_try(dvp, cred, &saved_dvp);
1439 kprintf("lookupdotdot(longpath) failed %d "
1440 "dvp %p\n", error, dvp);
1448 * Get the parent directory and resolve its ncp.
1451 kfree(fakename, M_TEMP);
1454 error = vop_nlookupdotdot(*dvp->v_ops, dvp, &pvp, cred,
1457 kprintf("lookupdotdot failed %d dvp %p\n", error, dvp);
1463 * Reuse makeit as a recursion depth counter. On success
1464 * nch will be fully referenced.
1466 cache_fromdvp(pvp, cred, makeit + 1, nch);
1468 if (nch->ncp == NULL)
1472 * Do an inefficient scan of pvp (embodied by ncp) to look
1473 * for dvp. This will create a namecache record for dvp on
1474 * success. We loop up to recheck on success.
1476 * ncp and dvp are both held but not locked.
1478 error = cache_inefficient_scan(nch, cred, dvp, fakename);
1480 kprintf("cache_fromdvp: scan %p (%s) failed on dvp=%p\n",
1481 pvp, nch->ncp->nc_name, dvp);
1483 /* nch was NULLed out, reload mount */
1484 nch->mount = dvp->v_mount;
1488 kprintf("cache_fromdvp: scan %p (%s) succeeded\n",
1489 pvp, nch->ncp->nc_name);
1492 /* nch was NULLed out, reload mount */
1493 nch->mount = dvp->v_mount;
1497 * If nch->ncp is non-NULL it will have been held already.
1500 kfree(fakename, M_TEMP);
1509 * Go up the chain of parent directories until we find something
1510 * we can resolve into the namecache. This is very inefficient.
1514 cache_fromdvp_try(struct vnode *dvp, struct ucred *cred,
1515 struct vnode **saved_dvp)
1517 struct nchandle nch;
1520 static time_t last_fromdvp_report;
1524 * Loop getting the parent directory vnode until we get something we
1525 * can resolve in the namecache.
1528 nch.mount = dvp->v_mount;
1534 kfree(fakename, M_TEMP);
1537 error = vop_nlookupdotdot(*dvp->v_ops, dvp, &pvp, cred,
1544 spin_lock_wr(&pvp->v_spinlock);
1545 if ((nch.ncp = TAILQ_FIRST(&pvp->v_namecache)) != NULL) {
1546 _cache_hold(nch.ncp);
1547 spin_unlock_wr(&pvp->v_spinlock);
1551 spin_unlock_wr(&pvp->v_spinlock);
1552 if (pvp->v_flag & VROOT) {
1553 nch.ncp = _cache_get(pvp->v_mount->mnt_ncmountpt.ncp);
1554 error = cache_resolve_mp(nch.mount);
1555 _cache_unlock(nch.ncp);
1558 _cache_drop(nch.ncp);
1568 if (last_fromdvp_report != time_second) {
1569 last_fromdvp_report = time_second;
1570 kprintf("Warning: extremely inefficient path "
1571 "resolution on %s\n",
1574 error = cache_inefficient_scan(&nch, cred, dvp, fakename);
1577 * Hopefully dvp now has a namecache record associated with
1578 * it. Leave it referenced to prevent the kernel from
1579 * recycling the vnode. Otherwise extremely long directory
1580 * paths could result in endless recycling.
1585 _cache_drop(nch.ncp);
1588 kfree(fakename, M_TEMP);
1593 * Do an inefficient scan of the directory represented by ncp looking for
1594 * the directory vnode dvp. ncp must be held but not locked on entry and
1595 * will be held on return. dvp must be refd but not locked on entry and
1596 * will remain refd on return.
1598 * Why do this at all? Well, due to its stateless nature the NFS server
1599 * converts file handles directly to vnodes without necessarily going through
1600 * the namecache ops that would otherwise create the namecache topology
1601 * leading to the vnode. We could either (1) Change the namecache algorithms
1602 * to allow disconnect namecache records that are re-merged opportunistically,
1603 * or (2) Make the NFS server backtrack and scan to recover a connected
1604 * namecache topology in order to then be able to issue new API lookups.
1606 * It turns out that (1) is a huge mess. It takes a nice clean set of
1607 * namecache algorithms and introduces a lot of complication in every subsystem
1608 * that calls into the namecache to deal with the re-merge case, especially
1609 * since we are using the namecache to placehold negative lookups and the
1610 * vnode might not be immediately assigned. (2) is certainly far less
1611 * efficient then (1), but since we are only talking about directories here
1612 * (which are likely to remain cached), the case does not actually run all
1613 * that often and has the supreme advantage of not polluting the namecache
1616 * If a fakename is supplied just construct a namecache entry using the
1620 cache_inefficient_scan(struct nchandle *nch, struct ucred *cred,
1621 struct vnode *dvp, char *fakename)
1623 struct nlcomponent nlc;
1624 struct nchandle rncp;
1636 vat.va_blocksize = 0;
1637 if ((error = VOP_GETATTR(dvp, &vat)) != 0)
1639 if ((error = cache_vref(nch, cred, &pvp)) != 0)
1642 kprintf("inefficient_scan: directory iosize %ld "
1643 "vattr fileid = %lld\n",
1645 (long long)vat.va_fileid);
1649 * Use the supplied fakename if not NULL. Fake names are typically
1650 * not in the actual filesystem hierarchy. This is used by HAMMER
1651 * to glue @@timestamp recursions together.
1654 nlc.nlc_nameptr = fakename;
1655 nlc.nlc_namelen = strlen(fakename);
1656 rncp = cache_nlookup(nch, &nlc);
1660 if ((blksize = vat.va_blocksize) == 0)
1661 blksize = DEV_BSIZE;
1662 rbuf = kmalloc(blksize, M_TEMP, M_WAITOK);
1668 iov.iov_base = rbuf;
1669 iov.iov_len = blksize;
1672 uio.uio_resid = blksize;
1673 uio.uio_segflg = UIO_SYSSPACE;
1674 uio.uio_rw = UIO_READ;
1675 uio.uio_td = curthread;
1677 if (ncvp_debug >= 2)
1678 kprintf("cache_inefficient_scan: readdir @ %08x\n", (int)uio.uio_offset);
1679 error = VOP_READDIR(pvp, &uio, cred, &eofflag, NULL, NULL);
1681 den = (struct dirent *)rbuf;
1682 bytes = blksize - uio.uio_resid;
1685 if (ncvp_debug >= 2) {
1686 kprintf("cache_inefficient_scan: %*.*s\n",
1687 den->d_namlen, den->d_namlen,
1690 if (den->d_type != DT_WHT &&
1691 den->d_ino == vat.va_fileid) {
1693 kprintf("cache_inefficient_scan: "
1694 "MATCHED inode %lld path %s/%*.*s\n",
1695 (long long)vat.va_fileid,
1697 den->d_namlen, den->d_namlen,
1700 nlc.nlc_nameptr = den->d_name;
1701 nlc.nlc_namelen = den->d_namlen;
1702 rncp = cache_nlookup(nch, &nlc);
1703 KKASSERT(rncp.ncp != NULL);
1706 bytes -= _DIRENT_DIRSIZ(den);
1707 den = _DIRENT_NEXT(den);
1709 if (rncp.ncp == NULL && eofflag == 0 && uio.uio_resid != blksize)
1712 kfree(rbuf, M_TEMP);
1716 if (rncp.ncp->nc_flag & NCF_UNRESOLVED) {
1717 _cache_setvp(rncp.mount, rncp.ncp, dvp);
1718 if (ncvp_debug >= 2) {
1719 kprintf("cache_inefficient_scan: setvp %s/%s = %p\n",
1720 nch->ncp->nc_name, rncp.ncp->nc_name, dvp);
1723 if (ncvp_debug >= 2) {
1724 kprintf("cache_inefficient_scan: setvp %s/%s already set %p/%p\n",
1725 nch->ncp->nc_name, rncp.ncp->nc_name, dvp,
1729 if (rncp.ncp->nc_vp == NULL)
1730 error = rncp.ncp->nc_error;
1732 * Release rncp after a successful nlookup. rncp was fully
1737 kprintf("cache_inefficient_scan: dvp %p NOT FOUND in %s\n",
1738 dvp, nch->ncp->nc_name);
1745 * Zap a namecache entry. The ncp is unconditionally set to an unresolved
1746 * state, which disassociates it from its vnode or ncneglist.
1748 * Then, if there are no additional references to the ncp and no children,
1749 * the ncp is removed from the topology and destroyed.
1751 * References and/or children may exist if the ncp is in the middle of the
1752 * topology, preventing the ncp from being destroyed.
1754 * This function must be called with the ncp held and locked and will unlock
1755 * and drop it during zapping.
1757 * This function may returned a held (but NOT locked) parent node which the
1758 * caller must drop. We do this so _cache_drop() can loop, to avoid
1759 * blowing out the kernel stack.
1761 * WARNING! For MPSAFE operation this routine must acquire up to three
1762 * spin locks to be able to safely test nc_refs. Lock order is
1765 * hash spinlock if on hash list
1766 * parent spinlock if child of parent
1767 * (the ncp is unresolved so there is no vnode association)
1769 static struct namecache *
1770 cache_zap(struct namecache *ncp)
1772 struct namecache *par;
1773 struct spinlock *hspin;
1774 struct vnode *dropvp;
1779 * Disassociate the vnode or negative cache ref and set NCF_UNRESOLVED.
1781 _cache_setunresolved(ncp);
1784 * Try to scrap the entry and possibly tail-recurse on its parent.
1785 * We only scrap unref'd (other then our ref) unresolved entries,
1786 * we do not scrap 'live' entries.
1788 * Note that once the spinlocks are acquired if nc_refs == 1 no
1789 * other references are possible. If it isn't, however, we have
1790 * to decrement but also be sure to avoid a 1->0 transition.
1792 KKASSERT(ncp->nc_flag & NCF_UNRESOLVED);
1793 KKASSERT(ncp->nc_refs > 0);
1798 lwkt_gettoken(&nlock, &vfs_token);
1801 hspin = &ncp->nc_head->spin;
1802 spin_lock_wr(hspin);
1806 * If someone other then us has a ref or we have children
1807 * we cannot zap the entry. The 1->0 transition and any
1808 * further list operation is protected by the spinlocks
1809 * we have acquired but other transitions are not.
1812 refs = ncp->nc_refs;
1813 if (refs == 1 && TAILQ_EMPTY(&ncp->nc_list))
1815 if (atomic_cmpset_int(&ncp->nc_refs, refs, refs - 1)) {
1817 spin_unlock_wr(hspin);
1818 lwkt_reltoken(&nlock);
1825 * We are the only ref and with the spinlocks held no further
1826 * refs can be acquired by others.
1828 * Remove us from the hash list and parent list. We have to
1829 * drop a ref on the parent's vp if the parent's list becomes
1833 LIST_REMOVE(ncp, nc_hash);
1834 ncp->nc_head = NULL;
1837 if ((par = ncp->nc_parent) != NULL) {
1838 par = _cache_hold(par);
1839 TAILQ_REMOVE(&par->nc_list, ncp, nc_entry);
1840 ncp->nc_parent = NULL;
1842 if (par->nc_vp && TAILQ_EMPTY(&par->nc_list))
1843 dropvp = par->nc_vp;
1847 * ncp should not have picked up any refs. Physically
1851 spin_unlock_wr(hspin);
1852 lwkt_reltoken(&nlock);
1853 KKASSERT(ncp->nc_refs == 1);
1854 atomic_add_int(&numunres, -1);
1855 /* _cache_unlock(ncp) not required */
1856 ncp->nc_refs = -1; /* safety */
1858 kfree(ncp->nc_name, M_VFSCACHE);
1859 kfree(ncp, M_VFSCACHE);
1862 * Delayed drop (we had to release our spinlocks)
1864 * The refed parent (if not NULL) must be dropped. The
1865 * caller is responsible for looping.
1872 static enum { CHI_LOW, CHI_HIGH } cache_hysteresis_state = CHI_LOW;
1876 _cache_hysteresis(void)
1879 * Don't cache too many negative hits. We use hysteresis to reduce
1880 * the impact on the critical path.
1882 switch(cache_hysteresis_state) {
1884 if (numneg > MINNEG && numneg * ncnegfactor > numcache) {
1886 cache_hysteresis_state = CHI_HIGH;
1890 if (numneg > MINNEG * 9 / 10 &&
1891 numneg * ncnegfactor * 9 / 10 > numcache
1895 cache_hysteresis_state = CHI_LOW;
1902 * NEW NAMECACHE LOOKUP API
1904 * Lookup an entry in the cache. A locked, referenced, non-NULL
1905 * entry is *always* returned, even if the supplied component is illegal.
1906 * The resulting namecache entry should be returned to the system with
1907 * cache_put() or _cache_unlock() + cache_drop().
1909 * namecache locks are recursive but care must be taken to avoid lock order
1912 * Nobody else will be able to manipulate the associated namespace (e.g.
1913 * create, delete, rename, rename-target) until the caller unlocks the
1916 * The returned entry will be in one of three states: positive hit (non-null
1917 * vnode), negative hit (null vnode), or unresolved (NCF_UNRESOLVED is set).
1918 * Unresolved entries must be resolved through the filesystem to associate the
1919 * vnode and/or determine whether a positive or negative hit has occured.
1921 * It is not necessary to lock a directory in order to lock namespace under
1922 * that directory. In fact, it is explicitly not allowed to do that. A
1923 * directory is typically only locked when being created, renamed, or
1926 * The directory (par) may be unresolved, in which case any returned child
1927 * will likely also be marked unresolved. Likely but not guarenteed. Since
1928 * the filesystem lookup requires a resolved directory vnode the caller is
1929 * responsible for resolving the namecache chain top-down. This API
1930 * specifically allows whole chains to be created in an unresolved state.
1933 cache_nlookup(struct nchandle *par_nch, struct nlcomponent *nlc)
1935 struct nchandle nch;
1936 struct namecache *ncp;
1937 struct namecache *new_ncp;
1938 struct nchash_head *nchpp;
1946 mp = par_nch->mount;
1949 * Try to locate an existing entry
1951 hash = fnv_32_buf(nlc->nlc_nameptr, nlc->nlc_namelen, FNV1_32_INIT);
1952 hash = fnv_32_buf(&par_nch->ncp, sizeof(par_nch->ncp), hash);
1954 nchpp = NCHHASH(hash);
1956 spin_lock_wr(&nchpp->spin);
1957 LIST_FOREACH(ncp, &nchpp->list, nc_hash) {
1961 * Break out if we find a matching entry. Note that
1962 * UNRESOLVED entries may match, but DESTROYED entries
1965 if (ncp->nc_parent == par_nch->ncp &&
1966 ncp->nc_nlen == nlc->nlc_namelen &&
1967 bcmp(ncp->nc_name, nlc->nlc_nameptr, ncp->nc_nlen) == 0 &&
1968 (ncp->nc_flag & NCF_DESTROYED) == 0
1971 spin_unlock_wr(&nchpp->spin);
1972 if (_cache_get_nonblock(ncp) == 0) {
1973 _cache_auto_unresolve(mp, ncp);
1975 _cache_free(new_ncp);
1984 spin_unlock_wr(&nchpp->spin);
1987 * We failed to locate an entry, create a new entry and add it to
1988 * the cache. We have to relookup after possibly blocking in
1991 if (new_ncp == NULL) {
1992 new_ncp = cache_alloc(nlc->nlc_namelen);
1999 * Initialize as a new UNRESOLVED entry, lock (non-blocking),
2000 * and link to the parent. The mount point is usually inherited
2001 * from the parent unless this is a special case such as a mount
2002 * point where nlc_namelen is 0. If nlc_namelen is 0 nc_name will
2005 if (nlc->nlc_namelen) {
2006 bcopy(nlc->nlc_nameptr, ncp->nc_name, nlc->nlc_namelen);
2007 ncp->nc_name[nlc->nlc_namelen] = 0;
2009 nchpp = NCHHASH(hash); /* compiler optimization */
2010 spin_lock_wr(&nchpp->spin);
2011 LIST_INSERT_HEAD(&nchpp->list, ncp, nc_hash);
2012 ncp->nc_head = nchpp;
2013 spin_unlock_wr(&nchpp->spin);
2014 lwkt_gettoken(&nlock, &vfs_token);
2015 _cache_link_parent(ncp, par_nch->ncp);
2016 lwkt_reltoken(&nlock);
2019 * stats and namecache size management
2021 if (ncp->nc_flag & NCF_UNRESOLVED)
2022 ++gd->gd_nchstats->ncs_miss;
2023 else if (ncp->nc_vp)
2024 ++gd->gd_nchstats->ncs_goodhits;
2026 ++gd->gd_nchstats->ncs_neghits;
2027 _cache_hysteresis();
2030 atomic_add_int(&nch.mount->mnt_refs, 1);
2035 * The namecache entry is marked as being used as a mount point.
2036 * Locate the mount if it is visible to the caller.
2038 struct findmount_info {
2039 struct mount *result;
2040 struct mount *nch_mount;
2041 struct namecache *nch_ncp;
2046 cache_findmount_callback(struct mount *mp, void *data)
2048 struct findmount_info *info = data;
2051 * Check the mount's mounted-on point against the passed nch.
2053 if (mp->mnt_ncmounton.mount == info->nch_mount &&
2054 mp->mnt_ncmounton.ncp == info->nch_ncp
2063 cache_findmount(struct nchandle *nch)
2065 struct findmount_info info;
2068 info.nch_mount = nch->mount;
2069 info.nch_ncp = nch->ncp;
2070 mountlist_scan(cache_findmount_callback, &info,
2071 MNTSCAN_FORWARD|MNTSCAN_NOBUSY);
2072 return(info.result);
2076 * Resolve an unresolved namecache entry, generally by looking it up.
2077 * The passed ncp must be locked and refd.
2079 * Theoretically since a vnode cannot be recycled while held, and since
2080 * the nc_parent chain holds its vnode as long as children exist, the
2081 * direct parent of the cache entry we are trying to resolve should
2082 * have a valid vnode. If not then generate an error that we can
2083 * determine is related to a resolver bug.
2085 * However, if a vnode was in the middle of a recyclement when the NCP
2086 * got locked, ncp->nc_vp might point to a vnode that is about to become
2087 * invalid. cache_resolve() handles this case by unresolving the entry
2088 * and then re-resolving it.
2090 * Note that successful resolution does not necessarily return an error
2091 * code of 0. If the ncp resolves to a negative cache hit then ENOENT
2095 cache_resolve(struct nchandle *nch, struct ucred *cred)
2097 struct namecache *par;
2098 struct namecache *ncp;
2099 struct nchandle nctmp;
2108 * If the ncp is already resolved we have nothing to do. However,
2109 * we do want to guarentee that a usable vnode is returned when
2110 * a vnode is present, so make sure it hasn't been reclaimed.
2112 if ((ncp->nc_flag & NCF_UNRESOLVED) == 0) {
2113 if (ncp->nc_vp && (ncp->nc_vp->v_flag & VRECLAIMED))
2114 _cache_setunresolved(ncp);
2115 if ((ncp->nc_flag & NCF_UNRESOLVED) == 0)
2116 return (ncp->nc_error);
2120 * Mount points need special handling because the parent does not
2121 * belong to the same filesystem as the ncp.
2123 if (ncp == mp->mnt_ncmountpt.ncp)
2124 return (cache_resolve_mp(mp));
2127 * We expect an unbroken chain of ncps to at least the mount point,
2128 * and even all the way to root (but this code doesn't have to go
2129 * past the mount point).
2131 if (ncp->nc_parent == NULL) {
2132 kprintf("EXDEV case 1 %p %*.*s\n", ncp,
2133 ncp->nc_nlen, ncp->nc_nlen, ncp->nc_name);
2134 ncp->nc_error = EXDEV;
2135 return(ncp->nc_error);
2139 * The vp's of the parent directories in the chain are held via vhold()
2140 * due to the existance of the child, and should not disappear.
2141 * However, there are cases where they can disappear:
2143 * - due to filesystem I/O errors.
2144 * - due to NFS being stupid about tracking the namespace and
2145 * destroys the namespace for entire directories quite often.
2146 * - due to forced unmounts.
2147 * - due to an rmdir (parent will be marked DESTROYED)
2149 * When this occurs we have to track the chain backwards and resolve
2150 * it, looping until the resolver catches up to the current node. We
2151 * could recurse here but we might run ourselves out of kernel stack
2152 * so we do it in a more painful manner. This situation really should
2153 * not occur all that often, or if it does not have to go back too
2154 * many nodes to resolve the ncp.
2156 while ((dvp = cache_dvpref(ncp)) == NULL) {
2158 * This case can occur if a process is CD'd into a
2159 * directory which is then rmdir'd. If the parent is marked
2160 * destroyed there is no point trying to resolve it.
2162 if (ncp->nc_parent->nc_flag & NCF_DESTROYED)
2165 par = ncp->nc_parent;
2166 while (par->nc_parent && par->nc_parent->nc_vp == NULL)
2167 par = par->nc_parent;
2168 if (par->nc_parent == NULL) {
2169 kprintf("EXDEV case 2 %*.*s\n",
2170 par->nc_nlen, par->nc_nlen, par->nc_name);
2173 kprintf("[diagnostic] cache_resolve: had to recurse on %*.*s\n",
2174 par->nc_nlen, par->nc_nlen, par->nc_name);
2176 * The parent is not set in stone, ref and lock it to prevent
2177 * it from disappearing. Also note that due to renames it
2178 * is possible for our ncp to move and for par to no longer
2179 * be one of its parents. We resolve it anyway, the loop
2180 * will handle any moves.
2183 if (par == nch->mount->mnt_ncmountpt.ncp) {
2184 cache_resolve_mp(nch->mount);
2185 } else if ((dvp = cache_dvpref(par)) == NULL) {
2186 kprintf("[diagnostic] cache_resolve: raced on %*.*s\n", par->nc_nlen, par->nc_nlen, par->nc_name);
2190 if (par->nc_flag & NCF_UNRESOLVED) {
2193 par->nc_error = VOP_NRESOLVE(&nctmp, dvp, cred);
2197 if ((error = par->nc_error) != 0) {
2198 if (par->nc_error != EAGAIN) {
2199 kprintf("EXDEV case 3 %*.*s error %d\n",
2200 par->nc_nlen, par->nc_nlen, par->nc_name,
2205 kprintf("[diagnostic] cache_resolve: EAGAIN par %p %*.*s\n",
2206 par, par->nc_nlen, par->nc_nlen, par->nc_name);
2213 * Call VOP_NRESOLVE() to get the vp, then scan for any disconnected
2214 * ncp's and reattach them. If this occurs the original ncp is marked
2215 * EAGAIN to force a relookup.
2217 * NOTE: in order to call VOP_NRESOLVE(), the parent of the passed
2218 * ncp must already be resolved.
2223 ncp->nc_error = VOP_NRESOLVE(&nctmp, dvp, cred);
2226 ncp->nc_error = EPERM;
2228 if (ncp->nc_error == EAGAIN) {
2229 kprintf("[diagnostic] cache_resolve: EAGAIN ncp %p %*.*s\n",
2230 ncp, ncp->nc_nlen, ncp->nc_nlen, ncp->nc_name);
2233 return(ncp->nc_error);
2237 * Resolve the ncp associated with a mount point. Such ncp's almost always
2238 * remain resolved and this routine is rarely called. NFS MPs tends to force
2239 * re-resolution more often due to its mac-truck-smash-the-namecache
2240 * method of tracking namespace changes.
2242 * The semantics for this call is that the passed ncp must be locked on
2243 * entry and will be locked on return. However, if we actually have to
2244 * resolve the mount point we temporarily unlock the entry in order to
2245 * avoid race-to-root deadlocks due to e.g. dead NFS mounts. Because of
2246 * the unlock we have to recheck the flags after we relock.
2249 cache_resolve_mp(struct mount *mp)
2251 struct namecache *ncp = mp->mnt_ncmountpt.ncp;
2255 KKASSERT(mp != NULL);
2258 * If the ncp is already resolved we have nothing to do. However,
2259 * we do want to guarentee that a usable vnode is returned when
2260 * a vnode is present, so make sure it hasn't been reclaimed.
2262 if ((ncp->nc_flag & NCF_UNRESOLVED) == 0) {
2263 if (ncp->nc_vp && (ncp->nc_vp->v_flag & VRECLAIMED))
2264 _cache_setunresolved(ncp);
2267 if (ncp->nc_flag & NCF_UNRESOLVED) {
2269 while (vfs_busy(mp, 0))
2271 error = VFS_ROOT(mp, &vp);
2275 * recheck the ncp state after relocking.
2277 if (ncp->nc_flag & NCF_UNRESOLVED) {
2278 ncp->nc_error = error;
2280 _cache_setvp(mp, ncp, vp);
2283 kprintf("[diagnostic] cache_resolve_mp: failed"
2284 " to resolve mount %p err=%d ncp=%p\n",
2286 _cache_setvp(mp, ncp, NULL);
2288 } else if (error == 0) {
2293 return(ncp->nc_error);
2300 cache_cleanneg(int count)
2302 struct namecache *ncp;
2305 * Automode from the vnlru proc - clean out 10% of the negative cache
2309 count = numneg / 10 + 1;
2312 * Attempt to clean out the specified number of negative cache
2316 spin_lock_wr(&ncspin);
2317 ncp = TAILQ_FIRST(&ncneglist);
2319 spin_unlock_wr(&ncspin);
2322 TAILQ_REMOVE(&ncneglist, ncp, nc_vnode);
2323 TAILQ_INSERT_TAIL(&ncneglist, ncp, nc_vnode);
2325 spin_unlock_wr(&ncspin);
2326 if (_cache_get_nonblock(ncp) == 0) {
2327 ncp = cache_zap(ncp);
2338 * Rehash a ncp. Rehashing is typically required if the name changes (should
2339 * not generally occur) or the parent link changes. This function will
2340 * unhash the ncp if the ncp is no longer hashable.
2343 _cache_rehash(struct namecache *ncp)
2345 struct nchash_head *nchpp;
2348 if ((nchpp = ncp->nc_head) != NULL) {
2349 spin_lock_wr(&nchpp->spin);
2350 LIST_REMOVE(ncp, nc_hash);
2351 ncp->nc_head = NULL;
2352 spin_unlock_wr(&nchpp->spin);
2354 if (ncp->nc_nlen && ncp->nc_parent) {
2355 hash = fnv_32_buf(ncp->nc_name, ncp->nc_nlen, FNV1_32_INIT);
2356 hash = fnv_32_buf(&ncp->nc_parent,
2357 sizeof(ncp->nc_parent), hash);
2358 nchpp = NCHHASH(hash);
2359 spin_lock_wr(&nchpp->spin);
2360 LIST_INSERT_HEAD(&nchpp->list, ncp, nc_hash);
2361 ncp->nc_head = nchpp;
2362 spin_unlock_wr(&nchpp->spin);
2367 * Name cache initialization, from vfsinit() when we are booting
2375 /* initialise per-cpu namecache effectiveness statistics. */
2376 for (i = 0; i < ncpus; ++i) {
2377 gd = globaldata_find(i);
2378 gd->gd_nchstats = &nchstats[i];
2380 TAILQ_INIT(&ncneglist);
2382 nchashtbl = hashinit_ext(desiredvnodes*2, sizeof(struct nchash_head),
2383 M_VFSCACHE, &nchash);
2384 for (i = 0; i <= (int)nchash; ++i) {
2385 LIST_INIT(&nchashtbl[i].list);
2386 spin_init(&nchashtbl[i].spin);
2388 nclockwarn = 5 * hz;
2392 * Called from start_init() to bootstrap the root filesystem. Returns
2393 * a referenced, unlocked namecache record.
2396 cache_allocroot(struct nchandle *nch, struct mount *mp, struct vnode *vp)
2398 nch->ncp = cache_alloc(0);
2400 atomic_add_int(&mp->mnt_refs, 1);
2402 _cache_setvp(nch->mount, nch->ncp, vp);
2406 * vfs_cache_setroot()
2408 * Create an association between the root of our namecache and
2409 * the root vnode. This routine may be called several times during
2412 * If the caller intends to save the returned namecache pointer somewhere
2413 * it must cache_hold() it.
2416 vfs_cache_setroot(struct vnode *nvp, struct nchandle *nch)
2419 struct nchandle onch;
2427 cache_zero(&rootnch);
2435 * XXX OLD API COMPAT FUNCTION. This really messes up the new namecache
2436 * topology and is being removed as quickly as possible. The new VOP_N*()
2437 * API calls are required to make specific adjustments using the supplied
2438 * ncp pointers rather then just bogusly purging random vnodes.
2440 * Invalidate all namecache entries to a particular vnode as well as
2441 * any direct children of that vnode in the namecache. This is a
2442 * 'catch all' purge used by filesystems that do not know any better.
2444 * Note that the linkage between the vnode and its namecache entries will
2445 * be removed, but the namecache entries themselves might stay put due to
2446 * active references from elsewhere in the system or due to the existance of
2447 * the children. The namecache topology is left intact even if we do not
2448 * know what the vnode association is. Such entries will be marked
2452 cache_purge(struct vnode *vp)
2454 cache_inval_vp(vp, CINV_DESTROY | CINV_CHILDREN);
2458 * Flush all entries referencing a particular filesystem.
2460 * Since we need to check it anyway, we will flush all the invalid
2461 * entries at the same time.
2466 cache_purgevfs(struct mount *mp)
2468 struct nchash_head *nchpp;
2469 struct namecache *ncp, *nnp;
2472 * Scan hash tables for applicable entries.
2474 for (nchpp = &nchashtbl[nchash]; nchpp >= nchashtbl; nchpp--) {
2475 spin_lock_wr(&nchpp->spin); XXX
2476 ncp = LIST_FIRST(&nchpp->list);
2480 nnp = LIST_NEXT(ncp, nc_hash);
2483 if (ncp->nc_mount == mp) {
2485 ncp = cache_zap(ncp);
2493 spin_unlock_wr(&nchpp->spin); XXX
2499 static int disablecwd;
2500 SYSCTL_INT(_debug, OID_AUTO, disablecwd, CTLFLAG_RW, &disablecwd, 0, "");
2502 static u_long numcwdcalls; STATNODE(CTLFLAG_RD, numcwdcalls, &numcwdcalls);
2503 static u_long numcwdfail1; STATNODE(CTLFLAG_RD, numcwdfail1, &numcwdfail1);
2504 static u_long numcwdfail2; STATNODE(CTLFLAG_RD, numcwdfail2, &numcwdfail2);
2505 static u_long numcwdfail3; STATNODE(CTLFLAG_RD, numcwdfail3, &numcwdfail3);
2506 static u_long numcwdfail4; STATNODE(CTLFLAG_RD, numcwdfail4, &numcwdfail4);
2507 static u_long numcwdfound; STATNODE(CTLFLAG_RD, numcwdfound, &numcwdfound);
2513 sys___getcwd(struct __getcwd_args *uap)
2523 buflen = uap->buflen;
2526 if (buflen > MAXPATHLEN)
2527 buflen = MAXPATHLEN;
2529 buf = kmalloc(buflen, M_TEMP, M_WAITOK);
2531 bp = kern_getcwd(buf, buflen, &error);
2534 error = copyout(bp, uap->buf, strlen(bp) + 1);
2540 kern_getcwd(char *buf, size_t buflen, int *error)
2542 struct proc *p = curproc;
2544 int i, slash_prefixed;
2545 struct filedesc *fdp;
2546 struct nchandle nch;
2555 nch = fdp->fd_ncdir;
2556 while (nch.ncp && (nch.ncp != fdp->fd_nrdir.ncp ||
2557 nch.mount != fdp->fd_nrdir.mount)
2560 * While traversing upwards if we encounter the root
2561 * of the current mount we have to skip to the mount point
2562 * in the underlying filesystem.
2564 if (nch.ncp == nch.mount->mnt_ncmountpt.ncp) {
2565 nch = nch.mount->mnt_ncmounton;
2570 * Prepend the path segment
2572 for (i = nch.ncp->nc_nlen - 1; i >= 0; i--) {
2578 *--bp = nch.ncp->nc_name[i];
2589 * Go up a directory. This isn't a mount point so we don't
2590 * have to check again.
2592 nch.ncp = nch.ncp->nc_parent;
2594 if (nch.ncp == NULL) {
2599 if (!slash_prefixed) {
2613 * Thus begins the fullpath magic.
2617 #define STATNODE(name) \
2618 static u_int name; \
2619 SYSCTL_UINT(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, 0, "")
2621 static int disablefullpath;
2622 SYSCTL_INT(_debug, OID_AUTO, disablefullpath, CTLFLAG_RW,
2623 &disablefullpath, 0, "");
2625 STATNODE(numfullpathcalls);
2626 STATNODE(numfullpathfail1);
2627 STATNODE(numfullpathfail2);
2628 STATNODE(numfullpathfail3);
2629 STATNODE(numfullpathfail4);
2630 STATNODE(numfullpathfound);
2633 cache_fullpath(struct proc *p, struct nchandle *nchp, char **retbuf, char **freebuf)
2635 struct nchandle fd_nrdir;
2636 struct nchandle nch;
2637 struct namecache *ncp;
2645 atomic_add_int(&numfullpathcalls, -1);
2646 lwkt_gettoken(&nlock, &vfs_token);
2651 buf = kmalloc(MAXPATHLEN, M_TEMP, M_WAITOK);
2652 bp = buf + MAXPATHLEN - 1;
2655 fd_nrdir = p->p_fd->fd_nrdir;
2659 cache_copy(nchp, &nch);
2663 while (ncp && (ncp != fd_nrdir.ncp || mp != fd_nrdir.mount)) {
2665 * While traversing upwards if we encounter the root
2666 * of the current mount we have to skip to the mount point.
2668 if (ncp == mp->mnt_ncmountpt.ncp) {
2670 cache_copy(&mp->mnt_ncmounton, &nch);
2677 * Prepend the path segment
2679 for (i = nch.ncp->nc_nlen - 1; i >= 0; i--) {
2686 *--bp = nch.ncp->nc_name[i];
2698 * Go up a directory. This isn't a mount point so we don't
2699 * have to check again.
2701 * We need the ncp's spinlock to safely access nc_parent.
2703 if ((nch.ncp = ncp->nc_parent) != NULL)
2704 _cache_hold(nch.ncp);
2708 if (nch.ncp == NULL) {
2715 if (!slash_prefixed) {
2730 lwkt_reltoken(&nlock);
2735 vn_fullpath(struct proc *p, struct vnode *vn, char **retbuf, char **freebuf)
2737 struct namecache *ncp;
2738 struct nchandle nch;
2741 atomic_add_int(&numfullpathcalls, 1);
2742 if (disablefullpath)
2748 /* vn is NULL, client wants us to use p->p_textvp */
2750 if ((vn = p->p_textvp) == NULL)
2753 spin_lock_wr(&vn->v_spinlock);
2754 TAILQ_FOREACH(ncp, &vn->v_namecache, nc_vnode) {
2759 spin_unlock_wr(&vn->v_spinlock);
2763 spin_unlock_wr(&vn->v_spinlock);
2765 atomic_add_int(&numfullpathcalls, -1);
2767 nch.mount = vn->v_mount;
2768 error = cache_fullpath(p, &nch, retbuf, freebuf);