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.44 2004/11/21 19:39:35 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(" [MOUNTPT %s]\n", ncp->nc_mount->mnt_stat.f_mntonname);
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.
600 cache_inval(struct namecache *ncp, int flags)
602 struct namecache *kid;
603 struct namecache *nextkid;
605 KKASSERT(ncp->nc_exlocks);
607 cache_setunresolved(ncp);
608 if (flags & CINV_DESTROY)
609 ncp->nc_flag |= NCF_DESTROYED;
611 if ((flags & CINV_CHILDREN) &&
612 (kid = TAILQ_FIRST(&ncp->nc_list)) != NULL
617 if ((nextkid = TAILQ_NEXT(kid, nc_entry)) != NULL)
619 if ((kid->nc_flag & NCF_UNRESOLVED) == 0 ||
620 TAILQ_FIRST(&kid->nc_list)
623 cache_inval(kid, flags & ~CINV_DESTROY);
632 * Someone could have gotten in there while ncp was unlocked,
635 if ((ncp->nc_flag & NCF_UNRESOLVED) == 0)
641 * Invalidate a vnode's namecache associations.
644 cache_inval_vp(struct vnode *vp, int flags)
646 struct namecache *ncp;
648 while ((ncp = TAILQ_FIRST(&vp->v_namecache)) != NULL) {
650 cache_inval(ncp, flags);
656 * The source ncp has been renamed to the target ncp. Both fncp and tncp
657 * must be locked. Both will be set to unresolved, any children of tncp
658 * will be disconnected (the prior contents of the target is assumed to be
659 * destroyed by the rename operation, e.g. renaming over an empty directory),
660 * and all children of fncp will be moved to tncp.
662 * XXX the disconnection could pose a problem, check code paths to make
663 * sure any code that blocks can handle the parent being changed out from
664 * under it. Maybe we should lock the children (watch out for deadlocks) ?
666 * After we return the caller has the option of calling cache_setvp() if
667 * the vnode of the new target ncp is known.
669 * Any process CD'd into any of the children will no longer be able to ".."
670 * back out. An rm -rf can cause this situation to occur.
673 cache_rename(struct namecache *fncp, struct namecache *tncp)
675 struct namecache *scan;
677 cache_setunresolved(fncp);
678 cache_setunresolved(tncp);
679 cache_inval(tncp, CINV_CHILDREN);
680 while ((scan = TAILQ_FIRST(&fncp->nc_list)) != NULL) {
682 cache_unlink_parent(scan);
683 cache_link_parent(scan, tncp);
684 if (scan->nc_flag & NCF_HASHED)
691 * vget the vnode associated with the namecache entry. Resolve the namecache
692 * entry if necessary and deal with namecache/vp races. The passed ncp must
693 * be referenced and may be locked. The ncp's ref/locking state is not
694 * effected by this call.
696 * lk_type may be LK_SHARED, LK_EXCLUSIVE. A ref'd, possibly locked
697 * (depending on the passed lk_type) will be returned in *vpp with an error
698 * of 0, or NULL will be returned in *vpp with a non-0 error code. The
699 * most typical error is ENOENT, meaning that the ncp represents a negative
700 * cache hit and there is no vnode to retrieve, but other errors can occur
703 * The main race we have to deal with are namecache zaps. The ncp itself
704 * will not disappear since it is referenced, and it turns out that the
705 * validity of the vp pointer can be checked simply by rechecking the
706 * contents of ncp->nc_vp.
709 cache_vget(struct namecache *ncp, struct ucred *cred,
710 int lk_type, struct vnode **vpp)
717 if (ncp->nc_flag & NCF_UNRESOLVED) {
719 error = cache_resolve(ncp, cred);
724 if (error == 0 && (vp = ncp->nc_vp) != NULL) {
725 error = vget(vp, lk_type, curthread);
727 if (vp != ncp->nc_vp) /* handle cache_zap race */
730 } else if (vp != ncp->nc_vp) { /* handle cache_zap race */
735 if (error == 0 && vp == NULL)
742 cache_vref(struct namecache *ncp, struct ucred *cred, struct vnode **vpp)
749 if (ncp->nc_flag & NCF_UNRESOLVED) {
751 error = cache_resolve(ncp, cred);
756 if (error == 0 && (vp = ncp->nc_vp) != NULL) {
758 if (vp != ncp->nc_vp) { /* handle cache_zap race */
763 if (error == 0 && vp == NULL)
770 * Convert a directory vnode to a namecache record without any other
771 * knowledge of the topology. This ONLY works with directory vnodes and
772 * is ONLY used by the NFS server. dvp must be refd but unlocked, and the
773 * returned ncp (if not NULL) will be held and unlocked.
775 * If 'makeit' is 0 and dvp has no existing namecache record, NULL is returned.
776 * If 'makeit' is 1 we attempt to track-down and create the namecache topology
777 * for dvp. This will fail only if the directory has been deleted out from
780 * Callers must always check for a NULL return no matter the value of 'makeit'.
783 static int cache_inefficient_scan(struct namecache *ncp, struct ucred *cred,
787 cache_fromdvp(struct vnode *dvp, struct ucred *cred, int makeit)
789 struct namecache *ncp;
794 * Temporary debugging code to force the directory scanning code
798 if (ncvp_debug >= 3 && makeit && TAILQ_FIRST(&dvp->v_namecache)) {
799 ncp = TAILQ_FIRST(&dvp->v_namecache);
800 printf("cache_fromdvp: forcing %s\n", ncp->nc_name);
805 * Loop until resolution, inside code will break out on error.
807 while ((ncp = TAILQ_FIRST(&dvp->v_namecache)) == NULL && makeit) {
810 * If dvp is the root of its filesystem it should already
811 * have a namecache pointer associated with it as a side
812 * effect of the mount, but it may have been disassociated.
814 if (dvp->v_flag & VROOT) {
815 ncp = cache_get(dvp->v_mount->mnt_ncp);
816 error = cache_resolve_mp(ncp);
819 printf("cache_fromdvp: resolve root of mount %p error %d",
820 dvp->v_mount, error);
829 printf(" succeeded\n");
834 * Get the parent directory and resolve its ncp.
836 error = vop_nlookupdotdot(dvp->v_ops, dvp, &pvp, cred);
838 printf("lookupdotdot failed %d %p\n", error, pvp);
841 VOP_UNLOCK(pvp, 0, curthread);
844 * XXX this recursion could run the kernel out of stack,
845 * change to a less efficient algorithm if we get too deep
846 * (use 'makeit' for a depth counter?)
848 ncp = cache_fromdvp(pvp, cred, makeit);
854 * Do an inefficient scan of pvp (embodied by ncp) to look
855 * for dvp. This will create a namecache record for dvp on
856 * success. We loop up to recheck on success.
858 * ncp and dvp are both held but not locked.
860 error = cache_inefficient_scan(ncp, cred, dvp);
863 printf("cache_fromdvp: scan %p (%s) failed on dvp=%p\n",
864 pvp, ncp->nc_name, dvp);
869 printf("cache_fromdvp: scan %p (%s) succeeded\n",
879 * Do an inefficient scan of the directory represented by ncp looking for
880 * the directory vnode dvp. ncp must be held but not locked on entry and
881 * will be held on return. dvp must be refd but not locked on entry and
882 * will remain refd on return.
884 * Why do this at all? Well, due to its stateless nature the NFS server
885 * converts file handles directly to vnodes without necessarily going through
886 * the namecache ops that would otherwise create the namecache topology
887 * leading to the vnode. We could either (1) Change the namecache algorithms
888 * to allow disconnect namecache records that are re-merged opportunistically,
889 * or (2) Make the NFS server backtrack and scan to recover a connected
890 * namecache topology in order to then be able to issue new API lookups.
892 * It turns out that (1) is a huge mess. It takes a nice clean set of
893 * namecache algorithms and introduces a lot of complication in every subsystem
894 * that calls into the namecache to deal with the re-merge case, especially
895 * since we are using the namecache to placehold negative lookups and the
896 * vnode might not be immediately assigned. (2) is certainly far less
897 * efficient then (1), but since we are only talking about directories here
898 * (which are likely to remain cached), the case does not actually run all
899 * that often and has the supreme advantage of not polluting the namecache
903 cache_inefficient_scan(struct namecache *ncp, struct ucred *cred,
906 struct nlcomponent nlc;
907 struct namecache *rncp;
923 vat.va_blocksize = 0;
924 if ((error = VOP_GETATTR(dvp, &vat, curthread)) != 0)
926 if ((error = cache_vget(ncp, cred, LK_SHARED, &pvp)) != 0)
929 printf("inefficient_scan: directory iosize %ld vattr fileid = %ld\n", vat.va_blocksize, (long)vat.va_fileid);
930 if ((blksize = vat.va_blocksize) == 0)
932 rbuf = malloc(blksize, M_TEMP, M_WAITOK);
939 baseoff = uio.uio_offset;
941 iov.iov_len = blksize;
944 uio.uio_resid = blksize;
945 uio.uio_segflg = UIO_SYSSPACE;
946 uio.uio_rw = UIO_READ;
947 uio.uio_td = curthread;
950 free(cookies, M_TEMP);
954 printf("cache_inefficient_scan: readdir @ %08x\n", (int)baseoff);
955 error = VOP_READDIR(pvp, &uio, cred, &eofflag, &ncookies, &cookies);
956 if (error == 0 && cookies == NULL)
959 for (i = 0; i < ncookies; ++i) {
960 xoff = (int)(cookies[i] - (u_long)baseoff);
962 * UFS plays a little trick to skip the first entry
963 * in a directory ("."), by assigning the cookie to
964 * dpoff + dp->d_reclen in the loop. This causes
965 * the last cookie to be assigned to the data-end of
970 KKASSERT(xoff >= 0 && xoff <= blksize);
971 den = (struct dirent *)(rbuf + xoff);
973 printf("cache_inefficient_scan: %*.*s\n",
974 den->d_namlen, den->d_namlen, den->d_name);
975 if (den->d_type != DT_WHT &&
976 den->d_fileno == vat.va_fileid) {
978 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);
979 nlc.nlc_nameptr = den->d_name;
980 nlc.nlc_namelen = den->d_namlen;
981 VOP_UNLOCK(pvp, 0, curthread);
982 rncp = cache_nlookup(ncp, &nlc);
983 KKASSERT(rncp != NULL);
987 if (rncp == NULL && eofflag == 0 && uio.uio_resid != blksize)
991 free(cookies, M_TEMP);
996 if (rncp->nc_flag & NCF_UNRESOLVED) {
997 cache_setvp(rncp, dvp);
998 if (ncvp_debug >= 2) {
999 printf("cache_inefficient_scan: setvp %s/%s = %p\n",
1000 ncp->nc_name, rncp->nc_name, dvp);
1003 if (ncvp_debug >= 2) {
1004 printf("cache_inefficient_scan: setvp %s/%s already set %p/%p\n",
1005 ncp->nc_name, rncp->nc_name, dvp,
1009 if (rncp->nc_vp == NULL)
1010 error = rncp->nc_error;
1013 printf("cache_inefficient_scan: dvp %p NOT FOUND in %s\n",
1023 * Zap a namecache entry. The ncp is unconditionally set to an unresolved
1024 * state, which disassociates it from its vnode or ncneglist.
1026 * Then, if there are no additional references to the ncp and no children,
1027 * the ncp is removed from the topology and destroyed. This function will
1028 * also run through the nc_parent chain and destroy parent ncps if possible.
1029 * As a side benefit, it turns out the only conditions that allow running
1030 * up the chain are also the conditions to ensure no deadlock will occur.
1032 * References and/or children may exist if the ncp is in the middle of the
1033 * topology, preventing the ncp from being destroyed.
1035 * This function must be called with the ncp held and locked and will unlock
1036 * and drop it during zapping.
1039 cache_zap(struct namecache *ncp)
1041 struct namecache *par;
1044 * Disassociate the vnode or negative cache ref and set NCF_UNRESOLVED.
1046 cache_setunresolved(ncp);
1049 * Try to scrap the entry and possibly tail-recurse on its parent.
1050 * We only scrap unref'd (other then our ref) unresolved entries,
1051 * we do not scrap 'live' entries.
1053 while (ncp->nc_flag & NCF_UNRESOLVED) {
1055 * Someone other then us has a ref, stop.
1057 if (ncp->nc_refs > 1)
1061 * We have children, stop.
1063 if (!TAILQ_EMPTY(&ncp->nc_list))
1067 * Remove ncp from the topology: hash table and parent linkage.
1069 if (ncp->nc_flag & NCF_HASHED) {
1070 ncp->nc_flag &= ~NCF_HASHED;
1071 LIST_REMOVE(ncp, nc_hash);
1073 if ((par = ncp->nc_parent) != NULL) {
1074 par = cache_hold(par);
1075 TAILQ_REMOVE(&par->nc_list, ncp, nc_entry);
1076 ncp->nc_parent = NULL;
1077 if (par->nc_vp && TAILQ_EMPTY(&par->nc_list))
1082 * ncp should not have picked up any refs. Physically
1085 KKASSERT(ncp->nc_refs == 1);
1087 /* cache_unlock(ncp) not required */
1088 ncp->nc_refs = -1; /* safety */
1090 free(ncp->nc_name, M_VFSCACHE);
1091 free(ncp, M_VFSCACHE);
1094 * Loop on the parent (it may be NULL). Only bother looping
1095 * if the parent has a single ref (ours), which also means
1096 * we can lock it trivially.
1101 if (ncp->nc_refs != 1) {
1105 KKASSERT(par->nc_exlocks == 0);
1113 static enum { CHI_LOW, CHI_HIGH } cache_hysteresis_state = CHI_LOW;
1117 cache_hysteresis(void)
1120 * Don't cache too many negative hits. We use hysteresis to reduce
1121 * the impact on the critical path.
1123 switch(cache_hysteresis_state) {
1125 if (numneg > MINNEG && numneg * ncnegfactor > numcache) {
1127 cache_hysteresis_state = CHI_HIGH;
1131 if (numneg > MINNEG * 9 / 10 &&
1132 numneg * ncnegfactor * 9 / 10 > numcache
1136 cache_hysteresis_state = CHI_LOW;
1143 * NEW NAMECACHE LOOKUP API
1145 * Lookup an entry in the cache. A locked, referenced, non-NULL
1146 * entry is *always* returned, even if the supplied component is illegal.
1147 * The resulting namecache entry should be returned to the system with
1148 * cache_put() or cache_unlock() + cache_drop().
1150 * namecache locks are recursive but care must be taken to avoid lock order
1153 * Nobody else will be able to manipulate the associated namespace (e.g.
1154 * create, delete, rename, rename-target) until the caller unlocks the
1157 * The returned entry will be in one of three states: positive hit (non-null
1158 * vnode), negative hit (null vnode), or unresolved (NCF_UNRESOLVED is set).
1159 * Unresolved entries must be resolved through the filesystem to associate the
1160 * vnode and/or determine whether a positive or negative hit has occured.
1162 * It is not necessary to lock a directory in order to lock namespace under
1163 * that directory. In fact, it is explicitly not allowed to do that. A
1164 * directory is typically only locked when being created, renamed, or
1167 * The directory (par) may be unresolved, in which case any returned child
1168 * will likely also be marked unresolved. Likely but not guarenteed. Since
1169 * the filesystem lookup requires a resolved directory vnode the caller is
1170 * responsible for resolving the namecache chain top-down. This API
1171 * specifically allows whole chains to be created in an unresolved state.
1174 cache_nlookup(struct namecache *par, struct nlcomponent *nlc)
1176 struct namecache *ncp;
1177 struct namecache *new_ncp;
1178 struct nchashhead *nchpp;
1186 * Try to locate an existing entry
1188 hash = fnv_32_buf(nlc->nlc_nameptr, nlc->nlc_namelen, FNV1_32_INIT);
1189 hash = fnv_32_buf(&par, sizeof(par), hash);
1192 LIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
1196 * Zap entries that have timed out.
1198 if (ncp->nc_timeout &&
1199 (int)(ncp->nc_timeout - ticks) < 0 &&
1200 (ncp->nc_flag & NCF_UNRESOLVED) == 0 &&
1201 ncp->nc_exlocks == 0
1203 cache_zap(cache_get(ncp));
1208 * Break out if we find a matching entry. Note that
1209 * UNRESOLVED entries may match, but DESTROYED entries
1212 if (ncp->nc_parent == par &&
1213 ncp->nc_nlen == nlc->nlc_namelen &&
1214 bcmp(ncp->nc_name, nlc->nlc_nameptr, ncp->nc_nlen) == 0 &&
1215 (ncp->nc_flag & NCF_DESTROYED) == 0
1217 if (cache_get_nonblock(ncp) == 0) {
1219 cache_free(new_ncp);
1229 * We failed to locate an entry, create a new entry and add it to
1230 * the cache. We have to relookup after possibly blocking in
1233 if (new_ncp == NULL) {
1234 new_ncp = cache_alloc(nlc->nlc_namelen);
1241 * Initialize as a new UNRESOLVED entry, lock (non-blocking),
1242 * and link to the parent. The mount point is usually inherited
1243 * from the parent unless this is a special case such as a mount
1244 * point where nlc_namelen is 0. The caller is responsible for
1245 * setting nc_mount in that case. If nlc_namelen is 0 nc_name will
1248 if (nlc->nlc_namelen) {
1249 bcopy(nlc->nlc_nameptr, ncp->nc_name, nlc->nlc_namelen);
1250 ncp->nc_name[nlc->nlc_namelen] = 0;
1251 ncp->nc_mount = par->nc_mount;
1253 nchpp = NCHHASH(hash);
1254 LIST_INSERT_HEAD(nchpp, ncp, nc_hash);
1255 ncp->nc_flag |= NCF_HASHED;
1256 cache_link_parent(ncp, par);
1259 * stats and namecache size management
1261 if (ncp->nc_flag & NCF_UNRESOLVED)
1262 ++gd->gd_nchstats->ncs_miss;
1263 else if (ncp->nc_vp)
1264 ++gd->gd_nchstats->ncs_goodhits;
1266 ++gd->gd_nchstats->ncs_neghits;
1272 * Resolve an unresolved namecache entry, generally by looking it up.
1273 * The passed ncp must be locked and refd.
1275 * Theoretically since a vnode cannot be recycled while held, and since
1276 * the nc_parent chain holds its vnode as long as children exist, the
1277 * direct parent of the cache entry we are trying to resolve should
1278 * have a valid vnode. If not then generate an error that we can
1279 * determine is related to a resolver bug.
1281 * Note that successful resolution does not necessarily return an error
1282 * code of 0. If the ncp resolves to a negative cache hit then ENOENT
1286 cache_resolve(struct namecache *ncp, struct ucred *cred)
1288 struct namecache *par;
1293 * If the ncp is already resolved we have nothing to do.
1295 if ((ncp->nc_flag & NCF_UNRESOLVED) == 0)
1296 return (ncp->nc_error);
1299 * Mount points need special handling because the parent does not
1300 * belong to the same filesystem as the ncp.
1302 if (ncp->nc_flag & NCF_MOUNTPT)
1303 return (cache_resolve_mp(ncp));
1306 * We expect an unbroken chain of ncps to at least the mount point,
1307 * and even all the way to root (but this code doesn't have to go
1308 * past the mount point).
1310 if (ncp->nc_parent == NULL) {
1311 printf("EXDEV case 1 %p %*.*s\n", ncp,
1312 ncp->nc_nlen, ncp->nc_nlen, ncp->nc_name);
1313 ncp->nc_error = EXDEV;
1314 return(ncp->nc_error);
1318 * The vp's of the parent directories in the chain are held via vhold()
1319 * due to the existance of the child, and should not disappear.
1320 * However, there are cases where they can disappear:
1322 * - due to filesystem I/O errors.
1323 * - due to NFS being stupid about tracking the namespace and
1324 * destroys the namespace for entire directories quite often.
1325 * - due to forced unmounts.
1326 * - due to an rmdir (parent will be marked DESTROYED)
1328 * When this occurs we have to track the chain backwards and resolve
1329 * it, looping until the resolver catches up to the current node. We
1330 * could recurse here but we might run ourselves out of kernel stack
1331 * so we do it in a more painful manner. This situation really should
1332 * not occur all that often, or if it does not have to go back too
1333 * many nodes to resolve the ncp.
1335 while (ncp->nc_parent->nc_vp == NULL) {
1337 * This case can occur if a process is CD'd into a
1338 * directory which is then rmdir'd. If the parent is marked
1339 * destroyed there is no point trying to resolve it.
1341 if (ncp->nc_parent->nc_flag & NCF_DESTROYED)
1344 par = ncp->nc_parent;
1345 while (par->nc_parent && par->nc_parent->nc_vp == NULL)
1346 par = par->nc_parent;
1347 if (par->nc_parent == NULL) {
1348 printf("EXDEV case 2 %*.*s\n",
1349 par->nc_nlen, par->nc_nlen, par->nc_name);
1352 printf("[diagnostic] cache_resolve: had to recurse on %*.*s\n",
1353 par->nc_nlen, par->nc_nlen, par->nc_name);
1355 * The parent is not set in stone, ref and lock it to prevent
1356 * it from disappearing. Also note that due to renames it
1357 * is possible for our ncp to move and for par to no longer
1358 * be one of its parents. We resolve it anyway, the loop
1359 * will handle any moves.
1362 if (par->nc_flag & NCF_MOUNTPT) {
1363 cache_resolve_mp(par);
1364 } else if (par->nc_parent->nc_vp == NULL) {
1365 printf("[diagnostic] cache_resolve: raced on %*.*s\n", par->nc_nlen, par->nc_nlen, par->nc_name);
1368 } else if (par->nc_flag & NCF_UNRESOLVED) {
1369 par->nc_error = VOP_NRESOLVE(par, cred);
1371 if ((error = par->nc_error) != 0) {
1372 if (par->nc_error != EAGAIN) {
1373 printf("EXDEV case 3 %*.*s error %d\n",
1374 par->nc_nlen, par->nc_nlen, par->nc_name,
1379 printf("[diagnostic] cache_resolve: EAGAIN par %p %*.*s\n",
1380 par, par->nc_nlen, par->nc_nlen, par->nc_name);
1387 * Call VOP_NRESOLVE() to get the vp, then scan for any disconnected
1388 * ncp's and reattach them. If this occurs the original ncp is marked
1389 * EAGAIN to force a relookup.
1391 * NOTE: in order to call VOP_NRESOLVE(), the parent of the passed
1392 * ncp must already be resolved.
1394 KKASSERT((ncp->nc_flag & NCF_MOUNTPT) == 0);
1395 ncp->nc_error = VOP_NRESOLVE(ncp, cred);
1396 /*vop_nresolve(ncp->nc_parent->nc_vp->v_ops, ncp, cred);*/
1397 if (ncp->nc_error == EAGAIN) {
1398 printf("[diagnostic] cache_resolve: EAGAIN ncp %p %*.*s\n",
1399 ncp, ncp->nc_nlen, ncp->nc_nlen, ncp->nc_name);
1402 return(ncp->nc_error);
1406 * Resolve the ncp associated with a mount point. Such ncp's almost always
1407 * remain resolved and this routine is rarely called. NFS MPs tends to force
1408 * re-resolution more often due to its mac-truck-smash-the-namecache
1409 * method of tracking namespace changes.
1411 * The passed ncp must be locked.
1414 cache_resolve_mp(struct namecache *ncp)
1417 struct mount *mp = ncp->nc_mount;
1419 KKASSERT(mp != NULL);
1420 if (ncp->nc_flag & NCF_UNRESOLVED) {
1421 while (vfs_busy(mp, 0, NULL, curthread))
1423 ncp->nc_error = VFS_ROOT(mp, &vp);
1424 if (ncp->nc_error == 0) {
1425 cache_setvp(ncp, vp);
1428 printf("[diagnostic] cache_resolve_mp: failed to resolve mount %p\n", mp);
1429 cache_setvp(ncp, NULL);
1431 vfs_unbusy(mp, curthread);
1433 return(ncp->nc_error);
1437 cache_cleanneg(int count)
1439 struct namecache *ncp;
1442 * Automode from the vnlru proc - clean out 10% of the negative cache
1446 count = numneg / 10 + 1;
1449 * Attempt to clean out the specified number of negative cache
1453 ncp = TAILQ_FIRST(&ncneglist);
1455 KKASSERT(numneg == 0);
1458 TAILQ_REMOVE(&ncneglist, ncp, nc_vnode);
1459 TAILQ_INSERT_TAIL(&ncneglist, ncp, nc_vnode);
1460 if (cache_get_nonblock(ncp) == 0)
1467 * Rehash a ncp. Rehashing is typically required if the name changes (should
1468 * not generally occur) or the parent link changes. This function will
1469 * unhash the ncp if the ncp is no longer hashable.
1472 cache_rehash(struct namecache *ncp)
1474 struct nchashhead *nchpp;
1477 if (ncp->nc_flag & NCF_HASHED) {
1478 ncp->nc_flag &= ~NCF_HASHED;
1479 LIST_REMOVE(ncp, nc_hash);
1481 if (ncp->nc_nlen && ncp->nc_parent) {
1482 hash = fnv_32_buf(ncp->nc_name, ncp->nc_nlen, FNV1_32_INIT);
1483 hash = fnv_32_buf(&ncp->nc_parent,
1484 sizeof(ncp->nc_parent), hash);
1485 nchpp = NCHHASH(hash);
1486 LIST_INSERT_HEAD(nchpp, ncp, nc_hash);
1487 ncp->nc_flag |= NCF_HASHED;
1492 * Name cache initialization, from vfsinit() when we are booting
1500 /* initialise per-cpu namecache effectiveness statistics. */
1501 for (i = 0; i < ncpus; ++i) {
1502 gd = globaldata_find(i);
1503 gd->gd_nchstats = &nchstats[i];
1505 TAILQ_INIT(&ncneglist);
1506 nchashtbl = hashinit(desiredvnodes*2, M_VFSCACHE, &nchash);
1507 nclockwarn = 1 * hz;
1511 * Called from start_init() to bootstrap the root filesystem. Returns
1512 * a referenced, unlocked namecache record.
1515 cache_allocroot(struct mount *mp, struct vnode *vp)
1517 struct namecache *ncp = cache_alloc(0);
1519 ncp->nc_flag |= NCF_MOUNTPT | NCF_ROOT;
1521 cache_setvp(ncp, vp);
1526 * vfs_cache_setroot()
1528 * Create an association between the root of our namecache and
1529 * the root vnode. This routine may be called several times during
1532 * If the caller intends to save the returned namecache pointer somewhere
1533 * it must cache_hold() it.
1536 vfs_cache_setroot(struct vnode *nvp, struct namecache *ncp)
1539 struct namecache *oncp;
1553 * XXX OLD API COMPAT FUNCTION. This really messes up the new namecache
1554 * topology and is being removed as quickly as possible. The new VOP_N*()
1555 * API calls are required to make specific adjustments using the supplied
1556 * ncp pointers rather then just bogusly purging random vnodes.
1558 * Invalidate all namecache entries to a particular vnode as well as
1559 * any direct children of that vnode in the namecache. This is a
1560 * 'catch all' purge used by filesystems that do not know any better.
1562 * A new vnode v_id is generated. Note that no vnode will ever have a
1565 * Note that the linkage between the vnode and its namecache entries will
1566 * be removed, but the namecache entries themselves might stay put due to
1567 * active references from elsewhere in the system or due to the existance of
1568 * the children. The namecache topology is left intact even if we do not
1569 * know what the vnode association is. Such entries will be marked
1572 * XXX: Only time and the size of v_id prevents this from failing:
1573 * XXX: In theory we should hunt down all (struct vnode*, v_id)
1574 * XXX: soft references and nuke them, at least on the global
1575 * XXX: v_id wraparound. The period of resistance can be extended
1576 * XXX: by incrementing each vnodes v_id individually instead of
1577 * XXX: using the global v_id.
1580 cache_purge(struct vnode *vp)
1582 static u_long nextid;
1584 cache_inval_vp(vp, CINV_DESTROY | CINV_CHILDREN);
1587 * Calculate a new unique id for ".." handling
1591 } while (nextid == vp->v_id || nextid == 0);
1596 * Flush all entries referencing a particular filesystem.
1598 * Since we need to check it anyway, we will flush all the invalid
1599 * entries at the same time.
1602 cache_purgevfs(struct mount *mp)
1604 struct nchashhead *nchpp;
1605 struct namecache *ncp, *nnp;
1608 * Scan hash tables for applicable entries.
1610 for (nchpp = &nchashtbl[nchash]; nchpp >= nchashtbl; nchpp--) {
1611 ncp = LIST_FIRST(nchpp);
1615 nnp = LIST_NEXT(ncp, nc_hash);
1618 if (ncp->nc_mount == mp) {
1629 static int disablecwd;
1630 SYSCTL_INT(_debug, OID_AUTO, disablecwd, CTLFLAG_RW, &disablecwd, 0, "");
1632 static u_long numcwdcalls; STATNODE(CTLFLAG_RD, numcwdcalls, &numcwdcalls);
1633 static u_long numcwdfail1; STATNODE(CTLFLAG_RD, numcwdfail1, &numcwdfail1);
1634 static u_long numcwdfail2; STATNODE(CTLFLAG_RD, numcwdfail2, &numcwdfail2);
1635 static u_long numcwdfail3; STATNODE(CTLFLAG_RD, numcwdfail3, &numcwdfail3);
1636 static u_long numcwdfail4; STATNODE(CTLFLAG_RD, numcwdfail4, &numcwdfail4);
1637 static u_long numcwdfound; STATNODE(CTLFLAG_RD, numcwdfound, &numcwdfound);
1640 __getcwd(struct __getcwd_args *uap)
1650 buflen = uap->buflen;
1653 if (buflen > MAXPATHLEN)
1654 buflen = MAXPATHLEN;
1656 buf = malloc(buflen, M_TEMP, M_WAITOK);
1657 bp = kern_getcwd(buf, buflen, &error);
1659 error = copyout(bp, uap->buf, strlen(bp) + 1);
1665 kern_getcwd(char *buf, size_t buflen, int *error)
1667 struct proc *p = curproc;
1669 int i, slash_prefixed;
1670 struct filedesc *fdp;
1671 struct namecache *ncp;
1680 ncp = fdp->fd_ncdir;
1681 while (ncp && ncp != fdp->fd_nrdir && (ncp->nc_flag & NCF_ROOT) == 0) {
1682 if (ncp->nc_flag & NCF_MOUNTPT) {
1683 if (ncp->nc_mount == NULL) {
1684 *error = EBADF; /* forced unmount? */
1687 ncp = ncp->nc_parent;
1690 for (i = ncp->nc_nlen - 1; i >= 0; i--) {
1696 *--bp = ncp->nc_name[i];
1705 ncp = ncp->nc_parent;
1712 if (!slash_prefixed) {
1726 * Thus begins the fullpath magic.
1730 #define STATNODE(name) \
1731 static u_int name; \
1732 SYSCTL_UINT(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, 0, "")
1734 static int disablefullpath;
1735 SYSCTL_INT(_debug, OID_AUTO, disablefullpath, CTLFLAG_RW,
1736 &disablefullpath, 0, "");
1738 STATNODE(numfullpathcalls);
1739 STATNODE(numfullpathfail1);
1740 STATNODE(numfullpathfail2);
1741 STATNODE(numfullpathfail3);
1742 STATNODE(numfullpathfail4);
1743 STATNODE(numfullpathfound);
1746 vn_fullpath(struct proc *p, struct vnode *vn, char **retbuf, char **freebuf)
1749 int i, slash_prefixed;
1750 struct filedesc *fdp;
1751 struct namecache *ncp;
1754 if (disablefullpath)
1760 /* vn is NULL, client wants us to use p->p_textvp */
1762 if ((vn = p->p_textvp) == NULL)
1765 TAILQ_FOREACH(ncp, &vn->v_namecache, nc_vnode) {
1772 buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1773 bp = buf + MAXPATHLEN - 1;
1777 while (ncp && ncp != fdp->fd_nrdir && (ncp->nc_flag & NCF_ROOT) == 0) {
1778 if (ncp->nc_flag & NCF_MOUNTPT) {
1779 if (ncp->nc_mount == NULL) {
1783 ncp = ncp->nc_parent;
1786 for (i = ncp->nc_nlen - 1; i >= 0; i--) {
1792 *--bp = ncp->nc_name[i];
1801 ncp = ncp->nc_parent;
1808 if (!slash_prefixed) {