]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/kern/vfs_cache.c
Copy head to stable/8 as part of 8.0 Release cycle.
[FreeBSD/stable/8.git] / sys / kern / vfs_cache.c
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                                 goto unlock;
420                         }
421                         if (dvp->v_cache_dd->nc_flag & NCF_ISDOTDOT)
422                                 *vpp = dvp->v_cache_dd->nc_vp;
423                         else
424                                 *vpp = dvp->v_cache_dd->nc_dvp;
425                         /* Return failure if negative entry was found. */
426                         if (*vpp == NULL) {
427                                 ncp = dvp->v_cache_dd;
428                                 goto negative_success;
429                         }
430                         CTR3(KTR_VFS, "cache_lookup(%p, %s) found %p via ..",
431                             dvp, cnp->cn_nameptr, *vpp);
432                         SDT_PROBE(vfs, namecache, lookup, hit, dvp, "..",
433                             *vpp, 0, 0);
434                         goto success;
435                 }
436         }
437
438         hash = fnv_32_buf(cnp->cn_nameptr, cnp->cn_namelen, FNV1_32_INIT);
439         hash = fnv_32_buf(&dvp, sizeof(dvp), hash);
440         LIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
441                 numchecks++;
442                 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
443                     !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
444                         break;
445         }
446
447         /* We failed to find an entry */
448         if (ncp == NULL) {
449                 SDT_PROBE(vfs, namecache, lookup, miss, dvp, cnp->cn_nameptr,
450                     NULL, 0, 0);
451                 if ((cnp->cn_flags & MAKEENTRY) == 0) {
452                         nummisszap++;
453                 } else {
454                         nummiss++;
455                 }
456                 nchstats.ncs_miss++;
457                 goto unlock;
458         }
459
460         /* We don't want to have an entry, so dump it */
461         if ((cnp->cn_flags & MAKEENTRY) == 0) {
462                 numposzaps++;
463                 nchstats.ncs_badhits++;
464                 if (!wlocked && !CACHE_UPGRADE_LOCK())
465                         goto wlock;
466                 cache_zap(ncp);
467                 CACHE_WUNLOCK();
468                 return (0);
469         }
470
471         /* We found a "positive" match, return the vnode */
472         if (ncp->nc_vp) {
473                 numposhits++;
474                 nchstats.ncs_goodhits++;
475                 *vpp = ncp->nc_vp;
476                 CTR4(KTR_VFS, "cache_lookup(%p, %s) found %p via ncp %p",
477                     dvp, cnp->cn_nameptr, *vpp, ncp);
478                 SDT_PROBE(vfs, namecache, lookup, hit, dvp, ncp->nc_name,
479                     *vpp, 0, 0);
480                 goto success;
481         }
482
483 negative_success:
484         /* We found a negative match, and want to create it, so purge */
485         if (cnp->cn_nameiop == CREATE) {
486                 numnegzaps++;
487                 nchstats.ncs_badhits++;
488                 if (!wlocked && !CACHE_UPGRADE_LOCK())
489                         goto wlock;
490                 cache_zap(ncp);
491                 CACHE_WUNLOCK();
492                 return (0);
493         }
494
495         if (!wlocked && !CACHE_UPGRADE_LOCK())
496                 goto wlock;
497         numneghits++;
498         /*
499          * We found a "negative" match, so we shift it to the end of
500          * the "negative" cache entries queue to satisfy LRU.  Also,
501          * check to see if the entry is a whiteout; indicate this to
502          * the componentname, if so.
503          */
504         TAILQ_REMOVE(&ncneg, ncp, nc_dst);
505         TAILQ_INSERT_TAIL(&ncneg, ncp, nc_dst);
506         nchstats.ncs_neghits++;
507         if (ncp->nc_flag & NCF_WHITE)
508                 cnp->cn_flags |= ISWHITEOUT;
509         SDT_PROBE(vfs, namecache, lookup, hit_negative, dvp, ncp->nc_name,
510             0, 0, 0);
511         CACHE_WUNLOCK();
512         return (ENOENT);
513
514 wlock:
515         /*
516          * We need to update the cache after our lookup, so upgrade to
517          * a write lock and retry the operation.
518          */
519         CACHE_RUNLOCK();
520         CACHE_WLOCK();
521         numupgrades++;
522         wlocked = 1;
523         goto retry_wlocked;
524
525 success:
526         /*
527          * On success we return a locked and ref'd vnode as per the lookup
528          * protocol.
529          */
530         if (dvp == *vpp) {   /* lookup on "." */
531                 VREF(*vpp);
532                 if (wlocked)
533                         CACHE_WUNLOCK();
534                 else
535                         CACHE_RUNLOCK();
536                 /*
537                  * When we lookup "." we still can be asked to lock it
538                  * differently...
539                  */
540                 ltype = cnp->cn_lkflags & LK_TYPE_MASK;
541                 if (ltype != VOP_ISLOCKED(*vpp)) {
542                         if (ltype == LK_EXCLUSIVE) {
543                                 vn_lock(*vpp, LK_UPGRADE | LK_RETRY);
544                                 if ((*vpp)->v_iflag & VI_DOOMED) {
545                                         /* forced unmount */
546                                         vrele(*vpp);
547                                         *vpp = NULL;
548                                         return (ENOENT);
549                                 }
550                         } else
551                                 vn_lock(*vpp, LK_DOWNGRADE | LK_RETRY);
552                 }
553                 return (-1);
554         }
555         ltype = 0;      /* silence gcc warning */
556         if (cnp->cn_flags & ISDOTDOT) {
557                 ltype = VOP_ISLOCKED(dvp);
558                 VOP_UNLOCK(dvp, 0);
559         }
560         VI_LOCK(*vpp);
561         if (wlocked)
562                 CACHE_WUNLOCK();
563         else
564                 CACHE_RUNLOCK();
565         error = vget(*vpp, cnp->cn_lkflags | LK_INTERLOCK, cnp->cn_thread);
566         if (cnp->cn_flags & ISDOTDOT) {
567                 vn_lock(dvp, ltype | LK_RETRY);
568                 if (dvp->v_iflag & VI_DOOMED) {
569                         if (error == 0)
570                                 vput(*vpp);
571                         *vpp = NULL;
572                         return (ENOENT);
573                 }
574         }
575         if (error) {
576                 *vpp = NULL;
577                 goto retry;
578         }
579         if ((cnp->cn_flags & ISLASTCN) &&
580             (cnp->cn_lkflags & LK_TYPE_MASK) == LK_EXCLUSIVE) {
581                 ASSERT_VOP_ELOCKED(*vpp, "cache_lookup");
582         }
583         return (-1);
584
585 unlock:
586         if (wlocked)
587                 CACHE_WUNLOCK();
588         else
589                 CACHE_RUNLOCK();
590         return (0);
591 }
592
593 /*
594  * Add an entry to the cache.
595  */
596 void
597 cache_enter(dvp, vp, cnp)
598         struct vnode *dvp;
599         struct vnode *vp;
600         struct componentname *cnp;
601 {
602         struct namecache *ncp, *n2;
603         struct nchashhead *ncpp;
604         u_int32_t hash;
605         int flag;
606         int hold;
607         int zap;
608         int len;
609
610         CTR3(KTR_VFS, "cache_enter(%p, %p, %s)", dvp, vp, cnp->cn_nameptr);
611         VNASSERT(vp == NULL || (vp->v_iflag & VI_DOOMED) == 0, vp,
612             ("cahe_enter: Adding a doomed vnode"));
613
614         if (!doingcache)
615                 return;
616
617         /*
618          * Avoid blowout in namecache entries.
619          */
620         if (numcache >= desiredvnodes * 2)
621                 return;
622
623         flag = 0;
624         if (cnp->cn_nameptr[0] == '.') {
625                 if (cnp->cn_namelen == 1)
626                         return;
627                 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
628                         CACHE_WLOCK();
629                         /*
630                          * If dotdot entry already exists, just retarget it
631                          * to new parent vnode, otherwise continue with new
632                          * namecache entry allocation.
633                          */
634                         if ((ncp = dvp->v_cache_dd) != NULL &&
635                             ncp->nc_flag & NCF_ISDOTDOT) {
636                                 KASSERT(ncp->nc_dvp == dvp,
637                                     ("wrong isdotdot parent"));
638                                 if (ncp->nc_vp != NULL)
639                                         TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst,
640                                             ncp, nc_dst);
641                                 else
642                                         TAILQ_REMOVE(&ncneg, ncp, nc_dst);
643                                 if (vp != NULL)
644                                         TAILQ_INSERT_HEAD(&vp->v_cache_dst,
645                                             ncp, nc_dst);
646                                 else
647                                         TAILQ_INSERT_TAIL(&ncneg, ncp, nc_dst);
648                                 ncp->nc_vp = vp;
649                                 CACHE_WUNLOCK();
650                                 return;
651                         }
652                         dvp->v_cache_dd = NULL;
653                         SDT_PROBE(vfs, namecache, enter, done, dvp, "..", vp,
654                             0, 0);
655                         CACHE_WUNLOCK();
656                         flag = NCF_ISDOTDOT;
657                 }
658         }
659
660         hold = 0;
661         zap = 0;
662
663         /*
664          * Calculate the hash key and setup as much of the new
665          * namecache entry as possible before acquiring the lock.
666          */
667         ncp = cache_alloc(cnp->cn_namelen);
668         ncp->nc_vp = vp;
669         ncp->nc_dvp = dvp;
670         ncp->nc_flag = flag;
671         len = ncp->nc_nlen = cnp->cn_namelen;
672         hash = fnv_32_buf(cnp->cn_nameptr, len, FNV1_32_INIT);
673         strlcpy(ncp->nc_name, cnp->cn_nameptr, len + 1);
674         hash = fnv_32_buf(&dvp, sizeof(dvp), hash);
675         CACHE_WLOCK();
676
677         /*
678          * See if this vnode or negative entry is already in the cache
679          * with this name.  This can happen with concurrent lookups of
680          * the same path name.
681          */
682         ncpp = NCHHASH(hash);
683         LIST_FOREACH(n2, ncpp, nc_hash) {
684                 if (n2->nc_dvp == dvp &&
685                     n2->nc_nlen == cnp->cn_namelen &&
686                     !bcmp(n2->nc_name, cnp->cn_nameptr, n2->nc_nlen)) {
687                         CACHE_WUNLOCK();
688                         cache_free(ncp);
689                         return;
690                 }
691         }
692
693         if (flag == NCF_ISDOTDOT) {
694                 /*
695                  * See if we are trying to add .. entry, but some other lookup
696                  * has populated v_cache_dd pointer already.
697                  */
698                 if (dvp->v_cache_dd != NULL) {
699                     CACHE_WUNLOCK();
700                     cache_free(ncp);
701                     return;
702                 }
703                 KASSERT(vp == NULL || vp->v_type == VDIR,
704                     ("wrong vnode type %p", vp));
705                 dvp->v_cache_dd = ncp;
706         }
707
708         numcache++;
709         if (!vp) {
710                 numneg++;
711                 if (cnp->cn_flags & ISWHITEOUT)
712                         ncp->nc_flag |= NCF_WHITE;
713         } else if (vp->v_type == VDIR) {
714                 if (flag != NCF_ISDOTDOT) {
715                         if ((n2 = vp->v_cache_dd) != NULL &&
716                             (n2->nc_flag & NCF_ISDOTDOT) != 0)
717                                 cache_zap(n2);
718                         vp->v_cache_dd = ncp;
719                 }
720         } else {
721                 vp->v_cache_dd = NULL;
722         }
723
724         /*
725          * Insert the new namecache entry into the appropriate chain
726          * within the cache entries table.
727          */
728         LIST_INSERT_HEAD(ncpp, ncp, nc_hash);
729         if (flag != NCF_ISDOTDOT) {
730                 if (LIST_EMPTY(&dvp->v_cache_src)) {
731                         hold = 1;
732                         numcachehv++;
733                 }
734                 LIST_INSERT_HEAD(&dvp->v_cache_src, ncp, nc_src);
735         }
736
737         /*
738          * If the entry is "negative", we place it into the
739          * "negative" cache queue, otherwise, we place it into the
740          * destination vnode's cache entries queue.
741          */
742         if (vp) {
743                 TAILQ_INSERT_HEAD(&vp->v_cache_dst, ncp, nc_dst);
744                 SDT_PROBE(vfs, namecache, enter, done, dvp, ncp->nc_name, vp,
745                     0, 0);
746         } else {
747                 TAILQ_INSERT_TAIL(&ncneg, ncp, nc_dst);
748                 SDT_PROBE(vfs, namecache, enter_negative, done, dvp,
749                     ncp->nc_name, 0, 0, 0);
750         }
751         if (numneg * ncnegfactor > numcache) {
752                 ncp = TAILQ_FIRST(&ncneg);
753                 zap = 1;
754         }
755         if (hold)
756                 vhold(dvp);
757         if (zap)
758                 cache_zap(ncp);
759         CACHE_WUNLOCK();
760 }
761
762 /*
763  * Name cache initialization, from vfs_init() when we are booting
764  */
765 static void
766 nchinit(void *dummy __unused)
767 {
768
769         TAILQ_INIT(&ncneg);
770
771         cache_zone_small = uma_zcreate("S VFS Cache", CACHE_ZONE_SMALL, NULL,
772             NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
773         cache_zone_large = uma_zcreate("L VFS Cache", CACHE_ZONE_LARGE, NULL,
774             NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
775
776         nchashtbl = hashinit(desiredvnodes * 2, M_VFSCACHE, &nchash);
777 }
778 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nchinit, NULL);
779
780
781 /*
782  * Invalidate all entries to a particular vnode.
783  */
784 void
785 cache_purge(vp)
786         struct vnode *vp;
787 {
788
789         CTR1(KTR_VFS, "cache_purge(%p)", vp);
790         SDT_PROBE(vfs, namecache, purge, done, vp, 0, 0, 0, 0);
791         CACHE_WLOCK();
792         while (!LIST_EMPTY(&vp->v_cache_src))
793                 cache_zap(LIST_FIRST(&vp->v_cache_src));
794         while (!TAILQ_EMPTY(&vp->v_cache_dst))
795                 cache_zap(TAILQ_FIRST(&vp->v_cache_dst));
796         if (vp->v_cache_dd != NULL) {
797                 KASSERT(vp->v_cache_dd->nc_flag & NCF_ISDOTDOT,
798                    ("lost dotdot link"));
799                 cache_zap(vp->v_cache_dd);
800         }
801         KASSERT(vp->v_cache_dd == NULL, ("incomplete purge"));
802         CACHE_WUNLOCK();
803 }
804
805 /*
806  * Invalidate all negative entries for a particular directory vnode.
807  */
808 void
809 cache_purge_negative(vp)
810         struct vnode *vp;
811 {
812         struct namecache *cp, *ncp;
813
814         CTR1(KTR_VFS, "cache_purge_negative(%p)", vp);
815         SDT_PROBE(vfs, namecache, purge_negative, done, vp, 0, 0, 0, 0);
816         CACHE_WLOCK();
817         LIST_FOREACH_SAFE(cp, &vp->v_cache_src, nc_src, ncp) {
818                 if (cp->nc_vp == NULL)
819                         cache_zap(cp);
820         }
821         CACHE_WUNLOCK();
822 }
823
824 /*
825  * Flush all entries referencing a particular filesystem.
826  */
827 void
828 cache_purgevfs(mp)
829         struct mount *mp;
830 {
831         struct nchashhead *ncpp;
832         struct namecache *ncp, *nnp;
833
834         /* Scan hash tables for applicable entries */
835         SDT_PROBE(vfs, namecache, purgevfs, done, mp, 0, 0, 0, 0);
836         CACHE_WLOCK();
837         for (ncpp = &nchashtbl[nchash]; ncpp >= nchashtbl; ncpp--) {
838                 LIST_FOREACH_SAFE(ncp, ncpp, nc_hash, nnp) {
839                         if (ncp->nc_dvp->v_mount == mp)
840                                 cache_zap(ncp);
841                 }
842         }
843         CACHE_WUNLOCK();
844 }
845
846 /*
847  * Perform canonical checks and cache lookup and pass on to filesystem
848  * through the vop_cachedlookup only if needed.
849  */
850
851 int
852 vfs_cache_lookup(ap)
853         struct vop_lookup_args /* {
854                 struct vnode *a_dvp;
855                 struct vnode **a_vpp;
856                 struct componentname *a_cnp;
857         } */ *ap;
858 {
859         struct vnode *dvp;
860         int error;
861         struct vnode **vpp = ap->a_vpp;
862         struct componentname *cnp = ap->a_cnp;
863         struct ucred *cred = cnp->cn_cred;
864         int flags = cnp->cn_flags;
865         struct thread *td = cnp->cn_thread;
866
867         *vpp = NULL;
868         dvp = ap->a_dvp;
869
870         if (dvp->v_type != VDIR)
871                 return (ENOTDIR);
872
873         if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
874             (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
875                 return (EROFS);
876
877         error = VOP_ACCESS(dvp, VEXEC, cred, td);
878         if (error)
879                 return (error);
880
881         error = cache_lookup(dvp, vpp, cnp);
882         if (error == 0)
883                 return (VOP_CACHEDLOOKUP(dvp, vpp, cnp));
884         if (error == -1)
885                 return (0);
886         return (error);
887 }
888
889
890 #ifndef _SYS_SYSPROTO_H_
891 struct  __getcwd_args {
892         u_char  *buf;
893         u_int   buflen;
894 };
895 #endif
896
897 /*
898  * XXX All of these sysctls would probably be more productive dead.
899  */
900 static int disablecwd;
901 SYSCTL_INT(_debug, OID_AUTO, disablecwd, CTLFLAG_RW, &disablecwd, 0,
902    "Disable the getcwd syscall");
903
904 /* Implementation of the getcwd syscall. */
905 int
906 __getcwd(td, uap)
907         struct thread *td;
908         struct __getcwd_args *uap;
909 {
910
911         return (kern___getcwd(td, uap->buf, UIO_USERSPACE, uap->buflen));
912 }
913
914 int
915 kern___getcwd(struct thread *td, u_char *buf, enum uio_seg bufseg, u_int buflen)
916 {
917         char *bp, *tmpbuf;
918         struct filedesc *fdp;
919         struct vnode *cdir, *rdir;
920         int error, vfslocked;
921
922         if (disablecwd)
923                 return (ENODEV);
924         if (buflen < 2)
925                 return (EINVAL);
926         if (buflen > MAXPATHLEN)
927                 buflen = MAXPATHLEN;
928
929         tmpbuf = malloc(buflen, M_TEMP, M_WAITOK);
930         fdp = td->td_proc->p_fd;
931         FILEDESC_SLOCK(fdp);
932         cdir = fdp->fd_cdir;
933         VREF(cdir);
934         rdir = fdp->fd_rdir;
935         VREF(rdir);
936         FILEDESC_SUNLOCK(fdp);
937         error = vn_fullpath1(td, cdir, rdir, tmpbuf, &bp, buflen);
938         vfslocked = VFS_LOCK_GIANT(rdir->v_mount);
939         vrele(rdir);
940         VFS_UNLOCK_GIANT(vfslocked);
941         vfslocked = VFS_LOCK_GIANT(cdir->v_mount);
942         vrele(cdir);
943         VFS_UNLOCK_GIANT(vfslocked);
944
945         if (!error) {
946                 if (bufseg == UIO_SYSSPACE)
947                         bcopy(bp, buf, strlen(bp) + 1);
948                 else
949                         error = copyout(bp, buf, strlen(bp) + 1);
950 #ifdef KTRACE
951         if (KTRPOINT(curthread, KTR_NAMEI))
952                 ktrnamei(bp);
953 #endif
954         }
955         free(tmpbuf, M_TEMP);
956         return (error);
957 }
958
959 /*
960  * Thus begins the fullpath magic.
961  */
962
963 #undef STATNODE
964 #define STATNODE(name)                                                  \
965         static u_int name;                                              \
966         SYSCTL_UINT(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, 0, "")
967
968 static int disablefullpath;
969 SYSCTL_INT(_debug, OID_AUTO, disablefullpath, CTLFLAG_RW, &disablefullpath, 0,
970         "Disable the vn_fullpath function");
971
972 /* These count for kern___getcwd(), too. */
973 STATNODE(numfullpathcalls);
974 STATNODE(numfullpathfail1);
975 STATNODE(numfullpathfail2);
976 STATNODE(numfullpathfail4);
977 STATNODE(numfullpathfound);
978
979 /*
980  * Retrieve the full filesystem path that correspond to a vnode from the name
981  * cache (if available)
982  */
983 int
984 vn_fullpath(struct thread *td, struct vnode *vn, char **retbuf, char **freebuf)
985 {
986         char *buf;
987         struct filedesc *fdp;
988         struct vnode *rdir;
989         int error, vfslocked;
990
991         if (disablefullpath)
992                 return (ENODEV);
993         if (vn == NULL)
994                 return (EINVAL);
995
996         buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
997         fdp = td->td_proc->p_fd;
998         FILEDESC_SLOCK(fdp);
999         rdir = fdp->fd_rdir;
1000         VREF(rdir);
1001         FILEDESC_SUNLOCK(fdp);
1002         error = vn_fullpath1(td, vn, rdir, buf, retbuf, MAXPATHLEN);
1003         vfslocked = VFS_LOCK_GIANT(rdir->v_mount);
1004         vrele(rdir);
1005         VFS_UNLOCK_GIANT(vfslocked);
1006
1007         if (!error)
1008                 *freebuf = buf;
1009         else
1010                 free(buf, M_TEMP);
1011         return (error);
1012 }
1013
1014 /*
1015  * This function is similar to vn_fullpath, but it attempts to lookup the
1016  * pathname relative to the global root mount point.  This is required for the
1017  * auditing sub-system, as audited pathnames must be absolute, relative to the
1018  * global root mount point.
1019  */
1020 int
1021 vn_fullpath_global(struct thread *td, struct vnode *vn,
1022     char **retbuf, char **freebuf)
1023 {
1024         char *buf;
1025         int error;
1026
1027         if (disablefullpath)
1028                 return (ENODEV);
1029         if (vn == NULL)
1030                 return (EINVAL);
1031         buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1032         error = vn_fullpath1(td, vn, rootvnode, buf, retbuf, MAXPATHLEN);
1033         if (!error)
1034                 *freebuf = buf;
1035         else
1036                 free(buf, M_TEMP);
1037         return (error);
1038 }
1039
1040 int
1041 vn_vptocnp(struct vnode **vp, struct ucred *cred, char *buf, u_int *buflen)
1042 {
1043         int error;
1044
1045         CACHE_RLOCK();
1046         error = vn_vptocnp_locked(vp, cred, buf, buflen);
1047         if (error == 0) {
1048                 /*
1049                  * vn_vptocnp_locked() dropped hold acquired by
1050                  * VOP_VPTOCNP immediately after locking the
1051                  * cache. Since we are going to drop the cache rlock,
1052                  * re-hold the result.
1053                  */
1054                 vhold(*vp);
1055                 CACHE_RUNLOCK();
1056         }
1057         return (error);
1058 }
1059
1060 static int
1061 vn_vptocnp_locked(struct vnode **vp, struct ucred *cred, char *buf,
1062     u_int *buflen)
1063 {
1064         struct vnode *dvp;
1065         struct namecache *ncp;
1066         int error, vfslocked;
1067
1068         TAILQ_FOREACH(ncp, &((*vp)->v_cache_dst), nc_dst) {
1069                 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1070                         break;
1071         }
1072         if (ncp != NULL) {
1073                 if (*buflen < ncp->nc_nlen) {
1074                         CACHE_RUNLOCK();
1075                         numfullpathfail4++;
1076                         error = ENOMEM;
1077                         SDT_PROBE(vfs, namecache, fullpath, return, error,
1078                             vp, NULL, 0, 0);
1079                         return (error);
1080                 }
1081                 *buflen -= ncp->nc_nlen;
1082                 memcpy(buf + *buflen, ncp->nc_name, ncp->nc_nlen);
1083                 SDT_PROBE(vfs, namecache, fullpath, hit, ncp->nc_dvp,
1084                     ncp->nc_name, vp, 0, 0);
1085                 *vp = ncp->nc_dvp;
1086                 return (0);
1087         }
1088         SDT_PROBE(vfs, namecache, fullpath, miss, vp, 0, 0, 0, 0);
1089
1090         vhold(*vp);
1091         CACHE_RUNLOCK();
1092         vfslocked = VFS_LOCK_GIANT((*vp)->v_mount);
1093         vn_lock(*vp, LK_SHARED | LK_RETRY);
1094         error = VOP_VPTOCNP(*vp, &dvp, cred, buf, buflen);
1095         VOP_UNLOCK(*vp, 0);
1096         vdrop(*vp);
1097         VFS_UNLOCK_GIANT(vfslocked);
1098         if (error) {
1099                 numfullpathfail2++;
1100                 SDT_PROBE(vfs, namecache, fullpath, return,  error, vp,
1101                     NULL, 0, 0);
1102                 return (error);
1103         }
1104
1105         *vp = dvp;
1106         CACHE_RLOCK();
1107         if ((*vp)->v_iflag & VI_DOOMED) {
1108                 /* forced unmount */
1109                 CACHE_RUNLOCK();
1110                 vdrop(*vp);
1111                 error = ENOENT;
1112                 SDT_PROBE(vfs, namecache, fullpath, return, error, vp,
1113                     NULL, 0, 0);
1114                 return (error);
1115         }
1116         vdrop(*vp);
1117
1118         return (0);
1119 }
1120
1121 /*
1122  * The magic behind kern___getcwd() and vn_fullpath().
1123  */
1124 static int
1125 vn_fullpath1(struct thread *td, struct vnode *vp, struct vnode *rdir,
1126     char *buf, char **retbuf, u_int buflen)
1127 {
1128         int error, slash_prefixed;
1129 #ifdef KDTRACE_HOOKS
1130         struct vnode *startvp = vp;
1131 #endif
1132
1133         buflen--;
1134         buf[buflen] = '\0';
1135         error = 0;
1136         slash_prefixed = 0;
1137
1138         SDT_PROBE(vfs, namecache, fullpath, entry, vp, 0, 0, 0, 0);
1139         numfullpathcalls++;
1140         CACHE_RLOCK();
1141         if (vp->v_type != VDIR) {
1142                 error = vn_vptocnp_locked(&vp, td->td_ucred, buf, &buflen);
1143                 if (error)
1144                         return (error);
1145                 if (buflen == 0) {
1146                         CACHE_RUNLOCK();
1147                         return (ENOMEM);
1148                 }
1149                 buf[--buflen] = '/';
1150                 slash_prefixed = 1;
1151         }
1152         while (vp != rdir && vp != rootvnode) {
1153                 if (vp->v_vflag & VV_ROOT) {
1154                         if (vp->v_iflag & VI_DOOMED) {  /* forced unmount */
1155                                 CACHE_RUNLOCK();
1156                                 error = ENOENT;
1157                                 SDT_PROBE(vfs, namecache, fullpath, return,
1158                                     error, vp, NULL, 0, 0);
1159                                 break;
1160                         }
1161                         vp = vp->v_mount->mnt_vnodecovered;
1162                         continue;
1163                 }
1164                 if (vp->v_type != VDIR) {
1165                         CACHE_RUNLOCK();
1166                         numfullpathfail1++;
1167                         error = ENOTDIR;
1168                         SDT_PROBE(vfs, namecache, fullpath, return,
1169                             error, vp, NULL, 0, 0);
1170                         break;
1171                 }
1172                 error = vn_vptocnp_locked(&vp, td->td_ucred, buf, &buflen);
1173                 if (error)
1174                         break;
1175                 if (buflen == 0) {
1176                         CACHE_RUNLOCK();
1177                         error = ENOMEM;
1178                         SDT_PROBE(vfs, namecache, fullpath, return, error,
1179                             startvp, NULL, 0, 0);
1180                         break;
1181                 }
1182                 buf[--buflen] = '/';
1183                 slash_prefixed = 1;
1184         }
1185         if (error)
1186                 return (error);
1187         if (!slash_prefixed) {
1188                 if (buflen == 0) {
1189                         CACHE_RUNLOCK();
1190                         numfullpathfail4++;
1191                         SDT_PROBE(vfs, namecache, fullpath, return, ENOMEM,
1192                             startvp, NULL, 0, 0);
1193                         return (ENOMEM);
1194                 }
1195                 buf[--buflen] = '/';
1196         }
1197         numfullpathfound++;
1198         CACHE_RUNLOCK();
1199
1200         SDT_PROBE(vfs, namecache, fullpath, return, 0, startvp, buf + buflen,
1201             0, 0);
1202         *retbuf = buf + buflen;
1203         return (0);
1204 }
1205
1206 int
1207 vn_commname(struct vnode *vp, char *buf, u_int buflen)
1208 {
1209         struct namecache *ncp;
1210         int l;
1211
1212         CACHE_RLOCK();
1213         TAILQ_FOREACH(ncp, &vp->v_cache_dst, nc_dst)
1214                 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1215                         break;
1216         if (ncp == NULL) {
1217                 CACHE_RUNLOCK();
1218                 return (ENOENT);
1219         }
1220         l = min(ncp->nc_nlen, buflen - 1);
1221         memcpy(buf, ncp->nc_name, l);
1222         CACHE_RUNLOCK();
1223         buf[l] = '\0';
1224         return (0);
1225 }