xref: /original-bsd/sys/kern/vfs_subr.c (revision 333da485)
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.9 (Berkeley) 01/21/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 	if (vp->v_flag & VXLOCK) {
562 		vp->v_flag |= VXWANT;
563 		sleep((caddr_t)vp, PINOD);
564 		return (1);
565 	}
566 	if (vp->v_usecount == 0)
567 		TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
568 	vp->v_usecount++;
569 	if (lockflag)
570 		VOP_LOCK(vp);
571 	return (0);
572 }
573 
574 /*
575  * Vnode reference, just increment the count
576  */
577 void vref(vp)
578 	struct vnode *vp;
579 {
580 
581 	if (vp->v_usecount <= 0)
582 		panic("vref used where vget required");
583 	vp->v_usecount++;
584 }
585 
586 /*
587  * vput(), just unlock and vrele()
588  */
589 void vput(vp)
590 	register struct vnode *vp;
591 {
592 
593 	VOP_UNLOCK(vp);
594 	vrele(vp);
595 }
596 
597 /*
598  * Vnode release.
599  * If count drops to zero, call inactive routine and return to freelist.
600  */
601 void vrele(vp)
602 	register struct vnode *vp;
603 {
604 
605 #ifdef DIAGNOSTIC
606 	if (vp == NULL)
607 		panic("vrele: null vp");
608 #endif
609 	vp->v_usecount--;
610 	if (vp->v_usecount > 0)
611 		return;
612 #ifdef DIAGNOSTIC
613 	if (vp->v_usecount != 0 || vp->v_writecount != 0) {
614 		vprint("vrele: bad ref count", vp);
615 		panic("vrele: ref cnt");
616 	}
617 #endif
618 	/*
619 	 * insert at tail of LRU list
620 	 */
621 	TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
622 	VOP_INACTIVE(vp);
623 }
624 
625 /*
626  * Page or buffer structure gets a reference.
627  */
628 void vhold(vp)
629 	register struct vnode *vp;
630 {
631 
632 	vp->v_holdcnt++;
633 }
634 
635 /*
636  * Page or buffer structure frees a reference.
637  */
638 void holdrele(vp)
639 	register struct vnode *vp;
640 {
641 
642 	if (vp->v_holdcnt <= 0)
643 		panic("holdrele: holdcnt");
644 	vp->v_holdcnt--;
645 }
646 
647 /*
648  * Remove any vnodes in the vnode table belonging to mount point mp.
649  *
650  * If MNT_NOFORCE is specified, there should not be any active ones,
651  * return error if any are found (nb: this is a user error, not a
652  * system error). If MNT_FORCE is specified, detach any active vnodes
653  * that are found.
654  */
655 #ifdef DIAGNOSTIC
656 int busyprt = 0;	/* print out busy vnodes */
657 struct ctldebug debug1 = { "busyprt", &busyprt };
658 #endif
659 
660 vflush(mp, skipvp, flags)
661 	struct mount *mp;
662 	struct vnode *skipvp;
663 	int flags;
664 {
665 	register struct vnode *vp, *nvp;
666 	int busy = 0;
667 
668 	if ((mp->mnt_flag & MNT_MPBUSY) == 0)
669 		panic("vflush: not busy");
670 loop:
671 	for (vp = mp->mnt_vnodelist.lh_first; vp; vp = nvp) {
672 		if (vp->v_mount != mp)
673 			goto loop;
674 		nvp = vp->v_mntvnodes.le_next;
675 		/*
676 		 * Skip over a selected vnode.
677 		 */
678 		if (vp == skipvp)
679 			continue;
680 		/*
681 		 * Skip over a vnodes marked VSYSTEM.
682 		 */
683 		if ((flags & SKIPSYSTEM) && (vp->v_flag & VSYSTEM))
684 			continue;
685 		/*
686 		 * If WRITECLOSE is set, only flush out regular file
687 		 * vnodes open for writing.
688 		 */
689 		if ((flags & WRITECLOSE) &&
690 		    (vp->v_writecount == 0 || vp->v_type != VREG))
691 			continue;
692 		/*
693 		 * With v_usecount == 0, all we need to do is clear
694 		 * out the vnode data structures and we are done.
695 		 */
696 		if (vp->v_usecount == 0) {
697 			vgone(vp);
698 			continue;
699 		}
700 		/*
701 		 * If FORCECLOSE is set, forcibly close the vnode.
702 		 * For block or character devices, revert to an
703 		 * anonymous device. For all other files, just kill them.
704 		 */
705 		if (flags & FORCECLOSE) {
706 			if (vp->v_type != VBLK && vp->v_type != VCHR) {
707 				vgone(vp);
708 			} else {
709 				vclean(vp, 0);
710 				vp->v_op = spec_vnodeop_p;
711 				insmntque(vp, (struct mount *)0);
712 			}
713 			continue;
714 		}
715 #ifdef DIAGNOSTIC
716 		if (busyprt)
717 			vprint("vflush: busy vnode", vp);
718 #endif
719 		busy++;
720 	}
721 	if (busy)
722 		return (EBUSY);
723 	return (0);
724 }
725 
726 /*
727  * Disassociate the underlying file system from a vnode.
728  */
729 void
730 vclean(vp, flags)
731 	register struct vnode *vp;
732 	int flags;
733 {
734 	int active;
735 
736 	/*
737 	 * Check to see if the vnode is in use.
738 	 * If so we have to reference it before we clean it out
739 	 * so that its count cannot fall to zero and generate a
740 	 * race against ourselves to recycle it.
741 	 */
742 	if (active = vp->v_usecount)
743 		VREF(vp);
744 	/*
745 	 * Even if the count is zero, the VOP_INACTIVE routine may still
746 	 * have the object locked while it cleans it out. The VOP_LOCK
747 	 * ensures that the VOP_INACTIVE routine is done with its work.
748 	 * For active vnodes, it ensures that no other activity can
749 	 * occur while the underlying object is being cleaned out.
750 	 */
751 	VOP_LOCK(vp);
752 	/*
753 	 * Prevent the vnode from being recycled or
754 	 * brought into use while we clean it out.
755 	 */
756 	if (vp->v_flag & VXLOCK)
757 		panic("vclean: deadlock");
758 	vp->v_flag |= VXLOCK;
759 	/*
760 	 * Clean out any buffers associated with the vnode.
761 	 */
762 	if (flags & DOCLOSE)
763 		vinvalbuf(vp, V_SAVE, NOCRED, NULL, 0, 0);
764 	/*
765 	 * Any other processes trying to obtain this lock must first
766 	 * wait for VXLOCK to clear, then call the new lock operation.
767 	 */
768 	VOP_UNLOCK(vp);
769 	/*
770 	 * If purging an active vnode, it must be closed and
771 	 * deactivated before being reclaimed.
772 	 */
773 	if (active) {
774 		if (flags & DOCLOSE)
775 			VOP_CLOSE(vp, IO_NDELAY, NOCRED, NULL);
776 		VOP_INACTIVE(vp);
777 	}
778 	/*
779 	 * Reclaim the vnode.
780 	 */
781 	if (VOP_RECLAIM(vp))
782 		panic("vclean: cannot reclaim");
783 	if (active)
784 		vrele(vp);
785 
786 	/*
787 	 * Done with purge, notify sleepers of the grim news.
788 	 */
789 	vp->v_op = dead_vnodeop_p;
790 	vp->v_tag = VT_NON;
791 	vp->v_flag &= ~VXLOCK;
792 	if (vp->v_flag & VXWANT) {
793 		vp->v_flag &= ~VXWANT;
794 		wakeup((caddr_t)vp);
795 	}
796 }
797 
798 /*
799  * Eliminate all activity associated with  the requested vnode
800  * and with all vnodes aliased to the requested vnode.
801  */
802 void vgoneall(vp)
803 	register struct vnode *vp;
804 {
805 	register struct vnode *vq;
806 
807 	if (vp->v_flag & VALIASED) {
808 		/*
809 		 * If a vgone (or vclean) is already in progress,
810 		 * wait until it is done and return.
811 		 */
812 		if (vp->v_flag & VXLOCK) {
813 			vp->v_flag |= VXWANT;
814 			sleep((caddr_t)vp, PINOD);
815 			return;
816 		}
817 		/*
818 		 * Ensure that vp will not be vgone'd while we
819 		 * are eliminating its aliases.
820 		 */
821 		vp->v_flag |= VXLOCK;
822 		while (vp->v_flag & VALIASED) {
823 			for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
824 				if (vq->v_rdev != vp->v_rdev ||
825 				    vq->v_type != vp->v_type || vp == vq)
826 					continue;
827 				vgone(vq);
828 				break;
829 			}
830 		}
831 		/*
832 		 * Remove the lock so that vgone below will
833 		 * really eliminate the vnode after which time
834 		 * vgone will awaken any sleepers.
835 		 */
836 		vp->v_flag &= ~VXLOCK;
837 	}
838 	vgone(vp);
839 }
840 
841 /*
842  * Eliminate all activity associated with a vnode
843  * in preparation for reuse.
844  */
845 void vgone(vp)
846 	register struct vnode *vp;
847 {
848 	register struct vnode *vq;
849 	struct vnode *vx;
850 
851 	/*
852 	 * If a vgone (or vclean) is already in progress,
853 	 * wait until it is done and return.
854 	 */
855 	if (vp->v_flag & VXLOCK) {
856 		vp->v_flag |= VXWANT;
857 		sleep((caddr_t)vp, PINOD);
858 		return;
859 	}
860 	/*
861 	 * Clean out the filesystem specific data.
862 	 */
863 	vclean(vp, DOCLOSE);
864 	/*
865 	 * Delete from old mount point vnode list, if on one.
866 	 */
867 	if (vp->v_mount != NULL) {
868 		LIST_REMOVE(vp, v_mntvnodes);
869 		vp->v_mount = NULL;
870 	}
871 	/*
872 	 * If special device, remove it from special device alias list.
873 	 */
874 	if (vp->v_type == VBLK || vp->v_type == VCHR) {
875 		if (*vp->v_hashchain == vp) {
876 			*vp->v_hashchain = vp->v_specnext;
877 		} else {
878 			for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
879 				if (vq->v_specnext != vp)
880 					continue;
881 				vq->v_specnext = vp->v_specnext;
882 				break;
883 			}
884 			if (vq == NULL)
885 				panic("missing bdev");
886 		}
887 		if (vp->v_flag & VALIASED) {
888 			vx = NULL;
889 			for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
890 				if (vq->v_rdev != vp->v_rdev ||
891 				    vq->v_type != vp->v_type)
892 					continue;
893 				if (vx)
894 					break;
895 				vx = vq;
896 			}
897 			if (vx == NULL)
898 				panic("missing alias");
899 			if (vq == NULL)
900 				vx->v_flag &= ~VALIASED;
901 			vp->v_flag &= ~VALIASED;
902 		}
903 		FREE(vp->v_specinfo, M_VNODE);
904 		vp->v_specinfo = NULL;
905 	}
906 	/*
907 	 * If it is on the freelist and not already at the head,
908 	 * move it to the head of the list. The test of the back
909 	 * pointer and the reference count of zero is because
910 	 * it will be removed from the free list by getnewvnode,
911 	 * but will not have its reference count incremented until
912 	 * after calling vgone. If the reference count were
913 	 * incremented first, vgone would (incorrectly) try to
914 	 * close the previous instance of the underlying object.
915 	 * So, the back pointer is explicitly set to `0xdeadb' in
916 	 * getnewvnode after removing it from the freelist to ensure
917 	 * that we do not try to move it here.
918 	 */
919 	if (vp->v_usecount == 0 &&
920 	    vp->v_freelist.tqe_prev != (struct vnode **)0xdeadb &&
921 	    vnode_free_list.tqh_first != vp) {
922 		TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
923 		TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
924 	}
925 	vp->v_type = VBAD;
926 }
927 
928 /*
929  * Lookup a vnode by device number.
930  */
931 vfinddev(dev, type, vpp)
932 	dev_t dev;
933 	enum vtype type;
934 	struct vnode **vpp;
935 {
936 	register struct vnode *vp;
937 
938 	for (vp = speclisth[SPECHASH(dev)]; vp; vp = vp->v_specnext) {
939 		if (dev != vp->v_rdev || type != vp->v_type)
940 			continue;
941 		*vpp = vp;
942 		return (1);
943 	}
944 	return (0);
945 }
946 
947 /*
948  * Calculate the total number of references to a special device.
949  */
950 vcount(vp)
951 	register struct vnode *vp;
952 {
953 	register struct vnode *vq;
954 	int count;
955 
956 	if ((vp->v_flag & VALIASED) == 0)
957 		return (vp->v_usecount);
958 loop:
959 	for (count = 0, vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
960 		if (vq->v_rdev != vp->v_rdev || vq->v_type != vp->v_type)
961 			continue;
962 		/*
963 		 * Alias, but not in use, so flush it out.
964 		 */
965 		if (vq->v_usecount == 0) {
966 			vgone(vq);
967 			goto loop;
968 		}
969 		count += vq->v_usecount;
970 	}
971 	return (count);
972 }
973 
974 /*
975  * Print out a description of a vnode.
976  */
977 static char *typename[] =
978    { "VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD" };
979 
980 vprint(label, vp)
981 	char *label;
982 	register struct vnode *vp;
983 {
984 	char buf[64];
985 
986 	if (label != NULL)
987 		printf("%s: ", label);
988 	printf("type %s, usecount %d, writecount %d, refcount %d,",
989 		typename[vp->v_type], vp->v_usecount, vp->v_writecount,
990 		vp->v_holdcnt);
991 	buf[0] = '\0';
992 	if (vp->v_flag & VROOT)
993 		strcat(buf, "|VROOT");
994 	if (vp->v_flag & VTEXT)
995 		strcat(buf, "|VTEXT");
996 	if (vp->v_flag & VSYSTEM)
997 		strcat(buf, "|VSYSTEM");
998 	if (vp->v_flag & VXLOCK)
999 		strcat(buf, "|VXLOCK");
1000 	if (vp->v_flag & VXWANT)
1001 		strcat(buf, "|VXWANT");
1002 	if (vp->v_flag & VBWAIT)
1003 		strcat(buf, "|VBWAIT");
1004 	if (vp->v_flag & VALIASED)
1005 		strcat(buf, "|VALIASED");
1006 	if (buf[0] != '\0')
1007 		printf(" flags (%s)", &buf[1]);
1008 	if (vp->v_data == NULL) {
1009 		printf("\n");
1010 	} else {
1011 		printf("\n\t");
1012 		VOP_PRINT(vp);
1013 	}
1014 }
1015 
1016 #ifdef DEBUG
1017 /*
1018  * List all of the locked vnodes in the system.
1019  * Called when debugging the kernel.
1020  */
1021 printlockedvnodes()
1022 {
1023 	register struct mount *mp;
1024 	register struct vnode *vp;
1025 
1026 	printf("Locked vnodes\n");
1027 	for (mp = mountlist.tqh_first; mp != NULL; mp = mp->mnt_list.tqe_next) {
1028 		for (vp = mp->mnt_vnodelist.lh_first;
1029 		     vp != NULL;
1030 		     vp = vp->v_mntvnodes.le_next)
1031 			if (VOP_ISLOCKED(vp))
1032 				vprint((char *)0, vp);
1033 	}
1034 }
1035 #endif
1036 
1037 int kinfo_vdebug = 1;
1038 int kinfo_vgetfailed;
1039 #define KINFO_VNODESLOP	10
1040 /*
1041  * Dump vnode list (via sysctl).
1042  * Copyout address of vnode followed by vnode.
1043  */
1044 /* ARGSUSED */
1045 sysctl_vnode(where, sizep)
1046 	char *where;
1047 	size_t *sizep;
1048 {
1049 	register struct mount *mp, *nmp;
1050 	struct vnode *vp;
1051 	register char *bp = where, *savebp;
1052 	char *ewhere;
1053 	int error;
1054 
1055 #define VPTRSZ	sizeof (struct vnode *)
1056 #define VNODESZ	sizeof (struct vnode)
1057 	if (where == NULL) {
1058 		*sizep = (numvnodes + KINFO_VNODESLOP) * (VPTRSZ + VNODESZ);
1059 		return (0);
1060 	}
1061 	ewhere = where + *sizep;
1062 
1063 	for (mp = mountlist.tqh_first; mp != NULL; mp = nmp) {
1064 		nmp = mp->mnt_list.tqe_next;
1065 		if (vfs_busy(mp))
1066 			continue;
1067 		savebp = bp;
1068 again:
1069 		for (vp = mp->mnt_vnodelist.lh_first;
1070 		     vp != NULL;
1071 		     vp = vp->v_mntvnodes.le_next) {
1072 			/*
1073 			 * Check that the vp is still associated with
1074 			 * this filesystem.  RACE: could have been
1075 			 * recycled onto the same filesystem.
1076 			 */
1077 			if (vp->v_mount != mp) {
1078 				if (kinfo_vdebug)
1079 					printf("kinfo: vp changed\n");
1080 				bp = savebp;
1081 				goto again;
1082 			}
1083 			if (bp + VPTRSZ + VNODESZ > ewhere) {
1084 				*sizep = bp - where;
1085 				return (ENOMEM);
1086 			}
1087 			if ((error = copyout((caddr_t)&vp, bp, VPTRSZ)) ||
1088 			   (error = copyout((caddr_t)vp, bp + VPTRSZ, VNODESZ)))
1089 				return (error);
1090 			bp += VPTRSZ + VNODESZ;
1091 		}
1092 		vfs_unbusy(mp);
1093 	}
1094 
1095 	*sizep = bp - where;
1096 	return (0);
1097 }
1098 
1099 /*
1100  * Check to see if a filesystem is mounted on a block device.
1101  */
1102 int
1103 vfs_mountedon(vp)
1104 	register struct vnode *vp;
1105 {
1106 	register struct vnode *vq;
1107 
1108 	if (vp->v_specflags & SI_MOUNTEDON)
1109 		return (EBUSY);
1110 	if (vp->v_flag & VALIASED) {
1111 		for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
1112 			if (vq->v_rdev != vp->v_rdev ||
1113 			    vq->v_type != vp->v_type)
1114 				continue;
1115 			if (vq->v_specflags & SI_MOUNTEDON)
1116 				return (EBUSY);
1117 		}
1118 	}
1119 	return (0);
1120 }
1121 
1122 /*
1123  * Build hash lists of net addresses and hang them off the mount point.
1124  * Called by ufs_mount() to set up the lists of export addresses.
1125  */
1126 static int
1127 vfs_hang_addrlist(mp, nep, argp)
1128 	struct mount *mp;
1129 	struct netexport *nep;
1130 	struct export_args *argp;
1131 {
1132 	register struct netcred *np;
1133 	register struct radix_node_head *rnh;
1134 	register int i;
1135 	struct radix_node *rn;
1136 	struct sockaddr *saddr, *smask = 0;
1137 	struct domain *dom;
1138 	int error;
1139 
1140 	if (argp->ex_addrlen == 0) {
1141 		if (mp->mnt_flag & MNT_DEFEXPORTED)
1142 			return (EPERM);
1143 		np = &nep->ne_defexported;
1144 		np->netc_exflags = argp->ex_flags;
1145 		np->netc_anon = argp->ex_anon;
1146 		np->netc_anon.cr_ref = 1;
1147 		mp->mnt_flag |= MNT_DEFEXPORTED;
1148 		return (0);
1149 	}
1150 	i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
1151 	np = (struct netcred *)malloc(i, M_NETADDR, M_WAITOK);
1152 	bzero((caddr_t)np, i);
1153 	saddr = (struct sockaddr *)(np + 1);
1154 	if (error = copyin(argp->ex_addr, (caddr_t)saddr, argp->ex_addrlen))
1155 		goto out;
1156 	if (saddr->sa_len > argp->ex_addrlen)
1157 		saddr->sa_len = argp->ex_addrlen;
1158 	if (argp->ex_masklen) {
1159 		smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen);
1160 		error = copyin(argp->ex_addr, (caddr_t)smask, argp->ex_masklen);
1161 		if (error)
1162 			goto out;
1163 		if (smask->sa_len > argp->ex_masklen)
1164 			smask->sa_len = argp->ex_masklen;
1165 	}
1166 	i = saddr->sa_family;
1167 	if ((rnh = nep->ne_rtable[i]) == 0) {
1168 		/*
1169 		 * Seems silly to initialize every AF when most are not
1170 		 * used, do so on demand here
1171 		 */
1172 		for (dom = domains; dom; dom = dom->dom_next)
1173 			if (dom->dom_family == i && dom->dom_rtattach) {
1174 				dom->dom_rtattach((void **)&nep->ne_rtable[i],
1175 					dom->dom_rtoffset);
1176 				break;
1177 			}
1178 		if ((rnh = nep->ne_rtable[i]) == 0) {
1179 			error = ENOBUFS;
1180 			goto out;
1181 		}
1182 	}
1183 	rn = (*rnh->rnh_addaddr)((caddr_t)saddr, (caddr_t)smask, rnh,
1184 		np->netc_rnodes);
1185 	if (rn == 0 || np != (struct netcred *)rn) { /* already exists */
1186 		error = EPERM;
1187 		goto out;
1188 	}
1189 	np->netc_exflags = argp->ex_flags;
1190 	np->netc_anon = argp->ex_anon;
1191 	np->netc_anon.cr_ref = 1;
1192 	return (0);
1193 out:
1194 	free(np, M_NETADDR);
1195 	return (error);
1196 }
1197 
1198 /* ARGSUSED */
1199 static int
1200 vfs_free_netcred(rn, w)
1201 	struct radix_node *rn;
1202 	caddr_t w;
1203 {
1204 	register struct radix_node_head *rnh = (struct radix_node_head *)w;
1205 
1206 	(*rnh->rnh_deladdr)(rn->rn_key, rn->rn_mask, rnh);
1207 	free((caddr_t)rn, M_NETADDR);
1208 	return (0);
1209 }
1210 
1211 /*
1212  * Free the net address hash lists that are hanging off the mount points.
1213  */
1214 static void
1215 vfs_free_addrlist(nep)
1216 	struct netexport *nep;
1217 {
1218 	register int i;
1219 	register struct radix_node_head *rnh;
1220 
1221 	for (i = 0; i <= AF_MAX; i++)
1222 		if (rnh = nep->ne_rtable[i]) {
1223 			(*rnh->rnh_walktree)(rnh, vfs_free_netcred,
1224 			    (caddr_t)rnh);
1225 			free((caddr_t)rnh, M_RTABLE);
1226 			nep->ne_rtable[i] = 0;
1227 		}
1228 }
1229 
1230 int
1231 vfs_export(mp, nep, argp)
1232 	struct mount *mp;
1233 	struct netexport *nep;
1234 	struct export_args *argp;
1235 {
1236 	int error;
1237 
1238 	if (argp->ex_flags & MNT_DELEXPORT) {
1239 		vfs_free_addrlist(nep);
1240 		mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
1241 	}
1242 	if (argp->ex_flags & MNT_EXPORTED) {
1243 		if (error = vfs_hang_addrlist(mp, nep, argp))
1244 			return (error);
1245 		mp->mnt_flag |= MNT_EXPORTED;
1246 	}
1247 	return (0);
1248 }
1249 
1250 struct netcred *
1251 vfs_export_lookup(mp, nep, nam)
1252 	register struct mount *mp;
1253 	struct netexport *nep;
1254 	struct mbuf *nam;
1255 {
1256 	register struct netcred *np;
1257 	register struct radix_node_head *rnh;
1258 	struct sockaddr *saddr;
1259 
1260 	np = NULL;
1261 	if (mp->mnt_flag & MNT_EXPORTED) {
1262 		/*
1263 		 * Lookup in the export list first.
1264 		 */
1265 		if (nam != NULL) {
1266 			saddr = mtod(nam, struct sockaddr *);
1267 			rnh = nep->ne_rtable[saddr->sa_family];
1268 			if (rnh != NULL) {
1269 				np = (struct netcred *)
1270 					(*rnh->rnh_matchaddr)((caddr_t)saddr,
1271 							      rnh);
1272 				if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
1273 					np = NULL;
1274 			}
1275 		}
1276 		/*
1277 		 * If no address match, use the default if it exists.
1278 		 */
1279 		if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
1280 			np = &nep->ne_defexported;
1281 	}
1282 	return (np);
1283 }
1284