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