xref: /freebsd/sys/kern/vfs_subr.c (revision c98323078dede7579020518ec84cdcb478e5c142)
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  * 4. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	@(#)vfs_subr.c	8.31 (Berkeley) 5/26/95
35  */
36 
37 /*
38  * External virtual filesystem routines
39  */
40 
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
43 
44 #include "opt_ddb.h"
45 #include "opt_mac.h"
46 
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/bio.h>
50 #include <sys/buf.h>
51 #include <sys/conf.h>
52 #include <sys/event.h>
53 #include <sys/eventhandler.h>
54 #include <sys/extattr.h>
55 #include <sys/fcntl.h>
56 #include <sys/kdb.h>
57 #include <sys/kernel.h>
58 #include <sys/kthread.h>
59 #include <sys/mac.h>
60 #include <sys/malloc.h>
61 #include <sys/mount.h>
62 #include <sys/namei.h>
63 #include <sys/sleepqueue.h>
64 #include <sys/stat.h>
65 #include <sys/sysctl.h>
66 #include <sys/syslog.h>
67 #include <sys/vmmeter.h>
68 #include <sys/vnode.h>
69 
70 #include <vm/vm.h>
71 #include <vm/vm_object.h>
72 #include <vm/vm_extern.h>
73 #include <vm/pmap.h>
74 #include <vm/vm_map.h>
75 #include <vm/vm_page.h>
76 #include <vm/vm_kern.h>
77 #include <vm/uma.h>
78 
79 static MALLOC_DEFINE(M_NETADDR, "Export Host", "Export host address structure");
80 
81 static void	addalias(struct vnode *vp, struct cdev *nvp_rdev);
82 static void	delmntque(struct vnode *vp);
83 static void	insmntque(struct vnode *vp, struct mount *mp);
84 static void	vclean(struct vnode *vp, int flags, struct thread *td);
85 static void	vlruvp(struct vnode *vp);
86 static int	flushbuflist(struct buf *blist, int flags, struct vnode *vp,
87 		    int slpflag, int slptimeo, int *errorp);
88 static void	syncer_shutdown(void *arg, int howto);
89 static int	vtryrecycle(struct vnode *vp);
90 static void	vx_lock(struct vnode *vp);
91 static void	vx_unlock(struct vnode *vp);
92 static void	vgonechrl(struct vnode *vp, struct thread *td);
93 
94 
95 /*
96  * Number of vnodes in existence.  Increased whenever getnewvnode()
97  * allocates a new vnode, never decreased.
98  */
99 static unsigned long	numvnodes;
100 
101 SYSCTL_LONG(_vfs, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0, "");
102 
103 /*
104  * Conversion tables for conversion from vnode types to inode formats
105  * and back.
106  */
107 enum vtype iftovt_tab[16] = {
108 	VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
109 	VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
110 };
111 int vttoif_tab[9] = {
112 	0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
113 	S_IFSOCK, S_IFIFO, S_IFMT,
114 };
115 
116 /*
117  * List of vnodes that are ready for recycling.
118  */
119 static TAILQ_HEAD(freelst, vnode) vnode_free_list;
120 
121 /*
122  * Minimum number of free vnodes.  If there are fewer than this free vnodes,
123  * getnewvnode() will return a newly allocated vnode.
124  */
125 static u_long wantfreevnodes = 25;
126 SYSCTL_LONG(_vfs, OID_AUTO, wantfreevnodes, CTLFLAG_RW, &wantfreevnodes, 0, "");
127 /* Number of vnodes in the free list. */
128 static u_long freevnodes;
129 SYSCTL_LONG(_vfs, OID_AUTO, freevnodes, CTLFLAG_RD, &freevnodes, 0, "");
130 
131 /*
132  * Various variables used for debugging the new implementation of
133  * reassignbuf().
134  * XXX these are probably of (very) limited utility now.
135  */
136 static int reassignbufcalls;
137 SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW, &reassignbufcalls, 0, "");
138 static int nameileafonly;
139 SYSCTL_INT(_vfs, OID_AUTO, nameileafonly, CTLFLAG_RW, &nameileafonly, 0, "");
140 
141 /*
142  * Cache for the mount type id assigned to NFS.  This is used for
143  * special checks in nfs/nfs_nqlease.c and vm/vnode_pager.c.
144  */
145 int	nfs_mount_type = -1;
146 
147 /* To keep more than one thread at a time from running vfs_getnewfsid */
148 static struct mtx mntid_mtx;
149 
150 /*
151  * Lock for any access to the following:
152  *	vnode_free_list
153  *	numvnodes
154  *	freevnodes
155  */
156 static struct mtx vnode_free_list_mtx;
157 
158 /*
159  * For any iteration/modification of dev->si_hlist (linked through
160  * v_specnext)
161  */
162 static struct mtx spechash_mtx;
163 
164 /* Publicly exported FS */
165 struct nfs_public nfs_pub;
166 
167 /* Zone for allocation of new vnodes - used exclusively by getnewvnode() */
168 static uma_zone_t vnode_zone;
169 static uma_zone_t vnodepoll_zone;
170 
171 /* Set to 1 to print out reclaim of active vnodes */
172 int	prtactive;
173 
174 /*
175  * The workitem queue.
176  *
177  * It is useful to delay writes of file data and filesystem metadata
178  * for tens of seconds so that quickly created and deleted files need
179  * not waste disk bandwidth being created and removed. To realize this,
180  * we append vnodes to a "workitem" queue. When running with a soft
181  * updates implementation, most pending metadata dependencies should
182  * not wait for more than a few seconds. Thus, mounted on block devices
183  * are delayed only about a half the time that file data is delayed.
184  * Similarly, directory updates are more critical, so are only delayed
185  * about a third the time that file data is delayed. Thus, there are
186  * SYNCER_MAXDELAY queues that are processed round-robin at a rate of
187  * one each second (driven off the filesystem syncer process). The
188  * syncer_delayno variable indicates the next queue that is to be processed.
189  * Items that need to be processed soon are placed in this queue:
190  *
191  *	syncer_workitem_pending[syncer_delayno]
192  *
193  * A delay of fifteen seconds is done by placing the request fifteen
194  * entries later in the queue:
195  *
196  *	syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask]
197  *
198  */
199 static int syncer_delayno;
200 static long syncer_mask;
201 LIST_HEAD(synclist, vnode);
202 static struct synclist *syncer_workitem_pending;
203 /*
204  * The sync_mtx protects:
205  *	vp->v_synclist
206  *	sync_vnode_count
207  *	syncer_delayno
208  *	syncer_state
209  *	syncer_workitem_pending
210  *	syncer_worklist_len
211  *	rushjob
212  */
213 static struct mtx sync_mtx;
214 
215 #define SYNCER_MAXDELAY		32
216 static int syncer_maxdelay = SYNCER_MAXDELAY;	/* maximum delay time */
217 static int syncdelay = 30;		/* max time to delay syncing data */
218 static int filedelay = 30;		/* time to delay syncing files */
219 SYSCTL_INT(_kern, OID_AUTO, filedelay, CTLFLAG_RW, &filedelay, 0, "");
220 static int dirdelay = 29;		/* time to delay syncing directories */
221 SYSCTL_INT(_kern, OID_AUTO, dirdelay, CTLFLAG_RW, &dirdelay, 0, "");
222 static int metadelay = 28;		/* time to delay syncing metadata */
223 SYSCTL_INT(_kern, OID_AUTO, metadelay, CTLFLAG_RW, &metadelay, 0, "");
224 static int rushjob;		/* number of slots to run ASAP */
225 static int stat_rush_requests;	/* number of times I/O speeded up */
226 SYSCTL_INT(_debug, OID_AUTO, rush_requests, CTLFLAG_RW, &stat_rush_requests, 0, "");
227 
228 /*
229  * When shutting down the syncer, run it at four times normal speed.
230  */
231 #define SYNCER_SHUTDOWN_SPEEDUP		4
232 static int sync_vnode_count;
233 static int syncer_worklist_len;
234 static enum { SYNCER_RUNNING, SYNCER_SHUTTING_DOWN, SYNCER_FINAL_DELAY }
235     syncer_state;
236 
237 /*
238  * Number of vnodes we want to exist at any one time.  This is mostly used
239  * to size hash tables in vnode-related code.  It is normally not used in
240  * getnewvnode(), as wantfreevnodes is normally nonzero.)
241  *
242  * XXX desiredvnodes is historical cruft and should not exist.
243  */
244 int desiredvnodes;
245 SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RW,
246     &desiredvnodes, 0, "Maximum number of vnodes");
247 static int minvnodes;
248 SYSCTL_INT(_kern, OID_AUTO, minvnodes, CTLFLAG_RW,
249     &minvnodes, 0, "Minimum number of vnodes");
250 static int vnlru_nowhere;
251 SYSCTL_INT(_debug, OID_AUTO, vnlru_nowhere, CTLFLAG_RW,
252     &vnlru_nowhere, 0, "Number of times the vnlru process ran without success");
253 
254 /* Hook for calling soft updates. */
255 int (*softdep_process_worklist_hook)(struct mount *);
256 
257 /*
258  * Initialize the vnode management data structures.
259  */
260 static void
261 vntblinit(void *dummy __unused)
262 {
263 
264 	/*
265 	 * Desiredvnodes is a function of the physical memory size and
266 	 * the kernel's heap size.  Specifically, desiredvnodes scales
267 	 * in proportion to the physical memory size until two fifths
268 	 * of the kernel's heap size is consumed by vnodes and vm
269 	 * objects.
270 	 */
271 	desiredvnodes = min(maxproc + cnt.v_page_count / 4, 2 * vm_kmem_size /
272 	    (5 * (sizeof(struct vm_object) + sizeof(struct vnode))));
273 	minvnodes = desiredvnodes / 4;
274 	mtx_init(&mountlist_mtx, "mountlist", NULL, MTX_DEF);
275 	mtx_init(&mntid_mtx, "mntid", NULL, MTX_DEF);
276 	mtx_init(&spechash_mtx, "spechash", NULL, MTX_DEF);
277 	TAILQ_INIT(&vnode_free_list);
278 	mtx_init(&vnode_free_list_mtx, "vnode_free_list", NULL, MTX_DEF);
279 	vnode_zone = uma_zcreate("VNODE", sizeof (struct vnode), NULL, NULL,
280 	    NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
281 	vnodepoll_zone = uma_zcreate("VNODEPOLL", sizeof (struct vpollinfo),
282 	      NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
283 	/*
284 	 * Initialize the filesystem syncer.
285 	 */
286 	syncer_workitem_pending = hashinit(syncer_maxdelay, M_VNODE,
287 		&syncer_mask);
288 	syncer_maxdelay = syncer_mask + 1;
289 	mtx_init(&sync_mtx, "Syncer mtx", NULL, MTX_DEF);
290 }
291 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_FIRST, vntblinit, NULL)
292 
293 
294 /*
295  * Mark a mount point as busy. Used to synchronize access and to delay
296  * unmounting. Interlock is not released on failure.
297  */
298 int
299 vfs_busy(mp, flags, interlkp, td)
300 	struct mount *mp;
301 	int flags;
302 	struct mtx *interlkp;
303 	struct thread *td;
304 {
305 	int lkflags;
306 
307 	if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
308 		if (flags & LK_NOWAIT)
309 			return (ENOENT);
310 		mp->mnt_kern_flag |= MNTK_MWAIT;
311 		/*
312 		 * Since all busy locks are shared except the exclusive
313 		 * lock granted when unmounting, the only place that a
314 		 * wakeup needs to be done is at the release of the
315 		 * exclusive lock at the end of dounmount.
316 		 */
317 		msleep(mp, interlkp, PVFS, "vfs_busy", 0);
318 		return (ENOENT);
319 	}
320 	lkflags = LK_SHARED | LK_NOPAUSE;
321 	if (interlkp)
322 		lkflags |= LK_INTERLOCK;
323 	if (lockmgr(&mp->mnt_lock, lkflags, interlkp, td))
324 		panic("vfs_busy: unexpected lock failure");
325 	return (0);
326 }
327 
328 /*
329  * Free a busy filesystem.
330  */
331 void
332 vfs_unbusy(mp, td)
333 	struct mount *mp;
334 	struct thread *td;
335 {
336 
337 	lockmgr(&mp->mnt_lock, LK_RELEASE, NULL, td);
338 }
339 
340 /*
341  * Lookup a mount point by filesystem identifier.
342  */
343 struct mount *
344 vfs_getvfs(fsid)
345 	fsid_t *fsid;
346 {
347 	register struct mount *mp;
348 
349 	mtx_lock(&mountlist_mtx);
350 	TAILQ_FOREACH(mp, &mountlist, mnt_list) {
351 		if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
352 		    mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
353 			mtx_unlock(&mountlist_mtx);
354 			return (mp);
355 		}
356 	}
357 	mtx_unlock(&mountlist_mtx);
358 	return ((struct mount *) 0);
359 }
360 
361 /*
362  * Check if a user can access priveledged mount options.
363  */
364 int
365 vfs_suser(struct mount *mp, struct thread *td)
366 {
367 	int error;
368 
369 	if ((mp->mnt_flag & MNT_USER) == 0 ||
370 	    mp->mnt_cred->cr_uid != td->td_ucred->cr_uid) {
371 		if ((error = suser(td)) != 0)
372 			return (error);
373 	}
374 	return (0);
375 }
376 
377 /*
378  * Get a new unique fsid.  Try to make its val[0] unique, since this value
379  * will be used to create fake device numbers for stat().  Also try (but
380  * not so hard) make its val[0] unique mod 2^16, since some emulators only
381  * support 16-bit device numbers.  We end up with unique val[0]'s for the
382  * first 2^16 calls and unique val[0]'s mod 2^16 for the first 2^8 calls.
383  *
384  * Keep in mind that several mounts may be running in parallel.  Starting
385  * the search one past where the previous search terminated is both a
386  * micro-optimization and a defense against returning the same fsid to
387  * different mounts.
388  */
389 void
390 vfs_getnewfsid(mp)
391 	struct mount *mp;
392 {
393 	static u_int16_t mntid_base;
394 	fsid_t tfsid;
395 	int mtype;
396 
397 	mtx_lock(&mntid_mtx);
398 	mtype = mp->mnt_vfc->vfc_typenum;
399 	tfsid.val[1] = mtype;
400 	mtype = (mtype & 0xFF) << 24;
401 	for (;;) {
402 		tfsid.val[0] = makedev(255,
403 		    mtype | ((mntid_base & 0xFF00) << 8) | (mntid_base & 0xFF));
404 		mntid_base++;
405 		if (vfs_getvfs(&tfsid) == NULL)
406 			break;
407 	}
408 	mp->mnt_stat.f_fsid.val[0] = tfsid.val[0];
409 	mp->mnt_stat.f_fsid.val[1] = tfsid.val[1];
410 	mtx_unlock(&mntid_mtx);
411 }
412 
413 /*
414  * Knob to control the precision of file timestamps:
415  *
416  *   0 = seconds only; nanoseconds zeroed.
417  *   1 = seconds and nanoseconds, accurate within 1/HZ.
418  *   2 = seconds and nanoseconds, truncated to microseconds.
419  * >=3 = seconds and nanoseconds, maximum precision.
420  */
421 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC };
422 
423 static int timestamp_precision = TSP_SEC;
424 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW,
425     &timestamp_precision, 0, "");
426 
427 /*
428  * Get a current timestamp.
429  */
430 void
431 vfs_timestamp(tsp)
432 	struct timespec *tsp;
433 {
434 	struct timeval tv;
435 
436 	switch (timestamp_precision) {
437 	case TSP_SEC:
438 		tsp->tv_sec = time_second;
439 		tsp->tv_nsec = 0;
440 		break;
441 	case TSP_HZ:
442 		getnanotime(tsp);
443 		break;
444 	case TSP_USEC:
445 		microtime(&tv);
446 		TIMEVAL_TO_TIMESPEC(&tv, tsp);
447 		break;
448 	case TSP_NSEC:
449 	default:
450 		nanotime(tsp);
451 		break;
452 	}
453 }
454 
455 /*
456  * Set vnode attributes to VNOVAL
457  */
458 void
459 vattr_null(vap)
460 	register struct vattr *vap;
461 {
462 
463 	vap->va_type = VNON;
464 	vap->va_size = VNOVAL;
465 	vap->va_bytes = VNOVAL;
466 	vap->va_mode = VNOVAL;
467 	vap->va_nlink = VNOVAL;
468 	vap->va_uid = VNOVAL;
469 	vap->va_gid = VNOVAL;
470 	vap->va_fsid = VNOVAL;
471 	vap->va_fileid = VNOVAL;
472 	vap->va_blocksize = VNOVAL;
473 	vap->va_rdev = VNOVAL;
474 	vap->va_atime.tv_sec = VNOVAL;
475 	vap->va_atime.tv_nsec = VNOVAL;
476 	vap->va_mtime.tv_sec = VNOVAL;
477 	vap->va_mtime.tv_nsec = VNOVAL;
478 	vap->va_ctime.tv_sec = VNOVAL;
479 	vap->va_ctime.tv_nsec = VNOVAL;
480 	vap->va_birthtime.tv_sec = VNOVAL;
481 	vap->va_birthtime.tv_nsec = VNOVAL;
482 	vap->va_flags = VNOVAL;
483 	vap->va_gen = VNOVAL;
484 	vap->va_vaflags = 0;
485 }
486 
487 /*
488  * This routine is called when we have too many vnodes.  It attempts
489  * to free <count> vnodes and will potentially free vnodes that still
490  * have VM backing store (VM backing store is typically the cause
491  * of a vnode blowout so we want to do this).  Therefore, this operation
492  * is not considered cheap.
493  *
494  * A number of conditions may prevent a vnode from being reclaimed.
495  * the buffer cache may have references on the vnode, a directory
496  * vnode may still have references due to the namei cache representing
497  * underlying files, or the vnode may be in active use.   It is not
498  * desireable to reuse such vnodes.  These conditions may cause the
499  * number of vnodes to reach some minimum value regardless of what
500  * you set kern.maxvnodes to.  Do not set kern.maxvnodes too low.
501  */
502 static int
503 vlrureclaim(struct mount *mp)
504 {
505 	struct vnode *vp;
506 	int done;
507 	int trigger;
508 	int usevnodes;
509 	int count;
510 
511 	/*
512 	 * Calculate the trigger point, don't allow user
513 	 * screwups to blow us up.   This prevents us from
514 	 * recycling vnodes with lots of resident pages.  We
515 	 * aren't trying to free memory, we are trying to
516 	 * free vnodes.
517 	 */
518 	usevnodes = desiredvnodes;
519 	if (usevnodes <= 0)
520 		usevnodes = 1;
521 	trigger = cnt.v_page_count * 2 / usevnodes;
522 
523 	done = 0;
524 	MNT_ILOCK(mp);
525 	count = mp->mnt_nvnodelistsize / 10 + 1;
526 	while (count && (vp = TAILQ_FIRST(&mp->mnt_nvnodelist)) != NULL) {
527 		TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
528 		TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
529 
530 		if (vp->v_type != VNON &&
531 		    vp->v_type != VBAD &&
532 		    VI_TRYLOCK(vp)) {
533 			if (VMIGHTFREE(vp) &&           /* critical path opt */
534 			    (vp->v_object == NULL ||
535 			    vp->v_object->resident_page_count < trigger)) {
536 				MNT_IUNLOCK(mp);
537 				vgonel(vp, curthread);
538 				done++;
539 				MNT_ILOCK(mp);
540 			} else
541 				VI_UNLOCK(vp);
542 		}
543 		--count;
544 	}
545 	MNT_IUNLOCK(mp);
546 	return done;
547 }
548 
549 /*
550  * Attempt to recycle vnodes in a context that is always safe to block.
551  * Calling vlrurecycle() from the bowels of filesystem code has some
552  * interesting deadlock problems.
553  */
554 static struct proc *vnlruproc;
555 static int vnlruproc_sig;
556 
557 static void
558 vnlru_proc(void)
559 {
560 	struct mount *mp, *nmp;
561 	int done;
562 	struct proc *p = vnlruproc;
563 	struct thread *td = FIRST_THREAD_IN_PROC(p);
564 
565 	mtx_lock(&Giant);
566 
567 	EVENTHANDLER_REGISTER(shutdown_pre_sync, kproc_shutdown, p,
568 	    SHUTDOWN_PRI_FIRST);
569 
570 	for (;;) {
571 		kthread_suspend_check(p);
572 		mtx_lock(&vnode_free_list_mtx);
573 		if (numvnodes - freevnodes <= desiredvnodes * 9 / 10) {
574 			mtx_unlock(&vnode_free_list_mtx);
575 			vnlruproc_sig = 0;
576 			wakeup(&vnlruproc_sig);
577 			tsleep(vnlruproc, PVFS, "vlruwt", hz);
578 			continue;
579 		}
580 		mtx_unlock(&vnode_free_list_mtx);
581 		done = 0;
582 		mtx_lock(&mountlist_mtx);
583 		for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
584 			if (vfs_busy(mp, LK_NOWAIT, &mountlist_mtx, td)) {
585 				nmp = TAILQ_NEXT(mp, mnt_list);
586 				continue;
587 			}
588 			done += vlrureclaim(mp);
589 			mtx_lock(&mountlist_mtx);
590 			nmp = TAILQ_NEXT(mp, mnt_list);
591 			vfs_unbusy(mp, td);
592 		}
593 		mtx_unlock(&mountlist_mtx);
594 		if (done == 0) {
595 #if 0
596 			/* These messages are temporary debugging aids */
597 			if (vnlru_nowhere < 5)
598 				printf("vnlru process getting nowhere..\n");
599 			else if (vnlru_nowhere == 5)
600 				printf("vnlru process messages stopped.\n");
601 #endif
602 			vnlru_nowhere++;
603 			tsleep(vnlruproc, PPAUSE, "vlrup", hz * 3);
604 		}
605 	}
606 }
607 
608 static struct kproc_desc vnlru_kp = {
609 	"vnlru",
610 	vnlru_proc,
611 	&vnlruproc
612 };
613 SYSINIT(vnlru, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &vnlru_kp)
614 
615 
616 /*
617  * Routines having to do with the management of the vnode table.
618  */
619 
620 /*
621  * Check to see if a free vnode can be recycled. If it can,
622  * recycle it and return it with the vnode interlock held.
623  */
624 static int
625 vtryrecycle(struct vnode *vp)
626 {
627 	struct thread *td = curthread;
628 	vm_object_t object;
629 	struct mount *vnmp;
630 	int error;
631 
632 	/* Don't recycle if we can't get the interlock */
633 	if (!VI_TRYLOCK(vp))
634 		return (EWOULDBLOCK);
635 	/*
636 	 * This vnode may found and locked via some other list, if so we
637 	 * can't recycle it yet.
638 	 */
639 	if (vn_lock(vp, LK_INTERLOCK | LK_EXCLUSIVE | LK_NOWAIT, td) != 0)
640 		return (EWOULDBLOCK);
641 	/*
642 	 * Don't recycle if its filesystem is being suspended.
643 	 */
644 	if (vn_start_write(vp, &vnmp, V_NOWAIT) != 0) {
645 		VOP_UNLOCK(vp, 0, td);
646 		return (EBUSY);
647 	}
648 
649 	/*
650 	 * Don't recycle if we still have cached pages.
651 	 */
652 	if (VOP_GETVOBJECT(vp, &object) == 0) {
653 		VM_OBJECT_LOCK(object);
654 		if (object->resident_page_count ||
655 		    object->ref_count) {
656 			VM_OBJECT_UNLOCK(object);
657 			error = EBUSY;
658 			goto done;
659 		}
660 		VM_OBJECT_UNLOCK(object);
661 	}
662 	if (LIST_FIRST(&vp->v_cache_src)) {
663 		/*
664 		 * note: nameileafonly sysctl is temporary,
665 		 * for debugging only, and will eventually be
666 		 * removed.
667 		 */
668 		if (nameileafonly > 0) {
669 			/*
670 			 * Do not reuse namei-cached directory
671 			 * vnodes that have cached
672 			 * subdirectories.
673 			 */
674 			if (cache_leaf_test(vp) < 0) {
675 				error = EISDIR;
676 				goto done;
677 			}
678 		} else if (nameileafonly < 0 ||
679 			    vmiodirenable == 0) {
680 			/*
681 			 * Do not reuse namei-cached directory
682 			 * vnodes if nameileafonly is -1 or
683 			 * if VMIO backing for directories is
684 			 * turned off (otherwise we reuse them
685 			 * too quickly).
686 			 */
687 			error = EBUSY;
688 			goto done;
689 		}
690 	}
691 	/*
692 	 * If we got this far, we need to acquire the interlock and see if
693 	 * anyone picked up this vnode from another list.  If not, we will
694 	 * mark it with XLOCK via vgonel() so that anyone who does find it
695 	 * will skip over it.
696 	 */
697 	VI_LOCK(vp);
698 	if (VSHOULDBUSY(vp) && (vp->v_iflag & VI_XLOCK) == 0) {
699 		VI_UNLOCK(vp);
700 		error = EBUSY;
701 		goto done;
702 	}
703 	mtx_lock(&vnode_free_list_mtx);
704 	TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
705 	vp->v_iflag &= ~VI_FREE;
706 	mtx_unlock(&vnode_free_list_mtx);
707 	vp->v_iflag |= VI_DOOMED;
708 	if (vp->v_type != VBAD) {
709 		VOP_UNLOCK(vp, 0, td);
710 		vgonel(vp, td);
711 		VI_LOCK(vp);
712 	} else
713 		VOP_UNLOCK(vp, 0, td);
714 	vn_finished_write(vnmp);
715 	return (0);
716 done:
717 	VOP_UNLOCK(vp, 0, td);
718 	vn_finished_write(vnmp);
719 	return (error);
720 }
721 
722 /*
723  * Return the next vnode from the free list.
724  */
725 int
726 getnewvnode(tag, mp, vops, vpp)
727 	const char *tag;
728 	struct mount *mp;
729 	vop_t **vops;
730 	struct vnode **vpp;
731 {
732 	struct vnode *vp = NULL;
733 	struct vpollinfo *pollinfo = NULL;
734 
735 	mtx_lock(&vnode_free_list_mtx);
736 
737 	/*
738 	 * Try to reuse vnodes if we hit the max.  This situation only
739 	 * occurs in certain large-memory (2G+) situations.  We cannot
740 	 * attempt to directly reclaim vnodes due to nasty recursion
741 	 * problems.
742 	 */
743 	while (numvnodes - freevnodes > desiredvnodes) {
744 		if (vnlruproc_sig == 0) {
745 			vnlruproc_sig = 1;      /* avoid unnecessary wakeups */
746 			wakeup(vnlruproc);
747 		}
748 		mtx_unlock(&vnode_free_list_mtx);
749 		tsleep(&vnlruproc_sig, PVFS, "vlruwk", hz);
750 		mtx_lock(&vnode_free_list_mtx);
751 	}
752 
753 	/*
754 	 * Attempt to reuse a vnode already on the free list, allocating
755 	 * a new vnode if we can't find one or if we have not reached a
756 	 * good minimum for good LRU performance.
757 	 */
758 
759 	if (freevnodes >= wantfreevnodes && numvnodes >= minvnodes) {
760 		int error;
761 		int count;
762 
763 		for (count = 0; count < freevnodes; count++) {
764 			vp = TAILQ_FIRST(&vnode_free_list);
765 
766 			KASSERT(vp->v_usecount == 0 &&
767 			    (vp->v_iflag & VI_DOINGINACT) == 0,
768 			    ("getnewvnode: free vnode isn't"));
769 
770 			TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
771 			TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
772 			mtx_unlock(&vnode_free_list_mtx);
773 			error = vtryrecycle(vp);
774 			mtx_lock(&vnode_free_list_mtx);
775 			if (error == 0)
776 				break;
777 			vp = NULL;
778 		}
779 	}
780 	if (vp) {
781 		freevnodes--;
782 		mtx_unlock(&vnode_free_list_mtx);
783 
784 #ifdef INVARIANTS
785 		{
786 			if (vp->v_data)
787 				panic("cleaned vnode isn't");
788 			if (vp->v_numoutput)
789 				panic("Clean vnode has pending I/O's");
790 			if (vp->v_writecount != 0)
791 				panic("Non-zero write count");
792 		}
793 #endif
794 		if ((pollinfo = vp->v_pollinfo) != NULL) {
795 			/*
796 			 * To avoid lock order reversals, the call to
797 			 * uma_zfree() must be delayed until the vnode
798 			 * interlock is released.
799 			 */
800 			vp->v_pollinfo = NULL;
801 		}
802 #ifdef MAC
803 		mac_destroy_vnode(vp);
804 #endif
805 		vp->v_iflag = 0;
806 		vp->v_vflag = 0;
807 		vp->v_lastw = 0;
808 		vp->v_lasta = 0;
809 		vp->v_cstart = 0;
810 		vp->v_clen = 0;
811 		vp->v_socket = 0;
812 		lockdestroy(vp->v_vnlock);
813 		lockinit(vp->v_vnlock, PVFS, tag, VLKTIMEOUT, LK_NOPAUSE);
814 		KASSERT(vp->v_cleanbufcnt == 0, ("cleanbufcnt not 0"));
815 		KASSERT(vp->v_cleanblkroot == NULL, ("cleanblkroot not NULL"));
816 		KASSERT(vp->v_dirtybufcnt == 0, ("dirtybufcnt not 0"));
817 		KASSERT(vp->v_dirtyblkroot == NULL, ("dirtyblkroot not NULL"));
818 	} else {
819 		numvnodes++;
820 		mtx_unlock(&vnode_free_list_mtx);
821 
822 		vp = (struct vnode *) uma_zalloc(vnode_zone, M_WAITOK|M_ZERO);
823 		mtx_init(&vp->v_interlock, "vnode interlock", NULL, MTX_DEF);
824 		VI_LOCK(vp);
825 		vp->v_dd = vp;
826 		vp->v_vnlock = &vp->v_lock;
827 		lockinit(vp->v_vnlock, PVFS, tag, VLKTIMEOUT, LK_NOPAUSE);
828 		cache_purge(vp);		/* Sets up v_id. */
829 		LIST_INIT(&vp->v_cache_src);
830 		TAILQ_INIT(&vp->v_cache_dst);
831 	}
832 
833 	TAILQ_INIT(&vp->v_cleanblkhd);
834 	TAILQ_INIT(&vp->v_dirtyblkhd);
835 	vp->v_type = VNON;
836 	vp->v_tag = tag;
837 	vp->v_op = vops;
838 	*vpp = vp;
839 	vp->v_usecount = 1;
840 	vp->v_data = 0;
841 	vp->v_cachedid = -1;
842 	VI_UNLOCK(vp);
843 	if (pollinfo != NULL) {
844 		mtx_destroy(&pollinfo->vpi_lock);
845 		uma_zfree(vnodepoll_zone, pollinfo);
846 	}
847 #ifdef MAC
848 	mac_init_vnode(vp);
849 	if (mp != NULL && (mp->mnt_flag & MNT_MULTILABEL) == 0)
850 		mac_associate_vnode_singlelabel(mp, vp);
851 #endif
852 	delmntque(vp);
853 	if (mp != NULL) {
854 		insmntque(vp, mp);
855 		vp->v_bsize = mp->mnt_stat.f_iosize;
856 	}
857 
858 	return (0);
859 }
860 
861 /*
862  * Delete from old mount point vnode list, if on one.
863  */
864 static void
865 delmntque(struct vnode *vp)
866 {
867 	struct mount *mp;
868 
869 	if (vp->v_mount == NULL)
870 		return;
871 	mp = vp->v_mount;
872 	MNT_ILOCK(mp);
873 	vp->v_mount = NULL;
874 	KASSERT(mp->mnt_nvnodelistsize > 0,
875 		("bad mount point vnode list size"));
876 	TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
877 	mp->mnt_nvnodelistsize--;
878 	MNT_IUNLOCK(mp);
879 }
880 
881 /*
882  * Insert into list of vnodes for the new mount point, if available.
883  */
884 static void
885 insmntque(struct vnode *vp, struct mount *mp)
886 {
887 
888 	vp->v_mount = mp;
889 	KASSERT(mp != NULL, ("Don't call insmntque(foo, NULL)"));
890 	MNT_ILOCK(vp->v_mount);
891 	TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
892 	mp->mnt_nvnodelistsize++;
893 	MNT_IUNLOCK(vp->v_mount);
894 }
895 
896 /*
897  * Update outstanding I/O count and do wakeup if requested.
898  */
899 void
900 vwakeup(bp)
901 	register struct buf *bp;
902 {
903 	register struct vnode *vp;
904 
905 	bp->b_flags &= ~B_WRITEINPROG;
906 	if ((vp = bp->b_vp)) {
907 		VI_LOCK(vp);
908 		vp->v_numoutput--;
909 		if (vp->v_numoutput < 0)
910 			panic("vwakeup: neg numoutput");
911 		if ((vp->v_numoutput == 0) && (vp->v_iflag & VI_BWAIT)) {
912 			vp->v_iflag &= ~VI_BWAIT;
913 			wakeup(&vp->v_numoutput);
914 		}
915 		VI_UNLOCK(vp);
916 	}
917 }
918 
919 /*
920  * Flush out and invalidate all buffers associated with a vnode.
921  * Called with the underlying object locked.
922  */
923 int
924 vinvalbuf(vp, flags, cred, td, slpflag, slptimeo)
925 	struct vnode *vp;
926 	int flags;
927 	struct ucred *cred;
928 	struct thread *td;
929 	int slpflag, slptimeo;
930 {
931 	struct buf *blist;
932 	int error;
933 	vm_object_t object;
934 
935 	GIANT_REQUIRED;
936 
937 	ASSERT_VOP_LOCKED(vp, "vinvalbuf");
938 
939 	VI_LOCK(vp);
940 	if (flags & V_SAVE) {
941 		while (vp->v_numoutput) {
942 			vp->v_iflag |= VI_BWAIT;
943 			error = msleep(&vp->v_numoutput, VI_MTX(vp),
944 			    slpflag | (PRIBIO + 1), "vinvlbuf", slptimeo);
945 			if (error) {
946 				VI_UNLOCK(vp);
947 				return (error);
948 			}
949 		}
950 		if (!TAILQ_EMPTY(&vp->v_dirtyblkhd)) {
951 			VI_UNLOCK(vp);
952 			if ((error = VOP_FSYNC(vp, cred, MNT_WAIT, td)) != 0)
953 				return (error);
954 			/*
955 			 * XXX We could save a lock/unlock if this was only
956 			 * enabled under INVARIANTS
957 			 */
958 			VI_LOCK(vp);
959 			if (vp->v_numoutput > 0 ||
960 			    !TAILQ_EMPTY(&vp->v_dirtyblkhd))
961 				panic("vinvalbuf: dirty bufs");
962 		}
963 	}
964 	/*
965 	 * If you alter this loop please notice that interlock is dropped and
966 	 * reacquired in flushbuflist.  Special care is needed to ensure that
967 	 * no race conditions occur from this.
968 	 */
969 	for (error = 0;;) {
970 		if ((blist = TAILQ_FIRST(&vp->v_cleanblkhd)) != 0 &&
971 		    flushbuflist(blist, flags, vp, slpflag, slptimeo, &error)) {
972 			if (error)
973 				break;
974 			continue;
975 		}
976 		if ((blist = TAILQ_FIRST(&vp->v_dirtyblkhd)) != 0 &&
977 		    flushbuflist(blist, flags, vp, slpflag, slptimeo, &error)) {
978 			if (error)
979 				break;
980 			continue;
981 		}
982 		break;
983 	}
984 	if (error) {
985 		VI_UNLOCK(vp);
986 		return (error);
987 	}
988 
989 	/*
990 	 * Wait for I/O to complete.  XXX needs cleaning up.  The vnode can
991 	 * have write I/O in-progress but if there is a VM object then the
992 	 * VM object can also have read-I/O in-progress.
993 	 */
994 	do {
995 		while (vp->v_numoutput > 0) {
996 			vp->v_iflag |= VI_BWAIT;
997 			msleep(&vp->v_numoutput, VI_MTX(vp), PVM, "vnvlbv", 0);
998 		}
999 		VI_UNLOCK(vp);
1000 		if (VOP_GETVOBJECT(vp, &object) == 0) {
1001 			VM_OBJECT_LOCK(object);
1002 			vm_object_pip_wait(object, "vnvlbx");
1003 			VM_OBJECT_UNLOCK(object);
1004 		}
1005 		VI_LOCK(vp);
1006 	} while (vp->v_numoutput > 0);
1007 	VI_UNLOCK(vp);
1008 
1009 	/*
1010 	 * Destroy the copy in the VM cache, too.
1011 	 */
1012 	if (VOP_GETVOBJECT(vp, &object) == 0) {
1013 		VM_OBJECT_LOCK(object);
1014 		vm_object_page_remove(object, 0, 0,
1015 			(flags & V_SAVE) ? TRUE : FALSE);
1016 		VM_OBJECT_UNLOCK(object);
1017 	}
1018 
1019 #ifdef INVARIANTS
1020 	VI_LOCK(vp);
1021 	if ((flags & (V_ALT | V_NORMAL)) == 0 &&
1022 	    (!TAILQ_EMPTY(&vp->v_dirtyblkhd) ||
1023 	     !TAILQ_EMPTY(&vp->v_cleanblkhd)))
1024 		panic("vinvalbuf: flush failed");
1025 	VI_UNLOCK(vp);
1026 #endif
1027 	return (0);
1028 }
1029 
1030 /*
1031  * Flush out buffers on the specified list.
1032  *
1033  */
1034 static int
1035 flushbuflist(blist, flags, vp, slpflag, slptimeo, errorp)
1036 	struct buf *blist;
1037 	int flags;
1038 	struct vnode *vp;
1039 	int slpflag, slptimeo;
1040 	int *errorp;
1041 {
1042 	struct buf *bp, *nbp;
1043 	int found, error;
1044 
1045 	ASSERT_VI_LOCKED(vp, "flushbuflist");
1046 
1047 	for (found = 0, bp = blist; bp; bp = nbp) {
1048 		nbp = TAILQ_NEXT(bp, b_vnbufs);
1049 		if (((flags & V_NORMAL) && (bp->b_xflags & BX_ALTDATA)) ||
1050 		    ((flags & V_ALT) && (bp->b_xflags & BX_ALTDATA) == 0)) {
1051 			continue;
1052 		}
1053 		found += 1;
1054 		error = BUF_TIMELOCK(bp,
1055 		    LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, VI_MTX(vp),
1056 		    "flushbuf", slpflag, slptimeo);
1057 		if (error) {
1058 			if (error != ENOLCK)
1059 				*errorp = error;
1060 			goto done;
1061 		}
1062 		/*
1063 		 * XXX Since there are no node locks for NFS, I
1064 		 * believe there is a slight chance that a delayed
1065 		 * write will occur while sleeping just above, so
1066 		 * check for it.  Note that vfs_bio_awrite expects
1067 		 * buffers to reside on a queue, while bwrite and
1068 		 * brelse do not.
1069 		 */
1070 		if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
1071 			(flags & V_SAVE)) {
1072 
1073 			if (bp->b_vp == vp) {
1074 				if (bp->b_flags & B_CLUSTEROK) {
1075 					vfs_bio_awrite(bp);
1076 				} else {
1077 					bremfree(bp);
1078 					bp->b_flags |= B_ASYNC;
1079 					bwrite(bp);
1080 				}
1081 			} else {
1082 				bremfree(bp);
1083 				(void) bwrite(bp);
1084 			}
1085 			goto done;
1086 		}
1087 		bremfree(bp);
1088 		bp->b_flags |= (B_INVAL | B_NOCACHE | B_RELBUF);
1089 		bp->b_flags &= ~B_ASYNC;
1090 		brelse(bp);
1091 		VI_LOCK(vp);
1092 	}
1093 	return (found);
1094 done:
1095 	VI_LOCK(vp);
1096 	return (found);
1097 }
1098 
1099 /*
1100  * Truncate a file's buffer and pages to a specified length.  This
1101  * is in lieu of the old vinvalbuf mechanism, which performed unneeded
1102  * sync activity.
1103  */
1104 int
1105 vtruncbuf(vp, cred, td, length, blksize)
1106 	register struct vnode *vp;
1107 	struct ucred *cred;
1108 	struct thread *td;
1109 	off_t length;
1110 	int blksize;
1111 {
1112 	register struct buf *bp;
1113 	struct buf *nbp;
1114 	int anyfreed;
1115 	int trunclbn;
1116 
1117 	/*
1118 	 * Round up to the *next* lbn.
1119 	 */
1120 	trunclbn = (length + blksize - 1) / blksize;
1121 
1122 	ASSERT_VOP_LOCKED(vp, "vtruncbuf");
1123 restart:
1124 	VI_LOCK(vp);
1125 	anyfreed = 1;
1126 	for (;anyfreed;) {
1127 		anyfreed = 0;
1128 		for (bp = TAILQ_FIRST(&vp->v_cleanblkhd); bp; bp = nbp) {
1129 			nbp = TAILQ_NEXT(bp, b_vnbufs);
1130 			if (bp->b_lblkno >= trunclbn) {
1131 				if (BUF_LOCK(bp,
1132 				    LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1133 				    VI_MTX(vp)) == ENOLCK)
1134 					goto restart;
1135 
1136 				bremfree(bp);
1137 				bp->b_flags |= (B_INVAL | B_RELBUF);
1138 				bp->b_flags &= ~B_ASYNC;
1139 				brelse(bp);
1140 				anyfreed = 1;
1141 
1142 				if (nbp &&
1143 				    (((nbp->b_xflags & BX_VNCLEAN) == 0) ||
1144 				    (nbp->b_vp != vp) ||
1145 				    (nbp->b_flags & B_DELWRI))) {
1146 					goto restart;
1147 				}
1148 				VI_LOCK(vp);
1149 			}
1150 		}
1151 
1152 		for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
1153 			nbp = TAILQ_NEXT(bp, b_vnbufs);
1154 			if (bp->b_lblkno >= trunclbn) {
1155 				if (BUF_LOCK(bp,
1156 				    LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1157 				    VI_MTX(vp)) == ENOLCK)
1158 					goto restart;
1159 				bremfree(bp);
1160 				bp->b_flags |= (B_INVAL | B_RELBUF);
1161 				bp->b_flags &= ~B_ASYNC;
1162 				brelse(bp);
1163 				anyfreed = 1;
1164 				if (nbp &&
1165 				    (((nbp->b_xflags & BX_VNDIRTY) == 0) ||
1166 				    (nbp->b_vp != vp) ||
1167 				    (nbp->b_flags & B_DELWRI) == 0)) {
1168 					goto restart;
1169 				}
1170 				VI_LOCK(vp);
1171 			}
1172 		}
1173 	}
1174 
1175 	if (length > 0) {
1176 restartsync:
1177 		for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
1178 			nbp = TAILQ_NEXT(bp, b_vnbufs);
1179 			if (bp->b_lblkno > 0)
1180 				continue;
1181 			/*
1182 			 * Since we hold the vnode lock this should only
1183 			 * fail if we're racing with the buf daemon.
1184 			 */
1185 			if (BUF_LOCK(bp,
1186 			    LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1187 			    VI_MTX(vp)) == ENOLCK) {
1188 				goto restart;
1189 			}
1190 			KASSERT((bp->b_flags & B_DELWRI),
1191 			    ("buf(%p) on dirty queue without DELWRI", bp));
1192 
1193 			bremfree(bp);
1194 			bawrite(bp);
1195 			VI_LOCK(vp);
1196 			goto restartsync;
1197 		}
1198 	}
1199 
1200 	while (vp->v_numoutput > 0) {
1201 		vp->v_iflag |= VI_BWAIT;
1202 		msleep(&vp->v_numoutput, VI_MTX(vp), PVM, "vbtrunc", 0);
1203 	}
1204 	VI_UNLOCK(vp);
1205 	vnode_pager_setsize(vp, length);
1206 
1207 	return (0);
1208 }
1209 
1210 /*
1211  * buf_splay() - splay tree core for the clean/dirty list of buffers in
1212  * 		 a vnode.
1213  *
1214  *	NOTE: We have to deal with the special case of a background bitmap
1215  *	buffer, a situation where two buffers will have the same logical
1216  *	block offset.  We want (1) only the foreground buffer to be accessed
1217  *	in a lookup and (2) must differentiate between the foreground and
1218  *	background buffer in the splay tree algorithm because the splay
1219  *	tree cannot normally handle multiple entities with the same 'index'.
1220  *	We accomplish this by adding differentiating flags to the splay tree's
1221  *	numerical domain.
1222  */
1223 static
1224 struct buf *
1225 buf_splay(daddr_t lblkno, b_xflags_t xflags, struct buf *root)
1226 {
1227 	struct buf dummy;
1228 	struct buf *lefttreemax, *righttreemin, *y;
1229 
1230 	if (root == NULL)
1231 		return (NULL);
1232 	lefttreemax = righttreemin = &dummy;
1233 	for (;;) {
1234 		if (lblkno < root->b_lblkno ||
1235 		    (lblkno == root->b_lblkno &&
1236 		    (xflags & BX_BKGRDMARKER) < (root->b_xflags & BX_BKGRDMARKER))) {
1237 			if ((y = root->b_left) == NULL)
1238 				break;
1239 			if (lblkno < y->b_lblkno) {
1240 				/* Rotate right. */
1241 				root->b_left = y->b_right;
1242 				y->b_right = root;
1243 				root = y;
1244 				if ((y = root->b_left) == NULL)
1245 					break;
1246 			}
1247 			/* Link into the new root's right tree. */
1248 			righttreemin->b_left = root;
1249 			righttreemin = root;
1250 		} else if (lblkno > root->b_lblkno ||
1251 		    (lblkno == root->b_lblkno &&
1252 		    (xflags & BX_BKGRDMARKER) > (root->b_xflags & BX_BKGRDMARKER))) {
1253 			if ((y = root->b_right) == NULL)
1254 				break;
1255 			if (lblkno > y->b_lblkno) {
1256 				/* Rotate left. */
1257 				root->b_right = y->b_left;
1258 				y->b_left = root;
1259 				root = y;
1260 				if ((y = root->b_right) == NULL)
1261 					break;
1262 			}
1263 			/* Link into the new root's left tree. */
1264 			lefttreemax->b_right = root;
1265 			lefttreemax = root;
1266 		} else {
1267 			break;
1268 		}
1269 		root = y;
1270 	}
1271 	/* Assemble the new root. */
1272 	lefttreemax->b_right = root->b_left;
1273 	righttreemin->b_left = root->b_right;
1274 	root->b_left = dummy.b_right;
1275 	root->b_right = dummy.b_left;
1276 	return (root);
1277 }
1278 
1279 static
1280 void
1281 buf_vlist_remove(struct buf *bp)
1282 {
1283 	struct vnode *vp = bp->b_vp;
1284 	struct buf *root;
1285 
1286 	ASSERT_VI_LOCKED(vp, "buf_vlist_remove");
1287 	if (bp->b_xflags & BX_VNDIRTY) {
1288 		if (bp != vp->v_dirtyblkroot) {
1289 			root = buf_splay(bp->b_lblkno, bp->b_xflags,
1290 			    vp->v_dirtyblkroot);
1291 			KASSERT(root == bp,
1292 			    ("splay lookup failed during dirty remove"));
1293 		}
1294 		if (bp->b_left == NULL) {
1295 			root = bp->b_right;
1296 		} else {
1297 			root = buf_splay(bp->b_lblkno, bp->b_xflags,
1298 			    bp->b_left);
1299 			root->b_right = bp->b_right;
1300 		}
1301 		vp->v_dirtyblkroot = root;
1302 		TAILQ_REMOVE(&vp->v_dirtyblkhd, bp, b_vnbufs);
1303 		vp->v_dirtybufcnt--;
1304 	} else {
1305 		/* KASSERT(bp->b_xflags & BX_VNCLEAN, ("bp wasn't clean")); */
1306 		if (bp != vp->v_cleanblkroot) {
1307 			root = buf_splay(bp->b_lblkno, bp->b_xflags,
1308 			    vp->v_cleanblkroot);
1309 			KASSERT(root == bp,
1310 			    ("splay lookup failed during clean remove"));
1311 		}
1312 		if (bp->b_left == NULL) {
1313 			root = bp->b_right;
1314 		} else {
1315 			root = buf_splay(bp->b_lblkno, bp->b_xflags,
1316 			    bp->b_left);
1317 			root->b_right = bp->b_right;
1318 		}
1319 		vp->v_cleanblkroot = root;
1320 		TAILQ_REMOVE(&vp->v_cleanblkhd, bp, b_vnbufs);
1321 		vp->v_cleanbufcnt--;
1322 	}
1323 	bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN);
1324 }
1325 
1326 /*
1327  * Add the buffer to the sorted clean or dirty block list using a
1328  * splay tree algorithm.
1329  *
1330  * NOTE: xflags is passed as a constant, optimizing this inline function!
1331  */
1332 static
1333 void
1334 buf_vlist_add(struct buf *bp, struct vnode *vp, b_xflags_t xflags)
1335 {
1336 	struct buf *root;
1337 
1338 	ASSERT_VI_LOCKED(vp, "buf_vlist_add");
1339 	bp->b_xflags |= xflags;
1340 	if (xflags & BX_VNDIRTY) {
1341 		root = buf_splay(bp->b_lblkno, bp->b_xflags, vp->v_dirtyblkroot);
1342 		if (root == NULL) {
1343 			bp->b_left = NULL;
1344 			bp->b_right = NULL;
1345 			TAILQ_INSERT_TAIL(&vp->v_dirtyblkhd, bp, b_vnbufs);
1346 		} else if (bp->b_lblkno < root->b_lblkno ||
1347 		    (bp->b_lblkno == root->b_lblkno &&
1348 		    (bp->b_xflags & BX_BKGRDMARKER) < (root->b_xflags & BX_BKGRDMARKER))) {
1349 			bp->b_left = root->b_left;
1350 			bp->b_right = root;
1351 			root->b_left = NULL;
1352 			TAILQ_INSERT_BEFORE(root, bp, b_vnbufs);
1353 		} else {
1354 			bp->b_right = root->b_right;
1355 			bp->b_left = root;
1356 			root->b_right = NULL;
1357 			TAILQ_INSERT_AFTER(&vp->v_dirtyblkhd,
1358 			    root, bp, b_vnbufs);
1359 		}
1360 		vp->v_dirtybufcnt++;
1361 		vp->v_dirtyblkroot = bp;
1362 	} else {
1363 		/* KASSERT(xflags & BX_VNCLEAN, ("xflags not clean")); */
1364 		root = buf_splay(bp->b_lblkno, bp->b_xflags, vp->v_cleanblkroot);
1365 		if (root == NULL) {
1366 			bp->b_left = NULL;
1367 			bp->b_right = NULL;
1368 			TAILQ_INSERT_TAIL(&vp->v_cleanblkhd, bp, b_vnbufs);
1369 		} else if (bp->b_lblkno < root->b_lblkno ||
1370 		    (bp->b_lblkno == root->b_lblkno &&
1371 		    (bp->b_xflags & BX_BKGRDMARKER) < (root->b_xflags & BX_BKGRDMARKER))) {
1372 			bp->b_left = root->b_left;
1373 			bp->b_right = root;
1374 			root->b_left = NULL;
1375 			TAILQ_INSERT_BEFORE(root, bp, b_vnbufs);
1376 		} else {
1377 			bp->b_right = root->b_right;
1378 			bp->b_left = root;
1379 			root->b_right = NULL;
1380 			TAILQ_INSERT_AFTER(&vp->v_cleanblkhd,
1381 			    root, bp, b_vnbufs);
1382 		}
1383 		vp->v_cleanbufcnt++;
1384 		vp->v_cleanblkroot = bp;
1385 	}
1386 }
1387 
1388 /*
1389  * Lookup a buffer using the splay tree.  Note that we specifically avoid
1390  * shadow buffers used in background bitmap writes.
1391  *
1392  * This code isn't quite efficient as it could be because we are maintaining
1393  * two sorted lists and do not know which list the block resides in.
1394  *
1395  * During a "make buildworld" the desired buffer is found at one of
1396  * the roots more than 60% of the time.  Thus, checking both roots
1397  * before performing either splay eliminates unnecessary splays on the
1398  * first tree splayed.
1399  */
1400 struct buf *
1401 gbincore(struct vnode *vp, daddr_t lblkno)
1402 {
1403 	struct buf *bp;
1404 
1405 	GIANT_REQUIRED;
1406 
1407 	ASSERT_VI_LOCKED(vp, "gbincore");
1408 	if ((bp = vp->v_cleanblkroot) != NULL &&
1409 	    bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
1410 		return (bp);
1411 	if ((bp = vp->v_dirtyblkroot) != NULL &&
1412 	    bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
1413 		return (bp);
1414 	if ((bp = vp->v_cleanblkroot) != NULL) {
1415 		vp->v_cleanblkroot = bp = buf_splay(lblkno, 0, bp);
1416 		if (bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
1417 			return (bp);
1418 	}
1419 	if ((bp = vp->v_dirtyblkroot) != NULL) {
1420 		vp->v_dirtyblkroot = bp = buf_splay(lblkno, 0, bp);
1421 		if (bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
1422 			return (bp);
1423 	}
1424 	return (NULL);
1425 }
1426 
1427 /*
1428  * Associate a buffer with a vnode.
1429  */
1430 void
1431 bgetvp(vp, bp)
1432 	register struct vnode *vp;
1433 	register struct buf *bp;
1434 {
1435 
1436 	KASSERT(bp->b_vp == NULL, ("bgetvp: not free"));
1437 
1438 	KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0,
1439 	    ("bgetvp: bp already attached! %p", bp));
1440 
1441 	ASSERT_VI_LOCKED(vp, "bgetvp");
1442 	vholdl(vp);
1443 	bp->b_vp = vp;
1444 	bp->b_dev = vn_todev(vp);
1445 	/*
1446 	 * Insert onto list for new vnode.
1447 	 */
1448 	buf_vlist_add(bp, vp, BX_VNCLEAN);
1449 }
1450 
1451 /*
1452  * Disassociate a buffer from a vnode.
1453  */
1454 void
1455 brelvp(bp)
1456 	register struct buf *bp;
1457 {
1458 	struct vnode *vp;
1459 
1460 	KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
1461 
1462 	/*
1463 	 * Delete from old vnode list, if on one.
1464 	 */
1465 	vp = bp->b_vp;
1466 	VI_LOCK(vp);
1467 	if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
1468 		buf_vlist_remove(bp);
1469 	if ((vp->v_iflag & VI_ONWORKLST) && TAILQ_EMPTY(&vp->v_dirtyblkhd)) {
1470 		vp->v_iflag &= ~VI_ONWORKLST;
1471 		mtx_lock(&sync_mtx);
1472 		LIST_REMOVE(vp, v_synclist);
1473  		syncer_worklist_len--;
1474 		mtx_unlock(&sync_mtx);
1475 	}
1476 	vdropl(vp);
1477 	bp->b_vp = (struct vnode *) 0;
1478 	if (bp->b_object)
1479 		bp->b_object = NULL;
1480 	VI_UNLOCK(vp);
1481 }
1482 
1483 /*
1484  * Add an item to the syncer work queue.
1485  */
1486 static void
1487 vn_syncer_add_to_worklist(struct vnode *vp, int delay)
1488 {
1489 	int slot;
1490 
1491 	ASSERT_VI_LOCKED(vp, "vn_syncer_add_to_worklist");
1492 
1493 	mtx_lock(&sync_mtx);
1494 	if (vp->v_iflag & VI_ONWORKLST)
1495 		LIST_REMOVE(vp, v_synclist);
1496 	else {
1497 		vp->v_iflag |= VI_ONWORKLST;
1498  		syncer_worklist_len++;
1499 	}
1500 
1501 	if (delay > syncer_maxdelay - 2)
1502 		delay = syncer_maxdelay - 2;
1503 	slot = (syncer_delayno + delay) & syncer_mask;
1504 
1505 	LIST_INSERT_HEAD(&syncer_workitem_pending[slot], vp, v_synclist);
1506 	mtx_unlock(&sync_mtx);
1507 }
1508 
1509 static int
1510 sysctl_vfs_worklist_len(SYSCTL_HANDLER_ARGS)
1511 {
1512 	int error, len;
1513 
1514 	mtx_lock(&sync_mtx);
1515 	len = syncer_worklist_len - sync_vnode_count;
1516 	mtx_unlock(&sync_mtx);
1517 	error = SYSCTL_OUT(req, &len, sizeof(len));
1518 	return (error);
1519 }
1520 
1521 SYSCTL_PROC(_vfs, OID_AUTO, worklist_len, CTLTYPE_INT | CTLFLAG_RD, NULL, 0,
1522     sysctl_vfs_worklist_len, "I", "Syncer thread worklist length");
1523 
1524 struct  proc *updateproc;
1525 static void sched_sync(void);
1526 static struct kproc_desc up_kp = {
1527 	"syncer",
1528 	sched_sync,
1529 	&updateproc
1530 };
1531 SYSINIT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp)
1532 
1533 /*
1534  * System filesystem synchronizer daemon.
1535  */
1536 static void
1537 sched_sync(void)
1538 {
1539 	struct synclist *next;
1540 	struct synclist *slp;
1541 	struct vnode *vp;
1542 	struct mount *mp;
1543 	long starttime;
1544 	struct thread *td = FIRST_THREAD_IN_PROC(updateproc);
1545 	static int dummychan;
1546 	int last_work_seen;
1547 	int net_worklist_len;
1548 	int syncer_final_iter;
1549 	int first_printf;
1550 
1551 	mtx_lock(&Giant);
1552 	last_work_seen = 0;
1553 	syncer_final_iter = 0;
1554 	first_printf = 1;
1555 	syncer_state = SYNCER_RUNNING;
1556 	starttime = time_second;
1557 
1558 	EVENTHANDLER_REGISTER(shutdown_pre_sync, syncer_shutdown, td->td_proc,
1559 	    SHUTDOWN_PRI_LAST);
1560 
1561 	for (;;) {
1562 		mtx_lock(&sync_mtx);
1563 		if (syncer_state == SYNCER_FINAL_DELAY &&
1564 		    syncer_final_iter == 0) {
1565 			mtx_unlock(&sync_mtx);
1566 			printf("done.\n");
1567 			kthread_suspend_check(td->td_proc);
1568 			mtx_lock(&sync_mtx);
1569 		}
1570 		net_worklist_len = syncer_worklist_len - sync_vnode_count;
1571 		if (syncer_state != SYNCER_RUNNING &&
1572 		    starttime != time_second) {
1573 			if (first_printf) {
1574 				printf("Syncer syncing, vnodes remaining... ");
1575 				first_printf = 0;
1576 			}
1577 			printf("%d ", net_worklist_len);
1578 		}
1579 		starttime = time_second;
1580 
1581 		/*
1582 		 * Push files whose dirty time has expired.  Be careful
1583 		 * of interrupt race on slp queue.
1584 		 *
1585 		 * Skip over empty worklist slots when shutting down.
1586 		 */
1587 		do {
1588 			slp = &syncer_workitem_pending[syncer_delayno];
1589 			syncer_delayno += 1;
1590 			if (syncer_delayno == syncer_maxdelay)
1591 				syncer_delayno = 0;
1592 			next = &syncer_workitem_pending[syncer_delayno];
1593 			/*
1594 			 * If the worklist has wrapped since the
1595 			 * it was emptied of all but syncer vnodes,
1596 			 * switch to the FINAL_DELAY state and run
1597 			 * for one more second.
1598 			 */
1599 			if (syncer_state == SYNCER_SHUTTING_DOWN &&
1600 			    net_worklist_len == 0 &&
1601 			    last_work_seen == syncer_delayno) {
1602 				syncer_state = SYNCER_FINAL_DELAY;
1603 				syncer_final_iter = SYNCER_SHUTDOWN_SPEEDUP;
1604 			}
1605 		} while (syncer_state != SYNCER_RUNNING && LIST_EMPTY(slp) &&
1606 		    syncer_worklist_len > 0);
1607 
1608 		/*
1609 		 * Keep track of the last time there was anything
1610 		 * on the worklist other than syncer vnodes.
1611 		 * Return to the SHUTTING_DOWN state if any
1612 		 * new work appears.
1613 		 */
1614 		if (net_worklist_len > 0 || syncer_state == SYNCER_RUNNING)
1615 			last_work_seen = syncer_delayno;
1616 		if (net_worklist_len > 0 && syncer_state == SYNCER_FINAL_DELAY)
1617 			syncer_state = SYNCER_SHUTTING_DOWN;
1618 		while ((vp = LIST_FIRST(slp)) != NULL) {
1619 			if (VOP_ISLOCKED(vp, NULL) != 0 ||
1620 			    vn_start_write(vp, &mp, V_NOWAIT) != 0) {
1621 				LIST_REMOVE(vp, v_synclist);
1622 				LIST_INSERT_HEAD(next, vp, v_synclist);
1623 				continue;
1624 			}
1625 			if (VI_TRYLOCK(vp) == 0) {
1626 				LIST_REMOVE(vp, v_synclist);
1627 				LIST_INSERT_HEAD(next, vp, v_synclist);
1628 				vn_finished_write(mp);
1629 				continue;
1630 			}
1631 			/*
1632 			 * We use vhold in case the vnode does not
1633 			 * successfully sync.  vhold prevents the vnode from
1634 			 * going away when we unlock the sync_mtx so that
1635 			 * we can acquire the vnode interlock.
1636 			 */
1637 			vholdl(vp);
1638 			mtx_unlock(&sync_mtx);
1639 			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_INTERLOCK, td);
1640 			(void) VOP_FSYNC(vp, td->td_ucred, MNT_LAZY, td);
1641 			VOP_UNLOCK(vp, 0, td);
1642 			vn_finished_write(mp);
1643 			VI_LOCK(vp);
1644 			if ((vp->v_iflag & VI_ONWORKLST) != 0) {
1645 				/*
1646 				 * Put us back on the worklist.  The worklist
1647 				 * routine will remove us from our current
1648 				 * position and then add us back in at a later
1649 				 * position.
1650 				 */
1651 				vn_syncer_add_to_worklist(vp, syncdelay);
1652 			}
1653 			vdropl(vp);
1654 			VI_UNLOCK(vp);
1655 			mtx_lock(&sync_mtx);
1656 		}
1657 		if (syncer_state == SYNCER_FINAL_DELAY && syncer_final_iter > 0)
1658 			syncer_final_iter--;
1659 		mtx_unlock(&sync_mtx);
1660 
1661 		/*
1662 		 * Do soft update processing.
1663 		 */
1664 		if (softdep_process_worklist_hook != NULL)
1665 			(*softdep_process_worklist_hook)(NULL);
1666 
1667 		/*
1668 		 * The variable rushjob allows the kernel to speed up the
1669 		 * processing of the filesystem syncer process. A rushjob
1670 		 * value of N tells the filesystem syncer to process the next
1671 		 * N seconds worth of work on its queue ASAP. Currently rushjob
1672 		 * is used by the soft update code to speed up the filesystem
1673 		 * syncer process when the incore state is getting so far
1674 		 * ahead of the disk that the kernel memory pool is being
1675 		 * threatened with exhaustion.
1676 		 */
1677 		mtx_lock(&sync_mtx);
1678 		if (rushjob > 0) {
1679 			rushjob -= 1;
1680 			mtx_unlock(&sync_mtx);
1681 			continue;
1682 		}
1683 		mtx_unlock(&sync_mtx);
1684 		/*
1685 		 * Just sleep for a short period if time between
1686 		 * iterations when shutting down to allow some I/O
1687 		 * to happen.
1688 		 *
1689 		 * If it has taken us less than a second to process the
1690 		 * current work, then wait. Otherwise start right over
1691 		 * again. We can still lose time if any single round
1692 		 * takes more than two seconds, but it does not really
1693 		 * matter as we are just trying to generally pace the
1694 		 * filesystem activity.
1695 		 */
1696 		if (syncer_state != SYNCER_RUNNING)
1697 			tsleep(&dummychan, PPAUSE, "syncfnl",
1698 			    hz / SYNCER_SHUTDOWN_SPEEDUP);
1699 		else if (time_second == starttime)
1700 			tsleep(&lbolt, PPAUSE, "syncer", 0);
1701 	}
1702 }
1703 
1704 /*
1705  * Request the syncer daemon to speed up its work.
1706  * We never push it to speed up more than half of its
1707  * normal turn time, otherwise it could take over the cpu.
1708  */
1709 int
1710 speedup_syncer()
1711 {
1712 	struct thread *td;
1713 	int ret = 0;
1714 
1715 	td = FIRST_THREAD_IN_PROC(updateproc);
1716 	sleepq_remove(td, &lbolt);
1717 	mtx_lock(&sync_mtx);
1718 	if (rushjob < syncdelay / 2) {
1719 		rushjob += 1;
1720 		stat_rush_requests += 1;
1721 		ret = 1;
1722 	}
1723 	mtx_unlock(&sync_mtx);
1724 	return (ret);
1725 }
1726 
1727 /*
1728  * Tell the syncer to speed up its work and run though its work
1729  * list several times, then tell it to shut down.
1730  */
1731 static void
1732 syncer_shutdown(void *arg, int howto)
1733 {
1734 	struct thread *td;
1735 
1736 	td = FIRST_THREAD_IN_PROC(updateproc);
1737 	sleepq_remove(td, &lbolt);
1738 	mtx_lock(&sync_mtx);
1739 	syncer_state = SYNCER_SHUTTING_DOWN;
1740 	rushjob = 0;
1741 	mtx_unlock(&sync_mtx);
1742 	kproc_shutdown(arg, howto);
1743 }
1744 
1745 /*
1746  * Associate a p-buffer with a vnode.
1747  *
1748  * Also sets B_PAGING flag to indicate that vnode is not fully associated
1749  * with the buffer.  i.e. the bp has not been linked into the vnode or
1750  * ref-counted.
1751  */
1752 void
1753 pbgetvp(vp, bp)
1754 	register struct vnode *vp;
1755 	register struct buf *bp;
1756 {
1757 
1758 	KASSERT(bp->b_vp == NULL, ("pbgetvp: not free"));
1759 
1760 	bp->b_vp = vp;
1761 	bp->b_object = vp->v_object;
1762 	bp->b_flags |= B_PAGING;
1763 	bp->b_dev = vn_todev(vp);
1764 }
1765 
1766 /*
1767  * Disassociate a p-buffer from a vnode.
1768  */
1769 void
1770 pbrelvp(bp)
1771 	register struct buf *bp;
1772 {
1773 
1774 	KASSERT(bp->b_vp != NULL, ("pbrelvp: NULL"));
1775 
1776 	/* XXX REMOVE ME */
1777 	VI_LOCK(bp->b_vp);
1778 	if (TAILQ_NEXT(bp, b_vnbufs) != NULL) {
1779 		panic(
1780 		    "relpbuf(): b_vp was probably reassignbuf()d %p %x",
1781 		    bp,
1782 		    (int)bp->b_flags
1783 		);
1784 	}
1785 	VI_UNLOCK(bp->b_vp);
1786 	bp->b_vp = (struct vnode *) 0;
1787 	bp->b_object = NULL;
1788 	bp->b_flags &= ~B_PAGING;
1789 }
1790 
1791 /*
1792  * Reassign a buffer from one vnode to another.
1793  * Used to assign file specific control information
1794  * (indirect blocks) to the vnode to which they belong.
1795  */
1796 void
1797 reassignbuf(bp, newvp)
1798 	register struct buf *bp;
1799 	register struct vnode *newvp;
1800 {
1801 	struct vnode *vp;
1802 	int delay;
1803 
1804 	if (newvp == NULL) {
1805 		printf("reassignbuf: NULL");
1806 		return;
1807 	}
1808 	vp = bp->b_vp;
1809 	++reassignbufcalls;
1810 
1811 	/*
1812 	 * B_PAGING flagged buffers cannot be reassigned because their vp
1813 	 * is not fully linked in.
1814 	 */
1815 	if (bp->b_flags & B_PAGING)
1816 		panic("cannot reassign paging buffer");
1817 
1818 	/*
1819 	 * Delete from old vnode list, if on one.
1820 	 */
1821 	VI_LOCK(vp);
1822 	if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) {
1823 		buf_vlist_remove(bp);
1824 		if (vp != newvp) {
1825 			vdropl(bp->b_vp);
1826 			bp->b_vp = NULL;	/* for clarification */
1827 		}
1828 	}
1829 	if (vp != newvp) {
1830 		VI_UNLOCK(vp);
1831 		VI_LOCK(newvp);
1832 	}
1833 	/*
1834 	 * If dirty, put on list of dirty buffers; otherwise insert onto list
1835 	 * of clean buffers.
1836 	 */
1837 	if (bp->b_flags & B_DELWRI) {
1838 		if ((newvp->v_iflag & VI_ONWORKLST) == 0) {
1839 			switch (newvp->v_type) {
1840 			case VDIR:
1841 				delay = dirdelay;
1842 				break;
1843 			case VCHR:
1844 				delay = metadelay;
1845 				break;
1846 			default:
1847 				delay = filedelay;
1848 			}
1849 			vn_syncer_add_to_worklist(newvp, delay);
1850 		}
1851 		buf_vlist_add(bp, newvp, BX_VNDIRTY);
1852 	} else {
1853 		buf_vlist_add(bp, newvp, BX_VNCLEAN);
1854 
1855 		if ((newvp->v_iflag & VI_ONWORKLST) &&
1856 		    TAILQ_EMPTY(&newvp->v_dirtyblkhd)) {
1857 			mtx_lock(&sync_mtx);
1858 			LIST_REMOVE(newvp, v_synclist);
1859  			syncer_worklist_len--;
1860 			mtx_unlock(&sync_mtx);
1861 			newvp->v_iflag &= ~VI_ONWORKLST;
1862 		}
1863 	}
1864 	if (bp->b_vp != newvp) {
1865 		bp->b_vp = newvp;
1866 		vholdl(bp->b_vp);
1867 	}
1868 	VI_UNLOCK(newvp);
1869 }
1870 
1871 /*
1872  * Create a vnode for a device.
1873  * Used for mounting the root filesystem.
1874  */
1875 int
1876 bdevvp(dev, vpp)
1877 	struct cdev *dev;
1878 	struct vnode **vpp;
1879 {
1880 	register struct vnode *vp;
1881 	struct vnode *nvp;
1882 	int error;
1883 
1884 	if (dev == NULL) {
1885 		*vpp = NULLVP;
1886 		return (ENXIO);
1887 	}
1888 	if (vfinddev(dev, vpp))
1889 		return (0);
1890 
1891 	error = getnewvnode("none", (struct mount *)0, spec_vnodeop_p, &nvp);
1892 	if (error) {
1893 		*vpp = NULLVP;
1894 		return (error);
1895 	}
1896 	vp = nvp;
1897 	vp->v_type = VCHR;
1898 	vp->v_bsize = DEV_BSIZE;
1899 	addalias(vp, dev);
1900 	*vpp = vp;
1901 	return (0);
1902 }
1903 
1904 static void
1905 v_incr_usecount(struct vnode *vp, int delta)
1906 {
1907 
1908 	vp->v_usecount += delta;
1909 	if (vp->v_type == VCHR && vp->v_rdev != NULL) {
1910 		mtx_lock(&spechash_mtx);
1911 		vp->v_rdev->si_usecount += delta;
1912 		mtx_unlock(&spechash_mtx);
1913 	}
1914 }
1915 
1916 /*
1917  * Add vnode to the alias list hung off the struct cdev *.
1918  *
1919  * The reason for this gunk is that multiple vnodes can reference
1920  * the same physical device, so checking vp->v_usecount to see
1921  * how many users there are is inadequate; the v_usecount for
1922  * the vnodes need to be accumulated.  vcount() does that.
1923  */
1924 struct vnode *
1925 addaliasu(nvp, nvp_rdev)
1926 	struct vnode *nvp;
1927 	dev_t nvp_rdev;
1928 {
1929 	struct vnode *ovp;
1930 	vop_t **ops;
1931 	struct cdev *dev;
1932 
1933 	if (nvp->v_type == VBLK)
1934 		return (nvp);
1935 	if (nvp->v_type != VCHR)
1936 		panic("addaliasu on non-special vnode");
1937 	dev = findcdev(nvp_rdev);
1938 	if (dev == NULL)
1939 		return (nvp);
1940 	/*
1941 	 * Check to see if we have a bdevvp vnode with no associated
1942 	 * filesystem. If so, we want to associate the filesystem of
1943 	 * the new newly instigated vnode with the bdevvp vnode and
1944 	 * discard the newly created vnode rather than leaving the
1945 	 * bdevvp vnode lying around with no associated filesystem.
1946 	 */
1947 	if (vfinddev(dev, &ovp) == 0 || ovp->v_data != NULL) {
1948 		addalias(nvp, dev);
1949 		return (nvp);
1950 	}
1951 	/*
1952 	 * Discard unneeded vnode, but save its node specific data.
1953 	 * Note that if there is a lock, it is carried over in the
1954 	 * node specific data to the replacement vnode.
1955 	 */
1956 	vref(ovp);
1957 	ovp->v_data = nvp->v_data;
1958 	ovp->v_tag = nvp->v_tag;
1959 	nvp->v_data = NULL;
1960 	lockdestroy(ovp->v_vnlock);
1961 	lockinit(ovp->v_vnlock, PVFS, nvp->v_vnlock->lk_wmesg,
1962 	    nvp->v_vnlock->lk_timo, nvp->v_vnlock->lk_flags & LK_EXTFLG_MASK);
1963 	ops = ovp->v_op;
1964 	ovp->v_op = nvp->v_op;
1965 	if (VOP_ISLOCKED(nvp, curthread)) {
1966 		VOP_UNLOCK(nvp, 0, curthread);
1967 		vn_lock(ovp, LK_EXCLUSIVE | LK_RETRY, curthread);
1968 	}
1969 	nvp->v_op = ops;
1970 	delmntque(ovp);
1971 	insmntque(ovp, nvp->v_mount);
1972 	vrele(nvp);
1973 	vgone(nvp);
1974 	return (ovp);
1975 }
1976 
1977 /* This is a local helper function that do the same as addaliasu, but for a
1978  * struct cdev *instead of an dev_t. */
1979 static void
1980 addalias(nvp, dev)
1981 	struct vnode *nvp;
1982 	struct cdev *dev;
1983 {
1984 
1985 	KASSERT(nvp->v_type == VCHR, ("addalias on non-special vnode"));
1986 	dev_ref(dev);
1987 	nvp->v_rdev = dev;
1988 	VI_LOCK(nvp);
1989 	mtx_lock(&spechash_mtx);
1990 	SLIST_INSERT_HEAD(&dev->si_hlist, nvp, v_specnext);
1991 	dev->si_usecount += nvp->v_usecount;
1992 	mtx_unlock(&spechash_mtx);
1993 	VI_UNLOCK(nvp);
1994 }
1995 
1996 /*
1997  * Grab a particular vnode from the free list, increment its
1998  * reference count and lock it. The vnode lock bit is set if the
1999  * vnode is being eliminated in vgone. The process is awakened
2000  * when the transition is completed, and an error returned to
2001  * indicate that the vnode is no longer usable (possibly having
2002  * been changed to a new filesystem type).
2003  */
2004 int
2005 vget(vp, flags, td)
2006 	register struct vnode *vp;
2007 	int flags;
2008 	struct thread *td;
2009 {
2010 	int error;
2011 
2012 	/*
2013 	 * If the vnode is in the process of being cleaned out for
2014 	 * another use, we wait for the cleaning to finish and then
2015 	 * return failure. Cleaning is determined by checking that
2016 	 * the VI_XLOCK flag is set.
2017 	 */
2018 	if ((flags & LK_INTERLOCK) == 0)
2019 		VI_LOCK(vp);
2020 	if (vp->v_iflag & VI_XLOCK && vp->v_vxthread != curthread) {
2021 		if ((flags & LK_NOWAIT) == 0) {
2022 			vp->v_iflag |= VI_XWANT;
2023 			msleep(vp, VI_MTX(vp), PINOD | PDROP, "vget", 0);
2024 			return (ENOENT);
2025 		}
2026 		VI_UNLOCK(vp);
2027 		return (EBUSY);
2028 	}
2029 
2030 	v_incr_usecount(vp, 1);
2031 
2032 	if (VSHOULDBUSY(vp))
2033 		vbusy(vp);
2034 	if (flags & LK_TYPE_MASK) {
2035 		if ((error = vn_lock(vp, flags | LK_INTERLOCK, td)) != 0) {
2036 			/*
2037 			 * must expand vrele here because we do not want
2038 			 * to call VOP_INACTIVE if the reference count
2039 			 * drops back to zero since it was never really
2040 			 * active. We must remove it from the free list
2041 			 * before sleeping so that multiple processes do
2042 			 * not try to recycle it.
2043 			 */
2044 			VI_LOCK(vp);
2045 			v_incr_usecount(vp, -1);
2046 			if (VSHOULDFREE(vp))
2047 				vfree(vp);
2048 			else
2049 				vlruvp(vp);
2050 			VI_UNLOCK(vp);
2051 		}
2052 		return (error);
2053 	}
2054 	VI_UNLOCK(vp);
2055 	return (0);
2056 }
2057 
2058 /*
2059  * Increase the reference count of a vnode.
2060  */
2061 void
2062 vref(struct vnode *vp)
2063 {
2064 
2065 	VI_LOCK(vp);
2066 	v_incr_usecount(vp, 1);
2067 	VI_UNLOCK(vp);
2068 }
2069 
2070 /*
2071  * Return reference count of a vnode.
2072  *
2073  * The results of this call are only guaranteed when some mechanism other
2074  * than the VI lock is used to stop other processes from gaining references
2075  * to the vnode.  This may be the case if the caller holds the only reference.
2076  * This is also useful when stale data is acceptable as race conditions may
2077  * be accounted for by some other means.
2078  */
2079 int
2080 vrefcnt(struct vnode *vp)
2081 {
2082 	int usecnt;
2083 
2084 	VI_LOCK(vp);
2085 	usecnt = vp->v_usecount;
2086 	VI_UNLOCK(vp);
2087 
2088 	return (usecnt);
2089 }
2090 
2091 
2092 /*
2093  * Vnode put/release.
2094  * If count drops to zero, call inactive routine and return to freelist.
2095  */
2096 void
2097 vrele(vp)
2098 	struct vnode *vp;
2099 {
2100 	struct thread *td = curthread;	/* XXX */
2101 
2102 	GIANT_REQUIRED;
2103 
2104 	KASSERT(vp != NULL, ("vrele: null vp"));
2105 
2106 	VI_LOCK(vp);
2107 
2108 	/* Skip this v_writecount check if we're going to panic below. */
2109 	KASSERT(vp->v_writecount < vp->v_usecount || vp->v_usecount < 1,
2110 	    ("vrele: missed vn_close"));
2111 
2112 	if (vp->v_usecount > 1 || ((vp->v_iflag & VI_DOINGINACT) &&
2113 	    vp->v_usecount == 1)) {
2114 		v_incr_usecount(vp, -1);
2115 		VI_UNLOCK(vp);
2116 
2117 		return;
2118 	}
2119 
2120 	if (vp->v_usecount == 1) {
2121 		v_incr_usecount(vp, -1);
2122 		/*
2123 		 * We must call VOP_INACTIVE with the node locked. Mark
2124 		 * as VI_DOINGINACT to avoid recursion.
2125 		 */
2126 		if (vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK, td) == 0) {
2127 			VI_LOCK(vp);
2128 			vp->v_iflag |= VI_DOINGINACT;
2129 			VI_UNLOCK(vp);
2130 			VOP_INACTIVE(vp, td);
2131 			VI_LOCK(vp);
2132 			KASSERT(vp->v_iflag & VI_DOINGINACT,
2133 			    ("vrele: lost VI_DOINGINACT"));
2134 			vp->v_iflag &= ~VI_DOINGINACT;
2135 		} else
2136 			VI_LOCK(vp);
2137 		if (VSHOULDFREE(vp))
2138 			vfree(vp);
2139 		else
2140 			vlruvp(vp);
2141 		VI_UNLOCK(vp);
2142 
2143 	} else {
2144 #ifdef DIAGNOSTIC
2145 		vprint("vrele: negative ref count", vp);
2146 #endif
2147 		VI_UNLOCK(vp);
2148 		panic("vrele: negative ref cnt");
2149 	}
2150 }
2151 
2152 /*
2153  * Release an already locked vnode.  This give the same effects as
2154  * unlock+vrele(), but takes less time and avoids releasing and
2155  * re-aquiring the lock (as vrele() aquires the lock internally.)
2156  */
2157 void
2158 vput(vp)
2159 	struct vnode *vp;
2160 {
2161 	struct thread *td = curthread;	/* XXX */
2162 
2163 	GIANT_REQUIRED;
2164 
2165 	KASSERT(vp != NULL, ("vput: null vp"));
2166 	VI_LOCK(vp);
2167 	/* Skip this v_writecount check if we're going to panic below. */
2168 	KASSERT(vp->v_writecount < vp->v_usecount || vp->v_usecount < 1,
2169 	    ("vput: missed vn_close"));
2170 
2171 	if (vp->v_usecount > 1 || ((vp->v_iflag & VI_DOINGINACT) &&
2172 	    vp->v_usecount == 1)) {
2173 		v_incr_usecount(vp, -1);
2174 		VOP_UNLOCK(vp, LK_INTERLOCK, td);
2175 		return;
2176 	}
2177 
2178 	if (vp->v_usecount == 1) {
2179 		v_incr_usecount(vp, -1);
2180 		/*
2181 		 * We must call VOP_INACTIVE with the node locked, so
2182 		 * we just need to release the vnode mutex. Mark as
2183 		 * as VI_DOINGINACT to avoid recursion.
2184 		 */
2185 		vp->v_iflag |= VI_DOINGINACT;
2186 		VI_UNLOCK(vp);
2187 		VOP_INACTIVE(vp, td);
2188 		VI_LOCK(vp);
2189 		KASSERT(vp->v_iflag & VI_DOINGINACT,
2190 		    ("vput: lost VI_DOINGINACT"));
2191 		vp->v_iflag &= ~VI_DOINGINACT;
2192 		if (VSHOULDFREE(vp))
2193 			vfree(vp);
2194 		else
2195 			vlruvp(vp);
2196 		VI_UNLOCK(vp);
2197 
2198 	} else {
2199 #ifdef DIAGNOSTIC
2200 		vprint("vput: negative ref count", vp);
2201 #endif
2202 		panic("vput: negative ref cnt");
2203 	}
2204 }
2205 
2206 /*
2207  * Somebody doesn't want the vnode recycled.
2208  */
2209 void
2210 vhold(struct vnode *vp)
2211 {
2212 
2213 	VI_LOCK(vp);
2214 	vholdl(vp);
2215 	VI_UNLOCK(vp);
2216 }
2217 
2218 void
2219 vholdl(vp)
2220 	register struct vnode *vp;
2221 {
2222 
2223 	vp->v_holdcnt++;
2224 	if (VSHOULDBUSY(vp))
2225 		vbusy(vp);
2226 }
2227 
2228 /*
2229  * Note that there is one less who cares about this vnode.  vdrop() is the
2230  * opposite of vhold().
2231  */
2232 void
2233 vdrop(struct vnode *vp)
2234 {
2235 
2236 	VI_LOCK(vp);
2237 	vdropl(vp);
2238 	VI_UNLOCK(vp);
2239 }
2240 
2241 void
2242 vdropl(vp)
2243 	register struct vnode *vp;
2244 {
2245 
2246 	if (vp->v_holdcnt <= 0)
2247 		panic("vdrop: holdcnt");
2248 	vp->v_holdcnt--;
2249 	if (VSHOULDFREE(vp))
2250 		vfree(vp);
2251 	else
2252 		vlruvp(vp);
2253 }
2254 
2255 /*
2256  * Remove any vnodes in the vnode table belonging to mount point mp.
2257  *
2258  * If FORCECLOSE is not specified, there should not be any active ones,
2259  * return error if any are found (nb: this is a user error, not a
2260  * system error). If FORCECLOSE is specified, detach any active vnodes
2261  * that are found.
2262  *
2263  * If WRITECLOSE is set, only flush out regular file vnodes open for
2264  * writing.
2265  *
2266  * SKIPSYSTEM causes any vnodes marked VV_SYSTEM to be skipped.
2267  *
2268  * `rootrefs' specifies the base reference count for the root vnode
2269  * of this filesystem. The root vnode is considered busy if its
2270  * v_usecount exceeds this value. On a successful return, vflush(, td)
2271  * will call vrele() on the root vnode exactly rootrefs times.
2272  * If the SKIPSYSTEM or WRITECLOSE flags are specified, rootrefs must
2273  * be zero.
2274  */
2275 #ifdef DIAGNOSTIC
2276 static int busyprt = 0;		/* print out busy vnodes */
2277 SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, "");
2278 #endif
2279 
2280 int
2281 vflush(mp, rootrefs, flags, td)
2282 	struct mount *mp;
2283 	int rootrefs;
2284 	int flags;
2285 	struct thread *td;
2286 {
2287 	struct vnode *vp, *nvp, *rootvp = NULL;
2288 	struct vattr vattr;
2289 	int busy = 0, error;
2290 
2291 	if (rootrefs > 0) {
2292 		KASSERT((flags & (SKIPSYSTEM | WRITECLOSE)) == 0,
2293 		    ("vflush: bad args"));
2294 		/*
2295 		 * Get the filesystem root vnode. We can vput() it
2296 		 * immediately, since with rootrefs > 0, it won't go away.
2297 		 */
2298 		if ((error = VFS_ROOT(mp, &rootvp, td)) != 0)
2299 			return (error);
2300 		vput(rootvp);
2301 
2302 	}
2303 	MNT_ILOCK(mp);
2304 loop:
2305 	MNT_VNODE_FOREACH(vp, mp, nvp) {
2306 
2307 		VI_LOCK(vp);
2308 		MNT_IUNLOCK(mp);
2309 		error = vn_lock(vp, LK_INTERLOCK | LK_EXCLUSIVE, td);
2310 		if (error) {
2311 			MNT_ILOCK(mp);
2312 			goto loop;
2313 		}
2314 		/*
2315 		 * Skip over a vnodes marked VV_SYSTEM.
2316 		 */
2317 		if ((flags & SKIPSYSTEM) && (vp->v_vflag & VV_SYSTEM)) {
2318 			VOP_UNLOCK(vp, 0, td);
2319 			MNT_ILOCK(mp);
2320 			continue;
2321 		}
2322 		/*
2323 		 * If WRITECLOSE is set, flush out unlinked but still open
2324 		 * files (even if open only for reading) and regular file
2325 		 * vnodes open for writing.
2326 		 */
2327 		if (flags & WRITECLOSE) {
2328 			error = VOP_GETATTR(vp, &vattr, td->td_ucred, td);
2329 			VI_LOCK(vp);
2330 
2331 			if ((vp->v_type == VNON ||
2332 			    (error == 0 && vattr.va_nlink > 0)) &&
2333 			    (vp->v_writecount == 0 || vp->v_type != VREG)) {
2334 				VOP_UNLOCK(vp, LK_INTERLOCK, td);
2335 				MNT_ILOCK(mp);
2336 				continue;
2337 			}
2338 		} else
2339 			VI_LOCK(vp);
2340 
2341 		VOP_UNLOCK(vp, 0, td);
2342 
2343 		/*
2344 		 * With v_usecount == 0, all we need to do is clear out the
2345 		 * vnode data structures and we are done.
2346 		 */
2347 		if (vp->v_usecount == 0) {
2348 			vgonel(vp, td);
2349 			MNT_ILOCK(mp);
2350 			continue;
2351 		}
2352 
2353 		/*
2354 		 * If FORCECLOSE is set, forcibly close the vnode. For block
2355 		 * or character devices, revert to an anonymous device. For
2356 		 * all other files, just kill them.
2357 		 */
2358 		if (flags & FORCECLOSE) {
2359 			if (vp->v_type != VCHR)
2360 				vgonel(vp, td);
2361 			else
2362 				vgonechrl(vp, td);
2363 			MNT_ILOCK(mp);
2364 			continue;
2365 		}
2366 #ifdef DIAGNOSTIC
2367 		if (busyprt)
2368 			vprint("vflush: busy vnode", vp);
2369 #endif
2370 		VI_UNLOCK(vp);
2371 		MNT_ILOCK(mp);
2372 		busy++;
2373 	}
2374 	MNT_IUNLOCK(mp);
2375 	if (rootrefs > 0 && (flags & FORCECLOSE) == 0) {
2376 		/*
2377 		 * If just the root vnode is busy, and if its refcount
2378 		 * is equal to `rootrefs', then go ahead and kill it.
2379 		 */
2380 		VI_LOCK(rootvp);
2381 		KASSERT(busy > 0, ("vflush: not busy"));
2382 		KASSERT(rootvp->v_usecount >= rootrefs,
2383 		    ("vflush: usecount %d < rootrefs %d",
2384 		     rootvp->v_usecount, rootrefs));
2385 		if (busy == 1 && rootvp->v_usecount == rootrefs) {
2386 			vgonel(rootvp, td);
2387 			busy = 0;
2388 		} else
2389 			VI_UNLOCK(rootvp);
2390 	}
2391 	if (busy)
2392 		return (EBUSY);
2393 	for (; rootrefs > 0; rootrefs--)
2394 		vrele(rootvp);
2395 	return (0);
2396 }
2397 
2398 /*
2399  * This moves a now (likely recyclable) vnode to the end of the
2400  * mountlist.  XXX However, it is temporarily disabled until we
2401  * can clean up ffs_sync() and friends, which have loop restart
2402  * conditions which this code causes to operate O(N^2).
2403  */
2404 static void
2405 vlruvp(struct vnode *vp)
2406 {
2407 #if 0
2408 	struct mount *mp;
2409 
2410 	if ((mp = vp->v_mount) != NULL) {
2411 		MNT_ILOCK(mp);
2412 		TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
2413 		TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
2414 		MNT_IUNLOCK(mp);
2415 	}
2416 #endif
2417 }
2418 
2419 static void
2420 vx_lock(struct vnode *vp)
2421 {
2422 
2423 	ASSERT_VI_LOCKED(vp, "vx_lock");
2424 
2425 	/*
2426 	 * Prevent the vnode from being recycled or brought into use while we
2427 	 * clean it out.
2428 	 */
2429 	if (vp->v_iflag & VI_XLOCK)
2430 		panic("vclean: deadlock");
2431 	vp->v_iflag |= VI_XLOCK;
2432 	vp->v_vxthread = curthread;
2433 }
2434 
2435 static void
2436 vx_unlock(struct vnode *vp)
2437 {
2438 	ASSERT_VI_LOCKED(vp, "vx_unlock");
2439 	vp->v_iflag &= ~VI_XLOCK;
2440 	vp->v_vxthread = NULL;
2441 	if (vp->v_iflag & VI_XWANT) {
2442 		vp->v_iflag &= ~VI_XWANT;
2443 		wakeup(vp);
2444 	}
2445 }
2446 
2447 /*
2448  * Disassociate the underlying filesystem from a vnode.
2449  */
2450 static void
2451 vclean(vp, flags, td)
2452 	struct vnode *vp;
2453 	int flags;
2454 	struct thread *td;
2455 {
2456 	int active;
2457 
2458 	ASSERT_VI_LOCKED(vp, "vclean");
2459 	/*
2460 	 * Check to see if the vnode is in use. If so we have to reference it
2461 	 * before we clean it out so that its count cannot fall to zero and
2462 	 * generate a race against ourselves to recycle it.
2463 	 */
2464 	if ((active = vp->v_usecount))
2465 		v_incr_usecount(vp, 1);
2466 
2467 	/*
2468 	 * Even if the count is zero, the VOP_INACTIVE routine may still
2469 	 * have the object locked while it cleans it out. The VOP_LOCK
2470 	 * ensures that the VOP_INACTIVE routine is done with its work.
2471 	 * For active vnodes, it ensures that no other activity can
2472 	 * occur while the underlying object is being cleaned out.
2473 	 */
2474 	VOP_LOCK(vp, LK_DRAIN | LK_INTERLOCK, td);
2475 
2476 	/*
2477 	 * Clean out any buffers associated with the vnode.
2478 	 * If the flush fails, just toss the buffers.
2479 	 */
2480 	if (flags & DOCLOSE) {
2481 		struct buf *bp;
2482 		bp = TAILQ_FIRST(&vp->v_dirtyblkhd);
2483 		if (bp != NULL)
2484 			(void) vn_write_suspend_wait(vp, NULL, V_WAIT);
2485 		if (vinvalbuf(vp, V_SAVE, NOCRED, td, 0, 0) != 0)
2486 			vinvalbuf(vp, 0, NOCRED, td, 0, 0);
2487 	}
2488 
2489 	VOP_DESTROYVOBJECT(vp);
2490 
2491 	/*
2492 	 * Any other processes trying to obtain this lock must first
2493 	 * wait for VXLOCK to clear, then call the new lock operation.
2494 	 */
2495 	VOP_UNLOCK(vp, 0, td);
2496 
2497 	/*
2498 	 * If purging an active vnode, it must be closed and
2499 	 * deactivated before being reclaimed. Note that the
2500 	 * VOP_INACTIVE will unlock the vnode.
2501 	 */
2502 	if (active) {
2503 		if (flags & DOCLOSE)
2504 			VOP_CLOSE(vp, FNONBLOCK, NOCRED, td);
2505 		VI_LOCK(vp);
2506 		if ((vp->v_iflag & VI_DOINGINACT) == 0) {
2507 			vp->v_iflag |= VI_DOINGINACT;
2508 			VI_UNLOCK(vp);
2509 			if (vn_lock(vp, LK_EXCLUSIVE | LK_NOWAIT, td) != 0)
2510 				panic("vclean: cannot relock.");
2511 			VOP_INACTIVE(vp, td);
2512 			VI_LOCK(vp);
2513 			KASSERT(vp->v_iflag & VI_DOINGINACT,
2514 			    ("vclean: lost VI_DOINGINACT"));
2515 			vp->v_iflag &= ~VI_DOINGINACT;
2516 		}
2517 		VI_UNLOCK(vp);
2518 	}
2519 	/*
2520 	 * Reclaim the vnode.
2521 	 */
2522 	if (VOP_RECLAIM(vp, td))
2523 		panic("vclean: cannot reclaim");
2524 
2525 	if (active) {
2526 		/*
2527 		 * Inline copy of vrele() since VOP_INACTIVE
2528 		 * has already been called.
2529 		 */
2530 		VI_LOCK(vp);
2531 		v_incr_usecount(vp, -1);
2532 		if (vp->v_usecount <= 0) {
2533 #ifdef INVARIANTS
2534 			if (vp->v_usecount < 0 || vp->v_writecount != 0) {
2535 				vprint("vclean: bad ref count", vp);
2536 				panic("vclean: ref cnt");
2537 			}
2538 #endif
2539 			if (VSHOULDFREE(vp))
2540 				vfree(vp);
2541 		}
2542 		VI_UNLOCK(vp);
2543 	}
2544 	/*
2545 	 * Delete from old mount point vnode list.
2546 	 */
2547 	delmntque(vp);
2548 	cache_purge(vp);
2549 	VI_LOCK(vp);
2550 	if (VSHOULDFREE(vp))
2551 		vfree(vp);
2552 
2553 	/*
2554 	 * Done with purge, reset to the standard lock and
2555 	 * notify sleepers of the grim news.
2556 	 */
2557 	vp->v_vnlock = &vp->v_lock;
2558 	vp->v_op = dead_vnodeop_p;
2559 	if (vp->v_pollinfo != NULL)
2560 		vn_pollgone(vp);
2561 	vp->v_tag = "none";
2562 }
2563 
2564 /*
2565  * Eliminate all activity associated with the requested vnode
2566  * and with all vnodes aliased to the requested vnode.
2567  */
2568 int
2569 vop_revoke(ap)
2570 	struct vop_revoke_args /* {
2571 		struct vnode *a_vp;
2572 		int a_flags;
2573 	} */ *ap;
2574 {
2575 	struct vnode *vp, *vq;
2576 	struct cdev *dev;
2577 
2578 	KASSERT((ap->a_flags & REVOKEALL) != 0, ("vop_revoke"));
2579 	vp = ap->a_vp;
2580 	KASSERT((vp->v_type == VCHR), ("vop_revoke: not VCHR"));
2581 
2582 	VI_LOCK(vp);
2583 	/*
2584 	 * If a vgone (or vclean) is already in progress,
2585 	 * wait until it is done and return.
2586 	 */
2587 	if (vp->v_iflag & VI_XLOCK) {
2588 		vp->v_iflag |= VI_XWANT;
2589 		msleep(vp, VI_MTX(vp), PINOD | PDROP,
2590 		    "vop_revokeall", 0);
2591 		return (0);
2592 	}
2593 	VI_UNLOCK(vp);
2594 	dev = vp->v_rdev;
2595 	for (;;) {
2596 		mtx_lock(&spechash_mtx);
2597 		vq = SLIST_FIRST(&dev->si_hlist);
2598 		mtx_unlock(&spechash_mtx);
2599 		if (vq == NULL)
2600 			break;
2601 		vgone(vq);
2602 	}
2603 	return (0);
2604 }
2605 
2606 /*
2607  * Recycle an unused vnode to the front of the free list.
2608  * Release the passed interlock if the vnode will be recycled.
2609  */
2610 int
2611 vrecycle(vp, inter_lkp, td)
2612 	struct vnode *vp;
2613 	struct mtx *inter_lkp;
2614 	struct thread *td;
2615 {
2616 
2617 	VI_LOCK(vp);
2618 	if (vp->v_usecount == 0) {
2619 		if (inter_lkp) {
2620 			mtx_unlock(inter_lkp);
2621 		}
2622 		vgonel(vp, td);
2623 		return (1);
2624 	}
2625 	VI_UNLOCK(vp);
2626 	return (0);
2627 }
2628 
2629 /*
2630  * Eliminate all activity associated with a vnode
2631  * in preparation for reuse.
2632  */
2633 void
2634 vgone(vp)
2635 	register struct vnode *vp;
2636 {
2637 	struct thread *td = curthread;	/* XXX */
2638 
2639 	VI_LOCK(vp);
2640 	vgonel(vp, td);
2641 }
2642 
2643 /*
2644  * Disassociate a character device from the its underlying filesystem and
2645  * attach it to spec.  This is for use when the chr device is still active
2646  * and the filesystem is going away.
2647  */
2648 static void
2649 vgonechrl(struct vnode *vp, struct thread *td)
2650 {
2651 	ASSERT_VI_LOCKED(vp, "vgonechrl");
2652 	vx_lock(vp);
2653 	/*
2654 	 * This is a custom version of vclean() which does not tearm down
2655 	 * the bufs or vm objects held by this vnode.  This allows filesystems
2656 	 * to continue using devices which were discovered via another
2657 	 * filesystem that has been unmounted.
2658 	 */
2659 	if (vp->v_usecount != 0) {
2660 		v_incr_usecount(vp, 1);
2661 		/*
2662 		 * Ensure that no other activity can occur while the
2663 		 * underlying object is being cleaned out.
2664 		 */
2665 		VOP_LOCK(vp, LK_DRAIN | LK_INTERLOCK, td);
2666 		/*
2667 		 * Any other processes trying to obtain this lock must first
2668 		 * wait for VXLOCK to clear, then call the new lock operation.
2669 		 */
2670 		VOP_UNLOCK(vp, 0, td);
2671 		vp->v_vnlock = &vp->v_lock;
2672 		vp->v_tag = "orphanchr";
2673 		vp->v_op = spec_vnodeop_p;
2674 		delmntque(vp);
2675 		cache_purge(vp);
2676 		vrele(vp);
2677 		VI_LOCK(vp);
2678 	} else
2679 		vclean(vp, 0, td);
2680 	vp->v_op = spec_vnodeop_p;
2681 	vx_unlock(vp);
2682 	VI_UNLOCK(vp);
2683 }
2684 
2685 /*
2686  * vgone, with the vp interlock held.
2687  */
2688 void
2689 vgonel(vp, td)
2690 	struct vnode *vp;
2691 	struct thread *td;
2692 {
2693 	/*
2694 	 * If a vgone (or vclean) is already in progress,
2695 	 * wait until it is done and return.
2696 	 */
2697 	ASSERT_VI_LOCKED(vp, "vgonel");
2698 	if (vp->v_iflag & VI_XLOCK) {
2699 		vp->v_iflag |= VI_XWANT;
2700 		msleep(vp, VI_MTX(vp), PINOD | PDROP, "vgone", 0);
2701 		return;
2702 	}
2703 	vx_lock(vp);
2704 
2705 	/*
2706 	 * Clean out the filesystem specific data.
2707 	 */
2708 	vclean(vp, DOCLOSE, td);
2709 	VI_UNLOCK(vp);
2710 
2711 	/*
2712 	 * If special device, remove it from special device alias list
2713 	 * if it is on one.
2714 	 */
2715 	VI_LOCK(vp);
2716 	if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2717 		mtx_lock(&spechash_mtx);
2718 		SLIST_REMOVE(&vp->v_rdev->si_hlist, vp, vnode, v_specnext);
2719 		vp->v_rdev->si_usecount -= vp->v_usecount;
2720 		mtx_unlock(&spechash_mtx);
2721 		dev_rel(vp->v_rdev);
2722 		vp->v_rdev = NULL;
2723 	}
2724 
2725 	/*
2726 	 * If it is on the freelist and not already at the head,
2727 	 * move it to the head of the list. The test of the
2728 	 * VDOOMED flag and the reference count of zero is because
2729 	 * it will be removed from the free list by getnewvnode,
2730 	 * but will not have its reference count incremented until
2731 	 * after calling vgone. If the reference count were
2732 	 * incremented first, vgone would (incorrectly) try to
2733 	 * close the previous instance of the underlying object.
2734 	 */
2735 	if (vp->v_usecount == 0 && !(vp->v_iflag & VI_DOOMED)) {
2736 		mtx_lock(&vnode_free_list_mtx);
2737 		if (vp->v_iflag & VI_FREE) {
2738 			TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
2739 		} else {
2740 			vp->v_iflag |= VI_FREE;
2741 			freevnodes++;
2742 		}
2743 		TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
2744 		mtx_unlock(&vnode_free_list_mtx);
2745 	}
2746 
2747 	vp->v_type = VBAD;
2748 	vx_unlock(vp);
2749 	VI_UNLOCK(vp);
2750 }
2751 
2752 /*
2753  * Lookup a vnode by device number.
2754  */
2755 int
2756 vfinddev(dev, vpp)
2757 	struct cdev *dev;
2758 	struct vnode **vpp;
2759 {
2760 	struct vnode *vp;
2761 
2762 	mtx_lock(&spechash_mtx);
2763 	SLIST_FOREACH(vp, &dev->si_hlist, v_specnext) {
2764 		*vpp = vp;
2765 		mtx_unlock(&spechash_mtx);
2766 		return (1);
2767 	}
2768 	mtx_unlock(&spechash_mtx);
2769 	return (0);
2770 }
2771 
2772 /*
2773  * Calculate the total number of references to a special device.
2774  */
2775 int
2776 vcount(vp)
2777 	struct vnode *vp;
2778 {
2779 	int count;
2780 
2781 	mtx_lock(&spechash_mtx);
2782 	count = vp->v_rdev->si_usecount;
2783 	mtx_unlock(&spechash_mtx);
2784 	return (count);
2785 }
2786 
2787 /*
2788  * Same as above, but using the struct cdev *as argument
2789  */
2790 int
2791 count_dev(dev)
2792 	struct cdev *dev;
2793 {
2794 	int count;
2795 
2796 	mtx_lock(&spechash_mtx);
2797 	count = dev->si_usecount;
2798 	mtx_unlock(&spechash_mtx);
2799 	return(count);
2800 }
2801 
2802 /*
2803  * Print out a description of a vnode.
2804  */
2805 static char *typename[] =
2806 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD"};
2807 
2808 void
2809 vprint(label, vp)
2810 	char *label;
2811 	struct vnode *vp;
2812 {
2813 	char buf[96];
2814 
2815 	if (label != NULL)
2816 		printf("%s: %p: ", label, (void *)vp);
2817 	else
2818 		printf("%p: ", (void *)vp);
2819 	printf("tag %s, type %s, usecount %d, writecount %d, refcount %d,",
2820 	    vp->v_tag, typename[vp->v_type], vp->v_usecount,
2821 	    vp->v_writecount, vp->v_holdcnt);
2822 	buf[0] = '\0';
2823 	if (vp->v_vflag & VV_ROOT)
2824 		strcat(buf, "|VV_ROOT");
2825 	if (vp->v_vflag & VV_TEXT)
2826 		strcat(buf, "|VV_TEXT");
2827 	if (vp->v_vflag & VV_SYSTEM)
2828 		strcat(buf, "|VV_SYSTEM");
2829 	if (vp->v_iflag & VI_XLOCK)
2830 		strcat(buf, "|VI_XLOCK");
2831 	if (vp->v_iflag & VI_XWANT)
2832 		strcat(buf, "|VI_XWANT");
2833 	if (vp->v_iflag & VI_BWAIT)
2834 		strcat(buf, "|VI_BWAIT");
2835 	if (vp->v_iflag & VI_DOOMED)
2836 		strcat(buf, "|VI_DOOMED");
2837 	if (vp->v_iflag & VI_FREE)
2838 		strcat(buf, "|VI_FREE");
2839 	if (vp->v_vflag & VV_OBJBUF)
2840 		strcat(buf, "|VV_OBJBUF");
2841 	if (buf[0] != '\0')
2842 		printf(" flags (%s),", &buf[1]);
2843 	lockmgr_printinfo(vp->v_vnlock);
2844 	printf("\n");
2845 	if (vp->v_data != NULL)
2846 		VOP_PRINT(vp);
2847 }
2848 
2849 #ifdef DDB
2850 #include <ddb/ddb.h>
2851 /*
2852  * List all of the locked vnodes in the system.
2853  * Called when debugging the kernel.
2854  */
2855 DB_SHOW_COMMAND(lockedvnods, lockedvnodes)
2856 {
2857 	struct mount *mp, *nmp;
2858 	struct vnode *vp;
2859 
2860 	/*
2861 	 * Note: because this is DDB, we can't obey the locking semantics
2862 	 * for these structures, which means we could catch an inconsistent
2863 	 * state and dereference a nasty pointer.  Not much to be done
2864 	 * about that.
2865 	 */
2866 	printf("Locked vnodes\n");
2867 	for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
2868 		nmp = TAILQ_NEXT(mp, mnt_list);
2869 		TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
2870 			if (VOP_ISLOCKED(vp, NULL))
2871 				vprint(NULL, vp);
2872 		}
2873 		nmp = TAILQ_NEXT(mp, mnt_list);
2874 	}
2875 }
2876 #endif
2877 
2878 /*
2879  * Fill in a struct xvfsconf based on a struct vfsconf.
2880  */
2881 static void
2882 vfsconf2x(struct vfsconf *vfsp, struct xvfsconf *xvfsp)
2883 {
2884 
2885 	strcpy(xvfsp->vfc_name, vfsp->vfc_name);
2886 	xvfsp->vfc_typenum = vfsp->vfc_typenum;
2887 	xvfsp->vfc_refcount = vfsp->vfc_refcount;
2888 	xvfsp->vfc_flags = vfsp->vfc_flags;
2889 	/*
2890 	 * These are unused in userland, we keep them
2891 	 * to not break binary compatibility.
2892 	 */
2893 	xvfsp->vfc_vfsops = NULL;
2894 	xvfsp->vfc_next = NULL;
2895 }
2896 
2897 /*
2898  * Top level filesystem related information gathering.
2899  */
2900 static int
2901 sysctl_vfs_conflist(SYSCTL_HANDLER_ARGS)
2902 {
2903 	struct vfsconf *vfsp;
2904 	struct xvfsconf *xvfsp;
2905 	int cnt, error, i;
2906 
2907 	cnt = 0;
2908 	for (vfsp = vfsconf; vfsp != NULL; vfsp = vfsp->vfc_next)
2909 		cnt++;
2910 	xvfsp = malloc(sizeof(struct xvfsconf) * cnt, M_TEMP, M_WAITOK);
2911 	/*
2912 	 * Handle the race that we will have here when struct vfsconf
2913 	 * will be locked down by using both cnt and checking vfc_next
2914 	 * against NULL to determine the end of the loop.  The race will
2915 	 * happen because we will have to unlock before calling malloc().
2916 	 * We are protected by Giant for now.
2917 	 */
2918 	i = 0;
2919 	for (vfsp = vfsconf; vfsp != NULL && i < cnt; vfsp = vfsp->vfc_next) {
2920 		vfsconf2x(vfsp, xvfsp + i);
2921 		i++;
2922 	}
2923 	error = SYSCTL_OUT(req, xvfsp, sizeof(struct xvfsconf) * i);
2924 	free(xvfsp, M_TEMP);
2925 	return (error);
2926 }
2927 
2928 SYSCTL_PROC(_vfs, OID_AUTO, conflist, CTLFLAG_RD, NULL, 0, sysctl_vfs_conflist,
2929     "S,xvfsconf", "List of all configured filesystems");
2930 
2931 #ifndef BURN_BRIDGES
2932 static int	sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS);
2933 
2934 static int
2935 vfs_sysctl(SYSCTL_HANDLER_ARGS)
2936 {
2937 	int *name = (int *)arg1 - 1;	/* XXX */
2938 	u_int namelen = arg2 + 1;	/* XXX */
2939 	struct vfsconf *vfsp;
2940 	struct xvfsconf xvfsp;
2941 
2942 	printf("WARNING: userland calling deprecated sysctl, "
2943 	    "please rebuild world\n");
2944 
2945 #if 1 || defined(COMPAT_PRELITE2)
2946 	/* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
2947 	if (namelen == 1)
2948 		return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
2949 #endif
2950 
2951 	switch (name[1]) {
2952 	case VFS_MAXTYPENUM:
2953 		if (namelen != 2)
2954 			return (ENOTDIR);
2955 		return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int)));
2956 	case VFS_CONF:
2957 		if (namelen != 3)
2958 			return (ENOTDIR);	/* overloaded */
2959 		for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
2960 			if (vfsp->vfc_typenum == name[2])
2961 				break;
2962 		if (vfsp == NULL)
2963 			return (EOPNOTSUPP);
2964 		vfsconf2x(vfsp, &xvfsp);
2965 		return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp)));
2966 	}
2967 	return (EOPNOTSUPP);
2968 }
2969 
2970 SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD | CTLFLAG_SKIP, vfs_sysctl,
2971 	"Generic filesystem");
2972 
2973 #if 1 || defined(COMPAT_PRELITE2)
2974 
2975 static int
2976 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
2977 {
2978 	int error;
2979 	struct vfsconf *vfsp;
2980 	struct ovfsconf ovfs;
2981 
2982 	for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
2983 		ovfs.vfc_vfsops = vfsp->vfc_vfsops;	/* XXX used as flag */
2984 		strcpy(ovfs.vfc_name, vfsp->vfc_name);
2985 		ovfs.vfc_index = vfsp->vfc_typenum;
2986 		ovfs.vfc_refcount = vfsp->vfc_refcount;
2987 		ovfs.vfc_flags = vfsp->vfc_flags;
2988 		error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
2989 		if (error)
2990 			return error;
2991 	}
2992 	return 0;
2993 }
2994 
2995 #endif /* 1 || COMPAT_PRELITE2 */
2996 #endif /* !BURN_BRIDGES */
2997 
2998 #define KINFO_VNODESLOP		10
2999 #ifdef notyet
3000 /*
3001  * Dump vnode list (via sysctl).
3002  */
3003 /* ARGSUSED */
3004 static int
3005 sysctl_vnode(SYSCTL_HANDLER_ARGS)
3006 {
3007 	struct xvnode *xvn;
3008 	struct thread *td = req->td;
3009 	struct mount *mp;
3010 	struct vnode *vp;
3011 	int error, len, n;
3012 
3013 	/*
3014 	 * Stale numvnodes access is not fatal here.
3015 	 */
3016 	req->lock = 0;
3017 	len = (numvnodes + KINFO_VNODESLOP) * sizeof *xvn;
3018 	if (!req->oldptr)
3019 		/* Make an estimate */
3020 		return (SYSCTL_OUT(req, 0, len));
3021 
3022 	error = sysctl_wire_old_buffer(req, 0);
3023 	if (error != 0)
3024 		return (error);
3025 	xvn = malloc(len, M_TEMP, M_ZERO | M_WAITOK);
3026 	n = 0;
3027 	mtx_lock(&mountlist_mtx);
3028 	TAILQ_FOREACH(mp, &mountlist, mnt_list) {
3029 		if (vfs_busy(mp, LK_NOWAIT, &mountlist_mtx, td))
3030 			continue;
3031 		MNT_ILOCK(mp);
3032 		TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
3033 			if (n == len)
3034 				break;
3035 			vref(vp);
3036 			xvn[n].xv_size = sizeof *xvn;
3037 			xvn[n].xv_vnode = vp;
3038 #define XV_COPY(field) xvn[n].xv_##field = vp->v_##field
3039 			XV_COPY(usecount);
3040 			XV_COPY(writecount);
3041 			XV_COPY(holdcnt);
3042 			XV_COPY(id);
3043 			XV_COPY(mount);
3044 			XV_COPY(numoutput);
3045 			XV_COPY(type);
3046 #undef XV_COPY
3047 			xvn[n].xv_flag = vp->v_vflag;
3048 
3049 			switch (vp->v_type) {
3050 			case VREG:
3051 			case VDIR:
3052 			case VLNK:
3053 				xvn[n].xv_dev = vp->v_cachedfs;
3054 				xvn[n].xv_ino = vp->v_cachedid;
3055 				break;
3056 			case VBLK:
3057 			case VCHR:
3058 				if (vp->v_rdev == NULL) {
3059 					vrele(vp);
3060 					continue;
3061 				}
3062 				xvn[n].xv_dev = dev2udev(vp->v_rdev);
3063 				break;
3064 			case VSOCK:
3065 				xvn[n].xv_socket = vp->v_socket;
3066 				break;
3067 			case VFIFO:
3068 				xvn[n].xv_fifo = vp->v_fifoinfo;
3069 				break;
3070 			case VNON:
3071 			case VBAD:
3072 			default:
3073 				/* shouldn't happen? */
3074 				vrele(vp);
3075 				continue;
3076 			}
3077 			vrele(vp);
3078 			++n;
3079 		}
3080 		MNT_IUNLOCK(mp);
3081 		mtx_lock(&mountlist_mtx);
3082 		vfs_unbusy(mp, td);
3083 		if (n == len)
3084 			break;
3085 	}
3086 	mtx_unlock(&mountlist_mtx);
3087 
3088 	error = SYSCTL_OUT(req, xvn, n * sizeof *xvn);
3089 	free(xvn, M_TEMP);
3090 	return (error);
3091 }
3092 
3093 SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE|CTLFLAG_RD,
3094 	0, 0, sysctl_vnode, "S,xvnode", "");
3095 #endif
3096 
3097 /*
3098  * Check to see if a filesystem is mounted on a block device.
3099  */
3100 int
3101 vfs_mountedon(vp)
3102 	struct vnode *vp;
3103 {
3104 
3105 	if (vp->v_rdev->si_mountpoint != NULL)
3106 		return (EBUSY);
3107 	return (0);
3108 }
3109 
3110 /*
3111  * Unmount all filesystems. The list is traversed in reverse order
3112  * of mounting to avoid dependencies.
3113  */
3114 void
3115 vfs_unmountall()
3116 {
3117 	struct mount *mp;
3118 	struct thread *td;
3119 	int error;
3120 
3121 	if (curthread != NULL)
3122 		td = curthread;
3123 	else
3124 		td = FIRST_THREAD_IN_PROC(initproc); /* XXX XXX proc0? */
3125 	/*
3126 	 * Since this only runs when rebooting, it is not interlocked.
3127 	 */
3128 	while(!TAILQ_EMPTY(&mountlist)) {
3129 		mp = TAILQ_LAST(&mountlist, mntlist);
3130 		error = dounmount(mp, MNT_FORCE, td);
3131 		if (error) {
3132 			TAILQ_REMOVE(&mountlist, mp, mnt_list);
3133 			printf("unmount of %s failed (",
3134 			    mp->mnt_stat.f_mntonname);
3135 			if (error == EBUSY)
3136 				printf("BUSY)\n");
3137 			else
3138 				printf("%d)\n", error);
3139 		} else {
3140 			/* The unmount has removed mp from the mountlist */
3141 		}
3142 	}
3143 }
3144 
3145 /*
3146  * perform msync on all vnodes under a mount point
3147  * the mount point must be locked.
3148  */
3149 void
3150 vfs_msync(struct mount *mp, int flags)
3151 {
3152 	struct vnode *vp, *nvp;
3153 	struct vm_object *obj;
3154 	int tries;
3155 
3156 	GIANT_REQUIRED;
3157 
3158 	tries = 5;
3159 	MNT_ILOCK(mp);
3160 loop:
3161 	TAILQ_FOREACH_SAFE(vp, &mp->mnt_nvnodelist, v_nmntvnodes, nvp) {
3162 		if (vp->v_mount != mp) {
3163 			if (--tries > 0)
3164 				goto loop;
3165 			break;
3166 		}
3167 
3168 		VI_LOCK(vp);
3169 		if (vp->v_iflag & VI_XLOCK) {
3170 			VI_UNLOCK(vp);
3171 			continue;
3172 		}
3173 
3174 		if ((vp->v_iflag & VI_OBJDIRTY) &&
3175 		    (flags == MNT_WAIT || VOP_ISLOCKED(vp, NULL) == 0)) {
3176 			MNT_IUNLOCK(mp);
3177 			if (!vget(vp,
3178 			    LK_EXCLUSIVE | LK_RETRY | LK_INTERLOCK,
3179 			    curthread)) {
3180 				if (vp->v_vflag & VV_NOSYNC) {	/* unlinked */
3181 					vput(vp);
3182 					MNT_ILOCK(mp);
3183 					continue;
3184 				}
3185 
3186 				if (VOP_GETVOBJECT(vp, &obj) == 0) {
3187 					VM_OBJECT_LOCK(obj);
3188 					vm_object_page_clean(obj, 0, 0,
3189 					    flags == MNT_WAIT ?
3190 					    OBJPC_SYNC : OBJPC_NOSYNC);
3191 					VM_OBJECT_UNLOCK(obj);
3192 				}
3193 				vput(vp);
3194 			}
3195 			MNT_ILOCK(mp);
3196 			if (TAILQ_NEXT(vp, v_nmntvnodes) != nvp) {
3197 				if (--tries > 0)
3198 					goto loop;
3199 				break;
3200 			}
3201 		} else
3202 			VI_UNLOCK(vp);
3203 	}
3204 	MNT_IUNLOCK(mp);
3205 }
3206 
3207 /*
3208  * Create the VM object needed for VMIO and mmap support.  This
3209  * is done for all VREG files in the system.  Some filesystems might
3210  * afford the additional metadata buffering capability of the
3211  * VMIO code by making the device node be VMIO mode also.
3212  *
3213  * vp must be locked when vfs_object_create is called.
3214  */
3215 int
3216 vfs_object_create(vp, td, cred)
3217 	struct vnode *vp;
3218 	struct thread *td;
3219 	struct ucred *cred;
3220 {
3221 
3222 	GIANT_REQUIRED;
3223 	return (VOP_CREATEVOBJECT(vp, cred, td));
3224 }
3225 
3226 /*
3227  * Mark a vnode as free, putting it up for recycling.
3228  */
3229 void
3230 vfree(vp)
3231 	struct vnode *vp;
3232 {
3233 
3234 	ASSERT_VI_LOCKED(vp, "vfree");
3235 	mtx_lock(&vnode_free_list_mtx);
3236 	KASSERT((vp->v_iflag & VI_FREE) == 0, ("vnode already free"));
3237 	if (vp->v_iflag & VI_AGE) {
3238 		TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
3239 	} else {
3240 		TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
3241 	}
3242 	freevnodes++;
3243 	mtx_unlock(&vnode_free_list_mtx);
3244 	vp->v_iflag &= ~VI_AGE;
3245 	vp->v_iflag |= VI_FREE;
3246 }
3247 
3248 /*
3249  * Opposite of vfree() - mark a vnode as in use.
3250  */
3251 void
3252 vbusy(vp)
3253 	struct vnode *vp;
3254 {
3255 
3256 	ASSERT_VI_LOCKED(vp, "vbusy");
3257 	KASSERT((vp->v_iflag & VI_FREE) != 0, ("vnode not free"));
3258 
3259 	mtx_lock(&vnode_free_list_mtx);
3260 	TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
3261 	freevnodes--;
3262 	mtx_unlock(&vnode_free_list_mtx);
3263 
3264 	vp->v_iflag &= ~(VI_FREE|VI_AGE);
3265 }
3266 
3267 /*
3268  * Initalize per-vnode helper structure to hold poll-related state.
3269  */
3270 void
3271 v_addpollinfo(struct vnode *vp)
3272 {
3273 
3274 	vp->v_pollinfo = uma_zalloc(vnodepoll_zone, M_WAITOK);
3275 	mtx_init(&vp->v_pollinfo->vpi_lock, "vnode pollinfo", NULL, MTX_DEF);
3276 }
3277 
3278 /*
3279  * Record a process's interest in events which might happen to
3280  * a vnode.  Because poll uses the historic select-style interface
3281  * internally, this routine serves as both the ``check for any
3282  * pending events'' and the ``record my interest in future events''
3283  * functions.  (These are done together, while the lock is held,
3284  * to avoid race conditions.)
3285  */
3286 int
3287 vn_pollrecord(vp, td, events)
3288 	struct vnode *vp;
3289 	struct thread *td;
3290 	short events;
3291 {
3292 
3293 	if (vp->v_pollinfo == NULL)
3294 		v_addpollinfo(vp);
3295 	mtx_lock(&vp->v_pollinfo->vpi_lock);
3296 	if (vp->v_pollinfo->vpi_revents & events) {
3297 		/*
3298 		 * This leaves events we are not interested
3299 		 * in available for the other process which
3300 		 * which presumably had requested them
3301 		 * (otherwise they would never have been
3302 		 * recorded).
3303 		 */
3304 		events &= vp->v_pollinfo->vpi_revents;
3305 		vp->v_pollinfo->vpi_revents &= ~events;
3306 
3307 		mtx_unlock(&vp->v_pollinfo->vpi_lock);
3308 		return events;
3309 	}
3310 	vp->v_pollinfo->vpi_events |= events;
3311 	selrecord(td, &vp->v_pollinfo->vpi_selinfo);
3312 	mtx_unlock(&vp->v_pollinfo->vpi_lock);
3313 	return 0;
3314 }
3315 
3316 /*
3317  * Note the occurrence of an event.  If the VN_POLLEVENT macro is used,
3318  * it is possible for us to miss an event due to race conditions, but
3319  * that condition is expected to be rare, so for the moment it is the
3320  * preferred interface.
3321  */
3322 void
3323 vn_pollevent(vp, events)
3324 	struct vnode *vp;
3325 	short events;
3326 {
3327 
3328 	if (vp->v_pollinfo == NULL)
3329 		v_addpollinfo(vp);
3330 	mtx_lock(&vp->v_pollinfo->vpi_lock);
3331 	if (vp->v_pollinfo->vpi_events & events) {
3332 		/*
3333 		 * We clear vpi_events so that we don't
3334 		 * call selwakeup() twice if two events are
3335 		 * posted before the polling process(es) is
3336 		 * awakened.  This also ensures that we take at
3337 		 * most one selwakeup() if the polling process
3338 		 * is no longer interested.  However, it does
3339 		 * mean that only one event can be noticed at
3340 		 * a time.  (Perhaps we should only clear those
3341 		 * event bits which we note?) XXX
3342 		 */
3343 		vp->v_pollinfo->vpi_events = 0;	/* &= ~events ??? */
3344 		vp->v_pollinfo->vpi_revents |= events;
3345 		selwakeuppri(&vp->v_pollinfo->vpi_selinfo, PRIBIO);
3346 	}
3347 	mtx_unlock(&vp->v_pollinfo->vpi_lock);
3348 }
3349 
3350 /*
3351  * Wake up anyone polling on vp because it is being revoked.
3352  * This depends on dead_poll() returning POLLHUP for correct
3353  * behavior.
3354  */
3355 void
3356 vn_pollgone(vp)
3357 	struct vnode *vp;
3358 {
3359 
3360 	mtx_lock(&vp->v_pollinfo->vpi_lock);
3361 	VN_KNOTE(vp, NOTE_REVOKE);
3362 	if (vp->v_pollinfo->vpi_events) {
3363 		vp->v_pollinfo->vpi_events = 0;
3364 		selwakeuppri(&vp->v_pollinfo->vpi_selinfo, PRIBIO);
3365 	}
3366 	mtx_unlock(&vp->v_pollinfo->vpi_lock);
3367 }
3368 
3369 
3370 
3371 /*
3372  * Routine to create and manage a filesystem syncer vnode.
3373  */
3374 #define sync_close ((int (*)(struct  vop_close_args *))nullop)
3375 static int	sync_fsync(struct  vop_fsync_args *);
3376 static int	sync_inactive(struct  vop_inactive_args *);
3377 static int	sync_reclaim(struct  vop_reclaim_args *);
3378 
3379 static vop_t **sync_vnodeop_p;
3380 static struct vnodeopv_entry_desc sync_vnodeop_entries[] = {
3381 	{ &vop_default_desc,	(vop_t *) vop_eopnotsupp },
3382 	{ &vop_close_desc,	(vop_t *) sync_close },		/* close */
3383 	{ &vop_fsync_desc,	(vop_t *) sync_fsync },		/* fsync */
3384 	{ &vop_inactive_desc,	(vop_t *) sync_inactive },	/* inactive */
3385 	{ &vop_reclaim_desc,	(vop_t *) sync_reclaim },	/* reclaim */
3386 	{ &vop_lock_desc,	(vop_t *) vop_stdlock },	/* lock */
3387 	{ &vop_unlock_desc,	(vop_t *) vop_stdunlock },	/* unlock */
3388 	{ &vop_islocked_desc,	(vop_t *) vop_stdislocked },	/* islocked */
3389 	{ NULL, NULL }
3390 };
3391 static struct vnodeopv_desc sync_vnodeop_opv_desc =
3392 	{ &sync_vnodeop_p, sync_vnodeop_entries };
3393 
3394 VNODEOP_SET(sync_vnodeop_opv_desc);
3395 
3396 /*
3397  * Create a new filesystem syncer vnode for the specified mount point.
3398  */
3399 int
3400 vfs_allocate_syncvnode(mp)
3401 	struct mount *mp;
3402 {
3403 	struct vnode *vp;
3404 	static long start, incr, next;
3405 	int error;
3406 
3407 	/* Allocate a new vnode */
3408 	if ((error = getnewvnode("syncer", mp, sync_vnodeop_p, &vp)) != 0) {
3409 		mp->mnt_syncer = NULL;
3410 		return (error);
3411 	}
3412 	vp->v_type = VNON;
3413 	/*
3414 	 * Place the vnode onto the syncer worklist. We attempt to
3415 	 * scatter them about on the list so that they will go off
3416 	 * at evenly distributed times even if all the filesystems
3417 	 * are mounted at once.
3418 	 */
3419 	next += incr;
3420 	if (next == 0 || next > syncer_maxdelay) {
3421 		start /= 2;
3422 		incr /= 2;
3423 		if (start == 0) {
3424 			start = syncer_maxdelay / 2;
3425 			incr = syncer_maxdelay;
3426 		}
3427 		next = start;
3428 	}
3429 	VI_LOCK(vp);
3430 	vn_syncer_add_to_worklist(vp, syncdelay > 0 ? next % syncdelay : 0);
3431 	/* XXX - vn_syncer_add_to_worklist() also grabs and drops sync_mtx. */
3432 	mtx_lock(&sync_mtx);
3433 	sync_vnode_count++;
3434 	mtx_unlock(&sync_mtx);
3435 	VI_UNLOCK(vp);
3436 	mp->mnt_syncer = vp;
3437 	return (0);
3438 }
3439 
3440 /*
3441  * Do a lazy sync of the filesystem.
3442  */
3443 static int
3444 sync_fsync(ap)
3445 	struct vop_fsync_args /* {
3446 		struct vnode *a_vp;
3447 		struct ucred *a_cred;
3448 		int a_waitfor;
3449 		struct thread *a_td;
3450 	} */ *ap;
3451 {
3452 	struct vnode *syncvp = ap->a_vp;
3453 	struct mount *mp = syncvp->v_mount;
3454 	struct thread *td = ap->a_td;
3455 	int error, asyncflag;
3456 
3457 	/*
3458 	 * We only need to do something if this is a lazy evaluation.
3459 	 */
3460 	if (ap->a_waitfor != MNT_LAZY)
3461 		return (0);
3462 
3463 	/*
3464 	 * Move ourselves to the back of the sync list.
3465 	 */
3466 	VI_LOCK(syncvp);
3467 	vn_syncer_add_to_worklist(syncvp, syncdelay);
3468 	VI_UNLOCK(syncvp);
3469 
3470 	/*
3471 	 * Walk the list of vnodes pushing all that are dirty and
3472 	 * not already on the sync list.
3473 	 */
3474 	mtx_lock(&mountlist_mtx);
3475 	if (vfs_busy(mp, LK_EXCLUSIVE | LK_NOWAIT, &mountlist_mtx, td) != 0) {
3476 		mtx_unlock(&mountlist_mtx);
3477 		return (0);
3478 	}
3479 	if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) {
3480 		vfs_unbusy(mp, td);
3481 		return (0);
3482 	}
3483 	asyncflag = mp->mnt_flag & MNT_ASYNC;
3484 	mp->mnt_flag &= ~MNT_ASYNC;
3485 	vfs_msync(mp, MNT_NOWAIT);
3486 	error = VFS_SYNC(mp, MNT_LAZY, ap->a_cred, td);
3487 	if (asyncflag)
3488 		mp->mnt_flag |= MNT_ASYNC;
3489 	vn_finished_write(mp);
3490 	vfs_unbusy(mp, td);
3491 	return (error);
3492 }
3493 
3494 /*
3495  * The syncer vnode is no referenced.
3496  */
3497 static int
3498 sync_inactive(ap)
3499 	struct vop_inactive_args /* {
3500 		struct vnode *a_vp;
3501 		struct thread *a_td;
3502 	} */ *ap;
3503 {
3504 
3505 	VOP_UNLOCK(ap->a_vp, 0, ap->a_td);
3506 	vgone(ap->a_vp);
3507 	return (0);
3508 }
3509 
3510 /*
3511  * The syncer vnode is no longer needed and is being decommissioned.
3512  *
3513  * Modifications to the worklist must be protected by sync_mtx.
3514  */
3515 static int
3516 sync_reclaim(ap)
3517 	struct vop_reclaim_args /* {
3518 		struct vnode *a_vp;
3519 	} */ *ap;
3520 {
3521 	struct vnode *vp = ap->a_vp;
3522 
3523 	VI_LOCK(vp);
3524 	vp->v_mount->mnt_syncer = NULL;
3525 	if (vp->v_iflag & VI_ONWORKLST) {
3526 		mtx_lock(&sync_mtx);
3527 		LIST_REMOVE(vp, v_synclist);
3528  		syncer_worklist_len--;
3529 		sync_vnode_count--;
3530 		mtx_unlock(&sync_mtx);
3531 		vp->v_iflag &= ~VI_ONWORKLST;
3532 	}
3533 	VI_UNLOCK(vp);
3534 
3535 	return (0);
3536 }
3537 
3538 /*
3539  * extract the struct cdev *from a VCHR
3540  */
3541 struct cdev *
3542 vn_todev(vp)
3543 	struct vnode *vp;
3544 {
3545 
3546 	if (vp->v_type != VCHR)
3547 		return (NULL);
3548 	return (vp->v_rdev);
3549 }
3550 
3551 /*
3552  * Check if vnode represents a disk device
3553  */
3554 int
3555 vn_isdisk(vp, errp)
3556 	struct vnode *vp;
3557 	int *errp;
3558 {
3559 	int error;
3560 
3561 	error = 0;
3562 	if (vp->v_type != VCHR)
3563 		error = ENOTBLK;
3564 	else if (vp->v_rdev == NULL)
3565 		error = ENXIO;
3566 	else if (!(devsw(vp->v_rdev)->d_flags & D_DISK))
3567 		error = ENOTBLK;
3568 	if (errp != NULL)
3569 		*errp = error;
3570 	return (error == 0);
3571 }
3572 
3573 /*
3574  * Free data allocated by namei(); see namei(9) for details.
3575  */
3576 void
3577 NDFREE(ndp, flags)
3578      struct nameidata *ndp;
3579      const u_int flags;
3580 {
3581 
3582 	if (!(flags & NDF_NO_FREE_PNBUF) &&
3583 	    (ndp->ni_cnd.cn_flags & HASBUF)) {
3584 		uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
3585 		ndp->ni_cnd.cn_flags &= ~HASBUF;
3586 	}
3587 	if (!(flags & NDF_NO_DVP_UNLOCK) &&
3588 	    (ndp->ni_cnd.cn_flags & LOCKPARENT) &&
3589 	    ndp->ni_dvp != ndp->ni_vp)
3590 		VOP_UNLOCK(ndp->ni_dvp, 0, ndp->ni_cnd.cn_thread);
3591 	if (!(flags & NDF_NO_DVP_RELE) &&
3592 	    (ndp->ni_cnd.cn_flags & (LOCKPARENT|WANTPARENT))) {
3593 		vrele(ndp->ni_dvp);
3594 		ndp->ni_dvp = NULL;
3595 	}
3596 	if (!(flags & NDF_NO_VP_UNLOCK) &&
3597 	    (ndp->ni_cnd.cn_flags & LOCKLEAF) && ndp->ni_vp)
3598 		VOP_UNLOCK(ndp->ni_vp, 0, ndp->ni_cnd.cn_thread);
3599 	if (!(flags & NDF_NO_VP_RELE) &&
3600 	    ndp->ni_vp) {
3601 		vrele(ndp->ni_vp);
3602 		ndp->ni_vp = NULL;
3603 	}
3604 	if (!(flags & NDF_NO_STARTDIR_RELE) &&
3605 	    (ndp->ni_cnd.cn_flags & SAVESTART)) {
3606 		vrele(ndp->ni_startdir);
3607 		ndp->ni_startdir = NULL;
3608 	}
3609 }
3610 
3611 /*
3612  * Common filesystem object access control check routine.  Accepts a
3613  * vnode's type, "mode", uid and gid, requested access mode, credentials,
3614  * and optional call-by-reference privused argument allowing vaccess()
3615  * to indicate to the caller whether privilege was used to satisfy the
3616  * request (obsoleted).  Returns 0 on success, or an errno on failure.
3617  */
3618 int
3619 vaccess(type, file_mode, file_uid, file_gid, acc_mode, cred, privused)
3620 	enum vtype type;
3621 	mode_t file_mode;
3622 	uid_t file_uid;
3623 	gid_t file_gid;
3624 	mode_t acc_mode;
3625 	struct ucred *cred;
3626 	int *privused;
3627 {
3628 	mode_t dac_granted;
3629 #ifdef CAPABILITIES
3630 	mode_t cap_granted;
3631 #endif
3632 
3633 	/*
3634 	 * Look for a normal, non-privileged way to access the file/directory
3635 	 * as requested.  If it exists, go with that.
3636 	 */
3637 
3638 	if (privused != NULL)
3639 		*privused = 0;
3640 
3641 	dac_granted = 0;
3642 
3643 	/* Check the owner. */
3644 	if (cred->cr_uid == file_uid) {
3645 		dac_granted |= VADMIN;
3646 		if (file_mode & S_IXUSR)
3647 			dac_granted |= VEXEC;
3648 		if (file_mode & S_IRUSR)
3649 			dac_granted |= VREAD;
3650 		if (file_mode & S_IWUSR)
3651 			dac_granted |= (VWRITE | VAPPEND);
3652 
3653 		if ((acc_mode & dac_granted) == acc_mode)
3654 			return (0);
3655 
3656 		goto privcheck;
3657 	}
3658 
3659 	/* Otherwise, check the groups (first match) */
3660 	if (groupmember(file_gid, cred)) {
3661 		if (file_mode & S_IXGRP)
3662 			dac_granted |= VEXEC;
3663 		if (file_mode & S_IRGRP)
3664 			dac_granted |= VREAD;
3665 		if (file_mode & S_IWGRP)
3666 			dac_granted |= (VWRITE | VAPPEND);
3667 
3668 		if ((acc_mode & dac_granted) == acc_mode)
3669 			return (0);
3670 
3671 		goto privcheck;
3672 	}
3673 
3674 	/* Otherwise, check everyone else. */
3675 	if (file_mode & S_IXOTH)
3676 		dac_granted |= VEXEC;
3677 	if (file_mode & S_IROTH)
3678 		dac_granted |= VREAD;
3679 	if (file_mode & S_IWOTH)
3680 		dac_granted |= (VWRITE | VAPPEND);
3681 	if ((acc_mode & dac_granted) == acc_mode)
3682 		return (0);
3683 
3684 privcheck:
3685 	if (!suser_cred(cred, PRISON_ROOT)) {
3686 		/* XXX audit: privilege used */
3687 		if (privused != NULL)
3688 			*privused = 1;
3689 		return (0);
3690 	}
3691 
3692 #ifdef CAPABILITIES
3693 	/*
3694 	 * Build a capability mask to determine if the set of capabilities
3695 	 * satisfies the requirements when combined with the granted mask
3696 	 * from above.
3697 	 * For each capability, if the capability is required, bitwise
3698 	 * or the request type onto the cap_granted mask.
3699 	 */
3700 	cap_granted = 0;
3701 
3702 	if (type == VDIR) {
3703 		/*
3704 		 * For directories, use CAP_DAC_READ_SEARCH to satisfy
3705 		 * VEXEC requests, instead of CAP_DAC_EXECUTE.
3706 		 */
3707 		if ((acc_mode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
3708 		    !cap_check(cred, NULL, CAP_DAC_READ_SEARCH, PRISON_ROOT))
3709 			cap_granted |= VEXEC;
3710 	} else {
3711 		if ((acc_mode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
3712 		    !cap_check(cred, NULL, CAP_DAC_EXECUTE, PRISON_ROOT))
3713 			cap_granted |= VEXEC;
3714 	}
3715 
3716 	if ((acc_mode & VREAD) && ((dac_granted & VREAD) == 0) &&
3717 	    !cap_check(cred, NULL, CAP_DAC_READ_SEARCH, PRISON_ROOT))
3718 		cap_granted |= VREAD;
3719 
3720 	if ((acc_mode & VWRITE) && ((dac_granted & VWRITE) == 0) &&
3721 	    !cap_check(cred, NULL, CAP_DAC_WRITE, PRISON_ROOT))
3722 		cap_granted |= (VWRITE | VAPPEND);
3723 
3724 	if ((acc_mode & VADMIN) && ((dac_granted & VADMIN) == 0) &&
3725 	    !cap_check(cred, NULL, CAP_FOWNER, PRISON_ROOT))
3726 		cap_granted |= VADMIN;
3727 
3728 	if ((acc_mode & (cap_granted | dac_granted)) == acc_mode) {
3729 		/* XXX audit: privilege used */
3730 		if (privused != NULL)
3731 			*privused = 1;
3732 		return (0);
3733 	}
3734 #endif
3735 
3736 	return ((acc_mode & VADMIN) ? EPERM : EACCES);
3737 }
3738 
3739 /*
3740  * Credential check based on process requesting service, and per-attribute
3741  * permissions.
3742  */
3743 int
3744 extattr_check_cred(struct vnode *vp, int attrnamespace,
3745     struct ucred *cred, struct thread *td, int access)
3746 {
3747 
3748 	/*
3749 	 * Kernel-invoked always succeeds.
3750 	 */
3751 	if (cred == NOCRED)
3752 		return (0);
3753 
3754 	/*
3755 	 * Do not allow privileged processes in jail to directly
3756 	 * manipulate system attributes.
3757 	 *
3758 	 * XXX What capability should apply here?
3759 	 * Probably CAP_SYS_SETFFLAG.
3760 	 */
3761 	switch (attrnamespace) {
3762 	case EXTATTR_NAMESPACE_SYSTEM:
3763 		/* Potentially should be: return (EPERM); */
3764 		return (suser_cred(cred, 0));
3765 	case EXTATTR_NAMESPACE_USER:
3766 		return (VOP_ACCESS(vp, access, cred, td));
3767 	default:
3768 		return (EPERM);
3769 	}
3770 }
3771 
3772 #ifdef DEBUG_VFS_LOCKS
3773 /*
3774  * This only exists to supress warnings from unlocked specfs accesses.  It is
3775  * no longer ok to have an unlocked VFS.
3776  */
3777 #define	IGNORE_LOCK(vp) ((vp)->v_type == VCHR || (vp)->v_type == VBAD)
3778 
3779 int vfs_badlock_ddb = 1;	/* Drop into debugger on violation. */
3780 int vfs_badlock_mutex = 1;	/* Check for interlock across VOPs. */
3781 int vfs_badlock_print = 1;	/* Print lock violations. */
3782 
3783 static void
3784 vfs_badlock(const char *msg, const char *str, struct vnode *vp)
3785 {
3786 
3787 	if (vfs_badlock_print)
3788 		printf("%s: %p %s\n", str, (void *)vp, msg);
3789 	if (vfs_badlock_ddb)
3790 		kdb_enter("lock violation");
3791 }
3792 
3793 void
3794 assert_vi_locked(struct vnode *vp, const char *str)
3795 {
3796 
3797 	if (vfs_badlock_mutex && !mtx_owned(VI_MTX(vp)))
3798 		vfs_badlock("interlock is not locked but should be", str, vp);
3799 }
3800 
3801 void
3802 assert_vi_unlocked(struct vnode *vp, const char *str)
3803 {
3804 
3805 	if (vfs_badlock_mutex && mtx_owned(VI_MTX(vp)))
3806 		vfs_badlock("interlock is locked but should not be", str, vp);
3807 }
3808 
3809 void
3810 assert_vop_locked(struct vnode *vp, const char *str)
3811 {
3812 
3813 	if (vp && !IGNORE_LOCK(vp) && VOP_ISLOCKED(vp, NULL) == 0)
3814 		vfs_badlock("is not locked but should be", str, vp);
3815 }
3816 
3817 void
3818 assert_vop_unlocked(struct vnode *vp, const char *str)
3819 {
3820 
3821 	if (vp && !IGNORE_LOCK(vp) &&
3822 	    VOP_ISLOCKED(vp, curthread) == LK_EXCLUSIVE)
3823 		vfs_badlock("is locked but should not be", str, vp);
3824 }
3825 
3826 #if 0
3827 void
3828 assert_vop_elocked(struct vnode *vp, const char *str)
3829 {
3830 
3831 	if (vp && !IGNORE_LOCK(vp) &&
3832 	    VOP_ISLOCKED(vp, curthread) != LK_EXCLUSIVE)
3833 		vfs_badlock("is not exclusive locked but should be", str, vp);
3834 }
3835 
3836 void
3837 assert_vop_elocked_other(struct vnode *vp, const char *str)
3838 {
3839 
3840 	if (vp && !IGNORE_LOCK(vp) &&
3841 	    VOP_ISLOCKED(vp, curthread) != LK_EXCLOTHER)
3842 		vfs_badlock("is not exclusive locked by another thread",
3843 		    str, vp);
3844 }
3845 
3846 void
3847 assert_vop_slocked(struct vnode *vp, const char *str)
3848 {
3849 
3850 	if (vp && !IGNORE_LOCK(vp) &&
3851 	    VOP_ISLOCKED(vp, curthread) != LK_SHARED)
3852 		vfs_badlock("is not locked shared but should be", str, vp);
3853 }
3854 #endif /* 0 */
3855 
3856 void
3857 vop_rename_pre(void *ap)
3858 {
3859 	struct vop_rename_args *a = ap;
3860 
3861 	if (a->a_tvp)
3862 		ASSERT_VI_UNLOCKED(a->a_tvp, "VOP_RENAME");
3863 	ASSERT_VI_UNLOCKED(a->a_tdvp, "VOP_RENAME");
3864 	ASSERT_VI_UNLOCKED(a->a_fvp, "VOP_RENAME");
3865 	ASSERT_VI_UNLOCKED(a->a_fdvp, "VOP_RENAME");
3866 
3867 	/* Check the source (from). */
3868 	if (a->a_tdvp != a->a_fdvp)
3869 		ASSERT_VOP_UNLOCKED(a->a_fdvp, "vop_rename: fdvp locked");
3870 	if (a->a_tvp != a->a_fvp)
3871 		ASSERT_VOP_UNLOCKED(a->a_fvp, "vop_rename: tvp locked");
3872 
3873 	/* Check the target. */
3874 	if (a->a_tvp)
3875 		ASSERT_VOP_LOCKED(a->a_tvp, "vop_rename: tvp not locked");
3876 	ASSERT_VOP_LOCKED(a->a_tdvp, "vop_rename: tdvp not locked");
3877 }
3878 
3879 void
3880 vop_strategy_pre(void *ap)
3881 {
3882 	struct vop_strategy_args *a;
3883 	struct buf *bp;
3884 
3885 	a = ap;
3886 	bp = a->a_bp;
3887 
3888 	/*
3889 	 * Cluster ops lock their component buffers but not the IO container.
3890 	 */
3891 	if ((bp->b_flags & B_CLUSTER) != 0)
3892 		return;
3893 
3894 	if (BUF_REFCNT(bp) < 1) {
3895 		if (vfs_badlock_print)
3896 			printf(
3897 			    "VOP_STRATEGY: bp is not locked but should be\n");
3898 		if (vfs_badlock_ddb)
3899 			kdb_enter("lock violation");
3900 	}
3901 }
3902 
3903 void
3904 vop_lookup_pre(void *ap)
3905 {
3906 	struct vop_lookup_args *a;
3907 	struct vnode *dvp;
3908 
3909 	a = ap;
3910 	dvp = a->a_dvp;
3911 	ASSERT_VI_UNLOCKED(dvp, "VOP_LOOKUP");
3912 	ASSERT_VOP_LOCKED(dvp, "VOP_LOOKUP");
3913 }
3914 
3915 void
3916 vop_lookup_post(void *ap, int rc)
3917 {
3918 	struct vop_lookup_args *a;
3919 	struct componentname *cnp;
3920 	struct vnode *dvp;
3921 	struct vnode *vp;
3922 	int flags;
3923 
3924 	a = ap;
3925 	dvp = a->a_dvp;
3926 	cnp = a->a_cnp;
3927 	vp = *(a->a_vpp);
3928 	flags = cnp->cn_flags;
3929 
3930 	ASSERT_VI_UNLOCKED(dvp, "VOP_LOOKUP");
3931 
3932 	/*
3933 	 * If this is the last path component for this lookup and LOCKPARENT
3934 	 * is set, OR if there is an error the directory has to be locked.
3935 	 */
3936 	if ((flags & LOCKPARENT) && (flags & ISLASTCN))
3937 		ASSERT_VOP_LOCKED(dvp, "VOP_LOOKUP (LOCKPARENT)");
3938 	else if (rc != 0)
3939 		ASSERT_VOP_LOCKED(dvp, "VOP_LOOKUP (error)");
3940 	else if (dvp != vp)
3941 		ASSERT_VOP_UNLOCKED(dvp, "VOP_LOOKUP (dvp)");
3942 	if (flags & PDIRUNLOCK)
3943 		ASSERT_VOP_UNLOCKED(dvp, "VOP_LOOKUP (PDIRUNLOCK)");
3944 }
3945 
3946 void
3947 vop_lock_pre(void *ap)
3948 {
3949 	struct vop_lock_args *a = ap;
3950 
3951 	if ((a->a_flags & LK_INTERLOCK) == 0)
3952 		ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
3953 	else
3954 		ASSERT_VI_LOCKED(a->a_vp, "VOP_LOCK");
3955 }
3956 
3957 void
3958 vop_lock_post(void *ap, int rc)
3959 {
3960 	struct vop_lock_args *a = ap;
3961 
3962 	ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
3963 	if (rc == 0)
3964 		ASSERT_VOP_LOCKED(a->a_vp, "VOP_LOCK");
3965 }
3966 
3967 void
3968 vop_unlock_pre(void *ap)
3969 {
3970 	struct vop_unlock_args *a = ap;
3971 
3972 	if (a->a_flags & LK_INTERLOCK)
3973 		ASSERT_VI_LOCKED(a->a_vp, "VOP_UNLOCK");
3974 	ASSERT_VOP_LOCKED(a->a_vp, "VOP_UNLOCK");
3975 }
3976 
3977 void
3978 vop_unlock_post(void *ap, int rc)
3979 {
3980 	struct vop_unlock_args *a = ap;
3981 
3982 	if (a->a_flags & LK_INTERLOCK)
3983 		ASSERT_VI_UNLOCKED(a->a_vp, "VOP_UNLOCK");
3984 }
3985 #endif /* DEBUG_VFS_LOCKS */
3986 
3987 static struct klist fs_klist = SLIST_HEAD_INITIALIZER(&fs_klist);
3988 
3989 void
3990 vfs_event_signal(fsid_t *fsid, u_int32_t event, intptr_t data __unused)
3991 {
3992 
3993 	KNOTE(&fs_klist, event);
3994 }
3995 
3996 static int	filt_fsattach(struct knote *kn);
3997 static void	filt_fsdetach(struct knote *kn);
3998 static int	filt_fsevent(struct knote *kn, long hint);
3999 
4000 struct filterops fs_filtops =
4001 	{ 0, filt_fsattach, filt_fsdetach, filt_fsevent };
4002 
4003 static int
4004 filt_fsattach(struct knote *kn)
4005 {
4006 
4007 	kn->kn_flags |= EV_CLEAR;
4008 	SLIST_INSERT_HEAD(&fs_klist, kn, kn_selnext);
4009 	return (0);
4010 }
4011 
4012 static void
4013 filt_fsdetach(struct knote *kn)
4014 {
4015 
4016 	SLIST_REMOVE(&fs_klist, kn, knote, kn_selnext);
4017 }
4018 
4019 static int
4020 filt_fsevent(struct knote *kn, long hint)
4021 {
4022 
4023 	kn->kn_fflags |= hint;
4024 	return (kn->kn_fflags != 0);
4025 }
4026 
4027 static int
4028 sysctl_vfs_ctl(SYSCTL_HANDLER_ARGS)
4029 {
4030 	struct vfsidctl vc;
4031 	int error;
4032 	struct mount *mp;
4033 
4034 	error = SYSCTL_IN(req, &vc, sizeof(vc));
4035 	if (error)
4036 		return (error);
4037 	if (vc.vc_vers != VFS_CTL_VERS1)
4038 		return (EINVAL);
4039 	mp = vfs_getvfs(&vc.vc_fsid);
4040 	if (mp == NULL)
4041 		return (ENOENT);
4042 	/* ensure that a specific sysctl goes to the right filesystem. */
4043 	if (strcmp(vc.vc_fstypename, "*") != 0 &&
4044 	    strcmp(vc.vc_fstypename, mp->mnt_vfc->vfc_name) != 0) {
4045 		return (EINVAL);
4046 	}
4047 	VCTLTOREQ(&vc, req);
4048 	return (VFS_SYSCTL(mp, vc.vc_op, req));
4049 }
4050 
4051 SYSCTL_PROC(_vfs, OID_AUTO, ctl, CTLFLAG_WR,
4052         NULL, 0, sysctl_vfs_ctl, "", "Sysctl by fsid");
4053