xref: /original-bsd/sys/kern/vfs_subr.c (revision e58c8952)
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.13 (Berkeley) 04/18/94
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 
57 TAILQ_HEAD(freelst, vnode) vnode_free_list;	/* vnode free list */
58 struct mntlist mountlist;			/* mounted filesystem list */
59 
60 /*
61  * Initialize the vnode management data structures.
62  */
63 vntblinit()
64 {
65 
66 	TAILQ_INIT(&vnode_free_list);
67 	TAILQ_INIT(&mountlist);
68 }
69 
70 /*
71  * Lock a filesystem.
72  * Used to prevent access to it while mounting and unmounting.
73  */
74 vfs_lock(mp)
75 	register struct mount *mp;
76 {
77 
78 	while(mp->mnt_flag & MNT_MLOCK) {
79 		mp->mnt_flag |= MNT_MWAIT;
80 		sleep((caddr_t)mp, PVFS);
81 	}
82 	mp->mnt_flag |= MNT_MLOCK;
83 	return (0);
84 }
85 
86 /*
87  * Unlock a locked filesystem.
88  * Panic if filesystem is not locked.
89  */
90 void
91 vfs_unlock(mp)
92 	register struct mount *mp;
93 {
94 
95 	if ((mp->mnt_flag & MNT_MLOCK) == 0)
96 		panic("vfs_unlock: not locked");
97 	mp->mnt_flag &= ~MNT_MLOCK;
98 	if (mp->mnt_flag & MNT_MWAIT) {
99 		mp->mnt_flag &= ~MNT_MWAIT;
100 		wakeup((caddr_t)mp);
101 	}
102 }
103 
104 /*
105  * Mark a mount point as busy.
106  * Used to synchronize access and to delay unmounting.
107  */
108 vfs_busy(mp)
109 	register struct mount *mp;
110 {
111 
112 	while(mp->mnt_flag & MNT_MPBUSY) {
113 		mp->mnt_flag |= MNT_MPWANT;
114 		sleep((caddr_t)&mp->mnt_flag, PVFS);
115 	}
116 	if (mp->mnt_flag & MNT_UNMOUNT)
117 		return (1);
118 	mp->mnt_flag |= MNT_MPBUSY;
119 	return (0);
120 }
121 
122 /*
123  * Free a busy filesystem.
124  * Panic if filesystem is not busy.
125  */
126 vfs_unbusy(mp)
127 	register struct mount *mp;
128 {
129 
130 	if ((mp->mnt_flag & MNT_MPBUSY) == 0)
131 		panic("vfs_unbusy: not busy");
132 	mp->mnt_flag &= ~MNT_MPBUSY;
133 	if (mp->mnt_flag & MNT_MPWANT) {
134 		mp->mnt_flag &= ~MNT_MPWANT;
135 		wakeup((caddr_t)&mp->mnt_flag);
136 	}
137 }
138 
139 /*
140  * Lookup a mount point by filesystem identifier.
141  */
142 struct mount *
143 getvfs(fsid)
144 	fsid_t *fsid;
145 {
146 	register struct mount *mp;
147 
148 	for (mp = mountlist.tqh_first; mp != NULL; mp = mp->mnt_list.tqe_next) {
149 		if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
150 		    mp->mnt_stat.f_fsid.val[1] == fsid->val[1])
151 			return (mp);
152 	}
153 	return ((struct mount *)0);
154 }
155 
156 /*
157  * Get a new unique fsid
158  */
159 void
160 getnewfsid(mp, mtype)
161 	struct mount *mp;
162 	int mtype;
163 {
164 static u_short xxxfs_mntid;
165 
166 	fsid_t tfsid;
167 
168 	mp->mnt_stat.f_fsid.val[0] = makedev(nblkdev + mtype, 0);
169 	mp->mnt_stat.f_fsid.val[1] = mtype;
170 	if (xxxfs_mntid == 0)
171 		++xxxfs_mntid;
172 	tfsid.val[0] = makedev(nblkdev + mtype, xxxfs_mntid);
173 	tfsid.val[1] = mtype;
174 	if (mountlist.tqh_first != NULL) {
175 		while (getvfs(&tfsid)) {
176 			tfsid.val[0]++;
177 			xxxfs_mntid++;
178 		}
179 	}
180 	mp->mnt_stat.f_fsid.val[0] = tfsid.val[0];
181 }
182 
183 /*
184  * Set vnode attributes to VNOVAL
185  */
186 void vattr_null(vap)
187 	register struct vattr *vap;
188 {
189 
190 	vap->va_type = VNON;
191 	vap->va_size = vap->va_bytes = VNOVAL;
192 	vap->va_mode = vap->va_nlink = vap->va_uid = vap->va_gid =
193 		vap->va_fsid = vap->va_fileid =
194 		vap->va_blocksize = vap->va_rdev =
195 		vap->va_atime.ts_sec = vap->va_atime.ts_nsec =
196 		vap->va_mtime.ts_sec = vap->va_mtime.ts_nsec =
197 		vap->va_ctime.ts_sec = vap->va_ctime.ts_nsec =
198 		vap->va_flags = vap->va_gen = VNOVAL;
199 	vap->va_vaflags = 0;
200 }
201 
202 /*
203  * Routines having to do with the management of the vnode table.
204  */
205 extern int (**dead_vnodeop_p)();
206 extern void vclean();
207 long numvnodes;
208 extern struct vattr va_null;
209 
210 /*
211  * Return the next vnode from the free list.
212  */
213 getnewvnode(tag, mp, vops, vpp)
214 	enum vtagtype tag;
215 	struct mount *mp;
216 	int (**vops)();
217 	struct vnode **vpp;
218 {
219 	register struct vnode *vp;
220 	int s;
221 
222 	if ((vnode_free_list.tqh_first == NULL &&
223 	     numvnodes < 2 * desiredvnodes) ||
224 	    numvnodes < desiredvnodes) {
225 		vp = (struct vnode *)malloc((u_long)sizeof *vp,
226 		    M_VNODE, M_WAITOK);
227 		bzero((char *)vp, sizeof *vp);
228 		numvnodes++;
229 	} else {
230 		if ((vp = vnode_free_list.tqh_first) == NULL) {
231 			tablefull("vnode");
232 			*vpp = 0;
233 			return (ENFILE);
234 		}
235 		if (vp->v_usecount)
236 			panic("free vnode isn't");
237 		TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
238 		/* see comment on why 0xdeadb is set at end of vgone (below) */
239 		vp->v_freelist.tqe_prev = (struct vnode **)0xdeadb;
240 		vp->v_lease = NULL;
241 		if (vp->v_type != VBAD)
242 			vgone(vp);
243 #ifdef DIAGNOSTIC
244 		if (vp->v_data)
245 			panic("cleaned vnode isn't");
246 		s = splbio();
247 		if (vp->v_numoutput)
248 			panic("Clean vnode has pending I/O's");
249 		splx(s);
250 #endif
251 		vp->v_flag = 0;
252 		vp->v_lastr = 0;
253 		vp->v_ralen = 0;
254 		vp->v_maxra = 0;
255 		vp->v_lastw = 0;
256 		vp->v_lasta = 0;
257 		vp->v_cstart = 0;
258 		vp->v_clen = 0;
259 		vp->v_socket = 0;
260 	}
261 	vp->v_type = VNON;
262 	cache_purge(vp);
263 	vp->v_tag = tag;
264 	vp->v_op = vops;
265 	insmntque(vp, mp);
266 	*vpp = vp;
267 	vp->v_usecount = 1;
268 	vp->v_data = 0;
269 	return (0);
270 }
271 
272 /*
273  * Move a vnode from one mount queue to another.
274  */
275 insmntque(vp, mp)
276 	register struct vnode *vp;
277 	register struct mount *mp;
278 {
279 
280 	/*
281 	 * Delete from old mount point vnode list, if on one.
282 	 */
283 	if (vp->v_mount != NULL)
284 		LIST_REMOVE(vp, v_mntvnodes);
285 	/*
286 	 * Insert into list of vnodes for the new mount point, if available.
287 	 */
288 	if ((vp->v_mount = mp) == NULL)
289 		return;
290 	LIST_INSERT_HEAD(&mp->mnt_vnodelist, vp, v_mntvnodes);
291 }
292 
293 /*
294  * Update outstanding I/O count and do wakeup if requested.
295  */
296 vwakeup(bp)
297 	register struct buf *bp;
298 {
299 	register struct vnode *vp;
300 
301 	bp->b_flags &= ~B_WRITEINPROG;
302 	if (vp = bp->b_vp) {
303 		vp->v_numoutput--;
304 		if (vp->v_numoutput < 0)
305 			panic("vwakeup: neg numoutput");
306 		if ((vp->v_flag & VBWAIT) && vp->v_numoutput <= 0) {
307 			if (vp->v_numoutput < 0)
308 				panic("vwakeup: neg numoutput");
309 			vp->v_flag &= ~VBWAIT;
310 			wakeup((caddr_t)&vp->v_numoutput);
311 		}
312 	}
313 }
314 
315 /*
316  * Flush out and invalidate all buffers associated with a vnode.
317  * Called with the underlying object locked.
318  */
319 int
320 vinvalbuf(vp, flags, cred, p, slpflag, slptimeo)
321 	register struct vnode *vp;
322 	int flags;
323 	struct ucred *cred;
324 	struct proc *p;
325 	int slpflag, slptimeo;
326 {
327 	register struct buf *bp;
328 	struct buf *nbp, *blist;
329 	int s, error;
330 
331 	if (flags & V_SAVE) {
332 		if (error = VOP_FSYNC(vp, cred, MNT_WAIT, p))
333 			return (error);
334 		if (vp->v_dirtyblkhd.lh_first != NULL)
335 			panic("vinvalbuf: dirty bufs");
336 	}
337 	for (;;) {
338 		if ((blist = vp->v_cleanblkhd.lh_first) && flags & V_SAVEMETA)
339 			while (blist && blist->b_lblkno < 0)
340 				blist = blist->b_vnbufs.le_next;
341 		if (!blist && (blist = vp->v_dirtyblkhd.lh_first) &&
342 		    (flags & V_SAVEMETA))
343 			while (blist && blist->b_lblkno < 0)
344 				blist = blist->b_vnbufs.le_next;
345 		if (!blist)
346 			break;
347 
348 		for (bp = blist; bp; bp = nbp) {
349 			nbp = bp->b_vnbufs.le_next;
350 			if (flags & V_SAVEMETA && bp->b_lblkno < 0)
351 				continue;
352 			s = splbio();
353 			if (bp->b_flags & B_BUSY) {
354 				bp->b_flags |= B_WANTED;
355 				error = tsleep((caddr_t)bp,
356 					slpflag | (PRIBIO + 1), "vinvalbuf",
357 					slptimeo);
358 				splx(s);
359 				if (error)
360 					return (error);
361 				break;
362 			}
363 			bremfree(bp);
364 			bp->b_flags |= B_BUSY;
365 			splx(s);
366 			/*
367 			 * XXX Since there are no node locks for NFS, I believe
368 			 * there is a slight chance that a delayed write will
369 			 * occur while sleeping just above, so check for it.
370 			 */
371 			if ((bp->b_flags & B_DELWRI) && (flags & V_SAVE)) {
372 				(void) VOP_BWRITE(bp);
373 				break;
374 			}
375 			bp->b_flags |= B_INVAL;
376 			brelse(bp);
377 		}
378 	}
379 	if (!(flags & V_SAVEMETA) &&
380 	    (vp->v_dirtyblkhd.lh_first || vp->v_cleanblkhd.lh_first))
381 		panic("vinvalbuf: flush failed");
382 	return (0);
383 }
384 
385 /*
386  * Associate a buffer with a vnode.
387  */
388 bgetvp(vp, bp)
389 	register struct vnode *vp;
390 	register struct buf *bp;
391 {
392 
393 	if (bp->b_vp)
394 		panic("bgetvp: not free");
395 	VHOLD(vp);
396 	bp->b_vp = vp;
397 	if (vp->v_type == VBLK || vp->v_type == VCHR)
398 		bp->b_dev = vp->v_rdev;
399 	else
400 		bp->b_dev = NODEV;
401 	/*
402 	 * Insert onto list for new vnode.
403 	 */
404 	bufinsvn(bp, &vp->v_cleanblkhd);
405 }
406 
407 /*
408  * Disassociate a buffer from a vnode.
409  */
410 brelvp(bp)
411 	register struct buf *bp;
412 {
413 	struct vnode *vp;
414 
415 	if (bp->b_vp == (struct vnode *) 0)
416 		panic("brelvp: NULL");
417 	/*
418 	 * Delete from old vnode list, if on one.
419 	 */
420 	if (bp->b_vnbufs.le_next != NOLIST)
421 		bufremvn(bp);
422 	vp = bp->b_vp;
423 	bp->b_vp = (struct vnode *) 0;
424 	HOLDRELE(vp);
425 }
426 
427 /*
428  * Reassign a buffer from one vnode to another.
429  * Used to assign file specific control information
430  * (indirect blocks) to the vnode to which they belong.
431  */
432 reassignbuf(bp, newvp)
433 	register struct buf *bp;
434 	register struct vnode *newvp;
435 {
436 	register struct buflists *listheadp;
437 
438 	if (newvp == NULL) {
439 		printf("reassignbuf: NULL");
440 		return;
441 	}
442 	/*
443 	 * Delete from old vnode list, if on one.
444 	 */
445 	if (bp->b_vnbufs.le_next != NOLIST)
446 		bufremvn(bp);
447 	/*
448 	 * If dirty, put on list of dirty buffers;
449 	 * otherwise insert onto list of clean buffers.
450 	 */
451 	if (bp->b_flags & B_DELWRI)
452 		listheadp = &newvp->v_dirtyblkhd;
453 	else
454 		listheadp = &newvp->v_cleanblkhd;
455 	bufinsvn(bp, listheadp);
456 }
457 
458 /*
459  * Create a vnode for a block device.
460  * Used for root filesystem, argdev, and swap areas.
461  * Also used for memory file system special devices.
462  */
463 bdevvp(dev, vpp)
464 	dev_t dev;
465 	struct vnode **vpp;
466 {
467 	register struct vnode *vp;
468 	struct vnode *nvp;
469 	int error;
470 
471 	if (dev == NODEV)
472 		return (0);
473 	error = getnewvnode(VT_NON, (struct mount *)0, spec_vnodeop_p, &nvp);
474 	if (error) {
475 		*vpp = 0;
476 		return (error);
477 	}
478 	vp = nvp;
479 	vp->v_type = VBLK;
480 	if (nvp = checkalias(vp, dev, (struct mount *)0)) {
481 		vput(vp);
482 		vp = nvp;
483 	}
484 	*vpp = vp;
485 	return (0);
486 }
487 
488 /*
489  * Check to see if the new vnode represents a special device
490  * for which we already have a vnode (either because of
491  * bdevvp() or because of a different vnode representing
492  * the same block device). If such an alias exists, deallocate
493  * the existing contents and return the aliased vnode. The
494  * caller is responsible for filling it with its new contents.
495  */
496 struct vnode *
497 checkalias(nvp, nvp_rdev, mp)
498 	register struct vnode *nvp;
499 	dev_t nvp_rdev;
500 	struct mount *mp;
501 {
502 	register struct vnode *vp;
503 	struct vnode **vpp;
504 
505 	if (nvp->v_type != VBLK && nvp->v_type != VCHR)
506 		return (NULLVP);
507 
508 	vpp = &speclisth[SPECHASH(nvp_rdev)];
509 loop:
510 	for (vp = *vpp; vp; vp = vp->v_specnext) {
511 		if (nvp_rdev != vp->v_rdev || nvp->v_type != vp->v_type)
512 			continue;
513 		/*
514 		 * Alias, but not in use, so flush it out.
515 		 */
516 		if (vp->v_usecount == 0) {
517 			vgone(vp);
518 			goto loop;
519 		}
520 		if (vget(vp, 1))
521 			goto loop;
522 		break;
523 	}
524 	if (vp == NULL || vp->v_tag != VT_NON) {
525 		MALLOC(nvp->v_specinfo, struct specinfo *,
526 			sizeof(struct specinfo), M_VNODE, M_WAITOK);
527 		nvp->v_rdev = nvp_rdev;
528 		nvp->v_hashchain = vpp;
529 		nvp->v_specnext = *vpp;
530 		nvp->v_specflags = 0;
531 		*vpp = nvp;
532 		if (vp != NULL) {
533 			nvp->v_flag |= VALIASED;
534 			vp->v_flag |= VALIASED;
535 			vput(vp);
536 		}
537 		return (NULLVP);
538 	}
539 	VOP_UNLOCK(vp);
540 	vclean(vp, 0);
541 	vp->v_op = nvp->v_op;
542 	vp->v_tag = nvp->v_tag;
543 	nvp->v_type = VNON;
544 	insmntque(vp, mp);
545 	return (vp);
546 }
547 
548 /*
549  * Grab a particular vnode from the free list, increment its
550  * reference count and lock it. The vnode lock bit is set the
551  * vnode is being eliminated in vgone. The process is awakened
552  * when the transition is completed, and an error returned to
553  * indicate that the vnode is no longer usable (possibly having
554  * been changed to a new file system type).
555  */
556 vget(vp, lockflag)
557 	register struct vnode *vp;
558 	int lockflag;
559 {
560 
561 	/*
562 	 * If the vnode is in the process of being cleaned out for
563 	 * another use, we wait for the cleaning to finish and then
564 	 * return failure. Cleaning is determined either by checking
565 	 * that the VXLOCK flag is set, or that the use count is
566 	 * zero with the back pointer set to show that it has been
567 	 * removed from the free list by getnewvnode. The VXLOCK
568 	 * flag may not have been set yet because vclean is blocked in
569 	 * the VOP_LOCK call waiting for the VOP_INACTIVE to complete.
570 	 */
571 	if ((vp->v_flag & VXLOCK) ||
572 	    (vp->v_usecount == 0 &&
573 	     vp->v_freelist.tqe_prev == (struct vnode **)0xdeadb)) {
574 		vp->v_flag |= VXWANT;
575 		sleep((caddr_t)vp, PINOD);
576 		return (1);
577 	}
578 	if (vp->v_usecount == 0)
579 		TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
580 	vp->v_usecount++;
581 	if (lockflag)
582 		VOP_LOCK(vp);
583 	return (0);
584 }
585 
586 /*
587  * Vnode reference, just increment the count
588  */
589 void vref(vp)
590 	struct vnode *vp;
591 {
592 
593 	if (vp->v_usecount <= 0)
594 		panic("vref used where vget required");
595 	vp->v_usecount++;
596 }
597 
598 /*
599  * vput(), just unlock and vrele()
600  */
601 void vput(vp)
602 	register struct vnode *vp;
603 {
604 
605 	VOP_UNLOCK(vp);
606 	vrele(vp);
607 }
608 
609 /*
610  * Vnode release.
611  * If count drops to zero, call inactive routine and return to freelist.
612  */
613 void vrele(vp)
614 	register struct vnode *vp;
615 {
616 
617 #ifdef DIAGNOSTIC
618 	if (vp == NULL)
619 		panic("vrele: null vp");
620 #endif
621 	vp->v_usecount--;
622 	if (vp->v_usecount > 0)
623 		return;
624 #ifdef DIAGNOSTIC
625 	if (vp->v_usecount != 0 || vp->v_writecount != 0) {
626 		vprint("vrele: bad ref count", vp);
627 		panic("vrele: ref cnt");
628 	}
629 #endif
630 	/*
631 	 * insert at tail of LRU list
632 	 */
633 	TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
634 	VOP_INACTIVE(vp);
635 }
636 
637 /*
638  * Page or buffer structure gets a reference.
639  */
640 void vhold(vp)
641 	register struct vnode *vp;
642 {
643 
644 	vp->v_holdcnt++;
645 }
646 
647 /*
648  * Page or buffer structure frees a reference.
649  */
650 void holdrele(vp)
651 	register struct vnode *vp;
652 {
653 
654 	if (vp->v_holdcnt <= 0)
655 		panic("holdrele: holdcnt");
656 	vp->v_holdcnt--;
657 }
658 
659 /*
660  * Remove any vnodes in the vnode table belonging to mount point mp.
661  *
662  * If MNT_NOFORCE is specified, there should not be any active ones,
663  * return error if any are found (nb: this is a user error, not a
664  * system error). If MNT_FORCE is specified, detach any active vnodes
665  * that are found.
666  */
667 #ifdef DIAGNOSTIC
668 int busyprt = 0;	/* print out busy vnodes */
669 struct ctldebug debug1 = { "busyprt", &busyprt };
670 #endif
671 
672 vflush(mp, skipvp, flags)
673 	struct mount *mp;
674 	struct vnode *skipvp;
675 	int flags;
676 {
677 	register struct vnode *vp, *nvp;
678 	int busy = 0;
679 
680 	if ((mp->mnt_flag & MNT_MPBUSY) == 0)
681 		panic("vflush: not busy");
682 loop:
683 	for (vp = mp->mnt_vnodelist.lh_first; vp; vp = nvp) {
684 		if (vp->v_mount != mp)
685 			goto loop;
686 		nvp = vp->v_mntvnodes.le_next;
687 		/*
688 		 * Skip over a selected vnode.
689 		 */
690 		if (vp == skipvp)
691 			continue;
692 		/*
693 		 * Skip over a vnodes marked VSYSTEM.
694 		 */
695 		if ((flags & SKIPSYSTEM) && (vp->v_flag & VSYSTEM))
696 			continue;
697 		/*
698 		 * If WRITECLOSE is set, only flush out regular file
699 		 * vnodes open for writing.
700 		 */
701 		if ((flags & WRITECLOSE) &&
702 		    (vp->v_writecount == 0 || vp->v_type != VREG))
703 			continue;
704 		/*
705 		 * With v_usecount == 0, all we need to do is clear
706 		 * out the vnode data structures and we are done.
707 		 */
708 		if (vp->v_usecount == 0) {
709 			vgone(vp);
710 			continue;
711 		}
712 		/*
713 		 * If FORCECLOSE is set, forcibly close the vnode.
714 		 * For block or character devices, revert to an
715 		 * anonymous device. For all other files, just kill them.
716 		 */
717 		if (flags & FORCECLOSE) {
718 			if (vp->v_type != VBLK && vp->v_type != VCHR) {
719 				vgone(vp);
720 			} else {
721 				vclean(vp, 0);
722 				vp->v_op = spec_vnodeop_p;
723 				insmntque(vp, (struct mount *)0);
724 			}
725 			continue;
726 		}
727 #ifdef DIAGNOSTIC
728 		if (busyprt)
729 			vprint("vflush: busy vnode", vp);
730 #endif
731 		busy++;
732 	}
733 	if (busy)
734 		return (EBUSY);
735 	return (0);
736 }
737 
738 /*
739  * Disassociate the underlying file system from a vnode.
740  */
741 void
742 vclean(vp, flags)
743 	register struct vnode *vp;
744 	int flags;
745 {
746 	int active;
747 
748 	/*
749 	 * Check to see if the vnode is in use.
750 	 * If so we have to reference it before we clean it out
751 	 * so that its count cannot fall to zero and generate a
752 	 * race against ourselves to recycle it.
753 	 */
754 	if (active = vp->v_usecount)
755 		VREF(vp);
756 	/*
757 	 * Even if the count is zero, the VOP_INACTIVE routine may still
758 	 * have the object locked while it cleans it out. The VOP_LOCK
759 	 * ensures that the VOP_INACTIVE routine is done with its work.
760 	 * For active vnodes, it ensures that no other activity can
761 	 * occur while the underlying object is being cleaned out.
762 	 */
763 	VOP_LOCK(vp);
764 	/*
765 	 * Prevent the vnode from being recycled or
766 	 * brought into use while we clean it out.
767 	 */
768 	if (vp->v_flag & VXLOCK)
769 		panic("vclean: deadlock");
770 	vp->v_flag |= VXLOCK;
771 	/*
772 	 * Clean out any buffers associated with the vnode.
773 	 */
774 	if (flags & DOCLOSE)
775 		vinvalbuf(vp, V_SAVE, NOCRED, NULL, 0, 0);
776 	/*
777 	 * Any other processes trying to obtain this lock must first
778 	 * wait for VXLOCK to clear, then call the new lock operation.
779 	 */
780 	VOP_UNLOCK(vp);
781 	/*
782 	 * If purging an active vnode, it must be closed and
783 	 * deactivated before being reclaimed.
784 	 */
785 	if (active) {
786 		if (flags & DOCLOSE)
787 			VOP_CLOSE(vp, IO_NDELAY, NOCRED, NULL);
788 		VOP_INACTIVE(vp);
789 	}
790 	/*
791 	 * Reclaim the vnode.
792 	 */
793 	if (VOP_RECLAIM(vp))
794 		panic("vclean: cannot reclaim");
795 	if (active)
796 		vrele(vp);
797 
798 	/*
799 	 * Done with purge, notify sleepers of the grim news.
800 	 */
801 	vp->v_op = dead_vnodeop_p;
802 	vp->v_tag = VT_NON;
803 	vp->v_flag &= ~VXLOCK;
804 	if (vp->v_flag & VXWANT) {
805 		vp->v_flag &= ~VXWANT;
806 		wakeup((caddr_t)vp);
807 	}
808 }
809 
810 /*
811  * Eliminate all activity associated with  the requested vnode
812  * and with all vnodes aliased to the requested vnode.
813  */
814 void vgoneall(vp)
815 	register struct vnode *vp;
816 {
817 	register struct vnode *vq;
818 
819 	if (vp->v_flag & VALIASED) {
820 		/*
821 		 * If a vgone (or vclean) is already in progress,
822 		 * wait until it is done and return.
823 		 */
824 		if (vp->v_flag & VXLOCK) {
825 			vp->v_flag |= VXWANT;
826 			sleep((caddr_t)vp, PINOD);
827 			return;
828 		}
829 		/*
830 		 * Ensure that vp will not be vgone'd while we
831 		 * are eliminating its aliases.
832 		 */
833 		vp->v_flag |= VXLOCK;
834 		while (vp->v_flag & VALIASED) {
835 			for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
836 				if (vq->v_rdev != vp->v_rdev ||
837 				    vq->v_type != vp->v_type || vp == vq)
838 					continue;
839 				vgone(vq);
840 				break;
841 			}
842 		}
843 		/*
844 		 * Remove the lock so that vgone below will
845 		 * really eliminate the vnode after which time
846 		 * vgone will awaken any sleepers.
847 		 */
848 		vp->v_flag &= ~VXLOCK;
849 	}
850 	vgone(vp);
851 }
852 
853 /*
854  * Eliminate all activity associated with a vnode
855  * in preparation for reuse.
856  */
857 void vgone(vp)
858 	register struct vnode *vp;
859 {
860 	register struct vnode *vq;
861 	struct vnode *vx;
862 
863 	/*
864 	 * If a vgone (or vclean) is already in progress,
865 	 * wait until it is done and return.
866 	 */
867 	if (vp->v_flag & VXLOCK) {
868 		vp->v_flag |= VXWANT;
869 		sleep((caddr_t)vp, PINOD);
870 		return;
871 	}
872 	/*
873 	 * Clean out the filesystem specific data.
874 	 */
875 	vclean(vp, DOCLOSE);
876 	/*
877 	 * Delete from old mount point vnode list, if on one.
878 	 */
879 	if (vp->v_mount != NULL) {
880 		LIST_REMOVE(vp, v_mntvnodes);
881 		vp->v_mount = NULL;
882 	}
883 	/*
884 	 * If special device, remove it from special device alias list.
885 	 */
886 	if (vp->v_type == VBLK || vp->v_type == VCHR) {
887 		if (*vp->v_hashchain == vp) {
888 			*vp->v_hashchain = vp->v_specnext;
889 		} else {
890 			for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
891 				if (vq->v_specnext != vp)
892 					continue;
893 				vq->v_specnext = vp->v_specnext;
894 				break;
895 			}
896 			if (vq == NULL)
897 				panic("missing bdev");
898 		}
899 		if (vp->v_flag & VALIASED) {
900 			vx = NULL;
901 			for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
902 				if (vq->v_rdev != vp->v_rdev ||
903 				    vq->v_type != vp->v_type)
904 					continue;
905 				if (vx)
906 					break;
907 				vx = vq;
908 			}
909 			if (vx == NULL)
910 				panic("missing alias");
911 			if (vq == NULL)
912 				vx->v_flag &= ~VALIASED;
913 			vp->v_flag &= ~VALIASED;
914 		}
915 		FREE(vp->v_specinfo, M_VNODE);
916 		vp->v_specinfo = NULL;
917 	}
918 	/*
919 	 * If it is on the freelist and not already at the head,
920 	 * move it to the head of the list. The test of the back
921 	 * pointer and the reference count of zero is because
922 	 * it will be removed from the free list by getnewvnode,
923 	 * but will not have its reference count incremented until
924 	 * after calling vgone. If the reference count were
925 	 * incremented first, vgone would (incorrectly) try to
926 	 * close the previous instance of the underlying object.
927 	 * So, the back pointer is explicitly set to `0xdeadb' in
928 	 * getnewvnode after removing it from the freelist to ensure
929 	 * that we do not try to move it here.
930 	 */
931 	if (vp->v_usecount == 0 &&
932 	    vp->v_freelist.tqe_prev != (struct vnode **)0xdeadb &&
933 	    vnode_free_list.tqh_first != vp) {
934 		TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
935 		TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
936 	}
937 	vp->v_type = VBAD;
938 }
939 
940 /*
941  * Lookup a vnode by device number.
942  */
943 vfinddev(dev, type, vpp)
944 	dev_t dev;
945 	enum vtype type;
946 	struct vnode **vpp;
947 {
948 	register struct vnode *vp;
949 
950 	for (vp = speclisth[SPECHASH(dev)]; vp; vp = vp->v_specnext) {
951 		if (dev != vp->v_rdev || type != vp->v_type)
952 			continue;
953 		*vpp = vp;
954 		return (1);
955 	}
956 	return (0);
957 }
958 
959 /*
960  * Calculate the total number of references to a special device.
961  */
962 vcount(vp)
963 	register struct vnode *vp;
964 {
965 	register struct vnode *vq, *vnext;
966 	int count;
967 
968 loop:
969 	if ((vp->v_flag & VALIASED) == 0)
970 		return (vp->v_usecount);
971 	for (count = 0, vq = *vp->v_hashchain; vq; vq = vnext) {
972 		vnext = vq->v_specnext;
973 		if (vq->v_rdev != vp->v_rdev || vq->v_type != vp->v_type)
974 			continue;
975 		/*
976 		 * Alias, but not in use, so flush it out.
977 		 */
978 		if (vq->v_usecount == 0 && vq != vp) {
979 			vgone(vq);
980 			goto loop;
981 		}
982 		count += vq->v_usecount;
983 	}
984 	return (count);
985 }
986 
987 /*
988  * Print out a description of a vnode.
989  */
990 static char *typename[] =
991    { "VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD" };
992 
993 vprint(label, vp)
994 	char *label;
995 	register struct vnode *vp;
996 {
997 	char buf[64];
998 
999 	if (label != NULL)
1000 		printf("%s: ", label);
1001 	printf("type %s, usecount %d, writecount %d, refcount %d,",
1002 		typename[vp->v_type], vp->v_usecount, vp->v_writecount,
1003 		vp->v_holdcnt);
1004 	buf[0] = '\0';
1005 	if (vp->v_flag & VROOT)
1006 		strcat(buf, "|VROOT");
1007 	if (vp->v_flag & VTEXT)
1008 		strcat(buf, "|VTEXT");
1009 	if (vp->v_flag & VSYSTEM)
1010 		strcat(buf, "|VSYSTEM");
1011 	if (vp->v_flag & VXLOCK)
1012 		strcat(buf, "|VXLOCK");
1013 	if (vp->v_flag & VXWANT)
1014 		strcat(buf, "|VXWANT");
1015 	if (vp->v_flag & VBWAIT)
1016 		strcat(buf, "|VBWAIT");
1017 	if (vp->v_flag & VALIASED)
1018 		strcat(buf, "|VALIASED");
1019 	if (buf[0] != '\0')
1020 		printf(" flags (%s)", &buf[1]);
1021 	if (vp->v_data == NULL) {
1022 		printf("\n");
1023 	} else {
1024 		printf("\n\t");
1025 		VOP_PRINT(vp);
1026 	}
1027 }
1028 
1029 #ifdef DEBUG
1030 /*
1031  * List all of the locked vnodes in the system.
1032  * Called when debugging the kernel.
1033  */
1034 printlockedvnodes()
1035 {
1036 	register struct mount *mp;
1037 	register struct vnode *vp;
1038 
1039 	printf("Locked vnodes\n");
1040 	for (mp = mountlist.tqh_first; mp != NULL; mp = mp->mnt_list.tqe_next) {
1041 		for (vp = mp->mnt_vnodelist.lh_first;
1042 		     vp != NULL;
1043 		     vp = vp->v_mntvnodes.le_next)
1044 			if (VOP_ISLOCKED(vp))
1045 				vprint((char *)0, vp);
1046 	}
1047 }
1048 #endif
1049 
1050 int kinfo_vdebug = 1;
1051 int kinfo_vgetfailed;
1052 #define KINFO_VNODESLOP	10
1053 /*
1054  * Dump vnode list (via sysctl).
1055  * Copyout address of vnode followed by vnode.
1056  */
1057 /* ARGSUSED */
1058 sysctl_vnode(where, sizep)
1059 	char *where;
1060 	size_t *sizep;
1061 {
1062 	register struct mount *mp, *nmp;
1063 	struct vnode *vp;
1064 	register char *bp = where, *savebp;
1065 	char *ewhere;
1066 	int error;
1067 
1068 #define VPTRSZ	sizeof (struct vnode *)
1069 #define VNODESZ	sizeof (struct vnode)
1070 	if (where == NULL) {
1071 		*sizep = (numvnodes + KINFO_VNODESLOP) * (VPTRSZ + VNODESZ);
1072 		return (0);
1073 	}
1074 	ewhere = where + *sizep;
1075 
1076 	for (mp = mountlist.tqh_first; mp != NULL; mp = nmp) {
1077 		nmp = mp->mnt_list.tqe_next;
1078 		if (vfs_busy(mp))
1079 			continue;
1080 		savebp = bp;
1081 again:
1082 		for (vp = mp->mnt_vnodelist.lh_first;
1083 		     vp != NULL;
1084 		     vp = vp->v_mntvnodes.le_next) {
1085 			/*
1086 			 * Check that the vp is still associated with
1087 			 * this filesystem.  RACE: could have been
1088 			 * recycled onto the same filesystem.
1089 			 */
1090 			if (vp->v_mount != mp) {
1091 				if (kinfo_vdebug)
1092 					printf("kinfo: vp changed\n");
1093 				bp = savebp;
1094 				goto again;
1095 			}
1096 			if (bp + VPTRSZ + VNODESZ > ewhere) {
1097 				*sizep = bp - where;
1098 				return (ENOMEM);
1099 			}
1100 			if ((error = copyout((caddr_t)&vp, bp, VPTRSZ)) ||
1101 			   (error = copyout((caddr_t)vp, bp + VPTRSZ, VNODESZ)))
1102 				return (error);
1103 			bp += VPTRSZ + VNODESZ;
1104 		}
1105 		vfs_unbusy(mp);
1106 	}
1107 
1108 	*sizep = bp - where;
1109 	return (0);
1110 }
1111 
1112 /*
1113  * Check to see if a filesystem is mounted on a block device.
1114  */
1115 int
1116 vfs_mountedon(vp)
1117 	register struct vnode *vp;
1118 {
1119 	register struct vnode *vq;
1120 
1121 	if (vp->v_specflags & SI_MOUNTEDON)
1122 		return (EBUSY);
1123 	if (vp->v_flag & VALIASED) {
1124 		for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
1125 			if (vq->v_rdev != vp->v_rdev ||
1126 			    vq->v_type != vp->v_type)
1127 				continue;
1128 			if (vq->v_specflags & SI_MOUNTEDON)
1129 				return (EBUSY);
1130 		}
1131 	}
1132 	return (0);
1133 }
1134 
1135 /*
1136  * Build hash lists of net addresses and hang them off the mount point.
1137  * Called by ufs_mount() to set up the lists of export addresses.
1138  */
1139 static int
1140 vfs_hang_addrlist(mp, nep, argp)
1141 	struct mount *mp;
1142 	struct netexport *nep;
1143 	struct export_args *argp;
1144 {
1145 	register struct netcred *np;
1146 	register struct radix_node_head *rnh;
1147 	register int i;
1148 	struct radix_node *rn;
1149 	struct sockaddr *saddr, *smask = 0;
1150 	struct domain *dom;
1151 	int error;
1152 
1153 	if (argp->ex_addrlen == 0) {
1154 		if (mp->mnt_flag & MNT_DEFEXPORTED)
1155 			return (EPERM);
1156 		np = &nep->ne_defexported;
1157 		np->netc_exflags = argp->ex_flags;
1158 		np->netc_anon = argp->ex_anon;
1159 		np->netc_anon.cr_ref = 1;
1160 		mp->mnt_flag |= MNT_DEFEXPORTED;
1161 		return (0);
1162 	}
1163 	i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
1164 	np = (struct netcred *)malloc(i, M_NETADDR, M_WAITOK);
1165 	bzero((caddr_t)np, i);
1166 	saddr = (struct sockaddr *)(np + 1);
1167 	if (error = copyin(argp->ex_addr, (caddr_t)saddr, argp->ex_addrlen))
1168 		goto out;
1169 	if (saddr->sa_len > argp->ex_addrlen)
1170 		saddr->sa_len = argp->ex_addrlen;
1171 	if (argp->ex_masklen) {
1172 		smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen);
1173 		error = copyin(argp->ex_addr, (caddr_t)smask, argp->ex_masklen);
1174 		if (error)
1175 			goto out;
1176 		if (smask->sa_len > argp->ex_masklen)
1177 			smask->sa_len = argp->ex_masklen;
1178 	}
1179 	i = saddr->sa_family;
1180 	if ((rnh = nep->ne_rtable[i]) == 0) {
1181 		/*
1182 		 * Seems silly to initialize every AF when most are not
1183 		 * used, do so on demand here
1184 		 */
1185 		for (dom = domains; dom; dom = dom->dom_next)
1186 			if (dom->dom_family == i && dom->dom_rtattach) {
1187 				dom->dom_rtattach((void **)&nep->ne_rtable[i],
1188 					dom->dom_rtoffset);
1189 				break;
1190 			}
1191 		if ((rnh = nep->ne_rtable[i]) == 0) {
1192 			error = ENOBUFS;
1193 			goto out;
1194 		}
1195 	}
1196 	rn = (*rnh->rnh_addaddr)((caddr_t)saddr, (caddr_t)smask, rnh,
1197 		np->netc_rnodes);
1198 	if (rn == 0 || np != (struct netcred *)rn) { /* already exists */
1199 		error = EPERM;
1200 		goto out;
1201 	}
1202 	np->netc_exflags = argp->ex_flags;
1203 	np->netc_anon = argp->ex_anon;
1204 	np->netc_anon.cr_ref = 1;
1205 	return (0);
1206 out:
1207 	free(np, M_NETADDR);
1208 	return (error);
1209 }
1210 
1211 /* ARGSUSED */
1212 static int
1213 vfs_free_netcred(rn, w)
1214 	struct radix_node *rn;
1215 	caddr_t w;
1216 {
1217 	register struct radix_node_head *rnh = (struct radix_node_head *)w;
1218 
1219 	(*rnh->rnh_deladdr)(rn->rn_key, rn->rn_mask, rnh);
1220 	free((caddr_t)rn, M_NETADDR);
1221 	return (0);
1222 }
1223 
1224 /*
1225  * Free the net address hash lists that are hanging off the mount points.
1226  */
1227 static void
1228 vfs_free_addrlist(nep)
1229 	struct netexport *nep;
1230 {
1231 	register int i;
1232 	register struct radix_node_head *rnh;
1233 
1234 	for (i = 0; i <= AF_MAX; i++)
1235 		if (rnh = nep->ne_rtable[i]) {
1236 			(*rnh->rnh_walktree)(rnh, vfs_free_netcred,
1237 			    (caddr_t)rnh);
1238 			free((caddr_t)rnh, M_RTABLE);
1239 			nep->ne_rtable[i] = 0;
1240 		}
1241 }
1242 
1243 int
1244 vfs_export(mp, nep, argp)
1245 	struct mount *mp;
1246 	struct netexport *nep;
1247 	struct export_args *argp;
1248 {
1249 	int error;
1250 
1251 	if (argp->ex_flags & MNT_DELEXPORT) {
1252 		vfs_free_addrlist(nep);
1253 		mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
1254 	}
1255 	if (argp->ex_flags & MNT_EXPORTED) {
1256 		if (error = vfs_hang_addrlist(mp, nep, argp))
1257 			return (error);
1258 		mp->mnt_flag |= MNT_EXPORTED;
1259 	}
1260 	return (0);
1261 }
1262 
1263 struct netcred *
1264 vfs_export_lookup(mp, nep, nam)
1265 	register struct mount *mp;
1266 	struct netexport *nep;
1267 	struct mbuf *nam;
1268 {
1269 	register struct netcred *np;
1270 	register struct radix_node_head *rnh;
1271 	struct sockaddr *saddr;
1272 
1273 	np = NULL;
1274 	if (mp->mnt_flag & MNT_EXPORTED) {
1275 		/*
1276 		 * Lookup in the export list first.
1277 		 */
1278 		if (nam != NULL) {
1279 			saddr = mtod(nam, struct sockaddr *);
1280 			rnh = nep->ne_rtable[saddr->sa_family];
1281 			if (rnh != NULL) {
1282 				np = (struct netcred *)
1283 					(*rnh->rnh_matchaddr)((caddr_t)saddr,
1284 							      rnh);
1285 				if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
1286 					np = NULL;
1287 			}
1288 		}
1289 		/*
1290 		 * If no address match, use the default if it exists.
1291 		 */
1292 		if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
1293 			np = &nep->ne_defexported;
1294 	}
1295 	return (np);
1296 }
1297