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