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