p_devtmp => p_dupfd; eliminate u.u_error from RETURN macro
[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 *
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16 *
2c957a90 17 * @(#)vfs_subr.c 7.46 (Berkeley) %G%
3c4390e8
KM
18 */
19
20/*
21 * External virtual filesystem routines
22 */
23
24#include "param.h"
25#include "mount.h"
26#include "time.h"
27#include "vnode.h"
0f93ba7b 28#include "specdev.h"
c60798ca
KM
29#include "namei.h"
30#include "ucred.h"
3c4390e8 31#include "errno.h"
ef24f6dd 32#include "malloc.h"
3c4390e8 33
3c4390e8
KM
34/*
35 * Remove a mount point from the list of mounted filesystems.
36 * Unmount of the root is illegal.
37 */
38void
39vfs_remove(mp)
40 register struct mount *mp;
41{
42
43 if (mp == rootfs)
44 panic("vfs_remove: unmounting root");
54fb9dc2
KM
45 mp->mnt_prev->mnt_next = mp->mnt_next;
46 mp->mnt_next->mnt_prev = mp->mnt_prev;
47 mp->mnt_vnodecovered->v_mountedhere = (struct mount *)0;
3c4390e8
KM
48 vfs_unlock(mp);
49}
50
51/*
52 * Lock a filesystem.
53 * Used to prevent access to it while mounting and unmounting.
54 */
55vfs_lock(mp)
56 register struct mount *mp;
57{
58
54fb9dc2
KM
59 while(mp->mnt_flag & MNT_MLOCK) {
60 mp->mnt_flag |= MNT_MWAIT;
594501df
KM
61 sleep((caddr_t)mp, PVFS);
62 }
54fb9dc2 63 mp->mnt_flag |= MNT_MLOCK;
3c4390e8
KM
64 return (0);
65}
66
67/*
68 * Unlock a locked filesystem.
69 * Panic if filesystem is not locked.
70 */
71void
72vfs_unlock(mp)
73 register struct mount *mp;
74{
75
54fb9dc2 76 if ((mp->mnt_flag & MNT_MLOCK) == 0)
36ef03ec 77 panic("vfs_unlock: not locked");
54fb9dc2
KM
78 mp->mnt_flag &= ~MNT_MLOCK;
79 if (mp->mnt_flag & MNT_MWAIT) {
80 mp->mnt_flag &= ~MNT_MWAIT;
3c4390e8
KM
81 wakeup((caddr_t)mp);
82 }
83}
84
36ef03ec
KM
85/*
86 * Mark a mount point as busy.
87 * Used to synchronize access and to delay unmounting.
88 */
89vfs_busy(mp)
90 register struct mount *mp;
91{
92
54fb9dc2
KM
93 while(mp->mnt_flag & MNT_MPBUSY) {
94 mp->mnt_flag |= MNT_MPWANT;
95 sleep((caddr_t)&mp->mnt_flag, PVFS);
36ef03ec 96 }
d8b63609
KM
97 if (mp->mnt_flag & MNT_UNMOUNT)
98 return (1);
54fb9dc2 99 mp->mnt_flag |= MNT_MPBUSY;
36ef03ec
KM
100 return (0);
101}
102
103/*
104 * Free a busy filesystem.
105 * Panic if filesystem is not busy.
106 */
107void
108vfs_unbusy(mp)
109 register struct mount *mp;
110{
111
54fb9dc2 112 if ((mp->mnt_flag & MNT_MPBUSY) == 0)
36ef03ec 113 panic("vfs_unbusy: not busy");
54fb9dc2
KM
114 mp->mnt_flag &= ~MNT_MPBUSY;
115 if (mp->mnt_flag & MNT_MPWANT) {
116 mp->mnt_flag &= ~MNT_MPWANT;
117 wakeup((caddr_t)&mp->mnt_flag);
36ef03ec
KM
118 }
119}
120
3c4390e8
KM
121/*
122 * Lookup a mount point by filesystem identifier.
123 */
124struct mount *
125getvfs(fsid)
126 fsid_t *fsid;
127{
128 register struct mount *mp;
129
d713f801
KM
130 mp = rootfs;
131 do {
54fb9dc2
KM
132 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
133 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
d713f801 134 return (mp);
3c4390e8 135 }
54fb9dc2 136 mp = mp->mnt_next;
d713f801
KM
137 } while (mp != rootfs);
138 return ((struct mount *)0);
3c4390e8
KM
139}
140
141/*
142 * Set vnode attributes to VNOVAL
143 */
144void vattr_null(vap)
145 register struct vattr *vap;
146{
147
148 vap->va_type = VNON;
149 vap->va_mode = vap->va_nlink = vap->va_uid = vap->va_gid =
150 vap->va_fsid = vap->va_fileid = vap->va_size =
8fae943a
KM
151 vap->va_size_rsv = vap->va_blocksize = vap->va_rdev =
152 vap->va_bytes = vap->va_bytes_rsv =
3c4390e8
KM
153 vap->va_atime.tv_sec = vap->va_atime.tv_usec =
154 vap->va_mtime.tv_sec = vap->va_mtime.tv_usec =
8cf4d4fb
KM
155 vap->va_ctime.tv_sec = vap->va_ctime.tv_usec =
156 vap->va_flags = vap->va_gen = VNOVAL;
3c4390e8 157}
c60798ca
KM
158
159/*
160 * Initialize a nameidata structure
161 */
162ndinit(ndp)
163 register struct nameidata *ndp;
164{
165
166 bzero((caddr_t)ndp, sizeof(struct nameidata));
167 ndp->ni_iov = &ndp->ni_nd.nd_iovec;
168 ndp->ni_iovcnt = 1;
169 ndp->ni_base = (caddr_t)&ndp->ni_dent;
170 ndp->ni_rw = UIO_WRITE;
4dbe3531 171 ndp->ni_uioseg = UIO_SYSSPACE;
c60798ca
KM
172}
173
174/*
175 * Duplicate a nameidata structure
176 */
177nddup(ndp, newndp)
178 register struct nameidata *ndp, *newndp;
179{
180
181 ndinit(newndp);
182 newndp->ni_cdir = ndp->ni_cdir;
8fe1c702 183 VREF(newndp->ni_cdir);
c60798ca
KM
184 newndp->ni_rdir = ndp->ni_rdir;
185 if (newndp->ni_rdir)
8fe1c702 186 VREF(newndp->ni_rdir);
c60798ca
KM
187 newndp->ni_cred = ndp->ni_cred;
188 crhold(newndp->ni_cred);
189}
190
191/*
192 * Release a nameidata structure
193 */
194ndrele(ndp)
195 register struct nameidata *ndp;
196{
197
198 vrele(ndp->ni_cdir);
199 if (ndp->ni_rdir)
200 vrele(ndp->ni_rdir);
201 crfree(ndp->ni_cred);
202}
36d09cb1
KM
203
204/*
205 * Routines having to do with the management of the vnode table.
206 */
207struct vnode *vfreeh, **vfreet;
4ef5d036 208extern struct vnodeops dead_vnodeops, spec_vnodeops;
32339c94 209extern void vclean();
1a80f56e 210long numvnodes;
d3664d8f 211struct vattr va_null;
c0de8792 212
36d09cb1 213/*
ef24f6dd 214 * Initialize the vnode structures and initialize each file system type.
36d09cb1 215 */
ef24f6dd 216vfsinit()
36d09cb1 217{
ef24f6dd 218 struct vfsops **vfsp;
36d09cb1 219
ef24f6dd
KM
220 /*
221 * Initialize the vnode name cache
222 */
223 nchinit();
224 /*
225 * Initialize each file system type.
226 */
d3664d8f 227 vattr_null(&va_null);
ef24f6dd
KM
228 for (vfsp = &vfssw[0]; vfsp <= &vfssw[MOUNT_MAXTYPE]; vfsp++) {
229 if (*vfsp == NULL)
230 continue;
231 (*(*vfsp)->vfs_init)();
232 }
36d09cb1
KM
233}
234
235/*
236 * Return the next vnode from the free list.
237 */
238getnewvnode(tag, mp, vops, vpp)
239 enum vtagtype tag;
240 struct mount *mp;
241 struct vnodeops *vops;
242 struct vnode **vpp;
243{
244 register struct vnode *vp, *vq;
245
1a80f56e
KM
246 if (numvnodes < desiredvnodes) {
247 vp = (struct vnode *)malloc(sizeof *vp, M_VNODE, M_WAITOK);
248 bzero((char *)vp, sizeof *vp);
249 numvnodes++;
250 } else {
251 if ((vp = vfreeh) == NULL) {
252 tablefull("vnode");
253 *vpp = 0;
254 return (ENFILE);
255 }
256 if (vp->v_usecount)
257 panic("free vnode isn't");
258 if (vq = vp->v_freef)
259 vq->v_freeb = &vfreeh;
260 else
261 vfreet = &vfreeh;
262 vfreeh = vq;
263 vp->v_freef = NULL;
264 vp->v_freeb = NULL;
265 if (vp->v_type != VBAD)
266 vgone(vp);
267 vp->v_flag = 0;
268 vp->v_shlockc = 0;
269 vp->v_exlockc = 0;
270 vp->v_lastr = 0;
271 vp->v_socket = 0;
36d09cb1 272 }
b027498b 273 vp->v_type = VNON;
36d09cb1
KM
274 cache_purge(vp);
275 vp->v_tag = tag;
ef24f6dd 276 vp->v_op = vops;
36d09cb1
KM
277 insmntque(vp, mp);
278 VREF(vp);
279 *vpp = vp;
280 return (0);
281}
282
283/*
284 * Move a vnode from one mount queue to another.
285 */
286insmntque(vp, mp)
287 register struct vnode *vp;
288 register struct mount *mp;
289{
290 struct vnode *vq;
291
292 /*
293 * Delete from old mount point vnode list, if on one.
294 */
295 if (vp->v_mountb) {
296 if (vq = vp->v_mountf)
297 vq->v_mountb = vp->v_mountb;
298 *vp->v_mountb = vq;
299 }
300 /*
301 * Insert into list of vnodes for the new mount point, if available.
302 */
a45ff315 303 vp->v_mount = mp;
36d09cb1
KM
304 if (mp == NULL) {
305 vp->v_mountf = NULL;
306 vp->v_mountb = NULL;
307 return;
308 }
54fb9dc2
KM
309 if (mp->mnt_mounth) {
310 vp->v_mountf = mp->mnt_mounth;
311 vp->v_mountb = &mp->mnt_mounth;
312 mp->mnt_mounth->v_mountb = &vp->v_mountf;
313 mp->mnt_mounth = vp;
36d09cb1 314 } else {
54fb9dc2
KM
315 mp->mnt_mounth = vp;
316 vp->v_mountb = &mp->mnt_mounth;
36d09cb1
KM
317 vp->v_mountf = NULL;
318 }
319}
320
321/*
ef24f6dd
KM
322 * Create a vnode for a block device.
323 * Used for root filesystem, argdev, and swap areas.
324 * Also used for memory file system special devices.
325 */
326bdevvp(dev, vpp)
327 dev_t dev;
328 struct vnode **vpp;
329{
ef24f6dd
KM
330 register struct vnode *vp;
331 struct vnode *nvp;
332 int error;
333
4ef5d036 334 error = getnewvnode(VT_NON, (struct mount *)0, &spec_vnodeops, &nvp);
ef24f6dd
KM
335 if (error) {
336 *vpp = 0;
337 return (error);
338 }
339 vp = nvp;
340 vp->v_type = VBLK;
c0de8792 341 if (nvp = checkalias(vp, dev, (struct mount *)0)) {
ef24f6dd
KM
342 vput(vp);
343 vp = nvp;
344 }
345 *vpp = vp;
346 return (0);
347}
348
349/*
350 * Check to see if the new vnode represents a special device
351 * for which we already have a vnode (either because of
352 * bdevvp() or because of a different vnode representing
353 * the same block device). If such an alias exists, deallocate
f0556f86 354 * the existing contents and return the aliased vnode. The
ef24f6dd
KM
355 * caller is responsible for filling it with its new contents.
356 */
357struct vnode *
c0de8792 358checkalias(nvp, nvp_rdev, mp)
ef24f6dd 359 register struct vnode *nvp;
c0de8792 360 dev_t nvp_rdev;
ef24f6dd
KM
361 struct mount *mp;
362{
363 register struct vnode *vp;
c0de8792 364 struct vnode **vpp;
ef24f6dd
KM
365
366 if (nvp->v_type != VBLK && nvp->v_type != VCHR)
54fb9dc2 367 return (NULLVP);
c0de8792
KM
368
369 vpp = &speclisth[SPECHASH(nvp_rdev)];
ef24f6dd 370loop:
c0de8792
KM
371 for (vp = *vpp; vp; vp = vp->v_specnext) {
372 if (nvp_rdev != vp->v_rdev || nvp->v_type != vp->v_type)
ef24f6dd 373 continue;
c0de8792
KM
374 /*
375 * Alias, but not in use, so flush it out.
376 */
7f7b7d89 377 if (vp->v_usecount == 0) {
c0de8792
KM
378 vgone(vp);
379 goto loop;
380 }
ef62830d
KM
381 if (vget(vp))
382 goto loop;
ef24f6dd
KM
383 break;
384 }
c0de8792 385 if (vp == NULL || vp->v_tag != VT_NON) {
c0de8792
KM
386 MALLOC(nvp->v_specinfo, struct specinfo *,
387 sizeof(struct specinfo), M_VNODE, M_WAITOK);
388 nvp->v_rdev = nvp_rdev;
7f7b7d89 389 nvp->v_hashchain = vpp;
c0de8792 390 nvp->v_specnext = *vpp;
2c957a90 391 nvp->v_specflags = 0;
c0de8792 392 *vpp = nvp;
40452d5e
KM
393 if (vp != NULL) {
394 nvp->v_flag |= VALIASED;
395 vp->v_flag |= VALIASED;
396 vput(vp);
397 }
54fb9dc2 398 return (NULLVP);
ef24f6dd 399 }
2bae1875
KM
400 VOP_UNLOCK(vp);
401 vclean(vp, 0);
ef24f6dd
KM
402 vp->v_op = nvp->v_op;
403 vp->v_tag = nvp->v_tag;
404 nvp->v_type = VNON;
405 insmntque(vp, mp);
406 return (vp);
407}
408
409/*
410 * Grab a particular vnode from the free list, increment its
411 * reference count and lock it. The vnode lock bit is set the
412 * vnode is being eliminated in vgone. The process is awakened
413 * when the transition is completed, and an error returned to
414 * indicate that the vnode is no longer usable (possibly having
415 * been changed to a new file system type).
36d09cb1
KM
416 */
417vget(vp)
418 register struct vnode *vp;
419{
420 register struct vnode *vq;
421
ef24f6dd
KM
422 if (vp->v_flag & VXLOCK) {
423 vp->v_flag |= VXWANT;
424 sleep((caddr_t)vp, PINOD);
425 return (1);
426 }
7f7b7d89 427 if (vp->v_usecount == 0) {
ef24f6dd
KM
428 if (vq = vp->v_freef)
429 vq->v_freeb = vp->v_freeb;
430 else
431 vfreet = vp->v_freeb;
432 *vp->v_freeb = vq;
433 vp->v_freef = NULL;
434 vp->v_freeb = NULL;
435 }
36d09cb1 436 VREF(vp);
ef24f6dd
KM
437 VOP_LOCK(vp);
438 return (0);
36d09cb1
KM
439}
440
441/*
442 * Vnode reference, just increment the count
443 */
444void vref(vp)
445 struct vnode *vp;
446{
447
7f7b7d89 448 vp->v_usecount++;
36d09cb1
KM
449}
450
451/*
452 * vput(), just unlock and vrele()
453 */
454void vput(vp)
455 register struct vnode *vp;
456{
457 VOP_UNLOCK(vp);
458 vrele(vp);
459}
460
461/*
462 * Vnode release.
463 * If count drops to zero, call inactive routine and return to freelist.
464 */
465void vrele(vp)
466 register struct vnode *vp;
467{
468
469 if (vp == NULL)
ef24f6dd 470 panic("vrele: null vp");
7f7b7d89
KM
471 vp->v_usecount--;
472 if (vp->v_usecount < 0)
0bf84b18 473 vprint("vrele: bad ref count", vp);
7f7b7d89 474 if (vp->v_usecount > 0)
36d09cb1 475 return;
54fb9dc2 476 if (vfreeh == NULLVP) {
36d09cb1
KM
477 /*
478 * insert into empty list
479 */
480 vfreeh = vp;
481 vp->v_freeb = &vfreeh;
36d09cb1
KM
482 } else {
483 /*
484 * insert at tail of list
485 */
486 *vfreet = vp;
487 vp->v_freeb = vfreet;
36d09cb1 488 }
ef24f6dd
KM
489 vp->v_freef = NULL;
490 vfreet = &vp->v_freef;
491 VOP_INACTIVE(vp);
492}
493
7f7b7d89
KM
494/*
495 * Page or buffer structure gets a reference.
496 */
497vhold(vp)
498 register struct vnode *vp;
499{
500
501 vp->v_holdcnt++;
502}
503
504/*
505 * Page or buffer structure frees a reference.
506 */
507holdrele(vp)
508 register struct vnode *vp;
509{
510
511 if (vp->v_holdcnt <= 0)
512 panic("holdrele: holdcnt");
513 vp->v_holdcnt--;
514}
515
f0556f86
KM
516/*
517 * Remove any vnodes in the vnode table belonging to mount point mp.
518 *
519 * If MNT_NOFORCE is specified, there should not be any active ones,
520 * return error if any are found (nb: this is a user error, not a
521 * system error). If MNT_FORCE is specified, detach any active vnodes
522 * that are found.
523 */
524int busyprt = 0; /* patch to print out busy vnodes */
525
526vflush(mp, skipvp, flags)
527 struct mount *mp;
528 struct vnode *skipvp;
529 int flags;
530{
531 register struct vnode *vp, *nvp;
532 int busy = 0;
533
54fb9dc2 534 if ((mp->mnt_flag & MNT_MPBUSY) == 0)
36ef03ec 535 panic("vflush: not busy");
4597dd33 536loop:
54fb9dc2 537 for (vp = mp->mnt_mounth; vp; vp = nvp) {
4597dd33
KM
538 if (vp->v_mount != mp)
539 goto loop;
f0556f86
KM
540 nvp = vp->v_mountf;
541 /*
542 * Skip over a selected vnode.
f0556f86
KM
543 */
544 if (vp == skipvp)
545 continue;
36ef03ec
KM
546 /*
547 * Skip over a vnodes marked VSYSTEM.
548 */
549 if ((flags & SKIPSYSTEM) && (vp->v_flag & VSYSTEM))
550 continue;
f0556f86 551 /*
7f7b7d89 552 * With v_usecount == 0, all we need to do is clear
f0556f86
KM
553 * out the vnode data structures and we are done.
554 */
7f7b7d89 555 if (vp->v_usecount == 0) {
f0556f86
KM
556 vgone(vp);
557 continue;
558 }
559 /*
560 * For block or character devices, revert to an
561 * anonymous device. For all other files, just kill them.
562 */
36ef03ec 563 if (flags & FORCECLOSE) {
f0556f86
KM
564 if (vp->v_type != VBLK && vp->v_type != VCHR) {
565 vgone(vp);
566 } else {
567 vclean(vp, 0);
568 vp->v_op = &spec_vnodeops;
569 insmntque(vp, (struct mount *)0);
570 }
571 continue;
572 }
573 if (busyprt)
0bf84b18 574 vprint("vflush: busy vnode", vp);
f0556f86
KM
575 busy++;
576 }
577 if (busy)
578 return (EBUSY);
579 return (0);
580}
581
ef24f6dd
KM
582/*
583 * Disassociate the underlying file system from a vnode.
ef24f6dd 584 */
36ef03ec 585void vclean(vp, flags)
ef24f6dd 586 register struct vnode *vp;
36ef03ec 587 long flags;
ef24f6dd
KM
588{
589 struct vnodeops *origops;
2bae1875 590 int active;
ef24f6dd 591
2bae1875
KM
592 /*
593 * Check to see if the vnode is in use.
0bf84b18
KM
594 * If so we have to reference it before we clean it out
595 * so that its count cannot fall to zero and generate a
596 * race against ourselves to recycle it.
2bae1875 597 */
7f7b7d89 598 if (active = vp->v_usecount)
2bae1875 599 VREF(vp);
2bae1875
KM
600 /*
601 * Prevent the vnode from being recycled or
602 * brought into use while we clean it out.
603 */
0bf84b18
KM
604 if (vp->v_flag & VXLOCK)
605 panic("vclean: deadlock");
ef24f6dd 606 vp->v_flag |= VXLOCK;
0bf84b18
KM
607 /*
608 * Even if the count is zero, the VOP_INACTIVE routine may still
609 * have the object locked while it cleans it out. The VOP_LOCK
610 * ensures that the VOP_INACTIVE routine is done with its work.
611 * For active vnodes, it ensures that no other activity can
612 * occur while the buffer list is being cleaned out.
613 */
614 VOP_LOCK(vp);
36ef03ec 615 if (flags & DOCLOSE)
0bf84b18 616 vinvalbuf(vp, 1);
ef24f6dd
KM
617 /*
618 * Prevent any further operations on the vnode from
619 * being passed through to the old file system.
620 */
621 origops = vp->v_op;
622 vp->v_op = &dead_vnodeops;
623 vp->v_tag = VT_NON;
624 /*
2bae1875
KM
625 * If purging an active vnode, it must be unlocked, closed,
626 * and deactivated before being reclaimed.
ef24f6dd 627 */
0bf84b18 628 (*(origops->vn_unlock))(vp);
2bae1875 629 if (active) {
36ef03ec 630 if (flags & DOCLOSE)
2bae1875 631 (*(origops->vn_close))(vp, 0, NOCRED);
ef24f6dd
KM
632 (*(origops->vn_inactive))(vp);
633 }
634 /*
635 * Reclaim the vnode.
636 */
637 if ((*(origops->vn_reclaim))(vp))
638 panic("vclean: cannot reclaim");
2bae1875
KM
639 if (active)
640 vrele(vp);
ef24f6dd
KM
641 /*
642 * Done with purge, notify sleepers in vget of the grim news.
643 */
644 vp->v_flag &= ~VXLOCK;
645 if (vp->v_flag & VXWANT) {
646 vp->v_flag &= ~VXWANT;
647 wakeup((caddr_t)vp);
648 }
649}
650
ef62830d
KM
651/*
652 * Eliminate all activity associated with the requested vnode
653 * and with all vnodes aliased to the requested vnode.
654 */
655void vgoneall(vp)
656 register struct vnode *vp;
657{
7f7b7d89 658 register struct vnode *vq;
ef62830d 659
7a7b3a95
KM
660 if (vp->v_flag & VALIASED) {
661 /*
662 * If a vgone (or vclean) is already in progress,
663 * wait until it is done and return.
664 */
665 if (vp->v_flag & VXLOCK) {
666 vp->v_flag |= VXWANT;
667 sleep((caddr_t)vp, PINOD);
668 return;
669 }
670 /*
671 * Ensure that vp will not be vgone'd while we
672 * are eliminating its aliases.
673 */
674 vp->v_flag |= VXLOCK;
675 while (vp->v_flag & VALIASED) {
676 for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
677 if (vq->v_rdev != vp->v_rdev ||
678 vq->v_type != vp->v_type || vp == vq)
679 continue;
680 vgone(vq);
681 break;
682 }
ef62830d 683 }
7a7b3a95
KM
684 /*
685 * Remove the lock so that vgone below will
686 * really eliminate the vnode after which time
687 * vgone will awaken any sleepers.
688 */
689 vp->v_flag &= ~VXLOCK;
ef62830d
KM
690 }
691 vgone(vp);
692}
693
ef24f6dd
KM
694/*
695 * Eliminate all activity associated with a vnode
696 * in preparation for reuse.
697 */
698void vgone(vp)
699 register struct vnode *vp;
700{
7f7b7d89 701 register struct vnode *vq;
c0de8792
KM
702 struct vnode *vx;
703 long count;
ef24f6dd 704
4f55e3ec
KM
705 /*
706 * If a vgone (or vclean) is already in progress,
707 * wait until it is done and return.
708 */
709 if (vp->v_flag & VXLOCK) {
710 vp->v_flag |= VXWANT;
711 sleep((caddr_t)vp, PINOD);
712 return;
713 }
ef24f6dd
KM
714 /*
715 * Clean out the filesystem specific data.
716 */
36ef03ec 717 vclean(vp, DOCLOSE);
ef24f6dd
KM
718 /*
719 * Delete from old mount point vnode list, if on one.
720 */
721 if (vp->v_mountb) {
722 if (vq = vp->v_mountf)
723 vq->v_mountb = vp->v_mountb;
724 *vp->v_mountb = vq;
725 vp->v_mountf = NULL;
726 vp->v_mountb = NULL;
727 }
728 /*
729 * If special device, remove it from special device alias list.
730 */
731 if (vp->v_type == VBLK || vp->v_type == VCHR) {
7f7b7d89
KM
732 if (*vp->v_hashchain == vp) {
733 *vp->v_hashchain = vp->v_specnext;
ef24f6dd 734 } else {
7f7b7d89 735 for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
c0de8792 736 if (vq->v_specnext != vp)
ef24f6dd 737 continue;
c0de8792 738 vq->v_specnext = vp->v_specnext;
ef24f6dd
KM
739 break;
740 }
c0de8792 741 if (vq == NULL)
ef24f6dd
KM
742 panic("missing bdev");
743 }
c0de8792 744 if (vp->v_flag & VALIASED) {
7f7b7d89
KM
745 count = 0;
746 for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
de81e10c
KM
747 if (vq->v_rdev != vp->v_rdev ||
748 vq->v_type != vp->v_type)
c0de8792
KM
749 continue;
750 count++;
751 vx = vq;
752 }
753 if (count == 0)
754 panic("missing alias");
755 if (count == 1)
756 vx->v_flag &= ~VALIASED;
757 vp->v_flag &= ~VALIASED;
758 }
759 FREE(vp->v_specinfo, M_VNODE);
760 vp->v_specinfo = NULL;
ef24f6dd
KM
761 }
762 /*
763 * If it is on the freelist, move it to the head of the list.
764 */
765 if (vp->v_freeb) {
766 if (vq = vp->v_freef)
767 vq->v_freeb = vp->v_freeb;
768 else
769 vfreet = vp->v_freeb;
770 *vp->v_freeb = vq;
771 vp->v_freef = vfreeh;
772 vp->v_freeb = &vfreeh;
773 vfreeh->v_freeb = &vp->v_freef;
774 vfreeh = vp;
775 }
2bae1875 776 vp->v_type = VBAD;
36d09cb1 777}
ef62830d 778
2bcd6066
KM
779/*
780 * Lookup a vnode by device number.
781 */
782vfinddev(dev, type, vpp)
783 dev_t dev;
784 enum vtype type;
785 struct vnode **vpp;
786{
787 register struct vnode *vp;
788
789 for (vp = speclisth[SPECHASH(dev)]; vp; vp = vp->v_specnext) {
790 if (dev != vp->v_rdev || type != vp->v_type)
791 continue;
792 *vpp = vp;
793 return (0);
794 }
795 return (1);
796}
797
ef62830d
KM
798/*
799 * Calculate the total number of references to a special device.
800 */
801vcount(vp)
802 register struct vnode *vp;
803{
7f7b7d89 804 register struct vnode *vq;
ef62830d
KM
805 int count;
806
807 if ((vp->v_flag & VALIASED) == 0)
7f7b7d89 808 return (vp->v_usecount);
ef62830d 809loop:
7f7b7d89 810 for (count = 0, vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
de81e10c 811 if (vq->v_rdev != vp->v_rdev || vq->v_type != vp->v_type)
ef62830d
KM
812 continue;
813 /*
814 * Alias, but not in use, so flush it out.
815 */
7f7b7d89 816 if (vq->v_usecount == 0) {
ef62830d
KM
817 vgone(vq);
818 goto loop;
819 }
7f7b7d89 820 count += vq->v_usecount;
ef62830d
KM
821 }
822 return (count);
823}
0bf84b18
KM
824
825/*
826 * Print out a description of a vnode.
827 */
828static char *typename[] =
61f846a8 829 { "VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD" };
0bf84b18
KM
830
831vprint(label, vp)
832 char *label;
833 register struct vnode *vp;
834{
f2f730c6 835 char buf[64];
0bf84b18
KM
836
837 if (label != NULL)
838 printf("%s: ", label);
f2f730c6 839 printf("type %s, usecount %d, refcount %d,", typename[vp->v_type],
7f7b7d89 840 vp->v_usecount, vp->v_holdcnt);
f2f730c6
KM
841 buf[0] = '\0';
842 if (vp->v_flag & VROOT)
843 strcat(buf, "|VROOT");
844 if (vp->v_flag & VTEXT)
845 strcat(buf, "|VTEXT");
36ef03ec
KM
846 if (vp->v_flag & VSYSTEM)
847 strcat(buf, "|VSYSTEM");
f2f730c6
KM
848 if (vp->v_flag & VEXLOCK)
849 strcat(buf, "|VEXLOCK");
850 if (vp->v_flag & VSHLOCK)
851 strcat(buf, "|VSHLOCK");
852 if (vp->v_flag & VLWAIT)
853 strcat(buf, "|VLWAIT");
36ef03ec
KM
854 if (vp->v_flag & VXLOCK)
855 strcat(buf, "|VXLOCK");
856 if (vp->v_flag & VXWANT)
857 strcat(buf, "|VXWANT");
f2f730c6
KM
858 if (vp->v_flag & VBWAIT)
859 strcat(buf, "|VBWAIT");
36ef03ec
KM
860 if (vp->v_flag & VALIASED)
861 strcat(buf, "|VALIASED");
f2f730c6
KM
862 if (buf[0] != '\0')
863 printf(" flags (%s)", &buf[1]);
864 printf("\n\t");
0bf84b18
KM
865 VOP_PRINT(vp);
866}
985cbdd5
MT
867
868int kinfo_vdebug = 1;
869int kinfo_vgetfailed;
870#define KINFO_VNODESLOP 10
871/*
872 * Dump vnode list (via kinfo).
873 * Copyout address of vnode followed by vnode.
874 */
875kinfo_vnode(op, where, acopysize, arg, aneeded)
876 char *where;
877 int *acopysize, *aneeded;
878{
879 register struct mount *mp = rootfs;
36ef03ec 880 struct mount *omp;
985cbdd5
MT
881 struct vnode *vp;
882 register needed = 0;
883 register char *bp = where, *savebp;
884 char *ewhere = where + *acopysize;
885 int error;
886
887#define VPTRSZ sizeof (struct vnode *)
888#define VNODESZ sizeof (struct vnode)
889 if (where == NULL) {
890 *aneeded = (numvnodes + KINFO_VNODESLOP) * (VPTRSZ + VNODESZ);
891 return (0);
892 }
893
985cbdd5 894 do {
36ef03ec 895 if (vfs_busy(mp)) {
54fb9dc2 896 mp = mp->mnt_next;
36ef03ec
KM
897 continue;
898 }
985cbdd5
MT
899 savebp = bp;
900again:
4597dd33 901 for (vp = mp->mnt_mounth; vp; vp = vp->v_mountf) {
41185b3b
KM
902 /*
903 * Check that the vp is still associated with
904 * this filesystem. RACE: could have been
905 * recycled onto the same filesystem.
906 */
4597dd33
KM
907 if (vp->v_mount != mp) {
908 if (kinfo_vdebug)
909 printf("kinfo: vp changed\n");
910 bp = savebp;
911 goto again;
912 }
985cbdd5
MT
913 if ((bp + VPTRSZ + VNODESZ <= ewhere) &&
914 ((error = copyout((caddr_t)&vp, bp, VPTRSZ)) ||
915 (error = copyout((caddr_t)vp, bp + VPTRSZ,
41185b3b 916 VNODESZ))))
985cbdd5 917 return (error);
985cbdd5 918 bp += VPTRSZ + VNODESZ;
985cbdd5 919 }
36ef03ec 920 omp = mp;
54fb9dc2 921 mp = mp->mnt_next;
36ef03ec 922 vfs_unbusy(omp);
985cbdd5
MT
923 } while (mp != rootfs);
924
925 *aneeded = bp - where;
926 if (bp > ewhere)
927 *acopysize = ewhere - where;
928 else
929 *acopysize = bp - where;
930 return (0);
931}