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