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