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