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