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