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