2 * Copyright (c) 2003,2004,2009 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
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/kernel.h>
72 #include <sys/sysctl.h>
73 #include <sys/mount.h>
74 #include <sys/vnode.h>
75 #include <sys/malloc.h>
76 #include <sys/sysproto.h>
77 #include <sys/spinlock.h>
79 #include <sys/namei.h>
80 #include <sys/nlookup.h>
81 #include <sys/filedesc.h>
82 #include <sys/fnv_hash.h>
83 #include <sys/globaldata.h>
84 #include <sys/kern_syscall.h>
85 #include <sys/dirent.h>
88 #include <sys/sysref2.h>
89 #include <sys/spinlock2.h>
90 #include <sys/mplock2.h>
92 #define MAX_RECURSION_DEPTH 64
95 * Random lookups in the cache are accomplished with a hash table using
96 * a hash key of (nc_src_vp, name). Each hash chain has its own spin lock.
98 * Negative entries may exist and correspond to resolved namecache
99 * structures where nc_vp is NULL. In a negative entry, NCF_WHITEOUT
100 * will be set if the entry corresponds to a whited-out directory entry
101 * (verses simply not finding the entry at all). ncneglist is locked
102 * with a global spinlock (ncspin).
106 * (1) A ncp must be referenced before it can be locked.
108 * (2) A ncp must be locked in order to modify it.
110 * (3) ncp locks are always ordered child -> parent. That may seem
111 * backwards but forward scans use the hash table and thus can hold
112 * the parent unlocked when traversing downward.
114 * This allows insert/rename/delete/dot-dot and other operations
115 * to use ncp->nc_parent links.
117 * This also prevents a locked up e.g. NFS node from creating a
118 * chain reaction all the way back to the root vnode / namecache.
120 * (4) parent linkages require both the parent and child to be locked.
124 * Structures associated with name cacheing.
126 #define NCHHASH(hash) (&nchashtbl[(hash) & nchash])
129 #define NCMOUNT_NUMCACHE 1009 /* prime number */
131 MALLOC_DEFINE(M_VFSCACHE, "vfscache", "VFS name cache entries");
133 LIST_HEAD(nchash_list, namecache);
136 struct nchash_list list;
137 struct spinlock spin;
140 struct ncmount_cache {
141 struct spinlock spin;
142 struct namecache *ncp;
144 int isneg; /* if != 0 mp is originator and not target */
147 static struct nchash_head *nchashtbl;
148 static struct namecache_list ncneglist;
149 static struct spinlock ncspin;
150 static struct ncmount_cache ncmount_cache[NCMOUNT_NUMCACHE];
153 * ncvp_debug - debug cache_fromvp(). This is used by the NFS server
154 * to create the namecache infrastructure leading to a dangling vnode.
156 * 0 Only errors are reported
157 * 1 Successes are reported
158 * 2 Successes + the whole directory scan is reported
159 * 3 Force the directory scan code run as if the parent vnode did not
160 * have a namecache record, even if it does have one.
162 static int ncvp_debug;
163 SYSCTL_INT(_debug, OID_AUTO, ncvp_debug, CTLFLAG_RW, &ncvp_debug, 0,
164 "Namecache debug level (0-3)");
166 static u_long nchash; /* size of hash table */
167 SYSCTL_ULONG(_debug, OID_AUTO, nchash, CTLFLAG_RD, &nchash, 0,
168 "Size of namecache hash table");
170 static int ncnegfactor = 16; /* ratio of negative entries */
171 SYSCTL_INT(_debug, OID_AUTO, ncnegfactor, CTLFLAG_RW, &ncnegfactor, 0,
172 "Ratio of namecache negative entries");
174 static int nclockwarn; /* warn on locked entries in ticks */
175 SYSCTL_INT(_debug, OID_AUTO, nclockwarn, CTLFLAG_RW, &nclockwarn, 0,
176 "Warn on locked namecache entries in ticks");
178 static int numdefered; /* number of cache entries allocated */
179 SYSCTL_INT(_debug, OID_AUTO, numdefered, CTLFLAG_RD, &numdefered, 0,
180 "Number of cache entries allocated");
182 static int ncposlimit; /* number of cache entries allocated */
183 SYSCTL_INT(_debug, OID_AUTO, ncposlimit, CTLFLAG_RW, &ncposlimit, 0,
184 "Number of cache entries allocated");
186 static int ncp_shared_lock_disable = 1;
187 SYSCTL_INT(_debug, OID_AUTO, ncp_shared_lock_disable, CTLFLAG_RW,
188 &ncp_shared_lock_disable, 0, "Disable shared namecache locks");
190 SYSCTL_INT(_debug, OID_AUTO, vnsize, CTLFLAG_RD, 0, sizeof(struct vnode),
191 "sizeof(struct vnode)");
192 SYSCTL_INT(_debug, OID_AUTO, ncsize, CTLFLAG_RD, 0, sizeof(struct namecache),
193 "sizeof(struct namecache)");
195 static int ncmount_cache_enable = 1;
196 SYSCTL_INT(_debug, OID_AUTO, ncmount_cache_enable, CTLFLAG_RW,
197 &ncmount_cache_enable, 0, "mount point cache");
198 static long ncmount_cache_hit;
199 SYSCTL_LONG(_debug, OID_AUTO, ncmount_cache_hit, CTLFLAG_RW,
200 &ncmount_cache_hit, 0, "mpcache hits");
201 static long ncmount_cache_miss;
202 SYSCTL_LONG(_debug, OID_AUTO, ncmount_cache_miss, CTLFLAG_RW,
203 &ncmount_cache_miss, 0, "mpcache misses");
204 static long ncmount_cache_overwrite;
205 SYSCTL_LONG(_debug, OID_AUTO, ncmount_cache_overwrite, CTLFLAG_RW,
206 &ncmount_cache_overwrite, 0, "mpcache entry overwrites");
208 static int cache_resolve_mp(struct mount *mp);
209 static struct vnode *cache_dvpref(struct namecache *ncp);
210 static void _cache_lock(struct namecache *ncp);
211 static void _cache_setunresolved(struct namecache *ncp);
212 static void _cache_cleanneg(int count);
213 static void _cache_cleanpos(int count);
214 static void _cache_cleandefered(void);
215 static void _cache_unlink(struct namecache *ncp);
218 * The new name cache statistics
220 SYSCTL_NODE(_vfs, OID_AUTO, cache, CTLFLAG_RW, 0, "Name cache statistics");
222 SYSCTL_INT(_vfs_cache, OID_AUTO, numneg, CTLFLAG_RD, &numneg, 0,
223 "Number of negative namecache entries");
225 SYSCTL_INT(_vfs_cache, OID_AUTO, numcache, CTLFLAG_RD, &numcache, 0,
226 "Number of namecaches entries");
227 static u_long numcalls;
228 SYSCTL_ULONG(_vfs_cache, OID_AUTO, numcalls, CTLFLAG_RD, &numcalls, 0,
229 "Number of namecache lookups");
230 static u_long numchecks;
231 SYSCTL_ULONG(_vfs_cache, OID_AUTO, numchecks, CTLFLAG_RD, &numchecks, 0,
232 "Number of checked entries in namecache lookups");
234 struct nchstats nchstats[SMP_MAXCPU];
236 * Export VFS cache effectiveness statistics to user-land.
238 * The statistics are left for aggregation to user-land so
239 * neat things can be achieved, like observing per-CPU cache
243 sysctl_nchstats(SYSCTL_HANDLER_ARGS)
245 struct globaldata *gd;
249 for (i = 0; i < ncpus; ++i) {
250 gd = globaldata_find(i);
251 if ((error = SYSCTL_OUT(req, (void *)&(*gd->gd_nchstats),
252 sizeof(struct nchstats))))
258 SYSCTL_PROC(_vfs_cache, OID_AUTO, nchstats, CTLTYPE_OPAQUE|CTLFLAG_RD,
259 0, 0, sysctl_nchstats, "S,nchstats", "VFS cache effectiveness statistics");
261 static struct namecache *cache_zap(struct namecache *ncp, int nonblock);
264 * Namespace locking. The caller must already hold a reference to the
265 * namecache structure in order to lock/unlock it. This function prevents
266 * the namespace from being created or destroyed by accessors other then
269 * Note that holding a locked namecache structure prevents other threads
270 * from making namespace changes (e.g. deleting or creating), prevents
271 * vnode association state changes by other threads, and prevents the
272 * namecache entry from being resolved or unresolved by other threads.
274 * An exclusive lock owner has full authority to associate/disassociate
275 * vnodes and resolve/unresolve the locked ncp.
277 * A shared lock owner only has authority to acquire the underlying vnode,
280 * The primary lock field is nc_lockstatus. nc_locktd is set after the
281 * fact (when locking) or cleared prior to unlocking.
283 * WARNING! Holding a locked ncp will prevent a vnode from being destroyed
284 * or recycled, but it does NOT help you if the vnode had already
285 * initiated a recyclement. If this is important, use cache_get()
286 * rather then cache_lock() (and deal with the differences in the
287 * way the refs counter is handled). Or, alternatively, make an
288 * unconditional call to cache_validate() or cache_resolve()
289 * after cache_lock() returns.
293 _cache_lock(struct namecache *ncp)
300 KKASSERT(ncp->nc_refs != 0);
305 count = ncp->nc_lockstatus;
308 if ((count & ~(NC_EXLOCK_REQ|NC_SHLOCK_REQ)) == 0) {
309 if (atomic_cmpset_int(&ncp->nc_lockstatus,
312 * The vp associated with a locked ncp must
313 * be held to prevent it from being recycled.
315 * WARNING! If VRECLAIMED is set the vnode
316 * could already be in the middle of a recycle.
317 * Callers must use cache_vref() or
318 * cache_vget() on the locked ncp to
319 * validate the vp or set the cache entry
322 * NOTE! vhold() is allowed if we hold a
323 * lock on the ncp (which we do).
333 if (ncp->nc_locktd == td) {
334 KKASSERT((count & NC_SHLOCK_FLAG) == 0);
335 if (atomic_cmpset_int(&ncp->nc_lockstatus,
342 tsleep_interlock(&ncp->nc_locktd, 0);
343 if (atomic_cmpset_int(&ncp->nc_lockstatus, count,
344 count | NC_EXLOCK_REQ) == 0) {
348 error = tsleep(&ncp->nc_locktd, PINTERLOCKED,
349 "clock", nclockwarn);
350 if (error == EWOULDBLOCK) {
353 kprintf("[diagnostic] cache_lock: "
354 "blocked on %p %08x",
356 kprintf(" \"%*.*s\"\n",
357 ncp->nc_nlen, ncp->nc_nlen,
364 kprintf("[diagnostic] cache_lock: unblocked %*.*s after "
366 ncp->nc_nlen, ncp->nc_nlen, ncp->nc_name,
367 (int)(ticks - didwarn) / hz);
372 * The shared lock works similarly to the exclusive lock except
373 * nc_locktd is left NULL and we need an interlock (VHOLD) to
374 * prevent vhold() races, since the moment our cmpset_int succeeds
375 * another cpu can come in and get its own shared lock.
377 * A critical section is needed to prevent interruption during the
382 _cache_lock_shared(struct namecache *ncp)
388 KKASSERT(ncp->nc_refs != 0);
392 count = ncp->nc_lockstatus;
395 if ((count & ~NC_SHLOCK_REQ) == 0) {
397 if (atomic_cmpset_int(&ncp->nc_lockstatus,
399 (count + 1) | NC_SHLOCK_FLAG |
402 * The vp associated with a locked ncp must
403 * be held to prevent it from being recycled.
405 * WARNING! If VRECLAIMED is set the vnode
406 * could already be in the middle of a recycle.
407 * Callers must use cache_vref() or
408 * cache_vget() on the locked ncp to
409 * validate the vp or set the cache entry
412 * NOTE! vhold() is allowed if we hold a
413 * lock on the ncp (which we do).
417 atomic_clear_int(&ncp->nc_lockstatus,
428 * If already held shared we can just bump the count, but
429 * only allow this if nobody is trying to get the lock
432 * VHOLD is a bit of a hack. Even though we successfully
433 * added another shared ref, the cpu that got the first
434 * shared ref might not yet have held the vnode.
436 if ((count & (NC_EXLOCK_REQ|NC_SHLOCK_FLAG)) ==
438 KKASSERT((count & ~(NC_EXLOCK_REQ |
440 NC_SHLOCK_FLAG)) > 0);
441 if (atomic_cmpset_int(&ncp->nc_lockstatus,
443 while (ncp->nc_lockstatus & NC_SHLOCK_VHOLD)
449 tsleep_interlock(ncp, 0);
450 if (atomic_cmpset_int(&ncp->nc_lockstatus, count,
451 count | NC_SHLOCK_REQ) == 0) {
455 error = tsleep(ncp, PINTERLOCKED, "clocksh", nclockwarn);
456 if (error == EWOULDBLOCK) {
459 kprintf("[diagnostic] cache_lock_shared: "
460 "blocked on %p %08x",
462 kprintf(" \"%*.*s\"\n",
463 ncp->nc_nlen, ncp->nc_nlen,
470 kprintf("[diagnostic] cache_lock_shared: "
471 "unblocked %*.*s after %d secs\n",
472 ncp->nc_nlen, ncp->nc_nlen, ncp->nc_name,
473 (int)(ticks - didwarn) / hz);
478 * NOTE: nc_refs may be zero if the ncp is interlocked by circumstance,
479 * such as the case where one of its children is locked.
483 _cache_lock_nonblock(struct namecache *ncp)
491 count = ncp->nc_lockstatus;
493 if ((count & ~(NC_EXLOCK_REQ|NC_SHLOCK_REQ)) == 0) {
494 if (atomic_cmpset_int(&ncp->nc_lockstatus,
497 * The vp associated with a locked ncp must
498 * be held to prevent it from being recycled.
500 * WARNING! If VRECLAIMED is set the vnode
501 * could already be in the middle of a recycle.
502 * Callers must use cache_vref() or
503 * cache_vget() on the locked ncp to
504 * validate the vp or set the cache entry
507 * NOTE! vhold() is allowed if we hold a
508 * lock on the ncp (which we do).
518 if (ncp->nc_locktd == td) {
519 if (atomic_cmpset_int(&ncp->nc_lockstatus,
532 * The shared lock works similarly to the exclusive lock except
533 * nc_locktd is left NULL and we need an interlock (VHOLD) to
534 * prevent vhold() races, since the moment our cmpset_int succeeds
535 * another cpu can come in and get its own shared lock.
537 * A critical section is needed to prevent interruption during the
542 _cache_lock_shared_nonblock(struct namecache *ncp)
547 count = ncp->nc_lockstatus;
549 if ((count & ~NC_SHLOCK_REQ) == 0) {
551 if (atomic_cmpset_int(&ncp->nc_lockstatus,
553 (count + 1) | NC_SHLOCK_FLAG |
556 * The vp associated with a locked ncp must
557 * be held to prevent it from being recycled.
559 * WARNING! If VRECLAIMED is set the vnode
560 * could already be in the middle of a recycle.
561 * Callers must use cache_vref() or
562 * cache_vget() on the locked ncp to
563 * validate the vp or set the cache entry
566 * NOTE! vhold() is allowed if we hold a
567 * lock on the ncp (which we do).
571 atomic_clear_int(&ncp->nc_lockstatus,
582 * If already held shared we can just bump the count, but
583 * only allow this if nobody is trying to get the lock
586 * VHOLD is a bit of a hack. Even though we successfully
587 * added another shared ref, the cpu that got the first
588 * shared ref might not yet have held the vnode.
590 if ((count & (NC_EXLOCK_REQ|NC_SHLOCK_FLAG)) ==
592 KKASSERT((count & ~(NC_EXLOCK_REQ |
594 NC_SHLOCK_FLAG)) > 0);
595 if (atomic_cmpset_int(&ncp->nc_lockstatus,
597 while (ncp->nc_lockstatus & NC_SHLOCK_VHOLD)
611 * NOTE: nc_refs can be 0 (degenerate case during _cache_drop).
613 * nc_locktd must be NULLed out prior to nc_lockstatus getting cleared.
617 _cache_unlock(struct namecache *ncp)
619 thread_t td __debugvar = curthread;
622 struct vnode *dropvp;
624 KKASSERT(ncp->nc_refs >= 0);
625 KKASSERT((ncp->nc_lockstatus & ~(NC_EXLOCK_REQ|NC_SHLOCK_REQ)) > 0);
626 KKASSERT((ncp->nc_lockstatus & NC_SHLOCK_FLAG) || ncp->nc_locktd == td);
628 count = ncp->nc_lockstatus;
632 * Clear nc_locktd prior to the atomic op (excl lock only)
634 if ((count & ~(NC_EXLOCK_REQ|NC_SHLOCK_REQ)) == 1)
635 ncp->nc_locktd = NULL;
640 ~(NC_EXLOCK_REQ|NC_SHLOCK_REQ|NC_SHLOCK_FLAG)) == 1) {
642 if (count & NC_EXLOCK_REQ)
643 ncount = count & NC_SHLOCK_REQ; /* cnt->0 */
647 if (atomic_cmpset_int(&ncp->nc_lockstatus,
649 if (count & NC_EXLOCK_REQ)
650 wakeup(&ncp->nc_locktd);
651 else if (count & NC_SHLOCK_REQ)
657 KKASSERT((count & NC_SHLOCK_VHOLD) == 0);
658 KKASSERT((count & ~(NC_EXLOCK_REQ |
660 NC_SHLOCK_FLAG)) > 1);
661 if (atomic_cmpset_int(&ncp->nc_lockstatus,
666 count = ncp->nc_lockstatus;
671 * Don't actually drop the vp until we successfully clean out
672 * the lock, otherwise we may race another shared lock.
680 _cache_lockstatus(struct namecache *ncp)
682 if (ncp->nc_locktd == curthread)
683 return(LK_EXCLUSIVE);
684 if (ncp->nc_lockstatus & NC_SHLOCK_FLAG)
690 * cache_hold() and cache_drop() prevent the premature deletion of a
691 * namecache entry but do not prevent operations (such as zapping) on
692 * that namecache entry.
694 * This routine may only be called from outside this source module if
695 * nc_refs is already at least 1.
697 * This is a rare case where callers are allowed to hold a spinlock,
698 * so we can't ourselves.
702 _cache_hold(struct namecache *ncp)
704 atomic_add_int(&ncp->nc_refs, 1);
709 * Drop a cache entry, taking care to deal with races.
711 * For potential 1->0 transitions we must hold the ncp lock to safely
712 * test its flags. An unresolved entry with no children must be zapped
715 * The call to cache_zap() itself will handle all remaining races and
716 * will decrement the ncp's refs regardless. If we are resolved or
717 * have children nc_refs can safely be dropped to 0 without having to
720 * NOTE: cache_zap() will re-check nc_refs and nc_list in a MPSAFE fashion.
722 * NOTE: cache_zap() may return a non-NULL referenced parent which must
723 * be dropped in a loop.
727 _cache_drop(struct namecache *ncp)
732 KKASSERT(ncp->nc_refs > 0);
736 if (_cache_lock_nonblock(ncp) == 0) {
737 ncp->nc_flag &= ~NCF_DEFEREDZAP;
738 if ((ncp->nc_flag & NCF_UNRESOLVED) &&
739 TAILQ_EMPTY(&ncp->nc_list)) {
740 ncp = cache_zap(ncp, 1);
743 if (atomic_cmpset_int(&ncp->nc_refs, 1, 0)) {
750 if (atomic_cmpset_int(&ncp->nc_refs, refs, refs - 1))
758 * Link a new namecache entry to its parent and to the hash table. Be
759 * careful to avoid races if vhold() blocks in the future.
761 * Both ncp and par must be referenced and locked.
763 * NOTE: The hash table spinlock is likely held during this call, we
764 * can't do anything fancy.
767 _cache_link_parent(struct namecache *ncp, struct namecache *par,
768 struct nchash_head *nchpp)
770 KKASSERT(ncp->nc_parent == NULL);
771 ncp->nc_parent = par;
772 ncp->nc_head = nchpp;
775 * Set inheritance flags. Note that the parent flags may be
776 * stale due to getattr potentially not having been run yet
777 * (it gets run during nlookup()'s).
779 ncp->nc_flag &= ~(NCF_SF_PNOCACHE | NCF_UF_PCACHE);
780 if (par->nc_flag & (NCF_SF_NOCACHE | NCF_SF_PNOCACHE))
781 ncp->nc_flag |= NCF_SF_PNOCACHE;
782 if (par->nc_flag & (NCF_UF_CACHE | NCF_UF_PCACHE))
783 ncp->nc_flag |= NCF_UF_PCACHE;
785 LIST_INSERT_HEAD(&nchpp->list, ncp, nc_hash);
787 if (TAILQ_EMPTY(&par->nc_list)) {
788 TAILQ_INSERT_HEAD(&par->nc_list, ncp, nc_entry);
790 * Any vp associated with an ncp which has children must
791 * be held to prevent it from being recycled.
796 TAILQ_INSERT_HEAD(&par->nc_list, ncp, nc_entry);
801 * Remove the parent and hash associations from a namecache structure.
802 * If this is the last child of the parent the cache_drop(par) will
803 * attempt to recursively zap the parent.
805 * ncp must be locked. This routine will acquire a temporary lock on
806 * the parent as wlel as the appropriate hash chain.
809 _cache_unlink_parent(struct namecache *ncp)
811 struct namecache *par;
812 struct vnode *dropvp;
814 if ((par = ncp->nc_parent) != NULL) {
815 KKASSERT(ncp->nc_parent == par);
818 spin_lock(&ncp->nc_head->spin);
819 LIST_REMOVE(ncp, nc_hash);
820 TAILQ_REMOVE(&par->nc_list, ncp, nc_entry);
822 if (par->nc_vp && TAILQ_EMPTY(&par->nc_list))
824 spin_unlock(&ncp->nc_head->spin);
825 ncp->nc_parent = NULL;
831 * We can only safely vdrop with no spinlocks held.
839 * Allocate a new namecache structure. Most of the code does not require
840 * zero-termination of the string but it makes vop_compat_ncreate() easier.
842 static struct namecache *
843 cache_alloc(int nlen)
845 struct namecache *ncp;
847 ncp = kmalloc(sizeof(*ncp), M_VFSCACHE, M_WAITOK|M_ZERO);
849 ncp->nc_name = kmalloc(nlen + 1, M_VFSCACHE, M_WAITOK);
851 ncp->nc_flag = NCF_UNRESOLVED;
852 ncp->nc_error = ENOTCONN; /* needs to be resolved */
855 TAILQ_INIT(&ncp->nc_list);
861 * Can only be called for the case where the ncp has never been
862 * associated with anything (so no spinlocks are needed).
865 _cache_free(struct namecache *ncp)
867 KKASSERT(ncp->nc_refs == 1 && ncp->nc_lockstatus == 1);
869 kfree(ncp->nc_name, M_VFSCACHE);
870 kfree(ncp, M_VFSCACHE);
874 * [re]initialize a nchandle.
877 cache_zero(struct nchandle *nch)
884 * Ref and deref a namecache structure.
886 * The caller must specify a stable ncp pointer, typically meaning the
887 * ncp is already referenced but this can also occur indirectly through
888 * e.g. holding a lock on a direct child.
890 * WARNING: Caller may hold an unrelated read spinlock, which means we can't
891 * use read spinlocks here.
896 cache_hold(struct nchandle *nch)
898 _cache_hold(nch->ncp);
899 atomic_add_int(&nch->mount->mnt_refs, 1);
904 * Create a copy of a namecache handle for an already-referenced
910 cache_copy(struct nchandle *nch, struct nchandle *target)
914 _cache_hold(target->ncp);
915 atomic_add_int(&nch->mount->mnt_refs, 1);
922 cache_changemount(struct nchandle *nch, struct mount *mp)
924 atomic_add_int(&nch->mount->mnt_refs, -1);
926 atomic_add_int(&nch->mount->mnt_refs, 1);
930 cache_drop(struct nchandle *nch)
932 atomic_add_int(&nch->mount->mnt_refs, -1);
933 _cache_drop(nch->ncp);
939 cache_lockstatus(struct nchandle *nch)
941 return(_cache_lockstatus(nch->ncp));
945 cache_lock(struct nchandle *nch)
947 _cache_lock(nch->ncp);
951 cache_lock_maybe_shared(struct nchandle *nch, int excl)
953 struct namecache *ncp = nch->ncp;
955 if (ncp_shared_lock_disable || excl ||
956 (ncp->nc_flag & NCF_UNRESOLVED)) {
959 _cache_lock_shared(ncp);
960 if ((ncp->nc_flag & NCF_UNRESOLVED) == 0) {
961 if (ncp->nc_vp && (ncp->nc_vp->v_flag & VRECLAIMED)) {
973 * Relock nch1 given an unlocked nch1 and a locked nch2. The caller
974 * is responsible for checking both for validity on return as they
975 * may have become invalid.
977 * We have to deal with potential deadlocks here, just ping pong
978 * the lock until we get it (we will always block somewhere when
979 * looping so this is not cpu-intensive).
981 * which = 0 nch1 not locked, nch2 is locked
982 * which = 1 nch1 is locked, nch2 is not locked
985 cache_relock(struct nchandle *nch1, struct ucred *cred1,
986 struct nchandle *nch2, struct ucred *cred2)
994 if (cache_lock_nonblock(nch1) == 0) {
995 cache_resolve(nch1, cred1);
1000 cache_resolve(nch1, cred1);
1003 if (cache_lock_nonblock(nch2) == 0) {
1004 cache_resolve(nch2, cred2);
1009 cache_resolve(nch2, cred2);
1016 cache_lock_nonblock(struct nchandle *nch)
1018 return(_cache_lock_nonblock(nch->ncp));
1022 cache_unlock(struct nchandle *nch)
1024 _cache_unlock(nch->ncp);
1028 * ref-and-lock, unlock-and-deref functions.
1030 * This function is primarily used by nlookup. Even though cache_lock
1031 * holds the vnode, it is possible that the vnode may have already
1032 * initiated a recyclement.
1034 * We want cache_get() to return a definitively usable vnode or a
1035 * definitively unresolved ncp.
1039 _cache_get(struct namecache *ncp)
1043 if (ncp->nc_vp && (ncp->nc_vp->v_flag & VRECLAIMED))
1044 _cache_setunresolved(ncp);
1049 * Attempt to obtain a shared lock on the ncp. A shared lock will only
1050 * be obtained if the ncp is resolved and the vnode (if not ENOENT) is
1051 * valid. Otherwise an exclusive lock will be acquired instead.
1055 _cache_get_maybe_shared(struct namecache *ncp, int excl)
1057 if (ncp_shared_lock_disable || excl ||
1058 (ncp->nc_flag & NCF_UNRESOLVED)) {
1059 return(_cache_get(ncp));
1062 _cache_lock_shared(ncp);
1063 if ((ncp->nc_flag & NCF_UNRESOLVED) == 0) {
1064 if (ncp->nc_vp && (ncp->nc_vp->v_flag & VRECLAIMED)) {
1066 ncp = _cache_get(ncp);
1071 ncp = _cache_get(ncp);
1078 * This is a special form of _cache_lock() which only succeeds if
1079 * it can get a pristine, non-recursive lock. The caller must have
1080 * already ref'd the ncp.
1082 * On success the ncp will be locked, on failure it will not. The
1083 * ref count does not change either way.
1085 * We want _cache_lock_special() (on success) to return a definitively
1086 * usable vnode or a definitively unresolved ncp.
1089 _cache_lock_special(struct namecache *ncp)
1091 if (_cache_lock_nonblock(ncp) == 0) {
1092 if ((ncp->nc_lockstatus &
1093 ~(NC_EXLOCK_REQ|NC_SHLOCK_REQ)) == 1) {
1094 if (ncp->nc_vp && (ncp->nc_vp->v_flag & VRECLAIMED))
1095 _cache_setunresolved(ncp);
1100 return(EWOULDBLOCK);
1104 _cache_lock_shared_special(struct namecache *ncp)
1106 if (_cache_lock_shared_nonblock(ncp) == 0) {
1107 if ((ncp->nc_lockstatus &
1108 ~(NC_EXLOCK_REQ|NC_SHLOCK_REQ)) == (NC_SHLOCK_FLAG | 1)) {
1109 if (ncp->nc_vp == NULL ||
1110 (ncp->nc_vp->v_flag & VRECLAIMED) == 0) {
1116 return(EWOULDBLOCK);
1121 * NOTE: The same nchandle can be passed for both arguments.
1124 cache_get(struct nchandle *nch, struct nchandle *target)
1126 KKASSERT(nch->ncp->nc_refs > 0);
1127 target->mount = nch->mount;
1128 target->ncp = _cache_get(nch->ncp);
1129 atomic_add_int(&target->mount->mnt_refs, 1);
1133 cache_get_maybe_shared(struct nchandle *nch, struct nchandle *target, int excl)
1135 KKASSERT(nch->ncp->nc_refs > 0);
1136 target->mount = nch->mount;
1137 target->ncp = _cache_get_maybe_shared(nch->ncp, excl);
1138 atomic_add_int(&target->mount->mnt_refs, 1);
1146 _cache_put(struct namecache *ncp)
1156 cache_put(struct nchandle *nch)
1158 atomic_add_int(&nch->mount->mnt_refs, -1);
1159 _cache_put(nch->ncp);
1165 * Resolve an unresolved ncp by associating a vnode with it. If the
1166 * vnode is NULL, a negative cache entry is created.
1168 * The ncp should be locked on entry and will remain locked on return.
1172 _cache_setvp(struct mount *mp, struct namecache *ncp, struct vnode *vp)
1174 KKASSERT(ncp->nc_flag & NCF_UNRESOLVED);
1175 KKASSERT(_cache_lockstatus(ncp) == LK_EXCLUSIVE);
1179 * Any vp associated with an ncp which has children must
1180 * be held. Any vp associated with a locked ncp must be held.
1182 if (!TAILQ_EMPTY(&ncp->nc_list))
1184 spin_lock(&vp->v_spin);
1186 TAILQ_INSERT_HEAD(&vp->v_namecache, ncp, nc_vnode);
1187 spin_unlock(&vp->v_spin);
1188 if (ncp->nc_lockstatus & ~(NC_EXLOCK_REQ|NC_SHLOCK_REQ))
1192 * Set auxiliary flags
1194 switch(vp->v_type) {
1196 ncp->nc_flag |= NCF_ISDIR;
1199 ncp->nc_flag |= NCF_ISSYMLINK;
1200 /* XXX cache the contents of the symlink */
1205 atomic_add_int(&numcache, 1);
1207 /* XXX: this is a hack to work-around the lack of a real pfs vfs
1210 if (strncmp(mp->mnt_stat.f_fstypename, "null", 5) == 0)
1214 * When creating a negative cache hit we set the
1215 * namecache_gen. A later resolve will clean out the
1216 * negative cache hit if the mount point's namecache_gen
1217 * has changed. Used by devfs, could also be used by
1222 TAILQ_INSERT_TAIL(&ncneglist, ncp, nc_vnode);
1224 spin_unlock(&ncspin);
1225 ncp->nc_error = ENOENT;
1227 VFS_NCPGEN_SET(mp, ncp);
1229 ncp->nc_flag &= ~(NCF_UNRESOLVED | NCF_DEFEREDZAP);
1236 cache_setvp(struct nchandle *nch, struct vnode *vp)
1238 _cache_setvp(nch->mount, nch->ncp, vp);
1245 cache_settimeout(struct nchandle *nch, int nticks)
1247 struct namecache *ncp = nch->ncp;
1249 if ((ncp->nc_timeout = ticks + nticks) == 0)
1250 ncp->nc_timeout = 1;
1254 * Disassociate the vnode or negative-cache association and mark a
1255 * namecache entry as unresolved again. Note that the ncp is still
1256 * left in the hash table and still linked to its parent.
1258 * The ncp should be locked and refd on entry and will remain locked and refd
1261 * This routine is normally never called on a directory containing children.
1262 * However, NFS often does just that in its rename() code as a cop-out to
1263 * avoid complex namespace operations. This disconnects a directory vnode
1264 * from its namecache and can cause the OLDAPI and NEWAPI to get out of
1270 _cache_setunresolved(struct namecache *ncp)
1274 if ((ncp->nc_flag & NCF_UNRESOLVED) == 0) {
1275 ncp->nc_flag |= NCF_UNRESOLVED;
1276 ncp->nc_timeout = 0;
1277 ncp->nc_error = ENOTCONN;
1278 if ((vp = ncp->nc_vp) != NULL) {
1279 atomic_add_int(&numcache, -1);
1280 spin_lock(&vp->v_spin);
1282 TAILQ_REMOVE(&vp->v_namecache, ncp, nc_vnode);
1283 spin_unlock(&vp->v_spin);
1286 * Any vp associated with an ncp with children is
1287 * held by that ncp. Any vp associated with a locked
1288 * ncp is held by that ncp. These conditions must be
1289 * undone when the vp is cleared out from the ncp.
1291 if (!TAILQ_EMPTY(&ncp->nc_list))
1293 if (ncp->nc_lockstatus & ~(NC_EXLOCK_REQ|NC_SHLOCK_REQ))
1297 TAILQ_REMOVE(&ncneglist, ncp, nc_vnode);
1299 spin_unlock(&ncspin);
1301 ncp->nc_flag &= ~(NCF_WHITEOUT|NCF_ISDIR|NCF_ISSYMLINK);
1306 * The cache_nresolve() code calls this function to automatically
1307 * set a resolved cache element to unresolved if it has timed out
1308 * or if it is a negative cache hit and the mount point namecache_gen
1312 _cache_auto_unresolve_test(struct mount *mp, struct namecache *ncp)
1315 * Try to zap entries that have timed out. We have
1316 * to be careful here because locked leafs may depend
1317 * on the vnode remaining intact in a parent, so only
1318 * do this under very specific conditions.
1320 if (ncp->nc_timeout && (int)(ncp->nc_timeout - ticks) < 0 &&
1321 TAILQ_EMPTY(&ncp->nc_list)) {
1326 * If a resolved negative cache hit is invalid due to
1327 * the mount's namecache generation being bumped, zap it.
1329 if (ncp->nc_vp == NULL && VFS_NCPGEN_TEST(mp, ncp)) {
1334 * Otherwise we are good
1339 static __inline void
1340 _cache_auto_unresolve(struct mount *mp, struct namecache *ncp)
1343 * Already in an unresolved state, nothing to do.
1345 if ((ncp->nc_flag & NCF_UNRESOLVED) == 0) {
1346 if (_cache_auto_unresolve_test(mp, ncp))
1347 _cache_setunresolved(ncp);
1355 cache_setunresolved(struct nchandle *nch)
1357 _cache_setunresolved(nch->ncp);
1361 * Determine if we can clear NCF_ISMOUNTPT by scanning the mountlist
1362 * looking for matches. This flag tells the lookup code when it must
1363 * check for a mount linkage and also prevents the directories in question
1364 * from being deleted or renamed.
1368 cache_clrmountpt_callback(struct mount *mp, void *data)
1370 struct nchandle *nch = data;
1372 if (mp->mnt_ncmounton.ncp == nch->ncp)
1374 if (mp->mnt_ncmountpt.ncp == nch->ncp)
1383 cache_clrmountpt(struct nchandle *nch)
1387 count = mountlist_scan(cache_clrmountpt_callback, nch,
1388 MNTSCAN_FORWARD|MNTSCAN_NOBUSY);
1390 nch->ncp->nc_flag &= ~NCF_ISMOUNTPT;
1394 * Invalidate portions of the namecache topology given a starting entry.
1395 * The passed ncp is set to an unresolved state and:
1397 * The passed ncp must be referencxed and locked. The routine may unlock
1398 * and relock ncp several times, and will recheck the children and loop
1399 * to catch races. When done the passed ncp will be returned with the
1400 * reference and lock intact.
1402 * CINV_DESTROY - Set a flag in the passed ncp entry indicating
1403 * that the physical underlying nodes have been
1404 * destroyed... as in deleted. For example, when
1405 * a directory is removed. This will cause record
1406 * lookups on the name to no longer be able to find
1407 * the record and tells the resolver to return failure
1408 * rather then trying to resolve through the parent.
1410 * The topology itself, including ncp->nc_name,
1413 * This only applies to the passed ncp, if CINV_CHILDREN
1414 * is specified the children are not flagged.
1416 * CINV_CHILDREN - Set all children (recursively) to an unresolved
1419 * Note that this will also have the side effect of
1420 * cleaning out any unreferenced nodes in the topology
1421 * from the leaves up as the recursion backs out.
1423 * Note that the topology for any referenced nodes remains intact, but
1424 * the nodes will be marked as having been destroyed and will be set
1425 * to an unresolved state.
1427 * It is possible for cache_inval() to race a cache_resolve(), meaning that
1428 * the namecache entry may not actually be invalidated on return if it was
1429 * revalidated while recursing down into its children. This code guarentees
1430 * that the node(s) will go through an invalidation cycle, but does not
1431 * guarentee that they will remain in an invalidated state.
1433 * Returns non-zero if a revalidation was detected during the invalidation
1434 * recursion, zero otherwise. Note that since only the original ncp is
1435 * locked the revalidation ultimately can only indicate that the original ncp
1436 * *MIGHT* no have been reresolved.
1438 * DEEP RECURSION HANDLING - If a recursive invalidation recurses deeply we
1439 * have to avoid blowing out the kernel stack. We do this by saving the
1440 * deep namecache node and aborting the recursion, then re-recursing at that
1441 * node using a depth-first algorithm in order to allow multiple deep
1442 * recursions to chain through each other, then we restart the invalidation
1447 struct namecache *resume_ncp;
1451 static int _cache_inval_internal(struct namecache *, int, struct cinvtrack *);
1455 _cache_inval(struct namecache *ncp, int flags)
1457 struct cinvtrack track;
1458 struct namecache *ncp2;
1462 track.resume_ncp = NULL;
1465 r = _cache_inval_internal(ncp, flags, &track);
1466 if (track.resume_ncp == NULL)
1468 kprintf("Warning: deep namecache recursion at %s\n",
1471 while ((ncp2 = track.resume_ncp) != NULL) {
1472 track.resume_ncp = NULL;
1474 _cache_inval_internal(ncp2, flags & ~CINV_DESTROY,
1484 cache_inval(struct nchandle *nch, int flags)
1486 return(_cache_inval(nch->ncp, flags));
1490 * Helper for _cache_inval(). The passed ncp is refd and locked and
1491 * remains that way on return, but may be unlocked/relocked multiple
1492 * times by the routine.
1495 _cache_inval_internal(struct namecache *ncp, int flags, struct cinvtrack *track)
1497 struct namecache *kid;
1498 struct namecache *nextkid;
1501 KKASSERT(_cache_lockstatus(ncp) == LK_EXCLUSIVE);
1503 _cache_setunresolved(ncp);
1504 if (flags & CINV_DESTROY)
1505 ncp->nc_flag |= NCF_DESTROYED;
1506 if ((flags & CINV_CHILDREN) &&
1507 (kid = TAILQ_FIRST(&ncp->nc_list)) != NULL
1510 if (++track->depth > MAX_RECURSION_DEPTH) {
1511 track->resume_ncp = ncp;
1517 if (track->resume_ncp) {
1521 if ((nextkid = TAILQ_NEXT(kid, nc_entry)) != NULL)
1522 _cache_hold(nextkid);
1523 if ((kid->nc_flag & NCF_UNRESOLVED) == 0 ||
1524 TAILQ_FIRST(&kid->nc_list)
1527 rcnt += _cache_inval_internal(kid, flags & ~CINV_DESTROY, track);
1538 * Someone could have gotten in there while ncp was unlocked,
1541 if ((ncp->nc_flag & NCF_UNRESOLVED) == 0)
1547 * Invalidate a vnode's namecache associations. To avoid races against
1548 * the resolver we do not invalidate a node which we previously invalidated
1549 * but which was then re-resolved while we were in the invalidation loop.
1551 * Returns non-zero if any namecache entries remain after the invalidation
1554 * NOTE: Unlike the namecache topology which guarentees that ncp's will not
1555 * be ripped out of the topology while held, the vnode's v_namecache
1556 * list has no such restriction. NCP's can be ripped out of the list
1557 * at virtually any time if not locked, even if held.
1559 * In addition, the v_namecache list itself must be locked via
1560 * the vnode's spinlock.
1563 cache_inval_vp(struct vnode *vp, int flags)
1565 struct namecache *ncp;
1566 struct namecache *next;
1569 spin_lock(&vp->v_spin);
1570 ncp = TAILQ_FIRST(&vp->v_namecache);
1574 /* loop entered with ncp held and vp spin-locked */
1575 if ((next = TAILQ_NEXT(ncp, nc_vnode)) != NULL)
1577 spin_unlock(&vp->v_spin);
1579 if (ncp->nc_vp != vp) {
1580 kprintf("Warning: cache_inval_vp: race-A detected on "
1581 "%s\n", ncp->nc_name);
1587 _cache_inval(ncp, flags);
1588 _cache_put(ncp); /* also releases reference */
1590 spin_lock(&vp->v_spin);
1591 if (ncp && ncp->nc_vp != vp) {
1592 spin_unlock(&vp->v_spin);
1593 kprintf("Warning: cache_inval_vp: race-B detected on "
1594 "%s\n", ncp->nc_name);
1599 spin_unlock(&vp->v_spin);
1600 return(TAILQ_FIRST(&vp->v_namecache) != NULL);
1604 * This routine is used instead of the normal cache_inval_vp() when we
1605 * are trying to recycle otherwise good vnodes.
1607 * Return 0 on success, non-zero if not all namecache records could be
1608 * disassociated from the vnode (for various reasons).
1611 cache_inval_vp_nonblock(struct vnode *vp)
1613 struct namecache *ncp;
1614 struct namecache *next;
1616 spin_lock(&vp->v_spin);
1617 ncp = TAILQ_FIRST(&vp->v_namecache);
1621 /* loop entered with ncp held */
1622 if ((next = TAILQ_NEXT(ncp, nc_vnode)) != NULL)
1624 spin_unlock(&vp->v_spin);
1625 if (_cache_lock_nonblock(ncp)) {
1631 if (ncp->nc_vp != vp) {
1632 kprintf("Warning: cache_inval_vp: race-A detected on "
1633 "%s\n", ncp->nc_name);
1639 _cache_inval(ncp, 0);
1640 _cache_put(ncp); /* also releases reference */
1642 spin_lock(&vp->v_spin);
1643 if (ncp && ncp->nc_vp != vp) {
1644 spin_unlock(&vp->v_spin);
1645 kprintf("Warning: cache_inval_vp: race-B detected on "
1646 "%s\n", ncp->nc_name);
1651 spin_unlock(&vp->v_spin);
1653 return(TAILQ_FIRST(&vp->v_namecache) != NULL);
1657 * The source ncp has been renamed to the target ncp. Both fncp and tncp
1658 * must be locked. The target ncp is destroyed (as a normal rename-over
1659 * would destroy the target file or directory).
1661 * Because there may be references to the source ncp we cannot copy its
1662 * contents to the target. Instead the source ncp is relinked as the target
1663 * and the target ncp is removed from the namecache topology.
1666 cache_rename(struct nchandle *fnch, struct nchandle *tnch)
1668 struct namecache *fncp = fnch->ncp;
1669 struct namecache *tncp = tnch->ncp;
1670 struct namecache *tncp_par;
1671 struct nchash_head *nchpp;
1676 if (tncp->nc_nlen) {
1677 nname = kmalloc(tncp->nc_nlen + 1, M_VFSCACHE, M_WAITOK);
1678 bcopy(tncp->nc_name, nname, tncp->nc_nlen);
1679 nname[tncp->nc_nlen] = 0;
1685 * Rename fncp (unlink)
1687 _cache_unlink_parent(fncp);
1688 oname = fncp->nc_name;
1689 fncp->nc_name = nname;
1690 fncp->nc_nlen = tncp->nc_nlen;
1692 kfree(oname, M_VFSCACHE);
1694 tncp_par = tncp->nc_parent;
1695 _cache_hold(tncp_par);
1696 _cache_lock(tncp_par);
1699 * Rename fncp (relink)
1701 hash = fnv_32_buf(fncp->nc_name, fncp->nc_nlen, FNV1_32_INIT);
1702 hash = fnv_32_buf(&tncp_par, sizeof(tncp_par), hash);
1703 nchpp = NCHHASH(hash);
1705 spin_lock(&nchpp->spin);
1706 _cache_link_parent(fncp, tncp_par, nchpp);
1707 spin_unlock(&nchpp->spin);
1709 _cache_put(tncp_par);
1712 * Get rid of the overwritten tncp (unlink)
1714 _cache_unlink(tncp);
1718 * Perform actions consistent with unlinking a file. The passed-in ncp
1721 * The ncp is marked DESTROYED so it no longer shows up in searches,
1722 * and will be physically deleted when the vnode goes away.
1724 * If the related vnode has no refs then we cycle it through vget()/vput()
1725 * to (possibly if we don't have a ref race) trigger a deactivation,
1726 * allowing the VFS to trivially detect and recycle the deleted vnode
1727 * via VOP_INACTIVE().
1729 * NOTE: _cache_rename() will automatically call _cache_unlink() on the
1733 cache_unlink(struct nchandle *nch)
1735 _cache_unlink(nch->ncp);
1739 _cache_unlink(struct namecache *ncp)
1744 * Causes lookups to fail and allows another ncp with the same
1745 * name to be created under ncp->nc_parent.
1747 ncp->nc_flag |= NCF_DESTROYED;
1750 * Attempt to trigger a deactivation.
1752 if ((ncp->nc_flag & NCF_UNRESOLVED) == 0 &&
1753 (vp = ncp->nc_vp) != NULL &&
1754 !sysref_isactive(&vp->v_sysref)) {
1755 if (vget(vp, LK_SHARED) == 0)
1761 * vget the vnode associated with the namecache entry. Resolve the namecache
1762 * entry if necessary. The passed ncp must be referenced and locked.
1764 * lk_type may be LK_SHARED, LK_EXCLUSIVE. A ref'd, possibly locked
1765 * (depending on the passed lk_type) will be returned in *vpp with an error
1766 * of 0, or NULL will be returned in *vpp with a non-0 error code. The
1767 * most typical error is ENOENT, meaning that the ncp represents a negative
1768 * cache hit and there is no vnode to retrieve, but other errors can occur
1771 * The vget() can race a reclaim. If this occurs we re-resolve the
1774 * There are numerous places in the kernel where vget() is called on a
1775 * vnode while one or more of its namecache entries is locked. Releasing
1776 * a vnode never deadlocks against locked namecache entries (the vnode
1777 * will not get recycled while referenced ncp's exist). This means we
1778 * can safely acquire the vnode. In fact, we MUST NOT release the ncp
1779 * lock when acquiring the vp lock or we might cause a deadlock.
1781 * NOTE: The passed-in ncp must be locked exclusively if it is initially
1782 * unresolved. If a reclaim race occurs the passed-in ncp will be
1783 * relocked exclusively before being re-resolved.
1786 cache_vget(struct nchandle *nch, struct ucred *cred,
1787 int lk_type, struct vnode **vpp)
1789 struct namecache *ncp;
1796 if (ncp->nc_flag & NCF_UNRESOLVED)
1797 error = cache_resolve(nch, cred);
1801 if (error == 0 && (vp = ncp->nc_vp) != NULL) {
1802 error = vget(vp, lk_type);
1807 if (error == ENOENT) {
1808 kprintf("Warning: vnode reclaim race detected "
1809 "in cache_vget on %p (%s)\n",
1813 _cache_setunresolved(ncp);
1818 * Not a reclaim race, some other error.
1820 KKASSERT(ncp->nc_vp == vp);
1823 KKASSERT(ncp->nc_vp == vp);
1824 KKASSERT((vp->v_flag & VRECLAIMED) == 0);
1827 if (error == 0 && vp == NULL)
1834 * Similar to cache_vget() but only acquires a ref on the vnode.
1836 * NOTE: The passed-in ncp must be locked exclusively if it is initially
1837 * unresolved. If a reclaim race occurs the passed-in ncp will be
1838 * relocked exclusively before being re-resolved.
1841 cache_vref(struct nchandle *nch, struct ucred *cred, struct vnode **vpp)
1843 struct namecache *ncp;
1850 if (ncp->nc_flag & NCF_UNRESOLVED)
1851 error = cache_resolve(nch, cred);
1855 if (error == 0 && (vp = ncp->nc_vp) != NULL) {
1856 error = vget(vp, LK_SHARED);
1861 if (error == ENOENT) {
1862 kprintf("Warning: vnode reclaim race detected "
1863 "in cache_vget on %p (%s)\n",
1867 _cache_setunresolved(ncp);
1872 * Not a reclaim race, some other error.
1874 KKASSERT(ncp->nc_vp == vp);
1877 KKASSERT(ncp->nc_vp == vp);
1878 KKASSERT((vp->v_flag & VRECLAIMED) == 0);
1879 /* caller does not want a lock */
1883 if (error == 0 && vp == NULL)
1890 * Return a referenced vnode representing the parent directory of
1893 * Because the caller has locked the ncp it should not be possible for
1894 * the parent ncp to go away. However, the parent can unresolve its
1895 * dvp at any time so we must be able to acquire a lock on the parent
1896 * to safely access nc_vp.
1898 * We have to leave par unlocked when vget()ing dvp to avoid a deadlock,
1899 * so use vhold()/vdrop() while holding the lock to prevent dvp from
1900 * getting destroyed.
1902 * NOTE: vhold() is allowed when dvp has 0 refs if we hold a
1903 * lock on the ncp in question..
1905 static struct vnode *
1906 cache_dvpref(struct namecache *ncp)
1908 struct namecache *par;
1912 if ((par = ncp->nc_parent) != NULL) {
1915 if ((par->nc_flag & NCF_UNRESOLVED) == 0) {
1916 if ((dvp = par->nc_vp) != NULL)
1921 if (vget(dvp, LK_SHARED) == 0) {
1924 /* return refd, unlocked dvp */
1936 * Convert a directory vnode to a namecache record without any other
1937 * knowledge of the topology. This ONLY works with directory vnodes and
1938 * is ONLY used by the NFS server. dvp must be refd but unlocked, and the
1939 * returned ncp (if not NULL) will be held and unlocked.
1941 * If 'makeit' is 0 and dvp has no existing namecache record, NULL is returned.
1942 * If 'makeit' is 1 we attempt to track-down and create the namecache topology
1943 * for dvp. This will fail only if the directory has been deleted out from
1946 * Callers must always check for a NULL return no matter the value of 'makeit'.
1948 * To avoid underflowing the kernel stack each recursive call increments
1949 * the makeit variable.
1952 static int cache_inefficient_scan(struct nchandle *nch, struct ucred *cred,
1953 struct vnode *dvp, char *fakename);
1954 static int cache_fromdvp_try(struct vnode *dvp, struct ucred *cred,
1955 struct vnode **saved_dvp);
1958 cache_fromdvp(struct vnode *dvp, struct ucred *cred, int makeit,
1959 struct nchandle *nch)
1961 struct vnode *saved_dvp;
1967 nch->mount = dvp->v_mount;
1972 * Handle the makeit == 0 degenerate case
1975 spin_lock(&dvp->v_spin);
1976 nch->ncp = TAILQ_FIRST(&dvp->v_namecache);
1979 spin_unlock(&dvp->v_spin);
1983 * Loop until resolution, inside code will break out on error.
1987 * Break out if we successfully acquire a working ncp.
1989 spin_lock(&dvp->v_spin);
1990 nch->ncp = TAILQ_FIRST(&dvp->v_namecache);
1993 spin_unlock(&dvp->v_spin);
1996 spin_unlock(&dvp->v_spin);
1999 * If dvp is the root of its filesystem it should already
2000 * have a namecache pointer associated with it as a side
2001 * effect of the mount, but it may have been disassociated.
2003 if (dvp->v_flag & VROOT) {
2004 nch->ncp = _cache_get(nch->mount->mnt_ncmountpt.ncp);
2005 error = cache_resolve_mp(nch->mount);
2006 _cache_put(nch->ncp);
2008 kprintf("cache_fromdvp: resolve root of mount %p error %d",
2009 dvp->v_mount, error);
2013 kprintf(" failed\n");
2018 kprintf(" succeeded\n");
2023 * If we are recursed too deeply resort to an O(n^2)
2024 * algorithm to resolve the namecache topology. The
2025 * resolved pvp is left referenced in saved_dvp to
2026 * prevent the tree from being destroyed while we loop.
2029 error = cache_fromdvp_try(dvp, cred, &saved_dvp);
2031 kprintf("lookupdotdot(longpath) failed %d "
2032 "dvp %p\n", error, dvp);
2040 * Get the parent directory and resolve its ncp.
2043 kfree(fakename, M_TEMP);
2046 error = vop_nlookupdotdot(*dvp->v_ops, dvp, &pvp, cred,
2049 kprintf("lookupdotdot failed %d dvp %p\n", error, dvp);
2055 * Reuse makeit as a recursion depth counter. On success
2056 * nch will be fully referenced.
2058 cache_fromdvp(pvp, cred, makeit + 1, nch);
2060 if (nch->ncp == NULL)
2064 * Do an inefficient scan of pvp (embodied by ncp) to look
2065 * for dvp. This will create a namecache record for dvp on
2066 * success. We loop up to recheck on success.
2068 * ncp and dvp are both held but not locked.
2070 error = cache_inefficient_scan(nch, cred, dvp, fakename);
2072 kprintf("cache_fromdvp: scan %p (%s) failed on dvp=%p\n",
2073 pvp, nch->ncp->nc_name, dvp);
2075 /* nch was NULLed out, reload mount */
2076 nch->mount = dvp->v_mount;
2080 kprintf("cache_fromdvp: scan %p (%s) succeeded\n",
2081 pvp, nch->ncp->nc_name);
2084 /* nch was NULLed out, reload mount */
2085 nch->mount = dvp->v_mount;
2089 * If nch->ncp is non-NULL it will have been held already.
2092 kfree(fakename, M_TEMP);
2101 * Go up the chain of parent directories until we find something
2102 * we can resolve into the namecache. This is very inefficient.
2106 cache_fromdvp_try(struct vnode *dvp, struct ucred *cred,
2107 struct vnode **saved_dvp)
2109 struct nchandle nch;
2112 static time_t last_fromdvp_report;
2116 * Loop getting the parent directory vnode until we get something we
2117 * can resolve in the namecache.
2120 nch.mount = dvp->v_mount;
2126 kfree(fakename, M_TEMP);
2129 error = vop_nlookupdotdot(*dvp->v_ops, dvp, &pvp, cred,
2136 spin_lock(&pvp->v_spin);
2137 if ((nch.ncp = TAILQ_FIRST(&pvp->v_namecache)) != NULL) {
2138 _cache_hold(nch.ncp);
2139 spin_unlock(&pvp->v_spin);
2143 spin_unlock(&pvp->v_spin);
2144 if (pvp->v_flag & VROOT) {
2145 nch.ncp = _cache_get(pvp->v_mount->mnt_ncmountpt.ncp);
2146 error = cache_resolve_mp(nch.mount);
2147 _cache_unlock(nch.ncp);
2150 _cache_drop(nch.ncp);
2160 if (last_fromdvp_report != time_second) {
2161 last_fromdvp_report = time_second;
2162 kprintf("Warning: extremely inefficient path "
2163 "resolution on %s\n",
2166 error = cache_inefficient_scan(&nch, cred, dvp, fakename);
2169 * Hopefully dvp now has a namecache record associated with
2170 * it. Leave it referenced to prevent the kernel from
2171 * recycling the vnode. Otherwise extremely long directory
2172 * paths could result in endless recycling.
2177 _cache_drop(nch.ncp);
2180 kfree(fakename, M_TEMP);
2185 * Do an inefficient scan of the directory represented by ncp looking for
2186 * the directory vnode dvp. ncp must be held but not locked on entry and
2187 * will be held on return. dvp must be refd but not locked on entry and
2188 * will remain refd on return.
2190 * Why do this at all? Well, due to its stateless nature the NFS server
2191 * converts file handles directly to vnodes without necessarily going through
2192 * the namecache ops that would otherwise create the namecache topology
2193 * leading to the vnode. We could either (1) Change the namecache algorithms
2194 * to allow disconnect namecache records that are re-merged opportunistically,
2195 * or (2) Make the NFS server backtrack and scan to recover a connected
2196 * namecache topology in order to then be able to issue new API lookups.
2198 * It turns out that (1) is a huge mess. It takes a nice clean set of
2199 * namecache algorithms and introduces a lot of complication in every subsystem
2200 * that calls into the namecache to deal with the re-merge case, especially
2201 * since we are using the namecache to placehold negative lookups and the
2202 * vnode might not be immediately assigned. (2) is certainly far less
2203 * efficient then (1), but since we are only talking about directories here
2204 * (which are likely to remain cached), the case does not actually run all
2205 * that often and has the supreme advantage of not polluting the namecache
2208 * If a fakename is supplied just construct a namecache entry using the
2212 cache_inefficient_scan(struct nchandle *nch, struct ucred *cred,
2213 struct vnode *dvp, char *fakename)
2215 struct nlcomponent nlc;
2216 struct nchandle rncp;
2228 vat.va_blocksize = 0;
2229 if ((error = VOP_GETATTR(dvp, &vat)) != 0)
2232 error = cache_vref(nch, cred, &pvp);
2237 kprintf("inefficient_scan: directory iosize %ld "
2238 "vattr fileid = %lld\n",
2240 (long long)vat.va_fileid);
2244 * Use the supplied fakename if not NULL. Fake names are typically
2245 * not in the actual filesystem hierarchy. This is used by HAMMER
2246 * to glue @@timestamp recursions together.
2249 nlc.nlc_nameptr = fakename;
2250 nlc.nlc_namelen = strlen(fakename);
2251 rncp = cache_nlookup(nch, &nlc);
2255 if ((blksize = vat.va_blocksize) == 0)
2256 blksize = DEV_BSIZE;
2257 rbuf = kmalloc(blksize, M_TEMP, M_WAITOK);
2263 iov.iov_base = rbuf;
2264 iov.iov_len = blksize;
2267 uio.uio_resid = blksize;
2268 uio.uio_segflg = UIO_SYSSPACE;
2269 uio.uio_rw = UIO_READ;
2270 uio.uio_td = curthread;
2272 if (ncvp_debug >= 2)
2273 kprintf("cache_inefficient_scan: readdir @ %08x\n", (int)uio.uio_offset);
2274 error = VOP_READDIR(pvp, &uio, cred, &eofflag, NULL, NULL);
2276 den = (struct dirent *)rbuf;
2277 bytes = blksize - uio.uio_resid;
2280 if (ncvp_debug >= 2) {
2281 kprintf("cache_inefficient_scan: %*.*s\n",
2282 den->d_namlen, den->d_namlen,
2285 if (den->d_type != DT_WHT &&
2286 den->d_ino == vat.va_fileid) {
2288 kprintf("cache_inefficient_scan: "
2289 "MATCHED inode %lld path %s/%*.*s\n",
2290 (long long)vat.va_fileid,
2292 den->d_namlen, den->d_namlen,
2295 nlc.nlc_nameptr = den->d_name;
2296 nlc.nlc_namelen = den->d_namlen;
2297 rncp = cache_nlookup(nch, &nlc);
2298 KKASSERT(rncp.ncp != NULL);
2301 bytes -= _DIRENT_DIRSIZ(den);
2302 den = _DIRENT_NEXT(den);
2304 if (rncp.ncp == NULL && eofflag == 0 && uio.uio_resid != blksize)
2307 kfree(rbuf, M_TEMP);
2311 if (rncp.ncp->nc_flag & NCF_UNRESOLVED) {
2312 _cache_setvp(rncp.mount, rncp.ncp, dvp);
2313 if (ncvp_debug >= 2) {
2314 kprintf("cache_inefficient_scan: setvp %s/%s = %p\n",
2315 nch->ncp->nc_name, rncp.ncp->nc_name, dvp);
2318 if (ncvp_debug >= 2) {
2319 kprintf("cache_inefficient_scan: setvp %s/%s already set %p/%p\n",
2320 nch->ncp->nc_name, rncp.ncp->nc_name, dvp,
2324 if (rncp.ncp->nc_vp == NULL)
2325 error = rncp.ncp->nc_error;
2327 * Release rncp after a successful nlookup. rncp was fully
2332 kprintf("cache_inefficient_scan: dvp %p NOT FOUND in %s\n",
2333 dvp, nch->ncp->nc_name);
2340 * Zap a namecache entry. The ncp is unconditionally set to an unresolved
2341 * state, which disassociates it from its vnode or ncneglist.
2343 * Then, if there are no additional references to the ncp and no children,
2344 * the ncp is removed from the topology and destroyed.
2346 * References and/or children may exist if the ncp is in the middle of the
2347 * topology, preventing the ncp from being destroyed.
2349 * This function must be called with the ncp held and locked and will unlock
2350 * and drop it during zapping.
2352 * If nonblock is non-zero and the parent ncp cannot be locked we give up.
2353 * This case can occur in the cache_drop() path.
2355 * This function may returned a held (but NOT locked) parent node which the
2356 * caller must drop. We do this so _cache_drop() can loop, to avoid
2357 * blowing out the kernel stack.
2359 * WARNING! For MPSAFE operation this routine must acquire up to three
2360 * spin locks to be able to safely test nc_refs. Lock order is
2363 * hash spinlock if on hash list
2364 * parent spinlock if child of parent
2365 * (the ncp is unresolved so there is no vnode association)
2367 static struct namecache *
2368 cache_zap(struct namecache *ncp, int nonblock)
2370 struct namecache *par;
2371 struct vnode *dropvp;
2375 * Disassociate the vnode or negative cache ref and set NCF_UNRESOLVED.
2377 _cache_setunresolved(ncp);
2380 * Try to scrap the entry and possibly tail-recurse on its parent.
2381 * We only scrap unref'd (other then our ref) unresolved entries,
2382 * we do not scrap 'live' entries.
2384 * Note that once the spinlocks are acquired if nc_refs == 1 no
2385 * other references are possible. If it isn't, however, we have
2386 * to decrement but also be sure to avoid a 1->0 transition.
2388 KKASSERT(ncp->nc_flag & NCF_UNRESOLVED);
2389 KKASSERT(ncp->nc_refs > 0);
2392 * Acquire locks. Note that the parent can't go away while we hold
2395 if ((par = ncp->nc_parent) != NULL) {
2398 if (_cache_lock_nonblock(par) == 0)
2400 refs = ncp->nc_refs;
2401 ncp->nc_flag |= NCF_DEFEREDZAP;
2402 ++numdefered; /* MP race ok */
2403 if (atomic_cmpset_int(&ncp->nc_refs,
2415 spin_lock(&ncp->nc_head->spin);
2419 * If someone other then us has a ref or we have children
2420 * we cannot zap the entry. The 1->0 transition and any
2421 * further list operation is protected by the spinlocks
2422 * we have acquired but other transitions are not.
2425 refs = ncp->nc_refs;
2426 if (refs == 1 && TAILQ_EMPTY(&ncp->nc_list))
2428 if (atomic_cmpset_int(&ncp->nc_refs, refs, refs - 1)) {
2430 spin_unlock(&ncp->nc_head->spin);
2440 * We are the only ref and with the spinlocks held no further
2441 * refs can be acquired by others.
2443 * Remove us from the hash list and parent list. We have to
2444 * drop a ref on the parent's vp if the parent's list becomes
2449 struct nchash_head *nchpp = ncp->nc_head;
2451 KKASSERT(nchpp != NULL);
2452 LIST_REMOVE(ncp, nc_hash);
2453 TAILQ_REMOVE(&par->nc_list, ncp, nc_entry);
2454 if (par->nc_vp && TAILQ_EMPTY(&par->nc_list))
2455 dropvp = par->nc_vp;
2456 ncp->nc_head = NULL;
2457 ncp->nc_parent = NULL;
2458 spin_unlock(&nchpp->spin);
2461 KKASSERT(ncp->nc_head == NULL);
2465 * ncp should not have picked up any refs. Physically
2468 KKASSERT(ncp->nc_refs == 1);
2469 /* _cache_unlock(ncp) not required */
2470 ncp->nc_refs = -1; /* safety */
2472 kfree(ncp->nc_name, M_VFSCACHE);
2473 kfree(ncp, M_VFSCACHE);
2476 * Delayed drop (we had to release our spinlocks)
2478 * The refed parent (if not NULL) must be dropped. The
2479 * caller is responsible for looping.
2487 * Clean up dangling negative cache and defered-drop entries in the
2490 typedef enum { CHI_LOW, CHI_HIGH } cache_hs_t;
2492 static cache_hs_t neg_cache_hysteresis_state = CHI_LOW;
2493 static cache_hs_t pos_cache_hysteresis_state = CHI_LOW;
2496 cache_hysteresis(void)
2501 * Don't cache too many negative hits. We use hysteresis to reduce
2502 * the impact on the critical path.
2504 switch(neg_cache_hysteresis_state) {
2506 if (numneg > MINNEG && numneg * ncnegfactor > numcache) {
2507 _cache_cleanneg(10);
2508 neg_cache_hysteresis_state = CHI_HIGH;
2512 if (numneg > MINNEG * 9 / 10 &&
2513 numneg * ncnegfactor * 9 / 10 > numcache
2515 _cache_cleanneg(10);
2517 neg_cache_hysteresis_state = CHI_LOW;
2523 * Don't cache too many positive hits. We use hysteresis to reduce
2524 * the impact on the critical path.
2526 * Excessive positive hits can accumulate due to large numbers of
2527 * hardlinks (the vnode cache will not prevent hl ncps from growing
2530 if ((poslimit = ncposlimit) == 0)
2531 poslimit = desiredvnodes * 2;
2533 switch(pos_cache_hysteresis_state) {
2535 if (numcache > poslimit && numcache > MINPOS) {
2536 _cache_cleanpos(10);
2537 pos_cache_hysteresis_state = CHI_HIGH;
2541 if (numcache > poslimit * 5 / 6 && numcache > MINPOS) {
2542 _cache_cleanpos(10);
2544 pos_cache_hysteresis_state = CHI_LOW;
2550 * Clean out dangling defered-zap ncps which could not
2551 * be cleanly dropped if too many build up. Note
2552 * that numdefered is not an exact number as such ncps
2553 * can be reused and the counter is not handled in a MP
2554 * safe manner by design.
2556 if (numdefered * ncnegfactor > numcache) {
2557 _cache_cleandefered();
2562 * NEW NAMECACHE LOOKUP API
2564 * Lookup an entry in the namecache. The passed par_nch must be referenced
2565 * and unlocked. A referenced and locked nchandle with a non-NULL nch.ncp
2566 * is ALWAYS returned, eve if the supplied component is illegal.
2568 * The resulting namecache entry should be returned to the system with
2569 * cache_put() or cache_unlock() + cache_drop().
2571 * namecache locks are recursive but care must be taken to avoid lock order
2572 * reversals (hence why the passed par_nch must be unlocked). Locking
2573 * rules are to order for parent traversals, not for child traversals.
2575 * Nobody else will be able to manipulate the associated namespace (e.g.
2576 * create, delete, rename, rename-target) until the caller unlocks the
2579 * The returned entry will be in one of three states: positive hit (non-null
2580 * vnode), negative hit (null vnode), or unresolved (NCF_UNRESOLVED is set).
2581 * Unresolved entries must be resolved through the filesystem to associate the
2582 * vnode and/or determine whether a positive or negative hit has occured.
2584 * It is not necessary to lock a directory in order to lock namespace under
2585 * that directory. In fact, it is explicitly not allowed to do that. A
2586 * directory is typically only locked when being created, renamed, or
2589 * The directory (par) may be unresolved, in which case any returned child
2590 * will likely also be marked unresolved. Likely but not guarenteed. Since
2591 * the filesystem lookup requires a resolved directory vnode the caller is
2592 * responsible for resolving the namecache chain top-down. This API
2593 * specifically allows whole chains to be created in an unresolved state.
2596 cache_nlookup(struct nchandle *par_nch, struct nlcomponent *nlc)
2598 struct nchandle nch;
2599 struct namecache *ncp;
2600 struct namecache *new_ncp;
2601 struct nchash_head *nchpp;
2609 mp = par_nch->mount;
2613 * This is a good time to call it, no ncp's are locked by
2619 * Try to locate an existing entry
2621 hash = fnv_32_buf(nlc->nlc_nameptr, nlc->nlc_namelen, FNV1_32_INIT);
2622 hash = fnv_32_buf(&par_nch->ncp, sizeof(par_nch->ncp), hash);
2624 nchpp = NCHHASH(hash);
2626 spin_lock(&nchpp->spin);
2627 LIST_FOREACH(ncp, &nchpp->list, nc_hash) {
2631 * Break out if we find a matching entry. Note that
2632 * UNRESOLVED entries may match, but DESTROYED entries
2635 if (ncp->nc_parent == par_nch->ncp &&
2636 ncp->nc_nlen == nlc->nlc_namelen &&
2637 bcmp(ncp->nc_name, nlc->nlc_nameptr, ncp->nc_nlen) == 0 &&
2638 (ncp->nc_flag & NCF_DESTROYED) == 0
2641 spin_unlock(&nchpp->spin);
2643 _cache_unlock(par_nch->ncp);
2646 if (_cache_lock_special(ncp) == 0) {
2647 _cache_auto_unresolve(mp, ncp);
2649 _cache_free(new_ncp);
2660 * We failed to locate an entry, create a new entry and add it to
2661 * the cache. The parent ncp must also be locked so we
2664 * We have to relookup after possibly blocking in kmalloc or
2665 * when locking par_nch.
2667 * NOTE: nlc_namelen can be 0 and nlc_nameptr NULL as a special
2668 * mount case, in which case nc_name will be NULL.
2670 if (new_ncp == NULL) {
2671 spin_unlock(&nchpp->spin);
2672 new_ncp = cache_alloc(nlc->nlc_namelen);
2673 if (nlc->nlc_namelen) {
2674 bcopy(nlc->nlc_nameptr, new_ncp->nc_name,
2676 new_ncp->nc_name[nlc->nlc_namelen] = 0;
2680 if (par_locked == 0) {
2681 spin_unlock(&nchpp->spin);
2682 _cache_lock(par_nch->ncp);
2688 * WARNING! We still hold the spinlock. We have to set the hash
2689 * table entry atomically.
2692 _cache_link_parent(ncp, par_nch->ncp, nchpp);
2693 spin_unlock(&nchpp->spin);
2694 _cache_unlock(par_nch->ncp);
2695 /* par_locked = 0 - not used */
2698 * stats and namecache size management
2700 if (ncp->nc_flag & NCF_UNRESOLVED)
2701 ++gd->gd_nchstats->ncs_miss;
2702 else if (ncp->nc_vp)
2703 ++gd->gd_nchstats->ncs_goodhits;
2705 ++gd->gd_nchstats->ncs_neghits;
2708 atomic_add_int(&nch.mount->mnt_refs, 1);
2713 * Attempt to lookup a namecache entry and return with a shared namecache
2717 cache_nlookup_maybe_shared(struct nchandle *par_nch, struct nlcomponent *nlc,
2718 int excl, struct nchandle *res_nch)
2720 struct namecache *ncp;
2721 struct nchash_head *nchpp;
2727 * If exclusive requested or shared namecache locks are disabled,
2730 if (ncp_shared_lock_disable || excl)
2731 return(EWOULDBLOCK);
2735 mp = par_nch->mount;
2738 * This is a good time to call it, no ncp's are locked by
2744 * Try to locate an existing entry
2746 hash = fnv_32_buf(nlc->nlc_nameptr, nlc->nlc_namelen, FNV1_32_INIT);
2747 hash = fnv_32_buf(&par_nch->ncp, sizeof(par_nch->ncp), hash);
2748 nchpp = NCHHASH(hash);
2750 spin_lock(&nchpp->spin);
2752 LIST_FOREACH(ncp, &nchpp->list, nc_hash) {
2756 * Break out if we find a matching entry. Note that
2757 * UNRESOLVED entries may match, but DESTROYED entries
2760 if (ncp->nc_parent == par_nch->ncp &&
2761 ncp->nc_nlen == nlc->nlc_namelen &&
2762 bcmp(ncp->nc_name, nlc->nlc_nameptr, ncp->nc_nlen) == 0 &&
2763 (ncp->nc_flag & NCF_DESTROYED) == 0
2766 spin_unlock(&nchpp->spin);
2767 if (_cache_lock_shared_special(ncp) == 0) {
2768 if ((ncp->nc_flag & NCF_UNRESOLVED) == 0 &&
2769 (ncp->nc_flag & NCF_DESTROYED) == 0 &&
2770 _cache_auto_unresolve_test(mp, ncp) == 0) {
2776 spin_lock(&nchpp->spin);
2784 spin_unlock(&nchpp->spin);
2785 return(EWOULDBLOCK);
2790 * Note that nc_error might be non-zero (e.g ENOENT).
2793 res_nch->mount = mp;
2795 ++gd->gd_nchstats->ncs_goodhits;
2796 atomic_add_int(&res_nch->mount->mnt_refs, 1);
2798 KKASSERT(ncp->nc_error != EWOULDBLOCK);
2799 return(ncp->nc_error);
2803 * This is a non-blocking verison of cache_nlookup() used by
2804 * nfs_readdirplusrpc_uio(). It can fail for any reason and
2805 * will return nch.ncp == NULL in that case.
2808 cache_nlookup_nonblock(struct nchandle *par_nch, struct nlcomponent *nlc)
2810 struct nchandle nch;
2811 struct namecache *ncp;
2812 struct namecache *new_ncp;
2813 struct nchash_head *nchpp;
2821 mp = par_nch->mount;
2825 * Try to locate an existing entry
2827 hash = fnv_32_buf(nlc->nlc_nameptr, nlc->nlc_namelen, FNV1_32_INIT);
2828 hash = fnv_32_buf(&par_nch->ncp, sizeof(par_nch->ncp), hash);
2830 nchpp = NCHHASH(hash);
2832 spin_lock(&nchpp->spin);
2833 LIST_FOREACH(ncp, &nchpp->list, nc_hash) {
2837 * Break out if we find a matching entry. Note that
2838 * UNRESOLVED entries may match, but DESTROYED entries
2841 if (ncp->nc_parent == par_nch->ncp &&
2842 ncp->nc_nlen == nlc->nlc_namelen &&
2843 bcmp(ncp->nc_name, nlc->nlc_nameptr, ncp->nc_nlen) == 0 &&
2844 (ncp->nc_flag & NCF_DESTROYED) == 0
2847 spin_unlock(&nchpp->spin);
2849 _cache_unlock(par_nch->ncp);
2852 if (_cache_lock_special(ncp) == 0) {
2853 _cache_auto_unresolve(mp, ncp);
2855 _cache_free(new_ncp);
2866 * We failed to locate an entry, create a new entry and add it to
2867 * the cache. The parent ncp must also be locked so we
2870 * We have to relookup after possibly blocking in kmalloc or
2871 * when locking par_nch.
2873 * NOTE: nlc_namelen can be 0 and nlc_nameptr NULL as a special
2874 * mount case, in which case nc_name will be NULL.
2876 if (new_ncp == NULL) {
2877 spin_unlock(&nchpp->spin);
2878 new_ncp = cache_alloc(nlc->nlc_namelen);
2879 if (nlc->nlc_namelen) {
2880 bcopy(nlc->nlc_nameptr, new_ncp->nc_name,
2882 new_ncp->nc_name[nlc->nlc_namelen] = 0;
2886 if (par_locked == 0) {
2887 spin_unlock(&nchpp->spin);
2888 if (_cache_lock_nonblock(par_nch->ncp) == 0) {
2896 * WARNING! We still hold the spinlock. We have to set the hash
2897 * table entry atomically.
2900 _cache_link_parent(ncp, par_nch->ncp, nchpp);
2901 spin_unlock(&nchpp->spin);
2902 _cache_unlock(par_nch->ncp);
2903 /* par_locked = 0 - not used */
2906 * stats and namecache size management
2908 if (ncp->nc_flag & NCF_UNRESOLVED)
2909 ++gd->gd_nchstats->ncs_miss;
2910 else if (ncp->nc_vp)
2911 ++gd->gd_nchstats->ncs_goodhits;
2913 ++gd->gd_nchstats->ncs_neghits;
2916 atomic_add_int(&nch.mount->mnt_refs, 1);
2920 _cache_free(new_ncp);
2929 * The namecache entry is marked as being used as a mount point.
2930 * Locate the mount if it is visible to the caller. The DragonFly
2931 * mount system allows arbitrary loops in the topology and disentangles
2932 * those loops by matching against (mp, ncp) rather than just (ncp).
2933 * This means any given ncp can dive any number of mounts, depending
2934 * on the relative mount (e.g. nullfs) the caller is at in the topology.
2936 * We use a very simple frontend cache to reduce SMP conflicts,
2937 * which we have to do because the mountlist scan needs an exclusive
2938 * lock around its ripout info list. Not to mention that there might
2939 * be a lot of mounts.
2941 struct findmount_info {
2942 struct mount *result;
2943 struct mount *nch_mount;
2944 struct namecache *nch_ncp;
2948 struct ncmount_cache *
2949 ncmount_cache_lookup(struct mount *mp, struct namecache *ncp)
2953 hash = ((int)(intptr_t)mp / sizeof(*mp)) ^
2954 ((int)(intptr_t)ncp / sizeof(*ncp));
2955 hash = (hash & 0x7FFFFFFF) % NCMOUNT_NUMCACHE;
2956 return (&ncmount_cache[hash]);
2961 cache_findmount_callback(struct mount *mp, void *data)
2963 struct findmount_info *info = data;
2966 * Check the mount's mounted-on point against the passed nch.
2968 if (mp->mnt_ncmounton.mount == info->nch_mount &&
2969 mp->mnt_ncmounton.ncp == info->nch_ncp
2972 atomic_add_int(&mp->mnt_refs, 1);
2979 cache_findmount(struct nchandle *nch)
2981 struct findmount_info info;
2982 struct ncmount_cache *ncc;
2988 if (ncmount_cache_enable == 0) {
2992 ncc = ncmount_cache_lookup(nch->mount, nch->ncp);
2993 if (ncc->ncp == nch->ncp) {
2994 spin_lock_shared(&ncc->spin);
2995 if (ncc->isneg == 0 &&
2996 ncc->ncp == nch->ncp && (mp = ncc->mp) != NULL) {
2997 if (mp->mnt_ncmounton.mount == nch->mount &&
2998 mp->mnt_ncmounton.ncp == nch->ncp) {
3000 * Cache hit (positive)
3002 atomic_add_int(&mp->mnt_refs, 1);
3003 spin_unlock_shared(&ncc->spin);
3004 ++ncmount_cache_hit;
3007 /* else cache miss */
3010 ncc->ncp == nch->ncp && ncc->mp == nch->mount) {
3012 * Cache hit (negative)
3014 spin_unlock_shared(&ncc->spin);
3015 ++ncmount_cache_hit;
3018 spin_unlock_shared(&ncc->spin);
3026 info.nch_mount = nch->mount;
3027 info.nch_ncp = nch->ncp;
3028 mountlist_scan(cache_findmount_callback, &info,
3029 MNTSCAN_FORWARD|MNTSCAN_NOBUSY);
3034 * Negative lookups: We cache the originating {ncp,mp}. (mp) is
3035 * only used for pointer comparisons and is not
3036 * referenced (otherwise there would be dangling
3039 * Positive lookups: We cache the originating {ncp} and the target
3040 * (mp). (mp) is referenced.
3042 * Indeterminant: If the match is undergoing an unmount we do
3043 * not cache it to avoid racing cache_unmounting(),
3044 * but still return the match.
3047 spin_lock(&ncc->spin);
3048 if (info.result == NULL) {
3049 if (ncc->isneg == 0 && ncc->mp)
3050 atomic_add_int(&ncc->mp->mnt_refs, -1);
3051 ncc->ncp = nch->ncp;
3052 ncc->mp = nch->mount;
3054 spin_unlock(&ncc->spin);
3055 ++ncmount_cache_overwrite;
3056 } else if ((info.result->mnt_kern_flag & MNTK_UNMOUNT) == 0) {
3057 if (ncc->isneg == 0 && ncc->mp)
3058 atomic_add_int(&ncc->mp->mnt_refs, -1);
3059 atomic_add_int(&info.result->mnt_refs, 1);
3060 ncc->ncp = nch->ncp;
3061 ncc->mp = info.result;
3063 spin_unlock(&ncc->spin);
3064 ++ncmount_cache_overwrite;
3066 spin_unlock(&ncc->spin);
3068 ++ncmount_cache_miss;
3070 return(info.result);
3074 cache_dropmount(struct mount *mp)
3076 atomic_add_int(&mp->mnt_refs, -1);
3080 cache_ismounting(struct mount *mp)
3082 struct nchandle *nch = &mp->mnt_ncmounton;
3083 struct ncmount_cache *ncc;
3085 ncc = ncmount_cache_lookup(nch->mount, nch->ncp);
3087 ncc->ncp == nch->ncp && ncc->mp == nch->mount) {
3088 spin_lock(&ncc->spin);
3090 ncc->ncp == nch->ncp && ncc->mp == nch->mount) {
3094 spin_unlock(&ncc->spin);
3099 cache_unmounting(struct mount *mp)
3101 struct nchandle *nch = &mp->mnt_ncmounton;
3102 struct ncmount_cache *ncc;
3104 ncc = ncmount_cache_lookup(nch->mount, nch->ncp);
3105 if (ncc->isneg == 0 &&
3106 ncc->ncp == nch->ncp && ncc->mp == mp) {
3107 spin_lock(&ncc->spin);
3108 if (ncc->isneg == 0 &&
3109 ncc->ncp == nch->ncp && ncc->mp == mp) {
3110 atomic_add_int(&mp->mnt_refs, -1);
3114 spin_unlock(&ncc->spin);
3119 * Resolve an unresolved namecache entry, generally by looking it up.
3120 * The passed ncp must be locked and refd.
3122 * Theoretically since a vnode cannot be recycled while held, and since
3123 * the nc_parent chain holds its vnode as long as children exist, the
3124 * direct parent of the cache entry we are trying to resolve should
3125 * have a valid vnode. If not then generate an error that we can
3126 * determine is related to a resolver bug.
3128 * However, if a vnode was in the middle of a recyclement when the NCP
3129 * got locked, ncp->nc_vp might point to a vnode that is about to become
3130 * invalid. cache_resolve() handles this case by unresolving the entry
3131 * and then re-resolving it.
3133 * Note that successful resolution does not necessarily return an error
3134 * code of 0. If the ncp resolves to a negative cache hit then ENOENT
3138 cache_resolve(struct nchandle *nch, struct ucred *cred)
3140 struct namecache *par_tmp;
3141 struct namecache *par;
3142 struct namecache *ncp;
3143 struct nchandle nctmp;
3150 KKASSERT(_cache_lockstatus(ncp) == LK_EXCLUSIVE);
3153 * If the ncp is already resolved we have nothing to do. However,
3154 * we do want to guarentee that a usable vnode is returned when
3155 * a vnode is present, so make sure it hasn't been reclaimed.
3157 if ((ncp->nc_flag & NCF_UNRESOLVED) == 0) {
3158 if (ncp->nc_vp && (ncp->nc_vp->v_flag & VRECLAIMED))
3159 _cache_setunresolved(ncp);
3160 if ((ncp->nc_flag & NCF_UNRESOLVED) == 0)
3161 return (ncp->nc_error);
3165 * If the ncp was destroyed it will never resolve again. This
3166 * can basically only happen when someone is chdir'd into an
3167 * empty directory which is then rmdir'd. We want to catch this
3168 * here and not dive the VFS because the VFS might actually
3169 * have a way to re-resolve the disconnected ncp, which will
3170 * result in inconsistencies in the cdir/nch for proc->p_fd.
3172 if (ncp->nc_flag & NCF_DESTROYED) {
3173 kprintf("Warning: cache_resolve: ncp '%s' was unlinked\n",
3179 * Mount points need special handling because the parent does not
3180 * belong to the same filesystem as the ncp.
3182 if (ncp == mp->mnt_ncmountpt.ncp)
3183 return (cache_resolve_mp(mp));
3186 * We expect an unbroken chain of ncps to at least the mount point,
3187 * and even all the way to root (but this code doesn't have to go
3188 * past the mount point).
3190 if (ncp->nc_parent == NULL) {
3191 kprintf("EXDEV case 1 %p %*.*s\n", ncp,
3192 ncp->nc_nlen, ncp->nc_nlen, ncp->nc_name);
3193 ncp->nc_error = EXDEV;
3194 return(ncp->nc_error);
3198 * The vp's of the parent directories in the chain are held via vhold()
3199 * due to the existance of the child, and should not disappear.
3200 * However, there are cases where they can disappear:
3202 * - due to filesystem I/O errors.
3203 * - due to NFS being stupid about tracking the namespace and
3204 * destroys the namespace for entire directories quite often.
3205 * - due to forced unmounts.
3206 * - due to an rmdir (parent will be marked DESTROYED)
3208 * When this occurs we have to track the chain backwards and resolve
3209 * it, looping until the resolver catches up to the current node. We
3210 * could recurse here but we might run ourselves out of kernel stack
3211 * so we do it in a more painful manner. This situation really should
3212 * not occur all that often, or if it does not have to go back too
3213 * many nodes to resolve the ncp.
3215 while ((dvp = cache_dvpref(ncp)) == NULL) {
3217 * This case can occur if a process is CD'd into a
3218 * directory which is then rmdir'd. If the parent is marked
3219 * destroyed there is no point trying to resolve it.
3221 if (ncp->nc_parent->nc_flag & NCF_DESTROYED)
3223 par = ncp->nc_parent;
3226 while ((par_tmp = par->nc_parent) != NULL &&
3227 par_tmp->nc_vp == NULL) {
3228 _cache_hold(par_tmp);
3229 _cache_lock(par_tmp);
3233 if (par->nc_parent == NULL) {
3234 kprintf("EXDEV case 2 %*.*s\n",
3235 par->nc_nlen, par->nc_nlen, par->nc_name);
3239 kprintf("[diagnostic] cache_resolve: had to recurse on %*.*s\n",
3240 par->nc_nlen, par->nc_nlen, par->nc_name);
3242 * The parent is not set in stone, ref and lock it to prevent
3243 * it from disappearing. Also note that due to renames it
3244 * is possible for our ncp to move and for par to no longer
3245 * be one of its parents. We resolve it anyway, the loop
3246 * will handle any moves.
3248 _cache_get(par); /* additional hold/lock */
3249 _cache_put(par); /* from earlier hold/lock */
3250 if (par == nch->mount->mnt_ncmountpt.ncp) {
3251 cache_resolve_mp(nch->mount);
3252 } else if ((dvp = cache_dvpref(par)) == NULL) {
3253 kprintf("[diagnostic] cache_resolve: raced on %*.*s\n", par->nc_nlen, par->nc_nlen, par->nc_name);
3257 if (par->nc_flag & NCF_UNRESOLVED) {
3260 par->nc_error = VOP_NRESOLVE(&nctmp, dvp, cred);
3264 if ((error = par->nc_error) != 0) {
3265 if (par->nc_error != EAGAIN) {
3266 kprintf("EXDEV case 3 %*.*s error %d\n",
3267 par->nc_nlen, par->nc_nlen, par->nc_name,
3272 kprintf("[diagnostic] cache_resolve: EAGAIN par %p %*.*s\n",
3273 par, par->nc_nlen, par->nc_nlen, par->nc_name);
3280 * Call VOP_NRESOLVE() to get the vp, then scan for any disconnected
3281 * ncp's and reattach them. If this occurs the original ncp is marked
3282 * EAGAIN to force a relookup.
3284 * NOTE: in order to call VOP_NRESOLVE(), the parent of the passed
3285 * ncp must already be resolved.
3290 ncp->nc_error = VOP_NRESOLVE(&nctmp, dvp, cred);
3293 ncp->nc_error = EPERM;
3295 if (ncp->nc_error == EAGAIN) {
3296 kprintf("[diagnostic] cache_resolve: EAGAIN ncp %p %*.*s\n",
3297 ncp, ncp->nc_nlen, ncp->nc_nlen, ncp->nc_name);
3300 return(ncp->nc_error);
3304 * Resolve the ncp associated with a mount point. Such ncp's almost always
3305 * remain resolved and this routine is rarely called. NFS MPs tends to force
3306 * re-resolution more often due to its mac-truck-smash-the-namecache
3307 * method of tracking namespace changes.
3309 * The semantics for this call is that the passed ncp must be locked on
3310 * entry and will be locked on return. However, if we actually have to
3311 * resolve the mount point we temporarily unlock the entry in order to
3312 * avoid race-to-root deadlocks due to e.g. dead NFS mounts. Because of
3313 * the unlock we have to recheck the flags after we relock.
3316 cache_resolve_mp(struct mount *mp)
3318 struct namecache *ncp = mp->mnt_ncmountpt.ncp;
3322 KKASSERT(mp != NULL);
3325 * If the ncp is already resolved we have nothing to do. However,
3326 * we do want to guarentee that a usable vnode is returned when
3327 * a vnode is present, so make sure it hasn't been reclaimed.
3329 if ((ncp->nc_flag & NCF_UNRESOLVED) == 0) {
3330 if (ncp->nc_vp && (ncp->nc_vp->v_flag & VRECLAIMED))
3331 _cache_setunresolved(ncp);
3334 if (ncp->nc_flag & NCF_UNRESOLVED) {
3336 while (vfs_busy(mp, 0))
3338 error = VFS_ROOT(mp, &vp);
3342 * recheck the ncp state after relocking.
3344 if (ncp->nc_flag & NCF_UNRESOLVED) {
3345 ncp->nc_error = error;
3347 _cache_setvp(mp, ncp, vp);
3350 kprintf("[diagnostic] cache_resolve_mp: failed"
3351 " to resolve mount %p err=%d ncp=%p\n",
3353 _cache_setvp(mp, ncp, NULL);
3355 } else if (error == 0) {
3360 return(ncp->nc_error);
3364 * Clean out negative cache entries when too many have accumulated.
3367 _cache_cleanneg(int count)
3369 struct namecache *ncp;
3372 * Attempt to clean out the specified number of negative cache
3377 ncp = TAILQ_FIRST(&ncneglist);
3379 spin_unlock(&ncspin);
3382 TAILQ_REMOVE(&ncneglist, ncp, nc_vnode);
3383 TAILQ_INSERT_TAIL(&ncneglist, ncp, nc_vnode);
3385 spin_unlock(&ncspin);
3388 * This can race, so we must re-check that the ncp
3389 * is on the ncneglist after successfully locking it.
3391 if (_cache_lock_special(ncp) == 0) {
3392 if (ncp->nc_vp == NULL &&
3393 (ncp->nc_flag & NCF_UNRESOLVED) == 0) {
3394 ncp = cache_zap(ncp, 1);
3398 kprintf("cache_cleanneg: race avoided\n");
3409 * Clean out positive cache entries when too many have accumulated.
3412 _cache_cleanpos(int count)
3414 static volatile int rover;
3415 struct nchash_head *nchpp;
3416 struct namecache *ncp;
3420 * Attempt to clean out the specified number of negative cache
3424 rover_copy = ++rover; /* MPSAFEENOUGH */
3426 nchpp = NCHHASH(rover_copy);
3428 spin_lock(&nchpp->spin);
3429 ncp = LIST_FIRST(&nchpp->list);
3432 spin_unlock(&nchpp->spin);
3435 if (_cache_lock_special(ncp) == 0) {
3436 ncp = cache_zap(ncp, 1);
3448 * This is a kitchen sink function to clean out ncps which we
3449 * tried to zap from cache_drop() but failed because we were
3450 * unable to acquire the parent lock.
3452 * Such entries can also be removed via cache_inval_vp(), such
3453 * as when unmounting.
3456 _cache_cleandefered(void)
3458 struct nchash_head *nchpp;
3459 struct namecache *ncp;
3460 struct namecache dummy;
3464 bzero(&dummy, sizeof(dummy));
3465 dummy.nc_flag = NCF_DESTROYED;
3467 for (i = 0; i <= nchash; ++i) {
3468 nchpp = &nchashtbl[i];
3470 spin_lock(&nchpp->spin);
3471 LIST_INSERT_HEAD(&nchpp->list, &dummy, nc_hash);
3473 while ((ncp = LIST_NEXT(ncp, nc_hash)) != NULL) {
3474 if ((ncp->nc_flag & NCF_DEFEREDZAP) == 0)
3476 LIST_REMOVE(&dummy, nc_hash);
3477 LIST_INSERT_AFTER(ncp, &dummy, nc_hash);
3479 spin_unlock(&nchpp->spin);
3480 if (_cache_lock_nonblock(ncp) == 0) {
3481 ncp->nc_flag &= ~NCF_DEFEREDZAP;
3485 spin_lock(&nchpp->spin);
3488 LIST_REMOVE(&dummy, nc_hash);
3489 spin_unlock(&nchpp->spin);
3494 * Name cache initialization, from vfsinit() when we are booting
3502 /* initialise per-cpu namecache effectiveness statistics. */
3503 for (i = 0; i < ncpus; ++i) {
3504 gd = globaldata_find(i);
3505 gd->gd_nchstats = &nchstats[i];
3507 TAILQ_INIT(&ncneglist);
3509 nchashtbl = hashinit_ext(desiredvnodes / 2,
3510 sizeof(struct nchash_head),
3511 M_VFSCACHE, &nchash);
3512 for (i = 0; i <= (int)nchash; ++i) {
3513 LIST_INIT(&nchashtbl[i].list);
3514 spin_init(&nchashtbl[i].spin);
3516 for (i = 0; i < NCMOUNT_NUMCACHE; ++i)
3517 spin_init(&ncmount_cache[i].spin);
3518 nclockwarn = 5 * hz;
3522 * Called from start_init() to bootstrap the root filesystem. Returns
3523 * a referenced, unlocked namecache record.
3526 cache_allocroot(struct nchandle *nch, struct mount *mp, struct vnode *vp)
3528 nch->ncp = cache_alloc(0);
3530 atomic_add_int(&mp->mnt_refs, 1);
3532 _cache_setvp(nch->mount, nch->ncp, vp);
3536 * vfs_cache_setroot()
3538 * Create an association between the root of our namecache and
3539 * the root vnode. This routine may be called several times during
3542 * If the caller intends to save the returned namecache pointer somewhere
3543 * it must cache_hold() it.
3546 vfs_cache_setroot(struct vnode *nvp, struct nchandle *nch)
3549 struct nchandle onch;
3557 cache_zero(&rootnch);
3565 * XXX OLD API COMPAT FUNCTION. This really messes up the new namecache
3566 * topology and is being removed as quickly as possible. The new VOP_N*()
3567 * API calls are required to make specific adjustments using the supplied
3568 * ncp pointers rather then just bogusly purging random vnodes.
3570 * Invalidate all namecache entries to a particular vnode as well as
3571 * any direct children of that vnode in the namecache. This is a
3572 * 'catch all' purge used by filesystems that do not know any better.
3574 * Note that the linkage between the vnode and its namecache entries will
3575 * be removed, but the namecache entries themselves might stay put due to
3576 * active references from elsewhere in the system or due to the existance of
3577 * the children. The namecache topology is left intact even if we do not
3578 * know what the vnode association is. Such entries will be marked
3582 cache_purge(struct vnode *vp)
3584 cache_inval_vp(vp, CINV_DESTROY | CINV_CHILDREN);
3588 * Flush all entries referencing a particular filesystem.
3590 * Since we need to check it anyway, we will flush all the invalid
3591 * entries at the same time.
3596 cache_purgevfs(struct mount *mp)
3598 struct nchash_head *nchpp;
3599 struct namecache *ncp, *nnp;
3602 * Scan hash tables for applicable entries.
3604 for (nchpp = &nchashtbl[nchash]; nchpp >= nchashtbl; nchpp--) {
3605 spin_lock_wr(&nchpp->spin); XXX
3606 ncp = LIST_FIRST(&nchpp->list);
3610 nnp = LIST_NEXT(ncp, nc_hash);
3613 if (ncp->nc_mount == mp) {
3615 ncp = cache_zap(ncp, 0);
3623 spin_unlock_wr(&nchpp->spin); XXX
3629 static int disablecwd;
3630 SYSCTL_INT(_debug, OID_AUTO, disablecwd, CTLFLAG_RW, &disablecwd, 0,
3633 static u_long numcwdcalls;
3634 SYSCTL_ULONG(_vfs_cache, OID_AUTO, numcwdcalls, CTLFLAG_RD, &numcwdcalls, 0,
3635 "Number of current directory resolution calls");
3636 static u_long numcwdfailnf;
3637 SYSCTL_ULONG(_vfs_cache, OID_AUTO, numcwdfailnf, CTLFLAG_RD, &numcwdfailnf, 0,
3638 "Number of current directory failures due to lack of file");
3639 static u_long numcwdfailsz;
3640 SYSCTL_ULONG(_vfs_cache, OID_AUTO, numcwdfailsz, CTLFLAG_RD, &numcwdfailsz, 0,
3641 "Number of current directory failures due to large result");
3642 static u_long numcwdfound;
3643 SYSCTL_ULONG(_vfs_cache, OID_AUTO, numcwdfound, CTLFLAG_RD, &numcwdfound, 0,
3644 "Number of current directory resolution successes");
3650 sys___getcwd(struct __getcwd_args *uap)
3660 buflen = uap->buflen;
3663 if (buflen > MAXPATHLEN)
3664 buflen = MAXPATHLEN;
3666 buf = kmalloc(buflen, M_TEMP, M_WAITOK);
3667 bp = kern_getcwd(buf, buflen, &error);
3669 error = copyout(bp, uap->buf, strlen(bp) + 1);
3675 kern_getcwd(char *buf, size_t buflen, int *error)
3677 struct proc *p = curproc;
3679 int i, slash_prefixed;
3680 struct filedesc *fdp;
3681 struct nchandle nch;
3682 struct namecache *ncp;
3691 nch = fdp->fd_ncdir;
3696 while (ncp && (ncp != fdp->fd_nrdir.ncp ||
3697 nch.mount != fdp->fd_nrdir.mount)
3700 * While traversing upwards if we encounter the root
3701 * of the current mount we have to skip to the mount point
3702 * in the underlying filesystem.
3704 if (ncp == nch.mount->mnt_ncmountpt.ncp) {
3705 nch = nch.mount->mnt_ncmounton;
3714 * Prepend the path segment
3716 for (i = ncp->nc_nlen - 1; i >= 0; i--) {
3723 *--bp = ncp->nc_name[i];
3735 * Go up a directory. This isn't a mount point so we don't
3736 * have to check again.
3738 while ((nch.ncp = ncp->nc_parent) != NULL) {
3739 if (ncp_shared_lock_disable)
3742 _cache_lock_shared(ncp);
3743 if (nch.ncp != ncp->nc_parent) {
3747 _cache_hold(nch.ncp);
3760 if (!slash_prefixed) {
3778 * Thus begins the fullpath magic.
3780 * The passed nchp is referenced but not locked.
3782 static int disablefullpath;
3783 SYSCTL_INT(_debug, OID_AUTO, disablefullpath, CTLFLAG_RW,
3784 &disablefullpath, 0,
3785 "Disable fullpath lookups");
3787 static u_int numfullpathcalls;
3788 SYSCTL_UINT(_vfs_cache, OID_AUTO, numfullpathcalls, CTLFLAG_RD,
3789 &numfullpathcalls, 0,
3790 "Number of full path resolutions in progress");
3791 static u_int numfullpathfailnf;
3792 SYSCTL_UINT(_vfs_cache, OID_AUTO, numfullpathfailnf, CTLFLAG_RD,
3793 &numfullpathfailnf, 0,
3794 "Number of full path resolution failures due to lack of file");
3795 static u_int numfullpathfailsz;
3796 SYSCTL_UINT(_vfs_cache, OID_AUTO, numfullpathfailsz, CTLFLAG_RD,
3797 &numfullpathfailsz, 0,
3798 "Number of full path resolution failures due to insufficient memory");
3799 static u_int numfullpathfound;
3800 SYSCTL_UINT(_vfs_cache, OID_AUTO, numfullpathfound, CTLFLAG_RD,
3801 &numfullpathfound, 0,
3802 "Number of full path resolution successes");
3805 cache_fullpath(struct proc *p, struct nchandle *nchp, struct nchandle *nchbase,
3806 char **retbuf, char **freebuf, int guess)
3808 struct nchandle fd_nrdir;
3809 struct nchandle nch;
3810 struct namecache *ncp;
3811 struct mount *mp, *new_mp;
3817 atomic_add_int(&numfullpathcalls, -1);
3822 buf = kmalloc(MAXPATHLEN, M_TEMP, M_WAITOK);
3823 bp = buf + MAXPATHLEN - 1;
3826 fd_nrdir = *nchbase;
3828 fd_nrdir = p->p_fd->fd_nrdir;
3838 while (ncp && (ncp != fd_nrdir.ncp || mp != fd_nrdir.mount)) {
3842 * If we are asked to guess the upwards path, we do so whenever
3843 * we encounter an ncp marked as a mountpoint. We try to find
3844 * the actual mountpoint by finding the mountpoint with this
3847 if (guess && (ncp->nc_flag & NCF_ISMOUNTPT)) {
3848 new_mp = mount_get_by_nc(ncp);
3851 * While traversing upwards if we encounter the root
3852 * of the current mount we have to skip to the mount point.
3854 if (ncp == mp->mnt_ncmountpt.ncp) {
3858 nch = new_mp->mnt_ncmounton;
3868 * Prepend the path segment
3870 for (i = ncp->nc_nlen - 1; i >= 0; i--) {
3872 numfullpathfailsz++;
3877 *--bp = ncp->nc_name[i];
3880 numfullpathfailsz++;
3889 * Go up a directory. This isn't a mount point so we don't
3890 * have to check again.
3892 * We can only safely access nc_parent with ncp held locked.
3894 while ((nch.ncp = ncp->nc_parent) != NULL) {
3896 if (nch.ncp != ncp->nc_parent) {
3900 _cache_hold(nch.ncp);
3908 numfullpathfailnf++;
3914 if (!slash_prefixed) {
3916 numfullpathfailsz++;
3934 vn_fullpath(struct proc *p, struct vnode *vn, char **retbuf, char **freebuf,
3937 struct namecache *ncp;
3938 struct nchandle nch;
3942 atomic_add_int(&numfullpathcalls, 1);
3943 if (disablefullpath)
3949 /* vn is NULL, client wants us to use p->p_textvp */
3951 if ((vn = p->p_textvp) == NULL)
3954 spin_lock(&vn->v_spin);
3955 TAILQ_FOREACH(ncp, &vn->v_namecache, nc_vnode) {
3960 spin_unlock(&vn->v_spin);
3964 spin_unlock(&vn->v_spin);
3966 atomic_add_int(&numfullpathcalls, -1);
3968 nch.mount = vn->v_mount;
3969 error = cache_fullpath(p, &nch, NULL, retbuf, freebuf, guess);