xref: /freebsd/sys/kern/vfs_subr.c (revision a14a0223ae1b172e96dd2a1d849e22026a98b692)
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$
40  */
41 
42 /*
43  * External virtual filesystem routines
44  */
45 #include "opt_ddb.h"
46 
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/fcntl.h>
50 #include <sys/kernel.h>
51 #include <sys/proc.h>
52 #include <sys/kthread.h>
53 #include <sys/malloc.h>
54 #include <sys/mount.h>
55 #include <sys/socket.h>
56 #include <sys/vnode.h>
57 #include <sys/stat.h>
58 #include <sys/buf.h>
59 #include <sys/domain.h>
60 #include <sys/dirent.h>
61 #include <sys/vmmeter.h>
62 #include <sys/conf.h>
63 
64 #include <machine/limits.h>
65 
66 #include <vm/vm.h>
67 #include <vm/vm_object.h>
68 #include <vm/vm_extern.h>
69 #include <vm/pmap.h>
70 #include <vm/vm_map.h>
71 #include <vm/vm_page.h>
72 #include <vm/vm_pager.h>
73 #include <vm/vnode_pager.h>
74 #include <vm/vm_zone.h>
75 #include <sys/sysctl.h>
76 
77 static MALLOC_DEFINE(M_NETADDR, "Export Host", "Export host address structure");
78 
79 static void	insmntque __P((struct vnode *vp, struct mount *mp));
80 static void	vclean __P((struct vnode *vp, int flags, struct proc *p));
81 static void	vfree __P((struct vnode *));
82 static void	vgonel __P((struct vnode *vp, struct proc *p));
83 static unsigned long	numvnodes;
84 SYSCTL_INT(_debug, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0, "");
85 
86 enum vtype iftovt_tab[16] = {
87 	VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
88 	VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
89 };
90 int vttoif_tab[9] = {
91 	0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
92 	S_IFSOCK, S_IFIFO, S_IFMT,
93 };
94 
95 static TAILQ_HEAD(freelst, vnode) vnode_free_list;	/* vnode free list */
96 struct tobefreelist vnode_tobefree_list;	/* vnode free list */
97 
98 static u_long wantfreevnodes = 25;
99 SYSCTL_INT(_debug, OID_AUTO, wantfreevnodes, CTLFLAG_RW, &wantfreevnodes, 0, "");
100 static u_long freevnodes = 0;
101 SYSCTL_INT(_debug, OID_AUTO, freevnodes, CTLFLAG_RD, &freevnodes, 0, "");
102 
103 static int reassignbufcalls;
104 SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW, &reassignbufcalls, 0, "");
105 static int reassignbufloops;
106 SYSCTL_INT(_vfs, OID_AUTO, reassignbufloops, CTLFLAG_RW, &reassignbufloops, 0, "");
107 static int reassignbufsortgood;
108 SYSCTL_INT(_vfs, OID_AUTO, reassignbufsortgood, CTLFLAG_RW, &reassignbufsortgood, 0, "");
109 static int reassignbufsortbad;
110 SYSCTL_INT(_vfs, OID_AUTO, reassignbufsortbad, CTLFLAG_RW, &reassignbufsortbad, 0, "");
111 static int reassignbufmethod = 1;
112 SYSCTL_INT(_vfs, OID_AUTO, reassignbufmethod, CTLFLAG_RW, &reassignbufmethod, 0, "");
113 
114 #ifdef ENABLE_VFS_IOOPT
115 int vfs_ioopt = 0;
116 SYSCTL_INT(_vfs, OID_AUTO, ioopt, CTLFLAG_RW, &vfs_ioopt, 0, "");
117 #endif
118 
119 struct mntlist mountlist;	/* mounted filesystem list */
120 struct simplelock mountlist_slock;
121 struct simplelock mntvnode_slock;
122 int	nfs_mount_type = -1;
123 #ifndef NULL_SIMPLELOCKS
124 static struct simplelock mntid_slock;
125 static struct simplelock vnode_free_list_slock;
126 static struct simplelock spechash_slock;
127 #endif
128 struct nfs_public nfs_pub;	/* publicly exported FS */
129 static vm_zone_t vnode_zone;
130 
131 /*
132  * The workitem queue.
133  */
134 #define SYNCER_MAXDELAY		32
135 static int syncer_maxdelay = SYNCER_MAXDELAY;	/* maximum delay time */
136 time_t syncdelay = 30;		/* max time to delay syncing data */
137 time_t filedelay = 30;		/* time to delay syncing files */
138 SYSCTL_INT(_kern, OID_AUTO, filedelay, CTLFLAG_RW, &filedelay, 0, "");
139 time_t dirdelay = 29;		/* time to delay syncing directories */
140 SYSCTL_INT(_kern, OID_AUTO, dirdelay, CTLFLAG_RW, &dirdelay, 0, "");
141 time_t metadelay = 28;		/* time to delay syncing metadata */
142 SYSCTL_INT(_kern, OID_AUTO, metadelay, CTLFLAG_RW, &metadelay, 0, "");
143 static int rushjob;			/* number of slots to run ASAP */
144 static int stat_rush_requests;	/* number of times I/O speeded up */
145 SYSCTL_INT(_debug, OID_AUTO, rush_requests, CTLFLAG_RW, &stat_rush_requests, 0, "");
146 
147 static int syncer_delayno = 0;
148 static long syncer_mask;
149 LIST_HEAD(synclist, vnode);
150 static struct synclist *syncer_workitem_pending;
151 
152 int desiredvnodes;
153 SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RW,
154     &desiredvnodes, 0, "Maximum number of vnodes");
155 
156 static void	vfs_free_addrlist __P((struct netexport *nep));
157 static int	vfs_free_netcred __P((struct radix_node *rn, void *w));
158 static int	vfs_hang_addrlist __P((struct mount *mp, struct netexport *nep,
159 				       struct export_args *argp));
160 
161 /*
162  * Initialize the vnode management data structures.
163  */
164 void
165 vntblinit()
166 {
167 
168 	desiredvnodes = maxproc + cnt.v_page_count / 4;
169 	simple_lock_init(&mntvnode_slock);
170 	simple_lock_init(&mntid_slock);
171 	simple_lock_init(&spechash_slock);
172 	TAILQ_INIT(&vnode_free_list);
173 	TAILQ_INIT(&vnode_tobefree_list);
174 	simple_lock_init(&vnode_free_list_slock);
175 	CIRCLEQ_INIT(&mountlist);
176 	vnode_zone = zinit("VNODE", sizeof (struct vnode), 0, 0, 5);
177 	/*
178 	 * Initialize the filesystem syncer.
179 	 */
180 	syncer_workitem_pending = hashinit(syncer_maxdelay, M_VNODE,
181 		&syncer_mask);
182 	syncer_maxdelay = syncer_mask + 1;
183 }
184 
185 /*
186  * Mark a mount point as busy. Used to synchronize access and to delay
187  * unmounting. Interlock is not released on failure.
188  */
189 int
190 vfs_busy(mp, flags, interlkp, p)
191 	struct mount *mp;
192 	int flags;
193 	struct simplelock *interlkp;
194 	struct proc *p;
195 {
196 	int lkflags;
197 
198 	if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
199 		if (flags & LK_NOWAIT)
200 			return (ENOENT);
201 		mp->mnt_kern_flag |= MNTK_MWAIT;
202 		if (interlkp) {
203 			simple_unlock(interlkp);
204 		}
205 		/*
206 		 * Since all busy locks are shared except the exclusive
207 		 * lock granted when unmounting, the only place that a
208 		 * wakeup needs to be done is at the release of the
209 		 * exclusive lock at the end of dounmount.
210 		 */
211 		tsleep((caddr_t)mp, PVFS, "vfs_busy", 0);
212 		if (interlkp) {
213 			simple_lock(interlkp);
214 		}
215 		return (ENOENT);
216 	}
217 	lkflags = LK_SHARED | LK_NOPAUSE;
218 	if (interlkp)
219 		lkflags |= LK_INTERLOCK;
220 	if (lockmgr(&mp->mnt_lock, lkflags, interlkp, p))
221 		panic("vfs_busy: unexpected lock failure");
222 	return (0);
223 }
224 
225 /*
226  * Free a busy filesystem.
227  */
228 void
229 vfs_unbusy(mp, p)
230 	struct mount *mp;
231 	struct proc *p;
232 {
233 
234 	lockmgr(&mp->mnt_lock, LK_RELEASE, NULL, p);
235 }
236 
237 /*
238  * Lookup a filesystem type, and if found allocate and initialize
239  * a mount structure for it.
240  *
241  * Devname is usually updated by mount(8) after booting.
242  */
243 int
244 vfs_rootmountalloc(fstypename, devname, mpp)
245 	char *fstypename;
246 	char *devname;
247 	struct mount **mpp;
248 {
249 	struct proc *p = curproc;	/* XXX */
250 	struct vfsconf *vfsp;
251 	struct mount *mp;
252 
253 	if (fstypename == NULL)
254 		return (ENODEV);
255 	for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
256 		if (!strcmp(vfsp->vfc_name, fstypename))
257 			break;
258 	if (vfsp == NULL)
259 		return (ENODEV);
260 	mp = malloc((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK);
261 	bzero((char *)mp, (u_long)sizeof(struct mount));
262 	lockinit(&mp->mnt_lock, PVFS, "vfslock", 0, LK_NOPAUSE);
263 	(void)vfs_busy(mp, LK_NOWAIT, 0, p);
264 	LIST_INIT(&mp->mnt_vnodelist);
265 	mp->mnt_vfc = vfsp;
266 	mp->mnt_op = vfsp->vfc_vfsops;
267 	mp->mnt_flag = MNT_RDONLY;
268 	mp->mnt_vnodecovered = NULLVP;
269 	vfsp->vfc_refcount++;
270 	mp->mnt_iosize_max = DFLTPHYS;
271 	mp->mnt_stat.f_type = vfsp->vfc_typenum;
272 	mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
273 	strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
274 	mp->mnt_stat.f_mntonname[0] = '/';
275 	mp->mnt_stat.f_mntonname[1] = 0;
276 	(void) copystr(devname, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, 0);
277 	*mpp = mp;
278 	return (0);
279 }
280 
281 /*
282  * Find an appropriate filesystem to use for the root. If a filesystem
283  * has not been preselected, walk through the list of known filesystems
284  * trying those that have mountroot routines, and try them until one
285  * works or we have tried them all.
286  */
287 #ifdef notdef	/* XXX JH */
288 int
289 lite2_vfs_mountroot()
290 {
291 	struct vfsconf *vfsp;
292 	extern int (*lite2_mountroot) __P((void));
293 	int error;
294 
295 	if (lite2_mountroot != NULL)
296 		return ((*lite2_mountroot)());
297 	for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
298 		if (vfsp->vfc_mountroot == NULL)
299 			continue;
300 		if ((error = (*vfsp->vfc_mountroot)()) == 0)
301 			return (0);
302 		printf("%s_mountroot failed: %d\n", vfsp->vfc_name, error);
303 	}
304 	return (ENODEV);
305 }
306 #endif
307 
308 /*
309  * Lookup a mount point by filesystem identifier.
310  */
311 struct mount *
312 vfs_getvfs(fsid)
313 	fsid_t *fsid;
314 {
315 	register struct mount *mp;
316 
317 	simple_lock(&mountlist_slock);
318 	for (mp = mountlist.cqh_first; mp != (void *)&mountlist;
319 	    mp = mp->mnt_list.cqe_next) {
320 		if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
321 		    mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
322 			simple_unlock(&mountlist_slock);
323 			return (mp);
324 	    }
325 	}
326 	simple_unlock(&mountlist_slock);
327 	return ((struct mount *) 0);
328 }
329 
330 /*
331  * Get a new unique fsid
332  *
333  * Keep in mind that several mounts may be running in parallel,
334  * so always increment mntid_base even if lower numbers are available.
335  */
336 
337 static u_short mntid_base;
338 
339 void
340 vfs_getnewfsid(mp)
341 	struct mount *mp;
342 {
343 	fsid_t tfsid;
344 	int mtype;
345 
346 	simple_lock(&mntid_slock);
347 
348 	mtype = mp->mnt_vfc->vfc_typenum;
349 	for (;;) {
350 		tfsid.val[0] = makeudev(255, mtype + (mntid_base << 16));
351 		tfsid.val[1] = mtype;
352 		++mntid_base;
353 		if (vfs_getvfs(&tfsid) == NULL)
354 			break;
355 	}
356 
357 	mp->mnt_stat.f_fsid.val[0] = tfsid.val[0];
358 	mp->mnt_stat.f_fsid.val[1] = tfsid.val[1];
359 
360 	simple_unlock(&mntid_slock);
361 }
362 
363 /*
364  * Get what should become the root fsid.
365  *
366  * This is somewhat of a hack.  If the rootdev is not known we
367  * assume that vfs_getnewfsid() will be called momentarily to
368  * allocate it, and we return what vfs_getnewfsid() will return.
369  */
370 
371 dev_t
372 vfs_getrootfsid(struct mount *mp)
373 {
374 	int mtype;
375 
376 	mtype = mp->mnt_vfc->vfc_typenum;
377 	return(makedev(255, mtype + (mntid_base << 16)));
378 }
379 
380 /*
381  * Knob to control the precision of file timestamps:
382  *
383  *   0 = seconds only; nanoseconds zeroed.
384  *   1 = seconds and nanoseconds, accurate within 1/HZ.
385  *   2 = seconds and nanoseconds, truncated to microseconds.
386  * >=3 = seconds and nanoseconds, maximum precision.
387  */
388 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC };
389 
390 static int timestamp_precision = TSP_SEC;
391 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW,
392     &timestamp_precision, 0, "");
393 
394 /*
395  * Get a current timestamp.
396  */
397 void
398 vfs_timestamp(tsp)
399 	struct timespec *tsp;
400 {
401 	struct timeval tv;
402 
403 	switch (timestamp_precision) {
404 	case TSP_SEC:
405 		tsp->tv_sec = time_second;
406 		tsp->tv_nsec = 0;
407 		break;
408 	case TSP_HZ:
409 		getnanotime(tsp);
410 		break;
411 	case TSP_USEC:
412 		microtime(&tv);
413 		TIMEVAL_TO_TIMESPEC(&tv, tsp);
414 		break;
415 	case TSP_NSEC:
416 	default:
417 		nanotime(tsp);
418 		break;
419 	}
420 }
421 
422 /*
423  * Set vnode attributes to VNOVAL
424  */
425 void
426 vattr_null(vap)
427 	register struct vattr *vap;
428 {
429 
430 	vap->va_type = VNON;
431 	vap->va_size = VNOVAL;
432 	vap->va_bytes = VNOVAL;
433 	vap->va_mode = VNOVAL;
434 	vap->va_nlink = VNOVAL;
435 	vap->va_uid = VNOVAL;
436 	vap->va_gid = VNOVAL;
437 	vap->va_fsid = VNOVAL;
438 	vap->va_fileid = VNOVAL;
439 	vap->va_blocksize = VNOVAL;
440 	vap->va_rdev = VNOVAL;
441 	vap->va_atime.tv_sec = VNOVAL;
442 	vap->va_atime.tv_nsec = VNOVAL;
443 	vap->va_mtime.tv_sec = VNOVAL;
444 	vap->va_mtime.tv_nsec = VNOVAL;
445 	vap->va_ctime.tv_sec = VNOVAL;
446 	vap->va_ctime.tv_nsec = VNOVAL;
447 	vap->va_flags = VNOVAL;
448 	vap->va_gen = VNOVAL;
449 	vap->va_vaflags = 0;
450 }
451 
452 /*
453  * Routines having to do with the management of the vnode table.
454  */
455 extern vop_t **dead_vnodeop_p;
456 
457 /*
458  * Return the next vnode from the free list.
459  */
460 int
461 getnewvnode(tag, mp, vops, vpp)
462 	enum vtagtype tag;
463 	struct mount *mp;
464 	vop_t **vops;
465 	struct vnode **vpp;
466 {
467 	int s;
468 	struct proc *p = curproc;	/* XXX */
469 	struct vnode *vp, *tvp, *nvp;
470 	vm_object_t object;
471 	TAILQ_HEAD(freelst, vnode) vnode_tmp_list;
472 
473 	/*
474 	 * We take the least recently used vnode from the freelist
475 	 * if we can get it and it has no cached pages, and no
476 	 * namecache entries are relative to it.
477 	 * Otherwise we allocate a new vnode
478 	 */
479 
480 	s = splbio();
481 	simple_lock(&vnode_free_list_slock);
482 	TAILQ_INIT(&vnode_tmp_list);
483 
484 	for (vp = TAILQ_FIRST(&vnode_tobefree_list); vp; vp = nvp) {
485 		nvp = TAILQ_NEXT(vp, v_freelist);
486 		TAILQ_REMOVE(&vnode_tobefree_list, vp, v_freelist);
487 		if (vp->v_flag & VAGE) {
488 			TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
489 		} else {
490 			TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
491 		}
492 		vp->v_flag &= ~(VTBFREE|VAGE);
493 		vp->v_flag |= VFREE;
494 		if (vp->v_usecount)
495 			panic("tobe free vnode isn't");
496 		freevnodes++;
497 	}
498 
499 	if (wantfreevnodes && freevnodes < wantfreevnodes) {
500 		vp = NULL;
501 	} else if (!wantfreevnodes && freevnodes <= desiredvnodes) {
502 		/*
503 		 * XXX: this is only here to be backwards compatible
504 		 */
505 		vp = NULL;
506 	} else {
507 		for (vp = TAILQ_FIRST(&vnode_free_list); vp; vp = nvp) {
508 			nvp = TAILQ_NEXT(vp, v_freelist);
509 			if (!simple_lock_try(&vp->v_interlock))
510 				continue;
511 			if (vp->v_usecount)
512 				panic("free vnode isn't");
513 
514 			object = vp->v_object;
515 			if (object && (object->resident_page_count || object->ref_count)) {
516 				printf("object inconsistant state: RPC: %d, RC: %d\n",
517 					object->resident_page_count, object->ref_count);
518 				/* Don't recycle if it's caching some pages */
519 				TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
520 				TAILQ_INSERT_TAIL(&vnode_tmp_list, vp, v_freelist);
521 				continue;
522 			} else if (LIST_FIRST(&vp->v_cache_src)) {
523 				/* Don't recycle if active in the namecache */
524 				simple_unlock(&vp->v_interlock);
525 				continue;
526 			} else {
527 				break;
528 			}
529 		}
530 	}
531 
532 	for (tvp = TAILQ_FIRST(&vnode_tmp_list); tvp; tvp = nvp) {
533 		nvp = TAILQ_NEXT(tvp, v_freelist);
534 		TAILQ_REMOVE(&vnode_tmp_list, tvp, v_freelist);
535 		TAILQ_INSERT_TAIL(&vnode_free_list, tvp, v_freelist);
536 		simple_unlock(&tvp->v_interlock);
537 	}
538 
539 	if (vp) {
540 		vp->v_flag |= VDOOMED;
541 		TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
542 		freevnodes--;
543 		simple_unlock(&vnode_free_list_slock);
544 		cache_purge(vp);
545 		vp->v_lease = NULL;
546 		if (vp->v_type != VBAD) {
547 			vgonel(vp, p);
548 		} else {
549 			simple_unlock(&vp->v_interlock);
550 		}
551 
552 #ifdef INVARIANTS
553 		{
554 			int s;
555 
556 			if (vp->v_data)
557 				panic("cleaned vnode isn't");
558 			s = splbio();
559 			if (vp->v_numoutput)
560 				panic("Clean vnode has pending I/O's");
561 			splx(s);
562 		}
563 #endif
564 		vp->v_flag = 0;
565 		vp->v_lastw = 0;
566 		vp->v_lasta = 0;
567 		vp->v_cstart = 0;
568 		vp->v_clen = 0;
569 		vp->v_socket = 0;
570 		vp->v_writecount = 0;	/* XXX */
571 	} else {
572 		simple_unlock(&vnode_free_list_slock);
573 		vp = (struct vnode *) zalloc(vnode_zone);
574 		bzero((char *) vp, sizeof *vp);
575 		simple_lock_init(&vp->v_interlock);
576 		vp->v_dd = vp;
577 		cache_purge(vp);
578 		LIST_INIT(&vp->v_cache_src);
579 		TAILQ_INIT(&vp->v_cache_dst);
580 		numvnodes++;
581 	}
582 
583 	TAILQ_INIT(&vp->v_cleanblkhd);
584 	TAILQ_INIT(&vp->v_dirtyblkhd);
585 	vp->v_type = VNON;
586 	vp->v_tag = tag;
587 	vp->v_op = vops;
588 	insmntque(vp, mp);
589 	*vpp = vp;
590 	vp->v_usecount = 1;
591 	vp->v_data = 0;
592 	splx(s);
593 
594 	vfs_object_create(vp, p, p->p_ucred);
595 	return (0);
596 }
597 
598 /*
599  * Move a vnode from one mount queue to another.
600  */
601 static void
602 insmntque(vp, mp)
603 	register struct vnode *vp;
604 	register struct mount *mp;
605 {
606 
607 	simple_lock(&mntvnode_slock);
608 	/*
609 	 * Delete from old mount point vnode list, if on one.
610 	 */
611 	if (vp->v_mount != NULL)
612 		LIST_REMOVE(vp, v_mntvnodes);
613 	/*
614 	 * Insert into list of vnodes for the new mount point, if available.
615 	 */
616 	if ((vp->v_mount = mp) == NULL) {
617 		simple_unlock(&mntvnode_slock);
618 		return;
619 	}
620 	LIST_INSERT_HEAD(&mp->mnt_vnodelist, vp, v_mntvnodes);
621 	simple_unlock(&mntvnode_slock);
622 }
623 
624 /*
625  * Update outstanding I/O count and do wakeup if requested.
626  */
627 void
628 vwakeup(bp)
629 	register struct buf *bp;
630 {
631 	register struct vnode *vp;
632 
633 	bp->b_flags &= ~B_WRITEINPROG;
634 	if ((vp = bp->b_vp)) {
635 		vp->v_numoutput--;
636 		if (vp->v_numoutput < 0)
637 			panic("vwakeup: neg numoutput");
638 		if ((vp->v_numoutput == 0) && (vp->v_flag & VBWAIT)) {
639 			vp->v_flag &= ~VBWAIT;
640 			wakeup((caddr_t) &vp->v_numoutput);
641 		}
642 	}
643 }
644 
645 /*
646  * Flush out and invalidate all buffers associated with a vnode.
647  * Called with the underlying object locked.
648  */
649 int
650 vinvalbuf(vp, flags, cred, p, slpflag, slptimeo)
651 	register struct vnode *vp;
652 	int flags;
653 	struct ucred *cred;
654 	struct proc *p;
655 	int slpflag, slptimeo;
656 {
657 	register struct buf *bp;
658 	struct buf *nbp, *blist;
659 	int s, error;
660 	vm_object_t object;
661 
662 	if (flags & V_SAVE) {
663 		s = splbio();
664 		while (vp->v_numoutput) {
665 			vp->v_flag |= VBWAIT;
666 			error = tsleep((caddr_t)&vp->v_numoutput,
667 			    slpflag | (PRIBIO + 1), "vinvlbuf", slptimeo);
668 			if (error) {
669 				splx(s);
670 				return (error);
671 			}
672 		}
673 		if (!TAILQ_EMPTY(&vp->v_dirtyblkhd)) {
674 			splx(s);
675 			if ((error = VOP_FSYNC(vp, cred, MNT_WAIT, p)) != 0)
676 				return (error);
677 			s = splbio();
678 			if (vp->v_numoutput > 0 ||
679 			    !TAILQ_EMPTY(&vp->v_dirtyblkhd))
680 				panic("vinvalbuf: dirty bufs");
681 		}
682 		splx(s);
683   	}
684 	s = splbio();
685 	for (;;) {
686 		blist = TAILQ_FIRST(&vp->v_cleanblkhd);
687 		if (!blist)
688 			blist = TAILQ_FIRST(&vp->v_dirtyblkhd);
689 		if (!blist)
690 			break;
691 
692 		for (bp = blist; bp; bp = nbp) {
693 			nbp = TAILQ_NEXT(bp, b_vnbufs);
694 			if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
695 				error = BUF_TIMELOCK(bp,
696 				    LK_EXCLUSIVE | LK_SLEEPFAIL,
697 				    "vinvalbuf", slpflag, slptimeo);
698 				if (error == ENOLCK)
699 					break;
700 				splx(s);
701 				return (error);
702 			}
703 			/*
704 			 * XXX Since there are no node locks for NFS, I
705 			 * believe there is a slight chance that a delayed
706 			 * write will occur while sleeping just above, so
707 			 * check for it.  Note that vfs_bio_awrite expects
708 			 * buffers to reside on a queue, while VOP_BWRITE and
709 			 * brelse do not.
710 			 */
711 			if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
712 				(flags & V_SAVE)) {
713 
714 				if (bp->b_vp == vp) {
715 					if (bp->b_flags & B_CLUSTEROK) {
716 						BUF_UNLOCK(bp);
717 						vfs_bio_awrite(bp);
718 					} else {
719 						bremfree(bp);
720 						bp->b_flags |= B_ASYNC;
721 						VOP_BWRITE(bp->b_vp, bp);
722 					}
723 				} else {
724 					bremfree(bp);
725 					(void) VOP_BWRITE(bp->b_vp, bp);
726 				}
727 				break;
728 			}
729 			bremfree(bp);
730 			bp->b_flags |= (B_INVAL | B_NOCACHE | B_RELBUF);
731 			bp->b_flags &= ~B_ASYNC;
732 			brelse(bp);
733 		}
734 	}
735 
736 	while (vp->v_numoutput > 0) {
737 		vp->v_flag |= VBWAIT;
738 		tsleep(&vp->v_numoutput, PVM, "vnvlbv", 0);
739 	}
740 
741 	splx(s);
742 
743 	/*
744 	 * Destroy the copy in the VM cache, too.
745 	 */
746 	simple_lock(&vp->v_interlock);
747 	object = vp->v_object;
748 	if (object != NULL) {
749 		vm_object_page_remove(object, 0, 0,
750 			(flags & V_SAVE) ? TRUE : FALSE);
751 	}
752 	simple_unlock(&vp->v_interlock);
753 
754 	if (!TAILQ_EMPTY(&vp->v_dirtyblkhd) || !TAILQ_EMPTY(&vp->v_cleanblkhd))
755 		panic("vinvalbuf: flush failed");
756 	return (0);
757 }
758 
759 /*
760  * Truncate a file's buffer and pages to a specified length.  This
761  * is in lieu of the old vinvalbuf mechanism, which performed unneeded
762  * sync activity.
763  */
764 int
765 vtruncbuf(vp, cred, p, length, blksize)
766 	register struct vnode *vp;
767 	struct ucred *cred;
768 	struct proc *p;
769 	off_t length;
770 	int blksize;
771 {
772 	register struct buf *bp;
773 	struct buf *nbp;
774 	int s, anyfreed;
775 	int trunclbn;
776 
777 	/*
778 	 * Round up to the *next* lbn.
779 	 */
780 	trunclbn = (length + blksize - 1) / blksize;
781 
782 	s = splbio();
783 restart:
784 	anyfreed = 1;
785 	for (;anyfreed;) {
786 		anyfreed = 0;
787 		for (bp = TAILQ_FIRST(&vp->v_cleanblkhd); bp; bp = nbp) {
788 			nbp = TAILQ_NEXT(bp, b_vnbufs);
789 			if (bp->b_lblkno >= trunclbn) {
790 				if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
791 					BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL);
792 					goto restart;
793 				} else {
794 					bremfree(bp);
795 					bp->b_flags |= (B_INVAL | B_RELBUF);
796 					bp->b_flags &= ~B_ASYNC;
797 					brelse(bp);
798 					anyfreed = 1;
799 				}
800 				if (nbp && (((nbp->b_xflags & B_VNCLEAN) == 0)||
801 					 (nbp->b_vp != vp) ||
802 					 (nbp->b_flags & B_DELWRI))) {
803 					goto restart;
804 				}
805 			}
806 		}
807 
808 		for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
809 			nbp = TAILQ_NEXT(bp, b_vnbufs);
810 			if (bp->b_lblkno >= trunclbn) {
811 				if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
812 					BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL);
813 					goto restart;
814 				} else {
815 					bremfree(bp);
816 					bp->b_flags |= (B_INVAL | B_RELBUF);
817 					bp->b_flags &= ~B_ASYNC;
818 					brelse(bp);
819 					anyfreed = 1;
820 				}
821 				if (nbp && (((nbp->b_xflags & B_VNDIRTY) == 0)||
822 					 (nbp->b_vp != vp) ||
823 					 (nbp->b_flags & B_DELWRI) == 0)) {
824 					goto restart;
825 				}
826 			}
827 		}
828 	}
829 
830 	if (length > 0) {
831 restartsync:
832 		for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
833 			nbp = TAILQ_NEXT(bp, b_vnbufs);
834 			if ((bp->b_flags & B_DELWRI) && (bp->b_lblkno < 0)) {
835 				if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
836 					BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL);
837 					goto restart;
838 				} else {
839 					bremfree(bp);
840 					if (bp->b_vp == vp) {
841 						bp->b_flags |= B_ASYNC;
842 					} else {
843 						bp->b_flags &= ~B_ASYNC;
844 					}
845 					VOP_BWRITE(bp->b_vp, bp);
846 				}
847 				goto restartsync;
848 			}
849 
850 		}
851 	}
852 
853 	while (vp->v_numoutput > 0) {
854 		vp->v_flag |= VBWAIT;
855 		tsleep(&vp->v_numoutput, PVM, "vbtrunc", 0);
856 	}
857 
858 	splx(s);
859 
860 	vnode_pager_setsize(vp, length);
861 
862 	return (0);
863 }
864 
865 /*
866  * Associate a buffer with a vnode.
867  */
868 void
869 bgetvp(vp, bp)
870 	register struct vnode *vp;
871 	register struct buf *bp;
872 {
873 	int s;
874 
875 	KASSERT(bp->b_vp == NULL, ("bgetvp: not free"));
876 
877 	vhold(vp);
878 	bp->b_vp = vp;
879 	bp->b_dev = vn_todev(vp);
880 	/*
881 	 * Insert onto list for new vnode.
882 	 */
883 	s = splbio();
884 	bp->b_xflags |= B_VNCLEAN;
885 	bp->b_xflags &= ~B_VNDIRTY;
886 	TAILQ_INSERT_TAIL(&vp->v_cleanblkhd, bp, b_vnbufs);
887 	splx(s);
888 }
889 
890 /*
891  * Disassociate a buffer from a vnode.
892  */
893 void
894 brelvp(bp)
895 	register struct buf *bp;
896 {
897 	struct vnode *vp;
898 	struct buflists *listheadp;
899 	int s;
900 
901 	KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
902 
903 	/*
904 	 * Delete from old vnode list, if on one.
905 	 */
906 	vp = bp->b_vp;
907 	s = splbio();
908 	if (bp->b_xflags & (B_VNDIRTY|B_VNCLEAN)) {
909 		if (bp->b_xflags & B_VNDIRTY)
910 			listheadp = &vp->v_dirtyblkhd;
911 		else
912 			listheadp = &vp->v_cleanblkhd;
913 		TAILQ_REMOVE(listheadp, bp, b_vnbufs);
914 		bp->b_xflags &= ~(B_VNDIRTY|B_VNCLEAN);
915 	}
916 	if ((vp->v_flag & VONWORKLST) && TAILQ_EMPTY(&vp->v_dirtyblkhd)) {
917 		vp->v_flag &= ~VONWORKLST;
918 		LIST_REMOVE(vp, v_synclist);
919 	}
920 	splx(s);
921 	bp->b_vp = (struct vnode *) 0;
922 	vdrop(vp);
923 }
924 
925 /*
926  * The workitem queue.
927  *
928  * It is useful to delay writes of file data and filesystem metadata
929  * for tens of seconds so that quickly created and deleted files need
930  * not waste disk bandwidth being created and removed. To realize this,
931  * we append vnodes to a "workitem" queue. When running with a soft
932  * updates implementation, most pending metadata dependencies should
933  * not wait for more than a few seconds. Thus, mounted on block devices
934  * are delayed only about a half the time that file data is delayed.
935  * Similarly, directory updates are more critical, so are only delayed
936  * about a third the time that file data is delayed. Thus, there are
937  * SYNCER_MAXDELAY queues that are processed round-robin at a rate of
938  * one each second (driven off the filesystem syncer process). The
939  * syncer_delayno variable indicates the next queue that is to be processed.
940  * Items that need to be processed soon are placed in this queue:
941  *
942  *	syncer_workitem_pending[syncer_delayno]
943  *
944  * A delay of fifteen seconds is done by placing the request fifteen
945  * entries later in the queue:
946  *
947  *	syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask]
948  *
949  */
950 
951 /*
952  * Add an item to the syncer work queue.
953  */
954 static void
955 vn_syncer_add_to_worklist(struct vnode *vp, int delay)
956 {
957 	int s, slot;
958 
959 	s = splbio();
960 
961 	if (vp->v_flag & VONWORKLST) {
962 		LIST_REMOVE(vp, v_synclist);
963 	}
964 
965 	if (delay > syncer_maxdelay - 2)
966 		delay = syncer_maxdelay - 2;
967 	slot = (syncer_delayno + delay) & syncer_mask;
968 
969 	LIST_INSERT_HEAD(&syncer_workitem_pending[slot], vp, v_synclist);
970 	vp->v_flag |= VONWORKLST;
971 	splx(s);
972 }
973 
974 struct  proc *updateproc;
975 static void sched_sync __P((void));
976 static struct kproc_desc up_kp = {
977 	"syncer",
978 	sched_sync,
979 	&updateproc
980 };
981 SYSINIT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp)
982 
983 /*
984  * System filesystem synchronizer daemon.
985  */
986 void
987 sched_sync(void)
988 {
989 	struct synclist *slp;
990 	struct vnode *vp;
991 	long starttime;
992 	int s;
993 	struct proc *p = updateproc;
994 
995 	p->p_flag |= P_BUFEXHAUST;
996 
997 	for (;;) {
998 		starttime = time_second;
999 
1000 		/*
1001 		 * Push files whose dirty time has expired.  Be careful
1002 		 * of interrupt race on slp queue.
1003 		 */
1004 		s = splbio();
1005 		slp = &syncer_workitem_pending[syncer_delayno];
1006 		syncer_delayno += 1;
1007 		if (syncer_delayno == syncer_maxdelay)
1008 			syncer_delayno = 0;
1009 		splx(s);
1010 
1011 		while ((vp = LIST_FIRST(slp)) != NULL) {
1012 			if (VOP_ISLOCKED(vp) == 0) {
1013 				vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
1014 				(void) VOP_FSYNC(vp, p->p_ucred, MNT_LAZY, p);
1015 				VOP_UNLOCK(vp, 0, p);
1016 			}
1017 			s = splbio();
1018 			if (LIST_FIRST(slp) == vp) {
1019 				/*
1020 				 * Note: v_tag VT_VFS vps can remain on the
1021 				 * worklist too with no dirty blocks, but
1022 				 * since sync_fsync() moves it to a different
1023 				 * slot we are safe.
1024 				 */
1025 				if (TAILQ_EMPTY(&vp->v_dirtyblkhd) &&
1026 				    vp->v_type != VBLK)
1027 					panic("sched_sync: fsync failed vp %p tag %d", vp, vp->v_tag);
1028 				/*
1029 				 * Put us back on the worklist.  The worklist
1030 				 * routine will remove us from our current
1031 				 * position and then add us back in at a later
1032 				 * position.
1033 				 */
1034 				vn_syncer_add_to_worklist(vp, syncdelay);
1035 			}
1036 			splx(s);
1037 		}
1038 
1039 		/*
1040 		 * Do soft update processing.
1041 		 */
1042 		if (bioops.io_sync)
1043 			(*bioops.io_sync)(NULL);
1044 
1045 		/*
1046 		 * The variable rushjob allows the kernel to speed up the
1047 		 * processing of the filesystem syncer process. A rushjob
1048 		 * value of N tells the filesystem syncer to process the next
1049 		 * N seconds worth of work on its queue ASAP. Currently rushjob
1050 		 * is used by the soft update code to speed up the filesystem
1051 		 * syncer process when the incore state is getting so far
1052 		 * ahead of the disk that the kernel memory pool is being
1053 		 * threatened with exhaustion.
1054 		 */
1055 		if (rushjob > 0) {
1056 			rushjob -= 1;
1057 			continue;
1058 		}
1059 		/*
1060 		 * If it has taken us less than a second to process the
1061 		 * current work, then wait. Otherwise start right over
1062 		 * again. We can still lose time if any single round
1063 		 * takes more than two seconds, but it does not really
1064 		 * matter as we are just trying to generally pace the
1065 		 * filesystem activity.
1066 		 */
1067 		if (time_second == starttime)
1068 			tsleep(&lbolt, PPAUSE, "syncer", 0);
1069 	}
1070 }
1071 
1072 /*
1073  * Request the syncer daemon to speed up its work.
1074  * We never push it to speed up more than half of its
1075  * normal turn time, otherwise it could take over the cpu.
1076  */
1077 int
1078 speedup_syncer()
1079 {
1080 	int s;
1081 
1082 	s = splhigh();
1083 	if (updateproc->p_wchan == &lbolt)
1084 		setrunnable(updateproc);
1085 	splx(s);
1086 	if (rushjob < syncdelay / 2) {
1087 		rushjob += 1;
1088 		stat_rush_requests += 1;
1089 		return (1);
1090 	}
1091 	return(0);
1092 }
1093 
1094 /*
1095  * Associate a p-buffer with a vnode.
1096  *
1097  * Also sets B_PAGING flag to indicate that vnode is not fully associated
1098  * with the buffer.  i.e. the bp has not been linked into the vnode or
1099  * ref-counted.
1100  */
1101 void
1102 pbgetvp(vp, bp)
1103 	register struct vnode *vp;
1104 	register struct buf *bp;
1105 {
1106 
1107 	KASSERT(bp->b_vp == NULL, ("pbgetvp: not free"));
1108 
1109 	bp->b_vp = vp;
1110 	bp->b_flags |= B_PAGING;
1111 	bp->b_dev = vn_todev(vp);
1112 }
1113 
1114 /*
1115  * Disassociate a p-buffer from a vnode.
1116  */
1117 void
1118 pbrelvp(bp)
1119 	register struct buf *bp;
1120 {
1121 
1122 	KASSERT(bp->b_vp != NULL, ("pbrelvp: NULL"));
1123 
1124 #if !defined(MAX_PERF)
1125 	/* XXX REMOVE ME */
1126 	if (bp->b_vnbufs.tqe_next != NULL) {
1127 		panic(
1128 		    "relpbuf(): b_vp was probably reassignbuf()d %p %x",
1129 		    bp,
1130 		    (int)bp->b_flags
1131 		);
1132 	}
1133 #endif
1134 	bp->b_vp = (struct vnode *) 0;
1135 	bp->b_flags &= ~B_PAGING;
1136 }
1137 
1138 void
1139 pbreassignbuf(bp, newvp)
1140 	struct buf *bp;
1141 	struct vnode *newvp;
1142 {
1143 #if !defined(MAX_PERF)
1144 	if ((bp->b_flags & B_PAGING) == 0) {
1145 		panic(
1146 		    "pbreassignbuf() on non phys bp %p",
1147 		    bp
1148 		);
1149 	}
1150 #endif
1151 	bp->b_vp = newvp;
1152 }
1153 
1154 /*
1155  * Reassign a buffer from one vnode to another.
1156  * Used to assign file specific control information
1157  * (indirect blocks) to the vnode to which they belong.
1158  */
1159 void
1160 reassignbuf(bp, newvp)
1161 	register struct buf *bp;
1162 	register struct vnode *newvp;
1163 {
1164 	struct buflists *listheadp;
1165 	int delay;
1166 	int s;
1167 
1168 	if (newvp == NULL) {
1169 		printf("reassignbuf: NULL");
1170 		return;
1171 	}
1172 	++reassignbufcalls;
1173 
1174 #if !defined(MAX_PERF)
1175 	/*
1176 	 * B_PAGING flagged buffers cannot be reassigned because their vp
1177 	 * is not fully linked in.
1178 	 */
1179 	if (bp->b_flags & B_PAGING)
1180 		panic("cannot reassign paging buffer");
1181 #endif
1182 
1183 	s = splbio();
1184 	/*
1185 	 * Delete from old vnode list, if on one.
1186 	 */
1187 	if (bp->b_xflags & (B_VNDIRTY|B_VNCLEAN)) {
1188 		if (bp->b_xflags & B_VNDIRTY)
1189 			listheadp = &bp->b_vp->v_dirtyblkhd;
1190 		else
1191 			listheadp = &bp->b_vp->v_cleanblkhd;
1192 		TAILQ_REMOVE(listheadp, bp, b_vnbufs);
1193 		bp->b_xflags &= ~(B_VNDIRTY|B_VNCLEAN);
1194 		if (bp->b_vp != newvp) {
1195 			vdrop(bp->b_vp);
1196 			bp->b_vp = NULL;	/* for clarification */
1197 		}
1198 	}
1199 	/*
1200 	 * If dirty, put on list of dirty buffers; otherwise insert onto list
1201 	 * of clean buffers.
1202 	 */
1203 	if (bp->b_flags & B_DELWRI) {
1204 		struct buf *tbp;
1205 
1206 		listheadp = &newvp->v_dirtyblkhd;
1207 		if ((newvp->v_flag & VONWORKLST) == 0) {
1208 			switch (newvp->v_type) {
1209 			case VDIR:
1210 				delay = dirdelay;
1211 				break;
1212 			case VBLK:
1213 				if (newvp->v_specmountpoint != NULL) {
1214 					delay = metadelay;
1215 					break;
1216 				}
1217 				/* fall through */
1218 			default:
1219 				delay = filedelay;
1220 			}
1221 			vn_syncer_add_to_worklist(newvp, delay);
1222 		}
1223 		bp->b_xflags |= B_VNDIRTY;
1224 		tbp = TAILQ_FIRST(listheadp);
1225 		if (tbp == NULL ||
1226 		    bp->b_lblkno == 0 ||
1227 		    (bp->b_lblkno > 0 && bp->b_lblkno < tbp->b_lblkno)) {
1228 			TAILQ_INSERT_HEAD(listheadp, bp, b_vnbufs);
1229 			++reassignbufsortgood;
1230 		} else if (bp->b_lblkno < 0) {
1231 			TAILQ_INSERT_TAIL(listheadp, bp, b_vnbufs);
1232 			++reassignbufsortgood;
1233 		} else if (reassignbufmethod == 1) {
1234 			/*
1235 			 * New sorting algorithm, only handle sequential case,
1236 			 * otherwise guess.
1237 			 */
1238 			if ((tbp = gbincore(newvp, bp->b_lblkno - 1)) != NULL &&
1239 			    (tbp->b_xflags & B_VNDIRTY)) {
1240 				TAILQ_INSERT_AFTER(listheadp, tbp, bp, b_vnbufs);
1241 				++reassignbufsortgood;
1242 			} else {
1243 				TAILQ_INSERT_HEAD(listheadp, bp, b_vnbufs);
1244 				++reassignbufsortbad;
1245 			}
1246 		} else {
1247 			/*
1248 			 * Old sorting algorithm, scan queue and insert
1249 			 */
1250 			struct buf *ttbp;
1251 			while ((ttbp = TAILQ_NEXT(tbp, b_vnbufs)) &&
1252 			    (ttbp->b_lblkno < bp->b_lblkno)) {
1253 				++reassignbufloops;
1254 				tbp = ttbp;
1255 			}
1256 			TAILQ_INSERT_AFTER(listheadp, tbp, bp, b_vnbufs);
1257 		}
1258 	} else {
1259 		bp->b_xflags |= B_VNCLEAN;
1260 		TAILQ_INSERT_TAIL(&newvp->v_cleanblkhd, bp, b_vnbufs);
1261 		if ((newvp->v_flag & VONWORKLST) &&
1262 		    TAILQ_EMPTY(&newvp->v_dirtyblkhd)) {
1263 			newvp->v_flag &= ~VONWORKLST;
1264 			LIST_REMOVE(newvp, v_synclist);
1265 		}
1266 	}
1267 	if (bp->b_vp != newvp) {
1268 		bp->b_vp = newvp;
1269 		vhold(bp->b_vp);
1270 	}
1271 	splx(s);
1272 }
1273 
1274 /*
1275  * Create a vnode for a block device.
1276  * Used for mounting the root file system.
1277  */
1278 int
1279 bdevvp(dev, vpp)
1280 	dev_t dev;
1281 	struct vnode **vpp;
1282 {
1283 	register struct vnode *vp;
1284 	struct vnode *nvp;
1285 	int error;
1286 
1287 	if (dev == NODEV) {
1288 		*vpp = NULLVP;
1289 		return (ENXIO);
1290 	}
1291 	error = getnewvnode(VT_NON, (struct mount *)0, spec_vnodeop_p, &nvp);
1292 	if (error) {
1293 		*vpp = NULLVP;
1294 		return (error);
1295 	}
1296 	vp = nvp;
1297 	vp->v_type = VBLK;
1298 	addalias(vp, dev);
1299 	*vpp = vp;
1300 	return (0);
1301 }
1302 
1303 /*
1304  * Add vnode to the alias list hung off the dev_t.
1305  *
1306  * The reason for this gunk is that multiple vnodes can reference
1307  * the same physical device, so checking vp->v_usecount to see
1308  * how many users there are is inadequate; the v_usecount for
1309  * the vnodes need to be accumulated.  vcount() does that.
1310  */
1311 void
1312 addaliasu(nvp, nvp_rdev)
1313 	struct vnode *nvp;
1314 	udev_t nvp_rdev;
1315 {
1316 
1317 	if (nvp->v_type != VBLK && nvp->v_type != VCHR)
1318 		panic("addaliasu on non-special vnode");
1319 	addalias(nvp, udev2dev(nvp_rdev, nvp->v_type == VBLK ? 1 : 0));
1320 }
1321 
1322 void
1323 addalias(nvp, dev)
1324 	struct vnode *nvp;
1325 	dev_t dev;
1326 {
1327 
1328 	if (nvp->v_type != VBLK && nvp->v_type != VCHR)
1329 		panic("addalias on non-special vnode");
1330 
1331 	nvp->v_rdev = dev;
1332 	simple_lock(&spechash_slock);
1333 	SLIST_INSERT_HEAD(&dev->si_hlist, nvp, v_specnext);
1334 	simple_unlock(&spechash_slock);
1335 }
1336 
1337 /*
1338  * Grab a particular vnode from the free list, increment its
1339  * reference count and lock it. The vnode lock bit is set if the
1340  * vnode is being eliminated in vgone. The process is awakened
1341  * when the transition is completed, and an error returned to
1342  * indicate that the vnode is no longer usable (possibly having
1343  * been changed to a new file system type).
1344  */
1345 int
1346 vget(vp, flags, p)
1347 	register struct vnode *vp;
1348 	int flags;
1349 	struct proc *p;
1350 {
1351 	int error;
1352 
1353 	/*
1354 	 * If the vnode is in the process of being cleaned out for
1355 	 * another use, we wait for the cleaning to finish and then
1356 	 * return failure. Cleaning is determined by checking that
1357 	 * the VXLOCK flag is set.
1358 	 */
1359 	if ((flags & LK_INTERLOCK) == 0) {
1360 		simple_lock(&vp->v_interlock);
1361 	}
1362 	if (vp->v_flag & VXLOCK) {
1363 		vp->v_flag |= VXWANT;
1364 		simple_unlock(&vp->v_interlock);
1365 		tsleep((caddr_t)vp, PINOD, "vget", 0);
1366 		return (ENOENT);
1367 	}
1368 
1369 	vp->v_usecount++;
1370 
1371 	if (VSHOULDBUSY(vp))
1372 		vbusy(vp);
1373 	if (flags & LK_TYPE_MASK) {
1374 		if ((error = vn_lock(vp, flags | LK_INTERLOCK, p)) != 0) {
1375 			/*
1376 			 * must expand vrele here because we do not want
1377 			 * to call VOP_INACTIVE if the reference count
1378 			 * drops back to zero since it was never really
1379 			 * active. We must remove it from the free list
1380 			 * before sleeping so that multiple processes do
1381 			 * not try to recycle it.
1382 			 */
1383 			simple_lock(&vp->v_interlock);
1384 			vp->v_usecount--;
1385 			if (VSHOULDFREE(vp))
1386 				vfree(vp);
1387 			simple_unlock(&vp->v_interlock);
1388 		}
1389 		return (error);
1390 	}
1391 	simple_unlock(&vp->v_interlock);
1392 	return (0);
1393 }
1394 
1395 void
1396 vref(struct vnode *vp)
1397 {
1398 	simple_lock(&vp->v_interlock);
1399 	vp->v_usecount++;
1400 	simple_unlock(&vp->v_interlock);
1401 }
1402 
1403 /*
1404  * Vnode put/release.
1405  * If count drops to zero, call inactive routine and return to freelist.
1406  */
1407 void
1408 vrele(vp)
1409 	struct vnode *vp;
1410 {
1411 	struct proc *p = curproc;	/* XXX */
1412 
1413 	KASSERT(vp != NULL, ("vrele: null vp"));
1414 
1415 	simple_lock(&vp->v_interlock);
1416 
1417 	if (vp->v_usecount > 1) {
1418 
1419 		vp->v_usecount--;
1420 		simple_unlock(&vp->v_interlock);
1421 
1422 		return;
1423 	}
1424 
1425 	if (vp->v_usecount == 1) {
1426 
1427 		vp->v_usecount--;
1428 		if (VSHOULDFREE(vp))
1429 			vfree(vp);
1430 	/*
1431 	 * If we are doing a vput, the node is already locked, and we must
1432 	 * call VOP_INACTIVE with the node locked.  So, in the case of
1433 	 * vrele, we explicitly lock the vnode before calling VOP_INACTIVE.
1434 	 */
1435 		if (vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK, p) == 0) {
1436 			VOP_INACTIVE(vp, p);
1437 		}
1438 
1439 	} else {
1440 #ifdef DIAGNOSTIC
1441 		vprint("vrele: negative ref count", vp);
1442 		simple_unlock(&vp->v_interlock);
1443 #endif
1444 		panic("vrele: negative ref cnt");
1445 	}
1446 }
1447 
1448 void
1449 vput(vp)
1450 	struct vnode *vp;
1451 {
1452 	struct proc *p = curproc;	/* XXX */
1453 
1454 	KASSERT(vp != NULL, ("vput: null vp"));
1455 
1456 	simple_lock(&vp->v_interlock);
1457 
1458 	if (vp->v_usecount > 1) {
1459 
1460 		vp->v_usecount--;
1461 		VOP_UNLOCK(vp, LK_INTERLOCK, p);
1462 		return;
1463 
1464 	}
1465 
1466 	if (vp->v_usecount == 1) {
1467 
1468 		vp->v_usecount--;
1469 		if (VSHOULDFREE(vp))
1470 			vfree(vp);
1471 	/*
1472 	 * If we are doing a vput, the node is already locked, and we must
1473 	 * call VOP_INACTIVE with the node locked.  So, in the case of
1474 	 * vrele, we explicitly lock the vnode before calling VOP_INACTIVE.
1475 	 */
1476 		simple_unlock(&vp->v_interlock);
1477 		VOP_INACTIVE(vp, p);
1478 
1479 	} else {
1480 #ifdef DIAGNOSTIC
1481 		vprint("vput: negative ref count", vp);
1482 #endif
1483 		panic("vput: negative ref cnt");
1484 	}
1485 }
1486 
1487 /*
1488  * Somebody doesn't want the vnode recycled.
1489  */
1490 void
1491 vhold(vp)
1492 	register struct vnode *vp;
1493 {
1494 	int s;
1495 
1496   	s = splbio();
1497 	vp->v_holdcnt++;
1498 	if (VSHOULDBUSY(vp))
1499 		vbusy(vp);
1500 	splx(s);
1501 }
1502 
1503 /*
1504  * One less who cares about this vnode.
1505  */
1506 void
1507 vdrop(vp)
1508 	register struct vnode *vp;
1509 {
1510 	int s;
1511 
1512 	s = splbio();
1513 	if (vp->v_holdcnt <= 0)
1514 		panic("vdrop: holdcnt");
1515 	vp->v_holdcnt--;
1516 	if (VSHOULDFREE(vp))
1517 		vfree(vp);
1518 	splx(s);
1519 }
1520 
1521 /*
1522  * Remove any vnodes in the vnode table belonging to mount point mp.
1523  *
1524  * If MNT_NOFORCE is specified, there should not be any active ones,
1525  * return error if any are found (nb: this is a user error, not a
1526  * system error). If MNT_FORCE is specified, detach any active vnodes
1527  * that are found.
1528  */
1529 #ifdef DIAGNOSTIC
1530 static int busyprt = 0;		/* print out busy vnodes */
1531 SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, "");
1532 #endif
1533 
1534 int
1535 vflush(mp, skipvp, flags)
1536 	struct mount *mp;
1537 	struct vnode *skipvp;
1538 	int flags;
1539 {
1540 	struct proc *p = curproc;	/* XXX */
1541 	struct vnode *vp, *nvp;
1542 	int busy = 0;
1543 
1544 	simple_lock(&mntvnode_slock);
1545 loop:
1546 	for (vp = mp->mnt_vnodelist.lh_first; vp; vp = nvp) {
1547 		/*
1548 		 * Make sure this vnode wasn't reclaimed in getnewvnode().
1549 		 * Start over if it has (it won't be on the list anymore).
1550 		 */
1551 		if (vp->v_mount != mp)
1552 			goto loop;
1553 		nvp = vp->v_mntvnodes.le_next;
1554 		/*
1555 		 * Skip over a selected vnode.
1556 		 */
1557 		if (vp == skipvp)
1558 			continue;
1559 
1560 		simple_lock(&vp->v_interlock);
1561 		/*
1562 		 * Skip over a vnodes marked VSYSTEM.
1563 		 */
1564 		if ((flags & SKIPSYSTEM) && (vp->v_flag & VSYSTEM)) {
1565 			simple_unlock(&vp->v_interlock);
1566 			continue;
1567 		}
1568 		/*
1569 		 * If WRITECLOSE is set, only flush out regular file vnodes
1570 		 * open for writing.
1571 		 */
1572 		if ((flags & WRITECLOSE) &&
1573 		    (vp->v_writecount == 0 || vp->v_type != VREG)) {
1574 			simple_unlock(&vp->v_interlock);
1575 			continue;
1576 		}
1577 
1578 		/*
1579 		 * With v_usecount == 0, all we need to do is clear out the
1580 		 * vnode data structures and we are done.
1581 		 */
1582 		if (vp->v_usecount == 0) {
1583 			simple_unlock(&mntvnode_slock);
1584 			vgonel(vp, p);
1585 			simple_lock(&mntvnode_slock);
1586 			continue;
1587 		}
1588 
1589 		/*
1590 		 * If FORCECLOSE is set, forcibly close the vnode. For block
1591 		 * or character devices, revert to an anonymous device. For
1592 		 * all other files, just kill them.
1593 		 */
1594 		if (flags & FORCECLOSE) {
1595 			simple_unlock(&mntvnode_slock);
1596 			if (vp->v_type != VBLK && vp->v_type != VCHR) {
1597 				vgonel(vp, p);
1598 			} else {
1599 				vclean(vp, 0, p);
1600 				vp->v_op = spec_vnodeop_p;
1601 				insmntque(vp, (struct mount *) 0);
1602 			}
1603 			simple_lock(&mntvnode_slock);
1604 			continue;
1605 		}
1606 #ifdef DIAGNOSTIC
1607 		if (busyprt)
1608 			vprint("vflush: busy vnode", vp);
1609 #endif
1610 		simple_unlock(&vp->v_interlock);
1611 		busy++;
1612 	}
1613 	simple_unlock(&mntvnode_slock);
1614 	if (busy)
1615 		return (EBUSY);
1616 	return (0);
1617 }
1618 
1619 /*
1620  * Disassociate the underlying file system from a vnode.
1621  */
1622 static void
1623 vclean(vp, flags, p)
1624 	struct vnode *vp;
1625 	int flags;
1626 	struct proc *p;
1627 {
1628 	int active;
1629 	vm_object_t obj;
1630 
1631 	/*
1632 	 * Check to see if the vnode is in use. If so we have to reference it
1633 	 * before we clean it out so that its count cannot fall to zero and
1634 	 * generate a race against ourselves to recycle it.
1635 	 */
1636 	if ((active = vp->v_usecount))
1637 		vp->v_usecount++;
1638 
1639 	/*
1640 	 * Prevent the vnode from being recycled or brought into use while we
1641 	 * clean it out.
1642 	 */
1643 	if (vp->v_flag & VXLOCK)
1644 		panic("vclean: deadlock");
1645 	vp->v_flag |= VXLOCK;
1646 	/*
1647 	 * Even if the count is zero, the VOP_INACTIVE routine may still
1648 	 * have the object locked while it cleans it out. The VOP_LOCK
1649 	 * ensures that the VOP_INACTIVE routine is done with its work.
1650 	 * For active vnodes, it ensures that no other activity can
1651 	 * occur while the underlying object is being cleaned out.
1652 	 */
1653 	VOP_LOCK(vp, LK_DRAIN | LK_INTERLOCK, p);
1654 
1655 	/*
1656 	 * Clean out any buffers associated with the vnode.
1657 	 */
1658 	vinvalbuf(vp, V_SAVE, NOCRED, p, 0, 0);
1659 	if ((obj = vp->v_object) != NULL) {
1660 		if (obj->ref_count == 0) {
1661 			/*
1662 			 * vclean() may be called twice.  The first time removes the
1663 			 * primary reference to the object, the second time goes
1664 			 * one further and is a special-case to terminate the object.
1665 			 */
1666 			vm_object_terminate(obj);
1667 		} else {
1668 			/*
1669 			 * Woe to the process that tries to page now :-).
1670 			 */
1671 			vm_pager_deallocate(obj);
1672 		}
1673 	}
1674 
1675 	/*
1676 	 * If purging an active vnode, it must be closed and
1677 	 * deactivated before being reclaimed. Note that the
1678 	 * VOP_INACTIVE will unlock the vnode.
1679 	 */
1680 	if (active) {
1681 		if (flags & DOCLOSE)
1682 			VOP_CLOSE(vp, FNONBLOCK, NOCRED, p);
1683 		VOP_INACTIVE(vp, p);
1684 	} else {
1685 		/*
1686 		 * Any other processes trying to obtain this lock must first
1687 		 * wait for VXLOCK to clear, then call the new lock operation.
1688 		 */
1689 		VOP_UNLOCK(vp, 0, p);
1690 	}
1691 	/*
1692 	 * Reclaim the vnode.
1693 	 */
1694 	if (VOP_RECLAIM(vp, p))
1695 		panic("vclean: cannot reclaim");
1696 
1697 	if (active)
1698 		vrele(vp);
1699 
1700 	cache_purge(vp);
1701 	if (vp->v_vnlock) {
1702 		FREE(vp->v_vnlock, M_VNODE);
1703 		vp->v_vnlock = NULL;
1704 	}
1705 
1706 	if (VSHOULDFREE(vp))
1707 		vfree(vp);
1708 
1709 	/*
1710 	 * Done with purge, notify sleepers of the grim news.
1711 	 */
1712 	vp->v_op = dead_vnodeop_p;
1713 	vn_pollgone(vp);
1714 	vp->v_tag = VT_NON;
1715 	vp->v_flag &= ~VXLOCK;
1716 	if (vp->v_flag & VXWANT) {
1717 		vp->v_flag &= ~VXWANT;
1718 		wakeup((caddr_t) vp);
1719 	}
1720 }
1721 
1722 /*
1723  * Eliminate all activity associated with the requested vnode
1724  * and with all vnodes aliased to the requested vnode.
1725  */
1726 int
1727 vop_revoke(ap)
1728 	struct vop_revoke_args /* {
1729 		struct vnode *a_vp;
1730 		int a_flags;
1731 	} */ *ap;
1732 {
1733 	struct vnode *vp, *vq;
1734 	dev_t dev;
1735 
1736 	KASSERT((ap->a_flags & REVOKEALL) != 0, ("vop_revoke"));
1737 
1738 	vp = ap->a_vp;
1739 	/*
1740 	 * If a vgone (or vclean) is already in progress,
1741 	 * wait until it is done and return.
1742 	 */
1743 	if (vp->v_flag & VXLOCK) {
1744 		vp->v_flag |= VXWANT;
1745 		simple_unlock(&vp->v_interlock);
1746 		tsleep((caddr_t)vp, PINOD, "vop_revokeall", 0);
1747 		return (0);
1748 	}
1749 	dev = vp->v_rdev;
1750 	for (;;) {
1751 		simple_lock(&spechash_slock);
1752 		vq = SLIST_FIRST(&dev->si_hlist);
1753 		simple_unlock(&spechash_slock);
1754 		if (!vq)
1755 			break;
1756 		vgone(vq);
1757 	}
1758 	return (0);
1759 }
1760 
1761 /*
1762  * Recycle an unused vnode to the front of the free list.
1763  * Release the passed interlock if the vnode will be recycled.
1764  */
1765 int
1766 vrecycle(vp, inter_lkp, p)
1767 	struct vnode *vp;
1768 	struct simplelock *inter_lkp;
1769 	struct proc *p;
1770 {
1771 
1772 	simple_lock(&vp->v_interlock);
1773 	if (vp->v_usecount == 0) {
1774 		if (inter_lkp) {
1775 			simple_unlock(inter_lkp);
1776 		}
1777 		vgonel(vp, p);
1778 		return (1);
1779 	}
1780 	simple_unlock(&vp->v_interlock);
1781 	return (0);
1782 }
1783 
1784 /*
1785  * Eliminate all activity associated with a vnode
1786  * in preparation for reuse.
1787  */
1788 void
1789 vgone(vp)
1790 	register struct vnode *vp;
1791 {
1792 	struct proc *p = curproc;	/* XXX */
1793 
1794 	simple_lock(&vp->v_interlock);
1795 	vgonel(vp, p);
1796 }
1797 
1798 /*
1799  * vgone, with the vp interlock held.
1800  */
1801 static void
1802 vgonel(vp, p)
1803 	struct vnode *vp;
1804 	struct proc *p;
1805 {
1806 	int s;
1807 
1808 	/*
1809 	 * If a vgone (or vclean) is already in progress,
1810 	 * wait until it is done and return.
1811 	 */
1812 	if (vp->v_flag & VXLOCK) {
1813 		vp->v_flag |= VXWANT;
1814 		simple_unlock(&vp->v_interlock);
1815 		tsleep((caddr_t)vp, PINOD, "vgone", 0);
1816 		return;
1817 	}
1818 
1819 	/*
1820 	 * Clean out the filesystem specific data.
1821 	 */
1822 	vclean(vp, DOCLOSE, p);
1823 	simple_lock(&vp->v_interlock);
1824 
1825 	/*
1826 	 * Delete from old mount point vnode list, if on one.
1827 	 */
1828 	if (vp->v_mount != NULL)
1829 		insmntque(vp, (struct mount *)0);
1830 	/*
1831 	 * If special device, remove it from special device alias list
1832 	 * if it is on one.
1833 	 */
1834 	if ((vp->v_type == VBLK || vp->v_type == VCHR) && vp->v_rdev != NULL) {
1835 		simple_lock(&spechash_slock);
1836 		SLIST_REMOVE(&vp->v_hashchain, vp, vnode, v_specnext);
1837 		freedev(vp->v_rdev);
1838 		simple_unlock(&spechash_slock);
1839 		vp->v_rdev = NULL;
1840 	}
1841 
1842 	/*
1843 	 * If it is on the freelist and not already at the head,
1844 	 * move it to the head of the list. The test of the back
1845 	 * pointer and the reference count of zero is because
1846 	 * it will be removed from the free list by getnewvnode,
1847 	 * but will not have its reference count incremented until
1848 	 * after calling vgone. If the reference count were
1849 	 * incremented first, vgone would (incorrectly) try to
1850 	 * close the previous instance of the underlying object.
1851 	 */
1852 	if (vp->v_usecount == 0 && !(vp->v_flag & VDOOMED)) {
1853 		s = splbio();
1854 		simple_lock(&vnode_free_list_slock);
1855 		if (vp->v_flag & VFREE) {
1856 			TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
1857 		} else if (vp->v_flag & VTBFREE) {
1858 			TAILQ_REMOVE(&vnode_tobefree_list, vp, v_freelist);
1859 			vp->v_flag &= ~VTBFREE;
1860 			freevnodes++;
1861 		} else
1862 			freevnodes++;
1863 		vp->v_flag |= VFREE;
1864 		TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
1865 		simple_unlock(&vnode_free_list_slock);
1866 		splx(s);
1867 	}
1868 
1869 	vp->v_type = VBAD;
1870 	simple_unlock(&vp->v_interlock);
1871 }
1872 
1873 /*
1874  * Lookup a vnode by device number.
1875  */
1876 int
1877 vfinddev(dev, type, vpp)
1878 	dev_t dev;
1879 	enum vtype type;
1880 	struct vnode **vpp;
1881 {
1882 	struct vnode *vp;
1883 
1884 	simple_lock(&spechash_slock);
1885 	SLIST_FOREACH(vp, &dev->si_hlist, v_specnext) {
1886 		if (type == vp->v_type) {
1887 			*vpp = vp;
1888 			simple_unlock(&spechash_slock);
1889 			return (1);
1890 		}
1891 	}
1892 	simple_unlock(&spechash_slock);
1893 	return (0);
1894 }
1895 
1896 /*
1897  * Calculate the total number of references to a special device.
1898  */
1899 int
1900 vcount(vp)
1901 	struct vnode *vp;
1902 {
1903 	struct vnode *vq;
1904 	int count;
1905 
1906 	count = 0;
1907 	simple_lock(&spechash_slock);
1908 	SLIST_FOREACH(vq, &vp->v_hashchain, v_specnext)
1909 		count += vq->v_usecount;
1910 	simple_unlock(&spechash_slock);
1911 	return (count);
1912 }
1913 
1914 /*
1915  * Print out a description of a vnode.
1916  */
1917 static char *typename[] =
1918 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD"};
1919 
1920 void
1921 vprint(label, vp)
1922 	char *label;
1923 	struct vnode *vp;
1924 {
1925 	char buf[96];
1926 
1927 	if (label != NULL)
1928 		printf("%s: %p: ", label, (void *)vp);
1929 	else
1930 		printf("%p: ", (void *)vp);
1931 	printf("type %s, usecount %d, writecount %d, refcount %d,",
1932 	    typename[vp->v_type], vp->v_usecount, vp->v_writecount,
1933 	    vp->v_holdcnt);
1934 	buf[0] = '\0';
1935 	if (vp->v_flag & VROOT)
1936 		strcat(buf, "|VROOT");
1937 	if (vp->v_flag & VTEXT)
1938 		strcat(buf, "|VTEXT");
1939 	if (vp->v_flag & VSYSTEM)
1940 		strcat(buf, "|VSYSTEM");
1941 	if (vp->v_flag & VXLOCK)
1942 		strcat(buf, "|VXLOCK");
1943 	if (vp->v_flag & VXWANT)
1944 		strcat(buf, "|VXWANT");
1945 	if (vp->v_flag & VBWAIT)
1946 		strcat(buf, "|VBWAIT");
1947 	if (vp->v_flag & VDOOMED)
1948 		strcat(buf, "|VDOOMED");
1949 	if (vp->v_flag & VFREE)
1950 		strcat(buf, "|VFREE");
1951 	if (vp->v_flag & VOBJBUF)
1952 		strcat(buf, "|VOBJBUF");
1953 	if (buf[0] != '\0')
1954 		printf(" flags (%s)", &buf[1]);
1955 	if (vp->v_data == NULL) {
1956 		printf("\n");
1957 	} else {
1958 		printf("\n\t");
1959 		VOP_PRINT(vp);
1960 	}
1961 }
1962 
1963 #ifdef DDB
1964 #include <ddb/ddb.h>
1965 /*
1966  * List all of the locked vnodes in the system.
1967  * Called when debugging the kernel.
1968  */
1969 DB_SHOW_COMMAND(lockedvnodes, lockedvnodes)
1970 {
1971 	struct proc *p = curproc;	/* XXX */
1972 	struct mount *mp, *nmp;
1973 	struct vnode *vp;
1974 
1975 	printf("Locked vnodes\n");
1976 	simple_lock(&mountlist_slock);
1977 	for (mp = mountlist.cqh_first; mp != (void *)&mountlist; mp = nmp) {
1978 		if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock, p)) {
1979 			nmp = mp->mnt_list.cqe_next;
1980 			continue;
1981 		}
1982 		for (vp = mp->mnt_vnodelist.lh_first;
1983 		     vp != NULL;
1984 		     vp = vp->v_mntvnodes.le_next) {
1985 			if (VOP_ISLOCKED(vp))
1986 				vprint((char *)0, vp);
1987 		}
1988 		simple_lock(&mountlist_slock);
1989 		nmp = mp->mnt_list.cqe_next;
1990 		vfs_unbusy(mp, p);
1991 	}
1992 	simple_unlock(&mountlist_slock);
1993 }
1994 #endif
1995 
1996 /*
1997  * Top level filesystem related information gathering.
1998  */
1999 static int	sysctl_ovfs_conf __P(SYSCTL_HANDLER_ARGS);
2000 
2001 static int
2002 vfs_sysctl SYSCTL_HANDLER_ARGS
2003 {
2004 	int *name = (int *)arg1 - 1;	/* XXX */
2005 	u_int namelen = arg2 + 1;	/* XXX */
2006 	struct vfsconf *vfsp;
2007 
2008 #if 1 || defined(COMPAT_PRELITE2)
2009 	/* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
2010 	if (namelen == 1)
2011 		return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
2012 #endif
2013 
2014 #ifdef notyet
2015 	/* all sysctl names at this level are at least name and field */
2016 	if (namelen < 2)
2017 		return (ENOTDIR);		/* overloaded */
2018 	if (name[0] != VFS_GENERIC) {
2019 		for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
2020 			if (vfsp->vfc_typenum == name[0])
2021 				break;
2022 		if (vfsp == NULL)
2023 			return (EOPNOTSUPP);
2024 		return ((*vfsp->vfc_vfsops->vfs_sysctl)(&name[1], namelen - 1,
2025 		    oldp, oldlenp, newp, newlen, p));
2026 	}
2027 #endif
2028 	switch (name[1]) {
2029 	case VFS_MAXTYPENUM:
2030 		if (namelen != 2)
2031 			return (ENOTDIR);
2032 		return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int)));
2033 	case VFS_CONF:
2034 		if (namelen != 3)
2035 			return (ENOTDIR);	/* overloaded */
2036 		for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
2037 			if (vfsp->vfc_typenum == name[2])
2038 				break;
2039 		if (vfsp == NULL)
2040 			return (EOPNOTSUPP);
2041 		return (SYSCTL_OUT(req, vfsp, sizeof *vfsp));
2042 	}
2043 	return (EOPNOTSUPP);
2044 }
2045 
2046 SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD, vfs_sysctl,
2047 	"Generic filesystem");
2048 
2049 #if 1 || defined(COMPAT_PRELITE2)
2050 
2051 static int
2052 sysctl_ovfs_conf SYSCTL_HANDLER_ARGS
2053 {
2054 	int error;
2055 	struct vfsconf *vfsp;
2056 	struct ovfsconf ovfs;
2057 
2058 	for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
2059 		ovfs.vfc_vfsops = vfsp->vfc_vfsops;	/* XXX used as flag */
2060 		strcpy(ovfs.vfc_name, vfsp->vfc_name);
2061 		ovfs.vfc_index = vfsp->vfc_typenum;
2062 		ovfs.vfc_refcount = vfsp->vfc_refcount;
2063 		ovfs.vfc_flags = vfsp->vfc_flags;
2064 		error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
2065 		if (error)
2066 			return error;
2067 	}
2068 	return 0;
2069 }
2070 
2071 #endif /* 1 || COMPAT_PRELITE2 */
2072 
2073 #if 0
2074 #define KINFO_VNODESLOP	10
2075 /*
2076  * Dump vnode list (via sysctl).
2077  * Copyout address of vnode followed by vnode.
2078  */
2079 /* ARGSUSED */
2080 static int
2081 sysctl_vnode SYSCTL_HANDLER_ARGS
2082 {
2083 	struct proc *p = curproc;	/* XXX */
2084 	struct mount *mp, *nmp;
2085 	struct vnode *nvp, *vp;
2086 	int error;
2087 
2088 #define VPTRSZ	sizeof (struct vnode *)
2089 #define VNODESZ	sizeof (struct vnode)
2090 
2091 	req->lock = 0;
2092 	if (!req->oldptr) /* Make an estimate */
2093 		return (SYSCTL_OUT(req, 0,
2094 			(numvnodes + KINFO_VNODESLOP) * (VPTRSZ + VNODESZ)));
2095 
2096 	simple_lock(&mountlist_slock);
2097 	for (mp = mountlist.cqh_first; mp != (void *)&mountlist; mp = nmp) {
2098 		if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock, p)) {
2099 			nmp = mp->mnt_list.cqe_next;
2100 			continue;
2101 		}
2102 again:
2103 		simple_lock(&mntvnode_slock);
2104 		for (vp = mp->mnt_vnodelist.lh_first;
2105 		     vp != NULL;
2106 		     vp = nvp) {
2107 			/*
2108 			 * Check that the vp is still associated with
2109 			 * this filesystem.  RACE: could have been
2110 			 * recycled onto the same filesystem.
2111 			 */
2112 			if (vp->v_mount != mp) {
2113 				simple_unlock(&mntvnode_slock);
2114 				goto again;
2115 			}
2116 			nvp = vp->v_mntvnodes.le_next;
2117 			simple_unlock(&mntvnode_slock);
2118 			if ((error = SYSCTL_OUT(req, &vp, VPTRSZ)) ||
2119 			    (error = SYSCTL_OUT(req, vp, VNODESZ)))
2120 				return (error);
2121 			simple_lock(&mntvnode_slock);
2122 		}
2123 		simple_unlock(&mntvnode_slock);
2124 		simple_lock(&mountlist_slock);
2125 		nmp = mp->mnt_list.cqe_next;
2126 		vfs_unbusy(mp, p);
2127 	}
2128 	simple_unlock(&mountlist_slock);
2129 
2130 	return (0);
2131 }
2132 #endif
2133 
2134 /*
2135  * XXX
2136  * Exporting the vnode list on large systems causes them to crash.
2137  * Exporting the vnode list on medium systems causes sysctl to coredump.
2138  */
2139 #if 0
2140 SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE|CTLFLAG_RD,
2141 	0, 0, sysctl_vnode, "S,vnode", "");
2142 #endif
2143 
2144 /*
2145  * Check to see if a filesystem is mounted on a block device.
2146  */
2147 int
2148 vfs_mountedon(vp)
2149 	struct vnode *vp;
2150 {
2151 
2152 	if (vp->v_specmountpoint != NULL)
2153 		return (EBUSY);
2154 	return (0);
2155 }
2156 
2157 /*
2158  * Unmount all filesystems. The list is traversed in reverse order
2159  * of mounting to avoid dependencies.
2160  */
2161 void
2162 vfs_unmountall()
2163 {
2164 	struct mount *mp, *nmp;
2165 	struct proc *p;
2166 	int error;
2167 
2168 	if (curproc != NULL)
2169 		p = curproc;
2170 	else
2171 		p = initproc;	/* XXX XXX should this be proc0? */
2172 	/*
2173 	 * Since this only runs when rebooting, it is not interlocked.
2174 	 */
2175 	for (mp = mountlist.cqh_last; mp != (void *)&mountlist; mp = nmp) {
2176 		nmp = mp->mnt_list.cqe_prev;
2177 		error = dounmount(mp, MNT_FORCE, p);
2178 		if (error) {
2179 			printf("unmount of %s failed (",
2180 			    mp->mnt_stat.f_mntonname);
2181 			if (error == EBUSY)
2182 				printf("BUSY)\n");
2183 			else
2184 				printf("%d)\n", error);
2185 		}
2186 	}
2187 }
2188 
2189 /*
2190  * Build hash lists of net addresses and hang them off the mount point.
2191  * Called by ufs_mount() to set up the lists of export addresses.
2192  */
2193 static int
2194 vfs_hang_addrlist(mp, nep, argp)
2195 	struct mount *mp;
2196 	struct netexport *nep;
2197 	struct export_args *argp;
2198 {
2199 	register struct netcred *np;
2200 	register struct radix_node_head *rnh;
2201 	register int i;
2202 	struct radix_node *rn;
2203 	struct sockaddr *saddr, *smask = 0;
2204 	struct domain *dom;
2205 	int error;
2206 
2207 	if (argp->ex_addrlen == 0) {
2208 		if (mp->mnt_flag & MNT_DEFEXPORTED)
2209 			return (EPERM);
2210 		np = &nep->ne_defexported;
2211 		np->netc_exflags = argp->ex_flags;
2212 		np->netc_anon = argp->ex_anon;
2213 		np->netc_anon.cr_ref = 1;
2214 		mp->mnt_flag |= MNT_DEFEXPORTED;
2215 		return (0);
2216 	}
2217 	i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
2218 	np = (struct netcred *) malloc(i, M_NETADDR, M_WAITOK);
2219 	bzero((caddr_t) np, i);
2220 	saddr = (struct sockaddr *) (np + 1);
2221 	if ((error = copyin(argp->ex_addr, (caddr_t) saddr, argp->ex_addrlen)))
2222 		goto out;
2223 	if (saddr->sa_len > argp->ex_addrlen)
2224 		saddr->sa_len = argp->ex_addrlen;
2225 	if (argp->ex_masklen) {
2226 		smask = (struct sockaddr *) ((caddr_t) saddr + argp->ex_addrlen);
2227 		error = copyin(argp->ex_mask, (caddr_t) smask, argp->ex_masklen);
2228 		if (error)
2229 			goto out;
2230 		if (smask->sa_len > argp->ex_masklen)
2231 			smask->sa_len = argp->ex_masklen;
2232 	}
2233 	i = saddr->sa_family;
2234 	if ((rnh = nep->ne_rtable[i]) == 0) {
2235 		/*
2236 		 * Seems silly to initialize every AF when most are not used,
2237 		 * do so on demand here
2238 		 */
2239 		for (dom = domains; dom; dom = dom->dom_next)
2240 			if (dom->dom_family == i && dom->dom_rtattach) {
2241 				dom->dom_rtattach((void **) &nep->ne_rtable[i],
2242 				    dom->dom_rtoffset);
2243 				break;
2244 			}
2245 		if ((rnh = nep->ne_rtable[i]) == 0) {
2246 			error = ENOBUFS;
2247 			goto out;
2248 		}
2249 	}
2250 	rn = (*rnh->rnh_addaddr) ((caddr_t) saddr, (caddr_t) smask, rnh,
2251 	    np->netc_rnodes);
2252 	if (rn == 0 || np != (struct netcred *) rn) {	/* already exists */
2253 		error = EPERM;
2254 		goto out;
2255 	}
2256 	np->netc_exflags = argp->ex_flags;
2257 	np->netc_anon = argp->ex_anon;
2258 	np->netc_anon.cr_ref = 1;
2259 	return (0);
2260 out:
2261 	free(np, M_NETADDR);
2262 	return (error);
2263 }
2264 
2265 /* ARGSUSED */
2266 static int
2267 vfs_free_netcred(rn, w)
2268 	struct radix_node *rn;
2269 	void *w;
2270 {
2271 	register struct radix_node_head *rnh = (struct radix_node_head *) w;
2272 
2273 	(*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, rnh);
2274 	free((caddr_t) rn, M_NETADDR);
2275 	return (0);
2276 }
2277 
2278 /*
2279  * Free the net address hash lists that are hanging off the mount points.
2280  */
2281 static void
2282 vfs_free_addrlist(nep)
2283 	struct netexport *nep;
2284 {
2285 	register int i;
2286 	register struct radix_node_head *rnh;
2287 
2288 	for (i = 0; i <= AF_MAX; i++)
2289 		if ((rnh = nep->ne_rtable[i])) {
2290 			(*rnh->rnh_walktree) (rnh, vfs_free_netcred,
2291 			    (caddr_t) rnh);
2292 			free((caddr_t) rnh, M_RTABLE);
2293 			nep->ne_rtable[i] = 0;
2294 		}
2295 }
2296 
2297 int
2298 vfs_export(mp, nep, argp)
2299 	struct mount *mp;
2300 	struct netexport *nep;
2301 	struct export_args *argp;
2302 {
2303 	int error;
2304 
2305 	if (argp->ex_flags & MNT_DELEXPORT) {
2306 		if (mp->mnt_flag & MNT_EXPUBLIC) {
2307 			vfs_setpublicfs(NULL, NULL, NULL);
2308 			mp->mnt_flag &= ~MNT_EXPUBLIC;
2309 		}
2310 		vfs_free_addrlist(nep);
2311 		mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
2312 	}
2313 	if (argp->ex_flags & MNT_EXPORTED) {
2314 		if (argp->ex_flags & MNT_EXPUBLIC) {
2315 			if ((error = vfs_setpublicfs(mp, nep, argp)) != 0)
2316 				return (error);
2317 			mp->mnt_flag |= MNT_EXPUBLIC;
2318 		}
2319 		if ((error = vfs_hang_addrlist(mp, nep, argp)))
2320 			return (error);
2321 		mp->mnt_flag |= MNT_EXPORTED;
2322 	}
2323 	return (0);
2324 }
2325 
2326 
2327 /*
2328  * Set the publicly exported filesystem (WebNFS). Currently, only
2329  * one public filesystem is possible in the spec (RFC 2054 and 2055)
2330  */
2331 int
2332 vfs_setpublicfs(mp, nep, argp)
2333 	struct mount *mp;
2334 	struct netexport *nep;
2335 	struct export_args *argp;
2336 {
2337 	int error;
2338 	struct vnode *rvp;
2339 	char *cp;
2340 
2341 	/*
2342 	 * mp == NULL -> invalidate the current info, the FS is
2343 	 * no longer exported. May be called from either vfs_export
2344 	 * or unmount, so check if it hasn't already been done.
2345 	 */
2346 	if (mp == NULL) {
2347 		if (nfs_pub.np_valid) {
2348 			nfs_pub.np_valid = 0;
2349 			if (nfs_pub.np_index != NULL) {
2350 				FREE(nfs_pub.np_index, M_TEMP);
2351 				nfs_pub.np_index = NULL;
2352 			}
2353 		}
2354 		return (0);
2355 	}
2356 
2357 	/*
2358 	 * Only one allowed at a time.
2359 	 */
2360 	if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
2361 		return (EBUSY);
2362 
2363 	/*
2364 	 * Get real filehandle for root of exported FS.
2365 	 */
2366 	bzero((caddr_t)&nfs_pub.np_handle, sizeof(nfs_pub.np_handle));
2367 	nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid;
2368 
2369 	if ((error = VFS_ROOT(mp, &rvp)))
2370 		return (error);
2371 
2372 	if ((error = VFS_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid)))
2373 		return (error);
2374 
2375 	vput(rvp);
2376 
2377 	/*
2378 	 * If an indexfile was specified, pull it in.
2379 	 */
2380 	if (argp->ex_indexfile != NULL) {
2381 		MALLOC(nfs_pub.np_index, char *, MAXNAMLEN + 1, M_TEMP,
2382 		    M_WAITOK);
2383 		error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
2384 		    MAXNAMLEN, (size_t *)0);
2385 		if (!error) {
2386 			/*
2387 			 * Check for illegal filenames.
2388 			 */
2389 			for (cp = nfs_pub.np_index; *cp; cp++) {
2390 				if (*cp == '/') {
2391 					error = EINVAL;
2392 					break;
2393 				}
2394 			}
2395 		}
2396 		if (error) {
2397 			FREE(nfs_pub.np_index, M_TEMP);
2398 			return (error);
2399 		}
2400 	}
2401 
2402 	nfs_pub.np_mount = mp;
2403 	nfs_pub.np_valid = 1;
2404 	return (0);
2405 }
2406 
2407 struct netcred *
2408 vfs_export_lookup(mp, nep, nam)
2409 	register struct mount *mp;
2410 	struct netexport *nep;
2411 	struct sockaddr *nam;
2412 {
2413 	register struct netcred *np;
2414 	register struct radix_node_head *rnh;
2415 	struct sockaddr *saddr;
2416 
2417 	np = NULL;
2418 	if (mp->mnt_flag & MNT_EXPORTED) {
2419 		/*
2420 		 * Lookup in the export list first.
2421 		 */
2422 		if (nam != NULL) {
2423 			saddr = nam;
2424 			rnh = nep->ne_rtable[saddr->sa_family];
2425 			if (rnh != NULL) {
2426 				np = (struct netcred *)
2427 					(*rnh->rnh_matchaddr)((caddr_t)saddr,
2428 							      rnh);
2429 				if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
2430 					np = NULL;
2431 			}
2432 		}
2433 		/*
2434 		 * If no address match, use the default if it exists.
2435 		 */
2436 		if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
2437 			np = &nep->ne_defexported;
2438 	}
2439 	return (np);
2440 }
2441 
2442 /*
2443  * perform msync on all vnodes under a mount point
2444  * the mount point must be locked.
2445  */
2446 void
2447 vfs_msync(struct mount *mp, int flags) {
2448 	struct vnode *vp, *nvp;
2449 	struct vm_object *obj;
2450 	int anyio, tries;
2451 
2452 	tries = 5;
2453 loop:
2454 	anyio = 0;
2455 	for (vp = mp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
2456 
2457 		nvp = vp->v_mntvnodes.le_next;
2458 
2459 		if (vp->v_mount != mp) {
2460 			goto loop;
2461 		}
2462 
2463 		if (vp->v_flag & VXLOCK)	/* XXX: what if MNT_WAIT? */
2464 			continue;
2465 
2466 		if (flags != MNT_WAIT) {
2467 			obj = vp->v_object;
2468 			if (obj == NULL || (obj->flags & OBJ_MIGHTBEDIRTY) == 0)
2469 				continue;
2470 			if (VOP_ISLOCKED(vp))
2471 				continue;
2472 		}
2473 
2474 		simple_lock(&vp->v_interlock);
2475 		if (vp->v_object &&
2476 		   (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
2477 			if (!vget(vp,
2478 				LK_INTERLOCK | LK_EXCLUSIVE | LK_RETRY | LK_NOOBJ, curproc)) {
2479 				if (vp->v_object) {
2480 					vm_object_page_clean(vp->v_object, 0, 0, flags == MNT_WAIT ? OBJPC_SYNC : 0);
2481 					anyio = 1;
2482 				}
2483 				vput(vp);
2484 			}
2485 		} else {
2486 			simple_unlock(&vp->v_interlock);
2487 		}
2488 	}
2489 	if (anyio && (--tries > 0))
2490 		goto loop;
2491 }
2492 
2493 /*
2494  * Create the VM object needed for VMIO and mmap support.  This
2495  * is done for all VREG files in the system.  Some filesystems might
2496  * afford the additional metadata buffering capability of the
2497  * VMIO code by making the device node be VMIO mode also.
2498  *
2499  * vp must be locked when vfs_object_create is called.
2500  */
2501 int
2502 vfs_object_create(vp, p, cred)
2503 	struct vnode *vp;
2504 	struct proc *p;
2505 	struct ucred *cred;
2506 {
2507 	struct vattr vat;
2508 	vm_object_t object;
2509 	int error = 0;
2510 
2511 	if (vp->v_type != VBLK && vn_canvmio(vp) == FALSE)
2512 		return 0;
2513 
2514 retry:
2515 	if ((object = vp->v_object) == NULL) {
2516 		if (vp->v_type == VREG || vp->v_type == VDIR) {
2517 			if ((error = VOP_GETATTR(vp, &vat, cred, p)) != 0)
2518 				goto retn;
2519 			object = vnode_pager_alloc(vp, vat.va_size, 0, 0);
2520 		} else if (devsw(vp->v_rdev) != NULL) {
2521 			/*
2522 			 * This simply allocates the biggest object possible
2523 			 * for a VBLK vnode.  This should be fixed, but doesn't
2524 			 * cause any problems (yet).
2525 			 */
2526 			object = vnode_pager_alloc(vp, IDX_TO_OFF(INT_MAX), 0, 0);
2527 		} else {
2528 			goto retn;
2529 		}
2530 		/*
2531 		 * Dereference the reference we just created.  This assumes
2532 		 * that the object is associated with the vp.
2533 		 */
2534 		object->ref_count--;
2535 		vp->v_usecount--;
2536 	} else {
2537 		if (object->flags & OBJ_DEAD) {
2538 			VOP_UNLOCK(vp, 0, p);
2539 			tsleep(object, PVM, "vodead", 0);
2540 			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
2541 			goto retry;
2542 		}
2543 	}
2544 
2545 	KASSERT(vp->v_object != NULL, ("vfs_object_create: NULL object"));
2546 	vp->v_flag |= VOBJBUF;
2547 
2548 retn:
2549 	return error;
2550 }
2551 
2552 static void
2553 vfree(vp)
2554 	struct vnode *vp;
2555 {
2556 	int s;
2557 
2558 	s = splbio();
2559 	simple_lock(&vnode_free_list_slock);
2560 	if (vp->v_flag & VTBFREE) {
2561 		TAILQ_REMOVE(&vnode_tobefree_list, vp, v_freelist);
2562 		vp->v_flag &= ~VTBFREE;
2563 	}
2564 	if (vp->v_flag & VAGE) {
2565 		TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
2566 	} else {
2567 		TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
2568 	}
2569 	freevnodes++;
2570 	simple_unlock(&vnode_free_list_slock);
2571 	vp->v_flag &= ~VAGE;
2572 	vp->v_flag |= VFREE;
2573 	splx(s);
2574 }
2575 
2576 void
2577 vbusy(vp)
2578 	struct vnode *vp;
2579 {
2580 	int s;
2581 
2582 	s = splbio();
2583 	simple_lock(&vnode_free_list_slock);
2584 	if (vp->v_flag & VTBFREE) {
2585 		TAILQ_REMOVE(&vnode_tobefree_list, vp, v_freelist);
2586 		vp->v_flag &= ~VTBFREE;
2587 	} else {
2588 		TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
2589 		freevnodes--;
2590 	}
2591 	simple_unlock(&vnode_free_list_slock);
2592 	vp->v_flag &= ~(VFREE|VAGE);
2593 	splx(s);
2594 }
2595 
2596 /*
2597  * Record a process's interest in events which might happen to
2598  * a vnode.  Because poll uses the historic select-style interface
2599  * internally, this routine serves as both the ``check for any
2600  * pending events'' and the ``record my interest in future events''
2601  * functions.  (These are done together, while the lock is held,
2602  * to avoid race conditions.)
2603  */
2604 int
2605 vn_pollrecord(vp, p, events)
2606 	struct vnode *vp;
2607 	struct proc *p;
2608 	short events;
2609 {
2610 	simple_lock(&vp->v_pollinfo.vpi_lock);
2611 	if (vp->v_pollinfo.vpi_revents & events) {
2612 		/*
2613 		 * This leaves events we are not interested
2614 		 * in available for the other process which
2615 		 * which presumably had requested them
2616 		 * (otherwise they would never have been
2617 		 * recorded).
2618 		 */
2619 		events &= vp->v_pollinfo.vpi_revents;
2620 		vp->v_pollinfo.vpi_revents &= ~events;
2621 
2622 		simple_unlock(&vp->v_pollinfo.vpi_lock);
2623 		return events;
2624 	}
2625 	vp->v_pollinfo.vpi_events |= events;
2626 	selrecord(p, &vp->v_pollinfo.vpi_selinfo);
2627 	simple_unlock(&vp->v_pollinfo.vpi_lock);
2628 	return 0;
2629 }
2630 
2631 /*
2632  * Note the occurrence of an event.  If the VN_POLLEVENT macro is used,
2633  * it is possible for us to miss an event due to race conditions, but
2634  * that condition is expected to be rare, so for the moment it is the
2635  * preferred interface.
2636  */
2637 void
2638 vn_pollevent(vp, events)
2639 	struct vnode *vp;
2640 	short events;
2641 {
2642 	simple_lock(&vp->v_pollinfo.vpi_lock);
2643 	if (vp->v_pollinfo.vpi_events & events) {
2644 		/*
2645 		 * We clear vpi_events so that we don't
2646 		 * call selwakeup() twice if two events are
2647 		 * posted before the polling process(es) is
2648 		 * awakened.  This also ensures that we take at
2649 		 * most one selwakeup() if the polling process
2650 		 * is no longer interested.  However, it does
2651 		 * mean that only one event can be noticed at
2652 		 * a time.  (Perhaps we should only clear those
2653 		 * event bits which we note?) XXX
2654 		 */
2655 		vp->v_pollinfo.vpi_events = 0;	/* &= ~events ??? */
2656 		vp->v_pollinfo.vpi_revents |= events;
2657 		selwakeup(&vp->v_pollinfo.vpi_selinfo);
2658 	}
2659 	simple_unlock(&vp->v_pollinfo.vpi_lock);
2660 }
2661 
2662 /*
2663  * Wake up anyone polling on vp because it is being revoked.
2664  * This depends on dead_poll() returning POLLHUP for correct
2665  * behavior.
2666  */
2667 void
2668 vn_pollgone(vp)
2669 	struct vnode *vp;
2670 {
2671 	simple_lock(&vp->v_pollinfo.vpi_lock);
2672 	if (vp->v_pollinfo.vpi_events) {
2673 		vp->v_pollinfo.vpi_events = 0;
2674 		selwakeup(&vp->v_pollinfo.vpi_selinfo);
2675 	}
2676 	simple_unlock(&vp->v_pollinfo.vpi_lock);
2677 }
2678 
2679 
2680 
2681 /*
2682  * Routine to create and manage a filesystem syncer vnode.
2683  */
2684 #define sync_close ((int (*) __P((struct  vop_close_args *)))nullop)
2685 static int	sync_fsync __P((struct  vop_fsync_args *));
2686 static int	sync_inactive __P((struct  vop_inactive_args *));
2687 static int	sync_reclaim  __P((struct  vop_reclaim_args *));
2688 #define sync_lock ((int (*) __P((struct  vop_lock_args *)))vop_nolock)
2689 #define sync_unlock ((int (*) __P((struct  vop_unlock_args *)))vop_nounlock)
2690 static int	sync_print __P((struct vop_print_args *));
2691 #define sync_islocked ((int(*) __P((struct vop_islocked_args *)))vop_noislocked)
2692 
2693 static vop_t **sync_vnodeop_p;
2694 static struct vnodeopv_entry_desc sync_vnodeop_entries[] = {
2695 	{ &vop_default_desc,	(vop_t *) vop_eopnotsupp },
2696 	{ &vop_close_desc,	(vop_t *) sync_close },		/* close */
2697 	{ &vop_fsync_desc,	(vop_t *) sync_fsync },		/* fsync */
2698 	{ &vop_inactive_desc,	(vop_t *) sync_inactive },	/* inactive */
2699 	{ &vop_reclaim_desc,	(vop_t *) sync_reclaim },	/* reclaim */
2700 	{ &vop_lock_desc,	(vop_t *) sync_lock },		/* lock */
2701 	{ &vop_unlock_desc,	(vop_t *) sync_unlock },	/* unlock */
2702 	{ &vop_print_desc,	(vop_t *) sync_print },		/* print */
2703 	{ &vop_islocked_desc,	(vop_t *) sync_islocked },	/* islocked */
2704 	{ NULL, NULL }
2705 };
2706 static struct vnodeopv_desc sync_vnodeop_opv_desc =
2707 	{ &sync_vnodeop_p, sync_vnodeop_entries };
2708 
2709 VNODEOP_SET(sync_vnodeop_opv_desc);
2710 
2711 /*
2712  * Create a new filesystem syncer vnode for the specified mount point.
2713  */
2714 int
2715 vfs_allocate_syncvnode(mp)
2716 	struct mount *mp;
2717 {
2718 	struct vnode *vp;
2719 	static long start, incr, next;
2720 	int error;
2721 
2722 	/* Allocate a new vnode */
2723 	if ((error = getnewvnode(VT_VFS, mp, sync_vnodeop_p, &vp)) != 0) {
2724 		mp->mnt_syncer = NULL;
2725 		return (error);
2726 	}
2727 	vp->v_type = VNON;
2728 	/*
2729 	 * Place the vnode onto the syncer worklist. We attempt to
2730 	 * scatter them about on the list so that they will go off
2731 	 * at evenly distributed times even if all the filesystems
2732 	 * are mounted at once.
2733 	 */
2734 	next += incr;
2735 	if (next == 0 || next > syncer_maxdelay) {
2736 		start /= 2;
2737 		incr /= 2;
2738 		if (start == 0) {
2739 			start = syncer_maxdelay / 2;
2740 			incr = syncer_maxdelay;
2741 		}
2742 		next = start;
2743 	}
2744 	vn_syncer_add_to_worklist(vp, syncdelay > 0 ? next % syncdelay : 0);
2745 	mp->mnt_syncer = vp;
2746 	return (0);
2747 }
2748 
2749 /*
2750  * Do a lazy sync of the filesystem.
2751  */
2752 static int
2753 sync_fsync(ap)
2754 	struct vop_fsync_args /* {
2755 		struct vnode *a_vp;
2756 		struct ucred *a_cred;
2757 		int a_waitfor;
2758 		struct proc *a_p;
2759 	} */ *ap;
2760 {
2761 	struct vnode *syncvp = ap->a_vp;
2762 	struct mount *mp = syncvp->v_mount;
2763 	struct proc *p = ap->a_p;
2764 	int asyncflag;
2765 
2766 	/*
2767 	 * We only need to do something if this is a lazy evaluation.
2768 	 */
2769 	if (ap->a_waitfor != MNT_LAZY)
2770 		return (0);
2771 
2772 	/*
2773 	 * Move ourselves to the back of the sync list.
2774 	 */
2775 	vn_syncer_add_to_worklist(syncvp, syncdelay);
2776 
2777 	/*
2778 	 * Walk the list of vnodes pushing all that are dirty and
2779 	 * not already on the sync list.
2780 	 */
2781 	simple_lock(&mountlist_slock);
2782 	if (vfs_busy(mp, LK_EXCLUSIVE | LK_NOWAIT, &mountlist_slock, p) != 0) {
2783 		simple_unlock(&mountlist_slock);
2784 		return (0);
2785 	}
2786 	asyncflag = mp->mnt_flag & MNT_ASYNC;
2787 	mp->mnt_flag &= ~MNT_ASYNC;
2788 	vfs_msync(mp, MNT_NOWAIT);
2789 	VFS_SYNC(mp, MNT_LAZY, ap->a_cred, p);
2790 	if (asyncflag)
2791 		mp->mnt_flag |= MNT_ASYNC;
2792 	vfs_unbusy(mp, p);
2793 	return (0);
2794 }
2795 
2796 /*
2797  * The syncer vnode is no referenced.
2798  */
2799 static int
2800 sync_inactive(ap)
2801 	struct vop_inactive_args /* {
2802 		struct vnode *a_vp;
2803 		struct proc *a_p;
2804 	} */ *ap;
2805 {
2806 
2807 	vgone(ap->a_vp);
2808 	return (0);
2809 }
2810 
2811 /*
2812  * The syncer vnode is no longer needed and is being decommissioned.
2813  *
2814  * Modifications to the worklist must be protected at splbio().
2815  */
2816 static int
2817 sync_reclaim(ap)
2818 	struct vop_reclaim_args /* {
2819 		struct vnode *a_vp;
2820 	} */ *ap;
2821 {
2822 	struct vnode *vp = ap->a_vp;
2823 	int s;
2824 
2825 	s = splbio();
2826 	vp->v_mount->mnt_syncer = NULL;
2827 	if (vp->v_flag & VONWORKLST) {
2828 		LIST_REMOVE(vp, v_synclist);
2829 		vp->v_flag &= ~VONWORKLST;
2830 	}
2831 	splx(s);
2832 
2833 	return (0);
2834 }
2835 
2836 /*
2837  * Print out a syncer vnode.
2838  */
2839 static int
2840 sync_print(ap)
2841 	struct vop_print_args /* {
2842 		struct vnode *a_vp;
2843 	} */ *ap;
2844 {
2845 	struct vnode *vp = ap->a_vp;
2846 
2847 	printf("syncer vnode");
2848 	if (vp->v_vnlock != NULL)
2849 		lockmgr_printinfo(vp->v_vnlock);
2850 	printf("\n");
2851 	return (0);
2852 }
2853 
2854 /*
2855  * extract the dev_t from a VBLK or VCHR
2856  */
2857 dev_t
2858 vn_todev(vp)
2859 	struct vnode *vp;
2860 {
2861 	if (vp->v_type != VBLK && vp->v_type != VCHR)
2862 		return (NODEV);
2863 	return (vp->v_rdev);
2864 }
2865 
2866 /*
2867  * Check if vnode represents a disk device
2868  */
2869 int
2870 vn_isdisk(vp)
2871 	struct vnode *vp;
2872 {
2873 	if (vp->v_type != VBLK)
2874 		return (0);
2875 	if (!devsw(vp->v_rdev))
2876 		return (0);
2877 	if (!(devsw(vp->v_rdev)->d_flags & D_DISK))
2878 		return (0);
2879 	return (1);
2880 }
2881 
2882