vfs_cache.c revision 276955
12347Scsgr/*- 22347Scsgr * Copyright (c) 1989, 1993, 1995 32347Scsgr * The Regents of the University of California. All rights reserved. 42347Scsgr * 52347Scsgr * This code is derived from software contributed to Berkeley by 62347Scsgr * Poul-Henning Kamp of the FreeBSD Project. 72347Scsgr * 82347Scsgr * Redistribution and use in source and binary forms, with or without 92347Scsgr * modification, are permitted provided that the following conditions 102347Scsgr * are met: 112347Scsgr * 1. Redistributions of source code must retain the above copyright 122347Scsgr * notice, this list of conditions and the following disclaimer. 132347Scsgr * 2. Redistributions in binary form must reproduce the above copyright 142347Scsgr * notice, this list of conditions and the following disclaimer in the 152347Scsgr * documentation and/or other materials provided with the distribution. 162347Scsgr * 4. Neither the name of the University nor the names of its contributors 172347Scsgr * may be used to endorse or promote products derived from this software 182347Scsgr * without specific prior written permission. 192347Scsgr * 202347Scsgr * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 212347Scsgr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 222347Scsgr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 232347Scsgr * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 242347Scsgr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 252347Scsgr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 262347Scsgr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 272347Scsgr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 282347Scsgr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 292347Scsgr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 302347Scsgr * SUCH DAMAGE. 312347Scsgr * 322347Scsgr * @(#)vfs_cache.c 8.5 (Berkeley) 3/22/95 332347Scsgr */ 342347Scsgr 352347Scsgr#include <sys/cdefs.h> 3627977Scharnier__FBSDID("$FreeBSD: stable/10/sys/kern/vfs_cache.c 276955 2015-01-11 07:02:03Z dchagin $"); 372347Scsgr 382347Scsgr#include "opt_kdtrace.h" 392347Scsgr#include "opt_ktrace.h" 402347Scsgr 412347Scsgr#include <sys/param.h> 4291643Smarkm#include <sys/systm.h> 4327977Scharnier#include <sys/filedesc.h> 442347Scsgr#include <sys/fnv_hash.h> 4591808Smarkm#include <sys/kernel.h> 4691808Smarkm#include <sys/lock.h> 4791808Smarkm#include <sys/malloc.h> 482347Scsgr#include <sys/fcntl.h> 492347Scsgr#include <sys/mount.h> 502347Scsgr#include <sys/namei.h> 512347Scsgr#include <sys/proc.h> 522347Scsgr#include <sys/rwlock.h> 532347Scsgr#include <sys/sdt.h> 542347Scsgr#include <sys/syscallsubr.h> 552347Scsgr#include <sys/sysctl.h> 5691643Smarkm#include <sys/sysproto.h> 5791643Smarkm#include <sys/vnode.h> 5827977Scharnier#ifdef KTRACE 5927977Scharnier#include <sys/ktrace.h> 602347Scsgr#endif 612347Scsgr 622347Scsgr#include <vm/uma.h> 6327977Scharnier 6469231SkrisSDT_PROVIDER_DECLARE(vfs); 6527977ScharnierSDT_PROBE_DEFINE3(vfs, namecache, enter, done, "struct vnode *", "char *", 662347Scsgr "struct vnode *"); 672347ScsgrSDT_PROBE_DEFINE2(vfs, namecache, enter_negative, done, "struct vnode *", 682347Scsgr "char *"); 6991643SmarkmSDT_PROBE_DEFINE1(vfs, namecache, fullpath, entry, "struct vnode *"); 7027977ScharnierSDT_PROBE_DEFINE3(vfs, namecache, fullpath, hit, "struct vnode *", 7191643Smarkm "char *", "struct vnode *"); 7291643SmarkmSDT_PROBE_DEFINE1(vfs, namecache, fullpath, miss, "struct vnode *"); 7391643SmarkmSDT_PROBE_DEFINE3(vfs, namecache, fullpath, return, "int", 742347Scsgr "struct vnode *", "char *"); 7527977ScharnierSDT_PROBE_DEFINE3(vfs, namecache, lookup, hit, "struct vnode *", "char *", 7691643Smarkm "struct vnode *"); 772347ScsgrSDT_PROBE_DEFINE2(vfs, namecache, lookup, hit__negative, 782347Scsgr "struct vnode *", "char *"); 792347ScsgrSDT_PROBE_DEFINE2(vfs, namecache, lookup, miss, "struct vnode *", 8021095Speter "char *"); 812347ScsgrSDT_PROBE_DEFINE1(vfs, namecache, purge, done, "struct vnode *"); 8227977ScharnierSDT_PROBE_DEFINE1(vfs, namecache, purge_negative, done, "struct vnode *"); 8327977ScharnierSDT_PROBE_DEFINE1(vfs, namecache, purgevfs, done, "struct mount *"); 842347ScsgrSDT_PROBE_DEFINE3(vfs, namecache, zap, done, "struct vnode *", "char *", 852347Scsgr "struct vnode *"); 862347ScsgrSDT_PROBE_DEFINE2(vfs, namecache, zap_negative, done, "struct vnode *", 872347Scsgr "char *"); 882347Scsgr 892347Scsgr/* 902347Scsgr * This structure describes the elements in the cache of recent 912347Scsgr * names looked up by namei. 922347Scsgr */ 932347Scsgr 942347Scsgrstruct namecache { 952347Scsgr LIST_ENTRY(namecache) nc_hash; /* hash chain */ 962347Scsgr LIST_ENTRY(namecache) nc_src; /* source vnode list */ 972347Scsgr TAILQ_ENTRY(namecache) nc_dst; /* destination vnode list */ 982347Scsgr struct vnode *nc_dvp; /* vnode of parent of name */ 992347Scsgr struct vnode *nc_vp; /* vnode the name refers to */ 10027977Scharnier u_char nc_flag; /* flag bits */ 1012347Scsgr u_char nc_nlen; /* length of name */ 1022347Scsgr char nc_name[0]; /* segment name + nul */ 10321095Speter}; 10421095Speter 105121545Speter/* 106121545Speter * struct namecache_ts repeats struct namecache layout up to the 1072347Scsgr * nc_nlen member. 1088874Srgrimes * struct namecache_ts is used in place of struct namecache when time(s) need 1092347Scsgr * to be stored. The nc_dotdottime field is used when a cache entry is mapping 1102347Scsgr * both a non-dotdot directory name plus dotdot for the directory's 11127977Scharnier * parent. 1122347Scsgr */ 1132347Scsgrstruct namecache_ts { 11491643Smarkm LIST_ENTRY(namecache) nc_hash; /* hash chain */ 1152347Scsgr LIST_ENTRY(namecache) nc_src; /* source vnode list */ 1162347Scsgr TAILQ_ENTRY(namecache) nc_dst; /* destination vnode list */ 11727977Scharnier struct vnode *nc_dvp; /* vnode of parent of name */ 11891643Smarkm struct vnode *nc_vp; /* vnode the name refers to */ 11927977Scharnier u_char nc_flag; /* flag bits */ 12091643Smarkm u_char nc_nlen; /* length of name */ 12127977Scharnier struct timespec nc_time; /* timespec provided by fs */ 12227977Scharnier struct timespec nc_dotdottime; /* dotdot timespec provided by fs */ 12327977Scharnier int nc_ticks; /* ticks value when entry was added */ 12427977Scharnier char nc_name[0]; /* segment name + nul */ 12591643Smarkm}; 1262347Scsgr 1272347Scsgr/* 1282347Scsgr * Flags in namecache.nc_flag 1292347Scsgr */ 13069231Skris#define NCF_WHITE 0x01 1312347Scsgr#define NCF_ISDOTDOT 0x02 1322347Scsgr#define NCF_TS 0x04 13391643Smarkm#define NCF_DTS 0x08 13469231Skris 13569231Skris/* 1362347Scsgr * Name caching works as follows: 13791643Smarkm * 13891808Smarkm * Names found by directory scans are retained in a cache 13969231Skris * for future reference. It is managed LRU, so frequently 14091643Smarkm * used names will hang around. Cache is indexed by hash value 1412347Scsgr * obtained from (vp, name) where vp refers to the directory 14291643Smarkm * containing name. 14391643Smarkm * 14491643Smarkm * If it is a "negative" entry, (i.e. for a name that is known NOT to 14591643Smarkm * exist) the vnode pointer will be NULL. 14691643Smarkm * 14791643Smarkm * Upon reaching the last segment of a path, if the reference 14891643Smarkm * is for DELETE, or NOCACHE is set (rewrite), and the 1492347Scsgr * name is located in the cache, it will be dropped. 1502347Scsgr */ 1512347Scsgr 1522347Scsgr/* 1532347Scsgr * Structures associated with name cacheing. 1542347Scsgr */ 1552347Scsgr#define NCHHASH(hash) \ 1562347Scsgr (&nchashtbl[(hash) & nchash]) 1572347Scsgrstatic LIST_HEAD(nchashhead, namecache) *nchashtbl; /* Hash Table */ 15891643Smarkmstatic TAILQ_HEAD(, namecache) ncneg; /* Hash Table */ 1592347Scsgrstatic u_long nchash; /* size of hash table */ 16069231SkrisSYSCTL_ULONG(_debug, OID_AUTO, nchash, CTLFLAG_RD, &nchash, 0, 16169231Skris "Size of namecache hash table"); 16291643Smarkmstatic u_long ncnegfactor = 16; /* ratio of negative entries */ 16391643SmarkmSYSCTL_ULONG(_vfs, OID_AUTO, ncnegfactor, CTLFLAG_RW, &ncnegfactor, 0, 1642347Scsgr "Ratio of negative namecache entries"); 1652347Scsgrstatic u_long numneg; /* number of negative entries allocated */ 1662347ScsgrSYSCTL_ULONG(_debug, OID_AUTO, numneg, CTLFLAG_RD, &numneg, 0, 1672347Scsgr "Number of negative entries in namecache"); 16827977Scharnierstatic u_long numcache; /* number of cache entries allocated */ 16969231SkrisSYSCTL_ULONG(_debug, OID_AUTO, numcache, CTLFLAG_RD, &numcache, 0, 1702347Scsgr "Number of namecache entries"); 1712347Scsgrstatic u_long numcachehv; /* number of cache entries with vnodes held */ 1722347ScsgrSYSCTL_ULONG(_debug, OID_AUTO, numcachehv, CTLFLAG_RD, &numcachehv, 0, 1732347Scsgr "Number of namecache entries with vnodes held"); 17427977Scharnierstatic u_int ncsizefactor = 2; 17569231SkrisSYSCTL_UINT(_vfs, OID_AUTO, ncsizefactor, CTLFLAG_RW, &ncsizefactor, 0, 17691643Smarkm "Size factor for namecache"); 17791643Smarkm 17891643Smarkmstruct nchstats nchstats; /* cache effectiveness statistics */ 17969231Skris 180102944Sdwmalonestatic struct rwlock cache_lock; 18169231SkrisRW_SYSINIT(vfscache, &cache_lock, "Name Cache"); 18291643Smarkm 1832347Scsgr#define CACHE_UPGRADE_LOCK() rw_try_upgrade(&cache_lock) 184#define CACHE_RLOCK() rw_rlock(&cache_lock) 185#define CACHE_RUNLOCK() rw_runlock(&cache_lock) 186#define CACHE_WLOCK() rw_wlock(&cache_lock) 187#define CACHE_WUNLOCK() rw_wunlock(&cache_lock) 188 189/* 190 * UMA zones for the VFS cache. 191 * 192 * The small cache is used for entries with short names, which are the 193 * most common. The large cache is used for entries which are too big to 194 * fit in the small cache. 195 */ 196static uma_zone_t cache_zone_small; 197static uma_zone_t cache_zone_small_ts; 198static uma_zone_t cache_zone_large; 199static uma_zone_t cache_zone_large_ts; 200 201#define CACHE_PATH_CUTOFF 35 202 203static struct namecache * 204cache_alloc(int len, int ts) 205{ 206 207 if (len > CACHE_PATH_CUTOFF) { 208 if (ts) 209 return (uma_zalloc(cache_zone_large_ts, M_WAITOK)); 210 else 211 return (uma_zalloc(cache_zone_large, M_WAITOK)); 212 } 213 if (ts) 214 return (uma_zalloc(cache_zone_small_ts, M_WAITOK)); 215 else 216 return (uma_zalloc(cache_zone_small, M_WAITOK)); 217} 218 219static void 220cache_free(struct namecache *ncp) 221{ 222 int ts; 223 224 if (ncp == NULL) 225 return; 226 ts = ncp->nc_flag & NCF_TS; 227 if (ncp->nc_nlen <= CACHE_PATH_CUTOFF) { 228 if (ts) 229 uma_zfree(cache_zone_small_ts, ncp); 230 else 231 uma_zfree(cache_zone_small, ncp); 232 } else if (ts) 233 uma_zfree(cache_zone_large_ts, ncp); 234 else 235 uma_zfree(cache_zone_large, ncp); 236} 237 238static char * 239nc_get_name(struct namecache *ncp) 240{ 241 struct namecache_ts *ncp_ts; 242 243 if ((ncp->nc_flag & NCF_TS) == 0) 244 return (ncp->nc_name); 245 ncp_ts = (struct namecache_ts *)ncp; 246 return (ncp_ts->nc_name); 247} 248 249static void 250cache_out_ts(struct namecache *ncp, struct timespec *tsp, int *ticksp) 251{ 252 253 KASSERT((ncp->nc_flag & NCF_TS) != 0 || 254 (tsp == NULL && ticksp == NULL), 255 ("No NCF_TS")); 256 257 if (tsp != NULL) 258 *tsp = ((struct namecache_ts *)ncp)->nc_time; 259 if (ticksp != NULL) 260 *ticksp = ((struct namecache_ts *)ncp)->nc_ticks; 261} 262 263static int doingcache = 1; /* 1 => enable the cache */ 264SYSCTL_INT(_debug, OID_AUTO, vfscache, CTLFLAG_RW, &doingcache, 0, 265 "VFS namecache enabled"); 266 267/* Export size information to userland */ 268SYSCTL_INT(_debug_sizeof, OID_AUTO, namecache, CTLFLAG_RD, SYSCTL_NULL_INT_PTR, 269 sizeof(struct namecache), "sizeof(struct namecache)"); 270 271/* 272 * The new name cache statistics 273 */ 274static SYSCTL_NODE(_vfs, OID_AUTO, cache, CTLFLAG_RW, 0, 275 "Name cache statistics"); 276#define STATNODE(mode, name, var, descr) \ 277 SYSCTL_ULONG(_vfs_cache, OID_AUTO, name, mode, var, 0, descr); 278STATNODE(CTLFLAG_RD, numneg, &numneg, "Number of negative cache entries"); 279STATNODE(CTLFLAG_RD, numcache, &numcache, "Number of cache entries"); 280static u_long numcalls; STATNODE(CTLFLAG_RD, numcalls, &numcalls, 281 "Number of cache lookups"); 282static u_long dothits; STATNODE(CTLFLAG_RD, dothits, &dothits, 283 "Number of '.' hits"); 284static u_long dotdothits; STATNODE(CTLFLAG_RD, dotdothits, &dotdothits, 285 "Number of '..' hits"); 286static u_long numchecks; STATNODE(CTLFLAG_RD, numchecks, &numchecks, 287 "Number of checks in lookup"); 288static u_long nummiss; STATNODE(CTLFLAG_RD, nummiss, &nummiss, 289 "Number of cache misses"); 290static u_long nummisszap; STATNODE(CTLFLAG_RD, nummisszap, &nummisszap, 291 "Number of cache misses we do not want to cache"); 292static u_long numposzaps; STATNODE(CTLFLAG_RD, numposzaps, &numposzaps, 293 "Number of cache hits (positive) we do not want to cache"); 294static u_long numposhits; STATNODE(CTLFLAG_RD, numposhits, &numposhits, 295 "Number of cache hits (positive)"); 296static u_long numnegzaps; STATNODE(CTLFLAG_RD, numnegzaps, &numnegzaps, 297 "Number of cache hits (negative) we do not want to cache"); 298static u_long numneghits; STATNODE(CTLFLAG_RD, numneghits, &numneghits, 299 "Number of cache hits (negative)"); 300static u_long numupgrades; STATNODE(CTLFLAG_RD, numupgrades, &numupgrades, 301 "Number of updates of the cache after lookup (write lock + retry)"); 302 303SYSCTL_OPAQUE(_vfs_cache, OID_AUTO, nchstats, CTLFLAG_RD | CTLFLAG_MPSAFE, 304 &nchstats, sizeof(nchstats), "LU", 305 "VFS cache effectiveness statistics"); 306 307 308 309static void cache_zap(struct namecache *ncp); 310static int vn_vptocnp_locked(struct vnode **vp, struct ucred *cred, char *buf, 311 u_int *buflen); 312static int vn_fullpath1(struct thread *td, struct vnode *vp, struct vnode *rdir, 313 char *buf, char **retbuf, u_int buflen); 314 315static MALLOC_DEFINE(M_VFSCACHE, "vfscache", "VFS name cache entries"); 316 317#ifdef DIAGNOSTIC 318/* 319 * Grab an atomic snapshot of the name cache hash chain lengths 320 */ 321static SYSCTL_NODE(_debug, OID_AUTO, hashstat, CTLFLAG_RW, NULL, 322 "hash table stats"); 323 324static int 325sysctl_debug_hashstat_rawnchash(SYSCTL_HANDLER_ARGS) 326{ 327 int error; 328 struct nchashhead *ncpp; 329 struct namecache *ncp; 330 int n_nchash; 331 int count; 332 333 n_nchash = nchash + 1; /* nchash is max index, not count */ 334 if (!req->oldptr) 335 return SYSCTL_OUT(req, 0, n_nchash * sizeof(int)); 336 337 /* Scan hash tables for applicable entries */ 338 for (ncpp = nchashtbl; n_nchash > 0; n_nchash--, ncpp++) { 339 CACHE_RLOCK(); 340 count = 0; 341 LIST_FOREACH(ncp, ncpp, nc_hash) { 342 count++; 343 } 344 CACHE_RUNLOCK(); 345 error = SYSCTL_OUT(req, &count, sizeof(count)); 346 if (error) 347 return (error); 348 } 349 return (0); 350} 351SYSCTL_PROC(_debug_hashstat, OID_AUTO, rawnchash, CTLTYPE_INT|CTLFLAG_RD| 352 CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_rawnchash, "S,int", 353 "nchash chain lengths"); 354 355static int 356sysctl_debug_hashstat_nchash(SYSCTL_HANDLER_ARGS) 357{ 358 int error; 359 struct nchashhead *ncpp; 360 struct namecache *ncp; 361 int n_nchash; 362 int count, maxlength, used, pct; 363 364 if (!req->oldptr) 365 return SYSCTL_OUT(req, 0, 4 * sizeof(int)); 366 367 n_nchash = nchash + 1; /* nchash is max index, not count */ 368 used = 0; 369 maxlength = 0; 370 371 /* Scan hash tables for applicable entries */ 372 for (ncpp = nchashtbl; n_nchash > 0; n_nchash--, ncpp++) { 373 count = 0; 374 CACHE_RLOCK(); 375 LIST_FOREACH(ncp, ncpp, nc_hash) { 376 count++; 377 } 378 CACHE_RUNLOCK(); 379 if (count) 380 used++; 381 if (maxlength < count) 382 maxlength = count; 383 } 384 n_nchash = nchash + 1; 385 pct = (used * 100) / (n_nchash / 100); 386 error = SYSCTL_OUT(req, &n_nchash, sizeof(n_nchash)); 387 if (error) 388 return (error); 389 error = SYSCTL_OUT(req, &used, sizeof(used)); 390 if (error) 391 return (error); 392 error = SYSCTL_OUT(req, &maxlength, sizeof(maxlength)); 393 if (error) 394 return (error); 395 error = SYSCTL_OUT(req, &pct, sizeof(pct)); 396 if (error) 397 return (error); 398 return (0); 399} 400SYSCTL_PROC(_debug_hashstat, OID_AUTO, nchash, CTLTYPE_INT|CTLFLAG_RD| 401 CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_nchash, "I", 402 "nchash statistics (number of total/used buckets, maximum chain length, usage percentage)"); 403#endif 404 405/* 406 * cache_zap(): 407 * 408 * Removes a namecache entry from cache, whether it contains an actual 409 * pointer to a vnode or if it is just a negative cache entry. 410 */ 411static void 412cache_zap(ncp) 413 struct namecache *ncp; 414{ 415 struct vnode *vp; 416 417 rw_assert(&cache_lock, RA_WLOCKED); 418 CTR2(KTR_VFS, "cache_zap(%p) vp %p", ncp, ncp->nc_vp); 419#ifdef KDTRACE_HOOKS 420 if (ncp->nc_vp != NULL) { 421 SDT_PROBE(vfs, namecache, zap, done, ncp->nc_dvp, 422 nc_get_name(ncp), ncp->nc_vp, 0, 0); 423 } else { 424 SDT_PROBE(vfs, namecache, zap_negative, done, ncp->nc_dvp, 425 nc_get_name(ncp), 0, 0, 0); 426 } 427#endif 428 vp = NULL; 429 LIST_REMOVE(ncp, nc_hash); 430 if (ncp->nc_flag & NCF_ISDOTDOT) { 431 if (ncp == ncp->nc_dvp->v_cache_dd) 432 ncp->nc_dvp->v_cache_dd = NULL; 433 } else { 434 LIST_REMOVE(ncp, nc_src); 435 if (LIST_EMPTY(&ncp->nc_dvp->v_cache_src)) { 436 vp = ncp->nc_dvp; 437 numcachehv--; 438 } 439 } 440 if (ncp->nc_vp) { 441 TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst, ncp, nc_dst); 442 if (ncp == ncp->nc_vp->v_cache_dd) 443 ncp->nc_vp->v_cache_dd = NULL; 444 } else { 445 TAILQ_REMOVE(&ncneg, ncp, nc_dst); 446 numneg--; 447 } 448 numcache--; 449 cache_free(ncp); 450 if (vp) 451 vdrop(vp); 452} 453 454/* 455 * Lookup an entry in the cache 456 * 457 * Lookup is called with dvp pointing to the directory to search, 458 * cnp pointing to the name of the entry being sought. If the lookup 459 * succeeds, the vnode is returned in *vpp, and a status of -1 is 460 * returned. If the lookup determines that the name does not exist 461 * (negative cacheing), a status of ENOENT is returned. If the lookup 462 * fails, a status of zero is returned. If the directory vnode is 463 * recycled out from under us due to a forced unmount, a status of 464 * ENOENT is returned. 465 * 466 * vpp is locked and ref'd on return. If we're looking up DOTDOT, dvp is 467 * unlocked. If we're looking up . an extra ref is taken, but the lock is 468 * not recursively acquired. 469 */ 470 471int 472cache_lookup(dvp, vpp, cnp, tsp, ticksp) 473 struct vnode *dvp; 474 struct vnode **vpp; 475 struct componentname *cnp; 476 struct timespec *tsp; 477 int *ticksp; 478{ 479 struct namecache *ncp; 480 uint32_t hash; 481 int error, ltype, wlocked; 482 483 if (!doingcache) { 484 cnp->cn_flags &= ~MAKEENTRY; 485 return (0); 486 } 487retry: 488 CACHE_RLOCK(); 489 wlocked = 0; 490 numcalls++; 491 error = 0; 492 493retry_wlocked: 494 if (cnp->cn_nameptr[0] == '.') { 495 if (cnp->cn_namelen == 1) { 496 *vpp = dvp; 497 CTR2(KTR_VFS, "cache_lookup(%p, %s) found via .", 498 dvp, cnp->cn_nameptr); 499 dothits++; 500 SDT_PROBE(vfs, namecache, lookup, hit, dvp, ".", 501 *vpp, 0, 0); 502 if (tsp != NULL) 503 timespecclear(tsp); 504 if (ticksp != NULL) 505 *ticksp = ticks; 506 goto success; 507 } 508 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') { 509 dotdothits++; 510 if (dvp->v_cache_dd == NULL) { 511 SDT_PROBE(vfs, namecache, lookup, miss, dvp, 512 "..", NULL, 0, 0); 513 goto unlock; 514 } 515 if ((cnp->cn_flags & MAKEENTRY) == 0) { 516 if (!wlocked && !CACHE_UPGRADE_LOCK()) 517 goto wlock; 518 if (dvp->v_cache_dd->nc_flag & NCF_ISDOTDOT) 519 cache_zap(dvp->v_cache_dd); 520 dvp->v_cache_dd = NULL; 521 CACHE_WUNLOCK(); 522 return (0); 523 } 524 ncp = dvp->v_cache_dd; 525 if (ncp->nc_flag & NCF_ISDOTDOT) 526 *vpp = ncp->nc_vp; 527 else 528 *vpp = ncp->nc_dvp; 529 /* Return failure if negative entry was found. */ 530 if (*vpp == NULL) 531 goto negative_success; 532 CTR3(KTR_VFS, "cache_lookup(%p, %s) found %p via ..", 533 dvp, cnp->cn_nameptr, *vpp); 534 SDT_PROBE(vfs, namecache, lookup, hit, dvp, "..", 535 *vpp, 0, 0); 536 cache_out_ts(ncp, tsp, ticksp); 537 if ((ncp->nc_flag & (NCF_ISDOTDOT | NCF_DTS)) == 538 NCF_DTS && tsp != NULL) 539 *tsp = ((struct namecache_ts *)ncp)-> 540 nc_dotdottime; 541 goto success; 542 } 543 } 544 545 hash = fnv_32_buf(cnp->cn_nameptr, cnp->cn_namelen, FNV1_32_INIT); 546 hash = fnv_32_buf(&dvp, sizeof(dvp), hash); 547 LIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) { 548 numchecks++; 549 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen && 550 !bcmp(nc_get_name(ncp), cnp->cn_nameptr, ncp->nc_nlen)) 551 break; 552 } 553 554 /* We failed to find an entry */ 555 if (ncp == NULL) { 556 SDT_PROBE(vfs, namecache, lookup, miss, dvp, cnp->cn_nameptr, 557 NULL, 0, 0); 558 if ((cnp->cn_flags & MAKEENTRY) == 0) { 559 nummisszap++; 560 } else { 561 nummiss++; 562 } 563 nchstats.ncs_miss++; 564 goto unlock; 565 } 566 567 /* We don't want to have an entry, so dump it */ 568 if ((cnp->cn_flags & MAKEENTRY) == 0) { 569 numposzaps++; 570 nchstats.ncs_badhits++; 571 if (!wlocked && !CACHE_UPGRADE_LOCK()) 572 goto wlock; 573 cache_zap(ncp); 574 CACHE_WUNLOCK(); 575 return (0); 576 } 577 578 /* We found a "positive" match, return the vnode */ 579 if (ncp->nc_vp) { 580 numposhits++; 581 nchstats.ncs_goodhits++; 582 *vpp = ncp->nc_vp; 583 CTR4(KTR_VFS, "cache_lookup(%p, %s) found %p via ncp %p", 584 dvp, cnp->cn_nameptr, *vpp, ncp); 585 SDT_PROBE(vfs, namecache, lookup, hit, dvp, nc_get_name(ncp), 586 *vpp, 0, 0); 587 cache_out_ts(ncp, tsp, ticksp); 588 goto success; 589 } 590 591negative_success: 592 /* We found a negative match, and want to create it, so purge */ 593 if (cnp->cn_nameiop == CREATE) { 594 numnegzaps++; 595 nchstats.ncs_badhits++; 596 if (!wlocked && !CACHE_UPGRADE_LOCK()) 597 goto wlock; 598 cache_zap(ncp); 599 CACHE_WUNLOCK(); 600 return (0); 601 } 602 603 if (!wlocked && !CACHE_UPGRADE_LOCK()) 604 goto wlock; 605 numneghits++; 606 /* 607 * We found a "negative" match, so we shift it to the end of 608 * the "negative" cache entries queue to satisfy LRU. Also, 609 * check to see if the entry is a whiteout; indicate this to 610 * the componentname, if so. 611 */ 612 TAILQ_REMOVE(&ncneg, ncp, nc_dst); 613 TAILQ_INSERT_TAIL(&ncneg, ncp, nc_dst); 614 nchstats.ncs_neghits++; 615 if (ncp->nc_flag & NCF_WHITE) 616 cnp->cn_flags |= ISWHITEOUT; 617 SDT_PROBE(vfs, namecache, lookup, hit__negative, dvp, nc_get_name(ncp), 618 0, 0, 0); 619 cache_out_ts(ncp, tsp, ticksp); 620 CACHE_WUNLOCK(); 621 return (ENOENT); 622 623wlock: 624 /* 625 * We need to update the cache after our lookup, so upgrade to 626 * a write lock and retry the operation. 627 */ 628 CACHE_RUNLOCK(); 629 CACHE_WLOCK(); 630 numupgrades++; 631 wlocked = 1; 632 goto retry_wlocked; 633 634success: 635 /* 636 * On success we return a locked and ref'd vnode as per the lookup 637 * protocol. 638 */ 639 if (dvp == *vpp) { /* lookup on "." */ 640 VREF(*vpp); 641 if (wlocked) 642 CACHE_WUNLOCK(); 643 else 644 CACHE_RUNLOCK(); 645 /* 646 * When we lookup "." we still can be asked to lock it 647 * differently... 648 */ 649 ltype = cnp->cn_lkflags & LK_TYPE_MASK; 650 if (ltype != VOP_ISLOCKED(*vpp)) { 651 if (ltype == LK_EXCLUSIVE) { 652 vn_lock(*vpp, LK_UPGRADE | LK_RETRY); 653 if ((*vpp)->v_iflag & VI_DOOMED) { 654 /* forced unmount */ 655 vrele(*vpp); 656 *vpp = NULL; 657 return (ENOENT); 658 } 659 } else 660 vn_lock(*vpp, LK_DOWNGRADE | LK_RETRY); 661 } 662 return (-1); 663 } 664 ltype = 0; /* silence gcc warning */ 665 if (cnp->cn_flags & ISDOTDOT) { 666 ltype = VOP_ISLOCKED(dvp); 667 VOP_UNLOCK(dvp, 0); 668 } 669 VI_LOCK(*vpp); 670 if (wlocked) 671 CACHE_WUNLOCK(); 672 else 673 CACHE_RUNLOCK(); 674 error = vget(*vpp, cnp->cn_lkflags | LK_INTERLOCK, cnp->cn_thread); 675 if (cnp->cn_flags & ISDOTDOT) { 676 vn_lock(dvp, ltype | LK_RETRY); 677 if (dvp->v_iflag & VI_DOOMED) { 678 if (error == 0) 679 vput(*vpp); 680 *vpp = NULL; 681 return (ENOENT); 682 } 683 } 684 if (error) { 685 *vpp = NULL; 686 goto retry; 687 } 688 if ((cnp->cn_flags & ISLASTCN) && 689 (cnp->cn_lkflags & LK_TYPE_MASK) == LK_EXCLUSIVE) { 690 ASSERT_VOP_ELOCKED(*vpp, "cache_lookup"); 691 } 692 return (-1); 693 694unlock: 695 if (wlocked) 696 CACHE_WUNLOCK(); 697 else 698 CACHE_RUNLOCK(); 699 return (0); 700} 701 702/* 703 * Add an entry to the cache. 704 */ 705void 706cache_enter_time(dvp, vp, cnp, tsp, dtsp) 707 struct vnode *dvp; 708 struct vnode *vp; 709 struct componentname *cnp; 710 struct timespec *tsp; 711 struct timespec *dtsp; 712{ 713 struct namecache *ncp, *n2; 714 struct namecache_ts *n3; 715 struct nchashhead *ncpp; 716 uint32_t hash; 717 int flag; 718 int hold; 719 int zap; 720 int len; 721 722 CTR3(KTR_VFS, "cache_enter(%p, %p, %s)", dvp, vp, cnp->cn_nameptr); 723 VNASSERT(vp == NULL || (vp->v_iflag & VI_DOOMED) == 0, vp, 724 ("cache_enter: Adding a doomed vnode")); 725 VNASSERT(dvp == NULL || (dvp->v_iflag & VI_DOOMED) == 0, dvp, 726 ("cache_enter: Doomed vnode used as src")); 727 728 if (!doingcache) 729 return; 730 731 /* 732 * Avoid blowout in namecache entries. 733 */ 734 if (numcache >= desiredvnodes * ncsizefactor) 735 return; 736 737 flag = 0; 738 if (cnp->cn_nameptr[0] == '.') { 739 if (cnp->cn_namelen == 1) 740 return; 741 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') { 742 CACHE_WLOCK(); 743 /* 744 * If dotdot entry already exists, just retarget it 745 * to new parent vnode, otherwise continue with new 746 * namecache entry allocation. 747 */ 748 if ((ncp = dvp->v_cache_dd) != NULL && 749 ncp->nc_flag & NCF_ISDOTDOT) { 750 KASSERT(ncp->nc_dvp == dvp, 751 ("wrong isdotdot parent")); 752 if (ncp->nc_vp != NULL) { 753 TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst, 754 ncp, nc_dst); 755 } else { 756 TAILQ_REMOVE(&ncneg, ncp, nc_dst); 757 numneg--; 758 } 759 if (vp != NULL) { 760 TAILQ_INSERT_HEAD(&vp->v_cache_dst, 761 ncp, nc_dst); 762 } else { 763 TAILQ_INSERT_TAIL(&ncneg, ncp, nc_dst); 764 numneg++; 765 } 766 ncp->nc_vp = vp; 767 CACHE_WUNLOCK(); 768 return; 769 } 770 dvp->v_cache_dd = NULL; 771 SDT_PROBE(vfs, namecache, enter, done, dvp, "..", vp, 772 0, 0); 773 CACHE_WUNLOCK(); 774 flag = NCF_ISDOTDOT; 775 } 776 } 777 778 hold = 0; 779 zap = 0; 780 781 /* 782 * Calculate the hash key and setup as much of the new 783 * namecache entry as possible before acquiring the lock. 784 */ 785 ncp = cache_alloc(cnp->cn_namelen, tsp != NULL); 786 ncp->nc_vp = vp; 787 ncp->nc_dvp = dvp; 788 ncp->nc_flag = flag; 789 if (tsp != NULL) { 790 n3 = (struct namecache_ts *)ncp; 791 n3->nc_time = *tsp; 792 n3->nc_ticks = ticks; 793 n3->nc_flag |= NCF_TS; 794 if (dtsp != NULL) { 795 n3->nc_dotdottime = *dtsp; 796 n3->nc_flag |= NCF_DTS; 797 } 798 } 799 len = ncp->nc_nlen = cnp->cn_namelen; 800 hash = fnv_32_buf(cnp->cn_nameptr, len, FNV1_32_INIT); 801 strlcpy(nc_get_name(ncp), cnp->cn_nameptr, len + 1); 802 hash = fnv_32_buf(&dvp, sizeof(dvp), hash); 803 CACHE_WLOCK(); 804 805 /* 806 * See if this vnode or negative entry is already in the cache 807 * with this name. This can happen with concurrent lookups of 808 * the same path name. 809 */ 810 ncpp = NCHHASH(hash); 811 LIST_FOREACH(n2, ncpp, nc_hash) { 812 if (n2->nc_dvp == dvp && 813 n2->nc_nlen == cnp->cn_namelen && 814 !bcmp(nc_get_name(n2), cnp->cn_nameptr, n2->nc_nlen)) { 815 if (tsp != NULL) { 816 KASSERT((n2->nc_flag & NCF_TS) != 0, 817 ("no NCF_TS")); 818 n3 = (struct namecache_ts *)n2; 819 n3->nc_time = 820 ((struct namecache_ts *)ncp)->nc_time; 821 n3->nc_ticks = 822 ((struct namecache_ts *)ncp)->nc_ticks; 823 if (dtsp != NULL) { 824 n3->nc_dotdottime = 825 ((struct namecache_ts *)ncp)-> 826 nc_dotdottime; 827 n3->nc_flag |= NCF_DTS; 828 } 829 } 830 CACHE_WUNLOCK(); 831 cache_free(ncp); 832 return; 833 } 834 } 835 836 if (flag == NCF_ISDOTDOT) { 837 /* 838 * See if we are trying to add .. entry, but some other lookup 839 * has populated v_cache_dd pointer already. 840 */ 841 if (dvp->v_cache_dd != NULL) { 842 CACHE_WUNLOCK(); 843 cache_free(ncp); 844 return; 845 } 846 KASSERT(vp == NULL || vp->v_type == VDIR, 847 ("wrong vnode type %p", vp)); 848 dvp->v_cache_dd = ncp; 849 } 850 851 numcache++; 852 if (!vp) { 853 numneg++; 854 if (cnp->cn_flags & ISWHITEOUT) 855 ncp->nc_flag |= NCF_WHITE; 856 } else if (vp->v_type == VDIR) { 857 if (flag != NCF_ISDOTDOT) { 858 /* 859 * For this case, the cache entry maps both the 860 * directory name in it and the name ".." for the 861 * directory's parent. 862 */ 863 if ((n2 = vp->v_cache_dd) != NULL && 864 (n2->nc_flag & NCF_ISDOTDOT) != 0) 865 cache_zap(n2); 866 vp->v_cache_dd = ncp; 867 } 868 } else { 869 vp->v_cache_dd = NULL; 870 } 871 872 /* 873 * Insert the new namecache entry into the appropriate chain 874 * within the cache entries table. 875 */ 876 LIST_INSERT_HEAD(ncpp, ncp, nc_hash); 877 if (flag != NCF_ISDOTDOT) { 878 if (LIST_EMPTY(&dvp->v_cache_src)) { 879 hold = 1; 880 numcachehv++; 881 } 882 LIST_INSERT_HEAD(&dvp->v_cache_src, ncp, nc_src); 883 } 884 885 /* 886 * If the entry is "negative", we place it into the 887 * "negative" cache queue, otherwise, we place it into the 888 * destination vnode's cache entries queue. 889 */ 890 if (vp) { 891 TAILQ_INSERT_HEAD(&vp->v_cache_dst, ncp, nc_dst); 892 SDT_PROBE(vfs, namecache, enter, done, dvp, nc_get_name(ncp), 893 vp, 0, 0); 894 } else { 895 TAILQ_INSERT_TAIL(&ncneg, ncp, nc_dst); 896 SDT_PROBE(vfs, namecache, enter_negative, done, dvp, 897 nc_get_name(ncp), 0, 0, 0); 898 } 899 if (numneg * ncnegfactor > numcache) { 900 ncp = TAILQ_FIRST(&ncneg); 901 KASSERT(ncp->nc_vp == NULL, ("ncp %p vp %p on ncneg", 902 ncp, ncp->nc_vp)); 903 zap = 1; 904 } 905 if (hold) 906 vhold(dvp); 907 if (zap) 908 cache_zap(ncp); 909 CACHE_WUNLOCK(); 910} 911 912/* 913 * Name cache initialization, from vfs_init() when we are booting 914 */ 915static void 916nchinit(void *dummy __unused) 917{ 918 919 TAILQ_INIT(&ncneg); 920 921 cache_zone_small = uma_zcreate("S VFS Cache", 922 sizeof(struct namecache) + CACHE_PATH_CUTOFF + 1, 923 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT); 924 cache_zone_small_ts = uma_zcreate("STS VFS Cache", 925 sizeof(struct namecache_ts) + CACHE_PATH_CUTOFF + 1, 926 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT); 927 cache_zone_large = uma_zcreate("L VFS Cache", 928 sizeof(struct namecache) + NAME_MAX + 1, 929 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT); 930 cache_zone_large_ts = uma_zcreate("LTS VFS Cache", 931 sizeof(struct namecache_ts) + NAME_MAX + 1, 932 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT); 933 934 nchashtbl = hashinit(desiredvnodes * 2, M_VFSCACHE, &nchash); 935} 936SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nchinit, NULL); 937 938 939/* 940 * Invalidate all entries to a particular vnode. 941 */ 942void 943cache_purge(vp) 944 struct vnode *vp; 945{ 946 947 CTR1(KTR_VFS, "cache_purge(%p)", vp); 948 SDT_PROBE(vfs, namecache, purge, done, vp, 0, 0, 0, 0); 949 CACHE_WLOCK(); 950 while (!LIST_EMPTY(&vp->v_cache_src)) 951 cache_zap(LIST_FIRST(&vp->v_cache_src)); 952 while (!TAILQ_EMPTY(&vp->v_cache_dst)) 953 cache_zap(TAILQ_FIRST(&vp->v_cache_dst)); 954 if (vp->v_cache_dd != NULL) { 955 KASSERT(vp->v_cache_dd->nc_flag & NCF_ISDOTDOT, 956 ("lost dotdot link")); 957 cache_zap(vp->v_cache_dd); 958 } 959 KASSERT(vp->v_cache_dd == NULL, ("incomplete purge")); 960 CACHE_WUNLOCK(); 961} 962 963/* 964 * Invalidate all negative entries for a particular directory vnode. 965 */ 966void 967cache_purge_negative(vp) 968 struct vnode *vp; 969{ 970 struct namecache *cp, *ncp; 971 972 CTR1(KTR_VFS, "cache_purge_negative(%p)", vp); 973 SDT_PROBE(vfs, namecache, purge_negative, done, vp, 0, 0, 0, 0); 974 CACHE_WLOCK(); 975 LIST_FOREACH_SAFE(cp, &vp->v_cache_src, nc_src, ncp) { 976 if (cp->nc_vp == NULL) 977 cache_zap(cp); 978 } 979 CACHE_WUNLOCK(); 980} 981 982/* 983 * Flush all entries referencing a particular filesystem. 984 */ 985void 986cache_purgevfs(mp) 987 struct mount *mp; 988{ 989 struct nchashhead *ncpp; 990 struct namecache *ncp, *nnp; 991 992 /* Scan hash tables for applicable entries */ 993 SDT_PROBE(vfs, namecache, purgevfs, done, mp, 0, 0, 0, 0); 994 CACHE_WLOCK(); 995 for (ncpp = &nchashtbl[nchash]; ncpp >= nchashtbl; ncpp--) { 996 LIST_FOREACH_SAFE(ncp, ncpp, nc_hash, nnp) { 997 if (ncp->nc_dvp->v_mount == mp) 998 cache_zap(ncp); 999 } 1000 } 1001 CACHE_WUNLOCK(); 1002} 1003 1004/* 1005 * Perform canonical checks and cache lookup and pass on to filesystem 1006 * through the vop_cachedlookup only if needed. 1007 */ 1008 1009int 1010vfs_cache_lookup(ap) 1011 struct vop_lookup_args /* { 1012 struct vnode *a_dvp; 1013 struct vnode **a_vpp; 1014 struct componentname *a_cnp; 1015 } */ *ap; 1016{ 1017 struct vnode *dvp; 1018 int error; 1019 struct vnode **vpp = ap->a_vpp; 1020 struct componentname *cnp = ap->a_cnp; 1021 struct ucred *cred = cnp->cn_cred; 1022 int flags = cnp->cn_flags; 1023 struct thread *td = cnp->cn_thread; 1024 1025 *vpp = NULL; 1026 dvp = ap->a_dvp; 1027 1028 if (dvp->v_type != VDIR) 1029 return (ENOTDIR); 1030 1031 if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) && 1032 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) 1033 return (EROFS); 1034 1035 error = VOP_ACCESS(dvp, VEXEC, cred, td); 1036 if (error) 1037 return (error); 1038 1039 error = cache_lookup(dvp, vpp, cnp, NULL, NULL); 1040 if (error == 0) 1041 return (VOP_CACHEDLOOKUP(dvp, vpp, cnp)); 1042 if (error == -1) 1043 return (0); 1044 return (error); 1045} 1046 1047/* 1048 * XXX All of these sysctls would probably be more productive dead. 1049 */ 1050static int disablecwd; 1051SYSCTL_INT(_debug, OID_AUTO, disablecwd, CTLFLAG_RW, &disablecwd, 0, 1052 "Disable the getcwd syscall"); 1053 1054/* Implementation of the getcwd syscall. */ 1055int 1056sys___getcwd(td, uap) 1057 struct thread *td; 1058 struct __getcwd_args *uap; 1059{ 1060 1061 return (kern___getcwd(td, uap->buf, UIO_USERSPACE, uap->buflen)); 1062} 1063 1064int 1065kern___getcwd(struct thread *td, char *buf, enum uio_seg bufseg, u_int buflen) 1066{ 1067 char *bp, *tmpbuf; 1068 struct filedesc *fdp; 1069 struct vnode *cdir, *rdir; 1070 int error; 1071 1072 if (disablecwd) 1073 return (ENODEV); 1074 if (buflen < 2) 1075 return (EINVAL); 1076 if (buflen > MAXPATHLEN) 1077 buflen = MAXPATHLEN; 1078 1079 tmpbuf = malloc(buflen, M_TEMP, M_WAITOK); 1080 fdp = td->td_proc->p_fd; 1081 FILEDESC_SLOCK(fdp); 1082 cdir = fdp->fd_cdir; 1083 VREF(cdir); 1084 rdir = fdp->fd_rdir; 1085 VREF(rdir); 1086 FILEDESC_SUNLOCK(fdp); 1087 error = vn_fullpath1(td, cdir, rdir, tmpbuf, &bp, buflen); 1088 vrele(rdir); 1089 vrele(cdir); 1090 1091 if (!error) { 1092 if (bufseg == UIO_SYSSPACE) 1093 bcopy(bp, buf, strlen(bp) + 1); 1094 else 1095 error = copyout(bp, buf, strlen(bp) + 1); 1096#ifdef KTRACE 1097 if (KTRPOINT(curthread, KTR_NAMEI)) 1098 ktrnamei(bp); 1099#endif 1100 } 1101 free(tmpbuf, M_TEMP); 1102 return (error); 1103} 1104 1105/* 1106 * Thus begins the fullpath magic. 1107 */ 1108 1109#undef STATNODE 1110#define STATNODE(name, descr) \ 1111 static u_int name; \ 1112 SYSCTL_UINT(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, 0, descr) 1113 1114static int disablefullpath; 1115SYSCTL_INT(_debug, OID_AUTO, disablefullpath, CTLFLAG_RW, &disablefullpath, 0, 1116 "Disable the vn_fullpath function"); 1117 1118/* These count for kern___getcwd(), too. */ 1119STATNODE(numfullpathcalls, "Number of fullpath search calls"); 1120STATNODE(numfullpathfail1, "Number of fullpath search errors (ENOTDIR)"); 1121STATNODE(numfullpathfail2, 1122 "Number of fullpath search errors (VOP_VPTOCNP failures)"); 1123STATNODE(numfullpathfail4, "Number of fullpath search errors (ENOMEM)"); 1124STATNODE(numfullpathfound, "Number of successful fullpath calls"); 1125 1126/* 1127 * Retrieve the full filesystem path that correspond to a vnode from the name 1128 * cache (if available) 1129 */ 1130int 1131vn_fullpath(struct thread *td, struct vnode *vn, char **retbuf, char **freebuf) 1132{ 1133 char *buf; 1134 struct filedesc *fdp; 1135 struct vnode *rdir; 1136 int error; 1137 1138 if (disablefullpath) 1139 return (ENODEV); 1140 if (vn == NULL) 1141 return (EINVAL); 1142 1143 buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 1144 fdp = td->td_proc->p_fd; 1145 FILEDESC_SLOCK(fdp); 1146 rdir = fdp->fd_rdir; 1147 VREF(rdir); 1148 FILEDESC_SUNLOCK(fdp); 1149 error = vn_fullpath1(td, vn, rdir, buf, retbuf, MAXPATHLEN); 1150 vrele(rdir); 1151 1152 if (!error) 1153 *freebuf = buf; 1154 else 1155 free(buf, M_TEMP); 1156 return (error); 1157} 1158 1159/* 1160 * This function is similar to vn_fullpath, but it attempts to lookup the 1161 * pathname relative to the global root mount point. This is required for the 1162 * auditing sub-system, as audited pathnames must be absolute, relative to the 1163 * global root mount point. 1164 */ 1165int 1166vn_fullpath_global(struct thread *td, struct vnode *vn, 1167 char **retbuf, char **freebuf) 1168{ 1169 char *buf; 1170 int error; 1171 1172 if (disablefullpath) 1173 return (ENODEV); 1174 if (vn == NULL) 1175 return (EINVAL); 1176 buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 1177 error = vn_fullpath1(td, vn, rootvnode, buf, retbuf, MAXPATHLEN); 1178 if (!error) 1179 *freebuf = buf; 1180 else 1181 free(buf, M_TEMP); 1182 return (error); 1183} 1184 1185int 1186vn_vptocnp(struct vnode **vp, struct ucred *cred, char *buf, u_int *buflen) 1187{ 1188 int error; 1189 1190 CACHE_RLOCK(); 1191 error = vn_vptocnp_locked(vp, cred, buf, buflen); 1192 if (error == 0) 1193 CACHE_RUNLOCK(); 1194 return (error); 1195} 1196 1197static int 1198vn_vptocnp_locked(struct vnode **vp, struct ucred *cred, char *buf, 1199 u_int *buflen) 1200{ 1201 struct vnode *dvp; 1202 struct namecache *ncp; 1203 int error; 1204 1205 TAILQ_FOREACH(ncp, &((*vp)->v_cache_dst), nc_dst) { 1206 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0) 1207 break; 1208 } 1209 if (ncp != NULL) { 1210 if (*buflen < ncp->nc_nlen) { 1211 CACHE_RUNLOCK(); 1212 vrele(*vp); 1213 numfullpathfail4++; 1214 error = ENOMEM; 1215 SDT_PROBE(vfs, namecache, fullpath, return, error, 1216 vp, NULL, 0, 0); 1217 return (error); 1218 } 1219 *buflen -= ncp->nc_nlen; 1220 memcpy(buf + *buflen, nc_get_name(ncp), ncp->nc_nlen); 1221 SDT_PROBE(vfs, namecache, fullpath, hit, ncp->nc_dvp, 1222 nc_get_name(ncp), vp, 0, 0); 1223 dvp = *vp; 1224 *vp = ncp->nc_dvp; 1225 vref(*vp); 1226 CACHE_RUNLOCK(); 1227 vrele(dvp); 1228 CACHE_RLOCK(); 1229 return (0); 1230 } 1231 SDT_PROBE(vfs, namecache, fullpath, miss, vp, 0, 0, 0, 0); 1232 1233 CACHE_RUNLOCK(); 1234 vn_lock(*vp, LK_SHARED | LK_RETRY); 1235 error = VOP_VPTOCNP(*vp, &dvp, cred, buf, buflen); 1236 vput(*vp); 1237 if (error) { 1238 numfullpathfail2++; 1239 SDT_PROBE(vfs, namecache, fullpath, return, error, vp, 1240 NULL, 0, 0); 1241 return (error); 1242 } 1243 1244 *vp = dvp; 1245 CACHE_RLOCK(); 1246 if (dvp->v_iflag & VI_DOOMED) { 1247 /* forced unmount */ 1248 CACHE_RUNLOCK(); 1249 vrele(dvp); 1250 error = ENOENT; 1251 SDT_PROBE(vfs, namecache, fullpath, return, error, vp, 1252 NULL, 0, 0); 1253 return (error); 1254 } 1255 /* 1256 * *vp has its use count incremented still. 1257 */ 1258 1259 return (0); 1260} 1261 1262/* 1263 * The magic behind kern___getcwd() and vn_fullpath(). 1264 */ 1265static int 1266vn_fullpath1(struct thread *td, struct vnode *vp, struct vnode *rdir, 1267 char *buf, char **retbuf, u_int buflen) 1268{ 1269 int error, slash_prefixed; 1270#ifdef KDTRACE_HOOKS 1271 struct vnode *startvp = vp; 1272#endif 1273 struct vnode *vp1; 1274 1275 buflen--; 1276 buf[buflen] = '\0'; 1277 error = 0; 1278 slash_prefixed = 0; 1279 1280 SDT_PROBE(vfs, namecache, fullpath, entry, vp, 0, 0, 0, 0); 1281 numfullpathcalls++; 1282 vref(vp); 1283 CACHE_RLOCK(); 1284 if (vp->v_type != VDIR) { 1285 error = vn_vptocnp_locked(&vp, td->td_ucred, buf, &buflen); 1286 if (error) 1287 return (error); 1288 if (buflen == 0) { 1289 CACHE_RUNLOCK(); 1290 vrele(vp); 1291 return (ENOMEM); 1292 } 1293 buf[--buflen] = '/'; 1294 slash_prefixed = 1; 1295 } 1296 while (vp != rdir && vp != rootvnode) { 1297 if (vp->v_vflag & VV_ROOT) { 1298 if (vp->v_iflag & VI_DOOMED) { /* forced unmount */ 1299 CACHE_RUNLOCK(); 1300 vrele(vp); 1301 error = ENOENT; 1302 SDT_PROBE(vfs, namecache, fullpath, return, 1303 error, vp, NULL, 0, 0); 1304 break; 1305 } 1306 vp1 = vp->v_mount->mnt_vnodecovered; 1307 vref(vp1); 1308 CACHE_RUNLOCK(); 1309 vrele(vp); 1310 vp = vp1; 1311 CACHE_RLOCK(); 1312 continue; 1313 } 1314 if (vp->v_type != VDIR) { 1315 CACHE_RUNLOCK(); 1316 vrele(vp); 1317 numfullpathfail1++; 1318 error = ENOTDIR; 1319 SDT_PROBE(vfs, namecache, fullpath, return, 1320 error, vp, NULL, 0, 0); 1321 break; 1322 } 1323 error = vn_vptocnp_locked(&vp, td->td_ucred, buf, &buflen); 1324 if (error) 1325 break; 1326 if (buflen == 0) { 1327 CACHE_RUNLOCK(); 1328 vrele(vp); 1329 error = ENOMEM; 1330 SDT_PROBE(vfs, namecache, fullpath, return, error, 1331 startvp, NULL, 0, 0); 1332 break; 1333 } 1334 buf[--buflen] = '/'; 1335 slash_prefixed = 1; 1336 } 1337 if (error) 1338 return (error); 1339 if (!slash_prefixed) { 1340 if (buflen == 0) { 1341 CACHE_RUNLOCK(); 1342 vrele(vp); 1343 numfullpathfail4++; 1344 SDT_PROBE(vfs, namecache, fullpath, return, ENOMEM, 1345 startvp, NULL, 0, 0); 1346 return (ENOMEM); 1347 } 1348 buf[--buflen] = '/'; 1349 } 1350 numfullpathfound++; 1351 CACHE_RUNLOCK(); 1352 vrele(vp); 1353 1354 SDT_PROBE(vfs, namecache, fullpath, return, 0, startvp, buf + buflen, 1355 0, 0); 1356 *retbuf = buf + buflen; 1357 return (0); 1358} 1359 1360struct vnode * 1361vn_dir_dd_ino(struct vnode *vp) 1362{ 1363 struct namecache *ncp; 1364 struct vnode *ddvp; 1365 1366 ASSERT_VOP_LOCKED(vp, "vn_dir_dd_ino"); 1367 CACHE_RLOCK(); 1368 TAILQ_FOREACH(ncp, &(vp->v_cache_dst), nc_dst) { 1369 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0) 1370 continue; 1371 ddvp = ncp->nc_dvp; 1372 VI_LOCK(ddvp); 1373 CACHE_RUNLOCK(); 1374 if (vget(ddvp, LK_INTERLOCK | LK_SHARED | LK_NOWAIT, curthread)) 1375 return (NULL); 1376 return (ddvp); 1377 } 1378 CACHE_RUNLOCK(); 1379 return (NULL); 1380} 1381 1382int 1383vn_commname(struct vnode *vp, char *buf, u_int buflen) 1384{ 1385 struct namecache *ncp; 1386 int l; 1387 1388 CACHE_RLOCK(); 1389 TAILQ_FOREACH(ncp, &vp->v_cache_dst, nc_dst) 1390 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0) 1391 break; 1392 if (ncp == NULL) { 1393 CACHE_RUNLOCK(); 1394 return (ENOENT); 1395 } 1396 l = min(ncp->nc_nlen, buflen - 1); 1397 memcpy(buf, nc_get_name(ncp), l); 1398 CACHE_RUNLOCK(); 1399 buf[l] = '\0'; 1400 return (0); 1401} 1402 1403/* ABI compat shims for old kernel modules. */ 1404#undef cache_enter 1405 1406void cache_enter(struct vnode *dvp, struct vnode *vp, 1407 struct componentname *cnp); 1408 1409void 1410cache_enter(struct vnode *dvp, struct vnode *vp, struct componentname *cnp) 1411{ 1412 1413 cache_enter_time(dvp, vp, cnp, NULL, NULL); 1414} 1415 1416/* 1417 * This function updates path string to vnode's full global path 1418 * and checks the size of the new path string against the pathlen argument. 1419 * 1420 * Requires a locked, referenced vnode and GIANT lock held. 1421 * Vnode is re-locked on success or ENODEV, otherwise unlocked. 1422 * 1423 * If sysctl debug.disablefullpath is set, ENODEV is returned, 1424 * vnode is left locked and path remain untouched. 1425 * 1426 * If vp is a directory, the call to vn_fullpath_global() always succeeds 1427 * because it falls back to the ".." lookup if the namecache lookup fails. 1428 */ 1429int 1430vn_path_to_global_path(struct thread *td, struct vnode *vp, char *path, 1431 u_int pathlen) 1432{ 1433 struct nameidata nd; 1434 struct vnode *vp1; 1435 char *rpath, *fbuf; 1436 int error; 1437 1438 ASSERT_VOP_ELOCKED(vp, __func__); 1439 1440 /* Return ENODEV if sysctl debug.disablefullpath==1 */ 1441 if (disablefullpath) 1442 return (ENODEV); 1443 1444 /* Construct global filesystem path from vp. */ 1445 VOP_UNLOCK(vp, 0); 1446 error = vn_fullpath_global(td, vp, &rpath, &fbuf); 1447 1448 if (error != 0) { 1449 vrele(vp); 1450 return (error); 1451 } 1452 1453 if (strlen(rpath) >= pathlen) { 1454 vrele(vp); 1455 error = ENAMETOOLONG; 1456 goto out; 1457 } 1458 1459 /* 1460 * Re-lookup the vnode by path to detect a possible rename. 1461 * As a side effect, the vnode is relocked. 1462 * If vnode was renamed, return ENOENT. 1463 */ 1464 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, 1465 UIO_SYSSPACE, path, td); 1466 error = namei(&nd); 1467 if (error != 0) { 1468 vrele(vp); 1469 goto out; 1470 } 1471 NDFREE(&nd, NDF_ONLY_PNBUF); 1472 vp1 = nd.ni_vp; 1473 vrele(vp); 1474 if (vp1 == vp) 1475 strcpy(path, rpath); 1476 else { 1477 vput(vp1); 1478 error = ENOENT; 1479 } 1480 1481out: 1482 free(fbuf, M_TEMP); 1483 return (error); 1484} 1485