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_compat.h" 47 #include "opt_ddb.h" 48 #include "opt_watchdog.h" 49 50 #include <sys/param.h> 51 #include <sys/systm.h> 52 #include <sys/bio.h> 53 #include <sys/buf.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/lockf.h> 68 #include <sys/malloc.h> 69 #include <sys/mount.h> 70 #include <sys/namei.h> 71 #include <sys/pctrie.h> 72 #include <sys/priv.h> 73 #include <sys/reboot.h> 74 #include <sys/refcount.h> 75 #include <sys/rwlock.h> 76 #include <sys/sched.h> 77 #include <sys/sleepqueue.h> 78 #include <sys/smp.h> 79 #include <sys/stat.h> 80 #include <sys/sysctl.h> 81 #include <sys/syslog.h> 82 #include <sys/vmmeter.h> 83 #include <sys/vnode.h> 84 #include <sys/watchdog.h> 85 86 #include <machine/stdarg.h> 87 88 #include <security/mac/mac_framework.h> 89 90 #include <vm/vm.h> 91 #include <vm/vm_object.h> 92 #include <vm/vm_extern.h> 93 #include <vm/pmap.h> 94 #include <vm/vm_map.h> 95 #include <vm/vm_page.h> 96 #include <vm/vm_kern.h> 97 #include <vm/uma.h> 98 99 #ifdef DDB 100 #include <ddb/ddb.h> 101 #endif 102 103 static void delmntque(struct vnode *vp); 104 static int flushbuflist(struct bufv *bufv, int flags, struct bufobj *bo, 105 int slpflag, int slptimeo); 106 static void syncer_shutdown(void *arg, int howto); 107 static int vtryrecycle(struct vnode *vp); 108 static void v_init_counters(struct vnode *); 109 static void v_incr_usecount(struct vnode *); 110 static void v_incr_usecount_locked(struct vnode *); 111 static void v_incr_devcount(struct vnode *); 112 static void v_decr_devcount(struct vnode *); 113 static void vgonel(struct vnode *); 114 static void vfs_knllock(void *arg); 115 static void vfs_knlunlock(void *arg); 116 static void vfs_knl_assert_locked(void *arg); 117 static void vfs_knl_assert_unlocked(void *arg); 118 static void vnlru_return_batches(struct vfsops *mnt_op); 119 static void destroy_vpollinfo(struct vpollinfo *vi); 120 121 /* 122 * Number of vnodes in existence. Increased whenever getnewvnode() 123 * allocates a new vnode, decreased in vdropl() for VI_DOOMED vnode. 124 */ 125 static unsigned long numvnodes; 126 127 SYSCTL_ULONG(_vfs, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0, 128 "Number of vnodes in existence"); 129 130 static counter_u64_t vnodes_created; 131 SYSCTL_COUNTER_U64(_vfs, OID_AUTO, vnodes_created, CTLFLAG_RD, &vnodes_created, 132 "Number of vnodes created by getnewvnode"); 133 134 static u_long mnt_free_list_batch = 128; 135 SYSCTL_ULONG(_vfs, OID_AUTO, mnt_free_list_batch, CTLFLAG_RW, 136 &mnt_free_list_batch, 0, "Limit of vnodes held on mnt's free list"); 137 138 /* 139 * Conversion tables for conversion from vnode types to inode formats 140 * and back. 141 */ 142 enum vtype iftovt_tab[16] = { 143 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON, 144 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD, 145 }; 146 int vttoif_tab[10] = { 147 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK, 148 S_IFSOCK, S_IFIFO, S_IFMT, S_IFMT 149 }; 150 151 /* 152 * List of vnodes that are ready for recycling. 153 */ 154 static TAILQ_HEAD(freelst, vnode) vnode_free_list; 155 156 /* 157 * "Free" vnode target. Free vnodes are rarely completely free, but are 158 * just ones that are cheap to recycle. Usually they are for files which 159 * have been stat'd but not read; these usually have inode and namecache 160 * data attached to them. This target is the preferred minimum size of a 161 * sub-cache consisting mostly of such files. The system balances the size 162 * of this sub-cache with its complement to try to prevent either from 163 * thrashing while the other is relatively inactive. The targets express 164 * a preference for the best balance. 165 * 166 * "Above" this target there are 2 further targets (watermarks) related 167 * to recyling of free vnodes. In the best-operating case, the cache is 168 * exactly full, the free list has size between vlowat and vhiwat above the 169 * free target, and recycling from it and normal use maintains this state. 170 * Sometimes the free list is below vlowat or even empty, but this state 171 * is even better for immediate use provided the cache is not full. 172 * Otherwise, vnlru_proc() runs to reclaim enough vnodes (usually non-free 173 * ones) to reach one of these states. The watermarks are currently hard- 174 * coded as 4% and 9% of the available space higher. These and the default 175 * of 25% for wantfreevnodes are too large if the memory size is large. 176 * E.g., 9% of 75% of MAXVNODES is more than 566000 vnodes to reclaim 177 * whenever vnlru_proc() becomes active. 178 */ 179 static u_long wantfreevnodes; 180 SYSCTL_ULONG(_vfs, OID_AUTO, wantfreevnodes, CTLFLAG_RW, 181 &wantfreevnodes, 0, "Target for minimum number of \"free\" vnodes"); 182 static u_long freevnodes; 183 SYSCTL_ULONG(_vfs, OID_AUTO, freevnodes, CTLFLAG_RD, 184 &freevnodes, 0, "Number of \"free\" vnodes"); 185 186 static counter_u64_t recycles_count; 187 SYSCTL_COUNTER_U64(_vfs, OID_AUTO, recycles, CTLFLAG_RD, &recycles_count, 188 "Number of vnodes recycled to meet vnode cache targets"); 189 190 /* 191 * Various variables used for debugging the new implementation of 192 * reassignbuf(). 193 * XXX these are probably of (very) limited utility now. 194 */ 195 static int reassignbufcalls; 196 SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW, &reassignbufcalls, 0, 197 "Number of calls to reassignbuf"); 198 199 static counter_u64_t free_owe_inact; 200 SYSCTL_COUNTER_U64(_vfs, OID_AUTO, free_owe_inact, CTLFLAG_RD, &free_owe_inact, 201 "Number of times free vnodes kept on active list due to VFS " 202 "owing inactivation"); 203 204 /* To keep more than one thread at a time from running vfs_getnewfsid */ 205 static struct mtx mntid_mtx; 206 207 /* 208 * Lock for any access to the following: 209 * vnode_free_list 210 * numvnodes 211 * freevnodes 212 */ 213 static struct mtx vnode_free_list_mtx; 214 215 /* Publicly exported FS */ 216 struct nfs_public nfs_pub; 217 218 static uma_zone_t buf_trie_zone; 219 220 /* Zone for allocation of new vnodes - used exclusively by getnewvnode() */ 221 static uma_zone_t vnode_zone; 222 static uma_zone_t vnodepoll_zone; 223 224 /* 225 * The workitem queue. 226 * 227 * It is useful to delay writes of file data and filesystem metadata 228 * for tens of seconds so that quickly created and deleted files need 229 * not waste disk bandwidth being created and removed. To realize this, 230 * we append vnodes to a "workitem" queue. When running with a soft 231 * updates implementation, most pending metadata dependencies should 232 * not wait for more than a few seconds. Thus, mounted on block devices 233 * are delayed only about a half the time that file data is delayed. 234 * Similarly, directory updates are more critical, so are only delayed 235 * about a third the time that file data is delayed. Thus, there are 236 * SYNCER_MAXDELAY queues that are processed round-robin at a rate of 237 * one each second (driven off the filesystem syncer process). The 238 * syncer_delayno variable indicates the next queue that is to be processed. 239 * Items that need to be processed soon are placed in this queue: 240 * 241 * syncer_workitem_pending[syncer_delayno] 242 * 243 * A delay of fifteen seconds is done by placing the request fifteen 244 * entries later in the queue: 245 * 246 * syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask] 247 * 248 */ 249 static int syncer_delayno; 250 static long syncer_mask; 251 LIST_HEAD(synclist, bufobj); 252 static struct synclist *syncer_workitem_pending; 253 /* 254 * The sync_mtx protects: 255 * bo->bo_synclist 256 * sync_vnode_count 257 * syncer_delayno 258 * syncer_state 259 * syncer_workitem_pending 260 * syncer_worklist_len 261 * rushjob 262 */ 263 static struct mtx sync_mtx; 264 static struct cv sync_wakeup; 265 266 #define SYNCER_MAXDELAY 32 267 static int syncer_maxdelay = SYNCER_MAXDELAY; /* maximum delay time */ 268 static int syncdelay = 30; /* max time to delay syncing data */ 269 static int filedelay = 30; /* time to delay syncing files */ 270 SYSCTL_INT(_kern, OID_AUTO, filedelay, CTLFLAG_RW, &filedelay, 0, 271 "Time to delay syncing files (in seconds)"); 272 static int dirdelay = 29; /* time to delay syncing directories */ 273 SYSCTL_INT(_kern, OID_AUTO, dirdelay, CTLFLAG_RW, &dirdelay, 0, 274 "Time to delay syncing directories (in seconds)"); 275 static int metadelay = 28; /* time to delay syncing metadata */ 276 SYSCTL_INT(_kern, OID_AUTO, metadelay, CTLFLAG_RW, &metadelay, 0, 277 "Time to delay syncing metadata (in seconds)"); 278 static int rushjob; /* number of slots to run ASAP */ 279 static int stat_rush_requests; /* number of times I/O speeded up */ 280 SYSCTL_INT(_debug, OID_AUTO, rush_requests, CTLFLAG_RW, &stat_rush_requests, 0, 281 "Number of times I/O speeded up (rush requests)"); 282 283 /* 284 * When shutting down the syncer, run it at four times normal speed. 285 */ 286 #define SYNCER_SHUTDOWN_SPEEDUP 4 287 static int sync_vnode_count; 288 static int syncer_worklist_len; 289 static enum { SYNCER_RUNNING, SYNCER_SHUTTING_DOWN, SYNCER_FINAL_DELAY } 290 syncer_state; 291 292 /* Target for maximum number of vnodes. */ 293 int desiredvnodes; 294 static int gapvnodes; /* gap between wanted and desired */ 295 static int vhiwat; /* enough extras after expansion */ 296 static int vlowat; /* minimal extras before expansion */ 297 static int vstir; /* nonzero to stir non-free vnodes */ 298 static volatile int vsmalltrigger = 8; /* pref to keep if > this many pages */ 299 300 static int 301 sysctl_update_desiredvnodes(SYSCTL_HANDLER_ARGS) 302 { 303 int error, old_desiredvnodes; 304 305 old_desiredvnodes = desiredvnodes; 306 if ((error = sysctl_handle_int(oidp, arg1, arg2, req)) != 0) 307 return (error); 308 if (old_desiredvnodes != desiredvnodes) { 309 wantfreevnodes = desiredvnodes / 4; 310 /* XXX locking seems to be incomplete. */ 311 vfs_hash_changesize(desiredvnodes); 312 cache_changesize(desiredvnodes); 313 } 314 return (0); 315 } 316 317 SYSCTL_PROC(_kern, KERN_MAXVNODES, maxvnodes, 318 CTLTYPE_INT | CTLFLAG_MPSAFE | CTLFLAG_RW, &desiredvnodes, 0, 319 sysctl_update_desiredvnodes, "I", "Target for maximum number of vnodes"); 320 SYSCTL_ULONG(_kern, OID_AUTO, minvnodes, CTLFLAG_RW, 321 &wantfreevnodes, 0, "Old name for vfs.wantfreevnodes (legacy)"); 322 static int vnlru_nowhere; 323 SYSCTL_INT(_debug, OID_AUTO, vnlru_nowhere, CTLFLAG_RW, 324 &vnlru_nowhere, 0, "Number of times the vnlru process ran without success"); 325 326 /* Shift count for (uintptr_t)vp to initialize vp->v_hash. */ 327 static int vnsz2log; 328 329 /* 330 * Support for the bufobj clean & dirty pctrie. 331 */ 332 static void * 333 buf_trie_alloc(struct pctrie *ptree) 334 { 335 336 return uma_zalloc(buf_trie_zone, M_NOWAIT); 337 } 338 339 static void 340 buf_trie_free(struct pctrie *ptree, void *node) 341 { 342 343 uma_zfree(buf_trie_zone, node); 344 } 345 PCTRIE_DEFINE(BUF, buf, b_lblkno, buf_trie_alloc, buf_trie_free); 346 347 /* 348 * Initialize the vnode management data structures. 349 * 350 * Reevaluate the following cap on the number of vnodes after the physical 351 * memory size exceeds 512GB. In the limit, as the physical memory size 352 * grows, the ratio of the memory size in KB to to vnodes approaches 64:1. 353 */ 354 #ifndef MAXVNODES_MAX 355 #define MAXVNODES_MAX (512 * 1024 * 1024 / 64) /* 8M */ 356 #endif 357 358 /* 359 * Initialize a vnode as it first enters the zone. 360 */ 361 static int 362 vnode_init(void *mem, int size, int flags) 363 { 364 struct vnode *vp; 365 struct bufobj *bo; 366 367 vp = mem; 368 bzero(vp, size); 369 /* 370 * Setup locks. 371 */ 372 vp->v_vnlock = &vp->v_lock; 373 mtx_init(&vp->v_interlock, "vnode interlock", NULL, MTX_DEF); 374 /* 375 * By default, don't allow shared locks unless filesystems opt-in. 376 */ 377 lockinit(vp->v_vnlock, PVFS, "vnode", VLKTIMEOUT, 378 LK_NOSHARE | LK_IS_VNODE); 379 /* 380 * Initialize bufobj. 381 */ 382 bo = &vp->v_bufobj; 383 rw_init(BO_LOCKPTR(bo), "bufobj interlock"); 384 bo->bo_private = vp; 385 TAILQ_INIT(&bo->bo_clean.bv_hd); 386 TAILQ_INIT(&bo->bo_dirty.bv_hd); 387 /* 388 * Initialize namecache. 389 */ 390 LIST_INIT(&vp->v_cache_src); 391 TAILQ_INIT(&vp->v_cache_dst); 392 /* 393 * Initialize rangelocks. 394 */ 395 rangelock_init(&vp->v_rl); 396 return (0); 397 } 398 399 /* 400 * Free a vnode when it is cleared from the zone. 401 */ 402 static void 403 vnode_fini(void *mem, int size) 404 { 405 struct vnode *vp; 406 struct bufobj *bo; 407 408 vp = mem; 409 rangelock_destroy(&vp->v_rl); 410 lockdestroy(vp->v_vnlock); 411 mtx_destroy(&vp->v_interlock); 412 bo = &vp->v_bufobj; 413 rw_destroy(BO_LOCKPTR(bo)); 414 } 415 416 /* 417 * Provide the size of NFS nclnode and NFS fh for calculation of the 418 * vnode memory consumption. The size is specified directly to 419 * eliminate dependency on NFS-private header. 420 * 421 * Other filesystems may use bigger or smaller (like UFS and ZFS) 422 * private inode data, but the NFS-based estimation is ample enough. 423 * Still, we care about differences in the size between 64- and 32-bit 424 * platforms. 425 * 426 * Namecache structure size is heuristically 427 * sizeof(struct namecache_ts) + CACHE_PATH_CUTOFF + 1. 428 */ 429 #ifdef _LP64 430 #define NFS_NCLNODE_SZ (528 + 64) 431 #define NC_SZ 148 432 #else 433 #define NFS_NCLNODE_SZ (360 + 32) 434 #define NC_SZ 92 435 #endif 436 437 static void 438 vntblinit(void *dummy __unused) 439 { 440 u_int i; 441 int physvnodes, virtvnodes; 442 443 /* 444 * Desiredvnodes is a function of the physical memory size and the 445 * kernel's heap size. Generally speaking, it scales with the 446 * physical memory size. The ratio of desiredvnodes to the physical 447 * memory size is 1:16 until desiredvnodes exceeds 98,304. 448 * Thereafter, the 449 * marginal ratio of desiredvnodes to the physical memory size is 450 * 1:64. However, desiredvnodes is limited by the kernel's heap 451 * size. The memory required by desiredvnodes vnodes and vm objects 452 * must not exceed 1/10th of the kernel's heap size. 453 */ 454 physvnodes = maxproc + pgtok(vm_cnt.v_page_count) / 64 + 455 3 * min(98304 * 16, pgtok(vm_cnt.v_page_count)) / 64; 456 virtvnodes = vm_kmem_size / (10 * (sizeof(struct vm_object) + 457 sizeof(struct vnode) + NC_SZ * ncsizefactor + NFS_NCLNODE_SZ)); 458 desiredvnodes = min(physvnodes, virtvnodes); 459 if (desiredvnodes > MAXVNODES_MAX) { 460 if (bootverbose) 461 printf("Reducing kern.maxvnodes %d -> %d\n", 462 desiredvnodes, MAXVNODES_MAX); 463 desiredvnodes = MAXVNODES_MAX; 464 } 465 wantfreevnodes = desiredvnodes / 4; 466 mtx_init(&mntid_mtx, "mntid", NULL, MTX_DEF); 467 TAILQ_INIT(&vnode_free_list); 468 mtx_init(&vnode_free_list_mtx, "vnode_free_list", NULL, MTX_DEF); 469 vnode_zone = uma_zcreate("VNODE", sizeof (struct vnode), NULL, NULL, 470 vnode_init, vnode_fini, UMA_ALIGN_PTR, 0); 471 vnodepoll_zone = uma_zcreate("VNODEPOLL", sizeof (struct vpollinfo), 472 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 473 /* 474 * Preallocate enough nodes to support one-per buf so that 475 * we can not fail an insert. reassignbuf() callers can not 476 * tolerate the insertion failure. 477 */ 478 buf_trie_zone = uma_zcreate("BUF TRIE", pctrie_node_size(), 479 NULL, NULL, pctrie_zone_init, NULL, UMA_ALIGN_PTR, 480 UMA_ZONE_NOFREE | UMA_ZONE_VM); 481 uma_prealloc(buf_trie_zone, nbuf); 482 483 vnodes_created = counter_u64_alloc(M_WAITOK); 484 recycles_count = counter_u64_alloc(M_WAITOK); 485 free_owe_inact = counter_u64_alloc(M_WAITOK); 486 487 /* 488 * Initialize the filesystem syncer. 489 */ 490 syncer_workitem_pending = hashinit(syncer_maxdelay, M_VNODE, 491 &syncer_mask); 492 syncer_maxdelay = syncer_mask + 1; 493 mtx_init(&sync_mtx, "Syncer mtx", NULL, MTX_DEF); 494 cv_init(&sync_wakeup, "syncer"); 495 for (i = 1; i <= sizeof(struct vnode); i <<= 1) 496 vnsz2log++; 497 vnsz2log--; 498 } 499 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_FIRST, vntblinit, NULL); 500 501 502 /* 503 * Mark a mount point as busy. Used to synchronize access and to delay 504 * unmounting. Eventually, mountlist_mtx is not released on failure. 505 * 506 * vfs_busy() is a custom lock, it can block the caller. 507 * vfs_busy() only sleeps if the unmount is active on the mount point. 508 * For a mountpoint mp, vfs_busy-enforced lock is before lock of any 509 * vnode belonging to mp. 510 * 511 * Lookup uses vfs_busy() to traverse mount points. 512 * root fs var fs 513 * / vnode lock A / vnode lock (/var) D 514 * /var vnode lock B /log vnode lock(/var/log) E 515 * vfs_busy lock C vfs_busy lock F 516 * 517 * Within each file system, the lock order is C->A->B and F->D->E. 518 * 519 * When traversing across mounts, the system follows that lock order: 520 * 521 * C->A->B 522 * | 523 * +->F->D->E 524 * 525 * The lookup() process for namei("/var") illustrates the process: 526 * VOP_LOOKUP() obtains B while A is held 527 * vfs_busy() obtains a shared lock on F while A and B are held 528 * vput() releases lock on B 529 * vput() releases lock on A 530 * VFS_ROOT() obtains lock on D while shared lock on F is held 531 * vfs_unbusy() releases shared lock on F 532 * vn_lock() obtains lock on deadfs vnode vp_crossmp instead of A. 533 * Attempt to lock A (instead of vp_crossmp) while D is held would 534 * violate the global order, causing deadlocks. 535 * 536 * dounmount() locks B while F is drained. 537 */ 538 int 539 vfs_busy(struct mount *mp, int flags) 540 { 541 542 MPASS((flags & ~MBF_MASK) == 0); 543 CTR3(KTR_VFS, "%s: mp %p with flags %d", __func__, mp, flags); 544 545 MNT_ILOCK(mp); 546 MNT_REF(mp); 547 /* 548 * If mount point is currently being unmounted, sleep until the 549 * mount point fate is decided. If thread doing the unmounting fails, 550 * it will clear MNTK_UNMOUNT flag before waking us up, indicating 551 * that this mount point has survived the unmount attempt and vfs_busy 552 * should retry. Otherwise the unmounter thread will set MNTK_REFEXPIRE 553 * flag in addition to MNTK_UNMOUNT, indicating that mount point is 554 * about to be really destroyed. vfs_busy needs to release its 555 * reference on the mount point in this case and return with ENOENT, 556 * telling the caller that mount mount it tried to busy is no longer 557 * valid. 558 */ 559 while (mp->mnt_kern_flag & MNTK_UNMOUNT) { 560 if (flags & MBF_NOWAIT || mp->mnt_kern_flag & MNTK_REFEXPIRE) { 561 MNT_REL(mp); 562 MNT_IUNLOCK(mp); 563 CTR1(KTR_VFS, "%s: failed busying before sleeping", 564 __func__); 565 return (ENOENT); 566 } 567 if (flags & MBF_MNTLSTLOCK) 568 mtx_unlock(&mountlist_mtx); 569 mp->mnt_kern_flag |= MNTK_MWAIT; 570 msleep(mp, MNT_MTX(mp), PVFS | PDROP, "vfs_busy", 0); 571 if (flags & MBF_MNTLSTLOCK) 572 mtx_lock(&mountlist_mtx); 573 MNT_ILOCK(mp); 574 } 575 if (flags & MBF_MNTLSTLOCK) 576 mtx_unlock(&mountlist_mtx); 577 mp->mnt_lockref++; 578 MNT_IUNLOCK(mp); 579 return (0); 580 } 581 582 /* 583 * Free a busy filesystem. 584 */ 585 void 586 vfs_unbusy(struct mount *mp) 587 { 588 589 CTR2(KTR_VFS, "%s: mp %p", __func__, mp); 590 MNT_ILOCK(mp); 591 MNT_REL(mp); 592 KASSERT(mp->mnt_lockref > 0, ("negative mnt_lockref")); 593 mp->mnt_lockref--; 594 if (mp->mnt_lockref == 0 && (mp->mnt_kern_flag & MNTK_DRAINING) != 0) { 595 MPASS(mp->mnt_kern_flag & MNTK_UNMOUNT); 596 CTR1(KTR_VFS, "%s: waking up waiters", __func__); 597 mp->mnt_kern_flag &= ~MNTK_DRAINING; 598 wakeup(&mp->mnt_lockref); 599 } 600 MNT_IUNLOCK(mp); 601 } 602 603 /* 604 * Lookup a mount point by filesystem identifier. 605 */ 606 struct mount * 607 vfs_getvfs(fsid_t *fsid) 608 { 609 struct mount *mp; 610 611 CTR2(KTR_VFS, "%s: fsid %p", __func__, fsid); 612 mtx_lock(&mountlist_mtx); 613 TAILQ_FOREACH(mp, &mountlist, mnt_list) { 614 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] && 615 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) { 616 vfs_ref(mp); 617 mtx_unlock(&mountlist_mtx); 618 return (mp); 619 } 620 } 621 mtx_unlock(&mountlist_mtx); 622 CTR2(KTR_VFS, "%s: lookup failed for %p id", __func__, fsid); 623 return ((struct mount *) 0); 624 } 625 626 /* 627 * Lookup a mount point by filesystem identifier, busying it before 628 * returning. 629 * 630 * To avoid congestion on mountlist_mtx, implement simple direct-mapped 631 * cache for popular filesystem identifiers. The cache is lockess, using 632 * the fact that struct mount's are never freed. In worst case we may 633 * get pointer to unmounted or even different filesystem, so we have to 634 * check what we got, and go slow way if so. 635 */ 636 struct mount * 637 vfs_busyfs(fsid_t *fsid) 638 { 639 #define FSID_CACHE_SIZE 256 640 typedef struct mount * volatile vmp_t; 641 static vmp_t cache[FSID_CACHE_SIZE]; 642 struct mount *mp; 643 int error; 644 uint32_t hash; 645 646 CTR2(KTR_VFS, "%s: fsid %p", __func__, fsid); 647 hash = fsid->val[0] ^ fsid->val[1]; 648 hash = (hash >> 16 ^ hash) & (FSID_CACHE_SIZE - 1); 649 mp = cache[hash]; 650 if (mp == NULL || 651 mp->mnt_stat.f_fsid.val[0] != fsid->val[0] || 652 mp->mnt_stat.f_fsid.val[1] != fsid->val[1]) 653 goto slow; 654 if (vfs_busy(mp, 0) != 0) { 655 cache[hash] = NULL; 656 goto slow; 657 } 658 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] && 659 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) 660 return (mp); 661 else 662 vfs_unbusy(mp); 663 664 slow: 665 mtx_lock(&mountlist_mtx); 666 TAILQ_FOREACH(mp, &mountlist, mnt_list) { 667 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] && 668 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) { 669 error = vfs_busy(mp, MBF_MNTLSTLOCK); 670 if (error) { 671 cache[hash] = NULL; 672 mtx_unlock(&mountlist_mtx); 673 return (NULL); 674 } 675 cache[hash] = mp; 676 return (mp); 677 } 678 } 679 CTR2(KTR_VFS, "%s: lookup failed for %p id", __func__, fsid); 680 mtx_unlock(&mountlist_mtx); 681 return ((struct mount *) 0); 682 } 683 684 /* 685 * Check if a user can access privileged mount options. 686 */ 687 int 688 vfs_suser(struct mount *mp, struct thread *td) 689 { 690 int error; 691 692 /* 693 * If the thread is jailed, but this is not a jail-friendly file 694 * system, deny immediately. 695 */ 696 if (!(mp->mnt_vfc->vfc_flags & VFCF_JAIL) && jailed(td->td_ucred)) 697 return (EPERM); 698 699 /* 700 * If the file system was mounted outside the jail of the calling 701 * thread, deny immediately. 702 */ 703 if (prison_check(td->td_ucred, mp->mnt_cred) != 0) 704 return (EPERM); 705 706 /* 707 * If file system supports delegated administration, we don't check 708 * for the PRIV_VFS_MOUNT_OWNER privilege - it will be better verified 709 * by the file system itself. 710 * If this is not the user that did original mount, we check for 711 * the PRIV_VFS_MOUNT_OWNER privilege. 712 */ 713 if (!(mp->mnt_vfc->vfc_flags & VFCF_DELEGADMIN) && 714 mp->mnt_cred->cr_uid != td->td_ucred->cr_uid) { 715 if ((error = priv_check(td, PRIV_VFS_MOUNT_OWNER)) != 0) 716 return (error); 717 } 718 return (0); 719 } 720 721 /* 722 * Get a new unique fsid. Try to make its val[0] unique, since this value 723 * will be used to create fake device numbers for stat(). Also try (but 724 * not so hard) make its val[0] unique mod 2^16, since some emulators only 725 * support 16-bit device numbers. We end up with unique val[0]'s for the 726 * first 2^16 calls and unique val[0]'s mod 2^16 for the first 2^8 calls. 727 * 728 * Keep in mind that several mounts may be running in parallel. Starting 729 * the search one past where the previous search terminated is both a 730 * micro-optimization and a defense against returning the same fsid to 731 * different mounts. 732 */ 733 void 734 vfs_getnewfsid(struct mount *mp) 735 { 736 static uint16_t mntid_base; 737 struct mount *nmp; 738 fsid_t tfsid; 739 int mtype; 740 741 CTR2(KTR_VFS, "%s: mp %p", __func__, mp); 742 mtx_lock(&mntid_mtx); 743 mtype = mp->mnt_vfc->vfc_typenum; 744 tfsid.val[1] = mtype; 745 mtype = (mtype & 0xFF) << 24; 746 for (;;) { 747 tfsid.val[0] = makedev(255, 748 mtype | ((mntid_base & 0xFF00) << 8) | (mntid_base & 0xFF)); 749 mntid_base++; 750 if ((nmp = vfs_getvfs(&tfsid)) == NULL) 751 break; 752 vfs_rel(nmp); 753 } 754 mp->mnt_stat.f_fsid.val[0] = tfsid.val[0]; 755 mp->mnt_stat.f_fsid.val[1] = tfsid.val[1]; 756 mtx_unlock(&mntid_mtx); 757 } 758 759 /* 760 * Knob to control the precision of file timestamps: 761 * 762 * 0 = seconds only; nanoseconds zeroed. 763 * 1 = seconds and nanoseconds, accurate within 1/HZ. 764 * 2 = seconds and nanoseconds, truncated to microseconds. 765 * >=3 = seconds and nanoseconds, maximum precision. 766 */ 767 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC }; 768 769 static int timestamp_precision = TSP_USEC; 770 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW, 771 ×tamp_precision, 0, "File timestamp precision (0: seconds, " 772 "1: sec + ns accurate to 1/HZ, 2: sec + ns truncated to us, " 773 "3+: sec + ns (max. precision))"); 774 775 /* 776 * Get a current timestamp. 777 */ 778 void 779 vfs_timestamp(struct timespec *tsp) 780 { 781 struct timeval tv; 782 783 switch (timestamp_precision) { 784 case TSP_SEC: 785 tsp->tv_sec = time_second; 786 tsp->tv_nsec = 0; 787 break; 788 case TSP_HZ: 789 getnanotime(tsp); 790 break; 791 case TSP_USEC: 792 microtime(&tv); 793 TIMEVAL_TO_TIMESPEC(&tv, tsp); 794 break; 795 case TSP_NSEC: 796 default: 797 nanotime(tsp); 798 break; 799 } 800 } 801 802 /* 803 * Set vnode attributes to VNOVAL 804 */ 805 void 806 vattr_null(struct vattr *vap) 807 { 808 809 vap->va_type = VNON; 810 vap->va_size = VNOVAL; 811 vap->va_bytes = VNOVAL; 812 vap->va_mode = VNOVAL; 813 vap->va_nlink = VNOVAL; 814 vap->va_uid = VNOVAL; 815 vap->va_gid = VNOVAL; 816 vap->va_fsid = VNOVAL; 817 vap->va_fileid = VNOVAL; 818 vap->va_blocksize = VNOVAL; 819 vap->va_rdev = VNOVAL; 820 vap->va_atime.tv_sec = VNOVAL; 821 vap->va_atime.tv_nsec = VNOVAL; 822 vap->va_mtime.tv_sec = VNOVAL; 823 vap->va_mtime.tv_nsec = VNOVAL; 824 vap->va_ctime.tv_sec = VNOVAL; 825 vap->va_ctime.tv_nsec = VNOVAL; 826 vap->va_birthtime.tv_sec = VNOVAL; 827 vap->va_birthtime.tv_nsec = VNOVAL; 828 vap->va_flags = VNOVAL; 829 vap->va_gen = VNOVAL; 830 vap->va_vaflags = 0; 831 } 832 833 /* 834 * This routine is called when we have too many vnodes. It attempts 835 * to free <count> vnodes and will potentially free vnodes that still 836 * have VM backing store (VM backing store is typically the cause 837 * of a vnode blowout so we want to do this). Therefore, this operation 838 * is not considered cheap. 839 * 840 * A number of conditions may prevent a vnode from being reclaimed. 841 * the buffer cache may have references on the vnode, a directory 842 * vnode may still have references due to the namei cache representing 843 * underlying files, or the vnode may be in active use. It is not 844 * desirable to reuse such vnodes. These conditions may cause the 845 * number of vnodes to reach some minimum value regardless of what 846 * you set kern.maxvnodes to. Do not set kern.maxvnodes too low. 847 */ 848 static int 849 vlrureclaim(struct mount *mp, int reclaim_nc_src, int trigger) 850 { 851 struct vnode *vp; 852 int count, done, target; 853 854 done = 0; 855 vn_start_write(NULL, &mp, V_WAIT); 856 MNT_ILOCK(mp); 857 count = mp->mnt_nvnodelistsize; 858 target = count * (int64_t)gapvnodes / imax(desiredvnodes, 1); 859 target = target / 10 + 1; 860 while (count != 0 && done < target) { 861 vp = TAILQ_FIRST(&mp->mnt_nvnodelist); 862 while (vp != NULL && vp->v_type == VMARKER) 863 vp = TAILQ_NEXT(vp, v_nmntvnodes); 864 if (vp == NULL) 865 break; 866 /* 867 * XXX LRU is completely broken for non-free vnodes. First 868 * by calling here in mountpoint order, then by moving 869 * unselected vnodes to the end here, and most grossly by 870 * removing the vlruvp() function that was supposed to 871 * maintain the order. (This function was born broken 872 * since syncer problems prevented it doing anything.) The 873 * order is closer to LRC (C = Created). 874 * 875 * LRU reclaiming of vnodes seems to have last worked in 876 * FreeBSD-3 where LRU wasn't mentioned under any spelling. 877 * Then there was no hold count, and inactive vnodes were 878 * simply put on the free list in LRU order. The separate 879 * lists also break LRU. We prefer to reclaim from the 880 * free list for technical reasons. This tends to thrash 881 * the free list to keep very unrecently used held vnodes. 882 * The problem is mitigated by keeping the free list large. 883 */ 884 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes); 885 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes); 886 --count; 887 if (!VI_TRYLOCK(vp)) 888 goto next_iter; 889 /* 890 * If it's been deconstructed already, it's still 891 * referenced, or it exceeds the trigger, skip it. 892 * Also skip free vnodes. We are trying to make space 893 * to expand the free list, not reduce it. 894 */ 895 if (vp->v_usecount || 896 (!reclaim_nc_src && !LIST_EMPTY(&vp->v_cache_src)) || 897 ((vp->v_iflag & VI_FREE) != 0) || 898 (vp->v_iflag & VI_DOOMED) != 0 || (vp->v_object != NULL && 899 vp->v_object->resident_page_count > trigger)) { 900 VI_UNLOCK(vp); 901 goto next_iter; 902 } 903 MNT_IUNLOCK(mp); 904 vholdl(vp); 905 if (VOP_LOCK(vp, LK_INTERLOCK|LK_EXCLUSIVE|LK_NOWAIT)) { 906 vdrop(vp); 907 goto next_iter_mntunlocked; 908 } 909 VI_LOCK(vp); 910 /* 911 * v_usecount may have been bumped after VOP_LOCK() dropped 912 * the vnode interlock and before it was locked again. 913 * 914 * It is not necessary to recheck VI_DOOMED because it can 915 * only be set by another thread that holds both the vnode 916 * lock and vnode interlock. If another thread has the 917 * vnode lock before we get to VOP_LOCK() and obtains the 918 * vnode interlock after VOP_LOCK() drops the vnode 919 * interlock, the other thread will be unable to drop the 920 * vnode lock before our VOP_LOCK() call fails. 921 */ 922 if (vp->v_usecount || 923 (!reclaim_nc_src && !LIST_EMPTY(&vp->v_cache_src)) || 924 (vp->v_iflag & VI_FREE) != 0 || 925 (vp->v_object != NULL && 926 vp->v_object->resident_page_count > trigger)) { 927 VOP_UNLOCK(vp, LK_INTERLOCK); 928 vdrop(vp); 929 goto next_iter_mntunlocked; 930 } 931 KASSERT((vp->v_iflag & VI_DOOMED) == 0, 932 ("VI_DOOMED unexpectedly detected in vlrureclaim()")); 933 counter_u64_add(recycles_count, 1); 934 vgonel(vp); 935 VOP_UNLOCK(vp, 0); 936 vdropl(vp); 937 done++; 938 next_iter_mntunlocked: 939 if (!should_yield()) 940 goto relock_mnt; 941 goto yield; 942 next_iter: 943 if (!should_yield()) 944 continue; 945 MNT_IUNLOCK(mp); 946 yield: 947 kern_yield(PRI_USER); 948 relock_mnt: 949 MNT_ILOCK(mp); 950 } 951 MNT_IUNLOCK(mp); 952 vn_finished_write(mp); 953 return done; 954 } 955 956 static int max_vnlru_free = 10000; /* limit on vnode free requests per call */ 957 SYSCTL_INT(_debug, OID_AUTO, max_vnlru_free, CTLFLAG_RW, &max_vnlru_free, 958 0, 959 "limit on vnode free requests per call to the vnlru_free routine"); 960 961 /* 962 * Attempt to reduce the free list by the requested amount. 963 */ 964 static void 965 vnlru_free_locked(int count, struct vfsops *mnt_op) 966 { 967 struct vnode *vp; 968 struct mount *mp; 969 bool tried_batches; 970 971 tried_batches = false; 972 mtx_assert(&vnode_free_list_mtx, MA_OWNED); 973 if (count > max_vnlru_free) 974 count = max_vnlru_free; 975 for (; count > 0; count--) { 976 vp = TAILQ_FIRST(&vnode_free_list); 977 /* 978 * The list can be modified while the free_list_mtx 979 * has been dropped and vp could be NULL here. 980 */ 981 if (vp == NULL) { 982 if (tried_batches) 983 break; 984 mtx_unlock(&vnode_free_list_mtx); 985 vnlru_return_batches(mnt_op); 986 tried_batches = true; 987 mtx_lock(&vnode_free_list_mtx); 988 continue; 989 } 990 991 VNASSERT(vp->v_op != NULL, vp, 992 ("vnlru_free: vnode already reclaimed.")); 993 KASSERT((vp->v_iflag & VI_FREE) != 0, 994 ("Removing vnode not on freelist")); 995 KASSERT((vp->v_iflag & VI_ACTIVE) == 0, 996 ("Mangling active vnode")); 997 TAILQ_REMOVE(&vnode_free_list, vp, v_actfreelist); 998 999 /* 1000 * Don't recycle if our vnode is from different type 1001 * of mount point. Note that mp is type-safe, the 1002 * check does not reach unmapped address even if 1003 * vnode is reclaimed. 1004 * Don't recycle if we can't get the interlock without 1005 * blocking. 1006 */ 1007 if ((mnt_op != NULL && (mp = vp->v_mount) != NULL && 1008 mp->mnt_op != mnt_op) || !VI_TRYLOCK(vp)) { 1009 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_actfreelist); 1010 continue; 1011 } 1012 VNASSERT((vp->v_iflag & VI_FREE) != 0 && vp->v_holdcnt == 0, 1013 vp, ("vp inconsistent on freelist")); 1014 1015 /* 1016 * The clear of VI_FREE prevents activation of the 1017 * vnode. There is no sense in putting the vnode on 1018 * the mount point active list, only to remove it 1019 * later during recycling. Inline the relevant part 1020 * of vholdl(), to avoid triggering assertions or 1021 * activating. 1022 */ 1023 freevnodes--; 1024 vp->v_iflag &= ~VI_FREE; 1025 refcount_acquire(&vp->v_holdcnt); 1026 1027 mtx_unlock(&vnode_free_list_mtx); 1028 VI_UNLOCK(vp); 1029 vtryrecycle(vp); 1030 /* 1031 * If the recycled succeeded this vdrop will actually free 1032 * the vnode. If not it will simply place it back on 1033 * the free list. 1034 */ 1035 vdrop(vp); 1036 mtx_lock(&vnode_free_list_mtx); 1037 } 1038 } 1039 1040 void 1041 vnlru_free(int count, struct vfsops *mnt_op) 1042 { 1043 1044 mtx_lock(&vnode_free_list_mtx); 1045 vnlru_free_locked(count, mnt_op); 1046 mtx_unlock(&vnode_free_list_mtx); 1047 } 1048 1049 1050 /* XXX some names and initialization are bad for limits and watermarks. */ 1051 static int 1052 vspace(void) 1053 { 1054 int space; 1055 1056 gapvnodes = imax(desiredvnodes - wantfreevnodes, 100); 1057 vhiwat = gapvnodes / 11; /* 9% -- just under the 10% in vlrureclaim() */ 1058 vlowat = vhiwat / 2; 1059 if (numvnodes > desiredvnodes) 1060 return (0); 1061 space = desiredvnodes - numvnodes; 1062 if (freevnodes > wantfreevnodes) 1063 space += freevnodes - wantfreevnodes; 1064 return (space); 1065 } 1066 1067 static void 1068 vnlru_return_batch_locked(struct mount *mp) 1069 { 1070 struct vnode *vp; 1071 1072 mtx_assert(&mp->mnt_listmtx, MA_OWNED); 1073 1074 if (mp->mnt_tmpfreevnodelistsize == 0) 1075 return; 1076 1077 TAILQ_FOREACH(vp, &mp->mnt_tmpfreevnodelist, v_actfreelist) { 1078 VNASSERT((vp->v_mflag & VMP_TMPMNTFREELIST) != 0, vp, 1079 ("vnode without VMP_TMPMNTFREELIST on mnt_tmpfreevnodelist")); 1080 vp->v_mflag &= ~VMP_TMPMNTFREELIST; 1081 } 1082 mtx_lock(&vnode_free_list_mtx); 1083 TAILQ_CONCAT(&vnode_free_list, &mp->mnt_tmpfreevnodelist, v_actfreelist); 1084 freevnodes += mp->mnt_tmpfreevnodelistsize; 1085 mtx_unlock(&vnode_free_list_mtx); 1086 mp->mnt_tmpfreevnodelistsize = 0; 1087 } 1088 1089 static void 1090 vnlru_return_batch(struct mount *mp) 1091 { 1092 1093 mtx_lock(&mp->mnt_listmtx); 1094 vnlru_return_batch_locked(mp); 1095 mtx_unlock(&mp->mnt_listmtx); 1096 } 1097 1098 static void 1099 vnlru_return_batches(struct vfsops *mnt_op) 1100 { 1101 struct mount *mp, *nmp; 1102 bool need_unbusy; 1103 1104 mtx_lock(&mountlist_mtx); 1105 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) { 1106 need_unbusy = false; 1107 if (mnt_op != NULL && mp->mnt_op != mnt_op) 1108 goto next; 1109 if (mp->mnt_tmpfreevnodelistsize == 0) 1110 goto next; 1111 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK) == 0) { 1112 vnlru_return_batch(mp); 1113 need_unbusy = true; 1114 mtx_lock(&mountlist_mtx); 1115 } 1116 next: 1117 nmp = TAILQ_NEXT(mp, mnt_list); 1118 if (need_unbusy) 1119 vfs_unbusy(mp); 1120 } 1121 mtx_unlock(&mountlist_mtx); 1122 } 1123 1124 /* 1125 * Attempt to recycle vnodes in a context that is always safe to block. 1126 * Calling vlrurecycle() from the bowels of filesystem code has some 1127 * interesting deadlock problems. 1128 */ 1129 static struct proc *vnlruproc; 1130 static int vnlruproc_sig; 1131 1132 static void 1133 vnlru_proc(void) 1134 { 1135 struct mount *mp, *nmp; 1136 unsigned long ofreevnodes, onumvnodes; 1137 int done, force, reclaim_nc_src, trigger, usevnodes; 1138 1139 EVENTHANDLER_REGISTER(shutdown_pre_sync, kproc_shutdown, vnlruproc, 1140 SHUTDOWN_PRI_FIRST); 1141 1142 force = 0; 1143 for (;;) { 1144 kproc_suspend_check(vnlruproc); 1145 mtx_lock(&vnode_free_list_mtx); 1146 /* 1147 * If numvnodes is too large (due to desiredvnodes being 1148 * adjusted using its sysctl, or emergency growth), first 1149 * try to reduce it by discarding from the free list. 1150 */ 1151 if (numvnodes > desiredvnodes) 1152 vnlru_free_locked(numvnodes - desiredvnodes, NULL); 1153 /* 1154 * Sleep if the vnode cache is in a good state. This is 1155 * when it is not over-full and has space for about a 4% 1156 * or 9% expansion (by growing its size or inexcessively 1157 * reducing its free list). Otherwise, try to reclaim 1158 * space for a 10% expansion. 1159 */ 1160 if (vstir && force == 0) { 1161 force = 1; 1162 vstir = 0; 1163 } 1164 if (vspace() >= vlowat && force == 0) { 1165 vnlruproc_sig = 0; 1166 wakeup(&vnlruproc_sig); 1167 msleep(vnlruproc, &vnode_free_list_mtx, 1168 PVFS|PDROP, "vlruwt", hz); 1169 continue; 1170 } 1171 mtx_unlock(&vnode_free_list_mtx); 1172 done = 0; 1173 ofreevnodes = freevnodes; 1174 onumvnodes = numvnodes; 1175 /* 1176 * Calculate parameters for recycling. These are the same 1177 * throughout the loop to give some semblance of fairness. 1178 * The trigger point is to avoid recycling vnodes with lots 1179 * of resident pages. We aren't trying to free memory; we 1180 * are trying to recycle or at least free vnodes. 1181 */ 1182 if (numvnodes <= desiredvnodes) 1183 usevnodes = numvnodes - freevnodes; 1184 else 1185 usevnodes = numvnodes; 1186 if (usevnodes <= 0) 1187 usevnodes = 1; 1188 /* 1189 * The trigger value is is chosen to give a conservatively 1190 * large value to ensure that it alone doesn't prevent 1191 * making progress. The value can easily be so large that 1192 * it is effectively infinite in some congested and 1193 * misconfigured cases, and this is necessary. Normally 1194 * it is about 8 to 100 (pages), which is quite large. 1195 */ 1196 trigger = vm_cnt.v_page_count * 2 / usevnodes; 1197 if (force < 2) 1198 trigger = vsmalltrigger; 1199 reclaim_nc_src = force >= 3; 1200 mtx_lock(&mountlist_mtx); 1201 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) { 1202 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) { 1203 nmp = TAILQ_NEXT(mp, mnt_list); 1204 continue; 1205 } 1206 done += vlrureclaim(mp, reclaim_nc_src, trigger); 1207 mtx_lock(&mountlist_mtx); 1208 nmp = TAILQ_NEXT(mp, mnt_list); 1209 vfs_unbusy(mp); 1210 } 1211 mtx_unlock(&mountlist_mtx); 1212 if (onumvnodes > desiredvnodes && numvnodes <= desiredvnodes) 1213 uma_reclaim(); 1214 if (done == 0) { 1215 if (force == 0 || force == 1) { 1216 force = 2; 1217 continue; 1218 } 1219 if (force == 2) { 1220 force = 3; 1221 continue; 1222 } 1223 force = 0; 1224 vnlru_nowhere++; 1225 tsleep(vnlruproc, PPAUSE, "vlrup", hz * 3); 1226 } else 1227 kern_yield(PRI_USER); 1228 /* 1229 * After becoming active to expand above low water, keep 1230 * active until above high water. 1231 */ 1232 force = vspace() < vhiwat; 1233 } 1234 } 1235 1236 static struct kproc_desc vnlru_kp = { 1237 "vnlru", 1238 vnlru_proc, 1239 &vnlruproc 1240 }; 1241 SYSINIT(vnlru, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, 1242 &vnlru_kp); 1243 1244 /* 1245 * Routines having to do with the management of the vnode table. 1246 */ 1247 1248 /* 1249 * Try to recycle a freed vnode. We abort if anyone picks up a reference 1250 * before we actually vgone(). This function must be called with the vnode 1251 * held to prevent the vnode from being returned to the free list midway 1252 * through vgone(). 1253 */ 1254 static int 1255 vtryrecycle(struct vnode *vp) 1256 { 1257 struct mount *vnmp; 1258 1259 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 1260 VNASSERT(vp->v_holdcnt, vp, 1261 ("vtryrecycle: Recycling vp %p without a reference.", vp)); 1262 /* 1263 * This vnode may found and locked via some other list, if so we 1264 * can't recycle it yet. 1265 */ 1266 if (VOP_LOCK(vp, LK_EXCLUSIVE | LK_NOWAIT) != 0) { 1267 CTR2(KTR_VFS, 1268 "%s: impossible to recycle, vp %p lock is already held", 1269 __func__, vp); 1270 return (EWOULDBLOCK); 1271 } 1272 /* 1273 * Don't recycle if its filesystem is being suspended. 1274 */ 1275 if (vn_start_write(vp, &vnmp, V_NOWAIT) != 0) { 1276 VOP_UNLOCK(vp, 0); 1277 CTR2(KTR_VFS, 1278 "%s: impossible to recycle, cannot start the write for %p", 1279 __func__, vp); 1280 return (EBUSY); 1281 } 1282 /* 1283 * If we got this far, we need to acquire the interlock and see if 1284 * anyone picked up this vnode from another list. If not, we will 1285 * mark it with DOOMED via vgonel() so that anyone who does find it 1286 * will skip over it. 1287 */ 1288 VI_LOCK(vp); 1289 if (vp->v_usecount) { 1290 VOP_UNLOCK(vp, LK_INTERLOCK); 1291 vn_finished_write(vnmp); 1292 CTR2(KTR_VFS, 1293 "%s: impossible to recycle, %p is already referenced", 1294 __func__, vp); 1295 return (EBUSY); 1296 } 1297 if ((vp->v_iflag & VI_DOOMED) == 0) { 1298 counter_u64_add(recycles_count, 1); 1299 vgonel(vp); 1300 } 1301 VOP_UNLOCK(vp, LK_INTERLOCK); 1302 vn_finished_write(vnmp); 1303 return (0); 1304 } 1305 1306 static void 1307 vcheckspace(void) 1308 { 1309 1310 if (vspace() < vlowat && vnlruproc_sig == 0) { 1311 vnlruproc_sig = 1; 1312 wakeup(vnlruproc); 1313 } 1314 } 1315 1316 /* 1317 * Wait if necessary for space for a new vnode. 1318 */ 1319 static int 1320 getnewvnode_wait(int suspended) 1321 { 1322 1323 mtx_assert(&vnode_free_list_mtx, MA_OWNED); 1324 if (numvnodes >= desiredvnodes) { 1325 if (suspended) { 1326 /* 1327 * The file system is being suspended. We cannot 1328 * risk a deadlock here, so allow allocation of 1329 * another vnode even if this would give too many. 1330 */ 1331 return (0); 1332 } 1333 if (vnlruproc_sig == 0) { 1334 vnlruproc_sig = 1; /* avoid unnecessary wakeups */ 1335 wakeup(vnlruproc); 1336 } 1337 msleep(&vnlruproc_sig, &vnode_free_list_mtx, PVFS, 1338 "vlruwk", hz); 1339 } 1340 /* Post-adjust like the pre-adjust in getnewvnode(). */ 1341 if (numvnodes + 1 > desiredvnodes && freevnodes > 1) 1342 vnlru_free_locked(1, NULL); 1343 return (numvnodes >= desiredvnodes ? ENFILE : 0); 1344 } 1345 1346 /* 1347 * This hack is fragile, and probably not needed any more now that the 1348 * watermark handling works. 1349 */ 1350 void 1351 getnewvnode_reserve(u_int count) 1352 { 1353 struct thread *td; 1354 1355 /* Pre-adjust like the pre-adjust in getnewvnode(), with any count. */ 1356 /* XXX no longer so quick, but this part is not racy. */ 1357 mtx_lock(&vnode_free_list_mtx); 1358 if (numvnodes + count > desiredvnodes && freevnodes > wantfreevnodes) 1359 vnlru_free_locked(ulmin(numvnodes + count - desiredvnodes, 1360 freevnodes - wantfreevnodes), NULL); 1361 mtx_unlock(&vnode_free_list_mtx); 1362 1363 td = curthread; 1364 /* First try to be quick and racy. */ 1365 if (atomic_fetchadd_long(&numvnodes, count) + count <= desiredvnodes) { 1366 td->td_vp_reserv += count; 1367 vcheckspace(); /* XXX no longer so quick, but more racy */ 1368 return; 1369 } else 1370 atomic_subtract_long(&numvnodes, count); 1371 1372 mtx_lock(&vnode_free_list_mtx); 1373 while (count > 0) { 1374 if (getnewvnode_wait(0) == 0) { 1375 count--; 1376 td->td_vp_reserv++; 1377 atomic_add_long(&numvnodes, 1); 1378 } 1379 } 1380 vcheckspace(); 1381 mtx_unlock(&vnode_free_list_mtx); 1382 } 1383 1384 /* 1385 * This hack is fragile, especially if desiredvnodes or wantvnodes are 1386 * misconfgured or changed significantly. Reducing desiredvnodes below 1387 * the reserved amount should cause bizarre behaviour like reducing it 1388 * below the number of active vnodes -- the system will try to reduce 1389 * numvnodes to match, but should fail, so the subtraction below should 1390 * not overflow. 1391 */ 1392 void 1393 getnewvnode_drop_reserve(void) 1394 { 1395 struct thread *td; 1396 1397 td = curthread; 1398 atomic_subtract_long(&numvnodes, td->td_vp_reserv); 1399 td->td_vp_reserv = 0; 1400 } 1401 1402 /* 1403 * Return the next vnode from the free list. 1404 */ 1405 int 1406 getnewvnode(const char *tag, struct mount *mp, struct vop_vector *vops, 1407 struct vnode **vpp) 1408 { 1409 struct vnode *vp; 1410 struct thread *td; 1411 struct lock_object *lo; 1412 static int cyclecount; 1413 int error; 1414 1415 CTR3(KTR_VFS, "%s: mp %p with tag %s", __func__, mp, tag); 1416 vp = NULL; 1417 td = curthread; 1418 if (td->td_vp_reserv > 0) { 1419 td->td_vp_reserv -= 1; 1420 goto alloc; 1421 } 1422 mtx_lock(&vnode_free_list_mtx); 1423 if (numvnodes < desiredvnodes) 1424 cyclecount = 0; 1425 else if (cyclecount++ >= freevnodes) { 1426 cyclecount = 0; 1427 vstir = 1; 1428 } 1429 /* 1430 * Grow the vnode cache if it will not be above its target max 1431 * after growing. Otherwise, if the free list is nonempty, try 1432 * to reclaim 1 item from it before growing the cache (possibly 1433 * above its target max if the reclamation failed or is delayed). 1434 * Otherwise, wait for some space. In all cases, schedule 1435 * vnlru_proc() if we are getting short of space. The watermarks 1436 * should be chosen so that we never wait or even reclaim from 1437 * the free list to below its target minimum. 1438 */ 1439 if (numvnodes + 1 <= desiredvnodes) 1440 ; 1441 else if (freevnodes > 0) 1442 vnlru_free_locked(1, NULL); 1443 else { 1444 error = getnewvnode_wait(mp != NULL && (mp->mnt_kern_flag & 1445 MNTK_SUSPEND)); 1446 #if 0 /* XXX Not all VFS_VGET/ffs_vget callers check returns. */ 1447 if (error != 0) { 1448 mtx_unlock(&vnode_free_list_mtx); 1449 return (error); 1450 } 1451 #endif 1452 } 1453 vcheckspace(); 1454 atomic_add_long(&numvnodes, 1); 1455 mtx_unlock(&vnode_free_list_mtx); 1456 alloc: 1457 counter_u64_add(vnodes_created, 1); 1458 vp = (struct vnode *) uma_zalloc(vnode_zone, M_WAITOK); 1459 /* 1460 * Locks are given the generic name "vnode" when created. 1461 * Follow the historic practice of using the filesystem 1462 * name when they allocated, e.g., "zfs", "ufs", "nfs, etc. 1463 * 1464 * Locks live in a witness group keyed on their name. Thus, 1465 * when a lock is renamed, it must also move from the witness 1466 * group of its old name to the witness group of its new name. 1467 * 1468 * The change only needs to be made when the vnode moves 1469 * from one filesystem type to another. We ensure that each 1470 * filesystem use a single static name pointer for its tag so 1471 * that we can compare pointers rather than doing a strcmp(). 1472 */ 1473 lo = &vp->v_vnlock->lock_object; 1474 if (lo->lo_name != tag) { 1475 lo->lo_name = tag; 1476 WITNESS_DESTROY(lo); 1477 WITNESS_INIT(lo, tag); 1478 } 1479 /* 1480 * By default, don't allow shared locks unless filesystems opt-in. 1481 */ 1482 vp->v_vnlock->lock_object.lo_flags |= LK_NOSHARE; 1483 /* 1484 * Finalize various vnode identity bits. 1485 */ 1486 KASSERT(vp->v_object == NULL, ("stale v_object %p", vp)); 1487 KASSERT(vp->v_lockf == NULL, ("stale v_lockf %p", vp)); 1488 KASSERT(vp->v_pollinfo == NULL, ("stale v_pollinfo %p", vp)); 1489 vp->v_type = VNON; 1490 vp->v_tag = tag; 1491 vp->v_op = vops; 1492 v_init_counters(vp); 1493 vp->v_bufobj.bo_ops = &buf_ops_bio; 1494 #ifdef DIAGNOSTIC 1495 if (mp == NULL && vops != &dead_vnodeops) 1496 printf("NULL mp in getnewvnode(9), tag %s\n", tag); 1497 #endif 1498 #ifdef MAC 1499 mac_vnode_init(vp); 1500 if (mp != NULL && (mp->mnt_flag & MNT_MULTILABEL) == 0) 1501 mac_vnode_associate_singlelabel(mp, vp); 1502 #endif 1503 if (mp != NULL) { 1504 vp->v_bufobj.bo_bsize = mp->mnt_stat.f_iosize; 1505 if ((mp->mnt_kern_flag & MNTK_NOKNOTE) != 0) 1506 vp->v_vflag |= VV_NOKNOTE; 1507 } 1508 1509 /* 1510 * For the filesystems which do not use vfs_hash_insert(), 1511 * still initialize v_hash to have vfs_hash_index() useful. 1512 * E.g., nullfs uses vfs_hash_index() on the lower vnode for 1513 * its own hashing. 1514 */ 1515 vp->v_hash = (uintptr_t)vp >> vnsz2log; 1516 1517 *vpp = vp; 1518 return (0); 1519 } 1520 1521 /* 1522 * Delete from old mount point vnode list, if on one. 1523 */ 1524 static void 1525 delmntque(struct vnode *vp) 1526 { 1527 struct mount *mp; 1528 int active; 1529 1530 mp = vp->v_mount; 1531 if (mp == NULL) 1532 return; 1533 MNT_ILOCK(mp); 1534 VI_LOCK(vp); 1535 KASSERT(mp->mnt_activevnodelistsize <= mp->mnt_nvnodelistsize, 1536 ("Active vnode list size %d > Vnode list size %d", 1537 mp->mnt_activevnodelistsize, mp->mnt_nvnodelistsize)); 1538 active = vp->v_iflag & VI_ACTIVE; 1539 vp->v_iflag &= ~VI_ACTIVE; 1540 if (active) { 1541 mtx_lock(&mp->mnt_listmtx); 1542 TAILQ_REMOVE(&mp->mnt_activevnodelist, vp, v_actfreelist); 1543 mp->mnt_activevnodelistsize--; 1544 mtx_unlock(&mp->mnt_listmtx); 1545 } 1546 vp->v_mount = NULL; 1547 VI_UNLOCK(vp); 1548 VNASSERT(mp->mnt_nvnodelistsize > 0, vp, 1549 ("bad mount point vnode list size")); 1550 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes); 1551 mp->mnt_nvnodelistsize--; 1552 MNT_REL(mp); 1553 MNT_IUNLOCK(mp); 1554 } 1555 1556 static void 1557 insmntque_stddtr(struct vnode *vp, void *dtr_arg) 1558 { 1559 1560 vp->v_data = NULL; 1561 vp->v_op = &dead_vnodeops; 1562 vgone(vp); 1563 vput(vp); 1564 } 1565 1566 /* 1567 * Insert into list of vnodes for the new mount point, if available. 1568 */ 1569 int 1570 insmntque1(struct vnode *vp, struct mount *mp, 1571 void (*dtr)(struct vnode *, void *), void *dtr_arg) 1572 { 1573 1574 KASSERT(vp->v_mount == NULL, 1575 ("insmntque: vnode already on per mount vnode list")); 1576 VNASSERT(mp != NULL, vp, ("Don't call insmntque(foo, NULL)")); 1577 ASSERT_VOP_ELOCKED(vp, "insmntque: non-locked vp"); 1578 1579 /* 1580 * We acquire the vnode interlock early to ensure that the 1581 * vnode cannot be recycled by another process releasing a 1582 * holdcnt on it before we get it on both the vnode list 1583 * and the active vnode list. The mount mutex protects only 1584 * manipulation of the vnode list and the vnode freelist 1585 * mutex protects only manipulation of the active vnode list. 1586 * Hence the need to hold the vnode interlock throughout. 1587 */ 1588 MNT_ILOCK(mp); 1589 VI_LOCK(vp); 1590 if (((mp->mnt_kern_flag & MNTK_NOINSMNTQ) != 0 && 1591 ((mp->mnt_kern_flag & MNTK_UNMOUNTF) != 0 || 1592 mp->mnt_nvnodelistsize == 0)) && 1593 (vp->v_vflag & VV_FORCEINSMQ) == 0) { 1594 VI_UNLOCK(vp); 1595 MNT_IUNLOCK(mp); 1596 if (dtr != NULL) 1597 dtr(vp, dtr_arg); 1598 return (EBUSY); 1599 } 1600 vp->v_mount = mp; 1601 MNT_REF(mp); 1602 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes); 1603 VNASSERT(mp->mnt_nvnodelistsize >= 0, vp, 1604 ("neg mount point vnode list size")); 1605 mp->mnt_nvnodelistsize++; 1606 KASSERT((vp->v_iflag & VI_ACTIVE) == 0, 1607 ("Activating already active vnode")); 1608 vp->v_iflag |= VI_ACTIVE; 1609 mtx_lock(&mp->mnt_listmtx); 1610 TAILQ_INSERT_HEAD(&mp->mnt_activevnodelist, vp, v_actfreelist); 1611 mp->mnt_activevnodelistsize++; 1612 mtx_unlock(&mp->mnt_listmtx); 1613 VI_UNLOCK(vp); 1614 MNT_IUNLOCK(mp); 1615 return (0); 1616 } 1617 1618 int 1619 insmntque(struct vnode *vp, struct mount *mp) 1620 { 1621 1622 return (insmntque1(vp, mp, insmntque_stddtr, NULL)); 1623 } 1624 1625 /* 1626 * Flush out and invalidate all buffers associated with a bufobj 1627 * Called with the underlying object locked. 1628 */ 1629 int 1630 bufobj_invalbuf(struct bufobj *bo, int flags, int slpflag, int slptimeo) 1631 { 1632 int error; 1633 1634 BO_LOCK(bo); 1635 if (flags & V_SAVE) { 1636 error = bufobj_wwait(bo, slpflag, slptimeo); 1637 if (error) { 1638 BO_UNLOCK(bo); 1639 return (error); 1640 } 1641 if (bo->bo_dirty.bv_cnt > 0) { 1642 BO_UNLOCK(bo); 1643 if ((error = BO_SYNC(bo, MNT_WAIT)) != 0) 1644 return (error); 1645 /* 1646 * XXX We could save a lock/unlock if this was only 1647 * enabled under INVARIANTS 1648 */ 1649 BO_LOCK(bo); 1650 if (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0) 1651 panic("vinvalbuf: dirty bufs"); 1652 } 1653 } 1654 /* 1655 * If you alter this loop please notice that interlock is dropped and 1656 * reacquired in flushbuflist. Special care is needed to ensure that 1657 * no race conditions occur from this. 1658 */ 1659 do { 1660 error = flushbuflist(&bo->bo_clean, 1661 flags, bo, slpflag, slptimeo); 1662 if (error == 0 && !(flags & V_CLEANONLY)) 1663 error = flushbuflist(&bo->bo_dirty, 1664 flags, bo, slpflag, slptimeo); 1665 if (error != 0 && error != EAGAIN) { 1666 BO_UNLOCK(bo); 1667 return (error); 1668 } 1669 } while (error != 0); 1670 1671 /* 1672 * Wait for I/O to complete. XXX needs cleaning up. The vnode can 1673 * have write I/O in-progress but if there is a VM object then the 1674 * VM object can also have read-I/O in-progress. 1675 */ 1676 do { 1677 bufobj_wwait(bo, 0, 0); 1678 if ((flags & V_VMIO) == 0) { 1679 BO_UNLOCK(bo); 1680 if (bo->bo_object != NULL) { 1681 VM_OBJECT_WLOCK(bo->bo_object); 1682 vm_object_pip_wait(bo->bo_object, "bovlbx"); 1683 VM_OBJECT_WUNLOCK(bo->bo_object); 1684 } 1685 BO_LOCK(bo); 1686 } 1687 } while (bo->bo_numoutput > 0); 1688 BO_UNLOCK(bo); 1689 1690 /* 1691 * Destroy the copy in the VM cache, too. 1692 */ 1693 if (bo->bo_object != NULL && 1694 (flags & (V_ALT | V_NORMAL | V_CLEANONLY | V_VMIO)) == 0) { 1695 VM_OBJECT_WLOCK(bo->bo_object); 1696 vm_object_page_remove(bo->bo_object, 0, 0, (flags & V_SAVE) ? 1697 OBJPR_CLEANONLY : 0); 1698 VM_OBJECT_WUNLOCK(bo->bo_object); 1699 } 1700 1701 #ifdef INVARIANTS 1702 BO_LOCK(bo); 1703 if ((flags & (V_ALT | V_NORMAL | V_CLEANONLY | V_VMIO | 1704 V_ALLOWCLEAN)) == 0 && (bo->bo_dirty.bv_cnt > 0 || 1705 bo->bo_clean.bv_cnt > 0)) 1706 panic("vinvalbuf: flush failed"); 1707 if ((flags & (V_ALT | V_NORMAL | V_CLEANONLY | V_VMIO)) == 0 && 1708 bo->bo_dirty.bv_cnt > 0) 1709 panic("vinvalbuf: flush dirty failed"); 1710 BO_UNLOCK(bo); 1711 #endif 1712 return (0); 1713 } 1714 1715 /* 1716 * Flush out and invalidate all buffers associated with a vnode. 1717 * Called with the underlying object locked. 1718 */ 1719 int 1720 vinvalbuf(struct vnode *vp, int flags, int slpflag, int slptimeo) 1721 { 1722 1723 CTR3(KTR_VFS, "%s: vp %p with flags %d", __func__, vp, flags); 1724 ASSERT_VOP_LOCKED(vp, "vinvalbuf"); 1725 if (vp->v_object != NULL && vp->v_object->handle != vp) 1726 return (0); 1727 return (bufobj_invalbuf(&vp->v_bufobj, flags, slpflag, slptimeo)); 1728 } 1729 1730 /* 1731 * Flush out buffers on the specified list. 1732 * 1733 */ 1734 static int 1735 flushbuflist(struct bufv *bufv, int flags, struct bufobj *bo, int slpflag, 1736 int slptimeo) 1737 { 1738 struct buf *bp, *nbp; 1739 int retval, error; 1740 daddr_t lblkno; 1741 b_xflags_t xflags; 1742 1743 ASSERT_BO_WLOCKED(bo); 1744 1745 retval = 0; 1746 TAILQ_FOREACH_SAFE(bp, &bufv->bv_hd, b_bobufs, nbp) { 1747 if (((flags & V_NORMAL) && (bp->b_xflags & BX_ALTDATA)) || 1748 ((flags & V_ALT) && (bp->b_xflags & BX_ALTDATA) == 0)) { 1749 continue; 1750 } 1751 if (nbp != NULL) { 1752 lblkno = nbp->b_lblkno; 1753 xflags = nbp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN); 1754 } 1755 retval = EAGAIN; 1756 error = BUF_TIMELOCK(bp, 1757 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, BO_LOCKPTR(bo), 1758 "flushbuf", slpflag, slptimeo); 1759 if (error) { 1760 BO_LOCK(bo); 1761 return (error != ENOLCK ? error : EAGAIN); 1762 } 1763 KASSERT(bp->b_bufobj == bo, 1764 ("bp %p wrong b_bufobj %p should be %p", 1765 bp, bp->b_bufobj, bo)); 1766 /* 1767 * XXX Since there are no node locks for NFS, I 1768 * believe there is a slight chance that a delayed 1769 * write will occur while sleeping just above, so 1770 * check for it. 1771 */ 1772 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) && 1773 (flags & V_SAVE)) { 1774 bremfree(bp); 1775 bp->b_flags |= B_ASYNC; 1776 bwrite(bp); 1777 BO_LOCK(bo); 1778 return (EAGAIN); /* XXX: why not loop ? */ 1779 } 1780 bremfree(bp); 1781 bp->b_flags |= (B_INVAL | B_RELBUF); 1782 bp->b_flags &= ~B_ASYNC; 1783 brelse(bp); 1784 BO_LOCK(bo); 1785 if (nbp == NULL) 1786 break; 1787 nbp = gbincore(bo, lblkno); 1788 if (nbp == NULL || (nbp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) 1789 != xflags) 1790 break; /* nbp invalid */ 1791 } 1792 return (retval); 1793 } 1794 1795 int 1796 bnoreuselist(struct bufv *bufv, struct bufobj *bo, daddr_t startn, daddr_t endn) 1797 { 1798 struct buf *bp; 1799 int error; 1800 daddr_t lblkno; 1801 1802 ASSERT_BO_LOCKED(bo); 1803 1804 for (lblkno = startn;;) { 1805 again: 1806 bp = BUF_PCTRIE_LOOKUP_GE(&bufv->bv_root, lblkno); 1807 if (bp == NULL || bp->b_lblkno >= endn || 1808 bp->b_lblkno < startn) 1809 break; 1810 error = BUF_TIMELOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL | 1811 LK_INTERLOCK, BO_LOCKPTR(bo), "brlsfl", 0, 0); 1812 if (error != 0) { 1813 BO_RLOCK(bo); 1814 if (error == ENOLCK) 1815 goto again; 1816 return (error); 1817 } 1818 KASSERT(bp->b_bufobj == bo, 1819 ("bp %p wrong b_bufobj %p should be %p", 1820 bp, bp->b_bufobj, bo)); 1821 lblkno = bp->b_lblkno + 1; 1822 if ((bp->b_flags & B_MANAGED) == 0) 1823 bremfree(bp); 1824 bp->b_flags |= B_RELBUF; 1825 /* 1826 * In the VMIO case, use the B_NOREUSE flag to hint that the 1827 * pages backing each buffer in the range are unlikely to be 1828 * reused. Dirty buffers will have the hint applied once 1829 * they've been written. 1830 */ 1831 if (bp->b_vp->v_object != NULL) 1832 bp->b_flags |= B_NOREUSE; 1833 brelse(bp); 1834 BO_RLOCK(bo); 1835 } 1836 return (0); 1837 } 1838 1839 /* 1840 * Truncate a file's buffer and pages to a specified length. This 1841 * is in lieu of the old vinvalbuf mechanism, which performed unneeded 1842 * sync activity. 1843 */ 1844 int 1845 vtruncbuf(struct vnode *vp, struct ucred *cred, off_t length, int blksize) 1846 { 1847 struct buf *bp, *nbp; 1848 int anyfreed; 1849 int trunclbn; 1850 struct bufobj *bo; 1851 1852 CTR5(KTR_VFS, "%s: vp %p with cred %p and block %d:%ju", __func__, 1853 vp, cred, blksize, (uintmax_t)length); 1854 1855 /* 1856 * Round up to the *next* lbn. 1857 */ 1858 trunclbn = howmany(length, blksize); 1859 1860 ASSERT_VOP_LOCKED(vp, "vtruncbuf"); 1861 restart: 1862 bo = &vp->v_bufobj; 1863 BO_LOCK(bo); 1864 anyfreed = 1; 1865 for (;anyfreed;) { 1866 anyfreed = 0; 1867 TAILQ_FOREACH_SAFE(bp, &bo->bo_clean.bv_hd, b_bobufs, nbp) { 1868 if (bp->b_lblkno < trunclbn) 1869 continue; 1870 if (BUF_LOCK(bp, 1871 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, 1872 BO_LOCKPTR(bo)) == ENOLCK) 1873 goto restart; 1874 1875 bremfree(bp); 1876 bp->b_flags |= (B_INVAL | B_RELBUF); 1877 bp->b_flags &= ~B_ASYNC; 1878 brelse(bp); 1879 anyfreed = 1; 1880 1881 BO_LOCK(bo); 1882 if (nbp != NULL && 1883 (((nbp->b_xflags & BX_VNCLEAN) == 0) || 1884 (nbp->b_vp != vp) || 1885 (nbp->b_flags & B_DELWRI))) { 1886 BO_UNLOCK(bo); 1887 goto restart; 1888 } 1889 } 1890 1891 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) { 1892 if (bp->b_lblkno < trunclbn) 1893 continue; 1894 if (BUF_LOCK(bp, 1895 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, 1896 BO_LOCKPTR(bo)) == ENOLCK) 1897 goto restart; 1898 bremfree(bp); 1899 bp->b_flags |= (B_INVAL | B_RELBUF); 1900 bp->b_flags &= ~B_ASYNC; 1901 brelse(bp); 1902 anyfreed = 1; 1903 1904 BO_LOCK(bo); 1905 if (nbp != NULL && 1906 (((nbp->b_xflags & BX_VNDIRTY) == 0) || 1907 (nbp->b_vp != vp) || 1908 (nbp->b_flags & B_DELWRI) == 0)) { 1909 BO_UNLOCK(bo); 1910 goto restart; 1911 } 1912 } 1913 } 1914 1915 if (length > 0) { 1916 restartsync: 1917 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) { 1918 if (bp->b_lblkno > 0) 1919 continue; 1920 /* 1921 * Since we hold the vnode lock this should only 1922 * fail if we're racing with the buf daemon. 1923 */ 1924 if (BUF_LOCK(bp, 1925 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, 1926 BO_LOCKPTR(bo)) == ENOLCK) { 1927 goto restart; 1928 } 1929 VNASSERT((bp->b_flags & B_DELWRI), vp, 1930 ("buf(%p) on dirty queue without DELWRI", bp)); 1931 1932 bremfree(bp); 1933 bawrite(bp); 1934 BO_LOCK(bo); 1935 goto restartsync; 1936 } 1937 } 1938 1939 bufobj_wwait(bo, 0, 0); 1940 BO_UNLOCK(bo); 1941 vnode_pager_setsize(vp, length); 1942 1943 return (0); 1944 } 1945 1946 static void 1947 buf_vlist_remove(struct buf *bp) 1948 { 1949 struct bufv *bv; 1950 1951 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp)); 1952 ASSERT_BO_WLOCKED(bp->b_bufobj); 1953 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) != 1954 (BX_VNDIRTY|BX_VNCLEAN), 1955 ("buf_vlist_remove: Buf %p is on two lists", bp)); 1956 if (bp->b_xflags & BX_VNDIRTY) 1957 bv = &bp->b_bufobj->bo_dirty; 1958 else 1959 bv = &bp->b_bufobj->bo_clean; 1960 BUF_PCTRIE_REMOVE(&bv->bv_root, bp->b_lblkno); 1961 TAILQ_REMOVE(&bv->bv_hd, bp, b_bobufs); 1962 bv->bv_cnt--; 1963 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN); 1964 } 1965 1966 /* 1967 * Add the buffer to the sorted clean or dirty block list. 1968 * 1969 * NOTE: xflags is passed as a constant, optimizing this inline function! 1970 */ 1971 static void 1972 buf_vlist_add(struct buf *bp, struct bufobj *bo, b_xflags_t xflags) 1973 { 1974 struct bufv *bv; 1975 struct buf *n; 1976 int error; 1977 1978 ASSERT_BO_WLOCKED(bo); 1979 KASSERT((xflags & BX_VNDIRTY) == 0 || (bo->bo_flag & BO_DEAD) == 0, 1980 ("dead bo %p", bo)); 1981 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0, 1982 ("buf_vlist_add: Buf %p has existing xflags %d", bp, bp->b_xflags)); 1983 bp->b_xflags |= xflags; 1984 if (xflags & BX_VNDIRTY) 1985 bv = &bo->bo_dirty; 1986 else 1987 bv = &bo->bo_clean; 1988 1989 /* 1990 * Keep the list ordered. Optimize empty list insertion. Assume 1991 * we tend to grow at the tail so lookup_le should usually be cheaper 1992 * than _ge. 1993 */ 1994 if (bv->bv_cnt == 0 || 1995 bp->b_lblkno > TAILQ_LAST(&bv->bv_hd, buflists)->b_lblkno) 1996 TAILQ_INSERT_TAIL(&bv->bv_hd, bp, b_bobufs); 1997 else if ((n = BUF_PCTRIE_LOOKUP_LE(&bv->bv_root, bp->b_lblkno)) == NULL) 1998 TAILQ_INSERT_HEAD(&bv->bv_hd, bp, b_bobufs); 1999 else 2000 TAILQ_INSERT_AFTER(&bv->bv_hd, n, bp, b_bobufs); 2001 error = BUF_PCTRIE_INSERT(&bv->bv_root, bp); 2002 if (error) 2003 panic("buf_vlist_add: Preallocated nodes insufficient."); 2004 bv->bv_cnt++; 2005 } 2006 2007 /* 2008 * Look up a buffer using the buffer tries. 2009 */ 2010 struct buf * 2011 gbincore(struct bufobj *bo, daddr_t lblkno) 2012 { 2013 struct buf *bp; 2014 2015 ASSERT_BO_LOCKED(bo); 2016 bp = BUF_PCTRIE_LOOKUP(&bo->bo_clean.bv_root, lblkno); 2017 if (bp != NULL) 2018 return (bp); 2019 return BUF_PCTRIE_LOOKUP(&bo->bo_dirty.bv_root, lblkno); 2020 } 2021 2022 /* 2023 * Associate a buffer with a vnode. 2024 */ 2025 void 2026 bgetvp(struct vnode *vp, struct buf *bp) 2027 { 2028 struct bufobj *bo; 2029 2030 bo = &vp->v_bufobj; 2031 ASSERT_BO_WLOCKED(bo); 2032 VNASSERT(bp->b_vp == NULL, bp->b_vp, ("bgetvp: not free")); 2033 2034 CTR3(KTR_BUF, "bgetvp(%p) vp %p flags %X", bp, vp, bp->b_flags); 2035 VNASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0, vp, 2036 ("bgetvp: bp already attached! %p", bp)); 2037 2038 vhold(vp); 2039 bp->b_vp = vp; 2040 bp->b_bufobj = bo; 2041 /* 2042 * Insert onto list for new vnode. 2043 */ 2044 buf_vlist_add(bp, bo, BX_VNCLEAN); 2045 } 2046 2047 /* 2048 * Disassociate a buffer from a vnode. 2049 */ 2050 void 2051 brelvp(struct buf *bp) 2052 { 2053 struct bufobj *bo; 2054 struct vnode *vp; 2055 2056 CTR3(KTR_BUF, "brelvp(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags); 2057 KASSERT(bp->b_vp != NULL, ("brelvp: NULL")); 2058 2059 /* 2060 * Delete from old vnode list, if on one. 2061 */ 2062 vp = bp->b_vp; /* XXX */ 2063 bo = bp->b_bufobj; 2064 BO_LOCK(bo); 2065 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) 2066 buf_vlist_remove(bp); 2067 else 2068 panic("brelvp: Buffer %p not on queue.", bp); 2069 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) { 2070 bo->bo_flag &= ~BO_ONWORKLST; 2071 mtx_lock(&sync_mtx); 2072 LIST_REMOVE(bo, bo_synclist); 2073 syncer_worklist_len--; 2074 mtx_unlock(&sync_mtx); 2075 } 2076 bp->b_vp = NULL; 2077 bp->b_bufobj = NULL; 2078 BO_UNLOCK(bo); 2079 vdrop(vp); 2080 } 2081 2082 /* 2083 * Add an item to the syncer work queue. 2084 */ 2085 static void 2086 vn_syncer_add_to_worklist(struct bufobj *bo, int delay) 2087 { 2088 int slot; 2089 2090 ASSERT_BO_WLOCKED(bo); 2091 2092 mtx_lock(&sync_mtx); 2093 if (bo->bo_flag & BO_ONWORKLST) 2094 LIST_REMOVE(bo, bo_synclist); 2095 else { 2096 bo->bo_flag |= BO_ONWORKLST; 2097 syncer_worklist_len++; 2098 } 2099 2100 if (delay > syncer_maxdelay - 2) 2101 delay = syncer_maxdelay - 2; 2102 slot = (syncer_delayno + delay) & syncer_mask; 2103 2104 LIST_INSERT_HEAD(&syncer_workitem_pending[slot], bo, bo_synclist); 2105 mtx_unlock(&sync_mtx); 2106 } 2107 2108 static int 2109 sysctl_vfs_worklist_len(SYSCTL_HANDLER_ARGS) 2110 { 2111 int error, len; 2112 2113 mtx_lock(&sync_mtx); 2114 len = syncer_worklist_len - sync_vnode_count; 2115 mtx_unlock(&sync_mtx); 2116 error = SYSCTL_OUT(req, &len, sizeof(len)); 2117 return (error); 2118 } 2119 2120 SYSCTL_PROC(_vfs, OID_AUTO, worklist_len, CTLTYPE_INT | CTLFLAG_RD, NULL, 0, 2121 sysctl_vfs_worklist_len, "I", "Syncer thread worklist length"); 2122 2123 static struct proc *updateproc; 2124 static void sched_sync(void); 2125 static struct kproc_desc up_kp = { 2126 "syncer", 2127 sched_sync, 2128 &updateproc 2129 }; 2130 SYSINIT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp); 2131 2132 static int 2133 sync_vnode(struct synclist *slp, struct bufobj **bo, struct thread *td) 2134 { 2135 struct vnode *vp; 2136 struct mount *mp; 2137 2138 *bo = LIST_FIRST(slp); 2139 if (*bo == NULL) 2140 return (0); 2141 vp = bo2vnode(*bo); 2142 if (VOP_ISLOCKED(vp) != 0 || VI_TRYLOCK(vp) == 0) 2143 return (1); 2144 /* 2145 * We use vhold in case the vnode does not 2146 * successfully sync. vhold prevents the vnode from 2147 * going away when we unlock the sync_mtx so that 2148 * we can acquire the vnode interlock. 2149 */ 2150 vholdl(vp); 2151 mtx_unlock(&sync_mtx); 2152 VI_UNLOCK(vp); 2153 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) { 2154 vdrop(vp); 2155 mtx_lock(&sync_mtx); 2156 return (*bo == LIST_FIRST(slp)); 2157 } 2158 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 2159 (void) VOP_FSYNC(vp, MNT_LAZY, td); 2160 VOP_UNLOCK(vp, 0); 2161 vn_finished_write(mp); 2162 BO_LOCK(*bo); 2163 if (((*bo)->bo_flag & BO_ONWORKLST) != 0) { 2164 /* 2165 * Put us back on the worklist. The worklist 2166 * routine will remove us from our current 2167 * position and then add us back in at a later 2168 * position. 2169 */ 2170 vn_syncer_add_to_worklist(*bo, syncdelay); 2171 } 2172 BO_UNLOCK(*bo); 2173 vdrop(vp); 2174 mtx_lock(&sync_mtx); 2175 return (0); 2176 } 2177 2178 static int first_printf = 1; 2179 2180 /* 2181 * System filesystem synchronizer daemon. 2182 */ 2183 static void 2184 sched_sync(void) 2185 { 2186 struct synclist *next, *slp; 2187 struct bufobj *bo; 2188 long starttime; 2189 struct thread *td = curthread; 2190 int last_work_seen; 2191 int net_worklist_len; 2192 int syncer_final_iter; 2193 int error; 2194 2195 last_work_seen = 0; 2196 syncer_final_iter = 0; 2197 syncer_state = SYNCER_RUNNING; 2198 starttime = time_uptime; 2199 td->td_pflags |= TDP_NORUNNINGBUF; 2200 2201 EVENTHANDLER_REGISTER(shutdown_pre_sync, syncer_shutdown, td->td_proc, 2202 SHUTDOWN_PRI_LAST); 2203 2204 mtx_lock(&sync_mtx); 2205 for (;;) { 2206 if (syncer_state == SYNCER_FINAL_DELAY && 2207 syncer_final_iter == 0) { 2208 mtx_unlock(&sync_mtx); 2209 kproc_suspend_check(td->td_proc); 2210 mtx_lock(&sync_mtx); 2211 } 2212 net_worklist_len = syncer_worklist_len - sync_vnode_count; 2213 if (syncer_state != SYNCER_RUNNING && 2214 starttime != time_uptime) { 2215 if (first_printf) { 2216 printf("\nSyncing disks, vnodes remaining... "); 2217 first_printf = 0; 2218 } 2219 printf("%d ", net_worklist_len); 2220 } 2221 starttime = time_uptime; 2222 2223 /* 2224 * Push files whose dirty time has expired. Be careful 2225 * of interrupt race on slp queue. 2226 * 2227 * Skip over empty worklist slots when shutting down. 2228 */ 2229 do { 2230 slp = &syncer_workitem_pending[syncer_delayno]; 2231 syncer_delayno += 1; 2232 if (syncer_delayno == syncer_maxdelay) 2233 syncer_delayno = 0; 2234 next = &syncer_workitem_pending[syncer_delayno]; 2235 /* 2236 * If the worklist has wrapped since the 2237 * it was emptied of all but syncer vnodes, 2238 * switch to the FINAL_DELAY state and run 2239 * for one more second. 2240 */ 2241 if (syncer_state == SYNCER_SHUTTING_DOWN && 2242 net_worklist_len == 0 && 2243 last_work_seen == syncer_delayno) { 2244 syncer_state = SYNCER_FINAL_DELAY; 2245 syncer_final_iter = SYNCER_SHUTDOWN_SPEEDUP; 2246 } 2247 } while (syncer_state != SYNCER_RUNNING && LIST_EMPTY(slp) && 2248 syncer_worklist_len > 0); 2249 2250 /* 2251 * Keep track of the last time there was anything 2252 * on the worklist other than syncer vnodes. 2253 * Return to the SHUTTING_DOWN state if any 2254 * new work appears. 2255 */ 2256 if (net_worklist_len > 0 || syncer_state == SYNCER_RUNNING) 2257 last_work_seen = syncer_delayno; 2258 if (net_worklist_len > 0 && syncer_state == SYNCER_FINAL_DELAY) 2259 syncer_state = SYNCER_SHUTTING_DOWN; 2260 while (!LIST_EMPTY(slp)) { 2261 error = sync_vnode(slp, &bo, td); 2262 if (error == 1) { 2263 LIST_REMOVE(bo, bo_synclist); 2264 LIST_INSERT_HEAD(next, bo, bo_synclist); 2265 continue; 2266 } 2267 2268 if (first_printf == 0) { 2269 /* 2270 * Drop the sync mutex, because some watchdog 2271 * drivers need to sleep while patting 2272 */ 2273 mtx_unlock(&sync_mtx); 2274 wdog_kern_pat(WD_LASTVAL); 2275 mtx_lock(&sync_mtx); 2276 } 2277 2278 } 2279 if (syncer_state == SYNCER_FINAL_DELAY && syncer_final_iter > 0) 2280 syncer_final_iter--; 2281 /* 2282 * The variable rushjob allows the kernel to speed up the 2283 * processing of the filesystem syncer process. A rushjob 2284 * value of N tells the filesystem syncer to process the next 2285 * N seconds worth of work on its queue ASAP. Currently rushjob 2286 * is used by the soft update code to speed up the filesystem 2287 * syncer process when the incore state is getting so far 2288 * ahead of the disk that the kernel memory pool is being 2289 * threatened with exhaustion. 2290 */ 2291 if (rushjob > 0) { 2292 rushjob -= 1; 2293 continue; 2294 } 2295 /* 2296 * Just sleep for a short period of time between 2297 * iterations when shutting down to allow some I/O 2298 * to happen. 2299 * 2300 * If it has taken us less than a second to process the 2301 * current work, then wait. Otherwise start right over 2302 * again. We can still lose time if any single round 2303 * takes more than two seconds, but it does not really 2304 * matter as we are just trying to generally pace the 2305 * filesystem activity. 2306 */ 2307 if (syncer_state != SYNCER_RUNNING || 2308 time_uptime == starttime) { 2309 thread_lock(td); 2310 sched_prio(td, PPAUSE); 2311 thread_unlock(td); 2312 } 2313 if (syncer_state != SYNCER_RUNNING) 2314 cv_timedwait(&sync_wakeup, &sync_mtx, 2315 hz / SYNCER_SHUTDOWN_SPEEDUP); 2316 else if (time_uptime == starttime) 2317 cv_timedwait(&sync_wakeup, &sync_mtx, hz); 2318 } 2319 } 2320 2321 /* 2322 * Request the syncer daemon to speed up its work. 2323 * We never push it to speed up more than half of its 2324 * normal turn time, otherwise it could take over the cpu. 2325 */ 2326 int 2327 speedup_syncer(void) 2328 { 2329 int ret = 0; 2330 2331 mtx_lock(&sync_mtx); 2332 if (rushjob < syncdelay / 2) { 2333 rushjob += 1; 2334 stat_rush_requests += 1; 2335 ret = 1; 2336 } 2337 mtx_unlock(&sync_mtx); 2338 cv_broadcast(&sync_wakeup); 2339 return (ret); 2340 } 2341 2342 /* 2343 * Tell the syncer to speed up its work and run though its work 2344 * list several times, then tell it to shut down. 2345 */ 2346 static void 2347 syncer_shutdown(void *arg, int howto) 2348 { 2349 2350 if (howto & RB_NOSYNC) 2351 return; 2352 mtx_lock(&sync_mtx); 2353 syncer_state = SYNCER_SHUTTING_DOWN; 2354 rushjob = 0; 2355 mtx_unlock(&sync_mtx); 2356 cv_broadcast(&sync_wakeup); 2357 kproc_shutdown(arg, howto); 2358 } 2359 2360 void 2361 syncer_suspend(void) 2362 { 2363 2364 syncer_shutdown(updateproc, 0); 2365 } 2366 2367 void 2368 syncer_resume(void) 2369 { 2370 2371 mtx_lock(&sync_mtx); 2372 first_printf = 1; 2373 syncer_state = SYNCER_RUNNING; 2374 mtx_unlock(&sync_mtx); 2375 cv_broadcast(&sync_wakeup); 2376 kproc_resume(updateproc); 2377 } 2378 2379 /* 2380 * Reassign a buffer from one vnode to another. 2381 * Used to assign file specific control information 2382 * (indirect blocks) to the vnode to which they belong. 2383 */ 2384 void 2385 reassignbuf(struct buf *bp) 2386 { 2387 struct vnode *vp; 2388 struct bufobj *bo; 2389 int delay; 2390 #ifdef INVARIANTS 2391 struct bufv *bv; 2392 #endif 2393 2394 vp = bp->b_vp; 2395 bo = bp->b_bufobj; 2396 ++reassignbufcalls; 2397 2398 CTR3(KTR_BUF, "reassignbuf(%p) vp %p flags %X", 2399 bp, bp->b_vp, bp->b_flags); 2400 /* 2401 * B_PAGING flagged buffers cannot be reassigned because their vp 2402 * is not fully linked in. 2403 */ 2404 if (bp->b_flags & B_PAGING) 2405 panic("cannot reassign paging buffer"); 2406 2407 /* 2408 * Delete from old vnode list, if on one. 2409 */ 2410 BO_LOCK(bo); 2411 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) 2412 buf_vlist_remove(bp); 2413 else 2414 panic("reassignbuf: Buffer %p not on queue.", bp); 2415 /* 2416 * If dirty, put on list of dirty buffers; otherwise insert onto list 2417 * of clean buffers. 2418 */ 2419 if (bp->b_flags & B_DELWRI) { 2420 if ((bo->bo_flag & BO_ONWORKLST) == 0) { 2421 switch (vp->v_type) { 2422 case VDIR: 2423 delay = dirdelay; 2424 break; 2425 case VCHR: 2426 delay = metadelay; 2427 break; 2428 default: 2429 delay = filedelay; 2430 } 2431 vn_syncer_add_to_worklist(bo, delay); 2432 } 2433 buf_vlist_add(bp, bo, BX_VNDIRTY); 2434 } else { 2435 buf_vlist_add(bp, bo, BX_VNCLEAN); 2436 2437 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) { 2438 mtx_lock(&sync_mtx); 2439 LIST_REMOVE(bo, bo_synclist); 2440 syncer_worklist_len--; 2441 mtx_unlock(&sync_mtx); 2442 bo->bo_flag &= ~BO_ONWORKLST; 2443 } 2444 } 2445 #ifdef INVARIANTS 2446 bv = &bo->bo_clean; 2447 bp = TAILQ_FIRST(&bv->bv_hd); 2448 KASSERT(bp == NULL || bp->b_bufobj == bo, 2449 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo)); 2450 bp = TAILQ_LAST(&bv->bv_hd, buflists); 2451 KASSERT(bp == NULL || bp->b_bufobj == bo, 2452 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo)); 2453 bv = &bo->bo_dirty; 2454 bp = TAILQ_FIRST(&bv->bv_hd); 2455 KASSERT(bp == NULL || bp->b_bufobj == bo, 2456 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo)); 2457 bp = TAILQ_LAST(&bv->bv_hd, buflists); 2458 KASSERT(bp == NULL || bp->b_bufobj == bo, 2459 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo)); 2460 #endif 2461 BO_UNLOCK(bo); 2462 } 2463 2464 /* 2465 * A temporary hack until refcount_* APIs are sorted out. 2466 */ 2467 static __inline int 2468 vfs_refcount_acquire_if_not_zero(volatile u_int *count) 2469 { 2470 u_int old; 2471 2472 old = *count; 2473 for (;;) { 2474 if (old == 0) 2475 return (0); 2476 if (atomic_fcmpset_int(count, &old, old + 1)) 2477 return (1); 2478 } 2479 } 2480 2481 static __inline int 2482 vfs_refcount_release_if_not_last(volatile u_int *count) 2483 { 2484 u_int old; 2485 2486 old = *count; 2487 for (;;) { 2488 if (old == 1) 2489 return (0); 2490 if (atomic_fcmpset_int(count, &old, old - 1)) 2491 return (1); 2492 } 2493 } 2494 2495 static void 2496 v_init_counters(struct vnode *vp) 2497 { 2498 2499 VNASSERT(vp->v_type == VNON && vp->v_data == NULL && vp->v_iflag == 0, 2500 vp, ("%s called for an initialized vnode", __FUNCTION__)); 2501 ASSERT_VI_UNLOCKED(vp, __FUNCTION__); 2502 2503 refcount_init(&vp->v_holdcnt, 1); 2504 refcount_init(&vp->v_usecount, 1); 2505 } 2506 2507 static void 2508 v_incr_usecount_locked(struct vnode *vp) 2509 { 2510 2511 ASSERT_VI_LOCKED(vp, __func__); 2512 if ((vp->v_iflag & VI_OWEINACT) != 0) { 2513 VNASSERT(vp->v_usecount == 0, vp, 2514 ("vnode with usecount and VI_OWEINACT set")); 2515 vp->v_iflag &= ~VI_OWEINACT; 2516 } 2517 refcount_acquire(&vp->v_usecount); 2518 v_incr_devcount(vp); 2519 } 2520 2521 /* 2522 * Increment the use count on the vnode, taking care to reference 2523 * the driver's usecount if this is a chardev. 2524 */ 2525 static void 2526 v_incr_usecount(struct vnode *vp) 2527 { 2528 2529 ASSERT_VI_UNLOCKED(vp, __func__); 2530 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 2531 2532 if (vp->v_type != VCHR && 2533 vfs_refcount_acquire_if_not_zero(&vp->v_usecount)) { 2534 VNASSERT((vp->v_iflag & VI_OWEINACT) == 0, vp, 2535 ("vnode with usecount and VI_OWEINACT set")); 2536 } else { 2537 VI_LOCK(vp); 2538 v_incr_usecount_locked(vp); 2539 VI_UNLOCK(vp); 2540 } 2541 } 2542 2543 /* 2544 * Increment si_usecount of the associated device, if any. 2545 */ 2546 static void 2547 v_incr_devcount(struct vnode *vp) 2548 { 2549 2550 ASSERT_VI_LOCKED(vp, __FUNCTION__); 2551 if (vp->v_type == VCHR && vp->v_rdev != NULL) { 2552 dev_lock(); 2553 vp->v_rdev->si_usecount++; 2554 dev_unlock(); 2555 } 2556 } 2557 2558 /* 2559 * Decrement si_usecount of the associated device, if any. 2560 */ 2561 static void 2562 v_decr_devcount(struct vnode *vp) 2563 { 2564 2565 ASSERT_VI_LOCKED(vp, __FUNCTION__); 2566 if (vp->v_type == VCHR && vp->v_rdev != NULL) { 2567 dev_lock(); 2568 vp->v_rdev->si_usecount--; 2569 dev_unlock(); 2570 } 2571 } 2572 2573 /* 2574 * Grab a particular vnode from the free list, increment its 2575 * reference count and lock it. VI_DOOMED is set if the vnode 2576 * is being destroyed. Only callers who specify LK_RETRY will 2577 * see doomed vnodes. If inactive processing was delayed in 2578 * vput try to do it here. 2579 * 2580 * Notes on lockless counter manipulation: 2581 * _vhold, vputx and other routines make various decisions based 2582 * on either holdcnt or usecount being 0. As long as either counter 2583 * is not transitioning 0->1 nor 1->0, the manipulation can be done 2584 * with atomic operations. Otherwise the interlock is taken covering 2585 * both the atomic and additional actions. 2586 */ 2587 int 2588 vget(struct vnode *vp, int flags, struct thread *td) 2589 { 2590 int error, oweinact; 2591 2592 VNASSERT((flags & LK_TYPE_MASK) != 0, vp, 2593 ("vget: invalid lock operation")); 2594 2595 if ((flags & LK_INTERLOCK) != 0) 2596 ASSERT_VI_LOCKED(vp, __func__); 2597 else 2598 ASSERT_VI_UNLOCKED(vp, __func__); 2599 if ((flags & LK_VNHELD) != 0) 2600 VNASSERT((vp->v_holdcnt > 0), vp, 2601 ("vget: LK_VNHELD passed but vnode not held")); 2602 2603 CTR3(KTR_VFS, "%s: vp %p with flags %d", __func__, vp, flags); 2604 2605 if ((flags & LK_VNHELD) == 0) 2606 _vhold(vp, (flags & LK_INTERLOCK) != 0); 2607 2608 if ((error = vn_lock(vp, flags)) != 0) { 2609 vdrop(vp); 2610 CTR2(KTR_VFS, "%s: impossible to lock vnode %p", __func__, 2611 vp); 2612 return (error); 2613 } 2614 if (vp->v_iflag & VI_DOOMED && (flags & LK_RETRY) == 0) 2615 panic("vget: vn_lock failed to return ENOENT\n"); 2616 /* 2617 * We don't guarantee that any particular close will 2618 * trigger inactive processing so just make a best effort 2619 * here at preventing a reference to a removed file. If 2620 * we don't succeed no harm is done. 2621 * 2622 * Upgrade our holdcnt to a usecount. 2623 */ 2624 if (vp->v_type == VCHR || 2625 !vfs_refcount_acquire_if_not_zero(&vp->v_usecount)) { 2626 VI_LOCK(vp); 2627 if ((vp->v_iflag & VI_OWEINACT) == 0) { 2628 oweinact = 0; 2629 } else { 2630 oweinact = 1; 2631 vp->v_iflag &= ~VI_OWEINACT; 2632 } 2633 refcount_acquire(&vp->v_usecount); 2634 v_incr_devcount(vp); 2635 if (oweinact && VOP_ISLOCKED(vp) == LK_EXCLUSIVE && 2636 (flags & LK_NOWAIT) == 0) 2637 vinactive(vp, td); 2638 VI_UNLOCK(vp); 2639 } 2640 return (0); 2641 } 2642 2643 /* 2644 * Increase the reference (use) and hold count of a vnode. 2645 * This will also remove the vnode from the free list if it is presently free. 2646 */ 2647 void 2648 vref(struct vnode *vp) 2649 { 2650 2651 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 2652 _vhold(vp, false); 2653 v_incr_usecount(vp); 2654 } 2655 2656 void 2657 vrefl(struct vnode *vp) 2658 { 2659 2660 ASSERT_VI_LOCKED(vp, __func__); 2661 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 2662 _vhold(vp, true); 2663 v_incr_usecount_locked(vp); 2664 } 2665 2666 void 2667 vrefact(struct vnode *vp) 2668 { 2669 2670 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 2671 if (__predict_false(vp->v_type == VCHR)) { 2672 VNASSERT(vp->v_holdcnt > 0 && vp->v_usecount > 0, vp, 2673 ("%s: wrong ref counts", __func__)); 2674 vref(vp); 2675 return; 2676 } 2677 #ifdef INVARIANTS 2678 int old = atomic_fetchadd_int(&vp->v_holdcnt, 1); 2679 VNASSERT(old > 0, vp, ("%s: wrong hold count", __func__)); 2680 old = atomic_fetchadd_int(&vp->v_usecount, 1); 2681 VNASSERT(old > 0, vp, ("%s: wrong use count", __func__)); 2682 #else 2683 refcount_acquire(&vp->v_holdcnt); 2684 refcount_acquire(&vp->v_usecount); 2685 #endif 2686 } 2687 2688 /* 2689 * Return reference count of a vnode. 2690 * 2691 * The results of this call are only guaranteed when some mechanism is used to 2692 * stop other processes from gaining references to the vnode. This may be the 2693 * case if the caller holds the only reference. This is also useful when stale 2694 * data is acceptable as race conditions may be accounted for by some other 2695 * means. 2696 */ 2697 int 2698 vrefcnt(struct vnode *vp) 2699 { 2700 2701 return (vp->v_usecount); 2702 } 2703 2704 #define VPUTX_VRELE 1 2705 #define VPUTX_VPUT 2 2706 #define VPUTX_VUNREF 3 2707 2708 /* 2709 * Decrement the use and hold counts for a vnode. 2710 * 2711 * See an explanation near vget() as to why atomic operation is safe. 2712 */ 2713 static void 2714 vputx(struct vnode *vp, int func) 2715 { 2716 int error; 2717 2718 KASSERT(vp != NULL, ("vputx: null vp")); 2719 if (func == VPUTX_VUNREF) 2720 ASSERT_VOP_LOCKED(vp, "vunref"); 2721 else if (func == VPUTX_VPUT) 2722 ASSERT_VOP_LOCKED(vp, "vput"); 2723 else 2724 KASSERT(func == VPUTX_VRELE, ("vputx: wrong func")); 2725 ASSERT_VI_UNLOCKED(vp, __func__); 2726 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 2727 2728 if (vp->v_type != VCHR && 2729 vfs_refcount_release_if_not_last(&vp->v_usecount)) { 2730 if (func == VPUTX_VPUT) 2731 VOP_UNLOCK(vp, 0); 2732 vdrop(vp); 2733 return; 2734 } 2735 2736 VI_LOCK(vp); 2737 2738 /* 2739 * We want to hold the vnode until the inactive finishes to 2740 * prevent vgone() races. We drop the use count here and the 2741 * hold count below when we're done. 2742 */ 2743 if (!refcount_release(&vp->v_usecount) || 2744 (vp->v_iflag & VI_DOINGINACT)) { 2745 if (func == VPUTX_VPUT) 2746 VOP_UNLOCK(vp, 0); 2747 v_decr_devcount(vp); 2748 vdropl(vp); 2749 return; 2750 } 2751 2752 v_decr_devcount(vp); 2753 2754 error = 0; 2755 2756 if (vp->v_usecount != 0) { 2757 vn_printf(vp, "vputx: usecount not zero for vnode "); 2758 panic("vputx: usecount not zero"); 2759 } 2760 2761 CTR2(KTR_VFS, "%s: return vnode %p to the freelist", __func__, vp); 2762 2763 /* 2764 * We must call VOP_INACTIVE with the node locked. Mark 2765 * as VI_DOINGINACT to avoid recursion. 2766 */ 2767 vp->v_iflag |= VI_OWEINACT; 2768 switch (func) { 2769 case VPUTX_VRELE: 2770 error = vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK); 2771 VI_LOCK(vp); 2772 break; 2773 case VPUTX_VPUT: 2774 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) { 2775 error = VOP_LOCK(vp, LK_UPGRADE | LK_INTERLOCK | 2776 LK_NOWAIT); 2777 VI_LOCK(vp); 2778 } 2779 break; 2780 case VPUTX_VUNREF: 2781 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) { 2782 error = VOP_LOCK(vp, LK_TRYUPGRADE | LK_INTERLOCK); 2783 VI_LOCK(vp); 2784 } 2785 break; 2786 } 2787 VNASSERT(vp->v_usecount == 0 || (vp->v_iflag & VI_OWEINACT) == 0, vp, 2788 ("vnode with usecount and VI_OWEINACT set")); 2789 if (error == 0) { 2790 if (vp->v_iflag & VI_OWEINACT) 2791 vinactive(vp, curthread); 2792 if (func != VPUTX_VUNREF) 2793 VOP_UNLOCK(vp, 0); 2794 } 2795 vdropl(vp); 2796 } 2797 2798 /* 2799 * Vnode put/release. 2800 * If count drops to zero, call inactive routine and return to freelist. 2801 */ 2802 void 2803 vrele(struct vnode *vp) 2804 { 2805 2806 vputx(vp, VPUTX_VRELE); 2807 } 2808 2809 /* 2810 * Release an already locked vnode. This give the same effects as 2811 * unlock+vrele(), but takes less time and avoids releasing and 2812 * re-aquiring the lock (as vrele() acquires the lock internally.) 2813 */ 2814 void 2815 vput(struct vnode *vp) 2816 { 2817 2818 vputx(vp, VPUTX_VPUT); 2819 } 2820 2821 /* 2822 * Release an exclusively locked vnode. Do not unlock the vnode lock. 2823 */ 2824 void 2825 vunref(struct vnode *vp) 2826 { 2827 2828 vputx(vp, VPUTX_VUNREF); 2829 } 2830 2831 /* 2832 * Increase the hold count and activate if this is the first reference. 2833 */ 2834 void 2835 _vhold(struct vnode *vp, bool locked) 2836 { 2837 struct mount *mp; 2838 2839 if (locked) 2840 ASSERT_VI_LOCKED(vp, __func__); 2841 else 2842 ASSERT_VI_UNLOCKED(vp, __func__); 2843 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 2844 if (!locked && vfs_refcount_acquire_if_not_zero(&vp->v_holdcnt)) { 2845 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp, 2846 ("_vhold: vnode with holdcnt is free")); 2847 return; 2848 } 2849 2850 if (!locked) 2851 VI_LOCK(vp); 2852 if ((vp->v_iflag & VI_FREE) == 0) { 2853 refcount_acquire(&vp->v_holdcnt); 2854 if (!locked) 2855 VI_UNLOCK(vp); 2856 return; 2857 } 2858 VNASSERT(vp->v_holdcnt == 0, vp, 2859 ("%s: wrong hold count", __func__)); 2860 VNASSERT(vp->v_op != NULL, vp, 2861 ("%s: vnode already reclaimed.", __func__)); 2862 /* 2863 * Remove a vnode from the free list, mark it as in use, 2864 * and put it on the active list. 2865 */ 2866 VNASSERT(vp->v_mount != NULL, vp, 2867 ("_vhold: vnode not on per mount vnode list")); 2868 mp = vp->v_mount; 2869 mtx_lock(&mp->mnt_listmtx); 2870 if ((vp->v_mflag & VMP_TMPMNTFREELIST) != 0) { 2871 TAILQ_REMOVE(&mp->mnt_tmpfreevnodelist, vp, v_actfreelist); 2872 mp->mnt_tmpfreevnodelistsize--; 2873 vp->v_mflag &= ~VMP_TMPMNTFREELIST; 2874 } else { 2875 mtx_lock(&vnode_free_list_mtx); 2876 TAILQ_REMOVE(&vnode_free_list, vp, v_actfreelist); 2877 freevnodes--; 2878 mtx_unlock(&vnode_free_list_mtx); 2879 } 2880 KASSERT((vp->v_iflag & VI_ACTIVE) == 0, 2881 ("Activating already active vnode")); 2882 vp->v_iflag &= ~VI_FREE; 2883 vp->v_iflag |= VI_ACTIVE; 2884 TAILQ_INSERT_HEAD(&mp->mnt_activevnodelist, vp, v_actfreelist); 2885 mp->mnt_activevnodelistsize++; 2886 mtx_unlock(&mp->mnt_listmtx); 2887 refcount_acquire(&vp->v_holdcnt); 2888 if (!locked) 2889 VI_UNLOCK(vp); 2890 } 2891 2892 /* 2893 * Drop the hold count of the vnode. If this is the last reference to 2894 * the vnode we place it on the free list unless it has been vgone'd 2895 * (marked VI_DOOMED) in which case we will free it. 2896 * 2897 * Because the vnode vm object keeps a hold reference on the vnode if 2898 * there is at least one resident non-cached page, the vnode cannot 2899 * leave the active list without the page cleanup done. 2900 */ 2901 void 2902 _vdrop(struct vnode *vp, bool locked) 2903 { 2904 struct bufobj *bo; 2905 struct mount *mp; 2906 int active; 2907 2908 if (locked) 2909 ASSERT_VI_LOCKED(vp, __func__); 2910 else 2911 ASSERT_VI_UNLOCKED(vp, __func__); 2912 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 2913 if ((int)vp->v_holdcnt <= 0) 2914 panic("vdrop: holdcnt %d", vp->v_holdcnt); 2915 if (vfs_refcount_release_if_not_last(&vp->v_holdcnt)) { 2916 if (locked) 2917 VI_UNLOCK(vp); 2918 return; 2919 } 2920 2921 if (!locked) 2922 VI_LOCK(vp); 2923 if (refcount_release(&vp->v_holdcnt) == 0) { 2924 VI_UNLOCK(vp); 2925 return; 2926 } 2927 if ((vp->v_iflag & VI_DOOMED) == 0) { 2928 /* 2929 * Mark a vnode as free: remove it from its active list 2930 * and put it up for recycling on the freelist. 2931 */ 2932 VNASSERT(vp->v_op != NULL, vp, 2933 ("vdropl: vnode already reclaimed.")); 2934 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp, 2935 ("vnode already free")); 2936 VNASSERT(vp->v_holdcnt == 0, vp, 2937 ("vdropl: freeing when we shouldn't")); 2938 active = vp->v_iflag & VI_ACTIVE; 2939 if ((vp->v_iflag & VI_OWEINACT) == 0) { 2940 vp->v_iflag &= ~VI_ACTIVE; 2941 mp = vp->v_mount; 2942 if (mp != NULL) { 2943 mtx_lock(&mp->mnt_listmtx); 2944 if (active) { 2945 TAILQ_REMOVE(&mp->mnt_activevnodelist, 2946 vp, v_actfreelist); 2947 mp->mnt_activevnodelistsize--; 2948 } 2949 TAILQ_INSERT_TAIL(&mp->mnt_tmpfreevnodelist, 2950 vp, v_actfreelist); 2951 mp->mnt_tmpfreevnodelistsize++; 2952 vp->v_iflag |= VI_FREE; 2953 vp->v_mflag |= VMP_TMPMNTFREELIST; 2954 VI_UNLOCK(vp); 2955 if (mp->mnt_tmpfreevnodelistsize >= 2956 mnt_free_list_batch) 2957 vnlru_return_batch_locked(mp); 2958 mtx_unlock(&mp->mnt_listmtx); 2959 } else { 2960 VNASSERT(active == 0, vp, 2961 ("vdropl: active vnode not on per mount " 2962 "vnode list")); 2963 mtx_lock(&vnode_free_list_mtx); 2964 TAILQ_INSERT_TAIL(&vnode_free_list, vp, 2965 v_actfreelist); 2966 freevnodes++; 2967 vp->v_iflag |= VI_FREE; 2968 VI_UNLOCK(vp); 2969 mtx_unlock(&vnode_free_list_mtx); 2970 } 2971 } else { 2972 VI_UNLOCK(vp); 2973 counter_u64_add(free_owe_inact, 1); 2974 } 2975 return; 2976 } 2977 /* 2978 * The vnode has been marked for destruction, so free it. 2979 * 2980 * The vnode will be returned to the zone where it will 2981 * normally remain until it is needed for another vnode. We 2982 * need to cleanup (or verify that the cleanup has already 2983 * been done) any residual data left from its current use 2984 * so as not to contaminate the freshly allocated vnode. 2985 */ 2986 CTR2(KTR_VFS, "%s: destroying the vnode %p", __func__, vp); 2987 atomic_subtract_long(&numvnodes, 1); 2988 bo = &vp->v_bufobj; 2989 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp, 2990 ("cleaned vnode still on the free list.")); 2991 VNASSERT(vp->v_data == NULL, vp, ("cleaned vnode isn't")); 2992 VNASSERT(vp->v_holdcnt == 0, vp, ("Non-zero hold count")); 2993 VNASSERT(vp->v_usecount == 0, vp, ("Non-zero use count")); 2994 VNASSERT(vp->v_writecount == 0, vp, ("Non-zero write count")); 2995 VNASSERT(bo->bo_numoutput == 0, vp, ("Clean vnode has pending I/O's")); 2996 VNASSERT(bo->bo_clean.bv_cnt == 0, vp, ("cleanbufcnt not 0")); 2997 VNASSERT(pctrie_is_empty(&bo->bo_clean.bv_root), vp, 2998 ("clean blk trie not empty")); 2999 VNASSERT(bo->bo_dirty.bv_cnt == 0, vp, ("dirtybufcnt not 0")); 3000 VNASSERT(pctrie_is_empty(&bo->bo_dirty.bv_root), vp, 3001 ("dirty blk trie not empty")); 3002 VNASSERT(TAILQ_EMPTY(&vp->v_cache_dst), vp, ("vp has namecache dst")); 3003 VNASSERT(LIST_EMPTY(&vp->v_cache_src), vp, ("vp has namecache src")); 3004 VNASSERT(vp->v_cache_dd == NULL, vp, ("vp has namecache for ..")); 3005 VNASSERT(TAILQ_EMPTY(&vp->v_rl.rl_waiters), vp, 3006 ("Dangling rangelock waiters")); 3007 VI_UNLOCK(vp); 3008 #ifdef MAC 3009 mac_vnode_destroy(vp); 3010 #endif 3011 if (vp->v_pollinfo != NULL) { 3012 destroy_vpollinfo(vp->v_pollinfo); 3013 vp->v_pollinfo = NULL; 3014 } 3015 #ifdef INVARIANTS 3016 /* XXX Elsewhere we detect an already freed vnode via NULL v_op. */ 3017 vp->v_op = NULL; 3018 #endif 3019 vp->v_mountedhere = NULL; 3020 vp->v_unpcb = NULL; 3021 vp->v_rdev = NULL; 3022 vp->v_fifoinfo = NULL; 3023 vp->v_lasta = vp->v_clen = vp->v_cstart = vp->v_lastw = 0; 3024 vp->v_iflag = 0; 3025 vp->v_vflag = 0; 3026 bo->bo_flag = 0; 3027 uma_zfree(vnode_zone, vp); 3028 } 3029 3030 /* 3031 * Call VOP_INACTIVE on the vnode and manage the DOINGINACT and OWEINACT 3032 * flags. DOINGINACT prevents us from recursing in calls to vinactive. 3033 * OWEINACT tracks whether a vnode missed a call to inactive due to a 3034 * failed lock upgrade. 3035 */ 3036 void 3037 vinactive(struct vnode *vp, struct thread *td) 3038 { 3039 struct vm_object *obj; 3040 3041 ASSERT_VOP_ELOCKED(vp, "vinactive"); 3042 ASSERT_VI_LOCKED(vp, "vinactive"); 3043 VNASSERT((vp->v_iflag & VI_DOINGINACT) == 0, vp, 3044 ("vinactive: recursed on VI_DOINGINACT")); 3045 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 3046 vp->v_iflag |= VI_DOINGINACT; 3047 vp->v_iflag &= ~VI_OWEINACT; 3048 VI_UNLOCK(vp); 3049 /* 3050 * Before moving off the active list, we must be sure that any 3051 * modified pages are converted into the vnode's dirty 3052 * buffers, since these will no longer be checked once the 3053 * vnode is on the inactive list. 3054 * 3055 * The write-out of the dirty pages is asynchronous. At the 3056 * point that VOP_INACTIVE() is called, there could still be 3057 * pending I/O and dirty pages in the object. 3058 */ 3059 if ((obj = vp->v_object) != NULL && (vp->v_vflag & VV_NOSYNC) == 0 && 3060 (obj->flags & OBJ_MIGHTBEDIRTY) != 0) { 3061 VM_OBJECT_WLOCK(obj); 3062 vm_object_page_clean(obj, 0, 0, 0); 3063 VM_OBJECT_WUNLOCK(obj); 3064 } 3065 VOP_INACTIVE(vp, td); 3066 VI_LOCK(vp); 3067 VNASSERT(vp->v_iflag & VI_DOINGINACT, vp, 3068 ("vinactive: lost VI_DOINGINACT")); 3069 vp->v_iflag &= ~VI_DOINGINACT; 3070 } 3071 3072 /* 3073 * Remove any vnodes in the vnode table belonging to mount point mp. 3074 * 3075 * If FORCECLOSE is not specified, there should not be any active ones, 3076 * return error if any are found (nb: this is a user error, not a 3077 * system error). If FORCECLOSE is specified, detach any active vnodes 3078 * that are found. 3079 * 3080 * If WRITECLOSE is set, only flush out regular file vnodes open for 3081 * writing. 3082 * 3083 * SKIPSYSTEM causes any vnodes marked VV_SYSTEM to be skipped. 3084 * 3085 * `rootrefs' specifies the base reference count for the root vnode 3086 * of this filesystem. The root vnode is considered busy if its 3087 * v_usecount exceeds this value. On a successful return, vflush(, td) 3088 * will call vrele() on the root vnode exactly rootrefs times. 3089 * If the SKIPSYSTEM or WRITECLOSE flags are specified, rootrefs must 3090 * be zero. 3091 */ 3092 #ifdef DIAGNOSTIC 3093 static int busyprt = 0; /* print out busy vnodes */ 3094 SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, "Print out busy vnodes"); 3095 #endif 3096 3097 int 3098 vflush(struct mount *mp, int rootrefs, int flags, struct thread *td) 3099 { 3100 struct vnode *vp, *mvp, *rootvp = NULL; 3101 struct vattr vattr; 3102 int busy = 0, error; 3103 3104 CTR4(KTR_VFS, "%s: mp %p with rootrefs %d and flags %d", __func__, mp, 3105 rootrefs, flags); 3106 if (rootrefs > 0) { 3107 KASSERT((flags & (SKIPSYSTEM | WRITECLOSE)) == 0, 3108 ("vflush: bad args")); 3109 /* 3110 * Get the filesystem root vnode. We can vput() it 3111 * immediately, since with rootrefs > 0, it won't go away. 3112 */ 3113 if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rootvp)) != 0) { 3114 CTR2(KTR_VFS, "%s: vfs_root lookup failed with %d", 3115 __func__, error); 3116 return (error); 3117 } 3118 vput(rootvp); 3119 } 3120 loop: 3121 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) { 3122 vholdl(vp); 3123 error = vn_lock(vp, LK_INTERLOCK | LK_EXCLUSIVE); 3124 if (error) { 3125 vdrop(vp); 3126 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp); 3127 goto loop; 3128 } 3129 /* 3130 * Skip over a vnodes marked VV_SYSTEM. 3131 */ 3132 if ((flags & SKIPSYSTEM) && (vp->v_vflag & VV_SYSTEM)) { 3133 VOP_UNLOCK(vp, 0); 3134 vdrop(vp); 3135 continue; 3136 } 3137 /* 3138 * If WRITECLOSE is set, flush out unlinked but still open 3139 * files (even if open only for reading) and regular file 3140 * vnodes open for writing. 3141 */ 3142 if (flags & WRITECLOSE) { 3143 if (vp->v_object != NULL) { 3144 VM_OBJECT_WLOCK(vp->v_object); 3145 vm_object_page_clean(vp->v_object, 0, 0, 0); 3146 VM_OBJECT_WUNLOCK(vp->v_object); 3147 } 3148 error = VOP_FSYNC(vp, MNT_WAIT, td); 3149 if (error != 0) { 3150 VOP_UNLOCK(vp, 0); 3151 vdrop(vp); 3152 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp); 3153 return (error); 3154 } 3155 error = VOP_GETATTR(vp, &vattr, td->td_ucred); 3156 VI_LOCK(vp); 3157 3158 if ((vp->v_type == VNON || 3159 (error == 0 && vattr.va_nlink > 0)) && 3160 (vp->v_writecount == 0 || vp->v_type != VREG)) { 3161 VOP_UNLOCK(vp, 0); 3162 vdropl(vp); 3163 continue; 3164 } 3165 } else 3166 VI_LOCK(vp); 3167 /* 3168 * With v_usecount == 0, all we need to do is clear out the 3169 * vnode data structures and we are done. 3170 * 3171 * If FORCECLOSE is set, forcibly close the vnode. 3172 */ 3173 if (vp->v_usecount == 0 || (flags & FORCECLOSE)) { 3174 vgonel(vp); 3175 } else { 3176 busy++; 3177 #ifdef DIAGNOSTIC 3178 if (busyprt) 3179 vn_printf(vp, "vflush: busy vnode "); 3180 #endif 3181 } 3182 VOP_UNLOCK(vp, 0); 3183 vdropl(vp); 3184 } 3185 if (rootrefs > 0 && (flags & FORCECLOSE) == 0) { 3186 /* 3187 * If just the root vnode is busy, and if its refcount 3188 * is equal to `rootrefs', then go ahead and kill it. 3189 */ 3190 VI_LOCK(rootvp); 3191 KASSERT(busy > 0, ("vflush: not busy")); 3192 VNASSERT(rootvp->v_usecount >= rootrefs, rootvp, 3193 ("vflush: usecount %d < rootrefs %d", 3194 rootvp->v_usecount, rootrefs)); 3195 if (busy == 1 && rootvp->v_usecount == rootrefs) { 3196 VOP_LOCK(rootvp, LK_EXCLUSIVE|LK_INTERLOCK); 3197 vgone(rootvp); 3198 VOP_UNLOCK(rootvp, 0); 3199 busy = 0; 3200 } else 3201 VI_UNLOCK(rootvp); 3202 } 3203 if (busy) { 3204 CTR2(KTR_VFS, "%s: failing as %d vnodes are busy", __func__, 3205 busy); 3206 return (EBUSY); 3207 } 3208 for (; rootrefs > 0; rootrefs--) 3209 vrele(rootvp); 3210 return (0); 3211 } 3212 3213 /* 3214 * Recycle an unused vnode to the front of the free list. 3215 */ 3216 int 3217 vrecycle(struct vnode *vp) 3218 { 3219 int recycled; 3220 3221 VI_LOCK(vp); 3222 recycled = vrecyclel(vp); 3223 VI_UNLOCK(vp); 3224 return (recycled); 3225 } 3226 3227 /* 3228 * vrecycle, with the vp interlock held. 3229 */ 3230 int 3231 vrecyclel(struct vnode *vp) 3232 { 3233 int recycled; 3234 3235 ASSERT_VOP_ELOCKED(vp, __func__); 3236 ASSERT_VI_LOCKED(vp, __func__); 3237 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 3238 recycled = 0; 3239 if (vp->v_usecount == 0) { 3240 recycled = 1; 3241 vgonel(vp); 3242 } 3243 return (recycled); 3244 } 3245 3246 /* 3247 * Eliminate all activity associated with a vnode 3248 * in preparation for reuse. 3249 */ 3250 void 3251 vgone(struct vnode *vp) 3252 { 3253 VI_LOCK(vp); 3254 vgonel(vp); 3255 VI_UNLOCK(vp); 3256 } 3257 3258 static void 3259 notify_lowervp_vfs_dummy(struct mount *mp __unused, 3260 struct vnode *lowervp __unused) 3261 { 3262 } 3263 3264 /* 3265 * Notify upper mounts about reclaimed or unlinked vnode. 3266 */ 3267 void 3268 vfs_notify_upper(struct vnode *vp, int event) 3269 { 3270 static struct vfsops vgonel_vfsops = { 3271 .vfs_reclaim_lowervp = notify_lowervp_vfs_dummy, 3272 .vfs_unlink_lowervp = notify_lowervp_vfs_dummy, 3273 }; 3274 struct mount *mp, *ump, *mmp; 3275 3276 mp = vp->v_mount; 3277 if (mp == NULL) 3278 return; 3279 3280 MNT_ILOCK(mp); 3281 if (TAILQ_EMPTY(&mp->mnt_uppers)) 3282 goto unlock; 3283 MNT_IUNLOCK(mp); 3284 mmp = malloc(sizeof(struct mount), M_TEMP, M_WAITOK | M_ZERO); 3285 mmp->mnt_op = &vgonel_vfsops; 3286 mmp->mnt_kern_flag |= MNTK_MARKER; 3287 MNT_ILOCK(mp); 3288 mp->mnt_kern_flag |= MNTK_VGONE_UPPER; 3289 for (ump = TAILQ_FIRST(&mp->mnt_uppers); ump != NULL;) { 3290 if ((ump->mnt_kern_flag & MNTK_MARKER) != 0) { 3291 ump = TAILQ_NEXT(ump, mnt_upper_link); 3292 continue; 3293 } 3294 TAILQ_INSERT_AFTER(&mp->mnt_uppers, ump, mmp, mnt_upper_link); 3295 MNT_IUNLOCK(mp); 3296 switch (event) { 3297 case VFS_NOTIFY_UPPER_RECLAIM: 3298 VFS_RECLAIM_LOWERVP(ump, vp); 3299 break; 3300 case VFS_NOTIFY_UPPER_UNLINK: 3301 VFS_UNLINK_LOWERVP(ump, vp); 3302 break; 3303 default: 3304 KASSERT(0, ("invalid event %d", event)); 3305 break; 3306 } 3307 MNT_ILOCK(mp); 3308 ump = TAILQ_NEXT(mmp, mnt_upper_link); 3309 TAILQ_REMOVE(&mp->mnt_uppers, mmp, mnt_upper_link); 3310 } 3311 free(mmp, M_TEMP); 3312 mp->mnt_kern_flag &= ~MNTK_VGONE_UPPER; 3313 if ((mp->mnt_kern_flag & MNTK_VGONE_WAITER) != 0) { 3314 mp->mnt_kern_flag &= ~MNTK_VGONE_WAITER; 3315 wakeup(&mp->mnt_uppers); 3316 } 3317 unlock: 3318 MNT_IUNLOCK(mp); 3319 } 3320 3321 /* 3322 * vgone, with the vp interlock held. 3323 */ 3324 static void 3325 vgonel(struct vnode *vp) 3326 { 3327 struct thread *td; 3328 int oweinact; 3329 int active; 3330 struct mount *mp; 3331 3332 ASSERT_VOP_ELOCKED(vp, "vgonel"); 3333 ASSERT_VI_LOCKED(vp, "vgonel"); 3334 VNASSERT(vp->v_holdcnt, vp, 3335 ("vgonel: vp %p has no reference.", vp)); 3336 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 3337 td = curthread; 3338 3339 /* 3340 * Don't vgonel if we're already doomed. 3341 */ 3342 if (vp->v_iflag & VI_DOOMED) 3343 return; 3344 vp->v_iflag |= VI_DOOMED; 3345 3346 /* 3347 * Check to see if the vnode is in use. If so, we have to call 3348 * VOP_CLOSE() and VOP_INACTIVE(). 3349 */ 3350 active = vp->v_usecount; 3351 oweinact = (vp->v_iflag & VI_OWEINACT); 3352 VI_UNLOCK(vp); 3353 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_RECLAIM); 3354 3355 /* 3356 * If purging an active vnode, it must be closed and 3357 * deactivated before being reclaimed. 3358 */ 3359 if (active) 3360 VOP_CLOSE(vp, FNONBLOCK, NOCRED, td); 3361 if (oweinact || active) { 3362 VI_LOCK(vp); 3363 if ((vp->v_iflag & VI_DOINGINACT) == 0) 3364 vinactive(vp, td); 3365 VI_UNLOCK(vp); 3366 } 3367 if (vp->v_type == VSOCK) 3368 vfs_unp_reclaim(vp); 3369 3370 /* 3371 * Clean out any buffers associated with the vnode. 3372 * If the flush fails, just toss the buffers. 3373 */ 3374 mp = NULL; 3375 if (!TAILQ_EMPTY(&vp->v_bufobj.bo_dirty.bv_hd)) 3376 (void) vn_start_secondary_write(vp, &mp, V_WAIT); 3377 if (vinvalbuf(vp, V_SAVE, 0, 0) != 0) { 3378 while (vinvalbuf(vp, 0, 0, 0) != 0) 3379 ; 3380 } 3381 3382 BO_LOCK(&vp->v_bufobj); 3383 KASSERT(TAILQ_EMPTY(&vp->v_bufobj.bo_dirty.bv_hd) && 3384 vp->v_bufobj.bo_dirty.bv_cnt == 0 && 3385 TAILQ_EMPTY(&vp->v_bufobj.bo_clean.bv_hd) && 3386 vp->v_bufobj.bo_clean.bv_cnt == 0, 3387 ("vp %p bufobj not invalidated", vp)); 3388 3389 /* 3390 * For VMIO bufobj, BO_DEAD is set in vm_object_terminate() 3391 * after the object's page queue is flushed. 3392 */ 3393 if (vp->v_bufobj.bo_object == NULL) 3394 vp->v_bufobj.bo_flag |= BO_DEAD; 3395 BO_UNLOCK(&vp->v_bufobj); 3396 3397 /* 3398 * Reclaim the vnode. 3399 */ 3400 if (VOP_RECLAIM(vp, td)) 3401 panic("vgone: cannot reclaim"); 3402 if (mp != NULL) 3403 vn_finished_secondary_write(mp); 3404 VNASSERT(vp->v_object == NULL, vp, 3405 ("vop_reclaim left v_object vp=%p, tag=%s", vp, vp->v_tag)); 3406 /* 3407 * Clear the advisory locks and wake up waiting threads. 3408 */ 3409 (void)VOP_ADVLOCKPURGE(vp); 3410 vp->v_lockf = NULL; 3411 /* 3412 * Delete from old mount point vnode list. 3413 */ 3414 delmntque(vp); 3415 cache_purge(vp); 3416 /* 3417 * Done with purge, reset to the standard lock and invalidate 3418 * the vnode. 3419 */ 3420 VI_LOCK(vp); 3421 vp->v_vnlock = &vp->v_lock; 3422 vp->v_op = &dead_vnodeops; 3423 vp->v_tag = "none"; 3424 vp->v_type = VBAD; 3425 } 3426 3427 /* 3428 * Calculate the total number of references to a special device. 3429 */ 3430 int 3431 vcount(struct vnode *vp) 3432 { 3433 int count; 3434 3435 dev_lock(); 3436 count = vp->v_rdev->si_usecount; 3437 dev_unlock(); 3438 return (count); 3439 } 3440 3441 /* 3442 * Same as above, but using the struct cdev *as argument 3443 */ 3444 int 3445 count_dev(struct cdev *dev) 3446 { 3447 int count; 3448 3449 dev_lock(); 3450 count = dev->si_usecount; 3451 dev_unlock(); 3452 return(count); 3453 } 3454 3455 /* 3456 * Print out a description of a vnode. 3457 */ 3458 static char *typename[] = 3459 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD", 3460 "VMARKER"}; 3461 3462 void 3463 vn_printf(struct vnode *vp, const char *fmt, ...) 3464 { 3465 va_list ap; 3466 char buf[256], buf2[16]; 3467 u_long flags; 3468 3469 va_start(ap, fmt); 3470 vprintf(fmt, ap); 3471 va_end(ap); 3472 printf("%p: ", (void *)vp); 3473 printf("tag %s, type %s\n", vp->v_tag, typename[vp->v_type]); 3474 printf(" usecount %d, writecount %d, refcount %d mountedhere %p\n", 3475 vp->v_usecount, vp->v_writecount, vp->v_holdcnt, vp->v_mountedhere); 3476 buf[0] = '\0'; 3477 buf[1] = '\0'; 3478 if (vp->v_vflag & VV_ROOT) 3479 strlcat(buf, "|VV_ROOT", sizeof(buf)); 3480 if (vp->v_vflag & VV_ISTTY) 3481 strlcat(buf, "|VV_ISTTY", sizeof(buf)); 3482 if (vp->v_vflag & VV_NOSYNC) 3483 strlcat(buf, "|VV_NOSYNC", sizeof(buf)); 3484 if (vp->v_vflag & VV_ETERNALDEV) 3485 strlcat(buf, "|VV_ETERNALDEV", sizeof(buf)); 3486 if (vp->v_vflag & VV_CACHEDLABEL) 3487 strlcat(buf, "|VV_CACHEDLABEL", sizeof(buf)); 3488 if (vp->v_vflag & VV_TEXT) 3489 strlcat(buf, "|VV_TEXT", sizeof(buf)); 3490 if (vp->v_vflag & VV_COPYONWRITE) 3491 strlcat(buf, "|VV_COPYONWRITE", sizeof(buf)); 3492 if (vp->v_vflag & VV_SYSTEM) 3493 strlcat(buf, "|VV_SYSTEM", sizeof(buf)); 3494 if (vp->v_vflag & VV_PROCDEP) 3495 strlcat(buf, "|VV_PROCDEP", sizeof(buf)); 3496 if (vp->v_vflag & VV_NOKNOTE) 3497 strlcat(buf, "|VV_NOKNOTE", sizeof(buf)); 3498 if (vp->v_vflag & VV_DELETED) 3499 strlcat(buf, "|VV_DELETED", sizeof(buf)); 3500 if (vp->v_vflag & VV_MD) 3501 strlcat(buf, "|VV_MD", sizeof(buf)); 3502 if (vp->v_vflag & VV_FORCEINSMQ) 3503 strlcat(buf, "|VV_FORCEINSMQ", sizeof(buf)); 3504 flags = vp->v_vflag & ~(VV_ROOT | VV_ISTTY | VV_NOSYNC | VV_ETERNALDEV | 3505 VV_CACHEDLABEL | VV_TEXT | VV_COPYONWRITE | VV_SYSTEM | VV_PROCDEP | 3506 VV_NOKNOTE | VV_DELETED | VV_MD | VV_FORCEINSMQ); 3507 if (flags != 0) { 3508 snprintf(buf2, sizeof(buf2), "|VV(0x%lx)", flags); 3509 strlcat(buf, buf2, sizeof(buf)); 3510 } 3511 if (vp->v_iflag & VI_MOUNT) 3512 strlcat(buf, "|VI_MOUNT", sizeof(buf)); 3513 if (vp->v_iflag & VI_DOOMED) 3514 strlcat(buf, "|VI_DOOMED", sizeof(buf)); 3515 if (vp->v_iflag & VI_FREE) 3516 strlcat(buf, "|VI_FREE", sizeof(buf)); 3517 if (vp->v_iflag & VI_ACTIVE) 3518 strlcat(buf, "|VI_ACTIVE", sizeof(buf)); 3519 if (vp->v_iflag & VI_DOINGINACT) 3520 strlcat(buf, "|VI_DOINGINACT", sizeof(buf)); 3521 if (vp->v_iflag & VI_OWEINACT) 3522 strlcat(buf, "|VI_OWEINACT", sizeof(buf)); 3523 flags = vp->v_iflag & ~(VI_MOUNT | VI_DOOMED | VI_FREE | 3524 VI_ACTIVE | VI_DOINGINACT | VI_OWEINACT); 3525 if (flags != 0) { 3526 snprintf(buf2, sizeof(buf2), "|VI(0x%lx)", flags); 3527 strlcat(buf, buf2, sizeof(buf)); 3528 } 3529 printf(" flags (%s)\n", buf + 1); 3530 if (mtx_owned(VI_MTX(vp))) 3531 printf(" VI_LOCKed"); 3532 if (vp->v_object != NULL) 3533 printf(" v_object %p ref %d pages %d " 3534 "cleanbuf %d dirtybuf %d\n", 3535 vp->v_object, vp->v_object->ref_count, 3536 vp->v_object->resident_page_count, 3537 vp->v_bufobj.bo_clean.bv_cnt, 3538 vp->v_bufobj.bo_dirty.bv_cnt); 3539 printf(" "); 3540 lockmgr_printinfo(vp->v_vnlock); 3541 if (vp->v_data != NULL) 3542 VOP_PRINT(vp); 3543 } 3544 3545 #ifdef DDB 3546 /* 3547 * List all of the locked vnodes in the system. 3548 * Called when debugging the kernel. 3549 */ 3550 DB_SHOW_COMMAND(lockedvnods, lockedvnodes) 3551 { 3552 struct mount *mp; 3553 struct vnode *vp; 3554 3555 /* 3556 * Note: because this is DDB, we can't obey the locking semantics 3557 * for these structures, which means we could catch an inconsistent 3558 * state and dereference a nasty pointer. Not much to be done 3559 * about that. 3560 */ 3561 db_printf("Locked vnodes\n"); 3562 TAILQ_FOREACH(mp, &mountlist, mnt_list) { 3563 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) { 3564 if (vp->v_type != VMARKER && VOP_ISLOCKED(vp)) 3565 vn_printf(vp, "vnode "); 3566 } 3567 } 3568 } 3569 3570 /* 3571 * Show details about the given vnode. 3572 */ 3573 DB_SHOW_COMMAND(vnode, db_show_vnode) 3574 { 3575 struct vnode *vp; 3576 3577 if (!have_addr) 3578 return; 3579 vp = (struct vnode *)addr; 3580 vn_printf(vp, "vnode "); 3581 } 3582 3583 /* 3584 * Show details about the given mount point. 3585 */ 3586 DB_SHOW_COMMAND(mount, db_show_mount) 3587 { 3588 struct mount *mp; 3589 struct vfsopt *opt; 3590 struct statfs *sp; 3591 struct vnode *vp; 3592 char buf[512]; 3593 uint64_t mflags; 3594 u_int flags; 3595 3596 if (!have_addr) { 3597 /* No address given, print short info about all mount points. */ 3598 TAILQ_FOREACH(mp, &mountlist, mnt_list) { 3599 db_printf("%p %s on %s (%s)\n", mp, 3600 mp->mnt_stat.f_mntfromname, 3601 mp->mnt_stat.f_mntonname, 3602 mp->mnt_stat.f_fstypename); 3603 if (db_pager_quit) 3604 break; 3605 } 3606 db_printf("\nMore info: show mount <addr>\n"); 3607 return; 3608 } 3609 3610 mp = (struct mount *)addr; 3611 db_printf("%p %s on %s (%s)\n", mp, mp->mnt_stat.f_mntfromname, 3612 mp->mnt_stat.f_mntonname, mp->mnt_stat.f_fstypename); 3613 3614 buf[0] = '\0'; 3615 mflags = mp->mnt_flag; 3616 #define MNT_FLAG(flag) do { \ 3617 if (mflags & (flag)) { \ 3618 if (buf[0] != '\0') \ 3619 strlcat(buf, ", ", sizeof(buf)); \ 3620 strlcat(buf, (#flag) + 4, sizeof(buf)); \ 3621 mflags &= ~(flag); \ 3622 } \ 3623 } while (0) 3624 MNT_FLAG(MNT_RDONLY); 3625 MNT_FLAG(MNT_SYNCHRONOUS); 3626 MNT_FLAG(MNT_NOEXEC); 3627 MNT_FLAG(MNT_NOSUID); 3628 MNT_FLAG(MNT_NFS4ACLS); 3629 MNT_FLAG(MNT_UNION); 3630 MNT_FLAG(MNT_ASYNC); 3631 MNT_FLAG(MNT_SUIDDIR); 3632 MNT_FLAG(MNT_SOFTDEP); 3633 MNT_FLAG(MNT_NOSYMFOLLOW); 3634 MNT_FLAG(MNT_GJOURNAL); 3635 MNT_FLAG(MNT_MULTILABEL); 3636 MNT_FLAG(MNT_ACLS); 3637 MNT_FLAG(MNT_NOATIME); 3638 MNT_FLAG(MNT_NOCLUSTERR); 3639 MNT_FLAG(MNT_NOCLUSTERW); 3640 MNT_FLAG(MNT_SUJ); 3641 MNT_FLAG(MNT_EXRDONLY); 3642 MNT_FLAG(MNT_EXPORTED); 3643 MNT_FLAG(MNT_DEFEXPORTED); 3644 MNT_FLAG(MNT_EXPORTANON); 3645 MNT_FLAG(MNT_EXKERB); 3646 MNT_FLAG(MNT_EXPUBLIC); 3647 MNT_FLAG(MNT_LOCAL); 3648 MNT_FLAG(MNT_QUOTA); 3649 MNT_FLAG(MNT_ROOTFS); 3650 MNT_FLAG(MNT_USER); 3651 MNT_FLAG(MNT_IGNORE); 3652 MNT_FLAG(MNT_UPDATE); 3653 MNT_FLAG(MNT_DELEXPORT); 3654 MNT_FLAG(MNT_RELOAD); 3655 MNT_FLAG(MNT_FORCE); 3656 MNT_FLAG(MNT_SNAPSHOT); 3657 MNT_FLAG(MNT_BYFSID); 3658 #undef MNT_FLAG 3659 if (mflags != 0) { 3660 if (buf[0] != '\0') 3661 strlcat(buf, ", ", sizeof(buf)); 3662 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), 3663 "0x%016jx", mflags); 3664 } 3665 db_printf(" mnt_flag = %s\n", buf); 3666 3667 buf[0] = '\0'; 3668 flags = mp->mnt_kern_flag; 3669 #define MNT_KERN_FLAG(flag) do { \ 3670 if (flags & (flag)) { \ 3671 if (buf[0] != '\0') \ 3672 strlcat(buf, ", ", sizeof(buf)); \ 3673 strlcat(buf, (#flag) + 5, sizeof(buf)); \ 3674 flags &= ~(flag); \ 3675 } \ 3676 } while (0) 3677 MNT_KERN_FLAG(MNTK_UNMOUNTF); 3678 MNT_KERN_FLAG(MNTK_ASYNC); 3679 MNT_KERN_FLAG(MNTK_SOFTDEP); 3680 MNT_KERN_FLAG(MNTK_NOINSMNTQ); 3681 MNT_KERN_FLAG(MNTK_DRAINING); 3682 MNT_KERN_FLAG(MNTK_REFEXPIRE); 3683 MNT_KERN_FLAG(MNTK_EXTENDED_SHARED); 3684 MNT_KERN_FLAG(MNTK_SHARED_WRITES); 3685 MNT_KERN_FLAG(MNTK_NO_IOPF); 3686 MNT_KERN_FLAG(MNTK_VGONE_UPPER); 3687 MNT_KERN_FLAG(MNTK_VGONE_WAITER); 3688 MNT_KERN_FLAG(MNTK_LOOKUP_EXCL_DOTDOT); 3689 MNT_KERN_FLAG(MNTK_MARKER); 3690 MNT_KERN_FLAG(MNTK_USES_BCACHE); 3691 MNT_KERN_FLAG(MNTK_NOASYNC); 3692 MNT_KERN_FLAG(MNTK_UNMOUNT); 3693 MNT_KERN_FLAG(MNTK_MWAIT); 3694 MNT_KERN_FLAG(MNTK_SUSPEND); 3695 MNT_KERN_FLAG(MNTK_SUSPEND2); 3696 MNT_KERN_FLAG(MNTK_SUSPENDED); 3697 MNT_KERN_FLAG(MNTK_LOOKUP_SHARED); 3698 MNT_KERN_FLAG(MNTK_NOKNOTE); 3699 #undef MNT_KERN_FLAG 3700 if (flags != 0) { 3701 if (buf[0] != '\0') 3702 strlcat(buf, ", ", sizeof(buf)); 3703 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), 3704 "0x%08x", flags); 3705 } 3706 db_printf(" mnt_kern_flag = %s\n", buf); 3707 3708 db_printf(" mnt_opt = "); 3709 opt = TAILQ_FIRST(mp->mnt_opt); 3710 if (opt != NULL) { 3711 db_printf("%s", opt->name); 3712 opt = TAILQ_NEXT(opt, link); 3713 while (opt != NULL) { 3714 db_printf(", %s", opt->name); 3715 opt = TAILQ_NEXT(opt, link); 3716 } 3717 } 3718 db_printf("\n"); 3719 3720 sp = &mp->mnt_stat; 3721 db_printf(" mnt_stat = { version=%u type=%u flags=0x%016jx " 3722 "bsize=%ju iosize=%ju blocks=%ju bfree=%ju bavail=%jd files=%ju " 3723 "ffree=%jd syncwrites=%ju asyncwrites=%ju syncreads=%ju " 3724 "asyncreads=%ju namemax=%u owner=%u fsid=[%d, %d] }\n", 3725 (u_int)sp->f_version, (u_int)sp->f_type, (uintmax_t)sp->f_flags, 3726 (uintmax_t)sp->f_bsize, (uintmax_t)sp->f_iosize, 3727 (uintmax_t)sp->f_blocks, (uintmax_t)sp->f_bfree, 3728 (intmax_t)sp->f_bavail, (uintmax_t)sp->f_files, 3729 (intmax_t)sp->f_ffree, (uintmax_t)sp->f_syncwrites, 3730 (uintmax_t)sp->f_asyncwrites, (uintmax_t)sp->f_syncreads, 3731 (uintmax_t)sp->f_asyncreads, (u_int)sp->f_namemax, 3732 (u_int)sp->f_owner, (int)sp->f_fsid.val[0], (int)sp->f_fsid.val[1]); 3733 3734 db_printf(" mnt_cred = { uid=%u ruid=%u", 3735 (u_int)mp->mnt_cred->cr_uid, (u_int)mp->mnt_cred->cr_ruid); 3736 if (jailed(mp->mnt_cred)) 3737 db_printf(", jail=%d", mp->mnt_cred->cr_prison->pr_id); 3738 db_printf(" }\n"); 3739 db_printf(" mnt_ref = %d\n", mp->mnt_ref); 3740 db_printf(" mnt_gen = %d\n", mp->mnt_gen); 3741 db_printf(" mnt_nvnodelistsize = %d\n", mp->mnt_nvnodelistsize); 3742 db_printf(" mnt_activevnodelistsize = %d\n", 3743 mp->mnt_activevnodelistsize); 3744 db_printf(" mnt_writeopcount = %d\n", mp->mnt_writeopcount); 3745 db_printf(" mnt_maxsymlinklen = %d\n", mp->mnt_maxsymlinklen); 3746 db_printf(" mnt_iosize_max = %d\n", mp->mnt_iosize_max); 3747 db_printf(" mnt_hashseed = %u\n", mp->mnt_hashseed); 3748 db_printf(" mnt_lockref = %d\n", mp->mnt_lockref); 3749 db_printf(" mnt_secondary_writes = %d\n", mp->mnt_secondary_writes); 3750 db_printf(" mnt_secondary_accwrites = %d\n", 3751 mp->mnt_secondary_accwrites); 3752 db_printf(" mnt_gjprovider = %s\n", 3753 mp->mnt_gjprovider != NULL ? mp->mnt_gjprovider : "NULL"); 3754 3755 db_printf("\n\nList of active vnodes\n"); 3756 TAILQ_FOREACH(vp, &mp->mnt_activevnodelist, v_actfreelist) { 3757 if (vp->v_type != VMARKER) { 3758 vn_printf(vp, "vnode "); 3759 if (db_pager_quit) 3760 break; 3761 } 3762 } 3763 db_printf("\n\nList of inactive vnodes\n"); 3764 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) { 3765 if (vp->v_type != VMARKER && (vp->v_iflag & VI_ACTIVE) == 0) { 3766 vn_printf(vp, "vnode "); 3767 if (db_pager_quit) 3768 break; 3769 } 3770 } 3771 } 3772 #endif /* DDB */ 3773 3774 /* 3775 * Fill in a struct xvfsconf based on a struct vfsconf. 3776 */ 3777 static int 3778 vfsconf2x(struct sysctl_req *req, struct vfsconf *vfsp) 3779 { 3780 struct xvfsconf xvfsp; 3781 3782 bzero(&xvfsp, sizeof(xvfsp)); 3783 strcpy(xvfsp.vfc_name, vfsp->vfc_name); 3784 xvfsp.vfc_typenum = vfsp->vfc_typenum; 3785 xvfsp.vfc_refcount = vfsp->vfc_refcount; 3786 xvfsp.vfc_flags = vfsp->vfc_flags; 3787 /* 3788 * These are unused in userland, we keep them 3789 * to not break binary compatibility. 3790 */ 3791 xvfsp.vfc_vfsops = NULL; 3792 xvfsp.vfc_next = NULL; 3793 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp))); 3794 } 3795 3796 #ifdef COMPAT_FREEBSD32 3797 struct xvfsconf32 { 3798 uint32_t vfc_vfsops; 3799 char vfc_name[MFSNAMELEN]; 3800 int32_t vfc_typenum; 3801 int32_t vfc_refcount; 3802 int32_t vfc_flags; 3803 uint32_t vfc_next; 3804 }; 3805 3806 static int 3807 vfsconf2x32(struct sysctl_req *req, struct vfsconf *vfsp) 3808 { 3809 struct xvfsconf32 xvfsp; 3810 3811 bzero(&xvfsp, sizeof(xvfsp)); 3812 strcpy(xvfsp.vfc_name, vfsp->vfc_name); 3813 xvfsp.vfc_typenum = vfsp->vfc_typenum; 3814 xvfsp.vfc_refcount = vfsp->vfc_refcount; 3815 xvfsp.vfc_flags = vfsp->vfc_flags; 3816 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp))); 3817 } 3818 #endif 3819 3820 /* 3821 * Top level filesystem related information gathering. 3822 */ 3823 static int 3824 sysctl_vfs_conflist(SYSCTL_HANDLER_ARGS) 3825 { 3826 struct vfsconf *vfsp; 3827 int error; 3828 3829 error = 0; 3830 vfsconf_slock(); 3831 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) { 3832 #ifdef COMPAT_FREEBSD32 3833 if (req->flags & SCTL_MASK32) 3834 error = vfsconf2x32(req, vfsp); 3835 else 3836 #endif 3837 error = vfsconf2x(req, vfsp); 3838 if (error) 3839 break; 3840 } 3841 vfsconf_sunlock(); 3842 return (error); 3843 } 3844 3845 SYSCTL_PROC(_vfs, OID_AUTO, conflist, CTLTYPE_OPAQUE | CTLFLAG_RD | 3846 CTLFLAG_MPSAFE, NULL, 0, sysctl_vfs_conflist, 3847 "S,xvfsconf", "List of all configured filesystems"); 3848 3849 #ifndef BURN_BRIDGES 3850 static int sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS); 3851 3852 static int 3853 vfs_sysctl(SYSCTL_HANDLER_ARGS) 3854 { 3855 int *name = (int *)arg1 - 1; /* XXX */ 3856 u_int namelen = arg2 + 1; /* XXX */ 3857 struct vfsconf *vfsp; 3858 3859 log(LOG_WARNING, "userland calling deprecated sysctl, " 3860 "please rebuild world\n"); 3861 3862 #if 1 || defined(COMPAT_PRELITE2) 3863 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */ 3864 if (namelen == 1) 3865 return (sysctl_ovfs_conf(oidp, arg1, arg2, req)); 3866 #endif 3867 3868 switch (name[1]) { 3869 case VFS_MAXTYPENUM: 3870 if (namelen != 2) 3871 return (ENOTDIR); 3872 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int))); 3873 case VFS_CONF: 3874 if (namelen != 3) 3875 return (ENOTDIR); /* overloaded */ 3876 vfsconf_slock(); 3877 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) { 3878 if (vfsp->vfc_typenum == name[2]) 3879 break; 3880 } 3881 vfsconf_sunlock(); 3882 if (vfsp == NULL) 3883 return (EOPNOTSUPP); 3884 #ifdef COMPAT_FREEBSD32 3885 if (req->flags & SCTL_MASK32) 3886 return (vfsconf2x32(req, vfsp)); 3887 else 3888 #endif 3889 return (vfsconf2x(req, vfsp)); 3890 } 3891 return (EOPNOTSUPP); 3892 } 3893 3894 static SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD | CTLFLAG_SKIP | 3895 CTLFLAG_MPSAFE, vfs_sysctl, 3896 "Generic filesystem"); 3897 3898 #if 1 || defined(COMPAT_PRELITE2) 3899 3900 static int 3901 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS) 3902 { 3903 int error; 3904 struct vfsconf *vfsp; 3905 struct ovfsconf ovfs; 3906 3907 vfsconf_slock(); 3908 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) { 3909 bzero(&ovfs, sizeof(ovfs)); 3910 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */ 3911 strcpy(ovfs.vfc_name, vfsp->vfc_name); 3912 ovfs.vfc_index = vfsp->vfc_typenum; 3913 ovfs.vfc_refcount = vfsp->vfc_refcount; 3914 ovfs.vfc_flags = vfsp->vfc_flags; 3915 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs); 3916 if (error != 0) { 3917 vfsconf_sunlock(); 3918 return (error); 3919 } 3920 } 3921 vfsconf_sunlock(); 3922 return (0); 3923 } 3924 3925 #endif /* 1 || COMPAT_PRELITE2 */ 3926 #endif /* !BURN_BRIDGES */ 3927 3928 #define KINFO_VNODESLOP 10 3929 #ifdef notyet 3930 /* 3931 * Dump vnode list (via sysctl). 3932 */ 3933 /* ARGSUSED */ 3934 static int 3935 sysctl_vnode(SYSCTL_HANDLER_ARGS) 3936 { 3937 struct xvnode *xvn; 3938 struct mount *mp; 3939 struct vnode *vp; 3940 int error, len, n; 3941 3942 /* 3943 * Stale numvnodes access is not fatal here. 3944 */ 3945 req->lock = 0; 3946 len = (numvnodes + KINFO_VNODESLOP) * sizeof *xvn; 3947 if (!req->oldptr) 3948 /* Make an estimate */ 3949 return (SYSCTL_OUT(req, 0, len)); 3950 3951 error = sysctl_wire_old_buffer(req, 0); 3952 if (error != 0) 3953 return (error); 3954 xvn = malloc(len, M_TEMP, M_ZERO | M_WAITOK); 3955 n = 0; 3956 mtx_lock(&mountlist_mtx); 3957 TAILQ_FOREACH(mp, &mountlist, mnt_list) { 3958 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) 3959 continue; 3960 MNT_ILOCK(mp); 3961 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) { 3962 if (n == len) 3963 break; 3964 vref(vp); 3965 xvn[n].xv_size = sizeof *xvn; 3966 xvn[n].xv_vnode = vp; 3967 xvn[n].xv_id = 0; /* XXX compat */ 3968 #define XV_COPY(field) xvn[n].xv_##field = vp->v_##field 3969 XV_COPY(usecount); 3970 XV_COPY(writecount); 3971 XV_COPY(holdcnt); 3972 XV_COPY(mount); 3973 XV_COPY(numoutput); 3974 XV_COPY(type); 3975 #undef XV_COPY 3976 xvn[n].xv_flag = vp->v_vflag; 3977 3978 switch (vp->v_type) { 3979 case VREG: 3980 case VDIR: 3981 case VLNK: 3982 break; 3983 case VBLK: 3984 case VCHR: 3985 if (vp->v_rdev == NULL) { 3986 vrele(vp); 3987 continue; 3988 } 3989 xvn[n].xv_dev = dev2udev(vp->v_rdev); 3990 break; 3991 case VSOCK: 3992 xvn[n].xv_socket = vp->v_socket; 3993 break; 3994 case VFIFO: 3995 xvn[n].xv_fifo = vp->v_fifoinfo; 3996 break; 3997 case VNON: 3998 case VBAD: 3999 default: 4000 /* shouldn't happen? */ 4001 vrele(vp); 4002 continue; 4003 } 4004 vrele(vp); 4005 ++n; 4006 } 4007 MNT_IUNLOCK(mp); 4008 mtx_lock(&mountlist_mtx); 4009 vfs_unbusy(mp); 4010 if (n == len) 4011 break; 4012 } 4013 mtx_unlock(&mountlist_mtx); 4014 4015 error = SYSCTL_OUT(req, xvn, n * sizeof *xvn); 4016 free(xvn, M_TEMP); 4017 return (error); 4018 } 4019 4020 SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE | CTLFLAG_RD | 4021 CTLFLAG_MPSAFE, 0, 0, sysctl_vnode, "S,xvnode", 4022 ""); 4023 #endif 4024 4025 static void 4026 unmount_or_warn(struct mount *mp) 4027 { 4028 int error; 4029 4030 error = dounmount(mp, MNT_FORCE, curthread); 4031 if (error != 0) { 4032 printf("unmount of %s failed (", mp->mnt_stat.f_mntonname); 4033 if (error == EBUSY) 4034 printf("BUSY)\n"); 4035 else 4036 printf("%d)\n", error); 4037 } 4038 } 4039 4040 /* 4041 * Unmount all filesystems. The list is traversed in reverse order 4042 * of mounting to avoid dependencies. 4043 */ 4044 void 4045 vfs_unmountall(void) 4046 { 4047 struct mount *mp, *tmp; 4048 4049 CTR1(KTR_VFS, "%s: unmounting all filesystems", __func__); 4050 4051 /* 4052 * Since this only runs when rebooting, it is not interlocked. 4053 */ 4054 TAILQ_FOREACH_REVERSE_SAFE(mp, &mountlist, mntlist, mnt_list, tmp) { 4055 vfs_ref(mp); 4056 4057 /* 4058 * Forcibly unmounting "/dev" before "/" would prevent clean 4059 * unmount of the latter. 4060 */ 4061 if (mp == rootdevmp) 4062 continue; 4063 4064 unmount_or_warn(mp); 4065 } 4066 4067 if (rootdevmp != NULL) 4068 unmount_or_warn(rootdevmp); 4069 } 4070 4071 /* 4072 * perform msync on all vnodes under a mount point 4073 * the mount point must be locked. 4074 */ 4075 void 4076 vfs_msync(struct mount *mp, int flags) 4077 { 4078 struct vnode *vp, *mvp; 4079 struct vm_object *obj; 4080 4081 CTR2(KTR_VFS, "%s: mp %p", __func__, mp); 4082 4083 vnlru_return_batch(mp); 4084 4085 MNT_VNODE_FOREACH_ACTIVE(vp, mp, mvp) { 4086 obj = vp->v_object; 4087 if (obj != NULL && (obj->flags & OBJ_MIGHTBEDIRTY) != 0 && 4088 (flags == MNT_WAIT || VOP_ISLOCKED(vp) == 0)) { 4089 if (!vget(vp, 4090 LK_EXCLUSIVE | LK_RETRY | LK_INTERLOCK, 4091 curthread)) { 4092 if (vp->v_vflag & VV_NOSYNC) { /* unlinked */ 4093 vput(vp); 4094 continue; 4095 } 4096 4097 obj = vp->v_object; 4098 if (obj != NULL) { 4099 VM_OBJECT_WLOCK(obj); 4100 vm_object_page_clean(obj, 0, 0, 4101 flags == MNT_WAIT ? 4102 OBJPC_SYNC : OBJPC_NOSYNC); 4103 VM_OBJECT_WUNLOCK(obj); 4104 } 4105 vput(vp); 4106 } 4107 } else 4108 VI_UNLOCK(vp); 4109 } 4110 } 4111 4112 static void 4113 destroy_vpollinfo_free(struct vpollinfo *vi) 4114 { 4115 4116 knlist_destroy(&vi->vpi_selinfo.si_note); 4117 mtx_destroy(&vi->vpi_lock); 4118 uma_zfree(vnodepoll_zone, vi); 4119 } 4120 4121 static void 4122 destroy_vpollinfo(struct vpollinfo *vi) 4123 { 4124 4125 knlist_clear(&vi->vpi_selinfo.si_note, 1); 4126 seldrain(&vi->vpi_selinfo); 4127 destroy_vpollinfo_free(vi); 4128 } 4129 4130 /* 4131 * Initialize per-vnode helper structure to hold poll-related state. 4132 */ 4133 void 4134 v_addpollinfo(struct vnode *vp) 4135 { 4136 struct vpollinfo *vi; 4137 4138 if (vp->v_pollinfo != NULL) 4139 return; 4140 vi = uma_zalloc(vnodepoll_zone, M_WAITOK | M_ZERO); 4141 mtx_init(&vi->vpi_lock, "vnode pollinfo", NULL, MTX_DEF); 4142 knlist_init(&vi->vpi_selinfo.si_note, vp, vfs_knllock, 4143 vfs_knlunlock, vfs_knl_assert_locked, vfs_knl_assert_unlocked); 4144 VI_LOCK(vp); 4145 if (vp->v_pollinfo != NULL) { 4146 VI_UNLOCK(vp); 4147 destroy_vpollinfo_free(vi); 4148 return; 4149 } 4150 vp->v_pollinfo = vi; 4151 VI_UNLOCK(vp); 4152 } 4153 4154 /* 4155 * Record a process's interest in events which might happen to 4156 * a vnode. Because poll uses the historic select-style interface 4157 * internally, this routine serves as both the ``check for any 4158 * pending events'' and the ``record my interest in future events'' 4159 * functions. (These are done together, while the lock is held, 4160 * to avoid race conditions.) 4161 */ 4162 int 4163 vn_pollrecord(struct vnode *vp, struct thread *td, int events) 4164 { 4165 4166 v_addpollinfo(vp); 4167 mtx_lock(&vp->v_pollinfo->vpi_lock); 4168 if (vp->v_pollinfo->vpi_revents & events) { 4169 /* 4170 * This leaves events we are not interested 4171 * in available for the other process which 4172 * which presumably had requested them 4173 * (otherwise they would never have been 4174 * recorded). 4175 */ 4176 events &= vp->v_pollinfo->vpi_revents; 4177 vp->v_pollinfo->vpi_revents &= ~events; 4178 4179 mtx_unlock(&vp->v_pollinfo->vpi_lock); 4180 return (events); 4181 } 4182 vp->v_pollinfo->vpi_events |= events; 4183 selrecord(td, &vp->v_pollinfo->vpi_selinfo); 4184 mtx_unlock(&vp->v_pollinfo->vpi_lock); 4185 return (0); 4186 } 4187 4188 /* 4189 * Routine to create and manage a filesystem syncer vnode. 4190 */ 4191 #define sync_close ((int (*)(struct vop_close_args *))nullop) 4192 static int sync_fsync(struct vop_fsync_args *); 4193 static int sync_inactive(struct vop_inactive_args *); 4194 static int sync_reclaim(struct vop_reclaim_args *); 4195 4196 static struct vop_vector sync_vnodeops = { 4197 .vop_bypass = VOP_EOPNOTSUPP, 4198 .vop_close = sync_close, /* close */ 4199 .vop_fsync = sync_fsync, /* fsync */ 4200 .vop_inactive = sync_inactive, /* inactive */ 4201 .vop_reclaim = sync_reclaim, /* reclaim */ 4202 .vop_lock1 = vop_stdlock, /* lock */ 4203 .vop_unlock = vop_stdunlock, /* unlock */ 4204 .vop_islocked = vop_stdislocked, /* islocked */ 4205 }; 4206 4207 /* 4208 * Create a new filesystem syncer vnode for the specified mount point. 4209 */ 4210 void 4211 vfs_allocate_syncvnode(struct mount *mp) 4212 { 4213 struct vnode *vp; 4214 struct bufobj *bo; 4215 static long start, incr, next; 4216 int error; 4217 4218 /* Allocate a new vnode */ 4219 error = getnewvnode("syncer", mp, &sync_vnodeops, &vp); 4220 if (error != 0) 4221 panic("vfs_allocate_syncvnode: getnewvnode() failed"); 4222 vp->v_type = VNON; 4223 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 4224 vp->v_vflag |= VV_FORCEINSMQ; 4225 error = insmntque(vp, mp); 4226 if (error != 0) 4227 panic("vfs_allocate_syncvnode: insmntque() failed"); 4228 vp->v_vflag &= ~VV_FORCEINSMQ; 4229 VOP_UNLOCK(vp, 0); 4230 /* 4231 * Place the vnode onto the syncer worklist. We attempt to 4232 * scatter them about on the list so that they will go off 4233 * at evenly distributed times even if all the filesystems 4234 * are mounted at once. 4235 */ 4236 next += incr; 4237 if (next == 0 || next > syncer_maxdelay) { 4238 start /= 2; 4239 incr /= 2; 4240 if (start == 0) { 4241 start = syncer_maxdelay / 2; 4242 incr = syncer_maxdelay; 4243 } 4244 next = start; 4245 } 4246 bo = &vp->v_bufobj; 4247 BO_LOCK(bo); 4248 vn_syncer_add_to_worklist(bo, syncdelay > 0 ? next % syncdelay : 0); 4249 /* XXX - vn_syncer_add_to_worklist() also grabs and drops sync_mtx. */ 4250 mtx_lock(&sync_mtx); 4251 sync_vnode_count++; 4252 if (mp->mnt_syncer == NULL) { 4253 mp->mnt_syncer = vp; 4254 vp = NULL; 4255 } 4256 mtx_unlock(&sync_mtx); 4257 BO_UNLOCK(bo); 4258 if (vp != NULL) { 4259 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 4260 vgone(vp); 4261 vput(vp); 4262 } 4263 } 4264 4265 void 4266 vfs_deallocate_syncvnode(struct mount *mp) 4267 { 4268 struct vnode *vp; 4269 4270 mtx_lock(&sync_mtx); 4271 vp = mp->mnt_syncer; 4272 if (vp != NULL) 4273 mp->mnt_syncer = NULL; 4274 mtx_unlock(&sync_mtx); 4275 if (vp != NULL) 4276 vrele(vp); 4277 } 4278 4279 /* 4280 * Do a lazy sync of the filesystem. 4281 */ 4282 static int 4283 sync_fsync(struct vop_fsync_args *ap) 4284 { 4285 struct vnode *syncvp = ap->a_vp; 4286 struct mount *mp = syncvp->v_mount; 4287 int error, save; 4288 struct bufobj *bo; 4289 4290 /* 4291 * We only need to do something if this is a lazy evaluation. 4292 */ 4293 if (ap->a_waitfor != MNT_LAZY) 4294 return (0); 4295 4296 /* 4297 * Move ourselves to the back of the sync list. 4298 */ 4299 bo = &syncvp->v_bufobj; 4300 BO_LOCK(bo); 4301 vn_syncer_add_to_worklist(bo, syncdelay); 4302 BO_UNLOCK(bo); 4303 4304 /* 4305 * Walk the list of vnodes pushing all that are dirty and 4306 * not already on the sync list. 4307 */ 4308 if (vfs_busy(mp, MBF_NOWAIT) != 0) 4309 return (0); 4310 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) { 4311 vfs_unbusy(mp); 4312 return (0); 4313 } 4314 save = curthread_pflags_set(TDP_SYNCIO); 4315 vfs_msync(mp, MNT_NOWAIT); 4316 error = VFS_SYNC(mp, MNT_LAZY); 4317 curthread_pflags_restore(save); 4318 vn_finished_write(mp); 4319 vfs_unbusy(mp); 4320 return (error); 4321 } 4322 4323 /* 4324 * The syncer vnode is no referenced. 4325 */ 4326 static int 4327 sync_inactive(struct vop_inactive_args *ap) 4328 { 4329 4330 vgone(ap->a_vp); 4331 return (0); 4332 } 4333 4334 /* 4335 * The syncer vnode is no longer needed and is being decommissioned. 4336 * 4337 * Modifications to the worklist must be protected by sync_mtx. 4338 */ 4339 static int 4340 sync_reclaim(struct vop_reclaim_args *ap) 4341 { 4342 struct vnode *vp = ap->a_vp; 4343 struct bufobj *bo; 4344 4345 bo = &vp->v_bufobj; 4346 BO_LOCK(bo); 4347 mtx_lock(&sync_mtx); 4348 if (vp->v_mount->mnt_syncer == vp) 4349 vp->v_mount->mnt_syncer = NULL; 4350 if (bo->bo_flag & BO_ONWORKLST) { 4351 LIST_REMOVE(bo, bo_synclist); 4352 syncer_worklist_len--; 4353 sync_vnode_count--; 4354 bo->bo_flag &= ~BO_ONWORKLST; 4355 } 4356 mtx_unlock(&sync_mtx); 4357 BO_UNLOCK(bo); 4358 4359 return (0); 4360 } 4361 4362 /* 4363 * Check if vnode represents a disk device 4364 */ 4365 int 4366 vn_isdisk(struct vnode *vp, int *errp) 4367 { 4368 int error; 4369 4370 if (vp->v_type != VCHR) { 4371 error = ENOTBLK; 4372 goto out; 4373 } 4374 error = 0; 4375 dev_lock(); 4376 if (vp->v_rdev == NULL) 4377 error = ENXIO; 4378 else if (vp->v_rdev->si_devsw == NULL) 4379 error = ENXIO; 4380 else if (!(vp->v_rdev->si_devsw->d_flags & D_DISK)) 4381 error = ENOTBLK; 4382 dev_unlock(); 4383 out: 4384 if (errp != NULL) 4385 *errp = error; 4386 return (error == 0); 4387 } 4388 4389 /* 4390 * Common filesystem object access control check routine. Accepts a 4391 * vnode's type, "mode", uid and gid, requested access mode, credentials, 4392 * and optional call-by-reference privused argument allowing vaccess() 4393 * to indicate to the caller whether privilege was used to satisfy the 4394 * request (obsoleted). Returns 0 on success, or an errno on failure. 4395 */ 4396 int 4397 vaccess(enum vtype type, mode_t file_mode, uid_t file_uid, gid_t file_gid, 4398 accmode_t accmode, struct ucred *cred, int *privused) 4399 { 4400 accmode_t dac_granted; 4401 accmode_t priv_granted; 4402 4403 KASSERT((accmode & ~(VEXEC | VWRITE | VREAD | VADMIN | VAPPEND)) == 0, 4404 ("invalid bit in accmode")); 4405 KASSERT((accmode & VAPPEND) == 0 || (accmode & VWRITE), 4406 ("VAPPEND without VWRITE")); 4407 4408 /* 4409 * Look for a normal, non-privileged way to access the file/directory 4410 * as requested. If it exists, go with that. 4411 */ 4412 4413 if (privused != NULL) 4414 *privused = 0; 4415 4416 dac_granted = 0; 4417 4418 /* Check the owner. */ 4419 if (cred->cr_uid == file_uid) { 4420 dac_granted |= VADMIN; 4421 if (file_mode & S_IXUSR) 4422 dac_granted |= VEXEC; 4423 if (file_mode & S_IRUSR) 4424 dac_granted |= VREAD; 4425 if (file_mode & S_IWUSR) 4426 dac_granted |= (VWRITE | VAPPEND); 4427 4428 if ((accmode & dac_granted) == accmode) 4429 return (0); 4430 4431 goto privcheck; 4432 } 4433 4434 /* Otherwise, check the groups (first match) */ 4435 if (groupmember(file_gid, cred)) { 4436 if (file_mode & S_IXGRP) 4437 dac_granted |= VEXEC; 4438 if (file_mode & S_IRGRP) 4439 dac_granted |= VREAD; 4440 if (file_mode & S_IWGRP) 4441 dac_granted |= (VWRITE | VAPPEND); 4442 4443 if ((accmode & dac_granted) == accmode) 4444 return (0); 4445 4446 goto privcheck; 4447 } 4448 4449 /* Otherwise, check everyone else. */ 4450 if (file_mode & S_IXOTH) 4451 dac_granted |= VEXEC; 4452 if (file_mode & S_IROTH) 4453 dac_granted |= VREAD; 4454 if (file_mode & S_IWOTH) 4455 dac_granted |= (VWRITE | VAPPEND); 4456 if ((accmode & dac_granted) == accmode) 4457 return (0); 4458 4459 privcheck: 4460 /* 4461 * Build a privilege mask to determine if the set of privileges 4462 * satisfies the requirements when combined with the granted mask 4463 * from above. For each privilege, if the privilege is required, 4464 * bitwise or the request type onto the priv_granted mask. 4465 */ 4466 priv_granted = 0; 4467 4468 if (type == VDIR) { 4469 /* 4470 * For directories, use PRIV_VFS_LOOKUP to satisfy VEXEC 4471 * requests, instead of PRIV_VFS_EXEC. 4472 */ 4473 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) && 4474 !priv_check_cred(cred, PRIV_VFS_LOOKUP, 0)) 4475 priv_granted |= VEXEC; 4476 } else { 4477 /* 4478 * Ensure that at least one execute bit is on. Otherwise, 4479 * a privileged user will always succeed, and we don't want 4480 * this to happen unless the file really is executable. 4481 */ 4482 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) && 4483 (file_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0 && 4484 !priv_check_cred(cred, PRIV_VFS_EXEC, 0)) 4485 priv_granted |= VEXEC; 4486 } 4487 4488 if ((accmode & VREAD) && ((dac_granted & VREAD) == 0) && 4489 !priv_check_cred(cred, PRIV_VFS_READ, 0)) 4490 priv_granted |= VREAD; 4491 4492 if ((accmode & VWRITE) && ((dac_granted & VWRITE) == 0) && 4493 !priv_check_cred(cred, PRIV_VFS_WRITE, 0)) 4494 priv_granted |= (VWRITE | VAPPEND); 4495 4496 if ((accmode & VADMIN) && ((dac_granted & VADMIN) == 0) && 4497 !priv_check_cred(cred, PRIV_VFS_ADMIN, 0)) 4498 priv_granted |= VADMIN; 4499 4500 if ((accmode & (priv_granted | dac_granted)) == accmode) { 4501 /* XXX audit: privilege used */ 4502 if (privused != NULL) 4503 *privused = 1; 4504 return (0); 4505 } 4506 4507 return ((accmode & VADMIN) ? EPERM : EACCES); 4508 } 4509 4510 /* 4511 * Credential check based on process requesting service, and per-attribute 4512 * permissions. 4513 */ 4514 int 4515 extattr_check_cred(struct vnode *vp, int attrnamespace, struct ucred *cred, 4516 struct thread *td, accmode_t accmode) 4517 { 4518 4519 /* 4520 * Kernel-invoked always succeeds. 4521 */ 4522 if (cred == NOCRED) 4523 return (0); 4524 4525 /* 4526 * Do not allow privileged processes in jail to directly manipulate 4527 * system attributes. 4528 */ 4529 switch (attrnamespace) { 4530 case EXTATTR_NAMESPACE_SYSTEM: 4531 /* Potentially should be: return (EPERM); */ 4532 return (priv_check_cred(cred, PRIV_VFS_EXTATTR_SYSTEM, 0)); 4533 case EXTATTR_NAMESPACE_USER: 4534 return (VOP_ACCESS(vp, accmode, cred, td)); 4535 default: 4536 return (EPERM); 4537 } 4538 } 4539 4540 #ifdef DEBUG_VFS_LOCKS 4541 /* 4542 * This only exists to suppress warnings from unlocked specfs accesses. It is 4543 * no longer ok to have an unlocked VFS. 4544 */ 4545 #define IGNORE_LOCK(vp) (panicstr != NULL || (vp) == NULL || \ 4546 (vp)->v_type == VCHR || (vp)->v_type == VBAD) 4547 4548 int vfs_badlock_ddb = 1; /* Drop into debugger on violation. */ 4549 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_ddb, CTLFLAG_RW, &vfs_badlock_ddb, 0, 4550 "Drop into debugger on lock violation"); 4551 4552 int vfs_badlock_mutex = 1; /* Check for interlock across VOPs. */ 4553 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_mutex, CTLFLAG_RW, &vfs_badlock_mutex, 4554 0, "Check for interlock across VOPs"); 4555 4556 int vfs_badlock_print = 1; /* Print lock violations. */ 4557 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_print, CTLFLAG_RW, &vfs_badlock_print, 4558 0, "Print lock violations"); 4559 4560 int vfs_badlock_vnode = 1; /* Print vnode details on lock violations. */ 4561 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_vnode, CTLFLAG_RW, &vfs_badlock_vnode, 4562 0, "Print vnode details on lock violations"); 4563 4564 #ifdef KDB 4565 int vfs_badlock_backtrace = 1; /* Print backtrace at lock violations. */ 4566 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_backtrace, CTLFLAG_RW, 4567 &vfs_badlock_backtrace, 0, "Print backtrace at lock violations"); 4568 #endif 4569 4570 static void 4571 vfs_badlock(const char *msg, const char *str, struct vnode *vp) 4572 { 4573 4574 #ifdef KDB 4575 if (vfs_badlock_backtrace) 4576 kdb_backtrace(); 4577 #endif 4578 if (vfs_badlock_vnode) 4579 vn_printf(vp, "vnode "); 4580 if (vfs_badlock_print) 4581 printf("%s: %p %s\n", str, (void *)vp, msg); 4582 if (vfs_badlock_ddb) 4583 kdb_enter(KDB_WHY_VFSLOCK, "lock violation"); 4584 } 4585 4586 void 4587 assert_vi_locked(struct vnode *vp, const char *str) 4588 { 4589 4590 if (vfs_badlock_mutex && !mtx_owned(VI_MTX(vp))) 4591 vfs_badlock("interlock is not locked but should be", str, vp); 4592 } 4593 4594 void 4595 assert_vi_unlocked(struct vnode *vp, const char *str) 4596 { 4597 4598 if (vfs_badlock_mutex && mtx_owned(VI_MTX(vp))) 4599 vfs_badlock("interlock is locked but should not be", str, vp); 4600 } 4601 4602 void 4603 assert_vop_locked(struct vnode *vp, const char *str) 4604 { 4605 int locked; 4606 4607 if (!IGNORE_LOCK(vp)) { 4608 locked = VOP_ISLOCKED(vp); 4609 if (locked == 0 || locked == LK_EXCLOTHER) 4610 vfs_badlock("is not locked but should be", str, vp); 4611 } 4612 } 4613 4614 void 4615 assert_vop_unlocked(struct vnode *vp, const char *str) 4616 { 4617 4618 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) == LK_EXCLUSIVE) 4619 vfs_badlock("is locked but should not be", str, vp); 4620 } 4621 4622 void 4623 assert_vop_elocked(struct vnode *vp, const char *str) 4624 { 4625 4626 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_EXCLUSIVE) 4627 vfs_badlock("is not exclusive locked but should be", str, vp); 4628 } 4629 #endif /* DEBUG_VFS_LOCKS */ 4630 4631 void 4632 vop_rename_fail(struct vop_rename_args *ap) 4633 { 4634 4635 if (ap->a_tvp != NULL) 4636 vput(ap->a_tvp); 4637 if (ap->a_tdvp == ap->a_tvp) 4638 vrele(ap->a_tdvp); 4639 else 4640 vput(ap->a_tdvp); 4641 vrele(ap->a_fdvp); 4642 vrele(ap->a_fvp); 4643 } 4644 4645 void 4646 vop_rename_pre(void *ap) 4647 { 4648 struct vop_rename_args *a = ap; 4649 4650 #ifdef DEBUG_VFS_LOCKS 4651 if (a->a_tvp) 4652 ASSERT_VI_UNLOCKED(a->a_tvp, "VOP_RENAME"); 4653 ASSERT_VI_UNLOCKED(a->a_tdvp, "VOP_RENAME"); 4654 ASSERT_VI_UNLOCKED(a->a_fvp, "VOP_RENAME"); 4655 ASSERT_VI_UNLOCKED(a->a_fdvp, "VOP_RENAME"); 4656 4657 /* Check the source (from). */ 4658 if (a->a_tdvp->v_vnlock != a->a_fdvp->v_vnlock && 4659 (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fdvp->v_vnlock)) 4660 ASSERT_VOP_UNLOCKED(a->a_fdvp, "vop_rename: fdvp locked"); 4661 if (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fvp->v_vnlock) 4662 ASSERT_VOP_UNLOCKED(a->a_fvp, "vop_rename: fvp locked"); 4663 4664 /* Check the target. */ 4665 if (a->a_tvp) 4666 ASSERT_VOP_LOCKED(a->a_tvp, "vop_rename: tvp not locked"); 4667 ASSERT_VOP_LOCKED(a->a_tdvp, "vop_rename: tdvp not locked"); 4668 #endif 4669 if (a->a_tdvp != a->a_fdvp) 4670 vhold(a->a_fdvp); 4671 if (a->a_tvp != a->a_fvp) 4672 vhold(a->a_fvp); 4673 vhold(a->a_tdvp); 4674 if (a->a_tvp) 4675 vhold(a->a_tvp); 4676 } 4677 4678 #ifdef DEBUG_VFS_LOCKS 4679 void 4680 vop_strategy_pre(void *ap) 4681 { 4682 struct vop_strategy_args *a; 4683 struct buf *bp; 4684 4685 a = ap; 4686 bp = a->a_bp; 4687 4688 /* 4689 * Cluster ops lock their component buffers but not the IO container. 4690 */ 4691 if ((bp->b_flags & B_CLUSTER) != 0) 4692 return; 4693 4694 if (panicstr == NULL && !BUF_ISLOCKED(bp)) { 4695 if (vfs_badlock_print) 4696 printf( 4697 "VOP_STRATEGY: bp is not locked but should be\n"); 4698 if (vfs_badlock_ddb) 4699 kdb_enter(KDB_WHY_VFSLOCK, "lock violation"); 4700 } 4701 } 4702 4703 void 4704 vop_lock_pre(void *ap) 4705 { 4706 struct vop_lock1_args *a = ap; 4707 4708 if ((a->a_flags & LK_INTERLOCK) == 0) 4709 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK"); 4710 else 4711 ASSERT_VI_LOCKED(a->a_vp, "VOP_LOCK"); 4712 } 4713 4714 void 4715 vop_lock_post(void *ap, int rc) 4716 { 4717 struct vop_lock1_args *a = ap; 4718 4719 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK"); 4720 if (rc == 0 && (a->a_flags & LK_EXCLOTHER) == 0) 4721 ASSERT_VOP_LOCKED(a->a_vp, "VOP_LOCK"); 4722 } 4723 4724 void 4725 vop_unlock_pre(void *ap) 4726 { 4727 struct vop_unlock_args *a = ap; 4728 4729 if (a->a_flags & LK_INTERLOCK) 4730 ASSERT_VI_LOCKED(a->a_vp, "VOP_UNLOCK"); 4731 ASSERT_VOP_LOCKED(a->a_vp, "VOP_UNLOCK"); 4732 } 4733 4734 void 4735 vop_unlock_post(void *ap, int rc) 4736 { 4737 struct vop_unlock_args *a = ap; 4738 4739 if (a->a_flags & LK_INTERLOCK) 4740 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_UNLOCK"); 4741 } 4742 #endif 4743 4744 void 4745 vop_create_post(void *ap, int rc) 4746 { 4747 struct vop_create_args *a = ap; 4748 4749 if (!rc) 4750 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE); 4751 } 4752 4753 void 4754 vop_deleteextattr_post(void *ap, int rc) 4755 { 4756 struct vop_deleteextattr_args *a = ap; 4757 4758 if (!rc) 4759 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB); 4760 } 4761 4762 void 4763 vop_link_post(void *ap, int rc) 4764 { 4765 struct vop_link_args *a = ap; 4766 4767 if (!rc) { 4768 VFS_KNOTE_LOCKED(a->a_vp, NOTE_LINK); 4769 VFS_KNOTE_LOCKED(a->a_tdvp, NOTE_WRITE); 4770 } 4771 } 4772 4773 void 4774 vop_mkdir_post(void *ap, int rc) 4775 { 4776 struct vop_mkdir_args *a = ap; 4777 4778 if (!rc) 4779 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK); 4780 } 4781 4782 void 4783 vop_mknod_post(void *ap, int rc) 4784 { 4785 struct vop_mknod_args *a = ap; 4786 4787 if (!rc) 4788 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE); 4789 } 4790 4791 void 4792 vop_reclaim_post(void *ap, int rc) 4793 { 4794 struct vop_reclaim_args *a = ap; 4795 4796 if (!rc) 4797 VFS_KNOTE_LOCKED(a->a_vp, NOTE_REVOKE); 4798 } 4799 4800 void 4801 vop_remove_post(void *ap, int rc) 4802 { 4803 struct vop_remove_args *a = ap; 4804 4805 if (!rc) { 4806 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE); 4807 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE); 4808 } 4809 } 4810 4811 void 4812 vop_rename_post(void *ap, int rc) 4813 { 4814 struct vop_rename_args *a = ap; 4815 long hint; 4816 4817 if (!rc) { 4818 hint = NOTE_WRITE; 4819 if (a->a_fdvp == a->a_tdvp) { 4820 if (a->a_tvp != NULL && a->a_tvp->v_type == VDIR) 4821 hint |= NOTE_LINK; 4822 VFS_KNOTE_UNLOCKED(a->a_fdvp, hint); 4823 VFS_KNOTE_UNLOCKED(a->a_tdvp, hint); 4824 } else { 4825 hint |= NOTE_EXTEND; 4826 if (a->a_fvp->v_type == VDIR) 4827 hint |= NOTE_LINK; 4828 VFS_KNOTE_UNLOCKED(a->a_fdvp, hint); 4829 4830 if (a->a_fvp->v_type == VDIR && a->a_tvp != NULL && 4831 a->a_tvp->v_type == VDIR) 4832 hint &= ~NOTE_LINK; 4833 VFS_KNOTE_UNLOCKED(a->a_tdvp, hint); 4834 } 4835 4836 VFS_KNOTE_UNLOCKED(a->a_fvp, NOTE_RENAME); 4837 if (a->a_tvp) 4838 VFS_KNOTE_UNLOCKED(a->a_tvp, NOTE_DELETE); 4839 } 4840 if (a->a_tdvp != a->a_fdvp) 4841 vdrop(a->a_fdvp); 4842 if (a->a_tvp != a->a_fvp) 4843 vdrop(a->a_fvp); 4844 vdrop(a->a_tdvp); 4845 if (a->a_tvp) 4846 vdrop(a->a_tvp); 4847 } 4848 4849 void 4850 vop_rmdir_post(void *ap, int rc) 4851 { 4852 struct vop_rmdir_args *a = ap; 4853 4854 if (!rc) { 4855 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK); 4856 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE); 4857 } 4858 } 4859 4860 void 4861 vop_setattr_post(void *ap, int rc) 4862 { 4863 struct vop_setattr_args *a = ap; 4864 4865 if (!rc) 4866 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB); 4867 } 4868 4869 void 4870 vop_setextattr_post(void *ap, int rc) 4871 { 4872 struct vop_setextattr_args *a = ap; 4873 4874 if (!rc) 4875 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB); 4876 } 4877 4878 void 4879 vop_symlink_post(void *ap, int rc) 4880 { 4881 struct vop_symlink_args *a = ap; 4882 4883 if (!rc) 4884 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE); 4885 } 4886 4887 void 4888 vop_open_post(void *ap, int rc) 4889 { 4890 struct vop_open_args *a = ap; 4891 4892 if (!rc) 4893 VFS_KNOTE_LOCKED(a->a_vp, NOTE_OPEN); 4894 } 4895 4896 void 4897 vop_close_post(void *ap, int rc) 4898 { 4899 struct vop_close_args *a = ap; 4900 4901 if (!rc && (a->a_cred != NOCRED || /* filter out revokes */ 4902 (a->a_vp->v_iflag & VI_DOOMED) == 0)) { 4903 VFS_KNOTE_LOCKED(a->a_vp, (a->a_fflag & FWRITE) != 0 ? 4904 NOTE_CLOSE_WRITE : NOTE_CLOSE); 4905 } 4906 } 4907 4908 void 4909 vop_read_post(void *ap, int rc) 4910 { 4911 struct vop_read_args *a = ap; 4912 4913 if (!rc) 4914 VFS_KNOTE_LOCKED(a->a_vp, NOTE_READ); 4915 } 4916 4917 void 4918 vop_readdir_post(void *ap, int rc) 4919 { 4920 struct vop_readdir_args *a = ap; 4921 4922 if (!rc) 4923 VFS_KNOTE_LOCKED(a->a_vp, NOTE_READ); 4924 } 4925 4926 static struct knlist fs_knlist; 4927 4928 static void 4929 vfs_event_init(void *arg) 4930 { 4931 knlist_init_mtx(&fs_knlist, NULL); 4932 } 4933 /* XXX - correct order? */ 4934 SYSINIT(vfs_knlist, SI_SUB_VFS, SI_ORDER_ANY, vfs_event_init, NULL); 4935 4936 void 4937 vfs_event_signal(fsid_t *fsid, uint32_t event, intptr_t data __unused) 4938 { 4939 4940 KNOTE_UNLOCKED(&fs_knlist, event); 4941 } 4942 4943 static int filt_fsattach(struct knote *kn); 4944 static void filt_fsdetach(struct knote *kn); 4945 static int filt_fsevent(struct knote *kn, long hint); 4946 4947 struct filterops fs_filtops = { 4948 .f_isfd = 0, 4949 .f_attach = filt_fsattach, 4950 .f_detach = filt_fsdetach, 4951 .f_event = filt_fsevent 4952 }; 4953 4954 static int 4955 filt_fsattach(struct knote *kn) 4956 { 4957 4958 kn->kn_flags |= EV_CLEAR; 4959 knlist_add(&fs_knlist, kn, 0); 4960 return (0); 4961 } 4962 4963 static void 4964 filt_fsdetach(struct knote *kn) 4965 { 4966 4967 knlist_remove(&fs_knlist, kn, 0); 4968 } 4969 4970 static int 4971 filt_fsevent(struct knote *kn, long hint) 4972 { 4973 4974 kn->kn_fflags |= hint; 4975 return (kn->kn_fflags != 0); 4976 } 4977 4978 static int 4979 sysctl_vfs_ctl(SYSCTL_HANDLER_ARGS) 4980 { 4981 struct vfsidctl vc; 4982 int error; 4983 struct mount *mp; 4984 4985 error = SYSCTL_IN(req, &vc, sizeof(vc)); 4986 if (error) 4987 return (error); 4988 if (vc.vc_vers != VFS_CTL_VERS1) 4989 return (EINVAL); 4990 mp = vfs_getvfs(&vc.vc_fsid); 4991 if (mp == NULL) 4992 return (ENOENT); 4993 /* ensure that a specific sysctl goes to the right filesystem. */ 4994 if (strcmp(vc.vc_fstypename, "*") != 0 && 4995 strcmp(vc.vc_fstypename, mp->mnt_vfc->vfc_name) != 0) { 4996 vfs_rel(mp); 4997 return (EINVAL); 4998 } 4999 VCTLTOREQ(&vc, req); 5000 error = VFS_SYSCTL(mp, vc.vc_op, req); 5001 vfs_rel(mp); 5002 return (error); 5003 } 5004 5005 SYSCTL_PROC(_vfs, OID_AUTO, ctl, CTLTYPE_OPAQUE | CTLFLAG_WR, 5006 NULL, 0, sysctl_vfs_ctl, "", 5007 "Sysctl by fsid"); 5008 5009 /* 5010 * Function to initialize a va_filerev field sensibly. 5011 * XXX: Wouldn't a random number make a lot more sense ?? 5012 */ 5013 u_quad_t 5014 init_va_filerev(void) 5015 { 5016 struct bintime bt; 5017 5018 getbinuptime(&bt); 5019 return (((u_quad_t)bt.sec << 32LL) | (bt.frac >> 32LL)); 5020 } 5021 5022 static int filt_vfsread(struct knote *kn, long hint); 5023 static int filt_vfswrite(struct knote *kn, long hint); 5024 static int filt_vfsvnode(struct knote *kn, long hint); 5025 static void filt_vfsdetach(struct knote *kn); 5026 static struct filterops vfsread_filtops = { 5027 .f_isfd = 1, 5028 .f_detach = filt_vfsdetach, 5029 .f_event = filt_vfsread 5030 }; 5031 static struct filterops vfswrite_filtops = { 5032 .f_isfd = 1, 5033 .f_detach = filt_vfsdetach, 5034 .f_event = filt_vfswrite 5035 }; 5036 static struct filterops vfsvnode_filtops = { 5037 .f_isfd = 1, 5038 .f_detach = filt_vfsdetach, 5039 .f_event = filt_vfsvnode 5040 }; 5041 5042 static void 5043 vfs_knllock(void *arg) 5044 { 5045 struct vnode *vp = arg; 5046 5047 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 5048 } 5049 5050 static void 5051 vfs_knlunlock(void *arg) 5052 { 5053 struct vnode *vp = arg; 5054 5055 VOP_UNLOCK(vp, 0); 5056 } 5057 5058 static void 5059 vfs_knl_assert_locked(void *arg) 5060 { 5061 #ifdef DEBUG_VFS_LOCKS 5062 struct vnode *vp = arg; 5063 5064 ASSERT_VOP_LOCKED(vp, "vfs_knl_assert_locked"); 5065 #endif 5066 } 5067 5068 static void 5069 vfs_knl_assert_unlocked(void *arg) 5070 { 5071 #ifdef DEBUG_VFS_LOCKS 5072 struct vnode *vp = arg; 5073 5074 ASSERT_VOP_UNLOCKED(vp, "vfs_knl_assert_unlocked"); 5075 #endif 5076 } 5077 5078 int 5079 vfs_kqfilter(struct vop_kqfilter_args *ap) 5080 { 5081 struct vnode *vp = ap->a_vp; 5082 struct knote *kn = ap->a_kn; 5083 struct knlist *knl; 5084 5085 switch (kn->kn_filter) { 5086 case EVFILT_READ: 5087 kn->kn_fop = &vfsread_filtops; 5088 break; 5089 case EVFILT_WRITE: 5090 kn->kn_fop = &vfswrite_filtops; 5091 break; 5092 case EVFILT_VNODE: 5093 kn->kn_fop = &vfsvnode_filtops; 5094 break; 5095 default: 5096 return (EINVAL); 5097 } 5098 5099 kn->kn_hook = (caddr_t)vp; 5100 5101 v_addpollinfo(vp); 5102 if (vp->v_pollinfo == NULL) 5103 return (ENOMEM); 5104 knl = &vp->v_pollinfo->vpi_selinfo.si_note; 5105 vhold(vp); 5106 knlist_add(knl, kn, 0); 5107 5108 return (0); 5109 } 5110 5111 /* 5112 * Detach knote from vnode 5113 */ 5114 static void 5115 filt_vfsdetach(struct knote *kn) 5116 { 5117 struct vnode *vp = (struct vnode *)kn->kn_hook; 5118 5119 KASSERT(vp->v_pollinfo != NULL, ("Missing v_pollinfo")); 5120 knlist_remove(&vp->v_pollinfo->vpi_selinfo.si_note, kn, 0); 5121 vdrop(vp); 5122 } 5123 5124 /*ARGSUSED*/ 5125 static int 5126 filt_vfsread(struct knote *kn, long hint) 5127 { 5128 struct vnode *vp = (struct vnode *)kn->kn_hook; 5129 struct vattr va; 5130 int res; 5131 5132 /* 5133 * filesystem is gone, so set the EOF flag and schedule 5134 * the knote for deletion. 5135 */ 5136 if (hint == NOTE_REVOKE || (hint == 0 && vp->v_type == VBAD)) { 5137 VI_LOCK(vp); 5138 kn->kn_flags |= (EV_EOF | EV_ONESHOT); 5139 VI_UNLOCK(vp); 5140 return (1); 5141 } 5142 5143 if (VOP_GETATTR(vp, &va, curthread->td_ucred)) 5144 return (0); 5145 5146 VI_LOCK(vp); 5147 kn->kn_data = va.va_size - kn->kn_fp->f_offset; 5148 res = (kn->kn_sfflags & NOTE_FILE_POLL) != 0 || kn->kn_data != 0; 5149 VI_UNLOCK(vp); 5150 return (res); 5151 } 5152 5153 /*ARGSUSED*/ 5154 static int 5155 filt_vfswrite(struct knote *kn, long hint) 5156 { 5157 struct vnode *vp = (struct vnode *)kn->kn_hook; 5158 5159 VI_LOCK(vp); 5160 5161 /* 5162 * filesystem is gone, so set the EOF flag and schedule 5163 * the knote for deletion. 5164 */ 5165 if (hint == NOTE_REVOKE || (hint == 0 && vp->v_type == VBAD)) 5166 kn->kn_flags |= (EV_EOF | EV_ONESHOT); 5167 5168 kn->kn_data = 0; 5169 VI_UNLOCK(vp); 5170 return (1); 5171 } 5172 5173 static int 5174 filt_vfsvnode(struct knote *kn, long hint) 5175 { 5176 struct vnode *vp = (struct vnode *)kn->kn_hook; 5177 int res; 5178 5179 VI_LOCK(vp); 5180 if (kn->kn_sfflags & hint) 5181 kn->kn_fflags |= hint; 5182 if (hint == NOTE_REVOKE || (hint == 0 && vp->v_type == VBAD)) { 5183 kn->kn_flags |= EV_EOF; 5184 VI_UNLOCK(vp); 5185 return (1); 5186 } 5187 res = (kn->kn_fflags != 0); 5188 VI_UNLOCK(vp); 5189 return (res); 5190 } 5191 5192 int 5193 vfs_read_dirent(struct vop_readdir_args *ap, struct dirent *dp, off_t off) 5194 { 5195 int error; 5196 5197 if (dp->d_reclen > ap->a_uio->uio_resid) 5198 return (ENAMETOOLONG); 5199 error = uiomove(dp, dp->d_reclen, ap->a_uio); 5200 if (error) { 5201 if (ap->a_ncookies != NULL) { 5202 if (ap->a_cookies != NULL) 5203 free(ap->a_cookies, M_TEMP); 5204 ap->a_cookies = NULL; 5205 *ap->a_ncookies = 0; 5206 } 5207 return (error); 5208 } 5209 if (ap->a_ncookies == NULL) 5210 return (0); 5211 5212 KASSERT(ap->a_cookies, 5213 ("NULL ap->a_cookies value with non-NULL ap->a_ncookies!")); 5214 5215 *ap->a_cookies = realloc(*ap->a_cookies, 5216 (*ap->a_ncookies + 1) * sizeof(u_long), M_TEMP, M_WAITOK | M_ZERO); 5217 (*ap->a_cookies)[*ap->a_ncookies] = off; 5218 *ap->a_ncookies += 1; 5219 return (0); 5220 } 5221 5222 /* 5223 * Mark for update the access time of the file if the filesystem 5224 * supports VOP_MARKATIME. This functionality is used by execve and 5225 * mmap, so we want to avoid the I/O implied by directly setting 5226 * va_atime for the sake of efficiency. 5227 */ 5228 void 5229 vfs_mark_atime(struct vnode *vp, struct ucred *cred) 5230 { 5231 struct mount *mp; 5232 5233 mp = vp->v_mount; 5234 ASSERT_VOP_LOCKED(vp, "vfs_mark_atime"); 5235 if (mp != NULL && (mp->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0) 5236 (void)VOP_MARKATIME(vp); 5237 } 5238 5239 /* 5240 * The purpose of this routine is to remove granularity from accmode_t, 5241 * reducing it into standard unix access bits - VEXEC, VREAD, VWRITE, 5242 * VADMIN and VAPPEND. 5243 * 5244 * If it returns 0, the caller is supposed to continue with the usual 5245 * access checks using 'accmode' as modified by this routine. If it 5246 * returns nonzero value, the caller is supposed to return that value 5247 * as errno. 5248 * 5249 * Note that after this routine runs, accmode may be zero. 5250 */ 5251 int 5252 vfs_unixify_accmode(accmode_t *accmode) 5253 { 5254 /* 5255 * There is no way to specify explicit "deny" rule using 5256 * file mode or POSIX.1e ACLs. 5257 */ 5258 if (*accmode & VEXPLICIT_DENY) { 5259 *accmode = 0; 5260 return (0); 5261 } 5262 5263 /* 5264 * None of these can be translated into usual access bits. 5265 * Also, the common case for NFSv4 ACLs is to not contain 5266 * either of these bits. Caller should check for VWRITE 5267 * on the containing directory instead. 5268 */ 5269 if (*accmode & (VDELETE_CHILD | VDELETE)) 5270 return (EPERM); 5271 5272 if (*accmode & VADMIN_PERMS) { 5273 *accmode &= ~VADMIN_PERMS; 5274 *accmode |= VADMIN; 5275 } 5276 5277 /* 5278 * There is no way to deny VREAD_ATTRIBUTES, VREAD_ACL 5279 * or VSYNCHRONIZE using file mode or POSIX.1e ACL. 5280 */ 5281 *accmode &= ~(VSTAT_PERMS | VSYNCHRONIZE); 5282 5283 return (0); 5284 } 5285 5286 /* 5287 * These are helper functions for filesystems to traverse all 5288 * their vnodes. See MNT_VNODE_FOREACH_ALL() in sys/mount.h. 5289 * 5290 * This interface replaces MNT_VNODE_FOREACH. 5291 */ 5292 5293 MALLOC_DEFINE(M_VNODE_MARKER, "vnodemarker", "vnode marker"); 5294 5295 struct vnode * 5296 __mnt_vnode_next_all(struct vnode **mvp, struct mount *mp) 5297 { 5298 struct vnode *vp; 5299 5300 if (should_yield()) 5301 kern_yield(PRI_USER); 5302 MNT_ILOCK(mp); 5303 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch")); 5304 for (vp = TAILQ_NEXT(*mvp, v_nmntvnodes); vp != NULL; 5305 vp = TAILQ_NEXT(vp, v_nmntvnodes)) { 5306 /* Allow a racy peek at VI_DOOMED to save a lock acquisition. */ 5307 if (vp->v_type == VMARKER || (vp->v_iflag & VI_DOOMED) != 0) 5308 continue; 5309 VI_LOCK(vp); 5310 if ((vp->v_iflag & VI_DOOMED) != 0) { 5311 VI_UNLOCK(vp); 5312 continue; 5313 } 5314 break; 5315 } 5316 if (vp == NULL) { 5317 __mnt_vnode_markerfree_all(mvp, mp); 5318 /* MNT_IUNLOCK(mp); -- done in above function */ 5319 mtx_assert(MNT_MTX(mp), MA_NOTOWNED); 5320 return (NULL); 5321 } 5322 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes); 5323 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes); 5324 MNT_IUNLOCK(mp); 5325 return (vp); 5326 } 5327 5328 struct vnode * 5329 __mnt_vnode_first_all(struct vnode **mvp, struct mount *mp) 5330 { 5331 struct vnode *vp; 5332 5333 *mvp = malloc(sizeof(struct vnode), M_VNODE_MARKER, M_WAITOK | M_ZERO); 5334 MNT_ILOCK(mp); 5335 MNT_REF(mp); 5336 (*mvp)->v_mount = mp; 5337 (*mvp)->v_type = VMARKER; 5338 5339 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) { 5340 /* Allow a racy peek at VI_DOOMED to save a lock acquisition. */ 5341 if (vp->v_type == VMARKER || (vp->v_iflag & VI_DOOMED) != 0) 5342 continue; 5343 VI_LOCK(vp); 5344 if ((vp->v_iflag & VI_DOOMED) != 0) { 5345 VI_UNLOCK(vp); 5346 continue; 5347 } 5348 break; 5349 } 5350 if (vp == NULL) { 5351 MNT_REL(mp); 5352 MNT_IUNLOCK(mp); 5353 free(*mvp, M_VNODE_MARKER); 5354 *mvp = NULL; 5355 return (NULL); 5356 } 5357 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes); 5358 MNT_IUNLOCK(mp); 5359 return (vp); 5360 } 5361 5362 void 5363 __mnt_vnode_markerfree_all(struct vnode **mvp, struct mount *mp) 5364 { 5365 5366 if (*mvp == NULL) { 5367 MNT_IUNLOCK(mp); 5368 return; 5369 } 5370 5371 mtx_assert(MNT_MTX(mp), MA_OWNED); 5372 5373 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch")); 5374 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes); 5375 MNT_REL(mp); 5376 MNT_IUNLOCK(mp); 5377 free(*mvp, M_VNODE_MARKER); 5378 *mvp = NULL; 5379 } 5380 5381 /* 5382 * These are helper functions for filesystems to traverse their 5383 * active vnodes. See MNT_VNODE_FOREACH_ACTIVE() in sys/mount.h 5384 */ 5385 static void 5386 mnt_vnode_markerfree_active(struct vnode **mvp, struct mount *mp) 5387 { 5388 5389 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch")); 5390 5391 MNT_ILOCK(mp); 5392 MNT_REL(mp); 5393 MNT_IUNLOCK(mp); 5394 free(*mvp, M_VNODE_MARKER); 5395 *mvp = NULL; 5396 } 5397 5398 /* 5399 * Relock the mp mount vnode list lock with the vp vnode interlock in the 5400 * conventional lock order during mnt_vnode_next_active iteration. 5401 * 5402 * On entry, the mount vnode list lock is held and the vnode interlock is not. 5403 * The list lock is dropped and reacquired. On success, both locks are held. 5404 * On failure, the mount vnode list lock is held but the vnode interlock is 5405 * not, and the procedure may have yielded. 5406 */ 5407 static bool 5408 mnt_vnode_next_active_relock(struct vnode *mvp, struct mount *mp, 5409 struct vnode *vp) 5410 { 5411 const struct vnode *tmp; 5412 bool held, ret; 5413 5414 VNASSERT(mvp->v_mount == mp && mvp->v_type == VMARKER && 5415 TAILQ_NEXT(mvp, v_actfreelist) != NULL, mvp, 5416 ("%s: bad marker", __func__)); 5417 VNASSERT(vp->v_mount == mp && vp->v_type != VMARKER, vp, 5418 ("%s: inappropriate vnode", __func__)); 5419 ASSERT_VI_UNLOCKED(vp, __func__); 5420 mtx_assert(&mp->mnt_listmtx, MA_OWNED); 5421 5422 ret = false; 5423 5424 TAILQ_REMOVE(&mp->mnt_activevnodelist, mvp, v_actfreelist); 5425 TAILQ_INSERT_BEFORE(vp, mvp, v_actfreelist); 5426 5427 /* 5428 * Use a hold to prevent vp from disappearing while the mount vnode 5429 * list lock is dropped and reacquired. Normally a hold would be 5430 * acquired with vhold(), but that might try to acquire the vnode 5431 * interlock, which would be a LOR with the mount vnode list lock. 5432 */ 5433 held = vfs_refcount_acquire_if_not_zero(&vp->v_holdcnt); 5434 mtx_unlock(&mp->mnt_listmtx); 5435 if (!held) 5436 goto abort; 5437 VI_LOCK(vp); 5438 if (!vfs_refcount_release_if_not_last(&vp->v_holdcnt)) { 5439 vdropl(vp); 5440 goto abort; 5441 } 5442 mtx_lock(&mp->mnt_listmtx); 5443 5444 /* 5445 * Determine whether the vnode is still the next one after the marker, 5446 * excepting any other markers. If the vnode has not been doomed by 5447 * vgone() then the hold should have ensured that it remained on the 5448 * active list. If it has been doomed but is still on the active list, 5449 * don't abort, but rather skip over it (avoid spinning on doomed 5450 * vnodes). 5451 */ 5452 tmp = mvp; 5453 do { 5454 tmp = TAILQ_NEXT(tmp, v_actfreelist); 5455 } while (tmp != NULL && tmp->v_type == VMARKER); 5456 if (tmp != vp) { 5457 mtx_unlock(&mp->mnt_listmtx); 5458 VI_UNLOCK(vp); 5459 goto abort; 5460 } 5461 5462 ret = true; 5463 goto out; 5464 abort: 5465 maybe_yield(); 5466 mtx_lock(&mp->mnt_listmtx); 5467 out: 5468 if (ret) 5469 ASSERT_VI_LOCKED(vp, __func__); 5470 else 5471 ASSERT_VI_UNLOCKED(vp, __func__); 5472 mtx_assert(&mp->mnt_listmtx, MA_OWNED); 5473 return (ret); 5474 } 5475 5476 static struct vnode * 5477 mnt_vnode_next_active(struct vnode **mvp, struct mount *mp) 5478 { 5479 struct vnode *vp, *nvp; 5480 5481 mtx_assert(&mp->mnt_listmtx, MA_OWNED); 5482 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch")); 5483 restart: 5484 vp = TAILQ_NEXT(*mvp, v_actfreelist); 5485 while (vp != NULL) { 5486 if (vp->v_type == VMARKER) { 5487 vp = TAILQ_NEXT(vp, v_actfreelist); 5488 continue; 5489 } 5490 /* 5491 * Try-lock because this is the wrong lock order. If that does 5492 * not succeed, drop the mount vnode list lock and try to 5493 * reacquire it and the vnode interlock in the right order. 5494 */ 5495 if (!VI_TRYLOCK(vp) && 5496 !mnt_vnode_next_active_relock(*mvp, mp, vp)) 5497 goto restart; 5498 KASSERT(vp->v_type != VMARKER, ("locked marker %p", vp)); 5499 KASSERT(vp->v_mount == mp || vp->v_mount == NULL, 5500 ("alien vnode on the active list %p %p", vp, mp)); 5501 if (vp->v_mount == mp && (vp->v_iflag & VI_DOOMED) == 0) 5502 break; 5503 nvp = TAILQ_NEXT(vp, v_actfreelist); 5504 VI_UNLOCK(vp); 5505 vp = nvp; 5506 } 5507 TAILQ_REMOVE(&mp->mnt_activevnodelist, *mvp, v_actfreelist); 5508 5509 /* Check if we are done */ 5510 if (vp == NULL) { 5511 mtx_unlock(&mp->mnt_listmtx); 5512 mnt_vnode_markerfree_active(mvp, mp); 5513 return (NULL); 5514 } 5515 TAILQ_INSERT_AFTER(&mp->mnt_activevnodelist, vp, *mvp, v_actfreelist); 5516 mtx_unlock(&mp->mnt_listmtx); 5517 ASSERT_VI_LOCKED(vp, "active iter"); 5518 KASSERT((vp->v_iflag & VI_ACTIVE) != 0, ("Non-active vp %p", vp)); 5519 return (vp); 5520 } 5521 5522 struct vnode * 5523 __mnt_vnode_next_active(struct vnode **mvp, struct mount *mp) 5524 { 5525 5526 if (should_yield()) 5527 kern_yield(PRI_USER); 5528 mtx_lock(&mp->mnt_listmtx); 5529 return (mnt_vnode_next_active(mvp, mp)); 5530 } 5531 5532 struct vnode * 5533 __mnt_vnode_first_active(struct vnode **mvp, struct mount *mp) 5534 { 5535 struct vnode *vp; 5536 5537 *mvp = malloc(sizeof(struct vnode), M_VNODE_MARKER, M_WAITOK | M_ZERO); 5538 MNT_ILOCK(mp); 5539 MNT_REF(mp); 5540 MNT_IUNLOCK(mp); 5541 (*mvp)->v_type = VMARKER; 5542 (*mvp)->v_mount = mp; 5543 5544 mtx_lock(&mp->mnt_listmtx); 5545 vp = TAILQ_FIRST(&mp->mnt_activevnodelist); 5546 if (vp == NULL) { 5547 mtx_unlock(&mp->mnt_listmtx); 5548 mnt_vnode_markerfree_active(mvp, mp); 5549 return (NULL); 5550 } 5551 TAILQ_INSERT_BEFORE(vp, *mvp, v_actfreelist); 5552 return (mnt_vnode_next_active(mvp, mp)); 5553 } 5554 5555 void 5556 __mnt_vnode_markerfree_active(struct vnode **mvp, struct mount *mp) 5557 { 5558 5559 if (*mvp == NULL) 5560 return; 5561 5562 mtx_lock(&mp->mnt_listmtx); 5563 TAILQ_REMOVE(&mp->mnt_activevnodelist, *mvp, v_actfreelist); 5564 mtx_unlock(&mp->mnt_listmtx); 5565 mnt_vnode_markerfree_active(mvp, mp); 5566 } 5567