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