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