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