xref: /openbsd-src/sys/kern/vfs_subr.c (revision c90a81c56dcebd6a1b73fe4aff9b03385b8e63b3)
1 /*	$OpenBSD: vfs_subr.c,v 1.284 2018/12/23 10:46:51 natano Exp $	*/
2 /*	$NetBSD: vfs_subr.c,v 1.53 1996/04/22 01:39:13 christos Exp $	*/
3 
4 /*
5  * Copyright (c) 1989, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  * (c) UNIX System Laboratories, Inc.
8  * All or some portions of this file are derived from material licensed
9  * to the University of California by American Telephone and Telegraph
10  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
11  * the permission of UNIX System Laboratories, Inc.
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. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  *	@(#)vfs_subr.c	8.13 (Berkeley) 4/18/94
38  */
39 
40 /*
41  * External virtual filesystem routines
42  */
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/proc.h>
47 #include <sys/sysctl.h>
48 #include <sys/mount.h>
49 #include <sys/time.h>
50 #include <sys/fcntl.h>
51 #include <sys/kernel.h>
52 #include <sys/conf.h>
53 #include <sys/vnode.h>
54 #include <sys/lock.h>
55 #include <sys/stat.h>
56 #include <sys/acct.h>
57 #include <sys/namei.h>
58 #include <sys/ucred.h>
59 #include <sys/buf.h>
60 #include <sys/errno.h>
61 #include <sys/malloc.h>
62 #include <sys/mbuf.h>
63 #include <sys/syscallargs.h>
64 #include <sys/pool.h>
65 #include <sys/tree.h>
66 #include <sys/specdev.h>
67 #include <sys/atomic.h>
68 
69 #include <netinet/in.h>
70 
71 #include <uvm/uvm_extern.h>
72 #include <uvm/uvm_vnode.h>
73 
74 #include "softraid.h"
75 
76 void sr_quiesce(void);
77 
78 enum vtype iftovt_tab[16] = {
79 	VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
80 	VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
81 };
82 
83 int	vttoif_tab[9] = {
84 	0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
85 	S_IFSOCK, S_IFIFO, S_IFMT,
86 };
87 
88 int prtactive = 0;		/* 1 => print out reclaim of active vnodes */
89 int suid_clear = 1;		/* 1 => clear SUID / SGID on owner change */
90 
91 /*
92  * Insq/Remq for the vnode usage lists.
93  */
94 #define	bufinsvn(bp, dp)	LIST_INSERT_HEAD(dp, bp, b_vnbufs)
95 #define	bufremvn(bp) {							\
96 	LIST_REMOVE(bp, b_vnbufs);					\
97 	LIST_NEXT(bp, b_vnbufs) = NOLIST;				\
98 }
99 
100 struct freelst vnode_hold_list;	/* list of vnodes referencing buffers */
101 struct freelst vnode_free_list;	/* vnode free list */
102 
103 struct mntlist mountlist;	/* mounted filesystem list */
104 
105 void	vclean(struct vnode *, int, struct proc *);
106 
107 void insmntque(struct vnode *, struct mount *);
108 int getdevvp(dev_t, struct vnode **, enum vtype);
109 
110 int vfs_hang_addrlist(struct mount *, struct netexport *,
111 				  struct export_args *);
112 int vfs_free_netcred(struct radix_node *, void *, u_int);
113 void vfs_free_addrlist(struct netexport *);
114 void vputonfreelist(struct vnode *);
115 
116 int vflush_vnode(struct vnode *, void *);
117 int maxvnodes;
118 
119 void vfs_unmountall(void);
120 
121 #ifdef DEBUG
122 void printlockedvnodes(void);
123 #endif
124 
125 struct pool vnode_pool;
126 struct pool uvm_vnode_pool;
127 
128 static inline int rb_buf_compare(const struct buf *b1, const struct buf *b2);
129 RBT_GENERATE(buf_rb_bufs, buf, b_rbbufs, rb_buf_compare);
130 
131 static inline int
132 rb_buf_compare(const struct buf *b1, const struct buf *b2)
133 {
134 	if (b1->b_lblkno < b2->b_lblkno)
135 		return(-1);
136 	if (b1->b_lblkno > b2->b_lblkno)
137 		return(1);
138 	return(0);
139 }
140 
141 /*
142  * Initialize the vnode management data structures.
143  */
144 void
145 vntblinit(void)
146 {
147 	/* buffer cache may need a vnode for each buffer */
148 	maxvnodes = 2 * initialvnodes;
149 	pool_init(&vnode_pool, sizeof(struct vnode), 0, IPL_NONE,
150 	    PR_WAITOK, "vnodes", NULL);
151 	pool_init(&uvm_vnode_pool, sizeof(struct uvm_vnode), 0, IPL_NONE,
152 	    PR_WAITOK, "uvmvnodes", NULL);
153 	TAILQ_INIT(&vnode_hold_list);
154 	TAILQ_INIT(&vnode_free_list);
155 	TAILQ_INIT(&mountlist);
156 	/*
157 	 * Initialize the filesystem syncer.
158 	 */
159 	vn_initialize_syncerd();
160 
161 #ifdef NFSSERVER
162 	rn_init(sizeof(struct sockaddr_in));
163 #endif /* NFSSERVER */
164 }
165 
166 /*
167  * Allocate a mount point.
168  *
169  * The returned mount point is marked as busy.
170  */
171 struct mount *
172 vfs_mount_alloc(struct vnode *vp, struct vfsconf *vfsp)
173 {
174 	struct mount *mp;
175 
176 	mp = malloc(sizeof(*mp), M_MOUNT, M_WAITOK|M_ZERO);
177 	rw_init_flags(&mp->mnt_lock, "vfslock", RWL_IS_VNODE);
178 	(void)vfs_busy(mp, VB_READ|VB_NOWAIT);
179 
180 	LIST_INIT(&mp->mnt_vnodelist);
181 	mp->mnt_vnodecovered = vp;
182 
183 	atomic_inc_int(&vfsp->vfc_refcount);
184 	mp->mnt_vfc = vfsp;
185 	mp->mnt_op = vfsp->vfc_vfsops;
186 	mp->mnt_flag = vfsp->vfc_flags & MNT_VISFLAGMASK;
187 	strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
188 
189 	return (mp);
190 }
191 
192 /*
193  * Release a mount point.
194  */
195 void
196 vfs_mount_free(struct mount *mp)
197 {
198 	atomic_dec_int(&mp->mnt_vfc->vfc_refcount);
199 	free(mp, M_MOUNT, sizeof(*mp));
200 }
201 
202 /*
203  * Mark a mount point as busy. Used to synchronize access and to delay
204  * unmounting.
205  *
206  * Default behaviour is to attempt getting a READ lock and in case of an
207  * ongoing unmount, to wait for it to finish and then return failure.
208  */
209 int
210 vfs_busy(struct mount *mp, int flags)
211 {
212 	int rwflags = 0;
213 
214 	if (flags & VB_WRITE)
215 		rwflags |= RW_WRITE;
216 	else
217 		rwflags |= RW_READ;
218 
219 	if (flags & VB_WAIT)
220 		rwflags |= RW_SLEEPFAIL;
221 	else
222 		rwflags |= RW_NOSLEEP;
223 
224 #ifdef WITNESS
225 	if (flags & VB_DUPOK)
226 		rwflags |= RW_DUPOK;
227 #endif
228 
229 	if (rw_enter(&mp->mnt_lock, rwflags))
230 		return (EBUSY);
231 
232 	return (0);
233 }
234 
235 /*
236  * Free a busy file system
237  */
238 void
239 vfs_unbusy(struct mount *mp)
240 {
241 	rw_exit(&mp->mnt_lock);
242 }
243 
244 int
245 vfs_isbusy(struct mount *mp)
246 {
247 	if (RWLOCK_OWNER(&mp->mnt_lock) > 0)
248 		return (1);
249 	else
250 		return (0);
251 }
252 
253 /*
254  * Lookup a filesystem type, and if found allocate and initialize
255  * a mount structure for it.
256  *
257  * Devname is usually updated by mount(8) after booting.
258  */
259 int
260 vfs_rootmountalloc(char *fstypename, char *devname, struct mount **mpp)
261 {
262 	struct vfsconf *vfsp;
263 	struct mount *mp;
264 
265 	vfsp = vfs_byname(fstypename);
266 	if (vfsp == NULL)
267 		return (ENODEV);
268 	mp = vfs_mount_alloc(NULLVP, vfsp);
269 	mp->mnt_flag |= MNT_RDONLY;
270 	mp->mnt_stat.f_mntonname[0] = '/';
271 	copystr(devname, mp->mnt_stat.f_mntfromname, MNAMELEN, 0);
272 	copystr(devname, mp->mnt_stat.f_mntfromspec, MNAMELEN, 0);
273 	*mpp = mp;
274  	return (0);
275  }
276 
277 /*
278  * Lookup a mount point by filesystem identifier.
279  */
280 struct mount *
281 vfs_getvfs(fsid_t *fsid)
282 {
283 	struct mount *mp;
284 
285 	TAILQ_FOREACH(mp, &mountlist, mnt_list) {
286 		if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
287 		    mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
288 			return (mp);
289 		}
290 	}
291 
292 	return (NULL);
293 }
294 
295 
296 /*
297  * Get a new unique fsid
298  */
299 void
300 vfs_getnewfsid(struct mount *mp)
301 {
302 	static u_short xxxfs_mntid;
303 
304 	fsid_t tfsid;
305 	int mtype;
306 
307 	mtype = mp->mnt_vfc->vfc_typenum;
308 	mp->mnt_stat.f_fsid.val[0] = makedev(nblkdev + mtype, 0);
309 	mp->mnt_stat.f_fsid.val[1] = mtype;
310 	if (xxxfs_mntid == 0)
311 		++xxxfs_mntid;
312 	tfsid.val[0] = makedev(nblkdev + mtype, xxxfs_mntid);
313 	tfsid.val[1] = mtype;
314 	if (!TAILQ_EMPTY(&mountlist)) {
315 		while (vfs_getvfs(&tfsid)) {
316 			tfsid.val[0]++;
317 			xxxfs_mntid++;
318 		}
319 	}
320 	mp->mnt_stat.f_fsid.val[0] = tfsid.val[0];
321 }
322 
323 /*
324  * Set vnode attributes to VNOVAL
325  */
326 void
327 vattr_null(struct vattr *vap)
328 {
329 
330 	vap->va_type = VNON;
331 	/*
332 	 * Don't get fancy: u_quad_t = u_int = VNOVAL leaves the u_quad_t
333 	 * with 2^31-1 instead of 2^64-1.  Just write'm out and let
334 	 * the compiler do its job.
335 	 */
336 	vap->va_mode = VNOVAL;
337 	vap->va_nlink = VNOVAL;
338 	vap->va_uid = VNOVAL;
339 	vap->va_gid = VNOVAL;
340 	vap->va_fsid = VNOVAL;
341 	vap->va_fileid = VNOVAL;
342 	vap->va_size = VNOVAL;
343 	vap->va_blocksize = VNOVAL;
344 	vap->va_atime.tv_sec = VNOVAL;
345 	vap->va_atime.tv_nsec = VNOVAL;
346 	vap->va_mtime.tv_sec = VNOVAL;
347 	vap->va_mtime.tv_nsec = VNOVAL;
348 	vap->va_ctime.tv_sec = VNOVAL;
349 	vap->va_ctime.tv_nsec = VNOVAL;
350 	vap->va_gen = VNOVAL;
351 	vap->va_flags = VNOVAL;
352 	vap->va_rdev = VNOVAL;
353 	vap->va_bytes = VNOVAL;
354 	vap->va_filerev = VNOVAL;
355 	vap->va_vaflags = 0;
356 }
357 
358 /*
359  * Routines having to do with the management of the vnode table.
360  */
361 long numvnodes;
362 
363 /*
364  * Return the next vnode from the free list.
365  */
366 int
367 getnewvnode(enum vtagtype tag, struct mount *mp, struct vops *vops,
368     struct vnode **vpp)
369 {
370 	struct proc *p = curproc;
371 	struct freelst *listhd;
372 	static int toggle;
373 	struct vnode *vp;
374 	int s;
375 
376 	/*
377 	 * allow maxvnodes to increase if the buffer cache itself
378 	 * is big enough to justify it. (we don't shrink it ever)
379 	 */
380 	maxvnodes = maxvnodes < bcstats.numbufs ? bcstats.numbufs
381 	    : maxvnodes;
382 
383 	/*
384 	 * We must choose whether to allocate a new vnode or recycle an
385 	 * existing one. The criterion for allocating a new one is that
386 	 * the total number of vnodes is less than the number desired or
387 	 * there are no vnodes on either free list. Generally we only
388 	 * want to recycle vnodes that have no buffers associated with
389 	 * them, so we look first on the vnode_free_list. If it is empty,
390 	 * we next consider vnodes with referencing buffers on the
391 	 * vnode_hold_list. The toggle ensures that half the time we
392 	 * will use a buffer from the vnode_hold_list, and half the time
393 	 * we will allocate a new one unless the list has grown to twice
394 	 * the desired size. We are reticent to recycle vnodes from the
395 	 * vnode_hold_list because we will lose the identity of all its
396 	 * referencing buffers.
397 	 */
398 	toggle ^= 1;
399 	if (numvnodes / 2 > maxvnodes)
400 		toggle = 0;
401 
402 	s = splbio();
403 	if ((numvnodes < maxvnodes) ||
404 	    ((TAILQ_FIRST(listhd = &vnode_free_list) == NULL) &&
405 	    ((TAILQ_FIRST(listhd = &vnode_hold_list) == NULL) || toggle))) {
406 		splx(s);
407 		vp = pool_get(&vnode_pool, PR_WAITOK | PR_ZERO);
408 		vp->v_uvm = pool_get(&uvm_vnode_pool, PR_WAITOK | PR_ZERO);
409 		vp->v_uvm->u_vnode = vp;
410 		RBT_INIT(buf_rb_bufs, &vp->v_bufs_tree);
411 		cache_tree_init(&vp->v_nc_tree);
412 		TAILQ_INIT(&vp->v_cache_dst);
413 		numvnodes++;
414 	} else {
415 		TAILQ_FOREACH(vp, listhd, v_freelist) {
416 			if (VOP_ISLOCKED(vp) == 0)
417 				break;
418 		}
419 		/*
420 		 * Unless this is a bad time of the month, at most
421 		 * the first NCPUS items on the free list are
422 		 * locked, so this is close enough to being empty.
423 		 */
424 		if (vp == NULL) {
425 			splx(s);
426 			tablefull("vnode");
427 			*vpp = 0;
428 			return (ENFILE);
429 		}
430 
431 #ifdef DIAGNOSTIC
432 		if (vp->v_usecount) {
433 			vprint("free vnode", vp);
434 			panic("free vnode isn't");
435 		}
436 #endif
437 
438 		TAILQ_REMOVE(listhd, vp, v_freelist);
439 		vp->v_bioflag &= ~VBIOONFREELIST;
440 		splx(s);
441 
442 		if (vp->v_type != VBAD)
443 			vgonel(vp, p);
444 #ifdef DIAGNOSTIC
445 		if (vp->v_data) {
446 			vprint("cleaned vnode", vp);
447 			panic("cleaned vnode isn't");
448 		}
449 		s = splbio();
450 		if (vp->v_numoutput)
451 			panic("Clean vnode has pending I/O's");
452 		splx(s);
453 #endif
454 		vp->v_flag = 0;
455 		vp->v_socket = 0;
456 	}
457 	cache_purge(vp);
458 	vp->v_type = VNON;
459 	vp->v_tag = tag;
460 	vp->v_op = vops;
461 	insmntque(vp, mp);
462 	*vpp = vp;
463 	vp->v_usecount = 1;
464 	vp->v_data = 0;
465 	return (0);
466 }
467 
468 /*
469  * Move a vnode from one mount queue to another.
470  */
471 void
472 insmntque(struct vnode *vp, struct mount *mp)
473 {
474 	/*
475 	 * Delete from old mount point vnode list, if on one.
476 	 */
477 	if (vp->v_mount != NULL)
478 		LIST_REMOVE(vp, v_mntvnodes);
479 	/*
480 	 * Insert into list of vnodes for the new mount point, if available.
481 	 */
482 	if ((vp->v_mount = mp) != NULL)
483 		LIST_INSERT_HEAD(&mp->mnt_vnodelist, vp, v_mntvnodes);
484 }
485 
486 /*
487  * Create a vnode for a block device.
488  * Used for root filesystem, argdev, and swap areas.
489  * Also used for memory file system special devices.
490  */
491 int
492 bdevvp(dev_t dev, struct vnode **vpp)
493 {
494 	return (getdevvp(dev, vpp, VBLK));
495 }
496 
497 /*
498  * Create a vnode for a character device.
499  * Used for console handling.
500  */
501 int
502 cdevvp(dev_t dev, struct vnode **vpp)
503 {
504 	return (getdevvp(dev, vpp, VCHR));
505 }
506 
507 /*
508  * Create a vnode for a device.
509  * Used by bdevvp (block device) for root file system etc.,
510  * and by cdevvp (character device) for console.
511  */
512 int
513 getdevvp(dev_t dev, struct vnode **vpp, enum vtype type)
514 {
515 	struct vnode *vp;
516 	struct vnode *nvp;
517 	int error;
518 
519 	if (dev == NODEV) {
520 		*vpp = NULLVP;
521 		return (0);
522 	}
523 	error = getnewvnode(VT_NON, NULL, &spec_vops, &nvp);
524 	if (error) {
525 		*vpp = NULLVP;
526 		return (error);
527 	}
528 	vp = nvp;
529 	vp->v_type = type;
530 	if ((nvp = checkalias(vp, dev, NULL)) != 0) {
531 		vput(vp);
532 		vp = nvp;
533 	}
534 	if (vp->v_type == VCHR && cdevsw[major(vp->v_rdev)].d_type == D_TTY)
535 		vp->v_flag |= VISTTY;
536 	*vpp = vp;
537 	return (0);
538 }
539 
540 /*
541  * Check to see if the new vnode represents a special device
542  * for which we already have a vnode (either because of
543  * bdevvp() or because of a different vnode representing
544  * the same block device). If such an alias exists, deallocate
545  * the existing contents and return the aliased vnode. The
546  * caller is responsible for filling it with its new contents.
547  */
548 struct vnode *
549 checkalias(struct vnode *nvp, dev_t nvp_rdev, struct mount *mp)
550 {
551 	struct proc *p = curproc;
552 	struct vnode *vp;
553 	struct vnode **vpp;
554 
555 	if (nvp->v_type != VBLK && nvp->v_type != VCHR)
556 		return (NULLVP);
557 
558 	vpp = &speclisth[SPECHASH(nvp_rdev)];
559 loop:
560 	for (vp = *vpp; vp; vp = vp->v_specnext) {
561 		if (nvp_rdev != vp->v_rdev || nvp->v_type != vp->v_type) {
562 			continue;
563 		}
564 		/*
565 		 * Alias, but not in use, so flush it out.
566 		 */
567 		if (vp->v_usecount == 0) {
568 			vgonel(vp, p);
569 			goto loop;
570 		}
571 		if (vget(vp, LK_EXCLUSIVE)) {
572 			goto loop;
573 		}
574 		break;
575 	}
576 
577 	/*
578 	 * Common case is actually in the if statement
579 	 */
580 	if (vp == NULL || !(vp->v_tag == VT_NON && vp->v_type == VBLK)) {
581 		nvp->v_specinfo = malloc(sizeof(struct specinfo), M_VNODE,
582 			M_WAITOK);
583 		nvp->v_rdev = nvp_rdev;
584 		nvp->v_hashchain = vpp;
585 		nvp->v_specnext = *vpp;
586 		nvp->v_specmountpoint = NULL;
587 		nvp->v_speclockf = NULL;
588 		nvp->v_specbitmap = NULL;
589 		if (nvp->v_type == VCHR &&
590 		    (cdevsw[major(nvp_rdev)].d_flags & D_CLONE) &&
591 		    (minor(nvp_rdev) >> CLONE_SHIFT == 0)) {
592 			if (vp != NULLVP)
593 				nvp->v_specbitmap = vp->v_specbitmap;
594 			else
595 				nvp->v_specbitmap = malloc(CLONE_MAPSZ,
596 				    M_VNODE, M_WAITOK | M_ZERO);
597 		}
598 		*vpp = nvp;
599 		if (vp != NULLVP) {
600 			nvp->v_flag |= VALIASED;
601 			vp->v_flag |= VALIASED;
602 			vput(vp);
603 		}
604 		return (NULLVP);
605 	}
606 
607 	/*
608 	 * This code is the uncommon case. It is called in case
609 	 * we found an alias that was VT_NON && vtype of VBLK
610 	 * This means we found a block device that was created
611 	 * using bdevvp.
612 	 * An example of such a vnode is the root partition device vnode
613 	 * created in ffs_mountroot.
614 	 *
615 	 * The vnodes created by bdevvp should not be aliased (why?).
616 	 */
617 
618 	VOP_UNLOCK(vp);
619 	vclean(vp, 0, p);
620 	vp->v_op = nvp->v_op;
621 	vp->v_tag = nvp->v_tag;
622 	nvp->v_type = VNON;
623 	insmntque(vp, mp);
624 	return (vp);
625 }
626 
627 /*
628  * Grab a particular vnode from the free list, increment its
629  * reference count and lock it. If the vnode lock bit is set,
630  * the vnode is being eliminated in vgone. In that case, we
631  * cannot grab it, so the process is awakened when the
632  * transition is completed, and an error code is returned to
633  * indicate that the vnode is no longer usable, possibly
634  * having been changed to a new file system type.
635  */
636 int
637 vget(struct vnode *vp, int flags)
638 {
639 	int error, s, onfreelist;
640 
641 	/*
642 	 * If the vnode is in the process of being cleaned out for
643 	 * another use, we wait for the cleaning to finish and then
644 	 * return failure. Cleaning is determined by checking that
645 	 * the VXLOCK flag is set.
646 	 */
647 
648 	if (vp->v_flag & VXLOCK) {
649 		if (flags & LK_NOWAIT) {
650 			return (EBUSY);
651 		}
652 
653 		vp->v_flag |= VXWANT;
654 		tsleep(vp, PINOD, "vget", 0);
655 		return (ENOENT);
656 	}
657 
658 	onfreelist = vp->v_bioflag & VBIOONFREELIST;
659 	if (vp->v_usecount == 0 && onfreelist) {
660 		s = splbio();
661 		if (vp->v_holdcnt > 0)
662 			TAILQ_REMOVE(&vnode_hold_list, vp, v_freelist);
663 		else
664 			TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
665 		vp->v_bioflag &= ~VBIOONFREELIST;
666 		splx(s);
667 	}
668 
669  	vp->v_usecount++;
670 	if (flags & LK_TYPE_MASK) {
671 		if ((error = vn_lock(vp, flags)) != 0) {
672 			vp->v_usecount--;
673 			if (vp->v_usecount == 0 && onfreelist)
674 				vputonfreelist(vp);
675 		}
676 		return (error);
677 	}
678 
679 	return (0);
680 }
681 
682 
683 /* Vnode reference. */
684 void
685 vref(struct vnode *vp)
686 {
687 #ifdef DIAGNOSTIC
688 	if (vp->v_usecount == 0)
689 		panic("vref used where vget required");
690 	if (vp->v_type == VNON)
691 		panic("vref on a VNON vnode");
692 #endif
693 	vp->v_usecount++;
694 }
695 
696 void
697 vputonfreelist(struct vnode *vp)
698 {
699 	int s;
700 	struct freelst *lst;
701 
702 	s = splbio();
703 #ifdef DIAGNOSTIC
704 	if (vp->v_usecount != 0)
705 		panic("Use count is not zero!");
706 
707 	if (vp->v_bioflag & VBIOONFREELIST) {
708 		vprint("vnode already on free list: ", vp);
709 		panic("vnode already on free list");
710 	}
711 #endif
712 
713 	vp->v_bioflag |= VBIOONFREELIST;
714 
715 	if (vp->v_holdcnt > 0)
716 		lst = &vnode_hold_list;
717 	else
718 		lst = &vnode_free_list;
719 
720 	if (vp->v_type == VBAD)
721 		TAILQ_INSERT_HEAD(lst, vp, v_freelist);
722 	else
723 		TAILQ_INSERT_TAIL(lst, vp, v_freelist);
724 
725 	splx(s);
726 }
727 
728 /*
729  * vput(), just unlock and vrele()
730  */
731 void
732 vput(struct vnode *vp)
733 {
734 	struct proc *p = curproc;
735 
736 #ifdef DIAGNOSTIC
737 	if (vp == NULL)
738 		panic("vput: null vp");
739 #endif
740 
741 #ifdef DIAGNOSTIC
742 	if (vp->v_usecount == 0) {
743 		vprint("vput: bad ref count", vp);
744 		panic("vput: ref cnt");
745 	}
746 #endif
747 	vp->v_usecount--;
748 	KASSERT(vp->v_usecount > 0 || vp->v_uvcount == 0);
749 	if (vp->v_usecount > 0) {
750 		VOP_UNLOCK(vp);
751 		return;
752 	}
753 
754 #ifdef DIAGNOSTIC
755 	if (vp->v_writecount != 0) {
756 		vprint("vput: bad writecount", vp);
757 		panic("vput: v_writecount != 0");
758 	}
759 #endif
760 
761 	VOP_INACTIVE(vp, p);
762 
763 	if (vp->v_usecount == 0 && !(vp->v_bioflag & VBIOONFREELIST))
764 		vputonfreelist(vp);
765 }
766 
767 /*
768  * Vnode release - use for active VNODES.
769  * If count drops to zero, call inactive routine and return to freelist.
770  * Returns 0 if it did not sleep.
771  */
772 int
773 vrele(struct vnode *vp)
774 {
775 	struct proc *p = curproc;
776 
777 #ifdef DIAGNOSTIC
778 	if (vp == NULL)
779 		panic("vrele: null vp");
780 #endif
781 #ifdef DIAGNOSTIC
782 	if (vp->v_usecount == 0) {
783 		vprint("vrele: bad ref count", vp);
784 		panic("vrele: ref cnt");
785 	}
786 #endif
787 	vp->v_usecount--;
788 	if (vp->v_usecount > 0) {
789 		return (0);
790 	}
791 
792 #ifdef DIAGNOSTIC
793 	if (vp->v_writecount != 0) {
794 		vprint("vrele: bad writecount", vp);
795 		panic("vrele: v_writecount != 0");
796 	}
797 #endif
798 
799 	if (vn_lock(vp, LK_EXCLUSIVE)) {
800 #ifdef DIAGNOSTIC
801 		vprint("vrele: cannot lock", vp);
802 #endif
803 		return (1);
804 	}
805 
806 	VOP_INACTIVE(vp, p);
807 
808 	if (vp->v_usecount == 0 && !(vp->v_bioflag & VBIOONFREELIST))
809 		vputonfreelist(vp);
810 	return (1);
811 }
812 
813 /* Page or buffer structure gets a reference. */
814 void
815 vhold(struct vnode *vp)
816 {
817 	/*
818 	 * If it is on the freelist and the hold count is currently
819 	 * zero, move it to the hold list.
820 	 */
821 	if ((vp->v_bioflag & VBIOONFREELIST) &&
822 	    vp->v_holdcnt == 0 && vp->v_usecount == 0) {
823 		TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
824 		TAILQ_INSERT_TAIL(&vnode_hold_list, vp, v_freelist);
825 	}
826 	vp->v_holdcnt++;
827 }
828 
829 /* Lose interest in a vnode. */
830 void
831 vdrop(struct vnode *vp)
832 {
833 #ifdef DIAGNOSTIC
834 	if (vp->v_holdcnt == 0)
835 		panic("vdrop: zero holdcnt");
836 #endif
837 
838 	vp->v_holdcnt--;
839 
840 	/*
841 	 * If it is on the holdlist and the hold count drops to
842 	 * zero, move it to the free list.
843 	 */
844 	if ((vp->v_bioflag & VBIOONFREELIST) &&
845 	    vp->v_holdcnt == 0 && vp->v_usecount == 0) {
846 		TAILQ_REMOVE(&vnode_hold_list, vp, v_freelist);
847 		TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
848 	}
849 }
850 
851 /*
852  * Remove any vnodes in the vnode table belonging to mount point mp.
853  *
854  * If MNT_NOFORCE is specified, there should not be any active ones,
855  * return error if any are found (nb: this is a user error, not a
856  * system error). If MNT_FORCE is specified, detach any active vnodes
857  * that are found.
858  */
859 #ifdef DEBUG
860 int busyprt = 0;	/* print out busy vnodes */
861 struct ctldebug debug1 = { "busyprt", &busyprt };
862 #endif
863 
864 int
865 vfs_mount_foreach_vnode(struct mount *mp,
866     int (*func)(struct vnode *, void *), void *arg) {
867 	struct vnode *vp, *nvp;
868 	int error = 0;
869 
870 loop:
871 	LIST_FOREACH_SAFE(vp , &mp->mnt_vnodelist, v_mntvnodes, nvp) {
872 		if (vp->v_mount != mp)
873 			goto loop;
874 
875 		error = func(vp, arg);
876 
877 		if (error != 0)
878 			break;
879 	}
880 
881 	return (error);
882 }
883 
884 struct vflush_args {
885 	struct vnode *skipvp;
886 	int busy;
887 	int flags;
888 };
889 
890 int
891 vflush_vnode(struct vnode *vp, void *arg)
892 {
893 	struct vflush_args *va = arg;
894 	struct proc *p = curproc;
895 
896 	if (vp == va->skipvp) {
897 		return (0);
898 	}
899 
900 	if ((va->flags & SKIPSYSTEM) && (vp->v_flag & VSYSTEM)) {
901 		return (0);
902 	}
903 
904 	/*
905 	 * If WRITECLOSE is set, only flush out regular file
906 	 * vnodes open for writing.
907 	 */
908 	if ((va->flags & WRITECLOSE) &&
909 	    (vp->v_writecount == 0 || vp->v_type != VREG)) {
910 		return (0);
911 	}
912 
913 	/*
914 	 * With v_usecount == 0, all we need to do is clear
915 	 * out the vnode data structures and we are done.
916 	 */
917 	if (vp->v_usecount == 0) {
918 		vgonel(vp, p);
919 		return (0);
920 	}
921 
922 	/*
923 	 * If FORCECLOSE is set, forcibly close the vnode.
924 	 * For block or character devices, revert to an
925 	 * anonymous device. For all other files, just kill them.
926 	 */
927 	if (va->flags & FORCECLOSE) {
928 		if (vp->v_type != VBLK && vp->v_type != VCHR) {
929 			vgonel(vp, p);
930 		} else {
931 			vclean(vp, 0, p);
932 			vp->v_op = &spec_vops;
933 			insmntque(vp, NULL);
934 		}
935 		return (0);
936 	}
937 
938 	/*
939 	 * If set, this is allowed to ignore vnodes which don't
940 	 * have changes pending to disk.
941 	 * XXX Might be nice to check per-fs "inode" flags, but
942 	 * generally the filesystem is sync'd already, right?
943 	 */
944 	if ((va->flags & IGNORECLEAN) &&
945 	    LIST_EMPTY(&vp->v_dirtyblkhd))
946 		return (0);
947 
948 #ifdef DEBUG
949 	if (busyprt)
950 		vprint("vflush: busy vnode", vp);
951 #endif
952 	va->busy++;
953 	return (0);
954 }
955 
956 int
957 vflush(struct mount *mp, struct vnode *skipvp, int flags)
958 {
959 	struct vflush_args va;
960 	va.skipvp = skipvp;
961 	va.busy = 0;
962 	va.flags = flags;
963 
964 	vfs_mount_foreach_vnode(mp, vflush_vnode, &va);
965 
966 	if (va.busy)
967 		return (EBUSY);
968 	return (0);
969 }
970 
971 /*
972  * Disassociate the underlying file system from a vnode.
973  */
974 void
975 vclean(struct vnode *vp, int flags, struct proc *p)
976 {
977 	int active;
978 
979 	/*
980 	 * Check to see if the vnode is in use.
981 	 * If so we have to reference it before we clean it out
982 	 * so that its count cannot fall to zero and generate a
983 	 * race against ourselves to recycle it.
984 	 */
985 	if ((active = vp->v_usecount) != 0)
986 		vp->v_usecount++;
987 
988 	/*
989 	 * Prevent the vnode from being recycled or
990 	 * brought into use while we clean it out.
991 	 */
992 	if (vp->v_flag & VXLOCK)
993 		panic("vclean: deadlock");
994 	vp->v_flag |= VXLOCK;
995 	/*
996 	 * Even if the count is zero, the VOP_INACTIVE routine may still
997 	 * have the object locked while it cleans it out. The VOP_LOCK
998 	 * ensures that the VOP_INACTIVE routine is done with its work.
999 	 * For active vnodes, it ensures that no other activity can
1000 	 * occur while the underlying object is being cleaned out.
1001 	 */
1002 	VOP_LOCK(vp, LK_DRAIN | LK_EXCLUSIVE);
1003 
1004 	/*
1005 	 * Clean out any VM data associated with the vnode.
1006 	 */
1007 	uvm_vnp_terminate(vp);
1008 	/*
1009 	 * Clean out any buffers associated with the vnode.
1010 	 */
1011 	if (flags & DOCLOSE)
1012 		vinvalbuf(vp, V_SAVE, NOCRED, p, 0, 0);
1013 	/*
1014 	 * If purging an active vnode, it must be closed and
1015 	 * deactivated before being reclaimed. Note that the
1016 	 * VOP_INACTIVE will unlock the vnode
1017 	 */
1018 	if (active) {
1019 		if (flags & DOCLOSE)
1020 			VOP_CLOSE(vp, FNONBLOCK, NOCRED, p);
1021 		VOP_INACTIVE(vp, p);
1022 	} else {
1023 		/*
1024 		 * Any other processes trying to obtain this lock must first
1025 		 * wait for VXLOCK to clear, then call the new lock operation.
1026 		 */
1027 		VOP_UNLOCK(vp);
1028 	}
1029 
1030 	/*
1031 	 * Reclaim the vnode.
1032 	 */
1033 	if (VOP_RECLAIM(vp, p))
1034 		panic("vclean: cannot reclaim");
1035 	if (active) {
1036 		vp->v_usecount--;
1037 		if (vp->v_usecount == 0) {
1038 			if (vp->v_holdcnt > 0)
1039 				panic("vclean: not clean");
1040 			vputonfreelist(vp);
1041 		}
1042 	}
1043 	cache_purge(vp);
1044 
1045 	/*
1046 	 * Done with purge, notify sleepers of the grim news.
1047 	 */
1048 	vp->v_op = &dead_vops;
1049 	VN_KNOTE(vp, NOTE_REVOKE);
1050 	vp->v_tag = VT_NON;
1051 	vp->v_flag &= ~VXLOCK;
1052 #ifdef VFSLCKDEBUG
1053 	vp->v_flag &= ~VLOCKSWORK;
1054 #endif
1055 	if (vp->v_flag & VXWANT) {
1056 		vp->v_flag &= ~VXWANT;
1057 		wakeup(vp);
1058 	}
1059 }
1060 
1061 /*
1062  * Recycle an unused vnode to the front of the free list.
1063  */
1064 int
1065 vrecycle(struct vnode *vp, struct proc *p)
1066 {
1067 	if (vp->v_usecount == 0) {
1068 		vgonel(vp, p);
1069 		return (1);
1070 	}
1071 	return (0);
1072 }
1073 
1074 /*
1075  * Eliminate all activity associated with a vnode
1076  * in preparation for reuse.
1077  */
1078 void
1079 vgone(struct vnode *vp)
1080 {
1081 	struct proc *p = curproc;
1082 	vgonel(vp, p);
1083 }
1084 
1085 /*
1086  * vgone, with struct proc.
1087  */
1088 void
1089 vgonel(struct vnode *vp, struct proc *p)
1090 {
1091 	struct vnode *vq;
1092 	struct vnode *vx;
1093 
1094 	KASSERT(vp->v_uvcount == 0);
1095 
1096 	/*
1097 	 * If a vgone (or vclean) is already in progress,
1098 	 * wait until it is done and return.
1099 	 */
1100 	if (vp->v_flag & VXLOCK) {
1101 		vp->v_flag |= VXWANT;
1102 		tsleep(vp, PINOD, "vgone", 0);
1103 		return;
1104 	}
1105 
1106 	/*
1107 	 * Clean out the filesystem specific data.
1108 	 */
1109 	vclean(vp, DOCLOSE, p);
1110 	/*
1111 	 * Delete from old mount point vnode list, if on one.
1112 	 */
1113 	if (vp->v_mount != NULL)
1114 		insmntque(vp, NULL);
1115 	/*
1116 	 * If special device, remove it from special device alias list
1117 	 * if it is on one.
1118 	 */
1119 	if ((vp->v_type == VBLK || vp->v_type == VCHR) && vp->v_specinfo != 0) {
1120 		if ((vp->v_flag & VALIASED) == 0 && vp->v_type == VCHR &&
1121 		    (cdevsw[major(vp->v_rdev)].d_flags & D_CLONE) &&
1122 		    (minor(vp->v_rdev) >> CLONE_SHIFT == 0)) {
1123 			free(vp->v_specbitmap, M_VNODE, CLONE_MAPSZ);
1124 		}
1125 		if (*vp->v_hashchain == vp) {
1126 			*vp->v_hashchain = vp->v_specnext;
1127 		} else {
1128 			for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
1129 				if (vq->v_specnext != vp)
1130 					continue;
1131 				vq->v_specnext = vp->v_specnext;
1132 				break;
1133 			}
1134 			if (vq == NULL)
1135 				panic("missing bdev");
1136 		}
1137 		if (vp->v_flag & VALIASED) {
1138 			vx = NULL;
1139 			for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
1140 				if (vq->v_rdev != vp->v_rdev ||
1141 				    vq->v_type != vp->v_type)
1142 					continue;
1143 				if (vx)
1144 					break;
1145 				vx = vq;
1146 			}
1147 			if (vx == NULL)
1148 				panic("missing alias");
1149 			if (vq == NULL)
1150 				vx->v_flag &= ~VALIASED;
1151 			vp->v_flag &= ~VALIASED;
1152 		}
1153 		free(vp->v_specinfo, M_VNODE, sizeof(struct specinfo));
1154 		vp->v_specinfo = NULL;
1155 	}
1156 	/*
1157 	 * If it is on the freelist and not already at the head,
1158 	 * move it to the head of the list.
1159 	 */
1160 	vp->v_type = VBAD;
1161 
1162 	/*
1163 	 * Move onto the free list, unless we were called from
1164 	 * getnewvnode and we're not on any free list
1165 	 */
1166 	if (vp->v_usecount == 0 &&
1167 	    (vp->v_bioflag & VBIOONFREELIST)) {
1168 		int s;
1169 
1170 		s = splbio();
1171 
1172 		if (vp->v_holdcnt > 0)
1173 			panic("vgonel: not clean");
1174 
1175 		if (TAILQ_FIRST(&vnode_free_list) != vp) {
1176 			TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
1177 			TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
1178 		}
1179 		splx(s);
1180 	}
1181 }
1182 
1183 /*
1184  * Lookup a vnode by device number.
1185  */
1186 int
1187 vfinddev(dev_t dev, enum vtype type, struct vnode **vpp)
1188 {
1189 	struct vnode *vp;
1190 	int rc =0;
1191 
1192 	for (vp = speclisth[SPECHASH(dev)]; vp; vp = vp->v_specnext) {
1193 		if (dev != vp->v_rdev || type != vp->v_type)
1194 			continue;
1195 		*vpp = vp;
1196 		rc = 1;
1197 		break;
1198 	}
1199 	return (rc);
1200 }
1201 
1202 /*
1203  * Revoke all the vnodes corresponding to the specified minor number
1204  * range (endpoints inclusive) of the specified major.
1205  */
1206 void
1207 vdevgone(int maj, int minl, int minh, enum vtype type)
1208 {
1209 	struct vnode *vp;
1210 	int mn;
1211 
1212 	for (mn = minl; mn <= minh; mn++)
1213 		if (vfinddev(makedev(maj, mn), type, &vp))
1214 			VOP_REVOKE(vp, REVOKEALL);
1215 }
1216 
1217 /*
1218  * Calculate the total number of references to a special device.
1219  */
1220 int
1221 vcount(struct vnode *vp)
1222 {
1223 	struct vnode *vq, *vnext;
1224 	int count;
1225 
1226 loop:
1227 	if ((vp->v_flag & VALIASED) == 0)
1228 		return (vp->v_usecount);
1229 	for (count = 0, vq = *vp->v_hashchain; vq; vq = vnext) {
1230 		vnext = vq->v_specnext;
1231 		if (vq->v_rdev != vp->v_rdev || vq->v_type != vp->v_type)
1232 			continue;
1233 		/*
1234 		 * Alias, but not in use, so flush it out.
1235 		 */
1236 		if (vq->v_usecount == 0 && vq != vp) {
1237 			vgone(vq);
1238 			goto loop;
1239 		}
1240 		count += vq->v_usecount;
1241 	}
1242 	return (count);
1243 }
1244 
1245 #if defined(DEBUG) || defined(DIAGNOSTIC)
1246 /*
1247  * Print out a description of a vnode.
1248  */
1249 static char *typename[] =
1250    { "VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD" };
1251 
1252 void
1253 vprint(char *label, struct vnode *vp)
1254 {
1255 	char buf[64];
1256 
1257 	if (label != NULL)
1258 		printf("%s: ", label);
1259 	printf("%p, type %s, use %u, write %u, hold %u,",
1260 		vp, typename[vp->v_type], vp->v_usecount, vp->v_writecount,
1261 		vp->v_holdcnt);
1262 	buf[0] = '\0';
1263 	if (vp->v_flag & VROOT)
1264 		strlcat(buf, "|VROOT", sizeof buf);
1265 	if (vp->v_flag & VTEXT)
1266 		strlcat(buf, "|VTEXT", sizeof buf);
1267 	if (vp->v_flag & VSYSTEM)
1268 		strlcat(buf, "|VSYSTEM", sizeof buf);
1269 	if (vp->v_flag & VXLOCK)
1270 		strlcat(buf, "|VXLOCK", sizeof buf);
1271 	if (vp->v_flag & VXWANT)
1272 		strlcat(buf, "|VXWANT", sizeof buf);
1273 	if (vp->v_bioflag & VBIOWAIT)
1274 		strlcat(buf, "|VBIOWAIT", sizeof buf);
1275 	if (vp->v_bioflag & VBIOONFREELIST)
1276 		strlcat(buf, "|VBIOONFREELIST", sizeof buf);
1277 	if (vp->v_bioflag & VBIOONSYNCLIST)
1278 		strlcat(buf, "|VBIOONSYNCLIST", sizeof buf);
1279 	if (vp->v_flag & VALIASED)
1280 		strlcat(buf, "|VALIASED", sizeof buf);
1281 	if (buf[0] != '\0')
1282 		printf(" flags (%s)", &buf[1]);
1283 	if (vp->v_data == NULL) {
1284 		printf("\n");
1285 	} else {
1286 		printf("\n\t");
1287 		VOP_PRINT(vp);
1288 	}
1289 }
1290 #endif /* DEBUG || DIAGNOSTIC */
1291 
1292 #ifdef DEBUG
1293 /*
1294  * List all of the locked vnodes in the system.
1295  * Called when debugging the kernel.
1296  */
1297 void
1298 printlockedvnodes(void)
1299 {
1300 	struct mount *mp;
1301 	struct vnode *vp;
1302 
1303 	printf("Locked vnodes\n");
1304 
1305 	TAILQ_FOREACH(mp, &mountlist, mnt_list) {
1306 		if (vfs_busy(mp, VB_READ|VB_NOWAIT))
1307 			continue;
1308 		LIST_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) {
1309 			if (VOP_ISLOCKED(vp))
1310 				vprint(NULL, vp);
1311 		}
1312 		vfs_unbusy(mp);
1313  	}
1314 
1315 }
1316 #endif
1317 
1318 /*
1319  * Top level filesystem related information gathering.
1320  */
1321 int
1322 vfs_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
1323     size_t newlen, struct proc *p)
1324 {
1325 	struct vfsconf *vfsp, *tmpvfsp;
1326 	int ret;
1327 
1328 	/* all sysctl names at this level are at least name and field */
1329 	if (namelen < 2)
1330 		return (ENOTDIR);		/* overloaded */
1331 
1332 	if (name[0] != VFS_GENERIC) {
1333 		vfsp = vfs_bytypenum(name[0]);
1334 		if (vfsp == NULL || vfsp->vfc_vfsops->vfs_sysctl == NULL)
1335 			return (EOPNOTSUPP);
1336 
1337 		return ((*vfsp->vfc_vfsops->vfs_sysctl)(&name[1], namelen - 1,
1338 		    oldp, oldlenp, newp, newlen, p));
1339 	}
1340 
1341 	switch (name[1]) {
1342 	case VFS_MAXTYPENUM:
1343 		return (sysctl_rdint(oldp, oldlenp, newp, maxvfsconf));
1344 
1345 	case VFS_CONF:
1346 		if (namelen < 3)
1347 			return (ENOTDIR);	/* overloaded */
1348 
1349 		vfsp = vfs_bytypenum(name[2]);
1350 		if (vfsp == NULL)
1351 			return (EOPNOTSUPP);
1352 
1353 		/* Make a copy, clear out kernel pointers */
1354 		tmpvfsp = malloc(sizeof(*tmpvfsp), M_TEMP, M_WAITOK|M_ZERO);
1355 		memcpy(tmpvfsp, vfsp, sizeof(*tmpvfsp));
1356 		tmpvfsp->vfc_vfsops = NULL;
1357 
1358 		ret = sysctl_rdstruct(oldp, oldlenp, newp, tmpvfsp,
1359 		    sizeof(struct vfsconf));
1360 
1361 		free(tmpvfsp, M_TEMP, sizeof(*tmpvfsp));
1362 		return (ret);
1363 	case VFS_BCACHESTAT:	/* buffer cache statistics */
1364 		ret = sysctl_rdstruct(oldp, oldlenp, newp, &bcstats,
1365 		    sizeof(struct bcachestats));
1366 		return(ret);
1367 	}
1368 	return (EOPNOTSUPP);
1369 }
1370 
1371 /*
1372  * Check to see if a filesystem is mounted on a block device.
1373  */
1374 int
1375 vfs_mountedon(struct vnode *vp)
1376 {
1377 	struct vnode *vq;
1378 	int error = 0;
1379 
1380  	if (vp->v_specmountpoint != NULL)
1381 		return (EBUSY);
1382 	if (vp->v_flag & VALIASED) {
1383 		for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
1384 			if (vq->v_rdev != vp->v_rdev ||
1385 			    vq->v_type != vp->v_type)
1386 				continue;
1387 			if (vq->v_specmountpoint != NULL) {
1388 				error = EBUSY;
1389 				break;
1390 			}
1391  		}
1392 	}
1393 	return (error);
1394 }
1395 
1396 #ifdef NFSSERVER
1397 /*
1398  * Build hash lists of net addresses and hang them off the mount point.
1399  * Called by vfs_export() to set up the lists of export addresses.
1400  */
1401 int
1402 vfs_hang_addrlist(struct mount *mp, struct netexport *nep,
1403     struct export_args *argp)
1404 {
1405 	struct netcred *np;
1406 	struct radix_node_head *rnh;
1407 	int nplen, i;
1408 	struct radix_node *rn;
1409 	struct sockaddr *saddr, *smask = 0;
1410 	int error;
1411 
1412 	if (argp->ex_addrlen == 0) {
1413 		if (mp->mnt_flag & MNT_DEFEXPORTED)
1414 			return (EPERM);
1415 		np = &nep->ne_defexported;
1416 		/* fill in the kernel's ucred from userspace's xucred */
1417 		if ((error = crfromxucred(&np->netc_anon, &argp->ex_anon)))
1418 			return (error);
1419 		mp->mnt_flag |= MNT_DEFEXPORTED;
1420 		goto finish;
1421 	}
1422 	if (argp->ex_addrlen > MLEN || argp->ex_masklen > MLEN ||
1423 	    argp->ex_addrlen < 0 || argp->ex_masklen < 0)
1424 		return (EINVAL);
1425 	nplen = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
1426 	np = (struct netcred *)malloc(nplen, M_NETADDR, M_WAITOK|M_ZERO);
1427 	np->netc_len = nplen;
1428 	saddr = (struct sockaddr *)(np + 1);
1429 	error = copyin(argp->ex_addr, saddr, argp->ex_addrlen);
1430 	if (error)
1431 		goto out;
1432 	if (saddr->sa_len > argp->ex_addrlen)
1433 		saddr->sa_len = argp->ex_addrlen;
1434 	if (argp->ex_masklen) {
1435 		smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen);
1436 		error = copyin(argp->ex_mask, smask, argp->ex_masklen);
1437 		if (error)
1438 			goto out;
1439 		if (smask->sa_len > argp->ex_masklen)
1440 			smask->sa_len = argp->ex_masklen;
1441 	}
1442 	/* fill in the kernel's ucred from userspace's xucred */
1443 	if ((error = crfromxucred(&np->netc_anon, &argp->ex_anon)))
1444 		goto out;
1445 	i = saddr->sa_family;
1446 	switch (i) {
1447 	case AF_INET:
1448 		if ((rnh = nep->ne_rtable_inet) == NULL) {
1449 			if (!rn_inithead((void **)&nep->ne_rtable_inet,
1450 			    offsetof(struct sockaddr_in, sin_addr))) {
1451 				error = ENOBUFS;
1452 				goto out;
1453 			}
1454 			rnh = nep->ne_rtable_inet;
1455 		}
1456 		break;
1457 	default:
1458 		error = EINVAL;
1459 		goto out;
1460 	}
1461 	rn = rn_addroute(saddr, smask, rnh, np->netc_rnodes, 0);
1462 	if (rn == 0 || np != (struct netcred *)rn) { /* already exists */
1463 		error = EPERM;
1464 		goto out;
1465 	}
1466 finish:
1467 	np->netc_exflags = argp->ex_flags;
1468 	return (0);
1469 out:
1470 	free(np, M_NETADDR, np->netc_len);
1471 	return (error);
1472 }
1473 
1474 int
1475 vfs_free_netcred(struct radix_node *rn, void *w, u_int id)
1476 {
1477 	struct radix_node_head *rnh = (struct radix_node_head *)w;
1478 	struct netcred * np = (struct netcred *)rn;
1479 
1480 	rn_delete(rn->rn_key, rn->rn_mask, rnh, NULL);
1481 	free(np, M_NETADDR, np->netc_len);
1482 	return (0);
1483 }
1484 
1485 /*
1486  * Free the net address hash lists that are hanging off the mount points.
1487  */
1488 void
1489 vfs_free_addrlist(struct netexport *nep)
1490 {
1491 	struct radix_node_head *rnh;
1492 
1493 	if ((rnh = nep->ne_rtable_inet) != NULL) {
1494 		rn_walktree(rnh, vfs_free_netcred, rnh);
1495 		free(rnh, M_RTABLE, sizeof(*rnh));
1496 		nep->ne_rtable_inet = NULL;
1497 	}
1498 }
1499 #endif /* NFSSERVER */
1500 
1501 int
1502 vfs_export(struct mount *mp, struct netexport *nep, struct export_args *argp)
1503 {
1504 #ifdef NFSSERVER
1505 	int error;
1506 
1507 	if (argp->ex_flags & MNT_DELEXPORT) {
1508 		vfs_free_addrlist(nep);
1509 		mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
1510 	}
1511 	if (argp->ex_flags & MNT_EXPORTED) {
1512 		if ((error = vfs_hang_addrlist(mp, nep, argp)) != 0)
1513 			return (error);
1514 		mp->mnt_flag |= MNT_EXPORTED;
1515 	}
1516 	return (0);
1517 #else
1518 	return (ENOTSUP);
1519 #endif /* NFSSERVER */
1520 }
1521 
1522 struct netcred *
1523 vfs_export_lookup(struct mount *mp, struct netexport *nep, struct mbuf *nam)
1524 {
1525 #ifdef NFSSERVER
1526 	struct netcred *np;
1527 	struct radix_node_head *rnh;
1528 	struct sockaddr *saddr;
1529 
1530 	np = NULL;
1531 	if (mp->mnt_flag & MNT_EXPORTED) {
1532 		/*
1533 		 * Lookup in the export list first.
1534 		 */
1535 		if (nam != NULL) {
1536 			saddr = mtod(nam, struct sockaddr *);
1537 			switch(saddr->sa_family) {
1538 			case AF_INET:
1539 				rnh = nep->ne_rtable_inet;
1540 				break;
1541 			default:
1542 				rnh = NULL;
1543 				break;
1544 			}
1545 			if (rnh != NULL)
1546 				np = (struct netcred *)rn_match(saddr, rnh);
1547 		}
1548 		/*
1549 		 * If no address match, use the default if it exists.
1550 		 */
1551 		if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
1552 			np = &nep->ne_defexported;
1553 	}
1554 	return (np);
1555 #else
1556 	return (NULL);
1557 #endif /* NFSSERVER */
1558 }
1559 
1560 /*
1561  * Do the usual access checking.
1562  * file_mode, uid and gid are from the vnode in question,
1563  * while acc_mode and cred are from the VOP_ACCESS parameter list
1564  */
1565 int
1566 vaccess(enum vtype type, mode_t file_mode, uid_t uid, gid_t gid,
1567     mode_t acc_mode, struct ucred *cred)
1568 {
1569 	mode_t mask;
1570 
1571 	/* User id 0 always gets read/write access. */
1572 	if (cred->cr_uid == 0) {
1573 		/* For VEXEC, at least one of the execute bits must be set. */
1574 		if ((acc_mode & VEXEC) && type != VDIR &&
1575 		    (file_mode & (S_IXUSR|S_IXGRP|S_IXOTH)) == 0)
1576 			return EACCES;
1577 		return 0;
1578 	}
1579 
1580 	mask = 0;
1581 
1582 	/* Otherwise, check the owner. */
1583 	if (cred->cr_uid == uid) {
1584 		if (acc_mode & VEXEC)
1585 			mask |= S_IXUSR;
1586 		if (acc_mode & VREAD)
1587 			mask |= S_IRUSR;
1588 		if (acc_mode & VWRITE)
1589 			mask |= S_IWUSR;
1590 		return (file_mode & mask) == mask ? 0 : EACCES;
1591 	}
1592 
1593 	/* Otherwise, check the groups. */
1594 	if (groupmember(gid, cred)) {
1595 		if (acc_mode & VEXEC)
1596 			mask |= S_IXGRP;
1597 		if (acc_mode & VREAD)
1598 			mask |= S_IRGRP;
1599 		if (acc_mode & VWRITE)
1600 			mask |= S_IWGRP;
1601 		return (file_mode & mask) == mask ? 0 : EACCES;
1602 	}
1603 
1604 	/* Otherwise, check everyone else. */
1605 	if (acc_mode & VEXEC)
1606 		mask |= S_IXOTH;
1607 	if (acc_mode & VREAD)
1608 		mask |= S_IROTH;
1609 	if (acc_mode & VWRITE)
1610 		mask |= S_IWOTH;
1611 	return (file_mode & mask) == mask ? 0 : EACCES;
1612 }
1613 
1614 int
1615 vnoperm(struct vnode *vp)
1616 {
1617 	if (vp->v_flag & VROOT || vp->v_mount == NULL)
1618 		return 0;
1619 
1620 	return (vp->v_mount->mnt_flag & MNT_NOPERM);
1621 }
1622 
1623 struct rwlock vfs_stall_lock = RWLOCK_INITIALIZER("vfs_stall");
1624 
1625 int
1626 vfs_stall(struct proc *p, int stall)
1627 {
1628 	struct mount *mp;
1629 	int allerror = 0, error;
1630 
1631 	if (stall)
1632 		rw_enter_write(&vfs_stall_lock);
1633 
1634 	/*
1635 	 * The loop variable mp is protected by vfs_busy() so that it cannot
1636 	 * be unmounted while VFS_SYNC() sleeps.  Traverse forward to keep the
1637 	 * lock order consistent with dounmount().
1638 	 */
1639 	TAILQ_FOREACH(mp, &mountlist, mnt_list) {
1640 		if (stall) {
1641 			error = vfs_busy(mp, VB_WRITE|VB_WAIT|VB_DUPOK);
1642 			if (error) {
1643 				printf("%s: busy\n", mp->mnt_stat.f_mntonname);
1644 				allerror = error;
1645 				continue;
1646 			}
1647 			uvm_vnp_sync(mp);
1648 			error = VFS_SYNC(mp, MNT_WAIT, stall, p->p_ucred, p);
1649 			if (error) {
1650 				printf("%s: failed to sync\n", mp->mnt_stat.f_mntonname);
1651 				vfs_unbusy(mp);
1652 				allerror = error;
1653 				continue;
1654 			}
1655 			mp->mnt_flag |= MNT_STALLED;
1656 		} else {
1657 			if (mp->mnt_flag & MNT_STALLED) {
1658 				vfs_unbusy(mp);
1659 				mp->mnt_flag &= ~MNT_STALLED;
1660 			}
1661 		}
1662 	}
1663 
1664 	if (!stall)
1665 		rw_exit_write(&vfs_stall_lock);
1666 
1667 	return (allerror);
1668 }
1669 
1670 void
1671 vfs_stall_barrier(void)
1672 {
1673 	rw_enter_read(&vfs_stall_lock);
1674 	rw_exit_read(&vfs_stall_lock);
1675 }
1676 
1677 /*
1678  * Unmount all file systems.
1679  * We traverse the list in reverse order under the assumption that doing so
1680  * will avoid needing to worry about dependencies.
1681  */
1682 void
1683 vfs_unmountall(void)
1684 {
1685 	struct mount *mp, *nmp;
1686 	int allerror, error, again = 1;
1687 
1688  retry:
1689 	allerror = 0;
1690 	TAILQ_FOREACH_REVERSE_SAFE(mp, &mountlist, mntlist, mnt_list, nmp) {
1691 		if (vfs_busy(mp, VB_WRITE|VB_NOWAIT))
1692 			continue;
1693 		/* XXX Here is a race, the next pointer is not locked. */
1694 		if ((error = dounmount(mp, MNT_FORCE, curproc)) != 0) {
1695 			printf("unmount of %s failed with error %d\n",
1696 			    mp->mnt_stat.f_mntonname, error);
1697 			allerror = 1;
1698 		}
1699 	}
1700 
1701 	if (allerror) {
1702 		printf("WARNING: some file systems would not unmount\n");
1703 		if (again) {
1704 			printf("retrying\n");
1705 			again = 0;
1706 			goto retry;
1707 		}
1708 	}
1709 }
1710 
1711 /*
1712  * Sync and unmount file systems before shutting down.
1713  */
1714 void
1715 vfs_shutdown(struct proc *p)
1716 {
1717 #ifdef ACCOUNTING
1718 	acct_shutdown();
1719 #endif
1720 
1721 	printf("syncing disks...");
1722 
1723 	if (panicstr == 0) {
1724 		/* Sync before unmount, in case we hang on something. */
1725 		sys_sync(p, NULL, NULL);
1726 		vfs_unmountall();
1727 	}
1728 
1729 #if NSOFTRAID > 0
1730 	sr_quiesce();
1731 #endif
1732 
1733 	if (vfs_syncwait(p, 1))
1734 		printf(" giving up\n");
1735 	else
1736 		printf(" done\n");
1737 }
1738 
1739 /*
1740  * perform sync() operation and wait for buffers to flush.
1741  */
1742 int
1743 vfs_syncwait(struct proc *p, int verbose)
1744 {
1745 	struct buf *bp;
1746 	int iter, nbusy, dcount, s;
1747 #ifdef MULTIPROCESSOR
1748 	int hold_count;
1749 #endif
1750 
1751 	sys_sync(p, NULL, NULL);
1752 
1753 	/* Wait for sync to finish. */
1754 	dcount = 10000;
1755 	for (iter = 0; iter < 20; iter++) {
1756 		nbusy = 0;
1757 		LIST_FOREACH(bp, &bufhead, b_list) {
1758 			if ((bp->b_flags & (B_BUSY|B_INVAL|B_READ)) == B_BUSY)
1759 				nbusy++;
1760 			/*
1761 			 * With soft updates, some buffers that are
1762 			 * written will be remarked as dirty until other
1763 			 * buffers are written.
1764 			 */
1765 			if (bp->b_flags & B_DELWRI) {
1766 				s = splbio();
1767 				bremfree(bp);
1768 				buf_acquire(bp);
1769 				splx(s);
1770 				nbusy++;
1771 				bawrite(bp);
1772 				if (dcount-- <= 0) {
1773 					if (verbose)
1774 						printf("softdep ");
1775 					return 1;
1776 				}
1777 			}
1778 		}
1779 		if (nbusy == 0)
1780 			break;
1781 		if (verbose)
1782 			printf("%d ", nbusy);
1783 #ifdef MULTIPROCESSOR
1784 		if (_kernel_lock_held())
1785 			hold_count = __mp_release_all(&kernel_lock);
1786 		else
1787 			hold_count = 0;
1788 #endif
1789 		DELAY(40000 * iter);
1790 #ifdef MULTIPROCESSOR
1791 		if (hold_count)
1792 			__mp_acquire_count(&kernel_lock, hold_count);
1793 #endif
1794 	}
1795 
1796 	return nbusy;
1797 }
1798 
1799 /*
1800  * posix file system related system variables.
1801  */
1802 int
1803 fs_posix_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
1804     void *newp, size_t newlen, struct proc *p)
1805 {
1806 	/* all sysctl names at this level are terminal */
1807 	if (namelen != 1)
1808 		return (ENOTDIR);
1809 
1810 	switch (name[0]) {
1811 	case FS_POSIX_SETUID:
1812 		if (newp && securelevel > 0)
1813 			return (EPERM);
1814 		return(sysctl_int(oldp, oldlenp, newp, newlen, &suid_clear));
1815 	default:
1816 		return (EOPNOTSUPP);
1817 	}
1818 	/* NOTREACHED */
1819 }
1820 
1821 /*
1822  * file system related system variables.
1823  */
1824 int
1825 fs_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
1826     size_t newlen, struct proc *p)
1827 {
1828 	sysctlfn *fn;
1829 
1830 	switch (name[0]) {
1831 	case FS_POSIX:
1832 		fn = fs_posix_sysctl;
1833 		break;
1834 	default:
1835 		return (EOPNOTSUPP);
1836 	}
1837 	return (*fn)(name + 1, namelen - 1, oldp, oldlenp, newp, newlen, p);
1838 }
1839 
1840 
1841 /*
1842  * Routines dealing with vnodes and buffers
1843  */
1844 
1845 /*
1846  * Wait for all outstanding I/Os to complete
1847  *
1848  * Manipulates v_numoutput. Must be called at splbio()
1849  */
1850 int
1851 vwaitforio(struct vnode *vp, int slpflag, char *wmesg, int timeo)
1852 {
1853 	int error = 0;
1854 
1855 	splassert(IPL_BIO);
1856 
1857 	while (vp->v_numoutput) {
1858 		vp->v_bioflag |= VBIOWAIT;
1859 		error = tsleep(&vp->v_numoutput,
1860 		    slpflag | (PRIBIO + 1), wmesg, timeo);
1861 		if (error)
1862 			break;
1863 	}
1864 
1865 	return (error);
1866 }
1867 
1868 /*
1869  * Update outstanding I/O count and do wakeup if requested.
1870  *
1871  * Manipulates v_numoutput. Must be called at splbio()
1872  */
1873 void
1874 vwakeup(struct vnode *vp)
1875 {
1876 	splassert(IPL_BIO);
1877 
1878 	if (vp != NULL) {
1879 		if (vp->v_numoutput-- == 0)
1880 			panic("vwakeup: neg numoutput");
1881 		if ((vp->v_bioflag & VBIOWAIT) && vp->v_numoutput == 0) {
1882 			vp->v_bioflag &= ~VBIOWAIT;
1883 			wakeup(&vp->v_numoutput);
1884 		}
1885 	}
1886 }
1887 
1888 /*
1889  * Flush out and invalidate all buffers associated with a vnode.
1890  * Called with the underlying object locked.
1891  */
1892 int
1893 vinvalbuf(struct vnode *vp, int flags, struct ucred *cred, struct proc *p,
1894     int slpflag, int slptimeo)
1895 {
1896 	struct buf *bp;
1897 	struct buf *nbp, *blist;
1898 	int s, error;
1899 
1900 #ifdef VFSLCKDEBUG
1901 	if ((vp->v_flag & VLOCKSWORK) && !VOP_ISLOCKED(vp))
1902 		panic("%s: vp isn't locked, vp %p", __func__, vp);
1903 #endif
1904 
1905 	if (flags & V_SAVE) {
1906 		s = splbio();
1907 		vwaitforio(vp, 0, "vinvalbuf", 0);
1908 		if (!LIST_EMPTY(&vp->v_dirtyblkhd)) {
1909 			splx(s);
1910 			if ((error = VOP_FSYNC(vp, cred, MNT_WAIT, p)) != 0)
1911 				return (error);
1912 			s = splbio();
1913 			if (vp->v_numoutput > 0 ||
1914 			    !LIST_EMPTY(&vp->v_dirtyblkhd))
1915 				panic("%s: dirty bufs, vp %p", __func__, vp);
1916 		}
1917 		splx(s);
1918 	}
1919 loop:
1920 	s = splbio();
1921 	for (;;) {
1922 		if ((blist = LIST_FIRST(&vp->v_cleanblkhd)) &&
1923 		    (flags & V_SAVEMETA))
1924 			while (blist && blist->b_lblkno < 0)
1925 				blist = LIST_NEXT(blist, b_vnbufs);
1926 		if (blist == NULL &&
1927 		    (blist = LIST_FIRST(&vp->v_dirtyblkhd)) &&
1928 		    (flags & V_SAVEMETA))
1929 			while (blist && blist->b_lblkno < 0)
1930 				blist = LIST_NEXT(blist, b_vnbufs);
1931 		if (!blist)
1932 			break;
1933 
1934 		for (bp = blist; bp; bp = nbp) {
1935 			nbp = LIST_NEXT(bp, b_vnbufs);
1936 			if (flags & V_SAVEMETA && bp->b_lblkno < 0)
1937 				continue;
1938 			if (bp->b_flags & B_BUSY) {
1939 				bp->b_flags |= B_WANTED;
1940 				error = tsleep(bp, slpflag | (PRIBIO + 1),
1941 				    "vinvalbuf", slptimeo);
1942 				if (error) {
1943 					splx(s);
1944 					return (error);
1945 				}
1946 				break;
1947 			}
1948 			bremfree(bp);
1949 			/*
1950 			 * XXX Since there are no node locks for NFS, I believe
1951 			 * there is a slight chance that a delayed write will
1952 			 * occur while sleeping just above, so check for it.
1953 			 */
1954 			if ((bp->b_flags & B_DELWRI) && (flags & V_SAVE)) {
1955 				buf_acquire(bp);
1956 				splx(s);
1957 				(void) VOP_BWRITE(bp);
1958 				goto loop;
1959 			}
1960 			buf_acquire_nomap(bp);
1961 			bp->b_flags |= B_INVAL;
1962 			brelse(bp);
1963 		}
1964 	}
1965 	if (!(flags & V_SAVEMETA) &&
1966 	    (!LIST_EMPTY(&vp->v_dirtyblkhd) || !LIST_EMPTY(&vp->v_cleanblkhd)))
1967 		panic("%s: flush failed, vp %p", __func__, vp);
1968 	splx(s);
1969 	return (0);
1970 }
1971 
1972 void
1973 vflushbuf(struct vnode *vp, int sync)
1974 {
1975 	struct buf *bp, *nbp;
1976 	int s;
1977 
1978 loop:
1979 	s = splbio();
1980 	LIST_FOREACH_SAFE(bp, &vp->v_dirtyblkhd, b_vnbufs, nbp) {
1981 		if ((bp->b_flags & B_BUSY))
1982 			continue;
1983 		if ((bp->b_flags & B_DELWRI) == 0)
1984 			panic("vflushbuf: not dirty");
1985 		bremfree(bp);
1986 		buf_acquire(bp);
1987 		splx(s);
1988 		/*
1989 		 * Wait for I/O associated with indirect blocks to complete,
1990 		 * since there is no way to quickly wait for them below.
1991 		 */
1992 		if (bp->b_vp == vp || sync == 0)
1993 			(void) bawrite(bp);
1994 		else
1995 			(void) bwrite(bp);
1996 		goto loop;
1997 	}
1998 	if (sync == 0) {
1999 		splx(s);
2000 		return;
2001 	}
2002 	vwaitforio(vp, 0, "vflushbuf", 0);
2003 	if (!LIST_EMPTY(&vp->v_dirtyblkhd)) {
2004 		splx(s);
2005 #ifdef DIAGNOSTIC
2006 		vprint("vflushbuf: dirty", vp);
2007 #endif
2008 		goto loop;
2009 	}
2010 	splx(s);
2011 }
2012 
2013 /*
2014  * Associate a buffer with a vnode.
2015  *
2016  * Manipulates buffer vnode queues. Must be called at splbio().
2017  */
2018 void
2019 bgetvp(struct vnode *vp, struct buf *bp)
2020 {
2021 	splassert(IPL_BIO);
2022 
2023 
2024 	if (bp->b_vp)
2025 		panic("bgetvp: not free");
2026 	vhold(vp);
2027 	bp->b_vp = vp;
2028 	if (vp->v_type == VBLK || vp->v_type == VCHR)
2029 		bp->b_dev = vp->v_rdev;
2030 	else
2031 		bp->b_dev = NODEV;
2032 	/*
2033 	 * Insert onto list for new vnode.
2034 	 */
2035 	bufinsvn(bp, &vp->v_cleanblkhd);
2036 }
2037 
2038 /*
2039  * Disassociate a buffer from a vnode.
2040  *
2041  * Manipulates vnode buffer queues. Must be called at splbio().
2042  */
2043 void
2044 brelvp(struct buf *bp)
2045 {
2046 	struct vnode *vp;
2047 
2048 	splassert(IPL_BIO);
2049 
2050 	if ((vp = bp->b_vp) == (struct vnode *) 0)
2051 		panic("brelvp: NULL");
2052 	/*
2053 	 * Delete from old vnode list, if on one.
2054 	 */
2055 	if (LIST_NEXT(bp, b_vnbufs) != NOLIST)
2056 		bufremvn(bp);
2057 	if ((vp->v_bioflag & VBIOONSYNCLIST) &&
2058 	    LIST_EMPTY(&vp->v_dirtyblkhd)) {
2059 		vp->v_bioflag &= ~VBIOONSYNCLIST;
2060 		LIST_REMOVE(vp, v_synclist);
2061 	}
2062 	bp->b_vp = NULL;
2063 
2064 	vdrop(vp);
2065 }
2066 
2067 /*
2068  * Replaces the current vnode associated with the buffer, if any,
2069  * with a new vnode.
2070  *
2071  * If an output I/O is pending on the buffer, the old vnode
2072  * I/O count is adjusted.
2073  *
2074  * Ignores vnode buffer queues. Must be called at splbio().
2075  */
2076 void
2077 buf_replacevnode(struct buf *bp, struct vnode *newvp)
2078 {
2079 	struct vnode *oldvp = bp->b_vp;
2080 
2081 	splassert(IPL_BIO);
2082 
2083 	if (oldvp)
2084 		brelvp(bp);
2085 
2086 	if ((bp->b_flags & (B_READ | B_DONE)) == 0) {
2087 		newvp->v_numoutput++;	/* put it on swapdev */
2088 		vwakeup(oldvp);
2089 	}
2090 
2091 	bgetvp(newvp, bp);
2092 	bufremvn(bp);
2093 }
2094 
2095 /*
2096  * Used to assign buffers to the appropriate clean or dirty list on
2097  * the vnode and to add newly dirty vnodes to the appropriate
2098  * filesystem syncer list.
2099  *
2100  * Manipulates vnode buffer queues. Must be called at splbio().
2101  */
2102 void
2103 reassignbuf(struct buf *bp)
2104 {
2105 	struct buflists *listheadp;
2106 	int delay;
2107 	struct vnode *vp = bp->b_vp;
2108 
2109 	splassert(IPL_BIO);
2110 
2111 	/*
2112 	 * Delete from old vnode list, if on one.
2113 	 */
2114 	if (LIST_NEXT(bp, b_vnbufs) != NOLIST)
2115 		bufremvn(bp);
2116 
2117 	/*
2118 	 * If dirty, put on list of dirty buffers;
2119 	 * otherwise insert onto list of clean buffers.
2120 	 */
2121 	if ((bp->b_flags & B_DELWRI) == 0) {
2122 		listheadp = &vp->v_cleanblkhd;
2123 		if ((vp->v_bioflag & VBIOONSYNCLIST) &&
2124 		    LIST_EMPTY(&vp->v_dirtyblkhd)) {
2125 			vp->v_bioflag &= ~VBIOONSYNCLIST;
2126 			LIST_REMOVE(vp, v_synclist);
2127 		}
2128 	} else {
2129 		listheadp = &vp->v_dirtyblkhd;
2130 		if ((vp->v_bioflag & VBIOONSYNCLIST) == 0) {
2131 			switch (vp->v_type) {
2132 			case VDIR:
2133 				delay = syncdelay / 2;
2134 				break;
2135 			case VBLK:
2136 				if (vp->v_specmountpoint != NULL) {
2137 					delay = syncdelay / 3;
2138 					break;
2139 				}
2140 				/* FALLTHROUGH */
2141 			default:
2142 				delay = syncdelay;
2143 			}
2144 			vn_syncer_add_to_worklist(vp, delay);
2145 		}
2146 	}
2147 	bufinsvn(bp, listheadp);
2148 }
2149 
2150 /*
2151  * Check if vnode represents a disk device
2152  */
2153 int
2154 vn_isdisk(struct vnode *vp, int *errp)
2155 {
2156 	if (vp->v_type != VBLK && vp->v_type != VCHR)
2157 		return (0);
2158 
2159 	return (1);
2160 }
2161 
2162 #ifdef DDB
2163 #include <machine/db_machdep.h>
2164 #include <ddb/db_interface.h>
2165 
2166 void
2167 vfs_buf_print(void *b, int full,
2168     int (*pr)(const char *, ...) __attribute__((__format__(__kprintf__,1,2))))
2169 {
2170 	struct buf *bp = b;
2171 
2172 	(*pr)("  vp %p lblkno 0x%llx blkno 0x%llx dev 0x%x\n"
2173 	      "  proc %p error %d flags %lb\n",
2174 	    bp->b_vp, (int64_t)bp->b_lblkno, (int64_t)bp->b_blkno, bp->b_dev,
2175 	    bp->b_proc, bp->b_error, bp->b_flags, B_BITS);
2176 
2177 	(*pr)("  bufsize 0x%lx bcount 0x%lx resid 0x%lx\n"
2178 	      "  data %p saveaddr %p dep %p iodone %p\n",
2179 	    bp->b_bufsize, bp->b_bcount, (long)bp->b_resid,
2180 	    bp->b_data, bp->b_saveaddr,
2181 	    LIST_FIRST(&bp->b_dep), bp->b_iodone);
2182 
2183 	(*pr)("  dirty {off 0x%x end 0x%x} valid {off 0x%x end 0x%x}\n",
2184 	    bp->b_dirtyoff, bp->b_dirtyend, bp->b_validoff, bp->b_validend);
2185 
2186 #ifdef FFS_SOFTUPDATES
2187 	if (full)
2188 		softdep_print(bp, full, pr);
2189 #endif
2190 }
2191 
2192 const char *vtypes[] = { VTYPE_NAMES };
2193 const char *vtags[] = { VTAG_NAMES };
2194 
2195 void
2196 vfs_vnode_print(void *v, int full,
2197     int (*pr)(const char *, ...) __attribute__((__format__(__kprintf__,1,2))))
2198 {
2199 	struct vnode *vp = v;
2200 
2201 	(*pr)("tag %s(%d) type %s(%d) mount %p typedata %p\n",
2202 	      (u_int)vp->v_tag >= nitems(vtags)? "<unk>":vtags[vp->v_tag],
2203 	      vp->v_tag,
2204 	      (u_int)vp->v_type >= nitems(vtypes)? "<unk>":vtypes[vp->v_type],
2205 	      vp->v_type, vp->v_mount, vp->v_mountedhere);
2206 
2207 	(*pr)("data %p usecount %d writecount %d holdcnt %d numoutput %d\n",
2208 	      vp->v_data, vp->v_usecount, vp->v_writecount,
2209 	      vp->v_holdcnt, vp->v_numoutput);
2210 
2211 	/* uvm_object_printit(&vp->v_uobj, full, pr); */
2212 
2213 	if (full) {
2214 		struct buf *bp;
2215 
2216 		(*pr)("clean bufs:\n");
2217 		LIST_FOREACH(bp, &vp->v_cleanblkhd, b_vnbufs) {
2218 			(*pr)(" bp %p\n", bp);
2219 			vfs_buf_print(bp, full, pr);
2220 		}
2221 
2222 		(*pr)("dirty bufs:\n");
2223 		LIST_FOREACH(bp, &vp->v_dirtyblkhd, b_vnbufs) {
2224 			(*pr)(" bp %p\n", bp);
2225 			vfs_buf_print(bp, full, pr);
2226 		}
2227 	}
2228 }
2229 
2230 void
2231 vfs_mount_print(struct mount *mp, int full,
2232     int (*pr)(const char *, ...) __attribute__((__format__(__kprintf__,1,2))))
2233 {
2234 	struct vfsconf *vfc = mp->mnt_vfc;
2235 	struct vnode *vp;
2236 	int cnt;
2237 
2238 	(*pr)("flags %b\nvnodecovered %p syncer %p data %p\n",
2239 	    mp->mnt_flag, MNT_BITS,
2240 	    mp->mnt_vnodecovered, mp->mnt_syncer, mp->mnt_data);
2241 
2242 	(*pr)("vfsconf: ops %p name \"%s\" num %d ref %u flags 0x%x\n",
2243             vfc->vfc_vfsops, vfc->vfc_name, vfc->vfc_typenum,
2244 	    vfc->vfc_refcount, vfc->vfc_flags);
2245 
2246 	(*pr)("statvfs cache: bsize %x iosize %x\nblocks %llu free %llu avail %lld\n",
2247 	    mp->mnt_stat.f_bsize, mp->mnt_stat.f_iosize, mp->mnt_stat.f_blocks,
2248 	    mp->mnt_stat.f_bfree, mp->mnt_stat.f_bavail);
2249 
2250 	(*pr)("  files %llu ffiles %llu favail %lld\n", mp->mnt_stat.f_files,
2251 	    mp->mnt_stat.f_ffree, mp->mnt_stat.f_favail);
2252 
2253 	(*pr)("  f_fsidx {0x%x, 0x%x} owner %u ctime 0x%llx\n",
2254 	    mp->mnt_stat.f_fsid.val[0], mp->mnt_stat.f_fsid.val[1],
2255 	    mp->mnt_stat.f_owner, mp->mnt_stat.f_ctime);
2256 
2257  	(*pr)("  syncwrites %llu asyncwrites = %llu\n",
2258 	    mp->mnt_stat.f_syncwrites, mp->mnt_stat.f_asyncwrites);
2259 
2260  	(*pr)("  syncreads %llu asyncreads = %llu\n",
2261 	    mp->mnt_stat.f_syncreads, mp->mnt_stat.f_asyncreads);
2262 
2263 	(*pr)("  fstype \"%s\" mnton \"%s\" mntfrom \"%s\" mntspec \"%s\"\n",
2264 	    mp->mnt_stat.f_fstypename, mp->mnt_stat.f_mntonname,
2265 	    mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntfromspec);
2266 
2267 	(*pr)("locked vnodes:");
2268 	/* XXX would take mountlist lock, except ddb has no context */
2269 	cnt = 0;
2270 	LIST_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) {
2271 		if (VOP_ISLOCKED(vp)) {
2272 			if (cnt == 0)
2273 				(*pr)("\n  %p", vp);
2274 			else if ((cnt % (72 / (sizeof(void *) * 2 + 4))) == 0)
2275 				(*pr)(",\n  %p", vp);
2276 			else
2277 				(*pr)(", %p", vp);
2278 			cnt++;
2279 		}
2280 	}
2281 	(*pr)("\n");
2282 
2283 	if (full) {
2284 		(*pr)("all vnodes:");
2285 		/* XXX would take mountlist lock, except ddb has no context */
2286 		cnt = 0;
2287 		LIST_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) {
2288 			if (cnt == 0)
2289 				(*pr)("\n  %p", vp);
2290 			else if ((cnt % (72 / (sizeof(void *) * 2 + 4))) == 0)
2291 				(*pr)(",\n  %p", vp);
2292 			else
2293 				(*pr)(", %p", vp);
2294 			cnt++;
2295 		}
2296 		(*pr)("\n");
2297 	}
2298 }
2299 #endif /* DDB */
2300 
2301 void
2302 copy_statfs_info(struct statfs *sbp, const struct mount *mp)
2303 {
2304 	const struct statfs *mbp;
2305 
2306 	strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN);
2307 
2308 	if (sbp == (mbp = &mp->mnt_stat))
2309 		return;
2310 
2311 	sbp->f_fsid = mbp->f_fsid;
2312 	sbp->f_owner = mbp->f_owner;
2313 	sbp->f_flags = mbp->f_flags;
2314 	sbp->f_syncwrites = mbp->f_syncwrites;
2315 	sbp->f_asyncwrites = mbp->f_asyncwrites;
2316 	sbp->f_syncreads = mbp->f_syncreads;
2317 	sbp->f_asyncreads = mbp->f_asyncreads;
2318 	sbp->f_namemax = mbp->f_namemax;
2319 	memcpy(sbp->f_mntonname, mp->mnt_stat.f_mntonname, MNAMELEN);
2320 	memcpy(sbp->f_mntfromname, mp->mnt_stat.f_mntfromname, MNAMELEN);
2321 	memcpy(sbp->f_mntfromspec, mp->mnt_stat.f_mntfromspec, MNAMELEN);
2322 	memcpy(&sbp->mount_info, &mp->mnt_stat.mount_info,
2323 	    sizeof(union mount_info));
2324 }
2325