xref: /netbsd-src/sys/kern/vfs_subr.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: vfs_subr.c,v 1.415 2010/08/17 13:17:47 hannken Exp $	*/
2 
3 /*-
4  * Copyright (c) 1997, 1998, 2004, 2005, 2007, 2008 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9  * NASA Ames Research Center, by Charles M. Hannum, and by Andrew Doran.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*
34  * Copyright (c) 1989, 1993
35  *	The Regents of the University of California.  All rights reserved.
36  * (c) UNIX System Laboratories, Inc.
37  * All or some portions of this file are derived from material licensed
38  * to the University of California by American Telephone and Telegraph
39  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
40  * the permission of UNIX System Laboratories, Inc.
41  *
42  * Redistribution and use in source and binary forms, with or without
43  * modification, are permitted provided that the following conditions
44  * are met:
45  * 1. Redistributions of source code must retain the above copyright
46  *    notice, this list of conditions and the following disclaimer.
47  * 2. Redistributions in binary form must reproduce the above copyright
48  *    notice, this list of conditions and the following disclaimer in the
49  *    documentation and/or other materials provided with the distribution.
50  * 3. Neither the name of the University nor the names of its contributors
51  *    may be used to endorse or promote products derived from this software
52  *    without specific prior written permission.
53  *
54  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64  * SUCH DAMAGE.
65  *
66  *	@(#)vfs_subr.c	8.13 (Berkeley) 4/18/94
67  */
68 
69 /*
70  * Note on v_usecount and locking:
71  *
72  * At nearly all points it is known that v_usecount could be zero, the
73  * vnode interlock will be held.
74  *
75  * To change v_usecount away from zero, the interlock must be held.  To
76  * change from a non-zero value to zero, again the interlock must be
77  * held.
78  *
79  * There's a flag bit, VC_XLOCK, embedded in v_usecount.
80  * To raise v_usecount, if the VC_XLOCK bit is set in it, the interlock
81  * must be held.
82  * To modify the VC_XLOCK bit, the interlock must be held.
83  * We always keep the usecount (v_usecount & VC_MASK) non-zero while the
84  * VC_XLOCK bit is set.
85  *
86  * Unless the VC_XLOCK bit is set, changing the usecount from a non-zero
87  * value to a non-zero value can safely be done using atomic operations,
88  * without the interlock held.
89  * Even if the VC_XLOCK bit is set, decreasing the usecount to a non-zero
90  * value can be done using atomic operations, without the interlock held.
91  */
92 
93 #include <sys/cdefs.h>
94 __KERNEL_RCSID(0, "$NetBSD: vfs_subr.c,v 1.415 2010/08/17 13:17:47 hannken Exp $");
95 
96 #include "opt_ddb.h"
97 #include "opt_compat_netbsd.h"
98 #include "opt_compat_43.h"
99 
100 #include <sys/param.h>
101 #include <sys/systm.h>
102 #include <sys/conf.h>
103 #include <sys/dirent.h>
104 #include <sys/proc.h>
105 #include <sys/kernel.h>
106 #include <sys/mount.h>
107 #include <sys/fcntl.h>
108 #include <sys/vnode.h>
109 #include <sys/stat.h>
110 #include <sys/namei.h>
111 #include <sys/ucred.h>
112 #include <sys/buf.h>
113 #include <sys/errno.h>
114 #include <sys/kmem.h>
115 #include <sys/syscallargs.h>
116 #include <sys/device.h>
117 #include <sys/filedesc.h>
118 #include <sys/kauth.h>
119 #include <sys/atomic.h>
120 #include <sys/kthread.h>
121 #include <sys/wapbl.h>
122 #include <sys/module.h>
123 
124 #include <miscfs/genfs/genfs.h>
125 #include <miscfs/specfs/specdev.h>
126 #include <miscfs/syncfs/syncfs.h>
127 
128 #include <uvm/uvm.h>
129 #include <uvm/uvm_readahead.h>
130 #include <uvm/uvm_ddb.h>
131 
132 #include <sys/sysctl.h>
133 
134 const enum vtype iftovt_tab[16] = {
135 	VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
136 	VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
137 };
138 const int	vttoif_tab[9] = {
139 	0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
140 	S_IFSOCK, S_IFIFO, S_IFMT,
141 };
142 
143 /*
144  * Insq/Remq for the vnode usage lists.
145  */
146 #define	bufinsvn(bp, dp)	LIST_INSERT_HEAD(dp, bp, b_vnbufs)
147 #define	bufremvn(bp) {							\
148 	LIST_REMOVE(bp, b_vnbufs);					\
149 	(bp)->b_vnbufs.le_next = NOLIST;				\
150 }
151 
152 int doforce = 1;		/* 1 => permit forcible unmounting */
153 int prtactive = 0;		/* 1 => print out reclaim of active vnodes */
154 
155 static vnodelst_t vnode_free_list = TAILQ_HEAD_INITIALIZER(vnode_free_list);
156 static vnodelst_t vnode_hold_list = TAILQ_HEAD_INITIALIZER(vnode_hold_list);
157 static vnodelst_t vrele_list = TAILQ_HEAD_INITIALIZER(vrele_list);
158 
159 struct mntlist mountlist =			/* mounted filesystem list */
160     CIRCLEQ_HEAD_INITIALIZER(mountlist);
161 
162 u_int numvnodes;
163 static specificdata_domain_t mount_specificdata_domain;
164 
165 static int vrele_pending;
166 static int vrele_gen;
167 static kmutex_t	vrele_lock;
168 static kcondvar_t vrele_cv;
169 static lwp_t *vrele_lwp;
170 
171 static uint64_t mountgen = 0;
172 static kmutex_t mountgen_lock;
173 
174 kmutex_t mountlist_lock;
175 kmutex_t mntid_lock;
176 kmutex_t mntvnode_lock;
177 kmutex_t vnode_free_list_lock;
178 kmutex_t vfs_list_lock;
179 
180 static pool_cache_t vnode_cache;
181 
182 /*
183  * These define the root filesystem and device.
184  */
185 struct vnode *rootvnode;
186 struct device *root_device;			/* root device */
187 
188 /*
189  * Local declarations.
190  */
191 
192 static void vrele_thread(void *);
193 static void insmntque(vnode_t *, struct mount *);
194 static int getdevvp(dev_t, vnode_t **, enum vtype);
195 static vnode_t *getcleanvnode(void);
196 void vpanic(vnode_t *, const char *);
197 static void vfs_shutdown1(struct lwp *);
198 
199 #ifdef DEBUG
200 void printlockedvnodes(void);
201 #endif
202 
203 #ifdef DIAGNOSTIC
204 void
205 vpanic(vnode_t *vp, const char *msg)
206 {
207 
208 	vprint(NULL, vp);
209 	panic("%s\n", msg);
210 }
211 #else
212 #define	vpanic(vp, msg)	/* nothing */
213 #endif
214 
215 void
216 vn_init1(void)
217 {
218 
219 	vnode_cache = pool_cache_init(sizeof(struct vnode), 0, 0, 0, "vnodepl",
220 	    NULL, IPL_NONE, NULL, NULL, NULL);
221 	KASSERT(vnode_cache != NULL);
222 
223 	/* Create deferred release thread. */
224 	mutex_init(&vrele_lock, MUTEX_DEFAULT, IPL_NONE);
225 	cv_init(&vrele_cv, "vrele");
226 	if (kthread_create(PRI_VM, KTHREAD_MPSAFE, NULL, vrele_thread,
227 	    NULL, &vrele_lwp, "vrele"))
228 		panic("fork vrele");
229 }
230 
231 /*
232  * Initialize the vnode management data structures.
233  */
234 void
235 vntblinit(void)
236 {
237 
238 	mutex_init(&mountgen_lock, MUTEX_DEFAULT, IPL_NONE);
239 	mutex_init(&mountlist_lock, MUTEX_DEFAULT, IPL_NONE);
240 	mutex_init(&mntid_lock, MUTEX_DEFAULT, IPL_NONE);
241 	mutex_init(&mntvnode_lock, MUTEX_DEFAULT, IPL_NONE);
242 	mutex_init(&vnode_free_list_lock, MUTEX_DEFAULT, IPL_NONE);
243 	mutex_init(&vfs_list_lock, MUTEX_DEFAULT, IPL_NONE);
244 
245 	mount_specificdata_domain = specificdata_domain_create();
246 
247 	/* Initialize the filesystem syncer. */
248 	vn_initialize_syncerd();
249 	vn_init1();
250 }
251 
252 int
253 vfs_drainvnodes(long target, struct lwp *l)
254 {
255 
256 	while (numvnodes > target) {
257 		vnode_t *vp;
258 
259 		mutex_enter(&vnode_free_list_lock);
260 		vp = getcleanvnode();
261 		if (vp == NULL)
262 			return EBUSY; /* give up */
263 		ungetnewvnode(vp);
264 	}
265 
266 	return 0;
267 }
268 
269 /*
270  * Lookup a mount point by filesystem identifier.
271  *
272  * XXX Needs to add a reference to the mount point.
273  */
274 struct mount *
275 vfs_getvfs(fsid_t *fsid)
276 {
277 	struct mount *mp;
278 
279 	mutex_enter(&mountlist_lock);
280 	CIRCLEQ_FOREACH(mp, &mountlist, mnt_list) {
281 		if (mp->mnt_stat.f_fsidx.__fsid_val[0] == fsid->__fsid_val[0] &&
282 		    mp->mnt_stat.f_fsidx.__fsid_val[1] == fsid->__fsid_val[1]) {
283 			mutex_exit(&mountlist_lock);
284 			return (mp);
285 		}
286 	}
287 	mutex_exit(&mountlist_lock);
288 	return ((struct mount *)0);
289 }
290 
291 /*
292  * Drop a reference to a mount structure, freeing if the last reference.
293  */
294 void
295 vfs_destroy(struct mount *mp)
296 {
297 
298 	if (__predict_true((int)atomic_dec_uint_nv(&mp->mnt_refcnt) > 0)) {
299 		return;
300 	}
301 
302 	/*
303 	 * Nothing else has visibility of the mount: we can now
304 	 * free the data structures.
305 	 */
306 	KASSERT(mp->mnt_refcnt == 0);
307 	specificdata_fini(mount_specificdata_domain, &mp->mnt_specdataref);
308 	rw_destroy(&mp->mnt_unmounting);
309 	mutex_destroy(&mp->mnt_updating);
310 	mutex_destroy(&mp->mnt_renamelock);
311 	if (mp->mnt_op != NULL) {
312 		vfs_delref(mp->mnt_op);
313 	}
314 	kmem_free(mp, sizeof(*mp));
315 }
316 
317 /*
318  * grab a vnode from freelist and clean it.
319  */
320 vnode_t *
321 getcleanvnode(void)
322 {
323 	vnode_t *vp;
324 	vnodelst_t *listhd;
325 
326 	KASSERT(mutex_owned(&vnode_free_list_lock));
327 
328 retry:
329 	listhd = &vnode_free_list;
330 try_nextlist:
331 	TAILQ_FOREACH(vp, listhd, v_freelist) {
332 		/*
333 		 * It's safe to test v_usecount and v_iflag
334 		 * without holding the interlock here, since
335 		 * these vnodes should never appear on the
336 		 * lists.
337 		 */
338 		if (vp->v_usecount != 0) {
339 			vpanic(vp, "free vnode isn't");
340 		}
341 		if ((vp->v_iflag & VI_CLEAN) != 0) {
342 			vpanic(vp, "clean vnode on freelist");
343 		}
344 		if (vp->v_freelisthd != listhd) {
345 			printf("vnode sez %p, listhd %p\n", vp->v_freelisthd, listhd);
346 			vpanic(vp, "list head mismatch");
347 		}
348 		if (!mutex_tryenter(&vp->v_interlock))
349 			continue;
350 		if ((vp->v_iflag & VI_XLOCK) == 0)
351 			break;
352 		mutex_exit(&vp->v_interlock);
353 	}
354 
355 	if (vp == NULL) {
356 		if (listhd == &vnode_free_list) {
357 			listhd = &vnode_hold_list;
358 			goto try_nextlist;
359 		}
360 		mutex_exit(&vnode_free_list_lock);
361 		return NULL;
362 	}
363 
364 	/* Remove it from the freelist. */
365 	TAILQ_REMOVE(listhd, vp, v_freelist);
366 	vp->v_freelisthd = NULL;
367 	mutex_exit(&vnode_free_list_lock);
368 
369 	KASSERT(vp->v_usecount == 0);
370 
371 	/*
372 	 * The vnode is still associated with a file system, so we must
373 	 * clean it out before reusing it.  We need to add a reference
374 	 * before doing this.  If the vnode gains another reference while
375 	 * being cleaned out then we lose - retry.
376 	 */
377 	atomic_add_int(&vp->v_usecount, 1 + VC_XLOCK);
378 	vclean(vp, DOCLOSE);
379 	KASSERT(vp->v_usecount >= 1 + VC_XLOCK);
380 	atomic_add_int(&vp->v_usecount, -VC_XLOCK);
381 	if (vp->v_usecount == 1) {
382 		/* We're about to dirty it. */
383 		vp->v_iflag &= ~VI_CLEAN;
384 		mutex_exit(&vp->v_interlock);
385 		if (vp->v_type == VBLK || vp->v_type == VCHR) {
386 			spec_node_destroy(vp);
387 		}
388 		vp->v_type = VNON;
389 	} else {
390 		/*
391 		 * Don't return to freelist - the holder of the last
392 		 * reference will destroy it.
393 		 */
394 		vrelel(vp, 0); /* releases vp->v_interlock */
395 		mutex_enter(&vnode_free_list_lock);
396 		goto retry;
397 	}
398 
399 	if (vp->v_data != NULL || vp->v_uobj.uo_npages != 0 ||
400 	    !TAILQ_EMPTY(&vp->v_uobj.memq)) {
401 		vpanic(vp, "cleaned vnode isn't");
402 	}
403 	if (vp->v_numoutput != 0) {
404 		vpanic(vp, "clean vnode has pending I/O's");
405 	}
406 	if ((vp->v_iflag & VI_ONWORKLST) != 0) {
407 		vpanic(vp, "clean vnode on syncer list");
408 	}
409 
410 	return vp;
411 }
412 
413 /*
414  * Mark a mount point as busy, and gain a new reference to it.  Used to
415  * prevent the file system from being unmounted during critical sections.
416  *
417  * => The caller must hold a pre-existing reference to the mount.
418  * => Will fail if the file system is being unmounted, or is unmounted.
419  */
420 int
421 vfs_busy(struct mount *mp, struct mount **nextp)
422 {
423 
424 	KASSERT(mp->mnt_refcnt > 0);
425 
426 	if (__predict_false(!rw_tryenter(&mp->mnt_unmounting, RW_READER))) {
427 		if (nextp != NULL) {
428 			KASSERT(mutex_owned(&mountlist_lock));
429 			*nextp = CIRCLEQ_NEXT(mp, mnt_list);
430 		}
431 		return EBUSY;
432 	}
433 	if (__predict_false((mp->mnt_iflag & IMNT_GONE) != 0)) {
434 		rw_exit(&mp->mnt_unmounting);
435 		if (nextp != NULL) {
436 			KASSERT(mutex_owned(&mountlist_lock));
437 			*nextp = CIRCLEQ_NEXT(mp, mnt_list);
438 		}
439 		return ENOENT;
440 	}
441 	if (nextp != NULL) {
442 		mutex_exit(&mountlist_lock);
443 	}
444 	atomic_inc_uint(&mp->mnt_refcnt);
445 	return 0;
446 }
447 
448 /*
449  * Unbusy a busy filesystem.
450  *
451  * => If keepref is true, preserve reference added by vfs_busy().
452  * => If nextp != NULL, acquire mountlist_lock.
453  */
454 void
455 vfs_unbusy(struct mount *mp, bool keepref, struct mount **nextp)
456 {
457 
458 	KASSERT(mp->mnt_refcnt > 0);
459 
460 	if (nextp != NULL) {
461 		mutex_enter(&mountlist_lock);
462 	}
463 	rw_exit(&mp->mnt_unmounting);
464 	if (!keepref) {
465 		vfs_destroy(mp);
466 	}
467 	if (nextp != NULL) {
468 		KASSERT(mutex_owned(&mountlist_lock));
469 		*nextp = CIRCLEQ_NEXT(mp, mnt_list);
470 	}
471 }
472 
473 struct mount *
474 vfs_mountalloc(struct vfsops *vfsops, struct vnode *vp)
475 {
476 	int error;
477 	struct mount *mp;
478 
479 	mp = kmem_zalloc(sizeof(*mp), KM_SLEEP);
480 	if (mp == NULL)
481 		return NULL;
482 
483 	mp->mnt_op = vfsops;
484 	mp->mnt_refcnt = 1;
485 	TAILQ_INIT(&mp->mnt_vnodelist);
486 	rw_init(&mp->mnt_unmounting);
487 	mutex_init(&mp->mnt_renamelock, MUTEX_DEFAULT, IPL_NONE);
488 	mutex_init(&mp->mnt_updating, MUTEX_DEFAULT, IPL_NONE);
489 	error = vfs_busy(mp, NULL);
490 	KASSERT(error == 0);
491 	mp->mnt_vnodecovered = vp;
492 	mount_initspecific(mp);
493 
494 	mutex_enter(&mountgen_lock);
495 	mp->mnt_gen = mountgen++;
496 	mutex_exit(&mountgen_lock);
497 
498 	return mp;
499 }
500 
501 /*
502  * Lookup a filesystem type, and if found allocate and initialize
503  * a mount structure for it.
504  *
505  * Devname is usually updated by mount(8) after booting.
506  */
507 int
508 vfs_rootmountalloc(const char *fstypename, const char *devname,
509     struct mount **mpp)
510 {
511 	struct vfsops *vfsp = NULL;
512 	struct mount *mp;
513 
514 	mutex_enter(&vfs_list_lock);
515 	LIST_FOREACH(vfsp, &vfs_list, vfs_list)
516 		if (!strncmp(vfsp->vfs_name, fstypename,
517 		    sizeof(mp->mnt_stat.f_fstypename)))
518 			break;
519 	if (vfsp == NULL) {
520 		mutex_exit(&vfs_list_lock);
521 		return (ENODEV);
522 	}
523 	vfsp->vfs_refcount++;
524 	mutex_exit(&vfs_list_lock);
525 
526 	if ((mp = vfs_mountalloc(vfsp, NULL)) == NULL)
527 		return ENOMEM;
528 	mp->mnt_flag = MNT_RDONLY;
529 	(void)strlcpy(mp->mnt_stat.f_fstypename, vfsp->vfs_name,
530 	    sizeof(mp->mnt_stat.f_fstypename));
531 	mp->mnt_stat.f_mntonname[0] = '/';
532 	mp->mnt_stat.f_mntonname[1] = '\0';
533 	mp->mnt_stat.f_mntfromname[sizeof(mp->mnt_stat.f_mntfromname) - 1] =
534 	    '\0';
535 	(void)copystr(devname, mp->mnt_stat.f_mntfromname,
536 	    sizeof(mp->mnt_stat.f_mntfromname) - 1, 0);
537 	*mpp = mp;
538 	return (0);
539 }
540 
541 /*
542  * Routines having to do with the management of the vnode table.
543  */
544 extern int (**dead_vnodeop_p)(void *);
545 
546 /*
547  * Return the next vnode from the free list.
548  */
549 int
550 getnewvnode(enum vtagtype tag, struct mount *mp, int (**vops)(void *),
551 	    vnode_t **vpp)
552 {
553 	struct uvm_object *uobj;
554 	static int toggle;
555 	vnode_t *vp;
556 	int error = 0, tryalloc;
557 
558  try_again:
559 	if (mp != NULL) {
560 		/*
561 		 * Mark filesystem busy while we're creating a
562 		 * vnode.  If unmount is in progress, this will
563 		 * fail.
564 		 */
565 		error = vfs_busy(mp, NULL);
566 		if (error)
567 			return error;
568 	}
569 
570 	/*
571 	 * We must choose whether to allocate a new vnode or recycle an
572 	 * existing one. The criterion for allocating a new one is that
573 	 * the total number of vnodes is less than the number desired or
574 	 * there are no vnodes on either free list. Generally we only
575 	 * want to recycle vnodes that have no buffers associated with
576 	 * them, so we look first on the vnode_free_list. If it is empty,
577 	 * we next consider vnodes with referencing buffers on the
578 	 * vnode_hold_list. The toggle ensures that half the time we
579 	 * will use a buffer from the vnode_hold_list, and half the time
580 	 * we will allocate a new one unless the list has grown to twice
581 	 * the desired size. We are reticent to recycle vnodes from the
582 	 * vnode_hold_list because we will lose the identity of all its
583 	 * referencing buffers.
584 	 */
585 
586 	vp = NULL;
587 
588 	mutex_enter(&vnode_free_list_lock);
589 
590 	toggle ^= 1;
591 	if (numvnodes > 2 * desiredvnodes)
592 		toggle = 0;
593 
594 	tryalloc = numvnodes < desiredvnodes ||
595 	    (TAILQ_FIRST(&vnode_free_list) == NULL &&
596 	     (TAILQ_FIRST(&vnode_hold_list) == NULL || toggle));
597 
598 	if (tryalloc) {
599 		numvnodes++;
600 		mutex_exit(&vnode_free_list_lock);
601 		if ((vp = vnalloc(NULL)) == NULL) {
602 			mutex_enter(&vnode_free_list_lock);
603 			numvnodes--;
604 		} else
605 			vp->v_usecount = 1;
606 	}
607 
608 	if (vp == NULL) {
609 		vp = getcleanvnode();
610 		if (vp == NULL) {
611 			if (mp != NULL) {
612 				vfs_unbusy(mp, false, NULL);
613 			}
614 			if (tryalloc) {
615 				printf("WARNING: unable to allocate new "
616 				    "vnode, retrying...\n");
617 				kpause("newvn", false, hz, NULL);
618 				goto try_again;
619 			}
620 			tablefull("vnode", "increase kern.maxvnodes or NVNODE");
621 			*vpp = 0;
622 			return (ENFILE);
623 		}
624 		vp->v_iflag = 0;
625 		vp->v_vflag = 0;
626 		vp->v_uflag = 0;
627 		vp->v_socket = NULL;
628 	}
629 
630 	KASSERT(vp->v_usecount == 1);
631 	KASSERT(vp->v_freelisthd == NULL);
632 	KASSERT(LIST_EMPTY(&vp->v_nclist));
633 	KASSERT(LIST_EMPTY(&vp->v_dnclist));
634 
635 	vp->v_type = VNON;
636 	vp->v_tag = tag;
637 	vp->v_op = vops;
638 	insmntque(vp, mp);
639 	*vpp = vp;
640 	vp->v_data = 0;
641 
642 	/*
643 	 * initialize uvm_object within vnode.
644 	 */
645 
646 	uobj = &vp->v_uobj;
647 	KASSERT(uobj->pgops == &uvm_vnodeops);
648 	KASSERT(uobj->uo_npages == 0);
649 	KASSERT(TAILQ_FIRST(&uobj->memq) == NULL);
650 	vp->v_size = vp->v_writesize = VSIZENOTSET;
651 
652 	if (mp != NULL) {
653 		if ((mp->mnt_iflag & IMNT_MPSAFE) != 0)
654 			vp->v_vflag |= VV_MPSAFE;
655 		vfs_unbusy(mp, true, NULL);
656 	}
657 
658 	return (0);
659 }
660 
661 /*
662  * This is really just the reverse of getnewvnode(). Needed for
663  * VFS_VGET functions who may need to push back a vnode in case
664  * of a locking race.
665  */
666 void
667 ungetnewvnode(vnode_t *vp)
668 {
669 
670 	KASSERT(vp->v_usecount == 1);
671 	KASSERT(vp->v_data == NULL);
672 	KASSERT(vp->v_freelisthd == NULL);
673 
674 	mutex_enter(&vp->v_interlock);
675 	vp->v_iflag |= VI_CLEAN;
676 	vrelel(vp, 0);
677 }
678 
679 /*
680  * Allocate a new, uninitialized vnode.  If 'mp' is non-NULL, this is a
681  * marker vnode and we are prepared to wait for the allocation.
682  */
683 vnode_t *
684 vnalloc(struct mount *mp)
685 {
686 	vnode_t *vp;
687 
688 	vp = pool_cache_get(vnode_cache, (mp != NULL ? PR_WAITOK : PR_NOWAIT));
689 	if (vp == NULL) {
690 		return NULL;
691 	}
692 
693 	memset(vp, 0, sizeof(*vp));
694 	UVM_OBJ_INIT(&vp->v_uobj, &uvm_vnodeops, 0);
695 	cv_init(&vp->v_cv, "vnode");
696 	/*
697 	 * done by memset() above.
698 	 *	LIST_INIT(&vp->v_nclist);
699 	 *	LIST_INIT(&vp->v_dnclist);
700 	 */
701 
702 	if (mp != NULL) {
703 		vp->v_mount = mp;
704 		vp->v_type = VBAD;
705 		vp->v_iflag = VI_MARKER;
706 	} else {
707 		rw_init(&vp->v_lock);
708 	}
709 
710 	return vp;
711 }
712 
713 /*
714  * Free an unused, unreferenced vnode.
715  */
716 void
717 vnfree(vnode_t *vp)
718 {
719 
720 	KASSERT(vp->v_usecount == 0);
721 
722 	if ((vp->v_iflag & VI_MARKER) == 0) {
723 		rw_destroy(&vp->v_lock);
724 		mutex_enter(&vnode_free_list_lock);
725 		numvnodes--;
726 		mutex_exit(&vnode_free_list_lock);
727 	}
728 
729 	UVM_OBJ_DESTROY(&vp->v_uobj);
730 	cv_destroy(&vp->v_cv);
731 	pool_cache_put(vnode_cache, vp);
732 }
733 
734 /*
735  * Remove a vnode from its freelist.
736  */
737 static inline void
738 vremfree(vnode_t *vp)
739 {
740 
741 	KASSERT(mutex_owned(&vp->v_interlock));
742 	KASSERT(vp->v_usecount == 0);
743 
744 	/*
745 	 * Note that the reference count must not change until
746 	 * the vnode is removed.
747 	 */
748 	mutex_enter(&vnode_free_list_lock);
749 	if (vp->v_holdcnt > 0) {
750 		KASSERT(vp->v_freelisthd == &vnode_hold_list);
751 	} else {
752 		KASSERT(vp->v_freelisthd == &vnode_free_list);
753 	}
754 	TAILQ_REMOVE(vp->v_freelisthd, vp, v_freelist);
755 	vp->v_freelisthd = NULL;
756 	mutex_exit(&vnode_free_list_lock);
757 }
758 
759 /*
760  * Move a vnode from one mount queue to another.
761  */
762 static void
763 insmntque(vnode_t *vp, struct mount *mp)
764 {
765 	struct mount *omp;
766 
767 #ifdef DIAGNOSTIC
768 	if ((mp != NULL) &&
769 	    (mp->mnt_iflag & IMNT_UNMOUNT) &&
770 	    vp->v_tag != VT_VFS) {
771 		panic("insmntque into dying filesystem");
772 	}
773 #endif
774 
775 	mutex_enter(&mntvnode_lock);
776 	/*
777 	 * Delete from old mount point vnode list, if on one.
778 	 */
779 	if ((omp = vp->v_mount) != NULL)
780 		TAILQ_REMOVE(&vp->v_mount->mnt_vnodelist, vp, v_mntvnodes);
781 	/*
782 	 * Insert into list of vnodes for the new mount point, if
783 	 * available.  The caller must take a reference on the mount
784 	 * structure and donate to the vnode.
785 	 */
786 	if ((vp->v_mount = mp) != NULL)
787 		TAILQ_INSERT_TAIL(&mp->mnt_vnodelist, vp, v_mntvnodes);
788 	mutex_exit(&mntvnode_lock);
789 
790 	if (omp != NULL) {
791 		/* Release reference to old mount. */
792 		vfs_destroy(omp);
793 	}
794 }
795 
796 /*
797  * Wait for a vnode (typically with VI_XLOCK set) to be cleaned or
798  * recycled.
799  */
800 void
801 vwait(vnode_t *vp, int flags)
802 {
803 
804 	KASSERT(mutex_owned(&vp->v_interlock));
805 	KASSERT(vp->v_usecount != 0);
806 
807 	while ((vp->v_iflag & flags) != 0)
808 		cv_wait(&vp->v_cv, &vp->v_interlock);
809 }
810 
811 /*
812  * Insert a marker vnode into a mount's vnode list, after the
813  * specified vnode.  mntvnode_lock must be held.
814  */
815 void
816 vmark(vnode_t *mvp, vnode_t *vp)
817 {
818 	struct mount *mp;
819 
820 	mp = mvp->v_mount;
821 
822 	KASSERT(mutex_owned(&mntvnode_lock));
823 	KASSERT((mvp->v_iflag & VI_MARKER) != 0);
824 	KASSERT(vp->v_mount == mp);
825 
826 	TAILQ_INSERT_AFTER(&mp->mnt_vnodelist, vp, mvp, v_mntvnodes);
827 }
828 
829 /*
830  * Remove a marker vnode from a mount's vnode list, and return
831  * a pointer to the next vnode in the list.  mntvnode_lock must
832  * be held.
833  */
834 vnode_t *
835 vunmark(vnode_t *mvp)
836 {
837 	vnode_t *vp;
838 	struct mount *mp;
839 
840 	mp = mvp->v_mount;
841 
842 	KASSERT(mutex_owned(&mntvnode_lock));
843 	KASSERT((mvp->v_iflag & VI_MARKER) != 0);
844 
845 	vp = TAILQ_NEXT(mvp, v_mntvnodes);
846 	TAILQ_REMOVE(&mp->mnt_vnodelist, mvp, v_mntvnodes);
847 
848 	KASSERT(vp == NULL || vp->v_mount == mp);
849 
850 	return vp;
851 }
852 
853 /*
854  * Update outstanding I/O count and do wakeup if requested.
855  */
856 void
857 vwakeup(struct buf *bp)
858 {
859 	struct vnode *vp;
860 
861 	if ((vp = bp->b_vp) == NULL)
862 		return;
863 
864 	KASSERT(bp->b_objlock == &vp->v_interlock);
865 	KASSERT(mutex_owned(bp->b_objlock));
866 
867 	if (--vp->v_numoutput < 0)
868 		panic("vwakeup: neg numoutput, vp %p", vp);
869 	if (vp->v_numoutput == 0)
870 		cv_broadcast(&vp->v_cv);
871 }
872 
873 /*
874  * Flush out and invalidate all buffers associated with a vnode.
875  * Called with the underlying vnode locked, which should prevent new dirty
876  * buffers from being queued.
877  */
878 int
879 vinvalbuf(struct vnode *vp, int flags, kauth_cred_t cred, struct lwp *l,
880 	  bool catch, int slptimeo)
881 {
882 	struct buf *bp, *nbp;
883 	int error;
884 	int flushflags = PGO_ALLPAGES | PGO_FREE | PGO_SYNCIO |
885 	    (flags & V_SAVE ? PGO_CLEANIT | PGO_RECLAIM : 0);
886 
887 	/* XXXUBC this doesn't look at flags or slp* */
888 	mutex_enter(&vp->v_interlock);
889 	error = VOP_PUTPAGES(vp, 0, 0, flushflags);
890 	if (error) {
891 		return error;
892 	}
893 
894 	if (flags & V_SAVE) {
895 		error = VOP_FSYNC(vp, cred, FSYNC_WAIT|FSYNC_RECLAIM, 0, 0);
896 		if (error)
897 		        return (error);
898 		KASSERT(LIST_EMPTY(&vp->v_dirtyblkhd));
899 	}
900 
901 	mutex_enter(&bufcache_lock);
902 restart:
903 	for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
904 		nbp = LIST_NEXT(bp, b_vnbufs);
905 		error = bbusy(bp, catch, slptimeo, NULL);
906 		if (error != 0) {
907 			if (error == EPASSTHROUGH)
908 				goto restart;
909 			mutex_exit(&bufcache_lock);
910 			return (error);
911 		}
912 		brelsel(bp, BC_INVAL | BC_VFLUSH);
913 	}
914 
915 	for (bp = LIST_FIRST(&vp->v_cleanblkhd); bp; bp = nbp) {
916 		nbp = LIST_NEXT(bp, b_vnbufs);
917 		error = bbusy(bp, catch, slptimeo, NULL);
918 		if (error != 0) {
919 			if (error == EPASSTHROUGH)
920 				goto restart;
921 			mutex_exit(&bufcache_lock);
922 			return (error);
923 		}
924 		/*
925 		 * XXX Since there are no node locks for NFS, I believe
926 		 * there is a slight chance that a delayed write will
927 		 * occur while sleeping just above, so check for it.
928 		 */
929 		if ((bp->b_oflags & BO_DELWRI) && (flags & V_SAVE)) {
930 #ifdef DEBUG
931 			printf("buffer still DELWRI\n");
932 #endif
933 			bp->b_cflags |= BC_BUSY | BC_VFLUSH;
934 			mutex_exit(&bufcache_lock);
935 			VOP_BWRITE(bp);
936 			mutex_enter(&bufcache_lock);
937 			goto restart;
938 		}
939 		brelsel(bp, BC_INVAL | BC_VFLUSH);
940 	}
941 
942 #ifdef DIAGNOSTIC
943 	if (!LIST_EMPTY(&vp->v_cleanblkhd) || !LIST_EMPTY(&vp->v_dirtyblkhd))
944 		panic("vinvalbuf: flush failed, vp %p", vp);
945 #endif
946 
947 	mutex_exit(&bufcache_lock);
948 
949 	return (0);
950 }
951 
952 /*
953  * Destroy any in core blocks past the truncation length.
954  * Called with the underlying vnode locked, which should prevent new dirty
955  * buffers from being queued.
956  */
957 int
958 vtruncbuf(struct vnode *vp, daddr_t lbn, bool catch, int slptimeo)
959 {
960 	struct buf *bp, *nbp;
961 	int error;
962 	voff_t off;
963 
964 	off = round_page((voff_t)lbn << vp->v_mount->mnt_fs_bshift);
965 	mutex_enter(&vp->v_interlock);
966 	error = VOP_PUTPAGES(vp, off, 0, PGO_FREE | PGO_SYNCIO);
967 	if (error) {
968 		return error;
969 	}
970 
971 	mutex_enter(&bufcache_lock);
972 restart:
973 	for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
974 		nbp = LIST_NEXT(bp, b_vnbufs);
975 		if (bp->b_lblkno < lbn)
976 			continue;
977 		error = bbusy(bp, catch, slptimeo, NULL);
978 		if (error != 0) {
979 			if (error == EPASSTHROUGH)
980 				goto restart;
981 			mutex_exit(&bufcache_lock);
982 			return (error);
983 		}
984 		brelsel(bp, BC_INVAL | BC_VFLUSH);
985 	}
986 
987 	for (bp = LIST_FIRST(&vp->v_cleanblkhd); bp; bp = nbp) {
988 		nbp = LIST_NEXT(bp, b_vnbufs);
989 		if (bp->b_lblkno < lbn)
990 			continue;
991 		error = bbusy(bp, catch, slptimeo, NULL);
992 		if (error != 0) {
993 			if (error == EPASSTHROUGH)
994 				goto restart;
995 			mutex_exit(&bufcache_lock);
996 			return (error);
997 		}
998 		brelsel(bp, BC_INVAL | BC_VFLUSH);
999 	}
1000 	mutex_exit(&bufcache_lock);
1001 
1002 	return (0);
1003 }
1004 
1005 /*
1006  * Flush all dirty buffers from a vnode.
1007  * Called with the underlying vnode locked, which should prevent new dirty
1008  * buffers from being queued.
1009  */
1010 void
1011 vflushbuf(struct vnode *vp, int sync)
1012 {
1013 	struct buf *bp, *nbp;
1014 	int flags = PGO_CLEANIT | PGO_ALLPAGES | (sync ? PGO_SYNCIO : 0);
1015 	bool dirty;
1016 
1017 	mutex_enter(&vp->v_interlock);
1018 	(void) VOP_PUTPAGES(vp, 0, 0, flags);
1019 
1020 loop:
1021 	mutex_enter(&bufcache_lock);
1022 	for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
1023 		nbp = LIST_NEXT(bp, b_vnbufs);
1024 		if ((bp->b_cflags & BC_BUSY))
1025 			continue;
1026 		if ((bp->b_oflags & BO_DELWRI) == 0)
1027 			panic("vflushbuf: not dirty, bp %p", bp);
1028 		bp->b_cflags |= BC_BUSY | BC_VFLUSH;
1029 		mutex_exit(&bufcache_lock);
1030 		/*
1031 		 * Wait for I/O associated with indirect blocks to complete,
1032 		 * since there is no way to quickly wait for them below.
1033 		 */
1034 		if (bp->b_vp == vp || sync == 0)
1035 			(void) bawrite(bp);
1036 		else
1037 			(void) bwrite(bp);
1038 		goto loop;
1039 	}
1040 	mutex_exit(&bufcache_lock);
1041 
1042 	if (sync == 0)
1043 		return;
1044 
1045 	mutex_enter(&vp->v_interlock);
1046 	while (vp->v_numoutput != 0)
1047 		cv_wait(&vp->v_cv, &vp->v_interlock);
1048 	dirty = !LIST_EMPTY(&vp->v_dirtyblkhd);
1049 	mutex_exit(&vp->v_interlock);
1050 
1051 	if (dirty) {
1052 		vprint("vflushbuf: dirty", vp);
1053 		goto loop;
1054 	}
1055 }
1056 
1057 /*
1058  * Create a vnode for a block device.
1059  * Used for root filesystem and swap areas.
1060  * Also used for memory file system special devices.
1061  */
1062 int
1063 bdevvp(dev_t dev, vnode_t **vpp)
1064 {
1065 
1066 	return (getdevvp(dev, vpp, VBLK));
1067 }
1068 
1069 /*
1070  * Create a vnode for a character device.
1071  * Used for kernfs and some console handling.
1072  */
1073 int
1074 cdevvp(dev_t dev, vnode_t **vpp)
1075 {
1076 
1077 	return (getdevvp(dev, vpp, VCHR));
1078 }
1079 
1080 /*
1081  * Associate a buffer with a vnode.  There must already be a hold on
1082  * the vnode.
1083  */
1084 void
1085 bgetvp(struct vnode *vp, struct buf *bp)
1086 {
1087 
1088 	KASSERT(bp->b_vp == NULL);
1089 	KASSERT(bp->b_objlock == &buffer_lock);
1090 	KASSERT(mutex_owned(&vp->v_interlock));
1091 	KASSERT(mutex_owned(&bufcache_lock));
1092 	KASSERT((bp->b_cflags & BC_BUSY) != 0);
1093 	KASSERT(!cv_has_waiters(&bp->b_done));
1094 
1095 	vholdl(vp);
1096 	bp->b_vp = vp;
1097 	if (vp->v_type == VBLK || vp->v_type == VCHR)
1098 		bp->b_dev = vp->v_rdev;
1099 	else
1100 		bp->b_dev = NODEV;
1101 
1102 	/*
1103 	 * Insert onto list for new vnode.
1104 	 */
1105 	bufinsvn(bp, &vp->v_cleanblkhd);
1106 	bp->b_objlock = &vp->v_interlock;
1107 }
1108 
1109 /*
1110  * Disassociate a buffer from a vnode.
1111  */
1112 void
1113 brelvp(struct buf *bp)
1114 {
1115 	struct vnode *vp = bp->b_vp;
1116 
1117 	KASSERT(vp != NULL);
1118 	KASSERT(bp->b_objlock == &vp->v_interlock);
1119 	KASSERT(mutex_owned(&vp->v_interlock));
1120 	KASSERT(mutex_owned(&bufcache_lock));
1121 	KASSERT((bp->b_cflags & BC_BUSY) != 0);
1122 	KASSERT(!cv_has_waiters(&bp->b_done));
1123 
1124 	/*
1125 	 * Delete from old vnode list, if on one.
1126 	 */
1127 	if (LIST_NEXT(bp, b_vnbufs) != NOLIST)
1128 		bufremvn(bp);
1129 
1130 	if (vp->v_uobj.uo_npages == 0 && (vp->v_iflag & VI_ONWORKLST) &&
1131 	    LIST_FIRST(&vp->v_dirtyblkhd) == NULL) {
1132 		vp->v_iflag &= ~VI_WRMAPDIRTY;
1133 		vn_syncer_remove_from_worklist(vp);
1134 	}
1135 
1136 	bp->b_objlock = &buffer_lock;
1137 	bp->b_vp = NULL;
1138 	holdrelel(vp);
1139 }
1140 
1141 /*
1142  * Reassign a buffer from one vnode list to another.
1143  * The list reassignment must be within the same vnode.
1144  * Used to assign file specific control information
1145  * (indirect blocks) to the list to which they belong.
1146  */
1147 void
1148 reassignbuf(struct buf *bp, struct vnode *vp)
1149 {
1150 	struct buflists *listheadp;
1151 	int delayx;
1152 
1153 	KASSERT(mutex_owned(&bufcache_lock));
1154 	KASSERT(bp->b_objlock == &vp->v_interlock);
1155 	KASSERT(mutex_owned(&vp->v_interlock));
1156 	KASSERT((bp->b_cflags & BC_BUSY) != 0);
1157 
1158 	/*
1159 	 * Delete from old vnode list, if on one.
1160 	 */
1161 	if (LIST_NEXT(bp, b_vnbufs) != NOLIST)
1162 		bufremvn(bp);
1163 
1164 	/*
1165 	 * If dirty, put on list of dirty buffers;
1166 	 * otherwise insert onto list of clean buffers.
1167 	 */
1168 	if ((bp->b_oflags & BO_DELWRI) == 0) {
1169 		listheadp = &vp->v_cleanblkhd;
1170 		if (vp->v_uobj.uo_npages == 0 &&
1171 		    (vp->v_iflag & VI_ONWORKLST) &&
1172 		    LIST_FIRST(&vp->v_dirtyblkhd) == NULL) {
1173 			vp->v_iflag &= ~VI_WRMAPDIRTY;
1174 			vn_syncer_remove_from_worklist(vp);
1175 		}
1176 	} else {
1177 		listheadp = &vp->v_dirtyblkhd;
1178 		if ((vp->v_iflag & VI_ONWORKLST) == 0) {
1179 			switch (vp->v_type) {
1180 			case VDIR:
1181 				delayx = dirdelay;
1182 				break;
1183 			case VBLK:
1184 				if (vp->v_specmountpoint != NULL) {
1185 					delayx = metadelay;
1186 					break;
1187 				}
1188 				/* fall through */
1189 			default:
1190 				delayx = filedelay;
1191 				break;
1192 			}
1193 			if (!vp->v_mount ||
1194 			    (vp->v_mount->mnt_flag & MNT_ASYNC) == 0)
1195 				vn_syncer_add_to_worklist(vp, delayx);
1196 		}
1197 	}
1198 	bufinsvn(bp, listheadp);
1199 }
1200 
1201 /*
1202  * Create a vnode for a device.
1203  * Used by bdevvp (block device) for root file system etc.,
1204  * and by cdevvp (character device) for console and kernfs.
1205  */
1206 static int
1207 getdevvp(dev_t dev, vnode_t **vpp, enum vtype type)
1208 {
1209 	vnode_t *vp;
1210 	vnode_t *nvp;
1211 	int error;
1212 
1213 	if (dev == NODEV) {
1214 		*vpp = NULL;
1215 		return (0);
1216 	}
1217 	error = getnewvnode(VT_NON, NULL, spec_vnodeop_p, &nvp);
1218 	if (error) {
1219 		*vpp = NULL;
1220 		return (error);
1221 	}
1222 	vp = nvp;
1223 	vp->v_type = type;
1224 	vp->v_vflag |= VV_MPSAFE;
1225 	uvm_vnp_setsize(vp, 0);
1226 	spec_node_init(vp, dev);
1227 	*vpp = vp;
1228 	return (0);
1229 }
1230 
1231 /*
1232  * Try to gain a reference to a vnode, without acquiring its interlock.
1233  * The caller must hold a lock that will prevent the vnode from being
1234  * recycled or freed.
1235  */
1236 bool
1237 vtryget(vnode_t *vp)
1238 {
1239 	u_int use, next;
1240 
1241 	/*
1242 	 * If the vnode is being freed, don't make life any harder
1243 	 * for vclean() by adding another reference without waiting.
1244 	 * This is not strictly necessary, but we'll do it anyway.
1245 	 */
1246 	if (__predict_false((vp->v_iflag & VI_XLOCK) != 0)) {
1247 		return false;
1248 	}
1249 	for (use = vp->v_usecount;; use = next) {
1250 		if (use == 0 || __predict_false((use & VC_XLOCK) != 0)) {
1251 			/* Need interlock held if first reference. */
1252 			return false;
1253 		}
1254 		next = atomic_cas_uint(&vp->v_usecount, use, use + 1);
1255 		if (__predict_true(next == use)) {
1256 			return true;
1257 		}
1258 	}
1259 }
1260 
1261 /*
1262  * Grab a particular vnode from the free list, increment its
1263  * reference count and lock it. If the vnode lock bit is set the
1264  * vnode is being eliminated in vgone. In that case, we can not
1265  * grab the vnode, so the process is awakened when the transition is
1266  * completed, and an error returned to indicate that the vnode is no
1267  * longer usable (possibly having been changed to a new file system type).
1268  * Called with v_interlock held.
1269  */
1270 int
1271 vget(vnode_t *vp, int flags)
1272 {
1273 	int error = 0;
1274 
1275 	KASSERT((vp->v_iflag & VI_MARKER) == 0);
1276 	KASSERT(mutex_owned(&vp->v_interlock));
1277 	KASSERT((flags & ~(LK_SHARED|LK_EXCLUSIVE|LK_NOWAIT)) == 0);
1278 
1279 	/*
1280 	 * Before adding a reference, we must remove the vnode
1281 	 * from its freelist.
1282 	 */
1283 	if (vp->v_usecount == 0) {
1284 		vremfree(vp);
1285 		vp->v_usecount = 1;
1286 	} else {
1287 		atomic_inc_uint(&vp->v_usecount);
1288 	}
1289 
1290 	/*
1291 	 * If the vnode is in the process of being cleaned out for
1292 	 * another use, we wait for the cleaning to finish and then
1293 	 * return failure.  Cleaning is determined by checking if
1294 	 * the VI_XLOCK flag is set.
1295 	 */
1296 	if ((vp->v_iflag & VI_XLOCK) != 0) {
1297 		if ((flags & LK_NOWAIT) != 0) {
1298 			vrelel(vp, 0);
1299 			return EBUSY;
1300 		}
1301 		vwait(vp, VI_XLOCK);
1302 		vrelel(vp, 0);
1303 		return ENOENT;
1304 	}
1305 
1306 	/*
1307 	 * Ok, we got it in good shape.  Just locking left.
1308 	 */
1309 	KASSERT((vp->v_iflag & VI_CLEAN) == 0);
1310 	mutex_exit(&vp->v_interlock);
1311 	if (flags & (LK_EXCLUSIVE | LK_SHARED)) {
1312 		error = vn_lock(vp, flags);
1313 		if (error != 0) {
1314 			vrele(vp);
1315 		}
1316 	}
1317 	return error;
1318 }
1319 
1320 /*
1321  * vput(), just unlock and vrele()
1322  */
1323 void
1324 vput(vnode_t *vp)
1325 {
1326 
1327 	KASSERT((vp->v_iflag & VI_MARKER) == 0);
1328 
1329 	VOP_UNLOCK(vp);
1330 	vrele(vp);
1331 }
1332 
1333 /*
1334  * Try to drop reference on a vnode.  Abort if we are releasing the
1335  * last reference.  Note: this _must_ succeed if not the last reference.
1336  */
1337 static inline bool
1338 vtryrele(vnode_t *vp)
1339 {
1340 	u_int use, next;
1341 
1342 	for (use = vp->v_usecount;; use = next) {
1343 		if (use == 1) {
1344 			return false;
1345 		}
1346 		KASSERT((use & VC_MASK) > 1);
1347 		next = atomic_cas_uint(&vp->v_usecount, use, use - 1);
1348 		if (__predict_true(next == use)) {
1349 			return true;
1350 		}
1351 	}
1352 }
1353 
1354 /*
1355  * Vnode release.  If reference count drops to zero, call inactive
1356  * routine and either return to freelist or free to the pool.
1357  */
1358 void
1359 vrelel(vnode_t *vp, int flags)
1360 {
1361 	bool recycle, defer;
1362 	int error;
1363 
1364 	KASSERT(mutex_owned(&vp->v_interlock));
1365 	KASSERT((vp->v_iflag & VI_MARKER) == 0);
1366 	KASSERT(vp->v_freelisthd == NULL);
1367 
1368 	if (__predict_false(vp->v_op == dead_vnodeop_p &&
1369 	    (vp->v_iflag & (VI_CLEAN|VI_XLOCK)) == 0)) {
1370 		vpanic(vp, "dead but not clean");
1371 	}
1372 
1373 	/*
1374 	 * If not the last reference, just drop the reference count
1375 	 * and unlock.
1376 	 */
1377 	if (vtryrele(vp)) {
1378 		vp->v_iflag |= VI_INACTREDO;
1379 		mutex_exit(&vp->v_interlock);
1380 		return;
1381 	}
1382 	if (vp->v_usecount <= 0 || vp->v_writecount != 0) {
1383 		vpanic(vp, "vrelel: bad ref count");
1384 	}
1385 
1386 	KASSERT((vp->v_iflag & VI_XLOCK) == 0);
1387 
1388 	/*
1389 	 * If not clean, deactivate the vnode, but preserve
1390 	 * our reference across the call to VOP_INACTIVE().
1391 	 */
1392  retry:
1393 	if ((vp->v_iflag & VI_CLEAN) == 0) {
1394 		recycle = false;
1395 		vp->v_iflag |= VI_INACTNOW;
1396 
1397 		/*
1398 		 * XXX This ugly block can be largely eliminated if
1399 		 * locking is pushed down into the file systems.
1400 		 *
1401 		 * Defer vnode release to vrele_thread if caller
1402 		 * requests it explicitly.
1403 		 */
1404 		if ((curlwp == uvm.pagedaemon_lwp) ||
1405 		    (flags & VRELEL_ASYNC_RELE) != 0) {
1406 			/* The pagedaemon can't wait around; defer. */
1407 			defer = true;
1408 		} else if (curlwp == vrele_lwp) {
1409 			/* We have to try harder. */
1410 			vp->v_iflag &= ~VI_INACTREDO;
1411 			mutex_exit(&vp->v_interlock);
1412 			error = vn_lock(vp, LK_EXCLUSIVE);
1413 			if (error != 0) {
1414 				/* XXX */
1415 				vpanic(vp, "vrele: unable to lock %p");
1416 			}
1417 			defer = false;
1418 		} else if ((vp->v_iflag & VI_LAYER) != 0) {
1419 			/*
1420 			 * Acquiring the stack's lock in vclean() even
1421 			 * for an honest vput/vrele is dangerous because
1422 			 * our caller may hold other vnode locks; defer.
1423 			 */
1424 			defer = true;
1425 		} else {
1426 			/* If we can't acquire the lock, then defer. */
1427 			vp->v_iflag &= ~VI_INACTREDO;
1428 			mutex_exit(&vp->v_interlock);
1429 			error = vn_lock(vp, LK_EXCLUSIVE | LK_NOWAIT);
1430 			if (error != 0) {
1431 				defer = true;
1432 				mutex_enter(&vp->v_interlock);
1433 			} else {
1434 				defer = false;
1435 			}
1436 		}
1437 
1438 		if (defer) {
1439 			/*
1440 			 * Defer reclaim to the kthread; it's not safe to
1441 			 * clean it here.  We donate it our last reference.
1442 			 */
1443 			KASSERT(mutex_owned(&vp->v_interlock));
1444 			KASSERT((vp->v_iflag & VI_INACTPEND) == 0);
1445 			vp->v_iflag &= ~VI_INACTNOW;
1446 			vp->v_iflag |= VI_INACTPEND;
1447 			mutex_enter(&vrele_lock);
1448 			TAILQ_INSERT_TAIL(&vrele_list, vp, v_freelist);
1449 			if (++vrele_pending > (desiredvnodes >> 8))
1450 				cv_signal(&vrele_cv);
1451 			mutex_exit(&vrele_lock);
1452 			mutex_exit(&vp->v_interlock);
1453 			return;
1454 		}
1455 
1456 #ifdef DIAGNOSTIC
1457 		if ((vp->v_type == VBLK || vp->v_type == VCHR) &&
1458 		    vp->v_specnode != NULL && vp->v_specnode->sn_opencnt != 0) {
1459 			vprint("vrelel: missing VOP_CLOSE()", vp);
1460 		}
1461 #endif
1462 
1463 		/*
1464 		 * The vnode can gain another reference while being
1465 		 * deactivated.  If VOP_INACTIVE() indicates that
1466 		 * the described file has been deleted, then recycle
1467 		 * the vnode irrespective of additional references.
1468 		 * Another thread may be waiting to re-use the on-disk
1469 		 * inode.
1470 		 *
1471 		 * Note that VOP_INACTIVE() will drop the vnode lock.
1472 		 */
1473 		VOP_INACTIVE(vp, &recycle);
1474 		mutex_enter(&vp->v_interlock);
1475 		vp->v_iflag &= ~VI_INACTNOW;
1476 		if (!recycle) {
1477 			if (vtryrele(vp)) {
1478 				mutex_exit(&vp->v_interlock);
1479 				return;
1480 			}
1481 
1482 			/*
1483 			 * If we grew another reference while
1484 			 * VOP_INACTIVE() was underway, retry.
1485 			 */
1486 			if ((vp->v_iflag & VI_INACTREDO) != 0) {
1487 				goto retry;
1488 			}
1489 		}
1490 
1491 		/* Take care of space accounting. */
1492 		if (vp->v_iflag & VI_EXECMAP) {
1493 			atomic_add_int(&uvmexp.execpages,
1494 			    -vp->v_uobj.uo_npages);
1495 			atomic_add_int(&uvmexp.filepages,
1496 			    vp->v_uobj.uo_npages);
1497 		}
1498 		vp->v_iflag &= ~(VI_TEXT|VI_EXECMAP|VI_WRMAP);
1499 		vp->v_vflag &= ~VV_MAPPED;
1500 
1501 		/*
1502 		 * Recycle the vnode if the file is now unused (unlinked),
1503 		 * otherwise just free it.
1504 		 */
1505 		if (recycle) {
1506 			vclean(vp, DOCLOSE);
1507 		}
1508 		KASSERT(vp->v_usecount > 0);
1509 	}
1510 
1511 	if (atomic_dec_uint_nv(&vp->v_usecount) != 0) {
1512 		/* Gained another reference while being reclaimed. */
1513 		mutex_exit(&vp->v_interlock);
1514 		return;
1515 	}
1516 
1517 	if ((vp->v_iflag & VI_CLEAN) != 0) {
1518 		/*
1519 		 * It's clean so destroy it.  It isn't referenced
1520 		 * anywhere since it has been reclaimed.
1521 		 */
1522 		KASSERT(vp->v_holdcnt == 0);
1523 		KASSERT(vp->v_writecount == 0);
1524 		mutex_exit(&vp->v_interlock);
1525 		insmntque(vp, NULL);
1526 		if (vp->v_type == VBLK || vp->v_type == VCHR) {
1527 			spec_node_destroy(vp);
1528 		}
1529 		vnfree(vp);
1530 	} else {
1531 		/*
1532 		 * Otherwise, put it back onto the freelist.  It
1533 		 * can't be destroyed while still associated with
1534 		 * a file system.
1535 		 */
1536 		mutex_enter(&vnode_free_list_lock);
1537 		if (vp->v_holdcnt > 0) {
1538 			vp->v_freelisthd = &vnode_hold_list;
1539 		} else {
1540 			vp->v_freelisthd = &vnode_free_list;
1541 		}
1542 		TAILQ_INSERT_TAIL(vp->v_freelisthd, vp, v_freelist);
1543 		mutex_exit(&vnode_free_list_lock);
1544 		mutex_exit(&vp->v_interlock);
1545 	}
1546 }
1547 
1548 void
1549 vrele(vnode_t *vp)
1550 {
1551 
1552 	KASSERT((vp->v_iflag & VI_MARKER) == 0);
1553 
1554 	if ((vp->v_iflag & VI_INACTNOW) == 0 && vtryrele(vp)) {
1555 		return;
1556 	}
1557 	mutex_enter(&vp->v_interlock);
1558 	vrelel(vp, 0);
1559 }
1560 
1561 /*
1562  * Asynchronous vnode release, vnode is released in different context.
1563  */
1564 void
1565 vrele_async(vnode_t *vp)
1566 {
1567 
1568 	KASSERT((vp->v_iflag & VI_MARKER) == 0);
1569 
1570 	if ((vp->v_iflag & VI_INACTNOW) == 0 && vtryrele(vp)) {
1571 		return;
1572 	}
1573 
1574 	mutex_enter(&vp->v_interlock);
1575 	vrelel(vp, VRELEL_ASYNC_RELE);
1576 }
1577 
1578 static void
1579 vrele_thread(void *cookie)
1580 {
1581 	vnode_t *vp;
1582 
1583 	for (;;) {
1584 		mutex_enter(&vrele_lock);
1585 		while (TAILQ_EMPTY(&vrele_list)) {
1586 			vrele_gen++;
1587 			cv_broadcast(&vrele_cv);
1588 			cv_timedwait(&vrele_cv, &vrele_lock, hz);
1589 		}
1590 		vp = TAILQ_FIRST(&vrele_list);
1591 		TAILQ_REMOVE(&vrele_list, vp, v_freelist);
1592 		vrele_pending--;
1593 		mutex_exit(&vrele_lock);
1594 
1595 		/*
1596 		 * If not the last reference, then ignore the vnode
1597 		 * and look for more work.
1598 		 */
1599 		mutex_enter(&vp->v_interlock);
1600 		KASSERT((vp->v_iflag & VI_INACTPEND) != 0);
1601 		vp->v_iflag &= ~VI_INACTPEND;
1602 		vrelel(vp, 0);
1603 	}
1604 }
1605 
1606 /*
1607  * Page or buffer structure gets a reference.
1608  * Called with v_interlock held.
1609  */
1610 void
1611 vholdl(vnode_t *vp)
1612 {
1613 
1614 	KASSERT(mutex_owned(&vp->v_interlock));
1615 	KASSERT((vp->v_iflag & VI_MARKER) == 0);
1616 
1617 	if (vp->v_holdcnt++ == 0 && vp->v_usecount == 0) {
1618 		mutex_enter(&vnode_free_list_lock);
1619 		KASSERT(vp->v_freelisthd == &vnode_free_list);
1620 		TAILQ_REMOVE(vp->v_freelisthd, vp, v_freelist);
1621 		vp->v_freelisthd = &vnode_hold_list;
1622 		TAILQ_INSERT_TAIL(vp->v_freelisthd, vp, v_freelist);
1623 		mutex_exit(&vnode_free_list_lock);
1624 	}
1625 }
1626 
1627 /*
1628  * Page or buffer structure frees a reference.
1629  * Called with v_interlock held.
1630  */
1631 void
1632 holdrelel(vnode_t *vp)
1633 {
1634 
1635 	KASSERT(mutex_owned(&vp->v_interlock));
1636 	KASSERT((vp->v_iflag & VI_MARKER) == 0);
1637 
1638 	if (vp->v_holdcnt <= 0) {
1639 		vpanic(vp, "holdrelel: holdcnt vp %p");
1640 	}
1641 
1642 	vp->v_holdcnt--;
1643 	if (vp->v_holdcnt == 0 && vp->v_usecount == 0) {
1644 		mutex_enter(&vnode_free_list_lock);
1645 		KASSERT(vp->v_freelisthd == &vnode_hold_list);
1646 		TAILQ_REMOVE(vp->v_freelisthd, vp, v_freelist);
1647 		vp->v_freelisthd = &vnode_free_list;
1648 		TAILQ_INSERT_TAIL(vp->v_freelisthd, vp, v_freelist);
1649 		mutex_exit(&vnode_free_list_lock);
1650 	}
1651 }
1652 
1653 /*
1654  * Vnode reference, where a reference is already held by some other
1655  * object (for example, a file structure).
1656  */
1657 void
1658 vref(vnode_t *vp)
1659 {
1660 
1661 	KASSERT((vp->v_iflag & VI_MARKER) == 0);
1662 	KASSERT(vp->v_usecount != 0);
1663 
1664 	atomic_inc_uint(&vp->v_usecount);
1665 }
1666 
1667 /*
1668  * Remove any vnodes in the vnode table belonging to mount point mp.
1669  *
1670  * If FORCECLOSE is not specified, there should not be any active ones,
1671  * return error if any are found (nb: this is a user error, not a
1672  * system error). If FORCECLOSE is specified, detach any active vnodes
1673  * that are found.
1674  *
1675  * If WRITECLOSE is set, only flush out regular file vnodes open for
1676  * writing.
1677  *
1678  * SKIPSYSTEM causes any vnodes marked V_SYSTEM to be skipped.
1679  */
1680 #ifdef DEBUG
1681 int busyprt = 0;	/* print out busy vnodes */
1682 struct ctldebug debug1 = { "busyprt", &busyprt };
1683 #endif
1684 
1685 static vnode_t *
1686 vflushnext(vnode_t *mvp, int *when)
1687 {
1688 
1689 	if (hardclock_ticks > *when) {
1690 		mutex_exit(&mntvnode_lock);
1691 		yield();
1692 		mutex_enter(&mntvnode_lock);
1693 		*when = hardclock_ticks + hz / 10;
1694 	}
1695 
1696 	return vunmark(mvp);
1697 }
1698 
1699 int
1700 vflush(struct mount *mp, vnode_t *skipvp, int flags)
1701 {
1702 	vnode_t *vp, *mvp;
1703 	int busy = 0, when = 0, gen;
1704 
1705 	/*
1706 	 * First, flush out any vnode references from vrele_list.
1707 	 */
1708 	mutex_enter(&vrele_lock);
1709 	gen = vrele_gen;
1710 	while (vrele_pending && gen == vrele_gen) {
1711 		cv_broadcast(&vrele_cv);
1712 		cv_wait(&vrele_cv, &vrele_lock);
1713 	}
1714 	mutex_exit(&vrele_lock);
1715 
1716 	/* Allocate a marker vnode. */
1717 	if ((mvp = vnalloc(mp)) == NULL)
1718 		return (ENOMEM);
1719 
1720 	/*
1721 	 * NOTE: not using the TAILQ_FOREACH here since in this loop vgone()
1722 	 * and vclean() are called
1723 	 */
1724 	mutex_enter(&mntvnode_lock);
1725 	for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp != NULL;
1726 	    vp = vflushnext(mvp, &when)) {
1727 		vmark(mvp, vp);
1728 		if (vp->v_mount != mp || vismarker(vp))
1729 			continue;
1730 		/*
1731 		 * Skip over a selected vnode.
1732 		 */
1733 		if (vp == skipvp)
1734 			continue;
1735 		mutex_enter(&vp->v_interlock);
1736 		/*
1737 		 * Ignore clean but still referenced vnodes.
1738 		 */
1739 		if ((vp->v_iflag & VI_CLEAN) != 0) {
1740 			mutex_exit(&vp->v_interlock);
1741 			continue;
1742 		}
1743 		/*
1744 		 * Skip over a vnodes marked VSYSTEM.
1745 		 */
1746 		if ((flags & SKIPSYSTEM) && (vp->v_vflag & VV_SYSTEM)) {
1747 			mutex_exit(&vp->v_interlock);
1748 			continue;
1749 		}
1750 		/*
1751 		 * If WRITECLOSE is set, only flush out regular file
1752 		 * vnodes open for writing.
1753 		 */
1754 		if ((flags & WRITECLOSE) &&
1755 		    (vp->v_writecount == 0 || vp->v_type != VREG)) {
1756 			mutex_exit(&vp->v_interlock);
1757 			continue;
1758 		}
1759 		/*
1760 		 * With v_usecount == 0, all we need to do is clear
1761 		 * out the vnode data structures and we are done.
1762 		 */
1763 		if (vp->v_usecount == 0) {
1764 			mutex_exit(&mntvnode_lock);
1765 			vremfree(vp);
1766 			vp->v_usecount = 1;
1767 			vclean(vp, DOCLOSE);
1768 			vrelel(vp, 0);
1769 			mutex_enter(&mntvnode_lock);
1770 			continue;
1771 		}
1772 		/*
1773 		 * If FORCECLOSE is set, forcibly close the vnode.
1774 		 * For block or character devices, revert to an
1775 		 * anonymous device.  For all other files, just
1776 		 * kill them.
1777 		 */
1778 		if (flags & FORCECLOSE) {
1779 			mutex_exit(&mntvnode_lock);
1780 			atomic_inc_uint(&vp->v_usecount);
1781 			if (vp->v_type != VBLK && vp->v_type != VCHR) {
1782 				vclean(vp, DOCLOSE);
1783 				vrelel(vp, 0);
1784 			} else {
1785 				vclean(vp, 0);
1786 				vp->v_op = spec_vnodeop_p; /* XXXSMP */
1787 				mutex_exit(&vp->v_interlock);
1788 				/*
1789 				 * The vnode isn't clean, but still resides
1790 				 * on the mount list.  Remove it. XXX This
1791 				 * is a bit dodgy.
1792 				 */
1793 				insmntque(vp, NULL);
1794 				vrele(vp);
1795 			}
1796 			mutex_enter(&mntvnode_lock);
1797 			continue;
1798 		}
1799 #ifdef DEBUG
1800 		if (busyprt)
1801 			vprint("vflush: busy vnode", vp);
1802 #endif
1803 		mutex_exit(&vp->v_interlock);
1804 		busy++;
1805 	}
1806 	mutex_exit(&mntvnode_lock);
1807 	vnfree(mvp);
1808 	if (busy)
1809 		return (EBUSY);
1810 	return (0);
1811 }
1812 
1813 /*
1814  * Disassociate the underlying file system from a vnode.
1815  *
1816  * Must be called with the interlock held, and will return with it held.
1817  */
1818 void
1819 vclean(vnode_t *vp, int flags)
1820 {
1821 	lwp_t *l = curlwp;
1822 	bool recycle, active;
1823 	int error;
1824 
1825 	KASSERT(mutex_owned(&vp->v_interlock));
1826 	KASSERT((vp->v_iflag & VI_MARKER) == 0);
1827 	KASSERT(vp->v_usecount != 0);
1828 
1829 	/* If cleaning is already in progress wait until done and return. */
1830 	if (vp->v_iflag & VI_XLOCK) {
1831 		vwait(vp, VI_XLOCK);
1832 		return;
1833 	}
1834 
1835 	/* If already clean, nothing to do. */
1836 	if ((vp->v_iflag & VI_CLEAN) != 0) {
1837 		return;
1838 	}
1839 
1840 	/*
1841 	 * Prevent the vnode from being recycled or brought into use
1842 	 * while we clean it out.
1843 	 */
1844 	vp->v_iflag |= VI_XLOCK;
1845 	if (vp->v_iflag & VI_EXECMAP) {
1846 		atomic_add_int(&uvmexp.execpages, -vp->v_uobj.uo_npages);
1847 		atomic_add_int(&uvmexp.filepages, vp->v_uobj.uo_npages);
1848 	}
1849 	vp->v_iflag &= ~(VI_TEXT|VI_EXECMAP);
1850 	active = (vp->v_usecount > 1);
1851 
1852 	/* XXXAD should not lock vnode under layer */
1853 	mutex_exit(&vp->v_interlock);
1854 	VOP_LOCK(vp, LK_EXCLUSIVE);
1855 
1856 	/*
1857 	 * Clean out any cached data associated with the vnode.
1858 	 * If purging an active vnode, it must be closed and
1859 	 * deactivated before being reclaimed. Note that the
1860 	 * VOP_INACTIVE will unlock the vnode.
1861 	 */
1862 	if (flags & DOCLOSE) {
1863 		error = vinvalbuf(vp, V_SAVE, NOCRED, l, 0, 0);
1864 		if (error != 0) {
1865 			/* XXX, fix vn_start_write's grab of mp and use that. */
1866 
1867 			if (wapbl_vphaswapbl(vp))
1868 				WAPBL_DISCARD(wapbl_vptomp(vp));
1869 			error = vinvalbuf(vp, 0, NOCRED, l, 0, 0);
1870 		}
1871 		KASSERT(error == 0);
1872 		KASSERT((vp->v_iflag & VI_ONWORKLST) == 0);
1873 		if (active && (vp->v_type == VBLK || vp->v_type == VCHR)) {
1874 			 spec_node_revoke(vp);
1875 		}
1876 	}
1877 	if (active) {
1878 		VOP_INACTIVE(vp, &recycle);
1879 	} else {
1880 		/*
1881 		 * Any other processes trying to obtain this lock must first
1882 		 * wait for VI_XLOCK to clear, then call the new lock operation.
1883 		 */
1884 		VOP_UNLOCK(vp);
1885 	}
1886 
1887 	/* Disassociate the underlying file system from the vnode. */
1888 	if (VOP_RECLAIM(vp)) {
1889 		vpanic(vp, "vclean: cannot reclaim");
1890 	}
1891 
1892 	KASSERT(vp->v_uobj.uo_npages == 0);
1893 	if (vp->v_type == VREG && vp->v_ractx != NULL) {
1894 		uvm_ra_freectx(vp->v_ractx);
1895 		vp->v_ractx = NULL;
1896 	}
1897 	cache_purge(vp);
1898 
1899 	/* Done with purge, notify sleepers of the grim news. */
1900 	mutex_enter(&vp->v_interlock);
1901 	vp->v_op = dead_vnodeop_p;
1902 	vp->v_tag = VT_NON;
1903 	KNOTE(&vp->v_klist, NOTE_REVOKE);
1904 	vp->v_iflag &= ~VI_XLOCK;
1905 	vp->v_vflag &= ~VV_LOCKSWORK;
1906 	if ((flags & DOCLOSE) != 0) {
1907 		vp->v_iflag |= VI_CLEAN;
1908 	}
1909 	cv_broadcast(&vp->v_cv);
1910 
1911 	KASSERT((vp->v_iflag & VI_ONWORKLST) == 0);
1912 }
1913 
1914 /*
1915  * Recycle an unused vnode to the front of the free list.
1916  * Release the passed interlock if the vnode will be recycled.
1917  */
1918 int
1919 vrecycle(vnode_t *vp, kmutex_t *inter_lkp, struct lwp *l)
1920 {
1921 
1922 	KASSERT((vp->v_iflag & VI_MARKER) == 0);
1923 
1924 	mutex_enter(&vp->v_interlock);
1925 	if (vp->v_usecount != 0) {
1926 		mutex_exit(&vp->v_interlock);
1927 		return (0);
1928 	}
1929 	if (inter_lkp)
1930 		mutex_exit(inter_lkp);
1931 	vremfree(vp);
1932 	vp->v_usecount = 1;
1933 	vclean(vp, DOCLOSE);
1934 	vrelel(vp, 0);
1935 	return (1);
1936 }
1937 
1938 /*
1939  * Eliminate all activity associated with a vnode in preparation for
1940  * reuse.  Drops a reference from the vnode.
1941  */
1942 void
1943 vgone(vnode_t *vp)
1944 {
1945 
1946 	mutex_enter(&vp->v_interlock);
1947 	vclean(vp, DOCLOSE);
1948 	vrelel(vp, 0);
1949 }
1950 
1951 /*
1952  * Lookup a vnode by device number and return it referenced.
1953  */
1954 int
1955 vfinddev(dev_t dev, enum vtype type, vnode_t **vpp)
1956 {
1957 	vnode_t *vp;
1958 
1959 	mutex_enter(&device_lock);
1960 	for (vp = specfs_hash[SPECHASH(dev)]; vp; vp = vp->v_specnext) {
1961 		if (dev == vp->v_rdev && type == vp->v_type)
1962 			break;
1963 	}
1964 	if (vp == NULL) {
1965 		mutex_exit(&device_lock);
1966 		return 0;
1967 	}
1968 	mutex_enter(&vp->v_interlock);
1969 	mutex_exit(&device_lock);
1970 	if (vget(vp, 0) != 0)
1971 		return 0;
1972 	*vpp = vp;
1973 	return 1;
1974 }
1975 
1976 /*
1977  * Revoke all the vnodes corresponding to the specified minor number
1978  * range (endpoints inclusive) of the specified major.
1979  */
1980 void
1981 vdevgone(int maj, int minl, int minh, enum vtype type)
1982 {
1983 	vnode_t *vp, **vpp;
1984 	dev_t dev;
1985 	int mn;
1986 
1987 	vp = NULL;	/* XXX gcc */
1988 
1989 	mutex_enter(&device_lock);
1990 	for (mn = minl; mn <= minh; mn++) {
1991 		dev = makedev(maj, mn);
1992 		vpp = &specfs_hash[SPECHASH(dev)];
1993 		for (vp = *vpp; vp != NULL;) {
1994 			mutex_enter(&vp->v_interlock);
1995 			if ((vp->v_iflag & VI_CLEAN) != 0 ||
1996 			    dev != vp->v_rdev || type != vp->v_type) {
1997 				mutex_exit(&vp->v_interlock);
1998 				vp = vp->v_specnext;
1999 				continue;
2000 			}
2001 			mutex_exit(&device_lock);
2002 			if (vget(vp, 0) == 0) {
2003 				VOP_REVOKE(vp, REVOKEALL);
2004 				vrele(vp);
2005 			}
2006 			mutex_enter(&device_lock);
2007 			vp = *vpp;
2008 		}
2009 	}
2010 	mutex_exit(&device_lock);
2011 }
2012 
2013 /*
2014  * Eliminate all activity associated with the requested vnode
2015  * and with all vnodes aliased to the requested vnode.
2016  */
2017 void
2018 vrevoke(vnode_t *vp)
2019 {
2020 	vnode_t *vq, **vpp;
2021 	enum vtype type;
2022 	dev_t dev;
2023 
2024 	KASSERT(vp->v_usecount > 0);
2025 
2026 	mutex_enter(&vp->v_interlock);
2027 	if ((vp->v_iflag & VI_CLEAN) != 0) {
2028 		mutex_exit(&vp->v_interlock);
2029 		return;
2030 	} else if (vp->v_type != VBLK && vp->v_type != VCHR) {
2031 		atomic_inc_uint(&vp->v_usecount);
2032 		vclean(vp, DOCLOSE);
2033 		vrelel(vp, 0);
2034 		return;
2035 	} else {
2036 		dev = vp->v_rdev;
2037 		type = vp->v_type;
2038 		mutex_exit(&vp->v_interlock);
2039 	}
2040 
2041 	vpp = &specfs_hash[SPECHASH(dev)];
2042 	mutex_enter(&device_lock);
2043 	for (vq = *vpp; vq != NULL;) {
2044 		/* If clean or being cleaned, then ignore it. */
2045 		mutex_enter(&vq->v_interlock);
2046 		if ((vq->v_iflag & (VI_CLEAN | VI_XLOCK)) != 0 ||
2047 		    vq->v_rdev != dev || vq->v_type != type) {
2048 			mutex_exit(&vq->v_interlock);
2049 			vq = vq->v_specnext;
2050 			continue;
2051 		}
2052 		mutex_exit(&device_lock);
2053 		if (vq->v_usecount == 0) {
2054 			vremfree(vq);
2055 			vq->v_usecount = 1;
2056 		} else {
2057 			atomic_inc_uint(&vq->v_usecount);
2058 		}
2059 		vclean(vq, DOCLOSE);
2060 		vrelel(vq, 0);
2061 		mutex_enter(&device_lock);
2062 		vq = *vpp;
2063 	}
2064 	mutex_exit(&device_lock);
2065 }
2066 
2067 /*
2068  * sysctl helper routine to return list of supported fstypes
2069  */
2070 int
2071 sysctl_vfs_generic_fstypes(SYSCTLFN_ARGS)
2072 {
2073 	char bf[sizeof(((struct statvfs *)NULL)->f_fstypename)];
2074 	char *where = oldp;
2075 	struct vfsops *v;
2076 	size_t needed, left, slen;
2077 	int error, first;
2078 
2079 	if (newp != NULL)
2080 		return (EPERM);
2081 	if (namelen != 0)
2082 		return (EINVAL);
2083 
2084 	first = 1;
2085 	error = 0;
2086 	needed = 0;
2087 	left = *oldlenp;
2088 
2089 	sysctl_unlock();
2090 	mutex_enter(&vfs_list_lock);
2091 	LIST_FOREACH(v, &vfs_list, vfs_list) {
2092 		if (where == NULL)
2093 			needed += strlen(v->vfs_name) + 1;
2094 		else {
2095 			memset(bf, 0, sizeof(bf));
2096 			if (first) {
2097 				strncpy(bf, v->vfs_name, sizeof(bf));
2098 				first = 0;
2099 			} else {
2100 				bf[0] = ' ';
2101 				strncpy(bf + 1, v->vfs_name, sizeof(bf) - 1);
2102 			}
2103 			bf[sizeof(bf)-1] = '\0';
2104 			slen = strlen(bf);
2105 			if (left < slen + 1)
2106 				break;
2107 			v->vfs_refcount++;
2108 			mutex_exit(&vfs_list_lock);
2109 			/* +1 to copy out the trailing NUL byte */
2110 			error = copyout(bf, where, slen + 1);
2111 			mutex_enter(&vfs_list_lock);
2112 			v->vfs_refcount--;
2113 			if (error)
2114 				break;
2115 			where += slen;
2116 			needed += slen;
2117 			left -= slen;
2118 		}
2119 	}
2120 	mutex_exit(&vfs_list_lock);
2121 	sysctl_relock();
2122 	*oldlenp = needed;
2123 	return (error);
2124 }
2125 
2126 
2127 int kinfo_vdebug = 1;
2128 int kinfo_vgetfailed;
2129 #define KINFO_VNODESLOP	10
2130 /*
2131  * Dump vnode list (via sysctl).
2132  * Copyout address of vnode followed by vnode.
2133  */
2134 /* ARGSUSED */
2135 int
2136 sysctl_kern_vnode(SYSCTLFN_ARGS)
2137 {
2138 	char *where = oldp;
2139 	size_t *sizep = oldlenp;
2140 	struct mount *mp, *nmp;
2141 	vnode_t *vp, *mvp, vbuf;
2142 	char *bp = where;
2143 	char *ewhere;
2144 	int error;
2145 
2146 	if (namelen != 0)
2147 		return (EOPNOTSUPP);
2148 	if (newp != NULL)
2149 		return (EPERM);
2150 
2151 #define VPTRSZ	sizeof(vnode_t *)
2152 #define VNODESZ	sizeof(vnode_t)
2153 	if (where == NULL) {
2154 		*sizep = (numvnodes + KINFO_VNODESLOP) * (VPTRSZ + VNODESZ);
2155 		return (0);
2156 	}
2157 	ewhere = where + *sizep;
2158 
2159 	sysctl_unlock();
2160 	mutex_enter(&mountlist_lock);
2161 	for (mp = CIRCLEQ_FIRST(&mountlist); mp != (void *)&mountlist;
2162 	    mp = nmp) {
2163 		if (vfs_busy(mp, &nmp)) {
2164 			continue;
2165 		}
2166 		/* Allocate a marker vnode. */
2167 		mvp = vnalloc(mp);
2168 		/* Should never fail for mp != NULL */
2169 		KASSERT(mvp != NULL);
2170 		mutex_enter(&mntvnode_lock);
2171 		for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp;
2172 		    vp = vunmark(mvp)) {
2173 			vmark(mvp, vp);
2174 			/*
2175 			 * Check that the vp is still associated with
2176 			 * this filesystem.  RACE: could have been
2177 			 * recycled onto the same filesystem.
2178 			 */
2179 			if (vp->v_mount != mp || vismarker(vp))
2180 				continue;
2181 			if (bp + VPTRSZ + VNODESZ > ewhere) {
2182 				(void)vunmark(mvp);
2183 				mutex_exit(&mntvnode_lock);
2184 				vnfree(mvp);
2185 				vfs_unbusy(mp, false, NULL);
2186 				sysctl_relock();
2187 				*sizep = bp - where;
2188 				return (ENOMEM);
2189 			}
2190 			memcpy(&vbuf, vp, VNODESZ);
2191 			mutex_exit(&mntvnode_lock);
2192 			if ((error = copyout(&vp, bp, VPTRSZ)) ||
2193 			    (error = copyout(&vbuf, bp + VPTRSZ, VNODESZ))) {
2194 			   	mutex_enter(&mntvnode_lock);
2195 				(void)vunmark(mvp);
2196 				mutex_exit(&mntvnode_lock);
2197 				vnfree(mvp);
2198 				vfs_unbusy(mp, false, NULL);
2199 				sysctl_relock();
2200 				return (error);
2201 			}
2202 			bp += VPTRSZ + VNODESZ;
2203 			mutex_enter(&mntvnode_lock);
2204 		}
2205 		mutex_exit(&mntvnode_lock);
2206 		vnfree(mvp);
2207 		vfs_unbusy(mp, false, &nmp);
2208 	}
2209 	mutex_exit(&mountlist_lock);
2210 	sysctl_relock();
2211 
2212 	*sizep = bp - where;
2213 	return (0);
2214 }
2215 
2216 /*
2217  * Remove clean vnodes from a mountpoint's vnode list.
2218  */
2219 void
2220 vfs_scrubvnlist(struct mount *mp)
2221 {
2222 	vnode_t *vp, *nvp;
2223 
2224  retry:
2225 	mutex_enter(&mntvnode_lock);
2226 	for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = nvp) {
2227 		nvp = TAILQ_NEXT(vp, v_mntvnodes);
2228 		mutex_enter(&vp->v_interlock);
2229 		if ((vp->v_iflag & VI_CLEAN) != 0) {
2230 			TAILQ_REMOVE(&mp->mnt_vnodelist, vp, v_mntvnodes);
2231 			vp->v_mount = NULL;
2232 			mutex_exit(&mntvnode_lock);
2233 			mutex_exit(&vp->v_interlock);
2234 			vfs_destroy(mp);
2235 			goto retry;
2236 		}
2237 		mutex_exit(&vp->v_interlock);
2238 	}
2239 	mutex_exit(&mntvnode_lock);
2240 }
2241 
2242 /*
2243  * Check to see if a filesystem is mounted on a block device.
2244  */
2245 int
2246 vfs_mountedon(vnode_t *vp)
2247 {
2248 	vnode_t *vq;
2249 	int error = 0;
2250 
2251 	if (vp->v_type != VBLK)
2252 		return ENOTBLK;
2253 	if (vp->v_specmountpoint != NULL)
2254 		return (EBUSY);
2255 	mutex_enter(&device_lock);
2256 	for (vq = specfs_hash[SPECHASH(vp->v_rdev)]; vq != NULL;
2257 	    vq = vq->v_specnext) {
2258 		if (vq->v_rdev != vp->v_rdev || vq->v_type != vp->v_type)
2259 			continue;
2260 		if (vq->v_specmountpoint != NULL) {
2261 			error = EBUSY;
2262 			break;
2263 		}
2264 	}
2265 	mutex_exit(&device_lock);
2266 	return (error);
2267 }
2268 
2269 /*
2270  * Unmount all file systems.
2271  * We traverse the list in reverse order under the assumption that doing so
2272  * will avoid needing to worry about dependencies.
2273  */
2274 bool
2275 vfs_unmountall(struct lwp *l)
2276 {
2277 
2278 	printf("unmounting file systems...");
2279 	return vfs_unmountall1(l, true, true);
2280 }
2281 
2282 static void
2283 vfs_unmount_print(struct mount *mp, const char *pfx)
2284 {
2285 
2286 	aprint_verbose("%sunmounted %s on %s type %s\n", pfx,
2287 	    mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname,
2288 	    mp->mnt_stat.f_fstypename);
2289 }
2290 
2291 bool
2292 vfs_unmount_forceone(struct lwp *l)
2293 {
2294 	struct mount *mp, *nmp;
2295 	int error;
2296 
2297 	nmp = NULL;
2298 
2299 	CIRCLEQ_FOREACH_REVERSE(mp, &mountlist, mnt_list) {
2300 		if (nmp == NULL || mp->mnt_gen > nmp->mnt_gen) {
2301 			nmp = mp;
2302 		}
2303 	}
2304 	if (nmp == NULL) {
2305 		return false;
2306 	}
2307 
2308 #ifdef DEBUG
2309 	printf("\nforcefully unmounting %s (%s)...",
2310 	    nmp->mnt_stat.f_mntonname, nmp->mnt_stat.f_mntfromname);
2311 #endif
2312 	atomic_inc_uint(&nmp->mnt_refcnt);
2313 	if ((error = dounmount(nmp, MNT_FORCE, l)) == 0) {
2314 		vfs_unmount_print(nmp, "forcefully ");
2315 		return true;
2316 	} else {
2317 		vfs_destroy(nmp);
2318 	}
2319 
2320 #ifdef DEBUG
2321 	printf("forceful unmount of %s failed with error %d\n",
2322 	    nmp->mnt_stat.f_mntonname, error);
2323 #endif
2324 
2325 	return false;
2326 }
2327 
2328 bool
2329 vfs_unmountall1(struct lwp *l, bool force, bool verbose)
2330 {
2331 	struct mount *mp, *nmp;
2332 	bool any_error = false, progress = false;
2333 	int error;
2334 
2335 	for (mp = CIRCLEQ_LAST(&mountlist);
2336 	     mp != (void *)&mountlist;
2337 	     mp = nmp) {
2338 		nmp = CIRCLEQ_PREV(mp, mnt_list);
2339 #ifdef DEBUG
2340 		printf("\nunmounting %p %s (%s)...",
2341 		    (void *)mp, mp->mnt_stat.f_mntonname,
2342 		    mp->mnt_stat.f_mntfromname);
2343 #endif
2344 		atomic_inc_uint(&mp->mnt_refcnt);
2345 		if ((error = dounmount(mp, force ? MNT_FORCE : 0, l)) == 0) {
2346 			vfs_unmount_print(mp, "");
2347 			progress = true;
2348 		} else {
2349 			vfs_destroy(mp);
2350 			if (verbose) {
2351 				printf("unmount of %s failed with error %d\n",
2352 				    mp->mnt_stat.f_mntonname, error);
2353 			}
2354 			any_error = true;
2355 		}
2356 	}
2357 	if (verbose) {
2358 		printf(" done\n");
2359 	}
2360 	if (any_error && verbose) {
2361 		printf("WARNING: some file systems would not unmount\n");
2362 	}
2363 	return progress;
2364 }
2365 
2366 /*
2367  * Sync and unmount file systems before shutting down.
2368  */
2369 void
2370 vfs_shutdown(void)
2371 {
2372 	struct lwp *l;
2373 
2374 	/* XXX we're certainly not running in lwp0's context! */
2375 	l = (curlwp == NULL) ? &lwp0 : curlwp;
2376 
2377 	vfs_shutdown1(l);
2378 }
2379 
2380 void
2381 vfs_sync_all(struct lwp *l)
2382 {
2383 	printf("syncing disks... ");
2384 
2385 	/* remove user processes from run queue */
2386 	suspendsched();
2387 	(void) spl0();
2388 
2389 	/* avoid coming back this way again if we panic. */
2390 	doing_shutdown = 1;
2391 
2392 	sys_sync(l, NULL, NULL);
2393 
2394 	/* Wait for sync to finish. */
2395 	if (buf_syncwait() != 0) {
2396 #if defined(DDB) && defined(DEBUG_HALT_BUSY)
2397 		Debugger();
2398 #endif
2399 		printf("giving up\n");
2400 		return;
2401 	} else
2402 		printf("done\n");
2403 }
2404 
2405 static void
2406 vfs_shutdown1(struct lwp *l)
2407 {
2408 
2409 	vfs_sync_all(l);
2410 
2411 	/*
2412 	 * If we've panic'd, don't make the situation potentially
2413 	 * worse by unmounting the file systems.
2414 	 */
2415 	if (panicstr != NULL)
2416 		return;
2417 
2418 	/* Release inodes held by texts before update. */
2419 #ifdef notdef
2420 	vnshutdown();
2421 #endif
2422 	/* Unmount file systems. */
2423 	vfs_unmountall(l);
2424 }
2425 
2426 /*
2427  * Print a list of supported file system types (used by vfs_mountroot)
2428  */
2429 static void
2430 vfs_print_fstypes(void)
2431 {
2432 	struct vfsops *v;
2433 	int cnt = 0;
2434 
2435 	mutex_enter(&vfs_list_lock);
2436 	LIST_FOREACH(v, &vfs_list, vfs_list)
2437 		++cnt;
2438 	mutex_exit(&vfs_list_lock);
2439 
2440 	if (cnt == 0) {
2441 		printf("WARNING: No file system modules have been loaded.\n");
2442 		return;
2443 	}
2444 
2445 	printf("Supported file systems:");
2446 	mutex_enter(&vfs_list_lock);
2447 	LIST_FOREACH(v, &vfs_list, vfs_list) {
2448 		printf(" %s", v->vfs_name);
2449 	}
2450 	mutex_exit(&vfs_list_lock);
2451 	printf("\n");
2452 }
2453 
2454 /*
2455  * Mount the root file system.  If the operator didn't specify a
2456  * file system to use, try all possible file systems until one
2457  * succeeds.
2458  */
2459 int
2460 vfs_mountroot(void)
2461 {
2462 	struct vfsops *v;
2463 	int error = ENODEV;
2464 
2465 	if (root_device == NULL)
2466 		panic("vfs_mountroot: root device unknown");
2467 
2468 	switch (device_class(root_device)) {
2469 	case DV_IFNET:
2470 		if (rootdev != NODEV)
2471 			panic("vfs_mountroot: rootdev set for DV_IFNET "
2472 			    "(0x%llx -> %llu,%llu)",
2473 			    (unsigned long long)rootdev,
2474 			    (unsigned long long)major(rootdev),
2475 			    (unsigned long long)minor(rootdev));
2476 		break;
2477 
2478 	case DV_DISK:
2479 		if (rootdev == NODEV)
2480 			panic("vfs_mountroot: rootdev not set for DV_DISK");
2481 	        if (bdevvp(rootdev, &rootvp))
2482 	                panic("vfs_mountroot: can't get vnode for rootdev");
2483 		error = VOP_OPEN(rootvp, FREAD, FSCRED);
2484 		if (error) {
2485 			printf("vfs_mountroot: can't open root device\n");
2486 			return (error);
2487 		}
2488 		break;
2489 
2490 	case DV_VIRTUAL:
2491 		break;
2492 
2493 	default:
2494 		printf("%s: inappropriate for root file system\n",
2495 		    device_xname(root_device));
2496 		return (ENODEV);
2497 	}
2498 
2499 	/*
2500 	 * If user specified a root fs type, use it.  Make sure the
2501 	 * specified type exists and has a mount_root()
2502 	 */
2503 	if (strcmp(rootfstype, ROOT_FSTYPE_ANY) != 0) {
2504 		v = vfs_getopsbyname(rootfstype);
2505 		error = EFTYPE;
2506 		if (v != NULL) {
2507 			if (v->vfs_mountroot != NULL) {
2508 				error = (v->vfs_mountroot)();
2509 			}
2510 			v->vfs_refcount--;
2511 		}
2512 		goto done;
2513 	}
2514 
2515 	/*
2516 	 * Try each file system currently configured into the kernel.
2517 	 */
2518 	mutex_enter(&vfs_list_lock);
2519 	LIST_FOREACH(v, &vfs_list, vfs_list) {
2520 		if (v->vfs_mountroot == NULL)
2521 			continue;
2522 #ifdef DEBUG
2523 		aprint_normal("mountroot: trying %s...\n", v->vfs_name);
2524 #endif
2525 		v->vfs_refcount++;
2526 		mutex_exit(&vfs_list_lock);
2527 		error = (*v->vfs_mountroot)();
2528 		mutex_enter(&vfs_list_lock);
2529 		v->vfs_refcount--;
2530 		if (!error) {
2531 			aprint_normal("root file system type: %s\n",
2532 			    v->vfs_name);
2533 			break;
2534 		}
2535 	}
2536 	mutex_exit(&vfs_list_lock);
2537 
2538 	if (v == NULL) {
2539 		vfs_print_fstypes();
2540 		printf("no file system for %s", device_xname(root_device));
2541 		if (device_class(root_device) == DV_DISK)
2542 			printf(" (dev 0x%llx)", (unsigned long long)rootdev);
2543 		printf("\n");
2544 		error = EFTYPE;
2545 	}
2546 
2547 done:
2548 	if (error && device_class(root_device) == DV_DISK) {
2549 		VOP_CLOSE(rootvp, FREAD, FSCRED);
2550 		vrele(rootvp);
2551 	}
2552 	if (error == 0) {
2553 		extern struct cwdinfo cwdi0;
2554 
2555 		CIRCLEQ_FIRST(&mountlist)->mnt_flag |= MNT_ROOTFS;
2556 		CIRCLEQ_FIRST(&mountlist)->mnt_op->vfs_refcount++;
2557 
2558 		/*
2559 		 * Get the vnode for '/'.  Set cwdi0.cwdi_cdir to
2560 		 * reference it.
2561 		 */
2562 		error = VFS_ROOT(CIRCLEQ_FIRST(&mountlist), &rootvnode);
2563 		if (error)
2564 			panic("cannot find root vnode, error=%d", error);
2565 		cwdi0.cwdi_cdir = rootvnode;
2566 		vref(cwdi0.cwdi_cdir);
2567 		VOP_UNLOCK(rootvnode);
2568 		cwdi0.cwdi_rdir = NULL;
2569 
2570 		/*
2571 		 * Now that root is mounted, we can fixup initproc's CWD
2572 		 * info.  All other processes are kthreads, which merely
2573 		 * share proc0's CWD info.
2574 		 */
2575 		initproc->p_cwdi->cwdi_cdir = rootvnode;
2576 		vref(initproc->p_cwdi->cwdi_cdir);
2577 		initproc->p_cwdi->cwdi_rdir = NULL;
2578 		/*
2579 		 * Enable loading of modules from the filesystem
2580 		 */
2581 		module_load_vfs_init();
2582 
2583 	}
2584 	return (error);
2585 }
2586 
2587 /*
2588  * Get a new unique fsid
2589  */
2590 void
2591 vfs_getnewfsid(struct mount *mp)
2592 {
2593 	static u_short xxxfs_mntid;
2594 	fsid_t tfsid;
2595 	int mtype;
2596 
2597 	mutex_enter(&mntid_lock);
2598 	mtype = makefstype(mp->mnt_op->vfs_name);
2599 	mp->mnt_stat.f_fsidx.__fsid_val[0] = makedev(mtype, 0);
2600 	mp->mnt_stat.f_fsidx.__fsid_val[1] = mtype;
2601 	mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
2602 	if (xxxfs_mntid == 0)
2603 		++xxxfs_mntid;
2604 	tfsid.__fsid_val[0] = makedev(mtype & 0xff, xxxfs_mntid);
2605 	tfsid.__fsid_val[1] = mtype;
2606 	if (!CIRCLEQ_EMPTY(&mountlist)) {
2607 		while (vfs_getvfs(&tfsid)) {
2608 			tfsid.__fsid_val[0]++;
2609 			xxxfs_mntid++;
2610 		}
2611 	}
2612 	mp->mnt_stat.f_fsidx.__fsid_val[0] = tfsid.__fsid_val[0];
2613 	mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
2614 	mutex_exit(&mntid_lock);
2615 }
2616 
2617 /*
2618  * Make a 'unique' number from a mount type name.
2619  */
2620 long
2621 makefstype(const char *type)
2622 {
2623 	long rv;
2624 
2625 	for (rv = 0; *type; type++) {
2626 		rv <<= 2;
2627 		rv ^= *type;
2628 	}
2629 	return rv;
2630 }
2631 
2632 /*
2633  * Set vnode attributes to VNOVAL
2634  */
2635 void
2636 vattr_null(struct vattr *vap)
2637 {
2638 
2639 	memset(vap, 0, sizeof(*vap));
2640 
2641 	vap->va_type = VNON;
2642 
2643 	/*
2644 	 * Assign individually so that it is safe even if size and
2645 	 * sign of each member are varied.
2646 	 */
2647 	vap->va_mode = VNOVAL;
2648 	vap->va_nlink = VNOVAL;
2649 	vap->va_uid = VNOVAL;
2650 	vap->va_gid = VNOVAL;
2651 	vap->va_fsid = VNOVAL;
2652 	vap->va_fileid = VNOVAL;
2653 	vap->va_size = VNOVAL;
2654 	vap->va_blocksize = VNOVAL;
2655 	vap->va_atime.tv_sec =
2656 	    vap->va_mtime.tv_sec =
2657 	    vap->va_ctime.tv_sec =
2658 	    vap->va_birthtime.tv_sec = VNOVAL;
2659 	vap->va_atime.tv_nsec =
2660 	    vap->va_mtime.tv_nsec =
2661 	    vap->va_ctime.tv_nsec =
2662 	    vap->va_birthtime.tv_nsec = VNOVAL;
2663 	vap->va_gen = VNOVAL;
2664 	vap->va_flags = VNOVAL;
2665 	vap->va_rdev = VNOVAL;
2666 	vap->va_bytes = VNOVAL;
2667 }
2668 
2669 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
2670 #define ARRAY_PRINT(idx, arr) \
2671     ((unsigned int)(idx) < ARRAY_SIZE(arr) ? (arr)[(idx)] : "UNKNOWN")
2672 
2673 const char * const vnode_tags[] = { VNODE_TAGS };
2674 const char * const vnode_types[] = { VNODE_TYPES };
2675 const char vnode_flagbits[] = VNODE_FLAGBITS;
2676 
2677 /*
2678  * Print out a description of a vnode.
2679  */
2680 void
2681 vprint(const char *label, struct vnode *vp)
2682 {
2683 	char bf[96];
2684 	int flag;
2685 
2686 	flag = vp->v_iflag | vp->v_vflag | vp->v_uflag;
2687 	snprintb(bf, sizeof(bf), vnode_flagbits, flag);
2688 
2689 	if (label != NULL)
2690 		printf("%s: ", label);
2691 	printf("vnode @ %p, flags (%s)\n\ttag %s(%d), type %s(%d), "
2692 	    "usecount %d, writecount %d, holdcount %d\n"
2693 	    "\tfreelisthd %p, mount %p, data %p lock %p\n",
2694 	    vp, bf, ARRAY_PRINT(vp->v_tag, vnode_tags), vp->v_tag,
2695 	    ARRAY_PRINT(vp->v_type, vnode_types), vp->v_type,
2696 	    vp->v_usecount, vp->v_writecount, vp->v_holdcnt,
2697 	    vp->v_freelisthd, vp->v_mount, vp->v_data, &vp->v_lock);
2698 	if (vp->v_data != NULL) {
2699 		printf("\t");
2700 		VOP_PRINT(vp);
2701 	}
2702 }
2703 
2704 #ifdef DEBUG
2705 /*
2706  * List all of the locked vnodes in the system.
2707  * Called when debugging the kernel.
2708  */
2709 void
2710 printlockedvnodes(void)
2711 {
2712 	struct mount *mp, *nmp;
2713 	struct vnode *vp;
2714 
2715 	printf("Locked vnodes\n");
2716 	mutex_enter(&mountlist_lock);
2717 	for (mp = CIRCLEQ_FIRST(&mountlist); mp != (void *)&mountlist;
2718 	     mp = nmp) {
2719 		if (vfs_busy(mp, &nmp)) {
2720 			continue;
2721 		}
2722 		TAILQ_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) {
2723 			if (VOP_ISLOCKED(vp))
2724 				vprint(NULL, vp);
2725 		}
2726 		mutex_enter(&mountlist_lock);
2727 		vfs_unbusy(mp, false, &nmp);
2728 	}
2729 	mutex_exit(&mountlist_lock);
2730 }
2731 #endif
2732 
2733 /* Deprecated. Kept for KPI compatibility. */
2734 int
2735 vaccess(enum vtype type, mode_t file_mode, uid_t uid, gid_t gid,
2736     mode_t acc_mode, kauth_cred_t cred)
2737 {
2738 
2739 #ifdef DIAGNOSTIC
2740 	printf("vaccess: deprecated interface used.\n");
2741 #endif /* DIAGNOSTIC */
2742 
2743 	return genfs_can_access(type, file_mode, uid, gid, acc_mode, cred);
2744 }
2745 
2746 /*
2747  * Given a file system name, look up the vfsops for that
2748  * file system, or return NULL if file system isn't present
2749  * in the kernel.
2750  */
2751 struct vfsops *
2752 vfs_getopsbyname(const char *name)
2753 {
2754 	struct vfsops *v;
2755 
2756 	mutex_enter(&vfs_list_lock);
2757 	LIST_FOREACH(v, &vfs_list, vfs_list) {
2758 		if (strcmp(v->vfs_name, name) == 0)
2759 			break;
2760 	}
2761 	if (v != NULL)
2762 		v->vfs_refcount++;
2763 	mutex_exit(&vfs_list_lock);
2764 
2765 	return (v);
2766 }
2767 
2768 void
2769 copy_statvfs_info(struct statvfs *sbp, const struct mount *mp)
2770 {
2771 	const struct statvfs *mbp;
2772 
2773 	if (sbp == (mbp = &mp->mnt_stat))
2774 		return;
2775 
2776 	(void)memcpy(&sbp->f_fsidx, &mbp->f_fsidx, sizeof(sbp->f_fsidx));
2777 	sbp->f_fsid = mbp->f_fsid;
2778 	sbp->f_owner = mbp->f_owner;
2779 	sbp->f_flag = mbp->f_flag;
2780 	sbp->f_syncwrites = mbp->f_syncwrites;
2781 	sbp->f_asyncwrites = mbp->f_asyncwrites;
2782 	sbp->f_syncreads = mbp->f_syncreads;
2783 	sbp->f_asyncreads = mbp->f_asyncreads;
2784 	(void)memcpy(sbp->f_spare, mbp->f_spare, sizeof(mbp->f_spare));
2785 	(void)memcpy(sbp->f_fstypename, mbp->f_fstypename,
2786 	    sizeof(sbp->f_fstypename));
2787 	(void)memcpy(sbp->f_mntonname, mbp->f_mntonname,
2788 	    sizeof(sbp->f_mntonname));
2789 	(void)memcpy(sbp->f_mntfromname, mp->mnt_stat.f_mntfromname,
2790 	    sizeof(sbp->f_mntfromname));
2791 	sbp->f_namemax = mbp->f_namemax;
2792 }
2793 
2794 int
2795 set_statvfs_info(const char *onp, int ukon, const char *fromp, int ukfrom,
2796     const char *vfsname, struct mount *mp, struct lwp *l)
2797 {
2798 	int error;
2799 	size_t size;
2800 	struct statvfs *sfs = &mp->mnt_stat;
2801 	int (*fun)(const void *, void *, size_t, size_t *);
2802 
2803 	(void)strlcpy(mp->mnt_stat.f_fstypename, vfsname,
2804 	    sizeof(mp->mnt_stat.f_fstypename));
2805 
2806 	if (onp) {
2807 		struct cwdinfo *cwdi = l->l_proc->p_cwdi;
2808 		fun = (ukon == UIO_SYSSPACE) ? copystr : copyinstr;
2809 		if (cwdi->cwdi_rdir != NULL) {
2810 			size_t len;
2811 			char *bp;
2812 			char *path = PNBUF_GET();
2813 
2814 			bp = path + MAXPATHLEN;
2815 			*--bp = '\0';
2816 			rw_enter(&cwdi->cwdi_lock, RW_READER);
2817 			error = getcwd_common(cwdi->cwdi_rdir, rootvnode, &bp,
2818 			    path, MAXPATHLEN / 2, 0, l);
2819 			rw_exit(&cwdi->cwdi_lock);
2820 			if (error) {
2821 				PNBUF_PUT(path);
2822 				return error;
2823 			}
2824 
2825 			len = strlen(bp);
2826 			if (len > sizeof(sfs->f_mntonname) - 1)
2827 				len = sizeof(sfs->f_mntonname) - 1;
2828 			(void)strncpy(sfs->f_mntonname, bp, len);
2829 			PNBUF_PUT(path);
2830 
2831 			if (len < sizeof(sfs->f_mntonname) - 1) {
2832 				error = (*fun)(onp, &sfs->f_mntonname[len],
2833 				    sizeof(sfs->f_mntonname) - len - 1, &size);
2834 				if (error)
2835 					return error;
2836 				size += len;
2837 			} else {
2838 				size = len;
2839 			}
2840 		} else {
2841 			error = (*fun)(onp, &sfs->f_mntonname,
2842 			    sizeof(sfs->f_mntonname) - 1, &size);
2843 			if (error)
2844 				return error;
2845 		}
2846 		(void)memset(sfs->f_mntonname + size, 0,
2847 		    sizeof(sfs->f_mntonname) - size);
2848 	}
2849 
2850 	if (fromp) {
2851 		fun = (ukfrom == UIO_SYSSPACE) ? copystr : copyinstr;
2852 		error = (*fun)(fromp, sfs->f_mntfromname,
2853 		    sizeof(sfs->f_mntfromname) - 1, &size);
2854 		if (error)
2855 			return error;
2856 		(void)memset(sfs->f_mntfromname + size, 0,
2857 		    sizeof(sfs->f_mntfromname) - size);
2858 	}
2859 	return 0;
2860 }
2861 
2862 void
2863 vfs_timestamp(struct timespec *ts)
2864 {
2865 
2866 	nanotime(ts);
2867 }
2868 
2869 time_t	rootfstime;			/* recorded root fs time, if known */
2870 void
2871 setrootfstime(time_t t)
2872 {
2873 	rootfstime = t;
2874 }
2875 
2876 static const uint8_t vttodt_tab[9] = {
2877 	DT_UNKNOWN,	/* VNON  */
2878 	DT_REG,		/* VREG  */
2879 	DT_DIR,		/* VDIR  */
2880 	DT_BLK,		/* VBLK  */
2881 	DT_CHR,		/* VCHR  */
2882 	DT_LNK,		/* VLNK  */
2883 	DT_SOCK,	/* VSUCK */
2884 	DT_FIFO,	/* VFIFO */
2885 	DT_UNKNOWN	/* VBAD  */
2886 };
2887 
2888 uint8_t
2889 vtype2dt(enum vtype vt)
2890 {
2891 
2892 	CTASSERT(VBAD == __arraycount(vttodt_tab) - 1);
2893 	return vttodt_tab[vt];
2894 }
2895 
2896 /*
2897  * mount_specific_key_create --
2898  *	Create a key for subsystem mount-specific data.
2899  */
2900 int
2901 mount_specific_key_create(specificdata_key_t *keyp, specificdata_dtor_t dtor)
2902 {
2903 
2904 	return (specificdata_key_create(mount_specificdata_domain, keyp, dtor));
2905 }
2906 
2907 /*
2908  * mount_specific_key_delete --
2909  *	Delete a key for subsystem mount-specific data.
2910  */
2911 void
2912 mount_specific_key_delete(specificdata_key_t key)
2913 {
2914 
2915 	specificdata_key_delete(mount_specificdata_domain, key);
2916 }
2917 
2918 /*
2919  * mount_initspecific --
2920  *	Initialize a mount's specificdata container.
2921  */
2922 void
2923 mount_initspecific(struct mount *mp)
2924 {
2925 	int error;
2926 
2927 	error = specificdata_init(mount_specificdata_domain,
2928 				  &mp->mnt_specdataref);
2929 	KASSERT(error == 0);
2930 }
2931 
2932 /*
2933  * mount_finispecific --
2934  *	Finalize a mount's specificdata container.
2935  */
2936 void
2937 mount_finispecific(struct mount *mp)
2938 {
2939 
2940 	specificdata_fini(mount_specificdata_domain, &mp->mnt_specdataref);
2941 }
2942 
2943 /*
2944  * mount_getspecific --
2945  *	Return mount-specific data corresponding to the specified key.
2946  */
2947 void *
2948 mount_getspecific(struct mount *mp, specificdata_key_t key)
2949 {
2950 
2951 	return (specificdata_getspecific(mount_specificdata_domain,
2952 					 &mp->mnt_specdataref, key));
2953 }
2954 
2955 /*
2956  * mount_setspecific --
2957  *	Set mount-specific data corresponding to the specified key.
2958  */
2959 void
2960 mount_setspecific(struct mount *mp, specificdata_key_t key, void *data)
2961 {
2962 
2963 	specificdata_setspecific(mount_specificdata_domain,
2964 				 &mp->mnt_specdataref, key, data);
2965 }
2966 
2967 int
2968 VFS_MOUNT(struct mount *mp, const char *a, void *b, size_t *c)
2969 {
2970 	int error;
2971 
2972 	KERNEL_LOCK(1, NULL);
2973 	error = (*(mp->mnt_op->vfs_mount))(mp, a, b, c);
2974 	KERNEL_UNLOCK_ONE(NULL);
2975 
2976 	return error;
2977 }
2978 
2979 int
2980 VFS_START(struct mount *mp, int a)
2981 {
2982 	int error;
2983 
2984 	if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) {
2985 		KERNEL_LOCK(1, NULL);
2986 	}
2987 	error = (*(mp->mnt_op->vfs_start))(mp, a);
2988 	if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) {
2989 		KERNEL_UNLOCK_ONE(NULL);
2990 	}
2991 
2992 	return error;
2993 }
2994 
2995 int
2996 VFS_UNMOUNT(struct mount *mp, int a)
2997 {
2998 	int error;
2999 
3000 	KERNEL_LOCK(1, NULL);
3001 	error = (*(mp->mnt_op->vfs_unmount))(mp, a);
3002 	KERNEL_UNLOCK_ONE(NULL);
3003 
3004 	return error;
3005 }
3006 
3007 int
3008 VFS_ROOT(struct mount *mp, struct vnode **a)
3009 {
3010 	int error;
3011 
3012 	if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) {
3013 		KERNEL_LOCK(1, NULL);
3014 	}
3015 	error = (*(mp->mnt_op->vfs_root))(mp, a);
3016 	if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) {
3017 		KERNEL_UNLOCK_ONE(NULL);
3018 	}
3019 
3020 	return error;
3021 }
3022 
3023 int
3024 VFS_QUOTACTL(struct mount *mp, int a, uid_t b, void *c)
3025 {
3026 	int error;
3027 
3028 	if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) {
3029 		KERNEL_LOCK(1, NULL);
3030 	}
3031 	error = (*(mp->mnt_op->vfs_quotactl))(mp, a, b, c);
3032 	if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) {
3033 		KERNEL_UNLOCK_ONE(NULL);
3034 	}
3035 
3036 	return error;
3037 }
3038 
3039 int
3040 VFS_STATVFS(struct mount *mp, struct statvfs *a)
3041 {
3042 	int error;
3043 
3044 	if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) {
3045 		KERNEL_LOCK(1, NULL);
3046 	}
3047 	error = (*(mp->mnt_op->vfs_statvfs))(mp, a);
3048 	if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) {
3049 		KERNEL_UNLOCK_ONE(NULL);
3050 	}
3051 
3052 	return error;
3053 }
3054 
3055 int
3056 VFS_SYNC(struct mount *mp, int a, struct kauth_cred *b)
3057 {
3058 	int error;
3059 
3060 	if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) {
3061 		KERNEL_LOCK(1, NULL);
3062 	}
3063 	error = (*(mp->mnt_op->vfs_sync))(mp, a, b);
3064 	if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) {
3065 		KERNEL_UNLOCK_ONE(NULL);
3066 	}
3067 
3068 	return error;
3069 }
3070 
3071 int
3072 VFS_FHTOVP(struct mount *mp, struct fid *a, struct vnode **b)
3073 {
3074 	int error;
3075 
3076 	if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) {
3077 		KERNEL_LOCK(1, NULL);
3078 	}
3079 	error = (*(mp->mnt_op->vfs_fhtovp))(mp, a, b);
3080 	if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) {
3081 		KERNEL_UNLOCK_ONE(NULL);
3082 	}
3083 
3084 	return error;
3085 }
3086 
3087 int
3088 VFS_VPTOFH(struct vnode *vp, struct fid *a, size_t *b)
3089 {
3090 	int error;
3091 
3092 	if ((vp->v_vflag & VV_MPSAFE) == 0) {
3093 		KERNEL_LOCK(1, NULL);
3094 	}
3095 	error = (*(vp->v_mount->mnt_op->vfs_vptofh))(vp, a, b);
3096 	if ((vp->v_vflag & VV_MPSAFE) == 0) {
3097 		KERNEL_UNLOCK_ONE(NULL);
3098 	}
3099 
3100 	return error;
3101 }
3102 
3103 int
3104 VFS_SNAPSHOT(struct mount *mp, struct vnode *a, struct timespec *b)
3105 {
3106 	int error;
3107 
3108 	if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) {
3109 		KERNEL_LOCK(1, NULL);
3110 	}
3111 	error = (*(mp->mnt_op->vfs_snapshot))(mp, a, b);
3112 	if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) {
3113 		KERNEL_UNLOCK_ONE(NULL);
3114 	}
3115 
3116 	return error;
3117 }
3118 
3119 int
3120 VFS_EXTATTRCTL(struct mount *mp, int a, struct vnode *b, int c, const char *d)
3121 {
3122 	int error;
3123 
3124 	KERNEL_LOCK(1, NULL);		/* XXXSMP check ffs */
3125 	error = (*(mp->mnt_op->vfs_extattrctl))(mp, a, b, c, d);
3126 	KERNEL_UNLOCK_ONE(NULL);	/* XXX */
3127 
3128 	return error;
3129 }
3130 
3131 int
3132 VFS_SUSPENDCTL(struct mount *mp, int a)
3133 {
3134 	int error;
3135 
3136 	if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) {
3137 		KERNEL_LOCK(1, NULL);
3138 	}
3139 	error = (*(mp->mnt_op->vfs_suspendctl))(mp, a);
3140 	if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) {
3141 		KERNEL_UNLOCK_ONE(NULL);
3142 	}
3143 
3144 	return error;
3145 }
3146 
3147 #if defined(DDB) || defined(DEBUGPRINT)
3148 static const char buf_flagbits[] = BUF_FLAGBITS;
3149 
3150 void
3151 vfs_buf_print(struct buf *bp, int full, void (*pr)(const char *, ...))
3152 {
3153 	char bf[1024];
3154 
3155 	(*pr)("  vp %p lblkno 0x%"PRIx64" blkno 0x%"PRIx64" rawblkno 0x%"
3156 	    PRIx64 " dev 0x%x\n",
3157 	    bp->b_vp, bp->b_lblkno, bp->b_blkno, bp->b_rawblkno, bp->b_dev);
3158 
3159 	snprintb(bf, sizeof(bf),
3160 	    buf_flagbits, bp->b_flags | bp->b_oflags | bp->b_cflags);
3161 	(*pr)("  error %d flags 0x%s\n", bp->b_error, bf);
3162 
3163 	(*pr)("  bufsize 0x%lx bcount 0x%lx resid 0x%lx\n",
3164 		  bp->b_bufsize, bp->b_bcount, bp->b_resid);
3165 	(*pr)("  data %p saveaddr %p\n",
3166 		  bp->b_data, bp->b_saveaddr);
3167 	(*pr)("  iodone %p objlock %p\n", bp->b_iodone, bp->b_objlock);
3168 }
3169 
3170 
3171 void
3172 vfs_vnode_print(struct vnode *vp, int full, void (*pr)(const char *, ...))
3173 {
3174 	char bf[256];
3175 
3176 	uvm_object_printit(&vp->v_uobj, full, pr);
3177 	snprintb(bf, sizeof(bf),
3178 	    vnode_flagbits, vp->v_iflag | vp->v_vflag | vp->v_uflag);
3179 	(*pr)("\nVNODE flags %s\n", bf);
3180 	(*pr)("mp %p numoutput %d size 0x%llx writesize 0x%llx\n",
3181 	      vp->v_mount, vp->v_numoutput, vp->v_size, vp->v_writesize);
3182 
3183 	(*pr)("data %p writecount %ld holdcnt %ld\n",
3184 	      vp->v_data, vp->v_writecount, vp->v_holdcnt);
3185 
3186 	(*pr)("tag %s(%d) type %s(%d) mount %p typedata %p\n",
3187 	      ARRAY_PRINT(vp->v_tag, vnode_tags), vp->v_tag,
3188 	      ARRAY_PRINT(vp->v_type, vnode_types), vp->v_type,
3189 	      vp->v_mount, vp->v_mountedhere);
3190 
3191 	(*pr)("v_lock %p\n", &vp->v_lock);
3192 
3193 	if (full) {
3194 		struct buf *bp;
3195 
3196 		(*pr)("clean bufs:\n");
3197 		LIST_FOREACH(bp, &vp->v_cleanblkhd, b_vnbufs) {
3198 			(*pr)(" bp %p\n", bp);
3199 			vfs_buf_print(bp, full, pr);
3200 		}
3201 
3202 		(*pr)("dirty bufs:\n");
3203 		LIST_FOREACH(bp, &vp->v_dirtyblkhd, b_vnbufs) {
3204 			(*pr)(" bp %p\n", bp);
3205 			vfs_buf_print(bp, full, pr);
3206 		}
3207 	}
3208 }
3209 
3210 void
3211 vfs_mount_print(struct mount *mp, int full, void (*pr)(const char *, ...))
3212 {
3213 	char sbuf[256];
3214 
3215 	(*pr)("vnodecovered = %p syncer = %p data = %p\n",
3216 			mp->mnt_vnodecovered,mp->mnt_syncer,mp->mnt_data);
3217 
3218 	(*pr)("fs_bshift %d dev_bshift = %d\n",
3219 			mp->mnt_fs_bshift,mp->mnt_dev_bshift);
3220 
3221 	snprintb(sbuf, sizeof(sbuf), __MNT_FLAG_BITS, mp->mnt_flag);
3222 	(*pr)("flag = %s\n", sbuf);
3223 
3224 	snprintb(sbuf, sizeof(sbuf), __IMNT_FLAG_BITS, mp->mnt_iflag);
3225 	(*pr)("iflag = %s\n", sbuf);
3226 
3227 	(*pr)("refcnt = %d unmounting @ %p updating @ %p\n", mp->mnt_refcnt,
3228 	    &mp->mnt_unmounting, &mp->mnt_updating);
3229 
3230 	(*pr)("statvfs cache:\n");
3231 	(*pr)("\tbsize = %lu\n",mp->mnt_stat.f_bsize);
3232 	(*pr)("\tfrsize = %lu\n",mp->mnt_stat.f_frsize);
3233 	(*pr)("\tiosize = %lu\n",mp->mnt_stat.f_iosize);
3234 
3235 	(*pr)("\tblocks = %"PRIu64"\n",mp->mnt_stat.f_blocks);
3236 	(*pr)("\tbfree = %"PRIu64"\n",mp->mnt_stat.f_bfree);
3237 	(*pr)("\tbavail = %"PRIu64"\n",mp->mnt_stat.f_bavail);
3238 	(*pr)("\tbresvd = %"PRIu64"\n",mp->mnt_stat.f_bresvd);
3239 
3240 	(*pr)("\tfiles = %"PRIu64"\n",mp->mnt_stat.f_files);
3241 	(*pr)("\tffree = %"PRIu64"\n",mp->mnt_stat.f_ffree);
3242 	(*pr)("\tfavail = %"PRIu64"\n",mp->mnt_stat.f_favail);
3243 	(*pr)("\tfresvd = %"PRIu64"\n",mp->mnt_stat.f_fresvd);
3244 
3245 	(*pr)("\tf_fsidx = { 0x%"PRIx32", 0x%"PRIx32" }\n",
3246 			mp->mnt_stat.f_fsidx.__fsid_val[0],
3247 			mp->mnt_stat.f_fsidx.__fsid_val[1]);
3248 
3249 	(*pr)("\towner = %"PRIu32"\n",mp->mnt_stat.f_owner);
3250 	(*pr)("\tnamemax = %lu\n",mp->mnt_stat.f_namemax);
3251 
3252 	snprintb(sbuf, sizeof(sbuf), __MNT_FLAG_BITS, mp->mnt_stat.f_flag);
3253 
3254 	(*pr)("\tflag = %s\n",sbuf);
3255 	(*pr)("\tsyncwrites = %" PRIu64 "\n",mp->mnt_stat.f_syncwrites);
3256 	(*pr)("\tasyncwrites = %" PRIu64 "\n",mp->mnt_stat.f_asyncwrites);
3257 	(*pr)("\tsyncreads = %" PRIu64 "\n",mp->mnt_stat.f_syncreads);
3258 	(*pr)("\tasyncreads = %" PRIu64 "\n",mp->mnt_stat.f_asyncreads);
3259 	(*pr)("\tfstypename = %s\n",mp->mnt_stat.f_fstypename);
3260 	(*pr)("\tmntonname = %s\n",mp->mnt_stat.f_mntonname);
3261 	(*pr)("\tmntfromname = %s\n",mp->mnt_stat.f_mntfromname);
3262 
3263 	{
3264 		int cnt = 0;
3265 		struct vnode *vp;
3266 		(*pr)("locked vnodes =");
3267 		TAILQ_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) {
3268 			if (VOP_ISLOCKED(vp)) {
3269 				if ((++cnt % 6) == 0) {
3270 					(*pr)(" %p,\n\t", vp);
3271 				} else {
3272 					(*pr)(" %p,", vp);
3273 				}
3274 			}
3275 		}
3276 		(*pr)("\n");
3277 	}
3278 
3279 	if (full) {
3280 		int cnt = 0;
3281 		struct vnode *vp;
3282 		(*pr)("all vnodes =");
3283 		TAILQ_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) {
3284 			if (!TAILQ_NEXT(vp, v_mntvnodes)) {
3285 				(*pr)(" %p", vp);
3286 			} else if ((++cnt % 6) == 0) {
3287 				(*pr)(" %p,\n\t", vp);
3288 			} else {
3289 				(*pr)(" %p,", vp);
3290 			}
3291 		}
3292 		(*pr)("\n", vp);
3293 	}
3294 }
3295 #endif /* DDB || DEBUGPRINT */
3296 
3297 /*
3298  * Check if a device pointed to by vp is mounted.
3299  *
3300  * Returns:
3301  *   EINVAL	if it's not a disk
3302  *   EBUSY	if it's a disk and mounted
3303  *   0		if it's a disk and not mounted
3304  */
3305 int
3306 rawdev_mounted(struct vnode *vp, struct vnode **bvpp)
3307 {
3308 	struct vnode *bvp;
3309 	dev_t dev;
3310 	int d_type;
3311 
3312 	bvp = NULL;
3313 	dev = vp->v_rdev;
3314 	d_type = D_OTHER;
3315 
3316 	if (iskmemvp(vp))
3317 		return EINVAL;
3318 
3319 	switch (vp->v_type) {
3320 	case VCHR: {
3321 		const struct cdevsw *cdev;
3322 
3323 		cdev = cdevsw_lookup(dev);
3324 		if (cdev != NULL) {
3325 			dev_t blkdev;
3326 
3327 			blkdev = devsw_chr2blk(dev);
3328 			if (blkdev != NODEV) {
3329 				if (vfinddev(blkdev, VBLK, &bvp) != 0) {
3330 					d_type = (cdev->d_flag & D_TYPEMASK);
3331 					/* XXX: what if bvp disappears? */
3332 					vrele(bvp);
3333 				}
3334 			}
3335 		}
3336 
3337 		break;
3338 		}
3339 
3340 	case VBLK: {
3341 		const struct bdevsw *bdev;
3342 
3343 		bdev = bdevsw_lookup(dev);
3344 		if (bdev != NULL)
3345 			d_type = (bdev->d_flag & D_TYPEMASK);
3346 
3347 		bvp = vp;
3348 
3349 		break;
3350 		}
3351 
3352 	default:
3353 		break;
3354 	}
3355 
3356 	if (d_type != D_DISK)
3357 		return EINVAL;
3358 
3359 	if (bvpp != NULL)
3360 		*bvpp = bvp;
3361 
3362 	/*
3363 	 * XXX: This is bogus. We should be failing the request
3364 	 * XXX: not only if this specific slice is mounted, but
3365 	 * XXX: if it's on a disk with any other mounted slice.
3366 	 */
3367 	if (vfs_mountedon(bvp))
3368 		return EBUSY;
3369 
3370 	return 0;
3371 }
3372