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 MALLOC_DEFINE(M_VFSCACHE
, "vfscache", "VFS name cache entries");
131 LIST_HEAD(nchash_list
, namecache
);
134 struct nchash_list list
;
135 struct spinlock spin
;
138 static struct nchash_head
*nchashtbl
;
139 static struct namecache_list ncneglist
;
140 static struct spinlock ncspin
;
143 * ncvp_debug - debug cache_fromvp(). This is used by the NFS server
144 * to create the namecache infrastructure leading to a dangling vnode.
146 * 0 Only errors are reported
147 * 1 Successes are reported
148 * 2 Successes + the whole directory scan is reported
149 * 3 Force the directory scan code run as if the parent vnode did not
150 * have a namecache record, even if it does have one.
152 static int ncvp_debug
;
153 SYSCTL_INT(_debug
, OID_AUTO
, ncvp_debug
, CTLFLAG_RW
, &ncvp_debug
, 0, "");
155 static u_long nchash
; /* size of hash table */
156 SYSCTL_ULONG(_debug
, OID_AUTO
, nchash
, CTLFLAG_RD
, &nchash
, 0, "");
158 static int ncnegfactor
= 16; /* ratio of negative entries */
159 SYSCTL_INT(_debug
, OID_AUTO
, ncnegfactor
, CTLFLAG_RW
, &ncnegfactor
, 0, "");
161 static int nclockwarn
; /* warn on locked entries in ticks */
162 SYSCTL_INT(_debug
, OID_AUTO
, nclockwarn
, CTLFLAG_RW
, &nclockwarn
, 0, "");
164 static int numneg
; /* number of cache entries allocated */
165 SYSCTL_INT(_debug
, OID_AUTO
, numneg
, CTLFLAG_RD
, &numneg
, 0, "");
167 static int numdefered
; /* number of cache entries allocated */
168 SYSCTL_INT(_debug
, OID_AUTO
, numdefered
, CTLFLAG_RD
, &numdefered
, 0, "");
170 static int numcache
; /* number of cache entries allocated */
171 SYSCTL_INT(_debug
, OID_AUTO
, numcache
, CTLFLAG_RD
, &numcache
, 0, "");
173 SYSCTL_INT(_debug
, OID_AUTO
, vnsize
, CTLFLAG_RD
, 0, sizeof(struct vnode
), "");
174 SYSCTL_INT(_debug
, OID_AUTO
, ncsize
, CTLFLAG_RD
, 0, sizeof(struct namecache
), "");
177 SYSCTL_INT(_vfs
, OID_AUTO
, cache_mpsafe
, CTLFLAG_RW
, &cache_mpsafe
, 0, "");
179 static int cache_resolve_mp(struct mount
*mp
);
180 static struct vnode
*cache_dvpref(struct namecache
*ncp
);
181 static void _cache_lock(struct namecache
*ncp
);
182 static void _cache_setunresolved(struct namecache
*ncp
);
183 static void _cache_cleanneg(int count
);
184 static void _cache_cleandefered(void);
187 * The new name cache statistics
189 SYSCTL_NODE(_vfs
, OID_AUTO
, cache
, CTLFLAG_RW
, 0, "Name cache statistics");
190 #define STATNODE(mode, name, var) \
191 SYSCTL_ULONG(_vfs_cache, OID_AUTO, name, mode, var, 0, "");
192 STATNODE(CTLFLAG_RD
, numneg
, &numneg
);
193 STATNODE(CTLFLAG_RD
, numcache
, &numcache
);
194 static u_long numcalls
; STATNODE(CTLFLAG_RD
, numcalls
, &numcalls
);
195 static u_long dothits
; STATNODE(CTLFLAG_RD
, dothits
, &dothits
);
196 static u_long dotdothits
; STATNODE(CTLFLAG_RD
, dotdothits
, &dotdothits
);
197 static u_long numchecks
; STATNODE(CTLFLAG_RD
, numchecks
, &numchecks
);
198 static u_long nummiss
; STATNODE(CTLFLAG_RD
, nummiss
, &nummiss
);
199 static u_long nummisszap
; STATNODE(CTLFLAG_RD
, nummisszap
, &nummisszap
);
200 static u_long numposzaps
; STATNODE(CTLFLAG_RD
, numposzaps
, &numposzaps
);
201 static u_long numposhits
; STATNODE(CTLFLAG_RD
, numposhits
, &numposhits
);
202 static u_long numnegzaps
; STATNODE(CTLFLAG_RD
, numnegzaps
, &numnegzaps
);
203 static u_long numneghits
; STATNODE(CTLFLAG_RD
, numneghits
, &numneghits
);
205 struct nchstats nchstats
[SMP_MAXCPU
];
207 * Export VFS cache effectiveness statistics to user-land.
209 * The statistics are left for aggregation to user-land so
210 * neat things can be achieved, like observing per-CPU cache
214 sysctl_nchstats(SYSCTL_HANDLER_ARGS
)
216 struct globaldata
*gd
;
220 for (i
= 0; i
< ncpus
; ++i
) {
221 gd
= globaldata_find(i
);
222 if ((error
= SYSCTL_OUT(req
, (void *)&(*gd
->gd_nchstats
),
223 sizeof(struct nchstats
))))
229 SYSCTL_PROC(_vfs_cache
, OID_AUTO
, nchstats
, CTLTYPE_OPAQUE
|CTLFLAG_RD
,
230 0, 0, sysctl_nchstats
, "S,nchstats", "VFS cache effectiveness statistics");
232 static struct namecache
*cache_zap(struct namecache
*ncp
, int nonblock
);
235 * Namespace locking. The caller must already hold a reference to the
236 * namecache structure in order to lock/unlock it. This function prevents
237 * the namespace from being created or destroyed by accessors other then
240 * Note that holding a locked namecache structure prevents other threads
241 * from making namespace changes (e.g. deleting or creating), prevents
242 * vnode association state changes by other threads, and prevents the
243 * namecache entry from being resolved or unresolved by other threads.
245 * The lock owner has full authority to associate/disassociate vnodes
246 * and resolve/unresolve the locked ncp.
248 * The primary lock field is nc_exlocks. nc_locktd is set after the
249 * fact (when locking) or cleared prior to unlocking.
251 * WARNING! Holding a locked ncp will prevent a vnode from being destroyed
252 * or recycled, but it does NOT help you if the vnode had already
253 * initiated a recyclement. If this is important, use cache_get()
254 * rather then cache_lock() (and deal with the differences in the
255 * way the refs counter is handled). Or, alternatively, make an
256 * unconditional call to cache_validate() or cache_resolve()
257 * after cache_lock() returns.
263 _cache_lock(struct namecache
*ncp
)
270 KKASSERT(ncp
->nc_refs
!= 0);
275 count
= ncp
->nc_exlocks
;
278 if (atomic_cmpset_int(&ncp
->nc_exlocks
, 0, 1)) {
280 * The vp associated with a locked ncp must
281 * be held to prevent it from being recycled.
283 * WARNING! If VRECLAIMED is set the vnode
284 * could already be in the middle of a recycle.
285 * Callers must use cache_vref() or
286 * cache_vget() on the locked ncp to
287 * validate the vp or set the cache entry
290 * NOTE! vhold() is allowed if we hold a
291 * lock on the ncp (which we do).
295 vhold(ncp
->nc_vp
); /* MPSAFE */
301 if (ncp
->nc_locktd
== td
) {
302 if (atomic_cmpset_int(&ncp
->nc_exlocks
, count
,
309 tsleep_interlock(ncp
, 0);
310 if (atomic_cmpset_int(&ncp
->nc_exlocks
, count
,
311 count
| NC_EXLOCK_REQ
) == 0) {
315 error
= tsleep(ncp
, PINTERLOCKED
, "clock", nclockwarn
);
316 if (error
== EWOULDBLOCK
) {
319 kprintf("[diagnostic] cache_lock: blocked "
322 kprintf(" \"%*.*s\"\n",
323 ncp
->nc_nlen
, ncp
->nc_nlen
,
329 kprintf("[diagnostic] cache_lock: unblocked %*.*s after "
331 ncp
->nc_nlen
, ncp
->nc_nlen
, ncp
->nc_name
,
332 (int)(ticks
- didwarn
) / hz
);
337 * NOTE: nc_refs may be zero if the ncp is interlocked by circumstance,
338 * such as the case where one of its children is locked.
344 _cache_lock_nonblock(struct namecache
*ncp
)
352 count
= ncp
->nc_exlocks
;
355 if (atomic_cmpset_int(&ncp
->nc_exlocks
, 0, 1)) {
357 * The vp associated with a locked ncp must
358 * be held to prevent it from being recycled.
360 * WARNING! If VRECLAIMED is set the vnode
361 * could already be in the middle of a recycle.
362 * Callers must use cache_vref() or
363 * cache_vget() on the locked ncp to
364 * validate the vp or set the cache entry
367 * NOTE! vhold() is allowed if we hold a
368 * lock on the ncp (which we do).
372 vhold(ncp
->nc_vp
); /* MPSAFE */
378 if (ncp
->nc_locktd
== td
) {
379 if (atomic_cmpset_int(&ncp
->nc_exlocks
, count
,
394 * NOTE: nc_refs can be 0 (degenerate case during _cache_drop).
396 * nc_locktd must be NULLed out prior to nc_exlocks getting cleared.
402 _cache_unlock(struct namecache
*ncp
)
404 thread_t td __debugvar
= curthread
;
407 KKASSERT(ncp
->nc_refs
>= 0);
408 KKASSERT(ncp
->nc_exlocks
> 0);
409 KKASSERT(ncp
->nc_locktd
== td
);
411 count
= ncp
->nc_exlocks
;
412 if ((count
& ~NC_EXLOCK_REQ
) == 1) {
413 ncp
->nc_locktd
= NULL
;
418 if ((count
& ~NC_EXLOCK_REQ
) == 1) {
419 if (atomic_cmpset_int(&ncp
->nc_exlocks
, count
, 0)) {
420 if (count
& NC_EXLOCK_REQ
)
425 if (atomic_cmpset_int(&ncp
->nc_exlocks
, count
,
430 count
= ncp
->nc_exlocks
;
436 * cache_hold() and cache_drop() prevent the premature deletion of a
437 * namecache entry but do not prevent operations (such as zapping) on
438 * that namecache entry.
440 * This routine may only be called from outside this source module if
441 * nc_refs is already at least 1.
443 * This is a rare case where callers are allowed to hold a spinlock,
444 * so we can't ourselves.
450 _cache_hold(struct namecache
*ncp
)
452 atomic_add_int(&ncp
->nc_refs
, 1);
457 * Drop a cache entry, taking care to deal with races.
459 * For potential 1->0 transitions we must hold the ncp lock to safely
460 * test its flags. An unresolved entry with no children must be zapped
463 * The call to cache_zap() itself will handle all remaining races and
464 * will decrement the ncp's refs regardless. If we are resolved or
465 * have children nc_refs can safely be dropped to 0 without having to
468 * NOTE: cache_zap() will re-check nc_refs and nc_list in a MPSAFE fashion.
470 * NOTE: cache_zap() may return a non-NULL referenced parent which must
471 * be dropped in a loop.
477 _cache_drop(struct namecache
*ncp
)
482 KKASSERT(ncp
->nc_refs
> 0);
486 if (_cache_lock_nonblock(ncp
) == 0) {
487 ncp
->nc_flag
&= ~NCF_DEFEREDZAP
;
488 if ((ncp
->nc_flag
& NCF_UNRESOLVED
) &&
489 TAILQ_EMPTY(&ncp
->nc_list
)) {
490 ncp
= cache_zap(ncp
, 1);
493 if (atomic_cmpset_int(&ncp
->nc_refs
, 1, 0)) {
500 if (atomic_cmpset_int(&ncp
->nc_refs
, refs
, refs
- 1))
508 * Link a new namecache entry to its parent and to the hash table. Be
509 * careful to avoid races if vhold() blocks in the future.
511 * Both ncp and par must be referenced and locked.
513 * NOTE: The hash table spinlock is likely held during this call, we
514 * can't do anything fancy.
519 _cache_link_parent(struct namecache
*ncp
, struct namecache
*par
,
520 struct nchash_head
*nchpp
)
522 KKASSERT(ncp
->nc_parent
== NULL
);
523 ncp
->nc_parent
= par
;
524 ncp
->nc_head
= nchpp
;
525 LIST_INSERT_HEAD(&nchpp
->list
, ncp
, nc_hash
);
527 if (TAILQ_EMPTY(&par
->nc_list
)) {
528 TAILQ_INSERT_HEAD(&par
->nc_list
, ncp
, nc_entry
);
530 * Any vp associated with an ncp which has children must
531 * be held to prevent it from being recycled.
536 TAILQ_INSERT_HEAD(&par
->nc_list
, ncp
, nc_entry
);
541 * Remove the parent and hash associations from a namecache structure.
542 * If this is the last child of the parent the cache_drop(par) will
543 * attempt to recursively zap the parent.
545 * ncp must be locked. This routine will acquire a temporary lock on
546 * the parent as wlel as the appropriate hash chain.
551 _cache_unlink_parent(struct namecache
*ncp
)
553 struct namecache
*par
;
554 struct vnode
*dropvp
;
556 if ((par
= ncp
->nc_parent
) != NULL
) {
557 KKASSERT(ncp
->nc_parent
== par
);
560 spin_lock_wr(&ncp
->nc_head
->spin
);
561 LIST_REMOVE(ncp
, nc_hash
);
562 TAILQ_REMOVE(&par
->nc_list
, ncp
, nc_entry
);
564 if (par
->nc_vp
&& TAILQ_EMPTY(&par
->nc_list
))
566 spin_unlock_wr(&ncp
->nc_head
->spin
);
567 ncp
->nc_parent
= NULL
;
573 * We can only safely vdrop with no spinlocks held.
581 * Allocate a new namecache structure. Most of the code does not require
582 * zero-termination of the string but it makes vop_compat_ncreate() easier.
586 static struct namecache
*
587 cache_alloc(int nlen
)
589 struct namecache
*ncp
;
591 ncp
= kmalloc(sizeof(*ncp
), M_VFSCACHE
, M_WAITOK
|M_ZERO
);
593 ncp
->nc_name
= kmalloc(nlen
+ 1, M_VFSCACHE
, M_WAITOK
);
595 ncp
->nc_flag
= NCF_UNRESOLVED
;
596 ncp
->nc_error
= ENOTCONN
; /* needs to be resolved */
599 TAILQ_INIT(&ncp
->nc_list
);
605 * Can only be called for the case where the ncp has never been
606 * associated with anything (so no spinlocks are needed).
611 _cache_free(struct namecache
*ncp
)
613 KKASSERT(ncp
->nc_refs
== 1 && ncp
->nc_exlocks
== 1);
615 kfree(ncp
->nc_name
, M_VFSCACHE
);
616 kfree(ncp
, M_VFSCACHE
);
623 cache_zero(struct nchandle
*nch
)
630 * Ref and deref a namecache structure.
632 * The caller must specify a stable ncp pointer, typically meaning the
633 * ncp is already referenced but this can also occur indirectly through
634 * e.g. holding a lock on a direct child.
636 * WARNING: Caller may hold an unrelated read spinlock, which means we can't
637 * use read spinlocks here.
642 cache_hold(struct nchandle
*nch
)
644 _cache_hold(nch
->ncp
);
645 atomic_add_int(&nch
->mount
->mnt_refs
, 1);
650 * Create a copy of a namecache handle for an already-referenced
656 cache_copy(struct nchandle
*nch
, struct nchandle
*target
)
660 _cache_hold(target
->ncp
);
661 atomic_add_int(&nch
->mount
->mnt_refs
, 1);
668 cache_changemount(struct nchandle
*nch
, struct mount
*mp
)
670 atomic_add_int(&nch
->mount
->mnt_refs
, -1);
672 atomic_add_int(&nch
->mount
->mnt_refs
, 1);
679 cache_drop(struct nchandle
*nch
)
681 atomic_add_int(&nch
->mount
->mnt_refs
, -1);
682 _cache_drop(nch
->ncp
);
691 cache_lock(struct nchandle
*nch
)
693 _cache_lock(nch
->ncp
);
697 * Relock nch1 given an unlocked nch1 and a locked nch2. The caller
698 * is responsible for checking both for validity on return as they
699 * may have become invalid.
701 * We have to deal with potential deadlocks here, just ping pong
702 * the lock until we get it (we will always block somewhere when
703 * looping so this is not cpu-intensive).
705 * which = 0 nch1 not locked, nch2 is locked
706 * which = 1 nch1 is locked, nch2 is not locked
709 cache_relock(struct nchandle
*nch1
, struct ucred
*cred1
,
710 struct nchandle
*nch2
, struct ucred
*cred2
)
718 if (cache_lock_nonblock(nch1
) == 0) {
719 cache_resolve(nch1
, cred1
);
724 cache_resolve(nch1
, cred1
);
727 if (cache_lock_nonblock(nch2
) == 0) {
728 cache_resolve(nch2
, cred2
);
733 cache_resolve(nch2
, cred2
);
743 cache_lock_nonblock(struct nchandle
*nch
)
745 return(_cache_lock_nonblock(nch
->ncp
));
753 cache_unlock(struct nchandle
*nch
)
755 _cache_unlock(nch
->ncp
);
759 * ref-and-lock, unlock-and-deref functions.
761 * This function is primarily used by nlookup. Even though cache_lock
762 * holds the vnode, it is possible that the vnode may have already
763 * initiated a recyclement.
765 * We want cache_get() to return a definitively usable vnode or a
766 * definitively unresolved ncp.
772 _cache_get(struct namecache
*ncp
)
776 if (ncp
->nc_vp
&& (ncp
->nc_vp
->v_flag
& VRECLAIMED
))
777 _cache_setunresolved(ncp
);
782 * This is a special form of _cache_lock() which only succeeds if
783 * it can get a pristine, non-recursive lock. The caller must have
784 * already ref'd the ncp.
786 * On success the ncp will be locked, on failure it will not. The
787 * ref count does not change either way.
789 * We want _cache_lock_special() (on success) to return a definitively
790 * usable vnode or a definitively unresolved ncp.
795 _cache_lock_special(struct namecache
*ncp
)
797 if (_cache_lock_nonblock(ncp
) == 0) {
798 if ((ncp
->nc_exlocks
& ~NC_EXLOCK_REQ
) == 1) {
799 if (ncp
->nc_vp
&& (ncp
->nc_vp
->v_flag
& VRECLAIMED
))
800 _cache_setunresolved(ncp
);
810 * NOTE: The same nchandle can be passed for both arguments.
815 cache_get(struct nchandle
*nch
, struct nchandle
*target
)
817 KKASSERT(nch
->ncp
->nc_refs
> 0);
818 target
->mount
= nch
->mount
;
819 target
->ncp
= _cache_get(nch
->ncp
);
820 atomic_add_int(&target
->mount
->mnt_refs
, 1);
828 _cache_put(struct namecache
*ncp
)
838 cache_put(struct nchandle
*nch
)
840 atomic_add_int(&nch
->mount
->mnt_refs
, -1);
841 _cache_put(nch
->ncp
);
847 * Resolve an unresolved ncp by associating a vnode with it. If the
848 * vnode is NULL, a negative cache entry is created.
850 * The ncp should be locked on entry and will remain locked on return.
856 _cache_setvp(struct mount
*mp
, struct namecache
*ncp
, struct vnode
*vp
)
858 KKASSERT(ncp
->nc_flag
& NCF_UNRESOLVED
);
862 * Any vp associated with an ncp which has children must
863 * be held. Any vp associated with a locked ncp must be held.
865 if (!TAILQ_EMPTY(&ncp
->nc_list
))
867 spin_lock_wr(&vp
->v_spinlock
);
869 TAILQ_INSERT_HEAD(&vp
->v_namecache
, ncp
, nc_vnode
);
870 spin_unlock_wr(&vp
->v_spinlock
);
875 * Set auxiliary flags
879 ncp
->nc_flag
|= NCF_ISDIR
;
882 ncp
->nc_flag
|= NCF_ISSYMLINK
;
883 /* XXX cache the contents of the symlink */
888 atomic_add_int(&numcache
, 1);
892 * When creating a negative cache hit we set the
893 * namecache_gen. A later resolve will clean out the
894 * negative cache hit if the mount point's namecache_gen
895 * has changed. Used by devfs, could also be used by
899 spin_lock_wr(&ncspin
);
900 TAILQ_INSERT_TAIL(&ncneglist
, ncp
, nc_vnode
);
902 spin_unlock_wr(&ncspin
);
903 ncp
->nc_error
= ENOENT
;
905 ncp
->nc_namecache_gen
= mp
->mnt_namecache_gen
;
907 ncp
->nc_flag
&= ~(NCF_UNRESOLVED
| NCF_DEFEREDZAP
);
914 cache_setvp(struct nchandle
*nch
, struct vnode
*vp
)
916 _cache_setvp(nch
->mount
, nch
->ncp
, vp
);
923 cache_settimeout(struct nchandle
*nch
, int nticks
)
925 struct namecache
*ncp
= nch
->ncp
;
927 if ((ncp
->nc_timeout
= ticks
+ nticks
) == 0)
932 * Disassociate the vnode or negative-cache association and mark a
933 * namecache entry as unresolved again. Note that the ncp is still
934 * left in the hash table and still linked to its parent.
936 * The ncp should be locked and refd on entry and will remain locked and refd
939 * This routine is normally never called on a directory containing children.
940 * However, NFS often does just that in its rename() code as a cop-out to
941 * avoid complex namespace operations. This disconnects a directory vnode
942 * from its namecache and can cause the OLDAPI and NEWAPI to get out of
949 _cache_setunresolved(struct namecache
*ncp
)
953 if ((ncp
->nc_flag
& NCF_UNRESOLVED
) == 0) {
954 ncp
->nc_flag
|= NCF_UNRESOLVED
;
956 ncp
->nc_error
= ENOTCONN
;
957 if ((vp
= ncp
->nc_vp
) != NULL
) {
958 atomic_add_int(&numcache
, -1);
959 spin_lock_wr(&vp
->v_spinlock
);
961 TAILQ_REMOVE(&vp
->v_namecache
, ncp
, nc_vnode
);
962 spin_unlock_wr(&vp
->v_spinlock
);
965 * Any vp associated with an ncp with children is
966 * held by that ncp. Any vp associated with a locked
967 * ncp is held by that ncp. These conditions must be
968 * undone when the vp is cleared out from the ncp.
970 if (!TAILQ_EMPTY(&ncp
->nc_list
))
975 spin_lock_wr(&ncspin
);
976 TAILQ_REMOVE(&ncneglist
, ncp
, nc_vnode
);
978 spin_unlock_wr(&ncspin
);
980 ncp
->nc_flag
&= ~(NCF_WHITEOUT
|NCF_ISDIR
|NCF_ISSYMLINK
);
985 * The cache_nresolve() code calls this function to automatically
986 * set a resolved cache element to unresolved if it has timed out
987 * or if it is a negative cache hit and the mount point namecache_gen
993 _cache_auto_unresolve(struct mount
*mp
, struct namecache
*ncp
)
996 * Already in an unresolved state, nothing to do.
998 if (ncp
->nc_flag
& NCF_UNRESOLVED
)
1002 * Try to zap entries that have timed out. We have
1003 * to be careful here because locked leafs may depend
1004 * on the vnode remaining intact in a parent, so only
1005 * do this under very specific conditions.
1007 if (ncp
->nc_timeout
&& (int)(ncp
->nc_timeout
- ticks
) < 0 &&
1008 TAILQ_EMPTY(&ncp
->nc_list
)) {
1009 _cache_setunresolved(ncp
);
1014 * If a resolved negative cache hit is invalid due to
1015 * the mount's namecache generation being bumped, zap it.
1017 if (ncp
->nc_vp
== NULL
&&
1018 ncp
->nc_namecache_gen
!= mp
->mnt_namecache_gen
) {
1019 _cache_setunresolved(ncp
);
1028 cache_setunresolved(struct nchandle
*nch
)
1030 _cache_setunresolved(nch
->ncp
);
1034 * Determine if we can clear NCF_ISMOUNTPT by scanning the mountlist
1035 * looking for matches. This flag tells the lookup code when it must
1036 * check for a mount linkage and also prevents the directories in question
1037 * from being deleted or renamed.
1043 cache_clrmountpt_callback(struct mount
*mp
, void *data
)
1045 struct nchandle
*nch
= data
;
1047 if (mp
->mnt_ncmounton
.ncp
== nch
->ncp
)
1049 if (mp
->mnt_ncmountpt
.ncp
== nch
->ncp
)
1058 cache_clrmountpt(struct nchandle
*nch
)
1062 count
= mountlist_scan(cache_clrmountpt_callback
, nch
,
1063 MNTSCAN_FORWARD
|MNTSCAN_NOBUSY
);
1065 nch
->ncp
->nc_flag
&= ~NCF_ISMOUNTPT
;
1069 * Invalidate portions of the namecache topology given a starting entry.
1070 * The passed ncp is set to an unresolved state and:
1072 * The passed ncp must be referencxed and locked. The routine may unlock
1073 * and relock ncp several times, and will recheck the children and loop
1074 * to catch races. When done the passed ncp will be returned with the
1075 * reference and lock intact.
1077 * CINV_DESTROY - Set a flag in the passed ncp entry indicating
1078 * that the physical underlying nodes have been
1079 * destroyed... as in deleted. For example, when
1080 * a directory is removed. This will cause record
1081 * lookups on the name to no longer be able to find
1082 * the record and tells the resolver to return failure
1083 * rather then trying to resolve through the parent.
1085 * The topology itself, including ncp->nc_name,
1088 * This only applies to the passed ncp, if CINV_CHILDREN
1089 * is specified the children are not flagged.
1091 * CINV_CHILDREN - Set all children (recursively) to an unresolved
1094 * Note that this will also have the side effect of
1095 * cleaning out any unreferenced nodes in the topology
1096 * from the leaves up as the recursion backs out.
1098 * Note that the topology for any referenced nodes remains intact, but
1099 * the nodes will be marked as having been destroyed and will be set
1100 * to an unresolved state.
1102 * It is possible for cache_inval() to race a cache_resolve(), meaning that
1103 * the namecache entry may not actually be invalidated on return if it was
1104 * revalidated while recursing down into its children. This code guarentees
1105 * that the node(s) will go through an invalidation cycle, but does not
1106 * guarentee that they will remain in an invalidated state.
1108 * Returns non-zero if a revalidation was detected during the invalidation
1109 * recursion, zero otherwise. Note that since only the original ncp is
1110 * locked the revalidation ultimately can only indicate that the original ncp
1111 * *MIGHT* no have been reresolved.
1113 * DEEP RECURSION HANDLING - If a recursive invalidation recurses deeply we
1114 * have to avoid blowing out the kernel stack. We do this by saving the
1115 * deep namecache node and aborting the recursion, then re-recursing at that
1116 * node using a depth-first algorithm in order to allow multiple deep
1117 * recursions to chain through each other, then we restart the invalidation
1124 struct namecache
*resume_ncp
;
1128 static int _cache_inval_internal(struct namecache
*, int, struct cinvtrack
*);
1132 _cache_inval(struct namecache
*ncp
, int flags
)
1134 struct cinvtrack track
;
1135 struct namecache
*ncp2
;
1139 track
.resume_ncp
= NULL
;
1142 r
= _cache_inval_internal(ncp
, flags
, &track
);
1143 if (track
.resume_ncp
== NULL
)
1145 kprintf("Warning: deep namecache recursion at %s\n",
1148 while ((ncp2
= track
.resume_ncp
) != NULL
) {
1149 track
.resume_ncp
= NULL
;
1151 _cache_inval_internal(ncp2
, flags
& ~CINV_DESTROY
,
1161 cache_inval(struct nchandle
*nch
, int flags
)
1163 return(_cache_inval(nch
->ncp
, flags
));
1167 * Helper for _cache_inval(). The passed ncp is refd and locked and
1168 * remains that way on return, but may be unlocked/relocked multiple
1169 * times by the routine.
1172 _cache_inval_internal(struct namecache
*ncp
, int flags
, struct cinvtrack
*track
)
1174 struct namecache
*kid
;
1175 struct namecache
*nextkid
;
1178 KKASSERT(ncp
->nc_exlocks
);
1180 _cache_setunresolved(ncp
);
1181 if (flags
& CINV_DESTROY
)
1182 ncp
->nc_flag
|= NCF_DESTROYED
;
1183 if ((flags
& CINV_CHILDREN
) &&
1184 (kid
= TAILQ_FIRST(&ncp
->nc_list
)) != NULL
1187 if (++track
->depth
> MAX_RECURSION_DEPTH
) {
1188 track
->resume_ncp
= ncp
;
1194 if (track
->resume_ncp
) {
1198 if ((nextkid
= TAILQ_NEXT(kid
, nc_entry
)) != NULL
)
1199 _cache_hold(nextkid
);
1200 if ((kid
->nc_flag
& NCF_UNRESOLVED
) == 0 ||
1201 TAILQ_FIRST(&kid
->nc_list
)
1204 rcnt
+= _cache_inval_internal(kid
, flags
& ~CINV_DESTROY
, track
);
1215 * Someone could have gotten in there while ncp was unlocked,
1218 if ((ncp
->nc_flag
& NCF_UNRESOLVED
) == 0)
1224 * Invalidate a vnode's namecache associations. To avoid races against
1225 * the resolver we do not invalidate a node which we previously invalidated
1226 * but which was then re-resolved while we were in the invalidation loop.
1228 * Returns non-zero if any namecache entries remain after the invalidation
1231 * NOTE: Unlike the namecache topology which guarentees that ncp's will not
1232 * be ripped out of the topology while held, the vnode's v_namecache
1233 * list has no such restriction. NCP's can be ripped out of the list
1234 * at virtually any time if not locked, even if held.
1236 * In addition, the v_namecache list itself must be locked via
1237 * the vnode's spinlock.
1242 cache_inval_vp(struct vnode
*vp
, int flags
)
1244 struct namecache
*ncp
;
1245 struct namecache
*next
;
1248 spin_lock_wr(&vp
->v_spinlock
);
1249 ncp
= TAILQ_FIRST(&vp
->v_namecache
);
1253 /* loop entered with ncp held and vp spin-locked */
1254 if ((next
= TAILQ_NEXT(ncp
, nc_vnode
)) != NULL
)
1256 spin_unlock_wr(&vp
->v_spinlock
);
1258 if (ncp
->nc_vp
!= vp
) {
1259 kprintf("Warning: cache_inval_vp: race-A detected on "
1260 "%s\n", ncp
->nc_name
);
1266 _cache_inval(ncp
, flags
);
1267 _cache_put(ncp
); /* also releases reference */
1269 spin_lock_wr(&vp
->v_spinlock
);
1270 if (ncp
&& ncp
->nc_vp
!= vp
) {
1271 spin_unlock_wr(&vp
->v_spinlock
);
1272 kprintf("Warning: cache_inval_vp: race-B detected on "
1273 "%s\n", ncp
->nc_name
);
1278 spin_unlock_wr(&vp
->v_spinlock
);
1279 return(TAILQ_FIRST(&vp
->v_namecache
) != NULL
);
1283 * This routine is used instead of the normal cache_inval_vp() when we
1284 * are trying to recycle otherwise good vnodes.
1286 * Return 0 on success, non-zero if not all namecache records could be
1287 * disassociated from the vnode (for various reasons).
1292 cache_inval_vp_nonblock(struct vnode
*vp
)
1294 struct namecache
*ncp
;
1295 struct namecache
*next
;
1297 spin_lock_wr(&vp
->v_spinlock
);
1298 ncp
= TAILQ_FIRST(&vp
->v_namecache
);
1302 /* loop entered with ncp held */
1303 if ((next
= TAILQ_NEXT(ncp
, nc_vnode
)) != NULL
)
1305 spin_unlock_wr(&vp
->v_spinlock
);
1306 if (_cache_lock_nonblock(ncp
)) {
1312 if (ncp
->nc_vp
!= vp
) {
1313 kprintf("Warning: cache_inval_vp: race-A detected on "
1314 "%s\n", ncp
->nc_name
);
1320 _cache_inval(ncp
, 0);
1321 _cache_put(ncp
); /* also releases reference */
1323 spin_lock_wr(&vp
->v_spinlock
);
1324 if (ncp
&& ncp
->nc_vp
!= vp
) {
1325 spin_unlock_wr(&vp
->v_spinlock
);
1326 kprintf("Warning: cache_inval_vp: race-B detected on "
1327 "%s\n", ncp
->nc_name
);
1332 spin_unlock_wr(&vp
->v_spinlock
);
1334 return(TAILQ_FIRST(&vp
->v_namecache
) != NULL
);
1338 * The source ncp has been renamed to the target ncp. Both fncp and tncp
1339 * must be locked. The target ncp is destroyed (as a normal rename-over
1340 * would destroy the target file or directory).
1342 * Because there may be references to the source ncp we cannot copy its
1343 * contents to the target. Instead the source ncp is relinked as the target
1344 * and the target ncp is removed from the namecache topology.
1349 cache_rename(struct nchandle
*fnch
, struct nchandle
*tnch
)
1351 struct namecache
*fncp
= fnch
->ncp
;
1352 struct namecache
*tncp
= tnch
->ncp
;
1353 struct namecache
*tncp_par
;
1354 struct nchash_head
*nchpp
;
1359 * Rename fncp (unlink)
1361 _cache_unlink_parent(fncp
);
1362 oname
= fncp
->nc_name
;
1363 fncp
->nc_name
= tncp
->nc_name
;
1364 fncp
->nc_nlen
= tncp
->nc_nlen
;
1365 tncp_par
= tncp
->nc_parent
;
1366 _cache_hold(tncp_par
);
1367 _cache_lock(tncp_par
);
1370 * Rename fncp (relink)
1372 hash
= fnv_32_buf(fncp
->nc_name
, fncp
->nc_nlen
, FNV1_32_INIT
);
1373 hash
= fnv_32_buf(&tncp_par
, sizeof(tncp_par
), hash
);
1374 nchpp
= NCHHASH(hash
);
1376 spin_lock_wr(&nchpp
->spin
);
1377 _cache_link_parent(fncp
, tncp_par
, nchpp
);
1378 spin_unlock_wr(&nchpp
->spin
);
1380 _cache_put(tncp_par
);
1383 * Get rid of the overwritten tncp (unlink)
1385 _cache_setunresolved(tncp
);
1386 _cache_unlink_parent(tncp
);
1387 tncp
->nc_name
= NULL
;
1391 kfree(oname
, M_VFSCACHE
);
1395 * vget the vnode associated with the namecache entry. Resolve the namecache
1396 * entry if necessary. The passed ncp must be referenced and locked.
1398 * lk_type may be LK_SHARED, LK_EXCLUSIVE. A ref'd, possibly locked
1399 * (depending on the passed lk_type) will be returned in *vpp with an error
1400 * of 0, or NULL will be returned in *vpp with a non-0 error code. The
1401 * most typical error is ENOENT, meaning that the ncp represents a negative
1402 * cache hit and there is no vnode to retrieve, but other errors can occur
1405 * The vget() can race a reclaim. If this occurs we re-resolve the
1408 * There are numerous places in the kernel where vget() is called on a
1409 * vnode while one or more of its namecache entries is locked. Releasing
1410 * a vnode never deadlocks against locked namecache entries (the vnode
1411 * will not get recycled while referenced ncp's exist). This means we
1412 * can safely acquire the vnode. In fact, we MUST NOT release the ncp
1413 * lock when acquiring the vp lock or we might cause a deadlock.
1418 cache_vget(struct nchandle
*nch
, struct ucred
*cred
,
1419 int lk_type
, struct vnode
**vpp
)
1421 struct namecache
*ncp
;
1426 KKASSERT(ncp
->nc_locktd
== curthread
);
1429 if (ncp
->nc_flag
& NCF_UNRESOLVED
)
1430 error
= cache_resolve(nch
, cred
);
1434 if (error
== 0 && (vp
= ncp
->nc_vp
) != NULL
) {
1435 error
= vget(vp
, lk_type
);
1440 if (error
== ENOENT
) {
1441 kprintf("Warning: vnode reclaim race detected "
1442 "in cache_vget on %p (%s)\n",
1444 _cache_setunresolved(ncp
);
1449 * Not a reclaim race, some other error.
1451 KKASSERT(ncp
->nc_vp
== vp
);
1454 KKASSERT(ncp
->nc_vp
== vp
);
1455 KKASSERT((vp
->v_flag
& VRECLAIMED
) == 0);
1458 if (error
== 0 && vp
== NULL
)
1465 cache_vref(struct nchandle
*nch
, struct ucred
*cred
, struct vnode
**vpp
)
1467 struct namecache
*ncp
;
1472 KKASSERT(ncp
->nc_locktd
== curthread
);
1475 if (ncp
->nc_flag
& NCF_UNRESOLVED
)
1476 error
= cache_resolve(nch
, cred
);
1480 if (error
== 0 && (vp
= ncp
->nc_vp
) != NULL
) {
1481 error
= vget(vp
, LK_SHARED
);
1486 if (error
== ENOENT
) {
1487 kprintf("Warning: vnode reclaim race detected "
1488 "in cache_vget on %p (%s)\n",
1490 _cache_setunresolved(ncp
);
1495 * Not a reclaim race, some other error.
1497 KKASSERT(ncp
->nc_vp
== vp
);
1500 KKASSERT(ncp
->nc_vp
== vp
);
1501 KKASSERT((vp
->v_flag
& VRECLAIMED
) == 0);
1502 /* caller does not want a lock */
1506 if (error
== 0 && vp
== NULL
)
1513 * Return a referenced vnode representing the parent directory of
1516 * Because the caller has locked the ncp it should not be possible for
1517 * the parent ncp to go away. However, the parent can unresolve its
1518 * dvp at any time so we must be able to acquire a lock on the parent
1519 * to safely access nc_vp.
1521 * We have to leave par unlocked when vget()ing dvp to avoid a deadlock,
1522 * so use vhold()/vdrop() while holding the lock to prevent dvp from
1523 * getting destroyed.
1525 * MPSAFE - Note vhold() is allowed when dvp has 0 refs if we hold a
1526 * lock on the ncp in question..
1528 static struct vnode
*
1529 cache_dvpref(struct namecache
*ncp
)
1531 struct namecache
*par
;
1535 if ((par
= ncp
->nc_parent
) != NULL
) {
1538 if ((par
->nc_flag
& NCF_UNRESOLVED
) == 0) {
1539 if ((dvp
= par
->nc_vp
) != NULL
)
1544 if (vget(dvp
, LK_SHARED
) == 0) {
1547 /* return refd, unlocked dvp */
1559 * Convert a directory vnode to a namecache record without any other
1560 * knowledge of the topology. This ONLY works with directory vnodes and
1561 * is ONLY used by the NFS server. dvp must be refd but unlocked, and the
1562 * returned ncp (if not NULL) will be held and unlocked.
1564 * If 'makeit' is 0 and dvp has no existing namecache record, NULL is returned.
1565 * If 'makeit' is 1 we attempt to track-down and create the namecache topology
1566 * for dvp. This will fail only if the directory has been deleted out from
1569 * Callers must always check for a NULL return no matter the value of 'makeit'.
1571 * To avoid underflowing the kernel stack each recursive call increments
1572 * the makeit variable.
1575 static int cache_inefficient_scan(struct nchandle
*nch
, struct ucred
*cred
,
1576 struct vnode
*dvp
, char *fakename
);
1577 static int cache_fromdvp_try(struct vnode
*dvp
, struct ucred
*cred
,
1578 struct vnode
**saved_dvp
);
1581 cache_fromdvp(struct vnode
*dvp
, struct ucred
*cred
, int makeit
,
1582 struct nchandle
*nch
)
1584 struct vnode
*saved_dvp
;
1590 nch
->mount
= dvp
->v_mount
;
1595 * Handle the makeit == 0 degenerate case
1598 spin_lock_wr(&dvp
->v_spinlock
);
1599 nch
->ncp
= TAILQ_FIRST(&dvp
->v_namecache
);
1602 spin_unlock_wr(&dvp
->v_spinlock
);
1606 * Loop until resolution, inside code will break out on error.
1610 * Break out if we successfully acquire a working ncp.
1612 spin_lock_wr(&dvp
->v_spinlock
);
1613 nch
->ncp
= TAILQ_FIRST(&dvp
->v_namecache
);
1616 spin_unlock_wr(&dvp
->v_spinlock
);
1619 spin_unlock_wr(&dvp
->v_spinlock
);
1622 * If dvp is the root of its filesystem it should already
1623 * have a namecache pointer associated with it as a side
1624 * effect of the mount, but it may have been disassociated.
1626 if (dvp
->v_flag
& VROOT
) {
1627 nch
->ncp
= _cache_get(nch
->mount
->mnt_ncmountpt
.ncp
);
1628 error
= cache_resolve_mp(nch
->mount
);
1629 _cache_put(nch
->ncp
);
1631 kprintf("cache_fromdvp: resolve root of mount %p error %d",
1632 dvp
->v_mount
, error
);
1636 kprintf(" failed\n");
1641 kprintf(" succeeded\n");
1646 * If we are recursed too deeply resort to an O(n^2)
1647 * algorithm to resolve the namecache topology. The
1648 * resolved pvp is left referenced in saved_dvp to
1649 * prevent the tree from being destroyed while we loop.
1652 error
= cache_fromdvp_try(dvp
, cred
, &saved_dvp
);
1654 kprintf("lookupdotdot(longpath) failed %d "
1655 "dvp %p\n", error
, dvp
);
1663 * Get the parent directory and resolve its ncp.
1666 kfree(fakename
, M_TEMP
);
1669 error
= vop_nlookupdotdot(*dvp
->v_ops
, dvp
, &pvp
, cred
,
1672 kprintf("lookupdotdot failed %d dvp %p\n", error
, dvp
);
1678 * Reuse makeit as a recursion depth counter. On success
1679 * nch will be fully referenced.
1681 cache_fromdvp(pvp
, cred
, makeit
+ 1, nch
);
1683 if (nch
->ncp
== NULL
)
1687 * Do an inefficient scan of pvp (embodied by ncp) to look
1688 * for dvp. This will create a namecache record for dvp on
1689 * success. We loop up to recheck on success.
1691 * ncp and dvp are both held but not locked.
1693 error
= cache_inefficient_scan(nch
, cred
, dvp
, fakename
);
1695 kprintf("cache_fromdvp: scan %p (%s) failed on dvp=%p\n",
1696 pvp
, nch
->ncp
->nc_name
, dvp
);
1698 /* nch was NULLed out, reload mount */
1699 nch
->mount
= dvp
->v_mount
;
1703 kprintf("cache_fromdvp: scan %p (%s) succeeded\n",
1704 pvp
, nch
->ncp
->nc_name
);
1707 /* nch was NULLed out, reload mount */
1708 nch
->mount
= dvp
->v_mount
;
1712 * If nch->ncp is non-NULL it will have been held already.
1715 kfree(fakename
, M_TEMP
);
1724 * Go up the chain of parent directories until we find something
1725 * we can resolve into the namecache. This is very inefficient.
1729 cache_fromdvp_try(struct vnode
*dvp
, struct ucred
*cred
,
1730 struct vnode
**saved_dvp
)
1732 struct nchandle nch
;
1735 static time_t last_fromdvp_report
;
1739 * Loop getting the parent directory vnode until we get something we
1740 * can resolve in the namecache.
1743 nch
.mount
= dvp
->v_mount
;
1749 kfree(fakename
, M_TEMP
);
1752 error
= vop_nlookupdotdot(*dvp
->v_ops
, dvp
, &pvp
, cred
,
1759 spin_lock_wr(&pvp
->v_spinlock
);
1760 if ((nch
.ncp
= TAILQ_FIRST(&pvp
->v_namecache
)) != NULL
) {
1761 _cache_hold(nch
.ncp
);
1762 spin_unlock_wr(&pvp
->v_spinlock
);
1766 spin_unlock_wr(&pvp
->v_spinlock
);
1767 if (pvp
->v_flag
& VROOT
) {
1768 nch
.ncp
= _cache_get(pvp
->v_mount
->mnt_ncmountpt
.ncp
);
1769 error
= cache_resolve_mp(nch
.mount
);
1770 _cache_unlock(nch
.ncp
);
1773 _cache_drop(nch
.ncp
);
1783 if (last_fromdvp_report
!= time_second
) {
1784 last_fromdvp_report
= time_second
;
1785 kprintf("Warning: extremely inefficient path "
1786 "resolution on %s\n",
1789 error
= cache_inefficient_scan(&nch
, cred
, dvp
, fakename
);
1792 * Hopefully dvp now has a namecache record associated with
1793 * it. Leave it referenced to prevent the kernel from
1794 * recycling the vnode. Otherwise extremely long directory
1795 * paths could result in endless recycling.
1800 _cache_drop(nch
.ncp
);
1803 kfree(fakename
, M_TEMP
);
1808 * Do an inefficient scan of the directory represented by ncp looking for
1809 * the directory vnode dvp. ncp must be held but not locked on entry and
1810 * will be held on return. dvp must be refd but not locked on entry and
1811 * will remain refd on return.
1813 * Why do this at all? Well, due to its stateless nature the NFS server
1814 * converts file handles directly to vnodes without necessarily going through
1815 * the namecache ops that would otherwise create the namecache topology
1816 * leading to the vnode. We could either (1) Change the namecache algorithms
1817 * to allow disconnect namecache records that are re-merged opportunistically,
1818 * or (2) Make the NFS server backtrack and scan to recover a connected
1819 * namecache topology in order to then be able to issue new API lookups.
1821 * It turns out that (1) is a huge mess. It takes a nice clean set of
1822 * namecache algorithms and introduces a lot of complication in every subsystem
1823 * that calls into the namecache to deal with the re-merge case, especially
1824 * since we are using the namecache to placehold negative lookups and the
1825 * vnode might not be immediately assigned. (2) is certainly far less
1826 * efficient then (1), but since we are only talking about directories here
1827 * (which are likely to remain cached), the case does not actually run all
1828 * that often and has the supreme advantage of not polluting the namecache
1831 * If a fakename is supplied just construct a namecache entry using the
1835 cache_inefficient_scan(struct nchandle
*nch
, struct ucred
*cred
,
1836 struct vnode
*dvp
, char *fakename
)
1838 struct nlcomponent nlc
;
1839 struct nchandle rncp
;
1851 vat
.va_blocksize
= 0;
1852 if ((error
= VOP_GETATTR(dvp
, &vat
)) != 0)
1855 error
= cache_vref(nch
, cred
, &pvp
);
1860 kprintf("inefficient_scan: directory iosize %ld "
1861 "vattr fileid = %lld\n",
1863 (long long)vat
.va_fileid
);
1867 * Use the supplied fakename if not NULL. Fake names are typically
1868 * not in the actual filesystem hierarchy. This is used by HAMMER
1869 * to glue @@timestamp recursions together.
1872 nlc
.nlc_nameptr
= fakename
;
1873 nlc
.nlc_namelen
= strlen(fakename
);
1874 rncp
= cache_nlookup(nch
, &nlc
);
1878 if ((blksize
= vat
.va_blocksize
) == 0)
1879 blksize
= DEV_BSIZE
;
1880 rbuf
= kmalloc(blksize
, M_TEMP
, M_WAITOK
);
1886 iov
.iov_base
= rbuf
;
1887 iov
.iov_len
= blksize
;
1890 uio
.uio_resid
= blksize
;
1891 uio
.uio_segflg
= UIO_SYSSPACE
;
1892 uio
.uio_rw
= UIO_READ
;
1893 uio
.uio_td
= curthread
;
1895 if (ncvp_debug
>= 2)
1896 kprintf("cache_inefficient_scan: readdir @ %08x\n", (int)uio
.uio_offset
);
1897 error
= VOP_READDIR(pvp
, &uio
, cred
, &eofflag
, NULL
, NULL
);
1899 den
= (struct dirent
*)rbuf
;
1900 bytes
= blksize
- uio
.uio_resid
;
1903 if (ncvp_debug
>= 2) {
1904 kprintf("cache_inefficient_scan: %*.*s\n",
1905 den
->d_namlen
, den
->d_namlen
,
1908 if (den
->d_type
!= DT_WHT
&&
1909 den
->d_ino
== vat
.va_fileid
) {
1911 kprintf("cache_inefficient_scan: "
1912 "MATCHED inode %lld path %s/%*.*s\n",
1913 (long long)vat
.va_fileid
,
1915 den
->d_namlen
, den
->d_namlen
,
1918 nlc
.nlc_nameptr
= den
->d_name
;
1919 nlc
.nlc_namelen
= den
->d_namlen
;
1920 rncp
= cache_nlookup(nch
, &nlc
);
1921 KKASSERT(rncp
.ncp
!= NULL
);
1924 bytes
-= _DIRENT_DIRSIZ(den
);
1925 den
= _DIRENT_NEXT(den
);
1927 if (rncp
.ncp
== NULL
&& eofflag
== 0 && uio
.uio_resid
!= blksize
)
1930 kfree(rbuf
, M_TEMP
);
1934 if (rncp
.ncp
->nc_flag
& NCF_UNRESOLVED
) {
1935 _cache_setvp(rncp
.mount
, rncp
.ncp
, dvp
);
1936 if (ncvp_debug
>= 2) {
1937 kprintf("cache_inefficient_scan: setvp %s/%s = %p\n",
1938 nch
->ncp
->nc_name
, rncp
.ncp
->nc_name
, dvp
);
1941 if (ncvp_debug
>= 2) {
1942 kprintf("cache_inefficient_scan: setvp %s/%s already set %p/%p\n",
1943 nch
->ncp
->nc_name
, rncp
.ncp
->nc_name
, dvp
,
1947 if (rncp
.ncp
->nc_vp
== NULL
)
1948 error
= rncp
.ncp
->nc_error
;
1950 * Release rncp after a successful nlookup. rncp was fully
1955 kprintf("cache_inefficient_scan: dvp %p NOT FOUND in %s\n",
1956 dvp
, nch
->ncp
->nc_name
);
1963 * Zap a namecache entry. The ncp is unconditionally set to an unresolved
1964 * state, which disassociates it from its vnode or ncneglist.
1966 * Then, if there are no additional references to the ncp and no children,
1967 * the ncp is removed from the topology and destroyed.
1969 * References and/or children may exist if the ncp is in the middle of the
1970 * topology, preventing the ncp from being destroyed.
1972 * This function must be called with the ncp held and locked and will unlock
1973 * and drop it during zapping.
1975 * If nonblock is non-zero and the parent ncp cannot be locked we give up.
1976 * This case can occur in the cache_drop() path.
1978 * This function may returned a held (but NOT locked) parent node which the
1979 * caller must drop. We do this so _cache_drop() can loop, to avoid
1980 * blowing out the kernel stack.
1982 * WARNING! For MPSAFE operation this routine must acquire up to three
1983 * spin locks to be able to safely test nc_refs. Lock order is
1986 * hash spinlock if on hash list
1987 * parent spinlock if child of parent
1988 * (the ncp is unresolved so there is no vnode association)
1990 static struct namecache
*
1991 cache_zap(struct namecache
*ncp
, int nonblock
)
1993 struct namecache
*par
;
1994 struct vnode
*dropvp
;
1998 * Disassociate the vnode or negative cache ref and set NCF_UNRESOLVED.
2000 _cache_setunresolved(ncp
);
2003 * Try to scrap the entry and possibly tail-recurse on its parent.
2004 * We only scrap unref'd (other then our ref) unresolved entries,
2005 * we do not scrap 'live' entries.
2007 * Note that once the spinlocks are acquired if nc_refs == 1 no
2008 * other references are possible. If it isn't, however, we have
2009 * to decrement but also be sure to avoid a 1->0 transition.
2011 KKASSERT(ncp
->nc_flag
& NCF_UNRESOLVED
);
2012 KKASSERT(ncp
->nc_refs
> 0);
2015 * Acquire locks. Note that the parent can't go away while we hold
2018 if ((par
= ncp
->nc_parent
) != NULL
) {
2021 if (_cache_lock_nonblock(par
) == 0)
2023 refs
= ncp
->nc_refs
;
2024 ncp
->nc_flag
|= NCF_DEFEREDZAP
;
2025 ++numdefered
; /* MP race ok */
2026 if (atomic_cmpset_int(&ncp
->nc_refs
,
2038 spin_lock_wr(&ncp
->nc_head
->spin
);
2042 * If someone other then us has a ref or we have children
2043 * we cannot zap the entry. The 1->0 transition and any
2044 * further list operation is protected by the spinlocks
2045 * we have acquired but other transitions are not.
2048 refs
= ncp
->nc_refs
;
2049 if (refs
== 1 && TAILQ_EMPTY(&ncp
->nc_list
))
2051 if (atomic_cmpset_int(&ncp
->nc_refs
, refs
, refs
- 1)) {
2053 spin_unlock_wr(&ncp
->nc_head
->spin
);
2063 * We are the only ref and with the spinlocks held no further
2064 * refs can be acquired by others.
2066 * Remove us from the hash list and parent list. We have to
2067 * drop a ref on the parent's vp if the parent's list becomes
2072 struct nchash_head
*nchpp
= ncp
->nc_head
;
2074 KKASSERT(nchpp
!= NULL
);
2075 LIST_REMOVE(ncp
, nc_hash
);
2076 TAILQ_REMOVE(&par
->nc_list
, ncp
, nc_entry
);
2077 if (par
->nc_vp
&& TAILQ_EMPTY(&par
->nc_list
))
2078 dropvp
= par
->nc_vp
;
2079 ncp
->nc_head
= NULL
;
2080 ncp
->nc_parent
= NULL
;
2081 spin_unlock_wr(&nchpp
->spin
);
2084 KKASSERT(ncp
->nc_head
== NULL
);
2088 * ncp should not have picked up any refs. Physically
2091 KKASSERT(ncp
->nc_refs
== 1);
2092 /* _cache_unlock(ncp) not required */
2093 ncp
->nc_refs
= -1; /* safety */
2095 kfree(ncp
->nc_name
, M_VFSCACHE
);
2096 kfree(ncp
, M_VFSCACHE
);
2099 * Delayed drop (we had to release our spinlocks)
2101 * The refed parent (if not NULL) must be dropped. The
2102 * caller is responsible for looping.
2110 * Clean up dangling negative cache and defered-drop entries in the
2113 static enum { CHI_LOW
, CHI_HIGH
} cache_hysteresis_state
= CHI_LOW
;
2116 cache_hysteresis(void)
2119 * Don't cache too many negative hits. We use hysteresis to reduce
2120 * the impact on the critical path.
2122 switch(cache_hysteresis_state
) {
2124 if (numneg
> MINNEG
&& numneg
* ncnegfactor
> numcache
) {
2125 _cache_cleanneg(10);
2126 cache_hysteresis_state
= CHI_HIGH
;
2130 if (numneg
> MINNEG
* 9 / 10 &&
2131 numneg
* ncnegfactor
* 9 / 10 > numcache
2133 _cache_cleanneg(10);
2135 cache_hysteresis_state
= CHI_LOW
;
2141 * Clean out dangling defered-zap ncps which could not
2142 * be cleanly dropped if too many build up. Note
2143 * that numdefered is not an exact number as such ncps
2144 * can be reused and the counter is not handled in a MP
2145 * safe manner by design.
2147 if (numdefered
* ncnegfactor
> numcache
) {
2148 _cache_cleandefered();
2153 * NEW NAMECACHE LOOKUP API
2155 * Lookup an entry in the namecache. The passed par_nch must be referenced
2156 * and unlocked. A referenced and locked nchandle with a non-NULL nch.ncp
2157 * is ALWAYS returned, eve if the supplied component is illegal.
2159 * The resulting namecache entry should be returned to the system with
2160 * cache_put() or cache_unlock() + cache_drop().
2162 * namecache locks are recursive but care must be taken to avoid lock order
2163 * reversals (hence why the passed par_nch must be unlocked). Locking
2164 * rules are to order for parent traversals, not for child traversals.
2166 * Nobody else will be able to manipulate the associated namespace (e.g.
2167 * create, delete, rename, rename-target) until the caller unlocks the
2170 * The returned entry will be in one of three states: positive hit (non-null
2171 * vnode), negative hit (null vnode), or unresolved (NCF_UNRESOLVED is set).
2172 * Unresolved entries must be resolved through the filesystem to associate the
2173 * vnode and/or determine whether a positive or negative hit has occured.
2175 * It is not necessary to lock a directory in order to lock namespace under
2176 * that directory. In fact, it is explicitly not allowed to do that. A
2177 * directory is typically only locked when being created, renamed, or
2180 * The directory (par) may be unresolved, in which case any returned child
2181 * will likely also be marked unresolved. Likely but not guarenteed. Since
2182 * the filesystem lookup requires a resolved directory vnode the caller is
2183 * responsible for resolving the namecache chain top-down. This API
2184 * specifically allows whole chains to be created in an unresolved state.
2187 cache_nlookup(struct nchandle
*par_nch
, struct nlcomponent
*nlc
)
2189 struct nchandle nch
;
2190 struct namecache
*ncp
;
2191 struct namecache
*new_ncp
;
2192 struct nchash_head
*nchpp
;
2200 mp
= par_nch
->mount
;
2204 * This is a good time to call it, no ncp's are locked by
2210 * Try to locate an existing entry
2212 hash
= fnv_32_buf(nlc
->nlc_nameptr
, nlc
->nlc_namelen
, FNV1_32_INIT
);
2213 hash
= fnv_32_buf(&par_nch
->ncp
, sizeof(par_nch
->ncp
), hash
);
2215 nchpp
= NCHHASH(hash
);
2217 spin_lock_wr(&nchpp
->spin
);
2218 LIST_FOREACH(ncp
, &nchpp
->list
, nc_hash
) {
2222 * Break out if we find a matching entry. Note that
2223 * UNRESOLVED entries may match, but DESTROYED entries
2226 if (ncp
->nc_parent
== par_nch
->ncp
&&
2227 ncp
->nc_nlen
== nlc
->nlc_namelen
&&
2228 bcmp(ncp
->nc_name
, nlc
->nlc_nameptr
, ncp
->nc_nlen
) == 0 &&
2229 (ncp
->nc_flag
& NCF_DESTROYED
) == 0
2232 spin_unlock_wr(&nchpp
->spin
);
2234 _cache_unlock(par_nch
->ncp
);
2237 if (_cache_lock_special(ncp
) == 0) {
2238 _cache_auto_unresolve(mp
, ncp
);
2240 _cache_free(new_ncp
);
2251 * We failed to locate an entry, create a new entry and add it to
2252 * the cache. The parent ncp must also be locked so we
2255 * We have to relookup after possibly blocking in kmalloc or
2256 * when locking par_nch.
2258 * NOTE: nlc_namelen can be 0 and nlc_nameptr NULL as a special
2259 * mount case, in which case nc_name will be NULL.
2261 if (new_ncp
== NULL
) {
2262 spin_unlock_wr(&nchpp
->spin
);
2263 new_ncp
= cache_alloc(nlc
->nlc_namelen
);
2264 if (nlc
->nlc_namelen
) {
2265 bcopy(nlc
->nlc_nameptr
, new_ncp
->nc_name
,
2267 new_ncp
->nc_name
[nlc
->nlc_namelen
] = 0;
2271 if (par_locked
== 0) {
2272 spin_unlock_wr(&nchpp
->spin
);
2273 _cache_lock(par_nch
->ncp
);
2279 * WARNING! We still hold the spinlock. We have to set the hash
2280 * table entry atomically.
2283 _cache_link_parent(ncp
, par_nch
->ncp
, nchpp
);
2284 spin_unlock_wr(&nchpp
->spin
);
2285 _cache_unlock(par_nch
->ncp
);
2286 /* par_locked = 0 - not used */
2289 * stats and namecache size management
2291 if (ncp
->nc_flag
& NCF_UNRESOLVED
)
2292 ++gd
->gd_nchstats
->ncs_miss
;
2293 else if (ncp
->nc_vp
)
2294 ++gd
->gd_nchstats
->ncs_goodhits
;
2296 ++gd
->gd_nchstats
->ncs_neghits
;
2299 atomic_add_int(&nch
.mount
->mnt_refs
, 1);
2304 * This is a non-blocking verison of cache_nlookup() used by
2305 * nfs_readdirplusrpc_uio(). It can fail for any reason and
2306 * will return nch.ncp == NULL in that case.
2309 cache_nlookup_nonblock(struct nchandle
*par_nch
, struct nlcomponent
*nlc
)
2311 struct nchandle nch
;
2312 struct namecache
*ncp
;
2313 struct namecache
*new_ncp
;
2314 struct nchash_head
*nchpp
;
2322 mp
= par_nch
->mount
;
2326 * Try to locate an existing entry
2328 hash
= fnv_32_buf(nlc
->nlc_nameptr
, nlc
->nlc_namelen
, FNV1_32_INIT
);
2329 hash
= fnv_32_buf(&par_nch
->ncp
, sizeof(par_nch
->ncp
), hash
);
2331 nchpp
= NCHHASH(hash
);
2333 spin_lock_wr(&nchpp
->spin
);
2334 LIST_FOREACH(ncp
, &nchpp
->list
, nc_hash
) {
2338 * Break out if we find a matching entry. Note that
2339 * UNRESOLVED entries may match, but DESTROYED entries
2342 if (ncp
->nc_parent
== par_nch
->ncp
&&
2343 ncp
->nc_nlen
== nlc
->nlc_namelen
&&
2344 bcmp(ncp
->nc_name
, nlc
->nlc_nameptr
, ncp
->nc_nlen
) == 0 &&
2345 (ncp
->nc_flag
& NCF_DESTROYED
) == 0
2348 spin_unlock_wr(&nchpp
->spin
);
2350 _cache_unlock(par_nch
->ncp
);
2353 if (_cache_lock_special(ncp
) == 0) {
2354 _cache_auto_unresolve(mp
, ncp
);
2356 _cache_free(new_ncp
);
2367 * We failed to locate an entry, create a new entry and add it to
2368 * the cache. The parent ncp must also be locked so we
2371 * We have to relookup after possibly blocking in kmalloc or
2372 * when locking par_nch.
2374 * NOTE: nlc_namelen can be 0 and nlc_nameptr NULL as a special
2375 * mount case, in which case nc_name will be NULL.
2377 if (new_ncp
== NULL
) {
2378 spin_unlock_wr(&nchpp
->spin
);
2379 new_ncp
= cache_alloc(nlc
->nlc_namelen
);
2380 if (nlc
->nlc_namelen
) {
2381 bcopy(nlc
->nlc_nameptr
, new_ncp
->nc_name
,
2383 new_ncp
->nc_name
[nlc
->nlc_namelen
] = 0;
2387 if (par_locked
== 0) {
2388 spin_unlock_wr(&nchpp
->spin
);
2389 if (_cache_lock_nonblock(par_nch
->ncp
) == 0) {
2397 * WARNING! We still hold the spinlock. We have to set the hash
2398 * table entry atomically.
2401 _cache_link_parent(ncp
, par_nch
->ncp
, nchpp
);
2402 spin_unlock_wr(&nchpp
->spin
);
2403 _cache_unlock(par_nch
->ncp
);
2404 /* par_locked = 0 - not used */
2407 * stats and namecache size management
2409 if (ncp
->nc_flag
& NCF_UNRESOLVED
)
2410 ++gd
->gd_nchstats
->ncs_miss
;
2411 else if (ncp
->nc_vp
)
2412 ++gd
->gd_nchstats
->ncs_goodhits
;
2414 ++gd
->gd_nchstats
->ncs_neghits
;
2417 atomic_add_int(&nch
.mount
->mnt_refs
, 1);
2421 _cache_free(new_ncp
);
2430 * The namecache entry is marked as being used as a mount point.
2431 * Locate the mount if it is visible to the caller.
2433 struct findmount_info
{
2434 struct mount
*result
;
2435 struct mount
*nch_mount
;
2436 struct namecache
*nch_ncp
;
2441 cache_findmount_callback(struct mount
*mp
, void *data
)
2443 struct findmount_info
*info
= data
;
2446 * Check the mount's mounted-on point against the passed nch.
2448 if (mp
->mnt_ncmounton
.mount
== info
->nch_mount
&&
2449 mp
->mnt_ncmounton
.ncp
== info
->nch_ncp
2458 cache_findmount(struct nchandle
*nch
)
2460 struct findmount_info info
;
2463 info
.nch_mount
= nch
->mount
;
2464 info
.nch_ncp
= nch
->ncp
;
2465 mountlist_scan(cache_findmount_callback
, &info
,
2466 MNTSCAN_FORWARD
|MNTSCAN_NOBUSY
);
2467 return(info
.result
);
2471 * Resolve an unresolved namecache entry, generally by looking it up.
2472 * The passed ncp must be locked and refd.
2474 * Theoretically since a vnode cannot be recycled while held, and since
2475 * the nc_parent chain holds its vnode as long as children exist, the
2476 * direct parent of the cache entry we are trying to resolve should
2477 * have a valid vnode. If not then generate an error that we can
2478 * determine is related to a resolver bug.
2480 * However, if a vnode was in the middle of a recyclement when the NCP
2481 * got locked, ncp->nc_vp might point to a vnode that is about to become
2482 * invalid. cache_resolve() handles this case by unresolving the entry
2483 * and then re-resolving it.
2485 * Note that successful resolution does not necessarily return an error
2486 * code of 0. If the ncp resolves to a negative cache hit then ENOENT
2492 cache_resolve(struct nchandle
*nch
, struct ucred
*cred
)
2494 struct namecache
*par_tmp
;
2495 struct namecache
*par
;
2496 struct namecache
*ncp
;
2497 struct nchandle nctmp
;
2506 * If the ncp is already resolved we have nothing to do. However,
2507 * we do want to guarentee that a usable vnode is returned when
2508 * a vnode is present, so make sure it hasn't been reclaimed.
2510 if ((ncp
->nc_flag
& NCF_UNRESOLVED
) == 0) {
2511 if (ncp
->nc_vp
&& (ncp
->nc_vp
->v_flag
& VRECLAIMED
))
2512 _cache_setunresolved(ncp
);
2513 if ((ncp
->nc_flag
& NCF_UNRESOLVED
) == 0)
2514 return (ncp
->nc_error
);
2518 * Mount points need special handling because the parent does not
2519 * belong to the same filesystem as the ncp.
2521 if (ncp
== mp
->mnt_ncmountpt
.ncp
)
2522 return (cache_resolve_mp(mp
));
2525 * We expect an unbroken chain of ncps to at least the mount point,
2526 * and even all the way to root (but this code doesn't have to go
2527 * past the mount point).
2529 if (ncp
->nc_parent
== NULL
) {
2530 kprintf("EXDEV case 1 %p %*.*s\n", ncp
,
2531 ncp
->nc_nlen
, ncp
->nc_nlen
, ncp
->nc_name
);
2532 ncp
->nc_error
= EXDEV
;
2533 return(ncp
->nc_error
);
2537 * The vp's of the parent directories in the chain are held via vhold()
2538 * due to the existance of the child, and should not disappear.
2539 * However, there are cases where they can disappear:
2541 * - due to filesystem I/O errors.
2542 * - due to NFS being stupid about tracking the namespace and
2543 * destroys the namespace for entire directories quite often.
2544 * - due to forced unmounts.
2545 * - due to an rmdir (parent will be marked DESTROYED)
2547 * When this occurs we have to track the chain backwards and resolve
2548 * it, looping until the resolver catches up to the current node. We
2549 * could recurse here but we might run ourselves out of kernel stack
2550 * so we do it in a more painful manner. This situation really should
2551 * not occur all that often, or if it does not have to go back too
2552 * many nodes to resolve the ncp.
2554 while ((dvp
= cache_dvpref(ncp
)) == NULL
) {
2556 * This case can occur if a process is CD'd into a
2557 * directory which is then rmdir'd. If the parent is marked
2558 * destroyed there is no point trying to resolve it.
2560 if (ncp
->nc_parent
->nc_flag
& NCF_DESTROYED
)
2562 par
= ncp
->nc_parent
;
2565 while ((par_tmp
= par
->nc_parent
) != NULL
&&
2566 par_tmp
->nc_vp
== NULL
) {
2567 _cache_hold(par_tmp
);
2568 _cache_lock(par_tmp
);
2572 if (par
->nc_parent
== NULL
) {
2573 kprintf("EXDEV case 2 %*.*s\n",
2574 par
->nc_nlen
, par
->nc_nlen
, par
->nc_name
);
2578 kprintf("[diagnostic] cache_resolve: had to recurse on %*.*s\n",
2579 par
->nc_nlen
, par
->nc_nlen
, par
->nc_name
);
2581 * The parent is not set in stone, ref and lock it to prevent
2582 * it from disappearing. Also note that due to renames it
2583 * is possible for our ncp to move and for par to no longer
2584 * be one of its parents. We resolve it anyway, the loop
2585 * will handle any moves.
2587 _cache_get(par
); /* additional hold/lock */
2588 _cache_put(par
); /* from earlier hold/lock */
2589 if (par
== nch
->mount
->mnt_ncmountpt
.ncp
) {
2590 cache_resolve_mp(nch
->mount
);
2591 } else if ((dvp
= cache_dvpref(par
)) == NULL
) {
2592 kprintf("[diagnostic] cache_resolve: raced on %*.*s\n", par
->nc_nlen
, par
->nc_nlen
, par
->nc_name
);
2596 if (par
->nc_flag
& NCF_UNRESOLVED
) {
2599 par
->nc_error
= VOP_NRESOLVE(&nctmp
, dvp
, cred
);
2603 if ((error
= par
->nc_error
) != 0) {
2604 if (par
->nc_error
!= EAGAIN
) {
2605 kprintf("EXDEV case 3 %*.*s error %d\n",
2606 par
->nc_nlen
, par
->nc_nlen
, par
->nc_name
,
2611 kprintf("[diagnostic] cache_resolve: EAGAIN par %p %*.*s\n",
2612 par
, par
->nc_nlen
, par
->nc_nlen
, par
->nc_name
);
2619 * Call VOP_NRESOLVE() to get the vp, then scan for any disconnected
2620 * ncp's and reattach them. If this occurs the original ncp is marked
2621 * EAGAIN to force a relookup.
2623 * NOTE: in order to call VOP_NRESOLVE(), the parent of the passed
2624 * ncp must already be resolved.
2629 ncp
->nc_error
= VOP_NRESOLVE(&nctmp
, dvp
, cred
);
2632 ncp
->nc_error
= EPERM
;
2634 if (ncp
->nc_error
== EAGAIN
) {
2635 kprintf("[diagnostic] cache_resolve: EAGAIN ncp %p %*.*s\n",
2636 ncp
, ncp
->nc_nlen
, ncp
->nc_nlen
, ncp
->nc_name
);
2639 return(ncp
->nc_error
);
2643 * Resolve the ncp associated with a mount point. Such ncp's almost always
2644 * remain resolved and this routine is rarely called. NFS MPs tends to force
2645 * re-resolution more often due to its mac-truck-smash-the-namecache
2646 * method of tracking namespace changes.
2648 * The semantics for this call is that the passed ncp must be locked on
2649 * entry and will be locked on return. However, if we actually have to
2650 * resolve the mount point we temporarily unlock the entry in order to
2651 * avoid race-to-root deadlocks due to e.g. dead NFS mounts. Because of
2652 * the unlock we have to recheck the flags after we relock.
2655 cache_resolve_mp(struct mount
*mp
)
2657 struct namecache
*ncp
= mp
->mnt_ncmountpt
.ncp
;
2661 KKASSERT(mp
!= NULL
);
2664 * If the ncp is already resolved we have nothing to do. However,
2665 * we do want to guarentee that a usable vnode is returned when
2666 * a vnode is present, so make sure it hasn't been reclaimed.
2668 if ((ncp
->nc_flag
& NCF_UNRESOLVED
) == 0) {
2669 if (ncp
->nc_vp
&& (ncp
->nc_vp
->v_flag
& VRECLAIMED
))
2670 _cache_setunresolved(ncp
);
2673 if (ncp
->nc_flag
& NCF_UNRESOLVED
) {
2675 while (vfs_busy(mp
, 0))
2677 error
= VFS_ROOT(mp
, &vp
);
2681 * recheck the ncp state after relocking.
2683 if (ncp
->nc_flag
& NCF_UNRESOLVED
) {
2684 ncp
->nc_error
= error
;
2686 _cache_setvp(mp
, ncp
, vp
);
2689 kprintf("[diagnostic] cache_resolve_mp: failed"
2690 " to resolve mount %p err=%d ncp=%p\n",
2692 _cache_setvp(mp
, ncp
, NULL
);
2694 } else if (error
== 0) {
2699 return(ncp
->nc_error
);
2703 * Clean out negative cache entries when too many have accumulated.
2708 _cache_cleanneg(int count
)
2710 struct namecache
*ncp
;
2713 * Automode from the vnlru proc - clean out 10% of the negative cache
2717 count
= numneg
/ 10 + 1;
2720 * Attempt to clean out the specified number of negative cache
2724 spin_lock_wr(&ncspin
);
2725 ncp
= TAILQ_FIRST(&ncneglist
);
2727 spin_unlock_wr(&ncspin
);
2730 TAILQ_REMOVE(&ncneglist
, ncp
, nc_vnode
);
2731 TAILQ_INSERT_TAIL(&ncneglist
, ncp
, nc_vnode
);
2733 spin_unlock_wr(&ncspin
);
2734 if (_cache_lock_special(ncp
) == 0) {
2735 ncp
= cache_zap(ncp
, 0);
2746 * This is a kitchen sink function to clean out ncps which we
2747 * tried to zap from cache_drop() but failed because we were
2748 * unable to acquire the parent lock.
2750 * Such entries can also be removed via cache_inval_vp(), such
2751 * as when unmounting.
2756 _cache_cleandefered(void)
2758 struct nchash_head
*nchpp
;
2759 struct namecache
*ncp
;
2760 struct namecache dummy
;
2764 bzero(&dummy
, sizeof(dummy
));
2765 dummy
.nc_flag
= NCF_DESTROYED
;
2767 for (i
= 0; i
<= nchash
; ++i
) {
2768 nchpp
= &nchashtbl
[i
];
2770 spin_lock_wr(&nchpp
->spin
);
2771 LIST_INSERT_HEAD(&nchpp
->list
, &dummy
, nc_hash
);
2773 while ((ncp
= LIST_NEXT(ncp
, nc_hash
)) != NULL
) {
2774 if ((ncp
->nc_flag
& NCF_DEFEREDZAP
) == 0)
2776 LIST_REMOVE(&dummy
, nc_hash
);
2777 LIST_INSERT_AFTER(ncp
, &dummy
, nc_hash
);
2779 spin_unlock_wr(&nchpp
->spin
);
2780 if (_cache_lock_nonblock(ncp
) == 0) {
2781 ncp
->nc_flag
&= ~NCF_DEFEREDZAP
;
2785 spin_lock_wr(&nchpp
->spin
);
2788 LIST_REMOVE(&dummy
, nc_hash
);
2789 spin_unlock_wr(&nchpp
->spin
);
2794 * Name cache initialization, from vfsinit() when we are booting
2802 /* initialise per-cpu namecache effectiveness statistics. */
2803 for (i
= 0; i
< ncpus
; ++i
) {
2804 gd
= globaldata_find(i
);
2805 gd
->gd_nchstats
= &nchstats
[i
];
2807 TAILQ_INIT(&ncneglist
);
2809 nchashtbl
= hashinit_ext(desiredvnodes
*2, sizeof(struct nchash_head
),
2810 M_VFSCACHE
, &nchash
);
2811 for (i
= 0; i
<= (int)nchash
; ++i
) {
2812 LIST_INIT(&nchashtbl
[i
].list
);
2813 spin_init(&nchashtbl
[i
].spin
);
2815 nclockwarn
= 5 * hz
;
2819 * Called from start_init() to bootstrap the root filesystem. Returns
2820 * a referenced, unlocked namecache record.
2823 cache_allocroot(struct nchandle
*nch
, struct mount
*mp
, struct vnode
*vp
)
2825 nch
->ncp
= cache_alloc(0);
2827 atomic_add_int(&mp
->mnt_refs
, 1);
2829 _cache_setvp(nch
->mount
, nch
->ncp
, vp
);
2833 * vfs_cache_setroot()
2835 * Create an association between the root of our namecache and
2836 * the root vnode. This routine may be called several times during
2839 * If the caller intends to save the returned namecache pointer somewhere
2840 * it must cache_hold() it.
2843 vfs_cache_setroot(struct vnode
*nvp
, struct nchandle
*nch
)
2846 struct nchandle onch
;
2854 cache_zero(&rootnch
);
2862 * XXX OLD API COMPAT FUNCTION. This really messes up the new namecache
2863 * topology and is being removed as quickly as possible. The new VOP_N*()
2864 * API calls are required to make specific adjustments using the supplied
2865 * ncp pointers rather then just bogusly purging random vnodes.
2867 * Invalidate all namecache entries to a particular vnode as well as
2868 * any direct children of that vnode in the namecache. This is a
2869 * 'catch all' purge used by filesystems that do not know any better.
2871 * Note that the linkage between the vnode and its namecache entries will
2872 * be removed, but the namecache entries themselves might stay put due to
2873 * active references from elsewhere in the system or due to the existance of
2874 * the children. The namecache topology is left intact even if we do not
2875 * know what the vnode association is. Such entries will be marked
2879 cache_purge(struct vnode
*vp
)
2881 cache_inval_vp(vp
, CINV_DESTROY
| CINV_CHILDREN
);
2885 * Flush all entries referencing a particular filesystem.
2887 * Since we need to check it anyway, we will flush all the invalid
2888 * entries at the same time.
2893 cache_purgevfs(struct mount
*mp
)
2895 struct nchash_head
*nchpp
;
2896 struct namecache
*ncp
, *nnp
;
2899 * Scan hash tables for applicable entries.
2901 for (nchpp
= &nchashtbl
[nchash
]; nchpp
>= nchashtbl
; nchpp
--) {
2902 spin_lock_wr(&nchpp
->spin
); XXX
2903 ncp
= LIST_FIRST(&nchpp
->list
);
2907 nnp
= LIST_NEXT(ncp
, nc_hash
);
2910 if (ncp
->nc_mount
== mp
) {
2912 ncp
= cache_zap(ncp
, 0);
2920 spin_unlock_wr(&nchpp
->spin
); XXX
2926 static int disablecwd
;
2927 SYSCTL_INT(_debug
, OID_AUTO
, disablecwd
, CTLFLAG_RW
, &disablecwd
, 0, "");
2929 static u_long numcwdcalls
; STATNODE(CTLFLAG_RD
, numcwdcalls
, &numcwdcalls
);
2930 static u_long numcwdfail1
; STATNODE(CTLFLAG_RD
, numcwdfail1
, &numcwdfail1
);
2931 static u_long numcwdfail2
; STATNODE(CTLFLAG_RD
, numcwdfail2
, &numcwdfail2
);
2932 static u_long numcwdfail3
; STATNODE(CTLFLAG_RD
, numcwdfail3
, &numcwdfail3
);
2933 static u_long numcwdfail4
; STATNODE(CTLFLAG_RD
, numcwdfail4
, &numcwdfail4
);
2934 static u_long numcwdfound
; STATNODE(CTLFLAG_RD
, numcwdfound
, &numcwdfound
);
2940 sys___getcwd(struct __getcwd_args
*uap
)
2950 buflen
= uap
->buflen
;
2953 if (buflen
> MAXPATHLEN
)
2954 buflen
= MAXPATHLEN
;
2956 buf
= kmalloc(buflen
, M_TEMP
, M_WAITOK
);
2958 bp
= kern_getcwd(buf
, buflen
, &error
);
2961 error
= copyout(bp
, uap
->buf
, strlen(bp
) + 1);
2967 kern_getcwd(char *buf
, size_t buflen
, int *error
)
2969 struct proc
*p
= curproc
;
2971 int i
, slash_prefixed
;
2972 struct filedesc
*fdp
;
2973 struct nchandle nch
;
2974 struct namecache
*ncp
;
2983 nch
= fdp
->fd_ncdir
;
2988 while (ncp
&& (ncp
!= fdp
->fd_nrdir
.ncp
||
2989 nch
.mount
!= fdp
->fd_nrdir
.mount
)
2992 * While traversing upwards if we encounter the root
2993 * of the current mount we have to skip to the mount point
2994 * in the underlying filesystem.
2996 if (ncp
== nch
.mount
->mnt_ncmountpt
.ncp
) {
2997 nch
= nch
.mount
->mnt_ncmounton
;
3006 * Prepend the path segment
3008 for (i
= ncp
->nc_nlen
- 1; i
>= 0; i
--) {
3015 *--bp
= ncp
->nc_name
[i
];
3027 * Go up a directory. This isn't a mount point so we don't
3028 * have to check again.
3030 while ((nch
.ncp
= ncp
->nc_parent
) != NULL
) {
3032 if (nch
.ncp
!= ncp
->nc_parent
) {
3036 _cache_hold(nch
.ncp
);
3049 if (!slash_prefixed
) {
3067 * Thus begins the fullpath magic.
3069 * The passed nchp is referenced but not locked.
3072 #define STATNODE(name) \
3073 static u_int name; \
3074 SYSCTL_UINT(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, 0, "")
3076 static int disablefullpath
;
3077 SYSCTL_INT(_debug
, OID_AUTO
, disablefullpath
, CTLFLAG_RW
,
3078 &disablefullpath
, 0, "");
3080 STATNODE(numfullpathcalls
);
3081 STATNODE(numfullpathfail1
);
3082 STATNODE(numfullpathfail2
);
3083 STATNODE(numfullpathfail3
);
3084 STATNODE(numfullpathfail4
);
3085 STATNODE(numfullpathfound
);
3088 cache_fullpath(struct proc
*p
, struct nchandle
*nchp
,
3089 char **retbuf
, char **freebuf
)
3091 struct nchandle fd_nrdir
;
3092 struct nchandle nch
;
3093 struct namecache
*ncp
;
3100 atomic_add_int(&numfullpathcalls
, -1);
3105 buf
= kmalloc(MAXPATHLEN
, M_TEMP
, M_WAITOK
);
3106 bp
= buf
+ MAXPATHLEN
- 1;
3109 fd_nrdir
= p
->p_fd
->fd_nrdir
;
3119 while (ncp
&& (ncp
!= fd_nrdir
.ncp
|| mp
!= fd_nrdir
.mount
)) {
3121 * While traversing upwards if we encounter the root
3122 * of the current mount we have to skip to the mount point.
3124 if (ncp
== mp
->mnt_ncmountpt
.ncp
) {
3125 nch
= mp
->mnt_ncmounton
;
3135 * Prepend the path segment
3137 for (i
= ncp
->nc_nlen
- 1; i
>= 0; i
--) {
3144 *--bp
= ncp
->nc_name
[i
];
3156 * Go up a directory. This isn't a mount point so we don't
3157 * have to check again.
3159 * We can only safely access nc_parent with ncp held locked.
3161 while ((nch
.ncp
= ncp
->nc_parent
) != NULL
) {
3163 if (nch
.ncp
!= ncp
->nc_parent
) {
3167 _cache_hold(nch
.ncp
);
3181 if (!slash_prefixed
) {
3201 vn_fullpath(struct proc
*p
, struct vnode
*vn
, char **retbuf
, char **freebuf
)
3203 struct namecache
*ncp
;
3204 struct nchandle nch
;
3207 atomic_add_int(&numfullpathcalls
, 1);
3208 if (disablefullpath
)
3214 /* vn is NULL, client wants us to use p->p_textvp */
3216 if ((vn
= p
->p_textvp
) == NULL
)
3219 spin_lock_wr(&vn
->v_spinlock
);
3220 TAILQ_FOREACH(ncp
, &vn
->v_namecache
, nc_vnode
) {
3225 spin_unlock_wr(&vn
->v_spinlock
);
3229 spin_unlock_wr(&vn
->v_spinlock
);
3231 atomic_add_int(&numfullpathcalls
, -1);
3233 nch
.mount
= vn
->v_mount
;
3234 error
= cache_fullpath(p
, &nch
, retbuf
, freebuf
);