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