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