xref: /netbsd-src/sys/kern/vfs_subr.c (revision 9abe0ed18f5d33375f6b823ecc3bcefb4e80f00e)
1 /*	$NetBSD: vfs_subr.c,v 1.257 2005/12/23 15:31:40 yamt Exp $	*/
2 
3 /*-
4  * Copyright (c) 1997, 1998, 2004, 2005 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.
10  * This code is derived from software contributed to The NetBSD Foundation
11  * by Charles M. Hannum.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. All advertising materials mentioning features or use of this software
22  *    must display the following acknowledgement:
23  *	This product includes software developed by the NetBSD
24  *	Foundation, Inc. and its contributors.
25  * 4. Neither the name of The NetBSD Foundation nor the names of its
26  *    contributors may be used to endorse or promote products derived
27  *    from this software without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
30  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
31  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
33  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39  * POSSIBILITY OF SUCH DAMAGE.
40  */
41 
42 /*
43  * Copyright (c) 1989, 1993
44  *	The Regents of the University of California.  All rights reserved.
45  * (c) UNIX System Laboratories, Inc.
46  * All or some portions of this file are derived from material licensed
47  * to the University of California by American Telephone and Telegraph
48  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
49  * the permission of UNIX System Laboratories, Inc.
50  *
51  * Redistribution and use in source and binary forms, with or without
52  * modification, are permitted provided that the following conditions
53  * are met:
54  * 1. Redistributions of source code must retain the above copyright
55  *    notice, this list of conditions and the following disclaimer.
56  * 2. Redistributions in binary form must reproduce the above copyright
57  *    notice, this list of conditions and the following disclaimer in the
58  *    documentation and/or other materials provided with the distribution.
59  * 3. Neither the name of the University nor the names of its contributors
60  *    may be used to endorse or promote products derived from this software
61  *    without specific prior written permission.
62  *
63  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
64  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
65  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
66  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
67  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
68  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
69  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
70  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
71  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
72  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
73  * SUCH DAMAGE.
74  *
75  *	@(#)vfs_subr.c	8.13 (Berkeley) 4/18/94
76  */
77 
78 /*
79  * External virtual filesystem routines
80  */
81 
82 #include <sys/cdefs.h>
83 __KERNEL_RCSID(0, "$NetBSD: vfs_subr.c,v 1.257 2005/12/23 15:31:40 yamt Exp $");
84 
85 #include "opt_inet.h"
86 #include "opt_ddb.h"
87 #include "opt_compat_netbsd.h"
88 #include "opt_compat_43.h"
89 
90 #include <sys/param.h>
91 #include <sys/systm.h>
92 #include <sys/proc.h>
93 #include <sys/kernel.h>
94 #include <sys/mount.h>
95 #include <sys/fcntl.h>
96 #include <sys/vnode.h>
97 #include <sys/stat.h>
98 #include <sys/namei.h>
99 #include <sys/ucred.h>
100 #include <sys/buf.h>
101 #include <sys/errno.h>
102 #include <sys/malloc.h>
103 #include <sys/domain.h>
104 #include <sys/mbuf.h>
105 #include <sys/sa.h>
106 #include <sys/syscallargs.h>
107 #include <sys/device.h>
108 #include <sys/filedesc.h>
109 
110 #include <miscfs/specfs/specdev.h>
111 #include <miscfs/genfs/genfs.h>
112 #include <miscfs/syncfs/syncfs.h>
113 
114 #include <uvm/uvm.h>
115 #include <uvm/uvm_readahead.h>
116 #include <uvm/uvm_ddb.h>
117 
118 #include <sys/sysctl.h>
119 
120 const enum vtype iftovt_tab[16] = {
121 	VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
122 	VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
123 };
124 const int	vttoif_tab[9] = {
125 	0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
126 	S_IFSOCK, S_IFIFO, S_IFMT,
127 };
128 
129 int doforce = 1;		/* 1 => permit forcible unmounting */
130 int prtactive = 0;		/* 1 => print out reclaim of active vnodes */
131 
132 extern int dovfsusermount;	/* 1 => permit any user to mount filesystems */
133 
134 /*
135  * Insq/Remq for the vnode usage lists.
136  */
137 #define	bufinsvn(bp, dp)	LIST_INSERT_HEAD(dp, bp, b_vnbufs)
138 #define	bufremvn(bp) {							\
139 	LIST_REMOVE(bp, b_vnbufs);					\
140 	(bp)->b_vnbufs.le_next = NOLIST;				\
141 }
142 /* TAILQ_HEAD(freelst, vnode) vnode_free_list =	vnode free list (in vnode.h) */
143 struct freelst vnode_free_list = TAILQ_HEAD_INITIALIZER(vnode_free_list);
144 struct freelst vnode_hold_list = TAILQ_HEAD_INITIALIZER(vnode_hold_list);
145 
146 struct mntlist mountlist =			/* mounted filesystem list */
147     CIRCLEQ_HEAD_INITIALIZER(mountlist);
148 struct vfs_list_head vfs_list =			/* vfs list */
149     LIST_HEAD_INITIALIZER(vfs_list);
150 
151 struct simplelock mountlist_slock = SIMPLELOCK_INITIALIZER;
152 static struct simplelock mntid_slock = SIMPLELOCK_INITIALIZER;
153 struct simplelock mntvnode_slock = SIMPLELOCK_INITIALIZER;
154 struct simplelock vnode_free_list_slock = SIMPLELOCK_INITIALIZER;
155 struct simplelock spechash_slock = SIMPLELOCK_INITIALIZER;
156 
157 /* XXX - gross; single global lock to protect v_numoutput */
158 struct simplelock global_v_numoutput_slock = SIMPLELOCK_INITIALIZER;
159 
160 /*
161  * These define the root filesystem and device.
162  */
163 struct mount *rootfs;
164 struct vnode *rootvnode;
165 struct device *root_device;			/* root device */
166 
167 POOL_INIT(vnode_pool, sizeof(struct vnode), 0, 0, 0, "vnodepl",
168     &pool_allocator_nointr);
169 
170 MALLOC_DEFINE(M_VNODE, "vnodes", "Dynamically allocated vnodes");
171 
172 /*
173  * Local declarations.
174  */
175 void insmntque(struct vnode *, struct mount *);
176 int getdevvp(dev_t, struct vnode **, enum vtype);
177 
178 void vclean(struct vnode *, int, struct lwp *);
179 
180 static struct vnode *getcleanvnode(struct lwp *);
181 
182 #ifdef DEBUG
183 void printlockedvnodes(void);
184 #endif
185 
186 /*
187  * Initialize the vnode management data structures.
188  */
189 void
190 vntblinit(void)
191 {
192 
193 	/*
194 	 * Initialize the filesystem syncer.
195 	 */
196 	vn_initialize_syncerd();
197 }
198 
199 int
200 vfs_drainvnodes(long target, struct lwp *l)
201 {
202 
203 	simple_lock(&vnode_free_list_slock);
204 	while (numvnodes > target) {
205 		struct vnode *vp;
206 
207 		vp = getcleanvnode(l);
208 		if (vp == NULL)
209 			return EBUSY; /* give up */
210 		pool_put(&vnode_pool, vp);
211 		simple_lock(&vnode_free_list_slock);
212 		numvnodes--;
213 	}
214 	simple_unlock(&vnode_free_list_slock);
215 
216 	return 0;
217 }
218 
219 /*
220  * grab a vnode from freelist and clean it.
221  */
222 struct vnode *
223 getcleanvnode(struct lwp *l)
224 {
225 	struct vnode *vp;
226 	struct mount *mp;
227 	struct freelst *listhd;
228 
229 	LOCK_ASSERT(simple_lock_held(&vnode_free_list_slock));
230 
231 	listhd = &vnode_free_list;
232 try_nextlist:
233 	TAILQ_FOREACH(vp, listhd, v_freelist) {
234 		if (!simple_lock_try(&vp->v_interlock))
235 			continue;
236 		/*
237 		 * as our lwp might hold the underlying vnode locked,
238 		 * don't try to reclaim the VLAYER vnode if it's locked.
239 		 */
240 		if ((vp->v_flag & VXLOCK) == 0 &&
241 		    ((vp->v_flag & VLAYER) == 0 || VOP_ISLOCKED(vp) == 0)) {
242 			if (vn_start_write(vp, &mp, V_NOWAIT) == 0)
243 				break;
244 		}
245 		mp = NULL;
246 		simple_unlock(&vp->v_interlock);
247 	}
248 
249 	if (vp == NULLVP) {
250 		if (listhd == &vnode_free_list) {
251 			listhd = &vnode_hold_list;
252 			goto try_nextlist;
253 		}
254 		simple_unlock(&vnode_free_list_slock);
255 		return NULLVP;
256 	}
257 
258 	if (vp->v_usecount)
259 		panic("free vnode isn't, vp %p", vp);
260 	TAILQ_REMOVE(listhd, vp, v_freelist);
261 	/* see comment on why 0xdeadb is set at end of vgone (below) */
262 	vp->v_freelist.tqe_prev = (struct vnode **)0xdeadb;
263 	simple_unlock(&vnode_free_list_slock);
264 	vp->v_lease = NULL;
265 
266 	if (vp->v_type != VBAD)
267 		vgonel(vp, l);
268 	else
269 		simple_unlock(&vp->v_interlock);
270 	vn_finished_write(mp, 0);
271 #ifdef DIAGNOSTIC
272 	if (vp->v_data || vp->v_uobj.uo_npages ||
273 	    TAILQ_FIRST(&vp->v_uobj.memq))
274 		panic("cleaned vnode isn't, vp %p", vp);
275 	if (vp->v_numoutput)
276 		panic("clean vnode has pending I/O's, vp %p", vp);
277 #endif
278 	KASSERT((vp->v_flag & VONWORKLST) == 0);
279 
280 	return vp;
281 }
282 
283 /*
284  * Mark a mount point as busy. Used to synchronize access and to delay
285  * unmounting. Interlock is not released on failure.
286  */
287 int
288 vfs_busy(struct mount *mp, int flags, struct simplelock *interlkp)
289 {
290 	int lkflags;
291 
292 	while (mp->mnt_iflag & IMNT_UNMOUNT) {
293 		int gone, n;
294 
295 		if (flags & LK_NOWAIT)
296 			return (ENOENT);
297 		if ((flags & LK_RECURSEFAIL) && mp->mnt_unmounter != NULL
298 		    && mp->mnt_unmounter == curlwp)
299 			return (EDEADLK);
300 		if (interlkp)
301 			simple_unlock(interlkp);
302 		/*
303 		 * Since all busy locks are shared except the exclusive
304 		 * lock granted when unmounting, the only place that a
305 		 * wakeup needs to be done is at the release of the
306 		 * exclusive lock at the end of dounmount.
307 		 */
308 		simple_lock(&mp->mnt_slock);
309 		mp->mnt_wcnt++;
310 		ltsleep((caddr_t)mp, PVFS, "vfs_busy", 0, &mp->mnt_slock);
311 		n = --mp->mnt_wcnt;
312 		simple_unlock(&mp->mnt_slock);
313 		gone = mp->mnt_iflag & IMNT_GONE;
314 
315 		if (n == 0)
316 			wakeup(&mp->mnt_wcnt);
317 		if (interlkp)
318 			simple_lock(interlkp);
319 		if (gone)
320 			return (ENOENT);
321 	}
322 	lkflags = LK_SHARED;
323 	if (interlkp)
324 		lkflags |= LK_INTERLOCK;
325 	if (lockmgr(&mp->mnt_lock, lkflags, interlkp))
326 		panic("vfs_busy: unexpected lock failure");
327 	return (0);
328 }
329 
330 /*
331  * Free a busy filesystem.
332  */
333 void
334 vfs_unbusy(struct mount *mp)
335 {
336 
337 	lockmgr(&mp->mnt_lock, LK_RELEASE, NULL);
338 }
339 
340 /*
341  * Lookup a filesystem type, and if found allocate and initialize
342  * a mount structure for it.
343  *
344  * Devname is usually updated by mount(8) after booting.
345  */
346 int
347 vfs_rootmountalloc(const char *fstypename, const char *devname,
348     struct mount **mpp)
349 {
350 	struct vfsops *vfsp = NULL;
351 	struct mount *mp;
352 
353 	LIST_FOREACH(vfsp, &vfs_list, vfs_list)
354 		if (!strncmp(vfsp->vfs_name, fstypename, MFSNAMELEN))
355 			break;
356 
357 	if (vfsp == NULL)
358 		return (ENODEV);
359 	mp = malloc((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK);
360 	memset((char *)mp, 0, (u_long)sizeof(struct mount));
361 	lockinit(&mp->mnt_lock, PVFS, "vfslock", 0, 0);
362 	simple_lock_init(&mp->mnt_slock);
363 	(void)vfs_busy(mp, LK_NOWAIT, 0);
364 	LIST_INIT(&mp->mnt_vnodelist);
365 	mp->mnt_op = vfsp;
366 	mp->mnt_flag = MNT_RDONLY;
367 	mp->mnt_vnodecovered = NULLVP;
368 	mp->mnt_leaf = mp;
369 	vfsp->vfs_refcount++;
370 	strncpy(mp->mnt_stat.f_fstypename, vfsp->vfs_name, MFSNAMELEN);
371 	mp->mnt_stat.f_mntonname[0] = '/';
372 	(void) copystr(devname, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, 0);
373 	*mpp = mp;
374 	return (0);
375 }
376 
377 /*
378  * Lookup a mount point by filesystem identifier.
379  */
380 struct mount *
381 vfs_getvfs(fsid_t *fsid)
382 {
383 	struct mount *mp;
384 
385 	simple_lock(&mountlist_slock);
386 	CIRCLEQ_FOREACH(mp, &mountlist, mnt_list) {
387 		if (mp->mnt_stat.f_fsidx.__fsid_val[0] == fsid->__fsid_val[0] &&
388 		    mp->mnt_stat.f_fsidx.__fsid_val[1] == fsid->__fsid_val[1]) {
389 			simple_unlock(&mountlist_slock);
390 			return (mp);
391 		}
392 	}
393 	simple_unlock(&mountlist_slock);
394 	return ((struct mount *)0);
395 }
396 
397 /*
398  * Get a new unique fsid
399  */
400 void
401 vfs_getnewfsid(struct mount *mp)
402 {
403 	static u_short xxxfs_mntid;
404 	fsid_t tfsid;
405 	int mtype;
406 
407 	simple_lock(&mntid_slock);
408 	mtype = makefstype(mp->mnt_op->vfs_name);
409 	mp->mnt_stat.f_fsidx.__fsid_val[0] = makedev(mtype, 0);
410 	mp->mnt_stat.f_fsidx.__fsid_val[1] = mtype;
411 	mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
412 	if (xxxfs_mntid == 0)
413 		++xxxfs_mntid;
414 	tfsid.__fsid_val[0] = makedev(mtype & 0xff, xxxfs_mntid);
415 	tfsid.__fsid_val[1] = mtype;
416 	if (!CIRCLEQ_EMPTY(&mountlist)) {
417 		while (vfs_getvfs(&tfsid)) {
418 			tfsid.__fsid_val[0]++;
419 			xxxfs_mntid++;
420 		}
421 	}
422 	mp->mnt_stat.f_fsidx.__fsid_val[0] = tfsid.__fsid_val[0];
423 	mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
424 	simple_unlock(&mntid_slock);
425 }
426 
427 /*
428  * Make a 'unique' number from a mount type name.
429  */
430 long
431 makefstype(const char *type)
432 {
433 	long rv;
434 
435 	for (rv = 0; *type; type++) {
436 		rv <<= 2;
437 		rv ^= *type;
438 	}
439 	return rv;
440 }
441 
442 
443 /*
444  * Set vnode attributes to VNOVAL
445  */
446 void
447 vattr_null(struct vattr *vap)
448 {
449 
450 	vap->va_type = VNON;
451 
452 	/*
453 	 * Assign individually so that it is safe even if size and
454 	 * sign of each member are varied.
455 	 */
456 	vap->va_mode = VNOVAL;
457 	vap->va_nlink = VNOVAL;
458 	vap->va_uid = VNOVAL;
459 	vap->va_gid = VNOVAL;
460 	vap->va_fsid = VNOVAL;
461 	vap->va_fileid = VNOVAL;
462 	vap->va_size = VNOVAL;
463 	vap->va_blocksize = VNOVAL;
464 	vap->va_atime.tv_sec =
465 	    vap->va_mtime.tv_sec =
466 	    vap->va_ctime.tv_sec =
467 	    vap->va_birthtime.tv_sec = VNOVAL;
468 	vap->va_atime.tv_nsec =
469 	    vap->va_mtime.tv_nsec =
470 	    vap->va_ctime.tv_nsec =
471 	    vap->va_birthtime.tv_nsec = VNOVAL;
472 	vap->va_gen = VNOVAL;
473 	vap->va_flags = VNOVAL;
474 	vap->va_rdev = VNOVAL;
475 	vap->va_bytes = VNOVAL;
476 	vap->va_vaflags = 0;
477 }
478 
479 /*
480  * Routines having to do with the management of the vnode table.
481  */
482 extern int (**dead_vnodeop_p)(void *);
483 long numvnodes;
484 
485 /*
486  * Return the next vnode from the free list.
487  */
488 int
489 getnewvnode(enum vtagtype tag, struct mount *mp, int (**vops)(void *),
490     struct vnode **vpp)
491 {
492 	extern struct uvm_pagerops uvm_vnodeops;
493 	struct uvm_object *uobj;
494 	struct lwp *l = curlwp;		/* XXX */
495 	static int toggle;
496 	struct vnode *vp;
497 	int error = 0, tryalloc;
498 
499  try_again:
500 	if (mp) {
501 		/*
502 		 * Mark filesystem busy while we're creating a vnode.
503 		 * If unmount is in progress, this will wait; if the
504 		 * unmount succeeds (only if umount -f), this will
505 		 * return an error.  If the unmount fails, we'll keep
506 		 * going afterwards.
507 		 * (This puts the per-mount vnode list logically under
508 		 * the protection of the vfs_busy lock).
509 		 */
510 		error = vfs_busy(mp, LK_RECURSEFAIL, 0);
511 		if (error && error != EDEADLK)
512 			return error;
513 	}
514 
515 	/*
516 	 * We must choose whether to allocate a new vnode or recycle an
517 	 * existing one. The criterion for allocating a new one is that
518 	 * the total number of vnodes is less than the number desired or
519 	 * there are no vnodes on either free list. Generally we only
520 	 * want to recycle vnodes that have no buffers associated with
521 	 * them, so we look first on the vnode_free_list. If it is empty,
522 	 * we next consider vnodes with referencing buffers on the
523 	 * vnode_hold_list. The toggle ensures that half the time we
524 	 * will use a buffer from the vnode_hold_list, and half the time
525 	 * we will allocate a new one unless the list has grown to twice
526 	 * the desired size. We are reticent to recycle vnodes from the
527 	 * vnode_hold_list because we will lose the identity of all its
528 	 * referencing buffers.
529 	 */
530 
531 	vp = NULL;
532 
533 	simple_lock(&vnode_free_list_slock);
534 
535 	toggle ^= 1;
536 	if (numvnodes > 2 * desiredvnodes)
537 		toggle = 0;
538 
539 	tryalloc = numvnodes < desiredvnodes ||
540 	    (TAILQ_FIRST(&vnode_free_list) == NULL &&
541 	     (TAILQ_FIRST(&vnode_hold_list) == NULL || toggle));
542 
543 	if (tryalloc &&
544 	    (vp = pool_get(&vnode_pool, PR_NOWAIT)) != NULL) {
545 		numvnodes++;
546 		simple_unlock(&vnode_free_list_slock);
547 		memset(vp, 0, sizeof(*vp));
548 		UVM_OBJ_INIT(&vp->v_uobj, &uvm_vnodeops, 1);
549 		/*
550 		 * done by memset() above.
551 		 *	LIST_INIT(&vp->v_nclist);
552 		 *	LIST_INIT(&vp->v_dnclist);
553 		 */
554 	} else {
555 		vp = getcleanvnode(l);
556 		/*
557 		 * Unless this is a bad time of the month, at most
558 		 * the first NCPUS items on the free list are
559 		 * locked, so this is close enough to being empty.
560 		 */
561 		if (vp == NULLVP) {
562 			if (mp && error != EDEADLK)
563 				vfs_unbusy(mp);
564 			if (tryalloc) {
565 				printf("WARNING: unable to allocate new "
566 				    "vnode, retrying...\n");
567 				(void) tsleep(&lbolt, PRIBIO, "newvn", hz);
568 				goto try_again;
569 			}
570 			tablefull("vnode", "increase kern.maxvnodes or NVNODE");
571 			*vpp = 0;
572 			return (ENFILE);
573 		}
574 		vp->v_usecount = 1;
575 		vp->v_flag = 0;
576 		vp->v_socket = NULL;
577 	}
578 	vp->v_type = VNON;
579 	vp->v_vnlock = &vp->v_lock;
580 	lockinit(vp->v_vnlock, PVFS, "vnlock", 0, 0);
581 	KASSERT(LIST_EMPTY(&vp->v_nclist));
582 	KASSERT(LIST_EMPTY(&vp->v_dnclist));
583 	vp->v_tag = tag;
584 	vp->v_op = vops;
585 	insmntque(vp, mp);
586 	*vpp = vp;
587 	vp->v_data = 0;
588 	simple_lock_init(&vp->v_interlock);
589 
590 	/*
591 	 * initialize uvm_object within vnode.
592 	 */
593 
594 	uobj = &vp->v_uobj;
595 	KASSERT(uobj->pgops == &uvm_vnodeops);
596 	KASSERT(uobj->uo_npages == 0);
597 	KASSERT(TAILQ_FIRST(&uobj->memq) == NULL);
598 	vp->v_size = VSIZENOTSET;
599 
600 	if (mp && error != EDEADLK)
601 		vfs_unbusy(mp);
602 	return (0);
603 }
604 
605 /*
606  * This is really just the reverse of getnewvnode(). Needed for
607  * VFS_VGET functions who may need to push back a vnode in case
608  * of a locking race.
609  */
610 void
611 ungetnewvnode(struct vnode *vp)
612 {
613 #ifdef DIAGNOSTIC
614 	if (vp->v_usecount != 1)
615 		panic("ungetnewvnode: busy vnode");
616 #endif
617 	vp->v_usecount--;
618 	insmntque(vp, NULL);
619 	vp->v_type = VBAD;
620 
621 	simple_lock(&vp->v_interlock);
622 	/*
623 	 * Insert at head of LRU list
624 	 */
625 	simple_lock(&vnode_free_list_slock);
626 	if (vp->v_holdcnt > 0)
627 		TAILQ_INSERT_HEAD(&vnode_hold_list, vp, v_freelist);
628 	else
629 		TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
630 	simple_unlock(&vnode_free_list_slock);
631 	simple_unlock(&vp->v_interlock);
632 }
633 
634 /*
635  * Move a vnode from one mount queue to another.
636  */
637 void
638 insmntque(struct vnode *vp, struct mount *mp)
639 {
640 
641 #ifdef DIAGNOSTIC
642 	if ((mp != NULL) &&
643 	    (mp->mnt_iflag & IMNT_UNMOUNT) &&
644 	    !(mp->mnt_flag & MNT_SOFTDEP) &&
645 	    vp->v_tag != VT_VFS) {
646 		panic("insmntque into dying filesystem");
647 	}
648 #endif
649 
650 	simple_lock(&mntvnode_slock);
651 	/*
652 	 * Delete from old mount point vnode list, if on one.
653 	 */
654 	if (vp->v_mount != NULL)
655 		LIST_REMOVE(vp, v_mntvnodes);
656 	/*
657 	 * Insert into list of vnodes for the new mount point, if available.
658 	 */
659 	if ((vp->v_mount = mp) != NULL)
660 		LIST_INSERT_HEAD(&mp->mnt_vnodelist, vp, v_mntvnodes);
661 	simple_unlock(&mntvnode_slock);
662 }
663 
664 /*
665  * Update outstanding I/O count and do wakeup if requested.
666  */
667 void
668 vwakeup(struct buf *bp)
669 {
670 	struct vnode *vp;
671 
672 	if ((vp = bp->b_vp) != NULL) {
673 		/* XXX global lock hack
674 		 * can't use v_interlock here since this is called
675 		 * in interrupt context from biodone().
676 		 */
677 		simple_lock(&global_v_numoutput_slock);
678 		if (--vp->v_numoutput < 0)
679 			panic("vwakeup: neg numoutput, vp %p", vp);
680 		if ((vp->v_flag & VBWAIT) && vp->v_numoutput <= 0) {
681 			vp->v_flag &= ~VBWAIT;
682 			wakeup((caddr_t)&vp->v_numoutput);
683 		}
684 		simple_unlock(&global_v_numoutput_slock);
685 	}
686 }
687 
688 /*
689  * Flush out and invalidate all buffers associated with a vnode.
690  * Called with the underlying vnode locked, which should prevent new dirty
691  * buffers from being queued.
692  */
693 int
694 vinvalbuf(struct vnode *vp, int flags, struct ucred *cred, struct lwp *l,
695     int slpflag, int slptimeo)
696 {
697 	struct buf *bp, *nbp;
698 	int s, error;
699 	int flushflags = PGO_ALLPAGES | PGO_FREE | PGO_SYNCIO |
700 		(flags & V_SAVE ? PGO_CLEANIT : 0);
701 
702 	/* XXXUBC this doesn't look at flags or slp* */
703 	simple_lock(&vp->v_interlock);
704 	error = VOP_PUTPAGES(vp, 0, 0, flushflags);
705 	if (error) {
706 		return error;
707 	}
708 
709 	if (flags & V_SAVE) {
710 		error = VOP_FSYNC(vp, cred, FSYNC_WAIT|FSYNC_RECLAIM, 0, 0, l);
711 		if (error)
712 		        return (error);
713 #ifdef DIAGNOSTIC
714 		s = splbio();
715 		if (vp->v_numoutput > 0 || !LIST_EMPTY(&vp->v_dirtyblkhd))
716 		        panic("vinvalbuf: dirty bufs, vp %p", vp);
717 		splx(s);
718 #endif
719 	}
720 
721 	s = splbio();
722 
723 restart:
724 	for (bp = LIST_FIRST(&vp->v_cleanblkhd); bp; bp = nbp) {
725 		nbp = LIST_NEXT(bp, b_vnbufs);
726 		simple_lock(&bp->b_interlock);
727 		if (bp->b_flags & B_BUSY) {
728 			bp->b_flags |= B_WANTED;
729 			error = ltsleep((caddr_t)bp,
730 				    slpflag | (PRIBIO + 1) | PNORELOCK,
731 				    "vinvalbuf", slptimeo, &bp->b_interlock);
732 			if (error) {
733 				splx(s);
734 				return (error);
735 			}
736 			goto restart;
737 		}
738 		bp->b_flags |= B_BUSY | B_INVAL | B_VFLUSH;
739 		simple_unlock(&bp->b_interlock);
740 		brelse(bp);
741 	}
742 
743 	for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
744 		nbp = LIST_NEXT(bp, b_vnbufs);
745 		simple_lock(&bp->b_interlock);
746 		if (bp->b_flags & B_BUSY) {
747 			bp->b_flags |= B_WANTED;
748 			error = ltsleep((caddr_t)bp,
749 				    slpflag | (PRIBIO + 1) | PNORELOCK,
750 				    "vinvalbuf", slptimeo, &bp->b_interlock);
751 			if (error) {
752 				splx(s);
753 				return (error);
754 			}
755 			goto restart;
756 		}
757 		/*
758 		 * XXX Since there are no node locks for NFS, I believe
759 		 * there is a slight chance that a delayed write will
760 		 * occur while sleeping just above, so check for it.
761 		 */
762 		if ((bp->b_flags & B_DELWRI) && (flags & V_SAVE)) {
763 #ifdef DEBUG
764 			printf("buffer still DELWRI\n");
765 #endif
766 			bp->b_flags |= B_BUSY | B_VFLUSH;
767 			simple_unlock(&bp->b_interlock);
768 			VOP_BWRITE(bp);
769 			goto restart;
770 		}
771 		bp->b_flags |= B_BUSY | B_INVAL | B_VFLUSH;
772 		simple_unlock(&bp->b_interlock);
773 		brelse(bp);
774 	}
775 
776 #ifdef DIAGNOSTIC
777 	if (!LIST_EMPTY(&vp->v_cleanblkhd) || !LIST_EMPTY(&vp->v_dirtyblkhd))
778 		panic("vinvalbuf: flush failed, vp %p", vp);
779 #endif
780 
781 	splx(s);
782 
783 	return (0);
784 }
785 
786 /*
787  * Destroy any in core blocks past the truncation length.
788  * Called with the underlying vnode locked, which should prevent new dirty
789  * buffers from being queued.
790  */
791 int
792 vtruncbuf(struct vnode *vp, daddr_t lbn, int slpflag, int slptimeo)
793 {
794 	struct buf *bp, *nbp;
795 	int s, error;
796 	voff_t off;
797 
798 	off = round_page((voff_t)lbn << vp->v_mount->mnt_fs_bshift);
799 	simple_lock(&vp->v_interlock);
800 	error = VOP_PUTPAGES(vp, off, 0, PGO_FREE | PGO_SYNCIO);
801 	if (error) {
802 		return error;
803 	}
804 
805 	s = splbio();
806 
807 restart:
808 	for (bp = LIST_FIRST(&vp->v_cleanblkhd); bp; bp = nbp) {
809 		nbp = LIST_NEXT(bp, b_vnbufs);
810 		if (bp->b_lblkno < lbn)
811 			continue;
812 		simple_lock(&bp->b_interlock);
813 		if (bp->b_flags & B_BUSY) {
814 			bp->b_flags |= B_WANTED;
815 			error = ltsleep(bp, slpflag | (PRIBIO + 1) | PNORELOCK,
816 			    "vtruncbuf", slptimeo, &bp->b_interlock);
817 			if (error) {
818 				splx(s);
819 				return (error);
820 			}
821 			goto restart;
822 		}
823 		bp->b_flags |= B_BUSY | B_INVAL | B_VFLUSH;
824 		simple_unlock(&bp->b_interlock);
825 		brelse(bp);
826 	}
827 
828 	for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
829 		nbp = LIST_NEXT(bp, b_vnbufs);
830 		if (bp->b_lblkno < lbn)
831 			continue;
832 		simple_lock(&bp->b_interlock);
833 		if (bp->b_flags & B_BUSY) {
834 			bp->b_flags |= B_WANTED;
835 			error = ltsleep(bp, slpflag | (PRIBIO + 1) | PNORELOCK,
836 			    "vtruncbuf", slptimeo, &bp->b_interlock);
837 			if (error) {
838 				splx(s);
839 				return (error);
840 			}
841 			goto restart;
842 		}
843 		bp->b_flags |= B_BUSY | B_INVAL | B_VFLUSH;
844 		simple_unlock(&bp->b_interlock);
845 		brelse(bp);
846 	}
847 
848 	splx(s);
849 
850 	return (0);
851 }
852 
853 void
854 vflushbuf(struct vnode *vp, int sync)
855 {
856 	struct buf *bp, *nbp;
857 	int flags = PGO_CLEANIT | PGO_ALLPAGES | (sync ? PGO_SYNCIO : 0);
858 	int s;
859 
860 	simple_lock(&vp->v_interlock);
861 	(void) VOP_PUTPAGES(vp, 0, 0, flags);
862 
863 loop:
864 	s = splbio();
865 	for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
866 		nbp = LIST_NEXT(bp, b_vnbufs);
867 		simple_lock(&bp->b_interlock);
868 		if ((bp->b_flags & B_BUSY)) {
869 			simple_unlock(&bp->b_interlock);
870 			continue;
871 		}
872 		if ((bp->b_flags & B_DELWRI) == 0)
873 			panic("vflushbuf: not dirty, bp %p", bp);
874 		bp->b_flags |= B_BUSY | B_VFLUSH;
875 		simple_unlock(&bp->b_interlock);
876 		splx(s);
877 		/*
878 		 * Wait for I/O associated with indirect blocks to complete,
879 		 * since there is no way to quickly wait for them below.
880 		 */
881 		if (bp->b_vp == vp || sync == 0)
882 			(void) bawrite(bp);
883 		else
884 			(void) bwrite(bp);
885 		goto loop;
886 	}
887 	if (sync == 0) {
888 		splx(s);
889 		return;
890 	}
891 	simple_lock(&global_v_numoutput_slock);
892 	while (vp->v_numoutput) {
893 		vp->v_flag |= VBWAIT;
894 		ltsleep((caddr_t)&vp->v_numoutput, PRIBIO + 1, "vflushbuf", 0,
895 			&global_v_numoutput_slock);
896 	}
897 	simple_unlock(&global_v_numoutput_slock);
898 	splx(s);
899 	if (!LIST_EMPTY(&vp->v_dirtyblkhd)) {
900 		vprint("vflushbuf: dirty", vp);
901 		goto loop;
902 	}
903 }
904 
905 /*
906  * Associate a buffer with a vnode.
907  */
908 void
909 bgetvp(struct vnode *vp, struct buf *bp)
910 {
911 	int s;
912 
913 	if (bp->b_vp)
914 		panic("bgetvp: not free, bp %p", bp);
915 	VHOLD(vp);
916 	s = splbio();
917 	bp->b_vp = vp;
918 	if (vp->v_type == VBLK || vp->v_type == VCHR)
919 		bp->b_dev = vp->v_rdev;
920 	else
921 		bp->b_dev = NODEV;
922 	/*
923 	 * Insert onto list for new vnode.
924 	 */
925 	bufinsvn(bp, &vp->v_cleanblkhd);
926 	splx(s);
927 }
928 
929 /*
930  * Disassociate a buffer from a vnode.
931  */
932 void
933 brelvp(struct buf *bp)
934 {
935 	struct vnode *vp;
936 	int s;
937 
938 	if (bp->b_vp == NULL)
939 		panic("brelvp: vp NULL, bp %p", bp);
940 
941 	s = splbio();
942 	vp = bp->b_vp;
943 	/*
944 	 * Delete from old vnode list, if on one.
945 	 */
946 	if (LIST_NEXT(bp, b_vnbufs) != NOLIST)
947 		bufremvn(bp);
948 
949 	if (TAILQ_EMPTY(&vp->v_uobj.memq) && (vp->v_flag & VONWORKLST) &&
950 	    LIST_FIRST(&vp->v_dirtyblkhd) == NULL) {
951 		vp->v_flag &= ~(VWRITEMAPDIRTY|VONWORKLST);
952 		LIST_REMOVE(vp, v_synclist);
953 	}
954 
955 	bp->b_vp = NULL;
956 	HOLDRELE(vp);
957 	splx(s);
958 }
959 
960 /*
961  * Reassign a buffer from one vnode to another.
962  * Used to assign file specific control information
963  * (indirect blocks) to the vnode to which they belong.
964  *
965  * This function must be called at splbio().
966  */
967 void
968 reassignbuf(struct buf *bp, struct vnode *newvp)
969 {
970 	struct buflists *listheadp;
971 	int delayx;
972 
973 	/*
974 	 * Delete from old vnode list, if on one.
975 	 */
976 	if (LIST_NEXT(bp, b_vnbufs) != NOLIST)
977 		bufremvn(bp);
978 	/*
979 	 * If dirty, put on list of dirty buffers;
980 	 * otherwise insert onto list of clean buffers.
981 	 */
982 	if ((bp->b_flags & B_DELWRI) == 0) {
983 		listheadp = &newvp->v_cleanblkhd;
984 		if (TAILQ_EMPTY(&newvp->v_uobj.memq) &&
985 		    (newvp->v_flag & VONWORKLST) &&
986 		    LIST_FIRST(&newvp->v_dirtyblkhd) == NULL) {
987 			newvp->v_flag &= ~(VWRITEMAPDIRTY|VONWORKLST);
988 			LIST_REMOVE(newvp, v_synclist);
989 		}
990 	} else {
991 		listheadp = &newvp->v_dirtyblkhd;
992 		if ((newvp->v_flag & VONWORKLST) == 0) {
993 			switch (newvp->v_type) {
994 			case VDIR:
995 				delayx = dirdelay;
996 				break;
997 			case VBLK:
998 				if (newvp->v_specmountpoint != NULL) {
999 					delayx = metadelay;
1000 					break;
1001 				}
1002 				/* fall through */
1003 			default:
1004 				delayx = filedelay;
1005 				break;
1006 			}
1007 			if (!newvp->v_mount ||
1008 			    (newvp->v_mount->mnt_flag & MNT_ASYNC) == 0)
1009 				vn_syncer_add_to_worklist(newvp, delayx);
1010 		}
1011 	}
1012 	bufinsvn(bp, listheadp);
1013 }
1014 
1015 /*
1016  * Create a vnode for a block device.
1017  * Used for root filesystem and swap areas.
1018  * Also used for memory file system special devices.
1019  */
1020 int
1021 bdevvp(dev_t dev, struct vnode **vpp)
1022 {
1023 
1024 	return (getdevvp(dev, vpp, VBLK));
1025 }
1026 
1027 /*
1028  * Create a vnode for a character device.
1029  * Used for kernfs and some console handling.
1030  */
1031 int
1032 cdevvp(dev_t dev, struct vnode **vpp)
1033 {
1034 
1035 	return (getdevvp(dev, vpp, VCHR));
1036 }
1037 
1038 /*
1039  * Create a vnode for a device.
1040  * Used by bdevvp (block device) for root file system etc.,
1041  * and by cdevvp (character device) for console and kernfs.
1042  */
1043 int
1044 getdevvp(dev_t dev, struct vnode **vpp, enum vtype type)
1045 {
1046 	struct vnode *vp;
1047 	struct vnode *nvp;
1048 	int error;
1049 
1050 	if (dev == NODEV) {
1051 		*vpp = NULLVP;
1052 		return (0);
1053 	}
1054 	error = getnewvnode(VT_NON, NULL, spec_vnodeop_p, &nvp);
1055 	if (error) {
1056 		*vpp = NULLVP;
1057 		return (error);
1058 	}
1059 	vp = nvp;
1060 	vp->v_type = type;
1061 	if ((nvp = checkalias(vp, dev, NULL)) != 0) {
1062 		vput(vp);
1063 		vp = nvp;
1064 	}
1065 	*vpp = vp;
1066 	return (0);
1067 }
1068 
1069 /*
1070  * Check to see if the new vnode represents a special device
1071  * for which we already have a vnode (either because of
1072  * bdevvp() or because of a different vnode representing
1073  * the same block device). If such an alias exists, deallocate
1074  * the existing contents and return the aliased vnode. The
1075  * caller is responsible for filling it with its new contents.
1076  */
1077 struct vnode *
1078 checkalias(struct vnode *nvp, dev_t nvp_rdev, struct mount *mp)
1079 {
1080 	struct lwp *l = curlwp;		/* XXX */
1081 	struct vnode *vp;
1082 	struct vnode **vpp;
1083 
1084 	if (nvp->v_type != VBLK && nvp->v_type != VCHR)
1085 		return (NULLVP);
1086 
1087 	vpp = &speclisth[SPECHASH(nvp_rdev)];
1088 loop:
1089 	simple_lock(&spechash_slock);
1090 	for (vp = *vpp; vp; vp = vp->v_specnext) {
1091 		if (nvp_rdev != vp->v_rdev || nvp->v_type != vp->v_type)
1092 			continue;
1093 		/*
1094 		 * Alias, but not in use, so flush it out.
1095 		 */
1096 		simple_lock(&vp->v_interlock);
1097 		simple_unlock(&spechash_slock);
1098 		if (vp->v_usecount == 0) {
1099 			vgonel(vp, l);
1100 			goto loop;
1101 		}
1102 		/*
1103 		 * What we're interested to know here is if someone else has
1104 		 * removed this vnode from the device hash list while we were
1105 		 * waiting.  This can only happen if vclean() did it, and
1106 		 * this requires the vnode to be locked.  Therefore, we use
1107 		 * LK_SLEEPFAIL and retry.
1108 		 */
1109 		if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK | LK_SLEEPFAIL))
1110 			goto loop;
1111 		simple_lock(&spechash_slock);
1112 		break;
1113 	}
1114 	if (vp == NULL || vp->v_tag != VT_NON || vp->v_type != VBLK) {
1115 		MALLOC(nvp->v_specinfo, struct specinfo *,
1116 			sizeof(struct specinfo), M_VNODE, M_NOWAIT);
1117 		/* XXX Erg. */
1118 		if (nvp->v_specinfo == NULL) {
1119 			simple_unlock(&spechash_slock);
1120 			uvm_wait("checkalias");
1121 			goto loop;
1122 		}
1123 
1124 		nvp->v_rdev = nvp_rdev;
1125 		nvp->v_hashchain = vpp;
1126 		nvp->v_specnext = *vpp;
1127 		nvp->v_specmountpoint = NULL;
1128 		simple_unlock(&spechash_slock);
1129 		nvp->v_speclockf = NULL;
1130 		simple_lock_init(&nvp->v_spec_cow_slock);
1131 		SLIST_INIT(&nvp->v_spec_cow_head);
1132 		nvp->v_spec_cow_req = 0;
1133 		nvp->v_spec_cow_count = 0;
1134 
1135 		*vpp = nvp;
1136 		if (vp != NULLVP) {
1137 			nvp->v_flag |= VALIASED;
1138 			vp->v_flag |= VALIASED;
1139 			vput(vp);
1140 		}
1141 		return (NULLVP);
1142 	}
1143 	simple_unlock(&spechash_slock);
1144 	VOP_UNLOCK(vp, 0);
1145 	simple_lock(&vp->v_interlock);
1146 	vclean(vp, 0, l);
1147 	vp->v_op = nvp->v_op;
1148 	vp->v_tag = nvp->v_tag;
1149 	vp->v_vnlock = &vp->v_lock;
1150 	lockinit(vp->v_vnlock, PVFS, "vnlock", 0, 0);
1151 	nvp->v_type = VNON;
1152 	insmntque(vp, mp);
1153 	return (vp);
1154 }
1155 
1156 /*
1157  * Grab a particular vnode from the free list, increment its
1158  * reference count and lock it. If the vnode lock bit is set the
1159  * vnode is being eliminated in vgone. In that case, we can not
1160  * grab the vnode, so the process is awakened when the transition is
1161  * completed, and an error returned to indicate that the vnode is no
1162  * longer usable (possibly having been changed to a new file system type).
1163  */
1164 int
1165 vget(struct vnode *vp, int flags)
1166 {
1167 	int error;
1168 
1169 	/*
1170 	 * If the vnode is in the process of being cleaned out for
1171 	 * another use, we wait for the cleaning to finish and then
1172 	 * return failure. Cleaning is determined by checking that
1173 	 * the VXLOCK flag is set.
1174 	 */
1175 
1176 	if ((flags & LK_INTERLOCK) == 0)
1177 		simple_lock(&vp->v_interlock);
1178 	if ((vp->v_flag & (VXLOCK | VFREEING)) != 0) {
1179 		if (flags & LK_NOWAIT) {
1180 			simple_unlock(&vp->v_interlock);
1181 			return EBUSY;
1182 		}
1183 		vp->v_flag |= VXWANT;
1184 		ltsleep(vp, PINOD|PNORELOCK, "vget", 0, &vp->v_interlock);
1185 		return (ENOENT);
1186 	}
1187 	if (vp->v_usecount == 0) {
1188 		simple_lock(&vnode_free_list_slock);
1189 		if (vp->v_holdcnt > 0)
1190 			TAILQ_REMOVE(&vnode_hold_list, vp, v_freelist);
1191 		else
1192 			TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
1193 		simple_unlock(&vnode_free_list_slock);
1194 	}
1195 	vp->v_usecount++;
1196 #ifdef DIAGNOSTIC
1197 	if (vp->v_usecount == 0) {
1198 		vprint("vget", vp);
1199 		panic("vget: usecount overflow, vp %p", vp);
1200 	}
1201 #endif
1202 	if (flags & LK_TYPE_MASK) {
1203 		if ((error = vn_lock(vp, flags | LK_INTERLOCK))) {
1204 			vrele(vp);
1205 		}
1206 		return (error);
1207 	}
1208 	simple_unlock(&vp->v_interlock);
1209 	return (0);
1210 }
1211 
1212 /*
1213  * vput(), just unlock and vrele()
1214  */
1215 void
1216 vput(struct vnode *vp)
1217 {
1218 	struct lwp *l = curlwp;		/* XXX */
1219 
1220 #ifdef DIAGNOSTIC
1221 	if (vp == NULL)
1222 		panic("vput: null vp");
1223 #endif
1224 	simple_lock(&vp->v_interlock);
1225 	vp->v_usecount--;
1226 	if (vp->v_usecount > 0) {
1227 		simple_unlock(&vp->v_interlock);
1228 		VOP_UNLOCK(vp, 0);
1229 		return;
1230 	}
1231 #ifdef DIAGNOSTIC
1232 	if (vp->v_usecount < 0 || vp->v_writecount != 0) {
1233 		vprint("vput: bad ref count", vp);
1234 		panic("vput: ref cnt");
1235 	}
1236 #endif
1237 	/*
1238 	 * Insert at tail of LRU list.
1239 	 */
1240 	simple_lock(&vnode_free_list_slock);
1241 	if (vp->v_holdcnt > 0)
1242 		TAILQ_INSERT_TAIL(&vnode_hold_list, vp, v_freelist);
1243 	else
1244 		TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
1245 	simple_unlock(&vnode_free_list_slock);
1246 	if (vp->v_flag & VEXECMAP) {
1247 		uvmexp.execpages -= vp->v_uobj.uo_npages;
1248 		uvmexp.filepages += vp->v_uobj.uo_npages;
1249 	}
1250 	vp->v_flag &= ~(VTEXT|VEXECMAP|VWRITEMAP);
1251 	simple_unlock(&vp->v_interlock);
1252 	VOP_INACTIVE(vp, l);
1253 }
1254 
1255 /*
1256  * Vnode release.
1257  * If count drops to zero, call inactive routine and return to freelist.
1258  */
1259 void
1260 vrele(struct vnode *vp)
1261 {
1262 	struct lwp *l = curlwp;		/* XXX */
1263 
1264 #ifdef DIAGNOSTIC
1265 	if (vp == NULL)
1266 		panic("vrele: null vp");
1267 #endif
1268 	simple_lock(&vp->v_interlock);
1269 	vp->v_usecount--;
1270 	if (vp->v_usecount > 0) {
1271 		simple_unlock(&vp->v_interlock);
1272 		return;
1273 	}
1274 #ifdef DIAGNOSTIC
1275 	if (vp->v_usecount < 0 || vp->v_writecount != 0) {
1276 		vprint("vrele: bad ref count", vp);
1277 		panic("vrele: ref cnt vp %p", vp);
1278 	}
1279 #endif
1280 	/*
1281 	 * Insert at tail of LRU list.
1282 	 */
1283 	simple_lock(&vnode_free_list_slock);
1284 	if (vp->v_holdcnt > 0)
1285 		TAILQ_INSERT_TAIL(&vnode_hold_list, vp, v_freelist);
1286 	else
1287 		TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
1288 	simple_unlock(&vnode_free_list_slock);
1289 	if (vp->v_flag & VEXECMAP) {
1290 		uvmexp.execpages -= vp->v_uobj.uo_npages;
1291 		uvmexp.filepages += vp->v_uobj.uo_npages;
1292 	}
1293 	vp->v_flag &= ~(VTEXT|VEXECMAP|VWRITEMAP);
1294 	if (vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK) == 0)
1295 		VOP_INACTIVE(vp, l);
1296 }
1297 
1298 #ifdef DIAGNOSTIC
1299 /*
1300  * Page or buffer structure gets a reference.
1301  */
1302 void
1303 vholdl(struct vnode *vp)
1304 {
1305 
1306 	/*
1307 	 * If it is on the freelist and the hold count is currently
1308 	 * zero, move it to the hold list. The test of the back
1309 	 * pointer and the use reference count of zero is because
1310 	 * it will be removed from a free list by getnewvnode,
1311 	 * but will not have its reference count incremented until
1312 	 * after calling vgone. If the reference count were
1313 	 * incremented first, vgone would (incorrectly) try to
1314 	 * close the previous instance of the underlying object.
1315 	 * So, the back pointer is explicitly set to `0xdeadb' in
1316 	 * getnewvnode after removing it from a freelist to ensure
1317 	 * that we do not try to move it here.
1318 	 */
1319 	if ((vp->v_freelist.tqe_prev != (struct vnode **)0xdeadb) &&
1320 	    vp->v_holdcnt == 0 && vp->v_usecount == 0) {
1321 		simple_lock(&vnode_free_list_slock);
1322 		TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
1323 		TAILQ_INSERT_TAIL(&vnode_hold_list, vp, v_freelist);
1324 		simple_unlock(&vnode_free_list_slock);
1325 	}
1326 	vp->v_holdcnt++;
1327 }
1328 
1329 /*
1330  * Page or buffer structure frees a reference.
1331  */
1332 void
1333 holdrelel(struct vnode *vp)
1334 {
1335 
1336 	if (vp->v_holdcnt <= 0)
1337 		panic("holdrelel: holdcnt vp %p", vp);
1338 	vp->v_holdcnt--;
1339 
1340 	/*
1341 	 * If it is on the holdlist and the hold count drops to
1342 	 * zero, move it to the free list. The test of the back
1343 	 * pointer and the use reference count of zero is because
1344 	 * it will be removed from a free list by getnewvnode,
1345 	 * but will not have its reference count incremented until
1346 	 * after calling vgone. If the reference count were
1347 	 * incremented first, vgone would (incorrectly) try to
1348 	 * close the previous instance of the underlying object.
1349 	 * So, the back pointer is explicitly set to `0xdeadb' in
1350 	 * getnewvnode after removing it from a freelist to ensure
1351 	 * that we do not try to move it here.
1352 	 */
1353 
1354 	if ((vp->v_freelist.tqe_prev != (struct vnode **)0xdeadb) &&
1355 	    vp->v_holdcnt == 0 && vp->v_usecount == 0) {
1356 		simple_lock(&vnode_free_list_slock);
1357 		TAILQ_REMOVE(&vnode_hold_list, vp, v_freelist);
1358 		TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
1359 		simple_unlock(&vnode_free_list_slock);
1360 	}
1361 }
1362 
1363 /*
1364  * Vnode reference.
1365  */
1366 void
1367 vref(struct vnode *vp)
1368 {
1369 
1370 	simple_lock(&vp->v_interlock);
1371 	if (vp->v_usecount <= 0)
1372 		panic("vref used where vget required, vp %p", vp);
1373 	vp->v_usecount++;
1374 #ifdef DIAGNOSTIC
1375 	if (vp->v_usecount == 0) {
1376 		vprint("vref", vp);
1377 		panic("vref: usecount overflow, vp %p", vp);
1378 	}
1379 #endif
1380 	simple_unlock(&vp->v_interlock);
1381 }
1382 #endif /* DIAGNOSTIC */
1383 
1384 /*
1385  * Remove any vnodes in the vnode table belonging to mount point mp.
1386  *
1387  * If FORCECLOSE is not specified, there should not be any active ones,
1388  * return error if any are found (nb: this is a user error, not a
1389  * system error). If FORCECLOSE is specified, detach any active vnodes
1390  * that are found.
1391  *
1392  * If WRITECLOSE is set, only flush out regular file vnodes open for
1393  * writing.
1394  *
1395  * SKIPSYSTEM causes any vnodes marked V_SYSTEM to be skipped.
1396  */
1397 #ifdef DEBUG
1398 int busyprt = 0;	/* print out busy vnodes */
1399 struct ctldebug debug1 = { "busyprt", &busyprt };
1400 #endif
1401 
1402 int
1403 vflush(struct mount *mp, struct vnode *skipvp, int flags)
1404 {
1405 	struct lwp *l = curlwp;		/* XXX */
1406 	struct vnode *vp, *nvp;
1407 	int busy = 0;
1408 
1409 	simple_lock(&mntvnode_slock);
1410 loop:
1411 	for (vp = LIST_FIRST(&mp->mnt_vnodelist); vp; vp = nvp) {
1412 		if (vp->v_mount != mp)
1413 			goto loop;
1414 		nvp = LIST_NEXT(vp, v_mntvnodes);
1415 		/*
1416 		 * Skip over a selected vnode.
1417 		 */
1418 		if (vp == skipvp)
1419 			continue;
1420 		simple_lock(&vp->v_interlock);
1421 		/*
1422 		 * Skip over a vnodes marked VSYSTEM.
1423 		 */
1424 		if ((flags & SKIPSYSTEM) && (vp->v_flag & VSYSTEM)) {
1425 			simple_unlock(&vp->v_interlock);
1426 			continue;
1427 		}
1428 		/*
1429 		 * If WRITECLOSE is set, only flush out regular file
1430 		 * vnodes open for writing.
1431 		 */
1432 		if ((flags & WRITECLOSE) &&
1433 		    (vp->v_writecount == 0 || vp->v_type != VREG)) {
1434 			simple_unlock(&vp->v_interlock);
1435 			continue;
1436 		}
1437 		/*
1438 		 * With v_usecount == 0, all we need to do is clear
1439 		 * out the vnode data structures and we are done.
1440 		 */
1441 		if (vp->v_usecount == 0) {
1442 			simple_unlock(&mntvnode_slock);
1443 			vgonel(vp, l);
1444 			simple_lock(&mntvnode_slock);
1445 			continue;
1446 		}
1447 		/*
1448 		 * If FORCECLOSE is set, forcibly close the vnode.
1449 		 * For block or character devices, revert to an
1450 		 * anonymous device. For all other files, just kill them.
1451 		 */
1452 		if (flags & FORCECLOSE) {
1453 			simple_unlock(&mntvnode_slock);
1454 			if (vp->v_type != VBLK && vp->v_type != VCHR) {
1455 				vgonel(vp, l);
1456 			} else {
1457 				vclean(vp, 0, l);
1458 				vp->v_op = spec_vnodeop_p;
1459 				insmntque(vp, (struct mount *)0);
1460 			}
1461 			simple_lock(&mntvnode_slock);
1462 			continue;
1463 		}
1464 #ifdef DEBUG
1465 		if (busyprt)
1466 			vprint("vflush: busy vnode", vp);
1467 #endif
1468 		simple_unlock(&vp->v_interlock);
1469 		busy++;
1470 	}
1471 	simple_unlock(&mntvnode_slock);
1472 	if (busy)
1473 		return (EBUSY);
1474 	return (0);
1475 }
1476 
1477 /*
1478  * Disassociate the underlying file system from a vnode.
1479  */
1480 void
1481 vclean(struct vnode *vp, int flags, struct lwp *l)
1482 {
1483 	struct mount *mp;
1484 	int active;
1485 
1486 	LOCK_ASSERT(simple_lock_held(&vp->v_interlock));
1487 
1488 	/*
1489 	 * Check to see if the vnode is in use.
1490 	 * If so we have to reference it before we clean it out
1491 	 * so that its count cannot fall to zero and generate a
1492 	 * race against ourselves to recycle it.
1493 	 */
1494 
1495 	if ((active = vp->v_usecount) != 0) {
1496 		vp->v_usecount++;
1497 #ifdef DIAGNOSTIC
1498 		if (vp->v_usecount == 0) {
1499 			vprint("vclean", vp);
1500 			panic("vclean: usecount overflow");
1501 		}
1502 #endif
1503 	}
1504 
1505 	/*
1506 	 * Prevent the vnode from being recycled or
1507 	 * brought into use while we clean it out.
1508 	 */
1509 	if (vp->v_flag & VXLOCK)
1510 		panic("vclean: deadlock, vp %p", vp);
1511 	vp->v_flag |= VXLOCK;
1512 	if (vp->v_flag & VEXECMAP) {
1513 		uvmexp.execpages -= vp->v_uobj.uo_npages;
1514 		uvmexp.filepages += vp->v_uobj.uo_npages;
1515 	}
1516 	vp->v_flag &= ~(VTEXT|VEXECMAP);
1517 
1518 	/*
1519 	 * Even if the count is zero, the VOP_INACTIVE routine may still
1520 	 * have the object locked while it cleans it out. The VOP_LOCK
1521 	 * ensures that the VOP_INACTIVE routine is done with its work.
1522 	 * For active vnodes, it ensures that no other activity can
1523 	 * occur while the underlying object is being cleaned out.
1524 	 */
1525 	VOP_LOCK(vp, LK_DRAIN | LK_INTERLOCK);
1526 
1527 	/*
1528 	 * Clean out any cached data associated with the vnode.
1529 	 * If special device, remove it from special device alias list.
1530 	 * if it is on one.
1531 	 */
1532 	if (flags & DOCLOSE) {
1533 		int error;
1534 		struct vnode *vq, *vx;
1535 
1536 		vn_start_write(vp, &mp, V_WAIT | V_LOWER);
1537 		error = vinvalbuf(vp, V_SAVE, NOCRED, l, 0, 0);
1538 		vn_finished_write(mp, V_LOWER);
1539 		if (error)
1540 			error = vinvalbuf(vp, 0, NOCRED, l, 0, 0);
1541 		KASSERT(error == 0);
1542 		KASSERT((vp->v_flag & VONWORKLST) == 0);
1543 
1544 		if (active)
1545 			VOP_CLOSE(vp, FNONBLOCK, NOCRED, NULL);
1546 
1547 		if ((vp->v_type == VBLK || vp->v_type == VCHR) &&
1548 		    vp->v_specinfo != 0) {
1549 			simple_lock(&spechash_slock);
1550 			if (vp->v_hashchain != NULL) {
1551 				if (*vp->v_hashchain == vp) {
1552 					*vp->v_hashchain = vp->v_specnext;
1553 				} else {
1554 					for (vq = *vp->v_hashchain; vq;
1555 					     vq = vq->v_specnext) {
1556 						if (vq->v_specnext != vp)
1557 							continue;
1558 						vq->v_specnext = vp->v_specnext;
1559 						break;
1560 					}
1561 					if (vq == NULL)
1562 						panic("missing bdev");
1563 				}
1564 				if (vp->v_flag & VALIASED) {
1565 					vx = NULL;
1566 						for (vq = *vp->v_hashchain; vq;
1567 						     vq = vq->v_specnext) {
1568 						if (vq->v_rdev != vp->v_rdev ||
1569 						    vq->v_type != vp->v_type)
1570 							continue;
1571 						if (vx)
1572 							break;
1573 						vx = vq;
1574 					}
1575 					if (vx == NULL)
1576 						panic("missing alias");
1577 					if (vq == NULL)
1578 						vx->v_flag &= ~VALIASED;
1579 					vp->v_flag &= ~VALIASED;
1580 				}
1581 			}
1582 			simple_unlock(&spechash_slock);
1583 			FREE(vp->v_specinfo, M_VNODE);
1584 			vp->v_specinfo = NULL;
1585 		}
1586 	}
1587 	LOCK_ASSERT(!simple_lock_held(&vp->v_interlock));
1588 
1589 	/*
1590 	 * If purging an active vnode, it must be closed and
1591 	 * deactivated before being reclaimed. Note that the
1592 	 * VOP_INACTIVE will unlock the vnode.
1593 	 */
1594 	if (active) {
1595 		VOP_INACTIVE(vp, l);
1596 	} else {
1597 		/*
1598 		 * Any other processes trying to obtain this lock must first
1599 		 * wait for VXLOCK to clear, then call the new lock operation.
1600 		 */
1601 		VOP_UNLOCK(vp, 0);
1602 	}
1603 	/*
1604 	 * Reclaim the vnode.
1605 	 */
1606 	if (VOP_RECLAIM(vp, l))
1607 		panic("vclean: cannot reclaim, vp %p", vp);
1608 	if (active) {
1609 		/*
1610 		 * Inline copy of vrele() since VOP_INACTIVE
1611 		 * has already been called.
1612 		 */
1613 		simple_lock(&vp->v_interlock);
1614 		if (--vp->v_usecount <= 0) {
1615 #ifdef DIAGNOSTIC
1616 			if (vp->v_usecount < 0 || vp->v_writecount != 0) {
1617 				vprint("vclean: bad ref count", vp);
1618 				panic("vclean: ref cnt");
1619 			}
1620 #endif
1621 			/*
1622 			 * Insert at tail of LRU list.
1623 			 */
1624 
1625 			simple_unlock(&vp->v_interlock);
1626 			simple_lock(&vnode_free_list_slock);
1627 #ifdef DIAGNOSTIC
1628 			if (vp->v_holdcnt > 0)
1629 				panic("vclean: not clean, vp %p", vp);
1630 #endif
1631 			TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
1632 			simple_unlock(&vnode_free_list_slock);
1633 		} else
1634 			simple_unlock(&vp->v_interlock);
1635 	}
1636 
1637 	KASSERT(vp->v_uobj.uo_npages == 0);
1638 	if (vp->v_type == VREG && vp->v_ractx != NULL) {
1639 		uvm_ra_freectx(vp->v_ractx);
1640 		vp->v_ractx = NULL;
1641 	}
1642 	cache_purge(vp);
1643 
1644 	/*
1645 	 * Done with purge, notify sleepers of the grim news.
1646 	 */
1647 	vp->v_op = dead_vnodeop_p;
1648 	vp->v_tag = VT_NON;
1649 	simple_lock(&vp->v_interlock);
1650 	VN_KNOTE(vp, NOTE_REVOKE);	/* FreeBSD has this in vn_pollgone() */
1651 	vp->v_flag &= ~(VXLOCK|VLOCKSWORK);
1652 	if (vp->v_flag & VXWANT) {
1653 		vp->v_flag &= ~VXWANT;
1654 		simple_unlock(&vp->v_interlock);
1655 		wakeup((caddr_t)vp);
1656 	} else
1657 		simple_unlock(&vp->v_interlock);
1658 }
1659 
1660 /*
1661  * Recycle an unused vnode to the front of the free list.
1662  * Release the passed interlock if the vnode will be recycled.
1663  */
1664 int
1665 vrecycle(struct vnode *vp, struct simplelock *inter_lkp, struct lwp *l)
1666 {
1667 
1668 	simple_lock(&vp->v_interlock);
1669 	if (vp->v_usecount == 0) {
1670 		if (inter_lkp)
1671 			simple_unlock(inter_lkp);
1672 		vgonel(vp, l);
1673 		return (1);
1674 	}
1675 	simple_unlock(&vp->v_interlock);
1676 	return (0);
1677 }
1678 
1679 /*
1680  * Eliminate all activity associated with a vnode
1681  * in preparation for reuse.
1682  */
1683 void
1684 vgone(struct vnode *vp)
1685 {
1686 	struct lwp *l = curlwp;		/* XXX */
1687 
1688 	simple_lock(&vp->v_interlock);
1689 	vgonel(vp, l);
1690 }
1691 
1692 /*
1693  * vgone, with the vp interlock held.
1694  */
1695 void
1696 vgonel(struct vnode *vp, struct lwp *l)
1697 {
1698 
1699 	LOCK_ASSERT(simple_lock_held(&vp->v_interlock));
1700 
1701 	/*
1702 	 * If a vgone (or vclean) is already in progress,
1703 	 * wait until it is done and return.
1704 	 */
1705 
1706 	if (vp->v_flag & VXLOCK) {
1707 		vp->v_flag |= VXWANT;
1708 		ltsleep(vp, PINOD | PNORELOCK, "vgone", 0, &vp->v_interlock);
1709 		return;
1710 	}
1711 
1712 	/*
1713 	 * Clean out the filesystem specific data.
1714 	 */
1715 
1716 	vclean(vp, DOCLOSE, l);
1717 	KASSERT((vp->v_flag & VONWORKLST) == 0);
1718 
1719 	/*
1720 	 * Delete from old mount point vnode list, if on one.
1721 	 */
1722 
1723 	if (vp->v_mount != NULL)
1724 		insmntque(vp, (struct mount *)0);
1725 
1726 	/*
1727 	 * The test of the back pointer and the reference count of
1728 	 * zero is because it will be removed from the free list by
1729 	 * getcleanvnode, but will not have its reference count
1730 	 * incremented until after calling vgone. If the reference
1731 	 * count were incremented first, vgone would (incorrectly)
1732 	 * try to close the previous instance of the underlying object.
1733 	 * So, the back pointer is explicitly set to `0xdeadb' in
1734 	 * getnewvnode after removing it from the freelist to ensure
1735 	 * that we do not try to move it here.
1736 	 */
1737 
1738 	vp->v_type = VBAD;
1739 	if (vp->v_usecount == 0) {
1740 		boolean_t dofree;
1741 
1742 		simple_lock(&vnode_free_list_slock);
1743 		if (vp->v_holdcnt > 0)
1744 			panic("vgonel: not clean, vp %p", vp);
1745 		/*
1746 		 * if it isn't on the freelist, we're called by getcleanvnode
1747 		 * and vnode is being re-used.  otherwise, we'll free it.
1748 		 */
1749 		dofree = vp->v_freelist.tqe_prev != (struct vnode **)0xdeadb;
1750 		if (dofree) {
1751 			TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
1752 			numvnodes--;
1753 		}
1754 		simple_unlock(&vnode_free_list_slock);
1755 		if (dofree)
1756 			pool_put(&vnode_pool, vp);
1757 	}
1758 }
1759 
1760 /*
1761  * Lookup a vnode by device number.
1762  */
1763 int
1764 vfinddev(dev_t dev, enum vtype type, struct vnode **vpp)
1765 {
1766 	struct vnode *vp;
1767 	int rc = 0;
1768 
1769 	simple_lock(&spechash_slock);
1770 	for (vp = speclisth[SPECHASH(dev)]; vp; vp = vp->v_specnext) {
1771 		if (dev != vp->v_rdev || type != vp->v_type)
1772 			continue;
1773 		*vpp = vp;
1774 		rc = 1;
1775 		break;
1776 	}
1777 	simple_unlock(&spechash_slock);
1778 	return (rc);
1779 }
1780 
1781 /*
1782  * Revoke all the vnodes corresponding to the specified minor number
1783  * range (endpoints inclusive) of the specified major.
1784  */
1785 void
1786 vdevgone(int maj, int minl, int minh, enum vtype type)
1787 {
1788 	struct vnode *vp;
1789 	int mn;
1790 
1791 	for (mn = minl; mn <= minh; mn++)
1792 		if (vfinddev(makedev(maj, mn), type, &vp))
1793 			VOP_REVOKE(vp, REVOKEALL);
1794 }
1795 
1796 /*
1797  * Calculate the total number of references to a special device.
1798  */
1799 int
1800 vcount(struct vnode *vp)
1801 {
1802 	struct vnode *vq, *vnext;
1803 	int count;
1804 
1805 loop:
1806 	if ((vp->v_flag & VALIASED) == 0)
1807 		return (vp->v_usecount);
1808 	simple_lock(&spechash_slock);
1809 	for (count = 0, vq = *vp->v_hashchain; vq; vq = vnext) {
1810 		vnext = vq->v_specnext;
1811 		if (vq->v_rdev != vp->v_rdev || vq->v_type != vp->v_type)
1812 			continue;
1813 		/*
1814 		 * Alias, but not in use, so flush it out.
1815 		 */
1816 		if (vq->v_usecount == 0 && vq != vp &&
1817 		    (vq->v_flag & VXLOCK) == 0) {
1818 			simple_unlock(&spechash_slock);
1819 			vgone(vq);
1820 			goto loop;
1821 		}
1822 		count += vq->v_usecount;
1823 	}
1824 	simple_unlock(&spechash_slock);
1825 	return (count);
1826 }
1827 
1828 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
1829 #define ARRAY_PRINT(idx, arr) \
1830     ((idx) > 0 && (idx) < ARRAY_SIZE(arr) ? (arr)[(idx)] : "UNKNOWN")
1831 
1832 const char * const vnode_tags[] = { VNODE_TAGS };
1833 const char * const vnode_types[] = { VNODE_TYPES };
1834 const char vnode_flagbits[] = VNODE_FLAGBITS;
1835 
1836 /*
1837  * Print out a description of a vnode.
1838  */
1839 void
1840 vprint(const char *label, struct vnode *vp)
1841 {
1842 	char bf[96];
1843 
1844 	if (label != NULL)
1845 		printf("%s: ", label);
1846 	printf("tag %s(%d) type %s(%d), usecount %d, writecount %ld, "
1847 	    "refcount %ld,", ARRAY_PRINT(vp->v_tag, vnode_tags), vp->v_tag,
1848 	    ARRAY_PRINT(vp->v_type, vnode_types), vp->v_type,
1849 	    vp->v_usecount, vp->v_writecount, vp->v_holdcnt);
1850 	bitmask_snprintf(vp->v_flag, vnode_flagbits, bf, sizeof(bf));
1851 	if (bf[0] != '\0')
1852 		printf(" flags (%s)", &bf[1]);
1853 	if (vp->v_data == NULL) {
1854 		printf("\n");
1855 	} else {
1856 		printf("\n\t");
1857 		VOP_PRINT(vp);
1858 	}
1859 }
1860 
1861 #ifdef DEBUG
1862 /*
1863  * List all of the locked vnodes in the system.
1864  * Called when debugging the kernel.
1865  */
1866 void
1867 printlockedvnodes(void)
1868 {
1869 	struct mount *mp, *nmp;
1870 	struct vnode *vp;
1871 
1872 	printf("Locked vnodes\n");
1873 	simple_lock(&mountlist_slock);
1874 	for (mp = CIRCLEQ_FIRST(&mountlist); mp != (void *)&mountlist;
1875 	     mp = nmp) {
1876 		if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) {
1877 			nmp = CIRCLEQ_NEXT(mp, mnt_list);
1878 			continue;
1879 		}
1880 		LIST_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) {
1881 			if (VOP_ISLOCKED(vp))
1882 				vprint(NULL, vp);
1883 		}
1884 		simple_lock(&mountlist_slock);
1885 		nmp = CIRCLEQ_NEXT(mp, mnt_list);
1886 		vfs_unbusy(mp);
1887 	}
1888 	simple_unlock(&mountlist_slock);
1889 }
1890 #endif
1891 
1892 /*
1893  * sysctl helper routine to return list of supported fstypes
1894  */
1895 static int
1896 sysctl_vfs_generic_fstypes(SYSCTLFN_ARGS)
1897 {
1898 	char bf[MFSNAMELEN];
1899 	char *where = oldp;
1900 	struct vfsops *v;
1901 	size_t needed, left, slen;
1902 	int error, first;
1903 
1904 	if (newp != NULL)
1905 		return (EPERM);
1906 	if (namelen != 0)
1907 		return (EINVAL);
1908 
1909 	first = 1;
1910 	error = 0;
1911 	needed = 0;
1912 	left = *oldlenp;
1913 
1914 	LIST_FOREACH(v, &vfs_list, vfs_list) {
1915 		if (where == NULL)
1916 			needed += strlen(v->vfs_name) + 1;
1917 		else {
1918 			memset(bf, 0, sizeof(bf));
1919 			if (first) {
1920 				strncpy(bf, v->vfs_name, sizeof(bf));
1921 				first = 0;
1922 			} else {
1923 				bf[0] = ' ';
1924 				strncpy(bf + 1, v->vfs_name, sizeof(bf) - 1);
1925 			}
1926 			bf[sizeof(bf)-1] = '\0';
1927 			slen = strlen(bf);
1928 			if (left < slen + 1)
1929 				break;
1930 			/* +1 to copy out the trailing NUL byte */
1931 			error = copyout(bf, where, slen + 1);
1932 			if (error)
1933 				break;
1934 			where += slen;
1935 			needed += slen;
1936 			left -= slen;
1937 		}
1938 	}
1939 	*oldlenp = needed;
1940 	return (error);
1941 }
1942 
1943 /*
1944  * Top level filesystem related information gathering.
1945  */
1946 SYSCTL_SETUP(sysctl_vfs_setup, "sysctl vfs subtree setup")
1947 {
1948 	sysctl_createv(clog, 0, NULL, NULL,
1949 		       CTLFLAG_PERMANENT,
1950 		       CTLTYPE_NODE, "vfs", NULL,
1951 		       NULL, 0, NULL, 0,
1952 		       CTL_VFS, CTL_EOL);
1953 	sysctl_createv(clog, 0, NULL, NULL,
1954 		       CTLFLAG_PERMANENT,
1955 		       CTLTYPE_NODE, "generic",
1956 		       SYSCTL_DESCR("Non-specific vfs related information"),
1957 		       NULL, 0, NULL, 0,
1958 		       CTL_VFS, VFS_GENERIC, CTL_EOL);
1959 	sysctl_createv(clog, 0, NULL, NULL,
1960 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1961 		       CTLTYPE_INT, "usermount",
1962 		       SYSCTL_DESCR("Whether unprivileged users may mount "
1963 				    "filesystems"),
1964 		       NULL, 0, &dovfsusermount, 0,
1965 		       CTL_VFS, VFS_GENERIC, VFS_USERMOUNT, CTL_EOL);
1966 	sysctl_createv(clog, 0, NULL, NULL,
1967 		       CTLFLAG_PERMANENT,
1968 		       CTLTYPE_STRING, "fstypes",
1969 		       SYSCTL_DESCR("List of file systems present"),
1970 		       sysctl_vfs_generic_fstypes, 0, NULL, 0,
1971 		       CTL_VFS, VFS_GENERIC, CTL_CREATE, CTL_EOL);
1972 }
1973 
1974 
1975 int kinfo_vdebug = 1;
1976 int kinfo_vgetfailed;
1977 #define KINFO_VNODESLOP	10
1978 /*
1979  * Dump vnode list (via sysctl).
1980  * Copyout address of vnode followed by vnode.
1981  */
1982 /* ARGSUSED */
1983 int
1984 sysctl_kern_vnode(SYSCTLFN_ARGS)
1985 {
1986 	char *where = oldp;
1987 	size_t *sizep = oldlenp;
1988 	struct mount *mp, *nmp;
1989 	struct vnode *nvp, *vp;
1990 	char *bp = where, *savebp;
1991 	char *ewhere;
1992 	int error;
1993 
1994 	if (namelen != 0)
1995 		return (EOPNOTSUPP);
1996 	if (newp != NULL)
1997 		return (EPERM);
1998 
1999 #define VPTRSZ	sizeof(struct vnode *)
2000 #define VNODESZ	sizeof(struct vnode)
2001 	if (where == NULL) {
2002 		*sizep = (numvnodes + KINFO_VNODESLOP) * (VPTRSZ + VNODESZ);
2003 		return (0);
2004 	}
2005 	ewhere = where + *sizep;
2006 
2007 	simple_lock(&mountlist_slock);
2008 	for (mp = CIRCLEQ_FIRST(&mountlist); mp != (void *)&mountlist;
2009 	     mp = nmp) {
2010 		if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) {
2011 			nmp = CIRCLEQ_NEXT(mp, mnt_list);
2012 			continue;
2013 		}
2014 		savebp = bp;
2015 again:
2016 		simple_lock(&mntvnode_slock);
2017 		for (vp = LIST_FIRST(&mp->mnt_vnodelist);
2018 		     vp != NULL;
2019 		     vp = nvp) {
2020 			/*
2021 			 * Check that the vp is still associated with
2022 			 * this filesystem.  RACE: could have been
2023 			 * recycled onto the same filesystem.
2024 			 */
2025 			if (vp->v_mount != mp) {
2026 				simple_unlock(&mntvnode_slock);
2027 				if (kinfo_vdebug)
2028 					printf("kinfo: vp changed\n");
2029 				bp = savebp;
2030 				goto again;
2031 			}
2032 			nvp = LIST_NEXT(vp, v_mntvnodes);
2033 			if (bp + VPTRSZ + VNODESZ > ewhere) {
2034 				simple_unlock(&mntvnode_slock);
2035 				*sizep = bp - where;
2036 				return (ENOMEM);
2037 			}
2038 			simple_unlock(&mntvnode_slock);
2039 			if ((error = copyout((caddr_t)&vp, bp, VPTRSZ)) ||
2040 			   (error = copyout((caddr_t)vp, bp + VPTRSZ, VNODESZ)))
2041 				return (error);
2042 			bp += VPTRSZ + VNODESZ;
2043 			simple_lock(&mntvnode_slock);
2044 		}
2045 		simple_unlock(&mntvnode_slock);
2046 		simple_lock(&mountlist_slock);
2047 		nmp = CIRCLEQ_NEXT(mp, mnt_list);
2048 		vfs_unbusy(mp);
2049 	}
2050 	simple_unlock(&mountlist_slock);
2051 
2052 	*sizep = bp - where;
2053 	return (0);
2054 }
2055 
2056 /*
2057  * Check to see if a filesystem is mounted on a block device.
2058  */
2059 int
2060 vfs_mountedon(struct vnode *vp)
2061 {
2062 	struct vnode *vq;
2063 	int error = 0;
2064 
2065 	if (vp->v_specmountpoint != NULL)
2066 		return (EBUSY);
2067 	if (vp->v_flag & VALIASED) {
2068 		simple_lock(&spechash_slock);
2069 		for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
2070 			if (vq->v_rdev != vp->v_rdev ||
2071 			    vq->v_type != vp->v_type)
2072 				continue;
2073 			if (vq->v_specmountpoint != NULL) {
2074 				error = EBUSY;
2075 				break;
2076 			}
2077 		}
2078 		simple_unlock(&spechash_slock);
2079 	}
2080 	return (error);
2081 }
2082 
2083 /*
2084  * Do the usual access checking.
2085  * file_mode, uid and gid are from the vnode in question,
2086  * while acc_mode and cred are from the VOP_ACCESS parameter list
2087  */
2088 int
2089 vaccess(enum vtype type, mode_t file_mode, uid_t uid, gid_t gid,
2090     mode_t acc_mode, struct ucred *cred)
2091 {
2092 	mode_t mask;
2093 
2094 	/*
2095 	 * Super-user always gets read/write access, but execute access depends
2096 	 * on at least one execute bit being set.
2097 	 */
2098 	if (cred->cr_uid == 0) {
2099 		if ((acc_mode & VEXEC) && type != VDIR &&
2100 		    (file_mode & (S_IXUSR|S_IXGRP|S_IXOTH)) == 0)
2101 			return (EACCES);
2102 		return (0);
2103 	}
2104 
2105 	mask = 0;
2106 
2107 	/* Otherwise, check the owner. */
2108 	if (cred->cr_uid == uid) {
2109 		if (acc_mode & VEXEC)
2110 			mask |= S_IXUSR;
2111 		if (acc_mode & VREAD)
2112 			mask |= S_IRUSR;
2113 		if (acc_mode & VWRITE)
2114 			mask |= S_IWUSR;
2115 		return ((file_mode & mask) == mask ? 0 : EACCES);
2116 	}
2117 
2118 	/* Otherwise, check the groups. */
2119 	if (cred->cr_gid == gid || groupmember(gid, cred)) {
2120 		if (acc_mode & VEXEC)
2121 			mask |= S_IXGRP;
2122 		if (acc_mode & VREAD)
2123 			mask |= S_IRGRP;
2124 		if (acc_mode & VWRITE)
2125 			mask |= S_IWGRP;
2126 		return ((file_mode & mask) == mask ? 0 : EACCES);
2127 	}
2128 
2129 	/* Otherwise, check everyone else. */
2130 	if (acc_mode & VEXEC)
2131 		mask |= S_IXOTH;
2132 	if (acc_mode & VREAD)
2133 		mask |= S_IROTH;
2134 	if (acc_mode & VWRITE)
2135 		mask |= S_IWOTH;
2136 	return ((file_mode & mask) == mask ? 0 : EACCES);
2137 }
2138 
2139 /*
2140  * Unmount all file systems.
2141  * We traverse the list in reverse order under the assumption that doing so
2142  * will avoid needing to worry about dependencies.
2143  */
2144 void
2145 vfs_unmountall(struct lwp *l)
2146 {
2147 	struct mount *mp, *nmp;
2148 	int allerror, error;
2149 
2150 	printf("unmounting file systems...");
2151 	for (allerror = 0,
2152 	     mp = mountlist.cqh_last; mp != (void *)&mountlist; mp = nmp) {
2153 		nmp = mp->mnt_list.cqe_prev;
2154 #ifdef DEBUG
2155 		printf("\nunmounting %s (%s)...",
2156 		    mp->mnt_stat.f_mntonname, mp->mnt_stat.f_mntfromname);
2157 #endif
2158 		/*
2159 		 * XXX Freeze syncer.  Must do this before locking the
2160 		 * mount point.  See dounmount() for details.
2161 		 */
2162 		lockmgr(&syncer_lock, LK_EXCLUSIVE, NULL);
2163 		if (vfs_busy(mp, 0, 0)) {
2164 			lockmgr(&syncer_lock, LK_RELEASE, NULL);
2165 			continue;
2166 		}
2167 		if ((error = dounmount(mp, MNT_FORCE, l)) != 0) {
2168 			printf("unmount of %s failed with error %d\n",
2169 			    mp->mnt_stat.f_mntonname, error);
2170 			allerror = 1;
2171 		}
2172 	}
2173 	printf(" done\n");
2174 	if (allerror)
2175 		printf("WARNING: some file systems would not unmount\n");
2176 }
2177 
2178 extern struct simplelock bqueue_slock; /* XXX */
2179 
2180 /*
2181  * Sync and unmount file systems before shutting down.
2182  */
2183 void
2184 vfs_shutdown(void)
2185 {
2186 	struct lwp *l = curlwp;
2187 	struct proc *p;
2188 
2189 	/* XXX we're certainly not running in proc0's context! */
2190 	if (l == NULL || (p = l->l_proc) == NULL)
2191 		p = &proc0;
2192 
2193 	printf("syncing disks... ");
2194 
2195 	/* remove user process from run queue */
2196 	suspendsched();
2197 	(void) spl0();
2198 
2199 	/* avoid coming back this way again if we panic. */
2200 	doing_shutdown = 1;
2201 
2202 	sys_sync(l, NULL, NULL);
2203 
2204 	/* Wait for sync to finish. */
2205 	if (buf_syncwait() != 0) {
2206 #if defined(DDB) && defined(DEBUG_HALT_BUSY)
2207 		Debugger();
2208 #endif
2209 		printf("giving up\n");
2210 		return;
2211 	} else
2212 		printf("done\n");
2213 
2214 	/*
2215 	 * If we've panic'd, don't make the situation potentially
2216 	 * worse by unmounting the file systems.
2217 	 */
2218 	if (panicstr != NULL)
2219 		return;
2220 
2221 	/* Release inodes held by texts before update. */
2222 #ifdef notdef
2223 	vnshutdown();
2224 #endif
2225 	/* Unmount file systems. */
2226 	vfs_unmountall(l);
2227 }
2228 
2229 /*
2230  * Mount the root file system.  If the operator didn't specify a
2231  * file system to use, try all possible file systems until one
2232  * succeeds.
2233  */
2234 int
2235 vfs_mountroot(void)
2236 {
2237 	struct vfsops *v;
2238 	int error = ENODEV;
2239 
2240 	if (root_device == NULL)
2241 		panic("vfs_mountroot: root device unknown");
2242 
2243 	switch (root_device->dv_class) {
2244 	case DV_IFNET:
2245 		if (rootdev != NODEV)
2246 			panic("vfs_mountroot: rootdev set for DV_IFNET "
2247 			    "(0x%08x -> %d,%d)", rootdev,
2248 			    major(rootdev), minor(rootdev));
2249 		break;
2250 
2251 	case DV_DISK:
2252 		if (rootdev == NODEV)
2253 			panic("vfs_mountroot: rootdev not set for DV_DISK");
2254 	        if (bdevvp(rootdev, &rootvp))
2255 	                panic("vfs_mountroot: can't get vnode for rootdev");
2256 		error = VOP_OPEN(rootvp, FREAD, FSCRED, curlwp);
2257 		if (error) {
2258 			printf("vfs_mountroot: can't open root device\n");
2259 			return (error);
2260 		}
2261 		break;
2262 
2263 	default:
2264 		printf("%s: inappropriate for root file system\n",
2265 		    root_device->dv_xname);
2266 		return (ENODEV);
2267 	}
2268 
2269 	/*
2270 	 * If user specified a file system, use it.
2271 	 */
2272 	if (mountroot != NULL) {
2273 		error = (*mountroot)();
2274 		goto done;
2275 	}
2276 
2277 	/*
2278 	 * Try each file system currently configured into the kernel.
2279 	 */
2280 	LIST_FOREACH(v, &vfs_list, vfs_list) {
2281 		if (v->vfs_mountroot == NULL)
2282 			continue;
2283 #ifdef DEBUG
2284 		aprint_normal("mountroot: trying %s...\n", v->vfs_name);
2285 #endif
2286 		error = (*v->vfs_mountroot)();
2287 		if (!error) {
2288 			aprint_normal("root file system type: %s\n",
2289 			    v->vfs_name);
2290 			break;
2291 		}
2292 	}
2293 
2294 	if (v == NULL) {
2295 		printf("no file system for %s", root_device->dv_xname);
2296 		if (root_device->dv_class == DV_DISK)
2297 			printf(" (dev 0x%x)", rootdev);
2298 		printf("\n");
2299 		error = EFTYPE;
2300 	}
2301 
2302 done:
2303 	if (error && root_device->dv_class == DV_DISK) {
2304 		VOP_CLOSE(rootvp, FREAD, FSCRED, curlwp);
2305 		vrele(rootvp);
2306 	}
2307 	return (error);
2308 }
2309 
2310 /*
2311  * Given a file system name, look up the vfsops for that
2312  * file system, or return NULL if file system isn't present
2313  * in the kernel.
2314  */
2315 struct vfsops *
2316 vfs_getopsbyname(const char *name)
2317 {
2318 	struct vfsops *v;
2319 
2320 	LIST_FOREACH(v, &vfs_list, vfs_list) {
2321 		if (strcmp(v->vfs_name, name) == 0)
2322 			break;
2323 	}
2324 
2325 	return (v);
2326 }
2327 
2328 /*
2329  * Establish a file system and initialize it.
2330  */
2331 int
2332 vfs_attach(struct vfsops *vfs)
2333 {
2334 	struct vfsops *v;
2335 	int error = 0;
2336 
2337 
2338 	/*
2339 	 * Make sure this file system doesn't already exist.
2340 	 */
2341 	LIST_FOREACH(v, &vfs_list, vfs_list) {
2342 		if (strcmp(vfs->vfs_name, v->vfs_name) == 0) {
2343 			error = EEXIST;
2344 			goto out;
2345 		}
2346 	}
2347 
2348 	/*
2349 	 * Initialize the vnode operations for this file system.
2350 	 */
2351 	vfs_opv_init(vfs->vfs_opv_descs);
2352 
2353 	/*
2354 	 * Now initialize the file system itself.
2355 	 */
2356 	(*vfs->vfs_init)();
2357 
2358 	/*
2359 	 * ...and link it into the kernel's list.
2360 	 */
2361 	LIST_INSERT_HEAD(&vfs_list, vfs, vfs_list);
2362 
2363 	/*
2364 	 * Sanity: make sure the reference count is 0.
2365 	 */
2366 	vfs->vfs_refcount = 0;
2367 
2368  out:
2369 	return (error);
2370 }
2371 
2372 /*
2373  * Remove a file system from the kernel.
2374  */
2375 int
2376 vfs_detach(struct vfsops *vfs)
2377 {
2378 	struct vfsops *v;
2379 
2380 	/*
2381 	 * Make sure no one is using the filesystem.
2382 	 */
2383 	if (vfs->vfs_refcount != 0)
2384 		return (EBUSY);
2385 
2386 	/*
2387 	 * ...and remove it from the kernel's list.
2388 	 */
2389 	LIST_FOREACH(v, &vfs_list, vfs_list) {
2390 		if (v == vfs) {
2391 			LIST_REMOVE(v, vfs_list);
2392 			break;
2393 		}
2394 	}
2395 
2396 	if (v == NULL)
2397 		return (ESRCH);
2398 
2399 	/*
2400 	 * Now run the file system-specific cleanups.
2401 	 */
2402 	(*vfs->vfs_done)();
2403 
2404 	/*
2405 	 * Free the vnode operations vector.
2406 	 */
2407 	vfs_opv_free(vfs->vfs_opv_descs);
2408 	return (0);
2409 }
2410 
2411 void
2412 vfs_reinit(void)
2413 {
2414 	struct vfsops *vfs;
2415 
2416 	LIST_FOREACH(vfs, &vfs_list, vfs_list) {
2417 		if (vfs->vfs_reinit) {
2418 			(*vfs->vfs_reinit)();
2419 		}
2420 	}
2421 }
2422 
2423 /*
2424  * Request a filesystem to suspend write operations.
2425  */
2426 int
2427 vfs_write_suspend(struct mount *mp, int slpflag, int slptimeo)
2428 {
2429 	struct lwp *l = curlwp;	/* XXX */
2430 	int error;
2431 
2432 	while ((mp->mnt_iflag & IMNT_SUSPEND)) {
2433 		if (slptimeo < 0)
2434 			return EWOULDBLOCK;
2435 		error = tsleep(&mp->mnt_flag, slpflag, "suspwt1", slptimeo);
2436 		if (error)
2437 			return error;
2438 	}
2439 	mp->mnt_iflag |= IMNT_SUSPEND;
2440 
2441 	simple_lock(&mp->mnt_slock);
2442 	if (mp->mnt_writeopcountupper > 0)
2443 		ltsleep(&mp->mnt_writeopcountupper, PUSER - 1, "suspwt",
2444 			0, &mp->mnt_slock);
2445 	simple_unlock(&mp->mnt_slock);
2446 
2447 	error = VFS_SYNC(mp, MNT_WAIT, l->l_proc->p_ucred, l);
2448 	if (error) {
2449 		vfs_write_resume(mp);
2450 		return error;
2451 	}
2452 	mp->mnt_iflag |= IMNT_SUSPENDLOW;
2453 
2454 	simple_lock(&mp->mnt_slock);
2455 	if (mp->mnt_writeopcountlower > 0)
2456 		ltsleep(&mp->mnt_writeopcountlower, PUSER - 1, "suspwt",
2457 			0, &mp->mnt_slock);
2458 	mp->mnt_iflag |= IMNT_SUSPENDED;
2459 	simple_unlock(&mp->mnt_slock);
2460 
2461 	return 0;
2462 }
2463 
2464 /*
2465  * Request a filesystem to resume write operations.
2466  */
2467 void
2468 vfs_write_resume(struct mount *mp)
2469 {
2470 
2471 	if ((mp->mnt_iflag & IMNT_SUSPEND) == 0)
2472 		return;
2473 	mp->mnt_iflag &= ~(IMNT_SUSPEND | IMNT_SUSPENDLOW | IMNT_SUSPENDED);
2474 	wakeup(&mp->mnt_flag);
2475 }
2476 
2477 void
2478 copy_statvfs_info(struct statvfs *sbp, const struct mount *mp)
2479 {
2480 	const struct statvfs *mbp;
2481 
2482 	if (sbp == (mbp = &mp->mnt_stat))
2483 		return;
2484 
2485 	(void)memcpy(&sbp->f_fsidx, &mbp->f_fsidx, sizeof(sbp->f_fsidx));
2486 	sbp->f_fsid = mbp->f_fsid;
2487 	sbp->f_owner = mbp->f_owner;
2488 	sbp->f_flag = mbp->f_flag;
2489 	sbp->f_syncwrites = mbp->f_syncwrites;
2490 	sbp->f_asyncwrites = mbp->f_asyncwrites;
2491 	sbp->f_syncreads = mbp->f_syncreads;
2492 	sbp->f_asyncreads = mbp->f_asyncreads;
2493 	(void)memcpy(sbp->f_spare, mbp->f_spare, sizeof(mbp->f_spare));
2494 	(void)memcpy(sbp->f_fstypename, mbp->f_fstypename,
2495 	    sizeof(sbp->f_fstypename));
2496 	(void)memcpy(sbp->f_mntonname, mbp->f_mntonname,
2497 	    sizeof(sbp->f_mntonname));
2498 	(void)memcpy(sbp->f_mntfromname, mp->mnt_stat.f_mntfromname,
2499 	    sizeof(sbp->f_mntfromname));
2500 	sbp->f_namemax = mbp->f_namemax;
2501 }
2502 
2503 int
2504 set_statvfs_info(const char *onp, int ukon, const char *fromp, int ukfrom,
2505     struct mount *mp, struct lwp *l)
2506 {
2507 	int error;
2508 	size_t size;
2509 	struct statvfs *sfs = &mp->mnt_stat;
2510 	int (*fun)(const void *, void *, size_t, size_t *);
2511 
2512 	(void)strncpy(mp->mnt_stat.f_fstypename, mp->mnt_op->vfs_name,
2513 	    sizeof(mp->mnt_stat.f_fstypename));
2514 
2515 	if (onp) {
2516 		struct cwdinfo *cwdi = l->l_proc->p_cwdi;
2517 		fun = (ukon == UIO_SYSSPACE) ? copystr : copyinstr;
2518 		if (cwdi->cwdi_rdir != NULL) {
2519 			size_t len;
2520 			char *bp;
2521 			char *path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
2522 
2523 			if (!path) /* XXX can't happen with M_WAITOK */
2524 				return ENOMEM;
2525 
2526 			bp = path + MAXPATHLEN;
2527 			*--bp = '\0';
2528 			error = getcwd_common(cwdi->cwdi_rdir, rootvnode, &bp,
2529 			    path, MAXPATHLEN / 2, 0, l);
2530 			if (error) {
2531 				free(path, M_TEMP);
2532 				return error;
2533 			}
2534 
2535 			len = strlen(bp);
2536 			if (len > sizeof(sfs->f_mntonname) - 1)
2537 				len = sizeof(sfs->f_mntonname) - 1;
2538 			(void)strncpy(sfs->f_mntonname, bp, len);
2539 			free(path, M_TEMP);
2540 
2541 			if (len < sizeof(sfs->f_mntonname) - 1) {
2542 				error = (*fun)(onp, &sfs->f_mntonname[len],
2543 				    sizeof(sfs->f_mntonname) - len - 1, &size);
2544 				if (error)
2545 					return error;
2546 				size += len;
2547 			} else {
2548 				size = len;
2549 			}
2550 		} else {
2551 			error = (*fun)(onp, &sfs->f_mntonname,
2552 			    sizeof(sfs->f_mntonname) - 1, &size);
2553 			if (error)
2554 				return error;
2555 		}
2556 		(void)memset(sfs->f_mntonname + size, 0,
2557 		    sizeof(sfs->f_mntonname) - size);
2558 	}
2559 
2560 	if (fromp) {
2561 		fun = (ukfrom == UIO_SYSSPACE) ? copystr : copyinstr;
2562 		error = (*fun)(fromp, sfs->f_mntfromname,
2563 		    sizeof(sfs->f_mntfromname) - 1, &size);
2564 		if (error)
2565 			return error;
2566 		(void)memset(sfs->f_mntfromname + size, 0,
2567 		    sizeof(sfs->f_mntfromname) - size);
2568 	}
2569 	return 0;
2570 }
2571 
2572 #ifdef DDB
2573 static const char buf_flagbits[] = BUF_FLAGBITS;
2574 
2575 void
2576 vfs_buf_print(struct buf *bp, int full, void (*pr)(const char *, ...))
2577 {
2578 	char bf[1024];
2579 
2580 	(*pr)("  vp %p lblkno 0x%"PRIx64" blkno 0x%"PRIx64" dev 0x%x\n",
2581 		  bp->b_vp, bp->b_lblkno, bp->b_blkno, bp->b_dev);
2582 
2583 	bitmask_snprintf(bp->b_flags, buf_flagbits, bf, sizeof(bf));
2584 	(*pr)("  error %d flags 0x%s\n", bp->b_error, bf);
2585 
2586 	(*pr)("  bufsize 0x%lx bcount 0x%lx resid 0x%lx\n",
2587 		  bp->b_bufsize, bp->b_bcount, bp->b_resid);
2588 	(*pr)("  data %p saveaddr %p dep %p\n",
2589 		  bp->b_data, bp->b_saveaddr, LIST_FIRST(&bp->b_dep));
2590 	(*pr)("  iodone %p\n", bp->b_iodone);
2591 }
2592 
2593 
2594 void
2595 vfs_vnode_print(struct vnode *vp, int full, void (*pr)(const char *, ...))
2596 {
2597 	char bf[256];
2598 
2599 	uvm_object_printit(&vp->v_uobj, full, pr);
2600 	bitmask_snprintf(vp->v_flag, vnode_flagbits, bf, sizeof(bf));
2601 	(*pr)("\nVNODE flags %s\n", bf);
2602 	(*pr)("mp %p numoutput %d size 0x%llx\n",
2603 	      vp->v_mount, vp->v_numoutput, vp->v_size);
2604 
2605 	(*pr)("data %p usecount %d writecount %ld holdcnt %ld numoutput %d\n",
2606 	      vp->v_data, vp->v_usecount, vp->v_writecount,
2607 	      vp->v_holdcnt, vp->v_numoutput);
2608 
2609 	(*pr)("tag %s(%d) type %s(%d) mount %p typedata %p\n",
2610 	      ARRAY_PRINT(vp->v_tag, vnode_tags), vp->v_tag,
2611 	      ARRAY_PRINT(vp->v_type, vnode_types), vp->v_type,
2612 	      vp->v_mount, vp->v_mountedhere);
2613 
2614 	if (full) {
2615 		struct buf *bp;
2616 
2617 		(*pr)("clean bufs:\n");
2618 		LIST_FOREACH(bp, &vp->v_cleanblkhd, b_vnbufs) {
2619 			(*pr)(" bp %p\n", bp);
2620 			vfs_buf_print(bp, full, pr);
2621 		}
2622 
2623 		(*pr)("dirty bufs:\n");
2624 		LIST_FOREACH(bp, &vp->v_dirtyblkhd, b_vnbufs) {
2625 			(*pr)(" bp %p\n", bp);
2626 			vfs_buf_print(bp, full, pr);
2627 		}
2628 	}
2629 }
2630 
2631 void
2632 vfs_mount_print(struct mount *mp, int full, void (*pr)(const char *, ...))
2633 {
2634 	char sbuf[256];
2635 
2636 	(*pr)("vnodecovered = %p syncer = %p data = %p\n",
2637 			mp->mnt_vnodecovered,mp->mnt_syncer,mp->mnt_data);
2638 
2639 	(*pr)("fs_bshift %d dev_bshift = %d\n",
2640 			mp->mnt_fs_bshift,mp->mnt_dev_bshift);
2641 
2642 	bitmask_snprintf(mp->mnt_flag, __MNT_FLAG_BITS, sbuf, sizeof(sbuf));
2643 	(*pr)("flag = %s\n", sbuf);
2644 
2645 	bitmask_snprintf(mp->mnt_iflag, __IMNT_FLAG_BITS, sbuf, sizeof(sbuf));
2646 	(*pr)("iflag = %s\n", sbuf);
2647 
2648 	/* XXX use lockmgr_printinfo */
2649 	if (mp->mnt_lock.lk_sharecount)
2650 		(*pr)(" lock type %s: SHARED (count %d)", mp->mnt_lock.lk_wmesg,
2651 		    mp->mnt_lock.lk_sharecount);
2652 	else if (mp->mnt_lock.lk_flags & LK_HAVE_EXCL) {
2653 		(*pr)(" lock type %s: EXCL (count %d) by ",
2654 		    mp->mnt_lock.lk_wmesg, mp->mnt_lock.lk_exclusivecount);
2655 		if (mp->mnt_lock.lk_flags & LK_SPIN)
2656 			(*pr)("processor %lu", mp->mnt_lock.lk_cpu);
2657 		else
2658 			(*pr)("pid %d.%d", mp->mnt_lock.lk_lockholder,
2659 			    mp->mnt_lock.lk_locklwp);
2660 	} else
2661 		(*pr)(" not locked");
2662 	if ((mp->mnt_lock.lk_flags & LK_SPIN) == 0 && mp->mnt_lock.lk_waitcount > 0)
2663 		(*pr)(" with %d pending", mp->mnt_lock.lk_waitcount);
2664 
2665 	(*pr)("\n");
2666 
2667 	if (mp->mnt_unmounter) {
2668 		(*pr)("unmounter pid = %d ",mp->mnt_unmounter->l_proc);
2669 	}
2670 	(*pr)("wcnt = %d, writeopcountupper = %d, writeopcountupper = %d\n",
2671 		mp->mnt_wcnt,mp->mnt_writeopcountupper,mp->mnt_writeopcountlower);
2672 
2673 	(*pr)("statvfs cache:\n");
2674 	(*pr)("\tbsize = %lu\n",mp->mnt_stat.f_bsize);
2675 	(*pr)("\tfrsize = %lu\n",mp->mnt_stat.f_frsize);
2676 	(*pr)("\tiosize = %lu\n",mp->mnt_stat.f_iosize);
2677 
2678 	(*pr)("\tblocks = "PRIu64"\n",mp->mnt_stat.f_blocks);
2679 	(*pr)("\tbfree = "PRIu64"\n",mp->mnt_stat.f_bfree);
2680 	(*pr)("\tbavail = "PRIu64"\n",mp->mnt_stat.f_bavail);
2681 	(*pr)("\tbresvd = "PRIu64"\n",mp->mnt_stat.f_bresvd);
2682 
2683 	(*pr)("\tfiles = "PRIu64"\n",mp->mnt_stat.f_files);
2684 	(*pr)("\tffree = "PRIu64"\n",mp->mnt_stat.f_ffree);
2685 	(*pr)("\tfavail = "PRIu64"\n",mp->mnt_stat.f_favail);
2686 	(*pr)("\tfresvd = "PRIu64"\n",mp->mnt_stat.f_fresvd);
2687 
2688 	(*pr)("\tf_fsidx = { 0x%"PRIx32", 0x%"PRIx32" }\n",
2689 			mp->mnt_stat.f_fsidx.__fsid_val[0],
2690 			mp->mnt_stat.f_fsidx.__fsid_val[1]);
2691 
2692 	(*pr)("\towner = %"PRIu32"\n",mp->mnt_stat.f_owner);
2693 	(*pr)("\tnamemax = %lu\n",mp->mnt_stat.f_namemax);
2694 
2695 	bitmask_snprintf(mp->mnt_stat.f_flag, __MNT_FLAG_BITS, sbuf,
2696 	    sizeof(sbuf));
2697 	(*pr)("\tflag = %s\n",sbuf);
2698 	(*pr)("\tsyncwrites = " PRIu64 "\n",mp->mnt_stat.f_syncwrites);
2699 	(*pr)("\tasyncwrites = " PRIu64 "\n",mp->mnt_stat.f_asyncwrites);
2700 	(*pr)("\tsyncreads = " PRIu64 "\n",mp->mnt_stat.f_syncreads);
2701 	(*pr)("\tasyncreads = " PRIu64 "\n",mp->mnt_stat.f_asyncreads);
2702 	(*pr)("\tfstypename = %s\n",mp->mnt_stat.f_fstypename);
2703 	(*pr)("\tmntonname = %s\n",mp->mnt_stat.f_mntonname);
2704 	(*pr)("\tmntfromname = %s\n",mp->mnt_stat.f_mntfromname);
2705 
2706 	{
2707 		int cnt = 0;
2708 		struct vnode *vp;
2709 		(*pr)("locked vnodes =");
2710 		/* XXX would take mountlist lock, except ddb may not have context */
2711 		LIST_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) {
2712 			if (VOP_ISLOCKED(vp)) {
2713 				if ((++cnt % 6) == 0) {
2714 					(*pr)(" %p,\n\t", vp);
2715 				} else {
2716 					(*pr)(" %p,", vp);
2717 				}
2718 			}
2719 		}
2720 		(*pr)("\n");
2721 	}
2722 
2723 	if (full) {
2724 		int cnt = 0;
2725 		struct vnode *vp;
2726 		(*pr)("all vnodes =");
2727 		/* XXX would take mountlist lock, except ddb may not have context */
2728 		LIST_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) {
2729 			if (!LIST_NEXT(vp, v_mntvnodes)) {
2730 				(*pr)(" %p", vp);
2731 			} else if ((++cnt % 6) == 0) {
2732 				(*pr)(" %p,\n\t", vp);
2733 			} else {
2734 				(*pr)(" %p,", vp);
2735 			}
2736 		}
2737 		(*pr)("\n", vp);
2738 	}
2739 }
2740 #endif /* DDB */
2741