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