vfs_subr.c revision 118607
11541Srgrimes/* 21541Srgrimes * Copyright (c) 1989, 1993 31541Srgrimes * The Regents of the University of California. All rights reserved. 41541Srgrimes * (c) UNIX System Laboratories, Inc. 51541Srgrimes * All or some portions of this file are derived from material licensed 61541Srgrimes * to the University of California by American Telephone and Telegraph 71541Srgrimes * Co. or Unix System Laboratories, Inc. and are reproduced herein with 81541Srgrimes * the permission of UNIX System Laboratories, Inc. 91541Srgrimes * 101541Srgrimes * Redistribution and use in source and binary forms, with or without 111541Srgrimes * modification, are permitted provided that the following conditions 121541Srgrimes * are met: 131541Srgrimes * 1. Redistributions of source code must retain the above copyright 141541Srgrimes * notice, this list of conditions and the following disclaimer. 151541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 161541Srgrimes * notice, this list of conditions and the following disclaimer in the 171541Srgrimes * documentation and/or other materials provided with the distribution. 181541Srgrimes * 3. All advertising materials mentioning features or use of this software 191541Srgrimes * must display the following acknowledgement: 201541Srgrimes * This product includes software developed by the University of 211541Srgrimes * California, Berkeley and its contributors. 221541Srgrimes * 4. Neither the name of the University nor the names of its contributors 231541Srgrimes * may be used to endorse or promote products derived from this software 241541Srgrimes * without specific prior written permission. 251541Srgrimes * 261541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 271541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 281541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 291541Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 301541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 311541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 321541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3322521Sdyson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3438408Sbde * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 351541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 361541Srgrimes * SUCH DAMAGE. 3713260Swollman * 3813260Swollman * @(#)vfs_subr.c 8.31 (Berkeley) 5/26/95 391541Srgrimes */ 401541Srgrimes 411541Srgrimes/* 421541Srgrimes * External virtual filesystem routines 431541Srgrimes */ 441541Srgrimes 4538408Sbde#include <sys/cdefs.h> 4612911Sphk__FBSDID("$FreeBSD: head/sys/kern/vfs_subr.c 118607 2003-08-07 15:04:27Z jhb $"); 4738408Sbde 481541Srgrimes#include "opt_ddb.h" 491541Srgrimes#include "opt_mac.h" 501541Srgrimes 511541Srgrimes#include <sys/param.h> 5230474Sphk#include <sys/systm.h> 531541Srgrimes#include <sys/bio.h> 541541Srgrimes#include <sys/buf.h> 551541Srgrimes#include <sys/conf.h> 561541Srgrimes#include <sys/eventhandler.h> 5722521Sdyson#include <sys/extattr.h> 5822521Sdyson#include <sys/fcntl.h> 5912590Sbde#include <sys/kernel.h> 6022521Sdyson#include <sys/kthread.h> 6134266Sjulian#include <sys/mac.h> 6234266Sjulian#include <sys/malloc.h> 6331352Sbde#include <sys/mount.h> 6431352Sbde#include <sys/namei.h> 6531352Sbde#include <sys/stat.h> 6622521Sdyson#include <sys/sysctl.h> 6722521Sdyson#include <sys/syslog.h> 6831351Sbde#include <sys/vmmeter.h> 6931351Sbde#include <sys/vnode.h> 7031351Sbde 7131351Sbde#include <vm/vm.h> 721541Srgrimes#include <vm/vm_object.h> 7322521Sdyson#include <vm/vm_extern.h> 741541Srgrimes#include <vm/pmap.h> 751541Srgrimes#include <vm/vm_map.h> 7612590Sbde#include <vm/vm_page.h> 7722521Sdyson#include <vm/vm_kern.h> 7822521Sdyson#include <vm/uma.h> 7922521Sdyson 801541Srgrimesstatic MALLOC_DEFINE(M_NETADDR, "Export Host", "Export host address structure"); 811541Srgrimes 821541Srgrimesstatic void addalias(struct vnode *vp, dev_t nvp_rdev); 838876Srgrimesstatic void insmntque(struct vnode *vp, struct mount *mp); 841541Srgrimesstatic void vclean(struct vnode *vp, int flags, struct thread *td); 851541Srgrimesstatic void vlruvp(struct vnode *vp); 861541Srgrimesstatic int flushbuflist(struct buf *blist, int flags, struct vnode *vp, 871541Srgrimes int slpflag, int slptimeo, int *errorp); 881541Srgrimesstatic int vcanrecycle(struct vnode *vp, struct mount **vnmpp); 891541Srgrimes 901541Srgrimes 911541Srgrimes/* 921541Srgrimes * Number of vnodes in existence. Increased whenever getnewvnode() 931541Srgrimes * allocates a new vnode, never decreased. 941541Srgrimes */ 951541Srgrimesstatic unsigned long numvnodes; 961541Srgrimes 971541SrgrimesSYSCTL_LONG(_vfs, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0, ""); 981541Srgrimes 991541Srgrimes/* 1001549Srgrimes * Conversion tables for conversion from vnode types to inode formats 1011541Srgrimes * and back. 1021541Srgrimes */ 10322521Sdysonenum vtype iftovt_tab[16] = { 1041541Srgrimes VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON, 1051541Srgrimes VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD, 10622521Sdyson}; 1071541Srgrimesint vttoif_tab[9] = { 1081541Srgrimes 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK, 10922521Sdyson S_IFSOCK, S_IFIFO, S_IFMT, 1106357Sphk}; 1116357Sphk 1126357Sphk/* 1136357Sphk * List of vnodes that are ready for recycling. 1148876Srgrimes */ 1151541Srgrimesstatic TAILQ_HEAD(freelst, vnode) vnode_free_list; 1161541Srgrimes 1171541Srgrimes/* 1181541Srgrimes * Minimum number of free vnodes. If there are fewer than this free vnodes, 1193487Sphk * getnewvnode() will return a newly allocated vnode. 12037555Sbde */ 1211541Srgrimesstatic u_long wantfreevnodes = 25; 1221541SrgrimesSYSCTL_LONG(_vfs, OID_AUTO, wantfreevnodes, CTLFLAG_RW, &wantfreevnodes, 0, ""); 1231541Srgrimes/* Number of vnodes in the free list. */ 1247170Sdgstatic u_long freevnodes; 1251541SrgrimesSYSCTL_LONG(_vfs, OID_AUTO, freevnodes, CTLFLAG_RD, &freevnodes, 0, ""); 1261541Srgrimes 1271541Srgrimes/* 12829609Sphk * Various variables used for debugging the new implementation of 12929609Sphk * reassignbuf(). 1301541Srgrimes * XXX these are probably of (very) limited utility now. 1311541Srgrimes */ 1323487Sphkstatic int reassignbufcalls; 1333487SphkSYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW, &reassignbufcalls, 0, ""); 1341541Srgrimesstatic int nameileafonly; 1351541SrgrimesSYSCTL_INT(_vfs, OID_AUTO, nameileafonly, CTLFLAG_RW, &nameileafonly, 0, ""); 1361541Srgrimes 1371541Srgrimes/* 1381541Srgrimes * Cache for the mount type id assigned to NFS. This is used for 1391541Srgrimes * special checks in nfs/nfs_nqlease.c and vm/vnode_pager.c. 1401541Srgrimes */ 1411541Srgrimesint nfs_mount_type = -1; 14222521Sdyson 14322521Sdyson/* To keep more than one thread at a time from running vfs_getnewfsid */ 1441541Srgrimesstatic struct mtx mntid_mtx; 1451541Srgrimes 1461541Srgrimes/* 1471541Srgrimes * Lock for any access to the following: 1481541Srgrimes * vnode_free_list 1491541Srgrimes * numvnodes 1501541Srgrimes * freevnodes 1511541Srgrimes */ 1521541Srgrimesstatic struct mtx vnode_free_list_mtx; 1531541Srgrimes 1541541Srgrimes/* 1551541Srgrimes * For any iteration/modification of dev->si_hlist (linked through 1561541Srgrimes * v_specnext) 1571541Srgrimes */ 1581541Srgrimesstatic struct mtx spechash_mtx; 1591541Srgrimes 1601541Srgrimes/* Publicly exported FS */ 1611541Srgrimesstruct nfs_public nfs_pub; 1621541Srgrimes 1631541Srgrimes/* Zone for allocation of new vnodes - used exclusively by getnewvnode() */ 1641541Srgrimesstatic uma_zone_t vnode_zone; 1651541Srgrimesstatic uma_zone_t vnodepoll_zone; 1661541Srgrimes 1671541Srgrimes/* Set to 1 to print out reclaim of active vnodes */ 1681541Srgrimesint prtactive; 1691541Srgrimes 1701549Srgrimes/* 1711541Srgrimes * The workitem queue. 1721541Srgrimes * 17322521Sdyson * It is useful to delay writes of file data and filesystem metadata 17422521Sdyson * for tens of seconds so that quickly created and deleted files need 1751541Srgrimes * not waste disk bandwidth being created and removed. To realize this, 1761541Srgrimes * we append vnodes to a "workitem" queue. When running with a soft 1771541Srgrimes * updates implementation, most pending metadata dependencies should 1781541Srgrimes * not wait for more than a few seconds. Thus, mounted on block devices 1791541Srgrimes * are delayed only about a half the time that file data is delayed. 1801541Srgrimes * Similarly, directory updates are more critical, so are only delayed 1811541Srgrimes * about a third the time that file data is delayed. Thus, there are 18222521Sdyson * SYNCER_MAXDELAY queues that are processed round-robin at a rate of 1838876Srgrimes * one each second (driven off the filesystem syncer process). The 1841541Srgrimes * syncer_delayno variable indicates the next queue that is to be processed. 1851541Srgrimes * Items that need to be processed soon are placed in this queue: 1861541Srgrimes * 1871541Srgrimes * syncer_workitem_pending[syncer_delayno] 1881541Srgrimes * 1891541Srgrimes * A delay of fifteen seconds is done by placing the request fifteen 19037555Sbde * entries later in the queue: 19137555Sbde * 1928456Srgrimes * syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask] 1931541Srgrimes * 1941541Srgrimes */ 1951541Srgrimesstatic int syncer_delayno; 1967170Sdgstatic long syncer_mask; 1971541SrgrimesLIST_HEAD(synclist, vnode); 19829609Sphkstatic struct synclist *syncer_workitem_pending; 19929609Sphk/* 2001541Srgrimes * The sync_mtx protects: 2011541Srgrimes * vp->v_synclist 2026357Sphk * syncer_delayno 20337555Sbde * syncer_workitem_pending 20437555Sbde * rushjob 2051541Srgrimes */ 2061541Srgrimesstatic struct mtx sync_mtx; 2071541Srgrimes 2081541Srgrimes#define SYNCER_MAXDELAY 32 2091541Srgrimesstatic int syncer_maxdelay = SYNCER_MAXDELAY; /* maximum delay time */ 2103487Sphkstatic int syncdelay = 30; /* max time to delay syncing data */ 2113487Sphkstatic int filedelay = 30; /* time to delay syncing files */ 2121541SrgrimesSYSCTL_INT(_kern, OID_AUTO, filedelay, CTLFLAG_RW, &filedelay, 0, ""); 2131541Srgrimesstatic int dirdelay = 29; /* time to delay syncing directories */ 2141541SrgrimesSYSCTL_INT(_kern, OID_AUTO, dirdelay, CTLFLAG_RW, &dirdelay, 0, ""); 2156864Sdgstatic int metadelay = 28; /* time to delay syncing metadata */ 2166864SdgSYSCTL_INT(_kern, OID_AUTO, metadelay, CTLFLAG_RW, &metadelay, 0, ""); 2176864Sdgstatic int rushjob; /* number of slots to run ASAP */ 2186864Sdgstatic int stat_rush_requests; /* number of times I/O speeded up */ 2196864SdgSYSCTL_INT(_debug, OID_AUTO, rush_requests, CTLFLAG_RW, &stat_rush_requests, 0, ""); 2206864Sdg 2218876Srgrimes/* 2221541Srgrimes * Number of vnodes we want to exist at any one time. This is mostly used 2233487Sphk * to size hash tables in vnode-related code. It is normally not used in 2243487Sphk * getnewvnode(), as wantfreevnodes is normally nonzero.) 2251541Srgrimes * 2261541Srgrimes * XXX desiredvnodes is historical cruft and should not exist. 2271541Srgrimes */ 2281541Srgrimesint desiredvnodes; 2291541SrgrimesSYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RW, 2301541Srgrimes &desiredvnodes, 0, "Maximum number of vnodes"); 2311541Srgrimesstatic int minvnodes; 2321541SrgrimesSYSCTL_INT(_kern, OID_AUTO, minvnodes, CTLFLAG_RW, 2333487Sphk &minvnodes, 0, "Minimum number of vnodes"); 2343487Sphkstatic int vnlru_nowhere; 2351541SrgrimesSYSCTL_INT(_debug, OID_AUTO, vnlru_nowhere, CTLFLAG_RW, &vnlru_nowhere, 0, 23623560Smpp "Number of times the vnlru process ran without success"); 2371541Srgrimes 2381541Srgrimes/* Hook for calling soft updates */ 2397399Sdgint (*softdep_process_worklist_hook)(struct mount *); 2401541Srgrimes 2411541Srgrimes/* 2421541Srgrimes * This only exists to supress warnings from unlocked specfs accesses. It is 2431541Srgrimes * no longer ok to have an unlocked VFS. 2441541Srgrimes */ 2451541Srgrimes#define IGNORE_LOCK(vp) ((vp)->v_type == VCHR || (vp)->v_type == VBAD) 2461541Srgrimes 2471541Srgrimes/* Print lock violations */ 2481541Srgrimesint vfs_badlock_print = 1; 2491541Srgrimes 2501541Srgrimes/* Panic on violation */ 2511541Srgrimesint vfs_badlock_panic = 1; 2521541Srgrimes 2538876Srgrimes/* Check for interlock across VOPs */ 2548876Srgrimesint vfs_badlock_mutex = 1; 2551541Srgrimes 2561541Srgrimesstatic void 2571541Srgrimesvfs_badlock(char *msg, char *str, struct vnode *vp) 2581541Srgrimes{ 2591541Srgrimes if (vfs_badlock_print) 2606993Sdg printf("%s: %p %s\n", str, vp, msg); 2611541Srgrimes if (vfs_badlock_panic) 2621541Srgrimes Debugger("Lock violation.\n"); 2631541Srgrimes} 2641541Srgrimes 2651541Srgrimesvoid 2661541Srgrimesassert_vi_unlocked(struct vnode *vp, char *str) 2671541Srgrimes{ 2681541Srgrimes if (vfs_badlock_mutex && mtx_owned(VI_MTX(vp))) 2691541Srgrimes vfs_badlock("interlock is locked but should not be", str, vp); 2701541Srgrimes} 2711541Srgrimes 2721541Srgrimesvoid 2731541Srgrimesassert_vi_locked(struct vnode *vp, char *str) 2741541Srgrimes{ 2751541Srgrimes if (vfs_badlock_mutex && !mtx_owned(VI_MTX(vp))) 2761541Srgrimes vfs_badlock("interlock is not locked but should be", str, vp); 2771541Srgrimes} 2781541Srgrimes 2791541Srgrimesvoid 2801541Srgrimesassert_vop_locked(struct vnode *vp, char *str) 2811541Srgrimes{ 2821541Srgrimes if (vp && !IGNORE_LOCK(vp) && !VOP_ISLOCKED(vp, NULL)) 2831541Srgrimes vfs_badlock("is not locked but should be", str, vp); 2841541Srgrimes} 2851541Srgrimes 2861541Srgrimesvoid 2871541Srgrimesassert_vop_unlocked(struct vnode *vp, char *str) 2883487Sphk{ 28937555Sbde if (vp && !IGNORE_LOCK(vp) && 2901541Srgrimes VOP_ISLOCKED(vp, curthread) == LK_EXCLUSIVE) 2911541Srgrimes vfs_badlock("is locked but should not be", str, vp); 2921541Srgrimes} 29322521Sdyson 29422521Sdysonvoid 2951541Srgrimesassert_vop_elocked(struct vnode *vp, char *str) 2961541Srgrimes{ 29734266Sjulian if (vp && !IGNORE_LOCK(vp) && 29834266Sjulian VOP_ISLOCKED(vp, curthread) != LK_EXCLUSIVE) 2991541Srgrimes vfs_badlock("is not exclusive locked but should be", str, vp); 3001541Srgrimes} 3011541Srgrimes 3021541Srgrimesvoid 3031541Srgrimesassert_vop_elocked_other(struct vnode *vp, char *str) 3047399Sdg{ 3051541Srgrimes if (vp && !IGNORE_LOCK(vp) && 3061541Srgrimes VOP_ISLOCKED(vp, curthread) != LK_EXCLOTHER) 3071541Srgrimes vfs_badlock("is not exclusive locked by another thread", 3081541Srgrimes str, vp); 3091541Srgrimes} 3101541Srgrimes 3111541Srgrimesvoid 3121541Srgrimesassert_vop_slocked(struct vnode *vp, char *str) 3131541Srgrimes{ 3141541Srgrimes if (vp && !IGNORE_LOCK(vp) && 3151541Srgrimes VOP_ISLOCKED(vp, curthread) != LK_SHARED) 3161541Srgrimes vfs_badlock("is not locked shared but should be", str, vp); 3171541Srgrimes} 3181541Srgrimes 3191541Srgrimesvoid 3201541Srgrimesvop_rename_pre(void *ap) 3211541Srgrimes{ 3221541Srgrimes struct vop_rename_args *a = ap; 3231541Srgrimes 3241541Srgrimes if (a->a_tvp) 3251541Srgrimes ASSERT_VI_UNLOCKED(a->a_tvp, "VOP_RENAME"); 32631351Sbde ASSERT_VI_UNLOCKED(a->a_tdvp, "VOP_RENAME"); 3271541Srgrimes ASSERT_VI_UNLOCKED(a->a_fvp, "VOP_RENAME"); 3281541Srgrimes ASSERT_VI_UNLOCKED(a->a_fdvp, "VOP_RENAME"); 3291541Srgrimes 3301541Srgrimes /* Check the source (from) */ 3311541Srgrimes if (a->a_tdvp != a->a_fdvp) 3321541Srgrimes ASSERT_VOP_UNLOCKED(a->a_fdvp, "vop_rename: fdvp locked.\n"); 3331541Srgrimes if (a->a_tvp != a->a_fvp) 3341541Srgrimes ASSERT_VOP_UNLOCKED(a->a_fvp, "vop_rename: tvp locked.\n"); 3351541Srgrimes 3361541Srgrimes /* Check the target */ 3371541Srgrimes if (a->a_tvp) 3381541Srgrimes ASSERT_VOP_LOCKED(a->a_tvp, "vop_rename: tvp not locked.\n"); 3391541Srgrimes 3401541Srgrimes ASSERT_VOP_LOCKED(a->a_tdvp, "vop_rename: tdvp not locked.\n"); 34112911Sphk} 34222521Sdyson 34322521Sdysonvoid 34431352Sbdevop_strategy_pre(void *ap) 34522521Sdyson{ 34622521Sdyson struct vop_strategy_args *a = ap; 34731351Sbde struct buf *bp; 34831351Sbde 34931351Sbde bp = a->a_bp; 3501541Srgrimes 3511541Srgrimes /* 3521541Srgrimes * Cluster ops lock their component buffers but not the IO container. 3531541Srgrimes */ 3541541Srgrimes if ((bp->b_flags & B_CLUSTER) != 0) 3551541Srgrimes return; 3561541Srgrimes 35712911Sphk if (BUF_REFCNT(bp) < 1) { 35812405Sdyson if (vfs_badlock_print) 35912405Sdyson printf("VOP_STRATEGY: bp is not locked but should be.\n"); 3601541Srgrimes if (vfs_badlock_panic) 3611541Srgrimes Debugger("Lock violation.\n"); 3621541Srgrimes } 3631541Srgrimes} 36422521Sdyson 3651541Srgrimesvoid 36622521Sdysonvop_lookup_pre(void *ap) 3671541Srgrimes{ 3681541Srgrimes struct vop_lookup_args *a = ap; 36910269Sbde struct vnode *dvp; 3701541Srgrimes 37122521Sdyson dvp = a->a_dvp; 37222521Sdyson 3731541Srgrimes ASSERT_VI_UNLOCKED(dvp, "VOP_LOOKUP"); 3741541Srgrimes ASSERT_VOP_LOCKED(dvp, "VOP_LOOKUP"); 3751541Srgrimes} 3761541Srgrimes 3771541Srgrimesvoid 3781541Srgrimesvop_lookup_post(void *ap, int rc) 3791541Srgrimes{ 3801541Srgrimes struct vop_lookup_args *a = ap; 3811541Srgrimes struct componentname *cnp; 3821541Srgrimes struct vnode *dvp; 38322521Sdyson struct vnode *vp; 38422521Sdyson int flags; 38522521Sdyson 38622521Sdyson dvp = a->a_dvp; 3871541Srgrimes cnp = a->a_cnp; 3881541Srgrimes vp = *(a->a_vpp); 38922521Sdyson flags = cnp->cn_flags; 39022521Sdyson 39122521Sdyson 39222521Sdyson ASSERT_VI_UNLOCKED(dvp, "VOP_LOOKUP"); 39322521Sdyson /* 39422521Sdyson * If this is the last path component for this lookup and LOCPARENT 3951541Srgrimes * is set, OR if there is an error the directory has to be locked. 3961541Srgrimes */ 3971541Srgrimes if ((flags & LOCKPARENT) && (flags & ISLASTCN)) 3981541Srgrimes ASSERT_VOP_LOCKED(dvp, "VOP_LOOKUP (LOCKPARENT)"); 3991541Srgrimes else if (rc != 0) 4001541Srgrimes ASSERT_VOP_LOCKED(dvp, "VOP_LOOKUP (error)"); 4011541Srgrimes else if (dvp != vp) 4021541Srgrimes ASSERT_VOP_UNLOCKED(dvp, "VOP_LOOKUP (dvp)"); 4031541Srgrimes 4041541Srgrimes if (flags & PDIRUNLOCK) 4051541Srgrimes ASSERT_VOP_UNLOCKED(dvp, "VOP_LOOKUP (PDIRUNLOCK)"); 4061541Srgrimes} 4071541Srgrimes 4081541Srgrimesvoid 4091541Srgrimesvop_unlock_pre(void *ap) 4101541Srgrimes{ 4111541Srgrimes struct vop_unlock_args *a = ap; 4121541Srgrimes 4131541Srgrimes if (a->a_flags & LK_INTERLOCK) 4141541Srgrimes ASSERT_VI_LOCKED(a->a_vp, "VOP_UNLOCK"); 4151541Srgrimes 4161541Srgrimes ASSERT_VOP_LOCKED(a->a_vp, "VOP_UNLOCK"); 4171541Srgrimes} 4181541Srgrimes 41922521Sdysonvoid 4201541Srgrimesvop_unlock_post(void *ap, int rc) 4211541Srgrimes{ 4221541Srgrimes struct vop_unlock_args *a = ap; 4231541Srgrimes 4241541Srgrimes if (a->a_flags & LK_INTERLOCK) 4251541Srgrimes ASSERT_VI_UNLOCKED(a->a_vp, "VOP_UNLOCK"); 4261541Srgrimes} 4271541Srgrimes 4281541Srgrimesvoid 4291541Srgrimesvop_lock_pre(void *ap) 4301541Srgrimes{ 4311541Srgrimes struct vop_lock_args *a = ap; 4321541Srgrimes 4331541Srgrimes if ((a->a_flags & LK_INTERLOCK) == 0) 4341541Srgrimes ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK"); 4351541Srgrimes else 4361541Srgrimes ASSERT_VI_LOCKED(a->a_vp, "VOP_LOCK"); 4371541Srgrimes} 4381541Srgrimes 43922521Sdysonvoid 4401541Srgrimesvop_lock_post(void *ap, int rc) 4411541Srgrimes{ 4421541Srgrimes struct vop_lock_args *a; 4431541Srgrimes 44422521Sdyson a = ap; 44512590Sbde 4461541Srgrimes ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK"); 4471541Srgrimes if (rc == 0) 4481541Srgrimes ASSERT_VOP_LOCKED(a->a_vp, "VOP_LOCK"); 4491541Srgrimes} 4501541Srgrimes 4511541Srgrimesvoid 4521541Srgrimesv_addpollinfo(struct vnode *vp) 4531541Srgrimes{ 45422521Sdyson vp->v_pollinfo = uma_zalloc(vnodepoll_zone, M_WAITOK); 45522521Sdyson mtx_init(&vp->v_pollinfo->vpi_lock, "vnode pollinfo", NULL, MTX_DEF); 45622521Sdyson} 45722521Sdyson 45822521Sdyson/* 4591541Srgrimes * Initialize the vnode management data structures. 4601541Srgrimes */ 46134266Sjulianstatic void 4621541Srgrimesvntblinit(void *dummy __unused) 46334266Sjulian{ 46434266Sjulian 4651541Srgrimes /* 46622521Sdyson * Desiredvnodes is a function of the physical memory size and 46722521Sdyson * the kernel's heap size. Specifically, desiredvnodes scales 46822521Sdyson * in proportion to the physical memory size until two fifths 46922521Sdyson * of the kernel's heap size is consumed by vnodes and vm 4701541Srgrimes * objects. 4711541Srgrimes */ 47222521Sdyson desiredvnodes = min(maxproc + cnt.v_page_count / 4, 2 * vm_kmem_size / 47322521Sdyson (5 * (sizeof(struct vm_object) + sizeof(struct vnode)))); 47422521Sdyson minvnodes = desiredvnodes / 4; 47522521Sdyson mtx_init(&mountlist_mtx, "mountlist", NULL, MTX_DEF); 47634266Sjulian mtx_init(&mntvnode_mtx, "mntvnode", NULL, MTX_DEF); 47734266Sjulian mtx_init(&mntid_mtx, "mntid", NULL, MTX_DEF); 47834266Sjulian mtx_init(&spechash_mtx, "spechash", NULL, MTX_DEF); 47934266Sjulian TAILQ_INIT(&vnode_free_list); 48034266Sjulian mtx_init(&vnode_free_list_mtx, "vnode_free_list", NULL, MTX_DEF); 48134266Sjulian vnode_zone = uma_zcreate("VNODE", sizeof (struct vnode), NULL, NULL, 48234266Sjulian NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 48334266Sjulian vnodepoll_zone = uma_zcreate("VNODEPOLL", sizeof (struct vpollinfo), 48434266Sjulian NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 48534266Sjulian /* 4861541Srgrimes * Initialize the filesystem syncer. 4871541Srgrimes */ 4881541Srgrimes syncer_workitem_pending = hashinit(syncer_maxdelay, M_VNODE, 4891541Srgrimes &syncer_mask); 4901541Srgrimes syncer_maxdelay = syncer_mask + 1; 4911541Srgrimes mtx_init(&sync_mtx, "Syncer mtx", NULL, MTX_DEF); 4928876Srgrimes} 4931541SrgrimesSYSINIT(vfs, SI_SUB_VFS, SI_ORDER_FIRST, vntblinit, NULL) 4941541Srgrimes 4951541Srgrimes 4961541Srgrimes/* 4971541Srgrimes * Mark a mount point as busy. Used to synchronize access and to delay 4981541Srgrimes * unmounting. Interlock is not released on failure. 4991541Srgrimes */ 5001541Srgrimesint 5011541Srgrimesvfs_busy(mp, flags, interlkp, td) 5021541Srgrimes struct mount *mp; 5031541Srgrimes int flags; 5041541Srgrimes struct mtx *interlkp; 5051541Srgrimes struct thread *td; 5061541Srgrimes{ 5071541Srgrimes int lkflags; 5081541Srgrimes 50910269Sbde if (mp->mnt_kern_flag & MNTK_UNMOUNT) { 51024101Sbde if (flags & LK_NOWAIT) 51130492Sphk return (ENOENT); 51210269Sbde mp->mnt_kern_flag |= MNTK_MWAIT; 5131541Srgrimes /* 5141541Srgrimes * Since all busy locks are shared except the exclusive 5151541Srgrimes * lock granted when unmounting, the only place that a 5161541Srgrimes * wakeup needs to be done is at the release of the 5171541Srgrimes * exclusive lock at the end of dounmount. 5181541Srgrimes */ 5191541Srgrimes msleep(mp, interlkp, PVFS, "vfs_busy", 0); 5201541Srgrimes return (ENOENT); 5211541Srgrimes } 52222521Sdyson lkflags = LK_SHARED | LK_NOPAUSE; 52322521Sdyson if (interlkp) 52422521Sdyson lkflags |= LK_INTERLOCK; 52522521Sdyson if (lockmgr(&mp->mnt_lock, lkflags, interlkp, td)) 5261541Srgrimes panic("vfs_busy: unexpected lock failure"); 52734266Sjulian return (0); 52834266Sjulian} 52934266Sjulian 53034266Sjulian/* 5311541Srgrimes * Free a busy filesystem. 53222521Sdyson */ 53322521Sdysonvoid 53422521Sdysonvfs_unbusy(mp, td) 53522521Sdyson struct mount *mp; 53622521Sdyson struct thread *td; 53722521Sdyson{ 53822521Sdyson 5391541Srgrimes lockmgr(&mp->mnt_lock, LK_RELEASE, NULL, td); 54022521Sdyson} 54122521Sdyson 54222521Sdyson/* 54322521Sdyson * Lookup a mount point by filesystem identifier. 54422521Sdyson */ 54522521Sdysonstruct mount * 5461541Srgrimesvfs_getvfs(fsid) 5471541Srgrimes fsid_t *fsid; 5481541Srgrimes{ 5491541Srgrimes register struct mount *mp; 5501541Srgrimes 5511541Srgrimes mtx_lock(&mountlist_mtx); 5521541Srgrimes TAILQ_FOREACH(mp, &mountlist, mnt_list) { 5531541Srgrimes if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] && 55412405Sdyson mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) { 5551541Srgrimes mtx_unlock(&mountlist_mtx); 5561541Srgrimes return (mp); 5571541Srgrimes } 5581541Srgrimes } 5598876Srgrimes mtx_unlock(&mountlist_mtx); 5601541Srgrimes return ((struct mount *) 0); 5611541Srgrimes} 5621541Srgrimes 5631541Srgrimes/* 5641541Srgrimes * Get a new unique fsid. Try to make its val[0] unique, since this value 5651541Srgrimes * will be used to create fake device numbers for stat(). Also try (but 5661541Srgrimes * not so hard) make its val[0] unique mod 2^16, since some emulators only 5671541Srgrimes * support 16-bit device numbers. We end up with unique val[0]'s for the 5681541Srgrimes * first 2^16 calls and unique val[0]'s mod 2^16 for the first 2^8 calls. 5691541Srgrimes * 5701541Srgrimes * Keep in mind that several mounts may be running in parallel. Starting 5711541Srgrimes * the search one past where the previous search terminated is both a 5721549Srgrimes * micro-optimization and a defense against returning the same fsid to 57330474Sphk * different mounts. 57430474Sphk */ 57530474Sphkvoid 57630474Sphkvfs_getnewfsid(mp) 57730474Sphk struct mount *mp; 5781541Srgrimes{ 5791541Srgrimes static u_int16_t mntid_base; 5801541Srgrimes fsid_t tfsid; 5811541Srgrimes int mtype; 5821541Srgrimes 5831541Srgrimes mtx_lock(&mntid_mtx); 5848876Srgrimes mtype = mp->mnt_vfc->vfc_typenum; 58530474Sphk tfsid.val[1] = mtype; 5861541Srgrimes mtype = (mtype & 0xFF) << 24; 5871541Srgrimes for (;;) { 5881541Srgrimes tfsid.val[0] = makeudev(255, 5891541Srgrimes mtype | ((mntid_base & 0xFF00) << 8) | (mntid_base & 0xFF)); 5901541Srgrimes mntid_base++; 5911541Srgrimes if (vfs_getvfs(&tfsid) == NULL) 5921541Srgrimes break; 5931541Srgrimes } 5941541Srgrimes mp->mnt_stat.f_fsid.val[0] = tfsid.val[0]; 5951541Srgrimes mp->mnt_stat.f_fsid.val[1] = tfsid.val[1]; 5961541Srgrimes mtx_unlock(&mntid_mtx); 5971541Srgrimes} 59812861Speter 59912861Speter/* 6001541Srgrimes * Knob to control the precision of file timestamps: 6011541Srgrimes * 60230474Sphk * 0 = seconds only; nanoseconds zeroed. 6031541Srgrimes * 1 = seconds and nanoseconds, accurate within 1/HZ. 60430474Sphk * 2 = seconds and nanoseconds, truncated to microseconds. 6051541Srgrimes * >=3 = seconds and nanoseconds, maximum precision. 6061541Srgrimes */ 60730474Sphkenum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC }; 6081541Srgrimes 60937555Sbdestatic int timestamp_precision = TSP_SEC; 61037555SbdeSYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW, 6111541Srgrimes ×tamp_precision, 0, ""); 6121541Srgrimes 6131541Srgrimes/* 61437555Sbde * Get a current timestamp. 61537555Sbde */ 6161541Srgrimesvoid 6171541Srgrimesvfs_timestamp(tsp) 6181541Srgrimes struct timespec *tsp; 6191541Srgrimes{ 6201541Srgrimes struct timeval tv; 6211541Srgrimes 62231484Sbde switch (timestamp_precision) { 62324149Sguido case TSP_SEC: 6241541Srgrimes tsp->tv_sec = time_second; 6251541Srgrimes tsp->tv_nsec = 0; 62630474Sphk break; 6271541Srgrimes case TSP_HZ: 6281541Srgrimes getnanotime(tsp); 6291541Srgrimes break; 6301541Srgrimes case TSP_USEC: 6311541Srgrimes microtime(&tv); 6321541Srgrimes TIMEVAL_TO_TIMESPEC(&tv, tsp); 6331541Srgrimes break; 6341541Srgrimes case TSP_NSEC: 6351541Srgrimes default: 6361541Srgrimes nanotime(tsp); 6371541Srgrimes break; 6381541Srgrimes } 6391541Srgrimes} 6401541Srgrimes 6411541Srgrimes/* 6421541Srgrimes * Set vnode attributes to VNOVAL 6431541Srgrimes */ 6441541Srgrimesvoid 6451541Srgrimesvattr_null(vap) 6461541Srgrimes register struct vattr *vap; 6471541Srgrimes{ 6481541Srgrimes 6491541Srgrimes vap->va_type = VNON; 6501541Srgrimes vap->va_size = VNOVAL; 6511541Srgrimes vap->va_bytes = VNOVAL; 6521541Srgrimes vap->va_mode = VNOVAL; 6531541Srgrimes vap->va_nlink = VNOVAL; 6541541Srgrimes vap->va_uid = VNOVAL; 6551541Srgrimes vap->va_gid = VNOVAL; 6561541Srgrimes vap->va_fsid = VNOVAL; 6571541Srgrimes vap->va_fileid = VNOVAL; 6581541Srgrimes vap->va_blocksize = VNOVAL; 6591541Srgrimes vap->va_rdev = VNOVAL; 6608876Srgrimes vap->va_atime.tv_sec = VNOVAL; 6611541Srgrimes vap->va_atime.tv_nsec = VNOVAL; 6621541Srgrimes vap->va_mtime.tv_sec = VNOVAL; 6631541Srgrimes vap->va_mtime.tv_nsec = VNOVAL; 6641541Srgrimes vap->va_ctime.tv_sec = VNOVAL; 6651541Srgrimes vap->va_ctime.tv_nsec = VNOVAL; 6661541Srgrimes vap->va_birthtime.tv_sec = VNOVAL; 6671541Srgrimes vap->va_birthtime.tv_nsec = VNOVAL; 6681541Srgrimes vap->va_flags = VNOVAL; 6691541Srgrimes vap->va_gen = VNOVAL; 6701541Srgrimes vap->va_vaflags = 0; 6711541Srgrimes} 6721541Srgrimes 6731541Srgrimes/* 6748876Srgrimes * This routine is called when we have too many vnodes. It attempts 6751541Srgrimes * to free <count> vnodes and will potentially free vnodes that still 6761541Srgrimes * have VM backing store (VM backing store is typically the cause 6771541Srgrimes * of a vnode blowout so we want to do this). Therefore, this operation 6781541Srgrimes * is not considered cheap. 6791541Srgrimes * 6801541Srgrimes * A number of conditions may prevent a vnode from being reclaimed. 6811541Srgrimes * the buffer cache may have references on the vnode, a directory 68222521Sdyson * vnode may still have references due to the namei cache representing 6831541Srgrimes * underlying files, or the vnode may be in active use. It is not 6841541Srgrimes * desireable to reuse such vnodes. These conditions may cause the 68522521Sdyson * number of vnodes to reach some minimum value regardless of what 6861541Srgrimes * you set kern.maxvnodes to. Do not set kern.maxvnodes too low. 68722521Sdyson */ 6881541Srgrimesstatic int 6891541Srgrimesvlrureclaim(struct mount *mp) 6901541Srgrimes{ 6911541Srgrimes struct vnode *vp; 69222521Sdyson int done; 6931541Srgrimes int trigger; 6941541Srgrimes int usevnodes; 6951541Srgrimes int count; 6961541Srgrimes 6971541Srgrimes /* 6981541Srgrimes * Calculate the trigger point, don't allow user 6991541Srgrimes * screwups to blow us up. This prevents us from 7001541Srgrimes * recycling vnodes with lots of resident pages. We 7011541Srgrimes * aren't trying to free memory, we are trying to 7021541Srgrimes * free vnodes. 7031541Srgrimes */ 7041541Srgrimes usevnodes = desiredvnodes; 7051541Srgrimes if (usevnodes <= 0) 7061541Srgrimes usevnodes = 1; 7071541Srgrimes trigger = cnt.v_page_count * 2 / usevnodes; 7081541Srgrimes 7091541Srgrimes done = 0; 7101541Srgrimes mtx_lock(&mntvnode_mtx); 7111541Srgrimes count = mp->mnt_nvnodelistsize / 10 + 1; 7121541Srgrimes while (count && (vp = TAILQ_FIRST(&mp->mnt_nvnodelist)) != NULL) { 7131541Srgrimes TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes); 7141541Srgrimes TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes); 7151541Srgrimes 7161541Srgrimes if (vp->v_type != VNON && 7171541Srgrimes vp->v_type != VBAD && 7181541Srgrimes VI_TRYLOCK(vp)) { 7191541Srgrimes if (VMIGHTFREE(vp) && /* critical path opt */ 7201541Srgrimes (vp->v_object == NULL || 72117108Sbde vp->v_object->resident_page_count < trigger)) { 7221541Srgrimes mtx_unlock(&mntvnode_mtx); 7231541Srgrimes vgonel(vp, curthread); 7241541Srgrimes done++; 7251541Srgrimes mtx_lock(&mntvnode_mtx); 7261541Srgrimes } else 7271541Srgrimes VI_UNLOCK(vp); 7281541Srgrimes } 7291541Srgrimes --count; 73010632Sdg } 73110632Sdg mtx_unlock(&mntvnode_mtx); 7321541Srgrimes return done; 7331541Srgrimes} 73410632Sdg 73510632Sdg/* 73610632Sdg * Attempt to recycle vnodes in a context that is always safe to block. 73710632Sdg * Calling vlrurecycle() from the bowels of filesystem code has some 73810632Sdg * interesting deadlock problems. 73910632Sdg */ 74010632Sdgstatic struct proc *vnlruproc; 74110632Sdgstatic int vnlruproc_sig; 7421541Srgrimes 7431541Srgrimesstatic void 7441541Srgrimesvnlru_proc(void) 7451541Srgrimes{ 7461541Srgrimes struct mount *mp, *nmp; 7471541Srgrimes int s; 7481541Srgrimes int done; 7491541Srgrimes struct proc *p = vnlruproc; 7501541Srgrimes struct thread *td = FIRST_THREAD_IN_PROC(p); /* XXXKSE */ 7511541Srgrimes 7521541Srgrimes mtx_lock(&Giant); 7531541Srgrimes 7541541Srgrimes EVENTHANDLER_REGISTER(shutdown_pre_sync, kproc_shutdown, p, 7551541Srgrimes SHUTDOWN_PRI_FIRST); 7561541Srgrimes 7571541Srgrimes s = splbio(); 7581541Srgrimes for (;;) { 7591541Srgrimes kthread_suspend_check(p); 76012590Sbde mtx_lock(&vnode_free_list_mtx); 7611541Srgrimes if (numvnodes - freevnodes <= desiredvnodes * 9 / 10) { 7621541Srgrimes mtx_unlock(&vnode_free_list_mtx); 76312590Sbde vnlruproc_sig = 0; 7641541Srgrimes wakeup(&vnlruproc_sig); 7651541Srgrimes tsleep(vnlruproc, PVFS, "vlruwt", hz); 7661541Srgrimes continue; 7671541Srgrimes } 7681541Srgrimes mtx_unlock(&vnode_free_list_mtx); 7691541Srgrimes done = 0; 7701541Srgrimes mtx_lock(&mountlist_mtx); 7711541Srgrimes for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) { 7721541Srgrimes if (vfs_busy(mp, LK_NOWAIT, &mountlist_mtx, td)) { 7731541Srgrimes nmp = TAILQ_NEXT(mp, mnt_list); 7741541Srgrimes continue; 7751541Srgrimes } 7761541Srgrimes done += vlrureclaim(mp); 7771541Srgrimes mtx_lock(&mountlist_mtx); 7781541Srgrimes nmp = TAILQ_NEXT(mp, mnt_list); 7791541Srgrimes vfs_unbusy(mp, td); 7801541Srgrimes } 7811541Srgrimes mtx_unlock(&mountlist_mtx); 7821541Srgrimes if (done == 0) { 7831541Srgrimes#if 0 7841541Srgrimes /* These messages are temporary debugging aids */ 7851541Srgrimes if (vnlru_nowhere < 5) 7861541Srgrimes printf("vnlru process getting nowhere..\n"); 7871541Srgrimes else if (vnlru_nowhere == 5) 7881541Srgrimes printf("vnlru process messages stopped.\n"); 7891541Srgrimes#endif 7901541Srgrimes vnlru_nowhere++; 7911541Srgrimes tsleep(vnlruproc, PPAUSE, "vlrup", hz * 3); 7921541Srgrimes } 7931541Srgrimes } 7941541Srgrimes splx(s); 7951541Srgrimes} 7961541Srgrimes 7971541Srgrimesstatic struct kproc_desc vnlru_kp = { 79812590Sbde "vnlru", 7991541Srgrimes vnlru_proc, 8001541Srgrimes &vnlruproc 8011541Srgrimes}; 8021541SrgrimesSYSINIT(vnlru, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &vnlru_kp) 8031541Srgrimes 8048876Srgrimes 8051541Srgrimes/* 8061541Srgrimes * Routines having to do with the management of the vnode table. 80722521Sdyson */ 8081541Srgrimes 8091541Srgrimes/* 8101541Srgrimes * Check to see if a free vnode can be recycled. If it can, 8111541Srgrimes * return it locked with the vn lock, but not interlock. Also 8121541Srgrimes * get the vn_start_write lock. Otherwise indicate the error. 8131541Srgrimes */ 8141541Srgrimesstatic int 8151541Srgrimesvcanrecycle(struct vnode *vp, struct mount **vnmpp) 8161541Srgrimes{ 8171541Srgrimes struct thread *td = curthread; 8181541Srgrimes vm_object_t object; 8191541Srgrimes int error; 8201541Srgrimes 8211541Srgrimes /* Don't recycle if we can't get the interlock */ 8221541Srgrimes if (!VI_TRYLOCK(vp)) 82317108Sbde return (EWOULDBLOCK); 8241541Srgrimes 8251541Srgrimes /* We should be able to immediately acquire this */ 8261541Srgrimes /* XXX This looks like it should panic if it fails */ 8271541Srgrimes if (vn_lock(vp, LK_INTERLOCK | LK_EXCLUSIVE, td) != 0) { 82817108Sbde if (VOP_ISLOCKED(vp, td)) 8291541Srgrimes panic("vcanrecycle: locked vnode"); 8301541Srgrimes return (EWOULDBLOCK); 8311541Srgrimes } 8321541Srgrimes 8331541Srgrimes /* 83417108Sbde * Don't recycle if its filesystem is being suspended. 8351541Srgrimes */ 8361541Srgrimes if (vn_start_write(vp, vnmpp, V_NOWAIT) != 0) { 8371541Srgrimes error = EBUSY; 8381541Srgrimes goto done; 83917108Sbde } 8401541Srgrimes 84134961Sphk /* 8421541Srgrimes * Don't recycle if we still have cached pages. 8431541Srgrimes */ 8441541Srgrimes if (VOP_GETVOBJECT(vp, &object) == 0) { 8451541Srgrimes VM_OBJECT_LOCK(object); 84617108Sbde if (object->resident_page_count || 8471541Srgrimes object->ref_count) { 8481541Srgrimes VM_OBJECT_UNLOCK(object); 8491541Srgrimes error = EBUSY; 8501541Srgrimes goto done; 8511541Srgrimes } 8521541Srgrimes VM_OBJECT_UNLOCK(object); 8531541Srgrimes } 8541541Srgrimes if (LIST_FIRST(&vp->v_cache_src)) { 8551541Srgrimes /* 8561541Srgrimes * note: nameileafonly sysctl is temporary, 8571541Srgrimes * for debugging only, and will eventually be 8581541Srgrimes * removed. 8591541Srgrimes */ 8601541Srgrimes if (nameileafonly > 0) { 8611541Srgrimes /* 8621541Srgrimes * Do not reuse namei-cached directory 8631541Srgrimes * vnodes that have cached 8641541Srgrimes * subdirectories. 8651541Srgrimes */ 8661541Srgrimes if (cache_leaf_test(vp) < 0) { 86734266Sjulian error = EISDIR; 86834266Sjulian goto done; 8691541Srgrimes } 8701541Srgrimes } else if (nameileafonly < 0 || 8711541Srgrimes vmiodirenable == 0) { 8721541Srgrimes /* 8731541Srgrimes * Do not reuse namei-cached directory 8741541Srgrimes * vnodes if nameileafonly is -1 or 8751541Srgrimes * if VMIO backing for directories is 8761541Srgrimes * turned off (otherwise we reuse them 8771541Srgrimes * too quickly). 8781541Srgrimes */ 87922521Sdyson error = EBUSY; 8801541Srgrimes goto done; 8811541Srgrimes } 8821541Srgrimes } 88322521Sdyson return (0); 8841541Srgrimesdone: 8851541Srgrimes VOP_UNLOCK(vp, 0, td); 8861541Srgrimes return (error); 8871541Srgrimes} 8881541Srgrimes 8891541Srgrimes/* 89034266Sjulian * Return the next vnode from the free list. 89134266Sjulian */ 8921541Srgrimesint 8931541Srgrimesgetnewvnode(tag, mp, vops, vpp) 8941541Srgrimes const char *tag; 89517108Sbde struct mount *mp; 8961541Srgrimes vop_t **vops; 8971541Srgrimes struct vnode **vpp; 8981541Srgrimes{ 8991541Srgrimes struct thread *td = curthread; /* XXX */ 90017108Sbde struct vnode *vp = NULL; 9011541Srgrimes struct vpollinfo *pollinfo = NULL; 9021541Srgrimes struct mount *vnmp; 9031541Srgrimes 9041541Srgrimes mtx_lock(&vnode_free_list_mtx); 9051541Srgrimes 90617108Sbde /* 9071541Srgrimes * Try to reuse vnodes if we hit the max. This situation only 90834961Sphk * occurs in certain large-memory (2G+) situations. We cannot 9091541Srgrimes * attempt to directly reclaim vnodes due to nasty recursion 91034266Sjulian * problems. 9111541Srgrimes */ 9121541Srgrimes while (numvnodes - freevnodes > desiredvnodes) { 9131541Srgrimes if (vnlruproc_sig == 0) { 9141541Srgrimes vnlruproc_sig = 1; /* avoid unnecessary wakeups */ 9151541Srgrimes wakeup(vnlruproc); 9161541Srgrimes } 9171541Srgrimes mtx_unlock(&vnode_free_list_mtx); 9181541Srgrimes tsleep(&vnlruproc_sig, PVFS, "vlruwk", hz); 9191541Srgrimes mtx_lock(&vnode_free_list_mtx); 9201541Srgrimes } 9211541Srgrimes 9221541Srgrimes /* 9231541Srgrimes * Attempt to reuse a vnode already on the free list, allocating 9241541Srgrimes * a new vnode if we can't find one or if we have not reached a 9258876Srgrimes * good minimum for good LRU performance. 9261541Srgrimes */ 9271541Srgrimes 9281541Srgrimes if (freevnodes >= wantfreevnodes && numvnodes >= minvnodes) { 9291541Srgrimes int error; 93017108Sbde int count; 9311541Srgrimes 93234266Sjulian for (count = 0; count < freevnodes; count++) { 9331541Srgrimes vp = TAILQ_FIRST(&vnode_free_list); 9341541Srgrimes 9351541Srgrimes KASSERT(vp->v_usecount == 0 && 9361541Srgrimes (vp->v_iflag & VI_DOINGINACT) == 0, 9371541Srgrimes ("getnewvnode: free vnode isn't")); 9381541Srgrimes 9391541Srgrimes TAILQ_REMOVE(&vnode_free_list, vp, v_freelist); 9401541Srgrimes /* 9411541Srgrimes * We have to drop the free list mtx to avoid lock 9421541Srgrimes * order reversals with interlock. 9431541Srgrimes */ 9441541Srgrimes mtx_unlock(&vnode_free_list_mtx); 9451541Srgrimes error = vcanrecycle(vp, &vnmp); 9461541Srgrimes mtx_lock(&vnode_free_list_mtx); 9471541Srgrimes if (error == 0) 94817108Sbde break; 9491541Srgrimes TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist); 9501541Srgrimes vp = NULL; 9511541Srgrimes } 9521541Srgrimes } 9531541Srgrimes if (vp) { 9541541Srgrimes freevnodes--; 9551541Srgrimes mtx_unlock(&vnode_free_list_mtx); 9561541Srgrimes 9571541Srgrimes cache_purge(vp); 9581541Srgrimes VI_LOCK(vp); 95934266Sjulian vp->v_iflag |= VI_DOOMED; 96034266Sjulian vp->v_iflag &= ~VI_FREE; 96134266Sjulian if (vp->v_type != VBAD) { 9621541Srgrimes VOP_UNLOCK(vp, 0, td); 96334266Sjulian vgonel(vp, td); 9641541Srgrimes VI_LOCK(vp); 9651541Srgrimes } else { 9661541Srgrimes VOP_UNLOCK(vp, 0, td); 9671541Srgrimes } 9681541Srgrimes vn_finished_write(vnmp); 9691541Srgrimes 9701541Srgrimes#ifdef INVARIANTS 9711541Srgrimes { 9721541Srgrimes if (vp->v_data) 9731541Srgrimes panic("cleaned vnode isn't"); 9741541Srgrimes if (vp->v_numoutput) 9751541Srgrimes panic("Clean vnode has pending I/O's"); 9761541Srgrimes if (vp->v_writecount != 0) 97722521Sdyson panic("Non-zero write count"); 97834266Sjulian } 97934266Sjulian#endif 98034266Sjulian if ((pollinfo = vp->v_pollinfo) != NULL) { 98122521Sdyson /* 9821541Srgrimes * To avoid lock order reversals, the call to 98334266Sjulian * uma_zfree() must be delayed until the vnode 98434266Sjulian * interlock is released. 98522521Sdyson */ 9861541Srgrimes vp->v_pollinfo = NULL; 9871541Srgrimes } 9881541Srgrimes#ifdef MAC 9891541Srgrimes mac_destroy_vnode(vp); 99034266Sjulian#endif 99134266Sjulian vp->v_iflag = 0; 9921541Srgrimes vp->v_vflag = 0; 9931541Srgrimes vp->v_lastw = 0; 9941541Srgrimes vp->v_lasta = 0; 9951541Srgrimes vp->v_cstart = 0; 9961541Srgrimes vp->v_clen = 0; 9971541Srgrimes vp->v_socket = 0; 9981541Srgrimes lockdestroy(vp->v_vnlock); 9991541Srgrimes lockinit(vp->v_vnlock, PVFS, tag, VLKTIMEOUT, LK_NOPAUSE); 10001541Srgrimes KASSERT(vp->v_cleanbufcnt == 0, ("cleanbufcnt not 0")); 10011541Srgrimes KASSERT(vp->v_cleanblkroot == NULL, ("cleanblkroot not NULL")); 10021541Srgrimes KASSERT(vp->v_dirtybufcnt == 0, ("dirtybufcnt not 0")); 10031541Srgrimes KASSERT(vp->v_dirtyblkroot == NULL, ("dirtyblkroot not NULL")); 10041541Srgrimes } else { 10056769Sse numvnodes++; 10061541Srgrimes mtx_unlock(&vnode_free_list_mtx); 10071541Srgrimes 10081541Srgrimes vp = (struct vnode *) uma_zalloc(vnode_zone, M_WAITOK|M_ZERO); 10098876Srgrimes mtx_init(&vp->v_interlock, "vnode interlock", NULL, MTX_DEF); 10101541Srgrimes VI_LOCK(vp); 10111541Srgrimes vp->v_dd = vp; 10121541Srgrimes vp->v_vnlock = &vp->v_lock; 10131541Srgrimes lockinit(vp->v_vnlock, PVFS, tag, VLKTIMEOUT, LK_NOPAUSE); 10141541Srgrimes cache_purge(vp); 10151541Srgrimes LIST_INIT(&vp->v_cache_src); 10161541Srgrimes TAILQ_INIT(&vp->v_cache_dst); 10176769Sse } 10186769Sse 10196769Sse TAILQ_INIT(&vp->v_cleanblkhd); 10206769Sse TAILQ_INIT(&vp->v_dirtyblkhd); 10216769Sse vp->v_type = VNON; 10226769Sse vp->v_tag = tag; 10238876Srgrimes vp->v_op = vops; 10241541Srgrimes *vpp = vp; 10251541Srgrimes vp->v_usecount = 1; 10261541Srgrimes vp->v_data = 0; 10271541Srgrimes vp->v_cachedid = -1; 10281541Srgrimes VI_UNLOCK(vp); 10291541Srgrimes if (pollinfo != NULL) { 10301541Srgrimes mtx_destroy(&pollinfo->vpi_lock); 10311541Srgrimes uma_zfree(vnodepoll_zone, pollinfo); 10321541Srgrimes } 10331541Srgrimes#ifdef MAC 10341541Srgrimes mac_init_vnode(vp); 10351541Srgrimes if (mp != NULL && (mp->mnt_flag & MNT_MULTILABEL) == 0) 10361541Srgrimes mac_associate_vnode_singlelabel(mp, vp); 10371541Srgrimes#endif 10381541Srgrimes insmntque(vp, mp); 10391541Srgrimes 10401541Srgrimes return (0); 10411541Srgrimes} 10421541Srgrimes 10431541Srgrimes/* 10441541Srgrimes * Move a vnode from one mount queue to another. 10451541Srgrimes */ 10461541Srgrimesstatic void 10471541Srgrimesinsmntque(vp, mp) 10481541Srgrimes register struct vnode *vp; 10491541Srgrimes register struct mount *mp; 10501541Srgrimes{ 10511541Srgrimes 10521541Srgrimes mtx_lock(&mntvnode_mtx); 10531541Srgrimes /* 10541541Srgrimes * Delete from old mount point vnode list, if on one. 10551541Srgrimes */ 10561541Srgrimes if (vp->v_mount != NULL) { 10571541Srgrimes KASSERT(vp->v_mount->mnt_nvnodelistsize > 0, 10581541Srgrimes ("bad mount point vnode list size")); 10591541Srgrimes TAILQ_REMOVE(&vp->v_mount->mnt_nvnodelist, vp, v_nmntvnodes); 10601541Srgrimes vp->v_mount->mnt_nvnodelistsize--; 10611541Srgrimes } 10621541Srgrimes /* 10631541Srgrimes * Insert into list of vnodes for the new mount point, if available. 10641541Srgrimes */ 10651541Srgrimes if ((vp->v_mount = mp) == NULL) { 10661541Srgrimes mtx_unlock(&mntvnode_mtx); 10671541Srgrimes return; 10681541Srgrimes } 106917108Sbde TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes); 10701541Srgrimes mp->mnt_nvnodelistsize++; 10711541Srgrimes mtx_unlock(&mntvnode_mtx); 10721541Srgrimes} 10731541Srgrimes 10741541Srgrimes/* 10751541Srgrimes * Update outstanding I/O count and do wakeup if requested. 10761541Srgrimes */ 10771541Srgrimesvoid 10781541Srgrimesvwakeup(bp) 10791541Srgrimes register struct buf *bp; 10801541Srgrimes{ 10811541Srgrimes register struct vnode *vp; 108234266Sjulian 108334266Sjulian bp->b_flags &= ~B_WRITEINPROG; 108434266Sjulian if ((vp = bp->b_vp)) { 108534266Sjulian VI_LOCK(vp); 10861541Srgrimes vp->v_numoutput--; 10871541Srgrimes if (vp->v_numoutput < 0) 108812911Sphk panic("vwakeup: neg numoutput"); 10891541Srgrimes if ((vp->v_numoutput == 0) && (vp->v_iflag & VI_BWAIT)) { 10901541Srgrimes vp->v_iflag &= ~VI_BWAIT; 10911541Srgrimes wakeup(&vp->v_numoutput); 10921541Srgrimes } 10931541Srgrimes VI_UNLOCK(vp); 10941541Srgrimes } 10951541Srgrimes} 109622521Sdyson 10971541Srgrimes/* 10981541Srgrimes * Flush out and invalidate all buffers associated with a vnode. 10991541Srgrimes * Called with the underlying object locked. 110022521Sdyson */ 11011541Srgrimesint 11021541Srgrimesvinvalbuf(vp, flags, cred, td, slpflag, slptimeo) 11031541Srgrimes struct vnode *vp; 11041541Srgrimes int flags; 11051541Srgrimes struct ucred *cred; 110622521Sdyson struct thread *td; 11071541Srgrimes int slpflag, slptimeo; 110822521Sdyson{ 11091541Srgrimes struct buf *blist; 11101541Srgrimes int s, error; 111122521Sdyson vm_object_t object; 11121541Srgrimes 11131541Srgrimes GIANT_REQUIRED; 11141541Srgrimes 11151541Srgrimes ASSERT_VOP_LOCKED(vp, "vinvalbuf"); 11161541Srgrimes 11171541Srgrimes VI_LOCK(vp); 11181541Srgrimes if (flags & V_SAVE) { 11191541Srgrimes s = splbio(); 11201541Srgrimes while (vp->v_numoutput) { 11211541Srgrimes vp->v_iflag |= VI_BWAIT; 11221541Srgrimes error = msleep(&vp->v_numoutput, VI_MTX(vp), 112322521Sdyson slpflag | (PRIBIO + 1), "vinvlbuf", slptimeo); 11241541Srgrimes if (error) { 112522521Sdyson VI_UNLOCK(vp); 11261541Srgrimes splx(s); 112722521Sdyson return (error); 112822521Sdyson } 112922521Sdyson } 113022521Sdyson if (!TAILQ_EMPTY(&vp->v_dirtyblkhd)) { 113122521Sdyson splx(s); 113222521Sdyson VI_UNLOCK(vp); 113322521Sdyson if ((error = VOP_FSYNC(vp, cred, MNT_WAIT, td)) != 0) 113422521Sdyson return (error); 113522521Sdyson /* 113622521Sdyson * XXX We could save a lock/unlock if this was only 113722521Sdyson * enabled under INVARIANTS 113822521Sdyson */ 113922521Sdyson VI_LOCK(vp); 11401541Srgrimes s = splbio(); 114122521Sdyson if (vp->v_numoutput > 0 || 11421541Srgrimes !TAILQ_EMPTY(&vp->v_dirtyblkhd)) 11431541Srgrimes panic("vinvalbuf: dirty bufs"); 11441541Srgrimes } 11451541Srgrimes splx(s); 11461541Srgrimes } 11471541Srgrimes s = splbio(); 11481541Srgrimes /* 11491541Srgrimes * If you alter this loop please notice that interlock is dropped and 11501541Srgrimes * reacquired in flushbuflist. Special care is needed to ensure that 11511541Srgrimes * no race conditions occur from this. 11521541Srgrimes */ 11531541Srgrimes for (error = 0;;) { 11541541Srgrimes if ((blist = TAILQ_FIRST(&vp->v_cleanblkhd)) != 0 && 11551541Srgrimes flushbuflist(blist, flags, vp, slpflag, slptimeo, &error)) { 11561541Srgrimes if (error) 11571541Srgrimes break; 11581541Srgrimes continue; 11591541Srgrimes } 116022521Sdyson if ((blist = TAILQ_FIRST(&vp->v_dirtyblkhd)) != 0 && 11611541Srgrimes flushbuflist(blist, flags, vp, slpflag, slptimeo, &error)) { 11621541Srgrimes if (error) 11631541Srgrimes break; 11641541Srgrimes continue; 11651541Srgrimes } 11661541Srgrimes break; 11671541Srgrimes } 116822521Sdyson if (error) { 11691541Srgrimes splx(s); 11701541Srgrimes VI_UNLOCK(vp); 11711541Srgrimes return (error); 11721541Srgrimes } 11731541Srgrimes 11741541Srgrimes /* 117527890Sphk * Wait for I/O to complete. XXX needs cleaning up. The vnode can 11761541Srgrimes * have write I/O in-progress but if there is a VM object then the 11771541Srgrimes * VM object can also have read-I/O in-progress. 11781541Srgrimes */ 11791541Srgrimes do { 118022521Sdyson while (vp->v_numoutput > 0) { 118122521Sdyson vp->v_iflag |= VI_BWAIT; 118222521Sdyson msleep(&vp->v_numoutput, VI_MTX(vp), PVM, "vnvlbv", 0); 118322521Sdyson } 118422521Sdyson VI_UNLOCK(vp); 118522521Sdyson if (VOP_GETVOBJECT(vp, &object) == 0) { 11861541Srgrimes VM_OBJECT_LOCK(object); 11871541Srgrimes vm_object_pip_wait(object, "vnvlbx"); 118834266Sjulian VM_OBJECT_UNLOCK(object); 11891541Srgrimes } 11909980Sdg VI_LOCK(vp); 11911541Srgrimes } while (vp->v_numoutput > 0); 11921541Srgrimes VI_UNLOCK(vp); 11931541Srgrimes 11941541Srgrimes splx(s); 11951541Srgrimes 11961541Srgrimes /* 119712911Sphk * Destroy the copy in the VM cache, too. 11981541Srgrimes */ 11991541Srgrimes if (VOP_GETVOBJECT(vp, &object) == 0) { 12001541Srgrimes VM_OBJECT_LOCK(object); 12011541Srgrimes vm_object_page_remove(object, 0, 0, 12021541Srgrimes (flags & V_SAVE) ? TRUE : FALSE); 12031541Srgrimes VM_OBJECT_UNLOCK(object); 12041541Srgrimes } 12051541Srgrimes 12061541Srgrimes#ifdef INVARIANTS 12071541Srgrimes VI_LOCK(vp); 12081541Srgrimes if ((flags & (V_ALT | V_NORMAL)) == 0 && 12091541Srgrimes (!TAILQ_EMPTY(&vp->v_dirtyblkhd) || 12101541Srgrimes !TAILQ_EMPTY(&vp->v_cleanblkhd))) 12111541Srgrimes panic("vinvalbuf: flush failed"); 121222521Sdyson VI_UNLOCK(vp); 12131541Srgrimes#endif 12141541Srgrimes return (0); 12151541Srgrimes} 12161541Srgrimes 12171541Srgrimes/* 12181541Srgrimes * Flush out buffers on the specified list. 12191541Srgrimes * 12201541Srgrimes */ 12211541Srgrimesstatic int 122217108Sbdeflushbuflist(blist, flags, vp, slpflag, slptimeo, errorp) 12231541Srgrimes struct buf *blist; 12241541Srgrimes int flags; 12251541Srgrimes struct vnode *vp; 12261541Srgrimes int slpflag, slptimeo; 122717108Sbde int *errorp; 12281541Srgrimes{ 12291541Srgrimes struct buf *bp, *nbp; 12301541Srgrimes int found, error; 12311541Srgrimes 123217108Sbde ASSERT_VI_LOCKED(vp, "flushbuflist"); 12331541Srgrimes 123434961Sphk for (found = 0, bp = blist; bp; bp = nbp) { 12351541Srgrimes nbp = TAILQ_NEXT(bp, b_vnbufs); 12361541Srgrimes if (((flags & V_NORMAL) && (bp->b_xflags & BX_ALTDATA)) || 12371541Srgrimes ((flags & V_ALT) && (bp->b_xflags & BX_ALTDATA) == 0)) { 12381541Srgrimes continue; 12391541Srgrimes } 12401541Srgrimes found += 1; 12411541Srgrimes error = BUF_TIMELOCK(bp, 12421541Srgrimes LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, VI_MTX(vp), 12431541Srgrimes "flushbuf", slpflag, slptimeo); 12441541Srgrimes if (error) { 12451541Srgrimes if (error != ENOLCK) 12461541Srgrimes *errorp = error; 12471541Srgrimes goto done; 12486357Sphk } 124937555Sbde /* 12501541Srgrimes * XXX Since there are no node locks for NFS, I 12511541Srgrimes * believe there is a slight chance that a delayed 12521541Srgrimes * write will occur while sleeping just above, so 12531541Srgrimes * check for it. Note that vfs_bio_awrite expects 12541541Srgrimes * buffers to reside on a queue, while BUF_WRITE and 12551541Srgrimes * brelse do not. 12561541Srgrimes */ 12571541Srgrimes if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) && 12581541Srgrimes (flags & V_SAVE)) { 12591541Srgrimes 12601541Srgrimes if (bp->b_vp == vp) { 12611541Srgrimes if (bp->b_flags & B_CLUSTEROK) { 12621541Srgrimes vfs_bio_awrite(bp); 12631541Srgrimes } else { 12641541Srgrimes bremfree(bp); 12651541Srgrimes bp->b_flags |= B_ASYNC; 12661541Srgrimes BUF_WRITE(bp); 126734266Sjulian } 126834266Sjulian } else { 12691541Srgrimes bremfree(bp); 12701541Srgrimes (void) BUF_WRITE(bp); 12711541Srgrimes } 12721541Srgrimes goto done; 12731541Srgrimes } 12741541Srgrimes bremfree(bp); 12751541Srgrimes bp->b_flags |= (B_INVAL | B_NOCACHE | B_RELBUF); 12761541Srgrimes bp->b_flags &= ~B_ASYNC; 12771541Srgrimes brelse(bp); 12781541Srgrimes VI_LOCK(vp); 12791541Srgrimes } 12801541Srgrimes return (found); 12811541Srgrimesdone: 12821541Srgrimes VI_LOCK(vp); 12831541Srgrimes return (found); 12841541Srgrimes} 12851541Srgrimes 12861541Srgrimes/* 12878876Srgrimes * Truncate a file's buffer and pages to a specified length. This 12881541Srgrimes * is in lieu of the old vinvalbuf mechanism, which performed unneeded 12891541Srgrimes * sync activity. 12901549Srgrimes */ 12911541Srgrimesint 12921541Srgrimesvtruncbuf(vp, cred, td, length, blksize) 129322521Sdyson register struct vnode *vp; 12941541Srgrimes struct ucred *cred; 12951541Srgrimes struct thread *td; 12961541Srgrimes off_t length; 12971541Srgrimes int blksize; 12981541Srgrimes{ 129922521Sdyson register struct buf *bp; 13001541Srgrimes struct buf *nbp; 13011541Srgrimes int s, anyfreed; 13021541Srgrimes int trunclbn; 130334266Sjulian 130434266Sjulian /* 130534266Sjulian * Round up to the *next* lbn. 130634266Sjulian */ 130723560Smpp trunclbn = (length + blksize - 1) / blksize; 13081541Srgrimes 13091541Srgrimes s = splbio(); 13101541Srgrimes ASSERT_VOP_LOCKED(vp, "vtruncbuf"); 131137555Sbderestart: 131237555Sbde VI_LOCK(vp); 13131541Srgrimes anyfreed = 1; 13141541Srgrimes for (;anyfreed;) { 13151541Srgrimes anyfreed = 0; 13161541Srgrimes for (bp = TAILQ_FIRST(&vp->v_cleanblkhd); bp; bp = nbp) { 13171541Srgrimes nbp = TAILQ_NEXT(bp, b_vnbufs); 13181541Srgrimes if (bp->b_lblkno >= trunclbn) { 13191541Srgrimes if (BUF_LOCK(bp, 13201541Srgrimes LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, 13211541Srgrimes VI_MTX(vp)) == ENOLCK) 13221541Srgrimes goto restart; 13231541Srgrimes 13241541Srgrimes bremfree(bp); 13251541Srgrimes bp->b_flags |= (B_INVAL | B_RELBUF); 13261541Srgrimes bp->b_flags &= ~B_ASYNC; 132734961Sphk brelse(bp); 13281541Srgrimes anyfreed = 1; 13291541Srgrimes 13301541Srgrimes if (nbp && 133134266Sjulian (((nbp->b_xflags & BX_VNCLEAN) == 0) || 13326357Sphk (nbp->b_vp != vp) || 133337555Sbde (nbp->b_flags & B_DELWRI))) { 133423560Smpp goto restart; 13351541Srgrimes } 13361541Srgrimes VI_LOCK(vp); 13371541Srgrimes } 13381541Srgrimes } 13391541Srgrimes 13401541Srgrimes for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) { 13411541Srgrimes nbp = TAILQ_NEXT(bp, b_vnbufs); 13421541Srgrimes if (bp->b_lblkno >= trunclbn) { 13431541Srgrimes if (BUF_LOCK(bp, 13441541Srgrimes LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, 13451541Srgrimes VI_MTX(vp)) == ENOLCK) 13461541Srgrimes goto restart; 13471541Srgrimes bremfree(bp); 13481541Srgrimes bp->b_flags |= (B_INVAL | B_RELBUF); 13491541Srgrimes bp->b_flags &= ~B_ASYNC; 13501541Srgrimes brelse(bp); 13511541Srgrimes anyfreed = 1; 13521541Srgrimes if (nbp && 13531541Srgrimes (((nbp->b_xflags & BX_VNDIRTY) == 0) || 13541541Srgrimes (nbp->b_vp != vp) || 13551541Srgrimes (nbp->b_flags & B_DELWRI) == 0)) { 13561541Srgrimes goto restart; 13576357Sphk } 135837555Sbde VI_LOCK(vp); 135937555Sbde } 136023560Smpp } 13611541Srgrimes } 13621541Srgrimes 13631541Srgrimes if (length > 0) { 13641541Srgrimesrestartsync: 13651541Srgrimes for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) { 13661541Srgrimes nbp = TAILQ_NEXT(bp, b_vnbufs); 13671541Srgrimes if (bp->b_lblkno > 0) 13681541Srgrimes continue; 13691541Srgrimes /* 13701541Srgrimes * Since we hold the vnode lock this should only 13711541Srgrimes * fail if we're racing with the buf daemon. 13721541Srgrimes */ 13731541Srgrimes if (BUF_LOCK(bp, 13741541Srgrimes LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, 13751541Srgrimes VI_MTX(vp)) == ENOLCK) { 13761541Srgrimes goto restart; 13771541Srgrimes } 13781541Srgrimes KASSERT((bp->b_flags & B_DELWRI), 13791541Srgrimes ("buf(%p) on dirty queue without DELWRI.", bp)); 13801541Srgrimes 13811541Srgrimes bremfree(bp); 13821541Srgrimes bawrite(bp); 13831541Srgrimes VI_LOCK(vp); 13841541Srgrimes goto restartsync; 13851541Srgrimes } 13861541Srgrimes } 13871541Srgrimes 13881541Srgrimes while (vp->v_numoutput > 0) { 13891541Srgrimes vp->v_iflag |= VI_BWAIT; 13901541Srgrimes msleep(&vp->v_numoutput, VI_MTX(vp), PVM, "vbtrunc", 0); 13911541Srgrimes } 13921541Srgrimes VI_UNLOCK(vp); 139322521Sdyson splx(s); 13941541Srgrimes 139522521Sdyson vnode_pager_setsize(vp, length); 139622521Sdyson 139722521Sdyson return (0); 139831352Sbde} 139922521Sdyson 140022521Sdyson/* 140122521Sdyson * buf_splay() - splay tree core for the clean/dirty list of buffers in 140222521Sdyson * a vnode. 140322521Sdyson * 140422521Sdyson * NOTE: We have to deal with the special case of a background bitmap 140522521Sdyson * buffer, a situation where two buffers will have the same logical 140622521Sdyson * block offset. We want (1) only the foreground buffer to be accessed 140722521Sdyson * in a lookup and (2) must differentiate between the foreground and 140822521Sdyson * background buffer in the splay tree algorithm because the splay 140922521Sdyson * tree cannot normally handle multiple entities with the same 'index'. 141022521Sdyson * We accomplish this by adding differentiating flags to the splay tree's 141137555Sbde * numerical domain. 141237555Sbde */ 141322544Smppstatic 141422521Sdysonstruct buf * 141522521Sdysonbuf_splay(daddr_t lblkno, b_xflags_t xflags, struct buf *root) 141622544Smpp{ 141722521Sdyson struct buf dummy; 141822521Sdyson struct buf *lefttreemax, *righttreemin, *y; 141922544Smpp 142022544Smpp if (root == NULL) 142122521Sdyson return (NULL); 142222544Smpp lefttreemax = righttreemin = &dummy; 142322544Smpp for (;;) { 142422521Sdyson if (lblkno < root->b_lblkno || 142522521Sdyson (lblkno == root->b_lblkno && 142622521Sdyson (xflags & BX_BKGRDMARKER) < (root->b_xflags & BX_BKGRDMARKER))) { 142722521Sdyson if ((y = root->b_left) == NULL) 142822521Sdyson break; 142922521Sdyson if (lblkno < y->b_lblkno) { 143022521Sdyson /* Rotate right. */ 143122521Sdyson root->b_left = y->b_right; 143222521Sdyson y->b_right = root; 143322544Smpp root = y; 143422521Sdyson if ((y = root->b_left) == NULL) 143522521Sdyson break; 143622521Sdyson } 143722521Sdyson /* Link into the new root's right tree. */ 143822521Sdyson righttreemin->b_left = root; 143922521Sdyson righttreemin = root; 144022521Sdyson } else if (lblkno > root->b_lblkno || 14411541Srgrimes (lblkno == root->b_lblkno && 14421541Srgrimes (xflags & BX_BKGRDMARKER) > (root->b_xflags & BX_BKGRDMARKER))) { 14431541Srgrimes if ((y = root->b_right) == NULL) 144434266Sjulian break; 144530474Sphk if (lblkno > y->b_lblkno) { 144630474Sphk /* Rotate left. */ 144730474Sphk root->b_right = y->b_left; 14481541Srgrimes y->b_left = root; 144934266Sjulian root = y; 145034266Sjulian if ((y = root->b_right) == NULL) 145134266Sjulian break; 145234266Sjulian } 145334266Sjulian /* Link into the new root's left tree. */ 145434266Sjulian lefttreemax->b_right = root; 145534266Sjulian lefttreemax = root; 145634266Sjulian } else { 145734266Sjulian break; 145834266Sjulian } 145934266Sjulian root = y; 146034266Sjulian } 146134266Sjulian /* Assemble the new root. */ 146234266Sjulian lefttreemax->b_right = root->b_left; 146334266Sjulian righttreemin->b_left = root->b_right; 146434266Sjulian root->b_left = dummy.b_right; 146534266Sjulian root->b_right = dummy.b_left; 14661541Srgrimes return (root); 14671541Srgrimes} 14681541Srgrimes 14691541Srgrimesstatic 14701541Srgrimesvoid 14711541Srgrimesbuf_vlist_remove(struct buf *bp) 147230474Sphk{ 14731541Srgrimes struct vnode *vp = bp->b_vp; 14741541Srgrimes struct buf *root; 147523560Smpp 14761541Srgrimes ASSERT_VI_LOCKED(vp, "buf_vlist_remove"); 14771541Srgrimes if (bp->b_xflags & BX_VNDIRTY) { 14781541Srgrimes if (bp != vp->v_dirtyblkroot) { 14791541Srgrimes root = buf_splay(bp->b_lblkno, bp->b_xflags, vp->v_dirtyblkroot); 14801541Srgrimes KASSERT(root == bp, ("splay lookup failed during dirty remove")); 14811541Srgrimes } 148234266Sjulian if (bp->b_left == NULL) { 14831541Srgrimes root = bp->b_right; 14841541Srgrimes } else { 14851541Srgrimes root = buf_splay(bp->b_lblkno, bp->b_xflags, bp->b_left); 14861541Srgrimes root->b_right = bp->b_right; 14871541Srgrimes } 14881541Srgrimes vp->v_dirtyblkroot = root; 148934961Sphk TAILQ_REMOVE(&vp->v_dirtyblkhd, bp, b_vnbufs); 14901541Srgrimes vp->v_dirtybufcnt--; 14911541Srgrimes } else { 149237555Sbde /* KASSERT(bp->b_xflags & BX_VNCLEAN, ("bp wasn't clean")); */ 149337555Sbde if (bp != vp->v_cleanblkroot) { 14941541Srgrimes root = buf_splay(bp->b_lblkno, bp->b_xflags, vp->v_cleanblkroot); 149523560Smpp KASSERT(root == bp, ("splay lookup failed during clean remove")); 14961541Srgrimes } 14971541Srgrimes if (bp->b_left == NULL) { 14981541Srgrimes root = bp->b_right; 14991541Srgrimes } else { 15001541Srgrimes root = buf_splay(bp->b_lblkno, bp->b_xflags, bp->b_left); 15011541Srgrimes root->b_right = bp->b_right; 15021541Srgrimes } 150330474Sphk vp->v_cleanblkroot = root; 15041541Srgrimes TAILQ_REMOVE(&vp->v_cleanblkhd, bp, b_vnbufs); 15051541Srgrimes vp->v_cleanbufcnt--; 15061541Srgrimes } 15071541Srgrimes bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN); 15081541Srgrimes} 15091541Srgrimes 15101541Srgrimes/* 15111541Srgrimes * Add the buffer to the sorted clean or dirty block list using a 15121541Srgrimes * splay tree algorithm. 15131541Srgrimes * 15141541Srgrimes * NOTE: xflags is passed as a constant, optimizing this inline function! 15151541Srgrimes */ 15161541Srgrimesstatic 15171541Srgrimesvoid 15181541Srgrimesbuf_vlist_add(struct buf *bp, struct vnode *vp, b_xflags_t xflags) 151922521Sdyson{ 15201541Srgrimes struct buf *root; 15211541Srgrimes 15221541Srgrimes ASSERT_VI_LOCKED(vp, "buf_vlist_add"); 152322521Sdyson bp->b_xflags |= xflags; 15241541Srgrimes if (xflags & BX_VNDIRTY) { 15251541Srgrimes root = buf_splay(bp->b_lblkno, bp->b_xflags, vp->v_dirtyblkroot); 152622521Sdyson if (root == NULL) { 15271541Srgrimes bp->b_left = NULL; 15281541Srgrimes bp->b_right = NULL; 15291541Srgrimes TAILQ_INSERT_TAIL(&vp->v_dirtyblkhd, bp, b_vnbufs); 15301541Srgrimes } else if (bp->b_lblkno < root->b_lblkno || 15311541Srgrimes (bp->b_lblkno == root->b_lblkno && 15321541Srgrimes (bp->b_xflags & BX_BKGRDMARKER) < (root->b_xflags & BX_BKGRDMARKER))) { 15331541Srgrimes bp->b_left = root->b_left; 15341541Srgrimes bp->b_right = root; 15351541Srgrimes root->b_left = NULL; 15361541Srgrimes TAILQ_INSERT_BEFORE(root, bp, b_vnbufs); 15371541Srgrimes } else { 15381541Srgrimes bp->b_right = root->b_right; 15391541Srgrimes bp->b_left = root; 15401541Srgrimes root->b_right = NULL; 15411541Srgrimes TAILQ_INSERT_AFTER(&vp->v_dirtyblkhd, 15421541Srgrimes root, bp, b_vnbufs); 15431541Srgrimes } 15441541Srgrimes vp->v_dirtybufcnt++; 15451541Srgrimes vp->v_dirtyblkroot = bp; 15461541Srgrimes } else { 15471541Srgrimes /* KASSERT(xflags & BX_VNCLEAN, ("xflags not clean")); */ 15481541Srgrimes root = buf_splay(bp->b_lblkno, bp->b_xflags, vp->v_cleanblkroot); 15491541Srgrimes if (root == NULL) { 15501541Srgrimes bp->b_left = NULL; 15511541Srgrimes bp->b_right = NULL; 15521541Srgrimes TAILQ_INSERT_TAIL(&vp->v_cleanblkhd, bp, b_vnbufs); 15531541Srgrimes } else if (bp->b_lblkno < root->b_lblkno || 15541541Srgrimes (bp->b_lblkno == root->b_lblkno && 15551541Srgrimes (bp->b_xflags & BX_BKGRDMARKER) < (root->b_xflags & BX_BKGRDMARKER))) { 15561541Srgrimes bp->b_left = root->b_left; 15571541Srgrimes bp->b_right = root; 15581541Srgrimes root->b_left = NULL; 15591541Srgrimes TAILQ_INSERT_BEFORE(root, bp, b_vnbufs); 15601541Srgrimes } else { 15611541Srgrimes bp->b_right = root->b_right; 15621541Srgrimes bp->b_left = root; 15631541Srgrimes root->b_right = NULL; 15641541Srgrimes TAILQ_INSERT_AFTER(&vp->v_cleanblkhd, 15651541Srgrimes root, bp, b_vnbufs); 15661541Srgrimes } 15671541Srgrimes vp->v_cleanbufcnt++; 15681541Srgrimes vp->v_cleanblkroot = bp; 15691541Srgrimes } 15701541Srgrimes} 15711541Srgrimes 15721541Srgrimes/* 15733487Sphk * Lookup a buffer using the splay tree. Note that we specifically avoid 15741541Srgrimes * shadow buffers used in background bitmap writes. 15751541Srgrimes * 15761541Srgrimes * This code isn't quite efficient as it could be because we are maintaining 15771541Srgrimes * two sorted lists and do not know which list the block resides in. 15781541Srgrimes * 15791541Srgrimes * During a "make buildworld" the desired buffer is found at one of 15801541Srgrimes * the roots more than 60% of the time. Thus, checking both roots 15811541Srgrimes * before performing either splay eliminates unnecessary splays on the 15821541Srgrimes * first tree splayed. 158312911Sphk */ 15841541Srgrimesstruct buf * 15851541Srgrimesgbincore(struct vnode *vp, daddr_t lblkno) 15861541Srgrimes{ 158722521Sdyson struct buf *bp; 15881541Srgrimes 15891541Srgrimes GIANT_REQUIRED; 159022521Sdyson 159122521Sdyson ASSERT_VI_LOCKED(vp, "gbincore"); 15921541Srgrimes if ((bp = vp->v_cleanblkroot) != NULL && 15931541Srgrimes bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER)) 15941541Srgrimes return (bp); 15951541Srgrimes if ((bp = vp->v_dirtyblkroot) != NULL && 15961541Srgrimes bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER)) 15971541Srgrimes return (bp); 15981541Srgrimes if ((bp = vp->v_cleanblkroot) != NULL) { 15991541Srgrimes vp->v_cleanblkroot = bp = buf_splay(lblkno, 0, bp); 16001541Srgrimes if (bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER)) 16011541Srgrimes return (bp); 16021541Srgrimes } 16031541Srgrimes if ((bp = vp->v_dirtyblkroot) != NULL) { 16041541Srgrimes vp->v_dirtyblkroot = bp = buf_splay(lblkno, 0, bp); 16051541Srgrimes if (bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER)) 16061541Srgrimes return (bp); 16071541Srgrimes } 16081541Srgrimes return (NULL); 16091541Srgrimes} 16101541Srgrimes 16111541Srgrimes/* 16121541Srgrimes * Associate a buffer with a vnode. 16131541Srgrimes */ 16141541Srgrimesvoid 16151541Srgrimesbgetvp(vp, bp) 16161541Srgrimes register struct vnode *vp; 16171541Srgrimes register struct buf *bp; 16181541Srgrimes{ 16191541Srgrimes int s; 16201541Srgrimes 16211541Srgrimes KASSERT(bp->b_vp == NULL, ("bgetvp: not free")); 16221541Srgrimes 16231541Srgrimes KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0, 16241541Srgrimes ("bgetvp: bp already attached! %p", bp)); 16251541Srgrimes 16261541Srgrimes ASSERT_VI_LOCKED(vp, "bgetvp"); 16271541Srgrimes vholdl(vp); 16281541Srgrimes bp->b_vp = vp; 16291541Srgrimes bp->b_dev = vn_todev(vp); 16301541Srgrimes /* 16311541Srgrimes * Insert onto list for new vnode. 16321541Srgrimes */ 16331541Srgrimes s = splbio(); 16341541Srgrimes buf_vlist_add(bp, vp, BX_VNCLEAN); 16351541Srgrimes splx(s); 16361541Srgrimes} 16371541Srgrimes 16381541Srgrimes/* 16391541Srgrimes * Disassociate a buffer from a vnode. 16401541Srgrimes */ 16411541Srgrimesvoid 16421541Srgrimesbrelvp(bp) 16431541Srgrimes register struct buf *bp; 16441541Srgrimes{ 16451541Srgrimes struct vnode *vp; 16461541Srgrimes int s; 16471541Srgrimes 16481541Srgrimes KASSERT(bp->b_vp != NULL, ("brelvp: NULL")); 16491541Srgrimes 16501541Srgrimes /* 16511541Srgrimes * Delete from old vnode list, if on one. 16521541Srgrimes */ 16531541Srgrimes vp = bp->b_vp; 16541541Srgrimes s = splbio(); 16551541Srgrimes VI_LOCK(vp); 16561541Srgrimes if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) 16571541Srgrimes buf_vlist_remove(bp); 16581541Srgrimes if ((vp->v_iflag & VI_ONWORKLST) && TAILQ_EMPTY(&vp->v_dirtyblkhd)) { 16591541Srgrimes vp->v_iflag &= ~VI_ONWORKLST; 166022521Sdyson mtx_lock(&sync_mtx); 166122521Sdyson LIST_REMOVE(vp, v_synclist); 166222521Sdyson mtx_unlock(&sync_mtx); 166322521Sdyson } 166422521Sdyson vdropl(vp); 166522521Sdyson VI_UNLOCK(vp); 166622521Sdyson bp->b_vp = (struct vnode *) 0; 166722521Sdyson if (bp->b_object) 16681541Srgrimes bp->b_object = NULL; 16691541Srgrimes splx(s); 16701541Srgrimes} 16711541Srgrimes 16728876Srgrimes/* 16731541Srgrimes * Add an item to the syncer work queue. 16741541Srgrimes */ 16751541Srgrimesstatic void 16761541Srgrimesvn_syncer_add_to_worklist(struct vnode *vp, int delay) 16771541Srgrimes{ 16781541Srgrimes int s, slot; 16791541Srgrimes 16801541Srgrimes s = splbio(); 16811541Srgrimes ASSERT_VI_LOCKED(vp, "vn_syncer_add_to_worklist"); 168218330Speter 16831541Srgrimes mtx_lock(&sync_mtx); 168418330Speter if (vp->v_iflag & VI_ONWORKLST) 168518330Speter LIST_REMOVE(vp, v_synclist); 16861541Srgrimes else 1687 vp->v_iflag |= VI_ONWORKLST; 1688 1689 if (delay > syncer_maxdelay - 2) 1690 delay = syncer_maxdelay - 2; 1691 slot = (syncer_delayno + delay) & syncer_mask; 1692 1693 LIST_INSERT_HEAD(&syncer_workitem_pending[slot], vp, v_synclist); 1694 mtx_unlock(&sync_mtx); 1695 1696 splx(s); 1697} 1698 1699struct proc *updateproc; 1700static void sched_sync(void); 1701static struct kproc_desc up_kp = { 1702 "syncer", 1703 sched_sync, 1704 &updateproc 1705}; 1706SYSINIT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp) 1707 1708/* 1709 * System filesystem synchronizer daemon. 1710 */ 1711static void 1712sched_sync(void) 1713{ 1714 struct synclist *slp; 1715 struct vnode *vp; 1716 struct mount *mp; 1717 long starttime; 1718 int s; 1719 struct thread *td = FIRST_THREAD_IN_PROC(updateproc); /* XXXKSE */ 1720 1721 mtx_lock(&Giant); 1722 1723 EVENTHANDLER_REGISTER(shutdown_pre_sync, kproc_shutdown, td->td_proc, 1724 SHUTDOWN_PRI_LAST); 1725 1726 for (;;) { 1727 kthread_suspend_check(td->td_proc); 1728 1729 starttime = time_second; 1730 1731 /* 1732 * Push files whose dirty time has expired. Be careful 1733 * of interrupt race on slp queue. 1734 */ 1735 s = splbio(); 1736 mtx_lock(&sync_mtx); 1737 slp = &syncer_workitem_pending[syncer_delayno]; 1738 syncer_delayno += 1; 1739 if (syncer_delayno == syncer_maxdelay) 1740 syncer_delayno = 0; 1741 splx(s); 1742 1743 while ((vp = LIST_FIRST(slp)) != NULL) { 1744 mtx_unlock(&sync_mtx); 1745 if (VOP_ISLOCKED(vp, NULL) == 0 && 1746 vn_start_write(vp, &mp, V_NOWAIT) == 0) { 1747 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 1748 (void) VOP_FSYNC(vp, td->td_ucred, MNT_LAZY, td); 1749 VOP_UNLOCK(vp, 0, td); 1750 vn_finished_write(mp); 1751 } 1752 s = splbio(); 1753 mtx_lock(&sync_mtx); 1754 if (LIST_FIRST(slp) == vp) { 1755 mtx_unlock(&sync_mtx); 1756 /* 1757 * Note: VFS vnodes can remain on the 1758 * worklist too with no dirty blocks, but 1759 * since sync_fsync() moves it to a different 1760 * slot we are safe. 1761 */ 1762 VI_LOCK(vp); 1763 if (TAILQ_EMPTY(&vp->v_dirtyblkhd) && 1764 !vn_isdisk(vp, NULL)) { 1765 panic("sched_sync: fsync failed " 1766 "vp %p tag %s", vp, vp->v_tag); 1767 } 1768 /* 1769 * Put us back on the worklist. The worklist 1770 * routine will remove us from our current 1771 * position and then add us back in at a later 1772 * position. 1773 */ 1774 vn_syncer_add_to_worklist(vp, syncdelay); 1775 VI_UNLOCK(vp); 1776 mtx_lock(&sync_mtx); 1777 } 1778 splx(s); 1779 } 1780 mtx_unlock(&sync_mtx); 1781 1782 /* 1783 * Do soft update processing. 1784 */ 1785 if (softdep_process_worklist_hook != NULL) 1786 (*softdep_process_worklist_hook)(NULL); 1787 1788 /* 1789 * The variable rushjob allows the kernel to speed up the 1790 * processing of the filesystem syncer process. A rushjob 1791 * value of N tells the filesystem syncer to process the next 1792 * N seconds worth of work on its queue ASAP. Currently rushjob 1793 * is used by the soft update code to speed up the filesystem 1794 * syncer process when the incore state is getting so far 1795 * ahead of the disk that the kernel memory pool is being 1796 * threatened with exhaustion. 1797 */ 1798 mtx_lock(&sync_mtx); 1799 if (rushjob > 0) { 1800 rushjob -= 1; 1801 mtx_unlock(&sync_mtx); 1802 continue; 1803 } 1804 mtx_unlock(&sync_mtx); 1805 /* 1806 * If it has taken us less than a second to process the 1807 * current work, then wait. Otherwise start right over 1808 * again. We can still lose time if any single round 1809 * takes more than two seconds, but it does not really 1810 * matter as we are just trying to generally pace the 1811 * filesystem activity. 1812 */ 1813 if (time_second == starttime) 1814 tsleep(&lbolt, PPAUSE, "syncer", 0); 1815 } 1816} 1817 1818/* 1819 * Request the syncer daemon to speed up its work. 1820 * We never push it to speed up more than half of its 1821 * normal turn time, otherwise it could take over the cpu. 1822 * XXXKSE only one update? 1823 */ 1824int 1825speedup_syncer() 1826{ 1827 struct thread *td; 1828 int ret = 0; 1829 1830 td = FIRST_THREAD_IN_PROC(updateproc); 1831 mtx_lock_spin(&sched_lock); 1832 if (td->td_wchan == &lbolt) { 1833 unsleep(td); 1834 TD_CLR_SLEEPING(td); 1835 setrunnable(td); 1836 } 1837 mtx_unlock_spin(&sched_lock); 1838 mtx_lock(&sync_mtx); 1839 if (rushjob < syncdelay / 2) { 1840 rushjob += 1; 1841 stat_rush_requests += 1; 1842 ret = 1; 1843 } 1844 mtx_unlock(&sync_mtx); 1845 return (ret); 1846} 1847 1848/* 1849 * Associate a p-buffer with a vnode. 1850 * 1851 * Also sets B_PAGING flag to indicate that vnode is not fully associated 1852 * with the buffer. i.e. the bp has not been linked into the vnode or 1853 * ref-counted. 1854 */ 1855void 1856pbgetvp(vp, bp) 1857 register struct vnode *vp; 1858 register struct buf *bp; 1859{ 1860 1861 KASSERT(bp->b_vp == NULL, ("pbgetvp: not free")); 1862 1863 bp->b_vp = vp; 1864 bp->b_flags |= B_PAGING; 1865 bp->b_dev = vn_todev(vp); 1866} 1867 1868/* 1869 * Disassociate a p-buffer from a vnode. 1870 */ 1871void 1872pbrelvp(bp) 1873 register struct buf *bp; 1874{ 1875 1876 KASSERT(bp->b_vp != NULL, ("pbrelvp: NULL")); 1877 1878 /* XXX REMOVE ME */ 1879 VI_LOCK(bp->b_vp); 1880 if (TAILQ_NEXT(bp, b_vnbufs) != NULL) { 1881 panic( 1882 "relpbuf(): b_vp was probably reassignbuf()d %p %x", 1883 bp, 1884 (int)bp->b_flags 1885 ); 1886 } 1887 VI_UNLOCK(bp->b_vp); 1888 bp->b_vp = (struct vnode *) 0; 1889 bp->b_flags &= ~B_PAGING; 1890} 1891 1892/* 1893 * Reassign a buffer from one vnode to another. 1894 * Used to assign file specific control information 1895 * (indirect blocks) to the vnode to which they belong. 1896 */ 1897void 1898reassignbuf(bp, newvp) 1899 register struct buf *bp; 1900 register struct vnode *newvp; 1901{ 1902 int delay; 1903 int s; 1904 1905 if (newvp == NULL) { 1906 printf("reassignbuf: NULL"); 1907 return; 1908 } 1909 ++reassignbufcalls; 1910 1911 /* 1912 * B_PAGING flagged buffers cannot be reassigned because their vp 1913 * is not fully linked in. 1914 */ 1915 if (bp->b_flags & B_PAGING) 1916 panic("cannot reassign paging buffer"); 1917 1918 s = splbio(); 1919 /* 1920 * Delete from old vnode list, if on one. 1921 */ 1922 VI_LOCK(bp->b_vp); 1923 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) { 1924 buf_vlist_remove(bp); 1925 if (bp->b_vp != newvp) { 1926 vdropl(bp->b_vp); 1927 bp->b_vp = NULL; /* for clarification */ 1928 } 1929 } 1930 VI_UNLOCK(bp->b_vp); 1931 /* 1932 * If dirty, put on list of dirty buffers; otherwise insert onto list 1933 * of clean buffers. 1934 */ 1935 VI_LOCK(newvp); 1936 if (bp->b_flags & B_DELWRI) { 1937 if ((newvp->v_iflag & VI_ONWORKLST) == 0) { 1938 switch (newvp->v_type) { 1939 case VDIR: 1940 delay = dirdelay; 1941 break; 1942 case VCHR: 1943 if (newvp->v_rdev->si_mountpoint != NULL) { 1944 delay = metadelay; 1945 break; 1946 } 1947 /* FALLTHROUGH */ 1948 default: 1949 delay = filedelay; 1950 } 1951 vn_syncer_add_to_worklist(newvp, delay); 1952 } 1953 buf_vlist_add(bp, newvp, BX_VNDIRTY); 1954 } else { 1955 buf_vlist_add(bp, newvp, BX_VNCLEAN); 1956 1957 if ((newvp->v_iflag & VI_ONWORKLST) && 1958 TAILQ_EMPTY(&newvp->v_dirtyblkhd)) { 1959 mtx_lock(&sync_mtx); 1960 LIST_REMOVE(newvp, v_synclist); 1961 mtx_unlock(&sync_mtx); 1962 newvp->v_iflag &= ~VI_ONWORKLST; 1963 } 1964 } 1965 if (bp->b_vp != newvp) { 1966 bp->b_vp = newvp; 1967 vholdl(bp->b_vp); 1968 } 1969 VI_UNLOCK(newvp); 1970 splx(s); 1971} 1972 1973/* 1974 * Create a vnode for a device. 1975 * Used for mounting the root filesystem. 1976 */ 1977int 1978bdevvp(dev, vpp) 1979 dev_t dev; 1980 struct vnode **vpp; 1981{ 1982 register struct vnode *vp; 1983 struct vnode *nvp; 1984 int error; 1985 1986 if (dev == NODEV) { 1987 *vpp = NULLVP; 1988 return (ENXIO); 1989 } 1990 if (vfinddev(dev, VCHR, vpp)) 1991 return (0); 1992 error = getnewvnode("none", (struct mount *)0, spec_vnodeop_p, &nvp); 1993 if (error) { 1994 *vpp = NULLVP; 1995 return (error); 1996 } 1997 vp = nvp; 1998 vp->v_type = VCHR; 1999 addalias(vp, dev); 2000 *vpp = vp; 2001 return (0); 2002} 2003 2004static void 2005v_incr_usecount(struct vnode *vp, int delta) 2006{ 2007 vp->v_usecount += delta; 2008 if (vp->v_type == VCHR && vp->v_rdev != NULL) { 2009 mtx_lock(&spechash_mtx); 2010 vp->v_rdev->si_usecount += delta; 2011 mtx_unlock(&spechash_mtx); 2012 } 2013} 2014 2015/* 2016 * Add vnode to the alias list hung off the dev_t. 2017 * 2018 * The reason for this gunk is that multiple vnodes can reference 2019 * the same physical device, so checking vp->v_usecount to see 2020 * how many users there are is inadequate; the v_usecount for 2021 * the vnodes need to be accumulated. vcount() does that. 2022 */ 2023struct vnode * 2024addaliasu(nvp, nvp_rdev) 2025 struct vnode *nvp; 2026 udev_t nvp_rdev; 2027{ 2028 struct vnode *ovp; 2029 vop_t **ops; 2030 dev_t dev; 2031 2032 if (nvp->v_type == VBLK) 2033 return (nvp); 2034 if (nvp->v_type != VCHR) 2035 panic("addaliasu on non-special vnode"); 2036 dev = udev2dev(nvp_rdev, 0); 2037 /* 2038 * Check to see if we have a bdevvp vnode with no associated 2039 * filesystem. If so, we want to associate the filesystem of 2040 * the new newly instigated vnode with the bdevvp vnode and 2041 * discard the newly created vnode rather than leaving the 2042 * bdevvp vnode lying around with no associated filesystem. 2043 */ 2044 if (vfinddev(dev, nvp->v_type, &ovp) == 0 || ovp->v_data != NULL) { 2045 addalias(nvp, dev); 2046 return (nvp); 2047 } 2048 /* 2049 * Discard unneeded vnode, but save its node specific data. 2050 * Note that if there is a lock, it is carried over in the 2051 * node specific data to the replacement vnode. 2052 */ 2053 vref(ovp); 2054 ovp->v_data = nvp->v_data; 2055 ovp->v_tag = nvp->v_tag; 2056 nvp->v_data = NULL; 2057 lockdestroy(ovp->v_vnlock); 2058 lockinit(ovp->v_vnlock, PVFS, nvp->v_vnlock->lk_wmesg, 2059 nvp->v_vnlock->lk_timo, nvp->v_vnlock->lk_flags & LK_EXTFLG_MASK); 2060 ops = ovp->v_op; 2061 ovp->v_op = nvp->v_op; 2062 if (VOP_ISLOCKED(nvp, curthread)) { 2063 VOP_UNLOCK(nvp, 0, curthread); 2064 vn_lock(ovp, LK_EXCLUSIVE | LK_RETRY, curthread); 2065 } 2066 nvp->v_op = ops; 2067 insmntque(ovp, nvp->v_mount); 2068 vrele(nvp); 2069 vgone(nvp); 2070 return (ovp); 2071} 2072 2073/* This is a local helper function that do the same as addaliasu, but for a 2074 * dev_t instead of an udev_t. */ 2075static void 2076addalias(nvp, dev) 2077 struct vnode *nvp; 2078 dev_t dev; 2079{ 2080 2081 KASSERT(nvp->v_type == VCHR, ("addalias on non-special vnode")); 2082 nvp->v_rdev = dev; 2083 VI_LOCK(nvp); 2084 mtx_lock(&spechash_mtx); 2085 SLIST_INSERT_HEAD(&dev->si_hlist, nvp, v_specnext); 2086 dev->si_usecount += nvp->v_usecount; 2087 mtx_unlock(&spechash_mtx); 2088 VI_UNLOCK(nvp); 2089} 2090 2091/* 2092 * Grab a particular vnode from the free list, increment its 2093 * reference count and lock it. The vnode lock bit is set if the 2094 * vnode is being eliminated in vgone. The process is awakened 2095 * when the transition is completed, and an error returned to 2096 * indicate that the vnode is no longer usable (possibly having 2097 * been changed to a new filesystem type). 2098 */ 2099int 2100vget(vp, flags, td) 2101 register struct vnode *vp; 2102 int flags; 2103 struct thread *td; 2104{ 2105 int error; 2106 2107 /* 2108 * If the vnode is in the process of being cleaned out for 2109 * another use, we wait for the cleaning to finish and then 2110 * return failure. Cleaning is determined by checking that 2111 * the VI_XLOCK flag is set. 2112 */ 2113 if ((flags & LK_INTERLOCK) == 0) 2114 VI_LOCK(vp); 2115 if (vp->v_iflag & VI_XLOCK && vp->v_vxproc != curthread) { 2116 vp->v_iflag |= VI_XWANT; 2117 msleep(vp, VI_MTX(vp), PINOD | PDROP, "vget", 0); 2118 return (ENOENT); 2119 } 2120 2121 v_incr_usecount(vp, 1); 2122 2123 if (VSHOULDBUSY(vp)) 2124 vbusy(vp); 2125 if (flags & LK_TYPE_MASK) { 2126 if ((error = vn_lock(vp, flags | LK_INTERLOCK, td)) != 0) { 2127 /* 2128 * must expand vrele here because we do not want 2129 * to call VOP_INACTIVE if the reference count 2130 * drops back to zero since it was never really 2131 * active. We must remove it from the free list 2132 * before sleeping so that multiple processes do 2133 * not try to recycle it. 2134 */ 2135 VI_LOCK(vp); 2136 v_incr_usecount(vp, -1); 2137 if (VSHOULDFREE(vp)) 2138 vfree(vp); 2139 else 2140 vlruvp(vp); 2141 VI_UNLOCK(vp); 2142 } 2143 return (error); 2144 } 2145 VI_UNLOCK(vp); 2146 return (0); 2147} 2148 2149/* 2150 * Increase the reference count of a vnode. 2151 */ 2152void 2153vref(struct vnode *vp) 2154{ 2155 VI_LOCK(vp); 2156 v_incr_usecount(vp, 1); 2157 VI_UNLOCK(vp); 2158} 2159 2160/* 2161 * Return reference count of a vnode. 2162 * 2163 * The results of this call are only guaranteed when some mechanism other 2164 * than the VI lock is used to stop other processes from gaining references 2165 * to the vnode. This may be the case if the caller holds the only reference. 2166 * This is also useful when stale data is acceptable as race conditions may 2167 * be accounted for by some other means. 2168 */ 2169int 2170vrefcnt(struct vnode *vp) 2171{ 2172 int usecnt; 2173 2174 VI_LOCK(vp); 2175 usecnt = vp->v_usecount; 2176 VI_UNLOCK(vp); 2177 2178 return (usecnt); 2179} 2180 2181 2182/* 2183 * Vnode put/release. 2184 * If count drops to zero, call inactive routine and return to freelist. 2185 */ 2186void 2187vrele(vp) 2188 struct vnode *vp; 2189{ 2190 struct thread *td = curthread; /* XXX */ 2191 2192 KASSERT(vp != NULL, ("vrele: null vp")); 2193 2194 VI_LOCK(vp); 2195 2196 /* Skip this v_writecount check if we're going to panic below. */ 2197 KASSERT(vp->v_writecount < vp->v_usecount || vp->v_usecount < 1, 2198 ("vrele: missed vn_close")); 2199 2200 if (vp->v_usecount > 1 || ((vp->v_iflag & VI_DOINGINACT) && 2201 vp->v_usecount == 1)) { 2202 v_incr_usecount(vp, -1); 2203 VI_UNLOCK(vp); 2204 2205 return; 2206 } 2207 2208 if (vp->v_usecount == 1) { 2209 v_incr_usecount(vp, -1); 2210 /* 2211 * We must call VOP_INACTIVE with the node locked. Mark 2212 * as VI_DOINGINACT to avoid recursion. 2213 */ 2214 if (vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK, td) == 0) { 2215 VI_LOCK(vp); 2216 vp->v_iflag |= VI_DOINGINACT; 2217 VI_UNLOCK(vp); 2218 VOP_INACTIVE(vp, td); 2219 VI_LOCK(vp); 2220 KASSERT(vp->v_iflag & VI_DOINGINACT, 2221 ("vrele: lost VI_DOINGINACT")); 2222 vp->v_iflag &= ~VI_DOINGINACT; 2223 VI_UNLOCK(vp); 2224 } 2225 VI_LOCK(vp); 2226 if (VSHOULDFREE(vp)) 2227 vfree(vp); 2228 else 2229 vlruvp(vp); 2230 VI_UNLOCK(vp); 2231 2232 } else { 2233#ifdef DIAGNOSTIC 2234 vprint("vrele: negative ref count", vp); 2235#endif 2236 VI_UNLOCK(vp); 2237 panic("vrele: negative ref cnt"); 2238 } 2239} 2240 2241/* 2242 * Release an already locked vnode. This give the same effects as 2243 * unlock+vrele(), but takes less time and avoids releasing and 2244 * re-aquiring the lock (as vrele() aquires the lock internally.) 2245 */ 2246void 2247vput(vp) 2248 struct vnode *vp; 2249{ 2250 struct thread *td = curthread; /* XXX */ 2251 2252 GIANT_REQUIRED; 2253 2254 KASSERT(vp != NULL, ("vput: null vp")); 2255 VI_LOCK(vp); 2256 /* Skip this v_writecount check if we're going to panic below. */ 2257 KASSERT(vp->v_writecount < vp->v_usecount || vp->v_usecount < 1, 2258 ("vput: missed vn_close")); 2259 2260 if (vp->v_usecount > 1 || ((vp->v_iflag & VI_DOINGINACT) && 2261 vp->v_usecount == 1)) { 2262 v_incr_usecount(vp, -1); 2263 VOP_UNLOCK(vp, LK_INTERLOCK, td); 2264 return; 2265 } 2266 2267 if (vp->v_usecount == 1) { 2268 v_incr_usecount(vp, -1); 2269 /* 2270 * We must call VOP_INACTIVE with the node locked, so 2271 * we just need to release the vnode mutex. Mark as 2272 * as VI_DOINGINACT to avoid recursion. 2273 */ 2274 vp->v_iflag |= VI_DOINGINACT; 2275 VI_UNLOCK(vp); 2276 VOP_INACTIVE(vp, td); 2277 VI_LOCK(vp); 2278 KASSERT(vp->v_iflag & VI_DOINGINACT, 2279 ("vput: lost VI_DOINGINACT")); 2280 vp->v_iflag &= ~VI_DOINGINACT; 2281 if (VSHOULDFREE(vp)) 2282 vfree(vp); 2283 else 2284 vlruvp(vp); 2285 VI_UNLOCK(vp); 2286 2287 } else { 2288#ifdef DIAGNOSTIC 2289 vprint("vput: negative ref count", vp); 2290#endif 2291 panic("vput: negative ref cnt"); 2292 } 2293} 2294 2295/* 2296 * Somebody doesn't want the vnode recycled. 2297 */ 2298void 2299vhold(struct vnode *vp) 2300{ 2301 VI_LOCK(vp); 2302 vholdl(vp); 2303 VI_UNLOCK(vp); 2304} 2305 2306void 2307vholdl(vp) 2308 register struct vnode *vp; 2309{ 2310 int s; 2311 2312 s = splbio(); 2313 vp->v_holdcnt++; 2314 if (VSHOULDBUSY(vp)) 2315 vbusy(vp); 2316 splx(s); 2317} 2318 2319/* 2320 * Note that there is one less who cares about this vnode. vdrop() is the 2321 * opposite of vhold(). 2322 */ 2323void 2324vdrop(struct vnode *vp) 2325{ 2326 VI_LOCK(vp); 2327 vdropl(vp); 2328 VI_UNLOCK(vp); 2329} 2330 2331void 2332vdropl(vp) 2333 register struct vnode *vp; 2334{ 2335 int s; 2336 2337 s = splbio(); 2338 if (vp->v_holdcnt <= 0) 2339 panic("vdrop: holdcnt"); 2340 vp->v_holdcnt--; 2341 if (VSHOULDFREE(vp)) 2342 vfree(vp); 2343 else 2344 vlruvp(vp); 2345 splx(s); 2346} 2347 2348/* 2349 * Remove any vnodes in the vnode table belonging to mount point mp. 2350 * 2351 * If FORCECLOSE is not specified, there should not be any active ones, 2352 * return error if any are found (nb: this is a user error, not a 2353 * system error). If FORCECLOSE is specified, detach any active vnodes 2354 * that are found. 2355 * 2356 * If WRITECLOSE is set, only flush out regular file vnodes open for 2357 * writing. 2358 * 2359 * SKIPSYSTEM causes any vnodes marked VV_SYSTEM to be skipped. 2360 * 2361 * `rootrefs' specifies the base reference count for the root vnode 2362 * of this filesystem. The root vnode is considered busy if its 2363 * v_usecount exceeds this value. On a successful return, vflush() 2364 * will call vrele() on the root vnode exactly rootrefs times. 2365 * If the SKIPSYSTEM or WRITECLOSE flags are specified, rootrefs must 2366 * be zero. 2367 */ 2368#ifdef DIAGNOSTIC 2369static int busyprt = 0; /* print out busy vnodes */ 2370SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, ""); 2371#endif 2372 2373int 2374vflush(mp, rootrefs, flags) 2375 struct mount *mp; 2376 int rootrefs; 2377 int flags; 2378{ 2379 struct thread *td = curthread; /* XXX */ 2380 struct vnode *vp, *nvp, *rootvp = NULL; 2381 struct vattr vattr; 2382 int busy = 0, error; 2383 2384 if (rootrefs > 0) { 2385 KASSERT((flags & (SKIPSYSTEM | WRITECLOSE)) == 0, 2386 ("vflush: bad args")); 2387 /* 2388 * Get the filesystem root vnode. We can vput() it 2389 * immediately, since with rootrefs > 0, it won't go away. 2390 */ 2391 if ((error = VFS_ROOT(mp, &rootvp)) != 0) 2392 return (error); 2393 vput(rootvp); 2394 2395 } 2396 mtx_lock(&mntvnode_mtx); 2397loop: 2398 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp; vp = nvp) { 2399 /* 2400 * Make sure this vnode wasn't reclaimed in getnewvnode(). 2401 * Start over if it has (it won't be on the list anymore). 2402 */ 2403 if (vp->v_mount != mp) 2404 goto loop; 2405 nvp = TAILQ_NEXT(vp, v_nmntvnodes); 2406 2407 VI_LOCK(vp); 2408 mtx_unlock(&mntvnode_mtx); 2409 vn_lock(vp, LK_INTERLOCK | LK_EXCLUSIVE | LK_RETRY, td); 2410 /* 2411 * This vnode could have been reclaimed while we were 2412 * waiting for the lock since we are not holding a 2413 * reference. 2414 * Start over if the vnode was reclaimed. 2415 */ 2416 if (vp->v_mount != mp) { 2417 VOP_UNLOCK(vp, 0, td); 2418 mtx_lock(&mntvnode_mtx); 2419 goto loop; 2420 } 2421 /* 2422 * Skip over a vnodes marked VV_SYSTEM. 2423 */ 2424 if ((flags & SKIPSYSTEM) && (vp->v_vflag & VV_SYSTEM)) { 2425 VOP_UNLOCK(vp, 0, td); 2426 mtx_lock(&mntvnode_mtx); 2427 continue; 2428 } 2429 /* 2430 * If WRITECLOSE is set, flush out unlinked but still open 2431 * files (even if open only for reading) and regular file 2432 * vnodes open for writing. 2433 */ 2434 if (flags & WRITECLOSE) { 2435 error = VOP_GETATTR(vp, &vattr, td->td_ucred, td); 2436 VI_LOCK(vp); 2437 2438 if ((vp->v_type == VNON || 2439 (error == 0 && vattr.va_nlink > 0)) && 2440 (vp->v_writecount == 0 || vp->v_type != VREG)) { 2441 VOP_UNLOCK(vp, LK_INTERLOCK, td); 2442 mtx_lock(&mntvnode_mtx); 2443 continue; 2444 } 2445 } else 2446 VI_LOCK(vp); 2447 2448 VOP_UNLOCK(vp, 0, td); 2449 2450 /* 2451 * With v_usecount == 0, all we need to do is clear out the 2452 * vnode data structures and we are done. 2453 */ 2454 if (vp->v_usecount == 0) { 2455 vgonel(vp, td); 2456 mtx_lock(&mntvnode_mtx); 2457 continue; 2458 } 2459 2460 /* 2461 * If FORCECLOSE is set, forcibly close the vnode. For block 2462 * or character devices, revert to an anonymous device. For 2463 * all other files, just kill them. 2464 */ 2465 if (flags & FORCECLOSE) { 2466 if (vp->v_type != VCHR) { 2467 vgonel(vp, td); 2468 } else { 2469 vclean(vp, 0, td); 2470 VI_UNLOCK(vp); 2471 vp->v_op = spec_vnodeop_p; 2472 insmntque(vp, (struct mount *) 0); 2473 } 2474 mtx_lock(&mntvnode_mtx); 2475 continue; 2476 } 2477#ifdef DIAGNOSTIC 2478 if (busyprt) 2479 vprint("vflush: busy vnode", vp); 2480#endif 2481 VI_UNLOCK(vp); 2482 mtx_lock(&mntvnode_mtx); 2483 busy++; 2484 } 2485 mtx_unlock(&mntvnode_mtx); 2486 if (rootrefs > 0 && (flags & FORCECLOSE) == 0) { 2487 /* 2488 * If just the root vnode is busy, and if its refcount 2489 * is equal to `rootrefs', then go ahead and kill it. 2490 */ 2491 VI_LOCK(rootvp); 2492 KASSERT(busy > 0, ("vflush: not busy")); 2493 KASSERT(rootvp->v_usecount >= rootrefs, ("vflush: rootrefs")); 2494 if (busy == 1 && rootvp->v_usecount == rootrefs) { 2495 vgonel(rootvp, td); 2496 busy = 0; 2497 } else 2498 VI_UNLOCK(rootvp); 2499 } 2500 if (busy) 2501 return (EBUSY); 2502 for (; rootrefs > 0; rootrefs--) 2503 vrele(rootvp); 2504 return (0); 2505} 2506 2507/* 2508 * This moves a now (likely recyclable) vnode to the end of the 2509 * mountlist. XXX However, it is temporarily disabled until we 2510 * can clean up ffs_sync() and friends, which have loop restart 2511 * conditions which this code causes to operate O(N^2). 2512 */ 2513static void 2514vlruvp(struct vnode *vp) 2515{ 2516#if 0 2517 struct mount *mp; 2518 2519 if ((mp = vp->v_mount) != NULL) { 2520 mtx_lock(&mntvnode_mtx); 2521 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes); 2522 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes); 2523 mtx_unlock(&mntvnode_mtx); 2524 } 2525#endif 2526} 2527 2528/* 2529 * Disassociate the underlying filesystem from a vnode. 2530 */ 2531static void 2532vclean(vp, flags, td) 2533 struct vnode *vp; 2534 int flags; 2535 struct thread *td; 2536{ 2537 int active; 2538 2539 ASSERT_VI_LOCKED(vp, "vclean"); 2540 /* 2541 * Check to see if the vnode is in use. If so we have to reference it 2542 * before we clean it out so that its count cannot fall to zero and 2543 * generate a race against ourselves to recycle it. 2544 */ 2545 if ((active = vp->v_usecount)) 2546 v_incr_usecount(vp, 1); 2547 2548 /* 2549 * Prevent the vnode from being recycled or brought into use while we 2550 * clean it out. 2551 */ 2552 if (vp->v_iflag & VI_XLOCK) 2553 panic("vclean: deadlock"); 2554 vp->v_iflag |= VI_XLOCK; 2555 vp->v_vxproc = curthread; 2556 /* 2557 * Even if the count is zero, the VOP_INACTIVE routine may still 2558 * have the object locked while it cleans it out. The VOP_LOCK 2559 * ensures that the VOP_INACTIVE routine is done with its work. 2560 * For active vnodes, it ensures that no other activity can 2561 * occur while the underlying object is being cleaned out. 2562 */ 2563 VOP_LOCK(vp, LK_DRAIN | LK_INTERLOCK, td); 2564 2565 /* 2566 * Clean out any buffers associated with the vnode. 2567 * If the flush fails, just toss the buffers. 2568 */ 2569 if (flags & DOCLOSE) { 2570 struct buf *bp; 2571 VI_LOCK(vp); 2572 bp = TAILQ_FIRST(&vp->v_dirtyblkhd); 2573 VI_UNLOCK(vp); 2574 if (bp != NULL) 2575 (void) vn_write_suspend_wait(vp, NULL, V_WAIT); 2576 if (vinvalbuf(vp, V_SAVE, NOCRED, td, 0, 0) != 0) 2577 vinvalbuf(vp, 0, NOCRED, td, 0, 0); 2578 } 2579 2580 VOP_DESTROYVOBJECT(vp); 2581 2582 /* 2583 * Any other processes trying to obtain this lock must first 2584 * wait for VXLOCK to clear, then call the new lock operation. 2585 */ 2586 VOP_UNLOCK(vp, 0, td); 2587 2588 /* 2589 * If purging an active vnode, it must be closed and 2590 * deactivated before being reclaimed. Note that the 2591 * VOP_INACTIVE will unlock the vnode. 2592 */ 2593 if (active) { 2594 if (flags & DOCLOSE) 2595 VOP_CLOSE(vp, FNONBLOCK, NOCRED, td); 2596 VI_LOCK(vp); 2597 if ((vp->v_iflag & VI_DOINGINACT) == 0) { 2598 vp->v_iflag |= VI_DOINGINACT; 2599 VI_UNLOCK(vp); 2600 if (vn_lock(vp, LK_EXCLUSIVE | LK_NOWAIT, td) != 0) 2601 panic("vclean: cannot relock."); 2602 VOP_INACTIVE(vp, td); 2603 VI_LOCK(vp); 2604 KASSERT(vp->v_iflag & VI_DOINGINACT, 2605 ("vclean: lost VI_DOINGINACT")); 2606 vp->v_iflag &= ~VI_DOINGINACT; 2607 } 2608 VI_UNLOCK(vp); 2609 } 2610 2611 /* 2612 * Reclaim the vnode. 2613 */ 2614 if (VOP_RECLAIM(vp, td)) 2615 panic("vclean: cannot reclaim"); 2616 2617 if (active) { 2618 /* 2619 * Inline copy of vrele() since VOP_INACTIVE 2620 * has already been called. 2621 */ 2622 VI_LOCK(vp); 2623 v_incr_usecount(vp, -1); 2624 if (vp->v_usecount <= 0) { 2625#ifdef DIAGNOSTIC 2626 if (vp->v_usecount < 0 || vp->v_writecount != 0) { 2627 vprint("vclean: bad ref count", vp); 2628 panic("vclean: ref cnt"); 2629 } 2630#endif 2631 vfree(vp); 2632 } 2633 VI_UNLOCK(vp); 2634 } 2635 2636 cache_purge(vp); 2637 VI_LOCK(vp); 2638 if (VSHOULDFREE(vp)) 2639 vfree(vp); 2640 2641 /* 2642 * Done with purge, reset to the standard lock and 2643 * notify sleepers of the grim news. 2644 */ 2645 vp->v_vnlock = &vp->v_lock; 2646 vp->v_op = dead_vnodeop_p; 2647 if (vp->v_pollinfo != NULL) 2648 vn_pollgone(vp); 2649 vp->v_tag = "none"; 2650 vp->v_iflag &= ~VI_XLOCK; 2651 vp->v_vxproc = NULL; 2652 if (vp->v_iflag & VI_XWANT) { 2653 vp->v_iflag &= ~VI_XWANT; 2654 wakeup(vp); 2655 } 2656} 2657 2658/* 2659 * Eliminate all activity associated with the requested vnode 2660 * and with all vnodes aliased to the requested vnode. 2661 */ 2662int 2663vop_revoke(ap) 2664 struct vop_revoke_args /* { 2665 struct vnode *a_vp; 2666 int a_flags; 2667 } */ *ap; 2668{ 2669 struct vnode *vp, *vq; 2670 dev_t dev; 2671 2672 KASSERT((ap->a_flags & REVOKEALL) != 0, ("vop_revoke")); 2673 vp = ap->a_vp; 2674 KASSERT((vp->v_type == VCHR), ("vop_revoke: not VCHR")); 2675 2676 VI_LOCK(vp); 2677 /* 2678 * If a vgone (or vclean) is already in progress, 2679 * wait until it is done and return. 2680 */ 2681 if (vp->v_iflag & VI_XLOCK) { 2682 vp->v_iflag |= VI_XWANT; 2683 msleep(vp, VI_MTX(vp), PINOD | PDROP, 2684 "vop_revokeall", 0); 2685 return (0); 2686 } 2687 VI_UNLOCK(vp); 2688 dev = vp->v_rdev; 2689 for (;;) { 2690 mtx_lock(&spechash_mtx); 2691 vq = SLIST_FIRST(&dev->si_hlist); 2692 mtx_unlock(&spechash_mtx); 2693 if (!vq) 2694 break; 2695 vgone(vq); 2696 } 2697 return (0); 2698} 2699 2700/* 2701 * Recycle an unused vnode to the front of the free list. 2702 * Release the passed interlock if the vnode will be recycled. 2703 */ 2704int 2705vrecycle(vp, inter_lkp, td) 2706 struct vnode *vp; 2707 struct mtx *inter_lkp; 2708 struct thread *td; 2709{ 2710 2711 VI_LOCK(vp); 2712 if (vp->v_usecount == 0) { 2713 if (inter_lkp) { 2714 mtx_unlock(inter_lkp); 2715 } 2716 vgonel(vp, td); 2717 return (1); 2718 } 2719 VI_UNLOCK(vp); 2720 return (0); 2721} 2722 2723/* 2724 * Eliminate all activity associated with a vnode 2725 * in preparation for reuse. 2726 */ 2727void 2728vgone(vp) 2729 register struct vnode *vp; 2730{ 2731 struct thread *td = curthread; /* XXX */ 2732 2733 VI_LOCK(vp); 2734 vgonel(vp, td); 2735} 2736 2737/* 2738 * vgone, with the vp interlock held. 2739 */ 2740void 2741vgonel(vp, td) 2742 struct vnode *vp; 2743 struct thread *td; 2744{ 2745 int s; 2746 2747 /* 2748 * If a vgone (or vclean) is already in progress, 2749 * wait until it is done and return. 2750 */ 2751 ASSERT_VI_LOCKED(vp, "vgonel"); 2752 if (vp->v_iflag & VI_XLOCK) { 2753 vp->v_iflag |= VI_XWANT; 2754 msleep(vp, VI_MTX(vp), PINOD | PDROP, "vgone", 0); 2755 return; 2756 } 2757 2758 /* 2759 * Clean out the filesystem specific data. 2760 */ 2761 vclean(vp, DOCLOSE, td); 2762 VI_UNLOCK(vp); 2763 2764 /* 2765 * Delete from old mount point vnode list, if on one. 2766 */ 2767 if (vp->v_mount != NULL) 2768 insmntque(vp, (struct mount *)0); 2769 /* 2770 * If special device, remove it from special device alias list 2771 * if it is on one. 2772 */ 2773 if (vp->v_type == VCHR && vp->v_rdev != NULL && vp->v_rdev != NODEV) { 2774 VI_LOCK(vp); 2775 mtx_lock(&spechash_mtx); 2776 SLIST_REMOVE(&vp->v_rdev->si_hlist, vp, vnode, v_specnext); 2777 vp->v_rdev->si_usecount -= vp->v_usecount; 2778 mtx_unlock(&spechash_mtx); 2779 VI_UNLOCK(vp); 2780 vp->v_rdev = NULL; 2781 } 2782 2783 /* 2784 * If it is on the freelist and not already at the head, 2785 * move it to the head of the list. The test of the 2786 * VDOOMED flag and the reference count of zero is because 2787 * it will be removed from the free list by getnewvnode, 2788 * but will not have its reference count incremented until 2789 * after calling vgone. If the reference count were 2790 * incremented first, vgone would (incorrectly) try to 2791 * close the previous instance of the underlying object. 2792 */ 2793 VI_LOCK(vp); 2794 if (vp->v_usecount == 0 && !(vp->v_iflag & VI_DOOMED)) { 2795 s = splbio(); 2796 mtx_lock(&vnode_free_list_mtx); 2797 if (vp->v_iflag & VI_FREE) { 2798 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist); 2799 } else { 2800 vp->v_iflag |= VI_FREE; 2801 freevnodes++; 2802 } 2803 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist); 2804 mtx_unlock(&vnode_free_list_mtx); 2805 splx(s); 2806 } 2807 2808 vp->v_type = VBAD; 2809 VI_UNLOCK(vp); 2810} 2811 2812/* 2813 * Lookup a vnode by device number. 2814 */ 2815int 2816vfinddev(dev, type, vpp) 2817 dev_t dev; 2818 enum vtype type; 2819 struct vnode **vpp; 2820{ 2821 struct vnode *vp; 2822 2823 mtx_lock(&spechash_mtx); 2824 SLIST_FOREACH(vp, &dev->si_hlist, v_specnext) { 2825 if (type == vp->v_type) { 2826 *vpp = vp; 2827 mtx_unlock(&spechash_mtx); 2828 return (1); 2829 } 2830 } 2831 mtx_unlock(&spechash_mtx); 2832 return (0); 2833} 2834 2835/* 2836 * Calculate the total number of references to a special device. 2837 */ 2838int 2839vcount(vp) 2840 struct vnode *vp; 2841{ 2842 int count; 2843 2844 mtx_lock(&spechash_mtx); 2845 count = vp->v_rdev->si_usecount; 2846 mtx_unlock(&spechash_mtx); 2847 return (count); 2848} 2849 2850/* 2851 * Same as above, but using the dev_t as argument 2852 */ 2853int 2854count_dev(dev) 2855 dev_t dev; 2856{ 2857 struct vnode *vp; 2858 2859 vp = SLIST_FIRST(&dev->si_hlist); 2860 if (vp == NULL) 2861 return (0); 2862 return(vcount(vp)); 2863} 2864 2865/* 2866 * Print out a description of a vnode. 2867 */ 2868static char *typename[] = 2869{"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD"}; 2870 2871void 2872vprint(label, vp) 2873 char *label; 2874 struct vnode *vp; 2875{ 2876 char buf[96]; 2877 2878 if (label != NULL) 2879 printf("%s: %p: ", label, (void *)vp); 2880 else 2881 printf("%p: ", (void *)vp); 2882 printf("tag %s, type %s, usecount %d, writecount %d, refcount %d,", 2883 vp->v_tag, typename[vp->v_type], vp->v_usecount, 2884 vp->v_writecount, vp->v_holdcnt); 2885 buf[0] = '\0'; 2886 if (vp->v_vflag & VV_ROOT) 2887 strcat(buf, "|VV_ROOT"); 2888 if (vp->v_vflag & VV_TEXT) 2889 strcat(buf, "|VV_TEXT"); 2890 if (vp->v_vflag & VV_SYSTEM) 2891 strcat(buf, "|VV_SYSTEM"); 2892 if (vp->v_iflag & VI_XLOCK) 2893 strcat(buf, "|VI_XLOCK"); 2894 if (vp->v_iflag & VI_XWANT) 2895 strcat(buf, "|VI_XWANT"); 2896 if (vp->v_iflag & VI_BWAIT) 2897 strcat(buf, "|VI_BWAIT"); 2898 if (vp->v_iflag & VI_DOOMED) 2899 strcat(buf, "|VI_DOOMED"); 2900 if (vp->v_iflag & VI_FREE) 2901 strcat(buf, "|VI_FREE"); 2902 if (vp->v_vflag & VV_OBJBUF) 2903 strcat(buf, "|VV_OBJBUF"); 2904 if (buf[0] != '\0') 2905 printf(" flags (%s),", &buf[1]); 2906 lockmgr_printinfo(vp->v_vnlock); 2907 printf("\n"); 2908 if (vp->v_data != NULL) 2909 VOP_PRINT(vp); 2910} 2911 2912#ifdef DDB 2913#include <ddb/ddb.h> 2914/* 2915 * List all of the locked vnodes in the system. 2916 * Called when debugging the kernel. 2917 */ 2918DB_SHOW_COMMAND(lockedvnods, lockedvnodes) 2919{ 2920 struct mount *mp, *nmp; 2921 struct vnode *vp; 2922 2923 /* 2924 * Note: because this is DDB, we can't obey the locking semantics 2925 * for these structures, which means we could catch an inconsistent 2926 * state and dereference a nasty pointer. Not much to be done 2927 * about that. 2928 */ 2929 printf("Locked vnodes\n"); 2930 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) { 2931 nmp = TAILQ_NEXT(mp, mnt_list); 2932 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) { 2933 if (VOP_ISLOCKED(vp, NULL)) 2934 vprint(NULL, vp); 2935 } 2936 nmp = TAILQ_NEXT(mp, mnt_list); 2937 } 2938} 2939#endif 2940 2941/* 2942 * Fill in a struct xvfsconf based on a struct vfsconf. 2943 */ 2944static void 2945vfsconf2x(struct vfsconf *vfsp, struct xvfsconf *xvfsp) 2946{ 2947 2948 strcpy(xvfsp->vfc_name, vfsp->vfc_name); 2949 xvfsp->vfc_typenum = vfsp->vfc_typenum; 2950 xvfsp->vfc_refcount = vfsp->vfc_refcount; 2951 xvfsp->vfc_flags = vfsp->vfc_flags; 2952 /* 2953 * These are unused in userland, we keep them 2954 * to not break binary compatibility. 2955 */ 2956 xvfsp->vfc_vfsops = NULL; 2957 xvfsp->vfc_next = NULL; 2958} 2959 2960static int 2961sysctl_vfs_conflist(SYSCTL_HANDLER_ARGS) 2962{ 2963 struct vfsconf *vfsp; 2964 struct xvfsconf *xvfsp; 2965 int cnt, error, i; 2966 2967 cnt = 0; 2968 for (vfsp = vfsconf; vfsp != NULL; vfsp = vfsp->vfc_next) 2969 cnt++; 2970 xvfsp = malloc(sizeof(struct xvfsconf) * cnt, M_TEMP, M_WAITOK); 2971 /* 2972 * Handle the race that we will have here when struct vfsconf 2973 * will be locked down by using both cnt and checking vfc_next 2974 * against NULL to determine the end of the loop. The race will 2975 * happen because we will have to unlock before calling malloc(). 2976 * We are protected by Giant for now. 2977 */ 2978 i = 0; 2979 for (vfsp = vfsconf; vfsp != NULL && i < cnt; vfsp = vfsp->vfc_next) { 2980 vfsconf2x(vfsp, xvfsp + i); 2981 i++; 2982 } 2983 error = SYSCTL_OUT(req, xvfsp, sizeof(struct xvfsconf) * i); 2984 free(xvfsp, M_TEMP); 2985 return (error); 2986} 2987 2988SYSCTL_PROC(_vfs, OID_AUTO, conflist, CTLFLAG_RD, NULL, 0, sysctl_vfs_conflist, 2989 "S,xvfsconf", "List of all configured filesystems"); 2990 2991/* 2992 * Top level filesystem related information gathering. 2993 */ 2994static int sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS); 2995 2996static int 2997vfs_sysctl(SYSCTL_HANDLER_ARGS) 2998{ 2999 int *name = (int *)arg1 - 1; /* XXX */ 3000 u_int namelen = arg2 + 1; /* XXX */ 3001 struct vfsconf *vfsp; 3002 struct xvfsconf xvfsp; 3003 3004 printf("WARNING: userland calling deprecated sysctl, " 3005 "please rebuild world\n"); 3006 3007#if 1 || defined(COMPAT_PRELITE2) 3008 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */ 3009 if (namelen == 1) 3010 return (sysctl_ovfs_conf(oidp, arg1, arg2, req)); 3011#endif 3012 3013 switch (name[1]) { 3014 case VFS_MAXTYPENUM: 3015 if (namelen != 2) 3016 return (ENOTDIR); 3017 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int))); 3018 case VFS_CONF: 3019 if (namelen != 3) 3020 return (ENOTDIR); /* overloaded */ 3021 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) 3022 if (vfsp->vfc_typenum == name[2]) 3023 break; 3024 if (vfsp == NULL) 3025 return (EOPNOTSUPP); 3026 vfsconf2x(vfsp, &xvfsp); 3027 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp))); 3028 } 3029 return (EOPNOTSUPP); 3030} 3031 3032SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD | CTLFLAG_SKIP, vfs_sysctl, 3033 "Generic filesystem"); 3034 3035#if 1 || defined(COMPAT_PRELITE2) 3036 3037static int 3038sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS) 3039{ 3040 int error; 3041 struct vfsconf *vfsp; 3042 struct ovfsconf ovfs; 3043 3044 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) { 3045 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */ 3046 strcpy(ovfs.vfc_name, vfsp->vfc_name); 3047 ovfs.vfc_index = vfsp->vfc_typenum; 3048 ovfs.vfc_refcount = vfsp->vfc_refcount; 3049 ovfs.vfc_flags = vfsp->vfc_flags; 3050 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs); 3051 if (error) 3052 return error; 3053 } 3054 return 0; 3055} 3056 3057#endif /* 1 || COMPAT_PRELITE2 */ 3058 3059#define KINFO_VNODESLOP 10 3060#ifdef notyet 3061/* 3062 * Dump vnode list (via sysctl). 3063 */ 3064/* ARGSUSED */ 3065static int 3066sysctl_vnode(SYSCTL_HANDLER_ARGS) 3067{ 3068 struct xvnode *xvn; 3069 struct thread *td = req->td; 3070 struct mount *mp; 3071 struct vnode *vp; 3072 int error, len, n; 3073 3074 /* 3075 * Stale numvnodes access is not fatal here. 3076 */ 3077 req->lock = 0; 3078 len = (numvnodes + KINFO_VNODESLOP) * sizeof *xvn; 3079 if (!req->oldptr) 3080 /* Make an estimate */ 3081 return (SYSCTL_OUT(req, 0, len)); 3082 3083 sysctl_wire_old_buffer(req, 0); 3084 xvn = malloc(len, M_TEMP, M_ZERO | M_WAITOK); 3085 n = 0; 3086 mtx_lock(&mountlist_mtx); 3087 TAILQ_FOREACH(mp, &mountlist, mnt_list) { 3088 if (vfs_busy(mp, LK_NOWAIT, &mountlist_mtx, td)) 3089 continue; 3090 mtx_lock(&mntvnode_mtx); 3091 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) { 3092 if (n == len) 3093 break; 3094 vref(vp); 3095 xvn[n].xv_size = sizeof *xvn; 3096 xvn[n].xv_vnode = vp; 3097#define XV_COPY(field) xvn[n].xv_##field = vp->v_##field 3098 XV_COPY(usecount); 3099 XV_COPY(writecount); 3100 XV_COPY(holdcnt); 3101 XV_COPY(id); 3102 XV_COPY(mount); 3103 XV_COPY(numoutput); 3104 XV_COPY(type); 3105#undef XV_COPY 3106 xvn[n].xv_flag = vp->v_vflag; 3107 3108 switch (vp->v_type) { 3109 case VREG: 3110 case VDIR: 3111 case VLNK: 3112 xvn[n].xv_dev = vp->v_cachedfs; 3113 xvn[n].xv_ino = vp->v_cachedid; 3114 break; 3115 case VBLK: 3116 case VCHR: 3117 if (vp->v_rdev == NULL) { 3118 vrele(vp); 3119 continue; 3120 } 3121 xvn[n].xv_dev = dev2udev(vp->v_rdev); 3122 break; 3123 case VSOCK: 3124 xvn[n].xv_socket = vp->v_socket; 3125 break; 3126 case VFIFO: 3127 xvn[n].xv_fifo = vp->v_fifoinfo; 3128 break; 3129 case VNON: 3130 case VBAD: 3131 default: 3132 /* shouldn't happen? */ 3133 vrele(vp); 3134 continue; 3135 } 3136 vrele(vp); 3137 ++n; 3138 } 3139 mtx_unlock(&mntvnode_mtx); 3140 mtx_lock(&mountlist_mtx); 3141 vfs_unbusy(mp, td); 3142 if (n == len) 3143 break; 3144 } 3145 mtx_unlock(&mountlist_mtx); 3146 3147 error = SYSCTL_OUT(req, xvn, n * sizeof *xvn); 3148 free(xvn, M_TEMP); 3149 return (error); 3150} 3151 3152SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE|CTLFLAG_RD, 3153 0, 0, sysctl_vnode, "S,xvnode", ""); 3154#endif 3155 3156/* 3157 * Check to see if a filesystem is mounted on a block device. 3158 */ 3159int 3160vfs_mountedon(vp) 3161 struct vnode *vp; 3162{ 3163 3164 if (vp->v_rdev->si_mountpoint != NULL) 3165 return (EBUSY); 3166 return (0); 3167} 3168 3169/* 3170 * Unmount all filesystems. The list is traversed in reverse order 3171 * of mounting to avoid dependencies. 3172 */ 3173void 3174vfs_unmountall() 3175{ 3176 struct mount *mp; 3177 struct thread *td; 3178 int error; 3179 3180 if (curthread != NULL) 3181 td = curthread; 3182 else 3183 td = FIRST_THREAD_IN_PROC(initproc); /* XXX XXX proc0? */ 3184 /* 3185 * Since this only runs when rebooting, it is not interlocked. 3186 */ 3187 while(!TAILQ_EMPTY(&mountlist)) { 3188 mp = TAILQ_LAST(&mountlist, mntlist); 3189 error = dounmount(mp, MNT_FORCE, td); 3190 if (error) { 3191 TAILQ_REMOVE(&mountlist, mp, mnt_list); 3192 printf("unmount of %s failed (", 3193 mp->mnt_stat.f_mntonname); 3194 if (error == EBUSY) 3195 printf("BUSY)\n"); 3196 else 3197 printf("%d)\n", error); 3198 } else { 3199 /* The unmount has removed mp from the mountlist */ 3200 } 3201 } 3202} 3203 3204/* 3205 * perform msync on all vnodes under a mount point 3206 * the mount point must be locked. 3207 */ 3208void 3209vfs_msync(struct mount *mp, int flags) 3210{ 3211 struct vnode *vp, *nvp; 3212 struct vm_object *obj; 3213 int tries; 3214 3215 GIANT_REQUIRED; 3216 3217 tries = 5; 3218 mtx_lock(&mntvnode_mtx); 3219loop: 3220 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp != NULL; vp = nvp) { 3221 if (vp->v_mount != mp) { 3222 if (--tries > 0) 3223 goto loop; 3224 break; 3225 } 3226 nvp = TAILQ_NEXT(vp, v_nmntvnodes); 3227 3228 VI_LOCK(vp); 3229 if (vp->v_iflag & VI_XLOCK) { /* XXX: what if MNT_WAIT? */ 3230 VI_UNLOCK(vp); 3231 continue; 3232 } 3233 3234 if ((vp->v_iflag & VI_OBJDIRTY) && 3235 (flags == MNT_WAIT || VOP_ISLOCKED(vp, NULL) == 0)) { 3236 mtx_unlock(&mntvnode_mtx); 3237 if (!vget(vp, 3238 LK_EXCLUSIVE | LK_RETRY | LK_INTERLOCK, 3239 curthread)) { 3240 if (vp->v_vflag & VV_NOSYNC) { /* unlinked */ 3241 vput(vp); 3242 mtx_lock(&mntvnode_mtx); 3243 continue; 3244 } 3245 3246 if (VOP_GETVOBJECT(vp, &obj) == 0) { 3247 VM_OBJECT_LOCK(obj); 3248 vm_object_page_clean(obj, 0, 0, 3249 flags == MNT_WAIT ? 3250 OBJPC_SYNC : OBJPC_NOSYNC); 3251 VM_OBJECT_UNLOCK(obj); 3252 } 3253 vput(vp); 3254 } 3255 mtx_lock(&mntvnode_mtx); 3256 if (TAILQ_NEXT(vp, v_nmntvnodes) != nvp) { 3257 if (--tries > 0) 3258 goto loop; 3259 break; 3260 } 3261 } else 3262 VI_UNLOCK(vp); 3263 } 3264 mtx_unlock(&mntvnode_mtx); 3265} 3266 3267/* 3268 * Create the VM object needed for VMIO and mmap support. This 3269 * is done for all VREG files in the system. Some filesystems might 3270 * afford the additional metadata buffering capability of the 3271 * VMIO code by making the device node be VMIO mode also. 3272 * 3273 * vp must be locked when vfs_object_create is called. 3274 */ 3275int 3276vfs_object_create(vp, td, cred) 3277 struct vnode *vp; 3278 struct thread *td; 3279 struct ucred *cred; 3280{ 3281 GIANT_REQUIRED; 3282 return (VOP_CREATEVOBJECT(vp, cred, td)); 3283} 3284 3285/* 3286 * Mark a vnode as free, putting it up for recycling. 3287 */ 3288void 3289vfree(vp) 3290 struct vnode *vp; 3291{ 3292 int s; 3293 3294 ASSERT_VI_LOCKED(vp, "vfree"); 3295 s = splbio(); 3296 mtx_lock(&vnode_free_list_mtx); 3297 KASSERT((vp->v_iflag & VI_FREE) == 0, ("vnode already free")); 3298 if (vp->v_iflag & VI_AGE) { 3299 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist); 3300 } else { 3301 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist); 3302 } 3303 freevnodes++; 3304 mtx_unlock(&vnode_free_list_mtx); 3305 vp->v_iflag &= ~VI_AGE; 3306 vp->v_iflag |= VI_FREE; 3307 splx(s); 3308} 3309 3310/* 3311 * Opposite of vfree() - mark a vnode as in use. 3312 */ 3313void 3314vbusy(vp) 3315 struct vnode *vp; 3316{ 3317 int s; 3318 3319 s = splbio(); 3320 ASSERT_VI_LOCKED(vp, "vbusy"); 3321 KASSERT((vp->v_iflag & VI_FREE) != 0, ("vnode not free")); 3322 3323 mtx_lock(&vnode_free_list_mtx); 3324 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist); 3325 freevnodes--; 3326 mtx_unlock(&vnode_free_list_mtx); 3327 3328 vp->v_iflag &= ~(VI_FREE|VI_AGE); 3329 splx(s); 3330} 3331 3332/* 3333 * Record a process's interest in events which might happen to 3334 * a vnode. Because poll uses the historic select-style interface 3335 * internally, this routine serves as both the ``check for any 3336 * pending events'' and the ``record my interest in future events'' 3337 * functions. (These are done together, while the lock is held, 3338 * to avoid race conditions.) 3339 */ 3340int 3341vn_pollrecord(vp, td, events) 3342 struct vnode *vp; 3343 struct thread *td; 3344 short events; 3345{ 3346 3347 if (vp->v_pollinfo == NULL) 3348 v_addpollinfo(vp); 3349 mtx_lock(&vp->v_pollinfo->vpi_lock); 3350 if (vp->v_pollinfo->vpi_revents & events) { 3351 /* 3352 * This leaves events we are not interested 3353 * in available for the other process which 3354 * which presumably had requested them 3355 * (otherwise they would never have been 3356 * recorded). 3357 */ 3358 events &= vp->v_pollinfo->vpi_revents; 3359 vp->v_pollinfo->vpi_revents &= ~events; 3360 3361 mtx_unlock(&vp->v_pollinfo->vpi_lock); 3362 return events; 3363 } 3364 vp->v_pollinfo->vpi_events |= events; 3365 selrecord(td, &vp->v_pollinfo->vpi_selinfo); 3366 mtx_unlock(&vp->v_pollinfo->vpi_lock); 3367 return 0; 3368} 3369 3370/* 3371 * Note the occurrence of an event. If the VN_POLLEVENT macro is used, 3372 * it is possible for us to miss an event due to race conditions, but 3373 * that condition is expected to be rare, so for the moment it is the 3374 * preferred interface. 3375 */ 3376void 3377vn_pollevent(vp, events) 3378 struct vnode *vp; 3379 short events; 3380{ 3381 3382 if (vp->v_pollinfo == NULL) 3383 v_addpollinfo(vp); 3384 mtx_lock(&vp->v_pollinfo->vpi_lock); 3385 if (vp->v_pollinfo->vpi_events & events) { 3386 /* 3387 * We clear vpi_events so that we don't 3388 * call selwakeup() twice if two events are 3389 * posted before the polling process(es) is 3390 * awakened. This also ensures that we take at 3391 * most one selwakeup() if the polling process 3392 * is no longer interested. However, it does 3393 * mean that only one event can be noticed at 3394 * a time. (Perhaps we should only clear those 3395 * event bits which we note?) XXX 3396 */ 3397 vp->v_pollinfo->vpi_events = 0; /* &= ~events ??? */ 3398 vp->v_pollinfo->vpi_revents |= events; 3399 selwakeup(&vp->v_pollinfo->vpi_selinfo); 3400 } 3401 mtx_unlock(&vp->v_pollinfo->vpi_lock); 3402} 3403 3404/* 3405 * Wake up anyone polling on vp because it is being revoked. 3406 * This depends on dead_poll() returning POLLHUP for correct 3407 * behavior. 3408 */ 3409void 3410vn_pollgone(vp) 3411 struct vnode *vp; 3412{ 3413 3414 mtx_lock(&vp->v_pollinfo->vpi_lock); 3415 VN_KNOTE(vp, NOTE_REVOKE); 3416 if (vp->v_pollinfo->vpi_events) { 3417 vp->v_pollinfo->vpi_events = 0; 3418 selwakeup(&vp->v_pollinfo->vpi_selinfo); 3419 } 3420 mtx_unlock(&vp->v_pollinfo->vpi_lock); 3421} 3422 3423 3424 3425/* 3426 * Routine to create and manage a filesystem syncer vnode. 3427 */ 3428#define sync_close ((int (*)(struct vop_close_args *))nullop) 3429static int sync_fsync(struct vop_fsync_args *); 3430static int sync_inactive(struct vop_inactive_args *); 3431static int sync_reclaim(struct vop_reclaim_args *); 3432 3433static vop_t **sync_vnodeop_p; 3434static struct vnodeopv_entry_desc sync_vnodeop_entries[] = { 3435 { &vop_default_desc, (vop_t *) vop_eopnotsupp }, 3436 { &vop_close_desc, (vop_t *) sync_close }, /* close */ 3437 { &vop_fsync_desc, (vop_t *) sync_fsync }, /* fsync */ 3438 { &vop_inactive_desc, (vop_t *) sync_inactive }, /* inactive */ 3439 { &vop_reclaim_desc, (vop_t *) sync_reclaim }, /* reclaim */ 3440 { &vop_lock_desc, (vop_t *) vop_stdlock }, /* lock */ 3441 { &vop_unlock_desc, (vop_t *) vop_stdunlock }, /* unlock */ 3442 { &vop_islocked_desc, (vop_t *) vop_stdislocked }, /* islocked */ 3443 { NULL, NULL } 3444}; 3445static struct vnodeopv_desc sync_vnodeop_opv_desc = 3446 { &sync_vnodeop_p, sync_vnodeop_entries }; 3447 3448VNODEOP_SET(sync_vnodeop_opv_desc); 3449 3450/* 3451 * Create a new filesystem syncer vnode for the specified mount point. 3452 */ 3453int 3454vfs_allocate_syncvnode(mp) 3455 struct mount *mp; 3456{ 3457 struct vnode *vp; 3458 static long start, incr, next; 3459 int error; 3460 3461 /* Allocate a new vnode */ 3462 if ((error = getnewvnode("syncer", mp, sync_vnodeop_p, &vp)) != 0) { 3463 mp->mnt_syncer = NULL; 3464 return (error); 3465 } 3466 vp->v_type = VNON; 3467 /* 3468 * Place the vnode onto the syncer worklist. We attempt to 3469 * scatter them about on the list so that they will go off 3470 * at evenly distributed times even if all the filesystems 3471 * are mounted at once. 3472 */ 3473 next += incr; 3474 if (next == 0 || next > syncer_maxdelay) { 3475 start /= 2; 3476 incr /= 2; 3477 if (start == 0) { 3478 start = syncer_maxdelay / 2; 3479 incr = syncer_maxdelay; 3480 } 3481 next = start; 3482 } 3483 VI_LOCK(vp); 3484 vn_syncer_add_to_worklist(vp, syncdelay > 0 ? next % syncdelay : 0); 3485 VI_UNLOCK(vp); 3486 mp->mnt_syncer = vp; 3487 return (0); 3488} 3489 3490/* 3491 * Do a lazy sync of the filesystem. 3492 */ 3493static int 3494sync_fsync(ap) 3495 struct vop_fsync_args /* { 3496 struct vnode *a_vp; 3497 struct ucred *a_cred; 3498 int a_waitfor; 3499 struct thread *a_td; 3500 } */ *ap; 3501{ 3502 struct vnode *syncvp = ap->a_vp; 3503 struct mount *mp = syncvp->v_mount; 3504 struct thread *td = ap->a_td; 3505 int error, asyncflag; 3506 3507 /* 3508 * We only need to do something if this is a lazy evaluation. 3509 */ 3510 if (ap->a_waitfor != MNT_LAZY) 3511 return (0); 3512 3513 /* 3514 * Move ourselves to the back of the sync list. 3515 */ 3516 VI_LOCK(syncvp); 3517 vn_syncer_add_to_worklist(syncvp, syncdelay); 3518 VI_UNLOCK(syncvp); 3519 3520 /* 3521 * Walk the list of vnodes pushing all that are dirty and 3522 * not already on the sync list. 3523 */ 3524 mtx_lock(&mountlist_mtx); 3525 if (vfs_busy(mp, LK_EXCLUSIVE | LK_NOWAIT, &mountlist_mtx, td) != 0) { 3526 mtx_unlock(&mountlist_mtx); 3527 return (0); 3528 } 3529 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) { 3530 vfs_unbusy(mp, td); 3531 return (0); 3532 } 3533 asyncflag = mp->mnt_flag & MNT_ASYNC; 3534 mp->mnt_flag &= ~MNT_ASYNC; 3535 vfs_msync(mp, MNT_NOWAIT); 3536 error = VFS_SYNC(mp, MNT_LAZY, ap->a_cred, td); 3537 if (asyncflag) 3538 mp->mnt_flag |= MNT_ASYNC; 3539 vn_finished_write(mp); 3540 vfs_unbusy(mp, td); 3541 return (error); 3542} 3543 3544/* 3545 * The syncer vnode is no referenced. 3546 */ 3547static int 3548sync_inactive(ap) 3549 struct vop_inactive_args /* { 3550 struct vnode *a_vp; 3551 struct thread *a_td; 3552 } */ *ap; 3553{ 3554 3555 VOP_UNLOCK(ap->a_vp, 0, ap->a_td); 3556 vgone(ap->a_vp); 3557 return (0); 3558} 3559 3560/* 3561 * The syncer vnode is no longer needed and is being decommissioned. 3562 * 3563 * Modifications to the worklist must be protected at splbio(). 3564 */ 3565static int 3566sync_reclaim(ap) 3567 struct vop_reclaim_args /* { 3568 struct vnode *a_vp; 3569 } */ *ap; 3570{ 3571 struct vnode *vp = ap->a_vp; 3572 int s; 3573 3574 s = splbio(); 3575 vp->v_mount->mnt_syncer = NULL; 3576 VI_LOCK(vp); 3577 if (vp->v_iflag & VI_ONWORKLST) { 3578 mtx_lock(&sync_mtx); 3579 LIST_REMOVE(vp, v_synclist); 3580 mtx_unlock(&sync_mtx); 3581 vp->v_iflag &= ~VI_ONWORKLST; 3582 } 3583 VI_UNLOCK(vp); 3584 splx(s); 3585 3586 return (0); 3587} 3588 3589/* 3590 * extract the dev_t from a VCHR 3591 */ 3592dev_t 3593vn_todev(vp) 3594 struct vnode *vp; 3595{ 3596 if (vp->v_type != VCHR) 3597 return (NODEV); 3598 return (vp->v_rdev); 3599} 3600 3601/* 3602 * Check if vnode represents a disk device 3603 */ 3604int 3605vn_isdisk(vp, errp) 3606 struct vnode *vp; 3607 int *errp; 3608{ 3609 struct cdevsw *cdevsw; 3610 3611 if (vp->v_type != VCHR) { 3612 if (errp != NULL) 3613 *errp = ENOTBLK; 3614 return (0); 3615 } 3616 if (vp->v_rdev == NULL) { 3617 if (errp != NULL) 3618 *errp = ENXIO; 3619 return (0); 3620 } 3621 cdevsw = devsw(vp->v_rdev); 3622 if (cdevsw == NULL) { 3623 if (errp != NULL) 3624 *errp = ENXIO; 3625 return (0); 3626 } 3627 if (!(cdevsw->d_flags & D_DISK)) { 3628 if (errp != NULL) 3629 *errp = ENOTBLK; 3630 return (0); 3631 } 3632 if (errp != NULL) 3633 *errp = 0; 3634 return (1); 3635} 3636 3637/* 3638 * Free data allocated by namei(); see namei(9) for details. 3639 */ 3640void 3641NDFREE(ndp, flags) 3642 struct nameidata *ndp; 3643 const u_int flags; 3644{ 3645 if (!(flags & NDF_NO_FREE_PNBUF) && 3646 (ndp->ni_cnd.cn_flags & HASBUF)) { 3647 uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf); 3648 ndp->ni_cnd.cn_flags &= ~HASBUF; 3649 } 3650 if (!(flags & NDF_NO_DVP_UNLOCK) && 3651 (ndp->ni_cnd.cn_flags & LOCKPARENT) && 3652 ndp->ni_dvp != ndp->ni_vp) 3653 VOP_UNLOCK(ndp->ni_dvp, 0, ndp->ni_cnd.cn_thread); 3654 if (!(flags & NDF_NO_DVP_RELE) && 3655 (ndp->ni_cnd.cn_flags & (LOCKPARENT|WANTPARENT))) { 3656 vrele(ndp->ni_dvp); 3657 ndp->ni_dvp = NULL; 3658 } 3659 if (!(flags & NDF_NO_VP_UNLOCK) && 3660 (ndp->ni_cnd.cn_flags & LOCKLEAF) && ndp->ni_vp) 3661 VOP_UNLOCK(ndp->ni_vp, 0, ndp->ni_cnd.cn_thread); 3662 if (!(flags & NDF_NO_VP_RELE) && 3663 ndp->ni_vp) { 3664 vrele(ndp->ni_vp); 3665 ndp->ni_vp = NULL; 3666 } 3667 if (!(flags & NDF_NO_STARTDIR_RELE) && 3668 (ndp->ni_cnd.cn_flags & SAVESTART)) { 3669 vrele(ndp->ni_startdir); 3670 ndp->ni_startdir = NULL; 3671 } 3672} 3673 3674/* 3675 * Common filesystem object access control check routine. Accepts a 3676 * vnode's type, "mode", uid and gid, requested access mode, credentials, 3677 * and optional call-by-reference privused argument allowing vaccess() 3678 * to indicate to the caller whether privilege was used to satisfy the 3679 * request (obsoleted). Returns 0 on success, or an errno on failure. 3680 */ 3681int 3682vaccess(type, file_mode, file_uid, file_gid, acc_mode, cred, privused) 3683 enum vtype type; 3684 mode_t file_mode; 3685 uid_t file_uid; 3686 gid_t file_gid; 3687 mode_t acc_mode; 3688 struct ucred *cred; 3689 int *privused; 3690{ 3691 mode_t dac_granted; 3692#ifdef CAPABILITIES 3693 mode_t cap_granted; 3694#endif 3695 3696 /* 3697 * Look for a normal, non-privileged way to access the file/directory 3698 * as requested. If it exists, go with that. 3699 */ 3700 3701 if (privused != NULL) 3702 *privused = 0; 3703 3704 dac_granted = 0; 3705 3706 /* Check the owner. */ 3707 if (cred->cr_uid == file_uid) { 3708 dac_granted |= VADMIN; 3709 if (file_mode & S_IXUSR) 3710 dac_granted |= VEXEC; 3711 if (file_mode & S_IRUSR) 3712 dac_granted |= VREAD; 3713 if (file_mode & S_IWUSR) 3714 dac_granted |= (VWRITE | VAPPEND); 3715 3716 if ((acc_mode & dac_granted) == acc_mode) 3717 return (0); 3718 3719 goto privcheck; 3720 } 3721 3722 /* Otherwise, check the groups (first match) */ 3723 if (groupmember(file_gid, cred)) { 3724 if (file_mode & S_IXGRP) 3725 dac_granted |= VEXEC; 3726 if (file_mode & S_IRGRP) 3727 dac_granted |= VREAD; 3728 if (file_mode & S_IWGRP) 3729 dac_granted |= (VWRITE | VAPPEND); 3730 3731 if ((acc_mode & dac_granted) == acc_mode) 3732 return (0); 3733 3734 goto privcheck; 3735 } 3736 3737 /* Otherwise, check everyone else. */ 3738 if (file_mode & S_IXOTH) 3739 dac_granted |= VEXEC; 3740 if (file_mode & S_IROTH) 3741 dac_granted |= VREAD; 3742 if (file_mode & S_IWOTH) 3743 dac_granted |= (VWRITE | VAPPEND); 3744 if ((acc_mode & dac_granted) == acc_mode) 3745 return (0); 3746 3747privcheck: 3748 if (!suser_cred(cred, PRISON_ROOT)) { 3749 /* XXX audit: privilege used */ 3750 if (privused != NULL) 3751 *privused = 1; 3752 return (0); 3753 } 3754 3755#ifdef CAPABILITIES 3756 /* 3757 * Build a capability mask to determine if the set of capabilities 3758 * satisfies the requirements when combined with the granted mask 3759 * from above. 3760 * For each capability, if the capability is required, bitwise 3761 * or the request type onto the cap_granted mask. 3762 */ 3763 cap_granted = 0; 3764 3765 if (type == VDIR) { 3766 /* 3767 * For directories, use CAP_DAC_READ_SEARCH to satisfy 3768 * VEXEC requests, instead of CAP_DAC_EXECUTE. 3769 */ 3770 if ((acc_mode & VEXEC) && ((dac_granted & VEXEC) == 0) && 3771 !cap_check(cred, NULL, CAP_DAC_READ_SEARCH, PRISON_ROOT)) 3772 cap_granted |= VEXEC; 3773 } else { 3774 if ((acc_mode & VEXEC) && ((dac_granted & VEXEC) == 0) && 3775 !cap_check(cred, NULL, CAP_DAC_EXECUTE, PRISON_ROOT)) 3776 cap_granted |= VEXEC; 3777 } 3778 3779 if ((acc_mode & VREAD) && ((dac_granted & VREAD) == 0) && 3780 !cap_check(cred, NULL, CAP_DAC_READ_SEARCH, PRISON_ROOT)) 3781 cap_granted |= VREAD; 3782 3783 if ((acc_mode & VWRITE) && ((dac_granted & VWRITE) == 0) && 3784 !cap_check(cred, NULL, CAP_DAC_WRITE, PRISON_ROOT)) 3785 cap_granted |= (VWRITE | VAPPEND); 3786 3787 if ((acc_mode & VADMIN) && ((dac_granted & VADMIN) == 0) && 3788 !cap_check(cred, NULL, CAP_FOWNER, PRISON_ROOT)) 3789 cap_granted |= VADMIN; 3790 3791 if ((acc_mode & (cap_granted | dac_granted)) == acc_mode) { 3792 /* XXX audit: privilege used */ 3793 if (privused != NULL) 3794 *privused = 1; 3795 return (0); 3796 } 3797#endif 3798 3799 return ((acc_mode & VADMIN) ? EPERM : EACCES); 3800} 3801 3802/* 3803 * Credential check based on process requesting service, and per-attribute 3804 * permissions. 3805 */ 3806int 3807extattr_check_cred(struct vnode *vp, int attrnamespace, 3808 struct ucred *cred, struct thread *td, int access) 3809{ 3810 3811 /* 3812 * Kernel-invoked always succeeds. 3813 */ 3814 if (cred == NOCRED) 3815 return (0); 3816 3817 /* 3818 * Do not allow privileged processes in jail to directly 3819 * manipulate system attributes. 3820 * 3821 * XXX What capability should apply here? 3822 * Probably CAP_SYS_SETFFLAG. 3823 */ 3824 switch (attrnamespace) { 3825 case EXTATTR_NAMESPACE_SYSTEM: 3826 /* Potentially should be: return (EPERM); */ 3827 return (suser_cred(cred, 0)); 3828 case EXTATTR_NAMESPACE_USER: 3829 return (VOP_ACCESS(vp, access, cred, td)); 3830 default: 3831 return (EPERM); 3832 } 3833} 3834