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