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