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