2 * Copyright (c) 2003,2004,2009 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. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 #include <sys/param.h>
66 #include <sys/systm.h>
67 #include <sys/kernel.h>
68 #include <sys/sysctl.h>
69 #include <sys/mount.h>
70 #include <sys/vnode.h>
71 #include <sys/malloc.h>
72 #include <sys/sysproto.h>
73 #include <sys/spinlock.h>
75 #include <sys/namei.h>
76 #include <sys/nlookup.h>
77 #include <sys/filedesc.h>
78 #include <sys/fnv_hash.h>
79 #include <sys/globaldata.h>
80 #include <sys/kern_syscall.h>
81 #include <sys/dirent.h>
84 #include <sys/spinlock2.h>
86 #define MAX_RECURSION_DEPTH 64
89 * Random lookups in the cache are accomplished with a hash table using
90 * a hash key of (nc_src_vp, name). Each hash chain has its own spin lock.
92 * Negative entries may exist and correspond to resolved namecache
93 * structures where nc_vp is NULL. In a negative entry, NCF_WHITEOUT
94 * will be set if the entry corresponds to a whited-out directory entry
95 * (verses simply not finding the entry at all). pcpu_ncache[n].neg_list
96 * is locked via pcpu_ncache[n].neg_spin;
100 * (1) A ncp must be referenced before it can be locked.
102 * (2) A ncp must be locked in order to modify it.
104 * (3) ncp locks are always ordered child -> parent. That may seem
105 * backwards but forward scans use the hash table and thus can hold
106 * the parent unlocked when traversing downward.
108 * This allows insert/rename/delete/dot-dot and other operations
109 * to use ncp->nc_parent links.
111 * This also prevents a locked up e.g. NFS node from creating a
112 * chain reaction all the way back to the root vnode / namecache.
114 * (4) parent linkages require both the parent and child to be locked.
118 * Structures associated with name cacheing.
120 #define NCHHASH(hash) (&nchashtbl[(hash) & nchash])
123 #define NCMOUNT_NUMCACHE 16301 /* prime number */
125 MALLOC_DEFINE(M_VFSCACHE
, "vfscache", "VFS name cache entries");
127 LIST_HEAD(nchash_list
, namecache
);
130 * Don't cachealign, but at least pad to 32 bytes so entries
131 * don't cross a cache line.
134 struct nchash_list list
; /* 16 bytes */
135 struct spinlock spin
; /* 8 bytes */
136 long pad01
; /* 8 bytes */
139 struct ncmount_cache
{
140 struct spinlock spin
;
141 struct namecache
*ncp
;
143 int isneg
; /* if != 0 mp is originator and not target */
147 struct spinlock neg_spin
; /* for neg_list and neg_count */
148 struct namecache_list neg_list
;
155 static struct nchash_head
*nchashtbl
;
156 static struct pcpu_ncache
*pcpu_ncache
;
157 static struct ncmount_cache ncmount_cache
[NCMOUNT_NUMCACHE
];
160 * ncvp_debug - debug cache_fromvp(). This is used by the NFS server
161 * to create the namecache infrastructure leading to a dangling vnode.
163 * 0 Only errors are reported
164 * 1 Successes are reported
165 * 2 Successes + the whole directory scan is reported
166 * 3 Force the directory scan code run as if the parent vnode did not
167 * have a namecache record, even if it does have one.
169 static int ncvp_debug
;
170 SYSCTL_INT(_debug
, OID_AUTO
, ncvp_debug
, CTLFLAG_RW
, &ncvp_debug
, 0,
171 "Namecache debug level (0-3)");
173 static u_long nchash
; /* size of hash table */
174 SYSCTL_ULONG(_debug
, OID_AUTO
, nchash
, CTLFLAG_RD
, &nchash
, 0,
175 "Size of namecache hash table");
177 static int ncnegflush
= 10; /* burst for negative flush */
178 SYSCTL_INT(_debug
, OID_AUTO
, ncnegflush
, CTLFLAG_RW
, &ncnegflush
, 0,
179 "Batch flush negative entries");
181 static int ncposflush
= 10; /* burst for positive flush */
182 SYSCTL_INT(_debug
, OID_AUTO
, ncposflush
, CTLFLAG_RW
, &ncposflush
, 0,
183 "Batch flush positive entries");
185 static int ncnegfactor
= 16; /* ratio of negative entries */
186 SYSCTL_INT(_debug
, OID_AUTO
, ncnegfactor
, CTLFLAG_RW
, &ncnegfactor
, 0,
187 "Ratio of namecache negative entries");
189 static int nclockwarn
; /* warn on locked entries in ticks */
190 SYSCTL_INT(_debug
, OID_AUTO
, nclockwarn
, CTLFLAG_RW
, &nclockwarn
, 0,
191 "Warn on locked namecache entries in ticks");
193 static int numdefered
; /* number of cache entries allocated */
194 SYSCTL_INT(_debug
, OID_AUTO
, numdefered
, CTLFLAG_RD
, &numdefered
, 0,
195 "Number of cache entries allocated");
197 static int ncposlimit
; /* number of cache entries allocated */
198 SYSCTL_INT(_debug
, OID_AUTO
, ncposlimit
, CTLFLAG_RW
, &ncposlimit
, 0,
199 "Number of cache entries allocated");
201 static int ncp_shared_lock_disable
= 0;
202 SYSCTL_INT(_debug
, OID_AUTO
, ncp_shared_lock_disable
, CTLFLAG_RW
,
203 &ncp_shared_lock_disable
, 0, "Disable shared namecache locks");
205 SYSCTL_INT(_debug
, OID_AUTO
, vnsize
, CTLFLAG_RD
, 0, sizeof(struct vnode
),
206 "sizeof(struct vnode)");
207 SYSCTL_INT(_debug
, OID_AUTO
, ncsize
, CTLFLAG_RD
, 0, sizeof(struct namecache
),
208 "sizeof(struct namecache)");
210 static int ncmount_cache_enable
= 1;
211 SYSCTL_INT(_debug
, OID_AUTO
, ncmount_cache_enable
, CTLFLAG_RW
,
212 &ncmount_cache_enable
, 0, "mount point cache");
214 static __inline
void _cache_drop(struct namecache
*ncp
);
215 static int cache_resolve_mp(struct mount
*mp
);
216 static struct vnode
*cache_dvpref(struct namecache
*ncp
);
217 static void _cache_lock(struct namecache
*ncp
);
218 static void _cache_setunresolved(struct namecache
*ncp
);
219 static void _cache_cleanneg(long count
);
220 static void _cache_cleanpos(long count
);
221 static void _cache_cleandefered(void);
222 static void _cache_unlink(struct namecache
*ncp
);
224 static void vfscache_rollup_all(void);
228 * The new name cache statistics
230 SYSCTL_NODE(_vfs
, OID_AUTO
, cache
, CTLFLAG_RW
, 0, "Name cache statistics");
231 static long vfscache_negs
;
232 SYSCTL_LONG(_vfs_cache
, OID_AUTO
, numneg
, CTLFLAG_RD
, &vfscache_negs
, 0,
233 "Number of negative namecache entries");
234 static long vfscache_count
;
235 SYSCTL_LONG(_vfs_cache
, OID_AUTO
, numcache
, CTLFLAG_RD
, &vfscache_count
, 0,
236 "Number of namecaches entries");
237 static long vfscache_leafs
;
238 SYSCTL_LONG(_vfs_cache
, OID_AUTO
, numleafs
, CTLFLAG_RD
, &vfscache_leafs
, 0,
239 "Number of namecaches entries");
241 struct nchstats nchstats
[SMP_MAXCPU
];
243 * Export VFS cache effectiveness statistics to user-land.
245 * The statistics are left for aggregation to user-land so
246 * neat things can be achieved, like observing per-CPU cache
250 sysctl_nchstats(SYSCTL_HANDLER_ARGS
)
252 struct globaldata
*gd
;
256 for (i
= 0; i
< ncpus
; ++i
) {
257 gd
= globaldata_find(i
);
258 if ((error
= SYSCTL_OUT(req
, (void *)&(*gd
->gd_nchstats
),
259 sizeof(struct nchstats
))))
265 SYSCTL_PROC(_vfs_cache
, OID_AUTO
, nchstats
, CTLTYPE_OPAQUE
|CTLFLAG_RD
,
266 0, 0, sysctl_nchstats
, "S,nchstats", "VFS cache effectiveness statistics");
268 static struct namecache
*cache_zap(struct namecache
*ncp
, int nonblock
);
271 * Cache mount points and namecache records in order to avoid unnecessary
272 * atomic ops on mnt_refs and ncp->refs. This improves concurrent SMP
273 * performance and is particularly important on multi-socket systems to
274 * reduce cache-line ping-ponging.
276 * Try to keep the pcpu structure within one cache line (~64 bytes).
278 #define MNTCACHE_COUNT 5
281 struct mount
*mntary
[MNTCACHE_COUNT
];
282 struct namecache
*ncp1
;
283 struct namecache
*ncp2
;
284 struct nchandle ncdir
;
289 static struct mntcache pcpu_mntcache
[MAXCPU
];
293 _cache_mntref(struct mount
*mp
)
295 struct mntcache
*cache
= &pcpu_mntcache
[mycpu
->gd_cpuid
];
298 for (i
= 0; i
< MNTCACHE_COUNT
; ++i
) {
299 if (cache
->mntary
[i
] != mp
)
301 if (atomic_cmpset_ptr((void *)&cache
->mntary
[i
], mp
, NULL
))
304 atomic_add_int(&mp
->mnt_refs
, 1);
309 _cache_mntrel(struct mount
*mp
)
311 struct mntcache
*cache
= &pcpu_mntcache
[mycpu
->gd_cpuid
];
314 for (i
= 0; i
< MNTCACHE_COUNT
; ++i
) {
315 if (cache
->mntary
[i
] == NULL
) {
316 mp
= atomic_swap_ptr((void *)&cache
->mntary
[i
], mp
);
321 i
= (int)((uint32_t)++cache
->iter
% (uint32_t)MNTCACHE_COUNT
);
322 mp
= atomic_swap_ptr((void *)&cache
->mntary
[i
], mp
);
324 atomic_add_int(&mp
->mnt_refs
, -1);
328 * Clears all cached mount points on all cpus. This routine should only
329 * be called when we are waiting for a mount to clear, e.g. so we can
333 cache_clearmntcache(void)
337 for (n
= 0; n
< ncpus
; ++n
) {
338 struct mntcache
*cache
= &pcpu_mntcache
[n
];
339 struct namecache
*ncp
;
343 for (i
= 0; i
< MNTCACHE_COUNT
; ++i
) {
344 if (cache
->mntary
[i
]) {
345 mp
= atomic_swap_ptr(
346 (void *)&cache
->mntary
[i
], NULL
);
348 atomic_add_int(&mp
->mnt_refs
, -1);
352 ncp
= atomic_swap_ptr((void *)&cache
->ncp1
, NULL
);
357 ncp
= atomic_swap_ptr((void *)&cache
->ncp2
, NULL
);
361 if (cache
->ncdir
.ncp
) {
362 ncp
= atomic_swap_ptr((void *)&cache
->ncdir
.ncp
, NULL
);
366 if (cache
->ncdir
.mount
) {
367 mp
= atomic_swap_ptr((void *)&cache
->ncdir
.mount
, NULL
);
369 atomic_add_int(&mp
->mnt_refs
, -1);
376 * Namespace locking. The caller must already hold a reference to the
377 * namecache structure in order to lock/unlock it. This function prevents
378 * the namespace from being created or destroyed by accessors other then
381 * Note that holding a locked namecache structure prevents other threads
382 * from making namespace changes (e.g. deleting or creating), prevents
383 * vnode association state changes by other threads, and prevents the
384 * namecache entry from being resolved or unresolved by other threads.
386 * An exclusive lock owner has full authority to associate/disassociate
387 * vnodes and resolve/unresolve the locked ncp.
389 * A shared lock owner only has authority to acquire the underlying vnode,
392 * The primary lock field is nc_lockstatus. nc_locktd is set after the
393 * fact (when locking) or cleared prior to unlocking.
395 * WARNING! Holding a locked ncp will prevent a vnode from being destroyed
396 * or recycled, but it does NOT help you if the vnode had already
397 * initiated a recyclement. If this is important, use cache_get()
398 * rather then cache_lock() (and deal with the differences in the
399 * way the refs counter is handled). Or, alternatively, make an
400 * unconditional call to cache_validate() or cache_resolve()
401 * after cache_lock() returns.
405 _cache_lock(struct namecache
*ncp
)
413 KKASSERT(ncp
->nc_refs
!= 0);
419 count
= ncp
->nc_lockstatus
;
422 if ((count
& ~(NC_EXLOCK_REQ
|NC_SHLOCK_REQ
)) == 0) {
423 if (atomic_cmpset_int(&ncp
->nc_lockstatus
,
426 * The vp associated with a locked ncp must
427 * be held to prevent it from being recycled.
429 * WARNING! If VRECLAIMED is set the vnode
430 * could already be in the middle of a recycle.
431 * Callers must use cache_vref() or
432 * cache_vget() on the locked ncp to
433 * validate the vp or set the cache entry
436 * NOTE! vhold() is allowed if we hold a
437 * lock on the ncp (which we do).
447 if (ncp
->nc_locktd
== td
) {
448 KKASSERT((count
& NC_SHLOCK_FLAG
) == 0);
449 if (atomic_cmpset_int(&ncp
->nc_lockstatus
,
456 tsleep_interlock(&ncp
->nc_locktd
, 0);
457 if (atomic_cmpset_int(&ncp
->nc_lockstatus
, count
,
458 count
| NC_EXLOCK_REQ
) == 0) {
464 error
= tsleep(&ncp
->nc_locktd
, PINTERLOCKED
,
465 "clock", nclockwarn
);
466 if (error
== EWOULDBLOCK
) {
469 kprintf("[diagnostic] cache_lock: "
470 "%s blocked on %p %08x",
471 td
->td_comm
, ncp
, count
);
472 kprintf(" \"%*.*s\"\n",
473 ncp
->nc_nlen
, ncp
->nc_nlen
,
480 kprintf("[diagnostic] cache_lock: %s unblocked %*.*s after "
483 ncp
->nc_nlen
, ncp
->nc_nlen
, ncp
->nc_name
,
484 (int)(ticks
+ (hz
/ 2) - begticks
) / hz
);
489 * The shared lock works similarly to the exclusive lock except
490 * nc_locktd is left NULL and we need an interlock (VHOLD) to
491 * prevent vhold() races, since the moment our cmpset_int succeeds
492 * another cpu can come in and get its own shared lock.
494 * A critical section is needed to prevent interruption during the
499 _cache_lock_shared(struct namecache
*ncp
)
504 u_int optreq
= NC_EXLOCK_REQ
;
506 KKASSERT(ncp
->nc_refs
!= 0);
510 count
= ncp
->nc_lockstatus
;
513 if ((count
& ~NC_SHLOCK_REQ
) == 0) {
515 if (atomic_cmpset_int(&ncp
->nc_lockstatus
,
517 (count
+ 1) | NC_SHLOCK_FLAG
|
520 * The vp associated with a locked ncp must
521 * be held to prevent it from being recycled.
523 * WARNING! If VRECLAIMED is set the vnode
524 * could already be in the middle of a recycle.
525 * Callers must use cache_vref() or
526 * cache_vget() on the locked ncp to
527 * validate the vp or set the cache entry
530 * NOTE! vhold() is allowed if we hold a
531 * lock on the ncp (which we do).
535 atomic_clear_int(&ncp
->nc_lockstatus
,
546 * If already held shared we can just bump the count, but
547 * only allow this if nobody is trying to get the lock
548 * exclusively. If we are blocking too long ignore excl
549 * requests (which can race/deadlock us).
551 * VHOLD is a bit of a hack. Even though we successfully
552 * added another shared ref, the cpu that got the first
553 * shared ref might not yet have held the vnode.
555 if ((count
& (optreq
|NC_SHLOCK_FLAG
)) == NC_SHLOCK_FLAG
) {
556 KKASSERT((count
& ~(NC_EXLOCK_REQ
|
558 NC_SHLOCK_FLAG
)) > 0);
559 if (atomic_cmpset_int(&ncp
->nc_lockstatus
,
561 while (ncp
->nc_lockstatus
& NC_SHLOCK_VHOLD
)
567 tsleep_interlock(ncp
, 0);
568 if (atomic_cmpset_int(&ncp
->nc_lockstatus
, count
,
569 count
| NC_SHLOCK_REQ
) == 0) {
573 error
= tsleep(ncp
, PINTERLOCKED
, "clocksh", nclockwarn
);
574 if (error
== EWOULDBLOCK
) {
577 didwarn
= ticks
- nclockwarn
;
578 kprintf("[diagnostic] cache_lock_shared: "
579 "%s blocked on %p %08x",
580 curthread
->td_comm
, ncp
, count
);
581 kprintf(" \"%*.*s\"\n",
582 ncp
->nc_nlen
, ncp
->nc_nlen
,
589 kprintf("[diagnostic] cache_lock_shared: "
590 "%s unblocked %*.*s after %d secs\n",
592 ncp
->nc_nlen
, ncp
->nc_nlen
, ncp
->nc_name
,
593 (int)(ticks
- didwarn
) / hz
);
598 * Lock ncp exclusively, return 0 on success.
600 * NOTE: nc_refs may be zero if the ncp is interlocked by circumstance,
601 * such as the case where one of its children is locked.
605 _cache_lock_nonblock(struct namecache
*ncp
)
613 count
= ncp
->nc_lockstatus
;
615 if ((count
& ~(NC_EXLOCK_REQ
|NC_SHLOCK_REQ
)) == 0) {
616 if (atomic_cmpset_int(&ncp
->nc_lockstatus
,
619 * The vp associated with a locked ncp must
620 * be held to prevent it from being recycled.
622 * WARNING! If VRECLAIMED is set the vnode
623 * could already be in the middle of a recycle.
624 * Callers must use cache_vref() or
625 * cache_vget() on the locked ncp to
626 * validate the vp or set the cache entry
629 * NOTE! vhold() is allowed if we hold a
630 * lock on the ncp (which we do).
640 if (ncp
->nc_locktd
== td
) {
641 if (atomic_cmpset_int(&ncp
->nc_lockstatus
,
654 * The shared lock works similarly to the exclusive lock except
655 * nc_locktd is left NULL and we need an interlock (VHOLD) to
656 * prevent vhold() races, since the moment our cmpset_int succeeds
657 * another cpu can come in and get its own shared lock.
659 * A critical section is needed to prevent interruption during the
664 _cache_lock_shared_nonblock(struct namecache
*ncp
)
669 count
= ncp
->nc_lockstatus
;
671 if ((count
& ~NC_SHLOCK_REQ
) == 0) {
673 if (atomic_cmpset_int(&ncp
->nc_lockstatus
,
675 (count
+ 1) | NC_SHLOCK_FLAG
|
678 * The vp associated with a locked ncp must
679 * be held to prevent it from being recycled.
681 * WARNING! If VRECLAIMED is set the vnode
682 * could already be in the middle of a recycle.
683 * Callers must use cache_vref() or
684 * cache_vget() on the locked ncp to
685 * validate the vp or set the cache entry
688 * NOTE! vhold() is allowed if we hold a
689 * lock on the ncp (which we do).
693 atomic_clear_int(&ncp
->nc_lockstatus
,
704 * If already held shared we can just bump the count, but
705 * only allow this if nobody is trying to get the lock
708 * VHOLD is a bit of a hack. Even though we successfully
709 * added another shared ref, the cpu that got the first
710 * shared ref might not yet have held the vnode.
712 if ((count
& (NC_EXLOCK_REQ
|NC_SHLOCK_FLAG
)) ==
714 KKASSERT((count
& ~(NC_EXLOCK_REQ
|
716 NC_SHLOCK_FLAG
)) > 0);
717 if (atomic_cmpset_int(&ncp
->nc_lockstatus
,
719 while (ncp
->nc_lockstatus
& NC_SHLOCK_VHOLD
)
733 * NOTE: nc_refs can be 0 (degenerate case during _cache_drop).
735 * nc_locktd must be NULLed out prior to nc_lockstatus getting cleared.
739 _cache_unlock(struct namecache
*ncp
)
741 thread_t td __debugvar
= curthread
;
744 struct vnode
*dropvp
;
746 KKASSERT(ncp
->nc_refs
>= 0);
747 KKASSERT((ncp
->nc_lockstatus
& ~(NC_EXLOCK_REQ
|NC_SHLOCK_REQ
)) > 0);
748 KKASSERT((ncp
->nc_lockstatus
& NC_SHLOCK_FLAG
) || ncp
->nc_locktd
== td
);
750 count
= ncp
->nc_lockstatus
;
754 * Clear nc_locktd prior to the atomic op (excl lock only)
756 if ((count
& ~(NC_EXLOCK_REQ
|NC_SHLOCK_REQ
)) == 1)
757 ncp
->nc_locktd
= NULL
;
762 ~(NC_EXLOCK_REQ
|NC_SHLOCK_REQ
|NC_SHLOCK_FLAG
)) == 1) {
764 if (count
& NC_EXLOCK_REQ
)
765 ncount
= count
& NC_SHLOCK_REQ
; /* cnt->0 */
769 if (atomic_cmpset_int(&ncp
->nc_lockstatus
,
771 if (count
& NC_EXLOCK_REQ
)
772 wakeup(&ncp
->nc_locktd
);
773 else if (count
& NC_SHLOCK_REQ
)
779 KKASSERT((count
& NC_SHLOCK_VHOLD
) == 0);
780 KKASSERT((count
& ~(NC_EXLOCK_REQ
|
782 NC_SHLOCK_FLAG
)) > 1);
783 if (atomic_cmpset_int(&ncp
->nc_lockstatus
,
788 count
= ncp
->nc_lockstatus
;
793 * Don't actually drop the vp until we successfully clean out
794 * the lock, otherwise we may race another shared lock.
802 _cache_lockstatus(struct namecache
*ncp
)
804 if (ncp
->nc_locktd
== curthread
)
805 return(LK_EXCLUSIVE
);
806 if (ncp
->nc_lockstatus
& NC_SHLOCK_FLAG
)
812 * cache_hold() and cache_drop() prevent the premature deletion of a
813 * namecache entry but do not prevent operations (such as zapping) on
814 * that namecache entry.
816 * This routine may only be called from outside this source module if
817 * nc_refs is already at least 1.
819 * This is a rare case where callers are allowed to hold a spinlock,
820 * so we can't ourselves.
824 _cache_hold(struct namecache
*ncp
)
826 atomic_add_int(&ncp
->nc_refs
, 1);
831 * Drop a cache entry, taking care to deal with races.
833 * For potential 1->0 transitions we must hold the ncp lock to safely
834 * test its flags. An unresolved entry with no children must be zapped
837 * The call to cache_zap() itself will handle all remaining races and
838 * will decrement the ncp's refs regardless. If we are resolved or
839 * have children nc_refs can safely be dropped to 0 without having to
842 * NOTE: cache_zap() will re-check nc_refs and nc_list in a MPSAFE fashion.
844 * NOTE: cache_zap() may return a non-NULL referenced parent which must
845 * be dropped in a loop.
849 _cache_drop(struct namecache
*ncp
)
854 KKASSERT(ncp
->nc_refs
> 0);
858 if (_cache_lock_nonblock(ncp
) == 0) {
859 ncp
->nc_flag
&= ~NCF_DEFEREDZAP
;
860 if ((ncp
->nc_flag
& NCF_UNRESOLVED
) &&
861 TAILQ_EMPTY(&ncp
->nc_list
)) {
862 ncp
= cache_zap(ncp
, 1);
865 if (atomic_cmpset_int(&ncp
->nc_refs
, 1, 0)) {
872 if (atomic_cmpset_int(&ncp
->nc_refs
, refs
, refs
- 1))
880 * Link a new namecache entry to its parent and to the hash table. Be
881 * careful to avoid races if vhold() blocks in the future.
883 * Both ncp and par must be referenced and locked.
885 * NOTE: The hash table spinlock is held during this call, we can't do
889 _cache_link_parent(struct namecache
*ncp
, struct namecache
*par
,
890 struct nchash_head
*nchpp
)
892 struct pcpu_ncache
*pn
= &pcpu_ncache
[mycpu
->gd_cpuid
];
894 KKASSERT(ncp
->nc_parent
== NULL
);
895 ncp
->nc_parent
= par
;
896 ncp
->nc_head
= nchpp
;
899 * Set inheritance flags. Note that the parent flags may be
900 * stale due to getattr potentially not having been run yet
901 * (it gets run during nlookup()'s).
903 ncp
->nc_flag
&= ~(NCF_SF_PNOCACHE
| NCF_UF_PCACHE
);
904 if (par
->nc_flag
& (NCF_SF_NOCACHE
| NCF_SF_PNOCACHE
))
905 ncp
->nc_flag
|= NCF_SF_PNOCACHE
;
906 if (par
->nc_flag
& (NCF_UF_CACHE
| NCF_UF_PCACHE
))
907 ncp
->nc_flag
|= NCF_UF_PCACHE
;
910 * Add to hash table and parent, adjust accounting
912 LIST_INSERT_HEAD(&nchpp
->list
, ncp
, nc_hash
);
913 atomic_add_long(&pn
->vfscache_count
, 1);
914 if (TAILQ_EMPTY(&ncp
->nc_list
))
915 atomic_add_long(&pn
->vfscache_leafs
, 1);
917 if (TAILQ_EMPTY(&par
->nc_list
)) {
918 TAILQ_INSERT_HEAD(&par
->nc_list
, ncp
, nc_entry
);
919 atomic_add_long(&pn
->vfscache_leafs
, -1);
921 * Any vp associated with an ncp which has children must
922 * be held to prevent it from being recycled.
927 TAILQ_INSERT_HEAD(&par
->nc_list
, ncp
, nc_entry
);
932 * Remove the parent and hash associations from a namecache structure.
933 * If this is the last child of the parent the cache_drop(par) will
934 * attempt to recursively zap the parent.
936 * ncp must be locked. This routine will acquire a temporary lock on
937 * the parent as wlel as the appropriate hash chain.
940 _cache_unlink_parent(struct namecache
*ncp
)
942 struct pcpu_ncache
*pn
= &pcpu_ncache
[mycpu
->gd_cpuid
];
943 struct namecache
*par
;
944 struct vnode
*dropvp
;
946 if ((par
= ncp
->nc_parent
) != NULL
) {
947 KKASSERT(ncp
->nc_parent
== par
);
950 spin_lock(&ncp
->nc_head
->spin
);
953 * Remove from hash table and parent, adjust accounting
955 LIST_REMOVE(ncp
, nc_hash
);
956 TAILQ_REMOVE(&par
->nc_list
, ncp
, nc_entry
);
957 atomic_add_long(&pn
->vfscache_count
, -1);
958 if (TAILQ_EMPTY(&ncp
->nc_list
))
959 atomic_add_long(&pn
->vfscache_leafs
, -1);
962 if (TAILQ_EMPTY(&par
->nc_list
)) {
963 atomic_add_long(&pn
->vfscache_leafs
, 1);
967 spin_unlock(&ncp
->nc_head
->spin
);
968 ncp
->nc_parent
= NULL
;
974 * We can only safely vdrop with no spinlocks held.
982 * Allocate a new namecache structure. Most of the code does not require
983 * zero-termination of the string but it makes vop_compat_ncreate() easier.
985 static struct namecache
*
986 cache_alloc(int nlen
)
988 struct namecache
*ncp
;
990 ncp
= kmalloc(sizeof(*ncp
), M_VFSCACHE
, M_WAITOK
|M_ZERO
);
992 ncp
->nc_name
= kmalloc(nlen
+ 1, M_VFSCACHE
, M_WAITOK
);
994 ncp
->nc_flag
= NCF_UNRESOLVED
;
995 ncp
->nc_error
= ENOTCONN
; /* needs to be resolved */
998 TAILQ_INIT(&ncp
->nc_list
);
1004 * Can only be called for the case where the ncp has never been
1005 * associated with anything (so no spinlocks are needed).
1008 _cache_free(struct namecache
*ncp
)
1010 KKASSERT(ncp
->nc_refs
== 1 && ncp
->nc_lockstatus
== 1);
1012 kfree(ncp
->nc_name
, M_VFSCACHE
);
1013 kfree(ncp
, M_VFSCACHE
);
1017 * [re]initialize a nchandle.
1020 cache_zero(struct nchandle
*nch
)
1027 * Ref and deref a namecache structure.
1029 * The caller must specify a stable ncp pointer, typically meaning the
1030 * ncp is already referenced but this can also occur indirectly through
1031 * e.g. holding a lock on a direct child.
1033 * WARNING: Caller may hold an unrelated read spinlock, which means we can't
1034 * use read spinlocks here.
1037 cache_hold(struct nchandle
*nch
)
1039 _cache_hold(nch
->ncp
);
1040 _cache_mntref(nch
->mount
);
1045 * Create a copy of a namecache handle for an already-referenced
1049 cache_copy(struct nchandle
*nch
, struct nchandle
*target
)
1051 struct mntcache
*cache
= &pcpu_mntcache
[mycpu
->gd_cpuid
];
1052 struct namecache
*ncp
;
1055 _cache_mntref(target
->mount
);
1058 if (ncp
== cache
->ncp1
) {
1059 if (atomic_cmpset_ptr((void *)&cache
->ncp1
, ncp
, NULL
))
1062 if (ncp
== cache
->ncp2
) {
1063 if (atomic_cmpset_ptr((void *)&cache
->ncp2
, ncp
, NULL
))
1071 * Caller wants to copy the current directory, copy it out from our
1072 * pcpu cache if possible (the entire critical path is just two localized
1073 * cmpset ops). If the pcpu cache has a snapshot at all it will be a
1074 * valid one, so we don't have to lock p->p_fd even though we are loading
1077 * This has a limited effect since nlookup must still ref and shlock the
1078 * vnode to check perms. We do avoid the per-proc spin-lock though, which
1079 * can aid threaded programs.
1082 cache_copy_ncdir(struct proc
*p
, struct nchandle
*target
)
1084 struct mntcache
*cache
= &pcpu_mntcache
[mycpu
->gd_cpuid
];
1086 *target
= p
->p_fd
->fd_ncdir
;
1087 if (target
->ncp
== cache
->ncdir
.ncp
&&
1088 target
->mount
== cache
->ncdir
.mount
) {
1089 if (atomic_cmpset_ptr((void *)&cache
->ncdir
.ncp
,
1090 target
->ncp
, NULL
)) {
1091 if (atomic_cmpset_ptr((void *)&cache
->ncdir
.mount
,
1092 target
->mount
, NULL
)) {
1096 _cache_drop(target
->ncp
);
1099 spin_lock_shared(&p
->p_fd
->fd_spin
);
1100 cache_copy(&p
->p_fd
->fd_ncdir
, target
);
1101 spin_unlock_shared(&p
->p_fd
->fd_spin
);
1105 cache_changemount(struct nchandle
*nch
, struct mount
*mp
)
1108 _cache_mntrel(nch
->mount
);
1113 cache_drop(struct nchandle
*nch
)
1115 _cache_mntrel(nch
->mount
);
1116 _cache_drop(nch
->ncp
);
1122 * Drop the nchandle, but try to cache the ref to avoid global atomic
1123 * ops. This is typically done on the system root and jail root nchandles.
1126 cache_drop_and_cache(struct nchandle
*nch
)
1128 struct mntcache
*cache
= &pcpu_mntcache
[mycpu
->gd_cpuid
];
1129 struct namecache
*ncp
;
1131 _cache_mntrel(nch
->mount
);
1133 if (cache
->ncp1
== NULL
) {
1134 ncp
= atomic_swap_ptr((void *)&cache
->ncp1
, ncp
);
1138 if (cache
->ncp2
== NULL
) {
1139 ncp
= atomic_swap_ptr((void *)&cache
->ncp2
, ncp
);
1143 if (++cache
->iter
& 1)
1144 ncp
= atomic_swap_ptr((void *)&cache
->ncp2
, ncp
);
1146 ncp
= atomic_swap_ptr((void *)&cache
->ncp1
, ncp
);
1155 * We are dropping what the caller believes is the current directory,
1156 * unconditionally store it in our pcpu cache. Anything already in
1157 * the cache will be discarded.
1160 cache_drop_ncdir(struct nchandle
*nch
)
1162 struct mntcache
*cache
= &pcpu_mntcache
[mycpu
->gd_cpuid
];
1164 nch
->ncp
= atomic_swap_ptr((void *)&cache
->ncdir
.ncp
, nch
->ncp
);
1165 nch
->mount
= atomic_swap_ptr((void *)&cache
->ncdir
.mount
, nch
->mount
);
1167 _cache_drop(nch
->ncp
);
1169 _cache_mntrel(nch
->mount
);
1175 cache_lockstatus(struct nchandle
*nch
)
1177 return(_cache_lockstatus(nch
->ncp
));
1181 cache_lock(struct nchandle
*nch
)
1183 _cache_lock(nch
->ncp
);
1187 cache_lock_maybe_shared(struct nchandle
*nch
, int excl
)
1189 struct namecache
*ncp
= nch
->ncp
;
1191 if (ncp_shared_lock_disable
|| excl
||
1192 (ncp
->nc_flag
& NCF_UNRESOLVED
)) {
1195 _cache_lock_shared(ncp
);
1196 if ((ncp
->nc_flag
& NCF_UNRESOLVED
) == 0) {
1197 if (ncp
->nc_vp
&& (ncp
->nc_vp
->v_flag
& VRECLAIMED
)) {
1209 * Relock nch1 given an unlocked nch1 and a locked nch2. The caller
1210 * is responsible for checking both for validity on return as they
1211 * may have become invalid.
1213 * We have to deal with potential deadlocks here, just ping pong
1214 * the lock until we get it (we will always block somewhere when
1215 * looping so this is not cpu-intensive).
1217 * which = 0 nch1 not locked, nch2 is locked
1218 * which = 1 nch1 is locked, nch2 is not locked
1221 cache_relock(struct nchandle
*nch1
, struct ucred
*cred1
,
1222 struct nchandle
*nch2
, struct ucred
*cred2
)
1230 if (cache_lock_nonblock(nch1
) == 0) {
1231 cache_resolve(nch1
, cred1
);
1236 cache_resolve(nch1
, cred1
);
1239 if (cache_lock_nonblock(nch2
) == 0) {
1240 cache_resolve(nch2
, cred2
);
1245 cache_resolve(nch2
, cred2
);
1252 cache_lock_nonblock(struct nchandle
*nch
)
1254 return(_cache_lock_nonblock(nch
->ncp
));
1258 cache_unlock(struct nchandle
*nch
)
1260 _cache_unlock(nch
->ncp
);
1264 * ref-and-lock, unlock-and-deref functions.
1266 * This function is primarily used by nlookup. Even though cache_lock
1267 * holds the vnode, it is possible that the vnode may have already
1268 * initiated a recyclement.
1270 * We want cache_get() to return a definitively usable vnode or a
1271 * definitively unresolved ncp.
1275 _cache_get(struct namecache
*ncp
)
1279 if (ncp
->nc_vp
&& (ncp
->nc_vp
->v_flag
& VRECLAIMED
))
1280 _cache_setunresolved(ncp
);
1285 * Attempt to obtain a shared lock on the ncp. A shared lock will only
1286 * be obtained if the ncp is resolved and the vnode (if not ENOENT) is
1287 * valid. Otherwise an exclusive lock will be acquired instead.
1291 _cache_get_maybe_shared(struct namecache
*ncp
, int excl
)
1293 if (ncp_shared_lock_disable
|| excl
||
1294 (ncp
->nc_flag
& NCF_UNRESOLVED
)) {
1295 return(_cache_get(ncp
));
1298 _cache_lock_shared(ncp
);
1299 if ((ncp
->nc_flag
& NCF_UNRESOLVED
) == 0) {
1300 if (ncp
->nc_vp
&& (ncp
->nc_vp
->v_flag
& VRECLAIMED
)) {
1302 ncp
= _cache_get(ncp
);
1307 ncp
= _cache_get(ncp
);
1314 * This is a special form of _cache_lock() which only succeeds if
1315 * it can get a pristine, non-recursive lock. The caller must have
1316 * already ref'd the ncp.
1318 * On success the ncp will be locked, on failure it will not. The
1319 * ref count does not change either way.
1321 * We want _cache_lock_special() (on success) to return a definitively
1322 * usable vnode or a definitively unresolved ncp.
1325 _cache_lock_special(struct namecache
*ncp
)
1327 if (_cache_lock_nonblock(ncp
) == 0) {
1328 if ((ncp
->nc_lockstatus
&
1329 ~(NC_EXLOCK_REQ
|NC_SHLOCK_REQ
)) == 1) {
1330 if (ncp
->nc_vp
&& (ncp
->nc_vp
->v_flag
& VRECLAIMED
))
1331 _cache_setunresolved(ncp
);
1336 return(EWOULDBLOCK
);
1340 * This function tries to get a shared lock but will back-off to an exclusive
1343 * (1) Some other thread is trying to obtain an exclusive lock
1344 * (to prevent the exclusive requester from getting livelocked out
1345 * by many shared locks).
1347 * (2) The current thread already owns an exclusive lock (to avoid
1350 * WARNING! On machines with lots of cores we really want to try hard to
1351 * get a shared lock or concurrent path lookups can chain-react
1352 * into a very high-latency exclusive lock.
1355 _cache_lock_shared_special(struct namecache
*ncp
)
1358 * Only honor a successful shared lock (returning 0) if there is
1359 * no exclusive request pending and the vnode, if present, is not
1360 * in a reclaimed state.
1362 if (_cache_lock_shared_nonblock(ncp
) == 0) {
1363 if ((ncp
->nc_lockstatus
& NC_EXLOCK_REQ
) == 0) {
1364 if (ncp
->nc_vp
== NULL
||
1365 (ncp
->nc_vp
->v_flag
& VRECLAIMED
) == 0) {
1370 return(EWOULDBLOCK
);
1374 * Non-blocking shared lock failed. If we already own the exclusive
1375 * lock just acquire another exclusive lock (instead of deadlocking).
1376 * Otherwise acquire a shared lock.
1378 if (ncp
->nc_locktd
== curthread
) {
1382 _cache_lock_shared(ncp
);
1388 * NOTE: The same nchandle can be passed for both arguments.
1391 cache_get(struct nchandle
*nch
, struct nchandle
*target
)
1393 KKASSERT(nch
->ncp
->nc_refs
> 0);
1394 target
->mount
= nch
->mount
;
1395 target
->ncp
= _cache_get(nch
->ncp
);
1396 _cache_mntref(target
->mount
);
1400 cache_get_maybe_shared(struct nchandle
*nch
, struct nchandle
*target
, int excl
)
1402 KKASSERT(nch
->ncp
->nc_refs
> 0);
1403 target
->mount
= nch
->mount
;
1404 target
->ncp
= _cache_get_maybe_shared(nch
->ncp
, excl
);
1405 _cache_mntref(target
->mount
);
1413 _cache_put(struct namecache
*ncp
)
1423 cache_put(struct nchandle
*nch
)
1425 _cache_mntrel(nch
->mount
);
1426 _cache_put(nch
->ncp
);
1432 * Resolve an unresolved ncp by associating a vnode with it. If the
1433 * vnode is NULL, a negative cache entry is created.
1435 * The ncp should be locked on entry and will remain locked on return.
1439 _cache_setvp(struct mount
*mp
, struct namecache
*ncp
, struct vnode
*vp
)
1441 KKASSERT(ncp
->nc_flag
& NCF_UNRESOLVED
);
1442 KKASSERT(_cache_lockstatus(ncp
) == LK_EXCLUSIVE
);
1446 * Any vp associated with an ncp which has children must
1447 * be held. Any vp associated with a locked ncp must be held.
1449 if (!TAILQ_EMPTY(&ncp
->nc_list
))
1451 spin_lock(&vp
->v_spin
);
1453 TAILQ_INSERT_HEAD(&vp
->v_namecache
, ncp
, nc_vnode
);
1454 spin_unlock(&vp
->v_spin
);
1455 if (ncp
->nc_lockstatus
& ~(NC_EXLOCK_REQ
|NC_SHLOCK_REQ
))
1459 * Set auxiliary flags
1461 switch(vp
->v_type
) {
1463 ncp
->nc_flag
|= NCF_ISDIR
;
1466 ncp
->nc_flag
|= NCF_ISSYMLINK
;
1467 /* XXX cache the contents of the symlink */
1473 /* XXX: this is a hack to work-around the lack of a real pfs vfs
1476 if (strncmp(mp
->mnt_stat
.f_fstypename
, "null", 5) == 0)
1480 * When creating a negative cache hit we set the
1481 * namecache_gen. A later resolve will clean out the
1482 * negative cache hit if the mount point's namecache_gen
1483 * has changed. Used by devfs, could also be used by
1486 struct pcpu_ncache
*pn
= &pcpu_ncache
[mycpu
->gd_cpuid
];
1489 ncp
->nc_negcpu
= mycpu
->gd_cpuid
;
1490 spin_lock(&pn
->neg_spin
);
1491 TAILQ_INSERT_TAIL(&pn
->neg_list
, ncp
, nc_vnode
);
1493 spin_unlock(&pn
->neg_spin
);
1494 atomic_add_long(&pn
->vfscache_negs
, 1);
1496 ncp
->nc_error
= ENOENT
;
1498 VFS_NCPGEN_SET(mp
, ncp
);
1500 ncp
->nc_flag
&= ~(NCF_UNRESOLVED
| NCF_DEFEREDZAP
);
1507 cache_setvp(struct nchandle
*nch
, struct vnode
*vp
)
1509 _cache_setvp(nch
->mount
, nch
->ncp
, vp
);
1516 cache_settimeout(struct nchandle
*nch
, int nticks
)
1518 struct namecache
*ncp
= nch
->ncp
;
1520 if ((ncp
->nc_timeout
= ticks
+ nticks
) == 0)
1521 ncp
->nc_timeout
= 1;
1525 * Disassociate the vnode or negative-cache association and mark a
1526 * namecache entry as unresolved again. Note that the ncp is still
1527 * left in the hash table and still linked to its parent.
1529 * The ncp should be locked and refd on entry and will remain locked and refd
1532 * This routine is normally never called on a directory containing children.
1533 * However, NFS often does just that in its rename() code as a cop-out to
1534 * avoid complex namespace operations. This disconnects a directory vnode
1535 * from its namecache and can cause the OLDAPI and NEWAPI to get out of
1541 _cache_setunresolved(struct namecache
*ncp
)
1545 if ((ncp
->nc_flag
& NCF_UNRESOLVED
) == 0) {
1546 ncp
->nc_flag
|= NCF_UNRESOLVED
;
1547 ncp
->nc_timeout
= 0;
1548 ncp
->nc_error
= ENOTCONN
;
1549 if ((vp
= ncp
->nc_vp
) != NULL
) {
1550 spin_lock(&vp
->v_spin
);
1552 TAILQ_REMOVE(&vp
->v_namecache
, ncp
, nc_vnode
);
1553 spin_unlock(&vp
->v_spin
);
1556 * Any vp associated with an ncp with children is
1557 * held by that ncp. Any vp associated with a locked
1558 * ncp is held by that ncp. These conditions must be
1559 * undone when the vp is cleared out from the ncp.
1561 if (!TAILQ_EMPTY(&ncp
->nc_list
))
1563 if (ncp
->nc_lockstatus
& ~(NC_EXLOCK_REQ
|NC_SHLOCK_REQ
))
1566 struct pcpu_ncache
*pn
;
1568 pn
= &pcpu_ncache
[ncp
->nc_negcpu
];
1570 atomic_add_long(&pn
->vfscache_negs
, -1);
1571 spin_lock(&pn
->neg_spin
);
1572 TAILQ_REMOVE(&pn
->neg_list
, ncp
, nc_vnode
);
1574 spin_unlock(&pn
->neg_spin
);
1576 ncp
->nc_flag
&= ~(NCF_WHITEOUT
|NCF_ISDIR
|NCF_ISSYMLINK
);
1581 * The cache_nresolve() code calls this function to automatically
1582 * set a resolved cache element to unresolved if it has timed out
1583 * or if it is a negative cache hit and the mount point namecache_gen
1587 _cache_auto_unresolve_test(struct mount
*mp
, struct namecache
*ncp
)
1590 * Try to zap entries that have timed out. We have
1591 * to be careful here because locked leafs may depend
1592 * on the vnode remaining intact in a parent, so only
1593 * do this under very specific conditions.
1595 if (ncp
->nc_timeout
&& (int)(ncp
->nc_timeout
- ticks
) < 0 &&
1596 TAILQ_EMPTY(&ncp
->nc_list
)) {
1601 * If a resolved negative cache hit is invalid due to
1602 * the mount's namecache generation being bumped, zap it.
1604 if (ncp
->nc_vp
== NULL
&& VFS_NCPGEN_TEST(mp
, ncp
)) {
1609 * Otherwise we are good
1614 static __inline
void
1615 _cache_auto_unresolve(struct mount
*mp
, struct namecache
*ncp
)
1618 * Already in an unresolved state, nothing to do.
1620 if ((ncp
->nc_flag
& NCF_UNRESOLVED
) == 0) {
1621 if (_cache_auto_unresolve_test(mp
, ncp
))
1622 _cache_setunresolved(ncp
);
1630 cache_setunresolved(struct nchandle
*nch
)
1632 _cache_setunresolved(nch
->ncp
);
1636 * Determine if we can clear NCF_ISMOUNTPT by scanning the mountlist
1637 * looking for matches. This flag tells the lookup code when it must
1638 * check for a mount linkage and also prevents the directories in question
1639 * from being deleted or renamed.
1643 cache_clrmountpt_callback(struct mount
*mp
, void *data
)
1645 struct nchandle
*nch
= data
;
1647 if (mp
->mnt_ncmounton
.ncp
== nch
->ncp
)
1649 if (mp
->mnt_ncmountpt
.ncp
== nch
->ncp
)
1655 * Clear NCF_ISMOUNTPT on nch->ncp if it is no longer associated
1656 * with a mount point.
1659 cache_clrmountpt(struct nchandle
*nch
)
1663 count
= mountlist_scan(cache_clrmountpt_callback
, nch
,
1664 MNTSCAN_FORWARD
|MNTSCAN_NOBUSY
);
1666 nch
->ncp
->nc_flag
&= ~NCF_ISMOUNTPT
;
1670 * Invalidate portions of the namecache topology given a starting entry.
1671 * The passed ncp is set to an unresolved state and:
1673 * The passed ncp must be referencxed and locked. The routine may unlock
1674 * and relock ncp several times, and will recheck the children and loop
1675 * to catch races. When done the passed ncp will be returned with the
1676 * reference and lock intact.
1678 * CINV_DESTROY - Set a flag in the passed ncp entry indicating
1679 * that the physical underlying nodes have been
1680 * destroyed... as in deleted. For example, when
1681 * a directory is removed. This will cause record
1682 * lookups on the name to no longer be able to find
1683 * the record and tells the resolver to return failure
1684 * rather then trying to resolve through the parent.
1686 * The topology itself, including ncp->nc_name,
1689 * This only applies to the passed ncp, if CINV_CHILDREN
1690 * is specified the children are not flagged.
1692 * CINV_CHILDREN - Set all children (recursively) to an unresolved
1695 * Note that this will also have the side effect of
1696 * cleaning out any unreferenced nodes in the topology
1697 * from the leaves up as the recursion backs out.
1699 * Note that the topology for any referenced nodes remains intact, but
1700 * the nodes will be marked as having been destroyed and will be set
1701 * to an unresolved state.
1703 * It is possible for cache_inval() to race a cache_resolve(), meaning that
1704 * the namecache entry may not actually be invalidated on return if it was
1705 * revalidated while recursing down into its children. This code guarentees
1706 * that the node(s) will go through an invalidation cycle, but does not
1707 * guarentee that they will remain in an invalidated state.
1709 * Returns non-zero if a revalidation was detected during the invalidation
1710 * recursion, zero otherwise. Note that since only the original ncp is
1711 * locked the revalidation ultimately can only indicate that the original ncp
1712 * *MIGHT* no have been reresolved.
1714 * DEEP RECURSION HANDLING - If a recursive invalidation recurses deeply we
1715 * have to avoid blowing out the kernel stack. We do this by saving the
1716 * deep namecache node and aborting the recursion, then re-recursing at that
1717 * node using a depth-first algorithm in order to allow multiple deep
1718 * recursions to chain through each other, then we restart the invalidation
1723 struct namecache
*resume_ncp
;
1727 static int _cache_inval_internal(struct namecache
*, int, struct cinvtrack
*);
1731 _cache_inval(struct namecache
*ncp
, int flags
)
1733 struct cinvtrack track
;
1734 struct namecache
*ncp2
;
1738 track
.resume_ncp
= NULL
;
1741 r
= _cache_inval_internal(ncp
, flags
, &track
);
1742 if (track
.resume_ncp
== NULL
)
1745 while ((ncp2
= track
.resume_ncp
) != NULL
) {
1746 track
.resume_ncp
= NULL
;
1748 _cache_inval_internal(ncp2
, flags
& ~CINV_DESTROY
,
1758 cache_inval(struct nchandle
*nch
, int flags
)
1760 return(_cache_inval(nch
->ncp
, flags
));
1764 * Helper for _cache_inval(). The passed ncp is refd and locked and
1765 * remains that way on return, but may be unlocked/relocked multiple
1766 * times by the routine.
1769 _cache_inval_internal(struct namecache
*ncp
, int flags
, struct cinvtrack
*track
)
1771 struct namecache
*nextkid
;
1774 KKASSERT(_cache_lockstatus(ncp
) == LK_EXCLUSIVE
);
1776 _cache_setunresolved(ncp
);
1777 if (flags
& CINV_DESTROY
) {
1778 ncp
->nc_flag
|= NCF_DESTROYED
;
1779 ++ncp
->nc_generation
;
1781 while ((flags
& CINV_CHILDREN
) &&
1782 (nextkid
= TAILQ_FIRST(&ncp
->nc_list
)) != NULL
1784 struct namecache
*kid
;
1788 _cache_hold(nextkid
);
1789 if (++track
->depth
> MAX_RECURSION_DEPTH
) {
1790 track
->resume_ncp
= ncp
;
1794 while ((kid
= nextkid
) != NULL
) {
1796 * Parent (ncp) must be locked for the iteration.
1799 if (kid
->nc_parent
!= ncp
) {
1801 kprintf("cache_inval_internal restartA %s\n",
1806 if ((nextkid
= TAILQ_NEXT(kid
, nc_entry
)) != NULL
)
1807 _cache_hold(nextkid
);
1810 * Parent unlocked for this section to avoid
1814 if (track
->resume_ncp
) {
1819 if ((kid
->nc_flag
& NCF_UNRESOLVED
) == 0 ||
1820 TAILQ_FIRST(&kid
->nc_list
)
1823 if (kid
->nc_parent
!= ncp
) {
1824 kprintf("cache_inval_internal "
1834 rcnt
+= _cache_inval_internal(kid
, flags
& ~CINV_DESTROY
, track
);
1841 _cache_drop(nextkid
);
1848 * Someone could have gotten in there while ncp was unlocked,
1851 if ((ncp
->nc_flag
& NCF_UNRESOLVED
) == 0)
1857 * Invalidate a vnode's namecache associations. To avoid races against
1858 * the resolver we do not invalidate a node which we previously invalidated
1859 * but which was then re-resolved while we were in the invalidation loop.
1861 * Returns non-zero if any namecache entries remain after the invalidation
1864 * NOTE: Unlike the namecache topology which guarentees that ncp's will not
1865 * be ripped out of the topology while held, the vnode's v_namecache
1866 * list has no such restriction. NCP's can be ripped out of the list
1867 * at virtually any time if not locked, even if held.
1869 * In addition, the v_namecache list itself must be locked via
1870 * the vnode's spinlock.
1873 cache_inval_vp(struct vnode
*vp
, int flags
)
1875 struct namecache
*ncp
;
1876 struct namecache
*next
;
1879 spin_lock(&vp
->v_spin
);
1880 ncp
= TAILQ_FIRST(&vp
->v_namecache
);
1884 /* loop entered with ncp held and vp spin-locked */
1885 if ((next
= TAILQ_NEXT(ncp
, nc_vnode
)) != NULL
)
1887 spin_unlock(&vp
->v_spin
);
1889 if (ncp
->nc_vp
!= vp
) {
1890 kprintf("Warning: cache_inval_vp: race-A detected on "
1891 "%s\n", ncp
->nc_name
);
1897 _cache_inval(ncp
, flags
);
1898 _cache_put(ncp
); /* also releases reference */
1900 spin_lock(&vp
->v_spin
);
1901 if (ncp
&& ncp
->nc_vp
!= vp
) {
1902 spin_unlock(&vp
->v_spin
);
1903 kprintf("Warning: cache_inval_vp: race-B detected on "
1904 "%s\n", ncp
->nc_name
);
1909 spin_unlock(&vp
->v_spin
);
1910 return(TAILQ_FIRST(&vp
->v_namecache
) != NULL
);
1914 * This routine is used instead of the normal cache_inval_vp() when we
1915 * are trying to recycle otherwise good vnodes.
1917 * Return 0 on success, non-zero if not all namecache records could be
1918 * disassociated from the vnode (for various reasons).
1921 cache_inval_vp_nonblock(struct vnode
*vp
)
1923 struct namecache
*ncp
;
1924 struct namecache
*next
;
1926 spin_lock(&vp
->v_spin
);
1927 ncp
= TAILQ_FIRST(&vp
->v_namecache
);
1931 /* loop entered with ncp held */
1932 if ((next
= TAILQ_NEXT(ncp
, nc_vnode
)) != NULL
)
1934 spin_unlock(&vp
->v_spin
);
1935 if (_cache_lock_nonblock(ncp
)) {
1941 if (ncp
->nc_vp
!= vp
) {
1942 kprintf("Warning: cache_inval_vp: race-A detected on "
1943 "%s\n", ncp
->nc_name
);
1949 _cache_inval(ncp
, 0);
1950 _cache_put(ncp
); /* also releases reference */
1952 spin_lock(&vp
->v_spin
);
1953 if (ncp
&& ncp
->nc_vp
!= vp
) {
1954 spin_unlock(&vp
->v_spin
);
1955 kprintf("Warning: cache_inval_vp: race-B detected on "
1956 "%s\n", ncp
->nc_name
);
1961 spin_unlock(&vp
->v_spin
);
1963 return(TAILQ_FIRST(&vp
->v_namecache
) != NULL
);
1967 * Clears the universal directory search 'ok' flag. This flag allows
1968 * nlookup() to bypass normal vnode checks. This flag is a cached flag
1969 * so clearing it simply forces revalidation.
1972 cache_inval_wxok(struct vnode
*vp
)
1974 struct namecache
*ncp
;
1976 spin_lock(&vp
->v_spin
);
1977 TAILQ_FOREACH(ncp
, &vp
->v_namecache
, nc_vnode
) {
1978 if (ncp
->nc_flag
& NCF_WXOK
)
1979 atomic_clear_short(&ncp
->nc_flag
, NCF_WXOK
);
1981 spin_unlock(&vp
->v_spin
);
1985 * The source ncp has been renamed to the target ncp. Both fncp and tncp
1986 * must be locked. The target ncp is destroyed (as a normal rename-over
1987 * would destroy the target file or directory).
1989 * Because there may be references to the source ncp we cannot copy its
1990 * contents to the target. Instead the source ncp is relinked as the target
1991 * and the target ncp is removed from the namecache topology.
1994 cache_rename(struct nchandle
*fnch
, struct nchandle
*tnch
)
1996 struct namecache
*fncp
= fnch
->ncp
;
1997 struct namecache
*tncp
= tnch
->ncp
;
1998 struct namecache
*tncp_par
;
1999 struct nchash_head
*nchpp
;
2004 ++fncp
->nc_generation
;
2005 ++tncp
->nc_generation
;
2006 if (tncp
->nc_nlen
) {
2007 nname
= kmalloc(tncp
->nc_nlen
+ 1, M_VFSCACHE
, M_WAITOK
);
2008 bcopy(tncp
->nc_name
, nname
, tncp
->nc_nlen
);
2009 nname
[tncp
->nc_nlen
] = 0;
2015 * Rename fncp (unlink)
2017 _cache_unlink_parent(fncp
);
2018 oname
= fncp
->nc_name
;
2019 fncp
->nc_name
= nname
;
2020 fncp
->nc_nlen
= tncp
->nc_nlen
;
2022 kfree(oname
, M_VFSCACHE
);
2024 tncp_par
= tncp
->nc_parent
;
2025 _cache_hold(tncp_par
);
2026 _cache_lock(tncp_par
);
2029 * Rename fncp (relink)
2031 hash
= fnv_32_buf(fncp
->nc_name
, fncp
->nc_nlen
, FNV1_32_INIT
);
2032 hash
= fnv_32_buf(&tncp_par
, sizeof(tncp_par
), hash
);
2033 nchpp
= NCHHASH(hash
);
2035 spin_lock(&nchpp
->spin
);
2036 _cache_link_parent(fncp
, tncp_par
, nchpp
);
2037 spin_unlock(&nchpp
->spin
);
2039 _cache_put(tncp_par
);
2042 * Get rid of the overwritten tncp (unlink)
2044 _cache_unlink(tncp
);
2048 * Perform actions consistent with unlinking a file. The passed-in ncp
2051 * The ncp is marked DESTROYED so it no longer shows up in searches,
2052 * and will be physically deleted when the vnode goes away.
2054 * If the related vnode has no refs then we cycle it through vget()/vput()
2055 * to (possibly if we don't have a ref race) trigger a deactivation,
2056 * allowing the VFS to trivially detect and recycle the deleted vnode
2057 * via VOP_INACTIVE().
2059 * NOTE: _cache_rename() will automatically call _cache_unlink() on the
2063 cache_unlink(struct nchandle
*nch
)
2065 _cache_unlink(nch
->ncp
);
2069 _cache_unlink(struct namecache
*ncp
)
2074 * Causes lookups to fail and allows another ncp with the same
2075 * name to be created under ncp->nc_parent.
2077 ncp
->nc_flag
|= NCF_DESTROYED
;
2078 ++ncp
->nc_generation
;
2081 * Attempt to trigger a deactivation. Set VREF_FINALIZE to
2082 * force action on the 1->0 transition.
2084 if ((ncp
->nc_flag
& NCF_UNRESOLVED
) == 0 &&
2085 (vp
= ncp
->nc_vp
) != NULL
) {
2086 atomic_set_int(&vp
->v_refcnt
, VREF_FINALIZE
);
2087 if (VREFCNT(vp
) <= 0) {
2088 if (vget(vp
, LK_SHARED
) == 0)
2095 * Return non-zero if the nch might be associated with an open and/or mmap()'d
2096 * file. The easy solution is to just return non-zero if the vnode has refs.
2097 * Used to interlock hammer2 reclaims (VREF_FINALIZE should already be set to
2098 * force the reclaim).
2101 cache_isopen(struct nchandle
*nch
)
2104 struct namecache
*ncp
= nch
->ncp
;
2106 if ((ncp
->nc_flag
& NCF_UNRESOLVED
) == 0 &&
2107 (vp
= ncp
->nc_vp
) != NULL
&&
2116 * vget the vnode associated with the namecache entry. Resolve the namecache
2117 * entry if necessary. The passed ncp must be referenced and locked. If
2118 * the ncp is resolved it might be locked shared.
2120 * lk_type may be LK_SHARED, LK_EXCLUSIVE. A ref'd, possibly locked
2121 * (depending on the passed lk_type) will be returned in *vpp with an error
2122 * of 0, or NULL will be returned in *vpp with a non-0 error code. The
2123 * most typical error is ENOENT, meaning that the ncp represents a negative
2124 * cache hit and there is no vnode to retrieve, but other errors can occur
2127 * The vget() can race a reclaim. If this occurs we re-resolve the
2130 * There are numerous places in the kernel where vget() is called on a
2131 * vnode while one or more of its namecache entries is locked. Releasing
2132 * a vnode never deadlocks against locked namecache entries (the vnode
2133 * will not get recycled while referenced ncp's exist). This means we
2134 * can safely acquire the vnode. In fact, we MUST NOT release the ncp
2135 * lock when acquiring the vp lock or we might cause a deadlock.
2137 * NOTE: The passed-in ncp must be locked exclusively if it is initially
2138 * unresolved. If a reclaim race occurs the passed-in ncp will be
2139 * relocked exclusively before being re-resolved.
2142 cache_vget(struct nchandle
*nch
, struct ucred
*cred
,
2143 int lk_type
, struct vnode
**vpp
)
2145 struct namecache
*ncp
;
2152 if (ncp
->nc_flag
& NCF_UNRESOLVED
)
2153 error
= cache_resolve(nch
, cred
);
2157 if (error
== 0 && (vp
= ncp
->nc_vp
) != NULL
) {
2158 error
= vget(vp
, lk_type
);
2163 * The ncp may have been locked shared, we must relock
2164 * it exclusively before we can set it to unresolved.
2166 if (error
== ENOENT
) {
2167 kprintf("Warning: vnode reclaim race detected "
2168 "in cache_vget on %p (%s)\n",
2172 _cache_setunresolved(ncp
);
2177 * Not a reclaim race, some other error.
2179 KKASSERT(ncp
->nc_vp
== vp
);
2182 KKASSERT(ncp
->nc_vp
== vp
);
2183 KKASSERT((vp
->v_flag
& VRECLAIMED
) == 0);
2186 if (error
== 0 && vp
== NULL
)
2193 * Similar to cache_vget() but only acquires a ref on the vnode.
2195 * NOTE: The passed-in ncp must be locked exclusively if it is initially
2196 * unresolved. If a reclaim race occurs the passed-in ncp will be
2197 * relocked exclusively before being re-resolved.
2200 cache_vref(struct nchandle
*nch
, struct ucred
*cred
, struct vnode
**vpp
)
2202 struct namecache
*ncp
;
2209 if (ncp
->nc_flag
& NCF_UNRESOLVED
)
2210 error
= cache_resolve(nch
, cred
);
2214 if (error
== 0 && (vp
= ncp
->nc_vp
) != NULL
) {
2215 error
= vget(vp
, LK_SHARED
);
2220 if (error
== ENOENT
) {
2221 kprintf("Warning: vnode reclaim race detected "
2222 "in cache_vget on %p (%s)\n",
2226 _cache_setunresolved(ncp
);
2231 * Not a reclaim race, some other error.
2233 KKASSERT(ncp
->nc_vp
== vp
);
2236 KKASSERT(ncp
->nc_vp
== vp
);
2237 KKASSERT((vp
->v_flag
& VRECLAIMED
) == 0);
2238 /* caller does not want a lock */
2242 if (error
== 0 && vp
== NULL
)
2249 * Return a referenced vnode representing the parent directory of
2252 * Because the caller has locked the ncp it should not be possible for
2253 * the parent ncp to go away. However, the parent can unresolve its
2254 * dvp at any time so we must be able to acquire a lock on the parent
2255 * to safely access nc_vp.
2257 * We have to leave par unlocked when vget()ing dvp to avoid a deadlock,
2258 * so use vhold()/vdrop() while holding the lock to prevent dvp from
2259 * getting destroyed.
2261 * NOTE: vhold() is allowed when dvp has 0 refs if we hold a
2262 * lock on the ncp in question..
2264 static struct vnode
*
2265 cache_dvpref(struct namecache
*ncp
)
2267 struct namecache
*par
;
2271 if ((par
= ncp
->nc_parent
) != NULL
) {
2274 if ((par
->nc_flag
& NCF_UNRESOLVED
) == 0) {
2275 if ((dvp
= par
->nc_vp
) != NULL
)
2280 if (vget(dvp
, LK_SHARED
) == 0) {
2283 /* return refd, unlocked dvp */
2295 * Convert a directory vnode to a namecache record without any other
2296 * knowledge of the topology. This ONLY works with directory vnodes and
2297 * is ONLY used by the NFS server. dvp must be refd but unlocked, and the
2298 * returned ncp (if not NULL) will be held and unlocked.
2300 * If 'makeit' is 0 and dvp has no existing namecache record, NULL is returned.
2301 * If 'makeit' is 1 we attempt to track-down and create the namecache topology
2302 * for dvp. This will fail only if the directory has been deleted out from
2305 * Callers must always check for a NULL return no matter the value of 'makeit'.
2307 * To avoid underflowing the kernel stack each recursive call increments
2308 * the makeit variable.
2311 static int cache_inefficient_scan(struct nchandle
*nch
, struct ucred
*cred
,
2312 struct vnode
*dvp
, char *fakename
);
2313 static int cache_fromdvp_try(struct vnode
*dvp
, struct ucred
*cred
,
2314 struct vnode
**saved_dvp
);
2317 cache_fromdvp(struct vnode
*dvp
, struct ucred
*cred
, int makeit
,
2318 struct nchandle
*nch
)
2320 struct vnode
*saved_dvp
;
2326 nch
->mount
= dvp
->v_mount
;
2331 * Handle the makeit == 0 degenerate case
2334 spin_lock_shared(&dvp
->v_spin
);
2335 nch
->ncp
= TAILQ_FIRST(&dvp
->v_namecache
);
2338 spin_unlock_shared(&dvp
->v_spin
);
2342 * Loop until resolution, inside code will break out on error.
2346 * Break out if we successfully acquire a working ncp.
2348 spin_lock_shared(&dvp
->v_spin
);
2349 nch
->ncp
= TAILQ_FIRST(&dvp
->v_namecache
);
2352 spin_unlock_shared(&dvp
->v_spin
);
2355 spin_unlock_shared(&dvp
->v_spin
);
2358 * If dvp is the root of its filesystem it should already
2359 * have a namecache pointer associated with it as a side
2360 * effect of the mount, but it may have been disassociated.
2362 if (dvp
->v_flag
& VROOT
) {
2363 nch
->ncp
= _cache_get(nch
->mount
->mnt_ncmountpt
.ncp
);
2364 error
= cache_resolve_mp(nch
->mount
);
2365 _cache_put(nch
->ncp
);
2367 kprintf("cache_fromdvp: resolve root of mount %p error %d",
2368 dvp
->v_mount
, error
);
2372 kprintf(" failed\n");
2377 kprintf(" succeeded\n");
2382 * If we are recursed too deeply resort to an O(n^2)
2383 * algorithm to resolve the namecache topology. The
2384 * resolved pvp is left referenced in saved_dvp to
2385 * prevent the tree from being destroyed while we loop.
2388 error
= cache_fromdvp_try(dvp
, cred
, &saved_dvp
);
2390 kprintf("lookupdotdot(longpath) failed %d "
2391 "dvp %p\n", error
, dvp
);
2399 * Get the parent directory and resolve its ncp.
2402 kfree(fakename
, M_TEMP
);
2405 error
= vop_nlookupdotdot(*dvp
->v_ops
, dvp
, &pvp
, cred
,
2408 kprintf("lookupdotdot failed %d dvp %p\n", error
, dvp
);
2414 * Reuse makeit as a recursion depth counter. On success
2415 * nch will be fully referenced.
2417 cache_fromdvp(pvp
, cred
, makeit
+ 1, nch
);
2419 if (nch
->ncp
== NULL
)
2423 * Do an inefficient scan of pvp (embodied by ncp) to look
2424 * for dvp. This will create a namecache record for dvp on
2425 * success. We loop up to recheck on success.
2427 * ncp and dvp are both held but not locked.
2429 error
= cache_inefficient_scan(nch
, cred
, dvp
, fakename
);
2431 kprintf("cache_fromdvp: scan %p (%s) failed on dvp=%p\n",
2432 pvp
, nch
->ncp
->nc_name
, dvp
);
2434 /* nch was NULLed out, reload mount */
2435 nch
->mount
= dvp
->v_mount
;
2439 kprintf("cache_fromdvp: scan %p (%s) succeeded\n",
2440 pvp
, nch
->ncp
->nc_name
);
2443 /* nch was NULLed out, reload mount */
2444 nch
->mount
= dvp
->v_mount
;
2448 * If nch->ncp is non-NULL it will have been held already.
2451 kfree(fakename
, M_TEMP
);
2460 * Go up the chain of parent directories until we find something
2461 * we can resolve into the namecache. This is very inefficient.
2465 cache_fromdvp_try(struct vnode
*dvp
, struct ucred
*cred
,
2466 struct vnode
**saved_dvp
)
2468 struct nchandle nch
;
2471 static time_t last_fromdvp_report
;
2475 * Loop getting the parent directory vnode until we get something we
2476 * can resolve in the namecache.
2479 nch
.mount
= dvp
->v_mount
;
2485 kfree(fakename
, M_TEMP
);
2488 error
= vop_nlookupdotdot(*dvp
->v_ops
, dvp
, &pvp
, cred
,
2495 spin_lock_shared(&pvp
->v_spin
);
2496 if ((nch
.ncp
= TAILQ_FIRST(&pvp
->v_namecache
)) != NULL
) {
2497 _cache_hold(nch
.ncp
);
2498 spin_unlock_shared(&pvp
->v_spin
);
2502 spin_unlock_shared(&pvp
->v_spin
);
2503 if (pvp
->v_flag
& VROOT
) {
2504 nch
.ncp
= _cache_get(pvp
->v_mount
->mnt_ncmountpt
.ncp
);
2505 error
= cache_resolve_mp(nch
.mount
);
2506 _cache_unlock(nch
.ncp
);
2509 _cache_drop(nch
.ncp
);
2519 if (last_fromdvp_report
!= time_uptime
) {
2520 last_fromdvp_report
= time_uptime
;
2521 kprintf("Warning: extremely inefficient path "
2522 "resolution on %s\n",
2525 error
= cache_inefficient_scan(&nch
, cred
, dvp
, fakename
);
2528 * Hopefully dvp now has a namecache record associated with
2529 * it. Leave it referenced to prevent the kernel from
2530 * recycling the vnode. Otherwise extremely long directory
2531 * paths could result in endless recycling.
2536 _cache_drop(nch
.ncp
);
2539 kfree(fakename
, M_TEMP
);
2544 * Do an inefficient scan of the directory represented by ncp looking for
2545 * the directory vnode dvp. ncp must be held but not locked on entry and
2546 * will be held on return. dvp must be refd but not locked on entry and
2547 * will remain refd on return.
2549 * Why do this at all? Well, due to its stateless nature the NFS server
2550 * converts file handles directly to vnodes without necessarily going through
2551 * the namecache ops that would otherwise create the namecache topology
2552 * leading to the vnode. We could either (1) Change the namecache algorithms
2553 * to allow disconnect namecache records that are re-merged opportunistically,
2554 * or (2) Make the NFS server backtrack and scan to recover a connected
2555 * namecache topology in order to then be able to issue new API lookups.
2557 * It turns out that (1) is a huge mess. It takes a nice clean set of
2558 * namecache algorithms and introduces a lot of complication in every subsystem
2559 * that calls into the namecache to deal with the re-merge case, especially
2560 * since we are using the namecache to placehold negative lookups and the
2561 * vnode might not be immediately assigned. (2) is certainly far less
2562 * efficient then (1), but since we are only talking about directories here
2563 * (which are likely to remain cached), the case does not actually run all
2564 * that often and has the supreme advantage of not polluting the namecache
2567 * If a fakename is supplied just construct a namecache entry using the
2571 cache_inefficient_scan(struct nchandle
*nch
, struct ucred
*cred
,
2572 struct vnode
*dvp
, char *fakename
)
2574 struct nlcomponent nlc
;
2575 struct nchandle rncp
;
2587 vat
.va_blocksize
= 0;
2588 if ((error
= VOP_GETATTR(dvp
, &vat
)) != 0)
2591 error
= cache_vref(nch
, cred
, &pvp
);
2596 kprintf("inefficient_scan of (%p,%s): directory iosize %ld "
2597 "vattr fileid = %lld\n",
2598 nch
->ncp
, nch
->ncp
->nc_name
,
2600 (long long)vat
.va_fileid
);
2604 * Use the supplied fakename if not NULL. Fake names are typically
2605 * not in the actual filesystem hierarchy. This is used by HAMMER
2606 * to glue @@timestamp recursions together.
2609 nlc
.nlc_nameptr
= fakename
;
2610 nlc
.nlc_namelen
= strlen(fakename
);
2611 rncp
= cache_nlookup(nch
, &nlc
);
2615 if ((blksize
= vat
.va_blocksize
) == 0)
2616 blksize
= DEV_BSIZE
;
2617 rbuf
= kmalloc(blksize
, M_TEMP
, M_WAITOK
);
2623 iov
.iov_base
= rbuf
;
2624 iov
.iov_len
= blksize
;
2627 uio
.uio_resid
= blksize
;
2628 uio
.uio_segflg
= UIO_SYSSPACE
;
2629 uio
.uio_rw
= UIO_READ
;
2630 uio
.uio_td
= curthread
;
2632 if (ncvp_debug
>= 2)
2633 kprintf("cache_inefficient_scan: readdir @ %08x\n", (int)uio
.uio_offset
);
2634 error
= VOP_READDIR(pvp
, &uio
, cred
, &eofflag
, NULL
, NULL
);
2636 den
= (struct dirent
*)rbuf
;
2637 bytes
= blksize
- uio
.uio_resid
;
2640 if (ncvp_debug
>= 2) {
2641 kprintf("cache_inefficient_scan: %*.*s\n",
2642 den
->d_namlen
, den
->d_namlen
,
2645 if (den
->d_type
!= DT_WHT
&&
2646 den
->d_ino
== vat
.va_fileid
) {
2648 kprintf("cache_inefficient_scan: "
2649 "MATCHED inode %lld path %s/%*.*s\n",
2650 (long long)vat
.va_fileid
,
2652 den
->d_namlen
, den
->d_namlen
,
2655 nlc
.nlc_nameptr
= den
->d_name
;
2656 nlc
.nlc_namelen
= den
->d_namlen
;
2657 rncp
= cache_nlookup(nch
, &nlc
);
2658 KKASSERT(rncp
.ncp
!= NULL
);
2661 bytes
-= _DIRENT_DIRSIZ(den
);
2662 den
= _DIRENT_NEXT(den
);
2664 if (rncp
.ncp
== NULL
&& eofflag
== 0 && uio
.uio_resid
!= blksize
)
2667 kfree(rbuf
, M_TEMP
);
2671 if (rncp
.ncp
->nc_flag
& NCF_UNRESOLVED
) {
2672 _cache_setvp(rncp
.mount
, rncp
.ncp
, dvp
);
2673 if (ncvp_debug
>= 2) {
2674 kprintf("cache_inefficient_scan: setvp %s/%s = %p\n",
2675 nch
->ncp
->nc_name
, rncp
.ncp
->nc_name
, dvp
);
2678 if (ncvp_debug
>= 2) {
2679 kprintf("cache_inefficient_scan: setvp %s/%s already set %p/%p\n",
2680 nch
->ncp
->nc_name
, rncp
.ncp
->nc_name
, dvp
,
2684 if (rncp
.ncp
->nc_vp
== NULL
)
2685 error
= rncp
.ncp
->nc_error
;
2687 * Release rncp after a successful nlookup. rncp was fully
2692 kprintf("cache_inefficient_scan: dvp %p NOT FOUND in %s\n",
2693 dvp
, nch
->ncp
->nc_name
);
2700 * Zap a namecache entry. The ncp is unconditionally set to an unresolved
2701 * state, which disassociates it from its vnode or pcpu_ncache[n].neg_list.
2703 * Then, if there are no additional references to the ncp and no children,
2704 * the ncp is removed from the topology and destroyed.
2706 * References and/or children may exist if the ncp is in the middle of the
2707 * topology, preventing the ncp from being destroyed.
2709 * This function must be called with the ncp held and locked and will unlock
2710 * and drop it during zapping.
2712 * If nonblock is non-zero and the parent ncp cannot be locked we give up.
2713 * This case can occur in the cache_drop() path.
2715 * This function may returned a held (but NOT locked) parent node which the
2716 * caller must drop. We do this so _cache_drop() can loop, to avoid
2717 * blowing out the kernel stack.
2719 * WARNING! For MPSAFE operation this routine must acquire up to three
2720 * spin locks to be able to safely test nc_refs. Lock order is
2723 * hash spinlock if on hash list
2724 * parent spinlock if child of parent
2725 * (the ncp is unresolved so there is no vnode association)
2727 static struct namecache
*
2728 cache_zap(struct namecache
*ncp
, int nonblock
)
2730 struct namecache
*par
;
2731 struct vnode
*dropvp
;
2732 struct nchash_head
*nchpp
;
2736 * Disassociate the vnode or negative cache ref and set NCF_UNRESOLVED.
2738 _cache_setunresolved(ncp
);
2741 * Try to scrap the entry and possibly tail-recurse on its parent.
2742 * We only scrap unref'd (other then our ref) unresolved entries,
2743 * we do not scrap 'live' entries.
2745 * Note that once the spinlocks are acquired if nc_refs == 1 no
2746 * other references are possible. If it isn't, however, we have
2747 * to decrement but also be sure to avoid a 1->0 transition.
2749 KKASSERT(ncp
->nc_flag
& NCF_UNRESOLVED
);
2750 KKASSERT(ncp
->nc_refs
> 0);
2753 * Acquire locks. Note that the parent can't go away while we hold
2757 if ((par
= ncp
->nc_parent
) != NULL
) {
2760 if (_cache_lock_nonblock(par
) == 0)
2762 refs
= ncp
->nc_refs
;
2763 ncp
->nc_flag
|= NCF_DEFEREDZAP
;
2764 ++numdefered
; /* MP race ok */
2765 if (atomic_cmpset_int(&ncp
->nc_refs
,
2777 nchpp
= ncp
->nc_head
;
2778 spin_lock(&nchpp
->spin
);
2782 * At this point if we find refs == 1 it should not be possible for
2783 * anyone else to have access to the ncp. We are holding the only
2784 * possible access point left (nchpp) spin-locked.
2786 * If someone other then us has a ref or we have children
2787 * we cannot zap the entry. The 1->0 transition and any
2788 * further list operation is protected by the spinlocks
2789 * we have acquired but other transitions are not.
2792 refs
= ncp
->nc_refs
;
2794 if (refs
== 1 && TAILQ_EMPTY(&ncp
->nc_list
))
2796 if (atomic_cmpset_int(&ncp
->nc_refs
, refs
, refs
- 1)) {
2798 spin_unlock(&nchpp
->spin
);
2808 * We are the only ref and with the spinlocks held no further
2809 * refs can be acquired by others.
2811 * Remove us from the hash list and parent list. We have to
2812 * drop a ref on the parent's vp if the parent's list becomes
2817 struct pcpu_ncache
*pn
= &pcpu_ncache
[mycpu
->gd_cpuid
];
2819 KKASSERT(nchpp
== ncp
->nc_head
);
2820 LIST_REMOVE(ncp
, nc_hash
);
2821 TAILQ_REMOVE(&par
->nc_list
, ncp
, nc_entry
);
2822 atomic_add_long(&pn
->vfscache_count
, -1);
2823 if (TAILQ_EMPTY(&ncp
->nc_list
))
2824 atomic_add_long(&pn
->vfscache_leafs
, -1);
2826 if (TAILQ_EMPTY(&par
->nc_list
)) {
2827 atomic_add_long(&pn
->vfscache_leafs
, 1);
2829 dropvp
= par
->nc_vp
;
2831 ncp
->nc_head
= NULL
;
2832 ncp
->nc_parent
= NULL
;
2833 spin_unlock(&nchpp
->spin
);
2836 KKASSERT(ncp
->nc_head
== NULL
);
2840 * ncp should not have picked up any refs. Physically
2843 if (ncp
->nc_refs
!= 1) {
2844 int save_refs
= ncp
->nc_refs
;
2846 panic("cache_zap: %p bad refs %d (%d)\n",
2847 ncp
, save_refs
, atomic_fetchadd_int(&ncp
->nc_refs
, 0));
2849 KKASSERT(ncp
->nc_refs
== 1);
2850 /* _cache_unlock(ncp) not required */
2851 ncp
->nc_refs
= -1; /* safety */
2853 kfree(ncp
->nc_name
, M_VFSCACHE
);
2854 kfree(ncp
, M_VFSCACHE
);
2857 * Delayed drop (we had to release our spinlocks)
2859 * The refed parent (if not NULL) must be dropped. The
2860 * caller is responsible for looping.
2868 * Clean up dangling negative cache and defered-drop entries in the
2871 * This routine is called in the critical path and also called from
2872 * vnlru(). When called from vnlru we use a lower limit to try to
2873 * deal with the negative cache before the critical path has to start
2876 typedef enum { CHI_LOW
, CHI_HIGH
} cache_hs_t
;
2878 static cache_hs_t neg_cache_hysteresis_state
[2] = { CHI_LOW
, CHI_LOW
};
2879 static cache_hs_t pos_cache_hysteresis_state
[2] = { CHI_LOW
, CHI_LOW
};
2882 cache_hysteresis(int critpath
)
2885 long neglimit
= maxvnodes
/ ncnegfactor
;
2886 long xnumcache
= vfscache_leafs
;
2889 neglimit
= neglimit
* 8 / 10;
2892 * Don't cache too many negative hits. We use hysteresis to reduce
2893 * the impact on the critical path.
2895 switch(neg_cache_hysteresis_state
[critpath
]) {
2897 if (vfscache_negs
> MINNEG
&& vfscache_negs
> neglimit
) {
2899 _cache_cleanneg(ncnegflush
);
2901 _cache_cleanneg(ncnegflush
+
2902 vfscache_negs
- neglimit
);
2903 neg_cache_hysteresis_state
[critpath
] = CHI_HIGH
;
2907 if (vfscache_negs
> MINNEG
* 9 / 10 &&
2908 vfscache_negs
* 9 / 10 > neglimit
2911 _cache_cleanneg(ncnegflush
);
2913 _cache_cleanneg(ncnegflush
+
2914 vfscache_negs
* 9 / 10 -
2917 neg_cache_hysteresis_state
[critpath
] = CHI_LOW
;
2923 * Don't cache too many positive hits. We use hysteresis to reduce
2924 * the impact on the critical path.
2926 * Excessive positive hits can accumulate due to large numbers of
2927 * hardlinks (the vnode cache will not prevent hl ncps from growing
2930 if ((poslimit
= ncposlimit
) == 0)
2931 poslimit
= maxvnodes
* 2;
2933 poslimit
= poslimit
* 8 / 10;
2935 switch(pos_cache_hysteresis_state
[critpath
]) {
2937 if (xnumcache
> poslimit
&& xnumcache
> MINPOS
) {
2939 _cache_cleanpos(ncposflush
);
2941 _cache_cleanpos(ncposflush
+
2942 xnumcache
- poslimit
);
2943 pos_cache_hysteresis_state
[critpath
] = CHI_HIGH
;
2947 if (xnumcache
> poslimit
* 5 / 6 && xnumcache
> MINPOS
) {
2949 _cache_cleanpos(ncposflush
);
2951 _cache_cleanpos(ncposflush
+
2952 xnumcache
- poslimit
* 5 / 6);
2954 pos_cache_hysteresis_state
[critpath
] = CHI_LOW
;
2960 * Clean out dangling defered-zap ncps which could not
2961 * be cleanly dropped if too many build up. Note
2962 * that numdefered is not an exact number as such ncps
2963 * can be reused and the counter is not handled in a MP
2964 * safe manner by design.
2966 if (numdefered
> neglimit
) {
2967 _cache_cleandefered();
2972 * NEW NAMECACHE LOOKUP API
2974 * Lookup an entry in the namecache. The passed par_nch must be referenced
2975 * and unlocked. A referenced and locked nchandle with a non-NULL nch.ncp
2976 * is ALWAYS returned, eve if the supplied component is illegal.
2978 * The resulting namecache entry should be returned to the system with
2979 * cache_put() or cache_unlock() + cache_drop().
2981 * namecache locks are recursive but care must be taken to avoid lock order
2982 * reversals (hence why the passed par_nch must be unlocked). Locking
2983 * rules are to order for parent traversals, not for child traversals.
2985 * Nobody else will be able to manipulate the associated namespace (e.g.
2986 * create, delete, rename, rename-target) until the caller unlocks the
2989 * The returned entry will be in one of three states: positive hit (non-null
2990 * vnode), negative hit (null vnode), or unresolved (NCF_UNRESOLVED is set).
2991 * Unresolved entries must be resolved through the filesystem to associate the
2992 * vnode and/or determine whether a positive or negative hit has occured.
2994 * It is not necessary to lock a directory in order to lock namespace under
2995 * that directory. In fact, it is explicitly not allowed to do that. A
2996 * directory is typically only locked when being created, renamed, or
2999 * The directory (par) may be unresolved, in which case any returned child
3000 * will likely also be marked unresolved. Likely but not guarenteed. Since
3001 * the filesystem lookup requires a resolved directory vnode the caller is
3002 * responsible for resolving the namecache chain top-down. This API
3003 * specifically allows whole chains to be created in an unresolved state.
3006 cache_nlookup(struct nchandle
*par_nch
, struct nlcomponent
*nlc
)
3008 struct nchandle nch
;
3009 struct namecache
*ncp
;
3010 struct namecache
*new_ncp
;
3011 struct nchash_head
*nchpp
;
3018 mp
= par_nch
->mount
;
3022 * This is a good time to call it, no ncp's are locked by
3025 cache_hysteresis(1);
3028 * Try to locate an existing entry
3030 hash
= fnv_32_buf(nlc
->nlc_nameptr
, nlc
->nlc_namelen
, FNV1_32_INIT
);
3031 hash
= fnv_32_buf(&par_nch
->ncp
, sizeof(par_nch
->ncp
), hash
);
3033 nchpp
= NCHHASH(hash
);
3036 spin_lock(&nchpp
->spin
);
3038 spin_lock_shared(&nchpp
->spin
);
3040 LIST_FOREACH(ncp
, &nchpp
->list
, nc_hash
) {
3042 * Break out if we find a matching entry. Note that
3043 * UNRESOLVED entries may match, but DESTROYED entries
3046 if (ncp
->nc_parent
== par_nch
->ncp
&&
3047 ncp
->nc_nlen
== nlc
->nlc_namelen
&&
3048 bcmp(ncp
->nc_name
, nlc
->nlc_nameptr
, ncp
->nc_nlen
) == 0 &&
3049 (ncp
->nc_flag
& NCF_DESTROYED
) == 0
3053 spin_unlock(&nchpp
->spin
);
3055 spin_unlock_shared(&nchpp
->spin
);
3057 _cache_unlock(par_nch
->ncp
);
3060 if (_cache_lock_special(ncp
) == 0) {
3062 * Successfully locked but we must re-test
3063 * conditions that might have changed since
3064 * we did not have the lock before.
3066 if (ncp
->nc_parent
!= par_nch
->ncp
||
3067 ncp
->nc_nlen
!= nlc
->nlc_namelen
||
3068 bcmp(ncp
->nc_name
, nlc
->nlc_nameptr
,
3070 (ncp
->nc_flag
& NCF_DESTROYED
)) {
3074 _cache_auto_unresolve(mp
, ncp
);
3076 _cache_free(new_ncp
);
3079 _cache_get(ncp
); /* cycle the lock to block */
3087 * We failed to locate an entry, create a new entry and add it to
3088 * the cache. The parent ncp must also be locked so we
3091 * We have to relookup after possibly blocking in kmalloc or
3092 * when locking par_nch.
3094 * NOTE: nlc_namelen can be 0 and nlc_nameptr NULL as a special
3095 * mount case, in which case nc_name will be NULL.
3097 if (new_ncp
== NULL
) {
3098 spin_unlock_shared(&nchpp
->spin
);
3099 new_ncp
= cache_alloc(nlc
->nlc_namelen
);
3100 if (nlc
->nlc_namelen
) {
3101 bcopy(nlc
->nlc_nameptr
, new_ncp
->nc_name
,
3103 new_ncp
->nc_name
[nlc
->nlc_namelen
] = 0;
3109 * NOTE! The spinlock is held exclusively here because new_ncp
3112 if (par_locked
== 0) {
3113 spin_unlock(&nchpp
->spin
);
3114 _cache_lock(par_nch
->ncp
);
3120 * WARNING! We still hold the spinlock. We have to set the hash
3121 * table entry atomically.
3124 _cache_link_parent(ncp
, par_nch
->ncp
, nchpp
);
3125 spin_unlock(&nchpp
->spin
);
3126 _cache_unlock(par_nch
->ncp
);
3127 /* par_locked = 0 - not used */
3130 * stats and namecache size management
3132 if (ncp
->nc_flag
& NCF_UNRESOLVED
)
3133 ++gd
->gd_nchstats
->ncs_miss
;
3134 else if (ncp
->nc_vp
)
3135 ++gd
->gd_nchstats
->ncs_goodhits
;
3137 ++gd
->gd_nchstats
->ncs_neghits
;
3140 _cache_mntref(nch
.mount
);
3146 * Attempt to lookup a namecache entry and return with a shared namecache
3150 cache_nlookup_maybe_shared(struct nchandle
*par_nch
, struct nlcomponent
*nlc
,
3151 int excl
, struct nchandle
*res_nch
)
3153 struct namecache
*ncp
;
3154 struct nchash_head
*nchpp
;
3160 * If exclusive requested or shared namecache locks are disabled,
3163 if (ncp_shared_lock_disable
|| excl
)
3164 return(EWOULDBLOCK
);
3167 mp
= par_nch
->mount
;
3170 * This is a good time to call it, no ncp's are locked by
3173 cache_hysteresis(1);
3176 * Try to locate an existing entry
3178 hash
= fnv_32_buf(nlc
->nlc_nameptr
, nlc
->nlc_namelen
, FNV1_32_INIT
);
3179 hash
= fnv_32_buf(&par_nch
->ncp
, sizeof(par_nch
->ncp
), hash
);
3180 nchpp
= NCHHASH(hash
);
3182 spin_lock_shared(&nchpp
->spin
);
3184 LIST_FOREACH(ncp
, &nchpp
->list
, nc_hash
) {
3186 * Break out if we find a matching entry. Note that
3187 * UNRESOLVED entries may match, but DESTROYED entries
3190 if (ncp
->nc_parent
== par_nch
->ncp
&&
3191 ncp
->nc_nlen
== nlc
->nlc_namelen
&&
3192 bcmp(ncp
->nc_name
, nlc
->nlc_nameptr
, ncp
->nc_nlen
) == 0 &&
3193 (ncp
->nc_flag
& NCF_DESTROYED
) == 0
3196 spin_unlock_shared(&nchpp
->spin
);
3197 if (_cache_lock_shared_special(ncp
) == 0) {
3198 if (ncp
->nc_parent
== par_nch
->ncp
&&
3199 ncp
->nc_nlen
== nlc
->nlc_namelen
&&
3200 bcmp(ncp
->nc_name
, nlc
->nlc_nameptr
,
3201 ncp
->nc_nlen
) == 0 &&
3202 (ncp
->nc_flag
& NCF_DESTROYED
) == 0 &&
3203 (ncp
->nc_flag
& NCF_UNRESOLVED
) == 0 &&
3204 _cache_auto_unresolve_test(mp
, ncp
) == 0) {
3210 spin_lock_shared(&nchpp
->spin
);
3218 spin_unlock_shared(&nchpp
->spin
);
3219 return(EWOULDBLOCK
);
3224 * Note that nc_error might be non-zero (e.g ENOENT).
3227 res_nch
->mount
= mp
;
3229 ++gd
->gd_nchstats
->ncs_goodhits
;
3230 _cache_mntref(res_nch
->mount
);
3232 KKASSERT(ncp
->nc_error
!= EWOULDBLOCK
);
3233 return(ncp
->nc_error
);
3237 * This is a non-blocking verison of cache_nlookup() used by
3238 * nfs_readdirplusrpc_uio(). It can fail for any reason and
3239 * will return nch.ncp == NULL in that case.
3242 cache_nlookup_nonblock(struct nchandle
*par_nch
, struct nlcomponent
*nlc
)
3244 struct nchandle nch
;
3245 struct namecache
*ncp
;
3246 struct namecache
*new_ncp
;
3247 struct nchash_head
*nchpp
;
3254 mp
= par_nch
->mount
;
3258 * Try to locate an existing entry
3260 hash
= fnv_32_buf(nlc
->nlc_nameptr
, nlc
->nlc_namelen
, FNV1_32_INIT
);
3261 hash
= fnv_32_buf(&par_nch
->ncp
, sizeof(par_nch
->ncp
), hash
);
3263 nchpp
= NCHHASH(hash
);
3265 spin_lock(&nchpp
->spin
);
3266 LIST_FOREACH(ncp
, &nchpp
->list
, nc_hash
) {
3268 * Break out if we find a matching entry. Note that
3269 * UNRESOLVED entries may match, but DESTROYED entries
3272 if (ncp
->nc_parent
== par_nch
->ncp
&&
3273 ncp
->nc_nlen
== nlc
->nlc_namelen
&&
3274 bcmp(ncp
->nc_name
, nlc
->nlc_nameptr
, ncp
->nc_nlen
) == 0 &&
3275 (ncp
->nc_flag
& NCF_DESTROYED
) == 0
3278 spin_unlock(&nchpp
->spin
);
3280 _cache_unlock(par_nch
->ncp
);
3283 if (_cache_lock_special(ncp
) == 0) {
3284 if (ncp
->nc_parent
!= par_nch
->ncp
||
3285 ncp
->nc_nlen
!= nlc
->nlc_namelen
||
3286 bcmp(ncp
->nc_name
, nlc
->nlc_nameptr
, ncp
->nc_nlen
) ||
3287 (ncp
->nc_flag
& NCF_DESTROYED
)) {
3288 kprintf("cache_lookup_nonblock: "
3289 "ncp-race %p %*.*s\n",
3298 _cache_auto_unresolve(mp
, ncp
);
3300 _cache_free(new_ncp
);
3311 * We failed to locate an entry, create a new entry and add it to
3312 * the cache. The parent ncp must also be locked so we
3315 * We have to relookup after possibly blocking in kmalloc or
3316 * when locking par_nch.
3318 * NOTE: nlc_namelen can be 0 and nlc_nameptr NULL as a special
3319 * mount case, in which case nc_name will be NULL.
3321 if (new_ncp
== NULL
) {
3322 spin_unlock(&nchpp
->spin
);
3323 new_ncp
= cache_alloc(nlc
->nlc_namelen
);
3324 if (nlc
->nlc_namelen
) {
3325 bcopy(nlc
->nlc_nameptr
, new_ncp
->nc_name
,
3327 new_ncp
->nc_name
[nlc
->nlc_namelen
] = 0;
3331 if (par_locked
== 0) {
3332 spin_unlock(&nchpp
->spin
);
3333 if (_cache_lock_nonblock(par_nch
->ncp
) == 0) {
3341 * WARNING! We still hold the spinlock. We have to set the hash
3342 * table entry atomically.
3345 _cache_link_parent(ncp
, par_nch
->ncp
, nchpp
);
3346 spin_unlock(&nchpp
->spin
);
3347 _cache_unlock(par_nch
->ncp
);
3348 /* par_locked = 0 - not used */
3351 * stats and namecache size management
3353 if (ncp
->nc_flag
& NCF_UNRESOLVED
)
3354 ++gd
->gd_nchstats
->ncs_miss
;
3355 else if (ncp
->nc_vp
)
3356 ++gd
->gd_nchstats
->ncs_goodhits
;
3358 ++gd
->gd_nchstats
->ncs_neghits
;
3361 _cache_mntref(nch
.mount
);
3366 _cache_free(new_ncp
);
3375 * The namecache entry is marked as being used as a mount point.
3376 * Locate the mount if it is visible to the caller. The DragonFly
3377 * mount system allows arbitrary loops in the topology and disentangles
3378 * those loops by matching against (mp, ncp) rather than just (ncp).
3379 * This means any given ncp can dive any number of mounts, depending
3380 * on the relative mount (e.g. nullfs) the caller is at in the topology.
3382 * We use a very simple frontend cache to reduce SMP conflicts,
3383 * which we have to do because the mountlist scan needs an exclusive
3384 * lock around its ripout info list. Not to mention that there might
3385 * be a lot of mounts.
3387 struct findmount_info
{
3388 struct mount
*result
;
3389 struct mount
*nch_mount
;
3390 struct namecache
*nch_ncp
;
3393 #define MNTCACHE_PRIME 66555444443333333ULL
3396 struct ncmount_cache
*
3397 ncmount_cache_lookup(struct mount
*mp
, struct namecache
*ncp
)
3401 hash
= (uintptr_t)mp
+ ((uintptr_t)mp
>> 18);
3402 hash
%= MNTCACHE_PRIME
;
3403 hash
^= (uintptr_t)ncp
+ ((uintptr_t)ncp
>> 18);
3404 hash
%= MNTCACHE_PRIME
;
3405 hash
= hash
% NCMOUNT_NUMCACHE
;
3407 return (&ncmount_cache
[hash
]);
3412 cache_findmount_callback(struct mount
*mp
, void *data
)
3414 struct findmount_info
*info
= data
;
3417 * Check the mount's mounted-on point against the passed nch.
3419 if (mp
->mnt_ncmounton
.mount
== info
->nch_mount
&&
3420 mp
->mnt_ncmounton
.ncp
== info
->nch_ncp
3430 cache_findmount(struct nchandle
*nch
)
3432 struct findmount_info info
;
3433 struct ncmount_cache
*ncc
;
3439 if (ncmount_cache_enable
== 0) {
3443 ncc
= ncmount_cache_lookup(nch
->mount
, nch
->ncp
);
3444 if (ncc
->ncp
== nch
->ncp
) {
3445 spin_lock_shared(&ncc
->spin
);
3446 if (ncc
->isneg
== 0 &&
3447 ncc
->ncp
== nch
->ncp
&& (mp
= ncc
->mp
) != NULL
) {
3448 if (mp
->mnt_ncmounton
.mount
== nch
->mount
&&
3449 mp
->mnt_ncmounton
.ncp
== nch
->ncp
) {
3451 * Cache hit (positive)
3454 spin_unlock_shared(&ncc
->spin
);
3457 /* else cache miss */
3460 ncc
->ncp
== nch
->ncp
&& ncc
->mp
== nch
->mount
) {
3462 * Cache hit (negative)
3464 spin_unlock_shared(&ncc
->spin
);
3467 spin_unlock_shared(&ncc
->spin
);
3475 info
.nch_mount
= nch
->mount
;
3476 info
.nch_ncp
= nch
->ncp
;
3477 mountlist_scan(cache_findmount_callback
, &info
,
3478 MNTSCAN_FORWARD
|MNTSCAN_NOBUSY
);
3483 * Negative lookups: We cache the originating {ncp,mp}. (mp) is
3484 * only used for pointer comparisons and is not
3485 * referenced (otherwise there would be dangling
3488 * Positive lookups: We cache the originating {ncp} and the target
3489 * (mp). (mp) is referenced.
3491 * Indeterminant: If the match is undergoing an unmount we do
3492 * not cache it to avoid racing cache_unmounting(),
3493 * but still return the match.
3496 spin_lock(&ncc
->spin
);
3497 if (info
.result
== NULL
) {
3498 if (ncc
->isneg
== 0 && ncc
->mp
)
3499 _cache_mntrel(ncc
->mp
);
3500 ncc
->ncp
= nch
->ncp
;
3501 ncc
->mp
= nch
->mount
;
3503 spin_unlock(&ncc
->spin
);
3504 } else if ((info
.result
->mnt_kern_flag
& MNTK_UNMOUNT
) == 0) {
3505 if (ncc
->isneg
== 0 && ncc
->mp
)
3506 _cache_mntrel(ncc
->mp
);
3507 _cache_mntref(info
.result
);
3508 ncc
->ncp
= nch
->ncp
;
3509 ncc
->mp
= info
.result
;
3511 spin_unlock(&ncc
->spin
);
3513 spin_unlock(&ncc
->spin
);
3516 return(info
.result
);
3520 cache_dropmount(struct mount
*mp
)
3526 cache_ismounting(struct mount
*mp
)
3528 struct nchandle
*nch
= &mp
->mnt_ncmounton
;
3529 struct ncmount_cache
*ncc
;
3531 ncc
= ncmount_cache_lookup(nch
->mount
, nch
->ncp
);
3533 ncc
->ncp
== nch
->ncp
&& ncc
->mp
== nch
->mount
) {
3534 spin_lock(&ncc
->spin
);
3536 ncc
->ncp
== nch
->ncp
&& ncc
->mp
== nch
->mount
) {
3540 spin_unlock(&ncc
->spin
);
3545 cache_unmounting(struct mount
*mp
)
3547 struct nchandle
*nch
= &mp
->mnt_ncmounton
;
3548 struct ncmount_cache
*ncc
;
3550 ncc
= ncmount_cache_lookup(nch
->mount
, nch
->ncp
);
3551 if (ncc
->isneg
== 0 &&
3552 ncc
->ncp
== nch
->ncp
&& ncc
->mp
== mp
) {
3553 spin_lock(&ncc
->spin
);
3554 if (ncc
->isneg
== 0 &&
3555 ncc
->ncp
== nch
->ncp
&& ncc
->mp
== mp
) {
3560 spin_unlock(&ncc
->spin
);
3565 * Resolve an unresolved namecache entry, generally by looking it up.
3566 * The passed ncp must be locked and refd.
3568 * Theoretically since a vnode cannot be recycled while held, and since
3569 * the nc_parent chain holds its vnode as long as children exist, the
3570 * direct parent of the cache entry we are trying to resolve should
3571 * have a valid vnode. If not then generate an error that we can
3572 * determine is related to a resolver bug.
3574 * However, if a vnode was in the middle of a recyclement when the NCP
3575 * got locked, ncp->nc_vp might point to a vnode that is about to become
3576 * invalid. cache_resolve() handles this case by unresolving the entry
3577 * and then re-resolving it.
3579 * Note that successful resolution does not necessarily return an error
3580 * code of 0. If the ncp resolves to a negative cache hit then ENOENT
3584 cache_resolve(struct nchandle
*nch
, struct ucred
*cred
)
3586 struct namecache
*par_tmp
;
3587 struct namecache
*par
;
3588 struct namecache
*ncp
;
3589 struct nchandle nctmp
;
3596 KKASSERT(_cache_lockstatus(ncp
) == LK_EXCLUSIVE
);
3599 * If the ncp is already resolved we have nothing to do. However,
3600 * we do want to guarentee that a usable vnode is returned when
3601 * a vnode is present, so make sure it hasn't been reclaimed.
3603 if ((ncp
->nc_flag
& NCF_UNRESOLVED
) == 0) {
3604 if (ncp
->nc_vp
&& (ncp
->nc_vp
->v_flag
& VRECLAIMED
))
3605 _cache_setunresolved(ncp
);
3606 if ((ncp
->nc_flag
& NCF_UNRESOLVED
) == 0)
3607 return (ncp
->nc_error
);
3611 * If the ncp was destroyed it will never resolve again. This
3612 * can basically only happen when someone is chdir'd into an
3613 * empty directory which is then rmdir'd. We want to catch this
3614 * here and not dive the VFS because the VFS might actually
3615 * have a way to re-resolve the disconnected ncp, which will
3616 * result in inconsistencies in the cdir/nch for proc->p_fd.
3618 if (ncp
->nc_flag
& NCF_DESTROYED
)
3622 * Mount points need special handling because the parent does not
3623 * belong to the same filesystem as the ncp.
3625 if (ncp
== mp
->mnt_ncmountpt
.ncp
)
3626 return (cache_resolve_mp(mp
));
3629 * We expect an unbroken chain of ncps to at least the mount point,
3630 * and even all the way to root (but this code doesn't have to go
3631 * past the mount point).
3633 if (ncp
->nc_parent
== NULL
) {
3634 kprintf("EXDEV case 1 %p %*.*s\n", ncp
,
3635 ncp
->nc_nlen
, ncp
->nc_nlen
, ncp
->nc_name
);
3636 ncp
->nc_error
= EXDEV
;
3637 return(ncp
->nc_error
);
3641 * The vp's of the parent directories in the chain are held via vhold()
3642 * due to the existance of the child, and should not disappear.
3643 * However, there are cases where they can disappear:
3645 * - due to filesystem I/O errors.
3646 * - due to NFS being stupid about tracking the namespace and
3647 * destroys the namespace for entire directories quite often.
3648 * - due to forced unmounts.
3649 * - due to an rmdir (parent will be marked DESTROYED)
3651 * When this occurs we have to track the chain backwards and resolve
3652 * it, looping until the resolver catches up to the current node. We
3653 * could recurse here but we might run ourselves out of kernel stack
3654 * so we do it in a more painful manner. This situation really should
3655 * not occur all that often, or if it does not have to go back too
3656 * many nodes to resolve the ncp.
3658 while ((dvp
= cache_dvpref(ncp
)) == NULL
) {
3660 * This case can occur if a process is CD'd into a
3661 * directory which is then rmdir'd. If the parent is marked
3662 * destroyed there is no point trying to resolve it.
3664 if (ncp
->nc_parent
->nc_flag
& NCF_DESTROYED
)
3666 par
= ncp
->nc_parent
;
3669 while ((par_tmp
= par
->nc_parent
) != NULL
&&
3670 par_tmp
->nc_vp
== NULL
) {
3671 _cache_hold(par_tmp
);
3672 _cache_lock(par_tmp
);
3676 if (par
->nc_parent
== NULL
) {
3677 kprintf("EXDEV case 2 %*.*s\n",
3678 par
->nc_nlen
, par
->nc_nlen
, par
->nc_name
);
3683 * The parent is not set in stone, ref and lock it to prevent
3684 * it from disappearing. Also note that due to renames it
3685 * is possible for our ncp to move and for par to no longer
3686 * be one of its parents. We resolve it anyway, the loop
3687 * will handle any moves.
3689 _cache_get(par
); /* additional hold/lock */
3690 _cache_put(par
); /* from earlier hold/lock */
3691 if (par
== nch
->mount
->mnt_ncmountpt
.ncp
) {
3692 cache_resolve_mp(nch
->mount
);
3693 } else if ((dvp
= cache_dvpref(par
)) == NULL
) {
3694 kprintf("[diagnostic] cache_resolve: raced on %*.*s\n", par
->nc_nlen
, par
->nc_nlen
, par
->nc_name
);
3698 if (par
->nc_flag
& NCF_UNRESOLVED
) {
3701 par
->nc_error
= VOP_NRESOLVE(&nctmp
, dvp
, cred
);
3705 if ((error
= par
->nc_error
) != 0) {
3706 if (par
->nc_error
!= EAGAIN
) {
3707 kprintf("EXDEV case 3 %*.*s error %d\n",
3708 par
->nc_nlen
, par
->nc_nlen
, par
->nc_name
,
3713 kprintf("[diagnostic] cache_resolve: EAGAIN par %p %*.*s\n",
3714 par
, par
->nc_nlen
, par
->nc_nlen
, par
->nc_name
);
3721 * Call VOP_NRESOLVE() to get the vp, then scan for any disconnected
3722 * ncp's and reattach them. If this occurs the original ncp is marked
3723 * EAGAIN to force a relookup.
3725 * NOTE: in order to call VOP_NRESOLVE(), the parent of the passed
3726 * ncp must already be resolved.
3731 ncp
->nc_error
= VOP_NRESOLVE(&nctmp
, dvp
, cred
);
3734 ncp
->nc_error
= EPERM
;
3736 if (ncp
->nc_error
== EAGAIN
) {
3737 kprintf("[diagnostic] cache_resolve: EAGAIN ncp %p %*.*s\n",
3738 ncp
, ncp
->nc_nlen
, ncp
->nc_nlen
, ncp
->nc_name
);
3741 return(ncp
->nc_error
);
3745 * Resolve the ncp associated with a mount point. Such ncp's almost always
3746 * remain resolved and this routine is rarely called. NFS MPs tends to force
3747 * re-resolution more often due to its mac-truck-smash-the-namecache
3748 * method of tracking namespace changes.
3750 * The semantics for this call is that the passed ncp must be locked on
3751 * entry and will be locked on return. However, if we actually have to
3752 * resolve the mount point we temporarily unlock the entry in order to
3753 * avoid race-to-root deadlocks due to e.g. dead NFS mounts. Because of
3754 * the unlock we have to recheck the flags after we relock.
3757 cache_resolve_mp(struct mount
*mp
)
3759 struct namecache
*ncp
= mp
->mnt_ncmountpt
.ncp
;
3763 KKASSERT(mp
!= NULL
);
3766 * If the ncp is already resolved we have nothing to do. However,
3767 * we do want to guarentee that a usable vnode is returned when
3768 * a vnode is present, so make sure it hasn't been reclaimed.
3770 if ((ncp
->nc_flag
& NCF_UNRESOLVED
) == 0) {
3771 if (ncp
->nc_vp
&& (ncp
->nc_vp
->v_flag
& VRECLAIMED
))
3772 _cache_setunresolved(ncp
);
3775 if (ncp
->nc_flag
& NCF_UNRESOLVED
) {
3777 while (vfs_busy(mp
, 0))
3779 error
= VFS_ROOT(mp
, &vp
);
3783 * recheck the ncp state after relocking.
3785 if (ncp
->nc_flag
& NCF_UNRESOLVED
) {
3786 ncp
->nc_error
= error
;
3788 _cache_setvp(mp
, ncp
, vp
);
3791 kprintf("[diagnostic] cache_resolve_mp: failed"
3792 " to resolve mount %p err=%d ncp=%p\n",
3794 _cache_setvp(mp
, ncp
, NULL
);
3796 } else if (error
== 0) {
3801 return(ncp
->nc_error
);
3805 * Clean out negative cache entries when too many have accumulated.
3808 _cache_cleanneg(long count
)
3810 struct pcpu_ncache
*pn
;
3811 struct namecache
*ncp
;
3812 static uint32_t neg_rover
;
3815 n
= neg_rover
++; /* SMP heuristical, race ok */
3817 n
= n
% (uint32_t)ncpus
;
3819 pn
= &pcpu_ncache
[n
];
3820 spin_lock(&pn
->neg_spin
);
3821 count
= pn
->neg_count
* count
/ vfscache_negs
+ 1;
3822 spin_unlock(&pn
->neg_spin
);
3825 * Attempt to clean out the specified number of negative cache
3829 spin_lock(&pn
->neg_spin
);
3830 ncp
= TAILQ_FIRST(&pn
->neg_list
);
3832 spin_unlock(&pn
->neg_spin
);
3835 TAILQ_REMOVE(&pn
->neg_list
, ncp
, nc_vnode
);
3836 TAILQ_INSERT_TAIL(&pn
->neg_list
, ncp
, nc_vnode
);
3838 spin_unlock(&pn
->neg_spin
);
3841 * This can race, so we must re-check that the ncp
3842 * is on the ncneg.list after successfully locking it.
3844 if (_cache_lock_special(ncp
) == 0) {
3845 if (ncp
->nc_vp
== NULL
&&
3846 (ncp
->nc_flag
& NCF_UNRESOLVED
) == 0) {
3847 ncp
= cache_zap(ncp
, 1);
3851 kprintf("cache_cleanneg: race avoided\n");
3862 * Clean out positive cache entries when too many have accumulated.
3865 _cache_cleanpos(long count
)
3867 static volatile int rover
;
3868 struct nchash_head
*nchpp
;
3869 struct namecache
*ncp
;
3873 * Attempt to clean out the specified number of negative cache
3877 rover_copy
= ++rover
; /* MPSAFEENOUGH */
3879 nchpp
= NCHHASH(rover_copy
);
3881 spin_lock_shared(&nchpp
->spin
);
3882 ncp
= LIST_FIRST(&nchpp
->list
);
3883 while (ncp
&& (ncp
->nc_flag
& NCF_DESTROYED
))
3884 ncp
= LIST_NEXT(ncp
, nc_hash
);
3887 spin_unlock_shared(&nchpp
->spin
);
3890 if (_cache_lock_special(ncp
) == 0) {
3891 ncp
= cache_zap(ncp
, 1);
3903 * This is a kitchen sink function to clean out ncps which we
3904 * tried to zap from cache_drop() but failed because we were
3905 * unable to acquire the parent lock.
3907 * Such entries can also be removed via cache_inval_vp(), such
3908 * as when unmounting.
3911 _cache_cleandefered(void)
3913 struct nchash_head
*nchpp
;
3914 struct namecache
*ncp
;
3915 struct namecache dummy
;
3919 bzero(&dummy
, sizeof(dummy
));
3920 dummy
.nc_flag
= NCF_DESTROYED
;
3923 for (i
= 0; i
<= nchash
; ++i
) {
3924 nchpp
= &nchashtbl
[i
];
3926 spin_lock(&nchpp
->spin
);
3927 LIST_INSERT_HEAD(&nchpp
->list
, &dummy
, nc_hash
);
3929 while ((ncp
= LIST_NEXT(ncp
, nc_hash
)) != NULL
) {
3930 if ((ncp
->nc_flag
& NCF_DEFEREDZAP
) == 0)
3932 LIST_REMOVE(&dummy
, nc_hash
);
3933 LIST_INSERT_AFTER(ncp
, &dummy
, nc_hash
);
3935 spin_unlock(&nchpp
->spin
);
3936 if (_cache_lock_nonblock(ncp
) == 0) {
3937 ncp
->nc_flag
&= ~NCF_DEFEREDZAP
;
3941 spin_lock(&nchpp
->spin
);
3944 LIST_REMOVE(&dummy
, nc_hash
);
3945 spin_unlock(&nchpp
->spin
);
3950 * Name cache initialization, from vfsinit() when we are booting
3955 struct pcpu_ncache
*pn
;
3960 * Per-cpu accounting and negative hit list
3962 pcpu_ncache
= kmalloc(sizeof(*pcpu_ncache
) * ncpus
,
3963 M_VFSCACHE
, M_WAITOK
|M_ZERO
);
3964 for (i
= 0; i
< ncpus
; ++i
) {
3965 pn
= &pcpu_ncache
[i
];
3966 TAILQ_INIT(&pn
->neg_list
);
3967 spin_init(&pn
->neg_spin
, "ncneg");
3971 * Initialise per-cpu namecache effectiveness statistics.
3973 for (i
= 0; i
< ncpus
; ++i
) {
3974 gd
= globaldata_find(i
);
3975 gd
->gd_nchstats
= &nchstats
[i
];
3979 * Create a generous namecache hash table
3981 nchashtbl
= hashinit_ext(vfs_inodehashsize(),
3982 sizeof(struct nchash_head
),
3983 M_VFSCACHE
, &nchash
);
3984 for (i
= 0; i
<= (int)nchash
; ++i
) {
3985 LIST_INIT(&nchashtbl
[i
].list
);
3986 spin_init(&nchashtbl
[i
].spin
, "nchinit_hash");
3988 for (i
= 0; i
< NCMOUNT_NUMCACHE
; ++i
)
3989 spin_init(&ncmount_cache
[i
].spin
, "nchinit_cache");
3990 nclockwarn
= 5 * hz
;
3994 * Called from start_init() to bootstrap the root filesystem. Returns
3995 * a referenced, unlocked namecache record.
3998 cache_allocroot(struct nchandle
*nch
, struct mount
*mp
, struct vnode
*vp
)
4000 nch
->ncp
= cache_alloc(0);
4004 _cache_setvp(nch
->mount
, nch
->ncp
, vp
);
4008 * vfs_cache_setroot()
4010 * Create an association between the root of our namecache and
4011 * the root vnode. This routine may be called several times during
4014 * If the caller intends to save the returned namecache pointer somewhere
4015 * it must cache_hold() it.
4018 vfs_cache_setroot(struct vnode
*nvp
, struct nchandle
*nch
)
4021 struct nchandle onch
;
4029 cache_zero(&rootnch
);
4037 * XXX OLD API COMPAT FUNCTION. This really messes up the new namecache
4038 * topology and is being removed as quickly as possible. The new VOP_N*()
4039 * API calls are required to make specific adjustments using the supplied
4040 * ncp pointers rather then just bogusly purging random vnodes.
4042 * Invalidate all namecache entries to a particular vnode as well as
4043 * any direct children of that vnode in the namecache. This is a
4044 * 'catch all' purge used by filesystems that do not know any better.
4046 * Note that the linkage between the vnode and its namecache entries will
4047 * be removed, but the namecache entries themselves might stay put due to
4048 * active references from elsewhere in the system or due to the existance of
4049 * the children. The namecache topology is left intact even if we do not
4050 * know what the vnode association is. Such entries will be marked
4054 cache_purge(struct vnode
*vp
)
4056 cache_inval_vp(vp
, CINV_DESTROY
| CINV_CHILDREN
);
4059 static int disablecwd
;
4060 SYSCTL_INT(_debug
, OID_AUTO
, disablecwd
, CTLFLAG_RW
, &disablecwd
, 0,
4063 static u_long numcwdcalls
;
4064 SYSCTL_ULONG(_vfs_cache
, OID_AUTO
, numcwdcalls
, CTLFLAG_RD
, &numcwdcalls
, 0,
4065 "Number of current directory resolution calls");
4066 static u_long numcwdfailnf
;
4067 SYSCTL_ULONG(_vfs_cache
, OID_AUTO
, numcwdfailnf
, CTLFLAG_RD
, &numcwdfailnf
, 0,
4068 "Number of current directory failures due to lack of file");
4069 static u_long numcwdfailsz
;
4070 SYSCTL_ULONG(_vfs_cache
, OID_AUTO
, numcwdfailsz
, CTLFLAG_RD
, &numcwdfailsz
, 0,
4071 "Number of current directory failures due to large result");
4072 static u_long numcwdfound
;
4073 SYSCTL_ULONG(_vfs_cache
, OID_AUTO
, numcwdfound
, CTLFLAG_RD
, &numcwdfound
, 0,
4074 "Number of current directory resolution successes");
4080 sys___getcwd(struct __getcwd_args
*uap
)
4090 buflen
= uap
->buflen
;
4093 if (buflen
> MAXPATHLEN
)
4094 buflen
= MAXPATHLEN
;
4096 buf
= kmalloc(buflen
, M_TEMP
, M_WAITOK
);
4097 bp
= kern_getcwd(buf
, buflen
, &error
);
4099 error
= copyout(bp
, uap
->buf
, strlen(bp
) + 1);
4105 kern_getcwd(char *buf
, size_t buflen
, int *error
)
4107 struct proc
*p
= curproc
;
4109 int i
, slash_prefixed
;
4110 struct filedesc
*fdp
;
4111 struct nchandle nch
;
4112 struct namecache
*ncp
;
4121 nch
= fdp
->fd_ncdir
;
4126 while (ncp
&& (ncp
!= fdp
->fd_nrdir
.ncp
||
4127 nch
.mount
!= fdp
->fd_nrdir
.mount
)
4130 * While traversing upwards if we encounter the root
4131 * of the current mount we have to skip to the mount point
4132 * in the underlying filesystem.
4134 if (ncp
== nch
.mount
->mnt_ncmountpt
.ncp
) {
4135 nch
= nch
.mount
->mnt_ncmounton
;
4144 * Prepend the path segment
4146 for (i
= ncp
->nc_nlen
- 1; i
>= 0; i
--) {
4153 *--bp
= ncp
->nc_name
[i
];
4165 * Go up a directory. This isn't a mount point so we don't
4166 * have to check again.
4168 while ((nch
.ncp
= ncp
->nc_parent
) != NULL
) {
4169 if (ncp_shared_lock_disable
)
4172 _cache_lock_shared(ncp
);
4173 if (nch
.ncp
!= ncp
->nc_parent
) {
4177 _cache_hold(nch
.ncp
);
4190 if (!slash_prefixed
) {
4208 * Thus begins the fullpath magic.
4210 * The passed nchp is referenced but not locked.
4212 static int disablefullpath
;
4213 SYSCTL_INT(_debug
, OID_AUTO
, disablefullpath
, CTLFLAG_RW
,
4214 &disablefullpath
, 0,
4215 "Disable fullpath lookups");
4217 static u_int numfullpathcalls
;
4218 SYSCTL_UINT(_vfs_cache
, OID_AUTO
, numfullpathcalls
, CTLFLAG_RD
,
4219 &numfullpathcalls
, 0,
4220 "Number of full path resolutions in progress");
4221 static u_int numfullpathfailnf
;
4222 SYSCTL_UINT(_vfs_cache
, OID_AUTO
, numfullpathfailnf
, CTLFLAG_RD
,
4223 &numfullpathfailnf
, 0,
4224 "Number of full path resolution failures due to lack of file");
4225 static u_int numfullpathfailsz
;
4226 SYSCTL_UINT(_vfs_cache
, OID_AUTO
, numfullpathfailsz
, CTLFLAG_RD
,
4227 &numfullpathfailsz
, 0,
4228 "Number of full path resolution failures due to insufficient memory");
4229 static u_int numfullpathfound
;
4230 SYSCTL_UINT(_vfs_cache
, OID_AUTO
, numfullpathfound
, CTLFLAG_RD
,
4231 &numfullpathfound
, 0,
4232 "Number of full path resolution successes");
4235 cache_fullpath(struct proc
*p
, struct nchandle
*nchp
, struct nchandle
*nchbase
,
4236 char **retbuf
, char **freebuf
, int guess
)
4238 struct nchandle fd_nrdir
;
4239 struct nchandle nch
;
4240 struct namecache
*ncp
;
4241 struct mount
*mp
, *new_mp
;
4247 atomic_add_int(&numfullpathcalls
, -1);
4252 buf
= kmalloc(MAXPATHLEN
, M_TEMP
, M_WAITOK
);
4253 bp
= buf
+ MAXPATHLEN
- 1;
4256 fd_nrdir
= *nchbase
;
4258 fd_nrdir
= p
->p_fd
->fd_nrdir
;
4268 while (ncp
&& (ncp
!= fd_nrdir
.ncp
|| mp
!= fd_nrdir
.mount
)) {
4272 * If we are asked to guess the upwards path, we do so whenever
4273 * we encounter an ncp marked as a mountpoint. We try to find
4274 * the actual mountpoint by finding the mountpoint with this
4277 if (guess
&& (ncp
->nc_flag
& NCF_ISMOUNTPT
)) {
4278 new_mp
= mount_get_by_nc(ncp
);
4281 * While traversing upwards if we encounter the root
4282 * of the current mount we have to skip to the mount point.
4284 if (ncp
== mp
->mnt_ncmountpt
.ncp
) {
4288 nch
= new_mp
->mnt_ncmounton
;
4298 * Prepend the path segment
4300 for (i
= ncp
->nc_nlen
- 1; i
>= 0; i
--) {
4302 numfullpathfailsz
++;
4307 *--bp
= ncp
->nc_name
[i
];
4310 numfullpathfailsz
++;
4319 * Go up a directory. This isn't a mount point so we don't
4320 * have to check again.
4322 * We can only safely access nc_parent with ncp held locked.
4324 while ((nch
.ncp
= ncp
->nc_parent
) != NULL
) {
4326 if (nch
.ncp
!= ncp
->nc_parent
) {
4330 _cache_hold(nch
.ncp
);
4338 numfullpathfailnf
++;
4344 if (!slash_prefixed
) {
4346 numfullpathfailsz
++;
4364 vn_fullpath(struct proc
*p
, struct vnode
*vn
, char **retbuf
,
4365 char **freebuf
, int guess
)
4367 struct namecache
*ncp
;
4368 struct nchandle nch
;
4372 atomic_add_int(&numfullpathcalls
, 1);
4373 if (disablefullpath
)
4379 /* vn is NULL, client wants us to use p->p_textvp */
4381 if ((vn
= p
->p_textvp
) == NULL
)
4384 spin_lock_shared(&vn
->v_spin
);
4385 TAILQ_FOREACH(ncp
, &vn
->v_namecache
, nc_vnode
) {
4390 spin_unlock_shared(&vn
->v_spin
);
4394 spin_unlock_shared(&vn
->v_spin
);
4396 atomic_add_int(&numfullpathcalls
, -1);
4398 nch
.mount
= vn
->v_mount
;
4399 error
= cache_fullpath(p
, &nch
, NULL
, retbuf
, freebuf
, guess
);
4405 vfscache_rollup_cpu(struct globaldata
*gd
)
4407 struct pcpu_ncache
*pn
;
4410 if (pcpu_ncache
== NULL
)
4412 pn
= &pcpu_ncache
[gd
->gd_cpuid
];
4414 if (pn
->vfscache_count
) {
4415 count
= atomic_swap_long(&pn
->vfscache_count
, 0);
4416 atomic_add_long(&vfscache_count
, count
);
4418 if (pn
->vfscache_leafs
) {
4419 count
= atomic_swap_long(&pn
->vfscache_leafs
, 0);
4420 atomic_add_long(&vfscache_leafs
, count
);
4422 if (pn
->vfscache_negs
) {
4423 count
= atomic_swap_long(&pn
->vfscache_negs
, 0);
4424 atomic_add_long(&vfscache_negs
, count
);
4430 vfscache_rollup_all(void)
4434 for (n
= 0; n
< ncpus
; ++n
)
4435 vfscache_rollup_cpu(globaldata_find(n
));