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