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