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.51 2005/02/12 18:56:46 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 */
297 TAILQ_INIT(&ncp->nc_list);
303 cache_free(struct namecache *ncp)
305 KKASSERT(ncp->nc_refs == 1 && ncp->nc_exlocks == 1);
307 free(ncp->nc_name, M_VFSCACHE);
308 free(ncp, M_VFSCACHE);
312 * Ref and deref a namecache structure.
315 cache_hold(struct namecache *ncp)
317 return(_cache_hold(ncp));
321 cache_drop(struct namecache *ncp)
327 * Namespace locking. The caller must already hold a reference to the
328 * namecache structure in order to lock/unlock it. This function prevents
329 * the namespace from being created or destroyed by accessors other then
332 * Note that holding a locked namecache structure prevents other threads
333 * from making namespace changes (e.g. deleting or creating), prevents
334 * vnode association state changes by other threads, and prevents the
335 * namecache entry from being resolved or unresolved by other threads.
337 * The lock owner has full authority to associate/disassociate vnodes
338 * and resolve/unresolve the locked ncp.
340 * In particular, if a vnode is associated with a locked cache entry
341 * that vnode will *NOT* be recycled. We accomplish this by vhold()ing the
342 * vnode. XXX we should find a more efficient way to prevent the vnode
343 * from being recycled, but remember that any given vnode may have multiple
344 * namecache associations (think hardlinks).
347 cache_lock(struct namecache *ncp)
352 KKASSERT(ncp->nc_refs != 0);
357 if (ncp->nc_exlocks == 0) {
361 * The vp associated with a locked ncp must be held
362 * to prevent it from being recycled (which would
363 * cause the ncp to become unresolved).
365 * XXX loop on race for later MPSAFE work.
371 if (ncp->nc_locktd == td) {
375 ncp->nc_flag |= NCF_LOCKREQ;
376 if (tsleep(ncp, 0, "clock", nclockwarn) == EWOULDBLOCK) {
380 printf("[diagnostic] cache_lock: blocked on %p", ncp);
381 if ((ncp->nc_flag & NCF_MOUNTPT) && ncp->nc_mount)
382 printf(" [MOUNTFROM %s]\n", ncp->nc_mount->mnt_stat.f_mntfromname);
384 printf(" \"%*.*s\"\n",
385 ncp->nc_nlen, ncp->nc_nlen,
391 printf("[diagnostic] cache_lock: unblocked %*.*s\n",
392 ncp->nc_nlen, ncp->nc_nlen, ncp->nc_name);
397 cache_lock_nonblock(struct namecache *ncp)
401 KKASSERT(ncp->nc_refs != 0);
403 if (ncp->nc_exlocks == 0) {
407 * The vp associated with a locked ncp must be held
408 * to prevent it from being recycled (which would
409 * cause the ncp to become unresolved).
411 * XXX loop on race for later MPSAFE work.
422 cache_unlock(struct namecache *ncp)
424 thread_t td = curthread;
426 KKASSERT(ncp->nc_refs > 0);
427 KKASSERT(ncp->nc_exlocks > 0);
428 KKASSERT(ncp->nc_locktd == td);
429 if (--ncp->nc_exlocks == 0) {
432 ncp->nc_locktd = NULL;
433 if (ncp->nc_flag & NCF_LOCKREQ) {
434 ncp->nc_flag &= ~NCF_LOCKREQ;
441 * ref-and-lock, unlock-and-deref functions.
444 cache_get(struct namecache *ncp)
452 cache_get_nonblock(struct namecache *ncp)
455 if (ncp->nc_exlocks == 0 || ncp->nc_locktd == curthread) {
464 cache_put(struct namecache *ncp)
471 * Resolve an unresolved ncp by associating a vnode with it. If the
472 * vnode is NULL, a negative cache entry is created.
474 * The ncp should be locked on entry and will remain locked on return.
477 cache_setvp(struct namecache *ncp, struct vnode *vp)
479 KKASSERT(ncp->nc_flag & NCF_UNRESOLVED);
483 * Any vp associated with an ncp which has children must
484 * be held. Any vp associated with a locked ncp must be held.
486 if (!TAILQ_EMPTY(&ncp->nc_list))
488 TAILQ_INSERT_HEAD(&vp->v_namecache, ncp, nc_vnode);
493 * Set auxillary flags
497 ncp->nc_flag |= NCF_ISDIR;
500 ncp->nc_flag |= NCF_ISSYMLINK;
501 /* XXX cache the contents of the symlink */
509 TAILQ_INSERT_TAIL(&ncneglist, ncp, nc_vnode);
511 ncp->nc_error = ENOENT;
513 ncp->nc_flag &= ~NCF_UNRESOLVED;
517 cache_settimeout(struct namecache *ncp, int nticks)
519 if ((ncp->nc_timeout = ticks + nticks) == 0)
524 * Disassociate the vnode or negative-cache association and mark a
525 * namecache entry as unresolved again. Note that the ncp is still
526 * left in the hash table and still linked to its parent.
528 * The ncp should be locked and refd on entry and will remain locked and refd
531 * This routine is normally never called on a directory containing children.
532 * However, NFS often does just that in its rename() code as a cop-out to
533 * avoid complex namespace operations. This disconnects a directory vnode
534 * from its namecache and can cause the OLDAPI and NEWAPI to get out of
538 cache_setunresolved(struct namecache *ncp)
542 if ((ncp->nc_flag & NCF_UNRESOLVED) == 0) {
543 ncp->nc_flag |= NCF_UNRESOLVED;
544 ncp->nc_flag &= ~(NCF_WHITEOUT|NCF_ISDIR|NCF_ISSYMLINK);
546 ncp->nc_error = ENOTCONN;
548 if ((vp = ncp->nc_vp) != NULL) {
551 TAILQ_REMOVE(&vp->v_namecache, ncp, nc_vnode);
554 * Any vp associated with an ncp with children is
555 * held by that ncp. Any vp associated with a locked
556 * ncp is held by that ncp. These conditions must be
557 * undone when the vp is cleared out from the ncp.
559 if (!TAILQ_EMPTY(&ncp->nc_list))
564 TAILQ_REMOVE(&ncneglist, ncp, nc_vnode);
571 * Invalidate portions of the namecache topology given a starting entry.
572 * The passed ncp is set to an unresolved state and:
574 * The passed ncp must be locked.
576 * CINV_DESTROY - Set a flag in the passed ncp entry indicating
577 * that the physical underlying nodes have been
578 * destroyed... as in deleted. For example, when
579 * a directory is removed. This will cause record
580 * lookups on the name to no longer be able to find
581 * the record and tells the resolver to return failure
582 * rather then trying to resolve through the parent.
584 * The topology itself, including ncp->nc_name,
587 * This only applies to the passed ncp, if CINV_CHILDREN
588 * is specified the children are not flagged.
590 * CINV_CHILDREN - Set all children (recursively) to an unresolved
593 * Note that this will also have the side effect of
594 * cleaning out any unreferenced nodes in the topology
595 * from the leaves up as the recursion backs out.
597 * Note that the topology for any referenced nodes remains intact.
599 * It is possible for cache_inval() to race a cache_resolve(), meaning that
600 * the namecache entry may not actually be invalidated on return if it was
601 * revalidated while recursing down into its children. This code guarentees
602 * that the node(s) will go through an invalidation cycle, but does not
603 * guarentee that they will remain in an invalidated state.
605 * Returns non-zero if a revalidation was detected during the invalidation
606 * recursion, zero otherwise. Note that since only the original ncp is
607 * locked the revalidation ultimately can only indicate that the original ncp
608 * *MIGHT* no have been reresolved.
611 cache_inval(struct namecache *ncp, int flags)
613 struct namecache *kid;
614 struct namecache *nextkid;
617 KKASSERT(ncp->nc_exlocks);
619 cache_setunresolved(ncp);
620 if (flags & CINV_DESTROY)
621 ncp->nc_flag |= NCF_DESTROYED;
623 if ((flags & CINV_CHILDREN) &&
624 (kid = TAILQ_FIRST(&ncp->nc_list)) != NULL
629 if ((nextkid = TAILQ_NEXT(kid, nc_entry)) != NULL)
631 if ((kid->nc_flag & NCF_UNRESOLVED) == 0 ||
632 TAILQ_FIRST(&kid->nc_list)
635 rcnt += cache_inval(kid, flags & ~CINV_DESTROY);
645 * Someone could have gotten in there while ncp was unlocked,
648 if ((ncp->nc_flag & NCF_UNRESOLVED) == 0)
654 * Invalidate a vnode's namecache associations. To avoid races against
655 * the resolver we do not invalidate a node which we previously invalidated
656 * but which was then re-resolved while we were in the invalidation loop.
658 * Returns non-zero if any namecache entries remain after the invalidation
662 cache_inval_vp(struct vnode *vp, int flags)
664 struct namecache *ncp;
665 struct namecache *next;
667 ncp = TAILQ_FIRST(&vp->v_namecache);
671 /* loop entered with ncp held */
672 if ((next = TAILQ_NEXT(ncp, nc_entry)) != NULL)
675 cache_inval(ncp, flags);
676 cache_put(ncp); /* also releases reference */
679 return(TAILQ_FIRST(&vp->v_namecache) != NULL);
683 * The source ncp has been renamed to the target ncp. Both fncp and tncp
684 * must be locked. Both will be set to unresolved, any children of tncp
685 * will be disconnected (the prior contents of the target is assumed to be
686 * destroyed by the rename operation, e.g. renaming over an empty directory),
687 * and all children of fncp will be moved to tncp.
689 * XXX the disconnection could pose a problem, check code paths to make
690 * sure any code that blocks can handle the parent being changed out from
691 * under it. Maybe we should lock the children (watch out for deadlocks) ?
693 * After we return the caller has the option of calling cache_setvp() if
694 * the vnode of the new target ncp is known.
696 * Any process CD'd into any of the children will no longer be able to ".."
697 * back out. An rm -rf can cause this situation to occur.
700 cache_rename(struct namecache *fncp, struct namecache *tncp)
702 struct namecache *scan;
705 cache_setunresolved(fncp);
706 cache_setunresolved(tncp);
707 while (cache_inval(tncp, CINV_CHILDREN) != 0) {
708 if (didwarn++ % 10 == 0) {
709 printf("Warning: cache_rename: race during "
711 fncp->nc_name, tncp->nc_name);
713 tsleep(tncp, 0, "mvrace", hz / 10);
714 cache_setunresolved(tncp);
716 while ((scan = TAILQ_FIRST(&fncp->nc_list)) != NULL) {
718 cache_unlink_parent(scan);
719 cache_link_parent(scan, tncp);
720 if (scan->nc_flag & NCF_HASHED)
727 * vget the vnode associated with the namecache entry. Resolve the namecache
728 * entry if necessary and deal with namecache/vp races. The passed ncp must
729 * be referenced and may be locked. The ncp's ref/locking state is not
730 * effected by this call.
732 * lk_type may be LK_SHARED, LK_EXCLUSIVE. A ref'd, possibly locked
733 * (depending on the passed lk_type) will be returned in *vpp with an error
734 * of 0, or NULL will be returned in *vpp with a non-0 error code. The
735 * most typical error is ENOENT, meaning that the ncp represents a negative
736 * cache hit and there is no vnode to retrieve, but other errors can occur
739 * The main race we have to deal with are namecache zaps. The ncp itself
740 * will not disappear since it is referenced, and it turns out that the
741 * validity of the vp pointer can be checked simply by rechecking the
742 * contents of ncp->nc_vp.
745 cache_vget(struct namecache *ncp, struct ucred *cred,
746 int lk_type, struct vnode **vpp)
753 if (ncp->nc_flag & NCF_UNRESOLVED) {
755 error = cache_resolve(ncp, cred);
760 if (error == 0 && (vp = ncp->nc_vp) != NULL) {
761 error = vget(vp, lk_type, curthread);
763 if (vp != ncp->nc_vp) /* handle cache_zap race */
766 } else if (vp != ncp->nc_vp) { /* handle cache_zap race */
771 if (error == 0 && vp == NULL)
778 cache_vref(struct namecache *ncp, struct ucred *cred, struct vnode **vpp)
785 if (ncp->nc_flag & NCF_UNRESOLVED) {
787 error = cache_resolve(ncp, cred);
792 if (error == 0 && (vp = ncp->nc_vp) != NULL) {
794 if (vp != ncp->nc_vp) { /* handle cache_zap race */
799 if (error == 0 && vp == NULL)
806 * Convert a directory vnode to a namecache record without any other
807 * knowledge of the topology. This ONLY works with directory vnodes and
808 * is ONLY used by the NFS server. dvp must be refd but unlocked, and the
809 * returned ncp (if not NULL) will be held and unlocked.
811 * If 'makeit' is 0 and dvp has no existing namecache record, NULL is returned.
812 * If 'makeit' is 1 we attempt to track-down and create the namecache topology
813 * for dvp. This will fail only if the directory has been deleted out from
816 * Callers must always check for a NULL return no matter the value of 'makeit'.
819 static int cache_inefficient_scan(struct namecache *ncp, struct ucred *cred,
823 cache_fromdvp(struct vnode *dvp, struct ucred *cred, int makeit)
825 struct namecache *ncp;
830 * Temporary debugging code to force the directory scanning code
834 if (ncvp_debug >= 3 && makeit && TAILQ_FIRST(&dvp->v_namecache)) {
835 ncp = TAILQ_FIRST(&dvp->v_namecache);
836 printf("cache_fromdvp: forcing %s\n", ncp->nc_name);
841 * Loop until resolution, inside code will break out on error.
843 while ((ncp = TAILQ_FIRST(&dvp->v_namecache)) == NULL && makeit) {
846 * If dvp is the root of its filesystem it should already
847 * have a namecache pointer associated with it as a side
848 * effect of the mount, but it may have been disassociated.
850 if (dvp->v_flag & VROOT) {
851 ncp = cache_get(dvp->v_mount->mnt_ncp);
852 error = cache_resolve_mp(ncp);
855 printf("cache_fromdvp: resolve root of mount %p error %d",
856 dvp->v_mount, error);
865 printf(" succeeded\n");
870 * Get the parent directory and resolve its ncp.
872 error = vop_nlookupdotdot(*dvp->v_ops, dvp, &pvp, cred);
874 printf("lookupdotdot failed %d %p\n", error, pvp);
877 VOP_UNLOCK(pvp, 0, curthread);
880 * XXX this recursion could run the kernel out of stack,
881 * change to a less efficient algorithm if we get too deep
882 * (use 'makeit' for a depth counter?)
884 ncp = cache_fromdvp(pvp, cred, makeit);
890 * Do an inefficient scan of pvp (embodied by ncp) to look
891 * for dvp. This will create a namecache record for dvp on
892 * success. We loop up to recheck on success.
894 * ncp and dvp are both held but not locked.
896 error = cache_inefficient_scan(ncp, cred, dvp);
899 printf("cache_fromdvp: scan %p (%s) failed on dvp=%p\n",
900 pvp, ncp->nc_name, dvp);
905 printf("cache_fromdvp: scan %p (%s) succeeded\n",
915 * Do an inefficient scan of the directory represented by ncp looking for
916 * the directory vnode dvp. ncp must be held but not locked on entry and
917 * will be held on return. dvp must be refd but not locked on entry and
918 * will remain refd on return.
920 * Why do this at all? Well, due to its stateless nature the NFS server
921 * converts file handles directly to vnodes without necessarily going through
922 * the namecache ops that would otherwise create the namecache topology
923 * leading to the vnode. We could either (1) Change the namecache algorithms
924 * to allow disconnect namecache records that are re-merged opportunistically,
925 * or (2) Make the NFS server backtrack and scan to recover a connected
926 * namecache topology in order to then be able to issue new API lookups.
928 * It turns out that (1) is a huge mess. It takes a nice clean set of
929 * namecache algorithms and introduces a lot of complication in every subsystem
930 * that calls into the namecache to deal with the re-merge case, especially
931 * since we are using the namecache to placehold negative lookups and the
932 * vnode might not be immediately assigned. (2) is certainly far less
933 * efficient then (1), but since we are only talking about directories here
934 * (which are likely to remain cached), the case does not actually run all
935 * that often and has the supreme advantage of not polluting the namecache
939 cache_inefficient_scan(struct namecache *ncp, struct ucred *cred,
942 struct nlcomponent nlc;
943 struct namecache *rncp;
959 vat.va_blocksize = 0;
960 if ((error = VOP_GETATTR(dvp, &vat, curthread)) != 0)
962 if ((error = cache_vget(ncp, cred, LK_SHARED, &pvp)) != 0)
965 printf("inefficient_scan: directory iosize %ld vattr fileid = %ld\n", vat.va_blocksize, (long)vat.va_fileid);
966 if ((blksize = vat.va_blocksize) == 0)
968 rbuf = malloc(blksize, M_TEMP, M_WAITOK);
975 baseoff = uio.uio_offset;
977 iov.iov_len = blksize;
980 uio.uio_resid = blksize;
981 uio.uio_segflg = UIO_SYSSPACE;
982 uio.uio_rw = UIO_READ;
983 uio.uio_td = curthread;
986 free(cookies, M_TEMP);
990 printf("cache_inefficient_scan: readdir @ %08x\n", (int)baseoff);
991 error = VOP_READDIR(pvp, &uio, cred, &eofflag, &ncookies, &cookies);
992 if (error == 0 && cookies == NULL)
995 for (i = 0; i < ncookies; ++i) {
996 xoff = (int)(cookies[i] - (u_long)baseoff);
998 * UFS plays a little trick to skip the first entry
999 * in a directory ("."), by assigning the cookie to
1000 * dpoff + dp->d_reclen in the loop. This causes
1001 * the last cookie to be assigned to the data-end of
1002 * the directory. XXX
1004 if (xoff == blksize)
1006 KKASSERT(xoff >= 0 && xoff <= blksize);
1007 den = (struct dirent *)(rbuf + xoff);
1008 if (ncvp_debug >= 2)
1009 printf("cache_inefficient_scan: %*.*s\n",
1010 den->d_namlen, den->d_namlen, den->d_name);
1011 if (den->d_type != DT_WHT &&
1012 den->d_fileno == vat.va_fileid) {
1014 printf("cache_inefficient_scan: MATCHED inode %ld path %s/%*.*s\n", vat.va_fileid, ncp->nc_name, den->d_namlen, den->d_namlen, den->d_name);
1015 nlc.nlc_nameptr = den->d_name;
1016 nlc.nlc_namelen = den->d_namlen;
1017 VOP_UNLOCK(pvp, 0, curthread);
1018 rncp = cache_nlookup(ncp, &nlc);
1019 KKASSERT(rncp != NULL);
1023 if (rncp == NULL && eofflag == 0 && uio.uio_resid != blksize)
1027 free(cookies, M_TEMP);
1032 if (rncp->nc_flag & NCF_UNRESOLVED) {
1033 cache_setvp(rncp, dvp);
1034 if (ncvp_debug >= 2) {
1035 printf("cache_inefficient_scan: setvp %s/%s = %p\n",
1036 ncp->nc_name, rncp->nc_name, dvp);
1039 if (ncvp_debug >= 2) {
1040 printf("cache_inefficient_scan: setvp %s/%s already set %p/%p\n",
1041 ncp->nc_name, rncp->nc_name, dvp,
1045 if (rncp->nc_vp == NULL)
1046 error = rncp->nc_error;
1049 printf("cache_inefficient_scan: dvp %p NOT FOUND in %s\n",
1059 * Zap a namecache entry. The ncp is unconditionally set to an unresolved
1060 * state, which disassociates it from its vnode or ncneglist.
1062 * Then, if there are no additional references to the ncp and no children,
1063 * the ncp is removed from the topology and destroyed. This function will
1064 * also run through the nc_parent chain and destroy parent ncps if possible.
1065 * As a side benefit, it turns out the only conditions that allow running
1066 * up the chain are also the conditions to ensure no deadlock will occur.
1068 * References and/or children may exist if the ncp is in the middle of the
1069 * topology, preventing the ncp from being destroyed.
1071 * This function must be called with the ncp held and locked and will unlock
1072 * and drop it during zapping.
1075 cache_zap(struct namecache *ncp)
1077 struct namecache *par;
1080 * Disassociate the vnode or negative cache ref and set NCF_UNRESOLVED.
1082 cache_setunresolved(ncp);
1085 * Try to scrap the entry and possibly tail-recurse on its parent.
1086 * We only scrap unref'd (other then our ref) unresolved entries,
1087 * we do not scrap 'live' entries.
1089 while (ncp->nc_flag & NCF_UNRESOLVED) {
1091 * Someone other then us has a ref, stop.
1093 if (ncp->nc_refs > 1)
1097 * We have children, stop.
1099 if (!TAILQ_EMPTY(&ncp->nc_list))
1103 * Remove ncp from the topology: hash table and parent linkage.
1105 if (ncp->nc_flag & NCF_HASHED) {
1106 ncp->nc_flag &= ~NCF_HASHED;
1107 LIST_REMOVE(ncp, nc_hash);
1109 if ((par = ncp->nc_parent) != NULL) {
1110 par = cache_hold(par);
1111 TAILQ_REMOVE(&par->nc_list, ncp, nc_entry);
1112 ncp->nc_parent = NULL;
1113 if (par->nc_vp && TAILQ_EMPTY(&par->nc_list))
1118 * ncp should not have picked up any refs. Physically
1121 KKASSERT(ncp->nc_refs == 1);
1123 /* cache_unlock(ncp) not required */
1124 ncp->nc_refs = -1; /* safety */
1126 free(ncp->nc_name, M_VFSCACHE);
1127 free(ncp, M_VFSCACHE);
1130 * Loop on the parent (it may be NULL). Only bother looping
1131 * if the parent has a single ref (ours), which also means
1132 * we can lock it trivially.
1137 if (ncp->nc_refs != 1) {
1141 KKASSERT(par->nc_exlocks == 0);
1149 static enum { CHI_LOW, CHI_HIGH } cache_hysteresis_state = CHI_LOW;
1153 cache_hysteresis(void)
1156 * Don't cache too many negative hits. We use hysteresis to reduce
1157 * the impact on the critical path.
1159 switch(cache_hysteresis_state) {
1161 if (numneg > MINNEG && numneg * ncnegfactor > numcache) {
1163 cache_hysteresis_state = CHI_HIGH;
1167 if (numneg > MINNEG * 9 / 10 &&
1168 numneg * ncnegfactor * 9 / 10 > numcache
1172 cache_hysteresis_state = CHI_LOW;
1179 * NEW NAMECACHE LOOKUP API
1181 * Lookup an entry in the cache. A locked, referenced, non-NULL
1182 * entry is *always* returned, even if the supplied component is illegal.
1183 * The resulting namecache entry should be returned to the system with
1184 * cache_put() or cache_unlock() + cache_drop().
1186 * namecache locks are recursive but care must be taken to avoid lock order
1189 * Nobody else will be able to manipulate the associated namespace (e.g.
1190 * create, delete, rename, rename-target) until the caller unlocks the
1193 * The returned entry will be in one of three states: positive hit (non-null
1194 * vnode), negative hit (null vnode), or unresolved (NCF_UNRESOLVED is set).
1195 * Unresolved entries must be resolved through the filesystem to associate the
1196 * vnode and/or determine whether a positive or negative hit has occured.
1198 * It is not necessary to lock a directory in order to lock namespace under
1199 * that directory. In fact, it is explicitly not allowed to do that. A
1200 * directory is typically only locked when being created, renamed, or
1203 * The directory (par) may be unresolved, in which case any returned child
1204 * will likely also be marked unresolved. Likely but not guarenteed. Since
1205 * the filesystem lookup requires a resolved directory vnode the caller is
1206 * responsible for resolving the namecache chain top-down. This API
1207 * specifically allows whole chains to be created in an unresolved state.
1210 cache_nlookup(struct namecache *par, struct nlcomponent *nlc)
1212 struct namecache *ncp;
1213 struct namecache *new_ncp;
1214 struct nchashhead *nchpp;
1222 * Try to locate an existing entry
1224 hash = fnv_32_buf(nlc->nlc_nameptr, nlc->nlc_namelen, FNV1_32_INIT);
1225 hash = fnv_32_buf(&par, sizeof(par), hash);
1228 LIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
1232 * Zap entries that have timed out.
1234 if (ncp->nc_timeout &&
1235 (int)(ncp->nc_timeout - ticks) < 0 &&
1236 (ncp->nc_flag & NCF_UNRESOLVED) == 0 &&
1237 ncp->nc_exlocks == 0
1239 cache_zap(cache_get(ncp));
1244 * Break out if we find a matching entry. Note that
1245 * UNRESOLVED entries may match, but DESTROYED entries
1248 if (ncp->nc_parent == par &&
1249 ncp->nc_nlen == nlc->nlc_namelen &&
1250 bcmp(ncp->nc_name, nlc->nlc_nameptr, ncp->nc_nlen) == 0 &&
1251 (ncp->nc_flag & NCF_DESTROYED) == 0
1253 if (cache_get_nonblock(ncp) == 0) {
1255 cache_free(new_ncp);
1265 * We failed to locate an entry, create a new entry and add it to
1266 * the cache. We have to relookup after possibly blocking in
1269 if (new_ncp == NULL) {
1270 new_ncp = cache_alloc(nlc->nlc_namelen);
1277 * Initialize as a new UNRESOLVED entry, lock (non-blocking),
1278 * and link to the parent. The mount point is usually inherited
1279 * from the parent unless this is a special case such as a mount
1280 * point where nlc_namelen is 0. The caller is responsible for
1281 * setting nc_mount in that case. If nlc_namelen is 0 nc_name will
1284 if (nlc->nlc_namelen) {
1285 bcopy(nlc->nlc_nameptr, ncp->nc_name, nlc->nlc_namelen);
1286 ncp->nc_name[nlc->nlc_namelen] = 0;
1287 ncp->nc_mount = par->nc_mount;
1289 nchpp = NCHHASH(hash);
1290 LIST_INSERT_HEAD(nchpp, ncp, nc_hash);
1291 ncp->nc_flag |= NCF_HASHED;
1292 cache_link_parent(ncp, par);
1295 * stats and namecache size management
1297 if (ncp->nc_flag & NCF_UNRESOLVED)
1298 ++gd->gd_nchstats->ncs_miss;
1299 else if (ncp->nc_vp)
1300 ++gd->gd_nchstats->ncs_goodhits;
1302 ++gd->gd_nchstats->ncs_neghits;
1308 * Resolve an unresolved namecache entry, generally by looking it up.
1309 * The passed ncp must be locked and refd.
1311 * Theoretically since a vnode cannot be recycled while held, and since
1312 * the nc_parent chain holds its vnode as long as children exist, the
1313 * direct parent of the cache entry we are trying to resolve should
1314 * have a valid vnode. If not then generate an error that we can
1315 * determine is related to a resolver bug.
1317 * Note that successful resolution does not necessarily return an error
1318 * code of 0. If the ncp resolves to a negative cache hit then ENOENT
1322 cache_resolve(struct namecache *ncp, struct ucred *cred)
1324 struct namecache *par;
1329 * If the ncp is already resolved we have nothing to do.
1331 if ((ncp->nc_flag & NCF_UNRESOLVED) == 0)
1332 return (ncp->nc_error);
1335 * Mount points need special handling because the parent does not
1336 * belong to the same filesystem as the ncp.
1338 if (ncp->nc_flag & NCF_MOUNTPT)
1339 return (cache_resolve_mp(ncp));
1342 * We expect an unbroken chain of ncps to at least the mount point,
1343 * and even all the way to root (but this code doesn't have to go
1344 * past the mount point).
1346 if (ncp->nc_parent == NULL) {
1347 printf("EXDEV case 1 %p %*.*s\n", ncp,
1348 ncp->nc_nlen, ncp->nc_nlen, ncp->nc_name);
1349 ncp->nc_error = EXDEV;
1350 return(ncp->nc_error);
1354 * The vp's of the parent directories in the chain are held via vhold()
1355 * due to the existance of the child, and should not disappear.
1356 * However, there are cases where they can disappear:
1358 * - due to filesystem I/O errors.
1359 * - due to NFS being stupid about tracking the namespace and
1360 * destroys the namespace for entire directories quite often.
1361 * - due to forced unmounts.
1362 * - due to an rmdir (parent will be marked DESTROYED)
1364 * When this occurs we have to track the chain backwards and resolve
1365 * it, looping until the resolver catches up to the current node. We
1366 * could recurse here but we might run ourselves out of kernel stack
1367 * so we do it in a more painful manner. This situation really should
1368 * not occur all that often, or if it does not have to go back too
1369 * many nodes to resolve the ncp.
1371 while (ncp->nc_parent->nc_vp == NULL) {
1373 * This case can occur if a process is CD'd into a
1374 * directory which is then rmdir'd. If the parent is marked
1375 * destroyed there is no point trying to resolve it.
1377 if (ncp->nc_parent->nc_flag & NCF_DESTROYED)
1380 par = ncp->nc_parent;
1381 while (par->nc_parent && par->nc_parent->nc_vp == NULL)
1382 par = par->nc_parent;
1383 if (par->nc_parent == NULL) {
1384 printf("EXDEV case 2 %*.*s\n",
1385 par->nc_nlen, par->nc_nlen, par->nc_name);
1388 printf("[diagnostic] cache_resolve: had to recurse on %*.*s\n",
1389 par->nc_nlen, par->nc_nlen, par->nc_name);
1391 * The parent is not set in stone, ref and lock it to prevent
1392 * it from disappearing. Also note that due to renames it
1393 * is possible for our ncp to move and for par to no longer
1394 * be one of its parents. We resolve it anyway, the loop
1395 * will handle any moves.
1398 if (par->nc_flag & NCF_MOUNTPT) {
1399 cache_resolve_mp(par);
1400 } else if (par->nc_parent->nc_vp == NULL) {
1401 printf("[diagnostic] cache_resolve: raced on %*.*s\n", par->nc_nlen, par->nc_nlen, par->nc_name);
1404 } else if (par->nc_flag & NCF_UNRESOLVED) {
1405 par->nc_error = VOP_NRESOLVE(par, cred);
1407 if ((error = par->nc_error) != 0) {
1408 if (par->nc_error != EAGAIN) {
1409 printf("EXDEV case 3 %*.*s error %d\n",
1410 par->nc_nlen, par->nc_nlen, par->nc_name,
1415 printf("[diagnostic] cache_resolve: EAGAIN par %p %*.*s\n",
1416 par, par->nc_nlen, par->nc_nlen, par->nc_name);
1423 * Call VOP_NRESOLVE() to get the vp, then scan for any disconnected
1424 * ncp's and reattach them. If this occurs the original ncp is marked
1425 * EAGAIN to force a relookup.
1427 * NOTE: in order to call VOP_NRESOLVE(), the parent of the passed
1428 * ncp must already be resolved.
1430 KKASSERT((ncp->nc_flag & NCF_MOUNTPT) == 0);
1431 ncp->nc_error = VOP_NRESOLVE(ncp, cred);
1432 /*vop_nresolve(*ncp->nc_parent->nc_vp->v_ops, ncp, cred);*/
1433 if (ncp->nc_error == EAGAIN) {
1434 printf("[diagnostic] cache_resolve: EAGAIN ncp %p %*.*s\n",
1435 ncp, ncp->nc_nlen, ncp->nc_nlen, ncp->nc_name);
1438 return(ncp->nc_error);
1442 * Resolve the ncp associated with a mount point. Such ncp's almost always
1443 * remain resolved and this routine is rarely called. NFS MPs tends to force
1444 * re-resolution more often due to its mac-truck-smash-the-namecache
1445 * method of tracking namespace changes.
1447 * The semantics for this call is that the passed ncp must be locked on
1448 * entry and will be locked on return. However, if we actually have to
1449 * resolve the mount point we temporarily unlock the entry in order to
1450 * avoid race-to-root deadlocks due to e.g. dead NFS mounts. Because of
1451 * the unlock we have to recheck the flags after we relock.
1454 cache_resolve_mp(struct namecache *ncp)
1457 struct mount *mp = ncp->nc_mount;
1460 KKASSERT(mp != NULL);
1461 if (ncp->nc_flag & NCF_UNRESOLVED) {
1463 while (vfs_busy(mp, 0, NULL, curthread))
1465 error = VFS_ROOT(mp, &vp);
1469 * recheck the ncp state after relocking.
1471 if (ncp->nc_flag & NCF_UNRESOLVED) {
1472 ncp->nc_error = error;
1474 cache_setvp(ncp, vp);
1477 printf("[diagnostic] cache_resolve_mp: failed to resolve mount %p\n", mp);
1478 cache_setvp(ncp, NULL);
1480 } else if (error == 0) {
1483 vfs_unbusy(mp, curthread);
1485 return(ncp->nc_error);
1489 cache_cleanneg(int count)
1491 struct namecache *ncp;
1494 * Automode from the vnlru proc - clean out 10% of the negative cache
1498 count = numneg / 10 + 1;
1501 * Attempt to clean out the specified number of negative cache
1505 ncp = TAILQ_FIRST(&ncneglist);
1507 KKASSERT(numneg == 0);
1510 TAILQ_REMOVE(&ncneglist, ncp, nc_vnode);
1511 TAILQ_INSERT_TAIL(&ncneglist, ncp, nc_vnode);
1512 if (cache_get_nonblock(ncp) == 0)
1519 * Rehash a ncp. Rehashing is typically required if the name changes (should
1520 * not generally occur) or the parent link changes. This function will
1521 * unhash the ncp if the ncp is no longer hashable.
1524 cache_rehash(struct namecache *ncp)
1526 struct nchashhead *nchpp;
1529 if (ncp->nc_flag & NCF_HASHED) {
1530 ncp->nc_flag &= ~NCF_HASHED;
1531 LIST_REMOVE(ncp, nc_hash);
1533 if (ncp->nc_nlen && ncp->nc_parent) {
1534 hash = fnv_32_buf(ncp->nc_name, ncp->nc_nlen, FNV1_32_INIT);
1535 hash = fnv_32_buf(&ncp->nc_parent,
1536 sizeof(ncp->nc_parent), hash);
1537 nchpp = NCHHASH(hash);
1538 LIST_INSERT_HEAD(nchpp, ncp, nc_hash);
1539 ncp->nc_flag |= NCF_HASHED;
1544 * Name cache initialization, from vfsinit() when we are booting
1552 /* initialise per-cpu namecache effectiveness statistics. */
1553 for (i = 0; i < ncpus; ++i) {
1554 gd = globaldata_find(i);
1555 gd->gd_nchstats = &nchstats[i];
1557 TAILQ_INIT(&ncneglist);
1558 nchashtbl = hashinit(desiredvnodes*2, M_VFSCACHE, &nchash);
1559 nclockwarn = 1 * hz;
1563 * Called from start_init() to bootstrap the root filesystem. Returns
1564 * a referenced, unlocked namecache record.
1567 cache_allocroot(struct mount *mp, struct vnode *vp)
1569 struct namecache *ncp = cache_alloc(0);
1571 ncp->nc_flag |= NCF_MOUNTPT | NCF_ROOT;
1573 cache_setvp(ncp, vp);
1578 * vfs_cache_setroot()
1580 * Create an association between the root of our namecache and
1581 * the root vnode. This routine may be called several times during
1584 * If the caller intends to save the returned namecache pointer somewhere
1585 * it must cache_hold() it.
1588 vfs_cache_setroot(struct vnode *nvp, struct namecache *ncp)
1591 struct namecache *oncp;
1605 * XXX OLD API COMPAT FUNCTION. This really messes up the new namecache
1606 * topology and is being removed as quickly as possible. The new VOP_N*()
1607 * API calls are required to make specific adjustments using the supplied
1608 * ncp pointers rather then just bogusly purging random vnodes.
1610 * Invalidate all namecache entries to a particular vnode as well as
1611 * any direct children of that vnode in the namecache. This is a
1612 * 'catch all' purge used by filesystems that do not know any better.
1614 * A new vnode v_id is generated. Note that no vnode will ever have a
1617 * Note that the linkage between the vnode and its namecache entries will
1618 * be removed, but the namecache entries themselves might stay put due to
1619 * active references from elsewhere in the system or due to the existance of
1620 * the children. The namecache topology is left intact even if we do not
1621 * know what the vnode association is. Such entries will be marked
1624 * XXX: Only time and the size of v_id prevents this from failing:
1625 * XXX: In theory we should hunt down all (struct vnode*, v_id)
1626 * XXX: soft references and nuke them, at least on the global
1627 * XXX: v_id wraparound. The period of resistance can be extended
1628 * XXX: by incrementing each vnodes v_id individually instead of
1629 * XXX: using the global v_id.
1632 cache_purge(struct vnode *vp)
1634 static u_long nextid;
1636 cache_inval_vp(vp, CINV_DESTROY | CINV_CHILDREN);
1639 * Calculate a new unique id for ".." handling
1643 } while (nextid == vp->v_id || nextid == 0);
1648 * Flush all entries referencing a particular filesystem.
1650 * Since we need to check it anyway, we will flush all the invalid
1651 * entries at the same time.
1654 cache_purgevfs(struct mount *mp)
1656 struct nchashhead *nchpp;
1657 struct namecache *ncp, *nnp;
1660 * Scan hash tables for applicable entries.
1662 for (nchpp = &nchashtbl[nchash]; nchpp >= nchashtbl; nchpp--) {
1663 ncp = LIST_FIRST(nchpp);
1667 nnp = LIST_NEXT(ncp, nc_hash);
1670 if (ncp->nc_mount == mp) {
1681 static int disablecwd;
1682 SYSCTL_INT(_debug, OID_AUTO, disablecwd, CTLFLAG_RW, &disablecwd, 0, "");
1684 static u_long numcwdcalls; STATNODE(CTLFLAG_RD, numcwdcalls, &numcwdcalls);
1685 static u_long numcwdfail1; STATNODE(CTLFLAG_RD, numcwdfail1, &numcwdfail1);
1686 static u_long numcwdfail2; STATNODE(CTLFLAG_RD, numcwdfail2, &numcwdfail2);
1687 static u_long numcwdfail3; STATNODE(CTLFLAG_RD, numcwdfail3, &numcwdfail3);
1688 static u_long numcwdfail4; STATNODE(CTLFLAG_RD, numcwdfail4, &numcwdfail4);
1689 static u_long numcwdfound; STATNODE(CTLFLAG_RD, numcwdfound, &numcwdfound);
1692 __getcwd(struct __getcwd_args *uap)
1702 buflen = uap->buflen;
1705 if (buflen > MAXPATHLEN)
1706 buflen = MAXPATHLEN;
1708 buf = malloc(buflen, M_TEMP, M_WAITOK);
1709 bp = kern_getcwd(buf, buflen, &error);
1711 error = copyout(bp, uap->buf, strlen(bp) + 1);
1717 kern_getcwd(char *buf, size_t buflen, int *error)
1719 struct proc *p = curproc;
1721 int i, slash_prefixed;
1722 struct filedesc *fdp;
1723 struct namecache *ncp;
1732 ncp = fdp->fd_ncdir;
1733 while (ncp && ncp != fdp->fd_nrdir && (ncp->nc_flag & NCF_ROOT) == 0) {
1734 if (ncp->nc_flag & NCF_MOUNTPT) {
1735 if (ncp->nc_mount == NULL) {
1736 *error = EBADF; /* forced unmount? */
1739 ncp = ncp->nc_parent;
1742 for (i = ncp->nc_nlen - 1; i >= 0; i--) {
1748 *--bp = ncp->nc_name[i];
1757 ncp = ncp->nc_parent;
1764 if (!slash_prefixed) {
1778 * Thus begins the fullpath magic.
1782 #define STATNODE(name) \
1783 static u_int name; \
1784 SYSCTL_UINT(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, 0, "")
1786 static int disablefullpath;
1787 SYSCTL_INT(_debug, OID_AUTO, disablefullpath, CTLFLAG_RW,
1788 &disablefullpath, 0, "");
1790 STATNODE(numfullpathcalls);
1791 STATNODE(numfullpathfail1);
1792 STATNODE(numfullpathfail2);
1793 STATNODE(numfullpathfail3);
1794 STATNODE(numfullpathfail4);
1795 STATNODE(numfullpathfound);
1798 cache_fullpath(struct proc *p, struct namecache *ncp, char **retbuf, char **freebuf)
1801 int i, slash_prefixed;
1802 struct namecache *fd_nrdir;
1806 buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1807 bp = buf + MAXPATHLEN - 1;
1810 fd_nrdir = p->p_fd->fd_nrdir;
1814 while (ncp && ncp != fd_nrdir && (ncp->nc_flag & NCF_ROOT) == 0) {
1815 if (ncp->nc_flag & NCF_MOUNTPT) {
1816 if (ncp->nc_mount == NULL) {
1820 ncp = ncp->nc_parent;
1823 for (i = ncp->nc_nlen - 1; i >= 0; i--) {
1829 *--bp = ncp->nc_name[i];
1838 ncp = ncp->nc_parent;
1845 if (p != NULL && (ncp->nc_flag & NCF_ROOT) && ncp != fd_nrdir) {
1846 bp = buf + MAXPATHLEN - 1;
1850 if (!slash_prefixed) {
1866 vn_fullpath(struct proc *p, struct vnode *vn, char **retbuf, char **freebuf)
1868 struct namecache *ncp;
1871 if (disablefullpath)
1877 /* vn is NULL, client wants us to use p->p_textvp */
1879 if ((vn = p->p_textvp) == NULL)
1882 TAILQ_FOREACH(ncp, &vn->v_namecache, nc_vnode) {
1890 return(cache_fullpath(p, ncp, retbuf, freebuf));