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