make calls to read and write clustering
[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 *
da374605 7 * @(#)vfs_subr.c 7.90 (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;
da374605
KM
679 /*
680 * If WRITECLOSE is set, only flush out regular file
681 * vnodes open for writing.
682 */
683 if ((flags & WRITECLOSE) &&
684 (vp->v_writecount == 0 || vp->v_type != VREG))
685 continue;
f0556f86 686 /*
7f7b7d89 687 * With v_usecount == 0, all we need to do is clear
f0556f86
KM
688 * out the vnode data structures and we are done.
689 */
7f7b7d89 690 if (vp->v_usecount == 0) {
f0556f86
KM
691 vgone(vp);
692 continue;
693 }
694 /*
da374605 695 * If FORCECLOSE is set, forcibly close the vnode.
f0556f86
KM
696 * For block or character devices, revert to an
697 * anonymous device. For all other files, just kill them.
698 */
36ef03ec 699 if (flags & FORCECLOSE) {
f0556f86
KM
700 if (vp->v_type != VBLK && vp->v_type != VCHR) {
701 vgone(vp);
702 } else {
703 vclean(vp, 0);
9342689a 704 vp->v_op = spec_vnodeop_p;
f0556f86
KM
705 insmntque(vp, (struct mount *)0);
706 }
707 continue;
708 }
709 if (busyprt)
0bf84b18 710 vprint("vflush: busy vnode", vp);
f0556f86
KM
711 busy++;
712 }
713 if (busy)
714 return (EBUSY);
715 return (0);
716}
717
ef24f6dd
KM
718/*
719 * Disassociate the underlying file system from a vnode.
ef24f6dd 720 */
ecf75a7d
KM
721void
722vclean(vp, flags)
ef24f6dd 723 register struct vnode *vp;
aacc1bff 724 int flags;
ef24f6dd 725{
2bae1875 726 int active;
ef24f6dd 727
2bae1875
KM
728 /*
729 * Check to see if the vnode is in use.
0bf84b18
KM
730 * If so we have to reference it before we clean it out
731 * so that its count cannot fall to zero and generate a
732 * race against ourselves to recycle it.
2bae1875 733 */
7f7b7d89 734 if (active = vp->v_usecount)
2bae1875 735 VREF(vp);
669df1aa
KM
736 /*
737 * Even if the count is zero, the VOP_INACTIVE routine may still
738 * have the object locked while it cleans it out. The VOP_LOCK
739 * ensures that the VOP_INACTIVE routine is done with its work.
740 * For active vnodes, it ensures that no other activity can
741 * occur while the underlying object is being cleaned out.
742 */
743 VOP_LOCK(vp);
2bae1875
KM
744 /*
745 * Prevent the vnode from being recycled or
746 * brought into use while we clean it out.
747 */
0bf84b18
KM
748 if (vp->v_flag & VXLOCK)
749 panic("vclean: deadlock");
ef24f6dd 750 vp->v_flag |= VXLOCK;
0bf84b18 751 /*
669df1aa 752 * Clean out any buffers associated with the vnode.
0bf84b18 753 */
36ef03ec 754 if (flags & DOCLOSE)
d024c2ce 755 vinvalbuf(vp, 1, NOCRED, NULL);
ef24f6dd 756 /*
669df1aa
KM
757 * Any other processes trying to obtain this lock must first
758 * wait for VXLOCK to clear, then call the new lock operation.
ef24f6dd 759 */
669df1aa 760 VOP_UNLOCK(vp);
ef24f6dd 761 /*
669df1aa
KM
762 * If purging an active vnode, it must be closed and
763 * deactivated before being reclaimed.
ef24f6dd 764 */
2bae1875 765 if (active) {
669df1aa
KM
766 if (flags & DOCLOSE)
767 VOP_CLOSE(vp, IO_NDELAY, NOCRED, NULL);
768 VOP_INACTIVE(vp);
ef24f6dd
KM
769 }
770 /*
771 * Reclaim the vnode.
772 */
669df1aa 773 if (VOP_RECLAIM(vp))
ef24f6dd 774 panic("vclean: cannot reclaim");
2bae1875
KM
775 if (active)
776 vrele(vp);
38c46eee 777
ef24f6dd 778 /*
669df1aa 779 * Done with purge, notify sleepers of the grim news.
ef24f6dd 780 */
669df1aa
KM
781 vp->v_op = dead_vnodeop_p;
782 vp->v_tag = VT_NON;
ef24f6dd
KM
783 vp->v_flag &= ~VXLOCK;
784 if (vp->v_flag & VXWANT) {
785 vp->v_flag &= ~VXWANT;
786 wakeup((caddr_t)vp);
787 }
788}
789
ef62830d
KM
790/*
791 * Eliminate all activity associated with the requested vnode
792 * and with all vnodes aliased to the requested vnode.
793 */
794void vgoneall(vp)
795 register struct vnode *vp;
796{
7f7b7d89 797 register struct vnode *vq;
ef62830d 798
7a7b3a95
KM
799 if (vp->v_flag & VALIASED) {
800 /*
801 * If a vgone (or vclean) is already in progress,
802 * wait until it is done and return.
803 */
804 if (vp->v_flag & VXLOCK) {
805 vp->v_flag |= VXWANT;
806 sleep((caddr_t)vp, PINOD);
807 return;
808 }
809 /*
810 * Ensure that vp will not be vgone'd while we
811 * are eliminating its aliases.
812 */
813 vp->v_flag |= VXLOCK;
814 while (vp->v_flag & VALIASED) {
815 for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
816 if (vq->v_rdev != vp->v_rdev ||
817 vq->v_type != vp->v_type || vp == vq)
818 continue;
819 vgone(vq);
820 break;
821 }
ef62830d 822 }
7a7b3a95
KM
823 /*
824 * Remove the lock so that vgone below will
825 * really eliminate the vnode after which time
826 * vgone will awaken any sleepers.
827 */
828 vp->v_flag &= ~VXLOCK;
ef62830d
KM
829 }
830 vgone(vp);
831}
832
ef24f6dd
KM
833/*
834 * Eliminate all activity associated with a vnode
835 * in preparation for reuse.
836 */
837void vgone(vp)
838 register struct vnode *vp;
839{
7f7b7d89 840 register struct vnode *vq;
c0de8792 841 struct vnode *vx;
ef24f6dd 842
4f55e3ec
KM
843 /*
844 * If a vgone (or vclean) is already in progress,
845 * wait until it is done and return.
846 */
847 if (vp->v_flag & VXLOCK) {
848 vp->v_flag |= VXWANT;
849 sleep((caddr_t)vp, PINOD);
850 return;
851 }
ef24f6dd
KM
852 /*
853 * Clean out the filesystem specific data.
854 */
36ef03ec 855 vclean(vp, DOCLOSE);
ef24f6dd
KM
856 /*
857 * Delete from old mount point vnode list, if on one.
858 */
859 if (vp->v_mountb) {
860 if (vq = vp->v_mountf)
861 vq->v_mountb = vp->v_mountb;
862 *vp->v_mountb = vq;
863 vp->v_mountf = NULL;
864 vp->v_mountb = NULL;
d10e9258 865 vp->v_mount = NULL;
ef24f6dd
KM
866 }
867 /*
868 * If special device, remove it from special device alias list.
869 */
870 if (vp->v_type == VBLK || vp->v_type == VCHR) {
7f7b7d89
KM
871 if (*vp->v_hashchain == vp) {
872 *vp->v_hashchain = vp->v_specnext;
ef24f6dd 873 } else {
7f7b7d89 874 for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
c0de8792 875 if (vq->v_specnext != vp)
ef24f6dd 876 continue;
c0de8792 877 vq->v_specnext = vp->v_specnext;
ef24f6dd
KM
878 break;
879 }
c0de8792 880 if (vq == NULL)
ef24f6dd
KM
881 panic("missing bdev");
882 }
c0de8792 883 if (vp->v_flag & VALIASED) {
4d1ee2eb 884 vx = NULL;
7f7b7d89 885 for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
de81e10c
KM
886 if (vq->v_rdev != vp->v_rdev ||
887 vq->v_type != vp->v_type)
c0de8792 888 continue;
4d1ee2eb
CT
889 if (vx)
890 break;
c0de8792
KM
891 vx = vq;
892 }
4d1ee2eb 893 if (vx == NULL)
c0de8792 894 panic("missing alias");
4d1ee2eb 895 if (vq == NULL)
c0de8792
KM
896 vx->v_flag &= ~VALIASED;
897 vp->v_flag &= ~VALIASED;
898 }
899 FREE(vp->v_specinfo, M_VNODE);
900 vp->v_specinfo = NULL;
ef24f6dd
KM
901 }
902 /*
3387ef89
KM
903 * If it is on the freelist and not already at the head,
904 * move it to the head of the list.
ef24f6dd 905 */
3387ef89 906 if (vp->v_freeb && vfreeh != vp) {
ef24f6dd
KM
907 if (vq = vp->v_freef)
908 vq->v_freeb = vp->v_freeb;
909 else
910 vfreet = vp->v_freeb;
911 *vp->v_freeb = vq;
912 vp->v_freef = vfreeh;
913 vp->v_freeb = &vfreeh;
914 vfreeh->v_freeb = &vp->v_freef;
915 vfreeh = vp;
916 }
2bae1875 917 vp->v_type = VBAD;
36d09cb1 918}
ef62830d 919
2bcd6066
KM
920/*
921 * Lookup a vnode by device number.
922 */
923vfinddev(dev, type, vpp)
924 dev_t dev;
925 enum vtype type;
926 struct vnode **vpp;
927{
928 register struct vnode *vp;
929
930 for (vp = speclisth[SPECHASH(dev)]; vp; vp = vp->v_specnext) {
931 if (dev != vp->v_rdev || type != vp->v_type)
932 continue;
933 *vpp = vp;
934 return (0);
935 }
936 return (1);
937}
938
ef62830d
KM
939/*
940 * Calculate the total number of references to a special device.
941 */
942vcount(vp)
943 register struct vnode *vp;
944{
7f7b7d89 945 register struct vnode *vq;
ef62830d
KM
946 int count;
947
948 if ((vp->v_flag & VALIASED) == 0)
7f7b7d89 949 return (vp->v_usecount);
ef62830d 950loop:
7f7b7d89 951 for (count = 0, vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
de81e10c 952 if (vq->v_rdev != vp->v_rdev || vq->v_type != vp->v_type)
ef62830d
KM
953 continue;
954 /*
955 * Alias, but not in use, so flush it out.
956 */
7f7b7d89 957 if (vq->v_usecount == 0) {
ef62830d
KM
958 vgone(vq);
959 goto loop;
960 }
7f7b7d89 961 count += vq->v_usecount;
ef62830d
KM
962 }
963 return (count);
964}
0bf84b18
KM
965
966/*
967 * Print out a description of a vnode.
968 */
969static char *typename[] =
61f846a8 970 { "VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD" };
0bf84b18
KM
971
972vprint(label, vp)
973 char *label;
974 register struct vnode *vp;
975{
f2f730c6 976 char buf[64];
0bf84b18
KM
977
978 if (label != NULL)
979 printf("%s: ", label);
65c3b3a8
KM
980 printf("type %s, usecount %d, writecount %d, refcount %d,",
981 typename[vp->v_type], vp->v_usecount, vp->v_writecount,
982 vp->v_holdcnt);
f2f730c6
KM
983 buf[0] = '\0';
984 if (vp->v_flag & VROOT)
985 strcat(buf, "|VROOT");
986 if (vp->v_flag & VTEXT)
987 strcat(buf, "|VTEXT");
36ef03ec
KM
988 if (vp->v_flag & VSYSTEM)
989 strcat(buf, "|VSYSTEM");
36ef03ec
KM
990 if (vp->v_flag & VXLOCK)
991 strcat(buf, "|VXLOCK");
992 if (vp->v_flag & VXWANT)
993 strcat(buf, "|VXWANT");
f2f730c6
KM
994 if (vp->v_flag & VBWAIT)
995 strcat(buf, "|VBWAIT");
36ef03ec
KM
996 if (vp->v_flag & VALIASED)
997 strcat(buf, "|VALIASED");
f2f730c6
KM
998 if (buf[0] != '\0')
999 printf(" flags (%s)", &buf[1]);
1000 printf("\n\t");
0bf84b18
KM
1001 VOP_PRINT(vp);
1002}
985cbdd5 1003
34c62e18
KM
1004#ifdef DEBUG
1005/*
1006 * List all of the locked vnodes in the system.
1007 * Called when debugging the kernel.
1008 */
1009printlockedvnodes()
1010{
1011 register struct mount *mp;
1012 register struct vnode *vp;
1013
1014 printf("Locked vnodes\n");
1015 mp = rootfs;
1016 do {
1017 for (vp = mp->mnt_mounth; vp; vp = vp->v_mountf)
1018 if (VOP_ISLOCKED(vp))
1019 vprint((char *)0, vp);
1020 mp = mp->mnt_next;
1021 } while (mp != rootfs);
1022}
1023#endif
1024
985cbdd5
MT
1025int kinfo_vdebug = 1;
1026int kinfo_vgetfailed;
1027#define KINFO_VNODESLOP 10
1028/*
1029 * Dump vnode list (via kinfo).
1030 * Copyout address of vnode followed by vnode.
1031 */
aacc1bff 1032/* ARGSUSED */
985cbdd5 1033kinfo_vnode(op, where, acopysize, arg, aneeded)
aacc1bff 1034 int op;
985cbdd5 1035 char *where;
aacc1bff 1036 int *acopysize, arg, *aneeded;
985cbdd5
MT
1037{
1038 register struct mount *mp = rootfs;
36ef03ec 1039 struct mount *omp;
985cbdd5 1040 struct vnode *vp;
985cbdd5 1041 register char *bp = where, *savebp;
5bf57294 1042 char *ewhere;
985cbdd5
MT
1043 int error;
1044
1045#define VPTRSZ sizeof (struct vnode *)
1046#define VNODESZ sizeof (struct vnode)
1047 if (where == NULL) {
1048 *aneeded = (numvnodes + KINFO_VNODESLOP) * (VPTRSZ + VNODESZ);
1049 return (0);
1050 }
5bf57294 1051 ewhere = where + *acopysize;
985cbdd5 1052
985cbdd5 1053 do {
36ef03ec 1054 if (vfs_busy(mp)) {
54fb9dc2 1055 mp = mp->mnt_next;
36ef03ec
KM
1056 continue;
1057 }
985cbdd5
MT
1058 savebp = bp;
1059again:
4597dd33 1060 for (vp = mp->mnt_mounth; vp; vp = vp->v_mountf) {
41185b3b
KM
1061 /*
1062 * Check that the vp is still associated with
1063 * this filesystem. RACE: could have been
1064 * recycled onto the same filesystem.
1065 */
4597dd33
KM
1066 if (vp->v_mount != mp) {
1067 if (kinfo_vdebug)
1068 printf("kinfo: vp changed\n");
1069 bp = savebp;
1070 goto again;
1071 }
985cbdd5
MT
1072 if ((bp + VPTRSZ + VNODESZ <= ewhere) &&
1073 ((error = copyout((caddr_t)&vp, bp, VPTRSZ)) ||
1074 (error = copyout((caddr_t)vp, bp + VPTRSZ,
41185b3b 1075 VNODESZ))))
985cbdd5 1076 return (error);
985cbdd5 1077 bp += VPTRSZ + VNODESZ;
985cbdd5 1078 }
36ef03ec 1079 omp = mp;
54fb9dc2 1080 mp = mp->mnt_next;
36ef03ec 1081 vfs_unbusy(omp);
985cbdd5
MT
1082 } while (mp != rootfs);
1083
1084 *aneeded = bp - where;
1085 if (bp > ewhere)
1086 *acopysize = ewhere - where;
1087 else
1088 *acopysize = bp - where;
1089 return (0);
1090}