vfs_cache.c revision 178046
1/*-
2 * Copyright (c) 1989, 1993, 1995
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Poul-Henning Kamp of the FreeBSD Project.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 *	@(#)vfs_cache.c	8.5 (Berkeley) 3/22/95
33 */
34
35#include <sys/cdefs.h>
36__FBSDID("$FreeBSD: head/sys/kern/vfs_cache.c 178046 2008-04-09 20:19:55Z pjd $");
37
38#include <sys/param.h>
39#include <sys/systm.h>
40#include <sys/kernel.h>
41#include <sys/lock.h>
42#include <sys/mutex.h>
43#include <sys/sysctl.h>
44#include <sys/mount.h>
45#include <sys/vnode.h>
46#include <sys/namei.h>
47#include <sys/malloc.h>
48#include <sys/syscallsubr.h>
49#include <sys/sysproto.h>
50#include <sys/proc.h>
51#include <sys/filedesc.h>
52#include <sys/fnv_hash.h>
53
54#include <vm/uma.h>
55
56/*
57 * This structure describes the elements in the cache of recent
58 * names looked up by namei.
59 */
60
61struct	namecache {
62	LIST_ENTRY(namecache) nc_hash;	/* hash chain */
63	LIST_ENTRY(namecache) nc_src;	/* source vnode list */
64	TAILQ_ENTRY(namecache) nc_dst;	/* destination vnode list */
65	struct	vnode *nc_dvp;		/* vnode of parent of name */
66	struct	vnode *nc_vp;		/* vnode the name refers to */
67	u_char	nc_flag;		/* flag bits */
68	u_char	nc_nlen;		/* length of name */
69	char	nc_name[0];		/* segment name */
70};
71
72/*
73 * Name caching works as follows:
74 *
75 * Names found by directory scans are retained in a cache
76 * for future reference.  It is managed LRU, so frequently
77 * used names will hang around.  Cache is indexed by hash value
78 * obtained from (vp, name) where vp refers to the directory
79 * containing name.
80 *
81 * If it is a "negative" entry, (i.e. for a name that is known NOT to
82 * exist) the vnode pointer will be NULL.
83 *
84 * Upon reaching the last segment of a path, if the reference
85 * is for DELETE, or NOCACHE is set (rewrite), and the
86 * name is located in the cache, it will be dropped.
87 */
88
89/*
90 * Structures associated with name cacheing.
91 */
92#define NCHHASH(hash) \
93	(&nchashtbl[(hash) & nchash])
94static LIST_HEAD(nchashhead, namecache) *nchashtbl;	/* Hash Table */
95static TAILQ_HEAD(, namecache) ncneg;	/* Hash Table */
96static u_long	nchash;			/* size of hash table */
97SYSCTL_ULONG(_debug, OID_AUTO, nchash, CTLFLAG_RD, &nchash, 0, "");
98static u_long	ncnegfactor = 16;	/* ratio of negative entries */
99SYSCTL_ULONG(_debug, OID_AUTO, ncnegfactor, CTLFLAG_RW, &ncnegfactor, 0, "");
100static u_long	numneg;			/* number of cache entries allocated */
101SYSCTL_ULONG(_debug, OID_AUTO, numneg, CTLFLAG_RD, &numneg, 0, "");
102static u_long	numcache;		/* number of cache entries allocated */
103SYSCTL_ULONG(_debug, OID_AUTO, numcache, CTLFLAG_RD, &numcache, 0, "");
104static u_long	numcachehv;		/* number of cache entries with vnodes held */
105SYSCTL_ULONG(_debug, OID_AUTO, numcachehv, CTLFLAG_RD, &numcachehv, 0, "");
106#if 0
107static u_long	numcachepl;		/* number of cache purge for leaf entries */
108SYSCTL_ULONG(_debug, OID_AUTO, numcachepl, CTLFLAG_RD, &numcachepl, 0, "");
109#endif
110struct	nchstats nchstats;		/* cache effectiveness statistics */
111
112static struct mtx cache_lock;
113MTX_SYSINIT(vfscache, &cache_lock, "Name Cache", MTX_DEF);
114
115#define	CACHE_LOCK()	mtx_lock(&cache_lock)
116#define	CACHE_UNLOCK()	mtx_unlock(&cache_lock)
117
118/*
119 * UMA zones for the VFS cache.
120 *
121 * The small cache is used for entries with short names, which are the
122 * most common.  The large cache is used for entries which are too big to
123 * fit in the small cache.
124 */
125static uma_zone_t cache_zone_small;
126static uma_zone_t cache_zone_large;
127
128#define	CACHE_PATH_CUTOFF	32
129#define	CACHE_ZONE_SMALL	(sizeof(struct namecache) + CACHE_PATH_CUTOFF)
130#define	CACHE_ZONE_LARGE	(sizeof(struct namecache) + NAME_MAX)
131
132#define cache_alloc(len)	uma_zalloc(((len) <= CACHE_PATH_CUTOFF) ? \
133	cache_zone_small : cache_zone_large, M_WAITOK)
134#define cache_free(ncp)		do { \
135	if (ncp != NULL) \
136		uma_zfree(((ncp)->nc_nlen <= CACHE_PATH_CUTOFF) ? \
137		    cache_zone_small : cache_zone_large, (ncp)); \
138} while (0)
139
140static int	doingcache = 1;		/* 1 => enable the cache */
141SYSCTL_INT(_debug, OID_AUTO, vfscache, CTLFLAG_RW, &doingcache, 0, "");
142
143/* Export size information to userland */
144SYSCTL_INT(_debug_sizeof, OID_AUTO, namecache, CTLFLAG_RD, 0,
145	sizeof(struct namecache), "");
146
147/*
148 * The new name cache statistics
149 */
150static SYSCTL_NODE(_vfs, OID_AUTO, cache, CTLFLAG_RW, 0, "Name cache statistics");
151#define STATNODE(mode, name, var) \
152	SYSCTL_ULONG(_vfs_cache, OID_AUTO, name, mode, var, 0, "");
153STATNODE(CTLFLAG_RD, numneg, &numneg);
154STATNODE(CTLFLAG_RD, numcache, &numcache);
155static u_long numcalls; STATNODE(CTLFLAG_RD, numcalls, &numcalls);
156static u_long dothits; STATNODE(CTLFLAG_RD, dothits, &dothits);
157static u_long dotdothits; STATNODE(CTLFLAG_RD, dotdothits, &dotdothits);
158static u_long numchecks; STATNODE(CTLFLAG_RD, numchecks, &numchecks);
159static u_long nummiss; STATNODE(CTLFLAG_RD, nummiss, &nummiss);
160static u_long nummisszap; STATNODE(CTLFLAG_RD, nummisszap, &nummisszap);
161static u_long numposzaps; STATNODE(CTLFLAG_RD, numposzaps, &numposzaps);
162static u_long numposhits; STATNODE(CTLFLAG_RD, numposhits, &numposhits);
163static u_long numnegzaps; STATNODE(CTLFLAG_RD, numnegzaps, &numnegzaps);
164static u_long numneghits; STATNODE(CTLFLAG_RD, numneghits, &numneghits);
165
166SYSCTL_OPAQUE(_vfs_cache, OID_AUTO, nchstats, CTLFLAG_RD, &nchstats,
167	sizeof(nchstats), "LU", "VFS cache effectiveness statistics");
168
169
170
171static void cache_zap(struct namecache *ncp);
172static int vn_fullpath1(struct thread *td, struct vnode *vp, struct vnode *rdir,
173    char *buf, char **retbuf, u_int buflen);
174
175static MALLOC_DEFINE(M_VFSCACHE, "vfscache", "VFS name cache entries");
176
177/*
178 * Flags in namecache.nc_flag
179 */
180#define NCF_WHITE	1
181
182/*
183 * Grab an atomic snapshot of the name cache hash chain lengths
184 */
185SYSCTL_NODE(_debug, OID_AUTO, hashstat, CTLFLAG_RW, NULL, "hash table stats");
186
187static int
188sysctl_debug_hashstat_rawnchash(SYSCTL_HANDLER_ARGS)
189{
190	int error;
191	struct nchashhead *ncpp;
192	struct namecache *ncp;
193	int n_nchash;
194	int count;
195
196	n_nchash = nchash + 1;	/* nchash is max index, not count */
197	if (!req->oldptr)
198		return SYSCTL_OUT(req, 0, n_nchash * sizeof(int));
199
200	/* Scan hash tables for applicable entries */
201	for (ncpp = nchashtbl; n_nchash > 0; n_nchash--, ncpp++) {
202		count = 0;
203		LIST_FOREACH(ncp, ncpp, nc_hash) {
204			count++;
205		}
206		error = SYSCTL_OUT(req, &count, sizeof(count));
207		if (error)
208			return (error);
209	}
210	return (0);
211}
212SYSCTL_PROC(_debug_hashstat, OID_AUTO, rawnchash, CTLTYPE_INT|CTLFLAG_RD,
213	0, 0, sysctl_debug_hashstat_rawnchash, "S,int", "nchash chain lengths");
214
215static int
216sysctl_debug_hashstat_nchash(SYSCTL_HANDLER_ARGS)
217{
218	int error;
219	struct nchashhead *ncpp;
220	struct namecache *ncp;
221	int n_nchash;
222	int count, maxlength, used, pct;
223
224	if (!req->oldptr)
225		return SYSCTL_OUT(req, 0, 4 * sizeof(int));
226
227	n_nchash = nchash + 1;	/* nchash is max index, not count */
228	used = 0;
229	maxlength = 0;
230
231	/* Scan hash tables for applicable entries */
232	for (ncpp = nchashtbl; n_nchash > 0; n_nchash--, ncpp++) {
233		count = 0;
234		LIST_FOREACH(ncp, ncpp, nc_hash) {
235			count++;
236		}
237		if (count)
238			used++;
239		if (maxlength < count)
240			maxlength = count;
241	}
242	n_nchash = nchash + 1;
243	pct = (used * 100 * 100) / n_nchash;
244	error = SYSCTL_OUT(req, &n_nchash, sizeof(n_nchash));
245	if (error)
246		return (error);
247	error = SYSCTL_OUT(req, &used, sizeof(used));
248	if (error)
249		return (error);
250	error = SYSCTL_OUT(req, &maxlength, sizeof(maxlength));
251	if (error)
252		return (error);
253	error = SYSCTL_OUT(req, &pct, sizeof(pct));
254	if (error)
255		return (error);
256	return (0);
257}
258SYSCTL_PROC(_debug_hashstat, OID_AUTO, nchash, CTLTYPE_INT|CTLFLAG_RD,
259	0, 0, sysctl_debug_hashstat_nchash, "I", "nchash chain lengths");
260
261/*
262 * cache_zap():
263 *
264 *   Removes a namecache entry from cache, whether it contains an actual
265 *   pointer to a vnode or if it is just a negative cache entry.
266 */
267static void
268cache_zap(ncp)
269	struct namecache *ncp;
270{
271	struct vnode *vp;
272
273	mtx_assert(&cache_lock, MA_OWNED);
274	CTR2(KTR_VFS, "cache_zap(%p) vp %p", ncp, ncp->nc_vp);
275	vp = NULL;
276	LIST_REMOVE(ncp, nc_hash);
277	LIST_REMOVE(ncp, nc_src);
278	if (LIST_EMPTY(&ncp->nc_dvp->v_cache_src)) {
279		vp = ncp->nc_dvp;
280		numcachehv--;
281	}
282	if (ncp->nc_vp) {
283		TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst, ncp, nc_dst);
284		ncp->nc_vp->v_dd = NULL;
285	} else {
286		TAILQ_REMOVE(&ncneg, ncp, nc_dst);
287		numneg--;
288	}
289	numcache--;
290	cache_free(ncp);
291	if (vp)
292		vdrop(vp);
293}
294
295/*
296 * Lookup an entry in the cache
297 *
298 * Lookup is called with dvp pointing to the directory to search,
299 * cnp pointing to the name of the entry being sought. If the lookup
300 * succeeds, the vnode is returned in *vpp, and a status of -1 is
301 * returned. If the lookup determines that the name does not exist
302 * (negative cacheing), a status of ENOENT is returned. If the lookup
303 * fails, a status of zero is returned.
304 *
305 * vpp is locked and ref'd on return.  If we're looking up DOTDOT, dvp is
306 * unlocked.  If we're looking up . an extra ref is taken, but the lock is
307 * not recursively acquired.
308 */
309
310int
311cache_lookup(dvp, vpp, cnp)
312	struct vnode *dvp;
313	struct vnode **vpp;
314	struct componentname *cnp;
315{
316	struct namecache *ncp;
317	u_int32_t hash;
318	int error, ltype;
319
320	if (!doingcache) {
321		cnp->cn_flags &= ~MAKEENTRY;
322		return (0);
323	}
324retry:
325	CACHE_LOCK();
326	numcalls++;
327
328	if (cnp->cn_nameptr[0] == '.') {
329		if (cnp->cn_namelen == 1) {
330			*vpp = dvp;
331			CTR2(KTR_VFS, "cache_lookup(%p, %s) found via .",
332			    dvp, cnp->cn_nameptr);
333			dothits++;
334			goto success;
335		}
336		if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
337			dotdothits++;
338			if (dvp->v_dd == NULL ||
339			    (cnp->cn_flags & MAKEENTRY) == 0) {
340				CACHE_UNLOCK();
341				return (0);
342			}
343			*vpp = dvp->v_dd;
344			CTR3(KTR_VFS, "cache_lookup(%p, %s) found %p via ..",
345			    dvp, cnp->cn_nameptr, *vpp);
346			goto success;
347		}
348	}
349
350	hash = fnv_32_buf(cnp->cn_nameptr, cnp->cn_namelen, FNV1_32_INIT);
351	hash = fnv_32_buf(&dvp, sizeof(dvp), hash);
352	LIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
353		numchecks++;
354		if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
355		    !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
356			break;
357	}
358
359	/* We failed to find an entry */
360	if (ncp == 0) {
361		if ((cnp->cn_flags & MAKEENTRY) == 0) {
362			nummisszap++;
363		} else {
364			nummiss++;
365		}
366		nchstats.ncs_miss++;
367		CACHE_UNLOCK();
368		return (0);
369	}
370
371	/* We don't want to have an entry, so dump it */
372	if ((cnp->cn_flags & MAKEENTRY) == 0) {
373		numposzaps++;
374		nchstats.ncs_badhits++;
375		cache_zap(ncp);
376		CACHE_UNLOCK();
377		return (0);
378	}
379
380	/* We found a "positive" match, return the vnode */
381	if (ncp->nc_vp) {
382		numposhits++;
383		nchstats.ncs_goodhits++;
384		*vpp = ncp->nc_vp;
385		CTR4(KTR_VFS, "cache_lookup(%p, %s) found %p via ncp %p",
386		    dvp, cnp->cn_nameptr, *vpp, ncp);
387		goto success;
388	}
389
390	/* We found a negative match, and want to create it, so purge */
391	if (cnp->cn_nameiop == CREATE) {
392		numnegzaps++;
393		nchstats.ncs_badhits++;
394		cache_zap(ncp);
395		CACHE_UNLOCK();
396		return (0);
397	}
398
399	numneghits++;
400	/*
401	 * We found a "negative" match, so we shift it to the end of
402	 * the "negative" cache entries queue to satisfy LRU.  Also,
403	 * check to see if the entry is a whiteout; indicate this to
404	 * the componentname, if so.
405	 */
406	TAILQ_REMOVE(&ncneg, ncp, nc_dst);
407	TAILQ_INSERT_TAIL(&ncneg, ncp, nc_dst);
408	nchstats.ncs_neghits++;
409	if (ncp->nc_flag & NCF_WHITE)
410		cnp->cn_flags |= ISWHITEOUT;
411	CACHE_UNLOCK();
412	return (ENOENT);
413
414success:
415	/*
416	 * On success we return a locked and ref'd vnode as per the lookup
417	 * protocol.
418	 */
419	if (dvp == *vpp) {   /* lookup on "." */
420		VREF(*vpp);
421		CACHE_UNLOCK();
422		/*
423		 * When we lookup "." we still can be asked to lock it
424		 * differently...
425		 */
426		ltype = cnp->cn_lkflags & LK_TYPE_MASK;
427		if (ltype == VOP_ISLOCKED(*vpp))
428			return (-1);
429		else if (ltype == LK_EXCLUSIVE)
430			vn_lock(*vpp, LK_UPGRADE | LK_RETRY);
431		return (-1);
432	}
433	ltype = 0;	/* silence gcc warning */
434	if (cnp->cn_flags & ISDOTDOT) {
435		ltype = VOP_ISLOCKED(dvp);
436		VOP_UNLOCK(dvp, 0);
437	}
438	VI_LOCK(*vpp);
439	CACHE_UNLOCK();
440	error = vget(*vpp, cnp->cn_lkflags | LK_INTERLOCK, cnp->cn_thread);
441	if (cnp->cn_flags & ISDOTDOT)
442		vn_lock(dvp, ltype | LK_RETRY);
443	if (error) {
444		*vpp = NULL;
445		goto retry;
446	}
447	if ((cnp->cn_flags & ISLASTCN) &&
448	    (cnp->cn_lkflags & LK_TYPE_MASK) == LK_EXCLUSIVE) {
449		ASSERT_VOP_ELOCKED(*vpp, "cache_lookup");
450	}
451	return (-1);
452}
453
454/*
455 * Add an entry to the cache.
456 */
457void
458cache_enter(dvp, vp, cnp)
459	struct vnode *dvp;
460	struct vnode *vp;
461	struct componentname *cnp;
462{
463	struct namecache *ncp;
464	struct nchashhead *ncpp;
465	u_int32_t hash;
466	int hold;
467	int zap;
468	int len;
469
470	CTR3(KTR_VFS, "cache_enter(%p, %p, %s)", dvp, vp, cnp->cn_nameptr);
471	VNASSERT(vp == NULL || (vp->v_iflag & VI_DOOMED) == 0, vp,
472	    ("cahe_enter: Adding a doomed vnode"));
473
474	if (!doingcache)
475		return;
476
477	if (cnp->cn_nameptr[0] == '.') {
478		if (cnp->cn_namelen == 1) {
479			return;
480		}
481		/*
482		 * For dotdot lookups only cache the v_dd pointer if the
483		 * directory has a link back to its parent via v_cache_dst.
484		 * Without this an unlinked directory would keep a soft
485		 * reference to its parent which could not be NULLd at
486		 * cache_purge() time.
487		 */
488		if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
489			CACHE_LOCK();
490			if (!TAILQ_EMPTY(&dvp->v_cache_dst))
491				dvp->v_dd = vp;
492			CACHE_UNLOCK();
493			return;
494		}
495	}
496
497	hold = 0;
498	zap = 0;
499	ncp = cache_alloc(cnp->cn_namelen);
500	CACHE_LOCK();
501	numcache++;
502	if (!vp) {
503		numneg++;
504		ncp->nc_flag = cnp->cn_flags & ISWHITEOUT ? NCF_WHITE : 0;
505	} else if (vp->v_type == VDIR) {
506		vp->v_dd = dvp;
507	} else {
508		vp->v_dd = NULL;
509	}
510
511	/*
512	 * Set the rest of the namecache entry elements, calculate it's
513	 * hash key and insert it into the appropriate chain within
514	 * the cache entries table.
515	 */
516	ncp->nc_vp = vp;
517	ncp->nc_dvp = dvp;
518	len = ncp->nc_nlen = cnp->cn_namelen;
519	hash = fnv_32_buf(cnp->cn_nameptr, len, FNV1_32_INIT);
520	bcopy(cnp->cn_nameptr, ncp->nc_name, len);
521	hash = fnv_32_buf(&dvp, sizeof(dvp), hash);
522	ncpp = NCHHASH(hash);
523	LIST_INSERT_HEAD(ncpp, ncp, nc_hash);
524	if (LIST_EMPTY(&dvp->v_cache_src)) {
525		hold = 1;
526		numcachehv++;
527	}
528	LIST_INSERT_HEAD(&dvp->v_cache_src, ncp, nc_src);
529	/*
530	 * If the entry is "negative", we place it into the
531	 * "negative" cache queue, otherwise, we place it into the
532	 * destination vnode's cache entries queue.
533	 */
534	if (vp) {
535		TAILQ_INSERT_HEAD(&vp->v_cache_dst, ncp, nc_dst);
536	} else {
537		TAILQ_INSERT_TAIL(&ncneg, ncp, nc_dst);
538	}
539	if (numneg * ncnegfactor > numcache) {
540		ncp = TAILQ_FIRST(&ncneg);
541		zap = 1;
542	}
543	if (hold)
544		vhold(dvp);
545	if (zap)
546		cache_zap(ncp);
547	CACHE_UNLOCK();
548}
549
550/*
551 * Name cache initialization, from vfs_init() when we are booting
552 */
553static void
554nchinit(void *dummy __unused)
555{
556
557	TAILQ_INIT(&ncneg);
558
559	cache_zone_small = uma_zcreate("S VFS Cache", CACHE_ZONE_SMALL, NULL,
560	    NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
561	cache_zone_large = uma_zcreate("L VFS Cache", CACHE_ZONE_LARGE, NULL,
562	    NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
563
564	nchashtbl = hashinit(desiredvnodes * 2, M_VFSCACHE, &nchash);
565}
566SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nchinit, NULL);
567
568
569/*
570 * Invalidate all entries to a particular vnode.
571 */
572void
573cache_purge(vp)
574	struct vnode *vp;
575{
576
577	CTR1(KTR_VFS, "cache_purge(%p)", vp);
578	CACHE_LOCK();
579	while (!LIST_EMPTY(&vp->v_cache_src))
580		cache_zap(LIST_FIRST(&vp->v_cache_src));
581	while (!TAILQ_EMPTY(&vp->v_cache_dst))
582		cache_zap(TAILQ_FIRST(&vp->v_cache_dst));
583	vp->v_dd = NULL;
584	CACHE_UNLOCK();
585}
586
587/*
588 * Flush all entries referencing a particular filesystem.
589 */
590void
591cache_purgevfs(mp)
592	struct mount *mp;
593{
594	struct nchashhead *ncpp;
595	struct namecache *ncp, *nnp;
596
597	/* Scan hash tables for applicable entries */
598	CACHE_LOCK();
599	for (ncpp = &nchashtbl[nchash]; ncpp >= nchashtbl; ncpp--) {
600		LIST_FOREACH_SAFE(ncp, ncpp, nc_hash, nnp) {
601			if (ncp->nc_dvp->v_mount == mp)
602				cache_zap(ncp);
603		}
604	}
605	CACHE_UNLOCK();
606}
607
608/*
609 * Perform canonical checks and cache lookup and pass on to filesystem
610 * through the vop_cachedlookup only if needed.
611 */
612
613int
614vfs_cache_lookup(ap)
615	struct vop_lookup_args /* {
616		struct vnode *a_dvp;
617		struct vnode **a_vpp;
618		struct componentname *a_cnp;
619	} */ *ap;
620{
621	struct vnode *dvp;
622	int error;
623	struct vnode **vpp = ap->a_vpp;
624	struct componentname *cnp = ap->a_cnp;
625	struct ucred *cred = cnp->cn_cred;
626	int flags = cnp->cn_flags;
627	struct thread *td = cnp->cn_thread;
628
629	*vpp = NULL;
630	dvp = ap->a_dvp;
631
632	if (dvp->v_type != VDIR)
633		return (ENOTDIR);
634
635	if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
636	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
637		return (EROFS);
638
639	error = VOP_ACCESS(dvp, VEXEC, cred, td);
640	if (error)
641		return (error);
642
643	error = cache_lookup(dvp, vpp, cnp);
644	if (error == 0)
645		return (VOP_CACHEDLOOKUP(dvp, vpp, cnp));
646	if (error == ENOENT)
647		return (error);
648	return (0);
649}
650
651
652#ifndef _SYS_SYSPROTO_H_
653struct  __getcwd_args {
654	u_char	*buf;
655	u_int	buflen;
656};
657#endif
658
659/*
660 * XXX All of these sysctls would probably be more productive dead.
661 */
662static int disablecwd;
663SYSCTL_INT(_debug, OID_AUTO, disablecwd, CTLFLAG_RW, &disablecwd, 0,
664   "Disable the getcwd syscall");
665
666/* Implementation of the getcwd syscall. */
667int
668__getcwd(td, uap)
669	struct thread *td;
670	struct __getcwd_args *uap;
671{
672
673	return (kern___getcwd(td, uap->buf, UIO_USERSPACE, uap->buflen));
674}
675
676int
677kern___getcwd(struct thread *td, u_char *buf, enum uio_seg bufseg, u_int buflen)
678{
679	char *bp, *tmpbuf;
680	struct filedesc *fdp;
681	int error;
682
683	if (disablecwd)
684		return (ENODEV);
685	if (buflen < 2)
686		return (EINVAL);
687	if (buflen > MAXPATHLEN)
688		buflen = MAXPATHLEN;
689
690	tmpbuf = malloc(buflen, M_TEMP, M_WAITOK);
691	fdp = td->td_proc->p_fd;
692	FILEDESC_SLOCK(fdp);
693	error = vn_fullpath1(td, fdp->fd_cdir, fdp->fd_rdir, tmpbuf,
694	    &bp, buflen);
695	FILEDESC_SUNLOCK(fdp);
696
697	if (!error) {
698		if (bufseg == UIO_SYSSPACE)
699			bcopy(bp, buf, strlen(bp) + 1);
700		else
701			error = copyout(bp, buf, strlen(bp) + 1);
702	}
703	free(tmpbuf, M_TEMP);
704	return (error);
705}
706
707/*
708 * Thus begins the fullpath magic.
709 */
710
711#undef STATNODE
712#define STATNODE(name)							\
713	static u_int name;						\
714	SYSCTL_UINT(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, 0, "")
715
716static int disablefullpath;
717SYSCTL_INT(_debug, OID_AUTO, disablefullpath, CTLFLAG_RW, &disablefullpath, 0,
718	"Disable the vn_fullpath function");
719
720/* These count for kern___getcwd(), too. */
721STATNODE(numfullpathcalls);
722STATNODE(numfullpathfail1);
723STATNODE(numfullpathfail2);
724STATNODE(numfullpathfail4);
725STATNODE(numfullpathfound);
726
727/*
728 * Retrieve the full filesystem path that correspond to a vnode from the name
729 * cache (if available)
730 */
731int
732vn_fullpath(struct thread *td, struct vnode *vn, char **retbuf, char **freebuf)
733{
734	char *buf;
735	struct filedesc *fdp;
736	int error;
737
738	if (disablefullpath)
739		return (ENODEV);
740	if (vn == NULL)
741		return (EINVAL);
742
743	buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
744	fdp = td->td_proc->p_fd;
745	FILEDESC_SLOCK(fdp);
746	error = vn_fullpath1(td, vn, fdp->fd_rdir, buf, retbuf, MAXPATHLEN);
747	FILEDESC_SUNLOCK(fdp);
748
749	if (!error)
750		*freebuf = buf;
751	else
752		free(buf, M_TEMP);
753	return (error);
754}
755
756/*
757 * The magic behind kern___getcwd() and vn_fullpath().
758 */
759static int
760vn_fullpath1(struct thread *td, struct vnode *vp, struct vnode *rdir,
761    char *buf, char **retbuf, u_int buflen)
762{
763	char *bp;
764	int error, i, slash_prefixed;
765	struct namecache *ncp;
766
767	bp = buf + buflen - 1;
768	*bp = '\0';
769	error = 0;
770	slash_prefixed = 0;
771
772	CACHE_LOCK();
773	numfullpathcalls++;
774	if (vp->v_type != VDIR) {
775		ncp = TAILQ_FIRST(&vp->v_cache_dst);
776		if (!ncp) {
777			numfullpathfail2++;
778			CACHE_UNLOCK();
779			return (ENOENT);
780		}
781		for (i = ncp->nc_nlen - 1; i >= 0 && bp > buf; i--)
782			*--bp = ncp->nc_name[i];
783		if (bp == buf) {
784			numfullpathfail4++;
785			CACHE_UNLOCK();
786			return (ENOMEM);
787		}
788		*--bp = '/';
789		slash_prefixed = 1;
790		vp = ncp->nc_dvp;
791	}
792	while (vp != rdir && vp != rootvnode) {
793		if (vp->v_vflag & VV_ROOT) {
794			if (vp->v_iflag & VI_DOOMED) {	/* forced unmount */
795				error = EBADF;
796				break;
797			}
798			vp = vp->v_mount->mnt_vnodecovered;
799			continue;
800		}
801		if (vp->v_dd == NULL) {
802			numfullpathfail1++;
803			error = ENOTDIR;
804			break;
805		}
806		ncp = TAILQ_FIRST(&vp->v_cache_dst);
807		if (!ncp) {
808			numfullpathfail2++;
809			error = ENOENT;
810			break;
811		}
812		MPASS(ncp->nc_dvp == vp->v_dd);
813		for (i = ncp->nc_nlen - 1; i >= 0 && bp != buf; i--)
814			*--bp = ncp->nc_name[i];
815		if (bp == buf) {
816			numfullpathfail4++;
817			error = ENOMEM;
818			break;
819		}
820		*--bp = '/';
821		slash_prefixed = 1;
822		vp = ncp->nc_dvp;
823	}
824	if (error) {
825		CACHE_UNLOCK();
826		return (error);
827	}
828	if (!slash_prefixed) {
829		if (bp == buf) {
830			numfullpathfail4++;
831			CACHE_UNLOCK();
832			return (ENOMEM);
833		} else {
834			*--bp = '/';
835		}
836	}
837	numfullpathfound++;
838	CACHE_UNLOCK();
839
840	*retbuf = bp;
841	return (0);
842}
843
844int
845vn_commname(struct vnode *vp, char *buf, u_int buflen)
846{
847	struct namecache *ncp;
848	int l;
849
850	CACHE_LOCK();
851	ncp = TAILQ_FIRST(&vp->v_cache_dst);
852	if (!ncp) {
853		CACHE_UNLOCK();
854		return (ENOENT);
855	}
856	l = min(ncp->nc_nlen, buflen - 1);
857	memcpy(buf, ncp->nc_name, l);
858	CACHE_UNLOCK();
859	buf[l] = '\0';
860	return (0);
861}
862