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