xref: /freebsd/sys/kern/vfs_subr.c (revision b78ee15e9f04ae15c3e1200df974473167524d17)
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_compat.h"
45 #include "opt_ddb.h"
46 #include "opt_watchdog.h"
47 
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/bio.h>
51 #include <sys/buf.h>
52 #include <sys/condvar.h>
53 #include <sys/conf.h>
54 #include <sys/dirent.h>
55 #include <sys/event.h>
56 #include <sys/eventhandler.h>
57 #include <sys/extattr.h>
58 #include <sys/file.h>
59 #include <sys/fcntl.h>
60 #include <sys/jail.h>
61 #include <sys/kdb.h>
62 #include <sys/kernel.h>
63 #include <sys/kthread.h>
64 #include <sys/lockf.h>
65 #include <sys/malloc.h>
66 #include <sys/mount.h>
67 #include <sys/namei.h>
68 #include <sys/pctrie.h>
69 #include <sys/priv.h>
70 #include <sys/reboot.h>
71 #include <sys/rwlock.h>
72 #include <sys/sched.h>
73 #include <sys/sleepqueue.h>
74 #include <sys/smp.h>
75 #include <sys/stat.h>
76 #include <sys/sysctl.h>
77 #include <sys/syslog.h>
78 #include <sys/vmmeter.h>
79 #include <sys/vnode.h>
80 #include <sys/watchdog.h>
81 
82 #include <machine/stdarg.h>
83 
84 #include <security/mac/mac_framework.h>
85 
86 #include <vm/vm.h>
87 #include <vm/vm_object.h>
88 #include <vm/vm_extern.h>
89 #include <vm/pmap.h>
90 #include <vm/vm_map.h>
91 #include <vm/vm_page.h>
92 #include <vm/vm_kern.h>
93 #include <vm/uma.h>
94 
95 #ifdef DDB
96 #include <ddb/ddb.h>
97 #endif
98 
99 static void	delmntque(struct vnode *vp);
100 static int	flushbuflist(struct bufv *bufv, int flags, struct bufobj *bo,
101 		    int slpflag, int slptimeo);
102 static void	syncer_shutdown(void *arg, int howto);
103 static int	vtryrecycle(struct vnode *vp);
104 static void	v_incr_usecount(struct vnode *);
105 static void	v_decr_usecount(struct vnode *);
106 static void	v_decr_useonly(struct vnode *);
107 static void	v_upgrade_usecount(struct vnode *);
108 static void	vnlru_free(int);
109 static void	vgonel(struct vnode *);
110 static void	vfs_knllock(void *arg);
111 static void	vfs_knlunlock(void *arg);
112 static void	vfs_knl_assert_locked(void *arg);
113 static void	vfs_knl_assert_unlocked(void *arg);
114 static void	destroy_vpollinfo(struct vpollinfo *vi);
115 
116 /*
117  * Number of vnodes in existence.  Increased whenever getnewvnode()
118  * allocates a new vnode, decreased in vdropl() for VI_DOOMED vnode.
119  */
120 static unsigned long	numvnodes;
121 
122 SYSCTL_ULONG(_vfs, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0,
123     "Number of vnodes in existence");
124 
125 static u_long vnodes_created;
126 SYSCTL_ULONG(_vfs, OID_AUTO, vnodes_created, CTLFLAG_RD, &vnodes_created,
127     0, "Number of vnodes created by getnewvnode");
128 
129 /*
130  * Conversion tables for conversion from vnode types to inode formats
131  * and back.
132  */
133 enum vtype iftovt_tab[16] = {
134 	VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
135 	VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
136 };
137 int vttoif_tab[10] = {
138 	0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
139 	S_IFSOCK, S_IFIFO, S_IFMT, S_IFMT
140 };
141 
142 /*
143  * List of vnodes that are ready for recycling.
144  */
145 static TAILQ_HEAD(freelst, vnode) vnode_free_list;
146 
147 /*
148  * Free vnode target.  Free vnodes may simply be files which have been stat'd
149  * but not read.  This is somewhat common, and a small cache of such files
150  * should be kept to avoid recreation costs.
151  */
152 static u_long wantfreevnodes;
153 SYSCTL_ULONG(_vfs, OID_AUTO, wantfreevnodes, CTLFLAG_RW, &wantfreevnodes, 0, "");
154 /* Number of vnodes in the free list. */
155 static u_long freevnodes;
156 SYSCTL_ULONG(_vfs, OID_AUTO, freevnodes, CTLFLAG_RD, &freevnodes, 0,
157     "Number of vnodes in the free list");
158 
159 static int vlru_allow_cache_src;
160 SYSCTL_INT(_vfs, OID_AUTO, vlru_allow_cache_src, CTLFLAG_RW,
161     &vlru_allow_cache_src, 0, "Allow vlru to reclaim source vnode");
162 
163 static u_long recycles_count;
164 SYSCTL_ULONG(_vfs, OID_AUTO, recycles, CTLFLAG_RD, &recycles_count, 0,
165     "Number of vnodes recycled to avoid exceding kern.maxvnodes");
166 
167 /*
168  * Various variables used for debugging the new implementation of
169  * reassignbuf().
170  * XXX these are probably of (very) limited utility now.
171  */
172 static int reassignbufcalls;
173 SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW, &reassignbufcalls, 0,
174     "Number of calls to reassignbuf");
175 
176 static u_long free_owe_inact;
177 SYSCTL_ULONG(_vfs, OID_AUTO, free_owe_inact, CTLFLAG_RD, &free_owe_inact, 0,
178     "Number of times free vnodes kept on active list due to VFS "
179     "owing inactivation");
180 
181 /*
182  * Cache for the mount type id assigned to NFS.  This is used for
183  * special checks in nfs/nfs_nqlease.c and vm/vnode_pager.c.
184  */
185 int	nfs_mount_type = -1;
186 
187 /* To keep more than one thread at a time from running vfs_getnewfsid */
188 static struct mtx mntid_mtx;
189 
190 /*
191  * Lock for any access to the following:
192  *	vnode_free_list
193  *	numvnodes
194  *	freevnodes
195  */
196 static struct mtx vnode_free_list_mtx;
197 
198 /* Publicly exported FS */
199 struct nfs_public nfs_pub;
200 
201 static uma_zone_t buf_trie_zone;
202 
203 /* Zone for allocation of new vnodes - used exclusively by getnewvnode() */
204 static uma_zone_t vnode_zone;
205 static uma_zone_t vnodepoll_zone;
206 
207 /*
208  * The workitem queue.
209  *
210  * It is useful to delay writes of file data and filesystem metadata
211  * for tens of seconds so that quickly created and deleted files need
212  * not waste disk bandwidth being created and removed. To realize this,
213  * we append vnodes to a "workitem" queue. When running with a soft
214  * updates implementation, most pending metadata dependencies should
215  * not wait for more than a few seconds. Thus, mounted on block devices
216  * are delayed only about a half the time that file data is delayed.
217  * Similarly, directory updates are more critical, so are only delayed
218  * about a third the time that file data is delayed. Thus, there are
219  * SYNCER_MAXDELAY queues that are processed round-robin at a rate of
220  * one each second (driven off the filesystem syncer process). The
221  * syncer_delayno variable indicates the next queue that is to be processed.
222  * Items that need to be processed soon are placed in this queue:
223  *
224  *	syncer_workitem_pending[syncer_delayno]
225  *
226  * A delay of fifteen seconds is done by placing the request fifteen
227  * entries later in the queue:
228  *
229  *	syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask]
230  *
231  */
232 static int syncer_delayno;
233 static long syncer_mask;
234 LIST_HEAD(synclist, bufobj);
235 static struct synclist *syncer_workitem_pending;
236 /*
237  * The sync_mtx protects:
238  *	bo->bo_synclist
239  *	sync_vnode_count
240  *	syncer_delayno
241  *	syncer_state
242  *	syncer_workitem_pending
243  *	syncer_worklist_len
244  *	rushjob
245  */
246 static struct mtx sync_mtx;
247 static struct cv sync_wakeup;
248 
249 #define SYNCER_MAXDELAY		32
250 static int syncer_maxdelay = SYNCER_MAXDELAY;	/* maximum delay time */
251 static int syncdelay = 30;		/* max time to delay syncing data */
252 static int filedelay = 30;		/* time to delay syncing files */
253 SYSCTL_INT(_kern, OID_AUTO, filedelay, CTLFLAG_RW, &filedelay, 0,
254     "Time to delay syncing files (in seconds)");
255 static int dirdelay = 29;		/* time to delay syncing directories */
256 SYSCTL_INT(_kern, OID_AUTO, dirdelay, CTLFLAG_RW, &dirdelay, 0,
257     "Time to delay syncing directories (in seconds)");
258 static int metadelay = 28;		/* time to delay syncing metadata */
259 SYSCTL_INT(_kern, OID_AUTO, metadelay, CTLFLAG_RW, &metadelay, 0,
260     "Time to delay syncing metadata (in seconds)");
261 static int rushjob;		/* number of slots to run ASAP */
262 static int stat_rush_requests;	/* number of times I/O speeded up */
263 SYSCTL_INT(_debug, OID_AUTO, rush_requests, CTLFLAG_RW, &stat_rush_requests, 0,
264     "Number of times I/O speeded up (rush requests)");
265 
266 /*
267  * When shutting down the syncer, run it at four times normal speed.
268  */
269 #define SYNCER_SHUTDOWN_SPEEDUP		4
270 static int sync_vnode_count;
271 static int syncer_worklist_len;
272 static enum { SYNCER_RUNNING, SYNCER_SHUTTING_DOWN, SYNCER_FINAL_DELAY }
273     syncer_state;
274 
275 /*
276  * Number of vnodes we want to exist at any one time.  This is mostly used
277  * to size hash tables in vnode-related code.  It is normally not used in
278  * getnewvnode(), as wantfreevnodes is normally nonzero.)
279  *
280  * XXX desiredvnodes is historical cruft and should not exist.
281  */
282 int desiredvnodes;
283 SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RW,
284     &desiredvnodes, 0, "Maximum number of vnodes");
285 SYSCTL_ULONG(_kern, OID_AUTO, minvnodes, CTLFLAG_RW,
286     &wantfreevnodes, 0, "Minimum number of vnodes (legacy)");
287 static int vnlru_nowhere;
288 SYSCTL_INT(_debug, OID_AUTO, vnlru_nowhere, CTLFLAG_RW,
289     &vnlru_nowhere, 0, "Number of times the vnlru process ran without success");
290 
291 /* Shift count for (uintptr_t)vp to initialize vp->v_hash. */
292 static int vnsz2log;
293 
294 /*
295  * Support for the bufobj clean & dirty pctrie.
296  */
297 static void *
298 buf_trie_alloc(struct pctrie *ptree)
299 {
300 
301 	return uma_zalloc(buf_trie_zone, M_NOWAIT);
302 }
303 
304 static void
305 buf_trie_free(struct pctrie *ptree, void *node)
306 {
307 
308 	uma_zfree(buf_trie_zone, node);
309 }
310 PCTRIE_DEFINE(BUF, buf, b_lblkno, buf_trie_alloc, buf_trie_free);
311 
312 /*
313  * Initialize the vnode management data structures.
314  *
315  * Reevaluate the following cap on the number of vnodes after the physical
316  * memory size exceeds 512GB.  In the limit, as the physical memory size
317  * grows, the ratio of physical pages to vnodes approaches sixteen to one.
318  */
319 #ifndef	MAXVNODES_MAX
320 #define	MAXVNODES_MAX	(512 * (1024 * 1024 * 1024 / (int)PAGE_SIZE / 16))
321 #endif
322 static void
323 vntblinit(void *dummy __unused)
324 {
325 	u_int i;
326 	int physvnodes, virtvnodes;
327 
328 	/*
329 	 * Desiredvnodes is a function of the physical memory size and the
330 	 * kernel's heap size.  Generally speaking, it scales with the
331 	 * physical memory size.  The ratio of desiredvnodes to physical pages
332 	 * is one to four until desiredvnodes exceeds 98,304.  Thereafter, the
333 	 * marginal ratio of desiredvnodes to physical pages is one to
334 	 * sixteen.  However, desiredvnodes is limited by the kernel's heap
335 	 * size.  The memory required by desiredvnodes vnodes and vm objects
336 	 * may not exceed one seventh of the kernel's heap size.
337 	 */
338 	physvnodes = maxproc + vm_cnt.v_page_count / 16 + 3 * min(98304 * 4,
339 	    vm_cnt.v_page_count) / 16;
340 	virtvnodes = vm_kmem_size / (7 * (sizeof(struct vm_object) +
341 	    sizeof(struct vnode)));
342 	desiredvnodes = min(physvnodes, virtvnodes);
343 	if (desiredvnodes > MAXVNODES_MAX) {
344 		if (bootverbose)
345 			printf("Reducing kern.maxvnodes %d -> %d\n",
346 			    desiredvnodes, MAXVNODES_MAX);
347 		desiredvnodes = MAXVNODES_MAX;
348 	}
349 	wantfreevnodes = desiredvnodes / 4;
350 	mtx_init(&mntid_mtx, "mntid", NULL, MTX_DEF);
351 	TAILQ_INIT(&vnode_free_list);
352 	mtx_init(&vnode_free_list_mtx, "vnode_free_list", NULL, MTX_DEF);
353 	vnode_zone = uma_zcreate("VNODE", sizeof (struct vnode), NULL, NULL,
354 	    NULL, NULL, UMA_ALIGN_PTR, 0);
355 	vnodepoll_zone = uma_zcreate("VNODEPOLL", sizeof (struct vpollinfo),
356 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
357 	/*
358 	 * Preallocate enough nodes to support one-per buf so that
359 	 * we can not fail an insert.  reassignbuf() callers can not
360 	 * tolerate the insertion failure.
361 	 */
362 	buf_trie_zone = uma_zcreate("BUF TRIE", pctrie_node_size(),
363 	    NULL, NULL, pctrie_zone_init, NULL, UMA_ALIGN_PTR,
364 	    UMA_ZONE_NOFREE | UMA_ZONE_VM);
365 	uma_prealloc(buf_trie_zone, nbuf);
366 	/*
367 	 * Initialize the filesystem syncer.
368 	 */
369 	syncer_workitem_pending = hashinit(syncer_maxdelay, M_VNODE,
370 	    &syncer_mask);
371 	syncer_maxdelay = syncer_mask + 1;
372 	mtx_init(&sync_mtx, "Syncer mtx", NULL, MTX_DEF);
373 	cv_init(&sync_wakeup, "syncer");
374 	for (i = 1; i <= sizeof(struct vnode); i <<= 1)
375 		vnsz2log++;
376 	vnsz2log--;
377 }
378 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_FIRST, vntblinit, NULL);
379 
380 
381 /*
382  * Mark a mount point as busy. Used to synchronize access and to delay
383  * unmounting. Eventually, mountlist_mtx is not released on failure.
384  *
385  * vfs_busy() is a custom lock, it can block the caller.
386  * vfs_busy() only sleeps if the unmount is active on the mount point.
387  * For a mountpoint mp, vfs_busy-enforced lock is before lock of any
388  * vnode belonging to mp.
389  *
390  * Lookup uses vfs_busy() to traverse mount points.
391  * root fs			var fs
392  * / vnode lock		A	/ vnode lock (/var)		D
393  * /var vnode lock	B	/log vnode lock(/var/log)	E
394  * vfs_busy lock	C	vfs_busy lock			F
395  *
396  * Within each file system, the lock order is C->A->B and F->D->E.
397  *
398  * When traversing across mounts, the system follows that lock order:
399  *
400  *        C->A->B
401  *              |
402  *              +->F->D->E
403  *
404  * The lookup() process for namei("/var") illustrates the process:
405  *  VOP_LOOKUP() obtains B while A is held
406  *  vfs_busy() obtains a shared lock on F while A and B are held
407  *  vput() releases lock on B
408  *  vput() releases lock on A
409  *  VFS_ROOT() obtains lock on D while shared lock on F is held
410  *  vfs_unbusy() releases shared lock on F
411  *  vn_lock() obtains lock on deadfs vnode vp_crossmp instead of A.
412  *    Attempt to lock A (instead of vp_crossmp) while D is held would
413  *    violate the global order, causing deadlocks.
414  *
415  * dounmount() locks B while F is drained.
416  */
417 int
418 vfs_busy(struct mount *mp, int flags)
419 {
420 
421 	MPASS((flags & ~MBF_MASK) == 0);
422 	CTR3(KTR_VFS, "%s: mp %p with flags %d", __func__, mp, flags);
423 
424 	MNT_ILOCK(mp);
425 	MNT_REF(mp);
426 	/*
427 	 * If mount point is currenly being unmounted, sleep until the
428 	 * mount point fate is decided.  If thread doing the unmounting fails,
429 	 * it will clear MNTK_UNMOUNT flag before waking us up, indicating
430 	 * that this mount point has survived the unmount attempt and vfs_busy
431 	 * should retry.  Otherwise the unmounter thread will set MNTK_REFEXPIRE
432 	 * flag in addition to MNTK_UNMOUNT, indicating that mount point is
433 	 * about to be really destroyed.  vfs_busy needs to release its
434 	 * reference on the mount point in this case and return with ENOENT,
435 	 * telling the caller that mount mount it tried to busy is no longer
436 	 * valid.
437 	 */
438 	while (mp->mnt_kern_flag & MNTK_UNMOUNT) {
439 		if (flags & MBF_NOWAIT || mp->mnt_kern_flag & MNTK_REFEXPIRE) {
440 			MNT_REL(mp);
441 			MNT_IUNLOCK(mp);
442 			CTR1(KTR_VFS, "%s: failed busying before sleeping",
443 			    __func__);
444 			return (ENOENT);
445 		}
446 		if (flags & MBF_MNTLSTLOCK)
447 			mtx_unlock(&mountlist_mtx);
448 		mp->mnt_kern_flag |= MNTK_MWAIT;
449 		msleep(mp, MNT_MTX(mp), PVFS | PDROP, "vfs_busy", 0);
450 		if (flags & MBF_MNTLSTLOCK)
451 			mtx_lock(&mountlist_mtx);
452 		MNT_ILOCK(mp);
453 	}
454 	if (flags & MBF_MNTLSTLOCK)
455 		mtx_unlock(&mountlist_mtx);
456 	mp->mnt_lockref++;
457 	MNT_IUNLOCK(mp);
458 	return (0);
459 }
460 
461 /*
462  * Free a busy filesystem.
463  */
464 void
465 vfs_unbusy(struct mount *mp)
466 {
467 
468 	CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
469 	MNT_ILOCK(mp);
470 	MNT_REL(mp);
471 	KASSERT(mp->mnt_lockref > 0, ("negative mnt_lockref"));
472 	mp->mnt_lockref--;
473 	if (mp->mnt_lockref == 0 && (mp->mnt_kern_flag & MNTK_DRAINING) != 0) {
474 		MPASS(mp->mnt_kern_flag & MNTK_UNMOUNT);
475 		CTR1(KTR_VFS, "%s: waking up waiters", __func__);
476 		mp->mnt_kern_flag &= ~MNTK_DRAINING;
477 		wakeup(&mp->mnt_lockref);
478 	}
479 	MNT_IUNLOCK(mp);
480 }
481 
482 /*
483  * Lookup a mount point by filesystem identifier.
484  */
485 struct mount *
486 vfs_getvfs(fsid_t *fsid)
487 {
488 	struct mount *mp;
489 
490 	CTR2(KTR_VFS, "%s: fsid %p", __func__, fsid);
491 	mtx_lock(&mountlist_mtx);
492 	TAILQ_FOREACH(mp, &mountlist, mnt_list) {
493 		if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
494 		    mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
495 			vfs_ref(mp);
496 			mtx_unlock(&mountlist_mtx);
497 			return (mp);
498 		}
499 	}
500 	mtx_unlock(&mountlist_mtx);
501 	CTR2(KTR_VFS, "%s: lookup failed for %p id", __func__, fsid);
502 	return ((struct mount *) 0);
503 }
504 
505 /*
506  * Lookup a mount point by filesystem identifier, busying it before
507  * returning.
508  *
509  * To avoid congestion on mountlist_mtx, implement simple direct-mapped
510  * cache for popular filesystem identifiers.  The cache is lockess, using
511  * the fact that struct mount's are never freed.  In worst case we may
512  * get pointer to unmounted or even different filesystem, so we have to
513  * check what we got, and go slow way if so.
514  */
515 struct mount *
516 vfs_busyfs(fsid_t *fsid)
517 {
518 #define	FSID_CACHE_SIZE	256
519 	typedef struct mount * volatile vmp_t;
520 	static vmp_t cache[FSID_CACHE_SIZE];
521 	struct mount *mp;
522 	int error;
523 	uint32_t hash;
524 
525 	CTR2(KTR_VFS, "%s: fsid %p", __func__, fsid);
526 	hash = fsid->val[0] ^ fsid->val[1];
527 	hash = (hash >> 16 ^ hash) & (FSID_CACHE_SIZE - 1);
528 	mp = cache[hash];
529 	if (mp == NULL ||
530 	    mp->mnt_stat.f_fsid.val[0] != fsid->val[0] ||
531 	    mp->mnt_stat.f_fsid.val[1] != fsid->val[1])
532 		goto slow;
533 	if (vfs_busy(mp, 0) != 0) {
534 		cache[hash] = NULL;
535 		goto slow;
536 	}
537 	if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
538 	    mp->mnt_stat.f_fsid.val[1] == fsid->val[1])
539 		return (mp);
540 	else
541 	    vfs_unbusy(mp);
542 
543 slow:
544 	mtx_lock(&mountlist_mtx);
545 	TAILQ_FOREACH(mp, &mountlist, mnt_list) {
546 		if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
547 		    mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
548 			error = vfs_busy(mp, MBF_MNTLSTLOCK);
549 			if (error) {
550 				cache[hash] = NULL;
551 				mtx_unlock(&mountlist_mtx);
552 				return (NULL);
553 			}
554 			cache[hash] = mp;
555 			return (mp);
556 		}
557 	}
558 	CTR2(KTR_VFS, "%s: lookup failed for %p id", __func__, fsid);
559 	mtx_unlock(&mountlist_mtx);
560 	return ((struct mount *) 0);
561 }
562 
563 /*
564  * Check if a user can access privileged mount options.
565  */
566 int
567 vfs_suser(struct mount *mp, struct thread *td)
568 {
569 	int error;
570 
571 	/*
572 	 * If the thread is jailed, but this is not a jail-friendly file
573 	 * system, deny immediately.
574 	 */
575 	if (!(mp->mnt_vfc->vfc_flags & VFCF_JAIL) && jailed(td->td_ucred))
576 		return (EPERM);
577 
578 	/*
579 	 * If the file system was mounted outside the jail of the calling
580 	 * thread, deny immediately.
581 	 */
582 	if (prison_check(td->td_ucred, mp->mnt_cred) != 0)
583 		return (EPERM);
584 
585 	/*
586 	 * If file system supports delegated administration, we don't check
587 	 * for the PRIV_VFS_MOUNT_OWNER privilege - it will be better verified
588 	 * by the file system itself.
589 	 * If this is not the user that did original mount, we check for
590 	 * the PRIV_VFS_MOUNT_OWNER privilege.
591 	 */
592 	if (!(mp->mnt_vfc->vfc_flags & VFCF_DELEGADMIN) &&
593 	    mp->mnt_cred->cr_uid != td->td_ucred->cr_uid) {
594 		if ((error = priv_check(td, PRIV_VFS_MOUNT_OWNER)) != 0)
595 			return (error);
596 	}
597 	return (0);
598 }
599 
600 /*
601  * Get a new unique fsid.  Try to make its val[0] unique, since this value
602  * will be used to create fake device numbers for stat().  Also try (but
603  * not so hard) make its val[0] unique mod 2^16, since some emulators only
604  * support 16-bit device numbers.  We end up with unique val[0]'s for the
605  * first 2^16 calls and unique val[0]'s mod 2^16 for the first 2^8 calls.
606  *
607  * Keep in mind that several mounts may be running in parallel.  Starting
608  * the search one past where the previous search terminated is both a
609  * micro-optimization and a defense against returning the same fsid to
610  * different mounts.
611  */
612 void
613 vfs_getnewfsid(struct mount *mp)
614 {
615 	static uint16_t mntid_base;
616 	struct mount *nmp;
617 	fsid_t tfsid;
618 	int mtype;
619 
620 	CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
621 	mtx_lock(&mntid_mtx);
622 	mtype = mp->mnt_vfc->vfc_typenum;
623 	tfsid.val[1] = mtype;
624 	mtype = (mtype & 0xFF) << 24;
625 	for (;;) {
626 		tfsid.val[0] = makedev(255,
627 		    mtype | ((mntid_base & 0xFF00) << 8) | (mntid_base & 0xFF));
628 		mntid_base++;
629 		if ((nmp = vfs_getvfs(&tfsid)) == NULL)
630 			break;
631 		vfs_rel(nmp);
632 	}
633 	mp->mnt_stat.f_fsid.val[0] = tfsid.val[0];
634 	mp->mnt_stat.f_fsid.val[1] = tfsid.val[1];
635 	mtx_unlock(&mntid_mtx);
636 }
637 
638 /*
639  * Knob to control the precision of file timestamps:
640  *
641  *   0 = seconds only; nanoseconds zeroed.
642  *   1 = seconds and nanoseconds, accurate within 1/HZ.
643  *   2 = seconds and nanoseconds, truncated to microseconds.
644  * >=3 = seconds and nanoseconds, maximum precision.
645  */
646 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC };
647 
648 static int timestamp_precision = TSP_USEC;
649 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW,
650     &timestamp_precision, 0, "File timestamp precision (0: seconds, "
651     "1: sec + ns accurate to 1/HZ, 2: sec + ns truncated to ms, "
652     "3+: sec + ns (max. precision))");
653 
654 /*
655  * Get a current timestamp.
656  */
657 void
658 vfs_timestamp(struct timespec *tsp)
659 {
660 	struct timeval tv;
661 
662 	switch (timestamp_precision) {
663 	case TSP_SEC:
664 		tsp->tv_sec = time_second;
665 		tsp->tv_nsec = 0;
666 		break;
667 	case TSP_HZ:
668 		getnanotime(tsp);
669 		break;
670 	case TSP_USEC:
671 		microtime(&tv);
672 		TIMEVAL_TO_TIMESPEC(&tv, tsp);
673 		break;
674 	case TSP_NSEC:
675 	default:
676 		nanotime(tsp);
677 		break;
678 	}
679 }
680 
681 /*
682  * Set vnode attributes to VNOVAL
683  */
684 void
685 vattr_null(struct vattr *vap)
686 {
687 
688 	vap->va_type = VNON;
689 	vap->va_size = VNOVAL;
690 	vap->va_bytes = VNOVAL;
691 	vap->va_mode = VNOVAL;
692 	vap->va_nlink = VNOVAL;
693 	vap->va_uid = VNOVAL;
694 	vap->va_gid = VNOVAL;
695 	vap->va_fsid = VNOVAL;
696 	vap->va_fileid = VNOVAL;
697 	vap->va_blocksize = VNOVAL;
698 	vap->va_rdev = VNOVAL;
699 	vap->va_atime.tv_sec = VNOVAL;
700 	vap->va_atime.tv_nsec = VNOVAL;
701 	vap->va_mtime.tv_sec = VNOVAL;
702 	vap->va_mtime.tv_nsec = VNOVAL;
703 	vap->va_ctime.tv_sec = VNOVAL;
704 	vap->va_ctime.tv_nsec = VNOVAL;
705 	vap->va_birthtime.tv_sec = VNOVAL;
706 	vap->va_birthtime.tv_nsec = VNOVAL;
707 	vap->va_flags = VNOVAL;
708 	vap->va_gen = VNOVAL;
709 	vap->va_vaflags = 0;
710 }
711 
712 /*
713  * This routine is called when we have too many vnodes.  It attempts
714  * to free <count> vnodes and will potentially free vnodes that still
715  * have VM backing store (VM backing store is typically the cause
716  * of a vnode blowout so we want to do this).  Therefore, this operation
717  * is not considered cheap.
718  *
719  * A number of conditions may prevent a vnode from being reclaimed.
720  * the buffer cache may have references on the vnode, a directory
721  * vnode may still have references due to the namei cache representing
722  * underlying files, or the vnode may be in active use.   It is not
723  * desireable to reuse such vnodes.  These conditions may cause the
724  * number of vnodes to reach some minimum value regardless of what
725  * you set kern.maxvnodes to.  Do not set kern.maxvnodes too low.
726  */
727 static int
728 vlrureclaim(struct mount *mp)
729 {
730 	struct vnode *vp;
731 	int done;
732 	int trigger;
733 	int usevnodes;
734 	int count;
735 
736 	/*
737 	 * Calculate the trigger point, don't allow user
738 	 * screwups to blow us up.   This prevents us from
739 	 * recycling vnodes with lots of resident pages.  We
740 	 * aren't trying to free memory, we are trying to
741 	 * free vnodes.
742 	 */
743 	usevnodes = desiredvnodes;
744 	if (usevnodes <= 0)
745 		usevnodes = 1;
746 	trigger = vm_cnt.v_page_count * 2 / usevnodes;
747 	done = 0;
748 	vn_start_write(NULL, &mp, V_WAIT);
749 	MNT_ILOCK(mp);
750 	count = mp->mnt_nvnodelistsize / 10 + 1;
751 	while (count != 0) {
752 		vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
753 		while (vp != NULL && vp->v_type == VMARKER)
754 			vp = TAILQ_NEXT(vp, v_nmntvnodes);
755 		if (vp == NULL)
756 			break;
757 		TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
758 		TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
759 		--count;
760 		if (!VI_TRYLOCK(vp))
761 			goto next_iter;
762 		/*
763 		 * If it's been deconstructed already, it's still
764 		 * referenced, or it exceeds the trigger, skip it.
765 		 */
766 		if (vp->v_usecount ||
767 		    (!vlru_allow_cache_src &&
768 			!LIST_EMPTY(&(vp)->v_cache_src)) ||
769 		    (vp->v_iflag & VI_DOOMED) != 0 || (vp->v_object != NULL &&
770 		    vp->v_object->resident_page_count > trigger)) {
771 			VI_UNLOCK(vp);
772 			goto next_iter;
773 		}
774 		MNT_IUNLOCK(mp);
775 		vholdl(vp);
776 		if (VOP_LOCK(vp, LK_INTERLOCK|LK_EXCLUSIVE|LK_NOWAIT)) {
777 			vdrop(vp);
778 			goto next_iter_mntunlocked;
779 		}
780 		VI_LOCK(vp);
781 		/*
782 		 * v_usecount may have been bumped after VOP_LOCK() dropped
783 		 * the vnode interlock and before it was locked again.
784 		 *
785 		 * It is not necessary to recheck VI_DOOMED because it can
786 		 * only be set by another thread that holds both the vnode
787 		 * lock and vnode interlock.  If another thread has the
788 		 * vnode lock before we get to VOP_LOCK() and obtains the
789 		 * vnode interlock after VOP_LOCK() drops the vnode
790 		 * interlock, the other thread will be unable to drop the
791 		 * vnode lock before our VOP_LOCK() call fails.
792 		 */
793 		if (vp->v_usecount ||
794 		    (!vlru_allow_cache_src &&
795 			!LIST_EMPTY(&(vp)->v_cache_src)) ||
796 		    (vp->v_object != NULL &&
797 		    vp->v_object->resident_page_count > trigger)) {
798 			VOP_UNLOCK(vp, LK_INTERLOCK);
799 			vdrop(vp);
800 			goto next_iter_mntunlocked;
801 		}
802 		KASSERT((vp->v_iflag & VI_DOOMED) == 0,
803 		    ("VI_DOOMED unexpectedly detected in vlrureclaim()"));
804 		atomic_add_long(&recycles_count, 1);
805 		vgonel(vp);
806 		VOP_UNLOCK(vp, 0);
807 		vdropl(vp);
808 		done++;
809 next_iter_mntunlocked:
810 		if (!should_yield())
811 			goto relock_mnt;
812 		goto yield;
813 next_iter:
814 		if (!should_yield())
815 			continue;
816 		MNT_IUNLOCK(mp);
817 yield:
818 		kern_yield(PRI_USER);
819 relock_mnt:
820 		MNT_ILOCK(mp);
821 	}
822 	MNT_IUNLOCK(mp);
823 	vn_finished_write(mp);
824 	return done;
825 }
826 
827 /*
828  * Attempt to keep the free list at wantfreevnodes length.
829  */
830 static void
831 vnlru_free(int count)
832 {
833 	struct vnode *vp;
834 
835 	mtx_assert(&vnode_free_list_mtx, MA_OWNED);
836 	for (; count > 0; count--) {
837 		vp = TAILQ_FIRST(&vnode_free_list);
838 		/*
839 		 * The list can be modified while the free_list_mtx
840 		 * has been dropped and vp could be NULL here.
841 		 */
842 		if (!vp)
843 			break;
844 		VNASSERT(vp->v_op != NULL, vp,
845 		    ("vnlru_free: vnode already reclaimed."));
846 		KASSERT((vp->v_iflag & VI_FREE) != 0,
847 		    ("Removing vnode not on freelist"));
848 		KASSERT((vp->v_iflag & VI_ACTIVE) == 0,
849 		    ("Mangling active vnode"));
850 		TAILQ_REMOVE(&vnode_free_list, vp, v_actfreelist);
851 		/*
852 		 * Don't recycle if we can't get the interlock.
853 		 */
854 		if (!VI_TRYLOCK(vp)) {
855 			TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_actfreelist);
856 			continue;
857 		}
858 		VNASSERT((vp->v_iflag & VI_FREE) != 0 && vp->v_holdcnt == 0,
859 		    vp, ("vp inconsistent on freelist"));
860 
861 		/*
862 		 * The clear of VI_FREE prevents activation of the
863 		 * vnode.  There is no sense in putting the vnode on
864 		 * the mount point active list, only to remove it
865 		 * later during recycling.  Inline the relevant part
866 		 * of vholdl(), to avoid triggering assertions or
867 		 * activating.
868 		 */
869 		freevnodes--;
870 		vp->v_iflag &= ~VI_FREE;
871 		vp->v_holdcnt++;
872 
873 		mtx_unlock(&vnode_free_list_mtx);
874 		VI_UNLOCK(vp);
875 		vtryrecycle(vp);
876 		/*
877 		 * If the recycled succeeded this vdrop will actually free
878 		 * the vnode.  If not it will simply place it back on
879 		 * the free list.
880 		 */
881 		vdrop(vp);
882 		mtx_lock(&vnode_free_list_mtx);
883 	}
884 }
885 /*
886  * Attempt to recycle vnodes in a context that is always safe to block.
887  * Calling vlrurecycle() from the bowels of filesystem code has some
888  * interesting deadlock problems.
889  */
890 static struct proc *vnlruproc;
891 static int vnlruproc_sig;
892 
893 static void
894 vnlru_proc(void)
895 {
896 	struct mount *mp, *nmp;
897 	int done;
898 	struct proc *p = vnlruproc;
899 
900 	EVENTHANDLER_REGISTER(shutdown_pre_sync, kproc_shutdown, p,
901 	    SHUTDOWN_PRI_FIRST);
902 
903 	for (;;) {
904 		kproc_suspend_check(p);
905 		mtx_lock(&vnode_free_list_mtx);
906 		if (freevnodes > wantfreevnodes)
907 			vnlru_free(freevnodes - wantfreevnodes);
908 		if (numvnodes <= desiredvnodes * 9 / 10) {
909 			vnlruproc_sig = 0;
910 			wakeup(&vnlruproc_sig);
911 			msleep(vnlruproc, &vnode_free_list_mtx,
912 			    PVFS|PDROP, "vlruwt", hz);
913 			continue;
914 		}
915 		mtx_unlock(&vnode_free_list_mtx);
916 		done = 0;
917 		mtx_lock(&mountlist_mtx);
918 		for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
919 			if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
920 				nmp = TAILQ_NEXT(mp, mnt_list);
921 				continue;
922 			}
923 			done += vlrureclaim(mp);
924 			mtx_lock(&mountlist_mtx);
925 			nmp = TAILQ_NEXT(mp, mnt_list);
926 			vfs_unbusy(mp);
927 		}
928 		mtx_unlock(&mountlist_mtx);
929 		if (done == 0) {
930 #if 0
931 			/* These messages are temporary debugging aids */
932 			if (vnlru_nowhere < 5)
933 				printf("vnlru process getting nowhere..\n");
934 			else if (vnlru_nowhere == 5)
935 				printf("vnlru process messages stopped.\n");
936 #endif
937 			vnlru_nowhere++;
938 			tsleep(vnlruproc, PPAUSE, "vlrup", hz * 3);
939 		} else
940 			kern_yield(PRI_USER);
941 	}
942 }
943 
944 static struct kproc_desc vnlru_kp = {
945 	"vnlru",
946 	vnlru_proc,
947 	&vnlruproc
948 };
949 SYSINIT(vnlru, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start,
950     &vnlru_kp);
951 
952 /*
953  * Routines having to do with the management of the vnode table.
954  */
955 
956 /*
957  * Try to recycle a freed vnode.  We abort if anyone picks up a reference
958  * before we actually vgone().  This function must be called with the vnode
959  * held to prevent the vnode from being returned to the free list midway
960  * through vgone().
961  */
962 static int
963 vtryrecycle(struct vnode *vp)
964 {
965 	struct mount *vnmp;
966 
967 	CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
968 	VNASSERT(vp->v_holdcnt, vp,
969 	    ("vtryrecycle: Recycling vp %p without a reference.", vp));
970 	/*
971 	 * This vnode may found and locked via some other list, if so we
972 	 * can't recycle it yet.
973 	 */
974 	if (VOP_LOCK(vp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
975 		CTR2(KTR_VFS,
976 		    "%s: impossible to recycle, vp %p lock is already held",
977 		    __func__, vp);
978 		return (EWOULDBLOCK);
979 	}
980 	/*
981 	 * Don't recycle if its filesystem is being suspended.
982 	 */
983 	if (vn_start_write(vp, &vnmp, V_NOWAIT) != 0) {
984 		VOP_UNLOCK(vp, 0);
985 		CTR2(KTR_VFS,
986 		    "%s: impossible to recycle, cannot start the write for %p",
987 		    __func__, vp);
988 		return (EBUSY);
989 	}
990 	/*
991 	 * If we got this far, we need to acquire the interlock and see if
992 	 * anyone picked up this vnode from another list.  If not, we will
993 	 * mark it with DOOMED via vgonel() so that anyone who does find it
994 	 * will skip over it.
995 	 */
996 	VI_LOCK(vp);
997 	if (vp->v_usecount) {
998 		VOP_UNLOCK(vp, LK_INTERLOCK);
999 		vn_finished_write(vnmp);
1000 		CTR2(KTR_VFS,
1001 		    "%s: impossible to recycle, %p is already referenced",
1002 		    __func__, vp);
1003 		return (EBUSY);
1004 	}
1005 	if ((vp->v_iflag & VI_DOOMED) == 0) {
1006 		atomic_add_long(&recycles_count, 1);
1007 		vgonel(vp);
1008 	}
1009 	VOP_UNLOCK(vp, LK_INTERLOCK);
1010 	vn_finished_write(vnmp);
1011 	return (0);
1012 }
1013 
1014 /*
1015  * Wait for available vnodes.
1016  */
1017 static int
1018 getnewvnode_wait(int suspended)
1019 {
1020 
1021 	mtx_assert(&vnode_free_list_mtx, MA_OWNED);
1022 	if (numvnodes > desiredvnodes) {
1023 		if (suspended) {
1024 			/*
1025 			 * File system is beeing suspended, we cannot risk a
1026 			 * deadlock here, so allocate new vnode anyway.
1027 			 */
1028 			if (freevnodes > wantfreevnodes)
1029 				vnlru_free(freevnodes - wantfreevnodes);
1030 			return (0);
1031 		}
1032 		if (vnlruproc_sig == 0) {
1033 			vnlruproc_sig = 1;	/* avoid unnecessary wakeups */
1034 			wakeup(vnlruproc);
1035 		}
1036 		msleep(&vnlruproc_sig, &vnode_free_list_mtx, PVFS,
1037 		    "vlruwk", hz);
1038 	}
1039 	return (numvnodes > desiredvnodes ? ENFILE : 0);
1040 }
1041 
1042 void
1043 getnewvnode_reserve(u_int count)
1044 {
1045 	struct thread *td;
1046 
1047 	td = curthread;
1048 	/* First try to be quick and racy. */
1049 	if (atomic_fetchadd_long(&numvnodes, count) + count <= desiredvnodes) {
1050 		td->td_vp_reserv += count;
1051 		return;
1052 	} else
1053 		atomic_subtract_long(&numvnodes, count);
1054 
1055 	mtx_lock(&vnode_free_list_mtx);
1056 	while (count > 0) {
1057 		if (getnewvnode_wait(0) == 0) {
1058 			count--;
1059 			td->td_vp_reserv++;
1060 			atomic_add_long(&numvnodes, 1);
1061 		}
1062 	}
1063 	mtx_unlock(&vnode_free_list_mtx);
1064 }
1065 
1066 void
1067 getnewvnode_drop_reserve(void)
1068 {
1069 	struct thread *td;
1070 
1071 	td = curthread;
1072 	atomic_subtract_long(&numvnodes, td->td_vp_reserv);
1073 	td->td_vp_reserv = 0;
1074 }
1075 
1076 /*
1077  * Return the next vnode from the free list.
1078  */
1079 int
1080 getnewvnode(const char *tag, struct mount *mp, struct vop_vector *vops,
1081     struct vnode **vpp)
1082 {
1083 	struct vnode *vp;
1084 	struct bufobj *bo;
1085 	struct thread *td;
1086 	int error;
1087 
1088 	CTR3(KTR_VFS, "%s: mp %p with tag %s", __func__, mp, tag);
1089 	vp = NULL;
1090 	td = curthread;
1091 	if (td->td_vp_reserv > 0) {
1092 		td->td_vp_reserv -= 1;
1093 		goto alloc;
1094 	}
1095 	mtx_lock(&vnode_free_list_mtx);
1096 	/*
1097 	 * Lend our context to reclaim vnodes if they've exceeded the max.
1098 	 */
1099 	if (freevnodes > wantfreevnodes)
1100 		vnlru_free(1);
1101 	error = getnewvnode_wait(mp != NULL && (mp->mnt_kern_flag &
1102 	    MNTK_SUSPEND));
1103 #if 0	/* XXX Not all VFS_VGET/ffs_vget callers check returns. */
1104 	if (error != 0) {
1105 		mtx_unlock(&vnode_free_list_mtx);
1106 		return (error);
1107 	}
1108 #endif
1109 	atomic_add_long(&numvnodes, 1);
1110 	mtx_unlock(&vnode_free_list_mtx);
1111 alloc:
1112 	atomic_add_long(&vnodes_created, 1);
1113 	vp = (struct vnode *) uma_zalloc(vnode_zone, M_WAITOK|M_ZERO);
1114 	/*
1115 	 * Setup locks.
1116 	 */
1117 	vp->v_vnlock = &vp->v_lock;
1118 	mtx_init(&vp->v_interlock, "vnode interlock", NULL, MTX_DEF);
1119 	/*
1120 	 * By default, don't allow shared locks unless filesystems
1121 	 * opt-in.
1122 	 */
1123 	lockinit(vp->v_vnlock, PVFS, tag, VLKTIMEOUT, LK_NOSHARE | LK_IS_VNODE);
1124 	/*
1125 	 * Initialize bufobj.
1126 	 */
1127 	bo = &vp->v_bufobj;
1128 	bo->__bo_vnode = vp;
1129 	rw_init(BO_LOCKPTR(bo), "bufobj interlock");
1130 	bo->bo_ops = &buf_ops_bio;
1131 	bo->bo_private = vp;
1132 	TAILQ_INIT(&bo->bo_clean.bv_hd);
1133 	TAILQ_INIT(&bo->bo_dirty.bv_hd);
1134 	/*
1135 	 * Initialize namecache.
1136 	 */
1137 	LIST_INIT(&vp->v_cache_src);
1138 	TAILQ_INIT(&vp->v_cache_dst);
1139 	/*
1140 	 * Finalize various vnode identity bits.
1141 	 */
1142 	vp->v_type = VNON;
1143 	vp->v_tag = tag;
1144 	vp->v_op = vops;
1145 	v_incr_usecount(vp);
1146 	vp->v_data = NULL;
1147 #ifdef MAC
1148 	mac_vnode_init(vp);
1149 	if (mp != NULL && (mp->mnt_flag & MNT_MULTILABEL) == 0)
1150 		mac_vnode_associate_singlelabel(mp, vp);
1151 	else if (mp == NULL && vops != &dead_vnodeops)
1152 		printf("NULL mp in getnewvnode()\n");
1153 #endif
1154 	if (mp != NULL) {
1155 		bo->bo_bsize = mp->mnt_stat.f_iosize;
1156 		if ((mp->mnt_kern_flag & MNTK_NOKNOTE) != 0)
1157 			vp->v_vflag |= VV_NOKNOTE;
1158 	}
1159 	rangelock_init(&vp->v_rl);
1160 
1161 	/*
1162 	 * For the filesystems which do not use vfs_hash_insert(),
1163 	 * still initialize v_hash to have vfs_hash_index() useful.
1164 	 * E.g., nullfs uses vfs_hash_index() on the lower vnode for
1165 	 * its own hashing.
1166 	 */
1167 	vp->v_hash = (uintptr_t)vp >> vnsz2log;
1168 
1169 	*vpp = vp;
1170 	return (0);
1171 }
1172 
1173 /*
1174  * Delete from old mount point vnode list, if on one.
1175  */
1176 static void
1177 delmntque(struct vnode *vp)
1178 {
1179 	struct mount *mp;
1180 	int active;
1181 
1182 	mp = vp->v_mount;
1183 	if (mp == NULL)
1184 		return;
1185 	MNT_ILOCK(mp);
1186 	VI_LOCK(vp);
1187 	KASSERT(mp->mnt_activevnodelistsize <= mp->mnt_nvnodelistsize,
1188 	    ("Active vnode list size %d > Vnode list size %d",
1189 	     mp->mnt_activevnodelistsize, mp->mnt_nvnodelistsize));
1190 	active = vp->v_iflag & VI_ACTIVE;
1191 	vp->v_iflag &= ~VI_ACTIVE;
1192 	if (active) {
1193 		mtx_lock(&vnode_free_list_mtx);
1194 		TAILQ_REMOVE(&mp->mnt_activevnodelist, vp, v_actfreelist);
1195 		mp->mnt_activevnodelistsize--;
1196 		mtx_unlock(&vnode_free_list_mtx);
1197 	}
1198 	vp->v_mount = NULL;
1199 	VI_UNLOCK(vp);
1200 	VNASSERT(mp->mnt_nvnodelistsize > 0, vp,
1201 		("bad mount point vnode list size"));
1202 	TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1203 	mp->mnt_nvnodelistsize--;
1204 	MNT_REL(mp);
1205 	MNT_IUNLOCK(mp);
1206 }
1207 
1208 static void
1209 insmntque_stddtr(struct vnode *vp, void *dtr_arg)
1210 {
1211 
1212 	vp->v_data = NULL;
1213 	vp->v_op = &dead_vnodeops;
1214 	vgone(vp);
1215 	vput(vp);
1216 }
1217 
1218 /*
1219  * Insert into list of vnodes for the new mount point, if available.
1220  */
1221 int
1222 insmntque1(struct vnode *vp, struct mount *mp,
1223 	void (*dtr)(struct vnode *, void *), void *dtr_arg)
1224 {
1225 
1226 	KASSERT(vp->v_mount == NULL,
1227 		("insmntque: vnode already on per mount vnode list"));
1228 	VNASSERT(mp != NULL, vp, ("Don't call insmntque(foo, NULL)"));
1229 	ASSERT_VOP_ELOCKED(vp, "insmntque: non-locked vp");
1230 
1231 	/*
1232 	 * We acquire the vnode interlock early to ensure that the
1233 	 * vnode cannot be recycled by another process releasing a
1234 	 * holdcnt on it before we get it on both the vnode list
1235 	 * and the active vnode list. The mount mutex protects only
1236 	 * manipulation of the vnode list and the vnode freelist
1237 	 * mutex protects only manipulation of the active vnode list.
1238 	 * Hence the need to hold the vnode interlock throughout.
1239 	 */
1240 	MNT_ILOCK(mp);
1241 	VI_LOCK(vp);
1242 	if (((mp->mnt_kern_flag & MNTK_NOINSMNTQ) != 0 &&
1243 	    ((mp->mnt_kern_flag & MNTK_UNMOUNTF) != 0 ||
1244 	    mp->mnt_nvnodelistsize == 0)) &&
1245 	    (vp->v_vflag & VV_FORCEINSMQ) == 0) {
1246 		VI_UNLOCK(vp);
1247 		MNT_IUNLOCK(mp);
1248 		if (dtr != NULL)
1249 			dtr(vp, dtr_arg);
1250 		return (EBUSY);
1251 	}
1252 	vp->v_mount = mp;
1253 	MNT_REF(mp);
1254 	TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1255 	VNASSERT(mp->mnt_nvnodelistsize >= 0, vp,
1256 		("neg mount point vnode list size"));
1257 	mp->mnt_nvnodelistsize++;
1258 	KASSERT((vp->v_iflag & VI_ACTIVE) == 0,
1259 	    ("Activating already active vnode"));
1260 	vp->v_iflag |= VI_ACTIVE;
1261 	mtx_lock(&vnode_free_list_mtx);
1262 	TAILQ_INSERT_HEAD(&mp->mnt_activevnodelist, vp, v_actfreelist);
1263 	mp->mnt_activevnodelistsize++;
1264 	mtx_unlock(&vnode_free_list_mtx);
1265 	VI_UNLOCK(vp);
1266 	MNT_IUNLOCK(mp);
1267 	return (0);
1268 }
1269 
1270 int
1271 insmntque(struct vnode *vp, struct mount *mp)
1272 {
1273 
1274 	return (insmntque1(vp, mp, insmntque_stddtr, NULL));
1275 }
1276 
1277 /*
1278  * Flush out and invalidate all buffers associated with a bufobj
1279  * Called with the underlying object locked.
1280  */
1281 int
1282 bufobj_invalbuf(struct bufobj *bo, int flags, int slpflag, int slptimeo)
1283 {
1284 	int error;
1285 
1286 	BO_LOCK(bo);
1287 	if (flags & V_SAVE) {
1288 		error = bufobj_wwait(bo, slpflag, slptimeo);
1289 		if (error) {
1290 			BO_UNLOCK(bo);
1291 			return (error);
1292 		}
1293 		if (bo->bo_dirty.bv_cnt > 0) {
1294 			BO_UNLOCK(bo);
1295 			if ((error = BO_SYNC(bo, MNT_WAIT)) != 0)
1296 				return (error);
1297 			/*
1298 			 * XXX We could save a lock/unlock if this was only
1299 			 * enabled under INVARIANTS
1300 			 */
1301 			BO_LOCK(bo);
1302 			if (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0)
1303 				panic("vinvalbuf: dirty bufs");
1304 		}
1305 	}
1306 	/*
1307 	 * If you alter this loop please notice that interlock is dropped and
1308 	 * reacquired in flushbuflist.  Special care is needed to ensure that
1309 	 * no race conditions occur from this.
1310 	 */
1311 	do {
1312 		error = flushbuflist(&bo->bo_clean,
1313 		    flags, bo, slpflag, slptimeo);
1314 		if (error == 0 && !(flags & V_CLEANONLY))
1315 			error = flushbuflist(&bo->bo_dirty,
1316 			    flags, bo, slpflag, slptimeo);
1317 		if (error != 0 && error != EAGAIN) {
1318 			BO_UNLOCK(bo);
1319 			return (error);
1320 		}
1321 	} while (error != 0);
1322 
1323 	/*
1324 	 * Wait for I/O to complete.  XXX needs cleaning up.  The vnode can
1325 	 * have write I/O in-progress but if there is a VM object then the
1326 	 * VM object can also have read-I/O in-progress.
1327 	 */
1328 	do {
1329 		bufobj_wwait(bo, 0, 0);
1330 		BO_UNLOCK(bo);
1331 		if (bo->bo_object != NULL) {
1332 			VM_OBJECT_WLOCK(bo->bo_object);
1333 			vm_object_pip_wait(bo->bo_object, "bovlbx");
1334 			VM_OBJECT_WUNLOCK(bo->bo_object);
1335 		}
1336 		BO_LOCK(bo);
1337 	} while (bo->bo_numoutput > 0);
1338 	BO_UNLOCK(bo);
1339 
1340 	/*
1341 	 * Destroy the copy in the VM cache, too.
1342 	 */
1343 	if (bo->bo_object != NULL &&
1344 	    (flags & (V_ALT | V_NORMAL | V_CLEANONLY)) == 0) {
1345 		VM_OBJECT_WLOCK(bo->bo_object);
1346 		vm_object_page_remove(bo->bo_object, 0, 0, (flags & V_SAVE) ?
1347 		    OBJPR_CLEANONLY : 0);
1348 		VM_OBJECT_WUNLOCK(bo->bo_object);
1349 	}
1350 
1351 #ifdef INVARIANTS
1352 	BO_LOCK(bo);
1353 	if ((flags & (V_ALT | V_NORMAL | V_CLEANONLY)) == 0 &&
1354 	    (bo->bo_dirty.bv_cnt > 0 || bo->bo_clean.bv_cnt > 0))
1355 		panic("vinvalbuf: flush failed");
1356 	BO_UNLOCK(bo);
1357 #endif
1358 	return (0);
1359 }
1360 
1361 /*
1362  * Flush out and invalidate all buffers associated with a vnode.
1363  * Called with the underlying object locked.
1364  */
1365 int
1366 vinvalbuf(struct vnode *vp, int flags, int slpflag, int slptimeo)
1367 {
1368 
1369 	CTR3(KTR_VFS, "%s: vp %p with flags %d", __func__, vp, flags);
1370 	ASSERT_VOP_LOCKED(vp, "vinvalbuf");
1371 	if (vp->v_object != NULL && vp->v_object->handle != vp)
1372 		return (0);
1373 	return (bufobj_invalbuf(&vp->v_bufobj, flags, slpflag, slptimeo));
1374 }
1375 
1376 /*
1377  * Flush out buffers on the specified list.
1378  *
1379  */
1380 static int
1381 flushbuflist(struct bufv *bufv, int flags, struct bufobj *bo, int slpflag,
1382     int slptimeo)
1383 {
1384 	struct buf *bp, *nbp;
1385 	int retval, error;
1386 	daddr_t lblkno;
1387 	b_xflags_t xflags;
1388 
1389 	ASSERT_BO_WLOCKED(bo);
1390 
1391 	retval = 0;
1392 	TAILQ_FOREACH_SAFE(bp, &bufv->bv_hd, b_bobufs, nbp) {
1393 		if (((flags & V_NORMAL) && (bp->b_xflags & BX_ALTDATA)) ||
1394 		    ((flags & V_ALT) && (bp->b_xflags & BX_ALTDATA) == 0)) {
1395 			continue;
1396 		}
1397 		lblkno = 0;
1398 		xflags = 0;
1399 		if (nbp != NULL) {
1400 			lblkno = nbp->b_lblkno;
1401 			xflags = nbp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN);
1402 		}
1403 		retval = EAGAIN;
1404 		error = BUF_TIMELOCK(bp,
1405 		    LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, BO_LOCKPTR(bo),
1406 		    "flushbuf", slpflag, slptimeo);
1407 		if (error) {
1408 			BO_LOCK(bo);
1409 			return (error != ENOLCK ? error : EAGAIN);
1410 		}
1411 		KASSERT(bp->b_bufobj == bo,
1412 		    ("bp %p wrong b_bufobj %p should be %p",
1413 		    bp, bp->b_bufobj, bo));
1414 		if (bp->b_bufobj != bo) {	/* XXX: necessary ? */
1415 			BUF_UNLOCK(bp);
1416 			BO_LOCK(bo);
1417 			return (EAGAIN);
1418 		}
1419 		/*
1420 		 * XXX Since there are no node locks for NFS, I
1421 		 * believe there is a slight chance that a delayed
1422 		 * write will occur while sleeping just above, so
1423 		 * check for it.
1424 		 */
1425 		if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
1426 		    (flags & V_SAVE)) {
1427 			bremfree(bp);
1428 			bp->b_flags |= B_ASYNC;
1429 			bwrite(bp);
1430 			BO_LOCK(bo);
1431 			return (EAGAIN);	/* XXX: why not loop ? */
1432 		}
1433 		bremfree(bp);
1434 		bp->b_flags |= (B_INVAL | B_RELBUF);
1435 		bp->b_flags &= ~B_ASYNC;
1436 		brelse(bp);
1437 		BO_LOCK(bo);
1438 		if (nbp != NULL &&
1439 		    (nbp->b_bufobj != bo ||
1440 		     nbp->b_lblkno != lblkno ||
1441 		     (nbp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) != xflags))
1442 			break;			/* nbp invalid */
1443 	}
1444 	return (retval);
1445 }
1446 
1447 /*
1448  * Truncate a file's buffer and pages to a specified length.  This
1449  * is in lieu of the old vinvalbuf mechanism, which performed unneeded
1450  * sync activity.
1451  */
1452 int
1453 vtruncbuf(struct vnode *vp, struct ucred *cred, off_t length, int blksize)
1454 {
1455 	struct buf *bp, *nbp;
1456 	int anyfreed;
1457 	int trunclbn;
1458 	struct bufobj *bo;
1459 
1460 	CTR5(KTR_VFS, "%s: vp %p with cred %p and block %d:%ju", __func__,
1461 	    vp, cred, blksize, (uintmax_t)length);
1462 
1463 	/*
1464 	 * Round up to the *next* lbn.
1465 	 */
1466 	trunclbn = (length + blksize - 1) / blksize;
1467 
1468 	ASSERT_VOP_LOCKED(vp, "vtruncbuf");
1469 restart:
1470 	bo = &vp->v_bufobj;
1471 	BO_LOCK(bo);
1472 	anyfreed = 1;
1473 	for (;anyfreed;) {
1474 		anyfreed = 0;
1475 		TAILQ_FOREACH_SAFE(bp, &bo->bo_clean.bv_hd, b_bobufs, nbp) {
1476 			if (bp->b_lblkno < trunclbn)
1477 				continue;
1478 			if (BUF_LOCK(bp,
1479 			    LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1480 			    BO_LOCKPTR(bo)) == ENOLCK)
1481 				goto restart;
1482 
1483 			bremfree(bp);
1484 			bp->b_flags |= (B_INVAL | B_RELBUF);
1485 			bp->b_flags &= ~B_ASYNC;
1486 			brelse(bp);
1487 			anyfreed = 1;
1488 
1489 			BO_LOCK(bo);
1490 			if (nbp != NULL &&
1491 			    (((nbp->b_xflags & BX_VNCLEAN) == 0) ||
1492 			    (nbp->b_vp != vp) ||
1493 			    (nbp->b_flags & B_DELWRI))) {
1494 				BO_UNLOCK(bo);
1495 				goto restart;
1496 			}
1497 		}
1498 
1499 		TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
1500 			if (bp->b_lblkno < trunclbn)
1501 				continue;
1502 			if (BUF_LOCK(bp,
1503 			    LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1504 			    BO_LOCKPTR(bo)) == ENOLCK)
1505 				goto restart;
1506 			bremfree(bp);
1507 			bp->b_flags |= (B_INVAL | B_RELBUF);
1508 			bp->b_flags &= ~B_ASYNC;
1509 			brelse(bp);
1510 			anyfreed = 1;
1511 
1512 			BO_LOCK(bo);
1513 			if (nbp != NULL &&
1514 			    (((nbp->b_xflags & BX_VNDIRTY) == 0) ||
1515 			    (nbp->b_vp != vp) ||
1516 			    (nbp->b_flags & B_DELWRI) == 0)) {
1517 				BO_UNLOCK(bo);
1518 				goto restart;
1519 			}
1520 		}
1521 	}
1522 
1523 	if (length > 0) {
1524 restartsync:
1525 		TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
1526 			if (bp->b_lblkno > 0)
1527 				continue;
1528 			/*
1529 			 * Since we hold the vnode lock this should only
1530 			 * fail if we're racing with the buf daemon.
1531 			 */
1532 			if (BUF_LOCK(bp,
1533 			    LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1534 			    BO_LOCKPTR(bo)) == ENOLCK) {
1535 				goto restart;
1536 			}
1537 			VNASSERT((bp->b_flags & B_DELWRI), vp,
1538 			    ("buf(%p) on dirty queue without DELWRI", bp));
1539 
1540 			bremfree(bp);
1541 			bawrite(bp);
1542 			BO_LOCK(bo);
1543 			goto restartsync;
1544 		}
1545 	}
1546 
1547 	bufobj_wwait(bo, 0, 0);
1548 	BO_UNLOCK(bo);
1549 	vnode_pager_setsize(vp, length);
1550 
1551 	return (0);
1552 }
1553 
1554 static void
1555 buf_vlist_remove(struct buf *bp)
1556 {
1557 	struct bufv *bv;
1558 
1559 	KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp));
1560 	ASSERT_BO_WLOCKED(bp->b_bufobj);
1561 	KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) !=
1562 	    (BX_VNDIRTY|BX_VNCLEAN),
1563 	    ("buf_vlist_remove: Buf %p is on two lists", bp));
1564 	if (bp->b_xflags & BX_VNDIRTY)
1565 		bv = &bp->b_bufobj->bo_dirty;
1566 	else
1567 		bv = &bp->b_bufobj->bo_clean;
1568 	BUF_PCTRIE_REMOVE(&bv->bv_root, bp->b_lblkno);
1569 	TAILQ_REMOVE(&bv->bv_hd, bp, b_bobufs);
1570 	bv->bv_cnt--;
1571 	bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN);
1572 }
1573 
1574 /*
1575  * Add the buffer to the sorted clean or dirty block list.
1576  *
1577  * NOTE: xflags is passed as a constant, optimizing this inline function!
1578  */
1579 static void
1580 buf_vlist_add(struct buf *bp, struct bufobj *bo, b_xflags_t xflags)
1581 {
1582 	struct bufv *bv;
1583 	struct buf *n;
1584 	int error;
1585 
1586 	ASSERT_BO_WLOCKED(bo);
1587 	KASSERT((bo->bo_flag & BO_DEAD) == 0, ("dead bo %p", bo));
1588 	KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0,
1589 	    ("buf_vlist_add: Buf %p has existing xflags %d", bp, bp->b_xflags));
1590 	bp->b_xflags |= xflags;
1591 	if (xflags & BX_VNDIRTY)
1592 		bv = &bo->bo_dirty;
1593 	else
1594 		bv = &bo->bo_clean;
1595 
1596 	/*
1597 	 * Keep the list ordered.  Optimize empty list insertion.  Assume
1598 	 * we tend to grow at the tail so lookup_le should usually be cheaper
1599 	 * than _ge.
1600 	 */
1601 	if (bv->bv_cnt == 0 ||
1602 	    bp->b_lblkno > TAILQ_LAST(&bv->bv_hd, buflists)->b_lblkno)
1603 		TAILQ_INSERT_TAIL(&bv->bv_hd, bp, b_bobufs);
1604 	else if ((n = BUF_PCTRIE_LOOKUP_LE(&bv->bv_root, bp->b_lblkno)) == NULL)
1605 		TAILQ_INSERT_HEAD(&bv->bv_hd, bp, b_bobufs);
1606 	else
1607 		TAILQ_INSERT_AFTER(&bv->bv_hd, n, bp, b_bobufs);
1608 	error = BUF_PCTRIE_INSERT(&bv->bv_root, bp);
1609 	if (error)
1610 		panic("buf_vlist_add:  Preallocated nodes insufficient.");
1611 	bv->bv_cnt++;
1612 }
1613 
1614 /*
1615  * Lookup a buffer using the splay tree.  Note that we specifically avoid
1616  * shadow buffers used in background bitmap writes.
1617  *
1618  * This code isn't quite efficient as it could be because we are maintaining
1619  * two sorted lists and do not know which list the block resides in.
1620  *
1621  * During a "make buildworld" the desired buffer is found at one of
1622  * the roots more than 60% of the time.  Thus, checking both roots
1623  * before performing either splay eliminates unnecessary splays on the
1624  * first tree splayed.
1625  */
1626 struct buf *
1627 gbincore(struct bufobj *bo, daddr_t lblkno)
1628 {
1629 	struct buf *bp;
1630 
1631 	ASSERT_BO_LOCKED(bo);
1632 	bp = BUF_PCTRIE_LOOKUP(&bo->bo_clean.bv_root, lblkno);
1633 	if (bp != NULL)
1634 		return (bp);
1635 	return BUF_PCTRIE_LOOKUP(&bo->bo_dirty.bv_root, lblkno);
1636 }
1637 
1638 /*
1639  * Associate a buffer with a vnode.
1640  */
1641 void
1642 bgetvp(struct vnode *vp, struct buf *bp)
1643 {
1644 	struct bufobj *bo;
1645 
1646 	bo = &vp->v_bufobj;
1647 	ASSERT_BO_WLOCKED(bo);
1648 	VNASSERT(bp->b_vp == NULL, bp->b_vp, ("bgetvp: not free"));
1649 
1650 	CTR3(KTR_BUF, "bgetvp(%p) vp %p flags %X", bp, vp, bp->b_flags);
1651 	VNASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0, vp,
1652 	    ("bgetvp: bp already attached! %p", bp));
1653 
1654 	vhold(vp);
1655 	bp->b_vp = vp;
1656 	bp->b_bufobj = bo;
1657 	/*
1658 	 * Insert onto list for new vnode.
1659 	 */
1660 	buf_vlist_add(bp, bo, BX_VNCLEAN);
1661 }
1662 
1663 /*
1664  * Disassociate a buffer from a vnode.
1665  */
1666 void
1667 brelvp(struct buf *bp)
1668 {
1669 	struct bufobj *bo;
1670 	struct vnode *vp;
1671 
1672 	CTR3(KTR_BUF, "brelvp(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1673 	KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
1674 
1675 	/*
1676 	 * Delete from old vnode list, if on one.
1677 	 */
1678 	vp = bp->b_vp;		/* XXX */
1679 	bo = bp->b_bufobj;
1680 	BO_LOCK(bo);
1681 	if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
1682 		buf_vlist_remove(bp);
1683 	else
1684 		panic("brelvp: Buffer %p not on queue.", bp);
1685 	if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) {
1686 		bo->bo_flag &= ~BO_ONWORKLST;
1687 		mtx_lock(&sync_mtx);
1688 		LIST_REMOVE(bo, bo_synclist);
1689 		syncer_worklist_len--;
1690 		mtx_unlock(&sync_mtx);
1691 	}
1692 	bp->b_vp = NULL;
1693 	bp->b_bufobj = NULL;
1694 	BO_UNLOCK(bo);
1695 	vdrop(vp);
1696 }
1697 
1698 /*
1699  * Add an item to the syncer work queue.
1700  */
1701 static void
1702 vn_syncer_add_to_worklist(struct bufobj *bo, int delay)
1703 {
1704 	int slot;
1705 
1706 	ASSERT_BO_WLOCKED(bo);
1707 
1708 	mtx_lock(&sync_mtx);
1709 	if (bo->bo_flag & BO_ONWORKLST)
1710 		LIST_REMOVE(bo, bo_synclist);
1711 	else {
1712 		bo->bo_flag |= BO_ONWORKLST;
1713 		syncer_worklist_len++;
1714 	}
1715 
1716 	if (delay > syncer_maxdelay - 2)
1717 		delay = syncer_maxdelay - 2;
1718 	slot = (syncer_delayno + delay) & syncer_mask;
1719 
1720 	LIST_INSERT_HEAD(&syncer_workitem_pending[slot], bo, bo_synclist);
1721 	mtx_unlock(&sync_mtx);
1722 }
1723 
1724 static int
1725 sysctl_vfs_worklist_len(SYSCTL_HANDLER_ARGS)
1726 {
1727 	int error, len;
1728 
1729 	mtx_lock(&sync_mtx);
1730 	len = syncer_worklist_len - sync_vnode_count;
1731 	mtx_unlock(&sync_mtx);
1732 	error = SYSCTL_OUT(req, &len, sizeof(len));
1733 	return (error);
1734 }
1735 
1736 SYSCTL_PROC(_vfs, OID_AUTO, worklist_len, CTLTYPE_INT | CTLFLAG_RD, NULL, 0,
1737     sysctl_vfs_worklist_len, "I", "Syncer thread worklist length");
1738 
1739 static struct proc *updateproc;
1740 static void sched_sync(void);
1741 static struct kproc_desc up_kp = {
1742 	"syncer",
1743 	sched_sync,
1744 	&updateproc
1745 };
1746 SYSINIT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp);
1747 
1748 static int
1749 sync_vnode(struct synclist *slp, struct bufobj **bo, struct thread *td)
1750 {
1751 	struct vnode *vp;
1752 	struct mount *mp;
1753 
1754 	*bo = LIST_FIRST(slp);
1755 	if (*bo == NULL)
1756 		return (0);
1757 	vp = (*bo)->__bo_vnode;	/* XXX */
1758 	if (VOP_ISLOCKED(vp) != 0 || VI_TRYLOCK(vp) == 0)
1759 		return (1);
1760 	/*
1761 	 * We use vhold in case the vnode does not
1762 	 * successfully sync.  vhold prevents the vnode from
1763 	 * going away when we unlock the sync_mtx so that
1764 	 * we can acquire the vnode interlock.
1765 	 */
1766 	vholdl(vp);
1767 	mtx_unlock(&sync_mtx);
1768 	VI_UNLOCK(vp);
1769 	if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
1770 		vdrop(vp);
1771 		mtx_lock(&sync_mtx);
1772 		return (*bo == LIST_FIRST(slp));
1773 	}
1774 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1775 	(void) VOP_FSYNC(vp, MNT_LAZY, td);
1776 	VOP_UNLOCK(vp, 0);
1777 	vn_finished_write(mp);
1778 	BO_LOCK(*bo);
1779 	if (((*bo)->bo_flag & BO_ONWORKLST) != 0) {
1780 		/*
1781 		 * Put us back on the worklist.  The worklist
1782 		 * routine will remove us from our current
1783 		 * position and then add us back in at a later
1784 		 * position.
1785 		 */
1786 		vn_syncer_add_to_worklist(*bo, syncdelay);
1787 	}
1788 	BO_UNLOCK(*bo);
1789 	vdrop(vp);
1790 	mtx_lock(&sync_mtx);
1791 	return (0);
1792 }
1793 
1794 static int first_printf = 1;
1795 
1796 /*
1797  * System filesystem synchronizer daemon.
1798  */
1799 static void
1800 sched_sync(void)
1801 {
1802 	struct synclist *next, *slp;
1803 	struct bufobj *bo;
1804 	long starttime;
1805 	struct thread *td = curthread;
1806 	int last_work_seen;
1807 	int net_worklist_len;
1808 	int syncer_final_iter;
1809 	int error;
1810 
1811 	last_work_seen = 0;
1812 	syncer_final_iter = 0;
1813 	syncer_state = SYNCER_RUNNING;
1814 	starttime = time_uptime;
1815 	td->td_pflags |= TDP_NORUNNINGBUF;
1816 
1817 	EVENTHANDLER_REGISTER(shutdown_pre_sync, syncer_shutdown, td->td_proc,
1818 	    SHUTDOWN_PRI_LAST);
1819 
1820 	mtx_lock(&sync_mtx);
1821 	for (;;) {
1822 		if (syncer_state == SYNCER_FINAL_DELAY &&
1823 		    syncer_final_iter == 0) {
1824 			mtx_unlock(&sync_mtx);
1825 			kproc_suspend_check(td->td_proc);
1826 			mtx_lock(&sync_mtx);
1827 		}
1828 		net_worklist_len = syncer_worklist_len - sync_vnode_count;
1829 		if (syncer_state != SYNCER_RUNNING &&
1830 		    starttime != time_uptime) {
1831 			if (first_printf) {
1832 				printf("\nSyncing disks, vnodes remaining...");
1833 				first_printf = 0;
1834 			}
1835 			printf("%d ", net_worklist_len);
1836 		}
1837 		starttime = time_uptime;
1838 
1839 		/*
1840 		 * Push files whose dirty time has expired.  Be careful
1841 		 * of interrupt race on slp queue.
1842 		 *
1843 		 * Skip over empty worklist slots when shutting down.
1844 		 */
1845 		do {
1846 			slp = &syncer_workitem_pending[syncer_delayno];
1847 			syncer_delayno += 1;
1848 			if (syncer_delayno == syncer_maxdelay)
1849 				syncer_delayno = 0;
1850 			next = &syncer_workitem_pending[syncer_delayno];
1851 			/*
1852 			 * If the worklist has wrapped since the
1853 			 * it was emptied of all but syncer vnodes,
1854 			 * switch to the FINAL_DELAY state and run
1855 			 * for one more second.
1856 			 */
1857 			if (syncer_state == SYNCER_SHUTTING_DOWN &&
1858 			    net_worklist_len == 0 &&
1859 			    last_work_seen == syncer_delayno) {
1860 				syncer_state = SYNCER_FINAL_DELAY;
1861 				syncer_final_iter = SYNCER_SHUTDOWN_SPEEDUP;
1862 			}
1863 		} while (syncer_state != SYNCER_RUNNING && LIST_EMPTY(slp) &&
1864 		    syncer_worklist_len > 0);
1865 
1866 		/*
1867 		 * Keep track of the last time there was anything
1868 		 * on the worklist other than syncer vnodes.
1869 		 * Return to the SHUTTING_DOWN state if any
1870 		 * new work appears.
1871 		 */
1872 		if (net_worklist_len > 0 || syncer_state == SYNCER_RUNNING)
1873 			last_work_seen = syncer_delayno;
1874 		if (net_worklist_len > 0 && syncer_state == SYNCER_FINAL_DELAY)
1875 			syncer_state = SYNCER_SHUTTING_DOWN;
1876 		while (!LIST_EMPTY(slp)) {
1877 			error = sync_vnode(slp, &bo, td);
1878 			if (error == 1) {
1879 				LIST_REMOVE(bo, bo_synclist);
1880 				LIST_INSERT_HEAD(next, bo, bo_synclist);
1881 				continue;
1882 			}
1883 
1884 			if (first_printf == 0) {
1885 				/*
1886 				 * Drop the sync mutex, because some watchdog
1887 				 * drivers need to sleep while patting
1888 				 */
1889 				mtx_unlock(&sync_mtx);
1890 				wdog_kern_pat(WD_LASTVAL);
1891 				mtx_lock(&sync_mtx);
1892 			}
1893 
1894 		}
1895 		if (syncer_state == SYNCER_FINAL_DELAY && syncer_final_iter > 0)
1896 			syncer_final_iter--;
1897 		/*
1898 		 * The variable rushjob allows the kernel to speed up the
1899 		 * processing of the filesystem syncer process. A rushjob
1900 		 * value of N tells the filesystem syncer to process the next
1901 		 * N seconds worth of work on its queue ASAP. Currently rushjob
1902 		 * is used by the soft update code to speed up the filesystem
1903 		 * syncer process when the incore state is getting so far
1904 		 * ahead of the disk that the kernel memory pool is being
1905 		 * threatened with exhaustion.
1906 		 */
1907 		if (rushjob > 0) {
1908 			rushjob -= 1;
1909 			continue;
1910 		}
1911 		/*
1912 		 * Just sleep for a short period of time between
1913 		 * iterations when shutting down to allow some I/O
1914 		 * to happen.
1915 		 *
1916 		 * If it has taken us less than a second to process the
1917 		 * current work, then wait. Otherwise start right over
1918 		 * again. We can still lose time if any single round
1919 		 * takes more than two seconds, but it does not really
1920 		 * matter as we are just trying to generally pace the
1921 		 * filesystem activity.
1922 		 */
1923 		if (syncer_state != SYNCER_RUNNING ||
1924 		    time_uptime == starttime) {
1925 			thread_lock(td);
1926 			sched_prio(td, PPAUSE);
1927 			thread_unlock(td);
1928 		}
1929 		if (syncer_state != SYNCER_RUNNING)
1930 			cv_timedwait(&sync_wakeup, &sync_mtx,
1931 			    hz / SYNCER_SHUTDOWN_SPEEDUP);
1932 		else if (time_uptime == starttime)
1933 			cv_timedwait(&sync_wakeup, &sync_mtx, hz);
1934 	}
1935 }
1936 
1937 /*
1938  * Request the syncer daemon to speed up its work.
1939  * We never push it to speed up more than half of its
1940  * normal turn time, otherwise it could take over the cpu.
1941  */
1942 int
1943 speedup_syncer(void)
1944 {
1945 	int ret = 0;
1946 
1947 	mtx_lock(&sync_mtx);
1948 	if (rushjob < syncdelay / 2) {
1949 		rushjob += 1;
1950 		stat_rush_requests += 1;
1951 		ret = 1;
1952 	}
1953 	mtx_unlock(&sync_mtx);
1954 	cv_broadcast(&sync_wakeup);
1955 	return (ret);
1956 }
1957 
1958 /*
1959  * Tell the syncer to speed up its work and run though its work
1960  * list several times, then tell it to shut down.
1961  */
1962 static void
1963 syncer_shutdown(void *arg, int howto)
1964 {
1965 
1966 	if (howto & RB_NOSYNC)
1967 		return;
1968 	mtx_lock(&sync_mtx);
1969 	syncer_state = SYNCER_SHUTTING_DOWN;
1970 	rushjob = 0;
1971 	mtx_unlock(&sync_mtx);
1972 	cv_broadcast(&sync_wakeup);
1973 	kproc_shutdown(arg, howto);
1974 }
1975 
1976 void
1977 syncer_suspend(void)
1978 {
1979 
1980 	syncer_shutdown(updateproc, 0);
1981 }
1982 
1983 void
1984 syncer_resume(void)
1985 {
1986 
1987 	mtx_lock(&sync_mtx);
1988 	first_printf = 1;
1989 	syncer_state = SYNCER_RUNNING;
1990 	mtx_unlock(&sync_mtx);
1991 	cv_broadcast(&sync_wakeup);
1992 	kproc_resume(updateproc);
1993 }
1994 
1995 /*
1996  * Reassign a buffer from one vnode to another.
1997  * Used to assign file specific control information
1998  * (indirect blocks) to the vnode to which they belong.
1999  */
2000 void
2001 reassignbuf(struct buf *bp)
2002 {
2003 	struct vnode *vp;
2004 	struct bufobj *bo;
2005 	int delay;
2006 #ifdef INVARIANTS
2007 	struct bufv *bv;
2008 #endif
2009 
2010 	vp = bp->b_vp;
2011 	bo = bp->b_bufobj;
2012 	++reassignbufcalls;
2013 
2014 	CTR3(KTR_BUF, "reassignbuf(%p) vp %p flags %X",
2015 	    bp, bp->b_vp, bp->b_flags);
2016 	/*
2017 	 * B_PAGING flagged buffers cannot be reassigned because their vp
2018 	 * is not fully linked in.
2019 	 */
2020 	if (bp->b_flags & B_PAGING)
2021 		panic("cannot reassign paging buffer");
2022 
2023 	/*
2024 	 * Delete from old vnode list, if on one.
2025 	 */
2026 	BO_LOCK(bo);
2027 	if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
2028 		buf_vlist_remove(bp);
2029 	else
2030 		panic("reassignbuf: Buffer %p not on queue.", bp);
2031 	/*
2032 	 * If dirty, put on list of dirty buffers; otherwise insert onto list
2033 	 * of clean buffers.
2034 	 */
2035 	if (bp->b_flags & B_DELWRI) {
2036 		if ((bo->bo_flag & BO_ONWORKLST) == 0) {
2037 			switch (vp->v_type) {
2038 			case VDIR:
2039 				delay = dirdelay;
2040 				break;
2041 			case VCHR:
2042 				delay = metadelay;
2043 				break;
2044 			default:
2045 				delay = filedelay;
2046 			}
2047 			vn_syncer_add_to_worklist(bo, delay);
2048 		}
2049 		buf_vlist_add(bp, bo, BX_VNDIRTY);
2050 	} else {
2051 		buf_vlist_add(bp, bo, BX_VNCLEAN);
2052 
2053 		if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) {
2054 			mtx_lock(&sync_mtx);
2055 			LIST_REMOVE(bo, bo_synclist);
2056 			syncer_worklist_len--;
2057 			mtx_unlock(&sync_mtx);
2058 			bo->bo_flag &= ~BO_ONWORKLST;
2059 		}
2060 	}
2061 #ifdef INVARIANTS
2062 	bv = &bo->bo_clean;
2063 	bp = TAILQ_FIRST(&bv->bv_hd);
2064 	KASSERT(bp == NULL || bp->b_bufobj == bo,
2065 	    ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2066 	bp = TAILQ_LAST(&bv->bv_hd, buflists);
2067 	KASSERT(bp == NULL || bp->b_bufobj == bo,
2068 	    ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2069 	bv = &bo->bo_dirty;
2070 	bp = TAILQ_FIRST(&bv->bv_hd);
2071 	KASSERT(bp == NULL || bp->b_bufobj == bo,
2072 	    ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2073 	bp = TAILQ_LAST(&bv->bv_hd, buflists);
2074 	KASSERT(bp == NULL || bp->b_bufobj == bo,
2075 	    ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2076 #endif
2077 	BO_UNLOCK(bo);
2078 }
2079 
2080 /*
2081  * Increment the use and hold counts on the vnode, taking care to reference
2082  * the driver's usecount if this is a chardev.  The vholdl() will remove
2083  * the vnode from the free list if it is presently free.  Requires the
2084  * vnode interlock and returns with it held.
2085  */
2086 static void
2087 v_incr_usecount(struct vnode *vp)
2088 {
2089 
2090 	CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2091 	vholdl(vp);
2092 	vp->v_usecount++;
2093 	if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2094 		dev_lock();
2095 		vp->v_rdev->si_usecount++;
2096 		dev_unlock();
2097 	}
2098 }
2099 
2100 /*
2101  * Turn a holdcnt into a use+holdcnt such that only one call to
2102  * v_decr_usecount is needed.
2103  */
2104 static void
2105 v_upgrade_usecount(struct vnode *vp)
2106 {
2107 
2108 	CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2109 	vp->v_usecount++;
2110 	if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2111 		dev_lock();
2112 		vp->v_rdev->si_usecount++;
2113 		dev_unlock();
2114 	}
2115 }
2116 
2117 /*
2118  * Decrement the vnode use and hold count along with the driver's usecount
2119  * if this is a chardev.  The vdropl() below releases the vnode interlock
2120  * as it may free the vnode.
2121  */
2122 static void
2123 v_decr_usecount(struct vnode *vp)
2124 {
2125 
2126 	ASSERT_VI_LOCKED(vp, __FUNCTION__);
2127 	VNASSERT(vp->v_usecount > 0, vp,
2128 	    ("v_decr_usecount: negative usecount"));
2129 	CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2130 	vp->v_usecount--;
2131 	if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2132 		dev_lock();
2133 		vp->v_rdev->si_usecount--;
2134 		dev_unlock();
2135 	}
2136 	vdropl(vp);
2137 }
2138 
2139 /*
2140  * Decrement only the use count and driver use count.  This is intended to
2141  * be paired with a follow on vdropl() to release the remaining hold count.
2142  * In this way we may vgone() a vnode with a 0 usecount without risk of
2143  * having it end up on a free list because the hold count is kept above 0.
2144  */
2145 static void
2146 v_decr_useonly(struct vnode *vp)
2147 {
2148 
2149 	ASSERT_VI_LOCKED(vp, __FUNCTION__);
2150 	VNASSERT(vp->v_usecount > 0, vp,
2151 	    ("v_decr_useonly: negative usecount"));
2152 	CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2153 	vp->v_usecount--;
2154 	if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2155 		dev_lock();
2156 		vp->v_rdev->si_usecount--;
2157 		dev_unlock();
2158 	}
2159 }
2160 
2161 /*
2162  * Grab a particular vnode from the free list, increment its
2163  * reference count and lock it.  VI_DOOMED is set if the vnode
2164  * is being destroyed.  Only callers who specify LK_RETRY will
2165  * see doomed vnodes.  If inactive processing was delayed in
2166  * vput try to do it here.
2167  */
2168 int
2169 vget(struct vnode *vp, int flags, struct thread *td)
2170 {
2171 	int error;
2172 
2173 	error = 0;
2174 	VNASSERT((flags & LK_TYPE_MASK) != 0, vp,
2175 	    ("vget: invalid lock operation"));
2176 	CTR3(KTR_VFS, "%s: vp %p with flags %d", __func__, vp, flags);
2177 
2178 	if ((flags & LK_INTERLOCK) == 0)
2179 		VI_LOCK(vp);
2180 	vholdl(vp);
2181 	if ((error = vn_lock(vp, flags | LK_INTERLOCK)) != 0) {
2182 		vdrop(vp);
2183 		CTR2(KTR_VFS, "%s: impossible to lock vnode %p", __func__,
2184 		    vp);
2185 		return (error);
2186 	}
2187 	if (vp->v_iflag & VI_DOOMED && (flags & LK_RETRY) == 0)
2188 		panic("vget: vn_lock failed to return ENOENT\n");
2189 	VI_LOCK(vp);
2190 	/* Upgrade our holdcnt to a usecount. */
2191 	v_upgrade_usecount(vp);
2192 	/*
2193 	 * We don't guarantee that any particular close will
2194 	 * trigger inactive processing so just make a best effort
2195 	 * here at preventing a reference to a removed file.  If
2196 	 * we don't succeed no harm is done.
2197 	 */
2198 	if (vp->v_iflag & VI_OWEINACT) {
2199 		if (VOP_ISLOCKED(vp) == LK_EXCLUSIVE &&
2200 		    (flags & LK_NOWAIT) == 0)
2201 			vinactive(vp, td);
2202 		vp->v_iflag &= ~VI_OWEINACT;
2203 	}
2204 	VI_UNLOCK(vp);
2205 	return (0);
2206 }
2207 
2208 /*
2209  * Increase the reference count of a vnode.
2210  */
2211 void
2212 vref(struct vnode *vp)
2213 {
2214 
2215 	CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2216 	VI_LOCK(vp);
2217 	v_incr_usecount(vp);
2218 	VI_UNLOCK(vp);
2219 }
2220 
2221 /*
2222  * Return reference count of a vnode.
2223  *
2224  * The results of this call are only guaranteed when some mechanism other
2225  * than the VI lock is used to stop other processes from gaining references
2226  * to the vnode.  This may be the case if the caller holds the only reference.
2227  * This is also useful when stale data is acceptable as race conditions may
2228  * be accounted for by some other means.
2229  */
2230 int
2231 vrefcnt(struct vnode *vp)
2232 {
2233 	int usecnt;
2234 
2235 	VI_LOCK(vp);
2236 	usecnt = vp->v_usecount;
2237 	VI_UNLOCK(vp);
2238 
2239 	return (usecnt);
2240 }
2241 
2242 #define	VPUTX_VRELE	1
2243 #define	VPUTX_VPUT	2
2244 #define	VPUTX_VUNREF	3
2245 
2246 static void
2247 vputx(struct vnode *vp, int func)
2248 {
2249 	int error;
2250 
2251 	KASSERT(vp != NULL, ("vputx: null vp"));
2252 	if (func == VPUTX_VUNREF)
2253 		ASSERT_VOP_LOCKED(vp, "vunref");
2254 	else if (func == VPUTX_VPUT)
2255 		ASSERT_VOP_LOCKED(vp, "vput");
2256 	else
2257 		KASSERT(func == VPUTX_VRELE, ("vputx: wrong func"));
2258 	CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2259 	VI_LOCK(vp);
2260 
2261 	/* Skip this v_writecount check if we're going to panic below. */
2262 	VNASSERT(vp->v_writecount < vp->v_usecount || vp->v_usecount < 1, vp,
2263 	    ("vputx: missed vn_close"));
2264 	error = 0;
2265 
2266 	if (vp->v_usecount > 1 || ((vp->v_iflag & VI_DOINGINACT) &&
2267 	    vp->v_usecount == 1)) {
2268 		if (func == VPUTX_VPUT)
2269 			VOP_UNLOCK(vp, 0);
2270 		v_decr_usecount(vp);
2271 		return;
2272 	}
2273 
2274 	if (vp->v_usecount != 1) {
2275 		vprint("vputx: negative ref count", vp);
2276 		panic("vputx: negative ref cnt");
2277 	}
2278 	CTR2(KTR_VFS, "%s: return vnode %p to the freelist", __func__, vp);
2279 	/*
2280 	 * We want to hold the vnode until the inactive finishes to
2281 	 * prevent vgone() races.  We drop the use count here and the
2282 	 * hold count below when we're done.
2283 	 */
2284 	v_decr_useonly(vp);
2285 	/*
2286 	 * We must call VOP_INACTIVE with the node locked. Mark
2287 	 * as VI_DOINGINACT to avoid recursion.
2288 	 */
2289 	vp->v_iflag |= VI_OWEINACT;
2290 	switch (func) {
2291 	case VPUTX_VRELE:
2292 		error = vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK);
2293 		VI_LOCK(vp);
2294 		break;
2295 	case VPUTX_VPUT:
2296 		if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
2297 			error = VOP_LOCK(vp, LK_UPGRADE | LK_INTERLOCK |
2298 			    LK_NOWAIT);
2299 			VI_LOCK(vp);
2300 		}
2301 		break;
2302 	case VPUTX_VUNREF:
2303 		if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
2304 			error = VOP_LOCK(vp, LK_TRYUPGRADE | LK_INTERLOCK);
2305 			VI_LOCK(vp);
2306 		}
2307 		break;
2308 	}
2309 	if (vp->v_usecount > 0)
2310 		vp->v_iflag &= ~VI_OWEINACT;
2311 	if (error == 0) {
2312 		if (vp->v_iflag & VI_OWEINACT)
2313 			vinactive(vp, curthread);
2314 		if (func != VPUTX_VUNREF)
2315 			VOP_UNLOCK(vp, 0);
2316 	}
2317 	vdropl(vp);
2318 }
2319 
2320 /*
2321  * Vnode put/release.
2322  * If count drops to zero, call inactive routine and return to freelist.
2323  */
2324 void
2325 vrele(struct vnode *vp)
2326 {
2327 
2328 	vputx(vp, VPUTX_VRELE);
2329 }
2330 
2331 /*
2332  * Release an already locked vnode.  This give the same effects as
2333  * unlock+vrele(), but takes less time and avoids releasing and
2334  * re-aquiring the lock (as vrele() acquires the lock internally.)
2335  */
2336 void
2337 vput(struct vnode *vp)
2338 {
2339 
2340 	vputx(vp, VPUTX_VPUT);
2341 }
2342 
2343 /*
2344  * Release an exclusively locked vnode. Do not unlock the vnode lock.
2345  */
2346 void
2347 vunref(struct vnode *vp)
2348 {
2349 
2350 	vputx(vp, VPUTX_VUNREF);
2351 }
2352 
2353 /*
2354  * Somebody doesn't want the vnode recycled.
2355  */
2356 void
2357 vhold(struct vnode *vp)
2358 {
2359 
2360 	VI_LOCK(vp);
2361 	vholdl(vp);
2362 	VI_UNLOCK(vp);
2363 }
2364 
2365 /*
2366  * Increase the hold count and activate if this is the first reference.
2367  */
2368 void
2369 vholdl(struct vnode *vp)
2370 {
2371 	struct mount *mp;
2372 
2373 	CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2374 #ifdef INVARIANTS
2375 	/* getnewvnode() calls v_incr_usecount() without holding interlock. */
2376 	if (vp->v_type != VNON || vp->v_data != NULL)
2377 		ASSERT_VI_LOCKED(vp, "vholdl");
2378 #endif
2379 	vp->v_holdcnt++;
2380 	if ((vp->v_iflag & VI_FREE) == 0)
2381 		return;
2382 	VNASSERT(vp->v_holdcnt == 1, vp, ("vholdl: wrong hold count"));
2383 	VNASSERT(vp->v_op != NULL, vp, ("vholdl: vnode already reclaimed."));
2384 	/*
2385 	 * Remove a vnode from the free list, mark it as in use,
2386 	 * and put it on the active list.
2387 	 */
2388 	mtx_lock(&vnode_free_list_mtx);
2389 	TAILQ_REMOVE(&vnode_free_list, vp, v_actfreelist);
2390 	freevnodes--;
2391 	vp->v_iflag &= ~(VI_FREE|VI_AGE);
2392 	KASSERT((vp->v_iflag & VI_ACTIVE) == 0,
2393 	    ("Activating already active vnode"));
2394 	vp->v_iflag |= VI_ACTIVE;
2395 	mp = vp->v_mount;
2396 	TAILQ_INSERT_HEAD(&mp->mnt_activevnodelist, vp, v_actfreelist);
2397 	mp->mnt_activevnodelistsize++;
2398 	mtx_unlock(&vnode_free_list_mtx);
2399 }
2400 
2401 /*
2402  * Note that there is one less who cares about this vnode.
2403  * vdrop() is the opposite of vhold().
2404  */
2405 void
2406 vdrop(struct vnode *vp)
2407 {
2408 
2409 	VI_LOCK(vp);
2410 	vdropl(vp);
2411 }
2412 
2413 /*
2414  * Drop the hold count of the vnode.  If this is the last reference to
2415  * the vnode we place it on the free list unless it has been vgone'd
2416  * (marked VI_DOOMED) in which case we will free it.
2417  */
2418 void
2419 vdropl(struct vnode *vp)
2420 {
2421 	struct bufobj *bo;
2422 	struct mount *mp;
2423 	int active;
2424 
2425 	ASSERT_VI_LOCKED(vp, "vdropl");
2426 	CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2427 	if (vp->v_holdcnt <= 0)
2428 		panic("vdrop: holdcnt %d", vp->v_holdcnt);
2429 	vp->v_holdcnt--;
2430 	VNASSERT(vp->v_holdcnt >= vp->v_usecount, vp,
2431 	    ("hold count less than use count"));
2432 	if (vp->v_holdcnt > 0) {
2433 		VI_UNLOCK(vp);
2434 		return;
2435 	}
2436 	if ((vp->v_iflag & VI_DOOMED) == 0) {
2437 		/*
2438 		 * Mark a vnode as free: remove it from its active list
2439 		 * and put it up for recycling on the freelist.
2440 		 */
2441 		VNASSERT(vp->v_op != NULL, vp,
2442 		    ("vdropl: vnode already reclaimed."));
2443 		VNASSERT((vp->v_iflag & VI_FREE) == 0, vp,
2444 		    ("vnode already free"));
2445 		VNASSERT(vp->v_holdcnt == 0, vp,
2446 		    ("vdropl: freeing when we shouldn't"));
2447 		active = vp->v_iflag & VI_ACTIVE;
2448 		if ((vp->v_iflag & VI_OWEINACT) == 0) {
2449 			vp->v_iflag &= ~VI_ACTIVE;
2450 			mp = vp->v_mount;
2451 			mtx_lock(&vnode_free_list_mtx);
2452 			if (active) {
2453 				TAILQ_REMOVE(&mp->mnt_activevnodelist, vp,
2454 				    v_actfreelist);
2455 				mp->mnt_activevnodelistsize--;
2456 			}
2457 			if (vp->v_iflag & VI_AGE) {
2458 				TAILQ_INSERT_HEAD(&vnode_free_list, vp,
2459 				    v_actfreelist);
2460 			} else {
2461 				TAILQ_INSERT_TAIL(&vnode_free_list, vp,
2462 				    v_actfreelist);
2463 			}
2464 			freevnodes++;
2465 			vp->v_iflag &= ~VI_AGE;
2466 			vp->v_iflag |= VI_FREE;
2467 			mtx_unlock(&vnode_free_list_mtx);
2468 		} else {
2469 			atomic_add_long(&free_owe_inact, 1);
2470 		}
2471 		VI_UNLOCK(vp);
2472 		return;
2473 	}
2474 	/*
2475 	 * The vnode has been marked for destruction, so free it.
2476 	 */
2477 	CTR2(KTR_VFS, "%s: destroying the vnode %p", __func__, vp);
2478 	atomic_subtract_long(&numvnodes, 1);
2479 	bo = &vp->v_bufobj;
2480 	VNASSERT((vp->v_iflag & VI_FREE) == 0, vp,
2481 	    ("cleaned vnode still on the free list."));
2482 	VNASSERT(vp->v_data == NULL, vp, ("cleaned vnode isn't"));
2483 	VNASSERT(vp->v_holdcnt == 0, vp, ("Non-zero hold count"));
2484 	VNASSERT(vp->v_usecount == 0, vp, ("Non-zero use count"));
2485 	VNASSERT(vp->v_writecount == 0, vp, ("Non-zero write count"));
2486 	VNASSERT(bo->bo_numoutput == 0, vp, ("Clean vnode has pending I/O's"));
2487 	VNASSERT(bo->bo_clean.bv_cnt == 0, vp, ("cleanbufcnt not 0"));
2488 	VNASSERT(pctrie_is_empty(&bo->bo_clean.bv_root), vp,
2489 	    ("clean blk trie not empty"));
2490 	VNASSERT(bo->bo_dirty.bv_cnt == 0, vp, ("dirtybufcnt not 0"));
2491 	VNASSERT(pctrie_is_empty(&bo->bo_dirty.bv_root), vp,
2492 	    ("dirty blk trie not empty"));
2493 	VNASSERT(TAILQ_EMPTY(&vp->v_cache_dst), vp, ("vp has namecache dst"));
2494 	VNASSERT(LIST_EMPTY(&vp->v_cache_src), vp, ("vp has namecache src"));
2495 	VNASSERT(vp->v_cache_dd == NULL, vp, ("vp has namecache for .."));
2496 	VI_UNLOCK(vp);
2497 #ifdef MAC
2498 	mac_vnode_destroy(vp);
2499 #endif
2500 	if (vp->v_pollinfo != NULL)
2501 		destroy_vpollinfo(vp->v_pollinfo);
2502 #ifdef INVARIANTS
2503 	/* XXX Elsewhere we detect an already freed vnode via NULL v_op. */
2504 	vp->v_op = NULL;
2505 #endif
2506 	rangelock_destroy(&vp->v_rl);
2507 	lockdestroy(vp->v_vnlock);
2508 	mtx_destroy(&vp->v_interlock);
2509 	rw_destroy(BO_LOCKPTR(bo));
2510 	uma_zfree(vnode_zone, vp);
2511 }
2512 
2513 /*
2514  * Call VOP_INACTIVE on the vnode and manage the DOINGINACT and OWEINACT
2515  * flags.  DOINGINACT prevents us from recursing in calls to vinactive.
2516  * OWEINACT tracks whether a vnode missed a call to inactive due to a
2517  * failed lock upgrade.
2518  */
2519 void
2520 vinactive(struct vnode *vp, struct thread *td)
2521 {
2522 	struct vm_object *obj;
2523 
2524 	ASSERT_VOP_ELOCKED(vp, "vinactive");
2525 	ASSERT_VI_LOCKED(vp, "vinactive");
2526 	VNASSERT((vp->v_iflag & VI_DOINGINACT) == 0, vp,
2527 	    ("vinactive: recursed on VI_DOINGINACT"));
2528 	CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2529 	vp->v_iflag |= VI_DOINGINACT;
2530 	vp->v_iflag &= ~VI_OWEINACT;
2531 	VI_UNLOCK(vp);
2532 	/*
2533 	 * Before moving off the active list, we must be sure that any
2534 	 * modified pages are on the vnode's dirty list since these will
2535 	 * no longer be checked once the vnode is on the inactive list.
2536 	 * Because the vnode vm object keeps a hold reference on the vnode
2537 	 * if there is at least one resident non-cached page, the vnode
2538 	 * cannot leave the active list without the page cleanup done.
2539 	 */
2540 	obj = vp->v_object;
2541 	if (obj != NULL && (obj->flags & OBJ_MIGHTBEDIRTY) != 0) {
2542 		VM_OBJECT_WLOCK(obj);
2543 		vm_object_page_clean(obj, 0, 0, OBJPC_NOSYNC);
2544 		VM_OBJECT_WUNLOCK(obj);
2545 	}
2546 	VOP_INACTIVE(vp, td);
2547 	VI_LOCK(vp);
2548 	VNASSERT(vp->v_iflag & VI_DOINGINACT, vp,
2549 	    ("vinactive: lost VI_DOINGINACT"));
2550 	vp->v_iflag &= ~VI_DOINGINACT;
2551 }
2552 
2553 /*
2554  * Remove any vnodes in the vnode table belonging to mount point mp.
2555  *
2556  * If FORCECLOSE is not specified, there should not be any active ones,
2557  * return error if any are found (nb: this is a user error, not a
2558  * system error). If FORCECLOSE is specified, detach any active vnodes
2559  * that are found.
2560  *
2561  * If WRITECLOSE is set, only flush out regular file vnodes open for
2562  * writing.
2563  *
2564  * SKIPSYSTEM causes any vnodes marked VV_SYSTEM to be skipped.
2565  *
2566  * `rootrefs' specifies the base reference count for the root vnode
2567  * of this filesystem. The root vnode is considered busy if its
2568  * v_usecount exceeds this value. On a successful return, vflush(, td)
2569  * will call vrele() on the root vnode exactly rootrefs times.
2570  * If the SKIPSYSTEM or WRITECLOSE flags are specified, rootrefs must
2571  * be zero.
2572  */
2573 #ifdef DIAGNOSTIC
2574 static int busyprt = 0;		/* print out busy vnodes */
2575 SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, "Print out busy vnodes");
2576 #endif
2577 
2578 int
2579 vflush(struct mount *mp, int rootrefs, int flags, struct thread *td)
2580 {
2581 	struct vnode *vp, *mvp, *rootvp = NULL;
2582 	struct vattr vattr;
2583 	int busy = 0, error;
2584 
2585 	CTR4(KTR_VFS, "%s: mp %p with rootrefs %d and flags %d", __func__, mp,
2586 	    rootrefs, flags);
2587 	if (rootrefs > 0) {
2588 		KASSERT((flags & (SKIPSYSTEM | WRITECLOSE)) == 0,
2589 		    ("vflush: bad args"));
2590 		/*
2591 		 * Get the filesystem root vnode. We can vput() it
2592 		 * immediately, since with rootrefs > 0, it won't go away.
2593 		 */
2594 		if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rootvp)) != 0) {
2595 			CTR2(KTR_VFS, "%s: vfs_root lookup failed with %d",
2596 			    __func__, error);
2597 			return (error);
2598 		}
2599 		vput(rootvp);
2600 	}
2601 loop:
2602 	MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
2603 		vholdl(vp);
2604 		error = vn_lock(vp, LK_INTERLOCK | LK_EXCLUSIVE);
2605 		if (error) {
2606 			vdrop(vp);
2607 			MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
2608 			goto loop;
2609 		}
2610 		/*
2611 		 * Skip over a vnodes marked VV_SYSTEM.
2612 		 */
2613 		if ((flags & SKIPSYSTEM) && (vp->v_vflag & VV_SYSTEM)) {
2614 			VOP_UNLOCK(vp, 0);
2615 			vdrop(vp);
2616 			continue;
2617 		}
2618 		/*
2619 		 * If WRITECLOSE is set, flush out unlinked but still open
2620 		 * files (even if open only for reading) and regular file
2621 		 * vnodes open for writing.
2622 		 */
2623 		if (flags & WRITECLOSE) {
2624 			if (vp->v_object != NULL) {
2625 				VM_OBJECT_WLOCK(vp->v_object);
2626 				vm_object_page_clean(vp->v_object, 0, 0, 0);
2627 				VM_OBJECT_WUNLOCK(vp->v_object);
2628 			}
2629 			error = VOP_FSYNC(vp, MNT_WAIT, td);
2630 			if (error != 0) {
2631 				VOP_UNLOCK(vp, 0);
2632 				vdrop(vp);
2633 				MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
2634 				return (error);
2635 			}
2636 			error = VOP_GETATTR(vp, &vattr, td->td_ucred);
2637 			VI_LOCK(vp);
2638 
2639 			if ((vp->v_type == VNON ||
2640 			    (error == 0 && vattr.va_nlink > 0)) &&
2641 			    (vp->v_writecount == 0 || vp->v_type != VREG)) {
2642 				VOP_UNLOCK(vp, 0);
2643 				vdropl(vp);
2644 				continue;
2645 			}
2646 		} else
2647 			VI_LOCK(vp);
2648 		/*
2649 		 * With v_usecount == 0, all we need to do is clear out the
2650 		 * vnode data structures and we are done.
2651 		 *
2652 		 * If FORCECLOSE is set, forcibly close the vnode.
2653 		 */
2654 		if (vp->v_usecount == 0 || (flags & FORCECLOSE)) {
2655 			VNASSERT(vp->v_usecount == 0 ||
2656 			    vp->v_op != &devfs_specops ||
2657 			    (vp->v_type != VCHR && vp->v_type != VBLK), vp,
2658 			    ("device VNODE %p is FORCECLOSED", vp));
2659 			vgonel(vp);
2660 		} else {
2661 			busy++;
2662 #ifdef DIAGNOSTIC
2663 			if (busyprt)
2664 				vprint("vflush: busy vnode", vp);
2665 #endif
2666 		}
2667 		VOP_UNLOCK(vp, 0);
2668 		vdropl(vp);
2669 	}
2670 	if (rootrefs > 0 && (flags & FORCECLOSE) == 0) {
2671 		/*
2672 		 * If just the root vnode is busy, and if its refcount
2673 		 * is equal to `rootrefs', then go ahead and kill it.
2674 		 */
2675 		VI_LOCK(rootvp);
2676 		KASSERT(busy > 0, ("vflush: not busy"));
2677 		VNASSERT(rootvp->v_usecount >= rootrefs, rootvp,
2678 		    ("vflush: usecount %d < rootrefs %d",
2679 		     rootvp->v_usecount, rootrefs));
2680 		if (busy == 1 && rootvp->v_usecount == rootrefs) {
2681 			VOP_LOCK(rootvp, LK_EXCLUSIVE|LK_INTERLOCK);
2682 			vgone(rootvp);
2683 			VOP_UNLOCK(rootvp, 0);
2684 			busy = 0;
2685 		} else
2686 			VI_UNLOCK(rootvp);
2687 	}
2688 	if (busy) {
2689 		CTR2(KTR_VFS, "%s: failing as %d vnodes are busy", __func__,
2690 		    busy);
2691 		return (EBUSY);
2692 	}
2693 	for (; rootrefs > 0; rootrefs--)
2694 		vrele(rootvp);
2695 	return (0);
2696 }
2697 
2698 /*
2699  * Recycle an unused vnode to the front of the free list.
2700  */
2701 int
2702 vrecycle(struct vnode *vp)
2703 {
2704 	int recycled;
2705 
2706 	ASSERT_VOP_ELOCKED(vp, "vrecycle");
2707 	CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2708 	recycled = 0;
2709 	VI_LOCK(vp);
2710 	if (vp->v_usecount == 0) {
2711 		recycled = 1;
2712 		vgonel(vp);
2713 	}
2714 	VI_UNLOCK(vp);
2715 	return (recycled);
2716 }
2717 
2718 /*
2719  * Eliminate all activity associated with a vnode
2720  * in preparation for reuse.
2721  */
2722 void
2723 vgone(struct vnode *vp)
2724 {
2725 	VI_LOCK(vp);
2726 	vgonel(vp);
2727 	VI_UNLOCK(vp);
2728 }
2729 
2730 static void
2731 notify_lowervp_vfs_dummy(struct mount *mp __unused,
2732     struct vnode *lowervp __unused)
2733 {
2734 }
2735 
2736 /*
2737  * Notify upper mounts about reclaimed or unlinked vnode.
2738  */
2739 void
2740 vfs_notify_upper(struct vnode *vp, int event)
2741 {
2742 	static struct vfsops vgonel_vfsops = {
2743 		.vfs_reclaim_lowervp = notify_lowervp_vfs_dummy,
2744 		.vfs_unlink_lowervp = notify_lowervp_vfs_dummy,
2745 	};
2746 	struct mount *mp, *ump, *mmp;
2747 
2748 	mp = vp->v_mount;
2749 	if (mp == NULL)
2750 		return;
2751 
2752 	MNT_ILOCK(mp);
2753 	if (TAILQ_EMPTY(&mp->mnt_uppers))
2754 		goto unlock;
2755 	MNT_IUNLOCK(mp);
2756 	mmp = malloc(sizeof(struct mount), M_TEMP, M_WAITOK | M_ZERO);
2757 	mmp->mnt_op = &vgonel_vfsops;
2758 	mmp->mnt_kern_flag |= MNTK_MARKER;
2759 	MNT_ILOCK(mp);
2760 	mp->mnt_kern_flag |= MNTK_VGONE_UPPER;
2761 	for (ump = TAILQ_FIRST(&mp->mnt_uppers); ump != NULL;) {
2762 		if ((ump->mnt_kern_flag & MNTK_MARKER) != 0) {
2763 			ump = TAILQ_NEXT(ump, mnt_upper_link);
2764 			continue;
2765 		}
2766 		TAILQ_INSERT_AFTER(&mp->mnt_uppers, ump, mmp, mnt_upper_link);
2767 		MNT_IUNLOCK(mp);
2768 		switch (event) {
2769 		case VFS_NOTIFY_UPPER_RECLAIM:
2770 			VFS_RECLAIM_LOWERVP(ump, vp);
2771 			break;
2772 		case VFS_NOTIFY_UPPER_UNLINK:
2773 			VFS_UNLINK_LOWERVP(ump, vp);
2774 			break;
2775 		default:
2776 			KASSERT(0, ("invalid event %d", event));
2777 			break;
2778 		}
2779 		MNT_ILOCK(mp);
2780 		ump = TAILQ_NEXT(mmp, mnt_upper_link);
2781 		TAILQ_REMOVE(&mp->mnt_uppers, mmp, mnt_upper_link);
2782 	}
2783 	free(mmp, M_TEMP);
2784 	mp->mnt_kern_flag &= ~MNTK_VGONE_UPPER;
2785 	if ((mp->mnt_kern_flag & MNTK_VGONE_WAITER) != 0) {
2786 		mp->mnt_kern_flag &= ~MNTK_VGONE_WAITER;
2787 		wakeup(&mp->mnt_uppers);
2788 	}
2789 unlock:
2790 	MNT_IUNLOCK(mp);
2791 }
2792 
2793 /*
2794  * vgone, with the vp interlock held.
2795  */
2796 void
2797 vgonel(struct vnode *vp)
2798 {
2799 	struct thread *td;
2800 	int oweinact;
2801 	int active;
2802 	struct mount *mp;
2803 
2804 	ASSERT_VOP_ELOCKED(vp, "vgonel");
2805 	ASSERT_VI_LOCKED(vp, "vgonel");
2806 	VNASSERT(vp->v_holdcnt, vp,
2807 	    ("vgonel: vp %p has no reference.", vp));
2808 	CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2809 	td = curthread;
2810 
2811 	/*
2812 	 * Don't vgonel if we're already doomed.
2813 	 */
2814 	if (vp->v_iflag & VI_DOOMED)
2815 		return;
2816 	vp->v_iflag |= VI_DOOMED;
2817 
2818 	/*
2819 	 * Check to see if the vnode is in use.  If so, we have to call
2820 	 * VOP_CLOSE() and VOP_INACTIVE().
2821 	 */
2822 	active = vp->v_usecount;
2823 	oweinact = (vp->v_iflag & VI_OWEINACT);
2824 	VI_UNLOCK(vp);
2825 	vfs_notify_upper(vp, VFS_NOTIFY_UPPER_RECLAIM);
2826 
2827 	/*
2828 	 * If purging an active vnode, it must be closed and
2829 	 * deactivated before being reclaimed.
2830 	 */
2831 	if (active)
2832 		VOP_CLOSE(vp, FNONBLOCK, NOCRED, td);
2833 	if (oweinact || active) {
2834 		VI_LOCK(vp);
2835 		if ((vp->v_iflag & VI_DOINGINACT) == 0)
2836 			vinactive(vp, td);
2837 		VI_UNLOCK(vp);
2838 	}
2839 	if (vp->v_type == VSOCK)
2840 		vfs_unp_reclaim(vp);
2841 
2842 	/*
2843 	 * Clean out any buffers associated with the vnode.
2844 	 * If the flush fails, just toss the buffers.
2845 	 */
2846 	mp = NULL;
2847 	if (!TAILQ_EMPTY(&vp->v_bufobj.bo_dirty.bv_hd))
2848 		(void) vn_start_secondary_write(vp, &mp, V_WAIT);
2849 	if (vinvalbuf(vp, V_SAVE, 0, 0) != 0) {
2850 		while (vinvalbuf(vp, 0, 0, 0) != 0)
2851 			;
2852 	}
2853 #ifdef INVARIANTS
2854 	BO_LOCK(&vp->v_bufobj);
2855 	KASSERT(TAILQ_EMPTY(&vp->v_bufobj.bo_dirty.bv_hd) &&
2856 	    vp->v_bufobj.bo_dirty.bv_cnt == 0 &&
2857 	    TAILQ_EMPTY(&vp->v_bufobj.bo_clean.bv_hd) &&
2858 	    vp->v_bufobj.bo_clean.bv_cnt == 0,
2859 	    ("vp %p bufobj not invalidated", vp));
2860 	vp->v_bufobj.bo_flag |= BO_DEAD;
2861 	BO_UNLOCK(&vp->v_bufobj);
2862 #endif
2863 
2864 	/*
2865 	 * Reclaim the vnode.
2866 	 */
2867 	if (VOP_RECLAIM(vp, td))
2868 		panic("vgone: cannot reclaim");
2869 	if (mp != NULL)
2870 		vn_finished_secondary_write(mp);
2871 	VNASSERT(vp->v_object == NULL, vp,
2872 	    ("vop_reclaim left v_object vp=%p, tag=%s", vp, vp->v_tag));
2873 	/*
2874 	 * Clear the advisory locks and wake up waiting threads.
2875 	 */
2876 	(void)VOP_ADVLOCKPURGE(vp);
2877 	/*
2878 	 * Delete from old mount point vnode list.
2879 	 */
2880 	delmntque(vp);
2881 	cache_purge(vp);
2882 	/*
2883 	 * Done with purge, reset to the standard lock and invalidate
2884 	 * the vnode.
2885 	 */
2886 	VI_LOCK(vp);
2887 	vp->v_vnlock = &vp->v_lock;
2888 	vp->v_op = &dead_vnodeops;
2889 	vp->v_tag = "none";
2890 	vp->v_type = VBAD;
2891 }
2892 
2893 /*
2894  * Calculate the total number of references to a special device.
2895  */
2896 int
2897 vcount(struct vnode *vp)
2898 {
2899 	int count;
2900 
2901 	dev_lock();
2902 	count = vp->v_rdev->si_usecount;
2903 	dev_unlock();
2904 	return (count);
2905 }
2906 
2907 /*
2908  * Same as above, but using the struct cdev *as argument
2909  */
2910 int
2911 count_dev(struct cdev *dev)
2912 {
2913 	int count;
2914 
2915 	dev_lock();
2916 	count = dev->si_usecount;
2917 	dev_unlock();
2918 	return(count);
2919 }
2920 
2921 /*
2922  * Print out a description of a vnode.
2923  */
2924 static char *typename[] =
2925 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD",
2926  "VMARKER"};
2927 
2928 void
2929 vn_printf(struct vnode *vp, const char *fmt, ...)
2930 {
2931 	va_list ap;
2932 	char buf[256], buf2[16];
2933 	u_long flags;
2934 
2935 	va_start(ap, fmt);
2936 	vprintf(fmt, ap);
2937 	va_end(ap);
2938 	printf("%p: ", (void *)vp);
2939 	printf("tag %s, type %s\n", vp->v_tag, typename[vp->v_type]);
2940 	printf("    usecount %d, writecount %d, refcount %d mountedhere %p\n",
2941 	    vp->v_usecount, vp->v_writecount, vp->v_holdcnt, vp->v_mountedhere);
2942 	buf[0] = '\0';
2943 	buf[1] = '\0';
2944 	if (vp->v_vflag & VV_ROOT)
2945 		strlcat(buf, "|VV_ROOT", sizeof(buf));
2946 	if (vp->v_vflag & VV_ISTTY)
2947 		strlcat(buf, "|VV_ISTTY", sizeof(buf));
2948 	if (vp->v_vflag & VV_NOSYNC)
2949 		strlcat(buf, "|VV_NOSYNC", sizeof(buf));
2950 	if (vp->v_vflag & VV_ETERNALDEV)
2951 		strlcat(buf, "|VV_ETERNALDEV", sizeof(buf));
2952 	if (vp->v_vflag & VV_CACHEDLABEL)
2953 		strlcat(buf, "|VV_CACHEDLABEL", sizeof(buf));
2954 	if (vp->v_vflag & VV_TEXT)
2955 		strlcat(buf, "|VV_TEXT", sizeof(buf));
2956 	if (vp->v_vflag & VV_COPYONWRITE)
2957 		strlcat(buf, "|VV_COPYONWRITE", sizeof(buf));
2958 	if (vp->v_vflag & VV_SYSTEM)
2959 		strlcat(buf, "|VV_SYSTEM", sizeof(buf));
2960 	if (vp->v_vflag & VV_PROCDEP)
2961 		strlcat(buf, "|VV_PROCDEP", sizeof(buf));
2962 	if (vp->v_vflag & VV_NOKNOTE)
2963 		strlcat(buf, "|VV_NOKNOTE", sizeof(buf));
2964 	if (vp->v_vflag & VV_DELETED)
2965 		strlcat(buf, "|VV_DELETED", sizeof(buf));
2966 	if (vp->v_vflag & VV_MD)
2967 		strlcat(buf, "|VV_MD", sizeof(buf));
2968 	if (vp->v_vflag & VV_FORCEINSMQ)
2969 		strlcat(buf, "|VV_FORCEINSMQ", sizeof(buf));
2970 	flags = vp->v_vflag & ~(VV_ROOT | VV_ISTTY | VV_NOSYNC | VV_ETERNALDEV |
2971 	    VV_CACHEDLABEL | VV_TEXT | VV_COPYONWRITE | VV_SYSTEM | VV_PROCDEP |
2972 	    VV_NOKNOTE | VV_DELETED | VV_MD | VV_FORCEINSMQ);
2973 	if (flags != 0) {
2974 		snprintf(buf2, sizeof(buf2), "|VV(0x%lx)", flags);
2975 		strlcat(buf, buf2, sizeof(buf));
2976 	}
2977 	if (vp->v_iflag & VI_MOUNT)
2978 		strlcat(buf, "|VI_MOUNT", sizeof(buf));
2979 	if (vp->v_iflag & VI_AGE)
2980 		strlcat(buf, "|VI_AGE", sizeof(buf));
2981 	if (vp->v_iflag & VI_DOOMED)
2982 		strlcat(buf, "|VI_DOOMED", sizeof(buf));
2983 	if (vp->v_iflag & VI_FREE)
2984 		strlcat(buf, "|VI_FREE", sizeof(buf));
2985 	if (vp->v_iflag & VI_ACTIVE)
2986 		strlcat(buf, "|VI_ACTIVE", sizeof(buf));
2987 	if (vp->v_iflag & VI_DOINGINACT)
2988 		strlcat(buf, "|VI_DOINGINACT", sizeof(buf));
2989 	if (vp->v_iflag & VI_OWEINACT)
2990 		strlcat(buf, "|VI_OWEINACT", sizeof(buf));
2991 	flags = vp->v_iflag & ~(VI_MOUNT | VI_AGE | VI_DOOMED | VI_FREE |
2992 	    VI_ACTIVE | VI_DOINGINACT | VI_OWEINACT);
2993 	if (flags != 0) {
2994 		snprintf(buf2, sizeof(buf2), "|VI(0x%lx)", flags);
2995 		strlcat(buf, buf2, sizeof(buf));
2996 	}
2997 	printf("    flags (%s)\n", buf + 1);
2998 	if (mtx_owned(VI_MTX(vp)))
2999 		printf(" VI_LOCKed");
3000 	if (vp->v_object != NULL)
3001 		printf("    v_object %p ref %d pages %d "
3002 		    "cleanbuf %d dirtybuf %d\n",
3003 		    vp->v_object, vp->v_object->ref_count,
3004 		    vp->v_object->resident_page_count,
3005 		    vp->v_bufobj.bo_dirty.bv_cnt,
3006 		    vp->v_bufobj.bo_clean.bv_cnt);
3007 	printf("    ");
3008 	lockmgr_printinfo(vp->v_vnlock);
3009 	if (vp->v_data != NULL)
3010 		VOP_PRINT(vp);
3011 }
3012 
3013 #ifdef DDB
3014 /*
3015  * List all of the locked vnodes in the system.
3016  * Called when debugging the kernel.
3017  */
3018 DB_SHOW_COMMAND(lockedvnods, lockedvnodes)
3019 {
3020 	struct mount *mp;
3021 	struct vnode *vp;
3022 
3023 	/*
3024 	 * Note: because this is DDB, we can't obey the locking semantics
3025 	 * for these structures, which means we could catch an inconsistent
3026 	 * state and dereference a nasty pointer.  Not much to be done
3027 	 * about that.
3028 	 */
3029 	db_printf("Locked vnodes\n");
3030 	TAILQ_FOREACH(mp, &mountlist, mnt_list) {
3031 		TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
3032 			if (vp->v_type != VMARKER && VOP_ISLOCKED(vp))
3033 				vprint("", vp);
3034 		}
3035 	}
3036 }
3037 
3038 /*
3039  * Show details about the given vnode.
3040  */
3041 DB_SHOW_COMMAND(vnode, db_show_vnode)
3042 {
3043 	struct vnode *vp;
3044 
3045 	if (!have_addr)
3046 		return;
3047 	vp = (struct vnode *)addr;
3048 	vn_printf(vp, "vnode ");
3049 }
3050 
3051 /*
3052  * Show details about the given mount point.
3053  */
3054 DB_SHOW_COMMAND(mount, db_show_mount)
3055 {
3056 	struct mount *mp;
3057 	struct vfsopt *opt;
3058 	struct statfs *sp;
3059 	struct vnode *vp;
3060 	char buf[512];
3061 	uint64_t mflags;
3062 	u_int flags;
3063 
3064 	if (!have_addr) {
3065 		/* No address given, print short info about all mount points. */
3066 		TAILQ_FOREACH(mp, &mountlist, mnt_list) {
3067 			db_printf("%p %s on %s (%s)\n", mp,
3068 			    mp->mnt_stat.f_mntfromname,
3069 			    mp->mnt_stat.f_mntonname,
3070 			    mp->mnt_stat.f_fstypename);
3071 			if (db_pager_quit)
3072 				break;
3073 		}
3074 		db_printf("\nMore info: show mount <addr>\n");
3075 		return;
3076 	}
3077 
3078 	mp = (struct mount *)addr;
3079 	db_printf("%p %s on %s (%s)\n", mp, mp->mnt_stat.f_mntfromname,
3080 	    mp->mnt_stat.f_mntonname, mp->mnt_stat.f_fstypename);
3081 
3082 	buf[0] = '\0';
3083 	mflags = mp->mnt_flag;
3084 #define	MNT_FLAG(flag)	do {						\
3085 	if (mflags & (flag)) {						\
3086 		if (buf[0] != '\0')					\
3087 			strlcat(buf, ", ", sizeof(buf));		\
3088 		strlcat(buf, (#flag) + 4, sizeof(buf));			\
3089 		mflags &= ~(flag);					\
3090 	}								\
3091 } while (0)
3092 	MNT_FLAG(MNT_RDONLY);
3093 	MNT_FLAG(MNT_SYNCHRONOUS);
3094 	MNT_FLAG(MNT_NOEXEC);
3095 	MNT_FLAG(MNT_NOSUID);
3096 	MNT_FLAG(MNT_NFS4ACLS);
3097 	MNT_FLAG(MNT_UNION);
3098 	MNT_FLAG(MNT_ASYNC);
3099 	MNT_FLAG(MNT_SUIDDIR);
3100 	MNT_FLAG(MNT_SOFTDEP);
3101 	MNT_FLAG(MNT_NOSYMFOLLOW);
3102 	MNT_FLAG(MNT_GJOURNAL);
3103 	MNT_FLAG(MNT_MULTILABEL);
3104 	MNT_FLAG(MNT_ACLS);
3105 	MNT_FLAG(MNT_NOATIME);
3106 	MNT_FLAG(MNT_NOCLUSTERR);
3107 	MNT_FLAG(MNT_NOCLUSTERW);
3108 	MNT_FLAG(MNT_SUJ);
3109 	MNT_FLAG(MNT_EXRDONLY);
3110 	MNT_FLAG(MNT_EXPORTED);
3111 	MNT_FLAG(MNT_DEFEXPORTED);
3112 	MNT_FLAG(MNT_EXPORTANON);
3113 	MNT_FLAG(MNT_EXKERB);
3114 	MNT_FLAG(MNT_EXPUBLIC);
3115 	MNT_FLAG(MNT_LOCAL);
3116 	MNT_FLAG(MNT_QUOTA);
3117 	MNT_FLAG(MNT_ROOTFS);
3118 	MNT_FLAG(MNT_USER);
3119 	MNT_FLAG(MNT_IGNORE);
3120 	MNT_FLAG(MNT_UPDATE);
3121 	MNT_FLAG(MNT_DELEXPORT);
3122 	MNT_FLAG(MNT_RELOAD);
3123 	MNT_FLAG(MNT_FORCE);
3124 	MNT_FLAG(MNT_SNAPSHOT);
3125 	MNT_FLAG(MNT_BYFSID);
3126 #undef MNT_FLAG
3127 	if (mflags != 0) {
3128 		if (buf[0] != '\0')
3129 			strlcat(buf, ", ", sizeof(buf));
3130 		snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
3131 		    "0x%016jx", mflags);
3132 	}
3133 	db_printf("    mnt_flag = %s\n", buf);
3134 
3135 	buf[0] = '\0';
3136 	flags = mp->mnt_kern_flag;
3137 #define	MNT_KERN_FLAG(flag)	do {					\
3138 	if (flags & (flag)) {						\
3139 		if (buf[0] != '\0')					\
3140 			strlcat(buf, ", ", sizeof(buf));		\
3141 		strlcat(buf, (#flag) + 5, sizeof(buf));			\
3142 		flags &= ~(flag);					\
3143 	}								\
3144 } while (0)
3145 	MNT_KERN_FLAG(MNTK_UNMOUNTF);
3146 	MNT_KERN_FLAG(MNTK_ASYNC);
3147 	MNT_KERN_FLAG(MNTK_SOFTDEP);
3148 	MNT_KERN_FLAG(MNTK_NOINSMNTQ);
3149 	MNT_KERN_FLAG(MNTK_DRAINING);
3150 	MNT_KERN_FLAG(MNTK_REFEXPIRE);
3151 	MNT_KERN_FLAG(MNTK_EXTENDED_SHARED);
3152 	MNT_KERN_FLAG(MNTK_SHARED_WRITES);
3153 	MNT_KERN_FLAG(MNTK_NO_IOPF);
3154 	MNT_KERN_FLAG(MNTK_VGONE_UPPER);
3155 	MNT_KERN_FLAG(MNTK_VGONE_WAITER);
3156 	MNT_KERN_FLAG(MNTK_LOOKUP_EXCL_DOTDOT);
3157 	MNT_KERN_FLAG(MNTK_MARKER);
3158 	MNT_KERN_FLAG(MNTK_USES_BCACHE);
3159 	MNT_KERN_FLAG(MNTK_NOASYNC);
3160 	MNT_KERN_FLAG(MNTK_UNMOUNT);
3161 	MNT_KERN_FLAG(MNTK_MWAIT);
3162 	MNT_KERN_FLAG(MNTK_SUSPEND);
3163 	MNT_KERN_FLAG(MNTK_SUSPEND2);
3164 	MNT_KERN_FLAG(MNTK_SUSPENDED);
3165 	MNT_KERN_FLAG(MNTK_LOOKUP_SHARED);
3166 	MNT_KERN_FLAG(MNTK_NOKNOTE);
3167 #undef MNT_KERN_FLAG
3168 	if (flags != 0) {
3169 		if (buf[0] != '\0')
3170 			strlcat(buf, ", ", sizeof(buf));
3171 		snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
3172 		    "0x%08x", flags);
3173 	}
3174 	db_printf("    mnt_kern_flag = %s\n", buf);
3175 
3176 	db_printf("    mnt_opt = ");
3177 	opt = TAILQ_FIRST(mp->mnt_opt);
3178 	if (opt != NULL) {
3179 		db_printf("%s", opt->name);
3180 		opt = TAILQ_NEXT(opt, link);
3181 		while (opt != NULL) {
3182 			db_printf(", %s", opt->name);
3183 			opt = TAILQ_NEXT(opt, link);
3184 		}
3185 	}
3186 	db_printf("\n");
3187 
3188 	sp = &mp->mnt_stat;
3189 	db_printf("    mnt_stat = { version=%u type=%u flags=0x%016jx "
3190 	    "bsize=%ju iosize=%ju blocks=%ju bfree=%ju bavail=%jd files=%ju "
3191 	    "ffree=%jd syncwrites=%ju asyncwrites=%ju syncreads=%ju "
3192 	    "asyncreads=%ju namemax=%u owner=%u fsid=[%d, %d] }\n",
3193 	    (u_int)sp->f_version, (u_int)sp->f_type, (uintmax_t)sp->f_flags,
3194 	    (uintmax_t)sp->f_bsize, (uintmax_t)sp->f_iosize,
3195 	    (uintmax_t)sp->f_blocks, (uintmax_t)sp->f_bfree,
3196 	    (intmax_t)sp->f_bavail, (uintmax_t)sp->f_files,
3197 	    (intmax_t)sp->f_ffree, (uintmax_t)sp->f_syncwrites,
3198 	    (uintmax_t)sp->f_asyncwrites, (uintmax_t)sp->f_syncreads,
3199 	    (uintmax_t)sp->f_asyncreads, (u_int)sp->f_namemax,
3200 	    (u_int)sp->f_owner, (int)sp->f_fsid.val[0], (int)sp->f_fsid.val[1]);
3201 
3202 	db_printf("    mnt_cred = { uid=%u ruid=%u",
3203 	    (u_int)mp->mnt_cred->cr_uid, (u_int)mp->mnt_cred->cr_ruid);
3204 	if (jailed(mp->mnt_cred))
3205 		db_printf(", jail=%d", mp->mnt_cred->cr_prison->pr_id);
3206 	db_printf(" }\n");
3207 	db_printf("    mnt_ref = %d\n", mp->mnt_ref);
3208 	db_printf("    mnt_gen = %d\n", mp->mnt_gen);
3209 	db_printf("    mnt_nvnodelistsize = %d\n", mp->mnt_nvnodelistsize);
3210 	db_printf("    mnt_activevnodelistsize = %d\n",
3211 	    mp->mnt_activevnodelistsize);
3212 	db_printf("    mnt_writeopcount = %d\n", mp->mnt_writeopcount);
3213 	db_printf("    mnt_maxsymlinklen = %d\n", mp->mnt_maxsymlinklen);
3214 	db_printf("    mnt_iosize_max = %d\n", mp->mnt_iosize_max);
3215 	db_printf("    mnt_hashseed = %u\n", mp->mnt_hashseed);
3216 	db_printf("    mnt_lockref = %d\n", mp->mnt_lockref);
3217 	db_printf("    mnt_secondary_writes = %d\n", mp->mnt_secondary_writes);
3218 	db_printf("    mnt_secondary_accwrites = %d\n",
3219 	    mp->mnt_secondary_accwrites);
3220 	db_printf("    mnt_gjprovider = %s\n",
3221 	    mp->mnt_gjprovider != NULL ? mp->mnt_gjprovider : "NULL");
3222 
3223 	db_printf("\n\nList of active vnodes\n");
3224 	TAILQ_FOREACH(vp, &mp->mnt_activevnodelist, v_actfreelist) {
3225 		if (vp->v_type != VMARKER) {
3226 			vn_printf(vp, "vnode ");
3227 			if (db_pager_quit)
3228 				break;
3229 		}
3230 	}
3231 	db_printf("\n\nList of inactive vnodes\n");
3232 	TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
3233 		if (vp->v_type != VMARKER && (vp->v_iflag & VI_ACTIVE) == 0) {
3234 			vn_printf(vp, "vnode ");
3235 			if (db_pager_quit)
3236 				break;
3237 		}
3238 	}
3239 }
3240 #endif	/* DDB */
3241 
3242 /*
3243  * Fill in a struct xvfsconf based on a struct vfsconf.
3244  */
3245 static int
3246 vfsconf2x(struct sysctl_req *req, struct vfsconf *vfsp)
3247 {
3248 	struct xvfsconf xvfsp;
3249 
3250 	bzero(&xvfsp, sizeof(xvfsp));
3251 	strcpy(xvfsp.vfc_name, vfsp->vfc_name);
3252 	xvfsp.vfc_typenum = vfsp->vfc_typenum;
3253 	xvfsp.vfc_refcount = vfsp->vfc_refcount;
3254 	xvfsp.vfc_flags = vfsp->vfc_flags;
3255 	/*
3256 	 * These are unused in userland, we keep them
3257 	 * to not break binary compatibility.
3258 	 */
3259 	xvfsp.vfc_vfsops = NULL;
3260 	xvfsp.vfc_next = NULL;
3261 	return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp)));
3262 }
3263 
3264 #ifdef COMPAT_FREEBSD32
3265 struct xvfsconf32 {
3266 	uint32_t	vfc_vfsops;
3267 	char		vfc_name[MFSNAMELEN];
3268 	int32_t		vfc_typenum;
3269 	int32_t		vfc_refcount;
3270 	int32_t		vfc_flags;
3271 	uint32_t	vfc_next;
3272 };
3273 
3274 static int
3275 vfsconf2x32(struct sysctl_req *req, struct vfsconf *vfsp)
3276 {
3277 	struct xvfsconf32 xvfsp;
3278 
3279 	strcpy(xvfsp.vfc_name, vfsp->vfc_name);
3280 	xvfsp.vfc_typenum = vfsp->vfc_typenum;
3281 	xvfsp.vfc_refcount = vfsp->vfc_refcount;
3282 	xvfsp.vfc_flags = vfsp->vfc_flags;
3283 	xvfsp.vfc_vfsops = 0;
3284 	xvfsp.vfc_next = 0;
3285 	return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp)));
3286 }
3287 #endif
3288 
3289 /*
3290  * Top level filesystem related information gathering.
3291  */
3292 static int
3293 sysctl_vfs_conflist(SYSCTL_HANDLER_ARGS)
3294 {
3295 	struct vfsconf *vfsp;
3296 	int error;
3297 
3298 	error = 0;
3299 	vfsconf_slock();
3300 	TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
3301 #ifdef COMPAT_FREEBSD32
3302 		if (req->flags & SCTL_MASK32)
3303 			error = vfsconf2x32(req, vfsp);
3304 		else
3305 #endif
3306 			error = vfsconf2x(req, vfsp);
3307 		if (error)
3308 			break;
3309 	}
3310 	vfsconf_sunlock();
3311 	return (error);
3312 }
3313 
3314 SYSCTL_PROC(_vfs, OID_AUTO, conflist, CTLTYPE_OPAQUE | CTLFLAG_RD |
3315     CTLFLAG_MPSAFE, NULL, 0, sysctl_vfs_conflist,
3316     "S,xvfsconf", "List of all configured filesystems");
3317 
3318 #ifndef BURN_BRIDGES
3319 static int	sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS);
3320 
3321 static int
3322 vfs_sysctl(SYSCTL_HANDLER_ARGS)
3323 {
3324 	int *name = (int *)arg1 - 1;	/* XXX */
3325 	u_int namelen = arg2 + 1;	/* XXX */
3326 	struct vfsconf *vfsp;
3327 
3328 	log(LOG_WARNING, "userland calling deprecated sysctl, "
3329 	    "please rebuild world\n");
3330 
3331 #if 1 || defined(COMPAT_PRELITE2)
3332 	/* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
3333 	if (namelen == 1)
3334 		return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
3335 #endif
3336 
3337 	switch (name[1]) {
3338 	case VFS_MAXTYPENUM:
3339 		if (namelen != 2)
3340 			return (ENOTDIR);
3341 		return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int)));
3342 	case VFS_CONF:
3343 		if (namelen != 3)
3344 			return (ENOTDIR);	/* overloaded */
3345 		vfsconf_slock();
3346 		TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
3347 			if (vfsp->vfc_typenum == name[2])
3348 				break;
3349 		}
3350 		vfsconf_sunlock();
3351 		if (vfsp == NULL)
3352 			return (EOPNOTSUPP);
3353 #ifdef COMPAT_FREEBSD32
3354 		if (req->flags & SCTL_MASK32)
3355 			return (vfsconf2x32(req, vfsp));
3356 		else
3357 #endif
3358 			return (vfsconf2x(req, vfsp));
3359 	}
3360 	return (EOPNOTSUPP);
3361 }
3362 
3363 static SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD | CTLFLAG_SKIP |
3364     CTLFLAG_MPSAFE, vfs_sysctl,
3365     "Generic filesystem");
3366 
3367 #if 1 || defined(COMPAT_PRELITE2)
3368 
3369 static int
3370 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
3371 {
3372 	int error;
3373 	struct vfsconf *vfsp;
3374 	struct ovfsconf ovfs;
3375 
3376 	vfsconf_slock();
3377 	TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
3378 		bzero(&ovfs, sizeof(ovfs));
3379 		ovfs.vfc_vfsops = vfsp->vfc_vfsops;	/* XXX used as flag */
3380 		strcpy(ovfs.vfc_name, vfsp->vfc_name);
3381 		ovfs.vfc_index = vfsp->vfc_typenum;
3382 		ovfs.vfc_refcount = vfsp->vfc_refcount;
3383 		ovfs.vfc_flags = vfsp->vfc_flags;
3384 		error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
3385 		if (error != 0) {
3386 			vfsconf_sunlock();
3387 			return (error);
3388 		}
3389 	}
3390 	vfsconf_sunlock();
3391 	return (0);
3392 }
3393 
3394 #endif /* 1 || COMPAT_PRELITE2 */
3395 #endif /* !BURN_BRIDGES */
3396 
3397 #define KINFO_VNODESLOP		10
3398 #ifdef notyet
3399 /*
3400  * Dump vnode list (via sysctl).
3401  */
3402 /* ARGSUSED */
3403 static int
3404 sysctl_vnode(SYSCTL_HANDLER_ARGS)
3405 {
3406 	struct xvnode *xvn;
3407 	struct mount *mp;
3408 	struct vnode *vp;
3409 	int error, len, n;
3410 
3411 	/*
3412 	 * Stale numvnodes access is not fatal here.
3413 	 */
3414 	req->lock = 0;
3415 	len = (numvnodes + KINFO_VNODESLOP) * sizeof *xvn;
3416 	if (!req->oldptr)
3417 		/* Make an estimate */
3418 		return (SYSCTL_OUT(req, 0, len));
3419 
3420 	error = sysctl_wire_old_buffer(req, 0);
3421 	if (error != 0)
3422 		return (error);
3423 	xvn = malloc(len, M_TEMP, M_ZERO | M_WAITOK);
3424 	n = 0;
3425 	mtx_lock(&mountlist_mtx);
3426 	TAILQ_FOREACH(mp, &mountlist, mnt_list) {
3427 		if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK))
3428 			continue;
3429 		MNT_ILOCK(mp);
3430 		TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
3431 			if (n == len)
3432 				break;
3433 			vref(vp);
3434 			xvn[n].xv_size = sizeof *xvn;
3435 			xvn[n].xv_vnode = vp;
3436 			xvn[n].xv_id = 0;	/* XXX compat */
3437 #define XV_COPY(field) xvn[n].xv_##field = vp->v_##field
3438 			XV_COPY(usecount);
3439 			XV_COPY(writecount);
3440 			XV_COPY(holdcnt);
3441 			XV_COPY(mount);
3442 			XV_COPY(numoutput);
3443 			XV_COPY(type);
3444 #undef XV_COPY
3445 			xvn[n].xv_flag = vp->v_vflag;
3446 
3447 			switch (vp->v_type) {
3448 			case VREG:
3449 			case VDIR:
3450 			case VLNK:
3451 				break;
3452 			case VBLK:
3453 			case VCHR:
3454 				if (vp->v_rdev == NULL) {
3455 					vrele(vp);
3456 					continue;
3457 				}
3458 				xvn[n].xv_dev = dev2udev(vp->v_rdev);
3459 				break;
3460 			case VSOCK:
3461 				xvn[n].xv_socket = vp->v_socket;
3462 				break;
3463 			case VFIFO:
3464 				xvn[n].xv_fifo = vp->v_fifoinfo;
3465 				break;
3466 			case VNON:
3467 			case VBAD:
3468 			default:
3469 				/* shouldn't happen? */
3470 				vrele(vp);
3471 				continue;
3472 			}
3473 			vrele(vp);
3474 			++n;
3475 		}
3476 		MNT_IUNLOCK(mp);
3477 		mtx_lock(&mountlist_mtx);
3478 		vfs_unbusy(mp);
3479 		if (n == len)
3480 			break;
3481 	}
3482 	mtx_unlock(&mountlist_mtx);
3483 
3484 	error = SYSCTL_OUT(req, xvn, n * sizeof *xvn);
3485 	free(xvn, M_TEMP);
3486 	return (error);
3487 }
3488 
3489 SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE | CTLFLAG_RD |
3490     CTLFLAG_MPSAFE, 0, 0, sysctl_vnode, "S,xvnode",
3491     "");
3492 #endif
3493 
3494 /*
3495  * Unmount all filesystems. The list is traversed in reverse order
3496  * of mounting to avoid dependencies.
3497  */
3498 void
3499 vfs_unmountall(void)
3500 {
3501 	struct mount *mp;
3502 	struct thread *td;
3503 	int error;
3504 
3505 	CTR1(KTR_VFS, "%s: unmounting all filesystems", __func__);
3506 	td = curthread;
3507 
3508 	/*
3509 	 * Since this only runs when rebooting, it is not interlocked.
3510 	 */
3511 	while(!TAILQ_EMPTY(&mountlist)) {
3512 		mp = TAILQ_LAST(&mountlist, mntlist);
3513 		vfs_ref(mp);
3514 		error = dounmount(mp, MNT_FORCE, td);
3515 		if (error != 0) {
3516 			TAILQ_REMOVE(&mountlist, mp, mnt_list);
3517 			/*
3518 			 * XXX: Due to the way in which we mount the root
3519 			 * file system off of devfs, devfs will generate a
3520 			 * "busy" warning when we try to unmount it before
3521 			 * the root.  Don't print a warning as a result in
3522 			 * order to avoid false positive errors that may
3523 			 * cause needless upset.
3524 			 */
3525 			if (strcmp(mp->mnt_vfc->vfc_name, "devfs") != 0) {
3526 				printf("unmount of %s failed (",
3527 				    mp->mnt_stat.f_mntonname);
3528 				if (error == EBUSY)
3529 					printf("BUSY)\n");
3530 				else
3531 					printf("%d)\n", error);
3532 			}
3533 		} else {
3534 			/* The unmount has removed mp from the mountlist */
3535 		}
3536 	}
3537 }
3538 
3539 /*
3540  * perform msync on all vnodes under a mount point
3541  * the mount point must be locked.
3542  */
3543 void
3544 vfs_msync(struct mount *mp, int flags)
3545 {
3546 	struct vnode *vp, *mvp;
3547 	struct vm_object *obj;
3548 
3549 	CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
3550 	MNT_VNODE_FOREACH_ACTIVE(vp, mp, mvp) {
3551 		obj = vp->v_object;
3552 		if (obj != NULL && (obj->flags & OBJ_MIGHTBEDIRTY) != 0 &&
3553 		    (flags == MNT_WAIT || VOP_ISLOCKED(vp) == 0)) {
3554 			if (!vget(vp,
3555 			    LK_EXCLUSIVE | LK_RETRY | LK_INTERLOCK,
3556 			    curthread)) {
3557 				if (vp->v_vflag & VV_NOSYNC) {	/* unlinked */
3558 					vput(vp);
3559 					continue;
3560 				}
3561 
3562 				obj = vp->v_object;
3563 				if (obj != NULL) {
3564 					VM_OBJECT_WLOCK(obj);
3565 					vm_object_page_clean(obj, 0, 0,
3566 					    flags == MNT_WAIT ?
3567 					    OBJPC_SYNC : OBJPC_NOSYNC);
3568 					VM_OBJECT_WUNLOCK(obj);
3569 				}
3570 				vput(vp);
3571 			}
3572 		} else
3573 			VI_UNLOCK(vp);
3574 	}
3575 }
3576 
3577 static void
3578 destroy_vpollinfo_free(struct vpollinfo *vi)
3579 {
3580 
3581 	knlist_destroy(&vi->vpi_selinfo.si_note);
3582 	mtx_destroy(&vi->vpi_lock);
3583 	uma_zfree(vnodepoll_zone, vi);
3584 }
3585 
3586 static void
3587 destroy_vpollinfo(struct vpollinfo *vi)
3588 {
3589 
3590 	knlist_clear(&vi->vpi_selinfo.si_note, 1);
3591 	seldrain(&vi->vpi_selinfo);
3592 	destroy_vpollinfo_free(vi);
3593 }
3594 
3595 /*
3596  * Initalize per-vnode helper structure to hold poll-related state.
3597  */
3598 void
3599 v_addpollinfo(struct vnode *vp)
3600 {
3601 	struct vpollinfo *vi;
3602 
3603 	if (vp->v_pollinfo != NULL)
3604 		return;
3605 	vi = uma_zalloc(vnodepoll_zone, M_WAITOK | M_ZERO);
3606 	mtx_init(&vi->vpi_lock, "vnode pollinfo", NULL, MTX_DEF);
3607 	knlist_init(&vi->vpi_selinfo.si_note, vp, vfs_knllock,
3608 	    vfs_knlunlock, vfs_knl_assert_locked, vfs_knl_assert_unlocked);
3609 	VI_LOCK(vp);
3610 	if (vp->v_pollinfo != NULL) {
3611 		VI_UNLOCK(vp);
3612 		destroy_vpollinfo_free(vi);
3613 		return;
3614 	}
3615 	vp->v_pollinfo = vi;
3616 	VI_UNLOCK(vp);
3617 }
3618 
3619 /*
3620  * Record a process's interest in events which might happen to
3621  * a vnode.  Because poll uses the historic select-style interface
3622  * internally, this routine serves as both the ``check for any
3623  * pending events'' and the ``record my interest in future events''
3624  * functions.  (These are done together, while the lock is held,
3625  * to avoid race conditions.)
3626  */
3627 int
3628 vn_pollrecord(struct vnode *vp, struct thread *td, int events)
3629 {
3630 
3631 	v_addpollinfo(vp);
3632 	mtx_lock(&vp->v_pollinfo->vpi_lock);
3633 	if (vp->v_pollinfo->vpi_revents & events) {
3634 		/*
3635 		 * This leaves events we are not interested
3636 		 * in available for the other process which
3637 		 * which presumably had requested them
3638 		 * (otherwise they would never have been
3639 		 * recorded).
3640 		 */
3641 		events &= vp->v_pollinfo->vpi_revents;
3642 		vp->v_pollinfo->vpi_revents &= ~events;
3643 
3644 		mtx_unlock(&vp->v_pollinfo->vpi_lock);
3645 		return (events);
3646 	}
3647 	vp->v_pollinfo->vpi_events |= events;
3648 	selrecord(td, &vp->v_pollinfo->vpi_selinfo);
3649 	mtx_unlock(&vp->v_pollinfo->vpi_lock);
3650 	return (0);
3651 }
3652 
3653 /*
3654  * Routine to create and manage a filesystem syncer vnode.
3655  */
3656 #define sync_close ((int (*)(struct  vop_close_args *))nullop)
3657 static int	sync_fsync(struct  vop_fsync_args *);
3658 static int	sync_inactive(struct  vop_inactive_args *);
3659 static int	sync_reclaim(struct  vop_reclaim_args *);
3660 
3661 static struct vop_vector sync_vnodeops = {
3662 	.vop_bypass =	VOP_EOPNOTSUPP,
3663 	.vop_close =	sync_close,		/* close */
3664 	.vop_fsync =	sync_fsync,		/* fsync */
3665 	.vop_inactive =	sync_inactive,	/* inactive */
3666 	.vop_reclaim =	sync_reclaim,	/* reclaim */
3667 	.vop_lock1 =	vop_stdlock,	/* lock */
3668 	.vop_unlock =	vop_stdunlock,	/* unlock */
3669 	.vop_islocked =	vop_stdislocked,	/* islocked */
3670 };
3671 
3672 /*
3673  * Create a new filesystem syncer vnode for the specified mount point.
3674  */
3675 void
3676 vfs_allocate_syncvnode(struct mount *mp)
3677 {
3678 	struct vnode *vp;
3679 	struct bufobj *bo;
3680 	static long start, incr, next;
3681 	int error;
3682 
3683 	/* Allocate a new vnode */
3684 	error = getnewvnode("syncer", mp, &sync_vnodeops, &vp);
3685 	if (error != 0)
3686 		panic("vfs_allocate_syncvnode: getnewvnode() failed");
3687 	vp->v_type = VNON;
3688 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3689 	vp->v_vflag |= VV_FORCEINSMQ;
3690 	error = insmntque(vp, mp);
3691 	if (error != 0)
3692 		panic("vfs_allocate_syncvnode: insmntque() failed");
3693 	vp->v_vflag &= ~VV_FORCEINSMQ;
3694 	VOP_UNLOCK(vp, 0);
3695 	/*
3696 	 * Place the vnode onto the syncer worklist. We attempt to
3697 	 * scatter them about on the list so that they will go off
3698 	 * at evenly distributed times even if all the filesystems
3699 	 * are mounted at once.
3700 	 */
3701 	next += incr;
3702 	if (next == 0 || next > syncer_maxdelay) {
3703 		start /= 2;
3704 		incr /= 2;
3705 		if (start == 0) {
3706 			start = syncer_maxdelay / 2;
3707 			incr = syncer_maxdelay;
3708 		}
3709 		next = start;
3710 	}
3711 	bo = &vp->v_bufobj;
3712 	BO_LOCK(bo);
3713 	vn_syncer_add_to_worklist(bo, syncdelay > 0 ? next % syncdelay : 0);
3714 	/* XXX - vn_syncer_add_to_worklist() also grabs and drops sync_mtx. */
3715 	mtx_lock(&sync_mtx);
3716 	sync_vnode_count++;
3717 	if (mp->mnt_syncer == NULL) {
3718 		mp->mnt_syncer = vp;
3719 		vp = NULL;
3720 	}
3721 	mtx_unlock(&sync_mtx);
3722 	BO_UNLOCK(bo);
3723 	if (vp != NULL) {
3724 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3725 		vgone(vp);
3726 		vput(vp);
3727 	}
3728 }
3729 
3730 void
3731 vfs_deallocate_syncvnode(struct mount *mp)
3732 {
3733 	struct vnode *vp;
3734 
3735 	mtx_lock(&sync_mtx);
3736 	vp = mp->mnt_syncer;
3737 	if (vp != NULL)
3738 		mp->mnt_syncer = NULL;
3739 	mtx_unlock(&sync_mtx);
3740 	if (vp != NULL)
3741 		vrele(vp);
3742 }
3743 
3744 /*
3745  * Do a lazy sync of the filesystem.
3746  */
3747 static int
3748 sync_fsync(struct vop_fsync_args *ap)
3749 {
3750 	struct vnode *syncvp = ap->a_vp;
3751 	struct mount *mp = syncvp->v_mount;
3752 	int error, save;
3753 	struct bufobj *bo;
3754 
3755 	/*
3756 	 * We only need to do something if this is a lazy evaluation.
3757 	 */
3758 	if (ap->a_waitfor != MNT_LAZY)
3759 		return (0);
3760 
3761 	/*
3762 	 * Move ourselves to the back of the sync list.
3763 	 */
3764 	bo = &syncvp->v_bufobj;
3765 	BO_LOCK(bo);
3766 	vn_syncer_add_to_worklist(bo, syncdelay);
3767 	BO_UNLOCK(bo);
3768 
3769 	/*
3770 	 * Walk the list of vnodes pushing all that are dirty and
3771 	 * not already on the sync list.
3772 	 */
3773 	if (vfs_busy(mp, MBF_NOWAIT) != 0)
3774 		return (0);
3775 	if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) {
3776 		vfs_unbusy(mp);
3777 		return (0);
3778 	}
3779 	save = curthread_pflags_set(TDP_SYNCIO);
3780 	vfs_msync(mp, MNT_NOWAIT);
3781 	error = VFS_SYNC(mp, MNT_LAZY);
3782 	curthread_pflags_restore(save);
3783 	vn_finished_write(mp);
3784 	vfs_unbusy(mp);
3785 	return (error);
3786 }
3787 
3788 /*
3789  * The syncer vnode is no referenced.
3790  */
3791 static int
3792 sync_inactive(struct vop_inactive_args *ap)
3793 {
3794 
3795 	vgone(ap->a_vp);
3796 	return (0);
3797 }
3798 
3799 /*
3800  * The syncer vnode is no longer needed and is being decommissioned.
3801  *
3802  * Modifications to the worklist must be protected by sync_mtx.
3803  */
3804 static int
3805 sync_reclaim(struct vop_reclaim_args *ap)
3806 {
3807 	struct vnode *vp = ap->a_vp;
3808 	struct bufobj *bo;
3809 
3810 	bo = &vp->v_bufobj;
3811 	BO_LOCK(bo);
3812 	mtx_lock(&sync_mtx);
3813 	if (vp->v_mount->mnt_syncer == vp)
3814 		vp->v_mount->mnt_syncer = NULL;
3815 	if (bo->bo_flag & BO_ONWORKLST) {
3816 		LIST_REMOVE(bo, bo_synclist);
3817 		syncer_worklist_len--;
3818 		sync_vnode_count--;
3819 		bo->bo_flag &= ~BO_ONWORKLST;
3820 	}
3821 	mtx_unlock(&sync_mtx);
3822 	BO_UNLOCK(bo);
3823 
3824 	return (0);
3825 }
3826 
3827 /*
3828  * Check if vnode represents a disk device
3829  */
3830 int
3831 vn_isdisk(struct vnode *vp, int *errp)
3832 {
3833 	int error;
3834 
3835 	if (vp->v_type != VCHR) {
3836 		error = ENOTBLK;
3837 		goto out;
3838 	}
3839 	error = 0;
3840 	dev_lock();
3841 	if (vp->v_rdev == NULL)
3842 		error = ENXIO;
3843 	else if (vp->v_rdev->si_devsw == NULL)
3844 		error = ENXIO;
3845 	else if (!(vp->v_rdev->si_devsw->d_flags & D_DISK))
3846 		error = ENOTBLK;
3847 	dev_unlock();
3848 out:
3849 	if (errp != NULL)
3850 		*errp = error;
3851 	return (error == 0);
3852 }
3853 
3854 /*
3855  * Common filesystem object access control check routine.  Accepts a
3856  * vnode's type, "mode", uid and gid, requested access mode, credentials,
3857  * and optional call-by-reference privused argument allowing vaccess()
3858  * to indicate to the caller whether privilege was used to satisfy the
3859  * request (obsoleted).  Returns 0 on success, or an errno on failure.
3860  */
3861 int
3862 vaccess(enum vtype type, mode_t file_mode, uid_t file_uid, gid_t file_gid,
3863     accmode_t accmode, struct ucred *cred, int *privused)
3864 {
3865 	accmode_t dac_granted;
3866 	accmode_t priv_granted;
3867 
3868 	KASSERT((accmode & ~(VEXEC | VWRITE | VREAD | VADMIN | VAPPEND)) == 0,
3869 	    ("invalid bit in accmode"));
3870 	KASSERT((accmode & VAPPEND) == 0 || (accmode & VWRITE),
3871 	    ("VAPPEND without VWRITE"));
3872 
3873 	/*
3874 	 * Look for a normal, non-privileged way to access the file/directory
3875 	 * as requested.  If it exists, go with that.
3876 	 */
3877 
3878 	if (privused != NULL)
3879 		*privused = 0;
3880 
3881 	dac_granted = 0;
3882 
3883 	/* Check the owner. */
3884 	if (cred->cr_uid == file_uid) {
3885 		dac_granted |= VADMIN;
3886 		if (file_mode & S_IXUSR)
3887 			dac_granted |= VEXEC;
3888 		if (file_mode & S_IRUSR)
3889 			dac_granted |= VREAD;
3890 		if (file_mode & S_IWUSR)
3891 			dac_granted |= (VWRITE | VAPPEND);
3892 
3893 		if ((accmode & dac_granted) == accmode)
3894 			return (0);
3895 
3896 		goto privcheck;
3897 	}
3898 
3899 	/* Otherwise, check the groups (first match) */
3900 	if (groupmember(file_gid, cred)) {
3901 		if (file_mode & S_IXGRP)
3902 			dac_granted |= VEXEC;
3903 		if (file_mode & S_IRGRP)
3904 			dac_granted |= VREAD;
3905 		if (file_mode & S_IWGRP)
3906 			dac_granted |= (VWRITE | VAPPEND);
3907 
3908 		if ((accmode & dac_granted) == accmode)
3909 			return (0);
3910 
3911 		goto privcheck;
3912 	}
3913 
3914 	/* Otherwise, check everyone else. */
3915 	if (file_mode & S_IXOTH)
3916 		dac_granted |= VEXEC;
3917 	if (file_mode & S_IROTH)
3918 		dac_granted |= VREAD;
3919 	if (file_mode & S_IWOTH)
3920 		dac_granted |= (VWRITE | VAPPEND);
3921 	if ((accmode & dac_granted) == accmode)
3922 		return (0);
3923 
3924 privcheck:
3925 	/*
3926 	 * Build a privilege mask to determine if the set of privileges
3927 	 * satisfies the requirements when combined with the granted mask
3928 	 * from above.  For each privilege, if the privilege is required,
3929 	 * bitwise or the request type onto the priv_granted mask.
3930 	 */
3931 	priv_granted = 0;
3932 
3933 	if (type == VDIR) {
3934 		/*
3935 		 * For directories, use PRIV_VFS_LOOKUP to satisfy VEXEC
3936 		 * requests, instead of PRIV_VFS_EXEC.
3937 		 */
3938 		if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
3939 		    !priv_check_cred(cred, PRIV_VFS_LOOKUP, 0))
3940 			priv_granted |= VEXEC;
3941 	} else {
3942 		/*
3943 		 * Ensure that at least one execute bit is on. Otherwise,
3944 		 * a privileged user will always succeed, and we don't want
3945 		 * this to happen unless the file really is executable.
3946 		 */
3947 		if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
3948 		    (file_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0 &&
3949 		    !priv_check_cred(cred, PRIV_VFS_EXEC, 0))
3950 			priv_granted |= VEXEC;
3951 	}
3952 
3953 	if ((accmode & VREAD) && ((dac_granted & VREAD) == 0) &&
3954 	    !priv_check_cred(cred, PRIV_VFS_READ, 0))
3955 		priv_granted |= VREAD;
3956 
3957 	if ((accmode & VWRITE) && ((dac_granted & VWRITE) == 0) &&
3958 	    !priv_check_cred(cred, PRIV_VFS_WRITE, 0))
3959 		priv_granted |= (VWRITE | VAPPEND);
3960 
3961 	if ((accmode & VADMIN) && ((dac_granted & VADMIN) == 0) &&
3962 	    !priv_check_cred(cred, PRIV_VFS_ADMIN, 0))
3963 		priv_granted |= VADMIN;
3964 
3965 	if ((accmode & (priv_granted | dac_granted)) == accmode) {
3966 		/* XXX audit: privilege used */
3967 		if (privused != NULL)
3968 			*privused = 1;
3969 		return (0);
3970 	}
3971 
3972 	return ((accmode & VADMIN) ? EPERM : EACCES);
3973 }
3974 
3975 /*
3976  * Credential check based on process requesting service, and per-attribute
3977  * permissions.
3978  */
3979 int
3980 extattr_check_cred(struct vnode *vp, int attrnamespace, struct ucred *cred,
3981     struct thread *td, accmode_t accmode)
3982 {
3983 
3984 	/*
3985 	 * Kernel-invoked always succeeds.
3986 	 */
3987 	if (cred == NOCRED)
3988 		return (0);
3989 
3990 	/*
3991 	 * Do not allow privileged processes in jail to directly manipulate
3992 	 * system attributes.
3993 	 */
3994 	switch (attrnamespace) {
3995 	case EXTATTR_NAMESPACE_SYSTEM:
3996 		/* Potentially should be: return (EPERM); */
3997 		return (priv_check_cred(cred, PRIV_VFS_EXTATTR_SYSTEM, 0));
3998 	case EXTATTR_NAMESPACE_USER:
3999 		return (VOP_ACCESS(vp, accmode, cred, td));
4000 	default:
4001 		return (EPERM);
4002 	}
4003 }
4004 
4005 #ifdef DEBUG_VFS_LOCKS
4006 /*
4007  * This only exists to supress warnings from unlocked specfs accesses.  It is
4008  * no longer ok to have an unlocked VFS.
4009  */
4010 #define	IGNORE_LOCK(vp) (panicstr != NULL || (vp) == NULL ||		\
4011 	(vp)->v_type == VCHR ||	(vp)->v_type == VBAD)
4012 
4013 int vfs_badlock_ddb = 1;	/* Drop into debugger on violation. */
4014 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_ddb, CTLFLAG_RW, &vfs_badlock_ddb, 0,
4015     "Drop into debugger on lock violation");
4016 
4017 int vfs_badlock_mutex = 1;	/* Check for interlock across VOPs. */
4018 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_mutex, CTLFLAG_RW, &vfs_badlock_mutex,
4019     0, "Check for interlock across VOPs");
4020 
4021 int vfs_badlock_print = 1;	/* Print lock violations. */
4022 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_print, CTLFLAG_RW, &vfs_badlock_print,
4023     0, "Print lock violations");
4024 
4025 #ifdef KDB
4026 int vfs_badlock_backtrace = 1;	/* Print backtrace at lock violations. */
4027 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_backtrace, CTLFLAG_RW,
4028     &vfs_badlock_backtrace, 0, "Print backtrace at lock violations");
4029 #endif
4030 
4031 static void
4032 vfs_badlock(const char *msg, const char *str, struct vnode *vp)
4033 {
4034 
4035 #ifdef KDB
4036 	if (vfs_badlock_backtrace)
4037 		kdb_backtrace();
4038 #endif
4039 	if (vfs_badlock_print)
4040 		printf("%s: %p %s\n", str, (void *)vp, msg);
4041 	if (vfs_badlock_ddb)
4042 		kdb_enter(KDB_WHY_VFSLOCK, "lock violation");
4043 }
4044 
4045 void
4046 assert_vi_locked(struct vnode *vp, const char *str)
4047 {
4048 
4049 	if (vfs_badlock_mutex && !mtx_owned(VI_MTX(vp)))
4050 		vfs_badlock("interlock is not locked but should be", str, vp);
4051 }
4052 
4053 void
4054 assert_vi_unlocked(struct vnode *vp, const char *str)
4055 {
4056 
4057 	if (vfs_badlock_mutex && mtx_owned(VI_MTX(vp)))
4058 		vfs_badlock("interlock is locked but should not be", str, vp);
4059 }
4060 
4061 void
4062 assert_vop_locked(struct vnode *vp, const char *str)
4063 {
4064 	int locked;
4065 
4066 	if (!IGNORE_LOCK(vp)) {
4067 		locked = VOP_ISLOCKED(vp);
4068 		if (locked == 0 || locked == LK_EXCLOTHER)
4069 			vfs_badlock("is not locked but should be", str, vp);
4070 	}
4071 }
4072 
4073 void
4074 assert_vop_unlocked(struct vnode *vp, const char *str)
4075 {
4076 
4077 	if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) == LK_EXCLUSIVE)
4078 		vfs_badlock("is locked but should not be", str, vp);
4079 }
4080 
4081 void
4082 assert_vop_elocked(struct vnode *vp, const char *str)
4083 {
4084 
4085 	if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_EXCLUSIVE)
4086 		vfs_badlock("is not exclusive locked but should be", str, vp);
4087 }
4088 
4089 #if 0
4090 void
4091 assert_vop_elocked_other(struct vnode *vp, const char *str)
4092 {
4093 
4094 	if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_EXCLOTHER)
4095 		vfs_badlock("is not exclusive locked by another thread",
4096 		    str, vp);
4097 }
4098 
4099 void
4100 assert_vop_slocked(struct vnode *vp, const char *str)
4101 {
4102 
4103 	if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_SHARED)
4104 		vfs_badlock("is not locked shared but should be", str, vp);
4105 }
4106 #endif /* 0 */
4107 #endif /* DEBUG_VFS_LOCKS */
4108 
4109 void
4110 vop_rename_fail(struct vop_rename_args *ap)
4111 {
4112 
4113 	if (ap->a_tvp != NULL)
4114 		vput(ap->a_tvp);
4115 	if (ap->a_tdvp == ap->a_tvp)
4116 		vrele(ap->a_tdvp);
4117 	else
4118 		vput(ap->a_tdvp);
4119 	vrele(ap->a_fdvp);
4120 	vrele(ap->a_fvp);
4121 }
4122 
4123 void
4124 vop_rename_pre(void *ap)
4125 {
4126 	struct vop_rename_args *a = ap;
4127 
4128 #ifdef DEBUG_VFS_LOCKS
4129 	if (a->a_tvp)
4130 		ASSERT_VI_UNLOCKED(a->a_tvp, "VOP_RENAME");
4131 	ASSERT_VI_UNLOCKED(a->a_tdvp, "VOP_RENAME");
4132 	ASSERT_VI_UNLOCKED(a->a_fvp, "VOP_RENAME");
4133 	ASSERT_VI_UNLOCKED(a->a_fdvp, "VOP_RENAME");
4134 
4135 	/* Check the source (from). */
4136 	if (a->a_tdvp->v_vnlock != a->a_fdvp->v_vnlock &&
4137 	    (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fdvp->v_vnlock))
4138 		ASSERT_VOP_UNLOCKED(a->a_fdvp, "vop_rename: fdvp locked");
4139 	if (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fvp->v_vnlock)
4140 		ASSERT_VOP_UNLOCKED(a->a_fvp, "vop_rename: fvp locked");
4141 
4142 	/* Check the target. */
4143 	if (a->a_tvp)
4144 		ASSERT_VOP_LOCKED(a->a_tvp, "vop_rename: tvp not locked");
4145 	ASSERT_VOP_LOCKED(a->a_tdvp, "vop_rename: tdvp not locked");
4146 #endif
4147 	if (a->a_tdvp != a->a_fdvp)
4148 		vhold(a->a_fdvp);
4149 	if (a->a_tvp != a->a_fvp)
4150 		vhold(a->a_fvp);
4151 	vhold(a->a_tdvp);
4152 	if (a->a_tvp)
4153 		vhold(a->a_tvp);
4154 }
4155 
4156 void
4157 vop_strategy_pre(void *ap)
4158 {
4159 #ifdef DEBUG_VFS_LOCKS
4160 	struct vop_strategy_args *a;
4161 	struct buf *bp;
4162 
4163 	a = ap;
4164 	bp = a->a_bp;
4165 
4166 	/*
4167 	 * Cluster ops lock their component buffers but not the IO container.
4168 	 */
4169 	if ((bp->b_flags & B_CLUSTER) != 0)
4170 		return;
4171 
4172 	if (panicstr == NULL && !BUF_ISLOCKED(bp)) {
4173 		if (vfs_badlock_print)
4174 			printf(
4175 			    "VOP_STRATEGY: bp is not locked but should be\n");
4176 		if (vfs_badlock_ddb)
4177 			kdb_enter(KDB_WHY_VFSLOCK, "lock violation");
4178 	}
4179 #endif
4180 }
4181 
4182 void
4183 vop_lock_pre(void *ap)
4184 {
4185 #ifdef DEBUG_VFS_LOCKS
4186 	struct vop_lock1_args *a = ap;
4187 
4188 	if ((a->a_flags & LK_INTERLOCK) == 0)
4189 		ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
4190 	else
4191 		ASSERT_VI_LOCKED(a->a_vp, "VOP_LOCK");
4192 #endif
4193 }
4194 
4195 void
4196 vop_lock_post(void *ap, int rc)
4197 {
4198 #ifdef DEBUG_VFS_LOCKS
4199 	struct vop_lock1_args *a = ap;
4200 
4201 	ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
4202 	if (rc == 0 && (a->a_flags & LK_EXCLOTHER) == 0)
4203 		ASSERT_VOP_LOCKED(a->a_vp, "VOP_LOCK");
4204 #endif
4205 }
4206 
4207 void
4208 vop_unlock_pre(void *ap)
4209 {
4210 #ifdef DEBUG_VFS_LOCKS
4211 	struct vop_unlock_args *a = ap;
4212 
4213 	if (a->a_flags & LK_INTERLOCK)
4214 		ASSERT_VI_LOCKED(a->a_vp, "VOP_UNLOCK");
4215 	ASSERT_VOP_LOCKED(a->a_vp, "VOP_UNLOCK");
4216 #endif
4217 }
4218 
4219 void
4220 vop_unlock_post(void *ap, int rc)
4221 {
4222 #ifdef DEBUG_VFS_LOCKS
4223 	struct vop_unlock_args *a = ap;
4224 
4225 	if (a->a_flags & LK_INTERLOCK)
4226 		ASSERT_VI_UNLOCKED(a->a_vp, "VOP_UNLOCK");
4227 #endif
4228 }
4229 
4230 void
4231 vop_create_post(void *ap, int rc)
4232 {
4233 	struct vop_create_args *a = ap;
4234 
4235 	if (!rc)
4236 		VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4237 }
4238 
4239 void
4240 vop_deleteextattr_post(void *ap, int rc)
4241 {
4242 	struct vop_deleteextattr_args *a = ap;
4243 
4244 	if (!rc)
4245 		VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
4246 }
4247 
4248 void
4249 vop_link_post(void *ap, int rc)
4250 {
4251 	struct vop_link_args *a = ap;
4252 
4253 	if (!rc) {
4254 		VFS_KNOTE_LOCKED(a->a_vp, NOTE_LINK);
4255 		VFS_KNOTE_LOCKED(a->a_tdvp, NOTE_WRITE);
4256 	}
4257 }
4258 
4259 void
4260 vop_mkdir_post(void *ap, int rc)
4261 {
4262 	struct vop_mkdir_args *a = ap;
4263 
4264 	if (!rc)
4265 		VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK);
4266 }
4267 
4268 void
4269 vop_mknod_post(void *ap, int rc)
4270 {
4271 	struct vop_mknod_args *a = ap;
4272 
4273 	if (!rc)
4274 		VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4275 }
4276 
4277 void
4278 vop_remove_post(void *ap, int rc)
4279 {
4280 	struct vop_remove_args *a = ap;
4281 
4282 	if (!rc) {
4283 		VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4284 		VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE);
4285 	}
4286 }
4287 
4288 void
4289 vop_rename_post(void *ap, int rc)
4290 {
4291 	struct vop_rename_args *a = ap;
4292 
4293 	if (!rc) {
4294 		VFS_KNOTE_UNLOCKED(a->a_fdvp, NOTE_WRITE);
4295 		VFS_KNOTE_UNLOCKED(a->a_tdvp, NOTE_WRITE);
4296 		VFS_KNOTE_UNLOCKED(a->a_fvp, NOTE_RENAME);
4297 		if (a->a_tvp)
4298 			VFS_KNOTE_UNLOCKED(a->a_tvp, NOTE_DELETE);
4299 	}
4300 	if (a->a_tdvp != a->a_fdvp)
4301 		vdrop(a->a_fdvp);
4302 	if (a->a_tvp != a->a_fvp)
4303 		vdrop(a->a_fvp);
4304 	vdrop(a->a_tdvp);
4305 	if (a->a_tvp)
4306 		vdrop(a->a_tvp);
4307 }
4308 
4309 void
4310 vop_rmdir_post(void *ap, int rc)
4311 {
4312 	struct vop_rmdir_args *a = ap;
4313 
4314 	if (!rc) {
4315 		VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK);
4316 		VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE);
4317 	}
4318 }
4319 
4320 void
4321 vop_setattr_post(void *ap, int rc)
4322 {
4323 	struct vop_setattr_args *a = ap;
4324 
4325 	if (!rc)
4326 		VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
4327 }
4328 
4329 void
4330 vop_setextattr_post(void *ap, int rc)
4331 {
4332 	struct vop_setextattr_args *a = ap;
4333 
4334 	if (!rc)
4335 		VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
4336 }
4337 
4338 void
4339 vop_symlink_post(void *ap, int rc)
4340 {
4341 	struct vop_symlink_args *a = ap;
4342 
4343 	if (!rc)
4344 		VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4345 }
4346 
4347 static struct knlist fs_knlist;
4348 
4349 static void
4350 vfs_event_init(void *arg)
4351 {
4352 	knlist_init_mtx(&fs_knlist, NULL);
4353 }
4354 /* XXX - correct order? */
4355 SYSINIT(vfs_knlist, SI_SUB_VFS, SI_ORDER_ANY, vfs_event_init, NULL);
4356 
4357 void
4358 vfs_event_signal(fsid_t *fsid, uint32_t event, intptr_t data __unused)
4359 {
4360 
4361 	KNOTE_UNLOCKED(&fs_knlist, event);
4362 }
4363 
4364 static int	filt_fsattach(struct knote *kn);
4365 static void	filt_fsdetach(struct knote *kn);
4366 static int	filt_fsevent(struct knote *kn, long hint);
4367 
4368 struct filterops fs_filtops = {
4369 	.f_isfd = 0,
4370 	.f_attach = filt_fsattach,
4371 	.f_detach = filt_fsdetach,
4372 	.f_event = filt_fsevent
4373 };
4374 
4375 static int
4376 filt_fsattach(struct knote *kn)
4377 {
4378 
4379 	kn->kn_flags |= EV_CLEAR;
4380 	knlist_add(&fs_knlist, kn, 0);
4381 	return (0);
4382 }
4383 
4384 static void
4385 filt_fsdetach(struct knote *kn)
4386 {
4387 
4388 	knlist_remove(&fs_knlist, kn, 0);
4389 }
4390 
4391 static int
4392 filt_fsevent(struct knote *kn, long hint)
4393 {
4394 
4395 	kn->kn_fflags |= hint;
4396 	return (kn->kn_fflags != 0);
4397 }
4398 
4399 static int
4400 sysctl_vfs_ctl(SYSCTL_HANDLER_ARGS)
4401 {
4402 	struct vfsidctl vc;
4403 	int error;
4404 	struct mount *mp;
4405 
4406 	error = SYSCTL_IN(req, &vc, sizeof(vc));
4407 	if (error)
4408 		return (error);
4409 	if (vc.vc_vers != VFS_CTL_VERS1)
4410 		return (EINVAL);
4411 	mp = vfs_getvfs(&vc.vc_fsid);
4412 	if (mp == NULL)
4413 		return (ENOENT);
4414 	/* ensure that a specific sysctl goes to the right filesystem. */
4415 	if (strcmp(vc.vc_fstypename, "*") != 0 &&
4416 	    strcmp(vc.vc_fstypename, mp->mnt_vfc->vfc_name) != 0) {
4417 		vfs_rel(mp);
4418 		return (EINVAL);
4419 	}
4420 	VCTLTOREQ(&vc, req);
4421 	error = VFS_SYSCTL(mp, vc.vc_op, req);
4422 	vfs_rel(mp);
4423 	return (error);
4424 }
4425 
4426 SYSCTL_PROC(_vfs, OID_AUTO, ctl, CTLTYPE_OPAQUE | CTLFLAG_WR,
4427     NULL, 0, sysctl_vfs_ctl, "",
4428     "Sysctl by fsid");
4429 
4430 /*
4431  * Function to initialize a va_filerev field sensibly.
4432  * XXX: Wouldn't a random number make a lot more sense ??
4433  */
4434 u_quad_t
4435 init_va_filerev(void)
4436 {
4437 	struct bintime bt;
4438 
4439 	getbinuptime(&bt);
4440 	return (((u_quad_t)bt.sec << 32LL) | (bt.frac >> 32LL));
4441 }
4442 
4443 static int	filt_vfsread(struct knote *kn, long hint);
4444 static int	filt_vfswrite(struct knote *kn, long hint);
4445 static int	filt_vfsvnode(struct knote *kn, long hint);
4446 static void	filt_vfsdetach(struct knote *kn);
4447 static struct filterops vfsread_filtops = {
4448 	.f_isfd = 1,
4449 	.f_detach = filt_vfsdetach,
4450 	.f_event = filt_vfsread
4451 };
4452 static struct filterops vfswrite_filtops = {
4453 	.f_isfd = 1,
4454 	.f_detach = filt_vfsdetach,
4455 	.f_event = filt_vfswrite
4456 };
4457 static struct filterops vfsvnode_filtops = {
4458 	.f_isfd = 1,
4459 	.f_detach = filt_vfsdetach,
4460 	.f_event = filt_vfsvnode
4461 };
4462 
4463 static void
4464 vfs_knllock(void *arg)
4465 {
4466 	struct vnode *vp = arg;
4467 
4468 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4469 }
4470 
4471 static void
4472 vfs_knlunlock(void *arg)
4473 {
4474 	struct vnode *vp = arg;
4475 
4476 	VOP_UNLOCK(vp, 0);
4477 }
4478 
4479 static void
4480 vfs_knl_assert_locked(void *arg)
4481 {
4482 #ifdef DEBUG_VFS_LOCKS
4483 	struct vnode *vp = arg;
4484 
4485 	ASSERT_VOP_LOCKED(vp, "vfs_knl_assert_locked");
4486 #endif
4487 }
4488 
4489 static void
4490 vfs_knl_assert_unlocked(void *arg)
4491 {
4492 #ifdef DEBUG_VFS_LOCKS
4493 	struct vnode *vp = arg;
4494 
4495 	ASSERT_VOP_UNLOCKED(vp, "vfs_knl_assert_unlocked");
4496 #endif
4497 }
4498 
4499 int
4500 vfs_kqfilter(struct vop_kqfilter_args *ap)
4501 {
4502 	struct vnode *vp = ap->a_vp;
4503 	struct knote *kn = ap->a_kn;
4504 	struct knlist *knl;
4505 
4506 	switch (kn->kn_filter) {
4507 	case EVFILT_READ:
4508 		kn->kn_fop = &vfsread_filtops;
4509 		break;
4510 	case EVFILT_WRITE:
4511 		kn->kn_fop = &vfswrite_filtops;
4512 		break;
4513 	case EVFILT_VNODE:
4514 		kn->kn_fop = &vfsvnode_filtops;
4515 		break;
4516 	default:
4517 		return (EINVAL);
4518 	}
4519 
4520 	kn->kn_hook = (caddr_t)vp;
4521 
4522 	v_addpollinfo(vp);
4523 	if (vp->v_pollinfo == NULL)
4524 		return (ENOMEM);
4525 	knl = &vp->v_pollinfo->vpi_selinfo.si_note;
4526 	vhold(vp);
4527 	knlist_add(knl, kn, 0);
4528 
4529 	return (0);
4530 }
4531 
4532 /*
4533  * Detach knote from vnode
4534  */
4535 static void
4536 filt_vfsdetach(struct knote *kn)
4537 {
4538 	struct vnode *vp = (struct vnode *)kn->kn_hook;
4539 
4540 	KASSERT(vp->v_pollinfo != NULL, ("Missing v_pollinfo"));
4541 	knlist_remove(&vp->v_pollinfo->vpi_selinfo.si_note, kn, 0);
4542 	vdrop(vp);
4543 }
4544 
4545 /*ARGSUSED*/
4546 static int
4547 filt_vfsread(struct knote *kn, long hint)
4548 {
4549 	struct vnode *vp = (struct vnode *)kn->kn_hook;
4550 	struct vattr va;
4551 	int res;
4552 
4553 	/*
4554 	 * filesystem is gone, so set the EOF flag and schedule
4555 	 * the knote for deletion.
4556 	 */
4557 	if (hint == NOTE_REVOKE) {
4558 		VI_LOCK(vp);
4559 		kn->kn_flags |= (EV_EOF | EV_ONESHOT);
4560 		VI_UNLOCK(vp);
4561 		return (1);
4562 	}
4563 
4564 	if (VOP_GETATTR(vp, &va, curthread->td_ucred))
4565 		return (0);
4566 
4567 	VI_LOCK(vp);
4568 	kn->kn_data = va.va_size - kn->kn_fp->f_offset;
4569 	res = (kn->kn_data != 0);
4570 	VI_UNLOCK(vp);
4571 	return (res);
4572 }
4573 
4574 /*ARGSUSED*/
4575 static int
4576 filt_vfswrite(struct knote *kn, long hint)
4577 {
4578 	struct vnode *vp = (struct vnode *)kn->kn_hook;
4579 
4580 	VI_LOCK(vp);
4581 
4582 	/*
4583 	 * filesystem is gone, so set the EOF flag and schedule
4584 	 * the knote for deletion.
4585 	 */
4586 	if (hint == NOTE_REVOKE)
4587 		kn->kn_flags |= (EV_EOF | EV_ONESHOT);
4588 
4589 	kn->kn_data = 0;
4590 	VI_UNLOCK(vp);
4591 	return (1);
4592 }
4593 
4594 static int
4595 filt_vfsvnode(struct knote *kn, long hint)
4596 {
4597 	struct vnode *vp = (struct vnode *)kn->kn_hook;
4598 	int res;
4599 
4600 	VI_LOCK(vp);
4601 	if (kn->kn_sfflags & hint)
4602 		kn->kn_fflags |= hint;
4603 	if (hint == NOTE_REVOKE) {
4604 		kn->kn_flags |= EV_EOF;
4605 		VI_UNLOCK(vp);
4606 		return (1);
4607 	}
4608 	res = (kn->kn_fflags != 0);
4609 	VI_UNLOCK(vp);
4610 	return (res);
4611 }
4612 
4613 int
4614 vfs_read_dirent(struct vop_readdir_args *ap, struct dirent *dp, off_t off)
4615 {
4616 	int error;
4617 
4618 	if (dp->d_reclen > ap->a_uio->uio_resid)
4619 		return (ENAMETOOLONG);
4620 	error = uiomove(dp, dp->d_reclen, ap->a_uio);
4621 	if (error) {
4622 		if (ap->a_ncookies != NULL) {
4623 			if (ap->a_cookies != NULL)
4624 				free(ap->a_cookies, M_TEMP);
4625 			ap->a_cookies = NULL;
4626 			*ap->a_ncookies = 0;
4627 		}
4628 		return (error);
4629 	}
4630 	if (ap->a_ncookies == NULL)
4631 		return (0);
4632 
4633 	KASSERT(ap->a_cookies,
4634 	    ("NULL ap->a_cookies value with non-NULL ap->a_ncookies!"));
4635 
4636 	*ap->a_cookies = realloc(*ap->a_cookies,
4637 	    (*ap->a_ncookies + 1) * sizeof(u_long), M_TEMP, M_WAITOK | M_ZERO);
4638 	(*ap->a_cookies)[*ap->a_ncookies] = off;
4639 	return (0);
4640 }
4641 
4642 /*
4643  * Mark for update the access time of the file if the filesystem
4644  * supports VOP_MARKATIME.  This functionality is used by execve and
4645  * mmap, so we want to avoid the I/O implied by directly setting
4646  * va_atime for the sake of efficiency.
4647  */
4648 void
4649 vfs_mark_atime(struct vnode *vp, struct ucred *cred)
4650 {
4651 	struct mount *mp;
4652 
4653 	mp = vp->v_mount;
4654 	ASSERT_VOP_LOCKED(vp, "vfs_mark_atime");
4655 	if (mp != NULL && (mp->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0)
4656 		(void)VOP_MARKATIME(vp);
4657 }
4658 
4659 /*
4660  * The purpose of this routine is to remove granularity from accmode_t,
4661  * reducing it into standard unix access bits - VEXEC, VREAD, VWRITE,
4662  * VADMIN and VAPPEND.
4663  *
4664  * If it returns 0, the caller is supposed to continue with the usual
4665  * access checks using 'accmode' as modified by this routine.  If it
4666  * returns nonzero value, the caller is supposed to return that value
4667  * as errno.
4668  *
4669  * Note that after this routine runs, accmode may be zero.
4670  */
4671 int
4672 vfs_unixify_accmode(accmode_t *accmode)
4673 {
4674 	/*
4675 	 * There is no way to specify explicit "deny" rule using
4676 	 * file mode or POSIX.1e ACLs.
4677 	 */
4678 	if (*accmode & VEXPLICIT_DENY) {
4679 		*accmode = 0;
4680 		return (0);
4681 	}
4682 
4683 	/*
4684 	 * None of these can be translated into usual access bits.
4685 	 * Also, the common case for NFSv4 ACLs is to not contain
4686 	 * either of these bits. Caller should check for VWRITE
4687 	 * on the containing directory instead.
4688 	 */
4689 	if (*accmode & (VDELETE_CHILD | VDELETE))
4690 		return (EPERM);
4691 
4692 	if (*accmode & VADMIN_PERMS) {
4693 		*accmode &= ~VADMIN_PERMS;
4694 		*accmode |= VADMIN;
4695 	}
4696 
4697 	/*
4698 	 * There is no way to deny VREAD_ATTRIBUTES, VREAD_ACL
4699 	 * or VSYNCHRONIZE using file mode or POSIX.1e ACL.
4700 	 */
4701 	*accmode &= ~(VSTAT_PERMS | VSYNCHRONIZE);
4702 
4703 	return (0);
4704 }
4705 
4706 /*
4707  * These are helper functions for filesystems to traverse all
4708  * their vnodes.  See MNT_VNODE_FOREACH_ALL() in sys/mount.h.
4709  *
4710  * This interface replaces MNT_VNODE_FOREACH.
4711  */
4712 
4713 MALLOC_DEFINE(M_VNODE_MARKER, "vnodemarker", "vnode marker");
4714 
4715 struct vnode *
4716 __mnt_vnode_next_all(struct vnode **mvp, struct mount *mp)
4717 {
4718 	struct vnode *vp;
4719 
4720 	if (should_yield())
4721 		kern_yield(PRI_USER);
4722 	MNT_ILOCK(mp);
4723 	KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
4724 	vp = TAILQ_NEXT(*mvp, v_nmntvnodes);
4725 	while (vp != NULL && (vp->v_type == VMARKER ||
4726 	    (vp->v_iflag & VI_DOOMED) != 0))
4727 		vp = TAILQ_NEXT(vp, v_nmntvnodes);
4728 
4729 	/* Check if we are done */
4730 	if (vp == NULL) {
4731 		__mnt_vnode_markerfree_all(mvp, mp);
4732 		/* MNT_IUNLOCK(mp); -- done in above function */
4733 		mtx_assert(MNT_MTX(mp), MA_NOTOWNED);
4734 		return (NULL);
4735 	}
4736 	TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
4737 	TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
4738 	VI_LOCK(vp);
4739 	MNT_IUNLOCK(mp);
4740 	return (vp);
4741 }
4742 
4743 struct vnode *
4744 __mnt_vnode_first_all(struct vnode **mvp, struct mount *mp)
4745 {
4746 	struct vnode *vp;
4747 
4748 	*mvp = malloc(sizeof(struct vnode), M_VNODE_MARKER, M_WAITOK | M_ZERO);
4749 	MNT_ILOCK(mp);
4750 	MNT_REF(mp);
4751 	(*mvp)->v_type = VMARKER;
4752 
4753 	vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
4754 	while (vp != NULL && (vp->v_type == VMARKER ||
4755 	    (vp->v_iflag & VI_DOOMED) != 0))
4756 		vp = TAILQ_NEXT(vp, v_nmntvnodes);
4757 
4758 	/* Check if we are done */
4759 	if (vp == NULL) {
4760 		MNT_REL(mp);
4761 		MNT_IUNLOCK(mp);
4762 		free(*mvp, M_VNODE_MARKER);
4763 		*mvp = NULL;
4764 		return (NULL);
4765 	}
4766 	(*mvp)->v_mount = mp;
4767 	TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
4768 	VI_LOCK(vp);
4769 	MNT_IUNLOCK(mp);
4770 	return (vp);
4771 }
4772 
4773 
4774 void
4775 __mnt_vnode_markerfree_all(struct vnode **mvp, struct mount *mp)
4776 {
4777 
4778 	if (*mvp == NULL) {
4779 		MNT_IUNLOCK(mp);
4780 		return;
4781 	}
4782 
4783 	mtx_assert(MNT_MTX(mp), MA_OWNED);
4784 
4785 	KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
4786 	TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
4787 	MNT_REL(mp);
4788 	MNT_IUNLOCK(mp);
4789 	free(*mvp, M_VNODE_MARKER);
4790 	*mvp = NULL;
4791 }
4792 
4793 /*
4794  * These are helper functions for filesystems to traverse their
4795  * active vnodes.  See MNT_VNODE_FOREACH_ACTIVE() in sys/mount.h
4796  */
4797 static void
4798 mnt_vnode_markerfree_active(struct vnode **mvp, struct mount *mp)
4799 {
4800 
4801 	KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
4802 
4803 	MNT_ILOCK(mp);
4804 	MNT_REL(mp);
4805 	MNT_IUNLOCK(mp);
4806 	free(*mvp, M_VNODE_MARKER);
4807 	*mvp = NULL;
4808 }
4809 
4810 static struct vnode *
4811 mnt_vnode_next_active(struct vnode **mvp, struct mount *mp)
4812 {
4813 	struct vnode *vp, *nvp;
4814 
4815 	mtx_assert(&vnode_free_list_mtx, MA_OWNED);
4816 	KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
4817 restart:
4818 	vp = TAILQ_NEXT(*mvp, v_actfreelist);
4819 	TAILQ_REMOVE(&mp->mnt_activevnodelist, *mvp, v_actfreelist);
4820 	while (vp != NULL) {
4821 		if (vp->v_type == VMARKER) {
4822 			vp = TAILQ_NEXT(vp, v_actfreelist);
4823 			continue;
4824 		}
4825 		if (!VI_TRYLOCK(vp)) {
4826 			if (mp_ncpus == 1 || should_yield()) {
4827 				TAILQ_INSERT_BEFORE(vp, *mvp, v_actfreelist);
4828 				mtx_unlock(&vnode_free_list_mtx);
4829 				pause("vnacti", 1);
4830 				mtx_lock(&vnode_free_list_mtx);
4831 				goto restart;
4832 			}
4833 			continue;
4834 		}
4835 		KASSERT(vp->v_type != VMARKER, ("locked marker %p", vp));
4836 		KASSERT(vp->v_mount == mp || vp->v_mount == NULL,
4837 		    ("alien vnode on the active list %p %p", vp, mp));
4838 		if (vp->v_mount == mp && (vp->v_iflag & VI_DOOMED) == 0)
4839 			break;
4840 		nvp = TAILQ_NEXT(vp, v_actfreelist);
4841 		VI_UNLOCK(vp);
4842 		vp = nvp;
4843 	}
4844 
4845 	/* Check if we are done */
4846 	if (vp == NULL) {
4847 		mtx_unlock(&vnode_free_list_mtx);
4848 		mnt_vnode_markerfree_active(mvp, mp);
4849 		return (NULL);
4850 	}
4851 	TAILQ_INSERT_AFTER(&mp->mnt_activevnodelist, vp, *mvp, v_actfreelist);
4852 	mtx_unlock(&vnode_free_list_mtx);
4853 	ASSERT_VI_LOCKED(vp, "active iter");
4854 	KASSERT((vp->v_iflag & VI_ACTIVE) != 0, ("Non-active vp %p", vp));
4855 	return (vp);
4856 }
4857 
4858 struct vnode *
4859 __mnt_vnode_next_active(struct vnode **mvp, struct mount *mp)
4860 {
4861 
4862 	if (should_yield())
4863 		kern_yield(PRI_USER);
4864 	mtx_lock(&vnode_free_list_mtx);
4865 	return (mnt_vnode_next_active(mvp, mp));
4866 }
4867 
4868 struct vnode *
4869 __mnt_vnode_first_active(struct vnode **mvp, struct mount *mp)
4870 {
4871 	struct vnode *vp;
4872 
4873 	*mvp = malloc(sizeof(struct vnode), M_VNODE_MARKER, M_WAITOK | M_ZERO);
4874 	MNT_ILOCK(mp);
4875 	MNT_REF(mp);
4876 	MNT_IUNLOCK(mp);
4877 	(*mvp)->v_type = VMARKER;
4878 	(*mvp)->v_mount = mp;
4879 
4880 	mtx_lock(&vnode_free_list_mtx);
4881 	vp = TAILQ_FIRST(&mp->mnt_activevnodelist);
4882 	if (vp == NULL) {
4883 		mtx_unlock(&vnode_free_list_mtx);
4884 		mnt_vnode_markerfree_active(mvp, mp);
4885 		return (NULL);
4886 	}
4887 	TAILQ_INSERT_BEFORE(vp, *mvp, v_actfreelist);
4888 	return (mnt_vnode_next_active(mvp, mp));
4889 }
4890 
4891 void
4892 __mnt_vnode_markerfree_active(struct vnode **mvp, struct mount *mp)
4893 {
4894 
4895 	if (*mvp == NULL)
4896 		return;
4897 
4898 	mtx_lock(&vnode_free_list_mtx);
4899 	TAILQ_REMOVE(&mp->mnt_activevnodelist, *mvp, v_actfreelist);
4900 	mtx_unlock(&vnode_free_list_mtx);
4901 	mnt_vnode_markerfree_active(mvp, mp);
4902 }
4903