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 break; 1678 error = BUF_TIMELOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL | 1679 LK_INTERLOCK, BO_LOCKPTR(bo), "brlsfl", 0, 0); 1680 if (error != 0) { 1681 BO_RLOCK(bo); 1682 if (error == ENOLCK) 1683 goto again; 1684 return (error); 1685 } 1686 KASSERT(bp->b_bufobj == bo, 1687 ("bp %p wrong b_bufobj %p should be %p", 1688 bp, bp->b_bufobj, bo)); 1689 lblkno = bp->b_lblkno + 1; 1690 if ((bp->b_flags & B_MANAGED) == 0) 1691 bremfree(bp); 1692 bp->b_flags |= B_RELBUF; 1693 /* 1694 * In the VMIO case, use the B_NOREUSE flag to hint that the 1695 * pages backing each buffer in the range are unlikely to be 1696 * reused. Dirty buffers will have the hint applied once 1697 * they've been written. 1698 */ 1699 if (bp->b_vp->v_object != NULL) 1700 bp->b_flags |= B_NOREUSE; 1701 brelse(bp); 1702 BO_RLOCK(bo); 1703 } 1704 return (0); 1705 } 1706 1707 /* 1708 * Truncate a file's buffer and pages to a specified length. This 1709 * is in lieu of the old vinvalbuf mechanism, which performed unneeded 1710 * sync activity. 1711 */ 1712 int 1713 vtruncbuf(struct vnode *vp, struct ucred *cred, off_t length, int blksize) 1714 { 1715 struct buf *bp, *nbp; 1716 int anyfreed; 1717 int trunclbn; 1718 struct bufobj *bo; 1719 1720 CTR5(KTR_VFS, "%s: vp %p with cred %p and block %d:%ju", __func__, 1721 vp, cred, blksize, (uintmax_t)length); 1722 1723 /* 1724 * Round up to the *next* lbn. 1725 */ 1726 trunclbn = (length + blksize - 1) / blksize; 1727 1728 ASSERT_VOP_LOCKED(vp, "vtruncbuf"); 1729 restart: 1730 bo = &vp->v_bufobj; 1731 BO_LOCK(bo); 1732 anyfreed = 1; 1733 for (;anyfreed;) { 1734 anyfreed = 0; 1735 TAILQ_FOREACH_SAFE(bp, &bo->bo_clean.bv_hd, b_bobufs, nbp) { 1736 if (bp->b_lblkno < trunclbn) 1737 continue; 1738 if (BUF_LOCK(bp, 1739 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, 1740 BO_LOCKPTR(bo)) == ENOLCK) 1741 goto restart; 1742 1743 bremfree(bp); 1744 bp->b_flags |= (B_INVAL | B_RELBUF); 1745 bp->b_flags &= ~B_ASYNC; 1746 brelse(bp); 1747 anyfreed = 1; 1748 1749 BO_LOCK(bo); 1750 if (nbp != NULL && 1751 (((nbp->b_xflags & BX_VNCLEAN) == 0) || 1752 (nbp->b_vp != vp) || 1753 (nbp->b_flags & B_DELWRI))) { 1754 BO_UNLOCK(bo); 1755 goto restart; 1756 } 1757 } 1758 1759 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) { 1760 if (bp->b_lblkno < trunclbn) 1761 continue; 1762 if (BUF_LOCK(bp, 1763 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, 1764 BO_LOCKPTR(bo)) == ENOLCK) 1765 goto restart; 1766 bremfree(bp); 1767 bp->b_flags |= (B_INVAL | B_RELBUF); 1768 bp->b_flags &= ~B_ASYNC; 1769 brelse(bp); 1770 anyfreed = 1; 1771 1772 BO_LOCK(bo); 1773 if (nbp != NULL && 1774 (((nbp->b_xflags & BX_VNDIRTY) == 0) || 1775 (nbp->b_vp != vp) || 1776 (nbp->b_flags & B_DELWRI) == 0)) { 1777 BO_UNLOCK(bo); 1778 goto restart; 1779 } 1780 } 1781 } 1782 1783 if (length > 0) { 1784 restartsync: 1785 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) { 1786 if (bp->b_lblkno > 0) 1787 continue; 1788 /* 1789 * Since we hold the vnode lock this should only 1790 * fail if we're racing with the buf daemon. 1791 */ 1792 if (BUF_LOCK(bp, 1793 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, 1794 BO_LOCKPTR(bo)) == ENOLCK) { 1795 goto restart; 1796 } 1797 VNASSERT((bp->b_flags & B_DELWRI), vp, 1798 ("buf(%p) on dirty queue without DELWRI", bp)); 1799 1800 bremfree(bp); 1801 bawrite(bp); 1802 BO_LOCK(bo); 1803 goto restartsync; 1804 } 1805 } 1806 1807 bufobj_wwait(bo, 0, 0); 1808 BO_UNLOCK(bo); 1809 vnode_pager_setsize(vp, length); 1810 1811 return (0); 1812 } 1813 1814 static void 1815 buf_vlist_remove(struct buf *bp) 1816 { 1817 struct bufv *bv; 1818 1819 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp)); 1820 ASSERT_BO_WLOCKED(bp->b_bufobj); 1821 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) != 1822 (BX_VNDIRTY|BX_VNCLEAN), 1823 ("buf_vlist_remove: Buf %p is on two lists", bp)); 1824 if (bp->b_xflags & BX_VNDIRTY) 1825 bv = &bp->b_bufobj->bo_dirty; 1826 else 1827 bv = &bp->b_bufobj->bo_clean; 1828 BUF_PCTRIE_REMOVE(&bv->bv_root, bp->b_lblkno); 1829 TAILQ_REMOVE(&bv->bv_hd, bp, b_bobufs); 1830 bv->bv_cnt--; 1831 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN); 1832 } 1833 1834 /* 1835 * Add the buffer to the sorted clean or dirty block list. 1836 * 1837 * NOTE: xflags is passed as a constant, optimizing this inline function! 1838 */ 1839 static void 1840 buf_vlist_add(struct buf *bp, struct bufobj *bo, b_xflags_t xflags) 1841 { 1842 struct bufv *bv; 1843 struct buf *n; 1844 int error; 1845 1846 ASSERT_BO_WLOCKED(bo); 1847 KASSERT((xflags & BX_VNDIRTY) == 0 || (bo->bo_flag & BO_DEAD) == 0, 1848 ("dead bo %p", bo)); 1849 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0, 1850 ("buf_vlist_add: Buf %p has existing xflags %d", bp, bp->b_xflags)); 1851 bp->b_xflags |= xflags; 1852 if (xflags & BX_VNDIRTY) 1853 bv = &bo->bo_dirty; 1854 else 1855 bv = &bo->bo_clean; 1856 1857 /* 1858 * Keep the list ordered. Optimize empty list insertion. Assume 1859 * we tend to grow at the tail so lookup_le should usually be cheaper 1860 * than _ge. 1861 */ 1862 if (bv->bv_cnt == 0 || 1863 bp->b_lblkno > TAILQ_LAST(&bv->bv_hd, buflists)->b_lblkno) 1864 TAILQ_INSERT_TAIL(&bv->bv_hd, bp, b_bobufs); 1865 else if ((n = BUF_PCTRIE_LOOKUP_LE(&bv->bv_root, bp->b_lblkno)) == NULL) 1866 TAILQ_INSERT_HEAD(&bv->bv_hd, bp, b_bobufs); 1867 else 1868 TAILQ_INSERT_AFTER(&bv->bv_hd, n, bp, b_bobufs); 1869 error = BUF_PCTRIE_INSERT(&bv->bv_root, bp); 1870 if (error) 1871 panic("buf_vlist_add: Preallocated nodes insufficient."); 1872 bv->bv_cnt++; 1873 } 1874 1875 /* 1876 * Look up a buffer using the buffer tries. 1877 */ 1878 struct buf * 1879 gbincore(struct bufobj *bo, daddr_t lblkno) 1880 { 1881 struct buf *bp; 1882 1883 ASSERT_BO_LOCKED(bo); 1884 bp = BUF_PCTRIE_LOOKUP(&bo->bo_clean.bv_root, lblkno); 1885 if (bp != NULL) 1886 return (bp); 1887 return BUF_PCTRIE_LOOKUP(&bo->bo_dirty.bv_root, lblkno); 1888 } 1889 1890 /* 1891 * Associate a buffer with a vnode. 1892 */ 1893 void 1894 bgetvp(struct vnode *vp, struct buf *bp) 1895 { 1896 struct bufobj *bo; 1897 1898 bo = &vp->v_bufobj; 1899 ASSERT_BO_WLOCKED(bo); 1900 VNASSERT(bp->b_vp == NULL, bp->b_vp, ("bgetvp: not free")); 1901 1902 CTR3(KTR_BUF, "bgetvp(%p) vp %p flags %X", bp, vp, bp->b_flags); 1903 VNASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0, vp, 1904 ("bgetvp: bp already attached! %p", bp)); 1905 1906 vhold(vp); 1907 bp->b_vp = vp; 1908 bp->b_bufobj = bo; 1909 /* 1910 * Insert onto list for new vnode. 1911 */ 1912 buf_vlist_add(bp, bo, BX_VNCLEAN); 1913 } 1914 1915 /* 1916 * Disassociate a buffer from a vnode. 1917 */ 1918 void 1919 brelvp(struct buf *bp) 1920 { 1921 struct bufobj *bo; 1922 struct vnode *vp; 1923 1924 CTR3(KTR_BUF, "brelvp(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags); 1925 KASSERT(bp->b_vp != NULL, ("brelvp: NULL")); 1926 1927 /* 1928 * Delete from old vnode list, if on one. 1929 */ 1930 vp = bp->b_vp; /* XXX */ 1931 bo = bp->b_bufobj; 1932 BO_LOCK(bo); 1933 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) 1934 buf_vlist_remove(bp); 1935 else 1936 panic("brelvp: Buffer %p not on queue.", bp); 1937 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) { 1938 bo->bo_flag &= ~BO_ONWORKLST; 1939 mtx_lock(&sync_mtx); 1940 LIST_REMOVE(bo, bo_synclist); 1941 syncer_worklist_len--; 1942 mtx_unlock(&sync_mtx); 1943 } 1944 bp->b_vp = NULL; 1945 bp->b_bufobj = NULL; 1946 BO_UNLOCK(bo); 1947 vdrop(vp); 1948 } 1949 1950 /* 1951 * Add an item to the syncer work queue. 1952 */ 1953 static void 1954 vn_syncer_add_to_worklist(struct bufobj *bo, int delay) 1955 { 1956 int slot; 1957 1958 ASSERT_BO_WLOCKED(bo); 1959 1960 mtx_lock(&sync_mtx); 1961 if (bo->bo_flag & BO_ONWORKLST) 1962 LIST_REMOVE(bo, bo_synclist); 1963 else { 1964 bo->bo_flag |= BO_ONWORKLST; 1965 syncer_worklist_len++; 1966 } 1967 1968 if (delay > syncer_maxdelay - 2) 1969 delay = syncer_maxdelay - 2; 1970 slot = (syncer_delayno + delay) & syncer_mask; 1971 1972 LIST_INSERT_HEAD(&syncer_workitem_pending[slot], bo, bo_synclist); 1973 mtx_unlock(&sync_mtx); 1974 } 1975 1976 static int 1977 sysctl_vfs_worklist_len(SYSCTL_HANDLER_ARGS) 1978 { 1979 int error, len; 1980 1981 mtx_lock(&sync_mtx); 1982 len = syncer_worklist_len - sync_vnode_count; 1983 mtx_unlock(&sync_mtx); 1984 error = SYSCTL_OUT(req, &len, sizeof(len)); 1985 return (error); 1986 } 1987 1988 SYSCTL_PROC(_vfs, OID_AUTO, worklist_len, CTLTYPE_INT | CTLFLAG_RD, NULL, 0, 1989 sysctl_vfs_worklist_len, "I", "Syncer thread worklist length"); 1990 1991 static struct proc *updateproc; 1992 static void sched_sync(void); 1993 static struct kproc_desc up_kp = { 1994 "syncer", 1995 sched_sync, 1996 &updateproc 1997 }; 1998 SYSINIT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp); 1999 2000 static int 2001 sync_vnode(struct synclist *slp, struct bufobj **bo, struct thread *td) 2002 { 2003 struct vnode *vp; 2004 struct mount *mp; 2005 2006 *bo = LIST_FIRST(slp); 2007 if (*bo == NULL) 2008 return (0); 2009 vp = (*bo)->__bo_vnode; /* XXX */ 2010 if (VOP_ISLOCKED(vp) != 0 || VI_TRYLOCK(vp) == 0) 2011 return (1); 2012 /* 2013 * We use vhold in case the vnode does not 2014 * successfully sync. vhold prevents the vnode from 2015 * going away when we unlock the sync_mtx so that 2016 * we can acquire the vnode interlock. 2017 */ 2018 vholdl(vp); 2019 mtx_unlock(&sync_mtx); 2020 VI_UNLOCK(vp); 2021 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) { 2022 vdrop(vp); 2023 mtx_lock(&sync_mtx); 2024 return (*bo == LIST_FIRST(slp)); 2025 } 2026 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 2027 (void) VOP_FSYNC(vp, MNT_LAZY, td); 2028 VOP_UNLOCK(vp, 0); 2029 vn_finished_write(mp); 2030 BO_LOCK(*bo); 2031 if (((*bo)->bo_flag & BO_ONWORKLST) != 0) { 2032 /* 2033 * Put us back on the worklist. The worklist 2034 * routine will remove us from our current 2035 * position and then add us back in at a later 2036 * position. 2037 */ 2038 vn_syncer_add_to_worklist(*bo, syncdelay); 2039 } 2040 BO_UNLOCK(*bo); 2041 vdrop(vp); 2042 mtx_lock(&sync_mtx); 2043 return (0); 2044 } 2045 2046 static int first_printf = 1; 2047 2048 /* 2049 * System filesystem synchronizer daemon. 2050 */ 2051 static void 2052 sched_sync(void) 2053 { 2054 struct synclist *next, *slp; 2055 struct bufobj *bo; 2056 long starttime; 2057 struct thread *td = curthread; 2058 int last_work_seen; 2059 int net_worklist_len; 2060 int syncer_final_iter; 2061 int error; 2062 2063 last_work_seen = 0; 2064 syncer_final_iter = 0; 2065 syncer_state = SYNCER_RUNNING; 2066 starttime = time_uptime; 2067 td->td_pflags |= TDP_NORUNNINGBUF; 2068 2069 EVENTHANDLER_REGISTER(shutdown_pre_sync, syncer_shutdown, td->td_proc, 2070 SHUTDOWN_PRI_LAST); 2071 2072 mtx_lock(&sync_mtx); 2073 for (;;) { 2074 if (syncer_state == SYNCER_FINAL_DELAY && 2075 syncer_final_iter == 0) { 2076 mtx_unlock(&sync_mtx); 2077 kproc_suspend_check(td->td_proc); 2078 mtx_lock(&sync_mtx); 2079 } 2080 net_worklist_len = syncer_worklist_len - sync_vnode_count; 2081 if (syncer_state != SYNCER_RUNNING && 2082 starttime != time_uptime) { 2083 if (first_printf) { 2084 printf("\nSyncing disks, vnodes remaining..."); 2085 first_printf = 0; 2086 } 2087 printf("%d ", net_worklist_len); 2088 } 2089 starttime = time_uptime; 2090 2091 /* 2092 * Push files whose dirty time has expired. Be careful 2093 * of interrupt race on slp queue. 2094 * 2095 * Skip over empty worklist slots when shutting down. 2096 */ 2097 do { 2098 slp = &syncer_workitem_pending[syncer_delayno]; 2099 syncer_delayno += 1; 2100 if (syncer_delayno == syncer_maxdelay) 2101 syncer_delayno = 0; 2102 next = &syncer_workitem_pending[syncer_delayno]; 2103 /* 2104 * If the worklist has wrapped since the 2105 * it was emptied of all but syncer vnodes, 2106 * switch to the FINAL_DELAY state and run 2107 * for one more second. 2108 */ 2109 if (syncer_state == SYNCER_SHUTTING_DOWN && 2110 net_worklist_len == 0 && 2111 last_work_seen == syncer_delayno) { 2112 syncer_state = SYNCER_FINAL_DELAY; 2113 syncer_final_iter = SYNCER_SHUTDOWN_SPEEDUP; 2114 } 2115 } while (syncer_state != SYNCER_RUNNING && LIST_EMPTY(slp) && 2116 syncer_worklist_len > 0); 2117 2118 /* 2119 * Keep track of the last time there was anything 2120 * on the worklist other than syncer vnodes. 2121 * Return to the SHUTTING_DOWN state if any 2122 * new work appears. 2123 */ 2124 if (net_worklist_len > 0 || syncer_state == SYNCER_RUNNING) 2125 last_work_seen = syncer_delayno; 2126 if (net_worklist_len > 0 && syncer_state == SYNCER_FINAL_DELAY) 2127 syncer_state = SYNCER_SHUTTING_DOWN; 2128 while (!LIST_EMPTY(slp)) { 2129 error = sync_vnode(slp, &bo, td); 2130 if (error == 1) { 2131 LIST_REMOVE(bo, bo_synclist); 2132 LIST_INSERT_HEAD(next, bo, bo_synclist); 2133 continue; 2134 } 2135 2136 if (first_printf == 0) { 2137 /* 2138 * Drop the sync mutex, because some watchdog 2139 * drivers need to sleep while patting 2140 */ 2141 mtx_unlock(&sync_mtx); 2142 wdog_kern_pat(WD_LASTVAL); 2143 mtx_lock(&sync_mtx); 2144 } 2145 2146 } 2147 if (syncer_state == SYNCER_FINAL_DELAY && syncer_final_iter > 0) 2148 syncer_final_iter--; 2149 /* 2150 * The variable rushjob allows the kernel to speed up the 2151 * processing of the filesystem syncer process. A rushjob 2152 * value of N tells the filesystem syncer to process the next 2153 * N seconds worth of work on its queue ASAP. Currently rushjob 2154 * is used by the soft update code to speed up the filesystem 2155 * syncer process when the incore state is getting so far 2156 * ahead of the disk that the kernel memory pool is being 2157 * threatened with exhaustion. 2158 */ 2159 if (rushjob > 0) { 2160 rushjob -= 1; 2161 continue; 2162 } 2163 /* 2164 * Just sleep for a short period of time between 2165 * iterations when shutting down to allow some I/O 2166 * to happen. 2167 * 2168 * If it has taken us less than a second to process the 2169 * current work, then wait. Otherwise start right over 2170 * again. We can still lose time if any single round 2171 * takes more than two seconds, but it does not really 2172 * matter as we are just trying to generally pace the 2173 * filesystem activity. 2174 */ 2175 if (syncer_state != SYNCER_RUNNING || 2176 time_uptime == starttime) { 2177 thread_lock(td); 2178 sched_prio(td, PPAUSE); 2179 thread_unlock(td); 2180 } 2181 if (syncer_state != SYNCER_RUNNING) 2182 cv_timedwait(&sync_wakeup, &sync_mtx, 2183 hz / SYNCER_SHUTDOWN_SPEEDUP); 2184 else if (time_uptime == starttime) 2185 cv_timedwait(&sync_wakeup, &sync_mtx, hz); 2186 } 2187 } 2188 2189 /* 2190 * Request the syncer daemon to speed up its work. 2191 * We never push it to speed up more than half of its 2192 * normal turn time, otherwise it could take over the cpu. 2193 */ 2194 int 2195 speedup_syncer(void) 2196 { 2197 int ret = 0; 2198 2199 mtx_lock(&sync_mtx); 2200 if (rushjob < syncdelay / 2) { 2201 rushjob += 1; 2202 stat_rush_requests += 1; 2203 ret = 1; 2204 } 2205 mtx_unlock(&sync_mtx); 2206 cv_broadcast(&sync_wakeup); 2207 return (ret); 2208 } 2209 2210 /* 2211 * Tell the syncer to speed up its work and run though its work 2212 * list several times, then tell it to shut down. 2213 */ 2214 static void 2215 syncer_shutdown(void *arg, int howto) 2216 { 2217 2218 if (howto & RB_NOSYNC) 2219 return; 2220 mtx_lock(&sync_mtx); 2221 syncer_state = SYNCER_SHUTTING_DOWN; 2222 rushjob = 0; 2223 mtx_unlock(&sync_mtx); 2224 cv_broadcast(&sync_wakeup); 2225 kproc_shutdown(arg, howto); 2226 } 2227 2228 void 2229 syncer_suspend(void) 2230 { 2231 2232 syncer_shutdown(updateproc, 0); 2233 } 2234 2235 void 2236 syncer_resume(void) 2237 { 2238 2239 mtx_lock(&sync_mtx); 2240 first_printf = 1; 2241 syncer_state = SYNCER_RUNNING; 2242 mtx_unlock(&sync_mtx); 2243 cv_broadcast(&sync_wakeup); 2244 kproc_resume(updateproc); 2245 } 2246 2247 /* 2248 * Reassign a buffer from one vnode to another. 2249 * Used to assign file specific control information 2250 * (indirect blocks) to the vnode to which they belong. 2251 */ 2252 void 2253 reassignbuf(struct buf *bp) 2254 { 2255 struct vnode *vp; 2256 struct bufobj *bo; 2257 int delay; 2258 #ifdef INVARIANTS 2259 struct bufv *bv; 2260 #endif 2261 2262 vp = bp->b_vp; 2263 bo = bp->b_bufobj; 2264 ++reassignbufcalls; 2265 2266 CTR3(KTR_BUF, "reassignbuf(%p) vp %p flags %X", 2267 bp, bp->b_vp, bp->b_flags); 2268 /* 2269 * B_PAGING flagged buffers cannot be reassigned because their vp 2270 * is not fully linked in. 2271 */ 2272 if (bp->b_flags & B_PAGING) 2273 panic("cannot reassign paging buffer"); 2274 2275 /* 2276 * Delete from old vnode list, if on one. 2277 */ 2278 BO_LOCK(bo); 2279 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) 2280 buf_vlist_remove(bp); 2281 else 2282 panic("reassignbuf: Buffer %p not on queue.", bp); 2283 /* 2284 * If dirty, put on list of dirty buffers; otherwise insert onto list 2285 * of clean buffers. 2286 */ 2287 if (bp->b_flags & B_DELWRI) { 2288 if ((bo->bo_flag & BO_ONWORKLST) == 0) { 2289 switch (vp->v_type) { 2290 case VDIR: 2291 delay = dirdelay; 2292 break; 2293 case VCHR: 2294 delay = metadelay; 2295 break; 2296 default: 2297 delay = filedelay; 2298 } 2299 vn_syncer_add_to_worklist(bo, delay); 2300 } 2301 buf_vlist_add(bp, bo, BX_VNDIRTY); 2302 } else { 2303 buf_vlist_add(bp, bo, BX_VNCLEAN); 2304 2305 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) { 2306 mtx_lock(&sync_mtx); 2307 LIST_REMOVE(bo, bo_synclist); 2308 syncer_worklist_len--; 2309 mtx_unlock(&sync_mtx); 2310 bo->bo_flag &= ~BO_ONWORKLST; 2311 } 2312 } 2313 #ifdef INVARIANTS 2314 bv = &bo->bo_clean; 2315 bp = TAILQ_FIRST(&bv->bv_hd); 2316 KASSERT(bp == NULL || bp->b_bufobj == bo, 2317 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo)); 2318 bp = TAILQ_LAST(&bv->bv_hd, buflists); 2319 KASSERT(bp == NULL || bp->b_bufobj == bo, 2320 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo)); 2321 bv = &bo->bo_dirty; 2322 bp = TAILQ_FIRST(&bv->bv_hd); 2323 KASSERT(bp == NULL || bp->b_bufobj == bo, 2324 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo)); 2325 bp = TAILQ_LAST(&bv->bv_hd, buflists); 2326 KASSERT(bp == NULL || bp->b_bufobj == bo, 2327 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo)); 2328 #endif 2329 BO_UNLOCK(bo); 2330 } 2331 2332 /* 2333 * A temporary hack until refcount_* APIs are sorted out. 2334 */ 2335 static __inline int 2336 vfs_refcount_acquire_if_not_zero(volatile u_int *count) 2337 { 2338 u_int old; 2339 2340 for (;;) { 2341 old = *count; 2342 if (old == 0) 2343 return (0); 2344 if (atomic_cmpset_int(count, old, old + 1)) 2345 return (1); 2346 } 2347 } 2348 2349 static __inline int 2350 vfs_refcount_release_if_not_last(volatile u_int *count) 2351 { 2352 u_int old; 2353 2354 for (;;) { 2355 old = *count; 2356 if (old == 1) 2357 return (0); 2358 if (atomic_cmpset_int(count, old, old - 1)) 2359 return (1); 2360 } 2361 } 2362 2363 static void 2364 v_init_counters(struct vnode *vp) 2365 { 2366 2367 VNASSERT(vp->v_type == VNON && vp->v_data == NULL && vp->v_iflag == 0, 2368 vp, ("%s called for an initialized vnode", __FUNCTION__)); 2369 ASSERT_VI_UNLOCKED(vp, __FUNCTION__); 2370 2371 refcount_init(&vp->v_holdcnt, 1); 2372 refcount_init(&vp->v_usecount, 1); 2373 } 2374 2375 static void 2376 v_incr_usecount_locked(struct vnode *vp) 2377 { 2378 2379 ASSERT_VI_LOCKED(vp, __func__); 2380 if ((vp->v_iflag & VI_OWEINACT) != 0) { 2381 VNASSERT(vp->v_usecount == 0, vp, 2382 ("vnode with usecount and VI_OWEINACT set")); 2383 vp->v_iflag &= ~VI_OWEINACT; 2384 } 2385 refcount_acquire(&vp->v_usecount); 2386 v_incr_devcount(vp); 2387 } 2388 2389 /* 2390 * Increment the use and hold counts on the vnode, taking care to reference 2391 * the driver's usecount if this is a chardev. The _vhold() will remove 2392 * the vnode from the free list if it is presently free. 2393 */ 2394 static void 2395 v_incr_usecount(struct vnode *vp) 2396 { 2397 2398 ASSERT_VI_UNLOCKED(vp, __func__); 2399 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 2400 2401 if (vp->v_type != VCHR && 2402 vfs_refcount_acquire_if_not_zero(&vp->v_usecount)) { 2403 VNASSERT((vp->v_iflag & VI_OWEINACT) == 0, vp, 2404 ("vnode with usecount and VI_OWEINACT set")); 2405 } else { 2406 VI_LOCK(vp); 2407 v_incr_usecount_locked(vp); 2408 VI_UNLOCK(vp); 2409 } 2410 } 2411 2412 /* 2413 * Increment si_usecount of the associated device, if any. 2414 */ 2415 static void 2416 v_incr_devcount(struct vnode *vp) 2417 { 2418 2419 ASSERT_VI_LOCKED(vp, __FUNCTION__); 2420 if (vp->v_type == VCHR && vp->v_rdev != NULL) { 2421 dev_lock(); 2422 vp->v_rdev->si_usecount++; 2423 dev_unlock(); 2424 } 2425 } 2426 2427 /* 2428 * Decrement si_usecount of the associated device, if any. 2429 */ 2430 static void 2431 v_decr_devcount(struct vnode *vp) 2432 { 2433 2434 ASSERT_VI_LOCKED(vp, __FUNCTION__); 2435 if (vp->v_type == VCHR && vp->v_rdev != NULL) { 2436 dev_lock(); 2437 vp->v_rdev->si_usecount--; 2438 dev_unlock(); 2439 } 2440 } 2441 2442 /* 2443 * Grab a particular vnode from the free list, increment its 2444 * reference count and lock it. VI_DOOMED is set if the vnode 2445 * is being destroyed. Only callers who specify LK_RETRY will 2446 * see doomed vnodes. If inactive processing was delayed in 2447 * vput try to do it here. 2448 * 2449 * Notes on lockless counter manipulation: 2450 * _vhold, vputx and other routines make various decisions based 2451 * on either holdcnt or usecount being 0. As long as either contuner 2452 * is not transitioning 0->1 nor 1->0, the manipulation can be done 2453 * with atomic operations. Otherwise the interlock is taken. 2454 */ 2455 int 2456 vget(struct vnode *vp, int flags, struct thread *td) 2457 { 2458 int error, oweinact; 2459 2460 VNASSERT((flags & LK_TYPE_MASK) != 0, vp, 2461 ("vget: invalid lock operation")); 2462 2463 if ((flags & LK_INTERLOCK) != 0) 2464 ASSERT_VI_LOCKED(vp, __func__); 2465 else 2466 ASSERT_VI_UNLOCKED(vp, __func__); 2467 if ((flags & LK_VNHELD) != 0) 2468 VNASSERT((vp->v_holdcnt > 0), vp, 2469 ("vget: LK_VNHELD passed but vnode not held")); 2470 2471 CTR3(KTR_VFS, "%s: vp %p with flags %d", __func__, vp, flags); 2472 2473 if ((flags & LK_VNHELD) == 0) 2474 _vhold(vp, (flags & LK_INTERLOCK) != 0); 2475 2476 if ((error = vn_lock(vp, flags)) != 0) { 2477 vdrop(vp); 2478 CTR2(KTR_VFS, "%s: impossible to lock vnode %p", __func__, 2479 vp); 2480 return (error); 2481 } 2482 if (vp->v_iflag & VI_DOOMED && (flags & LK_RETRY) == 0) 2483 panic("vget: vn_lock failed to return ENOENT\n"); 2484 /* 2485 * We don't guarantee that any particular close will 2486 * trigger inactive processing so just make a best effort 2487 * here at preventing a reference to a removed file. If 2488 * we don't succeed no harm is done. 2489 * 2490 * Upgrade our holdcnt to a usecount. 2491 */ 2492 if (vp->v_type != VCHR && 2493 vfs_refcount_acquire_if_not_zero(&vp->v_usecount)) { 2494 VNASSERT((vp->v_iflag & VI_OWEINACT) == 0, vp, 2495 ("vnode with usecount and VI_OWEINACT set")); 2496 } else { 2497 VI_LOCK(vp); 2498 if ((vp->v_iflag & VI_OWEINACT) == 0) { 2499 oweinact = 0; 2500 } else { 2501 oweinact = 1; 2502 vp->v_iflag &= ~VI_OWEINACT; 2503 } 2504 refcount_acquire(&vp->v_usecount); 2505 v_incr_devcount(vp); 2506 if (oweinact && VOP_ISLOCKED(vp) == LK_EXCLUSIVE && 2507 (flags & LK_NOWAIT) == 0) 2508 vinactive(vp, td); 2509 VI_UNLOCK(vp); 2510 } 2511 return (0); 2512 } 2513 2514 /* 2515 * Increase the reference count of a vnode. 2516 */ 2517 void 2518 vref(struct vnode *vp) 2519 { 2520 2521 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 2522 _vhold(vp, false); 2523 v_incr_usecount(vp); 2524 } 2525 2526 void 2527 vrefl(struct vnode *vp) 2528 { 2529 2530 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 2531 _vhold(vp, true); 2532 v_incr_usecount_locked(vp); 2533 } 2534 2535 /* 2536 * Return reference count of a vnode. 2537 * 2538 * The results of this call are only guaranteed when some mechanism is used to 2539 * stop other processes from gaining references to the vnode. This may be the 2540 * case if the caller holds the only reference. This is also useful when stale 2541 * data is acceptable as race conditions may be accounted for by some other 2542 * means. 2543 */ 2544 int 2545 vrefcnt(struct vnode *vp) 2546 { 2547 2548 return (vp->v_usecount); 2549 } 2550 2551 #define VPUTX_VRELE 1 2552 #define VPUTX_VPUT 2 2553 #define VPUTX_VUNREF 3 2554 2555 /* 2556 * Decrement the use and hold counts for a vnode. 2557 * 2558 * See an explanation near vget() as to why atomic operation is safe. 2559 */ 2560 static void 2561 vputx(struct vnode *vp, int func) 2562 { 2563 int error; 2564 2565 KASSERT(vp != NULL, ("vputx: null vp")); 2566 if (func == VPUTX_VUNREF) 2567 ASSERT_VOP_LOCKED(vp, "vunref"); 2568 else if (func == VPUTX_VPUT) 2569 ASSERT_VOP_LOCKED(vp, "vput"); 2570 else 2571 KASSERT(func == VPUTX_VRELE, ("vputx: wrong func")); 2572 ASSERT_VI_UNLOCKED(vp, __func__); 2573 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 2574 2575 if (vp->v_type != VCHR && 2576 vfs_refcount_release_if_not_last(&vp->v_usecount)) { 2577 if (func == VPUTX_VPUT) 2578 VOP_UNLOCK(vp, 0); 2579 vdrop(vp); 2580 return; 2581 } 2582 2583 VI_LOCK(vp); 2584 2585 /* 2586 * We want to hold the vnode until the inactive finishes to 2587 * prevent vgone() races. We drop the use count here and the 2588 * hold count below when we're done. 2589 */ 2590 if (!refcount_release(&vp->v_usecount) || 2591 (vp->v_iflag & VI_DOINGINACT)) { 2592 if (func == VPUTX_VPUT) 2593 VOP_UNLOCK(vp, 0); 2594 v_decr_devcount(vp); 2595 vdropl(vp); 2596 return; 2597 } 2598 2599 v_decr_devcount(vp); 2600 2601 error = 0; 2602 2603 if (vp->v_usecount != 0) { 2604 vprint("vputx: usecount not zero", vp); 2605 panic("vputx: usecount not zero"); 2606 } 2607 2608 CTR2(KTR_VFS, "%s: return vnode %p to the freelist", __func__, vp); 2609 2610 /* 2611 * We must call VOP_INACTIVE with the node locked. Mark 2612 * as VI_DOINGINACT to avoid recursion. 2613 */ 2614 vp->v_iflag |= VI_OWEINACT; 2615 switch (func) { 2616 case VPUTX_VRELE: 2617 error = vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK); 2618 VI_LOCK(vp); 2619 break; 2620 case VPUTX_VPUT: 2621 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) { 2622 error = VOP_LOCK(vp, LK_UPGRADE | LK_INTERLOCK | 2623 LK_NOWAIT); 2624 VI_LOCK(vp); 2625 } 2626 break; 2627 case VPUTX_VUNREF: 2628 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) { 2629 error = VOP_LOCK(vp, LK_TRYUPGRADE | LK_INTERLOCK); 2630 VI_LOCK(vp); 2631 } 2632 break; 2633 } 2634 VNASSERT(vp->v_usecount == 0 || (vp->v_iflag & VI_OWEINACT) == 0, vp, 2635 ("vnode with usecount and VI_OWEINACT set")); 2636 if (error == 0) { 2637 if (vp->v_iflag & VI_OWEINACT) 2638 vinactive(vp, curthread); 2639 if (func != VPUTX_VUNREF) 2640 VOP_UNLOCK(vp, 0); 2641 } 2642 vdropl(vp); 2643 } 2644 2645 /* 2646 * Vnode put/release. 2647 * If count drops to zero, call inactive routine and return to freelist. 2648 */ 2649 void 2650 vrele(struct vnode *vp) 2651 { 2652 2653 vputx(vp, VPUTX_VRELE); 2654 } 2655 2656 /* 2657 * Release an already locked vnode. This give the same effects as 2658 * unlock+vrele(), but takes less time and avoids releasing and 2659 * re-aquiring the lock (as vrele() acquires the lock internally.) 2660 */ 2661 void 2662 vput(struct vnode *vp) 2663 { 2664 2665 vputx(vp, VPUTX_VPUT); 2666 } 2667 2668 /* 2669 * Release an exclusively locked vnode. Do not unlock the vnode lock. 2670 */ 2671 void 2672 vunref(struct vnode *vp) 2673 { 2674 2675 vputx(vp, VPUTX_VUNREF); 2676 } 2677 2678 /* 2679 * Increase the hold count and activate if this is the first reference. 2680 */ 2681 void 2682 _vhold(struct vnode *vp, bool locked) 2683 { 2684 struct mount *mp; 2685 2686 if (locked) 2687 ASSERT_VI_LOCKED(vp, __func__); 2688 else 2689 ASSERT_VI_UNLOCKED(vp, __func__); 2690 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 2691 if (!locked && vfs_refcount_acquire_if_not_zero(&vp->v_holdcnt)) { 2692 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp, 2693 ("_vhold: vnode with holdcnt is free")); 2694 return; 2695 } 2696 2697 if (!locked) 2698 VI_LOCK(vp); 2699 if ((vp->v_iflag & VI_FREE) == 0) { 2700 refcount_acquire(&vp->v_holdcnt); 2701 if (!locked) 2702 VI_UNLOCK(vp); 2703 return; 2704 } 2705 VNASSERT(vp->v_holdcnt == 0, vp, 2706 ("%s: wrong hold count", __func__)); 2707 VNASSERT(vp->v_op != NULL, vp, 2708 ("%s: vnode already reclaimed.", __func__)); 2709 /* 2710 * Remove a vnode from the free list, mark it as in use, 2711 * and put it on the active list. 2712 */ 2713 mtx_lock(&vnode_free_list_mtx); 2714 TAILQ_REMOVE(&vnode_free_list, vp, v_actfreelist); 2715 freevnodes--; 2716 vp->v_iflag &= ~VI_FREE; 2717 KASSERT((vp->v_iflag & VI_ACTIVE) == 0, 2718 ("Activating already active vnode")); 2719 vp->v_iflag |= VI_ACTIVE; 2720 mp = vp->v_mount; 2721 TAILQ_INSERT_HEAD(&mp->mnt_activevnodelist, vp, v_actfreelist); 2722 mp->mnt_activevnodelistsize++; 2723 mtx_unlock(&vnode_free_list_mtx); 2724 refcount_acquire(&vp->v_holdcnt); 2725 if (!locked) 2726 VI_UNLOCK(vp); 2727 } 2728 2729 /* 2730 * Drop the hold count of the vnode. If this is the last reference to 2731 * the vnode we place it on the free list unless it has been vgone'd 2732 * (marked VI_DOOMED) in which case we will free it. 2733 * 2734 * Because the vnode vm object keeps a hold reference on the vnode if 2735 * there is at least one resident non-cached page, the vnode cannot 2736 * leave the active list without the page cleanup done. 2737 */ 2738 void 2739 _vdrop(struct vnode *vp, bool locked) 2740 { 2741 struct bufobj *bo; 2742 struct mount *mp; 2743 int active; 2744 2745 if (locked) 2746 ASSERT_VI_LOCKED(vp, __func__); 2747 else 2748 ASSERT_VI_UNLOCKED(vp, __func__); 2749 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 2750 if ((int)vp->v_holdcnt <= 0) 2751 panic("vdrop: holdcnt %d", vp->v_holdcnt); 2752 if (vfs_refcount_release_if_not_last(&vp->v_holdcnt)) { 2753 if (locked) 2754 VI_UNLOCK(vp); 2755 return; 2756 } 2757 2758 if (!locked) 2759 VI_LOCK(vp); 2760 if (refcount_release(&vp->v_holdcnt) == 0) { 2761 VI_UNLOCK(vp); 2762 return; 2763 } 2764 if ((vp->v_iflag & VI_DOOMED) == 0) { 2765 /* 2766 * Mark a vnode as free: remove it from its active list 2767 * and put it up for recycling on the freelist. 2768 */ 2769 VNASSERT(vp->v_op != NULL, vp, 2770 ("vdropl: vnode already reclaimed.")); 2771 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp, 2772 ("vnode already free")); 2773 VNASSERT(vp->v_holdcnt == 0, vp, 2774 ("vdropl: freeing when we shouldn't")); 2775 active = vp->v_iflag & VI_ACTIVE; 2776 if ((vp->v_iflag & VI_OWEINACT) == 0) { 2777 vp->v_iflag &= ~VI_ACTIVE; 2778 mp = vp->v_mount; 2779 mtx_lock(&vnode_free_list_mtx); 2780 if (active) { 2781 TAILQ_REMOVE(&mp->mnt_activevnodelist, vp, 2782 v_actfreelist); 2783 mp->mnt_activevnodelistsize--; 2784 } 2785 TAILQ_INSERT_TAIL(&vnode_free_list, vp, 2786 v_actfreelist); 2787 freevnodes++; 2788 vp->v_iflag |= VI_FREE; 2789 mtx_unlock(&vnode_free_list_mtx); 2790 } else { 2791 atomic_add_long(&free_owe_inact, 1); 2792 } 2793 VI_UNLOCK(vp); 2794 return; 2795 } 2796 /* 2797 * The vnode has been marked for destruction, so free it. 2798 * 2799 * The vnode will be returned to the zone where it will 2800 * normally remain until it is needed for another vnode. We 2801 * need to cleanup (or verify that the cleanup has already 2802 * been done) any residual data left from its current use 2803 * so as not to contaminate the freshly allocated vnode. 2804 */ 2805 CTR2(KTR_VFS, "%s: destroying the vnode %p", __func__, vp); 2806 atomic_subtract_long(&numvnodes, 1); 2807 bo = &vp->v_bufobj; 2808 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp, 2809 ("cleaned vnode still on the free list.")); 2810 VNASSERT(vp->v_data == NULL, vp, ("cleaned vnode isn't")); 2811 VNASSERT(vp->v_holdcnt == 0, vp, ("Non-zero hold count")); 2812 VNASSERT(vp->v_usecount == 0, vp, ("Non-zero use count")); 2813 VNASSERT(vp->v_writecount == 0, vp, ("Non-zero write count")); 2814 VNASSERT(bo->bo_numoutput == 0, vp, ("Clean vnode has pending I/O's")); 2815 VNASSERT(bo->bo_clean.bv_cnt == 0, vp, ("cleanbufcnt not 0")); 2816 VNASSERT(pctrie_is_empty(&bo->bo_clean.bv_root), vp, 2817 ("clean blk trie not empty")); 2818 VNASSERT(bo->bo_dirty.bv_cnt == 0, vp, ("dirtybufcnt not 0")); 2819 VNASSERT(pctrie_is_empty(&bo->bo_dirty.bv_root), vp, 2820 ("dirty blk trie not empty")); 2821 VNASSERT(TAILQ_EMPTY(&vp->v_cache_dst), vp, ("vp has namecache dst")); 2822 VNASSERT(LIST_EMPTY(&vp->v_cache_src), vp, ("vp has namecache src")); 2823 VNASSERT(vp->v_cache_dd == NULL, vp, ("vp has namecache for ..")); 2824 VNASSERT(TAILQ_EMPTY(&vp->v_rl.rl_waiters), vp, 2825 ("Dangling rangelock waiters")); 2826 VI_UNLOCK(vp); 2827 #ifdef MAC 2828 mac_vnode_destroy(vp); 2829 #endif 2830 if (vp->v_pollinfo != NULL) { 2831 destroy_vpollinfo(vp->v_pollinfo); 2832 vp->v_pollinfo = NULL; 2833 } 2834 #ifdef INVARIANTS 2835 /* XXX Elsewhere we detect an already freed vnode via NULL v_op. */ 2836 vp->v_op = NULL; 2837 #endif 2838 bzero(&vp->v_un, sizeof(vp->v_un)); 2839 vp->v_lasta = vp->v_clen = vp->v_cstart = vp->v_lastw = 0; 2840 vp->v_iflag = 0; 2841 vp->v_vflag = 0; 2842 bo->bo_flag = 0; 2843 uma_zfree(vnode_zone, vp); 2844 } 2845 2846 /* 2847 * Call VOP_INACTIVE on the vnode and manage the DOINGINACT and OWEINACT 2848 * flags. DOINGINACT prevents us from recursing in calls to vinactive. 2849 * OWEINACT tracks whether a vnode missed a call to inactive due to a 2850 * failed lock upgrade. 2851 */ 2852 void 2853 vinactive(struct vnode *vp, struct thread *td) 2854 { 2855 struct vm_object *obj; 2856 2857 ASSERT_VOP_ELOCKED(vp, "vinactive"); 2858 ASSERT_VI_LOCKED(vp, "vinactive"); 2859 VNASSERT((vp->v_iflag & VI_DOINGINACT) == 0, vp, 2860 ("vinactive: recursed on VI_DOINGINACT")); 2861 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 2862 vp->v_iflag |= VI_DOINGINACT; 2863 vp->v_iflag &= ~VI_OWEINACT; 2864 VI_UNLOCK(vp); 2865 /* 2866 * Before moving off the active list, we must be sure that any 2867 * modified pages are converted into the vnode's dirty 2868 * buffers, since these will no longer be checked once the 2869 * vnode is on the inactive list. 2870 * 2871 * The write-out of the dirty pages is asynchronous. At the 2872 * point that VOP_INACTIVE() is called, there could still be 2873 * pending I/O and dirty pages in the object. 2874 */ 2875 obj = vp->v_object; 2876 if (obj != NULL && (obj->flags & OBJ_MIGHTBEDIRTY) != 0) { 2877 VM_OBJECT_WLOCK(obj); 2878 vm_object_page_clean(obj, 0, 0, OBJPC_NOSYNC); 2879 VM_OBJECT_WUNLOCK(obj); 2880 } 2881 VOP_INACTIVE(vp, td); 2882 VI_LOCK(vp); 2883 VNASSERT(vp->v_iflag & VI_DOINGINACT, vp, 2884 ("vinactive: lost VI_DOINGINACT")); 2885 vp->v_iflag &= ~VI_DOINGINACT; 2886 } 2887 2888 /* 2889 * Remove any vnodes in the vnode table belonging to mount point mp. 2890 * 2891 * If FORCECLOSE is not specified, there should not be any active ones, 2892 * return error if any are found (nb: this is a user error, not a 2893 * system error). If FORCECLOSE is specified, detach any active vnodes 2894 * that are found. 2895 * 2896 * If WRITECLOSE is set, only flush out regular file vnodes open for 2897 * writing. 2898 * 2899 * SKIPSYSTEM causes any vnodes marked VV_SYSTEM to be skipped. 2900 * 2901 * `rootrefs' specifies the base reference count for the root vnode 2902 * of this filesystem. The root vnode is considered busy if its 2903 * v_usecount exceeds this value. On a successful return, vflush(, td) 2904 * will call vrele() on the root vnode exactly rootrefs times. 2905 * If the SKIPSYSTEM or WRITECLOSE flags are specified, rootrefs must 2906 * be zero. 2907 */ 2908 #ifdef DIAGNOSTIC 2909 static int busyprt = 0; /* print out busy vnodes */ 2910 SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, "Print out busy vnodes"); 2911 #endif 2912 2913 int 2914 vflush(struct mount *mp, int rootrefs, int flags, struct thread *td) 2915 { 2916 struct vnode *vp, *mvp, *rootvp = NULL; 2917 struct vattr vattr; 2918 int busy = 0, error; 2919 2920 CTR4(KTR_VFS, "%s: mp %p with rootrefs %d and flags %d", __func__, mp, 2921 rootrefs, flags); 2922 if (rootrefs > 0) { 2923 KASSERT((flags & (SKIPSYSTEM | WRITECLOSE)) == 0, 2924 ("vflush: bad args")); 2925 /* 2926 * Get the filesystem root vnode. We can vput() it 2927 * immediately, since with rootrefs > 0, it won't go away. 2928 */ 2929 if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rootvp)) != 0) { 2930 CTR2(KTR_VFS, "%s: vfs_root lookup failed with %d", 2931 __func__, error); 2932 return (error); 2933 } 2934 vput(rootvp); 2935 } 2936 loop: 2937 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) { 2938 vholdl(vp); 2939 error = vn_lock(vp, LK_INTERLOCK | LK_EXCLUSIVE); 2940 if (error) { 2941 vdrop(vp); 2942 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp); 2943 goto loop; 2944 } 2945 /* 2946 * Skip over a vnodes marked VV_SYSTEM. 2947 */ 2948 if ((flags & SKIPSYSTEM) && (vp->v_vflag & VV_SYSTEM)) { 2949 VOP_UNLOCK(vp, 0); 2950 vdrop(vp); 2951 continue; 2952 } 2953 /* 2954 * If WRITECLOSE is set, flush out unlinked but still open 2955 * files (even if open only for reading) and regular file 2956 * vnodes open for writing. 2957 */ 2958 if (flags & WRITECLOSE) { 2959 if (vp->v_object != NULL) { 2960 VM_OBJECT_WLOCK(vp->v_object); 2961 vm_object_page_clean(vp->v_object, 0, 0, 0); 2962 VM_OBJECT_WUNLOCK(vp->v_object); 2963 } 2964 error = VOP_FSYNC(vp, MNT_WAIT, td); 2965 if (error != 0) { 2966 VOP_UNLOCK(vp, 0); 2967 vdrop(vp); 2968 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp); 2969 return (error); 2970 } 2971 error = VOP_GETATTR(vp, &vattr, td->td_ucred); 2972 VI_LOCK(vp); 2973 2974 if ((vp->v_type == VNON || 2975 (error == 0 && vattr.va_nlink > 0)) && 2976 (vp->v_writecount == 0 || vp->v_type != VREG)) { 2977 VOP_UNLOCK(vp, 0); 2978 vdropl(vp); 2979 continue; 2980 } 2981 } else 2982 VI_LOCK(vp); 2983 /* 2984 * With v_usecount == 0, all we need to do is clear out the 2985 * vnode data structures and we are done. 2986 * 2987 * If FORCECLOSE is set, forcibly close the vnode. 2988 */ 2989 if (vp->v_usecount == 0 || (flags & FORCECLOSE)) { 2990 vgonel(vp); 2991 } else { 2992 busy++; 2993 #ifdef DIAGNOSTIC 2994 if (busyprt) 2995 vprint("vflush: busy vnode", vp); 2996 #endif 2997 } 2998 VOP_UNLOCK(vp, 0); 2999 vdropl(vp); 3000 } 3001 if (rootrefs > 0 && (flags & FORCECLOSE) == 0) { 3002 /* 3003 * If just the root vnode is busy, and if its refcount 3004 * is equal to `rootrefs', then go ahead and kill it. 3005 */ 3006 VI_LOCK(rootvp); 3007 KASSERT(busy > 0, ("vflush: not busy")); 3008 VNASSERT(rootvp->v_usecount >= rootrefs, rootvp, 3009 ("vflush: usecount %d < rootrefs %d", 3010 rootvp->v_usecount, rootrefs)); 3011 if (busy == 1 && rootvp->v_usecount == rootrefs) { 3012 VOP_LOCK(rootvp, LK_EXCLUSIVE|LK_INTERLOCK); 3013 vgone(rootvp); 3014 VOP_UNLOCK(rootvp, 0); 3015 busy = 0; 3016 } else 3017 VI_UNLOCK(rootvp); 3018 } 3019 if (busy) { 3020 CTR2(KTR_VFS, "%s: failing as %d vnodes are busy", __func__, 3021 busy); 3022 return (EBUSY); 3023 } 3024 for (; rootrefs > 0; rootrefs--) 3025 vrele(rootvp); 3026 return (0); 3027 } 3028 3029 /* 3030 * Recycle an unused vnode to the front of the free list. 3031 */ 3032 int 3033 vrecycle(struct vnode *vp) 3034 { 3035 int recycled; 3036 3037 ASSERT_VOP_ELOCKED(vp, "vrecycle"); 3038 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 3039 recycled = 0; 3040 VI_LOCK(vp); 3041 if (vp->v_usecount == 0) { 3042 recycled = 1; 3043 vgonel(vp); 3044 } 3045 VI_UNLOCK(vp); 3046 return (recycled); 3047 } 3048 3049 /* 3050 * Eliminate all activity associated with a vnode 3051 * in preparation for reuse. 3052 */ 3053 void 3054 vgone(struct vnode *vp) 3055 { 3056 VI_LOCK(vp); 3057 vgonel(vp); 3058 VI_UNLOCK(vp); 3059 } 3060 3061 static void 3062 notify_lowervp_vfs_dummy(struct mount *mp __unused, 3063 struct vnode *lowervp __unused) 3064 { 3065 } 3066 3067 /* 3068 * Notify upper mounts about reclaimed or unlinked vnode. 3069 */ 3070 void 3071 vfs_notify_upper(struct vnode *vp, int event) 3072 { 3073 static struct vfsops vgonel_vfsops = { 3074 .vfs_reclaim_lowervp = notify_lowervp_vfs_dummy, 3075 .vfs_unlink_lowervp = notify_lowervp_vfs_dummy, 3076 }; 3077 struct mount *mp, *ump, *mmp; 3078 3079 mp = vp->v_mount; 3080 if (mp == NULL) 3081 return; 3082 3083 MNT_ILOCK(mp); 3084 if (TAILQ_EMPTY(&mp->mnt_uppers)) 3085 goto unlock; 3086 MNT_IUNLOCK(mp); 3087 mmp = malloc(sizeof(struct mount), M_TEMP, M_WAITOK | M_ZERO); 3088 mmp->mnt_op = &vgonel_vfsops; 3089 mmp->mnt_kern_flag |= MNTK_MARKER; 3090 MNT_ILOCK(mp); 3091 mp->mnt_kern_flag |= MNTK_VGONE_UPPER; 3092 for (ump = TAILQ_FIRST(&mp->mnt_uppers); ump != NULL;) { 3093 if ((ump->mnt_kern_flag & MNTK_MARKER) != 0) { 3094 ump = TAILQ_NEXT(ump, mnt_upper_link); 3095 continue; 3096 } 3097 TAILQ_INSERT_AFTER(&mp->mnt_uppers, ump, mmp, mnt_upper_link); 3098 MNT_IUNLOCK(mp); 3099 switch (event) { 3100 case VFS_NOTIFY_UPPER_RECLAIM: 3101 VFS_RECLAIM_LOWERVP(ump, vp); 3102 break; 3103 case VFS_NOTIFY_UPPER_UNLINK: 3104 VFS_UNLINK_LOWERVP(ump, vp); 3105 break; 3106 default: 3107 KASSERT(0, ("invalid event %d", event)); 3108 break; 3109 } 3110 MNT_ILOCK(mp); 3111 ump = TAILQ_NEXT(mmp, mnt_upper_link); 3112 TAILQ_REMOVE(&mp->mnt_uppers, mmp, mnt_upper_link); 3113 } 3114 free(mmp, M_TEMP); 3115 mp->mnt_kern_flag &= ~MNTK_VGONE_UPPER; 3116 if ((mp->mnt_kern_flag & MNTK_VGONE_WAITER) != 0) { 3117 mp->mnt_kern_flag &= ~MNTK_VGONE_WAITER; 3118 wakeup(&mp->mnt_uppers); 3119 } 3120 unlock: 3121 MNT_IUNLOCK(mp); 3122 } 3123 3124 /* 3125 * vgone, with the vp interlock held. 3126 */ 3127 static void 3128 vgonel(struct vnode *vp) 3129 { 3130 struct thread *td; 3131 int oweinact; 3132 int active; 3133 struct mount *mp; 3134 3135 ASSERT_VOP_ELOCKED(vp, "vgonel"); 3136 ASSERT_VI_LOCKED(vp, "vgonel"); 3137 VNASSERT(vp->v_holdcnt, vp, 3138 ("vgonel: vp %p has no reference.", vp)); 3139 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 3140 td = curthread; 3141 3142 /* 3143 * Don't vgonel if we're already doomed. 3144 */ 3145 if (vp->v_iflag & VI_DOOMED) 3146 return; 3147 vp->v_iflag |= VI_DOOMED; 3148 3149 /* 3150 * Check to see if the vnode is in use. If so, we have to call 3151 * VOP_CLOSE() and VOP_INACTIVE(). 3152 */ 3153 active = vp->v_usecount; 3154 oweinact = (vp->v_iflag & VI_OWEINACT); 3155 VI_UNLOCK(vp); 3156 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_RECLAIM); 3157 3158 /* 3159 * If purging an active vnode, it must be closed and 3160 * deactivated before being reclaimed. 3161 */ 3162 if (active) 3163 VOP_CLOSE(vp, FNONBLOCK, NOCRED, td); 3164 if (oweinact || active) { 3165 VI_LOCK(vp); 3166 if ((vp->v_iflag & VI_DOINGINACT) == 0) 3167 vinactive(vp, td); 3168 VI_UNLOCK(vp); 3169 } 3170 if (vp->v_type == VSOCK) 3171 vfs_unp_reclaim(vp); 3172 3173 /* 3174 * Clean out any buffers associated with the vnode. 3175 * If the flush fails, just toss the buffers. 3176 */ 3177 mp = NULL; 3178 if (!TAILQ_EMPTY(&vp->v_bufobj.bo_dirty.bv_hd)) 3179 (void) vn_start_secondary_write(vp, &mp, V_WAIT); 3180 if (vinvalbuf(vp, V_SAVE, 0, 0) != 0) { 3181 while (vinvalbuf(vp, 0, 0, 0) != 0) 3182 ; 3183 } 3184 3185 BO_LOCK(&vp->v_bufobj); 3186 KASSERT(TAILQ_EMPTY(&vp->v_bufobj.bo_dirty.bv_hd) && 3187 vp->v_bufobj.bo_dirty.bv_cnt == 0 && 3188 TAILQ_EMPTY(&vp->v_bufobj.bo_clean.bv_hd) && 3189 vp->v_bufobj.bo_clean.bv_cnt == 0, 3190 ("vp %p bufobj not invalidated", vp)); 3191 vp->v_bufobj.bo_flag |= BO_DEAD; 3192 BO_UNLOCK(&vp->v_bufobj); 3193 3194 /* 3195 * Reclaim the vnode. 3196 */ 3197 if (VOP_RECLAIM(vp, td)) 3198 panic("vgone: cannot reclaim"); 3199 if (mp != NULL) 3200 vn_finished_secondary_write(mp); 3201 VNASSERT(vp->v_object == NULL, vp, 3202 ("vop_reclaim left v_object vp=%p, tag=%s", vp, vp->v_tag)); 3203 /* 3204 * Clear the advisory locks and wake up waiting threads. 3205 */ 3206 (void)VOP_ADVLOCKPURGE(vp); 3207 vp->v_lockf = NULL; 3208 /* 3209 * Delete from old mount point vnode list. 3210 */ 3211 delmntque(vp); 3212 cache_purge(vp); 3213 /* 3214 * Done with purge, reset to the standard lock and invalidate 3215 * the vnode. 3216 */ 3217 VI_LOCK(vp); 3218 vp->v_vnlock = &vp->v_lock; 3219 vp->v_op = &dead_vnodeops; 3220 vp->v_tag = "none"; 3221 vp->v_type = VBAD; 3222 } 3223 3224 /* 3225 * Calculate the total number of references to a special device. 3226 */ 3227 int 3228 vcount(struct vnode *vp) 3229 { 3230 int count; 3231 3232 dev_lock(); 3233 count = vp->v_rdev->si_usecount; 3234 dev_unlock(); 3235 return (count); 3236 } 3237 3238 /* 3239 * Same as above, but using the struct cdev *as argument 3240 */ 3241 int 3242 count_dev(struct cdev *dev) 3243 { 3244 int count; 3245 3246 dev_lock(); 3247 count = dev->si_usecount; 3248 dev_unlock(); 3249 return(count); 3250 } 3251 3252 /* 3253 * Print out a description of a vnode. 3254 */ 3255 static char *typename[] = 3256 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD", 3257 "VMARKER"}; 3258 3259 void 3260 vn_printf(struct vnode *vp, const char *fmt, ...) 3261 { 3262 va_list ap; 3263 char buf[256], buf2[16]; 3264 u_long flags; 3265 3266 va_start(ap, fmt); 3267 vprintf(fmt, ap); 3268 va_end(ap); 3269 printf("%p: ", (void *)vp); 3270 printf("tag %s, type %s\n", vp->v_tag, typename[vp->v_type]); 3271 printf(" usecount %d, writecount %d, refcount %d mountedhere %p\n", 3272 vp->v_usecount, vp->v_writecount, vp->v_holdcnt, vp->v_mountedhere); 3273 buf[0] = '\0'; 3274 buf[1] = '\0'; 3275 if (vp->v_vflag & VV_ROOT) 3276 strlcat(buf, "|VV_ROOT", sizeof(buf)); 3277 if (vp->v_vflag & VV_ISTTY) 3278 strlcat(buf, "|VV_ISTTY", sizeof(buf)); 3279 if (vp->v_vflag & VV_NOSYNC) 3280 strlcat(buf, "|VV_NOSYNC", sizeof(buf)); 3281 if (vp->v_vflag & VV_ETERNALDEV) 3282 strlcat(buf, "|VV_ETERNALDEV", sizeof(buf)); 3283 if (vp->v_vflag & VV_CACHEDLABEL) 3284 strlcat(buf, "|VV_CACHEDLABEL", sizeof(buf)); 3285 if (vp->v_vflag & VV_TEXT) 3286 strlcat(buf, "|VV_TEXT", sizeof(buf)); 3287 if (vp->v_vflag & VV_COPYONWRITE) 3288 strlcat(buf, "|VV_COPYONWRITE", sizeof(buf)); 3289 if (vp->v_vflag & VV_SYSTEM) 3290 strlcat(buf, "|VV_SYSTEM", sizeof(buf)); 3291 if (vp->v_vflag & VV_PROCDEP) 3292 strlcat(buf, "|VV_PROCDEP", sizeof(buf)); 3293 if (vp->v_vflag & VV_NOKNOTE) 3294 strlcat(buf, "|VV_NOKNOTE", sizeof(buf)); 3295 if (vp->v_vflag & VV_DELETED) 3296 strlcat(buf, "|VV_DELETED", sizeof(buf)); 3297 if (vp->v_vflag & VV_MD) 3298 strlcat(buf, "|VV_MD", sizeof(buf)); 3299 if (vp->v_vflag & VV_FORCEINSMQ) 3300 strlcat(buf, "|VV_FORCEINSMQ", sizeof(buf)); 3301 flags = vp->v_vflag & ~(VV_ROOT | VV_ISTTY | VV_NOSYNC | VV_ETERNALDEV | 3302 VV_CACHEDLABEL | VV_TEXT | VV_COPYONWRITE | VV_SYSTEM | VV_PROCDEP | 3303 VV_NOKNOTE | VV_DELETED | VV_MD | VV_FORCEINSMQ); 3304 if (flags != 0) { 3305 snprintf(buf2, sizeof(buf2), "|VV(0x%lx)", flags); 3306 strlcat(buf, buf2, sizeof(buf)); 3307 } 3308 if (vp->v_iflag & VI_MOUNT) 3309 strlcat(buf, "|VI_MOUNT", sizeof(buf)); 3310 if (vp->v_iflag & VI_DOOMED) 3311 strlcat(buf, "|VI_DOOMED", sizeof(buf)); 3312 if (vp->v_iflag & VI_FREE) 3313 strlcat(buf, "|VI_FREE", sizeof(buf)); 3314 if (vp->v_iflag & VI_ACTIVE) 3315 strlcat(buf, "|VI_ACTIVE", sizeof(buf)); 3316 if (vp->v_iflag & VI_DOINGINACT) 3317 strlcat(buf, "|VI_DOINGINACT", sizeof(buf)); 3318 if (vp->v_iflag & VI_OWEINACT) 3319 strlcat(buf, "|VI_OWEINACT", sizeof(buf)); 3320 flags = vp->v_iflag & ~(VI_MOUNT | VI_DOOMED | VI_FREE | 3321 VI_ACTIVE | VI_DOINGINACT | VI_OWEINACT); 3322 if (flags != 0) { 3323 snprintf(buf2, sizeof(buf2), "|VI(0x%lx)", flags); 3324 strlcat(buf, buf2, sizeof(buf)); 3325 } 3326 printf(" flags (%s)\n", buf + 1); 3327 if (mtx_owned(VI_MTX(vp))) 3328 printf(" VI_LOCKed"); 3329 if (vp->v_object != NULL) 3330 printf(" v_object %p ref %d pages %d " 3331 "cleanbuf %d dirtybuf %d\n", 3332 vp->v_object, vp->v_object->ref_count, 3333 vp->v_object->resident_page_count, 3334 vp->v_bufobj.bo_clean.bv_cnt, 3335 vp->v_bufobj.bo_dirty.bv_cnt); 3336 printf(" "); 3337 lockmgr_printinfo(vp->v_vnlock); 3338 if (vp->v_data != NULL) 3339 VOP_PRINT(vp); 3340 } 3341 3342 #ifdef DDB 3343 /* 3344 * List all of the locked vnodes in the system. 3345 * Called when debugging the kernel. 3346 */ 3347 DB_SHOW_COMMAND(lockedvnods, lockedvnodes) 3348 { 3349 struct mount *mp; 3350 struct vnode *vp; 3351 3352 /* 3353 * Note: because this is DDB, we can't obey the locking semantics 3354 * for these structures, which means we could catch an inconsistent 3355 * state and dereference a nasty pointer. Not much to be done 3356 * about that. 3357 */ 3358 db_printf("Locked vnodes\n"); 3359 TAILQ_FOREACH(mp, &mountlist, mnt_list) { 3360 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) { 3361 if (vp->v_type != VMARKER && VOP_ISLOCKED(vp)) 3362 vprint("", vp); 3363 } 3364 } 3365 } 3366 3367 /* 3368 * Show details about the given vnode. 3369 */ 3370 DB_SHOW_COMMAND(vnode, db_show_vnode) 3371 { 3372 struct vnode *vp; 3373 3374 if (!have_addr) 3375 return; 3376 vp = (struct vnode *)addr; 3377 vn_printf(vp, "vnode "); 3378 } 3379 3380 /* 3381 * Show details about the given mount point. 3382 */ 3383 DB_SHOW_COMMAND(mount, db_show_mount) 3384 { 3385 struct mount *mp; 3386 struct vfsopt *opt; 3387 struct statfs *sp; 3388 struct vnode *vp; 3389 char buf[512]; 3390 uint64_t mflags; 3391 u_int flags; 3392 3393 if (!have_addr) { 3394 /* No address given, print short info about all mount points. */ 3395 TAILQ_FOREACH(mp, &mountlist, mnt_list) { 3396 db_printf("%p %s on %s (%s)\n", mp, 3397 mp->mnt_stat.f_mntfromname, 3398 mp->mnt_stat.f_mntonname, 3399 mp->mnt_stat.f_fstypename); 3400 if (db_pager_quit) 3401 break; 3402 } 3403 db_printf("\nMore info: show mount <addr>\n"); 3404 return; 3405 } 3406 3407 mp = (struct mount *)addr; 3408 db_printf("%p %s on %s (%s)\n", mp, mp->mnt_stat.f_mntfromname, 3409 mp->mnt_stat.f_mntonname, mp->mnt_stat.f_fstypename); 3410 3411 buf[0] = '\0'; 3412 mflags = mp->mnt_flag; 3413 #define MNT_FLAG(flag) do { \ 3414 if (mflags & (flag)) { \ 3415 if (buf[0] != '\0') \ 3416 strlcat(buf, ", ", sizeof(buf)); \ 3417 strlcat(buf, (#flag) + 4, sizeof(buf)); \ 3418 mflags &= ~(flag); \ 3419 } \ 3420 } while (0) 3421 MNT_FLAG(MNT_RDONLY); 3422 MNT_FLAG(MNT_SYNCHRONOUS); 3423 MNT_FLAG(MNT_NOEXEC); 3424 MNT_FLAG(MNT_NOSUID); 3425 MNT_FLAG(MNT_NFS4ACLS); 3426 MNT_FLAG(MNT_UNION); 3427 MNT_FLAG(MNT_ASYNC); 3428 MNT_FLAG(MNT_SUIDDIR); 3429 MNT_FLAG(MNT_SOFTDEP); 3430 MNT_FLAG(MNT_NOSYMFOLLOW); 3431 MNT_FLAG(MNT_GJOURNAL); 3432 MNT_FLAG(MNT_MULTILABEL); 3433 MNT_FLAG(MNT_ACLS); 3434 MNT_FLAG(MNT_NOATIME); 3435 MNT_FLAG(MNT_NOCLUSTERR); 3436 MNT_FLAG(MNT_NOCLUSTERW); 3437 MNT_FLAG(MNT_SUJ); 3438 MNT_FLAG(MNT_EXRDONLY); 3439 MNT_FLAG(MNT_EXPORTED); 3440 MNT_FLAG(MNT_DEFEXPORTED); 3441 MNT_FLAG(MNT_EXPORTANON); 3442 MNT_FLAG(MNT_EXKERB); 3443 MNT_FLAG(MNT_EXPUBLIC); 3444 MNT_FLAG(MNT_LOCAL); 3445 MNT_FLAG(MNT_QUOTA); 3446 MNT_FLAG(MNT_ROOTFS); 3447 MNT_FLAG(MNT_USER); 3448 MNT_FLAG(MNT_IGNORE); 3449 MNT_FLAG(MNT_UPDATE); 3450 MNT_FLAG(MNT_DELEXPORT); 3451 MNT_FLAG(MNT_RELOAD); 3452 MNT_FLAG(MNT_FORCE); 3453 MNT_FLAG(MNT_SNAPSHOT); 3454 MNT_FLAG(MNT_BYFSID); 3455 #undef MNT_FLAG 3456 if (mflags != 0) { 3457 if (buf[0] != '\0') 3458 strlcat(buf, ", ", sizeof(buf)); 3459 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), 3460 "0x%016jx", mflags); 3461 } 3462 db_printf(" mnt_flag = %s\n", buf); 3463 3464 buf[0] = '\0'; 3465 flags = mp->mnt_kern_flag; 3466 #define MNT_KERN_FLAG(flag) do { \ 3467 if (flags & (flag)) { \ 3468 if (buf[0] != '\0') \ 3469 strlcat(buf, ", ", sizeof(buf)); \ 3470 strlcat(buf, (#flag) + 5, sizeof(buf)); \ 3471 flags &= ~(flag); \ 3472 } \ 3473 } while (0) 3474 MNT_KERN_FLAG(MNTK_UNMOUNTF); 3475 MNT_KERN_FLAG(MNTK_ASYNC); 3476 MNT_KERN_FLAG(MNTK_SOFTDEP); 3477 MNT_KERN_FLAG(MNTK_NOINSMNTQ); 3478 MNT_KERN_FLAG(MNTK_DRAINING); 3479 MNT_KERN_FLAG(MNTK_REFEXPIRE); 3480 MNT_KERN_FLAG(MNTK_EXTENDED_SHARED); 3481 MNT_KERN_FLAG(MNTK_SHARED_WRITES); 3482 MNT_KERN_FLAG(MNTK_NO_IOPF); 3483 MNT_KERN_FLAG(MNTK_VGONE_UPPER); 3484 MNT_KERN_FLAG(MNTK_VGONE_WAITER); 3485 MNT_KERN_FLAG(MNTK_LOOKUP_EXCL_DOTDOT); 3486 MNT_KERN_FLAG(MNTK_MARKER); 3487 MNT_KERN_FLAG(MNTK_USES_BCACHE); 3488 MNT_KERN_FLAG(MNTK_NOASYNC); 3489 MNT_KERN_FLAG(MNTK_UNMOUNT); 3490 MNT_KERN_FLAG(MNTK_MWAIT); 3491 MNT_KERN_FLAG(MNTK_SUSPEND); 3492 MNT_KERN_FLAG(MNTK_SUSPEND2); 3493 MNT_KERN_FLAG(MNTK_SUSPENDED); 3494 MNT_KERN_FLAG(MNTK_LOOKUP_SHARED); 3495 MNT_KERN_FLAG(MNTK_NOKNOTE); 3496 #undef MNT_KERN_FLAG 3497 if (flags != 0) { 3498 if (buf[0] != '\0') 3499 strlcat(buf, ", ", sizeof(buf)); 3500 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), 3501 "0x%08x", flags); 3502 } 3503 db_printf(" mnt_kern_flag = %s\n", buf); 3504 3505 db_printf(" mnt_opt = "); 3506 opt = TAILQ_FIRST(mp->mnt_opt); 3507 if (opt != NULL) { 3508 db_printf("%s", opt->name); 3509 opt = TAILQ_NEXT(opt, link); 3510 while (opt != NULL) { 3511 db_printf(", %s", opt->name); 3512 opt = TAILQ_NEXT(opt, link); 3513 } 3514 } 3515 db_printf("\n"); 3516 3517 sp = &mp->mnt_stat; 3518 db_printf(" mnt_stat = { version=%u type=%u flags=0x%016jx " 3519 "bsize=%ju iosize=%ju blocks=%ju bfree=%ju bavail=%jd files=%ju " 3520 "ffree=%jd syncwrites=%ju asyncwrites=%ju syncreads=%ju " 3521 "asyncreads=%ju namemax=%u owner=%u fsid=[%d, %d] }\n", 3522 (u_int)sp->f_version, (u_int)sp->f_type, (uintmax_t)sp->f_flags, 3523 (uintmax_t)sp->f_bsize, (uintmax_t)sp->f_iosize, 3524 (uintmax_t)sp->f_blocks, (uintmax_t)sp->f_bfree, 3525 (intmax_t)sp->f_bavail, (uintmax_t)sp->f_files, 3526 (intmax_t)sp->f_ffree, (uintmax_t)sp->f_syncwrites, 3527 (uintmax_t)sp->f_asyncwrites, (uintmax_t)sp->f_syncreads, 3528 (uintmax_t)sp->f_asyncreads, (u_int)sp->f_namemax, 3529 (u_int)sp->f_owner, (int)sp->f_fsid.val[0], (int)sp->f_fsid.val[1]); 3530 3531 db_printf(" mnt_cred = { uid=%u ruid=%u", 3532 (u_int)mp->mnt_cred->cr_uid, (u_int)mp->mnt_cred->cr_ruid); 3533 if (jailed(mp->mnt_cred)) 3534 db_printf(", jail=%d", mp->mnt_cred->cr_prison->pr_id); 3535 db_printf(" }\n"); 3536 db_printf(" mnt_ref = %d\n", mp->mnt_ref); 3537 db_printf(" mnt_gen = %d\n", mp->mnt_gen); 3538 db_printf(" mnt_nvnodelistsize = %d\n", mp->mnt_nvnodelistsize); 3539 db_printf(" mnt_activevnodelistsize = %d\n", 3540 mp->mnt_activevnodelistsize); 3541 db_printf(" mnt_writeopcount = %d\n", mp->mnt_writeopcount); 3542 db_printf(" mnt_maxsymlinklen = %d\n", mp->mnt_maxsymlinklen); 3543 db_printf(" mnt_iosize_max = %d\n", mp->mnt_iosize_max); 3544 db_printf(" mnt_hashseed = %u\n", mp->mnt_hashseed); 3545 db_printf(" mnt_lockref = %d\n", mp->mnt_lockref); 3546 db_printf(" mnt_secondary_writes = %d\n", mp->mnt_secondary_writes); 3547 db_printf(" mnt_secondary_accwrites = %d\n", 3548 mp->mnt_secondary_accwrites); 3549 db_printf(" mnt_gjprovider = %s\n", 3550 mp->mnt_gjprovider != NULL ? mp->mnt_gjprovider : "NULL"); 3551 3552 db_printf("\n\nList of active vnodes\n"); 3553 TAILQ_FOREACH(vp, &mp->mnt_activevnodelist, v_actfreelist) { 3554 if (vp->v_type != VMARKER) { 3555 vn_printf(vp, "vnode "); 3556 if (db_pager_quit) 3557 break; 3558 } 3559 } 3560 db_printf("\n\nList of inactive vnodes\n"); 3561 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) { 3562 if (vp->v_type != VMARKER && (vp->v_iflag & VI_ACTIVE) == 0) { 3563 vn_printf(vp, "vnode "); 3564 if (db_pager_quit) 3565 break; 3566 } 3567 } 3568 } 3569 #endif /* DDB */ 3570 3571 /* 3572 * Fill in a struct xvfsconf based on a struct vfsconf. 3573 */ 3574 static int 3575 vfsconf2x(struct sysctl_req *req, struct vfsconf *vfsp) 3576 { 3577 struct xvfsconf xvfsp; 3578 3579 bzero(&xvfsp, sizeof(xvfsp)); 3580 strcpy(xvfsp.vfc_name, vfsp->vfc_name); 3581 xvfsp.vfc_typenum = vfsp->vfc_typenum; 3582 xvfsp.vfc_refcount = vfsp->vfc_refcount; 3583 xvfsp.vfc_flags = vfsp->vfc_flags; 3584 /* 3585 * These are unused in userland, we keep them 3586 * to not break binary compatibility. 3587 */ 3588 xvfsp.vfc_vfsops = NULL; 3589 xvfsp.vfc_next = NULL; 3590 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp))); 3591 } 3592 3593 #ifdef COMPAT_FREEBSD32 3594 struct xvfsconf32 { 3595 uint32_t vfc_vfsops; 3596 char vfc_name[MFSNAMELEN]; 3597 int32_t vfc_typenum; 3598 int32_t vfc_refcount; 3599 int32_t vfc_flags; 3600 uint32_t vfc_next; 3601 }; 3602 3603 static int 3604 vfsconf2x32(struct sysctl_req *req, struct vfsconf *vfsp) 3605 { 3606 struct xvfsconf32 xvfsp; 3607 3608 strcpy(xvfsp.vfc_name, vfsp->vfc_name); 3609 xvfsp.vfc_typenum = vfsp->vfc_typenum; 3610 xvfsp.vfc_refcount = vfsp->vfc_refcount; 3611 xvfsp.vfc_flags = vfsp->vfc_flags; 3612 xvfsp.vfc_vfsops = 0; 3613 xvfsp.vfc_next = 0; 3614 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp))); 3615 } 3616 #endif 3617 3618 /* 3619 * Top level filesystem related information gathering. 3620 */ 3621 static int 3622 sysctl_vfs_conflist(SYSCTL_HANDLER_ARGS) 3623 { 3624 struct vfsconf *vfsp; 3625 int error; 3626 3627 error = 0; 3628 vfsconf_slock(); 3629 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) { 3630 #ifdef COMPAT_FREEBSD32 3631 if (req->flags & SCTL_MASK32) 3632 error = vfsconf2x32(req, vfsp); 3633 else 3634 #endif 3635 error = vfsconf2x(req, vfsp); 3636 if (error) 3637 break; 3638 } 3639 vfsconf_sunlock(); 3640 return (error); 3641 } 3642 3643 SYSCTL_PROC(_vfs, OID_AUTO, conflist, CTLTYPE_OPAQUE | CTLFLAG_RD | 3644 CTLFLAG_MPSAFE, NULL, 0, sysctl_vfs_conflist, 3645 "S,xvfsconf", "List of all configured filesystems"); 3646 3647 #ifndef BURN_BRIDGES 3648 static int sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS); 3649 3650 static int 3651 vfs_sysctl(SYSCTL_HANDLER_ARGS) 3652 { 3653 int *name = (int *)arg1 - 1; /* XXX */ 3654 u_int namelen = arg2 + 1; /* XXX */ 3655 struct vfsconf *vfsp; 3656 3657 log(LOG_WARNING, "userland calling deprecated sysctl, " 3658 "please rebuild world\n"); 3659 3660 #if 1 || defined(COMPAT_PRELITE2) 3661 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */ 3662 if (namelen == 1) 3663 return (sysctl_ovfs_conf(oidp, arg1, arg2, req)); 3664 #endif 3665 3666 switch (name[1]) { 3667 case VFS_MAXTYPENUM: 3668 if (namelen != 2) 3669 return (ENOTDIR); 3670 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int))); 3671 case VFS_CONF: 3672 if (namelen != 3) 3673 return (ENOTDIR); /* overloaded */ 3674 vfsconf_slock(); 3675 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) { 3676 if (vfsp->vfc_typenum == name[2]) 3677 break; 3678 } 3679 vfsconf_sunlock(); 3680 if (vfsp == NULL) 3681 return (EOPNOTSUPP); 3682 #ifdef COMPAT_FREEBSD32 3683 if (req->flags & SCTL_MASK32) 3684 return (vfsconf2x32(req, vfsp)); 3685 else 3686 #endif 3687 return (vfsconf2x(req, vfsp)); 3688 } 3689 return (EOPNOTSUPP); 3690 } 3691 3692 static SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD | CTLFLAG_SKIP | 3693 CTLFLAG_MPSAFE, vfs_sysctl, 3694 "Generic filesystem"); 3695 3696 #if 1 || defined(COMPAT_PRELITE2) 3697 3698 static int 3699 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS) 3700 { 3701 int error; 3702 struct vfsconf *vfsp; 3703 struct ovfsconf ovfs; 3704 3705 vfsconf_slock(); 3706 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) { 3707 bzero(&ovfs, sizeof(ovfs)); 3708 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */ 3709 strcpy(ovfs.vfc_name, vfsp->vfc_name); 3710 ovfs.vfc_index = vfsp->vfc_typenum; 3711 ovfs.vfc_refcount = vfsp->vfc_refcount; 3712 ovfs.vfc_flags = vfsp->vfc_flags; 3713 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs); 3714 if (error != 0) { 3715 vfsconf_sunlock(); 3716 return (error); 3717 } 3718 } 3719 vfsconf_sunlock(); 3720 return (0); 3721 } 3722 3723 #endif /* 1 || COMPAT_PRELITE2 */ 3724 #endif /* !BURN_BRIDGES */ 3725 3726 #define KINFO_VNODESLOP 10 3727 #ifdef notyet 3728 /* 3729 * Dump vnode list (via sysctl). 3730 */ 3731 /* ARGSUSED */ 3732 static int 3733 sysctl_vnode(SYSCTL_HANDLER_ARGS) 3734 { 3735 struct xvnode *xvn; 3736 struct mount *mp; 3737 struct vnode *vp; 3738 int error, len, n; 3739 3740 /* 3741 * Stale numvnodes access is not fatal here. 3742 */ 3743 req->lock = 0; 3744 len = (numvnodes + KINFO_VNODESLOP) * sizeof *xvn; 3745 if (!req->oldptr) 3746 /* Make an estimate */ 3747 return (SYSCTL_OUT(req, 0, len)); 3748 3749 error = sysctl_wire_old_buffer(req, 0); 3750 if (error != 0) 3751 return (error); 3752 xvn = malloc(len, M_TEMP, M_ZERO | M_WAITOK); 3753 n = 0; 3754 mtx_lock(&mountlist_mtx); 3755 TAILQ_FOREACH(mp, &mountlist, mnt_list) { 3756 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) 3757 continue; 3758 MNT_ILOCK(mp); 3759 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) { 3760 if (n == len) 3761 break; 3762 vref(vp); 3763 xvn[n].xv_size = sizeof *xvn; 3764 xvn[n].xv_vnode = vp; 3765 xvn[n].xv_id = 0; /* XXX compat */ 3766 #define XV_COPY(field) xvn[n].xv_##field = vp->v_##field 3767 XV_COPY(usecount); 3768 XV_COPY(writecount); 3769 XV_COPY(holdcnt); 3770 XV_COPY(mount); 3771 XV_COPY(numoutput); 3772 XV_COPY(type); 3773 #undef XV_COPY 3774 xvn[n].xv_flag = vp->v_vflag; 3775 3776 switch (vp->v_type) { 3777 case VREG: 3778 case VDIR: 3779 case VLNK: 3780 break; 3781 case VBLK: 3782 case VCHR: 3783 if (vp->v_rdev == NULL) { 3784 vrele(vp); 3785 continue; 3786 } 3787 xvn[n].xv_dev = dev2udev(vp->v_rdev); 3788 break; 3789 case VSOCK: 3790 xvn[n].xv_socket = vp->v_socket; 3791 break; 3792 case VFIFO: 3793 xvn[n].xv_fifo = vp->v_fifoinfo; 3794 break; 3795 case VNON: 3796 case VBAD: 3797 default: 3798 /* shouldn't happen? */ 3799 vrele(vp); 3800 continue; 3801 } 3802 vrele(vp); 3803 ++n; 3804 } 3805 MNT_IUNLOCK(mp); 3806 mtx_lock(&mountlist_mtx); 3807 vfs_unbusy(mp); 3808 if (n == len) 3809 break; 3810 } 3811 mtx_unlock(&mountlist_mtx); 3812 3813 error = SYSCTL_OUT(req, xvn, n * sizeof *xvn); 3814 free(xvn, M_TEMP); 3815 return (error); 3816 } 3817 3818 SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE | CTLFLAG_RD | 3819 CTLFLAG_MPSAFE, 0, 0, sysctl_vnode, "S,xvnode", 3820 ""); 3821 #endif 3822 3823 static void 3824 unmount_or_warn(struct mount *mp) 3825 { 3826 int error; 3827 3828 error = dounmount(mp, MNT_FORCE, curthread); 3829 if (error != 0) { 3830 printf("unmount of %s failed (", mp->mnt_stat.f_mntonname); 3831 if (error == EBUSY) 3832 printf("BUSY)\n"); 3833 else 3834 printf("%d)\n", error); 3835 } 3836 } 3837 3838 /* 3839 * Unmount all filesystems. The list is traversed in reverse order 3840 * of mounting to avoid dependencies. 3841 */ 3842 void 3843 vfs_unmountall(void) 3844 { 3845 struct mount *mp, *tmp; 3846 3847 CTR1(KTR_VFS, "%s: unmounting all filesystems", __func__); 3848 3849 /* 3850 * Since this only runs when rebooting, it is not interlocked. 3851 */ 3852 TAILQ_FOREACH_REVERSE_SAFE(mp, &mountlist, mntlist, mnt_list, tmp) { 3853 vfs_ref(mp); 3854 3855 /* 3856 * Forcibly unmounting "/dev" before "/" would prevent clean 3857 * unmount of the latter. 3858 */ 3859 if (mp == rootdevmp) 3860 continue; 3861 3862 unmount_or_warn(mp); 3863 } 3864 3865 if (rootdevmp != NULL) 3866 unmount_or_warn(rootdevmp); 3867 } 3868 3869 /* 3870 * perform msync on all vnodes under a mount point 3871 * the mount point must be locked. 3872 */ 3873 void 3874 vfs_msync(struct mount *mp, int flags) 3875 { 3876 struct vnode *vp, *mvp; 3877 struct vm_object *obj; 3878 3879 CTR2(KTR_VFS, "%s: mp %p", __func__, mp); 3880 MNT_VNODE_FOREACH_ACTIVE(vp, mp, mvp) { 3881 obj = vp->v_object; 3882 if (obj != NULL && (obj->flags & OBJ_MIGHTBEDIRTY) != 0 && 3883 (flags == MNT_WAIT || VOP_ISLOCKED(vp) == 0)) { 3884 if (!vget(vp, 3885 LK_EXCLUSIVE | LK_RETRY | LK_INTERLOCK, 3886 curthread)) { 3887 if (vp->v_vflag & VV_NOSYNC) { /* unlinked */ 3888 vput(vp); 3889 continue; 3890 } 3891 3892 obj = vp->v_object; 3893 if (obj != NULL) { 3894 VM_OBJECT_WLOCK(obj); 3895 vm_object_page_clean(obj, 0, 0, 3896 flags == MNT_WAIT ? 3897 OBJPC_SYNC : OBJPC_NOSYNC); 3898 VM_OBJECT_WUNLOCK(obj); 3899 } 3900 vput(vp); 3901 } 3902 } else 3903 VI_UNLOCK(vp); 3904 } 3905 } 3906 3907 static void 3908 destroy_vpollinfo_free(struct vpollinfo *vi) 3909 { 3910 3911 knlist_destroy(&vi->vpi_selinfo.si_note); 3912 mtx_destroy(&vi->vpi_lock); 3913 uma_zfree(vnodepoll_zone, vi); 3914 } 3915 3916 static void 3917 destroy_vpollinfo(struct vpollinfo *vi) 3918 { 3919 3920 knlist_clear(&vi->vpi_selinfo.si_note, 1); 3921 seldrain(&vi->vpi_selinfo); 3922 destroy_vpollinfo_free(vi); 3923 } 3924 3925 /* 3926 * Initalize per-vnode helper structure to hold poll-related state. 3927 */ 3928 void 3929 v_addpollinfo(struct vnode *vp) 3930 { 3931 struct vpollinfo *vi; 3932 3933 if (vp->v_pollinfo != NULL) 3934 return; 3935 vi = uma_zalloc(vnodepoll_zone, M_WAITOK | M_ZERO); 3936 mtx_init(&vi->vpi_lock, "vnode pollinfo", NULL, MTX_DEF); 3937 knlist_init(&vi->vpi_selinfo.si_note, vp, vfs_knllock, 3938 vfs_knlunlock, vfs_knl_assert_locked, vfs_knl_assert_unlocked); 3939 VI_LOCK(vp); 3940 if (vp->v_pollinfo != NULL) { 3941 VI_UNLOCK(vp); 3942 destroy_vpollinfo_free(vi); 3943 return; 3944 } 3945 vp->v_pollinfo = vi; 3946 VI_UNLOCK(vp); 3947 } 3948 3949 /* 3950 * Record a process's interest in events which might happen to 3951 * a vnode. Because poll uses the historic select-style interface 3952 * internally, this routine serves as both the ``check for any 3953 * pending events'' and the ``record my interest in future events'' 3954 * functions. (These are done together, while the lock is held, 3955 * to avoid race conditions.) 3956 */ 3957 int 3958 vn_pollrecord(struct vnode *vp, struct thread *td, int events) 3959 { 3960 3961 v_addpollinfo(vp); 3962 mtx_lock(&vp->v_pollinfo->vpi_lock); 3963 if (vp->v_pollinfo->vpi_revents & events) { 3964 /* 3965 * This leaves events we are not interested 3966 * in available for the other process which 3967 * which presumably had requested them 3968 * (otherwise they would never have been 3969 * recorded). 3970 */ 3971 events &= vp->v_pollinfo->vpi_revents; 3972 vp->v_pollinfo->vpi_revents &= ~events; 3973 3974 mtx_unlock(&vp->v_pollinfo->vpi_lock); 3975 return (events); 3976 } 3977 vp->v_pollinfo->vpi_events |= events; 3978 selrecord(td, &vp->v_pollinfo->vpi_selinfo); 3979 mtx_unlock(&vp->v_pollinfo->vpi_lock); 3980 return (0); 3981 } 3982 3983 /* 3984 * Routine to create and manage a filesystem syncer vnode. 3985 */ 3986 #define sync_close ((int (*)(struct vop_close_args *))nullop) 3987 static int sync_fsync(struct vop_fsync_args *); 3988 static int sync_inactive(struct vop_inactive_args *); 3989 static int sync_reclaim(struct vop_reclaim_args *); 3990 3991 static struct vop_vector sync_vnodeops = { 3992 .vop_bypass = VOP_EOPNOTSUPP, 3993 .vop_close = sync_close, /* close */ 3994 .vop_fsync = sync_fsync, /* fsync */ 3995 .vop_inactive = sync_inactive, /* inactive */ 3996 .vop_reclaim = sync_reclaim, /* reclaim */ 3997 .vop_lock1 = vop_stdlock, /* lock */ 3998 .vop_unlock = vop_stdunlock, /* unlock */ 3999 .vop_islocked = vop_stdislocked, /* islocked */ 4000 }; 4001 4002 /* 4003 * Create a new filesystem syncer vnode for the specified mount point. 4004 */ 4005 void 4006 vfs_allocate_syncvnode(struct mount *mp) 4007 { 4008 struct vnode *vp; 4009 struct bufobj *bo; 4010 static long start, incr, next; 4011 int error; 4012 4013 /* Allocate a new vnode */ 4014 error = getnewvnode("syncer", mp, &sync_vnodeops, &vp); 4015 if (error != 0) 4016 panic("vfs_allocate_syncvnode: getnewvnode() failed"); 4017 vp->v_type = VNON; 4018 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 4019 vp->v_vflag |= VV_FORCEINSMQ; 4020 error = insmntque(vp, mp); 4021 if (error != 0) 4022 panic("vfs_allocate_syncvnode: insmntque() failed"); 4023 vp->v_vflag &= ~VV_FORCEINSMQ; 4024 VOP_UNLOCK(vp, 0); 4025 /* 4026 * Place the vnode onto the syncer worklist. We attempt to 4027 * scatter them about on the list so that they will go off 4028 * at evenly distributed times even if all the filesystems 4029 * are mounted at once. 4030 */ 4031 next += incr; 4032 if (next == 0 || next > syncer_maxdelay) { 4033 start /= 2; 4034 incr /= 2; 4035 if (start == 0) { 4036 start = syncer_maxdelay / 2; 4037 incr = syncer_maxdelay; 4038 } 4039 next = start; 4040 } 4041 bo = &vp->v_bufobj; 4042 BO_LOCK(bo); 4043 vn_syncer_add_to_worklist(bo, syncdelay > 0 ? next % syncdelay : 0); 4044 /* XXX - vn_syncer_add_to_worklist() also grabs and drops sync_mtx. */ 4045 mtx_lock(&sync_mtx); 4046 sync_vnode_count++; 4047 if (mp->mnt_syncer == NULL) { 4048 mp->mnt_syncer = vp; 4049 vp = NULL; 4050 } 4051 mtx_unlock(&sync_mtx); 4052 BO_UNLOCK(bo); 4053 if (vp != NULL) { 4054 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 4055 vgone(vp); 4056 vput(vp); 4057 } 4058 } 4059 4060 void 4061 vfs_deallocate_syncvnode(struct mount *mp) 4062 { 4063 struct vnode *vp; 4064 4065 mtx_lock(&sync_mtx); 4066 vp = mp->mnt_syncer; 4067 if (vp != NULL) 4068 mp->mnt_syncer = NULL; 4069 mtx_unlock(&sync_mtx); 4070 if (vp != NULL) 4071 vrele(vp); 4072 } 4073 4074 /* 4075 * Do a lazy sync of the filesystem. 4076 */ 4077 static int 4078 sync_fsync(struct vop_fsync_args *ap) 4079 { 4080 struct vnode *syncvp = ap->a_vp; 4081 struct mount *mp = syncvp->v_mount; 4082 int error, save; 4083 struct bufobj *bo; 4084 4085 /* 4086 * We only need to do something if this is a lazy evaluation. 4087 */ 4088 if (ap->a_waitfor != MNT_LAZY) 4089 return (0); 4090 4091 /* 4092 * Move ourselves to the back of the sync list. 4093 */ 4094 bo = &syncvp->v_bufobj; 4095 BO_LOCK(bo); 4096 vn_syncer_add_to_worklist(bo, syncdelay); 4097 BO_UNLOCK(bo); 4098 4099 /* 4100 * Walk the list of vnodes pushing all that are dirty and 4101 * not already on the sync list. 4102 */ 4103 if (vfs_busy(mp, MBF_NOWAIT) != 0) 4104 return (0); 4105 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) { 4106 vfs_unbusy(mp); 4107 return (0); 4108 } 4109 save = curthread_pflags_set(TDP_SYNCIO); 4110 vfs_msync(mp, MNT_NOWAIT); 4111 error = VFS_SYNC(mp, MNT_LAZY); 4112 curthread_pflags_restore(save); 4113 vn_finished_write(mp); 4114 vfs_unbusy(mp); 4115 return (error); 4116 } 4117 4118 /* 4119 * The syncer vnode is no referenced. 4120 */ 4121 static int 4122 sync_inactive(struct vop_inactive_args *ap) 4123 { 4124 4125 vgone(ap->a_vp); 4126 return (0); 4127 } 4128 4129 /* 4130 * The syncer vnode is no longer needed and is being decommissioned. 4131 * 4132 * Modifications to the worklist must be protected by sync_mtx. 4133 */ 4134 static int 4135 sync_reclaim(struct vop_reclaim_args *ap) 4136 { 4137 struct vnode *vp = ap->a_vp; 4138 struct bufobj *bo; 4139 4140 bo = &vp->v_bufobj; 4141 BO_LOCK(bo); 4142 mtx_lock(&sync_mtx); 4143 if (vp->v_mount->mnt_syncer == vp) 4144 vp->v_mount->mnt_syncer = NULL; 4145 if (bo->bo_flag & BO_ONWORKLST) { 4146 LIST_REMOVE(bo, bo_synclist); 4147 syncer_worklist_len--; 4148 sync_vnode_count--; 4149 bo->bo_flag &= ~BO_ONWORKLST; 4150 } 4151 mtx_unlock(&sync_mtx); 4152 BO_UNLOCK(bo); 4153 4154 return (0); 4155 } 4156 4157 /* 4158 * Check if vnode represents a disk device 4159 */ 4160 int 4161 vn_isdisk(struct vnode *vp, int *errp) 4162 { 4163 int error; 4164 4165 if (vp->v_type != VCHR) { 4166 error = ENOTBLK; 4167 goto out; 4168 } 4169 error = 0; 4170 dev_lock(); 4171 if (vp->v_rdev == NULL) 4172 error = ENXIO; 4173 else if (vp->v_rdev->si_devsw == NULL) 4174 error = ENXIO; 4175 else if (!(vp->v_rdev->si_devsw->d_flags & D_DISK)) 4176 error = ENOTBLK; 4177 dev_unlock(); 4178 out: 4179 if (errp != NULL) 4180 *errp = error; 4181 return (error == 0); 4182 } 4183 4184 /* 4185 * Common filesystem object access control check routine. Accepts a 4186 * vnode's type, "mode", uid and gid, requested access mode, credentials, 4187 * and optional call-by-reference privused argument allowing vaccess() 4188 * to indicate to the caller whether privilege was used to satisfy the 4189 * request (obsoleted). Returns 0 on success, or an errno on failure. 4190 */ 4191 int 4192 vaccess(enum vtype type, mode_t file_mode, uid_t file_uid, gid_t file_gid, 4193 accmode_t accmode, struct ucred *cred, int *privused) 4194 { 4195 accmode_t dac_granted; 4196 accmode_t priv_granted; 4197 4198 KASSERT((accmode & ~(VEXEC | VWRITE | VREAD | VADMIN | VAPPEND)) == 0, 4199 ("invalid bit in accmode")); 4200 KASSERT((accmode & VAPPEND) == 0 || (accmode & VWRITE), 4201 ("VAPPEND without VWRITE")); 4202 4203 /* 4204 * Look for a normal, non-privileged way to access the file/directory 4205 * as requested. If it exists, go with that. 4206 */ 4207 4208 if (privused != NULL) 4209 *privused = 0; 4210 4211 dac_granted = 0; 4212 4213 /* Check the owner. */ 4214 if (cred->cr_uid == file_uid) { 4215 dac_granted |= VADMIN; 4216 if (file_mode & S_IXUSR) 4217 dac_granted |= VEXEC; 4218 if (file_mode & S_IRUSR) 4219 dac_granted |= VREAD; 4220 if (file_mode & S_IWUSR) 4221 dac_granted |= (VWRITE | VAPPEND); 4222 4223 if ((accmode & dac_granted) == accmode) 4224 return (0); 4225 4226 goto privcheck; 4227 } 4228 4229 /* Otherwise, check the groups (first match) */ 4230 if (groupmember(file_gid, cred)) { 4231 if (file_mode & S_IXGRP) 4232 dac_granted |= VEXEC; 4233 if (file_mode & S_IRGRP) 4234 dac_granted |= VREAD; 4235 if (file_mode & S_IWGRP) 4236 dac_granted |= (VWRITE | VAPPEND); 4237 4238 if ((accmode & dac_granted) == accmode) 4239 return (0); 4240 4241 goto privcheck; 4242 } 4243 4244 /* Otherwise, check everyone else. */ 4245 if (file_mode & S_IXOTH) 4246 dac_granted |= VEXEC; 4247 if (file_mode & S_IROTH) 4248 dac_granted |= VREAD; 4249 if (file_mode & S_IWOTH) 4250 dac_granted |= (VWRITE | VAPPEND); 4251 if ((accmode & dac_granted) == accmode) 4252 return (0); 4253 4254 privcheck: 4255 /* 4256 * Build a privilege mask to determine if the set of privileges 4257 * satisfies the requirements when combined with the granted mask 4258 * from above. For each privilege, if the privilege is required, 4259 * bitwise or the request type onto the priv_granted mask. 4260 */ 4261 priv_granted = 0; 4262 4263 if (type == VDIR) { 4264 /* 4265 * For directories, use PRIV_VFS_LOOKUP to satisfy VEXEC 4266 * requests, instead of PRIV_VFS_EXEC. 4267 */ 4268 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) && 4269 !priv_check_cred(cred, PRIV_VFS_LOOKUP, 0)) 4270 priv_granted |= VEXEC; 4271 } else { 4272 /* 4273 * Ensure that at least one execute bit is on. Otherwise, 4274 * a privileged user will always succeed, and we don't want 4275 * this to happen unless the file really is executable. 4276 */ 4277 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) && 4278 (file_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0 && 4279 !priv_check_cred(cred, PRIV_VFS_EXEC, 0)) 4280 priv_granted |= VEXEC; 4281 } 4282 4283 if ((accmode & VREAD) && ((dac_granted & VREAD) == 0) && 4284 !priv_check_cred(cred, PRIV_VFS_READ, 0)) 4285 priv_granted |= VREAD; 4286 4287 if ((accmode & VWRITE) && ((dac_granted & VWRITE) == 0) && 4288 !priv_check_cred(cred, PRIV_VFS_WRITE, 0)) 4289 priv_granted |= (VWRITE | VAPPEND); 4290 4291 if ((accmode & VADMIN) && ((dac_granted & VADMIN) == 0) && 4292 !priv_check_cred(cred, PRIV_VFS_ADMIN, 0)) 4293 priv_granted |= VADMIN; 4294 4295 if ((accmode & (priv_granted | dac_granted)) == accmode) { 4296 /* XXX audit: privilege used */ 4297 if (privused != NULL) 4298 *privused = 1; 4299 return (0); 4300 } 4301 4302 return ((accmode & VADMIN) ? EPERM : EACCES); 4303 } 4304 4305 /* 4306 * Credential check based on process requesting service, and per-attribute 4307 * permissions. 4308 */ 4309 int 4310 extattr_check_cred(struct vnode *vp, int attrnamespace, struct ucred *cred, 4311 struct thread *td, accmode_t accmode) 4312 { 4313 4314 /* 4315 * Kernel-invoked always succeeds. 4316 */ 4317 if (cred == NOCRED) 4318 return (0); 4319 4320 /* 4321 * Do not allow privileged processes in jail to directly manipulate 4322 * system attributes. 4323 */ 4324 switch (attrnamespace) { 4325 case EXTATTR_NAMESPACE_SYSTEM: 4326 /* Potentially should be: return (EPERM); */ 4327 return (priv_check_cred(cred, PRIV_VFS_EXTATTR_SYSTEM, 0)); 4328 case EXTATTR_NAMESPACE_USER: 4329 return (VOP_ACCESS(vp, accmode, cred, td)); 4330 default: 4331 return (EPERM); 4332 } 4333 } 4334 4335 #ifdef DEBUG_VFS_LOCKS 4336 /* 4337 * This only exists to supress warnings from unlocked specfs accesses. It is 4338 * no longer ok to have an unlocked VFS. 4339 */ 4340 #define IGNORE_LOCK(vp) (panicstr != NULL || (vp) == NULL || \ 4341 (vp)->v_type == VCHR || (vp)->v_type == VBAD) 4342 4343 int vfs_badlock_ddb = 1; /* Drop into debugger on violation. */ 4344 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_ddb, CTLFLAG_RW, &vfs_badlock_ddb, 0, 4345 "Drop into debugger on lock violation"); 4346 4347 int vfs_badlock_mutex = 1; /* Check for interlock across VOPs. */ 4348 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_mutex, CTLFLAG_RW, &vfs_badlock_mutex, 4349 0, "Check for interlock across VOPs"); 4350 4351 int vfs_badlock_print = 1; /* Print lock violations. */ 4352 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_print, CTLFLAG_RW, &vfs_badlock_print, 4353 0, "Print lock violations"); 4354 4355 #ifdef KDB 4356 int vfs_badlock_backtrace = 1; /* Print backtrace at lock violations. */ 4357 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_backtrace, CTLFLAG_RW, 4358 &vfs_badlock_backtrace, 0, "Print backtrace at lock violations"); 4359 #endif 4360 4361 static void 4362 vfs_badlock(const char *msg, const char *str, struct vnode *vp) 4363 { 4364 4365 #ifdef KDB 4366 if (vfs_badlock_backtrace) 4367 kdb_backtrace(); 4368 #endif 4369 if (vfs_badlock_print) 4370 printf("%s: %p %s\n", str, (void *)vp, msg); 4371 if (vfs_badlock_ddb) 4372 kdb_enter(KDB_WHY_VFSLOCK, "lock violation"); 4373 } 4374 4375 void 4376 assert_vi_locked(struct vnode *vp, const char *str) 4377 { 4378 4379 if (vfs_badlock_mutex && !mtx_owned(VI_MTX(vp))) 4380 vfs_badlock("interlock is not locked but should be", str, vp); 4381 } 4382 4383 void 4384 assert_vi_unlocked(struct vnode *vp, const char *str) 4385 { 4386 4387 if (vfs_badlock_mutex && mtx_owned(VI_MTX(vp))) 4388 vfs_badlock("interlock is locked but should not be", str, vp); 4389 } 4390 4391 void 4392 assert_vop_locked(struct vnode *vp, const char *str) 4393 { 4394 int locked; 4395 4396 if (!IGNORE_LOCK(vp)) { 4397 locked = VOP_ISLOCKED(vp); 4398 if (locked == 0 || locked == LK_EXCLOTHER) 4399 vfs_badlock("is not locked but should be", str, vp); 4400 } 4401 } 4402 4403 void 4404 assert_vop_unlocked(struct vnode *vp, const char *str) 4405 { 4406 4407 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) == LK_EXCLUSIVE) 4408 vfs_badlock("is locked but should not be", str, vp); 4409 } 4410 4411 void 4412 assert_vop_elocked(struct vnode *vp, const char *str) 4413 { 4414 4415 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_EXCLUSIVE) 4416 vfs_badlock("is not exclusive locked but should be", str, vp); 4417 } 4418 4419 #if 0 4420 void 4421 assert_vop_elocked_other(struct vnode *vp, const char *str) 4422 { 4423 4424 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_EXCLOTHER) 4425 vfs_badlock("is not exclusive locked by another thread", 4426 str, vp); 4427 } 4428 4429 void 4430 assert_vop_slocked(struct vnode *vp, const char *str) 4431 { 4432 4433 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_SHARED) 4434 vfs_badlock("is not locked shared but should be", str, vp); 4435 } 4436 #endif /* 0 */ 4437 #endif /* DEBUG_VFS_LOCKS */ 4438 4439 void 4440 vop_rename_fail(struct vop_rename_args *ap) 4441 { 4442 4443 if (ap->a_tvp != NULL) 4444 vput(ap->a_tvp); 4445 if (ap->a_tdvp == ap->a_tvp) 4446 vrele(ap->a_tdvp); 4447 else 4448 vput(ap->a_tdvp); 4449 vrele(ap->a_fdvp); 4450 vrele(ap->a_fvp); 4451 } 4452 4453 void 4454 vop_rename_pre(void *ap) 4455 { 4456 struct vop_rename_args *a = ap; 4457 4458 #ifdef DEBUG_VFS_LOCKS 4459 if (a->a_tvp) 4460 ASSERT_VI_UNLOCKED(a->a_tvp, "VOP_RENAME"); 4461 ASSERT_VI_UNLOCKED(a->a_tdvp, "VOP_RENAME"); 4462 ASSERT_VI_UNLOCKED(a->a_fvp, "VOP_RENAME"); 4463 ASSERT_VI_UNLOCKED(a->a_fdvp, "VOP_RENAME"); 4464 4465 /* Check the source (from). */ 4466 if (a->a_tdvp->v_vnlock != a->a_fdvp->v_vnlock && 4467 (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fdvp->v_vnlock)) 4468 ASSERT_VOP_UNLOCKED(a->a_fdvp, "vop_rename: fdvp locked"); 4469 if (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fvp->v_vnlock) 4470 ASSERT_VOP_UNLOCKED(a->a_fvp, "vop_rename: fvp locked"); 4471 4472 /* Check the target. */ 4473 if (a->a_tvp) 4474 ASSERT_VOP_LOCKED(a->a_tvp, "vop_rename: tvp not locked"); 4475 ASSERT_VOP_LOCKED(a->a_tdvp, "vop_rename: tdvp not locked"); 4476 #endif 4477 if (a->a_tdvp != a->a_fdvp) 4478 vhold(a->a_fdvp); 4479 if (a->a_tvp != a->a_fvp) 4480 vhold(a->a_fvp); 4481 vhold(a->a_tdvp); 4482 if (a->a_tvp) 4483 vhold(a->a_tvp); 4484 } 4485 4486 void 4487 vop_strategy_pre(void *ap) 4488 { 4489 #ifdef DEBUG_VFS_LOCKS 4490 struct vop_strategy_args *a; 4491 struct buf *bp; 4492 4493 a = ap; 4494 bp = a->a_bp; 4495 4496 /* 4497 * Cluster ops lock their component buffers but not the IO container. 4498 */ 4499 if ((bp->b_flags & B_CLUSTER) != 0) 4500 return; 4501 4502 if (panicstr == NULL && !BUF_ISLOCKED(bp)) { 4503 if (vfs_badlock_print) 4504 printf( 4505 "VOP_STRATEGY: bp is not locked but should be\n"); 4506 if (vfs_badlock_ddb) 4507 kdb_enter(KDB_WHY_VFSLOCK, "lock violation"); 4508 } 4509 #endif 4510 } 4511 4512 void 4513 vop_lock_pre(void *ap) 4514 { 4515 #ifdef DEBUG_VFS_LOCKS 4516 struct vop_lock1_args *a = ap; 4517 4518 if ((a->a_flags & LK_INTERLOCK) == 0) 4519 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK"); 4520 else 4521 ASSERT_VI_LOCKED(a->a_vp, "VOP_LOCK"); 4522 #endif 4523 } 4524 4525 void 4526 vop_lock_post(void *ap, int rc) 4527 { 4528 #ifdef DEBUG_VFS_LOCKS 4529 struct vop_lock1_args *a = ap; 4530 4531 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK"); 4532 if (rc == 0 && (a->a_flags & LK_EXCLOTHER) == 0) 4533 ASSERT_VOP_LOCKED(a->a_vp, "VOP_LOCK"); 4534 #endif 4535 } 4536 4537 void 4538 vop_unlock_pre(void *ap) 4539 { 4540 #ifdef DEBUG_VFS_LOCKS 4541 struct vop_unlock_args *a = ap; 4542 4543 if (a->a_flags & LK_INTERLOCK) 4544 ASSERT_VI_LOCKED(a->a_vp, "VOP_UNLOCK"); 4545 ASSERT_VOP_LOCKED(a->a_vp, "VOP_UNLOCK"); 4546 #endif 4547 } 4548 4549 void 4550 vop_unlock_post(void *ap, int rc) 4551 { 4552 #ifdef DEBUG_VFS_LOCKS 4553 struct vop_unlock_args *a = ap; 4554 4555 if (a->a_flags & LK_INTERLOCK) 4556 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_UNLOCK"); 4557 #endif 4558 } 4559 4560 void 4561 vop_create_post(void *ap, int rc) 4562 { 4563 struct vop_create_args *a = ap; 4564 4565 if (!rc) 4566 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE); 4567 } 4568 4569 void 4570 vop_deleteextattr_post(void *ap, int rc) 4571 { 4572 struct vop_deleteextattr_args *a = ap; 4573 4574 if (!rc) 4575 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB); 4576 } 4577 4578 void 4579 vop_link_post(void *ap, int rc) 4580 { 4581 struct vop_link_args *a = ap; 4582 4583 if (!rc) { 4584 VFS_KNOTE_LOCKED(a->a_vp, NOTE_LINK); 4585 VFS_KNOTE_LOCKED(a->a_tdvp, NOTE_WRITE); 4586 } 4587 } 4588 4589 void 4590 vop_mkdir_post(void *ap, int rc) 4591 { 4592 struct vop_mkdir_args *a = ap; 4593 4594 if (!rc) 4595 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK); 4596 } 4597 4598 void 4599 vop_mknod_post(void *ap, int rc) 4600 { 4601 struct vop_mknod_args *a = ap; 4602 4603 if (!rc) 4604 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE); 4605 } 4606 4607 void 4608 vop_reclaim_post(void *ap, int rc) 4609 { 4610 struct vop_reclaim_args *a = ap; 4611 4612 if (!rc) 4613 VFS_KNOTE_LOCKED(a->a_vp, NOTE_REVOKE); 4614 } 4615 4616 void 4617 vop_remove_post(void *ap, int rc) 4618 { 4619 struct vop_remove_args *a = ap; 4620 4621 if (!rc) { 4622 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE); 4623 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE); 4624 } 4625 } 4626 4627 void 4628 vop_rename_post(void *ap, int rc) 4629 { 4630 struct vop_rename_args *a = ap; 4631 4632 if (!rc) { 4633 VFS_KNOTE_UNLOCKED(a->a_fdvp, NOTE_WRITE); 4634 VFS_KNOTE_UNLOCKED(a->a_tdvp, NOTE_WRITE); 4635 VFS_KNOTE_UNLOCKED(a->a_fvp, NOTE_RENAME); 4636 if (a->a_tvp) 4637 VFS_KNOTE_UNLOCKED(a->a_tvp, NOTE_DELETE); 4638 } 4639 if (a->a_tdvp != a->a_fdvp) 4640 vdrop(a->a_fdvp); 4641 if (a->a_tvp != a->a_fvp) 4642 vdrop(a->a_fvp); 4643 vdrop(a->a_tdvp); 4644 if (a->a_tvp) 4645 vdrop(a->a_tvp); 4646 } 4647 4648 void 4649 vop_rmdir_post(void *ap, int rc) 4650 { 4651 struct vop_rmdir_args *a = ap; 4652 4653 if (!rc) { 4654 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK); 4655 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE); 4656 } 4657 } 4658 4659 void 4660 vop_setattr_post(void *ap, int rc) 4661 { 4662 struct vop_setattr_args *a = ap; 4663 4664 if (!rc) 4665 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB); 4666 } 4667 4668 void 4669 vop_setextattr_post(void *ap, int rc) 4670 { 4671 struct vop_setextattr_args *a = ap; 4672 4673 if (!rc) 4674 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB); 4675 } 4676 4677 void 4678 vop_symlink_post(void *ap, int rc) 4679 { 4680 struct vop_symlink_args *a = ap; 4681 4682 if (!rc) 4683 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE); 4684 } 4685 4686 static struct knlist fs_knlist; 4687 4688 static void 4689 vfs_event_init(void *arg) 4690 { 4691 knlist_init_mtx(&fs_knlist, NULL); 4692 } 4693 /* XXX - correct order? */ 4694 SYSINIT(vfs_knlist, SI_SUB_VFS, SI_ORDER_ANY, vfs_event_init, NULL); 4695 4696 void 4697 vfs_event_signal(fsid_t *fsid, uint32_t event, intptr_t data __unused) 4698 { 4699 4700 KNOTE_UNLOCKED(&fs_knlist, event); 4701 } 4702 4703 static int filt_fsattach(struct knote *kn); 4704 static void filt_fsdetach(struct knote *kn); 4705 static int filt_fsevent(struct knote *kn, long hint); 4706 4707 struct filterops fs_filtops = { 4708 .f_isfd = 0, 4709 .f_attach = filt_fsattach, 4710 .f_detach = filt_fsdetach, 4711 .f_event = filt_fsevent 4712 }; 4713 4714 static int 4715 filt_fsattach(struct knote *kn) 4716 { 4717 4718 kn->kn_flags |= EV_CLEAR; 4719 knlist_add(&fs_knlist, kn, 0); 4720 return (0); 4721 } 4722 4723 static void 4724 filt_fsdetach(struct knote *kn) 4725 { 4726 4727 knlist_remove(&fs_knlist, kn, 0); 4728 } 4729 4730 static int 4731 filt_fsevent(struct knote *kn, long hint) 4732 { 4733 4734 kn->kn_fflags |= hint; 4735 return (kn->kn_fflags != 0); 4736 } 4737 4738 static int 4739 sysctl_vfs_ctl(SYSCTL_HANDLER_ARGS) 4740 { 4741 struct vfsidctl vc; 4742 int error; 4743 struct mount *mp; 4744 4745 error = SYSCTL_IN(req, &vc, sizeof(vc)); 4746 if (error) 4747 return (error); 4748 if (vc.vc_vers != VFS_CTL_VERS1) 4749 return (EINVAL); 4750 mp = vfs_getvfs(&vc.vc_fsid); 4751 if (mp == NULL) 4752 return (ENOENT); 4753 /* ensure that a specific sysctl goes to the right filesystem. */ 4754 if (strcmp(vc.vc_fstypename, "*") != 0 && 4755 strcmp(vc.vc_fstypename, mp->mnt_vfc->vfc_name) != 0) { 4756 vfs_rel(mp); 4757 return (EINVAL); 4758 } 4759 VCTLTOREQ(&vc, req); 4760 error = VFS_SYSCTL(mp, vc.vc_op, req); 4761 vfs_rel(mp); 4762 return (error); 4763 } 4764 4765 SYSCTL_PROC(_vfs, OID_AUTO, ctl, CTLTYPE_OPAQUE | CTLFLAG_WR, 4766 NULL, 0, sysctl_vfs_ctl, "", 4767 "Sysctl by fsid"); 4768 4769 /* 4770 * Function to initialize a va_filerev field sensibly. 4771 * XXX: Wouldn't a random number make a lot more sense ?? 4772 */ 4773 u_quad_t 4774 init_va_filerev(void) 4775 { 4776 struct bintime bt; 4777 4778 getbinuptime(&bt); 4779 return (((u_quad_t)bt.sec << 32LL) | (bt.frac >> 32LL)); 4780 } 4781 4782 static int filt_vfsread(struct knote *kn, long hint); 4783 static int filt_vfswrite(struct knote *kn, long hint); 4784 static int filt_vfsvnode(struct knote *kn, long hint); 4785 static void filt_vfsdetach(struct knote *kn); 4786 static struct filterops vfsread_filtops = { 4787 .f_isfd = 1, 4788 .f_detach = filt_vfsdetach, 4789 .f_event = filt_vfsread 4790 }; 4791 static struct filterops vfswrite_filtops = { 4792 .f_isfd = 1, 4793 .f_detach = filt_vfsdetach, 4794 .f_event = filt_vfswrite 4795 }; 4796 static struct filterops vfsvnode_filtops = { 4797 .f_isfd = 1, 4798 .f_detach = filt_vfsdetach, 4799 .f_event = filt_vfsvnode 4800 }; 4801 4802 static void 4803 vfs_knllock(void *arg) 4804 { 4805 struct vnode *vp = arg; 4806 4807 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 4808 } 4809 4810 static void 4811 vfs_knlunlock(void *arg) 4812 { 4813 struct vnode *vp = arg; 4814 4815 VOP_UNLOCK(vp, 0); 4816 } 4817 4818 static void 4819 vfs_knl_assert_locked(void *arg) 4820 { 4821 #ifdef DEBUG_VFS_LOCKS 4822 struct vnode *vp = arg; 4823 4824 ASSERT_VOP_LOCKED(vp, "vfs_knl_assert_locked"); 4825 #endif 4826 } 4827 4828 static void 4829 vfs_knl_assert_unlocked(void *arg) 4830 { 4831 #ifdef DEBUG_VFS_LOCKS 4832 struct vnode *vp = arg; 4833 4834 ASSERT_VOP_UNLOCKED(vp, "vfs_knl_assert_unlocked"); 4835 #endif 4836 } 4837 4838 int 4839 vfs_kqfilter(struct vop_kqfilter_args *ap) 4840 { 4841 struct vnode *vp = ap->a_vp; 4842 struct knote *kn = ap->a_kn; 4843 struct knlist *knl; 4844 4845 switch (kn->kn_filter) { 4846 case EVFILT_READ: 4847 kn->kn_fop = &vfsread_filtops; 4848 break; 4849 case EVFILT_WRITE: 4850 kn->kn_fop = &vfswrite_filtops; 4851 break; 4852 case EVFILT_VNODE: 4853 kn->kn_fop = &vfsvnode_filtops; 4854 break; 4855 default: 4856 return (EINVAL); 4857 } 4858 4859 kn->kn_hook = (caddr_t)vp; 4860 4861 v_addpollinfo(vp); 4862 if (vp->v_pollinfo == NULL) 4863 return (ENOMEM); 4864 knl = &vp->v_pollinfo->vpi_selinfo.si_note; 4865 vhold(vp); 4866 knlist_add(knl, kn, 0); 4867 4868 return (0); 4869 } 4870 4871 /* 4872 * Detach knote from vnode 4873 */ 4874 static void 4875 filt_vfsdetach(struct knote *kn) 4876 { 4877 struct vnode *vp = (struct vnode *)kn->kn_hook; 4878 4879 KASSERT(vp->v_pollinfo != NULL, ("Missing v_pollinfo")); 4880 knlist_remove(&vp->v_pollinfo->vpi_selinfo.si_note, kn, 0); 4881 vdrop(vp); 4882 } 4883 4884 /*ARGSUSED*/ 4885 static int 4886 filt_vfsread(struct knote *kn, long hint) 4887 { 4888 struct vnode *vp = (struct vnode *)kn->kn_hook; 4889 struct vattr va; 4890 int res; 4891 4892 /* 4893 * filesystem is gone, so set the EOF flag and schedule 4894 * the knote for deletion. 4895 */ 4896 if (hint == NOTE_REVOKE || (hint == 0 && vp->v_type == VBAD)) { 4897 VI_LOCK(vp); 4898 kn->kn_flags |= (EV_EOF | EV_ONESHOT); 4899 VI_UNLOCK(vp); 4900 return (1); 4901 } 4902 4903 if (VOP_GETATTR(vp, &va, curthread->td_ucred)) 4904 return (0); 4905 4906 VI_LOCK(vp); 4907 kn->kn_data = va.va_size - kn->kn_fp->f_offset; 4908 res = (kn->kn_sfflags & NOTE_FILE_POLL) != 0 || kn->kn_data != 0; 4909 VI_UNLOCK(vp); 4910 return (res); 4911 } 4912 4913 /*ARGSUSED*/ 4914 static int 4915 filt_vfswrite(struct knote *kn, long hint) 4916 { 4917 struct vnode *vp = (struct vnode *)kn->kn_hook; 4918 4919 VI_LOCK(vp); 4920 4921 /* 4922 * filesystem is gone, so set the EOF flag and schedule 4923 * the knote for deletion. 4924 */ 4925 if (hint == NOTE_REVOKE || (hint == 0 && vp->v_type == VBAD)) 4926 kn->kn_flags |= (EV_EOF | EV_ONESHOT); 4927 4928 kn->kn_data = 0; 4929 VI_UNLOCK(vp); 4930 return (1); 4931 } 4932 4933 static int 4934 filt_vfsvnode(struct knote *kn, long hint) 4935 { 4936 struct vnode *vp = (struct vnode *)kn->kn_hook; 4937 int res; 4938 4939 VI_LOCK(vp); 4940 if (kn->kn_sfflags & hint) 4941 kn->kn_fflags |= hint; 4942 if (hint == NOTE_REVOKE || (hint == 0 && vp->v_type == VBAD)) { 4943 kn->kn_flags |= EV_EOF; 4944 VI_UNLOCK(vp); 4945 return (1); 4946 } 4947 res = (kn->kn_fflags != 0); 4948 VI_UNLOCK(vp); 4949 return (res); 4950 } 4951 4952 int 4953 vfs_read_dirent(struct vop_readdir_args *ap, struct dirent *dp, off_t off) 4954 { 4955 int error; 4956 4957 if (dp->d_reclen > ap->a_uio->uio_resid) 4958 return (ENAMETOOLONG); 4959 error = uiomove(dp, dp->d_reclen, ap->a_uio); 4960 if (error) { 4961 if (ap->a_ncookies != NULL) { 4962 if (ap->a_cookies != NULL) 4963 free(ap->a_cookies, M_TEMP); 4964 ap->a_cookies = NULL; 4965 *ap->a_ncookies = 0; 4966 } 4967 return (error); 4968 } 4969 if (ap->a_ncookies == NULL) 4970 return (0); 4971 4972 KASSERT(ap->a_cookies, 4973 ("NULL ap->a_cookies value with non-NULL ap->a_ncookies!")); 4974 4975 *ap->a_cookies = realloc(*ap->a_cookies, 4976 (*ap->a_ncookies + 1) * sizeof(u_long), M_TEMP, M_WAITOK | M_ZERO); 4977 (*ap->a_cookies)[*ap->a_ncookies] = off; 4978 return (0); 4979 } 4980 4981 /* 4982 * Mark for update the access time of the file if the filesystem 4983 * supports VOP_MARKATIME. This functionality is used by execve and 4984 * mmap, so we want to avoid the I/O implied by directly setting 4985 * va_atime for the sake of efficiency. 4986 */ 4987 void 4988 vfs_mark_atime(struct vnode *vp, struct ucred *cred) 4989 { 4990 struct mount *mp; 4991 4992 mp = vp->v_mount; 4993 ASSERT_VOP_LOCKED(vp, "vfs_mark_atime"); 4994 if (mp != NULL && (mp->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0) 4995 (void)VOP_MARKATIME(vp); 4996 } 4997 4998 /* 4999 * The purpose of this routine is to remove granularity from accmode_t, 5000 * reducing it into standard unix access bits - VEXEC, VREAD, VWRITE, 5001 * VADMIN and VAPPEND. 5002 * 5003 * If it returns 0, the caller is supposed to continue with the usual 5004 * access checks using 'accmode' as modified by this routine. If it 5005 * returns nonzero value, the caller is supposed to return that value 5006 * as errno. 5007 * 5008 * Note that after this routine runs, accmode may be zero. 5009 */ 5010 int 5011 vfs_unixify_accmode(accmode_t *accmode) 5012 { 5013 /* 5014 * There is no way to specify explicit "deny" rule using 5015 * file mode or POSIX.1e ACLs. 5016 */ 5017 if (*accmode & VEXPLICIT_DENY) { 5018 *accmode = 0; 5019 return (0); 5020 } 5021 5022 /* 5023 * None of these can be translated into usual access bits. 5024 * Also, the common case for NFSv4 ACLs is to not contain 5025 * either of these bits. Caller should check for VWRITE 5026 * on the containing directory instead. 5027 */ 5028 if (*accmode & (VDELETE_CHILD | VDELETE)) 5029 return (EPERM); 5030 5031 if (*accmode & VADMIN_PERMS) { 5032 *accmode &= ~VADMIN_PERMS; 5033 *accmode |= VADMIN; 5034 } 5035 5036 /* 5037 * There is no way to deny VREAD_ATTRIBUTES, VREAD_ACL 5038 * or VSYNCHRONIZE using file mode or POSIX.1e ACL. 5039 */ 5040 *accmode &= ~(VSTAT_PERMS | VSYNCHRONIZE); 5041 5042 return (0); 5043 } 5044 5045 /* 5046 * These are helper functions for filesystems to traverse all 5047 * their vnodes. See MNT_VNODE_FOREACH_ALL() in sys/mount.h. 5048 * 5049 * This interface replaces MNT_VNODE_FOREACH. 5050 */ 5051 5052 MALLOC_DEFINE(M_VNODE_MARKER, "vnodemarker", "vnode marker"); 5053 5054 struct vnode * 5055 __mnt_vnode_next_all(struct vnode **mvp, struct mount *mp) 5056 { 5057 struct vnode *vp; 5058 5059 if (should_yield()) 5060 kern_yield(PRI_USER); 5061 MNT_ILOCK(mp); 5062 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch")); 5063 vp = TAILQ_NEXT(*mvp, v_nmntvnodes); 5064 while (vp != NULL && (vp->v_type == VMARKER || 5065 (vp->v_iflag & VI_DOOMED) != 0)) 5066 vp = TAILQ_NEXT(vp, v_nmntvnodes); 5067 5068 /* Check if we are done */ 5069 if (vp == NULL) { 5070 __mnt_vnode_markerfree_all(mvp, mp); 5071 /* MNT_IUNLOCK(mp); -- done in above function */ 5072 mtx_assert(MNT_MTX(mp), MA_NOTOWNED); 5073 return (NULL); 5074 } 5075 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes); 5076 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes); 5077 VI_LOCK(vp); 5078 MNT_IUNLOCK(mp); 5079 return (vp); 5080 } 5081 5082 struct vnode * 5083 __mnt_vnode_first_all(struct vnode **mvp, struct mount *mp) 5084 { 5085 struct vnode *vp; 5086 5087 *mvp = malloc(sizeof(struct vnode), M_VNODE_MARKER, M_WAITOK | M_ZERO); 5088 MNT_ILOCK(mp); 5089 MNT_REF(mp); 5090 (*mvp)->v_type = VMARKER; 5091 5092 vp = TAILQ_FIRST(&mp->mnt_nvnodelist); 5093 while (vp != NULL && (vp->v_type == VMARKER || 5094 (vp->v_iflag & VI_DOOMED) != 0)) 5095 vp = TAILQ_NEXT(vp, v_nmntvnodes); 5096 5097 /* Check if we are done */ 5098 if (vp == NULL) { 5099 MNT_REL(mp); 5100 MNT_IUNLOCK(mp); 5101 free(*mvp, M_VNODE_MARKER); 5102 *mvp = NULL; 5103 return (NULL); 5104 } 5105 (*mvp)->v_mount = mp; 5106 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes); 5107 VI_LOCK(vp); 5108 MNT_IUNLOCK(mp); 5109 return (vp); 5110 } 5111 5112 5113 void 5114 __mnt_vnode_markerfree_all(struct vnode **mvp, struct mount *mp) 5115 { 5116 5117 if (*mvp == NULL) { 5118 MNT_IUNLOCK(mp); 5119 return; 5120 } 5121 5122 mtx_assert(MNT_MTX(mp), MA_OWNED); 5123 5124 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch")); 5125 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes); 5126 MNT_REL(mp); 5127 MNT_IUNLOCK(mp); 5128 free(*mvp, M_VNODE_MARKER); 5129 *mvp = NULL; 5130 } 5131 5132 /* 5133 * These are helper functions for filesystems to traverse their 5134 * active vnodes. See MNT_VNODE_FOREACH_ACTIVE() in sys/mount.h 5135 */ 5136 static void 5137 mnt_vnode_markerfree_active(struct vnode **mvp, struct mount *mp) 5138 { 5139 5140 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch")); 5141 5142 MNT_ILOCK(mp); 5143 MNT_REL(mp); 5144 MNT_IUNLOCK(mp); 5145 free(*mvp, M_VNODE_MARKER); 5146 *mvp = NULL; 5147 } 5148 5149 static struct vnode * 5150 mnt_vnode_next_active(struct vnode **mvp, struct mount *mp) 5151 { 5152 struct vnode *vp, *nvp; 5153 5154 mtx_assert(&vnode_free_list_mtx, MA_OWNED); 5155 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch")); 5156 restart: 5157 vp = TAILQ_NEXT(*mvp, v_actfreelist); 5158 TAILQ_REMOVE(&mp->mnt_activevnodelist, *mvp, v_actfreelist); 5159 while (vp != NULL) { 5160 if (vp->v_type == VMARKER) { 5161 vp = TAILQ_NEXT(vp, v_actfreelist); 5162 continue; 5163 } 5164 if (!VI_TRYLOCK(vp)) { 5165 if (mp_ncpus == 1 || should_yield()) { 5166 TAILQ_INSERT_BEFORE(vp, *mvp, v_actfreelist); 5167 mtx_unlock(&vnode_free_list_mtx); 5168 pause("vnacti", 1); 5169 mtx_lock(&vnode_free_list_mtx); 5170 goto restart; 5171 } 5172 continue; 5173 } 5174 KASSERT(vp->v_type != VMARKER, ("locked marker %p", vp)); 5175 KASSERT(vp->v_mount == mp || vp->v_mount == NULL, 5176 ("alien vnode on the active list %p %p", vp, mp)); 5177 if (vp->v_mount == mp && (vp->v_iflag & VI_DOOMED) == 0) 5178 break; 5179 nvp = TAILQ_NEXT(vp, v_actfreelist); 5180 VI_UNLOCK(vp); 5181 vp = nvp; 5182 } 5183 5184 /* Check if we are done */ 5185 if (vp == NULL) { 5186 mtx_unlock(&vnode_free_list_mtx); 5187 mnt_vnode_markerfree_active(mvp, mp); 5188 return (NULL); 5189 } 5190 TAILQ_INSERT_AFTER(&mp->mnt_activevnodelist, vp, *mvp, v_actfreelist); 5191 mtx_unlock(&vnode_free_list_mtx); 5192 ASSERT_VI_LOCKED(vp, "active iter"); 5193 KASSERT((vp->v_iflag & VI_ACTIVE) != 0, ("Non-active vp %p", vp)); 5194 return (vp); 5195 } 5196 5197 struct vnode * 5198 __mnt_vnode_next_active(struct vnode **mvp, struct mount *mp) 5199 { 5200 5201 if (should_yield()) 5202 kern_yield(PRI_USER); 5203 mtx_lock(&vnode_free_list_mtx); 5204 return (mnt_vnode_next_active(mvp, mp)); 5205 } 5206 5207 struct vnode * 5208 __mnt_vnode_first_active(struct vnode **mvp, struct mount *mp) 5209 { 5210 struct vnode *vp; 5211 5212 *mvp = malloc(sizeof(struct vnode), M_VNODE_MARKER, M_WAITOK | M_ZERO); 5213 MNT_ILOCK(mp); 5214 MNT_REF(mp); 5215 MNT_IUNLOCK(mp); 5216 (*mvp)->v_type = VMARKER; 5217 (*mvp)->v_mount = mp; 5218 5219 mtx_lock(&vnode_free_list_mtx); 5220 vp = TAILQ_FIRST(&mp->mnt_activevnodelist); 5221 if (vp == NULL) { 5222 mtx_unlock(&vnode_free_list_mtx); 5223 mnt_vnode_markerfree_active(mvp, mp); 5224 return (NULL); 5225 } 5226 TAILQ_INSERT_BEFORE(vp, *mvp, v_actfreelist); 5227 return (mnt_vnode_next_active(mvp, mp)); 5228 } 5229 5230 void 5231 __mnt_vnode_markerfree_active(struct vnode **mvp, struct mount *mp) 5232 { 5233 5234 if (*mvp == NULL) 5235 return; 5236 5237 mtx_lock(&vnode_free_list_mtx); 5238 TAILQ_REMOVE(&mp->mnt_activevnodelist, *mvp, v_actfreelist); 5239 mtx_unlock(&vnode_free_list_mtx); 5240 mnt_vnode_markerfree_active(mvp, mp); 5241 } 5242