kernel - Change namecache negative caching algorithm
[dragonfly.git] / sys / kern / vfs_cache.c
CommitLineData
984263bc 1/*
2247fe02 2 * Copyright (c) 2003,2004,2009 The DragonFly Project. All rights reserved.
8c10bfcf
MD
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
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
16 * distribution.
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.
20 *
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
32 * SUCH DAMAGE.
33 *
984263bc
MD
34 * Copyright (c) 1989, 1993, 1995
35 * The Regents of the University of California. All rights reserved.
36 *
37 * This code is derived from software contributed to Berkeley by
38 * Poul-Henning Kamp of the FreeBSD Project.
39 *
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. All advertising materials mentioning features or use of this software
49 * must display the following acknowledgement:
50 * This product includes software developed by the University of
51 * California, Berkeley and its contributors.
52 * 4. Neither the name of the University nor the names of its contributors
53 * may be used to endorse or promote products derived from this software
54 * without specific prior written permission.
55 *
56 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
57 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
60 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * SUCH DAMAGE.
984263bc
MD
67 */
68
69#include <sys/param.h>
70#include <sys/systm.h>
71#include <sys/kernel.h>
72#include <sys/sysctl.h>
73#include <sys/mount.h>
74#include <sys/vnode.h>
984263bc
MD
75#include <sys/malloc.h>
76#include <sys/sysproto.h>
f63911bf 77#include <sys/spinlock.h>
984263bc 78#include <sys/proc.h>
dadab5e9 79#include <sys/namei.h>
690a3127 80#include <sys/nlookup.h>
984263bc
MD
81#include <sys/filedesc.h>
82#include <sys/fnv_hash.h>
24e51f36 83#include <sys/globaldata.h>
63f58b90 84#include <sys/kern_syscall.h>
fad57d0e 85#include <sys/dirent.h>
8c361dda 86#include <ddb/ddb.h>
984263bc 87
3c37c940 88#include <sys/sysref2.h>
f63911bf 89#include <sys/spinlock2.h>
684a93c4 90#include <sys/mplock2.h>
3c37c940 91
bf40a153
MD
92#define MAX_RECURSION_DEPTH 64
93
984263bc 94/*
7ea21ed1 95 * Random lookups in the cache are accomplished with a hash table using
2247fe02
MD
96 * a hash key of (nc_src_vp, name). Each hash chain has its own spin lock.
97 *
98 * Negative entries may exist and correspond to resolved namecache
99 * structures where nc_vp is NULL. In a negative entry, NCF_WHITEOUT
100 * will be set if the entry corresponds to a whited-out directory entry
101 * (verses simply not finding the entry at all). ncneglist is locked
102 * with a global spinlock (ncspin).
103 *
104 * MPSAFE RULES:
105 *
106 * (1) A ncp must be referenced before it can be locked.
107 *
108 * (2) A ncp must be locked in order to modify it.
109 *
110 * (3) ncp locks are always ordered child -> parent. That may seem
111 * backwards but forward scans use the hash table and thus can hold
112 * the parent unlocked when traversing downward.
984263bc 113 *
2247fe02
MD
114 * This allows insert/rename/delete/dot-dot and other operations
115 * to use ncp->nc_parent links.
984263bc 116 *
2247fe02
MD
117 * This also prevents a locked up e.g. NFS node from creating a
118 * chain reaction all the way back to the root vnode / namecache.
119 *
120 * (4) parent linkages require both the parent and child to be locked.
984263bc
MD
121 */
122
123/*
124 * Structures associated with name cacheing.
125 */
07baed26
MD
126#define NCHHASH(hash) (&nchashtbl[(hash) & nchash])
127#define MINNEG 1024
128#define MINPOS 1024
7e9c94bd 129#define NCMOUNT_NUMCACHE 1009 /* prime number */
8987aad7 130
24e51f36
HP
131MALLOC_DEFINE(M_VFSCACHE, "vfscache", "VFS name cache entries");
132
f63911bf
MD
133LIST_HEAD(nchash_list, namecache);
134
135struct nchash_head {
65870584
MD
136 struct nchash_list list;
137 struct spinlock spin;
f63911bf
MD
138};
139
07baed26
MD
140struct ncmount_cache {
141 struct spinlock spin;
142 struct namecache *ncp;
143 struct mount *mp;
9c105d5b 144 int isneg; /* if != 0 mp is originator and not target */
07baed26
MD
145};
146
f63911bf
MD
147static struct nchash_head *nchashtbl;
148static struct namecache_list ncneglist;
149static struct spinlock ncspin;
07baed26 150static struct ncmount_cache ncmount_cache[NCMOUNT_NUMCACHE];
8987aad7 151
fad57d0e
MD
152/*
153 * ncvp_debug - debug cache_fromvp(). This is used by the NFS server
154 * to create the namecache infrastructure leading to a dangling vnode.
155 *
156 * 0 Only errors are reported
157 * 1 Successes are reported
158 * 2 Successes + the whole directory scan is reported
159 * 3 Force the directory scan code run as if the parent vnode did not
160 * have a namecache record, even if it does have one.
161 */
162static int ncvp_debug;
0c52fa62
SG
163SYSCTL_INT(_debug, OID_AUTO, ncvp_debug, CTLFLAG_RW, &ncvp_debug, 0,
164 "Namecache debug level (0-3)");
fad57d0e 165
984263bc 166static u_long nchash; /* size of hash table */
0c52fa62
SG
167SYSCTL_ULONG(_debug, OID_AUTO, nchash, CTLFLAG_RD, &nchash, 0,
168 "Size of namecache hash table");
8987aad7 169
6363f268
MD
170static int ncnegflush = 10; /* burst for negative flush */
171SYSCTL_INT(_debug, OID_AUTO, ncnegflush, CTLFLAG_RW, &ncnegflush, 0,
172 "Batch flush negative entries");
173
174static int ncposflush = 10; /* burst for positive flush */
175SYSCTL_INT(_debug, OID_AUTO, ncposflush, CTLFLAG_RW, &ncposflush, 0,
176 "Batch flush positive entries");
177
f63911bf 178static int ncnegfactor = 16; /* ratio of negative entries */
0c52fa62
SG
179SYSCTL_INT(_debug, OID_AUTO, ncnegfactor, CTLFLAG_RW, &ncnegfactor, 0,
180 "Ratio of namecache negative entries");
8987aad7 181
fc21741a 182static int nclockwarn; /* warn on locked entries in ticks */
0c52fa62
SG
183SYSCTL_INT(_debug, OID_AUTO, nclockwarn, CTLFLAG_RW, &nclockwarn, 0,
184 "Warn on locked namecache entries in ticks");
fc21741a 185
65870584 186static int numdefered; /* number of cache entries allocated */
0c52fa62
SG
187SYSCTL_INT(_debug, OID_AUTO, numdefered, CTLFLAG_RD, &numdefered, 0,
188 "Number of cache entries allocated");
65870584 189
9e10d70b 190static int ncposlimit; /* number of cache entries allocated */
0c52fa62
SG
191SYSCTL_INT(_debug, OID_AUTO, ncposlimit, CTLFLAG_RW, &ncposlimit, 0,
192 "Number of cache entries allocated");
9e10d70b 193
79fd1696
MD
194static int ncp_shared_lock_disable = 1;
195SYSCTL_INT(_debug, OID_AUTO, ncp_shared_lock_disable, CTLFLAG_RW,
196 &ncp_shared_lock_disable, 0, "Disable shared namecache locks");
197
0c52fa62
SG
198SYSCTL_INT(_debug, OID_AUTO, vnsize, CTLFLAG_RD, 0, sizeof(struct vnode),
199 "sizeof(struct vnode)");
200SYSCTL_INT(_debug, OID_AUTO, ncsize, CTLFLAG_RD, 0, sizeof(struct namecache),
201 "sizeof(struct namecache)");
984263bc 202
7e9c94bd
MD
203static int ncmount_cache_enable = 1;
204SYSCTL_INT(_debug, OID_AUTO, ncmount_cache_enable, CTLFLAG_RW,
205 &ncmount_cache_enable, 0, "mount point cache");
206static long ncmount_cache_hit;
207SYSCTL_LONG(_debug, OID_AUTO, ncmount_cache_hit, CTLFLAG_RW,
208 &ncmount_cache_hit, 0, "mpcache hits");
209static long ncmount_cache_miss;
210SYSCTL_LONG(_debug, OID_AUTO, ncmount_cache_miss, CTLFLAG_RW,
211 &ncmount_cache_miss, 0, "mpcache misses");
212static long ncmount_cache_overwrite;
213SYSCTL_LONG(_debug, OID_AUTO, ncmount_cache_overwrite, CTLFLAG_RW,
214 &ncmount_cache_overwrite, 0, "mpcache entry overwrites");
215
28623bf9 216static int cache_resolve_mp(struct mount *mp);
5312fa43 217static struct vnode *cache_dvpref(struct namecache *ncp);
28623bf9
MD
218static void _cache_lock(struct namecache *ncp);
219static void _cache_setunresolved(struct namecache *ncp);
65870584 220static void _cache_cleanneg(int count);
9e10d70b 221static void _cache_cleanpos(int count);
65870584 222static void _cache_cleandefered(void);
8d09ad3d 223static void _cache_unlink(struct namecache *ncp);
646a1cda 224
984263bc
MD
225/*
226 * The new name cache statistics
227 */
228SYSCTL_NODE(_vfs, OID_AUTO, cache, CTLFLAG_RW, 0, "Name cache statistics");
093e85dc 229static int numneg;
8d09ad3d 230SYSCTL_INT(_vfs_cache, OID_AUTO, numneg, CTLFLAG_RD, &numneg, 0,
093e85dc
SG
231 "Number of negative namecache entries");
232static int numcache;
8d09ad3d 233SYSCTL_INT(_vfs_cache, OID_AUTO, numcache, CTLFLAG_RD, &numcache, 0,
093e85dc
SG
234 "Number of namecaches entries");
235static u_long numcalls;
236SYSCTL_ULONG(_vfs_cache, OID_AUTO, numcalls, CTLFLAG_RD, &numcalls, 0,
237 "Number of namecache lookups");
238static u_long numchecks;
239SYSCTL_ULONG(_vfs_cache, OID_AUTO, numchecks, CTLFLAG_RD, &numchecks, 0,
240 "Number of checked entries in namecache lookups");
984263bc 241
24e51f36
HP
242struct nchstats nchstats[SMP_MAXCPU];
243/*
244 * Export VFS cache effectiveness statistics to user-land.
245 *
246 * The statistics are left for aggregation to user-land so
247 * neat things can be achieved, like observing per-CPU cache
248 * distribution.
249 */
250static int
3736bb9b 251sysctl_nchstats(SYSCTL_HANDLER_ARGS)
24e51f36
HP
252{
253 struct globaldata *gd;
254 int i, error;
255
256 error = 0;
257 for (i = 0; i < ncpus; ++i) {
258 gd = globaldata_find(i);
259 if ((error = SYSCTL_OUT(req, (void *)&(*gd->gd_nchstats),
260 sizeof(struct nchstats))))
261 break;
262 }
984263bc 263
24e51f36
HP
264 return (error);
265}
266SYSCTL_PROC(_vfs_cache, OID_AUTO, nchstats, CTLTYPE_OPAQUE|CTLFLAG_RD,
3736bb9b 267 0, 0, sysctl_nchstats, "S,nchstats", "VFS cache effectiveness statistics");
984263bc 268
65870584 269static struct namecache *cache_zap(struct namecache *ncp, int nonblock);
f63911bf
MD
270
271/*
272 * Namespace locking. The caller must already hold a reference to the
273 * namecache structure in order to lock/unlock it. This function prevents
274 * the namespace from being created or destroyed by accessors other then
275 * the lock holder.
276 *
277 * Note that holding a locked namecache structure prevents other threads
278 * from making namespace changes (e.g. deleting or creating), prevents
279 * vnode association state changes by other threads, and prevents the
280 * namecache entry from being resolved or unresolved by other threads.
281 *
79fd1696
MD
282 * An exclusive lock owner has full authority to associate/disassociate
283 * vnodes and resolve/unresolve the locked ncp.
284 *
285 * A shared lock owner only has authority to acquire the underlying vnode,
286 * if any.
f63911bf 287 *
79fd1696 288 * The primary lock field is nc_lockstatus. nc_locktd is set after the
2247fe02
MD
289 * fact (when locking) or cleared prior to unlocking.
290 *
f63911bf
MD
291 * WARNING! Holding a locked ncp will prevent a vnode from being destroyed
292 * or recycled, but it does NOT help you if the vnode had already
293 * initiated a recyclement. If this is important, use cache_get()
294 * rather then cache_lock() (and deal with the differences in the
295 * way the refs counter is handled). Or, alternatively, make an
296 * unconditional call to cache_validate() or cache_resolve()
297 * after cache_lock() returns.
298 */
299static
300void
301_cache_lock(struct namecache *ncp)
302{
303 thread_t td;
f63911bf
MD
304 int didwarn;
305 int error;
2247fe02 306 u_int count;
f63911bf
MD
307
308 KKASSERT(ncp->nc_refs != 0);
309 didwarn = 0;
310 td = curthread;
311
312 for (;;) {
79fd1696
MD
313 count = ncp->nc_lockstatus;
314 cpu_ccfence();
f63911bf 315
79fd1696
MD
316 if ((count & ~(NC_EXLOCK_REQ|NC_SHLOCK_REQ)) == 0) {
317 if (atomic_cmpset_int(&ncp->nc_lockstatus,
318 count, count + 1)) {
f63911bf
MD
319 /*
320 * The vp associated with a locked ncp must
321 * be held to prevent it from being recycled.
322 *
323 * WARNING! If VRECLAIMED is set the vnode
324 * could already be in the middle of a recycle.
325 * Callers must use cache_vref() or
326 * cache_vget() on the locked ncp to
327 * validate the vp or set the cache entry
328 * to unresolved.
2247fe02
MD
329 *
330 * NOTE! vhold() is allowed if we hold a
331 * lock on the ncp (which we do).
f63911bf 332 */
2247fe02 333 ncp->nc_locktd = td;
f63911bf 334 if (ncp->nc_vp)
79fd1696 335 vhold(ncp->nc_vp);
f63911bf
MD
336 break;
337 }
2247fe02
MD
338 /* cmpset failed */
339 continue;
340 }
341 if (ncp->nc_locktd == td) {
79fd1696
MD
342 KKASSERT((count & NC_SHLOCK_FLAG) == 0);
343 if (atomic_cmpset_int(&ncp->nc_lockstatus,
344 count, count + 1)) {
2247fe02
MD
345 break;
346 }
347 /* cmpset failed */
f63911bf
MD
348 continue;
349 }
79fd1696
MD
350 tsleep_interlock(&ncp->nc_locktd, 0);
351 if (atomic_cmpset_int(&ncp->nc_lockstatus, count,
2247fe02
MD
352 count | NC_EXLOCK_REQ) == 0) {
353 /* cmpset failed */
f63911bf 354 continue;
2247fe02 355 }
79fd1696
MD
356 error = tsleep(&ncp->nc_locktd, PINTERLOCKED,
357 "clock", nclockwarn);
f63911bf 358 if (error == EWOULDBLOCK) {
2247fe02
MD
359 if (didwarn == 0) {
360 didwarn = ticks;
79fd1696
MD
361 kprintf("[diagnostic] cache_lock: "
362 "blocked on %p %08x",
363 ncp, count);
2247fe02
MD
364 kprintf(" \"%*.*s\"\n",
365 ncp->nc_nlen, ncp->nc_nlen,
366 ncp->nc_name);
367 }
f63911bf 368 }
79fd1696 369 /* loop */
f63911bf 370 }
2247fe02
MD
371 if (didwarn) {
372 kprintf("[diagnostic] cache_lock: unblocked %*.*s after "
373 "%d secs\n",
374 ncp->nc_nlen, ncp->nc_nlen, ncp->nc_name,
375 (int)(ticks - didwarn) / hz);
f63911bf
MD
376 }
377}
378
79fd1696
MD
379/*
380 * The shared lock works similarly to the exclusive lock except
381 * nc_locktd is left NULL and we need an interlock (VHOLD) to
382 * prevent vhold() races, since the moment our cmpset_int succeeds
383 * another cpu can come in and get its own shared lock.
384 *
385 * A critical section is needed to prevent interruption during the
386 * VHOLD interlock.
387 */
388static
389void
390_cache_lock_shared(struct namecache *ncp)
391{
392 int didwarn;
393 int error;
394 u_int count;
395
396 KKASSERT(ncp->nc_refs != 0);
397 didwarn = 0;
398
399 for (;;) {
400 count = ncp->nc_lockstatus;
401 cpu_ccfence();
402
403 if ((count & ~NC_SHLOCK_REQ) == 0) {
404 crit_enter();
405 if (atomic_cmpset_int(&ncp->nc_lockstatus,
406 count,
407 (count + 1) | NC_SHLOCK_FLAG |
408 NC_SHLOCK_VHOLD)) {
409 /*
410 * The vp associated with a locked ncp must
411 * be held to prevent it from being recycled.
412 *
413 * WARNING! If VRECLAIMED is set the vnode
414 * could already be in the middle of a recycle.
415 * Callers must use cache_vref() or
416 * cache_vget() on the locked ncp to
417 * validate the vp or set the cache entry
418 * to unresolved.
419 *
420 * NOTE! vhold() is allowed if we hold a
421 * lock on the ncp (which we do).
422 */
423 if (ncp->nc_vp)
424 vhold(ncp->nc_vp);
425 atomic_clear_int(&ncp->nc_lockstatus,
426 NC_SHLOCK_VHOLD);
427 crit_exit();
428 break;
429 }
430 /* cmpset failed */
431 crit_exit();
432 continue;
433 }
434
435 /*
436 * If already held shared we can just bump the count, but
437 * only allow this if nobody is trying to get the lock
438 * exclusively.
439 *
440 * VHOLD is a bit of a hack. Even though we successfully
441 * added another shared ref, the cpu that got the first
442 * shared ref might not yet have held the vnode.
443 */
444 if ((count & (NC_EXLOCK_REQ|NC_SHLOCK_FLAG)) ==
445 NC_SHLOCK_FLAG) {
446 KKASSERT((count & ~(NC_EXLOCK_REQ |
447 NC_SHLOCK_REQ |
448 NC_SHLOCK_FLAG)) > 0);
449 if (atomic_cmpset_int(&ncp->nc_lockstatus,
450 count, count + 1)) {
451 while (ncp->nc_lockstatus & NC_SHLOCK_VHOLD)
452 cpu_pause();
453 break;
454 }
455 continue;
456 }
457 tsleep_interlock(ncp, 0);
458 if (atomic_cmpset_int(&ncp->nc_lockstatus, count,
459 count | NC_SHLOCK_REQ) == 0) {
460 /* cmpset failed */
461 continue;
462 }
463 error = tsleep(ncp, PINTERLOCKED, "clocksh", nclockwarn);
464 if (error == EWOULDBLOCK) {
465 if (didwarn == 0) {
466 didwarn = ticks;
467 kprintf("[diagnostic] cache_lock_shared: "
468 "blocked on %p %08x",
469 ncp, count);
470 kprintf(" \"%*.*s\"\n",
471 ncp->nc_nlen, ncp->nc_nlen,
472 ncp->nc_name);
473 }
474 }
475 /* loop */
476 }
477 if (didwarn) {
478 kprintf("[diagnostic] cache_lock_shared: "
479 "unblocked %*.*s after %d secs\n",
480 ncp->nc_nlen, ncp->nc_nlen, ncp->nc_name,
481 (int)(ticks - didwarn) / hz);
482 }
483}
484
2247fe02 485/*
65870584
MD
486 * NOTE: nc_refs may be zero if the ncp is interlocked by circumstance,
487 * such as the case where one of its children is locked.
2247fe02 488 */
f63911bf
MD
489static
490int
491_cache_lock_nonblock(struct namecache *ncp)
492{
493 thread_t td;
2247fe02 494 u_int count;
f63911bf 495
f63911bf
MD
496 td = curthread;
497
498 for (;;) {
79fd1696 499 count = ncp->nc_lockstatus;
f63911bf 500
79fd1696
MD
501 if ((count & ~(NC_EXLOCK_REQ|NC_SHLOCK_REQ)) == 0) {
502 if (atomic_cmpset_int(&ncp->nc_lockstatus,
503 count, count + 1)) {
f63911bf
MD
504 /*
505 * The vp associated with a locked ncp must
506 * be held to prevent it from being recycled.
507 *
508 * WARNING! If VRECLAIMED is set the vnode
509 * could already be in the middle of a recycle.
510 * Callers must use cache_vref() or
511 * cache_vget() on the locked ncp to
512 * validate the vp or set the cache entry
513 * to unresolved.
2247fe02
MD
514 *
515 * NOTE! vhold() is allowed if we hold a
516 * lock on the ncp (which we do).
f63911bf 517 */
2247fe02 518 ncp->nc_locktd = td;
f63911bf 519 if (ncp->nc_vp)
79fd1696 520 vhold(ncp->nc_vp);
f63911bf
MD
521 break;
522 }
2247fe02
MD
523 /* cmpset failed */
524 continue;
525 }
526 if (ncp->nc_locktd == td) {
79fd1696
MD
527 if (atomic_cmpset_int(&ncp->nc_lockstatus,
528 count, count + 1)) {
2247fe02
MD
529 break;
530 }
531 /* cmpset failed */
f63911bf
MD
532 continue;
533 }
534 return(EWOULDBLOCK);
535 }
536 return(0);
537}
538
79fd1696
MD
539/*
540 * The shared lock works similarly to the exclusive lock except
541 * nc_locktd is left NULL and we need an interlock (VHOLD) to
542 * prevent vhold() races, since the moment our cmpset_int succeeds
543 * another cpu can come in and get its own shared lock.
544 *
545 * A critical section is needed to prevent interruption during the
546 * VHOLD interlock.
547 */
548static
549int
550_cache_lock_shared_nonblock(struct namecache *ncp)
551{
552 u_int count;
553
554 for (;;) {
555 count = ncp->nc_lockstatus;
556
557 if ((count & ~NC_SHLOCK_REQ) == 0) {
558 crit_enter();
559 if (atomic_cmpset_int(&ncp->nc_lockstatus,
560 count,
561 (count + 1) | NC_SHLOCK_FLAG |
562 NC_SHLOCK_VHOLD)) {
563 /*
564 * The vp associated with a locked ncp must
565 * be held to prevent it from being recycled.
566 *
567 * WARNING! If VRECLAIMED is set the vnode
568 * could already be in the middle of a recycle.
569 * Callers must use cache_vref() or
570 * cache_vget() on the locked ncp to
571 * validate the vp or set the cache entry
572 * to unresolved.
573 *
574 * NOTE! vhold() is allowed if we hold a
575 * lock on the ncp (which we do).
576 */
577 if (ncp->nc_vp)
578 vhold(ncp->nc_vp);
579 atomic_clear_int(&ncp->nc_lockstatus,
580 NC_SHLOCK_VHOLD);
581 crit_exit();
582 break;
583 }
584 /* cmpset failed */
585 crit_exit();
586 continue;
587 }
588
589 /*
590 * If already held shared we can just bump the count, but
591 * only allow this if nobody is trying to get the lock
592 * exclusively.
593 *
594 * VHOLD is a bit of a hack. Even though we successfully
595 * added another shared ref, the cpu that got the first
596 * shared ref might not yet have held the vnode.
597 */
598 if ((count & (NC_EXLOCK_REQ|NC_SHLOCK_FLAG)) ==
599 NC_SHLOCK_FLAG) {
600 KKASSERT((count & ~(NC_EXLOCK_REQ |
601 NC_SHLOCK_REQ |
602 NC_SHLOCK_FLAG)) > 0);
603 if (atomic_cmpset_int(&ncp->nc_lockstatus,
604 count, count + 1)) {
605 while (ncp->nc_lockstatus & NC_SHLOCK_VHOLD)
606 cpu_pause();
607 break;
608 }
609 continue;
610 }
611 return(EWOULDBLOCK);
612 }
613 return(0);
614}
615
f63911bf
MD
616/*
617 * Helper function
618 *
619 * NOTE: nc_refs can be 0 (degenerate case during _cache_drop).
2247fe02 620 *
79fd1696 621 * nc_locktd must be NULLed out prior to nc_lockstatus getting cleared.
f63911bf
MD
622 */
623static
624void
625_cache_unlock(struct namecache *ncp)
626{
627 thread_t td __debugvar = curthread;
2247fe02 628 u_int count;
79fd1696
MD
629 u_int ncount;
630 struct vnode *dropvp;
f63911bf
MD
631
632 KKASSERT(ncp->nc_refs >= 0);
79fd1696
MD
633 KKASSERT((ncp->nc_lockstatus & ~(NC_EXLOCK_REQ|NC_SHLOCK_REQ)) > 0);
634 KKASSERT((ncp->nc_lockstatus & NC_SHLOCK_FLAG) || ncp->nc_locktd == td);
635
636 count = ncp->nc_lockstatus;
637 cpu_ccfence();
f63911bf 638
79fd1696
MD
639 /*
640 * Clear nc_locktd prior to the atomic op (excl lock only)
641 */
642 if ((count & ~(NC_EXLOCK_REQ|NC_SHLOCK_REQ)) == 1)
2247fe02 643 ncp->nc_locktd = NULL;
79fd1696
MD
644 dropvp = NULL;
645
2247fe02 646 for (;;) {
79fd1696
MD
647 if ((count &
648 ~(NC_EXLOCK_REQ|NC_SHLOCK_REQ|NC_SHLOCK_FLAG)) == 1) {
649 dropvp = ncp->nc_vp;
650 if (count & NC_EXLOCK_REQ)
651 ncount = count & NC_SHLOCK_REQ; /* cnt->0 */
652 else
653 ncount = 0;
654
655 if (atomic_cmpset_int(&ncp->nc_lockstatus,
656 count, ncount)) {
2247fe02 657 if (count & NC_EXLOCK_REQ)
79fd1696
MD
658 wakeup(&ncp->nc_locktd);
659 else if (count & NC_SHLOCK_REQ)
2247fe02
MD
660 wakeup(ncp);
661 break;
662 }
79fd1696 663 dropvp = NULL;
2247fe02 664 } else {
79fd1696
MD
665 KKASSERT((count & NC_SHLOCK_VHOLD) == 0);
666 KKASSERT((count & ~(NC_EXLOCK_REQ |
667 NC_SHLOCK_REQ |
668 NC_SHLOCK_FLAG)) > 1);
669 if (atomic_cmpset_int(&ncp->nc_lockstatus,
670 count, count - 1)) {
2247fe02
MD
671 break;
672 }
f63911bf 673 }
79fd1696
MD
674 count = ncp->nc_lockstatus;
675 cpu_ccfence();
f63911bf 676 }
79fd1696
MD
677
678 /*
679 * Don't actually drop the vp until we successfully clean out
680 * the lock, otherwise we may race another shared lock.
681 */
682 if (dropvp)
683 vdrop(dropvp);
f63911bf
MD
684}
685
79fd1696
MD
686static
687int
688_cache_lockstatus(struct namecache *ncp)
689{
690 if (ncp->nc_locktd == curthread)
691 return(LK_EXCLUSIVE);
692 if (ncp->nc_lockstatus & NC_SHLOCK_FLAG)
693 return(LK_SHARED);
694 return(-1);
695}
984263bc
MD
696
697/*
7ea21ed1
MD
698 * cache_hold() and cache_drop() prevent the premature deletion of a
699 * namecache entry but do not prevent operations (such as zapping) on
700 * that namecache entry.
5b287bba 701 *
36e90efd
MD
702 * This routine may only be called from outside this source module if
703 * nc_refs is already at least 1.
5b287bba 704 *
36e90efd
MD
705 * This is a rare case where callers are allowed to hold a spinlock,
706 * so we can't ourselves.
984263bc 707 */
7ea21ed1
MD
708static __inline
709struct namecache *
bc0c094e 710_cache_hold(struct namecache *ncp)
7ea21ed1 711{
5b287bba 712 atomic_add_int(&ncp->nc_refs, 1);
7ea21ed1
MD
713 return(ncp);
714}
715
8c361dda 716/*
f63911bf
MD
717 * Drop a cache entry, taking care to deal with races.
718 *
719 * For potential 1->0 transitions we must hold the ncp lock to safely
720 * test its flags. An unresolved entry with no children must be zapped
721 * to avoid leaks.
722 *
723 * The call to cache_zap() itself will handle all remaining races and
724 * will decrement the ncp's refs regardless. If we are resolved or
725 * have children nc_refs can safely be dropped to 0 without having to
726 * zap the entry.
727 *
728 * NOTE: cache_zap() will re-check nc_refs and nc_list in a MPSAFE fashion.
729 *
730 * NOTE: cache_zap() may return a non-NULL referenced parent which must
731 * be dropped in a loop.
8c361dda 732 */
7ea21ed1
MD
733static __inline
734void
bc0c094e 735_cache_drop(struct namecache *ncp)
7ea21ed1 736{
f63911bf
MD
737 int refs;
738
739 while (ncp) {
740 KKASSERT(ncp->nc_refs > 0);
741 refs = ncp->nc_refs;
742
743 if (refs == 1) {
744 if (_cache_lock_nonblock(ncp) == 0) {
055f5cc8 745 ncp->nc_flag &= ~NCF_DEFEREDZAP;
f63911bf
MD
746 if ((ncp->nc_flag & NCF_UNRESOLVED) &&
747 TAILQ_EMPTY(&ncp->nc_list)) {
65870584 748 ncp = cache_zap(ncp, 1);
f63911bf
MD
749 continue;
750 }
751 if (atomic_cmpset_int(&ncp->nc_refs, 1, 0)) {
752 _cache_unlock(ncp);
753 break;
754 }
755 _cache_unlock(ncp);
756 }
757 } else {
758 if (atomic_cmpset_int(&ncp->nc_refs, refs, refs - 1))
759 break;
760 }
2247fe02 761 cpu_pause();
f517a1bb 762 }
7ea21ed1 763}
8987aad7 764
690a3127 765/*
2247fe02
MD
766 * Link a new namecache entry to its parent and to the hash table. Be
767 * careful to avoid races if vhold() blocks in the future.
768 *
769 * Both ncp and par must be referenced and locked.
770 *
771 * NOTE: The hash table spinlock is likely held during this call, we
772 * can't do anything fancy.
690a3127
MD
773 */
774static void
2247fe02
MD
775_cache_link_parent(struct namecache *ncp, struct namecache *par,
776 struct nchash_head *nchpp)
690a3127
MD
777{
778 KKASSERT(ncp->nc_parent == NULL);
779 ncp->nc_parent = par;
2247fe02 780 ncp->nc_head = nchpp;
aabd5ce8
MD
781
782 /*
783 * Set inheritance flags. Note that the parent flags may be
784 * stale due to getattr potentially not having been run yet
785 * (it gets run during nlookup()'s).
786 */
787 ncp->nc_flag &= ~(NCF_SF_PNOCACHE | NCF_UF_PCACHE);
788 if (par->nc_flag & (NCF_SF_NOCACHE | NCF_SF_PNOCACHE))
789 ncp->nc_flag |= NCF_SF_PNOCACHE;
790 if (par->nc_flag & (NCF_UF_CACHE | NCF_UF_PCACHE))
791 ncp->nc_flag |= NCF_UF_PCACHE;
792
2247fe02
MD
793 LIST_INSERT_HEAD(&nchpp->list, ncp, nc_hash);
794
690a3127
MD
795 if (TAILQ_EMPTY(&par->nc_list)) {
796 TAILQ_INSERT_HEAD(&par->nc_list, ncp, nc_entry);
21739618
MD
797 /*
798 * Any vp associated with an ncp which has children must
55361147 799 * be held to prevent it from being recycled.
21739618 800 */
690a3127 801 if (par->nc_vp)
2247fe02 802 vhold(par->nc_vp);
690a3127
MD
803 } else {
804 TAILQ_INSERT_HEAD(&par->nc_list, ncp, nc_entry);
805 }
806}
807
808/*
2247fe02
MD
809 * Remove the parent and hash associations from a namecache structure.
810 * If this is the last child of the parent the cache_drop(par) will
811 * attempt to recursively zap the parent.
812 *
813 * ncp must be locked. This routine will acquire a temporary lock on
814 * the parent as wlel as the appropriate hash chain.
690a3127
MD
815 */
816static void
f63911bf 817_cache_unlink_parent(struct namecache *ncp)
690a3127
MD
818{
819 struct namecache *par;
f63911bf 820 struct vnode *dropvp;
690a3127
MD
821
822 if ((par = ncp->nc_parent) != NULL) {
2247fe02 823 KKASSERT(ncp->nc_parent == par);
f63911bf 824 _cache_hold(par);
2247fe02 825 _cache_lock(par);
287a8577 826 spin_lock(&ncp->nc_head->spin);
2247fe02 827 LIST_REMOVE(ncp, nc_hash);
690a3127 828 TAILQ_REMOVE(&par->nc_list, ncp, nc_entry);
f63911bf 829 dropvp = NULL;
690a3127 830 if (par->nc_vp && TAILQ_EMPTY(&par->nc_list))
f63911bf 831 dropvp = par->nc_vp;
287a8577 832 spin_unlock(&ncp->nc_head->spin);
2247fe02
MD
833 ncp->nc_parent = NULL;
834 ncp->nc_head = NULL;
835 _cache_unlock(par);
28623bf9 836 _cache_drop(par);
f63911bf
MD
837
838 /*
839 * We can only safely vdrop with no spinlocks held.
840 */
841 if (dropvp)
842 vdrop(dropvp);
690a3127
MD
843 }
844}
845
846/*
fad57d0e
MD
847 * Allocate a new namecache structure. Most of the code does not require
848 * zero-termination of the string but it makes vop_compat_ncreate() easier.
690a3127
MD
849 */
850static struct namecache *
524c845c 851cache_alloc(int nlen)
690a3127
MD
852{
853 struct namecache *ncp;
854
efda3bd0 855 ncp = kmalloc(sizeof(*ncp), M_VFSCACHE, M_WAITOK|M_ZERO);
524c845c 856 if (nlen)
efda3bd0 857 ncp->nc_name = kmalloc(nlen + 1, M_VFSCACHE, M_WAITOK);
524c845c 858 ncp->nc_nlen = nlen;
690a3127
MD
859 ncp->nc_flag = NCF_UNRESOLVED;
860 ncp->nc_error = ENOTCONN; /* needs to be resolved */
8c361dda 861 ncp->nc_refs = 1;
e4bff3c8 862
690a3127 863 TAILQ_INIT(&ncp->nc_list);
28623bf9 864 _cache_lock(ncp);
690a3127
MD
865 return(ncp);
866}
867
f63911bf
MD
868/*
869 * Can only be called for the case where the ncp has never been
870 * associated with anything (so no spinlocks are needed).
871 */
8c361dda 872static void
28623bf9 873_cache_free(struct namecache *ncp)
8c361dda 874{
79fd1696 875 KKASSERT(ncp->nc_refs == 1 && ncp->nc_lockstatus == 1);
8c361dda 876 if (ncp->nc_name)
efda3bd0
MD
877 kfree(ncp->nc_name, M_VFSCACHE);
878 kfree(ncp, M_VFSCACHE);
8c361dda 879}
690a3127 880
2247fe02 881/*
79fd1696 882 * [re]initialize a nchandle.
2247fe02 883 */
28623bf9
MD
884void
885cache_zero(struct nchandle *nch)
886{
887 nch->ncp = NULL;
888 nch->mount = NULL;
889}
890
690a3127
MD
891/*
892 * Ref and deref a namecache structure.
5b287bba 893 *
2247fe02
MD
894 * The caller must specify a stable ncp pointer, typically meaning the
895 * ncp is already referenced but this can also occur indirectly through
896 * e.g. holding a lock on a direct child.
897 *
898 * WARNING: Caller may hold an unrelated read spinlock, which means we can't
899 * use read spinlocks here.
61f96b6f
MD
900 *
901 * MPSAFE if nch is
690a3127 902 */
28623bf9
MD
903struct nchandle *
904cache_hold(struct nchandle *nch)
bc0c094e 905{
28623bf9 906 _cache_hold(nch->ncp);
61f96b6f 907 atomic_add_int(&nch->mount->mnt_refs, 1);
28623bf9 908 return(nch);
bc0c094e
MD
909}
910
61f96b6f
MD
911/*
912 * Create a copy of a namecache handle for an already-referenced
913 * entry.
914 *
915 * MPSAFE if nch is
916 */
bc0c094e 917void
28623bf9 918cache_copy(struct nchandle *nch, struct nchandle *target)
bc0c094e 919{
28623bf9 920 *target = *nch;
cf37bc1a
MD
921 if (target->ncp)
922 _cache_hold(target->ncp);
61f96b6f 923 atomic_add_int(&nch->mount->mnt_refs, 1);
28623bf9
MD
924}
925
61f96b6f
MD
926/*
927 * MPSAFE if nch is
928 */
28623bf9
MD
929void
930cache_changemount(struct nchandle *nch, struct mount *mp)
931{
61f96b6f 932 atomic_add_int(&nch->mount->mnt_refs, -1);
28623bf9 933 nch->mount = mp;
61f96b6f 934 atomic_add_int(&nch->mount->mnt_refs, 1);
28623bf9
MD
935}
936
937void
938cache_drop(struct nchandle *nch)
939{
61f96b6f 940 atomic_add_int(&nch->mount->mnt_refs, -1);
28623bf9
MD
941 _cache_drop(nch->ncp);
942 nch->ncp = NULL;
943 nch->mount = NULL;
bc0c094e
MD
944}
945
79fd1696
MD
946int
947cache_lockstatus(struct nchandle *nch)
948{
949 return(_cache_lockstatus(nch->ncp));
950}
951
28623bf9
MD
952void
953cache_lock(struct nchandle *nch)
954{
955 _cache_lock(nch->ncp);
956}
957
79fd1696
MD
958void
959cache_lock_maybe_shared(struct nchandle *nch, int excl)
960{
961 struct namecache *ncp = nch->ncp;
962
963 if (ncp_shared_lock_disable || excl ||
964 (ncp->nc_flag & NCF_UNRESOLVED)) {
965 _cache_lock(ncp);
966 } else {
967 _cache_lock_shared(ncp);
968 if ((ncp->nc_flag & NCF_UNRESOLVED) == 0) {
969 if (ncp->nc_vp && (ncp->nc_vp->v_flag & VRECLAIMED)) {
970 _cache_unlock(ncp);
971 _cache_lock(ncp);
972 }
973 } else {
974 _cache_unlock(ncp);
975 _cache_lock(ncp);
976 }
977 }
978}
979
2247fe02
MD
980/*
981 * Relock nch1 given an unlocked nch1 and a locked nch2. The caller
982 * is responsible for checking both for validity on return as they
983 * may have become invalid.
984 *
985 * We have to deal with potential deadlocks here, just ping pong
986 * the lock until we get it (we will always block somewhere when
987 * looping so this is not cpu-intensive).
988 *
989 * which = 0 nch1 not locked, nch2 is locked
990 * which = 1 nch1 is locked, nch2 is not locked
991 */
992void
993cache_relock(struct nchandle *nch1, struct ucred *cred1,
994 struct nchandle *nch2, struct ucred *cred2)
995{
996 int which;
997
998 which = 0;
999
1000 for (;;) {
1001 if (which == 0) {
1002 if (cache_lock_nonblock(nch1) == 0) {
1003 cache_resolve(nch1, cred1);
1004 break;
1005 }
1006 cache_unlock(nch2);
1007 cache_lock(nch1);
1008 cache_resolve(nch1, cred1);
1009 which = 1;
1010 } else {
1011 if (cache_lock_nonblock(nch2) == 0) {
1012 cache_resolve(nch2, cred2);
1013 break;
1014 }
1015 cache_unlock(nch1);
1016 cache_lock(nch2);
1017 cache_resolve(nch2, cred2);
1018 which = 0;
1019 }
1020 }
1021}
1022
28623bf9
MD
1023int
1024cache_lock_nonblock(struct nchandle *nch)
1025{
1026 return(_cache_lock_nonblock(nch->ncp));
1027}
1028
28623bf9
MD
1029void
1030cache_unlock(struct nchandle *nch)
1031{
1032 _cache_unlock(nch->ncp);
1033}
1034
14c92d03 1035/*
690a3127 1036 * ref-and-lock, unlock-and-deref functions.
9b1b3591
MD
1037 *
1038 * This function is primarily used by nlookup. Even though cache_lock
1039 * holds the vnode, it is possible that the vnode may have already
f63911bf
MD
1040 * initiated a recyclement.
1041 *
1042 * We want cache_get() to return a definitively usable vnode or a
1043 * definitively unresolved ncp.
14c92d03 1044 */
28623bf9 1045static
21739618 1046struct namecache *
28623bf9 1047_cache_get(struct namecache *ncp)
690a3127
MD
1048{
1049 _cache_hold(ncp);
28623bf9 1050 _cache_lock(ncp);
9b1b3591 1051 if (ncp->nc_vp && (ncp->nc_vp->v_flag & VRECLAIMED))
28623bf9 1052 _cache_setunresolved(ncp);
21739618 1053 return(ncp);
690a3127
MD
1054}
1055
79fd1696
MD
1056/*
1057 * Attempt to obtain a shared lock on the ncp. A shared lock will only
1058 * be obtained if the ncp is resolved and the vnode (if not ENOENT) is
1059 * valid. Otherwise an exclusive lock will be acquired instead.
1060 */
1061static
1062struct namecache *
1063_cache_get_maybe_shared(struct namecache *ncp, int excl)
1064{
1065 if (ncp_shared_lock_disable || excl ||
1066 (ncp->nc_flag & NCF_UNRESOLVED)) {
1067 return(_cache_get(ncp));
1068 }
1069 _cache_hold(ncp);
1070 _cache_lock_shared(ncp);
1071 if ((ncp->nc_flag & NCF_UNRESOLVED) == 0) {
1072 if (ncp->nc_vp && (ncp->nc_vp->v_flag & VRECLAIMED)) {
1073 _cache_unlock(ncp);
1074 ncp = _cache_get(ncp);
1075 _cache_drop(ncp);
1076 }
1077 } else {
1078 _cache_unlock(ncp);
1079 ncp = _cache_get(ncp);
1080 _cache_drop(ncp);
1081 }
1082 return(ncp);
1083}
1084
28623bf9 1085/*
2247fe02 1086 * This is a special form of _cache_lock() which only succeeds if
f63911bf
MD
1087 * it can get a pristine, non-recursive lock. The caller must have
1088 * already ref'd the ncp.
1089 *
1090 * On success the ncp will be locked, on failure it will not. The
1091 * ref count does not change either way.
1092 *
2247fe02 1093 * We want _cache_lock_special() (on success) to return a definitively
f63911bf
MD
1094 * usable vnode or a definitively unresolved ncp.
1095 */
1096static int
2247fe02 1097_cache_lock_special(struct namecache *ncp)
f63911bf
MD
1098{
1099 if (_cache_lock_nonblock(ncp) == 0) {
79fd1696
MD
1100 if ((ncp->nc_lockstatus &
1101 ~(NC_EXLOCK_REQ|NC_SHLOCK_REQ)) == 1) {
f63911bf
MD
1102 if (ncp->nc_vp && (ncp->nc_vp->v_flag & VRECLAIMED))
1103 _cache_setunresolved(ncp);
1104 return(0);
1105 }
1106 _cache_unlock(ncp);
1107 }
1108 return(EWOULDBLOCK);
1109}
1110
79fd1696
MD
1111static int
1112_cache_lock_shared_special(struct namecache *ncp)
1113{
1114 if (_cache_lock_shared_nonblock(ncp) == 0) {
1115 if ((ncp->nc_lockstatus &
1116 ~(NC_EXLOCK_REQ|NC_SHLOCK_REQ)) == (NC_SHLOCK_FLAG | 1)) {
1117 if (ncp->nc_vp == NULL ||
1118 (ncp->nc_vp->v_flag & VRECLAIMED) == 0) {
1119 return(0);
1120 }
1121 }
1122 _cache_unlock(ncp);
1123 }
1124 return(EWOULDBLOCK);
1125}
1126
f63911bf
MD
1127
1128/*
1129 * NOTE: The same nchandle can be passed for both arguments.
28623bf9
MD
1130 */
1131void
1132cache_get(struct nchandle *nch, struct nchandle *target)
1133{
f63911bf 1134 KKASSERT(nch->ncp->nc_refs > 0);
28623bf9
MD
1135 target->mount = nch->mount;
1136 target->ncp = _cache_get(nch->ncp);
61f96b6f 1137 atomic_add_int(&target->mount->mnt_refs, 1);
28623bf9
MD
1138}
1139
79fd1696
MD
1140void
1141cache_get_maybe_shared(struct nchandle *nch, struct nchandle *target, int excl)
1142{
1143 KKASSERT(nch->ncp->nc_refs > 0);
1144 target->mount = nch->mount;
1145 target->ncp = _cache_get_maybe_shared(nch->ncp, excl);
1146 atomic_add_int(&target->mount->mnt_refs, 1);
1147}
1148
2247fe02 1149/*
79fd1696 1150 *
2247fe02 1151 */
28623bf9 1152static __inline
14c92d03 1153void
28623bf9 1154_cache_put(struct namecache *ncp)
14c92d03 1155{
28623bf9 1156 _cache_unlock(ncp);
14c92d03
MD
1157 _cache_drop(ncp);
1158}
1159
2247fe02 1160/*
79fd1696 1161 *
2247fe02 1162 */
28623bf9
MD
1163void
1164cache_put(struct nchandle *nch)
1165{
61f96b6f 1166 atomic_add_int(&nch->mount->mnt_refs, -1);
28623bf9
MD
1167 _cache_put(nch->ncp);
1168 nch->ncp = NULL;
1169 nch->mount = NULL;
1170}
1171
690a3127
MD
1172/*
1173 * Resolve an unresolved ncp by associating a vnode with it. If the
1174 * vnode is NULL, a negative cache entry is created.
1175 *
1176 * The ncp should be locked on entry and will remain locked on return.
1177 */
28623bf9 1178static
690a3127 1179void
4b5bbb78 1180_cache_setvp(struct mount *mp, struct namecache *ncp, struct vnode *vp)
ce6da7e4 1181{
690a3127 1182 KKASSERT(ncp->nc_flag & NCF_UNRESOLVED);
79fd1696 1183 KKASSERT(_cache_lockstatus(ncp) == LK_EXCLUSIVE);
2247fe02 1184
ce6da7e4 1185 if (vp != NULL) {
21739618
MD
1186 /*
1187 * Any vp associated with an ncp which has children must
55361147 1188 * be held. Any vp associated with a locked ncp must be held.
21739618
MD
1189 */
1190 if (!TAILQ_EMPTY(&ncp->nc_list))
1191 vhold(vp);
b12defdc 1192 spin_lock(&vp->v_spin);
f63911bf 1193 ncp->nc_vp = vp;
ce6da7e4 1194 TAILQ_INSERT_HEAD(&vp->v_namecache, ncp, nc_vnode);
b12defdc 1195 spin_unlock(&vp->v_spin);
79fd1696 1196 if (ncp->nc_lockstatus & ~(NC_EXLOCK_REQ|NC_SHLOCK_REQ))
55361147 1197 vhold(vp);
21739618
MD
1198
1199 /*
3c37c940 1200 * Set auxiliary flags
21739618 1201 */
690a3127
MD
1202 switch(vp->v_type) {
1203 case VDIR:
21739618
MD
1204 ncp->nc_flag |= NCF_ISDIR;
1205 break;
690a3127 1206 case VLNK:
21739618
MD
1207 ncp->nc_flag |= NCF_ISSYMLINK;
1208 /* XXX cache the contents of the symlink */
1209 break;
690a3127 1210 default:
21739618 1211 break;
690a3127 1212 }
f63911bf 1213 atomic_add_int(&numcache, 1);
21739618 1214 ncp->nc_error = 0;
f423d507
FT
1215 /* XXX: this is a hack to work-around the lack of a real pfs vfs
1216 * implementation*/
1217 if (mp != NULL)
35d98733
FT
1218 if (strncmp(mp->mnt_stat.f_fstypename, "null", 5) == 0)
1219 vp->v_pfsmp = mp;
ce6da7e4 1220 } else {
4b5bbb78
MD
1221 /*
1222 * When creating a negative cache hit we set the
1223 * namecache_gen. A later resolve will clean out the
1224 * negative cache hit if the mount point's namecache_gen
1225 * has changed. Used by devfs, could also be used by
1226 * other remote FSs.
1227 */
f63911bf 1228 ncp->nc_vp = NULL;
287a8577 1229 spin_lock(&ncspin);
1345c2b6 1230 TAILQ_INSERT_TAIL(&ncneglist, ncp, nc_vnode);
ce6da7e4 1231 ++numneg;
287a8577 1232 spin_unlock(&ncspin);
21739618 1233 ncp->nc_error = ENOENT;
4b5bbb78 1234 if (mp)
75779c3c 1235 VFS_NCPGEN_SET(mp, ncp);
ce6da7e4 1236 }
65870584 1237 ncp->nc_flag &= ~(NCF_UNRESOLVED | NCF_DEFEREDZAP);
ce6da7e4
MD
1238}
1239
2247fe02 1240/*
79fd1696 1241 *
2247fe02 1242 */
fad57d0e 1243void
28623bf9 1244cache_setvp(struct nchandle *nch, struct vnode *vp)
fad57d0e 1245{
4b5bbb78 1246 _cache_setvp(nch->mount, nch->ncp, vp);
28623bf9
MD
1247}
1248
2247fe02 1249/*
79fd1696 1250 *
2247fe02 1251 */
28623bf9
MD
1252void
1253cache_settimeout(struct nchandle *nch, int nticks)
1254{
1255 struct namecache *ncp = nch->ncp;
1256
fad57d0e
MD
1257 if ((ncp->nc_timeout = ticks + nticks) == 0)
1258 ncp->nc_timeout = 1;
1259}
1260
690a3127
MD
1261/*
1262 * Disassociate the vnode or negative-cache association and mark a
1263 * namecache entry as unresolved again. Note that the ncp is still
1264 * left in the hash table and still linked to its parent.
1265 *
67773eb3
MD
1266 * The ncp should be locked and refd on entry and will remain locked and refd
1267 * on return.
8c361dda
MD
1268 *
1269 * This routine is normally never called on a directory containing children.
1270 * However, NFS often does just that in its rename() code as a cop-out to
1271 * avoid complex namespace operations. This disconnects a directory vnode
1272 * from its namecache and can cause the OLDAPI and NEWAPI to get out of
1273 * sync.
2247fe02 1274 *
690a3127 1275 */
28623bf9 1276static
690a3127 1277void
28623bf9 1278_cache_setunresolved(struct namecache *ncp)
14c92d03 1279{
690a3127 1280 struct vnode *vp;
14c92d03 1281
690a3127
MD
1282 if ((ncp->nc_flag & NCF_UNRESOLVED) == 0) {
1283 ncp->nc_flag |= NCF_UNRESOLVED;
fad57d0e 1284 ncp->nc_timeout = 0;
690a3127 1285 ncp->nc_error = ENOTCONN;
690a3127 1286 if ((vp = ncp->nc_vp) != NULL) {
f63911bf 1287 atomic_add_int(&numcache, -1);
b12defdc 1288 spin_lock(&vp->v_spin);
fad57d0e 1289 ncp->nc_vp = NULL;
690a3127 1290 TAILQ_REMOVE(&vp->v_namecache, ncp, nc_vnode);
b12defdc 1291 spin_unlock(&vp->v_spin);
55361147
MD
1292
1293 /*
1294 * Any vp associated with an ncp with children is
1295 * held by that ncp. Any vp associated with a locked
1296 * ncp is held by that ncp. These conditions must be
1297 * undone when the vp is cleared out from the ncp.
1298 */
690a3127
MD
1299 if (!TAILQ_EMPTY(&ncp->nc_list))
1300 vdrop(vp);
79fd1696 1301 if (ncp->nc_lockstatus & ~(NC_EXLOCK_REQ|NC_SHLOCK_REQ))
55361147 1302 vdrop(vp);
690a3127 1303 } else {
287a8577 1304 spin_lock(&ncspin);
690a3127
MD
1305 TAILQ_REMOVE(&ncneglist, ncp, nc_vnode);
1306 --numneg;
287a8577 1307 spin_unlock(&ncspin);
690a3127 1308 }
d98152a8 1309 ncp->nc_flag &= ~(NCF_WHITEOUT|NCF_ISDIR|NCF_ISSYMLINK);
8e005a45
MD
1310 }
1311}
8c361dda 1312
4b5bbb78
MD
1313/*
1314 * The cache_nresolve() code calls this function to automatically
1315 * set a resolved cache element to unresolved if it has timed out
1316 * or if it is a negative cache hit and the mount point namecache_gen
1317 * has changed.
1318 */
79fd1696
MD
1319static __inline int
1320_cache_auto_unresolve_test(struct mount *mp, struct namecache *ncp)
4b5bbb78 1321{
4b5bbb78
MD
1322 /*
1323 * Try to zap entries that have timed out. We have
1324 * to be careful here because locked leafs may depend
1325 * on the vnode remaining intact in a parent, so only
1326 * do this under very specific conditions.
1327 */
1328 if (ncp->nc_timeout && (int)(ncp->nc_timeout - ticks) < 0 &&
1329 TAILQ_EMPTY(&ncp->nc_list)) {
79fd1696 1330 return 1;
4b5bbb78
MD
1331 }
1332
1333 /*
1334 * If a resolved negative cache hit is invalid due to
1335 * the mount's namecache generation being bumped, zap it.
1336 */
75779c3c 1337 if (ncp->nc_vp == NULL && VFS_NCPGEN_TEST(mp, ncp)) {
79fd1696
MD
1338 return 1;
1339 }
1340
1341 /*
1342 * Otherwise we are good
1343 */
1344 return 0;
1345}
1346
1347static __inline void
1348_cache_auto_unresolve(struct mount *mp, struct namecache *ncp)
1349{
1350 /*
1351 * Already in an unresolved state, nothing to do.
1352 */
1353 if ((ncp->nc_flag & NCF_UNRESOLVED) == 0) {
1354 if (_cache_auto_unresolve_test(mp, ncp))
1355 _cache_setunresolved(ncp);
4b5bbb78
MD
1356 }
1357}
1358
2247fe02 1359/*
79fd1696 1360 *
2247fe02 1361 */
1d505369 1362void
28623bf9 1363cache_setunresolved(struct nchandle *nch)
1d505369 1364{
28623bf9 1365 _cache_setunresolved(nch->ncp);
1d505369
MD
1366}
1367
1368/*
28623bf9
MD
1369 * Determine if we can clear NCF_ISMOUNTPT by scanning the mountlist
1370 * looking for matches. This flag tells the lookup code when it must
1371 * check for a mount linkage and also prevents the directories in question
1372 * from being deleted or renamed.
1d505369 1373 */
28623bf9
MD
1374static
1375int
1376cache_clrmountpt_callback(struct mount *mp, void *data)
1377{
1378 struct nchandle *nch = data;
1379
1380 if (mp->mnt_ncmounton.ncp == nch->ncp)
1381 return(1);
1382 if (mp->mnt_ncmountpt.ncp == nch->ncp)
1383 return(1);
1384 return(0);
1385}
1386
2247fe02 1387/*
79fd1696 1388 *
2247fe02 1389 */
1d505369 1390void
28623bf9 1391cache_clrmountpt(struct nchandle *nch)
1d505369 1392{
28623bf9
MD
1393 int count;
1394
1395 count = mountlist_scan(cache_clrmountpt_callback, nch,
1396 MNTSCAN_FORWARD|MNTSCAN_NOBUSY);
1397 if (count == 0)
1398 nch->ncp->nc_flag &= ~NCF_ISMOUNTPT;
1d505369
MD
1399}
1400
8e005a45 1401/*
e09206ba
MD
1402 * Invalidate portions of the namecache topology given a starting entry.
1403 * The passed ncp is set to an unresolved state and:
8e005a45 1404 *
2247fe02
MD
1405 * The passed ncp must be referencxed and locked. The routine may unlock
1406 * and relock ncp several times, and will recheck the children and loop
1407 * to catch races. When done the passed ncp will be returned with the
1408 * reference and lock intact.
e09206ba
MD
1409 *
1410 * CINV_DESTROY - Set a flag in the passed ncp entry indicating
1411 * that the physical underlying nodes have been
1412 * destroyed... as in deleted. For example, when
1413 * a directory is removed. This will cause record
1414 * lookups on the name to no longer be able to find
1415 * the record and tells the resolver to return failure
1416 * rather then trying to resolve through the parent.
1417 *
1418 * The topology itself, including ncp->nc_name,
1419 * remains intact.
1420 *
1421 * This only applies to the passed ncp, if CINV_CHILDREN
1422 * is specified the children are not flagged.
1423 *
1424 * CINV_CHILDREN - Set all children (recursively) to an unresolved
1425 * state as well.
1426 *
1427 * Note that this will also have the side effect of
1428 * cleaning out any unreferenced nodes in the topology
1429 * from the leaves up as the recursion backs out.
1430 *
2247fe02
MD
1431 * Note that the topology for any referenced nodes remains intact, but
1432 * the nodes will be marked as having been destroyed and will be set
1433 * to an unresolved state.
25cb3304
MD
1434 *
1435 * It is possible for cache_inval() to race a cache_resolve(), meaning that
1436 * the namecache entry may not actually be invalidated on return if it was
1437 * revalidated while recursing down into its children. This code guarentees
1438 * that the node(s) will go through an invalidation cycle, but does not
1439 * guarentee that they will remain in an invalidated state.
1440 *
1441 * Returns non-zero if a revalidation was detected during the invalidation
1442 * recursion, zero otherwise. Note that since only the original ncp is
1443 * locked the revalidation ultimately can only indicate that the original ncp
1444 * *MIGHT* no have been reresolved.
bf40a153
MD
1445 *
1446 * DEEP RECURSION HANDLING - If a recursive invalidation recurses deeply we
1447 * have to avoid blowing out the kernel stack. We do this by saving the
1448 * deep namecache node and aborting the recursion, then re-recursing at that
1449 * node using a depth-first algorithm in order to allow multiple deep
1450 * recursions to chain through each other, then we restart the invalidation
1451 * from scratch.
8e005a45 1452 */
bf40a153
MD
1453
1454struct cinvtrack {
1455 struct namecache *resume_ncp;
1456 int depth;
1457};
1458
28623bf9 1459static int _cache_inval_internal(struct namecache *, int, struct cinvtrack *);
bf40a153 1460
28623bf9 1461static
25cb3304 1462int
28623bf9 1463_cache_inval(struct namecache *ncp, int flags)
bf40a153
MD
1464{
1465 struct cinvtrack track;
1466 struct namecache *ncp2;
1467 int r;
1468
1469 track.depth = 0;
1470 track.resume_ncp = NULL;
1471
1472 for (;;) {
28623bf9 1473 r = _cache_inval_internal(ncp, flags, &track);
bf40a153
MD
1474 if (track.resume_ncp == NULL)
1475 break;
6ea70f76 1476 kprintf("Warning: deep namecache recursion at %s\n",
bf40a153 1477 ncp->nc_name);
28623bf9 1478 _cache_unlock(ncp);
bf40a153
MD
1479 while ((ncp2 = track.resume_ncp) != NULL) {
1480 track.resume_ncp = NULL;
28623bf9
MD
1481 _cache_lock(ncp2);
1482 _cache_inval_internal(ncp2, flags & ~CINV_DESTROY,
bf40a153 1483 &track);
28623bf9 1484 _cache_put(ncp2);
bf40a153 1485 }
28623bf9 1486 _cache_lock(ncp);
bf40a153
MD
1487 }
1488 return(r);
1489}
1490
28623bf9
MD
1491int
1492cache_inval(struct nchandle *nch, int flags)
1493{
1494 return(_cache_inval(nch->ncp, flags));
1495}
1496
2247fe02
MD
1497/*
1498 * Helper for _cache_inval(). The passed ncp is refd and locked and
1499 * remains that way on return, but may be unlocked/relocked multiple
1500 * times by the routine.
1501 */
bf40a153 1502static int
28623bf9 1503_cache_inval_internal(struct namecache *ncp, int flags, struct cinvtrack *track)
8e005a45
MD
1504{
1505 struct namecache *kid;
b8997912 1506 struct namecache *nextkid;
25cb3304 1507 int rcnt = 0;
8e005a45 1508
79fd1696 1509 KKASSERT(_cache_lockstatus(ncp) == LK_EXCLUSIVE);
25cb3304 1510
28623bf9 1511 _cache_setunresolved(ncp);
e09206ba
MD
1512 if (flags & CINV_DESTROY)
1513 ncp->nc_flag |= NCF_DESTROYED;
e09206ba
MD
1514 if ((flags & CINV_CHILDREN) &&
1515 (kid = TAILQ_FIRST(&ncp->nc_list)) != NULL
1516 ) {
f63911bf 1517 _cache_hold(kid);
bf40a153
MD
1518 if (++track->depth > MAX_RECURSION_DEPTH) {
1519 track->resume_ncp = ncp;
28623bf9 1520 _cache_hold(ncp);
bf40a153
MD
1521 ++rcnt;
1522 }
28623bf9 1523 _cache_unlock(ncp);
b8997912 1524 while (kid) {
bf40a153 1525 if (track->resume_ncp) {
28623bf9 1526 _cache_drop(kid);
bf40a153
MD
1527 break;
1528 }
b8997912 1529 if ((nextkid = TAILQ_NEXT(kid, nc_entry)) != NULL)
28623bf9 1530 _cache_hold(nextkid);
e09206ba
MD
1531 if ((kid->nc_flag & NCF_UNRESOLVED) == 0 ||
1532 TAILQ_FIRST(&kid->nc_list)
b8997912 1533 ) {
28623bf9
MD
1534 _cache_lock(kid);
1535 rcnt += _cache_inval_internal(kid, flags & ~CINV_DESTROY, track);
1536 _cache_unlock(kid);
b8997912 1537 }
28623bf9 1538 _cache_drop(kid);
fad57d0e 1539 kid = nextkid;
8e005a45 1540 }
bf40a153 1541 --track->depth;
28623bf9 1542 _cache_lock(ncp);
8e005a45 1543 }
25cb3304
MD
1544
1545 /*
1546 * Someone could have gotten in there while ncp was unlocked,
1547 * retry if so.
1548 */
1549 if ((ncp->nc_flag & NCF_UNRESOLVED) == 0)
1550 ++rcnt;
1551 return (rcnt);
8e005a45
MD
1552}
1553
e09206ba 1554/*
25cb3304
MD
1555 * Invalidate a vnode's namecache associations. To avoid races against
1556 * the resolver we do not invalidate a node which we previously invalidated
1557 * but which was then re-resolved while we were in the invalidation loop.
1558 *
1559 * Returns non-zero if any namecache entries remain after the invalidation
1560 * loop completed.
2aefb2c5 1561 *
f63911bf
MD
1562 * NOTE: Unlike the namecache topology which guarentees that ncp's will not
1563 * be ripped out of the topology while held, the vnode's v_namecache
1564 * list has no such restriction. NCP's can be ripped out of the list
1565 * at virtually any time if not locked, even if held.
1566 *
1567 * In addition, the v_namecache list itself must be locked via
1568 * the vnode's spinlock.
e09206ba 1569 */
25cb3304 1570int
6b008938 1571cache_inval_vp(struct vnode *vp, int flags)
8e005a45
MD
1572{
1573 struct namecache *ncp;
25cb3304
MD
1574 struct namecache *next;
1575
2aefb2c5 1576restart:
b12defdc 1577 spin_lock(&vp->v_spin);
25cb3304
MD
1578 ncp = TAILQ_FIRST(&vp->v_namecache);
1579 if (ncp)
28623bf9 1580 _cache_hold(ncp);
25cb3304 1581 while (ncp) {
f63911bf 1582 /* loop entered with ncp held and vp spin-locked */
2aefb2c5 1583 if ((next = TAILQ_NEXT(ncp, nc_vnode)) != NULL)
28623bf9 1584 _cache_hold(next);
b12defdc 1585 spin_unlock(&vp->v_spin);
28623bf9 1586 _cache_lock(ncp);
2aefb2c5 1587 if (ncp->nc_vp != vp) {
6ea70f76 1588 kprintf("Warning: cache_inval_vp: race-A detected on "
2aefb2c5 1589 "%s\n", ncp->nc_name);
28623bf9 1590 _cache_put(ncp);
69313361 1591 if (next)
28623bf9 1592 _cache_drop(next);
2aefb2c5
MD
1593 goto restart;
1594 }
28623bf9
MD
1595 _cache_inval(ncp, flags);
1596 _cache_put(ncp); /* also releases reference */
25cb3304 1597 ncp = next;
b12defdc 1598 spin_lock(&vp->v_spin);
2aefb2c5 1599 if (ncp && ncp->nc_vp != vp) {
b12defdc 1600 spin_unlock(&vp->v_spin);
6ea70f76 1601 kprintf("Warning: cache_inval_vp: race-B detected on "
2aefb2c5 1602 "%s\n", ncp->nc_name);
28623bf9 1603 _cache_drop(ncp);
2aefb2c5
MD
1604 goto restart;
1605 }
690a3127 1606 }
b12defdc 1607 spin_unlock(&vp->v_spin);
25cb3304 1608 return(TAILQ_FIRST(&vp->v_namecache) != NULL);
5c6c3cac
MD
1609}
1610
1611/*
1612 * This routine is used instead of the normal cache_inval_vp() when we
1613 * are trying to recycle otherwise good vnodes.
1614 *
1615 * Return 0 on success, non-zero if not all namecache records could be
1616 * disassociated from the vnode (for various reasons).
1617 */
1618int
1619cache_inval_vp_nonblock(struct vnode *vp)
1620{
1621 struct namecache *ncp;
1622 struct namecache *next;
1623
b12defdc 1624 spin_lock(&vp->v_spin);
5c6c3cac
MD
1625 ncp = TAILQ_FIRST(&vp->v_namecache);
1626 if (ncp)
1627 _cache_hold(ncp);
1628 while (ncp) {
1629 /* loop entered with ncp held */
1630 if ((next = TAILQ_NEXT(ncp, nc_vnode)) != NULL)
1631 _cache_hold(next);
b12defdc 1632 spin_unlock(&vp->v_spin);
5c6c3cac
MD
1633 if (_cache_lock_nonblock(ncp)) {
1634 _cache_drop(ncp);
1635 if (next)
1636 _cache_drop(next);
2247fe02 1637 goto done;
5c6c3cac
MD
1638 }
1639 if (ncp->nc_vp != vp) {
1640 kprintf("Warning: cache_inval_vp: race-A detected on "
1641 "%s\n", ncp->nc_name);
1642 _cache_put(ncp);
1643 if (next)
1644 _cache_drop(next);
2247fe02 1645 goto done;
5c6c3cac
MD
1646 }
1647 _cache_inval(ncp, 0);
1648 _cache_put(ncp); /* also releases reference */
1649 ncp = next;
b12defdc 1650 spin_lock(&vp->v_spin);
5c6c3cac 1651 if (ncp && ncp->nc_vp != vp) {
b12defdc 1652 spin_unlock(&vp->v_spin);
5c6c3cac
MD
1653 kprintf("Warning: cache_inval_vp: race-B detected on "
1654 "%s\n", ncp->nc_name);
1655 _cache_drop(ncp);
2247fe02 1656 goto done;
5c6c3cac
MD
1657 }
1658 }
b12defdc 1659 spin_unlock(&vp->v_spin);
2247fe02 1660done:
5c6c3cac 1661 return(TAILQ_FIRST(&vp->v_namecache) != NULL);
14c92d03 1662}
14c92d03 1663
fad57d0e
MD
1664/*
1665 * The source ncp has been renamed to the target ncp. Both fncp and tncp
227cf16d
MD
1666 * must be locked. The target ncp is destroyed (as a normal rename-over
1667 * would destroy the target file or directory).
fad57d0e 1668 *
227cf16d
MD
1669 * Because there may be references to the source ncp we cannot copy its
1670 * contents to the target. Instead the source ncp is relinked as the target
1671 * and the target ncp is removed from the namecache topology.
fad57d0e
MD
1672 */
1673void
28623bf9 1674cache_rename(struct nchandle *fnch, struct nchandle *tnch)
fad57d0e 1675{
28623bf9
MD
1676 struct namecache *fncp = fnch->ncp;
1677 struct namecache *tncp = tnch->ncp;
2247fe02
MD
1678 struct namecache *tncp_par;
1679 struct nchash_head *nchpp;
1680 u_int32_t hash;
227cf16d 1681 char *oname;
8d09ad3d
MD
1682 char *nname;
1683
1684 if (tncp->nc_nlen) {
1685 nname = kmalloc(tncp->nc_nlen + 1, M_VFSCACHE, M_WAITOK);
1686 bcopy(tncp->nc_name, nname, tncp->nc_nlen);
1687 nname[tncp->nc_nlen] = 0;
1688 } else {
1689 nname = NULL;
1690 }
fad57d0e 1691
2247fe02
MD
1692 /*
1693 * Rename fncp (unlink)
1694 */
f63911bf 1695 _cache_unlink_parent(fncp);
227cf16d 1696 oname = fncp->nc_name;
8d09ad3d 1697 fncp->nc_name = nname;
227cf16d 1698 fncp->nc_nlen = tncp->nc_nlen;
8d09ad3d
MD
1699 if (oname)
1700 kfree(oname, M_VFSCACHE);
1701
2247fe02
MD
1702 tncp_par = tncp->nc_parent;
1703 _cache_hold(tncp_par);
1704 _cache_lock(tncp_par);
1705
1706 /*
1707 * Rename fncp (relink)
1708 */
1709 hash = fnv_32_buf(fncp->nc_name, fncp->nc_nlen, FNV1_32_INIT);
1710 hash = fnv_32_buf(&tncp_par, sizeof(tncp_par), hash);
1711 nchpp = NCHHASH(hash);
1712
287a8577 1713 spin_lock(&nchpp->spin);
2247fe02 1714 _cache_link_parent(fncp, tncp_par, nchpp);
287a8577 1715 spin_unlock(&nchpp->spin);
2247fe02
MD
1716
1717 _cache_put(tncp_par);
1718
1719 /*
1720 * Get rid of the overwritten tncp (unlink)
1721 */
8d09ad3d
MD
1722 _cache_unlink(tncp);
1723}
f63911bf 1724
8d09ad3d 1725/*
245d8a0d
MD
1726 * Perform actions consistent with unlinking a file. The passed-in ncp
1727 * must be locked.
1728 *
1729 * The ncp is marked DESTROYED so it no longer shows up in searches,
8d09ad3d 1730 * and will be physically deleted when the vnode goes away.
245d8a0d
MD
1731 *
1732 * If the related vnode has no refs then we cycle it through vget()/vput()
1733 * to (possibly if we don't have a ref race) trigger a deactivation,
1734 * allowing the VFS to trivially detect and recycle the deleted vnode
1735 * via VOP_INACTIVE().
1736 *
1737 * NOTE: _cache_rename() will automatically call _cache_unlink() on the
1738 * target ncp.
8d09ad3d
MD
1739 */
1740void
1741cache_unlink(struct nchandle *nch)
1742{
1743 _cache_unlink(nch->ncp);
1744}
1745
1746static void
1747_cache_unlink(struct namecache *ncp)
1748{
245d8a0d
MD
1749 struct vnode *vp;
1750
1751 /*
1752 * Causes lookups to fail and allows another ncp with the same
1753 * name to be created under ncp->nc_parent.
1754 */
8d09ad3d 1755 ncp->nc_flag |= NCF_DESTROYED;
245d8a0d
MD
1756
1757 /*
1758 * Attempt to trigger a deactivation.
1759 */
1760 if ((ncp->nc_flag & NCF_UNRESOLVED) == 0 &&
1761 (vp = ncp->nc_vp) != NULL &&
1762 !sysref_isactive(&vp->v_sysref)) {
1763 if (vget(vp, LK_SHARED) == 0)
1764 vput(vp);
1765 }
fad57d0e
MD
1766}
1767
21739618
MD
1768/*
1769 * vget the vnode associated with the namecache entry. Resolve the namecache
2247fe02 1770 * entry if necessary. The passed ncp must be referenced and locked.
21739618
MD
1771 *
1772 * lk_type may be LK_SHARED, LK_EXCLUSIVE. A ref'd, possibly locked
1773 * (depending on the passed lk_type) will be returned in *vpp with an error
1774 * of 0, or NULL will be returned in *vpp with a non-0 error code. The
1775 * most typical error is ENOENT, meaning that the ncp represents a negative
1776 * cache hit and there is no vnode to retrieve, but other errors can occur
1777 * too.
1778 *
2247fe02
MD
1779 * The vget() can race a reclaim. If this occurs we re-resolve the
1780 * namecache entry.
1781 *
1782 * There are numerous places in the kernel where vget() is called on a
1783 * vnode while one or more of its namecache entries is locked. Releasing
1784 * a vnode never deadlocks against locked namecache entries (the vnode
1785 * will not get recycled while referenced ncp's exist). This means we
1786 * can safely acquire the vnode. In fact, we MUST NOT release the ncp
1787 * lock when acquiring the vp lock or we might cause a deadlock.
1788 *
79fd1696
MD
1789 * NOTE: The passed-in ncp must be locked exclusively if it is initially
1790 * unresolved. If a reclaim race occurs the passed-in ncp will be
1791 * relocked exclusively before being re-resolved.
21739618
MD
1792 */
1793int
28623bf9 1794cache_vget(struct nchandle *nch, struct ucred *cred,
21739618
MD
1795 int lk_type, struct vnode **vpp)
1796{
28623bf9 1797 struct namecache *ncp;
21739618
MD
1798 struct vnode *vp;
1799 int error;
1800
28623bf9 1801 ncp = nch->ncp;
21739618
MD
1802again:
1803 vp = NULL;
2247fe02 1804 if (ncp->nc_flag & NCF_UNRESOLVED)
28623bf9 1805 error = cache_resolve(nch, cred);
2247fe02 1806 else
21739618 1807 error = 0;
2247fe02 1808
21739618 1809 if (error == 0 && (vp = ncp->nc_vp) != NULL) {
87de5057 1810 error = vget(vp, lk_type);
21739618 1811 if (error) {
2247fe02
MD
1812 /*
1813 * VRECLAIM race
1814 */
1815 if (error == ENOENT) {
1816 kprintf("Warning: vnode reclaim race detected "
1817 "in cache_vget on %p (%s)\n",
1818 vp, ncp->nc_name);
79fd1696
MD
1819 _cache_unlock(ncp);
1820 _cache_lock(ncp);
2247fe02 1821 _cache_setunresolved(ncp);
21739618 1822 goto again;
2247fe02
MD
1823 }
1824
1825 /*
1826 * Not a reclaim race, some other error.
1827 */
1828 KKASSERT(ncp->nc_vp == vp);
21739618 1829 vp = NULL;
2247fe02
MD
1830 } else {
1831 KKASSERT(ncp->nc_vp == vp);
1832 KKASSERT((vp->v_flag & VRECLAIMED) == 0);
21739618
MD
1833 }
1834 }
1835 if (error == 0 && vp == NULL)
1836 error = ENOENT;
1837 *vpp = vp;
1838 return(error);
1839}
1840
79fd1696
MD
1841/*
1842 * Similar to cache_vget() but only acquires a ref on the vnode.
1843 *
1844 * NOTE: The passed-in ncp must be locked exclusively if it is initially
1845 * unresolved. If a reclaim race occurs the passed-in ncp will be
1846 * relocked exclusively before being re-resolved.
1847 */
21739618 1848int
28623bf9 1849cache_vref(struct nchandle *nch, struct ucred *cred, struct vnode **vpp)
21739618 1850{
28623bf9 1851 struct namecache *ncp;
21739618
MD
1852 struct vnode *vp;
1853 int error;
1854
28623bf9 1855 ncp = nch->ncp;
21739618
MD
1856again:
1857 vp = NULL;
2247fe02 1858 if (ncp->nc_flag & NCF_UNRESOLVED)
28623bf9 1859 error = cache_resolve(nch, cred);
2247fe02 1860 else
21739618 1861 error = 0;
2247fe02 1862
21739618 1863 if (error == 0 && (vp = ncp->nc_vp) != NULL) {
2247fe02
MD
1864 error = vget(vp, LK_SHARED);
1865 if (error) {
1866 /*
1867 * VRECLAIM race
1868 */
3c37c940 1869 if (error == ENOENT) {
2247fe02
MD
1870 kprintf("Warning: vnode reclaim race detected "
1871 "in cache_vget on %p (%s)\n",
1872 vp, ncp->nc_name);
79fd1696
MD
1873 _cache_unlock(ncp);
1874 _cache_lock(ncp);
3c37c940 1875 _cache_setunresolved(ncp);
3c37c940
MD
1876 goto again;
1877 }
2247fe02
MD
1878
1879 /*
1880 * Not a reclaim race, some other error.
1881 */
1882 KKASSERT(ncp->nc_vp == vp);
1883 vp = NULL;
3c37c940 1884 } else {
2247fe02
MD
1885 KKASSERT(ncp->nc_vp == vp);
1886 KKASSERT((vp->v_flag & VRECLAIMED) == 0);
3c37c940
MD
1887 /* caller does not want a lock */
1888 vn_unlock(vp);
21739618
MD
1889 }
1890 }
1891 if (error == 0 && vp == NULL)
1892 error = ENOENT;
1893 *vpp = vp;
1894 return(error);
1895}
1896
c0c70b27
MD
1897/*
1898 * Return a referenced vnode representing the parent directory of
f63911bf
MD
1899 * ncp.
1900 *
1901 * Because the caller has locked the ncp it should not be possible for
1902 * the parent ncp to go away. However, the parent can unresolve its
1903 * dvp at any time so we must be able to acquire a lock on the parent
1904 * to safely access nc_vp.
5312fa43 1905 *
f63911bf
MD
1906 * We have to leave par unlocked when vget()ing dvp to avoid a deadlock,
1907 * so use vhold()/vdrop() while holding the lock to prevent dvp from
1908 * getting destroyed.
2247fe02 1909 *
79fd1696
MD
1910 * NOTE: vhold() is allowed when dvp has 0 refs if we hold a
1911 * lock on the ncp in question..
c0c70b27 1912 */
5312fa43 1913static struct vnode *
c0c70b27
MD
1914cache_dvpref(struct namecache *ncp)
1915{
5312fa43 1916 struct namecache *par;
c0c70b27 1917 struct vnode *dvp;
c0c70b27 1918
5312fa43
MD
1919 dvp = NULL;
1920 if ((par = ncp->nc_parent) != NULL) {
f63911bf 1921 _cache_hold(par);
2247fe02
MD
1922 _cache_lock(par);
1923 if ((par->nc_flag & NCF_UNRESOLVED) == 0) {
1924 if ((dvp = par->nc_vp) != NULL)
1925 vhold(dvp);
1926 }
1927 _cache_unlock(par);
1928 if (dvp) {
1929 if (vget(dvp, LK_SHARED) == 0) {
1930 vn_unlock(dvp);
1931 vdrop(dvp);
1932 /* return refd, unlocked dvp */
1933 } else {
1934 vdrop(dvp);
1935 dvp = NULL;
5312fa43
MD
1936 }
1937 }
f63911bf 1938 _cache_drop(par);
5312fa43
MD
1939 }
1940 return(dvp);
c0c70b27
MD
1941}
1942
fad57d0e
MD
1943/*
1944 * Convert a directory vnode to a namecache record without any other
1945 * knowledge of the topology. This ONLY works with directory vnodes and
1946 * is ONLY used by the NFS server. dvp must be refd but unlocked, and the
1947 * returned ncp (if not NULL) will be held and unlocked.
1948 *
1949 * If 'makeit' is 0 and dvp has no existing namecache record, NULL is returned.
1950 * If 'makeit' is 1 we attempt to track-down and create the namecache topology
1951 * for dvp. This will fail only if the directory has been deleted out from
1952 * under the caller.
1953 *
1954 * Callers must always check for a NULL return no matter the value of 'makeit'.
a0d57516
MD
1955 *
1956 * To avoid underflowing the kernel stack each recursive call increments
1957 * the makeit variable.
fad57d0e
MD
1958 */
1959
28623bf9 1960static int cache_inefficient_scan(struct nchandle *nch, struct ucred *cred,
33387738 1961 struct vnode *dvp, char *fakename);
a0d57516 1962static int cache_fromdvp_try(struct vnode *dvp, struct ucred *cred,
cc4c3b52 1963 struct vnode **saved_dvp);
fad57d0e 1964
28623bf9
MD
1965int
1966cache_fromdvp(struct vnode *dvp, struct ucred *cred, int makeit,
1967 struct nchandle *nch)
fad57d0e 1968{
cc4c3b52 1969 struct vnode *saved_dvp;
fad57d0e 1970 struct vnode *pvp;
33387738 1971 char *fakename;
fad57d0e
MD
1972 int error;
1973
28623bf9
MD
1974 nch->ncp = NULL;
1975 nch->mount = dvp->v_mount;
cc4c3b52 1976 saved_dvp = NULL;
33387738 1977 fakename = NULL;
a0d57516 1978
269a08e4
MD
1979 /*
1980 * Handle the makeit == 0 degenerate case
1981 */
1982 if (makeit == 0) {
b12defdc 1983 spin_lock(&dvp->v_spin);
269a08e4
MD
1984 nch->ncp = TAILQ_FIRST(&dvp->v_namecache);
1985 if (nch->ncp)
1986 cache_hold(nch);
b12defdc 1987 spin_unlock(&dvp->v_spin);
269a08e4
MD
1988 }
1989
fad57d0e 1990 /*
f63911bf 1991 * Loop until resolution, inside code will break out on error.
fad57d0e 1992 */
f63911bf
MD
1993 while (makeit) {
1994 /*
1995 * Break out if we successfully acquire a working ncp.
1996 */
b12defdc 1997 spin_lock(&dvp->v_spin);
28623bf9 1998 nch->ncp = TAILQ_FIRST(&dvp->v_namecache);
f63911bf
MD
1999 if (nch->ncp) {
2000 cache_hold(nch);
b12defdc 2001 spin_unlock(&dvp->v_spin);
f63911bf
MD
2002 break;
2003 }
b12defdc 2004 spin_unlock(&dvp->v_spin);
fad57d0e 2005
fad57d0e
MD
2006 /*
2007 * If dvp is the root of its filesystem it should already
2008 * have a namecache pointer associated with it as a side
2009 * effect of the mount, but it may have been disassociated.
2010 */
2011 if (dvp->v_flag & VROOT) {
28623bf9
MD
2012 nch->ncp = _cache_get(nch->mount->mnt_ncmountpt.ncp);
2013 error = cache_resolve_mp(nch->mount);
2014 _cache_put(nch->ncp);
fad57d0e 2015 if (ncvp_debug) {
6ea70f76 2016 kprintf("cache_fromdvp: resolve root of mount %p error %d",
fad57d0e
MD
2017 dvp->v_mount, error);
2018 }
2019 if (error) {
2020 if (ncvp_debug)
6ea70f76 2021 kprintf(" failed\n");
28623bf9 2022 nch->ncp = NULL;
fad57d0e
MD
2023 break;
2024 }
2025 if (ncvp_debug)
6ea70f76 2026 kprintf(" succeeded\n");
fad57d0e
MD
2027 continue;
2028 }
2029
a0d57516
MD
2030 /*
2031 * If we are recursed too deeply resort to an O(n^2)
2032 * algorithm to resolve the namecache topology. The
cc4c3b52 2033 * resolved pvp is left referenced in saved_dvp to
a0d57516
MD
2034 * prevent the tree from being destroyed while we loop.
2035 */
2036 if (makeit > 20) {
cc4c3b52 2037 error = cache_fromdvp_try(dvp, cred, &saved_dvp);
a0d57516 2038 if (error) {
6ea70f76 2039 kprintf("lookupdotdot(longpath) failed %d "
a0d57516 2040 "dvp %p\n", error, dvp);
1142bff7 2041 nch->ncp = NULL;
a0d57516
MD
2042 break;
2043 }
2044 continue;
2045 }
2046
fad57d0e
MD
2047 /*
2048 * Get the parent directory and resolve its ncp.
2049 */
33387738
MD
2050 if (fakename) {
2051 kfree(fakename, M_TEMP);
2052 fakename = NULL;
2053 }
2054 error = vop_nlookupdotdot(*dvp->v_ops, dvp, &pvp, cred,
2055 &fakename);
fad57d0e 2056 if (error) {
6ea70f76 2057 kprintf("lookupdotdot failed %d dvp %p\n", error, dvp);
fad57d0e
MD
2058 break;
2059 }
a11aaa81 2060 vn_unlock(pvp);
fad57d0e
MD
2061
2062 /*
1142bff7
MD
2063 * Reuse makeit as a recursion depth counter. On success
2064 * nch will be fully referenced.
fad57d0e 2065 */
28623bf9 2066 cache_fromdvp(pvp, cred, makeit + 1, nch);
fad57d0e 2067 vrele(pvp);
28623bf9 2068 if (nch->ncp == NULL)
fad57d0e
MD
2069 break;
2070
2071 /*
2072 * Do an inefficient scan of pvp (embodied by ncp) to look
2073 * for dvp. This will create a namecache record for dvp on
2074 * success. We loop up to recheck on success.
2075 *
2076 * ncp and dvp are both held but not locked.
2077 */
33387738 2078 error = cache_inefficient_scan(nch, cred, dvp, fakename);
fad57d0e 2079 if (error) {
6ea70f76 2080 kprintf("cache_fromdvp: scan %p (%s) failed on dvp=%p\n",
28623bf9 2081 pvp, nch->ncp->nc_name, dvp);
1142bff7
MD
2082 cache_drop(nch);
2083 /* nch was NULLed out, reload mount */
2084 nch->mount = dvp->v_mount;
fad57d0e
MD
2085 break;
2086 }
2087 if (ncvp_debug) {
6ea70f76 2088 kprintf("cache_fromdvp: scan %p (%s) succeeded\n",
28623bf9 2089 pvp, nch->ncp->nc_name);
fad57d0e 2090 }
1142bff7
MD
2091 cache_drop(nch);
2092 /* nch was NULLed out, reload mount */
2093 nch->mount = dvp->v_mount;
fad57d0e 2094 }
28623bf9
MD
2095
2096 /*
f63911bf 2097 * If nch->ncp is non-NULL it will have been held already.
28623bf9 2098 */
f63911bf
MD
2099 if (fakename)
2100 kfree(fakename, M_TEMP);
cc4c3b52
MD
2101 if (saved_dvp)
2102 vrele(saved_dvp);
28623bf9
MD
2103 if (nch->ncp)
2104 return (0);
2105 return (EINVAL);
fad57d0e
MD
2106}
2107
a0d57516
MD
2108/*
2109 * Go up the chain of parent directories until we find something
2110 * we can resolve into the namecache. This is very inefficient.
2111 */
2112static
2113int
2114cache_fromdvp_try(struct vnode *dvp, struct ucred *cred,
cc4c3b52 2115 struct vnode **saved_dvp)
a0d57516 2116{
28623bf9 2117 struct nchandle nch;
a0d57516
MD
2118 struct vnode *pvp;
2119 int error;
2120 static time_t last_fromdvp_report;
33387738 2121 char *fakename;
a0d57516
MD
2122
2123 /*
2124 * Loop getting the parent directory vnode until we get something we
2125 * can resolve in the namecache.
2126 */
2127 vref(dvp);
28623bf9 2128 nch.mount = dvp->v_mount;
1142bff7 2129 nch.ncp = NULL;
33387738 2130 fakename = NULL;
28623bf9 2131
a0d57516 2132 for (;;) {
33387738
MD
2133 if (fakename) {
2134 kfree(fakename, M_TEMP);
2135 fakename = NULL;
2136 }
2137 error = vop_nlookupdotdot(*dvp->v_ops, dvp, &pvp, cred,
2138 &fakename);
a0d57516
MD
2139 if (error) {
2140 vrele(dvp);
33387738 2141 break;
a0d57516 2142 }
a11aaa81 2143 vn_unlock(pvp);
b12defdc 2144 spin_lock(&pvp->v_spin);
28623bf9
MD
2145 if ((nch.ncp = TAILQ_FIRST(&pvp->v_namecache)) != NULL) {
2146 _cache_hold(nch.ncp);
b12defdc 2147 spin_unlock(&pvp->v_spin);
a0d57516
MD
2148 vrele(pvp);
2149 break;
2150 }
b12defdc 2151 spin_unlock(&pvp->v_spin);
a0d57516 2152 if (pvp->v_flag & VROOT) {
28623bf9
MD
2153 nch.ncp = _cache_get(pvp->v_mount->mnt_ncmountpt.ncp);
2154 error = cache_resolve_mp(nch.mount);
2155 _cache_unlock(nch.ncp);
a0d57516
MD
2156 vrele(pvp);
2157 if (error) {
28623bf9 2158 _cache_drop(nch.ncp);
1142bff7 2159 nch.ncp = NULL;
a0d57516 2160 vrele(dvp);
a0d57516
MD
2161 }
2162 break;
2163 }
2164 vrele(dvp);
2165 dvp = pvp;
2166 }
33387738
MD
2167 if (error == 0) {
2168 if (last_fromdvp_report != time_second) {
2169 last_fromdvp_report = time_second;
2170 kprintf("Warning: extremely inefficient path "
2171 "resolution on %s\n",
2172 nch.ncp->nc_name);
2173 }
2174 error = cache_inefficient_scan(&nch, cred, dvp, fakename);
cc4c3b52 2175
33387738
MD
2176 /*
2177 * Hopefully dvp now has a namecache record associated with
2178 * it. Leave it referenced to prevent the kernel from
2179 * recycling the vnode. Otherwise extremely long directory
2180 * paths could result in endless recycling.
2181 */
2182 if (*saved_dvp)
2183 vrele(*saved_dvp);
2184 *saved_dvp = dvp;
1142bff7 2185 _cache_drop(nch.ncp);
33387738
MD
2186 }
2187 if (fakename)
2188 kfree(fakename, M_TEMP);
a0d57516
MD
2189 return (error);
2190}
2191
fad57d0e
MD
2192/*
2193 * Do an inefficient scan of the directory represented by ncp looking for
2194 * the directory vnode dvp. ncp must be held but not locked on entry and
2195 * will be held on return. dvp must be refd but not locked on entry and
2196 * will remain refd on return.
2197 *
2198 * Why do this at all? Well, due to its stateless nature the NFS server
2199 * converts file handles directly to vnodes without necessarily going through
2200 * the namecache ops that would otherwise create the namecache topology
2201 * leading to the vnode. We could either (1) Change the namecache algorithms
2202 * to allow disconnect namecache records that are re-merged opportunistically,
2203 * or (2) Make the NFS server backtrack and scan to recover a connected
2204 * namecache topology in order to then be able to issue new API lookups.
2205 *
2206 * It turns out that (1) is a huge mess. It takes a nice clean set of
2207 * namecache algorithms and introduces a lot of complication in every subsystem
2208 * that calls into the namecache to deal with the re-merge case, especially
2209 * since we are using the namecache to placehold negative lookups and the
2210 * vnode might not be immediately assigned. (2) is certainly far less
2211 * efficient then (1), but since we are only talking about directories here
2212 * (which are likely to remain cached), the case does not actually run all
2213 * that often and has the supreme advantage of not polluting the namecache
2214 * algorithms.
33387738
MD
2215 *
2216 * If a fakename is supplied just construct a namecache entry using the
2217 * fake name.
fad57d0e
MD
2218 */
2219static int
28623bf9 2220cache_inefficient_scan(struct nchandle *nch, struct ucred *cred,
33387738 2221 struct vnode *dvp, char *fakename)
fad57d0e
MD
2222{
2223 struct nlcomponent nlc;
28623bf9 2224 struct nchandle rncp;
fad57d0e
MD
2225 struct dirent *den;
2226 struct vnode *pvp;
2227 struct vattr vat;
2228 struct iovec iov;
2229 struct uio uio;
fad57d0e
MD
2230 int blksize;
2231 int eofflag;
4d22f42a 2232 int bytes;
fad57d0e
MD
2233 char *rbuf;
2234 int error;
fad57d0e
MD
2235
2236 vat.va_blocksize = 0;
87de5057 2237 if ((error = VOP_GETATTR(dvp, &vat)) != 0)
fad57d0e 2238 return (error);
2247fe02
MD
2239 cache_lock(nch);
2240 error = cache_vref(nch, cred, &pvp);
2241 cache_unlock(nch);
2242 if (error)
fad57d0e 2243 return (error);
973c11b9
MD
2244 if (ncvp_debug) {
2245 kprintf("inefficient_scan: directory iosize %ld "
2246 "vattr fileid = %lld\n",
2247 vat.va_blocksize,
2248 (long long)vat.va_fileid);
2249 }
33387738
MD
2250
2251 /*
2252 * Use the supplied fakename if not NULL. Fake names are typically
2253 * not in the actual filesystem hierarchy. This is used by HAMMER
2254 * to glue @@timestamp recursions together.
2255 */
2256 if (fakename) {
2257 nlc.nlc_nameptr = fakename;
2258 nlc.nlc_namelen = strlen(fakename);
2259 rncp = cache_nlookup(nch, &nlc);
2260 goto done;
2261 }
2262
fad57d0e
MD
2263 if ((blksize = vat.va_blocksize) == 0)
2264 blksize = DEV_BSIZE;
efda3bd0 2265 rbuf = kmalloc(blksize, M_TEMP, M_WAITOK);
28623bf9 2266 rncp.ncp = NULL;
fad57d0e
MD
2267
2268 eofflag = 0;
2269 uio.uio_offset = 0;
fad57d0e 2270again:
fad57d0e
MD
2271 iov.iov_base = rbuf;
2272 iov.iov_len = blksize;
2273 uio.uio_iov = &iov;
2274 uio.uio_iovcnt = 1;
2275 uio.uio_resid = blksize;
2276 uio.uio_segflg = UIO_SYSSPACE;
2277 uio.uio_rw = UIO_READ;
2278 uio.uio_td = curthread;
2279
fad57d0e 2280 if (ncvp_debug >= 2)
6ea70f76 2281 kprintf("cache_inefficient_scan: readdir @ %08x\n", (int)uio.uio_offset);
4d22f42a 2282 error = VOP_READDIR(pvp, &uio, cred, &eofflag, NULL, NULL);
fad57d0e 2283 if (error == 0) {
4d22f42a
MD
2284 den = (struct dirent *)rbuf;
2285 bytes = blksize - uio.uio_resid;
2286
2287 while (bytes > 0) {
2288 if (ncvp_debug >= 2) {
6ea70f76 2289 kprintf("cache_inefficient_scan: %*.*s\n",
4d22f42a
MD
2290 den->d_namlen, den->d_namlen,
2291 den->d_name);
2292 }
fad57d0e 2293 if (den->d_type != DT_WHT &&
01f31ab3 2294 den->d_ino == vat.va_fileid) {
4d22f42a 2295 if (ncvp_debug) {
6ea70f76 2296 kprintf("cache_inefficient_scan: "
50626622 2297 "MATCHED inode %lld path %s/%*.*s\n",
973c11b9
MD
2298 (long long)vat.va_fileid,
2299 nch->ncp->nc_name,
4d22f42a
MD
2300 den->d_namlen, den->d_namlen,
2301 den->d_name);
2302 }
fad57d0e
MD
2303 nlc.nlc_nameptr = den->d_name;
2304 nlc.nlc_namelen = den->d_namlen;
28623bf9
MD
2305 rncp = cache_nlookup(nch, &nlc);
2306 KKASSERT(rncp.ncp != NULL);
fad57d0e
MD
2307 break;
2308 }
01f31ab3
JS
2309 bytes -= _DIRENT_DIRSIZ(den);
2310 den = _DIRENT_NEXT(den);
fad57d0e 2311 }
28623bf9 2312 if (rncp.ncp == NULL && eofflag == 0 && uio.uio_resid != blksize)
fad57d0e
MD
2313 goto again;
2314 }
33387738
MD
2315 kfree(rbuf, M_TEMP);
2316done:
885ecb13 2317 vrele(pvp);
28623bf9
MD
2318 if (rncp.ncp) {
2319 if (rncp.ncp->nc_flag & NCF_UNRESOLVED) {
4b5bbb78 2320 _cache_setvp(rncp.mount, rncp.ncp, dvp);
fad57d0e 2321 if (ncvp_debug >= 2) {
6ea70f76 2322 kprintf("cache_inefficient_scan: setvp %s/%s = %p\n",
28623bf9 2323 nch->ncp->nc_name, rncp.ncp->nc_name, dvp);
fad57d0e
MD
2324 }
2325 } else {
2326 if (ncvp_debug >= 2) {
6ea70f76 2327 kprintf("cache_inefficient_scan: setvp %s/%s already set %p/%p\n",
28623bf9
MD
2328 nch->ncp->nc_name, rncp.ncp->nc_name, dvp,
2329 rncp.ncp->nc_vp);
fad57d0e
MD
2330 }
2331 }
28623bf9
MD
2332 if (rncp.ncp->nc_vp == NULL)
2333 error = rncp.ncp->nc_error;
1142bff7
MD
2334 /*
2335 * Release rncp after a successful nlookup. rncp was fully
2336 * referenced.
2337 */
2338 cache_put(&rncp);
fad57d0e 2339 } else {
6ea70f76 2340 kprintf("cache_inefficient_scan: dvp %p NOT FOUND in %s\n",
28623bf9 2341 dvp, nch->ncp->nc_name);
fad57d0e
MD
2342 error = ENOENT;
2343 }
fad57d0e
MD
2344 return (error);
2345}
2346
984263bc 2347/*
67773eb3
MD
2348 * Zap a namecache entry. The ncp is unconditionally set to an unresolved
2349 * state, which disassociates it from its vnode or ncneglist.
7ea21ed1 2350 *
67773eb3 2351 * Then, if there are no additional references to the ncp and no children,
f63911bf 2352 * the ncp is removed from the topology and destroyed.
7ea21ed1 2353 *
67773eb3
MD
2354 * References and/or children may exist if the ncp is in the middle of the
2355 * topology, preventing the ncp from being destroyed.
7ea21ed1 2356 *
67773eb3
MD
2357 * This function must be called with the ncp held and locked and will unlock
2358 * and drop it during zapping.
f63911bf 2359 *
65870584
MD
2360 * If nonblock is non-zero and the parent ncp cannot be locked we give up.
2361 * This case can occur in the cache_drop() path.
2362 *
f63911bf
MD
2363 * This function may returned a held (but NOT locked) parent node which the
2364 * caller must drop. We do this so _cache_drop() can loop, to avoid
2365 * blowing out the kernel stack.
2366 *
2367 * WARNING! For MPSAFE operation this routine must acquire up to three
2368 * spin locks to be able to safely test nc_refs. Lock order is
2369 * very important.
2370 *
2371 * hash spinlock if on hash list
2372 * parent spinlock if child of parent
2373 * (the ncp is unresolved so there is no vnode association)
984263bc 2374 */
f63911bf 2375static struct namecache *
65870584 2376cache_zap(struct namecache *ncp, int nonblock)
984263bc 2377{
7ea21ed1 2378 struct namecache *par;
f63911bf 2379 struct vnode *dropvp;
f63911bf 2380 int refs;
7ea21ed1
MD
2381
2382 /*
2383 * Disassociate the vnode or negative cache ref and set NCF_UNRESOLVED.
2384 */
28623bf9 2385 _cache_setunresolved(ncp);
7ea21ed1
MD
2386
2387 /*
2388 * Try to scrap the entry and possibly tail-recurse on its parent.
2389 * We only scrap unref'd (other then our ref) unresolved entries,
2390 * we do not scrap 'live' entries.
f63911bf
MD
2391 *
2392 * Note that once the spinlocks are acquired if nc_refs == 1 no
2393 * other references are possible. If it isn't, however, we have
2394 * to decrement but also be sure to avoid a 1->0 transition.
7ea21ed1 2395 */
f63911bf
MD
2396 KKASSERT(ncp->nc_flag & NCF_UNRESOLVED);
2397 KKASSERT(ncp->nc_refs > 0);
7ea21ed1 2398
f63911bf 2399 /*
65870584
MD
2400 * Acquire locks. Note that the parent can't go away while we hold
2401 * a child locked.
f63911bf 2402 */
2247fe02 2403 if ((par = ncp->nc_parent) != NULL) {
65870584
MD
2404 if (nonblock) {
2405 for (;;) {
2406 if (_cache_lock_nonblock(par) == 0)
2407 break;
65870584
MD
2408 refs = ncp->nc_refs;
2409 ncp->nc_flag |= NCF_DEFEREDZAP;
2410 ++numdefered; /* MP race ok */
2411 if (atomic_cmpset_int(&ncp->nc_refs,
2412 refs, refs - 1)) {
2413 _cache_unlock(ncp);
2414 return(NULL);
2415 }
2416 cpu_pause();
2417 }
2418 _cache_hold(par);
2419 } else {
2420 _cache_hold(par);
2421 _cache_lock(par);
2422 }
287a8577 2423 spin_lock(&ncp->nc_head->spin);
f63911bf 2424 }
7ea21ed1 2425
f63911bf
MD
2426 /*
2427 * If someone other then us has a ref or we have children
2428 * we cannot zap the entry. The 1->0 transition and any
2429 * further list operation is protected by the spinlocks
2430 * we have acquired but other transitions are not.
2431 */
2432 for (;;) {
2433 refs = ncp->nc_refs;
2434 if (refs == 1 && TAILQ_EMPTY(&ncp->nc_list))
2435 break;
2436 if (atomic_cmpset_int(&ncp->nc_refs, refs, refs - 1)) {
2247fe02 2437 if (par) {
287a8577 2438 spin_unlock(&ncp->nc_head->spin);
2247fe02
MD
2439 _cache_put(par);
2440 }
f63911bf
MD
2441 _cache_unlock(ncp);
2442 return(NULL);
7ea21ed1 2443 }
2247fe02 2444 cpu_pause();
f63911bf 2445 }
67773eb3 2446
f63911bf
MD
2447 /*
2448 * We are the only ref and with the spinlocks held no further
2449 * refs can be acquired by others.
2450 *
2451 * Remove us from the hash list and parent list. We have to
2452 * drop a ref on the parent's vp if the parent's list becomes
2453 * empty.
2454 */
f63911bf 2455 dropvp = NULL;
2247fe02
MD
2456 if (par) {
2457 struct nchash_head *nchpp = ncp->nc_head;
67773eb3 2458
2247fe02
MD
2459 KKASSERT(nchpp != NULL);
2460 LIST_REMOVE(ncp, nc_hash);
2461 TAILQ_REMOVE(&par->nc_list, ncp, nc_entry);
f63911bf
MD
2462 if (par->nc_vp && TAILQ_EMPTY(&par->nc_list))
2463 dropvp = par->nc_vp;
2247fe02
MD
2464 ncp->nc_head = NULL;
2465 ncp->nc_parent = NULL;
287a8577 2466 spin_unlock(&nchpp->spin);
2247fe02
MD
2467 _cache_unlock(par);
2468 } else {
2469 KKASSERT(ncp->nc_head == NULL);
7ea21ed1 2470 }
f63911bf
MD
2471
2472 /*
2473 * ncp should not have picked up any refs. Physically
2474 * destroy the ncp.
2475 */
f63911bf 2476 KKASSERT(ncp->nc_refs == 1);
f63911bf
MD
2477 /* _cache_unlock(ncp) not required */
2478 ncp->nc_refs = -1; /* safety */
2479 if (ncp->nc_name)
2480 kfree(ncp->nc_name, M_VFSCACHE);
2481 kfree(ncp, M_VFSCACHE);
2482
2483 /*
2484 * Delayed drop (we had to release our spinlocks)
2485 *
2486 * The refed parent (if not NULL) must be dropped. The
2487 * caller is responsible for looping.
2488 */
2489 if (dropvp)
2490 vdrop(dropvp);
2491 return(par);
984263bc
MD
2492}
2493
65870584
MD
2494/*
2495 * Clean up dangling negative cache and defered-drop entries in the
2496 * namecache.
6363f268
MD
2497 *
2498 * This routine is called in the critical path and also called from
2499 * vnlru(). When called from vnlru we use a lower limit to try to
2500 * deal with the negative cache before the critical path has to start
2501 * dealing with it.
65870584 2502 */
9e10d70b
MD
2503typedef enum { CHI_LOW, CHI_HIGH } cache_hs_t;
2504
6363f268
MD
2505static cache_hs_t neg_cache_hysteresis_state[2] = { CHI_LOW, CHI_LOW };
2506static cache_hs_t pos_cache_hysteresis_state[2] = { CHI_LOW, CHI_LOW };
62d0f1f0 2507
62d0f1f0 2508void
6363f268 2509cache_hysteresis(int critpath)
62d0f1f0 2510{
9e10d70b 2511 int poslimit;
6363f268
MD
2512 int neglimit = desiredvnodes / ncnegfactor;
2513 int xnumcache = numcache;
2514
2515 if (critpath == 0)
2516 neglimit = neglimit * 8 / 10;
9e10d70b 2517
62d0f1f0
MD
2518 /*
2519 * Don't cache too many negative hits. We use hysteresis to reduce
2520 * the impact on the critical path.
2521 */
6363f268 2522 switch(neg_cache_hysteresis_state[critpath]) {
62d0f1f0 2523 case CHI_LOW:
6363f268
MD
2524 if (numneg > MINNEG && numneg > neglimit) {
2525 if (critpath)
2526 _cache_cleanneg(ncnegflush);
2527 else
2528 _cache_cleanneg(ncnegflush +
2529 numneg - neglimit);
2530 neg_cache_hysteresis_state[critpath] = CHI_HIGH;
62d0f1f0
MD
2531 }
2532 break;
2533 case CHI_HIGH:
2534 if (numneg > MINNEG * 9 / 10 &&
6363f268 2535 numneg * 9 / 10 > neglimit
62d0f1f0 2536 ) {
6363f268
MD
2537 if (critpath)
2538 _cache_cleanneg(ncnegflush);
2539 else
2540 _cache_cleanneg(ncnegflush +
2541 numneg * 9 / 10 - neglimit);
62d0f1f0 2542 } else {
6363f268 2543 neg_cache_hysteresis_state[critpath] = CHI_LOW;
9e10d70b
MD
2544 }
2545 break;
2546 }
2547
2548 /*
2549 * Don't cache too many positive hits. We use hysteresis to reduce
2550 * the impact on the critical path.
2551 *
2552 * Excessive positive hits can accumulate due to large numbers of
2553 * hardlinks (the vnode cache will not prevent hl ncps from growing
2554 * into infinity).
2555 */
2556 if ((poslimit = ncposlimit) == 0)
2557 poslimit = desiredvnodes * 2;
6363f268
MD
2558 if (critpath == 0)
2559 poslimit = poslimit * 8 / 10;
9e10d70b 2560
6363f268 2561 switch(pos_cache_hysteresis_state[critpath]) {
9e10d70b 2562 case CHI_LOW:
6363f268
MD
2563 if (xnumcache > poslimit && xnumcache > MINPOS) {
2564 if (critpath)
2565 _cache_cleanpos(ncposflush);
2566 else
2567 _cache_cleanpos(ncposflush +
2568 xnumcache - poslimit);
2569 pos_cache_hysteresis_state[critpath] = CHI_HIGH;
9e10d70b
MD
2570 }
2571 break;
2572 case CHI_HIGH:
6363f268
MD
2573 if (xnumcache > poslimit * 5 / 6 && xnumcache > MINPOS) {
2574 if (critpath)
2575 _cache_cleanpos(ncposflush);
2576 else
2577 _cache_cleanpos(ncposflush +
2578 xnumcache - poslimit * 5 / 6);
9e10d70b 2579 } else {
6363f268 2580 pos_cache_hysteresis_state[critpath] = CHI_LOW;
62d0f1f0
MD
2581 }
2582 break;
2583 }
65870584
MD
2584
2585 /*
2586 * Clean out dangling defered-zap ncps which could not
2587 * be cleanly dropped if too many build up. Note
2588 * that numdefered is not an exact number as such ncps
2589 * can be reused and the counter is not handled in a MP
2590 * safe manner by design.
2591 */
6363f268 2592 if (numdefered > neglimit) {
65870584
MD
2593 _cache_cleandefered();
2594 }
62d0f1f0
MD
2595}
2596
14c92d03
MD
2597/*
2598 * NEW NAMECACHE LOOKUP API
2599 *
2247fe02
MD
2600 * Lookup an entry in the namecache. The passed par_nch must be referenced
2601 * and unlocked. A referenced and locked nchandle with a non-NULL nch.ncp
2602 * is ALWAYS returned, eve if the supplied component is illegal.
2603 *
fad57d0e 2604 * The resulting namecache entry should be returned to the system with
2247fe02 2605 * cache_put() or cache_unlock() + cache_drop().
14c92d03
MD
2606 *
2607 * namecache locks are recursive but care must be taken to avoid lock order
2247fe02
MD
2608 * reversals (hence why the passed par_nch must be unlocked). Locking
2609 * rules are to order for parent traversals, not for child traversals.
14c92d03
MD
2610 *
2611 * Nobody else will be able to manipulate the associated namespace (e.g.
2612 * create, delete, rename, rename-target) until the caller unlocks the
2613 * entry.
2614 *
2615 * The returned entry will be in one of three states: positive hit (non-null
2616 * vnode), negative hit (null vnode), or unresolved (NCF_UNRESOLVED is set).
2617 * Unresolved entries must be resolved through the filesystem to associate the
2618 * vnode and/or determine whether a positive or negative hit has occured.
2619 *
2620 * It is not necessary to lock a directory in order to lock namespace under
2621 * that directory. In fact, it is explicitly not allowed to do that. A
2622 * directory is typically only locked when being created, renamed, or
2623 * destroyed.
2624 *
2625 * The directory (par) may be unresolved, in which case any returned child
2626 * will likely also be marked unresolved. Likely but not guarenteed. Since
fad57d0e
MD
2627 * the filesystem lookup requires a resolved directory vnode the caller is
2628 * responsible for resolving the namecache chain top-down. This API
14c92d03
MD
2629 * specifically allows whole chains to be created in an unresolved state.
2630 */
28623bf9
MD
2631struct nchandle
2632cache_nlookup(struct nchandle *par_nch, struct nlcomponent *nlc)
14c92d03 2633{
28623bf9 2634 struct nchandle nch;
690a3127
MD
2635 struct namecache *ncp;
2636 struct namecache *new_ncp;
f63911bf 2637 struct nchash_head *nchpp;
4b5bbb78 2638 struct mount *mp;
690a3127
MD
2639 u_int32_t hash;
2640 globaldata_t gd;
2247fe02 2641 int par_locked;
690a3127
MD
2642
2643 numcalls++;
2644 gd = mycpu;
4b5bbb78 2645 mp = par_nch->mount;
2247fe02
MD
2646 par_locked = 0;
2647
2648 /*
2649 * This is a good time to call it, no ncp's are locked by
2650 * the caller or us.
2651 */
6363f268 2652 cache_hysteresis(1);
690a3127 2653
690a3127
MD
2654 /*
2655 * Try to locate an existing entry
2656 */
2657 hash = fnv_32_buf(nlc->nlc_nameptr, nlc->nlc_namelen, FNV1_32_INIT);
28623bf9 2658 hash = fnv_32_buf(&par_nch->ncp, sizeof(par_nch->ncp), hash);
690a3127 2659 new_ncp = NULL;
f63911bf 2660 nchpp = NCHHASH(hash);
690a3127 2661restart:
287a8577 2662 spin_lock(&nchpp->spin);
f63911bf 2663 LIST_FOREACH(ncp, &nchpp->list, nc_hash) {
690a3127
MD
2664 numchecks++;
2665
690a3127
MD
2666 /*
2667 * Break out if we find a matching entry. Note that
e09206ba
MD
2668 * UNRESOLVED entries may match, but DESTROYED entries
2669 * do not.
690a3127 2670 */
28623bf9 2671 if (ncp->nc_parent == par_nch->ncp &&
690a3127 2672 ncp->nc_nlen == nlc->nlc_namelen &&
e09206ba
MD
2673 bcmp(ncp->nc_name, nlc->nlc_nameptr, ncp->nc_nlen) == 0 &&
2674 (ncp->nc_flag & NCF_DESTROYED) == 0
690a3127 2675 ) {
f63911bf 2676 _cache_hold(ncp);
287a8577 2677 spin_unlock(&nchpp->spin);
2247fe02
MD
2678 if (par_locked) {
2679 _cache_unlock(par_nch->ncp);
2680 par_locked = 0;
2681 }
2682 if (_cache_lock_special(ncp) == 0) {
4b5bbb78 2683 _cache_auto_unresolve(mp, ncp);
67773eb3 2684 if (new_ncp)
28623bf9 2685 _cache_free(new_ncp);
67773eb3
MD
2686 goto found;
2687 }
28623bf9
MD
2688 _cache_get(ncp);
2689 _cache_put(ncp);
f63911bf 2690 _cache_drop(ncp);
67773eb3 2691 goto restart;
690a3127
MD
2692 }
2693 }
2694
2695 /*
2696 * We failed to locate an entry, create a new entry and add it to
2247fe02
MD
2697 * the cache. The parent ncp must also be locked so we
2698 * can link into it.
2699 *
2700 * We have to relookup after possibly blocking in kmalloc or
2701 * when locking par_nch.
2702 *
2703 * NOTE: nlc_namelen can be 0 and nlc_nameptr NULL as a special
2704 * mount case, in which case nc_name will be NULL.
690a3127
MD
2705 */
2706 if (new_ncp == NULL) {
287a8577 2707 spin_unlock(&nchpp->spin);
524c845c 2708 new_ncp = cache_alloc(nlc->nlc_namelen);
2247fe02
MD
2709 if (nlc->nlc_namelen) {
2710 bcopy(nlc->nlc_nameptr, new_ncp->nc_name,
2711 nlc->nlc_namelen);
2712 new_ncp->nc_name[nlc->nlc_namelen] = 0;
2713 }
2714 goto restart;
2715 }
2716 if (par_locked == 0) {
287a8577 2717 spin_unlock(&nchpp->spin);
2247fe02
MD
2718 _cache_lock(par_nch->ncp);
2719 par_locked = 1;
690a3127
MD
2720 goto restart;
2721 }
690a3127
MD
2722
2723 /*
2247fe02 2724 * WARNING! We still hold the spinlock. We have to set the hash
668b43c5 2725 * table entry atomically.
690a3127 2726 */
2247fe02
MD
2727 ncp = new_ncp;
2728 _cache_link_parent(ncp, par_nch->ncp, nchpp);
287a8577 2729 spin_unlock(&nchpp->spin);
2247fe02
MD
2730 _cache_unlock(par_nch->ncp);
2731 /* par_locked = 0 - not used */
690a3127 2732found:
fad57d0e
MD
2733 /*
2734 * stats and namecache size management
2735 */
2736 if (ncp->nc_flag & NCF_UNRESOLVED)
2737 ++gd->gd_nchstats->ncs_miss;
2738 else if (ncp->nc_vp)
2739 ++gd->gd_nchstats->ncs_goodhits;
2740 else
2741 ++gd->gd_nchstats->ncs_neghits;
4b5bbb78 2742 nch.mount = mp;
28623bf9 2743 nch.ncp = ncp;
61f96b6f 2744 atomic_add_int(&nch.mount->mnt_refs, 1);
28623bf9 2745 return(nch);
690a3127
MD
2746}
2747
79fd1696
MD
2748/*
2749 * Attempt to lookup a namecache entry and return with a shared namecache
2750 * lock.
2751 */
2752int
2753cache_nlookup_maybe_shared(struct nchandle *par_nch, struct nlcomponent *nlc,
2754 int excl, struct nchandle *res_nch)
2755{
2756 struct namecache *ncp;
2757 struct nchash_head *nchpp;
2758 struct mount *mp;
2759 u_int32_t hash;
2760 globaldata_t gd;
2761
2762 /*
2763 * If exclusive requested or shared namecache locks are disabled,
2764 * return failure.
2765 */
2766 if (ncp_shared_lock_disable || excl)
2767 return(EWOULDBLOCK);
2768
2769 numcalls++;
2770 gd = mycpu;
2771 mp = par_nch->mount;
2772
2773 /*
2774 * This is a good time to call it, no ncp's are locked by
2775 * the caller or us.
2776 */
6363f268 2777 cache_hysteresis(1);
79fd1696
MD
2778
2779 /*
2780 * Try to locate an existing entry
2781 */
2782 hash = fnv_32_buf(nlc->nlc_nameptr, nlc->nlc_namelen, FNV1_32_INIT);
2783 hash = fnv_32_buf(&par_nch->ncp, sizeof(par_nch->ncp), hash);
2784 nchpp = NCHHASH(hash);
2785
2786 spin_lock(&nchpp->spin);
2787
2788 LIST_FOREACH(ncp, &nchpp->list, nc_hash) {
2789 numchecks++;
2790
2791 /*
2792 * Break out if we find a matching entry. Note that
2793 * UNRESOLVED entries may match, but DESTROYED entries
2794 * do not.
2795 */
2796 if (ncp->nc_parent == par_nch->ncp &&
2797 ncp->nc_nlen == nlc->nlc_namelen &&
2798 bcmp(ncp->nc_name, nlc->nlc_nameptr, ncp->nc_nlen) == 0 &&
2799 (ncp->nc_flag & NCF_DESTROYED) == 0
2800 ) {
2801 _cache_hold(ncp);
2802 spin_unlock(&nchpp->spin);
2803 if (_cache_lock_shared_special(ncp) == 0) {
2804 if ((ncp->nc_flag & NCF_UNRESOLVED) == 0 &&
2805 (ncp->nc_flag & NCF_DESTROYED) == 0 &&
2806 _cache_auto_unresolve_test(mp, ncp) == 0) {
2807 goto found;
2808 }
2809 _cache_unlock(ncp);
2810 }
2811 _cache_drop(ncp);
2812 spin_lock(&nchpp->spin);
2813 break;
2814 }
2815 }
2816
2817 /*
2818 * Failure
2819 */
2820 spin_unlock(&nchpp->spin);
2821 return(EWOULDBLOCK);
2822
2823 /*
2824 * Success
2825 *
2826 * Note that nc_error might be non-zero (e.g ENOENT).
2827 */
2828found:
2829 res_nch->mount = mp;
2830 res_nch->ncp = ncp;
2831 ++gd->gd_nchstats->ncs_goodhits;
2832 atomic_add_int(&res_nch->mount->mnt_refs, 1);
2833
2834 KKASSERT(ncp->nc_error != EWOULDBLOCK);
2835 return(ncp->nc_error);
2836}
2837
668b43c5
MD
2838/*
2839 * This is a non-blocking verison of cache_nlookup() used by
2840 * nfs_readdirplusrpc_uio(). It can fail for any reason and
2841 * will return nch.ncp == NULL in that case.
2842 */
2843struct nchandle
2844cache_nlookup_nonblock(struct nchandle *par_nch, struct nlcomponent *nlc)
2845{
2846 struct nchandle nch;
2847 struct namecache *ncp;
2848 struct namecache *new_ncp;
2849 struct nchash_head *nchpp;
2850 struct mount *mp;
2851 u_int32_t hash;
2852 globaldata_t gd;
2853 int par_locked;
2854
2855 numcalls++;
2856 gd = mycpu;
2857 mp = par_nch->mount;
2858 par_locked = 0;
2859
2860 /*
2861 * Try to locate an existing entry
2862 */
2863 hash = fnv_32_buf(nlc->nlc_nameptr, nlc->nlc_namelen, FNV1_32_INIT);
2864 hash = fnv_32_buf(&par_nch->ncp, sizeof(par_nch->ncp), hash);
2865 new_ncp = NULL;
2866 nchpp = NCHHASH(hash);
2867restart:
287a8577 2868 spin_lock(&nchpp->spin);
668b43c5
MD
2869 LIST_FOREACH(ncp, &nchpp->list, nc_hash) {
2870 numchecks++;
2871
2872 /*
2873 * Break out if we find a matching entry. Note that
2874 * UNRESOLVED entries may match, but DESTROYED entries
2875 * do not.
2876 */
2877 if (ncp->nc_parent == par_nch->ncp &&
2878 ncp->nc_nlen == nlc->nlc_namelen &&
2879 bcmp(ncp->nc_name, nlc->nlc_nameptr, ncp->nc_nlen) == 0 &&
2880 (ncp->nc_flag & NCF_DESTROYED) == 0
2881 ) {
2882 _cache_hold(ncp);
287a8577 2883 spin_unlock(&nchpp->spin);
668b43c5
MD
2884 if (par_locked) {
2885 _cache_unlock(par_nch->ncp);
2886 par_locked = 0;
2887 }
2888 if (_cache_lock_special(ncp) == 0) {
2889 _cache_auto_unresolve(mp, ncp);
2890 if (new_ncp) {
2891 _cache_free(new_ncp);
2892 new_ncp = NULL;
2893 }
2894 goto found;
2895 }
2896 _cache_drop(ncp);
2897 goto failed;
2898 }
2899 }
2900
2901 /*
2902 * We failed to locate an entry, create a new entry and add it to
2903 * the cache. The parent ncp must also be locked so we
2904 * can link into it.
2905 *
2906 * We have to relookup after possibly blocking in kmalloc or
2907 * when locking par_nch.
2908 *
2909 * NOTE: nlc_namelen can be 0 and nlc_nameptr NULL as a special
2910 * mount case, in which case nc_name will be NULL.
2911 */
2912 if (new_ncp == NULL) {
287a8577 2913 spin_unlock(&nchpp->spin);
668b43c5
MD
2914 new_ncp = cache_alloc(nlc->nlc_namelen);
2915 if (nlc->nlc_namelen) {
2916 bcopy(nlc->nlc_nameptr, new_ncp->nc_name,
2917 nlc->nlc_namelen);
2918 new_ncp->nc_name[nlc->nlc_namelen] = 0;
2919 }
2920 goto restart;
2921 }
2922 if (par_locked == 0) {
287a8577 2923 spin_unlock(&nchpp->spin);
668b43c5
MD
2924 if (_cache_lock_nonblock(par_nch->ncp) == 0) {
2925 par_locked = 1;
2926 goto restart;
2927 }
2928 goto failed;
2929 }
2930
2931 /*
2932 * WARNING! We still hold the spinlock. We have to set the hash
2933 * table entry atomically.
2934 */
2935 ncp = new_ncp;
2936 _cache_link_parent(ncp, par_nch->ncp, nchpp);
287a8577 2937 spin_unlock(&nchpp->spin);
668b43c5
MD
2938 _cache_unlock(par_nch->ncp);
2939 /* par_locked = 0 - not used */
2940found:
2941 /*
2942 * stats and namecache size management
2943 */
2944 if (ncp->nc_flag & NCF_UNRESOLVED)
2945 ++gd->gd_nchstats->ncs_miss;
2946 else if (ncp->nc_vp)
2947 ++gd->gd_nchstats->ncs_goodhits;
2948 else
2949 ++gd->gd_nchstats->ncs_neghits;
2950 nch.mount = mp;
2951 nch.ncp = ncp;
2952 atomic_add_int(&nch.mount->mnt_refs, 1);
2953 return(nch);
2954failed:
2955 if (new_ncp) {
2956 _cache_free(new_ncp);
2957 new_ncp = NULL;
2958 }
2959 nch.mount = NULL;
2960 nch.ncp = NULL;
2961 return(nch);
2962}
2963
1d505369 2964/*
28623bf9 2965 * The namecache entry is marked as being used as a mount point.
07baed26
MD
2966 * Locate the mount if it is visible to the caller. The DragonFly
2967 * mount system allows arbitrary loops in the topology and disentangles
2968 * those loops by matching against (mp, ncp) rather than just (ncp).
2969 * This means any given ncp can dive any number of mounts, depending
2970 * on the relative mount (e.g. nullfs) the caller is at in the topology.
2971 *
2972 * We use a very simple frontend cache to reduce SMP conflicts,
2973 * which we have to do because the mountlist scan needs an exclusive
2974 * lock around its ripout info list. Not to mention that there might
2975 * be a lot of mounts.
1d505369 2976 */
28623bf9
MD
2977struct findmount_info {
2978 struct mount *result;
2979 struct mount *nch_mount;
2980 struct namecache *nch_ncp;
2981};
2982
07baed26
MD
2983static
2984struct ncmount_cache *
2985ncmount_cache_lookup(struct mount *mp, struct namecache *ncp)
2986{
2987 int hash;
2988
2989 hash = ((int)(intptr_t)mp / sizeof(*mp)) ^
2990 ((int)(intptr_t)ncp / sizeof(*ncp));
7e9c94bd 2991 hash = (hash & 0x7FFFFFFF) % NCMOUNT_NUMCACHE;
07baed26
MD
2992 return (&ncmount_cache[hash]);
2993}
2994
28623bf9
MD
2995static
2996int
2997cache_findmount_callback(struct mount *mp, void *data)
1d505369 2998{
28623bf9 2999 struct findmount_info *info = data;
1d505369 3000
28623bf9
MD
3001 /*
3002 * Check the mount's mounted-on point against the passed nch.
3003 */
3004 if (mp->mnt_ncmounton.mount == info->nch_mount &&
3005 mp->mnt_ncmounton.ncp == info->nch_ncp
3006 ) {
3007 info->result = mp;
6fa06591 3008 atomic_add_int(&mp->mnt_refs, 1);
28623bf9 3009 return(-1);
1d505369 3010 }
28623bf9 3011 return(0);
1d505369
MD
3012}
3013
28623bf9
MD
3014struct mount *
3015cache_findmount(struct nchandle *nch)
9b1b3591 3016{
28623bf9 3017 struct findmount_info info;
07baed26
MD
3018 struct ncmount_cache *ncc;
3019 struct mount *mp;
3020
3021 /*
3022 * Fast
3023 */
7e9c94bd
MD
3024 if (ncmount_cache_enable == 0) {
3025 ncc = NULL;
3026 goto skip;
3027 }
07baed26
MD
3028 ncc = ncmount_cache_lookup(nch->mount, nch->ncp);
3029 if (ncc->ncp == nch->ncp) {
3030 spin_lock_shared(&ncc->spin);
9c105d5b
MD
3031 if (ncc->isneg == 0 &&
3032 ncc->ncp == nch->ncp && (mp = ncc->mp) != NULL) {
07baed26
MD
3033 if (mp->mnt_ncmounton.mount == nch->mount &&
3034 mp->mnt_ncmounton.ncp == nch->ncp) {
9c105d5b
MD
3035 /*
3036 * Cache hit (positive)
3037 */
07baed26
MD
3038 atomic_add_int(&mp->mnt_refs, 1);
3039 spin_unlock_shared(&ncc->spin);
7e9c94bd 3040 ++ncmount_cache_hit;
07baed26
MD
3041 return(mp);
3042 }
9c105d5b
MD
3043 /* else cache miss */
3044 }
3045 if (ncc->isneg &&
3046 ncc->ncp == nch->ncp && ncc->mp == nch->mount) {
3047 /*
3048 * Cache hit (negative)
3049 */
3050 spin_unlock_shared(&ncc->spin);
3051 ++ncmount_cache_hit;
3052 return(NULL);
07baed26
MD
3053 }
3054 spin_unlock_shared(&ncc->spin);
3055 }
7e9c94bd 3056skip:
28623bf9 3057
07baed26
MD
3058 /*
3059 * Slow
3060 */
28623bf9
MD
3061 info.result = NULL;
3062 info.nch_mount = nch->mount;
3063 info.nch_ncp = nch->ncp;
3064 mountlist_scan(cache_findmount_callback, &info,
3065 MNTSCAN_FORWARD|MNTSCAN_NOBUSY);
07baed26 3066
9c105d5b
MD
3067 /*
3068 * Cache the result.
3069 *
3070 * Negative lookups: We cache the originating {ncp,mp}. (mp) is
3071 * only used for pointer comparisons and is not
3072 * referenced (otherwise there would be dangling
3073 * refs).
3074 *
3075 * Positive lookups: We cache the originating {ncp} and the target
3076 * (mp). (mp) is referenced.
3077 *
3078 * Indeterminant: If the match is undergoing an unmount we do
3079 * not cache it to avoid racing cache_unmounting(),
3080 * but still return the match.
3081 */
3082 if (ncc) {
07baed26 3083 spin_lock(&ncc->spin);
9c105d5b
MD
3084 if (info.result == NULL) {
3085 if (ncc->isneg == 0 && ncc->mp)
3086 atomic_add_int(&ncc->mp->mnt_refs, -1);
3087 ncc->ncp = nch->ncp;
3088 ncc->mp = nch->mount;
3089 ncc->isneg = 1;
3090 spin_unlock(&ncc->spin);
3091 ++ncmount_cache_overwrite;
3092 } else if ((info.result->mnt_kern_flag & MNTK_UNMOUNT) == 0) {
3093 if (ncc->isneg == 0 && ncc->mp)
07baed26
MD
3094 atomic_add_int(&ncc->mp->mnt_refs, -1);
3095 atomic_add_int(&info.result->mnt_refs, 1);
07baed26 3096 ncc->ncp = nch->ncp;
9c105d5b
MD
3097 ncc->mp = info.result;
3098 ncc->isneg = 0;
07baed26 3099 spin_unlock(&ncc->spin);
7e9c94bd 3100 ++ncmount_cache_overwrite;
07baed26
MD
3101 } else {
3102 spin_unlock(&ncc->spin);
3103 }
7e9c94bd 3104 ++ncmount_cache_miss;
07baed26 3105 }
28623bf9 3106 return(info.result);
9b1b3591
MD
3107}
3108
6fa06591
MD
3109void
3110cache_dropmount(struct mount *mp)
3111{
3112 atomic_add_int(&mp->mnt_refs, -1);
3113}
3114
9c105d5b
MD
3115void
3116cache_ismounting(struct mount *mp)
3117{
3118 struct nchandle *nch = &mp->mnt_ncmounton;
3119 struct ncmount_cache *ncc;
3120
3121 ncc = ncmount_cache_lookup(nch->mount, nch->ncp);
3122 if (ncc->isneg &&
3123 ncc->ncp == nch->ncp && ncc->mp == nch->mount) {
3124 spin_lock(&ncc->spin);
3125 if (ncc->isneg &&
3126 ncc->ncp == nch->ncp && ncc->mp == nch->mount) {
3127 ncc->ncp = NULL;
3128 ncc->mp = NULL;
3129 }
3130 spin_unlock(&ncc->spin);
3131 }
3132}
3133
07baed26
MD
3134void
3135cache_unmounting(struct mount *mp)
3136{
3137 struct nchandle *nch = &mp->mnt_ncmounton;
3138 struct ncmount_cache *ncc;
3139
3140 ncc = ncmount_cache_lookup(nch->mount, nch->ncp);
9c105d5b
MD
3141 if (ncc->isneg == 0 &&
3142 ncc->ncp == nch->ncp && ncc->mp == mp) {
07baed26 3143 spin_lock(&ncc->spin);
9c105d5b
MD
3144 if (ncc->isneg == 0 &&
3145 ncc->ncp == nch->ncp && ncc->mp == mp) {
07baed26
MD
3146 atomic_add_int(&mp->mnt_refs, -1);
3147 ncc->ncp = NULL;
3148 ncc->mp = NULL;
3149 }
3150 spin_unlock(&ncc->spin);
3151 }
3152}
3153
690a3127 3154/*
21739618 3155 * Resolve an unresolved namecache entry, generally by looking it up.
67773eb3 3156 * The passed ncp must be locked and refd.
21739618
MD
3157 *
3158 * Theoretically since a vnode cannot be recycled while held, and since
3159 * the nc_parent chain holds its vnode as long as children exist, the
3160 * direct parent of the cache entry we are trying to resolve should
3161 * have a valid vnode. If not then generate an error that we can
3162 * determine is related to a resolver bug.
fad57d0e 3163 *
9b1b3591
MD
3164 * However, if a vnode was in the middle of a recyclement when the NCP
3165 * got locked, ncp->nc_vp might point to a vnode that is about to become
3166 * invalid. cache_resolve() handles this case by unresolving the entry
3167 * and then re-resolving it.
3168 *
fad57d0e
MD
3169 * Note that successful resolution does not necessarily return an error
3170 * code of 0. If the ncp resolves to a negative cache hit then ENOENT
3171 * will be returned.
690a3127
MD
3172 */
3173int
28623bf9 3174cache_resolve(struct nchandle *nch, struct ucred *cred)
690a3127 3175{
2247fe02 3176 struct namecache *par_tmp;
21739618 3177 struct namecache *par;
28623bf9
MD
3178 struct namecache *ncp;
3179 struct nchandle nctmp;
3180 struct mount *mp;
dff430ab 3181 struct vnode *dvp;
67773eb3 3182 int error;
8e005a45 3183
28623bf9
MD
3184 ncp = nch->ncp;
3185 mp = nch->mount;
79fd1696 3186 KKASSERT(_cache_lockstatus(ncp) == LK_EXCLUSIVE);
67773eb3 3187restart:
8e005a45 3188 /*
9b1b3591
MD
3189 * If the ncp is already resolved we have nothing to do. However,
3190 * we do want to guarentee that a usable vnode is returned when
3191 * a vnode is present, so make sure it hasn't been reclaimed.
8e005a45 3192 */
9b1b3591
MD
3193 if ((ncp->nc_flag & NCF_UNRESOLVED) == 0) {
3194 if (ncp->nc_vp && (ncp->nc_vp->v_flag & VRECLAIMED))
28623bf9 3195 _cache_setunresolved(ncp);
9b1b3591
MD
3196 if ((ncp->nc_flag & NCF_UNRESOLVED) == 0)
3197 return (ncp->nc_error);
3198 }
21739618 3199
81c4868c
MD
3200 /*
3201 * If the ncp was destroyed it will never resolve again. This
3202 * can basically only happen when someone is chdir'd into an
3203 * empty directory which is then rmdir'd. We want to catch this
3204 * here and not dive the VFS because the VFS might actually
3205 * have a way to re-resolve the disconnected ncp, which will
3206 * result in inconsistencies in the cdir/nch for proc->p_fd.
3207 */
3208 if (ncp->nc_flag & NCF_DESTROYED) {
3209 kprintf("Warning: cache_resolve: ncp '%s' was unlinked\n",
3210 ncp->nc_name);
3211 return(EINVAL);
3212 }
3213
646a1cda
MD
3214 /*
3215 * Mount points need special handling because the parent does not
3216 * belong to the same filesystem as the ncp.
3217 */
28623bf9
MD
3218 if (ncp == mp->mnt_ncmountpt.ncp)
3219 return (cache_resolve_mp(mp));
646a1cda
MD
3220
3221 /*
3222 * We expect an unbroken chain of ncps to at least the mount point,
3223 * and even all the way to root (but this code doesn't have to go
3224 * past the mount point).
3225 */
3226 if (ncp->nc_parent == NULL) {
6ea70f76 3227 kprintf("EXDEV case 1 %p %*.*s\n", ncp,
646a1cda 3228 ncp->nc_nlen, ncp->nc_nlen, ncp->nc_name);
21739618 3229 ncp->nc_error = EXDEV;
646a1cda
MD
3230 return(ncp->nc_error);
3231 }
3232
3233 /*
3234 * The vp's of the parent directories in the chain are held via vhold()
3235 * due to the existance of the child, and should not disappear.
3236 * However, there are cases where they can disappear:
3237 *
3238 * - due to filesystem I/O errors.
3239 * - due to NFS being stupid about tracking the namespace and
3240 * destroys the namespace for entire directories quite often.
3241 * - due to forced unmounts.
e09206ba 3242 * - due to an rmdir (parent will be marked DESTROYED)
646a1cda
MD
3243 *
3244 * When this occurs we have to track the chain backwards and resolve
3245 * it, looping until the resolver catches up to the current node. We
3246 * could recurse here but we might run ourselves out of kernel stack
3247 * so we do it in a more painful manner. This situation really should
3248 * not occur all that often, or if it does not have to go back too
3249 * many nodes to resolve the ncp.
3250 */
5312fa43 3251 while ((dvp = cache_dvpref(ncp)) == NULL) {
e09206ba
MD
3252 /*
3253 * This case can occur if a process is CD'd into a
3254 * directory which is then rmdir'd. If the parent is marked
3255 * destroyed there is no point trying to resolve it.
3256 */
3257 if (ncp->nc_parent->nc_flag & NCF_DESTROYED)
3258 return(ENOENT);
646a1cda 3259 par = ncp->nc_parent;
2247fe02
MD
3260 _cache_hold(par);
3261 _cache_lock(par);
3262 while ((par_tmp = par->nc_parent) != NULL &&
3263 par_tmp->nc_vp == NULL) {
3264 _cache_hold(par_tmp);
3265 _cache_lock(par_tmp);
3266 _cache_put(par);
3267 par = par_tmp;
3268 }
646a1cda 3269 if (par->nc_parent == NULL) {
6ea70f76 3270 kprintf("EXDEV case 2 %*.*s\n",
646a1cda 3271 par->nc_nlen, par->nc_nlen, par->nc_name);
2247fe02 3272 _cache_put(par);
646a1cda
MD
3273 return (EXDEV);
3274 }
6ea70f76 3275 kprintf("[diagnostic] cache_resolve: had to recurse on %*.*s\n",
646a1cda
MD
3276 par->nc_nlen, par->nc_nlen, par->nc_name);
3277 /*
67773eb3
MD
3278 * The parent is not set in stone, ref and lock it to prevent
3279 * it from disappearing. Also note that due to renames it
3280 * is possible for our ncp to move and for par to no longer
3281 * be one of its parents. We resolve it anyway, the loop
3282 * will handle any moves.
646a1cda 3283 */
2247fe02
MD
3284 _cache_get(par); /* additional hold/lock */
3285 _cache_put(par); /* from earlier hold/lock */
28623bf9
MD
3286 if (par == nch->mount->mnt_ncmountpt.ncp) {
3287 cache_resolve_mp(nch->mount);
c0c70b27 3288 } else if ((dvp = cache_dvpref(par)) == NULL) {
6ea70f76 3289 kprintf("[diagnostic] cache_resolve: raced on %*.*s\n", par->nc_nlen, par->nc_nlen, par->nc_name);
28623bf9 3290 _cache_put(par);
8e005a45 3291 continue;
c0c70b27
MD
3292 } else {
3293 if (par->nc_flag & NCF_UNRESOLVED) {
3294 nctmp.mount = mp;
3295 nctmp.ncp = par;
3296 par->nc_error = VOP_NRESOLVE(&nctmp, dvp, cred);
3297 }
5312fa43 3298 vrele(dvp);
646a1cda 3299 }
67773eb3
MD
3300 if ((error = par->nc_error) != 0) {
3301 if (par->nc_error != EAGAIN) {
6ea70f76 3302 kprintf("EXDEV case 3 %*.*s error %d\n",
67773eb3
MD
3303 par->nc_nlen, par->nc_nlen, par->nc_name,
3304 par->nc_error);
28623bf9 3305 _cache_put(par);
67773eb3
MD
3306 return(error);
3307 }
6ea70f76 3308 kprintf("[diagnostic] cache_resolve: EAGAIN par %p %*.*s\n",
67773eb3 3309 par, par->nc_nlen, par->nc_nlen, par->nc_name);
646a1cda 3310 }
28623bf9 3311 _cache_put(par);
67773eb3 3312 /* loop */
646a1cda 3313 }
8e005a45
MD
3314
3315 /*
fad57d0e 3316 * Call VOP_NRESOLVE() to get the vp, then scan for any disconnected
8e005a45
MD
3317 * ncp's and reattach them. If this occurs the original ncp is marked
3318 * EAGAIN to force a relookup.
fad57d0e
MD
3319 *
3320 * NOTE: in order to call VOP_NRESOLVE(), the parent of the passed
3321 * ncp must already be resolved.
8e005a45 3322 */
5312fa43 3323 if (dvp) {
c0c70b27
MD
3324 nctmp.mount = mp;
3325 nctmp.ncp = ncp;
3326 ncp->nc_error = VOP_NRESOLVE(&nctmp, dvp, cred);
5312fa43 3327 vrele(dvp);
c0c70b27
MD
3328 } else {
3329 ncp->nc_error = EPERM;
3330 }
67773eb3 3331 if (ncp->nc_error == EAGAIN) {
6ea70f76 3332 kprintf("[diagnostic] cache_resolve: EAGAIN ncp %p %*.*s\n",
67773eb3
MD
3333 ncp, ncp->nc_nlen, ncp->nc_nlen, ncp->nc_name);
3334 goto restart;
3335 }
646a1cda
MD
3336 return(ncp->nc_error);
3337}
3338
3339/*
3340 * Resolve the ncp associated with a mount point. Such ncp's almost always
3341 * remain resolved and this routine is rarely called. NFS MPs tends to force
3342 * re-resolution more often due to its mac-truck-smash-the-namecache
3343 * method of tracking namespace changes.
3344 *
6215aa92
MD
3345 * The semantics for this call is that the passed ncp must be locked on
3346 * entry and will be locked on return. However, if we actually have to
3347 * resolve the mount point we temporarily unlock the entry in order to
3348 * avoid race-to-root deadlocks due to e.g. dead NFS mounts. Because of
3349 * the unlock we have to recheck the flags after we relock.
646a1cda
MD
3350 */
3351static int
28623bf9 3352cache_resolve_mp(struct mount *mp)
646a1cda 3353{
28623bf9 3354 struct namecache *ncp = mp->mnt_ncmountpt.ncp;
646a1cda 3355 struct vnode *vp;
6215aa92 3356 int error;
646a1cda
MD
3357
3358 KKASSERT(mp != NULL);
9b1b3591
MD
3359
3360 /*
3361 * If the ncp is already resolved we have nothing to do. However,
3362 * we do want to guarentee that a usable vnode is returned when
3363 * a vnode is present, so make sure it hasn't been reclaimed.
3364 */
3365 if ((ncp->nc_flag & NCF_UNRESOLVED) == 0) {
3366 if (ncp->nc_vp && (ncp->nc_vp->v_flag & VRECLAIMED))
28623bf9 3367 _cache_setunresolved(ncp);
9b1b3591
MD
3368 }
3369
646a1cda 3370 if (ncp->nc_flag & NCF_UNRESOLVED) {
28623bf9 3371 _cache_unlock(ncp);
f9642f56 3372 while (vfs_busy(mp, 0))
646a1cda 3373 ;
6215aa92 3374 error = VFS_ROOT(mp, &vp);
28623bf9 3375 _cache_lock(ncp);
6215aa92
MD
3376
3377 /*
3378 * recheck the ncp state after relocking.
3379 */
3380 if (ncp->nc_flag & NCF_UNRESOLVED) {
3381 ncp->nc_error = error;
3382 if (error == 0) {
4b5bbb78 3383 _cache_setvp(mp, ncp, vp);
6215aa92
MD
3384 vput(vp);
3385 } else {
341a6e45
MD
3386 kprintf("[diagnostic] cache_resolve_mp: failed"
3387 " to resolve mount %p err=%d ncp=%p\n",
3388 mp, error, ncp);
4b5bbb78 3389 _cache_setvp(mp, ncp, NULL);
6215aa92
MD
3390 }
3391 } else if (error == 0) {
646a1cda 3392 vput(vp);
646a1cda 3393 }
f9642f56 3394 vfs_unbusy(mp);
21739618
MD
3395 }
3396 return(ncp->nc_error);
14c92d03
MD
3397}
3398
f63911bf 3399/*
65870584 3400 * Clean out negative cache entries when too many have accumulated.
f63911bf 3401 */
65870584
MD
3402static void
3403_cache_cleanneg(int count)
62d0f1f0
MD
3404{
3405 struct namecache *ncp;
7ea21ed1 3406
62d0f1f0
MD
3407 /*
3408 * Attempt to clean out the specified number of negative cache
3409 * entries.
3410 */
3411 while (count) {
287a8577 3412 spin_lock(&ncspin);
7ea21ed1 3413 ncp = TAILQ_FIRST(&ncneglist);
eb82ae62 3414 if (ncp == NULL) {
287a8577 3415 spin_unlock(&ncspin);
eb82ae62
MD
3416 break;
3417 }
62d0f1f0
MD
3418 TAILQ_REMOVE(&ncneglist, ncp, nc_vnode);
3419 TAILQ_INSERT_TAIL(&ncneglist, ncp, nc_vnode);
f63911bf 3420 _cache_hold(ncp);
287a8577 3421 spin_unlock(&ncspin);
8214af3d
MD
3422
3423 /*
3424 * This can race, so we must re-check that the ncp
3425 * is on the ncneglist after successfully locking it.
3426 */
2247fe02 3427 if (_cache_lock_special(ncp) == 0) {
8214af3d
MD
3428 if (ncp->nc_vp == NULL &&
3429 (ncp->nc_flag & NCF_UNRESOLVED) == 0) {
3430 ncp = cache_zap(ncp, 1);
3431 if (ncp)
3432 _cache_drop(ncp);
3433 } else {
3434 kprintf("cache_cleanneg: race avoided\n");
3435 _cache_unlock(ncp);
3436 }
f63911bf
MD
3437 } else {
3438 _cache_drop(ncp);
3439 }
62d0f1f0 3440 --count;
984263bc
MD
3441 }
3442}
3443
9e10d70b
MD
3444/*
3445 * Clean out positive cache entries when too many have accumulated.
9e10d70b
MD
3446 */
3447static void
3448_cache_cleanpos(int count)
3449{
3450 static volatile int rover;
3451 struct nchash_head *nchpp;
3452 struct namecache *ncp;
3453 int rover_copy;
3454
3455 /*
3456 * Attempt to clean out the specified number of negative cache
3457 * entries.
3458 */
3459 while (count) {
3460 rover_copy = ++rover; /* MPSAFEENOUGH */
5c551789 3461 cpu_ccfence();
9e10d70b
MD
3462 nchpp = NCHHASH(rover_copy);
3463
3464 spin_lock(&nchpp->spin);
3465 ncp = LIST_FIRST(&nchpp->list);
3466 if (ncp)
3467 _cache_hold(ncp);
3468 spin_unlock(&nchpp->spin);
3469
3470 if (ncp) {
3471 if (_cache_lock_special(ncp) == 0) {
3472 ncp = cache_zap(ncp, 1);
3473 if (ncp)
3474 _cache_drop(ncp);
3475 } else {
3476 _cache_drop(ncp);
3477 }
3478 }
3479 --count;
3480 }
3481}
3482
65870584
MD
3483/*
3484 * This is a kitchen sink function to clean out ncps which we
3485 * tried to zap from cache_drop() but failed because we were
3486 * unable to acquire the parent lock.
3487 *
3488 * Such entries can also be removed via cache_inval_vp(), such
3489 * as when unmounting.
65870584
MD
3490 */
3491static void
3492_cache_cleandefered(void)
3493{
3494 struct nchash_head *nchpp;
3495 struct namecache *ncp;
3496 struct namecache dummy;
3497 int i;
3498