xref: /dflybsd-src/sys/kern/vfs_subr.c (revision c5541aee854b0d32586182b733a9ea4d4c92168b)
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.28 2004/03/28 07:54:00 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 		vp->v_dd = vp;
817 		cache_purge(vp);
818 		TAILQ_INIT(&vp->v_namecache);
819 		numvnodes++;
820 	}
821 
822 	TAILQ_INIT(&vp->v_cleanblkhd);
823 	TAILQ_INIT(&vp->v_dirtyblkhd);
824 	vp->v_type = VNON;
825 	vp->v_tag = tag;
826 	vp->v_op = vops;
827 	insmntque(vp, mp);
828 	*vpp = vp;
829 	vp->v_usecount = 1;
830 	vp->v_data = 0;
831 	splx(s);
832 
833 	vfs_object_create(vp, td);
834 	return (0);
835 }
836 
837 /*
838  * Move a vnode from one mount queue to another.
839  */
840 static void
841 insmntque(vp, mp)
842 	struct vnode *vp;
843 	struct mount *mp;
844 {
845 	lwkt_tokref ilock;
846 
847 	lwkt_gettoken(&ilock, &mntvnode_token);
848 	/*
849 	 * Delete from old mount point vnode list, if on one.
850 	 */
851 	if (vp->v_mount != NULL) {
852 		KASSERT(vp->v_mount->mnt_nvnodelistsize > 0,
853 			("bad mount point vnode list size"));
854 		TAILQ_REMOVE(&vp->v_mount->mnt_nvnodelist, vp, v_nmntvnodes);
855 		vp->v_mount->mnt_nvnodelistsize--;
856 	}
857 	/*
858 	 * Insert into list of vnodes for the new mount point, if available.
859 	 */
860 	if ((vp->v_mount = mp) == NULL) {
861 		lwkt_reltoken(&ilock);
862 		return;
863 	}
864 	TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
865 	mp->mnt_nvnodelistsize++;
866 	lwkt_reltoken(&ilock);
867 }
868 
869 /*
870  * Update outstanding I/O count and do wakeup if requested.
871  */
872 void
873 vwakeup(bp)
874 	struct buf *bp;
875 {
876 	struct vnode *vp;
877 
878 	bp->b_flags &= ~B_WRITEINPROG;
879 	if ((vp = bp->b_vp)) {
880 		vp->v_numoutput--;
881 		if (vp->v_numoutput < 0)
882 			panic("vwakeup: neg numoutput");
883 		if ((vp->v_numoutput == 0) && (vp->v_flag & VBWAIT)) {
884 			vp->v_flag &= ~VBWAIT;
885 			wakeup((caddr_t) &vp->v_numoutput);
886 		}
887 	}
888 }
889 
890 /*
891  * Flush out and invalidate all buffers associated with a vnode.
892  * Called with the underlying object locked.
893  */
894 int
895 vinvalbuf(struct vnode *vp, int flags, struct thread *td,
896 	int slpflag, int slptimeo)
897 {
898 	struct buf *bp;
899 	struct buf *nbp, *blist;
900 	int s, error;
901 	vm_object_t object;
902 	lwkt_tokref vlock;
903 
904 	if (flags & V_SAVE) {
905 		s = splbio();
906 		while (vp->v_numoutput) {
907 			vp->v_flag |= VBWAIT;
908 			error = tsleep((caddr_t)&vp->v_numoutput,
909 			    slpflag, "vinvlbuf", slptimeo);
910 			if (error) {
911 				splx(s);
912 				return (error);
913 			}
914 		}
915 		if (!TAILQ_EMPTY(&vp->v_dirtyblkhd)) {
916 			splx(s);
917 			if ((error = VOP_FSYNC(vp, MNT_WAIT, td)) != 0)
918 				return (error);
919 			s = splbio();
920 			if (vp->v_numoutput > 0 ||
921 			    !TAILQ_EMPTY(&vp->v_dirtyblkhd))
922 				panic("vinvalbuf: dirty bufs");
923 		}
924 		splx(s);
925   	}
926 	s = splbio();
927 	for (;;) {
928 		blist = TAILQ_FIRST(&vp->v_cleanblkhd);
929 		if (!blist)
930 			blist = TAILQ_FIRST(&vp->v_dirtyblkhd);
931 		if (!blist)
932 			break;
933 
934 		for (bp = blist; bp; bp = nbp) {
935 			nbp = TAILQ_NEXT(bp, b_vnbufs);
936 			if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
937 				error = BUF_TIMELOCK(bp,
938 				    LK_EXCLUSIVE | LK_SLEEPFAIL,
939 				    "vinvalbuf", slpflag, slptimeo);
940 				if (error == ENOLCK)
941 					break;
942 				splx(s);
943 				return (error);
944 			}
945 			/*
946 			 * XXX Since there are no node locks for NFS, I
947 			 * believe there is a slight chance that a delayed
948 			 * write will occur while sleeping just above, so
949 			 * check for it.  Note that vfs_bio_awrite expects
950 			 * buffers to reside on a queue, while VOP_BWRITE and
951 			 * brelse do not.
952 			 */
953 			if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
954 				(flags & V_SAVE)) {
955 
956 				if (bp->b_vp == vp) {
957 					if (bp->b_flags & B_CLUSTEROK) {
958 						BUF_UNLOCK(bp);
959 						vfs_bio_awrite(bp);
960 					} else {
961 						bremfree(bp);
962 						bp->b_flags |= B_ASYNC;
963 						VOP_BWRITE(bp->b_vp, bp);
964 					}
965 				} else {
966 					bremfree(bp);
967 					(void) VOP_BWRITE(bp->b_vp, bp);
968 				}
969 				break;
970 			}
971 			bremfree(bp);
972 			bp->b_flags |= (B_INVAL | B_NOCACHE | B_RELBUF);
973 			bp->b_flags &= ~B_ASYNC;
974 			brelse(bp);
975 		}
976 	}
977 
978 	/*
979 	 * Wait for I/O to complete.  XXX needs cleaning up.  The vnode can
980 	 * have write I/O in-progress but if there is a VM object then the
981 	 * VM object can also have read-I/O in-progress.
982 	 */
983 	do {
984 		while (vp->v_numoutput > 0) {
985 			vp->v_flag |= VBWAIT;
986 			tsleep(&vp->v_numoutput, 0, "vnvlbv", 0);
987 		}
988 		if (VOP_GETVOBJECT(vp, &object) == 0) {
989 			while (object->paging_in_progress)
990 				vm_object_pip_sleep(object, "vnvlbx");
991 		}
992 	} while (vp->v_numoutput > 0);
993 
994 	splx(s);
995 
996 	/*
997 	 * Destroy the copy in the VM cache, too.
998 	 */
999 	lwkt_gettoken(&vlock, vp->v_interlock);
1000 	if (VOP_GETVOBJECT(vp, &object) == 0) {
1001 		vm_object_page_remove(object, 0, 0,
1002 			(flags & V_SAVE) ? TRUE : FALSE);
1003 	}
1004 	lwkt_reltoken(&vlock);
1005 
1006 	if (!TAILQ_EMPTY(&vp->v_dirtyblkhd) || !TAILQ_EMPTY(&vp->v_cleanblkhd))
1007 		panic("vinvalbuf: flush failed");
1008 	return (0);
1009 }
1010 
1011 /*
1012  * Truncate a file's buffer and pages to a specified length.  This
1013  * is in lieu of the old vinvalbuf mechanism, which performed unneeded
1014  * sync activity.
1015  */
1016 int
1017 vtruncbuf(struct vnode *vp, struct thread *td, off_t length, int blksize)
1018 {
1019 	struct buf *bp;
1020 	struct buf *nbp;
1021 	int s, anyfreed;
1022 	int trunclbn;
1023 
1024 	/*
1025 	 * Round up to the *next* lbn.
1026 	 */
1027 	trunclbn = (length + blksize - 1) / blksize;
1028 
1029 	s = splbio();
1030 restart:
1031 	anyfreed = 1;
1032 	for (;anyfreed;) {
1033 		anyfreed = 0;
1034 		for (bp = TAILQ_FIRST(&vp->v_cleanblkhd); bp; bp = nbp) {
1035 			nbp = TAILQ_NEXT(bp, b_vnbufs);
1036 			if (bp->b_lblkno >= trunclbn) {
1037 				if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
1038 					BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL);
1039 					goto restart;
1040 				} else {
1041 					bremfree(bp);
1042 					bp->b_flags |= (B_INVAL | B_RELBUF);
1043 					bp->b_flags &= ~B_ASYNC;
1044 					brelse(bp);
1045 					anyfreed = 1;
1046 				}
1047 				if (nbp &&
1048 				    (((nbp->b_xflags & BX_VNCLEAN) == 0) ||
1049 				    (nbp->b_vp != vp) ||
1050 				    (nbp->b_flags & B_DELWRI))) {
1051 					goto restart;
1052 				}
1053 			}
1054 		}
1055 
1056 		for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
1057 			nbp = TAILQ_NEXT(bp, b_vnbufs);
1058 			if (bp->b_lblkno >= trunclbn) {
1059 				if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
1060 					BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL);
1061 					goto restart;
1062 				} else {
1063 					bremfree(bp);
1064 					bp->b_flags |= (B_INVAL | B_RELBUF);
1065 					bp->b_flags &= ~B_ASYNC;
1066 					brelse(bp);
1067 					anyfreed = 1;
1068 				}
1069 				if (nbp &&
1070 				    (((nbp->b_xflags & BX_VNDIRTY) == 0) ||
1071 				    (nbp->b_vp != vp) ||
1072 				    (nbp->b_flags & B_DELWRI) == 0)) {
1073 					goto restart;
1074 				}
1075 			}
1076 		}
1077 	}
1078 
1079 	if (length > 0) {
1080 restartsync:
1081 		for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
1082 			nbp = TAILQ_NEXT(bp, b_vnbufs);
1083 			if ((bp->b_flags & B_DELWRI) && (bp->b_lblkno < 0)) {
1084 				if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
1085 					BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL);
1086 					goto restart;
1087 				} else {
1088 					bremfree(bp);
1089 					if (bp->b_vp == vp) {
1090 						bp->b_flags |= B_ASYNC;
1091 					} else {
1092 						bp->b_flags &= ~B_ASYNC;
1093 					}
1094 					VOP_BWRITE(bp->b_vp, bp);
1095 				}
1096 				goto restartsync;
1097 			}
1098 
1099 		}
1100 	}
1101 
1102 	while (vp->v_numoutput > 0) {
1103 		vp->v_flag |= VBWAIT;
1104 		tsleep(&vp->v_numoutput, 0, "vbtrunc", 0);
1105 	}
1106 
1107 	splx(s);
1108 
1109 	vnode_pager_setsize(vp, length);
1110 
1111 	return (0);
1112 }
1113 
1114 /*
1115  * Associate a buffer with a vnode.
1116  */
1117 void
1118 bgetvp(vp, bp)
1119 	struct vnode *vp;
1120 	struct buf *bp;
1121 {
1122 	int s;
1123 
1124 	KASSERT(bp->b_vp == NULL, ("bgetvp: not free"));
1125 
1126 	vhold(vp);
1127 	bp->b_vp = vp;
1128 	bp->b_dev = vn_todev(vp);
1129 	/*
1130 	 * Insert onto list for new vnode.
1131 	 */
1132 	s = splbio();
1133 	bp->b_xflags |= BX_VNCLEAN;
1134 	bp->b_xflags &= ~BX_VNDIRTY;
1135 	TAILQ_INSERT_TAIL(&vp->v_cleanblkhd, bp, b_vnbufs);
1136 	splx(s);
1137 }
1138 
1139 /*
1140  * Disassociate a buffer from a vnode.
1141  */
1142 void
1143 brelvp(bp)
1144 	struct buf *bp;
1145 {
1146 	struct vnode *vp;
1147 	struct buflists *listheadp;
1148 	int s;
1149 
1150 	KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
1151 
1152 	/*
1153 	 * Delete from old vnode list, if on one.
1154 	 */
1155 	vp = bp->b_vp;
1156 	s = splbio();
1157 	if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) {
1158 		if (bp->b_xflags & BX_VNDIRTY)
1159 			listheadp = &vp->v_dirtyblkhd;
1160 		else
1161 			listheadp = &vp->v_cleanblkhd;
1162 		TAILQ_REMOVE(listheadp, bp, b_vnbufs);
1163 		bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN);
1164 	}
1165 	if ((vp->v_flag & VONWORKLST) && TAILQ_EMPTY(&vp->v_dirtyblkhd)) {
1166 		vp->v_flag &= ~VONWORKLST;
1167 		LIST_REMOVE(vp, v_synclist);
1168 	}
1169 	splx(s);
1170 	bp->b_vp = (struct vnode *) 0;
1171 	vdrop(vp);
1172 }
1173 
1174 /*
1175  * The workitem queue.
1176  *
1177  * It is useful to delay writes of file data and filesystem metadata
1178  * for tens of seconds so that quickly created and deleted files need
1179  * not waste disk bandwidth being created and removed. To realize this,
1180  * we append vnodes to a "workitem" queue. When running with a soft
1181  * updates implementation, most pending metadata dependencies should
1182  * not wait for more than a few seconds. Thus, mounted on block devices
1183  * are delayed only about a half the time that file data is delayed.
1184  * Similarly, directory updates are more critical, so are only delayed
1185  * about a third the time that file data is delayed. Thus, there are
1186  * SYNCER_MAXDELAY queues that are processed round-robin at a rate of
1187  * one each second (driven off the filesystem syncer process). The
1188  * syncer_delayno variable indicates the next queue that is to be processed.
1189  * Items that need to be processed soon are placed in this queue:
1190  *
1191  *	syncer_workitem_pending[syncer_delayno]
1192  *
1193  * A delay of fifteen seconds is done by placing the request fifteen
1194  * entries later in the queue:
1195  *
1196  *	syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask]
1197  *
1198  */
1199 
1200 /*
1201  * Add an item to the syncer work queue.
1202  */
1203 static void
1204 vn_syncer_add_to_worklist(struct vnode *vp, int delay)
1205 {
1206 	int s, slot;
1207 
1208 	s = splbio();
1209 
1210 	if (vp->v_flag & VONWORKLST) {
1211 		LIST_REMOVE(vp, v_synclist);
1212 	}
1213 
1214 	if (delay > syncer_maxdelay - 2)
1215 		delay = syncer_maxdelay - 2;
1216 	slot = (syncer_delayno + delay) & syncer_mask;
1217 
1218 	LIST_INSERT_HEAD(&syncer_workitem_pending[slot], vp, v_synclist);
1219 	vp->v_flag |= VONWORKLST;
1220 	splx(s);
1221 }
1222 
1223 struct  thread *updatethread;
1224 static void sched_sync (void);
1225 static struct kproc_desc up_kp = {
1226 	"syncer",
1227 	sched_sync,
1228 	&updatethread
1229 };
1230 SYSINIT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp)
1231 
1232 /*
1233  * System filesystem synchronizer daemon.
1234  */
1235 void
1236 sched_sync(void)
1237 {
1238 	struct synclist *slp;
1239 	struct vnode *vp;
1240 	long starttime;
1241 	int s;
1242 	struct thread *td = curthread;
1243 
1244 	EVENTHANDLER_REGISTER(shutdown_pre_sync, shutdown_kproc, td,
1245 	    SHUTDOWN_PRI_LAST);
1246 
1247 	for (;;) {
1248 		kproc_suspend_loop();
1249 
1250 		starttime = time_second;
1251 
1252 		/*
1253 		 * Push files whose dirty time has expired.  Be careful
1254 		 * of interrupt race on slp queue.
1255 		 */
1256 		s = splbio();
1257 		slp = &syncer_workitem_pending[syncer_delayno];
1258 		syncer_delayno += 1;
1259 		if (syncer_delayno == syncer_maxdelay)
1260 			syncer_delayno = 0;
1261 		splx(s);
1262 
1263 		while ((vp = LIST_FIRST(slp)) != NULL) {
1264 			if (VOP_ISLOCKED(vp, NULL) == 0) {
1265 				vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
1266 				(void) VOP_FSYNC(vp, MNT_LAZY, td);
1267 				VOP_UNLOCK(vp, NULL, 0, td);
1268 			}
1269 			s = splbio();
1270 			if (LIST_FIRST(slp) == vp) {
1271 				/*
1272 				 * Note: v_tag VT_VFS vps can remain on the
1273 				 * worklist too with no dirty blocks, but
1274 				 * since sync_fsync() moves it to a different
1275 				 * slot we are safe.
1276 				 */
1277 				if (TAILQ_EMPTY(&vp->v_dirtyblkhd) &&
1278 				    !vn_isdisk(vp, NULL))
1279 					panic("sched_sync: fsync failed vp %p tag %d", vp, vp->v_tag);
1280 				/*
1281 				 * Put us back on the worklist.  The worklist
1282 				 * routine will remove us from our current
1283 				 * position and then add us back in at a later
1284 				 * position.
1285 				 */
1286 				vn_syncer_add_to_worklist(vp, syncdelay);
1287 			}
1288 			splx(s);
1289 		}
1290 
1291 		/*
1292 		 * Do soft update processing.
1293 		 */
1294 		if (bioops.io_sync)
1295 			(*bioops.io_sync)(NULL);
1296 
1297 		/*
1298 		 * The variable rushjob allows the kernel to speed up the
1299 		 * processing of the filesystem syncer process. A rushjob
1300 		 * value of N tells the filesystem syncer to process the next
1301 		 * N seconds worth of work on its queue ASAP. Currently rushjob
1302 		 * is used by the soft update code to speed up the filesystem
1303 		 * syncer process when the incore state is getting so far
1304 		 * ahead of the disk that the kernel memory pool is being
1305 		 * threatened with exhaustion.
1306 		 */
1307 		if (rushjob > 0) {
1308 			rushjob -= 1;
1309 			continue;
1310 		}
1311 		/*
1312 		 * If it has taken us less than a second to process the
1313 		 * current work, then wait. Otherwise start right over
1314 		 * again. We can still lose time if any single round
1315 		 * takes more than two seconds, but it does not really
1316 		 * matter as we are just trying to generally pace the
1317 		 * filesystem activity.
1318 		 */
1319 		if (time_second == starttime)
1320 			tsleep(&lbolt, 0, "syncer", 0);
1321 	}
1322 }
1323 
1324 /*
1325  * Request the syncer daemon to speed up its work.
1326  * We never push it to speed up more than half of its
1327  * normal turn time, otherwise it could take over the cpu.
1328  *
1329  * YYY wchan field protected by the BGL.
1330  */
1331 int
1332 speedup_syncer()
1333 {
1334 	crit_enter();
1335 	if (updatethread->td_wchan == &lbolt) { /* YYY */
1336 		unsleep(updatethread);
1337 		lwkt_schedule(updatethread);
1338 	}
1339 	crit_exit();
1340 	if (rushjob < syncdelay / 2) {
1341 		rushjob += 1;
1342 		stat_rush_requests += 1;
1343 		return (1);
1344 	}
1345 	return(0);
1346 }
1347 
1348 /*
1349  * Associate a p-buffer with a vnode.
1350  *
1351  * Also sets B_PAGING flag to indicate that vnode is not fully associated
1352  * with the buffer.  i.e. the bp has not been linked into the vnode or
1353  * ref-counted.
1354  */
1355 void
1356 pbgetvp(vp, bp)
1357 	struct vnode *vp;
1358 	struct buf *bp;
1359 {
1360 
1361 	KASSERT(bp->b_vp == NULL, ("pbgetvp: not free"));
1362 
1363 	bp->b_vp = vp;
1364 	bp->b_flags |= B_PAGING;
1365 	bp->b_dev = vn_todev(vp);
1366 }
1367 
1368 /*
1369  * Disassociate a p-buffer from a vnode.
1370  */
1371 void
1372 pbrelvp(bp)
1373 	struct buf *bp;
1374 {
1375 
1376 	KASSERT(bp->b_vp != NULL, ("pbrelvp: NULL"));
1377 
1378 	/* XXX REMOVE ME */
1379 	if (TAILQ_NEXT(bp, b_vnbufs) != NULL) {
1380 		panic(
1381 		    "relpbuf(): b_vp was probably reassignbuf()d %p %x",
1382 		    bp,
1383 		    (int)bp->b_flags
1384 		);
1385 	}
1386 	bp->b_vp = (struct vnode *) 0;
1387 	bp->b_flags &= ~B_PAGING;
1388 }
1389 
1390 void
1391 pbreassignbuf(bp, newvp)
1392 	struct buf *bp;
1393 	struct vnode *newvp;
1394 {
1395 	if ((bp->b_flags & B_PAGING) == 0) {
1396 		panic(
1397 		    "pbreassignbuf() on non phys bp %p",
1398 		    bp
1399 		);
1400 	}
1401 	bp->b_vp = newvp;
1402 }
1403 
1404 /*
1405  * Reassign a buffer from one vnode to another.
1406  * Used to assign file specific control information
1407  * (indirect blocks) to the vnode to which they belong.
1408  */
1409 void
1410 reassignbuf(bp, newvp)
1411 	struct buf *bp;
1412 	struct vnode *newvp;
1413 {
1414 	struct buflists *listheadp;
1415 	int delay;
1416 	int s;
1417 
1418 	if (newvp == NULL) {
1419 		printf("reassignbuf: NULL");
1420 		return;
1421 	}
1422 	++reassignbufcalls;
1423 
1424 	/*
1425 	 * B_PAGING flagged buffers cannot be reassigned because their vp
1426 	 * is not fully linked in.
1427 	 */
1428 	if (bp->b_flags & B_PAGING)
1429 		panic("cannot reassign paging buffer");
1430 
1431 	s = splbio();
1432 	/*
1433 	 * Delete from old vnode list, if on one.
1434 	 */
1435 	if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) {
1436 		if (bp->b_xflags & BX_VNDIRTY)
1437 			listheadp = &bp->b_vp->v_dirtyblkhd;
1438 		else
1439 			listheadp = &bp->b_vp->v_cleanblkhd;
1440 		TAILQ_REMOVE(listheadp, bp, b_vnbufs);
1441 		bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN);
1442 		if (bp->b_vp != newvp) {
1443 			vdrop(bp->b_vp);
1444 			bp->b_vp = NULL;	/* for clarification */
1445 		}
1446 	}
1447 	/*
1448 	 * If dirty, put on list of dirty buffers; otherwise insert onto list
1449 	 * of clean buffers.
1450 	 */
1451 	if (bp->b_flags & B_DELWRI) {
1452 		struct buf *tbp;
1453 
1454 		listheadp = &newvp->v_dirtyblkhd;
1455 		if ((newvp->v_flag & VONWORKLST) == 0) {
1456 			switch (newvp->v_type) {
1457 			case VDIR:
1458 				delay = dirdelay;
1459 				break;
1460 			case VCHR:
1461 			case VBLK:
1462 				if (newvp->v_specmountpoint != NULL) {
1463 					delay = metadelay;
1464 					break;
1465 				}
1466 				/* fall through */
1467 			default:
1468 				delay = filedelay;
1469 			}
1470 			vn_syncer_add_to_worklist(newvp, delay);
1471 		}
1472 		bp->b_xflags |= BX_VNDIRTY;
1473 		tbp = TAILQ_FIRST(listheadp);
1474 		if (tbp == NULL ||
1475 		    bp->b_lblkno == 0 ||
1476 		    (bp->b_lblkno > 0 && tbp->b_lblkno < 0) ||
1477 		    (bp->b_lblkno > 0 && bp->b_lblkno < tbp->b_lblkno)) {
1478 			TAILQ_INSERT_HEAD(listheadp, bp, b_vnbufs);
1479 			++reassignbufsortgood;
1480 		} else if (bp->b_lblkno < 0) {
1481 			TAILQ_INSERT_TAIL(listheadp, bp, b_vnbufs);
1482 			++reassignbufsortgood;
1483 		} else if (reassignbufmethod == 1) {
1484 			/*
1485 			 * New sorting algorithm, only handle sequential case,
1486 			 * otherwise append to end (but before metadata)
1487 			 */
1488 			if ((tbp = gbincore(newvp, bp->b_lblkno - 1)) != NULL &&
1489 			    (tbp->b_xflags & BX_VNDIRTY)) {
1490 				/*
1491 				 * Found the best place to insert the buffer
1492 				 */
1493 				TAILQ_INSERT_AFTER(listheadp, tbp, bp, b_vnbufs);
1494 				++reassignbufsortgood;
1495 			} else {
1496 				/*
1497 				 * Missed, append to end, but before meta-data.
1498 				 * We know that the head buffer in the list is
1499 				 * not meta-data due to prior conditionals.
1500 				 *
1501 				 * Indirect effects:  NFS second stage write
1502 				 * tends to wind up here, giving maximum
1503 				 * distance between the unstable write and the
1504 				 * commit rpc.
1505 				 */
1506 				tbp = TAILQ_LAST(listheadp, buflists);
1507 				while (tbp && tbp->b_lblkno < 0)
1508 					tbp = TAILQ_PREV(tbp, buflists, b_vnbufs);
1509 				TAILQ_INSERT_AFTER(listheadp, tbp, bp, b_vnbufs);
1510 				++reassignbufsortbad;
1511 			}
1512 		} else {
1513 			/*
1514 			 * Old sorting algorithm, scan queue and insert
1515 			 */
1516 			struct buf *ttbp;
1517 			while ((ttbp = TAILQ_NEXT(tbp, b_vnbufs)) &&
1518 			    (ttbp->b_lblkno < bp->b_lblkno)) {
1519 				++reassignbufloops;
1520 				tbp = ttbp;
1521 			}
1522 			TAILQ_INSERT_AFTER(listheadp, tbp, bp, b_vnbufs);
1523 		}
1524 	} else {
1525 		bp->b_xflags |= BX_VNCLEAN;
1526 		TAILQ_INSERT_TAIL(&newvp->v_cleanblkhd, bp, b_vnbufs);
1527 		if ((newvp->v_flag & VONWORKLST) &&
1528 		    TAILQ_EMPTY(&newvp->v_dirtyblkhd)) {
1529 			newvp->v_flag &= ~VONWORKLST;
1530 			LIST_REMOVE(newvp, v_synclist);
1531 		}
1532 	}
1533 	if (bp->b_vp != newvp) {
1534 		bp->b_vp = newvp;
1535 		vhold(bp->b_vp);
1536 	}
1537 	splx(s);
1538 }
1539 
1540 /*
1541  * Create a vnode for a block device.
1542  * Used for mounting the root file system.
1543  */
1544 int
1545 bdevvp(dev, vpp)
1546 	dev_t dev;
1547 	struct vnode **vpp;
1548 {
1549 	struct vnode *vp;
1550 	struct vnode *nvp;
1551 	int error;
1552 
1553 	if (dev == NODEV) {
1554 		*vpp = NULLVP;
1555 		return (ENXIO);
1556 	}
1557 	error = getnewvnode(VT_NON, (struct mount *)0, spec_vnodeop_p, &nvp);
1558 	if (error) {
1559 		*vpp = NULLVP;
1560 		return (error);
1561 	}
1562 	vp = nvp;
1563 	vp->v_type = VBLK;
1564 	addalias(vp, dev);
1565 	*vpp = vp;
1566 	return (0);
1567 }
1568 
1569 /*
1570  * Add a vnode to the alias list hung off the dev_t.
1571  *
1572  * The reason for this gunk is that multiple vnodes can reference
1573  * the same physical device, so checking vp->v_usecount to see
1574  * how many users there are is inadequate; the v_usecount for
1575  * the vnodes need to be accumulated.  vcount() does that.
1576  */
1577 void
1578 addaliasu(struct vnode *nvp, udev_t nvp_rdev)
1579 {
1580 	dev_t dev;
1581 
1582 	if (nvp->v_type != VBLK && nvp->v_type != VCHR)
1583 		panic("addaliasu on non-special vnode");
1584 	dev = udev2dev(nvp_rdev, nvp->v_type == VBLK ? 1 : 0);
1585 	if (dev != NODEV) {
1586 		nvp->v_rdev = dev;
1587 		addalias(nvp, dev);
1588 	} else
1589 		nvp->v_rdev = NULL;
1590 }
1591 
1592 void
1593 addalias(struct vnode *nvp, dev_t dev)
1594 {
1595 	lwkt_tokref ilock;
1596 
1597 	if (nvp->v_type != VBLK && nvp->v_type != VCHR)
1598 		panic("addalias on non-special vnode");
1599 
1600 	nvp->v_rdev = dev;
1601 	lwkt_gettoken(&ilock, &spechash_token);
1602 	SLIST_INSERT_HEAD(&dev->si_hlist, nvp, v_specnext);
1603 	lwkt_reltoken(&ilock);
1604 }
1605 
1606 /*
1607  * Grab a particular vnode from the free list, increment its
1608  * reference count and lock it. The vnode lock bit is set if the
1609  * vnode is being eliminated in vgone. The process is awakened
1610  * when the transition is completed, and an error returned to
1611  * indicate that the vnode is no longer usable (possibly having
1612  * been changed to a new file system type).
1613  *
1614  * This code is very sensitive.  We are depending on the vnode interlock
1615  * to be maintained through to the vn_lock() call, which means that we
1616  * cannot block which means that we cannot call vbusy() until after vn_lock().
1617  * If the interlock is not maintained, the VXLOCK check will not properly
1618  * interlock against a vclean()'s LK_DRAIN operation on the lock.
1619  */
1620 int
1621 vget(struct vnode *vp, lwkt_tokref_t vlock, int flags, thread_t td)
1622 {
1623 	int error;
1624 	lwkt_tokref vvlock;
1625 
1626 	/*
1627 	 * We need the interlock to safely modify the v_ fields.  ZZZ it is
1628 	 * only legal to pass (1) the vnode's interlock and (2) only pass
1629 	 * NULL w/o LK_INTERLOCK if the vnode is *ALREADY* referenced or
1630 	 * held.
1631 	 */
1632 	if ((flags & LK_INTERLOCK) == 0) {
1633 		lwkt_gettoken(&vvlock, vp->v_interlock);
1634 		vlock = &vvlock;
1635 	}
1636 
1637 	/*
1638 	 * If the vnode is in the process of being cleaned out for
1639 	 * another use, we wait for the cleaning to finish and then
1640 	 * return failure. Cleaning is determined by checking that
1641 	 * the VXLOCK flag is set.  It is possible for the vnode to be
1642 	 * self-referenced during the cleaning operation.
1643 	 */
1644 	if (vp->v_flag & VXLOCK) {
1645 		if (vp->v_vxthread == curthread) {
1646 #if 0
1647 			/* this can now occur in normal operation */
1648 			log(LOG_INFO, "VXLOCK interlock avoided\n");
1649 #endif
1650 		} else {
1651 			vp->v_flag |= VXWANT;
1652 			lwkt_reltoken(vlock);
1653 			tsleep((caddr_t)vp, 0, "vget", 0);
1654 			return (ENOENT);
1655 		}
1656 	}
1657 
1658 	/*
1659 	 * Bump v_usecount to prevent the vnode from being recycled.  The
1660 	 * usecount needs to be bumped before we successfully get our lock.
1661 	 */
1662 	vp->v_usecount++;
1663 	if (flags & LK_TYPE_MASK) {
1664 		if ((error = vn_lock(vp, vlock, flags | LK_INTERLOCK, td)) != 0) {
1665 			/*
1666 			 * must expand vrele here because we do not want
1667 			 * to call VOP_INACTIVE if the reference count
1668 			 * drops back to zero since it was never really
1669 			 * active. We must remove it from the free list
1670 			 * before sleeping so that multiple processes do
1671 			 * not try to recycle it.
1672 			 */
1673 			lwkt_gettokref(vlock);
1674 			vp->v_usecount--;
1675 			vmaybefree(vp);
1676 			lwkt_reltoken(vlock);
1677 		}
1678 		return (error);
1679 	}
1680 	if (VSHOULDBUSY(vp))
1681 		vbusy(vp);	/* interlock must be held on call */
1682 	lwkt_reltoken(vlock);
1683 	return (0);
1684 }
1685 
1686 void
1687 vref(struct vnode *vp)
1688 {
1689 	crit_enter();	/* YYY use crit section for moment / BGL protected */
1690 	vp->v_usecount++;
1691 	crit_exit();
1692 }
1693 
1694 /*
1695  * Vnode put/release.
1696  * If count drops to zero, call inactive routine and return to freelist.
1697  */
1698 void
1699 vrele(struct vnode *vp)
1700 {
1701 	struct thread *td = curthread;	/* XXX */
1702 	lwkt_tokref vlock;
1703 
1704 	KASSERT(vp != NULL && vp->v_usecount >= 0,
1705 	    ("vrele: null vp or <=0 v_usecount"));
1706 
1707 	lwkt_gettoken(&vlock, vp->v_interlock);
1708 
1709 	if (vp->v_usecount > 1) {
1710 		vp->v_usecount--;
1711 		lwkt_reltoken(&vlock);
1712 		return;
1713 	}
1714 
1715 	if (vp->v_usecount == 1) {
1716 		vp->v_usecount--;
1717 		/*
1718 		 * We must call VOP_INACTIVE with the node locked and the
1719 		 * usecount 0.  If we are doing a vpu, the node is already
1720 		 * locked, but, in the case of vrele, we must explicitly lock
1721 		 * the vnode before calling VOP_INACTIVE.
1722 		 */
1723 
1724 		if (vn_lock(vp, NULL, LK_EXCLUSIVE, td) == 0)
1725 			VOP_INACTIVE(vp, td);
1726 		vmaybefree(vp);
1727 		lwkt_reltoken(&vlock);
1728 	} else {
1729 #ifdef DIAGNOSTIC
1730 		vprint("vrele: negative ref count", vp);
1731 #endif
1732 		lwkt_reltoken(&vlock);
1733 		panic("vrele: negative ref cnt");
1734 	}
1735 }
1736 
1737 void
1738 vput(struct vnode *vp)
1739 {
1740 	struct thread *td = curthread;	/* XXX */
1741 	lwkt_tokref vlock;
1742 
1743 	KASSERT(vp != NULL, ("vput: null vp"));
1744 
1745 	lwkt_gettoken(&vlock, vp->v_interlock);
1746 
1747 	if (vp->v_usecount > 1) {
1748 		vp->v_usecount--;
1749 		VOP_UNLOCK(vp, &vlock, LK_INTERLOCK, td);
1750 		return;
1751 	}
1752 
1753 	if (vp->v_usecount == 1) {
1754 		vp->v_usecount--;
1755 		/*
1756 		 * We must call VOP_INACTIVE with the node locked.
1757 		 * If we are doing a vpu, the node is already locked,
1758 		 * so we just need to release the vnode mutex.
1759 		 */
1760 		VOP_INACTIVE(vp, td);
1761 		vmaybefree(vp);
1762 		lwkt_reltoken(&vlock);
1763 	} else {
1764 #ifdef DIAGNOSTIC
1765 		vprint("vput: negative ref count", vp);
1766 #endif
1767 		lwkt_reltoken(&vlock);
1768 		panic("vput: negative ref cnt");
1769 	}
1770 }
1771 
1772 /*
1773  * Somebody doesn't want the vnode recycled. ZZZ vnode interlock should
1774  * be held but isn't.
1775  */
1776 void
1777 vhold(vp)
1778 	struct vnode *vp;
1779 {
1780 	int s;
1781 
1782   	s = splbio();
1783 	vp->v_holdcnt++;
1784 	if (VSHOULDBUSY(vp))
1785 		vbusy(vp);	/* interlock must be held on call */
1786 	splx(s);
1787 }
1788 
1789 /*
1790  * One less who cares about this vnode.
1791  */
1792 void
1793 vdrop(vp)
1794 	struct vnode *vp;
1795 {
1796 	lwkt_tokref vlock;
1797 
1798 	lwkt_gettoken(&vlock, vp->v_interlock);
1799 	if (vp->v_holdcnt <= 0)
1800 		panic("vdrop: holdcnt");
1801 	vp->v_holdcnt--;
1802 	vmaybefree(vp);
1803 	lwkt_reltoken(&vlock);
1804 }
1805 
1806 int
1807 vmntvnodescan(
1808     struct mount *mp,
1809     int (*fastfunc)(struct mount *mp, struct vnode *vp, void *data),
1810     int (*slowfunc)(struct mount *mp, struct vnode *vp, lwkt_tokref_t vlock, void *data),
1811     void *data
1812 ) {
1813 	lwkt_tokref ilock;
1814 	lwkt_tokref vlock;
1815 	struct vnode *pvp;
1816 	struct vnode *vp;
1817 	int r = 0;
1818 
1819 	/*
1820 	 * Scan the vnodes on the mount's vnode list.  Use a placemarker
1821 	 */
1822 	pvp = zalloc(vnode_zone);
1823 	pvp->v_flag |= VPLACEMARKER;
1824 
1825 	lwkt_gettoken(&ilock, &mntvnode_token);
1826 	TAILQ_INSERT_HEAD(&mp->mnt_nvnodelist, pvp, v_nmntvnodes);
1827 
1828 	while ((vp = TAILQ_NEXT(pvp, v_nmntvnodes)) != NULL) {
1829 		/*
1830 		 * Move the placemarker and skip other placemarkers we
1831 		 * encounter.  The nothing can get in our way so the
1832 		 * mount point on the vp must be valid.
1833 		 */
1834 		TAILQ_REMOVE(&mp->mnt_nvnodelist, pvp, v_nmntvnodes);
1835 		TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, pvp, v_nmntvnodes);
1836 		if (vp->v_flag & VPLACEMARKER)
1837 			continue;
1838 		KKASSERT(vp->v_mount == mp);
1839 
1840 		/*
1841 		 * Quick test
1842 		 */
1843 		if (fastfunc) {
1844 			if ((r = fastfunc(mp, vp, data)) < 0)
1845 				continue;
1846 			if (r)
1847 				break;
1848 		}
1849 
1850 		/*
1851 		 * Get the vnodes interlock and make sure it is still on the
1852 		 * mount list.  Skip it if it has moved (we may encounter it
1853 		 * later).  Then do the with-interlock test.  The callback
1854 		 * is responsible for releasing the vnode interlock.
1855 		 *
1856 		 * The interlock is type-stable.
1857 		 */
1858 		if (slowfunc) {
1859 			lwkt_gettoken(&vlock, vp->v_interlock);
1860 			if (vp != TAILQ_PREV(pvp, vnodelst, v_nmntvnodes)) {
1861 				printf("vmntvnodescan (debug info only): f=%p vp=%p vnode ripped out from under us\n", slowfunc, vp);
1862 				lwkt_reltoken(&vlock);
1863 				continue;
1864 			}
1865 			if ((r = slowfunc(mp, vp, &vlock, data)) != 0) {
1866 				KKASSERT(lwkt_havetokref(&vlock) == 0);
1867 				break;
1868 			}
1869 			KKASSERT(lwkt_havetokref(&vlock) == 0);
1870 		}
1871 	}
1872 	TAILQ_REMOVE(&mp->mnt_nvnodelist, pvp, v_nmntvnodes);
1873 	zfree(vnode_zone, pvp);
1874 	lwkt_reltoken(&ilock);
1875 	return(r);
1876 }
1877 
1878 /*
1879  * Remove any vnodes in the vnode table belonging to mount point mp.
1880  *
1881  * If FORCECLOSE is not specified, there should not be any active ones,
1882  * return error if any are found (nb: this is a user error, not a
1883  * system error). If FORCECLOSE is specified, detach any active vnodes
1884  * that are found.
1885  *
1886  * If WRITECLOSE is set, only flush out regular file vnodes open for
1887  * writing.
1888  *
1889  * SKIPSYSTEM causes any vnodes marked VSYSTEM to be skipped.
1890  *
1891  * `rootrefs' specifies the base reference count for the root vnode
1892  * of this filesystem. The root vnode is considered busy if its
1893  * v_usecount exceeds this value. On a successful return, vflush()
1894  * will call vrele() on the root vnode exactly rootrefs times.
1895  * If the SKIPSYSTEM or WRITECLOSE flags are specified, rootrefs must
1896  * be zero.
1897  */
1898 #ifdef DIAGNOSTIC
1899 static int busyprt = 0;		/* print out busy vnodes */
1900 SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, "");
1901 #endif
1902 
1903 static int vflush_scan(struct mount *mp, struct vnode *vp, lwkt_tokref_t vlock, void *data);
1904 
1905 struct vflush_info {
1906 	int flags;
1907 	int busy;
1908 	thread_t td;
1909 };
1910 
1911 int
1912 vflush(mp, rootrefs, flags)
1913 	struct mount *mp;
1914 	int rootrefs;
1915 	int flags;
1916 {
1917 	struct thread *td = curthread;	/* XXX */
1918 	struct vnode *rootvp = NULL;
1919 	int error;
1920 	lwkt_tokref vlock;
1921 	struct vflush_info vflush_info;
1922 
1923 	if (rootrefs > 0) {
1924 		KASSERT((flags & (SKIPSYSTEM | WRITECLOSE)) == 0,
1925 		    ("vflush: bad args"));
1926 		/*
1927 		 * Get the filesystem root vnode. We can vput() it
1928 		 * immediately, since with rootrefs > 0, it won't go away.
1929 		 */
1930 		if ((error = VFS_ROOT(mp, &rootvp)) != 0)
1931 			return (error);
1932 		vput(rootvp);
1933 	}
1934 
1935 	vflush_info.busy = 0;
1936 	vflush_info.flags = flags;
1937 	vflush_info.td = td;
1938 	vmntvnodescan(mp, NULL, vflush_scan, &vflush_info);
1939 
1940 	if (rootrefs > 0 && (flags & FORCECLOSE) == 0) {
1941 		/*
1942 		 * If just the root vnode is busy, and if its refcount
1943 		 * is equal to `rootrefs', then go ahead and kill it.
1944 		 */
1945 		lwkt_gettoken(&vlock, rootvp->v_interlock);
1946 		KASSERT(vflush_info.busy > 0, ("vflush: not busy"));
1947 		KASSERT(rootvp->v_usecount >= rootrefs, ("vflush: rootrefs"));
1948 		if (vflush_info.busy == 1 && rootvp->v_usecount == rootrefs) {
1949 			vgonel(rootvp, &vlock, td);
1950 			vflush_info.busy = 0;
1951 		} else {
1952 			lwkt_reltoken(&vlock);
1953 		}
1954 	}
1955 	if (vflush_info.busy)
1956 		return (EBUSY);
1957 	for (; rootrefs > 0; rootrefs--)
1958 		vrele(rootvp);
1959 	return (0);
1960 }
1961 
1962 /*
1963  * The scan callback is made with an interlocked vnode.
1964  */
1965 static int
1966 vflush_scan(struct mount *mp, struct vnode *vp, lwkt_tokref_t vlock, void *data)
1967 {
1968 	struct vflush_info *info = data;
1969 	struct vattr vattr;
1970 
1971 	/*
1972 	 * Skip over a vnodes marked VSYSTEM.
1973 	 */
1974 	if ((info->flags & SKIPSYSTEM) && (vp->v_flag & VSYSTEM)) {
1975 		lwkt_reltoken(vlock);
1976 		return(0);
1977 	}
1978 
1979 	/*
1980 	 * If WRITECLOSE is set, flush out unlinked but still open
1981 	 * files (even if open only for reading) and regular file
1982 	 * vnodes open for writing.
1983 	 */
1984 	if ((info->flags & WRITECLOSE) &&
1985 	    (vp->v_type == VNON ||
1986 	    (VOP_GETATTR(vp, &vattr, info->td) == 0 &&
1987 	    vattr.va_nlink > 0)) &&
1988 	    (vp->v_writecount == 0 || vp->v_type != VREG)) {
1989 		lwkt_reltoken(vlock);
1990 		return(0);
1991 	}
1992 
1993 	/*
1994 	 * With v_usecount == 0, all we need to do is clear out the
1995 	 * vnode data structures and we are done.
1996 	 */
1997 	if (vp->v_usecount == 0) {
1998 		vgonel(vp, vlock, info->td);
1999 		return(0);
2000 	}
2001 
2002 	/*
2003 	 * If FORCECLOSE is set, forcibly close the vnode. For block
2004 	 * or character devices, revert to an anonymous device. For
2005 	 * all other files, just kill them.
2006 	 */
2007 	if (info->flags & FORCECLOSE) {
2008 		if (vp->v_type != VBLK && vp->v_type != VCHR) {
2009 			vgonel(vp, vlock, info->td);
2010 		} else {
2011 			vclean(vp, vlock, 0, info->td);
2012 			vp->v_op = spec_vnodeop_p;
2013 			insmntque(vp, (struct mount *) 0);
2014 		}
2015 		return(0);
2016 	}
2017 #ifdef DIAGNOSTIC
2018 	if (busyprt)
2019 		vprint("vflush: busy vnode", vp);
2020 #endif
2021 	lwkt_reltoken(vlock);
2022 	++info->busy;
2023 	return(0);
2024 }
2025 
2026 /*
2027  * Disassociate the underlying file system from a vnode.
2028  */
2029 static void
2030 vclean(struct vnode *vp, lwkt_tokref_t vlock, int flags, struct thread *td)
2031 {
2032 	int active;
2033 
2034 	/*
2035 	 * Check to see if the vnode is in use. If so we have to reference it
2036 	 * before we clean it out so that its count cannot fall to zero and
2037 	 * generate a race against ourselves to recycle it.
2038 	 */
2039 	if ((active = vp->v_usecount))
2040 		vp->v_usecount++;
2041 
2042 	/*
2043 	 * Prevent the vnode from being recycled or brought into use while we
2044 	 * clean it out.
2045 	 */
2046 	if (vp->v_flag & VXLOCK)
2047 		panic("vclean: deadlock");
2048 	vp->v_flag |= VXLOCK;
2049 	vp->v_vxthread = curthread;
2050 
2051 	/*
2052 	 * Even if the count is zero, the VOP_INACTIVE routine may still
2053 	 * have the object locked while it cleans it out. The VOP_LOCK
2054 	 * ensures that the VOP_INACTIVE routine is done with its work.
2055 	 * For active vnodes, it ensures that no other activity can
2056 	 * occur while the underlying object is being cleaned out.
2057 	 *
2058 	 * NOTE: we continue to hold the vnode interlock through to the
2059 	 * end of vclean().
2060 	 */
2061 	VOP_LOCK(vp, NULL, LK_DRAIN, td);
2062 
2063 	/*
2064 	 * Clean out any buffers associated with the vnode.
2065 	 */
2066 	vinvalbuf(vp, V_SAVE, td, 0, 0);
2067 	VOP_DESTROYVOBJECT(vp);
2068 
2069 	/*
2070 	 * If purging an active vnode, it must be closed and
2071 	 * deactivated before being reclaimed. Note that the
2072 	 * VOP_INACTIVE will unlock the vnode.
2073 	 */
2074 	if (active) {
2075 		if (flags & DOCLOSE)
2076 			VOP_CLOSE(vp, FNONBLOCK, td);
2077 		VOP_INACTIVE(vp, td);
2078 	} else {
2079 		/*
2080 		 * Any other processes trying to obtain this lock must first
2081 		 * wait for VXLOCK to clear, then call the new lock operation.
2082 		 */
2083 		VOP_UNLOCK(vp, NULL, 0, td);
2084 	}
2085 	/*
2086 	 * Reclaim the vnode.
2087 	 */
2088 	if (VOP_RECLAIM(vp, td))
2089 		panic("vclean: cannot reclaim");
2090 
2091 	if (active) {
2092 		/*
2093 		 * Inline copy of vrele() since VOP_INACTIVE
2094 		 * has already been called.
2095 		 */
2096 		if (--vp->v_usecount <= 0) {
2097 #ifdef DIAGNOSTIC
2098 			if (vp->v_usecount < 0 || vp->v_writecount != 0) {
2099 				vprint("vclean: bad ref count", vp);
2100 				panic("vclean: ref cnt");
2101 			}
2102 #endif
2103 			vfree(vp);
2104 		}
2105 	}
2106 
2107 	cache_purge(vp);
2108 	vp->v_vnlock = NULL;
2109 	vmaybefree(vp);
2110 
2111 	/*
2112 	 * Done with purge, notify sleepers of the grim news.
2113 	 */
2114 	vp->v_op = dead_vnodeop_p;
2115 	vn_pollgone(vp);
2116 	vp->v_tag = VT_NON;
2117 	vp->v_flag &= ~VXLOCK;
2118 	vp->v_vxthread = NULL;
2119 	if (vp->v_flag & VXWANT) {
2120 		vp->v_flag &= ~VXWANT;
2121 		wakeup((caddr_t) vp);
2122 	}
2123 	lwkt_reltoken(vlock);
2124 }
2125 
2126 /*
2127  * Eliminate all activity associated with the requested vnode
2128  * and with all vnodes aliased to the requested vnode.
2129  */
2130 int
2131 vop_revoke(ap)
2132 	struct vop_revoke_args /* {
2133 		struct vnode *a_vp;
2134 		int a_flags;
2135 	} */ *ap;
2136 {
2137 	struct vnode *vp, *vq;
2138 	lwkt_tokref ilock;
2139 	dev_t dev;
2140 
2141 	KASSERT((ap->a_flags & REVOKEALL) != 0, ("vop_revoke"));
2142 
2143 	vp = ap->a_vp;
2144 	/*
2145 	 * If a vgone (or vclean) is already in progress,
2146 	 * wait until it is done and return.
2147 	 */
2148 	if (vp->v_flag & VXLOCK) {
2149 		vp->v_flag |= VXWANT;
2150 		/*lwkt_reltoken(vlock); ZZZ */
2151 		tsleep((caddr_t)vp, 0, "vop_revokeall", 0);
2152 		return (0);
2153 	}
2154 	dev = vp->v_rdev;
2155 	for (;;) {
2156 		lwkt_gettoken(&ilock, &spechash_token);
2157 		vq = SLIST_FIRST(&dev->si_hlist);
2158 		lwkt_reltoken(&ilock);
2159 		if (!vq)
2160 			break;
2161 		vgone(vq);
2162 	}
2163 	return (0);
2164 }
2165 
2166 /*
2167  * Recycle an unused vnode to the front of the free list.
2168  * Release the passed interlock if the vnode will be recycled.
2169  */
2170 int
2171 vrecycle(struct vnode *vp, lwkt_tokref_t inter_lkp, struct thread *td)
2172 {
2173 	lwkt_tokref vlock;
2174 
2175 	lwkt_gettoken(&vlock, vp->v_interlock);
2176 	if (vp->v_usecount == 0) {
2177 		if (inter_lkp)
2178 			lwkt_reltoken(inter_lkp);
2179 		vgonel(vp, &vlock, td);
2180 		return (1);
2181 	}
2182 	lwkt_reltoken(&vlock);
2183 	return (0);
2184 }
2185 
2186 /*
2187  * Eliminate all activity associated with a vnode
2188  * in preparation for reuse.
2189  */
2190 void
2191 vgone(struct vnode *vp)
2192 {
2193 	struct thread *td = curthread;	/* XXX */
2194 	lwkt_tokref vlock;
2195 
2196 	lwkt_gettoken(&vlock, vp->v_interlock);
2197 	vgonel(vp, &vlock, td);
2198 }
2199 
2200 /*
2201  * vgone, with the vp interlock held.
2202  */
2203 void
2204 vgonel(struct vnode *vp, lwkt_tokref_t vlock, struct thread *td)
2205 {
2206 	lwkt_tokref ilock;
2207 	int s;
2208 
2209 	/*
2210 	 * If a vgone (or vclean) is already in progress,
2211 	 * wait until it is done and return.
2212 	 */
2213 	if (vp->v_flag & VXLOCK) {
2214 		vp->v_flag |= VXWANT;
2215 		lwkt_reltoken(vlock);
2216 		tsleep((caddr_t)vp, 0, "vgone", 0);
2217 		return;
2218 	}
2219 
2220 	/*
2221 	 * Clean out the filesystem specific data.
2222 	 */
2223 	vclean(vp, vlock, DOCLOSE, td);
2224 	lwkt_gettokref(vlock);
2225 
2226 	/*
2227 	 * Delete from old mount point vnode list, if on one.
2228 	 */
2229 	if (vp->v_mount != NULL)
2230 		insmntque(vp, (struct mount *)0);
2231 	/*
2232 	 * If special device, remove it from special device alias list
2233 	 * if it is on one.
2234 	 */
2235 	if ((vp->v_type == VBLK || vp->v_type == VCHR) && vp->v_rdev != NULL) {
2236 		lwkt_gettoken(&ilock, &spechash_token);
2237 		SLIST_REMOVE(&vp->v_hashchain, vp, vnode, v_specnext);
2238 		freedev(vp->v_rdev);
2239 		lwkt_reltoken(&ilock);
2240 		vp->v_rdev = NULL;
2241 	}
2242 
2243 	/*
2244 	 * If it is on the freelist and not already at the head,
2245 	 * move it to the head of the list. The test of the
2246 	 * VDOOMED flag and the reference count of zero is because
2247 	 * it will be removed from the free list by getnewvnode,
2248 	 * but will not have its reference count incremented until
2249 	 * after calling vgone. If the reference count were
2250 	 * incremented first, vgone would (incorrectly) try to
2251 	 * close the previous instance of the underlying object.
2252 	 */
2253 	if (vp->v_usecount == 0 && !(vp->v_flag & VDOOMED)) {
2254 		s = splbio();
2255 		lwkt_gettoken(&ilock, &vnode_free_list_token);
2256 		if (vp->v_flag & VFREE)
2257 			TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
2258 		else
2259 			freevnodes++;
2260 		vp->v_flag |= VFREE;
2261 		TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
2262 		lwkt_reltoken(&ilock);
2263 		splx(s);
2264 	}
2265 	vp->v_type = VBAD;
2266 	lwkt_reltoken(vlock);
2267 }
2268 
2269 /*
2270  * Lookup a vnode by device number.
2271  */
2272 int
2273 vfinddev(dev, type, vpp)
2274 	dev_t dev;
2275 	enum vtype type;
2276 	struct vnode **vpp;
2277 {
2278 	lwkt_tokref ilock;
2279 	struct vnode *vp;
2280 
2281 	lwkt_gettoken(&ilock, &spechash_token);
2282 	SLIST_FOREACH(vp, &dev->si_hlist, v_specnext) {
2283 		if (type == vp->v_type) {
2284 			*vpp = vp;
2285 			lwkt_reltoken(&ilock);
2286 			return (1);
2287 		}
2288 	}
2289 	lwkt_reltoken(&ilock);
2290 	return (0);
2291 }
2292 
2293 /*
2294  * Calculate the total number of references to a special device.
2295  */
2296 int
2297 vcount(vp)
2298 	struct vnode *vp;
2299 {
2300 	lwkt_tokref ilock;
2301 	struct vnode *vq;
2302 	int count;
2303 
2304 	count = 0;
2305 	lwkt_gettoken(&ilock, &spechash_token);
2306 	SLIST_FOREACH(vq, &vp->v_hashchain, v_specnext)
2307 		count += vq->v_usecount;
2308 	lwkt_reltoken(&ilock);
2309 	return (count);
2310 }
2311 
2312 /*
2313  * Same as above, but using the dev_t as argument
2314  */
2315 
2316 int
2317 count_dev(dev)
2318 	dev_t dev;
2319 {
2320 	struct vnode *vp;
2321 
2322 	vp = SLIST_FIRST(&dev->si_hlist);
2323 	if (vp == NULL)
2324 		return (0);
2325 	return(vcount(vp));
2326 }
2327 
2328 /*
2329  * Print out a description of a vnode.
2330  */
2331 static char *typename[] =
2332 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD"};
2333 
2334 void
2335 vprint(label, vp)
2336 	char *label;
2337 	struct vnode *vp;
2338 {
2339 	char buf[96];
2340 
2341 	if (label != NULL)
2342 		printf("%s: %p: ", label, (void *)vp);
2343 	else
2344 		printf("%p: ", (void *)vp);
2345 	printf("type %s, usecount %d, writecount %d, refcount %d,",
2346 	    typename[vp->v_type], vp->v_usecount, vp->v_writecount,
2347 	    vp->v_holdcnt);
2348 	buf[0] = '\0';
2349 	if (vp->v_flag & VROOT)
2350 		strcat(buf, "|VROOT");
2351 	if (vp->v_flag & VTEXT)
2352 		strcat(buf, "|VTEXT");
2353 	if (vp->v_flag & VSYSTEM)
2354 		strcat(buf, "|VSYSTEM");
2355 	if (vp->v_flag & VXLOCK)
2356 		strcat(buf, "|VXLOCK");
2357 	if (vp->v_flag & VXWANT)
2358 		strcat(buf, "|VXWANT");
2359 	if (vp->v_flag & VBWAIT)
2360 		strcat(buf, "|VBWAIT");
2361 	if (vp->v_flag & VDOOMED)
2362 		strcat(buf, "|VDOOMED");
2363 	if (vp->v_flag & VFREE)
2364 		strcat(buf, "|VFREE");
2365 	if (vp->v_flag & VOBJBUF)
2366 		strcat(buf, "|VOBJBUF");
2367 	if (buf[0] != '\0')
2368 		printf(" flags (%s)", &buf[1]);
2369 	if (vp->v_data == NULL) {
2370 		printf("\n");
2371 	} else {
2372 		printf("\n\t");
2373 		VOP_PRINT(vp);
2374 	}
2375 }
2376 
2377 #ifdef DDB
2378 #include <ddb/ddb.h>
2379 /*
2380  * List all of the locked vnodes in the system.
2381  * Called when debugging the kernel.
2382  */
2383 DB_SHOW_COMMAND(lockedvnodes, lockedvnodes)
2384 {
2385 	struct thread *td = curthread;	/* XXX */
2386 	lwkt_tokref ilock;
2387 	struct mount *mp, *nmp;
2388 	struct vnode *vp;
2389 
2390 	printf("Locked vnodes\n");
2391 	lwkt_gettoken(&ilock, &mountlist_token);
2392 	for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
2393 		if (vfs_busy(mp, LK_NOWAIT, &ilock, td)) {
2394 			nmp = TAILQ_NEXT(mp, mnt_list);
2395 			continue;
2396 		}
2397 		TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
2398 			if (VOP_ISLOCKED(vp, NULL))
2399 				vprint((char *)0, vp);
2400 		}
2401 		lwkt_gettokref(&ilock);
2402 		nmp = TAILQ_NEXT(mp, mnt_list);
2403 		vfs_unbusy(mp, td);
2404 	}
2405 	lwkt_reltoken(&ilock);
2406 }
2407 #endif
2408 
2409 /*
2410  * Top level filesystem related information gathering.
2411  */
2412 static int	sysctl_ovfs_conf (SYSCTL_HANDLER_ARGS);
2413 
2414 static int
2415 vfs_sysctl(SYSCTL_HANDLER_ARGS)
2416 {
2417 	int *name = (int *)arg1 - 1;	/* XXX */
2418 	u_int namelen = arg2 + 1;	/* XXX */
2419 	struct vfsconf *vfsp;
2420 
2421 #if 1 || defined(COMPAT_PRELITE2)
2422 	/* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
2423 	if (namelen == 1)
2424 		return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
2425 #endif
2426 
2427 #ifdef notyet
2428 	/* all sysctl names at this level are at least name and field */
2429 	if (namelen < 2)
2430 		return (ENOTDIR);		/* overloaded */
2431 	if (name[0] != VFS_GENERIC) {
2432 		for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
2433 			if (vfsp->vfc_typenum == name[0])
2434 				break;
2435 		if (vfsp == NULL)
2436 			return (EOPNOTSUPP);
2437 		return ((*vfsp->vfc_vfsops->vfs_sysctl)(&name[1], namelen - 1,
2438 		    oldp, oldlenp, newp, newlen, p));
2439 	}
2440 #endif
2441 	switch (name[1]) {
2442 	case VFS_MAXTYPENUM:
2443 		if (namelen != 2)
2444 			return (ENOTDIR);
2445 		return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int)));
2446 	case VFS_CONF:
2447 		if (namelen != 3)
2448 			return (ENOTDIR);	/* overloaded */
2449 		for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
2450 			if (vfsp->vfc_typenum == name[2])
2451 				break;
2452 		if (vfsp == NULL)
2453 			return (EOPNOTSUPP);
2454 		return (SYSCTL_OUT(req, vfsp, sizeof *vfsp));
2455 	}
2456 	return (EOPNOTSUPP);
2457 }
2458 
2459 SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD, vfs_sysctl,
2460 	"Generic filesystem");
2461 
2462 #if 1 || defined(COMPAT_PRELITE2)
2463 
2464 static int
2465 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
2466 {
2467 	int error;
2468 	struct vfsconf *vfsp;
2469 	struct ovfsconf ovfs;
2470 
2471 	for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
2472 		ovfs.vfc_vfsops = vfsp->vfc_vfsops;	/* XXX used as flag */
2473 		strcpy(ovfs.vfc_name, vfsp->vfc_name);
2474 		ovfs.vfc_index = vfsp->vfc_typenum;
2475 		ovfs.vfc_refcount = vfsp->vfc_refcount;
2476 		ovfs.vfc_flags = vfsp->vfc_flags;
2477 		error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
2478 		if (error)
2479 			return error;
2480 	}
2481 	return 0;
2482 }
2483 
2484 #endif /* 1 || COMPAT_PRELITE2 */
2485 
2486 #if 0
2487 #define KINFO_VNODESLOP	10
2488 /*
2489  * Dump vnode list (via sysctl).
2490  * Copyout address of vnode followed by vnode.
2491  */
2492 /* ARGSUSED */
2493 static int
2494 sysctl_vnode(SYSCTL_HANDLER_ARGS)
2495 {
2496 	struct proc *p = curproc;	/* XXX */
2497 	struct mount *mp, *nmp;
2498 	struct vnode *nvp, *vp;
2499 	lwkt_tokref ilock;
2500 	lwkt_tokref jlock;
2501 	int error;
2502 
2503 #define VPTRSZ	sizeof (struct vnode *)
2504 #define VNODESZ	sizeof (struct vnode)
2505 
2506 	req->lock = 0;
2507 	if (!req->oldptr) /* Make an estimate */
2508 		return (SYSCTL_OUT(req, 0,
2509 			(numvnodes + KINFO_VNODESLOP) * (VPTRSZ + VNODESZ)));
2510 
2511 	lwkt_gettoken(&ilock, &mountlist_token);
2512 	for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
2513 		if (vfs_busy(mp, LK_NOWAIT, &ilock, p)) {
2514 			nmp = TAILQ_NEXT(mp, mnt_list);
2515 			continue;
2516 		}
2517 		lwkt_gettoken(&jlock, &mntvnode_token);
2518 again:
2519 		for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
2520 		     vp != NULL;
2521 		     vp = nvp) {
2522 			/*
2523 			 * Check that the vp is still associated with
2524 			 * this filesystem.  RACE: could have been
2525 			 * recycled onto the same filesystem.
2526 			 */
2527 			if (vp->v_mount != mp)
2528 				goto again;
2529 			nvp = TAILQ_NEXT(vp, v_nmntvnodes);
2530 			if ((error = SYSCTL_OUT(req, &vp, VPTRSZ)) ||
2531 			    (error = SYSCTL_OUT(req, vp, VNODESZ))) {
2532 				lwkt_reltoken(&jlock);
2533 				return (error);
2534 			}
2535 		}
2536 		lwkt_reltoken(&jlock);
2537 		lwkt_gettokref(&ilock);
2538 		nmp = TAILQ_NEXT(mp, mnt_list);	/* ZZZ */
2539 		vfs_unbusy(mp, p);
2540 	}
2541 	lwkt_reltoken(&ilock);
2542 
2543 	return (0);
2544 }
2545 #endif
2546 
2547 /*
2548  * XXX
2549  * Exporting the vnode list on large systems causes them to crash.
2550  * Exporting the vnode list on medium systems causes sysctl to coredump.
2551  */
2552 #if 0
2553 SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE|CTLFLAG_RD,
2554 	0, 0, sysctl_vnode, "S,vnode", "");
2555 #endif
2556 
2557 /*
2558  * Check to see if a filesystem is mounted on a block device.
2559  */
2560 int
2561 vfs_mountedon(vp)
2562 	struct vnode *vp;
2563 {
2564 
2565 	if (vp->v_specmountpoint != NULL)
2566 		return (EBUSY);
2567 	return (0);
2568 }
2569 
2570 /*
2571  * Unmount all filesystems. The list is traversed in reverse order
2572  * of mounting to avoid dependencies.
2573  */
2574 void
2575 vfs_unmountall()
2576 {
2577 	struct mount *mp;
2578 	struct thread *td = curthread;
2579 	int error;
2580 
2581 	if (td->td_proc == NULL)
2582 		td = initproc->p_thread;	/* XXX XXX use proc0 instead? */
2583 
2584 	/*
2585 	 * Since this only runs when rebooting, it is not interlocked.
2586 	 */
2587 	while(!TAILQ_EMPTY(&mountlist)) {
2588 		mp = TAILQ_LAST(&mountlist, mntlist);
2589 		error = dounmount(mp, MNT_FORCE, td);
2590 		if (error) {
2591 			TAILQ_REMOVE(&mountlist, mp, mnt_list);
2592 			printf("unmount of %s failed (",
2593 			    mp->mnt_stat.f_mntonname);
2594 			if (error == EBUSY)
2595 				printf("BUSY)\n");
2596 			else
2597 				printf("%d)\n", error);
2598 		} else {
2599 			/* The unmount has removed mp from the mountlist */
2600 		}
2601 	}
2602 }
2603 
2604 /*
2605  * Build hash lists of net addresses and hang them off the mount point.
2606  * Called by ufs_mount() to set up the lists of export addresses.
2607  */
2608 static int
2609 vfs_hang_addrlist(mp, nep, argp)
2610 	struct mount *mp;
2611 	struct netexport *nep;
2612 	struct export_args *argp;
2613 {
2614 	struct netcred *np;
2615 	struct radix_node_head *rnh;
2616 	int i;
2617 	struct radix_node *rn;
2618 	struct sockaddr *saddr, *smask = 0;
2619 	struct domain *dom;
2620 	int error;
2621 
2622 	if (argp->ex_addrlen == 0) {
2623 		if (mp->mnt_flag & MNT_DEFEXPORTED)
2624 			return (EPERM);
2625 		np = &nep->ne_defexported;
2626 		np->netc_exflags = argp->ex_flags;
2627 		np->netc_anon = argp->ex_anon;
2628 		np->netc_anon.cr_ref = 1;
2629 		mp->mnt_flag |= MNT_DEFEXPORTED;
2630 		return (0);
2631 	}
2632 
2633 	if (argp->ex_addrlen < 0 || argp->ex_addrlen > MLEN)
2634 		return (EINVAL);
2635 	if (argp->ex_masklen < 0 || argp->ex_masklen > MLEN)
2636 		return (EINVAL);
2637 
2638 	i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
2639 	np = (struct netcred *) malloc(i, M_NETADDR, M_WAITOK);
2640 	bzero((caddr_t) np, i);
2641 	saddr = (struct sockaddr *) (np + 1);
2642 	if ((error = copyin(argp->ex_addr, (caddr_t) saddr, argp->ex_addrlen)))
2643 		goto out;
2644 	if (saddr->sa_len > argp->ex_addrlen)
2645 		saddr->sa_len = argp->ex_addrlen;
2646 	if (argp->ex_masklen) {
2647 		smask = (struct sockaddr *) ((caddr_t) saddr + argp->ex_addrlen);
2648 		error = copyin(argp->ex_mask, (caddr_t) smask, argp->ex_masklen);
2649 		if (error)
2650 			goto out;
2651 		if (smask->sa_len > argp->ex_masklen)
2652 			smask->sa_len = argp->ex_masklen;
2653 	}
2654 	i = saddr->sa_family;
2655 	if ((rnh = nep->ne_rtable[i]) == 0) {
2656 		/*
2657 		 * Seems silly to initialize every AF when most are not used,
2658 		 * do so on demand here
2659 		 */
2660 		for (dom = domains; dom; dom = dom->dom_next)
2661 			if (dom->dom_family == i && dom->dom_rtattach) {
2662 				dom->dom_rtattach((void **) &nep->ne_rtable[i],
2663 				    dom->dom_rtoffset);
2664 				break;
2665 			}
2666 		if ((rnh = nep->ne_rtable[i]) == 0) {
2667 			error = ENOBUFS;
2668 			goto out;
2669 		}
2670 	}
2671 	rn = (*rnh->rnh_addaddr) ((caddr_t) saddr, (caddr_t) smask, rnh,
2672 	    np->netc_rnodes);
2673 	if (rn == 0 || np != (struct netcred *) rn) {	/* already exists */
2674 		error = EPERM;
2675 		goto out;
2676 	}
2677 	np->netc_exflags = argp->ex_flags;
2678 	np->netc_anon = argp->ex_anon;
2679 	np->netc_anon.cr_ref = 1;
2680 	return (0);
2681 out:
2682 	free(np, M_NETADDR);
2683 	return (error);
2684 }
2685 
2686 /* ARGSUSED */
2687 static int
2688 vfs_free_netcred(rn, w)
2689 	struct radix_node *rn;
2690 	void *w;
2691 {
2692 	struct radix_node_head *rnh = (struct radix_node_head *) w;
2693 
2694 	(*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, rnh);
2695 	free((caddr_t) rn, M_NETADDR);
2696 	return (0);
2697 }
2698 
2699 /*
2700  * Free the net address hash lists that are hanging off the mount points.
2701  */
2702 static void
2703 vfs_free_addrlist(nep)
2704 	struct netexport *nep;
2705 {
2706 	int i;
2707 	struct radix_node_head *rnh;
2708 
2709 	for (i = 0; i <= AF_MAX; i++)
2710 		if ((rnh = nep->ne_rtable[i])) {
2711 			(*rnh->rnh_walktree) (rnh, vfs_free_netcred,
2712 			    (caddr_t) rnh);
2713 			free((caddr_t) rnh, M_RTABLE);
2714 			nep->ne_rtable[i] = 0;
2715 		}
2716 }
2717 
2718 int
2719 vfs_export(mp, nep, argp)
2720 	struct mount *mp;
2721 	struct netexport *nep;
2722 	struct export_args *argp;
2723 {
2724 	int error;
2725 
2726 	if (argp->ex_flags & MNT_DELEXPORT) {
2727 		if (mp->mnt_flag & MNT_EXPUBLIC) {
2728 			vfs_setpublicfs(NULL, NULL, NULL);
2729 			mp->mnt_flag &= ~MNT_EXPUBLIC;
2730 		}
2731 		vfs_free_addrlist(nep);
2732 		mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
2733 	}
2734 	if (argp->ex_flags & MNT_EXPORTED) {
2735 		if (argp->ex_flags & MNT_EXPUBLIC) {
2736 			if ((error = vfs_setpublicfs(mp, nep, argp)) != 0)
2737 				return (error);
2738 			mp->mnt_flag |= MNT_EXPUBLIC;
2739 		}
2740 		if ((error = vfs_hang_addrlist(mp, nep, argp)))
2741 			return (error);
2742 		mp->mnt_flag |= MNT_EXPORTED;
2743 	}
2744 	return (0);
2745 }
2746 
2747 
2748 /*
2749  * Set the publicly exported filesystem (WebNFS). Currently, only
2750  * one public filesystem is possible in the spec (RFC 2054 and 2055)
2751  */
2752 int
2753 vfs_setpublicfs(mp, nep, argp)
2754 	struct mount *mp;
2755 	struct netexport *nep;
2756 	struct export_args *argp;
2757 {
2758 	int error;
2759 	struct vnode *rvp;
2760 	char *cp;
2761 
2762 	/*
2763 	 * mp == NULL -> invalidate the current info, the FS is
2764 	 * no longer exported. May be called from either vfs_export
2765 	 * or unmount, so check if it hasn't already been done.
2766 	 */
2767 	if (mp == NULL) {
2768 		if (nfs_pub.np_valid) {
2769 			nfs_pub.np_valid = 0;
2770 			if (nfs_pub.np_index != NULL) {
2771 				FREE(nfs_pub.np_index, M_TEMP);
2772 				nfs_pub.np_index = NULL;
2773 			}
2774 		}
2775 		return (0);
2776 	}
2777 
2778 	/*
2779 	 * Only one allowed at a time.
2780 	 */
2781 	if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
2782 		return (EBUSY);
2783 
2784 	/*
2785 	 * Get real filehandle for root of exported FS.
2786 	 */
2787 	bzero((caddr_t)&nfs_pub.np_handle, sizeof(nfs_pub.np_handle));
2788 	nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid;
2789 
2790 	if ((error = VFS_ROOT(mp, &rvp)))
2791 		return (error);
2792 
2793 	if ((error = VFS_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid)))
2794 		return (error);
2795 
2796 	vput(rvp);
2797 
2798 	/*
2799 	 * If an indexfile was specified, pull it in.
2800 	 */
2801 	if (argp->ex_indexfile != NULL) {
2802 		MALLOC(nfs_pub.np_index, char *, MAXNAMLEN + 1, M_TEMP,
2803 		    M_WAITOK);
2804 		error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
2805 		    MAXNAMLEN, (size_t *)0);
2806 		if (!error) {
2807 			/*
2808 			 * Check for illegal filenames.
2809 			 */
2810 			for (cp = nfs_pub.np_index; *cp; cp++) {
2811 				if (*cp == '/') {
2812 					error = EINVAL;
2813 					break;
2814 				}
2815 			}
2816 		}
2817 		if (error) {
2818 			FREE(nfs_pub.np_index, M_TEMP);
2819 			return (error);
2820 		}
2821 	}
2822 
2823 	nfs_pub.np_mount = mp;
2824 	nfs_pub.np_valid = 1;
2825 	return (0);
2826 }
2827 
2828 struct netcred *
2829 vfs_export_lookup(mp, nep, nam)
2830 	struct mount *mp;
2831 	struct netexport *nep;
2832 	struct sockaddr *nam;
2833 {
2834 	struct netcred *np;
2835 	struct radix_node_head *rnh;
2836 	struct sockaddr *saddr;
2837 
2838 	np = NULL;
2839 	if (mp->mnt_flag & MNT_EXPORTED) {
2840 		/*
2841 		 * Lookup in the export list first.
2842 		 */
2843 		if (nam != NULL) {
2844 			saddr = nam;
2845 			rnh = nep->ne_rtable[saddr->sa_family];
2846 			if (rnh != NULL) {
2847 				np = (struct netcred *)
2848 					(*rnh->rnh_matchaddr)((caddr_t)saddr,
2849 							      rnh);
2850 				if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
2851 					np = NULL;
2852 			}
2853 		}
2854 		/*
2855 		 * If no address match, use the default if it exists.
2856 		 */
2857 		if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
2858 			np = &nep->ne_defexported;
2859 	}
2860 	return (np);
2861 }
2862 
2863 /*
2864  * perform msync on all vnodes under a mount point.  The mount point must
2865  * be locked.  This code is also responsible for lazy-freeing unreferenced
2866  * vnodes whos VM objects no longer contain pages.
2867  *
2868  * NOTE: MNT_WAIT still skips vnodes in the VXLOCK state.
2869  */
2870 static int vfs_msync_scan1(struct mount *mp, struct vnode *vp, void *data);
2871 static int vfs_msync_scan2(struct mount *mp, struct vnode *vp,
2872 				lwkt_tokref_t vlock, void *data);
2873 
2874 void
2875 vfs_msync(struct mount *mp, int flags)
2876 {
2877 	vmntvnodescan(mp, vfs_msync_scan1, vfs_msync_scan2, (void *)flags);
2878 }
2879 
2880 /*
2881  * scan1 is a fast pre-check.  There could be hundreds of thousands of
2882  * vnodes, we cannot afford to do anything heavy weight until we have a
2883  * fairly good indication that there is work to do.
2884  */
2885 static
2886 int
2887 vfs_msync_scan1(struct mount *mp, struct vnode *vp, void *data)
2888 {
2889 	int flags = (int)data;
2890 
2891 	if ((vp->v_flag & VXLOCK) == 0) {
2892 		if (VSHOULDFREE(vp))
2893 			return(0);
2894 		if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
2895 		    (vp->v_flag & VOBJDIRTY) &&
2896 		    (flags == MNT_WAIT || VOP_ISLOCKED(vp, NULL) == 0)) {
2897 			return(0);
2898 		}
2899 	}
2900 	return(-1);
2901 }
2902 
2903 static
2904 int
2905 vfs_msync_scan2(struct mount *mp, struct vnode *vp, lwkt_tokref_t vlock, void *data)
2906 {
2907 	vm_object_t obj;
2908 	int error;
2909 	int flags = (int)data;
2910 
2911 	if (vp->v_flag & VXLOCK)
2912 		return(0);
2913 
2914 	if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
2915 	    (vp->v_flag & VOBJDIRTY) &&
2916 	    (flags == MNT_WAIT || VOP_ISLOCKED(vp, NULL) == 0)) {
2917 		error = vget(vp, vlock, LK_EXCLUSIVE | LK_RETRY | LK_NOOBJ | LK_INTERLOCK, curthread);
2918 		if (error == 0) {
2919 			if (VOP_GETVOBJECT(vp, &obj) == 0) {
2920 				vm_object_page_clean(obj, 0, 0,
2921 				 flags == MNT_WAIT ? OBJPC_SYNC : OBJPC_NOSYNC);
2922 			}
2923 			vput(vp);
2924 		}
2925 		return(0);
2926 	}
2927 	vmaybefree(vp);
2928 	lwkt_reltoken(vlock);
2929 	return(0);
2930 }
2931 
2932 /*
2933  * Create the VM object needed for VMIO and mmap support.  This
2934  * is done for all VREG files in the system.  Some filesystems might
2935  * afford the additional metadata buffering capability of the
2936  * VMIO code by making the device node be VMIO mode also.
2937  *
2938  * vp must be locked when vfs_object_create is called.
2939  */
2940 int
2941 vfs_object_create(struct vnode *vp, struct thread *td)
2942 {
2943 	return (VOP_CREATEVOBJECT(vp, td));
2944 }
2945 
2946 /*
2947  * NOTE: the vnode interlock must be held during the call.  We have to recheck
2948  * the VFREE flag since the vnode may have been removed from the free list
2949  * while we were blocked on vnode_free_list_token.  The use or hold count
2950  * must have already been bumped by the caller.
2951  */
2952 static void
2953 vbusy(struct vnode *vp)
2954 {
2955 	lwkt_tokref ilock;
2956 
2957 	lwkt_gettoken(&ilock, &vnode_free_list_token);
2958 	if ((vp->v_flag & VFREE) != 0) {
2959 	    TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
2960 	    freevnodes--;
2961 	    vp->v_flag &= ~(VFREE|VAGE);
2962 	}
2963 	lwkt_reltoken(&ilock);
2964 }
2965 
2966 /*
2967  * NOTE: the vnode interlock must be held during the call.  The use or hold
2968  * count must have already been bumped by the caller.  We use a VINFREE to
2969  * interlock against other calls to vfree() which might occur while we
2970  * are blocked.  The vnode cannot be reused until it has actually been
2971  * placed on the free list, so there are no other races even though the
2972  * use and hold counts are 0.
2973  */
2974 static void
2975 vfree(struct vnode *vp)
2976 {
2977 	lwkt_tokref ilock;
2978 
2979 	if ((vp->v_flag & VINFREE) == 0) {
2980 		vp->v_flag |= VINFREE;
2981 		lwkt_gettoken(&ilock, &vnode_free_list_token); /* can block */
2982 		KASSERT((vp->v_flag & VFREE) == 0, ("vnode already free"));
2983 		if (vp->v_flag & VAGE) {
2984 			TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
2985 		} else {
2986 			TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
2987 		}
2988 		freevnodes++;
2989 		vp->v_flag &= ~(VAGE|VINFREE);
2990 		vp->v_flag |= VFREE;
2991 		lwkt_reltoken(&ilock);	/* can block */
2992 	}
2993 }
2994 
2995 
2996 /*
2997  * Record a process's interest in events which might happen to
2998  * a vnode.  Because poll uses the historic select-style interface
2999  * internally, this routine serves as both the ``check for any
3000  * pending events'' and the ``record my interest in future events''
3001  * functions.  (These are done together, while the lock is held,
3002  * to avoid race conditions.)
3003  */
3004 int
3005 vn_pollrecord(struct vnode *vp, struct thread *td, int events)
3006 {
3007 	lwkt_tokref ilock;
3008 
3009 	lwkt_gettoken(&ilock, &vp->v_pollinfo.vpi_token);
3010 	if (vp->v_pollinfo.vpi_revents & events) {
3011 		/*
3012 		 * This leaves events we are not interested
3013 		 * in available for the other process which
3014 		 * which presumably had requested them
3015 		 * (otherwise they would never have been
3016 		 * recorded).
3017 		 */
3018 		events &= vp->v_pollinfo.vpi_revents;
3019 		vp->v_pollinfo.vpi_revents &= ~events;
3020 
3021 		lwkt_reltoken(&ilock);
3022 		return events;
3023 	}
3024 	vp->v_pollinfo.vpi_events |= events;
3025 	selrecord(td, &vp->v_pollinfo.vpi_selinfo);
3026 	lwkt_reltoken(&ilock);
3027 	return 0;
3028 }
3029 
3030 /*
3031  * Note the occurrence of an event.  If the VN_POLLEVENT macro is used,
3032  * it is possible for us to miss an event due to race conditions, but
3033  * that condition is expected to be rare, so for the moment it is the
3034  * preferred interface.
3035  */
3036 void
3037 vn_pollevent(vp, events)
3038 	struct vnode *vp;
3039 	short events;
3040 {
3041 	lwkt_tokref ilock;
3042 
3043 	lwkt_gettoken(&ilock, &vp->v_pollinfo.vpi_token);
3044 	if (vp->v_pollinfo.vpi_events & events) {
3045 		/*
3046 		 * We clear vpi_events so that we don't
3047 		 * call selwakeup() twice if two events are
3048 		 * posted before the polling process(es) is
3049 		 * awakened.  This also ensures that we take at
3050 		 * most one selwakeup() if the polling process
3051 		 * is no longer interested.  However, it does
3052 		 * mean that only one event can be noticed at
3053 		 * a time.  (Perhaps we should only clear those
3054 		 * event bits which we note?) XXX
3055 		 */
3056 		vp->v_pollinfo.vpi_events = 0;	/* &= ~events ??? */
3057 		vp->v_pollinfo.vpi_revents |= events;
3058 		selwakeup(&vp->v_pollinfo.vpi_selinfo);
3059 	}
3060 	lwkt_reltoken(&ilock);
3061 }
3062 
3063 /*
3064  * Wake up anyone polling on vp because it is being revoked.
3065  * This depends on dead_poll() returning POLLHUP for correct
3066  * behavior.
3067  */
3068 void
3069 vn_pollgone(vp)
3070 	struct vnode *vp;
3071 {
3072 	lwkt_tokref ilock;
3073 
3074 	lwkt_gettoken(&ilock, &vp->v_pollinfo.vpi_token);
3075 	if (vp->v_pollinfo.vpi_events) {
3076 		vp->v_pollinfo.vpi_events = 0;
3077 		selwakeup(&vp->v_pollinfo.vpi_selinfo);
3078 	}
3079 	lwkt_reltoken(&ilock);
3080 }
3081 
3082 
3083 
3084 /*
3085  * Routine to create and manage a filesystem syncer vnode.
3086  */
3087 #define sync_close ((int (*) (struct  vop_close_args *))nullop)
3088 static int	sync_fsync (struct  vop_fsync_args *);
3089 static int	sync_inactive (struct  vop_inactive_args *);
3090 static int	sync_reclaim  (struct  vop_reclaim_args *);
3091 #define sync_lock ((int (*) (struct  vop_lock_args *))vop_nolock)
3092 #define sync_unlock ((int (*) (struct  vop_unlock_args *))vop_nounlock)
3093 static int	sync_print (struct vop_print_args *);
3094 #define sync_islocked ((int(*) (struct vop_islocked_args *))vop_noislocked)
3095 
3096 static vop_t **sync_vnodeop_p;
3097 static struct vnodeopv_entry_desc sync_vnodeop_entries[] = {
3098 	{ &vop_default_desc,	(vop_t *) vop_eopnotsupp },
3099 	{ &vop_close_desc,	(vop_t *) sync_close },		/* close */
3100 	{ &vop_fsync_desc,	(vop_t *) sync_fsync },		/* fsync */
3101 	{ &vop_inactive_desc,	(vop_t *) sync_inactive },	/* inactive */
3102 	{ &vop_reclaim_desc,	(vop_t *) sync_reclaim },	/* reclaim */
3103 	{ &vop_lock_desc,	(vop_t *) sync_lock },		/* lock */
3104 	{ &vop_unlock_desc,	(vop_t *) sync_unlock },	/* unlock */
3105 	{ &vop_print_desc,	(vop_t *) sync_print },		/* print */
3106 	{ &vop_islocked_desc,	(vop_t *) sync_islocked },	/* islocked */
3107 	{ NULL, NULL }
3108 };
3109 static struct vnodeopv_desc sync_vnodeop_opv_desc =
3110 	{ &sync_vnodeop_p, sync_vnodeop_entries };
3111 
3112 VNODEOP_SET(sync_vnodeop_opv_desc);
3113 
3114 /*
3115  * Create a new filesystem syncer vnode for the specified mount point.
3116  * This vnode is placed on the worklist and is responsible for sync'ing
3117  * the filesystem.
3118  *
3119  * NOTE: read-only mounts are also placed on the worklist.  The filesystem
3120  * sync code is also responsible for cleaning up vnodes.
3121  */
3122 int
3123 vfs_allocate_syncvnode(struct mount *mp)
3124 {
3125 	struct vnode *vp;
3126 	static long start, incr, next;
3127 	int error;
3128 
3129 	/* Allocate a new vnode */
3130 	if ((error = getnewvnode(VT_VFS, mp, sync_vnodeop_p, &vp)) != 0) {
3131 		mp->mnt_syncer = NULL;
3132 		return (error);
3133 	}
3134 	vp->v_type = VNON;
3135 	/*
3136 	 * Place the vnode onto the syncer worklist. We attempt to
3137 	 * scatter them about on the list so that they will go off
3138 	 * at evenly distributed times even if all the filesystems
3139 	 * are mounted at once.
3140 	 */
3141 	next += incr;
3142 	if (next == 0 || next > syncer_maxdelay) {
3143 		start /= 2;
3144 		incr /= 2;
3145 		if (start == 0) {
3146 			start = syncer_maxdelay / 2;
3147 			incr = syncer_maxdelay;
3148 		}
3149 		next = start;
3150 	}
3151 	vn_syncer_add_to_worklist(vp, syncdelay > 0 ? next % syncdelay : 0);
3152 	mp->mnt_syncer = vp;
3153 	return (0);
3154 }
3155 
3156 /*
3157  * Do a lazy sync of the filesystem.
3158  */
3159 static int
3160 sync_fsync(ap)
3161 	struct vop_fsync_args /* {
3162 		struct vnode *a_vp;
3163 		struct ucred *a_cred;
3164 		int a_waitfor;
3165 		struct thread *a_td;
3166 	} */ *ap;
3167 {
3168 	struct vnode *syncvp = ap->a_vp;
3169 	struct mount *mp = syncvp->v_mount;
3170 	struct thread *td = ap->a_td;
3171 	lwkt_tokref ilock;
3172 	int asyncflag;
3173 
3174 	/*
3175 	 * We only need to do something if this is a lazy evaluation.
3176 	 */
3177 	if (ap->a_waitfor != MNT_LAZY)
3178 		return (0);
3179 
3180 	/*
3181 	 * Move ourselves to the back of the sync list.
3182 	 */
3183 	vn_syncer_add_to_worklist(syncvp, syncdelay);
3184 
3185 	/*
3186 	 * Walk the list of vnodes pushing all that are dirty and
3187 	 * not already on the sync list, and freeing vnodes which have
3188 	 * no refs and whos VM objects are empty.  vfs_msync() handles
3189 	 * the VM issues and must be called whether the mount is readonly
3190 	 * or not.
3191 	 */
3192 	lwkt_gettoken(&ilock, &mountlist_token);
3193 	if (vfs_busy(mp, LK_EXCLUSIVE | LK_NOWAIT, &ilock, td) != 0) {
3194 		lwkt_reltoken(&ilock);
3195 		return (0);
3196 	}
3197 	if (mp->mnt_flag & MNT_RDONLY) {
3198 		vfs_msync(mp, MNT_NOWAIT);
3199 	} else {
3200 		asyncflag = mp->mnt_flag & MNT_ASYNC;
3201 		mp->mnt_flag &= ~MNT_ASYNC;	/* ZZZ hack */
3202 		vfs_msync(mp, MNT_NOWAIT);
3203 		VFS_SYNC(mp, MNT_LAZY, td);
3204 		if (asyncflag)
3205 			mp->mnt_flag |= MNT_ASYNC;
3206 	}
3207 	vfs_unbusy(mp, td);
3208 	return (0);
3209 }
3210 
3211 /*
3212  * The syncer vnode is no referenced.
3213  */
3214 static int
3215 sync_inactive(ap)
3216 	struct vop_inactive_args /* {
3217 		struct vnode *a_vp;
3218 		struct proc *a_p;
3219 	} */ *ap;
3220 {
3221 
3222 	vgone(ap->a_vp);
3223 	return (0);
3224 }
3225 
3226 /*
3227  * The syncer vnode is no longer needed and is being decommissioned.
3228  *
3229  * Modifications to the worklist must be protected at splbio().
3230  */
3231 static int
3232 sync_reclaim(ap)
3233 	struct vop_reclaim_args /* {
3234 		struct vnode *a_vp;
3235 	} */ *ap;
3236 {
3237 	struct vnode *vp = ap->a_vp;
3238 	int s;
3239 
3240 	s = splbio();
3241 	vp->v_mount->mnt_syncer = NULL;
3242 	if (vp->v_flag & VONWORKLST) {
3243 		LIST_REMOVE(vp, v_synclist);
3244 		vp->v_flag &= ~VONWORKLST;
3245 	}
3246 	splx(s);
3247 
3248 	return (0);
3249 }
3250 
3251 /*
3252  * Print out a syncer vnode.
3253  */
3254 static int
3255 sync_print(ap)
3256 	struct vop_print_args /* {
3257 		struct vnode *a_vp;
3258 	} */ *ap;
3259 {
3260 	struct vnode *vp = ap->a_vp;
3261 
3262 	printf("syncer vnode");
3263 	if (vp->v_vnlock != NULL)
3264 		lockmgr_printinfo(vp->v_vnlock);
3265 	printf("\n");
3266 	return (0);
3267 }
3268 
3269 /*
3270  * extract the dev_t from a VBLK or VCHR
3271  */
3272 dev_t
3273 vn_todev(vp)
3274 	struct vnode *vp;
3275 {
3276 	if (vp->v_type != VBLK && vp->v_type != VCHR)
3277 		return (NODEV);
3278 	return (vp->v_rdev);
3279 }
3280 
3281 /*
3282  * Check if vnode represents a disk device
3283  */
3284 int
3285 vn_isdisk(vp, errp)
3286 	struct vnode *vp;
3287 	int *errp;
3288 {
3289 	if (vp->v_type != VBLK && vp->v_type != VCHR) {
3290 		if (errp != NULL)
3291 			*errp = ENOTBLK;
3292 		return (0);
3293 	}
3294 	if (vp->v_rdev == NULL) {
3295 		if (errp != NULL)
3296 			*errp = ENXIO;
3297 		return (0);
3298 	}
3299 	if (!dev_dport(vp->v_rdev)) {
3300 		if (errp != NULL)
3301 			*errp = ENXIO;
3302 		return (0);
3303 	}
3304 	if (!(dev_dflags(vp->v_rdev) & D_DISK)) {
3305 		if (errp != NULL)
3306 			*errp = ENOTBLK;
3307 		return (0);
3308 	}
3309 	if (errp != NULL)
3310 		*errp = 0;
3311 	return (1);
3312 }
3313 
3314 void
3315 NDFREE(ndp, flags)
3316      struct nameidata *ndp;
3317      const uint flags;
3318 {
3319 	if (!(flags & NDF_NO_FREE_PNBUF) &&
3320 	    (ndp->ni_cnd.cn_flags & CNP_HASBUF)) {
3321 		zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
3322 		ndp->ni_cnd.cn_flags &= ~CNP_HASBUF;
3323 	}
3324 	if (!(flags & NDF_NO_DNCP_RELE) &&
3325 	    (ndp->ni_cnd.cn_flags & CNP_WANTDNCP) &&
3326 	    ndp->ni_dncp) {
3327 		cache_drop(ndp->ni_dncp);
3328 		ndp->ni_dncp = NULL;
3329 	}
3330 	if (!(flags & NDF_NO_NCP_RELE) &&
3331 	    (ndp->ni_cnd.cn_flags & CNP_WANTNCP) &&
3332 	    ndp->ni_ncp) {
3333 		cache_drop(ndp->ni_ncp);
3334 		ndp->ni_ncp = NULL;
3335 	}
3336 	if (!(flags & NDF_NO_DVP_UNLOCK) &&
3337 	    (ndp->ni_cnd.cn_flags & CNP_LOCKPARENT) &&
3338 	    ndp->ni_dvp != ndp->ni_vp) {
3339 		VOP_UNLOCK(ndp->ni_dvp, NULL, 0, ndp->ni_cnd.cn_td);
3340 	}
3341 	if (!(flags & NDF_NO_DVP_RELE) &&
3342 	    (ndp->ni_cnd.cn_flags & (CNP_LOCKPARENT|CNP_WANTPARENT))) {
3343 		vrele(ndp->ni_dvp);
3344 		ndp->ni_dvp = NULL;
3345 	}
3346 	if (!(flags & NDF_NO_VP_UNLOCK) &&
3347 	    (ndp->ni_cnd.cn_flags & CNP_LOCKLEAF) && ndp->ni_vp) {
3348 		VOP_UNLOCK(ndp->ni_vp, NULL, 0, ndp->ni_cnd.cn_td);
3349 	}
3350 	if (!(flags & NDF_NO_VP_RELE) &&
3351 	    ndp->ni_vp) {
3352 		vrele(ndp->ni_vp);
3353 		ndp->ni_vp = NULL;
3354 	}
3355 	if (!(flags & NDF_NO_STARTDIR_RELE) &&
3356 	    (ndp->ni_cnd.cn_flags & CNP_SAVESTART)) {
3357 		vrele(ndp->ni_startdir);
3358 		ndp->ni_startdir = NULL;
3359 	}
3360 }
3361 
3362 #ifdef DEBUG_VFS_LOCKS
3363 
3364 void
3365 assert_vop_locked(struct vnode *vp, const char *str)
3366 {
3367 
3368 	if (vp && IS_LOCKING_VFS(vp) && !VOP_ISLOCKED(vp, NULL)) {
3369 		panic("%s: %p is not locked shared but should be", str, vp);
3370 	}
3371 }
3372 
3373 void
3374 assert_vop_unlocked(struct vnode *vp, const char *str)
3375 {
3376 
3377 	if (vp && IS_LOCKING_VFS(vp)) {
3378 		if (VOP_ISLOCKED(vp, curthread) == LK_EXCLUSIVE) {
3379 			panic("%s: %p is locked but should not be", str, vp);
3380 		}
3381 	}
3382 }
3383 
3384 #endif
3385