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