two minor fixes for 386; others unaffected
[unix-history] / usr / src / sys / kern / vfs_subr.c
CommitLineData
3c4390e8
KM
1/*
2 * Copyright (c) 1989 The Regents of the University of California.
3 * All rights reserved.
4 *
dbf0c423 5 * %sccs.include.redist.c%
3c4390e8 6 *
77dc8a8c 7 * @(#)vfs_subr.c 7.55 (Berkeley) %G%
3c4390e8
KM
8 */
9
10/*
11 * External virtual filesystem routines
12 */
13
14#include "param.h"
c099667a 15#include "proc.h"
3c4390e8
KM
16#include "mount.h"
17#include "time.h"
18#include "vnode.h"
0f93ba7b 19#include "specdev.h"
c60798ca
KM
20#include "namei.h"
21#include "ucred.h"
76429560 22#include "buf.h"
3c4390e8 23#include "errno.h"
ef24f6dd 24#include "malloc.h"
3c4390e8 25
3c4390e8
KM
26/*
27 * Remove a mount point from the list of mounted filesystems.
28 * Unmount of the root is illegal.
29 */
30void
31vfs_remove(mp)
32 register struct mount *mp;
33{
34
35 if (mp == rootfs)
36 panic("vfs_remove: unmounting root");
54fb9dc2
KM
37 mp->mnt_prev->mnt_next = mp->mnt_next;
38 mp->mnt_next->mnt_prev = mp->mnt_prev;
39 mp->mnt_vnodecovered->v_mountedhere = (struct mount *)0;
3c4390e8
KM
40 vfs_unlock(mp);
41}
42
43/*
44 * Lock a filesystem.
45 * Used to prevent access to it while mounting and unmounting.
46 */
47vfs_lock(mp)
48 register struct mount *mp;
49{
50
54fb9dc2
KM
51 while(mp->mnt_flag & MNT_MLOCK) {
52 mp->mnt_flag |= MNT_MWAIT;
594501df
KM
53 sleep((caddr_t)mp, PVFS);
54 }
54fb9dc2 55 mp->mnt_flag |= MNT_MLOCK;
3c4390e8
KM
56 return (0);
57}
58
59/*
60 * Unlock a locked filesystem.
61 * Panic if filesystem is not locked.
62 */
63void
64vfs_unlock(mp)
65 register struct mount *mp;
66{
67
54fb9dc2 68 if ((mp->mnt_flag & MNT_MLOCK) == 0)
36ef03ec 69 panic("vfs_unlock: not locked");
54fb9dc2
KM
70 mp->mnt_flag &= ~MNT_MLOCK;
71 if (mp->mnt_flag & MNT_MWAIT) {
72 mp->mnt_flag &= ~MNT_MWAIT;
3c4390e8
KM
73 wakeup((caddr_t)mp);
74 }
75}
76
36ef03ec
KM
77/*
78 * Mark a mount point as busy.
79 * Used to synchronize access and to delay unmounting.
80 */
81vfs_busy(mp)
82 register struct mount *mp;
83{
84
54fb9dc2
KM
85 while(mp->mnt_flag & MNT_MPBUSY) {
86 mp->mnt_flag |= MNT_MPWANT;
87 sleep((caddr_t)&mp->mnt_flag, PVFS);
36ef03ec 88 }
d8b63609
KM
89 if (mp->mnt_flag & MNT_UNMOUNT)
90 return (1);
54fb9dc2 91 mp->mnt_flag |= MNT_MPBUSY;
36ef03ec
KM
92 return (0);
93}
94
95/*
96 * Free a busy filesystem.
97 * Panic if filesystem is not busy.
98 */
36ef03ec
KM
99vfs_unbusy(mp)
100 register struct mount *mp;
101{
102
54fb9dc2 103 if ((mp->mnt_flag & MNT_MPBUSY) == 0)
36ef03ec 104 panic("vfs_unbusy: not busy");
54fb9dc2
KM
105 mp->mnt_flag &= ~MNT_MPBUSY;
106 if (mp->mnt_flag & MNT_MPWANT) {
107 mp->mnt_flag &= ~MNT_MPWANT;
108 wakeup((caddr_t)&mp->mnt_flag);
36ef03ec
KM
109 }
110}
111
3c4390e8
KM
112/*
113 * Lookup a mount point by filesystem identifier.
114 */
115struct mount *
116getvfs(fsid)
117 fsid_t *fsid;
118{
119 register struct mount *mp;
120
d713f801
KM
121 mp = rootfs;
122 do {
54fb9dc2
KM
123 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
124 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
d713f801 125 return (mp);
3c4390e8 126 }
54fb9dc2 127 mp = mp->mnt_next;
d713f801
KM
128 } while (mp != rootfs);
129 return ((struct mount *)0);
3c4390e8
KM
130}
131
132/*
133 * Set vnode attributes to VNOVAL
134 */
135void vattr_null(vap)
136 register struct vattr *vap;
137{
138
139 vap->va_type = VNON;
140 vap->va_mode = vap->va_nlink = vap->va_uid = vap->va_gid =
141 vap->va_fsid = vap->va_fileid = vap->va_size =
8fae943a
KM
142 vap->va_size_rsv = vap->va_blocksize = vap->va_rdev =
143 vap->va_bytes = vap->va_bytes_rsv =
3c4390e8
KM
144 vap->va_atime.tv_sec = vap->va_atime.tv_usec =
145 vap->va_mtime.tv_sec = vap->va_mtime.tv_usec =
8cf4d4fb
KM
146 vap->va_ctime.tv_sec = vap->va_ctime.tv_usec =
147 vap->va_flags = vap->va_gen = VNOVAL;
3c4390e8 148}
c60798ca
KM
149
150/*
151 * Initialize a nameidata structure
152 */
153ndinit(ndp)
154 register struct nameidata *ndp;
155{
156
157 bzero((caddr_t)ndp, sizeof(struct nameidata));
158 ndp->ni_iov = &ndp->ni_nd.nd_iovec;
159 ndp->ni_iovcnt = 1;
160 ndp->ni_base = (caddr_t)&ndp->ni_dent;
161 ndp->ni_rw = UIO_WRITE;
4dbe3531 162 ndp->ni_uioseg = UIO_SYSSPACE;
c60798ca
KM
163}
164
165/*
166 * Duplicate a nameidata structure
167 */
168nddup(ndp, newndp)
169 register struct nameidata *ndp, *newndp;
170{
171
172 ndinit(newndp);
c60798ca
KM
173 newndp->ni_cred = ndp->ni_cred;
174 crhold(newndp->ni_cred);
175}
176
177/*
178 * Release a nameidata structure
179 */
180ndrele(ndp)
181 register struct nameidata *ndp;
182{
183
c60798ca
KM
184 crfree(ndp->ni_cred);
185}
36d09cb1
KM
186
187/*
188 * Routines having to do with the management of the vnode table.
189 */
190struct vnode *vfreeh, **vfreet;
4ef5d036 191extern struct vnodeops dead_vnodeops, spec_vnodeops;
32339c94 192extern void vclean();
1a80f56e 193long numvnodes;
d3664d8f 194struct vattr va_null;
c0de8792 195
36d09cb1 196/*
ef24f6dd 197 * Initialize the vnode structures and initialize each file system type.
36d09cb1 198 */
ef24f6dd 199vfsinit()
36d09cb1 200{
ef24f6dd 201 struct vfsops **vfsp;
36d09cb1 202
ef24f6dd
KM
203 /*
204 * Initialize the vnode name cache
205 */
206 nchinit();
207 /*
208 * Initialize each file system type.
209 */
d3664d8f 210 vattr_null(&va_null);
ef24f6dd
KM
211 for (vfsp = &vfssw[0]; vfsp <= &vfssw[MOUNT_MAXTYPE]; vfsp++) {
212 if (*vfsp == NULL)
213 continue;
214 (*(*vfsp)->vfs_init)();
215 }
36d09cb1
KM
216}
217
218/*
219 * Return the next vnode from the free list.
220 */
221getnewvnode(tag, mp, vops, vpp)
222 enum vtagtype tag;
223 struct mount *mp;
224 struct vnodeops *vops;
225 struct vnode **vpp;
226{
227 register struct vnode *vp, *vq;
228
1a80f56e 229 if (numvnodes < desiredvnodes) {
aacc1bff
KM
230 vp = (struct vnode *)malloc((u_long)sizeof *vp,
231 M_VNODE, M_WAITOK);
1a80f56e
KM
232 bzero((char *)vp, sizeof *vp);
233 numvnodes++;
234 } else {
235 if ((vp = vfreeh) == NULL) {
236 tablefull("vnode");
237 *vpp = 0;
238 return (ENFILE);
239 }
240 if (vp->v_usecount)
241 panic("free vnode isn't");
242 if (vq = vp->v_freef)
243 vq->v_freeb = &vfreeh;
244 else
245 vfreet = &vfreeh;
246 vfreeh = vq;
247 vp->v_freef = NULL;
248 vp->v_freeb = NULL;
249 if (vp->v_type != VBAD)
250 vgone(vp);
251 vp->v_flag = 0;
1a80f56e
KM
252 vp->v_lastr = 0;
253 vp->v_socket = 0;
36d09cb1 254 }
b027498b 255 vp->v_type = VNON;
36d09cb1
KM
256 cache_purge(vp);
257 vp->v_tag = tag;
ef24f6dd 258 vp->v_op = vops;
36d09cb1
KM
259 insmntque(vp, mp);
260 VREF(vp);
261 *vpp = vp;
262 return (0);
263}
264
265/*
266 * Move a vnode from one mount queue to another.
267 */
268insmntque(vp, mp)
269 register struct vnode *vp;
270 register struct mount *mp;
271{
272 struct vnode *vq;
273
274 /*
275 * Delete from old mount point vnode list, if on one.
276 */
277 if (vp->v_mountb) {
278 if (vq = vp->v_mountf)
279 vq->v_mountb = vp->v_mountb;
280 *vp->v_mountb = vq;
281 }
282 /*
283 * Insert into list of vnodes for the new mount point, if available.
284 */
a45ff315 285 vp->v_mount = mp;
36d09cb1
KM
286 if (mp == NULL) {
287 vp->v_mountf = NULL;
288 vp->v_mountb = NULL;
289 return;
290 }
54fb9dc2
KM
291 if (mp->mnt_mounth) {
292 vp->v_mountf = mp->mnt_mounth;
293 vp->v_mountb = &mp->mnt_mounth;
294 mp->mnt_mounth->v_mountb = &vp->v_mountf;
295 mp->mnt_mounth = vp;
36d09cb1 296 } else {
54fb9dc2
KM
297 mp->mnt_mounth = vp;
298 vp->v_mountb = &mp->mnt_mounth;
36d09cb1
KM
299 vp->v_mountf = NULL;
300 }
301}
302
76429560
KM
303/*
304 * Make sure all write-behind blocks associated
305 * with mount point are flushed out (from sync).
306 */
307mntflushbuf(mountp, flags)
308 struct mount *mountp;
309 int flags;
310{
311 register struct vnode *vp;
312
313 if ((mountp->mnt_flag & MNT_MPBUSY) == 0)
314 panic("mntflushbuf: not busy");
315loop:
316 for (vp = mountp->mnt_mounth; vp; vp = vp->v_mountf) {
317 if (VOP_ISLOCKED(vp))
318 continue;
319 if (vget(vp))
320 goto loop;
321 vflushbuf(vp, flags);
322 vput(vp);
323 if (vp->v_mount != mountp)
324 goto loop;
325 }
326}
327
328/*
329 * Flush all dirty buffers associated with a vnode.
330 */
331vflushbuf(vp, flags)
332 register struct vnode *vp;
333 int flags;
334{
335 register struct buf *bp;
336 struct buf *nbp;
337 int s;
338
339loop:
340 s = splbio();
341 for (bp = vp->v_dirtyblkhd; bp; bp = nbp) {
342 nbp = bp->b_blockf;
343 if ((bp->b_flags & B_BUSY))
344 continue;
345 if ((bp->b_flags & B_DELWRI) == 0)
346 panic("vflushbuf: not dirty");
347 bremfree(bp);
348 bp->b_flags |= B_BUSY;
349 splx(s);
350 /*
351 * Wait for I/O associated with indirect blocks to complete,
352 * since there is no way to quickly wait for them below.
353 * NB: This is really specific to ufs, but is done here
354 * as it is easier and quicker.
355 */
77dc8a8c 356 if (bp->b_vp == vp || (flags & B_SYNC) == 0)
76429560 357 (void) bawrite(bp);
77dc8a8c 358 else
76429560 359 (void) bwrite(bp);
77dc8a8c 360 goto loop;
76429560
KM
361 }
362 splx(s);
363 if ((flags & B_SYNC) == 0)
364 return;
365 s = splbio();
366 while (vp->v_numoutput) {
367 vp->v_flag |= VBWAIT;
368 sleep((caddr_t)&vp->v_numoutput, PRIBIO + 1);
369 }
370 splx(s);
371 if (vp->v_dirtyblkhd) {
372 vprint("vflushbuf: dirty", vp);
373 goto loop;
374 }
375}
376
377/*
378 * Update outstanding I/O count and do wakeup if requested.
379 */
380vwakeup(bp)
381 register struct buf *bp;
382{
383 register struct vnode *vp;
384
385 bp->b_dirtyoff = bp->b_dirtyend = 0;
386 if (vp = bp->b_vp) {
387 vp->v_numoutput--;
388 if ((vp->v_flag & VBWAIT) && vp->v_numoutput <= 0) {
389 if (vp->v_numoutput < 0)
390 panic("vwakeup: neg numoutput");
391 vp->v_flag &= ~VBWAIT;
392 wakeup((caddr_t)&vp->v_numoutput);
393 }
394 }
395}
396
397/*
398 * Invalidate in core blocks belonging to closed or umounted filesystem
399 *
400 * Go through the list of vnodes associated with the file system;
401 * for each vnode invalidate any buffers that it holds. Normally
402 * this routine is preceeded by a bflush call, so that on a quiescent
403 * filesystem there will be no dirty buffers when we are done. Binval
404 * returns the count of dirty buffers when it is finished.
405 */
406mntinvalbuf(mountp)
407 struct mount *mountp;
408{
409 register struct vnode *vp;
410 int dirty = 0;
411
412 if ((mountp->mnt_flag & MNT_MPBUSY) == 0)
413 panic("mntinvalbuf: not busy");
414loop:
415 for (vp = mountp->mnt_mounth; vp; vp = vp->v_mountf) {
416 if (vget(vp))
417 goto loop;
418 dirty += vinvalbuf(vp, 1);
419 vput(vp);
420 if (vp->v_mount != mountp)
421 goto loop;
422 }
423 return (dirty);
424}
425
426/*
427 * Flush out and invalidate all buffers associated with a vnode.
428 * Called with the underlying object locked.
429 */
430vinvalbuf(vp, save)
431 register struct vnode *vp;
432 int save;
433{
434 register struct buf *bp;
435 struct buf *nbp, *blist;
436 int s, dirty = 0;
437
438 for (;;) {
439 if (blist = vp->v_dirtyblkhd)
440 /* void */;
441 else if (blist = vp->v_cleanblkhd)
442 /* void */;
443 else
444 break;
445 for (bp = blist; bp; bp = nbp) {
446 nbp = bp->b_blockf;
447 s = splbio();
448 if (bp->b_flags & B_BUSY) {
449 bp->b_flags |= B_WANTED;
450 sleep((caddr_t)bp, PRIBIO + 1);
451 splx(s);
452 break;
453 }
454 bremfree(bp);
455 bp->b_flags |= B_BUSY;
456 splx(s);
457 if (save && (bp->b_flags & B_DELWRI)) {
458 dirty++;
459 (void) bwrite(bp);
460 break;
461 }
462 if (bp->b_vp != vp)
463 reassignbuf(bp, bp->b_vp);
464 else
465 bp->b_flags |= B_INVAL;
466 brelse(bp);
467 }
468 }
469 if (vp->v_dirtyblkhd || vp->v_cleanblkhd)
470 panic("vinvalbuf: flush failed");
471 return (dirty);
472}
473
474/*
475 * Associate a buffer with a vnode.
476 */
477bgetvp(vp, bp)
478 register struct vnode *vp;
479 register struct buf *bp;
480{
481
482 if (bp->b_vp)
483 panic("bgetvp: not free");
484 VHOLD(vp);
485 bp->b_vp = vp;
486 if (vp->v_type == VBLK || vp->v_type == VCHR)
487 bp->b_dev = vp->v_rdev;
488 else
489 bp->b_dev = NODEV;
490 /*
491 * Insert onto list for new vnode.
492 */
493 if (vp->v_cleanblkhd) {
494 bp->b_blockf = vp->v_cleanblkhd;
495 bp->b_blockb = &vp->v_cleanblkhd;
496 vp->v_cleanblkhd->b_blockb = &bp->b_blockf;
497 vp->v_cleanblkhd = bp;
498 } else {
499 vp->v_cleanblkhd = bp;
500 bp->b_blockb = &vp->v_cleanblkhd;
501 bp->b_blockf = NULL;
502 }
503}
504
505/*
506 * Disassociate a buffer from a vnode.
507 */
508brelvp(bp)
509 register struct buf *bp;
510{
511 struct buf *bq;
512 struct vnode *vp;
513
514 if (bp->b_vp == (struct vnode *) 0)
515 panic("brelvp: NULL");
516 /*
517 * Delete from old vnode list, if on one.
518 */
519 if (bp->b_blockb) {
520 if (bq = bp->b_blockf)
521 bq->b_blockb = bp->b_blockb;
522 *bp->b_blockb = bq;
523 bp->b_blockf = NULL;
524 bp->b_blockb = NULL;
525 }
526 vp = bp->b_vp;
527 bp->b_vp = (struct vnode *) 0;
528 HOLDRELE(vp);
529}
530
531/*
532 * Reassign a buffer from one vnode to another.
533 * Used to assign file specific control information
534 * (indirect blocks) to the vnode to which they belong.
535 */
536reassignbuf(bp, newvp)
537 register struct buf *bp;
538 register struct vnode *newvp;
539{
540 register struct buf *bq, **listheadp;
541
542 if (newvp == NULL)
543 panic("reassignbuf: NULL");
544 /*
545 * Delete from old vnode list, if on one.
546 */
547 if (bp->b_blockb) {
548 if (bq = bp->b_blockf)
549 bq->b_blockb = bp->b_blockb;
550 *bp->b_blockb = bq;
551 }
552 /*
553 * If dirty, put on list of dirty buffers;
554 * otherwise insert onto list of clean buffers.
555 */
556 if (bp->b_flags & B_DELWRI)
557 listheadp = &newvp->v_dirtyblkhd;
558 else
559 listheadp = &newvp->v_cleanblkhd;
560 if (*listheadp) {
561 bp->b_blockf = *listheadp;
562 bp->b_blockb = listheadp;
563 bp->b_blockf->b_blockb = &bp->b_blockf;
564 *listheadp = bp;
565 } else {
566 *listheadp = bp;
567 bp->b_blockb = listheadp;
568 bp->b_blockf = NULL;
569 }
570}
571
36d09cb1 572/*
ef24f6dd
KM
573 * Create a vnode for a block device.
574 * Used for root filesystem, argdev, and swap areas.
575 * Also used for memory file system special devices.
576 */
577bdevvp(dev, vpp)
578 dev_t dev;
579 struct vnode **vpp;
580{
ef24f6dd
KM
581 register struct vnode *vp;
582 struct vnode *nvp;
583 int error;
584
1c89915d
KM
585 if (dev == NODEV)
586 return (0);
4ef5d036 587 error = getnewvnode(VT_NON, (struct mount *)0, &spec_vnodeops, &nvp);
ef24f6dd
KM
588 if (error) {
589 *vpp = 0;
590 return (error);
591 }
592 vp = nvp;
593 vp->v_type = VBLK;
c0de8792 594 if (nvp = checkalias(vp, dev, (struct mount *)0)) {
ef24f6dd
KM
595 vput(vp);
596 vp = nvp;
597 }
598 *vpp = vp;
599 return (0);
600}
601
602/*
603 * Check to see if the new vnode represents a special device
604 * for which we already have a vnode (either because of
605 * bdevvp() or because of a different vnode representing
606 * the same block device). If such an alias exists, deallocate
f0556f86 607 * the existing contents and return the aliased vnode. The
ef24f6dd
KM
608 * caller is responsible for filling it with its new contents.
609 */
610struct vnode *
c0de8792 611checkalias(nvp, nvp_rdev, mp)
ef24f6dd 612 register struct vnode *nvp;
c0de8792 613 dev_t nvp_rdev;
ef24f6dd
KM
614 struct mount *mp;
615{
616 register struct vnode *vp;
c0de8792 617 struct vnode **vpp;
ef24f6dd
KM
618
619 if (nvp->v_type != VBLK && nvp->v_type != VCHR)
54fb9dc2 620 return (NULLVP);
c0de8792
KM
621
622 vpp = &speclisth[SPECHASH(nvp_rdev)];
ef24f6dd 623loop:
c0de8792
KM
624 for (vp = *vpp; vp; vp = vp->v_specnext) {
625 if (nvp_rdev != vp->v_rdev || nvp->v_type != vp->v_type)
ef24f6dd 626 continue;
c0de8792
KM
627 /*
628 * Alias, but not in use, so flush it out.
629 */
7f7b7d89 630 if (vp->v_usecount == 0) {
c0de8792
KM
631 vgone(vp);
632 goto loop;
633 }
ef62830d
KM
634 if (vget(vp))
635 goto loop;
ef24f6dd
KM
636 break;
637 }
c0de8792 638 if (vp == NULL || vp->v_tag != VT_NON) {
c0de8792
KM
639 MALLOC(nvp->v_specinfo, struct specinfo *,
640 sizeof(struct specinfo), M_VNODE, M_WAITOK);
641 nvp->v_rdev = nvp_rdev;
7f7b7d89 642 nvp->v_hashchain = vpp;
c0de8792 643 nvp->v_specnext = *vpp;
2c957a90 644 nvp->v_specflags = 0;
c0de8792 645 *vpp = nvp;
40452d5e
KM
646 if (vp != NULL) {
647 nvp->v_flag |= VALIASED;
648 vp->v_flag |= VALIASED;
649 vput(vp);
650 }
54fb9dc2 651 return (NULLVP);
ef24f6dd 652 }
2bae1875
KM
653 VOP_UNLOCK(vp);
654 vclean(vp, 0);
ef24f6dd
KM
655 vp->v_op = nvp->v_op;
656 vp->v_tag = nvp->v_tag;
657 nvp->v_type = VNON;
658 insmntque(vp, mp);
659 return (vp);
660}
661
662/*
663 * Grab a particular vnode from the free list, increment its
664 * reference count and lock it. The vnode lock bit is set the
665 * vnode is being eliminated in vgone. The process is awakened
666 * when the transition is completed, and an error returned to
667 * indicate that the vnode is no longer usable (possibly having
668 * been changed to a new file system type).
36d09cb1
KM
669 */
670vget(vp)
671 register struct vnode *vp;
672{
673 register struct vnode *vq;
674
ef24f6dd
KM
675 if (vp->v_flag & VXLOCK) {
676 vp->v_flag |= VXWANT;
677 sleep((caddr_t)vp, PINOD);
678 return (1);
679 }
7f7b7d89 680 if (vp->v_usecount == 0) {
ef24f6dd
KM
681 if (vq = vp->v_freef)
682 vq->v_freeb = vp->v_freeb;
683 else
684 vfreet = vp->v_freeb;
685 *vp->v_freeb = vq;
686 vp->v_freef = NULL;
687 vp->v_freeb = NULL;
688 }
36d09cb1 689 VREF(vp);
ef24f6dd
KM
690 VOP_LOCK(vp);
691 return (0);
36d09cb1
KM
692}
693
694/*
695 * Vnode reference, just increment the count
696 */
697void vref(vp)
698 struct vnode *vp;
699{
700
7f7b7d89 701 vp->v_usecount++;
36d09cb1
KM
702}
703
704/*
705 * vput(), just unlock and vrele()
706 */
707void vput(vp)
708 register struct vnode *vp;
709{
710 VOP_UNLOCK(vp);
711 vrele(vp);
712}
713
714/*
715 * Vnode release.
716 * If count drops to zero, call inactive routine and return to freelist.
717 */
718void vrele(vp)
719 register struct vnode *vp;
720{
c099667a 721 struct proc *p = curproc; /* XXX */
36d09cb1
KM
722
723 if (vp == NULL)
ef24f6dd 724 panic("vrele: null vp");
7f7b7d89
KM
725 vp->v_usecount--;
726 if (vp->v_usecount < 0)
0bf84b18 727 vprint("vrele: bad ref count", vp);
7f7b7d89 728 if (vp->v_usecount > 0)
36d09cb1 729 return;
54fb9dc2 730 if (vfreeh == NULLVP) {
36d09cb1
KM
731 /*
732 * insert into empty list
733 */
734 vfreeh = vp;
735 vp->v_freeb = &vfreeh;
36d09cb1
KM
736 } else {
737 /*
738 * insert at tail of list
739 */
740 *vfreet = vp;
741 vp->v_freeb = vfreet;
36d09cb1 742 }
ef24f6dd
KM
743 vp->v_freef = NULL;
744 vfreet = &vp->v_freef;
c099667a 745 VOP_INACTIVE(vp, p);
ef24f6dd
KM
746}
747
7f7b7d89
KM
748/*
749 * Page or buffer structure gets a reference.
750 */
751vhold(vp)
752 register struct vnode *vp;
753{
754
755 vp->v_holdcnt++;
756}
757
758/*
759 * Page or buffer structure frees a reference.
760 */
761holdrele(vp)
762 register struct vnode *vp;
763{
764
765 if (vp->v_holdcnt <= 0)
766 panic("holdrele: holdcnt");
767 vp->v_holdcnt--;
768}
769
f0556f86
KM
770/*
771 * Remove any vnodes in the vnode table belonging to mount point mp.
772 *
773 * If MNT_NOFORCE is specified, there should not be any active ones,
774 * return error if any are found (nb: this is a user error, not a
775 * system error). If MNT_FORCE is specified, detach any active vnodes
776 * that are found.
777 */
778int busyprt = 0; /* patch to print out busy vnodes */
779
780vflush(mp, skipvp, flags)
781 struct mount *mp;
782 struct vnode *skipvp;
783 int flags;
784{
785 register struct vnode *vp, *nvp;
786 int busy = 0;
787
54fb9dc2 788 if ((mp->mnt_flag & MNT_MPBUSY) == 0)
36ef03ec 789 panic("vflush: not busy");
4597dd33 790loop:
54fb9dc2 791 for (vp = mp->mnt_mounth; vp; vp = nvp) {
4597dd33
KM
792 if (vp->v_mount != mp)
793 goto loop;
f0556f86
KM
794 nvp = vp->v_mountf;
795 /*
796 * Skip over a selected vnode.
f0556f86
KM
797 */
798 if (vp == skipvp)
799 continue;
36ef03ec
KM
800 /*
801 * Skip over a vnodes marked VSYSTEM.
802 */
803 if ((flags & SKIPSYSTEM) && (vp->v_flag & VSYSTEM))
804 continue;
f0556f86 805 /*
7f7b7d89 806 * With v_usecount == 0, all we need to do is clear
f0556f86
KM
807 * out the vnode data structures and we are done.
808 */
7f7b7d89 809 if (vp->v_usecount == 0) {
f0556f86
KM
810 vgone(vp);
811 continue;
812 }
813 /*
814 * For block or character devices, revert to an
815 * anonymous device. For all other files, just kill them.
816 */
36ef03ec 817 if (flags & FORCECLOSE) {
f0556f86
KM
818 if (vp->v_type != VBLK && vp->v_type != VCHR) {
819 vgone(vp);
820 } else {
821 vclean(vp, 0);
822 vp->v_op = &spec_vnodeops;
823 insmntque(vp, (struct mount *)0);
824 }
825 continue;
826 }
827 if (busyprt)
0bf84b18 828 vprint("vflush: busy vnode", vp);
f0556f86
KM
829 busy++;
830 }
831 if (busy)
832 return (EBUSY);
833 return (0);
834}
835
ef24f6dd
KM
836/*
837 * Disassociate the underlying file system from a vnode.
ef24f6dd 838 */
36ef03ec 839void vclean(vp, flags)
ef24f6dd 840 register struct vnode *vp;
aacc1bff 841 int flags;
ef24f6dd
KM
842{
843 struct vnodeops *origops;
2bae1875 844 int active;
c099667a 845 struct proc *p = curproc; /* XXX */
ef24f6dd 846
2bae1875
KM
847 /*
848 * Check to see if the vnode is in use.
0bf84b18
KM
849 * If so we have to reference it before we clean it out
850 * so that its count cannot fall to zero and generate a
851 * race against ourselves to recycle it.
2bae1875 852 */
7f7b7d89 853 if (active = vp->v_usecount)
2bae1875 854 VREF(vp);
2bae1875
KM
855 /*
856 * Prevent the vnode from being recycled or
857 * brought into use while we clean it out.
858 */
0bf84b18
KM
859 if (vp->v_flag & VXLOCK)
860 panic("vclean: deadlock");
ef24f6dd 861 vp->v_flag |= VXLOCK;
0bf84b18
KM
862 /*
863 * Even if the count is zero, the VOP_INACTIVE routine may still
864 * have the object locked while it cleans it out. The VOP_LOCK
865 * ensures that the VOP_INACTIVE routine is done with its work.
866 * For active vnodes, it ensures that no other activity can
867 * occur while the buffer list is being cleaned out.
868 */
869 VOP_LOCK(vp);
36ef03ec 870 if (flags & DOCLOSE)
0bf84b18 871 vinvalbuf(vp, 1);
ef24f6dd
KM
872 /*
873 * Prevent any further operations on the vnode from
874 * being passed through to the old file system.
875 */
876 origops = vp->v_op;
877 vp->v_op = &dead_vnodeops;
878 vp->v_tag = VT_NON;
879 /*
2bae1875
KM
880 * If purging an active vnode, it must be unlocked, closed,
881 * and deactivated before being reclaimed.
ef24f6dd 882 */
0bf84b18 883 (*(origops->vn_unlock))(vp);
2bae1875 884 if (active) {
36ef03ec 885 if (flags & DOCLOSE)
44b28806 886 (*(origops->vn_close))(vp, IO_NDELAY, NOCRED, p);
c099667a 887 (*(origops->vn_inactive))(vp, p);
ef24f6dd
KM
888 }
889 /*
890 * Reclaim the vnode.
891 */
892 if ((*(origops->vn_reclaim))(vp))
893 panic("vclean: cannot reclaim");
2bae1875
KM
894 if (active)
895 vrele(vp);
ef24f6dd
KM
896 /*
897 * Done with purge, notify sleepers in vget of the grim news.
898 */
899 vp->v_flag &= ~VXLOCK;
900 if (vp->v_flag & VXWANT) {
901 vp->v_flag &= ~VXWANT;
902 wakeup((caddr_t)vp);
903 }
904}
905
ef62830d
KM
906/*
907 * Eliminate all activity associated with the requested vnode
908 * and with all vnodes aliased to the requested vnode.
909 */
910void vgoneall(vp)
911 register struct vnode *vp;
912{
7f7b7d89 913 register struct vnode *vq;
ef62830d 914
7a7b3a95
KM
915 if (vp->v_flag & VALIASED) {
916 /*
917 * If a vgone (or vclean) is already in progress,
918 * wait until it is done and return.
919 */
920 if (vp->v_flag & VXLOCK) {
921 vp->v_flag |= VXWANT;
922 sleep((caddr_t)vp, PINOD);
923 return;
924 }
925 /*
926 * Ensure that vp will not be vgone'd while we
927 * are eliminating its aliases.
928 */
929 vp->v_flag |= VXLOCK;
930 while (vp->v_flag & VALIASED) {
931 for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
932 if (vq->v_rdev != vp->v_rdev ||
933 vq->v_type != vp->v_type || vp == vq)
934 continue;
935 vgone(vq);
936 break;
937 }
ef62830d 938 }
7a7b3a95
KM
939 /*
940 * Remove the lock so that vgone below will
941 * really eliminate the vnode after which time
942 * vgone will awaken any sleepers.
943 */
944 vp->v_flag &= ~VXLOCK;
ef62830d
KM
945 }
946 vgone(vp);
947}
948
ef24f6dd
KM
949/*
950 * Eliminate all activity associated with a vnode
951 * in preparation for reuse.
952 */
953void vgone(vp)
954 register struct vnode *vp;
955{
7f7b7d89 956 register struct vnode *vq;
c0de8792
KM
957 struct vnode *vx;
958 long count;
ef24f6dd 959
4f55e3ec
KM
960 /*
961 * If a vgone (or vclean) is already in progress,
962 * wait until it is done and return.
963 */
964 if (vp->v_flag & VXLOCK) {
965 vp->v_flag |= VXWANT;
966 sleep((caddr_t)vp, PINOD);
967 return;
968 }
ef24f6dd
KM
969 /*
970 * Clean out the filesystem specific data.
971 */
36ef03ec 972 vclean(vp, DOCLOSE);
ef24f6dd
KM
973 /*
974 * Delete from old mount point vnode list, if on one.
975 */
976 if (vp->v_mountb) {
977 if (vq = vp->v_mountf)
978 vq->v_mountb = vp->v_mountb;
979 *vp->v_mountb = vq;
980 vp->v_mountf = NULL;
981 vp->v_mountb = NULL;
982 }
983 /*
984 * If special device, remove it from special device alias list.
985 */
986 if (vp->v_type == VBLK || vp->v_type == VCHR) {
7f7b7d89
KM
987 if (*vp->v_hashchain == vp) {
988 *vp->v_hashchain = vp->v_specnext;
ef24f6dd 989 } else {
7f7b7d89 990 for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
c0de8792 991 if (vq->v_specnext != vp)
ef24f6dd 992 continue;
c0de8792 993 vq->v_specnext = vp->v_specnext;
ef24f6dd
KM
994 break;
995 }
c0de8792 996 if (vq == NULL)
ef24f6dd
KM
997 panic("missing bdev");
998 }
c0de8792 999 if (vp->v_flag & VALIASED) {
7f7b7d89
KM
1000 count = 0;
1001 for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
de81e10c
KM
1002 if (vq->v_rdev != vp->v_rdev ||
1003 vq->v_type != vp->v_type)
c0de8792
KM
1004 continue;
1005 count++;
1006 vx = vq;
1007 }
1008 if (count == 0)
1009 panic("missing alias");
1010 if (count == 1)
1011 vx->v_flag &= ~VALIASED;
1012 vp->v_flag &= ~VALIASED;
1013 }
1014 FREE(vp->v_specinfo, M_VNODE);
1015 vp->v_specinfo = NULL;
ef24f6dd
KM
1016 }
1017 /*
1018 * If it is on the freelist, move it to the head of the list.
1019 */
1020 if (vp->v_freeb) {
1021 if (vq = vp->v_freef)
1022 vq->v_freeb = vp->v_freeb;
1023 else
1024 vfreet = vp->v_freeb;
1025 *vp->v_freeb = vq;
1026 vp->v_freef = vfreeh;
1027 vp->v_freeb = &vfreeh;
1028 vfreeh->v_freeb = &vp->v_freef;
1029 vfreeh = vp;
1030 }
2bae1875 1031 vp->v_type = VBAD;
36d09cb1 1032}
ef62830d 1033
2bcd6066
KM
1034/*
1035 * Lookup a vnode by device number.
1036 */
1037vfinddev(dev, type, vpp)
1038 dev_t dev;
1039 enum vtype type;
1040 struct vnode **vpp;
1041{
1042 register struct vnode *vp;
1043
1044 for (vp = speclisth[SPECHASH(dev)]; vp; vp = vp->v_specnext) {
1045 if (dev != vp->v_rdev || type != vp->v_type)
1046 continue;
1047 *vpp = vp;
1048 return (0);
1049 }
1050 return (1);
1051}
1052
ef62830d
KM
1053/*
1054 * Calculate the total number of references to a special device.
1055 */
1056vcount(vp)
1057 register struct vnode *vp;
1058{
7f7b7d89 1059 register struct vnode *vq;
ef62830d
KM
1060 int count;
1061
1062 if ((vp->v_flag & VALIASED) == 0)
7f7b7d89 1063 return (vp->v_usecount);
ef62830d 1064loop:
7f7b7d89 1065 for (count = 0, vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
de81e10c 1066 if (vq->v_rdev != vp->v_rdev || vq->v_type != vp->v_type)
ef62830d
KM
1067 continue;
1068 /*
1069 * Alias, but not in use, so flush it out.
1070 */
7f7b7d89 1071 if (vq->v_usecount == 0) {
ef62830d
KM
1072 vgone(vq);
1073 goto loop;
1074 }
7f7b7d89 1075 count += vq->v_usecount;
ef62830d
KM
1076 }
1077 return (count);
1078}
0bf84b18
KM
1079
1080/*
1081 * Print out a description of a vnode.
1082 */
1083static char *typename[] =
61f846a8 1084 { "VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD" };
0bf84b18
KM
1085
1086vprint(label, vp)
1087 char *label;
1088 register struct vnode *vp;
1089{
f2f730c6 1090 char buf[64];
0bf84b18
KM
1091
1092 if (label != NULL)
1093 printf("%s: ", label);
f2f730c6 1094 printf("type %s, usecount %d, refcount %d,", typename[vp->v_type],
7f7b7d89 1095 vp->v_usecount, vp->v_holdcnt);
f2f730c6
KM
1096 buf[0] = '\0';
1097 if (vp->v_flag & VROOT)
1098 strcat(buf, "|VROOT");
1099 if (vp->v_flag & VTEXT)
1100 strcat(buf, "|VTEXT");
36ef03ec
KM
1101 if (vp->v_flag & VSYSTEM)
1102 strcat(buf, "|VSYSTEM");
36ef03ec
KM
1103 if (vp->v_flag & VXLOCK)
1104 strcat(buf, "|VXLOCK");
1105 if (vp->v_flag & VXWANT)
1106 strcat(buf, "|VXWANT");
f2f730c6
KM
1107 if (vp->v_flag & VBWAIT)
1108 strcat(buf, "|VBWAIT");
36ef03ec
KM
1109 if (vp->v_flag & VALIASED)
1110 strcat(buf, "|VALIASED");
f2f730c6
KM
1111 if (buf[0] != '\0')
1112 printf(" flags (%s)", &buf[1]);
1113 printf("\n\t");
0bf84b18
KM
1114 VOP_PRINT(vp);
1115}
985cbdd5
MT
1116
1117int kinfo_vdebug = 1;
1118int kinfo_vgetfailed;
1119#define KINFO_VNODESLOP 10
1120/*
1121 * Dump vnode list (via kinfo).
1122 * Copyout address of vnode followed by vnode.
1123 */
aacc1bff 1124/* ARGSUSED */
985cbdd5 1125kinfo_vnode(op, where, acopysize, arg, aneeded)
aacc1bff 1126 int op;
985cbdd5 1127 char *where;
aacc1bff 1128 int *acopysize, arg, *aneeded;
985cbdd5
MT
1129{
1130 register struct mount *mp = rootfs;
36ef03ec 1131 struct mount *omp;
985cbdd5 1132 struct vnode *vp;
985cbdd5
MT
1133 register char *bp = where, *savebp;
1134 char *ewhere = where + *acopysize;
1135 int error;
1136
1137#define VPTRSZ sizeof (struct vnode *)
1138#define VNODESZ sizeof (struct vnode)
1139 if (where == NULL) {
1140 *aneeded = (numvnodes + KINFO_VNODESLOP) * (VPTRSZ + VNODESZ);
1141 return (0);
1142 }
1143
985cbdd5 1144 do {
36ef03ec 1145 if (vfs_busy(mp)) {
54fb9dc2 1146 mp = mp->mnt_next;
36ef03ec
KM
1147 continue;
1148 }
985cbdd5
MT
1149 savebp = bp;
1150again:
4597dd33 1151 for (vp = mp->mnt_mounth; vp; vp = vp->v_mountf) {
41185b3b
KM
1152 /*
1153 * Check that the vp is still associated with
1154 * this filesystem. RACE: could have been
1155 * recycled onto the same filesystem.
1156 */
4597dd33
KM
1157 if (vp->v_mount != mp) {
1158 if (kinfo_vdebug)
1159 printf("kinfo: vp changed\n");
1160 bp = savebp;
1161 goto again;
1162 }
985cbdd5
MT
1163 if ((bp + VPTRSZ + VNODESZ <= ewhere) &&
1164 ((error = copyout((caddr_t)&vp, bp, VPTRSZ)) ||
1165 (error = copyout((caddr_t)vp, bp + VPTRSZ,
41185b3b 1166 VNODESZ))))
985cbdd5 1167 return (error);
985cbdd5 1168 bp += VPTRSZ + VNODESZ;
985cbdd5 1169 }
36ef03ec 1170 omp = mp;
54fb9dc2 1171 mp = mp->mnt_next;
36ef03ec 1172 vfs_unbusy(omp);
985cbdd5
MT
1173 } while (mp != rootfs);
1174
1175 *aneeded = bp - where;
1176 if (bp > ewhere)
1177 *acopysize = ewhere - where;
1178 else
1179 *acopysize = bp - where;
1180 return (0);
1181}