1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1989, 1993 5 * The Regents of the University of California. All rights reserved. 6 * (c) UNIX System Laboratories, Inc. 7 * All or some portions of this file are derived from material licensed 8 * to the University of California by American Telephone and Telegraph 9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 10 * the permission of UNIX System Laboratories, Inc. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#)vfs_subr.c 8.31 (Berkeley) 5/26/95 37 */ 38 39 /* 40 * External virtual filesystem routines 41 */ 42 43 #include <sys/cdefs.h> 44 __FBSDID("$FreeBSD$"); 45 46 #include "opt_ddb.h" 47 #include "opt_watchdog.h" 48 49 #include <sys/param.h> 50 #include <sys/systm.h> 51 #include <sys/bio.h> 52 #include <sys/buf.h> 53 #include <sys/capsicum.h> 54 #include <sys/condvar.h> 55 #include <sys/conf.h> 56 #include <sys/counter.h> 57 #include <sys/dirent.h> 58 #include <sys/event.h> 59 #include <sys/eventhandler.h> 60 #include <sys/extattr.h> 61 #include <sys/file.h> 62 #include <sys/fcntl.h> 63 #include <sys/jail.h> 64 #include <sys/kdb.h> 65 #include <sys/kernel.h> 66 #include <sys/kthread.h> 67 #include <sys/ktr.h> 68 #include <sys/lockf.h> 69 #include <sys/malloc.h> 70 #include <sys/mount.h> 71 #include <sys/namei.h> 72 #include <sys/pctrie.h> 73 #include <sys/priv.h> 74 #include <sys/reboot.h> 75 #include <sys/refcount.h> 76 #include <sys/rwlock.h> 77 #include <sys/sched.h> 78 #include <sys/sleepqueue.h> 79 #include <sys/smp.h> 80 #include <sys/stat.h> 81 #include <sys/sysctl.h> 82 #include <sys/syslog.h> 83 #include <sys/vmmeter.h> 84 #include <sys/vnode.h> 85 #include <sys/watchdog.h> 86 87 #include <machine/stdarg.h> 88 89 #include <security/mac/mac_framework.h> 90 91 #include <vm/vm.h> 92 #include <vm/vm_object.h> 93 #include <vm/vm_extern.h> 94 #include <vm/pmap.h> 95 #include <vm/vm_map.h> 96 #include <vm/vm_page.h> 97 #include <vm/vm_kern.h> 98 #include <vm/uma.h> 99 100 #ifdef DDB 101 #include <ddb/ddb.h> 102 #endif 103 104 static void delmntque(struct vnode *vp); 105 static int flushbuflist(struct bufv *bufv, int flags, struct bufobj *bo, 106 int slpflag, int slptimeo); 107 static void syncer_shutdown(void *arg, int howto); 108 static int vtryrecycle(struct vnode *vp); 109 static void v_init_counters(struct vnode *); 110 static void v_incr_devcount(struct vnode *); 111 static void v_decr_devcount(struct vnode *); 112 static void vgonel(struct vnode *); 113 static void vfs_knllock(void *arg); 114 static void vfs_knlunlock(void *arg); 115 static void vfs_knl_assert_locked(void *arg); 116 static void vfs_knl_assert_unlocked(void *arg); 117 static void destroy_vpollinfo(struct vpollinfo *vi); 118 static int v_inval_buf_range_locked(struct vnode *vp, struct bufobj *bo, 119 daddr_t startlbn, daddr_t endlbn); 120 static void vnlru_recalc(void); 121 122 /* 123 * These fences are intended for cases where some synchronization is 124 * needed between access of v_iflags and lockless vnode refcount (v_holdcnt 125 * and v_usecount) updates. Access to v_iflags is generally synchronized 126 * by the interlock, but we have some internal assertions that check vnode 127 * flags without acquiring the lock. Thus, these fences are INVARIANTS-only 128 * for now. 129 */ 130 #ifdef INVARIANTS 131 #define VNODE_REFCOUNT_FENCE_ACQ() atomic_thread_fence_acq() 132 #define VNODE_REFCOUNT_FENCE_REL() atomic_thread_fence_rel() 133 #else 134 #define VNODE_REFCOUNT_FENCE_ACQ() 135 #define VNODE_REFCOUNT_FENCE_REL() 136 #endif 137 138 /* 139 * Number of vnodes in existence. Increased whenever getnewvnode() 140 * allocates a new vnode, decreased in vdropl() for VIRF_DOOMED vnode. 141 */ 142 static u_long __exclusive_cache_line numvnodes; 143 144 SYSCTL_ULONG(_vfs, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0, 145 "Number of vnodes in existence"); 146 147 static counter_u64_t vnodes_created; 148 SYSCTL_COUNTER_U64(_vfs, OID_AUTO, vnodes_created, CTLFLAG_RD, &vnodes_created, 149 "Number of vnodes created by getnewvnode"); 150 151 /* 152 * Conversion tables for conversion from vnode types to inode formats 153 * and back. 154 */ 155 enum vtype iftovt_tab[16] = { 156 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON, 157 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VNON 158 }; 159 int vttoif_tab[10] = { 160 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK, 161 S_IFSOCK, S_IFIFO, S_IFMT, S_IFMT 162 }; 163 164 /* 165 * List of allocates vnodes in the system. 166 */ 167 static TAILQ_HEAD(freelst, vnode) vnode_list; 168 static struct vnode *vnode_list_free_marker; 169 static struct vnode *vnode_list_reclaim_marker; 170 171 /* 172 * "Free" vnode target. Free vnodes are rarely completely free, but are 173 * just ones that are cheap to recycle. Usually they are for files which 174 * have been stat'd but not read; these usually have inode and namecache 175 * data attached to them. This target is the preferred minimum size of a 176 * sub-cache consisting mostly of such files. The system balances the size 177 * of this sub-cache with its complement to try to prevent either from 178 * thrashing while the other is relatively inactive. The targets express 179 * a preference for the best balance. 180 * 181 * "Above" this target there are 2 further targets (watermarks) related 182 * to recyling of free vnodes. In the best-operating case, the cache is 183 * exactly full, the free list has size between vlowat and vhiwat above the 184 * free target, and recycling from it and normal use maintains this state. 185 * Sometimes the free list is below vlowat or even empty, but this state 186 * is even better for immediate use provided the cache is not full. 187 * Otherwise, vnlru_proc() runs to reclaim enough vnodes (usually non-free 188 * ones) to reach one of these states. The watermarks are currently hard- 189 * coded as 4% and 9% of the available space higher. These and the default 190 * of 25% for wantfreevnodes are too large if the memory size is large. 191 * E.g., 9% of 75% of MAXVNODES is more than 566000 vnodes to reclaim 192 * whenever vnlru_proc() becomes active. 193 */ 194 static long wantfreevnodes; 195 static long __exclusive_cache_line freevnodes; 196 SYSCTL_ULONG(_vfs, OID_AUTO, freevnodes, CTLFLAG_RD, 197 &freevnodes, 0, "Number of \"free\" vnodes"); 198 static long freevnodes_old; 199 200 static counter_u64_t recycles_count; 201 SYSCTL_COUNTER_U64(_vfs, OID_AUTO, recycles, CTLFLAG_RD, &recycles_count, 202 "Number of vnodes recycled to meet vnode cache targets"); 203 204 static counter_u64_t recycles_free_count; 205 SYSCTL_COUNTER_U64(_vfs, OID_AUTO, recycles_free, CTLFLAG_RD, &recycles_free_count, 206 "Number of free vnodes recycled to meet vnode cache targets"); 207 208 /* 209 * Various variables used for debugging the new implementation of 210 * reassignbuf(). 211 * XXX these are probably of (very) limited utility now. 212 */ 213 static int reassignbufcalls; 214 SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW | CTLFLAG_STATS, 215 &reassignbufcalls, 0, "Number of calls to reassignbuf"); 216 217 static counter_u64_t deferred_inact; 218 SYSCTL_COUNTER_U64(_vfs, OID_AUTO, deferred_inact, CTLFLAG_RD, &deferred_inact, 219 "Number of times inactive processing was deferred"); 220 221 /* To keep more than one thread at a time from running vfs_getnewfsid */ 222 static struct mtx mntid_mtx; 223 224 /* 225 * Lock for any access to the following: 226 * vnode_list 227 * numvnodes 228 * freevnodes 229 */ 230 static struct mtx __exclusive_cache_line vnode_list_mtx; 231 232 /* Publicly exported FS */ 233 struct nfs_public nfs_pub; 234 235 static uma_zone_t buf_trie_zone; 236 237 /* Zone for allocation of new vnodes - used exclusively by getnewvnode() */ 238 static uma_zone_t vnode_zone; 239 static uma_zone_t vnodepoll_zone; 240 241 /* 242 * The workitem queue. 243 * 244 * It is useful to delay writes of file data and filesystem metadata 245 * for tens of seconds so that quickly created and deleted files need 246 * not waste disk bandwidth being created and removed. To realize this, 247 * we append vnodes to a "workitem" queue. When running with a soft 248 * updates implementation, most pending metadata dependencies should 249 * not wait for more than a few seconds. Thus, mounted on block devices 250 * are delayed only about a half the time that file data is delayed. 251 * Similarly, directory updates are more critical, so are only delayed 252 * about a third the time that file data is delayed. Thus, there are 253 * SYNCER_MAXDELAY queues that are processed round-robin at a rate of 254 * one each second (driven off the filesystem syncer process). The 255 * syncer_delayno variable indicates the next queue that is to be processed. 256 * Items that need to be processed soon are placed in this queue: 257 * 258 * syncer_workitem_pending[syncer_delayno] 259 * 260 * A delay of fifteen seconds is done by placing the request fifteen 261 * entries later in the queue: 262 * 263 * syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask] 264 * 265 */ 266 static int syncer_delayno; 267 static long syncer_mask; 268 LIST_HEAD(synclist, bufobj); 269 static struct synclist *syncer_workitem_pending; 270 /* 271 * The sync_mtx protects: 272 * bo->bo_synclist 273 * sync_vnode_count 274 * syncer_delayno 275 * syncer_state 276 * syncer_workitem_pending 277 * syncer_worklist_len 278 * rushjob 279 */ 280 static struct mtx sync_mtx; 281 static struct cv sync_wakeup; 282 283 #define SYNCER_MAXDELAY 32 284 static int syncer_maxdelay = SYNCER_MAXDELAY; /* maximum delay time */ 285 static int syncdelay = 30; /* max time to delay syncing data */ 286 static int filedelay = 30; /* time to delay syncing files */ 287 SYSCTL_INT(_kern, OID_AUTO, filedelay, CTLFLAG_RW, &filedelay, 0, 288 "Time to delay syncing files (in seconds)"); 289 static int dirdelay = 29; /* time to delay syncing directories */ 290 SYSCTL_INT(_kern, OID_AUTO, dirdelay, CTLFLAG_RW, &dirdelay, 0, 291 "Time to delay syncing directories (in seconds)"); 292 static int metadelay = 28; /* time to delay syncing metadata */ 293 SYSCTL_INT(_kern, OID_AUTO, metadelay, CTLFLAG_RW, &metadelay, 0, 294 "Time to delay syncing metadata (in seconds)"); 295 static int rushjob; /* number of slots to run ASAP */ 296 static int stat_rush_requests; /* number of times I/O speeded up */ 297 SYSCTL_INT(_debug, OID_AUTO, rush_requests, CTLFLAG_RW, &stat_rush_requests, 0, 298 "Number of times I/O speeded up (rush requests)"); 299 300 #define VDBATCH_SIZE 8 301 struct vdbatch { 302 u_int index; 303 long freevnodes; 304 struct mtx lock; 305 struct vnode *tab[VDBATCH_SIZE]; 306 }; 307 DPCPU_DEFINE_STATIC(struct vdbatch, vd); 308 309 static void vdbatch_dequeue(struct vnode *vp); 310 311 /* 312 * When shutting down the syncer, run it at four times normal speed. 313 */ 314 #define SYNCER_SHUTDOWN_SPEEDUP 4 315 static int sync_vnode_count; 316 static int syncer_worklist_len; 317 static enum { SYNCER_RUNNING, SYNCER_SHUTTING_DOWN, SYNCER_FINAL_DELAY } 318 syncer_state; 319 320 /* Target for maximum number of vnodes. */ 321 u_long desiredvnodes; 322 static u_long gapvnodes; /* gap between wanted and desired */ 323 static u_long vhiwat; /* enough extras after expansion */ 324 static u_long vlowat; /* minimal extras before expansion */ 325 static u_long vstir; /* nonzero to stir non-free vnodes */ 326 static volatile int vsmalltrigger = 8; /* pref to keep if > this many pages */ 327 328 static u_long vnlru_read_freevnodes(void); 329 330 /* 331 * Note that no attempt is made to sanitize these parameters. 332 */ 333 static int 334 sysctl_maxvnodes(SYSCTL_HANDLER_ARGS) 335 { 336 u_long val; 337 int error; 338 339 val = desiredvnodes; 340 error = sysctl_handle_long(oidp, &val, 0, req); 341 if (error != 0 || req->newptr == NULL) 342 return (error); 343 344 if (val == desiredvnodes) 345 return (0); 346 mtx_lock(&vnode_list_mtx); 347 desiredvnodes = val; 348 wantfreevnodes = desiredvnodes / 4; 349 vnlru_recalc(); 350 mtx_unlock(&vnode_list_mtx); 351 /* 352 * XXX There is no protection against multiple threads changing 353 * desiredvnodes at the same time. Locking above only helps vnlru and 354 * getnewvnode. 355 */ 356 vfs_hash_changesize(desiredvnodes); 357 cache_changesize(desiredvnodes); 358 return (0); 359 } 360 361 SYSCTL_PROC(_kern, KERN_MAXVNODES, maxvnodes, 362 CTLTYPE_ULONG | CTLFLAG_MPSAFE | CTLFLAG_RW, NULL, 0, sysctl_maxvnodes, 363 "UL", "Target for maximum number of vnodes"); 364 365 static int 366 sysctl_wantfreevnodes(SYSCTL_HANDLER_ARGS) 367 { 368 u_long val; 369 int error; 370 371 val = wantfreevnodes; 372 error = sysctl_handle_long(oidp, &val, 0, req); 373 if (error != 0 || req->newptr == NULL) 374 return (error); 375 376 if (val == wantfreevnodes) 377 return (0); 378 mtx_lock(&vnode_list_mtx); 379 wantfreevnodes = val; 380 vnlru_recalc(); 381 mtx_unlock(&vnode_list_mtx); 382 return (0); 383 } 384 385 SYSCTL_PROC(_vfs, OID_AUTO, wantfreevnodes, 386 CTLTYPE_ULONG | CTLFLAG_MPSAFE | CTLFLAG_RW, NULL, 0, sysctl_wantfreevnodes, 387 "UL", "Target for minimum number of \"free\" vnodes"); 388 389 SYSCTL_ULONG(_kern, OID_AUTO, minvnodes, CTLFLAG_RW, 390 &wantfreevnodes, 0, "Old name for vfs.wantfreevnodes (legacy)"); 391 static int vnlru_nowhere; 392 SYSCTL_INT(_debug, OID_AUTO, vnlru_nowhere, CTLFLAG_RW, 393 &vnlru_nowhere, 0, "Number of times the vnlru process ran without success"); 394 395 static int 396 sysctl_try_reclaim_vnode(SYSCTL_HANDLER_ARGS) 397 { 398 struct vnode *vp; 399 struct nameidata nd; 400 char *buf; 401 unsigned long ndflags; 402 int error; 403 404 if (req->newptr == NULL) 405 return (EINVAL); 406 if (req->newlen >= PATH_MAX) 407 return (E2BIG); 408 409 buf = malloc(PATH_MAX, M_TEMP, M_WAITOK); 410 error = SYSCTL_IN(req, buf, req->newlen); 411 if (error != 0) 412 goto out; 413 414 buf[req->newlen] = '\0'; 415 416 ndflags = LOCKLEAF | NOFOLLOW | AUDITVNODE1 | NOCACHE | SAVENAME; 417 NDINIT(&nd, LOOKUP, ndflags, UIO_SYSSPACE, buf, curthread); 418 if ((error = namei(&nd)) != 0) 419 goto out; 420 vp = nd.ni_vp; 421 422 if (VN_IS_DOOMED(vp)) { 423 /* 424 * This vnode is being recycled. Return != 0 to let the caller 425 * know that the sysctl had no effect. Return EAGAIN because a 426 * subsequent call will likely succeed (since namei will create 427 * a new vnode if necessary) 428 */ 429 error = EAGAIN; 430 goto putvnode; 431 } 432 433 counter_u64_add(recycles_count, 1); 434 vgone(vp); 435 putvnode: 436 NDFREE(&nd, 0); 437 out: 438 free(buf, M_TEMP); 439 return (error); 440 } 441 442 static int 443 sysctl_ftry_reclaim_vnode(SYSCTL_HANDLER_ARGS) 444 { 445 struct thread *td = curthread; 446 struct vnode *vp; 447 struct file *fp; 448 int error; 449 int fd; 450 451 if (req->newptr == NULL) 452 return (EBADF); 453 454 error = sysctl_handle_int(oidp, &fd, 0, req); 455 if (error != 0) 456 return (error); 457 error = getvnode(curthread, fd, &cap_fcntl_rights, &fp); 458 if (error != 0) 459 return (error); 460 vp = fp->f_vnode; 461 462 error = vn_lock(vp, LK_EXCLUSIVE); 463 if (error != 0) 464 goto drop; 465 466 counter_u64_add(recycles_count, 1); 467 vgone(vp); 468 VOP_UNLOCK(vp); 469 drop: 470 fdrop(fp, td); 471 return (error); 472 } 473 474 SYSCTL_PROC(_debug, OID_AUTO, try_reclaim_vnode, 475 CTLTYPE_STRING | CTLFLAG_MPSAFE | CTLFLAG_WR, NULL, 0, 476 sysctl_try_reclaim_vnode, "A", "Try to reclaim a vnode by its pathname"); 477 SYSCTL_PROC(_debug, OID_AUTO, ftry_reclaim_vnode, 478 CTLTYPE_INT | CTLFLAG_MPSAFE | CTLFLAG_WR, NULL, 0, 479 sysctl_ftry_reclaim_vnode, "I", 480 "Try to reclaim a vnode by its file descriptor"); 481 482 /* Shift count for (uintptr_t)vp to initialize vp->v_hash. */ 483 static int vnsz2log; 484 485 /* 486 * Support for the bufobj clean & dirty pctrie. 487 */ 488 static void * 489 buf_trie_alloc(struct pctrie *ptree) 490 { 491 492 return uma_zalloc(buf_trie_zone, M_NOWAIT); 493 } 494 495 static void 496 buf_trie_free(struct pctrie *ptree, void *node) 497 { 498 499 uma_zfree(buf_trie_zone, node); 500 } 501 PCTRIE_DEFINE(BUF, buf, b_lblkno, buf_trie_alloc, buf_trie_free); 502 503 /* 504 * Initialize the vnode management data structures. 505 * 506 * Reevaluate the following cap on the number of vnodes after the physical 507 * memory size exceeds 512GB. In the limit, as the physical memory size 508 * grows, the ratio of the memory size in KB to vnodes approaches 64:1. 509 */ 510 #ifndef MAXVNODES_MAX 511 #define MAXVNODES_MAX (512UL * 1024 * 1024 / 64) /* 8M */ 512 #endif 513 514 static MALLOC_DEFINE(M_VNODE_MARKER, "vnodemarker", "vnode marker"); 515 516 static struct vnode * 517 vn_alloc_marker(struct mount *mp) 518 { 519 struct vnode *vp; 520 521 vp = malloc(sizeof(struct vnode), M_VNODE_MARKER, M_WAITOK | M_ZERO); 522 vp->v_type = VMARKER; 523 vp->v_mount = mp; 524 525 return (vp); 526 } 527 528 static void 529 vn_free_marker(struct vnode *vp) 530 { 531 532 MPASS(vp->v_type == VMARKER); 533 free(vp, M_VNODE_MARKER); 534 } 535 536 /* 537 * Initialize a vnode as it first enters the zone. 538 */ 539 static int 540 vnode_init(void *mem, int size, int flags) 541 { 542 struct vnode *vp; 543 544 vp = mem; 545 bzero(vp, size); 546 /* 547 * Setup locks. 548 */ 549 vp->v_vnlock = &vp->v_lock; 550 mtx_init(&vp->v_interlock, "vnode interlock", NULL, MTX_DEF); 551 /* 552 * By default, don't allow shared locks unless filesystems opt-in. 553 */ 554 lockinit(vp->v_vnlock, PVFS, "vnode", VLKTIMEOUT, 555 LK_NOSHARE | LK_IS_VNODE); 556 /* 557 * Initialize bufobj. 558 */ 559 bufobj_init(&vp->v_bufobj, vp); 560 /* 561 * Initialize namecache. 562 */ 563 LIST_INIT(&vp->v_cache_src); 564 TAILQ_INIT(&vp->v_cache_dst); 565 /* 566 * Initialize rangelocks. 567 */ 568 rangelock_init(&vp->v_rl); 569 570 vp->v_dbatchcpu = NOCPU; 571 572 mtx_lock(&vnode_list_mtx); 573 TAILQ_INSERT_BEFORE(vnode_list_free_marker, vp, v_vnodelist); 574 mtx_unlock(&vnode_list_mtx); 575 return (0); 576 } 577 578 /* 579 * Free a vnode when it is cleared from the zone. 580 */ 581 static void 582 vnode_fini(void *mem, int size) 583 { 584 struct vnode *vp; 585 struct bufobj *bo; 586 587 vp = mem; 588 vdbatch_dequeue(vp); 589 mtx_lock(&vnode_list_mtx); 590 TAILQ_REMOVE(&vnode_list, vp, v_vnodelist); 591 mtx_unlock(&vnode_list_mtx); 592 rangelock_destroy(&vp->v_rl); 593 lockdestroy(vp->v_vnlock); 594 mtx_destroy(&vp->v_interlock); 595 bo = &vp->v_bufobj; 596 rw_destroy(BO_LOCKPTR(bo)); 597 } 598 599 /* 600 * Provide the size of NFS nclnode and NFS fh for calculation of the 601 * vnode memory consumption. The size is specified directly to 602 * eliminate dependency on NFS-private header. 603 * 604 * Other filesystems may use bigger or smaller (like UFS and ZFS) 605 * private inode data, but the NFS-based estimation is ample enough. 606 * Still, we care about differences in the size between 64- and 32-bit 607 * platforms. 608 * 609 * Namecache structure size is heuristically 610 * sizeof(struct namecache_ts) + CACHE_PATH_CUTOFF + 1. 611 */ 612 #ifdef _LP64 613 #define NFS_NCLNODE_SZ (528 + 64) 614 #define NC_SZ 148 615 #else 616 #define NFS_NCLNODE_SZ (360 + 32) 617 #define NC_SZ 92 618 #endif 619 620 static void 621 vntblinit(void *dummy __unused) 622 { 623 struct vdbatch *vd; 624 int cpu, physvnodes, virtvnodes; 625 u_int i; 626 627 /* 628 * Desiredvnodes is a function of the physical memory size and the 629 * kernel's heap size. Generally speaking, it scales with the 630 * physical memory size. The ratio of desiredvnodes to the physical 631 * memory size is 1:16 until desiredvnodes exceeds 98,304. 632 * Thereafter, the 633 * marginal ratio of desiredvnodes to the physical memory size is 634 * 1:64. However, desiredvnodes is limited by the kernel's heap 635 * size. The memory required by desiredvnodes vnodes and vm objects 636 * must not exceed 1/10th of the kernel's heap size. 637 */ 638 physvnodes = maxproc + pgtok(vm_cnt.v_page_count) / 64 + 639 3 * min(98304 * 16, pgtok(vm_cnt.v_page_count)) / 64; 640 virtvnodes = vm_kmem_size / (10 * (sizeof(struct vm_object) + 641 sizeof(struct vnode) + NC_SZ * ncsizefactor + NFS_NCLNODE_SZ)); 642 desiredvnodes = min(physvnodes, virtvnodes); 643 if (desiredvnodes > MAXVNODES_MAX) { 644 if (bootverbose) 645 printf("Reducing kern.maxvnodes %lu -> %lu\n", 646 desiredvnodes, MAXVNODES_MAX); 647 desiredvnodes = MAXVNODES_MAX; 648 } 649 wantfreevnodes = desiredvnodes / 4; 650 mtx_init(&mntid_mtx, "mntid", NULL, MTX_DEF); 651 TAILQ_INIT(&vnode_list); 652 mtx_init(&vnode_list_mtx, "vnode_list", NULL, MTX_DEF); 653 /* 654 * The lock is taken to appease WITNESS. 655 */ 656 mtx_lock(&vnode_list_mtx); 657 vnlru_recalc(); 658 mtx_unlock(&vnode_list_mtx); 659 vnode_list_free_marker = vn_alloc_marker(NULL); 660 TAILQ_INSERT_HEAD(&vnode_list, vnode_list_free_marker, v_vnodelist); 661 vnode_list_reclaim_marker = vn_alloc_marker(NULL); 662 TAILQ_INSERT_HEAD(&vnode_list, vnode_list_reclaim_marker, v_vnodelist); 663 vnode_zone = uma_zcreate("VNODE", sizeof (struct vnode), NULL, NULL, 664 vnode_init, vnode_fini, UMA_ALIGN_PTR, 0); 665 vnodepoll_zone = uma_zcreate("VNODEPOLL", sizeof (struct vpollinfo), 666 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 667 /* 668 * Preallocate enough nodes to support one-per buf so that 669 * we can not fail an insert. reassignbuf() callers can not 670 * tolerate the insertion failure. 671 */ 672 buf_trie_zone = uma_zcreate("BUF TRIE", pctrie_node_size(), 673 NULL, NULL, pctrie_zone_init, NULL, UMA_ALIGN_PTR, 674 UMA_ZONE_NOFREE | UMA_ZONE_VM); 675 uma_prealloc(buf_trie_zone, nbuf); 676 677 vnodes_created = counter_u64_alloc(M_WAITOK); 678 recycles_count = counter_u64_alloc(M_WAITOK); 679 recycles_free_count = counter_u64_alloc(M_WAITOK); 680 deferred_inact = counter_u64_alloc(M_WAITOK); 681 682 /* 683 * Initialize the filesystem syncer. 684 */ 685 syncer_workitem_pending = hashinit(syncer_maxdelay, M_VNODE, 686 &syncer_mask); 687 syncer_maxdelay = syncer_mask + 1; 688 mtx_init(&sync_mtx, "Syncer mtx", NULL, MTX_DEF); 689 cv_init(&sync_wakeup, "syncer"); 690 for (i = 1; i <= sizeof(struct vnode); i <<= 1) 691 vnsz2log++; 692 vnsz2log--; 693 694 CPU_FOREACH(cpu) { 695 vd = DPCPU_ID_PTR((cpu), vd); 696 bzero(vd, sizeof(*vd)); 697 mtx_init(&vd->lock, "vdbatch", NULL, MTX_DEF); 698 } 699 } 700 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_FIRST, vntblinit, NULL); 701 702 703 /* 704 * Mark a mount point as busy. Used to synchronize access and to delay 705 * unmounting. Eventually, mountlist_mtx is not released on failure. 706 * 707 * vfs_busy() is a custom lock, it can block the caller. 708 * vfs_busy() only sleeps if the unmount is active on the mount point. 709 * For a mountpoint mp, vfs_busy-enforced lock is before lock of any 710 * vnode belonging to mp. 711 * 712 * Lookup uses vfs_busy() to traverse mount points. 713 * root fs var fs 714 * / vnode lock A / vnode lock (/var) D 715 * /var vnode lock B /log vnode lock(/var/log) E 716 * vfs_busy lock C vfs_busy lock F 717 * 718 * Within each file system, the lock order is C->A->B and F->D->E. 719 * 720 * When traversing across mounts, the system follows that lock order: 721 * 722 * C->A->B 723 * | 724 * +->F->D->E 725 * 726 * The lookup() process for namei("/var") illustrates the process: 727 * VOP_LOOKUP() obtains B while A is held 728 * vfs_busy() obtains a shared lock on F while A and B are held 729 * vput() releases lock on B 730 * vput() releases lock on A 731 * VFS_ROOT() obtains lock on D while shared lock on F is held 732 * vfs_unbusy() releases shared lock on F 733 * vn_lock() obtains lock on deadfs vnode vp_crossmp instead of A. 734 * Attempt to lock A (instead of vp_crossmp) while D is held would 735 * violate the global order, causing deadlocks. 736 * 737 * dounmount() locks B while F is drained. 738 */ 739 int 740 vfs_busy(struct mount *mp, int flags) 741 { 742 743 MPASS((flags & ~MBF_MASK) == 0); 744 CTR3(KTR_VFS, "%s: mp %p with flags %d", __func__, mp, flags); 745 746 if (vfs_op_thread_enter(mp)) { 747 MPASS((mp->mnt_kern_flag & MNTK_DRAINING) == 0); 748 MPASS((mp->mnt_kern_flag & MNTK_UNMOUNT) == 0); 749 MPASS((mp->mnt_kern_flag & MNTK_REFEXPIRE) == 0); 750 vfs_mp_count_add_pcpu(mp, ref, 1); 751 vfs_mp_count_add_pcpu(mp, lockref, 1); 752 vfs_op_thread_exit(mp); 753 if (flags & MBF_MNTLSTLOCK) 754 mtx_unlock(&mountlist_mtx); 755 return (0); 756 } 757 758 MNT_ILOCK(mp); 759 vfs_assert_mount_counters(mp); 760 MNT_REF(mp); 761 /* 762 * If mount point is currently being unmounted, sleep until the 763 * mount point fate is decided. If thread doing the unmounting fails, 764 * it will clear MNTK_UNMOUNT flag before waking us up, indicating 765 * that this mount point has survived the unmount attempt and vfs_busy 766 * should retry. Otherwise the unmounter thread will set MNTK_REFEXPIRE 767 * flag in addition to MNTK_UNMOUNT, indicating that mount point is 768 * about to be really destroyed. vfs_busy needs to release its 769 * reference on the mount point in this case and return with ENOENT, 770 * telling the caller that mount mount it tried to busy is no longer 771 * valid. 772 */ 773 while (mp->mnt_kern_flag & MNTK_UNMOUNT) { 774 if (flags & MBF_NOWAIT || mp->mnt_kern_flag & MNTK_REFEXPIRE) { 775 MNT_REL(mp); 776 MNT_IUNLOCK(mp); 777 CTR1(KTR_VFS, "%s: failed busying before sleeping", 778 __func__); 779 return (ENOENT); 780 } 781 if (flags & MBF_MNTLSTLOCK) 782 mtx_unlock(&mountlist_mtx); 783 mp->mnt_kern_flag |= MNTK_MWAIT; 784 msleep(mp, MNT_MTX(mp), PVFS | PDROP, "vfs_busy", 0); 785 if (flags & MBF_MNTLSTLOCK) 786 mtx_lock(&mountlist_mtx); 787 MNT_ILOCK(mp); 788 } 789 if (flags & MBF_MNTLSTLOCK) 790 mtx_unlock(&mountlist_mtx); 791 mp->mnt_lockref++; 792 MNT_IUNLOCK(mp); 793 return (0); 794 } 795 796 /* 797 * Free a busy filesystem. 798 */ 799 void 800 vfs_unbusy(struct mount *mp) 801 { 802 int c; 803 804 CTR2(KTR_VFS, "%s: mp %p", __func__, mp); 805 806 if (vfs_op_thread_enter(mp)) { 807 MPASS((mp->mnt_kern_flag & MNTK_DRAINING) == 0); 808 vfs_mp_count_sub_pcpu(mp, lockref, 1); 809 vfs_mp_count_sub_pcpu(mp, ref, 1); 810 vfs_op_thread_exit(mp); 811 return; 812 } 813 814 MNT_ILOCK(mp); 815 vfs_assert_mount_counters(mp); 816 MNT_REL(mp); 817 c = --mp->mnt_lockref; 818 if (mp->mnt_vfs_ops == 0) { 819 MPASS((mp->mnt_kern_flag & MNTK_DRAINING) == 0); 820 MNT_IUNLOCK(mp); 821 return; 822 } 823 if (c < 0) 824 vfs_dump_mount_counters(mp); 825 if (c == 0 && (mp->mnt_kern_flag & MNTK_DRAINING) != 0) { 826 MPASS(mp->mnt_kern_flag & MNTK_UNMOUNT); 827 CTR1(KTR_VFS, "%s: waking up waiters", __func__); 828 mp->mnt_kern_flag &= ~MNTK_DRAINING; 829 wakeup(&mp->mnt_lockref); 830 } 831 MNT_IUNLOCK(mp); 832 } 833 834 /* 835 * Lookup a mount point by filesystem identifier. 836 */ 837 struct mount * 838 vfs_getvfs(fsid_t *fsid) 839 { 840 struct mount *mp; 841 842 CTR2(KTR_VFS, "%s: fsid %p", __func__, fsid); 843 mtx_lock(&mountlist_mtx); 844 TAILQ_FOREACH(mp, &mountlist, mnt_list) { 845 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] && 846 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) { 847 vfs_ref(mp); 848 mtx_unlock(&mountlist_mtx); 849 return (mp); 850 } 851 } 852 mtx_unlock(&mountlist_mtx); 853 CTR2(KTR_VFS, "%s: lookup failed for %p id", __func__, fsid); 854 return ((struct mount *) 0); 855 } 856 857 /* 858 * Lookup a mount point by filesystem identifier, busying it before 859 * returning. 860 * 861 * To avoid congestion on mountlist_mtx, implement simple direct-mapped 862 * cache for popular filesystem identifiers. The cache is lockess, using 863 * the fact that struct mount's are never freed. In worst case we may 864 * get pointer to unmounted or even different filesystem, so we have to 865 * check what we got, and go slow way if so. 866 */ 867 struct mount * 868 vfs_busyfs(fsid_t *fsid) 869 { 870 #define FSID_CACHE_SIZE 256 871 typedef struct mount * volatile vmp_t; 872 static vmp_t cache[FSID_CACHE_SIZE]; 873 struct mount *mp; 874 int error; 875 uint32_t hash; 876 877 CTR2(KTR_VFS, "%s: fsid %p", __func__, fsid); 878 hash = fsid->val[0] ^ fsid->val[1]; 879 hash = (hash >> 16 ^ hash) & (FSID_CACHE_SIZE - 1); 880 mp = cache[hash]; 881 if (mp == NULL || 882 mp->mnt_stat.f_fsid.val[0] != fsid->val[0] || 883 mp->mnt_stat.f_fsid.val[1] != fsid->val[1]) 884 goto slow; 885 if (vfs_busy(mp, 0) != 0) { 886 cache[hash] = NULL; 887 goto slow; 888 } 889 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] && 890 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) 891 return (mp); 892 else 893 vfs_unbusy(mp); 894 895 slow: 896 mtx_lock(&mountlist_mtx); 897 TAILQ_FOREACH(mp, &mountlist, mnt_list) { 898 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] && 899 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) { 900 error = vfs_busy(mp, MBF_MNTLSTLOCK); 901 if (error) { 902 cache[hash] = NULL; 903 mtx_unlock(&mountlist_mtx); 904 return (NULL); 905 } 906 cache[hash] = mp; 907 return (mp); 908 } 909 } 910 CTR2(KTR_VFS, "%s: lookup failed for %p id", __func__, fsid); 911 mtx_unlock(&mountlist_mtx); 912 return ((struct mount *) 0); 913 } 914 915 /* 916 * Check if a user can access privileged mount options. 917 */ 918 int 919 vfs_suser(struct mount *mp, struct thread *td) 920 { 921 int error; 922 923 if (jailed(td->td_ucred)) { 924 /* 925 * If the jail of the calling thread lacks permission for 926 * this type of file system, deny immediately. 927 */ 928 if (!prison_allow(td->td_ucred, mp->mnt_vfc->vfc_prison_flag)) 929 return (EPERM); 930 931 /* 932 * If the file system was mounted outside the jail of the 933 * calling thread, deny immediately. 934 */ 935 if (prison_check(td->td_ucred, mp->mnt_cred) != 0) 936 return (EPERM); 937 } 938 939 /* 940 * If file system supports delegated administration, we don't check 941 * for the PRIV_VFS_MOUNT_OWNER privilege - it will be better verified 942 * by the file system itself. 943 * If this is not the user that did original mount, we check for 944 * the PRIV_VFS_MOUNT_OWNER privilege. 945 */ 946 if (!(mp->mnt_vfc->vfc_flags & VFCF_DELEGADMIN) && 947 mp->mnt_cred->cr_uid != td->td_ucred->cr_uid) { 948 if ((error = priv_check(td, PRIV_VFS_MOUNT_OWNER)) != 0) 949 return (error); 950 } 951 return (0); 952 } 953 954 /* 955 * Get a new unique fsid. Try to make its val[0] unique, since this value 956 * will be used to create fake device numbers for stat(). Also try (but 957 * not so hard) make its val[0] unique mod 2^16, since some emulators only 958 * support 16-bit device numbers. We end up with unique val[0]'s for the 959 * first 2^16 calls and unique val[0]'s mod 2^16 for the first 2^8 calls. 960 * 961 * Keep in mind that several mounts may be running in parallel. Starting 962 * the search one past where the previous search terminated is both a 963 * micro-optimization and a defense against returning the same fsid to 964 * different mounts. 965 */ 966 void 967 vfs_getnewfsid(struct mount *mp) 968 { 969 static uint16_t mntid_base; 970 struct mount *nmp; 971 fsid_t tfsid; 972 int mtype; 973 974 CTR2(KTR_VFS, "%s: mp %p", __func__, mp); 975 mtx_lock(&mntid_mtx); 976 mtype = mp->mnt_vfc->vfc_typenum; 977 tfsid.val[1] = mtype; 978 mtype = (mtype & 0xFF) << 24; 979 for (;;) { 980 tfsid.val[0] = makedev(255, 981 mtype | ((mntid_base & 0xFF00) << 8) | (mntid_base & 0xFF)); 982 mntid_base++; 983 if ((nmp = vfs_getvfs(&tfsid)) == NULL) 984 break; 985 vfs_rel(nmp); 986 } 987 mp->mnt_stat.f_fsid.val[0] = tfsid.val[0]; 988 mp->mnt_stat.f_fsid.val[1] = tfsid.val[1]; 989 mtx_unlock(&mntid_mtx); 990 } 991 992 /* 993 * Knob to control the precision of file timestamps: 994 * 995 * 0 = seconds only; nanoseconds zeroed. 996 * 1 = seconds and nanoseconds, accurate within 1/HZ. 997 * 2 = seconds and nanoseconds, truncated to microseconds. 998 * >=3 = seconds and nanoseconds, maximum precision. 999 */ 1000 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC }; 1001 1002 static int timestamp_precision = TSP_USEC; 1003 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW, 1004 ×tamp_precision, 0, "File timestamp precision (0: seconds, " 1005 "1: sec + ns accurate to 1/HZ, 2: sec + ns truncated to us, " 1006 "3+: sec + ns (max. precision))"); 1007 1008 /* 1009 * Get a current timestamp. 1010 */ 1011 void 1012 vfs_timestamp(struct timespec *tsp) 1013 { 1014 struct timeval tv; 1015 1016 switch (timestamp_precision) { 1017 case TSP_SEC: 1018 tsp->tv_sec = time_second; 1019 tsp->tv_nsec = 0; 1020 break; 1021 case TSP_HZ: 1022 getnanotime(tsp); 1023 break; 1024 case TSP_USEC: 1025 microtime(&tv); 1026 TIMEVAL_TO_TIMESPEC(&tv, tsp); 1027 break; 1028 case TSP_NSEC: 1029 default: 1030 nanotime(tsp); 1031 break; 1032 } 1033 } 1034 1035 /* 1036 * Set vnode attributes to VNOVAL 1037 */ 1038 void 1039 vattr_null(struct vattr *vap) 1040 { 1041 1042 vap->va_type = VNON; 1043 vap->va_size = VNOVAL; 1044 vap->va_bytes = VNOVAL; 1045 vap->va_mode = VNOVAL; 1046 vap->va_nlink = VNOVAL; 1047 vap->va_uid = VNOVAL; 1048 vap->va_gid = VNOVAL; 1049 vap->va_fsid = VNOVAL; 1050 vap->va_fileid = VNOVAL; 1051 vap->va_blocksize = VNOVAL; 1052 vap->va_rdev = VNOVAL; 1053 vap->va_atime.tv_sec = VNOVAL; 1054 vap->va_atime.tv_nsec = VNOVAL; 1055 vap->va_mtime.tv_sec = VNOVAL; 1056 vap->va_mtime.tv_nsec = VNOVAL; 1057 vap->va_ctime.tv_sec = VNOVAL; 1058 vap->va_ctime.tv_nsec = VNOVAL; 1059 vap->va_birthtime.tv_sec = VNOVAL; 1060 vap->va_birthtime.tv_nsec = VNOVAL; 1061 vap->va_flags = VNOVAL; 1062 vap->va_gen = VNOVAL; 1063 vap->va_vaflags = 0; 1064 } 1065 1066 /* 1067 * Try to reduce the total number of vnodes. 1068 * 1069 * This routine (and its user) are buggy in at least the following ways: 1070 * - all parameters were picked years ago when RAM sizes were significantly 1071 * smaller 1072 * - it can pick vnodes based on pages used by the vm object, but filesystems 1073 * like ZFS don't use it making the pick broken 1074 * - since ZFS has its own aging policy it gets partially combated by this one 1075 * - a dedicated method should be provided for filesystems to let them decide 1076 * whether the vnode should be recycled 1077 * 1078 * This routine is called when we have too many vnodes. It attempts 1079 * to free <count> vnodes and will potentially free vnodes that still 1080 * have VM backing store (VM backing store is typically the cause 1081 * of a vnode blowout so we want to do this). Therefore, this operation 1082 * is not considered cheap. 1083 * 1084 * A number of conditions may prevent a vnode from being reclaimed. 1085 * the buffer cache may have references on the vnode, a directory 1086 * vnode may still have references due to the namei cache representing 1087 * underlying files, or the vnode may be in active use. It is not 1088 * desirable to reuse such vnodes. These conditions may cause the 1089 * number of vnodes to reach some minimum value regardless of what 1090 * you set kern.maxvnodes to. Do not set kern.maxvnodes too low. 1091 * 1092 * @param reclaim_nc_src Only reclaim directories with outgoing namecache 1093 * entries if this argument is strue 1094 * @param trigger Only reclaim vnodes with fewer than this many resident 1095 * pages. 1096 * @param target How many vnodes to reclaim. 1097 * @return The number of vnodes that were reclaimed. 1098 */ 1099 static int 1100 vlrureclaim(bool reclaim_nc_src, int trigger, u_long target) 1101 { 1102 struct vnode *vp, *mvp; 1103 struct mount *mp; 1104 u_long done; 1105 bool retried; 1106 1107 mtx_assert(&vnode_list_mtx, MA_OWNED); 1108 1109 retried = false; 1110 done = 0; 1111 1112 mvp = vnode_list_reclaim_marker; 1113 restart: 1114 vp = mvp; 1115 while (done < target) { 1116 vp = TAILQ_NEXT(vp, v_vnodelist); 1117 if (__predict_false(vp == NULL)) 1118 break; 1119 1120 if (__predict_false(vp->v_type == VMARKER)) 1121 continue; 1122 1123 /* 1124 * If it's been deconstructed already, it's still 1125 * referenced, or it exceeds the trigger, skip it. 1126 * Also skip free vnodes. We are trying to make space 1127 * to expand the free list, not reduce it. 1128 */ 1129 if (vp->v_usecount > 0 || vp->v_holdcnt == 0 || 1130 (!reclaim_nc_src && !LIST_EMPTY(&vp->v_cache_src))) 1131 goto next_iter; 1132 1133 if (vp->v_type == VBAD || vp->v_type == VNON) 1134 goto next_iter; 1135 1136 if (!VI_TRYLOCK(vp)) 1137 goto next_iter; 1138 1139 if (vp->v_usecount > 0 || vp->v_holdcnt == 0 || 1140 (!reclaim_nc_src && !LIST_EMPTY(&vp->v_cache_src)) || 1141 vp->v_type == VBAD || vp->v_type == VNON || 1142 (vp->v_object != NULL && 1143 vp->v_object->resident_page_count > trigger)) { 1144 VI_UNLOCK(vp); 1145 goto next_iter; 1146 } 1147 vholdl(vp); 1148 VI_UNLOCK(vp); 1149 TAILQ_REMOVE(&vnode_list, mvp, v_vnodelist); 1150 TAILQ_INSERT_AFTER(&vnode_list, vp, mvp, v_vnodelist); 1151 mtx_unlock(&vnode_list_mtx); 1152 1153 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) { 1154 vdrop(vp); 1155 goto next_iter_unlocked; 1156 } 1157 if (VOP_LOCK(vp, LK_EXCLUSIVE|LK_NOWAIT) != 0) { 1158 vdrop(vp); 1159 vn_finished_write(mp); 1160 goto next_iter_unlocked; 1161 } 1162 1163 VI_LOCK(vp); 1164 if (vp->v_usecount > 0 || 1165 (!reclaim_nc_src && !LIST_EMPTY(&vp->v_cache_src)) || 1166 (vp->v_object != NULL && 1167 vp->v_object->resident_page_count > trigger)) { 1168 VOP_UNLOCK(vp); 1169 vdropl(vp); 1170 vn_finished_write(mp); 1171 goto next_iter_unlocked; 1172 } 1173 counter_u64_add(recycles_count, 1); 1174 vgonel(vp); 1175 VOP_UNLOCK(vp); 1176 vdropl(vp); 1177 vn_finished_write(mp); 1178 done++; 1179 next_iter_unlocked: 1180 if (should_yield()) 1181 kern_yield(PRI_USER); 1182 mtx_lock(&vnode_list_mtx); 1183 goto restart; 1184 next_iter: 1185 MPASS(vp->v_type != VMARKER); 1186 if (!should_yield()) 1187 continue; 1188 TAILQ_REMOVE(&vnode_list, mvp, v_vnodelist); 1189 TAILQ_INSERT_AFTER(&vnode_list, vp, mvp, v_vnodelist); 1190 mtx_unlock(&vnode_list_mtx); 1191 kern_yield(PRI_USER); 1192 mtx_lock(&vnode_list_mtx); 1193 goto restart; 1194 } 1195 if (done == 0 && !retried) { 1196 TAILQ_REMOVE(&vnode_list, mvp, v_vnodelist); 1197 TAILQ_INSERT_HEAD(&vnode_list, mvp, v_vnodelist); 1198 retried = true; 1199 goto restart; 1200 } 1201 return (done); 1202 } 1203 1204 static int max_vnlru_free = 10000; /* limit on vnode free requests per call */ 1205 SYSCTL_INT(_debug, OID_AUTO, max_vnlru_free, CTLFLAG_RW, &max_vnlru_free, 1206 0, 1207 "limit on vnode free requests per call to the vnlru_free routine"); 1208 1209 /* 1210 * Attempt to reduce the free list by the requested amount. 1211 */ 1212 static int 1213 vnlru_free_locked(int count, struct vfsops *mnt_op) 1214 { 1215 struct vnode *vp, *mvp; 1216 struct mount *mp; 1217 int ocount; 1218 1219 mtx_assert(&vnode_list_mtx, MA_OWNED); 1220 if (count > max_vnlru_free) 1221 count = max_vnlru_free; 1222 ocount = count; 1223 mvp = vnode_list_free_marker; 1224 restart: 1225 vp = mvp; 1226 while (count > 0) { 1227 vp = TAILQ_NEXT(vp, v_vnodelist); 1228 if (__predict_false(vp == NULL)) { 1229 TAILQ_REMOVE(&vnode_list, mvp, v_vnodelist); 1230 TAILQ_INSERT_TAIL(&vnode_list, mvp, v_vnodelist); 1231 break; 1232 } 1233 if (__predict_false(vp->v_type == VMARKER)) 1234 continue; 1235 1236 /* 1237 * Don't recycle if our vnode is from different type 1238 * of mount point. Note that mp is type-safe, the 1239 * check does not reach unmapped address even if 1240 * vnode is reclaimed. 1241 * Don't recycle if we can't get the interlock without 1242 * blocking. 1243 */ 1244 if (vp->v_holdcnt > 0 || (mnt_op != NULL && (mp = vp->v_mount) != NULL && 1245 mp->mnt_op != mnt_op) || !VI_TRYLOCK(vp)) { 1246 continue; 1247 } 1248 TAILQ_REMOVE(&vnode_list, mvp, v_vnodelist); 1249 TAILQ_INSERT_AFTER(&vnode_list, vp, mvp, v_vnodelist); 1250 if (__predict_false(vp->v_type == VBAD || vp->v_type == VNON)) { 1251 VI_UNLOCK(vp); 1252 continue; 1253 } 1254 vholdl(vp); 1255 count--; 1256 mtx_unlock(&vnode_list_mtx); 1257 VI_UNLOCK(vp); 1258 vtryrecycle(vp); 1259 vdrop(vp); 1260 mtx_lock(&vnode_list_mtx); 1261 goto restart; 1262 } 1263 return (ocount - count); 1264 } 1265 1266 void 1267 vnlru_free(int count, struct vfsops *mnt_op) 1268 { 1269 1270 mtx_lock(&vnode_list_mtx); 1271 vnlru_free_locked(count, mnt_op); 1272 mtx_unlock(&vnode_list_mtx); 1273 } 1274 1275 static void 1276 vnlru_recalc(void) 1277 { 1278 1279 mtx_assert(&vnode_list_mtx, MA_OWNED); 1280 gapvnodes = imax(desiredvnodes - wantfreevnodes, 100); 1281 vhiwat = gapvnodes / 11; /* 9% -- just under the 10% in vlrureclaim() */ 1282 vlowat = vhiwat / 2; 1283 } 1284 1285 /* 1286 * Attempt to recycle vnodes in a context that is always safe to block. 1287 * Calling vlrurecycle() from the bowels of filesystem code has some 1288 * interesting deadlock problems. 1289 */ 1290 static struct proc *vnlruproc; 1291 static int vnlruproc_sig; 1292 1293 /* 1294 * The main freevnodes counter is only updated when threads requeue their vnode 1295 * batches. CPUs are conditionally walked to compute a more accurate total. 1296 * 1297 * Limit how much of a slop are we willing to tolerate. Note: the actual value 1298 * at any given moment can still exceed slop, but it should not be by significant 1299 * margin in practice. 1300 */ 1301 #define VNLRU_FREEVNODES_SLOP 128 1302 1303 static u_long 1304 vnlru_read_freevnodes(void) 1305 { 1306 struct vdbatch *vd; 1307 long slop; 1308 int cpu; 1309 1310 mtx_assert(&vnode_list_mtx, MA_OWNED); 1311 if (freevnodes > freevnodes_old) 1312 slop = freevnodes - freevnodes_old; 1313 else 1314 slop = freevnodes_old - freevnodes; 1315 if (slop < VNLRU_FREEVNODES_SLOP) 1316 return (freevnodes >= 0 ? freevnodes : 0); 1317 freevnodes_old = freevnodes; 1318 CPU_FOREACH(cpu) { 1319 vd = DPCPU_ID_PTR((cpu), vd); 1320 freevnodes_old += vd->freevnodes; 1321 } 1322 return (freevnodes_old >= 0 ? freevnodes_old : 0); 1323 } 1324 1325 static bool 1326 vnlru_under(u_long rnumvnodes, u_long limit) 1327 { 1328 u_long rfreevnodes, space; 1329 1330 if (__predict_false(rnumvnodes > desiredvnodes)) 1331 return (true); 1332 1333 space = desiredvnodes - rnumvnodes; 1334 if (space < limit) { 1335 rfreevnodes = vnlru_read_freevnodes(); 1336 if (rfreevnodes > wantfreevnodes) 1337 space += rfreevnodes - wantfreevnodes; 1338 } 1339 return (space < limit); 1340 } 1341 1342 static bool 1343 vnlru_under_unlocked(u_long rnumvnodes, u_long limit) 1344 { 1345 long rfreevnodes, space; 1346 1347 if (__predict_false(rnumvnodes > desiredvnodes)) 1348 return (true); 1349 1350 space = desiredvnodes - rnumvnodes; 1351 if (space < limit) { 1352 rfreevnodes = atomic_load_long(&freevnodes); 1353 if (rfreevnodes > wantfreevnodes) 1354 space += rfreevnodes - wantfreevnodes; 1355 } 1356 return (space < limit); 1357 } 1358 1359 static void 1360 vnlru_kick(void) 1361 { 1362 1363 mtx_assert(&vnode_list_mtx, MA_OWNED); 1364 if (vnlruproc_sig == 0) { 1365 vnlruproc_sig = 1; 1366 wakeup(vnlruproc); 1367 } 1368 } 1369 1370 static void 1371 vnlru_proc(void) 1372 { 1373 u_long rnumvnodes, rfreevnodes, target; 1374 unsigned long onumvnodes; 1375 int done, force, trigger, usevnodes; 1376 bool reclaim_nc_src, want_reread; 1377 1378 EVENTHANDLER_REGISTER(shutdown_pre_sync, kproc_shutdown, vnlruproc, 1379 SHUTDOWN_PRI_FIRST); 1380 1381 force = 0; 1382 want_reread = false; 1383 for (;;) { 1384 kproc_suspend_check(vnlruproc); 1385 mtx_lock(&vnode_list_mtx); 1386 rnumvnodes = atomic_load_long(&numvnodes); 1387 1388 if (want_reread) { 1389 force = vnlru_under(numvnodes, vhiwat) ? 1 : 0; 1390 want_reread = false; 1391 } 1392 1393 /* 1394 * If numvnodes is too large (due to desiredvnodes being 1395 * adjusted using its sysctl, or emergency growth), first 1396 * try to reduce it by discarding from the free list. 1397 */ 1398 if (rnumvnodes > desiredvnodes) { 1399 vnlru_free_locked(rnumvnodes - desiredvnodes, NULL); 1400 rnumvnodes = atomic_load_long(&numvnodes); 1401 } 1402 /* 1403 * Sleep if the vnode cache is in a good state. This is 1404 * when it is not over-full and has space for about a 4% 1405 * or 9% expansion (by growing its size or inexcessively 1406 * reducing its free list). Otherwise, try to reclaim 1407 * space for a 10% expansion. 1408 */ 1409 if (vstir && force == 0) { 1410 force = 1; 1411 vstir = 0; 1412 } 1413 if (force == 0 && !vnlru_under(rnumvnodes, vlowat)) { 1414 vnlruproc_sig = 0; 1415 wakeup(&vnlruproc_sig); 1416 msleep(vnlruproc, &vnode_list_mtx, 1417 PVFS|PDROP, "vlruwt", hz); 1418 continue; 1419 } 1420 rfreevnodes = vnlru_read_freevnodes(); 1421 1422 onumvnodes = rnumvnodes; 1423 /* 1424 * Calculate parameters for recycling. These are the same 1425 * throughout the loop to give some semblance of fairness. 1426 * The trigger point is to avoid recycling vnodes with lots 1427 * of resident pages. We aren't trying to free memory; we 1428 * are trying to recycle or at least free vnodes. 1429 */ 1430 if (rnumvnodes <= desiredvnodes) 1431 usevnodes = rnumvnodes - rfreevnodes; 1432 else 1433 usevnodes = rnumvnodes; 1434 if (usevnodes <= 0) 1435 usevnodes = 1; 1436 /* 1437 * The trigger value is is chosen to give a conservatively 1438 * large value to ensure that it alone doesn't prevent 1439 * making progress. The value can easily be so large that 1440 * it is effectively infinite in some congested and 1441 * misconfigured cases, and this is necessary. Normally 1442 * it is about 8 to 100 (pages), which is quite large. 1443 */ 1444 trigger = vm_cnt.v_page_count * 2 / usevnodes; 1445 if (force < 2) 1446 trigger = vsmalltrigger; 1447 reclaim_nc_src = force >= 3; 1448 target = rnumvnodes * (int64_t)gapvnodes / imax(desiredvnodes, 1); 1449 target = target / 10 + 1; 1450 done = vlrureclaim(reclaim_nc_src, trigger, target); 1451 mtx_unlock(&vnode_list_mtx); 1452 if (onumvnodes > desiredvnodes && numvnodes <= desiredvnodes) 1453 uma_reclaim(UMA_RECLAIM_DRAIN); 1454 if (done == 0) { 1455 if (force == 0 || force == 1) { 1456 force = 2; 1457 continue; 1458 } 1459 if (force == 2) { 1460 force = 3; 1461 continue; 1462 } 1463 want_reread = true; 1464 force = 0; 1465 vnlru_nowhere++; 1466 tsleep(vnlruproc, PPAUSE, "vlrup", hz * 3); 1467 } else { 1468 want_reread = true; 1469 kern_yield(PRI_USER); 1470 } 1471 } 1472 } 1473 1474 static struct kproc_desc vnlru_kp = { 1475 "vnlru", 1476 vnlru_proc, 1477 &vnlruproc 1478 }; 1479 SYSINIT(vnlru, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, 1480 &vnlru_kp); 1481 1482 /* 1483 * Routines having to do with the management of the vnode table. 1484 */ 1485 1486 /* 1487 * Try to recycle a freed vnode. We abort if anyone picks up a reference 1488 * before we actually vgone(). This function must be called with the vnode 1489 * held to prevent the vnode from being returned to the free list midway 1490 * through vgone(). 1491 */ 1492 static int 1493 vtryrecycle(struct vnode *vp) 1494 { 1495 struct mount *vnmp; 1496 1497 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 1498 VNASSERT(vp->v_holdcnt, vp, 1499 ("vtryrecycle: Recycling vp %p without a reference.", vp)); 1500 /* 1501 * This vnode may found and locked via some other list, if so we 1502 * can't recycle it yet. 1503 */ 1504 if (VOP_LOCK(vp, LK_EXCLUSIVE | LK_NOWAIT) != 0) { 1505 CTR2(KTR_VFS, 1506 "%s: impossible to recycle, vp %p lock is already held", 1507 __func__, vp); 1508 return (EWOULDBLOCK); 1509 } 1510 /* 1511 * Don't recycle if its filesystem is being suspended. 1512 */ 1513 if (vn_start_write(vp, &vnmp, V_NOWAIT) != 0) { 1514 VOP_UNLOCK(vp); 1515 CTR2(KTR_VFS, 1516 "%s: impossible to recycle, cannot start the write for %p", 1517 __func__, vp); 1518 return (EBUSY); 1519 } 1520 /* 1521 * If we got this far, we need to acquire the interlock and see if 1522 * anyone picked up this vnode from another list. If not, we will 1523 * mark it with DOOMED via vgonel() so that anyone who does find it 1524 * will skip over it. 1525 */ 1526 VI_LOCK(vp); 1527 if (vp->v_usecount) { 1528 VOP_UNLOCK(vp); 1529 VI_UNLOCK(vp); 1530 vn_finished_write(vnmp); 1531 CTR2(KTR_VFS, 1532 "%s: impossible to recycle, %p is already referenced", 1533 __func__, vp); 1534 return (EBUSY); 1535 } 1536 if (!VN_IS_DOOMED(vp)) { 1537 counter_u64_add(recycles_free_count, 1); 1538 vgonel(vp); 1539 } 1540 VOP_UNLOCK(vp); 1541 VI_UNLOCK(vp); 1542 vn_finished_write(vnmp); 1543 return (0); 1544 } 1545 1546 /* 1547 * Allocate a new vnode. 1548 * 1549 * The operation never returns an error. Returning an error was disabled 1550 * in r145385 (dated 2005) with the following comment: 1551 * 1552 * XXX Not all VFS_VGET/ffs_vget callers check returns. 1553 * 1554 * Given the age of this commit (almost 15 years at the time of writing this 1555 * comment) restoring the ability to fail requires a significant audit of 1556 * all codepaths. 1557 * 1558 * The routine can try to free a vnode or stall for up to 1 second waiting for 1559 * vnlru to clear things up, but ultimately always performs a M_WAITOK allocation. 1560 */ 1561 static u_long vn_alloc_cyclecount; 1562 1563 static struct vnode * __noinline 1564 vn_alloc_hard(struct mount *mp) 1565 { 1566 u_long rnumvnodes, rfreevnodes; 1567 1568 mtx_lock(&vnode_list_mtx); 1569 rnumvnodes = atomic_load_long(&numvnodes); 1570 if (rnumvnodes + 1 < desiredvnodes) { 1571 vn_alloc_cyclecount = 0; 1572 goto alloc; 1573 } 1574 rfreevnodes = vnlru_read_freevnodes(); 1575 if (vn_alloc_cyclecount++ >= rfreevnodes) { 1576 vn_alloc_cyclecount = 0; 1577 vstir = 1; 1578 } 1579 /* 1580 * Grow the vnode cache if it will not be above its target max 1581 * after growing. Otherwise, if the free list is nonempty, try 1582 * to reclaim 1 item from it before growing the cache (possibly 1583 * above its target max if the reclamation failed or is delayed). 1584 * Otherwise, wait for some space. In all cases, schedule 1585 * vnlru_proc() if we are getting short of space. The watermarks 1586 * should be chosen so that we never wait or even reclaim from 1587 * the free list to below its target minimum. 1588 */ 1589 if (vnlru_free_locked(1, NULL) > 0) 1590 goto alloc; 1591 if (mp == NULL || (mp->mnt_kern_flag & MNTK_SUSPEND) == 0) { 1592 /* 1593 * Wait for space for a new vnode. 1594 */ 1595 vnlru_kick(); 1596 msleep(&vnlruproc_sig, &vnode_list_mtx, PVFS, "vlruwk", hz); 1597 if (atomic_load_long(&numvnodes) + 1 > desiredvnodes && 1598 vnlru_read_freevnodes() > 1) 1599 vnlru_free_locked(1, NULL); 1600 } 1601 alloc: 1602 rnumvnodes = atomic_fetchadd_long(&numvnodes, 1) + 1; 1603 if (vnlru_under(rnumvnodes, vlowat)) 1604 vnlru_kick(); 1605 mtx_unlock(&vnode_list_mtx); 1606 return (uma_zalloc(vnode_zone, M_WAITOK)); 1607 } 1608 1609 static struct vnode * 1610 vn_alloc(struct mount *mp) 1611 { 1612 u_long rnumvnodes; 1613 1614 if (__predict_false(vn_alloc_cyclecount != 0)) 1615 return (vn_alloc_hard(mp)); 1616 rnumvnodes = atomic_fetchadd_long(&numvnodes, 1) + 1; 1617 if (__predict_false(vnlru_under_unlocked(rnumvnodes, vlowat))) { 1618 atomic_subtract_long(&numvnodes, 1); 1619 return (vn_alloc_hard(mp)); 1620 } 1621 1622 return (uma_zalloc(vnode_zone, M_WAITOK)); 1623 } 1624 1625 static void 1626 vn_free(struct vnode *vp) 1627 { 1628 1629 atomic_subtract_long(&numvnodes, 1); 1630 uma_zfree(vnode_zone, vp); 1631 } 1632 1633 /* 1634 * Return the next vnode from the free list. 1635 */ 1636 int 1637 getnewvnode(const char *tag, struct mount *mp, struct vop_vector *vops, 1638 struct vnode **vpp) 1639 { 1640 struct vnode *vp; 1641 struct thread *td; 1642 struct lock_object *lo; 1643 1644 CTR3(KTR_VFS, "%s: mp %p with tag %s", __func__, mp, tag); 1645 1646 KASSERT(vops->registered, 1647 ("%s: not registered vector op %p\n", __func__, vops)); 1648 1649 td = curthread; 1650 if (td->td_vp_reserved != NULL) { 1651 vp = td->td_vp_reserved; 1652 td->td_vp_reserved = NULL; 1653 } else { 1654 vp = vn_alloc(mp); 1655 } 1656 counter_u64_add(vnodes_created, 1); 1657 /* 1658 * Locks are given the generic name "vnode" when created. 1659 * Follow the historic practice of using the filesystem 1660 * name when they allocated, e.g., "zfs", "ufs", "nfs, etc. 1661 * 1662 * Locks live in a witness group keyed on their name. Thus, 1663 * when a lock is renamed, it must also move from the witness 1664 * group of its old name to the witness group of its new name. 1665 * 1666 * The change only needs to be made when the vnode moves 1667 * from one filesystem type to another. We ensure that each 1668 * filesystem use a single static name pointer for its tag so 1669 * that we can compare pointers rather than doing a strcmp(). 1670 */ 1671 lo = &vp->v_vnlock->lock_object; 1672 #ifdef WITNESS 1673 if (lo->lo_name != tag) { 1674 #endif 1675 lo->lo_name = tag; 1676 #ifdef WITNESS 1677 WITNESS_DESTROY(lo); 1678 WITNESS_INIT(lo, tag); 1679 } 1680 #endif 1681 /* 1682 * By default, don't allow shared locks unless filesystems opt-in. 1683 */ 1684 vp->v_vnlock->lock_object.lo_flags |= LK_NOSHARE; 1685 /* 1686 * Finalize various vnode identity bits. 1687 */ 1688 KASSERT(vp->v_object == NULL, ("stale v_object %p", vp)); 1689 KASSERT(vp->v_lockf == NULL, ("stale v_lockf %p", vp)); 1690 KASSERT(vp->v_pollinfo == NULL, ("stale v_pollinfo %p", vp)); 1691 vp->v_type = VNON; 1692 vp->v_op = vops; 1693 v_init_counters(vp); 1694 vp->v_bufobj.bo_ops = &buf_ops_bio; 1695 #ifdef DIAGNOSTIC 1696 if (mp == NULL && vops != &dead_vnodeops) 1697 printf("NULL mp in getnewvnode(9), tag %s\n", tag); 1698 #endif 1699 #ifdef MAC 1700 mac_vnode_init(vp); 1701 if (mp != NULL && (mp->mnt_flag & MNT_MULTILABEL) == 0) 1702 mac_vnode_associate_singlelabel(mp, vp); 1703 #endif 1704 if (mp != NULL) { 1705 vp->v_bufobj.bo_bsize = mp->mnt_stat.f_iosize; 1706 if ((mp->mnt_kern_flag & MNTK_NOKNOTE) != 0) 1707 vp->v_vflag |= VV_NOKNOTE; 1708 } 1709 1710 /* 1711 * For the filesystems which do not use vfs_hash_insert(), 1712 * still initialize v_hash to have vfs_hash_index() useful. 1713 * E.g., nullfs uses vfs_hash_index() on the lower vnode for 1714 * its own hashing. 1715 */ 1716 vp->v_hash = (uintptr_t)vp >> vnsz2log; 1717 1718 *vpp = vp; 1719 return (0); 1720 } 1721 1722 void 1723 getnewvnode_reserve(void) 1724 { 1725 struct thread *td; 1726 1727 td = curthread; 1728 MPASS(td->td_vp_reserved == NULL); 1729 td->td_vp_reserved = vn_alloc(NULL); 1730 } 1731 1732 void 1733 getnewvnode_drop_reserve(void) 1734 { 1735 struct thread *td; 1736 1737 td = curthread; 1738 if (td->td_vp_reserved != NULL) { 1739 vn_free(td->td_vp_reserved); 1740 td->td_vp_reserved = NULL; 1741 } 1742 } 1743 1744 static void 1745 freevnode(struct vnode *vp) 1746 { 1747 struct bufobj *bo; 1748 1749 /* 1750 * The vnode has been marked for destruction, so free it. 1751 * 1752 * The vnode will be returned to the zone where it will 1753 * normally remain until it is needed for another vnode. We 1754 * need to cleanup (or verify that the cleanup has already 1755 * been done) any residual data left from its current use 1756 * so as not to contaminate the freshly allocated vnode. 1757 */ 1758 CTR2(KTR_VFS, "%s: destroying the vnode %p", __func__, vp); 1759 bo = &vp->v_bufobj; 1760 VNASSERT(vp->v_data == NULL, vp, ("cleaned vnode isn't")); 1761 VNASSERT(vp->v_holdcnt == 0, vp, ("Non-zero hold count")); 1762 VNASSERT(vp->v_usecount == 0, vp, ("Non-zero use count")); 1763 VNASSERT(vp->v_writecount == 0, vp, ("Non-zero write count")); 1764 VNASSERT(bo->bo_numoutput == 0, vp, ("Clean vnode has pending I/O's")); 1765 VNASSERT(bo->bo_clean.bv_cnt == 0, vp, ("cleanbufcnt not 0")); 1766 VNASSERT(pctrie_is_empty(&bo->bo_clean.bv_root), vp, 1767 ("clean blk trie not empty")); 1768 VNASSERT(bo->bo_dirty.bv_cnt == 0, vp, ("dirtybufcnt not 0")); 1769 VNASSERT(pctrie_is_empty(&bo->bo_dirty.bv_root), vp, 1770 ("dirty blk trie not empty")); 1771 VNASSERT(TAILQ_EMPTY(&vp->v_cache_dst), vp, ("vp has namecache dst")); 1772 VNASSERT(LIST_EMPTY(&vp->v_cache_src), vp, ("vp has namecache src")); 1773 VNASSERT(vp->v_cache_dd == NULL, vp, ("vp has namecache for ..")); 1774 VNASSERT(TAILQ_EMPTY(&vp->v_rl.rl_waiters), vp, 1775 ("Dangling rangelock waiters")); 1776 VI_UNLOCK(vp); 1777 #ifdef MAC 1778 mac_vnode_destroy(vp); 1779 #endif 1780 if (vp->v_pollinfo != NULL) { 1781 destroy_vpollinfo(vp->v_pollinfo); 1782 vp->v_pollinfo = NULL; 1783 } 1784 #ifdef INVARIANTS 1785 /* XXX Elsewhere we detect an already freed vnode via NULL v_op. */ 1786 vp->v_op = NULL; 1787 #endif 1788 vp->v_mountedhere = NULL; 1789 vp->v_unpcb = NULL; 1790 vp->v_rdev = NULL; 1791 vp->v_fifoinfo = NULL; 1792 vp->v_lasta = vp->v_clen = vp->v_cstart = vp->v_lastw = 0; 1793 vp->v_irflag = 0; 1794 vp->v_iflag = 0; 1795 vp->v_vflag = 0; 1796 bo->bo_flag = 0; 1797 vn_free(vp); 1798 } 1799 1800 /* 1801 * Delete from old mount point vnode list, if on one. 1802 */ 1803 static void 1804 delmntque(struct vnode *vp) 1805 { 1806 struct mount *mp; 1807 1808 mp = vp->v_mount; 1809 if (mp == NULL) 1810 return; 1811 MNT_ILOCK(mp); 1812 VI_LOCK(vp); 1813 if (vp->v_mflag & VMP_LAZYLIST) { 1814 mtx_lock(&mp->mnt_listmtx); 1815 if (vp->v_mflag & VMP_LAZYLIST) { 1816 vp->v_mflag &= ~VMP_LAZYLIST; 1817 TAILQ_REMOVE(&mp->mnt_lazyvnodelist, vp, v_lazylist); 1818 mp->mnt_lazyvnodelistsize--; 1819 } 1820 mtx_unlock(&mp->mnt_listmtx); 1821 } 1822 vp->v_mount = NULL; 1823 VI_UNLOCK(vp); 1824 VNASSERT(mp->mnt_nvnodelistsize > 0, vp, 1825 ("bad mount point vnode list size")); 1826 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes); 1827 mp->mnt_nvnodelistsize--; 1828 MNT_REL(mp); 1829 MNT_IUNLOCK(mp); 1830 } 1831 1832 static void 1833 insmntque_stddtr(struct vnode *vp, void *dtr_arg) 1834 { 1835 1836 vp->v_data = NULL; 1837 vp->v_op = &dead_vnodeops; 1838 vgone(vp); 1839 vput(vp); 1840 } 1841 1842 /* 1843 * Insert into list of vnodes for the new mount point, if available. 1844 */ 1845 int 1846 insmntque1(struct vnode *vp, struct mount *mp, 1847 void (*dtr)(struct vnode *, void *), void *dtr_arg) 1848 { 1849 1850 KASSERT(vp->v_mount == NULL, 1851 ("insmntque: vnode already on per mount vnode list")); 1852 VNASSERT(mp != NULL, vp, ("Don't call insmntque(foo, NULL)")); 1853 ASSERT_VOP_ELOCKED(vp, "insmntque: non-locked vp"); 1854 1855 /* 1856 * We acquire the vnode interlock early to ensure that the 1857 * vnode cannot be recycled by another process releasing a 1858 * holdcnt on it before we get it on both the vnode list 1859 * and the active vnode list. The mount mutex protects only 1860 * manipulation of the vnode list and the vnode freelist 1861 * mutex protects only manipulation of the active vnode list. 1862 * Hence the need to hold the vnode interlock throughout. 1863 */ 1864 MNT_ILOCK(mp); 1865 VI_LOCK(vp); 1866 if (((mp->mnt_kern_flag & MNTK_UNMOUNT) != 0 && 1867 ((mp->mnt_kern_flag & MNTK_UNMOUNTF) != 0 || 1868 mp->mnt_nvnodelistsize == 0)) && 1869 (vp->v_vflag & VV_FORCEINSMQ) == 0) { 1870 VI_UNLOCK(vp); 1871 MNT_IUNLOCK(mp); 1872 if (dtr != NULL) 1873 dtr(vp, dtr_arg); 1874 return (EBUSY); 1875 } 1876 vp->v_mount = mp; 1877 MNT_REF(mp); 1878 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes); 1879 VNASSERT(mp->mnt_nvnodelistsize >= 0, vp, 1880 ("neg mount point vnode list size")); 1881 mp->mnt_nvnodelistsize++; 1882 VI_UNLOCK(vp); 1883 MNT_IUNLOCK(mp); 1884 return (0); 1885 } 1886 1887 int 1888 insmntque(struct vnode *vp, struct mount *mp) 1889 { 1890 1891 return (insmntque1(vp, mp, insmntque_stddtr, NULL)); 1892 } 1893 1894 /* 1895 * Flush out and invalidate all buffers associated with a bufobj 1896 * Called with the underlying object locked. 1897 */ 1898 int 1899 bufobj_invalbuf(struct bufobj *bo, int flags, int slpflag, int slptimeo) 1900 { 1901 int error; 1902 1903 BO_LOCK(bo); 1904 if (flags & V_SAVE) { 1905 error = bufobj_wwait(bo, slpflag, slptimeo); 1906 if (error) { 1907 BO_UNLOCK(bo); 1908 return (error); 1909 } 1910 if (bo->bo_dirty.bv_cnt > 0) { 1911 BO_UNLOCK(bo); 1912 if ((error = BO_SYNC(bo, MNT_WAIT)) != 0) 1913 return (error); 1914 /* 1915 * XXX We could save a lock/unlock if this was only 1916 * enabled under INVARIANTS 1917 */ 1918 BO_LOCK(bo); 1919 if (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0) 1920 panic("vinvalbuf: dirty bufs"); 1921 } 1922 } 1923 /* 1924 * If you alter this loop please notice that interlock is dropped and 1925 * reacquired in flushbuflist. Special care is needed to ensure that 1926 * no race conditions occur from this. 1927 */ 1928 do { 1929 error = flushbuflist(&bo->bo_clean, 1930 flags, bo, slpflag, slptimeo); 1931 if (error == 0 && !(flags & V_CLEANONLY)) 1932 error = flushbuflist(&bo->bo_dirty, 1933 flags, bo, slpflag, slptimeo); 1934 if (error != 0 && error != EAGAIN) { 1935 BO_UNLOCK(bo); 1936 return (error); 1937 } 1938 } while (error != 0); 1939 1940 /* 1941 * Wait for I/O to complete. XXX needs cleaning up. The vnode can 1942 * have write I/O in-progress but if there is a VM object then the 1943 * VM object can also have read-I/O in-progress. 1944 */ 1945 do { 1946 bufobj_wwait(bo, 0, 0); 1947 if ((flags & V_VMIO) == 0 && bo->bo_object != NULL) { 1948 BO_UNLOCK(bo); 1949 vm_object_pip_wait_unlocked(bo->bo_object, "bovlbx"); 1950 BO_LOCK(bo); 1951 } 1952 } while (bo->bo_numoutput > 0); 1953 BO_UNLOCK(bo); 1954 1955 /* 1956 * Destroy the copy in the VM cache, too. 1957 */ 1958 if (bo->bo_object != NULL && 1959 (flags & (V_ALT | V_NORMAL | V_CLEANONLY | V_VMIO)) == 0) { 1960 VM_OBJECT_WLOCK(bo->bo_object); 1961 vm_object_page_remove(bo->bo_object, 0, 0, (flags & V_SAVE) ? 1962 OBJPR_CLEANONLY : 0); 1963 VM_OBJECT_WUNLOCK(bo->bo_object); 1964 } 1965 1966 #ifdef INVARIANTS 1967 BO_LOCK(bo); 1968 if ((flags & (V_ALT | V_NORMAL | V_CLEANONLY | V_VMIO | 1969 V_ALLOWCLEAN)) == 0 && (bo->bo_dirty.bv_cnt > 0 || 1970 bo->bo_clean.bv_cnt > 0)) 1971 panic("vinvalbuf: flush failed"); 1972 if ((flags & (V_ALT | V_NORMAL | V_CLEANONLY | V_VMIO)) == 0 && 1973 bo->bo_dirty.bv_cnt > 0) 1974 panic("vinvalbuf: flush dirty failed"); 1975 BO_UNLOCK(bo); 1976 #endif 1977 return (0); 1978 } 1979 1980 /* 1981 * Flush out and invalidate all buffers associated with a vnode. 1982 * Called with the underlying object locked. 1983 */ 1984 int 1985 vinvalbuf(struct vnode *vp, int flags, int slpflag, int slptimeo) 1986 { 1987 1988 CTR3(KTR_VFS, "%s: vp %p with flags %d", __func__, vp, flags); 1989 ASSERT_VOP_LOCKED(vp, "vinvalbuf"); 1990 if (vp->v_object != NULL && vp->v_object->handle != vp) 1991 return (0); 1992 return (bufobj_invalbuf(&vp->v_bufobj, flags, slpflag, slptimeo)); 1993 } 1994 1995 /* 1996 * Flush out buffers on the specified list. 1997 * 1998 */ 1999 static int 2000 flushbuflist(struct bufv *bufv, int flags, struct bufobj *bo, int slpflag, 2001 int slptimeo) 2002 { 2003 struct buf *bp, *nbp; 2004 int retval, error; 2005 daddr_t lblkno; 2006 b_xflags_t xflags; 2007 2008 ASSERT_BO_WLOCKED(bo); 2009 2010 retval = 0; 2011 TAILQ_FOREACH_SAFE(bp, &bufv->bv_hd, b_bobufs, nbp) { 2012 /* 2013 * If we are flushing both V_NORMAL and V_ALT buffers then 2014 * do not skip any buffers. If we are flushing only V_NORMAL 2015 * buffers then skip buffers marked as BX_ALTDATA. If we are 2016 * flushing only V_ALT buffers then skip buffers not marked 2017 * as BX_ALTDATA. 2018 */ 2019 if (((flags & (V_NORMAL | V_ALT)) != (V_NORMAL | V_ALT)) && 2020 (((flags & V_NORMAL) && (bp->b_xflags & BX_ALTDATA) != 0) || 2021 ((flags & V_ALT) && (bp->b_xflags & BX_ALTDATA) == 0))) { 2022 continue; 2023 } 2024 if (nbp != NULL) { 2025 lblkno = nbp->b_lblkno; 2026 xflags = nbp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN); 2027 } 2028 retval = EAGAIN; 2029 error = BUF_TIMELOCK(bp, 2030 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, BO_LOCKPTR(bo), 2031 "flushbuf", slpflag, slptimeo); 2032 if (error) { 2033 BO_LOCK(bo); 2034 return (error != ENOLCK ? error : EAGAIN); 2035 } 2036 KASSERT(bp->b_bufobj == bo, 2037 ("bp %p wrong b_bufobj %p should be %p", 2038 bp, bp->b_bufobj, bo)); 2039 /* 2040 * XXX Since there are no node locks for NFS, I 2041 * believe there is a slight chance that a delayed 2042 * write will occur while sleeping just above, so 2043 * check for it. 2044 */ 2045 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) && 2046 (flags & V_SAVE)) { 2047 bremfree(bp); 2048 bp->b_flags |= B_ASYNC; 2049 bwrite(bp); 2050 BO_LOCK(bo); 2051 return (EAGAIN); /* XXX: why not loop ? */ 2052 } 2053 bremfree(bp); 2054 bp->b_flags |= (B_INVAL | B_RELBUF); 2055 bp->b_flags &= ~B_ASYNC; 2056 brelse(bp); 2057 BO_LOCK(bo); 2058 if (nbp == NULL) 2059 break; 2060 nbp = gbincore(bo, lblkno); 2061 if (nbp == NULL || (nbp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) 2062 != xflags) 2063 break; /* nbp invalid */ 2064 } 2065 return (retval); 2066 } 2067 2068 int 2069 bnoreuselist(struct bufv *bufv, struct bufobj *bo, daddr_t startn, daddr_t endn) 2070 { 2071 struct buf *bp; 2072 int error; 2073 daddr_t lblkno; 2074 2075 ASSERT_BO_LOCKED(bo); 2076 2077 for (lblkno = startn;;) { 2078 again: 2079 bp = BUF_PCTRIE_LOOKUP_GE(&bufv->bv_root, lblkno); 2080 if (bp == NULL || bp->b_lblkno >= endn || 2081 bp->b_lblkno < startn) 2082 break; 2083 error = BUF_TIMELOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL | 2084 LK_INTERLOCK, BO_LOCKPTR(bo), "brlsfl", 0, 0); 2085 if (error != 0) { 2086 BO_RLOCK(bo); 2087 if (error == ENOLCK) 2088 goto again; 2089 return (error); 2090 } 2091 KASSERT(bp->b_bufobj == bo, 2092 ("bp %p wrong b_bufobj %p should be %p", 2093 bp, bp->b_bufobj, bo)); 2094 lblkno = bp->b_lblkno + 1; 2095 if ((bp->b_flags & B_MANAGED) == 0) 2096 bremfree(bp); 2097 bp->b_flags |= B_RELBUF; 2098 /* 2099 * In the VMIO case, use the B_NOREUSE flag to hint that the 2100 * pages backing each buffer in the range are unlikely to be 2101 * reused. Dirty buffers will have the hint applied once 2102 * they've been written. 2103 */ 2104 if ((bp->b_flags & B_VMIO) != 0) 2105 bp->b_flags |= B_NOREUSE; 2106 brelse(bp); 2107 BO_RLOCK(bo); 2108 } 2109 return (0); 2110 } 2111 2112 /* 2113 * Truncate a file's buffer and pages to a specified length. This 2114 * is in lieu of the old vinvalbuf mechanism, which performed unneeded 2115 * sync activity. 2116 */ 2117 int 2118 vtruncbuf(struct vnode *vp, off_t length, int blksize) 2119 { 2120 struct buf *bp, *nbp; 2121 struct bufobj *bo; 2122 daddr_t startlbn; 2123 2124 CTR4(KTR_VFS, "%s: vp %p with block %d:%ju", __func__, 2125 vp, blksize, (uintmax_t)length); 2126 2127 /* 2128 * Round up to the *next* lbn. 2129 */ 2130 startlbn = howmany(length, blksize); 2131 2132 ASSERT_VOP_LOCKED(vp, "vtruncbuf"); 2133 2134 bo = &vp->v_bufobj; 2135 restart_unlocked: 2136 BO_LOCK(bo); 2137 2138 while (v_inval_buf_range_locked(vp, bo, startlbn, INT64_MAX) == EAGAIN) 2139 ; 2140 2141 if (length > 0) { 2142 restartsync: 2143 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) { 2144 if (bp->b_lblkno > 0) 2145 continue; 2146 /* 2147 * Since we hold the vnode lock this should only 2148 * fail if we're racing with the buf daemon. 2149 */ 2150 if (BUF_LOCK(bp, 2151 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, 2152 BO_LOCKPTR(bo)) == ENOLCK) 2153 goto restart_unlocked; 2154 2155 VNASSERT((bp->b_flags & B_DELWRI), vp, 2156 ("buf(%p) on dirty queue without DELWRI", bp)); 2157 2158 bremfree(bp); 2159 bawrite(bp); 2160 BO_LOCK(bo); 2161 goto restartsync; 2162 } 2163 } 2164 2165 bufobj_wwait(bo, 0, 0); 2166 BO_UNLOCK(bo); 2167 vnode_pager_setsize(vp, length); 2168 2169 return (0); 2170 } 2171 2172 /* 2173 * Invalidate the cached pages of a file's buffer within the range of block 2174 * numbers [startlbn, endlbn). 2175 */ 2176 void 2177 v_inval_buf_range(struct vnode *vp, daddr_t startlbn, daddr_t endlbn, 2178 int blksize) 2179 { 2180 struct bufobj *bo; 2181 off_t start, end; 2182 2183 ASSERT_VOP_LOCKED(vp, "v_inval_buf_range"); 2184 2185 start = blksize * startlbn; 2186 end = blksize * endlbn; 2187 2188 bo = &vp->v_bufobj; 2189 BO_LOCK(bo); 2190 MPASS(blksize == bo->bo_bsize); 2191 2192 while (v_inval_buf_range_locked(vp, bo, startlbn, endlbn) == EAGAIN) 2193 ; 2194 2195 BO_UNLOCK(bo); 2196 vn_pages_remove(vp, OFF_TO_IDX(start), OFF_TO_IDX(end + PAGE_SIZE - 1)); 2197 } 2198 2199 static int 2200 v_inval_buf_range_locked(struct vnode *vp, struct bufobj *bo, 2201 daddr_t startlbn, daddr_t endlbn) 2202 { 2203 struct buf *bp, *nbp; 2204 bool anyfreed; 2205 2206 ASSERT_VOP_LOCKED(vp, "v_inval_buf_range_locked"); 2207 ASSERT_BO_LOCKED(bo); 2208 2209 do { 2210 anyfreed = false; 2211 TAILQ_FOREACH_SAFE(bp, &bo->bo_clean.bv_hd, b_bobufs, nbp) { 2212 if (bp->b_lblkno < startlbn || bp->b_lblkno >= endlbn) 2213 continue; 2214 if (BUF_LOCK(bp, 2215 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, 2216 BO_LOCKPTR(bo)) == ENOLCK) { 2217 BO_LOCK(bo); 2218 return (EAGAIN); 2219 } 2220 2221 bremfree(bp); 2222 bp->b_flags |= B_INVAL | B_RELBUF; 2223 bp->b_flags &= ~B_ASYNC; 2224 brelse(bp); 2225 anyfreed = true; 2226 2227 BO_LOCK(bo); 2228 if (nbp != NULL && 2229 (((nbp->b_xflags & BX_VNCLEAN) == 0) || 2230 nbp->b_vp != vp || 2231 (nbp->b_flags & B_DELWRI) != 0)) 2232 return (EAGAIN); 2233 } 2234 2235 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) { 2236 if (bp->b_lblkno < startlbn || bp->b_lblkno >= endlbn) 2237 continue; 2238 if (BUF_LOCK(bp, 2239 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, 2240 BO_LOCKPTR(bo)) == ENOLCK) { 2241 BO_LOCK(bo); 2242 return (EAGAIN); 2243 } 2244 bremfree(bp); 2245 bp->b_flags |= B_INVAL | B_RELBUF; 2246 bp->b_flags &= ~B_ASYNC; 2247 brelse(bp); 2248 anyfreed = true; 2249 2250 BO_LOCK(bo); 2251 if (nbp != NULL && 2252 (((nbp->b_xflags & BX_VNDIRTY) == 0) || 2253 (nbp->b_vp != vp) || 2254 (nbp->b_flags & B_DELWRI) == 0)) 2255 return (EAGAIN); 2256 } 2257 } while (anyfreed); 2258 return (0); 2259 } 2260 2261 static void 2262 buf_vlist_remove(struct buf *bp) 2263 { 2264 struct bufv *bv; 2265 2266 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp)); 2267 ASSERT_BO_WLOCKED(bp->b_bufobj); 2268 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) != 2269 (BX_VNDIRTY|BX_VNCLEAN), 2270 ("buf_vlist_remove: Buf %p is on two lists", bp)); 2271 if (bp->b_xflags & BX_VNDIRTY) 2272 bv = &bp->b_bufobj->bo_dirty; 2273 else 2274 bv = &bp->b_bufobj->bo_clean; 2275 BUF_PCTRIE_REMOVE(&bv->bv_root, bp->b_lblkno); 2276 TAILQ_REMOVE(&bv->bv_hd, bp, b_bobufs); 2277 bv->bv_cnt--; 2278 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN); 2279 } 2280 2281 /* 2282 * Add the buffer to the sorted clean or dirty block list. 2283 * 2284 * NOTE: xflags is passed as a constant, optimizing this inline function! 2285 */ 2286 static void 2287 buf_vlist_add(struct buf *bp, struct bufobj *bo, b_xflags_t xflags) 2288 { 2289 struct bufv *bv; 2290 struct buf *n; 2291 int error; 2292 2293 ASSERT_BO_WLOCKED(bo); 2294 KASSERT((xflags & BX_VNDIRTY) == 0 || (bo->bo_flag & BO_DEAD) == 0, 2295 ("dead bo %p", bo)); 2296 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0, 2297 ("buf_vlist_add: Buf %p has existing xflags %d", bp, bp->b_xflags)); 2298 bp->b_xflags |= xflags; 2299 if (xflags & BX_VNDIRTY) 2300 bv = &bo->bo_dirty; 2301 else 2302 bv = &bo->bo_clean; 2303 2304 /* 2305 * Keep the list ordered. Optimize empty list insertion. Assume 2306 * we tend to grow at the tail so lookup_le should usually be cheaper 2307 * than _ge. 2308 */ 2309 if (bv->bv_cnt == 0 || 2310 bp->b_lblkno > TAILQ_LAST(&bv->bv_hd, buflists)->b_lblkno) 2311 TAILQ_INSERT_TAIL(&bv->bv_hd, bp, b_bobufs); 2312 else if ((n = BUF_PCTRIE_LOOKUP_LE(&bv->bv_root, bp->b_lblkno)) == NULL) 2313 TAILQ_INSERT_HEAD(&bv->bv_hd, bp, b_bobufs); 2314 else 2315 TAILQ_INSERT_AFTER(&bv->bv_hd, n, bp, b_bobufs); 2316 error = BUF_PCTRIE_INSERT(&bv->bv_root, bp); 2317 if (error) 2318 panic("buf_vlist_add: Preallocated nodes insufficient."); 2319 bv->bv_cnt++; 2320 } 2321 2322 /* 2323 * Look up a buffer using the buffer tries. 2324 */ 2325 struct buf * 2326 gbincore(struct bufobj *bo, daddr_t lblkno) 2327 { 2328 struct buf *bp; 2329 2330 ASSERT_BO_LOCKED(bo); 2331 bp = BUF_PCTRIE_LOOKUP(&bo->bo_clean.bv_root, lblkno); 2332 if (bp != NULL) 2333 return (bp); 2334 return BUF_PCTRIE_LOOKUP(&bo->bo_dirty.bv_root, lblkno); 2335 } 2336 2337 /* 2338 * Associate a buffer with a vnode. 2339 */ 2340 void 2341 bgetvp(struct vnode *vp, struct buf *bp) 2342 { 2343 struct bufobj *bo; 2344 2345 bo = &vp->v_bufobj; 2346 ASSERT_BO_WLOCKED(bo); 2347 VNASSERT(bp->b_vp == NULL, bp->b_vp, ("bgetvp: not free")); 2348 2349 CTR3(KTR_BUF, "bgetvp(%p) vp %p flags %X", bp, vp, bp->b_flags); 2350 VNASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0, vp, 2351 ("bgetvp: bp already attached! %p", bp)); 2352 2353 vhold(vp); 2354 bp->b_vp = vp; 2355 bp->b_bufobj = bo; 2356 /* 2357 * Insert onto list for new vnode. 2358 */ 2359 buf_vlist_add(bp, bo, BX_VNCLEAN); 2360 } 2361 2362 /* 2363 * Disassociate a buffer from a vnode. 2364 */ 2365 void 2366 brelvp(struct buf *bp) 2367 { 2368 struct bufobj *bo; 2369 struct vnode *vp; 2370 2371 CTR3(KTR_BUF, "brelvp(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags); 2372 KASSERT(bp->b_vp != NULL, ("brelvp: NULL")); 2373 2374 /* 2375 * Delete from old vnode list, if on one. 2376 */ 2377 vp = bp->b_vp; /* XXX */ 2378 bo = bp->b_bufobj; 2379 BO_LOCK(bo); 2380 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) 2381 buf_vlist_remove(bp); 2382 else 2383 panic("brelvp: Buffer %p not on queue.", bp); 2384 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) { 2385 bo->bo_flag &= ~BO_ONWORKLST; 2386 mtx_lock(&sync_mtx); 2387 LIST_REMOVE(bo, bo_synclist); 2388 syncer_worklist_len--; 2389 mtx_unlock(&sync_mtx); 2390 } 2391 bp->b_vp = NULL; 2392 bp->b_bufobj = NULL; 2393 BO_UNLOCK(bo); 2394 vdrop(vp); 2395 } 2396 2397 /* 2398 * Add an item to the syncer work queue. 2399 */ 2400 static void 2401 vn_syncer_add_to_worklist(struct bufobj *bo, int delay) 2402 { 2403 int slot; 2404 2405 ASSERT_BO_WLOCKED(bo); 2406 2407 mtx_lock(&sync_mtx); 2408 if (bo->bo_flag & BO_ONWORKLST) 2409 LIST_REMOVE(bo, bo_synclist); 2410 else { 2411 bo->bo_flag |= BO_ONWORKLST; 2412 syncer_worklist_len++; 2413 } 2414 2415 if (delay > syncer_maxdelay - 2) 2416 delay = syncer_maxdelay - 2; 2417 slot = (syncer_delayno + delay) & syncer_mask; 2418 2419 LIST_INSERT_HEAD(&syncer_workitem_pending[slot], bo, bo_synclist); 2420 mtx_unlock(&sync_mtx); 2421 } 2422 2423 static int 2424 sysctl_vfs_worklist_len(SYSCTL_HANDLER_ARGS) 2425 { 2426 int error, len; 2427 2428 mtx_lock(&sync_mtx); 2429 len = syncer_worklist_len - sync_vnode_count; 2430 mtx_unlock(&sync_mtx); 2431 error = SYSCTL_OUT(req, &len, sizeof(len)); 2432 return (error); 2433 } 2434 2435 SYSCTL_PROC(_vfs, OID_AUTO, worklist_len, 2436 CTLTYPE_INT | CTLFLAG_MPSAFE| CTLFLAG_RD, NULL, 0, 2437 sysctl_vfs_worklist_len, "I", "Syncer thread worklist length"); 2438 2439 static struct proc *updateproc; 2440 static void sched_sync(void); 2441 static struct kproc_desc up_kp = { 2442 "syncer", 2443 sched_sync, 2444 &updateproc 2445 }; 2446 SYSINIT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp); 2447 2448 static int 2449 sync_vnode(struct synclist *slp, struct bufobj **bo, struct thread *td) 2450 { 2451 struct vnode *vp; 2452 struct mount *mp; 2453 2454 *bo = LIST_FIRST(slp); 2455 if (*bo == NULL) 2456 return (0); 2457 vp = bo2vnode(*bo); 2458 if (VOP_ISLOCKED(vp) != 0 || VI_TRYLOCK(vp) == 0) 2459 return (1); 2460 /* 2461 * We use vhold in case the vnode does not 2462 * successfully sync. vhold prevents the vnode from 2463 * going away when we unlock the sync_mtx so that 2464 * we can acquire the vnode interlock. 2465 */ 2466 vholdl(vp); 2467 mtx_unlock(&sync_mtx); 2468 VI_UNLOCK(vp); 2469 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) { 2470 vdrop(vp); 2471 mtx_lock(&sync_mtx); 2472 return (*bo == LIST_FIRST(slp)); 2473 } 2474 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 2475 (void) VOP_FSYNC(vp, MNT_LAZY, td); 2476 VOP_UNLOCK(vp); 2477 vn_finished_write(mp); 2478 BO_LOCK(*bo); 2479 if (((*bo)->bo_flag & BO_ONWORKLST) != 0) { 2480 /* 2481 * Put us back on the worklist. The worklist 2482 * routine will remove us from our current 2483 * position and then add us back in at a later 2484 * position. 2485 */ 2486 vn_syncer_add_to_worklist(*bo, syncdelay); 2487 } 2488 BO_UNLOCK(*bo); 2489 vdrop(vp); 2490 mtx_lock(&sync_mtx); 2491 return (0); 2492 } 2493 2494 static int first_printf = 1; 2495 2496 /* 2497 * System filesystem synchronizer daemon. 2498 */ 2499 static void 2500 sched_sync(void) 2501 { 2502 struct synclist *next, *slp; 2503 struct bufobj *bo; 2504 long starttime; 2505 struct thread *td = curthread; 2506 int last_work_seen; 2507 int net_worklist_len; 2508 int syncer_final_iter; 2509 int error; 2510 2511 last_work_seen = 0; 2512 syncer_final_iter = 0; 2513 syncer_state = SYNCER_RUNNING; 2514 starttime = time_uptime; 2515 td->td_pflags |= TDP_NORUNNINGBUF; 2516 2517 EVENTHANDLER_REGISTER(shutdown_pre_sync, syncer_shutdown, td->td_proc, 2518 SHUTDOWN_PRI_LAST); 2519 2520 mtx_lock(&sync_mtx); 2521 for (;;) { 2522 if (syncer_state == SYNCER_FINAL_DELAY && 2523 syncer_final_iter == 0) { 2524 mtx_unlock(&sync_mtx); 2525 kproc_suspend_check(td->td_proc); 2526 mtx_lock(&sync_mtx); 2527 } 2528 net_worklist_len = syncer_worklist_len - sync_vnode_count; 2529 if (syncer_state != SYNCER_RUNNING && 2530 starttime != time_uptime) { 2531 if (first_printf) { 2532 printf("\nSyncing disks, vnodes remaining... "); 2533 first_printf = 0; 2534 } 2535 printf("%d ", net_worklist_len); 2536 } 2537 starttime = time_uptime; 2538 2539 /* 2540 * Push files whose dirty time has expired. Be careful 2541 * of interrupt race on slp queue. 2542 * 2543 * Skip over empty worklist slots when shutting down. 2544 */ 2545 do { 2546 slp = &syncer_workitem_pending[syncer_delayno]; 2547 syncer_delayno += 1; 2548 if (syncer_delayno == syncer_maxdelay) 2549 syncer_delayno = 0; 2550 next = &syncer_workitem_pending[syncer_delayno]; 2551 /* 2552 * If the worklist has wrapped since the 2553 * it was emptied of all but syncer vnodes, 2554 * switch to the FINAL_DELAY state and run 2555 * for one more second. 2556 */ 2557 if (syncer_state == SYNCER_SHUTTING_DOWN && 2558 net_worklist_len == 0 && 2559 last_work_seen == syncer_delayno) { 2560 syncer_state = SYNCER_FINAL_DELAY; 2561 syncer_final_iter = SYNCER_SHUTDOWN_SPEEDUP; 2562 } 2563 } while (syncer_state != SYNCER_RUNNING && LIST_EMPTY(slp) && 2564 syncer_worklist_len > 0); 2565 2566 /* 2567 * Keep track of the last time there was anything 2568 * on the worklist other than syncer vnodes. 2569 * Return to the SHUTTING_DOWN state if any 2570 * new work appears. 2571 */ 2572 if (net_worklist_len > 0 || syncer_state == SYNCER_RUNNING) 2573 last_work_seen = syncer_delayno; 2574 if (net_worklist_len > 0 && syncer_state == SYNCER_FINAL_DELAY) 2575 syncer_state = SYNCER_SHUTTING_DOWN; 2576 while (!LIST_EMPTY(slp)) { 2577 error = sync_vnode(slp, &bo, td); 2578 if (error == 1) { 2579 LIST_REMOVE(bo, bo_synclist); 2580 LIST_INSERT_HEAD(next, bo, bo_synclist); 2581 continue; 2582 } 2583 2584 if (first_printf == 0) { 2585 /* 2586 * Drop the sync mutex, because some watchdog 2587 * drivers need to sleep while patting 2588 */ 2589 mtx_unlock(&sync_mtx); 2590 wdog_kern_pat(WD_LASTVAL); 2591 mtx_lock(&sync_mtx); 2592 } 2593 2594 } 2595 if (syncer_state == SYNCER_FINAL_DELAY && syncer_final_iter > 0) 2596 syncer_final_iter--; 2597 /* 2598 * The variable rushjob allows the kernel to speed up the 2599 * processing of the filesystem syncer process. A rushjob 2600 * value of N tells the filesystem syncer to process the next 2601 * N seconds worth of work on its queue ASAP. Currently rushjob 2602 * is used by the soft update code to speed up the filesystem 2603 * syncer process when the incore state is getting so far 2604 * ahead of the disk that the kernel memory pool is being 2605 * threatened with exhaustion. 2606 */ 2607 if (rushjob > 0) { 2608 rushjob -= 1; 2609 continue; 2610 } 2611 /* 2612 * Just sleep for a short period of time between 2613 * iterations when shutting down to allow some I/O 2614 * to happen. 2615 * 2616 * If it has taken us less than a second to process the 2617 * current work, then wait. Otherwise start right over 2618 * again. We can still lose time if any single round 2619 * takes more than two seconds, but it does not really 2620 * matter as we are just trying to generally pace the 2621 * filesystem activity. 2622 */ 2623 if (syncer_state != SYNCER_RUNNING || 2624 time_uptime == starttime) { 2625 thread_lock(td); 2626 sched_prio(td, PPAUSE); 2627 thread_unlock(td); 2628 } 2629 if (syncer_state != SYNCER_RUNNING) 2630 cv_timedwait(&sync_wakeup, &sync_mtx, 2631 hz / SYNCER_SHUTDOWN_SPEEDUP); 2632 else if (time_uptime == starttime) 2633 cv_timedwait(&sync_wakeup, &sync_mtx, hz); 2634 } 2635 } 2636 2637 /* 2638 * Request the syncer daemon to speed up its work. 2639 * We never push it to speed up more than half of its 2640 * normal turn time, otherwise it could take over the cpu. 2641 */ 2642 int 2643 speedup_syncer(void) 2644 { 2645 int ret = 0; 2646 2647 mtx_lock(&sync_mtx); 2648 if (rushjob < syncdelay / 2) { 2649 rushjob += 1; 2650 stat_rush_requests += 1; 2651 ret = 1; 2652 } 2653 mtx_unlock(&sync_mtx); 2654 cv_broadcast(&sync_wakeup); 2655 return (ret); 2656 } 2657 2658 /* 2659 * Tell the syncer to speed up its work and run though its work 2660 * list several times, then tell it to shut down. 2661 */ 2662 static void 2663 syncer_shutdown(void *arg, int howto) 2664 { 2665 2666 if (howto & RB_NOSYNC) 2667 return; 2668 mtx_lock(&sync_mtx); 2669 syncer_state = SYNCER_SHUTTING_DOWN; 2670 rushjob = 0; 2671 mtx_unlock(&sync_mtx); 2672 cv_broadcast(&sync_wakeup); 2673 kproc_shutdown(arg, howto); 2674 } 2675 2676 void 2677 syncer_suspend(void) 2678 { 2679 2680 syncer_shutdown(updateproc, 0); 2681 } 2682 2683 void 2684 syncer_resume(void) 2685 { 2686 2687 mtx_lock(&sync_mtx); 2688 first_printf = 1; 2689 syncer_state = SYNCER_RUNNING; 2690 mtx_unlock(&sync_mtx); 2691 cv_broadcast(&sync_wakeup); 2692 kproc_resume(updateproc); 2693 } 2694 2695 /* 2696 * Reassign a buffer from one vnode to another. 2697 * Used to assign file specific control information 2698 * (indirect blocks) to the vnode to which they belong. 2699 */ 2700 void 2701 reassignbuf(struct buf *bp) 2702 { 2703 struct vnode *vp; 2704 struct bufobj *bo; 2705 int delay; 2706 #ifdef INVARIANTS 2707 struct bufv *bv; 2708 #endif 2709 2710 vp = bp->b_vp; 2711 bo = bp->b_bufobj; 2712 ++reassignbufcalls; 2713 2714 CTR3(KTR_BUF, "reassignbuf(%p) vp %p flags %X", 2715 bp, bp->b_vp, bp->b_flags); 2716 /* 2717 * B_PAGING flagged buffers cannot be reassigned because their vp 2718 * is not fully linked in. 2719 */ 2720 if (bp->b_flags & B_PAGING) 2721 panic("cannot reassign paging buffer"); 2722 2723 /* 2724 * Delete from old vnode list, if on one. 2725 */ 2726 BO_LOCK(bo); 2727 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) 2728 buf_vlist_remove(bp); 2729 else 2730 panic("reassignbuf: Buffer %p not on queue.", bp); 2731 /* 2732 * If dirty, put on list of dirty buffers; otherwise insert onto list 2733 * of clean buffers. 2734 */ 2735 if (bp->b_flags & B_DELWRI) { 2736 if ((bo->bo_flag & BO_ONWORKLST) == 0) { 2737 switch (vp->v_type) { 2738 case VDIR: 2739 delay = dirdelay; 2740 break; 2741 case VCHR: 2742 delay = metadelay; 2743 break; 2744 default: 2745 delay = filedelay; 2746 } 2747 vn_syncer_add_to_worklist(bo, delay); 2748 } 2749 buf_vlist_add(bp, bo, BX_VNDIRTY); 2750 } else { 2751 buf_vlist_add(bp, bo, BX_VNCLEAN); 2752 2753 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) { 2754 mtx_lock(&sync_mtx); 2755 LIST_REMOVE(bo, bo_synclist); 2756 syncer_worklist_len--; 2757 mtx_unlock(&sync_mtx); 2758 bo->bo_flag &= ~BO_ONWORKLST; 2759 } 2760 } 2761 #ifdef INVARIANTS 2762 bv = &bo->bo_clean; 2763 bp = TAILQ_FIRST(&bv->bv_hd); 2764 KASSERT(bp == NULL || bp->b_bufobj == bo, 2765 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo)); 2766 bp = TAILQ_LAST(&bv->bv_hd, buflists); 2767 KASSERT(bp == NULL || bp->b_bufobj == bo, 2768 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo)); 2769 bv = &bo->bo_dirty; 2770 bp = TAILQ_FIRST(&bv->bv_hd); 2771 KASSERT(bp == NULL || bp->b_bufobj == bo, 2772 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo)); 2773 bp = TAILQ_LAST(&bv->bv_hd, buflists); 2774 KASSERT(bp == NULL || bp->b_bufobj == bo, 2775 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo)); 2776 #endif 2777 BO_UNLOCK(bo); 2778 } 2779 2780 static void 2781 v_init_counters(struct vnode *vp) 2782 { 2783 2784 VNASSERT(vp->v_type == VNON && vp->v_data == NULL && vp->v_iflag == 0, 2785 vp, ("%s called for an initialized vnode", __FUNCTION__)); 2786 ASSERT_VI_UNLOCKED(vp, __FUNCTION__); 2787 2788 refcount_init(&vp->v_holdcnt, 1); 2789 refcount_init(&vp->v_usecount, 1); 2790 } 2791 2792 /* 2793 * Increment si_usecount of the associated device, if any. 2794 */ 2795 static void 2796 v_incr_devcount(struct vnode *vp) 2797 { 2798 2799 ASSERT_VI_LOCKED(vp, __FUNCTION__); 2800 if (vp->v_type == VCHR && vp->v_rdev != NULL) { 2801 dev_lock(); 2802 vp->v_rdev->si_usecount++; 2803 dev_unlock(); 2804 } 2805 } 2806 2807 /* 2808 * Decrement si_usecount of the associated device, if any. 2809 */ 2810 static void 2811 v_decr_devcount(struct vnode *vp) 2812 { 2813 2814 ASSERT_VI_LOCKED(vp, __FUNCTION__); 2815 if (vp->v_type == VCHR && vp->v_rdev != NULL) { 2816 dev_lock(); 2817 vp->v_rdev->si_usecount--; 2818 dev_unlock(); 2819 } 2820 } 2821 2822 /* 2823 * Grab a particular vnode from the free list, increment its 2824 * reference count and lock it. VIRF_DOOMED is set if the vnode 2825 * is being destroyed. Only callers who specify LK_RETRY will 2826 * see doomed vnodes. If inactive processing was delayed in 2827 * vput try to do it here. 2828 * 2829 * usecount is manipulated using atomics without holding any locks. 2830 * 2831 * holdcnt can be manipulated using atomics without holding any locks, 2832 * except when transitioning 1<->0, in which case the interlock is held. 2833 */ 2834 enum vgetstate 2835 vget_prep(struct vnode *vp) 2836 { 2837 enum vgetstate vs; 2838 2839 if (refcount_acquire_if_not_zero(&vp->v_usecount)) { 2840 vs = VGET_USECOUNT; 2841 } else { 2842 vhold(vp); 2843 vs = VGET_HOLDCNT; 2844 } 2845 return (vs); 2846 } 2847 2848 int 2849 vget(struct vnode *vp, int flags, struct thread *td) 2850 { 2851 enum vgetstate vs; 2852 2853 MPASS(td == curthread); 2854 2855 vs = vget_prep(vp); 2856 return (vget_finish(vp, flags, vs)); 2857 } 2858 2859 static int __noinline 2860 vget_finish_vchr(struct vnode *vp) 2861 { 2862 2863 VNASSERT(vp->v_type == VCHR, vp, ("type != VCHR)")); 2864 2865 /* 2866 * See the comment in vget_finish before usecount bump. 2867 */ 2868 if (refcount_acquire_if_not_zero(&vp->v_usecount)) { 2869 #ifdef INVARIANTS 2870 int old = atomic_fetchadd_int(&vp->v_holdcnt, -1); 2871 VNASSERT(old > 0, vp, ("%s: wrong hold count %d", __func__, old)); 2872 #else 2873 refcount_release(&vp->v_holdcnt); 2874 #endif 2875 return (0); 2876 } 2877 2878 VI_LOCK(vp); 2879 if (refcount_acquire_if_not_zero(&vp->v_usecount)) { 2880 #ifdef INVARIANTS 2881 int old = atomic_fetchadd_int(&vp->v_holdcnt, -1); 2882 VNASSERT(old > 1, vp, ("%s: wrong hold count %d", __func__, old)); 2883 #else 2884 refcount_release(&vp->v_holdcnt); 2885 #endif 2886 VI_UNLOCK(vp); 2887 return (0); 2888 } 2889 v_incr_devcount(vp); 2890 refcount_acquire(&vp->v_usecount); 2891 VI_UNLOCK(vp); 2892 return (0); 2893 } 2894 2895 int 2896 vget_finish(struct vnode *vp, int flags, enum vgetstate vs) 2897 { 2898 int error, old; 2899 2900 VNASSERT((flags & LK_TYPE_MASK) != 0, vp, 2901 ("%s: invalid lock operation", __func__)); 2902 2903 if ((flags & LK_INTERLOCK) != 0) 2904 ASSERT_VI_LOCKED(vp, __func__); 2905 else 2906 ASSERT_VI_UNLOCKED(vp, __func__); 2907 VNASSERT(vp->v_holdcnt > 0, vp, ("%s: vnode not held", __func__)); 2908 if (vs == VGET_USECOUNT) { 2909 VNASSERT(vp->v_usecount > 0, vp, 2910 ("%s: vnode without usecount when VGET_USECOUNT was passed", 2911 __func__)); 2912 } 2913 2914 if ((error = vn_lock(vp, flags)) != 0) { 2915 if (vs == VGET_USECOUNT) 2916 vrele(vp); 2917 else 2918 vdrop(vp); 2919 CTR2(KTR_VFS, "%s: impossible to lock vnode %p", __func__, 2920 vp); 2921 return (error); 2922 } 2923 2924 if (vs == VGET_USECOUNT) { 2925 return (0); 2926 } 2927 2928 if (__predict_false(vp->v_type == VCHR)) 2929 return (vget_finish_vchr(vp)); 2930 2931 /* 2932 * We hold the vnode. If the usecount is 0 it will be utilized to keep 2933 * the vnode around. Otherwise someone else lended their hold count and 2934 * we have to drop ours. 2935 */ 2936 old = atomic_fetchadd_int(&vp->v_usecount, 1); 2937 VNASSERT(old >= 0, vp, ("%s: wrong use count %d", __func__, old)); 2938 if (old != 0) { 2939 #ifdef INVARIANTS 2940 old = atomic_fetchadd_int(&vp->v_holdcnt, -1); 2941 VNASSERT(old > 1, vp, ("%s: wrong hold count %d", __func__, old)); 2942 #else 2943 refcount_release(&vp->v_holdcnt); 2944 #endif 2945 } 2946 return (0); 2947 } 2948 2949 /* 2950 * Increase the reference (use) and hold count of a vnode. 2951 * This will also remove the vnode from the free list if it is presently free. 2952 */ 2953 static void __noinline 2954 vref_vchr(struct vnode *vp, bool interlock) 2955 { 2956 2957 /* 2958 * See the comment in vget_finish before usecount bump. 2959 */ 2960 if (!interlock) { 2961 if (refcount_acquire_if_not_zero(&vp->v_usecount)) { 2962 VNODE_REFCOUNT_FENCE_ACQ(); 2963 VNASSERT(vp->v_holdcnt > 0, vp, 2964 ("%s: active vnode not held", __func__)); 2965 return; 2966 } 2967 VI_LOCK(vp); 2968 /* 2969 * By the time we get here the vnode might have been doomed, at 2970 * which point the 0->1 use count transition is no longer 2971 * protected by the interlock. Since it can't bounce back to 2972 * VCHR and requires vref semantics, punt it back 2973 */ 2974 if (__predict_false(vp->v_type == VBAD)) { 2975 VI_UNLOCK(vp); 2976 vref(vp); 2977 return; 2978 } 2979 } 2980 VNASSERT(vp->v_type == VCHR, vp, ("type != VCHR)")); 2981 if (refcount_acquire_if_not_zero(&vp->v_usecount)) { 2982 VNODE_REFCOUNT_FENCE_ACQ(); 2983 VNASSERT(vp->v_holdcnt > 0, vp, 2984 ("%s: active vnode not held", __func__)); 2985 if (!interlock) 2986 VI_UNLOCK(vp); 2987 return; 2988 } 2989 vhold(vp); 2990 v_incr_devcount(vp); 2991 refcount_acquire(&vp->v_usecount); 2992 if (!interlock) 2993 VI_UNLOCK(vp); 2994 return; 2995 } 2996 2997 void 2998 vref(struct vnode *vp) 2999 { 3000 int old; 3001 3002 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 3003 if (__predict_false(vp->v_type == VCHR)) { 3004 vref_vchr(vp, false); 3005 return; 3006 } 3007 3008 if (refcount_acquire_if_not_zero(&vp->v_usecount)) { 3009 VNODE_REFCOUNT_FENCE_ACQ(); 3010 VNASSERT(vp->v_holdcnt > 0, vp, 3011 ("%s: active vnode not held", __func__)); 3012 return; 3013 } 3014 vhold(vp); 3015 /* 3016 * See the comment in vget_finish. 3017 */ 3018 old = atomic_fetchadd_int(&vp->v_usecount, 1); 3019 VNASSERT(old >= 0, vp, ("%s: wrong use count %d", __func__, old)); 3020 if (old != 0) { 3021 #ifdef INVARIANTS 3022 old = atomic_fetchadd_int(&vp->v_holdcnt, -1); 3023 VNASSERT(old > 1, vp, ("%s: wrong hold count %d", __func__, old)); 3024 #else 3025 refcount_release(&vp->v_holdcnt); 3026 #endif 3027 } 3028 } 3029 3030 void 3031 vrefl(struct vnode *vp) 3032 { 3033 3034 ASSERT_VI_LOCKED(vp, __func__); 3035 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 3036 if (__predict_false(vp->v_type == VCHR)) { 3037 vref_vchr(vp, true); 3038 return; 3039 } 3040 vref(vp); 3041 } 3042 3043 void 3044 vrefact(struct vnode *vp) 3045 { 3046 3047 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 3048 #ifdef INVARIANTS 3049 int old = atomic_fetchadd_int(&vp->v_usecount, 1); 3050 VNASSERT(old > 0, vp, ("%s: wrong use count %d", __func__, old)); 3051 #else 3052 refcount_acquire(&vp->v_usecount); 3053 #endif 3054 } 3055 3056 /* 3057 * Return reference count of a vnode. 3058 * 3059 * The results of this call are only guaranteed when some mechanism is used to 3060 * stop other processes from gaining references to the vnode. This may be the 3061 * case if the caller holds the only reference. This is also useful when stale 3062 * data is acceptable as race conditions may be accounted for by some other 3063 * means. 3064 */ 3065 int 3066 vrefcnt(struct vnode *vp) 3067 { 3068 3069 return (vp->v_usecount); 3070 } 3071 3072 void 3073 vlazy(struct vnode *vp) 3074 { 3075 struct mount *mp; 3076 3077 VNASSERT(vp->v_holdcnt > 0, vp, ("%s: vnode not held", __func__)); 3078 3079 if ((vp->v_mflag & VMP_LAZYLIST) != 0) 3080 return; 3081 mp = vp->v_mount; 3082 mtx_lock(&mp->mnt_listmtx); 3083 if ((vp->v_mflag & VMP_LAZYLIST) == 0) { 3084 vp->v_mflag |= VMP_LAZYLIST; 3085 TAILQ_INSERT_TAIL(&mp->mnt_lazyvnodelist, vp, v_lazylist); 3086 mp->mnt_lazyvnodelistsize++; 3087 } 3088 mtx_unlock(&mp->mnt_listmtx); 3089 } 3090 3091 static void 3092 vdefer_inactive(struct vnode *vp) 3093 { 3094 3095 ASSERT_VI_LOCKED(vp, __func__); 3096 VNASSERT(vp->v_holdcnt > 0, vp, 3097 ("%s: vnode without hold count", __func__)); 3098 if (VN_IS_DOOMED(vp)) { 3099 vdropl(vp); 3100 return; 3101 } 3102 if (vp->v_iflag & VI_DEFINACT) { 3103 VNASSERT(vp->v_holdcnt > 1, vp, ("lost hold count")); 3104 vdropl(vp); 3105 return; 3106 } 3107 if (vp->v_usecount > 0) { 3108 vp->v_iflag &= ~VI_OWEINACT; 3109 vdropl(vp); 3110 return; 3111 } 3112 vlazy(vp); 3113 vp->v_iflag |= VI_DEFINACT; 3114 VI_UNLOCK(vp); 3115 counter_u64_add(deferred_inact, 1); 3116 } 3117 3118 static void 3119 vdefer_inactive_unlocked(struct vnode *vp) 3120 { 3121 3122 VI_LOCK(vp); 3123 if ((vp->v_iflag & VI_OWEINACT) == 0) { 3124 vdropl(vp); 3125 return; 3126 } 3127 vdefer_inactive(vp); 3128 } 3129 3130 enum vputx_op { VPUTX_VRELE, VPUTX_VPUT, VPUTX_VUNREF }; 3131 3132 /* 3133 * Decrement the use and hold counts for a vnode. 3134 * 3135 * See an explanation near vget() as to why atomic operation is safe. 3136 * 3137 * XXX Some filesystems pass in an exclusively locked vnode and strongly depend 3138 * on the lock being held all the way until VOP_INACTIVE. This in particular 3139 * happens with UFS which adds half-constructed vnodes to the hash, where they 3140 * can be found by other code. 3141 */ 3142 static void 3143 vputx(struct vnode *vp, enum vputx_op func) 3144 { 3145 int error; 3146 3147 KASSERT(vp != NULL, ("vputx: null vp")); 3148 if (func == VPUTX_VUNREF) 3149 ASSERT_VOP_LOCKED(vp, "vunref"); 3150 else if (func == VPUTX_VPUT) 3151 ASSERT_VOP_LOCKED(vp, "vput"); 3152 ASSERT_VI_UNLOCKED(vp, __func__); 3153 VNASSERT(vp->v_holdcnt > 0 && vp->v_usecount > 0, vp, 3154 ("%s: wrong ref counts", __func__)); 3155 3156 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 3157 3158 /* 3159 * We want to hold the vnode until the inactive finishes to 3160 * prevent vgone() races. We drop the use count here and the 3161 * hold count below when we're done. 3162 * 3163 * If we release the last usecount we take ownership of the hold 3164 * count which provides liveness of the vnode, in which case we 3165 * have to vdrop. 3166 */ 3167 if (!refcount_release(&vp->v_usecount)) { 3168 if (func == VPUTX_VPUT) 3169 VOP_UNLOCK(vp); 3170 return; 3171 } 3172 VI_LOCK(vp); 3173 v_decr_devcount(vp); 3174 /* 3175 * By the time we got here someone else might have transitioned 3176 * the count back to > 0. 3177 */ 3178 if (vp->v_usecount > 0 || vp->v_iflag & VI_DOINGINACT) 3179 goto out; 3180 3181 /* 3182 * Check if the fs wants to perform inactive processing. Note we 3183 * may be only holding the interlock, in which case it is possible 3184 * someone else called vgone on the vnode and ->v_data is now NULL. 3185 * Since vgone performs inactive on its own there is nothing to do 3186 * here but to drop our hold count. 3187 */ 3188 if (__predict_false(VN_IS_DOOMED(vp)) || 3189 VOP_NEED_INACTIVE(vp) == 0) 3190 goto out; 3191 3192 /* 3193 * We must call VOP_INACTIVE with the node locked. Mark 3194 * as VI_DOINGINACT to avoid recursion. 3195 */ 3196 vp->v_iflag |= VI_OWEINACT; 3197 switch (func) { 3198 case VPUTX_VRELE: 3199 error = vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK); 3200 VI_LOCK(vp); 3201 break; 3202 case VPUTX_VPUT: 3203 error = 0; 3204 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) { 3205 error = VOP_LOCK(vp, LK_UPGRADE | LK_INTERLOCK | 3206 LK_NOWAIT); 3207 VI_LOCK(vp); 3208 } 3209 break; 3210 case VPUTX_VUNREF: 3211 error = 0; 3212 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) { 3213 error = VOP_LOCK(vp, LK_TRYUPGRADE | LK_INTERLOCK); 3214 VI_LOCK(vp); 3215 } 3216 break; 3217 } 3218 if (error == 0) { 3219 vinactive(vp); 3220 if (func != VPUTX_VUNREF) 3221 VOP_UNLOCK(vp); 3222 vdropl(vp); 3223 } else { 3224 vdefer_inactive(vp); 3225 } 3226 return; 3227 out: 3228 if (func == VPUTX_VPUT) 3229 VOP_UNLOCK(vp); 3230 vdropl(vp); 3231 } 3232 3233 /* 3234 * Vnode put/release. 3235 * If count drops to zero, call inactive routine and return to freelist. 3236 */ 3237 void 3238 vrele(struct vnode *vp) 3239 { 3240 3241 vputx(vp, VPUTX_VRELE); 3242 } 3243 3244 /* 3245 * Release an already locked vnode. This give the same effects as 3246 * unlock+vrele(), but takes less time and avoids releasing and 3247 * re-aquiring the lock (as vrele() acquires the lock internally.) 3248 */ 3249 void 3250 vput(struct vnode *vp) 3251 { 3252 3253 vputx(vp, VPUTX_VPUT); 3254 } 3255 3256 /* 3257 * Release an exclusively locked vnode. Do not unlock the vnode lock. 3258 */ 3259 void 3260 vunref(struct vnode *vp) 3261 { 3262 3263 vputx(vp, VPUTX_VUNREF); 3264 } 3265 3266 void 3267 vhold(struct vnode *vp) 3268 { 3269 struct vdbatch *vd; 3270 int old; 3271 3272 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 3273 old = atomic_fetchadd_int(&vp->v_holdcnt, 1); 3274 VNASSERT(old >= 0, vp, ("%s: wrong hold count %d", __func__, old)); 3275 if (old != 0) 3276 return; 3277 critical_enter(); 3278 vd = DPCPU_PTR(vd); 3279 vd->freevnodes--; 3280 critical_exit(); 3281 } 3282 3283 void 3284 vholdl(struct vnode *vp) 3285 { 3286 3287 ASSERT_VI_LOCKED(vp, __func__); 3288 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 3289 vhold(vp); 3290 } 3291 3292 void 3293 vholdnz(struct vnode *vp) 3294 { 3295 3296 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 3297 #ifdef INVARIANTS 3298 int old = atomic_fetchadd_int(&vp->v_holdcnt, 1); 3299 VNASSERT(old > 0, vp, ("%s: wrong hold count %d", __func__, old)); 3300 #else 3301 atomic_add_int(&vp->v_holdcnt, 1); 3302 #endif 3303 } 3304 3305 static void __noinline 3306 vdbatch_process(struct vdbatch *vd) 3307 { 3308 struct vnode *vp; 3309 int i; 3310 3311 mtx_assert(&vd->lock, MA_OWNED); 3312 MPASS(curthread->td_pinned > 0); 3313 MPASS(vd->index == VDBATCH_SIZE); 3314 3315 mtx_lock(&vnode_list_mtx); 3316 critical_enter(); 3317 freevnodes += vd->freevnodes; 3318 for (i = 0; i < VDBATCH_SIZE; i++) { 3319 vp = vd->tab[i]; 3320 TAILQ_REMOVE(&vnode_list, vp, v_vnodelist); 3321 TAILQ_INSERT_TAIL(&vnode_list, vp, v_vnodelist); 3322 MPASS(vp->v_dbatchcpu != NOCPU); 3323 vp->v_dbatchcpu = NOCPU; 3324 } 3325 mtx_unlock(&vnode_list_mtx); 3326 critical_exit(); 3327 vd->freevnodes = 0; 3328 bzero(vd->tab, sizeof(vd->tab)); 3329 vd->index = 0; 3330 } 3331 3332 static void 3333 vdbatch_enqueue(struct vnode *vp) 3334 { 3335 struct vdbatch *vd; 3336 3337 ASSERT_VI_LOCKED(vp, __func__); 3338 VNASSERT(!VN_IS_DOOMED(vp), vp, 3339 ("%s: deferring requeue of a doomed vnode", __func__)); 3340 3341 critical_enter(); 3342 vd = DPCPU_PTR(vd); 3343 vd->freevnodes++; 3344 if (vp->v_dbatchcpu != NOCPU) { 3345 VI_UNLOCK(vp); 3346 critical_exit(); 3347 return; 3348 } 3349 3350 sched_pin(); 3351 critical_exit(); 3352 mtx_lock(&vd->lock); 3353 MPASS(vd->index < VDBATCH_SIZE); 3354 MPASS(vd->tab[vd->index] == NULL); 3355 /* 3356 * A hack: we depend on being pinned so that we know what to put in 3357 * ->v_dbatchcpu. 3358 */ 3359 vp->v_dbatchcpu = curcpu; 3360 vd->tab[vd->index] = vp; 3361 vd->index++; 3362 VI_UNLOCK(vp); 3363 if (vd->index == VDBATCH_SIZE) 3364 vdbatch_process(vd); 3365 mtx_unlock(&vd->lock); 3366 sched_unpin(); 3367 } 3368 3369 /* 3370 * This routine must only be called for vnodes which are about to be 3371 * deallocated. Supporting dequeue for arbitrary vndoes would require 3372 * validating that the locked batch matches. 3373 */ 3374 static void 3375 vdbatch_dequeue(struct vnode *vp) 3376 { 3377 struct vdbatch *vd; 3378 int i; 3379 short cpu; 3380 3381 VNASSERT(vp->v_type == VBAD || vp->v_type == VNON, vp, 3382 ("%s: called for a used vnode\n", __func__)); 3383 3384 cpu = vp->v_dbatchcpu; 3385 if (cpu == NOCPU) 3386 return; 3387 3388 vd = DPCPU_ID_PTR(cpu, vd); 3389 mtx_lock(&vd->lock); 3390 for (i = 0; i < vd->index; i++) { 3391 if (vd->tab[i] != vp) 3392 continue; 3393 vp->v_dbatchcpu = NOCPU; 3394 vd->index--; 3395 vd->tab[i] = vd->tab[vd->index]; 3396 vd->tab[vd->index] = NULL; 3397 break; 3398 } 3399 mtx_unlock(&vd->lock); 3400 /* 3401 * Either we dequeued the vnode above or the target CPU beat us to it. 3402 */ 3403 MPASS(vp->v_dbatchcpu == NOCPU); 3404 } 3405 3406 /* 3407 * Drop the hold count of the vnode. If this is the last reference to 3408 * the vnode we place it on the free list unless it has been vgone'd 3409 * (marked VIRF_DOOMED) in which case we will free it. 3410 * 3411 * Because the vnode vm object keeps a hold reference on the vnode if 3412 * there is at least one resident non-cached page, the vnode cannot 3413 * leave the active list without the page cleanup done. 3414 */ 3415 static void 3416 vdrop_deactivate(struct vnode *vp) 3417 { 3418 struct mount *mp; 3419 3420 ASSERT_VI_LOCKED(vp, __func__); 3421 /* 3422 * Mark a vnode as free: remove it from its active list 3423 * and put it up for recycling on the freelist. 3424 */ 3425 VNASSERT(!VN_IS_DOOMED(vp), vp, 3426 ("vdrop: returning doomed vnode")); 3427 VNASSERT(vp->v_op != NULL, vp, 3428 ("vdrop: vnode already reclaimed.")); 3429 VNASSERT((vp->v_iflag & VI_OWEINACT) == 0, vp, 3430 ("vnode with VI_OWEINACT set")); 3431 VNASSERT((vp->v_iflag & VI_DEFINACT) == 0, vp, 3432 ("vnode with VI_DEFINACT set")); 3433 if (vp->v_mflag & VMP_LAZYLIST) { 3434 mp = vp->v_mount; 3435 mtx_lock(&mp->mnt_listmtx); 3436 VNASSERT(vp->v_mflag & VMP_LAZYLIST, vp, ("lost VMP_LAZYLIST")); 3437 /* 3438 * Don't remove the vnode from the lazy list if another thread 3439 * has increased the hold count. It may have re-enqueued the 3440 * vnode to the lazy list and is now responsible for its 3441 * removal. 3442 */ 3443 if (vp->v_holdcnt == 0) { 3444 vp->v_mflag &= ~VMP_LAZYLIST; 3445 TAILQ_REMOVE(&mp->mnt_lazyvnodelist, vp, v_lazylist); 3446 mp->mnt_lazyvnodelistsize--; 3447 } 3448 mtx_unlock(&mp->mnt_listmtx); 3449 } 3450 vdbatch_enqueue(vp); 3451 } 3452 3453 void 3454 vdrop(struct vnode *vp) 3455 { 3456 3457 ASSERT_VI_UNLOCKED(vp, __func__); 3458 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 3459 if (refcount_release_if_not_last(&vp->v_holdcnt)) 3460 return; 3461 VI_LOCK(vp); 3462 vdropl(vp); 3463 } 3464 3465 void 3466 vdropl(struct vnode *vp) 3467 { 3468 3469 ASSERT_VI_LOCKED(vp, __func__); 3470 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 3471 if (!refcount_release(&vp->v_holdcnt)) { 3472 VI_UNLOCK(vp); 3473 return; 3474 } 3475 if (VN_IS_DOOMED(vp)) { 3476 freevnode(vp); 3477 return; 3478 } 3479 vdrop_deactivate(vp); 3480 } 3481 3482 /* 3483 * Call VOP_INACTIVE on the vnode and manage the DOINGINACT and OWEINACT 3484 * flags. DOINGINACT prevents us from recursing in calls to vinactive. 3485 */ 3486 static void 3487 vinactivef(struct vnode *vp) 3488 { 3489 struct vm_object *obj; 3490 3491 ASSERT_VOP_ELOCKED(vp, "vinactive"); 3492 ASSERT_VI_LOCKED(vp, "vinactive"); 3493 VNASSERT((vp->v_iflag & VI_DOINGINACT) == 0, vp, 3494 ("vinactive: recursed on VI_DOINGINACT")); 3495 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 3496 vp->v_iflag |= VI_DOINGINACT; 3497 vp->v_iflag &= ~VI_OWEINACT; 3498 VI_UNLOCK(vp); 3499 /* 3500 * Before moving off the active list, we must be sure that any 3501 * modified pages are converted into the vnode's dirty 3502 * buffers, since these will no longer be checked once the 3503 * vnode is on the inactive list. 3504 * 3505 * The write-out of the dirty pages is asynchronous. At the 3506 * point that VOP_INACTIVE() is called, there could still be 3507 * pending I/O and dirty pages in the object. 3508 */ 3509 if ((obj = vp->v_object) != NULL && (vp->v_vflag & VV_NOSYNC) == 0 && 3510 vm_object_mightbedirty(obj)) { 3511 VM_OBJECT_WLOCK(obj); 3512 vm_object_page_clean(obj, 0, 0, 0); 3513 VM_OBJECT_WUNLOCK(obj); 3514 } 3515 VOP_INACTIVE(vp, curthread); 3516 VI_LOCK(vp); 3517 VNASSERT(vp->v_iflag & VI_DOINGINACT, vp, 3518 ("vinactive: lost VI_DOINGINACT")); 3519 vp->v_iflag &= ~VI_DOINGINACT; 3520 } 3521 3522 void 3523 vinactive(struct vnode *vp) 3524 { 3525 3526 ASSERT_VOP_ELOCKED(vp, "vinactive"); 3527 ASSERT_VI_LOCKED(vp, "vinactive"); 3528 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 3529 3530 if ((vp->v_iflag & VI_OWEINACT) == 0) 3531 return; 3532 if (vp->v_iflag & VI_DOINGINACT) 3533 return; 3534 if (vp->v_usecount > 0) { 3535 vp->v_iflag &= ~VI_OWEINACT; 3536 return; 3537 } 3538 vinactivef(vp); 3539 } 3540 3541 /* 3542 * Remove any vnodes in the vnode table belonging to mount point mp. 3543 * 3544 * If FORCECLOSE is not specified, there should not be any active ones, 3545 * return error if any are found (nb: this is a user error, not a 3546 * system error). If FORCECLOSE is specified, detach any active vnodes 3547 * that are found. 3548 * 3549 * If WRITECLOSE is set, only flush out regular file vnodes open for 3550 * writing. 3551 * 3552 * SKIPSYSTEM causes any vnodes marked VV_SYSTEM to be skipped. 3553 * 3554 * `rootrefs' specifies the base reference count for the root vnode 3555 * of this filesystem. The root vnode is considered busy if its 3556 * v_usecount exceeds this value. On a successful return, vflush(, td) 3557 * will call vrele() on the root vnode exactly rootrefs times. 3558 * If the SKIPSYSTEM or WRITECLOSE flags are specified, rootrefs must 3559 * be zero. 3560 */ 3561 #ifdef DIAGNOSTIC 3562 static int busyprt = 0; /* print out busy vnodes */ 3563 SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, "Print out busy vnodes"); 3564 #endif 3565 3566 int 3567 vflush(struct mount *mp, int rootrefs, int flags, struct thread *td) 3568 { 3569 struct vnode *vp, *mvp, *rootvp = NULL; 3570 struct vattr vattr; 3571 int busy = 0, error; 3572 3573 CTR4(KTR_VFS, "%s: mp %p with rootrefs %d and flags %d", __func__, mp, 3574 rootrefs, flags); 3575 if (rootrefs > 0) { 3576 KASSERT((flags & (SKIPSYSTEM | WRITECLOSE)) == 0, 3577 ("vflush: bad args")); 3578 /* 3579 * Get the filesystem root vnode. We can vput() it 3580 * immediately, since with rootrefs > 0, it won't go away. 3581 */ 3582 if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rootvp)) != 0) { 3583 CTR2(KTR_VFS, "%s: vfs_root lookup failed with %d", 3584 __func__, error); 3585 return (error); 3586 } 3587 vput(rootvp); 3588 } 3589 loop: 3590 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) { 3591 vholdl(vp); 3592 error = vn_lock(vp, LK_INTERLOCK | LK_EXCLUSIVE); 3593 if (error) { 3594 vdrop(vp); 3595 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp); 3596 goto loop; 3597 } 3598 /* 3599 * Skip over a vnodes marked VV_SYSTEM. 3600 */ 3601 if ((flags & SKIPSYSTEM) && (vp->v_vflag & VV_SYSTEM)) { 3602 VOP_UNLOCK(vp); 3603 vdrop(vp); 3604 continue; 3605 } 3606 /* 3607 * If WRITECLOSE is set, flush out unlinked but still open 3608 * files (even if open only for reading) and regular file 3609 * vnodes open for writing. 3610 */ 3611 if (flags & WRITECLOSE) { 3612 if (vp->v_object != NULL) { 3613 VM_OBJECT_WLOCK(vp->v_object); 3614 vm_object_page_clean(vp->v_object, 0, 0, 0); 3615 VM_OBJECT_WUNLOCK(vp->v_object); 3616 } 3617 error = VOP_FSYNC(vp, MNT_WAIT, td); 3618 if (error != 0) { 3619 VOP_UNLOCK(vp); 3620 vdrop(vp); 3621 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp); 3622 return (error); 3623 } 3624 error = VOP_GETATTR(vp, &vattr, td->td_ucred); 3625 VI_LOCK(vp); 3626 3627 if ((vp->v_type == VNON || 3628 (error == 0 && vattr.va_nlink > 0)) && 3629 (vp->v_writecount <= 0 || vp->v_type != VREG)) { 3630 VOP_UNLOCK(vp); 3631 vdropl(vp); 3632 continue; 3633 } 3634 } else 3635 VI_LOCK(vp); 3636 /* 3637 * With v_usecount == 0, all we need to do is clear out the 3638 * vnode data structures and we are done. 3639 * 3640 * If FORCECLOSE is set, forcibly close the vnode. 3641 */ 3642 if (vp->v_usecount == 0 || (flags & FORCECLOSE)) { 3643 vgonel(vp); 3644 } else { 3645 busy++; 3646 #ifdef DIAGNOSTIC 3647 if (busyprt) 3648 vn_printf(vp, "vflush: busy vnode "); 3649 #endif 3650 } 3651 VOP_UNLOCK(vp); 3652 vdropl(vp); 3653 } 3654 if (rootrefs > 0 && (flags & FORCECLOSE) == 0) { 3655 /* 3656 * If just the root vnode is busy, and if its refcount 3657 * is equal to `rootrefs', then go ahead and kill it. 3658 */ 3659 VI_LOCK(rootvp); 3660 KASSERT(busy > 0, ("vflush: not busy")); 3661 VNASSERT(rootvp->v_usecount >= rootrefs, rootvp, 3662 ("vflush: usecount %d < rootrefs %d", 3663 rootvp->v_usecount, rootrefs)); 3664 if (busy == 1 && rootvp->v_usecount == rootrefs) { 3665 VOP_LOCK(rootvp, LK_EXCLUSIVE|LK_INTERLOCK); 3666 vgone(rootvp); 3667 VOP_UNLOCK(rootvp); 3668 busy = 0; 3669 } else 3670 VI_UNLOCK(rootvp); 3671 } 3672 if (busy) { 3673 CTR2(KTR_VFS, "%s: failing as %d vnodes are busy", __func__, 3674 busy); 3675 return (EBUSY); 3676 } 3677 for (; rootrefs > 0; rootrefs--) 3678 vrele(rootvp); 3679 return (0); 3680 } 3681 3682 /* 3683 * Recycle an unused vnode to the front of the free list. 3684 */ 3685 int 3686 vrecycle(struct vnode *vp) 3687 { 3688 int recycled; 3689 3690 VI_LOCK(vp); 3691 recycled = vrecyclel(vp); 3692 VI_UNLOCK(vp); 3693 return (recycled); 3694 } 3695 3696 /* 3697 * vrecycle, with the vp interlock held. 3698 */ 3699 int 3700 vrecyclel(struct vnode *vp) 3701 { 3702 int recycled; 3703 3704 ASSERT_VOP_ELOCKED(vp, __func__); 3705 ASSERT_VI_LOCKED(vp, __func__); 3706 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 3707 recycled = 0; 3708 if (vp->v_usecount == 0) { 3709 recycled = 1; 3710 vgonel(vp); 3711 } 3712 return (recycled); 3713 } 3714 3715 /* 3716 * Eliminate all activity associated with a vnode 3717 * in preparation for reuse. 3718 */ 3719 void 3720 vgone(struct vnode *vp) 3721 { 3722 VI_LOCK(vp); 3723 vgonel(vp); 3724 VI_UNLOCK(vp); 3725 } 3726 3727 static void 3728 notify_lowervp_vfs_dummy(struct mount *mp __unused, 3729 struct vnode *lowervp __unused) 3730 { 3731 } 3732 3733 /* 3734 * Notify upper mounts about reclaimed or unlinked vnode. 3735 */ 3736 void 3737 vfs_notify_upper(struct vnode *vp, int event) 3738 { 3739 static struct vfsops vgonel_vfsops = { 3740 .vfs_reclaim_lowervp = notify_lowervp_vfs_dummy, 3741 .vfs_unlink_lowervp = notify_lowervp_vfs_dummy, 3742 }; 3743 struct mount *mp, *ump, *mmp; 3744 3745 mp = vp->v_mount; 3746 if (mp == NULL) 3747 return; 3748 if (TAILQ_EMPTY(&mp->mnt_uppers)) 3749 return; 3750 3751 mmp = malloc(sizeof(struct mount), M_TEMP, M_WAITOK | M_ZERO); 3752 mmp->mnt_op = &vgonel_vfsops; 3753 mmp->mnt_kern_flag |= MNTK_MARKER; 3754 MNT_ILOCK(mp); 3755 mp->mnt_kern_flag |= MNTK_VGONE_UPPER; 3756 for (ump = TAILQ_FIRST(&mp->mnt_uppers); ump != NULL;) { 3757 if ((ump->mnt_kern_flag & MNTK_MARKER) != 0) { 3758 ump = TAILQ_NEXT(ump, mnt_upper_link); 3759 continue; 3760 } 3761 TAILQ_INSERT_AFTER(&mp->mnt_uppers, ump, mmp, mnt_upper_link); 3762 MNT_IUNLOCK(mp); 3763 switch (event) { 3764 case VFS_NOTIFY_UPPER_RECLAIM: 3765 VFS_RECLAIM_LOWERVP(ump, vp); 3766 break; 3767 case VFS_NOTIFY_UPPER_UNLINK: 3768 VFS_UNLINK_LOWERVP(ump, vp); 3769 break; 3770 default: 3771 KASSERT(0, ("invalid event %d", event)); 3772 break; 3773 } 3774 MNT_ILOCK(mp); 3775 ump = TAILQ_NEXT(mmp, mnt_upper_link); 3776 TAILQ_REMOVE(&mp->mnt_uppers, mmp, mnt_upper_link); 3777 } 3778 free(mmp, M_TEMP); 3779 mp->mnt_kern_flag &= ~MNTK_VGONE_UPPER; 3780 if ((mp->mnt_kern_flag & MNTK_VGONE_WAITER) != 0) { 3781 mp->mnt_kern_flag &= ~MNTK_VGONE_WAITER; 3782 wakeup(&mp->mnt_uppers); 3783 } 3784 MNT_IUNLOCK(mp); 3785 } 3786 3787 /* 3788 * vgone, with the vp interlock held. 3789 */ 3790 static void 3791 vgonel(struct vnode *vp) 3792 { 3793 struct thread *td; 3794 struct mount *mp; 3795 vm_object_t object; 3796 bool active, oweinact; 3797 3798 ASSERT_VOP_ELOCKED(vp, "vgonel"); 3799 ASSERT_VI_LOCKED(vp, "vgonel"); 3800 VNASSERT(vp->v_holdcnt, vp, 3801 ("vgonel: vp %p has no reference.", vp)); 3802 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 3803 td = curthread; 3804 3805 /* 3806 * Don't vgonel if we're already doomed. 3807 */ 3808 if (vp->v_irflag & VIRF_DOOMED) 3809 return; 3810 vp->v_irflag |= VIRF_DOOMED; 3811 3812 /* 3813 * Check to see if the vnode is in use. If so, we have to call 3814 * VOP_CLOSE() and VOP_INACTIVE(). 3815 */ 3816 active = vp->v_usecount > 0; 3817 oweinact = (vp->v_iflag & VI_OWEINACT) != 0; 3818 /* 3819 * If we need to do inactive VI_OWEINACT will be set. 3820 */ 3821 if (vp->v_iflag & VI_DEFINACT) { 3822 VNASSERT(vp->v_holdcnt > 1, vp, ("lost hold count")); 3823 vp->v_iflag &= ~VI_DEFINACT; 3824 vdropl(vp); 3825 } else { 3826 VNASSERT(vp->v_holdcnt > 0, vp, ("vnode without hold count")); 3827 VI_UNLOCK(vp); 3828 } 3829 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_RECLAIM); 3830 3831 /* 3832 * If purging an active vnode, it must be closed and 3833 * deactivated before being reclaimed. 3834 */ 3835 if (active) 3836 VOP_CLOSE(vp, FNONBLOCK, NOCRED, td); 3837 if (oweinact || active) { 3838 VI_LOCK(vp); 3839 vinactivef(vp); 3840 VI_UNLOCK(vp); 3841 } 3842 if (vp->v_type == VSOCK) 3843 vfs_unp_reclaim(vp); 3844 3845 /* 3846 * Clean out any buffers associated with the vnode. 3847 * If the flush fails, just toss the buffers. 3848 */ 3849 mp = NULL; 3850 if (!TAILQ_EMPTY(&vp->v_bufobj.bo_dirty.bv_hd)) 3851 (void) vn_start_secondary_write(vp, &mp, V_WAIT); 3852 if (vinvalbuf(vp, V_SAVE, 0, 0) != 0) { 3853 while (vinvalbuf(vp, 0, 0, 0) != 0) 3854 ; 3855 } 3856 3857 BO_LOCK(&vp->v_bufobj); 3858 KASSERT(TAILQ_EMPTY(&vp->v_bufobj.bo_dirty.bv_hd) && 3859 vp->v_bufobj.bo_dirty.bv_cnt == 0 && 3860 TAILQ_EMPTY(&vp->v_bufobj.bo_clean.bv_hd) && 3861 vp->v_bufobj.bo_clean.bv_cnt == 0, 3862 ("vp %p bufobj not invalidated", vp)); 3863 3864 /* 3865 * For VMIO bufobj, BO_DEAD is set later, or in 3866 * vm_object_terminate() after the object's page queue is 3867 * flushed. 3868 */ 3869 object = vp->v_bufobj.bo_object; 3870 if (object == NULL) 3871 vp->v_bufobj.bo_flag |= BO_DEAD; 3872 BO_UNLOCK(&vp->v_bufobj); 3873 3874 /* 3875 * Handle the VM part. Tmpfs handles v_object on its own (the 3876 * OBJT_VNODE check). Nullfs or other bypassing filesystems 3877 * should not touch the object borrowed from the lower vnode 3878 * (the handle check). 3879 */ 3880 if (object != NULL && object->type == OBJT_VNODE && 3881 object->handle == vp) 3882 vnode_destroy_vobject(vp); 3883 3884 /* 3885 * Reclaim the vnode. 3886 */ 3887 if (VOP_RECLAIM(vp, td)) 3888 panic("vgone: cannot reclaim"); 3889 if (mp != NULL) 3890 vn_finished_secondary_write(mp); 3891 VNASSERT(vp->v_object == NULL, vp, 3892 ("vop_reclaim left v_object vp=%p", vp)); 3893 /* 3894 * Clear the advisory locks and wake up waiting threads. 3895 */ 3896 (void)VOP_ADVLOCKPURGE(vp); 3897 vp->v_lockf = NULL; 3898 /* 3899 * Delete from old mount point vnode list. 3900 */ 3901 delmntque(vp); 3902 cache_purge(vp); 3903 /* 3904 * Done with purge, reset to the standard lock and invalidate 3905 * the vnode. 3906 */ 3907 VI_LOCK(vp); 3908 vp->v_vnlock = &vp->v_lock; 3909 vp->v_op = &dead_vnodeops; 3910 vp->v_type = VBAD; 3911 } 3912 3913 /* 3914 * Calculate the total number of references to a special device. 3915 */ 3916 int 3917 vcount(struct vnode *vp) 3918 { 3919 int count; 3920 3921 dev_lock(); 3922 count = vp->v_rdev->si_usecount; 3923 dev_unlock(); 3924 return (count); 3925 } 3926 3927 /* 3928 * Print out a description of a vnode. 3929 */ 3930 static char *typename[] = 3931 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD", 3932 "VMARKER"}; 3933 3934 void 3935 vn_printf(struct vnode *vp, const char *fmt, ...) 3936 { 3937 va_list ap; 3938 char buf[256], buf2[16]; 3939 u_long flags; 3940 3941 va_start(ap, fmt); 3942 vprintf(fmt, ap); 3943 va_end(ap); 3944 printf("%p: ", (void *)vp); 3945 printf("type %s\n", typename[vp->v_type]); 3946 printf(" usecount %d, writecount %d, refcount %d", 3947 vp->v_usecount, vp->v_writecount, vp->v_holdcnt); 3948 switch (vp->v_type) { 3949 case VDIR: 3950 printf(" mountedhere %p\n", vp->v_mountedhere); 3951 break; 3952 case VCHR: 3953 printf(" rdev %p\n", vp->v_rdev); 3954 break; 3955 case VSOCK: 3956 printf(" socket %p\n", vp->v_unpcb); 3957 break; 3958 case VFIFO: 3959 printf(" fifoinfo %p\n", vp->v_fifoinfo); 3960 break; 3961 default: 3962 printf("\n"); 3963 break; 3964 } 3965 buf[0] = '\0'; 3966 buf[1] = '\0'; 3967 if (vp->v_irflag & VIRF_DOOMED) 3968 strlcat(buf, "|VIRF_DOOMED", sizeof(buf)); 3969 flags = vp->v_irflag & ~(VIRF_DOOMED); 3970 if (flags != 0) { 3971 snprintf(buf2, sizeof(buf2), "|VIRF(0x%lx)", flags); 3972 strlcat(buf, buf2, sizeof(buf)); 3973 } 3974 if (vp->v_vflag & VV_ROOT) 3975 strlcat(buf, "|VV_ROOT", sizeof(buf)); 3976 if (vp->v_vflag & VV_ISTTY) 3977 strlcat(buf, "|VV_ISTTY", sizeof(buf)); 3978 if (vp->v_vflag & VV_NOSYNC) 3979 strlcat(buf, "|VV_NOSYNC", sizeof(buf)); 3980 if (vp->v_vflag & VV_ETERNALDEV) 3981 strlcat(buf, "|VV_ETERNALDEV", sizeof(buf)); 3982 if (vp->v_vflag & VV_CACHEDLABEL) 3983 strlcat(buf, "|VV_CACHEDLABEL", sizeof(buf)); 3984 if (vp->v_vflag & VV_VMSIZEVNLOCK) 3985 strlcat(buf, "|VV_VMSIZEVNLOCK", sizeof(buf)); 3986 if (vp->v_vflag & VV_COPYONWRITE) 3987 strlcat(buf, "|VV_COPYONWRITE", sizeof(buf)); 3988 if (vp->v_vflag & VV_SYSTEM) 3989 strlcat(buf, "|VV_SYSTEM", sizeof(buf)); 3990 if (vp->v_vflag & VV_PROCDEP) 3991 strlcat(buf, "|VV_PROCDEP", sizeof(buf)); 3992 if (vp->v_vflag & VV_NOKNOTE) 3993 strlcat(buf, "|VV_NOKNOTE", sizeof(buf)); 3994 if (vp->v_vflag & VV_DELETED) 3995 strlcat(buf, "|VV_DELETED", sizeof(buf)); 3996 if (vp->v_vflag & VV_MD) 3997 strlcat(buf, "|VV_MD", sizeof(buf)); 3998 if (vp->v_vflag & VV_FORCEINSMQ) 3999 strlcat(buf, "|VV_FORCEINSMQ", sizeof(buf)); 4000 if (vp->v_vflag & VV_READLINK) 4001 strlcat(buf, "|VV_READLINK", sizeof(buf)); 4002 flags = vp->v_vflag & ~(VV_ROOT | VV_ISTTY | VV_NOSYNC | VV_ETERNALDEV | 4003 VV_CACHEDLABEL | VV_COPYONWRITE | VV_SYSTEM | VV_PROCDEP | 4004 VV_NOKNOTE | VV_DELETED | VV_MD | VV_FORCEINSMQ); 4005 if (flags != 0) { 4006 snprintf(buf2, sizeof(buf2), "|VV(0x%lx)", flags); 4007 strlcat(buf, buf2, sizeof(buf)); 4008 } 4009 if (vp->v_iflag & VI_TEXT_REF) 4010 strlcat(buf, "|VI_TEXT_REF", sizeof(buf)); 4011 if (vp->v_iflag & VI_MOUNT) 4012 strlcat(buf, "|VI_MOUNT", sizeof(buf)); 4013 if (vp->v_iflag & VI_DOINGINACT) 4014 strlcat(buf, "|VI_DOINGINACT", sizeof(buf)); 4015 if (vp->v_iflag & VI_OWEINACT) 4016 strlcat(buf, "|VI_OWEINACT", sizeof(buf)); 4017 if (vp->v_iflag & VI_DEFINACT) 4018 strlcat(buf, "|VI_DEFINACT", sizeof(buf)); 4019 flags = vp->v_iflag & ~(VI_TEXT_REF | VI_MOUNT | VI_DOINGINACT | 4020 VI_OWEINACT | VI_DEFINACT); 4021 if (flags != 0) { 4022 snprintf(buf2, sizeof(buf2), "|VI(0x%lx)", flags); 4023 strlcat(buf, buf2, sizeof(buf)); 4024 } 4025 if (vp->v_mflag & VMP_LAZYLIST) 4026 strlcat(buf, "|VMP_LAZYLIST", sizeof(buf)); 4027 flags = vp->v_mflag & ~(VMP_LAZYLIST); 4028 if (flags != 0) { 4029 snprintf(buf2, sizeof(buf2), "|VMP(0x%lx)", flags); 4030 strlcat(buf, buf2, sizeof(buf)); 4031 } 4032 printf(" flags (%s)\n", buf + 1); 4033 if (mtx_owned(VI_MTX(vp))) 4034 printf(" VI_LOCKed"); 4035 if (vp->v_object != NULL) 4036 printf(" v_object %p ref %d pages %d " 4037 "cleanbuf %d dirtybuf %d\n", 4038 vp->v_object, vp->v_object->ref_count, 4039 vp->v_object->resident_page_count, 4040 vp->v_bufobj.bo_clean.bv_cnt, 4041 vp->v_bufobj.bo_dirty.bv_cnt); 4042 printf(" "); 4043 lockmgr_printinfo(vp->v_vnlock); 4044 if (vp->v_data != NULL) 4045 VOP_PRINT(vp); 4046 } 4047 4048 #ifdef DDB 4049 /* 4050 * List all of the locked vnodes in the system. 4051 * Called when debugging the kernel. 4052 */ 4053 DB_SHOW_COMMAND(lockedvnods, lockedvnodes) 4054 { 4055 struct mount *mp; 4056 struct vnode *vp; 4057 4058 /* 4059 * Note: because this is DDB, we can't obey the locking semantics 4060 * for these structures, which means we could catch an inconsistent 4061 * state and dereference a nasty pointer. Not much to be done 4062 * about that. 4063 */ 4064 db_printf("Locked vnodes\n"); 4065 TAILQ_FOREACH(mp, &mountlist, mnt_list) { 4066 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) { 4067 if (vp->v_type != VMARKER && VOP_ISLOCKED(vp)) 4068 vn_printf(vp, "vnode "); 4069 } 4070 } 4071 } 4072 4073 /* 4074 * Show details about the given vnode. 4075 */ 4076 DB_SHOW_COMMAND(vnode, db_show_vnode) 4077 { 4078 struct vnode *vp; 4079 4080 if (!have_addr) 4081 return; 4082 vp = (struct vnode *)addr; 4083 vn_printf(vp, "vnode "); 4084 } 4085 4086 /* 4087 * Show details about the given mount point. 4088 */ 4089 DB_SHOW_COMMAND(mount, db_show_mount) 4090 { 4091 struct mount *mp; 4092 struct vfsopt *opt; 4093 struct statfs *sp; 4094 struct vnode *vp; 4095 char buf[512]; 4096 uint64_t mflags; 4097 u_int flags; 4098 4099 if (!have_addr) { 4100 /* No address given, print short info about all mount points. */ 4101 TAILQ_FOREACH(mp, &mountlist, mnt_list) { 4102 db_printf("%p %s on %s (%s)\n", mp, 4103 mp->mnt_stat.f_mntfromname, 4104 mp->mnt_stat.f_mntonname, 4105 mp->mnt_stat.f_fstypename); 4106 if (db_pager_quit) 4107 break; 4108 } 4109 db_printf("\nMore info: show mount <addr>\n"); 4110 return; 4111 } 4112 4113 mp = (struct mount *)addr; 4114 db_printf("%p %s on %s (%s)\n", mp, mp->mnt_stat.f_mntfromname, 4115 mp->mnt_stat.f_mntonname, mp->mnt_stat.f_fstypename); 4116 4117 buf[0] = '\0'; 4118 mflags = mp->mnt_flag; 4119 #define MNT_FLAG(flag) do { \ 4120 if (mflags & (flag)) { \ 4121 if (buf[0] != '\0') \ 4122 strlcat(buf, ", ", sizeof(buf)); \ 4123 strlcat(buf, (#flag) + 4, sizeof(buf)); \ 4124 mflags &= ~(flag); \ 4125 } \ 4126 } while (0) 4127 MNT_FLAG(MNT_RDONLY); 4128 MNT_FLAG(MNT_SYNCHRONOUS); 4129 MNT_FLAG(MNT_NOEXEC); 4130 MNT_FLAG(MNT_NOSUID); 4131 MNT_FLAG(MNT_NFS4ACLS); 4132 MNT_FLAG(MNT_UNION); 4133 MNT_FLAG(MNT_ASYNC); 4134 MNT_FLAG(MNT_SUIDDIR); 4135 MNT_FLAG(MNT_SOFTDEP); 4136 MNT_FLAG(MNT_NOSYMFOLLOW); 4137 MNT_FLAG(MNT_GJOURNAL); 4138 MNT_FLAG(MNT_MULTILABEL); 4139 MNT_FLAG(MNT_ACLS); 4140 MNT_FLAG(MNT_NOATIME); 4141 MNT_FLAG(MNT_NOCLUSTERR); 4142 MNT_FLAG(MNT_NOCLUSTERW); 4143 MNT_FLAG(MNT_SUJ); 4144 MNT_FLAG(MNT_EXRDONLY); 4145 MNT_FLAG(MNT_EXPORTED); 4146 MNT_FLAG(MNT_DEFEXPORTED); 4147 MNT_FLAG(MNT_EXPORTANON); 4148 MNT_FLAG(MNT_EXKERB); 4149 MNT_FLAG(MNT_EXPUBLIC); 4150 MNT_FLAG(MNT_LOCAL); 4151 MNT_FLAG(MNT_QUOTA); 4152 MNT_FLAG(MNT_ROOTFS); 4153 MNT_FLAG(MNT_USER); 4154 MNT_FLAG(MNT_IGNORE); 4155 MNT_FLAG(MNT_UPDATE); 4156 MNT_FLAG(MNT_DELEXPORT); 4157 MNT_FLAG(MNT_RELOAD); 4158 MNT_FLAG(MNT_FORCE); 4159 MNT_FLAG(MNT_SNAPSHOT); 4160 MNT_FLAG(MNT_BYFSID); 4161 #undef MNT_FLAG 4162 if (mflags != 0) { 4163 if (buf[0] != '\0') 4164 strlcat(buf, ", ", sizeof(buf)); 4165 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), 4166 "0x%016jx", mflags); 4167 } 4168 db_printf(" mnt_flag = %s\n", buf); 4169 4170 buf[0] = '\0'; 4171 flags = mp->mnt_kern_flag; 4172 #define MNT_KERN_FLAG(flag) do { \ 4173 if (flags & (flag)) { \ 4174 if (buf[0] != '\0') \ 4175 strlcat(buf, ", ", sizeof(buf)); \ 4176 strlcat(buf, (#flag) + 5, sizeof(buf)); \ 4177 flags &= ~(flag); \ 4178 } \ 4179 } while (0) 4180 MNT_KERN_FLAG(MNTK_UNMOUNTF); 4181 MNT_KERN_FLAG(MNTK_ASYNC); 4182 MNT_KERN_FLAG(MNTK_SOFTDEP); 4183 MNT_KERN_FLAG(MNTK_DRAINING); 4184 MNT_KERN_FLAG(MNTK_REFEXPIRE); 4185 MNT_KERN_FLAG(MNTK_EXTENDED_SHARED); 4186 MNT_KERN_FLAG(MNTK_SHARED_WRITES); 4187 MNT_KERN_FLAG(MNTK_NO_IOPF); 4188 MNT_KERN_FLAG(MNTK_VGONE_UPPER); 4189 MNT_KERN_FLAG(MNTK_VGONE_WAITER); 4190 MNT_KERN_FLAG(MNTK_LOOKUP_EXCL_DOTDOT); 4191 MNT_KERN_FLAG(MNTK_MARKER); 4192 MNT_KERN_FLAG(MNTK_USES_BCACHE); 4193 MNT_KERN_FLAG(MNTK_NOASYNC); 4194 MNT_KERN_FLAG(MNTK_UNMOUNT); 4195 MNT_KERN_FLAG(MNTK_MWAIT); 4196 MNT_KERN_FLAG(MNTK_SUSPEND); 4197 MNT_KERN_FLAG(MNTK_SUSPEND2); 4198 MNT_KERN_FLAG(MNTK_SUSPENDED); 4199 MNT_KERN_FLAG(MNTK_LOOKUP_SHARED); 4200 MNT_KERN_FLAG(MNTK_NOKNOTE); 4201 #undef MNT_KERN_FLAG 4202 if (flags != 0) { 4203 if (buf[0] != '\0') 4204 strlcat(buf, ", ", sizeof(buf)); 4205 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), 4206 "0x%08x", flags); 4207 } 4208 db_printf(" mnt_kern_flag = %s\n", buf); 4209 4210 db_printf(" mnt_opt = "); 4211 opt = TAILQ_FIRST(mp->mnt_opt); 4212 if (opt != NULL) { 4213 db_printf("%s", opt->name); 4214 opt = TAILQ_NEXT(opt, link); 4215 while (opt != NULL) { 4216 db_printf(", %s", opt->name); 4217 opt = TAILQ_NEXT(opt, link); 4218 } 4219 } 4220 db_printf("\n"); 4221 4222 sp = &mp->mnt_stat; 4223 db_printf(" mnt_stat = { version=%u type=%u flags=0x%016jx " 4224 "bsize=%ju iosize=%ju blocks=%ju bfree=%ju bavail=%jd files=%ju " 4225 "ffree=%jd syncwrites=%ju asyncwrites=%ju syncreads=%ju " 4226 "asyncreads=%ju namemax=%u owner=%u fsid=[%d, %d] }\n", 4227 (u_int)sp->f_version, (u_int)sp->f_type, (uintmax_t)sp->f_flags, 4228 (uintmax_t)sp->f_bsize, (uintmax_t)sp->f_iosize, 4229 (uintmax_t)sp->f_blocks, (uintmax_t)sp->f_bfree, 4230 (intmax_t)sp->f_bavail, (uintmax_t)sp->f_files, 4231 (intmax_t)sp->f_ffree, (uintmax_t)sp->f_syncwrites, 4232 (uintmax_t)sp->f_asyncwrites, (uintmax_t)sp->f_syncreads, 4233 (uintmax_t)sp->f_asyncreads, (u_int)sp->f_namemax, 4234 (u_int)sp->f_owner, (int)sp->f_fsid.val[0], (int)sp->f_fsid.val[1]); 4235 4236 db_printf(" mnt_cred = { uid=%u ruid=%u", 4237 (u_int)mp->mnt_cred->cr_uid, (u_int)mp->mnt_cred->cr_ruid); 4238 if (jailed(mp->mnt_cred)) 4239 db_printf(", jail=%d", mp->mnt_cred->cr_prison->pr_id); 4240 db_printf(" }\n"); 4241 db_printf(" mnt_ref = %d (with %d in the struct)\n", 4242 vfs_mount_fetch_counter(mp, MNT_COUNT_REF), mp->mnt_ref); 4243 db_printf(" mnt_gen = %d\n", mp->mnt_gen); 4244 db_printf(" mnt_nvnodelistsize = %d\n", mp->mnt_nvnodelistsize); 4245 db_printf(" mnt_lazyvnodelistsize = %d\n", 4246 mp->mnt_lazyvnodelistsize); 4247 db_printf(" mnt_writeopcount = %d (with %d in the struct)\n", 4248 vfs_mount_fetch_counter(mp, MNT_COUNT_WRITEOPCOUNT), mp->mnt_writeopcount); 4249 db_printf(" mnt_maxsymlinklen = %d\n", mp->mnt_maxsymlinklen); 4250 db_printf(" mnt_iosize_max = %d\n", mp->mnt_iosize_max); 4251 db_printf(" mnt_hashseed = %u\n", mp->mnt_hashseed); 4252 db_printf(" mnt_lockref = %d (with %d in the struct)\n", 4253 vfs_mount_fetch_counter(mp, MNT_COUNT_LOCKREF), mp->mnt_lockref); 4254 db_printf(" mnt_secondary_writes = %d\n", mp->mnt_secondary_writes); 4255 db_printf(" mnt_secondary_accwrites = %d\n", 4256 mp->mnt_secondary_accwrites); 4257 db_printf(" mnt_gjprovider = %s\n", 4258 mp->mnt_gjprovider != NULL ? mp->mnt_gjprovider : "NULL"); 4259 db_printf(" mnt_vfs_ops = %d\n", mp->mnt_vfs_ops); 4260 4261 db_printf("\n\nList of active vnodes\n"); 4262 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) { 4263 if (vp->v_type != VMARKER && vp->v_holdcnt > 0) { 4264 vn_printf(vp, "vnode "); 4265 if (db_pager_quit) 4266 break; 4267 } 4268 } 4269 db_printf("\n\nList of inactive vnodes\n"); 4270 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) { 4271 if (vp->v_type != VMARKER && vp->v_holdcnt == 0) { 4272 vn_printf(vp, "vnode "); 4273 if (db_pager_quit) 4274 break; 4275 } 4276 } 4277 } 4278 #endif /* DDB */ 4279 4280 /* 4281 * Fill in a struct xvfsconf based on a struct vfsconf. 4282 */ 4283 static int 4284 vfsconf2x(struct sysctl_req *req, struct vfsconf *vfsp) 4285 { 4286 struct xvfsconf xvfsp; 4287 4288 bzero(&xvfsp, sizeof(xvfsp)); 4289 strcpy(xvfsp.vfc_name, vfsp->vfc_name); 4290 xvfsp.vfc_typenum = vfsp->vfc_typenum; 4291 xvfsp.vfc_refcount = vfsp->vfc_refcount; 4292 xvfsp.vfc_flags = vfsp->vfc_flags; 4293 /* 4294 * These are unused in userland, we keep them 4295 * to not break binary compatibility. 4296 */ 4297 xvfsp.vfc_vfsops = NULL; 4298 xvfsp.vfc_next = NULL; 4299 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp))); 4300 } 4301 4302 #ifdef COMPAT_FREEBSD32 4303 struct xvfsconf32 { 4304 uint32_t vfc_vfsops; 4305 char vfc_name[MFSNAMELEN]; 4306 int32_t vfc_typenum; 4307 int32_t vfc_refcount; 4308 int32_t vfc_flags; 4309 uint32_t vfc_next; 4310 }; 4311 4312 static int 4313 vfsconf2x32(struct sysctl_req *req, struct vfsconf *vfsp) 4314 { 4315 struct xvfsconf32 xvfsp; 4316 4317 bzero(&xvfsp, sizeof(xvfsp)); 4318 strcpy(xvfsp.vfc_name, vfsp->vfc_name); 4319 xvfsp.vfc_typenum = vfsp->vfc_typenum; 4320 xvfsp.vfc_refcount = vfsp->vfc_refcount; 4321 xvfsp.vfc_flags = vfsp->vfc_flags; 4322 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp))); 4323 } 4324 #endif 4325 4326 /* 4327 * Top level filesystem related information gathering. 4328 */ 4329 static int 4330 sysctl_vfs_conflist(SYSCTL_HANDLER_ARGS) 4331 { 4332 struct vfsconf *vfsp; 4333 int error; 4334 4335 error = 0; 4336 vfsconf_slock(); 4337 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) { 4338 #ifdef COMPAT_FREEBSD32 4339 if (req->flags & SCTL_MASK32) 4340 error = vfsconf2x32(req, vfsp); 4341 else 4342 #endif 4343 error = vfsconf2x(req, vfsp); 4344 if (error) 4345 break; 4346 } 4347 vfsconf_sunlock(); 4348 return (error); 4349 } 4350 4351 SYSCTL_PROC(_vfs, OID_AUTO, conflist, CTLTYPE_OPAQUE | CTLFLAG_RD | 4352 CTLFLAG_MPSAFE, NULL, 0, sysctl_vfs_conflist, 4353 "S,xvfsconf", "List of all configured filesystems"); 4354 4355 #ifndef BURN_BRIDGES 4356 static int sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS); 4357 4358 static int 4359 vfs_sysctl(SYSCTL_HANDLER_ARGS) 4360 { 4361 int *name = (int *)arg1 - 1; /* XXX */ 4362 u_int namelen = arg2 + 1; /* XXX */ 4363 struct vfsconf *vfsp; 4364 4365 log(LOG_WARNING, "userland calling deprecated sysctl, " 4366 "please rebuild world\n"); 4367 4368 #if 1 || defined(COMPAT_PRELITE2) 4369 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */ 4370 if (namelen == 1) 4371 return (sysctl_ovfs_conf(oidp, arg1, arg2, req)); 4372 #endif 4373 4374 switch (name[1]) { 4375 case VFS_MAXTYPENUM: 4376 if (namelen != 2) 4377 return (ENOTDIR); 4378 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int))); 4379 case VFS_CONF: 4380 if (namelen != 3) 4381 return (ENOTDIR); /* overloaded */ 4382 vfsconf_slock(); 4383 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) { 4384 if (vfsp->vfc_typenum == name[2]) 4385 break; 4386 } 4387 vfsconf_sunlock(); 4388 if (vfsp == NULL) 4389 return (EOPNOTSUPP); 4390 #ifdef COMPAT_FREEBSD32 4391 if (req->flags & SCTL_MASK32) 4392 return (vfsconf2x32(req, vfsp)); 4393 else 4394 #endif 4395 return (vfsconf2x(req, vfsp)); 4396 } 4397 return (EOPNOTSUPP); 4398 } 4399 4400 static SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD | CTLFLAG_SKIP | 4401 CTLFLAG_MPSAFE, vfs_sysctl, 4402 "Generic filesystem"); 4403 4404 #if 1 || defined(COMPAT_PRELITE2) 4405 4406 static int 4407 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS) 4408 { 4409 int error; 4410 struct vfsconf *vfsp; 4411 struct ovfsconf ovfs; 4412 4413 vfsconf_slock(); 4414 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) { 4415 bzero(&ovfs, sizeof(ovfs)); 4416 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */ 4417 strcpy(ovfs.vfc_name, vfsp->vfc_name); 4418 ovfs.vfc_index = vfsp->vfc_typenum; 4419 ovfs.vfc_refcount = vfsp->vfc_refcount; 4420 ovfs.vfc_flags = vfsp->vfc_flags; 4421 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs); 4422 if (error != 0) { 4423 vfsconf_sunlock(); 4424 return (error); 4425 } 4426 } 4427 vfsconf_sunlock(); 4428 return (0); 4429 } 4430 4431 #endif /* 1 || COMPAT_PRELITE2 */ 4432 #endif /* !BURN_BRIDGES */ 4433 4434 #define KINFO_VNODESLOP 10 4435 #ifdef notyet 4436 /* 4437 * Dump vnode list (via sysctl). 4438 */ 4439 /* ARGSUSED */ 4440 static int 4441 sysctl_vnode(SYSCTL_HANDLER_ARGS) 4442 { 4443 struct xvnode *xvn; 4444 struct mount *mp; 4445 struct vnode *vp; 4446 int error, len, n; 4447 4448 /* 4449 * Stale numvnodes access is not fatal here. 4450 */ 4451 req->lock = 0; 4452 len = (numvnodes + KINFO_VNODESLOP) * sizeof *xvn; 4453 if (!req->oldptr) 4454 /* Make an estimate */ 4455 return (SYSCTL_OUT(req, 0, len)); 4456 4457 error = sysctl_wire_old_buffer(req, 0); 4458 if (error != 0) 4459 return (error); 4460 xvn = malloc(len, M_TEMP, M_ZERO | M_WAITOK); 4461 n = 0; 4462 mtx_lock(&mountlist_mtx); 4463 TAILQ_FOREACH(mp, &mountlist, mnt_list) { 4464 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) 4465 continue; 4466 MNT_ILOCK(mp); 4467 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) { 4468 if (n == len) 4469 break; 4470 vref(vp); 4471 xvn[n].xv_size = sizeof *xvn; 4472 xvn[n].xv_vnode = vp; 4473 xvn[n].xv_id = 0; /* XXX compat */ 4474 #define XV_COPY(field) xvn[n].xv_##field = vp->v_##field 4475 XV_COPY(usecount); 4476 XV_COPY(writecount); 4477 XV_COPY(holdcnt); 4478 XV_COPY(mount); 4479 XV_COPY(numoutput); 4480 XV_COPY(type); 4481 #undef XV_COPY 4482 xvn[n].xv_flag = vp->v_vflag; 4483 4484 switch (vp->v_type) { 4485 case VREG: 4486 case VDIR: 4487 case VLNK: 4488 break; 4489 case VBLK: 4490 case VCHR: 4491 if (vp->v_rdev == NULL) { 4492 vrele(vp); 4493 continue; 4494 } 4495 xvn[n].xv_dev = dev2udev(vp->v_rdev); 4496 break; 4497 case VSOCK: 4498 xvn[n].xv_socket = vp->v_socket; 4499 break; 4500 case VFIFO: 4501 xvn[n].xv_fifo = vp->v_fifoinfo; 4502 break; 4503 case VNON: 4504 case VBAD: 4505 default: 4506 /* shouldn't happen? */ 4507 vrele(vp); 4508 continue; 4509 } 4510 vrele(vp); 4511 ++n; 4512 } 4513 MNT_IUNLOCK(mp); 4514 mtx_lock(&mountlist_mtx); 4515 vfs_unbusy(mp); 4516 if (n == len) 4517 break; 4518 } 4519 mtx_unlock(&mountlist_mtx); 4520 4521 error = SYSCTL_OUT(req, xvn, n * sizeof *xvn); 4522 free(xvn, M_TEMP); 4523 return (error); 4524 } 4525 4526 SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE | CTLFLAG_RD | 4527 CTLFLAG_MPSAFE, 0, 0, sysctl_vnode, "S,xvnode", 4528 ""); 4529 #endif 4530 4531 static void 4532 unmount_or_warn(struct mount *mp) 4533 { 4534 int error; 4535 4536 error = dounmount(mp, MNT_FORCE, curthread); 4537 if (error != 0) { 4538 printf("unmount of %s failed (", mp->mnt_stat.f_mntonname); 4539 if (error == EBUSY) 4540 printf("BUSY)\n"); 4541 else 4542 printf("%d)\n", error); 4543 } 4544 } 4545 4546 /* 4547 * Unmount all filesystems. The list is traversed in reverse order 4548 * of mounting to avoid dependencies. 4549 */ 4550 void 4551 vfs_unmountall(void) 4552 { 4553 struct mount *mp, *tmp; 4554 4555 CTR1(KTR_VFS, "%s: unmounting all filesystems", __func__); 4556 4557 /* 4558 * Since this only runs when rebooting, it is not interlocked. 4559 */ 4560 TAILQ_FOREACH_REVERSE_SAFE(mp, &mountlist, mntlist, mnt_list, tmp) { 4561 vfs_ref(mp); 4562 4563 /* 4564 * Forcibly unmounting "/dev" before "/" would prevent clean 4565 * unmount of the latter. 4566 */ 4567 if (mp == rootdevmp) 4568 continue; 4569 4570 unmount_or_warn(mp); 4571 } 4572 4573 if (rootdevmp != NULL) 4574 unmount_or_warn(rootdevmp); 4575 } 4576 4577 static void 4578 vfs_deferred_inactive(struct vnode *vp, int lkflags) 4579 { 4580 4581 ASSERT_VI_LOCKED(vp, __func__); 4582 VNASSERT((vp->v_iflag & VI_DEFINACT) == 0, vp, ("VI_DEFINACT still set")); 4583 if ((vp->v_iflag & VI_OWEINACT) == 0) { 4584 vdropl(vp); 4585 return; 4586 } 4587 if (vn_lock(vp, lkflags) == 0) { 4588 VI_LOCK(vp); 4589 vinactive(vp); 4590 VOP_UNLOCK(vp); 4591 vdropl(vp); 4592 return; 4593 } 4594 vdefer_inactive_unlocked(vp); 4595 } 4596 4597 static int 4598 vfs_periodic_inactive_filter(struct vnode *vp, void *arg) 4599 { 4600 4601 return (vp->v_iflag & VI_DEFINACT); 4602 } 4603 4604 static void __noinline 4605 vfs_periodic_inactive(struct mount *mp, int flags) 4606 { 4607 struct vnode *vp, *mvp; 4608 int lkflags; 4609 4610 lkflags = LK_EXCLUSIVE | LK_INTERLOCK; 4611 if (flags != MNT_WAIT) 4612 lkflags |= LK_NOWAIT; 4613 4614 MNT_VNODE_FOREACH_LAZY(vp, mp, mvp, vfs_periodic_inactive_filter, NULL) { 4615 if ((vp->v_iflag & VI_DEFINACT) == 0) { 4616 VI_UNLOCK(vp); 4617 continue; 4618 } 4619 vp->v_iflag &= ~VI_DEFINACT; 4620 vfs_deferred_inactive(vp, lkflags); 4621 } 4622 } 4623 4624 static inline bool 4625 vfs_want_msync(struct vnode *vp) 4626 { 4627 struct vm_object *obj; 4628 4629 /* 4630 * This test may be performed without any locks held. 4631 * We rely on vm_object's type stability. 4632 */ 4633 if (vp->v_vflag & VV_NOSYNC) 4634 return (false); 4635 obj = vp->v_object; 4636 return (obj != NULL && vm_object_mightbedirty(obj)); 4637 } 4638 4639 static int 4640 vfs_periodic_msync_inactive_filter(struct vnode *vp, void *arg __unused) 4641 { 4642 4643 if (vp->v_vflag & VV_NOSYNC) 4644 return (false); 4645 if (vp->v_iflag & VI_DEFINACT) 4646 return (true); 4647 return (vfs_want_msync(vp)); 4648 } 4649 4650 static void __noinline 4651 vfs_periodic_msync_inactive(struct mount *mp, int flags) 4652 { 4653 struct vnode *vp, *mvp; 4654 struct vm_object *obj; 4655 struct thread *td; 4656 int lkflags, objflags; 4657 bool seen_defer; 4658 4659 td = curthread; 4660 4661 lkflags = LK_EXCLUSIVE | LK_INTERLOCK; 4662 if (flags != MNT_WAIT) { 4663 lkflags |= LK_NOWAIT; 4664 objflags = OBJPC_NOSYNC; 4665 } else { 4666 objflags = OBJPC_SYNC; 4667 } 4668 4669 MNT_VNODE_FOREACH_LAZY(vp, mp, mvp, vfs_periodic_msync_inactive_filter, NULL) { 4670 seen_defer = false; 4671 if (vp->v_iflag & VI_DEFINACT) { 4672 vp->v_iflag &= ~VI_DEFINACT; 4673 seen_defer = true; 4674 } 4675 if (!vfs_want_msync(vp)) { 4676 if (seen_defer) 4677 vfs_deferred_inactive(vp, lkflags); 4678 else 4679 VI_UNLOCK(vp); 4680 continue; 4681 } 4682 if (vget(vp, lkflags, td) == 0) { 4683 obj = vp->v_object; 4684 if (obj != NULL && (vp->v_vflag & VV_NOSYNC) == 0) { 4685 VM_OBJECT_WLOCK(obj); 4686 vm_object_page_clean(obj, 0, 0, objflags); 4687 VM_OBJECT_WUNLOCK(obj); 4688 } 4689 vput(vp); 4690 if (seen_defer) 4691 vdrop(vp); 4692 } else { 4693 if (seen_defer) 4694 vdefer_inactive_unlocked(vp); 4695 } 4696 } 4697 } 4698 4699 void 4700 vfs_periodic(struct mount *mp, int flags) 4701 { 4702 4703 CTR2(KTR_VFS, "%s: mp %p", __func__, mp); 4704 4705 if ((mp->mnt_kern_flag & MNTK_NOMSYNC) != 0) 4706 vfs_periodic_inactive(mp, flags); 4707 else 4708 vfs_periodic_msync_inactive(mp, flags); 4709 } 4710 4711 static void 4712 destroy_vpollinfo_free(struct vpollinfo *vi) 4713 { 4714 4715 knlist_destroy(&vi->vpi_selinfo.si_note); 4716 mtx_destroy(&vi->vpi_lock); 4717 uma_zfree(vnodepoll_zone, vi); 4718 } 4719 4720 static void 4721 destroy_vpollinfo(struct vpollinfo *vi) 4722 { 4723 4724 knlist_clear(&vi->vpi_selinfo.si_note, 1); 4725 seldrain(&vi->vpi_selinfo); 4726 destroy_vpollinfo_free(vi); 4727 } 4728 4729 /* 4730 * Initialize per-vnode helper structure to hold poll-related state. 4731 */ 4732 void 4733 v_addpollinfo(struct vnode *vp) 4734 { 4735 struct vpollinfo *vi; 4736 4737 if (vp->v_pollinfo != NULL) 4738 return; 4739 vi = uma_zalloc(vnodepoll_zone, M_WAITOK | M_ZERO); 4740 mtx_init(&vi->vpi_lock, "vnode pollinfo", NULL, MTX_DEF); 4741 knlist_init(&vi->vpi_selinfo.si_note, vp, vfs_knllock, 4742 vfs_knlunlock, vfs_knl_assert_locked, vfs_knl_assert_unlocked); 4743 VI_LOCK(vp); 4744 if (vp->v_pollinfo != NULL) { 4745 VI_UNLOCK(vp); 4746 destroy_vpollinfo_free(vi); 4747 return; 4748 } 4749 vp->v_pollinfo = vi; 4750 VI_UNLOCK(vp); 4751 } 4752 4753 /* 4754 * Record a process's interest in events which might happen to 4755 * a vnode. Because poll uses the historic select-style interface 4756 * internally, this routine serves as both the ``check for any 4757 * pending events'' and the ``record my interest in future events'' 4758 * functions. (These are done together, while the lock is held, 4759 * to avoid race conditions.) 4760 */ 4761 int 4762 vn_pollrecord(struct vnode *vp, struct thread *td, int events) 4763 { 4764 4765 v_addpollinfo(vp); 4766 mtx_lock(&vp->v_pollinfo->vpi_lock); 4767 if (vp->v_pollinfo->vpi_revents & events) { 4768 /* 4769 * This leaves events we are not interested 4770 * in available for the other process which 4771 * which presumably had requested them 4772 * (otherwise they would never have been 4773 * recorded). 4774 */ 4775 events &= vp->v_pollinfo->vpi_revents; 4776 vp->v_pollinfo->vpi_revents &= ~events; 4777 4778 mtx_unlock(&vp->v_pollinfo->vpi_lock); 4779 return (events); 4780 } 4781 vp->v_pollinfo->vpi_events |= events; 4782 selrecord(td, &vp->v_pollinfo->vpi_selinfo); 4783 mtx_unlock(&vp->v_pollinfo->vpi_lock); 4784 return (0); 4785 } 4786 4787 /* 4788 * Routine to create and manage a filesystem syncer vnode. 4789 */ 4790 #define sync_close ((int (*)(struct vop_close_args *))nullop) 4791 static int sync_fsync(struct vop_fsync_args *); 4792 static int sync_inactive(struct vop_inactive_args *); 4793 static int sync_reclaim(struct vop_reclaim_args *); 4794 4795 static struct vop_vector sync_vnodeops = { 4796 .vop_bypass = VOP_EOPNOTSUPP, 4797 .vop_close = sync_close, /* close */ 4798 .vop_fsync = sync_fsync, /* fsync */ 4799 .vop_inactive = sync_inactive, /* inactive */ 4800 .vop_need_inactive = vop_stdneed_inactive, /* need_inactive */ 4801 .vop_reclaim = sync_reclaim, /* reclaim */ 4802 .vop_lock1 = vop_stdlock, /* lock */ 4803 .vop_unlock = vop_stdunlock, /* unlock */ 4804 .vop_islocked = vop_stdislocked, /* islocked */ 4805 }; 4806 VFS_VOP_VECTOR_REGISTER(sync_vnodeops); 4807 4808 /* 4809 * Create a new filesystem syncer vnode for the specified mount point. 4810 */ 4811 void 4812 vfs_allocate_syncvnode(struct mount *mp) 4813 { 4814 struct vnode *vp; 4815 struct bufobj *bo; 4816 static long start, incr, next; 4817 int error; 4818 4819 /* Allocate a new vnode */ 4820 error = getnewvnode("syncer", mp, &sync_vnodeops, &vp); 4821 if (error != 0) 4822 panic("vfs_allocate_syncvnode: getnewvnode() failed"); 4823 vp->v_type = VNON; 4824 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 4825 vp->v_vflag |= VV_FORCEINSMQ; 4826 error = insmntque(vp, mp); 4827 if (error != 0) 4828 panic("vfs_allocate_syncvnode: insmntque() failed"); 4829 vp->v_vflag &= ~VV_FORCEINSMQ; 4830 VOP_UNLOCK(vp); 4831 /* 4832 * Place the vnode onto the syncer worklist. We attempt to 4833 * scatter them about on the list so that they will go off 4834 * at evenly distributed times even if all the filesystems 4835 * are mounted at once. 4836 */ 4837 next += incr; 4838 if (next == 0 || next > syncer_maxdelay) { 4839 start /= 2; 4840 incr /= 2; 4841 if (start == 0) { 4842 start = syncer_maxdelay / 2; 4843 incr = syncer_maxdelay; 4844 } 4845 next = start; 4846 } 4847 bo = &vp->v_bufobj; 4848 BO_LOCK(bo); 4849 vn_syncer_add_to_worklist(bo, syncdelay > 0 ? next % syncdelay : 0); 4850 /* XXX - vn_syncer_add_to_worklist() also grabs and drops sync_mtx. */ 4851 mtx_lock(&sync_mtx); 4852 sync_vnode_count++; 4853 if (mp->mnt_syncer == NULL) { 4854 mp->mnt_syncer = vp; 4855 vp = NULL; 4856 } 4857 mtx_unlock(&sync_mtx); 4858 BO_UNLOCK(bo); 4859 if (vp != NULL) { 4860 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 4861 vgone(vp); 4862 vput(vp); 4863 } 4864 } 4865 4866 void 4867 vfs_deallocate_syncvnode(struct mount *mp) 4868 { 4869 struct vnode *vp; 4870 4871 mtx_lock(&sync_mtx); 4872 vp = mp->mnt_syncer; 4873 if (vp != NULL) 4874 mp->mnt_syncer = NULL; 4875 mtx_unlock(&sync_mtx); 4876 if (vp != NULL) 4877 vrele(vp); 4878 } 4879 4880 /* 4881 * Do a lazy sync of the filesystem. 4882 */ 4883 static int 4884 sync_fsync(struct vop_fsync_args *ap) 4885 { 4886 struct vnode *syncvp = ap->a_vp; 4887 struct mount *mp = syncvp->v_mount; 4888 int error, save; 4889 struct bufobj *bo; 4890 4891 /* 4892 * We only need to do something if this is a lazy evaluation. 4893 */ 4894 if (ap->a_waitfor != MNT_LAZY) 4895 return (0); 4896 4897 /* 4898 * Move ourselves to the back of the sync list. 4899 */ 4900 bo = &syncvp->v_bufobj; 4901 BO_LOCK(bo); 4902 vn_syncer_add_to_worklist(bo, syncdelay); 4903 BO_UNLOCK(bo); 4904 4905 /* 4906 * Walk the list of vnodes pushing all that are dirty and 4907 * not already on the sync list. 4908 */ 4909 if (vfs_busy(mp, MBF_NOWAIT) != 0) 4910 return (0); 4911 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) { 4912 vfs_unbusy(mp); 4913 return (0); 4914 } 4915 save = curthread_pflags_set(TDP_SYNCIO); 4916 /* 4917 * The filesystem at hand may be idle with free vnodes stored in the 4918 * batch. Return them instead of letting them stay there indefinitely. 4919 */ 4920 vfs_periodic(mp, MNT_NOWAIT); 4921 error = VFS_SYNC(mp, MNT_LAZY); 4922 curthread_pflags_restore(save); 4923 vn_finished_write(mp); 4924 vfs_unbusy(mp); 4925 return (error); 4926 } 4927 4928 /* 4929 * The syncer vnode is no referenced. 4930 */ 4931 static int 4932 sync_inactive(struct vop_inactive_args *ap) 4933 { 4934 4935 vgone(ap->a_vp); 4936 return (0); 4937 } 4938 4939 /* 4940 * The syncer vnode is no longer needed and is being decommissioned. 4941 * 4942 * Modifications to the worklist must be protected by sync_mtx. 4943 */ 4944 static int 4945 sync_reclaim(struct vop_reclaim_args *ap) 4946 { 4947 struct vnode *vp = ap->a_vp; 4948 struct bufobj *bo; 4949 4950 bo = &vp->v_bufobj; 4951 BO_LOCK(bo); 4952 mtx_lock(&sync_mtx); 4953 if (vp->v_mount->mnt_syncer == vp) 4954 vp->v_mount->mnt_syncer = NULL; 4955 if (bo->bo_flag & BO_ONWORKLST) { 4956 LIST_REMOVE(bo, bo_synclist); 4957 syncer_worklist_len--; 4958 sync_vnode_count--; 4959 bo->bo_flag &= ~BO_ONWORKLST; 4960 } 4961 mtx_unlock(&sync_mtx); 4962 BO_UNLOCK(bo); 4963 4964 return (0); 4965 } 4966 4967 int 4968 vn_need_pageq_flush(struct vnode *vp) 4969 { 4970 struct vm_object *obj; 4971 int need; 4972 4973 MPASS(mtx_owned(VI_MTX(vp))); 4974 need = 0; 4975 if ((obj = vp->v_object) != NULL && (vp->v_vflag & VV_NOSYNC) == 0 && 4976 vm_object_mightbedirty(obj)) 4977 need = 1; 4978 return (need); 4979 } 4980 4981 /* 4982 * Check if vnode represents a disk device 4983 */ 4984 int 4985 vn_isdisk(struct vnode *vp, int *errp) 4986 { 4987 int error; 4988 4989 if (vp->v_type != VCHR) { 4990 error = ENOTBLK; 4991 goto out; 4992 } 4993 error = 0; 4994 dev_lock(); 4995 if (vp->v_rdev == NULL) 4996 error = ENXIO; 4997 else if (vp->v_rdev->si_devsw == NULL) 4998 error = ENXIO; 4999 else if (!(vp->v_rdev->si_devsw->d_flags & D_DISK)) 5000 error = ENOTBLK; 5001 dev_unlock(); 5002 out: 5003 if (errp != NULL) 5004 *errp = error; 5005 return (error == 0); 5006 } 5007 5008 /* 5009 * Common filesystem object access control check routine. Accepts a 5010 * vnode's type, "mode", uid and gid, requested access mode, credentials, 5011 * and optional call-by-reference privused argument allowing vaccess() 5012 * to indicate to the caller whether privilege was used to satisfy the 5013 * request (obsoleted). Returns 0 on success, or an errno on failure. 5014 */ 5015 int 5016 vaccess(enum vtype type, mode_t file_mode, uid_t file_uid, gid_t file_gid, 5017 accmode_t accmode, struct ucred *cred, int *privused) 5018 { 5019 accmode_t dac_granted; 5020 accmode_t priv_granted; 5021 5022 KASSERT((accmode & ~(VEXEC | VWRITE | VREAD | VADMIN | VAPPEND)) == 0, 5023 ("invalid bit in accmode")); 5024 KASSERT((accmode & VAPPEND) == 0 || (accmode & VWRITE), 5025 ("VAPPEND without VWRITE")); 5026 5027 /* 5028 * Look for a normal, non-privileged way to access the file/directory 5029 * as requested. If it exists, go with that. 5030 */ 5031 5032 if (privused != NULL) 5033 *privused = 0; 5034 5035 dac_granted = 0; 5036 5037 /* Check the owner. */ 5038 if (cred->cr_uid == file_uid) { 5039 dac_granted |= VADMIN; 5040 if (file_mode & S_IXUSR) 5041 dac_granted |= VEXEC; 5042 if (file_mode & S_IRUSR) 5043 dac_granted |= VREAD; 5044 if (file_mode & S_IWUSR) 5045 dac_granted |= (VWRITE | VAPPEND); 5046 5047 if ((accmode & dac_granted) == accmode) 5048 return (0); 5049 5050 goto privcheck; 5051 } 5052 5053 /* Otherwise, check the groups (first match) */ 5054 if (groupmember(file_gid, cred)) { 5055 if (file_mode & S_IXGRP) 5056 dac_granted |= VEXEC; 5057 if (file_mode & S_IRGRP) 5058 dac_granted |= VREAD; 5059 if (file_mode & S_IWGRP) 5060 dac_granted |= (VWRITE | VAPPEND); 5061 5062 if ((accmode & dac_granted) == accmode) 5063 return (0); 5064 5065 goto privcheck; 5066 } 5067 5068 /* Otherwise, check everyone else. */ 5069 if (file_mode & S_IXOTH) 5070 dac_granted |= VEXEC; 5071 if (file_mode & S_IROTH) 5072 dac_granted |= VREAD; 5073 if (file_mode & S_IWOTH) 5074 dac_granted |= (VWRITE | VAPPEND); 5075 if ((accmode & dac_granted) == accmode) 5076 return (0); 5077 5078 privcheck: 5079 /* 5080 * Build a privilege mask to determine if the set of privileges 5081 * satisfies the requirements when combined with the granted mask 5082 * from above. For each privilege, if the privilege is required, 5083 * bitwise or the request type onto the priv_granted mask. 5084 */ 5085 priv_granted = 0; 5086 5087 if (type == VDIR) { 5088 /* 5089 * For directories, use PRIV_VFS_LOOKUP to satisfy VEXEC 5090 * requests, instead of PRIV_VFS_EXEC. 5091 */ 5092 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) && 5093 !priv_check_cred(cred, PRIV_VFS_LOOKUP)) 5094 priv_granted |= VEXEC; 5095 } else { 5096 /* 5097 * Ensure that at least one execute bit is on. Otherwise, 5098 * a privileged user will always succeed, and we don't want 5099 * this to happen unless the file really is executable. 5100 */ 5101 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) && 5102 (file_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0 && 5103 !priv_check_cred(cred, PRIV_VFS_EXEC)) 5104 priv_granted |= VEXEC; 5105 } 5106 5107 if ((accmode & VREAD) && ((dac_granted & VREAD) == 0) && 5108 !priv_check_cred(cred, PRIV_VFS_READ)) 5109 priv_granted |= VREAD; 5110 5111 if ((accmode & VWRITE) && ((dac_granted & VWRITE) == 0) && 5112 !priv_check_cred(cred, PRIV_VFS_WRITE)) 5113 priv_granted |= (VWRITE | VAPPEND); 5114 5115 if ((accmode & VADMIN) && ((dac_granted & VADMIN) == 0) && 5116 !priv_check_cred(cred, PRIV_VFS_ADMIN)) 5117 priv_granted |= VADMIN; 5118 5119 if ((accmode & (priv_granted | dac_granted)) == accmode) { 5120 /* XXX audit: privilege used */ 5121 if (privused != NULL) 5122 *privused = 1; 5123 return (0); 5124 } 5125 5126 return ((accmode & VADMIN) ? EPERM : EACCES); 5127 } 5128 5129 /* 5130 * Credential check based on process requesting service, and per-attribute 5131 * permissions. 5132 */ 5133 int 5134 extattr_check_cred(struct vnode *vp, int attrnamespace, struct ucred *cred, 5135 struct thread *td, accmode_t accmode) 5136 { 5137 5138 /* 5139 * Kernel-invoked always succeeds. 5140 */ 5141 if (cred == NOCRED) 5142 return (0); 5143 5144 /* 5145 * Do not allow privileged processes in jail to directly manipulate 5146 * system attributes. 5147 */ 5148 switch (attrnamespace) { 5149 case EXTATTR_NAMESPACE_SYSTEM: 5150 /* Potentially should be: return (EPERM); */ 5151 return (priv_check_cred(cred, PRIV_VFS_EXTATTR_SYSTEM)); 5152 case EXTATTR_NAMESPACE_USER: 5153 return (VOP_ACCESS(vp, accmode, cred, td)); 5154 default: 5155 return (EPERM); 5156 } 5157 } 5158 5159 #ifdef DEBUG_VFS_LOCKS 5160 /* 5161 * This only exists to suppress warnings from unlocked specfs accesses. It is 5162 * no longer ok to have an unlocked VFS. 5163 */ 5164 #define IGNORE_LOCK(vp) (KERNEL_PANICKED() || (vp) == NULL || \ 5165 (vp)->v_type == VCHR || (vp)->v_type == VBAD) 5166 5167 int vfs_badlock_ddb = 1; /* Drop into debugger on violation. */ 5168 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_ddb, CTLFLAG_RW, &vfs_badlock_ddb, 0, 5169 "Drop into debugger on lock violation"); 5170 5171 int vfs_badlock_mutex = 1; /* Check for interlock across VOPs. */ 5172 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_mutex, CTLFLAG_RW, &vfs_badlock_mutex, 5173 0, "Check for interlock across VOPs"); 5174 5175 int vfs_badlock_print = 1; /* Print lock violations. */ 5176 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_print, CTLFLAG_RW, &vfs_badlock_print, 5177 0, "Print lock violations"); 5178 5179 int vfs_badlock_vnode = 1; /* Print vnode details on lock violations. */ 5180 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_vnode, CTLFLAG_RW, &vfs_badlock_vnode, 5181 0, "Print vnode details on lock violations"); 5182 5183 #ifdef KDB 5184 int vfs_badlock_backtrace = 1; /* Print backtrace at lock violations. */ 5185 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_backtrace, CTLFLAG_RW, 5186 &vfs_badlock_backtrace, 0, "Print backtrace at lock violations"); 5187 #endif 5188 5189 static void 5190 vfs_badlock(const char *msg, const char *str, struct vnode *vp) 5191 { 5192 5193 #ifdef KDB 5194 if (vfs_badlock_backtrace) 5195 kdb_backtrace(); 5196 #endif 5197 if (vfs_badlock_vnode) 5198 vn_printf(vp, "vnode "); 5199 if (vfs_badlock_print) 5200 printf("%s: %p %s\n", str, (void *)vp, msg); 5201 if (vfs_badlock_ddb) 5202 kdb_enter(KDB_WHY_VFSLOCK, "lock violation"); 5203 } 5204 5205 void 5206 assert_vi_locked(struct vnode *vp, const char *str) 5207 { 5208 5209 if (vfs_badlock_mutex && !mtx_owned(VI_MTX(vp))) 5210 vfs_badlock("interlock is not locked but should be", str, vp); 5211 } 5212 5213 void 5214 assert_vi_unlocked(struct vnode *vp, const char *str) 5215 { 5216 5217 if (vfs_badlock_mutex && mtx_owned(VI_MTX(vp))) 5218 vfs_badlock("interlock is locked but should not be", str, vp); 5219 } 5220 5221 void 5222 assert_vop_locked(struct vnode *vp, const char *str) 5223 { 5224 int locked; 5225 5226 if (!IGNORE_LOCK(vp)) { 5227 locked = VOP_ISLOCKED(vp); 5228 if (locked == 0 || locked == LK_EXCLOTHER) 5229 vfs_badlock("is not locked but should be", str, vp); 5230 } 5231 } 5232 5233 void 5234 assert_vop_unlocked(struct vnode *vp, const char *str) 5235 { 5236 5237 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) == LK_EXCLUSIVE) 5238 vfs_badlock("is locked but should not be", str, vp); 5239 } 5240 5241 void 5242 assert_vop_elocked(struct vnode *vp, const char *str) 5243 { 5244 5245 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_EXCLUSIVE) 5246 vfs_badlock("is not exclusive locked but should be", str, vp); 5247 } 5248 #endif /* DEBUG_VFS_LOCKS */ 5249 5250 void 5251 vop_rename_fail(struct vop_rename_args *ap) 5252 { 5253 5254 if (ap->a_tvp != NULL) 5255 vput(ap->a_tvp); 5256 if (ap->a_tdvp == ap->a_tvp) 5257 vrele(ap->a_tdvp); 5258 else 5259 vput(ap->a_tdvp); 5260 vrele(ap->a_fdvp); 5261 vrele(ap->a_fvp); 5262 } 5263 5264 void 5265 vop_rename_pre(void *ap) 5266 { 5267 struct vop_rename_args *a = ap; 5268 5269 #ifdef DEBUG_VFS_LOCKS 5270 if (a->a_tvp) 5271 ASSERT_VI_UNLOCKED(a->a_tvp, "VOP_RENAME"); 5272 ASSERT_VI_UNLOCKED(a->a_tdvp, "VOP_RENAME"); 5273 ASSERT_VI_UNLOCKED(a->a_fvp, "VOP_RENAME"); 5274 ASSERT_VI_UNLOCKED(a->a_fdvp, "VOP_RENAME"); 5275 5276 /* Check the source (from). */ 5277 if (a->a_tdvp->v_vnlock != a->a_fdvp->v_vnlock && 5278 (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fdvp->v_vnlock)) 5279 ASSERT_VOP_UNLOCKED(a->a_fdvp, "vop_rename: fdvp locked"); 5280 if (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fvp->v_vnlock) 5281 ASSERT_VOP_UNLOCKED(a->a_fvp, "vop_rename: fvp locked"); 5282 5283 /* Check the target. */ 5284 if (a->a_tvp) 5285 ASSERT_VOP_LOCKED(a->a_tvp, "vop_rename: tvp not locked"); 5286 ASSERT_VOP_LOCKED(a->a_tdvp, "vop_rename: tdvp not locked"); 5287 #endif 5288 if (a->a_tdvp != a->a_fdvp) 5289 vhold(a->a_fdvp); 5290 if (a->a_tvp != a->a_fvp) 5291 vhold(a->a_fvp); 5292 vhold(a->a_tdvp); 5293 if (a->a_tvp) 5294 vhold(a->a_tvp); 5295 } 5296 5297 #ifdef DEBUG_VFS_LOCKS 5298 void 5299 vop_strategy_pre(void *ap) 5300 { 5301 struct vop_strategy_args *a; 5302 struct buf *bp; 5303 5304 a = ap; 5305 bp = a->a_bp; 5306 5307 /* 5308 * Cluster ops lock their component buffers but not the IO container. 5309 */ 5310 if ((bp->b_flags & B_CLUSTER) != 0) 5311 return; 5312 5313 if (!KERNEL_PANICKED() && !BUF_ISLOCKED(bp)) { 5314 if (vfs_badlock_print) 5315 printf( 5316 "VOP_STRATEGY: bp is not locked but should be\n"); 5317 if (vfs_badlock_ddb) 5318 kdb_enter(KDB_WHY_VFSLOCK, "lock violation"); 5319 } 5320 } 5321 5322 void 5323 vop_lock_pre(void *ap) 5324 { 5325 struct vop_lock1_args *a = ap; 5326 5327 if ((a->a_flags & LK_INTERLOCK) == 0) 5328 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK"); 5329 else 5330 ASSERT_VI_LOCKED(a->a_vp, "VOP_LOCK"); 5331 } 5332 5333 void 5334 vop_lock_post(void *ap, int rc) 5335 { 5336 struct vop_lock1_args *a = ap; 5337 5338 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK"); 5339 if (rc == 0 && (a->a_flags & LK_EXCLOTHER) == 0) 5340 ASSERT_VOP_LOCKED(a->a_vp, "VOP_LOCK"); 5341 } 5342 5343 void 5344 vop_unlock_pre(void *ap) 5345 { 5346 struct vop_unlock_args *a = ap; 5347 5348 ASSERT_VOP_LOCKED(a->a_vp, "VOP_UNLOCK"); 5349 } 5350 5351 void 5352 vop_unlock_post(void *ap, int rc) 5353 { 5354 return; 5355 } 5356 5357 void 5358 vop_need_inactive_pre(void *ap) 5359 { 5360 struct vop_need_inactive_args *a = ap; 5361 5362 ASSERT_VI_LOCKED(a->a_vp, "VOP_NEED_INACTIVE"); 5363 } 5364 5365 void 5366 vop_need_inactive_post(void *ap, int rc) 5367 { 5368 struct vop_need_inactive_args *a = ap; 5369 5370 ASSERT_VI_LOCKED(a->a_vp, "VOP_NEED_INACTIVE"); 5371 } 5372 #endif 5373 5374 void 5375 vop_create_post(void *ap, int rc) 5376 { 5377 struct vop_create_args *a = ap; 5378 5379 if (!rc) 5380 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE); 5381 } 5382 5383 void 5384 vop_deleteextattr_post(void *ap, int rc) 5385 { 5386 struct vop_deleteextattr_args *a = ap; 5387 5388 if (!rc) 5389 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB); 5390 } 5391 5392 void 5393 vop_link_post(void *ap, int rc) 5394 { 5395 struct vop_link_args *a = ap; 5396 5397 if (!rc) { 5398 VFS_KNOTE_LOCKED(a->a_vp, NOTE_LINK); 5399 VFS_KNOTE_LOCKED(a->a_tdvp, NOTE_WRITE); 5400 } 5401 } 5402 5403 void 5404 vop_mkdir_post(void *ap, int rc) 5405 { 5406 struct vop_mkdir_args *a = ap; 5407 5408 if (!rc) 5409 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK); 5410 } 5411 5412 void 5413 vop_mknod_post(void *ap, int rc) 5414 { 5415 struct vop_mknod_args *a = ap; 5416 5417 if (!rc) 5418 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE); 5419 } 5420 5421 void 5422 vop_reclaim_post(void *ap, int rc) 5423 { 5424 struct vop_reclaim_args *a = ap; 5425 5426 if (!rc) 5427 VFS_KNOTE_LOCKED(a->a_vp, NOTE_REVOKE); 5428 } 5429 5430 void 5431 vop_remove_post(void *ap, int rc) 5432 { 5433 struct vop_remove_args *a = ap; 5434 5435 if (!rc) { 5436 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE); 5437 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE); 5438 } 5439 } 5440 5441 void 5442 vop_rename_post(void *ap, int rc) 5443 { 5444 struct vop_rename_args *a = ap; 5445 long hint; 5446 5447 if (!rc) { 5448 hint = NOTE_WRITE; 5449 if (a->a_fdvp == a->a_tdvp) { 5450 if (a->a_tvp != NULL && a->a_tvp->v_type == VDIR) 5451 hint |= NOTE_LINK; 5452 VFS_KNOTE_UNLOCKED(a->a_fdvp, hint); 5453 VFS_KNOTE_UNLOCKED(a->a_tdvp, hint); 5454 } else { 5455 hint |= NOTE_EXTEND; 5456 if (a->a_fvp->v_type == VDIR) 5457 hint |= NOTE_LINK; 5458 VFS_KNOTE_UNLOCKED(a->a_fdvp, hint); 5459 5460 if (a->a_fvp->v_type == VDIR && a->a_tvp != NULL && 5461 a->a_tvp->v_type == VDIR) 5462 hint &= ~NOTE_LINK; 5463 VFS_KNOTE_UNLOCKED(a->a_tdvp, hint); 5464 } 5465 5466 VFS_KNOTE_UNLOCKED(a->a_fvp, NOTE_RENAME); 5467 if (a->a_tvp) 5468 VFS_KNOTE_UNLOCKED(a->a_tvp, NOTE_DELETE); 5469 } 5470 if (a->a_tdvp != a->a_fdvp) 5471 vdrop(a->a_fdvp); 5472 if (a->a_tvp != a->a_fvp) 5473 vdrop(a->a_fvp); 5474 vdrop(a->a_tdvp); 5475 if (a->a_tvp) 5476 vdrop(a->a_tvp); 5477 } 5478 5479 void 5480 vop_rmdir_post(void *ap, int rc) 5481 { 5482 struct vop_rmdir_args *a = ap; 5483 5484 if (!rc) { 5485 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK); 5486 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE); 5487 } 5488 } 5489 5490 void 5491 vop_setattr_post(void *ap, int rc) 5492 { 5493 struct vop_setattr_args *a = ap; 5494 5495 if (!rc) 5496 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB); 5497 } 5498 5499 void 5500 vop_setextattr_post(void *ap, int rc) 5501 { 5502 struct vop_setextattr_args *a = ap; 5503 5504 if (!rc) 5505 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB); 5506 } 5507 5508 void 5509 vop_symlink_post(void *ap, int rc) 5510 { 5511 struct vop_symlink_args *a = ap; 5512 5513 if (!rc) 5514 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE); 5515 } 5516 5517 void 5518 vop_open_post(void *ap, int rc) 5519 { 5520 struct vop_open_args *a = ap; 5521 5522 if (!rc) 5523 VFS_KNOTE_LOCKED(a->a_vp, NOTE_OPEN); 5524 } 5525 5526 void 5527 vop_close_post(void *ap, int rc) 5528 { 5529 struct vop_close_args *a = ap; 5530 5531 if (!rc && (a->a_cred != NOCRED || /* filter out revokes */ 5532 !VN_IS_DOOMED(a->a_vp))) { 5533 VFS_KNOTE_LOCKED(a->a_vp, (a->a_fflag & FWRITE) != 0 ? 5534 NOTE_CLOSE_WRITE : NOTE_CLOSE); 5535 } 5536 } 5537 5538 void 5539 vop_read_post(void *ap, int rc) 5540 { 5541 struct vop_read_args *a = ap; 5542 5543 if (!rc) 5544 VFS_KNOTE_LOCKED(a->a_vp, NOTE_READ); 5545 } 5546 5547 void 5548 vop_readdir_post(void *ap, int rc) 5549 { 5550 struct vop_readdir_args *a = ap; 5551 5552 if (!rc) 5553 VFS_KNOTE_LOCKED(a->a_vp, NOTE_READ); 5554 } 5555 5556 static struct knlist fs_knlist; 5557 5558 static void 5559 vfs_event_init(void *arg) 5560 { 5561 knlist_init_mtx(&fs_knlist, NULL); 5562 } 5563 /* XXX - correct order? */ 5564 SYSINIT(vfs_knlist, SI_SUB_VFS, SI_ORDER_ANY, vfs_event_init, NULL); 5565 5566 void 5567 vfs_event_signal(fsid_t *fsid, uint32_t event, intptr_t data __unused) 5568 { 5569 5570 KNOTE_UNLOCKED(&fs_knlist, event); 5571 } 5572 5573 static int filt_fsattach(struct knote *kn); 5574 static void filt_fsdetach(struct knote *kn); 5575 static int filt_fsevent(struct knote *kn, long hint); 5576 5577 struct filterops fs_filtops = { 5578 .f_isfd = 0, 5579 .f_attach = filt_fsattach, 5580 .f_detach = filt_fsdetach, 5581 .f_event = filt_fsevent 5582 }; 5583 5584 static int 5585 filt_fsattach(struct knote *kn) 5586 { 5587 5588 kn->kn_flags |= EV_CLEAR; 5589 knlist_add(&fs_knlist, kn, 0); 5590 return (0); 5591 } 5592 5593 static void 5594 filt_fsdetach(struct knote *kn) 5595 { 5596 5597 knlist_remove(&fs_knlist, kn, 0); 5598 } 5599 5600 static int 5601 filt_fsevent(struct knote *kn, long hint) 5602 { 5603 5604 kn->kn_fflags |= hint; 5605 return (kn->kn_fflags != 0); 5606 } 5607 5608 static int 5609 sysctl_vfs_ctl(SYSCTL_HANDLER_ARGS) 5610 { 5611 struct vfsidctl vc; 5612 int error; 5613 struct mount *mp; 5614 5615 error = SYSCTL_IN(req, &vc, sizeof(vc)); 5616 if (error) 5617 return (error); 5618 if (vc.vc_vers != VFS_CTL_VERS1) 5619 return (EINVAL); 5620 mp = vfs_getvfs(&vc.vc_fsid); 5621 if (mp == NULL) 5622 return (ENOENT); 5623 /* ensure that a specific sysctl goes to the right filesystem. */ 5624 if (strcmp(vc.vc_fstypename, "*") != 0 && 5625 strcmp(vc.vc_fstypename, mp->mnt_vfc->vfc_name) != 0) { 5626 vfs_rel(mp); 5627 return (EINVAL); 5628 } 5629 VCTLTOREQ(&vc, req); 5630 error = VFS_SYSCTL(mp, vc.vc_op, req); 5631 vfs_rel(mp); 5632 return (error); 5633 } 5634 5635 SYSCTL_PROC(_vfs, OID_AUTO, ctl, CTLTYPE_OPAQUE | CTLFLAG_MPSAFE | CTLFLAG_WR, 5636 NULL, 0, sysctl_vfs_ctl, "", 5637 "Sysctl by fsid"); 5638 5639 /* 5640 * Function to initialize a va_filerev field sensibly. 5641 * XXX: Wouldn't a random number make a lot more sense ?? 5642 */ 5643 u_quad_t 5644 init_va_filerev(void) 5645 { 5646 struct bintime bt; 5647 5648 getbinuptime(&bt); 5649 return (((u_quad_t)bt.sec << 32LL) | (bt.frac >> 32LL)); 5650 } 5651 5652 static int filt_vfsread(struct knote *kn, long hint); 5653 static int filt_vfswrite(struct knote *kn, long hint); 5654 static int filt_vfsvnode(struct knote *kn, long hint); 5655 static void filt_vfsdetach(struct knote *kn); 5656 static struct filterops vfsread_filtops = { 5657 .f_isfd = 1, 5658 .f_detach = filt_vfsdetach, 5659 .f_event = filt_vfsread 5660 }; 5661 static struct filterops vfswrite_filtops = { 5662 .f_isfd = 1, 5663 .f_detach = filt_vfsdetach, 5664 .f_event = filt_vfswrite 5665 }; 5666 static struct filterops vfsvnode_filtops = { 5667 .f_isfd = 1, 5668 .f_detach = filt_vfsdetach, 5669 .f_event = filt_vfsvnode 5670 }; 5671 5672 static void 5673 vfs_knllock(void *arg) 5674 { 5675 struct vnode *vp = arg; 5676 5677 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 5678 } 5679 5680 static void 5681 vfs_knlunlock(void *arg) 5682 { 5683 struct vnode *vp = arg; 5684 5685 VOP_UNLOCK(vp); 5686 } 5687 5688 static void 5689 vfs_knl_assert_locked(void *arg) 5690 { 5691 #ifdef DEBUG_VFS_LOCKS 5692 struct vnode *vp = arg; 5693 5694 ASSERT_VOP_LOCKED(vp, "vfs_knl_assert_locked"); 5695 #endif 5696 } 5697 5698 static void 5699 vfs_knl_assert_unlocked(void *arg) 5700 { 5701 #ifdef DEBUG_VFS_LOCKS 5702 struct vnode *vp = arg; 5703 5704 ASSERT_VOP_UNLOCKED(vp, "vfs_knl_assert_unlocked"); 5705 #endif 5706 } 5707 5708 int 5709 vfs_kqfilter(struct vop_kqfilter_args *ap) 5710 { 5711 struct vnode *vp = ap->a_vp; 5712 struct knote *kn = ap->a_kn; 5713 struct knlist *knl; 5714 5715 switch (kn->kn_filter) { 5716 case EVFILT_READ: 5717 kn->kn_fop = &vfsread_filtops; 5718 break; 5719 case EVFILT_WRITE: 5720 kn->kn_fop = &vfswrite_filtops; 5721 break; 5722 case EVFILT_VNODE: 5723 kn->kn_fop = &vfsvnode_filtops; 5724 break; 5725 default: 5726 return (EINVAL); 5727 } 5728 5729 kn->kn_hook = (caddr_t)vp; 5730 5731 v_addpollinfo(vp); 5732 if (vp->v_pollinfo == NULL) 5733 return (ENOMEM); 5734 knl = &vp->v_pollinfo->vpi_selinfo.si_note; 5735 vhold(vp); 5736 knlist_add(knl, kn, 0); 5737 5738 return (0); 5739 } 5740 5741 /* 5742 * Detach knote from vnode 5743 */ 5744 static void 5745 filt_vfsdetach(struct knote *kn) 5746 { 5747 struct vnode *vp = (struct vnode *)kn->kn_hook; 5748 5749 KASSERT(vp->v_pollinfo != NULL, ("Missing v_pollinfo")); 5750 knlist_remove(&vp->v_pollinfo->vpi_selinfo.si_note, kn, 0); 5751 vdrop(vp); 5752 } 5753 5754 /*ARGSUSED*/ 5755 static int 5756 filt_vfsread(struct knote *kn, long hint) 5757 { 5758 struct vnode *vp = (struct vnode *)kn->kn_hook; 5759 struct vattr va; 5760 int res; 5761 5762 /* 5763 * filesystem is gone, so set the EOF flag and schedule 5764 * the knote for deletion. 5765 */ 5766 if (hint == NOTE_REVOKE || (hint == 0 && vp->v_type == VBAD)) { 5767 VI_LOCK(vp); 5768 kn->kn_flags |= (EV_EOF | EV_ONESHOT); 5769 VI_UNLOCK(vp); 5770 return (1); 5771 } 5772 5773 if (VOP_GETATTR(vp, &va, curthread->td_ucred)) 5774 return (0); 5775 5776 VI_LOCK(vp); 5777 kn->kn_data = va.va_size - kn->kn_fp->f_offset; 5778 res = (kn->kn_sfflags & NOTE_FILE_POLL) != 0 || kn->kn_data != 0; 5779 VI_UNLOCK(vp); 5780 return (res); 5781 } 5782 5783 /*ARGSUSED*/ 5784 static int 5785 filt_vfswrite(struct knote *kn, long hint) 5786 { 5787 struct vnode *vp = (struct vnode *)kn->kn_hook; 5788 5789 VI_LOCK(vp); 5790 5791 /* 5792 * filesystem is gone, so set the EOF flag and schedule 5793 * the knote for deletion. 5794 */ 5795 if (hint == NOTE_REVOKE || (hint == 0 && vp->v_type == VBAD)) 5796 kn->kn_flags |= (EV_EOF | EV_ONESHOT); 5797 5798 kn->kn_data = 0; 5799 VI_UNLOCK(vp); 5800 return (1); 5801 } 5802 5803 static int 5804 filt_vfsvnode(struct knote *kn, long hint) 5805 { 5806 struct vnode *vp = (struct vnode *)kn->kn_hook; 5807 int res; 5808 5809 VI_LOCK(vp); 5810 if (kn->kn_sfflags & hint) 5811 kn->kn_fflags |= hint; 5812 if (hint == NOTE_REVOKE || (hint == 0 && vp->v_type == VBAD)) { 5813 kn->kn_flags |= EV_EOF; 5814 VI_UNLOCK(vp); 5815 return (1); 5816 } 5817 res = (kn->kn_fflags != 0); 5818 VI_UNLOCK(vp); 5819 return (res); 5820 } 5821 5822 /* 5823 * Returns whether the directory is empty or not. 5824 * If it is empty, the return value is 0; otherwise 5825 * the return value is an error value (which may 5826 * be ENOTEMPTY). 5827 */ 5828 int 5829 vfs_emptydir(struct vnode *vp) 5830 { 5831 struct uio uio; 5832 struct iovec iov; 5833 struct dirent *dirent, *dp, *endp; 5834 int error, eof; 5835 5836 error = 0; 5837 eof = 0; 5838 5839 ASSERT_VOP_LOCKED(vp, "vfs_emptydir"); 5840 5841 dirent = malloc(sizeof(struct dirent), M_TEMP, M_WAITOK); 5842 iov.iov_base = dirent; 5843 iov.iov_len = sizeof(struct dirent); 5844 5845 uio.uio_iov = &iov; 5846 uio.uio_iovcnt = 1; 5847 uio.uio_offset = 0; 5848 uio.uio_resid = sizeof(struct dirent); 5849 uio.uio_segflg = UIO_SYSSPACE; 5850 uio.uio_rw = UIO_READ; 5851 uio.uio_td = curthread; 5852 5853 while (eof == 0 && error == 0) { 5854 error = VOP_READDIR(vp, &uio, curthread->td_ucred, &eof, 5855 NULL, NULL); 5856 if (error != 0) 5857 break; 5858 endp = (void *)((uint8_t *)dirent + 5859 sizeof(struct dirent) - uio.uio_resid); 5860 for (dp = dirent; dp < endp; 5861 dp = (void *)((uint8_t *)dp + GENERIC_DIRSIZ(dp))) { 5862 if (dp->d_type == DT_WHT) 5863 continue; 5864 if (dp->d_namlen == 0) 5865 continue; 5866 if (dp->d_type != DT_DIR && 5867 dp->d_type != DT_UNKNOWN) { 5868 error = ENOTEMPTY; 5869 break; 5870 } 5871 if (dp->d_namlen > 2) { 5872 error = ENOTEMPTY; 5873 break; 5874 } 5875 if (dp->d_namlen == 1 && 5876 dp->d_name[0] != '.') { 5877 error = ENOTEMPTY; 5878 break; 5879 } 5880 if (dp->d_namlen == 2 && 5881 dp->d_name[1] != '.') { 5882 error = ENOTEMPTY; 5883 break; 5884 } 5885 uio.uio_resid = sizeof(struct dirent); 5886 } 5887 } 5888 free(dirent, M_TEMP); 5889 return (error); 5890 } 5891 5892 int 5893 vfs_read_dirent(struct vop_readdir_args *ap, struct dirent *dp, off_t off) 5894 { 5895 int error; 5896 5897 if (dp->d_reclen > ap->a_uio->uio_resid) 5898 return (ENAMETOOLONG); 5899 error = uiomove(dp, dp->d_reclen, ap->a_uio); 5900 if (error) { 5901 if (ap->a_ncookies != NULL) { 5902 if (ap->a_cookies != NULL) 5903 free(ap->a_cookies, M_TEMP); 5904 ap->a_cookies = NULL; 5905 *ap->a_ncookies = 0; 5906 } 5907 return (error); 5908 } 5909 if (ap->a_ncookies == NULL) 5910 return (0); 5911 5912 KASSERT(ap->a_cookies, 5913 ("NULL ap->a_cookies value with non-NULL ap->a_ncookies!")); 5914 5915 *ap->a_cookies = realloc(*ap->a_cookies, 5916 (*ap->a_ncookies + 1) * sizeof(u_long), M_TEMP, M_WAITOK | M_ZERO); 5917 (*ap->a_cookies)[*ap->a_ncookies] = off; 5918 *ap->a_ncookies += 1; 5919 return (0); 5920 } 5921 5922 /* 5923 * Mark for update the access time of the file if the filesystem 5924 * supports VOP_MARKATIME. This functionality is used by execve and 5925 * mmap, so we want to avoid the I/O implied by directly setting 5926 * va_atime for the sake of efficiency. 5927 */ 5928 void 5929 vfs_mark_atime(struct vnode *vp, struct ucred *cred) 5930 { 5931 struct mount *mp; 5932 5933 mp = vp->v_mount; 5934 ASSERT_VOP_LOCKED(vp, "vfs_mark_atime"); 5935 if (mp != NULL && (mp->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0) 5936 (void)VOP_MARKATIME(vp); 5937 } 5938 5939 /* 5940 * The purpose of this routine is to remove granularity from accmode_t, 5941 * reducing it into standard unix access bits - VEXEC, VREAD, VWRITE, 5942 * VADMIN and VAPPEND. 5943 * 5944 * If it returns 0, the caller is supposed to continue with the usual 5945 * access checks using 'accmode' as modified by this routine. If it 5946 * returns nonzero value, the caller is supposed to return that value 5947 * as errno. 5948 * 5949 * Note that after this routine runs, accmode may be zero. 5950 */ 5951 int 5952 vfs_unixify_accmode(accmode_t *accmode) 5953 { 5954 /* 5955 * There is no way to specify explicit "deny" rule using 5956 * file mode or POSIX.1e ACLs. 5957 */ 5958 if (*accmode & VEXPLICIT_DENY) { 5959 *accmode = 0; 5960 return (0); 5961 } 5962 5963 /* 5964 * None of these can be translated into usual access bits. 5965 * Also, the common case for NFSv4 ACLs is to not contain 5966 * either of these bits. Caller should check for VWRITE 5967 * on the containing directory instead. 5968 */ 5969 if (*accmode & (VDELETE_CHILD | VDELETE)) 5970 return (EPERM); 5971 5972 if (*accmode & VADMIN_PERMS) { 5973 *accmode &= ~VADMIN_PERMS; 5974 *accmode |= VADMIN; 5975 } 5976 5977 /* 5978 * There is no way to deny VREAD_ATTRIBUTES, VREAD_ACL 5979 * or VSYNCHRONIZE using file mode or POSIX.1e ACL. 5980 */ 5981 *accmode &= ~(VSTAT_PERMS | VSYNCHRONIZE); 5982 5983 return (0); 5984 } 5985 5986 /* 5987 * Clear out a doomed vnode (if any) and replace it with a new one as long 5988 * as the fs is not being unmounted. Return the root vnode to the caller. 5989 */ 5990 static int __noinline 5991 vfs_cache_root_fallback(struct mount *mp, int flags, struct vnode **vpp) 5992 { 5993 struct vnode *vp; 5994 int error; 5995 5996 restart: 5997 if (mp->mnt_rootvnode != NULL) { 5998 MNT_ILOCK(mp); 5999 vp = mp->mnt_rootvnode; 6000 if (vp != NULL) { 6001 if (!VN_IS_DOOMED(vp)) { 6002 vrefact(vp); 6003 MNT_IUNLOCK(mp); 6004 error = vn_lock(vp, flags); 6005 if (error == 0) { 6006 *vpp = vp; 6007 return (0); 6008 } 6009 vrele(vp); 6010 goto restart; 6011 } 6012 /* 6013 * Clear the old one. 6014 */ 6015 mp->mnt_rootvnode = NULL; 6016 } 6017 MNT_IUNLOCK(mp); 6018 if (vp != NULL) { 6019 /* 6020 * Paired with a fence in vfs_op_thread_exit(). 6021 */ 6022 atomic_thread_fence_acq(); 6023 vfs_op_barrier_wait(mp); 6024 vrele(vp); 6025 } 6026 } 6027 error = VFS_CACHEDROOT(mp, flags, vpp); 6028 if (error != 0) 6029 return (error); 6030 if (mp->mnt_vfs_ops == 0) { 6031 MNT_ILOCK(mp); 6032 if (mp->mnt_vfs_ops != 0) { 6033 MNT_IUNLOCK(mp); 6034 return (0); 6035 } 6036 if (mp->mnt_rootvnode == NULL) { 6037 vrefact(*vpp); 6038 mp->mnt_rootvnode = *vpp; 6039 } else { 6040 if (mp->mnt_rootvnode != *vpp) { 6041 if (!VN_IS_DOOMED(mp->mnt_rootvnode)) { 6042 panic("%s: mismatch between vnode returned " 6043 " by VFS_CACHEDROOT and the one cached " 6044 " (%p != %p)", 6045 __func__, *vpp, mp->mnt_rootvnode); 6046 } 6047 } 6048 } 6049 MNT_IUNLOCK(mp); 6050 } 6051 return (0); 6052 } 6053 6054 int 6055 vfs_cache_root(struct mount *mp, int flags, struct vnode **vpp) 6056 { 6057 struct vnode *vp; 6058 int error; 6059 6060 if (!vfs_op_thread_enter(mp)) 6061 return (vfs_cache_root_fallback(mp, flags, vpp)); 6062 vp = (struct vnode *)atomic_load_ptr(&mp->mnt_rootvnode); 6063 if (vp == NULL || VN_IS_DOOMED(vp)) { 6064 vfs_op_thread_exit(mp); 6065 return (vfs_cache_root_fallback(mp, flags, vpp)); 6066 } 6067 vrefact(vp); 6068 vfs_op_thread_exit(mp); 6069 error = vn_lock(vp, flags); 6070 if (error != 0) { 6071 vrele(vp); 6072 return (vfs_cache_root_fallback(mp, flags, vpp)); 6073 } 6074 *vpp = vp; 6075 return (0); 6076 } 6077 6078 struct vnode * 6079 vfs_cache_root_clear(struct mount *mp) 6080 { 6081 struct vnode *vp; 6082 6083 /* 6084 * ops > 0 guarantees there is nobody who can see this vnode 6085 */ 6086 MPASS(mp->mnt_vfs_ops > 0); 6087 vp = mp->mnt_rootvnode; 6088 mp->mnt_rootvnode = NULL; 6089 return (vp); 6090 } 6091 6092 void 6093 vfs_cache_root_set(struct mount *mp, struct vnode *vp) 6094 { 6095 6096 MPASS(mp->mnt_vfs_ops > 0); 6097 vrefact(vp); 6098 mp->mnt_rootvnode = vp; 6099 } 6100 6101 /* 6102 * These are helper functions for filesystems to traverse all 6103 * their vnodes. See MNT_VNODE_FOREACH_ALL() in sys/mount.h. 6104 * 6105 * This interface replaces MNT_VNODE_FOREACH. 6106 */ 6107 6108 6109 struct vnode * 6110 __mnt_vnode_next_all(struct vnode **mvp, struct mount *mp) 6111 { 6112 struct vnode *vp; 6113 6114 if (should_yield()) 6115 kern_yield(PRI_USER); 6116 MNT_ILOCK(mp); 6117 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch")); 6118 for (vp = TAILQ_NEXT(*mvp, v_nmntvnodes); vp != NULL; 6119 vp = TAILQ_NEXT(vp, v_nmntvnodes)) { 6120 /* Allow a racy peek at VIRF_DOOMED to save a lock acquisition. */ 6121 if (vp->v_type == VMARKER || VN_IS_DOOMED(vp)) 6122 continue; 6123 VI_LOCK(vp); 6124 if (VN_IS_DOOMED(vp)) { 6125 VI_UNLOCK(vp); 6126 continue; 6127 } 6128 break; 6129 } 6130 if (vp == NULL) { 6131 __mnt_vnode_markerfree_all(mvp, mp); 6132 /* MNT_IUNLOCK(mp); -- done in above function */ 6133 mtx_assert(MNT_MTX(mp), MA_NOTOWNED); 6134 return (NULL); 6135 } 6136 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes); 6137 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes); 6138 MNT_IUNLOCK(mp); 6139 return (vp); 6140 } 6141 6142 struct vnode * 6143 __mnt_vnode_first_all(struct vnode **mvp, struct mount *mp) 6144 { 6145 struct vnode *vp; 6146 6147 *mvp = vn_alloc_marker(mp); 6148 MNT_ILOCK(mp); 6149 MNT_REF(mp); 6150 6151 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) { 6152 /* Allow a racy peek at VIRF_DOOMED to save a lock acquisition. */ 6153 if (vp->v_type == VMARKER || VN_IS_DOOMED(vp)) 6154 continue; 6155 VI_LOCK(vp); 6156 if (VN_IS_DOOMED(vp)) { 6157 VI_UNLOCK(vp); 6158 continue; 6159 } 6160 break; 6161 } 6162 if (vp == NULL) { 6163 MNT_REL(mp); 6164 MNT_IUNLOCK(mp); 6165 vn_free_marker(*mvp); 6166 *mvp = NULL; 6167 return (NULL); 6168 } 6169 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes); 6170 MNT_IUNLOCK(mp); 6171 return (vp); 6172 } 6173 6174 void 6175 __mnt_vnode_markerfree_all(struct vnode **mvp, struct mount *mp) 6176 { 6177 6178 if (*mvp == NULL) { 6179 MNT_IUNLOCK(mp); 6180 return; 6181 } 6182 6183 mtx_assert(MNT_MTX(mp), MA_OWNED); 6184 6185 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch")); 6186 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes); 6187 MNT_REL(mp); 6188 MNT_IUNLOCK(mp); 6189 vn_free_marker(*mvp); 6190 *mvp = NULL; 6191 } 6192 6193 /* 6194 * These are helper functions for filesystems to traverse their 6195 * lazy vnodes. See MNT_VNODE_FOREACH_LAZY() in sys/mount.h 6196 */ 6197 static void 6198 mnt_vnode_markerfree_lazy(struct vnode **mvp, struct mount *mp) 6199 { 6200 6201 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch")); 6202 6203 MNT_ILOCK(mp); 6204 MNT_REL(mp); 6205 MNT_IUNLOCK(mp); 6206 vn_free_marker(*mvp); 6207 *mvp = NULL; 6208 } 6209 6210 /* 6211 * Relock the mp mount vnode list lock with the vp vnode interlock in the 6212 * conventional lock order during mnt_vnode_next_lazy iteration. 6213 * 6214 * On entry, the mount vnode list lock is held and the vnode interlock is not. 6215 * The list lock is dropped and reacquired. On success, both locks are held. 6216 * On failure, the mount vnode list lock is held but the vnode interlock is 6217 * not, and the procedure may have yielded. 6218 */ 6219 static bool 6220 mnt_vnode_next_lazy_relock(struct vnode *mvp, struct mount *mp, 6221 struct vnode *vp) 6222 { 6223 const struct vnode *tmp; 6224 bool held, ret; 6225 6226 VNASSERT(mvp->v_mount == mp && mvp->v_type == VMARKER && 6227 TAILQ_NEXT(mvp, v_lazylist) != NULL, mvp, 6228 ("%s: bad marker", __func__)); 6229 VNASSERT(vp->v_mount == mp && vp->v_type != VMARKER, vp, 6230 ("%s: inappropriate vnode", __func__)); 6231 ASSERT_VI_UNLOCKED(vp, __func__); 6232 mtx_assert(&mp->mnt_listmtx, MA_OWNED); 6233 6234 ret = false; 6235 6236 TAILQ_REMOVE(&mp->mnt_lazyvnodelist, mvp, v_lazylist); 6237 TAILQ_INSERT_BEFORE(vp, mvp, v_lazylist); 6238 6239 /* 6240 * Use a hold to prevent vp from disappearing while the mount vnode 6241 * list lock is dropped and reacquired. Normally a hold would be 6242 * acquired with vhold(), but that might try to acquire the vnode 6243 * interlock, which would be a LOR with the mount vnode list lock. 6244 */ 6245 held = refcount_acquire_if_not_zero(&vp->v_holdcnt); 6246 mtx_unlock(&mp->mnt_listmtx); 6247 if (!held) 6248 goto abort; 6249 VI_LOCK(vp); 6250 if (!refcount_release_if_not_last(&vp->v_holdcnt)) { 6251 vdropl(vp); 6252 goto abort; 6253 } 6254 mtx_lock(&mp->mnt_listmtx); 6255 6256 /* 6257 * Determine whether the vnode is still the next one after the marker, 6258 * excepting any other markers. If the vnode has not been doomed by 6259 * vgone() then the hold should have ensured that it remained on the 6260 * lazy list. If it has been doomed but is still on the lazy list, 6261 * don't abort, but rather skip over it (avoid spinning on doomed 6262 * vnodes). 6263 */ 6264 tmp = mvp; 6265 do { 6266 tmp = TAILQ_NEXT(tmp, v_lazylist); 6267 } while (tmp != NULL && tmp->v_type == VMARKER); 6268 if (tmp != vp) { 6269 mtx_unlock(&mp->mnt_listmtx); 6270 VI_UNLOCK(vp); 6271 goto abort; 6272 } 6273 6274 ret = true; 6275 goto out; 6276 abort: 6277 maybe_yield(); 6278 mtx_lock(&mp->mnt_listmtx); 6279 out: 6280 if (ret) 6281 ASSERT_VI_LOCKED(vp, __func__); 6282 else 6283 ASSERT_VI_UNLOCKED(vp, __func__); 6284 mtx_assert(&mp->mnt_listmtx, MA_OWNED); 6285 return (ret); 6286 } 6287 6288 static struct vnode * 6289 mnt_vnode_next_lazy(struct vnode **mvp, struct mount *mp, mnt_lazy_cb_t *cb, 6290 void *cbarg) 6291 { 6292 struct vnode *vp, *nvp; 6293 6294 mtx_assert(&mp->mnt_listmtx, MA_OWNED); 6295 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch")); 6296 restart: 6297 vp = TAILQ_NEXT(*mvp, v_lazylist); 6298 while (vp != NULL) { 6299 if (vp->v_type == VMARKER) { 6300 vp = TAILQ_NEXT(vp, v_lazylist); 6301 continue; 6302 } 6303 /* 6304 * See if we want to process the vnode. Note we may encounter a 6305 * long string of vnodes we don't care about and hog the list 6306 * as a result. Check for it and requeue the marker. 6307 */ 6308 if (VN_IS_DOOMED(vp) || !cb(vp, cbarg)) { 6309 if (!should_yield()) { 6310 vp = TAILQ_NEXT(vp, v_lazylist); 6311 continue; 6312 } 6313 TAILQ_REMOVE(&mp->mnt_lazyvnodelist, *mvp, 6314 v_lazylist); 6315 TAILQ_INSERT_AFTER(&mp->mnt_lazyvnodelist, vp, *mvp, 6316 v_lazylist); 6317 mtx_unlock(&mp->mnt_listmtx); 6318 kern_yield(PRI_USER); 6319 mtx_lock(&mp->mnt_listmtx); 6320 goto restart; 6321 } 6322 /* 6323 * Try-lock because this is the wrong lock order. If that does 6324 * not succeed, drop the mount vnode list lock and try to 6325 * reacquire it and the vnode interlock in the right order. 6326 */ 6327 if (!VI_TRYLOCK(vp) && 6328 !mnt_vnode_next_lazy_relock(*mvp, mp, vp)) 6329 goto restart; 6330 KASSERT(vp->v_type != VMARKER, ("locked marker %p", vp)); 6331 KASSERT(vp->v_mount == mp || vp->v_mount == NULL, 6332 ("alien vnode on the lazy list %p %p", vp, mp)); 6333 if (vp->v_mount == mp && !VN_IS_DOOMED(vp)) 6334 break; 6335 nvp = TAILQ_NEXT(vp, v_lazylist); 6336 VI_UNLOCK(vp); 6337 vp = nvp; 6338 } 6339 TAILQ_REMOVE(&mp->mnt_lazyvnodelist, *mvp, v_lazylist); 6340 6341 /* Check if we are done */ 6342 if (vp == NULL) { 6343 mtx_unlock(&mp->mnt_listmtx); 6344 mnt_vnode_markerfree_lazy(mvp, mp); 6345 return (NULL); 6346 } 6347 TAILQ_INSERT_AFTER(&mp->mnt_lazyvnodelist, vp, *mvp, v_lazylist); 6348 mtx_unlock(&mp->mnt_listmtx); 6349 ASSERT_VI_LOCKED(vp, "lazy iter"); 6350 return (vp); 6351 } 6352 6353 struct vnode * 6354 __mnt_vnode_next_lazy(struct vnode **mvp, struct mount *mp, mnt_lazy_cb_t *cb, 6355 void *cbarg) 6356 { 6357 6358 if (should_yield()) 6359 kern_yield(PRI_USER); 6360 mtx_lock(&mp->mnt_listmtx); 6361 return (mnt_vnode_next_lazy(mvp, mp, cb, cbarg)); 6362 } 6363 6364 struct vnode * 6365 __mnt_vnode_first_lazy(struct vnode **mvp, struct mount *mp, mnt_lazy_cb_t *cb, 6366 void *cbarg) 6367 { 6368 struct vnode *vp; 6369 6370 *mvp = vn_alloc_marker(mp); 6371 MNT_ILOCK(mp); 6372 MNT_REF(mp); 6373 MNT_IUNLOCK(mp); 6374 6375 mtx_lock(&mp->mnt_listmtx); 6376 vp = TAILQ_FIRST(&mp->mnt_lazyvnodelist); 6377 if (vp == NULL) { 6378 mtx_unlock(&mp->mnt_listmtx); 6379 mnt_vnode_markerfree_lazy(mvp, mp); 6380 return (NULL); 6381 } 6382 TAILQ_INSERT_BEFORE(vp, *mvp, v_lazylist); 6383 return (mnt_vnode_next_lazy(mvp, mp, cb, cbarg)); 6384 } 6385 6386 void 6387 __mnt_vnode_markerfree_lazy(struct vnode **mvp, struct mount *mp) 6388 { 6389 6390 if (*mvp == NULL) 6391 return; 6392 6393 mtx_lock(&mp->mnt_listmtx); 6394 TAILQ_REMOVE(&mp->mnt_lazyvnodelist, *mvp, v_lazylist); 6395 mtx_unlock(&mp->mnt_listmtx); 6396 mnt_vnode_markerfree_lazy(mvp, mp); 6397 } 6398