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