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