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