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 if (lo->lo_name != tag) { 1673 lo->lo_name = tag; 1674 WITNESS_DESTROY(lo); 1675 WITNESS_INIT(lo, tag); 1676 } 1677 /* 1678 * By default, don't allow shared locks unless filesystems opt-in. 1679 */ 1680 vp->v_vnlock->lock_object.lo_flags |= LK_NOSHARE; 1681 /* 1682 * Finalize various vnode identity bits. 1683 */ 1684 KASSERT(vp->v_object == NULL, ("stale v_object %p", vp)); 1685 KASSERT(vp->v_lockf == NULL, ("stale v_lockf %p", vp)); 1686 KASSERT(vp->v_pollinfo == NULL, ("stale v_pollinfo %p", vp)); 1687 vp->v_type = VNON; 1688 vp->v_op = vops; 1689 v_init_counters(vp); 1690 vp->v_bufobj.bo_ops = &buf_ops_bio; 1691 #ifdef DIAGNOSTIC 1692 if (mp == NULL && vops != &dead_vnodeops) 1693 printf("NULL mp in getnewvnode(9), tag %s\n", tag); 1694 #endif 1695 #ifdef MAC 1696 mac_vnode_init(vp); 1697 if (mp != NULL && (mp->mnt_flag & MNT_MULTILABEL) == 0) 1698 mac_vnode_associate_singlelabel(mp, vp); 1699 #endif 1700 if (mp != NULL) { 1701 vp->v_bufobj.bo_bsize = mp->mnt_stat.f_iosize; 1702 if ((mp->mnt_kern_flag & MNTK_NOKNOTE) != 0) 1703 vp->v_vflag |= VV_NOKNOTE; 1704 } 1705 1706 /* 1707 * For the filesystems which do not use vfs_hash_insert(), 1708 * still initialize v_hash to have vfs_hash_index() useful. 1709 * E.g., nullfs uses vfs_hash_index() on the lower vnode for 1710 * its own hashing. 1711 */ 1712 vp->v_hash = (uintptr_t)vp >> vnsz2log; 1713 1714 *vpp = vp; 1715 return (0); 1716 } 1717 1718 void 1719 getnewvnode_reserve(void) 1720 { 1721 struct thread *td; 1722 1723 td = curthread; 1724 MPASS(td->td_vp_reserved == NULL); 1725 td->td_vp_reserved = vn_alloc(NULL); 1726 } 1727 1728 void 1729 getnewvnode_drop_reserve(void) 1730 { 1731 struct thread *td; 1732 1733 td = curthread; 1734 if (td->td_vp_reserved != NULL) { 1735 vn_free(td->td_vp_reserved); 1736 td->td_vp_reserved = NULL; 1737 } 1738 } 1739 1740 static void 1741 freevnode(struct vnode *vp) 1742 { 1743 struct bufobj *bo; 1744 1745 /* 1746 * The vnode has been marked for destruction, so free it. 1747 * 1748 * The vnode will be returned to the zone where it will 1749 * normally remain until it is needed for another vnode. We 1750 * need to cleanup (or verify that the cleanup has already 1751 * been done) any residual data left from its current use 1752 * so as not to contaminate the freshly allocated vnode. 1753 */ 1754 CTR2(KTR_VFS, "%s: destroying the vnode %p", __func__, vp); 1755 bo = &vp->v_bufobj; 1756 VNASSERT(vp->v_data == NULL, vp, ("cleaned vnode isn't")); 1757 VNASSERT(vp->v_holdcnt == 0, vp, ("Non-zero hold count")); 1758 VNASSERT(vp->v_usecount == 0, vp, ("Non-zero use count")); 1759 VNASSERT(vp->v_writecount == 0, vp, ("Non-zero write count")); 1760 VNASSERT(bo->bo_numoutput == 0, vp, ("Clean vnode has pending I/O's")); 1761 VNASSERT(bo->bo_clean.bv_cnt == 0, vp, ("cleanbufcnt not 0")); 1762 VNASSERT(pctrie_is_empty(&bo->bo_clean.bv_root), vp, 1763 ("clean blk trie not empty")); 1764 VNASSERT(bo->bo_dirty.bv_cnt == 0, vp, ("dirtybufcnt not 0")); 1765 VNASSERT(pctrie_is_empty(&bo->bo_dirty.bv_root), vp, 1766 ("dirty blk trie not empty")); 1767 VNASSERT(TAILQ_EMPTY(&vp->v_cache_dst), vp, ("vp has namecache dst")); 1768 VNASSERT(LIST_EMPTY(&vp->v_cache_src), vp, ("vp has namecache src")); 1769 VNASSERT(vp->v_cache_dd == NULL, vp, ("vp has namecache for ..")); 1770 VNASSERT(TAILQ_EMPTY(&vp->v_rl.rl_waiters), vp, 1771 ("Dangling rangelock waiters")); 1772 VI_UNLOCK(vp); 1773 #ifdef MAC 1774 mac_vnode_destroy(vp); 1775 #endif 1776 if (vp->v_pollinfo != NULL) { 1777 destroy_vpollinfo(vp->v_pollinfo); 1778 vp->v_pollinfo = NULL; 1779 } 1780 #ifdef INVARIANTS 1781 /* XXX Elsewhere we detect an already freed vnode via NULL v_op. */ 1782 vp->v_op = NULL; 1783 #endif 1784 vp->v_mountedhere = NULL; 1785 vp->v_unpcb = NULL; 1786 vp->v_rdev = NULL; 1787 vp->v_fifoinfo = NULL; 1788 vp->v_lasta = vp->v_clen = vp->v_cstart = vp->v_lastw = 0; 1789 vp->v_irflag = 0; 1790 vp->v_iflag = 0; 1791 vp->v_vflag = 0; 1792 bo->bo_flag = 0; 1793 vn_free(vp); 1794 } 1795 1796 /* 1797 * Delete from old mount point vnode list, if on one. 1798 */ 1799 static void 1800 delmntque(struct vnode *vp) 1801 { 1802 struct mount *mp; 1803 1804 mp = vp->v_mount; 1805 if (mp == NULL) 1806 return; 1807 MNT_ILOCK(mp); 1808 VI_LOCK(vp); 1809 if (vp->v_mflag & VMP_LAZYLIST) { 1810 mtx_lock(&mp->mnt_listmtx); 1811 if (vp->v_mflag & VMP_LAZYLIST) { 1812 vp->v_mflag &= ~VMP_LAZYLIST; 1813 TAILQ_REMOVE(&mp->mnt_lazyvnodelist, vp, v_lazylist); 1814 mp->mnt_lazyvnodelistsize--; 1815 } 1816 mtx_unlock(&mp->mnt_listmtx); 1817 } 1818 vp->v_mount = NULL; 1819 VI_UNLOCK(vp); 1820 VNASSERT(mp->mnt_nvnodelistsize > 0, vp, 1821 ("bad mount point vnode list size")); 1822 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes); 1823 mp->mnt_nvnodelistsize--; 1824 MNT_REL(mp); 1825 MNT_IUNLOCK(mp); 1826 } 1827 1828 static void 1829 insmntque_stddtr(struct vnode *vp, void *dtr_arg) 1830 { 1831 1832 vp->v_data = NULL; 1833 vp->v_op = &dead_vnodeops; 1834 vgone(vp); 1835 vput(vp); 1836 } 1837 1838 /* 1839 * Insert into list of vnodes for the new mount point, if available. 1840 */ 1841 int 1842 insmntque1(struct vnode *vp, struct mount *mp, 1843 void (*dtr)(struct vnode *, void *), void *dtr_arg) 1844 { 1845 1846 KASSERT(vp->v_mount == NULL, 1847 ("insmntque: vnode already on per mount vnode list")); 1848 VNASSERT(mp != NULL, vp, ("Don't call insmntque(foo, NULL)")); 1849 ASSERT_VOP_ELOCKED(vp, "insmntque: non-locked vp"); 1850 1851 /* 1852 * We acquire the vnode interlock early to ensure that the 1853 * vnode cannot be recycled by another process releasing a 1854 * holdcnt on it before we get it on both the vnode list 1855 * and the active vnode list. The mount mutex protects only 1856 * manipulation of the vnode list and the vnode freelist 1857 * mutex protects only manipulation of the active vnode list. 1858 * Hence the need to hold the vnode interlock throughout. 1859 */ 1860 MNT_ILOCK(mp); 1861 VI_LOCK(vp); 1862 if (((mp->mnt_kern_flag & MNTK_UNMOUNT) != 0 && 1863 ((mp->mnt_kern_flag & MNTK_UNMOUNTF) != 0 || 1864 mp->mnt_nvnodelistsize == 0)) && 1865 (vp->v_vflag & VV_FORCEINSMQ) == 0) { 1866 VI_UNLOCK(vp); 1867 MNT_IUNLOCK(mp); 1868 if (dtr != NULL) 1869 dtr(vp, dtr_arg); 1870 return (EBUSY); 1871 } 1872 vp->v_mount = mp; 1873 MNT_REF(mp); 1874 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes); 1875 VNASSERT(mp->mnt_nvnodelistsize >= 0, vp, 1876 ("neg mount point vnode list size")); 1877 mp->mnt_nvnodelistsize++; 1878 VI_UNLOCK(vp); 1879 MNT_IUNLOCK(mp); 1880 return (0); 1881 } 1882 1883 int 1884 insmntque(struct vnode *vp, struct mount *mp) 1885 { 1886 1887 return (insmntque1(vp, mp, insmntque_stddtr, NULL)); 1888 } 1889 1890 /* 1891 * Flush out and invalidate all buffers associated with a bufobj 1892 * Called with the underlying object locked. 1893 */ 1894 int 1895 bufobj_invalbuf(struct bufobj *bo, int flags, int slpflag, int slptimeo) 1896 { 1897 int error; 1898 1899 BO_LOCK(bo); 1900 if (flags & V_SAVE) { 1901 error = bufobj_wwait(bo, slpflag, slptimeo); 1902 if (error) { 1903 BO_UNLOCK(bo); 1904 return (error); 1905 } 1906 if (bo->bo_dirty.bv_cnt > 0) { 1907 BO_UNLOCK(bo); 1908 if ((error = BO_SYNC(bo, MNT_WAIT)) != 0) 1909 return (error); 1910 /* 1911 * XXX We could save a lock/unlock if this was only 1912 * enabled under INVARIANTS 1913 */ 1914 BO_LOCK(bo); 1915 if (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0) 1916 panic("vinvalbuf: dirty bufs"); 1917 } 1918 } 1919 /* 1920 * If you alter this loop please notice that interlock is dropped and 1921 * reacquired in flushbuflist. Special care is needed to ensure that 1922 * no race conditions occur from this. 1923 */ 1924 do { 1925 error = flushbuflist(&bo->bo_clean, 1926 flags, bo, slpflag, slptimeo); 1927 if (error == 0 && !(flags & V_CLEANONLY)) 1928 error = flushbuflist(&bo->bo_dirty, 1929 flags, bo, slpflag, slptimeo); 1930 if (error != 0 && error != EAGAIN) { 1931 BO_UNLOCK(bo); 1932 return (error); 1933 } 1934 } while (error != 0); 1935 1936 /* 1937 * Wait for I/O to complete. XXX needs cleaning up. The vnode can 1938 * have write I/O in-progress but if there is a VM object then the 1939 * VM object can also have read-I/O in-progress. 1940 */ 1941 do { 1942 bufobj_wwait(bo, 0, 0); 1943 if ((flags & V_VMIO) == 0 && bo->bo_object != NULL) { 1944 BO_UNLOCK(bo); 1945 vm_object_pip_wait_unlocked(bo->bo_object, "bovlbx"); 1946 BO_LOCK(bo); 1947 } 1948 } while (bo->bo_numoutput > 0); 1949 BO_UNLOCK(bo); 1950 1951 /* 1952 * Destroy the copy in the VM cache, too. 1953 */ 1954 if (bo->bo_object != NULL && 1955 (flags & (V_ALT | V_NORMAL | V_CLEANONLY | V_VMIO)) == 0) { 1956 VM_OBJECT_WLOCK(bo->bo_object); 1957 vm_object_page_remove(bo->bo_object, 0, 0, (flags & V_SAVE) ? 1958 OBJPR_CLEANONLY : 0); 1959 VM_OBJECT_WUNLOCK(bo->bo_object); 1960 } 1961 1962 #ifdef INVARIANTS 1963 BO_LOCK(bo); 1964 if ((flags & (V_ALT | V_NORMAL | V_CLEANONLY | V_VMIO | 1965 V_ALLOWCLEAN)) == 0 && (bo->bo_dirty.bv_cnt > 0 || 1966 bo->bo_clean.bv_cnt > 0)) 1967 panic("vinvalbuf: flush failed"); 1968 if ((flags & (V_ALT | V_NORMAL | V_CLEANONLY | V_VMIO)) == 0 && 1969 bo->bo_dirty.bv_cnt > 0) 1970 panic("vinvalbuf: flush dirty failed"); 1971 BO_UNLOCK(bo); 1972 #endif 1973 return (0); 1974 } 1975 1976 /* 1977 * Flush out and invalidate all buffers associated with a vnode. 1978 * Called with the underlying object locked. 1979 */ 1980 int 1981 vinvalbuf(struct vnode *vp, int flags, int slpflag, int slptimeo) 1982 { 1983 1984 CTR3(KTR_VFS, "%s: vp %p with flags %d", __func__, vp, flags); 1985 ASSERT_VOP_LOCKED(vp, "vinvalbuf"); 1986 if (vp->v_object != NULL && vp->v_object->handle != vp) 1987 return (0); 1988 return (bufobj_invalbuf(&vp->v_bufobj, flags, slpflag, slptimeo)); 1989 } 1990 1991 /* 1992 * Flush out buffers on the specified list. 1993 * 1994 */ 1995 static int 1996 flushbuflist(struct bufv *bufv, int flags, struct bufobj *bo, int slpflag, 1997 int slptimeo) 1998 { 1999 struct buf *bp, *nbp; 2000 int retval, error; 2001 daddr_t lblkno; 2002 b_xflags_t xflags; 2003 2004 ASSERT_BO_WLOCKED(bo); 2005 2006 retval = 0; 2007 TAILQ_FOREACH_SAFE(bp, &bufv->bv_hd, b_bobufs, nbp) { 2008 /* 2009 * If we are flushing both V_NORMAL and V_ALT buffers then 2010 * do not skip any buffers. If we are flushing only V_NORMAL 2011 * buffers then skip buffers marked as BX_ALTDATA. If we are 2012 * flushing only V_ALT buffers then skip buffers not marked 2013 * as BX_ALTDATA. 2014 */ 2015 if (((flags & (V_NORMAL | V_ALT)) != (V_NORMAL | V_ALT)) && 2016 (((flags & V_NORMAL) && (bp->b_xflags & BX_ALTDATA) != 0) || 2017 ((flags & V_ALT) && (bp->b_xflags & BX_ALTDATA) == 0))) { 2018 continue; 2019 } 2020 if (nbp != NULL) { 2021 lblkno = nbp->b_lblkno; 2022 xflags = nbp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN); 2023 } 2024 retval = EAGAIN; 2025 error = BUF_TIMELOCK(bp, 2026 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, BO_LOCKPTR(bo), 2027 "flushbuf", slpflag, slptimeo); 2028 if (error) { 2029 BO_LOCK(bo); 2030 return (error != ENOLCK ? error : EAGAIN); 2031 } 2032 KASSERT(bp->b_bufobj == bo, 2033 ("bp %p wrong b_bufobj %p should be %p", 2034 bp, bp->b_bufobj, bo)); 2035 /* 2036 * XXX Since there are no node locks for NFS, I 2037 * believe there is a slight chance that a delayed 2038 * write will occur while sleeping just above, so 2039 * check for it. 2040 */ 2041 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) && 2042 (flags & V_SAVE)) { 2043 bremfree(bp); 2044 bp->b_flags |= B_ASYNC; 2045 bwrite(bp); 2046 BO_LOCK(bo); 2047 return (EAGAIN); /* XXX: why not loop ? */ 2048 } 2049 bremfree(bp); 2050 bp->b_flags |= (B_INVAL | B_RELBUF); 2051 bp->b_flags &= ~B_ASYNC; 2052 brelse(bp); 2053 BO_LOCK(bo); 2054 if (nbp == NULL) 2055 break; 2056 nbp = gbincore(bo, lblkno); 2057 if (nbp == NULL || (nbp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) 2058 != xflags) 2059 break; /* nbp invalid */ 2060 } 2061 return (retval); 2062 } 2063 2064 int 2065 bnoreuselist(struct bufv *bufv, struct bufobj *bo, daddr_t startn, daddr_t endn) 2066 { 2067 struct buf *bp; 2068 int error; 2069 daddr_t lblkno; 2070 2071 ASSERT_BO_LOCKED(bo); 2072 2073 for (lblkno = startn;;) { 2074 again: 2075 bp = BUF_PCTRIE_LOOKUP_GE(&bufv->bv_root, lblkno); 2076 if (bp == NULL || bp->b_lblkno >= endn || 2077 bp->b_lblkno < startn) 2078 break; 2079 error = BUF_TIMELOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL | 2080 LK_INTERLOCK, BO_LOCKPTR(bo), "brlsfl", 0, 0); 2081 if (error != 0) { 2082 BO_RLOCK(bo); 2083 if (error == ENOLCK) 2084 goto again; 2085 return (error); 2086 } 2087 KASSERT(bp->b_bufobj == bo, 2088 ("bp %p wrong b_bufobj %p should be %p", 2089 bp, bp->b_bufobj, bo)); 2090 lblkno = bp->b_lblkno + 1; 2091 if ((bp->b_flags & B_MANAGED) == 0) 2092 bremfree(bp); 2093 bp->b_flags |= B_RELBUF; 2094 /* 2095 * In the VMIO case, use the B_NOREUSE flag to hint that the 2096 * pages backing each buffer in the range are unlikely to be 2097 * reused. Dirty buffers will have the hint applied once 2098 * they've been written. 2099 */ 2100 if ((bp->b_flags & B_VMIO) != 0) 2101 bp->b_flags |= B_NOREUSE; 2102 brelse(bp); 2103 BO_RLOCK(bo); 2104 } 2105 return (0); 2106 } 2107 2108 /* 2109 * Truncate a file's buffer and pages to a specified length. This 2110 * is in lieu of the old vinvalbuf mechanism, which performed unneeded 2111 * sync activity. 2112 */ 2113 int 2114 vtruncbuf(struct vnode *vp, off_t length, int blksize) 2115 { 2116 struct buf *bp, *nbp; 2117 struct bufobj *bo; 2118 daddr_t startlbn; 2119 2120 CTR4(KTR_VFS, "%s: vp %p with block %d:%ju", __func__, 2121 vp, blksize, (uintmax_t)length); 2122 2123 /* 2124 * Round up to the *next* lbn. 2125 */ 2126 startlbn = howmany(length, blksize); 2127 2128 ASSERT_VOP_LOCKED(vp, "vtruncbuf"); 2129 2130 bo = &vp->v_bufobj; 2131 restart_unlocked: 2132 BO_LOCK(bo); 2133 2134 while (v_inval_buf_range_locked(vp, bo, startlbn, INT64_MAX) == EAGAIN) 2135 ; 2136 2137 if (length > 0) { 2138 restartsync: 2139 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) { 2140 if (bp->b_lblkno > 0) 2141 continue; 2142 /* 2143 * Since we hold the vnode lock this should only 2144 * fail if we're racing with the buf daemon. 2145 */ 2146 if (BUF_LOCK(bp, 2147 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, 2148 BO_LOCKPTR(bo)) == ENOLCK) 2149 goto restart_unlocked; 2150 2151 VNASSERT((bp->b_flags & B_DELWRI), vp, 2152 ("buf(%p) on dirty queue without DELWRI", bp)); 2153 2154 bremfree(bp); 2155 bawrite(bp); 2156 BO_LOCK(bo); 2157 goto restartsync; 2158 } 2159 } 2160 2161 bufobj_wwait(bo, 0, 0); 2162 BO_UNLOCK(bo); 2163 vnode_pager_setsize(vp, length); 2164 2165 return (0); 2166 } 2167 2168 /* 2169 * Invalidate the cached pages of a file's buffer within the range of block 2170 * numbers [startlbn, endlbn). 2171 */ 2172 void 2173 v_inval_buf_range(struct vnode *vp, daddr_t startlbn, daddr_t endlbn, 2174 int blksize) 2175 { 2176 struct bufobj *bo; 2177 off_t start, end; 2178 2179 ASSERT_VOP_LOCKED(vp, "v_inval_buf_range"); 2180 2181 start = blksize * startlbn; 2182 end = blksize * endlbn; 2183 2184 bo = &vp->v_bufobj; 2185 BO_LOCK(bo); 2186 MPASS(blksize == bo->bo_bsize); 2187 2188 while (v_inval_buf_range_locked(vp, bo, startlbn, endlbn) == EAGAIN) 2189 ; 2190 2191 BO_UNLOCK(bo); 2192 vn_pages_remove(vp, OFF_TO_IDX(start), OFF_TO_IDX(end + PAGE_SIZE - 1)); 2193 } 2194 2195 static int 2196 v_inval_buf_range_locked(struct vnode *vp, struct bufobj *bo, 2197 daddr_t startlbn, daddr_t endlbn) 2198 { 2199 struct buf *bp, *nbp; 2200 bool anyfreed; 2201 2202 ASSERT_VOP_LOCKED(vp, "v_inval_buf_range_locked"); 2203 ASSERT_BO_LOCKED(bo); 2204 2205 do { 2206 anyfreed = false; 2207 TAILQ_FOREACH_SAFE(bp, &bo->bo_clean.bv_hd, b_bobufs, nbp) { 2208 if (bp->b_lblkno < startlbn || bp->b_lblkno >= endlbn) 2209 continue; 2210 if (BUF_LOCK(bp, 2211 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, 2212 BO_LOCKPTR(bo)) == ENOLCK) { 2213 BO_LOCK(bo); 2214 return (EAGAIN); 2215 } 2216 2217 bremfree(bp); 2218 bp->b_flags |= B_INVAL | B_RELBUF; 2219 bp->b_flags &= ~B_ASYNC; 2220 brelse(bp); 2221 anyfreed = true; 2222 2223 BO_LOCK(bo); 2224 if (nbp != NULL && 2225 (((nbp->b_xflags & BX_VNCLEAN) == 0) || 2226 nbp->b_vp != vp || 2227 (nbp->b_flags & B_DELWRI) != 0)) 2228 return (EAGAIN); 2229 } 2230 2231 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) { 2232 if (bp->b_lblkno < startlbn || bp->b_lblkno >= endlbn) 2233 continue; 2234 if (BUF_LOCK(bp, 2235 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, 2236 BO_LOCKPTR(bo)) == ENOLCK) { 2237 BO_LOCK(bo); 2238 return (EAGAIN); 2239 } 2240 bremfree(bp); 2241 bp->b_flags |= B_INVAL | B_RELBUF; 2242 bp->b_flags &= ~B_ASYNC; 2243 brelse(bp); 2244 anyfreed = true; 2245 2246 BO_LOCK(bo); 2247 if (nbp != NULL && 2248 (((nbp->b_xflags & BX_VNDIRTY) == 0) || 2249 (nbp->b_vp != vp) || 2250 (nbp->b_flags & B_DELWRI) == 0)) 2251 return (EAGAIN); 2252 } 2253 } while (anyfreed); 2254 return (0); 2255 } 2256 2257 static void 2258 buf_vlist_remove(struct buf *bp) 2259 { 2260 struct bufv *bv; 2261 2262 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp)); 2263 ASSERT_BO_WLOCKED(bp->b_bufobj); 2264 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) != 2265 (BX_VNDIRTY|BX_VNCLEAN), 2266 ("buf_vlist_remove: Buf %p is on two lists", bp)); 2267 if (bp->b_xflags & BX_VNDIRTY) 2268 bv = &bp->b_bufobj->bo_dirty; 2269 else 2270 bv = &bp->b_bufobj->bo_clean; 2271 BUF_PCTRIE_REMOVE(&bv->bv_root, bp->b_lblkno); 2272 TAILQ_REMOVE(&bv->bv_hd, bp, b_bobufs); 2273 bv->bv_cnt--; 2274 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN); 2275 } 2276 2277 /* 2278 * Add the buffer to the sorted clean or dirty block list. 2279 * 2280 * NOTE: xflags is passed as a constant, optimizing this inline function! 2281 */ 2282 static void 2283 buf_vlist_add(struct buf *bp, struct bufobj *bo, b_xflags_t xflags) 2284 { 2285 struct bufv *bv; 2286 struct buf *n; 2287 int error; 2288 2289 ASSERT_BO_WLOCKED(bo); 2290 KASSERT((xflags & BX_VNDIRTY) == 0 || (bo->bo_flag & BO_DEAD) == 0, 2291 ("dead bo %p", bo)); 2292 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0, 2293 ("buf_vlist_add: Buf %p has existing xflags %d", bp, bp->b_xflags)); 2294 bp->b_xflags |= xflags; 2295 if (xflags & BX_VNDIRTY) 2296 bv = &bo->bo_dirty; 2297 else 2298 bv = &bo->bo_clean; 2299 2300 /* 2301 * Keep the list ordered. Optimize empty list insertion. Assume 2302 * we tend to grow at the tail so lookup_le should usually be cheaper 2303 * than _ge. 2304 */ 2305 if (bv->bv_cnt == 0 || 2306 bp->b_lblkno > TAILQ_LAST(&bv->bv_hd, buflists)->b_lblkno) 2307 TAILQ_INSERT_TAIL(&bv->bv_hd, bp, b_bobufs); 2308 else if ((n = BUF_PCTRIE_LOOKUP_LE(&bv->bv_root, bp->b_lblkno)) == NULL) 2309 TAILQ_INSERT_HEAD(&bv->bv_hd, bp, b_bobufs); 2310 else 2311 TAILQ_INSERT_AFTER(&bv->bv_hd, n, bp, b_bobufs); 2312 error = BUF_PCTRIE_INSERT(&bv->bv_root, bp); 2313 if (error) 2314 panic("buf_vlist_add: Preallocated nodes insufficient."); 2315 bv->bv_cnt++; 2316 } 2317 2318 /* 2319 * Look up a buffer using the buffer tries. 2320 */ 2321 struct buf * 2322 gbincore(struct bufobj *bo, daddr_t lblkno) 2323 { 2324 struct buf *bp; 2325 2326 ASSERT_BO_LOCKED(bo); 2327 bp = BUF_PCTRIE_LOOKUP(&bo->bo_clean.bv_root, lblkno); 2328 if (bp != NULL) 2329 return (bp); 2330 return BUF_PCTRIE_LOOKUP(&bo->bo_dirty.bv_root, lblkno); 2331 } 2332 2333 /* 2334 * Associate a buffer with a vnode. 2335 */ 2336 void 2337 bgetvp(struct vnode *vp, struct buf *bp) 2338 { 2339 struct bufobj *bo; 2340 2341 bo = &vp->v_bufobj; 2342 ASSERT_BO_WLOCKED(bo); 2343 VNASSERT(bp->b_vp == NULL, bp->b_vp, ("bgetvp: not free")); 2344 2345 CTR3(KTR_BUF, "bgetvp(%p) vp %p flags %X", bp, vp, bp->b_flags); 2346 VNASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0, vp, 2347 ("bgetvp: bp already attached! %p", bp)); 2348 2349 vhold(vp); 2350 bp->b_vp = vp; 2351 bp->b_bufobj = bo; 2352 /* 2353 * Insert onto list for new vnode. 2354 */ 2355 buf_vlist_add(bp, bo, BX_VNCLEAN); 2356 } 2357 2358 /* 2359 * Disassociate a buffer from a vnode. 2360 */ 2361 void 2362 brelvp(struct buf *bp) 2363 { 2364 struct bufobj *bo; 2365 struct vnode *vp; 2366 2367 CTR3(KTR_BUF, "brelvp(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags); 2368 KASSERT(bp->b_vp != NULL, ("brelvp: NULL")); 2369 2370 /* 2371 * Delete from old vnode list, if on one. 2372 */ 2373 vp = bp->b_vp; /* XXX */ 2374 bo = bp->b_bufobj; 2375 BO_LOCK(bo); 2376 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) 2377 buf_vlist_remove(bp); 2378 else 2379 panic("brelvp: Buffer %p not on queue.", bp); 2380 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) { 2381 bo->bo_flag &= ~BO_ONWORKLST; 2382 mtx_lock(&sync_mtx); 2383 LIST_REMOVE(bo, bo_synclist); 2384 syncer_worklist_len--; 2385 mtx_unlock(&sync_mtx); 2386 } 2387 bp->b_vp = NULL; 2388 bp->b_bufobj = NULL; 2389 BO_UNLOCK(bo); 2390 vdrop(vp); 2391 } 2392 2393 /* 2394 * Add an item to the syncer work queue. 2395 */ 2396 static void 2397 vn_syncer_add_to_worklist(struct bufobj *bo, int delay) 2398 { 2399 int slot; 2400 2401 ASSERT_BO_WLOCKED(bo); 2402 2403 mtx_lock(&sync_mtx); 2404 if (bo->bo_flag & BO_ONWORKLST) 2405 LIST_REMOVE(bo, bo_synclist); 2406 else { 2407 bo->bo_flag |= BO_ONWORKLST; 2408 syncer_worklist_len++; 2409 } 2410 2411 if (delay > syncer_maxdelay - 2) 2412 delay = syncer_maxdelay - 2; 2413 slot = (syncer_delayno + delay) & syncer_mask; 2414 2415 LIST_INSERT_HEAD(&syncer_workitem_pending[slot], bo, bo_synclist); 2416 mtx_unlock(&sync_mtx); 2417 } 2418 2419 static int 2420 sysctl_vfs_worklist_len(SYSCTL_HANDLER_ARGS) 2421 { 2422 int error, len; 2423 2424 mtx_lock(&sync_mtx); 2425 len = syncer_worklist_len - sync_vnode_count; 2426 mtx_unlock(&sync_mtx); 2427 error = SYSCTL_OUT(req, &len, sizeof(len)); 2428 return (error); 2429 } 2430 2431 SYSCTL_PROC(_vfs, OID_AUTO, worklist_len, 2432 CTLTYPE_INT | CTLFLAG_MPSAFE| CTLFLAG_RD, NULL, 0, 2433 sysctl_vfs_worklist_len, "I", "Syncer thread worklist length"); 2434 2435 static struct proc *updateproc; 2436 static void sched_sync(void); 2437 static struct kproc_desc up_kp = { 2438 "syncer", 2439 sched_sync, 2440 &updateproc 2441 }; 2442 SYSINIT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp); 2443 2444 static int 2445 sync_vnode(struct synclist *slp, struct bufobj **bo, struct thread *td) 2446 { 2447 struct vnode *vp; 2448 struct mount *mp; 2449 2450 *bo = LIST_FIRST(slp); 2451 if (*bo == NULL) 2452 return (0); 2453 vp = bo2vnode(*bo); 2454 if (VOP_ISLOCKED(vp) != 0 || VI_TRYLOCK(vp) == 0) 2455 return (1); 2456 /* 2457 * We use vhold in case the vnode does not 2458 * successfully sync. vhold prevents the vnode from 2459 * going away when we unlock the sync_mtx so that 2460 * we can acquire the vnode interlock. 2461 */ 2462 vholdl(vp); 2463 mtx_unlock(&sync_mtx); 2464 VI_UNLOCK(vp); 2465 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) { 2466 vdrop(vp); 2467 mtx_lock(&sync_mtx); 2468 return (*bo == LIST_FIRST(slp)); 2469 } 2470 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 2471 (void) VOP_FSYNC(vp, MNT_LAZY, td); 2472 VOP_UNLOCK(vp); 2473 vn_finished_write(mp); 2474 BO_LOCK(*bo); 2475 if (((*bo)->bo_flag & BO_ONWORKLST) != 0) { 2476 /* 2477 * Put us back on the worklist. The worklist 2478 * routine will remove us from our current 2479 * position and then add us back in at a later 2480 * position. 2481 */ 2482 vn_syncer_add_to_worklist(*bo, syncdelay); 2483 } 2484 BO_UNLOCK(*bo); 2485 vdrop(vp); 2486 mtx_lock(&sync_mtx); 2487 return (0); 2488 } 2489 2490 static int first_printf = 1; 2491 2492 /* 2493 * System filesystem synchronizer daemon. 2494 */ 2495 static void 2496 sched_sync(void) 2497 { 2498 struct synclist *next, *slp; 2499 struct bufobj *bo; 2500 long starttime; 2501 struct thread *td = curthread; 2502 int last_work_seen; 2503 int net_worklist_len; 2504 int syncer_final_iter; 2505 int error; 2506 2507 last_work_seen = 0; 2508 syncer_final_iter = 0; 2509 syncer_state = SYNCER_RUNNING; 2510 starttime = time_uptime; 2511 td->td_pflags |= TDP_NORUNNINGBUF; 2512 2513 EVENTHANDLER_REGISTER(shutdown_pre_sync, syncer_shutdown, td->td_proc, 2514 SHUTDOWN_PRI_LAST); 2515 2516 mtx_lock(&sync_mtx); 2517 for (;;) { 2518 if (syncer_state == SYNCER_FINAL_DELAY && 2519 syncer_final_iter == 0) { 2520 mtx_unlock(&sync_mtx); 2521 kproc_suspend_check(td->td_proc); 2522 mtx_lock(&sync_mtx); 2523 } 2524 net_worklist_len = syncer_worklist_len - sync_vnode_count; 2525 if (syncer_state != SYNCER_RUNNING && 2526 starttime != time_uptime) { 2527 if (first_printf) { 2528 printf("\nSyncing disks, vnodes remaining... "); 2529 first_printf = 0; 2530 } 2531 printf("%d ", net_worklist_len); 2532 } 2533 starttime = time_uptime; 2534 2535 /* 2536 * Push files whose dirty time has expired. Be careful 2537 * of interrupt race on slp queue. 2538 * 2539 * Skip over empty worklist slots when shutting down. 2540 */ 2541 do { 2542 slp = &syncer_workitem_pending[syncer_delayno]; 2543 syncer_delayno += 1; 2544 if (syncer_delayno == syncer_maxdelay) 2545 syncer_delayno = 0; 2546 next = &syncer_workitem_pending[syncer_delayno]; 2547 /* 2548 * If the worklist has wrapped since the 2549 * it was emptied of all but syncer vnodes, 2550 * switch to the FINAL_DELAY state and run 2551 * for one more second. 2552 */ 2553 if (syncer_state == SYNCER_SHUTTING_DOWN && 2554 net_worklist_len == 0 && 2555 last_work_seen == syncer_delayno) { 2556 syncer_state = SYNCER_FINAL_DELAY; 2557 syncer_final_iter = SYNCER_SHUTDOWN_SPEEDUP; 2558 } 2559 } while (syncer_state != SYNCER_RUNNING && LIST_EMPTY(slp) && 2560 syncer_worklist_len > 0); 2561 2562 /* 2563 * Keep track of the last time there was anything 2564 * on the worklist other than syncer vnodes. 2565 * Return to the SHUTTING_DOWN state if any 2566 * new work appears. 2567 */ 2568 if (net_worklist_len > 0 || syncer_state == SYNCER_RUNNING) 2569 last_work_seen = syncer_delayno; 2570 if (net_worklist_len > 0 && syncer_state == SYNCER_FINAL_DELAY) 2571 syncer_state = SYNCER_SHUTTING_DOWN; 2572 while (!LIST_EMPTY(slp)) { 2573 error = sync_vnode(slp, &bo, td); 2574 if (error == 1) { 2575 LIST_REMOVE(bo, bo_synclist); 2576 LIST_INSERT_HEAD(next, bo, bo_synclist); 2577 continue; 2578 } 2579 2580 if (first_printf == 0) { 2581 /* 2582 * Drop the sync mutex, because some watchdog 2583 * drivers need to sleep while patting 2584 */ 2585 mtx_unlock(&sync_mtx); 2586 wdog_kern_pat(WD_LASTVAL); 2587 mtx_lock(&sync_mtx); 2588 } 2589 2590 } 2591 if (syncer_state == SYNCER_FINAL_DELAY && syncer_final_iter > 0) 2592 syncer_final_iter--; 2593 /* 2594 * The variable rushjob allows the kernel to speed up the 2595 * processing of the filesystem syncer process. A rushjob 2596 * value of N tells the filesystem syncer to process the next 2597 * N seconds worth of work on its queue ASAP. Currently rushjob 2598 * is used by the soft update code to speed up the filesystem 2599 * syncer process when the incore state is getting so far 2600 * ahead of the disk that the kernel memory pool is being 2601 * threatened with exhaustion. 2602 */ 2603 if (rushjob > 0) { 2604 rushjob -= 1; 2605 continue; 2606 } 2607 /* 2608 * Just sleep for a short period of time between 2609 * iterations when shutting down to allow some I/O 2610 * to happen. 2611 * 2612 * If it has taken us less than a second to process the 2613 * current work, then wait. Otherwise start right over 2614 * again. We can still lose time if any single round 2615 * takes more than two seconds, but it does not really 2616 * matter as we are just trying to generally pace the 2617 * filesystem activity. 2618 */ 2619 if (syncer_state != SYNCER_RUNNING || 2620 time_uptime == starttime) { 2621 thread_lock(td); 2622 sched_prio(td, PPAUSE); 2623 thread_unlock(td); 2624 } 2625 if (syncer_state != SYNCER_RUNNING) 2626 cv_timedwait(&sync_wakeup, &sync_mtx, 2627 hz / SYNCER_SHUTDOWN_SPEEDUP); 2628 else if (time_uptime == starttime) 2629 cv_timedwait(&sync_wakeup, &sync_mtx, hz); 2630 } 2631 } 2632 2633 /* 2634 * Request the syncer daemon to speed up its work. 2635 * We never push it to speed up more than half of its 2636 * normal turn time, otherwise it could take over the cpu. 2637 */ 2638 int 2639 speedup_syncer(void) 2640 { 2641 int ret = 0; 2642 2643 mtx_lock(&sync_mtx); 2644 if (rushjob < syncdelay / 2) { 2645 rushjob += 1; 2646 stat_rush_requests += 1; 2647 ret = 1; 2648 } 2649 mtx_unlock(&sync_mtx); 2650 cv_broadcast(&sync_wakeup); 2651 return (ret); 2652 } 2653 2654 /* 2655 * Tell the syncer to speed up its work and run though its work 2656 * list several times, then tell it to shut down. 2657 */ 2658 static void 2659 syncer_shutdown(void *arg, int howto) 2660 { 2661 2662 if (howto & RB_NOSYNC) 2663 return; 2664 mtx_lock(&sync_mtx); 2665 syncer_state = SYNCER_SHUTTING_DOWN; 2666 rushjob = 0; 2667 mtx_unlock(&sync_mtx); 2668 cv_broadcast(&sync_wakeup); 2669 kproc_shutdown(arg, howto); 2670 } 2671 2672 void 2673 syncer_suspend(void) 2674 { 2675 2676 syncer_shutdown(updateproc, 0); 2677 } 2678 2679 void 2680 syncer_resume(void) 2681 { 2682 2683 mtx_lock(&sync_mtx); 2684 first_printf = 1; 2685 syncer_state = SYNCER_RUNNING; 2686 mtx_unlock(&sync_mtx); 2687 cv_broadcast(&sync_wakeup); 2688 kproc_resume(updateproc); 2689 } 2690 2691 /* 2692 * Reassign a buffer from one vnode to another. 2693 * Used to assign file specific control information 2694 * (indirect blocks) to the vnode to which they belong. 2695 */ 2696 void 2697 reassignbuf(struct buf *bp) 2698 { 2699 struct vnode *vp; 2700 struct bufobj *bo; 2701 int delay; 2702 #ifdef INVARIANTS 2703 struct bufv *bv; 2704 #endif 2705 2706 vp = bp->b_vp; 2707 bo = bp->b_bufobj; 2708 ++reassignbufcalls; 2709 2710 CTR3(KTR_BUF, "reassignbuf(%p) vp %p flags %X", 2711 bp, bp->b_vp, bp->b_flags); 2712 /* 2713 * B_PAGING flagged buffers cannot be reassigned because their vp 2714 * is not fully linked in. 2715 */ 2716 if (bp->b_flags & B_PAGING) 2717 panic("cannot reassign paging buffer"); 2718 2719 /* 2720 * Delete from old vnode list, if on one. 2721 */ 2722 BO_LOCK(bo); 2723 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) 2724 buf_vlist_remove(bp); 2725 else 2726 panic("reassignbuf: Buffer %p not on queue.", bp); 2727 /* 2728 * If dirty, put on list of dirty buffers; otherwise insert onto list 2729 * of clean buffers. 2730 */ 2731 if (bp->b_flags & B_DELWRI) { 2732 if ((bo->bo_flag & BO_ONWORKLST) == 0) { 2733 switch (vp->v_type) { 2734 case VDIR: 2735 delay = dirdelay; 2736 break; 2737 case VCHR: 2738 delay = metadelay; 2739 break; 2740 default: 2741 delay = filedelay; 2742 } 2743 vn_syncer_add_to_worklist(bo, delay); 2744 } 2745 buf_vlist_add(bp, bo, BX_VNDIRTY); 2746 } else { 2747 buf_vlist_add(bp, bo, BX_VNCLEAN); 2748 2749 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) { 2750 mtx_lock(&sync_mtx); 2751 LIST_REMOVE(bo, bo_synclist); 2752 syncer_worklist_len--; 2753 mtx_unlock(&sync_mtx); 2754 bo->bo_flag &= ~BO_ONWORKLST; 2755 } 2756 } 2757 #ifdef INVARIANTS 2758 bv = &bo->bo_clean; 2759 bp = TAILQ_FIRST(&bv->bv_hd); 2760 KASSERT(bp == NULL || bp->b_bufobj == bo, 2761 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo)); 2762 bp = TAILQ_LAST(&bv->bv_hd, buflists); 2763 KASSERT(bp == NULL || bp->b_bufobj == bo, 2764 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo)); 2765 bv = &bo->bo_dirty; 2766 bp = TAILQ_FIRST(&bv->bv_hd); 2767 KASSERT(bp == NULL || bp->b_bufobj == bo, 2768 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo)); 2769 bp = TAILQ_LAST(&bv->bv_hd, buflists); 2770 KASSERT(bp == NULL || bp->b_bufobj == bo, 2771 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo)); 2772 #endif 2773 BO_UNLOCK(bo); 2774 } 2775 2776 static void 2777 v_init_counters(struct vnode *vp) 2778 { 2779 2780 VNASSERT(vp->v_type == VNON && vp->v_data == NULL && vp->v_iflag == 0, 2781 vp, ("%s called for an initialized vnode", __FUNCTION__)); 2782 ASSERT_VI_UNLOCKED(vp, __FUNCTION__); 2783 2784 refcount_init(&vp->v_holdcnt, 1); 2785 refcount_init(&vp->v_usecount, 1); 2786 } 2787 2788 /* 2789 * Increment si_usecount of the associated device, if any. 2790 */ 2791 static void 2792 v_incr_devcount(struct vnode *vp) 2793 { 2794 2795 ASSERT_VI_LOCKED(vp, __FUNCTION__); 2796 if (vp->v_type == VCHR && vp->v_rdev != NULL) { 2797 dev_lock(); 2798 vp->v_rdev->si_usecount++; 2799 dev_unlock(); 2800 } 2801 } 2802 2803 /* 2804 * Decrement si_usecount of the associated device, if any. 2805 */ 2806 static void 2807 v_decr_devcount(struct vnode *vp) 2808 { 2809 2810 ASSERT_VI_LOCKED(vp, __FUNCTION__); 2811 if (vp->v_type == VCHR && vp->v_rdev != NULL) { 2812 dev_lock(); 2813 vp->v_rdev->si_usecount--; 2814 dev_unlock(); 2815 } 2816 } 2817 2818 /* 2819 * Grab a particular vnode from the free list, increment its 2820 * reference count and lock it. VIRF_DOOMED is set if the vnode 2821 * is being destroyed. Only callers who specify LK_RETRY will 2822 * see doomed vnodes. If inactive processing was delayed in 2823 * vput try to do it here. 2824 * 2825 * Both holdcnt and usecount can be manipulated using atomics without holding 2826 * any locks except in these cases which require the vnode interlock: 2827 * holdcnt: 1->0 and 0->1 2828 * usecount: 0->1 2829 * 2830 * usecount is permitted to transition 1->0 without the interlock because 2831 * vnode is kept live by holdcnt. 2832 */ 2833 static enum vgetstate __always_inline 2834 _vget_prep(struct vnode *vp, bool interlock) 2835 { 2836 enum vgetstate vs; 2837 2838 if (refcount_acquire_if_not_zero(&vp->v_usecount)) { 2839 vs = VGET_USECOUNT; 2840 } else { 2841 if (interlock) 2842 vholdl(vp); 2843 else 2844 vhold(vp); 2845 vs = VGET_HOLDCNT; 2846 } 2847 return (vs); 2848 } 2849 2850 enum vgetstate 2851 vget_prep(struct vnode *vp) 2852 { 2853 2854 return (_vget_prep(vp, false)); 2855 } 2856 2857 int 2858 vget(struct vnode *vp, int flags, struct thread *td) 2859 { 2860 enum vgetstate vs; 2861 2862 MPASS(td == curthread); 2863 2864 vs = _vget_prep(vp, (flags & LK_INTERLOCK) != 0); 2865 return (vget_finish(vp, flags, vs)); 2866 } 2867 2868 int 2869 vget_finish(struct vnode *vp, int flags, enum vgetstate vs) 2870 { 2871 int error, oweinact; 2872 2873 VNASSERT((flags & LK_TYPE_MASK) != 0, vp, 2874 ("%s: invalid lock operation", __func__)); 2875 2876 if ((flags & LK_INTERLOCK) != 0) 2877 ASSERT_VI_LOCKED(vp, __func__); 2878 else 2879 ASSERT_VI_UNLOCKED(vp, __func__); 2880 VNASSERT(vp->v_holdcnt > 0, vp, ("%s: vnode not held", __func__)); 2881 if (vs == VGET_USECOUNT) { 2882 VNASSERT(vp->v_usecount > 0, vp, 2883 ("%s: vnode without usecount when VGET_USECOUNT was passed", 2884 __func__)); 2885 } 2886 2887 if ((error = vn_lock(vp, flags)) != 0) { 2888 if (vs == VGET_USECOUNT) 2889 vrele(vp); 2890 else 2891 vdrop(vp); 2892 CTR2(KTR_VFS, "%s: impossible to lock vnode %p", __func__, 2893 vp); 2894 return (error); 2895 } 2896 2897 if (vs == VGET_USECOUNT) { 2898 VNASSERT((vp->v_iflag & VI_OWEINACT) == 0, vp, 2899 ("%s: vnode with usecount and VI_OWEINACT set", __func__)); 2900 return (0); 2901 } 2902 2903 /* 2904 * We hold the vnode. If the usecount is 0 it will be utilized to keep 2905 * the vnode around. Otherwise someone else lended their hold count and 2906 * we have to drop ours. 2907 */ 2908 if (refcount_acquire_if_not_zero(&vp->v_usecount)) { 2909 #ifdef INVARIANTS 2910 int old = atomic_fetchadd_int(&vp->v_holdcnt, -1); 2911 VNASSERT(old > 1, vp, ("%s: wrong hold count %d", __func__, old)); 2912 #else 2913 refcount_release(&vp->v_holdcnt); 2914 #endif 2915 VNODE_REFCOUNT_FENCE_ACQ(); 2916 VNASSERT((vp->v_iflag & VI_OWEINACT) == 0, vp, 2917 ("%s: vnode with usecount and VI_OWEINACT set", __func__)); 2918 return (0); 2919 } 2920 2921 /* 2922 * We don't guarantee that any particular close will 2923 * trigger inactive processing so just make a best effort 2924 * here at preventing a reference to a removed file. If 2925 * we don't succeed no harm is done. 2926 * 2927 * Upgrade our holdcnt to a usecount. 2928 */ 2929 VI_LOCK(vp); 2930 /* 2931 * See the previous section. By the time we get here we may find 2932 * ourselves in the same spot. 2933 */ 2934 if (refcount_acquire_if_not_zero(&vp->v_usecount)) { 2935 #ifdef INVARIANTS 2936 int old = atomic_fetchadd_int(&vp->v_holdcnt, -1); 2937 VNASSERT(old > 1, vp, ("%s: wrong hold count %d", __func__, old)); 2938 #else 2939 refcount_release(&vp->v_holdcnt); 2940 #endif 2941 VNODE_REFCOUNT_FENCE_ACQ(); 2942 VNASSERT((vp->v_iflag & VI_OWEINACT) == 0, vp, 2943 ("%s: vnode with usecount and VI_OWEINACT set", 2944 __func__)); 2945 VI_UNLOCK(vp); 2946 return (0); 2947 } 2948 if ((vp->v_iflag & VI_OWEINACT) == 0) { 2949 oweinact = 0; 2950 } else { 2951 oweinact = 1; 2952 vp->v_iflag &= ~VI_OWEINACT; 2953 VNODE_REFCOUNT_FENCE_REL(); 2954 } 2955 v_incr_devcount(vp); 2956 refcount_acquire(&vp->v_usecount); 2957 if (oweinact && VOP_ISLOCKED(vp) == LK_EXCLUSIVE && 2958 (flags & LK_NOWAIT) == 0) 2959 vinactive(vp); 2960 VI_UNLOCK(vp); 2961 return (0); 2962 } 2963 2964 /* 2965 * Increase the reference (use) and hold count of a vnode. 2966 * This will also remove the vnode from the free list if it is presently free. 2967 */ 2968 void 2969 vref(struct vnode *vp) 2970 { 2971 2972 ASSERT_VI_UNLOCKED(vp, __func__); 2973 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 2974 if (refcount_acquire_if_not_zero(&vp->v_usecount)) { 2975 VNODE_REFCOUNT_FENCE_ACQ(); 2976 VNASSERT(vp->v_holdcnt > 0, vp, 2977 ("%s: active vnode not held", __func__)); 2978 VNASSERT((vp->v_iflag & VI_OWEINACT) == 0, vp, 2979 ("%s: vnode with usecount and VI_OWEINACT set", __func__)); 2980 return; 2981 } 2982 VI_LOCK(vp); 2983 vrefl(vp); 2984 VI_UNLOCK(vp); 2985 } 2986 2987 void 2988 vrefl(struct vnode *vp) 2989 { 2990 2991 ASSERT_VI_LOCKED(vp, __func__); 2992 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 2993 if (refcount_acquire_if_not_zero(&vp->v_usecount)) { 2994 VNODE_REFCOUNT_FENCE_ACQ(); 2995 VNASSERT(vp->v_holdcnt > 0, vp, 2996 ("%s: active vnode not held", __func__)); 2997 VNASSERT((vp->v_iflag & VI_OWEINACT) == 0, vp, 2998 ("%s: vnode with usecount and VI_OWEINACT set", __func__)); 2999 return; 3000 } 3001 vholdl(vp); 3002 if ((vp->v_iflag & VI_OWEINACT) != 0) { 3003 vp->v_iflag &= ~VI_OWEINACT; 3004 VNODE_REFCOUNT_FENCE_REL(); 3005 } 3006 v_incr_devcount(vp); 3007 refcount_acquire(&vp->v_usecount); 3008 } 3009 3010 void 3011 vrefact(struct vnode *vp) 3012 { 3013 3014 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 3015 #ifdef INVARIANTS 3016 int old = atomic_fetchadd_int(&vp->v_usecount, 1); 3017 VNASSERT(old > 0, vp, ("%s: wrong use count %d", __func__, old)); 3018 #else 3019 refcount_acquire(&vp->v_usecount); 3020 #endif 3021 } 3022 3023 /* 3024 * Return reference count of a vnode. 3025 * 3026 * The results of this call are only guaranteed when some mechanism is used to 3027 * stop other processes from gaining references to the vnode. This may be the 3028 * case if the caller holds the only reference. This is also useful when stale 3029 * data is acceptable as race conditions may be accounted for by some other 3030 * means. 3031 */ 3032 int 3033 vrefcnt(struct vnode *vp) 3034 { 3035 3036 return (vp->v_usecount); 3037 } 3038 3039 void 3040 vlazy(struct vnode *vp) 3041 { 3042 struct mount *mp; 3043 3044 VNASSERT(vp->v_holdcnt > 0, vp, ("%s: vnode not held", __func__)); 3045 3046 if ((vp->v_mflag & VMP_LAZYLIST) != 0) 3047 return; 3048 mp = vp->v_mount; 3049 mtx_lock(&mp->mnt_listmtx); 3050 if ((vp->v_mflag & VMP_LAZYLIST) == 0) { 3051 vp->v_mflag |= VMP_LAZYLIST; 3052 TAILQ_INSERT_TAIL(&mp->mnt_lazyvnodelist, vp, v_lazylist); 3053 mp->mnt_lazyvnodelistsize++; 3054 } 3055 mtx_unlock(&mp->mnt_listmtx); 3056 } 3057 3058 static void 3059 vdefer_inactive(struct vnode *vp) 3060 { 3061 3062 ASSERT_VI_LOCKED(vp, __func__); 3063 VNASSERT(vp->v_iflag & VI_OWEINACT, vp, 3064 ("%s: vnode without VI_OWEINACT", __func__)); 3065 if (VN_IS_DOOMED(vp)) { 3066 vdropl(vp); 3067 return; 3068 } 3069 if (vp->v_iflag & VI_DEFINACT) { 3070 VNASSERT(vp->v_holdcnt > 1, vp, ("lost hold count")); 3071 vdropl(vp); 3072 return; 3073 } 3074 vlazy(vp); 3075 vp->v_iflag |= VI_DEFINACT; 3076 VI_UNLOCK(vp); 3077 counter_u64_add(deferred_inact, 1); 3078 } 3079 3080 static void 3081 vdefer_inactive_cond(struct vnode *vp) 3082 { 3083 3084 VI_LOCK(vp); 3085 VNASSERT(vp->v_holdcnt > 0, vp, ("vnode without hold count")); 3086 if ((vp->v_iflag & VI_OWEINACT) == 0) { 3087 vdropl(vp); 3088 return; 3089 } 3090 vdefer_inactive(vp); 3091 } 3092 3093 enum vputx_op { VPUTX_VRELE, VPUTX_VPUT, VPUTX_VUNREF }; 3094 3095 /* 3096 * Decrement the use and hold counts for a vnode. 3097 * 3098 * See an explanation near vget() as to why atomic operation is safe. 3099 */ 3100 static void 3101 vputx(struct vnode *vp, enum vputx_op func) 3102 { 3103 int error; 3104 3105 KASSERT(vp != NULL, ("vputx: null vp")); 3106 if (func == VPUTX_VUNREF) 3107 ASSERT_VOP_LOCKED(vp, "vunref"); 3108 ASSERT_VI_UNLOCKED(vp, __func__); 3109 VNASSERT(vp->v_holdcnt > 0 && vp->v_usecount > 0, vp, 3110 ("%s: wrong ref counts", __func__)); 3111 3112 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 3113 3114 /* 3115 * We want to hold the vnode until the inactive finishes to 3116 * prevent vgone() races. We drop the use count here and the 3117 * hold count below when we're done. 3118 * 3119 * If we release the last usecount we take ownership of the hold 3120 * count which provides liveness of the vnode, in which case we 3121 * have to vdrop. 3122 */ 3123 if (!refcount_release(&vp->v_usecount)) 3124 return; 3125 VI_LOCK(vp); 3126 v_decr_devcount(vp); 3127 /* 3128 * By the time we got here someone else might have transitioned 3129 * the count back to > 0. 3130 */ 3131 if (vp->v_usecount > 0) { 3132 vdropl(vp); 3133 return; 3134 } 3135 if (vp->v_iflag & VI_DOINGINACT) { 3136 vdropl(vp); 3137 return; 3138 } 3139 3140 /* 3141 * Check if the fs wants to perform inactive processing. Note we 3142 * may be only holding the interlock, in which case it is possible 3143 * someone else called vgone on the vnode and ->v_data is now NULL. 3144 * Since vgone performs inactive on its own there is nothing to do 3145 * here but to drop our hold count. 3146 */ 3147 if (__predict_false(VN_IS_DOOMED(vp)) || 3148 VOP_NEED_INACTIVE(vp) == 0) { 3149 vdropl(vp); 3150 return; 3151 } 3152 3153 /* 3154 * We must call VOP_INACTIVE with the node locked. Mark 3155 * as VI_DOINGINACT to avoid recursion. 3156 */ 3157 vp->v_iflag |= VI_OWEINACT; 3158 switch (func) { 3159 case VPUTX_VRELE: 3160 error = vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK); 3161 VI_LOCK(vp); 3162 break; 3163 case VPUTX_VPUT: 3164 error = VOP_LOCK(vp, LK_EXCLUSIVE | LK_INTERLOCK | LK_NOWAIT); 3165 VI_LOCK(vp); 3166 break; 3167 case VPUTX_VUNREF: 3168 error = 0; 3169 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) { 3170 error = VOP_LOCK(vp, LK_TRYUPGRADE | LK_INTERLOCK); 3171 VI_LOCK(vp); 3172 } 3173 break; 3174 } 3175 VNASSERT(vp->v_usecount == 0 || (vp->v_iflag & VI_OWEINACT) == 0, vp, 3176 ("vnode with usecount and VI_OWEINACT set")); 3177 if (error == 0) { 3178 if (vp->v_iflag & VI_OWEINACT) 3179 vinactive(vp); 3180 if (func != VPUTX_VUNREF) 3181 VOP_UNLOCK(vp); 3182 vdropl(vp); 3183 } else if (vp->v_iflag & VI_OWEINACT) { 3184 vdefer_inactive(vp); 3185 } else { 3186 vdropl(vp); 3187 } 3188 } 3189 3190 /* 3191 * Vnode put/release. 3192 * If count drops to zero, call inactive routine and return to freelist. 3193 */ 3194 void 3195 vrele(struct vnode *vp) 3196 { 3197 3198 vputx(vp, VPUTX_VRELE); 3199 } 3200 3201 /* 3202 * Release an already locked vnode. This give the same effects as 3203 * unlock+vrele(), but takes less time and avoids releasing and 3204 * re-aquiring the lock (as vrele() acquires the lock internally.) 3205 * 3206 * It is an invariant that all VOP_* calls operate on a held vnode. 3207 * We may be only having an implicit hold stemming from our usecount, 3208 * which we are about to release. If we unlock the vnode afterwards we 3209 * open a time window where someone else dropped the last usecount and 3210 * proceeded to free the vnode before our unlock finished. For this 3211 * reason we unlock the vnode early. This is a little bit wasteful as 3212 * it may be the vnode is exclusively locked and inactive processing is 3213 * needed, in which case we are adding work. 3214 */ 3215 void 3216 vput(struct vnode *vp) 3217 { 3218 3219 VOP_UNLOCK(vp); 3220 vputx(vp, VPUTX_VPUT); 3221 } 3222 3223 /* 3224 * Release an exclusively locked vnode. Do not unlock the vnode lock. 3225 */ 3226 void 3227 vunref(struct vnode *vp) 3228 { 3229 3230 vputx(vp, VPUTX_VUNREF); 3231 } 3232 3233 /* 3234 * Increase the hold count and activate if this is the first reference. 3235 */ 3236 static void 3237 vhold_activate(struct vnode *vp) 3238 { 3239 struct vdbatch *vd; 3240 3241 ASSERT_VI_LOCKED(vp, __func__); 3242 VNASSERT(vp->v_holdcnt == 0, vp, 3243 ("%s: wrong hold count", __func__)); 3244 VNASSERT(vp->v_op != NULL, vp, 3245 ("%s: vnode already reclaimed.", __func__)); 3246 critical_enter(); 3247 vd = DPCPU_PTR(vd); 3248 vd->freevnodes--; 3249 critical_exit(); 3250 refcount_acquire(&vp->v_holdcnt); 3251 } 3252 3253 void 3254 vhold(struct vnode *vp) 3255 { 3256 3257 ASSERT_VI_UNLOCKED(vp, __func__); 3258 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 3259 if (refcount_acquire_if_not_zero(&vp->v_holdcnt)) 3260 return; 3261 VI_LOCK(vp); 3262 vholdl(vp); 3263 VI_UNLOCK(vp); 3264 } 3265 3266 void 3267 vholdl(struct vnode *vp) 3268 { 3269 3270 ASSERT_VI_LOCKED(vp, __func__); 3271 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 3272 if (vp->v_holdcnt > 0) { 3273 refcount_acquire(&vp->v_holdcnt); 3274 return; 3275 } 3276 vhold_activate(vp); 3277 } 3278 3279 void 3280 vholdnz(struct vnode *vp) 3281 { 3282 3283 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 3284 #ifdef INVARIANTS 3285 int old = atomic_fetchadd_int(&vp->v_holdcnt, 1); 3286 VNASSERT(old > 0, vp, ("%s: wrong hold count %d", __func__, old)); 3287 #else 3288 atomic_add_int(&vp->v_holdcnt, 1); 3289 #endif 3290 } 3291 3292 static void __noinline 3293 vdbatch_process(struct vdbatch *vd) 3294 { 3295 struct vnode *vp; 3296 int i; 3297 3298 mtx_assert(&vd->lock, MA_OWNED); 3299 MPASS(curthread->td_pinned > 0); 3300 MPASS(vd->index == VDBATCH_SIZE); 3301 3302 mtx_lock(&vnode_list_mtx); 3303 critical_enter(); 3304 freevnodes += vd->freevnodes; 3305 for (i = 0; i < VDBATCH_SIZE; i++) { 3306 vp = vd->tab[i]; 3307 TAILQ_REMOVE(&vnode_list, vp, v_vnodelist); 3308 TAILQ_INSERT_TAIL(&vnode_list, vp, v_vnodelist); 3309 MPASS(vp->v_dbatchcpu != NOCPU); 3310 vp->v_dbatchcpu = NOCPU; 3311 } 3312 mtx_unlock(&vnode_list_mtx); 3313 critical_exit(); 3314 vd->freevnodes = 0; 3315 bzero(vd->tab, sizeof(vd->tab)); 3316 vd->index = 0; 3317 } 3318 3319 static void 3320 vdbatch_enqueue(struct vnode *vp) 3321 { 3322 struct vdbatch *vd; 3323 3324 ASSERT_VI_LOCKED(vp, __func__); 3325 VNASSERT(!VN_IS_DOOMED(vp), vp, 3326 ("%s: deferring requeue of a doomed vnode", __func__)); 3327 3328 critical_enter(); 3329 vd = DPCPU_PTR(vd); 3330 vd->freevnodes++; 3331 if (vp->v_dbatchcpu != NOCPU) { 3332 VI_UNLOCK(vp); 3333 critical_exit(); 3334 return; 3335 } 3336 3337 sched_pin(); 3338 critical_exit(); 3339 mtx_lock(&vd->lock); 3340 MPASS(vd->index < VDBATCH_SIZE); 3341 MPASS(vd->tab[vd->index] == NULL); 3342 /* 3343 * A hack: we depend on being pinned so that we know what to put in 3344 * ->v_dbatchcpu. 3345 */ 3346 vp->v_dbatchcpu = curcpu; 3347 vd->tab[vd->index] = vp; 3348 vd->index++; 3349 VI_UNLOCK(vp); 3350 if (vd->index == VDBATCH_SIZE) 3351 vdbatch_process(vd); 3352 mtx_unlock(&vd->lock); 3353 sched_unpin(); 3354 } 3355 3356 /* 3357 * This routine must only be called for vnodes which are about to be 3358 * deallocated. Supporting dequeue for arbitrary vndoes would require 3359 * validating that the locked batch matches. 3360 */ 3361 static void 3362 vdbatch_dequeue(struct vnode *vp) 3363 { 3364 struct vdbatch *vd; 3365 int i; 3366 short cpu; 3367 3368 VNASSERT(vp->v_type == VBAD || vp->v_type == VNON, vp, 3369 ("%s: called for a used vnode\n", __func__)); 3370 3371 cpu = vp->v_dbatchcpu; 3372 if (cpu == NOCPU) 3373 return; 3374 3375 vd = DPCPU_ID_PTR(cpu, vd); 3376 mtx_lock(&vd->lock); 3377 for (i = 0; i < vd->index; i++) { 3378 if (vd->tab[i] != vp) 3379 continue; 3380 vp->v_dbatchcpu = NOCPU; 3381 vd->index--; 3382 vd->tab[i] = vd->tab[vd->index]; 3383 vd->tab[vd->index] = NULL; 3384 break; 3385 } 3386 mtx_unlock(&vd->lock); 3387 /* 3388 * Either we dequeued the vnode above or the target CPU beat us to it. 3389 */ 3390 MPASS(vp->v_dbatchcpu == NOCPU); 3391 } 3392 3393 /* 3394 * Drop the hold count of the vnode. If this is the last reference to 3395 * the vnode we place it on the free list unless it has been vgone'd 3396 * (marked VIRF_DOOMED) in which case we will free it. 3397 * 3398 * Because the vnode vm object keeps a hold reference on the vnode if 3399 * there is at least one resident non-cached page, the vnode cannot 3400 * leave the active list without the page cleanup done. 3401 */ 3402 static void 3403 vdrop_deactivate(struct vnode *vp) 3404 { 3405 struct mount *mp; 3406 3407 ASSERT_VI_LOCKED(vp, __func__); 3408 /* 3409 * Mark a vnode as free: remove it from its active list 3410 * and put it up for recycling on the freelist. 3411 */ 3412 VNASSERT(!VN_IS_DOOMED(vp), vp, 3413 ("vdrop: returning doomed vnode")); 3414 VNASSERT(vp->v_op != NULL, vp, 3415 ("vdrop: vnode already reclaimed.")); 3416 VNASSERT(vp->v_holdcnt == 0, vp, 3417 ("vdrop: freeing when we shouldn't")); 3418 VNASSERT((vp->v_iflag & VI_OWEINACT) == 0, vp, 3419 ("vnode with VI_OWEINACT set")); 3420 VNASSERT((vp->v_iflag & VI_DEFINACT) == 0, vp, 3421 ("vnode with VI_DEFINACT set")); 3422 if (vp->v_mflag & VMP_LAZYLIST) { 3423 mp = vp->v_mount; 3424 mtx_lock(&mp->mnt_listmtx); 3425 vp->v_mflag &= ~VMP_LAZYLIST; 3426 TAILQ_REMOVE(&mp->mnt_lazyvnodelist, vp, v_lazylist); 3427 mp->mnt_lazyvnodelistsize--; 3428 mtx_unlock(&mp->mnt_listmtx); 3429 } 3430 vdbatch_enqueue(vp); 3431 } 3432 3433 void 3434 vdrop(struct vnode *vp) 3435 { 3436 3437 ASSERT_VI_UNLOCKED(vp, __func__); 3438 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 3439 if (refcount_release_if_not_last(&vp->v_holdcnt)) 3440 return; 3441 VI_LOCK(vp); 3442 vdropl(vp); 3443 } 3444 3445 void 3446 vdropl(struct vnode *vp) 3447 { 3448 3449 ASSERT_VI_LOCKED(vp, __func__); 3450 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 3451 if (!refcount_release(&vp->v_holdcnt)) { 3452 VI_UNLOCK(vp); 3453 return; 3454 } 3455 if (VN_IS_DOOMED(vp)) { 3456 freevnode(vp); 3457 return; 3458 } 3459 vdrop_deactivate(vp); 3460 } 3461 3462 /* 3463 * Call VOP_INACTIVE on the vnode and manage the DOINGINACT and OWEINACT 3464 * flags. DOINGINACT prevents us from recursing in calls to vinactive. 3465 * OWEINACT tracks whether a vnode missed a call to inactive due to a 3466 * failed lock upgrade. 3467 */ 3468 void 3469 vinactive(struct vnode *vp) 3470 { 3471 struct vm_object *obj; 3472 3473 ASSERT_VOP_ELOCKED(vp, "vinactive"); 3474 ASSERT_VI_LOCKED(vp, "vinactive"); 3475 VNASSERT((vp->v_iflag & VI_DOINGINACT) == 0, vp, 3476 ("vinactive: recursed on VI_DOINGINACT")); 3477 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 3478 vp->v_iflag |= VI_DOINGINACT; 3479 vp->v_iflag &= ~VI_OWEINACT; 3480 VI_UNLOCK(vp); 3481 /* 3482 * Before moving off the active list, we must be sure that any 3483 * modified pages are converted into the vnode's dirty 3484 * buffers, since these will no longer be checked once the 3485 * vnode is on the inactive list. 3486 * 3487 * The write-out of the dirty pages is asynchronous. At the 3488 * point that VOP_INACTIVE() is called, there could still be 3489 * pending I/O and dirty pages in the object. 3490 */ 3491 if ((obj = vp->v_object) != NULL && (vp->v_vflag & VV_NOSYNC) == 0 && 3492 vm_object_mightbedirty(obj)) { 3493 VM_OBJECT_WLOCK(obj); 3494 vm_object_page_clean(obj, 0, 0, 0); 3495 VM_OBJECT_WUNLOCK(obj); 3496 } 3497 VOP_INACTIVE(vp, curthread); 3498 VI_LOCK(vp); 3499 VNASSERT(vp->v_iflag & VI_DOINGINACT, vp, 3500 ("vinactive: lost VI_DOINGINACT")); 3501 vp->v_iflag &= ~VI_DOINGINACT; 3502 } 3503 3504 /* 3505 * Remove any vnodes in the vnode table belonging to mount point mp. 3506 * 3507 * If FORCECLOSE is not specified, there should not be any active ones, 3508 * return error if any are found (nb: this is a user error, not a 3509 * system error). If FORCECLOSE is specified, detach any active vnodes 3510 * that are found. 3511 * 3512 * If WRITECLOSE is set, only flush out regular file vnodes open for 3513 * writing. 3514 * 3515 * SKIPSYSTEM causes any vnodes marked VV_SYSTEM to be skipped. 3516 * 3517 * `rootrefs' specifies the base reference count for the root vnode 3518 * of this filesystem. The root vnode is considered busy if its 3519 * v_usecount exceeds this value. On a successful return, vflush(, td) 3520 * will call vrele() on the root vnode exactly rootrefs times. 3521 * If the SKIPSYSTEM or WRITECLOSE flags are specified, rootrefs must 3522 * be zero. 3523 */ 3524 #ifdef DIAGNOSTIC 3525 static int busyprt = 0; /* print out busy vnodes */ 3526 SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, "Print out busy vnodes"); 3527 #endif 3528 3529 int 3530 vflush(struct mount *mp, int rootrefs, int flags, struct thread *td) 3531 { 3532 struct vnode *vp, *mvp, *rootvp = NULL; 3533 struct vattr vattr; 3534 int busy = 0, error; 3535 3536 CTR4(KTR_VFS, "%s: mp %p with rootrefs %d and flags %d", __func__, mp, 3537 rootrefs, flags); 3538 if (rootrefs > 0) { 3539 KASSERT((flags & (SKIPSYSTEM | WRITECLOSE)) == 0, 3540 ("vflush: bad args")); 3541 /* 3542 * Get the filesystem root vnode. We can vput() it 3543 * immediately, since with rootrefs > 0, it won't go away. 3544 */ 3545 if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rootvp)) != 0) { 3546 CTR2(KTR_VFS, "%s: vfs_root lookup failed with %d", 3547 __func__, error); 3548 return (error); 3549 } 3550 vput(rootvp); 3551 } 3552 loop: 3553 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) { 3554 vholdl(vp); 3555 error = vn_lock(vp, LK_INTERLOCK | LK_EXCLUSIVE); 3556 if (error) { 3557 vdrop(vp); 3558 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp); 3559 goto loop; 3560 } 3561 /* 3562 * Skip over a vnodes marked VV_SYSTEM. 3563 */ 3564 if ((flags & SKIPSYSTEM) && (vp->v_vflag & VV_SYSTEM)) { 3565 VOP_UNLOCK(vp); 3566 vdrop(vp); 3567 continue; 3568 } 3569 /* 3570 * If WRITECLOSE is set, flush out unlinked but still open 3571 * files (even if open only for reading) and regular file 3572 * vnodes open for writing. 3573 */ 3574 if (flags & WRITECLOSE) { 3575 if (vp->v_object != NULL) { 3576 VM_OBJECT_WLOCK(vp->v_object); 3577 vm_object_page_clean(vp->v_object, 0, 0, 0); 3578 VM_OBJECT_WUNLOCK(vp->v_object); 3579 } 3580 error = VOP_FSYNC(vp, MNT_WAIT, td); 3581 if (error != 0) { 3582 VOP_UNLOCK(vp); 3583 vdrop(vp); 3584 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp); 3585 return (error); 3586 } 3587 error = VOP_GETATTR(vp, &vattr, td->td_ucred); 3588 VI_LOCK(vp); 3589 3590 if ((vp->v_type == VNON || 3591 (error == 0 && vattr.va_nlink > 0)) && 3592 (vp->v_writecount <= 0 || vp->v_type != VREG)) { 3593 VOP_UNLOCK(vp); 3594 vdropl(vp); 3595 continue; 3596 } 3597 } else 3598 VI_LOCK(vp); 3599 /* 3600 * With v_usecount == 0, all we need to do is clear out the 3601 * vnode data structures and we are done. 3602 * 3603 * If FORCECLOSE is set, forcibly close the vnode. 3604 */ 3605 if (vp->v_usecount == 0 || (flags & FORCECLOSE)) { 3606 vgonel(vp); 3607 } else { 3608 busy++; 3609 #ifdef DIAGNOSTIC 3610 if (busyprt) 3611 vn_printf(vp, "vflush: busy vnode "); 3612 #endif 3613 } 3614 VOP_UNLOCK(vp); 3615 vdropl(vp); 3616 } 3617 if (rootrefs > 0 && (flags & FORCECLOSE) == 0) { 3618 /* 3619 * If just the root vnode is busy, and if its refcount 3620 * is equal to `rootrefs', then go ahead and kill it. 3621 */ 3622 VI_LOCK(rootvp); 3623 KASSERT(busy > 0, ("vflush: not busy")); 3624 VNASSERT(rootvp->v_usecount >= rootrefs, rootvp, 3625 ("vflush: usecount %d < rootrefs %d", 3626 rootvp->v_usecount, rootrefs)); 3627 if (busy == 1 && rootvp->v_usecount == rootrefs) { 3628 VOP_LOCK(rootvp, LK_EXCLUSIVE|LK_INTERLOCK); 3629 vgone(rootvp); 3630 VOP_UNLOCK(rootvp); 3631 busy = 0; 3632 } else 3633 VI_UNLOCK(rootvp); 3634 } 3635 if (busy) { 3636 CTR2(KTR_VFS, "%s: failing as %d vnodes are busy", __func__, 3637 busy); 3638 return (EBUSY); 3639 } 3640 for (; rootrefs > 0; rootrefs--) 3641 vrele(rootvp); 3642 return (0); 3643 } 3644 3645 /* 3646 * Recycle an unused vnode to the front of the free list. 3647 */ 3648 int 3649 vrecycle(struct vnode *vp) 3650 { 3651 int recycled; 3652 3653 VI_LOCK(vp); 3654 recycled = vrecyclel(vp); 3655 VI_UNLOCK(vp); 3656 return (recycled); 3657 } 3658 3659 /* 3660 * vrecycle, with the vp interlock held. 3661 */ 3662 int 3663 vrecyclel(struct vnode *vp) 3664 { 3665 int recycled; 3666 3667 ASSERT_VOP_ELOCKED(vp, __func__); 3668 ASSERT_VI_LOCKED(vp, __func__); 3669 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 3670 recycled = 0; 3671 if (vp->v_usecount == 0) { 3672 recycled = 1; 3673 vgonel(vp); 3674 } 3675 return (recycled); 3676 } 3677 3678 /* 3679 * Eliminate all activity associated with a vnode 3680 * in preparation for reuse. 3681 */ 3682 void 3683 vgone(struct vnode *vp) 3684 { 3685 VI_LOCK(vp); 3686 vgonel(vp); 3687 VI_UNLOCK(vp); 3688 } 3689 3690 static void 3691 notify_lowervp_vfs_dummy(struct mount *mp __unused, 3692 struct vnode *lowervp __unused) 3693 { 3694 } 3695 3696 /* 3697 * Notify upper mounts about reclaimed or unlinked vnode. 3698 */ 3699 void 3700 vfs_notify_upper(struct vnode *vp, int event) 3701 { 3702 static struct vfsops vgonel_vfsops = { 3703 .vfs_reclaim_lowervp = notify_lowervp_vfs_dummy, 3704 .vfs_unlink_lowervp = notify_lowervp_vfs_dummy, 3705 }; 3706 struct mount *mp, *ump, *mmp; 3707 3708 mp = vp->v_mount; 3709 if (mp == NULL) 3710 return; 3711 if (TAILQ_EMPTY(&mp->mnt_uppers)) 3712 return; 3713 3714 mmp = malloc(sizeof(struct mount), M_TEMP, M_WAITOK | M_ZERO); 3715 mmp->mnt_op = &vgonel_vfsops; 3716 mmp->mnt_kern_flag |= MNTK_MARKER; 3717 MNT_ILOCK(mp); 3718 mp->mnt_kern_flag |= MNTK_VGONE_UPPER; 3719 for (ump = TAILQ_FIRST(&mp->mnt_uppers); ump != NULL;) { 3720 if ((ump->mnt_kern_flag & MNTK_MARKER) != 0) { 3721 ump = TAILQ_NEXT(ump, mnt_upper_link); 3722 continue; 3723 } 3724 TAILQ_INSERT_AFTER(&mp->mnt_uppers, ump, mmp, mnt_upper_link); 3725 MNT_IUNLOCK(mp); 3726 switch (event) { 3727 case VFS_NOTIFY_UPPER_RECLAIM: 3728 VFS_RECLAIM_LOWERVP(ump, vp); 3729 break; 3730 case VFS_NOTIFY_UPPER_UNLINK: 3731 VFS_UNLINK_LOWERVP(ump, vp); 3732 break; 3733 default: 3734 KASSERT(0, ("invalid event %d", event)); 3735 break; 3736 } 3737 MNT_ILOCK(mp); 3738 ump = TAILQ_NEXT(mmp, mnt_upper_link); 3739 TAILQ_REMOVE(&mp->mnt_uppers, mmp, mnt_upper_link); 3740 } 3741 free(mmp, M_TEMP); 3742 mp->mnt_kern_flag &= ~MNTK_VGONE_UPPER; 3743 if ((mp->mnt_kern_flag & MNTK_VGONE_WAITER) != 0) { 3744 mp->mnt_kern_flag &= ~MNTK_VGONE_WAITER; 3745 wakeup(&mp->mnt_uppers); 3746 } 3747 MNT_IUNLOCK(mp); 3748 } 3749 3750 /* 3751 * vgone, with the vp interlock held. 3752 */ 3753 static void 3754 vgonel(struct vnode *vp) 3755 { 3756 struct thread *td; 3757 struct mount *mp; 3758 vm_object_t object; 3759 bool active, oweinact; 3760 3761 ASSERT_VOP_ELOCKED(vp, "vgonel"); 3762 ASSERT_VI_LOCKED(vp, "vgonel"); 3763 VNASSERT(vp->v_holdcnt, vp, 3764 ("vgonel: vp %p has no reference.", vp)); 3765 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 3766 td = curthread; 3767 3768 /* 3769 * Don't vgonel if we're already doomed. 3770 */ 3771 if (vp->v_irflag & VIRF_DOOMED) 3772 return; 3773 vp->v_irflag |= VIRF_DOOMED; 3774 3775 /* 3776 * Check to see if the vnode is in use. If so, we have to call 3777 * VOP_CLOSE() and VOP_INACTIVE(). 3778 */ 3779 active = vp->v_usecount > 0; 3780 oweinact = (vp->v_iflag & VI_OWEINACT) != 0; 3781 /* 3782 * If we need to do inactive VI_OWEINACT will be set. 3783 */ 3784 if (vp->v_iflag & VI_DEFINACT) { 3785 VNASSERT(vp->v_holdcnt > 1, vp, ("lost hold count")); 3786 vp->v_iflag &= ~VI_DEFINACT; 3787 vdropl(vp); 3788 } else { 3789 VNASSERT(vp->v_holdcnt > 0, vp, ("vnode without hold count")); 3790 VI_UNLOCK(vp); 3791 } 3792 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_RECLAIM); 3793 3794 /* 3795 * If purging an active vnode, it must be closed and 3796 * deactivated before being reclaimed. 3797 */ 3798 if (active) 3799 VOP_CLOSE(vp, FNONBLOCK, NOCRED, td); 3800 if (oweinact || active) { 3801 VI_LOCK(vp); 3802 if ((vp->v_iflag & VI_DOINGINACT) == 0) 3803 vinactive(vp); 3804 VI_UNLOCK(vp); 3805 } 3806 if (vp->v_type == VSOCK) 3807 vfs_unp_reclaim(vp); 3808 3809 /* 3810 * Clean out any buffers associated with the vnode. 3811 * If the flush fails, just toss the buffers. 3812 */ 3813 mp = NULL; 3814 if (!TAILQ_EMPTY(&vp->v_bufobj.bo_dirty.bv_hd)) 3815 (void) vn_start_secondary_write(vp, &mp, V_WAIT); 3816 if (vinvalbuf(vp, V_SAVE, 0, 0) != 0) { 3817 while (vinvalbuf(vp, 0, 0, 0) != 0) 3818 ; 3819 } 3820 3821 BO_LOCK(&vp->v_bufobj); 3822 KASSERT(TAILQ_EMPTY(&vp->v_bufobj.bo_dirty.bv_hd) && 3823 vp->v_bufobj.bo_dirty.bv_cnt == 0 && 3824 TAILQ_EMPTY(&vp->v_bufobj.bo_clean.bv_hd) && 3825 vp->v_bufobj.bo_clean.bv_cnt == 0, 3826 ("vp %p bufobj not invalidated", vp)); 3827 3828 /* 3829 * For VMIO bufobj, BO_DEAD is set later, or in 3830 * vm_object_terminate() after the object's page queue is 3831 * flushed. 3832 */ 3833 object = vp->v_bufobj.bo_object; 3834 if (object == NULL) 3835 vp->v_bufobj.bo_flag |= BO_DEAD; 3836 BO_UNLOCK(&vp->v_bufobj); 3837 3838 /* 3839 * Handle the VM part. Tmpfs handles v_object on its own (the 3840 * OBJT_VNODE check). Nullfs or other bypassing filesystems 3841 * should not touch the object borrowed from the lower vnode 3842 * (the handle check). 3843 */ 3844 if (object != NULL && object->type == OBJT_VNODE && 3845 object->handle == vp) 3846 vnode_destroy_vobject(vp); 3847 3848 /* 3849 * Reclaim the vnode. 3850 */ 3851 if (VOP_RECLAIM(vp, td)) 3852 panic("vgone: cannot reclaim"); 3853 if (mp != NULL) 3854 vn_finished_secondary_write(mp); 3855 VNASSERT(vp->v_object == NULL, vp, 3856 ("vop_reclaim left v_object vp=%p", vp)); 3857 /* 3858 * Clear the advisory locks and wake up waiting threads. 3859 */ 3860 (void)VOP_ADVLOCKPURGE(vp); 3861 vp->v_lockf = NULL; 3862 /* 3863 * Delete from old mount point vnode list. 3864 */ 3865 delmntque(vp); 3866 cache_purge(vp); 3867 /* 3868 * Done with purge, reset to the standard lock and invalidate 3869 * the vnode. 3870 */ 3871 VI_LOCK(vp); 3872 vp->v_vnlock = &vp->v_lock; 3873 vp->v_op = &dead_vnodeops; 3874 vp->v_type = VBAD; 3875 } 3876 3877 /* 3878 * Calculate the total number of references to a special device. 3879 */ 3880 int 3881 vcount(struct vnode *vp) 3882 { 3883 int count; 3884 3885 dev_lock(); 3886 count = vp->v_rdev->si_usecount; 3887 dev_unlock(); 3888 return (count); 3889 } 3890 3891 /* 3892 * Print out a description of a vnode. 3893 */ 3894 static char *typename[] = 3895 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD", 3896 "VMARKER"}; 3897 3898 void 3899 vn_printf(struct vnode *vp, const char *fmt, ...) 3900 { 3901 va_list ap; 3902 char buf[256], buf2[16]; 3903 u_long flags; 3904 3905 va_start(ap, fmt); 3906 vprintf(fmt, ap); 3907 va_end(ap); 3908 printf("%p: ", (void *)vp); 3909 printf("type %s\n", typename[vp->v_type]); 3910 printf(" usecount %d, writecount %d, refcount %d", 3911 vp->v_usecount, vp->v_writecount, vp->v_holdcnt); 3912 switch (vp->v_type) { 3913 case VDIR: 3914 printf(" mountedhere %p\n", vp->v_mountedhere); 3915 break; 3916 case VCHR: 3917 printf(" rdev %p\n", vp->v_rdev); 3918 break; 3919 case VSOCK: 3920 printf(" socket %p\n", vp->v_unpcb); 3921 break; 3922 case VFIFO: 3923 printf(" fifoinfo %p\n", vp->v_fifoinfo); 3924 break; 3925 default: 3926 printf("\n"); 3927 break; 3928 } 3929 buf[0] = '\0'; 3930 buf[1] = '\0'; 3931 if (vp->v_irflag & VIRF_DOOMED) 3932 strlcat(buf, "|VIRF_DOOMED", sizeof(buf)); 3933 flags = vp->v_irflag & ~(VIRF_DOOMED); 3934 if (flags != 0) { 3935 snprintf(buf2, sizeof(buf2), "|VIRF(0x%lx)", flags); 3936 strlcat(buf, buf2, sizeof(buf)); 3937 } 3938 if (vp->v_vflag & VV_ROOT) 3939 strlcat(buf, "|VV_ROOT", sizeof(buf)); 3940 if (vp->v_vflag & VV_ISTTY) 3941 strlcat(buf, "|VV_ISTTY", sizeof(buf)); 3942 if (vp->v_vflag & VV_NOSYNC) 3943 strlcat(buf, "|VV_NOSYNC", sizeof(buf)); 3944 if (vp->v_vflag & VV_ETERNALDEV) 3945 strlcat(buf, "|VV_ETERNALDEV", sizeof(buf)); 3946 if (vp->v_vflag & VV_CACHEDLABEL) 3947 strlcat(buf, "|VV_CACHEDLABEL", sizeof(buf)); 3948 if (vp->v_vflag & VV_VMSIZEVNLOCK) 3949 strlcat(buf, "|VV_VMSIZEVNLOCK", sizeof(buf)); 3950 if (vp->v_vflag & VV_COPYONWRITE) 3951 strlcat(buf, "|VV_COPYONWRITE", sizeof(buf)); 3952 if (vp->v_vflag & VV_SYSTEM) 3953 strlcat(buf, "|VV_SYSTEM", sizeof(buf)); 3954 if (vp->v_vflag & VV_PROCDEP) 3955 strlcat(buf, "|VV_PROCDEP", sizeof(buf)); 3956 if (vp->v_vflag & VV_NOKNOTE) 3957 strlcat(buf, "|VV_NOKNOTE", sizeof(buf)); 3958 if (vp->v_vflag & VV_DELETED) 3959 strlcat(buf, "|VV_DELETED", sizeof(buf)); 3960 if (vp->v_vflag & VV_MD) 3961 strlcat(buf, "|VV_MD", sizeof(buf)); 3962 if (vp->v_vflag & VV_FORCEINSMQ) 3963 strlcat(buf, "|VV_FORCEINSMQ", sizeof(buf)); 3964 if (vp->v_vflag & VV_READLINK) 3965 strlcat(buf, "|VV_READLINK", sizeof(buf)); 3966 flags = vp->v_vflag & ~(VV_ROOT | VV_ISTTY | VV_NOSYNC | VV_ETERNALDEV | 3967 VV_CACHEDLABEL | VV_COPYONWRITE | VV_SYSTEM | VV_PROCDEP | 3968 VV_NOKNOTE | VV_DELETED | VV_MD | VV_FORCEINSMQ); 3969 if (flags != 0) { 3970 snprintf(buf2, sizeof(buf2), "|VV(0x%lx)", flags); 3971 strlcat(buf, buf2, sizeof(buf)); 3972 } 3973 if (vp->v_iflag & VI_TEXT_REF) 3974 strlcat(buf, "|VI_TEXT_REF", sizeof(buf)); 3975 if (vp->v_iflag & VI_MOUNT) 3976 strlcat(buf, "|VI_MOUNT", sizeof(buf)); 3977 if (vp->v_iflag & VI_DOINGINACT) 3978 strlcat(buf, "|VI_DOINGINACT", sizeof(buf)); 3979 if (vp->v_iflag & VI_OWEINACT) 3980 strlcat(buf, "|VI_OWEINACT", sizeof(buf)); 3981 if (vp->v_iflag & VI_DEFINACT) 3982 strlcat(buf, "|VI_DEFINACT", sizeof(buf)); 3983 flags = vp->v_iflag & ~(VI_TEXT_REF | VI_MOUNT | VI_DOINGINACT | 3984 VI_OWEINACT | VI_DEFINACT); 3985 if (flags != 0) { 3986 snprintf(buf2, sizeof(buf2), "|VI(0x%lx)", flags); 3987 strlcat(buf, buf2, sizeof(buf)); 3988 } 3989 if (vp->v_mflag & VMP_LAZYLIST) 3990 strlcat(buf, "|VMP_LAZYLIST", sizeof(buf)); 3991 flags = vp->v_mflag & ~(VMP_LAZYLIST); 3992 if (flags != 0) { 3993 snprintf(buf2, sizeof(buf2), "|VMP(0x%lx)", flags); 3994 strlcat(buf, buf2, sizeof(buf)); 3995 } 3996 printf(" flags (%s)\n", buf + 1); 3997 if (mtx_owned(VI_MTX(vp))) 3998 printf(" VI_LOCKed"); 3999 if (vp->v_object != NULL) 4000 printf(" v_object %p ref %d pages %d " 4001 "cleanbuf %d dirtybuf %d\n", 4002 vp->v_object, vp->v_object->ref_count, 4003 vp->v_object->resident_page_count, 4004 vp->v_bufobj.bo_clean.bv_cnt, 4005 vp->v_bufobj.bo_dirty.bv_cnt); 4006 printf(" "); 4007 lockmgr_printinfo(vp->v_vnlock); 4008 if (vp->v_data != NULL) 4009 VOP_PRINT(vp); 4010 } 4011 4012 #ifdef DDB 4013 /* 4014 * List all of the locked vnodes in the system. 4015 * Called when debugging the kernel. 4016 */ 4017 DB_SHOW_COMMAND(lockedvnods, lockedvnodes) 4018 { 4019 struct mount *mp; 4020 struct vnode *vp; 4021 4022 /* 4023 * Note: because this is DDB, we can't obey the locking semantics 4024 * for these structures, which means we could catch an inconsistent 4025 * state and dereference a nasty pointer. Not much to be done 4026 * about that. 4027 */ 4028 db_printf("Locked vnodes\n"); 4029 TAILQ_FOREACH(mp, &mountlist, mnt_list) { 4030 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) { 4031 if (vp->v_type != VMARKER && VOP_ISLOCKED(vp)) 4032 vn_printf(vp, "vnode "); 4033 } 4034 } 4035 } 4036 4037 /* 4038 * Show details about the given vnode. 4039 */ 4040 DB_SHOW_COMMAND(vnode, db_show_vnode) 4041 { 4042 struct vnode *vp; 4043 4044 if (!have_addr) 4045 return; 4046 vp = (struct vnode *)addr; 4047 vn_printf(vp, "vnode "); 4048 } 4049 4050 /* 4051 * Show details about the given mount point. 4052 */ 4053 DB_SHOW_COMMAND(mount, db_show_mount) 4054 { 4055 struct mount *mp; 4056 struct vfsopt *opt; 4057 struct statfs *sp; 4058 struct vnode *vp; 4059 char buf[512]; 4060 uint64_t mflags; 4061 u_int flags; 4062 4063 if (!have_addr) { 4064 /* No address given, print short info about all mount points. */ 4065 TAILQ_FOREACH(mp, &mountlist, mnt_list) { 4066 db_printf("%p %s on %s (%s)\n", mp, 4067 mp->mnt_stat.f_mntfromname, 4068 mp->mnt_stat.f_mntonname, 4069 mp->mnt_stat.f_fstypename); 4070 if (db_pager_quit) 4071 break; 4072 } 4073 db_printf("\nMore info: show mount <addr>\n"); 4074 return; 4075 } 4076 4077 mp = (struct mount *)addr; 4078 db_printf("%p %s on %s (%s)\n", mp, mp->mnt_stat.f_mntfromname, 4079 mp->mnt_stat.f_mntonname, mp->mnt_stat.f_fstypename); 4080 4081 buf[0] = '\0'; 4082 mflags = mp->mnt_flag; 4083 #define MNT_FLAG(flag) do { \ 4084 if (mflags & (flag)) { \ 4085 if (buf[0] != '\0') \ 4086 strlcat(buf, ", ", sizeof(buf)); \ 4087 strlcat(buf, (#flag) + 4, sizeof(buf)); \ 4088 mflags &= ~(flag); \ 4089 } \ 4090 } while (0) 4091 MNT_FLAG(MNT_RDONLY); 4092 MNT_FLAG(MNT_SYNCHRONOUS); 4093 MNT_FLAG(MNT_NOEXEC); 4094 MNT_FLAG(MNT_NOSUID); 4095 MNT_FLAG(MNT_NFS4ACLS); 4096 MNT_FLAG(MNT_UNION); 4097 MNT_FLAG(MNT_ASYNC); 4098 MNT_FLAG(MNT_SUIDDIR); 4099 MNT_FLAG(MNT_SOFTDEP); 4100 MNT_FLAG(MNT_NOSYMFOLLOW); 4101 MNT_FLAG(MNT_GJOURNAL); 4102 MNT_FLAG(MNT_MULTILABEL); 4103 MNT_FLAG(MNT_ACLS); 4104 MNT_FLAG(MNT_NOATIME); 4105 MNT_FLAG(MNT_NOCLUSTERR); 4106 MNT_FLAG(MNT_NOCLUSTERW); 4107 MNT_FLAG(MNT_SUJ); 4108 MNT_FLAG(MNT_EXRDONLY); 4109 MNT_FLAG(MNT_EXPORTED); 4110 MNT_FLAG(MNT_DEFEXPORTED); 4111 MNT_FLAG(MNT_EXPORTANON); 4112 MNT_FLAG(MNT_EXKERB); 4113 MNT_FLAG(MNT_EXPUBLIC); 4114 MNT_FLAG(MNT_LOCAL); 4115 MNT_FLAG(MNT_QUOTA); 4116 MNT_FLAG(MNT_ROOTFS); 4117 MNT_FLAG(MNT_USER); 4118 MNT_FLAG(MNT_IGNORE); 4119 MNT_FLAG(MNT_UPDATE); 4120 MNT_FLAG(MNT_DELEXPORT); 4121 MNT_FLAG(MNT_RELOAD); 4122 MNT_FLAG(MNT_FORCE); 4123 MNT_FLAG(MNT_SNAPSHOT); 4124 MNT_FLAG(MNT_BYFSID); 4125 #undef MNT_FLAG 4126 if (mflags != 0) { 4127 if (buf[0] != '\0') 4128 strlcat(buf, ", ", sizeof(buf)); 4129 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), 4130 "0x%016jx", mflags); 4131 } 4132 db_printf(" mnt_flag = %s\n", buf); 4133 4134 buf[0] = '\0'; 4135 flags = mp->mnt_kern_flag; 4136 #define MNT_KERN_FLAG(flag) do { \ 4137 if (flags & (flag)) { \ 4138 if (buf[0] != '\0') \ 4139 strlcat(buf, ", ", sizeof(buf)); \ 4140 strlcat(buf, (#flag) + 5, sizeof(buf)); \ 4141 flags &= ~(flag); \ 4142 } \ 4143 } while (0) 4144 MNT_KERN_FLAG(MNTK_UNMOUNTF); 4145 MNT_KERN_FLAG(MNTK_ASYNC); 4146 MNT_KERN_FLAG(MNTK_SOFTDEP); 4147 MNT_KERN_FLAG(MNTK_DRAINING); 4148 MNT_KERN_FLAG(MNTK_REFEXPIRE); 4149 MNT_KERN_FLAG(MNTK_EXTENDED_SHARED); 4150 MNT_KERN_FLAG(MNTK_SHARED_WRITES); 4151 MNT_KERN_FLAG(MNTK_NO_IOPF); 4152 MNT_KERN_FLAG(MNTK_VGONE_UPPER); 4153 MNT_KERN_FLAG(MNTK_VGONE_WAITER); 4154 MNT_KERN_FLAG(MNTK_LOOKUP_EXCL_DOTDOT); 4155 MNT_KERN_FLAG(MNTK_MARKER); 4156 MNT_KERN_FLAG(MNTK_USES_BCACHE); 4157 MNT_KERN_FLAG(MNTK_NOASYNC); 4158 MNT_KERN_FLAG(MNTK_UNMOUNT); 4159 MNT_KERN_FLAG(MNTK_MWAIT); 4160 MNT_KERN_FLAG(MNTK_SUSPEND); 4161 MNT_KERN_FLAG(MNTK_SUSPEND2); 4162 MNT_KERN_FLAG(MNTK_SUSPENDED); 4163 MNT_KERN_FLAG(MNTK_LOOKUP_SHARED); 4164 MNT_KERN_FLAG(MNTK_NOKNOTE); 4165 #undef MNT_KERN_FLAG 4166 if (flags != 0) { 4167 if (buf[0] != '\0') 4168 strlcat(buf, ", ", sizeof(buf)); 4169 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), 4170 "0x%08x", flags); 4171 } 4172 db_printf(" mnt_kern_flag = %s\n", buf); 4173 4174 db_printf(" mnt_opt = "); 4175 opt = TAILQ_FIRST(mp->mnt_opt); 4176 if (opt != NULL) { 4177 db_printf("%s", opt->name); 4178 opt = TAILQ_NEXT(opt, link); 4179 while (opt != NULL) { 4180 db_printf(", %s", opt->name); 4181 opt = TAILQ_NEXT(opt, link); 4182 } 4183 } 4184 db_printf("\n"); 4185 4186 sp = &mp->mnt_stat; 4187 db_printf(" mnt_stat = { version=%u type=%u flags=0x%016jx " 4188 "bsize=%ju iosize=%ju blocks=%ju bfree=%ju bavail=%jd files=%ju " 4189 "ffree=%jd syncwrites=%ju asyncwrites=%ju syncreads=%ju " 4190 "asyncreads=%ju namemax=%u owner=%u fsid=[%d, %d] }\n", 4191 (u_int)sp->f_version, (u_int)sp->f_type, (uintmax_t)sp->f_flags, 4192 (uintmax_t)sp->f_bsize, (uintmax_t)sp->f_iosize, 4193 (uintmax_t)sp->f_blocks, (uintmax_t)sp->f_bfree, 4194 (intmax_t)sp->f_bavail, (uintmax_t)sp->f_files, 4195 (intmax_t)sp->f_ffree, (uintmax_t)sp->f_syncwrites, 4196 (uintmax_t)sp->f_asyncwrites, (uintmax_t)sp->f_syncreads, 4197 (uintmax_t)sp->f_asyncreads, (u_int)sp->f_namemax, 4198 (u_int)sp->f_owner, (int)sp->f_fsid.val[0], (int)sp->f_fsid.val[1]); 4199 4200 db_printf(" mnt_cred = { uid=%u ruid=%u", 4201 (u_int)mp->mnt_cred->cr_uid, (u_int)mp->mnt_cred->cr_ruid); 4202 if (jailed(mp->mnt_cred)) 4203 db_printf(", jail=%d", mp->mnt_cred->cr_prison->pr_id); 4204 db_printf(" }\n"); 4205 db_printf(" mnt_ref = %d (with %d in the struct)\n", 4206 vfs_mount_fetch_counter(mp, MNT_COUNT_REF), mp->mnt_ref); 4207 db_printf(" mnt_gen = %d\n", mp->mnt_gen); 4208 db_printf(" mnt_nvnodelistsize = %d\n", mp->mnt_nvnodelistsize); 4209 db_printf(" mnt_lazyvnodelistsize = %d\n", 4210 mp->mnt_lazyvnodelistsize); 4211 db_printf(" mnt_writeopcount = %d (with %d in the struct)\n", 4212 vfs_mount_fetch_counter(mp, MNT_COUNT_WRITEOPCOUNT), mp->mnt_writeopcount); 4213 db_printf(" mnt_maxsymlinklen = %d\n", mp->mnt_maxsymlinklen); 4214 db_printf(" mnt_iosize_max = %d\n", mp->mnt_iosize_max); 4215 db_printf(" mnt_hashseed = %u\n", mp->mnt_hashseed); 4216 db_printf(" mnt_lockref = %d (with %d in the struct)\n", 4217 vfs_mount_fetch_counter(mp, MNT_COUNT_LOCKREF), mp->mnt_lockref); 4218 db_printf(" mnt_secondary_writes = %d\n", mp->mnt_secondary_writes); 4219 db_printf(" mnt_secondary_accwrites = %d\n", 4220 mp->mnt_secondary_accwrites); 4221 db_printf(" mnt_gjprovider = %s\n", 4222 mp->mnt_gjprovider != NULL ? mp->mnt_gjprovider : "NULL"); 4223 db_printf(" mnt_vfs_ops = %d\n", mp->mnt_vfs_ops); 4224 4225 db_printf("\n\nList of active vnodes\n"); 4226 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) { 4227 if (vp->v_type != VMARKER && vp->v_holdcnt > 0) { 4228 vn_printf(vp, "vnode "); 4229 if (db_pager_quit) 4230 break; 4231 } 4232 } 4233 db_printf("\n\nList of inactive vnodes\n"); 4234 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) { 4235 if (vp->v_type != VMARKER && vp->v_holdcnt == 0) { 4236 vn_printf(vp, "vnode "); 4237 if (db_pager_quit) 4238 break; 4239 } 4240 } 4241 } 4242 #endif /* DDB */ 4243 4244 /* 4245 * Fill in a struct xvfsconf based on a struct vfsconf. 4246 */ 4247 static int 4248 vfsconf2x(struct sysctl_req *req, struct vfsconf *vfsp) 4249 { 4250 struct xvfsconf xvfsp; 4251 4252 bzero(&xvfsp, sizeof(xvfsp)); 4253 strcpy(xvfsp.vfc_name, vfsp->vfc_name); 4254 xvfsp.vfc_typenum = vfsp->vfc_typenum; 4255 xvfsp.vfc_refcount = vfsp->vfc_refcount; 4256 xvfsp.vfc_flags = vfsp->vfc_flags; 4257 /* 4258 * These are unused in userland, we keep them 4259 * to not break binary compatibility. 4260 */ 4261 xvfsp.vfc_vfsops = NULL; 4262 xvfsp.vfc_next = NULL; 4263 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp))); 4264 } 4265 4266 #ifdef COMPAT_FREEBSD32 4267 struct xvfsconf32 { 4268 uint32_t vfc_vfsops; 4269 char vfc_name[MFSNAMELEN]; 4270 int32_t vfc_typenum; 4271 int32_t vfc_refcount; 4272 int32_t vfc_flags; 4273 uint32_t vfc_next; 4274 }; 4275 4276 static int 4277 vfsconf2x32(struct sysctl_req *req, struct vfsconf *vfsp) 4278 { 4279 struct xvfsconf32 xvfsp; 4280 4281 bzero(&xvfsp, sizeof(xvfsp)); 4282 strcpy(xvfsp.vfc_name, vfsp->vfc_name); 4283 xvfsp.vfc_typenum = vfsp->vfc_typenum; 4284 xvfsp.vfc_refcount = vfsp->vfc_refcount; 4285 xvfsp.vfc_flags = vfsp->vfc_flags; 4286 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp))); 4287 } 4288 #endif 4289 4290 /* 4291 * Top level filesystem related information gathering. 4292 */ 4293 static int 4294 sysctl_vfs_conflist(SYSCTL_HANDLER_ARGS) 4295 { 4296 struct vfsconf *vfsp; 4297 int error; 4298 4299 error = 0; 4300 vfsconf_slock(); 4301 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) { 4302 #ifdef COMPAT_FREEBSD32 4303 if (req->flags & SCTL_MASK32) 4304 error = vfsconf2x32(req, vfsp); 4305 else 4306 #endif 4307 error = vfsconf2x(req, vfsp); 4308 if (error) 4309 break; 4310 } 4311 vfsconf_sunlock(); 4312 return (error); 4313 } 4314 4315 SYSCTL_PROC(_vfs, OID_AUTO, conflist, CTLTYPE_OPAQUE | CTLFLAG_RD | 4316 CTLFLAG_MPSAFE, NULL, 0, sysctl_vfs_conflist, 4317 "S,xvfsconf", "List of all configured filesystems"); 4318 4319 #ifndef BURN_BRIDGES 4320 static int sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS); 4321 4322 static int 4323 vfs_sysctl(SYSCTL_HANDLER_ARGS) 4324 { 4325 int *name = (int *)arg1 - 1; /* XXX */ 4326 u_int namelen = arg2 + 1; /* XXX */ 4327 struct vfsconf *vfsp; 4328 4329 log(LOG_WARNING, "userland calling deprecated sysctl, " 4330 "please rebuild world\n"); 4331 4332 #if 1 || defined(COMPAT_PRELITE2) 4333 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */ 4334 if (namelen == 1) 4335 return (sysctl_ovfs_conf(oidp, arg1, arg2, req)); 4336 #endif 4337 4338 switch (name[1]) { 4339 case VFS_MAXTYPENUM: 4340 if (namelen != 2) 4341 return (ENOTDIR); 4342 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int))); 4343 case VFS_CONF: 4344 if (namelen != 3) 4345 return (ENOTDIR); /* overloaded */ 4346 vfsconf_slock(); 4347 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) { 4348 if (vfsp->vfc_typenum == name[2]) 4349 break; 4350 } 4351 vfsconf_sunlock(); 4352 if (vfsp == NULL) 4353 return (EOPNOTSUPP); 4354 #ifdef COMPAT_FREEBSD32 4355 if (req->flags & SCTL_MASK32) 4356 return (vfsconf2x32(req, vfsp)); 4357 else 4358 #endif 4359 return (vfsconf2x(req, vfsp)); 4360 } 4361 return (EOPNOTSUPP); 4362 } 4363 4364 static SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD | CTLFLAG_SKIP | 4365 CTLFLAG_MPSAFE, vfs_sysctl, 4366 "Generic filesystem"); 4367 4368 #if 1 || defined(COMPAT_PRELITE2) 4369 4370 static int 4371 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS) 4372 { 4373 int error; 4374 struct vfsconf *vfsp; 4375 struct ovfsconf ovfs; 4376 4377 vfsconf_slock(); 4378 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) { 4379 bzero(&ovfs, sizeof(ovfs)); 4380 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */ 4381 strcpy(ovfs.vfc_name, vfsp->vfc_name); 4382 ovfs.vfc_index = vfsp->vfc_typenum; 4383 ovfs.vfc_refcount = vfsp->vfc_refcount; 4384 ovfs.vfc_flags = vfsp->vfc_flags; 4385 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs); 4386 if (error != 0) { 4387 vfsconf_sunlock(); 4388 return (error); 4389 } 4390 } 4391 vfsconf_sunlock(); 4392 return (0); 4393 } 4394 4395 #endif /* 1 || COMPAT_PRELITE2 */ 4396 #endif /* !BURN_BRIDGES */ 4397 4398 #define KINFO_VNODESLOP 10 4399 #ifdef notyet 4400 /* 4401 * Dump vnode list (via sysctl). 4402 */ 4403 /* ARGSUSED */ 4404 static int 4405 sysctl_vnode(SYSCTL_HANDLER_ARGS) 4406 { 4407 struct xvnode *xvn; 4408 struct mount *mp; 4409 struct vnode *vp; 4410 int error, len, n; 4411 4412 /* 4413 * Stale numvnodes access is not fatal here. 4414 */ 4415 req->lock = 0; 4416 len = (numvnodes + KINFO_VNODESLOP) * sizeof *xvn; 4417 if (!req->oldptr) 4418 /* Make an estimate */ 4419 return (SYSCTL_OUT(req, 0, len)); 4420 4421 error = sysctl_wire_old_buffer(req, 0); 4422 if (error != 0) 4423 return (error); 4424 xvn = malloc(len, M_TEMP, M_ZERO | M_WAITOK); 4425 n = 0; 4426 mtx_lock(&mountlist_mtx); 4427 TAILQ_FOREACH(mp, &mountlist, mnt_list) { 4428 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) 4429 continue; 4430 MNT_ILOCK(mp); 4431 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) { 4432 if (n == len) 4433 break; 4434 vref(vp); 4435 xvn[n].xv_size = sizeof *xvn; 4436 xvn[n].xv_vnode = vp; 4437 xvn[n].xv_id = 0; /* XXX compat */ 4438 #define XV_COPY(field) xvn[n].xv_##field = vp->v_##field 4439 XV_COPY(usecount); 4440 XV_COPY(writecount); 4441 XV_COPY(holdcnt); 4442 XV_COPY(mount); 4443 XV_COPY(numoutput); 4444 XV_COPY(type); 4445 #undef XV_COPY 4446 xvn[n].xv_flag = vp->v_vflag; 4447 4448 switch (vp->v_type) { 4449 case VREG: 4450 case VDIR: 4451 case VLNK: 4452 break; 4453 case VBLK: 4454 case VCHR: 4455 if (vp->v_rdev == NULL) { 4456 vrele(vp); 4457 continue; 4458 } 4459 xvn[n].xv_dev = dev2udev(vp->v_rdev); 4460 break; 4461 case VSOCK: 4462 xvn[n].xv_socket = vp->v_socket; 4463 break; 4464 case VFIFO: 4465 xvn[n].xv_fifo = vp->v_fifoinfo; 4466 break; 4467 case VNON: 4468 case VBAD: 4469 default: 4470 /* shouldn't happen? */ 4471 vrele(vp); 4472 continue; 4473 } 4474 vrele(vp); 4475 ++n; 4476 } 4477 MNT_IUNLOCK(mp); 4478 mtx_lock(&mountlist_mtx); 4479 vfs_unbusy(mp); 4480 if (n == len) 4481 break; 4482 } 4483 mtx_unlock(&mountlist_mtx); 4484 4485 error = SYSCTL_OUT(req, xvn, n * sizeof *xvn); 4486 free(xvn, M_TEMP); 4487 return (error); 4488 } 4489 4490 SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE | CTLFLAG_RD | 4491 CTLFLAG_MPSAFE, 0, 0, sysctl_vnode, "S,xvnode", 4492 ""); 4493 #endif 4494 4495 static void 4496 unmount_or_warn(struct mount *mp) 4497 { 4498 int error; 4499 4500 error = dounmount(mp, MNT_FORCE, curthread); 4501 if (error != 0) { 4502 printf("unmount of %s failed (", mp->mnt_stat.f_mntonname); 4503 if (error == EBUSY) 4504 printf("BUSY)\n"); 4505 else 4506 printf("%d)\n", error); 4507 } 4508 } 4509 4510 /* 4511 * Unmount all filesystems. The list is traversed in reverse order 4512 * of mounting to avoid dependencies. 4513 */ 4514 void 4515 vfs_unmountall(void) 4516 { 4517 struct mount *mp, *tmp; 4518 4519 CTR1(KTR_VFS, "%s: unmounting all filesystems", __func__); 4520 4521 /* 4522 * Since this only runs when rebooting, it is not interlocked. 4523 */ 4524 TAILQ_FOREACH_REVERSE_SAFE(mp, &mountlist, mntlist, mnt_list, tmp) { 4525 vfs_ref(mp); 4526 4527 /* 4528 * Forcibly unmounting "/dev" before "/" would prevent clean 4529 * unmount of the latter. 4530 */ 4531 if (mp == rootdevmp) 4532 continue; 4533 4534 unmount_or_warn(mp); 4535 } 4536 4537 if (rootdevmp != NULL) 4538 unmount_or_warn(rootdevmp); 4539 } 4540 4541 static void 4542 vfs_deferred_inactive(struct vnode *vp, int lkflags) 4543 { 4544 4545 ASSERT_VI_LOCKED(vp, __func__); 4546 VNASSERT((vp->v_iflag & VI_DEFINACT) == 0, vp, ("VI_DEFINACT still set")); 4547 if ((vp->v_iflag & VI_OWEINACT) == 0) { 4548 vdropl(vp); 4549 return; 4550 } 4551 if (vn_lock(vp, lkflags) == 0) { 4552 VI_LOCK(vp); 4553 if ((vp->v_iflag & (VI_OWEINACT | VI_DOINGINACT)) == VI_OWEINACT) 4554 vinactive(vp); 4555 VOP_UNLOCK(vp); 4556 vdropl(vp); 4557 return; 4558 } 4559 vdefer_inactive_cond(vp); 4560 } 4561 4562 static int 4563 vfs_periodic_inactive_filter(struct vnode *vp, void *arg) 4564 { 4565 4566 return (vp->v_iflag & VI_DEFINACT); 4567 } 4568 4569 static void __noinline 4570 vfs_periodic_inactive(struct mount *mp, int flags) 4571 { 4572 struct vnode *vp, *mvp; 4573 int lkflags; 4574 4575 lkflags = LK_EXCLUSIVE | LK_INTERLOCK; 4576 if (flags != MNT_WAIT) 4577 lkflags |= LK_NOWAIT; 4578 4579 MNT_VNODE_FOREACH_LAZY(vp, mp, mvp, vfs_periodic_inactive_filter, NULL) { 4580 if ((vp->v_iflag & VI_DEFINACT) == 0) { 4581 VI_UNLOCK(vp); 4582 continue; 4583 } 4584 vp->v_iflag &= ~VI_DEFINACT; 4585 vfs_deferred_inactive(vp, lkflags); 4586 } 4587 } 4588 4589 static inline bool 4590 vfs_want_msync(struct vnode *vp) 4591 { 4592 struct vm_object *obj; 4593 4594 /* 4595 * This test may be performed without any locks held. 4596 * We rely on vm_object's type stability. 4597 */ 4598 if (vp->v_vflag & VV_NOSYNC) 4599 return (false); 4600 obj = vp->v_object; 4601 return (obj != NULL && vm_object_mightbedirty(obj)); 4602 } 4603 4604 static int 4605 vfs_periodic_msync_inactive_filter(struct vnode *vp, void *arg __unused) 4606 { 4607 4608 if (vp->v_vflag & VV_NOSYNC) 4609 return (false); 4610 if (vp->v_iflag & VI_DEFINACT) 4611 return (true); 4612 return (vfs_want_msync(vp)); 4613 } 4614 4615 static void __noinline 4616 vfs_periodic_msync_inactive(struct mount *mp, int flags) 4617 { 4618 struct vnode *vp, *mvp; 4619 struct vm_object *obj; 4620 struct thread *td; 4621 int lkflags, objflags; 4622 bool seen_defer; 4623 4624 td = curthread; 4625 4626 lkflags = LK_EXCLUSIVE | LK_INTERLOCK; 4627 if (flags != MNT_WAIT) { 4628 lkflags |= LK_NOWAIT; 4629 objflags = OBJPC_NOSYNC; 4630 } else { 4631 objflags = OBJPC_SYNC; 4632 } 4633 4634 MNT_VNODE_FOREACH_LAZY(vp, mp, mvp, vfs_periodic_msync_inactive_filter, NULL) { 4635 seen_defer = false; 4636 if (vp->v_iflag & VI_DEFINACT) { 4637 vp->v_iflag &= ~VI_DEFINACT; 4638 seen_defer = true; 4639 } 4640 if (!vfs_want_msync(vp)) { 4641 if (seen_defer) 4642 vfs_deferred_inactive(vp, lkflags); 4643 else 4644 VI_UNLOCK(vp); 4645 continue; 4646 } 4647 if (vget(vp, lkflags, td) == 0) { 4648 obj = vp->v_object; 4649 if (obj != NULL && (vp->v_vflag & VV_NOSYNC) == 0) { 4650 VM_OBJECT_WLOCK(obj); 4651 vm_object_page_clean(obj, 0, 0, objflags); 4652 VM_OBJECT_WUNLOCK(obj); 4653 } 4654 vput(vp); 4655 if (seen_defer) 4656 vdrop(vp); 4657 } else { 4658 if (seen_defer) 4659 vdefer_inactive_cond(vp); 4660 } 4661 } 4662 } 4663 4664 void 4665 vfs_periodic(struct mount *mp, int flags) 4666 { 4667 4668 CTR2(KTR_VFS, "%s: mp %p", __func__, mp); 4669 4670 if ((mp->mnt_kern_flag & MNTK_NOMSYNC) != 0) 4671 vfs_periodic_inactive(mp, flags); 4672 else 4673 vfs_periodic_msync_inactive(mp, flags); 4674 } 4675 4676 static void 4677 destroy_vpollinfo_free(struct vpollinfo *vi) 4678 { 4679 4680 knlist_destroy(&vi->vpi_selinfo.si_note); 4681 mtx_destroy(&vi->vpi_lock); 4682 uma_zfree(vnodepoll_zone, vi); 4683 } 4684 4685 static void 4686 destroy_vpollinfo(struct vpollinfo *vi) 4687 { 4688 4689 knlist_clear(&vi->vpi_selinfo.si_note, 1); 4690 seldrain(&vi->vpi_selinfo); 4691 destroy_vpollinfo_free(vi); 4692 } 4693 4694 /* 4695 * Initialize per-vnode helper structure to hold poll-related state. 4696 */ 4697 void 4698 v_addpollinfo(struct vnode *vp) 4699 { 4700 struct vpollinfo *vi; 4701 4702 if (vp->v_pollinfo != NULL) 4703 return; 4704 vi = uma_zalloc(vnodepoll_zone, M_WAITOK | M_ZERO); 4705 mtx_init(&vi->vpi_lock, "vnode pollinfo", NULL, MTX_DEF); 4706 knlist_init(&vi->vpi_selinfo.si_note, vp, vfs_knllock, 4707 vfs_knlunlock, vfs_knl_assert_locked, vfs_knl_assert_unlocked); 4708 VI_LOCK(vp); 4709 if (vp->v_pollinfo != NULL) { 4710 VI_UNLOCK(vp); 4711 destroy_vpollinfo_free(vi); 4712 return; 4713 } 4714 vp->v_pollinfo = vi; 4715 VI_UNLOCK(vp); 4716 } 4717 4718 /* 4719 * Record a process's interest in events which might happen to 4720 * a vnode. Because poll uses the historic select-style interface 4721 * internally, this routine serves as both the ``check for any 4722 * pending events'' and the ``record my interest in future events'' 4723 * functions. (These are done together, while the lock is held, 4724 * to avoid race conditions.) 4725 */ 4726 int 4727 vn_pollrecord(struct vnode *vp, struct thread *td, int events) 4728 { 4729 4730 v_addpollinfo(vp); 4731 mtx_lock(&vp->v_pollinfo->vpi_lock); 4732 if (vp->v_pollinfo->vpi_revents & events) { 4733 /* 4734 * This leaves events we are not interested 4735 * in available for the other process which 4736 * which presumably had requested them 4737 * (otherwise they would never have been 4738 * recorded). 4739 */ 4740 events &= vp->v_pollinfo->vpi_revents; 4741 vp->v_pollinfo->vpi_revents &= ~events; 4742 4743 mtx_unlock(&vp->v_pollinfo->vpi_lock); 4744 return (events); 4745 } 4746 vp->v_pollinfo->vpi_events |= events; 4747 selrecord(td, &vp->v_pollinfo->vpi_selinfo); 4748 mtx_unlock(&vp->v_pollinfo->vpi_lock); 4749 return (0); 4750 } 4751 4752 /* 4753 * Routine to create and manage a filesystem syncer vnode. 4754 */ 4755 #define sync_close ((int (*)(struct vop_close_args *))nullop) 4756 static int sync_fsync(struct vop_fsync_args *); 4757 static int sync_inactive(struct vop_inactive_args *); 4758 static int sync_reclaim(struct vop_reclaim_args *); 4759 4760 static struct vop_vector sync_vnodeops = { 4761 .vop_bypass = VOP_EOPNOTSUPP, 4762 .vop_close = sync_close, /* close */ 4763 .vop_fsync = sync_fsync, /* fsync */ 4764 .vop_inactive = sync_inactive, /* inactive */ 4765 .vop_need_inactive = vop_stdneed_inactive, /* need_inactive */ 4766 .vop_reclaim = sync_reclaim, /* reclaim */ 4767 .vop_lock1 = vop_stdlock, /* lock */ 4768 .vop_unlock = vop_stdunlock, /* unlock */ 4769 .vop_islocked = vop_stdislocked, /* islocked */ 4770 }; 4771 VFS_VOP_VECTOR_REGISTER(sync_vnodeops); 4772 4773 /* 4774 * Create a new filesystem syncer vnode for the specified mount point. 4775 */ 4776 void 4777 vfs_allocate_syncvnode(struct mount *mp) 4778 { 4779 struct vnode *vp; 4780 struct bufobj *bo; 4781 static long start, incr, next; 4782 int error; 4783 4784 /* Allocate a new vnode */ 4785 error = getnewvnode("syncer", mp, &sync_vnodeops, &vp); 4786 if (error != 0) 4787 panic("vfs_allocate_syncvnode: getnewvnode() failed"); 4788 vp->v_type = VNON; 4789 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 4790 vp->v_vflag |= VV_FORCEINSMQ; 4791 error = insmntque(vp, mp); 4792 if (error != 0) 4793 panic("vfs_allocate_syncvnode: insmntque() failed"); 4794 vp->v_vflag &= ~VV_FORCEINSMQ; 4795 VOP_UNLOCK(vp); 4796 /* 4797 * Place the vnode onto the syncer worklist. We attempt to 4798 * scatter them about on the list so that they will go off 4799 * at evenly distributed times even if all the filesystems 4800 * are mounted at once. 4801 */ 4802 next += incr; 4803 if (next == 0 || next > syncer_maxdelay) { 4804 start /= 2; 4805 incr /= 2; 4806 if (start == 0) { 4807 start = syncer_maxdelay / 2; 4808 incr = syncer_maxdelay; 4809 } 4810 next = start; 4811 } 4812 bo = &vp->v_bufobj; 4813 BO_LOCK(bo); 4814 vn_syncer_add_to_worklist(bo, syncdelay > 0 ? next % syncdelay : 0); 4815 /* XXX - vn_syncer_add_to_worklist() also grabs and drops sync_mtx. */ 4816 mtx_lock(&sync_mtx); 4817 sync_vnode_count++; 4818 if (mp->mnt_syncer == NULL) { 4819 mp->mnt_syncer = vp; 4820 vp = NULL; 4821 } 4822 mtx_unlock(&sync_mtx); 4823 BO_UNLOCK(bo); 4824 if (vp != NULL) { 4825 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 4826 vgone(vp); 4827 vput(vp); 4828 } 4829 } 4830 4831 void 4832 vfs_deallocate_syncvnode(struct mount *mp) 4833 { 4834 struct vnode *vp; 4835 4836 mtx_lock(&sync_mtx); 4837 vp = mp->mnt_syncer; 4838 if (vp != NULL) 4839 mp->mnt_syncer = NULL; 4840 mtx_unlock(&sync_mtx); 4841 if (vp != NULL) 4842 vrele(vp); 4843 } 4844 4845 /* 4846 * Do a lazy sync of the filesystem. 4847 */ 4848 static int 4849 sync_fsync(struct vop_fsync_args *ap) 4850 { 4851 struct vnode *syncvp = ap->a_vp; 4852 struct mount *mp = syncvp->v_mount; 4853 int error, save; 4854 struct bufobj *bo; 4855 4856 /* 4857 * We only need to do something if this is a lazy evaluation. 4858 */ 4859 if (ap->a_waitfor != MNT_LAZY) 4860 return (0); 4861 4862 /* 4863 * Move ourselves to the back of the sync list. 4864 */ 4865 bo = &syncvp->v_bufobj; 4866 BO_LOCK(bo); 4867 vn_syncer_add_to_worklist(bo, syncdelay); 4868 BO_UNLOCK(bo); 4869 4870 /* 4871 * Walk the list of vnodes pushing all that are dirty and 4872 * not already on the sync list. 4873 */ 4874 if (vfs_busy(mp, MBF_NOWAIT) != 0) 4875 return (0); 4876 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) { 4877 vfs_unbusy(mp); 4878 return (0); 4879 } 4880 save = curthread_pflags_set(TDP_SYNCIO); 4881 /* 4882 * The filesystem at hand may be idle with free vnodes stored in the 4883 * batch. Return them instead of letting them stay there indefinitely. 4884 */ 4885 vfs_periodic(mp, MNT_NOWAIT); 4886 error = VFS_SYNC(mp, MNT_LAZY); 4887 curthread_pflags_restore(save); 4888 vn_finished_write(mp); 4889 vfs_unbusy(mp); 4890 return (error); 4891 } 4892 4893 /* 4894 * The syncer vnode is no referenced. 4895 */ 4896 static int 4897 sync_inactive(struct vop_inactive_args *ap) 4898 { 4899 4900 vgone(ap->a_vp); 4901 return (0); 4902 } 4903 4904 /* 4905 * The syncer vnode is no longer needed and is being decommissioned. 4906 * 4907 * Modifications to the worklist must be protected by sync_mtx. 4908 */ 4909 static int 4910 sync_reclaim(struct vop_reclaim_args *ap) 4911 { 4912 struct vnode *vp = ap->a_vp; 4913 struct bufobj *bo; 4914 4915 bo = &vp->v_bufobj; 4916 BO_LOCK(bo); 4917 mtx_lock(&sync_mtx); 4918 if (vp->v_mount->mnt_syncer == vp) 4919 vp->v_mount->mnt_syncer = NULL; 4920 if (bo->bo_flag & BO_ONWORKLST) { 4921 LIST_REMOVE(bo, bo_synclist); 4922 syncer_worklist_len--; 4923 sync_vnode_count--; 4924 bo->bo_flag &= ~BO_ONWORKLST; 4925 } 4926 mtx_unlock(&sync_mtx); 4927 BO_UNLOCK(bo); 4928 4929 return (0); 4930 } 4931 4932 int 4933 vn_need_pageq_flush(struct vnode *vp) 4934 { 4935 struct vm_object *obj; 4936 int need; 4937 4938 MPASS(mtx_owned(VI_MTX(vp))); 4939 need = 0; 4940 if ((obj = vp->v_object) != NULL && (vp->v_vflag & VV_NOSYNC) == 0 && 4941 vm_object_mightbedirty(obj)) 4942 need = 1; 4943 return (need); 4944 } 4945 4946 /* 4947 * Check if vnode represents a disk device 4948 */ 4949 int 4950 vn_isdisk(struct vnode *vp, int *errp) 4951 { 4952 int error; 4953 4954 if (vp->v_type != VCHR) { 4955 error = ENOTBLK; 4956 goto out; 4957 } 4958 error = 0; 4959 dev_lock(); 4960 if (vp->v_rdev == NULL) 4961 error = ENXIO; 4962 else if (vp->v_rdev->si_devsw == NULL) 4963 error = ENXIO; 4964 else if (!(vp->v_rdev->si_devsw->d_flags & D_DISK)) 4965 error = ENOTBLK; 4966 dev_unlock(); 4967 out: 4968 if (errp != NULL) 4969 *errp = error; 4970 return (error == 0); 4971 } 4972 4973 /* 4974 * Common filesystem object access control check routine. Accepts a 4975 * vnode's type, "mode", uid and gid, requested access mode, credentials, 4976 * and optional call-by-reference privused argument allowing vaccess() 4977 * to indicate to the caller whether privilege was used to satisfy the 4978 * request (obsoleted). Returns 0 on success, or an errno on failure. 4979 */ 4980 int 4981 vaccess(enum vtype type, mode_t file_mode, uid_t file_uid, gid_t file_gid, 4982 accmode_t accmode, struct ucred *cred, int *privused) 4983 { 4984 accmode_t dac_granted; 4985 accmode_t priv_granted; 4986 4987 KASSERT((accmode & ~(VEXEC | VWRITE | VREAD | VADMIN | VAPPEND)) == 0, 4988 ("invalid bit in accmode")); 4989 KASSERT((accmode & VAPPEND) == 0 || (accmode & VWRITE), 4990 ("VAPPEND without VWRITE")); 4991 4992 /* 4993 * Look for a normal, non-privileged way to access the file/directory 4994 * as requested. If it exists, go with that. 4995 */ 4996 4997 if (privused != NULL) 4998 *privused = 0; 4999 5000 dac_granted = 0; 5001 5002 /* Check the owner. */ 5003 if (cred->cr_uid == file_uid) { 5004 dac_granted |= VADMIN; 5005 if (file_mode & S_IXUSR) 5006 dac_granted |= VEXEC; 5007 if (file_mode & S_IRUSR) 5008 dac_granted |= VREAD; 5009 if (file_mode & S_IWUSR) 5010 dac_granted |= (VWRITE | VAPPEND); 5011 5012 if ((accmode & dac_granted) == accmode) 5013 return (0); 5014 5015 goto privcheck; 5016 } 5017 5018 /* Otherwise, check the groups (first match) */ 5019 if (groupmember(file_gid, cred)) { 5020 if (file_mode & S_IXGRP) 5021 dac_granted |= VEXEC; 5022 if (file_mode & S_IRGRP) 5023 dac_granted |= VREAD; 5024 if (file_mode & S_IWGRP) 5025 dac_granted |= (VWRITE | VAPPEND); 5026 5027 if ((accmode & dac_granted) == accmode) 5028 return (0); 5029 5030 goto privcheck; 5031 } 5032 5033 /* Otherwise, check everyone else. */ 5034 if (file_mode & S_IXOTH) 5035 dac_granted |= VEXEC; 5036 if (file_mode & S_IROTH) 5037 dac_granted |= VREAD; 5038 if (file_mode & S_IWOTH) 5039 dac_granted |= (VWRITE | VAPPEND); 5040 if ((accmode & dac_granted) == accmode) 5041 return (0); 5042 5043 privcheck: 5044 /* 5045 * Build a privilege mask to determine if the set of privileges 5046 * satisfies the requirements when combined with the granted mask 5047 * from above. For each privilege, if the privilege is required, 5048 * bitwise or the request type onto the priv_granted mask. 5049 */ 5050 priv_granted = 0; 5051 5052 if (type == VDIR) { 5053 /* 5054 * For directories, use PRIV_VFS_LOOKUP to satisfy VEXEC 5055 * requests, instead of PRIV_VFS_EXEC. 5056 */ 5057 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) && 5058 !priv_check_cred(cred, PRIV_VFS_LOOKUP)) 5059 priv_granted |= VEXEC; 5060 } else { 5061 /* 5062 * Ensure that at least one execute bit is on. Otherwise, 5063 * a privileged user will always succeed, and we don't want 5064 * this to happen unless the file really is executable. 5065 */ 5066 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) && 5067 (file_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0 && 5068 !priv_check_cred(cred, PRIV_VFS_EXEC)) 5069 priv_granted |= VEXEC; 5070 } 5071 5072 if ((accmode & VREAD) && ((dac_granted & VREAD) == 0) && 5073 !priv_check_cred(cred, PRIV_VFS_READ)) 5074 priv_granted |= VREAD; 5075 5076 if ((accmode & VWRITE) && ((dac_granted & VWRITE) == 0) && 5077 !priv_check_cred(cred, PRIV_VFS_WRITE)) 5078 priv_granted |= (VWRITE | VAPPEND); 5079 5080 if ((accmode & VADMIN) && ((dac_granted & VADMIN) == 0) && 5081 !priv_check_cred(cred, PRIV_VFS_ADMIN)) 5082 priv_granted |= VADMIN; 5083 5084 if ((accmode & (priv_granted | dac_granted)) == accmode) { 5085 /* XXX audit: privilege used */ 5086 if (privused != NULL) 5087 *privused = 1; 5088 return (0); 5089 } 5090 5091 return ((accmode & VADMIN) ? EPERM : EACCES); 5092 } 5093 5094 /* 5095 * Credential check based on process requesting service, and per-attribute 5096 * permissions. 5097 */ 5098 int 5099 extattr_check_cred(struct vnode *vp, int attrnamespace, struct ucred *cred, 5100 struct thread *td, accmode_t accmode) 5101 { 5102 5103 /* 5104 * Kernel-invoked always succeeds. 5105 */ 5106 if (cred == NOCRED) 5107 return (0); 5108 5109 /* 5110 * Do not allow privileged processes in jail to directly manipulate 5111 * system attributes. 5112 */ 5113 switch (attrnamespace) { 5114 case EXTATTR_NAMESPACE_SYSTEM: 5115 /* Potentially should be: return (EPERM); */ 5116 return (priv_check_cred(cred, PRIV_VFS_EXTATTR_SYSTEM)); 5117 case EXTATTR_NAMESPACE_USER: 5118 return (VOP_ACCESS(vp, accmode, cred, td)); 5119 default: 5120 return (EPERM); 5121 } 5122 } 5123 5124 #ifdef DEBUG_VFS_LOCKS 5125 /* 5126 * This only exists to suppress warnings from unlocked specfs accesses. It is 5127 * no longer ok to have an unlocked VFS. 5128 */ 5129 #define IGNORE_LOCK(vp) (KERNEL_PANICKED() || (vp) == NULL || \ 5130 (vp)->v_type == VCHR || (vp)->v_type == VBAD) 5131 5132 int vfs_badlock_ddb = 1; /* Drop into debugger on violation. */ 5133 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_ddb, CTLFLAG_RW, &vfs_badlock_ddb, 0, 5134 "Drop into debugger on lock violation"); 5135 5136 int vfs_badlock_mutex = 1; /* Check for interlock across VOPs. */ 5137 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_mutex, CTLFLAG_RW, &vfs_badlock_mutex, 5138 0, "Check for interlock across VOPs"); 5139 5140 int vfs_badlock_print = 1; /* Print lock violations. */ 5141 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_print, CTLFLAG_RW, &vfs_badlock_print, 5142 0, "Print lock violations"); 5143 5144 int vfs_badlock_vnode = 1; /* Print vnode details on lock violations. */ 5145 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_vnode, CTLFLAG_RW, &vfs_badlock_vnode, 5146 0, "Print vnode details on lock violations"); 5147 5148 #ifdef KDB 5149 int vfs_badlock_backtrace = 1; /* Print backtrace at lock violations. */ 5150 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_backtrace, CTLFLAG_RW, 5151 &vfs_badlock_backtrace, 0, "Print backtrace at lock violations"); 5152 #endif 5153 5154 static void 5155 vfs_badlock(const char *msg, const char *str, struct vnode *vp) 5156 { 5157 5158 #ifdef KDB 5159 if (vfs_badlock_backtrace) 5160 kdb_backtrace(); 5161 #endif 5162 if (vfs_badlock_vnode) 5163 vn_printf(vp, "vnode "); 5164 if (vfs_badlock_print) 5165 printf("%s: %p %s\n", str, (void *)vp, msg); 5166 if (vfs_badlock_ddb) 5167 kdb_enter(KDB_WHY_VFSLOCK, "lock violation"); 5168 } 5169 5170 void 5171 assert_vi_locked(struct vnode *vp, const char *str) 5172 { 5173 5174 if (vfs_badlock_mutex && !mtx_owned(VI_MTX(vp))) 5175 vfs_badlock("interlock is not locked but should be", str, vp); 5176 } 5177 5178 void 5179 assert_vi_unlocked(struct vnode *vp, const char *str) 5180 { 5181 5182 if (vfs_badlock_mutex && mtx_owned(VI_MTX(vp))) 5183 vfs_badlock("interlock is locked but should not be", str, vp); 5184 } 5185 5186 void 5187 assert_vop_locked(struct vnode *vp, const char *str) 5188 { 5189 int locked; 5190 5191 if (!IGNORE_LOCK(vp)) { 5192 locked = VOP_ISLOCKED(vp); 5193 if (locked == 0 || locked == LK_EXCLOTHER) 5194 vfs_badlock("is not locked but should be", str, vp); 5195 } 5196 } 5197 5198 void 5199 assert_vop_unlocked(struct vnode *vp, const char *str) 5200 { 5201 5202 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) == LK_EXCLUSIVE) 5203 vfs_badlock("is locked but should not be", str, vp); 5204 } 5205 5206 void 5207 assert_vop_elocked(struct vnode *vp, const char *str) 5208 { 5209 5210 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_EXCLUSIVE) 5211 vfs_badlock("is not exclusive locked but should be", str, vp); 5212 } 5213 #endif /* DEBUG_VFS_LOCKS */ 5214 5215 void 5216 vop_rename_fail(struct vop_rename_args *ap) 5217 { 5218 5219 if (ap->a_tvp != NULL) 5220 vput(ap->a_tvp); 5221 if (ap->a_tdvp == ap->a_tvp) 5222 vrele(ap->a_tdvp); 5223 else 5224 vput(ap->a_tdvp); 5225 vrele(ap->a_fdvp); 5226 vrele(ap->a_fvp); 5227 } 5228 5229 void 5230 vop_rename_pre(void *ap) 5231 { 5232 struct vop_rename_args *a = ap; 5233 5234 #ifdef DEBUG_VFS_LOCKS 5235 if (a->a_tvp) 5236 ASSERT_VI_UNLOCKED(a->a_tvp, "VOP_RENAME"); 5237 ASSERT_VI_UNLOCKED(a->a_tdvp, "VOP_RENAME"); 5238 ASSERT_VI_UNLOCKED(a->a_fvp, "VOP_RENAME"); 5239 ASSERT_VI_UNLOCKED(a->a_fdvp, "VOP_RENAME"); 5240 5241 /* Check the source (from). */ 5242 if (a->a_tdvp->v_vnlock != a->a_fdvp->v_vnlock && 5243 (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fdvp->v_vnlock)) 5244 ASSERT_VOP_UNLOCKED(a->a_fdvp, "vop_rename: fdvp locked"); 5245 if (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fvp->v_vnlock) 5246 ASSERT_VOP_UNLOCKED(a->a_fvp, "vop_rename: fvp locked"); 5247 5248 /* Check the target. */ 5249 if (a->a_tvp) 5250 ASSERT_VOP_LOCKED(a->a_tvp, "vop_rename: tvp not locked"); 5251 ASSERT_VOP_LOCKED(a->a_tdvp, "vop_rename: tdvp not locked"); 5252 #endif 5253 if (a->a_tdvp != a->a_fdvp) 5254 vhold(a->a_fdvp); 5255 if (a->a_tvp != a->a_fvp) 5256 vhold(a->a_fvp); 5257 vhold(a->a_tdvp); 5258 if (a->a_tvp) 5259 vhold(a->a_tvp); 5260 } 5261 5262 #ifdef DEBUG_VFS_LOCKS 5263 void 5264 vop_strategy_pre(void *ap) 5265 { 5266 struct vop_strategy_args *a; 5267 struct buf *bp; 5268 5269 a = ap; 5270 bp = a->a_bp; 5271 5272 /* 5273 * Cluster ops lock their component buffers but not the IO container. 5274 */ 5275 if ((bp->b_flags & B_CLUSTER) != 0) 5276 return; 5277 5278 if (!KERNEL_PANICKED() && !BUF_ISLOCKED(bp)) { 5279 if (vfs_badlock_print) 5280 printf( 5281 "VOP_STRATEGY: bp is not locked but should be\n"); 5282 if (vfs_badlock_ddb) 5283 kdb_enter(KDB_WHY_VFSLOCK, "lock violation"); 5284 } 5285 } 5286 5287 void 5288 vop_lock_pre(void *ap) 5289 { 5290 struct vop_lock1_args *a = ap; 5291 5292 if ((a->a_flags & LK_INTERLOCK) == 0) 5293 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK"); 5294 else 5295 ASSERT_VI_LOCKED(a->a_vp, "VOP_LOCK"); 5296 } 5297 5298 void 5299 vop_lock_post(void *ap, int rc) 5300 { 5301 struct vop_lock1_args *a = ap; 5302 5303 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK"); 5304 if (rc == 0 && (a->a_flags & LK_EXCLOTHER) == 0) 5305 ASSERT_VOP_LOCKED(a->a_vp, "VOP_LOCK"); 5306 } 5307 5308 void 5309 vop_unlock_pre(void *ap) 5310 { 5311 struct vop_unlock_args *a = ap; 5312 5313 ASSERT_VOP_LOCKED(a->a_vp, "VOP_UNLOCK"); 5314 } 5315 5316 void 5317 vop_unlock_post(void *ap, int rc) 5318 { 5319 return; 5320 } 5321 5322 void 5323 vop_need_inactive_pre(void *ap) 5324 { 5325 struct vop_need_inactive_args *a = ap; 5326 5327 ASSERT_VI_LOCKED(a->a_vp, "VOP_NEED_INACTIVE"); 5328 } 5329 5330 void 5331 vop_need_inactive_post(void *ap, int rc) 5332 { 5333 struct vop_need_inactive_args *a = ap; 5334 5335 ASSERT_VI_LOCKED(a->a_vp, "VOP_NEED_INACTIVE"); 5336 } 5337 #endif 5338 5339 void 5340 vop_create_post(void *ap, int rc) 5341 { 5342 struct vop_create_args *a = ap; 5343 5344 if (!rc) 5345 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE); 5346 } 5347 5348 void 5349 vop_deleteextattr_post(void *ap, int rc) 5350 { 5351 struct vop_deleteextattr_args *a = ap; 5352 5353 if (!rc) 5354 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB); 5355 } 5356 5357 void 5358 vop_link_post(void *ap, int rc) 5359 { 5360 struct vop_link_args *a = ap; 5361 5362 if (!rc) { 5363 VFS_KNOTE_LOCKED(a->a_vp, NOTE_LINK); 5364 VFS_KNOTE_LOCKED(a->a_tdvp, NOTE_WRITE); 5365 } 5366 } 5367 5368 void 5369 vop_mkdir_post(void *ap, int rc) 5370 { 5371 struct vop_mkdir_args *a = ap; 5372 5373 if (!rc) 5374 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK); 5375 } 5376 5377 void 5378 vop_mknod_post(void *ap, int rc) 5379 { 5380 struct vop_mknod_args *a = ap; 5381 5382 if (!rc) 5383 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE); 5384 } 5385 5386 void 5387 vop_reclaim_post(void *ap, int rc) 5388 { 5389 struct vop_reclaim_args *a = ap; 5390 5391 if (!rc) 5392 VFS_KNOTE_LOCKED(a->a_vp, NOTE_REVOKE); 5393 } 5394 5395 void 5396 vop_remove_post(void *ap, int rc) 5397 { 5398 struct vop_remove_args *a = ap; 5399 5400 if (!rc) { 5401 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE); 5402 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE); 5403 } 5404 } 5405 5406 void 5407 vop_rename_post(void *ap, int rc) 5408 { 5409 struct vop_rename_args *a = ap; 5410 long hint; 5411 5412 if (!rc) { 5413 hint = NOTE_WRITE; 5414 if (a->a_fdvp == a->a_tdvp) { 5415 if (a->a_tvp != NULL && a->a_tvp->v_type == VDIR) 5416 hint |= NOTE_LINK; 5417 VFS_KNOTE_UNLOCKED(a->a_fdvp, hint); 5418 VFS_KNOTE_UNLOCKED(a->a_tdvp, hint); 5419 } else { 5420 hint |= NOTE_EXTEND; 5421 if (a->a_fvp->v_type == VDIR) 5422 hint |= NOTE_LINK; 5423 VFS_KNOTE_UNLOCKED(a->a_fdvp, hint); 5424 5425 if (a->a_fvp->v_type == VDIR && a->a_tvp != NULL && 5426 a->a_tvp->v_type == VDIR) 5427 hint &= ~NOTE_LINK; 5428 VFS_KNOTE_UNLOCKED(a->a_tdvp, hint); 5429 } 5430 5431 VFS_KNOTE_UNLOCKED(a->a_fvp, NOTE_RENAME); 5432 if (a->a_tvp) 5433 VFS_KNOTE_UNLOCKED(a->a_tvp, NOTE_DELETE); 5434 } 5435 if (a->a_tdvp != a->a_fdvp) 5436 vdrop(a->a_fdvp); 5437 if (a->a_tvp != a->a_fvp) 5438 vdrop(a->a_fvp); 5439 vdrop(a->a_tdvp); 5440 if (a->a_tvp) 5441 vdrop(a->a_tvp); 5442 } 5443 5444 void 5445 vop_rmdir_post(void *ap, int rc) 5446 { 5447 struct vop_rmdir_args *a = ap; 5448 5449 if (!rc) { 5450 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK); 5451 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE); 5452 } 5453 } 5454 5455 void 5456 vop_setattr_post(void *ap, int rc) 5457 { 5458 struct vop_setattr_args *a = ap; 5459 5460 if (!rc) 5461 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB); 5462 } 5463 5464 void 5465 vop_setextattr_post(void *ap, int rc) 5466 { 5467 struct vop_setextattr_args *a = ap; 5468 5469 if (!rc) 5470 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB); 5471 } 5472 5473 void 5474 vop_symlink_post(void *ap, int rc) 5475 { 5476 struct vop_symlink_args *a = ap; 5477 5478 if (!rc) 5479 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE); 5480 } 5481 5482 void 5483 vop_open_post(void *ap, int rc) 5484 { 5485 struct vop_open_args *a = ap; 5486 5487 if (!rc) 5488 VFS_KNOTE_LOCKED(a->a_vp, NOTE_OPEN); 5489 } 5490 5491 void 5492 vop_close_post(void *ap, int rc) 5493 { 5494 struct vop_close_args *a = ap; 5495 5496 if (!rc && (a->a_cred != NOCRED || /* filter out revokes */ 5497 !VN_IS_DOOMED(a->a_vp))) { 5498 VFS_KNOTE_LOCKED(a->a_vp, (a->a_fflag & FWRITE) != 0 ? 5499 NOTE_CLOSE_WRITE : NOTE_CLOSE); 5500 } 5501 } 5502 5503 void 5504 vop_read_post(void *ap, int rc) 5505 { 5506 struct vop_read_args *a = ap; 5507 5508 if (!rc) 5509 VFS_KNOTE_LOCKED(a->a_vp, NOTE_READ); 5510 } 5511 5512 void 5513 vop_readdir_post(void *ap, int rc) 5514 { 5515 struct vop_readdir_args *a = ap; 5516 5517 if (!rc) 5518 VFS_KNOTE_LOCKED(a->a_vp, NOTE_READ); 5519 } 5520 5521 static struct knlist fs_knlist; 5522 5523 static void 5524 vfs_event_init(void *arg) 5525 { 5526 knlist_init_mtx(&fs_knlist, NULL); 5527 } 5528 /* XXX - correct order? */ 5529 SYSINIT(vfs_knlist, SI_SUB_VFS, SI_ORDER_ANY, vfs_event_init, NULL); 5530 5531 void 5532 vfs_event_signal(fsid_t *fsid, uint32_t event, intptr_t data __unused) 5533 { 5534 5535 KNOTE_UNLOCKED(&fs_knlist, event); 5536 } 5537 5538 static int filt_fsattach(struct knote *kn); 5539 static void filt_fsdetach(struct knote *kn); 5540 static int filt_fsevent(struct knote *kn, long hint); 5541 5542 struct filterops fs_filtops = { 5543 .f_isfd = 0, 5544 .f_attach = filt_fsattach, 5545 .f_detach = filt_fsdetach, 5546 .f_event = filt_fsevent 5547 }; 5548 5549 static int 5550 filt_fsattach(struct knote *kn) 5551 { 5552 5553 kn->kn_flags |= EV_CLEAR; 5554 knlist_add(&fs_knlist, kn, 0); 5555 return (0); 5556 } 5557 5558 static void 5559 filt_fsdetach(struct knote *kn) 5560 { 5561 5562 knlist_remove(&fs_knlist, kn, 0); 5563 } 5564 5565 static int 5566 filt_fsevent(struct knote *kn, long hint) 5567 { 5568 5569 kn->kn_fflags |= hint; 5570 return (kn->kn_fflags != 0); 5571 } 5572 5573 static int 5574 sysctl_vfs_ctl(SYSCTL_HANDLER_ARGS) 5575 { 5576 struct vfsidctl vc; 5577 int error; 5578 struct mount *mp; 5579 5580 error = SYSCTL_IN(req, &vc, sizeof(vc)); 5581 if (error) 5582 return (error); 5583 if (vc.vc_vers != VFS_CTL_VERS1) 5584 return (EINVAL); 5585 mp = vfs_getvfs(&vc.vc_fsid); 5586 if (mp == NULL) 5587 return (ENOENT); 5588 /* ensure that a specific sysctl goes to the right filesystem. */ 5589 if (strcmp(vc.vc_fstypename, "*") != 0 && 5590 strcmp(vc.vc_fstypename, mp->mnt_vfc->vfc_name) != 0) { 5591 vfs_rel(mp); 5592 return (EINVAL); 5593 } 5594 VCTLTOREQ(&vc, req); 5595 error = VFS_SYSCTL(mp, vc.vc_op, req); 5596 vfs_rel(mp); 5597 return (error); 5598 } 5599 5600 SYSCTL_PROC(_vfs, OID_AUTO, ctl, CTLTYPE_OPAQUE | CTLFLAG_MPSAFE | CTLFLAG_WR, 5601 NULL, 0, sysctl_vfs_ctl, "", 5602 "Sysctl by fsid"); 5603 5604 /* 5605 * Function to initialize a va_filerev field sensibly. 5606 * XXX: Wouldn't a random number make a lot more sense ?? 5607 */ 5608 u_quad_t 5609 init_va_filerev(void) 5610 { 5611 struct bintime bt; 5612 5613 getbinuptime(&bt); 5614 return (((u_quad_t)bt.sec << 32LL) | (bt.frac >> 32LL)); 5615 } 5616 5617 static int filt_vfsread(struct knote *kn, long hint); 5618 static int filt_vfswrite(struct knote *kn, long hint); 5619 static int filt_vfsvnode(struct knote *kn, long hint); 5620 static void filt_vfsdetach(struct knote *kn); 5621 static struct filterops vfsread_filtops = { 5622 .f_isfd = 1, 5623 .f_detach = filt_vfsdetach, 5624 .f_event = filt_vfsread 5625 }; 5626 static struct filterops vfswrite_filtops = { 5627 .f_isfd = 1, 5628 .f_detach = filt_vfsdetach, 5629 .f_event = filt_vfswrite 5630 }; 5631 static struct filterops vfsvnode_filtops = { 5632 .f_isfd = 1, 5633 .f_detach = filt_vfsdetach, 5634 .f_event = filt_vfsvnode 5635 }; 5636 5637 static void 5638 vfs_knllock(void *arg) 5639 { 5640 struct vnode *vp = arg; 5641 5642 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 5643 } 5644 5645 static void 5646 vfs_knlunlock(void *arg) 5647 { 5648 struct vnode *vp = arg; 5649 5650 VOP_UNLOCK(vp); 5651 } 5652 5653 static void 5654 vfs_knl_assert_locked(void *arg) 5655 { 5656 #ifdef DEBUG_VFS_LOCKS 5657 struct vnode *vp = arg; 5658 5659 ASSERT_VOP_LOCKED(vp, "vfs_knl_assert_locked"); 5660 #endif 5661 } 5662 5663 static void 5664 vfs_knl_assert_unlocked(void *arg) 5665 { 5666 #ifdef DEBUG_VFS_LOCKS 5667 struct vnode *vp = arg; 5668 5669 ASSERT_VOP_UNLOCKED(vp, "vfs_knl_assert_unlocked"); 5670 #endif 5671 } 5672 5673 int 5674 vfs_kqfilter(struct vop_kqfilter_args *ap) 5675 { 5676 struct vnode *vp = ap->a_vp; 5677 struct knote *kn = ap->a_kn; 5678 struct knlist *knl; 5679 5680 switch (kn->kn_filter) { 5681 case EVFILT_READ: 5682 kn->kn_fop = &vfsread_filtops; 5683 break; 5684 case EVFILT_WRITE: 5685 kn->kn_fop = &vfswrite_filtops; 5686 break; 5687 case EVFILT_VNODE: 5688 kn->kn_fop = &vfsvnode_filtops; 5689 break; 5690 default: 5691 return (EINVAL); 5692 } 5693 5694 kn->kn_hook = (caddr_t)vp; 5695 5696 v_addpollinfo(vp); 5697 if (vp->v_pollinfo == NULL) 5698 return (ENOMEM); 5699 knl = &vp->v_pollinfo->vpi_selinfo.si_note; 5700 vhold(vp); 5701 knlist_add(knl, kn, 0); 5702 5703 return (0); 5704 } 5705 5706 /* 5707 * Detach knote from vnode 5708 */ 5709 static void 5710 filt_vfsdetach(struct knote *kn) 5711 { 5712 struct vnode *vp = (struct vnode *)kn->kn_hook; 5713 5714 KASSERT(vp->v_pollinfo != NULL, ("Missing v_pollinfo")); 5715 knlist_remove(&vp->v_pollinfo->vpi_selinfo.si_note, kn, 0); 5716 vdrop(vp); 5717 } 5718 5719 /*ARGSUSED*/ 5720 static int 5721 filt_vfsread(struct knote *kn, long hint) 5722 { 5723 struct vnode *vp = (struct vnode *)kn->kn_hook; 5724 struct vattr va; 5725 int res; 5726 5727 /* 5728 * filesystem is gone, so set the EOF flag and schedule 5729 * the knote for deletion. 5730 */ 5731 if (hint == NOTE_REVOKE || (hint == 0 && vp->v_type == VBAD)) { 5732 VI_LOCK(vp); 5733 kn->kn_flags |= (EV_EOF | EV_ONESHOT); 5734 VI_UNLOCK(vp); 5735 return (1); 5736 } 5737 5738 if (VOP_GETATTR(vp, &va, curthread->td_ucred)) 5739 return (0); 5740 5741 VI_LOCK(vp); 5742 kn->kn_data = va.va_size - kn->kn_fp->f_offset; 5743 res = (kn->kn_sfflags & NOTE_FILE_POLL) != 0 || kn->kn_data != 0; 5744 VI_UNLOCK(vp); 5745 return (res); 5746 } 5747 5748 /*ARGSUSED*/ 5749 static int 5750 filt_vfswrite(struct knote *kn, long hint) 5751 { 5752 struct vnode *vp = (struct vnode *)kn->kn_hook; 5753 5754 VI_LOCK(vp); 5755 5756 /* 5757 * filesystem is gone, so set the EOF flag and schedule 5758 * the knote for deletion. 5759 */ 5760 if (hint == NOTE_REVOKE || (hint == 0 && vp->v_type == VBAD)) 5761 kn->kn_flags |= (EV_EOF | EV_ONESHOT); 5762 5763 kn->kn_data = 0; 5764 VI_UNLOCK(vp); 5765 return (1); 5766 } 5767 5768 static int 5769 filt_vfsvnode(struct knote *kn, long hint) 5770 { 5771 struct vnode *vp = (struct vnode *)kn->kn_hook; 5772 int res; 5773 5774 VI_LOCK(vp); 5775 if (kn->kn_sfflags & hint) 5776 kn->kn_fflags |= hint; 5777 if (hint == NOTE_REVOKE || (hint == 0 && vp->v_type == VBAD)) { 5778 kn->kn_flags |= EV_EOF; 5779 VI_UNLOCK(vp); 5780 return (1); 5781 } 5782 res = (kn->kn_fflags != 0); 5783 VI_UNLOCK(vp); 5784 return (res); 5785 } 5786 5787 /* 5788 * Returns whether the directory is empty or not. 5789 * If it is empty, the return value is 0; otherwise 5790 * the return value is an error value (which may 5791 * be ENOTEMPTY). 5792 */ 5793 int 5794 vfs_emptydir(struct vnode *vp) 5795 { 5796 struct uio uio; 5797 struct iovec iov; 5798 struct dirent *dirent, *dp, *endp; 5799 int error, eof; 5800 5801 error = 0; 5802 eof = 0; 5803 5804 ASSERT_VOP_LOCKED(vp, "vfs_emptydir"); 5805 5806 dirent = malloc(sizeof(struct dirent), M_TEMP, M_WAITOK); 5807 iov.iov_base = dirent; 5808 iov.iov_len = sizeof(struct dirent); 5809 5810 uio.uio_iov = &iov; 5811 uio.uio_iovcnt = 1; 5812 uio.uio_offset = 0; 5813 uio.uio_resid = sizeof(struct dirent); 5814 uio.uio_segflg = UIO_SYSSPACE; 5815 uio.uio_rw = UIO_READ; 5816 uio.uio_td = curthread; 5817 5818 while (eof == 0 && error == 0) { 5819 error = VOP_READDIR(vp, &uio, curthread->td_ucred, &eof, 5820 NULL, NULL); 5821 if (error != 0) 5822 break; 5823 endp = (void *)((uint8_t *)dirent + 5824 sizeof(struct dirent) - uio.uio_resid); 5825 for (dp = dirent; dp < endp; 5826 dp = (void *)((uint8_t *)dp + GENERIC_DIRSIZ(dp))) { 5827 if (dp->d_type == DT_WHT) 5828 continue; 5829 if (dp->d_namlen == 0) 5830 continue; 5831 if (dp->d_type != DT_DIR && 5832 dp->d_type != DT_UNKNOWN) { 5833 error = ENOTEMPTY; 5834 break; 5835 } 5836 if (dp->d_namlen > 2) { 5837 error = ENOTEMPTY; 5838 break; 5839 } 5840 if (dp->d_namlen == 1 && 5841 dp->d_name[0] != '.') { 5842 error = ENOTEMPTY; 5843 break; 5844 } 5845 if (dp->d_namlen == 2 && 5846 dp->d_name[1] != '.') { 5847 error = ENOTEMPTY; 5848 break; 5849 } 5850 uio.uio_resid = sizeof(struct dirent); 5851 } 5852 } 5853 free(dirent, M_TEMP); 5854 return (error); 5855 } 5856 5857 int 5858 vfs_read_dirent(struct vop_readdir_args *ap, struct dirent *dp, off_t off) 5859 { 5860 int error; 5861 5862 if (dp->d_reclen > ap->a_uio->uio_resid) 5863 return (ENAMETOOLONG); 5864 error = uiomove(dp, dp->d_reclen, ap->a_uio); 5865 if (error) { 5866 if (ap->a_ncookies != NULL) { 5867 if (ap->a_cookies != NULL) 5868 free(ap->a_cookies, M_TEMP); 5869 ap->a_cookies = NULL; 5870 *ap->a_ncookies = 0; 5871 } 5872 return (error); 5873 } 5874 if (ap->a_ncookies == NULL) 5875 return (0); 5876 5877 KASSERT(ap->a_cookies, 5878 ("NULL ap->a_cookies value with non-NULL ap->a_ncookies!")); 5879 5880 *ap->a_cookies = realloc(*ap->a_cookies, 5881 (*ap->a_ncookies + 1) * sizeof(u_long), M_TEMP, M_WAITOK | M_ZERO); 5882 (*ap->a_cookies)[*ap->a_ncookies] = off; 5883 *ap->a_ncookies += 1; 5884 return (0); 5885 } 5886 5887 /* 5888 * Mark for update the access time of the file if the filesystem 5889 * supports VOP_MARKATIME. This functionality is used by execve and 5890 * mmap, so we want to avoid the I/O implied by directly setting 5891 * va_atime for the sake of efficiency. 5892 */ 5893 void 5894 vfs_mark_atime(struct vnode *vp, struct ucred *cred) 5895 { 5896 struct mount *mp; 5897 5898 mp = vp->v_mount; 5899 ASSERT_VOP_LOCKED(vp, "vfs_mark_atime"); 5900 if (mp != NULL && (mp->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0) 5901 (void)VOP_MARKATIME(vp); 5902 } 5903 5904 /* 5905 * The purpose of this routine is to remove granularity from accmode_t, 5906 * reducing it into standard unix access bits - VEXEC, VREAD, VWRITE, 5907 * VADMIN and VAPPEND. 5908 * 5909 * If it returns 0, the caller is supposed to continue with the usual 5910 * access checks using 'accmode' as modified by this routine. If it 5911 * returns nonzero value, the caller is supposed to return that value 5912 * as errno. 5913 * 5914 * Note that after this routine runs, accmode may be zero. 5915 */ 5916 int 5917 vfs_unixify_accmode(accmode_t *accmode) 5918 { 5919 /* 5920 * There is no way to specify explicit "deny" rule using 5921 * file mode or POSIX.1e ACLs. 5922 */ 5923 if (*accmode & VEXPLICIT_DENY) { 5924 *accmode = 0; 5925 return (0); 5926 } 5927 5928 /* 5929 * None of these can be translated into usual access bits. 5930 * Also, the common case for NFSv4 ACLs is to not contain 5931 * either of these bits. Caller should check for VWRITE 5932 * on the containing directory instead. 5933 */ 5934 if (*accmode & (VDELETE_CHILD | VDELETE)) 5935 return (EPERM); 5936 5937 if (*accmode & VADMIN_PERMS) { 5938 *accmode &= ~VADMIN_PERMS; 5939 *accmode |= VADMIN; 5940 } 5941 5942 /* 5943 * There is no way to deny VREAD_ATTRIBUTES, VREAD_ACL 5944 * or VSYNCHRONIZE using file mode or POSIX.1e ACL. 5945 */ 5946 *accmode &= ~(VSTAT_PERMS | VSYNCHRONIZE); 5947 5948 return (0); 5949 } 5950 5951 /* 5952 * Clear out a doomed vnode (if any) and replace it with a new one as long 5953 * as the fs is not being unmounted. Return the root vnode to the caller. 5954 */ 5955 static int __noinline 5956 vfs_cache_root_fallback(struct mount *mp, int flags, struct vnode **vpp) 5957 { 5958 struct vnode *vp; 5959 int error; 5960 5961 restart: 5962 if (mp->mnt_rootvnode != NULL) { 5963 MNT_ILOCK(mp); 5964 vp = mp->mnt_rootvnode; 5965 if (vp != NULL) { 5966 if (!VN_IS_DOOMED(vp)) { 5967 vrefact(vp); 5968 MNT_IUNLOCK(mp); 5969 error = vn_lock(vp, flags); 5970 if (error == 0) { 5971 *vpp = vp; 5972 return (0); 5973 } 5974 vrele(vp); 5975 goto restart; 5976 } 5977 /* 5978 * Clear the old one. 5979 */ 5980 mp->mnt_rootvnode = NULL; 5981 } 5982 MNT_IUNLOCK(mp); 5983 if (vp != NULL) { 5984 /* 5985 * Paired with a fence in vfs_op_thread_exit(). 5986 */ 5987 atomic_thread_fence_acq(); 5988 vfs_op_barrier_wait(mp); 5989 vrele(vp); 5990 } 5991 } 5992 error = VFS_CACHEDROOT(mp, flags, vpp); 5993 if (error != 0) 5994 return (error); 5995 if (mp->mnt_vfs_ops == 0) { 5996 MNT_ILOCK(mp); 5997 if (mp->mnt_vfs_ops != 0) { 5998 MNT_IUNLOCK(mp); 5999 return (0); 6000 } 6001 if (mp->mnt_rootvnode == NULL) { 6002 vrefact(*vpp); 6003 mp->mnt_rootvnode = *vpp; 6004 } else { 6005 if (mp->mnt_rootvnode != *vpp) { 6006 if (!VN_IS_DOOMED(mp->mnt_rootvnode)) { 6007 panic("%s: mismatch between vnode returned " 6008 " by VFS_CACHEDROOT and the one cached " 6009 " (%p != %p)", 6010 __func__, *vpp, mp->mnt_rootvnode); 6011 } 6012 } 6013 } 6014 MNT_IUNLOCK(mp); 6015 } 6016 return (0); 6017 } 6018 6019 int 6020 vfs_cache_root(struct mount *mp, int flags, struct vnode **vpp) 6021 { 6022 struct vnode *vp; 6023 int error; 6024 6025 if (!vfs_op_thread_enter(mp)) 6026 return (vfs_cache_root_fallback(mp, flags, vpp)); 6027 vp = (struct vnode *)atomic_load_ptr(&mp->mnt_rootvnode); 6028 if (vp == NULL || VN_IS_DOOMED(vp)) { 6029 vfs_op_thread_exit(mp); 6030 return (vfs_cache_root_fallback(mp, flags, vpp)); 6031 } 6032 vrefact(vp); 6033 vfs_op_thread_exit(mp); 6034 error = vn_lock(vp, flags); 6035 if (error != 0) { 6036 vrele(vp); 6037 return (vfs_cache_root_fallback(mp, flags, vpp)); 6038 } 6039 *vpp = vp; 6040 return (0); 6041 } 6042 6043 struct vnode * 6044 vfs_cache_root_clear(struct mount *mp) 6045 { 6046 struct vnode *vp; 6047 6048 /* 6049 * ops > 0 guarantees there is nobody who can see this vnode 6050 */ 6051 MPASS(mp->mnt_vfs_ops > 0); 6052 vp = mp->mnt_rootvnode; 6053 mp->mnt_rootvnode = NULL; 6054 return (vp); 6055 } 6056 6057 void 6058 vfs_cache_root_set(struct mount *mp, struct vnode *vp) 6059 { 6060 6061 MPASS(mp->mnt_vfs_ops > 0); 6062 vrefact(vp); 6063 mp->mnt_rootvnode = vp; 6064 } 6065 6066 /* 6067 * These are helper functions for filesystems to traverse all 6068 * their vnodes. See MNT_VNODE_FOREACH_ALL() in sys/mount.h. 6069 * 6070 * This interface replaces MNT_VNODE_FOREACH. 6071 */ 6072 6073 6074 struct vnode * 6075 __mnt_vnode_next_all(struct vnode **mvp, struct mount *mp) 6076 { 6077 struct vnode *vp; 6078 6079 if (should_yield()) 6080 kern_yield(PRI_USER); 6081 MNT_ILOCK(mp); 6082 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch")); 6083 for (vp = TAILQ_NEXT(*mvp, v_nmntvnodes); vp != NULL; 6084 vp = TAILQ_NEXT(vp, v_nmntvnodes)) { 6085 /* Allow a racy peek at VIRF_DOOMED to save a lock acquisition. */ 6086 if (vp->v_type == VMARKER || VN_IS_DOOMED(vp)) 6087 continue; 6088 VI_LOCK(vp); 6089 if (VN_IS_DOOMED(vp)) { 6090 VI_UNLOCK(vp); 6091 continue; 6092 } 6093 break; 6094 } 6095 if (vp == NULL) { 6096 __mnt_vnode_markerfree_all(mvp, mp); 6097 /* MNT_IUNLOCK(mp); -- done in above function */ 6098 mtx_assert(MNT_MTX(mp), MA_NOTOWNED); 6099 return (NULL); 6100 } 6101 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes); 6102 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes); 6103 MNT_IUNLOCK(mp); 6104 return (vp); 6105 } 6106 6107 struct vnode * 6108 __mnt_vnode_first_all(struct vnode **mvp, struct mount *mp) 6109 { 6110 struct vnode *vp; 6111 6112 *mvp = vn_alloc_marker(mp); 6113 MNT_ILOCK(mp); 6114 MNT_REF(mp); 6115 6116 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) { 6117 /* Allow a racy peek at VIRF_DOOMED to save a lock acquisition. */ 6118 if (vp->v_type == VMARKER || VN_IS_DOOMED(vp)) 6119 continue; 6120 VI_LOCK(vp); 6121 if (VN_IS_DOOMED(vp)) { 6122 VI_UNLOCK(vp); 6123 continue; 6124 } 6125 break; 6126 } 6127 if (vp == NULL) { 6128 MNT_REL(mp); 6129 MNT_IUNLOCK(mp); 6130 vn_free_marker(*mvp); 6131 *mvp = NULL; 6132 return (NULL); 6133 } 6134 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes); 6135 MNT_IUNLOCK(mp); 6136 return (vp); 6137 } 6138 6139 void 6140 __mnt_vnode_markerfree_all(struct vnode **mvp, struct mount *mp) 6141 { 6142 6143 if (*mvp == NULL) { 6144 MNT_IUNLOCK(mp); 6145 return; 6146 } 6147 6148 mtx_assert(MNT_MTX(mp), MA_OWNED); 6149 6150 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch")); 6151 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes); 6152 MNT_REL(mp); 6153 MNT_IUNLOCK(mp); 6154 vn_free_marker(*mvp); 6155 *mvp = NULL; 6156 } 6157 6158 /* 6159 * These are helper functions for filesystems to traverse their 6160 * lazy vnodes. See MNT_VNODE_FOREACH_LAZY() in sys/mount.h 6161 */ 6162 static void 6163 mnt_vnode_markerfree_lazy(struct vnode **mvp, struct mount *mp) 6164 { 6165 6166 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch")); 6167 6168 MNT_ILOCK(mp); 6169 MNT_REL(mp); 6170 MNT_IUNLOCK(mp); 6171 vn_free_marker(*mvp); 6172 *mvp = NULL; 6173 } 6174 6175 /* 6176 * Relock the mp mount vnode list lock with the vp vnode interlock in the 6177 * conventional lock order during mnt_vnode_next_lazy iteration. 6178 * 6179 * On entry, the mount vnode list lock is held and the vnode interlock is not. 6180 * The list lock is dropped and reacquired. On success, both locks are held. 6181 * On failure, the mount vnode list lock is held but the vnode interlock is 6182 * not, and the procedure may have yielded. 6183 */ 6184 static bool 6185 mnt_vnode_next_lazy_relock(struct vnode *mvp, struct mount *mp, 6186 struct vnode *vp) 6187 { 6188 const struct vnode *tmp; 6189 bool held, ret; 6190 6191 VNASSERT(mvp->v_mount == mp && mvp->v_type == VMARKER && 6192 TAILQ_NEXT(mvp, v_lazylist) != NULL, mvp, 6193 ("%s: bad marker", __func__)); 6194 VNASSERT(vp->v_mount == mp && vp->v_type != VMARKER, vp, 6195 ("%s: inappropriate vnode", __func__)); 6196 ASSERT_VI_UNLOCKED(vp, __func__); 6197 mtx_assert(&mp->mnt_listmtx, MA_OWNED); 6198 6199 ret = false; 6200 6201 TAILQ_REMOVE(&mp->mnt_lazyvnodelist, mvp, v_lazylist); 6202 TAILQ_INSERT_BEFORE(vp, mvp, v_lazylist); 6203 6204 /* 6205 * Use a hold to prevent vp from disappearing while the mount vnode 6206 * list lock is dropped and reacquired. Normally a hold would be 6207 * acquired with vhold(), but that might try to acquire the vnode 6208 * interlock, which would be a LOR with the mount vnode list lock. 6209 */ 6210 held = refcount_acquire_if_not_zero(&vp->v_holdcnt); 6211 mtx_unlock(&mp->mnt_listmtx); 6212 if (!held) 6213 goto abort; 6214 VI_LOCK(vp); 6215 if (!refcount_release_if_not_last(&vp->v_holdcnt)) { 6216 vdropl(vp); 6217 goto abort; 6218 } 6219 mtx_lock(&mp->mnt_listmtx); 6220 6221 /* 6222 * Determine whether the vnode is still the next one after the marker, 6223 * excepting any other markers. If the vnode has not been doomed by 6224 * vgone() then the hold should have ensured that it remained on the 6225 * lazy list. If it has been doomed but is still on the lazy list, 6226 * don't abort, but rather skip over it (avoid spinning on doomed 6227 * vnodes). 6228 */ 6229 tmp = mvp; 6230 do { 6231 tmp = TAILQ_NEXT(tmp, v_lazylist); 6232 } while (tmp != NULL && tmp->v_type == VMARKER); 6233 if (tmp != vp) { 6234 mtx_unlock(&mp->mnt_listmtx); 6235 VI_UNLOCK(vp); 6236 goto abort; 6237 } 6238 6239 ret = true; 6240 goto out; 6241 abort: 6242 maybe_yield(); 6243 mtx_lock(&mp->mnt_listmtx); 6244 out: 6245 if (ret) 6246 ASSERT_VI_LOCKED(vp, __func__); 6247 else 6248 ASSERT_VI_UNLOCKED(vp, __func__); 6249 mtx_assert(&mp->mnt_listmtx, MA_OWNED); 6250 return (ret); 6251 } 6252 6253 static struct vnode * 6254 mnt_vnode_next_lazy(struct vnode **mvp, struct mount *mp, mnt_lazy_cb_t *cb, 6255 void *cbarg) 6256 { 6257 struct vnode *vp, *nvp; 6258 6259 mtx_assert(&mp->mnt_listmtx, MA_OWNED); 6260 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch")); 6261 restart: 6262 vp = TAILQ_NEXT(*mvp, v_lazylist); 6263 while (vp != NULL) { 6264 if (vp->v_type == VMARKER) { 6265 vp = TAILQ_NEXT(vp, v_lazylist); 6266 continue; 6267 } 6268 /* 6269 * See if we want to process the vnode. Note we may encounter a 6270 * long string of vnodes we don't care about and hog the list 6271 * as a result. Check for it and requeue the marker. 6272 */ 6273 if (VN_IS_DOOMED(vp) || !cb(vp, cbarg)) { 6274 if (!should_yield()) { 6275 vp = TAILQ_NEXT(vp, v_lazylist); 6276 continue; 6277 } 6278 TAILQ_REMOVE(&mp->mnt_lazyvnodelist, *mvp, 6279 v_lazylist); 6280 TAILQ_INSERT_AFTER(&mp->mnt_lazyvnodelist, vp, *mvp, 6281 v_lazylist); 6282 mtx_unlock(&mp->mnt_listmtx); 6283 kern_yield(PRI_USER); 6284 mtx_lock(&mp->mnt_listmtx); 6285 goto restart; 6286 } 6287 /* 6288 * Try-lock because this is the wrong lock order. If that does 6289 * not succeed, drop the mount vnode list lock and try to 6290 * reacquire it and the vnode interlock in the right order. 6291 */ 6292 if (!VI_TRYLOCK(vp) && 6293 !mnt_vnode_next_lazy_relock(*mvp, mp, vp)) 6294 goto restart; 6295 KASSERT(vp->v_type != VMARKER, ("locked marker %p", vp)); 6296 KASSERT(vp->v_mount == mp || vp->v_mount == NULL, 6297 ("alien vnode on the lazy list %p %p", vp, mp)); 6298 if (vp->v_mount == mp && !VN_IS_DOOMED(vp)) 6299 break; 6300 nvp = TAILQ_NEXT(vp, v_lazylist); 6301 VI_UNLOCK(vp); 6302 vp = nvp; 6303 } 6304 TAILQ_REMOVE(&mp->mnt_lazyvnodelist, *mvp, v_lazylist); 6305 6306 /* Check if we are done */ 6307 if (vp == NULL) { 6308 mtx_unlock(&mp->mnt_listmtx); 6309 mnt_vnode_markerfree_lazy(mvp, mp); 6310 return (NULL); 6311 } 6312 TAILQ_INSERT_AFTER(&mp->mnt_lazyvnodelist, vp, *mvp, v_lazylist); 6313 mtx_unlock(&mp->mnt_listmtx); 6314 ASSERT_VI_LOCKED(vp, "lazy iter"); 6315 return (vp); 6316 } 6317 6318 struct vnode * 6319 __mnt_vnode_next_lazy(struct vnode **mvp, struct mount *mp, mnt_lazy_cb_t *cb, 6320 void *cbarg) 6321 { 6322 6323 if (should_yield()) 6324 kern_yield(PRI_USER); 6325 mtx_lock(&mp->mnt_listmtx); 6326 return (mnt_vnode_next_lazy(mvp, mp, cb, cbarg)); 6327 } 6328 6329 struct vnode * 6330 __mnt_vnode_first_lazy(struct vnode **mvp, struct mount *mp, mnt_lazy_cb_t *cb, 6331 void *cbarg) 6332 { 6333 struct vnode *vp; 6334 6335 *mvp = vn_alloc_marker(mp); 6336 MNT_ILOCK(mp); 6337 MNT_REF(mp); 6338 MNT_IUNLOCK(mp); 6339 6340 mtx_lock(&mp->mnt_listmtx); 6341 vp = TAILQ_FIRST(&mp->mnt_lazyvnodelist); 6342 if (vp == NULL) { 6343 mtx_unlock(&mp->mnt_listmtx); 6344 mnt_vnode_markerfree_lazy(mvp, mp); 6345 return (NULL); 6346 } 6347 TAILQ_INSERT_BEFORE(vp, *mvp, v_lazylist); 6348 return (mnt_vnode_next_lazy(mvp, mp, cb, cbarg)); 6349 } 6350 6351 void 6352 __mnt_vnode_markerfree_lazy(struct vnode **mvp, struct mount *mp) 6353 { 6354 6355 if (*mvp == NULL) 6356 return; 6357 6358 mtx_lock(&mp->mnt_listmtx); 6359 TAILQ_REMOVE(&mp->mnt_lazyvnodelist, *mvp, v_lazylist); 6360 mtx_unlock(&mp->mnt_listmtx); 6361 mnt_vnode_markerfree_lazy(mvp, mp); 6362 } 6363