xref: /original-bsd/sys/kern/vfs_subr.c (revision f17085de)
1 /*
2  * Copyright (c) 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  * (c) UNIX System Laboratories, Inc.
5  * All or some portions of this file are derived from material licensed
6  * to the University of California by American Telephone and Telegraph
7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8  * the permission of UNIX System Laboratories, Inc.
9  *
10  * %sccs.include.redist.c%
11  *
12  *	@(#)vfs_subr.c	8.27 (Berkeley) 05/18/95
13  */
14 
15 /*
16  * External virtual filesystem routines
17  */
18 
19 #include <sys/param.h>
20 #include <sys/systm.h>
21 #include <sys/proc.h>
22 #include <sys/mount.h>
23 #include <sys/time.h>
24 #include <sys/vnode.h>
25 #include <sys/stat.h>
26 #include <sys/namei.h>
27 #include <sys/ucred.h>
28 #include <sys/buf.h>
29 #include <sys/errno.h>
30 #include <sys/malloc.h>
31 #include <sys/domain.h>
32 #include <sys/mbuf.h>
33 
34 #include <vm/vm.h>
35 #include <sys/sysctl.h>
36 
37 #include <miscfs/specfs/specdev.h>
38 
39 enum vtype iftovt_tab[16] = {
40 	VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
41 	VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
42 };
43 int	vttoif_tab[9] = {
44 	0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
45 	S_IFSOCK, S_IFIFO, S_IFMT,
46 };
47 
48 /*
49  * Insq/Remq for the vnode usage lists.
50  */
51 #define	bufinsvn(bp, dp)	LIST_INSERT_HEAD(dp, bp, b_vnbufs)
52 #define	bufremvn(bp) {							\
53 	LIST_REMOVE(bp, b_vnbufs);					\
54 	(bp)->b_vnbufs.le_next = NOLIST;				\
55 }
56 TAILQ_HEAD(freelst, vnode) vnode_free_list;	/* vnode free list */
57 struct mntlist mountlist;			/* mounted filesystem list */
58 static struct simplelock mntid_slock;
59 struct simplelock mntvnode_slock;
60 static struct simplelock spechash_slock;
61 static struct simplelock vnode_free_list_slock;
62 
63 /*
64  * Initialize the vnode management data structures.
65  */
66 void
67 vntblinit()
68 {
69 
70 	simple_lock_init(&mntvnode_slock);
71 	simple_lock_init(&mntid_slock);
72 	simple_lock_init(&spechash_slock);
73 	TAILQ_INIT(&vnode_free_list);
74 	simple_lock_init(&vnode_free_list_slock);
75 	CIRCLEQ_INIT(&mountlist);
76 }
77 
78 /*
79  * Lock a filesystem.
80  * Used to prevent access to it while mounting and unmounting.
81  */
82 int
83 vfs_lock(mp)
84 	register struct mount *mp;
85 {
86 
87 	while (mp->mnt_flag & MNT_MLOCK) {
88 		mp->mnt_flag |= MNT_MWAIT;
89 		tsleep((caddr_t)mp, PVFS, "vfslock", 0);
90 	}
91 	mp->mnt_flag |= MNT_MLOCK;
92 	return (0);
93 }
94 
95 /*
96  * Unlock a locked filesystem.
97  * Panic if filesystem is not locked.
98  */
99 void
100 vfs_unlock(mp)
101 	register struct mount *mp;
102 {
103 
104 	if ((mp->mnt_flag & MNT_MLOCK) == 0)
105 		panic("vfs_unlock: not locked");
106 	mp->mnt_flag &= ~MNT_MLOCK;
107 	if (mp->mnt_flag & MNT_MWAIT) {
108 		mp->mnt_flag &= ~MNT_MWAIT;
109 		wakeup((caddr_t)mp);
110 	}
111 }
112 
113 /*
114  * Mark a mount point as busy.
115  * Used to synchronize access and to delay unmounting.
116  */
117 int
118 vfs_busy(mp)
119 	register struct mount *mp;
120 {
121 
122 	while (mp->mnt_flag & MNT_MPBUSY) {
123 		mp->mnt_flag |= MNT_MPWANT;
124 		tsleep((caddr_t)&mp->mnt_flag, PVFS, "vfsbusy", 0);
125 	}
126 	if (mp->mnt_flag & MNT_UNMOUNT)
127 		return (1);
128 	mp->mnt_flag |= MNT_MPBUSY;
129 	return (0);
130 }
131 
132 /*
133  * Free a busy filesystem.
134  * Panic if filesystem is not busy.
135  */
136 void
137 vfs_unbusy(mp)
138 	register struct mount *mp;
139 {
140 
141 	if ((mp->mnt_flag & MNT_MPBUSY) == 0)
142 		panic("vfs_unbusy: not busy");
143 	mp->mnt_flag &= ~MNT_MPBUSY;
144 	if (mp->mnt_flag & MNT_MPWANT) {
145 		mp->mnt_flag &= ~MNT_MPWANT;
146 		wakeup((caddr_t)&mp->mnt_flag);
147 	}
148 }
149 
150 /*
151  * Lookup a filesystem type, and if found allocate and initialize
152  * a mount structure for it.
153  *
154  * Devname is usually updated by mount(8) after booting.
155  */
156 int
157 vfs_rootmountalloc(fstypename, devname, mpp)
158 	char *fstypename;
159 	char *devname;
160 	struct mount **mpp;
161 {
162 	struct vfsconf *vfsp;
163 	struct mount *mp;
164 
165 	for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
166 		if (!strcmp(vfsp->vfc_name, fstypename))
167 			break;
168 	if (vfsp == NULL)
169 		return (ENODEV);
170 	mp = malloc((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK);
171 	bzero((char *)mp, (u_long)sizeof(struct mount));
172 	LIST_INIT(&mp->mnt_vnodelist);
173 	mp->mnt_vfc = vfsp;
174 	mp->mnt_op = vfsp->vfc_vfsops;
175 	mp->mnt_flag = MNT_RDONLY;
176 	mp->mnt_vnodecovered = NULLVP;
177 	vfsp->vfc_refcount++;
178 	mp->mnt_stat.f_type = vfsp->vfc_typenum;
179 	mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
180 	strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
181 	mp->mnt_stat.f_mntonname[0] = '/';
182 	(void) copystr(devname, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, 0);
183 	*mpp = mp;
184 	return (0);
185 }
186 
187 /*
188  * Find an appropriate filesystem to use for the root. If a filesystem
189  * has not been preselected, walk through the list of known filesystems
190  * trying those that have mountroot routines, and try them until one
191  * works or we have tried them all.
192  */
193 int
194 vfs_mountroot()
195 {
196 	struct vfsconf *vfsp;
197 	extern int (*mountroot)(void);
198 	int error;
199 
200 	if (mountroot != NULL)
201 		return ((*mountroot)());
202 	for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
203 		if (vfsp->vfc_mountroot == NULL)
204 			continue;
205 		if ((error = (*vfsp->vfc_mountroot)()) == 0)
206 			return (0);
207 		printf("%s_mountroot failed: %d\n", vfsp->vfc_name, error);
208 	}
209 	return (ENODEV);
210 }
211 
212 /*
213  * Lookup a mount point by filesystem identifier.
214  */
215 struct mount *
216 vfs_getvfs(fsid)
217 	fsid_t *fsid;
218 {
219 	register struct mount *mp;
220 
221 	for (mp = mountlist.cqh_first; mp != (void *)&mountlist;
222 	     mp = mp->mnt_list.cqe_next) {
223 		if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
224 		    mp->mnt_stat.f_fsid.val[1] == fsid->val[1])
225 			return (mp);
226 	}
227 	return ((struct mount *)0);
228 }
229 
230 /*
231  * Get a new unique fsid
232  */
233 void
234 vfs_getnewfsid(mp)
235 	struct mount *mp;
236 {
237 static u_short xxxfs_mntid;
238 
239 	fsid_t tfsid;
240 	int mtype;
241 
242 	simple_lock(&mntid_slock);
243 	mtype = mp->mnt_vfc->vfc_typenum;
244 	mp->mnt_stat.f_fsid.val[0] = makedev(nblkdev + mtype, 0);
245 	mp->mnt_stat.f_fsid.val[1] = mtype;
246 	if (xxxfs_mntid == 0)
247 		++xxxfs_mntid;
248 	tfsid.val[0] = makedev(nblkdev + mtype, xxxfs_mntid);
249 	tfsid.val[1] = mtype;
250 	if (mountlist.cqh_first != (void *)&mountlist) {
251 		while (vfs_getvfs(&tfsid)) {
252 			tfsid.val[0]++;
253 			xxxfs_mntid++;
254 		}
255 	}
256 	mp->mnt_stat.f_fsid.val[0] = tfsid.val[0];
257 	simple_unlock(&mntid_slock);
258 }
259 
260 /*
261  * Set vnode attributes to VNOVAL
262  */
263 void
264 vattr_null(vap)
265 	register struct vattr *vap;
266 {
267 
268 	vap->va_type = VNON;
269 	vap->va_size = vap->va_bytes = VNOVAL;
270 	vap->va_mode = vap->va_nlink = vap->va_uid = vap->va_gid =
271 		vap->va_fsid = vap->va_fileid =
272 		vap->va_blocksize = vap->va_rdev =
273 		vap->va_atime.ts_sec = vap->va_atime.ts_nsec =
274 		vap->va_mtime.ts_sec = vap->va_mtime.ts_nsec =
275 		vap->va_ctime.ts_sec = vap->va_ctime.ts_nsec =
276 		vap->va_flags = vap->va_gen = VNOVAL;
277 	vap->va_vaflags = 0;
278 }
279 
280 /*
281  * Routines having to do with the management of the vnode table.
282  */
283 extern int (**dead_vnodeop_p)();
284 static void vclean __P((struct vnode *vp, int flag, struct proc *p));
285 extern void vgonel __P((struct vnode *vp, struct proc *p));
286 long numvnodes;
287 extern struct vattr va_null;
288 
289 /*
290  * Return the next vnode from the free list.
291  */
292 int
293 getnewvnode(tag, mp, vops, vpp)
294 	enum vtagtype tag;
295 	struct mount *mp;
296 	int (**vops)();
297 	struct vnode **vpp;
298 {
299 	struct proc *p = curproc;	/* XXX */
300 	struct vnode *vp;
301 	int s;
302 	int cnt;
303 
304 top:
305 	simple_lock(&vnode_free_list_slock);
306 	if ((vnode_free_list.tqh_first == NULL &&
307 	     numvnodes < 2 * desiredvnodes) ||
308 	    numvnodes < desiredvnodes) {
309 		simple_unlock(&vnode_free_list_slock);
310 		vp = (struct vnode *)malloc((u_long)sizeof *vp,
311 		    M_VNODE, M_WAITOK);
312 		bzero((char *)vp, sizeof *vp);
313 		numvnodes++;
314 	} else {
315 		for (vp = vnode_free_list.tqh_first;
316 				vp != NULLVP; vp = vp->v_freelist.tqe_next) {
317 			if (simple_lock_try(&vp->v_interlock))
318 				break;
319 		}
320 		/*
321 		 * Unless this is a bad time of the month, at most
322 		 * the first NCPUS items on the free list are
323 		 * locked, so this is close enough to being empty.
324 		 */
325 		if (vp == NULLVP) {
326 			simple_unlock(&vnode_free_list_slock);
327 			tablefull("vnode");
328 			*vpp = 0;
329 			return (ENFILE);
330 		}
331 		if (vp->v_usecount)
332 			panic("free vnode isn't");
333 		TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
334 		/* see comment on why 0xdeadb is set at end of vgone (below) */
335 		vp->v_freelist.tqe_prev = (struct vnode **)0xdeadb;
336 		simple_unlock(&vnode_free_list_slock);
337 		vp->v_lease = NULL;
338 		if (vp->v_type != VBAD)
339 			vgonel(vp, p);
340 		else
341 			simple_unlock(&vp->v_interlock);
342 #ifdef DIAGNOSTIC
343 		if (vp->v_data)
344 			panic("cleaned vnode isn't");
345 		s = splbio();
346 		if (vp->v_numoutput)
347 			panic("Clean vnode has pending I/O's");
348 		splx(s);
349 #endif
350 		vp->v_flag = 0;
351 		vp->v_lastr = 0;
352 		vp->v_ralen = 0;
353 		vp->v_maxra = 0;
354 		vp->v_lastw = 0;
355 		vp->v_lasta = 0;
356 		vp->v_cstart = 0;
357 		vp->v_clen = 0;
358 		vp->v_socket = 0;
359 	}
360 	vp->v_type = VNON;
361 	cache_purge(vp);
362 	vp->v_tag = tag;
363 	vp->v_op = vops;
364 	insmntque(vp, mp);
365 	*vpp = vp;
366 	vp->v_usecount = 1;
367 	vp->v_data = 0;
368 	return (0);
369 }
370 
371 /*
372  * Move a vnode from one mount queue to another.
373  */
374 void
375 insmntque(vp, mp)
376 	struct vnode *vp;
377 	struct mount *mp;
378 {
379 
380 	simple_lock(&mntvnode_slock);
381 	/*
382 	 * Delete from old mount point vnode list, if on one.
383 	 */
384 	if (vp->v_mount != NULL)
385 		LIST_REMOVE(vp, v_mntvnodes);
386 	/*
387 	 * Insert into list of vnodes for the new mount point, if available.
388 	 */
389 	if ((vp->v_mount = mp) != NULL)
390 		LIST_INSERT_HEAD(&mp->mnt_vnodelist, vp, v_mntvnodes);
391 	simple_unlock(&mntvnode_slock);
392 }
393 
394 /*
395  * Update outstanding I/O count and do wakeup if requested.
396  */
397 void
398 vwakeup(bp)
399 	register struct buf *bp;
400 {
401 	register struct vnode *vp;
402 
403 	bp->b_flags &= ~B_WRITEINPROG;
404 	if (vp = bp->b_vp) {
405 		if (--vp->v_numoutput < 0)
406 			panic("vwakeup: neg numoutput");
407 		if ((vp->v_flag & VBWAIT) && vp->v_numoutput <= 0) {
408 			if (vp->v_numoutput < 0)
409 				panic("vwakeup: neg numoutput 2");
410 			vp->v_flag &= ~VBWAIT;
411 			wakeup((caddr_t)&vp->v_numoutput);
412 		}
413 	}
414 }
415 
416 /*
417  * Flush out and invalidate all buffers associated with a vnode.
418  * Called with the underlying object locked.
419  */
420 int
421 vinvalbuf(vp, flags, cred, p, slpflag, slptimeo)
422 	register struct vnode *vp;
423 	int flags;
424 	struct ucred *cred;
425 	struct proc *p;
426 	int slpflag, slptimeo;
427 {
428 	register struct buf *bp;
429 	struct buf *nbp, *blist;
430 	int s, error;
431 
432 	if (flags & V_SAVE) {
433 		if (error = VOP_FSYNC(vp, cred, MNT_WAIT, p))
434 			return (error);
435 		if (vp->v_dirtyblkhd.lh_first != NULL)
436 			panic("vinvalbuf: dirty bufs");
437 	}
438 	for (;;) {
439 		if ((blist = vp->v_cleanblkhd.lh_first) && flags & V_SAVEMETA)
440 			while (blist && blist->b_lblkno < 0)
441 				blist = blist->b_vnbufs.le_next;
442 		if (!blist && (blist = vp->v_dirtyblkhd.lh_first) &&
443 		    (flags & V_SAVEMETA))
444 			while (blist && blist->b_lblkno < 0)
445 				blist = blist->b_vnbufs.le_next;
446 		if (!blist)
447 			break;
448 
449 		for (bp = blist; bp; bp = nbp) {
450 			nbp = bp->b_vnbufs.le_next;
451 			if (flags & V_SAVEMETA && bp->b_lblkno < 0)
452 				continue;
453 			s = splbio();
454 			if (bp->b_flags & B_BUSY) {
455 				bp->b_flags |= B_WANTED;
456 				error = tsleep((caddr_t)bp,
457 					slpflag | (PRIBIO + 1), "vinvalbuf",
458 					slptimeo);
459 				splx(s);
460 				if (error)
461 					return (error);
462 				break;
463 			}
464 			bremfree(bp);
465 			bp->b_flags |= B_BUSY;
466 			splx(s);
467 			/*
468 			 * XXX Since there are no node locks for NFS, I believe
469 			 * there is a slight chance that a delayed write will
470 			 * occur while sleeping just above, so check for it.
471 			 */
472 			if ((bp->b_flags & B_DELWRI) && (flags & V_SAVE)) {
473 				(void) VOP_BWRITE(bp);
474 				break;
475 			}
476 			bp->b_flags |= B_INVAL;
477 			brelse(bp);
478 		}
479 	}
480 	if (!(flags & V_SAVEMETA) &&
481 	    (vp->v_dirtyblkhd.lh_first || vp->v_cleanblkhd.lh_first))
482 		panic("vinvalbuf: flush failed");
483 	return (0);
484 }
485 
486 /*
487  * Associate a buffer with a vnode.
488  */
489 void
490 bgetvp(vp, bp)
491 	register struct vnode *vp;
492 	register struct buf *bp;
493 {
494 
495 	if (bp->b_vp)
496 		panic("bgetvp: not free");
497 	VHOLD(vp);
498 	bp->b_vp = vp;
499 	if (vp->v_type == VBLK || vp->v_type == VCHR)
500 		bp->b_dev = vp->v_rdev;
501 	else
502 		bp->b_dev = NODEV;
503 	/*
504 	 * Insert onto list for new vnode.
505 	 */
506 	bufinsvn(bp, &vp->v_cleanblkhd);
507 }
508 
509 /*
510  * Disassociate a buffer from a vnode.
511  */
512 void
513 brelvp(bp)
514 	register struct buf *bp;
515 {
516 	struct vnode *vp;
517 
518 	if (bp->b_vp == (struct vnode *) 0)
519 		panic("brelvp: NULL");
520 	/*
521 	 * Delete from old vnode list, if on one.
522 	 */
523 	if (bp->b_vnbufs.le_next != NOLIST)
524 		bufremvn(bp);
525 	vp = bp->b_vp;
526 	bp->b_vp = (struct vnode *) 0;
527 	HOLDRELE(vp);
528 }
529 
530 /*
531  * Reassign a buffer from one vnode to another.
532  * Used to assign file specific control information
533  * (indirect blocks) to the vnode to which they belong.
534  */
535 void
536 reassignbuf(bp, newvp)
537 	register struct buf *bp;
538 	register struct vnode *newvp;
539 {
540 	register struct buflists *listheadp;
541 
542 	if (newvp == NULL) {
543 		printf("reassignbuf: NULL");
544 		return;
545 	}
546 	/*
547 	 * Delete from old vnode list, if on one.
548 	 */
549 	if (bp->b_vnbufs.le_next != NOLIST)
550 		bufremvn(bp);
551 	/*
552 	 * If dirty, put on list of dirty buffers;
553 	 * otherwise insert onto list of clean buffers.
554 	 */
555 	if (bp->b_flags & B_DELWRI)
556 		listheadp = &newvp->v_dirtyblkhd;
557 	else
558 		listheadp = &newvp->v_cleanblkhd;
559 	bufinsvn(bp, listheadp);
560 }
561 
562 /*
563  * Create a vnode for a block device.
564  * Used for root filesystem, argdev, and swap areas.
565  * Also used for memory file system special devices.
566  */
567 int
568 bdevvp(dev, vpp)
569 	dev_t dev;
570 	struct vnode **vpp;
571 {
572 	register struct vnode *vp;
573 	struct vnode *nvp;
574 	int error;
575 
576 	if (dev == NODEV) {
577 		*vpp = NULLVP;
578 		return (ENODEV);
579 	}
580 	error = getnewvnode(VT_NON, (struct mount *)0, spec_vnodeop_p, &nvp);
581 	if (error) {
582 		*vpp = NULLVP;
583 		return (error);
584 	}
585 	vp = nvp;
586 	vp->v_type = VBLK;
587 	if (nvp = checkalias(vp, dev, (struct mount *)0)) {
588 		vput(vp);
589 		vp = nvp;
590 	}
591 	*vpp = vp;
592 	return (0);
593 }
594 
595 /*
596  * Check to see if the new vnode represents a special device
597  * for which we already have a vnode (either because of
598  * bdevvp() or because of a different vnode representing
599  * the same block device). If such an alias exists, deallocate
600  * the existing contents and return the aliased vnode. The
601  * caller is responsible for filling it with its new contents.
602  */
603 struct vnode *
604 checkalias(nvp, nvp_rdev, mp)
605 	register struct vnode *nvp;
606 	dev_t nvp_rdev;
607 	struct mount *mp;
608 {
609 	struct proc *p = curproc;	/* XXX */
610 	struct vnode *vp;
611 	struct vnode **vpp;
612 
613 	if (nvp->v_type != VBLK && nvp->v_type != VCHR)
614 		return (NULLVP);
615 
616 	vpp = &speclisth[SPECHASH(nvp_rdev)];
617 loop:
618 	simple_lock(&spechash_slock);
619 	for (vp = *vpp; vp; vp = vp->v_specnext) {
620 		if (nvp_rdev != vp->v_rdev || nvp->v_type != vp->v_type)
621 			continue;
622 		/*
623 		 * Alias, but not in use, so flush it out.
624 		 */
625 		simple_lock(&vp->v_interlock);
626 		if (vp->v_usecount == 0) {
627 			simple_unlock(&spechash_slock);
628 			vgonel(vp, p);
629 			goto loop;
630 		}
631 		if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, p)) {
632 			simple_unlock(&spechash_slock);
633 			goto loop;
634 		}
635 		break;
636 	}
637 	if (vp == NULL || vp->v_tag != VT_NON) {
638 		MALLOC(nvp->v_specinfo, struct specinfo *,
639 			sizeof(struct specinfo), M_VNODE, M_WAITOK);
640 		nvp->v_rdev = nvp_rdev;
641 		nvp->v_hashchain = vpp;
642 		nvp->v_specnext = *vpp;
643 		nvp->v_specflags = 0;
644 		simple_unlock(&spechash_slock);
645 		*vpp = nvp;
646 		if (vp != NULLVP) {
647 			nvp->v_flag |= VALIASED;
648 			vp->v_flag |= VALIASED;
649 			vput(vp);
650 		}
651 		return (NULLVP);
652 	}
653 	simple_unlock(&spechash_slock);
654 	VOP_UNLOCK(vp, 0, p);
655 	simple_lock(&vp->v_interlock);
656 	vclean(vp, 0, p);
657 	vp->v_op = nvp->v_op;
658 	vp->v_tag = nvp->v_tag;
659 	nvp->v_type = VNON;
660 	insmntque(vp, mp);
661 	return (vp);
662 }
663 
664 /*
665  * Grab a particular vnode from the free list, increment its
666  * reference count and lock it. The vnode lock bit is set the
667  * vnode is being eliminated in vgone. The process is awakened
668  * when the transition is completed, and an error returned to
669  * indicate that the vnode is no longer usable (possibly having
670  * been changed to a new file system type).
671  */
672 int
673 vget(vp, flags, p)
674 	struct vnode *vp;
675 	int flags;
676 	struct proc *p;
677 {
678 	int error;
679 
680 	/*
681 	 * If the vnode is in the process of being cleaned out for
682 	 * another use, we wait for the cleaning to finish and then
683 	 * return failure. Cleaning is determined by checking that
684 	 * the VXLOCK flag is set.
685 	 */
686 	if ((flags & LK_INTERLOCK) == 0)
687 		simple_lock(&vp->v_interlock);
688 	if (vp->v_flag & VXLOCK) {
689 		vp->v_flag |= VXWANT;
690 		simple_unlock(&vp->v_interlock);
691 		tsleep((caddr_t)vp, PINOD, "vget", 0);
692 		return (ENOENT);
693 	}
694 	if (vp->v_usecount == 0) {
695 		simple_lock(&vnode_free_list_slock);
696 		TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
697 		simple_unlock(&vnode_free_list_slock);
698 	}
699 	vp->v_usecount++;
700 	if (flags & LK_TYPE_MASK) {
701 		if (error = vn_lock(vp, flags | LK_INTERLOCK, p))
702 			vrele(vp);
703 		return (error);
704 	}
705 	simple_unlock(&vp->v_interlock);
706 	return (0);
707 }
708 
709 /*
710  * Stubs to use when there is no locking to be done on the underlying object.
711  *
712  * Getting a lock just clears the interlock if necessary.
713  */
714 int
715 vop_nolock(ap)
716 	struct vop_lock_args /* {
717 		struct vnode *a_vp;
718 		int a_flags;
719 		struct proc *a_p;
720 	} */ *ap;
721 {
722 	struct vnode *vp = ap->a_vp;
723 
724 	/*
725 	 * Since we are not using the lock manager, we must clear
726 	 * the interlock here.
727 	 */
728 	if (ap->a_flags & LK_INTERLOCK)
729 		simple_unlock(&vp->v_interlock);
730 	return (0);
731 }
732 
733 /*
734  * Unlock has nothing to do.
735  */
736 int
737 vop_nounlock(ap)
738 	struct vop_unlock_args /* {
739 		struct vnode *a_vp;
740 		int a_flags;
741 		struct proc *a_p;
742 	} */ *ap;
743 {
744 
745 	return (0);
746 }
747 
748 /*
749  * Nothing is ever locked.
750  */
751 int
752 vop_noislocked(ap)
753 	struct vop_islocked_args /* {
754 		struct vnode *a_vp;
755 	} */ *ap;
756 {
757 
758 	return (0);
759 }
760 
761 /*
762  * Vnode reference.
763  */
764 void
765 vref(vp)
766 	struct vnode *vp;
767 {
768 
769 	simple_lock(&vp->v_interlock);
770 	if (vp->v_usecount <= 0)
771 		panic("vref used where vget required");
772 	vp->v_usecount++;
773 	simple_unlock(&vp->v_interlock);
774 }
775 
776 /*
777  * vput(), just unlock and vrele()
778  */
779 void
780 vput(vp)
781 	struct vnode *vp;
782 {
783 	struct proc *p = curproc;	/* XXX */
784 
785 #ifdef DIGANOSTIC
786 	if (vp == NULL)
787 		panic("vput: null vp");
788 #endif
789 	simple_lock(&vp->v_interlock);
790 	vp->v_usecount--;
791 	if (vp->v_usecount > 0) {
792 		simple_unlock(&vp->v_interlock);
793 		VOP_UNLOCK(vp, 0, p);
794 		return;
795 	}
796 #ifdef DIAGNOSTIC
797 	if (vp->v_usecount < 0 || vp->v_writecount != 0) {
798 		vprint("vput: bad ref count", vp);
799 		panic("vput: ref cnt");
800 	}
801 #endif
802 	/*
803 	 * insert at tail of LRU list
804 	 */
805 	simple_lock(&vnode_free_list_slock);
806 	TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
807 	simple_unlock(&vnode_free_list_slock);
808 	simple_unlock(&vp->v_interlock);
809 	VOP_INACTIVE(vp, p);
810 }
811 
812 /*
813  * Vnode release.
814  * If count drops to zero, call inactive routine and return to freelist.
815  */
816 void
817 vrele(vp)
818 	struct vnode *vp;
819 {
820 	struct proc *p = curproc;	/* XXX */
821 
822 #ifdef DIAGNOSTIC
823 	if (vp == NULL)
824 		panic("vrele: null vp");
825 #endif
826 	simple_lock(&vp->v_interlock);
827 	vp->v_usecount--;
828 	if (vp->v_usecount > 0) {
829 		simple_unlock(&vp->v_interlock);
830 		return;
831 	}
832 #ifdef DIAGNOSTIC
833 	if (vp->v_usecount < 0 || vp->v_writecount != 0) {
834 		vprint("vrele: bad ref count", vp);
835 		panic("vrele: ref cnt");
836 	}
837 #endif
838 	/*
839 	 * insert at tail of LRU list
840 	 */
841 	simple_lock(&vnode_free_list_slock);
842 	TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
843 	simple_unlock(&vnode_free_list_slock);
844 	if (vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK, p) == 0)
845 		VOP_INACTIVE(vp, p);
846 }
847 
848 #ifdef DIAGNOSTIC
849 /*
850  * Page or buffer structure gets a reference.
851  */
852 void
853 vhold(vp)
854 	register struct vnode *vp;
855 {
856 
857 	simple_lock(&vp->v_interlock);
858 	vp->v_holdcnt++;
859 	simple_unlock(&vp->v_interlock);
860 }
861 
862 /*
863  * Page or buffer structure frees a reference.
864  */
865 void
866 holdrele(vp)
867 	register struct vnode *vp;
868 {
869 
870 	simple_lock(&vp->v_interlock);
871 	if (vp->v_holdcnt <= 0)
872 		panic("holdrele: holdcnt");
873 	vp->v_holdcnt--;
874 	simple_unlock(&vp->v_interlock);
875 }
876 #endif /* DIAGNOSTIC */
877 
878 /*
879  * Remove any vnodes in the vnode table belonging to mount point mp.
880  *
881  * If MNT_NOFORCE is specified, there should not be any active ones,
882  * return error if any are found (nb: this is a user error, not a
883  * system error). If MNT_FORCE is specified, detach any active vnodes
884  * that are found.
885  */
886 #ifdef DIAGNOSTIC
887 int busyprt = 0;	/* print out busy vnodes */
888 struct ctldebug debug1 = { "busyprt", &busyprt };
889 #endif
890 
891 int
892 vflush(mp, skipvp, flags)
893 	struct mount *mp;
894 	struct vnode *skipvp;
895 	int flags;
896 {
897 	struct proc *p = curproc;	/* XXX */
898 	struct vnode *vp, *nvp;
899 	int busy = 0;
900 
901 #ifdef DIAGNOSTIC
902 	if ((mp->mnt_flag & MNT_MPBUSY) == 0)
903 		panic("vflush: not busy");
904 #endif
905 
906 	simple_lock(&mntvnode_slock);
907 loop:
908 	for (vp = mp->mnt_vnodelist.lh_first; vp; vp = nvp) {
909 		if (vp->v_mount != mp)
910 			goto loop;
911 		nvp = vp->v_mntvnodes.le_next;
912 		/*
913 		 * Skip over a selected vnode.
914 		 */
915 		if (vp == skipvp)
916 			continue;
917 
918 		simple_lock(&vp->v_interlock);
919 		/*
920 		 * Skip over a vnodes marked VSYSTEM.
921 		 */
922 		if ((flags & SKIPSYSTEM) && (vp->v_flag & VSYSTEM)) {
923 			simple_unlock(&vp->v_interlock);
924 			continue;
925 		}
926 		/*
927 		 * If WRITECLOSE is set, only flush out regular file
928 		 * vnodes open for writing.
929 		 */
930 		if ((flags & WRITECLOSE) &&
931 		    (vp->v_writecount == 0 || vp->v_type != VREG)) {
932 			simple_unlock(&vp->v_interlock);
933 			continue;
934 		}
935 		/*
936 		 * With v_usecount == 0, all we need to do is clear
937 		 * out the vnode data structures and we are done.
938 		 */
939 		if (vp->v_usecount == 0) {
940 			simple_unlock(&mntvnode_slock);
941 			vgonel(vp, p);
942 			simple_lock(&mntvnode_slock);
943 			continue;
944 		}
945 		/*
946 		 * If FORCECLOSE is set, forcibly close the vnode.
947 		 * For block or character devices, revert to an
948 		 * anonymous device. For all other files, just kill them.
949 		 */
950 		if (flags & FORCECLOSE) {
951 			simple_unlock(&mntvnode_slock);
952 			if (vp->v_type != VBLK && vp->v_type != VCHR) {
953 				vgonel(vp, p);
954 			} else {
955 				vclean(vp, 0, p);
956 				vp->v_op = spec_vnodeop_p;
957 				insmntque(vp, (struct mount *)0);
958 			}
959 			simple_lock(&mntvnode_slock);
960 			continue;
961 		}
962 #ifdef DIAGNOSTIC
963 		if (busyprt)
964 			vprint("vflush: busy vnode", vp);
965 #endif
966 		simple_unlock(&vp->v_interlock);
967 		busy++;
968 	}
969 	simple_unlock(&mntvnode_slock);
970 	if (busy)
971 		return (EBUSY);
972 	return (0);
973 }
974 
975 /*
976  * Disassociate the underlying file system from a vnode.
977  * The vnode interlock is held on entry.
978  */
979 static void
980 vclean(vp, flags, p)
981 	struct vnode *vp;
982 	int flags;
983 	struct proc *p;
984 {
985 	int active;
986 
987 	/*
988 	 * Check to see if the vnode is in use.
989 	 * If so we have to reference it before we clean it out
990 	 * so that its count cannot fall to zero and generate a
991 	 * race against ourselves to recycle it.
992 	 */
993 	if (active = vp->v_usecount)
994 		vp->v_usecount++;
995 	/*
996 	 * Prevent the vnode from being recycled or
997 	 * brought into use while we clean it out.
998 	 */
999 	if (vp->v_flag & VXLOCK)
1000 		panic("vclean: deadlock");
1001 	vp->v_flag |= VXLOCK;
1002 	/*
1003 	 * Even if the count is zero, the VOP_INACTIVE routine may still
1004 	 * have the object locked while it cleans it out. The VOP_LOCK
1005 	 * ensures that the VOP_INACTIVE routine is done with its work.
1006 	 * For active vnodes, it ensures that no other activity can
1007 	 * occur while the underlying object is being cleaned out.
1008 	 */
1009 	VOP_LOCK(vp, LK_DRAIN | LK_INTERLOCK, p);
1010 	/*
1011 	 * Clean out any buffers associated with the vnode.
1012 	 */
1013 	if (flags & DOCLOSE)
1014 		vinvalbuf(vp, V_SAVE, NOCRED, NULL, 0, 0);
1015 	/*
1016 	 * If purging an active vnode, it must be closed and
1017 	 * deactivated before being reclaimed. Note that the
1018 	 * VOP_INACTIVE will unlock the vnode.
1019 	 */
1020 	if (active) {
1021 		if (flags & DOCLOSE)
1022 			VOP_CLOSE(vp, IO_NDELAY, NOCRED, p);
1023 		VOP_INACTIVE(vp, p);
1024 	} else {
1025 		/*
1026 		 * Any other processes trying to obtain this lock must first
1027 		 * wait for VXLOCK to clear, then call the new lock operation.
1028 		 */
1029 		VOP_UNLOCK(vp, 0, p);
1030 	}
1031 	/*
1032 	 * Reclaim the vnode.
1033 	 */
1034 	if (VOP_RECLAIM(vp, p))
1035 		panic("vclean: cannot reclaim");
1036 	if (active)
1037 		vrele(vp);
1038 	cache_purge(vp);
1039 
1040 	/*
1041 	 * Done with purge, notify sleepers of the grim news.
1042 	 */
1043 	vp->v_op = dead_vnodeop_p;
1044 	vp->v_tag = VT_NON;
1045 	vp->v_flag &= ~VXLOCK;
1046 	if (vp->v_flag & VXWANT) {
1047 		vp->v_flag &= ~VXWANT;
1048 		wakeup((caddr_t)vp);
1049 	}
1050 }
1051 
1052 /*
1053  * Eliminate all activity associated with  the requested vnode
1054  * and with all vnodes aliased to the requested vnode.
1055  */
1056 int
1057 vop_revoke(ap)
1058 	struct vop_revoke_args /* {
1059 		struct vnode *a_vp;
1060 		int a_flags;
1061 	} */ *ap;
1062 {
1063 	struct vnode *vp, *vq;
1064 	struct proc *p = curproc;	/* XXX */
1065 
1066 #ifdef DIAGNOSTIC
1067 	if ((ap->a_flags & REVOKEALL) == 0)
1068 		panic("vop_revoke");
1069 #endif
1070 
1071 	vp = ap->a_vp;
1072 	simple_lock(&vp->v_interlock);
1073 
1074 	if (vp->v_flag & VALIASED) {
1075 		/*
1076 		 * If a vgone (or vclean) is already in progress,
1077 		 * wait until it is done and return.
1078 		 */
1079 		if (vp->v_flag & VXLOCK) {
1080 			vp->v_flag |= VXWANT;
1081 			simple_unlock(&vp->v_interlock);
1082 			tsleep((caddr_t)vp, PINOD, "vop_revokeall", 0);
1083 			return (0);
1084 		}
1085 		/*
1086 		 * Ensure that vp will not be vgone'd while we
1087 		 * are eliminating its aliases.
1088 		 */
1089 		vp->v_flag |= VXLOCK;
1090 		simple_unlock(&vp->v_interlock);
1091 		while (vp->v_flag & VALIASED) {
1092 			simple_lock(&spechash_slock);
1093 			for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
1094 				if (vq->v_rdev != vp->v_rdev ||
1095 				    vq->v_type != vp->v_type || vp == vq)
1096 					continue;
1097 				simple_unlock(&spechash_slock);
1098 				vgone(vq);
1099 				break;
1100 			}
1101 			if (vq == NULLVP)
1102 				simple_unlock(&spechash_slock);
1103 		}
1104 		/*
1105 		 * Remove the lock so that vgone below will
1106 		 * really eliminate the vnode after which time
1107 		 * vgone will awaken any sleepers.
1108 		 */
1109 		simple_lock(&vp->v_interlock);
1110 		vp->v_flag &= ~VXLOCK;
1111 	}
1112 	vgonel(vp, p);
1113 	return (0);
1114 }
1115 
1116 /*
1117  * Recycle an unused vnode to the front of the free list.
1118  * Release the passed interlock if the vnode will be recycled.
1119  */
1120 int
1121 vrecycle(vp, inter_lkp, p)
1122 	struct vnode *vp;
1123 	struct simplelock *inter_lkp;
1124 	struct proc *p;
1125 {
1126 
1127 	simple_lock(&vp->v_interlock);
1128 	if (vp->v_usecount == 0) {
1129 		if (inter_lkp)
1130 			simple_unlock(inter_lkp);
1131 		vgonel(vp, p);
1132 		return (1);
1133 	}
1134 	simple_unlock(&vp->v_interlock);
1135 	return (0);
1136 }
1137 
1138 /*
1139  * Eliminate all activity associated with a vnode
1140  * in preparation for reuse.
1141  */
1142 void
1143 vgone(vp)
1144 	struct vnode *vp;
1145 {
1146 	struct proc *p = curproc;	/* XXX */
1147 
1148 	simple_lock(&vp->v_interlock);
1149 	vgonel(vp, p);
1150 }
1151 
1152 /*
1153  * vgone, with the vp interlock held.
1154  */
1155 void
1156 vgonel(vp, p)
1157 	struct vnode *vp;
1158 	struct proc *p;
1159 {
1160 	struct vnode *vq;
1161 	struct vnode *vx;
1162 
1163 	/*
1164 	 * If a vgone (or vclean) is already in progress,
1165 	 * wait until it is done and return.
1166 	 */
1167 	if (vp->v_flag & VXLOCK) {
1168 		vp->v_flag |= VXWANT;
1169 		simple_unlock(&vp->v_interlock);
1170 		tsleep((caddr_t)vp, PINOD, "vgone", 0);
1171 		return;
1172 	}
1173 	/*
1174 	 * Clean out the filesystem specific data.
1175 	 */
1176 	vclean(vp, DOCLOSE, p);
1177 	/*
1178 	 * Delete from old mount point vnode list, if on one.
1179 	 */
1180 	if (vp->v_mount != NULL)
1181 		insmntque(vp, (struct mount *)0);
1182 	/*
1183 	 * If special device, remove it from special device alias list
1184 	 * if it is on one.
1185 	 */
1186 	if ((vp->v_type == VBLK || vp->v_type == VCHR) && vp->v_specinfo != 0) {
1187 		simple_lock(&spechash_slock);
1188 		if (*vp->v_hashchain == vp) {
1189 			*vp->v_hashchain = vp->v_specnext;
1190 		} else {
1191 			for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
1192 				if (vq->v_specnext != vp)
1193 					continue;
1194 				vq->v_specnext = vp->v_specnext;
1195 				break;
1196 			}
1197 			if (vq == NULL)
1198 				panic("missing bdev");
1199 		}
1200 		if (vp->v_flag & VALIASED) {
1201 			vx = NULL;
1202 			for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
1203 				if (vq->v_rdev != vp->v_rdev ||
1204 				    vq->v_type != vp->v_type)
1205 					continue;
1206 				if (vx)
1207 					break;
1208 				vx = vq;
1209 			}
1210 			if (vx == NULL)
1211 				panic("missing alias");
1212 			if (vq == NULL)
1213 				vx->v_flag &= ~VALIASED;
1214 			vp->v_flag &= ~VALIASED;
1215 		}
1216 		simple_unlock(&spechash_slock);
1217 		FREE(vp->v_specinfo, M_VNODE);
1218 		vp->v_specinfo = NULL;
1219 	}
1220 	/*
1221 	 * If it is on the freelist and not already at the head,
1222 	 * move it to the head of the list. The test of the back
1223 	 * pointer and the reference count of zero is because
1224 	 * it will be removed from the free list by getnewvnode,
1225 	 * but will not have its reference count incremented until
1226 	 * after calling vgone. If the reference count were
1227 	 * incremented first, vgone would (incorrectly) try to
1228 	 * close the previous instance of the underlying object.
1229 	 * So, the back pointer is explicitly set to `0xdeadb' in
1230 	 * getnewvnode after removing it from the freelist to ensure
1231 	 * that we do not try to move it here.
1232 	 */
1233 	if (vp->v_usecount == 0) {
1234 		simple_lock(&vnode_free_list_slock);
1235 		if ((vp->v_freelist.tqe_prev != (struct vnode **)0xdeadb) &&
1236 		    vnode_free_list.tqh_first != vp) {
1237 			TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
1238 			TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
1239 		}
1240 		simple_unlock(&vnode_free_list_slock);
1241 	}
1242 	vp->v_type = VBAD;
1243 }
1244 
1245 /*
1246  * Lookup a vnode by device number.
1247  */
1248 int
1249 vfinddev(dev, type, vpp)
1250 	dev_t dev;
1251 	enum vtype type;
1252 	struct vnode **vpp;
1253 {
1254 	struct vnode *vp;
1255 	int rc = 0;
1256 
1257 	simple_lock(&spechash_slock);
1258 	for (vp = speclisth[SPECHASH(dev)]; vp; vp = vp->v_specnext) {
1259 		if (dev != vp->v_rdev || type != vp->v_type)
1260 			continue;
1261 		*vpp = vp;
1262 		rc = 1;
1263 		break;
1264 	}
1265 	simple_unlock(&spechash_slock);
1266 	return (rc);
1267 }
1268 
1269 /*
1270  * Calculate the total number of references to a special device.
1271  */
1272 int
1273 vcount(vp)
1274 	struct vnode *vp;
1275 {
1276 	struct vnode *vq, *vnext;
1277 	int count;
1278 
1279 loop:
1280 	if ((vp->v_flag & VALIASED) == 0)
1281 		return (vp->v_usecount);
1282 	simple_lock(&spechash_slock);
1283 	for (count = 0, vq = *vp->v_hashchain; vq; vq = vnext) {
1284 		vnext = vq->v_specnext;
1285 		if (vq->v_rdev != vp->v_rdev || vq->v_type != vp->v_type)
1286 			continue;
1287 		/*
1288 		 * Alias, but not in use, so flush it out.
1289 		 */
1290 		if (vq->v_usecount == 0 && vq != vp) {
1291 			simple_unlock(&spechash_slock);
1292 			vgone(vq);
1293 			goto loop;
1294 		}
1295 		count += vq->v_usecount;
1296 	}
1297 	simple_unlock(&spechash_slock);
1298 	return (count);
1299 }
1300 
1301 /*
1302  * Print out a description of a vnode.
1303  */
1304 static char *typename[] =
1305    { "VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD" };
1306 
1307 void
1308 vprint(label, vp)
1309 	char *label;
1310 	register struct vnode *vp;
1311 {
1312 	char buf[64];
1313 
1314 	if (label != NULL)
1315 		printf("%s: ", label);
1316 	printf("type %s, usecount %d, writecount %d, refcount %d,",
1317 		typename[vp->v_type], vp->v_usecount, vp->v_writecount,
1318 		vp->v_holdcnt);
1319 	buf[0] = '\0';
1320 	if (vp->v_flag & VROOT)
1321 		strcat(buf, "|VROOT");
1322 	if (vp->v_flag & VTEXT)
1323 		strcat(buf, "|VTEXT");
1324 	if (vp->v_flag & VSYSTEM)
1325 		strcat(buf, "|VSYSTEM");
1326 	if (vp->v_flag & VXLOCK)
1327 		strcat(buf, "|VXLOCK");
1328 	if (vp->v_flag & VXWANT)
1329 		strcat(buf, "|VXWANT");
1330 	if (vp->v_flag & VBWAIT)
1331 		strcat(buf, "|VBWAIT");
1332 	if (vp->v_flag & VALIASED)
1333 		strcat(buf, "|VALIASED");
1334 	if (buf[0] != '\0')
1335 		printf(" flags (%s)", &buf[1]);
1336 	if (vp->v_data == NULL) {
1337 		printf("\n");
1338 	} else {
1339 		printf("\n\t");
1340 		VOP_PRINT(vp);
1341 	}
1342 }
1343 
1344 #ifdef DEBUG
1345 /*
1346  * List all of the locked vnodes in the system.
1347  * Called when debugging the kernel.
1348  */
1349 void
1350 printlockedvnodes()
1351 {
1352 	register struct mount *mp;
1353 	register struct vnode *vp;
1354 
1355 	printf("Locked vnodes\n");
1356 	for (mp = mountlist.cqh_first; mp != (void *)&mountlist;
1357 	     mp = mp->mnt_list.cqe_next) {
1358 		for (vp = mp->mnt_vnodelist.lh_first;
1359 		     vp != NULL;
1360 		     vp = vp->v_mntvnodes.le_next) {
1361 			if (VOP_ISLOCKED(vp))
1362 				vprint((char *)0, vp);
1363 		}
1364 	}
1365 }
1366 #endif
1367 
1368 /*
1369  * Top level filesystem related information gathering.
1370  */
1371 int
1372 vfs_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
1373 	int *name;
1374 	u_int namelen;
1375 	void *oldp;
1376 	size_t *oldlenp;
1377 	void *newp;
1378 	size_t newlen;
1379 	struct proc *p;
1380 {
1381 	struct ctldebug *cdp;
1382 	struct vfsconf *vfsp;
1383 
1384 	/* all sysctl names at this level are at least name and field */
1385 	if (namelen < 2)
1386 		return (ENOTDIR);		/* overloaded */
1387 	if (name[0] != VFS_GENERIC) {
1388 		for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
1389 			if (vfsp->vfc_typenum == name[0])
1390 				break;
1391 		if (vfsp == NULL)
1392 			return (EOPNOTSUPP);
1393 		return ((*vfsp->vfc_vfsops->vfs_sysctl)(&name[1], namelen - 1,
1394 		    oldp, oldlenp, newp, newlen, p));
1395 	}
1396 	switch (name[1]) {
1397 	case VFS_MAXTYPENUM:
1398 		return (sysctl_rdint(oldp, oldlenp, newp, maxvfsconf));
1399 	case VFS_CONF:
1400 		if (namelen < 3)
1401 			return (ENOTDIR);	/* overloaded */
1402 		for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
1403 			if (vfsp->vfc_typenum == name[2])
1404 				break;
1405 		if (vfsp == NULL)
1406 			return (EOPNOTSUPP);
1407 		return (sysctl_rdstruct(oldp, oldlenp, newp, vfsp,
1408 		    sizeof(struct vfsconf)));
1409 	}
1410 	return (EOPNOTSUPP);
1411 }
1412 
1413 int kinfo_vdebug = 1;
1414 int kinfo_vgetfailed;
1415 #define KINFO_VNODESLOP	10
1416 /*
1417  * Dump vnode list (via sysctl).
1418  * Copyout address of vnode followed by vnode.
1419  */
1420 /* ARGSUSED */
1421 int
1422 sysctl_vnode(where, sizep)
1423 	char *where;
1424 	size_t *sizep;
1425 {
1426 	register struct mount *mp, *nmp;
1427 	struct vnode *nvp, *vp;
1428 	register char *bp = where, *savebp;
1429 	char *ewhere;
1430 	int error;
1431 
1432 #define VPTRSZ	sizeof (struct vnode *)
1433 #define VNODESZ	sizeof (struct vnode)
1434 	if (where == NULL) {
1435 		*sizep = (numvnodes + KINFO_VNODESLOP) * (VPTRSZ + VNODESZ);
1436 		return (0);
1437 	}
1438 	ewhere = where + *sizep;
1439 
1440 	for (mp = mountlist.cqh_first; mp != (void *)&mountlist; mp = nmp) {
1441 		nmp = mp->mnt_list.cqe_next;
1442 		if (vfs_busy(mp))
1443 			continue;
1444 		savebp = bp;
1445 again:
1446 		simple_lock(&mntvnode_slock);
1447 		for (vp = mp->mnt_vnodelist.lh_first;
1448 		     vp != NULL;
1449 		     vp = nvp) {
1450 			/*
1451 			 * Check that the vp is still associated with
1452 			 * this filesystem.  RACE: could have been
1453 			 * recycled onto the same filesystem.
1454 			 */
1455 			if (vp->v_mount != mp) {
1456 				simple_unlock(&mntvnode_slock);
1457 				if (kinfo_vdebug)
1458 					printf("kinfo: vp changed\n");
1459 				bp = savebp;
1460 				goto again;
1461 			}
1462 			nvp = vp->v_mntvnodes.le_next;
1463 			if (bp + VPTRSZ + VNODESZ > ewhere) {
1464 				simple_unlock(&mntvnode_slock);
1465 				*sizep = bp - where;
1466 				return (ENOMEM);
1467 			}
1468 			simple_unlock(&mntvnode_slock);
1469 			if ((error = copyout((caddr_t)&vp, bp, VPTRSZ)) ||
1470 			   (error = copyout((caddr_t)vp, bp + VPTRSZ, VNODESZ)))
1471 				return (error);
1472 			bp += VPTRSZ + VNODESZ;
1473 			simple_lock(&mntvnode_slock);
1474 		}
1475 		simple_unlock(&mntvnode_slock);
1476 		vfs_unbusy(mp);
1477 	}
1478 
1479 	*sizep = bp - where;
1480 	return (0);
1481 }
1482 
1483 /*
1484  * Check to see if a filesystem is mounted on a block device.
1485  */
1486 int
1487 vfs_mountedon(vp)
1488 	struct vnode *vp;
1489 {
1490 	struct vnode *vq;
1491 	int error = 0;
1492 
1493 	if (vp->v_specflags & SI_MOUNTEDON)
1494 		return (EBUSY);
1495 	if (vp->v_flag & VALIASED) {
1496 		simple_lock(&spechash_slock);
1497 		for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
1498 			if (vq->v_rdev != vp->v_rdev ||
1499 			    vq->v_type != vp->v_type)
1500 				continue;
1501 			if (vq->v_specflags & SI_MOUNTEDON) {
1502 				error = EBUSY;
1503 				break;
1504 			}
1505 		}
1506 		simple_unlock(&spechash_slock);
1507 	}
1508 	return (error);
1509 }
1510 
1511 /*
1512  * Unmount all filesystems. The list is traversed in reverse order
1513  * of mounting to avoid dependencies.
1514  */
1515 void
1516 vfs_unmountall()
1517 {
1518 	struct mount *mp, *nmp;
1519 
1520 	for (mp = mountlist.cqh_last; mp != (void *)&mountlist; mp = nmp) {
1521 		nmp = mp->mnt_list.cqe_prev;
1522 		(void) dounmount(mp, MNT_FORCE, &proc0);
1523 	}
1524 }
1525 
1526 /*
1527  * Build hash lists of net addresses and hang them off the mount point.
1528  * Called by ufs_mount() to set up the lists of export addresses.
1529  */
1530 static int
1531 vfs_hang_addrlist(mp, nep, argp)
1532 	struct mount *mp;
1533 	struct netexport *nep;
1534 	struct export_args *argp;
1535 {
1536 	register struct netcred *np;
1537 	register struct radix_node_head *rnh;
1538 	register int i;
1539 	struct radix_node *rn;
1540 	struct sockaddr *saddr, *smask = 0;
1541 	struct domain *dom;
1542 	int error;
1543 
1544 	if (argp->ex_addrlen == 0) {
1545 		if (mp->mnt_flag & MNT_DEFEXPORTED)
1546 			return (EPERM);
1547 		np = &nep->ne_defexported;
1548 		np->netc_exflags = argp->ex_flags;
1549 		np->netc_anon = argp->ex_anon;
1550 		np->netc_anon.cr_ref = 1;
1551 		mp->mnt_flag |= MNT_DEFEXPORTED;
1552 		return (0);
1553 	}
1554 	i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
1555 	np = (struct netcred *)malloc(i, M_NETADDR, M_WAITOK);
1556 	bzero((caddr_t)np, i);
1557 	saddr = (struct sockaddr *)(np + 1);
1558 	if (error = copyin(argp->ex_addr, (caddr_t)saddr, argp->ex_addrlen))
1559 		goto out;
1560 	if (saddr->sa_len > argp->ex_addrlen)
1561 		saddr->sa_len = argp->ex_addrlen;
1562 	if (argp->ex_masklen) {
1563 		smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen);
1564 		error = copyin(argp->ex_addr, (caddr_t)smask, argp->ex_masklen);
1565 		if (error)
1566 			goto out;
1567 		if (smask->sa_len > argp->ex_masklen)
1568 			smask->sa_len = argp->ex_masklen;
1569 	}
1570 	i = saddr->sa_family;
1571 	if ((rnh = nep->ne_rtable[i]) == 0) {
1572 		/*
1573 		 * Seems silly to initialize every AF when most are not
1574 		 * used, do so on demand here
1575 		 */
1576 		for (dom = domains; dom; dom = dom->dom_next)
1577 			if (dom->dom_family == i && dom->dom_rtattach) {
1578 				dom->dom_rtattach((void **)&nep->ne_rtable[i],
1579 					dom->dom_rtoffset);
1580 				break;
1581 			}
1582 		if ((rnh = nep->ne_rtable[i]) == 0) {
1583 			error = ENOBUFS;
1584 			goto out;
1585 		}
1586 	}
1587 	rn = (*rnh->rnh_addaddr)((caddr_t)saddr, (caddr_t)smask, rnh,
1588 		np->netc_rnodes);
1589 	if (rn == 0) {
1590 		/*
1591 		 * One of the reasons that rnh_addaddr may fail is that
1592 		 * the entry already exists. To check for this case, we
1593 		 * look up the entry to see if it is there. If so, we
1594 		 * do not need to make a new entry but do return success.
1595 		 */
1596 		free(np, M_NETADDR);
1597 		rn = (*rnh->rnh_matchaddr)((caddr_t)saddr, rnh);
1598 		if (rn != 0 && (rn->rn_flags & RNF_ROOT) == 0 &&
1599 		    ((struct netcred *)rn)->netc_exflags == argp->ex_flags &&
1600 		    !bcmp((caddr_t)&((struct netcred *)rn)->netc_anon,
1601 			    (caddr_t)&argp->ex_anon, sizeof(struct ucred)))
1602 			return (0);
1603 		return (EPERM);
1604 	}
1605 	np->netc_exflags = argp->ex_flags;
1606 	np->netc_anon = argp->ex_anon;
1607 	np->netc_anon.cr_ref = 1;
1608 	return (0);
1609 out:
1610 	free(np, M_NETADDR);
1611 	return (error);
1612 }
1613 
1614 /* ARGSUSED */
1615 static int
1616 vfs_free_netcred(rn, w)
1617 	struct radix_node *rn;
1618 	caddr_t w;
1619 {
1620 	register struct radix_node_head *rnh = (struct radix_node_head *)w;
1621 
1622 	(*rnh->rnh_deladdr)(rn->rn_key, rn->rn_mask, rnh);
1623 	free((caddr_t)rn, M_NETADDR);
1624 	return (0);
1625 }
1626 
1627 /*
1628  * Free the net address hash lists that are hanging off the mount points.
1629  */
1630 static void
1631 vfs_free_addrlist(nep)
1632 	struct netexport *nep;
1633 {
1634 	register int i;
1635 	register struct radix_node_head *rnh;
1636 
1637 	for (i = 0; i <= AF_MAX; i++)
1638 		if (rnh = nep->ne_rtable[i]) {
1639 			(*rnh->rnh_walktree)(rnh, vfs_free_netcred,
1640 			    (caddr_t)rnh);
1641 			free((caddr_t)rnh, M_RTABLE);
1642 			nep->ne_rtable[i] = 0;
1643 		}
1644 }
1645 
1646 int
1647 vfs_export(mp, nep, argp)
1648 	struct mount *mp;
1649 	struct netexport *nep;
1650 	struct export_args *argp;
1651 {
1652 	int error;
1653 
1654 	if (argp->ex_flags & MNT_DELEXPORT) {
1655 		vfs_free_addrlist(nep);
1656 		mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
1657 	}
1658 	if (argp->ex_flags & MNT_EXPORTED) {
1659 		if (error = vfs_hang_addrlist(mp, nep, argp))
1660 			return (error);
1661 		mp->mnt_flag |= MNT_EXPORTED;
1662 	}
1663 	return (0);
1664 }
1665 
1666 struct netcred *
1667 vfs_export_lookup(mp, nep, nam)
1668 	register struct mount *mp;
1669 	struct netexport *nep;
1670 	struct mbuf *nam;
1671 {
1672 	register struct netcred *np;
1673 	register struct radix_node_head *rnh;
1674 	struct sockaddr *saddr;
1675 
1676 	np = NULL;
1677 	if (mp->mnt_flag & MNT_EXPORTED) {
1678 		/*
1679 		 * Lookup in the export list first.
1680 		 */
1681 		if (nam != NULL) {
1682 			saddr = mtod(nam, struct sockaddr *);
1683 			rnh = nep->ne_rtable[saddr->sa_family];
1684 			if (rnh != NULL) {
1685 				np = (struct netcred *)
1686 					(*rnh->rnh_matchaddr)((caddr_t)saddr,
1687 							      rnh);
1688 				if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
1689 					np = NULL;
1690 			}
1691 		}
1692 		/*
1693 		 * If no address match, use the default if it exists.
1694 		 */
1695 		if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
1696 			np = &nep->ne_defexported;
1697 	}
1698 	return (np);
1699 }
1700