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