1 /* 2 * Copyright (c) 2003,2004 The DragonFly Project. All rights reserved. 3 * 4 * This code is derived from software contributed to The DragonFly Project 5 * by Matthew Dillon <dillon@backplane.com> 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * 3. Neither the name of The DragonFly Project nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific, prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * Copyright (c) 1989, 1993, 1995 35 * The Regents of the University of California. All rights reserved. 36 * 37 * This code is derived from software contributed to Berkeley by 38 * Poul-Henning Kamp of the FreeBSD Project. 39 * 40 * Redistribution and use in source and binary forms, with or without 41 * modification, are permitted provided that the following conditions 42 * are met: 43 * 1. Redistributions of source code must retain the above copyright 44 * notice, this list of conditions and the following disclaimer. 45 * 2. Redistributions in binary form must reproduce the above copyright 46 * notice, this list of conditions and the following disclaimer in the 47 * documentation and/or other materials provided with the distribution. 48 * 3. All advertising materials mentioning features or use of this software 49 * must display the following acknowledgement: 50 * This product includes software developed by the University of 51 * California, Berkeley and its contributors. 52 * 4. Neither the name of the University nor the names of its contributors 53 * may be used to endorse or promote products derived from this software 54 * without specific prior written permission. 55 * 56 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 57 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 58 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 59 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 60 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 61 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 62 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 63 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 64 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 65 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 66 * SUCH DAMAGE. 67 * 68 * @(#)vfs_cache.c 8.5 (Berkeley) 3/22/95 69 * $FreeBSD: src/sys/kern/vfs_cache.c,v 1.42.2.6 2001/10/05 20:07:03 dillon Exp $ 70 * $DragonFly: src/sys/kern/vfs_cache.c,v 1.57 2005/08/27 20:23:05 joerg Exp $ 71 */ 72 73 #include <sys/param.h> 74 #include <sys/systm.h> 75 #include <sys/kernel.h> 76 #include <sys/sysctl.h> 77 #include <sys/mount.h> 78 #include <sys/vnode.h> 79 #include <sys/malloc.h> 80 #include <sys/sysproto.h> 81 #include <sys/proc.h> 82 #include <sys/namei.h> 83 #include <sys/nlookup.h> 84 #include <sys/filedesc.h> 85 #include <sys/fnv_hash.h> 86 #include <sys/globaldata.h> 87 #include <sys/kern_syscall.h> 88 #include <sys/dirent.h> 89 #include <ddb/ddb.h> 90 91 /* 92 * Random lookups in the cache are accomplished with a hash table using 93 * a hash key of (nc_src_vp, name). 94 * 95 * Negative entries may exist and correspond to structures where nc_vp 96 * is NULL. In a negative entry, NCF_WHITEOUT will be set if the entry 97 * corresponds to a whited-out directory entry (verses simply not finding the 98 * entry at all). 99 * 100 * Upon reaching the last segment of a path, if the reference is for DELETE, 101 * or NOCACHE is set (rewrite), and the name is located in the cache, it 102 * will be dropped. 103 */ 104 105 /* 106 * Structures associated with name cacheing. 107 */ 108 #define NCHHASH(hash) (&nchashtbl[(hash) & nchash]) 109 #define MINNEG 1024 110 111 MALLOC_DEFINE(M_VFSCACHE, "vfscache", "VFS name cache entries"); 112 113 static LIST_HEAD(nchashhead, namecache) *nchashtbl; /* Hash Table */ 114 static struct namecache_list ncneglist; /* instead of vnode */ 115 static int64_t last_fsmid; /* node change id */ 116 117 /* 118 * ncvp_debug - debug cache_fromvp(). This is used by the NFS server 119 * to create the namecache infrastructure leading to a dangling vnode. 120 * 121 * 0 Only errors are reported 122 * 1 Successes are reported 123 * 2 Successes + the whole directory scan is reported 124 * 3 Force the directory scan code run as if the parent vnode did not 125 * have a namecache record, even if it does have one. 126 */ 127 static int ncvp_debug; 128 SYSCTL_INT(_debug, OID_AUTO, ncvp_debug, CTLFLAG_RW, &ncvp_debug, 0, ""); 129 130 static u_long nchash; /* size of hash table */ 131 SYSCTL_ULONG(_debug, OID_AUTO, nchash, CTLFLAG_RD, &nchash, 0, ""); 132 133 static u_long ncnegfactor = 16; /* ratio of negative entries */ 134 SYSCTL_ULONG(_debug, OID_AUTO, ncnegfactor, CTLFLAG_RW, &ncnegfactor, 0, ""); 135 136 static int nclockwarn; /* warn on locked entries in ticks */ 137 SYSCTL_INT(_debug, OID_AUTO, nclockwarn, CTLFLAG_RW, &nclockwarn, 0, ""); 138 139 static u_long numneg; /* number of cache entries allocated */ 140 SYSCTL_ULONG(_debug, OID_AUTO, numneg, CTLFLAG_RD, &numneg, 0, ""); 141 142 static u_long numcache; /* number of cache entries allocated */ 143 SYSCTL_ULONG(_debug, OID_AUTO, numcache, CTLFLAG_RD, &numcache, 0, ""); 144 145 static u_long numunres; /* number of unresolved entries */ 146 SYSCTL_ULONG(_debug, OID_AUTO, numunres, CTLFLAG_RD, &numunres, 0, ""); 147 148 SYSCTL_INT(_debug, OID_AUTO, vnsize, CTLFLAG_RD, 0, sizeof(struct vnode), ""); 149 SYSCTL_INT(_debug, OID_AUTO, ncsize, CTLFLAG_RD, 0, sizeof(struct namecache), ""); 150 151 static int cache_resolve_mp(struct namecache *ncp); 152 static void cache_rehash(struct namecache *ncp); 153 154 /* 155 * The new name cache statistics 156 */ 157 SYSCTL_NODE(_vfs, OID_AUTO, cache, CTLFLAG_RW, 0, "Name cache statistics"); 158 #define STATNODE(mode, name, var) \ 159 SYSCTL_ULONG(_vfs_cache, OID_AUTO, name, mode, var, 0, ""); 160 STATNODE(CTLFLAG_RD, numneg, &numneg); 161 STATNODE(CTLFLAG_RD, numcache, &numcache); 162 static u_long numcalls; STATNODE(CTLFLAG_RD, numcalls, &numcalls); 163 static u_long dothits; STATNODE(CTLFLAG_RD, dothits, &dothits); 164 static u_long dotdothits; STATNODE(CTLFLAG_RD, dotdothits, &dotdothits); 165 static u_long numchecks; STATNODE(CTLFLAG_RD, numchecks, &numchecks); 166 static u_long nummiss; STATNODE(CTLFLAG_RD, nummiss, &nummiss); 167 static u_long nummisszap; STATNODE(CTLFLAG_RD, nummisszap, &nummisszap); 168 static u_long numposzaps; STATNODE(CTLFLAG_RD, numposzaps, &numposzaps); 169 static u_long numposhits; STATNODE(CTLFLAG_RD, numposhits, &numposhits); 170 static u_long numnegzaps; STATNODE(CTLFLAG_RD, numnegzaps, &numnegzaps); 171 static u_long numneghits; STATNODE(CTLFLAG_RD, numneghits, &numneghits); 172 173 struct nchstats nchstats[SMP_MAXCPU]; 174 /* 175 * Export VFS cache effectiveness statistics to user-land. 176 * 177 * The statistics are left for aggregation to user-land so 178 * neat things can be achieved, like observing per-CPU cache 179 * distribution. 180 */ 181 static int 182 sysctl_nchstats(SYSCTL_HANDLER_ARGS) 183 { 184 struct globaldata *gd; 185 int i, error; 186 187 error = 0; 188 for (i = 0; i < ncpus; ++i) { 189 gd = globaldata_find(i); 190 if ((error = SYSCTL_OUT(req, (void *)&(*gd->gd_nchstats), 191 sizeof(struct nchstats)))) 192 break; 193 } 194 195 return (error); 196 } 197 SYSCTL_PROC(_vfs_cache, OID_AUTO, nchstats, CTLTYPE_OPAQUE|CTLFLAG_RD, 198 0, 0, sysctl_nchstats, "S,nchstats", "VFS cache effectiveness statistics"); 199 200 static void cache_zap(struct namecache *ncp); 201 202 /* 203 * cache_hold() and cache_drop() prevent the premature deletion of a 204 * namecache entry but do not prevent operations (such as zapping) on 205 * that namecache entry. 206 */ 207 static __inline 208 struct namecache * 209 _cache_hold(struct namecache *ncp) 210 { 211 ++ncp->nc_refs; 212 return(ncp); 213 } 214 215 /* 216 * When dropping an entry, if only one ref remains and the entry has not 217 * been resolved, zap it. Since the one reference is being dropped the 218 * entry had better not be locked. 219 */ 220 static __inline 221 void 222 _cache_drop(struct namecache *ncp) 223 { 224 KKASSERT(ncp->nc_refs > 0); 225 if (ncp->nc_refs == 1 && 226 (ncp->nc_flag & NCF_UNRESOLVED) && 227 TAILQ_EMPTY(&ncp->nc_list) 228 ) { 229 KKASSERT(ncp->nc_exlocks == 0); 230 cache_lock(ncp); 231 cache_zap(ncp); 232 } else { 233 --ncp->nc_refs; 234 } 235 } 236 237 /* 238 * Link a new namecache entry to its parent. Be careful to avoid races 239 * if vhold() blocks in the future. 240 * 241 * If we are creating a child under an oldapi parent we must mark the 242 * child as being an oldapi entry as well. 243 */ 244 static void 245 cache_link_parent(struct namecache *ncp, struct namecache *par) 246 { 247 KKASSERT(ncp->nc_parent == NULL); 248 ncp->nc_parent = par; 249 if (TAILQ_EMPTY(&par->nc_list)) { 250 TAILQ_INSERT_HEAD(&par->nc_list, ncp, nc_entry); 251 /* 252 * Any vp associated with an ncp which has children must 253 * be held to prevent it from being recycled. 254 */ 255 if (par->nc_vp) 256 vhold(par->nc_vp); 257 } else { 258 TAILQ_INSERT_HEAD(&par->nc_list, ncp, nc_entry); 259 } 260 } 261 262 /* 263 * Remove the parent association from a namecache structure. If this is 264 * the last child of the parent the cache_drop(par) will attempt to 265 * recursively zap the parent. 266 */ 267 static void 268 cache_unlink_parent(struct namecache *ncp) 269 { 270 struct namecache *par; 271 272 if ((par = ncp->nc_parent) != NULL) { 273 ncp->nc_parent = NULL; 274 par = cache_hold(par); 275 TAILQ_REMOVE(&par->nc_list, ncp, nc_entry); 276 if (par->nc_vp && TAILQ_EMPTY(&par->nc_list)) 277 vdrop(par->nc_vp); 278 cache_drop(par); 279 } 280 } 281 282 /* 283 * Allocate a new namecache structure. Most of the code does not require 284 * zero-termination of the string but it makes vop_compat_ncreate() easier. 285 */ 286 static struct namecache * 287 cache_alloc(int nlen) 288 { 289 struct namecache *ncp; 290 291 ncp = malloc(sizeof(*ncp), M_VFSCACHE, M_WAITOK|M_ZERO); 292 if (nlen) 293 ncp->nc_name = malloc(nlen + 1, M_VFSCACHE, M_WAITOK); 294 ncp->nc_nlen = nlen; 295 ncp->nc_flag = NCF_UNRESOLVED; 296 ncp->nc_error = ENOTCONN; /* needs to be resolved */ 297 ncp->nc_refs = 1; 298 ncp->nc_fsmid = ++last_fsmid; 299 TAILQ_INIT(&ncp->nc_list); 300 cache_lock(ncp); 301 return(ncp); 302 } 303 304 static void 305 cache_free(struct namecache *ncp) 306 { 307 KKASSERT(ncp->nc_refs == 1 && ncp->nc_exlocks == 1); 308 if (ncp->nc_name) 309 free(ncp->nc_name, M_VFSCACHE); 310 free(ncp, M_VFSCACHE); 311 } 312 313 /* 314 * Ref and deref a namecache structure. 315 */ 316 struct namecache * 317 cache_hold(struct namecache *ncp) 318 { 319 return(_cache_hold(ncp)); 320 } 321 322 void 323 cache_drop(struct namecache *ncp) 324 { 325 _cache_drop(ncp); 326 } 327 328 /* 329 * Namespace locking. The caller must already hold a reference to the 330 * namecache structure in order to lock/unlock it. This function prevents 331 * the namespace from being created or destroyed by accessors other then 332 * the lock holder. 333 * 334 * Note that holding a locked namecache structure prevents other threads 335 * from making namespace changes (e.g. deleting or creating), prevents 336 * vnode association state changes by other threads, and prevents the 337 * namecache entry from being resolved or unresolved by other threads. 338 * 339 * The lock owner has full authority to associate/disassociate vnodes 340 * and resolve/unresolve the locked ncp. 341 * 342 * In particular, if a vnode is associated with a locked cache entry 343 * that vnode will *NOT* be recycled. We accomplish this by vhold()ing the 344 * vnode. XXX we should find a more efficient way to prevent the vnode 345 * from being recycled, but remember that any given vnode may have multiple 346 * namecache associations (think hardlinks). 347 */ 348 void 349 cache_lock(struct namecache *ncp) 350 { 351 thread_t td; 352 int didwarn; 353 354 KKASSERT(ncp->nc_refs != 0); 355 didwarn = 0; 356 td = curthread; 357 358 for (;;) { 359 if (ncp->nc_exlocks == 0) { 360 ncp->nc_exlocks = 1; 361 ncp->nc_locktd = td; 362 /* 363 * The vp associated with a locked ncp must be held 364 * to prevent it from being recycled (which would 365 * cause the ncp to become unresolved). 366 * 367 * XXX loop on race for later MPSAFE work. 368 */ 369 if (ncp->nc_vp) 370 vhold(ncp->nc_vp); 371 break; 372 } 373 if (ncp->nc_locktd == td) { 374 ++ncp->nc_exlocks; 375 break; 376 } 377 ncp->nc_flag |= NCF_LOCKREQ; 378 if (tsleep(ncp, 0, "clock", nclockwarn) == EWOULDBLOCK) { 379 if (didwarn) 380 continue; 381 didwarn = 1; 382 printf("[diagnostic] cache_lock: blocked on %p", ncp); 383 if ((ncp->nc_flag & NCF_MOUNTPT) && ncp->nc_mount) 384 printf(" [MOUNTFROM %s]\n", ncp->nc_mount->mnt_stat.f_mntfromname); 385 else 386 printf(" \"%*.*s\"\n", 387 ncp->nc_nlen, ncp->nc_nlen, 388 ncp->nc_name); 389 } 390 } 391 392 if (didwarn == 1) { 393 printf("[diagnostic] cache_lock: unblocked %*.*s\n", 394 ncp->nc_nlen, ncp->nc_nlen, ncp->nc_name); 395 } 396 } 397 398 int 399 cache_lock_nonblock(struct namecache *ncp) 400 { 401 thread_t td; 402 403 KKASSERT(ncp->nc_refs != 0); 404 td = curthread; 405 if (ncp->nc_exlocks == 0) { 406 ncp->nc_exlocks = 1; 407 ncp->nc_locktd = td; 408 /* 409 * The vp associated with a locked ncp must be held 410 * to prevent it from being recycled (which would 411 * cause the ncp to become unresolved). 412 * 413 * XXX loop on race for later MPSAFE work. 414 */ 415 if (ncp->nc_vp) 416 vhold(ncp->nc_vp); 417 return(0); 418 } else { 419 return(EWOULDBLOCK); 420 } 421 } 422 423 void 424 cache_unlock(struct namecache *ncp) 425 { 426 thread_t td = curthread; 427 428 KKASSERT(ncp->nc_refs > 0); 429 KKASSERT(ncp->nc_exlocks > 0); 430 KKASSERT(ncp->nc_locktd == td); 431 if (--ncp->nc_exlocks == 0) { 432 if (ncp->nc_vp) 433 vdrop(ncp->nc_vp); 434 ncp->nc_locktd = NULL; 435 if (ncp->nc_flag & NCF_LOCKREQ) { 436 ncp->nc_flag &= ~NCF_LOCKREQ; 437 wakeup(ncp); 438 } 439 } 440 } 441 442 /* 443 * ref-and-lock, unlock-and-deref functions. 444 */ 445 struct namecache * 446 cache_get(struct namecache *ncp) 447 { 448 _cache_hold(ncp); 449 cache_lock(ncp); 450 return(ncp); 451 } 452 453 int 454 cache_get_nonblock(struct namecache *ncp) 455 { 456 /* XXX MP */ 457 if (ncp->nc_exlocks == 0 || ncp->nc_locktd == curthread) { 458 _cache_hold(ncp); 459 cache_lock(ncp); 460 return(0); 461 } 462 return(EWOULDBLOCK); 463 } 464 465 void 466 cache_put(struct namecache *ncp) 467 { 468 cache_unlock(ncp); 469 _cache_drop(ncp); 470 } 471 472 /* 473 * Resolve an unresolved ncp by associating a vnode with it. If the 474 * vnode is NULL, a negative cache entry is created. 475 * 476 * The ncp should be locked on entry and will remain locked on return. 477 */ 478 void 479 cache_setvp(struct namecache *ncp, struct vnode *vp) 480 { 481 KKASSERT(ncp->nc_flag & NCF_UNRESOLVED); 482 ncp->nc_vp = vp; 483 if (vp != NULL) { 484 /* 485 * Any vp associated with an ncp which has children must 486 * be held. Any vp associated with a locked ncp must be held. 487 */ 488 if (!TAILQ_EMPTY(&ncp->nc_list)) 489 vhold(vp); 490 TAILQ_INSERT_HEAD(&vp->v_namecache, ncp, nc_vnode); 491 if (ncp->nc_exlocks) 492 vhold(vp); 493 494 /* 495 * Set auxillary flags 496 */ 497 switch(vp->v_type) { 498 case VDIR: 499 ncp->nc_flag |= NCF_ISDIR; 500 break; 501 case VLNK: 502 ncp->nc_flag |= NCF_ISSYMLINK; 503 /* XXX cache the contents of the symlink */ 504 break; 505 default: 506 break; 507 } 508 ++numcache; 509 ncp->nc_error = 0; 510 } else { 511 TAILQ_INSERT_TAIL(&ncneglist, ncp, nc_vnode); 512 ++numneg; 513 ncp->nc_error = ENOENT; 514 } 515 ncp->nc_flag &= ~NCF_UNRESOLVED; 516 } 517 518 void 519 cache_settimeout(struct namecache *ncp, int nticks) 520 { 521 if ((ncp->nc_timeout = ticks + nticks) == 0) 522 ncp->nc_timeout = 1; 523 } 524 525 /* 526 * Disassociate the vnode or negative-cache association and mark a 527 * namecache entry as unresolved again. Note that the ncp is still 528 * left in the hash table and still linked to its parent. 529 * 530 * The ncp should be locked and refd on entry and will remain locked and refd 531 * on return. 532 * 533 * This routine is normally never called on a directory containing children. 534 * However, NFS often does just that in its rename() code as a cop-out to 535 * avoid complex namespace operations. This disconnects a directory vnode 536 * from its namecache and can cause the OLDAPI and NEWAPI to get out of 537 * sync. 538 */ 539 void 540 cache_setunresolved(struct namecache *ncp) 541 { 542 struct vnode *vp; 543 544 if ((ncp->nc_flag & NCF_UNRESOLVED) == 0) { 545 ncp->nc_flag |= NCF_UNRESOLVED; 546 ncp->nc_flag &= ~(NCF_WHITEOUT|NCF_ISDIR|NCF_ISSYMLINK); 547 ncp->nc_timeout = 0; 548 ncp->nc_error = ENOTCONN; 549 ++numunres; 550 if ((vp = ncp->nc_vp) != NULL) { 551 --numcache; 552 ncp->nc_vp = NULL; 553 TAILQ_REMOVE(&vp->v_namecache, ncp, nc_vnode); 554 555 /* 556 * Any vp associated with an ncp with children is 557 * held by that ncp. Any vp associated with a locked 558 * ncp is held by that ncp. These conditions must be 559 * undone when the vp is cleared out from the ncp. 560 */ 561 if (!TAILQ_EMPTY(&ncp->nc_list)) 562 vdrop(vp); 563 if (ncp->nc_exlocks) 564 vdrop(vp); 565 } else { 566 TAILQ_REMOVE(&ncneglist, ncp, nc_vnode); 567 --numneg; 568 } 569 } 570 } 571 572 /* 573 * Invalidate portions of the namecache topology given a starting entry. 574 * The passed ncp is set to an unresolved state and: 575 * 576 * The passed ncp must be locked. 577 * 578 * CINV_DESTROY - Set a flag in the passed ncp entry indicating 579 * that the physical underlying nodes have been 580 * destroyed... as in deleted. For example, when 581 * a directory is removed. This will cause record 582 * lookups on the name to no longer be able to find 583 * the record and tells the resolver to return failure 584 * rather then trying to resolve through the parent. 585 * 586 * The topology itself, including ncp->nc_name, 587 * remains intact. 588 * 589 * This only applies to the passed ncp, if CINV_CHILDREN 590 * is specified the children are not flagged. 591 * 592 * CINV_CHILDREN - Set all children (recursively) to an unresolved 593 * state as well. 594 * 595 * Note that this will also have the side effect of 596 * cleaning out any unreferenced nodes in the topology 597 * from the leaves up as the recursion backs out. 598 * 599 * Note that the topology for any referenced nodes remains intact. 600 * 601 * It is possible for cache_inval() to race a cache_resolve(), meaning that 602 * the namecache entry may not actually be invalidated on return if it was 603 * revalidated while recursing down into its children. This code guarentees 604 * that the node(s) will go through an invalidation cycle, but does not 605 * guarentee that they will remain in an invalidated state. 606 * 607 * Returns non-zero if a revalidation was detected during the invalidation 608 * recursion, zero otherwise. Note that since only the original ncp is 609 * locked the revalidation ultimately can only indicate that the original ncp 610 * *MIGHT* no have been reresolved. 611 */ 612 int 613 cache_inval(struct namecache *ncp, int flags) 614 { 615 struct namecache *kid; 616 struct namecache *nextkid; 617 int rcnt = 0; 618 619 KKASSERT(ncp->nc_exlocks); 620 621 cache_setunresolved(ncp); 622 if (flags & CINV_DESTROY) 623 ncp->nc_flag |= NCF_DESTROYED; 624 625 if ((flags & CINV_CHILDREN) && 626 (kid = TAILQ_FIRST(&ncp->nc_list)) != NULL 627 ) { 628 cache_hold(kid); 629 cache_unlock(ncp); 630 while (kid) { 631 if ((nextkid = TAILQ_NEXT(kid, nc_entry)) != NULL) 632 cache_hold(nextkid); 633 if ((kid->nc_flag & NCF_UNRESOLVED) == 0 || 634 TAILQ_FIRST(&kid->nc_list) 635 ) { 636 cache_lock(kid); 637 rcnt += cache_inval(kid, flags & ~CINV_DESTROY); 638 cache_unlock(kid); 639 } 640 cache_drop(kid); 641 kid = nextkid; 642 } 643 cache_lock(ncp); 644 } 645 646 /* 647 * Someone could have gotten in there while ncp was unlocked, 648 * retry if so. 649 */ 650 if ((ncp->nc_flag & NCF_UNRESOLVED) == 0) 651 ++rcnt; 652 return (rcnt); 653 } 654 655 /* 656 * Invalidate a vnode's namecache associations. To avoid races against 657 * the resolver we do not invalidate a node which we previously invalidated 658 * but which was then re-resolved while we were in the invalidation loop. 659 * 660 * Returns non-zero if any namecache entries remain after the invalidation 661 * loop completed. 662 * 663 * NOTE: unlike the namecache topology which guarentees that ncp's will not 664 * be ripped out of the topology while held, the vnode's v_namecache list 665 * has no such restriction. NCP's can be ripped out of the list at virtually 666 * any time if not locked, even if held. 667 */ 668 int 669 cache_inval_vp(struct vnode *vp, int flags) 670 { 671 struct namecache *ncp; 672 struct namecache *next; 673 674 restart: 675 ncp = TAILQ_FIRST(&vp->v_namecache); 676 if (ncp) 677 cache_hold(ncp); 678 while (ncp) { 679 /* loop entered with ncp held */ 680 if ((next = TAILQ_NEXT(ncp, nc_vnode)) != NULL) 681 cache_hold(next); 682 cache_lock(ncp); 683 if (ncp->nc_vp != vp) { 684 printf("Warning: cache_inval_vp: race-A detected on " 685 "%s\n", ncp->nc_name); 686 cache_put(ncp); 687 if (next) 688 cache_drop(next); 689 goto restart; 690 } 691 cache_inval(ncp, flags); 692 cache_put(ncp); /* also releases reference */ 693 ncp = next; 694 if (ncp && ncp->nc_vp != vp) { 695 printf("Warning: cache_inval_vp: race-B detected on " 696 "%s\n", ncp->nc_name); 697 cache_drop(ncp); 698 goto restart; 699 } 700 } 701 return(TAILQ_FIRST(&vp->v_namecache) != NULL); 702 } 703 704 /* 705 * The source ncp has been renamed to the target ncp. Both fncp and tncp 706 * must be locked. Both will be set to unresolved, any children of tncp 707 * will be disconnected (the prior contents of the target is assumed to be 708 * destroyed by the rename operation, e.g. renaming over an empty directory), 709 * and all children of fncp will be moved to tncp. 710 * 711 * XXX the disconnection could pose a problem, check code paths to make 712 * sure any code that blocks can handle the parent being changed out from 713 * under it. Maybe we should lock the children (watch out for deadlocks) ? 714 * 715 * After we return the caller has the option of calling cache_setvp() if 716 * the vnode of the new target ncp is known. 717 * 718 * Any process CD'd into any of the children will no longer be able to ".." 719 * back out. An rm -rf can cause this situation to occur. 720 */ 721 void 722 cache_rename(struct namecache *fncp, struct namecache *tncp) 723 { 724 struct namecache *scan; 725 int didwarn = 0; 726 727 cache_setunresolved(fncp); 728 cache_setunresolved(tncp); 729 while (cache_inval(tncp, CINV_CHILDREN) != 0) { 730 if (didwarn++ % 10 == 0) { 731 printf("Warning: cache_rename: race during " 732 "rename %s->%s\n", 733 fncp->nc_name, tncp->nc_name); 734 } 735 tsleep(tncp, 0, "mvrace", hz / 10); 736 cache_setunresolved(tncp); 737 } 738 while ((scan = TAILQ_FIRST(&fncp->nc_list)) != NULL) { 739 cache_hold(scan); 740 cache_unlink_parent(scan); 741 cache_link_parent(scan, tncp); 742 if (scan->nc_flag & NCF_HASHED) 743 cache_rehash(scan); 744 cache_drop(scan); 745 } 746 } 747 748 /* 749 * vget the vnode associated with the namecache entry. Resolve the namecache 750 * entry if necessary and deal with namecache/vp races. The passed ncp must 751 * be referenced and may be locked. The ncp's ref/locking state is not 752 * effected by this call. 753 * 754 * lk_type may be LK_SHARED, LK_EXCLUSIVE. A ref'd, possibly locked 755 * (depending on the passed lk_type) will be returned in *vpp with an error 756 * of 0, or NULL will be returned in *vpp with a non-0 error code. The 757 * most typical error is ENOENT, meaning that the ncp represents a negative 758 * cache hit and there is no vnode to retrieve, but other errors can occur 759 * too. 760 * 761 * The main race we have to deal with are namecache zaps. The ncp itself 762 * will not disappear since it is referenced, and it turns out that the 763 * validity of the vp pointer can be checked simply by rechecking the 764 * contents of ncp->nc_vp. 765 */ 766 int 767 cache_vget(struct namecache *ncp, struct ucred *cred, 768 int lk_type, struct vnode **vpp) 769 { 770 struct vnode *vp; 771 int error; 772 773 again: 774 vp = NULL; 775 if (ncp->nc_flag & NCF_UNRESOLVED) { 776 cache_lock(ncp); 777 error = cache_resolve(ncp, cred); 778 cache_unlock(ncp); 779 } else { 780 error = 0; 781 } 782 if (error == 0 && (vp = ncp->nc_vp) != NULL) { 783 error = vget(vp, lk_type, curthread); 784 if (error) { 785 if (vp != ncp->nc_vp) /* handle cache_zap race */ 786 goto again; 787 vp = NULL; 788 } else if (vp != ncp->nc_vp) { /* handle cache_zap race */ 789 vput(vp); 790 goto again; 791 } 792 } 793 if (error == 0 && vp == NULL) 794 error = ENOENT; 795 *vpp = vp; 796 return(error); 797 } 798 799 int 800 cache_vref(struct namecache *ncp, struct ucred *cred, struct vnode **vpp) 801 { 802 struct vnode *vp; 803 int error; 804 805 again: 806 vp = NULL; 807 if (ncp->nc_flag & NCF_UNRESOLVED) { 808 cache_lock(ncp); 809 error = cache_resolve(ncp, cred); 810 cache_unlock(ncp); 811 } else { 812 error = 0; 813 } 814 if (error == 0 && (vp = ncp->nc_vp) != NULL) { 815 vref(vp); 816 if (vp != ncp->nc_vp) { /* handle cache_zap race */ 817 vrele(vp); 818 goto again; 819 } 820 } 821 if (error == 0 && vp == NULL) 822 error = ENOENT; 823 *vpp = vp; 824 return(error); 825 } 826 827 void 828 cache_update_fsmid(struct namecache *ncp) 829 { 830 struct vnode *vp; 831 struct namecache *scan; 832 int64_t fsmid = ++last_fsmid; 833 834 if ((vp = ncp->nc_vp) != NULL) { 835 TAILQ_FOREACH(ncp, &vp->v_namecache, nc_vnode) { 836 for (scan = ncp; scan; scan = scan->nc_parent) 837 scan->nc_fsmid = fsmid; 838 } 839 } else { 840 while (ncp) { 841 ncp->nc_fsmid = fsmid; 842 ncp = ncp->nc_parent; 843 } 844 } 845 } 846 847 void 848 cache_update_fsmid_vp(struct vnode *vp) 849 { 850 struct namecache *ncp; 851 struct namecache *scan; 852 int64_t fsmid = ++last_fsmid; 853 854 TAILQ_FOREACH(ncp, &vp->v_namecache, nc_vnode) { 855 for (scan = ncp; scan; scan = scan->nc_parent) 856 scan->nc_fsmid = fsmid; 857 } 858 } 859 860 /* 861 * Convert a directory vnode to a namecache record without any other 862 * knowledge of the topology. This ONLY works with directory vnodes and 863 * is ONLY used by the NFS server. dvp must be refd but unlocked, and the 864 * returned ncp (if not NULL) will be held and unlocked. 865 * 866 * If 'makeit' is 0 and dvp has no existing namecache record, NULL is returned. 867 * If 'makeit' is 1 we attempt to track-down and create the namecache topology 868 * for dvp. This will fail only if the directory has been deleted out from 869 * under the caller. 870 * 871 * Callers must always check for a NULL return no matter the value of 'makeit'. 872 */ 873 874 static int cache_inefficient_scan(struct namecache *ncp, struct ucred *cred, 875 struct vnode *dvp); 876 877 struct namecache * 878 cache_fromdvp(struct vnode *dvp, struct ucred *cred, int makeit) 879 { 880 struct namecache *ncp; 881 struct vnode *pvp; 882 int error; 883 884 /* 885 * Temporary debugging code to force the directory scanning code 886 * to be exercised. 887 */ 888 ncp = NULL; 889 if (ncvp_debug >= 3 && makeit && TAILQ_FIRST(&dvp->v_namecache)) { 890 ncp = TAILQ_FIRST(&dvp->v_namecache); 891 printf("cache_fromdvp: forcing %s\n", ncp->nc_name); 892 goto force; 893 } 894 895 /* 896 * Loop until resolution, inside code will break out on error. 897 */ 898 while ((ncp = TAILQ_FIRST(&dvp->v_namecache)) == NULL && makeit) { 899 force: 900 /* 901 * If dvp is the root of its filesystem it should already 902 * have a namecache pointer associated with it as a side 903 * effect of the mount, but it may have been disassociated. 904 */ 905 if (dvp->v_flag & VROOT) { 906 ncp = cache_get(dvp->v_mount->mnt_ncp); 907 error = cache_resolve_mp(ncp); 908 cache_put(ncp); 909 if (ncvp_debug) { 910 printf("cache_fromdvp: resolve root of mount %p error %d", 911 dvp->v_mount, error); 912 } 913 if (error) { 914 if (ncvp_debug) 915 printf(" failed\n"); 916 ncp = NULL; 917 break; 918 } 919 if (ncvp_debug) 920 printf(" succeeded\n"); 921 continue; 922 } 923 924 /* 925 * Get the parent directory and resolve its ncp. 926 */ 927 error = vop_nlookupdotdot(*dvp->v_ops, dvp, &pvp, cred); 928 if (error) { 929 printf("lookupdotdot failed %d %p\n", error, pvp); 930 break; 931 } 932 VOP_UNLOCK(pvp, 0, curthread); 933 934 /* 935 * XXX this recursion could run the kernel out of stack, 936 * change to a less efficient algorithm if we get too deep 937 * (use 'makeit' for a depth counter?) 938 */ 939 ncp = cache_fromdvp(pvp, cred, makeit); 940 vrele(pvp); 941 if (ncp == NULL) 942 break; 943 944 /* 945 * Do an inefficient scan of pvp (embodied by ncp) to look 946 * for dvp. This will create a namecache record for dvp on 947 * success. We loop up to recheck on success. 948 * 949 * ncp and dvp are both held but not locked. 950 */ 951 error = cache_inefficient_scan(ncp, cred, dvp); 952 cache_drop(ncp); 953 if (error) { 954 printf("cache_fromdvp: scan %p (%s) failed on dvp=%p\n", 955 pvp, ncp->nc_name, dvp); 956 ncp = NULL; 957 break; 958 } 959 if (ncvp_debug) { 960 printf("cache_fromdvp: scan %p (%s) succeeded\n", 961 pvp, ncp->nc_name); 962 } 963 } 964 if (ncp) 965 cache_hold(ncp); 966 return (ncp); 967 } 968 969 /* 970 * Do an inefficient scan of the directory represented by ncp looking for 971 * the directory vnode dvp. ncp must be held but not locked on entry and 972 * will be held on return. dvp must be refd but not locked on entry and 973 * will remain refd on return. 974 * 975 * Why do this at all? Well, due to its stateless nature the NFS server 976 * converts file handles directly to vnodes without necessarily going through 977 * the namecache ops that would otherwise create the namecache topology 978 * leading to the vnode. We could either (1) Change the namecache algorithms 979 * to allow disconnect namecache records that are re-merged opportunistically, 980 * or (2) Make the NFS server backtrack and scan to recover a connected 981 * namecache topology in order to then be able to issue new API lookups. 982 * 983 * It turns out that (1) is a huge mess. It takes a nice clean set of 984 * namecache algorithms and introduces a lot of complication in every subsystem 985 * that calls into the namecache to deal with the re-merge case, especially 986 * since we are using the namecache to placehold negative lookups and the 987 * vnode might not be immediately assigned. (2) is certainly far less 988 * efficient then (1), but since we are only talking about directories here 989 * (which are likely to remain cached), the case does not actually run all 990 * that often and has the supreme advantage of not polluting the namecache 991 * algorithms. 992 */ 993 static int 994 cache_inefficient_scan(struct namecache *ncp, struct ucred *cred, 995 struct vnode *dvp) 996 { 997 struct nlcomponent nlc; 998 struct namecache *rncp; 999 struct dirent *den; 1000 struct vnode *pvp; 1001 struct vattr vat; 1002 struct iovec iov; 1003 struct uio uio; 1004 int blksize; 1005 int eofflag; 1006 int bytes; 1007 char *rbuf; 1008 int error; 1009 1010 vat.va_blocksize = 0; 1011 if ((error = VOP_GETATTR(dvp, &vat, curthread)) != 0) 1012 return (error); 1013 if ((error = cache_vget(ncp, cred, LK_SHARED, &pvp)) != 0) 1014 return (error); 1015 if (ncvp_debug) 1016 printf("inefficient_scan: directory iosize %ld vattr fileid = %ld\n", vat.va_blocksize, (long)vat.va_fileid); 1017 if ((blksize = vat.va_blocksize) == 0) 1018 blksize = DEV_BSIZE; 1019 rbuf = malloc(blksize, M_TEMP, M_WAITOK); 1020 rncp = NULL; 1021 1022 eofflag = 0; 1023 uio.uio_offset = 0; 1024 again: 1025 iov.iov_base = rbuf; 1026 iov.iov_len = blksize; 1027 uio.uio_iov = &iov; 1028 uio.uio_iovcnt = 1; 1029 uio.uio_resid = blksize; 1030 uio.uio_segflg = UIO_SYSSPACE; 1031 uio.uio_rw = UIO_READ; 1032 uio.uio_td = curthread; 1033 1034 if (ncvp_debug >= 2) 1035 printf("cache_inefficient_scan: readdir @ %08x\n", (int)uio.uio_offset); 1036 error = VOP_READDIR(pvp, &uio, cred, &eofflag, NULL, NULL); 1037 if (error == 0) { 1038 den = (struct dirent *)rbuf; 1039 bytes = blksize - uio.uio_resid; 1040 1041 while (bytes > 0) { 1042 if (ncvp_debug >= 2) { 1043 printf("cache_inefficient_scan: %*.*s\n", 1044 den->d_namlen, den->d_namlen, 1045 den->d_name); 1046 } 1047 if (den->d_type != DT_WHT && 1048 den->d_ino == vat.va_fileid) { 1049 if (ncvp_debug) { 1050 printf("cache_inefficient_scan: " 1051 "MATCHED inode %ld path %s/%*.*s\n", 1052 vat.va_fileid, ncp->nc_name, 1053 den->d_namlen, den->d_namlen, 1054 den->d_name); 1055 } 1056 nlc.nlc_nameptr = den->d_name; 1057 nlc.nlc_namelen = den->d_namlen; 1058 VOP_UNLOCK(pvp, 0, curthread); 1059 rncp = cache_nlookup(ncp, &nlc); 1060 KKASSERT(rncp != NULL); 1061 break; 1062 } 1063 bytes -= _DIRENT_DIRSIZ(den); 1064 den = _DIRENT_NEXT(den); 1065 } 1066 if (rncp == NULL && eofflag == 0 && uio.uio_resid != blksize) 1067 goto again; 1068 } 1069 if (rncp) { 1070 vrele(pvp); 1071 if (rncp->nc_flag & NCF_UNRESOLVED) { 1072 cache_setvp(rncp, dvp); 1073 if (ncvp_debug >= 2) { 1074 printf("cache_inefficient_scan: setvp %s/%s = %p\n", 1075 ncp->nc_name, rncp->nc_name, dvp); 1076 } 1077 } else { 1078 if (ncvp_debug >= 2) { 1079 printf("cache_inefficient_scan: setvp %s/%s already set %p/%p\n", 1080 ncp->nc_name, rncp->nc_name, dvp, 1081 rncp->nc_vp); 1082 } 1083 } 1084 if (rncp->nc_vp == NULL) 1085 error = rncp->nc_error; 1086 cache_put(rncp); 1087 } else { 1088 printf("cache_inefficient_scan: dvp %p NOT FOUND in %s\n", 1089 dvp, ncp->nc_name); 1090 vput(pvp); 1091 error = ENOENT; 1092 } 1093 free(rbuf, M_TEMP); 1094 return (error); 1095 } 1096 1097 /* 1098 * Zap a namecache entry. The ncp is unconditionally set to an unresolved 1099 * state, which disassociates it from its vnode or ncneglist. 1100 * 1101 * Then, if there are no additional references to the ncp and no children, 1102 * the ncp is removed from the topology and destroyed. This function will 1103 * also run through the nc_parent chain and destroy parent ncps if possible. 1104 * As a side benefit, it turns out the only conditions that allow running 1105 * up the chain are also the conditions to ensure no deadlock will occur. 1106 * 1107 * References and/or children may exist if the ncp is in the middle of the 1108 * topology, preventing the ncp from being destroyed. 1109 * 1110 * This function must be called with the ncp held and locked and will unlock 1111 * and drop it during zapping. 1112 */ 1113 static void 1114 cache_zap(struct namecache *ncp) 1115 { 1116 struct namecache *par; 1117 1118 /* 1119 * Disassociate the vnode or negative cache ref and set NCF_UNRESOLVED. 1120 */ 1121 cache_setunresolved(ncp); 1122 1123 /* 1124 * Try to scrap the entry and possibly tail-recurse on its parent. 1125 * We only scrap unref'd (other then our ref) unresolved entries, 1126 * we do not scrap 'live' entries. 1127 */ 1128 while (ncp->nc_flag & NCF_UNRESOLVED) { 1129 /* 1130 * Someone other then us has a ref, stop. 1131 */ 1132 if (ncp->nc_refs > 1) 1133 goto done; 1134 1135 /* 1136 * We have children, stop. 1137 */ 1138 if (!TAILQ_EMPTY(&ncp->nc_list)) 1139 goto done; 1140 1141 /* 1142 * Remove ncp from the topology: hash table and parent linkage. 1143 */ 1144 if (ncp->nc_flag & NCF_HASHED) { 1145 ncp->nc_flag &= ~NCF_HASHED; 1146 LIST_REMOVE(ncp, nc_hash); 1147 } 1148 if ((par = ncp->nc_parent) != NULL) { 1149 par = cache_hold(par); 1150 TAILQ_REMOVE(&par->nc_list, ncp, nc_entry); 1151 ncp->nc_parent = NULL; 1152 if (par->nc_vp && TAILQ_EMPTY(&par->nc_list)) 1153 vdrop(par->nc_vp); 1154 } 1155 1156 /* 1157 * ncp should not have picked up any refs. Physically 1158 * destroy the ncp. 1159 */ 1160 KKASSERT(ncp->nc_refs == 1); 1161 --numunres; 1162 /* cache_unlock(ncp) not required */ 1163 ncp->nc_refs = -1; /* safety */ 1164 if (ncp->nc_name) 1165 free(ncp->nc_name, M_VFSCACHE); 1166 free(ncp, M_VFSCACHE); 1167 1168 /* 1169 * Loop on the parent (it may be NULL). Only bother looping 1170 * if the parent has a single ref (ours), which also means 1171 * we can lock it trivially. 1172 */ 1173 ncp = par; 1174 if (ncp == NULL) 1175 return; 1176 if (ncp->nc_refs != 1) { 1177 cache_drop(ncp); 1178 return; 1179 } 1180 KKASSERT(par->nc_exlocks == 0); 1181 cache_lock(ncp); 1182 } 1183 done: 1184 cache_unlock(ncp); 1185 --ncp->nc_refs; 1186 } 1187 1188 static enum { CHI_LOW, CHI_HIGH } cache_hysteresis_state = CHI_LOW; 1189 1190 static __inline 1191 void 1192 cache_hysteresis(void) 1193 { 1194 /* 1195 * Don't cache too many negative hits. We use hysteresis to reduce 1196 * the impact on the critical path. 1197 */ 1198 switch(cache_hysteresis_state) { 1199 case CHI_LOW: 1200 if (numneg > MINNEG && numneg * ncnegfactor > numcache) { 1201 cache_cleanneg(10); 1202 cache_hysteresis_state = CHI_HIGH; 1203 } 1204 break; 1205 case CHI_HIGH: 1206 if (numneg > MINNEG * 9 / 10 && 1207 numneg * ncnegfactor * 9 / 10 > numcache 1208 ) { 1209 cache_cleanneg(10); 1210 } else { 1211 cache_hysteresis_state = CHI_LOW; 1212 } 1213 break; 1214 } 1215 } 1216 1217 /* 1218 * NEW NAMECACHE LOOKUP API 1219 * 1220 * Lookup an entry in the cache. A locked, referenced, non-NULL 1221 * entry is *always* returned, even if the supplied component is illegal. 1222 * The resulting namecache entry should be returned to the system with 1223 * cache_put() or cache_unlock() + cache_drop(). 1224 * 1225 * namecache locks are recursive but care must be taken to avoid lock order 1226 * reversals. 1227 * 1228 * Nobody else will be able to manipulate the associated namespace (e.g. 1229 * create, delete, rename, rename-target) until the caller unlocks the 1230 * entry. 1231 * 1232 * The returned entry will be in one of three states: positive hit (non-null 1233 * vnode), negative hit (null vnode), or unresolved (NCF_UNRESOLVED is set). 1234 * Unresolved entries must be resolved through the filesystem to associate the 1235 * vnode and/or determine whether a positive or negative hit has occured. 1236 * 1237 * It is not necessary to lock a directory in order to lock namespace under 1238 * that directory. In fact, it is explicitly not allowed to do that. A 1239 * directory is typically only locked when being created, renamed, or 1240 * destroyed. 1241 * 1242 * The directory (par) may be unresolved, in which case any returned child 1243 * will likely also be marked unresolved. Likely but not guarenteed. Since 1244 * the filesystem lookup requires a resolved directory vnode the caller is 1245 * responsible for resolving the namecache chain top-down. This API 1246 * specifically allows whole chains to be created in an unresolved state. 1247 */ 1248 struct namecache * 1249 cache_nlookup(struct namecache *par, struct nlcomponent *nlc) 1250 { 1251 struct namecache *ncp; 1252 struct namecache *new_ncp; 1253 struct nchashhead *nchpp; 1254 u_int32_t hash; 1255 globaldata_t gd; 1256 1257 numcalls++; 1258 gd = mycpu; 1259 1260 /* 1261 * Try to locate an existing entry 1262 */ 1263 hash = fnv_32_buf(nlc->nlc_nameptr, nlc->nlc_namelen, FNV1_32_INIT); 1264 hash = fnv_32_buf(&par, sizeof(par), hash); 1265 new_ncp = NULL; 1266 restart: 1267 LIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) { 1268 numchecks++; 1269 1270 /* 1271 * Zap entries that have timed out. 1272 */ 1273 if (ncp->nc_timeout && 1274 (int)(ncp->nc_timeout - ticks) < 0 && 1275 (ncp->nc_flag & NCF_UNRESOLVED) == 0 && 1276 ncp->nc_exlocks == 0 1277 ) { 1278 cache_zap(cache_get(ncp)); 1279 goto restart; 1280 } 1281 1282 /* 1283 * Break out if we find a matching entry. Note that 1284 * UNRESOLVED entries may match, but DESTROYED entries 1285 * do not. 1286 */ 1287 if (ncp->nc_parent == par && 1288 ncp->nc_nlen == nlc->nlc_namelen && 1289 bcmp(ncp->nc_name, nlc->nlc_nameptr, ncp->nc_nlen) == 0 && 1290 (ncp->nc_flag & NCF_DESTROYED) == 0 1291 ) { 1292 if (cache_get_nonblock(ncp) == 0) { 1293 if (new_ncp) 1294 cache_free(new_ncp); 1295 goto found; 1296 } 1297 cache_get(ncp); 1298 cache_put(ncp); 1299 goto restart; 1300 } 1301 } 1302 1303 /* 1304 * We failed to locate an entry, create a new entry and add it to 1305 * the cache. We have to relookup after possibly blocking in 1306 * malloc. 1307 */ 1308 if (new_ncp == NULL) { 1309 new_ncp = cache_alloc(nlc->nlc_namelen); 1310 goto restart; 1311 } 1312 1313 ncp = new_ncp; 1314 1315 /* 1316 * Initialize as a new UNRESOLVED entry, lock (non-blocking), 1317 * and link to the parent. The mount point is usually inherited 1318 * from the parent unless this is a special case such as a mount 1319 * point where nlc_namelen is 0. The caller is responsible for 1320 * setting nc_mount in that case. If nlc_namelen is 0 nc_name will 1321 * be NULL. 1322 */ 1323 if (nlc->nlc_namelen) { 1324 bcopy(nlc->nlc_nameptr, ncp->nc_name, nlc->nlc_namelen); 1325 ncp->nc_name[nlc->nlc_namelen] = 0; 1326 ncp->nc_mount = par->nc_mount; 1327 } 1328 nchpp = NCHHASH(hash); 1329 LIST_INSERT_HEAD(nchpp, ncp, nc_hash); 1330 ncp->nc_flag |= NCF_HASHED; 1331 cache_link_parent(ncp, par); 1332 found: 1333 /* 1334 * stats and namecache size management 1335 */ 1336 if (ncp->nc_flag & NCF_UNRESOLVED) 1337 ++gd->gd_nchstats->ncs_miss; 1338 else if (ncp->nc_vp) 1339 ++gd->gd_nchstats->ncs_goodhits; 1340 else 1341 ++gd->gd_nchstats->ncs_neghits; 1342 cache_hysteresis(); 1343 return(ncp); 1344 } 1345 1346 /* 1347 * Resolve an unresolved namecache entry, generally by looking it up. 1348 * The passed ncp must be locked and refd. 1349 * 1350 * Theoretically since a vnode cannot be recycled while held, and since 1351 * the nc_parent chain holds its vnode as long as children exist, the 1352 * direct parent of the cache entry we are trying to resolve should 1353 * have a valid vnode. If not then generate an error that we can 1354 * determine is related to a resolver bug. 1355 * 1356 * Note that successful resolution does not necessarily return an error 1357 * code of 0. If the ncp resolves to a negative cache hit then ENOENT 1358 * will be returned. 1359 */ 1360 int 1361 cache_resolve(struct namecache *ncp, struct ucred *cred) 1362 { 1363 struct namecache *par; 1364 int error; 1365 1366 restart: 1367 /* 1368 * If the ncp is already resolved we have nothing to do. 1369 */ 1370 if ((ncp->nc_flag & NCF_UNRESOLVED) == 0) 1371 return (ncp->nc_error); 1372 1373 /* 1374 * Mount points need special handling because the parent does not 1375 * belong to the same filesystem as the ncp. 1376 */ 1377 if (ncp->nc_flag & NCF_MOUNTPT) 1378 return (cache_resolve_mp(ncp)); 1379 1380 /* 1381 * We expect an unbroken chain of ncps to at least the mount point, 1382 * and even all the way to root (but this code doesn't have to go 1383 * past the mount point). 1384 */ 1385 if (ncp->nc_parent == NULL) { 1386 printf("EXDEV case 1 %p %*.*s\n", ncp, 1387 ncp->nc_nlen, ncp->nc_nlen, ncp->nc_name); 1388 ncp->nc_error = EXDEV; 1389 return(ncp->nc_error); 1390 } 1391 1392 /* 1393 * The vp's of the parent directories in the chain are held via vhold() 1394 * due to the existance of the child, and should not disappear. 1395 * However, there are cases where they can disappear: 1396 * 1397 * - due to filesystem I/O errors. 1398 * - due to NFS being stupid about tracking the namespace and 1399 * destroys the namespace for entire directories quite often. 1400 * - due to forced unmounts. 1401 * - due to an rmdir (parent will be marked DESTROYED) 1402 * 1403 * When this occurs we have to track the chain backwards and resolve 1404 * it, looping until the resolver catches up to the current node. We 1405 * could recurse here but we might run ourselves out of kernel stack 1406 * so we do it in a more painful manner. This situation really should 1407 * not occur all that often, or if it does not have to go back too 1408 * many nodes to resolve the ncp. 1409 */ 1410 while (ncp->nc_parent->nc_vp == NULL) { 1411 /* 1412 * This case can occur if a process is CD'd into a 1413 * directory which is then rmdir'd. If the parent is marked 1414 * destroyed there is no point trying to resolve it. 1415 */ 1416 if (ncp->nc_parent->nc_flag & NCF_DESTROYED) 1417 return(ENOENT); 1418 1419 par = ncp->nc_parent; 1420 while (par->nc_parent && par->nc_parent->nc_vp == NULL) 1421 par = par->nc_parent; 1422 if (par->nc_parent == NULL) { 1423 printf("EXDEV case 2 %*.*s\n", 1424 par->nc_nlen, par->nc_nlen, par->nc_name); 1425 return (EXDEV); 1426 } 1427 printf("[diagnostic] cache_resolve: had to recurse on %*.*s\n", 1428 par->nc_nlen, par->nc_nlen, par->nc_name); 1429 /* 1430 * The parent is not set in stone, ref and lock it to prevent 1431 * it from disappearing. Also note that due to renames it 1432 * is possible for our ncp to move and for par to no longer 1433 * be one of its parents. We resolve it anyway, the loop 1434 * will handle any moves. 1435 */ 1436 cache_get(par); 1437 if (par->nc_flag & NCF_MOUNTPT) { 1438 cache_resolve_mp(par); 1439 } else if (par->nc_parent->nc_vp == NULL) { 1440 printf("[diagnostic] cache_resolve: raced on %*.*s\n", par->nc_nlen, par->nc_nlen, par->nc_name); 1441 cache_put(par); 1442 continue; 1443 } else if (par->nc_flag & NCF_UNRESOLVED) { 1444 par->nc_error = VOP_NRESOLVE(par, cred); 1445 } 1446 if ((error = par->nc_error) != 0) { 1447 if (par->nc_error != EAGAIN) { 1448 printf("EXDEV case 3 %*.*s error %d\n", 1449 par->nc_nlen, par->nc_nlen, par->nc_name, 1450 par->nc_error); 1451 cache_put(par); 1452 return(error); 1453 } 1454 printf("[diagnostic] cache_resolve: EAGAIN par %p %*.*s\n", 1455 par, par->nc_nlen, par->nc_nlen, par->nc_name); 1456 } 1457 cache_put(par); 1458 /* loop */ 1459 } 1460 1461 /* 1462 * Call VOP_NRESOLVE() to get the vp, then scan for any disconnected 1463 * ncp's and reattach them. If this occurs the original ncp is marked 1464 * EAGAIN to force a relookup. 1465 * 1466 * NOTE: in order to call VOP_NRESOLVE(), the parent of the passed 1467 * ncp must already be resolved. 1468 */ 1469 KKASSERT((ncp->nc_flag & NCF_MOUNTPT) == 0); 1470 ncp->nc_error = VOP_NRESOLVE(ncp, cred); 1471 /*vop_nresolve(*ncp->nc_parent->nc_vp->v_ops, ncp, cred);*/ 1472 if (ncp->nc_error == EAGAIN) { 1473 printf("[diagnostic] cache_resolve: EAGAIN ncp %p %*.*s\n", 1474 ncp, ncp->nc_nlen, ncp->nc_nlen, ncp->nc_name); 1475 goto restart; 1476 } 1477 return(ncp->nc_error); 1478 } 1479 1480 /* 1481 * Resolve the ncp associated with a mount point. Such ncp's almost always 1482 * remain resolved and this routine is rarely called. NFS MPs tends to force 1483 * re-resolution more often due to its mac-truck-smash-the-namecache 1484 * method of tracking namespace changes. 1485 * 1486 * The semantics for this call is that the passed ncp must be locked on 1487 * entry and will be locked on return. However, if we actually have to 1488 * resolve the mount point we temporarily unlock the entry in order to 1489 * avoid race-to-root deadlocks due to e.g. dead NFS mounts. Because of 1490 * the unlock we have to recheck the flags after we relock. 1491 */ 1492 static int 1493 cache_resolve_mp(struct namecache *ncp) 1494 { 1495 struct vnode *vp; 1496 struct mount *mp = ncp->nc_mount; 1497 int error; 1498 1499 KKASSERT(mp != NULL); 1500 if (ncp->nc_flag & NCF_UNRESOLVED) { 1501 cache_unlock(ncp); 1502 while (vfs_busy(mp, 0, curthread)) 1503 ; 1504 error = VFS_ROOT(mp, &vp); 1505 cache_lock(ncp); 1506 1507 /* 1508 * recheck the ncp state after relocking. 1509 */ 1510 if (ncp->nc_flag & NCF_UNRESOLVED) { 1511 ncp->nc_error = error; 1512 if (error == 0) { 1513 cache_setvp(ncp, vp); 1514 vput(vp); 1515 } else { 1516 printf("[diagnostic] cache_resolve_mp: failed to resolve mount %p\n", mp); 1517 cache_setvp(ncp, NULL); 1518 } 1519 } else if (error == 0) { 1520 vput(vp); 1521 } 1522 vfs_unbusy(mp, curthread); 1523 } 1524 return(ncp->nc_error); 1525 } 1526 1527 void 1528 cache_cleanneg(int count) 1529 { 1530 struct namecache *ncp; 1531 1532 /* 1533 * Automode from the vnlru proc - clean out 10% of the negative cache 1534 * entries. 1535 */ 1536 if (count == 0) 1537 count = numneg / 10 + 1; 1538 1539 /* 1540 * Attempt to clean out the specified number of negative cache 1541 * entries. 1542 */ 1543 while (count) { 1544 ncp = TAILQ_FIRST(&ncneglist); 1545 if (ncp == NULL) { 1546 KKASSERT(numneg == 0); 1547 break; 1548 } 1549 TAILQ_REMOVE(&ncneglist, ncp, nc_vnode); 1550 TAILQ_INSERT_TAIL(&ncneglist, ncp, nc_vnode); 1551 if (cache_get_nonblock(ncp) == 0) 1552 cache_zap(ncp); 1553 --count; 1554 } 1555 } 1556 1557 /* 1558 * Rehash a ncp. Rehashing is typically required if the name changes (should 1559 * not generally occur) or the parent link changes. This function will 1560 * unhash the ncp if the ncp is no longer hashable. 1561 */ 1562 static void 1563 cache_rehash(struct namecache *ncp) 1564 { 1565 struct nchashhead *nchpp; 1566 u_int32_t hash; 1567 1568 if (ncp->nc_flag & NCF_HASHED) { 1569 ncp->nc_flag &= ~NCF_HASHED; 1570 LIST_REMOVE(ncp, nc_hash); 1571 } 1572 if (ncp->nc_nlen && ncp->nc_parent) { 1573 hash = fnv_32_buf(ncp->nc_name, ncp->nc_nlen, FNV1_32_INIT); 1574 hash = fnv_32_buf(&ncp->nc_parent, 1575 sizeof(ncp->nc_parent), hash); 1576 nchpp = NCHHASH(hash); 1577 LIST_INSERT_HEAD(nchpp, ncp, nc_hash); 1578 ncp->nc_flag |= NCF_HASHED; 1579 } 1580 } 1581 1582 /* 1583 * Name cache initialization, from vfsinit() when we are booting 1584 */ 1585 void 1586 nchinit(void) 1587 { 1588 int i; 1589 globaldata_t gd; 1590 1591 /* initialise per-cpu namecache effectiveness statistics. */ 1592 for (i = 0; i < ncpus; ++i) { 1593 gd = globaldata_find(i); 1594 gd->gd_nchstats = &nchstats[i]; 1595 } 1596 TAILQ_INIT(&ncneglist); 1597 nchashtbl = hashinit(desiredvnodes*2, M_VFSCACHE, &nchash); 1598 nclockwarn = 1 * hz; 1599 } 1600 1601 /* 1602 * Called from start_init() to bootstrap the root filesystem. Returns 1603 * a referenced, unlocked namecache record. 1604 */ 1605 struct namecache * 1606 cache_allocroot(struct mount *mp, struct vnode *vp) 1607 { 1608 struct namecache *ncp = cache_alloc(0); 1609 1610 ncp->nc_flag |= NCF_MOUNTPT | NCF_ROOT; 1611 ncp->nc_mount = mp; 1612 cache_setvp(ncp, vp); 1613 return(ncp); 1614 } 1615 1616 /* 1617 * vfs_cache_setroot() 1618 * 1619 * Create an association between the root of our namecache and 1620 * the root vnode. This routine may be called several times during 1621 * booting. 1622 * 1623 * If the caller intends to save the returned namecache pointer somewhere 1624 * it must cache_hold() it. 1625 */ 1626 void 1627 vfs_cache_setroot(struct vnode *nvp, struct namecache *ncp) 1628 { 1629 struct vnode *ovp; 1630 struct namecache *oncp; 1631 1632 ovp = rootvnode; 1633 oncp = rootncp; 1634 rootvnode = nvp; 1635 rootncp = ncp; 1636 1637 if (ovp) 1638 vrele(ovp); 1639 if (oncp) 1640 cache_drop(oncp); 1641 } 1642 1643 /* 1644 * XXX OLD API COMPAT FUNCTION. This really messes up the new namecache 1645 * topology and is being removed as quickly as possible. The new VOP_N*() 1646 * API calls are required to make specific adjustments using the supplied 1647 * ncp pointers rather then just bogusly purging random vnodes. 1648 * 1649 * Invalidate all namecache entries to a particular vnode as well as 1650 * any direct children of that vnode in the namecache. This is a 1651 * 'catch all' purge used by filesystems that do not know any better. 1652 * 1653 * A new vnode v_id is generated. Note that no vnode will ever have a 1654 * v_id of 0. 1655 * 1656 * Note that the linkage between the vnode and its namecache entries will 1657 * be removed, but the namecache entries themselves might stay put due to 1658 * active references from elsewhere in the system or due to the existance of 1659 * the children. The namecache topology is left intact even if we do not 1660 * know what the vnode association is. Such entries will be marked 1661 * NCF_UNRESOLVED. 1662 * 1663 * XXX: Only time and the size of v_id prevents this from failing: 1664 * XXX: In theory we should hunt down all (struct vnode*, v_id) 1665 * XXX: soft references and nuke them, at least on the global 1666 * XXX: v_id wraparound. The period of resistance can be extended 1667 * XXX: by incrementing each vnodes v_id individually instead of 1668 * XXX: using the global v_id. 1669 */ 1670 void 1671 cache_purge(struct vnode *vp) 1672 { 1673 static u_long nextid; 1674 1675 cache_inval_vp(vp, CINV_DESTROY | CINV_CHILDREN); 1676 1677 /* 1678 * Calculate a new unique id for ".." handling 1679 */ 1680 do { 1681 nextid++; 1682 } while (nextid == vp->v_id || nextid == 0); 1683 vp->v_id = nextid; 1684 } 1685 1686 /* 1687 * Flush all entries referencing a particular filesystem. 1688 * 1689 * Since we need to check it anyway, we will flush all the invalid 1690 * entries at the same time. 1691 */ 1692 void 1693 cache_purgevfs(struct mount *mp) 1694 { 1695 struct nchashhead *nchpp; 1696 struct namecache *ncp, *nnp; 1697 1698 /* 1699 * Scan hash tables for applicable entries. 1700 */ 1701 for (nchpp = &nchashtbl[nchash]; nchpp >= nchashtbl; nchpp--) { 1702 ncp = LIST_FIRST(nchpp); 1703 if (ncp) 1704 cache_hold(ncp); 1705 while (ncp) { 1706 nnp = LIST_NEXT(ncp, nc_hash); 1707 if (nnp) 1708 cache_hold(nnp); 1709 if (ncp->nc_mount == mp) { 1710 cache_lock(ncp); 1711 cache_zap(ncp); 1712 } else { 1713 cache_drop(ncp); 1714 } 1715 ncp = nnp; 1716 } 1717 } 1718 } 1719 1720 static int disablecwd; 1721 SYSCTL_INT(_debug, OID_AUTO, disablecwd, CTLFLAG_RW, &disablecwd, 0, ""); 1722 1723 static u_long numcwdcalls; STATNODE(CTLFLAG_RD, numcwdcalls, &numcwdcalls); 1724 static u_long numcwdfail1; STATNODE(CTLFLAG_RD, numcwdfail1, &numcwdfail1); 1725 static u_long numcwdfail2; STATNODE(CTLFLAG_RD, numcwdfail2, &numcwdfail2); 1726 static u_long numcwdfail3; STATNODE(CTLFLAG_RD, numcwdfail3, &numcwdfail3); 1727 static u_long numcwdfail4; STATNODE(CTLFLAG_RD, numcwdfail4, &numcwdfail4); 1728 static u_long numcwdfound; STATNODE(CTLFLAG_RD, numcwdfound, &numcwdfound); 1729 1730 int 1731 __getcwd(struct __getcwd_args *uap) 1732 { 1733 int buflen; 1734 int error; 1735 char *buf; 1736 char *bp; 1737 1738 if (disablecwd) 1739 return (ENODEV); 1740 1741 buflen = uap->buflen; 1742 if (buflen < 2) 1743 return (EINVAL); 1744 if (buflen > MAXPATHLEN) 1745 buflen = MAXPATHLEN; 1746 1747 buf = malloc(buflen, M_TEMP, M_WAITOK); 1748 bp = kern_getcwd(buf, buflen, &error); 1749 if (error == 0) 1750 error = copyout(bp, uap->buf, strlen(bp) + 1); 1751 free(buf, M_TEMP); 1752 return (error); 1753 } 1754 1755 char * 1756 kern_getcwd(char *buf, size_t buflen, int *error) 1757 { 1758 struct proc *p = curproc; 1759 char *bp; 1760 int i, slash_prefixed; 1761 struct filedesc *fdp; 1762 struct namecache *ncp; 1763 1764 numcwdcalls++; 1765 bp = buf; 1766 bp += buflen - 1; 1767 *bp = '\0'; 1768 fdp = p->p_fd; 1769 slash_prefixed = 0; 1770 1771 ncp = fdp->fd_ncdir; 1772 while (ncp && ncp != fdp->fd_nrdir && (ncp->nc_flag & NCF_ROOT) == 0) { 1773 if (ncp->nc_flag & NCF_MOUNTPT) { 1774 if (ncp->nc_mount == NULL) { 1775 *error = EBADF; /* forced unmount? */ 1776 return(NULL); 1777 } 1778 ncp = ncp->nc_parent; 1779 continue; 1780 } 1781 for (i = ncp->nc_nlen - 1; i >= 0; i--) { 1782 if (bp == buf) { 1783 numcwdfail4++; 1784 *error = ENOMEM; 1785 return(NULL); 1786 } 1787 *--bp = ncp->nc_name[i]; 1788 } 1789 if (bp == buf) { 1790 numcwdfail4++; 1791 *error = ENOMEM; 1792 return(NULL); 1793 } 1794 *--bp = '/'; 1795 slash_prefixed = 1; 1796 ncp = ncp->nc_parent; 1797 } 1798 if (ncp == NULL) { 1799 numcwdfail2++; 1800 *error = ENOENT; 1801 return(NULL); 1802 } 1803 if (!slash_prefixed) { 1804 if (bp == buf) { 1805 numcwdfail4++; 1806 *error = ENOMEM; 1807 return(NULL); 1808 } 1809 *--bp = '/'; 1810 } 1811 numcwdfound++; 1812 *error = 0; 1813 return (bp); 1814 } 1815 1816 /* 1817 * Thus begins the fullpath magic. 1818 */ 1819 1820 #undef STATNODE 1821 #define STATNODE(name) \ 1822 static u_int name; \ 1823 SYSCTL_UINT(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, 0, "") 1824 1825 static int disablefullpath; 1826 SYSCTL_INT(_debug, OID_AUTO, disablefullpath, CTLFLAG_RW, 1827 &disablefullpath, 0, ""); 1828 1829 STATNODE(numfullpathcalls); 1830 STATNODE(numfullpathfail1); 1831 STATNODE(numfullpathfail2); 1832 STATNODE(numfullpathfail3); 1833 STATNODE(numfullpathfail4); 1834 STATNODE(numfullpathfound); 1835 1836 int 1837 cache_fullpath(struct proc *p, struct namecache *ncp, char **retbuf, char **freebuf) 1838 { 1839 char *bp, *buf; 1840 int i, slash_prefixed; 1841 struct namecache *fd_nrdir; 1842 1843 numfullpathcalls--; 1844 1845 buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 1846 bp = buf + MAXPATHLEN - 1; 1847 *bp = '\0'; 1848 if (p != NULL) 1849 fd_nrdir = p->p_fd->fd_nrdir; 1850 else 1851 fd_nrdir = NULL; 1852 slash_prefixed = 0; 1853 while (ncp && ncp != fd_nrdir && (ncp->nc_flag & NCF_ROOT) == 0) { 1854 if (ncp->nc_flag & NCF_MOUNTPT) { 1855 if (ncp->nc_mount == NULL) { 1856 free(buf, M_TEMP); 1857 return(EBADF); 1858 } 1859 ncp = ncp->nc_parent; 1860 continue; 1861 } 1862 for (i = ncp->nc_nlen - 1; i >= 0; i--) { 1863 if (bp == buf) { 1864 numfullpathfail4++; 1865 free(buf, M_TEMP); 1866 return(ENOMEM); 1867 } 1868 *--bp = ncp->nc_name[i]; 1869 } 1870 if (bp == buf) { 1871 numfullpathfail4++; 1872 free(buf, M_TEMP); 1873 return(ENOMEM); 1874 } 1875 *--bp = '/'; 1876 slash_prefixed = 1; 1877 ncp = ncp->nc_parent; 1878 } 1879 if (ncp == NULL) { 1880 numfullpathfail2++; 1881 free(buf, M_TEMP); 1882 return(ENOENT); 1883 } 1884 if (p != NULL && (ncp->nc_flag & NCF_ROOT) && ncp != fd_nrdir) { 1885 bp = buf + MAXPATHLEN - 1; 1886 *bp = '\0'; 1887 slash_prefixed = 0; 1888 } 1889 if (!slash_prefixed) { 1890 if (bp == buf) { 1891 numfullpathfail4++; 1892 free(buf, M_TEMP); 1893 return(ENOMEM); 1894 } 1895 *--bp = '/'; 1896 } 1897 numfullpathfound++; 1898 *retbuf = bp; 1899 *freebuf = buf; 1900 1901 return(0); 1902 } 1903 1904 int 1905 vn_fullpath(struct proc *p, struct vnode *vn, char **retbuf, char **freebuf) 1906 { 1907 struct namecache *ncp; 1908 1909 numfullpathcalls++; 1910 if (disablefullpath) 1911 return (ENODEV); 1912 1913 if (p == NULL) 1914 return (EINVAL); 1915 1916 /* vn is NULL, client wants us to use p->p_textvp */ 1917 if (vn == NULL) { 1918 if ((vn = p->p_textvp) == NULL) 1919 return (EINVAL); 1920 } 1921 TAILQ_FOREACH(ncp, &vn->v_namecache, nc_vnode) { 1922 if (ncp->nc_nlen) 1923 break; 1924 } 1925 if (ncp == NULL) 1926 return (EINVAL); 1927 1928 numfullpathcalls--; 1929 return(cache_fullpath(p, ncp, retbuf, freebuf)); 1930 } 1931