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