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