1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1989, 1993, 1995 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Poul-Henning Kamp of the FreeBSD Project. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * @(#)vfs_cache.c 8.5 (Berkeley) 3/22/95 35 */ 36 37 #include <sys/cdefs.h> 38 __FBSDID("$FreeBSD$"); 39 40 #include "opt_ddb.h" 41 #include "opt_ktrace.h" 42 43 #include <sys/param.h> 44 #include <sys/systm.h> 45 #include <sys/capsicum.h> 46 #include <sys/counter.h> 47 #include <sys/filedesc.h> 48 #include <sys/fnv_hash.h> 49 #include <sys/kernel.h> 50 #include <sys/ktr.h> 51 #include <sys/lock.h> 52 #include <sys/malloc.h> 53 #include <sys/fcntl.h> 54 #include <sys/jail.h> 55 #include <sys/mount.h> 56 #include <sys/namei.h> 57 #include <sys/proc.h> 58 #include <sys/seqc.h> 59 #include <sys/sdt.h> 60 #include <sys/smr.h> 61 #include <sys/smp.h> 62 #include <sys/syscallsubr.h> 63 #include <sys/sysctl.h> 64 #include <sys/sysproto.h> 65 #include <sys/vnode.h> 66 #include <ck_queue.h> 67 #ifdef KTRACE 68 #include <sys/ktrace.h> 69 #endif 70 #ifdef INVARIANTS 71 #include <machine/_inttypes.h> 72 #endif 73 74 #include <sys/capsicum.h> 75 76 #include <security/audit/audit.h> 77 #include <security/mac/mac_framework.h> 78 79 #ifdef DDB 80 #include <ddb/ddb.h> 81 #endif 82 83 #include <vm/uma.h> 84 85 /* 86 * High level overview of name caching in the VFS layer. 87 * 88 * Originally caching was implemented as part of UFS, later extracted to allow 89 * use by other filesystems. A decision was made to make it optional and 90 * completely detached from the rest of the kernel, which comes with limitations 91 * outlined near the end of this comment block. 92 * 93 * This fundamental choice needs to be revisited. In the meantime, the current 94 * state is described below. Significance of all notable routines is explained 95 * in comments placed above their implementation. Scattered thoroughout the 96 * file are TODO comments indicating shortcomings which can be fixed without 97 * reworking everything (most of the fixes will likely be reusable). Various 98 * details are omitted from this explanation to not clutter the overview, they 99 * have to be checked by reading the code and associated commentary. 100 * 101 * Keep in mind that it's individual path components which are cached, not full 102 * paths. That is, for a fully cached path "foo/bar/baz" there are 3 entries, 103 * one for each name. 104 * 105 * I. Data organization 106 * 107 * Entries are described by "struct namecache" objects and stored in a hash 108 * table. See cache_get_hash for more information. 109 * 110 * "struct vnode" contains pointers to source entries (names which can be found 111 * when traversing through said vnode), destination entries (names of that 112 * vnode (see "Limitations" for a breakdown on the subject) and a pointer to 113 * the parent vnode. 114 * 115 * The (directory vnode; name) tuple reliably determines the target entry if 116 * it exists. 117 * 118 * Since there are no small locks at this time (all are 32 bytes in size on 119 * LP64), the code works around the problem by introducing lock arrays to 120 * protect hash buckets and vnode lists. 121 * 122 * II. Filesystem integration 123 * 124 * Filesystems participating in name caching do the following: 125 * - set vop_lookup routine to vfs_cache_lookup 126 * - set vop_cachedlookup to whatever can perform the lookup if the above fails 127 * - if they support lockless lookup (see below), vop_fplookup_vexec and 128 * vop_fplookup_symlink are set along with the MNTK_FPLOOKUP flag on the 129 * mount point 130 * - call cache_purge or cache_vop_* routines to eliminate stale entries as 131 * applicable 132 * - call cache_enter to add entries depending on the MAKEENTRY flag 133 * 134 * With the above in mind, there are 2 entry points when doing lookups: 135 * - ... -> namei -> cache_fplookup -- this is the default 136 * - ... -> VOP_LOOKUP -> vfs_cache_lookup -- normally only called by namei 137 * should the above fail 138 * 139 * Example code flow how an entry is added: 140 * ... -> namei -> cache_fplookup -> cache_fplookup_noentry -> VOP_LOOKUP -> 141 * vfs_cache_lookup -> VOP_CACHEDLOOKUP -> ufs_lookup_ino -> cache_enter 142 * 143 * III. Performance considerations 144 * 145 * For lockless case forward lookup avoids any writes to shared areas apart 146 * from the terminal path component. In other words non-modifying lookups of 147 * different files don't suffer any scalability problems in the namecache. 148 * Looking up the same file is limited by VFS and goes beyond the scope of this 149 * file. 150 * 151 * At least on amd64 the single-threaded bottleneck for long paths is hashing 152 * (see cache_get_hash). There are cases where the code issues acquire fence 153 * multiple times, they can be combined on architectures which suffer from it. 154 * 155 * For locked case each encountered vnode has to be referenced and locked in 156 * order to be handed out to the caller (normally that's namei). This 157 * introduces significant hit single-threaded and serialization multi-threaded. 158 * 159 * Reverse lookup (e.g., "getcwd") fully scales provided it is fully cached -- 160 * avoids any writes to shared areas to any components. 161 * 162 * Unrelated insertions are partially serialized on updating the global entry 163 * counter and possibly serialized on colliding bucket or vnode locks. 164 * 165 * IV. Observability 166 * 167 * Note not everything has an explicit dtrace probe nor it should have, thus 168 * some of the one-liners below depend on implementation details. 169 * 170 * Examples: 171 * 172 * # Check what lookups failed to be handled in a lockless manner. Column 1 is 173 * # line number, column 2 is status code (see cache_fpl_status) 174 * dtrace -n 'vfs:fplookup:lookup:done { @[arg1, arg2] = count(); }' 175 * 176 * # Lengths of names added by binary name 177 * dtrace -n 'fbt::cache_enter_time:entry { @[execname] = quantize(args[2]->cn_namelen); }' 178 * 179 * # Same as above but only those which exceed 64 characters 180 * dtrace -n 'fbt::cache_enter_time:entry /args[2]->cn_namelen > 64/ { @[execname] = quantize(args[2]->cn_namelen); }' 181 * 182 * # Who is performing lookups with spurious slashes (e.g., "foo//bar") and what 183 * # path is it 184 * dtrace -n 'fbt::cache_fplookup_skip_slashes:entry { @[execname, stringof(args[0]->cnp->cn_pnbuf)] = count(); }' 185 * 186 * V. Limitations and implementation defects 187 * 188 * - since it is possible there is no entry for an open file, tools like 189 * "procstat" may fail to resolve fd -> vnode -> path to anything 190 * - even if a filesystem adds an entry, it may get purged (e.g., due to memory 191 * shortage) in which case the above problem applies 192 * - hardlinks are not tracked, thus if a vnode is reachable in more than one 193 * way, resolving a name may return a different path than the one used to 194 * open it (even if said path is still valid) 195 * - by default entries are not added for newly created files 196 * - adding an entry may need to evict negative entry first, which happens in 2 197 * distinct places (evicting on lookup, adding in a later VOP) making it 198 * impossible to simply reuse it 199 * - there is a simple scheme to evict negative entries as the cache is approaching 200 * its capacity, but it is very unclear if doing so is a good idea to begin with 201 * - vnodes are subject to being recycled even if target inode is left in memory, 202 * which loses the name cache entries when it perhaps should not. in case of tmpfs 203 * names get duplicated -- kept by filesystem itself and namecache separately 204 * - struct namecache has a fixed size and comes in 2 variants, often wasting space. 205 * now hard to replace with malloc due to dependence on SMR. 206 * - lack of better integration with the kernel also turns nullfs into a layered 207 * filesystem instead of something which can take advantage of caching 208 */ 209 210 static SYSCTL_NODE(_vfs, OID_AUTO, cache, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 211 "Name cache"); 212 213 SDT_PROVIDER_DECLARE(vfs); 214 SDT_PROBE_DEFINE3(vfs, namecache, enter, done, "struct vnode *", "char *", 215 "struct vnode *"); 216 SDT_PROBE_DEFINE3(vfs, namecache, enter, duplicate, "struct vnode *", "char *", 217 "struct vnode *"); 218 SDT_PROBE_DEFINE2(vfs, namecache, enter_negative, done, "struct vnode *", 219 "char *"); 220 SDT_PROBE_DEFINE2(vfs, namecache, fullpath_smr, hit, "struct vnode *", 221 "const char *"); 222 SDT_PROBE_DEFINE4(vfs, namecache, fullpath_smr, miss, "struct vnode *", 223 "struct namecache *", "int", "int"); 224 SDT_PROBE_DEFINE1(vfs, namecache, fullpath, entry, "struct vnode *"); 225 SDT_PROBE_DEFINE3(vfs, namecache, fullpath, hit, "struct vnode *", 226 "char *", "struct vnode *"); 227 SDT_PROBE_DEFINE1(vfs, namecache, fullpath, miss, "struct vnode *"); 228 SDT_PROBE_DEFINE3(vfs, namecache, fullpath, return, "int", 229 "struct vnode *", "char *"); 230 SDT_PROBE_DEFINE3(vfs, namecache, lookup, hit, "struct vnode *", "char *", 231 "struct vnode *"); 232 SDT_PROBE_DEFINE2(vfs, namecache, lookup, hit__negative, 233 "struct vnode *", "char *"); 234 SDT_PROBE_DEFINE2(vfs, namecache, lookup, miss, "struct vnode *", 235 "char *"); 236 SDT_PROBE_DEFINE2(vfs, namecache, removecnp, hit, "struct vnode *", 237 "struct componentname *"); 238 SDT_PROBE_DEFINE2(vfs, namecache, removecnp, miss, "struct vnode *", 239 "struct componentname *"); 240 SDT_PROBE_DEFINE1(vfs, namecache, purge, done, "struct vnode *"); 241 SDT_PROBE_DEFINE1(vfs, namecache, purge, batch, "int"); 242 SDT_PROBE_DEFINE1(vfs, namecache, purge_negative, done, "struct vnode *"); 243 SDT_PROBE_DEFINE1(vfs, namecache, purgevfs, done, "struct mount *"); 244 SDT_PROBE_DEFINE3(vfs, namecache, zap, done, "struct vnode *", "char *", 245 "struct vnode *"); 246 SDT_PROBE_DEFINE2(vfs, namecache, zap_negative, done, "struct vnode *", 247 "char *"); 248 SDT_PROBE_DEFINE2(vfs, namecache, evict_negative, done, "struct vnode *", 249 "char *"); 250 SDT_PROBE_DEFINE1(vfs, namecache, symlink, alloc__fail, "size_t"); 251 252 SDT_PROBE_DEFINE3(vfs, fplookup, lookup, done, "struct nameidata", "int", "bool"); 253 SDT_PROBE_DECLARE(vfs, namei, lookup, entry); 254 SDT_PROBE_DECLARE(vfs, namei, lookup, return); 255 256 /* 257 * This structure describes the elements in the cache of recent 258 * names looked up by namei. 259 */ 260 struct negstate { 261 u_char neg_flag; 262 u_char neg_hit; 263 }; 264 _Static_assert(sizeof(struct negstate) <= sizeof(struct vnode *), 265 "the state must fit in a union with a pointer without growing it"); 266 267 struct namecache { 268 LIST_ENTRY(namecache) nc_src; /* source vnode list */ 269 TAILQ_ENTRY(namecache) nc_dst; /* destination vnode list */ 270 CK_SLIST_ENTRY(namecache) nc_hash;/* hash chain */ 271 struct vnode *nc_dvp; /* vnode of parent of name */ 272 union { 273 struct vnode *nu_vp; /* vnode the name refers to */ 274 struct negstate nu_neg;/* negative entry state */ 275 } n_un; 276 u_char nc_flag; /* flag bits */ 277 u_char nc_nlen; /* length of name */ 278 char nc_name[0]; /* segment name + nul */ 279 }; 280 281 /* 282 * struct namecache_ts repeats struct namecache layout up to the 283 * nc_nlen member. 284 * struct namecache_ts is used in place of struct namecache when time(s) need 285 * to be stored. The nc_dotdottime field is used when a cache entry is mapping 286 * both a non-dotdot directory name plus dotdot for the directory's 287 * parent. 288 * 289 * See below for alignment requirement. 290 */ 291 struct namecache_ts { 292 struct timespec nc_time; /* timespec provided by fs */ 293 struct timespec nc_dotdottime; /* dotdot timespec provided by fs */ 294 int nc_ticks; /* ticks value when entry was added */ 295 int nc_pad; 296 struct namecache nc_nc; 297 }; 298 299 TAILQ_HEAD(cache_freebatch, namecache); 300 301 /* 302 * At least mips n32 performs 64-bit accesses to timespec as found 303 * in namecache_ts and requires them to be aligned. Since others 304 * may be in the same spot suffer a little bit and enforce the 305 * alignment for everyone. Note this is a nop for 64-bit platforms. 306 */ 307 #define CACHE_ZONE_ALIGNMENT UMA_ALIGNOF(time_t) 308 309 /* 310 * TODO: the initial value of CACHE_PATH_CUTOFF was inherited from the 311 * 4.4 BSD codebase. Later on struct namecache was tweaked to become 312 * smaller and the value was bumped to retain the total size, but it 313 * was never re-evaluated for suitability. A simple test counting 314 * lengths during package building shows that the value of 45 covers 315 * about 86% of all added entries, reaching 99% at 65. 316 * 317 * Regardless of the above, use of dedicated zones instead of malloc may be 318 * inducing additional waste. This may be hard to address as said zones are 319 * tied to VFS SMR. Even if retaining them, the current split should be 320 * re-evaluated. 321 */ 322 #ifdef __LP64__ 323 #define CACHE_PATH_CUTOFF 45 324 #define CACHE_LARGE_PAD 6 325 #else 326 #define CACHE_PATH_CUTOFF 41 327 #define CACHE_LARGE_PAD 2 328 #endif 329 330 #define CACHE_ZONE_SMALL_SIZE (offsetof(struct namecache, nc_name) + CACHE_PATH_CUTOFF + 1) 331 #define CACHE_ZONE_SMALL_TS_SIZE (offsetof(struct namecache_ts, nc_nc) + CACHE_ZONE_SMALL_SIZE) 332 #define CACHE_ZONE_LARGE_SIZE (offsetof(struct namecache, nc_name) + NAME_MAX + 1 + CACHE_LARGE_PAD) 333 #define CACHE_ZONE_LARGE_TS_SIZE (offsetof(struct namecache_ts, nc_nc) + CACHE_ZONE_LARGE_SIZE) 334 335 _Static_assert((CACHE_ZONE_SMALL_SIZE % (CACHE_ZONE_ALIGNMENT + 1)) == 0, "bad zone size"); 336 _Static_assert((CACHE_ZONE_SMALL_TS_SIZE % (CACHE_ZONE_ALIGNMENT + 1)) == 0, "bad zone size"); 337 _Static_assert((CACHE_ZONE_LARGE_SIZE % (CACHE_ZONE_ALIGNMENT + 1)) == 0, "bad zone size"); 338 _Static_assert((CACHE_ZONE_LARGE_TS_SIZE % (CACHE_ZONE_ALIGNMENT + 1)) == 0, "bad zone size"); 339 340 #define nc_vp n_un.nu_vp 341 #define nc_neg n_un.nu_neg 342 343 /* 344 * Flags in namecache.nc_flag 345 */ 346 #define NCF_WHITE 0x01 347 #define NCF_ISDOTDOT 0x02 348 #define NCF_TS 0x04 349 #define NCF_DTS 0x08 350 #define NCF_DVDROP 0x10 351 #define NCF_NEGATIVE 0x20 352 #define NCF_INVALID 0x40 353 #define NCF_WIP 0x80 354 355 /* 356 * Flags in negstate.neg_flag 357 */ 358 #define NEG_HOT 0x01 359 360 static bool cache_neg_evict_cond(u_long lnumcache); 361 362 /* 363 * Mark an entry as invalid. 364 * 365 * This is called before it starts getting deconstructed. 366 */ 367 static void 368 cache_ncp_invalidate(struct namecache *ncp) 369 { 370 371 KASSERT((ncp->nc_flag & NCF_INVALID) == 0, 372 ("%s: entry %p already invalid", __func__, ncp)); 373 atomic_store_char(&ncp->nc_flag, ncp->nc_flag | NCF_INVALID); 374 atomic_thread_fence_rel(); 375 } 376 377 /* 378 * Check whether the entry can be safely used. 379 * 380 * All places which elide locks are supposed to call this after they are 381 * done with reading from an entry. 382 */ 383 #define cache_ncp_canuse(ncp) ({ \ 384 struct namecache *_ncp = (ncp); \ 385 u_char _nc_flag; \ 386 \ 387 atomic_thread_fence_acq(); \ 388 _nc_flag = atomic_load_char(&_ncp->nc_flag); \ 389 __predict_true((_nc_flag & (NCF_INVALID | NCF_WIP)) == 0); \ 390 }) 391 392 /* 393 * Like the above but also checks NCF_WHITE. 394 */ 395 #define cache_fpl_neg_ncp_canuse(ncp) ({ \ 396 struct namecache *_ncp = (ncp); \ 397 u_char _nc_flag; \ 398 \ 399 atomic_thread_fence_acq(); \ 400 _nc_flag = atomic_load_char(&_ncp->nc_flag); \ 401 __predict_true((_nc_flag & (NCF_INVALID | NCF_WIP | NCF_WHITE)) == 0); \ 402 }) 403 404 VFS_SMR_DECLARE; 405 406 static SYSCTL_NODE(_vfs_cache, OID_AUTO, param, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 407 "Name cache parameters"); 408 409 static u_int __read_mostly ncsize; /* the size as computed on creation or resizing */ 410 SYSCTL_UINT(_vfs_cache_param, OID_AUTO, size, CTLFLAG_RW, &ncsize, 0, 411 "Total namecache capacity"); 412 413 u_int ncsizefactor = 2; 414 SYSCTL_UINT(_vfs_cache_param, OID_AUTO, sizefactor, CTLFLAG_RW, &ncsizefactor, 0, 415 "Size factor for namecache"); 416 417 static u_long __read_mostly ncnegfactor = 5; /* ratio of negative entries */ 418 SYSCTL_ULONG(_vfs_cache_param, OID_AUTO, negfactor, CTLFLAG_RW, &ncnegfactor, 0, 419 "Ratio of negative namecache entries"); 420 421 /* 422 * Negative entry % of namecache capacity above which automatic eviction is allowed. 423 * 424 * Check cache_neg_evict_cond for details. 425 */ 426 static u_int ncnegminpct = 3; 427 428 static u_int __read_mostly neg_min; /* the above recomputed against ncsize */ 429 SYSCTL_UINT(_vfs_cache_param, OID_AUTO, negmin, CTLFLAG_RD, &neg_min, 0, 430 "Negative entry count above which automatic eviction is allowed"); 431 432 /* 433 * Structures associated with name caching. 434 */ 435 #define NCHHASH(hash) \ 436 (&nchashtbl[(hash) & nchash]) 437 static __read_mostly CK_SLIST_HEAD(nchashhead, namecache) *nchashtbl;/* Hash Table */ 438 static u_long __read_mostly nchash; /* size of hash table */ 439 SYSCTL_ULONG(_debug, OID_AUTO, nchash, CTLFLAG_RD, &nchash, 0, 440 "Size of namecache hash table"); 441 static u_long __exclusive_cache_line numneg; /* number of negative entries allocated */ 442 static u_long __exclusive_cache_line numcache;/* number of cache entries allocated */ 443 444 struct nchstats nchstats; /* cache effectiveness statistics */ 445 446 static bool __read_frequently cache_fast_revlookup = true; 447 SYSCTL_BOOL(_vfs, OID_AUTO, cache_fast_revlookup, CTLFLAG_RW, 448 &cache_fast_revlookup, 0, ""); 449 450 static bool __read_mostly cache_rename_add = true; 451 SYSCTL_BOOL(_vfs, OID_AUTO, cache_rename_add, CTLFLAG_RW, 452 &cache_rename_add, 0, ""); 453 454 static u_int __exclusive_cache_line neg_cycle; 455 456 #define ncneghash 3 457 #define numneglists (ncneghash + 1) 458 459 struct neglist { 460 struct mtx nl_evict_lock; 461 struct mtx nl_lock __aligned(CACHE_LINE_SIZE); 462 TAILQ_HEAD(, namecache) nl_list; 463 TAILQ_HEAD(, namecache) nl_hotlist; 464 u_long nl_hotnum; 465 } __aligned(CACHE_LINE_SIZE); 466 467 static struct neglist neglists[numneglists]; 468 469 static inline struct neglist * 470 NCP2NEGLIST(struct namecache *ncp) 471 { 472 473 return (&neglists[(((uintptr_t)(ncp) >> 8) & ncneghash)]); 474 } 475 476 static inline struct negstate * 477 NCP2NEGSTATE(struct namecache *ncp) 478 { 479 480 MPASS(atomic_load_char(&ncp->nc_flag) & NCF_NEGATIVE); 481 return (&ncp->nc_neg); 482 } 483 484 #define numbucketlocks (ncbuckethash + 1) 485 static u_int __read_mostly ncbuckethash; 486 static struct mtx_padalign __read_mostly *bucketlocks; 487 #define HASH2BUCKETLOCK(hash) \ 488 ((struct mtx *)(&bucketlocks[((hash) & ncbuckethash)])) 489 490 #define numvnodelocks (ncvnodehash + 1) 491 static u_int __read_mostly ncvnodehash; 492 static struct mtx __read_mostly *vnodelocks; 493 static inline struct mtx * 494 VP2VNODELOCK(struct vnode *vp) 495 { 496 497 return (&vnodelocks[(((uintptr_t)(vp) >> 8) & ncvnodehash)]); 498 } 499 500 static void 501 cache_out_ts(struct namecache *ncp, struct timespec *tsp, int *ticksp) 502 { 503 struct namecache_ts *ncp_ts; 504 505 KASSERT((ncp->nc_flag & NCF_TS) != 0 || 506 (tsp == NULL && ticksp == NULL), 507 ("No NCF_TS")); 508 509 if (tsp == NULL) 510 return; 511 512 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc); 513 *tsp = ncp_ts->nc_time; 514 *ticksp = ncp_ts->nc_ticks; 515 } 516 517 #ifdef DEBUG_CACHE 518 static int __read_mostly doingcache = 1; /* 1 => enable the cache */ 519 SYSCTL_INT(_debug, OID_AUTO, vfscache, CTLFLAG_RW, &doingcache, 0, 520 "VFS namecache enabled"); 521 #endif 522 523 /* Export size information to userland */ 524 SYSCTL_INT(_debug_sizeof, OID_AUTO, namecache, CTLFLAG_RD, SYSCTL_NULL_INT_PTR, 525 sizeof(struct namecache), "sizeof(struct namecache)"); 526 527 /* 528 * The new name cache statistics 529 */ 530 static SYSCTL_NODE(_vfs_cache, OID_AUTO, stats, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 531 "Name cache statistics"); 532 533 #define STATNODE_ULONG(name, varname, descr) \ 534 SYSCTL_ULONG(_vfs_cache_stats, OID_AUTO, name, CTLFLAG_RD, &varname, 0, descr); 535 #define STATNODE_COUNTER(name, varname, descr) \ 536 static COUNTER_U64_DEFINE_EARLY(varname); \ 537 SYSCTL_COUNTER_U64(_vfs_cache_stats, OID_AUTO, name, CTLFLAG_RD, &varname, \ 538 descr); 539 STATNODE_ULONG(neg, numneg, "Number of negative cache entries"); 540 STATNODE_ULONG(count, numcache, "Number of cache entries"); 541 STATNODE_COUNTER(heldvnodes, numcachehv, "Number of namecache entries with vnodes held"); 542 STATNODE_COUNTER(drops, numdrops, "Number of dropped entries due to reaching the limit"); 543 STATNODE_COUNTER(dothits, dothits, "Number of '.' hits"); 544 STATNODE_COUNTER(dotdothis, dotdothits, "Number of '..' hits"); 545 STATNODE_COUNTER(miss, nummiss, "Number of cache misses"); 546 STATNODE_COUNTER(misszap, nummisszap, "Number of cache misses we do not want to cache"); 547 STATNODE_COUNTER(posszaps, numposzaps, 548 "Number of cache hits (positive) we do not want to cache"); 549 STATNODE_COUNTER(poshits, numposhits, "Number of cache hits (positive)"); 550 STATNODE_COUNTER(negzaps, numnegzaps, 551 "Number of cache hits (negative) we do not want to cache"); 552 STATNODE_COUNTER(neghits, numneghits, "Number of cache hits (negative)"); 553 /* These count for vn_getcwd(), too. */ 554 STATNODE_COUNTER(fullpathcalls, numfullpathcalls, "Number of fullpath search calls"); 555 STATNODE_COUNTER(fullpathfail1, numfullpathfail1, "Number of fullpath search errors (ENOTDIR)"); 556 STATNODE_COUNTER(fullpathfail2, numfullpathfail2, 557 "Number of fullpath search errors (VOP_VPTOCNP failures)"); 558 STATNODE_COUNTER(fullpathfail4, numfullpathfail4, "Number of fullpath search errors (ENOMEM)"); 559 STATNODE_COUNTER(fullpathfound, numfullpathfound, "Number of successful fullpath calls"); 560 STATNODE_COUNTER(symlinktoobig, symlinktoobig, "Number of times symlink did not fit the cache"); 561 562 /* 563 * Debug or developer statistics. 564 */ 565 static SYSCTL_NODE(_vfs_cache, OID_AUTO, debug, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 566 "Name cache debugging"); 567 #define DEBUGNODE_ULONG(name, varname, descr) \ 568 SYSCTL_ULONG(_vfs_cache_debug, OID_AUTO, name, CTLFLAG_RD, &varname, 0, descr); 569 #define DEBUGNODE_COUNTER(name, varname, descr) \ 570 static COUNTER_U64_DEFINE_EARLY(varname); \ 571 SYSCTL_COUNTER_U64(_vfs_cache_debug, OID_AUTO, name, CTLFLAG_RD, &varname, \ 572 descr); 573 DEBUGNODE_COUNTER(zap_bucket_relock_success, zap_bucket_relock_success, 574 "Number of successful removals after relocking"); 575 static long zap_bucket_fail; 576 DEBUGNODE_ULONG(zap_bucket_fail, zap_bucket_fail, ""); 577 static long zap_bucket_fail2; 578 DEBUGNODE_ULONG(zap_bucket_fail2, zap_bucket_fail2, ""); 579 static long cache_lock_vnodes_cel_3_failures; 580 DEBUGNODE_ULONG(vnodes_cel_3_failures, cache_lock_vnodes_cel_3_failures, 581 "Number of times 3-way vnode locking failed"); 582 583 static void cache_fplookup_lockout(void); 584 static void cache_fplookup_restore(void); 585 586 static void cache_zap_locked(struct namecache *ncp); 587 static int vn_fullpath_hardlink(struct nameidata *ndp, char **retbuf, 588 char **freebuf, size_t *buflen); 589 static int vn_fullpath_any_smr(struct vnode *vp, struct vnode *rdir, char *buf, 590 char **retbuf, size_t *buflen, size_t addend); 591 static int vn_fullpath_any(struct vnode *vp, struct vnode *rdir, char *buf, 592 char **retbuf, size_t *buflen); 593 static int vn_fullpath_dir(struct vnode *vp, struct vnode *rdir, char *buf, 594 char **retbuf, size_t *len, size_t addend); 595 596 static MALLOC_DEFINE(M_VFSCACHE, "vfscache", "VFS name cache entries"); 597 598 static inline void 599 cache_assert_vlp_locked(struct mtx *vlp) 600 { 601 602 if (vlp != NULL) 603 mtx_assert(vlp, MA_OWNED); 604 } 605 606 static inline void 607 cache_assert_vnode_locked(struct vnode *vp) 608 { 609 struct mtx *vlp; 610 611 vlp = VP2VNODELOCK(vp); 612 cache_assert_vlp_locked(vlp); 613 } 614 615 /* 616 * Directory vnodes with entries are held for two reasons: 617 * 1. make them less of a target for reclamation in vnlru 618 * 2. suffer smaller performance penalty in locked lookup as requeieing is avoided 619 * 620 * It will be feasible to stop doing it altogether if all filesystems start 621 * supporting lockless lookup. 622 */ 623 static void 624 cache_hold_vnode(struct vnode *vp) 625 { 626 627 cache_assert_vnode_locked(vp); 628 VNPASS(LIST_EMPTY(&vp->v_cache_src), vp); 629 vhold(vp); 630 counter_u64_add(numcachehv, 1); 631 } 632 633 static void 634 cache_drop_vnode(struct vnode *vp) 635 { 636 637 /* 638 * Called after all locks are dropped, meaning we can't assert 639 * on the state of v_cache_src. 640 */ 641 vdrop(vp); 642 counter_u64_add(numcachehv, -1); 643 } 644 645 /* 646 * UMA zones. 647 */ 648 static uma_zone_t __read_mostly cache_zone_small; 649 static uma_zone_t __read_mostly cache_zone_small_ts; 650 static uma_zone_t __read_mostly cache_zone_large; 651 static uma_zone_t __read_mostly cache_zone_large_ts; 652 653 char * 654 cache_symlink_alloc(size_t size, int flags) 655 { 656 657 if (size < CACHE_ZONE_SMALL_SIZE) { 658 return (uma_zalloc_smr(cache_zone_small, flags)); 659 } 660 if (size < CACHE_ZONE_LARGE_SIZE) { 661 return (uma_zalloc_smr(cache_zone_large, flags)); 662 } 663 counter_u64_add(symlinktoobig, 1); 664 SDT_PROBE1(vfs, namecache, symlink, alloc__fail, size); 665 return (NULL); 666 } 667 668 void 669 cache_symlink_free(char *string, size_t size) 670 { 671 672 MPASS(string != NULL); 673 KASSERT(size < CACHE_ZONE_LARGE_SIZE, 674 ("%s: size %zu too big", __func__, size)); 675 676 if (size < CACHE_ZONE_SMALL_SIZE) { 677 uma_zfree_smr(cache_zone_small, string); 678 return; 679 } 680 if (size < CACHE_ZONE_LARGE_SIZE) { 681 uma_zfree_smr(cache_zone_large, string); 682 return; 683 } 684 __assert_unreachable(); 685 } 686 687 static struct namecache * 688 cache_alloc_uma(int len, bool ts) 689 { 690 struct namecache_ts *ncp_ts; 691 struct namecache *ncp; 692 693 if (__predict_false(ts)) { 694 if (len <= CACHE_PATH_CUTOFF) 695 ncp_ts = uma_zalloc_smr(cache_zone_small_ts, M_WAITOK); 696 else 697 ncp_ts = uma_zalloc_smr(cache_zone_large_ts, M_WAITOK); 698 ncp = &ncp_ts->nc_nc; 699 } else { 700 if (len <= CACHE_PATH_CUTOFF) 701 ncp = uma_zalloc_smr(cache_zone_small, M_WAITOK); 702 else 703 ncp = uma_zalloc_smr(cache_zone_large, M_WAITOK); 704 } 705 return (ncp); 706 } 707 708 static void 709 cache_free_uma(struct namecache *ncp) 710 { 711 struct namecache_ts *ncp_ts; 712 713 if (__predict_false(ncp->nc_flag & NCF_TS)) { 714 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc); 715 if (ncp->nc_nlen <= CACHE_PATH_CUTOFF) 716 uma_zfree_smr(cache_zone_small_ts, ncp_ts); 717 else 718 uma_zfree_smr(cache_zone_large_ts, ncp_ts); 719 } else { 720 if (ncp->nc_nlen <= CACHE_PATH_CUTOFF) 721 uma_zfree_smr(cache_zone_small, ncp); 722 else 723 uma_zfree_smr(cache_zone_large, ncp); 724 } 725 } 726 727 static struct namecache * 728 cache_alloc(int len, bool ts) 729 { 730 u_long lnumcache; 731 732 /* 733 * Avoid blowout in namecache entries. 734 * 735 * Bugs: 736 * 1. filesystems may end up trying to add an already existing entry 737 * (for example this can happen after a cache miss during concurrent 738 * lookup), in which case we will call cache_neg_evict despite not 739 * adding anything. 740 * 2. the routine may fail to free anything and no provisions are made 741 * to make it try harder (see the inside for failure modes) 742 * 3. it only ever looks at negative entries. 743 */ 744 lnumcache = atomic_fetchadd_long(&numcache, 1) + 1; 745 if (cache_neg_evict_cond(lnumcache)) { 746 lnumcache = atomic_load_long(&numcache); 747 } 748 if (__predict_false(lnumcache >= ncsize)) { 749 atomic_subtract_long(&numcache, 1); 750 counter_u64_add(numdrops, 1); 751 return (NULL); 752 } 753 return (cache_alloc_uma(len, ts)); 754 } 755 756 static void 757 cache_free(struct namecache *ncp) 758 { 759 760 MPASS(ncp != NULL); 761 if ((ncp->nc_flag & NCF_DVDROP) != 0) { 762 cache_drop_vnode(ncp->nc_dvp); 763 } 764 cache_free_uma(ncp); 765 atomic_subtract_long(&numcache, 1); 766 } 767 768 static void 769 cache_free_batch(struct cache_freebatch *batch) 770 { 771 struct namecache *ncp, *nnp; 772 int i; 773 774 i = 0; 775 if (TAILQ_EMPTY(batch)) 776 goto out; 777 TAILQ_FOREACH_SAFE(ncp, batch, nc_dst, nnp) { 778 if ((ncp->nc_flag & NCF_DVDROP) != 0) { 779 cache_drop_vnode(ncp->nc_dvp); 780 } 781 cache_free_uma(ncp); 782 i++; 783 } 784 atomic_subtract_long(&numcache, i); 785 out: 786 SDT_PROBE1(vfs, namecache, purge, batch, i); 787 } 788 789 /* 790 * Hashing. 791 * 792 * The code was made to use FNV in 2001 and this choice needs to be revisited. 793 * 794 * Short summary of the difficulty: 795 * The longest name which can be inserted is NAME_MAX characters in length (or 796 * 255 at the time of writing this comment), while majority of names used in 797 * practice are significantly shorter (mostly below 10). More importantly 798 * majority of lookups performed find names are even shorter than that. 799 * 800 * This poses a problem where hashes which do better than FNV past word size 801 * (or so) tend to come with additional overhead when finalizing the result, 802 * making them noticeably slower for the most commonly used range. 803 * 804 * Consider a path like: /usr/obj/usr/src/sys/amd64/GENERIC/vnode_if.c 805 * 806 * When looking it up the most time consuming part by a large margin (at least 807 * on amd64) is hashing. Replacing FNV with something which pessimizes short 808 * input would make the slowest part stand out even more. 809 */ 810 811 /* 812 * TODO: With the value stored we can do better than computing the hash based 813 * on the address. 814 */ 815 static void 816 cache_prehash(struct vnode *vp) 817 { 818 819 vp->v_nchash = fnv_32_buf(&vp, sizeof(vp), FNV1_32_INIT); 820 } 821 822 static uint32_t 823 cache_get_hash(char *name, u_char len, struct vnode *dvp) 824 { 825 826 return (fnv_32_buf(name, len, dvp->v_nchash)); 827 } 828 829 static uint32_t 830 cache_get_hash_iter_start(struct vnode *dvp) 831 { 832 833 return (dvp->v_nchash); 834 } 835 836 static uint32_t 837 cache_get_hash_iter(char c, uint32_t hash) 838 { 839 840 return (fnv_32_buf(&c, 1, hash)); 841 } 842 843 static uint32_t 844 cache_get_hash_iter_finish(uint32_t hash) 845 { 846 847 return (hash); 848 } 849 850 static inline struct nchashhead * 851 NCP2BUCKET(struct namecache *ncp) 852 { 853 uint32_t hash; 854 855 hash = cache_get_hash(ncp->nc_name, ncp->nc_nlen, ncp->nc_dvp); 856 return (NCHHASH(hash)); 857 } 858 859 static inline struct mtx * 860 NCP2BUCKETLOCK(struct namecache *ncp) 861 { 862 uint32_t hash; 863 864 hash = cache_get_hash(ncp->nc_name, ncp->nc_nlen, ncp->nc_dvp); 865 return (HASH2BUCKETLOCK(hash)); 866 } 867 868 #ifdef INVARIANTS 869 static void 870 cache_assert_bucket_locked(struct namecache *ncp) 871 { 872 struct mtx *blp; 873 874 blp = NCP2BUCKETLOCK(ncp); 875 mtx_assert(blp, MA_OWNED); 876 } 877 878 static void 879 cache_assert_bucket_unlocked(struct namecache *ncp) 880 { 881 struct mtx *blp; 882 883 blp = NCP2BUCKETLOCK(ncp); 884 mtx_assert(blp, MA_NOTOWNED); 885 } 886 #else 887 #define cache_assert_bucket_locked(x) do { } while (0) 888 #define cache_assert_bucket_unlocked(x) do { } while (0) 889 #endif 890 891 #define cache_sort_vnodes(x, y) _cache_sort_vnodes((void **)(x), (void **)(y)) 892 static void 893 _cache_sort_vnodes(void **p1, void **p2) 894 { 895 void *tmp; 896 897 MPASS(*p1 != NULL || *p2 != NULL); 898 899 if (*p1 > *p2) { 900 tmp = *p2; 901 *p2 = *p1; 902 *p1 = tmp; 903 } 904 } 905 906 static void 907 cache_lock_all_buckets(void) 908 { 909 u_int i; 910 911 for (i = 0; i < numbucketlocks; i++) 912 mtx_lock(&bucketlocks[i]); 913 } 914 915 static void 916 cache_unlock_all_buckets(void) 917 { 918 u_int i; 919 920 for (i = 0; i < numbucketlocks; i++) 921 mtx_unlock(&bucketlocks[i]); 922 } 923 924 static void 925 cache_lock_all_vnodes(void) 926 { 927 u_int i; 928 929 for (i = 0; i < numvnodelocks; i++) 930 mtx_lock(&vnodelocks[i]); 931 } 932 933 static void 934 cache_unlock_all_vnodes(void) 935 { 936 u_int i; 937 938 for (i = 0; i < numvnodelocks; i++) 939 mtx_unlock(&vnodelocks[i]); 940 } 941 942 static int 943 cache_trylock_vnodes(struct mtx *vlp1, struct mtx *vlp2) 944 { 945 946 cache_sort_vnodes(&vlp1, &vlp2); 947 948 if (vlp1 != NULL) { 949 if (!mtx_trylock(vlp1)) 950 return (EAGAIN); 951 } 952 if (!mtx_trylock(vlp2)) { 953 if (vlp1 != NULL) 954 mtx_unlock(vlp1); 955 return (EAGAIN); 956 } 957 958 return (0); 959 } 960 961 static void 962 cache_lock_vnodes(struct mtx *vlp1, struct mtx *vlp2) 963 { 964 965 MPASS(vlp1 != NULL || vlp2 != NULL); 966 MPASS(vlp1 <= vlp2); 967 968 if (vlp1 != NULL) 969 mtx_lock(vlp1); 970 if (vlp2 != NULL) 971 mtx_lock(vlp2); 972 } 973 974 static void 975 cache_unlock_vnodes(struct mtx *vlp1, struct mtx *vlp2) 976 { 977 978 MPASS(vlp1 != NULL || vlp2 != NULL); 979 980 if (vlp1 != NULL) 981 mtx_unlock(vlp1); 982 if (vlp2 != NULL) 983 mtx_unlock(vlp2); 984 } 985 986 static int 987 sysctl_nchstats(SYSCTL_HANDLER_ARGS) 988 { 989 struct nchstats snap; 990 991 if (req->oldptr == NULL) 992 return (SYSCTL_OUT(req, 0, sizeof(snap))); 993 994 snap = nchstats; 995 snap.ncs_goodhits = counter_u64_fetch(numposhits); 996 snap.ncs_neghits = counter_u64_fetch(numneghits); 997 snap.ncs_badhits = counter_u64_fetch(numposzaps) + 998 counter_u64_fetch(numnegzaps); 999 snap.ncs_miss = counter_u64_fetch(nummisszap) + 1000 counter_u64_fetch(nummiss); 1001 1002 return (SYSCTL_OUT(req, &snap, sizeof(snap))); 1003 } 1004 SYSCTL_PROC(_vfs_cache, OID_AUTO, nchstats, CTLTYPE_OPAQUE | CTLFLAG_RD | 1005 CTLFLAG_MPSAFE, 0, 0, sysctl_nchstats, "LU", 1006 "VFS cache effectiveness statistics"); 1007 1008 static void 1009 cache_recalc_neg_min(u_int val) 1010 { 1011 1012 neg_min = (ncsize * val) / 100; 1013 } 1014 1015 static int 1016 sysctl_negminpct(SYSCTL_HANDLER_ARGS) 1017 { 1018 u_int val; 1019 int error; 1020 1021 val = ncnegminpct; 1022 error = sysctl_handle_int(oidp, &val, 0, req); 1023 if (error != 0 || req->newptr == NULL) 1024 return (error); 1025 1026 if (val == ncnegminpct) 1027 return (0); 1028 if (val < 0 || val > 99) 1029 return (EINVAL); 1030 ncnegminpct = val; 1031 cache_recalc_neg_min(val); 1032 return (0); 1033 } 1034 1035 SYSCTL_PROC(_vfs_cache_param, OID_AUTO, negminpct, 1036 CTLTYPE_INT | CTLFLAG_MPSAFE | CTLFLAG_RW, NULL, 0, sysctl_negminpct, 1037 "I", "Negative entry \% of namecache capacity above which automatic eviction is allowed"); 1038 1039 #ifdef DIAGNOSTIC 1040 /* 1041 * Grab an atomic snapshot of the name cache hash chain lengths 1042 */ 1043 static SYSCTL_NODE(_debug, OID_AUTO, hashstat, 1044 CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, 1045 "hash table stats"); 1046 1047 static int 1048 sysctl_debug_hashstat_rawnchash(SYSCTL_HANDLER_ARGS) 1049 { 1050 struct nchashhead *ncpp; 1051 struct namecache *ncp; 1052 int i, error, n_nchash, *cntbuf; 1053 1054 retry: 1055 n_nchash = nchash + 1; /* nchash is max index, not count */ 1056 if (req->oldptr == NULL) 1057 return SYSCTL_OUT(req, 0, n_nchash * sizeof(int)); 1058 cntbuf = malloc(n_nchash * sizeof(int), M_TEMP, M_ZERO | M_WAITOK); 1059 cache_lock_all_buckets(); 1060 if (n_nchash != nchash + 1) { 1061 cache_unlock_all_buckets(); 1062 free(cntbuf, M_TEMP); 1063 goto retry; 1064 } 1065 /* Scan hash tables counting entries */ 1066 for (ncpp = nchashtbl, i = 0; i < n_nchash; ncpp++, i++) 1067 CK_SLIST_FOREACH(ncp, ncpp, nc_hash) 1068 cntbuf[i]++; 1069 cache_unlock_all_buckets(); 1070 for (error = 0, i = 0; i < n_nchash; i++) 1071 if ((error = SYSCTL_OUT(req, &cntbuf[i], sizeof(int))) != 0) 1072 break; 1073 free(cntbuf, M_TEMP); 1074 return (error); 1075 } 1076 SYSCTL_PROC(_debug_hashstat, OID_AUTO, rawnchash, CTLTYPE_INT|CTLFLAG_RD| 1077 CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_rawnchash, "S,int", 1078 "nchash chain lengths"); 1079 1080 static int 1081 sysctl_debug_hashstat_nchash(SYSCTL_HANDLER_ARGS) 1082 { 1083 int error; 1084 struct nchashhead *ncpp; 1085 struct namecache *ncp; 1086 int n_nchash; 1087 int count, maxlength, used, pct; 1088 1089 if (!req->oldptr) 1090 return SYSCTL_OUT(req, 0, 4 * sizeof(int)); 1091 1092 cache_lock_all_buckets(); 1093 n_nchash = nchash + 1; /* nchash is max index, not count */ 1094 used = 0; 1095 maxlength = 0; 1096 1097 /* Scan hash tables for applicable entries */ 1098 for (ncpp = nchashtbl; n_nchash > 0; n_nchash--, ncpp++) { 1099 count = 0; 1100 CK_SLIST_FOREACH(ncp, ncpp, nc_hash) { 1101 count++; 1102 } 1103 if (count) 1104 used++; 1105 if (maxlength < count) 1106 maxlength = count; 1107 } 1108 n_nchash = nchash + 1; 1109 cache_unlock_all_buckets(); 1110 pct = (used * 100) / (n_nchash / 100); 1111 error = SYSCTL_OUT(req, &n_nchash, sizeof(n_nchash)); 1112 if (error) 1113 return (error); 1114 error = SYSCTL_OUT(req, &used, sizeof(used)); 1115 if (error) 1116 return (error); 1117 error = SYSCTL_OUT(req, &maxlength, sizeof(maxlength)); 1118 if (error) 1119 return (error); 1120 error = SYSCTL_OUT(req, &pct, sizeof(pct)); 1121 if (error) 1122 return (error); 1123 return (0); 1124 } 1125 SYSCTL_PROC(_debug_hashstat, OID_AUTO, nchash, CTLTYPE_INT|CTLFLAG_RD| 1126 CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_nchash, "I", 1127 "nchash statistics (number of total/used buckets, maximum chain length, usage percentage)"); 1128 #endif 1129 1130 /* 1131 * Negative entries management 1132 * 1133 * Various workloads create plenty of negative entries and barely use them 1134 * afterwards. Moreover malicious users can keep performing bogus lookups 1135 * adding even more entries. For example "make tinderbox" as of writing this 1136 * comment ends up with 2.6M namecache entries in total, 1.2M of which are 1137 * negative. 1138 * 1139 * As such, a rather aggressive eviction method is needed. The currently 1140 * employed method is a placeholder. 1141 * 1142 * Entries are split over numneglists separate lists, each of which is further 1143 * split into hot and cold entries. Entries get promoted after getting a hit. 1144 * Eviction happens on addition of new entry. 1145 */ 1146 static SYSCTL_NODE(_vfs_cache, OID_AUTO, neg, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1147 "Name cache negative entry statistics"); 1148 1149 SYSCTL_ULONG(_vfs_cache_neg, OID_AUTO, count, CTLFLAG_RD, &numneg, 0, 1150 "Number of negative cache entries"); 1151 1152 static COUNTER_U64_DEFINE_EARLY(neg_created); 1153 SYSCTL_COUNTER_U64(_vfs_cache_neg, OID_AUTO, created, CTLFLAG_RD, &neg_created, 1154 "Number of created negative entries"); 1155 1156 static COUNTER_U64_DEFINE_EARLY(neg_evicted); 1157 SYSCTL_COUNTER_U64(_vfs_cache_neg, OID_AUTO, evicted, CTLFLAG_RD, &neg_evicted, 1158 "Number of evicted negative entries"); 1159 1160 static COUNTER_U64_DEFINE_EARLY(neg_evict_skipped_empty); 1161 SYSCTL_COUNTER_U64(_vfs_cache_neg, OID_AUTO, evict_skipped_empty, CTLFLAG_RD, 1162 &neg_evict_skipped_empty, 1163 "Number of times evicting failed due to lack of entries"); 1164 1165 static COUNTER_U64_DEFINE_EARLY(neg_evict_skipped_missed); 1166 SYSCTL_COUNTER_U64(_vfs_cache_neg, OID_AUTO, evict_skipped_missed, CTLFLAG_RD, 1167 &neg_evict_skipped_missed, 1168 "Number of times evicting failed due to target entry disappearing"); 1169 1170 static COUNTER_U64_DEFINE_EARLY(neg_evict_skipped_contended); 1171 SYSCTL_COUNTER_U64(_vfs_cache_neg, OID_AUTO, evict_skipped_contended, CTLFLAG_RD, 1172 &neg_evict_skipped_contended, 1173 "Number of times evicting failed due to contention"); 1174 1175 SYSCTL_COUNTER_U64(_vfs_cache_neg, OID_AUTO, hits, CTLFLAG_RD, &numneghits, 1176 "Number of cache hits (negative)"); 1177 1178 static int 1179 sysctl_neg_hot(SYSCTL_HANDLER_ARGS) 1180 { 1181 int i, out; 1182 1183 out = 0; 1184 for (i = 0; i < numneglists; i++) 1185 out += neglists[i].nl_hotnum; 1186 1187 return (SYSCTL_OUT(req, &out, sizeof(out))); 1188 } 1189 SYSCTL_PROC(_vfs_cache_neg, OID_AUTO, hot, CTLTYPE_INT | CTLFLAG_RD | 1190 CTLFLAG_MPSAFE, 0, 0, sysctl_neg_hot, "I", 1191 "Number of hot negative entries"); 1192 1193 static void 1194 cache_neg_init(struct namecache *ncp) 1195 { 1196 struct negstate *ns; 1197 1198 ncp->nc_flag |= NCF_NEGATIVE; 1199 ns = NCP2NEGSTATE(ncp); 1200 ns->neg_flag = 0; 1201 ns->neg_hit = 0; 1202 counter_u64_add(neg_created, 1); 1203 } 1204 1205 #define CACHE_NEG_PROMOTION_THRESH 2 1206 1207 static bool 1208 cache_neg_hit_prep(struct namecache *ncp) 1209 { 1210 struct negstate *ns; 1211 u_char n; 1212 1213 ns = NCP2NEGSTATE(ncp); 1214 n = atomic_load_char(&ns->neg_hit); 1215 for (;;) { 1216 if (n >= CACHE_NEG_PROMOTION_THRESH) 1217 return (false); 1218 if (atomic_fcmpset_8(&ns->neg_hit, &n, n + 1)) 1219 break; 1220 } 1221 return (n + 1 == CACHE_NEG_PROMOTION_THRESH); 1222 } 1223 1224 /* 1225 * Nothing to do here but it is provided for completeness as some 1226 * cache_neg_hit_prep callers may end up returning without even 1227 * trying to promote. 1228 */ 1229 #define cache_neg_hit_abort(ncp) do { } while (0) 1230 1231 static void 1232 cache_neg_hit_finish(struct namecache *ncp) 1233 { 1234 1235 SDT_PROBE2(vfs, namecache, lookup, hit__negative, ncp->nc_dvp, ncp->nc_name); 1236 counter_u64_add(numneghits, 1); 1237 } 1238 1239 /* 1240 * Move a negative entry to the hot list. 1241 */ 1242 static void 1243 cache_neg_promote_locked(struct namecache *ncp) 1244 { 1245 struct neglist *nl; 1246 struct negstate *ns; 1247 1248 ns = NCP2NEGSTATE(ncp); 1249 nl = NCP2NEGLIST(ncp); 1250 mtx_assert(&nl->nl_lock, MA_OWNED); 1251 if ((ns->neg_flag & NEG_HOT) == 0) { 1252 TAILQ_REMOVE(&nl->nl_list, ncp, nc_dst); 1253 TAILQ_INSERT_TAIL(&nl->nl_hotlist, ncp, nc_dst); 1254 nl->nl_hotnum++; 1255 ns->neg_flag |= NEG_HOT; 1256 } 1257 } 1258 1259 /* 1260 * Move a hot negative entry to the cold list. 1261 */ 1262 static void 1263 cache_neg_demote_locked(struct namecache *ncp) 1264 { 1265 struct neglist *nl; 1266 struct negstate *ns; 1267 1268 ns = NCP2NEGSTATE(ncp); 1269 nl = NCP2NEGLIST(ncp); 1270 mtx_assert(&nl->nl_lock, MA_OWNED); 1271 MPASS(ns->neg_flag & NEG_HOT); 1272 TAILQ_REMOVE(&nl->nl_hotlist, ncp, nc_dst); 1273 TAILQ_INSERT_TAIL(&nl->nl_list, ncp, nc_dst); 1274 nl->nl_hotnum--; 1275 ns->neg_flag &= ~NEG_HOT; 1276 atomic_store_char(&ns->neg_hit, 0); 1277 } 1278 1279 /* 1280 * Move a negative entry to the hot list if it matches the lookup. 1281 * 1282 * We have to take locks, but they may be contended and in the worst 1283 * case we may need to go off CPU. We don't want to spin within the 1284 * smr section and we can't block with it. Exiting the section means 1285 * the found entry could have been evicted. We are going to look it 1286 * up again. 1287 */ 1288 static bool 1289 cache_neg_promote_cond(struct vnode *dvp, struct componentname *cnp, 1290 struct namecache *oncp, uint32_t hash) 1291 { 1292 struct namecache *ncp; 1293 struct neglist *nl; 1294 u_char nc_flag; 1295 1296 nl = NCP2NEGLIST(oncp); 1297 1298 mtx_lock(&nl->nl_lock); 1299 /* 1300 * For hash iteration. 1301 */ 1302 vfs_smr_enter(); 1303 1304 /* 1305 * Avoid all surprises by only succeeding if we got the same entry and 1306 * bailing completely otherwise. 1307 * XXX There are no provisions to keep the vnode around, meaning we may 1308 * end up promoting a negative entry for a *new* vnode and returning 1309 * ENOENT on its account. This is the error we want to return anyway 1310 * and promotion is harmless. 1311 * 1312 * In particular at this point there can be a new ncp which matches the 1313 * search but hashes to a different neglist. 1314 */ 1315 CK_SLIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) { 1316 if (ncp == oncp) 1317 break; 1318 } 1319 1320 /* 1321 * No match to begin with. 1322 */ 1323 if (__predict_false(ncp == NULL)) { 1324 goto out_abort; 1325 } 1326 1327 /* 1328 * The newly found entry may be something different... 1329 */ 1330 if (!(ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen && 1331 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))) { 1332 goto out_abort; 1333 } 1334 1335 /* 1336 * ... and not even negative. 1337 */ 1338 nc_flag = atomic_load_char(&ncp->nc_flag); 1339 if ((nc_flag & NCF_NEGATIVE) == 0) { 1340 goto out_abort; 1341 } 1342 1343 if (!cache_ncp_canuse(ncp)) { 1344 goto out_abort; 1345 } 1346 1347 cache_neg_promote_locked(ncp); 1348 cache_neg_hit_finish(ncp); 1349 vfs_smr_exit(); 1350 mtx_unlock(&nl->nl_lock); 1351 return (true); 1352 out_abort: 1353 vfs_smr_exit(); 1354 mtx_unlock(&nl->nl_lock); 1355 return (false); 1356 } 1357 1358 static void 1359 cache_neg_promote(struct namecache *ncp) 1360 { 1361 struct neglist *nl; 1362 1363 nl = NCP2NEGLIST(ncp); 1364 mtx_lock(&nl->nl_lock); 1365 cache_neg_promote_locked(ncp); 1366 mtx_unlock(&nl->nl_lock); 1367 } 1368 1369 static void 1370 cache_neg_insert(struct namecache *ncp) 1371 { 1372 struct neglist *nl; 1373 1374 MPASS(ncp->nc_flag & NCF_NEGATIVE); 1375 cache_assert_bucket_locked(ncp); 1376 nl = NCP2NEGLIST(ncp); 1377 mtx_lock(&nl->nl_lock); 1378 TAILQ_INSERT_TAIL(&nl->nl_list, ncp, nc_dst); 1379 mtx_unlock(&nl->nl_lock); 1380 atomic_add_long(&numneg, 1); 1381 } 1382 1383 static void 1384 cache_neg_remove(struct namecache *ncp) 1385 { 1386 struct neglist *nl; 1387 struct negstate *ns; 1388 1389 cache_assert_bucket_locked(ncp); 1390 nl = NCP2NEGLIST(ncp); 1391 ns = NCP2NEGSTATE(ncp); 1392 mtx_lock(&nl->nl_lock); 1393 if ((ns->neg_flag & NEG_HOT) != 0) { 1394 TAILQ_REMOVE(&nl->nl_hotlist, ncp, nc_dst); 1395 nl->nl_hotnum--; 1396 } else { 1397 TAILQ_REMOVE(&nl->nl_list, ncp, nc_dst); 1398 } 1399 mtx_unlock(&nl->nl_lock); 1400 atomic_subtract_long(&numneg, 1); 1401 } 1402 1403 static struct neglist * 1404 cache_neg_evict_select_list(void) 1405 { 1406 struct neglist *nl; 1407 u_int c; 1408 1409 c = atomic_fetchadd_int(&neg_cycle, 1) + 1; 1410 nl = &neglists[c % numneglists]; 1411 if (!mtx_trylock(&nl->nl_evict_lock)) { 1412 counter_u64_add(neg_evict_skipped_contended, 1); 1413 return (NULL); 1414 } 1415 return (nl); 1416 } 1417 1418 static struct namecache * 1419 cache_neg_evict_select_entry(struct neglist *nl) 1420 { 1421 struct namecache *ncp, *lncp; 1422 struct negstate *ns, *lns; 1423 int i; 1424 1425 mtx_assert(&nl->nl_evict_lock, MA_OWNED); 1426 mtx_assert(&nl->nl_lock, MA_OWNED); 1427 ncp = TAILQ_FIRST(&nl->nl_list); 1428 if (ncp == NULL) 1429 return (NULL); 1430 lncp = ncp; 1431 lns = NCP2NEGSTATE(lncp); 1432 for (i = 1; i < 4; i++) { 1433 ncp = TAILQ_NEXT(ncp, nc_dst); 1434 if (ncp == NULL) 1435 break; 1436 ns = NCP2NEGSTATE(ncp); 1437 if (ns->neg_hit < lns->neg_hit) { 1438 lncp = ncp; 1439 lns = ns; 1440 } 1441 } 1442 return (lncp); 1443 } 1444 1445 static bool 1446 cache_neg_evict(void) 1447 { 1448 struct namecache *ncp, *ncp2; 1449 struct neglist *nl; 1450 struct vnode *dvp; 1451 struct mtx *dvlp; 1452 struct mtx *blp; 1453 uint32_t hash; 1454 u_char nlen; 1455 bool evicted; 1456 1457 nl = cache_neg_evict_select_list(); 1458 if (nl == NULL) { 1459 return (false); 1460 } 1461 1462 mtx_lock(&nl->nl_lock); 1463 ncp = TAILQ_FIRST(&nl->nl_hotlist); 1464 if (ncp != NULL) { 1465 cache_neg_demote_locked(ncp); 1466 } 1467 ncp = cache_neg_evict_select_entry(nl); 1468 if (ncp == NULL) { 1469 counter_u64_add(neg_evict_skipped_empty, 1); 1470 mtx_unlock(&nl->nl_lock); 1471 mtx_unlock(&nl->nl_evict_lock); 1472 return (false); 1473 } 1474 nlen = ncp->nc_nlen; 1475 dvp = ncp->nc_dvp; 1476 hash = cache_get_hash(ncp->nc_name, nlen, dvp); 1477 dvlp = VP2VNODELOCK(dvp); 1478 blp = HASH2BUCKETLOCK(hash); 1479 mtx_unlock(&nl->nl_lock); 1480 mtx_unlock(&nl->nl_evict_lock); 1481 mtx_lock(dvlp); 1482 mtx_lock(blp); 1483 /* 1484 * Note that since all locks were dropped above, the entry may be 1485 * gone or reallocated to be something else. 1486 */ 1487 CK_SLIST_FOREACH(ncp2, (NCHHASH(hash)), nc_hash) { 1488 if (ncp2 == ncp && ncp2->nc_dvp == dvp && 1489 ncp2->nc_nlen == nlen && (ncp2->nc_flag & NCF_NEGATIVE) != 0) 1490 break; 1491 } 1492 if (ncp2 == NULL) { 1493 counter_u64_add(neg_evict_skipped_missed, 1); 1494 ncp = NULL; 1495 evicted = false; 1496 } else { 1497 MPASS(dvlp == VP2VNODELOCK(ncp->nc_dvp)); 1498 MPASS(blp == NCP2BUCKETLOCK(ncp)); 1499 SDT_PROBE2(vfs, namecache, evict_negative, done, ncp->nc_dvp, 1500 ncp->nc_name); 1501 cache_zap_locked(ncp); 1502 counter_u64_add(neg_evicted, 1); 1503 evicted = true; 1504 } 1505 mtx_unlock(blp); 1506 mtx_unlock(dvlp); 1507 if (ncp != NULL) 1508 cache_free(ncp); 1509 return (evicted); 1510 } 1511 1512 /* 1513 * Maybe evict a negative entry to create more room. 1514 * 1515 * The ncnegfactor parameter limits what fraction of the total count 1516 * can comprise of negative entries. However, if the cache is just 1517 * warming up this leads to excessive evictions. As such, ncnegminpct 1518 * (recomputed to neg_min) dictates whether the above should be 1519 * applied. 1520 * 1521 * Try evicting if the cache is close to full capacity regardless of 1522 * other considerations. 1523 */ 1524 static bool 1525 cache_neg_evict_cond(u_long lnumcache) 1526 { 1527 u_long lnumneg; 1528 1529 if (ncsize - 1000 < lnumcache) 1530 goto out_evict; 1531 lnumneg = atomic_load_long(&numneg); 1532 if (lnumneg < neg_min) 1533 return (false); 1534 if (lnumneg * ncnegfactor < lnumcache) 1535 return (false); 1536 out_evict: 1537 return (cache_neg_evict()); 1538 } 1539 1540 /* 1541 * cache_zap_locked(): 1542 * 1543 * Removes a namecache entry from cache, whether it contains an actual 1544 * pointer to a vnode or if it is just a negative cache entry. 1545 */ 1546 static void 1547 cache_zap_locked(struct namecache *ncp) 1548 { 1549 struct nchashhead *ncpp; 1550 struct vnode *dvp, *vp; 1551 1552 dvp = ncp->nc_dvp; 1553 vp = ncp->nc_vp; 1554 1555 if (!(ncp->nc_flag & NCF_NEGATIVE)) 1556 cache_assert_vnode_locked(vp); 1557 cache_assert_vnode_locked(dvp); 1558 cache_assert_bucket_locked(ncp); 1559 1560 cache_ncp_invalidate(ncp); 1561 1562 ncpp = NCP2BUCKET(ncp); 1563 CK_SLIST_REMOVE(ncpp, ncp, namecache, nc_hash); 1564 if (!(ncp->nc_flag & NCF_NEGATIVE)) { 1565 SDT_PROBE3(vfs, namecache, zap, done, dvp, ncp->nc_name, vp); 1566 TAILQ_REMOVE(&vp->v_cache_dst, ncp, nc_dst); 1567 if (ncp == vp->v_cache_dd) { 1568 atomic_store_ptr(&vp->v_cache_dd, NULL); 1569 } 1570 } else { 1571 SDT_PROBE2(vfs, namecache, zap_negative, done, dvp, ncp->nc_name); 1572 cache_neg_remove(ncp); 1573 } 1574 if (ncp->nc_flag & NCF_ISDOTDOT) { 1575 if (ncp == dvp->v_cache_dd) { 1576 atomic_store_ptr(&dvp->v_cache_dd, NULL); 1577 } 1578 } else { 1579 LIST_REMOVE(ncp, nc_src); 1580 if (LIST_EMPTY(&dvp->v_cache_src)) { 1581 ncp->nc_flag |= NCF_DVDROP; 1582 } 1583 } 1584 } 1585 1586 static void 1587 cache_zap_negative_locked_vnode_kl(struct namecache *ncp, struct vnode *vp) 1588 { 1589 struct mtx *blp; 1590 1591 MPASS(ncp->nc_dvp == vp); 1592 MPASS(ncp->nc_flag & NCF_NEGATIVE); 1593 cache_assert_vnode_locked(vp); 1594 1595 blp = NCP2BUCKETLOCK(ncp); 1596 mtx_lock(blp); 1597 cache_zap_locked(ncp); 1598 mtx_unlock(blp); 1599 } 1600 1601 static bool 1602 cache_zap_locked_vnode_kl2(struct namecache *ncp, struct vnode *vp, 1603 struct mtx **vlpp) 1604 { 1605 struct mtx *pvlp, *vlp1, *vlp2, *to_unlock; 1606 struct mtx *blp; 1607 1608 MPASS(vp == ncp->nc_dvp || vp == ncp->nc_vp); 1609 cache_assert_vnode_locked(vp); 1610 1611 if (ncp->nc_flag & NCF_NEGATIVE) { 1612 if (*vlpp != NULL) { 1613 mtx_unlock(*vlpp); 1614 *vlpp = NULL; 1615 } 1616 cache_zap_negative_locked_vnode_kl(ncp, vp); 1617 return (true); 1618 } 1619 1620 pvlp = VP2VNODELOCK(vp); 1621 blp = NCP2BUCKETLOCK(ncp); 1622 vlp1 = VP2VNODELOCK(ncp->nc_dvp); 1623 vlp2 = VP2VNODELOCK(ncp->nc_vp); 1624 1625 if (*vlpp == vlp1 || *vlpp == vlp2) { 1626 to_unlock = *vlpp; 1627 *vlpp = NULL; 1628 } else { 1629 if (*vlpp != NULL) { 1630 mtx_unlock(*vlpp); 1631 *vlpp = NULL; 1632 } 1633 cache_sort_vnodes(&vlp1, &vlp2); 1634 if (vlp1 == pvlp) { 1635 mtx_lock(vlp2); 1636 to_unlock = vlp2; 1637 } else { 1638 if (!mtx_trylock(vlp1)) 1639 goto out_relock; 1640 to_unlock = vlp1; 1641 } 1642 } 1643 mtx_lock(blp); 1644 cache_zap_locked(ncp); 1645 mtx_unlock(blp); 1646 if (to_unlock != NULL) 1647 mtx_unlock(to_unlock); 1648 return (true); 1649 1650 out_relock: 1651 mtx_unlock(vlp2); 1652 mtx_lock(vlp1); 1653 mtx_lock(vlp2); 1654 MPASS(*vlpp == NULL); 1655 *vlpp = vlp1; 1656 return (false); 1657 } 1658 1659 /* 1660 * If trylocking failed we can get here. We know enough to take all needed locks 1661 * in the right order and re-lookup the entry. 1662 */ 1663 static int 1664 cache_zap_unlocked_bucket(struct namecache *ncp, struct componentname *cnp, 1665 struct vnode *dvp, struct mtx *dvlp, struct mtx *vlp, uint32_t hash, 1666 struct mtx *blp) 1667 { 1668 struct namecache *rncp; 1669 1670 cache_assert_bucket_unlocked(ncp); 1671 1672 cache_sort_vnodes(&dvlp, &vlp); 1673 cache_lock_vnodes(dvlp, vlp); 1674 mtx_lock(blp); 1675 CK_SLIST_FOREACH(rncp, (NCHHASH(hash)), nc_hash) { 1676 if (rncp == ncp && rncp->nc_dvp == dvp && 1677 rncp->nc_nlen == cnp->cn_namelen && 1678 !bcmp(rncp->nc_name, cnp->cn_nameptr, rncp->nc_nlen)) 1679 break; 1680 } 1681 if (rncp != NULL) { 1682 cache_zap_locked(rncp); 1683 mtx_unlock(blp); 1684 cache_unlock_vnodes(dvlp, vlp); 1685 counter_u64_add(zap_bucket_relock_success, 1); 1686 return (0); 1687 } 1688 1689 mtx_unlock(blp); 1690 cache_unlock_vnodes(dvlp, vlp); 1691 return (EAGAIN); 1692 } 1693 1694 static int __noinline 1695 cache_zap_locked_bucket(struct namecache *ncp, struct componentname *cnp, 1696 uint32_t hash, struct mtx *blp) 1697 { 1698 struct mtx *dvlp, *vlp; 1699 struct vnode *dvp; 1700 1701 cache_assert_bucket_locked(ncp); 1702 1703 dvlp = VP2VNODELOCK(ncp->nc_dvp); 1704 vlp = NULL; 1705 if (!(ncp->nc_flag & NCF_NEGATIVE)) 1706 vlp = VP2VNODELOCK(ncp->nc_vp); 1707 if (cache_trylock_vnodes(dvlp, vlp) == 0) { 1708 cache_zap_locked(ncp); 1709 mtx_unlock(blp); 1710 cache_unlock_vnodes(dvlp, vlp); 1711 return (0); 1712 } 1713 1714 dvp = ncp->nc_dvp; 1715 mtx_unlock(blp); 1716 return (cache_zap_unlocked_bucket(ncp, cnp, dvp, dvlp, vlp, hash, blp)); 1717 } 1718 1719 static __noinline int 1720 cache_remove_cnp(struct vnode *dvp, struct componentname *cnp) 1721 { 1722 struct namecache *ncp; 1723 struct mtx *blp; 1724 struct mtx *dvlp, *dvlp2; 1725 uint32_t hash; 1726 int error; 1727 1728 if (cnp->cn_namelen == 2 && 1729 cnp->cn_nameptr[0] == '.' && cnp->cn_nameptr[1] == '.') { 1730 dvlp = VP2VNODELOCK(dvp); 1731 dvlp2 = NULL; 1732 mtx_lock(dvlp); 1733 retry_dotdot: 1734 ncp = dvp->v_cache_dd; 1735 if (ncp == NULL) { 1736 mtx_unlock(dvlp); 1737 if (dvlp2 != NULL) 1738 mtx_unlock(dvlp2); 1739 SDT_PROBE2(vfs, namecache, removecnp, miss, dvp, cnp); 1740 return (0); 1741 } 1742 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0) { 1743 if (!cache_zap_locked_vnode_kl2(ncp, dvp, &dvlp2)) 1744 goto retry_dotdot; 1745 MPASS(dvp->v_cache_dd == NULL); 1746 mtx_unlock(dvlp); 1747 if (dvlp2 != NULL) 1748 mtx_unlock(dvlp2); 1749 cache_free(ncp); 1750 } else { 1751 atomic_store_ptr(&dvp->v_cache_dd, NULL); 1752 mtx_unlock(dvlp); 1753 if (dvlp2 != NULL) 1754 mtx_unlock(dvlp2); 1755 } 1756 SDT_PROBE2(vfs, namecache, removecnp, hit, dvp, cnp); 1757 return (1); 1758 } 1759 1760 hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp); 1761 blp = HASH2BUCKETLOCK(hash); 1762 retry: 1763 if (CK_SLIST_EMPTY(NCHHASH(hash))) 1764 goto out_no_entry; 1765 1766 mtx_lock(blp); 1767 1768 CK_SLIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) { 1769 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen && 1770 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen)) 1771 break; 1772 } 1773 1774 if (ncp == NULL) { 1775 mtx_unlock(blp); 1776 goto out_no_entry; 1777 } 1778 1779 error = cache_zap_locked_bucket(ncp, cnp, hash, blp); 1780 if (__predict_false(error != 0)) { 1781 zap_bucket_fail++; 1782 goto retry; 1783 } 1784 counter_u64_add(numposzaps, 1); 1785 SDT_PROBE2(vfs, namecache, removecnp, hit, dvp, cnp); 1786 cache_free(ncp); 1787 return (1); 1788 out_no_entry: 1789 counter_u64_add(nummisszap, 1); 1790 SDT_PROBE2(vfs, namecache, removecnp, miss, dvp, cnp); 1791 return (0); 1792 } 1793 1794 static int __noinline 1795 cache_lookup_dot(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp, 1796 struct timespec *tsp, int *ticksp) 1797 { 1798 int ltype; 1799 1800 *vpp = dvp; 1801 counter_u64_add(dothits, 1); 1802 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ".", *vpp); 1803 if (tsp != NULL) 1804 timespecclear(tsp); 1805 if (ticksp != NULL) 1806 *ticksp = ticks; 1807 vrefact(*vpp); 1808 /* 1809 * When we lookup "." we still can be asked to lock it 1810 * differently... 1811 */ 1812 ltype = cnp->cn_lkflags & LK_TYPE_MASK; 1813 if (ltype != VOP_ISLOCKED(*vpp)) { 1814 if (ltype == LK_EXCLUSIVE) { 1815 vn_lock(*vpp, LK_UPGRADE | LK_RETRY); 1816 if (VN_IS_DOOMED((*vpp))) { 1817 /* forced unmount */ 1818 vrele(*vpp); 1819 *vpp = NULL; 1820 return (ENOENT); 1821 } 1822 } else 1823 vn_lock(*vpp, LK_DOWNGRADE | LK_RETRY); 1824 } 1825 return (-1); 1826 } 1827 1828 static int __noinline 1829 cache_lookup_dotdot(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp, 1830 struct timespec *tsp, int *ticksp) 1831 { 1832 struct namecache_ts *ncp_ts; 1833 struct namecache *ncp; 1834 struct mtx *dvlp; 1835 enum vgetstate vs; 1836 int error, ltype; 1837 bool whiteout; 1838 1839 MPASS((cnp->cn_flags & ISDOTDOT) != 0); 1840 1841 if ((cnp->cn_flags & MAKEENTRY) == 0) { 1842 cache_remove_cnp(dvp, cnp); 1843 return (0); 1844 } 1845 1846 counter_u64_add(dotdothits, 1); 1847 retry: 1848 dvlp = VP2VNODELOCK(dvp); 1849 mtx_lock(dvlp); 1850 ncp = dvp->v_cache_dd; 1851 if (ncp == NULL) { 1852 SDT_PROBE2(vfs, namecache, lookup, miss, dvp, ".."); 1853 mtx_unlock(dvlp); 1854 return (0); 1855 } 1856 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0) { 1857 if (ncp->nc_flag & NCF_NEGATIVE) 1858 *vpp = NULL; 1859 else 1860 *vpp = ncp->nc_vp; 1861 } else 1862 *vpp = ncp->nc_dvp; 1863 if (*vpp == NULL) 1864 goto negative_success; 1865 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, "..", *vpp); 1866 cache_out_ts(ncp, tsp, ticksp); 1867 if ((ncp->nc_flag & (NCF_ISDOTDOT | NCF_DTS)) == 1868 NCF_DTS && tsp != NULL) { 1869 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc); 1870 *tsp = ncp_ts->nc_dotdottime; 1871 } 1872 1873 MPASS(dvp != *vpp); 1874 ltype = VOP_ISLOCKED(dvp); 1875 VOP_UNLOCK(dvp); 1876 vs = vget_prep(*vpp); 1877 mtx_unlock(dvlp); 1878 error = vget_finish(*vpp, cnp->cn_lkflags, vs); 1879 vn_lock(dvp, ltype | LK_RETRY); 1880 if (VN_IS_DOOMED(dvp)) { 1881 if (error == 0) 1882 vput(*vpp); 1883 *vpp = NULL; 1884 return (ENOENT); 1885 } 1886 if (error) { 1887 *vpp = NULL; 1888 goto retry; 1889 } 1890 return (-1); 1891 negative_success: 1892 if (__predict_false(cnp->cn_nameiop == CREATE)) { 1893 if (cnp->cn_flags & ISLASTCN) { 1894 counter_u64_add(numnegzaps, 1); 1895 cache_zap_negative_locked_vnode_kl(ncp, dvp); 1896 mtx_unlock(dvlp); 1897 cache_free(ncp); 1898 return (0); 1899 } 1900 } 1901 1902 whiteout = (ncp->nc_flag & NCF_WHITE); 1903 cache_out_ts(ncp, tsp, ticksp); 1904 if (cache_neg_hit_prep(ncp)) 1905 cache_neg_promote(ncp); 1906 else 1907 cache_neg_hit_finish(ncp); 1908 mtx_unlock(dvlp); 1909 if (whiteout) 1910 cnp->cn_flags |= ISWHITEOUT; 1911 return (ENOENT); 1912 } 1913 1914 /** 1915 * Lookup a name in the name cache 1916 * 1917 * # Arguments 1918 * 1919 * - dvp: Parent directory in which to search. 1920 * - vpp: Return argument. Will contain desired vnode on cache hit. 1921 * - cnp: Parameters of the name search. The most interesting bits of 1922 * the cn_flags field have the following meanings: 1923 * - MAKEENTRY: If clear, free an entry from the cache rather than look 1924 * it up. 1925 * - ISDOTDOT: Must be set if and only if cn_nameptr == ".." 1926 * - tsp: Return storage for cache timestamp. On a successful (positive 1927 * or negative) lookup, tsp will be filled with any timespec that 1928 * was stored when this cache entry was created. However, it will 1929 * be clear for "." entries. 1930 * - ticks: Return storage for alternate cache timestamp. On a successful 1931 * (positive or negative) lookup, it will contain the ticks value 1932 * that was current when the cache entry was created, unless cnp 1933 * was ".". 1934 * 1935 * Either both tsp and ticks have to be provided or neither of them. 1936 * 1937 * # Returns 1938 * 1939 * - -1: A positive cache hit. vpp will contain the desired vnode. 1940 * - ENOENT: A negative cache hit, or dvp was recycled out from under us due 1941 * to a forced unmount. vpp will not be modified. If the entry 1942 * is a whiteout, then the ISWHITEOUT flag will be set in 1943 * cnp->cn_flags. 1944 * - 0: A cache miss. vpp will not be modified. 1945 * 1946 * # Locking 1947 * 1948 * On a cache hit, vpp will be returned locked and ref'd. If we're looking up 1949 * .., dvp is unlocked. If we're looking up . an extra ref is taken, but the 1950 * lock is not recursively acquired. 1951 */ 1952 static int __noinline 1953 cache_lookup_fallback(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp, 1954 struct timespec *tsp, int *ticksp) 1955 { 1956 struct namecache *ncp; 1957 struct mtx *blp; 1958 uint32_t hash; 1959 enum vgetstate vs; 1960 int error; 1961 bool whiteout; 1962 1963 MPASS((cnp->cn_flags & ISDOTDOT) == 0); 1964 MPASS((cnp->cn_flags & (MAKEENTRY | NC_KEEPPOSENTRY)) != 0); 1965 1966 retry: 1967 hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp); 1968 blp = HASH2BUCKETLOCK(hash); 1969 mtx_lock(blp); 1970 1971 CK_SLIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) { 1972 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen && 1973 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen)) 1974 break; 1975 } 1976 1977 if (__predict_false(ncp == NULL)) { 1978 mtx_unlock(blp); 1979 SDT_PROBE2(vfs, namecache, lookup, miss, dvp, cnp->cn_nameptr); 1980 counter_u64_add(nummiss, 1); 1981 return (0); 1982 } 1983 1984 if (ncp->nc_flag & NCF_NEGATIVE) 1985 goto negative_success; 1986 1987 counter_u64_add(numposhits, 1); 1988 *vpp = ncp->nc_vp; 1989 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ncp->nc_name, *vpp); 1990 cache_out_ts(ncp, tsp, ticksp); 1991 MPASS(dvp != *vpp); 1992 vs = vget_prep(*vpp); 1993 mtx_unlock(blp); 1994 error = vget_finish(*vpp, cnp->cn_lkflags, vs); 1995 if (error) { 1996 *vpp = NULL; 1997 goto retry; 1998 } 1999 return (-1); 2000 negative_success: 2001 /* 2002 * We don't get here with regular lookup apart from corner cases. 2003 */ 2004 if (__predict_true(cnp->cn_nameiop == CREATE)) { 2005 if (cnp->cn_flags & ISLASTCN) { 2006 counter_u64_add(numnegzaps, 1); 2007 error = cache_zap_locked_bucket(ncp, cnp, hash, blp); 2008 if (__predict_false(error != 0)) { 2009 zap_bucket_fail2++; 2010 goto retry; 2011 } 2012 cache_free(ncp); 2013 return (0); 2014 } 2015 } 2016 2017 whiteout = (ncp->nc_flag & NCF_WHITE); 2018 cache_out_ts(ncp, tsp, ticksp); 2019 if (cache_neg_hit_prep(ncp)) 2020 cache_neg_promote(ncp); 2021 else 2022 cache_neg_hit_finish(ncp); 2023 mtx_unlock(blp); 2024 if (whiteout) 2025 cnp->cn_flags |= ISWHITEOUT; 2026 return (ENOENT); 2027 } 2028 2029 int 2030 cache_lookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp, 2031 struct timespec *tsp, int *ticksp) 2032 { 2033 struct namecache *ncp; 2034 uint32_t hash; 2035 enum vgetstate vs; 2036 int error; 2037 bool whiteout, neg_promote; 2038 u_short nc_flag; 2039 2040 MPASS((tsp == NULL && ticksp == NULL) || (tsp != NULL && ticksp != NULL)); 2041 2042 #ifdef DEBUG_CACHE 2043 if (__predict_false(!doingcache)) { 2044 cnp->cn_flags &= ~MAKEENTRY; 2045 return (0); 2046 } 2047 #endif 2048 2049 if (__predict_false(cnp->cn_nameptr[0] == '.')) { 2050 if (cnp->cn_namelen == 1) 2051 return (cache_lookup_dot(dvp, vpp, cnp, tsp, ticksp)); 2052 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') 2053 return (cache_lookup_dotdot(dvp, vpp, cnp, tsp, ticksp)); 2054 } 2055 2056 MPASS((cnp->cn_flags & ISDOTDOT) == 0); 2057 2058 if ((cnp->cn_flags & (MAKEENTRY | NC_KEEPPOSENTRY)) == 0) { 2059 cache_remove_cnp(dvp, cnp); 2060 return (0); 2061 } 2062 2063 hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp); 2064 vfs_smr_enter(); 2065 2066 CK_SLIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) { 2067 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen && 2068 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen)) 2069 break; 2070 } 2071 2072 if (__predict_false(ncp == NULL)) { 2073 vfs_smr_exit(); 2074 SDT_PROBE2(vfs, namecache, lookup, miss, dvp, cnp->cn_nameptr); 2075 counter_u64_add(nummiss, 1); 2076 return (0); 2077 } 2078 2079 nc_flag = atomic_load_char(&ncp->nc_flag); 2080 if (nc_flag & NCF_NEGATIVE) 2081 goto negative_success; 2082 2083 counter_u64_add(numposhits, 1); 2084 *vpp = ncp->nc_vp; 2085 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ncp->nc_name, *vpp); 2086 cache_out_ts(ncp, tsp, ticksp); 2087 MPASS(dvp != *vpp); 2088 if (!cache_ncp_canuse(ncp)) { 2089 vfs_smr_exit(); 2090 *vpp = NULL; 2091 goto out_fallback; 2092 } 2093 vs = vget_prep_smr(*vpp); 2094 vfs_smr_exit(); 2095 if (__predict_false(vs == VGET_NONE)) { 2096 *vpp = NULL; 2097 goto out_fallback; 2098 } 2099 error = vget_finish(*vpp, cnp->cn_lkflags, vs); 2100 if (error) { 2101 *vpp = NULL; 2102 goto out_fallback; 2103 } 2104 return (-1); 2105 negative_success: 2106 if (cnp->cn_nameiop == CREATE) { 2107 if (cnp->cn_flags & ISLASTCN) { 2108 vfs_smr_exit(); 2109 goto out_fallback; 2110 } 2111 } 2112 2113 cache_out_ts(ncp, tsp, ticksp); 2114 whiteout = (atomic_load_char(&ncp->nc_flag) & NCF_WHITE); 2115 neg_promote = cache_neg_hit_prep(ncp); 2116 if (!cache_ncp_canuse(ncp)) { 2117 cache_neg_hit_abort(ncp); 2118 vfs_smr_exit(); 2119 goto out_fallback; 2120 } 2121 if (neg_promote) { 2122 vfs_smr_exit(); 2123 if (!cache_neg_promote_cond(dvp, cnp, ncp, hash)) 2124 goto out_fallback; 2125 } else { 2126 cache_neg_hit_finish(ncp); 2127 vfs_smr_exit(); 2128 } 2129 if (whiteout) 2130 cnp->cn_flags |= ISWHITEOUT; 2131 return (ENOENT); 2132 out_fallback: 2133 return (cache_lookup_fallback(dvp, vpp, cnp, tsp, ticksp)); 2134 } 2135 2136 struct celockstate { 2137 struct mtx *vlp[3]; 2138 struct mtx *blp[2]; 2139 }; 2140 CTASSERT((nitems(((struct celockstate *)0)->vlp) == 3)); 2141 CTASSERT((nitems(((struct celockstate *)0)->blp) == 2)); 2142 2143 static inline void 2144 cache_celockstate_init(struct celockstate *cel) 2145 { 2146 2147 bzero(cel, sizeof(*cel)); 2148 } 2149 2150 static void 2151 cache_lock_vnodes_cel(struct celockstate *cel, struct vnode *vp, 2152 struct vnode *dvp) 2153 { 2154 struct mtx *vlp1, *vlp2; 2155 2156 MPASS(cel->vlp[0] == NULL); 2157 MPASS(cel->vlp[1] == NULL); 2158 MPASS(cel->vlp[2] == NULL); 2159 2160 MPASS(vp != NULL || dvp != NULL); 2161 2162 vlp1 = VP2VNODELOCK(vp); 2163 vlp2 = VP2VNODELOCK(dvp); 2164 cache_sort_vnodes(&vlp1, &vlp2); 2165 2166 if (vlp1 != NULL) { 2167 mtx_lock(vlp1); 2168 cel->vlp[0] = vlp1; 2169 } 2170 mtx_lock(vlp2); 2171 cel->vlp[1] = vlp2; 2172 } 2173 2174 static void 2175 cache_unlock_vnodes_cel(struct celockstate *cel) 2176 { 2177 2178 MPASS(cel->vlp[0] != NULL || cel->vlp[1] != NULL); 2179 2180 if (cel->vlp[0] != NULL) 2181 mtx_unlock(cel->vlp[0]); 2182 if (cel->vlp[1] != NULL) 2183 mtx_unlock(cel->vlp[1]); 2184 if (cel->vlp[2] != NULL) 2185 mtx_unlock(cel->vlp[2]); 2186 } 2187 2188 static bool 2189 cache_lock_vnodes_cel_3(struct celockstate *cel, struct vnode *vp) 2190 { 2191 struct mtx *vlp; 2192 bool ret; 2193 2194 cache_assert_vlp_locked(cel->vlp[0]); 2195 cache_assert_vlp_locked(cel->vlp[1]); 2196 MPASS(cel->vlp[2] == NULL); 2197 2198 MPASS(vp != NULL); 2199 vlp = VP2VNODELOCK(vp); 2200 2201 ret = true; 2202 if (vlp >= cel->vlp[1]) { 2203 mtx_lock(vlp); 2204 } else { 2205 if (mtx_trylock(vlp)) 2206 goto out; 2207 cache_lock_vnodes_cel_3_failures++; 2208 cache_unlock_vnodes_cel(cel); 2209 if (vlp < cel->vlp[0]) { 2210 mtx_lock(vlp); 2211 mtx_lock(cel->vlp[0]); 2212 mtx_lock(cel->vlp[1]); 2213 } else { 2214 if (cel->vlp[0] != NULL) 2215 mtx_lock(cel->vlp[0]); 2216 mtx_lock(vlp); 2217 mtx_lock(cel->vlp[1]); 2218 } 2219 ret = false; 2220 } 2221 out: 2222 cel->vlp[2] = vlp; 2223 return (ret); 2224 } 2225 2226 static void 2227 cache_lock_buckets_cel(struct celockstate *cel, struct mtx *blp1, 2228 struct mtx *blp2) 2229 { 2230 2231 MPASS(cel->blp[0] == NULL); 2232 MPASS(cel->blp[1] == NULL); 2233 2234 cache_sort_vnodes(&blp1, &blp2); 2235 2236 if (blp1 != NULL) { 2237 mtx_lock(blp1); 2238 cel->blp[0] = blp1; 2239 } 2240 mtx_lock(blp2); 2241 cel->blp[1] = blp2; 2242 } 2243 2244 static void 2245 cache_unlock_buckets_cel(struct celockstate *cel) 2246 { 2247 2248 if (cel->blp[0] != NULL) 2249 mtx_unlock(cel->blp[0]); 2250 mtx_unlock(cel->blp[1]); 2251 } 2252 2253 /* 2254 * Lock part of the cache affected by the insertion. 2255 * 2256 * This means vnodelocks for dvp, vp and the relevant bucketlock. 2257 * However, insertion can result in removal of an old entry. In this 2258 * case we have an additional vnode and bucketlock pair to lock. 2259 * 2260 * That is, in the worst case we have to lock 3 vnodes and 2 bucketlocks, while 2261 * preserving the locking order (smaller address first). 2262 */ 2263 static void 2264 cache_enter_lock(struct celockstate *cel, struct vnode *dvp, struct vnode *vp, 2265 uint32_t hash) 2266 { 2267 struct namecache *ncp; 2268 struct mtx *blps[2]; 2269 u_char nc_flag; 2270 2271 blps[0] = HASH2BUCKETLOCK(hash); 2272 for (;;) { 2273 blps[1] = NULL; 2274 cache_lock_vnodes_cel(cel, dvp, vp); 2275 if (vp == NULL || vp->v_type != VDIR) 2276 break; 2277 ncp = atomic_load_consume_ptr(&vp->v_cache_dd); 2278 if (ncp == NULL) 2279 break; 2280 nc_flag = atomic_load_char(&ncp->nc_flag); 2281 if ((nc_flag & NCF_ISDOTDOT) == 0) 2282 break; 2283 MPASS(ncp->nc_dvp == vp); 2284 blps[1] = NCP2BUCKETLOCK(ncp); 2285 if ((nc_flag & NCF_NEGATIVE) != 0) 2286 break; 2287 if (cache_lock_vnodes_cel_3(cel, ncp->nc_vp)) 2288 break; 2289 /* 2290 * All vnodes got re-locked. Re-validate the state and if 2291 * nothing changed we are done. Otherwise restart. 2292 */ 2293 if (ncp == vp->v_cache_dd && 2294 (ncp->nc_flag & NCF_ISDOTDOT) != 0 && 2295 blps[1] == NCP2BUCKETLOCK(ncp) && 2296 VP2VNODELOCK(ncp->nc_vp) == cel->vlp[2]) 2297 break; 2298 cache_unlock_vnodes_cel(cel); 2299 cel->vlp[0] = NULL; 2300 cel->vlp[1] = NULL; 2301 cel->vlp[2] = NULL; 2302 } 2303 cache_lock_buckets_cel(cel, blps[0], blps[1]); 2304 } 2305 2306 static void 2307 cache_enter_lock_dd(struct celockstate *cel, struct vnode *dvp, struct vnode *vp, 2308 uint32_t hash) 2309 { 2310 struct namecache *ncp; 2311 struct mtx *blps[2]; 2312 u_char nc_flag; 2313 2314 blps[0] = HASH2BUCKETLOCK(hash); 2315 for (;;) { 2316 blps[1] = NULL; 2317 cache_lock_vnodes_cel(cel, dvp, vp); 2318 ncp = atomic_load_consume_ptr(&dvp->v_cache_dd); 2319 if (ncp == NULL) 2320 break; 2321 nc_flag = atomic_load_char(&ncp->nc_flag); 2322 if ((nc_flag & NCF_ISDOTDOT) == 0) 2323 break; 2324 MPASS(ncp->nc_dvp == dvp); 2325 blps[1] = NCP2BUCKETLOCK(ncp); 2326 if ((nc_flag & NCF_NEGATIVE) != 0) 2327 break; 2328 if (cache_lock_vnodes_cel_3(cel, ncp->nc_vp)) 2329 break; 2330 if (ncp == dvp->v_cache_dd && 2331 (ncp->nc_flag & NCF_ISDOTDOT) != 0 && 2332 blps[1] == NCP2BUCKETLOCK(ncp) && 2333 VP2VNODELOCK(ncp->nc_vp) == cel->vlp[2]) 2334 break; 2335 cache_unlock_vnodes_cel(cel); 2336 cel->vlp[0] = NULL; 2337 cel->vlp[1] = NULL; 2338 cel->vlp[2] = NULL; 2339 } 2340 cache_lock_buckets_cel(cel, blps[0], blps[1]); 2341 } 2342 2343 static void 2344 cache_enter_unlock(struct celockstate *cel) 2345 { 2346 2347 cache_unlock_buckets_cel(cel); 2348 cache_unlock_vnodes_cel(cel); 2349 } 2350 2351 static void __noinline 2352 cache_enter_dotdot_prep(struct vnode *dvp, struct vnode *vp, 2353 struct componentname *cnp) 2354 { 2355 struct celockstate cel; 2356 struct namecache *ncp; 2357 uint32_t hash; 2358 int len; 2359 2360 if (atomic_load_ptr(&dvp->v_cache_dd) == NULL) 2361 return; 2362 len = cnp->cn_namelen; 2363 cache_celockstate_init(&cel); 2364 hash = cache_get_hash(cnp->cn_nameptr, len, dvp); 2365 cache_enter_lock_dd(&cel, dvp, vp, hash); 2366 ncp = dvp->v_cache_dd; 2367 if (ncp != NULL && (ncp->nc_flag & NCF_ISDOTDOT)) { 2368 KASSERT(ncp->nc_dvp == dvp, ("wrong isdotdot parent")); 2369 cache_zap_locked(ncp); 2370 } else { 2371 ncp = NULL; 2372 } 2373 atomic_store_ptr(&dvp->v_cache_dd, NULL); 2374 cache_enter_unlock(&cel); 2375 if (ncp != NULL) 2376 cache_free(ncp); 2377 } 2378 2379 /* 2380 * Add an entry to the cache. 2381 */ 2382 void 2383 cache_enter_time(struct vnode *dvp, struct vnode *vp, struct componentname *cnp, 2384 struct timespec *tsp, struct timespec *dtsp) 2385 { 2386 struct celockstate cel; 2387 struct namecache *ncp, *n2, *ndd; 2388 struct namecache_ts *ncp_ts; 2389 struct nchashhead *ncpp; 2390 uint32_t hash; 2391 int flag; 2392 int len; 2393 2394 KASSERT(cnp->cn_namelen <= NAME_MAX, 2395 ("%s: passed len %ld exceeds NAME_MAX (%d)", __func__, cnp->cn_namelen, 2396 NAME_MAX)); 2397 #ifdef notyet 2398 /* 2399 * Not everything doing this is weeded out yet. 2400 */ 2401 VNPASS(dvp != vp, dvp); 2402 #endif 2403 VNPASS(!VN_IS_DOOMED(dvp), dvp); 2404 VNPASS(dvp->v_type != VNON, dvp); 2405 if (vp != NULL) { 2406 VNPASS(!VN_IS_DOOMED(vp), vp); 2407 VNPASS(vp->v_type != VNON, vp); 2408 } 2409 2410 #ifdef DEBUG_CACHE 2411 if (__predict_false(!doingcache)) 2412 return; 2413 #endif 2414 2415 flag = 0; 2416 if (__predict_false(cnp->cn_nameptr[0] == '.')) { 2417 if (cnp->cn_namelen == 1) 2418 return; 2419 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') { 2420 cache_enter_dotdot_prep(dvp, vp, cnp); 2421 flag = NCF_ISDOTDOT; 2422 } 2423 } 2424 2425 ncp = cache_alloc(cnp->cn_namelen, tsp != NULL); 2426 if (ncp == NULL) 2427 return; 2428 2429 cache_celockstate_init(&cel); 2430 ndd = NULL; 2431 ncp_ts = NULL; 2432 2433 /* 2434 * Calculate the hash key and setup as much of the new 2435 * namecache entry as possible before acquiring the lock. 2436 */ 2437 ncp->nc_flag = flag | NCF_WIP; 2438 ncp->nc_vp = vp; 2439 if (vp == NULL) 2440 cache_neg_init(ncp); 2441 ncp->nc_dvp = dvp; 2442 if (tsp != NULL) { 2443 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc); 2444 ncp_ts->nc_time = *tsp; 2445 ncp_ts->nc_ticks = ticks; 2446 ncp_ts->nc_nc.nc_flag |= NCF_TS; 2447 if (dtsp != NULL) { 2448 ncp_ts->nc_dotdottime = *dtsp; 2449 ncp_ts->nc_nc.nc_flag |= NCF_DTS; 2450 } 2451 } 2452 len = ncp->nc_nlen = cnp->cn_namelen; 2453 hash = cache_get_hash(cnp->cn_nameptr, len, dvp); 2454 memcpy(ncp->nc_name, cnp->cn_nameptr, len); 2455 ncp->nc_name[len] = '\0'; 2456 cache_enter_lock(&cel, dvp, vp, hash); 2457 2458 /* 2459 * See if this vnode or negative entry is already in the cache 2460 * with this name. This can happen with concurrent lookups of 2461 * the same path name. 2462 */ 2463 ncpp = NCHHASH(hash); 2464 CK_SLIST_FOREACH(n2, ncpp, nc_hash) { 2465 if (n2->nc_dvp == dvp && 2466 n2->nc_nlen == cnp->cn_namelen && 2467 !bcmp(n2->nc_name, cnp->cn_nameptr, n2->nc_nlen)) { 2468 MPASS(cache_ncp_canuse(n2)); 2469 if ((n2->nc_flag & NCF_NEGATIVE) != 0) 2470 KASSERT(vp == NULL, 2471 ("%s: found entry pointing to a different vnode (%p != %p)", 2472 __func__, NULL, vp)); 2473 else 2474 KASSERT(n2->nc_vp == vp, 2475 ("%s: found entry pointing to a different vnode (%p != %p)", 2476 __func__, n2->nc_vp, vp)); 2477 /* 2478 * Entries are supposed to be immutable unless in the 2479 * process of getting destroyed. Accommodating for 2480 * changing timestamps is possible but not worth it. 2481 * This should be harmless in terms of correctness, in 2482 * the worst case resulting in an earlier expiration. 2483 * Alternatively, the found entry can be replaced 2484 * altogether. 2485 */ 2486 MPASS((n2->nc_flag & (NCF_TS | NCF_DTS)) == (ncp->nc_flag & (NCF_TS | NCF_DTS))); 2487 #if 0 2488 if (tsp != NULL) { 2489 KASSERT((n2->nc_flag & NCF_TS) != 0, 2490 ("no NCF_TS")); 2491 n2_ts = __containerof(n2, struct namecache_ts, nc_nc); 2492 n2_ts->nc_time = ncp_ts->nc_time; 2493 n2_ts->nc_ticks = ncp_ts->nc_ticks; 2494 if (dtsp != NULL) { 2495 n2_ts->nc_dotdottime = ncp_ts->nc_dotdottime; 2496 n2_ts->nc_nc.nc_flag |= NCF_DTS; 2497 } 2498 } 2499 #endif 2500 SDT_PROBE3(vfs, namecache, enter, duplicate, dvp, ncp->nc_name, 2501 vp); 2502 goto out_unlock_free; 2503 } 2504 } 2505 2506 if (flag == NCF_ISDOTDOT) { 2507 /* 2508 * See if we are trying to add .. entry, but some other lookup 2509 * has populated v_cache_dd pointer already. 2510 */ 2511 if (dvp->v_cache_dd != NULL) 2512 goto out_unlock_free; 2513 KASSERT(vp == NULL || vp->v_type == VDIR, 2514 ("wrong vnode type %p", vp)); 2515 atomic_thread_fence_rel(); 2516 atomic_store_ptr(&dvp->v_cache_dd, ncp); 2517 } 2518 2519 if (vp != NULL) { 2520 if (flag != NCF_ISDOTDOT) { 2521 /* 2522 * For this case, the cache entry maps both the 2523 * directory name in it and the name ".." for the 2524 * directory's parent. 2525 */ 2526 if ((ndd = vp->v_cache_dd) != NULL) { 2527 if ((ndd->nc_flag & NCF_ISDOTDOT) != 0) 2528 cache_zap_locked(ndd); 2529 else 2530 ndd = NULL; 2531 } 2532 atomic_thread_fence_rel(); 2533 atomic_store_ptr(&vp->v_cache_dd, ncp); 2534 } else if (vp->v_type != VDIR) { 2535 if (vp->v_cache_dd != NULL) { 2536 atomic_store_ptr(&vp->v_cache_dd, NULL); 2537 } 2538 } 2539 } 2540 2541 if (flag != NCF_ISDOTDOT) { 2542 if (LIST_EMPTY(&dvp->v_cache_src)) { 2543 cache_hold_vnode(dvp); 2544 } 2545 LIST_INSERT_HEAD(&dvp->v_cache_src, ncp, nc_src); 2546 } 2547 2548 /* 2549 * If the entry is "negative", we place it into the 2550 * "negative" cache queue, otherwise, we place it into the 2551 * destination vnode's cache entries queue. 2552 */ 2553 if (vp != NULL) { 2554 TAILQ_INSERT_HEAD(&vp->v_cache_dst, ncp, nc_dst); 2555 SDT_PROBE3(vfs, namecache, enter, done, dvp, ncp->nc_name, 2556 vp); 2557 } else { 2558 if (cnp->cn_flags & ISWHITEOUT) 2559 atomic_store_char(&ncp->nc_flag, ncp->nc_flag | NCF_WHITE); 2560 cache_neg_insert(ncp); 2561 SDT_PROBE2(vfs, namecache, enter_negative, done, dvp, 2562 ncp->nc_name); 2563 } 2564 2565 /* 2566 * Insert the new namecache entry into the appropriate chain 2567 * within the cache entries table. 2568 */ 2569 CK_SLIST_INSERT_HEAD(ncpp, ncp, nc_hash); 2570 2571 atomic_thread_fence_rel(); 2572 /* 2573 * Mark the entry as fully constructed. 2574 * It is immutable past this point until its removal. 2575 */ 2576 atomic_store_char(&ncp->nc_flag, ncp->nc_flag & ~NCF_WIP); 2577 2578 cache_enter_unlock(&cel); 2579 if (ndd != NULL) 2580 cache_free(ndd); 2581 return; 2582 out_unlock_free: 2583 cache_enter_unlock(&cel); 2584 cache_free(ncp); 2585 return; 2586 } 2587 2588 static u_int 2589 cache_roundup_2(u_int val) 2590 { 2591 u_int res; 2592 2593 for (res = 1; res <= val; res <<= 1) 2594 continue; 2595 2596 return (res); 2597 } 2598 2599 static struct nchashhead * 2600 nchinittbl(u_long elements, u_long *hashmask) 2601 { 2602 struct nchashhead *hashtbl; 2603 u_long hashsize, i; 2604 2605 hashsize = cache_roundup_2(elements) / 2; 2606 2607 hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl), M_VFSCACHE, M_WAITOK); 2608 for (i = 0; i < hashsize; i++) 2609 CK_SLIST_INIT(&hashtbl[i]); 2610 *hashmask = hashsize - 1; 2611 return (hashtbl); 2612 } 2613 2614 static void 2615 ncfreetbl(struct nchashhead *hashtbl) 2616 { 2617 2618 free(hashtbl, M_VFSCACHE); 2619 } 2620 2621 /* 2622 * Name cache initialization, from vfs_init() when we are booting 2623 */ 2624 static void 2625 nchinit(void *dummy __unused) 2626 { 2627 u_int i; 2628 2629 cache_zone_small = uma_zcreate("S VFS Cache", CACHE_ZONE_SMALL_SIZE, 2630 NULL, NULL, NULL, NULL, CACHE_ZONE_ALIGNMENT, UMA_ZONE_ZINIT); 2631 cache_zone_small_ts = uma_zcreate("STS VFS Cache", CACHE_ZONE_SMALL_TS_SIZE, 2632 NULL, NULL, NULL, NULL, CACHE_ZONE_ALIGNMENT, UMA_ZONE_ZINIT); 2633 cache_zone_large = uma_zcreate("L VFS Cache", CACHE_ZONE_LARGE_SIZE, 2634 NULL, NULL, NULL, NULL, CACHE_ZONE_ALIGNMENT, UMA_ZONE_ZINIT); 2635 cache_zone_large_ts = uma_zcreate("LTS VFS Cache", CACHE_ZONE_LARGE_TS_SIZE, 2636 NULL, NULL, NULL, NULL, CACHE_ZONE_ALIGNMENT, UMA_ZONE_ZINIT); 2637 2638 VFS_SMR_ZONE_SET(cache_zone_small); 2639 VFS_SMR_ZONE_SET(cache_zone_small_ts); 2640 VFS_SMR_ZONE_SET(cache_zone_large); 2641 VFS_SMR_ZONE_SET(cache_zone_large_ts); 2642 2643 ncsize = desiredvnodes * ncsizefactor; 2644 cache_recalc_neg_min(ncnegminpct); 2645 nchashtbl = nchinittbl(desiredvnodes * 2, &nchash); 2646 ncbuckethash = cache_roundup_2(mp_ncpus * mp_ncpus) - 1; 2647 if (ncbuckethash < 7) /* arbitrarily chosen to avoid having one lock */ 2648 ncbuckethash = 7; 2649 if (ncbuckethash > nchash) 2650 ncbuckethash = nchash; 2651 bucketlocks = malloc(sizeof(*bucketlocks) * numbucketlocks, M_VFSCACHE, 2652 M_WAITOK | M_ZERO); 2653 for (i = 0; i < numbucketlocks; i++) 2654 mtx_init(&bucketlocks[i], "ncbuc", NULL, MTX_DUPOK | MTX_RECURSE); 2655 ncvnodehash = ncbuckethash; 2656 vnodelocks = malloc(sizeof(*vnodelocks) * numvnodelocks, M_VFSCACHE, 2657 M_WAITOK | M_ZERO); 2658 for (i = 0; i < numvnodelocks; i++) 2659 mtx_init(&vnodelocks[i], "ncvn", NULL, MTX_DUPOK | MTX_RECURSE); 2660 2661 for (i = 0; i < numneglists; i++) { 2662 mtx_init(&neglists[i].nl_evict_lock, "ncnege", NULL, MTX_DEF); 2663 mtx_init(&neglists[i].nl_lock, "ncnegl", NULL, MTX_DEF); 2664 TAILQ_INIT(&neglists[i].nl_list); 2665 TAILQ_INIT(&neglists[i].nl_hotlist); 2666 } 2667 } 2668 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nchinit, NULL); 2669 2670 void 2671 cache_vnode_init(struct vnode *vp) 2672 { 2673 2674 LIST_INIT(&vp->v_cache_src); 2675 TAILQ_INIT(&vp->v_cache_dst); 2676 vp->v_cache_dd = NULL; 2677 cache_prehash(vp); 2678 } 2679 2680 /* 2681 * Induce transient cache misses for lockless operation in cache_lookup() by 2682 * using a temporary hash table. 2683 * 2684 * This will force a fs lookup. 2685 * 2686 * Synchronisation is done in 2 steps, calling vfs_smr_quiesce each time 2687 * to observe all CPUs not performing the lookup. 2688 */ 2689 static void 2690 cache_changesize_set_temp(struct nchashhead *temptbl, u_long temphash) 2691 { 2692 2693 MPASS(temphash < nchash); 2694 /* 2695 * Change the size. The new size is smaller and can safely be used 2696 * against the existing table. All lookups which now hash wrong will 2697 * result in a cache miss, which all callers are supposed to know how 2698 * to handle. 2699 */ 2700 atomic_store_long(&nchash, temphash); 2701 atomic_thread_fence_rel(); 2702 vfs_smr_quiesce(); 2703 /* 2704 * At this point everyone sees the updated hash value, but they still 2705 * see the old table. 2706 */ 2707 atomic_store_ptr(&nchashtbl, temptbl); 2708 atomic_thread_fence_rel(); 2709 vfs_smr_quiesce(); 2710 /* 2711 * At this point everyone sees the updated table pointer and size pair. 2712 */ 2713 } 2714 2715 /* 2716 * Set the new hash table. 2717 * 2718 * Similarly to cache_changesize_set_temp(), this has to synchronize against 2719 * lockless operation in cache_lookup(). 2720 */ 2721 static void 2722 cache_changesize_set_new(struct nchashhead *new_tbl, u_long new_hash) 2723 { 2724 2725 MPASS(nchash < new_hash); 2726 /* 2727 * Change the pointer first. This wont result in out of bounds access 2728 * since the temporary table is guaranteed to be smaller. 2729 */ 2730 atomic_store_ptr(&nchashtbl, new_tbl); 2731 atomic_thread_fence_rel(); 2732 vfs_smr_quiesce(); 2733 /* 2734 * At this point everyone sees the updated pointer value, but they 2735 * still see the old size. 2736 */ 2737 atomic_store_long(&nchash, new_hash); 2738 atomic_thread_fence_rel(); 2739 vfs_smr_quiesce(); 2740 /* 2741 * At this point everyone sees the updated table pointer and size pair. 2742 */ 2743 } 2744 2745 void 2746 cache_changesize(u_long newmaxvnodes) 2747 { 2748 struct nchashhead *new_nchashtbl, *old_nchashtbl, *temptbl; 2749 u_long new_nchash, old_nchash, temphash; 2750 struct namecache *ncp; 2751 uint32_t hash; 2752 u_long newncsize; 2753 int i; 2754 2755 newncsize = newmaxvnodes * ncsizefactor; 2756 newmaxvnodes = cache_roundup_2(newmaxvnodes * 2); 2757 if (newmaxvnodes < numbucketlocks) 2758 newmaxvnodes = numbucketlocks; 2759 2760 new_nchashtbl = nchinittbl(newmaxvnodes, &new_nchash); 2761 /* If same hash table size, nothing to do */ 2762 if (nchash == new_nchash) { 2763 ncfreetbl(new_nchashtbl); 2764 return; 2765 } 2766 2767 temptbl = nchinittbl(1, &temphash); 2768 2769 /* 2770 * Move everything from the old hash table to the new table. 2771 * None of the namecache entries in the table can be removed 2772 * because to do so, they have to be removed from the hash table. 2773 */ 2774 cache_fplookup_lockout(); 2775 cache_lock_all_vnodes(); 2776 cache_lock_all_buckets(); 2777 old_nchashtbl = nchashtbl; 2778 old_nchash = nchash; 2779 cache_changesize_set_temp(temptbl, temphash); 2780 for (i = 0; i <= old_nchash; i++) { 2781 while ((ncp = CK_SLIST_FIRST(&old_nchashtbl[i])) != NULL) { 2782 hash = cache_get_hash(ncp->nc_name, ncp->nc_nlen, 2783 ncp->nc_dvp); 2784 CK_SLIST_REMOVE(&old_nchashtbl[i], ncp, namecache, nc_hash); 2785 CK_SLIST_INSERT_HEAD(&new_nchashtbl[hash & new_nchash], ncp, nc_hash); 2786 } 2787 } 2788 ncsize = newncsize; 2789 cache_recalc_neg_min(ncnegminpct); 2790 cache_changesize_set_new(new_nchashtbl, new_nchash); 2791 cache_unlock_all_buckets(); 2792 cache_unlock_all_vnodes(); 2793 cache_fplookup_restore(); 2794 ncfreetbl(old_nchashtbl); 2795 ncfreetbl(temptbl); 2796 } 2797 2798 /* 2799 * Remove all entries from and to a particular vnode. 2800 */ 2801 static void 2802 cache_purge_impl(struct vnode *vp) 2803 { 2804 struct cache_freebatch batch; 2805 struct namecache *ncp; 2806 struct mtx *vlp, *vlp2; 2807 2808 TAILQ_INIT(&batch); 2809 vlp = VP2VNODELOCK(vp); 2810 vlp2 = NULL; 2811 mtx_lock(vlp); 2812 retry: 2813 while (!LIST_EMPTY(&vp->v_cache_src)) { 2814 ncp = LIST_FIRST(&vp->v_cache_src); 2815 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2)) 2816 goto retry; 2817 TAILQ_INSERT_TAIL(&batch, ncp, nc_dst); 2818 } 2819 while (!TAILQ_EMPTY(&vp->v_cache_dst)) { 2820 ncp = TAILQ_FIRST(&vp->v_cache_dst); 2821 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2)) 2822 goto retry; 2823 TAILQ_INSERT_TAIL(&batch, ncp, nc_dst); 2824 } 2825 ncp = vp->v_cache_dd; 2826 if (ncp != NULL) { 2827 KASSERT(ncp->nc_flag & NCF_ISDOTDOT, 2828 ("lost dotdot link")); 2829 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2)) 2830 goto retry; 2831 TAILQ_INSERT_TAIL(&batch, ncp, nc_dst); 2832 } 2833 KASSERT(vp->v_cache_dd == NULL, ("incomplete purge")); 2834 mtx_unlock(vlp); 2835 if (vlp2 != NULL) 2836 mtx_unlock(vlp2); 2837 cache_free_batch(&batch); 2838 } 2839 2840 /* 2841 * Opportunistic check to see if there is anything to do. 2842 */ 2843 static bool 2844 cache_has_entries(struct vnode *vp) 2845 { 2846 2847 if (LIST_EMPTY(&vp->v_cache_src) && TAILQ_EMPTY(&vp->v_cache_dst) && 2848 atomic_load_ptr(&vp->v_cache_dd) == NULL) 2849 return (false); 2850 return (true); 2851 } 2852 2853 void 2854 cache_purge(struct vnode *vp) 2855 { 2856 2857 SDT_PROBE1(vfs, namecache, purge, done, vp); 2858 if (!cache_has_entries(vp)) 2859 return; 2860 cache_purge_impl(vp); 2861 } 2862 2863 /* 2864 * Only to be used by vgone. 2865 */ 2866 void 2867 cache_purge_vgone(struct vnode *vp) 2868 { 2869 struct mtx *vlp; 2870 2871 VNPASS(VN_IS_DOOMED(vp), vp); 2872 if (cache_has_entries(vp)) { 2873 cache_purge_impl(vp); 2874 return; 2875 } 2876 2877 /* 2878 * Serialize against a potential thread doing cache_purge. 2879 */ 2880 vlp = VP2VNODELOCK(vp); 2881 mtx_wait_unlocked(vlp); 2882 if (cache_has_entries(vp)) { 2883 cache_purge_impl(vp); 2884 return; 2885 } 2886 return; 2887 } 2888 2889 /* 2890 * Remove all negative entries for a particular directory vnode. 2891 */ 2892 void 2893 cache_purge_negative(struct vnode *vp) 2894 { 2895 struct cache_freebatch batch; 2896 struct namecache *ncp, *nnp; 2897 struct mtx *vlp; 2898 2899 SDT_PROBE1(vfs, namecache, purge_negative, done, vp); 2900 if (LIST_EMPTY(&vp->v_cache_src)) 2901 return; 2902 TAILQ_INIT(&batch); 2903 vlp = VP2VNODELOCK(vp); 2904 mtx_lock(vlp); 2905 LIST_FOREACH_SAFE(ncp, &vp->v_cache_src, nc_src, nnp) { 2906 if (!(ncp->nc_flag & NCF_NEGATIVE)) 2907 continue; 2908 cache_zap_negative_locked_vnode_kl(ncp, vp); 2909 TAILQ_INSERT_TAIL(&batch, ncp, nc_dst); 2910 } 2911 mtx_unlock(vlp); 2912 cache_free_batch(&batch); 2913 } 2914 2915 /* 2916 * Entry points for modifying VOP operations. 2917 */ 2918 void 2919 cache_vop_rename(struct vnode *fdvp, struct vnode *fvp, struct vnode *tdvp, 2920 struct vnode *tvp, struct componentname *fcnp, struct componentname *tcnp) 2921 { 2922 2923 ASSERT_VOP_IN_SEQC(fdvp); 2924 ASSERT_VOP_IN_SEQC(fvp); 2925 ASSERT_VOP_IN_SEQC(tdvp); 2926 if (tvp != NULL) 2927 ASSERT_VOP_IN_SEQC(tvp); 2928 2929 cache_purge(fvp); 2930 if (tvp != NULL) { 2931 cache_purge(tvp); 2932 KASSERT(!cache_remove_cnp(tdvp, tcnp), 2933 ("%s: lingering negative entry", __func__)); 2934 } else { 2935 cache_remove_cnp(tdvp, tcnp); 2936 } 2937 2938 /* 2939 * TODO 2940 * 2941 * Historically renaming was always purging all revelang entries, 2942 * but that's quite wasteful. In particular turns out that in many cases 2943 * the target file is immediately accessed after rename, inducing a cache 2944 * miss. 2945 * 2946 * Recode this to reduce relocking and reuse the existing entry (if any) 2947 * instead of just removing it above and allocating a new one here. 2948 */ 2949 if (cache_rename_add) { 2950 cache_enter(tdvp, fvp, tcnp); 2951 } 2952 } 2953 2954 void 2955 cache_vop_rmdir(struct vnode *dvp, struct vnode *vp) 2956 { 2957 2958 ASSERT_VOP_IN_SEQC(dvp); 2959 ASSERT_VOP_IN_SEQC(vp); 2960 cache_purge(vp); 2961 } 2962 2963 #ifdef INVARIANTS 2964 /* 2965 * Validate that if an entry exists it matches. 2966 */ 2967 void 2968 cache_validate(struct vnode *dvp, struct vnode *vp, struct componentname *cnp) 2969 { 2970 struct namecache *ncp; 2971 struct mtx *blp; 2972 uint32_t hash; 2973 2974 hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp); 2975 if (CK_SLIST_EMPTY(NCHHASH(hash))) 2976 return; 2977 blp = HASH2BUCKETLOCK(hash); 2978 mtx_lock(blp); 2979 CK_SLIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) { 2980 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen && 2981 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen)) { 2982 if (ncp->nc_vp != vp) 2983 panic("%s: mismatch (%p != %p); ncp %p [%s] dvp %p\n", 2984 __func__, vp, ncp->nc_vp, ncp, ncp->nc_name, ncp->nc_dvp); 2985 } 2986 } 2987 mtx_unlock(blp); 2988 } 2989 #endif 2990 2991 /* 2992 * Flush all entries referencing a particular filesystem. 2993 */ 2994 void 2995 cache_purgevfs(struct mount *mp) 2996 { 2997 struct vnode *vp, *mvp; 2998 2999 SDT_PROBE1(vfs, namecache, purgevfs, done, mp); 3000 /* 3001 * Somewhat wasteful iteration over all vnodes. Would be better to 3002 * support filtering and avoid the interlock to begin with. 3003 */ 3004 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) { 3005 if (!cache_has_entries(vp)) { 3006 VI_UNLOCK(vp); 3007 continue; 3008 } 3009 vholdl(vp); 3010 VI_UNLOCK(vp); 3011 cache_purge(vp); 3012 vdrop(vp); 3013 } 3014 } 3015 3016 /* 3017 * Perform canonical checks and cache lookup and pass on to filesystem 3018 * through the vop_cachedlookup only if needed. 3019 */ 3020 3021 int 3022 vfs_cache_lookup(struct vop_lookup_args *ap) 3023 { 3024 struct vnode *dvp; 3025 int error; 3026 struct vnode **vpp = ap->a_vpp; 3027 struct componentname *cnp = ap->a_cnp; 3028 int flags = cnp->cn_flags; 3029 3030 *vpp = NULL; 3031 dvp = ap->a_dvp; 3032 3033 if (dvp->v_type != VDIR) 3034 return (ENOTDIR); 3035 3036 if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) && 3037 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) 3038 return (EROFS); 3039 3040 error = vn_dir_check_exec(dvp, cnp); 3041 if (error != 0) 3042 return (error); 3043 3044 error = cache_lookup(dvp, vpp, cnp, NULL, NULL); 3045 if (error == 0) 3046 return (VOP_CACHEDLOOKUP(dvp, vpp, cnp)); 3047 if (error == -1) 3048 return (0); 3049 return (error); 3050 } 3051 3052 /* Implementation of the getcwd syscall. */ 3053 int 3054 sys___getcwd(struct thread *td, struct __getcwd_args *uap) 3055 { 3056 char *buf, *retbuf; 3057 size_t buflen; 3058 int error; 3059 3060 buflen = uap->buflen; 3061 if (__predict_false(buflen < 2)) 3062 return (EINVAL); 3063 if (buflen > MAXPATHLEN) 3064 buflen = MAXPATHLEN; 3065 3066 buf = uma_zalloc(namei_zone, M_WAITOK); 3067 error = vn_getcwd(buf, &retbuf, &buflen); 3068 if (error == 0) 3069 error = copyout(retbuf, uap->buf, buflen); 3070 uma_zfree(namei_zone, buf); 3071 return (error); 3072 } 3073 3074 int 3075 vn_getcwd(char *buf, char **retbuf, size_t *buflen) 3076 { 3077 struct pwd *pwd; 3078 int error; 3079 3080 vfs_smr_enter(); 3081 pwd = pwd_get_smr(); 3082 error = vn_fullpath_any_smr(pwd->pwd_cdir, pwd->pwd_rdir, buf, retbuf, 3083 buflen, 0); 3084 VFS_SMR_ASSERT_NOT_ENTERED(); 3085 if (error < 0) { 3086 pwd = pwd_hold(curthread); 3087 error = vn_fullpath_any(pwd->pwd_cdir, pwd->pwd_rdir, buf, 3088 retbuf, buflen); 3089 pwd_drop(pwd); 3090 } 3091 3092 #ifdef KTRACE 3093 if (KTRPOINT(curthread, KTR_NAMEI) && error == 0) 3094 ktrnamei(*retbuf); 3095 #endif 3096 return (error); 3097 } 3098 3099 static int 3100 kern___realpathat(struct thread *td, int fd, const char *path, char *buf, 3101 size_t size, int flags, enum uio_seg pathseg) 3102 { 3103 struct nameidata nd; 3104 char *retbuf, *freebuf; 3105 int error; 3106 3107 if (flags != 0) 3108 return (EINVAL); 3109 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | SAVENAME | WANTPARENT | AUDITVNODE1, 3110 pathseg, path, fd, &cap_fstat_rights, td); 3111 if ((error = namei(&nd)) != 0) 3112 return (error); 3113 error = vn_fullpath_hardlink(&nd, &retbuf, &freebuf, &size); 3114 if (error == 0) { 3115 error = copyout(retbuf, buf, size); 3116 free(freebuf, M_TEMP); 3117 } 3118 NDFREE(&nd, 0); 3119 return (error); 3120 } 3121 3122 int 3123 sys___realpathat(struct thread *td, struct __realpathat_args *uap) 3124 { 3125 3126 return (kern___realpathat(td, uap->fd, uap->path, uap->buf, uap->size, 3127 uap->flags, UIO_USERSPACE)); 3128 } 3129 3130 /* 3131 * Retrieve the full filesystem path that correspond to a vnode from the name 3132 * cache (if available) 3133 */ 3134 int 3135 vn_fullpath(struct vnode *vp, char **retbuf, char **freebuf) 3136 { 3137 struct pwd *pwd; 3138 char *buf; 3139 size_t buflen; 3140 int error; 3141 3142 if (__predict_false(vp == NULL)) 3143 return (EINVAL); 3144 3145 buflen = MAXPATHLEN; 3146 buf = malloc(buflen, M_TEMP, M_WAITOK); 3147 vfs_smr_enter(); 3148 pwd = pwd_get_smr(); 3149 error = vn_fullpath_any_smr(vp, pwd->pwd_rdir, buf, retbuf, &buflen, 0); 3150 VFS_SMR_ASSERT_NOT_ENTERED(); 3151 if (error < 0) { 3152 pwd = pwd_hold(curthread); 3153 error = vn_fullpath_any(vp, pwd->pwd_rdir, buf, retbuf, &buflen); 3154 pwd_drop(pwd); 3155 } 3156 if (error == 0) 3157 *freebuf = buf; 3158 else 3159 free(buf, M_TEMP); 3160 return (error); 3161 } 3162 3163 /* 3164 * This function is similar to vn_fullpath, but it attempts to lookup the 3165 * pathname relative to the global root mount point. This is required for the 3166 * auditing sub-system, as audited pathnames must be absolute, relative to the 3167 * global root mount point. 3168 */ 3169 int 3170 vn_fullpath_global(struct vnode *vp, char **retbuf, char **freebuf) 3171 { 3172 char *buf; 3173 size_t buflen; 3174 int error; 3175 3176 if (__predict_false(vp == NULL)) 3177 return (EINVAL); 3178 buflen = MAXPATHLEN; 3179 buf = malloc(buflen, M_TEMP, M_WAITOK); 3180 vfs_smr_enter(); 3181 error = vn_fullpath_any_smr(vp, rootvnode, buf, retbuf, &buflen, 0); 3182 VFS_SMR_ASSERT_NOT_ENTERED(); 3183 if (error < 0) { 3184 error = vn_fullpath_any(vp, rootvnode, buf, retbuf, &buflen); 3185 } 3186 if (error == 0) 3187 *freebuf = buf; 3188 else 3189 free(buf, M_TEMP); 3190 return (error); 3191 } 3192 3193 static struct namecache * 3194 vn_dd_from_dst(struct vnode *vp) 3195 { 3196 struct namecache *ncp; 3197 3198 cache_assert_vnode_locked(vp); 3199 TAILQ_FOREACH(ncp, &vp->v_cache_dst, nc_dst) { 3200 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0) 3201 return (ncp); 3202 } 3203 return (NULL); 3204 } 3205 3206 int 3207 vn_vptocnp(struct vnode **vp, char *buf, size_t *buflen) 3208 { 3209 struct vnode *dvp; 3210 struct namecache *ncp; 3211 struct mtx *vlp; 3212 int error; 3213 3214 vlp = VP2VNODELOCK(*vp); 3215 mtx_lock(vlp); 3216 ncp = (*vp)->v_cache_dd; 3217 if (ncp != NULL && (ncp->nc_flag & NCF_ISDOTDOT) == 0) { 3218 KASSERT(ncp == vn_dd_from_dst(*vp), 3219 ("%s: mismatch for dd entry (%p != %p)", __func__, 3220 ncp, vn_dd_from_dst(*vp))); 3221 } else { 3222 ncp = vn_dd_from_dst(*vp); 3223 } 3224 if (ncp != NULL) { 3225 if (*buflen < ncp->nc_nlen) { 3226 mtx_unlock(vlp); 3227 vrele(*vp); 3228 counter_u64_add(numfullpathfail4, 1); 3229 error = ENOMEM; 3230 SDT_PROBE3(vfs, namecache, fullpath, return, error, 3231 vp, NULL); 3232 return (error); 3233 } 3234 *buflen -= ncp->nc_nlen; 3235 memcpy(buf + *buflen, ncp->nc_name, ncp->nc_nlen); 3236 SDT_PROBE3(vfs, namecache, fullpath, hit, ncp->nc_dvp, 3237 ncp->nc_name, vp); 3238 dvp = *vp; 3239 *vp = ncp->nc_dvp; 3240 vref(*vp); 3241 mtx_unlock(vlp); 3242 vrele(dvp); 3243 return (0); 3244 } 3245 SDT_PROBE1(vfs, namecache, fullpath, miss, vp); 3246 3247 mtx_unlock(vlp); 3248 vn_lock(*vp, LK_SHARED | LK_RETRY); 3249 error = VOP_VPTOCNP(*vp, &dvp, buf, buflen); 3250 vput(*vp); 3251 if (error) { 3252 counter_u64_add(numfullpathfail2, 1); 3253 SDT_PROBE3(vfs, namecache, fullpath, return, error, vp, NULL); 3254 return (error); 3255 } 3256 3257 *vp = dvp; 3258 if (VN_IS_DOOMED(dvp)) { 3259 /* forced unmount */ 3260 vrele(dvp); 3261 error = ENOENT; 3262 SDT_PROBE3(vfs, namecache, fullpath, return, error, vp, NULL); 3263 return (error); 3264 } 3265 /* 3266 * *vp has its use count incremented still. 3267 */ 3268 3269 return (0); 3270 } 3271 3272 /* 3273 * Resolve a directory to a pathname. 3274 * 3275 * The name of the directory can always be found in the namecache or fetched 3276 * from the filesystem. There is also guaranteed to be only one parent, meaning 3277 * we can just follow vnodes up until we find the root. 3278 * 3279 * The vnode must be referenced. 3280 */ 3281 static int 3282 vn_fullpath_dir(struct vnode *vp, struct vnode *rdir, char *buf, char **retbuf, 3283 size_t *len, size_t addend) 3284 { 3285 #ifdef KDTRACE_HOOKS 3286 struct vnode *startvp = vp; 3287 #endif 3288 struct vnode *vp1; 3289 size_t buflen; 3290 int error; 3291 bool slash_prefixed; 3292 3293 VNPASS(vp->v_type == VDIR || VN_IS_DOOMED(vp), vp); 3294 VNPASS(vp->v_usecount > 0, vp); 3295 3296 buflen = *len; 3297 3298 slash_prefixed = true; 3299 if (addend == 0) { 3300 MPASS(*len >= 2); 3301 buflen--; 3302 buf[buflen] = '\0'; 3303 slash_prefixed = false; 3304 } 3305 3306 error = 0; 3307 3308 SDT_PROBE1(vfs, namecache, fullpath, entry, vp); 3309 counter_u64_add(numfullpathcalls, 1); 3310 while (vp != rdir && vp != rootvnode) { 3311 /* 3312 * The vp vnode must be already fully constructed, 3313 * since it is either found in namecache or obtained 3314 * from VOP_VPTOCNP(). We may test for VV_ROOT safely 3315 * without obtaining the vnode lock. 3316 */ 3317 if ((vp->v_vflag & VV_ROOT) != 0) { 3318 vn_lock(vp, LK_RETRY | LK_SHARED); 3319 3320 /* 3321 * With the vnode locked, check for races with 3322 * unmount, forced or not. Note that we 3323 * already verified that vp is not equal to 3324 * the root vnode, which means that 3325 * mnt_vnodecovered can be NULL only for the 3326 * case of unmount. 3327 */ 3328 if (VN_IS_DOOMED(vp) || 3329 (vp1 = vp->v_mount->mnt_vnodecovered) == NULL || 3330 vp1->v_mountedhere != vp->v_mount) { 3331 vput(vp); 3332 error = ENOENT; 3333 SDT_PROBE3(vfs, namecache, fullpath, return, 3334 error, vp, NULL); 3335 break; 3336 } 3337 3338 vref(vp1); 3339 vput(vp); 3340 vp = vp1; 3341 continue; 3342 } 3343 if (vp->v_type != VDIR) { 3344 vrele(vp); 3345 counter_u64_add(numfullpathfail1, 1); 3346 error = ENOTDIR; 3347 SDT_PROBE3(vfs, namecache, fullpath, return, 3348 error, vp, NULL); 3349 break; 3350 } 3351 error = vn_vptocnp(&vp, buf, &buflen); 3352 if (error) 3353 break; 3354 if (buflen == 0) { 3355 vrele(vp); 3356 error = ENOMEM; 3357 SDT_PROBE3(vfs, namecache, fullpath, return, error, 3358 startvp, NULL); 3359 break; 3360 } 3361 buf[--buflen] = '/'; 3362 slash_prefixed = true; 3363 } 3364 if (error) 3365 return (error); 3366 if (!slash_prefixed) { 3367 if (buflen == 0) { 3368 vrele(vp); 3369 counter_u64_add(numfullpathfail4, 1); 3370 SDT_PROBE3(vfs, namecache, fullpath, return, ENOMEM, 3371 startvp, NULL); 3372 return (ENOMEM); 3373 } 3374 buf[--buflen] = '/'; 3375 } 3376 counter_u64_add(numfullpathfound, 1); 3377 vrele(vp); 3378 3379 *retbuf = buf + buflen; 3380 SDT_PROBE3(vfs, namecache, fullpath, return, 0, startvp, *retbuf); 3381 *len -= buflen; 3382 *len += addend; 3383 return (0); 3384 } 3385 3386 /* 3387 * Resolve an arbitrary vnode to a pathname. 3388 * 3389 * Note 2 caveats: 3390 * - hardlinks are not tracked, thus if the vnode is not a directory this can 3391 * resolve to a different path than the one used to find it 3392 * - namecache is not mandatory, meaning names are not guaranteed to be added 3393 * (in which case resolving fails) 3394 */ 3395 static void __inline 3396 cache_rev_failed_impl(int *reason, int line) 3397 { 3398 3399 *reason = line; 3400 } 3401 #define cache_rev_failed(var) cache_rev_failed_impl((var), __LINE__) 3402 3403 static int 3404 vn_fullpath_any_smr(struct vnode *vp, struct vnode *rdir, char *buf, 3405 char **retbuf, size_t *buflen, size_t addend) 3406 { 3407 #ifdef KDTRACE_HOOKS 3408 struct vnode *startvp = vp; 3409 #endif 3410 struct vnode *tvp; 3411 struct mount *mp; 3412 struct namecache *ncp; 3413 size_t orig_buflen; 3414 int reason; 3415 int error; 3416 #ifdef KDTRACE_HOOKS 3417 int i; 3418 #endif 3419 seqc_t vp_seqc, tvp_seqc; 3420 u_char nc_flag; 3421 3422 VFS_SMR_ASSERT_ENTERED(); 3423 3424 if (!cache_fast_revlookup) { 3425 vfs_smr_exit(); 3426 return (-1); 3427 } 3428 3429 orig_buflen = *buflen; 3430 3431 if (addend == 0) { 3432 MPASS(*buflen >= 2); 3433 *buflen -= 1; 3434 buf[*buflen] = '\0'; 3435 } 3436 3437 if (vp == rdir || vp == rootvnode) { 3438 if (addend == 0) { 3439 *buflen -= 1; 3440 buf[*buflen] = '/'; 3441 } 3442 goto out_ok; 3443 } 3444 3445 #ifdef KDTRACE_HOOKS 3446 i = 0; 3447 #endif 3448 error = -1; 3449 ncp = NULL; /* for sdt probe down below */ 3450 vp_seqc = vn_seqc_read_any(vp); 3451 if (seqc_in_modify(vp_seqc)) { 3452 cache_rev_failed(&reason); 3453 goto out_abort; 3454 } 3455 3456 for (;;) { 3457 #ifdef KDTRACE_HOOKS 3458 i++; 3459 #endif 3460 if ((vp->v_vflag & VV_ROOT) != 0) { 3461 mp = atomic_load_ptr(&vp->v_mount); 3462 if (mp == NULL) { 3463 cache_rev_failed(&reason); 3464 goto out_abort; 3465 } 3466 tvp = atomic_load_ptr(&mp->mnt_vnodecovered); 3467 tvp_seqc = vn_seqc_read_any(tvp); 3468 if (seqc_in_modify(tvp_seqc)) { 3469 cache_rev_failed(&reason); 3470 goto out_abort; 3471 } 3472 if (!vn_seqc_consistent(vp, vp_seqc)) { 3473 cache_rev_failed(&reason); 3474 goto out_abort; 3475 } 3476 vp = tvp; 3477 vp_seqc = tvp_seqc; 3478 continue; 3479 } 3480 ncp = atomic_load_consume_ptr(&vp->v_cache_dd); 3481 if (ncp == NULL) { 3482 cache_rev_failed(&reason); 3483 goto out_abort; 3484 } 3485 nc_flag = atomic_load_char(&ncp->nc_flag); 3486 if ((nc_flag & NCF_ISDOTDOT) != 0) { 3487 cache_rev_failed(&reason); 3488 goto out_abort; 3489 } 3490 if (ncp->nc_nlen >= *buflen) { 3491 cache_rev_failed(&reason); 3492 error = ENOMEM; 3493 goto out_abort; 3494 } 3495 *buflen -= ncp->nc_nlen; 3496 memcpy(buf + *buflen, ncp->nc_name, ncp->nc_nlen); 3497 *buflen -= 1; 3498 buf[*buflen] = '/'; 3499 tvp = ncp->nc_dvp; 3500 tvp_seqc = vn_seqc_read_any(tvp); 3501 if (seqc_in_modify(tvp_seqc)) { 3502 cache_rev_failed(&reason); 3503 goto out_abort; 3504 } 3505 if (!vn_seqc_consistent(vp, vp_seqc)) { 3506 cache_rev_failed(&reason); 3507 goto out_abort; 3508 } 3509 /* 3510 * Acquire fence provided by vn_seqc_read_any above. 3511 */ 3512 if (__predict_false(atomic_load_ptr(&vp->v_cache_dd) != ncp)) { 3513 cache_rev_failed(&reason); 3514 goto out_abort; 3515 } 3516 if (!cache_ncp_canuse(ncp)) { 3517 cache_rev_failed(&reason); 3518 goto out_abort; 3519 } 3520 vp = tvp; 3521 vp_seqc = tvp_seqc; 3522 if (vp == rdir || vp == rootvnode) 3523 break; 3524 } 3525 out_ok: 3526 vfs_smr_exit(); 3527 *retbuf = buf + *buflen; 3528 *buflen = orig_buflen - *buflen + addend; 3529 SDT_PROBE2(vfs, namecache, fullpath_smr, hit, startvp, *retbuf); 3530 return (0); 3531 3532 out_abort: 3533 *buflen = orig_buflen; 3534 SDT_PROBE4(vfs, namecache, fullpath_smr, miss, startvp, ncp, reason, i); 3535 vfs_smr_exit(); 3536 return (error); 3537 } 3538 3539 static int 3540 vn_fullpath_any(struct vnode *vp, struct vnode *rdir, char *buf, char **retbuf, 3541 size_t *buflen) 3542 { 3543 size_t orig_buflen, addend; 3544 int error; 3545 3546 if (*buflen < 2) 3547 return (EINVAL); 3548 3549 orig_buflen = *buflen; 3550 3551 vref(vp); 3552 addend = 0; 3553 if (vp->v_type != VDIR) { 3554 *buflen -= 1; 3555 buf[*buflen] = '\0'; 3556 error = vn_vptocnp(&vp, buf, buflen); 3557 if (error) 3558 return (error); 3559 if (*buflen == 0) { 3560 vrele(vp); 3561 return (ENOMEM); 3562 } 3563 *buflen -= 1; 3564 buf[*buflen] = '/'; 3565 addend = orig_buflen - *buflen; 3566 } 3567 3568 return (vn_fullpath_dir(vp, rdir, buf, retbuf, buflen, addend)); 3569 } 3570 3571 /* 3572 * Resolve an arbitrary vnode to a pathname (taking care of hardlinks). 3573 * 3574 * Since the namecache does not track hardlinks, the caller is expected to first 3575 * look up the target vnode with SAVENAME | WANTPARENT flags passed to namei. 3576 * 3577 * Then we have 2 cases: 3578 * - if the found vnode is a directory, the path can be constructed just by 3579 * following names up the chain 3580 * - otherwise we populate the buffer with the saved name and start resolving 3581 * from the parent 3582 */ 3583 static int 3584 vn_fullpath_hardlink(struct nameidata *ndp, char **retbuf, char **freebuf, 3585 size_t *buflen) 3586 { 3587 char *buf, *tmpbuf; 3588 struct pwd *pwd; 3589 struct componentname *cnp; 3590 struct vnode *vp; 3591 size_t addend; 3592 int error; 3593 enum vtype type; 3594 3595 if (*buflen < 2) 3596 return (EINVAL); 3597 if (*buflen > MAXPATHLEN) 3598 *buflen = MAXPATHLEN; 3599 3600 buf = malloc(*buflen, M_TEMP, M_WAITOK); 3601 3602 addend = 0; 3603 vp = ndp->ni_vp; 3604 /* 3605 * Check for VBAD to work around the vp_crossmp bug in lookup(). 3606 * 3607 * For example consider tmpfs on /tmp and realpath /tmp. ni_vp will be 3608 * set to mount point's root vnode while ni_dvp will be vp_crossmp. 3609 * If the type is VDIR (like in this very case) we can skip looking 3610 * at ni_dvp in the first place. However, since vnodes get passed here 3611 * unlocked the target may transition to doomed state (type == VBAD) 3612 * before we get to evaluate the condition. If this happens, we will 3613 * populate part of the buffer and descend to vn_fullpath_dir with 3614 * vp == vp_crossmp. Prevent the problem by checking for VBAD. 3615 * 3616 * This should be atomic_load(&vp->v_type) but it is illegal to take 3617 * an address of a bit field, even if said field is sized to char. 3618 * Work around the problem by reading the value into a full-sized enum 3619 * and then re-reading it with atomic_load which will still prevent 3620 * the compiler from re-reading down the road. 3621 */ 3622 type = vp->v_type; 3623 type = atomic_load_int(&type); 3624 if (type == VBAD) { 3625 error = ENOENT; 3626 goto out_bad; 3627 } 3628 if (type != VDIR) { 3629 cnp = &ndp->ni_cnd; 3630 addend = cnp->cn_namelen + 2; 3631 if (*buflen < addend) { 3632 error = ENOMEM; 3633 goto out_bad; 3634 } 3635 *buflen -= addend; 3636 tmpbuf = buf + *buflen; 3637 tmpbuf[0] = '/'; 3638 memcpy(&tmpbuf[1], cnp->cn_nameptr, cnp->cn_namelen); 3639 tmpbuf[addend - 1] = '\0'; 3640 vp = ndp->ni_dvp; 3641 } 3642 3643 vfs_smr_enter(); 3644 pwd = pwd_get_smr(); 3645 error = vn_fullpath_any_smr(vp, pwd->pwd_rdir, buf, retbuf, buflen, 3646 addend); 3647 VFS_SMR_ASSERT_NOT_ENTERED(); 3648 if (error < 0) { 3649 pwd = pwd_hold(curthread); 3650 vref(vp); 3651 error = vn_fullpath_dir(vp, pwd->pwd_rdir, buf, retbuf, buflen, 3652 addend); 3653 pwd_drop(pwd); 3654 if (error != 0) 3655 goto out_bad; 3656 } 3657 3658 *freebuf = buf; 3659 3660 return (0); 3661 out_bad: 3662 free(buf, M_TEMP); 3663 return (error); 3664 } 3665 3666 struct vnode * 3667 vn_dir_dd_ino(struct vnode *vp) 3668 { 3669 struct namecache *ncp; 3670 struct vnode *ddvp; 3671 struct mtx *vlp; 3672 enum vgetstate vs; 3673 3674 ASSERT_VOP_LOCKED(vp, "vn_dir_dd_ino"); 3675 vlp = VP2VNODELOCK(vp); 3676 mtx_lock(vlp); 3677 TAILQ_FOREACH(ncp, &(vp->v_cache_dst), nc_dst) { 3678 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0) 3679 continue; 3680 ddvp = ncp->nc_dvp; 3681 vs = vget_prep(ddvp); 3682 mtx_unlock(vlp); 3683 if (vget_finish(ddvp, LK_SHARED | LK_NOWAIT, vs)) 3684 return (NULL); 3685 return (ddvp); 3686 } 3687 mtx_unlock(vlp); 3688 return (NULL); 3689 } 3690 3691 int 3692 vn_commname(struct vnode *vp, char *buf, u_int buflen) 3693 { 3694 struct namecache *ncp; 3695 struct mtx *vlp; 3696 int l; 3697 3698 vlp = VP2VNODELOCK(vp); 3699 mtx_lock(vlp); 3700 TAILQ_FOREACH(ncp, &vp->v_cache_dst, nc_dst) 3701 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0) 3702 break; 3703 if (ncp == NULL) { 3704 mtx_unlock(vlp); 3705 return (ENOENT); 3706 } 3707 l = min(ncp->nc_nlen, buflen - 1); 3708 memcpy(buf, ncp->nc_name, l); 3709 mtx_unlock(vlp); 3710 buf[l] = '\0'; 3711 return (0); 3712 } 3713 3714 /* 3715 * This function updates path string to vnode's full global path 3716 * and checks the size of the new path string against the pathlen argument. 3717 * 3718 * Requires a locked, referenced vnode. 3719 * Vnode is re-locked on success or ENODEV, otherwise unlocked. 3720 * 3721 * If vp is a directory, the call to vn_fullpath_global() always succeeds 3722 * because it falls back to the ".." lookup if the namecache lookup fails. 3723 */ 3724 int 3725 vn_path_to_global_path(struct thread *td, struct vnode *vp, char *path, 3726 u_int pathlen) 3727 { 3728 struct nameidata nd; 3729 struct vnode *vp1; 3730 char *rpath, *fbuf; 3731 int error; 3732 3733 ASSERT_VOP_ELOCKED(vp, __func__); 3734 3735 /* Construct global filesystem path from vp. */ 3736 VOP_UNLOCK(vp); 3737 error = vn_fullpath_global(vp, &rpath, &fbuf); 3738 3739 if (error != 0) { 3740 vrele(vp); 3741 return (error); 3742 } 3743 3744 if (strlen(rpath) >= pathlen) { 3745 vrele(vp); 3746 error = ENAMETOOLONG; 3747 goto out; 3748 } 3749 3750 /* 3751 * Re-lookup the vnode by path to detect a possible rename. 3752 * As a side effect, the vnode is relocked. 3753 * If vnode was renamed, return ENOENT. 3754 */ 3755 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, 3756 UIO_SYSSPACE, path, td); 3757 error = namei(&nd); 3758 if (error != 0) { 3759 vrele(vp); 3760 goto out; 3761 } 3762 NDFREE(&nd, NDF_ONLY_PNBUF); 3763 vp1 = nd.ni_vp; 3764 vrele(vp); 3765 if (vp1 == vp) 3766 strcpy(path, rpath); 3767 else { 3768 vput(vp1); 3769 error = ENOENT; 3770 } 3771 3772 out: 3773 free(fbuf, M_TEMP); 3774 return (error); 3775 } 3776 3777 #ifdef DDB 3778 static void 3779 db_print_vpath(struct vnode *vp) 3780 { 3781 3782 while (vp != NULL) { 3783 db_printf("%p: ", vp); 3784 if (vp == rootvnode) { 3785 db_printf("/"); 3786 vp = NULL; 3787 } else { 3788 if (vp->v_vflag & VV_ROOT) { 3789 db_printf("<mount point>"); 3790 vp = vp->v_mount->mnt_vnodecovered; 3791 } else { 3792 struct namecache *ncp; 3793 char *ncn; 3794 int i; 3795 3796 ncp = TAILQ_FIRST(&vp->v_cache_dst); 3797 if (ncp != NULL) { 3798 ncn = ncp->nc_name; 3799 for (i = 0; i < ncp->nc_nlen; i++) 3800 db_printf("%c", *ncn++); 3801 vp = ncp->nc_dvp; 3802 } else { 3803 vp = NULL; 3804 } 3805 } 3806 } 3807 db_printf("\n"); 3808 } 3809 3810 return; 3811 } 3812 3813 DB_SHOW_COMMAND(vpath, db_show_vpath) 3814 { 3815 struct vnode *vp; 3816 3817 if (!have_addr) { 3818 db_printf("usage: show vpath <struct vnode *>\n"); 3819 return; 3820 } 3821 3822 vp = (struct vnode *)addr; 3823 db_print_vpath(vp); 3824 } 3825 3826 #endif 3827 3828 static int cache_fast_lookup = 1; 3829 static char __read_frequently cache_fast_lookup_enabled = true; 3830 3831 #define CACHE_FPL_FAILED -2020 3832 3833 void 3834 cache_fast_lookup_enabled_recalc(void) 3835 { 3836 int lookup_flag; 3837 int mac_on; 3838 3839 #ifdef MAC 3840 mac_on = mac_vnode_check_lookup_enabled(); 3841 mac_on |= mac_vnode_check_readlink_enabled(); 3842 #else 3843 mac_on = 0; 3844 #endif 3845 3846 lookup_flag = atomic_load_int(&cache_fast_lookup); 3847 if (lookup_flag && !mac_on) { 3848 atomic_store_char(&cache_fast_lookup_enabled, true); 3849 } else { 3850 atomic_store_char(&cache_fast_lookup_enabled, false); 3851 } 3852 } 3853 3854 static int 3855 syscal_vfs_cache_fast_lookup(SYSCTL_HANDLER_ARGS) 3856 { 3857 int error, old; 3858 3859 old = atomic_load_int(&cache_fast_lookup); 3860 error = sysctl_handle_int(oidp, arg1, arg2, req); 3861 if (error == 0 && req->newptr && old != atomic_load_int(&cache_fast_lookup)) 3862 cache_fast_lookup_enabled_recalc(); 3863 return (error); 3864 } 3865 SYSCTL_PROC(_vfs, OID_AUTO, cache_fast_lookup, CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_MPSAFE, 3866 &cache_fast_lookup, 0, syscal_vfs_cache_fast_lookup, "IU", ""); 3867 3868 /* 3869 * Disable lockless lookup and observe all CPUs not executing it. 3870 * 3871 * Used when resizing the hash table. 3872 * 3873 * TODO: no provisions are made to handle tweaking of the knob at the same time 3874 */ 3875 static void 3876 cache_fplookup_lockout(void) 3877 { 3878 bool on; 3879 3880 on = atomic_load_char(&cache_fast_lookup_enabled); 3881 if (on) { 3882 atomic_store_char(&cache_fast_lookup_enabled, false); 3883 atomic_thread_fence_rel(); 3884 vfs_smr_quiesce(); 3885 } 3886 } 3887 3888 static void 3889 cache_fplookup_restore(void) 3890 { 3891 3892 cache_fast_lookup_enabled_recalc(); 3893 } 3894 3895 /* 3896 * Components of nameidata (or objects it can point to) which may 3897 * need restoring in case fast path lookup fails. 3898 */ 3899 struct nameidata_outer { 3900 size_t ni_pathlen; 3901 int cn_flags; 3902 }; 3903 3904 struct nameidata_saved { 3905 #ifdef INVARIANTS 3906 char *cn_nameptr; 3907 size_t ni_pathlen; 3908 #endif 3909 }; 3910 3911 #ifdef INVARIANTS 3912 struct cache_fpl_debug { 3913 size_t ni_pathlen; 3914 }; 3915 #endif 3916 3917 struct cache_fpl { 3918 struct nameidata *ndp; 3919 struct componentname *cnp; 3920 char *nulchar; 3921 struct vnode *dvp; 3922 struct vnode *tvp; 3923 seqc_t dvp_seqc; 3924 seqc_t tvp_seqc; 3925 uint32_t hash; 3926 struct nameidata_saved snd; 3927 struct nameidata_outer snd_outer; 3928 int line; 3929 enum cache_fpl_status status:8; 3930 bool in_smr; 3931 bool fsearch; 3932 bool savename; 3933 struct pwd **pwd; 3934 #ifdef INVARIANTS 3935 struct cache_fpl_debug debug; 3936 #endif 3937 }; 3938 3939 static bool cache_fplookup_is_mp(struct cache_fpl *fpl); 3940 static int cache_fplookup_cross_mount(struct cache_fpl *fpl); 3941 static int cache_fplookup_partial_setup(struct cache_fpl *fpl); 3942 static int cache_fplookup_skip_slashes(struct cache_fpl *fpl); 3943 static int cache_fplookup_trailingslash(struct cache_fpl *fpl); 3944 static void cache_fpl_pathlen_dec(struct cache_fpl *fpl); 3945 static void cache_fpl_pathlen_inc(struct cache_fpl *fpl); 3946 static void cache_fpl_pathlen_add(struct cache_fpl *fpl, size_t n); 3947 static void cache_fpl_pathlen_sub(struct cache_fpl *fpl, size_t n); 3948 3949 static void 3950 cache_fpl_cleanup_cnp(struct componentname *cnp) 3951 { 3952 3953 uma_zfree(namei_zone, cnp->cn_pnbuf); 3954 #ifdef DIAGNOSTIC 3955 cnp->cn_pnbuf = NULL; 3956 cnp->cn_nameptr = NULL; 3957 #endif 3958 } 3959 3960 static struct vnode * 3961 cache_fpl_handle_root(struct cache_fpl *fpl) 3962 { 3963 struct nameidata *ndp; 3964 struct componentname *cnp; 3965 3966 ndp = fpl->ndp; 3967 cnp = fpl->cnp; 3968 3969 MPASS(*(cnp->cn_nameptr) == '/'); 3970 cnp->cn_nameptr++; 3971 cache_fpl_pathlen_dec(fpl); 3972 3973 if (__predict_false(*(cnp->cn_nameptr) == '/')) { 3974 do { 3975 cnp->cn_nameptr++; 3976 cache_fpl_pathlen_dec(fpl); 3977 } while (*(cnp->cn_nameptr) == '/'); 3978 } 3979 3980 return (ndp->ni_rootdir); 3981 } 3982 3983 static void 3984 cache_fpl_checkpoint_outer(struct cache_fpl *fpl) 3985 { 3986 3987 fpl->snd_outer.ni_pathlen = fpl->ndp->ni_pathlen; 3988 fpl->snd_outer.cn_flags = fpl->ndp->ni_cnd.cn_flags; 3989 } 3990 3991 static void 3992 cache_fpl_checkpoint(struct cache_fpl *fpl) 3993 { 3994 3995 #ifdef INVARIANTS 3996 fpl->snd.cn_nameptr = fpl->ndp->ni_cnd.cn_nameptr; 3997 fpl->snd.ni_pathlen = fpl->debug.ni_pathlen; 3998 #endif 3999 } 4000 4001 static void 4002 cache_fpl_restore_partial(struct cache_fpl *fpl) 4003 { 4004 4005 fpl->ndp->ni_cnd.cn_flags = fpl->snd_outer.cn_flags; 4006 #ifdef INVARIANTS 4007 fpl->debug.ni_pathlen = fpl->snd.ni_pathlen; 4008 #endif 4009 } 4010 4011 static void 4012 cache_fpl_restore_abort(struct cache_fpl *fpl) 4013 { 4014 4015 cache_fpl_restore_partial(fpl); 4016 /* 4017 * It is 0 on entry by API contract. 4018 */ 4019 fpl->ndp->ni_resflags = 0; 4020 fpl->ndp->ni_cnd.cn_nameptr = fpl->ndp->ni_cnd.cn_pnbuf; 4021 fpl->ndp->ni_pathlen = fpl->snd_outer.ni_pathlen; 4022 } 4023 4024 #ifdef INVARIANTS 4025 #define cache_fpl_smr_assert_entered(fpl) ({ \ 4026 struct cache_fpl *_fpl = (fpl); \ 4027 MPASS(_fpl->in_smr == true); \ 4028 VFS_SMR_ASSERT_ENTERED(); \ 4029 }) 4030 #define cache_fpl_smr_assert_not_entered(fpl) ({ \ 4031 struct cache_fpl *_fpl = (fpl); \ 4032 MPASS(_fpl->in_smr == false); \ 4033 VFS_SMR_ASSERT_NOT_ENTERED(); \ 4034 }) 4035 static void 4036 cache_fpl_assert_status(struct cache_fpl *fpl) 4037 { 4038 4039 switch (fpl->status) { 4040 case CACHE_FPL_STATUS_UNSET: 4041 __assert_unreachable(); 4042 break; 4043 case CACHE_FPL_STATUS_DESTROYED: 4044 case CACHE_FPL_STATUS_ABORTED: 4045 case CACHE_FPL_STATUS_PARTIAL: 4046 case CACHE_FPL_STATUS_HANDLED: 4047 break; 4048 } 4049 } 4050 #else 4051 #define cache_fpl_smr_assert_entered(fpl) do { } while (0) 4052 #define cache_fpl_smr_assert_not_entered(fpl) do { } while (0) 4053 #define cache_fpl_assert_status(fpl) do { } while (0) 4054 #endif 4055 4056 #define cache_fpl_smr_enter_initial(fpl) ({ \ 4057 struct cache_fpl *_fpl = (fpl); \ 4058 vfs_smr_enter(); \ 4059 _fpl->in_smr = true; \ 4060 }) 4061 4062 #define cache_fpl_smr_enter(fpl) ({ \ 4063 struct cache_fpl *_fpl = (fpl); \ 4064 MPASS(_fpl->in_smr == false); \ 4065 vfs_smr_enter(); \ 4066 _fpl->in_smr = true; \ 4067 }) 4068 4069 #define cache_fpl_smr_exit(fpl) ({ \ 4070 struct cache_fpl *_fpl = (fpl); \ 4071 MPASS(_fpl->in_smr == true); \ 4072 vfs_smr_exit(); \ 4073 _fpl->in_smr = false; \ 4074 }) 4075 4076 static int 4077 cache_fpl_aborted_early_impl(struct cache_fpl *fpl, int line) 4078 { 4079 4080 if (fpl->status != CACHE_FPL_STATUS_UNSET) { 4081 KASSERT(fpl->status == CACHE_FPL_STATUS_PARTIAL, 4082 ("%s: converting to abort from %d at %d, set at %d\n", 4083 __func__, fpl->status, line, fpl->line)); 4084 } 4085 cache_fpl_smr_assert_not_entered(fpl); 4086 fpl->status = CACHE_FPL_STATUS_ABORTED; 4087 fpl->line = line; 4088 return (CACHE_FPL_FAILED); 4089 } 4090 4091 #define cache_fpl_aborted_early(x) cache_fpl_aborted_early_impl((x), __LINE__) 4092 4093 static int __noinline 4094 cache_fpl_aborted_impl(struct cache_fpl *fpl, int line) 4095 { 4096 struct nameidata *ndp; 4097 struct componentname *cnp; 4098 4099 ndp = fpl->ndp; 4100 cnp = fpl->cnp; 4101 4102 if (fpl->status != CACHE_FPL_STATUS_UNSET) { 4103 KASSERT(fpl->status == CACHE_FPL_STATUS_PARTIAL, 4104 ("%s: converting to abort from %d at %d, set at %d\n", 4105 __func__, fpl->status, line, fpl->line)); 4106 } 4107 fpl->status = CACHE_FPL_STATUS_ABORTED; 4108 fpl->line = line; 4109 if (fpl->in_smr) 4110 cache_fpl_smr_exit(fpl); 4111 cache_fpl_restore_abort(fpl); 4112 /* 4113 * Resolving symlinks overwrites data passed by the caller. 4114 * Let namei know. 4115 */ 4116 if (ndp->ni_loopcnt > 0) { 4117 fpl->status = CACHE_FPL_STATUS_DESTROYED; 4118 cache_fpl_cleanup_cnp(cnp); 4119 } 4120 return (CACHE_FPL_FAILED); 4121 } 4122 4123 #define cache_fpl_aborted(x) cache_fpl_aborted_impl((x), __LINE__) 4124 4125 static int __noinline 4126 cache_fpl_partial_impl(struct cache_fpl *fpl, int line) 4127 { 4128 4129 KASSERT(fpl->status == CACHE_FPL_STATUS_UNSET, 4130 ("%s: setting to partial at %d, but already set to %d at %d\n", 4131 __func__, line, fpl->status, fpl->line)); 4132 cache_fpl_smr_assert_entered(fpl); 4133 fpl->status = CACHE_FPL_STATUS_PARTIAL; 4134 fpl->line = line; 4135 return (cache_fplookup_partial_setup(fpl)); 4136 } 4137 4138 #define cache_fpl_partial(x) cache_fpl_partial_impl((x), __LINE__) 4139 4140 static int 4141 cache_fpl_handled_impl(struct cache_fpl *fpl, int line) 4142 { 4143 4144 KASSERT(fpl->status == CACHE_FPL_STATUS_UNSET, 4145 ("%s: setting to handled at %d, but already set to %d at %d\n", 4146 __func__, line, fpl->status, fpl->line)); 4147 cache_fpl_smr_assert_not_entered(fpl); 4148 fpl->status = CACHE_FPL_STATUS_HANDLED; 4149 fpl->line = line; 4150 return (0); 4151 } 4152 4153 #define cache_fpl_handled(x) cache_fpl_handled_impl((x), __LINE__) 4154 4155 static int 4156 cache_fpl_handled_error_impl(struct cache_fpl *fpl, int error, int line) 4157 { 4158 4159 KASSERT(fpl->status == CACHE_FPL_STATUS_UNSET, 4160 ("%s: setting to handled at %d, but already set to %d at %d\n", 4161 __func__, line, fpl->status, fpl->line)); 4162 MPASS(error != 0); 4163 MPASS(error != CACHE_FPL_FAILED); 4164 cache_fpl_smr_assert_not_entered(fpl); 4165 fpl->status = CACHE_FPL_STATUS_HANDLED; 4166 fpl->line = line; 4167 fpl->dvp = NULL; 4168 fpl->tvp = NULL; 4169 fpl->savename = false; 4170 return (error); 4171 } 4172 4173 #define cache_fpl_handled_error(x, e) cache_fpl_handled_error_impl((x), (e), __LINE__) 4174 4175 static bool 4176 cache_fpl_terminated(struct cache_fpl *fpl) 4177 { 4178 4179 return (fpl->status != CACHE_FPL_STATUS_UNSET); 4180 } 4181 4182 #define CACHE_FPL_SUPPORTED_CN_FLAGS \ 4183 (NC_NOMAKEENTRY | NC_KEEPPOSENTRY | LOCKLEAF | LOCKPARENT | WANTPARENT | \ 4184 FAILIFEXISTS | FOLLOW | LOCKSHARED | SAVENAME | SAVESTART | WILLBEDIR | \ 4185 ISOPEN | NOMACCHECK | AUDITVNODE1 | AUDITVNODE2 | NOCAPCHECK) 4186 4187 #define CACHE_FPL_INTERNAL_CN_FLAGS \ 4188 (ISDOTDOT | MAKEENTRY | ISLASTCN) 4189 4190 _Static_assert((CACHE_FPL_SUPPORTED_CN_FLAGS & CACHE_FPL_INTERNAL_CN_FLAGS) == 0, 4191 "supported and internal flags overlap"); 4192 4193 static bool 4194 cache_fpl_islastcn(struct nameidata *ndp) 4195 { 4196 4197 return (*ndp->ni_next == 0); 4198 } 4199 4200 static bool 4201 cache_fpl_istrailingslash(struct cache_fpl *fpl) 4202 { 4203 4204 return (*(fpl->nulchar - 1) == '/'); 4205 } 4206 4207 static bool 4208 cache_fpl_isdotdot(struct componentname *cnp) 4209 { 4210 4211 if (cnp->cn_namelen == 2 && 4212 cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.') 4213 return (true); 4214 return (false); 4215 } 4216 4217 static bool 4218 cache_can_fplookup(struct cache_fpl *fpl) 4219 { 4220 struct nameidata *ndp; 4221 struct componentname *cnp; 4222 struct thread *td; 4223 4224 ndp = fpl->ndp; 4225 cnp = fpl->cnp; 4226 td = cnp->cn_thread; 4227 4228 if (!atomic_load_char(&cache_fast_lookup_enabled)) { 4229 cache_fpl_aborted_early(fpl); 4230 return (false); 4231 } 4232 if ((cnp->cn_flags & ~CACHE_FPL_SUPPORTED_CN_FLAGS) != 0) { 4233 cache_fpl_aborted_early(fpl); 4234 return (false); 4235 } 4236 if (IN_CAPABILITY_MODE(td)) { 4237 cache_fpl_aborted_early(fpl); 4238 return (false); 4239 } 4240 if (AUDITING_TD(td)) { 4241 cache_fpl_aborted_early(fpl); 4242 return (false); 4243 } 4244 if (ndp->ni_startdir != NULL) { 4245 cache_fpl_aborted_early(fpl); 4246 return (false); 4247 } 4248 return (true); 4249 } 4250 4251 static int 4252 cache_fplookup_dirfd(struct cache_fpl *fpl, struct vnode **vpp) 4253 { 4254 struct nameidata *ndp; 4255 int error; 4256 bool fsearch; 4257 4258 ndp = fpl->ndp; 4259 error = fgetvp_lookup_smr(ndp->ni_dirfd, ndp, vpp, &fsearch); 4260 if (__predict_false(error != 0)) { 4261 return (cache_fpl_aborted(fpl)); 4262 } 4263 fpl->fsearch = fsearch; 4264 return (0); 4265 } 4266 4267 static int __noinline 4268 cache_fplookup_negative_promote(struct cache_fpl *fpl, struct namecache *oncp, 4269 uint32_t hash) 4270 { 4271 struct componentname *cnp; 4272 struct vnode *dvp; 4273 4274 cnp = fpl->cnp; 4275 dvp = fpl->dvp; 4276 4277 cache_fpl_smr_exit(fpl); 4278 if (cache_neg_promote_cond(dvp, cnp, oncp, hash)) 4279 return (cache_fpl_handled_error(fpl, ENOENT)); 4280 else 4281 return (cache_fpl_aborted(fpl)); 4282 } 4283 4284 /* 4285 * The target vnode is not supported, prepare for the slow path to take over. 4286 */ 4287 static int __noinline 4288 cache_fplookup_partial_setup(struct cache_fpl *fpl) 4289 { 4290 struct nameidata *ndp; 4291 struct componentname *cnp; 4292 enum vgetstate dvs; 4293 struct vnode *dvp; 4294 struct pwd *pwd; 4295 seqc_t dvp_seqc; 4296 4297 ndp = fpl->ndp; 4298 cnp = fpl->cnp; 4299 pwd = *(fpl->pwd); 4300 dvp = fpl->dvp; 4301 dvp_seqc = fpl->dvp_seqc; 4302 4303 if (!pwd_hold_smr(pwd)) { 4304 return (cache_fpl_aborted(fpl)); 4305 } 4306 4307 /* 4308 * Note that seqc is checked before the vnode is locked, so by 4309 * the time regular lookup gets to it it may have moved. 4310 * 4311 * Ultimately this does not affect correctness, any lookup errors 4312 * are userspace racing with itself. It is guaranteed that any 4313 * path which ultimately gets found could also have been found 4314 * by regular lookup going all the way in absence of concurrent 4315 * modifications. 4316 */ 4317 dvs = vget_prep_smr(dvp); 4318 cache_fpl_smr_exit(fpl); 4319 if (__predict_false(dvs == VGET_NONE)) { 4320 pwd_drop(pwd); 4321 return (cache_fpl_aborted(fpl)); 4322 } 4323 4324 vget_finish_ref(dvp, dvs); 4325 if (!vn_seqc_consistent(dvp, dvp_seqc)) { 4326 vrele(dvp); 4327 pwd_drop(pwd); 4328 return (cache_fpl_aborted(fpl)); 4329 } 4330 4331 cache_fpl_restore_partial(fpl); 4332 #ifdef INVARIANTS 4333 if (cnp->cn_nameptr != fpl->snd.cn_nameptr) { 4334 panic("%s: cn_nameptr mismatch (%p != %p) full [%s]\n", __func__, 4335 cnp->cn_nameptr, fpl->snd.cn_nameptr, cnp->cn_pnbuf); 4336 } 4337 #endif 4338 4339 ndp->ni_startdir = dvp; 4340 cnp->cn_flags |= MAKEENTRY; 4341 if (cache_fpl_islastcn(ndp)) 4342 cnp->cn_flags |= ISLASTCN; 4343 if (cache_fpl_isdotdot(cnp)) 4344 cnp->cn_flags |= ISDOTDOT; 4345 4346 /* 4347 * Skip potential extra slashes parsing did not take care of. 4348 * cache_fplookup_skip_slashes explains the mechanism. 4349 */ 4350 if (__predict_false(*(cnp->cn_nameptr) == '/')) { 4351 do { 4352 cnp->cn_nameptr++; 4353 cache_fpl_pathlen_dec(fpl); 4354 } while (*(cnp->cn_nameptr) == '/'); 4355 } 4356 4357 ndp->ni_pathlen = fpl->nulchar - cnp->cn_nameptr + 1; 4358 #ifdef INVARIANTS 4359 if (ndp->ni_pathlen != fpl->debug.ni_pathlen) { 4360 panic("%s: mismatch (%zu != %zu) nulchar %p nameptr %p [%s] ; full string [%s]\n", 4361 __func__, ndp->ni_pathlen, fpl->debug.ni_pathlen, fpl->nulchar, 4362 cnp->cn_nameptr, cnp->cn_nameptr, cnp->cn_pnbuf); 4363 } 4364 #endif 4365 return (0); 4366 } 4367 4368 static int 4369 cache_fplookup_final_child(struct cache_fpl *fpl, enum vgetstate tvs) 4370 { 4371 struct componentname *cnp; 4372 struct vnode *tvp; 4373 seqc_t tvp_seqc; 4374 int error, lkflags; 4375 4376 cnp = fpl->cnp; 4377 tvp = fpl->tvp; 4378 tvp_seqc = fpl->tvp_seqc; 4379 4380 if ((cnp->cn_flags & LOCKLEAF) != 0) { 4381 lkflags = LK_SHARED; 4382 if ((cnp->cn_flags & LOCKSHARED) == 0) 4383 lkflags = LK_EXCLUSIVE; 4384 error = vget_finish(tvp, lkflags, tvs); 4385 if (__predict_false(error != 0)) { 4386 return (cache_fpl_aborted(fpl)); 4387 } 4388 } else { 4389 vget_finish_ref(tvp, tvs); 4390 } 4391 4392 if (!vn_seqc_consistent(tvp, tvp_seqc)) { 4393 if ((cnp->cn_flags & LOCKLEAF) != 0) 4394 vput(tvp); 4395 else 4396 vrele(tvp); 4397 return (cache_fpl_aborted(fpl)); 4398 } 4399 4400 return (cache_fpl_handled(fpl)); 4401 } 4402 4403 /* 4404 * They want to possibly modify the state of the namecache. 4405 */ 4406 static int __noinline 4407 cache_fplookup_final_modifying(struct cache_fpl *fpl) 4408 { 4409 struct nameidata *ndp; 4410 struct componentname *cnp; 4411 enum vgetstate dvs; 4412 struct vnode *dvp, *tvp; 4413 struct mount *mp; 4414 seqc_t dvp_seqc; 4415 int error; 4416 bool docache; 4417 4418 ndp = fpl->ndp; 4419 cnp = fpl->cnp; 4420 dvp = fpl->dvp; 4421 dvp_seqc = fpl->dvp_seqc; 4422 4423 MPASS(*(cnp->cn_nameptr) != '/'); 4424 MPASS(cache_fpl_islastcn(ndp)); 4425 if ((cnp->cn_flags & LOCKPARENT) == 0) 4426 MPASS((cnp->cn_flags & WANTPARENT) != 0); 4427 MPASS((cnp->cn_flags & TRAILINGSLASH) == 0); 4428 MPASS(cnp->cn_nameiop == CREATE || cnp->cn_nameiop == DELETE || 4429 cnp->cn_nameiop == RENAME); 4430 MPASS((cnp->cn_flags & MAKEENTRY) == 0); 4431 MPASS((cnp->cn_flags & ISDOTDOT) == 0); 4432 4433 docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE; 4434 if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME) 4435 docache = false; 4436 4437 /* 4438 * Regular lookup nulifies the slash, which we don't do here. 4439 * Don't take chances with filesystem routines seeing it for 4440 * the last entry. 4441 */ 4442 if (cache_fpl_istrailingslash(fpl)) { 4443 return (cache_fpl_partial(fpl)); 4444 } 4445 4446 mp = atomic_load_ptr(&dvp->v_mount); 4447 if (__predict_false(mp == NULL)) { 4448 return (cache_fpl_aborted(fpl)); 4449 } 4450 4451 if (__predict_false(mp->mnt_flag & MNT_RDONLY)) { 4452 cache_fpl_smr_exit(fpl); 4453 /* 4454 * Original code keeps not checking for CREATE which 4455 * might be a bug. For now let the old lookup decide. 4456 */ 4457 if (cnp->cn_nameiop == CREATE) { 4458 return (cache_fpl_aborted(fpl)); 4459 } 4460 return (cache_fpl_handled_error(fpl, EROFS)); 4461 } 4462 4463 if (fpl->tvp != NULL && (cnp->cn_flags & FAILIFEXISTS) != 0) { 4464 cache_fpl_smr_exit(fpl); 4465 return (cache_fpl_handled_error(fpl, EEXIST)); 4466 } 4467 4468 /* 4469 * Secure access to dvp; check cache_fplookup_partial_setup for 4470 * reasoning. 4471 * 4472 * XXX At least UFS requires its lookup routine to be called for 4473 * the last path component, which leads to some level of complication 4474 * and inefficiency: 4475 * - the target routine always locks the target vnode, but our caller 4476 * may not need it locked 4477 * - some of the VOP machinery asserts that the parent is locked, which 4478 * once more may be not required 4479 * 4480 * TODO: add a flag for filesystems which don't need this. 4481 */ 4482 dvs = vget_prep_smr(dvp); 4483 cache_fpl_smr_exit(fpl); 4484 if (__predict_false(dvs == VGET_NONE)) { 4485 return (cache_fpl_aborted(fpl)); 4486 } 4487 4488 vget_finish_ref(dvp, dvs); 4489 if (!vn_seqc_consistent(dvp, dvp_seqc)) { 4490 vrele(dvp); 4491 return (cache_fpl_aborted(fpl)); 4492 } 4493 4494 error = vn_lock(dvp, LK_EXCLUSIVE); 4495 if (__predict_false(error != 0)) { 4496 vrele(dvp); 4497 return (cache_fpl_aborted(fpl)); 4498 } 4499 4500 tvp = NULL; 4501 cnp->cn_flags |= ISLASTCN; 4502 if (docache) 4503 cnp->cn_flags |= MAKEENTRY; 4504 if (cache_fpl_isdotdot(cnp)) 4505 cnp->cn_flags |= ISDOTDOT; 4506 cnp->cn_lkflags = LK_EXCLUSIVE; 4507 error = VOP_LOOKUP(dvp, &tvp, cnp); 4508 switch (error) { 4509 case EJUSTRETURN: 4510 case 0: 4511 break; 4512 case ENOTDIR: 4513 case ENOENT: 4514 vput(dvp); 4515 return (cache_fpl_handled_error(fpl, error)); 4516 default: 4517 vput(dvp); 4518 return (cache_fpl_aborted(fpl)); 4519 } 4520 4521 fpl->tvp = tvp; 4522 fpl->savename = (cnp->cn_flags & SAVENAME) != 0; 4523 4524 if (tvp == NULL) { 4525 if ((cnp->cn_flags & SAVESTART) != 0) { 4526 ndp->ni_startdir = dvp; 4527 vrefact(ndp->ni_startdir); 4528 cnp->cn_flags |= SAVENAME; 4529 fpl->savename = true; 4530 } 4531 MPASS(error == EJUSTRETURN); 4532 if ((cnp->cn_flags & LOCKPARENT) == 0) { 4533 VOP_UNLOCK(dvp); 4534 } 4535 return (cache_fpl_handled(fpl)); 4536 } 4537 4538 /* 4539 * There are very hairy corner cases concerning various flag combinations 4540 * and locking state. In particular here we only hold one lock instead of 4541 * two. 4542 * 4543 * Skip the complexity as it is of no significance for normal workloads. 4544 */ 4545 if (__predict_false(tvp == dvp)) { 4546 vput(dvp); 4547 vrele(tvp); 4548 return (cache_fpl_aborted(fpl)); 4549 } 4550 4551 /* 4552 * If they want the symlink itself we are fine, but if they want to 4553 * follow it regular lookup has to be engaged. 4554 */ 4555 if (tvp->v_type == VLNK) { 4556 if ((cnp->cn_flags & FOLLOW) != 0) { 4557 vput(dvp); 4558 vput(tvp); 4559 return (cache_fpl_aborted(fpl)); 4560 } 4561 } 4562 4563 /* 4564 * Since we expect this to be the terminal vnode it should almost never 4565 * be a mount point. 4566 */ 4567 if (__predict_false(cache_fplookup_is_mp(fpl))) { 4568 vput(dvp); 4569 vput(tvp); 4570 return (cache_fpl_aborted(fpl)); 4571 } 4572 4573 if ((cnp->cn_flags & FAILIFEXISTS) != 0) { 4574 vput(dvp); 4575 vput(tvp); 4576 return (cache_fpl_handled_error(fpl, EEXIST)); 4577 } 4578 4579 if ((cnp->cn_flags & LOCKLEAF) == 0) { 4580 VOP_UNLOCK(tvp); 4581 } 4582 4583 if ((cnp->cn_flags & LOCKPARENT) == 0) { 4584 VOP_UNLOCK(dvp); 4585 } 4586 4587 if ((cnp->cn_flags & SAVESTART) != 0) { 4588 ndp->ni_startdir = dvp; 4589 vrefact(ndp->ni_startdir); 4590 cnp->cn_flags |= SAVENAME; 4591 fpl->savename = true; 4592 } 4593 4594 return (cache_fpl_handled(fpl)); 4595 } 4596 4597 static int __noinline 4598 cache_fplookup_modifying(struct cache_fpl *fpl) 4599 { 4600 struct nameidata *ndp; 4601 4602 ndp = fpl->ndp; 4603 4604 if (!cache_fpl_islastcn(ndp)) { 4605 return (cache_fpl_partial(fpl)); 4606 } 4607 return (cache_fplookup_final_modifying(fpl)); 4608 } 4609 4610 static int __noinline 4611 cache_fplookup_final_withparent(struct cache_fpl *fpl) 4612 { 4613 struct componentname *cnp; 4614 enum vgetstate dvs, tvs; 4615 struct vnode *dvp, *tvp; 4616 seqc_t dvp_seqc; 4617 int error; 4618 4619 cnp = fpl->cnp; 4620 dvp = fpl->dvp; 4621 dvp_seqc = fpl->dvp_seqc; 4622 tvp = fpl->tvp; 4623 4624 MPASS((cnp->cn_flags & (LOCKPARENT|WANTPARENT)) != 0); 4625 4626 /* 4627 * This is less efficient than it can be for simplicity. 4628 */ 4629 dvs = vget_prep_smr(dvp); 4630 if (__predict_false(dvs == VGET_NONE)) { 4631 return (cache_fpl_aborted(fpl)); 4632 } 4633 tvs = vget_prep_smr(tvp); 4634 if (__predict_false(tvs == VGET_NONE)) { 4635 cache_fpl_smr_exit(fpl); 4636 vget_abort(dvp, dvs); 4637 return (cache_fpl_aborted(fpl)); 4638 } 4639 4640 cache_fpl_smr_exit(fpl); 4641 4642 if ((cnp->cn_flags & LOCKPARENT) != 0) { 4643 error = vget_finish(dvp, LK_EXCLUSIVE, dvs); 4644 if (__predict_false(error != 0)) { 4645 vget_abort(tvp, tvs); 4646 return (cache_fpl_aborted(fpl)); 4647 } 4648 } else { 4649 vget_finish_ref(dvp, dvs); 4650 } 4651 4652 if (!vn_seqc_consistent(dvp, dvp_seqc)) { 4653 vget_abort(tvp, tvs); 4654 if ((cnp->cn_flags & LOCKPARENT) != 0) 4655 vput(dvp); 4656 else 4657 vrele(dvp); 4658 return (cache_fpl_aborted(fpl)); 4659 } 4660 4661 error = cache_fplookup_final_child(fpl, tvs); 4662 if (__predict_false(error != 0)) { 4663 MPASS(fpl->status == CACHE_FPL_STATUS_ABORTED); 4664 if ((cnp->cn_flags & LOCKPARENT) != 0) 4665 vput(dvp); 4666 else 4667 vrele(dvp); 4668 return (error); 4669 } 4670 4671 MPASS(fpl->status == CACHE_FPL_STATUS_HANDLED); 4672 return (0); 4673 } 4674 4675 static int 4676 cache_fplookup_final(struct cache_fpl *fpl) 4677 { 4678 struct componentname *cnp; 4679 enum vgetstate tvs; 4680 struct vnode *dvp, *tvp; 4681 seqc_t dvp_seqc; 4682 4683 cnp = fpl->cnp; 4684 dvp = fpl->dvp; 4685 dvp_seqc = fpl->dvp_seqc; 4686 tvp = fpl->tvp; 4687 4688 MPASS(*(cnp->cn_nameptr) != '/'); 4689 4690 if (cnp->cn_nameiop != LOOKUP) { 4691 return (cache_fplookup_final_modifying(fpl)); 4692 } 4693 4694 if ((cnp->cn_flags & (LOCKPARENT|WANTPARENT)) != 0) 4695 return (cache_fplookup_final_withparent(fpl)); 4696 4697 tvs = vget_prep_smr(tvp); 4698 if (__predict_false(tvs == VGET_NONE)) { 4699 return (cache_fpl_partial(fpl)); 4700 } 4701 4702 if (!vn_seqc_consistent(dvp, dvp_seqc)) { 4703 cache_fpl_smr_exit(fpl); 4704 vget_abort(tvp, tvs); 4705 return (cache_fpl_aborted(fpl)); 4706 } 4707 4708 cache_fpl_smr_exit(fpl); 4709 return (cache_fplookup_final_child(fpl, tvs)); 4710 } 4711 4712 /* 4713 * Comment from locked lookup: 4714 * Check for degenerate name (e.g. / or "") which is a way of talking about a 4715 * directory, e.g. like "/." or ".". 4716 */ 4717 static int __noinline 4718 cache_fplookup_degenerate(struct cache_fpl *fpl) 4719 { 4720 struct componentname *cnp; 4721 struct vnode *dvp; 4722 enum vgetstate dvs; 4723 int error, lkflags; 4724 #ifdef INVARIANTS 4725 char *cp; 4726 #endif 4727 4728 fpl->tvp = fpl->dvp; 4729 fpl->tvp_seqc = fpl->dvp_seqc; 4730 4731 cnp = fpl->cnp; 4732 dvp = fpl->dvp; 4733 4734 #ifdef INVARIANTS 4735 for (cp = cnp->cn_pnbuf; *cp != '\0'; cp++) { 4736 KASSERT(*cp == '/', 4737 ("%s: encountered non-slash; string [%s]\n", __func__, 4738 cnp->cn_pnbuf)); 4739 } 4740 #endif 4741 4742 if (__predict_false(cnp->cn_nameiop != LOOKUP)) { 4743 cache_fpl_smr_exit(fpl); 4744 return (cache_fpl_handled_error(fpl, EISDIR)); 4745 } 4746 4747 MPASS((cnp->cn_flags & SAVESTART) == 0); 4748 4749 if ((cnp->cn_flags & (LOCKPARENT|WANTPARENT)) != 0) { 4750 return (cache_fplookup_final_withparent(fpl)); 4751 } 4752 4753 dvs = vget_prep_smr(dvp); 4754 cache_fpl_smr_exit(fpl); 4755 if (__predict_false(dvs == VGET_NONE)) { 4756 return (cache_fpl_aborted(fpl)); 4757 } 4758 4759 if ((cnp->cn_flags & LOCKLEAF) != 0) { 4760 lkflags = LK_SHARED; 4761 if ((cnp->cn_flags & LOCKSHARED) == 0) 4762 lkflags = LK_EXCLUSIVE; 4763 error = vget_finish(dvp, lkflags, dvs); 4764 if (__predict_false(error != 0)) { 4765 return (cache_fpl_aborted(fpl)); 4766 } 4767 } else { 4768 vget_finish_ref(dvp, dvs); 4769 } 4770 return (cache_fpl_handled(fpl)); 4771 } 4772 4773 static int __noinline 4774 cache_fplookup_noentry(struct cache_fpl *fpl) 4775 { 4776 struct nameidata *ndp; 4777 struct componentname *cnp; 4778 enum vgetstate dvs; 4779 struct vnode *dvp, *tvp; 4780 seqc_t dvp_seqc; 4781 int error; 4782 bool docache; 4783 4784 ndp = fpl->ndp; 4785 cnp = fpl->cnp; 4786 dvp = fpl->dvp; 4787 dvp_seqc = fpl->dvp_seqc; 4788 4789 MPASS((cnp->cn_flags & MAKEENTRY) == 0); 4790 MPASS((cnp->cn_flags & ISDOTDOT) == 0); 4791 MPASS(!cache_fpl_isdotdot(cnp)); 4792 4793 /* 4794 * Hack: delayed name len checking. 4795 */ 4796 if (__predict_false(cnp->cn_namelen > NAME_MAX)) { 4797 cache_fpl_smr_exit(fpl); 4798 return (cache_fpl_handled_error(fpl, ENAMETOOLONG)); 4799 } 4800 4801 if (cnp->cn_nameptr[0] == '/') { 4802 return (cache_fplookup_skip_slashes(fpl)); 4803 } 4804 4805 if (cnp->cn_nameptr[0] == '\0') { 4806 if (fpl->tvp == NULL) { 4807 return (cache_fplookup_degenerate(fpl)); 4808 } 4809 return (cache_fplookup_trailingslash(fpl)); 4810 } 4811 4812 if (cnp->cn_nameiop != LOOKUP) { 4813 fpl->tvp = NULL; 4814 return (cache_fplookup_modifying(fpl)); 4815 } 4816 4817 MPASS((cnp->cn_flags & SAVESTART) == 0); 4818 4819 /* 4820 * Only try to fill in the component if it is the last one, 4821 * otherwise not only there may be several to handle but the 4822 * walk may be complicated. 4823 */ 4824 if (!cache_fpl_islastcn(ndp)) { 4825 return (cache_fpl_partial(fpl)); 4826 } 4827 4828 /* 4829 * Regular lookup nulifies the slash, which we don't do here. 4830 * Don't take chances with filesystem routines seeing it for 4831 * the last entry. 4832 */ 4833 if (cache_fpl_istrailingslash(fpl)) { 4834 return (cache_fpl_partial(fpl)); 4835 } 4836 4837 /* 4838 * Secure access to dvp; check cache_fplookup_partial_setup for 4839 * reasoning. 4840 */ 4841 dvs = vget_prep_smr(dvp); 4842 cache_fpl_smr_exit(fpl); 4843 if (__predict_false(dvs == VGET_NONE)) { 4844 return (cache_fpl_aborted(fpl)); 4845 } 4846 4847 vget_finish_ref(dvp, dvs); 4848 if (!vn_seqc_consistent(dvp, dvp_seqc)) { 4849 vrele(dvp); 4850 return (cache_fpl_aborted(fpl)); 4851 } 4852 4853 error = vn_lock(dvp, LK_SHARED); 4854 if (__predict_false(error != 0)) { 4855 vrele(dvp); 4856 return (cache_fpl_aborted(fpl)); 4857 } 4858 4859 tvp = NULL; 4860 /* 4861 * TODO: provide variants which don't require locking either vnode. 4862 */ 4863 cnp->cn_flags |= ISLASTCN; 4864 docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE; 4865 if (docache) 4866 cnp->cn_flags |= MAKEENTRY; 4867 cnp->cn_lkflags = LK_SHARED; 4868 if ((cnp->cn_flags & LOCKSHARED) == 0) { 4869 cnp->cn_lkflags = LK_EXCLUSIVE; 4870 } 4871 error = VOP_LOOKUP(dvp, &tvp, cnp); 4872 switch (error) { 4873 case EJUSTRETURN: 4874 case 0: 4875 break; 4876 case ENOTDIR: 4877 case ENOENT: 4878 vput(dvp); 4879 return (cache_fpl_handled_error(fpl, error)); 4880 default: 4881 vput(dvp); 4882 return (cache_fpl_aborted(fpl)); 4883 } 4884 4885 fpl->tvp = tvp; 4886 if (!fpl->savename) { 4887 MPASS((cnp->cn_flags & SAVENAME) == 0); 4888 } 4889 4890 if (tvp == NULL) { 4891 MPASS(error == EJUSTRETURN); 4892 if ((cnp->cn_flags & (WANTPARENT | LOCKPARENT)) == 0) { 4893 vput(dvp); 4894 } else if ((cnp->cn_flags & LOCKPARENT) == 0) { 4895 VOP_UNLOCK(dvp); 4896 } 4897 return (cache_fpl_handled(fpl)); 4898 } 4899 4900 if (tvp->v_type == VLNK) { 4901 if ((cnp->cn_flags & FOLLOW) != 0) { 4902 vput(dvp); 4903 vput(tvp); 4904 return (cache_fpl_aborted(fpl)); 4905 } 4906 } 4907 4908 if (__predict_false(cache_fplookup_is_mp(fpl))) { 4909 vput(dvp); 4910 vput(tvp); 4911 return (cache_fpl_aborted(fpl)); 4912 } 4913 4914 if ((cnp->cn_flags & LOCKLEAF) == 0) { 4915 VOP_UNLOCK(tvp); 4916 } 4917 4918 if ((cnp->cn_flags & (WANTPARENT | LOCKPARENT)) == 0) { 4919 vput(dvp); 4920 } else if ((cnp->cn_flags & LOCKPARENT) == 0) { 4921 VOP_UNLOCK(dvp); 4922 } 4923 return (cache_fpl_handled(fpl)); 4924 } 4925 4926 static int __noinline 4927 cache_fplookup_dot(struct cache_fpl *fpl) 4928 { 4929 int error; 4930 4931 MPASS(!seqc_in_modify(fpl->dvp_seqc)); 4932 /* 4933 * Just re-assign the value. seqc will be checked later for the first 4934 * non-dot path component in line and/or before deciding to return the 4935 * vnode. 4936 */ 4937 fpl->tvp = fpl->dvp; 4938 fpl->tvp_seqc = fpl->dvp_seqc; 4939 4940 counter_u64_add(dothits, 1); 4941 SDT_PROBE3(vfs, namecache, lookup, hit, fpl->dvp, ".", fpl->dvp); 4942 4943 error = 0; 4944 if (cache_fplookup_is_mp(fpl)) { 4945 error = cache_fplookup_cross_mount(fpl); 4946 } 4947 return (error); 4948 } 4949 4950 static int __noinline 4951 cache_fplookup_dotdot(struct cache_fpl *fpl) 4952 { 4953 struct nameidata *ndp; 4954 struct componentname *cnp; 4955 struct namecache *ncp; 4956 struct vnode *dvp; 4957 struct prison *pr; 4958 u_char nc_flag; 4959 4960 ndp = fpl->ndp; 4961 cnp = fpl->cnp; 4962 dvp = fpl->dvp; 4963 4964 MPASS(cache_fpl_isdotdot(cnp)); 4965 4966 /* 4967 * XXX this is racy the same way regular lookup is 4968 */ 4969 for (pr = cnp->cn_cred->cr_prison; pr != NULL; 4970 pr = pr->pr_parent) 4971 if (dvp == pr->pr_root) 4972 break; 4973 4974 if (dvp == ndp->ni_rootdir || 4975 dvp == ndp->ni_topdir || 4976 dvp == rootvnode || 4977 pr != NULL) { 4978 fpl->tvp = dvp; 4979 fpl->tvp_seqc = vn_seqc_read_any(dvp); 4980 if (seqc_in_modify(fpl->tvp_seqc)) { 4981 return (cache_fpl_aborted(fpl)); 4982 } 4983 return (0); 4984 } 4985 4986 if ((dvp->v_vflag & VV_ROOT) != 0) { 4987 /* 4988 * TODO 4989 * The opposite of climb mount is needed here. 4990 */ 4991 return (cache_fpl_partial(fpl)); 4992 } 4993 4994 ncp = atomic_load_consume_ptr(&dvp->v_cache_dd); 4995 if (ncp == NULL) { 4996 return (cache_fpl_aborted(fpl)); 4997 } 4998 4999 nc_flag = atomic_load_char(&ncp->nc_flag); 5000 if ((nc_flag & NCF_ISDOTDOT) != 0) { 5001 if ((nc_flag & NCF_NEGATIVE) != 0) 5002 return (cache_fpl_aborted(fpl)); 5003 fpl->tvp = ncp->nc_vp; 5004 } else { 5005 fpl->tvp = ncp->nc_dvp; 5006 } 5007 5008 fpl->tvp_seqc = vn_seqc_read_any(fpl->tvp); 5009 if (seqc_in_modify(fpl->tvp_seqc)) { 5010 return (cache_fpl_partial(fpl)); 5011 } 5012 5013 /* 5014 * Acquire fence provided by vn_seqc_read_any above. 5015 */ 5016 if (__predict_false(atomic_load_ptr(&dvp->v_cache_dd) != ncp)) { 5017 return (cache_fpl_aborted(fpl)); 5018 } 5019 5020 if (!cache_ncp_canuse(ncp)) { 5021 return (cache_fpl_aborted(fpl)); 5022 } 5023 5024 counter_u64_add(dotdothits, 1); 5025 return (0); 5026 } 5027 5028 static int __noinline 5029 cache_fplookup_neg(struct cache_fpl *fpl, struct namecache *ncp, uint32_t hash) 5030 { 5031 u_char nc_flag; 5032 bool neg_promote; 5033 5034 nc_flag = atomic_load_char(&ncp->nc_flag); 5035 MPASS((nc_flag & NCF_NEGATIVE) != 0); 5036 /* 5037 * If they want to create an entry we need to replace this one. 5038 */ 5039 if (__predict_false(fpl->cnp->cn_nameiop != LOOKUP)) { 5040 fpl->tvp = NULL; 5041 return (cache_fplookup_modifying(fpl)); 5042 } 5043 neg_promote = cache_neg_hit_prep(ncp); 5044 if (!cache_fpl_neg_ncp_canuse(ncp)) { 5045 cache_neg_hit_abort(ncp); 5046 return (cache_fpl_partial(fpl)); 5047 } 5048 if (neg_promote) { 5049 return (cache_fplookup_negative_promote(fpl, ncp, hash)); 5050 } 5051 cache_neg_hit_finish(ncp); 5052 cache_fpl_smr_exit(fpl); 5053 return (cache_fpl_handled_error(fpl, ENOENT)); 5054 } 5055 5056 /* 5057 * Resolve a symlink. Called by filesystem-specific routines. 5058 * 5059 * Code flow is: 5060 * ... -> cache_fplookup_symlink -> VOP_FPLOOKUP_SYMLINK -> cache_symlink_resolve 5061 */ 5062 int 5063 cache_symlink_resolve(struct cache_fpl *fpl, const char *string, size_t len) 5064 { 5065 struct nameidata *ndp; 5066 struct componentname *cnp; 5067 size_t adjust; 5068 5069 ndp = fpl->ndp; 5070 cnp = fpl->cnp; 5071 5072 if (__predict_false(len == 0)) { 5073 return (ENOENT); 5074 } 5075 5076 if (__predict_false(len > MAXPATHLEN - 2)) { 5077 if (cache_fpl_istrailingslash(fpl)) { 5078 return (EAGAIN); 5079 } 5080 } 5081 5082 ndp->ni_pathlen = fpl->nulchar - cnp->cn_nameptr - cnp->cn_namelen + 1; 5083 #ifdef INVARIANTS 5084 if (ndp->ni_pathlen != fpl->debug.ni_pathlen) { 5085 panic("%s: mismatch (%zu != %zu) nulchar %p nameptr %p [%s] ; full string [%s]\n", 5086 __func__, ndp->ni_pathlen, fpl->debug.ni_pathlen, fpl->nulchar, 5087 cnp->cn_nameptr, cnp->cn_nameptr, cnp->cn_pnbuf); 5088 } 5089 #endif 5090 5091 if (__predict_false(len + ndp->ni_pathlen > MAXPATHLEN)) { 5092 return (ENAMETOOLONG); 5093 } 5094 5095 if (__predict_false(ndp->ni_loopcnt++ >= MAXSYMLINKS)) { 5096 return (ELOOP); 5097 } 5098 5099 adjust = len; 5100 if (ndp->ni_pathlen > 1) { 5101 bcopy(ndp->ni_next, cnp->cn_pnbuf + len, ndp->ni_pathlen); 5102 } else { 5103 if (cache_fpl_istrailingslash(fpl)) { 5104 adjust = len + 1; 5105 cnp->cn_pnbuf[len] = '/'; 5106 cnp->cn_pnbuf[len + 1] = '\0'; 5107 } else { 5108 cnp->cn_pnbuf[len] = '\0'; 5109 } 5110 } 5111 bcopy(string, cnp->cn_pnbuf, len); 5112 5113 ndp->ni_pathlen += adjust; 5114 cache_fpl_pathlen_add(fpl, adjust); 5115 cnp->cn_nameptr = cnp->cn_pnbuf; 5116 fpl->nulchar = &cnp->cn_nameptr[ndp->ni_pathlen - 1]; 5117 fpl->tvp = NULL; 5118 return (0); 5119 } 5120 5121 static int __noinline 5122 cache_fplookup_symlink(struct cache_fpl *fpl) 5123 { 5124 struct mount *mp; 5125 struct nameidata *ndp; 5126 struct componentname *cnp; 5127 struct vnode *dvp, *tvp; 5128 int error; 5129 5130 ndp = fpl->ndp; 5131 cnp = fpl->cnp; 5132 dvp = fpl->dvp; 5133 tvp = fpl->tvp; 5134 5135 if (cache_fpl_islastcn(ndp)) { 5136 if ((cnp->cn_flags & FOLLOW) == 0) { 5137 return (cache_fplookup_final(fpl)); 5138 } 5139 } 5140 5141 mp = atomic_load_ptr(&dvp->v_mount); 5142 if (__predict_false(mp == NULL)) { 5143 return (cache_fpl_aborted(fpl)); 5144 } 5145 5146 /* 5147 * Note this check races against setting the flag just like regular 5148 * lookup. 5149 */ 5150 if (__predict_false((mp->mnt_flag & MNT_NOSYMFOLLOW) != 0)) { 5151 cache_fpl_smr_exit(fpl); 5152 return (cache_fpl_handled_error(fpl, EACCES)); 5153 } 5154 5155 error = VOP_FPLOOKUP_SYMLINK(tvp, fpl); 5156 if (__predict_false(error != 0)) { 5157 switch (error) { 5158 case EAGAIN: 5159 return (cache_fpl_partial(fpl)); 5160 case ENOENT: 5161 case ENAMETOOLONG: 5162 case ELOOP: 5163 cache_fpl_smr_exit(fpl); 5164 return (cache_fpl_handled_error(fpl, error)); 5165 default: 5166 return (cache_fpl_aborted(fpl)); 5167 } 5168 } 5169 5170 if (*(cnp->cn_nameptr) == '/') { 5171 fpl->dvp = cache_fpl_handle_root(fpl); 5172 fpl->dvp_seqc = vn_seqc_read_any(fpl->dvp); 5173 if (seqc_in_modify(fpl->dvp_seqc)) { 5174 return (cache_fpl_aborted(fpl)); 5175 } 5176 } 5177 return (0); 5178 } 5179 5180 static int 5181 cache_fplookup_next(struct cache_fpl *fpl) 5182 { 5183 struct componentname *cnp; 5184 struct namecache *ncp; 5185 struct vnode *dvp, *tvp; 5186 u_char nc_flag; 5187 uint32_t hash; 5188 int error; 5189 5190 cnp = fpl->cnp; 5191 dvp = fpl->dvp; 5192 hash = fpl->hash; 5193 5194 if (__predict_false(cnp->cn_nameptr[0] == '.')) { 5195 if (cnp->cn_namelen == 1) { 5196 return (cache_fplookup_dot(fpl)); 5197 } 5198 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') { 5199 return (cache_fplookup_dotdot(fpl)); 5200 } 5201 } 5202 5203 MPASS(!cache_fpl_isdotdot(cnp)); 5204 5205 CK_SLIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) { 5206 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen && 5207 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen)) 5208 break; 5209 } 5210 5211 if (__predict_false(ncp == NULL)) { 5212 return (cache_fplookup_noentry(fpl)); 5213 } 5214 5215 tvp = atomic_load_ptr(&ncp->nc_vp); 5216 nc_flag = atomic_load_char(&ncp->nc_flag); 5217 if ((nc_flag & NCF_NEGATIVE) != 0) { 5218 return (cache_fplookup_neg(fpl, ncp, hash)); 5219 } 5220 5221 if (!cache_ncp_canuse(ncp)) { 5222 return (cache_fpl_partial(fpl)); 5223 } 5224 5225 fpl->tvp = tvp; 5226 fpl->tvp_seqc = vn_seqc_read_any(tvp); 5227 if (seqc_in_modify(fpl->tvp_seqc)) { 5228 return (cache_fpl_partial(fpl)); 5229 } 5230 5231 counter_u64_add(numposhits, 1); 5232 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ncp->nc_name, tvp); 5233 5234 error = 0; 5235 if (cache_fplookup_is_mp(fpl)) { 5236 error = cache_fplookup_cross_mount(fpl); 5237 } 5238 return (error); 5239 } 5240 5241 static bool 5242 cache_fplookup_mp_supported(struct mount *mp) 5243 { 5244 5245 MPASS(mp != NULL); 5246 if ((mp->mnt_kern_flag & MNTK_FPLOOKUP) == 0) 5247 return (false); 5248 return (true); 5249 } 5250 5251 /* 5252 * Walk up the mount stack (if any). 5253 * 5254 * Correctness is provided in the following ways: 5255 * - all vnodes are protected from freeing with SMR 5256 * - struct mount objects are type stable making them always safe to access 5257 * - stability of the particular mount is provided by busying it 5258 * - relationship between the vnode which is mounted on and the mount is 5259 * verified with the vnode sequence counter after busying 5260 * - association between root vnode of the mount and the mount is protected 5261 * by busy 5262 * 5263 * From that point on we can read the sequence counter of the root vnode 5264 * and get the next mount on the stack (if any) using the same protection. 5265 * 5266 * By the end of successful walk we are guaranteed the reached state was 5267 * indeed present at least at some point which matches the regular lookup. 5268 */ 5269 static int __noinline 5270 cache_fplookup_climb_mount(struct cache_fpl *fpl) 5271 { 5272 struct mount *mp, *prev_mp; 5273 struct mount_pcpu *mpcpu, *prev_mpcpu; 5274 struct vnode *vp; 5275 seqc_t vp_seqc; 5276 5277 vp = fpl->tvp; 5278 vp_seqc = fpl->tvp_seqc; 5279 5280 VNPASS(vp->v_type == VDIR || vp->v_type == VBAD, vp); 5281 mp = atomic_load_ptr(&vp->v_mountedhere); 5282 if (__predict_false(mp == NULL)) { 5283 return (0); 5284 } 5285 5286 prev_mp = NULL; 5287 for (;;) { 5288 if (!vfs_op_thread_enter_crit(mp, mpcpu)) { 5289 if (prev_mp != NULL) 5290 vfs_op_thread_exit_crit(prev_mp, prev_mpcpu); 5291 return (cache_fpl_partial(fpl)); 5292 } 5293 if (prev_mp != NULL) 5294 vfs_op_thread_exit_crit(prev_mp, prev_mpcpu); 5295 if (!vn_seqc_consistent(vp, vp_seqc)) { 5296 vfs_op_thread_exit_crit(mp, mpcpu); 5297 return (cache_fpl_partial(fpl)); 5298 } 5299 if (!cache_fplookup_mp_supported(mp)) { 5300 vfs_op_thread_exit_crit(mp, mpcpu); 5301 return (cache_fpl_partial(fpl)); 5302 } 5303 vp = atomic_load_ptr(&mp->mnt_rootvnode); 5304 if (vp == NULL) { 5305 vfs_op_thread_exit_crit(mp, mpcpu); 5306 return (cache_fpl_partial(fpl)); 5307 } 5308 vp_seqc = vn_seqc_read_any(vp); 5309 if (seqc_in_modify(vp_seqc)) { 5310 vfs_op_thread_exit_crit(mp, mpcpu); 5311 return (cache_fpl_partial(fpl)); 5312 } 5313 prev_mp = mp; 5314 prev_mpcpu = mpcpu; 5315 mp = atomic_load_ptr(&vp->v_mountedhere); 5316 if (mp == NULL) 5317 break; 5318 } 5319 5320 vfs_op_thread_exit_crit(prev_mp, prev_mpcpu); 5321 fpl->tvp = vp; 5322 fpl->tvp_seqc = vp_seqc; 5323 return (0); 5324 } 5325 5326 static int __noinline 5327 cache_fplookup_cross_mount(struct cache_fpl *fpl) 5328 { 5329 struct mount *mp; 5330 struct mount_pcpu *mpcpu; 5331 struct vnode *vp; 5332 seqc_t vp_seqc; 5333 5334 vp = fpl->tvp; 5335 vp_seqc = fpl->tvp_seqc; 5336 5337 VNPASS(vp->v_type == VDIR || vp->v_type == VBAD, vp); 5338 mp = atomic_load_ptr(&vp->v_mountedhere); 5339 if (__predict_false(mp == NULL)) { 5340 return (0); 5341 } 5342 5343 if (!vfs_op_thread_enter_crit(mp, mpcpu)) { 5344 return (cache_fpl_partial(fpl)); 5345 } 5346 if (!vn_seqc_consistent(vp, vp_seqc)) { 5347 vfs_op_thread_exit_crit(mp, mpcpu); 5348 return (cache_fpl_partial(fpl)); 5349 } 5350 if (!cache_fplookup_mp_supported(mp)) { 5351 vfs_op_thread_exit_crit(mp, mpcpu); 5352 return (cache_fpl_partial(fpl)); 5353 } 5354 vp = atomic_load_ptr(&mp->mnt_rootvnode); 5355 if (__predict_false(vp == NULL)) { 5356 vfs_op_thread_exit_crit(mp, mpcpu); 5357 return (cache_fpl_partial(fpl)); 5358 } 5359 vp_seqc = vn_seqc_read_any(vp); 5360 vfs_op_thread_exit_crit(mp, mpcpu); 5361 if (seqc_in_modify(vp_seqc)) { 5362 return (cache_fpl_partial(fpl)); 5363 } 5364 mp = atomic_load_ptr(&vp->v_mountedhere); 5365 if (__predict_false(mp != NULL)) { 5366 /* 5367 * There are possibly more mount points on top. 5368 * Normally this does not happen so for simplicity just start 5369 * over. 5370 */ 5371 return (cache_fplookup_climb_mount(fpl)); 5372 } 5373 5374 fpl->tvp = vp; 5375 fpl->tvp_seqc = vp_seqc; 5376 return (0); 5377 } 5378 5379 /* 5380 * Check if a vnode is mounted on. 5381 */ 5382 static bool 5383 cache_fplookup_is_mp(struct cache_fpl *fpl) 5384 { 5385 struct vnode *vp; 5386 5387 vp = fpl->tvp; 5388 return ((vn_irflag_read(vp) & VIRF_MOUNTPOINT) != 0); 5389 } 5390 5391 /* 5392 * Parse the path. 5393 * 5394 * The code was originally copy-pasted from regular lookup and despite 5395 * clean ups leaves performance on the table. Any modifications here 5396 * must take into account that in case off fallback the resulting 5397 * nameidata state has to be compatible with the original. 5398 */ 5399 5400 /* 5401 * Debug ni_pathlen tracking. 5402 */ 5403 #ifdef INVARIANTS 5404 static void 5405 cache_fpl_pathlen_add(struct cache_fpl *fpl, size_t n) 5406 { 5407 5408 fpl->debug.ni_pathlen += n; 5409 KASSERT(fpl->debug.ni_pathlen <= PATH_MAX, 5410 ("%s: pathlen overflow to %zd\n", __func__, fpl->debug.ni_pathlen)); 5411 } 5412 5413 static void 5414 cache_fpl_pathlen_sub(struct cache_fpl *fpl, size_t n) 5415 { 5416 5417 fpl->debug.ni_pathlen -= n; 5418 KASSERT(fpl->debug.ni_pathlen <= PATH_MAX, 5419 ("%s: pathlen underflow to %zd\n", __func__, fpl->debug.ni_pathlen)); 5420 } 5421 5422 static void 5423 cache_fpl_pathlen_inc(struct cache_fpl *fpl) 5424 { 5425 5426 cache_fpl_pathlen_add(fpl, 1); 5427 } 5428 5429 static void 5430 cache_fpl_pathlen_dec(struct cache_fpl *fpl) 5431 { 5432 5433 cache_fpl_pathlen_sub(fpl, 1); 5434 } 5435 #else 5436 static void 5437 cache_fpl_pathlen_add(struct cache_fpl *fpl, size_t n) 5438 { 5439 } 5440 5441 static void 5442 cache_fpl_pathlen_sub(struct cache_fpl *fpl, size_t n) 5443 { 5444 } 5445 5446 static void 5447 cache_fpl_pathlen_inc(struct cache_fpl *fpl) 5448 { 5449 } 5450 5451 static void 5452 cache_fpl_pathlen_dec(struct cache_fpl *fpl) 5453 { 5454 } 5455 #endif 5456 5457 static void 5458 cache_fplookup_parse(struct cache_fpl *fpl) 5459 { 5460 struct nameidata *ndp; 5461 struct componentname *cnp; 5462 struct vnode *dvp; 5463 char *cp; 5464 uint32_t hash; 5465 5466 ndp = fpl->ndp; 5467 cnp = fpl->cnp; 5468 dvp = fpl->dvp; 5469 5470 /* 5471 * Find the end of this path component, it is either / or nul. 5472 * 5473 * Store / as a temporary sentinel so that we only have one character 5474 * to test for. Pathnames tend to be short so this should not be 5475 * resulting in cache misses. 5476 * 5477 * TODO: fix this to be word-sized. 5478 */ 5479 KASSERT(&cnp->cn_nameptr[fpl->debug.ni_pathlen - 1] == fpl->nulchar, 5480 ("%s: mismatch between pathlen (%zu) and nulchar (%p != %p), string [%s]\n", 5481 __func__, fpl->debug.ni_pathlen, &cnp->cn_nameptr[fpl->debug.ni_pathlen - 1], 5482 fpl->nulchar, cnp->cn_pnbuf)); 5483 KASSERT(*fpl->nulchar == '\0', 5484 ("%s: expected nul at %p; string [%s]\n", __func__, fpl->nulchar, 5485 cnp->cn_pnbuf)); 5486 hash = cache_get_hash_iter_start(dvp); 5487 *fpl->nulchar = '/'; 5488 for (cp = cnp->cn_nameptr; *cp != '/'; cp++) { 5489 KASSERT(*cp != '\0', 5490 ("%s: encountered unexpected nul; string [%s]\n", __func__, 5491 cnp->cn_nameptr)); 5492 hash = cache_get_hash_iter(*cp, hash); 5493 continue; 5494 } 5495 *fpl->nulchar = '\0'; 5496 fpl->hash = cache_get_hash_iter_finish(hash); 5497 5498 cnp->cn_namelen = cp - cnp->cn_nameptr; 5499 cache_fpl_pathlen_sub(fpl, cnp->cn_namelen); 5500 5501 #ifdef INVARIANTS 5502 /* 5503 * cache_get_hash only accepts lengths up to NAME_MAX. This is fine since 5504 * we are going to fail this lookup with ENAMETOOLONG (see below). 5505 */ 5506 if (cnp->cn_namelen <= NAME_MAX) { 5507 if (fpl->hash != cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp)) { 5508 panic("%s: mismatched hash for [%s] len %ld", __func__, 5509 cnp->cn_nameptr, cnp->cn_namelen); 5510 } 5511 } 5512 #endif 5513 5514 /* 5515 * Hack: we have to check if the found path component's length exceeds 5516 * NAME_MAX. However, the condition is very rarely true and check can 5517 * be elided in the common case -- if an entry was found in the cache, 5518 * then it could not have been too long to begin with. 5519 */ 5520 ndp->ni_next = cp; 5521 } 5522 5523 static void 5524 cache_fplookup_parse_advance(struct cache_fpl *fpl) 5525 { 5526 struct nameidata *ndp; 5527 struct componentname *cnp; 5528 5529 ndp = fpl->ndp; 5530 cnp = fpl->cnp; 5531 5532 cnp->cn_nameptr = ndp->ni_next; 5533 KASSERT(*(cnp->cn_nameptr) == '/', 5534 ("%s: should have seen slash at %p ; buf %p [%s]\n", __func__, 5535 cnp->cn_nameptr, cnp->cn_pnbuf, cnp->cn_pnbuf)); 5536 cnp->cn_nameptr++; 5537 cache_fpl_pathlen_dec(fpl); 5538 } 5539 5540 /* 5541 * Skip spurious slashes in a pathname (e.g., "foo///bar") and retry. 5542 * 5543 * Lockless lookup tries to elide checking for spurious slashes and should they 5544 * be present is guaranteed to fail to find an entry. In this case the caller 5545 * must check if the name starts with a slash and call this routine. It is 5546 * going to fast forward across the spurious slashes and set the state up for 5547 * retry. 5548 */ 5549 static int __noinline 5550 cache_fplookup_skip_slashes(struct cache_fpl *fpl) 5551 { 5552 struct nameidata *ndp; 5553 struct componentname *cnp; 5554 5555 ndp = fpl->ndp; 5556 cnp = fpl->cnp; 5557 5558 MPASS(*(cnp->cn_nameptr) == '/'); 5559 do { 5560 cnp->cn_nameptr++; 5561 cache_fpl_pathlen_dec(fpl); 5562 } while (*(cnp->cn_nameptr) == '/'); 5563 5564 /* 5565 * Go back to one slash so that cache_fplookup_parse_advance has 5566 * something to skip. 5567 */ 5568 cnp->cn_nameptr--; 5569 cache_fpl_pathlen_inc(fpl); 5570 5571 /* 5572 * cache_fplookup_parse_advance starts from ndp->ni_next 5573 */ 5574 ndp->ni_next = cnp->cn_nameptr; 5575 5576 /* 5577 * See cache_fplookup_dot. 5578 */ 5579 fpl->tvp = fpl->dvp; 5580 fpl->tvp_seqc = fpl->dvp_seqc; 5581 5582 return (0); 5583 } 5584 5585 /* 5586 * Handle trailing slashes (e.g., "foo/"). 5587 * 5588 * If a trailing slash is found the terminal vnode must be a directory. 5589 * Regular lookup shortens the path by nulifying the first trailing slash and 5590 * sets the TRAILINGSLASH flag to denote this took place. There are several 5591 * checks on it performed later. 5592 * 5593 * Similarly to spurious slashes, lockless lookup handles this in a speculative 5594 * manner relying on an invariant that a non-directory vnode will get a miss. 5595 * In this case cn_nameptr[0] == '\0' and cn_namelen == 0. 5596 * 5597 * Thus for a path like "foo/bar/" the code unwinds the state back to "bar/" 5598 * and denotes this is the last path component, which avoids looping back. 5599 * 5600 * Only plain lookups are supported for now to restrict corner cases to handle. 5601 */ 5602 static int __noinline 5603 cache_fplookup_trailingslash(struct cache_fpl *fpl) 5604 { 5605 #ifdef INVARIANTS 5606 size_t ni_pathlen; 5607 #endif 5608 struct nameidata *ndp; 5609 struct componentname *cnp; 5610 struct namecache *ncp; 5611 struct vnode *tvp; 5612 char *cn_nameptr_orig, *cn_nameptr_slash; 5613 seqc_t tvp_seqc; 5614 u_char nc_flag; 5615 5616 ndp = fpl->ndp; 5617 cnp = fpl->cnp; 5618 tvp = fpl->tvp; 5619 tvp_seqc = fpl->tvp_seqc; 5620 5621 MPASS(fpl->dvp == fpl->tvp); 5622 KASSERT(cache_fpl_istrailingslash(fpl), 5623 ("%s: expected trailing slash at %p; string [%s]\n", __func__, fpl->nulchar - 1, 5624 cnp->cn_pnbuf)); 5625 KASSERT(cnp->cn_nameptr[0] == '\0', 5626 ("%s: expected nul char at %p; string [%s]\n", __func__, &cnp->cn_nameptr[0], 5627 cnp->cn_pnbuf)); 5628 KASSERT(cnp->cn_namelen == 0, 5629 ("%s: namelen 0 but got %ld; string [%s]\n", __func__, cnp->cn_namelen, 5630 cnp->cn_pnbuf)); 5631 MPASS(cnp->cn_nameptr > cnp->cn_pnbuf); 5632 5633 if (cnp->cn_nameiop != LOOKUP) { 5634 return (cache_fpl_aborted(fpl)); 5635 } 5636 5637 if (__predict_false(tvp->v_type != VDIR)) { 5638 if (!vn_seqc_consistent(tvp, tvp_seqc)) { 5639 return (cache_fpl_aborted(fpl)); 5640 } 5641 cache_fpl_smr_exit(fpl); 5642 return (cache_fpl_handled_error(fpl, ENOTDIR)); 5643 } 5644 5645 /* 5646 * Denote the last component. 5647 */ 5648 ndp->ni_next = &cnp->cn_nameptr[0]; 5649 MPASS(cache_fpl_islastcn(ndp)); 5650 5651 /* 5652 * Unwind trailing slashes. 5653 */ 5654 cn_nameptr_orig = cnp->cn_nameptr; 5655 while (cnp->cn_nameptr >= cnp->cn_pnbuf) { 5656 cnp->cn_nameptr--; 5657 if (cnp->cn_nameptr[0] != '/') { 5658 break; 5659 } 5660 } 5661 5662 /* 5663 * Unwind to the beginning of the path component. 5664 * 5665 * Note the path may or may not have started with a slash. 5666 */ 5667 cn_nameptr_slash = cnp->cn_nameptr; 5668 while (cnp->cn_nameptr > cnp->cn_pnbuf) { 5669 cnp->cn_nameptr--; 5670 if (cnp->cn_nameptr[0] == '/') { 5671 break; 5672 } 5673 } 5674 if (cnp->cn_nameptr[0] == '/') { 5675 cnp->cn_nameptr++; 5676 } 5677 5678 cnp->cn_namelen = cn_nameptr_slash - cnp->cn_nameptr + 1; 5679 cache_fpl_pathlen_add(fpl, cn_nameptr_orig - cnp->cn_nameptr); 5680 cache_fpl_checkpoint(fpl); 5681 5682 #ifdef INVARIANTS 5683 ni_pathlen = fpl->nulchar - cnp->cn_nameptr + 1; 5684 if (ni_pathlen != fpl->debug.ni_pathlen) { 5685 panic("%s: mismatch (%zu != %zu) nulchar %p nameptr %p [%s] ; full string [%s]\n", 5686 __func__, ni_pathlen, fpl->debug.ni_pathlen, fpl->nulchar, 5687 cnp->cn_nameptr, cnp->cn_nameptr, cnp->cn_pnbuf); 5688 } 5689 #endif 5690 5691 /* 5692 * If this was a "./" lookup the parent directory is already correct. 5693 */ 5694 if (cnp->cn_nameptr[0] == '.' && cnp->cn_namelen == 1) { 5695 return (0); 5696 } 5697 5698 /* 5699 * Otherwise we need to look it up. 5700 */ 5701 tvp = fpl->tvp; 5702 ncp = atomic_load_consume_ptr(&tvp->v_cache_dd); 5703 if (__predict_false(ncp == NULL)) { 5704 return (cache_fpl_aborted(fpl)); 5705 } 5706 nc_flag = atomic_load_char(&ncp->nc_flag); 5707 if ((nc_flag & NCF_ISDOTDOT) != 0) { 5708 return (cache_fpl_aborted(fpl)); 5709 } 5710 fpl->dvp = ncp->nc_dvp; 5711 fpl->dvp_seqc = vn_seqc_read_any(fpl->dvp); 5712 if (seqc_in_modify(fpl->dvp_seqc)) { 5713 return (cache_fpl_aborted(fpl)); 5714 } 5715 return (0); 5716 } 5717 5718 /* 5719 * See the API contract for VOP_FPLOOKUP_VEXEC. 5720 */ 5721 static int __noinline 5722 cache_fplookup_failed_vexec(struct cache_fpl *fpl, int error) 5723 { 5724 struct componentname *cnp; 5725 struct vnode *dvp; 5726 seqc_t dvp_seqc; 5727 5728 cnp = fpl->cnp; 5729 dvp = fpl->dvp; 5730 dvp_seqc = fpl->dvp_seqc; 5731 5732 /* 5733 * TODO: Due to ignoring trailing slashes lookup will perform a 5734 * permission check on the last dir when it should not be doing it. It 5735 * may fail, but said failure should be ignored. It is possible to fix 5736 * it up fully without resorting to regular lookup, but for now just 5737 * abort. 5738 */ 5739 if (cache_fpl_istrailingslash(fpl)) { 5740 return (cache_fpl_aborted(fpl)); 5741 } 5742 5743 /* 5744 * Hack: delayed degenerate path checking. 5745 */ 5746 if (cnp->cn_nameptr[0] == '\0' && fpl->tvp == NULL) { 5747 return (cache_fplookup_degenerate(fpl)); 5748 } 5749 5750 /* 5751 * Hack: delayed name len checking. 5752 */ 5753 if (__predict_false(cnp->cn_namelen > NAME_MAX)) { 5754 cache_fpl_smr_exit(fpl); 5755 return (cache_fpl_handled_error(fpl, ENAMETOOLONG)); 5756 } 5757 5758 /* 5759 * Hack: they may be looking up foo/bar, where foo is not a directory. 5760 * In such a case we need to return ENOTDIR, but we may happen to get 5761 * here with a different error. 5762 */ 5763 if (dvp->v_type != VDIR) { 5764 error = ENOTDIR; 5765 } 5766 5767 /* 5768 * Hack: handle O_SEARCH. 5769 * 5770 * Open Group Base Specifications Issue 7, 2018 edition states: 5771 * <quote> 5772 * If the access mode of the open file description associated with the 5773 * file descriptor is not O_SEARCH, the function shall check whether 5774 * directory searches are permitted using the current permissions of 5775 * the directory underlying the file descriptor. If the access mode is 5776 * O_SEARCH, the function shall not perform the check. 5777 * </quote> 5778 * 5779 * Regular lookup tests for the NOEXECCHECK flag for every path 5780 * component to decide whether to do the permission check. However, 5781 * since most lookups never have the flag (and when they do it is only 5782 * present for the first path component), lockless lookup only acts on 5783 * it if there is a permission problem. Here the flag is represented 5784 * with a boolean so that we don't have to clear it on the way out. 5785 * 5786 * For simplicity this always aborts. 5787 * TODO: check if this is the first lookup and ignore the permission 5788 * problem. Note the flag has to survive fallback (if it happens to be 5789 * performed). 5790 */ 5791 if (fpl->fsearch) { 5792 return (cache_fpl_aborted(fpl)); 5793 } 5794 5795 switch (error) { 5796 case EAGAIN: 5797 if (!vn_seqc_consistent(dvp, dvp_seqc)) { 5798 error = cache_fpl_aborted(fpl); 5799 } else { 5800 cache_fpl_partial(fpl); 5801 } 5802 break; 5803 default: 5804 if (!vn_seqc_consistent(dvp, dvp_seqc)) { 5805 error = cache_fpl_aborted(fpl); 5806 } else { 5807 cache_fpl_smr_exit(fpl); 5808 cache_fpl_handled_error(fpl, error); 5809 } 5810 break; 5811 } 5812 return (error); 5813 } 5814 5815 static int 5816 cache_fplookup_impl(struct vnode *dvp, struct cache_fpl *fpl) 5817 { 5818 struct nameidata *ndp; 5819 struct componentname *cnp; 5820 struct mount *mp; 5821 int error; 5822 5823 ndp = fpl->ndp; 5824 cnp = fpl->cnp; 5825 5826 cache_fpl_checkpoint(fpl); 5827 5828 /* 5829 * The vnode at hand is almost always stable, skip checking for it. 5830 * Worst case this postpones the check towards the end of the iteration 5831 * of the main loop. 5832 */ 5833 fpl->dvp = dvp; 5834 fpl->dvp_seqc = vn_seqc_read_notmodify(fpl->dvp); 5835 5836 mp = atomic_load_ptr(&dvp->v_mount); 5837 if (__predict_false(mp == NULL || !cache_fplookup_mp_supported(mp))) { 5838 return (cache_fpl_aborted(fpl)); 5839 } 5840 5841 MPASS(fpl->tvp == NULL); 5842 5843 for (;;) { 5844 cache_fplookup_parse(fpl); 5845 5846 error = VOP_FPLOOKUP_VEXEC(fpl->dvp, cnp->cn_cred); 5847 if (__predict_false(error != 0)) { 5848 error = cache_fplookup_failed_vexec(fpl, error); 5849 break; 5850 } 5851 5852 error = cache_fplookup_next(fpl); 5853 if (__predict_false(cache_fpl_terminated(fpl))) { 5854 break; 5855 } 5856 5857 VNPASS(!seqc_in_modify(fpl->tvp_seqc), fpl->tvp); 5858 5859 if (fpl->tvp->v_type == VLNK) { 5860 error = cache_fplookup_symlink(fpl); 5861 if (cache_fpl_terminated(fpl)) { 5862 break; 5863 } 5864 } else { 5865 if (cache_fpl_islastcn(ndp)) { 5866 error = cache_fplookup_final(fpl); 5867 break; 5868 } 5869 5870 if (!vn_seqc_consistent(fpl->dvp, fpl->dvp_seqc)) { 5871 error = cache_fpl_aborted(fpl); 5872 break; 5873 } 5874 5875 fpl->dvp = fpl->tvp; 5876 fpl->dvp_seqc = fpl->tvp_seqc; 5877 cache_fplookup_parse_advance(fpl); 5878 } 5879 5880 cache_fpl_checkpoint(fpl); 5881 } 5882 5883 return (error); 5884 } 5885 5886 /* 5887 * Fast path lookup protected with SMR and sequence counters. 5888 * 5889 * Note: all VOP_FPLOOKUP_VEXEC routines have a comment referencing this one. 5890 * 5891 * Filesystems can opt in by setting the MNTK_FPLOOKUP flag and meeting criteria 5892 * outlined below. 5893 * 5894 * Traditional vnode lookup conceptually looks like this: 5895 * 5896 * vn_lock(current); 5897 * for (;;) { 5898 * next = find(); 5899 * vn_lock(next); 5900 * vn_unlock(current); 5901 * current = next; 5902 * if (last) 5903 * break; 5904 * } 5905 * return (current); 5906 * 5907 * Each jump to the next vnode is safe memory-wise and atomic with respect to 5908 * any modifications thanks to holding respective locks. 5909 * 5910 * The same guarantee can be provided with a combination of safe memory 5911 * reclamation and sequence counters instead. If all operations which affect 5912 * the relationship between the current vnode and the one we are looking for 5913 * also modify the counter, we can verify whether all the conditions held as 5914 * we made the jump. This includes things like permissions, mount points etc. 5915 * Counter modification is provided by enclosing relevant places in 5916 * vn_seqc_write_begin()/end() calls. 5917 * 5918 * Thus this translates to: 5919 * 5920 * vfs_smr_enter(); 5921 * dvp_seqc = seqc_read_any(dvp); 5922 * if (seqc_in_modify(dvp_seqc)) // someone is altering the vnode 5923 * abort(); 5924 * for (;;) { 5925 * tvp = find(); 5926 * tvp_seqc = seqc_read_any(tvp); 5927 * if (seqc_in_modify(tvp_seqc)) // someone is altering the target vnode 5928 * abort(); 5929 * if (!seqc_consistent(dvp, dvp_seqc) // someone is altering the vnode 5930 * abort(); 5931 * dvp = tvp; // we know nothing of importance has changed 5932 * dvp_seqc = tvp_seqc; // store the counter for the tvp iteration 5933 * if (last) 5934 * break; 5935 * } 5936 * vget(); // secure the vnode 5937 * if (!seqc_consistent(tvp, tvp_seqc) // final check 5938 * abort(); 5939 * // at this point we know nothing has changed for any parent<->child pair 5940 * // as they were crossed during the lookup, meaning we matched the guarantee 5941 * // of the locked variant 5942 * return (tvp); 5943 * 5944 * The API contract for VOP_FPLOOKUP_VEXEC routines is as follows: 5945 * - they are called while within vfs_smr protection which they must never exit 5946 * - EAGAIN can be returned to denote checking could not be performed, it is 5947 * always valid to return it 5948 * - if the sequence counter has not changed the result must be valid 5949 * - if the sequence counter has changed both false positives and false negatives 5950 * are permitted (since the result will be rejected later) 5951 * - for simple cases of unix permission checks vaccess_vexec_smr can be used 5952 * 5953 * Caveats to watch out for: 5954 * - vnodes are passed unlocked and unreferenced with nothing stopping 5955 * VOP_RECLAIM, in turn meaning that ->v_data can become NULL. It is advised 5956 * to use atomic_load_ptr to fetch it. 5957 * - the aforementioned object can also get freed, meaning absent other means it 5958 * should be protected with vfs_smr 5959 * - either safely checking permissions as they are modified or guaranteeing 5960 * their stability is left to the routine 5961 */ 5962 int 5963 cache_fplookup(struct nameidata *ndp, enum cache_fpl_status *status, 5964 struct pwd **pwdp) 5965 { 5966 struct cache_fpl fpl; 5967 struct pwd *pwd; 5968 struct vnode *dvp; 5969 struct componentname *cnp; 5970 int error; 5971 5972 fpl.status = CACHE_FPL_STATUS_UNSET; 5973 fpl.in_smr = false; 5974 fpl.ndp = ndp; 5975 fpl.cnp = cnp = &ndp->ni_cnd; 5976 MPASS(ndp->ni_lcf == 0); 5977 MPASS(curthread == cnp->cn_thread); 5978 KASSERT ((cnp->cn_flags & CACHE_FPL_INTERNAL_CN_FLAGS) == 0, 5979 ("%s: internal flags found in cn_flags %" PRIx64, __func__, 5980 cnp->cn_flags)); 5981 if ((cnp->cn_flags & SAVESTART) != 0) { 5982 MPASS(cnp->cn_nameiop != LOOKUP); 5983 } 5984 MPASS(cnp->cn_nameptr == cnp->cn_pnbuf); 5985 5986 if (__predict_false(!cache_can_fplookup(&fpl))) { 5987 *status = fpl.status; 5988 SDT_PROBE3(vfs, fplookup, lookup, done, ndp, fpl.line, fpl.status); 5989 return (EOPNOTSUPP); 5990 } 5991 5992 cache_fpl_checkpoint_outer(&fpl); 5993 5994 cache_fpl_smr_enter_initial(&fpl); 5995 #ifdef INVARIANTS 5996 fpl.debug.ni_pathlen = ndp->ni_pathlen; 5997 #endif 5998 fpl.nulchar = &cnp->cn_nameptr[ndp->ni_pathlen - 1]; 5999 fpl.fsearch = false; 6000 fpl.savename = (cnp->cn_flags & SAVENAME) != 0; 6001 fpl.tvp = NULL; /* for degenerate path handling */ 6002 fpl.pwd = pwdp; 6003 pwd = pwd_get_smr(); 6004 *(fpl.pwd) = pwd; 6005 ndp->ni_rootdir = pwd->pwd_rdir; 6006 ndp->ni_topdir = pwd->pwd_jdir; 6007 6008 if (cnp->cn_pnbuf[0] == '/') { 6009 dvp = cache_fpl_handle_root(&fpl); 6010 MPASS(ndp->ni_resflags == 0); 6011 ndp->ni_resflags = NIRES_ABS; 6012 } else { 6013 if (ndp->ni_dirfd == AT_FDCWD) { 6014 dvp = pwd->pwd_cdir; 6015 } else { 6016 error = cache_fplookup_dirfd(&fpl, &dvp); 6017 if (__predict_false(error != 0)) { 6018 goto out; 6019 } 6020 } 6021 } 6022 6023 SDT_PROBE4(vfs, namei, lookup, entry, dvp, cnp->cn_pnbuf, cnp->cn_flags, true); 6024 error = cache_fplookup_impl(dvp, &fpl); 6025 out: 6026 cache_fpl_smr_assert_not_entered(&fpl); 6027 cache_fpl_assert_status(&fpl); 6028 *status = fpl.status; 6029 if (SDT_PROBES_ENABLED()) { 6030 SDT_PROBE3(vfs, fplookup, lookup, done, ndp, fpl.line, fpl.status); 6031 if (fpl.status == CACHE_FPL_STATUS_HANDLED) 6032 SDT_PROBE4(vfs, namei, lookup, return, error, ndp->ni_vp, true, 6033 ndp); 6034 } 6035 6036 if (__predict_true(fpl.status == CACHE_FPL_STATUS_HANDLED)) { 6037 MPASS(error != CACHE_FPL_FAILED); 6038 if (error != 0) { 6039 MPASS(fpl.dvp == NULL); 6040 MPASS(fpl.tvp == NULL); 6041 MPASS(fpl.savename == false); 6042 } 6043 ndp->ni_dvp = fpl.dvp; 6044 ndp->ni_vp = fpl.tvp; 6045 if (fpl.savename) { 6046 cnp->cn_flags |= HASBUF; 6047 } else { 6048 cache_fpl_cleanup_cnp(cnp); 6049 } 6050 } 6051 return (error); 6052 } 6053