Add tunable for each_burst.
[dragonfly.git] / sys / kern / vfs_cache.c
blob3b3104fdb988c0eb141ada8db8e5517aa5199e1b
1 /*
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:
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.
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.
34 * Copyright (c) 1989, 1993, 1995
35 * The Regents of the University of California. All rights reserved.
37 * This code is derived from software contributed to Berkeley by
38 * Poul-Henning Kamp of the FreeBSD Project.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
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.
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.
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 $
70 * $DragonFly: src/sys/kern/vfs_cache.c,v 1.88 2008/02/06 08:53:15 dillon Exp $
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>
79 #include <sys/malloc.h>
80 #include <sys/sysproto.h>
81 #include <sys/proc.h>
82 #include <sys/namei.h>
83 #include <sys/nlookup.h>
84 #include <sys/filedesc.h>
85 #include <sys/fnv_hash.h>
86 #include <sys/globaldata.h>
87 #include <sys/kern_syscall.h>
88 #include <sys/dirent.h>
89 #include <ddb/ddb.h>
91 #include <sys/sysref2.h>
93 #define MAX_RECURSION_DEPTH 64
96 * Random lookups in the cache are accomplished with a hash table using
97 * a hash key of (nc_src_vp, name).
99 * Negative entries may exist and correspond to structures where nc_vp
100 * is NULL. In a negative entry, NCF_WHITEOUT will be set if the entry
101 * corresponds to a whited-out directory entry (verses simply not finding the
102 * entry at all).
104 * Upon reaching the last segment of a path, if the reference is for DELETE,
105 * or NOCACHE is set (rewrite), and the name is located in the cache, it
106 * will be dropped.
110 * Structures associated with name cacheing.
112 #define NCHHASH(hash) (&nchashtbl[(hash) & nchash])
113 #define MINNEG 1024
115 MALLOC_DEFINE(M_VFSCACHE, "vfscache", "VFS name cache entries");
117 static LIST_HEAD(nchashhead, namecache) *nchashtbl; /* Hash Table */
118 static struct namecache_list ncneglist; /* instead of vnode */
121 * ncvp_debug - debug cache_fromvp(). This is used by the NFS server
122 * to create the namecache infrastructure leading to a dangling vnode.
124 * 0 Only errors are reported
125 * 1 Successes are reported
126 * 2 Successes + the whole directory scan is reported
127 * 3 Force the directory scan code run as if the parent vnode did not
128 * have a namecache record, even if it does have one.
130 static int ncvp_debug;
131 SYSCTL_INT(_debug, OID_AUTO, ncvp_debug, CTLFLAG_RW, &ncvp_debug, 0, "");
133 static u_long nchash; /* size of hash table */
134 SYSCTL_ULONG(_debug, OID_AUTO, nchash, CTLFLAG_RD, &nchash, 0, "");
136 static u_long ncnegfactor = 16; /* ratio of negative entries */
137 SYSCTL_ULONG(_debug, OID_AUTO, ncnegfactor, CTLFLAG_RW, &ncnegfactor, 0, "");
139 static int nclockwarn; /* warn on locked entries in ticks */
140 SYSCTL_INT(_debug, OID_AUTO, nclockwarn, CTLFLAG_RW, &nclockwarn, 0, "");
142 static u_long numneg; /* number of cache entries allocated */
143 SYSCTL_ULONG(_debug, OID_AUTO, numneg, CTLFLAG_RD, &numneg, 0, "");
145 static u_long numcache; /* number of cache entries allocated */
146 SYSCTL_ULONG(_debug, OID_AUTO, numcache, CTLFLAG_RD, &numcache, 0, "");
148 static u_long numunres; /* number of unresolved entries */
149 SYSCTL_ULONG(_debug, OID_AUTO, numunres, CTLFLAG_RD, &numunres, 0, "");
151 SYSCTL_INT(_debug, OID_AUTO, vnsize, CTLFLAG_RD, 0, sizeof(struct vnode), "");
152 SYSCTL_INT(_debug, OID_AUTO, ncsize, CTLFLAG_RD, 0, sizeof(struct namecache), "");
154 static int cache_resolve_mp(struct mount *mp);
155 static void _cache_rehash(struct namecache *ncp);
156 static void _cache_lock(struct namecache *ncp);
157 static void _cache_setunresolved(struct namecache *ncp);
160 * The new name cache statistics
162 SYSCTL_NODE(_vfs, OID_AUTO, cache, CTLFLAG_RW, 0, "Name cache statistics");
163 #define STATNODE(mode, name, var) \
164 SYSCTL_ULONG(_vfs_cache, OID_AUTO, name, mode, var, 0, "");
165 STATNODE(CTLFLAG_RD, numneg, &numneg);
166 STATNODE(CTLFLAG_RD, numcache, &numcache);
167 static u_long numcalls; STATNODE(CTLFLAG_RD, numcalls, &numcalls);
168 static u_long dothits; STATNODE(CTLFLAG_RD, dothits, &dothits);
169 static u_long dotdothits; STATNODE(CTLFLAG_RD, dotdothits, &dotdothits);
170 static u_long numchecks; STATNODE(CTLFLAG_RD, numchecks, &numchecks);
171 static u_long nummiss; STATNODE(CTLFLAG_RD, nummiss, &nummiss);
172 static u_long nummisszap; STATNODE(CTLFLAG_RD, nummisszap, &nummisszap);
173 static u_long numposzaps; STATNODE(CTLFLAG_RD, numposzaps, &numposzaps);
174 static u_long numposhits; STATNODE(CTLFLAG_RD, numposhits, &numposhits);
175 static u_long numnegzaps; STATNODE(CTLFLAG_RD, numnegzaps, &numnegzaps);
176 static u_long numneghits; STATNODE(CTLFLAG_RD, numneghits, &numneghits);
178 struct nchstats nchstats[SMP_MAXCPU];
180 * Export VFS cache effectiveness statistics to user-land.
182 * The statistics are left for aggregation to user-land so
183 * neat things can be achieved, like observing per-CPU cache
184 * distribution.
186 static int
187 sysctl_nchstats(SYSCTL_HANDLER_ARGS)
189 struct globaldata *gd;
190 int i, error;
192 error = 0;
193 for (i = 0; i < ncpus; ++i) {
194 gd = globaldata_find(i);
195 if ((error = SYSCTL_OUT(req, (void *)&(*gd->gd_nchstats),
196 sizeof(struct nchstats))))
197 break;
200 return (error);
202 SYSCTL_PROC(_vfs_cache, OID_AUTO, nchstats, CTLTYPE_OPAQUE|CTLFLAG_RD,
203 0, 0, sysctl_nchstats, "S,nchstats", "VFS cache effectiveness statistics");
205 static void cache_zap(struct namecache *ncp);
208 * cache_hold() and cache_drop() prevent the premature deletion of a
209 * namecache entry but do not prevent operations (such as zapping) on
210 * that namecache entry.
212 * This routine may only be called from outside this source module if
213 * nc_refs is already at least 1.
215 * This is a rare case where callers are allowed to hold a spinlock,
216 * so we can't ourselves.
218 static __inline
219 struct namecache *
220 _cache_hold(struct namecache *ncp)
222 atomic_add_int(&ncp->nc_refs, 1);
223 return(ncp);
227 * When dropping an entry, if only one ref remains and the entry has not
228 * been resolved, zap it. Since the one reference is being dropped the
229 * entry had better not be locked.
231 static __inline
232 void
233 _cache_drop(struct namecache *ncp)
235 KKASSERT(ncp->nc_refs > 0);
236 if (ncp->nc_refs == 1 &&
237 (ncp->nc_flag & NCF_UNRESOLVED) &&
238 TAILQ_EMPTY(&ncp->nc_list)
240 KKASSERT(ncp->nc_exlocks == 0);
241 _cache_lock(ncp);
242 cache_zap(ncp);
243 } else {
244 atomic_subtract_int(&ncp->nc_refs, 1);
249 * Link a new namecache entry to its parent. Be careful to avoid races
250 * if vhold() blocks in the future.
252 static void
253 cache_link_parent(struct namecache *ncp, struct namecache *par)
255 KKASSERT(ncp->nc_parent == NULL);
256 ncp->nc_parent = par;
257 if (TAILQ_EMPTY(&par->nc_list)) {
258 TAILQ_INSERT_HEAD(&par->nc_list, ncp, nc_entry);
260 * Any vp associated with an ncp which has children must
261 * be held to prevent it from being recycled.
263 if (par->nc_vp)
264 vhold(par->nc_vp);
265 } else {
266 TAILQ_INSERT_HEAD(&par->nc_list, ncp, nc_entry);
271 * Remove the parent association from a namecache structure. If this is
272 * the last child of the parent the cache_drop(par) will attempt to
273 * recursively zap the parent.
275 static void
276 cache_unlink_parent(struct namecache *ncp)
278 struct namecache *par;
280 if ((par = ncp->nc_parent) != NULL) {
281 ncp->nc_parent = NULL;
282 par = _cache_hold(par);
283 TAILQ_REMOVE(&par->nc_list, ncp, nc_entry);
284 if (par->nc_vp && TAILQ_EMPTY(&par->nc_list))
285 vdrop(par->nc_vp);
286 _cache_drop(par);
291 * Allocate a new namecache structure. Most of the code does not require
292 * zero-termination of the string but it makes vop_compat_ncreate() easier.
294 static struct namecache *
295 cache_alloc(int nlen)
297 struct namecache *ncp;
299 ncp = kmalloc(sizeof(*ncp), M_VFSCACHE, M_WAITOK|M_ZERO);
300 if (nlen)
301 ncp->nc_name = kmalloc(nlen + 1, M_VFSCACHE, M_WAITOK);
302 ncp->nc_nlen = nlen;
303 ncp->nc_flag = NCF_UNRESOLVED;
304 ncp->nc_error = ENOTCONN; /* needs to be resolved */
305 ncp->nc_refs = 1;
308 * Construct a fake FSMID based on the time of day and a 32 bit
309 * roller for uniqueness. This is used to generate a useful
310 * FSMID for filesystems which do not support it.
312 ncp->nc_fsmid = cache_getnewfsmid();
313 TAILQ_INIT(&ncp->nc_list);
314 _cache_lock(ncp);
315 return(ncp);
318 static void
319 _cache_free(struct namecache *ncp)
321 KKASSERT(ncp->nc_refs == 1 && ncp->nc_exlocks == 1);
322 if (ncp->nc_name)
323 kfree(ncp->nc_name, M_VFSCACHE);
324 kfree(ncp, M_VFSCACHE);
327 void
328 cache_zero(struct nchandle *nch)
330 nch->ncp = NULL;
331 nch->mount = NULL;
335 * Ref and deref a namecache structure.
337 * Warning: caller may hold an unrelated read spinlock, which means we can't
338 * use read spinlocks here.
340 struct nchandle *
341 cache_hold(struct nchandle *nch)
343 _cache_hold(nch->ncp);
344 ++nch->mount->mnt_refs;
345 return(nch);
348 void
349 cache_copy(struct nchandle *nch, struct nchandle *target)
351 *target = *nch;
352 _cache_hold(target->ncp);
353 ++nch->mount->mnt_refs;
356 void
357 cache_changemount(struct nchandle *nch, struct mount *mp)
359 --nch->mount->mnt_refs;
360 nch->mount = mp;
361 ++nch->mount->mnt_refs;
364 void
365 cache_drop(struct nchandle *nch)
367 --nch->mount->mnt_refs;
368 _cache_drop(nch->ncp);
369 nch->ncp = NULL;
370 nch->mount = NULL;
374 * Namespace locking. The caller must already hold a reference to the
375 * namecache structure in order to lock/unlock it. This function prevents
376 * the namespace from being created or destroyed by accessors other then
377 * the lock holder.
379 * Note that holding a locked namecache structure prevents other threads
380 * from making namespace changes (e.g. deleting or creating), prevents
381 * vnode association state changes by other threads, and prevents the
382 * namecache entry from being resolved or unresolved by other threads.
384 * The lock owner has full authority to associate/disassociate vnodes
385 * and resolve/unresolve the locked ncp.
387 * WARNING! Holding a locked ncp will prevent a vnode from being destroyed
388 * or recycled, but it does NOT help you if the vnode had already initiated
389 * a recyclement. If this is important, use cache_get() rather then
390 * cache_lock() (and deal with the differences in the way the refs counter
391 * is handled). Or, alternatively, make an unconditional call to
392 * cache_validate() or cache_resolve() after cache_lock() returns.
394 static
395 void
396 _cache_lock(struct namecache *ncp)
398 thread_t td;
399 int didwarn;
401 KKASSERT(ncp->nc_refs != 0);
402 didwarn = 0;
403 td = curthread;
405 for (;;) {
406 if (ncp->nc_exlocks == 0) {
407 ncp->nc_exlocks = 1;
408 ncp->nc_locktd = td;
410 * The vp associated with a locked ncp must be held
411 * to prevent it from being recycled (which would
412 * cause the ncp to become unresolved).
414 * WARNING! If VRECLAIMED is set the vnode could
415 * already be in the middle of a recycle. Callers
416 * should not assume that nc_vp is usable when
417 * not NULL. cache_vref() or cache_vget() must be
418 * called.
420 * XXX loop on race for later MPSAFE work.
422 if (ncp->nc_vp)
423 vhold(ncp->nc_vp);
424 break;
426 if (ncp->nc_locktd == td) {
427 ++ncp->nc_exlocks;
428 break;
430 ncp->nc_flag |= NCF_LOCKREQ;
431 if (tsleep(ncp, 0, "clock", nclockwarn) == EWOULDBLOCK) {
432 if (didwarn)
433 continue;
434 didwarn = 1;
435 kprintf("[diagnostic] cache_lock: blocked on %p", ncp);
436 kprintf(" \"%*.*s\"\n",
437 ncp->nc_nlen, ncp->nc_nlen, ncp->nc_name);
441 if (didwarn == 1) {
442 kprintf("[diagnostic] cache_lock: unblocked %*.*s\n",
443 ncp->nc_nlen, ncp->nc_nlen, ncp->nc_name);
447 void
448 cache_lock(struct nchandle *nch)
450 _cache_lock(nch->ncp);
453 static
455 _cache_lock_nonblock(struct namecache *ncp)
457 thread_t td;
459 KKASSERT(ncp->nc_refs != 0);
460 td = curthread;
461 if (ncp->nc_exlocks == 0) {
462 ncp->nc_exlocks = 1;
463 ncp->nc_locktd = td;
465 * The vp associated with a locked ncp must be held
466 * to prevent it from being recycled (which would
467 * cause the ncp to become unresolved).
469 * WARNING! If VRECLAIMED is set the vnode could
470 * already be in the middle of a recycle. Callers
471 * should not assume that nc_vp is usable when
472 * not NULL. cache_vref() or cache_vget() must be
473 * called.
475 * XXX loop on race for later MPSAFE work.
477 if (ncp->nc_vp)
478 vhold(ncp->nc_vp);
479 return(0);
480 } else {
481 return(EWOULDBLOCK);
486 cache_lock_nonblock(struct nchandle *nch)
488 return(_cache_lock_nonblock(nch->ncp));
491 static
492 void
493 _cache_unlock(struct namecache *ncp)
495 thread_t td = curthread;
497 KKASSERT(ncp->nc_refs > 0);
498 KKASSERT(ncp->nc_exlocks > 0);
499 KKASSERT(ncp->nc_locktd == td);
500 if (--ncp->nc_exlocks == 0) {
501 if (ncp->nc_vp)
502 vdrop(ncp->nc_vp);
503 ncp->nc_locktd = NULL;
504 if (ncp->nc_flag & NCF_LOCKREQ) {
505 ncp->nc_flag &= ~NCF_LOCKREQ;
506 wakeup(ncp);
511 void
512 cache_unlock(struct nchandle *nch)
514 _cache_unlock(nch->ncp);
518 * ref-and-lock, unlock-and-deref functions.
520 * This function is primarily used by nlookup. Even though cache_lock
521 * holds the vnode, it is possible that the vnode may have already
522 * initiated a recyclement. We want cache_get() to return a definitively
523 * usable vnode or a definitively unresolved ncp.
525 static
526 struct namecache *
527 _cache_get(struct namecache *ncp)
529 _cache_hold(ncp);
530 _cache_lock(ncp);
531 if (ncp->nc_vp && (ncp->nc_vp->v_flag & VRECLAIMED))
532 _cache_setunresolved(ncp);
533 return(ncp);
537 * note: the same nchandle can be passed for both arguments.
539 void
540 cache_get(struct nchandle *nch, struct nchandle *target)
542 target->mount = nch->mount;
543 target->ncp = _cache_get(nch->ncp);
544 ++target->mount->mnt_refs;
547 static int
548 _cache_get_nonblock(struct namecache *ncp)
550 /* XXX MP */
551 if (ncp->nc_exlocks == 0 || ncp->nc_locktd == curthread) {
552 _cache_hold(ncp);
553 _cache_lock(ncp);
554 if (ncp->nc_vp && (ncp->nc_vp->v_flag & VRECLAIMED))
555 _cache_setunresolved(ncp);
556 return(0);
558 return(EWOULDBLOCK);
562 cache_get_nonblock(struct nchandle *nch)
564 int error;
566 if ((error = _cache_get_nonblock(nch->ncp)) == 0)
567 ++nch->mount->mnt_refs;
568 return (error);
571 static __inline
572 void
573 _cache_put(struct namecache *ncp)
575 _cache_unlock(ncp);
576 _cache_drop(ncp);
579 void
580 cache_put(struct nchandle *nch)
582 --nch->mount->mnt_refs;
583 _cache_put(nch->ncp);
584 nch->ncp = NULL;
585 nch->mount = NULL;
589 * Resolve an unresolved ncp by associating a vnode with it. If the
590 * vnode is NULL, a negative cache entry is created.
592 * The ncp should be locked on entry and will remain locked on return.
594 static
595 void
596 _cache_setvp(struct namecache *ncp, struct vnode *vp)
598 KKASSERT(ncp->nc_flag & NCF_UNRESOLVED);
599 ncp->nc_vp = vp;
600 if (vp != NULL) {
602 * Any vp associated with an ncp which has children must
603 * be held. Any vp associated with a locked ncp must be held.
605 if (!TAILQ_EMPTY(&ncp->nc_list))
606 vhold(vp);
607 TAILQ_INSERT_HEAD(&vp->v_namecache, ncp, nc_vnode);
608 if (ncp->nc_exlocks)
609 vhold(vp);
612 * Set auxiliary flags
614 switch(vp->v_type) {
615 case VDIR:
616 ncp->nc_flag |= NCF_ISDIR;
617 break;
618 case VLNK:
619 ncp->nc_flag |= NCF_ISSYMLINK;
620 /* XXX cache the contents of the symlink */
621 break;
622 default:
623 break;
625 ++numcache;
626 ncp->nc_error = 0;
627 } else {
628 TAILQ_INSERT_TAIL(&ncneglist, ncp, nc_vnode);
629 ++numneg;
630 ncp->nc_error = ENOENT;
632 ncp->nc_flag &= ~NCF_UNRESOLVED;
635 void
636 cache_setvp(struct nchandle *nch, struct vnode *vp)
638 _cache_setvp(nch->ncp, vp);
641 void
642 cache_settimeout(struct nchandle *nch, int nticks)
644 struct namecache *ncp = nch->ncp;
646 if ((ncp->nc_timeout = ticks + nticks) == 0)
647 ncp->nc_timeout = 1;
651 * Disassociate the vnode or negative-cache association and mark a
652 * namecache entry as unresolved again. Note that the ncp is still
653 * left in the hash table and still linked to its parent.
655 * The ncp should be locked and refd on entry and will remain locked and refd
656 * on return.
658 * This routine is normally never called on a directory containing children.
659 * However, NFS often does just that in its rename() code as a cop-out to
660 * avoid complex namespace operations. This disconnects a directory vnode
661 * from its namecache and can cause the OLDAPI and NEWAPI to get out of
662 * sync.
664 * NOTE: NCF_FSMID must be cleared so a refurbishment of the ncp, such as
665 * in a create, properly propogates flag up the chain.
667 static
668 void
669 _cache_setunresolved(struct namecache *ncp)
671 struct vnode *vp;
673 if ((ncp->nc_flag & NCF_UNRESOLVED) == 0) {
674 ncp->nc_flag |= NCF_UNRESOLVED;
675 ncp->nc_timeout = 0;
676 ncp->nc_error = ENOTCONN;
677 ++numunres;
678 if ((vp = ncp->nc_vp) != NULL) {
679 --numcache;
680 ncp->nc_vp = NULL;
681 TAILQ_REMOVE(&vp->v_namecache, ncp, nc_vnode);
684 * Any vp associated with an ncp with children is
685 * held by that ncp. Any vp associated with a locked
686 * ncp is held by that ncp. These conditions must be
687 * undone when the vp is cleared out from the ncp.
689 if (ncp->nc_flag & NCF_FSMID)
690 vupdatefsmid(vp);
691 if (!TAILQ_EMPTY(&ncp->nc_list))
692 vdrop(vp);
693 if (ncp->nc_exlocks)
694 vdrop(vp);
695 } else {
696 TAILQ_REMOVE(&ncneglist, ncp, nc_vnode);
697 --numneg;
699 ncp->nc_flag &= ~(NCF_WHITEOUT|NCF_ISDIR|NCF_ISSYMLINK|
700 NCF_FSMID);
704 void
705 cache_setunresolved(struct nchandle *nch)
707 _cache_setunresolved(nch->ncp);
711 * Determine if we can clear NCF_ISMOUNTPT by scanning the mountlist
712 * looking for matches. This flag tells the lookup code when it must
713 * check for a mount linkage and also prevents the directories in question
714 * from being deleted or renamed.
716 static
718 cache_clrmountpt_callback(struct mount *mp, void *data)
720 struct nchandle *nch = data;
722 if (mp->mnt_ncmounton.ncp == nch->ncp)
723 return(1);
724 if (mp->mnt_ncmountpt.ncp == nch->ncp)
725 return(1);
726 return(0);
729 void
730 cache_clrmountpt(struct nchandle *nch)
732 int count;
734 count = mountlist_scan(cache_clrmountpt_callback, nch,
735 MNTSCAN_FORWARD|MNTSCAN_NOBUSY);
736 if (count == 0)
737 nch->ncp->nc_flag &= ~NCF_ISMOUNTPT;
741 * Invalidate portions of the namecache topology given a starting entry.
742 * The passed ncp is set to an unresolved state and:
744 * The passed ncp must be locked.
746 * CINV_DESTROY - Set a flag in the passed ncp entry indicating
747 * that the physical underlying nodes have been
748 * destroyed... as in deleted. For example, when
749 * a directory is removed. This will cause record
750 * lookups on the name to no longer be able to find
751 * the record and tells the resolver to return failure
752 * rather then trying to resolve through the parent.
754 * The topology itself, including ncp->nc_name,
755 * remains intact.
757 * This only applies to the passed ncp, if CINV_CHILDREN
758 * is specified the children are not flagged.
760 * CINV_CHILDREN - Set all children (recursively) to an unresolved
761 * state as well.
763 * Note that this will also have the side effect of
764 * cleaning out any unreferenced nodes in the topology
765 * from the leaves up as the recursion backs out.
767 * Note that the topology for any referenced nodes remains intact.
769 * It is possible for cache_inval() to race a cache_resolve(), meaning that
770 * the namecache entry may not actually be invalidated on return if it was
771 * revalidated while recursing down into its children. This code guarentees
772 * that the node(s) will go through an invalidation cycle, but does not
773 * guarentee that they will remain in an invalidated state.
775 * Returns non-zero if a revalidation was detected during the invalidation
776 * recursion, zero otherwise. Note that since only the original ncp is
777 * locked the revalidation ultimately can only indicate that the original ncp
778 * *MIGHT* no have been reresolved.
780 * DEEP RECURSION HANDLING - If a recursive invalidation recurses deeply we
781 * have to avoid blowing out the kernel stack. We do this by saving the
782 * deep namecache node and aborting the recursion, then re-recursing at that
783 * node using a depth-first algorithm in order to allow multiple deep
784 * recursions to chain through each other, then we restart the invalidation
785 * from scratch.
788 struct cinvtrack {
789 struct namecache *resume_ncp;
790 int depth;
793 static int _cache_inval_internal(struct namecache *, int, struct cinvtrack *);
795 static
797 _cache_inval(struct namecache *ncp, int flags)
799 struct cinvtrack track;
800 struct namecache *ncp2;
801 int r;
803 track.depth = 0;
804 track.resume_ncp = NULL;
806 for (;;) {
807 r = _cache_inval_internal(ncp, flags, &track);
808 if (track.resume_ncp == NULL)
809 break;
810 kprintf("Warning: deep namecache recursion at %s\n",
811 ncp->nc_name);
812 _cache_unlock(ncp);
813 while ((ncp2 = track.resume_ncp) != NULL) {
814 track.resume_ncp = NULL;
815 _cache_lock(ncp2);
816 _cache_inval_internal(ncp2, flags & ~CINV_DESTROY,
817 &track);
818 _cache_put(ncp2);
820 _cache_lock(ncp);
822 return(r);
826 cache_inval(struct nchandle *nch, int flags)
828 return(_cache_inval(nch->ncp, flags));
831 static int
832 _cache_inval_internal(struct namecache *ncp, int flags, struct cinvtrack *track)
834 struct namecache *kid;
835 struct namecache *nextkid;
836 int rcnt = 0;
838 KKASSERT(ncp->nc_exlocks);
840 _cache_setunresolved(ncp);
841 if (flags & CINV_DESTROY)
842 ncp->nc_flag |= NCF_DESTROYED;
844 if ((flags & CINV_CHILDREN) &&
845 (kid = TAILQ_FIRST(&ncp->nc_list)) != NULL
847 if (++track->depth > MAX_RECURSION_DEPTH) {
848 track->resume_ncp = ncp;
849 _cache_hold(ncp);
850 ++rcnt;
852 _cache_hold(kid);
853 _cache_unlock(ncp);
854 while (kid) {
855 if (track->resume_ncp) {
856 _cache_drop(kid);
857 break;
859 if ((nextkid = TAILQ_NEXT(kid, nc_entry)) != NULL)
860 _cache_hold(nextkid);
861 if ((kid->nc_flag & NCF_UNRESOLVED) == 0 ||
862 TAILQ_FIRST(&kid->nc_list)
864 _cache_lock(kid);
865 rcnt += _cache_inval_internal(kid, flags & ~CINV_DESTROY, track);
866 _cache_unlock(kid);
868 _cache_drop(kid);
869 kid = nextkid;
871 --track->depth;
872 _cache_lock(ncp);
876 * Someone could have gotten in there while ncp was unlocked,
877 * retry if so.
879 if ((ncp->nc_flag & NCF_UNRESOLVED) == 0)
880 ++rcnt;
881 return (rcnt);
885 * Invalidate a vnode's namecache associations. To avoid races against
886 * the resolver we do not invalidate a node which we previously invalidated
887 * but which was then re-resolved while we were in the invalidation loop.
889 * Returns non-zero if any namecache entries remain after the invalidation
890 * loop completed.
892 * NOTE: unlike the namecache topology which guarentees that ncp's will not
893 * be ripped out of the topology while held, the vnode's v_namecache list
894 * has no such restriction. NCP's can be ripped out of the list at virtually
895 * any time if not locked, even if held.
898 cache_inval_vp(struct vnode *vp, int flags)
900 struct namecache *ncp;
901 struct namecache *next;
903 restart:
904 ncp = TAILQ_FIRST(&vp->v_namecache);
905 if (ncp)
906 _cache_hold(ncp);
907 while (ncp) {
908 /* loop entered with ncp held */
909 if ((next = TAILQ_NEXT(ncp, nc_vnode)) != NULL)
910 _cache_hold(next);
911 _cache_lock(ncp);
912 if (ncp->nc_vp != vp) {
913 kprintf("Warning: cache_inval_vp: race-A detected on "
914 "%s\n", ncp->nc_name);
915 _cache_put(ncp);
916 if (next)
917 _cache_drop(next);
918 goto restart;
920 _cache_inval(ncp, flags);
921 _cache_put(ncp); /* also releases reference */
922 ncp = next;
923 if (ncp && ncp->nc_vp != vp) {
924 kprintf("Warning: cache_inval_vp: race-B detected on "
925 "%s\n", ncp->nc_name);
926 _cache_drop(ncp);
927 goto restart;
930 return(TAILQ_FIRST(&vp->v_namecache) != NULL);
934 * This routine is used instead of the normal cache_inval_vp() when we
935 * are trying to recycle otherwise good vnodes.
937 * Return 0 on success, non-zero if not all namecache records could be
938 * disassociated from the vnode (for various reasons).
941 cache_inval_vp_nonblock(struct vnode *vp)
943 struct namecache *ncp;
944 struct namecache *next;
946 ncp = TAILQ_FIRST(&vp->v_namecache);
947 if (ncp)
948 _cache_hold(ncp);
949 while (ncp) {
950 /* loop entered with ncp held */
951 if ((next = TAILQ_NEXT(ncp, nc_vnode)) != NULL)
952 _cache_hold(next);
953 if (_cache_lock_nonblock(ncp)) {
954 _cache_drop(ncp);
955 if (next)
956 _cache_drop(next);
957 break;
959 if (ncp->nc_vp != vp) {
960 kprintf("Warning: cache_inval_vp: race-A detected on "
961 "%s\n", ncp->nc_name);
962 _cache_put(ncp);
963 if (next)
964 _cache_drop(next);
965 break;
967 _cache_inval(ncp, 0);
968 _cache_put(ncp); /* also releases reference */
969 ncp = next;
970 if (ncp && ncp->nc_vp != vp) {
971 kprintf("Warning: cache_inval_vp: race-B detected on "
972 "%s\n", ncp->nc_name);
973 _cache_drop(ncp);
974 break;
977 return(TAILQ_FIRST(&vp->v_namecache) != NULL);
981 * The source ncp has been renamed to the target ncp. Both fncp and tncp
982 * must be locked. The target ncp is destroyed (as a normal rename-over
983 * would destroy the target file or directory).
985 * Because there may be references to the source ncp we cannot copy its
986 * contents to the target. Instead the source ncp is relinked as the target
987 * and the target ncp is removed from the namecache topology.
989 void
990 cache_rename(struct nchandle *fnch, struct nchandle *tnch)
992 struct namecache *fncp = fnch->ncp;
993 struct namecache *tncp = tnch->ncp;
994 char *oname;
996 _cache_setunresolved(tncp);
997 cache_unlink_parent(fncp);
998 cache_link_parent(fncp, tncp->nc_parent);
999 cache_unlink_parent(tncp);
1000 oname = fncp->nc_name;
1001 fncp->nc_name = tncp->nc_name;
1002 fncp->nc_nlen = tncp->nc_nlen;
1003 tncp->nc_name = NULL;
1004 tncp->nc_nlen = 0;
1005 if (fncp->nc_flag & NCF_HASHED)
1006 _cache_rehash(fncp);
1007 if (tncp->nc_flag & NCF_HASHED)
1008 _cache_rehash(tncp);
1009 if (oname)
1010 kfree(oname, M_VFSCACHE);
1014 * vget the vnode associated with the namecache entry. Resolve the namecache
1015 * entry if necessary and deal with namecache/vp races. The passed ncp must
1016 * be referenced and may be locked. The ncp's ref/locking state is not
1017 * effected by this call.
1019 * lk_type may be LK_SHARED, LK_EXCLUSIVE. A ref'd, possibly locked
1020 * (depending on the passed lk_type) will be returned in *vpp with an error
1021 * of 0, or NULL will be returned in *vpp with a non-0 error code. The
1022 * most typical error is ENOENT, meaning that the ncp represents a negative
1023 * cache hit and there is no vnode to retrieve, but other errors can occur
1024 * too.
1026 * The main race we have to deal with are namecache zaps. The ncp itself
1027 * will not disappear since it is referenced, and it turns out that the
1028 * validity of the vp pointer can be checked simply by rechecking the
1029 * contents of ncp->nc_vp.
1032 cache_vget(struct nchandle *nch, struct ucred *cred,
1033 int lk_type, struct vnode **vpp)
1035 struct namecache *ncp;
1036 struct vnode *vp;
1037 int error;
1039 ncp = nch->ncp;
1040 again:
1041 vp = NULL;
1042 if (ncp->nc_flag & NCF_UNRESOLVED) {
1043 _cache_lock(ncp);
1044 error = cache_resolve(nch, cred);
1045 _cache_unlock(ncp);
1046 } else {
1047 error = 0;
1049 if (error == 0 && (vp = ncp->nc_vp) != NULL) {
1051 * Accessing the vnode from the namecache is a bit
1052 * dangerous. Because there are no refs on the vnode, it
1053 * could be in the middle of a reclaim.
1055 if (vp->v_flag & VRECLAIMED) {
1056 kprintf("Warning: vnode reclaim race detected in cache_vget on %p (%s)\n", vp, ncp->nc_name);
1057 _cache_lock(ncp);
1058 _cache_setunresolved(ncp);
1059 _cache_unlock(ncp);
1060 goto again;
1062 error = vget(vp, lk_type);
1063 if (error) {
1064 if (vp != ncp->nc_vp)
1065 goto again;
1066 vp = NULL;
1067 } else if (vp != ncp->nc_vp) {
1068 vput(vp);
1069 goto again;
1070 } else if (vp->v_flag & VRECLAIMED) {
1071 panic("vget succeeded on a VRECLAIMED node! vp %p", vp);
1074 if (error == 0 && vp == NULL)
1075 error = ENOENT;
1076 *vpp = vp;
1077 return(error);
1081 cache_vref(struct nchandle *nch, struct ucred *cred, struct vnode **vpp)
1083 struct namecache *ncp;
1084 struct vnode *vp;
1085 int error;
1087 ncp = nch->ncp;
1089 again:
1090 vp = NULL;
1091 if (ncp->nc_flag & NCF_UNRESOLVED) {
1092 _cache_lock(ncp);
1093 error = cache_resolve(nch, cred);
1094 _cache_unlock(ncp);
1095 } else {
1096 error = 0;
1098 if (error == 0 && (vp = ncp->nc_vp) != NULL) {
1100 * Since we did not obtain any locks, a cache zap
1101 * race can occur here if the vnode is in the middle
1102 * of being reclaimed and has not yet been able to
1103 * clean out its cache node. If that case occurs,
1104 * we must lock and unresolve the cache, then loop
1105 * to retry.
1107 if ((error = vget(vp, LK_SHARED)) != 0) {
1108 if (error == ENOENT) {
1109 kprintf("Warning: vnode reclaim race detected on cache_vref %p (%s)\n", vp, ncp->nc_name);
1110 _cache_lock(ncp);
1111 _cache_setunresolved(ncp);
1112 _cache_unlock(ncp);
1113 goto again;
1115 /* fatal error */
1116 } else {
1117 /* caller does not want a lock */
1118 vn_unlock(vp);
1121 if (error == 0 && vp == NULL)
1122 error = ENOENT;
1123 *vpp = vp;
1124 return(error);
1128 * Recursively set the FSMID update flag for namecache nodes leading
1129 * to root. This will cause the next getattr or reclaim to increment the
1130 * fsmid and mark the inode for lazy updating.
1132 * Stop recursing when we hit a node whos NCF_FSMID flag is already set.
1133 * This makes FSMIDs work in an Einsteinian fashion - where the observation
1134 * effects the result. In this case a program monitoring a higher level
1135 * node will have detected some prior change and started its scan (clearing
1136 * NCF_FSMID in higher level nodes), but since it has not yet observed the
1137 * node where we find NCF_FSMID still set, we can safely make the related
1138 * modification without interfering with the theorized program.
1140 * This also means that FSMIDs cannot represent time-domain quantities
1141 * in a hierarchical sense. But the main reason for doing it this way
1142 * is to reduce the amount of recursion that occurs in the critical path
1143 * when e.g. a program is writing to a file that sits deep in a directory
1144 * hierarchy.
1146 void
1147 cache_update_fsmid(struct nchandle *nch)
1149 struct namecache *ncp;
1150 struct namecache *scan;
1151 struct vnode *vp;
1153 ncp = nch->ncp;
1156 * Warning: even if we get a non-NULL vp it could still be in the
1157 * middle of a recyclement. Don't do anything fancy, just set
1158 * NCF_FSMID.
1160 if ((vp = ncp->nc_vp) != NULL) {
1161 TAILQ_FOREACH(ncp, &vp->v_namecache, nc_vnode) {
1162 for (scan = ncp; scan; scan = scan->nc_parent) {
1163 if (scan->nc_flag & NCF_FSMID)
1164 break;
1165 scan->nc_flag |= NCF_FSMID;
1168 } else {
1169 while (ncp && (ncp->nc_flag & NCF_FSMID) == 0) {
1170 ncp->nc_flag |= NCF_FSMID;
1171 ncp = ncp->nc_parent;
1176 void
1177 cache_update_fsmid_vp(struct vnode *vp)
1179 struct namecache *ncp;
1180 struct namecache *scan;
1182 TAILQ_FOREACH(ncp, &vp->v_namecache, nc_vnode) {
1183 for (scan = ncp; scan; scan = scan->nc_parent) {
1184 if (scan->nc_flag & NCF_FSMID)
1185 break;
1186 scan->nc_flag |= NCF_FSMID;
1192 * If getattr is called on a vnode (e.g. a stat call), the filesystem
1193 * may call this routine to determine if the namecache has the hierarchical
1194 * change flag set, requiring the fsmid to be updated.
1196 * Since 0 indicates no support, make sure the filesystem fsmid is at least
1197 * 1.
1200 cache_check_fsmid_vp(struct vnode *vp, int64_t *fsmid)
1202 struct namecache *ncp;
1203 int changed = 0;
1205 TAILQ_FOREACH(ncp, &vp->v_namecache, nc_vnode) {
1206 if (ncp->nc_flag & NCF_FSMID) {
1207 ncp->nc_flag &= ~NCF_FSMID;
1208 changed = 1;
1211 if (*fsmid == 0)
1212 ++*fsmid;
1213 if (changed)
1214 ++*fsmid;
1215 return(changed);
1219 * Obtain the FSMID for a vnode for filesystems which do not support
1220 * a built-in FSMID.
1222 int64_t
1223 cache_sync_fsmid_vp(struct vnode *vp)
1225 struct namecache *ncp;
1227 if ((ncp = TAILQ_FIRST(&vp->v_namecache)) != NULL) {
1228 if (ncp->nc_flag & NCF_FSMID) {
1229 ncp->nc_flag &= ~NCF_FSMID;
1230 ++ncp->nc_fsmid;
1232 return(ncp->nc_fsmid);
1234 return(VNOVAL);
1238 * Convert a directory vnode to a namecache record without any other
1239 * knowledge of the topology. This ONLY works with directory vnodes and
1240 * is ONLY used by the NFS server. dvp must be refd but unlocked, and the
1241 * returned ncp (if not NULL) will be held and unlocked.
1243 * If 'makeit' is 0 and dvp has no existing namecache record, NULL is returned.
1244 * If 'makeit' is 1 we attempt to track-down and create the namecache topology
1245 * for dvp. This will fail only if the directory has been deleted out from
1246 * under the caller.
1248 * Callers must always check for a NULL return no matter the value of 'makeit'.
1250 * To avoid underflowing the kernel stack each recursive call increments
1251 * the makeit variable.
1254 static int cache_inefficient_scan(struct nchandle *nch, struct ucred *cred,
1255 struct vnode *dvp, char *fakename);
1256 static int cache_fromdvp_try(struct vnode *dvp, struct ucred *cred,
1257 struct vnode **saved_dvp);
1260 cache_fromdvp(struct vnode *dvp, struct ucred *cred, int makeit,
1261 struct nchandle *nch)
1263 struct vnode *saved_dvp;
1264 struct vnode *pvp;
1265 char *fakename;
1266 int error;
1268 nch->ncp = NULL;
1269 nch->mount = dvp->v_mount;
1270 saved_dvp = NULL;
1271 fakename = NULL;
1274 * Temporary debugging code to force the directory scanning code
1275 * to be exercised.
1277 if (ncvp_debug >= 3 && makeit && TAILQ_FIRST(&dvp->v_namecache)) {
1278 nch->ncp = TAILQ_FIRST(&dvp->v_namecache);
1279 kprintf("cache_fromdvp: forcing %s\n", nch->ncp->nc_name);
1280 goto force;
1284 * Loop until resolution, inside code will break out on error.
1286 while ((nch->ncp = TAILQ_FIRST(&dvp->v_namecache)) == NULL && makeit) {
1287 force:
1289 * If dvp is the root of its filesystem it should already
1290 * have a namecache pointer associated with it as a side
1291 * effect of the mount, but it may have been disassociated.
1293 if (dvp->v_flag & VROOT) {
1294 nch->ncp = _cache_get(nch->mount->mnt_ncmountpt.ncp);
1295 error = cache_resolve_mp(nch->mount);
1296 _cache_put(nch->ncp);
1297 if (ncvp_debug) {
1298 kprintf("cache_fromdvp: resolve root of mount %p error %d",
1299 dvp->v_mount, error);
1301 if (error) {
1302 if (ncvp_debug)
1303 kprintf(" failed\n");
1304 nch->ncp = NULL;
1305 break;
1307 if (ncvp_debug)
1308 kprintf(" succeeded\n");
1309 continue;
1313 * If we are recursed too deeply resort to an O(n^2)
1314 * algorithm to resolve the namecache topology. The
1315 * resolved pvp is left referenced in saved_dvp to
1316 * prevent the tree from being destroyed while we loop.
1318 if (makeit > 20) {
1319 error = cache_fromdvp_try(dvp, cred, &saved_dvp);
1320 if (error) {
1321 kprintf("lookupdotdot(longpath) failed %d "
1322 "dvp %p\n", error, dvp);
1323 nch->ncp = NULL;
1324 break;
1326 continue;
1330 * Get the parent directory and resolve its ncp.
1332 if (fakename) {
1333 kfree(fakename, M_TEMP);
1334 fakename = NULL;
1336 error = vop_nlookupdotdot(*dvp->v_ops, dvp, &pvp, cred,
1337 &fakename);
1338 if (error) {
1339 kprintf("lookupdotdot failed %d dvp %p\n", error, dvp);
1340 break;
1342 vn_unlock(pvp);
1345 * Reuse makeit as a recursion depth counter. On success
1346 * nch will be fully referenced.
1348 cache_fromdvp(pvp, cred, makeit + 1, nch);
1349 vrele(pvp);
1350 if (nch->ncp == NULL)
1351 break;
1354 * Do an inefficient scan of pvp (embodied by ncp) to look
1355 * for dvp. This will create a namecache record for dvp on
1356 * success. We loop up to recheck on success.
1358 * ncp and dvp are both held but not locked.
1360 error = cache_inefficient_scan(nch, cred, dvp, fakename);
1361 if (error) {
1362 kprintf("cache_fromdvp: scan %p (%s) failed on dvp=%p\n",
1363 pvp, nch->ncp->nc_name, dvp);
1364 cache_drop(nch);
1365 /* nch was NULLed out, reload mount */
1366 nch->mount = dvp->v_mount;
1367 break;
1369 if (ncvp_debug) {
1370 kprintf("cache_fromdvp: scan %p (%s) succeeded\n",
1371 pvp, nch->ncp->nc_name);
1373 cache_drop(nch);
1374 /* nch was NULLed out, reload mount */
1375 nch->mount = dvp->v_mount;
1378 if (fakename)
1379 kfree(fakename, M_TEMP);
1382 * hold it for real so the mount gets a ref
1384 if (nch->ncp)
1385 cache_hold(nch);
1386 if (saved_dvp)
1387 vrele(saved_dvp);
1388 if (nch->ncp)
1389 return (0);
1390 return (EINVAL);
1394 * Go up the chain of parent directories until we find something
1395 * we can resolve into the namecache. This is very inefficient.
1397 static
1399 cache_fromdvp_try(struct vnode *dvp, struct ucred *cred,
1400 struct vnode **saved_dvp)
1402 struct nchandle nch;
1403 struct vnode *pvp;
1404 int error;
1405 static time_t last_fromdvp_report;
1406 char *fakename;
1409 * Loop getting the parent directory vnode until we get something we
1410 * can resolve in the namecache.
1412 vref(dvp);
1413 nch.mount = dvp->v_mount;
1414 nch.ncp = NULL;
1415 fakename = NULL;
1417 for (;;) {
1418 if (fakename) {
1419 kfree(fakename, M_TEMP);
1420 fakename = NULL;
1422 error = vop_nlookupdotdot(*dvp->v_ops, dvp, &pvp, cred,
1423 &fakename);
1424 if (error) {
1425 vrele(dvp);
1426 break;
1428 vn_unlock(pvp);
1429 if ((nch.ncp = TAILQ_FIRST(&pvp->v_namecache)) != NULL) {
1430 _cache_hold(nch.ncp);
1431 vrele(pvp);
1432 break;
1434 if (pvp->v_flag & VROOT) {
1435 nch.ncp = _cache_get(pvp->v_mount->mnt_ncmountpt.ncp);
1436 error = cache_resolve_mp(nch.mount);
1437 _cache_unlock(nch.ncp);
1438 vrele(pvp);
1439 if (error) {
1440 _cache_drop(nch.ncp);
1441 nch.ncp = NULL;
1442 vrele(dvp);
1444 break;
1446 vrele(dvp);
1447 dvp = pvp;
1449 if (error == 0) {
1450 if (last_fromdvp_report != time_second) {
1451 last_fromdvp_report = time_second;
1452 kprintf("Warning: extremely inefficient path "
1453 "resolution on %s\n",
1454 nch.ncp->nc_name);
1456 error = cache_inefficient_scan(&nch, cred, dvp, fakename);
1459 * Hopefully dvp now has a namecache record associated with
1460 * it. Leave it referenced to prevent the kernel from
1461 * recycling the vnode. Otherwise extremely long directory
1462 * paths could result in endless recycling.
1464 if (*saved_dvp)
1465 vrele(*saved_dvp);
1466 *saved_dvp = dvp;
1467 _cache_drop(nch.ncp);
1469 if (fakename)
1470 kfree(fakename, M_TEMP);
1471 return (error);
1475 * Do an inefficient scan of the directory represented by ncp looking for
1476 * the directory vnode dvp. ncp must be held but not locked on entry and
1477 * will be held on return. dvp must be refd but not locked on entry and
1478 * will remain refd on return.
1480 * Why do this at all? Well, due to its stateless nature the NFS server
1481 * converts file handles directly to vnodes without necessarily going through
1482 * the namecache ops that would otherwise create the namecache topology
1483 * leading to the vnode. We could either (1) Change the namecache algorithms
1484 * to allow disconnect namecache records that are re-merged opportunistically,
1485 * or (2) Make the NFS server backtrack and scan to recover a connected
1486 * namecache topology in order to then be able to issue new API lookups.
1488 * It turns out that (1) is a huge mess. It takes a nice clean set of
1489 * namecache algorithms and introduces a lot of complication in every subsystem
1490 * that calls into the namecache to deal with the re-merge case, especially
1491 * since we are using the namecache to placehold negative lookups and the
1492 * vnode might not be immediately assigned. (2) is certainly far less
1493 * efficient then (1), but since we are only talking about directories here
1494 * (which are likely to remain cached), the case does not actually run all
1495 * that often and has the supreme advantage of not polluting the namecache
1496 * algorithms.
1498 * If a fakename is supplied just construct a namecache entry using the
1499 * fake name.
1501 static int
1502 cache_inefficient_scan(struct nchandle *nch, struct ucred *cred,
1503 struct vnode *dvp, char *fakename)
1505 struct nlcomponent nlc;
1506 struct nchandle rncp;
1507 struct dirent *den;
1508 struct vnode *pvp;
1509 struct vattr vat;
1510 struct iovec iov;
1511 struct uio uio;
1512 int blksize;
1513 int eofflag;
1514 int bytes;
1515 char *rbuf;
1516 int error;
1518 vat.va_blocksize = 0;
1519 if ((error = VOP_GETATTR(dvp, &vat)) != 0)
1520 return (error);
1521 if ((error = cache_vref(nch, cred, &pvp)) != 0)
1522 return (error);
1523 if (ncvp_debug)
1524 kprintf("inefficient_scan: directory iosize %ld vattr fileid = %lld\n", vat.va_blocksize, vat.va_fileid);
1527 * Use the supplied fakename if not NULL. Fake names are typically
1528 * not in the actual filesystem hierarchy. This is used by HAMMER
1529 * to glue @@timestamp recursions together.
1531 if (fakename) {
1532 nlc.nlc_nameptr = fakename;
1533 nlc.nlc_namelen = strlen(fakename);
1534 rncp = cache_nlookup(nch, &nlc);
1535 goto done;
1538 if ((blksize = vat.va_blocksize) == 0)
1539 blksize = DEV_BSIZE;
1540 rbuf = kmalloc(blksize, M_TEMP, M_WAITOK);
1541 rncp.ncp = NULL;
1543 eofflag = 0;
1544 uio.uio_offset = 0;
1545 again:
1546 iov.iov_base = rbuf;
1547 iov.iov_len = blksize;
1548 uio.uio_iov = &iov;
1549 uio.uio_iovcnt = 1;
1550 uio.uio_resid = blksize;
1551 uio.uio_segflg = UIO_SYSSPACE;
1552 uio.uio_rw = UIO_READ;
1553 uio.uio_td = curthread;
1555 if (ncvp_debug >= 2)
1556 kprintf("cache_inefficient_scan: readdir @ %08x\n", (int)uio.uio_offset);
1557 error = VOP_READDIR(pvp, &uio, cred, &eofflag, NULL, NULL);
1558 if (error == 0) {
1559 den = (struct dirent *)rbuf;
1560 bytes = blksize - uio.uio_resid;
1562 while (bytes > 0) {
1563 if (ncvp_debug >= 2) {
1564 kprintf("cache_inefficient_scan: %*.*s\n",
1565 den->d_namlen, den->d_namlen,
1566 den->d_name);
1568 if (den->d_type != DT_WHT &&
1569 den->d_ino == vat.va_fileid) {
1570 if (ncvp_debug) {
1571 kprintf("cache_inefficient_scan: "
1572 "MATCHED inode %lld path %s/%*.*s\n",
1573 vat.va_fileid, nch->ncp->nc_name,
1574 den->d_namlen, den->d_namlen,
1575 den->d_name);
1577 nlc.nlc_nameptr = den->d_name;
1578 nlc.nlc_namelen = den->d_namlen;
1579 rncp = cache_nlookup(nch, &nlc);
1580 KKASSERT(rncp.ncp != NULL);
1581 break;
1583 bytes -= _DIRENT_DIRSIZ(den);
1584 den = _DIRENT_NEXT(den);
1586 if (rncp.ncp == NULL && eofflag == 0 && uio.uio_resid != blksize)
1587 goto again;
1589 kfree(rbuf, M_TEMP);
1590 done:
1591 vrele(pvp);
1592 if (rncp.ncp) {
1593 if (rncp.ncp->nc_flag & NCF_UNRESOLVED) {
1594 _cache_setvp(rncp.ncp, dvp);
1595 if (ncvp_debug >= 2) {
1596 kprintf("cache_inefficient_scan: setvp %s/%s = %p\n",
1597 nch->ncp->nc_name, rncp.ncp->nc_name, dvp);
1599 } else {
1600 if (ncvp_debug >= 2) {
1601 kprintf("cache_inefficient_scan: setvp %s/%s already set %p/%p\n",
1602 nch->ncp->nc_name, rncp.ncp->nc_name, dvp,
1603 rncp.ncp->nc_vp);
1606 if (rncp.ncp->nc_vp == NULL)
1607 error = rncp.ncp->nc_error;
1609 * Release rncp after a successful nlookup. rncp was fully
1610 * referenced.
1612 cache_put(&rncp);
1613 } else {
1614 kprintf("cache_inefficient_scan: dvp %p NOT FOUND in %s\n",
1615 dvp, nch->ncp->nc_name);
1616 error = ENOENT;
1618 return (error);
1622 * Zap a namecache entry. The ncp is unconditionally set to an unresolved
1623 * state, which disassociates it from its vnode or ncneglist.
1625 * Then, if there are no additional references to the ncp and no children,
1626 * the ncp is removed from the topology and destroyed. This function will
1627 * also run through the nc_parent chain and destroy parent ncps if possible.
1628 * As a side benefit, it turns out the only conditions that allow running
1629 * up the chain are also the conditions to ensure no deadlock will occur.
1631 * References and/or children may exist if the ncp is in the middle of the
1632 * topology, preventing the ncp from being destroyed.
1634 * This function must be called with the ncp held and locked and will unlock
1635 * and drop it during zapping.
1637 static void
1638 cache_zap(struct namecache *ncp)
1640 struct namecache *par;
1643 * Disassociate the vnode or negative cache ref and set NCF_UNRESOLVED.
1645 _cache_setunresolved(ncp);
1648 * Try to scrap the entry and possibly tail-recurse on its parent.
1649 * We only scrap unref'd (other then our ref) unresolved entries,
1650 * we do not scrap 'live' entries.
1652 while (ncp->nc_flag & NCF_UNRESOLVED) {
1654 * Someone other then us has a ref, stop.
1656 if (ncp->nc_refs > 1)
1657 goto done;
1660 * We have children, stop.
1662 if (!TAILQ_EMPTY(&ncp->nc_list))
1663 goto done;
1666 * Remove ncp from the topology: hash table and parent linkage.
1668 if (ncp->nc_flag & NCF_HASHED) {
1669 ncp->nc_flag &= ~NCF_HASHED;
1670 LIST_REMOVE(ncp, nc_hash);
1672 if ((par = ncp->nc_parent) != NULL) {
1673 par = _cache_hold(par);
1674 TAILQ_REMOVE(&par->nc_list, ncp, nc_entry);
1675 ncp->nc_parent = NULL;
1676 if (par->nc_vp && TAILQ_EMPTY(&par->nc_list))
1677 vdrop(par->nc_vp);
1681 * ncp should not have picked up any refs. Physically
1682 * destroy the ncp.
1684 KKASSERT(ncp->nc_refs == 1);
1685 --numunres;
1686 /* _cache_unlock(ncp) not required */
1687 ncp->nc_refs = -1; /* safety */
1688 if (ncp->nc_name)
1689 kfree(ncp->nc_name, M_VFSCACHE);
1690 kfree(ncp, M_VFSCACHE);
1693 * Loop on the parent (it may be NULL). Only bother looping
1694 * if the parent has a single ref (ours), which also means
1695 * we can lock it trivially.
1697 ncp = par;
1698 if (ncp == NULL)
1699 return;
1700 if (ncp->nc_refs != 1) {
1701 _cache_drop(ncp);
1702 return;
1704 KKASSERT(par->nc_exlocks == 0);
1705 _cache_lock(ncp);
1707 done:
1708 _cache_unlock(ncp);
1709 atomic_subtract_int(&ncp->nc_refs, 1);
1712 static enum { CHI_LOW, CHI_HIGH } cache_hysteresis_state = CHI_LOW;
1714 static __inline
1715 void
1716 cache_hysteresis(void)
1719 * Don't cache too many negative hits. We use hysteresis to reduce
1720 * the impact on the critical path.
1722 switch(cache_hysteresis_state) {
1723 case CHI_LOW:
1724 if (numneg > MINNEG && numneg * ncnegfactor > numcache) {
1725 cache_cleanneg(10);
1726 cache_hysteresis_state = CHI_HIGH;
1728 break;
1729 case CHI_HIGH:
1730 if (numneg > MINNEG * 9 / 10 &&
1731 numneg * ncnegfactor * 9 / 10 > numcache
1733 cache_cleanneg(10);
1734 } else {
1735 cache_hysteresis_state = CHI_LOW;
1737 break;
1742 * NEW NAMECACHE LOOKUP API
1744 * Lookup an entry in the cache. A locked, referenced, non-NULL
1745 * entry is *always* returned, even if the supplied component is illegal.
1746 * The resulting namecache entry should be returned to the system with
1747 * cache_put() or _cache_unlock() + cache_drop().
1749 * namecache locks are recursive but care must be taken to avoid lock order
1750 * reversals.
1752 * Nobody else will be able to manipulate the associated namespace (e.g.
1753 * create, delete, rename, rename-target) until the caller unlocks the
1754 * entry.
1756 * The returned entry will be in one of three states: positive hit (non-null
1757 * vnode), negative hit (null vnode), or unresolved (NCF_UNRESOLVED is set).
1758 * Unresolved entries must be resolved through the filesystem to associate the
1759 * vnode and/or determine whether a positive or negative hit has occured.
1761 * It is not necessary to lock a directory in order to lock namespace under
1762 * that directory. In fact, it is explicitly not allowed to do that. A
1763 * directory is typically only locked when being created, renamed, or
1764 * destroyed.
1766 * The directory (par) may be unresolved, in which case any returned child
1767 * will likely also be marked unresolved. Likely but not guarenteed. Since
1768 * the filesystem lookup requires a resolved directory vnode the caller is
1769 * responsible for resolving the namecache chain top-down. This API
1770 * specifically allows whole chains to be created in an unresolved state.
1772 struct nchandle
1773 cache_nlookup(struct nchandle *par_nch, struct nlcomponent *nlc)
1775 struct nchandle nch;
1776 struct namecache *ncp;
1777 struct namecache *new_ncp;
1778 struct nchashhead *nchpp;
1779 u_int32_t hash;
1780 globaldata_t gd;
1782 numcalls++;
1783 gd = mycpu;
1786 * Try to locate an existing entry
1788 hash = fnv_32_buf(nlc->nlc_nameptr, nlc->nlc_namelen, FNV1_32_INIT);
1789 hash = fnv_32_buf(&par_nch->ncp, sizeof(par_nch->ncp), hash);
1790 new_ncp = NULL;
1791 restart:
1792 LIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
1793 numchecks++;
1796 * Try to zap entries that have timed out. We have
1797 * to be careful here because locked leafs may depend
1798 * on the vnode remaining intact in a parent, so only
1799 * do this under very specific conditions.
1801 if (ncp->nc_timeout &&
1802 (int)(ncp->nc_timeout - ticks) < 0 &&
1803 (ncp->nc_flag & NCF_UNRESOLVED) == 0 &&
1804 ncp->nc_exlocks == 0 &&
1805 TAILQ_EMPTY(&ncp->nc_list)
1807 cache_zap(_cache_get(ncp));
1808 goto restart;
1812 * Break out if we find a matching entry. Note that
1813 * UNRESOLVED entries may match, but DESTROYED entries
1814 * do not.
1816 if (ncp->nc_parent == par_nch->ncp &&
1817 ncp->nc_nlen == nlc->nlc_namelen &&
1818 bcmp(ncp->nc_name, nlc->nlc_nameptr, ncp->nc_nlen) == 0 &&
1819 (ncp->nc_flag & NCF_DESTROYED) == 0
1821 if (_cache_get_nonblock(ncp) == 0) {
1822 if (new_ncp)
1823 _cache_free(new_ncp);
1824 goto found;
1826 _cache_get(ncp);
1827 _cache_put(ncp);
1828 goto restart;
1833 * We failed to locate an entry, create a new entry and add it to
1834 * the cache. We have to relookup after possibly blocking in
1835 * malloc.
1837 if (new_ncp == NULL) {
1838 new_ncp = cache_alloc(nlc->nlc_namelen);
1839 goto restart;
1842 ncp = new_ncp;
1845 * Initialize as a new UNRESOLVED entry, lock (non-blocking),
1846 * and link to the parent. The mount point is usually inherited
1847 * from the parent unless this is a special case such as a mount
1848 * point where nlc_namelen is 0. If nlc_namelen is 0 nc_name will
1849 * be NULL.
1851 if (nlc->nlc_namelen) {
1852 bcopy(nlc->nlc_nameptr, ncp->nc_name, nlc->nlc_namelen);
1853 ncp->nc_name[nlc->nlc_namelen] = 0;
1855 nchpp = NCHHASH(hash);
1856 LIST_INSERT_HEAD(nchpp, ncp, nc_hash);
1857 ncp->nc_flag |= NCF_HASHED;
1858 cache_link_parent(ncp, par_nch->ncp);
1859 found:
1861 * stats and namecache size management
1863 if (ncp->nc_flag & NCF_UNRESOLVED)
1864 ++gd->gd_nchstats->ncs_miss;
1865 else if (ncp->nc_vp)
1866 ++gd->gd_nchstats->ncs_goodhits;
1867 else
1868 ++gd->gd_nchstats->ncs_neghits;
1869 cache_hysteresis();
1870 nch.mount = par_nch->mount;
1871 nch.ncp = ncp;
1872 ++nch.mount->mnt_refs;
1873 return(nch);
1877 * The namecache entry is marked as being used as a mount point.
1878 * Locate the mount if it is visible to the caller.
1880 struct findmount_info {
1881 struct mount *result;
1882 struct mount *nch_mount;
1883 struct namecache *nch_ncp;
1886 static
1888 cache_findmount_callback(struct mount *mp, void *data)
1890 struct findmount_info *info = data;
1893 * Check the mount's mounted-on point against the passed nch.
1895 if (mp->mnt_ncmounton.mount == info->nch_mount &&
1896 mp->mnt_ncmounton.ncp == info->nch_ncp
1898 info->result = mp;
1899 return(-1);
1901 return(0);
1904 struct mount *
1905 cache_findmount(struct nchandle *nch)
1907 struct findmount_info info;
1909 info.result = NULL;
1910 info.nch_mount = nch->mount;
1911 info.nch_ncp = nch->ncp;
1912 mountlist_scan(cache_findmount_callback, &info,
1913 MNTSCAN_FORWARD|MNTSCAN_NOBUSY);
1914 return(info.result);
1918 * Resolve an unresolved namecache entry, generally by looking it up.
1919 * The passed ncp must be locked and refd.
1921 * Theoretically since a vnode cannot be recycled while held, and since
1922 * the nc_parent chain holds its vnode as long as children exist, the
1923 * direct parent of the cache entry we are trying to resolve should
1924 * have a valid vnode. If not then generate an error that we can
1925 * determine is related to a resolver bug.
1927 * However, if a vnode was in the middle of a recyclement when the NCP
1928 * got locked, ncp->nc_vp might point to a vnode that is about to become
1929 * invalid. cache_resolve() handles this case by unresolving the entry
1930 * and then re-resolving it.
1932 * Note that successful resolution does not necessarily return an error
1933 * code of 0. If the ncp resolves to a negative cache hit then ENOENT
1934 * will be returned.
1937 cache_resolve(struct nchandle *nch, struct ucred *cred)
1939 struct namecache *par;
1940 struct namecache *ncp;
1941 struct nchandle nctmp;
1942 struct mount *mp;
1943 struct vnode *dvp;
1944 int error;
1946 ncp = nch->ncp;
1947 mp = nch->mount;
1948 restart:
1950 * If the ncp is already resolved we have nothing to do. However,
1951 * we do want to guarentee that a usable vnode is returned when
1952 * a vnode is present, so make sure it hasn't been reclaimed.
1954 if ((ncp->nc_flag & NCF_UNRESOLVED) == 0) {
1955 if (ncp->nc_vp && (ncp->nc_vp->v_flag & VRECLAIMED))
1956 _cache_setunresolved(ncp);
1957 if ((ncp->nc_flag & NCF_UNRESOLVED) == 0)
1958 return (ncp->nc_error);
1962 * Mount points need special handling because the parent does not
1963 * belong to the same filesystem as the ncp.
1965 if (ncp == mp->mnt_ncmountpt.ncp)
1966 return (cache_resolve_mp(mp));
1969 * We expect an unbroken chain of ncps to at least the mount point,
1970 * and even all the way to root (but this code doesn't have to go
1971 * past the mount point).
1973 if (ncp->nc_parent == NULL) {
1974 kprintf("EXDEV case 1 %p %*.*s\n", ncp,
1975 ncp->nc_nlen, ncp->nc_nlen, ncp->nc_name);
1976 ncp->nc_error = EXDEV;
1977 return(ncp->nc_error);
1981 * The vp's of the parent directories in the chain are held via vhold()
1982 * due to the existance of the child, and should not disappear.
1983 * However, there are cases where they can disappear:
1985 * - due to filesystem I/O errors.
1986 * - due to NFS being stupid about tracking the namespace and
1987 * destroys the namespace for entire directories quite often.
1988 * - due to forced unmounts.
1989 * - due to an rmdir (parent will be marked DESTROYED)
1991 * When this occurs we have to track the chain backwards and resolve
1992 * it, looping until the resolver catches up to the current node. We
1993 * could recurse here but we might run ourselves out of kernel stack
1994 * so we do it in a more painful manner. This situation really should
1995 * not occur all that often, or if it does not have to go back too
1996 * many nodes to resolve the ncp.
1998 while (ncp->nc_parent->nc_vp == NULL) {
2000 * This case can occur if a process is CD'd into a
2001 * directory which is then rmdir'd. If the parent is marked
2002 * destroyed there is no point trying to resolve it.
2004 if (ncp->nc_parent->nc_flag & NCF_DESTROYED)
2005 return(ENOENT);
2007 par = ncp->nc_parent;
2008 while (par->nc_parent && par->nc_parent->nc_vp == NULL)
2009 par = par->nc_parent;
2010 if (par->nc_parent == NULL) {
2011 kprintf("EXDEV case 2 %*.*s\n",
2012 par->nc_nlen, par->nc_nlen, par->nc_name);
2013 return (EXDEV);
2015 kprintf("[diagnostic] cache_resolve: had to recurse on %*.*s\n",
2016 par->nc_nlen, par->nc_nlen, par->nc_name);
2018 * The parent is not set in stone, ref and lock it to prevent
2019 * it from disappearing. Also note that due to renames it
2020 * is possible for our ncp to move and for par to no longer
2021 * be one of its parents. We resolve it anyway, the loop
2022 * will handle any moves.
2024 _cache_get(par);
2025 if (par == nch->mount->mnt_ncmountpt.ncp) {
2026 cache_resolve_mp(nch->mount);
2027 } else if ((dvp = par->nc_parent->nc_vp) == NULL) {
2028 kprintf("[diagnostic] cache_resolve: raced on %*.*s\n", par->nc_nlen, par->nc_nlen, par->nc_name);
2029 _cache_put(par);
2030 continue;
2031 } else if (par->nc_flag & NCF_UNRESOLVED) {
2032 /* vhold(dvp); - DVP can't go away */
2033 nctmp.mount = mp;
2034 nctmp.ncp = par;
2035 par->nc_error = VOP_NRESOLVE(&nctmp, dvp, cred);
2036 /* vdrop(dvp); */
2038 if ((error = par->nc_error) != 0) {
2039 if (par->nc_error != EAGAIN) {
2040 kprintf("EXDEV case 3 %*.*s error %d\n",
2041 par->nc_nlen, par->nc_nlen, par->nc_name,
2042 par->nc_error);
2043 _cache_put(par);
2044 return(error);
2046 kprintf("[diagnostic] cache_resolve: EAGAIN par %p %*.*s\n",
2047 par, par->nc_nlen, par->nc_nlen, par->nc_name);
2049 _cache_put(par);
2050 /* loop */
2054 * Call VOP_NRESOLVE() to get the vp, then scan for any disconnected
2055 * ncp's and reattach them. If this occurs the original ncp is marked
2056 * EAGAIN to force a relookup.
2058 * NOTE: in order to call VOP_NRESOLVE(), the parent of the passed
2059 * ncp must already be resolved.
2061 dvp = ncp->nc_parent->nc_vp;
2062 /* vhold(dvp); - dvp can't go away */
2063 nctmp.mount = mp;
2064 nctmp.ncp = ncp;
2065 ncp->nc_error = VOP_NRESOLVE(&nctmp, dvp, cred);
2066 /* vdrop(dvp); */
2067 if (ncp->nc_error == EAGAIN) {
2068 kprintf("[diagnostic] cache_resolve: EAGAIN ncp %p %*.*s\n",
2069 ncp, ncp->nc_nlen, ncp->nc_nlen, ncp->nc_name);
2070 goto restart;
2072 return(ncp->nc_error);
2076 * Resolve the ncp associated with a mount point. Such ncp's almost always
2077 * remain resolved and this routine is rarely called. NFS MPs tends to force
2078 * re-resolution more often due to its mac-truck-smash-the-namecache
2079 * method of tracking namespace changes.
2081 * The semantics for this call is that the passed ncp must be locked on
2082 * entry and will be locked on return. However, if we actually have to
2083 * resolve the mount point we temporarily unlock the entry in order to
2084 * avoid race-to-root deadlocks due to e.g. dead NFS mounts. Because of
2085 * the unlock we have to recheck the flags after we relock.
2087 static int
2088 cache_resolve_mp(struct mount *mp)
2090 struct namecache *ncp = mp->mnt_ncmountpt.ncp;
2091 struct vnode *vp;
2092 int error;
2094 KKASSERT(mp != NULL);
2097 * If the ncp is already resolved we have nothing to do. However,
2098 * we do want to guarentee that a usable vnode is returned when
2099 * a vnode is present, so make sure it hasn't been reclaimed.
2101 if ((ncp->nc_flag & NCF_UNRESOLVED) == 0) {
2102 if (ncp->nc_vp && (ncp->nc_vp->v_flag & VRECLAIMED))
2103 _cache_setunresolved(ncp);
2106 if (ncp->nc_flag & NCF_UNRESOLVED) {
2107 _cache_unlock(ncp);
2108 while (vfs_busy(mp, 0))
2110 error = VFS_ROOT(mp, &vp);
2111 _cache_lock(ncp);
2114 * recheck the ncp state after relocking.
2116 if (ncp->nc_flag & NCF_UNRESOLVED) {
2117 ncp->nc_error = error;
2118 if (error == 0) {
2119 _cache_setvp(ncp, vp);
2120 vput(vp);
2121 } else {
2122 kprintf("[diagnostic] cache_resolve_mp: failed to resolve mount %p\n", mp);
2123 _cache_setvp(ncp, NULL);
2125 } else if (error == 0) {
2126 vput(vp);
2128 vfs_unbusy(mp);
2130 return(ncp->nc_error);
2133 void
2134 cache_cleanneg(int count)
2136 struct namecache *ncp;
2139 * Automode from the vnlru proc - clean out 10% of the negative cache
2140 * entries.
2142 if (count == 0)
2143 count = numneg / 10 + 1;
2146 * Attempt to clean out the specified number of negative cache
2147 * entries.
2149 while (count) {
2150 ncp = TAILQ_FIRST(&ncneglist);
2151 if (ncp == NULL) {
2152 KKASSERT(numneg == 0);
2153 break;
2155 TAILQ_REMOVE(&ncneglist, ncp, nc_vnode);
2156 TAILQ_INSERT_TAIL(&ncneglist, ncp, nc_vnode);
2157 if (_cache_get_nonblock(ncp) == 0)
2158 cache_zap(ncp);
2159 --count;
2164 * Rehash a ncp. Rehashing is typically required if the name changes (should
2165 * not generally occur) or the parent link changes. This function will
2166 * unhash the ncp if the ncp is no longer hashable.
2168 static void
2169 _cache_rehash(struct namecache *ncp)
2171 struct nchashhead *nchpp;
2172 u_int32_t hash;
2174 if (ncp->nc_flag & NCF_HASHED) {
2175 ncp->nc_flag &= ~NCF_HASHED;
2176 LIST_REMOVE(ncp, nc_hash);
2178 if (ncp->nc_nlen && ncp->nc_parent) {
2179 hash = fnv_32_buf(ncp->nc_name, ncp->nc_nlen, FNV1_32_INIT);
2180 hash = fnv_32_buf(&ncp->nc_parent,
2181 sizeof(ncp->nc_parent), hash);
2182 nchpp = NCHHASH(hash);
2183 LIST_INSERT_HEAD(nchpp, ncp, nc_hash);
2184 ncp->nc_flag |= NCF_HASHED;
2189 * Name cache initialization, from vfsinit() when we are booting
2191 void
2192 nchinit(void)
2194 int i;
2195 globaldata_t gd;
2197 /* initialise per-cpu namecache effectiveness statistics. */
2198 for (i = 0; i < ncpus; ++i) {
2199 gd = globaldata_find(i);
2200 gd->gd_nchstats = &nchstats[i];
2202 TAILQ_INIT(&ncneglist);
2203 nchashtbl = hashinit(desiredvnodes*2, M_VFSCACHE, &nchash);
2204 nclockwarn = 1 * hz;
2208 * Called from start_init() to bootstrap the root filesystem. Returns
2209 * a referenced, unlocked namecache record.
2211 void
2212 cache_allocroot(struct nchandle *nch, struct mount *mp, struct vnode *vp)
2214 nch->ncp = cache_alloc(0);
2215 nch->mount = mp;
2216 ++mp->mnt_refs;
2217 if (vp)
2218 _cache_setvp(nch->ncp, vp);
2222 * vfs_cache_setroot()
2224 * Create an association between the root of our namecache and
2225 * the root vnode. This routine may be called several times during
2226 * booting.
2228 * If the caller intends to save the returned namecache pointer somewhere
2229 * it must cache_hold() it.
2231 void
2232 vfs_cache_setroot(struct vnode *nvp, struct nchandle *nch)
2234 struct vnode *ovp;
2235 struct nchandle onch;
2237 ovp = rootvnode;
2238 onch = rootnch;
2239 rootvnode = nvp;
2240 if (nch)
2241 rootnch = *nch;
2242 else
2243 cache_zero(&rootnch);
2244 if (ovp)
2245 vrele(ovp);
2246 if (onch.ncp)
2247 cache_drop(&onch);
2251 * XXX OLD API COMPAT FUNCTION. This really messes up the new namecache
2252 * topology and is being removed as quickly as possible. The new VOP_N*()
2253 * API calls are required to make specific adjustments using the supplied
2254 * ncp pointers rather then just bogusly purging random vnodes.
2256 * Invalidate all namecache entries to a particular vnode as well as
2257 * any direct children of that vnode in the namecache. This is a
2258 * 'catch all' purge used by filesystems that do not know any better.
2260 * Note that the linkage between the vnode and its namecache entries will
2261 * be removed, but the namecache entries themselves might stay put due to
2262 * active references from elsewhere in the system or due to the existance of
2263 * the children. The namecache topology is left intact even if we do not
2264 * know what the vnode association is. Such entries will be marked
2265 * NCF_UNRESOLVED.
2267 void
2268 cache_purge(struct vnode *vp)
2270 cache_inval_vp(vp, CINV_DESTROY | CINV_CHILDREN);
2274 * Flush all entries referencing a particular filesystem.
2276 * Since we need to check it anyway, we will flush all the invalid
2277 * entries at the same time.
2279 #if 0
2281 void
2282 cache_purgevfs(struct mount *mp)
2284 struct nchashhead *nchpp;
2285 struct namecache *ncp, *nnp;
2288 * Scan hash tables for applicable entries.
2290 for (nchpp = &nchashtbl[nchash]; nchpp >= nchashtbl; nchpp--) {
2291 ncp = LIST_FIRST(nchpp);
2292 if (ncp)
2293 _cache_hold(ncp);
2294 while (ncp) {
2295 nnp = LIST_NEXT(ncp, nc_hash);
2296 if (nnp)
2297 _cache_hold(nnp);
2298 if (ncp->nc_mount == mp) {
2299 _cache_lock(ncp);
2300 cache_zap(ncp);
2301 } else {
2302 _cache_drop(ncp);
2304 ncp = nnp;
2309 #endif
2312 * Create a new (theoretically) unique fsmid
2314 int64_t
2315 cache_getnewfsmid(void)
2317 static int fsmid_roller;
2318 int64_t fsmid;
2320 ++fsmid_roller;
2321 fsmid = ((int64_t)time_second << 32) |
2322 (fsmid_roller & 0x7FFFFFFF);
2323 return (fsmid);
2327 static int disablecwd;
2328 SYSCTL_INT(_debug, OID_AUTO, disablecwd, CTLFLAG_RW, &disablecwd, 0, "");
2330 static u_long numcwdcalls; STATNODE(CTLFLAG_RD, numcwdcalls, &numcwdcalls);
2331 static u_long numcwdfail1; STATNODE(CTLFLAG_RD, numcwdfail1, &numcwdfail1);
2332 static u_long numcwdfail2; STATNODE(CTLFLAG_RD, numcwdfail2, &numcwdfail2);
2333 static u_long numcwdfail3; STATNODE(CTLFLAG_RD, numcwdfail3, &numcwdfail3);
2334 static u_long numcwdfail4; STATNODE(CTLFLAG_RD, numcwdfail4, &numcwdfail4);
2335 static u_long numcwdfound; STATNODE(CTLFLAG_RD, numcwdfound, &numcwdfound);
2338 sys___getcwd(struct __getcwd_args *uap)
2340 int buflen;
2341 int error;
2342 char *buf;
2343 char *bp;
2345 if (disablecwd)
2346 return (ENODEV);
2348 buflen = uap->buflen;
2349 if (buflen < 2)
2350 return (EINVAL);
2351 if (buflen > MAXPATHLEN)
2352 buflen = MAXPATHLEN;
2354 buf = kmalloc(buflen, M_TEMP, M_WAITOK);
2355 bp = kern_getcwd(buf, buflen, &error);
2356 if (error == 0)
2357 error = copyout(bp, uap->buf, strlen(bp) + 1);
2358 kfree(buf, M_TEMP);
2359 return (error);
2362 char *
2363 kern_getcwd(char *buf, size_t buflen, int *error)
2365 struct proc *p = curproc;
2366 char *bp;
2367 int i, slash_prefixed;
2368 struct filedesc *fdp;
2369 struct nchandle nch;
2371 numcwdcalls++;
2372 bp = buf;
2373 bp += buflen - 1;
2374 *bp = '\0';
2375 fdp = p->p_fd;
2376 slash_prefixed = 0;
2378 nch = fdp->fd_ncdir;
2379 while (nch.ncp && (nch.ncp != fdp->fd_nrdir.ncp ||
2380 nch.mount != fdp->fd_nrdir.mount)
2383 * While traversing upwards if we encounter the root
2384 * of the current mount we have to skip to the mount point
2385 * in the underlying filesystem.
2387 if (nch.ncp == nch.mount->mnt_ncmountpt.ncp) {
2388 nch = nch.mount->mnt_ncmounton;
2389 continue;
2393 * Prepend the path segment
2395 for (i = nch.ncp->nc_nlen - 1; i >= 0; i--) {
2396 if (bp == buf) {
2397 numcwdfail4++;
2398 *error = ENOMEM;
2399 return(NULL);
2401 *--bp = nch.ncp->nc_name[i];
2403 if (bp == buf) {
2404 numcwdfail4++;
2405 *error = ENOMEM;
2406 return(NULL);
2408 *--bp = '/';
2409 slash_prefixed = 1;
2412 * Go up a directory. This isn't a mount point so we don't
2413 * have to check again.
2415 nch.ncp = nch.ncp->nc_parent;
2417 if (nch.ncp == NULL) {
2418 numcwdfail2++;
2419 *error = ENOENT;
2420 return(NULL);
2422 if (!slash_prefixed) {
2423 if (bp == buf) {
2424 numcwdfail4++;
2425 *error = ENOMEM;
2426 return(NULL);
2428 *--bp = '/';
2430 numcwdfound++;
2431 *error = 0;
2432 return (bp);
2436 * Thus begins the fullpath magic.
2439 #undef STATNODE
2440 #define STATNODE(name) \
2441 static u_int name; \
2442 SYSCTL_UINT(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, 0, "")
2444 static int disablefullpath;
2445 SYSCTL_INT(_debug, OID_AUTO, disablefullpath, CTLFLAG_RW,
2446 &disablefullpath, 0, "");
2448 STATNODE(numfullpathcalls);
2449 STATNODE(numfullpathfail1);
2450 STATNODE(numfullpathfail2);
2451 STATNODE(numfullpathfail3);
2452 STATNODE(numfullpathfail4);
2453 STATNODE(numfullpathfound);
2456 cache_fullpath(struct proc *p, struct nchandle *nchp, char **retbuf, char **freebuf)
2458 char *bp, *buf;
2459 int i, slash_prefixed;
2460 struct nchandle fd_nrdir;
2461 struct nchandle nch;
2463 numfullpathcalls--;
2465 *retbuf = NULL;
2466 *freebuf = NULL;
2468 buf = kmalloc(MAXPATHLEN, M_TEMP, M_WAITOK);
2469 bp = buf + MAXPATHLEN - 1;
2470 *bp = '\0';
2471 if (p != NULL)
2472 fd_nrdir = p->p_fd->fd_nrdir;
2473 else
2474 fd_nrdir = rootnch;
2475 slash_prefixed = 0;
2476 nch = *nchp;
2478 while (nch.ncp &&
2479 (nch.ncp != fd_nrdir.ncp || nch.mount != fd_nrdir.mount)
2482 * While traversing upwards if we encounter the root
2483 * of the current mount we have to skip to the mount point.
2485 if (nch.ncp == nch.mount->mnt_ncmountpt.ncp) {
2486 nch = nch.mount->mnt_ncmounton;
2487 continue;
2491 * Prepend the path segment
2493 for (i = nch.ncp->nc_nlen - 1; i >= 0; i--) {
2494 if (bp == buf) {
2495 numfullpathfail4++;
2496 kfree(buf, M_TEMP);
2497 return(ENOMEM);
2499 *--bp = nch.ncp->nc_name[i];
2501 if (bp == buf) {
2502 numfullpathfail4++;
2503 kfree(buf, M_TEMP);
2504 return(ENOMEM);
2506 *--bp = '/';
2507 slash_prefixed = 1;
2510 * Go up a directory. This isn't a mount point so we don't
2511 * have to check again.
2513 nch.ncp = nch.ncp->nc_parent;
2515 if (nch.ncp == NULL) {
2516 numfullpathfail2++;
2517 kfree(buf, M_TEMP);
2518 return(ENOENT);
2521 if (!slash_prefixed) {
2522 if (bp == buf) {
2523 numfullpathfail4++;
2524 kfree(buf, M_TEMP);
2525 return(ENOMEM);
2527 *--bp = '/';
2529 numfullpathfound++;
2530 *retbuf = bp;
2531 *freebuf = buf;
2533 return(0);
2537 vn_fullpath(struct proc *p, struct vnode *vn, char **retbuf, char **freebuf)
2539 struct namecache *ncp;
2540 struct nchandle nch;
2542 numfullpathcalls++;
2543 if (disablefullpath)
2544 return (ENODEV);
2546 if (p == NULL)
2547 return (EINVAL);
2549 /* vn is NULL, client wants us to use p->p_textvp */
2550 if (vn == NULL) {
2551 if ((vn = p->p_textvp) == NULL)
2552 return (EINVAL);
2554 TAILQ_FOREACH(ncp, &vn->v_namecache, nc_vnode) {
2555 if (ncp->nc_nlen)
2556 break;
2558 if (ncp == NULL)
2559 return (EINVAL);
2561 numfullpathcalls--;
2562 nch.ncp = ncp;;
2563 nch.mount = vn->v_mount;
2564 return(cache_fullpath(p, &nch, retbuf, freebuf));