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