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.38 2004/10/12 19:20: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>
91 * Random lookups in the cache are accomplished with a hash table using
92 * a hash key of (nc_src_vp, name).
94 * Negative entries may exist and correspond to structures where nc_vp
95 * is NULL. In a negative entry, NCF_WHITEOUT will be set if the entry
96 * corresponds to a whited-out directory entry (verses simply not finding the
99 * Upon reaching the last segment of a path, if the reference is for DELETE,
100 * or NOCACHE is set (rewrite), and the name is located in the cache, it
105 * Structures associated with name cacheing.
107 #define NCHHASH(hash) (&nchashtbl[(hash) & nchash])
110 MALLOC_DEFINE(M_VFSCACHE, "vfscache", "VFS name cache entries");
112 static LIST_HEAD(nchashhead, namecache) *nchashtbl; /* Hash Table */
113 static struct namecache_list ncneglist; /* instead of vnode */
115 static u_long nchash; /* size of hash table */
116 SYSCTL_ULONG(_debug, OID_AUTO, nchash, CTLFLAG_RD, &nchash, 0, "");
118 static u_long ncnegfactor = 16; /* ratio of negative entries */
119 SYSCTL_ULONG(_debug, OID_AUTO, ncnegfactor, CTLFLAG_RW, &ncnegfactor, 0, "");
121 static u_long numneg; /* number of cache entries allocated */
122 SYSCTL_ULONG(_debug, OID_AUTO, numneg, CTLFLAG_RD, &numneg, 0, "");
124 static u_long numcache; /* number of cache entries allocated */
125 SYSCTL_ULONG(_debug, OID_AUTO, numcache, CTLFLAG_RD, &numcache, 0, "");
127 static u_long numunres; /* number of unresolved entries */
128 SYSCTL_ULONG(_debug, OID_AUTO, numunres, CTLFLAG_RD, &numunres, 0, "");
130 SYSCTL_INT(_debug, OID_AUTO, vnsize, CTLFLAG_RD, 0, sizeof(struct vnode), "");
131 SYSCTL_INT(_debug, OID_AUTO, ncsize, CTLFLAG_RD, 0, sizeof(struct namecache), "");
133 static int cache_resolve_mp(struct namecache *ncp);
134 static void cache_rehash(struct namecache *ncp);
137 * The new name cache statistics
139 SYSCTL_NODE(_vfs, OID_AUTO, cache, CTLFLAG_RW, 0, "Name cache statistics");
140 #define STATNODE(mode, name, var) \
141 SYSCTL_ULONG(_vfs_cache, OID_AUTO, name, mode, var, 0, "");
142 STATNODE(CTLFLAG_RD, numneg, &numneg);
143 STATNODE(CTLFLAG_RD, numcache, &numcache);
144 static u_long numcalls; STATNODE(CTLFLAG_RD, numcalls, &numcalls);
145 static u_long dothits; STATNODE(CTLFLAG_RD, dothits, &dothits);
146 static u_long dotdothits; STATNODE(CTLFLAG_RD, dotdothits, &dotdothits);
147 static u_long numchecks; STATNODE(CTLFLAG_RD, numchecks, &numchecks);
148 static u_long nummiss; STATNODE(CTLFLAG_RD, nummiss, &nummiss);
149 static u_long nummisszap; STATNODE(CTLFLAG_RD, nummisszap, &nummisszap);
150 static u_long numposzaps; STATNODE(CTLFLAG_RD, numposzaps, &numposzaps);
151 static u_long numposhits; STATNODE(CTLFLAG_RD, numposhits, &numposhits);
152 static u_long numnegzaps; STATNODE(CTLFLAG_RD, numnegzaps, &numnegzaps);
153 static u_long numneghits; STATNODE(CTLFLAG_RD, numneghits, &numneghits);
155 struct nchstats nchstats[SMP_MAXCPU];
157 * Export VFS cache effectiveness statistics to user-land.
159 * The statistics are left for aggregation to user-land so
160 * neat things can be achieved, like observing per-CPU cache
164 sysctl_nchstats(SYSCTL_HANDLER_ARGS)
166 struct globaldata *gd;
170 for (i = 0; i < ncpus; ++i) {
171 gd = globaldata_find(i);
172 if ((error = SYSCTL_OUT(req, (void *)&(*gd->gd_nchstats),
173 sizeof(struct nchstats))))
179 SYSCTL_PROC(_vfs_cache, OID_AUTO, nchstats, CTLTYPE_OPAQUE|CTLFLAG_RD,
180 0, 0, sysctl_nchstats, "S,nchstats", "VFS cache effectiveness statistics");
182 static void cache_zap(struct namecache *ncp);
185 * cache_hold() and cache_drop() prevent the premature deletion of a
186 * namecache entry but do not prevent operations (such as zapping) on
187 * that namecache entry.
191 _cache_hold(struct namecache *ncp)
198 * When dropping an entry, if only one ref remains and the entry has not
199 * been resolved, zap it. Since the one reference is being dropped the
200 * entry had better not be locked.
204 _cache_drop(struct namecache *ncp)
206 KKASSERT(ncp->nc_refs > 0);
207 if (ncp->nc_refs == 1 &&
208 (ncp->nc_flag & NCF_UNRESOLVED) &&
209 TAILQ_EMPTY(&ncp->nc_list)
211 KKASSERT(ncp->nc_exlocks == 0);
220 * Link a new namecache entry to its parent. Be careful to avoid races
221 * if vhold() blocks in the future.
223 * If we are creating a child under an oldapi parent we must mark the
224 * child as being an oldapi entry as well.
227 cache_link_parent(struct namecache *ncp, struct namecache *par)
229 KKASSERT(ncp->nc_parent == NULL);
230 ncp->nc_parent = par;
231 if (TAILQ_EMPTY(&par->nc_list)) {
232 TAILQ_INSERT_HEAD(&par->nc_list, ncp, nc_entry);
234 * Any vp associated with an ncp which has children must
235 * be held to prevent it from being recycled.
240 TAILQ_INSERT_HEAD(&par->nc_list, ncp, nc_entry);
245 * Remove the parent association from a namecache structure. If this is
246 * the last child of the parent the cache_drop(par) will attempt to
247 * recursively zap the parent.
250 cache_unlink_parent(struct namecache *ncp)
252 struct namecache *par;
254 if ((par = ncp->nc_parent) != NULL) {
255 ncp->nc_parent = NULL;
256 par = cache_hold(par);
257 TAILQ_REMOVE(&par->nc_list, ncp, nc_entry);
258 if (par->nc_vp && TAILQ_EMPTY(&par->nc_list))
265 * Allocate a new namecache structure.
267 static struct namecache *
268 cache_alloc(int nlen)
270 struct namecache *ncp;
272 ncp = malloc(sizeof(*ncp), M_VFSCACHE, M_WAITOK|M_ZERO);
274 ncp->nc_name = malloc(nlen, M_VFSCACHE, M_WAITOK);
276 ncp->nc_flag = NCF_UNRESOLVED;
277 ncp->nc_error = ENOTCONN; /* needs to be resolved */
279 TAILQ_INIT(&ncp->nc_list);
285 cache_free(struct namecache *ncp)
287 KKASSERT(ncp->nc_refs == 1 && ncp->nc_exlocks == 1);
289 free(ncp->nc_name, M_VFSCACHE);
290 free(ncp, M_VFSCACHE);
294 * Ref and deref a namecache structure.
297 cache_hold(struct namecache *ncp)
299 return(_cache_hold(ncp));
303 cache_drop(struct namecache *ncp)
309 * Namespace locking. The caller must already hold a reference to the
310 * namecache structure in order to lock/unlock it. This function prevents
311 * the namespace from being created or destroyed by accessors other then
314 * Note that holding a locked namecache structure prevents other threads
315 * from making namespace changes (e.g. deleting or creating), prevents
316 * vnode association state changes by other threads, and prevents the
317 * namecache entry from being resolved or unresolved by other threads.
319 * The lock owner has full authority to associate/disassociate vnodes
320 * and resolve/unresolve the locked ncp.
322 * In particular, if a vnode is associated with a locked cache entry
323 * that vnode will *NOT* be recycled. We accomplish this by vhold()ing the
324 * vnode. XXX we should find a more efficient way to prevent the vnode
325 * from being recycled, but remember that any given vnode may have multiple
326 * namecache associations (think hardlinks).
329 cache_lock(struct namecache *ncp)
334 KKASSERT(ncp->nc_refs != 0);
339 if (ncp->nc_exlocks == 0) {
343 * The vp associated with a locked ncp must be held
344 * to prevent it from being recycled (which would
345 * cause the ncp to become unresolved).
347 * XXX loop on race for later MPSAFE work.
353 if (ncp->nc_locktd == td) {
357 ncp->nc_flag |= NCF_LOCKREQ;
358 if (tsleep(ncp, 0, "clock", hz) == EWOULDBLOCK) {
362 printf("[diagnostic] cache_lock: blocked on %p", ncp);
363 if ((ncp->nc_flag & NCF_MOUNTPT) && ncp->nc_mount)
364 printf(" [MOUNTPT %s]\n", ncp->nc_mount->mnt_stat.f_mntonname);
366 printf(" \"%*.*s\"\n",
367 ncp->nc_nlen, ncp->nc_nlen,
373 printf("[diagnostic] cache_lock: unblocked %*.*s\n",
374 ncp->nc_nlen, ncp->nc_nlen, ncp->nc_name);
379 cache_unlock(struct namecache *ncp)
381 thread_t td = curthread;
383 KKASSERT(ncp->nc_refs > 0);
384 KKASSERT(ncp->nc_exlocks > 0);
385 KKASSERT(ncp->nc_locktd == td);
386 if (--ncp->nc_exlocks == 0) {
389 ncp->nc_locktd = NULL;
390 if (ncp->nc_flag & NCF_LOCKREQ) {
391 ncp->nc_flag &= ~NCF_LOCKREQ;
398 * ref-and-lock, unlock-and-deref functions.
401 cache_get(struct namecache *ncp)
409 cache_get_nonblock(struct namecache *ncp)
412 if (ncp->nc_exlocks == 0 || ncp->nc_locktd == curthread) {
421 cache_put(struct namecache *ncp)
428 * Resolve an unresolved ncp by associating a vnode with it. If the
429 * vnode is NULL, a negative cache entry is created.
431 * The ncp should be locked on entry and will remain locked on return.
434 cache_setvp(struct namecache *ncp, struct vnode *vp)
436 KKASSERT(ncp->nc_flag & NCF_UNRESOLVED);
440 * Any vp associated with an ncp which has children must
441 * be held. Any vp associated with a locked ncp must be held.
443 if (!TAILQ_EMPTY(&ncp->nc_list))
445 TAILQ_INSERT_HEAD(&vp->v_namecache, ncp, nc_vnode);
450 * Set auxillary flags
454 ncp->nc_flag |= NCF_ISDIR;
457 ncp->nc_flag |= NCF_ISSYMLINK;
458 /* XXX cache the contents of the symlink */
466 TAILQ_INSERT_TAIL(&ncneglist, ncp, nc_vnode);
468 ncp->nc_error = ENOENT;
470 ncp->nc_flag &= ~NCF_UNRESOLVED;
474 * Disassociate the vnode or negative-cache association and mark a
475 * namecache entry as unresolved again. Note that the ncp is still
476 * left in the hash table and still linked to its parent.
478 * The ncp should be locked and refd on entry and will remain locked and refd
481 * This routine is normally never called on a directory containing children.
482 * However, NFS often does just that in its rename() code as a cop-out to
483 * avoid complex namespace operations. This disconnects a directory vnode
484 * from its namecache and can cause the OLDAPI and NEWAPI to get out of
488 cache_setunresolved(struct namecache *ncp)
492 if ((ncp->nc_flag & NCF_UNRESOLVED) == 0) {
493 ncp->nc_flag |= NCF_UNRESOLVED;
494 ncp->nc_flag &= ~(NCF_WHITEOUT|NCF_ISDIR|NCF_ISSYMLINK);
495 ncp->nc_error = ENOTCONN;
497 if ((vp = ncp->nc_vp) != NULL) {
499 ncp->nc_vp = NULL; /* safety */
500 TAILQ_REMOVE(&vp->v_namecache, ncp, nc_vnode);
503 * Any vp associated with an ncp with children is
504 * held by that ncp. Any vp associated with a locked
505 * ncp is held by that ncp. These conditions must be
506 * undone when the vp is cleared out from the ncp.
508 if (!TAILQ_EMPTY(&ncp->nc_list))
513 TAILQ_REMOVE(&ncneglist, ncp, nc_vnode);
518 if (TAILQ_FIRST(&ncp->nc_list)) {
519 db_print_backtrace();
520 printf("[diagnostic] cache_setunresolved() called on directory with children: %p %*.*s\n", ncp, ncp->nc_nlen, ncp->nc_nlen, ncp->nc_name);
527 * Invalidate portions of a namecache entry. The passed ncp should be
528 * referenced and locked but we might not adhere to that rule during the
529 * old api -> new api transition period.
531 * CINV_PARENT - disconnect the ncp from its parent
532 * CINV_SELF - same as cache_setunresolved(ncp)
533 * CINV_CHILDREN - disconnect children of the ncp from the ncp
536 cache_inval(struct namecache *ncp, int flags)
538 struct namecache *kid;
539 struct namecache *nextkid;
541 if (flags & CINV_SELF)
542 cache_setunresolved(ncp);
543 if (flags & CINV_PARENT) {
544 ncp->nc_flag |= NCF_REVALPARENT;
545 cache_unlink_parent(ncp);
549 * TEMPORARY XX old-api / rename handling. Any unresolved or
550 * negative cache-hit children with a ref count of 0 must be
551 * recursively destroyed or this disconnection from our parent,
552 * or the childrens disconnection from us, may leave them dangling
555 * In the new API it won't be possible to unlink in the middle of
556 * the topology and we will have a cache_rename() to physically
557 * move a subtree from one place to another.
559 if (flags & (CINV_PARENT|CINV_CHILDREN)) {
560 if ((kid = TAILQ_FIRST(&ncp->nc_list)) != NULL)
563 if ((nextkid = TAILQ_NEXT(kid, nc_entry)) != NULL)
565 if (kid->nc_refs == 0 &&
566 ((kid->nc_flag & NCF_UNRESOLVED) ||
569 cache_inval(kid, CINV_PARENT);
577 * TEMPORARY XXX old-api / rename handling.
579 if (flags & CINV_CHILDREN) {
580 while ((kid = TAILQ_FIRST(&ncp->nc_list)) != NULL) {
581 kid->nc_flag |= NCF_REVALPARENT;
583 cache_unlink_parent(kid);
590 cache_inval_vp(struct vnode *vp, int flags)
592 struct namecache *ncp;
594 if (flags & CINV_SELF) {
595 while ((ncp = TAILQ_FIRST(&vp->v_namecache)) != NULL) {
597 KKASSERT((ncp->nc_flag & NCF_UNRESOLVED) == 0);
598 cache_inval(ncp, flags);
602 TAILQ_FOREACH(ncp, &vp->v_namecache, nc_vnode) {
604 cache_inval(ncp, flags);
611 * vget the vnode associated with the namecache entry. Resolve the namecache
612 * entry if necessary and deal with namecache/vp races. The passed ncp must
613 * be referenced and may be locked. The ncp's ref/locking state is not
614 * effected by this call.
616 * lk_type may be LK_SHARED, LK_EXCLUSIVE. A ref'd, possibly locked
617 * (depending on the passed lk_type) will be returned in *vpp with an error
618 * of 0, or NULL will be returned in *vpp with a non-0 error code. The
619 * most typical error is ENOENT, meaning that the ncp represents a negative
620 * cache hit and there is no vnode to retrieve, but other errors can occur
623 * The main race we have to deal with are namecache zaps. The ncp itself
624 * will not disappear since it is referenced, and it turns out that the
625 * validity of the vp pointer can be checked simply by rechecking the
626 * contents of ncp->nc_vp.
629 cache_vget(struct namecache *ncp, struct ucred *cred,
630 int lk_type, struct vnode **vpp)
637 if (ncp->nc_flag & NCF_UNRESOLVED) {
639 error = cache_resolve(ncp, cred);
644 if (error == 0 && (vp = ncp->nc_vp) != NULL) {
645 error = vget(vp, lk_type, curthread);
647 if (vp != ncp->nc_vp) /* handle cache_zap race */
650 } else if (vp != ncp->nc_vp) { /* handle cache_zap race */
655 if (error == 0 && vp == NULL)
662 cache_vref(struct namecache *ncp, struct ucred *cred, struct vnode **vpp)
669 if (ncp->nc_flag & NCF_UNRESOLVED) {
671 error = cache_resolve(ncp, cred);
676 if (error == 0 && (vp = ncp->nc_vp) != NULL) {
678 if (vp != ncp->nc_vp) { /* handle cache_zap race */
683 if (error == 0 && vp == NULL)
690 * Zap a namecache entry. The ncp is unconditionally set to an unresolved
691 * state, which disassociates it from its vnode or ncneglist.
693 * Then, if there are no additional references to the ncp and no children,
694 * the ncp is removed from the topology and destroyed. This function will
695 * also run through the nc_parent chain and destroy parent ncps if possible.
696 * As a side benefit, it turns out the only conditions that allow running
697 * up the chain are also the conditions to ensure no deadlock will occur.
699 * References and/or children may exist if the ncp is in the middle of the
700 * topology, preventing the ncp from being destroyed.
702 * This function must be called with the ncp held and locked and will unlock
703 * and drop it during zapping.
706 cache_zap(struct namecache *ncp)
708 struct namecache *par;
711 * Disassociate the vnode or negative cache ref and set NCF_UNRESOLVED.
713 cache_setunresolved(ncp);
716 * Try to scrap the entry and possibly tail-recurse on its parent.
717 * We only scrap unref'd (other then our ref) unresolved entries,
718 * we do not scrap 'live' entries.
720 while (ncp->nc_flag & NCF_UNRESOLVED) {
722 * Someone other then us has a ref, stop.
724 if (ncp->nc_refs > 1)
728 * We have children, stop.
730 if (!TAILQ_EMPTY(&ncp->nc_list))
734 * Remove ncp from the topology: hash table and parent linkage.
736 if (ncp->nc_flag & NCF_HASHED) {
737 ncp->nc_flag &= ~NCF_HASHED;
738 LIST_REMOVE(ncp, nc_hash);
740 if ((par = ncp->nc_parent) != NULL) {
741 par = cache_hold(par);
742 TAILQ_REMOVE(&par->nc_list, ncp, nc_entry);
743 ncp->nc_parent = NULL;
744 if (par->nc_vp && TAILQ_EMPTY(&par->nc_list))
749 * ncp should not have picked up any refs. Physically
752 KKASSERT(ncp->nc_refs == 1);
754 /* cache_unlock(ncp) not required */
755 ncp->nc_refs = -1; /* safety */
757 free(ncp->nc_name, M_VFSCACHE);
758 free(ncp, M_VFSCACHE);
761 * Loop on the parent (it may be NULL). Only bother looping
762 * if the parent has a single ref (ours), which also means
763 * we can lock it trivially.
768 if (ncp->nc_refs != 1) {
772 KKASSERT(par->nc_exlocks == 0);
781 * NEW NAMECACHE LOOKUP API
783 * Lookup an entry in the cache. A locked, referenced, non-NULL
784 * entry is *always* returned, even if the supplied component is illegal.
785 * The returned namecache entry should be returned to the system with
786 * cache_put() or cache_unlock() + cache_drop().
788 * namecache locks are recursive but care must be taken to avoid lock order
791 * Nobody else will be able to manipulate the associated namespace (e.g.
792 * create, delete, rename, rename-target) until the caller unlocks the
795 * The returned entry will be in one of three states: positive hit (non-null
796 * vnode), negative hit (null vnode), or unresolved (NCF_UNRESOLVED is set).
797 * Unresolved entries must be resolved through the filesystem to associate the
798 * vnode and/or determine whether a positive or negative hit has occured.
800 * It is not necessary to lock a directory in order to lock namespace under
801 * that directory. In fact, it is explicitly not allowed to do that. A
802 * directory is typically only locked when being created, renamed, or
805 * The directory (par) may be unresolved, in which case any returned child
806 * will likely also be marked unresolved. Likely but not guarenteed. Since
807 * the filesystem VOP_NEWLOOKUP() requires a resolved directory vnode the
808 * caller is responsible for resolving the namecache chain top-down. This API
809 * specifically allows whole chains to be created in an unresolved state.
812 cache_nlookup(struct namecache *par, struct nlcomponent *nlc)
814 struct namecache *ncp;
815 struct namecache *new_ncp;
816 struct nchashhead *nchpp;
824 * Try to locate an existing entry
826 hash = fnv_32_buf(nlc->nlc_nameptr, nlc->nlc_namelen, FNV1_32_INIT);
827 hash = fnv_32_buf(&par, sizeof(par), hash);
830 LIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
834 * Zap entries that have timed out.
836 if (ncp->nc_timeout &&
837 (int)(ncp->nc_timeout - ticks) < 0 &&
838 (ncp->nc_flag & NCF_UNRESOLVED) == 0 &&
841 cache_zap(cache_get(ncp));
846 * Break out if we find a matching entry. Note that
847 * UNRESOLVED entries may match.
849 if (ncp->nc_parent == par &&
850 ncp->nc_nlen == nlc->nlc_namelen &&
851 bcmp(ncp->nc_name, nlc->nlc_nameptr, ncp->nc_nlen) == 0
853 if (cache_get_nonblock(ncp) == 0) {
865 * We failed to locate an entry, create a new entry and add it to
866 * the cache. We have to relookup after possibly blocking in
869 if (new_ncp == NULL) {
870 new_ncp = cache_alloc(nlc->nlc_namelen);
877 * Initialize as a new UNRESOLVED entry, lock (non-blocking),
878 * and link to the parent.
880 bcopy(nlc->nlc_nameptr, ncp->nc_name, nlc->nlc_namelen);
881 nchpp = NCHHASH(hash);
882 LIST_INSERT_HEAD(nchpp, ncp, nc_hash);
883 ncp->nc_flag |= NCF_HASHED;
884 cache_link_parent(ncp, par);
890 * Resolve an unresolved namecache entry, generally by looking it up.
891 * The passed ncp must be locked and refd.
893 * Theoretically since a vnode cannot be recycled while held, and since
894 * the nc_parent chain holds its vnode as long as children exist, the
895 * direct parent of the cache entry we are trying to resolve should
896 * have a valid vnode. If not then generate an error that we can
897 * determine is related to a resolver bug.
900 cache_resolve(struct namecache *ncp, struct ucred *cred)
902 struct namecache *par;
903 struct namecache *scan;
908 * If the ncp is already resolved we have nothing to do.
910 if ((ncp->nc_flag & NCF_UNRESOLVED) == 0)
911 return (ncp->nc_error);
914 * Mount points need special handling because the parent does not
915 * belong to the same filesystem as the ncp.
917 if (ncp->nc_flag & NCF_MOUNTPT)
918 return (cache_resolve_mp(ncp));
921 * We expect an unbroken chain of ncps to at least the mount point,
922 * and even all the way to root (but this code doesn't have to go
923 * past the mount point).
925 if (ncp->nc_parent == NULL) {
926 printf("EXDEV case 1 %p %*.*s\n", ncp,
927 ncp->nc_nlen, ncp->nc_nlen, ncp->nc_name);
928 ncp->nc_error = EXDEV;
929 return(ncp->nc_error);
933 * The vp's of the parent directories in the chain are held via vhold()
934 * due to the existance of the child, and should not disappear.
935 * However, there are cases where they can disappear:
937 * - due to filesystem I/O errors.
938 * - due to NFS being stupid about tracking the namespace and
939 * destroys the namespace for entire directories quite often.
940 * - due to forced unmounts.
942 * When this occurs we have to track the chain backwards and resolve
943 * it, looping until the resolver catches up to the current node. We
944 * could recurse here but we might run ourselves out of kernel stack
945 * so we do it in a more painful manner. This situation really should
946 * not occur all that often, or if it does not have to go back too
947 * many nodes to resolve the ncp.
949 while (ncp->nc_parent->nc_vp == NULL) {
950 par = ncp->nc_parent;
951 while (par->nc_parent && par->nc_parent->nc_vp == NULL)
952 par = par->nc_parent;
953 if (par->nc_parent == NULL) {
954 printf("EXDEV case 2 %*.*s\n",
955 par->nc_nlen, par->nc_nlen, par->nc_name);
958 printf("[diagnostic] cache_resolve: had to recurse on %*.*s\n",
959 par->nc_nlen, par->nc_nlen, par->nc_name);
961 * The parent is not set in stone, ref and lock it to prevent
962 * it from disappearing. Also note that due to renames it
963 * is possible for our ncp to move and for par to no longer
964 * be one of its parents. We resolve it anyway, the loop
965 * will handle any moves.
968 if (par->nc_flag & NCF_MOUNTPT) {
969 cache_resolve_mp(par);
970 } else if (par->nc_parent->nc_vp == NULL) {
971 printf("[diagnostic] cache_resolve: raced on %*.*s\n", par->nc_nlen, par->nc_nlen, par->nc_name);
976 vop_resolve(par->nc_parent->nc_vp->v_ops, par, cred);
978 if ((error = par->nc_error) != 0) {
979 if (par->nc_error != EAGAIN) {
980 printf("EXDEV case 3 %*.*s error %d\n",
981 par->nc_nlen, par->nc_nlen, par->nc_name,
986 printf("[diagnostic] cache_resolve: EAGAIN par %p %*.*s\n",
987 par, par->nc_nlen, par->nc_nlen, par->nc_name);
994 * Call vop_resolve() to get the vp, then scan for any disconnected
995 * ncp's and reattach them. If this occurs the original ncp is marked
996 * EAGAIN to force a relookup.
998 KKASSERT((ncp->nc_flag & NCF_MOUNTPT) == 0);
999 ncp->nc_error = vop_resolve(ncp->nc_parent->nc_vp->v_ops, ncp, cred);
1000 if (ncp->nc_error == EAGAIN) {
1001 printf("[diagnostic] cache_resolve: EAGAIN ncp %p %*.*s\n",
1002 ncp, ncp->nc_nlen, ncp->nc_nlen, ncp->nc_name);
1005 if (ncp->nc_error == 0) {
1006 TAILQ_FOREACH(scan, &ncp->nc_vp->v_namecache, nc_vnode) {
1007 if (scan != ncp && (scan->nc_flag & NCF_REVALPARENT)) {
1009 cache_link_parent(scan, ncp->nc_parent);
1010 cache_unlink_parent(ncp);
1011 scan->nc_flag &= ~NCF_REVALPARENT;
1012 ncp->nc_error = EAGAIN;
1013 if (scan->nc_flag & NCF_HASHED)
1015 printf("[diagnostic] cache_resolve: relinked %*.*s\n", scan->nc_nlen, scan->nc_nlen, scan->nc_name);
1021 return(ncp->nc_error);
1025 * Resolve the ncp associated with a mount point. Such ncp's almost always
1026 * remain resolved and this routine is rarely called. NFS MPs tends to force
1027 * re-resolution more often due to its mac-truck-smash-the-namecache
1028 * method of tracking namespace changes.
1030 * The passed ncp must be locked.
1033 cache_resolve_mp(struct namecache *ncp)
1036 struct mount *mp = ncp->nc_mount;
1038 KKASSERT(mp != NULL);
1039 if (ncp->nc_flag & NCF_UNRESOLVED) {
1040 while (vfs_busy(mp, 0, NULL, curthread))
1042 ncp->nc_error = VFS_ROOT(mp, &vp);
1043 if (ncp->nc_error == 0) {
1044 cache_setvp(ncp, vp);
1047 printf("[diagnostic] cache_resolve_mp: failed to resolve mount %p\n", mp);
1048 cache_setvp(ncp, NULL);
1050 vfs_unbusy(mp, curthread);
1052 return(ncp->nc_error);
1056 * Lookup an entry in the cache.
1058 * XXX OLD API ROUTINE! WHEN ALL VFSs HAVE BEEN CLEANED UP THIS PROCEDURE
1059 * WILL BE REMOVED. NOTE: even though this is an old api function it had
1060 * to be modified to vref() the returned vnode (whereas in 4.x an unreferenced
1061 * vnode was returned). This is necessary because our namecache structure
1062 * manipulation can cause the vnode to be recycled if it isn't refd.
1064 * Lookup is called with dvp pointing to the directory to search,
1065 * cnp pointing to the name of the entry being sought.
1067 * If the lookup succeeds, a REFd but unlocked vnode is returned in *vpp,
1068 * and a status of -1 is returned.
1070 * If the lookup determines that the name does not exist (negative cacheing),
1071 * a status of ENOENT is returned.
1073 * If the lookup fails, a status of zero is returned.
1075 * Matching UNRESOLVED entries are resolved.
1077 * HACKS: we create dummy nodes for parents
1080 cache_lookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
1082 struct namecache *ncp;
1083 struct namecache *par;
1084 struct namecache *bpar;
1086 globaldata_t gd = mycpu;
1092 * Obtain the namecache entry associated with dvp. If there is no
1093 * entry then assume a miss.
1095 if ((par = TAILQ_FIRST(&dvp->v_namecache)) == NULL) {
1096 if ((cnp->cn_flags & CNP_MAKEENTRY) == 0) {
1101 gd->gd_nchstats->ncs_miss++;
1106 * Deal with "." and "..". Note that if the namecache is disjoint,
1107 * we won't find a vnode for ".." and we return a miss.
1109 if (cnp->cn_nameptr[0] == '.') {
1110 if (cnp->cn_namelen == 1) {
1114 numposhits++; /* include in total statistics */
1117 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
1118 if ((cnp->cn_flags & CNP_MAKEENTRY) == 0) {
1123 if (par->nc_parent == NULL ||
1124 par->nc_parent->nc_vp == NULL) {
1126 gd->gd_nchstats->ncs_miss++;
1129 *vpp = par->nc_parent->nc_vp;
1132 numposhits++; /* include in total statistics */
1138 * Try to locate an existing entry
1141 hash = fnv_32_buf(cnp->cn_nameptr, cnp->cn_namelen, FNV1_32_INIT);
1143 hash = fnv_32_buf(&bpar, sizeof(bpar), hash);
1145 LIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
1149 * Zap entries that have timed out. Don't do anything if
1150 * the entry is in an unresolved state or is held locked.
1152 if (ncp->nc_timeout &&
1153 (int)(ncp->nc_timeout - ticks) < 0 &&
1154 (ncp->nc_flag & NCF_UNRESOLVED) == 0 &&
1155 ncp->nc_exlocks == 0
1157 cache_zap(cache_get(ncp));
1162 * Break out if we find a matching entry.
1164 if (ncp->nc_parent == par &&
1165 ncp->nc_nlen == cnp->cn_namelen &&
1166 bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen) == 0
1168 if (cache_get_nonblock(ncp) == 0)
1178 * We found an entry but it is unresolved, act the same as if we
1179 * failed to locate the entry. cache_enter() will do the right
1182 if (ncp && (ncp->nc_flag & NCF_UNRESOLVED)) {
1188 * If we failed to locate an entry, return 0 (indicates failure).
1191 if ((cnp->cn_flags & CNP_MAKEENTRY) == 0) {
1196 gd->gd_nchstats->ncs_miss++;
1201 * If we found an entry, but we don't want to have one, we just
1202 * return. The old API tried to zap the entry in the vfs_lookup()
1203 * phase but this is too early to know whether the operation
1204 * will have succeeded or not. The new API zaps it after the
1205 * operation has succeeded, not here.
1207 * At the same time, the old api's rename() function uses the
1208 * old api lookup to clear out any negative cache hit on the
1209 * target name. We still have to do that.
1211 if ((cnp->cn_flags & CNP_MAKEENTRY) == 0) {
1212 if (cnp->cn_nameiop == NAMEI_RENAME && ncp->nc_vp == NULL)
1220 * If the vnode is not NULL then return the positive match.
1224 gd->gd_nchstats->ncs_goodhits++;
1232 * If the vnode is NULL we found a negative match. If we want to
1233 * create it, purge the negative match and return failure (as if
1234 * we hadn't found a match in the first place).
1236 if (cnp->cn_nameiop == NAMEI_CREATE) {
1238 gd->gd_nchstats->ncs_badhits++;
1246 * We found a "negative" match, ENOENT notifies client of this match.
1247 * The nc_flag field records whether this is a whiteout. Since there
1248 * is no vnode we can use the vnode tailq link field with ncneglist.
1250 TAILQ_REMOVE(&ncneglist, ncp, nc_vnode);
1251 TAILQ_INSERT_TAIL(&ncneglist, ncp, nc_vnode);
1252 gd->gd_nchstats->ncs_neghits++;
1253 if (ncp->nc_flag & NCF_WHITEOUT)
1254 cnp->cn_flags |= CNP_ISWHITEOUT;
1260 * Add an entry to the cache. (OLD API)
1262 * XXX OLD API ROUTINE! WHEN ALL VFSs HAVE BEEN CLEANED UP THIS PROCEDURE
1265 * Generally speaking this is 'optional'. It's ok to do nothing at all.
1266 * The only reason I don't just return is to try to set nc_timeout if
1270 cache_enter(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
1272 struct namecache *par;
1273 struct namecache *ncp;
1274 struct namecache *new_ncp;
1275 struct namecache *bpar;
1276 struct nchashhead *nchpp;
1280 * If the directory has no namecache entry we bail. This will result
1281 * in a lot of misses but frankly we don't have much of a choice if
1282 * we want to be compatible with the new api's storage scheme.
1284 if ((ncp = TAILQ_FIRST(&dvp->v_namecache)) == NULL)
1289 * This may be a bit confusing. "." and ".." are 'virtual' entries.
1290 * We do not actually create a namecache entry representing either.
1291 * However, the ".." case is used to linkup a potentially disjoint
1292 * directory with its parent, to disconnect a directory from its
1293 * parent, or to change an existing linkage that may no longer be
1294 * correct (as might occur when a subdirectory is renamed).
1297 if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
1301 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[0] == '.' &&
1302 cnp->cn_nameptr[1] == '.'
1309 * Ok, no special cases, ncp is actually the parent directory so
1310 * assign it to par. Note that it is held.
1315 * Try to find a match in the hash table, allocate a new entry if
1316 * we can't. We have to retry the loop after any potential blocking
1320 hash = fnv_32_buf(cnp->cn_nameptr, cnp->cn_namelen, FNV1_32_INIT);
1321 hash = fnv_32_buf(&bpar, sizeof(bpar), hash);
1325 LIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
1329 * Break out if we find a matching entry. Because cache_enter
1330 * is called with one or more vnodes potentially locked, we
1331 * cannot block trying to get the ncp lock (or we might
1334 if (ncp->nc_parent == par &&
1335 ncp->nc_nlen == cnp->cn_namelen &&
1336 bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen) == 0
1338 if (cache_get_nonblock(ncp) != 0) {
1339 printf("[diagnostic] cache_enter: avoided race on %p %*.*s\n", ncp, ncp->nc_nlen, ncp->nc_nlen, ncp->nc_name);
1347 if (new_ncp == NULL) {
1348 new_ncp = cache_alloc(cnp->cn_namelen);
1352 bcopy(cnp->cn_nameptr, ncp->nc_name, cnp->cn_namelen);
1353 nchpp = NCHHASH(hash);
1354 LIST_INSERT_HEAD(nchpp, ncp, nc_hash);
1355 ncp->nc_flag |= NCF_HASHED;
1356 cache_link_parent(ncp, par);
1357 } else if (new_ncp) {
1358 cache_free(new_ncp);
1363 * Avoid side effects if we are simply re-entering the same
1366 if ((ncp->nc_flag & NCF_UNRESOLVED) == 0 && ncp->nc_vp == vp) {
1367 ncp->nc_error = vp ? 0 : ENOENT;
1369 cache_setunresolved(ncp);
1370 cache_setvp(ncp, vp);
1376 if (cnp->cn_flags & CNP_CACHETIMEOUT) {
1377 if ((ncp->nc_timeout = ticks + cnp->cn_timeout) == 0)
1378 ncp->nc_timeout = 1;
1382 * If the target vnode is NULL if this is to be a negative cache
1386 ncp->nc_flag &= ~NCF_WHITEOUT;
1387 if (cnp->cn_flags & CNP_ISWHITEOUT)
1388 ncp->nc_flag |= NCF_WHITEOUT;
1393 * Don't cache too many negative hits
1395 if (numneg > MINNEG && numneg * ncnegfactor > numcache) {
1396 ncp = TAILQ_FIRST(&ncneglist);
1397 KKASSERT(ncp != NULL);
1398 if (cache_get_nonblock(ncp) == 0)
1404 cache_rehash(struct namecache *ncp)
1406 struct nchashhead *nchpp;
1409 if (ncp->nc_flag & NCF_HASHED) {
1410 ncp->nc_flag &= ~NCF_HASHED;
1411 LIST_REMOVE(ncp, nc_hash);
1413 hash = fnv_32_buf(ncp->nc_name, ncp->nc_nlen, FNV1_32_INIT);
1414 hash = fnv_32_buf(&ncp->nc_parent, sizeof(ncp->nc_parent), hash);
1415 nchpp = NCHHASH(hash);
1416 LIST_INSERT_HEAD(nchpp, ncp, nc_hash);
1417 ncp->nc_flag |= NCF_HASHED;
1422 * Name cache initialization, from vfsinit() when we are booting
1430 /* initialise per-cpu namecache effectiveness statistics. */
1431 for (i = 0; i < ncpus; ++i) {
1432 gd = globaldata_find(i);
1433 gd->gd_nchstats = &nchstats[i];
1436 TAILQ_INIT(&ncneglist);
1437 nchashtbl = hashinit(desiredvnodes*2, M_VFSCACHE, &nchash);
1441 * Called from start_init() to bootstrap the root filesystem. Returns
1442 * a referenced, unlocked namecache record.
1445 cache_allocroot(struct mount *mp, struct vnode *vp)
1447 struct namecache *ncp = cache_alloc(0);
1449 ncp->nc_flag |= NCF_MOUNTPT | NCF_ROOT;
1451 cache_setvp(ncp, vp);
1456 * vfs_cache_setroot()
1458 * Create an association between the root of our namecache and
1459 * the root vnode. This routine may be called several times during
1462 * If the caller intends to save the returned namecache pointer somewhere
1463 * it must cache_hold() it.
1466 vfs_cache_setroot(struct vnode *nvp, struct namecache *ncp)
1469 struct namecache *oncp;
1483 * Invalidate all namecache entries to a particular vnode as well as
1484 * any direct children of that vnode in the namecache. This is a
1485 * 'catch all' purge used by filesystems that do not know any better.
1487 * A new vnode v_id is generated. Note that no vnode will ever have a
1490 * Note that the linkage between the vnode and its namecache entries will
1491 * be removed, but the namecache entries themselves might stay put due to
1492 * active references from elsewhere in the system or due to the existance of
1493 * the children. The namecache topology is left intact even if we do not
1494 * know what the vnode association is. Such entries will be marked
1497 * XXX: Only time and the size of v_id prevents this from failing:
1498 * XXX: In theory we should hunt down all (struct vnode*, v_id)
1499 * XXX: soft references and nuke them, at least on the global
1500 * XXX: v_id wraparound. The period of resistance can be extended
1501 * XXX: by incrementing each vnodes v_id individually instead of
1502 * XXX: using the global v_id.
1505 cache_purge(struct vnode *vp)
1507 static u_long nextid;
1509 cache_inval_vp(vp, CINV_PARENT | CINV_SELF | CINV_CHILDREN);
1512 * Calculate a new unique id for ".." handling
1516 } while (nextid == vp->v_id || nextid == 0);
1521 * Flush all entries referencing a particular filesystem.
1523 * Since we need to check it anyway, we will flush all the invalid
1524 * entries at the same time.
1527 cache_purgevfs(struct mount *mp)
1529 struct nchashhead *nchpp;
1530 struct namecache *ncp, *nnp;
1533 * Scan hash tables for applicable entries.
1535 for (nchpp = &nchashtbl[nchash]; nchpp >= nchashtbl; nchpp--) {
1536 ncp = LIST_FIRST(nchpp);
1540 nnp = LIST_NEXT(ncp, nc_hash);
1543 if (ncp->nc_vp && ncp->nc_vp->v_mount == mp) {
1555 * Perform canonical checks and cache lookup and pass on to filesystem
1556 * through the vop_cachedlookup only if needed.
1559 * struct vnode a_dvp;
1560 * struct vnode **a_vpp;
1561 * struct componentname *a_cnp;
1565 vfs_cache_lookup(struct vop_lookup_args *ap)
1567 struct vnode *dvp, *vp;
1570 struct vnode **vpp = ap->a_vpp;
1571 struct componentname *cnp = ap->a_cnp;
1572 struct ucred *cred = cnp->cn_cred;
1573 int flags = cnp->cn_flags;
1574 struct thread *td = cnp->cn_td;
1575 u_long vpid; /* capability number of vnode */
1579 lockparent = flags & CNP_LOCKPARENT;
1581 if (dvp->v_type != VDIR)
1584 if ((flags & CNP_ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
1585 (cnp->cn_nameiop == NAMEI_DELETE || cnp->cn_nameiop == NAMEI_RENAME)) {
1589 error = VOP_ACCESS(dvp, VEXEC, cred, td);
1594 error = cache_lookup(dvp, vpp, cnp);
1597 * failure if error == 0, do a physical lookup
1600 return (VOP_CACHEDLOOKUP(dvp, vpp, cnp));
1602 if (error == ENOENT)
1607 cnp->cn_flags &= ~CNP_PDIRUNLOCK;
1608 if (dvp == vp) { /* lookup on "." */
1609 /* already ref'd from cache_lookup() */
1611 } else if (flags & CNP_ISDOTDOT) {
1612 VOP_UNLOCK(dvp, 0, td);
1613 cnp->cn_flags |= CNP_PDIRUNLOCK;
1614 error = vget(vp, LK_EXCLUSIVE, td);
1616 if (!error && lockparent && (flags & CNP_ISLASTCN)) {
1617 if ((error = vn_lock(dvp, LK_EXCLUSIVE, td)) == 0)
1618 cnp->cn_flags &= ~CNP_PDIRUNLOCK;
1621 error = vget(vp, LK_EXCLUSIVE, td);
1623 if (!lockparent || error || !(flags & CNP_ISLASTCN)) {
1624 VOP_UNLOCK(dvp, 0, td);
1625 cnp->cn_flags |= CNP_PDIRUNLOCK;
1629 * Check that the capability number did not change
1630 * while we were waiting for the lock.
1633 if (vpid == vp->v_id)
1636 if (lockparent && dvp != vp && (flags & CNP_ISLASTCN)) {
1637 VOP_UNLOCK(dvp, 0, td);
1638 cnp->cn_flags |= CNP_PDIRUNLOCK;
1641 if (cnp->cn_flags & CNP_PDIRUNLOCK) {
1642 error = vn_lock(dvp, LK_EXCLUSIVE, td);
1645 cnp->cn_flags &= ~CNP_PDIRUNLOCK;
1647 return (VOP_CACHEDLOOKUP(dvp, vpp, cnp));
1650 static int disablecwd;
1651 SYSCTL_INT(_debug, OID_AUTO, disablecwd, CTLFLAG_RW, &disablecwd, 0, "");
1653 static u_long numcwdcalls; STATNODE(CTLFLAG_RD, numcwdcalls, &numcwdcalls);
1654 static u_long numcwdfail1; STATNODE(CTLFLAG_RD, numcwdfail1, &numcwdfail1);
1655 static u_long numcwdfail2; STATNODE(CTLFLAG_RD, numcwdfail2, &numcwdfail2);
1656 static u_long numcwdfail3; STATNODE(CTLFLAG_RD, numcwdfail3, &numcwdfail3);
1657 static u_long numcwdfail4; STATNODE(CTLFLAG_RD, numcwdfail4, &numcwdfail4);
1658 static u_long numcwdfound; STATNODE(CTLFLAG_RD, numcwdfound, &numcwdfound);
1661 __getcwd(struct __getcwd_args *uap)
1671 buflen = uap->buflen;
1674 if (buflen > MAXPATHLEN)
1675 buflen = MAXPATHLEN;
1677 buf = malloc(buflen, M_TEMP, M_WAITOK);
1678 bp = kern_getcwd(buf, buflen, &error);
1680 error = copyout(bp, uap->buf, strlen(bp) + 1);
1686 kern_getcwd(char *buf, size_t buflen, int *error)
1688 struct proc *p = curproc;
1690 int i, slash_prefixed;
1691 struct filedesc *fdp;
1692 struct namecache *ncp;
1701 ncp = fdp->fd_ncdir;
1702 while (ncp && ncp != fdp->fd_nrdir && (ncp->nc_flag & NCF_ROOT) == 0) {
1703 if (ncp->nc_flag & NCF_MOUNTPT) {
1704 if (ncp->nc_mount == NULL) {
1705 *error = EBADF; /* forced unmount? */
1708 ncp = ncp->nc_parent;
1711 for (i = ncp->nc_nlen - 1; i >= 0; i--) {
1717 *--bp = ncp->nc_name[i];
1726 ncp = ncp->nc_parent;
1733 if (!slash_prefixed) {
1747 * Thus begins the fullpath magic.
1751 #define STATNODE(name) \
1752 static u_int name; \
1753 SYSCTL_UINT(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, 0, "")
1755 static int disablefullpath;
1756 SYSCTL_INT(_debug, OID_AUTO, disablefullpath, CTLFLAG_RW,
1757 &disablefullpath, 0, "");
1759 STATNODE(numfullpathcalls);
1760 STATNODE(numfullpathfail1);
1761 STATNODE(numfullpathfail2);
1762 STATNODE(numfullpathfail3);
1763 STATNODE(numfullpathfail4);
1764 STATNODE(numfullpathfound);
1767 vn_fullpath(struct proc *p, struct vnode *vn, char **retbuf, char **freebuf)
1770 int i, slash_prefixed;
1771 struct filedesc *fdp;
1772 struct namecache *ncp;
1775 if (disablefullpath)
1781 /* vn is NULL, client wants us to use p->p_textvp */
1783 if ((vn = p->p_textvp) == NULL)
1786 ncp = TAILQ_FIRST(&vn->v_namecache);
1790 buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1791 bp = buf + MAXPATHLEN - 1;
1795 while (ncp && ncp != fdp->fd_nrdir && (ncp->nc_flag & NCF_ROOT) == 0) {
1796 if (ncp->nc_flag & NCF_MOUNTPT) {
1797 if (ncp->nc_mount == NULL) {
1801 ncp = ncp->nc_parent;
1804 for (i = ncp->nc_nlen - 1; i >= 0; i--) {
1810 *--bp = ncp->nc_name[i];
1819 ncp = ncp->nc_parent;
1826 if (!slash_prefixed) {