xref: /freebsd/sys/kern/vfs_cache.c (revision 830940567b49bb0c08dfaed40418999e76616909)
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$");
37 
38 #include "opt_kdtrace.h"
39 #include "opt_ktrace.h"
40 
41 #include <sys/param.h>
42 #include <sys/filedesc.h>
43 #include <sys/fnv_hash.h>
44 #include <sys/kernel.h>
45 #include <sys/lock.h>
46 #include <sys/malloc.h>
47 #include <sys/mount.h>
48 #include <sys/namei.h>
49 #include <sys/proc.h>
50 #include <sys/rwlock.h>
51 #include <sys/sdt.h>
52 #include <sys/syscallsubr.h>
53 #include <sys/sysctl.h>
54 #include <sys/sysproto.h>
55 #include <sys/systm.h>
56 #include <sys/vnode.h>
57 #ifdef KTRACE
58 #include <sys/ktrace.h>
59 #endif
60 
61 #include <vm/uma.h>
62 
63 SDT_PROVIDER_DECLARE(vfs);
64 SDT_PROBE_DEFINE3(vfs, namecache, enter, done, "struct vnode *", "char *",
65     "struct vnode *");
66 SDT_PROBE_DEFINE2(vfs, namecache, enter_negative, done, "struct vnode *",
67     "char *");
68 SDT_PROBE_DEFINE1(vfs, namecache, fullpath, entry, "struct vnode *");
69 SDT_PROBE_DEFINE3(vfs, namecache, fullpath, hit, "struct vnode *",
70     "struct char *", "struct vnode *");
71 SDT_PROBE_DEFINE1(vfs, namecache, fullpath, miss, "struct vnode *");
72 SDT_PROBE_DEFINE3(vfs, namecache, fullpath, return, "int", "struct vnode *",
73     "struct char *");
74 SDT_PROBE_DEFINE3(vfs, namecache, lookup, hit, "struct vnode *", "char *",
75     "struct vnode *");
76 SDT_PROBE_DEFINE2(vfs, namecache, lookup, hit_negative, "struct vnode *",
77     "char *");
78 SDT_PROBE_DEFINE2(vfs, namecache, lookup, miss, "struct vnode *",
79     "char *");
80 SDT_PROBE_DEFINE1(vfs, namecache, purge, done, "struct vnode *");
81 SDT_PROBE_DEFINE1(vfs, namecache, purge_negative, done, "struct vnode *");
82 SDT_PROBE_DEFINE1(vfs, namecache, purgevfs, done, "struct mount *");
83 SDT_PROBE_DEFINE3(vfs, namecache, zap, done, "struct vnode *", "char *",
84     "struct vnode *");
85 SDT_PROBE_DEFINE2(vfs, namecache, zap_negative, done, "struct vnode *",
86     "char *");
87 
88 /*
89  * This structure describes the elements in the cache of recent
90  * names looked up by namei.
91  */
92 
93 struct	namecache {
94 	LIST_ENTRY(namecache) nc_hash;	/* hash chain */
95 	LIST_ENTRY(namecache) nc_src;	/* source vnode list */
96 	TAILQ_ENTRY(namecache) nc_dst;	/* destination vnode list */
97 	struct	vnode *nc_dvp;		/* vnode of parent of name */
98 	struct	vnode *nc_vp;		/* vnode the name refers to */
99 	u_char	nc_flag;		/* flag bits */
100 	u_char	nc_nlen;		/* length of name */
101 	char	nc_name[0];		/* segment name + nul */
102 };
103 
104 /*
105  * Name caching works as follows:
106  *
107  * Names found by directory scans are retained in a cache
108  * for future reference.  It is managed LRU, so frequently
109  * used names will hang around.  Cache is indexed by hash value
110  * obtained from (vp, name) where vp refers to the directory
111  * containing name.
112  *
113  * If it is a "negative" entry, (i.e. for a name that is known NOT to
114  * exist) the vnode pointer will be NULL.
115  *
116  * Upon reaching the last segment of a path, if the reference
117  * is for DELETE, or NOCACHE is set (rewrite), and the
118  * name is located in the cache, it will be dropped.
119  */
120 
121 /*
122  * Structures associated with name cacheing.
123  */
124 #define NCHHASH(hash) \
125 	(&nchashtbl[(hash) & nchash])
126 static LIST_HEAD(nchashhead, namecache) *nchashtbl;	/* Hash Table */
127 static TAILQ_HEAD(, namecache) ncneg;	/* Hash Table */
128 static u_long	nchash;			/* size of hash table */
129 SYSCTL_ULONG(_debug, OID_AUTO, nchash, CTLFLAG_RD, &nchash, 0, "");
130 static u_long	ncnegfactor = 16;	/* ratio of negative entries */
131 SYSCTL_ULONG(_debug, OID_AUTO, ncnegfactor, CTLFLAG_RW, &ncnegfactor, 0, "");
132 static u_long	numneg;			/* number of cache entries allocated */
133 SYSCTL_ULONG(_debug, OID_AUTO, numneg, CTLFLAG_RD, &numneg, 0, "");
134 static u_long	numcache;		/* number of cache entries allocated */
135 SYSCTL_ULONG(_debug, OID_AUTO, numcache, CTLFLAG_RD, &numcache, 0, "");
136 static u_long	numcachehv;		/* number of cache entries with vnodes held */
137 SYSCTL_ULONG(_debug, OID_AUTO, numcachehv, CTLFLAG_RD, &numcachehv, 0, "");
138 #if 0
139 static u_long	numcachepl;		/* number of cache purge for leaf entries */
140 SYSCTL_ULONG(_debug, OID_AUTO, numcachepl, CTLFLAG_RD, &numcachepl, 0, "");
141 #endif
142 struct	nchstats nchstats;		/* cache effectiveness statistics */
143 
144 static struct rwlock cache_lock;
145 RW_SYSINIT(vfscache, &cache_lock, "Name Cache");
146 
147 #define	CACHE_UPGRADE_LOCK()	rw_try_upgrade(&cache_lock)
148 #define	CACHE_RLOCK()		rw_rlock(&cache_lock)
149 #define	CACHE_RUNLOCK()		rw_runlock(&cache_lock)
150 #define	CACHE_WLOCK()		rw_wlock(&cache_lock)
151 #define	CACHE_WUNLOCK()		rw_wunlock(&cache_lock)
152 
153 /*
154  * UMA zones for the VFS cache.
155  *
156  * The small cache is used for entries with short names, which are the
157  * most common.  The large cache is used for entries which are too big to
158  * fit in the small cache.
159  */
160 static uma_zone_t cache_zone_small;
161 static uma_zone_t cache_zone_large;
162 
163 #define	CACHE_PATH_CUTOFF	35
164 #define	CACHE_ZONE_SMALL	(sizeof(struct namecache) + CACHE_PATH_CUTOFF \
165 				    + 1)
166 #define	CACHE_ZONE_LARGE	(sizeof(struct namecache) + NAME_MAX + 1)
167 
168 #define cache_alloc(len)	uma_zalloc(((len) <= CACHE_PATH_CUTOFF) ? \
169 	cache_zone_small : cache_zone_large, M_WAITOK)
170 #define cache_free(ncp)		do { \
171 	if (ncp != NULL) \
172 		uma_zfree(((ncp)->nc_nlen <= CACHE_PATH_CUTOFF) ? \
173 		    cache_zone_small : cache_zone_large, (ncp)); \
174 } while (0)
175 
176 static int	doingcache = 1;		/* 1 => enable the cache */
177 SYSCTL_INT(_debug, OID_AUTO, vfscache, CTLFLAG_RW, &doingcache, 0, "");
178 
179 /* Export size information to userland */
180 SYSCTL_INT(_debug_sizeof, OID_AUTO, namecache, CTLFLAG_RD, 0,
181 	sizeof(struct namecache), "");
182 
183 /*
184  * The new name cache statistics
185  */
186 static SYSCTL_NODE(_vfs, OID_AUTO, cache, CTLFLAG_RW, 0, "Name cache statistics");
187 #define STATNODE(mode, name, var) \
188 	SYSCTL_ULONG(_vfs_cache, OID_AUTO, name, mode, var, 0, "");
189 STATNODE(CTLFLAG_RD, numneg, &numneg);
190 STATNODE(CTLFLAG_RD, numcache, &numcache);
191 static u_long numcalls; STATNODE(CTLFLAG_RD, numcalls, &numcalls);
192 static u_long dothits; STATNODE(CTLFLAG_RD, dothits, &dothits);
193 static u_long dotdothits; STATNODE(CTLFLAG_RD, dotdothits, &dotdothits);
194 static u_long numchecks; STATNODE(CTLFLAG_RD, numchecks, &numchecks);
195 static u_long nummiss; STATNODE(CTLFLAG_RD, nummiss, &nummiss);
196 static u_long nummisszap; STATNODE(CTLFLAG_RD, nummisszap, &nummisszap);
197 static u_long numposzaps; STATNODE(CTLFLAG_RD, numposzaps, &numposzaps);
198 static u_long numposhits; STATNODE(CTLFLAG_RD, numposhits, &numposhits);
199 static u_long numnegzaps; STATNODE(CTLFLAG_RD, numnegzaps, &numnegzaps);
200 static u_long numneghits; STATNODE(CTLFLAG_RD, numneghits, &numneghits);
201 static u_long numupgrades; STATNODE(CTLFLAG_RD, numupgrades, &numupgrades);
202 
203 SYSCTL_OPAQUE(_vfs_cache, OID_AUTO, nchstats, CTLFLAG_RD | CTLFLAG_MPSAFE,
204 	&nchstats, sizeof(nchstats), "LU", "VFS cache effectiveness statistics");
205 
206 
207 
208 static void cache_zap(struct namecache *ncp);
209 static int vn_vptocnp_locked(struct vnode **vp, struct ucred *cred, char *buf,
210     u_int *buflen);
211 static int vn_fullpath1(struct thread *td, struct vnode *vp, struct vnode *rdir,
212     char *buf, char **retbuf, u_int buflen);
213 
214 static MALLOC_DEFINE(M_VFSCACHE, "vfscache", "VFS name cache entries");
215 
216 /*
217  * Flags in namecache.nc_flag
218  */
219 #define NCF_WHITE	0x01
220 #define NCF_ISDOTDOT	0x02
221 
222 #ifdef DIAGNOSTIC
223 /*
224  * Grab an atomic snapshot of the name cache hash chain lengths
225  */
226 SYSCTL_NODE(_debug, OID_AUTO, hashstat, CTLFLAG_RW, NULL, "hash table stats");
227 
228 static int
229 sysctl_debug_hashstat_rawnchash(SYSCTL_HANDLER_ARGS)
230 {
231 	int error;
232 	struct nchashhead *ncpp;
233 	struct namecache *ncp;
234 	int n_nchash;
235 	int count;
236 
237 	n_nchash = nchash + 1;	/* nchash is max index, not count */
238 	if (!req->oldptr)
239 		return SYSCTL_OUT(req, 0, n_nchash * sizeof(int));
240 
241 	/* Scan hash tables for applicable entries */
242 	for (ncpp = nchashtbl; n_nchash > 0; n_nchash--, ncpp++) {
243 		CACHE_RLOCK();
244 		count = 0;
245 		LIST_FOREACH(ncp, ncpp, nc_hash) {
246 			count++;
247 		}
248 		CACHE_RUNLOCK();
249 		error = SYSCTL_OUT(req, &count, sizeof(count));
250 		if (error)
251 			return (error);
252 	}
253 	return (0);
254 }
255 SYSCTL_PROC(_debug_hashstat, OID_AUTO, rawnchash, CTLTYPE_INT|CTLFLAG_RD|
256 	CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_rawnchash, "S,int",
257 	"nchash chain lengths");
258 
259 static int
260 sysctl_debug_hashstat_nchash(SYSCTL_HANDLER_ARGS)
261 {
262 	int error;
263 	struct nchashhead *ncpp;
264 	struct namecache *ncp;
265 	int n_nchash;
266 	int count, maxlength, used, pct;
267 
268 	if (!req->oldptr)
269 		return SYSCTL_OUT(req, 0, 4 * sizeof(int));
270 
271 	n_nchash = nchash + 1;	/* nchash is max index, not count */
272 	used = 0;
273 	maxlength = 0;
274 
275 	/* Scan hash tables for applicable entries */
276 	for (ncpp = nchashtbl; n_nchash > 0; n_nchash--, ncpp++) {
277 		count = 0;
278 		CACHE_RLOCK();
279 		LIST_FOREACH(ncp, ncpp, nc_hash) {
280 			count++;
281 		}
282 		CACHE_RUNLOCK();
283 		if (count)
284 			used++;
285 		if (maxlength < count)
286 			maxlength = count;
287 	}
288 	n_nchash = nchash + 1;
289 	pct = (used * 100 * 100) / n_nchash;
290 	error = SYSCTL_OUT(req, &n_nchash, sizeof(n_nchash));
291 	if (error)
292 		return (error);
293 	error = SYSCTL_OUT(req, &used, sizeof(used));
294 	if (error)
295 		return (error);
296 	error = SYSCTL_OUT(req, &maxlength, sizeof(maxlength));
297 	if (error)
298 		return (error);
299 	error = SYSCTL_OUT(req, &pct, sizeof(pct));
300 	if (error)
301 		return (error);
302 	return (0);
303 }
304 SYSCTL_PROC(_debug_hashstat, OID_AUTO, nchash, CTLTYPE_INT|CTLFLAG_RD|
305 	CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_nchash, "I",
306 	"nchash chain lengths");
307 #endif
308 
309 /*
310  * cache_zap():
311  *
312  *   Removes a namecache entry from cache, whether it contains an actual
313  *   pointer to a vnode or if it is just a negative cache entry.
314  */
315 static void
316 cache_zap(ncp)
317 	struct namecache *ncp;
318 {
319 	struct vnode *vp;
320 
321 	rw_assert(&cache_lock, RA_WLOCKED);
322 	CTR2(KTR_VFS, "cache_zap(%p) vp %p", ncp, ncp->nc_vp);
323 #ifdef KDTRACE_HOOKS
324 	if (ncp->nc_vp != NULL) {
325 		SDT_PROBE(vfs, namecache, zap, done, ncp->nc_dvp,
326 		    ncp->nc_name, ncp->nc_vp, 0, 0);
327 	} else {
328 		SDT_PROBE(vfs, namecache, zap_negative, done, ncp->nc_dvp,
329 		    ncp->nc_name, 0, 0, 0);
330 	}
331 #endif
332 	vp = NULL;
333 	LIST_REMOVE(ncp, nc_hash);
334 	if (ncp->nc_flag & NCF_ISDOTDOT) {
335 		if (ncp == ncp->nc_dvp->v_cache_dd)
336 			ncp->nc_dvp->v_cache_dd = NULL;
337 	} else {
338 		LIST_REMOVE(ncp, nc_src);
339 		if (LIST_EMPTY(&ncp->nc_dvp->v_cache_src)) {
340 			vp = ncp->nc_dvp;
341 			numcachehv--;
342 		}
343 	}
344 	if (ncp->nc_vp) {
345 		TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst, ncp, nc_dst);
346 		if (ncp == ncp->nc_vp->v_cache_dd)
347 			ncp->nc_vp->v_cache_dd = NULL;
348 	} else {
349 		TAILQ_REMOVE(&ncneg, ncp, nc_dst);
350 		numneg--;
351 	}
352 	numcache--;
353 	cache_free(ncp);
354 	if (vp)
355 		vdrop(vp);
356 }
357 
358 /*
359  * Lookup an entry in the cache
360  *
361  * Lookup is called with dvp pointing to the directory to search,
362  * cnp pointing to the name of the entry being sought. If the lookup
363  * succeeds, the vnode is returned in *vpp, and a status of -1 is
364  * returned. If the lookup determines that the name does not exist
365  * (negative cacheing), a status of ENOENT is returned. If the lookup
366  * fails, a status of zero is returned.  If the directory vnode is
367  * recycled out from under us due to a forced unmount, a status of
368  * ENOENT is returned.
369  *
370  * vpp is locked and ref'd on return.  If we're looking up DOTDOT, dvp is
371  * unlocked.  If we're looking up . an extra ref is taken, but the lock is
372  * not recursively acquired.
373  */
374 
375 int
376 cache_lookup(dvp, vpp, cnp)
377 	struct vnode *dvp;
378 	struct vnode **vpp;
379 	struct componentname *cnp;
380 {
381 	struct namecache *ncp;
382 	u_int32_t hash;
383 	int error, ltype, wlocked;
384 
385 	if (!doingcache) {
386 		cnp->cn_flags &= ~MAKEENTRY;
387 		return (0);
388 	}
389 retry:
390 	CACHE_RLOCK();
391 	wlocked = 0;
392 	numcalls++;
393 	error = 0;
394 
395 retry_wlocked:
396 	if (cnp->cn_nameptr[0] == '.') {
397 		if (cnp->cn_namelen == 1) {
398 			*vpp = dvp;
399 			CTR2(KTR_VFS, "cache_lookup(%p, %s) found via .",
400 			    dvp, cnp->cn_nameptr);
401 			dothits++;
402 			SDT_PROBE(vfs, namecache, lookup, hit, dvp, ".",
403 			    *vpp, 0, 0);
404 			goto success;
405 		}
406 		if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
407 			dotdothits++;
408 			if (dvp->v_cache_dd == NULL) {
409 				SDT_PROBE(vfs, namecache, lookup, miss, dvp,
410 				    "..", NULL, 0, 0);
411 				goto unlock;
412 			}
413 			if ((cnp->cn_flags & MAKEENTRY) == 0) {
414 				if (!wlocked && !CACHE_UPGRADE_LOCK())
415 					goto wlock;
416 				if (dvp->v_cache_dd->nc_flag & NCF_ISDOTDOT)
417 					cache_zap(dvp->v_cache_dd);
418 				dvp->v_cache_dd = NULL;
419 				CACHE_WUNLOCK();
420 				return (0);
421 			}
422 			if (dvp->v_cache_dd->nc_flag & NCF_ISDOTDOT)
423 				*vpp = dvp->v_cache_dd->nc_vp;
424 			else
425 				*vpp = dvp->v_cache_dd->nc_dvp;
426 			/* Return failure if negative entry was found. */
427 			if (*vpp == NULL) {
428 				ncp = dvp->v_cache_dd;
429 				goto negative_success;
430 			}
431 			CTR3(KTR_VFS, "cache_lookup(%p, %s) found %p via ..",
432 			    dvp, cnp->cn_nameptr, *vpp);
433 			SDT_PROBE(vfs, namecache, lookup, hit, dvp, "..",
434 			    *vpp, 0, 0);
435 			goto success;
436 		}
437 	}
438 
439 	hash = fnv_32_buf(cnp->cn_nameptr, cnp->cn_namelen, FNV1_32_INIT);
440 	hash = fnv_32_buf(&dvp, sizeof(dvp), hash);
441 	LIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
442 		numchecks++;
443 		if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
444 		    !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
445 			break;
446 	}
447 
448 	/* We failed to find an entry */
449 	if (ncp == NULL) {
450 		SDT_PROBE(vfs, namecache, lookup, miss, dvp, cnp->cn_nameptr,
451 		    NULL, 0, 0);
452 		if ((cnp->cn_flags & MAKEENTRY) == 0) {
453 			nummisszap++;
454 		} else {
455 			nummiss++;
456 		}
457 		nchstats.ncs_miss++;
458 		goto unlock;
459 	}
460 
461 	/* We don't want to have an entry, so dump it */
462 	if ((cnp->cn_flags & MAKEENTRY) == 0) {
463 		numposzaps++;
464 		nchstats.ncs_badhits++;
465 		if (!wlocked && !CACHE_UPGRADE_LOCK())
466 			goto wlock;
467 		cache_zap(ncp);
468 		CACHE_WUNLOCK();
469 		return (0);
470 	}
471 
472 	/* We found a "positive" match, return the vnode */
473 	if (ncp->nc_vp) {
474 		numposhits++;
475 		nchstats.ncs_goodhits++;
476 		*vpp = ncp->nc_vp;
477 		CTR4(KTR_VFS, "cache_lookup(%p, %s) found %p via ncp %p",
478 		    dvp, cnp->cn_nameptr, *vpp, ncp);
479 		SDT_PROBE(vfs, namecache, lookup, hit, dvp, ncp->nc_name,
480 		    *vpp, 0, 0);
481 		goto success;
482 	}
483 
484 negative_success:
485 	/* We found a negative match, and want to create it, so purge */
486 	if (cnp->cn_nameiop == CREATE) {
487 		numnegzaps++;
488 		nchstats.ncs_badhits++;
489 		if (!wlocked && !CACHE_UPGRADE_LOCK())
490 			goto wlock;
491 		cache_zap(ncp);
492 		CACHE_WUNLOCK();
493 		return (0);
494 	}
495 
496 	if (!wlocked && !CACHE_UPGRADE_LOCK())
497 		goto wlock;
498 	numneghits++;
499 	/*
500 	 * We found a "negative" match, so we shift it to the end of
501 	 * the "negative" cache entries queue to satisfy LRU.  Also,
502 	 * check to see if the entry is a whiteout; indicate this to
503 	 * the componentname, if so.
504 	 */
505 	TAILQ_REMOVE(&ncneg, ncp, nc_dst);
506 	TAILQ_INSERT_TAIL(&ncneg, ncp, nc_dst);
507 	nchstats.ncs_neghits++;
508 	if (ncp->nc_flag & NCF_WHITE)
509 		cnp->cn_flags |= ISWHITEOUT;
510 	SDT_PROBE(vfs, namecache, lookup, hit_negative, dvp, ncp->nc_name,
511 	    0, 0, 0);
512 	CACHE_WUNLOCK();
513 	return (ENOENT);
514 
515 wlock:
516 	/*
517 	 * We need to update the cache after our lookup, so upgrade to
518 	 * a write lock and retry the operation.
519 	 */
520 	CACHE_RUNLOCK();
521 	CACHE_WLOCK();
522 	numupgrades++;
523 	wlocked = 1;
524 	goto retry_wlocked;
525 
526 success:
527 	/*
528 	 * On success we return a locked and ref'd vnode as per the lookup
529 	 * protocol.
530 	 */
531 	if (dvp == *vpp) {   /* lookup on "." */
532 		VREF(*vpp);
533 		if (wlocked)
534 			CACHE_WUNLOCK();
535 		else
536 			CACHE_RUNLOCK();
537 		/*
538 		 * When we lookup "." we still can be asked to lock it
539 		 * differently...
540 		 */
541 		ltype = cnp->cn_lkflags & LK_TYPE_MASK;
542 		if (ltype != VOP_ISLOCKED(*vpp)) {
543 			if (ltype == LK_EXCLUSIVE) {
544 				vn_lock(*vpp, LK_UPGRADE | LK_RETRY);
545 				if ((*vpp)->v_iflag & VI_DOOMED) {
546 					/* forced unmount */
547 					vrele(*vpp);
548 					*vpp = NULL;
549 					return (ENOENT);
550 				}
551 			} else
552 				vn_lock(*vpp, LK_DOWNGRADE | LK_RETRY);
553 		}
554 		return (-1);
555 	}
556 	ltype = 0;	/* silence gcc warning */
557 	if (cnp->cn_flags & ISDOTDOT) {
558 		ltype = VOP_ISLOCKED(dvp);
559 		VOP_UNLOCK(dvp, 0);
560 	}
561 	VI_LOCK(*vpp);
562 	if (wlocked)
563 		CACHE_WUNLOCK();
564 	else
565 		CACHE_RUNLOCK();
566 	error = vget(*vpp, cnp->cn_lkflags | LK_INTERLOCK, cnp->cn_thread);
567 	if (cnp->cn_flags & ISDOTDOT) {
568 		vn_lock(dvp, ltype | LK_RETRY);
569 		if (dvp->v_iflag & VI_DOOMED) {
570 			if (error == 0)
571 				vput(*vpp);
572 			*vpp = NULL;
573 			return (ENOENT);
574 		}
575 	}
576 	if (error) {
577 		*vpp = NULL;
578 		goto retry;
579 	}
580 	if ((cnp->cn_flags & ISLASTCN) &&
581 	    (cnp->cn_lkflags & LK_TYPE_MASK) == LK_EXCLUSIVE) {
582 		ASSERT_VOP_ELOCKED(*vpp, "cache_lookup");
583 	}
584 	return (-1);
585 
586 unlock:
587 	if (wlocked)
588 		CACHE_WUNLOCK();
589 	else
590 		CACHE_RUNLOCK();
591 	return (0);
592 }
593 
594 /*
595  * Add an entry to the cache.
596  */
597 void
598 cache_enter(dvp, vp, cnp)
599 	struct vnode *dvp;
600 	struct vnode *vp;
601 	struct componentname *cnp;
602 {
603 	struct namecache *ncp, *n2;
604 	struct nchashhead *ncpp;
605 	u_int32_t hash;
606 	int flag;
607 	int hold;
608 	int zap;
609 	int len;
610 
611 	CTR3(KTR_VFS, "cache_enter(%p, %p, %s)", dvp, vp, cnp->cn_nameptr);
612 	VNASSERT(vp == NULL || (vp->v_iflag & VI_DOOMED) == 0, vp,
613 	    ("cahe_enter: Adding a doomed vnode"));
614 
615 	if (!doingcache)
616 		return;
617 
618 	/*
619 	 * Avoid blowout in namecache entries.
620 	 */
621 	if (numcache >= desiredvnodes * 2)
622 		return;
623 
624 	flag = 0;
625 	if (cnp->cn_nameptr[0] == '.') {
626 		if (cnp->cn_namelen == 1)
627 			return;
628 		if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
629 			CACHE_WLOCK();
630 			/*
631 			 * If dotdot entry already exists, just retarget it
632 			 * to new parent vnode, otherwise continue with new
633 			 * namecache entry allocation.
634 			 */
635 			if ((ncp = dvp->v_cache_dd) != NULL &&
636 			    ncp->nc_flag & NCF_ISDOTDOT) {
637 				KASSERT(ncp->nc_dvp == dvp,
638 				    ("wrong isdotdot parent"));
639 				if (ncp->nc_vp != NULL)
640 					TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst,
641 					    ncp, nc_dst);
642 				else
643 					TAILQ_REMOVE(&ncneg, ncp, nc_dst);
644 				if (vp != NULL)
645 					TAILQ_INSERT_HEAD(&vp->v_cache_dst,
646 					    ncp, nc_dst);
647 				else
648 					TAILQ_INSERT_TAIL(&ncneg, ncp, nc_dst);
649 				ncp->nc_vp = vp;
650 				CACHE_WUNLOCK();
651 				return;
652 			}
653 			dvp->v_cache_dd = NULL;
654 			SDT_PROBE(vfs, namecache, enter, done, dvp, "..", vp,
655 			    0, 0);
656 			CACHE_WUNLOCK();
657 			flag = NCF_ISDOTDOT;
658 		}
659 	}
660 
661 	hold = 0;
662 	zap = 0;
663 
664 	/*
665 	 * Calculate the hash key and setup as much of the new
666 	 * namecache entry as possible before acquiring the lock.
667 	 */
668 	ncp = cache_alloc(cnp->cn_namelen);
669 	ncp->nc_vp = vp;
670 	ncp->nc_dvp = dvp;
671 	ncp->nc_flag = flag;
672 	len = ncp->nc_nlen = cnp->cn_namelen;
673 	hash = fnv_32_buf(cnp->cn_nameptr, len, FNV1_32_INIT);
674 	strlcpy(ncp->nc_name, cnp->cn_nameptr, len + 1);
675 	hash = fnv_32_buf(&dvp, sizeof(dvp), hash);
676 	CACHE_WLOCK();
677 
678 	/*
679 	 * See if this vnode or negative entry is already in the cache
680 	 * with this name.  This can happen with concurrent lookups of
681 	 * the same path name.
682 	 */
683 	ncpp = NCHHASH(hash);
684 	LIST_FOREACH(n2, ncpp, nc_hash) {
685 		if (n2->nc_dvp == dvp &&
686 		    n2->nc_nlen == cnp->cn_namelen &&
687 		    !bcmp(n2->nc_name, cnp->cn_nameptr, n2->nc_nlen)) {
688 			CACHE_WUNLOCK();
689 			cache_free(ncp);
690 			return;
691 		}
692 	}
693 
694 	if (flag == NCF_ISDOTDOT) {
695 		/*
696 		 * See if we are trying to add .. entry, but some other lookup
697 		 * has populated v_cache_dd pointer already.
698 		 */
699 		if (dvp->v_cache_dd != NULL) {
700 		    CACHE_WUNLOCK();
701 		    cache_free(ncp);
702 		    return;
703 		}
704 		KASSERT(vp == NULL || vp->v_type == VDIR,
705 		    ("wrong vnode type %p", vp));
706 		dvp->v_cache_dd = ncp;
707 	}
708 
709 	numcache++;
710 	if (!vp) {
711 		numneg++;
712 		if (cnp->cn_flags & ISWHITEOUT)
713 			ncp->nc_flag |= NCF_WHITE;
714 	} else if (vp->v_type == VDIR) {
715 		if (flag != NCF_ISDOTDOT) {
716 			if ((n2 = vp->v_cache_dd) != NULL &&
717 			    (n2->nc_flag & NCF_ISDOTDOT) != 0)
718 				cache_zap(n2);
719 			vp->v_cache_dd = ncp;
720 		}
721 	} else {
722 		vp->v_cache_dd = NULL;
723 	}
724 
725 	/*
726 	 * Insert the new namecache entry into the appropriate chain
727 	 * within the cache entries table.
728 	 */
729 	LIST_INSERT_HEAD(ncpp, ncp, nc_hash);
730 	if (flag != NCF_ISDOTDOT) {
731 		if (LIST_EMPTY(&dvp->v_cache_src)) {
732 			hold = 1;
733 			numcachehv++;
734 		}
735 		LIST_INSERT_HEAD(&dvp->v_cache_src, ncp, nc_src);
736 	}
737 
738 	/*
739 	 * If the entry is "negative", we place it into the
740 	 * "negative" cache queue, otherwise, we place it into the
741 	 * destination vnode's cache entries queue.
742 	 */
743 	if (vp) {
744 		TAILQ_INSERT_HEAD(&vp->v_cache_dst, ncp, nc_dst);
745 		SDT_PROBE(vfs, namecache, enter, done, dvp, ncp->nc_name, vp,
746 		    0, 0);
747 	} else {
748 		TAILQ_INSERT_TAIL(&ncneg, ncp, nc_dst);
749 		SDT_PROBE(vfs, namecache, enter_negative, done, dvp,
750 		    ncp->nc_name, 0, 0, 0);
751 	}
752 	if (numneg * ncnegfactor > numcache) {
753 		ncp = TAILQ_FIRST(&ncneg);
754 		zap = 1;
755 	}
756 	if (hold)
757 		vhold(dvp);
758 	if (zap)
759 		cache_zap(ncp);
760 	CACHE_WUNLOCK();
761 }
762 
763 /*
764  * Name cache initialization, from vfs_init() when we are booting
765  */
766 static void
767 nchinit(void *dummy __unused)
768 {
769 
770 	TAILQ_INIT(&ncneg);
771 
772 	cache_zone_small = uma_zcreate("S VFS Cache", CACHE_ZONE_SMALL, NULL,
773 	    NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
774 	cache_zone_large = uma_zcreate("L VFS Cache", CACHE_ZONE_LARGE, NULL,
775 	    NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
776 
777 	nchashtbl = hashinit(desiredvnodes * 2, M_VFSCACHE, &nchash);
778 }
779 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nchinit, NULL);
780 
781 
782 /*
783  * Invalidate all entries to a particular vnode.
784  */
785 void
786 cache_purge(vp)
787 	struct vnode *vp;
788 {
789 
790 	CTR1(KTR_VFS, "cache_purge(%p)", vp);
791 	SDT_PROBE(vfs, namecache, purge, done, vp, 0, 0, 0, 0);
792 	CACHE_WLOCK();
793 	while (!LIST_EMPTY(&vp->v_cache_src))
794 		cache_zap(LIST_FIRST(&vp->v_cache_src));
795 	while (!TAILQ_EMPTY(&vp->v_cache_dst))
796 		cache_zap(TAILQ_FIRST(&vp->v_cache_dst));
797 	if (vp->v_cache_dd != NULL) {
798 		KASSERT(vp->v_cache_dd->nc_flag & NCF_ISDOTDOT,
799 		   ("lost dotdot link"));
800 		cache_zap(vp->v_cache_dd);
801 	}
802 	KASSERT(vp->v_cache_dd == NULL, ("incomplete purge"));
803 	CACHE_WUNLOCK();
804 }
805 
806 /*
807  * Invalidate all negative entries for a particular directory vnode.
808  */
809 void
810 cache_purge_negative(vp)
811 	struct vnode *vp;
812 {
813 	struct namecache *cp, *ncp;
814 
815 	CTR1(KTR_VFS, "cache_purge_negative(%p)", vp);
816 	SDT_PROBE(vfs, namecache, purge_negative, done, vp, 0, 0, 0, 0);
817 	CACHE_WLOCK();
818 	LIST_FOREACH_SAFE(cp, &vp->v_cache_src, nc_src, ncp) {
819 		if (cp->nc_vp == NULL)
820 			cache_zap(cp);
821 	}
822 	CACHE_WUNLOCK();
823 }
824 
825 /*
826  * Flush all entries referencing a particular filesystem.
827  */
828 void
829 cache_purgevfs(mp)
830 	struct mount *mp;
831 {
832 	struct nchashhead *ncpp;
833 	struct namecache *ncp, *nnp;
834 
835 	/* Scan hash tables for applicable entries */
836 	SDT_PROBE(vfs, namecache, purgevfs, done, mp, 0, 0, 0, 0);
837 	CACHE_WLOCK();
838 	for (ncpp = &nchashtbl[nchash]; ncpp >= nchashtbl; ncpp--) {
839 		LIST_FOREACH_SAFE(ncp, ncpp, nc_hash, nnp) {
840 			if (ncp->nc_dvp->v_mount == mp)
841 				cache_zap(ncp);
842 		}
843 	}
844 	CACHE_WUNLOCK();
845 }
846 
847 /*
848  * Perform canonical checks and cache lookup and pass on to filesystem
849  * through the vop_cachedlookup only if needed.
850  */
851 
852 int
853 vfs_cache_lookup(ap)
854 	struct vop_lookup_args /* {
855 		struct vnode *a_dvp;
856 		struct vnode **a_vpp;
857 		struct componentname *a_cnp;
858 	} */ *ap;
859 {
860 	struct vnode *dvp;
861 	int error;
862 	struct vnode **vpp = ap->a_vpp;
863 	struct componentname *cnp = ap->a_cnp;
864 	struct ucred *cred = cnp->cn_cred;
865 	int flags = cnp->cn_flags;
866 	struct thread *td = cnp->cn_thread;
867 
868 	*vpp = NULL;
869 	dvp = ap->a_dvp;
870 
871 	if (dvp->v_type != VDIR)
872 		return (ENOTDIR);
873 
874 	if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
875 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
876 		return (EROFS);
877 
878 	error = VOP_ACCESS(dvp, VEXEC, cred, td);
879 	if (error)
880 		return (error);
881 
882 	error = cache_lookup(dvp, vpp, cnp);
883 	if (error == 0)
884 		return (VOP_CACHEDLOOKUP(dvp, vpp, cnp));
885 	if (error == -1)
886 		return (0);
887 	return (error);
888 }
889 
890 
891 #ifndef _SYS_SYSPROTO_H_
892 struct  __getcwd_args {
893 	u_char	*buf;
894 	u_int	buflen;
895 };
896 #endif
897 
898 /*
899  * XXX All of these sysctls would probably be more productive dead.
900  */
901 static int disablecwd;
902 SYSCTL_INT(_debug, OID_AUTO, disablecwd, CTLFLAG_RW, &disablecwd, 0,
903    "Disable the getcwd syscall");
904 
905 /* Implementation of the getcwd syscall. */
906 int
907 __getcwd(td, uap)
908 	struct thread *td;
909 	struct __getcwd_args *uap;
910 {
911 
912 	return (kern___getcwd(td, uap->buf, UIO_USERSPACE, uap->buflen));
913 }
914 
915 int
916 kern___getcwd(struct thread *td, u_char *buf, enum uio_seg bufseg, u_int buflen)
917 {
918 	char *bp, *tmpbuf;
919 	struct filedesc *fdp;
920 	struct vnode *cdir, *rdir;
921 	int error, vfslocked;
922 
923 	if (disablecwd)
924 		return (ENODEV);
925 	if (buflen < 2)
926 		return (EINVAL);
927 	if (buflen > MAXPATHLEN)
928 		buflen = MAXPATHLEN;
929 
930 	tmpbuf = malloc(buflen, M_TEMP, M_WAITOK);
931 	fdp = td->td_proc->p_fd;
932 	FILEDESC_SLOCK(fdp);
933 	cdir = fdp->fd_cdir;
934 	VREF(cdir);
935 	rdir = fdp->fd_rdir;
936 	VREF(rdir);
937 	FILEDESC_SUNLOCK(fdp);
938 	error = vn_fullpath1(td, cdir, rdir, tmpbuf, &bp, buflen);
939 	vfslocked = VFS_LOCK_GIANT(rdir->v_mount);
940 	vrele(rdir);
941 	VFS_UNLOCK_GIANT(vfslocked);
942 	vfslocked = VFS_LOCK_GIANT(cdir->v_mount);
943 	vrele(cdir);
944 	VFS_UNLOCK_GIANT(vfslocked);
945 
946 	if (!error) {
947 		if (bufseg == UIO_SYSSPACE)
948 			bcopy(bp, buf, strlen(bp) + 1);
949 		else
950 			error = copyout(bp, buf, strlen(bp) + 1);
951 #ifdef KTRACE
952 	if (KTRPOINT(curthread, KTR_NAMEI))
953 		ktrnamei(bp);
954 #endif
955 	}
956 	free(tmpbuf, M_TEMP);
957 	return (error);
958 }
959 
960 /*
961  * Thus begins the fullpath magic.
962  */
963 
964 #undef STATNODE
965 #define STATNODE(name)							\
966 	static u_int name;						\
967 	SYSCTL_UINT(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, 0, "")
968 
969 static int disablefullpath;
970 SYSCTL_INT(_debug, OID_AUTO, disablefullpath, CTLFLAG_RW, &disablefullpath, 0,
971 	"Disable the vn_fullpath function");
972 
973 /* These count for kern___getcwd(), too. */
974 STATNODE(numfullpathcalls);
975 STATNODE(numfullpathfail1);
976 STATNODE(numfullpathfail2);
977 STATNODE(numfullpathfail4);
978 STATNODE(numfullpathfound);
979 
980 /*
981  * Retrieve the full filesystem path that correspond to a vnode from the name
982  * cache (if available)
983  */
984 int
985 vn_fullpath(struct thread *td, struct vnode *vn, char **retbuf, char **freebuf)
986 {
987 	char *buf;
988 	struct filedesc *fdp;
989 	struct vnode *rdir;
990 	int error, vfslocked;
991 
992 	if (disablefullpath)
993 		return (ENODEV);
994 	if (vn == NULL)
995 		return (EINVAL);
996 
997 	buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
998 	fdp = td->td_proc->p_fd;
999 	FILEDESC_SLOCK(fdp);
1000 	rdir = fdp->fd_rdir;
1001 	VREF(rdir);
1002 	FILEDESC_SUNLOCK(fdp);
1003 	error = vn_fullpath1(td, vn, rdir, buf, retbuf, MAXPATHLEN);
1004 	vfslocked = VFS_LOCK_GIANT(rdir->v_mount);
1005 	vrele(rdir);
1006 	VFS_UNLOCK_GIANT(vfslocked);
1007 
1008 	if (!error)
1009 		*freebuf = buf;
1010 	else
1011 		free(buf, M_TEMP);
1012 	return (error);
1013 }
1014 
1015 /*
1016  * This function is similar to vn_fullpath, but it attempts to lookup the
1017  * pathname relative to the global root mount point.  This is required for the
1018  * auditing sub-system, as audited pathnames must be absolute, relative to the
1019  * global root mount point.
1020  */
1021 int
1022 vn_fullpath_global(struct thread *td, struct vnode *vn,
1023     char **retbuf, char **freebuf)
1024 {
1025 	char *buf;
1026 	int error;
1027 
1028 	if (disablefullpath)
1029 		return (ENODEV);
1030 	if (vn == NULL)
1031 		return (EINVAL);
1032 	buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1033 	error = vn_fullpath1(td, vn, rootvnode, buf, retbuf, MAXPATHLEN);
1034 	if (!error)
1035 		*freebuf = buf;
1036 	else
1037 		free(buf, M_TEMP);
1038 	return (error);
1039 }
1040 
1041 int
1042 vn_vptocnp(struct vnode **vp, struct ucred *cred, char *buf, u_int *buflen)
1043 {
1044 	int error;
1045 
1046 	CACHE_RLOCK();
1047 	error = vn_vptocnp_locked(vp, cred, buf, buflen);
1048 	if (error == 0) {
1049 		/*
1050 		 * vn_vptocnp_locked() dropped hold acquired by
1051 		 * VOP_VPTOCNP immediately after locking the
1052 		 * cache. Since we are going to drop the cache rlock,
1053 		 * re-hold the result.
1054 		 */
1055 		vhold(*vp);
1056 		CACHE_RUNLOCK();
1057 	}
1058 	return (error);
1059 }
1060 
1061 static int
1062 vn_vptocnp_locked(struct vnode **vp, struct ucred *cred, char *buf,
1063     u_int *buflen)
1064 {
1065 	struct vnode *dvp;
1066 	struct namecache *ncp;
1067 	int error, vfslocked;
1068 
1069 	TAILQ_FOREACH(ncp, &((*vp)->v_cache_dst), nc_dst) {
1070 		if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1071 			break;
1072 	}
1073 	if (ncp != NULL) {
1074 		if (*buflen < ncp->nc_nlen) {
1075 			CACHE_RUNLOCK();
1076 			numfullpathfail4++;
1077 			error = ENOMEM;
1078 			SDT_PROBE(vfs, namecache, fullpath, return, error,
1079 			    vp, NULL, 0, 0);
1080 			return (error);
1081 		}
1082 		*buflen -= ncp->nc_nlen;
1083 		memcpy(buf + *buflen, ncp->nc_name, ncp->nc_nlen);
1084 		SDT_PROBE(vfs, namecache, fullpath, hit, ncp->nc_dvp,
1085 		    ncp->nc_name, vp, 0, 0);
1086 		*vp = ncp->nc_dvp;
1087 		return (0);
1088 	}
1089 	SDT_PROBE(vfs, namecache, fullpath, miss, vp, 0, 0, 0, 0);
1090 
1091 	vhold(*vp);
1092 	CACHE_RUNLOCK();
1093 	vfslocked = VFS_LOCK_GIANT((*vp)->v_mount);
1094 	vn_lock(*vp, LK_SHARED | LK_RETRY);
1095 	error = VOP_VPTOCNP(*vp, &dvp, cred, buf, buflen);
1096 	VOP_UNLOCK(*vp, 0);
1097 	vdrop(*vp);
1098 	VFS_UNLOCK_GIANT(vfslocked);
1099 	if (error) {
1100 		numfullpathfail2++;
1101 		SDT_PROBE(vfs, namecache, fullpath, return,  error, vp,
1102 		    NULL, 0, 0);
1103 		return (error);
1104 	}
1105 
1106 	*vp = dvp;
1107 	CACHE_RLOCK();
1108 	if ((*vp)->v_iflag & VI_DOOMED) {
1109 		/* forced unmount */
1110 		CACHE_RUNLOCK();
1111 		vdrop(*vp);
1112 		error = ENOENT;
1113 		SDT_PROBE(vfs, namecache, fullpath, return, error, vp,
1114 		    NULL, 0, 0);
1115 		return (error);
1116 	}
1117 	vdrop(*vp);
1118 
1119 	return (0);
1120 }
1121 
1122 /*
1123  * The magic behind kern___getcwd() and vn_fullpath().
1124  */
1125 static int
1126 vn_fullpath1(struct thread *td, struct vnode *vp, struct vnode *rdir,
1127     char *buf, char **retbuf, u_int buflen)
1128 {
1129 	int error, slash_prefixed;
1130 #ifdef KDTRACE_HOOKS
1131 	struct vnode *startvp = vp;
1132 #endif
1133 
1134 	buflen--;
1135 	buf[buflen] = '\0';
1136 	error = 0;
1137 	slash_prefixed = 0;
1138 
1139 	SDT_PROBE(vfs, namecache, fullpath, entry, vp, 0, 0, 0, 0);
1140 	numfullpathcalls++;
1141 	CACHE_RLOCK();
1142 	if (vp->v_type != VDIR) {
1143 		error = vn_vptocnp_locked(&vp, td->td_ucred, buf, &buflen);
1144 		if (error)
1145 			return (error);
1146 		if (buflen == 0) {
1147 			CACHE_RUNLOCK();
1148 			return (ENOMEM);
1149 		}
1150 		buf[--buflen] = '/';
1151 		slash_prefixed = 1;
1152 	}
1153 	while (vp != rdir && vp != rootvnode) {
1154 		if (vp->v_vflag & VV_ROOT) {
1155 			if (vp->v_iflag & VI_DOOMED) {	/* forced unmount */
1156 				CACHE_RUNLOCK();
1157 				error = ENOENT;
1158 				SDT_PROBE(vfs, namecache, fullpath, return,
1159 				    error, vp, NULL, 0, 0);
1160 				break;
1161 			}
1162 			vp = vp->v_mount->mnt_vnodecovered;
1163 			continue;
1164 		}
1165 		if (vp->v_type != VDIR) {
1166 			CACHE_RUNLOCK();
1167 			numfullpathfail1++;
1168 			error = ENOTDIR;
1169 			SDT_PROBE(vfs, namecache, fullpath, return,
1170 			    error, vp, NULL, 0, 0);
1171 			break;
1172 		}
1173 		error = vn_vptocnp_locked(&vp, td->td_ucred, buf, &buflen);
1174 		if (error)
1175 			break;
1176 		if (buflen == 0) {
1177 			CACHE_RUNLOCK();
1178 			error = ENOMEM;
1179 			SDT_PROBE(vfs, namecache, fullpath, return, error,
1180 			    startvp, NULL, 0, 0);
1181 			break;
1182 		}
1183 		buf[--buflen] = '/';
1184 		slash_prefixed = 1;
1185 	}
1186 	if (error)
1187 		return (error);
1188 	if (!slash_prefixed) {
1189 		if (buflen == 0) {
1190 			CACHE_RUNLOCK();
1191 			numfullpathfail4++;
1192 			SDT_PROBE(vfs, namecache, fullpath, return, ENOMEM,
1193 			    startvp, NULL, 0, 0);
1194 			return (ENOMEM);
1195 		}
1196 		buf[--buflen] = '/';
1197 	}
1198 	numfullpathfound++;
1199 	CACHE_RUNLOCK();
1200 
1201 	SDT_PROBE(vfs, namecache, fullpath, return, 0, startvp, buf + buflen,
1202 	    0, 0);
1203 	*retbuf = buf + buflen;
1204 	return (0);
1205 }
1206 
1207 int
1208 vn_commname(struct vnode *vp, char *buf, u_int buflen)
1209 {
1210 	struct namecache *ncp;
1211 	int l;
1212 
1213 	CACHE_RLOCK();
1214 	TAILQ_FOREACH(ncp, &vp->v_cache_dst, nc_dst)
1215 		if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1216 			break;
1217 	if (ncp == NULL) {
1218 		CACHE_RUNLOCK();
1219 		return (ENOENT);
1220 	}
1221 	l = min(ncp->nc_nlen, buflen - 1);
1222 	memcpy(buf, ncp->nc_name, l);
1223 	CACHE_RUNLOCK();
1224 	buf[l] = '\0';
1225 	return (0);
1226 }
1227