File:  [DragonFly] / src / sys / kern / vfs_subr.c
Revision 1.29: download - view: text, annotated - select for diffs
Thu Apr 8 17:56:48 2004 UTC (10 years, 6 months ago) by dillon
Branches: MAIN
CVS tags: HEAD
namecache work stage 4:

(1) Remove vnode->v_dd, vnode->v_ddid, namecache->nc_dvp_data, and
namecache->nc_dvp_id.  These identifiers were being used to detect stale
parent directory linkages in the namecache and were leftovers from the
original FreeBSD-4.x namecache topology.  The new namecache topology
actively discards such linkages and does not require them.

(2) Cleanup kern/vfs_cache.c, abstracting out allocation and parent
link/unlink operations into their own procedures.

(3) Formally allow a disjoint topology.  That is, allow the case where
nc_parent is NULL.  When constructing namecache entries (dvp,vp), require
that that dvp be associated with a namecache record so we can create the
proper parent->child linkage.  Since no naming information is known for
dbp, formally allow unnamed namecache records to be created in order to
create the association.

(4) Properly relink parent namecache entries when ".." is entered into
the cache.  This is what relinks a disjoint namecache topology after it
has been partially purged or when the namecache is instantiated in the
middle of the logical topology (and thus disjoint).

Note that the original plan was to not allow a disjoint topology, but after
much hair pulling I've come to the conclusion that it is impossible to do
this.  So the work now formally allows a disjoint topology but also, unlike
the original FreeBSD code, takes pains to try to keep the topology intact
by only recycling 'leaf' vnodes.  This is accomplished by vref()ing a vnode
when its namecache records have children.

    1: /*
    2:  * Copyright (c) 1989, 1993
    3:  *	The Regents of the University of California.  All rights reserved.
    4:  * (c) UNIX System Laboratories, Inc.
    5:  * All or some portions of this file are derived from material licensed
    6:  * to the University of California by American Telephone and Telegraph
    7:  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
    8:  * the permission of UNIX System Laboratories, Inc.
    9:  *
   10:  * Redistribution and use in source and binary forms, with or without
   11:  * modification, are permitted provided that the following conditions
   12:  * are met:
   13:  * 1. Redistributions of source code must retain the above copyright
   14:  *    notice, this list of conditions and the following disclaimer.
   15:  * 2. Redistributions in binary form must reproduce the above copyright
   16:  *    notice, this list of conditions and the following disclaimer in the
   17:  *    documentation and/or other materials provided with the distribution.
   18:  * 3. All advertising materials mentioning features or use of this software
   19:  *    must display the following acknowledgement:
   20:  *	This product includes software developed by the University of
   21:  *	California, Berkeley and its contributors.
   22:  * 4. Neither the name of the University nor the names of its contributors
   23:  *    may be used to endorse or promote products derived from this software
   24:  *    without specific prior written permission.
   25:  *
   26:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   27:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   28:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   29:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   30:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   31:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   32:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   33:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   34:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   35:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   36:  * SUCH DAMAGE.
   37:  *
   38:  *	@(#)vfs_subr.c	8.31 (Berkeley) 5/26/95
   39:  * $FreeBSD: src/sys/kern/vfs_subr.c,v 1.249.2.30 2003/04/04 20:35:57 tegge Exp $
   40:  * $DragonFly: src/sys/kern/vfs_subr.c,v 1.29 2004/04/08 17:56:48 dillon Exp $
   41:  */
   42: 
   43: /*
   44:  * External virtual filesystem routines
   45:  */
   46: #include "opt_ddb.h"
   47: 
   48: #include <sys/param.h>
   49: #include <sys/systm.h>
   50: #include <sys/buf.h>
   51: #include <sys/conf.h>
   52: #include <sys/dirent.h>
   53: #include <sys/domain.h>
   54: #include <sys/eventhandler.h>
   55: #include <sys/fcntl.h>
   56: #include <sys/kernel.h>
   57: #include <sys/kthread.h>
   58: #include <sys/malloc.h>
   59: #include <sys/mbuf.h>
   60: #include <sys/mount.h>
   61: #include <sys/proc.h>
   62: #include <sys/namei.h>
   63: #include <sys/reboot.h>
   64: #include <sys/socket.h>
   65: #include <sys/stat.h>
   66: #include <sys/sysctl.h>
   67: #include <sys/syslog.h>
   68: #include <sys/vmmeter.h>
   69: #include <sys/vnode.h>
   70: 
   71: #include <machine/limits.h>
   72: 
   73: #include <vm/vm.h>
   74: #include <vm/vm_object.h>
   75: #include <vm/vm_extern.h>
   76: #include <vm/vm_kern.h>
   77: #include <vm/pmap.h>
   78: #include <vm/vm_map.h>
   79: #include <vm/vm_page.h>
   80: #include <vm/vm_pager.h>
   81: #include <vm/vnode_pager.h>
   82: #include <vm/vm_zone.h>
   83: 
   84: #include <sys/buf2.h>
   85: #include <sys/thread2.h>
   86: 
   87: static MALLOC_DEFINE(M_NETADDR, "Export Host", "Export host address structure");
   88: 
   89: static void	insmntque (struct vnode *vp, struct mount *mp);
   90: static void	vclean (struct vnode *vp, lwkt_tokref_t vlock, int flags, struct thread *td);
   91: static unsigned long	numvnodes;
   92: SYSCTL_INT(_debug, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0, "");
   93: 
   94: enum vtype iftovt_tab[16] = {
   95: 	VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
   96: 	VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
   97: };
   98: int vttoif_tab[9] = {
   99: 	0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
  100: 	S_IFSOCK, S_IFIFO, S_IFMT,
  101: };
  102: 
  103: static TAILQ_HEAD(freelst, vnode) vnode_free_list;	/* vnode free list */
  104: 
  105: static u_long wantfreevnodes = 25;
  106: SYSCTL_INT(_debug, OID_AUTO, wantfreevnodes, CTLFLAG_RW, &wantfreevnodes, 0, "");
  107: static u_long freevnodes = 0;
  108: SYSCTL_INT(_debug, OID_AUTO, freevnodes, CTLFLAG_RD, &freevnodes, 0, "");
  109: 
  110: static int reassignbufcalls;
  111: SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW, &reassignbufcalls, 0, "");
  112: static int reassignbufloops;
  113: SYSCTL_INT(_vfs, OID_AUTO, reassignbufloops, CTLFLAG_RW, &reassignbufloops, 0, "");
  114: static int reassignbufsortgood;
  115: SYSCTL_INT(_vfs, OID_AUTO, reassignbufsortgood, CTLFLAG_RW, &reassignbufsortgood, 0, "");
  116: static int reassignbufsortbad;
  117: SYSCTL_INT(_vfs, OID_AUTO, reassignbufsortbad, CTLFLAG_RW, &reassignbufsortbad, 0, "");
  118: static int reassignbufmethod = 1;
  119: SYSCTL_INT(_vfs, OID_AUTO, reassignbufmethod, CTLFLAG_RW, &reassignbufmethod, 0, "");
  120: 
  121: #ifdef ENABLE_VFS_IOOPT
  122: int vfs_ioopt = 0;
  123: SYSCTL_INT(_vfs, OID_AUTO, ioopt, CTLFLAG_RW, &vfs_ioopt, 0, "");
  124: #endif
  125: 
  126: struct mntlist mountlist = TAILQ_HEAD_INITIALIZER(mountlist); /* mounted fs */
  127: struct lwkt_token mountlist_token;
  128: struct lwkt_token mntvnode_token;
  129: int	nfs_mount_type = -1;
  130: static struct lwkt_token mntid_token;
  131: static struct lwkt_token vnode_free_list_token;
  132: static struct lwkt_token spechash_token;
  133: struct nfs_public nfs_pub;	/* publicly exported FS */
  134: static vm_zone_t vnode_zone;
  135: 
  136: /*
  137:  * The workitem queue.
  138:  */
  139: #define SYNCER_MAXDELAY		32
  140: static int syncer_maxdelay = SYNCER_MAXDELAY;	/* maximum delay time */
  141: time_t syncdelay = 30;		/* max time to delay syncing data */
  142: SYSCTL_INT(_kern, OID_AUTO, syncdelay, CTLFLAG_RW, &syncdelay, 0,
  143: 	"VFS data synchronization delay");
  144: time_t filedelay = 30;		/* time to delay syncing files */
  145: SYSCTL_INT(_kern, OID_AUTO, filedelay, CTLFLAG_RW, &filedelay, 0,
  146: 	"File synchronization delay");
  147: time_t dirdelay = 29;		/* time to delay syncing directories */
  148: SYSCTL_INT(_kern, OID_AUTO, dirdelay, CTLFLAG_RW, &dirdelay, 0,
  149: 	"Directory synchronization delay");
  150: time_t metadelay = 28;		/* time to delay syncing metadata */
  151: SYSCTL_INT(_kern, OID_AUTO, metadelay, CTLFLAG_RW, &metadelay, 0,
  152: 	"VFS metadata synchronization delay");
  153: static int rushjob;			/* number of slots to run ASAP */
  154: static int stat_rush_requests;	/* number of times I/O speeded up */
  155: SYSCTL_INT(_debug, OID_AUTO, rush_requests, CTLFLAG_RW, &stat_rush_requests, 0, "");
  156: 
  157: static int syncer_delayno = 0;
  158: static long syncer_mask; 
  159: LIST_HEAD(synclist, vnode);
  160: static struct synclist *syncer_workitem_pending;
  161: 
  162: int desiredvnodes;
  163: SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RW, 
  164:     &desiredvnodes, 0, "Maximum number of vnodes");
  165: static int minvnodes;
  166: SYSCTL_INT(_kern, OID_AUTO, minvnodes, CTLFLAG_RW, 
  167:     &minvnodes, 0, "Minimum number of vnodes");
  168: static int vnlru_nowhere = 0;
  169: SYSCTL_INT(_debug, OID_AUTO, vnlru_nowhere, CTLFLAG_RW, &vnlru_nowhere, 0,
  170:     "Number of times the vnlru process ran without success");
  171: 
  172: static void	vfs_free_addrlist (struct netexport *nep);
  173: static int	vfs_free_netcred (struct radix_node *rn, void *w);
  174: static int	vfs_hang_addrlist (struct mount *mp, struct netexport *nep,
  175: 				       struct export_args *argp);
  176: 
  177: #define VSHOULDFREE(vp) \
  178: 	(!((vp)->v_flag & (VFREE|VDOOMED)) && \
  179: 	 !(vp)->v_holdcnt && !(vp)->v_usecount && \
  180: 	 (!(vp)->v_object || \
  181: 	  !((vp)->v_object->ref_count || (vp)->v_object->resident_page_count)))
  182:  
  183: #define VMIGHTFREE(vp) \
  184: 	(((vp)->v_flag & (VFREE|VDOOMED|VXLOCK)) == 0 &&   \
  185: 	 cache_leaf_test(vp) == 0 && (vp)->v_usecount == 0)
  186:  
  187: #define VSHOULDBUSY(vp) \
  188: 	(((vp)->v_flag & VFREE) && \
  189: 	 ((vp)->v_holdcnt || (vp)->v_usecount))
  190: 
  191: static void vbusy(struct vnode *vp);
  192: static void vfree(struct vnode *vp);
  193: static void vmaybefree(struct vnode *vp);
  194: 
  195: /*
  196:  * NOTE: the vnode interlock must be held on call.
  197:  */
  198: static __inline void
  199: vmaybefree(struct vnode *vp)
  200: {
  201: 	if (VSHOULDFREE(vp))
  202: 		vfree(vp);
  203: }
  204:  
  205: /*
  206:  * Initialize the vnode management data structures.
  207:  */
  208: void
  209: vntblinit()
  210: {
  211: 
  212: 	/*
  213: 	 * Desired vnodes is a result of the physical page count
  214: 	 * and the size of kernel's heap.  It scales in proportion
  215: 	 * to the amount of available physical memory.  This can
  216: 	 * cause trouble on 64-bit and large memory platforms.
  217: 	 */
  218: 	/* desiredvnodes = maxproc + vmstats.v_page_count / 4; */
  219: 	desiredvnodes =
  220: 		min(maxproc + vmstats.v_page_count /4,
  221: 		    2 * (VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS) /
  222: 		    (5 * (sizeof(struct vm_object) + sizeof(struct vnode))));
  223: 
  224: 	minvnodes = desiredvnodes / 4;
  225: 	lwkt_token_init(&mountlist_token);
  226: 	lwkt_token_init(&mntvnode_token);
  227: 	lwkt_token_init(&mntid_token);
  228: 	lwkt_token_init(&spechash_token);
  229: 	TAILQ_INIT(&vnode_free_list);
  230: 	lwkt_token_init(&vnode_free_list_token);
  231: 	vnode_zone = zinit("VNODE", sizeof (struct vnode), 0, 0, 5);
  232: 	/*
  233: 	 * Initialize the filesystem syncer.
  234: 	 */     
  235: 	syncer_workitem_pending = hashinit(syncer_maxdelay, M_VNODE, 
  236: 		&syncer_mask);
  237: 	syncer_maxdelay = syncer_mask + 1;
  238: }
  239: 
  240: /*
  241:  * Mark a mount point as busy. Used to synchronize access and to delay
  242:  * unmounting. Interlock is not released on failure.
  243:  */
  244: int
  245: vfs_busy(struct mount *mp, int flags, lwkt_tokref_t interlkp, struct thread *td)
  246: {
  247: 	int lkflags;
  248: 
  249: 	if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
  250: 		if (flags & LK_NOWAIT)
  251: 			return (ENOENT);
  252: 		mp->mnt_kern_flag |= MNTK_MWAIT;
  253: 		/*
  254: 		 * Since all busy locks are shared except the exclusive
  255: 		 * lock granted when unmounting, the only place that a
  256: 		 * wakeup needs to be done is at the release of the
  257: 		 * exclusive lock at the end of dounmount.
  258: 		 *
  259: 		 * note: interlkp is a serializer and thus can be safely
  260: 		 * held through any sleep
  261: 		 */
  262: 		tsleep((caddr_t)mp, 0, "vfs_busy", 0);
  263: 		return (ENOENT);
  264: 	}
  265: 	lkflags = LK_SHARED | LK_NOPAUSE;
  266: 	if (interlkp)
  267: 		lkflags |= LK_INTERLOCK;
  268: 	if (lockmgr(&mp->mnt_lock, lkflags, interlkp, td))
  269: 		panic("vfs_busy: unexpected lock failure");
  270: 	return (0);
  271: }
  272: 
  273: /*
  274:  * Free a busy filesystem.
  275:  */
  276: void
  277: vfs_unbusy(struct mount *mp, struct thread *td)
  278: {
  279: 	lockmgr(&mp->mnt_lock, LK_RELEASE, NULL, td);
  280: }
  281: 
  282: /*
  283:  * Lookup a filesystem type, and if found allocate and initialize
  284:  * a mount structure for it.
  285:  *
  286:  * Devname is usually updated by mount(8) after booting.
  287:  */
  288: int
  289: vfs_rootmountalloc(char *fstypename, char *devname, struct mount **mpp)
  290: {
  291: 	struct thread *td = curthread;	/* XXX */
  292: 	struct vfsconf *vfsp;
  293: 	struct mount *mp;
  294: 
  295: 	if (fstypename == NULL)
  296: 		return (ENODEV);
  297: 	for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
  298: 		if (!strcmp(vfsp->vfc_name, fstypename))
  299: 			break;
  300: 	if (vfsp == NULL)
  301: 		return (ENODEV);
  302: 	mp = malloc((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK);
  303: 	bzero((char *)mp, (u_long)sizeof(struct mount));
  304: 	lockinit(&mp->mnt_lock, 0, "vfslock", VLKTIMEOUT, LK_NOPAUSE);
  305: 	vfs_busy(mp, LK_NOWAIT, NULL, td);
  306: 	TAILQ_INIT(&mp->mnt_nvnodelist);
  307: 	TAILQ_INIT(&mp->mnt_reservedvnlist);
  308: 	mp->mnt_nvnodelistsize = 0;
  309: 	mp->mnt_vfc = vfsp;
  310: 	mp->mnt_op = vfsp->vfc_vfsops;
  311: 	mp->mnt_flag = MNT_RDONLY;
  312: 	mp->mnt_vnodecovered = NULLVP;
  313: 	vfsp->vfc_refcount++;
  314: 	mp->mnt_iosize_max = DFLTPHYS;
  315: 	mp->mnt_stat.f_type = vfsp->vfc_typenum;
  316: 	mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
  317: 	strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
  318: 	mp->mnt_stat.f_mntonname[0] = '/';
  319: 	mp->mnt_stat.f_mntonname[1] = 0;
  320: 	(void) copystr(devname, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, 0);
  321: 	*mpp = mp;
  322: 	return (0);
  323: }
  324: 
  325: /*
  326:  * Find an appropriate filesystem to use for the root. If a filesystem
  327:  * has not been preselected, walk through the list of known filesystems
  328:  * trying those that have mountroot routines, and try them until one
  329:  * works or we have tried them all.
  330:  */
  331: #ifdef notdef	/* XXX JH */
  332: int
  333: lite2_vfs_mountroot()
  334: {
  335: 	struct vfsconf *vfsp;
  336: 	extern int (*lite2_mountroot) (void);
  337: 	int error;
  338: 
  339: 	if (lite2_mountroot != NULL)
  340: 		return ((*lite2_mountroot)());
  341: 	for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
  342: 		if (vfsp->vfc_mountroot == NULL)
  343: 			continue;
  344: 		if ((error = (*vfsp->vfc_mountroot)()) == 0)
  345: 			return (0);
  346: 		printf("%s_mountroot failed: %d\n", vfsp->vfc_name, error);
  347: 	}
  348: 	return (ENODEV);
  349: }
  350: #endif
  351: 
  352: /*
  353:  * Lookup a mount point by filesystem identifier.
  354:  */
  355: struct mount *
  356: vfs_getvfs(fsid)
  357: 	fsid_t *fsid;
  358: {
  359: 	struct mount *mp;
  360: 	lwkt_tokref ilock;
  361: 
  362: 	lwkt_gettoken(&ilock, &mountlist_token);
  363: 	TAILQ_FOREACH(mp, &mountlist, mnt_list) {
  364: 		if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
  365: 		    mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
  366: 			break;
  367: 	    }
  368: 	}
  369: 	lwkt_reltoken(&ilock);
  370: 	return (mp);
  371: }
  372: 
  373: /*
  374:  * Get a new unique fsid.  Try to make its val[0] unique, since this value
  375:  * will be used to create fake device numbers for stat().  Also try (but
  376:  * not so hard) make its val[0] unique mod 2^16, since some emulators only
  377:  * support 16-bit device numbers.  We end up with unique val[0]'s for the
  378:  * first 2^16 calls and unique val[0]'s mod 2^16 for the first 2^8 calls.
  379:  *
  380:  * Keep in mind that several mounts may be running in parallel.  Starting
  381:  * the search one past where the previous search terminated is both a
  382:  * micro-optimization and a defense against returning the same fsid to
  383:  * different mounts.
  384:  */
  385: void
  386: vfs_getnewfsid(mp)
  387: 	struct mount *mp;
  388: {
  389: 	static u_int16_t mntid_base;
  390: 	lwkt_tokref ilock;
  391: 	fsid_t tfsid;
  392: 	int mtype;
  393: 
  394: 	lwkt_gettoken(&ilock, &mntid_token);
  395: 	mtype = mp->mnt_vfc->vfc_typenum;
  396: 	tfsid.val[1] = mtype;
  397: 	mtype = (mtype & 0xFF) << 24;
  398: 	for (;;) {
  399: 		tfsid.val[0] = makeudev(255,
  400: 		    mtype | ((mntid_base & 0xFF00) << 8) | (mntid_base & 0xFF));
  401: 		mntid_base++;
  402: 		if (vfs_getvfs(&tfsid) == NULL)
  403: 			break;
  404: 	}
  405: 	mp->mnt_stat.f_fsid.val[0] = tfsid.val[0];
  406: 	mp->mnt_stat.f_fsid.val[1] = tfsid.val[1];
  407: 	lwkt_reltoken(&ilock);
  408: }
  409: 
  410: /*
  411:  * Knob to control the precision of file timestamps:
  412:  *
  413:  *   0 = seconds only; nanoseconds zeroed.
  414:  *   1 = seconds and nanoseconds, accurate within 1/HZ.
  415:  *   2 = seconds and nanoseconds, truncated to microseconds.
  416:  * >=3 = seconds and nanoseconds, maximum precision.
  417:  */
  418: enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC };
  419: 
  420: static int timestamp_precision = TSP_SEC;
  421: SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW,
  422:     &timestamp_precision, 0, "");
  423: 
  424: /*
  425:  * Get a current timestamp.
  426:  */
  427: void
  428: vfs_timestamp(tsp)
  429: 	struct timespec *tsp;
  430: {
  431: 	struct timeval tv;
  432: 
  433: 	switch (timestamp_precision) {
  434: 	case TSP_SEC:
  435: 		tsp->tv_sec = time_second;
  436: 		tsp->tv_nsec = 0;
  437: 		break;
  438: 	case TSP_HZ:
  439: 		getnanotime(tsp);
  440: 		break;
  441: 	case TSP_USEC:
  442: 		microtime(&tv);
  443: 		TIMEVAL_TO_TIMESPEC(&tv, tsp);
  444: 		break;
  445: 	case TSP_NSEC:
  446: 	default:
  447: 		nanotime(tsp);
  448: 		break;
  449: 	}
  450: }
  451: 
  452: /*
  453:  * Set vnode attributes to VNOVAL
  454:  */
  455: void
  456: vattr_null(vap)
  457: 	struct vattr *vap;
  458: {
  459: 
  460: 	vap->va_type = VNON;
  461: 	vap->va_size = VNOVAL;
  462: 	vap->va_bytes = VNOVAL;
  463: 	vap->va_mode = VNOVAL;
  464: 	vap->va_nlink = VNOVAL;
  465: 	vap->va_uid = VNOVAL;
  466: 	vap->va_gid = VNOVAL;
  467: 	vap->va_fsid = VNOVAL;
  468: 	vap->va_fileid = VNOVAL;
  469: 	vap->va_blocksize = VNOVAL;
  470: 	vap->va_rdev = VNOVAL;
  471: 	vap->va_atime.tv_sec = VNOVAL;
  472: 	vap->va_atime.tv_nsec = VNOVAL;
  473: 	vap->va_mtime.tv_sec = VNOVAL;
  474: 	vap->va_mtime.tv_nsec = VNOVAL;
  475: 	vap->va_ctime.tv_sec = VNOVAL;
  476: 	vap->va_ctime.tv_nsec = VNOVAL;
  477: 	vap->va_flags = VNOVAL;
  478: 	vap->va_gen = VNOVAL;
  479: 	vap->va_vaflags = 0;
  480: }
  481: 
  482: /*
  483:  * This routine is called when we have too many vnodes.  It attempts
  484:  * to free <count> vnodes and will potentially free vnodes that still
  485:  * have VM backing store (VM backing store is typically the cause
  486:  * of a vnode blowout so we want to do this).  Therefore, this operation
  487:  * is not considered cheap.
  488:  *
  489:  * A number of conditions may prevent a vnode from being reclaimed.
  490:  * the buffer cache may have references on the vnode, a directory
  491:  * vnode may still have references due to the namei cache representing
  492:  * underlying files, or the vnode may be in active use.   It is not
  493:  * desireable to reuse such vnodes.  These conditions may cause the
  494:  * number of vnodes to reach some minimum value regardless of what
  495:  * you set kern.maxvnodes to.  Do not set kern.maxvnodes too low.
  496:  */
  497: static int
  498: vlrureclaim(struct mount *mp)
  499: {
  500: 	struct vnode *vp;
  501: 	lwkt_tokref ilock;
  502: 	lwkt_tokref vlock;
  503: 	int done;
  504: 	int trigger;
  505: 	int usevnodes;
  506: 	int count;
  507: 
  508: 	/*
  509: 	 * Calculate the trigger point, don't allow user
  510: 	 * screwups to blow us up.   This prevents us from
  511: 	 * recycling vnodes with lots of resident pages.  We
  512: 	 * aren't trying to free memory, we are trying to
  513: 	 * free vnodes.
  514: 	 */
  515: 	usevnodes = desiredvnodes;
  516: 	if (usevnodes <= 0)
  517: 		usevnodes = 1;
  518: 	trigger = vmstats.v_page_count * 2 / usevnodes;
  519: 
  520: 	done = 0;
  521: 	lwkt_gettoken(&ilock, &mntvnode_token);
  522: 	count = mp->mnt_nvnodelistsize / 10 + 1;
  523: 	while (count && (vp = TAILQ_FIRST(&mp->mnt_nvnodelist)) != NULL) {
  524: 		/*
  525: 		 * __VNODESCAN__
  526: 		 *
  527: 		 * The VP will stick around while we hold mntvnode_token,
  528: 		 * at least until we block, so we can safely do an initial
  529: 		 * check.  But we have to check again after obtaining
  530: 		 * the vnode interlock.  vp->v_interlock points to stable
  531: 		 * storage so it's ok if the vp gets ripped out from
  532: 		 * under us while we are blocked.
  533: 		 */
  534: 		if (vp->v_type == VNON ||
  535: 		    vp->v_type == VBAD ||
  536: 		    !VMIGHTFREE(vp) ||		/* critical path opt */
  537: 		    (vp->v_object &&
  538: 		     vp->v_object->resident_page_count >= trigger)
  539: 		) {
  540: 			TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
  541: 			TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist,vp, v_nmntvnodes);
  542: 			--count;
  543: 			continue;
  544: 		}
  545: 
  546: 		/*
  547: 		 * Get the interlock, delay moving the node to the tail so
  548: 		 * we don't race against new additions to the mountlist.
  549: 		 */
  550: 		lwkt_gettoken(&vlock, vp->v_interlock);
  551: 		if (TAILQ_FIRST(&mp->mnt_nvnodelist) != vp) {
  552: 			lwkt_reltoken(&vlock);
  553: 			continue;
  554: 		}
  555: 		TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
  556: 		TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist,vp, v_nmntvnodes);
  557: 
  558: 		/*
  559: 		 * Must check again
  560: 		 */
  561: 		if (vp->v_type == VNON ||
  562: 		    vp->v_type == VBAD ||
  563: 		    !VMIGHTFREE(vp) ||		/* critical path opt */
  564: 		    (vp->v_object &&
  565: 		     vp->v_object->resident_page_count >= trigger)
  566: 		) {
  567: 			lwkt_reltoken(&vlock);
  568: 			--count;
  569: 			continue;
  570: 		}
  571: 		vgonel(vp, &vlock, curthread);
  572: 		++done;
  573: 		--count;
  574: 	}
  575: 	lwkt_reltoken(&ilock);
  576: 	return done;
  577: }
  578: 
  579: /*
  580:  * Attempt to recycle vnodes in a context that is always safe to block.
  581:  * Calling vlrurecycle() from the bowels of file system code has some
  582:  * interesting deadlock problems.
  583:  */
  584: static struct thread *vnlruthread;
  585: static int vnlruproc_sig;
  586: 
  587: static void 
  588: vnlru_proc(void)
  589: {
  590: 	struct mount *mp, *nmp;
  591: 	lwkt_tokref ilock;
  592: 	int s;
  593: 	int done;
  594: 	struct thread *td = curthread;
  595: 
  596: 	EVENTHANDLER_REGISTER(shutdown_pre_sync, shutdown_kproc, td,
  597: 	    SHUTDOWN_PRI_FIRST);   
  598: 
  599: 	s = splbio();
  600: 	for (;;) {
  601: 		kproc_suspend_loop();
  602: 		if (numvnodes - freevnodes <= desiredvnodes * 9 / 10) {
  603: 			vnlruproc_sig = 0;
  604: 			wakeup(&vnlruproc_sig);
  605: 			tsleep(td, 0, "vlruwt", hz);
  606: 			continue;
  607: 		}
  608: 		done = 0;
  609: 		lwkt_gettoken(&ilock, &mountlist_token);
  610: 		for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
  611: 			if (vfs_busy(mp, LK_NOWAIT, &ilock, td)) {
  612: 				nmp = TAILQ_NEXT(mp, mnt_list);
  613: 				continue;
  614: 			}
  615: 			done += vlrureclaim(mp);
  616: 			lwkt_gettokref(&ilock);
  617: 			nmp = TAILQ_NEXT(mp, mnt_list);
  618: 			vfs_unbusy(mp, td);
  619: 		}
  620: 		lwkt_reltoken(&ilock);
  621: 		if (done == 0) {
  622: 			vnlru_nowhere++;
  623: 			tsleep(td, 0, "vlrup", hz * 3);
  624: 		}
  625: 	}
  626: 	splx(s);
  627: }
  628: 
  629: static struct kproc_desc vnlru_kp = {
  630: 	"vnlru",
  631: 	vnlru_proc,
  632: 	&vnlruthread
  633: };
  634: SYSINIT(vnlru, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &vnlru_kp)
  635: 
  636: /*
  637:  * Routines having to do with the management of the vnode table.
  638:  */
  639: extern vop_t **dead_vnodeop_p;
  640: 
  641: /*
  642:  * Return the next vnode from the free list.
  643:  */
  644: int
  645: getnewvnode(tag, mp, vops, vpp)
  646: 	enum vtagtype tag;
  647: 	struct mount *mp;
  648: 	vop_t **vops;
  649: 	struct vnode **vpp;
  650: {
  651: 	int s;
  652: 	struct thread *td = curthread;	/* XXX */
  653: 	struct vnode *vp = NULL;
  654: 	struct vnode *xvp;
  655: 	vm_object_t object;
  656: 	lwkt_tokref ilock;
  657: 	lwkt_tokref vlock;
  658: 
  659: 	s = splbio();
  660: 
  661: 	/*
  662: 	 * Try to reuse vnodes if we hit the max.  This situation only
  663: 	 * occurs in certain large-memory (2G+) situations.  We cannot
  664: 	 * attempt to directly reclaim vnodes due to nasty recursion
  665: 	 * problems.
  666: 	 */
  667: 	while (numvnodes - freevnodes > desiredvnodes) {
  668: 		if (vnlruproc_sig == 0) {
  669: 			vnlruproc_sig = 1;	/* avoid unnecessary wakeups */
  670: 			wakeup(vnlruthread);
  671: 		}
  672: 		tsleep(&vnlruproc_sig, 0, "vlruwk", hz);
  673: 	}
  674: 
  675: 
  676: 	/*
  677: 	 * Attempt to reuse a vnode already on the free list, allocating
  678: 	 * a new vnode if we can't find one or if we have not reached a
  679: 	 * good minimum for good LRU performance.
  680: 	 */
  681: 	lwkt_gettoken(&ilock, &vnode_free_list_token);
  682: 	if (freevnodes >= wantfreevnodes && numvnodes >= minvnodes) {
  683: 		int count;
  684: 
  685: 		for (count = 0; count < freevnodes; count++) {
  686: 			/*
  687: 			 * __VNODESCAN__
  688: 			 *
  689: 			 * Pull the next vnode off the free list and do some
  690: 			 * sanity checks.  Note that regardless of how we
  691: 			 * block, if freevnodes is non-zero there had better
  692: 			 * be something on the list.
  693: 			 */
  694: 			vp = TAILQ_FIRST(&vnode_free_list);
  695: 			if (vp == NULL)
  696: 				panic("getnewvnode: free vnode isn't");
  697: 
  698: 			/*
  699: 			 * Move the vnode to the end of the list so other
  700: 			 * processes do not double-block trying to recycle
  701: 			 * the same vnode (as an optimization), then get
  702: 			 * the interlock.
  703: 			 */
  704: 			TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
  705: 			TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
  706: 
  707: 			/*
  708: 			 * Skip vnodes that are in the process of being
  709: 			 * held or referenced.  Since the act of adding or
  710: 			 * removing a vnode on the freelist requires a token
  711: 			 * and may block, the ref count may be adjusted
  712: 			 * prior to its addition or removal.
  713: 			 */
  714: 			if (VSHOULDBUSY(vp)) {
  715: 				vp = NULL;
  716: 				continue;
  717: 			}
  718: 
  719: 
  720: 			/*
  721: 			 * Obtain the vnode interlock and check that the
  722: 			 * vnode is still on the free list.
  723: 			 *
  724: 			 * This normally devolves into a degenerate case so
  725: 			 * it is optimal.   Loop up if it isn't.  Note that
  726: 			 * the vnode could be in the middle of being moved
  727: 			 * off the free list (the VSHOULDBUSY() check) and
  728: 			 * must be skipped if so.
  729: 			 */
  730: 			lwkt_gettoken(&vlock, vp->v_interlock);
  731: 			TAILQ_FOREACH_REVERSE(xvp, &vnode_free_list, 
  732: 			    freelst, v_freelist) {
  733: 				if (vp == xvp)
  734: 					break;
  735: 			}
  736: 			if (vp != xvp || VSHOULDBUSY(vp)) {
  737: 				vp = NULL;
  738: 				continue;
  739: 			}
  740: 
  741: 			/*
  742: 			 * We now safely own the vnode.  If the vnode has
  743: 			 * an object do not recycle it if its VM object
  744: 			 * has resident pages or references.
  745: 			 */
  746: 			if ((VOP_GETVOBJECT(vp, &object) == 0 &&
  747: 			    (object->resident_page_count || object->ref_count))
  748: 			) {
  749: 				lwkt_reltoken(&vlock);
  750: 				vp = NULL;
  751: 				continue;
  752: 			}
  753: 
  754: 			/*
  755: 			 * We can almost reuse this vnode.  But we don't want
  756: 			 * to recycle it if the vnode has children in the
  757: 			 * namecache because that breaks the namecache's
  758: 			 * path element chain.  (YYY use nc_refs for the
  759: 			 * check?)
  760: 			 */
  761: 			KKASSERT(vp->v_flag & VFREE);
  762: 			TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
  763: 
  764: 			if (TAILQ_FIRST(&vp->v_namecache) == NULL ||
  765: 			    cache_leaf_test(vp) >= 0) {
  766: 				/* ok, we can reuse this vnode */
  767: 				break;
  768: 			}
  769: 			lwkt_reltoken(&vlock);
  770: 			TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
  771: 			vp = NULL;
  772: 		}
  773: 	}
  774: 
  775: 	/*
  776: 	 * If vp is non-NULL we hold it's interlock.
  777: 	 */
  778: 	if (vp) {
  779: 		vp->v_flag |= VDOOMED;
  780: 		vp->v_flag &= ~VFREE;
  781: 		freevnodes--;
  782: 		lwkt_reltoken(&ilock);
  783: 		cache_purge(vp);	/* YYY may block */
  784: 		vp->v_lease = NULL;
  785: 		if (vp->v_type != VBAD) {
  786: 			vgonel(vp, &vlock, td);
  787: 		} else {
  788: 			lwkt_reltoken(&vlock);
  789: 		}
  790: 
  791: #ifdef INVARIANTS
  792: 		{
  793: 			int s;
  794: 
  795: 			if (vp->v_data)
  796: 				panic("cleaned vnode isn't");
  797: 			s = splbio();
  798: 			if (vp->v_numoutput)
  799: 				panic("Clean vnode has pending I/O's");
  800: 			splx(s);
  801: 		}
  802: #endif
  803: 		vp->v_flag = 0;
  804: 		vp->v_lastw = 0;
  805: 		vp->v_lasta = 0;
  806: 		vp->v_cstart = 0;
  807: 		vp->v_clen = 0;
  808: 		vp->v_socket = 0;
  809: 		vp->v_writecount = 0;	/* XXX */
  810: 	} else {
  811: 		lwkt_reltoken(&ilock);
  812: 		vp = zalloc(vnode_zone);
  813: 		bzero(vp, sizeof(*vp));
  814: 		vp->v_interlock = lwkt_token_pool_get(vp);
  815: 		lwkt_token_init(&vp->v_pollinfo.vpi_token);
  816: 		cache_purge(vp);
  817: 		TAILQ_INIT(&vp->v_namecache);
  818: 		numvnodes++;
  819: 	}
  820: 
  821: 	TAILQ_INIT(&vp->v_cleanblkhd);
  822: 	TAILQ_INIT(&vp->v_dirtyblkhd);
  823: 	vp->v_type = VNON;
  824: 	vp->v_tag = tag;
  825: 	vp->v_op = vops;
  826: 	insmntque(vp, mp);
  827: 	*vpp = vp;
  828: 	vp->v_usecount = 1;
  829: 	vp->v_data = 0;
  830: 	splx(s);
  831: 
  832: 	vfs_object_create(vp, td);
  833: 	return (0);
  834: }
  835: 
  836: /*
  837:  * Move a vnode from one mount queue to another.
  838:  */
  839: static void
  840: insmntque(vp, mp)
  841: 	struct vnode *vp;
  842: 	struct mount *mp;
  843: {
  844: 	lwkt_tokref ilock;
  845: 
  846: 	lwkt_gettoken(&ilock, &mntvnode_token);
  847: 	/*
  848: 	 * Delete from old mount point vnode list, if on one.
  849: 	 */
  850: 	if (vp->v_mount != NULL) {
  851: 		KASSERT(vp->v_mount->mnt_nvnodelistsize > 0,
  852: 			("bad mount point vnode list size"));
  853: 		TAILQ_REMOVE(&vp->v_mount->mnt_nvnodelist, vp, v_nmntvnodes);
  854: 		vp->v_mount->mnt_nvnodelistsize--;
  855: 	}
  856: 	/*
  857: 	 * Insert into list of vnodes for the new mount point, if available.
  858: 	 */
  859: 	if ((vp->v_mount = mp) == NULL) {
  860: 		lwkt_reltoken(&ilock);
  861: 		return;
  862: 	}
  863: 	TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
  864: 	mp->mnt_nvnodelistsize++;
  865: 	lwkt_reltoken(&ilock);
  866: }
  867: 
  868: /*
  869:  * Update outstanding I/O count and do wakeup if requested.
  870:  */
  871: void
  872: vwakeup(bp)
  873: 	struct buf *bp;
  874: {
  875: 	struct vnode *vp;
  876: 
  877: 	bp->b_flags &= ~B_WRITEINPROG;
  878: 	if ((vp = bp->b_vp)) {
  879: 		vp->v_numoutput--;
  880: 		if (vp->v_numoutput < 0)
  881: 			panic("vwakeup: neg numoutput");
  882: 		if ((vp->v_numoutput == 0) && (vp->v_flag & VBWAIT)) {
  883: 			vp->v_flag &= ~VBWAIT;
  884: 			wakeup((caddr_t) &vp->v_numoutput);
  885: 		}
  886: 	}
  887: }
  888: 
  889: /*
  890:  * Flush out and invalidate all buffers associated with a vnode.
  891:  * Called with the underlying object locked.
  892:  */
  893: int
  894: vinvalbuf(struct vnode *vp, int flags, struct thread *td,
  895: 	int slpflag, int slptimeo)
  896: {
  897: 	struct buf *bp;
  898: 	struct buf *nbp, *blist;
  899: 	int s, error;
  900: 	vm_object_t object;
  901: 	lwkt_tokref vlock;
  902: 
  903: 	if (flags & V_SAVE) {
  904: 		s = splbio();
  905: 		while (vp->v_numoutput) {
  906: 			vp->v_flag |= VBWAIT;
  907: 			error = tsleep((caddr_t)&vp->v_numoutput,
  908: 			    slpflag, "vinvlbuf", slptimeo);
  909: 			if (error) {
  910: 				splx(s);
  911: 				return (error);
  912: 			}
  913: 		}
  914: 		if (!TAILQ_EMPTY(&vp->v_dirtyblkhd)) {
  915: 			splx(s);
  916: 			if ((error = VOP_FSYNC(vp, MNT_WAIT, td)) != 0)
  917: 				return (error);
  918: 			s = splbio();
  919: 			if (vp->v_numoutput > 0 ||
  920: 			    !TAILQ_EMPTY(&vp->v_dirtyblkhd))
  921: 				panic("vinvalbuf: dirty bufs");
  922: 		}
  923: 		splx(s);
  924:   	}
  925: 	s = splbio();
  926: 	for (;;) {
  927: 		blist = TAILQ_FIRST(&vp->v_cleanblkhd);
  928: 		if (!blist)
  929: 			blist = TAILQ_FIRST(&vp->v_dirtyblkhd);
  930: 		if (!blist)
  931: 			break;
  932: 
  933: 		for (bp = blist; bp; bp = nbp) {
  934: 			nbp = TAILQ_NEXT(bp, b_vnbufs);
  935: 			if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
  936: 				error = BUF_TIMELOCK(bp,
  937: 				    LK_EXCLUSIVE | LK_SLEEPFAIL,
  938: 				    "vinvalbuf", slpflag, slptimeo);
  939: 				if (error == ENOLCK)
  940: 					break;
  941: 				splx(s);
  942: 				return (error);
  943: 			}
  944: 			/*
  945: 			 * XXX Since there are no node locks for NFS, I
  946: 			 * believe there is a slight chance that a delayed
  947: 			 * write will occur while sleeping just above, so
  948: 			 * check for it.  Note that vfs_bio_awrite expects
  949: 			 * buffers to reside on a queue, while VOP_BWRITE and
  950: 			 * brelse do not.
  951: 			 */
  952: 			if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
  953: 				(flags & V_SAVE)) {
  954: 
  955: 				if (bp->b_vp == vp) {
  956: 					if (bp->b_flags & B_CLUSTEROK) {
  957: 						BUF_UNLOCK(bp);
  958: 						vfs_bio_awrite(bp);
  959: 					} else {
  960: 						bremfree(bp);
  961: 						bp->b_flags |= B_ASYNC;
  962: 						VOP_BWRITE(bp->b_vp, bp);
  963: 					}
  964: 				} else {
  965: 					bremfree(bp);
  966: 					(void) VOP_BWRITE(bp->b_vp, bp);
  967: 				}
  968: 				break;
  969: 			}
  970: 			bremfree(bp);
  971: 			bp->b_flags |= (B_INVAL | B_NOCACHE | B_RELBUF);
  972: 			bp->b_flags &= ~B_ASYNC;
  973: 			brelse(bp);
  974: 		}
  975: 	}
  976: 
  977: 	/*
  978: 	 * Wait for I/O to complete.  XXX needs cleaning up.  The vnode can
  979: 	 * have write I/O in-progress but if there is a VM object then the
  980: 	 * VM object can also have read-I/O in-progress.
  981: 	 */
  982: 	do {
  983: 		while (vp->v_numoutput > 0) {
  984: 			vp->v_flag |= VBWAIT;
  985: 			tsleep(&vp->v_numoutput, 0, "vnvlbv", 0);
  986: 		}
  987: 		if (VOP_GETVOBJECT(vp, &object) == 0) {
  988: 			while (object->paging_in_progress)
  989: 				vm_object_pip_sleep(object, "vnvlbx");
  990: 		}
  991: 	} while (vp->v_numoutput > 0);
  992: 
  993: 	splx(s);
  994: 
  995: 	/*
  996: 	 * Destroy the copy in the VM cache, too.
  997: 	 */
  998: 	lwkt_gettoken(&vlock, vp->v_interlock);
  999: 	if (VOP_GETVOBJECT(vp, &object) == 0) {
 1000: 		vm_object_page_remove(object, 0, 0,
 1001: 			(flags & V_SAVE) ? TRUE : FALSE);
 1002: 	}
 1003: 	lwkt_reltoken(&vlock);
 1004: 
 1005: 	if (!TAILQ_EMPTY(&vp->v_dirtyblkhd) || !TAILQ_EMPTY(&vp->v_cleanblkhd))
 1006: 		panic("vinvalbuf: flush failed");
 1007: 	return (0);
 1008: }
 1009: 
 1010: /*
 1011:  * Truncate a file's buffer and pages to a specified length.  This
 1012:  * is in lieu of the old vinvalbuf mechanism, which performed unneeded
 1013:  * sync activity.
 1014:  */
 1015: int
 1016: vtruncbuf(struct vnode *vp, struct thread *td, off_t length, int blksize)
 1017: {
 1018: 	struct buf *bp;
 1019: 	struct buf *nbp;
 1020: 	int s, anyfreed;
 1021: 	int trunclbn;
 1022: 
 1023: 	/*
 1024: 	 * Round up to the *next* lbn.
 1025: 	 */
 1026: 	trunclbn = (length + blksize - 1) / blksize;
 1027: 
 1028: 	s = splbio();
 1029: restart:
 1030: 	anyfreed = 1;
 1031: 	for (;anyfreed;) {
 1032: 		anyfreed = 0;
 1033: 		for (bp = TAILQ_FIRST(&vp->v_cleanblkhd); bp; bp = nbp) {
 1034: 			nbp = TAILQ_NEXT(bp, b_vnbufs);
 1035: 			if (bp->b_lblkno >= trunclbn) {
 1036: 				if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
 1037: 					BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL);
 1038: 					goto restart;
 1039: 				} else {
 1040: 					bremfree(bp);
 1041: 					bp->b_flags |= (B_INVAL | B_RELBUF);
 1042: 					bp->b_flags &= ~B_ASYNC;
 1043: 					brelse(bp);
 1044: 					anyfreed = 1;
 1045: 				}
 1046: 				if (nbp &&
 1047: 				    (((nbp->b_xflags & BX_VNCLEAN) == 0) ||
 1048: 				    (nbp->b_vp != vp) ||
 1049: 				    (nbp->b_flags & B_DELWRI))) {
 1050: 					goto restart;
 1051: 				}
 1052: 			}
 1053: 		}
 1054: 
 1055: 		for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
 1056: 			nbp = TAILQ_NEXT(bp, b_vnbufs);
 1057: 			if (bp->b_lblkno >= trunclbn) {
 1058: 				if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
 1059: 					BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL);
 1060: 					goto restart;
 1061: 				} else {
 1062: 					bremfree(bp);
 1063: 					bp->b_flags |= (B_INVAL | B_RELBUF);
 1064: 					bp->b_flags &= ~B_ASYNC;
 1065: 					brelse(bp);
 1066: 					anyfreed = 1;
 1067: 				}
 1068: 				if (nbp &&
 1069: 				    (((nbp->b_xflags & BX_VNDIRTY) == 0) ||
 1070: 				    (nbp->b_vp != vp) ||
 1071: 				    (nbp->b_flags & B_DELWRI) == 0)) {
 1072: 					goto restart;
 1073: 				}
 1074: 			}
 1075: 		}
 1076: 	}
 1077: 
 1078: 	if (length > 0) {
 1079: restartsync:
 1080: 		for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
 1081: 			nbp = TAILQ_NEXT(bp, b_vnbufs);
 1082: 			if ((bp->b_flags & B_DELWRI) && (bp->b_lblkno < 0)) {
 1083: 				if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
 1084: 					BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL);
 1085: 					goto restart;
 1086: 				} else {
 1087: 					bremfree(bp);
 1088: 					if (bp->b_vp == vp) {
 1089: 						bp->b_flags |= B_ASYNC;
 1090: 					} else {
 1091: 						bp->b_flags &= ~B_ASYNC;
 1092: 					}
 1093: 					VOP_BWRITE(bp->b_vp, bp);
 1094: 				}
 1095: 				goto restartsync;
 1096: 			}
 1097: 
 1098: 		}
 1099: 	}
 1100: 
 1101: 	while (vp->v_numoutput > 0) {
 1102: 		vp->v_flag |= VBWAIT;
 1103: 		tsleep(&vp->v_numoutput, 0, "vbtrunc", 0);
 1104: 	}
 1105: 
 1106: 	splx(s);
 1107: 
 1108: 	vnode_pager_setsize(vp, length);
 1109: 
 1110: 	return (0);
 1111: }
 1112: 
 1113: /*
 1114:  * Associate a buffer with a vnode.
 1115:  */
 1116: void
 1117: bgetvp(vp, bp)
 1118: 	struct vnode *vp;
 1119: 	struct buf *bp;
 1120: {
 1121: 	int s;
 1122: 
 1123: 	KASSERT(bp->b_vp == NULL, ("bgetvp: not free"));
 1124: 
 1125: 	vhold(vp);
 1126: 	bp->b_vp = vp;
 1127: 	bp->b_dev = vn_todev(vp);
 1128: 	/*
 1129: 	 * Insert onto list for new vnode.
 1130: 	 */
 1131: 	s = splbio();
 1132: 	bp->b_xflags |= BX_VNCLEAN;
 1133: 	bp->b_xflags &= ~BX_VNDIRTY;
 1134: 	TAILQ_INSERT_TAIL(&vp->v_cleanblkhd, bp, b_vnbufs);
 1135: 	splx(s);
 1136: }
 1137: 
 1138: /*
 1139:  * Disassociate a buffer from a vnode.
 1140:  */
 1141: void
 1142: brelvp(bp)
 1143: 	struct buf *bp;
 1144: {
 1145: 	struct vnode *vp;
 1146: 	struct buflists *listheadp;
 1147: 	int s;
 1148: 
 1149: 	KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
 1150: 
 1151: 	/*
 1152: 	 * Delete from old vnode list, if on one.
 1153: 	 */
 1154: 	vp = bp->b_vp;
 1155: 	s = splbio();
 1156: 	if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) {
 1157: 		if (bp->b_xflags & BX_VNDIRTY)
 1158: 			listheadp = &vp->v_dirtyblkhd;
 1159: 		else 
 1160: 			listheadp = &vp->v_cleanblkhd;
 1161: 		TAILQ_REMOVE(listheadp, bp, b_vnbufs);
 1162: 		bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN);
 1163: 	}
 1164: 	if ((vp->v_flag & VONWORKLST) && TAILQ_EMPTY(&vp->v_dirtyblkhd)) {
 1165: 		vp->v_flag &= ~VONWORKLST;
 1166: 		LIST_REMOVE(vp, v_synclist);
 1167: 	}
 1168: 	splx(s);
 1169: 	bp->b_vp = (struct vnode *) 0;
 1170: 	vdrop(vp);
 1171: }
 1172: 
 1173: /*
 1174:  * The workitem queue.
 1175:  * 
 1176:  * It is useful to delay writes of file data and filesystem metadata
 1177:  * for tens of seconds so that quickly created and deleted files need
 1178:  * not waste disk bandwidth being created and removed. To realize this,
 1179:  * we append vnodes to a "workitem" queue. When running with a soft
 1180:  * updates implementation, most pending metadata dependencies should
 1181:  * not wait for more than a few seconds. Thus, mounted on block devices
 1182:  * are delayed only about a half the time that file data is delayed.
 1183:  * Similarly, directory updates are more critical, so are only delayed
 1184:  * about a third the time that file data is delayed. Thus, there are
 1185:  * SYNCER_MAXDELAY queues that are processed round-robin at a rate of
 1186:  * one each second (driven off the filesystem syncer process). The
 1187:  * syncer_delayno variable indicates the next queue that is to be processed.
 1188:  * Items that need to be processed soon are placed in this queue:
 1189:  *
 1190:  *	syncer_workitem_pending[syncer_delayno]
 1191:  *
 1192:  * A delay of fifteen seconds is done by placing the request fifteen
 1193:  * entries later in the queue:
 1194:  *
 1195:  *	syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask]
 1196:  *
 1197:  */
 1198: 
 1199: /*
 1200:  * Add an item to the syncer work queue.
 1201:  */
 1202: static void
 1203: vn_syncer_add_to_worklist(struct vnode *vp, int delay)
 1204: {
 1205: 	int s, slot;
 1206: 
 1207: 	s = splbio();
 1208: 
 1209: 	if (vp->v_flag & VONWORKLST) {
 1210: 		LIST_REMOVE(vp, v_synclist);
 1211: 	}
 1212: 
 1213: 	if (delay > syncer_maxdelay - 2)
 1214: 		delay = syncer_maxdelay - 2;
 1215: 	slot = (syncer_delayno + delay) & syncer_mask;
 1216: 
 1217: 	LIST_INSERT_HEAD(&syncer_workitem_pending[slot], vp, v_synclist);
 1218: 	vp->v_flag |= VONWORKLST;
 1219: 	splx(s);
 1220: }
 1221: 
 1222: struct  thread *updatethread;
 1223: static void sched_sync (void);
 1224: static struct kproc_desc up_kp = {
 1225: 	"syncer",
 1226: 	sched_sync,
 1227: 	&updatethread
 1228: };
 1229: SYSINIT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp)
 1230: 
 1231: /*
 1232:  * System filesystem synchronizer daemon.
 1233:  */
 1234: void 
 1235: sched_sync(void)
 1236: {
 1237: 	struct synclist *slp;
 1238: 	struct vnode *vp;
 1239: 	long starttime;
 1240: 	int s;
 1241: 	struct thread *td = curthread;
 1242: 
 1243: 	EVENTHANDLER_REGISTER(shutdown_pre_sync, shutdown_kproc, td,
 1244: 	    SHUTDOWN_PRI_LAST);   
 1245: 
 1246: 	for (;;) {
 1247: 		kproc_suspend_loop();
 1248: 
 1249: 		starttime = time_second;
 1250: 
 1251: 		/*
 1252: 		 * Push files whose dirty time has expired.  Be careful
 1253: 		 * of interrupt race on slp queue.
 1254: 		 */
 1255: 		s = splbio();
 1256: 		slp = &syncer_workitem_pending[syncer_delayno];
 1257: 		syncer_delayno += 1;
 1258: 		if (syncer_delayno == syncer_maxdelay)
 1259: 			syncer_delayno = 0;
 1260: 		splx(s);
 1261: 
 1262: 		while ((vp = LIST_FIRST(slp)) != NULL) {
 1263: 			if (VOP_ISLOCKED(vp, NULL) == 0) {
 1264: 				vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
 1265: 				(void) VOP_FSYNC(vp, MNT_LAZY, td);
 1266: 				VOP_UNLOCK(vp, NULL, 0, td);
 1267: 			}
 1268: 			s = splbio();
 1269: 			if (LIST_FIRST(slp) == vp) {
 1270: 				/*
 1271: 				 * Note: v_tag VT_VFS vps can remain on the
 1272: 				 * worklist too with no dirty blocks, but 
 1273: 				 * since sync_fsync() moves it to a different 
 1274: 				 * slot we are safe.
 1275: 				 */
 1276: 				if (TAILQ_EMPTY(&vp->v_dirtyblkhd) &&
 1277: 				    !vn_isdisk(vp, NULL))
 1278: 					panic("sched_sync: fsync failed vp %p tag %d", vp, vp->v_tag);
 1279: 				/*
 1280: 				 * Put us back on the worklist.  The worklist
 1281: 				 * routine will remove us from our current
 1282: 				 * position and then add us back in at a later
 1283: 				 * position.
 1284: 				 */
 1285: 				vn_syncer_add_to_worklist(vp, syncdelay);
 1286: 			}
 1287: 			splx(s);
 1288: 		}
 1289: 
 1290: 		/*
 1291: 		 * Do soft update processing.
 1292: 		 */
 1293: 		if (bioops.io_sync)
 1294: 			(*bioops.io_sync)(NULL);
 1295: 
 1296: 		/*
 1297: 		 * The variable rushjob allows the kernel to speed up the
 1298: 		 * processing of the filesystem syncer process. A rushjob
 1299: 		 * value of N tells the filesystem syncer to process the next
 1300: 		 * N seconds worth of work on its queue ASAP. Currently rushjob
 1301: 		 * is used by the soft update code to speed up the filesystem
 1302: 		 * syncer process when the incore state is getting so far
 1303: 		 * ahead of the disk that the kernel memory pool is being
 1304: 		 * threatened with exhaustion.
 1305: 		 */
 1306: 		if (rushjob > 0) {
 1307: 			rushjob -= 1;
 1308: 			continue;
 1309: 		}
 1310: 		/*
 1311: 		 * If it has taken us less than a second to process the
 1312: 		 * current work, then wait. Otherwise start right over
 1313: 		 * again. We can still lose time if any single round
 1314: 		 * takes more than two seconds, but it does not really
 1315: 		 * matter as we are just trying to generally pace the
 1316: 		 * filesystem activity.
 1317: 		 */
 1318: 		if (time_second == starttime)
 1319: 			tsleep(&lbolt, 0, "syncer", 0);
 1320: 	}
 1321: }
 1322: 
 1323: /*
 1324:  * Request the syncer daemon to speed up its work.
 1325:  * We never push it to speed up more than half of its
 1326:  * normal turn time, otherwise it could take over the cpu.
 1327:  *
 1328:  * YYY wchan field protected by the BGL.
 1329:  */
 1330: int
 1331: speedup_syncer()
 1332: {
 1333: 	crit_enter();
 1334: 	if (updatethread->td_wchan == &lbolt) { /* YYY */
 1335: 		unsleep(updatethread);
 1336: 		lwkt_schedule(updatethread);
 1337: 	}
 1338: 	crit_exit();
 1339: 	if (rushjob < syncdelay / 2) {
 1340: 		rushjob += 1;
 1341: 		stat_rush_requests += 1;
 1342: 		return (1);
 1343: 	}
 1344: 	return(0);
 1345: }
 1346: 
 1347: /*
 1348:  * Associate a p-buffer with a vnode.
 1349:  *
 1350:  * Also sets B_PAGING flag to indicate that vnode is not fully associated
 1351:  * with the buffer.  i.e. the bp has not been linked into the vnode or
 1352:  * ref-counted.
 1353:  */
 1354: void
 1355: pbgetvp(vp, bp)
 1356: 	struct vnode *vp;
 1357: 	struct buf *bp;
 1358: {
 1359: 
 1360: 	KASSERT(bp->b_vp == NULL, ("pbgetvp: not free"));
 1361: 
 1362: 	bp->b_vp = vp;
 1363: 	bp->b_flags |= B_PAGING;
 1364: 	bp->b_dev = vn_todev(vp);
 1365: }
 1366: 
 1367: /*
 1368:  * Disassociate a p-buffer from a vnode.
 1369:  */
 1370: void
 1371: pbrelvp(bp)
 1372: 	struct buf *bp;
 1373: {
 1374: 
 1375: 	KASSERT(bp->b_vp != NULL, ("pbrelvp: NULL"));
 1376: 
 1377: 	/* XXX REMOVE ME */
 1378: 	if (TAILQ_NEXT(bp, b_vnbufs) != NULL) {
 1379: 		panic(
 1380: 		    "relpbuf(): b_vp was probably reassignbuf()d %p %x", 
 1381: 		    bp,
 1382: 		    (int)bp->b_flags
 1383: 		);
 1384: 	}
 1385: 	bp->b_vp = (struct vnode *) 0;
 1386: 	bp->b_flags &= ~B_PAGING;
 1387: }
 1388: 
 1389: void
 1390: pbreassignbuf(bp, newvp)
 1391: 	struct buf *bp;
 1392: 	struct vnode *newvp;
 1393: {
 1394: 	if ((bp->b_flags & B_PAGING) == 0) {
 1395: 		panic(
 1396: 		    "pbreassignbuf() on non phys bp %p", 
 1397: 		    bp
 1398: 		);
 1399: 	}
 1400: 	bp->b_vp = newvp;
 1401: }
 1402: 
 1403: /*
 1404:  * Reassign a buffer from one vnode to another.
 1405:  * Used to assign file specific control information
 1406:  * (indirect blocks) to the vnode to which they belong.
 1407:  */
 1408: void
 1409: reassignbuf(bp, newvp)
 1410: 	struct buf *bp;
 1411: 	struct vnode *newvp;
 1412: {
 1413: 	struct buflists *listheadp;
 1414: 	int delay;
 1415: 	int s;
 1416: 
 1417: 	if (newvp == NULL) {
 1418: 		printf("reassignbuf: NULL");
 1419: 		return;
 1420: 	}
 1421: 	++reassignbufcalls;
 1422: 
 1423: 	/*
 1424: 	 * B_PAGING flagged buffers cannot be reassigned because their vp
 1425: 	 * is not fully linked in.
 1426: 	 */
 1427: 	if (bp->b_flags & B_PAGING)
 1428: 		panic("cannot reassign paging buffer");
 1429: 
 1430: 	s = splbio();
 1431: 	/*
 1432: 	 * Delete from old vnode list, if on one.
 1433: 	 */
 1434: 	if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) {
 1435: 		if (bp->b_xflags & BX_VNDIRTY)
 1436: 			listheadp = &bp->b_vp->v_dirtyblkhd;
 1437: 		else 
 1438: 			listheadp = &bp->b_vp->v_cleanblkhd;
 1439: 		TAILQ_REMOVE(listheadp, bp, b_vnbufs);
 1440: 		bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN);
 1441: 		if (bp->b_vp != newvp) {
 1442: 			vdrop(bp->b_vp);
 1443: 			bp->b_vp = NULL;	/* for clarification */
 1444: 		}
 1445: 	}
 1446: 	/*
 1447: 	 * If dirty, put on list of dirty buffers; otherwise insert onto list
 1448: 	 * of clean buffers.
 1449: 	 */
 1450: 	if (bp->b_flags & B_DELWRI) {
 1451: 		struct buf *tbp;
 1452: 
 1453: 		listheadp = &newvp->v_dirtyblkhd;
 1454: 		if ((newvp->v_flag & VONWORKLST) == 0) {
 1455: 			switch (newvp->v_type) {
 1456: 			case VDIR:
 1457: 				delay = dirdelay;
 1458: 				break;
 1459: 			case VCHR:
 1460: 			case VBLK:
 1461: 				if (newvp->v_specmountpoint != NULL) {
 1462: 					delay = metadelay;
 1463: 					break;
 1464: 				}
 1465: 				/* fall through */
 1466: 			default:
 1467: 				delay = filedelay;
 1468: 			}
 1469: 			vn_syncer_add_to_worklist(newvp, delay);
 1470: 		}
 1471: 		bp->b_xflags |= BX_VNDIRTY;
 1472: 		tbp = TAILQ_FIRST(listheadp);
 1473: 		if (tbp == NULL ||
 1474: 		    bp->b_lblkno == 0 ||
 1475: 		    (bp->b_lblkno > 0 && tbp->b_lblkno < 0) ||
 1476: 		    (bp->b_lblkno > 0 && bp->b_lblkno < tbp->b_lblkno)) {
 1477: 			TAILQ_INSERT_HEAD(listheadp, bp, b_vnbufs);
 1478: 			++reassignbufsortgood;
 1479: 		} else if (bp->b_lblkno < 0) {
 1480: 			TAILQ_INSERT_TAIL(listheadp, bp, b_vnbufs);
 1481: 			++reassignbufsortgood;
 1482: 		} else if (reassignbufmethod == 1) {
 1483: 			/*
 1484: 			 * New sorting algorithm, only handle sequential case,
 1485: 			 * otherwise append to end (but before metadata)
 1486: 			 */
 1487: 			if ((tbp = gbincore(newvp, bp->b_lblkno - 1)) != NULL &&
 1488: 			    (tbp->b_xflags & BX_VNDIRTY)) {
 1489: 				/*
 1490: 				 * Found the best place to insert the buffer
 1491: 				 */
 1492: 				TAILQ_INSERT_AFTER(listheadp, tbp, bp, b_vnbufs);
 1493: 				++reassignbufsortgood;
 1494: 			} else {
 1495: 				/*
 1496: 				 * Missed, append to end, but before meta-data.
 1497: 				 * We know that the head buffer in the list is
 1498: 				 * not meta-data due to prior conditionals.
 1499: 				 *
 1500: 				 * Indirect effects:  NFS second stage write
 1501: 				 * tends to wind up here, giving maximum 
 1502: 				 * distance between the unstable write and the
 1503: 				 * commit rpc.
 1504: 				 */
 1505: 				tbp = TAILQ_LAST(listheadp, buflists);
 1506: 				while (tbp && tbp->b_lblkno < 0)
 1507: 					tbp = TAILQ_PREV(tbp, buflists, b_vnbufs);
 1508: 				TAILQ_INSERT_AFTER(listheadp, tbp, bp, b_vnbufs);
 1509: 				++reassignbufsortbad;
 1510: 			}
 1511: 		} else {
 1512: 			/*
 1513: 			 * Old sorting algorithm, scan queue and insert
 1514: 			 */
 1515: 			struct buf *ttbp;
 1516: 			while ((ttbp = TAILQ_NEXT(tbp, b_vnbufs)) &&
 1517: 			    (ttbp->b_lblkno < bp->b_lblkno)) {
 1518: 				++reassignbufloops;
 1519: 				tbp = ttbp;
 1520: 			}
 1521: 			TAILQ_INSERT_AFTER(listheadp, tbp, bp, b_vnbufs);
 1522: 		}
 1523: 	} else {
 1524: 		bp->b_xflags |= BX_VNCLEAN;
 1525: 		TAILQ_INSERT_TAIL(&newvp->v_cleanblkhd, bp, b_vnbufs);
 1526: 		if ((newvp->v_flag & VONWORKLST) &&
 1527: 		    TAILQ_EMPTY(&newvp->v_dirtyblkhd)) {
 1528: 			newvp->v_flag &= ~VONWORKLST;
 1529: 			LIST_REMOVE(newvp, v_synclist);
 1530: 		}
 1531: 	}
 1532: 	if (bp->b_vp != newvp) {
 1533: 		bp->b_vp = newvp;
 1534: 		vhold(bp->b_vp);
 1535: 	}
 1536: 	splx(s);
 1537: }
 1538: 
 1539: /*
 1540:  * Create a vnode for a block device.
 1541:  * Used for mounting the root file system.
 1542:  */
 1543: int
 1544: bdevvp(dev, vpp)
 1545: 	dev_t dev;
 1546: 	struct vnode **vpp;
 1547: {
 1548: 	struct vnode *vp;
 1549: 	struct vnode *nvp;
 1550: 	int error;
 1551: 
 1552: 	if (dev == NODEV) {
 1553: 		*vpp = NULLVP;
 1554: 		return (ENXIO);
 1555: 	}
 1556: 	error = getnewvnode(VT_NON, (struct mount *)0, spec_vnodeop_p, &nvp);
 1557: 	if (error) {
 1558: 		*vpp = NULLVP;
 1559: 		return (error);
 1560: 	}
 1561: 	vp = nvp;
 1562: 	vp->v_type = VBLK;
 1563: 	addalias(vp, dev);
 1564: 	*vpp = vp;
 1565: 	return (0);
 1566: }
 1567: 
 1568: /*
 1569:  * Add a vnode to the alias list hung off the dev_t.
 1570:  *
 1571:  * The reason for this gunk is that multiple vnodes can reference
 1572:  * the same physical device, so checking vp->v_usecount to see
 1573:  * how many users there are is inadequate; the v_usecount for
 1574:  * the vnodes need to be accumulated.  vcount() does that.
 1575:  */
 1576: void
 1577: addaliasu(struct vnode *nvp, udev_t nvp_rdev)
 1578: {
 1579: 	dev_t dev;
 1580: 
 1581: 	if (nvp->v_type != VBLK && nvp->v_type != VCHR)
 1582: 		panic("addaliasu on non-special vnode");
 1583: 	dev = udev2dev(nvp_rdev, nvp->v_type == VBLK ? 1 : 0);
 1584: 	if (dev != NODEV) {
 1585: 		nvp->v_rdev = dev;
 1586: 		addalias(nvp, dev);
 1587: 	} else
 1588: 		nvp->v_rdev = NULL;
 1589: }
 1590: 
 1591: void
 1592: addalias(struct vnode *nvp, dev_t dev)
 1593: {
 1594: 	lwkt_tokref ilock;
 1595: 
 1596: 	if (nvp->v_type != VBLK && nvp->v_type != VCHR)
 1597: 		panic("addalias on non-special vnode");
 1598: 
 1599: 	nvp->v_rdev = dev;
 1600: 	lwkt_gettoken(&ilock, &spechash_token);
 1601: 	SLIST_INSERT_HEAD(&dev->si_hlist, nvp, v_specnext);
 1602: 	lwkt_reltoken(&ilock);
 1603: }
 1604: 
 1605: /*
 1606:  * Grab a particular vnode from the free list, increment its
 1607:  * reference count and lock it. The vnode lock bit is set if the
 1608:  * vnode is being eliminated in vgone. The process is awakened
 1609:  * when the transition is completed, and an error returned to
 1610:  * indicate that the vnode is no longer usable (possibly having
 1611:  * been changed to a new file system type).
 1612:  *
 1613:  * This code is very sensitive.  We are depending on the vnode interlock
 1614:  * to be maintained through to the vn_lock() call, which means that we
 1615:  * cannot block which means that we cannot call vbusy() until after vn_lock().
 1616:  * If the interlock is not maintained, the VXLOCK check will not properly
 1617:  * interlock against a vclean()'s LK_DRAIN operation on the lock.
 1618:  */
 1619: int
 1620: vget(struct vnode *vp, lwkt_tokref_t vlock, int flags, thread_t td)
 1621: {
 1622: 	int error;
 1623: 	lwkt_tokref vvlock;
 1624: 
 1625: 	/*
 1626: 	 * We need the interlock to safely modify the v_ fields.  ZZZ it is
 1627: 	 * only legal to pass (1) the vnode's interlock and (2) only pass
 1628: 	 * NULL w/o LK_INTERLOCK if the vnode is *ALREADY* referenced or
 1629: 	 * held.
 1630: 	 */
 1631: 	if ((flags & LK_INTERLOCK) == 0) {
 1632: 		lwkt_gettoken(&vvlock, vp->v_interlock);
 1633: 		vlock = &vvlock;
 1634: 	}
 1635: 
 1636: 	/*
 1637: 	 * If the vnode is in the process of being cleaned out for
 1638: 	 * another use, we wait for the cleaning to finish and then
 1639: 	 * return failure. Cleaning is determined by checking that
 1640: 	 * the VXLOCK flag is set.  It is possible for the vnode to be
 1641: 	 * self-referenced during the cleaning operation.
 1642: 	 */
 1643: 	if (vp->v_flag & VXLOCK) {
 1644: 		if (vp->v_vxthread == curthread) {
 1645: #if 0
 1646: 			/* this can now occur in normal operation */
 1647: 			log(LOG_INFO, "VXLOCK interlock avoided\n");
 1648: #endif
 1649: 		} else {
 1650: 			vp->v_flag |= VXWANT;
 1651: 			lwkt_reltoken(vlock);
 1652: 			tsleep((caddr_t)vp, 0, "vget", 0);
 1653: 			return (ENOENT);
 1654: 		}
 1655: 	}
 1656: 
 1657: 	/*
 1658: 	 * Bump v_usecount to prevent the vnode from being recycled.  The
 1659: 	 * usecount needs to be bumped before we successfully get our lock.
 1660: 	 */
 1661: 	vp->v_usecount++;
 1662: 	if (flags & LK_TYPE_MASK) {
 1663: 		if ((error = vn_lock(vp, vlock, flags | LK_INTERLOCK, td)) != 0) {
 1664: 			/*
 1665: 			 * must expand vrele here because we do not want
 1666: 			 * to call VOP_INACTIVE if the reference count
 1667: 			 * drops back to zero since it was never really
 1668: 			 * active. We must remove it from the free list
 1669: 			 * before sleeping so that multiple processes do
 1670: 			 * not try to recycle it.
 1671: 			 */
 1672: 			lwkt_gettokref(vlock);
 1673: 			vp->v_usecount--;
 1674: 			vmaybefree(vp);
 1675: 			lwkt_reltoken(vlock);
 1676: 		}
 1677: 		return (error);
 1678: 	}
 1679: 	if (VSHOULDBUSY(vp))
 1680: 		vbusy(vp);	/* interlock must be held on call */
 1681: 	lwkt_reltoken(vlock);
 1682: 	return (0);
 1683: }
 1684: 
 1685: void
 1686: vref(struct vnode *vp)
 1687: {
 1688: 	crit_enter();	/* YYY use crit section for moment / BGL protected */
 1689: 	vp->v_usecount++;
 1690: 	crit_exit();
 1691: }
 1692: 
 1693: /*
 1694:  * Vnode put/release.
 1695:  * If count drops to zero, call inactive routine and return to freelist.
 1696:  */
 1697: void
 1698: vrele(struct vnode *vp)
 1699: {
 1700: 	struct thread *td = curthread;	/* XXX */
 1701: 	lwkt_tokref vlock;
 1702: 
 1703: 	KASSERT(vp != NULL && vp->v_usecount >= 0,
 1704: 	    ("vrele: null vp or <=0 v_usecount"));
 1705: 
 1706: 	lwkt_gettoken(&vlock, vp->v_interlock);
 1707: 
 1708: 	if (vp->v_usecount > 1) {
 1709: 		vp->v_usecount--;
 1710: 		lwkt_reltoken(&vlock);
 1711: 		return;
 1712: 	}
 1713: 
 1714: 	if (vp->v_usecount == 1) {
 1715: 		vp->v_usecount--;
 1716: 		/*
 1717: 		 * We must call VOP_INACTIVE with the node locked and the
 1718: 		 * usecount 0.  If we are doing a vpu, the node is already
 1719: 		 * locked, but, in the case of vrele, we must explicitly lock
 1720: 		 * the vnode before calling VOP_INACTIVE.
 1721: 		 */
 1722: 
 1723: 		if (vn_lock(vp, NULL, LK_EXCLUSIVE, td) == 0)
 1724: 			VOP_INACTIVE(vp, td);
 1725: 		vmaybefree(vp);
 1726: 		lwkt_reltoken(&vlock);
 1727: 	} else {
 1728: #ifdef DIAGNOSTIC
 1729: 		vprint("vrele: negative ref count", vp);
 1730: #endif
 1731: 		lwkt_reltoken(&vlock);
 1732: 		panic("vrele: negative ref cnt");
 1733: 	}
 1734: }
 1735: 
 1736: void
 1737: vput(struct vnode *vp)
 1738: {
 1739: 	struct thread *td = curthread;	/* XXX */
 1740: 	lwkt_tokref vlock;
 1741: 
 1742: 	KASSERT(vp != NULL, ("vput: null vp"));
 1743: 
 1744: 	lwkt_gettoken(&vlock, vp->v_interlock);
 1745: 
 1746: 	if (vp->v_usecount > 1) {
 1747: 		vp->v_usecount--;
 1748: 		VOP_UNLOCK(vp, &vlock, LK_INTERLOCK, td);
 1749: 		return;
 1750: 	}
 1751: 
 1752: 	if (vp->v_usecount == 1) {
 1753: 		vp->v_usecount--;
 1754: 		/*
 1755: 		 * We must call VOP_INACTIVE with the node locked.
 1756: 		 * If we are doing a vpu, the node is already locked,
 1757: 		 * so we just need to release the vnode mutex.
 1758: 		 */
 1759: 		VOP_INACTIVE(vp, td);
 1760: 		vmaybefree(vp);
 1761: 		lwkt_reltoken(&vlock);
 1762: 	} else {
 1763: #ifdef DIAGNOSTIC
 1764: 		vprint("vput: negative ref count", vp);
 1765: #endif
 1766: 		lwkt_reltoken(&vlock);
 1767: 		panic("vput: negative ref cnt");
 1768: 	}
 1769: }
 1770: 
 1771: /*
 1772:  * Somebody doesn't want the vnode recycled. ZZZ vnode interlock should
 1773:  * be held but isn't.
 1774:  */
 1775: void
 1776: vhold(vp)
 1777: 	struct vnode *vp;
 1778: {
 1779: 	int s;
 1780: 
 1781:   	s = splbio();
 1782: 	vp->v_holdcnt++;
 1783: 	if (VSHOULDBUSY(vp))
 1784: 		vbusy(vp);	/* interlock must be held on call */
 1785: 	splx(s);
 1786: }
 1787: 
 1788: /*
 1789:  * One less who cares about this vnode.
 1790:  */
 1791: void
 1792: vdrop(vp)
 1793: 	struct vnode *vp;
 1794: {
 1795: 	lwkt_tokref vlock;
 1796: 
 1797: 	lwkt_gettoken(&vlock, vp->v_interlock);
 1798: 	if (vp->v_holdcnt <= 0)
 1799: 		panic("vdrop: holdcnt");
 1800: 	vp->v_holdcnt--;
 1801: 	vmaybefree(vp);
 1802: 	lwkt_reltoken(&vlock);
 1803: }
 1804: 
 1805: int
 1806: vmntvnodescan(
 1807:     struct mount *mp, 
 1808:     int (*fastfunc)(struct mount *mp, struct vnode *vp, void *data),
 1809:     int (*slowfunc)(struct mount *mp, struct vnode *vp, lwkt_tokref_t vlock, void *data),
 1810:     void *data
 1811: ) {
 1812: 	lwkt_tokref ilock;
 1813: 	lwkt_tokref vlock;
 1814: 	struct vnode *pvp;
 1815: 	struct vnode *vp;
 1816: 	int r = 0;
 1817: 
 1818: 	/*
 1819: 	 * Scan the vnodes on the mount's vnode list.  Use a placemarker
 1820: 	 */
 1821: 	pvp = zalloc(vnode_zone);
 1822: 	pvp->v_flag |= VPLACEMARKER;
 1823: 
 1824: 	lwkt_gettoken(&ilock, &mntvnode_token);
 1825: 	TAILQ_INSERT_HEAD(&mp->mnt_nvnodelist, pvp, v_nmntvnodes);
 1826: 
 1827: 	while ((vp = TAILQ_NEXT(pvp, v_nmntvnodes)) != NULL) {
 1828: 		/*
 1829: 		 * Move the placemarker and skip other placemarkers we
 1830: 		 * encounter.  The nothing can get in our way so the
 1831: 		 * mount point on the vp must be valid.
 1832: 		 */
 1833: 		TAILQ_REMOVE(&mp->mnt_nvnodelist, pvp, v_nmntvnodes);
 1834: 		TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, pvp, v_nmntvnodes);
 1835: 		if (vp->v_flag & VPLACEMARKER)
 1836: 			continue;
 1837: 		KKASSERT(vp->v_mount == mp);
 1838: 
 1839: 		/*
 1840: 		 * Quick test
 1841: 		 */
 1842: 		if (fastfunc) {
 1843: 			if ((r = fastfunc(mp, vp, data)) < 0)
 1844: 				continue;
 1845: 			if (r)
 1846: 				break;
 1847: 		}
 1848: 
 1849: 		/*
 1850: 		 * Get the vnodes interlock and make sure it is still on the
 1851: 		 * mount list.  Skip it if it has moved (we may encounter it
 1852: 		 * later).  Then do the with-interlock test.  The callback
 1853: 		 * is responsible for releasing the vnode interlock.
 1854: 		 *
 1855: 		 * The interlock is type-stable.
 1856: 		 */
 1857: 		if (slowfunc) {
 1858: 			lwkt_gettoken(&vlock, vp->v_interlock);
 1859: 			if (vp != TAILQ_PREV(pvp, vnodelst, v_nmntvnodes)) {
 1860: 				printf("vmntvnodescan (debug info only): f=%p vp=%p vnode ripped out from under us\n", slowfunc, vp);
 1861: 				lwkt_reltoken(&vlock);
 1862: 				continue;
 1863: 			}
 1864: 			if ((r = slowfunc(mp, vp, &vlock, data)) != 0) {
 1865: 				KKASSERT(lwkt_havetokref(&vlock) == 0);
 1866: 				break;
 1867: 			}
 1868: 			KKASSERT(lwkt_havetokref(&vlock) == 0);
 1869: 		}
 1870: 	}
 1871: 	TAILQ_REMOVE(&mp->mnt_nvnodelist, pvp, v_nmntvnodes);
 1872: 	zfree(vnode_zone, pvp);
 1873: 	lwkt_reltoken(&ilock);
 1874: 	return(r);
 1875: }
 1876: 
 1877: /*
 1878:  * Remove any vnodes in the vnode table belonging to mount point mp.
 1879:  *
 1880:  * If FORCECLOSE is not specified, there should not be any active ones,
 1881:  * return error if any are found (nb: this is a user error, not a
 1882:  * system error). If FORCECLOSE is specified, detach any active vnodes
 1883:  * that are found.
 1884:  *
 1885:  * If WRITECLOSE is set, only flush out regular file vnodes open for
 1886:  * writing.
 1887:  *
 1888:  * SKIPSYSTEM causes any vnodes marked VSYSTEM to be skipped.
 1889:  *
 1890:  * `rootrefs' specifies the base reference count for the root vnode
 1891:  * of this filesystem. The root vnode is considered busy if its
 1892:  * v_usecount exceeds this value. On a successful return, vflush()
 1893:  * will call vrele() on the root vnode exactly rootrefs times.
 1894:  * If the SKIPSYSTEM or WRITECLOSE flags are specified, rootrefs must
 1895:  * be zero.
 1896:  */
 1897: #ifdef DIAGNOSTIC
 1898: static int busyprt = 0;		/* print out busy vnodes */
 1899: SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, "");
 1900: #endif
 1901: 
 1902: static int vflush_scan(struct mount *mp, struct vnode *vp, lwkt_tokref_t vlock, void *data);
 1903: 
 1904: struct vflush_info {
 1905: 	int flags;
 1906: 	int busy;
 1907: 	thread_t td;
 1908: };
 1909: 
 1910: int
 1911: vflush(mp, rootrefs, flags)
 1912: 	struct mount *mp;
 1913: 	int rootrefs;
 1914: 	int flags;
 1915: {
 1916: 	struct thread *td = curthread;	/* XXX */
 1917: 	struct vnode *rootvp = NULL;
 1918: 	int error;
 1919: 	lwkt_tokref vlock;
 1920: 	struct vflush_info vflush_info;
 1921: 
 1922: 	if (rootrefs > 0) {
 1923: 		KASSERT((flags & (SKIPSYSTEM | WRITECLOSE)) == 0,
 1924: 		    ("vflush: bad args"));
 1925: 		/*
 1926: 		 * Get the filesystem root vnode. We can vput() it
 1927: 		 * immediately, since with rootrefs > 0, it won't go away.
 1928: 		 */
 1929: 		if ((error = VFS_ROOT(mp, &rootvp)) != 0)
 1930: 			return (error);
 1931: 		vput(rootvp);
 1932: 	}
 1933: 
 1934: 	vflush_info.busy = 0;
 1935: 	vflush_info.flags = flags;
 1936: 	vflush_info.td = td;
 1937: 	vmntvnodescan(mp, NULL, vflush_scan, &vflush_info);
 1938: 
 1939: 	if (rootrefs > 0 && (flags & FORCECLOSE) == 0) {
 1940: 		/*
 1941: 		 * If just the root vnode is busy, and if its refcount
 1942: 		 * is equal to `rootrefs', then go ahead and kill it.
 1943: 		 */
 1944: 		lwkt_gettoken(&vlock, rootvp->v_interlock);
 1945: 		KASSERT(vflush_info.busy > 0, ("vflush: not busy"));
 1946: 		KASSERT(rootvp->v_usecount >= rootrefs, ("vflush: rootrefs"));
 1947: 		if (vflush_info.busy == 1 && rootvp->v_usecount == rootrefs) {
 1948: 			vgonel(rootvp, &vlock, td);
 1949: 			vflush_info.busy = 0;
 1950: 		} else {
 1951: 			lwkt_reltoken(&vlock);
 1952: 		}
 1953: 	}
 1954: 	if (vflush_info.busy)
 1955: 		return (EBUSY);
 1956: 	for (; rootrefs > 0; rootrefs--)
 1957: 		vrele(rootvp);
 1958: 	return (0);
 1959: }
 1960: 
 1961: /*
 1962:  * The scan callback is made with an interlocked vnode.
 1963:  */
 1964: static int
 1965: vflush_scan(struct mount *mp, struct vnode *vp, lwkt_tokref_t vlock, void *data)
 1966: {
 1967: 	struct vflush_info *info = data;
 1968: 	struct vattr vattr;
 1969: 
 1970: 	/*
 1971: 	 * Skip over a vnodes marked VSYSTEM.
 1972: 	 */
 1973: 	if ((info->flags & SKIPSYSTEM) && (vp->v_flag & VSYSTEM)) {
 1974: 		lwkt_reltoken(vlock);
 1975: 		return(0);
 1976: 	}
 1977: 
 1978: 	/*
 1979: 	 * If WRITECLOSE is set, flush out unlinked but still open
 1980: 	 * files (even if open only for reading) and regular file
 1981: 	 * vnodes open for writing. 
 1982: 	 */
 1983: 	if ((info->flags & WRITECLOSE) &&
 1984: 	    (vp->v_type == VNON ||
 1985: 	    (VOP_GETATTR(vp, &vattr, info->td) == 0 &&
 1986: 	    vattr.va_nlink > 0)) &&
 1987: 	    (vp->v_writecount == 0 || vp->v_type != VREG)) {
 1988: 		lwkt_reltoken(vlock);
 1989: 		return(0);
 1990: 	}
 1991: 
 1992: 	/*
 1993: 	 * With v_usecount == 0, all we need to do is clear out the
 1994: 	 * vnode data structures and we are done.
 1995: 	 */
 1996: 	if (vp->v_usecount == 0) {
 1997: 		vgonel(vp, vlock, info->td);
 1998: 		return(0);
 1999: 	}
 2000: 
 2001: 	/*
 2002: 	 * If FORCECLOSE is set, forcibly close the vnode. For block
 2003: 	 * or character devices, revert to an anonymous device. For
 2004: 	 * all other files, just kill them.
 2005: 	 */
 2006: 	if (info->flags & FORCECLOSE) {
 2007: 		if (vp->v_type != VBLK && vp->v_type != VCHR) {
 2008: 			vgonel(vp, vlock, info->td);
 2009: 		} else {
 2010: 			vclean(vp, vlock, 0, info->td);
 2011: 			vp->v_op = spec_vnodeop_p;
 2012: 			insmntque(vp, (struct mount *) 0);
 2013: 		}
 2014: 		return(0);
 2015: 	}
 2016: #ifdef DIAGNOSTIC
 2017: 	if (busyprt)
 2018: 		vprint("vflush: busy vnode", vp);
 2019: #endif
 2020: 	lwkt_reltoken(vlock);
 2021: 	++info->busy;
 2022: 	return(0);
 2023: }
 2024: 
 2025: /*
 2026:  * Disassociate the underlying file system from a vnode.
 2027:  */
 2028: static void
 2029: vclean(struct vnode *vp, lwkt_tokref_t vlock, int flags, struct thread *td)
 2030: {
 2031: 	int active;
 2032: 
 2033: 	/*
 2034: 	 * Check to see if the vnode is in use. If so we have to reference it
 2035: 	 * before we clean it out so that its count cannot fall to zero and
 2036: 	 * generate a race against ourselves to recycle it.
 2037: 	 */
 2038: 	if ((active = vp->v_usecount))
 2039: 		vp->v_usecount++;
 2040: 
 2041: 	/*
 2042: 	 * Prevent the vnode from being recycled or brought into use while we
 2043: 	 * clean it out.
 2044: 	 */
 2045: 	if (vp->v_flag & VXLOCK)
 2046: 		panic("vclean: deadlock");
 2047: 	vp->v_flag |= VXLOCK;
 2048: 	vp->v_vxthread = curthread;
 2049: 
 2050: 	/*
 2051: 	 * Even if the count is zero, the VOP_INACTIVE routine may still
 2052: 	 * have the object locked while it cleans it out. The VOP_LOCK
 2053: 	 * ensures that the VOP_INACTIVE routine is done with its work.
 2054: 	 * For active vnodes, it ensures that no other activity can
 2055: 	 * occur while the underlying object is being cleaned out.
 2056: 	 *
 2057: 	 * NOTE: we continue to hold the vnode interlock through to the
 2058: 	 * end of vclean().
 2059: 	 */
 2060: 	VOP_LOCK(vp, NULL, LK_DRAIN, td);
 2061: 
 2062: 	/*
 2063: 	 * Clean out any buffers associated with the vnode.
 2064: 	 */
 2065: 	vinvalbuf(vp, V_SAVE, td, 0, 0);
 2066: 	VOP_DESTROYVOBJECT(vp);
 2067: 
 2068: 	/*
 2069: 	 * If purging an active vnode, it must be closed and
 2070: 	 * deactivated before being reclaimed. Note that the
 2071: 	 * VOP_INACTIVE will unlock the vnode.
 2072: 	 */
 2073: 	if (active) {
 2074: 		if (flags & DOCLOSE)
 2075: 			VOP_CLOSE(vp, FNONBLOCK, td);
 2076: 		VOP_INACTIVE(vp, td);
 2077: 	} else {
 2078: 		/*
 2079: 		 * Any other processes trying to obtain this lock must first
 2080: 		 * wait for VXLOCK to clear, then call the new lock operation.
 2081: 		 */
 2082: 		VOP_UNLOCK(vp, NULL, 0, td);
 2083: 	}
 2084: 	/*
 2085: 	 * Reclaim the vnode.
 2086: 	 */
 2087: 	if (VOP_RECLAIM(vp, td))
 2088: 		panic("vclean: cannot reclaim");
 2089: 
 2090: 	if (active) {
 2091: 		/*
 2092: 		 * Inline copy of vrele() since VOP_INACTIVE
 2093: 		 * has already been called.
 2094: 		 */
 2095: 		if (--vp->v_usecount <= 0) {
 2096: #ifdef DIAGNOSTIC
 2097: 			if (vp->v_usecount < 0 || vp->v_writecount != 0) {
 2098: 				vprint("vclean: bad ref count", vp);
 2099: 				panic("vclean: ref cnt");
 2100: 			}
 2101: #endif
 2102: 			vfree(vp);
 2103: 		}
 2104: 	}
 2105: 
 2106: 	cache_purge(vp);
 2107: 	vp->v_vnlock = NULL;
 2108: 	vmaybefree(vp);
 2109: 	
 2110: 	/*
 2111: 	 * Done with purge, notify sleepers of the grim news.
 2112: 	 */
 2113: 	vp->v_op = dead_vnodeop_p;
 2114: 	vn_pollgone(vp);
 2115: 	vp->v_tag = VT_NON;
 2116: 	vp->v_flag &= ~VXLOCK;
 2117: 	vp->v_vxthread = NULL;
 2118: 	if (vp->v_flag & VXWANT) {
 2119: 		vp->v_flag &= ~VXWANT;
 2120: 		wakeup((caddr_t) vp);
 2121: 	}
 2122: 	lwkt_reltoken(vlock);
 2123: }
 2124: 
 2125: /*
 2126:  * Eliminate all activity associated with the requested vnode
 2127:  * and with all vnodes aliased to the requested vnode.
 2128:  */
 2129: int
 2130: vop_revoke(ap)
 2131: 	struct vop_revoke_args /* {
 2132: 		struct vnode *a_vp;
 2133: 		int a_flags;
 2134: 	} */ *ap;
 2135: {
 2136: 	struct vnode *vp, *vq;
 2137: 	lwkt_tokref ilock;
 2138: 	dev_t dev;
 2139: 
 2140: 	KASSERT((ap->a_flags & REVOKEALL) != 0, ("vop_revoke"));
 2141: 
 2142: 	vp = ap->a_vp;
 2143: 	/*
 2144: 	 * If a vgone (or vclean) is already in progress,
 2145: 	 * wait until it is done and return.
 2146: 	 */
 2147: 	if (vp->v_flag & VXLOCK) {
 2148: 		vp->v_flag |= VXWANT;
 2149: 		/*lwkt_reltoken(vlock); ZZZ */
 2150: 		tsleep((caddr_t)vp, 0, "vop_revokeall", 0);
 2151: 		return (0);
 2152: 	}
 2153: 	dev = vp->v_rdev;
 2154: 	for (;;) {
 2155: 		lwkt_gettoken(&ilock, &spechash_token);
 2156: 		vq = SLIST_FIRST(&dev->si_hlist);
 2157: 		lwkt_reltoken(&ilock);
 2158: 		if (!vq)
 2159: 			break;
 2160: 		vgone(vq);
 2161: 	}
 2162: 	return (0);
 2163: }
 2164: 
 2165: /*
 2166:  * Recycle an unused vnode to the front of the free list.
 2167:  * Release the passed interlock if the vnode will be recycled.
 2168:  */
 2169: int
 2170: vrecycle(struct vnode *vp, lwkt_tokref_t inter_lkp, struct thread *td)
 2171: {
 2172: 	lwkt_tokref vlock;
 2173: 
 2174: 	lwkt_gettoken(&vlock, vp->v_interlock);
 2175: 	if (vp->v_usecount == 0) {
 2176: 		if (inter_lkp)
 2177: 			lwkt_reltoken(inter_lkp);
 2178: 		vgonel(vp, &vlock, td);
 2179: 		return (1);
 2180: 	}
 2181: 	lwkt_reltoken(&vlock);
 2182: 	return (0);
 2183: }
 2184: 
 2185: /*
 2186:  * Eliminate all activity associated with a vnode
 2187:  * in preparation for reuse.
 2188:  */
 2189: void
 2190: vgone(struct vnode *vp)
 2191: {
 2192: 	struct thread *td = curthread;	/* XXX */
 2193: 	lwkt_tokref vlock;
 2194: 
 2195: 	lwkt_gettoken(&vlock, vp->v_interlock);
 2196: 	vgonel(vp, &vlock, td);
 2197: }
 2198: 
 2199: /*
 2200:  * vgone, with the vp interlock held.
 2201:  */
 2202: void
 2203: vgonel(struct vnode *vp, lwkt_tokref_t vlock, struct thread *td)
 2204: {
 2205: 	lwkt_tokref ilock;
 2206: 	int s;
 2207: 
 2208: 	/*
 2209: 	 * If a vgone (or vclean) is already in progress,
 2210: 	 * wait until it is done and return.
 2211: 	 */
 2212: 	if (vp->v_flag & VXLOCK) {
 2213: 		vp->v_flag |= VXWANT;
 2214: 		lwkt_reltoken(vlock);
 2215: 		tsleep((caddr_t)vp, 0, "vgone", 0);
 2216: 		return;
 2217: 	}
 2218: 
 2219: 	/*
 2220: 	 * Clean out the filesystem specific data.
 2221: 	 */
 2222: 	vclean(vp, vlock, DOCLOSE, td);
 2223: 	lwkt_gettokref(vlock);
 2224: 
 2225: 	/*
 2226: 	 * Delete from old mount point vnode list, if on one.
 2227: 	 */
 2228: 	if (vp->v_mount != NULL)
 2229: 		insmntque(vp, (struct mount *)0);
 2230: 	/*
 2231: 	 * If special device, remove it from special device alias list
 2232: 	 * if it is on one.
 2233: 	 */
 2234: 	if ((vp->v_type == VBLK || vp->v_type == VCHR) && vp->v_rdev != NULL) {
 2235: 		lwkt_gettoken(&ilock, &spechash_token);
 2236: 		SLIST_REMOVE(&vp->v_hashchain, vp, vnode, v_specnext);
 2237: 		freedev(vp->v_rdev);
 2238: 		lwkt_reltoken(&ilock);
 2239: 		vp->v_rdev = NULL;
 2240: 	}
 2241: 
 2242: 	/*
 2243: 	 * If it is on the freelist and not already at the head,
 2244: 	 * move it to the head of the list. The test of the
 2245: 	 * VDOOMED flag and the reference count of zero is because
 2246: 	 * it will be removed from the free list by getnewvnode,
 2247: 	 * but will not have its reference count incremented until
 2248: 	 * after calling vgone. If the reference count were
 2249: 	 * incremented first, vgone would (incorrectly) try to
 2250: 	 * close the previous instance of the underlying object.
 2251: 	 */
 2252: 	if (vp->v_usecount == 0 && !(vp->v_flag & VDOOMED)) {
 2253: 		s = splbio();
 2254: 		lwkt_gettoken(&ilock, &vnode_free_list_token);
 2255: 		if (vp->v_flag & VFREE)
 2256: 			TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
 2257: 		else
 2258: 			freevnodes++;
 2259: 		vp->v_flag |= VFREE;
 2260: 		TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
 2261: 		lwkt_reltoken(&ilock);
 2262: 		splx(s);
 2263: 	}
 2264: 	vp->v_type = VBAD;
 2265: 	lwkt_reltoken(vlock);
 2266: }
 2267: 
 2268: /*
 2269:  * Lookup a vnode by device number.
 2270:  */
 2271: int
 2272: vfinddev(dev, type, vpp)
 2273: 	dev_t dev;
 2274: 	enum vtype type;
 2275: 	struct vnode **vpp;
 2276: {
 2277: 	lwkt_tokref ilock;
 2278: 	struct vnode *vp;
 2279: 
 2280: 	lwkt_gettoken(&ilock, &spechash_token);
 2281: 	SLIST_FOREACH(vp, &dev->si_hlist, v_specnext) {
 2282: 		if (type == vp->v_type) {
 2283: 			*vpp = vp;
 2284: 			lwkt_reltoken(&ilock);
 2285: 			return (1);
 2286: 		}
 2287: 	}
 2288: 	lwkt_reltoken(&ilock);
 2289: 	return (0);
 2290: }
 2291: 
 2292: /*
 2293:  * Calculate the total number of references to a special device.
 2294:  */
 2295: int
 2296: vcount(vp)
 2297: 	struct vnode *vp;
 2298: {
 2299: 	lwkt_tokref ilock;
 2300: 	struct vnode *vq;
 2301: 	int count;
 2302: 
 2303: 	count = 0;
 2304: 	lwkt_gettoken(&ilock, &spechash_token);
 2305: 	SLIST_FOREACH(vq, &vp->v_hashchain, v_specnext)
 2306: 		count += vq->v_usecount;
 2307: 	lwkt_reltoken(&ilock);
 2308: 	return (count);
 2309: }
 2310: 
 2311: /*
 2312:  * Same as above, but using the dev_t as argument
 2313:  */
 2314: 
 2315: int
 2316: count_dev(dev)
 2317: 	dev_t dev;
 2318: {
 2319: 	struct vnode *vp;
 2320: 
 2321: 	vp = SLIST_FIRST(&dev->si_hlist);
 2322: 	if (vp == NULL)
 2323: 		return (0);
 2324: 	return(vcount(vp));
 2325: }
 2326: 
 2327: /*
 2328:  * Print out a description of a vnode.
 2329:  */
 2330: static char *typename[] =
 2331: {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD"};
 2332: 
 2333: void
 2334: vprint(label, vp)
 2335: 	char *label;
 2336: 	struct vnode *vp;
 2337: {
 2338: 	char buf[96];
 2339: 
 2340: 	if (label != NULL)
 2341: 		printf("%s: %p: ", label, (void *)vp);
 2342: 	else
 2343: 		printf("%p: ", (void *)vp);
 2344: 	printf("type %s, usecount %d, writecount %d, refcount %d,",
 2345: 	    typename[vp->v_type], vp->v_usecount, vp->v_writecount,
 2346: 	    vp->v_holdcnt);
 2347: 	buf[0] = '\0';
 2348: 	if (vp->v_flag & VROOT)
 2349: 		strcat(buf, "|VROOT");
 2350: 	if (vp->v_flag & VTEXT)
 2351: 		strcat(buf, "|VTEXT");
 2352: 	if (vp->v_flag & VSYSTEM)
 2353: 		strcat(buf, "|VSYSTEM");
 2354: 	if (vp->v_flag & VXLOCK)
 2355: 		strcat(buf, "|VXLOCK");
 2356: 	if (vp->v_flag & VXWANT)
 2357: 		strcat(buf, "|VXWANT");
 2358: 	if (vp->v_flag & VBWAIT)
 2359: 		strcat(buf, "|VBWAIT");
 2360: 	if (vp->v_flag & VDOOMED)
 2361: 		strcat(buf, "|VDOOMED");
 2362: 	if (vp->v_flag & VFREE)
 2363: 		strcat(buf, "|VFREE");
 2364: 	if (vp->v_flag & VOBJBUF)
 2365: 		strcat(buf, "|VOBJBUF");
 2366: 	if (buf[0] != '\0')
 2367: 		printf(" flags (%s)", &buf[1]);
 2368: 	if (vp->v_data == NULL) {
 2369: 		printf("\n");
 2370: 	} else {
 2371: 		printf("\n\t");
 2372: 		VOP_PRINT(vp);
 2373: 	}
 2374: }
 2375: 
 2376: #ifdef DDB
 2377: #include <ddb/ddb.h>
 2378: /*
 2379:  * List all of the locked vnodes in the system.
 2380:  * Called when debugging the kernel.
 2381:  */
 2382: DB_SHOW_COMMAND(lockedvnodes, lockedvnodes)
 2383: {
 2384: 	struct thread *td = curthread;	/* XXX */
 2385: 	lwkt_tokref ilock;
 2386: 	struct mount *mp, *nmp;
 2387: 	struct vnode *vp;
 2388: 
 2389: 	printf("Locked vnodes\n");
 2390: 	lwkt_gettoken(&ilock, &mountlist_token);
 2391: 	for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
 2392: 		if (vfs_busy(mp, LK_NOWAIT, &ilock, td)) {
 2393: 			nmp = TAILQ_NEXT(mp, mnt_list);
 2394: 			continue;
 2395: 		}
 2396: 		TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
 2397: 			if (VOP_ISLOCKED(vp, NULL))
 2398: 				vprint((char *)0, vp);
 2399: 		}
 2400: 		lwkt_gettokref(&ilock);
 2401: 		nmp = TAILQ_NEXT(mp, mnt_list);
 2402: 		vfs_unbusy(mp, td);
 2403: 	}
 2404: 	lwkt_reltoken(&ilock);
 2405: }
 2406: #endif
 2407: 
 2408: /*
 2409:  * Top level filesystem related information gathering.
 2410:  */
 2411: static int	sysctl_ovfs_conf (SYSCTL_HANDLER_ARGS);
 2412: 
 2413: static int
 2414: vfs_sysctl(SYSCTL_HANDLER_ARGS)
 2415: {
 2416: 	int *name = (int *)arg1 - 1;	/* XXX */
 2417: 	u_int namelen = arg2 + 1;	/* XXX */
 2418: 	struct vfsconf *vfsp;
 2419: 
 2420: #if 1 || defined(COMPAT_PRELITE2)
 2421: 	/* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
 2422: 	if (namelen == 1)
 2423: 		return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
 2424: #endif
 2425: 
 2426: #ifdef notyet
 2427: 	/* all sysctl names at this level are at least name and field */
 2428: 	if (namelen < 2)
 2429: 		return (ENOTDIR);		/* overloaded */
 2430: 	if (name[0] != VFS_GENERIC) {
 2431: 		for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
 2432: 			if (vfsp->vfc_typenum == name[0])
 2433: 				break;
 2434: 		if (vfsp == NULL)
 2435: 			return (EOPNOTSUPP);
 2436: 		return ((*vfsp->vfc_vfsops->vfs_sysctl)(&name[1], namelen - 1,
 2437: 		    oldp, oldlenp, newp, newlen, p));
 2438: 	}
 2439: #endif
 2440: 	switch (name[1]) {
 2441: 	case VFS_MAXTYPENUM:
 2442: 		if (namelen != 2)
 2443: 			return (ENOTDIR);
 2444: 		return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int)));
 2445: 	case VFS_CONF:
 2446: 		if (namelen != 3)
 2447: 			return (ENOTDIR);	/* overloaded */
 2448: 		for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
 2449: 			if (vfsp->vfc_typenum == name[2])
 2450: 				break;
 2451: 		if (vfsp == NULL)
 2452: 			return (EOPNOTSUPP);
 2453: 		return (SYSCTL_OUT(req, vfsp, sizeof *vfsp));
 2454: 	}
 2455: 	return (EOPNOTSUPP);
 2456: }
 2457: 
 2458: SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD, vfs_sysctl,
 2459: 	"Generic filesystem");
 2460: 
 2461: #if 1 || defined(COMPAT_PRELITE2)
 2462: 
 2463: static int
 2464: sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
 2465: {
 2466: 	int error;
 2467: 	struct vfsconf *vfsp;
 2468: 	struct ovfsconf ovfs;
 2469: 
 2470: 	for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
 2471: 		ovfs.vfc_vfsops = vfsp->vfc_vfsops;	/* XXX used as flag */
 2472: 		strcpy(ovfs.vfc_name, vfsp->vfc_name);
 2473: 		ovfs.vfc_index = vfsp->vfc_typenum;
 2474: 		ovfs.vfc_refcount = vfsp->vfc_refcount;
 2475: 		ovfs.vfc_flags = vfsp->vfc_flags;
 2476: 		error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
 2477: 		if (error)
 2478: 			return error;
 2479: 	}
 2480: 	return 0;
 2481: }
 2482: 
 2483: #endif /* 1 || COMPAT_PRELITE2 */
 2484: 
 2485: #if 0
 2486: #define KINFO_VNODESLOP	10
 2487: /*
 2488:  * Dump vnode list (via sysctl).
 2489:  * Copyout address of vnode followed by vnode.
 2490:  */
 2491: /* ARGSUSED */
 2492: static int
 2493: sysctl_vnode(SYSCTL_HANDLER_ARGS)
 2494: {
 2495: 	struct proc *p = curproc;	/* XXX */
 2496: 	struct mount *mp, *nmp;
 2497: 	struct vnode *nvp, *vp;
 2498: 	lwkt_tokref ilock;
 2499: 	lwkt_tokref jlock;
 2500: 	int error;
 2501: 
 2502: #define VPTRSZ	sizeof (struct vnode *)
 2503: #define VNODESZ	sizeof (struct vnode)
 2504: 
 2505: 	req->lock = 0;
 2506: 	if (!req->oldptr) /* Make an estimate */
 2507: 		return (SYSCTL_OUT(req, 0,
 2508: 			(numvnodes + KINFO_VNODESLOP) * (VPTRSZ + VNODESZ)));
 2509: 
 2510: 	lwkt_gettoken(&ilock, &mountlist_token);
 2511: 	for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
 2512: 		if (vfs_busy(mp, LK_NOWAIT, &ilock, p)) {
 2513: 			nmp = TAILQ_NEXT(mp, mnt_list);
 2514: 			continue;
 2515: 		}
 2516: 		lwkt_gettoken(&jlock, &mntvnode_token);
 2517: again:
 2518: 		for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
 2519: 		     vp != NULL;
 2520: 		     vp = nvp) {
 2521: 			/*
 2522: 			 * Check that the vp is still associated with
 2523: 			 * this filesystem.  RACE: could have been
 2524: 			 * recycled onto the same filesystem.
 2525: 			 */
 2526: 			if (vp->v_mount != mp)
 2527: 				goto again;
 2528: 			nvp = TAILQ_NEXT(vp, v_nmntvnodes);
 2529: 			if ((error = SYSCTL_OUT(req, &vp, VPTRSZ)) ||
 2530: 			    (error = SYSCTL_OUT(req, vp, VNODESZ))) {
 2531: 				lwkt_reltoken(&jlock);
 2532: 				return (error);
 2533: 			}
 2534: 		}
 2535: 		lwkt_reltoken(&jlock);
 2536: 		lwkt_gettokref(&ilock);
 2537: 		nmp = TAILQ_NEXT(mp, mnt_list);	/* ZZZ */
 2538: 		vfs_unbusy(mp, p);
 2539: 	}
 2540: 	lwkt_reltoken(&ilock);
 2541: 
 2542: 	return (0);
 2543: }
 2544: #endif
 2545: 
 2546: /*
 2547:  * XXX
 2548:  * Exporting the vnode list on large systems causes them to crash.
 2549:  * Exporting the vnode list on medium systems causes sysctl to coredump.
 2550:  */
 2551: #if 0
 2552: SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE|CTLFLAG_RD,
 2553: 	0, 0, sysctl_vnode, "S,vnode", "");
 2554: #endif
 2555: 
 2556: /*
 2557:  * Check to see if a filesystem is mounted on a block device.
 2558:  */
 2559: int
 2560: vfs_mountedon(vp)
 2561: 	struct vnode *vp;
 2562: {
 2563: 
 2564: 	if (vp->v_specmountpoint != NULL)
 2565: 		return (EBUSY);
 2566: 	return (0);
 2567: }
 2568: 
 2569: /*
 2570:  * Unmount all filesystems. The list is traversed in reverse order
 2571:  * of mounting to avoid dependencies.
 2572:  */
 2573: void
 2574: vfs_unmountall()
 2575: {
 2576: 	struct mount *mp;
 2577: 	struct thread *td = curthread;
 2578: 	int error;
 2579: 
 2580: 	if (td->td_proc == NULL)
 2581: 		td = initproc->p_thread;	/* XXX XXX use proc0 instead? */
 2582: 
 2583: 	/*
 2584: 	 * Since this only runs when rebooting, it is not interlocked.
 2585: 	 */
 2586: 	while(!TAILQ_EMPTY(&mountlist)) {
 2587: 		mp = TAILQ_LAST(&mountlist, mntlist);
 2588: 		error = dounmount(mp, MNT_FORCE, td);
 2589: 		if (error) {
 2590: 			TAILQ_REMOVE(&mountlist, mp, mnt_list);
 2591: 			printf("unmount of %s failed (",
 2592: 			    mp->mnt_stat.f_mntonname);
 2593: 			if (error == EBUSY)
 2594: 				printf("BUSY)\n");
 2595: 			else
 2596: 				printf("%d)\n", error);
 2597: 		} else {
 2598: 			/* The unmount has removed mp from the mountlist */
 2599: 		}
 2600: 	}
 2601: }
 2602: 
 2603: /*
 2604:  * Build hash lists of net addresses and hang them off the mount point.
 2605:  * Called by ufs_mount() to set up the lists of export addresses.
 2606:  */
 2607: static int
 2608: vfs_hang_addrlist(mp, nep, argp)
 2609: 	struct mount *mp;
 2610: 	struct netexport *nep;
 2611: 	struct export_args *argp;
 2612: {
 2613: 	struct netcred *np;
 2614: 	struct radix_node_head *rnh;
 2615: 	int i;
 2616: 	struct radix_node *rn;
 2617: 	struct sockaddr *saddr, *smask = 0;
 2618: 	struct domain *dom;
 2619: 	int error;
 2620: 
 2621: 	if (argp->ex_addrlen == 0) {
 2622: 		if (mp->mnt_flag & MNT_DEFEXPORTED)
 2623: 			return (EPERM);
 2624: 		np = &nep->ne_defexported;
 2625: 		np->netc_exflags = argp->ex_flags;
 2626: 		np->netc_anon = argp->ex_anon;
 2627: 		np->netc_anon.cr_ref = 1;
 2628: 		mp->mnt_flag |= MNT_DEFEXPORTED;
 2629: 		return (0);
 2630: 	}
 2631: 
 2632: 	if (argp->ex_addrlen < 0 || argp->ex_addrlen > MLEN)
 2633: 		return (EINVAL);
 2634: 	if (argp->ex_masklen < 0 || argp->ex_masklen > MLEN)
 2635: 		return (EINVAL);
 2636: 
 2637: 	i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
 2638: 	np = (struct netcred *) malloc(i, M_NETADDR, M_WAITOK);
 2639: 	bzero((caddr_t) np, i);
 2640: 	saddr = (struct sockaddr *) (np + 1);
 2641: 	if ((error = copyin(argp->ex_addr, (caddr_t) saddr, argp->ex_addrlen)))
 2642: 		goto out;
 2643: 	if (saddr->sa_len > argp->ex_addrlen)
 2644: 		saddr->sa_len = argp->ex_addrlen;
 2645: 	if (argp->ex_masklen) {
 2646: 		smask = (struct sockaddr *) ((caddr_t) saddr + argp->ex_addrlen);
 2647: 		error = copyin(argp->ex_mask, (caddr_t) smask, argp->ex_masklen);
 2648: 		if (error)
 2649: 			goto out;
 2650: 		if (smask->sa_len > argp->ex_masklen)
 2651: 			smask->sa_len = argp->ex_masklen;
 2652: 	}
 2653: 	i = saddr->sa_family;
 2654: 	if ((rnh = nep->ne_rtable[i]) == 0) {
 2655: 		/*
 2656: 		 * Seems silly to initialize every AF when most are not used,
 2657: 		 * do so on demand here
 2658: 		 */
 2659: 		for (dom = domains; dom; dom = dom->dom_next)
 2660: 			if (dom->dom_family == i && dom->dom_rtattach) {
 2661: 				dom->dom_rtattach((void **) &nep->ne_rtable[i],
 2662: 				    dom->dom_rtoffset);
 2663: 				break;
 2664: 			}
 2665: 		if ((rnh = nep->ne_rtable[i]) == 0) {
 2666: 			error = ENOBUFS;
 2667: 			goto out;
 2668: 		}
 2669: 	}
 2670: 	rn = (*rnh->rnh_addaddr) ((caddr_t) saddr, (caddr_t) smask, rnh,
 2671: 	    np->netc_rnodes);
 2672: 	if (rn == 0 || np != (struct netcred *) rn) {	/* already exists */
 2673: 		error = EPERM;
 2674: 		goto out;
 2675: 	}
 2676: 	np->netc_exflags = argp->ex_flags;
 2677: 	np->netc_anon = argp->ex_anon;
 2678: 	np->netc_anon.cr_ref = 1;
 2679: 	return (0);
 2680: out:
 2681: 	free(np, M_NETADDR);
 2682: 	return (error);
 2683: }
 2684: 
 2685: /* ARGSUSED */
 2686: static int
 2687: vfs_free_netcred(rn, w)
 2688: 	struct radix_node *rn;
 2689: 	void *w;
 2690: {
 2691: 	struct radix_node_head *rnh = (struct radix_node_head *) w;
 2692: 
 2693: 	(*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, rnh);
 2694: 	free((caddr_t) rn, M_NETADDR);
 2695: 	return (0);
 2696: }
 2697: 
 2698: /*
 2699:  * Free the net address hash lists that are hanging off the mount points.
 2700:  */
 2701: static void
 2702: vfs_free_addrlist(nep)
 2703: 	struct netexport *nep;
 2704: {
 2705: 	int i;
 2706: 	struct radix_node_head *rnh;
 2707: 
 2708: 	for (i = 0; i <= AF_MAX; i++)
 2709: 		if ((rnh = nep->ne_rtable[i])) {
 2710: 			(*rnh->rnh_walktree) (rnh, vfs_free_netcred,
 2711: 			    (caddr_t) rnh);
 2712: 			free((caddr_t) rnh, M_RTABLE);
 2713: 			nep->ne_rtable[i] = 0;
 2714: 		}
 2715: }
 2716: 
 2717: int
 2718: vfs_export(mp, nep, argp)
 2719: 	struct mount *mp;
 2720: 	struct netexport *nep;
 2721: 	struct export_args *argp;
 2722: {
 2723: 	int error;
 2724: 
 2725: 	if (argp->ex_flags & MNT_DELEXPORT) {
 2726: 		if (mp->mnt_flag & MNT_EXPUBLIC) {
 2727: 			vfs_setpublicfs(NULL, NULL, NULL);
 2728: 			mp->mnt_flag &= ~MNT_EXPUBLIC;
 2729: 		}
 2730: 		vfs_free_addrlist(nep);
 2731: 		mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
 2732: 	}
 2733: 	if (argp->ex_flags & MNT_EXPORTED) {
 2734: 		if (argp->ex_flags & MNT_EXPUBLIC) {
 2735: 			if ((error = vfs_setpublicfs(mp, nep, argp)) != 0)
 2736: 				return (error);
 2737: 			mp->mnt_flag |= MNT_EXPUBLIC;
 2738: 		}
 2739: 		if ((error = vfs_hang_addrlist(mp, nep, argp)))
 2740: 			return (error);
 2741: 		mp->mnt_flag |= MNT_EXPORTED;
 2742: 	}
 2743: 	return (0);
 2744: }
 2745: 
 2746: 
 2747: /*
 2748:  * Set the publicly exported filesystem (WebNFS). Currently, only
 2749:  * one public filesystem is possible in the spec (RFC 2054 and 2055)
 2750:  */
 2751: int
 2752: vfs_setpublicfs(mp, nep, argp)
 2753: 	struct mount *mp;
 2754: 	struct netexport *nep;
 2755: 	struct export_args *argp;
 2756: {
 2757: 	int error;
 2758: 	struct vnode *rvp;
 2759: 	char *cp;
 2760: 
 2761: 	/*
 2762: 	 * mp == NULL -> invalidate the current info, the FS is
 2763: 	 * no longer exported. May be called from either vfs_export
 2764: 	 * or unmount, so check if it hasn't already been done.
 2765: 	 */
 2766: 	if (mp == NULL) {
 2767: 		if (nfs_pub.np_valid) {
 2768: 			nfs_pub.np_valid = 0;
 2769: 			if (nfs_pub.np_index != NULL) {
 2770: 				FREE(nfs_pub.np_index, M_TEMP);
 2771: 				nfs_pub.np_index = NULL;
 2772: 			}
 2773: 		}
 2774: 		return (0);
 2775: 	}
 2776: 
 2777: 	/*
 2778: 	 * Only one allowed at a time.
 2779: 	 */
 2780: 	if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
 2781: 		return (EBUSY);
 2782: 
 2783: 	/*
 2784: 	 * Get real filehandle for root of exported FS.
 2785: 	 */
 2786: 	bzero((caddr_t)&nfs_pub.np_handle, sizeof(nfs_pub.np_handle));
 2787: 	nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid;
 2788: 
 2789: 	if ((error = VFS_ROOT(mp, &rvp)))
 2790: 		return (error);
 2791: 
 2792: 	if ((error = VFS_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid)))
 2793: 		return (error);
 2794: 
 2795: 	vput(rvp);
 2796: 
 2797: 	/*
 2798: 	 * If an indexfile was specified, pull it in.
 2799: 	 */
 2800: 	if (argp->ex_indexfile != NULL) {
 2801: 		MALLOC(nfs_pub.np_index, char *, MAXNAMLEN + 1, M_TEMP,
 2802: 		    M_WAITOK);
 2803: 		error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
 2804: 		    MAXNAMLEN, (size_t *)0);
 2805: 		if (!error) {
 2806: 			/*
 2807: 			 * Check for illegal filenames.
 2808: 			 */
 2809: 			for (cp = nfs_pub.np_index; *cp; cp++) {
 2810: 				if (*cp == '/') {
 2811: 					error = EINVAL;
 2812: 					break;
 2813: 				}
 2814: 			}
 2815: 		}
 2816: 		if (error) {
 2817: 			FREE(nfs_pub.np_index, M_TEMP);
 2818: 			return (error);
 2819: 		}
 2820: 	}
 2821: 
 2822: 	nfs_pub.np_mount = mp;
 2823: 	nfs_pub.np_valid = 1;
 2824: 	return (0);
 2825: }
 2826: 
 2827: struct netcred *
 2828: vfs_export_lookup(mp, nep, nam)
 2829: 	struct mount *mp;
 2830: 	struct netexport *nep;
 2831: 	struct sockaddr *nam;
 2832: {
 2833: 	struct netcred *np;
 2834: 	struct radix_node_head *rnh;
 2835: 	struct sockaddr *saddr;
 2836: 
 2837: 	np = NULL;
 2838: 	if (mp->mnt_flag & MNT_EXPORTED) {
 2839: 		/*
 2840: 		 * Lookup in the export list first.
 2841: 		 */
 2842: 		if (nam != NULL) {
 2843: 			saddr = nam;
 2844: 			rnh = nep->ne_rtable[saddr->sa_family];
 2845: 			if (rnh != NULL) {
 2846: 				np = (struct netcred *)
 2847: 					(*rnh->rnh_matchaddr)((caddr_t)saddr,
 2848: 							      rnh);
 2849: 				if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
 2850: 					np = NULL;
 2851: 			}
 2852: 		}
 2853: 		/*
 2854: 		 * If no address match, use the default if it exists.
 2855: 		 */
 2856: 		if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
 2857: 			np = &nep->ne_defexported;
 2858: 	}
 2859: 	return (np);
 2860: }
 2861: 
 2862: /*
 2863:  * perform msync on all vnodes under a mount point.  The mount point must
 2864:  * be locked.  This code is also responsible for lazy-freeing unreferenced
 2865:  * vnodes whos VM objects no longer contain pages.
 2866:  *
 2867:  * NOTE: MNT_WAIT still skips vnodes in the VXLOCK state.
 2868:  */
 2869: static int vfs_msync_scan1(struct mount *mp, struct vnode *vp, void *data);
 2870: static int vfs_msync_scan2(struct mount *mp, struct vnode *vp, 
 2871: 				lwkt_tokref_t vlock, void *data);
 2872: 
 2873: void
 2874: vfs_msync(struct mount *mp, int flags) 
 2875: {
 2876: 	vmntvnodescan(mp, vfs_msync_scan1, vfs_msync_scan2, (void *)flags);
 2877: }
 2878: 
 2879: /*
 2880:  * scan1 is a fast pre-check.  There could be hundreds of thousands of
 2881:  * vnodes, we cannot afford to do anything heavy weight until we have a
 2882:  * fairly good indication that there is work to do.
 2883:  */
 2884: static
 2885: int
 2886: vfs_msync_scan1(struct mount *mp, struct vnode *vp, void *data)
 2887: {
 2888: 	int flags = (int)data;
 2889: 
 2890: 	if ((vp->v_flag & VXLOCK) == 0) {
 2891: 		if (VSHOULDFREE(vp))
 2892: 			return(0);
 2893: 		if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
 2894: 		    (vp->v_flag & VOBJDIRTY) &&
 2895: 		    (flags == MNT_WAIT || VOP_ISLOCKED(vp, NULL) == 0)) {
 2896: 			return(0);
 2897: 		}
 2898: 	}
 2899: 	return(-1);
 2900: }
 2901: 
 2902: static
 2903: int
 2904: vfs_msync_scan2(struct mount *mp, struct vnode *vp, lwkt_tokref_t vlock, void *data)
 2905: {
 2906: 	vm_object_t obj;
 2907: 	int error;
 2908: 	int flags = (int)data;
 2909: 
 2910: 	if (vp->v_flag & VXLOCK)
 2911: 		return(0);
 2912: 
 2913: 	if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
 2914: 	    (vp->v_flag & VOBJDIRTY) &&
 2915: 	    (flags == MNT_WAIT || VOP_ISLOCKED(vp, NULL) == 0)) {
 2916: 		error = vget(vp, vlock, LK_EXCLUSIVE | LK_RETRY | LK_NOOBJ | LK_INTERLOCK, curthread);
 2917: 		if (error == 0) {
 2918: 			if (VOP_GETVOBJECT(vp, &obj) == 0) {
 2919: 				vm_object_page_clean(obj, 0, 0, 
 2920: 				 flags == MNT_WAIT ? OBJPC_SYNC : OBJPC_NOSYNC);
 2921: 			}
 2922: 			vput(vp);
 2923: 		}
 2924: 		return(0);
 2925: 	}
 2926: 	vmaybefree(vp);
 2927: 	lwkt_reltoken(vlock);
 2928: 	return(0);
 2929: }
 2930: 
 2931: /*
 2932:  * Create the VM object needed for VMIO and mmap support.  This
 2933:  * is done for all VREG files in the system.  Some filesystems might
 2934:  * afford the additional metadata buffering capability of the
 2935:  * VMIO code by making the device node be VMIO mode also.
 2936:  *
 2937:  * vp must be locked when vfs_object_create is called.
 2938:  */
 2939: int
 2940: vfs_object_create(struct vnode *vp, struct thread *td)
 2941: {
 2942: 	return (VOP_CREATEVOBJECT(vp, td));
 2943: }
 2944: 
 2945: /*
 2946:  * NOTE: the vnode interlock must be held during the call.  We have to recheck
 2947:  * the VFREE flag since the vnode may have been removed from the free list
 2948:  * while we were blocked on vnode_free_list_token.  The use or hold count
 2949:  * must have already been bumped by the caller.
 2950:  */
 2951: static void
 2952: vbusy(struct vnode *vp)
 2953: {
 2954: 	lwkt_tokref ilock;
 2955: 
 2956: 	lwkt_gettoken(&ilock, &vnode_free_list_token);
 2957: 	if ((vp->v_flag & VFREE) != 0) {
 2958: 	    TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
 2959: 	    freevnodes--;
 2960: 	    vp->v_flag &= ~(VFREE|VAGE);
 2961: 	}
 2962: 	lwkt_reltoken(&ilock);
 2963: }
 2964: 
 2965: /*
 2966:  * NOTE: the vnode interlock must be held during the call.  The use or hold
 2967:  * count must have already been bumped by the caller.  We use a VINFREE to
 2968:  * interlock against other calls to vfree() which might occur while we 
 2969:  * are blocked.  The vnode cannot be reused until it has actually been
 2970:  * placed on the free list, so there are no other races even though the
 2971:  * use and hold counts are 0.
 2972:  */
 2973: static void
 2974: vfree(struct vnode *vp)
 2975: {
 2976: 	lwkt_tokref ilock;
 2977: 
 2978: 	if ((vp->v_flag & VINFREE) == 0) {
 2979: 		vp->v_flag |= VINFREE;
 2980: 		lwkt_gettoken(&ilock, &vnode_free_list_token); /* can block */
 2981: 		KASSERT((vp->v_flag & VFREE) == 0, ("vnode already free"));
 2982: 		if (vp->v_flag & VAGE) {
 2983: 			TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
 2984: 		} else {
 2985: 			TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
 2986: 		}
 2987: 		freevnodes++;
 2988: 		vp->v_flag &= ~(VAGE|VINFREE);
 2989: 		vp->v_flag |= VFREE;
 2990: 		lwkt_reltoken(&ilock);	/* can block */
 2991: 	}
 2992: }
 2993: 
 2994: 
 2995: /*
 2996:  * Record a process's interest in events which might happen to
 2997:  * a vnode.  Because poll uses the historic select-style interface
 2998:  * internally, this routine serves as both the ``check for any
 2999:  * pending events'' and the ``record my interest in future events''
 3000:  * functions.  (These are done together, while the lock is held,
 3001:  * to avoid race conditions.)
 3002:  */
 3003: int
 3004: vn_pollrecord(struct vnode *vp, struct thread *td, int events)
 3005: {
 3006: 	lwkt_tokref ilock;
 3007: 
 3008: 	lwkt_gettoken(&ilock, &vp->v_pollinfo.vpi_token);
 3009: 	if (vp->v_pollinfo.vpi_revents & events) {
 3010: 		/*
 3011: 		 * This leaves events we are not interested
 3012: 		 * in available for the other process which
 3013: 		 * which presumably had requested them
 3014: 		 * (otherwise they would never have been
 3015: 		 * recorded).
 3016: 		 */
 3017: 		events &= vp->v_pollinfo.vpi_revents;
 3018: 		vp->v_pollinfo.vpi_revents &= ~events;
 3019: 
 3020: 		lwkt_reltoken(&ilock);
 3021: 		return events;
 3022: 	}
 3023: 	vp->v_pollinfo.vpi_events |= events;
 3024: 	selrecord(td, &vp->v_pollinfo.vpi_selinfo);
 3025: 	lwkt_reltoken(&ilock);
 3026: 	return 0;
 3027: }
 3028: 
 3029: /*
 3030:  * Note the occurrence of an event.  If the VN_POLLEVENT macro is used,
 3031:  * it is possible for us to miss an event due to race conditions, but
 3032:  * that condition is expected to be rare, so for the moment it is the
 3033:  * preferred interface.
 3034:  */
 3035: void
 3036: vn_pollevent(vp, events)
 3037: 	struct vnode *vp;
 3038: 	short events;
 3039: {
 3040: 	lwkt_tokref ilock;
 3041: 
 3042: 	lwkt_gettoken(&ilock, &vp->v_pollinfo.vpi_token);
 3043: 	if (vp->v_pollinfo.vpi_events & events) {
 3044: 		/*
 3045: 		 * We clear vpi_events so that we don't
 3046: 		 * call selwakeup() twice if two events are
 3047: 		 * posted before the polling process(es) is
 3048: 		 * awakened.  This also ensures that we take at
 3049: 		 * most one selwakeup() if the polling process
 3050: 		 * is no longer interested.  However, it does
 3051: 		 * mean that only one event can be noticed at
 3052: 		 * a time.  (Perhaps we should only clear those
 3053: 		 * event bits which we note?) XXX
 3054: 		 */
 3055: 		vp->v_pollinfo.vpi_events = 0;	/* &= ~events ??? */
 3056: 		vp->v_pollinfo.vpi_revents |= events;
 3057: 		selwakeup(&vp->v_pollinfo.vpi_selinfo);
 3058: 	}
 3059: 	lwkt_reltoken(&ilock);
 3060: }
 3061: 
 3062: /*
 3063:  * Wake up anyone polling on vp because it is being revoked.
 3064:  * This depends on dead_poll() returning POLLHUP for correct
 3065:  * behavior.
 3066:  */
 3067: void
 3068: vn_pollgone(vp)
 3069: 	struct vnode *vp;
 3070: {
 3071: 	lwkt_tokref ilock;
 3072: 
 3073: 	lwkt_gettoken(&ilock, &vp->v_pollinfo.vpi_token);
 3074: 	if (vp->v_pollinfo.vpi_events) {
 3075: 		vp->v_pollinfo.vpi_events = 0;
 3076: 		selwakeup(&vp->v_pollinfo.vpi_selinfo);
 3077: 	}
 3078: 	lwkt_reltoken(&ilock);
 3079: }
 3080: 
 3081: 
 3082: 
 3083: /*
 3084:  * Routine to create and manage a filesystem syncer vnode.
 3085:  */
 3086: #define sync_close ((int (*) (struct  vop_close_args *))nullop)
 3087: static int	sync_fsync (struct  vop_fsync_args *);
 3088: static int	sync_inactive (struct  vop_inactive_args *);
 3089: static int	sync_reclaim  (struct  vop_reclaim_args *);
 3090: #define sync_lock ((int (*) (struct  vop_lock_args *))vop_nolock)
 3091: #define sync_unlock ((int (*) (struct  vop_unlock_args *))vop_nounlock)
 3092: static int	sync_print (struct vop_print_args *);
 3093: #define sync_islocked ((int(*) (struct vop_islocked_args *))vop_noislocked)
 3094: 
 3095: static vop_t **sync_vnodeop_p;
 3096: static struct vnodeopv_entry_desc sync_vnodeop_entries[] = {
 3097: 	{ &vop_default_desc,	(vop_t *) vop_eopnotsupp },
 3098: 	{ &vop_close_desc,	(vop_t *) sync_close },		/* close */
 3099: 	{ &vop_fsync_desc,	(vop_t *) sync_fsync },		/* fsync */
 3100: 	{ &vop_inactive_desc,	(vop_t *) sync_inactive },	/* inactive */
 3101: 	{ &vop_reclaim_desc,	(vop_t *) sync_reclaim },	/* reclaim */
 3102: 	{ &vop_lock_desc,	(vop_t *) sync_lock },		/* lock */
 3103: 	{ &vop_unlock_desc,	(vop_t *) sync_unlock },	/* unlock */
 3104: 	{ &vop_print_desc,	(vop_t *) sync_print },		/* print */
 3105: 	{ &vop_islocked_desc,	(vop_t *) sync_islocked },	/* islocked */
 3106: 	{ NULL, NULL }
 3107: };
 3108: static struct vnodeopv_desc sync_vnodeop_opv_desc =
 3109: 	{ &sync_vnodeop_p, sync_vnodeop_entries };
 3110: 
 3111: VNODEOP_SET(sync_vnodeop_opv_desc);
 3112: 
 3113: /*
 3114:  * Create a new filesystem syncer vnode for the specified mount point.
 3115:  * This vnode is placed on the worklist and is responsible for sync'ing
 3116:  * the filesystem.
 3117:  *
 3118:  * NOTE: read-only mounts are also placed on the worklist.  The filesystem
 3119:  * sync code is also responsible for cleaning up vnodes.
 3120:  */
 3121: int
 3122: vfs_allocate_syncvnode(struct mount *mp)
 3123: {
 3124: 	struct vnode *vp;
 3125: 	static long start, incr, next;
 3126: 	int error;
 3127: 
 3128: 	/* Allocate a new vnode */
 3129: 	if ((error = getnewvnode(VT_VFS, mp, sync_vnodeop_p, &vp)) != 0) {
 3130: 		mp->mnt_syncer = NULL;
 3131: 		return (error);
 3132: 	}
 3133: 	vp->v_type = VNON;
 3134: 	/*
 3135: 	 * Place the vnode onto the syncer worklist. We attempt to
 3136: 	 * scatter them about on the list so that they will go off
 3137: 	 * at evenly distributed times even if all the filesystems
 3138: 	 * are mounted at once.
 3139: 	 */
 3140: 	next += incr;
 3141: 	if (next == 0 || next > syncer_maxdelay) {
 3142: 		start /= 2;
 3143: 		incr /= 2;
 3144: 		if (start == 0) {
 3145: 			start = syncer_maxdelay / 2;
 3146: 			incr = syncer_maxdelay;
 3147: 		}
 3148: 		next = start;
 3149: 	}
 3150: 	vn_syncer_add_to_worklist(vp, syncdelay > 0 ? next % syncdelay : 0);
 3151: 	mp->mnt_syncer = vp;
 3152: 	return (0);
 3153: }
 3154: 
 3155: /*
 3156:  * Do a lazy sync of the filesystem.
 3157:  */
 3158: static int
 3159: sync_fsync(ap)
 3160: 	struct vop_fsync_args /* {
 3161: 		struct vnode *a_vp;
 3162: 		struct ucred *a_cred;
 3163: 		int a_waitfor;
 3164: 		struct thread *a_td;
 3165: 	} */ *ap;
 3166: {
 3167: 	struct vnode *syncvp = ap->a_vp;
 3168: 	struct mount *mp = syncvp->v_mount;
 3169: 	struct thread *td = ap->a_td;
 3170: 	lwkt_tokref ilock;
 3171: 	int asyncflag;
 3172: 
 3173: 	/*
 3174: 	 * We only need to do something if this is a lazy evaluation.
 3175: 	 */
 3176: 	if (ap->a_waitfor != MNT_LAZY)
 3177: 		return (0);
 3178: 
 3179: 	/*
 3180: 	 * Move ourselves to the back of the sync list.
 3181: 	 */
 3182: 	vn_syncer_add_to_worklist(syncvp, syncdelay);
 3183: 
 3184: 	/*
 3185: 	 * Walk the list of vnodes pushing all that are dirty and
 3186: 	 * not already on the sync list, and freeing vnodes which have
 3187: 	 * no refs and whos VM objects are empty.  vfs_msync() handles
 3188: 	 * the VM issues and must be called whether the mount is readonly
 3189: 	 * or not.
 3190: 	 */
 3191: 	lwkt_gettoken(&ilock, &mountlist_token);
 3192: 	if (vfs_busy(mp, LK_EXCLUSIVE | LK_NOWAIT, &ilock, td) != 0) {
 3193: 		lwkt_reltoken(&ilock);
 3194: 		return (0);
 3195: 	}
 3196: 	if (mp->mnt_flag & MNT_RDONLY) {
 3197: 		vfs_msync(mp, MNT_NOWAIT);
 3198: 	} else {
 3199: 		asyncflag = mp->mnt_flag & MNT_ASYNC;
 3200: 		mp->mnt_flag &= ~MNT_ASYNC;	/* ZZZ hack */
 3201: 		vfs_msync(mp, MNT_NOWAIT);
 3202: 		VFS_SYNC(mp, MNT_LAZY, td);
 3203: 		if (asyncflag)
 3204: 			mp->mnt_flag |= MNT_ASYNC;
 3205: 	}
 3206: 	vfs_unbusy(mp, td);
 3207: 	return (0);
 3208: }
 3209: 
 3210: /*
 3211:  * The syncer vnode is no referenced.
 3212:  */
 3213: static int
 3214: sync_inactive(ap)
 3215: 	struct vop_inactive_args /* {
 3216: 		struct vnode *a_vp;
 3217: 		struct proc *a_p;
 3218: 	} */ *ap;
 3219: {
 3220: 
 3221: 	vgone(ap->a_vp);
 3222: 	return (0);
 3223: }
 3224: 
 3225: /*
 3226:  * The syncer vnode is no longer needed and is being decommissioned.
 3227:  *
 3228:  * Modifications to the worklist must be protected at splbio().
 3229:  */
 3230: static int
 3231: sync_reclaim(ap)
 3232: 	struct vop_reclaim_args /* {
 3233: 		struct vnode *a_vp;
 3234: 	} */ *ap;
 3235: {
 3236: 	struct vnode *vp = ap->a_vp;
 3237: 	int s;
 3238: 
 3239: 	s = splbio();
 3240: 	vp->v_mount->mnt_syncer = NULL;
 3241: 	if (vp->v_flag & VONWORKLST) {
 3242: 		LIST_REMOVE(vp, v_synclist);
 3243: 		vp->v_flag &= ~VONWORKLST;
 3244: 	}
 3245: 	splx(s);
 3246: 
 3247: 	return (0);
 3248: }
 3249: 
 3250: /*
 3251:  * Print out a syncer vnode.
 3252:  */
 3253: static int
 3254: sync_print(ap)
 3255: 	struct vop_print_args /* {
 3256: 		struct vnode *a_vp;
 3257: 	} */ *ap;
 3258: {
 3259: 	struct vnode *vp = ap->a_vp;
 3260: 
 3261: 	printf("syncer vnode");
 3262: 	if (vp->v_vnlock != NULL)
 3263: 		lockmgr_printinfo(vp->v_vnlock);
 3264: 	printf("\n");
 3265: 	return (0);
 3266: }
 3267: 
 3268: /*
 3269:  * extract the dev_t from a VBLK or VCHR
 3270:  */
 3271: dev_t
 3272: vn_todev(vp)
 3273: 	struct vnode *vp;
 3274: {
 3275: 	if (vp->v_type != VBLK && vp->v_type != VCHR)
 3276: 		return (NODEV);
 3277: 	return (vp->v_rdev);
 3278: }
 3279: 
 3280: /*
 3281:  * Check if vnode represents a disk device
 3282:  */
 3283: int
 3284: vn_isdisk(vp, errp)
 3285: 	struct vnode *vp;
 3286: 	int *errp;
 3287: {
 3288: 	if (vp->v_type != VBLK && vp->v_type != VCHR) {
 3289: 		if (errp != NULL)
 3290: 			*errp = ENOTBLK;
 3291: 		return (0);
 3292: 	}
 3293: 	if (vp->v_rdev == NULL) {
 3294: 		if (errp != NULL)
 3295: 			*errp = ENXIO;
 3296: 		return (0);
 3297: 	}
 3298: 	if (!dev_dport(vp->v_rdev)) {
 3299: 		if (errp != NULL)
 3300: 			*errp = ENXIO;
 3301: 		return (0);
 3302: 	}
 3303: 	if (!(dev_dflags(vp->v_rdev) & D_DISK)) {
 3304: 		if (errp != NULL)
 3305: 			*errp = ENOTBLK;
 3306: 		return (0);
 3307: 	}
 3308: 	if (errp != NULL)
 3309: 		*errp = 0;
 3310: 	return (1);
 3311: }
 3312: 
 3313: void
 3314: NDFREE(ndp, flags)
 3315:      struct nameidata *ndp;
 3316:      const uint flags;
 3317: {
 3318: 	if (!(flags & NDF_NO_FREE_PNBUF) &&
 3319: 	    (ndp->ni_cnd.cn_flags & CNP_HASBUF)) {
 3320: 		zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
 3321: 		ndp->ni_cnd.cn_flags &= ~CNP_HASBUF;
 3322: 	}
 3323: 	if (!(flags & NDF_NO_DNCP_RELE) &&
 3324: 	    (ndp->ni_cnd.cn_flags & CNP_WANTDNCP) &&
 3325: 	    ndp->ni_dncp) {
 3326: 		cache_drop(ndp->ni_dncp);
 3327: 		ndp->ni_dncp = NULL;
 3328: 	}
 3329: 	if (!(flags & NDF_NO_NCP_RELE) &&
 3330: 	    (ndp->ni_cnd.cn_flags & CNP_WANTNCP) &&
 3331: 	    ndp->ni_ncp) {
 3332: 		cache_drop(ndp->ni_ncp);
 3333: 		ndp->ni_ncp = NULL;
 3334: 	}
 3335: 	if (!(flags & NDF_NO_DVP_UNLOCK) &&
 3336: 	    (ndp->ni_cnd.cn_flags & CNP_LOCKPARENT) &&
 3337: 	    ndp->ni_dvp != ndp->ni_vp) {
 3338: 		VOP_UNLOCK(ndp->ni_dvp, NULL, 0, ndp->ni_cnd.cn_td);
 3339: 	}
 3340: 	if (!(flags & NDF_NO_DVP_RELE) &&
 3341: 	    (ndp->ni_cnd.cn_flags & (CNP_LOCKPARENT|CNP_WANTPARENT))) {
 3342: 		vrele(ndp->ni_dvp);
 3343: 		ndp->ni_dvp = NULL;
 3344: 	}
 3345: 	if (!(flags & NDF_NO_VP_UNLOCK) &&
 3346: 	    (ndp->ni_cnd.cn_flags & CNP_LOCKLEAF) && ndp->ni_vp) {
 3347: 		VOP_UNLOCK(ndp->ni_vp, NULL, 0, ndp->ni_cnd.cn_td);
 3348: 	}
 3349: 	if (!(flags & NDF_NO_VP_RELE) &&
 3350: 	    ndp->ni_vp) {
 3351: 		vrele(ndp->ni_vp);
 3352: 		ndp->ni_vp = NULL;
 3353: 	}
 3354: 	if (!(flags & NDF_NO_STARTDIR_RELE) &&
 3355: 	    (ndp->ni_cnd.cn_flags & CNP_SAVESTART)) {
 3356: 		vrele(ndp->ni_startdir);
 3357: 		ndp->ni_startdir = NULL;
 3358: 	}
 3359: }
 3360: 
 3361: #ifdef DEBUG_VFS_LOCKS
 3362: 
 3363: void
 3364: assert_vop_locked(struct vnode *vp, const char *str)
 3365: {
 3366: 
 3367: 	if (vp && IS_LOCKING_VFS(vp) && !VOP_ISLOCKED(vp, NULL)) {
 3368: 		panic("%s: %p is not locked shared but should be", str, vp);
 3369: 	}
 3370: }
 3371: 
 3372: void
 3373: assert_vop_unlocked(struct vnode *vp, const char *str)
 3374: {
 3375: 
 3376: 	if (vp && IS_LOCKING_VFS(vp)) {
 3377: 		if (VOP_ISLOCKED(vp, curthread) == LK_EXCLUSIVE) {
 3378: 			panic("%s: %p is locked but should not be", str, vp);
 3379: 		}
 3380: 	}
 3381: }
 3382: 
 3383: #endif