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