2 * Copyright (c) 2003,2004 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * Copyright (c) 1989, 1993, 1995
35 * The Regents of the University of California. All rights reserved.
37 * This code is derived from software contributed to Berkeley by
38 * Poul-Henning Kamp of the FreeBSD Project.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. All advertising materials mentioning features or use of this software
49 * must display the following acknowledgement:
50 * This product includes software developed by the University of
51 * California, Berkeley and its contributors.
52 * 4. Neither the name of the University nor the names of its contributors
53 * may be used to endorse or promote products derived from this software
54 * without specific prior written permission.
56 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
57 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
60 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * @(#)vfs_cache.c 8.5 (Berkeley) 3/22/95
69 * $FreeBSD: src/sys/kern/vfs_cache.c,v 1.42.2.6 2001/10/05 20:07:03 dillon Exp $
70 * $DragonFly: src/sys/kern/vfs_cache.c,v 1.59 2005/09/17 08:29:42 dillon Exp $
73 #include <sys/param.h>
74 #include <sys/systm.h>
75 #include <sys/kernel.h>
76 #include <sys/sysctl.h>
77 #include <sys/mount.h>
78 #include <sys/vnode.h>
79 #include <sys/malloc.h>
80 #include <sys/sysproto.h>
82 #include <sys/namei.h>
83 #include <sys/nlookup.h>
84 #include <sys/filedesc.h>
85 #include <sys/fnv_hash.h>
86 #include <sys/globaldata.h>
87 #include <sys/kern_syscall.h>
88 #include <sys/dirent.h>
92 * Random lookups in the cache are accomplished with a hash table using
93 * a hash key of (nc_src_vp, name).
95 * Negative entries may exist and correspond to structures where nc_vp
96 * is NULL. In a negative entry, NCF_WHITEOUT will be set if the entry
97 * corresponds to a whited-out directory entry (verses simply not finding the
100 * Upon reaching the last segment of a path, if the reference is for DELETE,
101 * or NOCACHE is set (rewrite), and the name is located in the cache, it
106 * Structures associated with name cacheing.
108 #define NCHHASH(hash) (&nchashtbl[(hash) & nchash])
111 MALLOC_DEFINE(M_VFSCACHE, "vfscache", "VFS name cache entries");
113 static LIST_HEAD(nchashhead, namecache) *nchashtbl; /* Hash Table */
114 static struct namecache_list ncneglist; /* instead of vnode */
117 * ncvp_debug - debug cache_fromvp(). This is used by the NFS server
118 * to create the namecache infrastructure leading to a dangling vnode.
120 * 0 Only errors are reported
121 * 1 Successes are reported
122 * 2 Successes + the whole directory scan is reported
123 * 3 Force the directory scan code run as if the parent vnode did not
124 * have a namecache record, even if it does have one.
126 static int ncvp_debug;
127 SYSCTL_INT(_debug, OID_AUTO, ncvp_debug, CTLFLAG_RW, &ncvp_debug, 0, "");
129 static u_long nchash; /* size of hash table */
130 SYSCTL_ULONG(_debug, OID_AUTO, nchash, CTLFLAG_RD, &nchash, 0, "");
132 static u_long ncnegfactor = 16; /* ratio of negative entries */
133 SYSCTL_ULONG(_debug, OID_AUTO, ncnegfactor, CTLFLAG_RW, &ncnegfactor, 0, "");
135 static int nclockwarn; /* warn on locked entries in ticks */
136 SYSCTL_INT(_debug, OID_AUTO, nclockwarn, CTLFLAG_RW, &nclockwarn, 0, "");
138 static u_long numneg; /* number of cache entries allocated */
139 SYSCTL_ULONG(_debug, OID_AUTO, numneg, CTLFLAG_RD, &numneg, 0, "");
141 static u_long numcache; /* number of cache entries allocated */
142 SYSCTL_ULONG(_debug, OID_AUTO, numcache, CTLFLAG_RD, &numcache, 0, "");
144 static u_long numunres; /* number of unresolved entries */
145 SYSCTL_ULONG(_debug, OID_AUTO, numunres, CTLFLAG_RD, &numunres, 0, "");
147 SYSCTL_INT(_debug, OID_AUTO, vnsize, CTLFLAG_RD, 0, sizeof(struct vnode), "");
148 SYSCTL_INT(_debug, OID_AUTO, ncsize, CTLFLAG_RD, 0, sizeof(struct namecache), "");
150 static int cache_resolve_mp(struct namecache *ncp);
151 static void cache_rehash(struct namecache *ncp);
154 * The new name cache statistics
156 SYSCTL_NODE(_vfs, OID_AUTO, cache, CTLFLAG_RW, 0, "Name cache statistics");
157 #define STATNODE(mode, name, var) \
158 SYSCTL_ULONG(_vfs_cache, OID_AUTO, name, mode, var, 0, "");
159 STATNODE(CTLFLAG_RD, numneg, &numneg);
160 STATNODE(CTLFLAG_RD, numcache, &numcache);
161 static u_long numcalls; STATNODE(CTLFLAG_RD, numcalls, &numcalls);
162 static u_long dothits; STATNODE(CTLFLAG_RD, dothits, &dothits);
163 static u_long dotdothits; STATNODE(CTLFLAG_RD, dotdothits, &dotdothits);
164 static u_long numchecks; STATNODE(CTLFLAG_RD, numchecks, &numchecks);
165 static u_long nummiss; STATNODE(CTLFLAG_RD, nummiss, &nummiss);
166 static u_long nummisszap; STATNODE(CTLFLAG_RD, nummisszap, &nummisszap);
167 static u_long numposzaps; STATNODE(CTLFLAG_RD, numposzaps, &numposzaps);
168 static u_long numposhits; STATNODE(CTLFLAG_RD, numposhits, &numposhits);
169 static u_long numnegzaps; STATNODE(CTLFLAG_RD, numnegzaps, &numnegzaps);
170 static u_long numneghits; STATNODE(CTLFLAG_RD, numneghits, &numneghits);
172 struct nchstats nchstats[SMP_MAXCPU];
174 * Export VFS cache effectiveness statistics to user-land.
176 * The statistics are left for aggregation to user-land so
177 * neat things can be achieved, like observing per-CPU cache
181 sysctl_nchstats(SYSCTL_HANDLER_ARGS)
183 struct globaldata *gd;
187 for (i = 0; i < ncpus; ++i) {
188 gd = globaldata_find(i);
189 if ((error = SYSCTL_OUT(req, (void *)&(*gd->gd_nchstats),
190 sizeof(struct nchstats))))
196 SYSCTL_PROC(_vfs_cache, OID_AUTO, nchstats, CTLTYPE_OPAQUE|CTLFLAG_RD,
197 0, 0, sysctl_nchstats, "S,nchstats", "VFS cache effectiveness statistics");
199 static void cache_zap(struct namecache *ncp);
202 * cache_hold() and cache_drop() prevent the premature deletion of a
203 * namecache entry but do not prevent operations (such as zapping) on
204 * that namecache entry.
208 _cache_hold(struct namecache *ncp)
215 * When dropping an entry, if only one ref remains and the entry has not
216 * been resolved, zap it. Since the one reference is being dropped the
217 * entry had better not be locked.
221 _cache_drop(struct namecache *ncp)
223 KKASSERT(ncp->nc_refs > 0);
224 if (ncp->nc_refs == 1 &&
225 (ncp->nc_flag & NCF_UNRESOLVED) &&
226 TAILQ_EMPTY(&ncp->nc_list)
228 KKASSERT(ncp->nc_exlocks == 0);
237 * Link a new namecache entry to its parent. Be careful to avoid races
238 * if vhold() blocks in the future.
240 * If we are creating a child under an oldapi parent we must mark the
241 * child as being an oldapi entry as well.
244 cache_link_parent(struct namecache *ncp, struct namecache *par)
246 KKASSERT(ncp->nc_parent == NULL);
247 ncp->nc_parent = par;
248 if (TAILQ_EMPTY(&par->nc_list)) {
249 TAILQ_INSERT_HEAD(&par->nc_list, ncp, nc_entry);
251 * Any vp associated with an ncp which has children must
252 * be held to prevent it from being recycled.
257 TAILQ_INSERT_HEAD(&par->nc_list, ncp, nc_entry);
262 * Remove the parent association from a namecache structure. If this is
263 * the last child of the parent the cache_drop(par) will attempt to
264 * recursively zap the parent.
267 cache_unlink_parent(struct namecache *ncp)
269 struct namecache *par;
271 if ((par = ncp->nc_parent) != NULL) {
272 ncp->nc_parent = NULL;
273 par = cache_hold(par);
274 TAILQ_REMOVE(&par->nc_list, ncp, nc_entry);
275 if (par->nc_vp && TAILQ_EMPTY(&par->nc_list))
282 * Allocate a new namecache structure. Most of the code does not require
283 * zero-termination of the string but it makes vop_compat_ncreate() easier.
285 static struct namecache *
286 cache_alloc(int nlen)
288 struct namecache *ncp;
290 ncp = malloc(sizeof(*ncp), M_VFSCACHE, M_WAITOK|M_ZERO);
292 ncp->nc_name = malloc(nlen + 1, M_VFSCACHE, M_WAITOK);
294 ncp->nc_flag = NCF_UNRESOLVED;
295 ncp->nc_error = ENOTCONN; /* needs to be resolved */
298 TAILQ_INIT(&ncp->nc_list);
304 cache_free(struct namecache *ncp)
306 KKASSERT(ncp->nc_refs == 1 && ncp->nc_exlocks == 1);
308 free(ncp->nc_name, M_VFSCACHE);
309 free(ncp, M_VFSCACHE);
313 * Ref and deref a namecache structure.
316 cache_hold(struct namecache *ncp)
318 return(_cache_hold(ncp));
322 cache_drop(struct namecache *ncp)
328 * Namespace locking. The caller must already hold a reference to the
329 * namecache structure in order to lock/unlock it. This function prevents
330 * the namespace from being created or destroyed by accessors other then
333 * Note that holding a locked namecache structure prevents other threads
334 * from making namespace changes (e.g. deleting or creating), prevents
335 * vnode association state changes by other threads, and prevents the
336 * namecache entry from being resolved or unresolved by other threads.
338 * The lock owner has full authority to associate/disassociate vnodes
339 * and resolve/unresolve the locked ncp.
341 * In particular, if a vnode is associated with a locked cache entry
342 * that vnode will *NOT* be recycled. We accomplish this by vhold()ing the
343 * vnode. XXX we should find a more efficient way to prevent the vnode
344 * from being recycled, but remember that any given vnode may have multiple
345 * namecache associations (think hardlinks).
348 cache_lock(struct namecache *ncp)
353 KKASSERT(ncp->nc_refs != 0);
358 if (ncp->nc_exlocks == 0) {
362 * The vp associated with a locked ncp must be held
363 * to prevent it from being recycled (which would
364 * cause the ncp to become unresolved).
366 * XXX loop on race for later MPSAFE work.
372 if (ncp->nc_locktd == td) {
376 ncp->nc_flag |= NCF_LOCKREQ;
377 if (tsleep(ncp, 0, "clock", nclockwarn) == EWOULDBLOCK) {
381 printf("[diagnostic] cache_lock: blocked on %p", ncp);
382 if ((ncp->nc_flag & NCF_MOUNTPT) && ncp->nc_mount)
383 printf(" [MOUNTFROM %s]\n", ncp->nc_mount->mnt_stat.f_mntfromname);
385 printf(" \"%*.*s\"\n",
386 ncp->nc_nlen, ncp->nc_nlen,
392 printf("[diagnostic] cache_lock: unblocked %*.*s\n",
393 ncp->nc_nlen, ncp->nc_nlen, ncp->nc_name);
398 cache_lock_nonblock(struct namecache *ncp)
402 KKASSERT(ncp->nc_refs != 0);
404 if (ncp->nc_exlocks == 0) {
408 * The vp associated with a locked ncp must be held
409 * to prevent it from being recycled (which would
410 * cause the ncp to become unresolved).
412 * XXX loop on race for later MPSAFE work.
423 cache_unlock(struct namecache *ncp)
425 thread_t td = curthread;
427 KKASSERT(ncp->nc_refs > 0);
428 KKASSERT(ncp->nc_exlocks > 0);
429 KKASSERT(ncp->nc_locktd == td);
430 if (--ncp->nc_exlocks == 0) {
433 ncp->nc_locktd = NULL;
434 if (ncp->nc_flag & NCF_LOCKREQ) {
435 ncp->nc_flag &= ~NCF_LOCKREQ;
442 * ref-and-lock, unlock-and-deref functions.
445 cache_get(struct namecache *ncp)
453 cache_get_nonblock(struct namecache *ncp)
456 if (ncp->nc_exlocks == 0 || ncp->nc_locktd == curthread) {
465 cache_put(struct namecache *ncp)
472 * Resolve an unresolved ncp by associating a vnode with it. If the
473 * vnode is NULL, a negative cache entry is created.
475 * The ncp should be locked on entry and will remain locked on return.
478 cache_setvp(struct namecache *ncp, struct vnode *vp)
480 KKASSERT(ncp->nc_flag & NCF_UNRESOLVED);
484 * Any vp associated with an ncp which has children must
485 * be held. Any vp associated with a locked ncp must be held.
487 if (!TAILQ_EMPTY(&ncp->nc_list))
489 TAILQ_INSERT_HEAD(&vp->v_namecache, ncp, nc_vnode);
494 * Set auxillary flags
498 ncp->nc_flag |= NCF_ISDIR;
501 ncp->nc_flag |= NCF_ISSYMLINK;
502 /* XXX cache the contents of the symlink */
510 TAILQ_INSERT_TAIL(&ncneglist, ncp, nc_vnode);
512 ncp->nc_error = ENOENT;
514 ncp->nc_flag &= ~NCF_UNRESOLVED;
518 cache_settimeout(struct namecache *ncp, int nticks)
520 if ((ncp->nc_timeout = ticks + nticks) == 0)
525 * Disassociate the vnode or negative-cache association and mark a
526 * namecache entry as unresolved again. Note that the ncp is still
527 * left in the hash table and still linked to its parent.
529 * The ncp should be locked and refd on entry and will remain locked and refd
532 * This routine is normally never called on a directory containing children.
533 * However, NFS often does just that in its rename() code as a cop-out to
534 * avoid complex namespace operations. This disconnects a directory vnode
535 * from its namecache and can cause the OLDAPI and NEWAPI to get out of
538 * NOTE: NCF_FSMID must be cleared so a refurbishment of the ncp, such as
539 * in a create, properly propogates flag up the chain.
542 cache_setunresolved(struct namecache *ncp)
546 if ((ncp->nc_flag & NCF_UNRESOLVED) == 0) {
547 ncp->nc_flag |= NCF_UNRESOLVED;
548 ncp->nc_flag &= ~(NCF_WHITEOUT|NCF_ISDIR|NCF_ISSYMLINK|
551 ncp->nc_error = ENOTCONN;
553 if ((vp = ncp->nc_vp) != NULL) {
556 TAILQ_REMOVE(&vp->v_namecache, ncp, nc_vnode);
559 * Any vp associated with an ncp with children is
560 * held by that ncp. Any vp associated with a locked
561 * ncp is held by that ncp. These conditions must be
562 * undone when the vp is cleared out from the ncp.
564 if (!TAILQ_EMPTY(&ncp->nc_list))
569 TAILQ_REMOVE(&ncneglist, ncp, nc_vnode);
576 * Invalidate portions of the namecache topology given a starting entry.
577 * The passed ncp is set to an unresolved state and:
579 * The passed ncp must be locked.
581 * CINV_DESTROY - Set a flag in the passed ncp entry indicating
582 * that the physical underlying nodes have been
583 * destroyed... as in deleted. For example, when
584 * a directory is removed. This will cause record
585 * lookups on the name to no longer be able to find
586 * the record and tells the resolver to return failure
587 * rather then trying to resolve through the parent.
589 * The topology itself, including ncp->nc_name,
592 * This only applies to the passed ncp, if CINV_CHILDREN
593 * is specified the children are not flagged.
595 * CINV_CHILDREN - Set all children (recursively) to an unresolved
598 * Note that this will also have the side effect of
599 * cleaning out any unreferenced nodes in the topology
600 * from the leaves up as the recursion backs out.
602 * Note that the topology for any referenced nodes remains intact.
604 * It is possible for cache_inval() to race a cache_resolve(), meaning that
605 * the namecache entry may not actually be invalidated on return if it was
606 * revalidated while recursing down into its children. This code guarentees
607 * that the node(s) will go through an invalidation cycle, but does not
608 * guarentee that they will remain in an invalidated state.
610 * Returns non-zero if a revalidation was detected during the invalidation
611 * recursion, zero otherwise. Note that since only the original ncp is
612 * locked the revalidation ultimately can only indicate that the original ncp
613 * *MIGHT* no have been reresolved.
616 cache_inval(struct namecache *ncp, int flags)
618 struct namecache *kid;
619 struct namecache *nextkid;
622 KKASSERT(ncp->nc_exlocks);
624 cache_setunresolved(ncp);
625 if (flags & CINV_DESTROY)
626 ncp->nc_flag |= NCF_DESTROYED;
628 if ((flags & CINV_CHILDREN) &&
629 (kid = TAILQ_FIRST(&ncp->nc_list)) != NULL
634 if ((nextkid = TAILQ_NEXT(kid, nc_entry)) != NULL)
636 if ((kid->nc_flag & NCF_UNRESOLVED) == 0 ||
637 TAILQ_FIRST(&kid->nc_list)
640 rcnt += cache_inval(kid, flags & ~CINV_DESTROY);
650 * Someone could have gotten in there while ncp was unlocked,
653 if ((ncp->nc_flag & NCF_UNRESOLVED) == 0)
659 * Invalidate a vnode's namecache associations. To avoid races against
660 * the resolver we do not invalidate a node which we previously invalidated
661 * but which was then re-resolved while we were in the invalidation loop.
663 * Returns non-zero if any namecache entries remain after the invalidation
666 * NOTE: unlike the namecache topology which guarentees that ncp's will not
667 * be ripped out of the topology while held, the vnode's v_namecache list
668 * has no such restriction. NCP's can be ripped out of the list at virtually
669 * any time if not locked, even if held.
672 cache_inval_vp(struct vnode *vp, int flags, int *retflags)
674 struct namecache *ncp;
675 struct namecache *next;
678 ncp = TAILQ_FIRST(&vp->v_namecache);
682 /* loop entered with ncp held */
683 if ((next = TAILQ_NEXT(ncp, nc_vnode)) != NULL)
686 if (ncp->nc_vp != vp) {
687 printf("Warning: cache_inval_vp: race-A detected on "
688 "%s\n", ncp->nc_name);
694 *retflags |= ncp->nc_flag & NCF_FSMID;
695 cache_inval(ncp, flags);
696 cache_put(ncp); /* also releases reference */
698 if (ncp && ncp->nc_vp != vp) {
699 printf("Warning: cache_inval_vp: race-B detected on "
700 "%s\n", ncp->nc_name);
705 return(TAILQ_FIRST(&vp->v_namecache) != NULL);
709 * The source ncp has been renamed to the target ncp. Both fncp and tncp
710 * must be locked. Both will be set to unresolved, any children of tncp
711 * will be disconnected (the prior contents of the target is assumed to be
712 * destroyed by the rename operation, e.g. renaming over an empty directory),
713 * and all children of fncp will be moved to tncp.
715 * XXX the disconnection could pose a problem, check code paths to make
716 * sure any code that blocks can handle the parent being changed out from
717 * under it. Maybe we should lock the children (watch out for deadlocks) ?
719 * After we return the caller has the option of calling cache_setvp() if
720 * the vnode of the new target ncp is known.
722 * Any process CD'd into any of the children will no longer be able to ".."
723 * back out. An rm -rf can cause this situation to occur.
726 cache_rename(struct namecache *fncp, struct namecache *tncp)
728 struct namecache *scan;
731 cache_setunresolved(fncp);
732 cache_setunresolved(tncp);
733 while (cache_inval(tncp, CINV_CHILDREN) != 0) {
734 if (didwarn++ % 10 == 0) {
735 printf("Warning: cache_rename: race during "
737 fncp->nc_name, tncp->nc_name);
739 tsleep(tncp, 0, "mvrace", hz / 10);
740 cache_setunresolved(tncp);
742 while ((scan = TAILQ_FIRST(&fncp->nc_list)) != NULL) {
744 cache_unlink_parent(scan);
745 cache_link_parent(scan, tncp);
746 if (scan->nc_flag & NCF_HASHED)
753 * vget the vnode associated with the namecache entry. Resolve the namecache
754 * entry if necessary and deal with namecache/vp races. The passed ncp must
755 * be referenced and may be locked. The ncp's ref/locking state is not
756 * effected by this call.
758 * lk_type may be LK_SHARED, LK_EXCLUSIVE. A ref'd, possibly locked
759 * (depending on the passed lk_type) will be returned in *vpp with an error
760 * of 0, or NULL will be returned in *vpp with a non-0 error code. The
761 * most typical error is ENOENT, meaning that the ncp represents a negative
762 * cache hit and there is no vnode to retrieve, but other errors can occur
765 * The main race we have to deal with are namecache zaps. The ncp itself
766 * will not disappear since it is referenced, and it turns out that the
767 * validity of the vp pointer can be checked simply by rechecking the
768 * contents of ncp->nc_vp.
771 cache_vget(struct namecache *ncp, struct ucred *cred,
772 int lk_type, struct vnode **vpp)
779 if (ncp->nc_flag & NCF_UNRESOLVED) {
781 error = cache_resolve(ncp, cred);
786 if (error == 0 && (vp = ncp->nc_vp) != NULL) {
787 error = vget(vp, lk_type, curthread);
789 if (vp != ncp->nc_vp) /* handle cache_zap race */
792 } else if (vp != ncp->nc_vp) { /* handle cache_zap race */
797 if (error == 0 && vp == NULL)
804 cache_vref(struct namecache *ncp, struct ucred *cred, struct vnode **vpp)
811 if (ncp->nc_flag & NCF_UNRESOLVED) {
813 error = cache_resolve(ncp, cred);
818 if (error == 0 && (vp = ncp->nc_vp) != NULL) {
820 if (vp != ncp->nc_vp) { /* handle cache_zap race */
825 if (error == 0 && vp == NULL)
832 * Recursively set the FSMID update flag for namecache nodes leading
833 * to root. This will cause the next getattr or reclaim to increment the
834 * fsmid and mark the inode for lazy updating.
836 * Stop recursing when we hit a node whos NCF_FSMID flag is already set.
837 * This makes FSMIDs work in an Einsteinian fashion - where the observation
838 * effects the result. In this case a program monitoring a higher level
839 * node will have detected some prior change and started its scan (clearing
840 * NCF_FSMID in higher level nodes), but since it has not yet observed the
841 * node where we find NCF_FSMID still set, we can safely make the related
842 * modification without interfering with the theorized program.
844 * This also means that FSMIDs cannot represent time-domain quantities
845 * in a hierarchical sense. But the main reason for doing it this way
846 * is to reduce the amount of recursion that occurs in the critical path
847 * when e.g. a program is writing to a file that sits deep in a directory
851 cache_update_fsmid(struct namecache *ncp)
854 struct namecache *scan;
856 if ((vp = ncp->nc_vp) != NULL) {
857 TAILQ_FOREACH(ncp, &vp->v_namecache, nc_vnode) {
858 for (scan = ncp; scan; scan = scan->nc_parent) {
859 if (scan->nc_flag & NCF_FSMID)
861 scan->nc_flag |= NCF_FSMID;
865 while (ncp && (ncp->nc_flag & NCF_FSMID) == 0) {
866 ncp->nc_flag |= NCF_FSMID;
867 ncp = ncp->nc_parent;
873 cache_update_fsmid_vp(struct vnode *vp)
875 struct namecache *ncp;
876 struct namecache *scan;
878 TAILQ_FOREACH(ncp, &vp->v_namecache, nc_vnode) {
879 for (scan = ncp; scan; scan = scan->nc_parent) {
880 if (scan->nc_flag & NCF_FSMID)
882 scan->nc_flag |= NCF_FSMID;
888 * If getattr is called on a vnode (e.g. a stat call), the filesystem
889 * may call this routine to determine if the namecache has the hierarchical
890 * change flag set, requiring the fsmid to be updated.
892 * Since 0 indicates no support, make sure the filesystem fsmid is at least
896 cache_check_fsmid_vp(struct vnode *vp, int64_t *fsmid)
898 struct namecache *ncp;
901 TAILQ_FOREACH(ncp, &vp->v_namecache, nc_vnode) {
902 if (ncp->nc_flag & NCF_FSMID) {
903 ncp->nc_flag &= ~NCF_FSMID;
915 * Convert a directory vnode to a namecache record without any other
916 * knowledge of the topology. This ONLY works with directory vnodes and
917 * is ONLY used by the NFS server. dvp must be refd but unlocked, and the
918 * returned ncp (if not NULL) will be held and unlocked.
920 * If 'makeit' is 0 and dvp has no existing namecache record, NULL is returned.
921 * If 'makeit' is 1 we attempt to track-down and create the namecache topology
922 * for dvp. This will fail only if the directory has been deleted out from
925 * Callers must always check for a NULL return no matter the value of 'makeit'.
928 static int cache_inefficient_scan(struct namecache *ncp, struct ucred *cred,
932 cache_fromdvp(struct vnode *dvp, struct ucred *cred, int makeit)
934 struct namecache *ncp;
939 * Temporary debugging code to force the directory scanning code
943 if (ncvp_debug >= 3 && makeit && TAILQ_FIRST(&dvp->v_namecache)) {
944 ncp = TAILQ_FIRST(&dvp->v_namecache);
945 printf("cache_fromdvp: forcing %s\n", ncp->nc_name);
950 * Loop until resolution, inside code will break out on error.
952 while ((ncp = TAILQ_FIRST(&dvp->v_namecache)) == NULL && makeit) {
955 * If dvp is the root of its filesystem it should already
956 * have a namecache pointer associated with it as a side
957 * effect of the mount, but it may have been disassociated.
959 if (dvp->v_flag & VROOT) {
960 ncp = cache_get(dvp->v_mount->mnt_ncp);
961 error = cache_resolve_mp(ncp);
964 printf("cache_fromdvp: resolve root of mount %p error %d",
965 dvp->v_mount, error);
974 printf(" succeeded\n");
979 * Get the parent directory and resolve its ncp.
981 error = vop_nlookupdotdot(*dvp->v_ops, dvp, &pvp, cred);
983 printf("lookupdotdot failed %d %p\n", error, pvp);
986 VOP_UNLOCK(pvp, 0, curthread);
989 * XXX this recursion could run the kernel out of stack,
990 * change to a less efficient algorithm if we get too deep
991 * (use 'makeit' for a depth counter?)
993 ncp = cache_fromdvp(pvp, cred, makeit);
999 * Do an inefficient scan of pvp (embodied by ncp) to look
1000 * for dvp. This will create a namecache record for dvp on
1001 * success. We loop up to recheck on success.
1003 * ncp and dvp are both held but not locked.
1005 error = cache_inefficient_scan(ncp, cred, dvp);
1008 printf("cache_fromdvp: scan %p (%s) failed on dvp=%p\n",
1009 pvp, ncp->nc_name, dvp);
1014 printf("cache_fromdvp: scan %p (%s) succeeded\n",
1024 * Do an inefficient scan of the directory represented by ncp looking for
1025 * the directory vnode dvp. ncp must be held but not locked on entry and
1026 * will be held on return. dvp must be refd but not locked on entry and
1027 * will remain refd on return.
1029 * Why do this at all? Well, due to its stateless nature the NFS server
1030 * converts file handles directly to vnodes without necessarily going through
1031 * the namecache ops that would otherwise create the namecache topology
1032 * leading to the vnode. We could either (1) Change the namecache algorithms
1033 * to allow disconnect namecache records that are re-merged opportunistically,
1034 * or (2) Make the NFS server backtrack and scan to recover a connected
1035 * namecache topology in order to then be able to issue new API lookups.
1037 * It turns out that (1) is a huge mess. It takes a nice clean set of
1038 * namecache algorithms and introduces a lot of complication in every subsystem
1039 * that calls into the namecache to deal with the re-merge case, especially
1040 * since we are using the namecache to placehold negative lookups and the
1041 * vnode might not be immediately assigned. (2) is certainly far less
1042 * efficient then (1), but since we are only talking about directories here
1043 * (which are likely to remain cached), the case does not actually run all
1044 * that often and has the supreme advantage of not polluting the namecache
1048 cache_inefficient_scan(struct namecache *ncp, struct ucred *cred,
1051 struct nlcomponent nlc;
1052 struct namecache *rncp;
1064 vat.va_blocksize = 0;
1065 if ((error = VOP_GETATTR(dvp, &vat, curthread)) != 0)
1067 if ((error = cache_vget(ncp, cred, LK_SHARED, &pvp)) != 0)
1070 printf("inefficient_scan: directory iosize %ld vattr fileid = %ld\n", vat.va_blocksize, (long)vat.va_fileid);
1071 if ((blksize = vat.va_blocksize) == 0)
1072 blksize = DEV_BSIZE;
1073 rbuf = malloc(blksize, M_TEMP, M_WAITOK);
1079 iov.iov_base = rbuf;
1080 iov.iov_len = blksize;
1083 uio.uio_resid = blksize;
1084 uio.uio_segflg = UIO_SYSSPACE;
1085 uio.uio_rw = UIO_READ;
1086 uio.uio_td = curthread;
1088 if (ncvp_debug >= 2)
1089 printf("cache_inefficient_scan: readdir @ %08x\n", (int)uio.uio_offset);
1090 error = VOP_READDIR(pvp, &uio, cred, &eofflag, NULL, NULL);
1092 den = (struct dirent *)rbuf;
1093 bytes = blksize - uio.uio_resid;
1096 if (ncvp_debug >= 2) {
1097 printf("cache_inefficient_scan: %*.*s\n",
1098 den->d_namlen, den->d_namlen,
1101 if (den->d_type != DT_WHT &&
1102 den->d_ino == vat.va_fileid) {
1104 printf("cache_inefficient_scan: "
1105 "MATCHED inode %ld path %s/%*.*s\n",
1106 vat.va_fileid, ncp->nc_name,
1107 den->d_namlen, den->d_namlen,
1110 nlc.nlc_nameptr = den->d_name;
1111 nlc.nlc_namelen = den->d_namlen;
1112 VOP_UNLOCK(pvp, 0, curthread);
1113 rncp = cache_nlookup(ncp, &nlc);
1114 KKASSERT(rncp != NULL);
1117 bytes -= _DIRENT_DIRSIZ(den);
1118 den = _DIRENT_NEXT(den);
1120 if (rncp == NULL && eofflag == 0 && uio.uio_resid != blksize)
1125 if (rncp->nc_flag & NCF_UNRESOLVED) {
1126 cache_setvp(rncp, dvp);
1127 if (ncvp_debug >= 2) {
1128 printf("cache_inefficient_scan: setvp %s/%s = %p\n",
1129 ncp->nc_name, rncp->nc_name, dvp);
1132 if (ncvp_debug >= 2) {
1133 printf("cache_inefficient_scan: setvp %s/%s already set %p/%p\n",
1134 ncp->nc_name, rncp->nc_name, dvp,
1138 if (rncp->nc_vp == NULL)
1139 error = rncp->nc_error;
1142 printf("cache_inefficient_scan: dvp %p NOT FOUND in %s\n",
1152 * Zap a namecache entry. The ncp is unconditionally set to an unresolved
1153 * state, which disassociates it from its vnode or ncneglist.
1155 * Then, if there are no additional references to the ncp and no children,
1156 * the ncp is removed from the topology and destroyed. This function will
1157 * also run through the nc_parent chain and destroy parent ncps if possible.
1158 * As a side benefit, it turns out the only conditions that allow running
1159 * up the chain are also the conditions to ensure no deadlock will occur.
1161 * References and/or children may exist if the ncp is in the middle of the
1162 * topology, preventing the ncp from being destroyed.
1164 * This function must be called with the ncp held and locked and will unlock
1165 * and drop it during zapping.
1168 cache_zap(struct namecache *ncp)
1170 struct namecache *par;
1173 * Disassociate the vnode or negative cache ref and set NCF_UNRESOLVED.
1175 cache_setunresolved(ncp);
1178 * Try to scrap the entry and possibly tail-recurse on its parent.
1179 * We only scrap unref'd (other then our ref) unresolved entries,
1180 * we do not scrap 'live' entries.
1182 while (ncp->nc_flag & NCF_UNRESOLVED) {
1184 * Someone other then us has a ref, stop.
1186 if (ncp->nc_refs > 1)
1190 * We have children, stop.
1192 if (!TAILQ_EMPTY(&ncp->nc_list))
1196 * Remove ncp from the topology: hash table and parent linkage.
1198 if (ncp->nc_flag & NCF_HASHED) {
1199 ncp->nc_flag &= ~NCF_HASHED;
1200 LIST_REMOVE(ncp, nc_hash);
1202 if ((par = ncp->nc_parent) != NULL) {
1203 par = cache_hold(par);
1204 TAILQ_REMOVE(&par->nc_list, ncp, nc_entry);
1205 ncp->nc_parent = NULL;
1206 if (par->nc_vp && TAILQ_EMPTY(&par->nc_list))
1211 * ncp should not have picked up any refs. Physically
1214 KKASSERT(ncp->nc_refs == 1);
1216 /* cache_unlock(ncp) not required */
1217 ncp->nc_refs = -1; /* safety */
1219 free(ncp->nc_name, M_VFSCACHE);
1220 free(ncp, M_VFSCACHE);
1223 * Loop on the parent (it may be NULL). Only bother looping
1224 * if the parent has a single ref (ours), which also means
1225 * we can lock it trivially.
1230 if (ncp->nc_refs != 1) {
1234 KKASSERT(par->nc_exlocks == 0);
1242 static enum { CHI_LOW, CHI_HIGH } cache_hysteresis_state = CHI_LOW;
1246 cache_hysteresis(void)
1249 * Don't cache too many negative hits. We use hysteresis to reduce
1250 * the impact on the critical path.
1252 switch(cache_hysteresis_state) {
1254 if (numneg > MINNEG && numneg * ncnegfactor > numcache) {
1256 cache_hysteresis_state = CHI_HIGH;
1260 if (numneg > MINNEG * 9 / 10 &&
1261 numneg * ncnegfactor * 9 / 10 > numcache
1265 cache_hysteresis_state = CHI_LOW;
1272 * NEW NAMECACHE LOOKUP API
1274 * Lookup an entry in the cache. A locked, referenced, non-NULL
1275 * entry is *always* returned, even if the supplied component is illegal.
1276 * The resulting namecache entry should be returned to the system with
1277 * cache_put() or cache_unlock() + cache_drop().
1279 * namecache locks are recursive but care must be taken to avoid lock order
1282 * Nobody else will be able to manipulate the associated namespace (e.g.
1283 * create, delete, rename, rename-target) until the caller unlocks the
1286 * The returned entry will be in one of three states: positive hit (non-null
1287 * vnode), negative hit (null vnode), or unresolved (NCF_UNRESOLVED is set).
1288 * Unresolved entries must be resolved through the filesystem to associate the
1289 * vnode and/or determine whether a positive or negative hit has occured.
1291 * It is not necessary to lock a directory in order to lock namespace under
1292 * that directory. In fact, it is explicitly not allowed to do that. A
1293 * directory is typically only locked when being created, renamed, or
1296 * The directory (par) may be unresolved, in which case any returned child
1297 * will likely also be marked unresolved. Likely but not guarenteed. Since
1298 * the filesystem lookup requires a resolved directory vnode the caller is
1299 * responsible for resolving the namecache chain top-down. This API
1300 * specifically allows whole chains to be created in an unresolved state.
1303 cache_nlookup(struct namecache *par, struct nlcomponent *nlc)
1305 struct namecache *ncp;
1306 struct namecache *new_ncp;
1307 struct nchashhead *nchpp;
1315 * Try to locate an existing entry
1317 hash = fnv_32_buf(nlc->nlc_nameptr, nlc->nlc_namelen, FNV1_32_INIT);
1318 hash = fnv_32_buf(&par, sizeof(par), hash);
1321 LIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
1325 * Zap entries that have timed out.
1327 if (ncp->nc_timeout &&
1328 (int)(ncp->nc_timeout - ticks) < 0 &&
1329 (ncp->nc_flag & NCF_UNRESOLVED) == 0 &&
1330 ncp->nc_exlocks == 0
1332 cache_zap(cache_get(ncp));
1337 * Break out if we find a matching entry. Note that
1338 * UNRESOLVED entries may match, but DESTROYED entries
1341 if (ncp->nc_parent == par &&
1342 ncp->nc_nlen == nlc->nlc_namelen &&
1343 bcmp(ncp->nc_name, nlc->nlc_nameptr, ncp->nc_nlen) == 0 &&
1344 (ncp->nc_flag & NCF_DESTROYED) == 0
1346 if (cache_get_nonblock(ncp) == 0) {
1348 cache_free(new_ncp);
1358 * We failed to locate an entry, create a new entry and add it to
1359 * the cache. We have to relookup after possibly blocking in
1362 if (new_ncp == NULL) {
1363 new_ncp = cache_alloc(nlc->nlc_namelen);
1370 * Initialize as a new UNRESOLVED entry, lock (non-blocking),
1371 * and link to the parent. The mount point is usually inherited
1372 * from the parent unless this is a special case such as a mount
1373 * point where nlc_namelen is 0. The caller is responsible for
1374 * setting nc_mount in that case. If nlc_namelen is 0 nc_name will
1377 if (nlc->nlc_namelen) {
1378 bcopy(nlc->nlc_nameptr, ncp->nc_name, nlc->nlc_namelen);
1379 ncp->nc_name[nlc->nlc_namelen] = 0;
1380 ncp->nc_mount = par->nc_mount;
1382 nchpp = NCHHASH(hash);
1383 LIST_INSERT_HEAD(nchpp, ncp, nc_hash);
1384 ncp->nc_flag |= NCF_HASHED;
1385 cache_link_parent(ncp, par);
1388 * stats and namecache size management
1390 if (ncp->nc_flag & NCF_UNRESOLVED)
1391 ++gd->gd_nchstats->ncs_miss;
1392 else if (ncp->nc_vp)
1393 ++gd->gd_nchstats->ncs_goodhits;
1395 ++gd->gd_nchstats->ncs_neghits;
1401 * Resolve an unresolved namecache entry, generally by looking it up.
1402 * The passed ncp must be locked and refd.
1404 * Theoretically since a vnode cannot be recycled while held, and since
1405 * the nc_parent chain holds its vnode as long as children exist, the
1406 * direct parent of the cache entry we are trying to resolve should
1407 * have a valid vnode. If not then generate an error that we can
1408 * determine is related to a resolver bug.
1410 * Note that successful resolution does not necessarily return an error
1411 * code of 0. If the ncp resolves to a negative cache hit then ENOENT
1415 cache_resolve(struct namecache *ncp, struct ucred *cred)
1417 struct namecache *par;
1422 * If the ncp is already resolved we have nothing to do.
1424 if ((ncp->nc_flag & NCF_UNRESOLVED) == 0)
1425 return (ncp->nc_error);
1428 * Mount points need special handling because the parent does not
1429 * belong to the same filesystem as the ncp.
1431 if (ncp->nc_flag & NCF_MOUNTPT)
1432 return (cache_resolve_mp(ncp));
1435 * We expect an unbroken chain of ncps to at least the mount point,
1436 * and even all the way to root (but this code doesn't have to go
1437 * past the mount point).
1439 if (ncp->nc_parent == NULL) {
1440 printf("EXDEV case 1 %p %*.*s\n", ncp,
1441 ncp->nc_nlen, ncp->nc_nlen, ncp->nc_name);
1442 ncp->nc_error = EXDEV;
1443 return(ncp->nc_error);
1447 * The vp's of the parent directories in the chain are held via vhold()
1448 * due to the existance of the child, and should not disappear.
1449 * However, there are cases where they can disappear:
1451 * - due to filesystem I/O errors.
1452 * - due to NFS being stupid about tracking the namespace and
1453 * destroys the namespace for entire directories quite often.
1454 * - due to forced unmounts.
1455 * - due to an rmdir (parent will be marked DESTROYED)
1457 * When this occurs we have to track the chain backwards and resolve
1458 * it, looping until the resolver catches up to the current node. We
1459 * could recurse here but we might run ourselves out of kernel stack
1460 * so we do it in a more painful manner. This situation really should
1461 * not occur all that often, or if it does not have to go back too
1462 * many nodes to resolve the ncp.
1464 while (ncp->nc_parent->nc_vp == NULL) {
1466 * This case can occur if a process is CD'd into a
1467 * directory which is then rmdir'd. If the parent is marked
1468 * destroyed there is no point trying to resolve it.
1470 if (ncp->nc_parent->nc_flag & NCF_DESTROYED)
1473 par = ncp->nc_parent;
1474 while (par->nc_parent && par->nc_parent->nc_vp == NULL)
1475 par = par->nc_parent;
1476 if (par->nc_parent == NULL) {
1477 printf("EXDEV case 2 %*.*s\n",
1478 par->nc_nlen, par->nc_nlen, par->nc_name);
1481 printf("[diagnostic] cache_resolve: had to recurse on %*.*s\n",
1482 par->nc_nlen, par->nc_nlen, par->nc_name);
1484 * The parent is not set in stone, ref and lock it to prevent
1485 * it from disappearing. Also note that due to renames it
1486 * is possible for our ncp to move and for par to no longer
1487 * be one of its parents. We resolve it anyway, the loop
1488 * will handle any moves.
1491 if (par->nc_flag & NCF_MOUNTPT) {
1492 cache_resolve_mp(par);
1493 } else if (par->nc_parent->nc_vp == NULL) {
1494 printf("[diagnostic] cache_resolve: raced on %*.*s\n", par->nc_nlen, par->nc_nlen, par->nc_name);
1497 } else if (par->nc_flag & NCF_UNRESOLVED) {
1498 par->nc_error = VOP_NRESOLVE(par, cred);
1500 if ((error = par->nc_error) != 0) {
1501 if (par->nc_error != EAGAIN) {
1502 printf("EXDEV case 3 %*.*s error %d\n",
1503 par->nc_nlen, par->nc_nlen, par->nc_name,
1508 printf("[diagnostic] cache_resolve: EAGAIN par %p %*.*s\n",
1509 par, par->nc_nlen, par->nc_nlen, par->nc_name);
1516 * Call VOP_NRESOLVE() to get the vp, then scan for any disconnected
1517 * ncp's and reattach them. If this occurs the original ncp is marked
1518 * EAGAIN to force a relookup.
1520 * NOTE: in order to call VOP_NRESOLVE(), the parent of the passed
1521 * ncp must already be resolved.
1523 KKASSERT((ncp->nc_flag & NCF_MOUNTPT) == 0);
1524 ncp->nc_error = VOP_NRESOLVE(ncp, cred);
1525 /*vop_nresolve(*ncp->nc_parent->nc_vp->v_ops, ncp, cred);*/
1526 if (ncp->nc_error == EAGAIN) {
1527 printf("[diagnostic] cache_resolve: EAGAIN ncp %p %*.*s\n",
1528 ncp, ncp->nc_nlen, ncp->nc_nlen, ncp->nc_name);
1531 return(ncp->nc_error);
1535 * Resolve the ncp associated with a mount point. Such ncp's almost always
1536 * remain resolved and this routine is rarely called. NFS MPs tends to force
1537 * re-resolution more often due to its mac-truck-smash-the-namecache
1538 * method of tracking namespace changes.
1540 * The semantics for this call is that the passed ncp must be locked on
1541 * entry and will be locked on return. However, if we actually have to
1542 * resolve the mount point we temporarily unlock the entry in order to
1543 * avoid race-to-root deadlocks due to e.g. dead NFS mounts. Because of
1544 * the unlock we have to recheck the flags after we relock.
1547 cache_resolve_mp(struct namecache *ncp)
1550 struct mount *mp = ncp->nc_mount;
1553 KKASSERT(mp != NULL);
1554 if (ncp->nc_flag & NCF_UNRESOLVED) {
1556 while (vfs_busy(mp, 0, curthread))
1558 error = VFS_ROOT(mp, &vp);
1562 * recheck the ncp state after relocking.
1564 if (ncp->nc_flag & NCF_UNRESOLVED) {
1565 ncp->nc_error = error;
1567 cache_setvp(ncp, vp);
1570 printf("[diagnostic] cache_resolve_mp: failed to resolve mount %p\n", mp);
1571 cache_setvp(ncp, NULL);
1573 } else if (error == 0) {
1576 vfs_unbusy(mp, curthread);
1578 return(ncp->nc_error);
1582 cache_cleanneg(int count)
1584 struct namecache *ncp;
1587 * Automode from the vnlru proc - clean out 10% of the negative cache
1591 count = numneg / 10 + 1;
1594 * Attempt to clean out the specified number of negative cache
1598 ncp = TAILQ_FIRST(&ncneglist);
1600 KKASSERT(numneg == 0);
1603 TAILQ_REMOVE(&ncneglist, ncp, nc_vnode);
1604 TAILQ_INSERT_TAIL(&ncneglist, ncp, nc_vnode);
1605 if (cache_get_nonblock(ncp) == 0)
1612 * Rehash a ncp. Rehashing is typically required if the name changes (should
1613 * not generally occur) or the parent link changes. This function will
1614 * unhash the ncp if the ncp is no longer hashable.
1617 cache_rehash(struct namecache *ncp)
1619 struct nchashhead *nchpp;
1622 if (ncp->nc_flag & NCF_HASHED) {
1623 ncp->nc_flag &= ~NCF_HASHED;
1624 LIST_REMOVE(ncp, nc_hash);
1626 if (ncp->nc_nlen && ncp->nc_parent) {
1627 hash = fnv_32_buf(ncp->nc_name, ncp->nc_nlen, FNV1_32_INIT);
1628 hash = fnv_32_buf(&ncp->nc_parent,
1629 sizeof(ncp->nc_parent), hash);
1630 nchpp = NCHHASH(hash);
1631 LIST_INSERT_HEAD(nchpp, ncp, nc_hash);
1632 ncp->nc_flag |= NCF_HASHED;
1637 * Name cache initialization, from vfsinit() when we are booting
1645 /* initialise per-cpu namecache effectiveness statistics. */
1646 for (i = 0; i < ncpus; ++i) {
1647 gd = globaldata_find(i);
1648 gd->gd_nchstats = &nchstats[i];
1650 TAILQ_INIT(&ncneglist);
1651 nchashtbl = hashinit(desiredvnodes*2, M_VFSCACHE, &nchash);
1652 nclockwarn = 1 * hz;
1656 * Called from start_init() to bootstrap the root filesystem. Returns
1657 * a referenced, unlocked namecache record.
1660 cache_allocroot(struct mount *mp, struct vnode *vp)
1662 struct namecache *ncp = cache_alloc(0);
1664 ncp->nc_flag |= NCF_MOUNTPT | NCF_ROOT;
1666 cache_setvp(ncp, vp);
1671 * vfs_cache_setroot()
1673 * Create an association between the root of our namecache and
1674 * the root vnode. This routine may be called several times during
1677 * If the caller intends to save the returned namecache pointer somewhere
1678 * it must cache_hold() it.
1681 vfs_cache_setroot(struct vnode *nvp, struct namecache *ncp)
1684 struct namecache *oncp;
1698 * XXX OLD API COMPAT FUNCTION. This really messes up the new namecache
1699 * topology and is being removed as quickly as possible. The new VOP_N*()
1700 * API calls are required to make specific adjustments using the supplied
1701 * ncp pointers rather then just bogusly purging random vnodes.
1703 * Invalidate all namecache entries to a particular vnode as well as
1704 * any direct children of that vnode in the namecache. This is a
1705 * 'catch all' purge used by filesystems that do not know any better.
1707 * A new vnode v_id is generated. Note that no vnode will ever have a
1710 * Note that the linkage between the vnode and its namecache entries will
1711 * be removed, but the namecache entries themselves might stay put due to
1712 * active references from elsewhere in the system or due to the existance of
1713 * the children. The namecache topology is left intact even if we do not
1714 * know what the vnode association is. Such entries will be marked
1717 * XXX: Only time and the size of v_id prevents this from failing:
1718 * XXX: In theory we should hunt down all (struct vnode*, v_id)
1719 * XXX: soft references and nuke them, at least on the global
1720 * XXX: v_id wraparound. The period of resistance can be extended
1721 * XXX: by incrementing each vnodes v_id individually instead of
1722 * XXX: using the global v_id.
1724 * Does not support NCP_FSMID accumulation on invalidation (retflags is
1728 cache_purge(struct vnode *vp)
1730 static u_long nextid;
1733 cache_inval_vp(vp, CINV_DESTROY | CINV_CHILDREN, &retflags);
1736 * Calculate a new unique id for ".." handling
1740 } while (nextid == vp->v_id || nextid == 0);
1745 * Flush all entries referencing a particular filesystem.
1747 * Since we need to check it anyway, we will flush all the invalid
1748 * entries at the same time.
1751 cache_purgevfs(struct mount *mp)
1753 struct nchashhead *nchpp;
1754 struct namecache *ncp, *nnp;
1757 * Scan hash tables for applicable entries.
1759 for (nchpp = &nchashtbl[nchash]; nchpp >= nchashtbl; nchpp--) {
1760 ncp = LIST_FIRST(nchpp);
1764 nnp = LIST_NEXT(ncp, nc_hash);
1767 if (ncp->nc_mount == mp) {
1778 static int disablecwd;
1779 SYSCTL_INT(_debug, OID_AUTO, disablecwd, CTLFLAG_RW, &disablecwd, 0, "");
1781 static u_long numcwdcalls; STATNODE(CTLFLAG_RD, numcwdcalls, &numcwdcalls);
1782 static u_long numcwdfail1; STATNODE(CTLFLAG_RD, numcwdfail1, &numcwdfail1);
1783 static u_long numcwdfail2; STATNODE(CTLFLAG_RD, numcwdfail2, &numcwdfail2);
1784 static u_long numcwdfail3; STATNODE(CTLFLAG_RD, numcwdfail3, &numcwdfail3);
1785 static u_long numcwdfail4; STATNODE(CTLFLAG_RD, numcwdfail4, &numcwdfail4);
1786 static u_long numcwdfound; STATNODE(CTLFLAG_RD, numcwdfound, &numcwdfound);
1789 __getcwd(struct __getcwd_args *uap)
1799 buflen = uap->buflen;
1802 if (buflen > MAXPATHLEN)
1803 buflen = MAXPATHLEN;
1805 buf = malloc(buflen, M_TEMP, M_WAITOK);
1806 bp = kern_getcwd(buf, buflen, &error);
1808 error = copyout(bp, uap->buf, strlen(bp) + 1);
1814 kern_getcwd(char *buf, size_t buflen, int *error)
1816 struct proc *p = curproc;
1818 int i, slash_prefixed;
1819 struct filedesc *fdp;
1820 struct namecache *ncp;
1829 ncp = fdp->fd_ncdir;
1830 while (ncp && ncp != fdp->fd_nrdir && (ncp->nc_flag & NCF_ROOT) == 0) {
1831 if (ncp->nc_flag & NCF_MOUNTPT) {
1832 if (ncp->nc_mount == NULL) {
1833 *error = EBADF; /* forced unmount? */
1836 ncp = ncp->nc_parent;
1839 for (i = ncp->nc_nlen - 1; i >= 0; i--) {
1845 *--bp = ncp->nc_name[i];
1854 ncp = ncp->nc_parent;
1861 if (!slash_prefixed) {
1875 * Thus begins the fullpath magic.
1879 #define STATNODE(name) \
1880 static u_int name; \
1881 SYSCTL_UINT(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, 0, "")
1883 static int disablefullpath;
1884 SYSCTL_INT(_debug, OID_AUTO, disablefullpath, CTLFLAG_RW,
1885 &disablefullpath, 0, "");
1887 STATNODE(numfullpathcalls);
1888 STATNODE(numfullpathfail1);
1889 STATNODE(numfullpathfail2);
1890 STATNODE(numfullpathfail3);
1891 STATNODE(numfullpathfail4);
1892 STATNODE(numfullpathfound);
1895 cache_fullpath(struct proc *p, struct namecache *ncp, char **retbuf, char **freebuf)
1898 int i, slash_prefixed;
1899 struct namecache *fd_nrdir;
1903 buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1904 bp = buf + MAXPATHLEN - 1;
1907 fd_nrdir = p->p_fd->fd_nrdir;
1911 while (ncp && ncp != fd_nrdir && (ncp->nc_flag & NCF_ROOT) == 0) {
1912 if (ncp->nc_flag & NCF_MOUNTPT) {
1913 if (ncp->nc_mount == NULL) {
1917 ncp = ncp->nc_parent;
1920 for (i = ncp->nc_nlen - 1; i >= 0; i--) {
1926 *--bp = ncp->nc_name[i];
1935 ncp = ncp->nc_parent;
1942 if (p != NULL && (ncp->nc_flag & NCF_ROOT) && ncp != fd_nrdir) {
1943 bp = buf + MAXPATHLEN - 1;
1947 if (!slash_prefixed) {
1963 vn_fullpath(struct proc *p, struct vnode *vn, char **retbuf, char **freebuf)
1965 struct namecache *ncp;
1968 if (disablefullpath)
1974 /* vn is NULL, client wants us to use p->p_textvp */
1976 if ((vn = p->p_textvp) == NULL)
1979 TAILQ_FOREACH(ncp, &vn->v_namecache, nc_vnode) {
1987 return(cache_fullpath(p, ncp, retbuf, freebuf));