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