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