namecache->nc_refs is no longer protected by the MP lock. Atomic ops must
[dragonfly.git] / sys / kern / vfs_cache.c
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:
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  * 
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 $
70  * $DragonFly: src/sys/kern/vfs_cache.c,v 1.69 2006/06/01 22:45:19 dillon Exp $
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>
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>
90
91 /*
92  * Random lookups in the cache are accomplished with a hash table using
93  * a hash key of (nc_src_vp, name).
94  *
95  * Negative entries may exist and correspond to structures where nc_vp
96  * is NULL.  In a negative entry, NCF_WHITEOUT will be set if the entry
97  * corresponds to a whited-out directory entry (verses simply not finding the
98  * entry at all).
99  *
100  * Upon reaching the last segment of a path, if the reference is for DELETE,
101  * or NOCACHE is set (rewrite), and the name is located in the cache, it
102  * will be dropped.
103  */
104
105 /*
106  * Structures associated with name cacheing.
107  */
108 #define NCHHASH(hash)   (&nchashtbl[(hash) & nchash])
109 #define MINNEG          1024
110
111 MALLOC_DEFINE(M_VFSCACHE, "vfscache", "VFS name cache entries");
112
113 static LIST_HEAD(nchashhead, namecache) *nchashtbl;     /* Hash Table */
114 static struct namecache_list    ncneglist;              /* instead of vnode */
115
116 /*
117  * ncvp_debug - debug cache_fromvp().  This is used by the NFS server
118  * to create the namecache infrastructure leading to a dangling vnode.
119  *
120  * 0    Only errors are reported
121  * 1    Successes are reported
122  * 2    Successes + the whole directory scan is reported
123  * 3    Force the directory scan code run as if the parent vnode did not
124  *      have a namecache record, even if it does have one.
125  */
126 static int      ncvp_debug;
127 SYSCTL_INT(_debug, OID_AUTO, ncvp_debug, CTLFLAG_RW, &ncvp_debug, 0, "");
128
129 static u_long   nchash;                 /* size of hash table */
130 SYSCTL_ULONG(_debug, OID_AUTO, nchash, CTLFLAG_RD, &nchash, 0, "");
131
132 static u_long   ncnegfactor = 16;       /* ratio of negative entries */
133 SYSCTL_ULONG(_debug, OID_AUTO, ncnegfactor, CTLFLAG_RW, &ncnegfactor, 0, "");
134
135 static int      nclockwarn;             /* warn on locked entries in ticks */
136 SYSCTL_INT(_debug, OID_AUTO, nclockwarn, CTLFLAG_RW, &nclockwarn, 0, "");
137
138 static u_long   numneg;         /* number of cache entries allocated */
139 SYSCTL_ULONG(_debug, OID_AUTO, numneg, CTLFLAG_RD, &numneg, 0, "");
140
141 static u_long   numcache;               /* number of cache entries allocated */
142 SYSCTL_ULONG(_debug, OID_AUTO, numcache, CTLFLAG_RD, &numcache, 0, "");
143
144 static u_long   numunres;               /* number of unresolved entries */
145 SYSCTL_ULONG(_debug, OID_AUTO, numunres, CTLFLAG_RD, &numunres, 0, "");
146
147 SYSCTL_INT(_debug, OID_AUTO, vnsize, CTLFLAG_RD, 0, sizeof(struct vnode), "");
148 SYSCTL_INT(_debug, OID_AUTO, ncsize, CTLFLAG_RD, 0, sizeof(struct namecache), "");
149
150 static int cache_resolve_mp(struct namecache *ncp);
151 static void cache_rehash(struct namecache *ncp);
152
153 /*
154  * The new name cache statistics
155  */
156 SYSCTL_NODE(_vfs, OID_AUTO, cache, CTLFLAG_RW, 0, "Name cache statistics");
157 #define STATNODE(mode, name, var) \
158         SYSCTL_ULONG(_vfs_cache, OID_AUTO, name, mode, var, 0, "");
159 STATNODE(CTLFLAG_RD, numneg, &numneg);
160 STATNODE(CTLFLAG_RD, numcache, &numcache);
161 static u_long numcalls; STATNODE(CTLFLAG_RD, numcalls, &numcalls);
162 static u_long dothits; STATNODE(CTLFLAG_RD, dothits, &dothits);
163 static u_long dotdothits; STATNODE(CTLFLAG_RD, dotdothits, &dotdothits);
164 static u_long numchecks; STATNODE(CTLFLAG_RD, numchecks, &numchecks);
165 static u_long nummiss; STATNODE(CTLFLAG_RD, nummiss, &nummiss);
166 static u_long nummisszap; STATNODE(CTLFLAG_RD, nummisszap, &nummisszap);
167 static u_long numposzaps; STATNODE(CTLFLAG_RD, numposzaps, &numposzaps);
168 static u_long numposhits; STATNODE(CTLFLAG_RD, numposhits, &numposhits);
169 static u_long numnegzaps; STATNODE(CTLFLAG_RD, numnegzaps, &numnegzaps);
170 static u_long numneghits; STATNODE(CTLFLAG_RD, numneghits, &numneghits);
171
172 struct nchstats nchstats[SMP_MAXCPU];
173 /*
174  * Export VFS cache effectiveness statistics to user-land.
175  *
176  * The statistics are left for aggregation to user-land so
177  * neat things can be achieved, like observing per-CPU cache
178  * distribution.
179  */
180 static int
181 sysctl_nchstats(SYSCTL_HANDLER_ARGS)
182 {
183         struct globaldata *gd;
184         int i, error;
185
186         error = 0;
187         for (i = 0; i < ncpus; ++i) {
188                 gd = globaldata_find(i);
189                 if ((error = SYSCTL_OUT(req, (void *)&(*gd->gd_nchstats),
190                         sizeof(struct nchstats))))
191                         break;
192         }
193
194         return (error);
195 }
196 SYSCTL_PROC(_vfs_cache, OID_AUTO, nchstats, CTLTYPE_OPAQUE|CTLFLAG_RD,
197   0, 0, sysctl_nchstats, "S,nchstats", "VFS cache effectiveness statistics");
198
199 static void cache_zap(struct namecache *ncp);
200
201 /*
202  * cache_hold() and cache_drop() prevent the premature deletion of a
203  * namecache entry but do not prevent operations (such as zapping) on
204  * that namecache entry.
205  *
206  * This routine may only be called from outside this source module if
207  * nc_refs is already at least 1.
208  *
209  * This is a rare case where callers are allowed to hold a spinlock,
210  * so we can't ourselves.
211  */
212 static __inline
213 struct namecache *
214 _cache_hold(struct namecache *ncp)
215 {
216         atomic_add_int(&ncp->nc_refs, 1);
217         return(ncp);
218 }
219
220 /*
221  * When dropping an entry, if only one ref remains and the entry has not
222  * been resolved, zap it.  Since the one reference is being dropped the
223  * entry had better not be locked.
224  */
225 static __inline
226 void
227 _cache_drop(struct namecache *ncp)
228 {
229         KKASSERT(ncp->nc_refs > 0);
230         if (ncp->nc_refs == 1 && 
231             (ncp->nc_flag & NCF_UNRESOLVED) && 
232             TAILQ_EMPTY(&ncp->nc_list)
233         ) {
234                 KKASSERT(ncp->nc_exlocks == 0);
235                 cache_lock(ncp);
236                 cache_zap(ncp);
237         } else {
238                 atomic_subtract_int(&ncp->nc_refs, 1);
239         }
240 }
241
242 /*
243  * Link a new namecache entry to its parent.  Be careful to avoid races
244  * if vhold() blocks in the future.
245  *
246  * If we are creating a child under an oldapi parent we must mark the
247  * child as being an oldapi entry as well.
248  */
249 static void
250 cache_link_parent(struct namecache *ncp, struct namecache *par)
251 {
252         KKASSERT(ncp->nc_parent == NULL);
253         ncp->nc_parent = par;
254         if (TAILQ_EMPTY(&par->nc_list)) {
255                 TAILQ_INSERT_HEAD(&par->nc_list, ncp, nc_entry);
256                 /*
257                  * Any vp associated with an ncp which has children must
258                  * be held to prevent it from being recycled.
259                  */
260                 if (par->nc_vp)
261                         vhold(par->nc_vp);
262         } else {
263                 TAILQ_INSERT_HEAD(&par->nc_list, ncp, nc_entry);
264         }
265 }
266
267 /*
268  * Remove the parent association from a namecache structure.  If this is
269  * the last child of the parent the cache_drop(par) will attempt to
270  * recursively zap the parent.
271  */
272 static void
273 cache_unlink_parent(struct namecache *ncp)
274 {
275         struct namecache *par;
276
277         if ((par = ncp->nc_parent) != NULL) {
278                 ncp->nc_parent = NULL;
279                 par = cache_hold(par);
280                 TAILQ_REMOVE(&par->nc_list, ncp, nc_entry);
281                 if (par->nc_vp && TAILQ_EMPTY(&par->nc_list))
282                         vdrop(par->nc_vp);
283                 cache_drop(par);
284         }
285 }
286
287 /*
288  * Allocate a new namecache structure.  Most of the code does not require
289  * zero-termination of the string but it makes vop_compat_ncreate() easier.
290  */
291 static struct namecache *
292 cache_alloc(int nlen)
293 {
294         struct namecache *ncp;
295
296         ncp = malloc(sizeof(*ncp), M_VFSCACHE, M_WAITOK|M_ZERO);
297         if (nlen)
298                 ncp->nc_name = malloc(nlen + 1, M_VFSCACHE, M_WAITOK);
299         ncp->nc_nlen = nlen;
300         ncp->nc_flag = NCF_UNRESOLVED;
301         ncp->nc_error = ENOTCONN;       /* needs to be resolved */
302         ncp->nc_refs = 1;
303
304         /*
305          * Construct a fake FSMID based on the time of day and a 32 bit
306          * roller for uniqueness.  This is used to generate a useful
307          * FSMID for filesystems which do not support it.
308          */
309         ncp->nc_fsmid = cache_getnewfsmid();
310         TAILQ_INIT(&ncp->nc_list);
311         cache_lock(ncp);
312         return(ncp);
313 }
314
315 static void
316 cache_free(struct namecache *ncp)
317 {
318         KKASSERT(ncp->nc_refs == 1 && ncp->nc_exlocks == 1);
319         if (ncp->nc_name)
320                 free(ncp->nc_name, M_VFSCACHE);
321         free(ncp, M_VFSCACHE);
322 }
323
324 /*
325  * Ref and deref a namecache structure.
326  *
327  * Warning: caller may hold an unrelated read spinlock, which means we can't
328  * use read spinlocks here.
329  */
330 struct namecache *
331 cache_hold(struct namecache *ncp)
332 {
333         return(_cache_hold(ncp));
334 }
335
336 void
337 cache_drop(struct namecache *ncp)
338 {
339         _cache_drop(ncp);
340 }
341
342 /*
343  * Namespace locking.  The caller must already hold a reference to the
344  * namecache structure in order to lock/unlock it.  This function prevents
345  * the namespace from being created or destroyed by accessors other then
346  * the lock holder.
347  *
348  * Note that holding a locked namecache structure prevents other threads
349  * from making namespace changes (e.g. deleting or creating), prevents
350  * vnode association state changes by other threads, and prevents the
351  * namecache entry from being resolved or unresolved by other threads.
352  *
353  * The lock owner has full authority to associate/disassociate vnodes
354  * and resolve/unresolve the locked ncp.
355  *
356  * WARNING!  Holding a locked ncp will prevent a vnode from being destroyed
357  * or recycled, but it does NOT help you if the vnode had already initiated
358  * a recyclement.  If this is important, use cache_get() rather then 
359  * cache_lock() (and deal with the differences in the way the refs counter
360  * is handled).  Or, alternatively, make an unconditional call to 
361  * cache_validate() or cache_resolve() after cache_lock() returns.
362  */
363 void
364 cache_lock(struct namecache *ncp)
365 {
366         thread_t td;
367         int didwarn;
368
369         KKASSERT(ncp->nc_refs != 0);
370         didwarn = 0;
371         td = curthread;
372
373         for (;;) {
374                 if (ncp->nc_exlocks == 0) {
375                         ncp->nc_exlocks = 1;
376                         ncp->nc_locktd = td;
377                         /* 
378                          * The vp associated with a locked ncp must be held
379                          * to prevent it from being recycled (which would
380                          * cause the ncp to become unresolved).
381                          *
382                          * WARNING!  If VRECLAIMED is set the vnode could
383                          * already be in the middle of a recycle.  Callers
384                          * should not assume that nc_vp is usable when
385                          * not NULL.  cache_vref() or cache_vget() must be
386                          * called.
387                          *
388                          * XXX loop on race for later MPSAFE work.
389                          */
390                         if (ncp->nc_vp)
391                                 vhold(ncp->nc_vp);
392                         break;
393                 }
394                 if (ncp->nc_locktd == td) {
395                         ++ncp->nc_exlocks;
396                         break;
397                 }
398                 ncp->nc_flag |= NCF_LOCKREQ;
399                 if (tsleep(ncp, 0, "clock", nclockwarn) == EWOULDBLOCK) {
400                         if (didwarn)
401                                 continue;
402                         didwarn = 1;
403                         printf("[diagnostic] cache_lock: blocked on %p", ncp);
404                         if ((ncp->nc_flag & NCF_MOUNTPT) && ncp->nc_mount)
405                             printf(" [MOUNTFROM %s]\n", ncp->nc_mount->mnt_stat.f_mntfromname);
406                         else
407                             printf(" \"%*.*s\"\n",
408                                 ncp->nc_nlen, ncp->nc_nlen,
409                                 ncp->nc_name);
410                 }
411         }
412
413         if (didwarn == 1) {
414                 printf("[diagnostic] cache_lock: unblocked %*.*s\n",
415                         ncp->nc_nlen, ncp->nc_nlen, ncp->nc_name);
416         }
417 }
418
419 int
420 cache_lock_nonblock(struct namecache *ncp)
421 {
422         thread_t td;
423
424         KKASSERT(ncp->nc_refs != 0);
425         td = curthread;
426         if (ncp->nc_exlocks == 0) {
427                 ncp->nc_exlocks = 1;
428                 ncp->nc_locktd = td;
429                 /* 
430                  * The vp associated with a locked ncp must be held
431                  * to prevent it from being recycled (which would
432                  * cause the ncp to become unresolved).
433                  *
434                  * WARNING!  If VRECLAIMED is set the vnode could
435                  * already be in the middle of a recycle.  Callers
436                  * should not assume that nc_vp is usable when
437                  * not NULL.  cache_vref() or cache_vget() must be
438                  * called.
439                  *
440                  * XXX loop on race for later MPSAFE work.
441                  */
442                 if (ncp->nc_vp)
443                         vhold(ncp->nc_vp);
444                 return(0);
445         } else {
446                 return(EWOULDBLOCK);
447         }
448 }
449
450 void
451 cache_unlock(struct namecache *ncp)
452 {
453         thread_t td = curthread;
454
455         KKASSERT(ncp->nc_refs > 0);
456         KKASSERT(ncp->nc_exlocks > 0);
457         KKASSERT(ncp->nc_locktd == td);
458         if (--ncp->nc_exlocks == 0) {
459                 if (ncp->nc_vp)
460                         vdrop(ncp->nc_vp);
461                 ncp->nc_locktd = NULL;
462                 if (ncp->nc_flag & NCF_LOCKREQ) {
463                         ncp->nc_flag &= ~NCF_LOCKREQ;
464                         wakeup(ncp);
465                 }
466         }
467 }
468
469 /*
470  * ref-and-lock, unlock-and-deref functions.
471  *
472  * This function is primarily used by nlookup.  Even though cache_lock
473  * holds the vnode, it is possible that the vnode may have already
474  * initiated a recyclement.  We want cache_get() to return a definitively
475  * usable vnode or a definitively unresolved ncp.
476  */
477 struct namecache *
478 cache_get(struct namecache *ncp)
479 {
480         _cache_hold(ncp);
481         cache_lock(ncp);
482         if (ncp->nc_vp && (ncp->nc_vp->v_flag & VRECLAIMED))
483                 cache_setunresolved(ncp);
484         return(ncp);
485 }
486
487 int
488 cache_get_nonblock(struct namecache *ncp)
489 {
490         /* XXX MP */
491         if (ncp->nc_exlocks == 0 || ncp->nc_locktd == curthread) {
492                 _cache_hold(ncp);
493                 cache_lock(ncp);
494                 if (ncp->nc_vp && (ncp->nc_vp->v_flag & VRECLAIMED))
495                         cache_setunresolved(ncp);
496                 return(0);
497         }
498         return(EWOULDBLOCK);
499 }
500
501 void
502 cache_put(struct namecache *ncp)
503 {
504         cache_unlock(ncp);
505         _cache_drop(ncp);
506 }
507
508 /*
509  * Resolve an unresolved ncp by associating a vnode with it.  If the
510  * vnode is NULL, a negative cache entry is created.
511  *
512  * The ncp should be locked on entry and will remain locked on return.
513  */
514 void
515 cache_setvp(struct namecache *ncp, struct vnode *vp)
516 {
517         KKASSERT(ncp->nc_flag & NCF_UNRESOLVED);
518         ncp->nc_vp = vp;
519         if (vp != NULL) {
520                 /*
521                  * Any vp associated with an ncp which has children must
522                  * be held.  Any vp associated with a locked ncp must be held.
523                  */
524                 if (!TAILQ_EMPTY(&ncp->nc_list))
525                         vhold(vp);
526                 TAILQ_INSERT_HEAD(&vp->v_namecache, ncp, nc_vnode);
527                 if (ncp->nc_exlocks)
528                         vhold(vp);
529
530                 /*
531                  * Set auxillary flags
532                  */
533                 switch(vp->v_type) {
534                 case VDIR:
535                         ncp->nc_flag |= NCF_ISDIR;
536                         break;
537                 case VLNK:
538                         ncp->nc_flag |= NCF_ISSYMLINK;
539                         /* XXX cache the contents of the symlink */
540                         break;
541                 default:
542                         break;
543                 }
544                 ++numcache;
545                 ncp->nc_error = 0;
546         } else {
547                 TAILQ_INSERT_TAIL(&ncneglist, ncp, nc_vnode);
548                 ++numneg;
549                 ncp->nc_error = ENOENT;
550         }
551         ncp->nc_flag &= ~NCF_UNRESOLVED;
552 }
553
554 void
555 cache_settimeout(struct namecache *ncp, int nticks)
556 {
557         if ((ncp->nc_timeout = ticks + nticks) == 0)
558                 ncp->nc_timeout = 1;
559 }
560
561 /*
562  * Disassociate the vnode or negative-cache association and mark a
563  * namecache entry as unresolved again.  Note that the ncp is still
564  * left in the hash table and still linked to its parent.
565  *
566  * The ncp should be locked and refd on entry and will remain locked and refd
567  * on return.
568  *
569  * This routine is normally never called on a directory containing children.
570  * However, NFS often does just that in its rename() code as a cop-out to
571  * avoid complex namespace operations.  This disconnects a directory vnode
572  * from its namecache and can cause the OLDAPI and NEWAPI to get out of
573  * sync.
574  *
575  * NOTE: NCF_FSMID must be cleared so a refurbishment of the ncp, such as
576  * in a create, properly propogates flag up the chain.
577  */
578 void
579 cache_setunresolved(struct namecache *ncp)
580 {
581         struct vnode *vp;
582
583         if ((ncp->nc_flag & NCF_UNRESOLVED) == 0) {
584                 ncp->nc_flag |= NCF_UNRESOLVED;
585                 ncp->nc_timeout = 0;
586                 ncp->nc_error = ENOTCONN;
587                 ++numunres;
588                 if ((vp = ncp->nc_vp) != NULL) {
589                         --numcache;
590                         ncp->nc_vp = NULL;
591                         TAILQ_REMOVE(&vp->v_namecache, ncp, nc_vnode);
592
593                         /*
594                          * Any vp associated with an ncp with children is
595                          * held by that ncp.  Any vp associated with a locked
596                          * ncp is held by that ncp.  These conditions must be
597                          * undone when the vp is cleared out from the ncp.
598                          */
599                         if (ncp->nc_flag & NCF_FSMID)
600                                 vupdatefsmid(vp);
601                         if (!TAILQ_EMPTY(&ncp->nc_list))
602                                 vdrop(vp);
603                         if (ncp->nc_exlocks)
604                                 vdrop(vp);
605                 } else {
606                         TAILQ_REMOVE(&ncneglist, ncp, nc_vnode);
607                         --numneg;
608                 }
609                 ncp->nc_flag &= ~(NCF_WHITEOUT|NCF_ISDIR|NCF_ISSYMLINK|
610                                   NCF_FSMID);
611         }
612 }
613
614 /*
615  * Invalidate portions of the namecache topology given a starting entry.
616  * The passed ncp is set to an unresolved state and:
617  *
618  * The passed ncp must be locked.
619  *
620  * CINV_DESTROY         - Set a flag in the passed ncp entry indicating
621  *                        that the physical underlying nodes have been 
622  *                        destroyed... as in deleted.  For example, when
623  *                        a directory is removed.  This will cause record
624  *                        lookups on the name to no longer be able to find
625  *                        the record and tells the resolver to return failure
626  *                        rather then trying to resolve through the parent.
627  *
628  *                        The topology itself, including ncp->nc_name,
629  *                        remains intact.
630  *
631  *                        This only applies to the passed ncp, if CINV_CHILDREN
632  *                        is specified the children are not flagged.
633  *
634  * CINV_CHILDREN        - Set all children (recursively) to an unresolved
635  *                        state as well.
636  *
637  *                        Note that this will also have the side effect of
638  *                        cleaning out any unreferenced nodes in the topology
639  *                        from the leaves up as the recursion backs out.
640  *
641  * Note that the topology for any referenced nodes remains intact.
642  *
643  * It is possible for cache_inval() to race a cache_resolve(), meaning that
644  * the namecache entry may not actually be invalidated on return if it was
645  * revalidated while recursing down into its children.  This code guarentees
646  * that the node(s) will go through an invalidation cycle, but does not 
647  * guarentee that they will remain in an invalidated state. 
648  *
649  * Returns non-zero if a revalidation was detected during the invalidation
650  * recursion, zero otherwise.  Note that since only the original ncp is
651  * locked the revalidation ultimately can only indicate that the original ncp
652  * *MIGHT* no have been reresolved.
653  */
654 int
655 cache_inval(struct namecache *ncp, int flags)
656 {
657         struct namecache *kid;
658         struct namecache *nextkid;
659         int rcnt = 0;
660
661         KKASSERT(ncp->nc_exlocks);
662
663         cache_setunresolved(ncp);
664         if (flags & CINV_DESTROY)
665                 ncp->nc_flag |= NCF_DESTROYED;
666
667         if ((flags & CINV_CHILDREN) && 
668             (kid = TAILQ_FIRST(&ncp->nc_list)) != NULL
669         ) {
670                 cache_hold(kid);
671                 cache_unlock(ncp);
672                 while (kid) {
673                         if ((nextkid = TAILQ_NEXT(kid, nc_entry)) != NULL)
674                                 cache_hold(nextkid);
675                         if ((kid->nc_flag & NCF_UNRESOLVED) == 0 ||
676                             TAILQ_FIRST(&kid->nc_list)
677                         ) {
678                                 cache_lock(kid);
679                                 rcnt += cache_inval(kid, flags & ~CINV_DESTROY);
680                                 cache_unlock(kid);
681                         }
682                         cache_drop(kid);
683                         kid = nextkid;
684                 }
685                 cache_lock(ncp);
686         }
687
688         /*
689          * Someone could have gotten in there while ncp was unlocked,
690          * retry if so.
691          */
692         if ((ncp->nc_flag & NCF_UNRESOLVED) == 0)
693                 ++rcnt;
694         return (rcnt);
695 }
696
697 /*
698  * Invalidate a vnode's namecache associations.  To avoid races against
699  * the resolver we do not invalidate a node which we previously invalidated
700  * but which was then re-resolved while we were in the invalidation loop.
701  *
702  * Returns non-zero if any namecache entries remain after the invalidation
703  * loop completed.
704  *
705  * NOTE: unlike the namecache topology which guarentees that ncp's will not
706  * be ripped out of the topology while held, the vnode's v_namecache list
707  * has no such restriction.  NCP's can be ripped out of the list at virtually
708  * any time if not locked, even if held.
709  */
710 int
711 cache_inval_vp(struct vnode *vp, int flags)
712 {
713         struct namecache *ncp;
714         struct namecache *next;
715
716 restart:
717         ncp = TAILQ_FIRST(&vp->v_namecache);
718         if (ncp)
719                 cache_hold(ncp);
720         while (ncp) {
721                 /* loop entered with ncp held */
722                 if ((next = TAILQ_NEXT(ncp, nc_vnode)) != NULL)
723                         cache_hold(next);
724                 cache_lock(ncp);
725                 if (ncp->nc_vp != vp) {
726                         printf("Warning: cache_inval_vp: race-A detected on "
727                                 "%s\n", ncp->nc_name);
728                         cache_put(ncp);
729                         if (next)
730                                 cache_drop(next);
731                         goto restart;
732                 }
733                 cache_inval(ncp, flags);
734                 cache_put(ncp);         /* also releases reference */
735                 ncp = next;
736                 if (ncp && ncp->nc_vp != vp) {
737                         printf("Warning: cache_inval_vp: race-B detected on "
738                                 "%s\n", ncp->nc_name);
739                         cache_drop(ncp);
740                         goto restart;
741                 }
742         }
743         return(TAILQ_FIRST(&vp->v_namecache) != NULL);
744 }
745
746 /*
747  * The source ncp has been renamed to the target ncp.  Both fncp and tncp
748  * must be locked.  Both will be set to unresolved, any children of tncp
749  * will be disconnected (the prior contents of the target is assumed to be
750  * destroyed by the rename operation, e.g. renaming over an empty directory),
751  * and all children of fncp will be moved to tncp.
752  *
753  * XXX the disconnection could pose a problem, check code paths to make
754  * sure any code that blocks can handle the parent being changed out from
755  * under it.  Maybe we should lock the children (watch out for deadlocks) ?
756  *
757  * After we return the caller has the option of calling cache_setvp() if
758  * the vnode of the new target ncp is known.
759  *
760  * Any process CD'd into any of the children will no longer be able to ".."
761  * back out.  An rm -rf can cause this situation to occur.
762  */
763 void
764 cache_rename(struct namecache *fncp, struct namecache *tncp)
765 {
766         struct namecache *scan;
767         int didwarn = 0;
768
769         cache_setunresolved(fncp);
770         cache_setunresolved(tncp);
771         while (cache_inval(tncp, CINV_CHILDREN) != 0) {
772                 if (didwarn++ % 10 == 0) {
773                         printf("Warning: cache_rename: race during "
774                                 "rename %s->%s\n",
775                                 fncp->nc_name, tncp->nc_name);
776                 }
777                 tsleep(tncp, 0, "mvrace", hz / 10);
778                 cache_setunresolved(tncp);
779         }
780         while ((scan = TAILQ_FIRST(&fncp->nc_list)) != NULL) {
781                 cache_hold(scan);
782                 cache_unlink_parent(scan);
783                 cache_link_parent(scan, tncp);
784                 if (scan->nc_flag & NCF_HASHED)
785                         cache_rehash(scan);
786                 cache_drop(scan);
787         }
788 }
789
790 /*
791  * vget the vnode associated with the namecache entry.  Resolve the namecache
792  * entry if necessary and deal with namecache/vp races.  The passed ncp must
793  * be referenced and may be locked.  The ncp's ref/locking state is not 
794  * effected by this call.
795  *
796  * lk_type may be LK_SHARED, LK_EXCLUSIVE.  A ref'd, possibly locked
797  * (depending on the passed lk_type) will be returned in *vpp with an error
798  * of 0, or NULL will be returned in *vpp with a non-0 error code.  The
799  * most typical error is ENOENT, meaning that the ncp represents a negative
800  * cache hit and there is no vnode to retrieve, but other errors can occur
801  * too.
802  *
803  * The main race we have to deal with are namecache zaps.  The ncp itself
804  * will not disappear since it is referenced, and it turns out that the
805  * validity of the vp pointer can be checked simply by rechecking the
806  * contents of ncp->nc_vp.
807  */
808 int
809 cache_vget(struct namecache *ncp, struct ucred *cred,
810            int lk_type, struct vnode **vpp)
811 {
812         struct vnode *vp;
813         int error;
814
815 again:
816         vp = NULL;
817         if (ncp->nc_flag & NCF_UNRESOLVED) {
818                 cache_lock(ncp);
819                 error = cache_resolve(ncp, cred);
820                 cache_unlock(ncp);
821         } else {
822                 error = 0;
823         }
824         if (error == 0 && (vp = ncp->nc_vp) != NULL) {
825                 /*
826                  * Accessing the vnode from the namecache is a bit 
827                  * dangerous.  Because there are no refs on the vnode, it
828                  * could be in the middle of a reclaim.
829                  */
830                 if (vp->v_flag & VRECLAIMED) {
831                         printf("Warning: vnode reclaim race detected in cache_vget on %p (%s)\n", vp, ncp->nc_name);
832                         cache_lock(ncp);
833                         cache_setunresolved(ncp);
834                         cache_unlock(ncp);
835                         goto again;
836                 }
837                 error = vget(vp, lk_type);
838                 if (error) {
839                         if (vp != ncp->nc_vp)
840                                 goto again;
841                         vp = NULL;
842                 } else if (vp != ncp->nc_vp) {
843                         vput(vp);
844                         goto again;
845                 } else if (vp->v_flag & VRECLAIMED) {
846                         panic("vget succeeded on a VRECLAIMED node! vp %p", vp);
847                 }
848         }
849         if (error == 0 && vp == NULL)
850                 error = ENOENT;
851         *vpp = vp;
852         return(error);
853 }
854
855 int
856 cache_vref(struct namecache *ncp, struct ucred *cred, struct vnode **vpp)
857 {
858         struct vnode *vp;
859         int error;
860
861 again:
862         vp = NULL;
863         if (ncp->nc_flag & NCF_UNRESOLVED) {
864                 cache_lock(ncp);
865                 error = cache_resolve(ncp, cred);
866                 cache_unlock(ncp);
867         } else {
868                 error = 0;
869         }
870         if (error == 0 && (vp = ncp->nc_vp) != NULL) {
871                 /*
872                  * Since we did not obtain any locks, a cache zap 
873                  * race can occur here if the vnode is in the middle
874                  * of being reclaimed and has not yet been able to
875                  * clean out its cache node.  If that case occurs,
876                  * we must lock and unresolve the cache, then loop
877                  * to retry.
878                  */
879                 if (vp->v_flag & VRECLAIMED) {
880                         printf("Warning: vnode reclaim race detected on cache_vref %p (%s)\n", vp, ncp->nc_name);
881                         cache_lock(ncp);
882                         cache_setunresolved(ncp);
883                         cache_unlock(ncp);
884                         goto again;
885                 }
886                 vref(vp);
887         }
888         if (error == 0 && vp == NULL)
889                 error = ENOENT;
890         *vpp = vp;
891         return(error);
892 }
893
894 /*
895  * Recursively set the FSMID update flag for namecache nodes leading
896  * to root.  This will cause the next getattr or reclaim to increment the
897  * fsmid and mark the inode for lazy updating.
898  *
899  * Stop recursing when we hit a node whos NCF_FSMID flag is already set.
900  * This makes FSMIDs work in an Einsteinian fashion - where the observation
901  * effects the result.  In this case a program monitoring a higher level
902  * node will have detected some prior change and started its scan (clearing
903  * NCF_FSMID in higher level nodes), but since it has not yet observed the
904  * node where we find NCF_FSMID still set, we can safely make the related
905  * modification without interfering with the theorized program.
906  *
907  * This also means that FSMIDs cannot represent time-domain quantities
908  * in a hierarchical sense.  But the main reason for doing it this way
909  * is to reduce the amount of recursion that occurs in the critical path
910  * when e.g. a program is writing to a file that sits deep in a directory
911  * hierarchy.
912  */
913 void
914 cache_update_fsmid(struct namecache *ncp)
915 {
916         struct vnode *vp;
917         struct namecache *scan;
918
919         /*
920          * Warning: even if we get a non-NULL vp it could still be in the
921          * middle of a recyclement.  Don't do anything fancy, just set
922          * NCF_FSMID.
923          */
924         if ((vp = ncp->nc_vp) != NULL) {
925                 TAILQ_FOREACH(ncp, &vp->v_namecache, nc_vnode) {
926                         for (scan = ncp; scan; scan = scan->nc_parent) {
927                                 if (scan->nc_flag & NCF_FSMID)
928                                         break;
929                                 scan->nc_flag |= NCF_FSMID;
930                         }
931                 }
932         } else {
933                 while (ncp && (ncp->nc_flag & NCF_FSMID) == 0) {
934                         ncp->nc_flag |= NCF_FSMID;
935                         ncp = ncp->nc_parent;
936                 }
937         }
938 }
939
940 void
941 cache_update_fsmid_vp(struct vnode *vp)
942 {
943         struct namecache *ncp;
944         struct namecache *scan;
945
946         TAILQ_FOREACH(ncp, &vp->v_namecache, nc_vnode) {
947                 for (scan = ncp; scan; scan = scan->nc_parent) {
948                         if (scan->nc_flag & NCF_FSMID)
949                                 break;
950                         scan->nc_flag |= NCF_FSMID;
951                 }
952         }
953 }
954
955 /*
956  * If getattr is called on a vnode (e.g. a stat call), the filesystem
957  * may call this routine to determine if the namecache has the hierarchical
958  * change flag set, requiring the fsmid to be updated.
959  *
960  * Since 0 indicates no support, make sure the filesystem fsmid is at least
961  * 1.
962  */
963 int
964 cache_check_fsmid_vp(struct vnode *vp, int64_t *fsmid)
965 {
966         struct namecache *ncp;
967         int changed = 0;
968
969         TAILQ_FOREACH(ncp, &vp->v_namecache, nc_vnode) {
970                 if (ncp->nc_flag & NCF_FSMID) {
971                         ncp->nc_flag &= ~NCF_FSMID;
972                         changed = 1;
973                 }
974         }
975         if (*fsmid == 0)
976                 ++*fsmid;
977         if (changed)
978                 ++*fsmid;
979         return(changed);
980 }
981
982 /*
983  * Convert a directory vnode to a namecache record without any other 
984  * knowledge of the topology.  This ONLY works with directory vnodes and
985  * is ONLY used by the NFS server.  dvp must be refd but unlocked, and the
986  * returned ncp (if not NULL) will be held and unlocked.
987  *
988  * If 'makeit' is 0 and dvp has no existing namecache record, NULL is returned.
989  * If 'makeit' is 1 we attempt to track-down and create the namecache topology
990  * for dvp.  This will fail only if the directory has been deleted out from
991  * under the caller.  
992  *
993  * Callers must always check for a NULL return no matter the value of 'makeit'.
994  *
995  * To avoid underflowing the kernel stack each recursive call increments
996  * the makeit variable.
997  */
998
999 static int cache_inefficient_scan(struct namecache *ncp, struct ucred *cred,
1000                                   struct vnode *dvp);
1001 static int cache_fromdvp_try(struct vnode *dvp, struct ucred *cred, 
1002                                   struct vnode **saved_dvp);
1003
1004 struct namecache *
1005 cache_fromdvp(struct vnode *dvp, struct ucred *cred, int makeit)
1006 {
1007         struct namecache *ncp;
1008         struct vnode *saved_dvp;
1009         struct vnode *pvp;
1010         int error;
1011
1012         ncp = NULL;
1013         saved_dvp = NULL;
1014
1015         /*
1016          * Temporary debugging code to force the directory scanning code
1017          * to be exercised.
1018          */
1019         if (ncvp_debug >= 3 && makeit && TAILQ_FIRST(&dvp->v_namecache)) {
1020                 ncp = TAILQ_FIRST(&dvp->v_namecache);
1021                 printf("cache_fromdvp: forcing %s\n", ncp->nc_name);
1022                 goto force;
1023         }
1024
1025         /*
1026          * Loop until resolution, inside code will break out on error.
1027          */
1028         while ((ncp = TAILQ_FIRST(&dvp->v_namecache)) == NULL && makeit) {
1029 force:
1030                 /*
1031                  * If dvp is the root of its filesystem it should already
1032                  * have a namecache pointer associated with it as a side 
1033                  * effect of the mount, but it may have been disassociated.
1034                  */
1035                 if (dvp->v_flag & VROOT) {
1036                         ncp = cache_get(dvp->v_mount->mnt_ncp);
1037                         error = cache_resolve_mp(ncp);
1038                         cache_put(ncp);
1039                         if (ncvp_debug) {
1040                                 printf("cache_fromdvp: resolve root of mount %p error %d", 
1041                                         dvp->v_mount, error);
1042                         }
1043                         if (error) {
1044                                 if (ncvp_debug)
1045                                         printf(" failed\n");
1046                                 ncp = NULL;
1047                                 break;
1048                         }
1049                         if (ncvp_debug)
1050                                 printf(" succeeded\n");
1051                         continue;
1052                 }
1053
1054                 /*
1055                  * If we are recursed too deeply resort to an O(n^2)
1056                  * algorithm to resolve the namecache topology.  The
1057                  * resolved pvp is left referenced in saved_dvp to
1058                  * prevent the tree from being destroyed while we loop.
1059                  */
1060                 if (makeit > 20) {
1061                         error = cache_fromdvp_try(dvp, cred, &saved_dvp);
1062                         if (error) {
1063                                 printf("lookupdotdot(longpath) failed %d "
1064                                        "dvp %p\n", error, dvp);
1065                                 break;
1066                         }
1067                         continue;
1068                 }
1069
1070                 /*
1071                  * Get the parent directory and resolve its ncp.
1072                  */
1073                 error = vop_nlookupdotdot(*dvp->v_ops, dvp, &pvp, cred);
1074                 if (error) {
1075                         printf("lookupdotdot failed %d dvp %p\n", error, dvp);
1076                         break;
1077                 }
1078                 VOP_UNLOCK(pvp, 0);
1079
1080                 /*
1081                  * Reuse makeit as a recursion depth counter.
1082                  */
1083                 ncp = cache_fromdvp(pvp, cred, makeit + 1);
1084                 vrele(pvp);
1085                 if (ncp == NULL)
1086                         break;
1087
1088                 /*
1089                  * Do an inefficient scan of pvp (embodied by ncp) to look
1090                  * for dvp.  This will create a namecache record for dvp on
1091                  * success.  We loop up to recheck on success.
1092                  *
1093                  * ncp and dvp are both held but not locked.
1094                  */
1095                 error = cache_inefficient_scan(ncp, cred, dvp);
1096                 cache_drop(ncp);
1097                 if (error) {
1098                         printf("cache_fromdvp: scan %p (%s) failed on dvp=%p\n",
1099                                 pvp, ncp->nc_name, dvp);
1100                         ncp = NULL;
1101                         break;
1102                 }
1103                 if (ncvp_debug) {
1104                         printf("cache_fromdvp: scan %p (%s) succeeded\n",
1105                                 pvp, ncp->nc_name);
1106                 }
1107         }
1108         if (ncp)
1109                 cache_hold(ncp);
1110         if (saved_dvp)
1111                 vrele(saved_dvp);
1112         return (ncp);
1113 }
1114
1115 /*
1116  * Go up the chain of parent directories until we find something
1117  * we can resolve into the namecache.  This is very inefficient.
1118  */
1119 static
1120 int
1121 cache_fromdvp_try(struct vnode *dvp, struct ucred *cred,
1122                   struct vnode **saved_dvp)
1123 {
1124         struct namecache *ncp;
1125         struct vnode *pvp;
1126         int error;
1127         static time_t last_fromdvp_report;
1128
1129         /*
1130          * Loop getting the parent directory vnode until we get something we
1131          * can resolve in the namecache.
1132          */
1133         vref(dvp);
1134         for (;;) {
1135                 error = vop_nlookupdotdot(*dvp->v_ops, dvp, &pvp, cred);
1136                 if (error) {
1137                         vrele(dvp);
1138                         return (error);
1139                 }
1140                 VOP_UNLOCK(pvp, 0);
1141                 if ((ncp = TAILQ_FIRST(&pvp->v_namecache)) != NULL) {
1142                         cache_hold(ncp);
1143                         vrele(pvp);
1144                         break;
1145                 }
1146                 if (pvp->v_flag & VROOT) {
1147                         ncp = cache_get(pvp->v_mount->mnt_ncp);
1148                         error = cache_resolve_mp(ncp);
1149                         cache_unlock(ncp);
1150                         vrele(pvp);
1151                         if (error) {
1152                                 cache_drop(ncp);
1153                                 vrele(dvp);
1154                                 return (error);
1155                         }
1156                         break;
1157                 }
1158                 vrele(dvp);
1159                 dvp = pvp;
1160         }
1161         if (last_fromdvp_report != time_second) {
1162                 last_fromdvp_report = time_second;
1163                 printf("Warning: extremely inefficient path resolution on %s\n",
1164                         ncp->nc_name);
1165         }
1166         error = cache_inefficient_scan(ncp, cred, dvp);
1167
1168         /*
1169          * Hopefully dvp now has a namecache record associated with it.
1170          * Leave it referenced to prevent the kernel from recycling the
1171          * vnode.  Otherwise extremely long directory paths could result
1172          * in endless recycling.
1173          */
1174         if (*saved_dvp)
1175             vrele(*saved_dvp);
1176         *saved_dvp = dvp;
1177         return (error);
1178 }
1179
1180
1181 /*
1182  * Do an inefficient scan of the directory represented by ncp looking for
1183  * the directory vnode dvp.  ncp must be held but not locked on entry and
1184  * will be held on return.  dvp must be refd but not locked on entry and
1185  * will remain refd on return.
1186  *
1187  * Why do this at all?  Well, due to its stateless nature the NFS server
1188  * converts file handles directly to vnodes without necessarily going through
1189  * the namecache ops that would otherwise create the namecache topology
1190  * leading to the vnode.  We could either (1) Change the namecache algorithms
1191  * to allow disconnect namecache records that are re-merged opportunistically,
1192  * or (2) Make the NFS server backtrack and scan to recover a connected
1193  * namecache topology in order to then be able to issue new API lookups.
1194  *
1195  * It turns out that (1) is a huge mess.  It takes a nice clean set of 
1196  * namecache algorithms and introduces a lot of complication in every subsystem
1197  * that calls into the namecache to deal with the re-merge case, especially
1198  * since we are using the namecache to placehold negative lookups and the
1199  * vnode might not be immediately assigned. (2) is certainly far less
1200  * efficient then (1), but since we are only talking about directories here
1201  * (which are likely to remain cached), the case does not actually run all
1202  * that often and has the supreme advantage of not polluting the namecache
1203  * algorithms.
1204  */
1205 static int
1206 cache_inefficient_scan(struct namecache *ncp, struct ucred *cred, 
1207                        struct vnode *dvp)
1208 {
1209         struct nlcomponent nlc;
1210         struct namecache *rncp;
1211         struct dirent *den;
1212         struct vnode *pvp;
1213         struct vattr vat;
1214         struct iovec iov;
1215         struct uio uio;
1216         int blksize;
1217         int eofflag;
1218         int bytes;
1219         char *rbuf;
1220         int error;
1221
1222         vat.va_blocksize = 0;
1223         if ((error = VOP_GETATTR(dvp, &vat)) != 0)
1224                 return (error);
1225         if ((error = cache_vget(ncp, cred, LK_SHARED, &pvp)) != 0)
1226                 return (error);
1227         if (ncvp_debug)
1228                 printf("inefficient_scan: directory iosize %ld vattr fileid = %ld\n", vat.va_blocksize, (long)vat.va_fileid);
1229         if ((blksize = vat.va_blocksize) == 0)
1230                 blksize = DEV_BSIZE;
1231         rbuf = malloc(blksize, M_TEMP, M_WAITOK);
1232         rncp = NULL;
1233
1234         eofflag = 0;
1235         uio.uio_offset = 0;
1236 again:
1237         iov.iov_base = rbuf;
1238         iov.iov_len = blksize;
1239         uio.uio_iov = &iov;
1240         uio.uio_iovcnt = 1;
1241         uio.uio_resid = blksize;
1242         uio.uio_segflg = UIO_SYSSPACE;
1243         uio.uio_rw = UIO_READ;
1244         uio.uio_td = curthread;
1245
1246         if (ncvp_debug >= 2)
1247                 printf("cache_inefficient_scan: readdir @ %08x\n", (int)uio.uio_offset);
1248         error = VOP_READDIR(pvp, &uio, cred, &eofflag, NULL, NULL);
1249         if (error == 0) {
1250                 den = (struct dirent *)rbuf;
1251                 bytes = blksize - uio.uio_resid;
1252
1253                 while (bytes > 0) {
1254                         if (ncvp_debug >= 2) {
1255                                 printf("cache_inefficient_scan: %*.*s\n",
1256                                         den->d_namlen, den->d_namlen, 
1257                                         den->d_name);
1258                         }
1259                         if (den->d_type != DT_WHT &&
1260                             den->d_ino == vat.va_fileid) {
1261                                 if (ncvp_debug) {
1262                                         printf("cache_inefficient_scan: "
1263                                                "MATCHED inode %ld path %s/%*.*s\n",
1264                                                vat.va_fileid, ncp->nc_name,
1265                                                den->d_namlen, den->d_namlen,
1266                                                den->d_name);
1267                                 }
1268                                 nlc.nlc_nameptr = den->d_name;
1269                                 nlc.nlc_namelen = den->d_namlen;
1270                                 VOP_UNLOCK(pvp, 0);
1271                                 rncp = cache_nlookup(ncp, &nlc);
1272                                 KKASSERT(rncp != NULL);
1273                                 break;
1274                         }
1275                         bytes -= _DIRENT_DIRSIZ(den);
1276                         den = _DIRENT_NEXT(den);
1277                 }
1278                 if (rncp == NULL && eofflag == 0 && uio.uio_resid != blksize)
1279                         goto again;
1280         }
1281         if (rncp) {
1282                 vrele(pvp);
1283                 if (rncp->nc_flag & NCF_UNRESOLVED) {
1284                         cache_setvp(rncp, dvp);
1285                         if (ncvp_debug >= 2) {
1286                                 printf("cache_inefficient_scan: setvp %s/%s = %p\n",
1287                                         ncp->nc_name, rncp->nc_name, dvp);
1288                         }
1289                 } else {
1290                         if (ncvp_debug >= 2) {
1291                                 printf("cache_inefficient_scan: setvp %s/%s already set %p/%p\n", 
1292                                         ncp->nc_name, rncp->nc_name, dvp,
1293                                         rncp->nc_vp);
1294                         }
1295                 }
1296                 if (rncp->nc_vp == NULL)
1297                         error = rncp->nc_error;
1298                 cache_put(rncp);
1299         } else {
1300                 printf("cache_inefficient_scan: dvp %p NOT FOUND in %s\n",
1301                         dvp, ncp->nc_name);
1302                 vput(pvp);
1303                 error = ENOENT;
1304         }
1305         free(rbuf, M_TEMP);
1306         return (error);
1307 }
1308
1309 /*
1310  * Zap a namecache entry.  The ncp is unconditionally set to an unresolved
1311  * state, which disassociates it from its vnode or ncneglist.
1312  *
1313  * Then, if there are no additional references to the ncp and no children,
1314  * the ncp is removed from the topology and destroyed.  This function will
1315  * also run through the nc_parent chain and destroy parent ncps if possible.
1316  * As a side benefit, it turns out the only conditions that allow running
1317  * up the chain are also the conditions to ensure no deadlock will occur.
1318  *
1319  * References and/or children may exist if the ncp is in the middle of the
1320  * topology, preventing the ncp from being destroyed.
1321  *
1322  * This function must be called with the ncp held and locked and will unlock
1323  * and drop it during zapping.
1324  */
1325 static void
1326 cache_zap(struct namecache *ncp)
1327 {
1328         struct namecache *par;
1329
1330         /*
1331          * Disassociate the vnode or negative cache ref and set NCF_UNRESOLVED.
1332          */
1333         cache_setunresolved(ncp);
1334
1335         /*
1336          * Try to scrap the entry and possibly tail-recurse on its parent.
1337          * We only scrap unref'd (other then our ref) unresolved entries,
1338          * we do not scrap 'live' entries.
1339          */
1340         while (ncp->nc_flag & NCF_UNRESOLVED) {
1341                 /*
1342                  * Someone other then us has a ref, stop.
1343                  */
1344                 if (ncp->nc_refs > 1)
1345                         goto done;
1346
1347                 /*
1348                  * We have children, stop.
1349                  */
1350                 if (!TAILQ_EMPTY(&ncp->nc_list))
1351                         goto done;
1352
1353                 /*
1354                  * Remove ncp from the topology: hash table and parent linkage.
1355                  */
1356                 if (ncp->nc_flag & NCF_HASHED) {
1357                         ncp->nc_flag &= ~NCF_HASHED;
1358                         LIST_REMOVE(ncp, nc_hash);
1359                 }
1360                 if ((par = ncp->nc_parent) != NULL) {
1361                         par = cache_hold(par);
1362                         TAILQ_REMOVE(&par->nc_list, ncp, nc_entry);
1363                         ncp->nc_parent = NULL;
1364                         if (par->nc_vp && TAILQ_EMPTY(&par->nc_list))
1365                                 vdrop(par->nc_vp);
1366                 }
1367
1368                 /*
1369                  * ncp should not have picked up any refs.  Physically
1370                  * destroy the ncp.
1371                  */
1372                 KKASSERT(ncp->nc_refs == 1);
1373                 --numunres;
1374                 /* cache_unlock(ncp) not required */
1375                 ncp->nc_refs = -1;      /* safety */
1376                 if (ncp->nc_name)
1377                         free(ncp->nc_name, M_VFSCACHE);
1378                 free(ncp, M_VFSCACHE);
1379
1380                 /*
1381                  * Loop on the parent (it may be NULL).  Only bother looping
1382                  * if the parent has a single ref (ours), which also means
1383                  * we can lock it trivially.
1384                  */
1385                 ncp = par;
1386                 if (ncp == NULL)
1387                         return;
1388                 if (ncp->nc_refs != 1) {
1389                         cache_drop(ncp);
1390                         return;
1391                 }
1392                 KKASSERT(par->nc_exlocks == 0);
1393                 cache_lock(ncp);
1394         }
1395 done:
1396         cache_unlock(ncp);
1397         atomic_subtract_int(&ncp->nc_refs, 1);
1398 }
1399
1400 static enum { CHI_LOW, CHI_HIGH } cache_hysteresis_state = CHI_LOW;
1401
1402 static __inline
1403 void
1404 cache_hysteresis(void)
1405 {
1406         /*
1407          * Don't cache too many negative hits.  We use hysteresis to reduce
1408          * the impact on the critical path.
1409          */
1410         switch(cache_hysteresis_state) {
1411         case CHI_LOW:
1412                 if (numneg > MINNEG && numneg * ncnegfactor > numcache) {
1413                         cache_cleanneg(10);
1414                         cache_hysteresis_state = CHI_HIGH;
1415                 }
1416                 break;
1417         case CHI_HIGH:
1418                 if (numneg > MINNEG * 9 / 10 && 
1419                     numneg * ncnegfactor * 9 / 10 > numcache
1420                 ) {
1421                         cache_cleanneg(10);
1422                 } else {
1423                         cache_hysteresis_state = CHI_LOW;
1424                 }
1425                 break;
1426         }
1427 }
1428
1429 /*
1430  * NEW NAMECACHE LOOKUP API
1431  *
1432  * Lookup an entry in the cache.  A locked, referenced, non-NULL 
1433  * entry is *always* returned, even if the supplied component is illegal.
1434  * The resulting namecache entry should be returned to the system with
1435  * cache_put() or cache_unlock() + cache_drop().
1436  *
1437  * namecache locks are recursive but care must be taken to avoid lock order
1438  * reversals.
1439  *
1440  * Nobody else will be able to manipulate the associated namespace (e.g.
1441  * create, delete, rename, rename-target) until the caller unlocks the
1442  * entry.
1443  *
1444  * The returned entry will be in one of three states:  positive hit (non-null
1445  * vnode), negative hit (null vnode), or unresolved (NCF_UNRESOLVED is set).
1446  * Unresolved entries must be resolved through the filesystem to associate the
1447  * vnode and/or determine whether a positive or negative hit has occured.
1448  *
1449  * It is not necessary to lock a directory in order to lock namespace under
1450  * that directory.  In fact, it is explicitly not allowed to do that.  A
1451  * directory is typically only locked when being created, renamed, or
1452  * destroyed.
1453  *
1454  * The directory (par) may be unresolved, in which case any returned child
1455  * will likely also be marked unresolved.  Likely but not guarenteed.  Since
1456  * the filesystem lookup requires a resolved directory vnode the caller is
1457  * responsible for resolving the namecache chain top-down.  This API 
1458  * specifically allows whole chains to be created in an unresolved state.
1459  */
1460 struct namecache *
1461 cache_nlookup(struct namecache *par, struct nlcomponent *nlc)
1462 {
1463         struct namecache *ncp;
1464         struct namecache *new_ncp;
1465         struct nchashhead *nchpp;
1466         u_int32_t hash;
1467         globaldata_t gd;
1468
1469         numcalls++;
1470         gd = mycpu;
1471
1472         /*
1473          * Try to locate an existing entry
1474          */
1475         hash = fnv_32_buf(nlc->nlc_nameptr, nlc->nlc_namelen, FNV1_32_INIT);
1476         hash = fnv_32_buf(&par, sizeof(par), hash);
1477         new_ncp = NULL;
1478 restart:
1479         LIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
1480                 numchecks++;
1481
1482                 /*
1483                  * Zap entries that have timed out.
1484                  */
1485                 if (ncp->nc_timeout && 
1486                     (int)(ncp->nc_timeout - ticks) < 0 &&
1487                     (ncp->nc_flag & NCF_UNRESOLVED) == 0 &&
1488                     ncp->nc_exlocks == 0
1489                 ) {
1490                         cache_zap(cache_get(ncp));
1491                         goto restart;
1492                 }
1493
1494                 /*
1495                  * Break out if we find a matching entry.  Note that
1496                  * UNRESOLVED entries may match, but DESTROYED entries
1497                  * do not.
1498                  */
1499                 if (ncp->nc_parent == par &&
1500                     ncp->nc_nlen == nlc->nlc_namelen &&
1501                     bcmp(ncp->nc_name, nlc->nlc_nameptr, ncp->nc_nlen) == 0 &&
1502                     (ncp->nc_flag & NCF_DESTROYED) == 0
1503                 ) {
1504                         if (cache_get_nonblock(ncp) == 0) {
1505                                 if (new_ncp)
1506                                         cache_free(new_ncp);
1507                                 goto found;
1508                         }
1509                         cache_get(ncp);
1510                         cache_put(ncp);
1511                         goto restart;
1512                 }
1513         }
1514
1515         /*
1516          * We failed to locate an entry, create a new entry and add it to
1517          * the cache.  We have to relookup after possibly blocking in
1518          * malloc.
1519          */
1520         if (new_ncp == NULL) {
1521                 new_ncp = cache_alloc(nlc->nlc_namelen);
1522                 goto restart;
1523         }
1524
1525         ncp = new_ncp;
1526
1527         /*
1528          * Initialize as a new UNRESOLVED entry, lock (non-blocking),
1529          * and link to the parent.  The mount point is usually inherited
1530          * from the parent unless this is a special case such as a mount
1531          * point where nlc_namelen is 0.  The caller is responsible for
1532          * setting nc_mount in that case.  If nlc_namelen is 0 nc_name will
1533          * be NULL.
1534          */
1535         if (nlc->nlc_namelen) {
1536                 bcopy(nlc->nlc_nameptr, ncp->nc_name, nlc->nlc_namelen);
1537                 ncp->nc_name[nlc->nlc_namelen] = 0;
1538                 ncp->nc_mount = par->nc_mount;
1539         }
1540         nchpp = NCHHASH(hash);
1541         LIST_INSERT_HEAD(nchpp, ncp, nc_hash);
1542         ncp->nc_flag |= NCF_HASHED;
1543         cache_link_parent(ncp, par);
1544 found:
1545         /*
1546          * stats and namecache size management
1547          */
1548         if (ncp->nc_flag & NCF_UNRESOLVED)
1549                 ++gd->gd_nchstats->ncs_miss;
1550         else if (ncp->nc_vp)
1551                 ++gd->gd_nchstats->ncs_goodhits;
1552         else
1553                 ++gd->gd_nchstats->ncs_neghits;
1554         cache_hysteresis();
1555         return(ncp);
1556 }
1557
1558 /*
1559  * Given a locked ncp, validate that the vnode, if present, is actually
1560  * usable.  If it is not usable set the ncp to an unresolved state.
1561  */
1562 void
1563 cache_validate(struct namecache *ncp)
1564 {
1565         if ((ncp->nc_flag & NCF_UNRESOLVED) == 0) {
1566                 if (ncp->nc_vp && (ncp->nc_vp->v_flag & VRECLAIMED))
1567                         cache_setunresolved(ncp);
1568         }
1569 }
1570
1571 /*
1572  * Resolve an unresolved namecache entry, generally by looking it up.
1573  * The passed ncp must be locked and refd. 
1574  *
1575  * Theoretically since a vnode cannot be recycled while held, and since
1576  * the nc_parent chain holds its vnode as long as children exist, the
1577  * direct parent of the cache entry we are trying to resolve should
1578  * have a valid vnode.  If not then generate an error that we can 
1579  * determine is related to a resolver bug.
1580  *
1581  * However, if a vnode was in the middle of a recyclement when the NCP
1582  * got locked, ncp->nc_vp might point to a vnode that is about to become
1583  * invalid.  cache_resolve() handles this case by unresolving the entry
1584  * and then re-resolving it.
1585  *
1586  * Note that successful resolution does not necessarily return an error
1587  * code of 0.  If the ncp resolves to a negative cache hit then ENOENT
1588  * will be returned.
1589  */
1590 int
1591 cache_resolve(struct namecache *ncp, struct ucred *cred)
1592 {
1593         struct namecache *par;
1594         int error;
1595
1596 restart:
1597         /*
1598          * If the ncp is already resolved we have nothing to do.  However,
1599          * we do want to guarentee that a usable vnode is returned when
1600          * a vnode is present, so make sure it hasn't been reclaimed.
1601          */
1602         if ((ncp->nc_flag & NCF_UNRESOLVED) == 0) {
1603                 if (ncp->nc_vp && (ncp->nc_vp->v_flag & VRECLAIMED))
1604                         cache_setunresolved(ncp);
1605                 if ((ncp->nc_flag & NCF_UNRESOLVED) == 0)
1606                         return (ncp->nc_error);
1607         }
1608
1609         /*
1610          * Mount points need special handling because the parent does not
1611          * belong to the same filesystem as the ncp.
1612          */
1613         if (ncp->nc_flag & NCF_MOUNTPT)
1614                 return (cache_resolve_mp(ncp));
1615
1616         /*
1617          * We expect an unbroken chain of ncps to at least the mount point,
1618          * and even all the way to root (but this code doesn't have to go
1619          * past the mount point).
1620          */
1621         if (ncp->nc_parent == NULL) {
1622                 printf("EXDEV case 1 %p %*.*s\n", ncp,
1623                         ncp->nc_nlen, ncp->nc_nlen, ncp->nc_name);
1624                 ncp->nc_error = EXDEV;
1625                 return(ncp->nc_error);
1626         }
1627
1628         /*
1629          * The vp's of the parent directories in the chain are held via vhold()
1630          * due to the existance of the child, and should not disappear. 
1631          * However, there are cases where they can disappear:
1632          *
1633          *      - due to filesystem I/O errors.
1634          *      - due to NFS being stupid about tracking the namespace and
1635          *        destroys the namespace for entire directories quite often.
1636          *      - due to forced unmounts.
1637          *      - due to an rmdir (parent will be marked DESTROYED)
1638          *
1639          * When this occurs we have to track the chain backwards and resolve
1640          * it, looping until the resolver catches up to the current node.  We
1641          * could recurse here but we might run ourselves out of kernel stack
1642          * so we do it in a more painful manner.  This situation really should
1643          * not occur all that often, or if it does not have to go back too
1644          * many nodes to resolve the ncp.
1645          */
1646         while (ncp->nc_parent->nc_vp == NULL) {
1647                 /*
1648                  * This case can occur if a process is CD'd into a
1649                  * directory which is then rmdir'd.  If the parent is marked
1650                  * destroyed there is no point trying to resolve it.
1651                  */
1652                 if (ncp->nc_parent->nc_flag & NCF_DESTROYED)
1653                         return(ENOENT);
1654
1655                 par = ncp->nc_parent;
1656                 while (par->nc_parent && par->nc_parent->nc_vp == NULL)
1657                         par = par->nc_parent;
1658                 if (par->nc_parent == NULL) {
1659                         printf("EXDEV case 2 %*.*s\n",
1660                                 par->nc_nlen, par->nc_nlen, par->nc_name);
1661                         return (EXDEV);
1662                 }
1663                 printf("[diagnostic] cache_resolve: had to recurse on %*.*s\n",
1664                         par->nc_nlen, par->nc_nlen, par->nc_name);
1665                 /*
1666                  * The parent is not set in stone, ref and lock it to prevent
1667                  * it from disappearing.  Also note that due to renames it
1668                  * is possible for our ncp to move and for par to no longer
1669                  * be one of its parents.  We resolve it anyway, the loop 
1670                  * will handle any moves.
1671                  */
1672                 cache_get(par);
1673                 if (par->nc_flag & NCF_MOUNTPT) {
1674                         cache_resolve_mp(par);
1675                 } else if (par->nc_parent->nc_vp == NULL) {
1676                         printf("[diagnostic] cache_resolve: raced on %*.*s\n", par->nc_nlen, par->nc_nlen, par->nc_name);
1677                         cache_put(par);
1678                         continue;
1679                 } else if (par->nc_flag & NCF_UNRESOLVED) {
1680                         par->nc_error = VOP_NRESOLVE(par, cred);
1681                 }
1682                 if ((error = par->nc_error) != 0) {
1683                         if (par->nc_error != EAGAIN) {
1684                                 printf("EXDEV case 3 %*.*s error %d\n",
1685                                     par->nc_nlen, par->nc_nlen, par->nc_name,
1686                                     par->nc_error);
1687                                 cache_put(par);
1688                                 return(error);
1689                         }
1690                         printf("[diagnostic] cache_resolve: EAGAIN par %p %*.*s\n",
1691                                 par, par->nc_nlen, par->nc_nlen, par->nc_name);
1692                 }
1693                 cache_put(par);
1694                 /* loop */
1695         }
1696
1697         /*
1698          * Call VOP_NRESOLVE() to get the vp, then scan for any disconnected
1699          * ncp's and reattach them.  If this occurs the original ncp is marked
1700          * EAGAIN to force a relookup.
1701          *
1702          * NOTE: in order to call VOP_NRESOLVE(), the parent of the passed
1703          * ncp must already be resolved.
1704          */
1705         KKASSERT((ncp->nc_flag & NCF_MOUNTPT) == 0);
1706         ncp->nc_error = VOP_NRESOLVE(ncp, cred);
1707         /*vop_nresolve(*ncp->nc_parent->nc_vp->v_ops, ncp, cred);*/
1708         if (ncp->nc_error == EAGAIN) {
1709                 printf("[diagnostic] cache_resolve: EAGAIN ncp %p %*.*s\n",
1710                         ncp, ncp->nc_nlen, ncp->nc_nlen, ncp->nc_name);
1711                 goto restart;
1712         }
1713         return(ncp->nc_error);
1714 }
1715
1716 /*
1717  * Resolve the ncp associated with a mount point.  Such ncp's almost always
1718  * remain resolved and this routine is rarely called.  NFS MPs tends to force
1719  * re-resolution more often due to its mac-truck-smash-the-namecache
1720  * method of tracking namespace changes.
1721  *
1722  * The semantics for this call is that the passed ncp must be locked on
1723  * entry and will be locked on return.  However, if we actually have to
1724  * resolve the mount point we temporarily unlock the entry in order to
1725  * avoid race-to-root deadlocks due to e.g. dead NFS mounts.  Because of
1726  * the unlock we have to recheck the flags after we relock.
1727  */
1728 static int
1729 cache_resolve_mp(struct namecache *ncp)
1730 {
1731         struct vnode *vp;
1732         struct mount *mp = ncp->nc_mount;
1733         int error;
1734
1735         KKASSERT(mp != NULL);
1736
1737         /*
1738          * If the ncp is already resolved we have nothing to do.  However,
1739          * we do want to guarentee that a usable vnode is returned when
1740          * a vnode is present, so make sure it hasn't been reclaimed.
1741          */
1742         if ((ncp->nc_flag & NCF_UNRESOLVED) == 0) {
1743                 if (ncp->nc_vp && (ncp->nc_vp->v_flag & VRECLAIMED))
1744                         cache_setunresolved(ncp);
1745         }
1746
1747         if (ncp->nc_flag & NCF_UNRESOLVED) {
1748                 cache_unlock(ncp);
1749                 while (vfs_busy(mp, 0))
1750                         ;
1751                 error = VFS_ROOT(mp, &vp);
1752                 cache_lock(ncp);
1753
1754                 /*
1755                  * recheck the ncp state after relocking.
1756                  */
1757                 if (ncp->nc_flag & NCF_UNRESOLVED) {
1758                         ncp->nc_error = error;
1759                         if (error == 0) {
1760                                 cache_setvp(ncp, vp);
1761                                 vput(vp);
1762                         } else {
1763                                 printf("[diagnostic] cache_resolve_mp: failed to resolve mount %p\n", mp);
1764                                 cache_setvp(ncp, NULL);
1765                         }
1766                 } else if (error == 0) {
1767                         vput(vp);
1768                 }
1769                 vfs_unbusy(mp);
1770         }
1771         return(ncp->nc_error);
1772 }
1773
1774 void
1775 cache_cleanneg(int count)
1776 {
1777         struct namecache *ncp;
1778
1779         /*
1780          * Automode from the vnlru proc - clean out 10% of the negative cache
1781          * entries.
1782          */
1783         if (count == 0)
1784                 count = numneg / 10 + 1;
1785
1786         /*
1787          * Attempt to clean out the specified number of negative cache
1788          * entries.
1789          */
1790         while (count) {
1791                 ncp = TAILQ_FIRST(&ncneglist);
1792                 if (ncp == NULL) {
1793                         KKASSERT(numneg == 0);
1794                         break;
1795                 }
1796                 TAILQ_REMOVE(&ncneglist, ncp, nc_vnode);
1797                 TAILQ_INSERT_TAIL(&ncneglist, ncp, nc_vnode);
1798                 if (cache_get_nonblock(ncp) == 0)
1799                         cache_zap(ncp);
1800                 --count;
1801         }
1802 }
1803
1804 /*
1805  * Rehash a ncp.  Rehashing is typically required if the name changes (should
1806  * not generally occur) or the parent link changes.  This function will
1807  * unhash the ncp if the ncp is no longer hashable.
1808  */
1809 static void
1810 cache_rehash(struct namecache *ncp)
1811 {
1812         struct nchashhead *nchpp;
1813         u_int32_t hash;
1814
1815         if (ncp->nc_flag & NCF_HASHED) {
1816                 ncp->nc_flag &= ~NCF_HASHED;
1817                 LIST_REMOVE(ncp, nc_hash);
1818         }
1819         if (ncp->nc_nlen && ncp->nc_parent) {
1820                 hash = fnv_32_buf(ncp->nc_name, ncp->nc_nlen, FNV1_32_INIT);
1821                 hash = fnv_32_buf(&ncp->nc_parent, 
1822                                         sizeof(ncp->nc_parent), hash);
1823                 nchpp = NCHHASH(hash);
1824                 LIST_INSERT_HEAD(nchpp, ncp, nc_hash);
1825                 ncp->nc_flag |= NCF_HASHED;
1826         }
1827 }
1828
1829 /*
1830  * Name cache initialization, from vfsinit() when we are booting
1831  */
1832 void
1833 nchinit(void)
1834 {
1835         int i;
1836         globaldata_t gd;
1837
1838         /* initialise per-cpu namecache effectiveness statistics. */
1839         for (i = 0; i < ncpus; ++i) {
1840                 gd = globaldata_find(i);
1841                 gd->gd_nchstats = &nchstats[i];
1842         }
1843         TAILQ_INIT(&ncneglist);
1844         nchashtbl = hashinit(desiredvnodes*2, M_VFSCACHE, &nchash);
1845         nclockwarn = 1 * hz;
1846 }
1847
1848 /*
1849  * Called from start_init() to bootstrap the root filesystem.  Returns
1850  * a referenced, unlocked namecache record.
1851  */
1852 struct namecache *
1853 cache_allocroot(struct mount *mp, struct vnode *vp)
1854 {
1855         struct namecache *ncp = cache_alloc(0);
1856
1857         ncp->nc_flag |= NCF_MOUNTPT | NCF_ROOT;
1858         ncp->nc_mount = mp;
1859         cache_setvp(ncp, vp);
1860         return(ncp);
1861 }
1862
1863 /*
1864  * vfs_cache_setroot()
1865  *
1866  *      Create an association between the root of our namecache and
1867  *      the root vnode.  This routine may be called several times during
1868  *      booting.
1869  *
1870  *      If the caller intends to save the returned namecache pointer somewhere
1871  *      it must cache_hold() it.
1872  */
1873 void
1874 vfs_cache_setroot(struct vnode *nvp, struct namecache *ncp)
1875 {
1876         struct vnode *ovp;
1877         struct namecache *oncp;
1878
1879         ovp = rootvnode;
1880         oncp = rootncp;
1881         rootvnode = nvp;
1882         rootncp = ncp;
1883
1884         if (ovp)
1885                 vrele(ovp);
1886         if (oncp)
1887                 cache_drop(oncp);
1888 }
1889
1890 /*
1891  * XXX OLD API COMPAT FUNCTION.  This really messes up the new namecache
1892  * topology and is being removed as quickly as possible.  The new VOP_N*()
1893  * API calls are required to make specific adjustments using the supplied
1894  * ncp pointers rather then just bogusly purging random vnodes.
1895  *
1896  * Invalidate all namecache entries to a particular vnode as well as 
1897  * any direct children of that vnode in the namecache.  This is a 
1898  * 'catch all' purge used by filesystems that do not know any better.
1899  *
1900  * A new vnode v_id is generated.  Note that no vnode will ever have a
1901  * v_id of 0.
1902  *
1903  * Note that the linkage between the vnode and its namecache entries will
1904  * be removed, but the namecache entries themselves might stay put due to
1905  * active references from elsewhere in the system or due to the existance of
1906  * the children.   The namecache topology is left intact even if we do not
1907  * know what the vnode association is.  Such entries will be marked
1908  * NCF_UNRESOLVED.
1909  *
1910  * XXX: Only time and the size of v_id prevents this from failing:
1911  * XXX: In theory we should hunt down all (struct vnode*, v_id)
1912  * XXX: soft references and nuke them, at least on the global
1913  * XXX: v_id wraparound.  The period of resistance can be extended
1914  * XXX: by incrementing each vnodes v_id individually instead of
1915  * XXX: using the global v_id.
1916  */
1917 void
1918 cache_purge(struct vnode *vp)
1919 {
1920         static u_long nextid;
1921
1922         cache_inval_vp(vp, CINV_DESTROY | CINV_CHILDREN);
1923
1924         /*
1925          * Calculate a new unique id for ".." handling
1926          */
1927         do {
1928                 nextid++;
1929         } while (nextid == vp->v_id || nextid == 0);
1930         vp->v_id = nextid;
1931 }
1932
1933 /*
1934  * Flush all entries referencing a particular filesystem.
1935  *
1936  * Since we need to check it anyway, we will flush all the invalid
1937  * entries at the same time.
1938  */
1939 void
1940 cache_purgevfs(struct mount *mp)
1941 {
1942         struct nchashhead *nchpp;
1943         struct namecache *ncp, *nnp;
1944
1945         /*
1946          * Scan hash tables for applicable entries.
1947          */
1948         for (nchpp = &nchashtbl[nchash]; nchpp >= nchashtbl; nchpp--) {
1949                 ncp = LIST_FIRST(nchpp);
1950                 if (ncp)
1951                         cache_hold(ncp);
1952                 while (ncp) {
1953                         nnp = LIST_NEXT(ncp, nc_hash);
1954                         if (nnp)
1955                                 cache_hold(nnp);
1956                         if (ncp->nc_mount == mp) {
1957                                 cache_lock(ncp);
1958                                 cache_zap(ncp);
1959                         } else {
1960                                 cache_drop(ncp);
1961                         }
1962                         ncp = nnp;
1963                 }
1964         }
1965 }
1966
1967 /*
1968  * Create a new (theoretically) unique fsmid
1969  */
1970 int64_t
1971 cache_getnewfsmid(void)
1972 {
1973         static int fsmid_roller;
1974         int64_t fsmid;
1975
1976         ++fsmid_roller;
1977         fsmid = ((int64_t)time_second << 32) |
1978                         (fsmid_roller & 0x7FFFFFFF);
1979         return (fsmid);
1980 }
1981
1982
1983 static int disablecwd;
1984 SYSCTL_INT(_debug, OID_AUTO, disablecwd, CTLFLAG_RW, &disablecwd, 0, "");
1985
1986 static u_long numcwdcalls; STATNODE(CTLFLAG_RD, numcwdcalls, &numcwdcalls);
1987 static u_long numcwdfail1; STATNODE(CTLFLAG_RD, numcwdfail1, &numcwdfail1);
1988 static u_long numcwdfail2; STATNODE(CTLFLAG_RD, numcwdfail2, &numcwdfail2);
1989 static u_long numcwdfail3; STATNODE(CTLFLAG_RD, numcwdfail3, &numcwdfail3);
1990 static u_long numcwdfail4; STATNODE(CTLFLAG_RD, numcwdfail4, &numcwdfail4);
1991 static u_long numcwdfound; STATNODE(CTLFLAG_RD, numcwdfound, &numcwdfound);
1992
1993 int
1994 __getcwd(struct __getcwd_args *uap)
1995 {
1996         int buflen;
1997         int error;
1998         char *buf;
1999         char *bp;
2000
2001         if (disablecwd)
2002                 return (ENODEV);
2003
2004         buflen = uap->buflen;
2005         if (buflen < 2)
2006                 return (EINVAL);
2007         if (buflen > MAXPATHLEN)
2008                 buflen = MAXPATHLEN;
2009
2010         buf = malloc(buflen, M_TEMP, M_WAITOK);
2011         bp = kern_getcwd(buf, buflen, &error);
2012         if (error == 0)
2013                 error = copyout(bp, uap->buf, strlen(bp) + 1);
2014         free(buf, M_TEMP);
2015         return (error);
2016 }
2017
2018 char *
2019 kern_getcwd(char *buf, size_t buflen, int *error)
2020 {
2021         struct proc *p = curproc;
2022         char *bp;
2023         int i, slash_prefixed;
2024         struct filedesc *fdp;
2025         struct namecache *ncp;
2026
2027         numcwdcalls++;
2028         bp = buf;
2029         bp += buflen - 1;
2030         *bp = '\0';
2031         fdp = p->p_fd;
2032         slash_prefixed = 0;
2033
2034         ncp = fdp->fd_ncdir;
2035         while (ncp && ncp != fdp->fd_nrdir && (ncp->nc_flag & NCF_ROOT) == 0) {
2036                 if (ncp->nc_flag & NCF_MOUNTPT) {
2037                         if (ncp->nc_mount == NULL) {
2038                                 *error = EBADF;         /* forced unmount? */
2039                                 return(NULL);
2040                         }
2041                         ncp = ncp->nc_parent;
2042                         continue;
2043                 }
2044                 for (i = ncp->nc_nlen - 1; i >= 0; i--) {
2045                         if (bp == buf) {
2046                                 numcwdfail4++;
2047                                 *error = ENOMEM;
2048                                 return(NULL);
2049                         }
2050                         *--bp = ncp->nc_name[i];
2051                 }
2052                 if (bp == buf) {
2053                         numcwdfail4++;
2054                         *error = ENOMEM;
2055                         return(NULL);
2056                 }
2057                 *--bp = '/';
2058                 slash_prefixed = 1;
2059                 ncp = ncp->nc_parent;
2060         }
2061         if (ncp == NULL) {
2062                 numcwdfail2++;
2063                 *error = ENOENT;
2064                 return(NULL);
2065         }
2066         if (!slash_prefixed) {
2067                 if (bp == buf) {
2068                         numcwdfail4++;
2069                         *error = ENOMEM;
2070                         return(NULL);
2071                 }
2072                 *--bp = '/';
2073         }
2074         numcwdfound++;
2075         *error = 0;
2076         return (bp);
2077 }
2078
2079 /*
2080  * Thus begins the fullpath magic.
2081  */
2082
2083 #undef STATNODE
2084 #define STATNODE(name)                                                  \
2085         static u_int name;                                              \
2086         SYSCTL_UINT(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, 0, "")
2087
2088 static int disablefullpath;
2089 SYSCTL_INT(_debug, OID_AUTO, disablefullpath, CTLFLAG_RW,
2090     &disablefullpath, 0, "");
2091
2092 STATNODE(numfullpathcalls);
2093 STATNODE(numfullpathfail1);
2094 STATNODE(numfullpathfail2);
2095 STATNODE(numfullpathfail3);
2096 STATNODE(numfullpathfail4);
2097 STATNODE(numfullpathfound);
2098
2099 int
2100 cache_fullpath(struct proc *p, struct namecache *ncp, char **retbuf, char **freebuf)
2101 {
2102         char *bp, *buf;
2103         int i, slash_prefixed;
2104         struct namecache *fd_nrdir;
2105
2106         numfullpathcalls--;
2107
2108         buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
2109         bp = buf + MAXPATHLEN - 1;
2110         *bp = '\0';
2111         if (p != NULL)
2112                 fd_nrdir = p->p_fd->fd_nrdir;
2113         else
2114                 fd_nrdir = NULL;
2115         slash_prefixed = 0;
2116         while (ncp && ncp != fd_nrdir && (ncp->nc_flag & NCF_ROOT) == 0) {
2117                 if (ncp->nc_flag & NCF_MOUNTPT) {
2118                         if (ncp->nc_mount == NULL) {
2119                                 free(buf, M_TEMP);
2120                                 return(EBADF);
2121                         }
2122                         ncp = ncp->nc_parent;
2123                         continue;
2124                 }
2125                 for (i = ncp->nc_nlen - 1; i >= 0; i--) {
2126                         if (bp == buf) {
2127                                 numfullpathfail4++;
2128                                 free(buf, M_TEMP);
2129                                 return(ENOMEM);
2130                         }
2131                         *--bp = ncp->nc_name[i];
2132                 }
2133                 if (bp == buf) {
2134                         numfullpathfail4++;
2135                         free(buf, M_TEMP);
2136                         return(ENOMEM);
2137                 }
2138                 *--bp = '/';
2139                 slash_prefixed = 1;
2140                 ncp = ncp->nc_parent;
2141         }
2142         if (ncp == NULL) {
2143                 numfullpathfail2++;
2144                 free(buf, M_TEMP);
2145                 return(ENOENT);
2146         }
2147         if (p != NULL && (ncp->nc_flag & NCF_ROOT) && ncp != fd_nrdir) {
2148                 bp = buf + MAXPATHLEN - 1;
2149                 *bp = '\0';
2150                 slash_prefixed = 0;
2151         }
2152         if (!slash_prefixed) {
2153                 if (bp == buf) {
2154                         numfullpathfail4++;
2155                         free(buf, M_TEMP);
2156                         return(ENOMEM);
2157                 }
2158                 *--bp = '/';
2159         }
2160         numfullpathfound++;
2161         *retbuf = bp; 
2162         *freebuf = buf;
2163
2164         return(0);
2165 }
2166
2167 int
2168 vn_fullpath(struct proc *p, struct vnode *vn, char **retbuf, char **freebuf) 
2169 {
2170         struct namecache *ncp;
2171
2172         numfullpathcalls++;
2173         if (disablefullpath)
2174                 return (ENODEV);
2175
2176         if (p == NULL)
2177                 return (EINVAL);
2178
2179         /* vn is NULL, client wants us to use p->p_textvp */
2180         if (vn == NULL) {
2181                 if ((vn = p->p_textvp) == NULL)
2182                         return (EINVAL);
2183         }
2184         TAILQ_FOREACH(ncp, &vn->v_namecache, nc_vnode) {
2185                 if (ncp->nc_nlen)
2186                         break;
2187         }
2188         if (ncp == NULL)
2189                 return (EINVAL);
2190
2191         numfullpathcalls--;
2192         return(cache_fullpath(p, ncp, retbuf, freebuf));
2193 }