xref: /freebsd/sys/kern/vfs_cache.c (revision c68159a6d8eede11766cf13896d0f7670dbd51aa)
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  * $FreeBSD$
38  */
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.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/sysproto.h>
49 #include <sys/proc.h>
50 #include <sys/filedesc.h>
51 
52 /*
53  * This structure describes the elements in the cache of recent
54  * names looked up by namei.
55  */
56 
57 struct	namecache {
58 	LIST_ENTRY(namecache) nc_hash;	/* hash chain */
59 	LIST_ENTRY(namecache) nc_src;	/* source vnode list */
60 	TAILQ_ENTRY(namecache) nc_dst;	/* destination vnode list */
61 	struct	vnode *nc_dvp;		/* vnode of parent of name */
62 	struct	vnode *nc_vp;		/* vnode the name refers to */
63 	u_char	nc_flag;		/* flag bits */
64 	u_char	nc_nlen;		/* length of name */
65 	char	nc_name[0];		/* segment name */
66 };
67 
68 /*
69  * Name caching works as follows:
70  *
71  * Names found by directory scans are retained in a cache
72  * for future reference.  It is managed LRU, so frequently
73  * used names will hang around.  Cache is indexed by hash value
74  * obtained from (vp, name) where vp refers to the directory
75  * containing name.
76  *
77  * If it is a "negative" entry, (i.e. for a name that is known NOT to
78  * exist) the vnode pointer will be NULL.
79  *
80  * Upon reaching the last segment of a path, if the reference
81  * is for DELETE, or NOCACHE is set (rewrite), and the
82  * name is located in the cache, it will be dropped.
83  */
84 
85 /*
86  * Structures associated with name cacheing.
87  */
88 #define NCHHASH(dvp, hash) \
89 	(&nchashtbl[((dvp)->v_id + (hash)) & nchash])
90 static LIST_HEAD(nchashhead, namecache) *nchashtbl;	/* Hash Table */
91 static TAILQ_HEAD(, namecache) ncneg;	/* Hash Table */
92 static u_long	nchash;			/* size of hash table */
93 SYSCTL_ULONG(_debug, OID_AUTO, nchash, CTLFLAG_RD, &nchash, 0, "");
94 static u_long	ncnegfactor = 16;	/* ratio of negative entries */
95 SYSCTL_ULONG(_debug, OID_AUTO, ncnegfactor, CTLFLAG_RW, &ncnegfactor, 0, "");
96 static u_long	numneg;		/* number of cache entries allocated */
97 SYSCTL_ULONG(_debug, OID_AUTO, numneg, CTLFLAG_RD, &numneg, 0, "");
98 static u_long	numcache;		/* number of cache entries allocated */
99 SYSCTL_ULONG(_debug, OID_AUTO, numcache, CTLFLAG_RD, &numcache, 0, "");
100 struct	nchstats nchstats;		/* cache effectiveness statistics */
101 
102 static int	doingcache = 1;		/* 1 => enable the cache */
103 SYSCTL_INT(_debug, OID_AUTO, vfscache, CTLFLAG_RW, &doingcache, 0, "");
104 SYSCTL_INT(_debug, OID_AUTO, vnsize, CTLFLAG_RD, 0, sizeof(struct vnode), "");
105 SYSCTL_INT(_debug, OID_AUTO, ncsize, CTLFLAG_RD, 0, sizeof(struct namecache), "");
106 
107 /*
108  * The new name cache statistics
109  */
110 SYSCTL_NODE(_vfs, OID_AUTO, cache, CTLFLAG_RW, 0, "Name cache statistics");
111 #define STATNODE(mode, name, var) \
112 	SYSCTL_ULONG(_vfs_cache, OID_AUTO, name, mode, var, 0, "");
113 STATNODE(CTLFLAG_RD, numneg, &numneg);
114 STATNODE(CTLFLAG_RD, numcache, &numcache);
115 static u_long numcalls; STATNODE(CTLFLAG_RD, numcalls, &numcalls);
116 static u_long dothits; STATNODE(CTLFLAG_RD, dothits, &dothits);
117 static u_long dotdothits; STATNODE(CTLFLAG_RD, dotdothits, &dotdothits);
118 static u_long numchecks; STATNODE(CTLFLAG_RD, numchecks, &numchecks);
119 static u_long nummiss; STATNODE(CTLFLAG_RD, nummiss, &nummiss);
120 static u_long nummisszap; STATNODE(CTLFLAG_RD, nummisszap, &nummisszap);
121 static u_long numposzaps; STATNODE(CTLFLAG_RD, numposzaps, &numposzaps);
122 static u_long numposhits; STATNODE(CTLFLAG_RD, numposhits, &numposhits);
123 static u_long numnegzaps; STATNODE(CTLFLAG_RD, numnegzaps, &numnegzaps);
124 static u_long numneghits; STATNODE(CTLFLAG_RD, numneghits, &numneghits);
125 
126 SYSCTL_OPAQUE(_vfs_cache, OID_AUTO, nchstats, CTLFLAG_RD, &nchstats,
127         sizeof(nchstats), "LU", "VFS cache effectiveness statistics");
128 
129 
130 
131 static void cache_zap __P((struct namecache *ncp));
132 
133 static MALLOC_DEFINE(M_VFSCACHE, "vfscache", "VFS name cache entries");
134 
135 /*
136  * Flags in namecache.nc_flag
137  */
138 #define NCF_WHITE	1
139 /*
140  * Delete an entry from its hash list and move it to the front
141  * of the LRU list for immediate reuse.
142  */
143 static void
144 cache_zap(ncp)
145 	struct namecache *ncp;
146 {
147 	LIST_REMOVE(ncp, nc_hash);
148 	LIST_REMOVE(ncp, nc_src);
149 	if (LIST_EMPTY(&ncp->nc_dvp->v_cache_src))
150 		vdrop(ncp->nc_dvp);
151 	if (ncp->nc_vp) {
152 		TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst, ncp, nc_dst);
153 	} else {
154 		TAILQ_REMOVE(&ncneg, ncp, nc_dst);
155 		numneg--;
156 	}
157 	numcache--;
158 	free(ncp, M_VFSCACHE);
159 }
160 
161 /*
162  * Lookup an entry in the cache
163  *
164  * We don't do this if the segment name is long, simply so the cache
165  * can avoid holding long names (which would either waste space, or
166  * add greatly to the complexity).
167  *
168  * Lookup is called with dvp pointing to the directory to search,
169  * cnp pointing to the name of the entry being sought. If the lookup
170  * succeeds, the vnode is returned in *vpp, and a status of -1 is
171  * returned. If the lookup determines that the name does not exist
172  * (negative cacheing), a status of ENOENT is returned. If the lookup
173  * fails, a status of zero is returned.
174  */
175 
176 int
177 cache_lookup(dvp, vpp, cnp)
178 	struct vnode *dvp;
179 	struct vnode **vpp;
180 	struct componentname *cnp;
181 {
182 	struct namecache *ncp;
183 	u_long hash;
184 	u_char *cp;
185 	int len;
186 
187 	if (!doingcache) {
188 		cnp->cn_flags &= ~MAKEENTRY;
189 		return (0);
190 	}
191 
192 	numcalls++;
193 
194 	if (cnp->cn_nameptr[0] == '.') {
195 		if (cnp->cn_namelen == 1) {
196 			*vpp = dvp;
197 			dothits++;
198 			return (-1);
199 		}
200 		if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
201 			dotdothits++;
202 			if (dvp->v_dd->v_id != dvp->v_ddid ||
203 			    (cnp->cn_flags & MAKEENTRY) == 0) {
204 				dvp->v_ddid = 0;
205 				return (0);
206 			}
207 			*vpp = dvp->v_dd;
208 			return (-1);
209 		}
210 	}
211 
212 	hash = 0;
213 	len = cnp->cn_namelen;
214 	for (cp = cnp->cn_nameptr; len; len--, cp++)
215 		hash += *cp;
216 	LIST_FOREACH(ncp, (NCHHASH(dvp, hash)), nc_hash) {
217 		numchecks++;
218 		if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
219 		    !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
220 			break;
221 	}
222 
223 	/* We failed to find an entry */
224 	if (ncp == 0) {
225 		if ((cnp->cn_flags & MAKEENTRY) == 0) {
226 			nummisszap++;
227 		} else {
228 			nummiss++;
229 		}
230 		nchstats.ncs_miss++;
231 		return (0);
232 	}
233 
234 	/* We don't want to have an entry, so dump it */
235 	if ((cnp->cn_flags & MAKEENTRY) == 0) {
236 		numposzaps++;
237 		nchstats.ncs_badhits++;
238 		cache_zap(ncp);
239 		return (0);
240 	}
241 
242 	/* We found a "positive" match, return the vnode */
243         if (ncp->nc_vp) {
244 		numposhits++;
245 		nchstats.ncs_goodhits++;
246 		*vpp = ncp->nc_vp;
247 		return (-1);
248 	}
249 
250 	/* We found a negative match, and want to create it, so purge */
251 	if (cnp->cn_nameiop == CREATE) {
252 		numnegzaps++;
253 		nchstats.ncs_badhits++;
254 		cache_zap(ncp);
255 		return (0);
256 	}
257 
258 	numneghits++;
259 	/*
260 	 * We found a "negative" match, ENOENT notifies client of this match.
261 	 * The nc_vpid field records whether this is a whiteout.
262 	 */
263 	TAILQ_REMOVE(&ncneg, ncp, nc_dst);
264 	TAILQ_INSERT_TAIL(&ncneg, ncp, nc_dst);
265 	nchstats.ncs_neghits++;
266 	if (ncp->nc_flag & NCF_WHITE)
267 		cnp->cn_flags |= ISWHITEOUT;
268 	return (ENOENT);
269 }
270 
271 /*
272  * Add an entry to the cache.
273  */
274 void
275 cache_enter(dvp, vp, cnp)
276 	struct vnode *dvp;
277 	struct vnode *vp;
278 	struct componentname *cnp;
279 {
280 	struct namecache *ncp;
281 	struct nchashhead *ncpp;
282 	u_long hash;
283 	u_char *cp, *dp;
284 	int len;
285 
286 	if (!doingcache)
287 		return;
288 
289 	if (cnp->cn_nameptr[0] == '.') {
290 		if (cnp->cn_namelen == 1) {
291 			return;
292 		}
293 		if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
294 			if (vp) {
295 				dvp->v_dd = vp;
296 				dvp->v_ddid = vp->v_id;
297 			} else {
298 				dvp->v_dd = dvp;
299 				dvp->v_ddid = 0;
300 			}
301 			return;
302 		}
303 	}
304 
305 	ncp = (struct namecache *)
306 		malloc(sizeof *ncp + cnp->cn_namelen, M_VFSCACHE, M_WAITOK);
307 	bzero((char *)ncp, sizeof *ncp);
308 	numcache++;
309 	if (!vp) {
310 		numneg++;
311 		ncp->nc_flag = cnp->cn_flags & ISWHITEOUT ? NCF_WHITE : 0;
312 	} else if (vp->v_type == VDIR) {
313 		vp->v_dd = dvp;
314 		vp->v_ddid = dvp->v_id;
315 	}
316 
317 	/*
318 	 * Fill in cache info, if vp is NULL this is a "negative" cache entry.
319 	 * For negative entries, we have to record whether it is a whiteout.
320 	 * the whiteout flag is stored in the nc_vpid field which is
321 	 * otherwise unused.
322 	 */
323 	ncp->nc_vp = vp;
324 	ncp->nc_dvp = dvp;
325 	len = ncp->nc_nlen = cnp->cn_namelen;
326 	hash = 0;
327 	dp = ncp->nc_name;
328 	for (cp = cnp->cn_nameptr; len; len--, cp++, dp++)
329 		hash += (*dp = *cp);
330 	ncpp = NCHHASH(dvp, hash);
331 	LIST_INSERT_HEAD(ncpp, ncp, nc_hash);
332 	if (LIST_EMPTY(&dvp->v_cache_src))
333 		vhold(dvp);
334 	LIST_INSERT_HEAD(&dvp->v_cache_src, ncp, nc_src);
335 	if (vp) {
336 		TAILQ_INSERT_HEAD(&vp->v_cache_dst, ncp, nc_dst);
337 	} else {
338 		TAILQ_INSERT_TAIL(&ncneg, ncp, nc_dst);
339 	}
340 	if (numneg * ncnegfactor > numcache) {
341 		ncp = TAILQ_FIRST(&ncneg);
342 		cache_zap(ncp);
343 	}
344 }
345 
346 /*
347  * Name cache initialization, from vfs_init() when we are booting
348  */
349 static void
350 nchinit(void *dummy __unused)
351 {
352 
353 	TAILQ_INIT(&ncneg);
354 	nchashtbl = hashinit(desiredvnodes * 2, M_VFSCACHE, &nchash);
355 }
356 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nchinit, NULL)
357 
358 
359 /*
360  * Invalidate all entries to a particular vnode.
361  *
362  * Remove all entries in the namecache relating to this vnode and
363  * change the v_id.  We take the v_id from a global counter, since
364  * it becomes a handy sequence number in crash-dumps that way.
365  * No valid vnode will ever have (v_id == 0).
366  *
367  * XXX: Only time and the size of v_id prevents this from failing:
368  * XXX: In theory we should hunt down all (struct vnode*, v_id)
369  * XXX: soft references and nuke them, at least on the global
370  * XXX: v_id wraparound.  The period of resistance can be extended
371  * XXX: by incrementing each vnodes v_id individually instead of
372  * XXX: using the global v_id.
373  */
374 
375 void
376 cache_purge(vp)
377 	struct vnode *vp;
378 {
379 	static u_long nextid;
380 
381 	while (!LIST_EMPTY(&vp->v_cache_src))
382 		cache_zap(LIST_FIRST(&vp->v_cache_src));
383 	while (!TAILQ_EMPTY(&vp->v_cache_dst))
384 		cache_zap(TAILQ_FIRST(&vp->v_cache_dst));
385 
386 	do
387 		nextid++;
388 	while (nextid == vp->v_id || !nextid);
389 	vp->v_id = nextid;
390 	vp->v_dd = vp;
391 	vp->v_ddid = 0;
392 }
393 
394 /*
395  * Flush all entries referencing a particular filesystem.
396  *
397  * Since we need to check it anyway, we will flush all the invalid
398  * entries at the same time.
399  */
400 void
401 cache_purgevfs(mp)
402 	struct mount *mp;
403 {
404 	struct nchashhead *ncpp;
405 	struct namecache *ncp, *nnp;
406 
407 	/* Scan hash tables for applicable entries */
408 	for (ncpp = &nchashtbl[nchash]; ncpp >= nchashtbl; ncpp--) {
409 		for (ncp = LIST_FIRST(ncpp); ncp != 0; ncp = nnp) {
410 			nnp = LIST_NEXT(ncp, nc_hash);
411 			if (ncp->nc_dvp->v_mount == mp) {
412 				cache_zap(ncp);
413 			}
414 		}
415 	}
416 }
417 
418 /*
419  * Perform canonical checks and cache lookup and pass on to filesystem
420  * through the vop_cachedlookup only if needed.
421  */
422 
423 int
424 vfs_cache_lookup(ap)
425 	struct vop_lookup_args /* {
426 		struct vnode *a_dvp;
427 		struct vnode **a_vpp;
428 		struct componentname *a_cnp;
429 	} */ *ap;
430 {
431 	struct vnode *dvp, *vp;
432 	int lockparent;
433 	int error;
434 	struct vnode **vpp = ap->a_vpp;
435 	struct componentname *cnp = ap->a_cnp;
436 	struct ucred *cred = cnp->cn_cred;
437 	int flags = cnp->cn_flags;
438 	struct proc *p = cnp->cn_proc;
439 	u_long vpid;	/* capability number of vnode */
440 
441 	*vpp = NULL;
442 	dvp = ap->a_dvp;
443 	lockparent = flags & LOCKPARENT;
444 
445 	if (dvp->v_type != VDIR)
446                 return (ENOTDIR);
447 
448 	if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
449 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
450 		return (EROFS);
451 
452 	error = VOP_ACCESS(dvp, VEXEC, cred, p);
453 
454 	if (error)
455 		return (error);
456 
457 	error = cache_lookup(dvp, vpp, cnp);
458 
459 	if (!error)
460 		return (VOP_CACHEDLOOKUP(dvp, vpp, cnp));
461 
462 	if (error == ENOENT)
463 		return (error);
464 
465 	vp = *vpp;
466 	vpid = vp->v_id;
467 	cnp->cn_flags &= ~PDIRUNLOCK;
468 	if (dvp == vp) {   /* lookup on "." */
469 		VREF(vp);
470 		error = 0;
471 	} else if (flags & ISDOTDOT) {
472 		VOP_UNLOCK(dvp, 0, p);
473 		cnp->cn_flags |= PDIRUNLOCK;
474 		error = vget(vp, LK_EXCLUSIVE, p);
475 		if (!error && lockparent && (flags & ISLASTCN)) {
476 			if ((error = vn_lock(dvp, LK_EXCLUSIVE, p)) == 0)
477 				cnp->cn_flags &= ~PDIRUNLOCK;
478 		}
479 	} else {
480 		error = vget(vp, LK_EXCLUSIVE, p);
481 		if (!lockparent || error || !(flags & ISLASTCN)) {
482 			VOP_UNLOCK(dvp, 0, p);
483 			cnp->cn_flags |= PDIRUNLOCK;
484 		}
485 	}
486 	/*
487 	 * Check that the capability number did not change
488 	 * while we were waiting for the lock.
489 	 */
490 	if (!error) {
491 		if (vpid == vp->v_id)
492 			return (0);
493 		vput(vp);
494 		if (lockparent && dvp != vp && (flags & ISLASTCN)) {
495 			VOP_UNLOCK(dvp, 0, p);
496 			cnp->cn_flags |= PDIRUNLOCK;
497 		}
498 	}
499 	if (cnp->cn_flags & PDIRUNLOCK) {
500 		error = vn_lock(dvp, LK_EXCLUSIVE, p);
501 		if (error)
502 			return (error);
503 		cnp->cn_flags &= ~PDIRUNLOCK;
504 	}
505 	return (VOP_CACHEDLOOKUP(dvp, vpp, cnp));
506 }
507 
508 
509 #ifndef _SYS_SYSPROTO_H_
510 struct  __getcwd_args {
511 	u_char	*buf;
512 	u_int	buflen;
513 };
514 #endif
515 
516 static int disablecwd;
517 SYSCTL_INT(_debug, OID_AUTO, disablecwd, CTLFLAG_RW, &disablecwd, 0, "");
518 
519 static u_long numcwdcalls; STATNODE(CTLFLAG_RD, numcwdcalls, &numcwdcalls);
520 static u_long numcwdfail1; STATNODE(CTLFLAG_RD, numcwdfail1, &numcwdfail1);
521 static u_long numcwdfail2; STATNODE(CTLFLAG_RD, numcwdfail2, &numcwdfail2);
522 static u_long numcwdfail3; STATNODE(CTLFLAG_RD, numcwdfail3, &numcwdfail3);
523 static u_long numcwdfail4; STATNODE(CTLFLAG_RD, numcwdfail4, &numcwdfail4);
524 static u_long numcwdfound; STATNODE(CTLFLAG_RD, numcwdfound, &numcwdfound);
525 int
526 __getcwd(p, uap)
527 	struct proc *p;
528 	struct __getcwd_args *uap;
529 {
530 	char *bp, *buf;
531 	int error, i, slash_prefixed;
532 	struct filedesc *fdp;
533 	struct namecache *ncp;
534 	struct vnode *vp;
535 
536 	numcwdcalls++;
537 	if (disablecwd)
538 		return (ENODEV);
539 	if (uap->buflen < 2)
540 		return (EINVAL);
541 	if (uap->buflen > MAXPATHLEN)
542 		uap->buflen = MAXPATHLEN;
543 	buf = bp = malloc(uap->buflen, M_TEMP, M_WAITOK);
544 	bp += uap->buflen - 1;
545 	*bp = '\0';
546 	fdp = p->p_fd;
547 	slash_prefixed = 0;
548 	for (vp = fdp->fd_cdir; vp != fdp->fd_rdir && vp != rootvnode;) {
549 		if (vp->v_flag & VROOT) {
550 			if (vp->v_mount == NULL)	/* forced unmount */
551 				return (EBADF);
552 			vp = vp->v_mount->mnt_vnodecovered;
553 			continue;
554 		}
555 		if (vp->v_dd->v_id != vp->v_ddid) {
556 			numcwdfail1++;
557 			free(buf, M_TEMP);
558 			return (ENOTDIR);
559 		}
560 		ncp = TAILQ_FIRST(&vp->v_cache_dst);
561 		if (!ncp) {
562 			numcwdfail2++;
563 			free(buf, M_TEMP);
564 			return (ENOENT);
565 		}
566 		if (ncp->nc_dvp != vp->v_dd) {
567 			numcwdfail3++;
568 			free(buf, M_TEMP);
569 			return (EBADF);
570 		}
571 		for (i = ncp->nc_nlen - 1; i >= 0; i--) {
572 			if (bp == buf) {
573 				numcwdfail4++;
574 				free(buf, M_TEMP);
575 				return (ENOMEM);
576 			}
577 			*--bp = ncp->nc_name[i];
578 		}
579 		if (bp == buf) {
580 			numcwdfail4++;
581 			free(buf, M_TEMP);
582 			return (ENOMEM);
583 		}
584 		*--bp = '/';
585 		slash_prefixed = 1;
586 		vp = vp->v_dd;
587 	}
588 	if (!slash_prefixed) {
589 		if (bp == buf) {
590 			numcwdfail4++;
591 			free(buf, M_TEMP);
592 			return (ENOMEM);
593 		}
594 		*--bp = '/';
595 	}
596 	numcwdfound++;
597 	error = copyout(bp, uap->buf, strlen(bp) + 1);
598 	free(buf, M_TEMP);
599 	return (error);
600 }
601 
602 /*
603  * Thus begins the fullpath magic.
604  */
605 
606 #undef STATNODE
607 #define STATNODE(name)							\
608 	static u_int name;						\
609 	SYSCTL_UINT(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, 0, "")
610 
611 static int disablefullpath;
612 SYSCTL_INT(_debug, OID_AUTO, disablefullpath, CTLFLAG_RW,
613     &disablefullpath, 0, "");
614 
615 STATNODE(numfullpathcalls);
616 STATNODE(numfullpathfail1);
617 STATNODE(numfullpathfail2);
618 STATNODE(numfullpathfail3);
619 STATNODE(numfullpathfail4);
620 STATNODE(numfullpathfound);
621 
622 int
623 textvp_fullpath(struct proc *p, char **retbuf, char **retfreebuf) {
624 	char *bp, *buf;
625 	int i, slash_prefixed;
626 	struct filedesc *fdp;
627 	struct namecache *ncp;
628 	struct vnode *vp, *textvp;
629 
630 	numfullpathcalls++;
631 	if (disablefullpath)
632 		return (ENODEV);
633 	textvp = p->p_textvp;
634 	if (textvp == NULL)
635 		return (EINVAL);
636 	buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
637 	bp = buf + MAXPATHLEN - 1;
638 	*bp = '\0';
639 	fdp = p->p_fd;
640 	slash_prefixed = 0;
641 	for (vp = textvp; vp != fdp->fd_rdir && vp != rootvnode;) {
642 		if (vp->v_flag & VROOT) {
643 			if (vp->v_mount == NULL) {	/* forced unmount */
644 				free(buf, M_TEMP);
645 				return (EBADF);
646 			}
647 			vp = vp->v_mount->mnt_vnodecovered;
648 			continue;
649 		}
650 		if (vp != textvp && vp->v_dd->v_id != vp->v_ddid) {
651 			numfullpathfail1++;
652 			free(buf, M_TEMP);
653 			return (ENOTDIR);
654 		}
655 		ncp = TAILQ_FIRST(&vp->v_cache_dst);
656 		if (!ncp) {
657 			numfullpathfail2++;
658 			free(buf, M_TEMP);
659 			return (ENOENT);
660 		}
661 		if (vp != textvp && ncp->nc_dvp != vp->v_dd) {
662 			numfullpathfail3++;
663 			free(buf, M_TEMP);
664 			return (EBADF);
665 		}
666 		for (i = ncp->nc_nlen - 1; i >= 0; i--) {
667 			if (bp == buf) {
668 				numfullpathfail4++;
669 				free(buf, M_TEMP);
670 				return (ENOMEM);
671 			}
672 			*--bp = ncp->nc_name[i];
673 		}
674 		if (bp == buf) {
675 			numfullpathfail4++;
676 			free(buf, M_TEMP);
677 			return (ENOMEM);
678 		}
679 		*--bp = '/';
680 		slash_prefixed = 1;
681 		vp = ncp->nc_dvp;
682 	}
683 	if (!slash_prefixed) {
684 		if (bp == buf) {
685 			numfullpathfail4++;
686 			free(buf, M_TEMP);
687 			return (ENOMEM);
688 		}
689 		*--bp = '/';
690 	}
691 	numfullpathfound++;
692 	*retbuf = bp;
693 	*retfreebuf = buf;
694 	return (0);
695 }
696