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.91 2008/06/14 05:34:06 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>
91 #include <sys/sysref2.h>
93 #define MAX_RECURSION_DEPTH 64
96 * Random lookups in the cache are accomplished with a hash table using
97 * a hash key of (nc_src_vp, name).
99 * Negative entries may exist and correspond to structures where nc_vp
100 * is NULL. In a negative entry, NCF_WHITEOUT will be set if the entry
101 * corresponds to a whited-out directory entry (verses simply not finding the
104 * Upon reaching the last segment of a path, if the reference is for DELETE,
105 * or NOCACHE is set (rewrite), and the name is located in the cache, it
110 * Structures associated with name cacheing.
112 #define NCHHASH(hash) (&nchashtbl[(hash) & nchash])
115 MALLOC_DEFINE(M_VFSCACHE
, "vfscache", "VFS name cache entries");
117 static LIST_HEAD(nchashhead
, namecache
) *nchashtbl
; /* Hash Table */
118 static struct namecache_list ncneglist
; /* instead of vnode */
121 * ncvp_debug - debug cache_fromvp(). This is used by the NFS server
122 * to create the namecache infrastructure leading to a dangling vnode.
124 * 0 Only errors are reported
125 * 1 Successes are reported
126 * 2 Successes + the whole directory scan is reported
127 * 3 Force the directory scan code run as if the parent vnode did not
128 * have a namecache record, even if it does have one.
130 static int ncvp_debug
;
131 SYSCTL_INT(_debug
, OID_AUTO
, ncvp_debug
, CTLFLAG_RW
, &ncvp_debug
, 0, "");
133 static u_long nchash
; /* size of hash table */
134 SYSCTL_ULONG(_debug
, OID_AUTO
, nchash
, CTLFLAG_RD
, &nchash
, 0, "");
136 static u_long ncnegfactor
= 16; /* ratio of negative entries */
137 SYSCTL_ULONG(_debug
, OID_AUTO
, ncnegfactor
, CTLFLAG_RW
, &ncnegfactor
, 0, "");
139 static int nclockwarn
; /* warn on locked entries in ticks */
140 SYSCTL_INT(_debug
, OID_AUTO
, nclockwarn
, CTLFLAG_RW
, &nclockwarn
, 0, "");
142 static u_long numneg
; /* number of cache entries allocated */
143 SYSCTL_ULONG(_debug
, OID_AUTO
, numneg
, CTLFLAG_RD
, &numneg
, 0, "");
145 static u_long numcache
; /* number of cache entries allocated */
146 SYSCTL_ULONG(_debug
, OID_AUTO
, numcache
, CTLFLAG_RD
, &numcache
, 0, "");
148 static u_long numunres
; /* number of unresolved entries */
149 SYSCTL_ULONG(_debug
, OID_AUTO
, numunres
, CTLFLAG_RD
, &numunres
, 0, "");
151 SYSCTL_INT(_debug
, OID_AUTO
, vnsize
, CTLFLAG_RD
, 0, sizeof(struct vnode
), "");
152 SYSCTL_INT(_debug
, OID_AUTO
, ncsize
, CTLFLAG_RD
, 0, sizeof(struct namecache
), "");
154 static int cache_resolve_mp(struct mount
*mp
);
155 static struct vnode
*cache_dvpref(struct namecache
*ncp
);
156 static void _cache_rehash(struct namecache
*ncp
);
157 static void _cache_lock(struct namecache
*ncp
);
158 static void _cache_setunresolved(struct namecache
*ncp
);
161 * The new name cache statistics
163 SYSCTL_NODE(_vfs
, OID_AUTO
, cache
, CTLFLAG_RW
, 0, "Name cache statistics");
164 #define STATNODE(mode, name, var) \
165 SYSCTL_ULONG(_vfs_cache, OID_AUTO, name, mode, var, 0, "");
166 STATNODE(CTLFLAG_RD
, numneg
, &numneg
);
167 STATNODE(CTLFLAG_RD
, numcache
, &numcache
);
168 static u_long numcalls
; STATNODE(CTLFLAG_RD
, numcalls
, &numcalls
);
169 static u_long dothits
; STATNODE(CTLFLAG_RD
, dothits
, &dothits
);
170 static u_long dotdothits
; STATNODE(CTLFLAG_RD
, dotdothits
, &dotdothits
);
171 static u_long numchecks
; STATNODE(CTLFLAG_RD
, numchecks
, &numchecks
);
172 static u_long nummiss
; STATNODE(CTLFLAG_RD
, nummiss
, &nummiss
);
173 static u_long nummisszap
; STATNODE(CTLFLAG_RD
, nummisszap
, &nummisszap
);
174 static u_long numposzaps
; STATNODE(CTLFLAG_RD
, numposzaps
, &numposzaps
);
175 static u_long numposhits
; STATNODE(CTLFLAG_RD
, numposhits
, &numposhits
);
176 static u_long numnegzaps
; STATNODE(CTLFLAG_RD
, numnegzaps
, &numnegzaps
);
177 static u_long numneghits
; STATNODE(CTLFLAG_RD
, numneghits
, &numneghits
);
179 struct nchstats nchstats
[SMP_MAXCPU
];
181 * Export VFS cache effectiveness statistics to user-land.
183 * The statistics are left for aggregation to user-land so
184 * neat things can be achieved, like observing per-CPU cache
188 sysctl_nchstats(SYSCTL_HANDLER_ARGS
)
190 struct globaldata
*gd
;
194 for (i
= 0; i
< ncpus
; ++i
) {
195 gd
= globaldata_find(i
);
196 if ((error
= SYSCTL_OUT(req
, (void *)&(*gd
->gd_nchstats
),
197 sizeof(struct nchstats
))))
203 SYSCTL_PROC(_vfs_cache
, OID_AUTO
, nchstats
, CTLTYPE_OPAQUE
|CTLFLAG_RD
,
204 0, 0, sysctl_nchstats
, "S,nchstats", "VFS cache effectiveness statistics");
206 static void cache_zap(struct namecache
*ncp
);
209 * cache_hold() and cache_drop() prevent the premature deletion of a
210 * namecache entry but do not prevent operations (such as zapping) on
211 * that namecache entry.
213 * This routine may only be called from outside this source module if
214 * nc_refs is already at least 1.
216 * This is a rare case where callers are allowed to hold a spinlock,
217 * so we can't ourselves.
221 _cache_hold(struct namecache
*ncp
)
223 atomic_add_int(&ncp
->nc_refs
, 1);
228 * When dropping an entry, if only one ref remains and the entry has not
229 * been resolved, zap it. Since the one reference is being dropped the
230 * entry had better not be locked.
234 _cache_drop(struct namecache
*ncp
)
236 KKASSERT(ncp
->nc_refs
> 0);
237 if (ncp
->nc_refs
== 1 &&
238 (ncp
->nc_flag
& NCF_UNRESOLVED
) &&
239 TAILQ_EMPTY(&ncp
->nc_list
)
241 KKASSERT(ncp
->nc_exlocks
== 0);
245 atomic_subtract_int(&ncp
->nc_refs
, 1);
250 * Link a new namecache entry to its parent. Be careful to avoid races
251 * if vhold() blocks in the future.
254 cache_link_parent(struct namecache
*ncp
, struct namecache
*par
)
256 KKASSERT(ncp
->nc_parent
== NULL
);
257 ncp
->nc_parent
= par
;
258 if (TAILQ_EMPTY(&par
->nc_list
)) {
259 TAILQ_INSERT_HEAD(&par
->nc_list
, ncp
, nc_entry
);
261 * Any vp associated with an ncp which has children must
262 * be held to prevent it from being recycled.
267 TAILQ_INSERT_HEAD(&par
->nc_list
, ncp
, nc_entry
);
272 * Remove the parent association from a namecache structure. If this is
273 * the last child of the parent the cache_drop(par) will attempt to
274 * recursively zap the parent.
277 cache_unlink_parent(struct namecache
*ncp
)
279 struct namecache
*par
;
281 if ((par
= ncp
->nc_parent
) != NULL
) {
282 ncp
->nc_parent
= NULL
;
283 par
= _cache_hold(par
);
284 TAILQ_REMOVE(&par
->nc_list
, ncp
, nc_entry
);
285 if (par
->nc_vp
&& TAILQ_EMPTY(&par
->nc_list
))
292 * Allocate a new namecache structure. Most of the code does not require
293 * zero-termination of the string but it makes vop_compat_ncreate() easier.
295 static struct namecache
*
296 cache_alloc(int nlen
)
298 struct namecache
*ncp
;
300 ncp
= kmalloc(sizeof(*ncp
), M_VFSCACHE
, M_WAITOK
|M_ZERO
);
302 ncp
->nc_name
= kmalloc(nlen
+ 1, M_VFSCACHE
, M_WAITOK
);
304 ncp
->nc_flag
= NCF_UNRESOLVED
;
305 ncp
->nc_error
= ENOTCONN
; /* needs to be resolved */
309 * Construct a fake FSMID based on the time of day and a 32 bit
310 * roller for uniqueness. This is used to generate a useful
311 * FSMID for filesystems which do not support it.
313 ncp
->nc_fsmid
= cache_getnewfsmid();
314 TAILQ_INIT(&ncp
->nc_list
);
320 _cache_free(struct namecache
*ncp
)
322 KKASSERT(ncp
->nc_refs
== 1 && ncp
->nc_exlocks
== 1);
324 kfree(ncp
->nc_name
, M_VFSCACHE
);
325 kfree(ncp
, M_VFSCACHE
);
329 cache_zero(struct nchandle
*nch
)
336 * Ref and deref a namecache structure.
338 * Warning: caller may hold an unrelated read spinlock, which means we can't
339 * use read spinlocks here.
342 cache_hold(struct nchandle
*nch
)
344 _cache_hold(nch
->ncp
);
345 ++nch
->mount
->mnt_refs
;
350 cache_copy(struct nchandle
*nch
, struct nchandle
*target
)
353 _cache_hold(target
->ncp
);
354 ++nch
->mount
->mnt_refs
;
358 cache_changemount(struct nchandle
*nch
, struct mount
*mp
)
360 --nch
->mount
->mnt_refs
;
362 ++nch
->mount
->mnt_refs
;
366 cache_drop(struct nchandle
*nch
)
368 --nch
->mount
->mnt_refs
;
369 _cache_drop(nch
->ncp
);
375 * Namespace locking. The caller must already hold a reference to the
376 * namecache structure in order to lock/unlock it. This function prevents
377 * the namespace from being created or destroyed by accessors other then
380 * Note that holding a locked namecache structure prevents other threads
381 * from making namespace changes (e.g. deleting or creating), prevents
382 * vnode association state changes by other threads, and prevents the
383 * namecache entry from being resolved or unresolved by other threads.
385 * The lock owner has full authority to associate/disassociate vnodes
386 * and resolve/unresolve the locked ncp.
388 * WARNING! Holding a locked ncp will prevent a vnode from being destroyed
389 * or recycled, but it does NOT help you if the vnode had already initiated
390 * a recyclement. If this is important, use cache_get() rather then
391 * cache_lock() (and deal with the differences in the way the refs counter
392 * is handled). Or, alternatively, make an unconditional call to
393 * cache_validate() or cache_resolve() after cache_lock() returns.
397 _cache_lock(struct namecache
*ncp
)
402 KKASSERT(ncp
->nc_refs
!= 0);
407 if (ncp
->nc_exlocks
== 0) {
411 * The vp associated with a locked ncp must be held
412 * to prevent it from being recycled (which would
413 * cause the ncp to become unresolved).
415 * WARNING! If VRECLAIMED is set the vnode could
416 * already be in the middle of a recycle. Callers
417 * should not assume that nc_vp is usable when
418 * not NULL. cache_vref() or cache_vget() must be
421 * XXX loop on race for later MPSAFE work.
427 if (ncp
->nc_locktd
== td
) {
431 ncp
->nc_flag
|= NCF_LOCKREQ
;
432 if (tsleep(ncp
, 0, "clock", nclockwarn
) == EWOULDBLOCK
) {
436 kprintf("[diagnostic] cache_lock: blocked on %p", ncp
);
437 kprintf(" \"%*.*s\"\n",
438 ncp
->nc_nlen
, ncp
->nc_nlen
, ncp
->nc_name
);
443 kprintf("[diagnostic] cache_lock: unblocked %*.*s\n",
444 ncp
->nc_nlen
, ncp
->nc_nlen
, ncp
->nc_name
);
449 cache_lock(struct nchandle
*nch
)
451 _cache_lock(nch
->ncp
);
456 _cache_lock_nonblock(struct namecache
*ncp
)
460 KKASSERT(ncp
->nc_refs
!= 0);
462 if (ncp
->nc_exlocks
== 0) {
466 * The vp associated with a locked ncp must be held
467 * to prevent it from being recycled (which would
468 * cause the ncp to become unresolved).
470 * WARNING! If VRECLAIMED is set the vnode could
471 * already be in the middle of a recycle. Callers
472 * should not assume that nc_vp is usable when
473 * not NULL. cache_vref() or cache_vget() must be
476 * XXX loop on race for later MPSAFE work.
487 cache_lock_nonblock(struct nchandle
*nch
)
489 return(_cache_lock_nonblock(nch
->ncp
));
494 _cache_unlock(struct namecache
*ncp
)
496 thread_t td
= curthread
;
498 KKASSERT(ncp
->nc_refs
> 0);
499 KKASSERT(ncp
->nc_exlocks
> 0);
500 KKASSERT(ncp
->nc_locktd
== td
);
501 if (--ncp
->nc_exlocks
== 0) {
504 ncp
->nc_locktd
= NULL
;
505 if (ncp
->nc_flag
& NCF_LOCKREQ
) {
506 ncp
->nc_flag
&= ~NCF_LOCKREQ
;
513 cache_unlock(struct nchandle
*nch
)
515 _cache_unlock(nch
->ncp
);
519 * ref-and-lock, unlock-and-deref functions.
521 * This function is primarily used by nlookup. Even though cache_lock
522 * holds the vnode, it is possible that the vnode may have already
523 * initiated a recyclement. We want cache_get() to return a definitively
524 * usable vnode or a definitively unresolved ncp.
528 _cache_get(struct namecache
*ncp
)
532 if (ncp
->nc_vp
&& (ncp
->nc_vp
->v_flag
& VRECLAIMED
))
533 _cache_setunresolved(ncp
);
538 * note: the same nchandle can be passed for both arguments.
541 cache_get(struct nchandle
*nch
, struct nchandle
*target
)
543 target
->mount
= nch
->mount
;
544 target
->ncp
= _cache_get(nch
->ncp
);
545 ++target
->mount
->mnt_refs
;
549 _cache_get_nonblock(struct namecache
*ncp
)
552 if (ncp
->nc_exlocks
== 0 || ncp
->nc_locktd
== curthread
) {
555 if (ncp
->nc_vp
&& (ncp
->nc_vp
->v_flag
& VRECLAIMED
))
556 _cache_setunresolved(ncp
);
563 cache_get_nonblock(struct nchandle
*nch
)
567 if ((error
= _cache_get_nonblock(nch
->ncp
)) == 0)
568 ++nch
->mount
->mnt_refs
;
574 _cache_put(struct namecache
*ncp
)
581 cache_put(struct nchandle
*nch
)
583 --nch
->mount
->mnt_refs
;
584 _cache_put(nch
->ncp
);
590 * Resolve an unresolved ncp by associating a vnode with it. If the
591 * vnode is NULL, a negative cache entry is created.
593 * The ncp should be locked on entry and will remain locked on return.
597 _cache_setvp(struct namecache
*ncp
, struct vnode
*vp
)
599 KKASSERT(ncp
->nc_flag
& NCF_UNRESOLVED
);
603 * Any vp associated with an ncp which has children must
604 * be held. Any vp associated with a locked ncp must be held.
606 if (!TAILQ_EMPTY(&ncp
->nc_list
))
608 TAILQ_INSERT_HEAD(&vp
->v_namecache
, ncp
, nc_vnode
);
613 * Set auxiliary flags
617 ncp
->nc_flag
|= NCF_ISDIR
;
620 ncp
->nc_flag
|= NCF_ISSYMLINK
;
621 /* XXX cache the contents of the symlink */
629 TAILQ_INSERT_TAIL(&ncneglist
, ncp
, nc_vnode
);
631 ncp
->nc_error
= ENOENT
;
633 ncp
->nc_flag
&= ~NCF_UNRESOLVED
;
637 cache_setvp(struct nchandle
*nch
, struct vnode
*vp
)
639 _cache_setvp(nch
->ncp
, vp
);
643 cache_settimeout(struct nchandle
*nch
, int nticks
)
645 struct namecache
*ncp
= nch
->ncp
;
647 if ((ncp
->nc_timeout
= ticks
+ nticks
) == 0)
652 * Disassociate the vnode or negative-cache association and mark a
653 * namecache entry as unresolved again. Note that the ncp is still
654 * left in the hash table and still linked to its parent.
656 * The ncp should be locked and refd on entry and will remain locked and refd
659 * This routine is normally never called on a directory containing children.
660 * However, NFS often does just that in its rename() code as a cop-out to
661 * avoid complex namespace operations. This disconnects a directory vnode
662 * from its namecache and can cause the OLDAPI and NEWAPI to get out of
665 * NOTE: NCF_FSMID must be cleared so a refurbishment of the ncp, such as
666 * in a create, properly propogates flag up the chain.
670 _cache_setunresolved(struct namecache
*ncp
)
674 if ((ncp
->nc_flag
& NCF_UNRESOLVED
) == 0) {
675 ncp
->nc_flag
|= NCF_UNRESOLVED
;
677 ncp
->nc_error
= ENOTCONN
;
679 if ((vp
= ncp
->nc_vp
) != NULL
) {
682 TAILQ_REMOVE(&vp
->v_namecache
, ncp
, nc_vnode
);
685 * Any vp associated with an ncp with children is
686 * held by that ncp. Any vp associated with a locked
687 * ncp is held by that ncp. These conditions must be
688 * undone when the vp is cleared out from the ncp.
690 if (ncp
->nc_flag
& NCF_FSMID
)
692 if (!TAILQ_EMPTY(&ncp
->nc_list
))
697 TAILQ_REMOVE(&ncneglist
, ncp
, nc_vnode
);
700 ncp
->nc_flag
&= ~(NCF_WHITEOUT
|NCF_ISDIR
|NCF_ISSYMLINK
|
706 cache_setunresolved(struct nchandle
*nch
)
708 _cache_setunresolved(nch
->ncp
);
712 * Determine if we can clear NCF_ISMOUNTPT by scanning the mountlist
713 * looking for matches. This flag tells the lookup code when it must
714 * check for a mount linkage and also prevents the directories in question
715 * from being deleted or renamed.
719 cache_clrmountpt_callback(struct mount
*mp
, void *data
)
721 struct nchandle
*nch
= data
;
723 if (mp
->mnt_ncmounton
.ncp
== nch
->ncp
)
725 if (mp
->mnt_ncmountpt
.ncp
== nch
->ncp
)
731 cache_clrmountpt(struct nchandle
*nch
)
735 count
= mountlist_scan(cache_clrmountpt_callback
, nch
,
736 MNTSCAN_FORWARD
|MNTSCAN_NOBUSY
);
738 nch
->ncp
->nc_flag
&= ~NCF_ISMOUNTPT
;
742 * Invalidate portions of the namecache topology given a starting entry.
743 * The passed ncp is set to an unresolved state and:
745 * The passed ncp must be locked.
747 * CINV_DESTROY - Set a flag in the passed ncp entry indicating
748 * that the physical underlying nodes have been
749 * destroyed... as in deleted. For example, when
750 * a directory is removed. This will cause record
751 * lookups on the name to no longer be able to find
752 * the record and tells the resolver to return failure
753 * rather then trying to resolve through the parent.
755 * The topology itself, including ncp->nc_name,
758 * This only applies to the passed ncp, if CINV_CHILDREN
759 * is specified the children are not flagged.
761 * CINV_CHILDREN - Set all children (recursively) to an unresolved
764 * Note that this will also have the side effect of
765 * cleaning out any unreferenced nodes in the topology
766 * from the leaves up as the recursion backs out.
768 * Note that the topology for any referenced nodes remains intact.
770 * It is possible for cache_inval() to race a cache_resolve(), meaning that
771 * the namecache entry may not actually be invalidated on return if it was
772 * revalidated while recursing down into its children. This code guarentees
773 * that the node(s) will go through an invalidation cycle, but does not
774 * guarentee that they will remain in an invalidated state.
776 * Returns non-zero if a revalidation was detected during the invalidation
777 * recursion, zero otherwise. Note that since only the original ncp is
778 * locked the revalidation ultimately can only indicate that the original ncp
779 * *MIGHT* no have been reresolved.
781 * DEEP RECURSION HANDLING - If a recursive invalidation recurses deeply we
782 * have to avoid blowing out the kernel stack. We do this by saving the
783 * deep namecache node and aborting the recursion, then re-recursing at that
784 * node using a depth-first algorithm in order to allow multiple deep
785 * recursions to chain through each other, then we restart the invalidation
790 struct namecache
*resume_ncp
;
794 static int _cache_inval_internal(struct namecache
*, int, struct cinvtrack
*);
798 _cache_inval(struct namecache
*ncp
, int flags
)
800 struct cinvtrack track
;
801 struct namecache
*ncp2
;
805 track
.resume_ncp
= NULL
;
808 r
= _cache_inval_internal(ncp
, flags
, &track
);
809 if (track
.resume_ncp
== NULL
)
811 kprintf("Warning: deep namecache recursion at %s\n",
814 while ((ncp2
= track
.resume_ncp
) != NULL
) {
815 track
.resume_ncp
= NULL
;
817 _cache_inval_internal(ncp2
, flags
& ~CINV_DESTROY
,
827 cache_inval(struct nchandle
*nch
, int flags
)
829 return(_cache_inval(nch
->ncp
, flags
));
833 _cache_inval_internal(struct namecache
*ncp
, int flags
, struct cinvtrack
*track
)
835 struct namecache
*kid
;
836 struct namecache
*nextkid
;
839 KKASSERT(ncp
->nc_exlocks
);
841 _cache_setunresolved(ncp
);
842 if (flags
& CINV_DESTROY
)
843 ncp
->nc_flag
|= NCF_DESTROYED
;
845 if ((flags
& CINV_CHILDREN
) &&
846 (kid
= TAILQ_FIRST(&ncp
->nc_list
)) != NULL
848 if (++track
->depth
> MAX_RECURSION_DEPTH
) {
849 track
->resume_ncp
= ncp
;
856 if (track
->resume_ncp
) {
860 if ((nextkid
= TAILQ_NEXT(kid
, nc_entry
)) != NULL
)
861 _cache_hold(nextkid
);
862 if ((kid
->nc_flag
& NCF_UNRESOLVED
) == 0 ||
863 TAILQ_FIRST(&kid
->nc_list
)
866 rcnt
+= _cache_inval_internal(kid
, flags
& ~CINV_DESTROY
, track
);
877 * Someone could have gotten in there while ncp was unlocked,
880 if ((ncp
->nc_flag
& NCF_UNRESOLVED
) == 0)
886 * Invalidate a vnode's namecache associations. To avoid races against
887 * the resolver we do not invalidate a node which we previously invalidated
888 * but which was then re-resolved while we were in the invalidation loop.
890 * Returns non-zero if any namecache entries remain after the invalidation
893 * NOTE: unlike the namecache topology which guarentees that ncp's will not
894 * be ripped out of the topology while held, the vnode's v_namecache list
895 * has no such restriction. NCP's can be ripped out of the list at virtually
896 * any time if not locked, even if held.
899 cache_inval_vp(struct vnode
*vp
, int flags
)
901 struct namecache
*ncp
;
902 struct namecache
*next
;
905 ncp
= TAILQ_FIRST(&vp
->v_namecache
);
909 /* loop entered with ncp held */
910 if ((next
= TAILQ_NEXT(ncp
, nc_vnode
)) != NULL
)
913 if (ncp
->nc_vp
!= vp
) {
914 kprintf("Warning: cache_inval_vp: race-A detected on "
915 "%s\n", ncp
->nc_name
);
921 _cache_inval(ncp
, flags
);
922 _cache_put(ncp
); /* also releases reference */
924 if (ncp
&& ncp
->nc_vp
!= vp
) {
925 kprintf("Warning: cache_inval_vp: race-B detected on "
926 "%s\n", ncp
->nc_name
);
931 return(TAILQ_FIRST(&vp
->v_namecache
) != NULL
);
935 * This routine is used instead of the normal cache_inval_vp() when we
936 * are trying to recycle otherwise good vnodes.
938 * Return 0 on success, non-zero if not all namecache records could be
939 * disassociated from the vnode (for various reasons).
942 cache_inval_vp_nonblock(struct vnode
*vp
)
944 struct namecache
*ncp
;
945 struct namecache
*next
;
947 ncp
= TAILQ_FIRST(&vp
->v_namecache
);
951 /* loop entered with ncp held */
952 if ((next
= TAILQ_NEXT(ncp
, nc_vnode
)) != NULL
)
954 if (_cache_lock_nonblock(ncp
)) {
960 if (ncp
->nc_vp
!= vp
) {
961 kprintf("Warning: cache_inval_vp: race-A detected on "
962 "%s\n", ncp
->nc_name
);
968 _cache_inval(ncp
, 0);
969 _cache_put(ncp
); /* also releases reference */
971 if (ncp
&& ncp
->nc_vp
!= vp
) {
972 kprintf("Warning: cache_inval_vp: race-B detected on "
973 "%s\n", ncp
->nc_name
);
978 return(TAILQ_FIRST(&vp
->v_namecache
) != NULL
);
982 * The source ncp has been renamed to the target ncp. Both fncp and tncp
983 * must be locked. The target ncp is destroyed (as a normal rename-over
984 * would destroy the target file or directory).
986 * Because there may be references to the source ncp we cannot copy its
987 * contents to the target. Instead the source ncp is relinked as the target
988 * and the target ncp is removed from the namecache topology.
991 cache_rename(struct nchandle
*fnch
, struct nchandle
*tnch
)
993 struct namecache
*fncp
= fnch
->ncp
;
994 struct namecache
*tncp
= tnch
->ncp
;
997 _cache_setunresolved(tncp
);
998 cache_unlink_parent(fncp
);
999 cache_link_parent(fncp
, tncp
->nc_parent
);
1000 cache_unlink_parent(tncp
);
1001 oname
= fncp
->nc_name
;
1002 fncp
->nc_name
= tncp
->nc_name
;
1003 fncp
->nc_nlen
= tncp
->nc_nlen
;
1004 tncp
->nc_name
= NULL
;
1006 if (fncp
->nc_flag
& NCF_HASHED
)
1007 _cache_rehash(fncp
);
1008 if (tncp
->nc_flag
& NCF_HASHED
)
1009 _cache_rehash(tncp
);
1011 kfree(oname
, M_VFSCACHE
);
1015 * vget the vnode associated with the namecache entry. Resolve the namecache
1016 * entry if necessary and deal with namecache/vp races. The passed ncp must
1017 * be referenced and may be locked. The ncp's ref/locking state is not
1018 * effected by this call.
1020 * lk_type may be LK_SHARED, LK_EXCLUSIVE. A ref'd, possibly locked
1021 * (depending on the passed lk_type) will be returned in *vpp with an error
1022 * of 0, or NULL will be returned in *vpp with a non-0 error code. The
1023 * most typical error is ENOENT, meaning that the ncp represents a negative
1024 * cache hit and there is no vnode to retrieve, but other errors can occur
1027 * The main race we have to deal with are namecache zaps. The ncp itself
1028 * will not disappear since it is referenced, and it turns out that the
1029 * validity of the vp pointer can be checked simply by rechecking the
1030 * contents of ncp->nc_vp.
1033 cache_vget(struct nchandle
*nch
, struct ucred
*cred
,
1034 int lk_type
, struct vnode
**vpp
)
1036 struct namecache
*ncp
;
1043 if (ncp
->nc_flag
& NCF_UNRESOLVED
) {
1045 error
= cache_resolve(nch
, cred
);
1050 if (error
== 0 && (vp
= ncp
->nc_vp
) != NULL
) {
1052 * Accessing the vnode from the namecache is a bit
1053 * dangerous. Because there are no refs on the vnode, it
1054 * could be in the middle of a reclaim.
1056 if (vp
->v_flag
& VRECLAIMED
) {
1057 kprintf("Warning: vnode reclaim race detected in cache_vget on %p (%s)\n", vp
, ncp
->nc_name
);
1059 _cache_setunresolved(ncp
);
1063 error
= vget(vp
, lk_type
);
1065 if (vp
!= ncp
->nc_vp
)
1068 } else if (vp
!= ncp
->nc_vp
) {
1071 } else if (vp
->v_flag
& VRECLAIMED
) {
1072 panic("vget succeeded on a VRECLAIMED node! vp %p", vp
);
1075 if (error
== 0 && vp
== NULL
)
1082 cache_vref(struct nchandle
*nch
, struct ucred
*cred
, struct vnode
**vpp
)
1084 struct namecache
*ncp
;
1092 if (ncp
->nc_flag
& NCF_UNRESOLVED
) {
1094 error
= cache_resolve(nch
, cred
);
1099 if (error
== 0 && (vp
= ncp
->nc_vp
) != NULL
) {
1101 * Since we did not obtain any locks, a cache zap
1102 * race can occur here if the vnode is in the middle
1103 * of being reclaimed and has not yet been able to
1104 * clean out its cache node. If that case occurs,
1105 * we must lock and unresolve the cache, then loop
1108 if ((error
= vget(vp
, LK_SHARED
)) != 0) {
1109 if (error
== ENOENT
) {
1110 kprintf("Warning: vnode reclaim race detected on cache_vref %p (%s)\n", vp
, ncp
->nc_name
);
1112 _cache_setunresolved(ncp
);
1118 /* caller does not want a lock */
1122 if (error
== 0 && vp
== NULL
)
1129 * Return a referenced vnode representing the parent directory of
1130 * ncp. Because the caller has locked the ncp it should not be possible for
1131 * the parent ncp to go away.
1133 * However, we might race against the parent dvp and not be able to
1134 * reference it. If we race, return NULL.
1136 static struct vnode
*
1137 cache_dvpref(struct namecache
*ncp
)
1139 struct namecache
*par
;
1143 if ((par
= ncp
->nc_parent
) != NULL
) {
1144 if ((par
->nc_flag
& NCF_UNRESOLVED
) == 0) {
1145 if ((dvp
= par
->nc_vp
) != NULL
) {
1146 if (vget(dvp
, LK_SHARED
) == 0) {
1148 /* return referenced, unlocked dvp */
1159 * Recursively set the FSMID update flag for namecache nodes leading
1160 * to root. This will cause the next getattr or reclaim to increment the
1161 * fsmid and mark the inode for lazy updating.
1163 * Stop recursing when we hit a node whos NCF_FSMID flag is already set.
1164 * This makes FSMIDs work in an Einsteinian fashion - where the observation
1165 * effects the result. In this case a program monitoring a higher level
1166 * node will have detected some prior change and started its scan (clearing
1167 * NCF_FSMID in higher level nodes), but since it has not yet observed the
1168 * node where we find NCF_FSMID still set, we can safely make the related
1169 * modification without interfering with the theorized program.
1171 * This also means that FSMIDs cannot represent time-domain quantities
1172 * in a hierarchical sense. But the main reason for doing it this way
1173 * is to reduce the amount of recursion that occurs in the critical path
1174 * when e.g. a program is writing to a file that sits deep in a directory
1178 cache_update_fsmid(struct nchandle
*nch
)
1180 struct namecache
*ncp
;
1181 struct namecache
*scan
;
1187 * Warning: even if we get a non-NULL vp it could still be in the
1188 * middle of a recyclement. Don't do anything fancy, just set
1191 if ((vp
= ncp
->nc_vp
) != NULL
) {
1192 TAILQ_FOREACH(ncp
, &vp
->v_namecache
, nc_vnode
) {
1193 for (scan
= ncp
; scan
; scan
= scan
->nc_parent
) {
1194 if (scan
->nc_flag
& NCF_FSMID
)
1196 scan
->nc_flag
|= NCF_FSMID
;
1200 while (ncp
&& (ncp
->nc_flag
& NCF_FSMID
) == 0) {
1201 ncp
->nc_flag
|= NCF_FSMID
;
1202 ncp
= ncp
->nc_parent
;
1208 cache_update_fsmid_vp(struct vnode
*vp
)
1210 struct namecache
*ncp
;
1211 struct namecache
*scan
;
1213 TAILQ_FOREACH(ncp
, &vp
->v_namecache
, nc_vnode
) {
1214 for (scan
= ncp
; scan
; scan
= scan
->nc_parent
) {
1215 if (scan
->nc_flag
& NCF_FSMID
)
1217 scan
->nc_flag
|= NCF_FSMID
;
1223 * If getattr is called on a vnode (e.g. a stat call), the filesystem
1224 * may call this routine to determine if the namecache has the hierarchical
1225 * change flag set, requiring the fsmid to be updated.
1227 * Since 0 indicates no support, make sure the filesystem fsmid is at least
1231 cache_check_fsmid_vp(struct vnode
*vp
, int64_t *fsmid
)
1233 struct namecache
*ncp
;
1236 TAILQ_FOREACH(ncp
, &vp
->v_namecache
, nc_vnode
) {
1237 if (ncp
->nc_flag
& NCF_FSMID
) {
1238 ncp
->nc_flag
&= ~NCF_FSMID
;
1250 * Obtain the FSMID for a vnode for filesystems which do not support
1254 cache_sync_fsmid_vp(struct vnode
*vp
)
1256 struct namecache
*ncp
;
1258 if ((ncp
= TAILQ_FIRST(&vp
->v_namecache
)) != NULL
) {
1259 if (ncp
->nc_flag
& NCF_FSMID
) {
1260 ncp
->nc_flag
&= ~NCF_FSMID
;
1263 return(ncp
->nc_fsmid
);
1269 * Convert a directory vnode to a namecache record without any other
1270 * knowledge of the topology. This ONLY works with directory vnodes and
1271 * is ONLY used by the NFS server. dvp must be refd but unlocked, and the
1272 * returned ncp (if not NULL) will be held and unlocked.
1274 * If 'makeit' is 0 and dvp has no existing namecache record, NULL is returned.
1275 * If 'makeit' is 1 we attempt to track-down and create the namecache topology
1276 * for dvp. This will fail only if the directory has been deleted out from
1279 * Callers must always check for a NULL return no matter the value of 'makeit'.
1281 * To avoid underflowing the kernel stack each recursive call increments
1282 * the makeit variable.
1285 static int cache_inefficient_scan(struct nchandle
*nch
, struct ucred
*cred
,
1286 struct vnode
*dvp
, char *fakename
);
1287 static int cache_fromdvp_try(struct vnode
*dvp
, struct ucred
*cred
,
1288 struct vnode
**saved_dvp
);
1291 cache_fromdvp(struct vnode
*dvp
, struct ucred
*cred
, int makeit
,
1292 struct nchandle
*nch
)
1294 struct vnode
*saved_dvp
;
1300 nch
->mount
= dvp
->v_mount
;
1305 * Temporary debugging code to force the directory scanning code
1308 if (ncvp_debug
>= 3 && makeit
&& TAILQ_FIRST(&dvp
->v_namecache
)) {
1309 nch
->ncp
= TAILQ_FIRST(&dvp
->v_namecache
);
1310 kprintf("cache_fromdvp: forcing %s\n", nch
->ncp
->nc_name
);
1315 * Loop until resolution, inside code will break out on error.
1317 while ((nch
->ncp
= TAILQ_FIRST(&dvp
->v_namecache
)) == NULL
&& makeit
) {
1320 * If dvp is the root of its filesystem it should already
1321 * have a namecache pointer associated with it as a side
1322 * effect of the mount, but it may have been disassociated.
1324 if (dvp
->v_flag
& VROOT
) {
1325 nch
->ncp
= _cache_get(nch
->mount
->mnt_ncmountpt
.ncp
);
1326 error
= cache_resolve_mp(nch
->mount
);
1327 _cache_put(nch
->ncp
);
1329 kprintf("cache_fromdvp: resolve root of mount %p error %d",
1330 dvp
->v_mount
, error
);
1334 kprintf(" failed\n");
1339 kprintf(" succeeded\n");
1344 * If we are recursed too deeply resort to an O(n^2)
1345 * algorithm to resolve the namecache topology. The
1346 * resolved pvp is left referenced in saved_dvp to
1347 * prevent the tree from being destroyed while we loop.
1350 error
= cache_fromdvp_try(dvp
, cred
, &saved_dvp
);
1352 kprintf("lookupdotdot(longpath) failed %d "
1353 "dvp %p\n", error
, dvp
);
1361 * Get the parent directory and resolve its ncp.
1364 kfree(fakename
, M_TEMP
);
1367 error
= vop_nlookupdotdot(*dvp
->v_ops
, dvp
, &pvp
, cred
,
1370 kprintf("lookupdotdot failed %d dvp %p\n", error
, dvp
);
1376 * Reuse makeit as a recursion depth counter. On success
1377 * nch will be fully referenced.
1379 cache_fromdvp(pvp
, cred
, makeit
+ 1, nch
);
1381 if (nch
->ncp
== NULL
)
1385 * Do an inefficient scan of pvp (embodied by ncp) to look
1386 * for dvp. This will create a namecache record for dvp on
1387 * success. We loop up to recheck on success.
1389 * ncp and dvp are both held but not locked.
1391 error
= cache_inefficient_scan(nch
, cred
, dvp
, fakename
);
1393 kprintf("cache_fromdvp: scan %p (%s) failed on dvp=%p\n",
1394 pvp
, nch
->ncp
->nc_name
, dvp
);
1396 /* nch was NULLed out, reload mount */
1397 nch
->mount
= dvp
->v_mount
;
1401 kprintf("cache_fromdvp: scan %p (%s) succeeded\n",
1402 pvp
, nch
->ncp
->nc_name
);
1405 /* nch was NULLed out, reload mount */
1406 nch
->mount
= dvp
->v_mount
;
1410 kfree(fakename
, M_TEMP
);
1413 * hold it for real so the mount gets a ref
1425 * Go up the chain of parent directories until we find something
1426 * we can resolve into the namecache. This is very inefficient.
1430 cache_fromdvp_try(struct vnode
*dvp
, struct ucred
*cred
,
1431 struct vnode
**saved_dvp
)
1433 struct nchandle nch
;
1436 static time_t last_fromdvp_report
;
1440 * Loop getting the parent directory vnode until we get something we
1441 * can resolve in the namecache.
1444 nch
.mount
= dvp
->v_mount
;
1450 kfree(fakename
, M_TEMP
);
1453 error
= vop_nlookupdotdot(*dvp
->v_ops
, dvp
, &pvp
, cred
,
1460 if ((nch
.ncp
= TAILQ_FIRST(&pvp
->v_namecache
)) != NULL
) {
1461 _cache_hold(nch
.ncp
);
1465 if (pvp
->v_flag
& VROOT
) {
1466 nch
.ncp
= _cache_get(pvp
->v_mount
->mnt_ncmountpt
.ncp
);
1467 error
= cache_resolve_mp(nch
.mount
);
1468 _cache_unlock(nch
.ncp
);
1471 _cache_drop(nch
.ncp
);
1481 if (last_fromdvp_report
!= time_second
) {
1482 last_fromdvp_report
= time_second
;
1483 kprintf("Warning: extremely inefficient path "
1484 "resolution on %s\n",
1487 error
= cache_inefficient_scan(&nch
, cred
, dvp
, fakename
);
1490 * Hopefully dvp now has a namecache record associated with
1491 * it. Leave it referenced to prevent the kernel from
1492 * recycling the vnode. Otherwise extremely long directory
1493 * paths could result in endless recycling.
1498 _cache_drop(nch
.ncp
);
1501 kfree(fakename
, M_TEMP
);
1506 * Do an inefficient scan of the directory represented by ncp looking for
1507 * the directory vnode dvp. ncp must be held but not locked on entry and
1508 * will be held on return. dvp must be refd but not locked on entry and
1509 * will remain refd on return.
1511 * Why do this at all? Well, due to its stateless nature the NFS server
1512 * converts file handles directly to vnodes without necessarily going through
1513 * the namecache ops that would otherwise create the namecache topology
1514 * leading to the vnode. We could either (1) Change the namecache algorithms
1515 * to allow disconnect namecache records that are re-merged opportunistically,
1516 * or (2) Make the NFS server backtrack and scan to recover a connected
1517 * namecache topology in order to then be able to issue new API lookups.
1519 * It turns out that (1) is a huge mess. It takes a nice clean set of
1520 * namecache algorithms and introduces a lot of complication in every subsystem
1521 * that calls into the namecache to deal with the re-merge case, especially
1522 * since we are using the namecache to placehold negative lookups and the
1523 * vnode might not be immediately assigned. (2) is certainly far less
1524 * efficient then (1), but since we are only talking about directories here
1525 * (which are likely to remain cached), the case does not actually run all
1526 * that often and has the supreme advantage of not polluting the namecache
1529 * If a fakename is supplied just construct a namecache entry using the
1533 cache_inefficient_scan(struct nchandle
*nch
, struct ucred
*cred
,
1534 struct vnode
*dvp
, char *fakename
)
1536 struct nlcomponent nlc
;
1537 struct nchandle rncp
;
1549 vat
.va_blocksize
= 0;
1550 if ((error
= VOP_GETATTR(dvp
, &vat
)) != 0)
1552 if ((error
= cache_vref(nch
, cred
, &pvp
)) != 0)
1555 kprintf("inefficient_scan: directory iosize %ld vattr fileid = %lld\n", vat
.va_blocksize
, vat
.va_fileid
);
1558 * Use the supplied fakename if not NULL. Fake names are typically
1559 * not in the actual filesystem hierarchy. This is used by HAMMER
1560 * to glue @@timestamp recursions together.
1563 nlc
.nlc_nameptr
= fakename
;
1564 nlc
.nlc_namelen
= strlen(fakename
);
1565 rncp
= cache_nlookup(nch
, &nlc
);
1569 if ((blksize
= vat
.va_blocksize
) == 0)
1570 blksize
= DEV_BSIZE
;
1571 rbuf
= kmalloc(blksize
, M_TEMP
, M_WAITOK
);
1577 iov
.iov_base
= rbuf
;
1578 iov
.iov_len
= blksize
;
1581 uio
.uio_resid
= blksize
;
1582 uio
.uio_segflg
= UIO_SYSSPACE
;
1583 uio
.uio_rw
= UIO_READ
;
1584 uio
.uio_td
= curthread
;
1586 if (ncvp_debug
>= 2)
1587 kprintf("cache_inefficient_scan: readdir @ %08x\n", (int)uio
.uio_offset
);
1588 error
= VOP_READDIR(pvp
, &uio
, cred
, &eofflag
, NULL
, NULL
);
1590 den
= (struct dirent
*)rbuf
;
1591 bytes
= blksize
- uio
.uio_resid
;
1594 if (ncvp_debug
>= 2) {
1595 kprintf("cache_inefficient_scan: %*.*s\n",
1596 den
->d_namlen
, den
->d_namlen
,
1599 if (den
->d_type
!= DT_WHT
&&
1600 den
->d_ino
== vat
.va_fileid
) {
1602 kprintf("cache_inefficient_scan: "
1603 "MATCHED inode %lld path %s/%*.*s\n",
1604 vat
.va_fileid
, nch
->ncp
->nc_name
,
1605 den
->d_namlen
, den
->d_namlen
,
1608 nlc
.nlc_nameptr
= den
->d_name
;
1609 nlc
.nlc_namelen
= den
->d_namlen
;
1610 rncp
= cache_nlookup(nch
, &nlc
);
1611 KKASSERT(rncp
.ncp
!= NULL
);
1614 bytes
-= _DIRENT_DIRSIZ(den
);
1615 den
= _DIRENT_NEXT(den
);
1617 if (rncp
.ncp
== NULL
&& eofflag
== 0 && uio
.uio_resid
!= blksize
)
1620 kfree(rbuf
, M_TEMP
);
1624 if (rncp
.ncp
->nc_flag
& NCF_UNRESOLVED
) {
1625 _cache_setvp(rncp
.ncp
, dvp
);
1626 if (ncvp_debug
>= 2) {
1627 kprintf("cache_inefficient_scan: setvp %s/%s = %p\n",
1628 nch
->ncp
->nc_name
, rncp
.ncp
->nc_name
, dvp
);
1631 if (ncvp_debug
>= 2) {
1632 kprintf("cache_inefficient_scan: setvp %s/%s already set %p/%p\n",
1633 nch
->ncp
->nc_name
, rncp
.ncp
->nc_name
, dvp
,
1637 if (rncp
.ncp
->nc_vp
== NULL
)
1638 error
= rncp
.ncp
->nc_error
;
1640 * Release rncp after a successful nlookup. rncp was fully
1645 kprintf("cache_inefficient_scan: dvp %p NOT FOUND in %s\n",
1646 dvp
, nch
->ncp
->nc_name
);
1653 * Zap a namecache entry. The ncp is unconditionally set to an unresolved
1654 * state, which disassociates it from its vnode or ncneglist.
1656 * Then, if there are no additional references to the ncp and no children,
1657 * the ncp is removed from the topology and destroyed. This function will
1658 * also run through the nc_parent chain and destroy parent ncps if possible.
1659 * As a side benefit, it turns out the only conditions that allow running
1660 * up the chain are also the conditions to ensure no deadlock will occur.
1662 * References and/or children may exist if the ncp is in the middle of the
1663 * topology, preventing the ncp from being destroyed.
1665 * This function must be called with the ncp held and locked and will unlock
1666 * and drop it during zapping.
1669 cache_zap(struct namecache
*ncp
)
1671 struct namecache
*par
;
1674 * Disassociate the vnode or negative cache ref and set NCF_UNRESOLVED.
1676 _cache_setunresolved(ncp
);
1679 * Try to scrap the entry and possibly tail-recurse on its parent.
1680 * We only scrap unref'd (other then our ref) unresolved entries,
1681 * we do not scrap 'live' entries.
1683 while (ncp
->nc_flag
& NCF_UNRESOLVED
) {
1685 * Someone other then us has a ref, stop.
1687 if (ncp
->nc_refs
> 1)
1691 * We have children, stop.
1693 if (!TAILQ_EMPTY(&ncp
->nc_list
))
1697 * Remove ncp from the topology: hash table and parent linkage.
1699 if (ncp
->nc_flag
& NCF_HASHED
) {
1700 ncp
->nc_flag
&= ~NCF_HASHED
;
1701 LIST_REMOVE(ncp
, nc_hash
);
1703 if ((par
= ncp
->nc_parent
) != NULL
) {
1704 par
= _cache_hold(par
);
1705 TAILQ_REMOVE(&par
->nc_list
, ncp
, nc_entry
);
1706 ncp
->nc_parent
= NULL
;
1707 if (par
->nc_vp
&& TAILQ_EMPTY(&par
->nc_list
))
1712 * ncp should not have picked up any refs. Physically
1715 KKASSERT(ncp
->nc_refs
== 1);
1717 /* _cache_unlock(ncp) not required */
1718 ncp
->nc_refs
= -1; /* safety */
1720 kfree(ncp
->nc_name
, M_VFSCACHE
);
1721 kfree(ncp
, M_VFSCACHE
);
1724 * Loop on the parent (it may be NULL). Only bother looping
1725 * if the parent has a single ref (ours), which also means
1726 * we can lock it trivially.
1731 if (ncp
->nc_refs
!= 1) {
1735 KKASSERT(par
->nc_exlocks
== 0);
1740 atomic_subtract_int(&ncp
->nc_refs
, 1);
1743 static enum { CHI_LOW
, CHI_HIGH
} cache_hysteresis_state
= CHI_LOW
;
1747 cache_hysteresis(void)
1750 * Don't cache too many negative hits. We use hysteresis to reduce
1751 * the impact on the critical path.
1753 switch(cache_hysteresis_state
) {
1755 if (numneg
> MINNEG
&& numneg
* ncnegfactor
> numcache
) {
1757 cache_hysteresis_state
= CHI_HIGH
;
1761 if (numneg
> MINNEG
* 9 / 10 &&
1762 numneg
* ncnegfactor
* 9 / 10 > numcache
1766 cache_hysteresis_state
= CHI_LOW
;
1773 * NEW NAMECACHE LOOKUP API
1775 * Lookup an entry in the cache. A locked, referenced, non-NULL
1776 * entry is *always* returned, even if the supplied component is illegal.
1777 * The resulting namecache entry should be returned to the system with
1778 * cache_put() or _cache_unlock() + cache_drop().
1780 * namecache locks are recursive but care must be taken to avoid lock order
1783 * Nobody else will be able to manipulate the associated namespace (e.g.
1784 * create, delete, rename, rename-target) until the caller unlocks the
1787 * The returned entry will be in one of three states: positive hit (non-null
1788 * vnode), negative hit (null vnode), or unresolved (NCF_UNRESOLVED is set).
1789 * Unresolved entries must be resolved through the filesystem to associate the
1790 * vnode and/or determine whether a positive or negative hit has occured.
1792 * It is not necessary to lock a directory in order to lock namespace under
1793 * that directory. In fact, it is explicitly not allowed to do that. A
1794 * directory is typically only locked when being created, renamed, or
1797 * The directory (par) may be unresolved, in which case any returned child
1798 * will likely also be marked unresolved. Likely but not guarenteed. Since
1799 * the filesystem lookup requires a resolved directory vnode the caller is
1800 * responsible for resolving the namecache chain top-down. This API
1801 * specifically allows whole chains to be created in an unresolved state.
1804 cache_nlookup(struct nchandle
*par_nch
, struct nlcomponent
*nlc
)
1806 struct nchandle nch
;
1807 struct namecache
*ncp
;
1808 struct namecache
*new_ncp
;
1809 struct nchashhead
*nchpp
;
1817 * Try to locate an existing entry
1819 hash
= fnv_32_buf(nlc
->nlc_nameptr
, nlc
->nlc_namelen
, FNV1_32_INIT
);
1820 hash
= fnv_32_buf(&par_nch
->ncp
, sizeof(par_nch
->ncp
), hash
);
1823 LIST_FOREACH(ncp
, (NCHHASH(hash
)), nc_hash
) {
1827 * Try to zap entries that have timed out. We have
1828 * to be careful here because locked leafs may depend
1829 * on the vnode remaining intact in a parent, so only
1830 * do this under very specific conditions.
1832 if (ncp
->nc_timeout
&&
1833 (int)(ncp
->nc_timeout
- ticks
) < 0 &&
1834 (ncp
->nc_flag
& NCF_UNRESOLVED
) == 0 &&
1835 ncp
->nc_exlocks
== 0 &&
1836 TAILQ_EMPTY(&ncp
->nc_list
)
1838 cache_zap(_cache_get(ncp
));
1843 * Break out if we find a matching entry. Note that
1844 * UNRESOLVED entries may match, but DESTROYED entries
1847 if (ncp
->nc_parent
== par_nch
->ncp
&&
1848 ncp
->nc_nlen
== nlc
->nlc_namelen
&&
1849 bcmp(ncp
->nc_name
, nlc
->nlc_nameptr
, ncp
->nc_nlen
) == 0 &&
1850 (ncp
->nc_flag
& NCF_DESTROYED
) == 0
1852 if (_cache_get_nonblock(ncp
) == 0) {
1854 _cache_free(new_ncp
);
1864 * We failed to locate an entry, create a new entry and add it to
1865 * the cache. We have to relookup after possibly blocking in
1868 if (new_ncp
== NULL
) {
1869 new_ncp
= cache_alloc(nlc
->nlc_namelen
);
1876 * Initialize as a new UNRESOLVED entry, lock (non-blocking),
1877 * and link to the parent. The mount point is usually inherited
1878 * from the parent unless this is a special case such as a mount
1879 * point where nlc_namelen is 0. If nlc_namelen is 0 nc_name will
1882 if (nlc
->nlc_namelen
) {
1883 bcopy(nlc
->nlc_nameptr
, ncp
->nc_name
, nlc
->nlc_namelen
);
1884 ncp
->nc_name
[nlc
->nlc_namelen
] = 0;
1886 nchpp
= NCHHASH(hash
);
1887 LIST_INSERT_HEAD(nchpp
, ncp
, nc_hash
);
1888 ncp
->nc_flag
|= NCF_HASHED
;
1889 cache_link_parent(ncp
, par_nch
->ncp
);
1892 * stats and namecache size management
1894 if (ncp
->nc_flag
& NCF_UNRESOLVED
)
1895 ++gd
->gd_nchstats
->ncs_miss
;
1896 else if (ncp
->nc_vp
)
1897 ++gd
->gd_nchstats
->ncs_goodhits
;
1899 ++gd
->gd_nchstats
->ncs_neghits
;
1901 nch
.mount
= par_nch
->mount
;
1903 ++nch
.mount
->mnt_refs
;
1908 * The namecache entry is marked as being used as a mount point.
1909 * Locate the mount if it is visible to the caller.
1911 struct findmount_info
{
1912 struct mount
*result
;
1913 struct mount
*nch_mount
;
1914 struct namecache
*nch_ncp
;
1919 cache_findmount_callback(struct mount
*mp
, void *data
)
1921 struct findmount_info
*info
= data
;
1924 * Check the mount's mounted-on point against the passed nch.
1926 if (mp
->mnt_ncmounton
.mount
== info
->nch_mount
&&
1927 mp
->mnt_ncmounton
.ncp
== info
->nch_ncp
1936 cache_findmount(struct nchandle
*nch
)
1938 struct findmount_info info
;
1941 info
.nch_mount
= nch
->mount
;
1942 info
.nch_ncp
= nch
->ncp
;
1943 mountlist_scan(cache_findmount_callback
, &info
,
1944 MNTSCAN_FORWARD
|MNTSCAN_NOBUSY
);
1945 return(info
.result
);
1949 * Resolve an unresolved namecache entry, generally by looking it up.
1950 * The passed ncp must be locked and refd.
1952 * Theoretically since a vnode cannot be recycled while held, and since
1953 * the nc_parent chain holds its vnode as long as children exist, the
1954 * direct parent of the cache entry we are trying to resolve should
1955 * have a valid vnode. If not then generate an error that we can
1956 * determine is related to a resolver bug.
1958 * However, if a vnode was in the middle of a recyclement when the NCP
1959 * got locked, ncp->nc_vp might point to a vnode that is about to become
1960 * invalid. cache_resolve() handles this case by unresolving the entry
1961 * and then re-resolving it.
1963 * Note that successful resolution does not necessarily return an error
1964 * code of 0. If the ncp resolves to a negative cache hit then ENOENT
1968 cache_resolve(struct nchandle
*nch
, struct ucred
*cred
)
1970 struct namecache
*par
;
1971 struct namecache
*ncp
;
1972 struct nchandle nctmp
;
1981 * If the ncp is already resolved we have nothing to do. However,
1982 * we do want to guarentee that a usable vnode is returned when
1983 * a vnode is present, so make sure it hasn't been reclaimed.
1985 if ((ncp
->nc_flag
& NCF_UNRESOLVED
) == 0) {
1986 if (ncp
->nc_vp
&& (ncp
->nc_vp
->v_flag
& VRECLAIMED
))
1987 _cache_setunresolved(ncp
);
1988 if ((ncp
->nc_flag
& NCF_UNRESOLVED
) == 0)
1989 return (ncp
->nc_error
);
1993 * Mount points need special handling because the parent does not
1994 * belong to the same filesystem as the ncp.
1996 if (ncp
== mp
->mnt_ncmountpt
.ncp
)
1997 return (cache_resolve_mp(mp
));
2000 * We expect an unbroken chain of ncps to at least the mount point,
2001 * and even all the way to root (but this code doesn't have to go
2002 * past the mount point).
2004 if (ncp
->nc_parent
== NULL
) {
2005 kprintf("EXDEV case 1 %p %*.*s\n", ncp
,
2006 ncp
->nc_nlen
, ncp
->nc_nlen
, ncp
->nc_name
);
2007 ncp
->nc_error
= EXDEV
;
2008 return(ncp
->nc_error
);
2012 * The vp's of the parent directories in the chain are held via vhold()
2013 * due to the existance of the child, and should not disappear.
2014 * However, there are cases where they can disappear:
2016 * - due to filesystem I/O errors.
2017 * - due to NFS being stupid about tracking the namespace and
2018 * destroys the namespace for entire directories quite often.
2019 * - due to forced unmounts.
2020 * - due to an rmdir (parent will be marked DESTROYED)
2022 * When this occurs we have to track the chain backwards and resolve
2023 * it, looping until the resolver catches up to the current node. We
2024 * could recurse here but we might run ourselves out of kernel stack
2025 * so we do it in a more painful manner. This situation really should
2026 * not occur all that often, or if it does not have to go back too
2027 * many nodes to resolve the ncp.
2029 while ((dvp
= cache_dvpref(ncp
)) == NULL
) {
2031 * This case can occur if a process is CD'd into a
2032 * directory which is then rmdir'd. If the parent is marked
2033 * destroyed there is no point trying to resolve it.
2035 if (ncp
->nc_parent
->nc_flag
& NCF_DESTROYED
)
2038 par
= ncp
->nc_parent
;
2039 while (par
->nc_parent
&& par
->nc_parent
->nc_vp
== NULL
)
2040 par
= par
->nc_parent
;
2041 if (par
->nc_parent
== NULL
) {
2042 kprintf("EXDEV case 2 %*.*s\n",
2043 par
->nc_nlen
, par
->nc_nlen
, par
->nc_name
);
2046 kprintf("[diagnostic] cache_resolve: had to recurse on %*.*s\n",
2047 par
->nc_nlen
, par
->nc_nlen
, par
->nc_name
);
2049 * The parent is not set in stone, ref and lock it to prevent
2050 * it from disappearing. Also note that due to renames it
2051 * is possible for our ncp to move and for par to no longer
2052 * be one of its parents. We resolve it anyway, the loop
2053 * will handle any moves.
2056 if (par
== nch
->mount
->mnt_ncmountpt
.ncp
) {
2057 cache_resolve_mp(nch
->mount
);
2058 } else if ((dvp
= cache_dvpref(par
)) == NULL
) {
2059 kprintf("[diagnostic] cache_resolve: raced on %*.*s\n", par
->nc_nlen
, par
->nc_nlen
, par
->nc_name
);
2063 if (par
->nc_flag
& NCF_UNRESOLVED
) {
2066 par
->nc_error
= VOP_NRESOLVE(&nctmp
, dvp
, cred
);
2070 if ((error
= par
->nc_error
) != 0) {
2071 if (par
->nc_error
!= EAGAIN
) {
2072 kprintf("EXDEV case 3 %*.*s error %d\n",
2073 par
->nc_nlen
, par
->nc_nlen
, par
->nc_name
,
2078 kprintf("[diagnostic] cache_resolve: EAGAIN par %p %*.*s\n",
2079 par
, par
->nc_nlen
, par
->nc_nlen
, par
->nc_name
);
2086 * Call VOP_NRESOLVE() to get the vp, then scan for any disconnected
2087 * ncp's and reattach them. If this occurs the original ncp is marked
2088 * EAGAIN to force a relookup.
2090 * NOTE: in order to call VOP_NRESOLVE(), the parent of the passed
2091 * ncp must already be resolved.
2096 ncp
->nc_error
= VOP_NRESOLVE(&nctmp
, dvp
, cred
);
2099 ncp
->nc_error
= EPERM
;
2101 if (ncp
->nc_error
== EAGAIN
) {
2102 kprintf("[diagnostic] cache_resolve: EAGAIN ncp %p %*.*s\n",
2103 ncp
, ncp
->nc_nlen
, ncp
->nc_nlen
, ncp
->nc_name
);
2106 return(ncp
->nc_error
);
2110 * Resolve the ncp associated with a mount point. Such ncp's almost always
2111 * remain resolved and this routine is rarely called. NFS MPs tends to force
2112 * re-resolution more often due to its mac-truck-smash-the-namecache
2113 * method of tracking namespace changes.
2115 * The semantics for this call is that the passed ncp must be locked on
2116 * entry and will be locked on return. However, if we actually have to
2117 * resolve the mount point we temporarily unlock the entry in order to
2118 * avoid race-to-root deadlocks due to e.g. dead NFS mounts. Because of
2119 * the unlock we have to recheck the flags after we relock.
2122 cache_resolve_mp(struct mount
*mp
)
2124 struct namecache
*ncp
= mp
->mnt_ncmountpt
.ncp
;
2128 KKASSERT(mp
!= NULL
);
2131 * If the ncp is already resolved we have nothing to do. However,
2132 * we do want to guarentee that a usable vnode is returned when
2133 * a vnode is present, so make sure it hasn't been reclaimed.
2135 if ((ncp
->nc_flag
& NCF_UNRESOLVED
) == 0) {
2136 if (ncp
->nc_vp
&& (ncp
->nc_vp
->v_flag
& VRECLAIMED
))
2137 _cache_setunresolved(ncp
);
2140 if (ncp
->nc_flag
& NCF_UNRESOLVED
) {
2142 while (vfs_busy(mp
, 0))
2144 error
= VFS_ROOT(mp
, &vp
);
2148 * recheck the ncp state after relocking.
2150 if (ncp
->nc_flag
& NCF_UNRESOLVED
) {
2151 ncp
->nc_error
= error
;
2153 _cache_setvp(ncp
, vp
);
2156 kprintf("[diagnostic] cache_resolve_mp: failed"
2157 " to resolve mount %p err=%d ncp=%p\n",
2159 _cache_setvp(ncp
, NULL
);
2161 } else if (error
== 0) {
2166 return(ncp
->nc_error
);
2170 cache_cleanneg(int count
)
2172 struct namecache
*ncp
;
2175 * Automode from the vnlru proc - clean out 10% of the negative cache
2179 count
= numneg
/ 10 + 1;
2182 * Attempt to clean out the specified number of negative cache
2186 ncp
= TAILQ_FIRST(&ncneglist
);
2188 KKASSERT(numneg
== 0);
2191 TAILQ_REMOVE(&ncneglist
, ncp
, nc_vnode
);
2192 TAILQ_INSERT_TAIL(&ncneglist
, ncp
, nc_vnode
);
2193 if (_cache_get_nonblock(ncp
) == 0)
2200 * Rehash a ncp. Rehashing is typically required if the name changes (should
2201 * not generally occur) or the parent link changes. This function will
2202 * unhash the ncp if the ncp is no longer hashable.
2205 _cache_rehash(struct namecache
*ncp
)
2207 struct nchashhead
*nchpp
;
2210 if (ncp
->nc_flag
& NCF_HASHED
) {
2211 ncp
->nc_flag
&= ~NCF_HASHED
;
2212 LIST_REMOVE(ncp
, nc_hash
);
2214 if (ncp
->nc_nlen
&& ncp
->nc_parent
) {
2215 hash
= fnv_32_buf(ncp
->nc_name
, ncp
->nc_nlen
, FNV1_32_INIT
);
2216 hash
= fnv_32_buf(&ncp
->nc_parent
,
2217 sizeof(ncp
->nc_parent
), hash
);
2218 nchpp
= NCHHASH(hash
);
2219 LIST_INSERT_HEAD(nchpp
, ncp
, nc_hash
);
2220 ncp
->nc_flag
|= NCF_HASHED
;
2225 * Name cache initialization, from vfsinit() when we are booting
2233 /* initialise per-cpu namecache effectiveness statistics. */
2234 for (i
= 0; i
< ncpus
; ++i
) {
2235 gd
= globaldata_find(i
);
2236 gd
->gd_nchstats
= &nchstats
[i
];
2238 TAILQ_INIT(&ncneglist
);
2239 nchashtbl
= hashinit(desiredvnodes
*2, M_VFSCACHE
, &nchash
);
2240 nclockwarn
= 5 * hz
;
2244 * Called from start_init() to bootstrap the root filesystem. Returns
2245 * a referenced, unlocked namecache record.
2248 cache_allocroot(struct nchandle
*nch
, struct mount
*mp
, struct vnode
*vp
)
2250 nch
->ncp
= cache_alloc(0);
2254 _cache_setvp(nch
->ncp
, vp
);
2258 * vfs_cache_setroot()
2260 * Create an association between the root of our namecache and
2261 * the root vnode. This routine may be called several times during
2264 * If the caller intends to save the returned namecache pointer somewhere
2265 * it must cache_hold() it.
2268 vfs_cache_setroot(struct vnode
*nvp
, struct nchandle
*nch
)
2271 struct nchandle onch
;
2279 cache_zero(&rootnch
);
2287 * XXX OLD API COMPAT FUNCTION. This really messes up the new namecache
2288 * topology and is being removed as quickly as possible. The new VOP_N*()
2289 * API calls are required to make specific adjustments using the supplied
2290 * ncp pointers rather then just bogusly purging random vnodes.
2292 * Invalidate all namecache entries to a particular vnode as well as
2293 * any direct children of that vnode in the namecache. This is a
2294 * 'catch all' purge used by filesystems that do not know any better.
2296 * Note that the linkage between the vnode and its namecache entries will
2297 * be removed, but the namecache entries themselves might stay put due to
2298 * active references from elsewhere in the system or due to the existance of
2299 * the children. The namecache topology is left intact even if we do not
2300 * know what the vnode association is. Such entries will be marked
2304 cache_purge(struct vnode
*vp
)
2306 cache_inval_vp(vp
, CINV_DESTROY
| CINV_CHILDREN
);
2310 * Flush all entries referencing a particular filesystem.
2312 * Since we need to check it anyway, we will flush all the invalid
2313 * entries at the same time.
2318 cache_purgevfs(struct mount
*mp
)
2320 struct nchashhead
*nchpp
;
2321 struct namecache
*ncp
, *nnp
;
2324 * Scan hash tables for applicable entries.
2326 for (nchpp
= &nchashtbl
[nchash
]; nchpp
>= nchashtbl
; nchpp
--) {
2327 ncp
= LIST_FIRST(nchpp
);
2331 nnp
= LIST_NEXT(ncp
, nc_hash
);
2334 if (ncp
->nc_mount
== mp
) {
2348 * Create a new (theoretically) unique fsmid
2351 cache_getnewfsmid(void)
2353 static int fsmid_roller
;
2357 fsmid
= ((int64_t)time_second
<< 32) |
2358 (fsmid_roller
& 0x7FFFFFFF);
2363 static int disablecwd
;
2364 SYSCTL_INT(_debug
, OID_AUTO
, disablecwd
, CTLFLAG_RW
, &disablecwd
, 0, "");
2366 static u_long numcwdcalls
; STATNODE(CTLFLAG_RD
, numcwdcalls
, &numcwdcalls
);
2367 static u_long numcwdfail1
; STATNODE(CTLFLAG_RD
, numcwdfail1
, &numcwdfail1
);
2368 static u_long numcwdfail2
; STATNODE(CTLFLAG_RD
, numcwdfail2
, &numcwdfail2
);
2369 static u_long numcwdfail3
; STATNODE(CTLFLAG_RD
, numcwdfail3
, &numcwdfail3
);
2370 static u_long numcwdfail4
; STATNODE(CTLFLAG_RD
, numcwdfail4
, &numcwdfail4
);
2371 static u_long numcwdfound
; STATNODE(CTLFLAG_RD
, numcwdfound
, &numcwdfound
);
2374 sys___getcwd(struct __getcwd_args
*uap
)
2384 buflen
= uap
->buflen
;
2387 if (buflen
> MAXPATHLEN
)
2388 buflen
= MAXPATHLEN
;
2390 buf
= kmalloc(buflen
, M_TEMP
, M_WAITOK
);
2391 bp
= kern_getcwd(buf
, buflen
, &error
);
2393 error
= copyout(bp
, uap
->buf
, strlen(bp
) + 1);
2399 kern_getcwd(char *buf
, size_t buflen
, int *error
)
2401 struct proc
*p
= curproc
;
2403 int i
, slash_prefixed
;
2404 struct filedesc
*fdp
;
2405 struct nchandle nch
;
2414 nch
= fdp
->fd_ncdir
;
2415 while (nch
.ncp
&& (nch
.ncp
!= fdp
->fd_nrdir
.ncp
||
2416 nch
.mount
!= fdp
->fd_nrdir
.mount
)
2419 * While traversing upwards if we encounter the root
2420 * of the current mount we have to skip to the mount point
2421 * in the underlying filesystem.
2423 if (nch
.ncp
== nch
.mount
->mnt_ncmountpt
.ncp
) {
2424 nch
= nch
.mount
->mnt_ncmounton
;
2429 * Prepend the path segment
2431 for (i
= nch
.ncp
->nc_nlen
- 1; i
>= 0; i
--) {
2437 *--bp
= nch
.ncp
->nc_name
[i
];
2448 * Go up a directory. This isn't a mount point so we don't
2449 * have to check again.
2451 nch
.ncp
= nch
.ncp
->nc_parent
;
2453 if (nch
.ncp
== NULL
) {
2458 if (!slash_prefixed
) {
2472 * Thus begins the fullpath magic.
2476 #define STATNODE(name) \
2477 static u_int name; \
2478 SYSCTL_UINT(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, 0, "")
2480 static int disablefullpath
;
2481 SYSCTL_INT(_debug
, OID_AUTO
, disablefullpath
, CTLFLAG_RW
,
2482 &disablefullpath
, 0, "");
2484 STATNODE(numfullpathcalls
);
2485 STATNODE(numfullpathfail1
);
2486 STATNODE(numfullpathfail2
);
2487 STATNODE(numfullpathfail3
);
2488 STATNODE(numfullpathfail4
);
2489 STATNODE(numfullpathfound
);
2492 cache_fullpath(struct proc
*p
, struct nchandle
*nchp
, char **retbuf
, char **freebuf
)
2495 int i
, slash_prefixed
;
2496 struct nchandle fd_nrdir
;
2497 struct nchandle nch
;
2504 buf
= kmalloc(MAXPATHLEN
, M_TEMP
, M_WAITOK
);
2505 bp
= buf
+ MAXPATHLEN
- 1;
2508 fd_nrdir
= p
->p_fd
->fd_nrdir
;
2515 (nch
.ncp
!= fd_nrdir
.ncp
|| nch
.mount
!= fd_nrdir
.mount
)
2518 * While traversing upwards if we encounter the root
2519 * of the current mount we have to skip to the mount point.
2521 if (nch
.ncp
== nch
.mount
->mnt_ncmountpt
.ncp
) {
2522 nch
= nch
.mount
->mnt_ncmounton
;
2527 * Prepend the path segment
2529 for (i
= nch
.ncp
->nc_nlen
- 1; i
>= 0; i
--) {
2535 *--bp
= nch
.ncp
->nc_name
[i
];
2546 * Go up a directory. This isn't a mount point so we don't
2547 * have to check again.
2549 nch
.ncp
= nch
.ncp
->nc_parent
;
2551 if (nch
.ncp
== NULL
) {
2557 if (!slash_prefixed
) {
2573 vn_fullpath(struct proc
*p
, struct vnode
*vn
, char **retbuf
, char **freebuf
)
2575 struct namecache
*ncp
;
2576 struct nchandle nch
;
2579 if (disablefullpath
)
2585 /* vn is NULL, client wants us to use p->p_textvp */
2587 if ((vn
= p
->p_textvp
) == NULL
)
2590 TAILQ_FOREACH(ncp
, &vn
->v_namecache
, nc_vnode
) {
2599 nch
.mount
= vn
->v_mount
;
2600 return(cache_fullpath(p
, &nch
, retbuf
, freebuf
));