]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/vfs_subr.c
Reviewed by: Julian Elischer <julian@whistle.com>
[FreeBSD/FreeBSD.git] / sys / kern / vfs_subr.c
1 /*
2  * Copyright (c) 1989, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  * (c) UNIX System Laboratories, Inc.
5  * All or some portions of this file are derived from material licensed
6  * to the University of California by American Telephone and Telegraph
7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8  * the permission of UNIX System Laboratories, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *      This product includes software developed by the University of
21  *      California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *      @(#)vfs_subr.c  8.31 (Berkeley) 5/26/95
39  * $Id: vfs_subr.c,v 1.187 1999/02/19 17:36:58 dillon Exp $
40  */
41
42 /*
43  * External virtual filesystem routines
44  */
45 #include "opt_ddb.h"
46
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/conf.h>
50 #include <sys/fcntl.h>
51 #include <sys/kernel.h>
52 #include <sys/proc.h>
53 #include <sys/malloc.h>
54 #include <sys/mount.h>
55 #include <sys/socket.h>
56 #include <sys/vnode.h>
57 #include <sys/stat.h>
58 #include <sys/buf.h>
59 #include <sys/domain.h>
60 #include <sys/dirent.h>
61 #include <sys/vmmeter.h>
62
63 #include <machine/limits.h>
64
65 #include <vm/vm.h>
66 #include <vm/vm_param.h>
67 #include <vm/vm_prot.h>
68 #include <vm/vm_object.h>
69 #include <vm/vm_extern.h>
70 #include <vm/pmap.h>
71 #include <vm/vm_map.h>
72 #include <vm/vm_page.h>
73 #include <vm/vm_pager.h>
74 #include <vm/vnode_pager.h>
75 #include <vm/vm_zone.h>
76 #include <sys/sysctl.h>
77
78 #include <miscfs/specfs/specdev.h>
79
80 static MALLOC_DEFINE(M_NETADDR, "Export Host", "Export host address structure");
81
82 static void     insmntque __P((struct vnode *vp, struct mount *mp));
83 static void     vclean __P((struct vnode *vp, int flags, struct proc *p));
84 static void     vfree __P((struct vnode *));
85 static void     vgonel __P((struct vnode *vp, struct proc *p));
86 static unsigned long    numvnodes;
87 SYSCTL_INT(_debug, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0, "");
88
89 enum vtype iftovt_tab[16] = {
90         VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
91         VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
92 };
93 int vttoif_tab[9] = {
94         0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
95         S_IFSOCK, S_IFIFO, S_IFMT,
96 };
97
98 static TAILQ_HEAD(freelst, vnode) vnode_free_list;      /* vnode free list */
99 struct tobefreelist vnode_tobefree_list;        /* vnode free list */
100
101 static u_long wantfreevnodes = 25;
102 SYSCTL_INT(_debug, OID_AUTO, wantfreevnodes, CTLFLAG_RW, &wantfreevnodes, 0, "");
103 static u_long freevnodes = 0;
104 SYSCTL_INT(_debug, OID_AUTO, freevnodes, CTLFLAG_RD, &freevnodes, 0, "");
105
106 int vfs_ioopt = 0;
107 #ifdef ENABLE_VFS_IOOPT
108 SYSCTL_INT(_vfs, OID_AUTO, ioopt, CTLFLAG_RW, &vfs_ioopt, 0, "");
109 #endif
110
111 struct mntlist mountlist;       /* mounted filesystem list */
112 struct simplelock mountlist_slock;
113 struct simplelock mntvnode_slock;
114 int     nfs_mount_type = -1;
115 #ifndef NULL_SIMPLELOCKS
116 static struct simplelock mntid_slock;
117 static struct simplelock vnode_free_list_slock;
118 static struct simplelock spechash_slock;
119 #endif
120 struct nfs_public nfs_pub;      /* publicly exported FS */
121 static vm_zone_t vnode_zone;
122
123 /*
124  * The workitem queue.
125  */
126 #define SYNCER_MAXDELAY         32
127 static int syncer_maxdelay = SYNCER_MAXDELAY;   /* maximum delay time */
128 time_t syncdelay =              30;
129 int rushjob;                            /* number of slots to run ASAP */
130
131 static int syncer_delayno = 0;
132 static long syncer_mask; 
133 LIST_HEAD(synclist, vnode);
134 static struct synclist *syncer_workitem_pending;
135
136 int desiredvnodes;
137 SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RW, &desiredvnodes, 0, "");
138
139 static void     vfs_free_addrlist __P((struct netexport *nep));
140 static int      vfs_free_netcred __P((struct radix_node *rn, void *w));
141 static int      vfs_hang_addrlist __P((struct mount *mp, struct netexport *nep,
142                                        struct export_args *argp));
143
144 /*
145  * Initialize the vnode management data structures.
146  */
147 void
148 vntblinit()
149 {
150
151         desiredvnodes = maxproc + cnt.v_page_count / 4;
152         simple_lock_init(&mntvnode_slock);
153         simple_lock_init(&mntid_slock);
154         simple_lock_init(&spechash_slock);
155         TAILQ_INIT(&vnode_free_list);
156         TAILQ_INIT(&vnode_tobefree_list);
157         simple_lock_init(&vnode_free_list_slock);
158         CIRCLEQ_INIT(&mountlist);
159         vnode_zone = zinit("VNODE", sizeof (struct vnode), 0, 0, 5);
160         /*
161          * Initialize the filesystem syncer.
162          */     
163         syncer_workitem_pending = hashinit(syncer_maxdelay, M_VNODE, 
164                 &syncer_mask);
165         syncer_maxdelay = syncer_mask + 1;
166 }
167
168 /*
169  * Mark a mount point as busy. Used to synchronize access and to delay
170  * unmounting. Interlock is not released on failure.
171  */
172 int
173 vfs_busy(mp, flags, interlkp, p)
174         struct mount *mp;
175         int flags;
176         struct simplelock *interlkp;
177         struct proc *p;
178 {
179         int lkflags;
180
181         if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
182                 if (flags & LK_NOWAIT)
183                         return (ENOENT);
184                 mp->mnt_kern_flag |= MNTK_MWAIT;
185                 if (interlkp) {
186                         simple_unlock(interlkp);
187                 }
188                 /*
189                  * Since all busy locks are shared except the exclusive
190                  * lock granted when unmounting, the only place that a
191                  * wakeup needs to be done is at the release of the
192                  * exclusive lock at the end of dounmount.
193                  */
194                 tsleep((caddr_t)mp, PVFS, "vfs_busy", 0);
195                 if (interlkp) {
196                         simple_lock(interlkp);
197                 }
198                 return (ENOENT);
199         }
200         lkflags = LK_SHARED | LK_NOPAUSE;
201         if (interlkp)
202                 lkflags |= LK_INTERLOCK;
203         if (lockmgr(&mp->mnt_lock, lkflags, interlkp, p))
204                 panic("vfs_busy: unexpected lock failure");
205         return (0);
206 }
207
208 /*
209  * Free a busy filesystem.
210  */
211 void
212 vfs_unbusy(mp, p)
213         struct mount *mp;
214         struct proc *p;
215 {
216
217         lockmgr(&mp->mnt_lock, LK_RELEASE, NULL, p);
218 }
219
220 /*
221  * Lookup a filesystem type, and if found allocate and initialize
222  * a mount structure for it.
223  *
224  * Devname is usually updated by mount(8) after booting.
225  */
226 int
227 vfs_rootmountalloc(fstypename, devname, mpp)
228         char *fstypename;
229         char *devname;
230         struct mount **mpp;
231 {
232         struct proc *p = curproc;       /* XXX */
233         struct vfsconf *vfsp;
234         struct mount *mp;
235
236         if (fstypename == NULL)
237                 return (ENODEV);
238         for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
239                 if (!strcmp(vfsp->vfc_name, fstypename))
240                         break;
241         if (vfsp == NULL)
242                 return (ENODEV);
243         mp = malloc((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK);
244         bzero((char *)mp, (u_long)sizeof(struct mount));
245         lockinit(&mp->mnt_lock, PVFS, "vfslock", 0, LK_NOPAUSE);
246         (void)vfs_busy(mp, LK_NOWAIT, 0, p);
247         LIST_INIT(&mp->mnt_vnodelist);
248         mp->mnt_vfc = vfsp;
249         mp->mnt_op = vfsp->vfc_vfsops;
250         mp->mnt_flag = MNT_RDONLY;
251         mp->mnt_vnodecovered = NULLVP;
252         vfsp->vfc_refcount++;
253         mp->mnt_stat.f_type = vfsp->vfc_typenum;
254         mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
255         strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
256         mp->mnt_stat.f_mntonname[0] = '/';
257         mp->mnt_stat.f_mntonname[1] = 0;
258         (void) copystr(devname, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, 0);
259         *mpp = mp;
260         return (0);
261 }
262
263 /*
264  * Find an appropriate filesystem to use for the root. If a filesystem
265  * has not been preselected, walk through the list of known filesystems
266  * trying those that have mountroot routines, and try them until one
267  * works or we have tried them all.
268  */
269 #ifdef notdef   /* XXX JH */
270 int
271 lite2_vfs_mountroot()
272 {
273         struct vfsconf *vfsp;
274         extern int (*lite2_mountroot) __P((void));
275         int error;
276
277         if (lite2_mountroot != NULL)
278                 return ((*lite2_mountroot)());
279         for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
280                 if (vfsp->vfc_mountroot == NULL)
281                         continue;
282                 if ((error = (*vfsp->vfc_mountroot)()) == 0)
283                         return (0);
284                 printf("%s_mountroot failed: %d\n", vfsp->vfc_name, error);
285         }
286         return (ENODEV);
287 }
288 #endif
289
290 /*
291  * Lookup a mount point by filesystem identifier.
292  */
293 struct mount *
294 vfs_getvfs(fsid)
295         fsid_t *fsid;
296 {
297         register struct mount *mp;
298
299         simple_lock(&mountlist_slock);
300         for (mp = mountlist.cqh_first; mp != (void *)&mountlist;
301             mp = mp->mnt_list.cqe_next) {
302                 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
303                     mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
304                         simple_unlock(&mountlist_slock);
305                         return (mp);
306             }
307         }
308         simple_unlock(&mountlist_slock);
309         return ((struct mount *) 0);
310 }
311
312 /*
313  * Get a new unique fsid
314  */
315 void
316 vfs_getnewfsid(mp)
317         struct mount *mp;
318 {
319         static u_short xxxfs_mntid;
320
321         fsid_t tfsid;
322         int mtype;
323
324         simple_lock(&mntid_slock); 
325         mtype = mp->mnt_vfc->vfc_typenum;
326         mp->mnt_stat.f_fsid.val[0] = makedev(nblkdev + mtype, 0);
327         mp->mnt_stat.f_fsid.val[1] = mtype;
328         if (xxxfs_mntid == 0)
329                 ++xxxfs_mntid;
330         tfsid.val[0] = makedev(nblkdev + mtype, xxxfs_mntid);
331         tfsid.val[1] = mtype;
332         if (mountlist.cqh_first != (void *)&mountlist) {
333                 while (vfs_getvfs(&tfsid)) {
334                         tfsid.val[0]++;
335                         xxxfs_mntid++;
336                 }
337         }
338         mp->mnt_stat.f_fsid.val[0] = tfsid.val[0];
339         simple_unlock(&mntid_slock);
340 }
341
342 /*
343  * Set vnode attributes to VNOVAL
344  */
345 void
346 vattr_null(vap)
347         register struct vattr *vap;
348 {
349
350         vap->va_type = VNON;
351         vap->va_size = VNOVAL;
352         vap->va_bytes = VNOVAL;
353         vap->va_mode = VNOVAL;
354         vap->va_nlink = VNOVAL;
355         vap->va_uid = VNOVAL;
356         vap->va_gid = VNOVAL;
357         vap->va_fsid = VNOVAL;
358         vap->va_fileid = VNOVAL;
359         vap->va_blocksize = VNOVAL;
360         vap->va_rdev = VNOVAL;
361         vap->va_atime.tv_sec = VNOVAL;
362         vap->va_atime.tv_nsec = VNOVAL;
363         vap->va_mtime.tv_sec = VNOVAL;
364         vap->va_mtime.tv_nsec = VNOVAL;
365         vap->va_ctime.tv_sec = VNOVAL;
366         vap->va_ctime.tv_nsec = VNOVAL;
367         vap->va_flags = VNOVAL;
368         vap->va_gen = VNOVAL;
369         vap->va_vaflags = 0;
370 }
371
372 /*
373  * Routines having to do with the management of the vnode table.
374  */
375 extern vop_t **dead_vnodeop_p;
376
377 /*
378  * Return the next vnode from the free list.
379  */
380 int
381 getnewvnode(tag, mp, vops, vpp)
382         enum vtagtype tag;
383         struct mount *mp;
384         vop_t **vops;
385         struct vnode **vpp;
386 {
387         int s;
388         struct proc *p = curproc;       /* XXX */
389         struct vnode *vp, *tvp, *nvp;
390         vm_object_t object;
391         TAILQ_HEAD(freelst, vnode) vnode_tmp_list;
392
393         /*
394          * We take the least recently used vnode from the freelist
395          * if we can get it and it has no cached pages, and no
396          * namecache entries are relative to it.
397          * Otherwise we allocate a new vnode
398          */
399
400         s = splbio();
401         simple_lock(&vnode_free_list_slock);
402         TAILQ_INIT(&vnode_tmp_list);
403
404         for (vp = TAILQ_FIRST(&vnode_tobefree_list); vp; vp = nvp) {
405                 nvp = TAILQ_NEXT(vp, v_freelist);
406                 TAILQ_REMOVE(&vnode_tobefree_list, vp, v_freelist);
407                 if (vp->v_flag & VAGE) {
408                         TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
409                 } else {
410                         TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
411                 }
412                 vp->v_flag &= ~(VTBFREE|VAGE);
413                 vp->v_flag |= VFREE;
414                 if (vp->v_usecount)
415                         panic("tobe free vnode isn't");
416                 freevnodes++;
417         }
418
419         if (wantfreevnodes && freevnodes < wantfreevnodes) {
420                 vp = NULL;
421         } else if (!wantfreevnodes && freevnodes <= desiredvnodes) {
422                 /* 
423                  * XXX: this is only here to be backwards compatible
424                  */
425                 vp = NULL;
426         } else {
427                 for (vp = TAILQ_FIRST(&vnode_free_list); vp; vp = nvp) {
428                         nvp = TAILQ_NEXT(vp, v_freelist);
429                         if (!simple_lock_try(&vp->v_interlock)) 
430                                 continue;
431                         if (vp->v_usecount)
432                                 panic("free vnode isn't");
433
434                         object = vp->v_object;
435                         if (object && (object->resident_page_count || object->ref_count)) {
436                                 printf("object inconsistant state: RPC: %d, RC: %d\n",
437                                         object->resident_page_count, object->ref_count);
438                                 /* Don't recycle if it's caching some pages */
439                                 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
440                                 TAILQ_INSERT_TAIL(&vnode_tmp_list, vp, v_freelist);
441                                 continue;
442                         } else if (LIST_FIRST(&vp->v_cache_src)) {
443                                 /* Don't recycle if active in the namecache */
444                                 simple_unlock(&vp->v_interlock);
445                                 continue;
446                         } else {
447                                 break;
448                         }
449                 }
450         }
451
452         for (tvp = TAILQ_FIRST(&vnode_tmp_list); tvp; tvp = nvp) {
453                 nvp = TAILQ_NEXT(tvp, v_freelist);
454                 TAILQ_REMOVE(&vnode_tmp_list, tvp, v_freelist);
455                 TAILQ_INSERT_TAIL(&vnode_free_list, tvp, v_freelist);
456                 simple_unlock(&tvp->v_interlock);
457         }
458
459         if (vp) {
460                 vp->v_flag |= VDOOMED;
461                 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
462                 freevnodes--;
463                 simple_unlock(&vnode_free_list_slock);
464                 cache_purge(vp);
465                 vp->v_lease = NULL;
466                 if (vp->v_type != VBAD) {
467                         vgonel(vp, p);
468                 } else {
469                         simple_unlock(&vp->v_interlock);
470                 }
471
472 #ifdef INVARIANTS
473                 {
474                         int s;
475
476                         if (vp->v_data)
477                                 panic("cleaned vnode isn't");
478                         s = splbio();
479                         if (vp->v_numoutput)
480                                 panic("Clean vnode has pending I/O's");
481                         splx(s);
482                 }
483 #endif
484                 vp->v_flag = 0;
485                 vp->v_lastr = 0;
486                 vp->v_lastw = 0;
487                 vp->v_lasta = 0;
488                 vp->v_cstart = 0;
489                 vp->v_clen = 0;
490                 vp->v_socket = 0;
491                 vp->v_writecount = 0;   /* XXX */
492                 vp->v_maxio = 0;
493         } else {
494                 simple_unlock(&vnode_free_list_slock);
495                 vp = (struct vnode *) zalloc(vnode_zone);
496                 bzero((char *) vp, sizeof *vp);
497                 simple_lock_init(&vp->v_interlock);
498                 vp->v_dd = vp;
499                 cache_purge(vp);
500                 LIST_INIT(&vp->v_cache_src);
501                 TAILQ_INIT(&vp->v_cache_dst);
502                 numvnodes++;
503         }
504
505         TAILQ_INIT(&vp->v_cleanblkhd);
506         TAILQ_INIT(&vp->v_dirtyblkhd);
507         vp->v_type = VNON;
508         vp->v_tag = tag;
509         vp->v_op = vops;
510         insmntque(vp, mp);
511         *vpp = vp;
512         vp->v_usecount = 1;
513         vp->v_data = 0;
514         splx(s);
515
516         vfs_object_create(vp, p, p->p_ucred);
517         return (0);
518 }
519
520 /*
521  * Move a vnode from one mount queue to another.
522  */
523 static void
524 insmntque(vp, mp)
525         register struct vnode *vp;
526         register struct mount *mp;
527 {
528
529         simple_lock(&mntvnode_slock);
530         /*
531          * Delete from old mount point vnode list, if on one.
532          */
533         if (vp->v_mount != NULL)
534                 LIST_REMOVE(vp, v_mntvnodes);
535         /*
536          * Insert into list of vnodes for the new mount point, if available.
537          */
538         if ((vp->v_mount = mp) == NULL) {
539                 simple_unlock(&mntvnode_slock);
540                 return;
541         }
542         LIST_INSERT_HEAD(&mp->mnt_vnodelist, vp, v_mntvnodes);
543         simple_unlock(&mntvnode_slock);
544 }
545
546 /*
547  * Update outstanding I/O count and do wakeup if requested.
548  */
549 void
550 vwakeup(bp)
551         register struct buf *bp;
552 {
553         register struct vnode *vp;
554
555         bp->b_flags &= ~B_WRITEINPROG;
556         if ((vp = bp->b_vp)) {
557                 vp->v_numoutput--;
558                 if (vp->v_numoutput < 0)
559                         panic("vwakeup: neg numoutput");
560                 if ((vp->v_numoutput == 0) && (vp->v_flag & VBWAIT)) {
561                         vp->v_flag &= ~VBWAIT;
562                         wakeup((caddr_t) &vp->v_numoutput);
563                 }
564         }
565 }
566
567 /*
568  * Flush out and invalidate all buffers associated with a vnode.
569  * Called with the underlying object locked.
570  */
571 int
572 vinvalbuf(vp, flags, cred, p, slpflag, slptimeo)
573         register struct vnode *vp;
574         int flags;
575         struct ucred *cred;
576         struct proc *p;
577         int slpflag, slptimeo;
578 {
579         register struct buf *bp;
580         struct buf *nbp, *blist;
581         int s, error;
582         vm_object_t object;
583
584         if (flags & V_SAVE) {
585                 s = splbio();
586                 while (vp->v_numoutput) {
587                         vp->v_flag |= VBWAIT;
588                         error = tsleep((caddr_t)&vp->v_numoutput,
589                             slpflag | (PRIBIO + 1), "vinvlbuf", slptimeo);
590                         if (error) {
591                                 splx(s);
592                                 return (error);
593                         }
594                 }
595                 if (!TAILQ_EMPTY(&vp->v_dirtyblkhd)) {
596                         splx(s);
597                         if ((error = VOP_FSYNC(vp, cred, MNT_WAIT, p)) != 0)
598                                 return (error);
599                         s = splbio();
600                         if (vp->v_numoutput > 0 ||
601                             !TAILQ_EMPTY(&vp->v_dirtyblkhd))
602                                 panic("vinvalbuf: dirty bufs");
603                 }
604                 splx(s);
605         }
606         s = splbio();
607         for (;;) {
608                 blist = TAILQ_FIRST(&vp->v_cleanblkhd);
609                 if (!blist)
610                         blist = TAILQ_FIRST(&vp->v_dirtyblkhd);
611                 if (!blist)
612                         break;
613
614                 for (bp = blist; bp; bp = nbp) {
615                         nbp = TAILQ_NEXT(bp, b_vnbufs);
616                         if (bp->b_flags & B_BUSY) {
617                                 bp->b_flags |= B_WANTED;
618                                 error = tsleep((caddr_t) bp,
619                                     slpflag | (PRIBIO + 4), "vinvalbuf",
620                                     slptimeo);
621                                 if (error) {
622                                         splx(s);
623                                         return (error);
624                                 }
625                                 break;
626                         }
627                         /*
628                          * XXX Since there are no node locks for NFS, I
629                          * believe there is a slight chance that a delayed
630                          * write will occur while sleeping just above, so
631                          * check for it.  Note that vfs_bio_awrite expects
632                          * buffers to reside on a queue, while VOP_BWRITE and
633                          * brelse do not.
634                          */
635                         if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
636                                 (flags & V_SAVE)) {
637
638                                 if (bp->b_vp == vp) {
639                                         if (bp->b_flags & B_CLUSTEROK) {
640                                                 vfs_bio_awrite(bp);
641                                         } else {
642                                                 bremfree(bp);
643                                                 bp->b_flags |= (B_BUSY | B_ASYNC);
644                                                 VOP_BWRITE(bp);
645                                         }
646                                 } else {
647                                         bremfree(bp);
648                                         bp->b_flags |= B_BUSY;
649                                         (void) VOP_BWRITE(bp);
650                                 }
651                                 break;
652                         }
653                         bremfree(bp);
654                         bp->b_flags |= (B_INVAL | B_NOCACHE | B_RELBUF | B_BUSY);
655                         bp->b_flags &= ~B_ASYNC;
656                         brelse(bp);
657                 }
658         }
659
660         while (vp->v_numoutput > 0) {
661                 vp->v_flag |= VBWAIT;
662                 tsleep(&vp->v_numoutput, PVM, "vnvlbv", 0);
663         }
664
665         splx(s);
666
667         /*
668          * Destroy the copy in the VM cache, too.
669          */
670         simple_lock(&vp->v_interlock);
671         object = vp->v_object;
672         if (object != NULL) {
673                 vm_object_page_remove(object, 0, 0,
674                         (flags & V_SAVE) ? TRUE : FALSE);
675         }
676         simple_unlock(&vp->v_interlock);
677
678         if (!TAILQ_EMPTY(&vp->v_dirtyblkhd) || !TAILQ_EMPTY(&vp->v_cleanblkhd))
679                 panic("vinvalbuf: flush failed");
680         return (0);
681 }
682
683 /*
684  * Truncate a file's buffer and pages to a specified length.  This
685  * is in lieu of the old vinvalbuf mechanism, which performed unneeded
686  * sync activity.
687  */
688 int
689 vtruncbuf(vp, cred, p, length, blksize)
690         register struct vnode *vp;
691         struct ucred *cred;
692         struct proc *p;
693         off_t length;
694         int blksize;
695 {
696         register struct buf *bp;
697         struct buf *nbp;
698         int s, anyfreed;
699         int trunclbn;
700
701         /*
702          * Round up to the *next* lbn.
703          */
704         trunclbn = (length + blksize - 1) / blksize;
705
706         s = splbio();
707 restart:
708         anyfreed = 1;
709         for (;anyfreed;) {
710                 anyfreed = 0;
711                 for (bp = TAILQ_FIRST(&vp->v_cleanblkhd); bp; bp = nbp) {
712                         nbp = TAILQ_NEXT(bp, b_vnbufs);
713                         if (bp->b_lblkno >= trunclbn) {
714                                 if (bp->b_flags & B_BUSY) {
715                                         bp->b_flags |= B_WANTED;
716                                         tsleep(bp, PRIBIO + 4, "vtrb1", 0);
717                                         goto restart;
718                                 } else {
719                                         bremfree(bp);
720                                         bp->b_flags |= (B_BUSY | B_INVAL | B_RELBUF);
721                                         bp->b_flags &= ~B_ASYNC;
722                                         brelse(bp);
723                                         anyfreed = 1;
724                                 }
725                                 if (nbp && (((nbp->b_xflags & B_VNCLEAN) == 0)||
726                                          (nbp->b_vp != vp) ||
727                                          (nbp->b_flags & B_DELWRI))) {
728                                         goto restart;
729                                 }
730                         }
731                 }
732
733                 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
734                         nbp = TAILQ_NEXT(bp, b_vnbufs);
735                         if (bp->b_lblkno >= trunclbn) {
736                                 if (bp->b_flags & B_BUSY) {
737                                         bp->b_flags |= B_WANTED;
738                                         tsleep(bp, PRIBIO + 4, "vtrb2", 0);
739                                         goto restart;
740                                 } else {
741                                         bremfree(bp);
742                                         bp->b_flags |= (B_BUSY | B_INVAL | B_RELBUF);
743                                         bp->b_flags &= ~B_ASYNC;
744                                         brelse(bp);
745                                         anyfreed = 1;
746                                 }
747                                 if (nbp && (((nbp->b_xflags & B_VNDIRTY) == 0)||
748                                          (nbp->b_vp != vp) ||
749                                          (nbp->b_flags & B_DELWRI) == 0)) {
750                                         goto restart;
751                                 }
752                         }
753                 }
754         }
755
756         if (length > 0) {
757 restartsync:
758                 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
759                         nbp = TAILQ_NEXT(bp, b_vnbufs);
760                         if ((bp->b_flags & B_DELWRI) && (bp->b_lblkno < 0)) {
761                                 if (bp->b_flags & B_BUSY) {
762                                         bp->b_flags |= B_WANTED;
763                                         tsleep(bp, PRIBIO, "vtrb3", 0);
764                                 } else {
765                                         bremfree(bp);
766                                         bp->b_flags |= B_BUSY;
767                                         if (bp->b_vp == vp) {
768                                                 bp->b_flags |= B_ASYNC;
769                                         } else {
770                                                 bp->b_flags &= ~B_ASYNC;
771                                         }
772                                         VOP_BWRITE(bp);
773                                 }
774                                 goto restartsync;
775                         }
776
777                 }
778         }
779
780         while (vp->v_numoutput > 0) {
781                 vp->v_flag |= VBWAIT;
782                 tsleep(&vp->v_numoutput, PVM, "vbtrunc", 0);
783         }
784
785         splx(s);
786
787         vnode_pager_setsize(vp, length);
788
789         return (0);
790 }
791
792 /*
793  * Associate a buffer with a vnode.
794  */
795 void
796 bgetvp(vp, bp)
797         register struct vnode *vp;
798         register struct buf *bp;
799 {
800         int s;
801
802         KASSERT(bp->b_vp == NULL, ("bgetvp: not free"));
803
804         vhold(vp);
805         bp->b_vp = vp;
806         if (vp->v_type == VBLK || vp->v_type == VCHR)
807                 bp->b_dev = vp->v_rdev;
808         else
809                 bp->b_dev = NODEV;
810         /*
811          * Insert onto list for new vnode.
812          */
813         s = splbio();
814         bp->b_xflags |= B_VNCLEAN;
815         bp->b_xflags &= ~B_VNDIRTY;
816         TAILQ_INSERT_TAIL(&vp->v_cleanblkhd, bp, b_vnbufs);
817         splx(s);
818 }
819
820 /*
821  * Disassociate a buffer from a vnode.
822  */
823 void
824 brelvp(bp)
825         register struct buf *bp;
826 {
827         struct vnode *vp;
828         struct buflists *listheadp;
829         int s;
830
831         KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
832
833         /*
834          * Delete from old vnode list, if on one.
835          */
836         vp = bp->b_vp;
837         s = splbio();
838         if (bp->b_xflags & (B_VNDIRTY|B_VNCLEAN)) {
839                 if (bp->b_xflags & B_VNDIRTY)
840                         listheadp = &vp->v_dirtyblkhd;
841                 else 
842                         listheadp = &vp->v_cleanblkhd;
843                 TAILQ_REMOVE(listheadp, bp, b_vnbufs);
844                 bp->b_xflags &= ~(B_VNDIRTY|B_VNCLEAN);
845         }
846         if ((vp->v_flag & VONWORKLST) && TAILQ_EMPTY(&vp->v_dirtyblkhd)) {
847                 vp->v_flag &= ~VONWORKLST;
848                 LIST_REMOVE(vp, v_synclist);
849         }
850         splx(s);
851         bp->b_vp = (struct vnode *) 0;
852         vdrop(vp);
853 }
854
855 /*
856  * The workitem queue.
857  * 
858  * It is useful to delay writes of file data and filesystem metadata
859  * for tens of seconds so that quickly created and deleted files need
860  * not waste disk bandwidth being created and removed. To realize this,
861  * we append vnodes to a "workitem" queue. When running with a soft
862  * updates implementation, most pending metadata dependencies should
863  * not wait for more than a few seconds. Thus, mounted on block devices
864  * are delayed only about a half the time that file data is delayed.
865  * Similarly, directory updates are more critical, so are only delayed
866  * about a third the time that file data is delayed. Thus, there are
867  * SYNCER_MAXDELAY queues that are processed round-robin at a rate of
868  * one each second (driven off the filesystem syner process). The
869  * syncer_delayno variable indicates the next queue that is to be processed.
870  * Items that need to be processed soon are placed in this queue:
871  *
872  *      syncer_workitem_pending[syncer_delayno]
873  *
874  * A delay of fifteen seconds is done by placing the request fifteen
875  * entries later in the queue:
876  *
877  *      syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask]
878  *
879  */
880
881 /*
882  * Add an item to the syncer work queue.
883  */
884 static void
885 vn_syncer_add_to_worklist(struct vnode *vp, int delay)
886 {
887         int s, slot;
888
889         s = splbio();
890
891         if (vp->v_flag & VONWORKLST) {
892                 LIST_REMOVE(vp, v_synclist);
893         }
894
895         if (delay > syncer_maxdelay - 2)
896                 delay = syncer_maxdelay - 2;
897         slot = (syncer_delayno + delay) & syncer_mask;
898
899         LIST_INSERT_HEAD(&syncer_workitem_pending[slot], vp, v_synclist);
900         vp->v_flag |= VONWORKLST;
901         splx(s);
902 }
903
904 static struct  proc *updateproc;
905 static void sched_sync __P((void));
906 static const struct kproc_desc up_kp = {
907         "syncer",
908         sched_sync,
909         &updateproc
910 };
911 SYSINIT_KT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp)
912
913 /*
914  * System filesystem synchronizer daemon.
915  */
916 void 
917 sched_sync(void)
918 {
919         struct synclist *slp;
920         struct vnode *vp;
921         long starttime;
922         int s;
923         struct proc *p = updateproc;
924
925         for (;;) {
926                 starttime = time_second;
927
928                 /*
929                  * Push files whose dirty time has expired.  Be careful
930                  * of interrupt race on slp queue.
931                  */
932                 s = splbio();
933                 slp = &syncer_workitem_pending[syncer_delayno];
934                 syncer_delayno += 1;
935                 if (syncer_delayno == syncer_maxdelay)
936                         syncer_delayno = 0;
937                 splx(s);
938
939                 while ((vp = LIST_FIRST(slp)) != NULL) {
940                         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
941                         (void) VOP_FSYNC(vp, p->p_ucred, MNT_LAZY, p);
942                         VOP_UNLOCK(vp, 0, p);
943                         s = splbio();
944                         if (LIST_FIRST(slp) == vp) {
945                                 if (TAILQ_EMPTY(&vp->v_dirtyblkhd) &&
946                                     vp->v_type != VBLK)
947                                         panic("sched_sync: fsync failed vp %p tag %d", vp, vp->v_tag);
948                                 /*
949                                  * Put us back on the worklist.  The worklist
950                                  * routine will remove us from our current
951                                  * position and then add us back in at a later
952                                  * position.
953                                  */
954                                 vn_syncer_add_to_worklist(vp, syncdelay);
955                         }
956                         splx(s);
957                 }
958
959                 /*
960                  * Do soft update processing.
961                  */
962                 if (bioops.io_sync)
963                         (*bioops.io_sync)(NULL);
964
965                 /*
966                  * The variable rushjob allows the kernel to speed up the
967                  * processing of the filesystem syncer process. A rushjob
968                  * value of N tells the filesystem syncer to process the next
969                  * N seconds worth of work on its queue ASAP. Currently rushjob
970                  * is used by the soft update code to speed up the filesystem
971                  * syncer process when the incore state is getting so far
972                  * ahead of the disk that the kernel memory pool is being
973                  * threatened with exhaustion.
974                  */
975                 if (rushjob > 0) {
976                         rushjob -= 1;
977                         continue;
978                 }
979                 /*
980                  * If it has taken us less than a second to process the
981                  * current work, then wait. Otherwise start right over
982                  * again. We can still lose time if any single round
983                  * takes more than two seconds, but it does not really
984                  * matter as we are just trying to generally pace the
985                  * filesystem activity.
986                  */
987                 if (time_second == starttime)
988                         tsleep(&lbolt, PPAUSE, "syncer", 0);
989         }
990 }
991
992 /*
993  * Associate a p-buffer with a vnode.
994  *
995  * Also sets B_PAGING flag to indicate that vnode is not fully associated
996  * with the buffer.  i.e. the bp has not been linked into the vnode or
997  * ref-counted.
998  */
999 void
1000 pbgetvp(vp, bp)
1001         register struct vnode *vp;
1002         register struct buf *bp;
1003 {
1004
1005         KASSERT(bp->b_vp == NULL, ("pbgetvp: not free"));
1006
1007         bp->b_vp = vp;
1008         bp->b_flags |= B_PAGING;
1009         if (vp->v_type == VBLK || vp->v_type == VCHR)
1010                 bp->b_dev = vp->v_rdev;
1011         else
1012                 bp->b_dev = NODEV;
1013 }
1014
1015 /*
1016  * Disassociate a p-buffer from a vnode.
1017  */
1018 void
1019 pbrelvp(bp)
1020         register struct buf *bp;
1021 {
1022
1023         KASSERT(bp->b_vp != NULL, ("pbrelvp: NULL"));
1024
1025 #if !defined(MAX_PERF)
1026         /* XXX REMOVE ME */
1027         if (bp->b_vnbufs.tqe_next != NULL) {
1028                 panic(
1029                     "relpbuf(): b_vp was probably reassignbuf()d %p %x", 
1030                     bp,
1031                     (int)bp->b_flags
1032                 );
1033         }
1034 #endif
1035         bp->b_vp = (struct vnode *) 0;
1036         bp->b_flags &= ~B_PAGING;
1037 }
1038
1039 void
1040 pbreassignbuf(bp, newvp)
1041         struct buf *bp;
1042         struct vnode *newvp;
1043 {
1044 #if !defined(MAX_PERF)
1045         if ((bp->b_flags & B_PAGING) == 0) {
1046                 panic(
1047                     "pbreassignbuf() on non phys bp %p", 
1048                     bp
1049                 );
1050         }
1051 #endif
1052         bp->b_vp = newvp;
1053 }
1054
1055 /*
1056  * Reassign a buffer from one vnode to another.
1057  * Used to assign file specific control information
1058  * (indirect blocks) to the vnode to which they belong.
1059  */
1060 void
1061 reassignbuf(bp, newvp)
1062         register struct buf *bp;
1063         register struct vnode *newvp;
1064 {
1065         struct buflists *listheadp;
1066         struct vnode *oldvp;
1067         int delay;
1068         int s;
1069
1070         if (newvp == NULL) {
1071                 printf("reassignbuf: NULL");
1072                 return;
1073         }
1074
1075 #if !defined(MAX_PERF)
1076         /*
1077          * B_PAGING flagged buffers cannot be reassigned because their vp
1078          * is not fully linked in.
1079          */
1080         if (bp->b_flags & B_PAGING)
1081                 panic("cannot reassign paging buffer");
1082 #endif
1083
1084         s = splbio();
1085         /*
1086          * Delete from old vnode list, if on one.
1087          */
1088         if (bp->b_xflags & (B_VNDIRTY|B_VNCLEAN)) {
1089                 oldvp = bp->b_vp;
1090                 if (bp->b_xflags & B_VNDIRTY)
1091                         listheadp = &oldvp->v_dirtyblkhd;
1092                 else 
1093                         listheadp = &oldvp->v_cleanblkhd;
1094                 TAILQ_REMOVE(listheadp, bp, b_vnbufs);
1095                 bp->b_xflags &= ~(B_VNDIRTY|B_VNCLEAN);
1096                 vdrop(oldvp);
1097         }
1098         /*
1099          * If dirty, put on list of dirty buffers; otherwise insert onto list
1100          * of clean buffers.
1101          */
1102         if (bp->b_flags & B_DELWRI) {
1103                 struct buf *tbp;
1104
1105                 listheadp = &newvp->v_dirtyblkhd;
1106                 if ((newvp->v_flag & VONWORKLST) == 0) {
1107                         switch (newvp->v_type) {
1108                         case VDIR:
1109                                 delay = syncdelay / 3;
1110                                 break;
1111                         case VBLK:
1112                                 if (newvp->v_specmountpoint != NULL) {
1113                                         delay = syncdelay / 2;
1114                                         break;
1115                                 }
1116                                 /* fall through */
1117                         default:
1118                                 delay = syncdelay;
1119                         }
1120                         vn_syncer_add_to_worklist(newvp, delay);
1121                 }
1122                 bp->b_xflags |= B_VNDIRTY;
1123                 tbp = TAILQ_FIRST(listheadp);
1124                 if (tbp == NULL ||
1125                     (bp->b_lblkno >= 0 && tbp->b_lblkno > bp->b_lblkno)) {
1126                         TAILQ_INSERT_HEAD(listheadp, bp, b_vnbufs);
1127                 } else {
1128                         if (bp->b_lblkno >= 0) {
1129                                 struct buf *ttbp;
1130                                 while ((ttbp = TAILQ_NEXT(tbp, b_vnbufs)) &&
1131                                     (ttbp->b_lblkno < bp->b_lblkno)) {
1132                                         tbp = ttbp;
1133                                 }
1134                                 TAILQ_INSERT_AFTER(listheadp, tbp, bp, b_vnbufs);
1135                         } else {
1136                                 TAILQ_INSERT_TAIL(listheadp, bp, b_vnbufs);
1137                         }
1138                 }
1139         } else {
1140                 bp->b_xflags |= B_VNCLEAN;
1141                 TAILQ_INSERT_TAIL(&newvp->v_cleanblkhd, bp, b_vnbufs);
1142                 if ((newvp->v_flag & VONWORKLST) &&
1143                     TAILQ_EMPTY(&newvp->v_dirtyblkhd)) {
1144                         newvp->v_flag &= ~VONWORKLST;
1145                         LIST_REMOVE(newvp, v_synclist);
1146                 }
1147         }
1148         bp->b_vp = newvp;
1149         vhold(bp->b_vp);
1150         splx(s);
1151 }
1152
1153 /*
1154  * Create a vnode for a block device.
1155  * Used for mounting the root file system.
1156  */
1157 int
1158 bdevvp(dev, vpp)
1159         dev_t dev;
1160         struct vnode **vpp;
1161 {
1162         register struct vnode *vp;
1163         struct vnode *nvp;
1164         int error;
1165
1166         /* XXX 255 is for mfs. */
1167         if (dev == NODEV || (major(dev) != 255 && (major(dev) >= nblkdev ||
1168             bdevsw[major(dev)] == NULL))) {
1169                 *vpp = NULLVP;
1170                 return (ENXIO);
1171         }
1172         error = getnewvnode(VT_NON, (struct mount *)0, spec_vnodeop_p, &nvp);
1173         if (error) {
1174                 *vpp = NULLVP;
1175                 return (error);
1176         }
1177         vp = nvp;
1178         vp->v_type = VBLK;
1179         if ((nvp = checkalias(vp, dev, (struct mount *)0)) != NULL) {
1180                 vput(vp);
1181                 vp = nvp;
1182         }
1183         *vpp = vp;
1184         return (0);
1185 }
1186
1187 /*
1188  * Check to see if the new vnode represents a special device
1189  * for which we already have a vnode (either because of
1190  * bdevvp() or because of a different vnode representing
1191  * the same block device). If such an alias exists, deallocate
1192  * the existing contents and return the aliased vnode. The
1193  * caller is responsible for filling it with its new contents.
1194  */
1195 struct vnode *
1196 checkalias(nvp, nvp_rdev, mp)
1197         register struct vnode *nvp;
1198         dev_t nvp_rdev;
1199         struct mount *mp;
1200 {
1201         struct proc *p = curproc;       /* XXX */
1202         struct vnode *vp;
1203         struct vnode **vpp;
1204         int rmaj = major(nvp_rdev);
1205
1206         if (nvp->v_type != VBLK && nvp->v_type != VCHR)
1207                 return (NULLVP);
1208
1209         vpp = &speclisth[SPECHASH(nvp_rdev)];
1210 loop:
1211         simple_lock(&spechash_slock);
1212         for (vp = *vpp; vp; vp = vp->v_specnext) {
1213                 if (nvp_rdev != vp->v_rdev || nvp->v_type != vp->v_type)
1214                         continue;
1215                 /*
1216                  * Alias, but not in use, so flush it out.
1217                  * Only alias active device nodes.
1218                  * Not sure why we don't re-use this like we do below.
1219                  */
1220                 simple_lock(&vp->v_interlock);
1221                 if (vp->v_usecount == 0) {
1222                         simple_unlock(&spechash_slock);
1223                         vgonel(vp, p);
1224                         goto loop;
1225                 }
1226                 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, p)) {
1227                         /*
1228                          * It dissappeared, and we may have slept.
1229                          * Restart from the beginning
1230                          */
1231                         simple_unlock(&spechash_slock);
1232                         goto loop;
1233                 }
1234                 break;
1235         }
1236         /*
1237          * It would be a lot clearer what is going on here if
1238          * this had been expressed as:
1239          * if ( vp && (vp->v_tag == VT_NULL))
1240          * and the clauses had been swapped.
1241          */
1242         if (vp == NULL || vp->v_tag != VT_NON) {
1243                 struct specinfo *sinfo;
1244
1245                 /*
1246                  * Put the new vnode into the hash chain.
1247                  * and if there was an alias, connect them.
1248                  */
1249                 MALLOC(sinfo, struct specinfo *,
1250                     sizeof(struct specinfo), M_VNODE, M_WAITOK);
1251                 bzero(sinfo, sizeof(struct specinfo));
1252                 nvp->v_specinfo = sinfo;
1253                 sinfo->si_rdev = nvp_rdev;
1254                 sinfo->si_hashchain = vpp;
1255                 sinfo->si_specnext = *vpp;
1256                 sinfo->si_bsize_phys = DEV_BSIZE;
1257                 sinfo->si_bsize_best = BLKDEV_IOSIZE;
1258                 sinfo->si_bsize_max = MAXBSIZE;
1259
1260                 /*
1261                  * Ask the device to fix up specinfo.  Typically the 
1262                  * si_bsize_* parameters may need fixing up.
1263                  */
1264
1265                 if (nvp->v_type == VBLK && rmaj < nblkdev) {
1266                         if (bdevsw[rmaj] && bdevsw[rmaj]->d_parms)
1267                                 
1268                                 (*bdevsw[rmaj]->d_parms)(nvp_rdev, sinfo, DPARM_GET);
1269                 } else if (nvp->v_type == VCHR && rmaj < nchrdev) {
1270                         if (cdevsw[rmaj] && cdevsw[rmaj]->d_parms)
1271                                 (*cdevsw[rmaj]->d_parms)(nvp_rdev, sinfo, DPARM_GET);
1272                 }
1273
1274                 simple_unlock(&spechash_slock);
1275                 *vpp = nvp;
1276                 if (vp != NULLVP) {
1277                         nvp->v_flag |= VALIASED;
1278                         vp->v_flag |= VALIASED;
1279                         vput(vp);
1280                 }
1281                 return (NULLVP);
1282         }
1283         /*
1284          * if ( vp && (vp->v_tag == VT_NULL))
1285          * We have a vnode alias, but it is a trashed.
1286          * Make it look like it's newley allocated. (by getnewvnode())
1287          * The caller should use this instead.
1288          */
1289         simple_unlock(&spechash_slock);
1290         VOP_UNLOCK(vp, 0, p);
1291         simple_lock(&vp->v_interlock);
1292         vclean(vp, 0, p);
1293         vp->v_op = nvp->v_op;
1294         vp->v_tag = nvp->v_tag;
1295         nvp->v_type = VNON;
1296         insmntque(vp, mp);
1297         return (vp);
1298 }
1299
1300 /*
1301  * Grab a particular vnode from the free list, increment its
1302  * reference count and lock it. The vnode lock bit is set the
1303  * vnode is being eliminated in vgone. The process is awakened
1304  * when the transition is completed, and an error returned to
1305  * indicate that the vnode is no longer usable (possibly having
1306  * been changed to a new file system type).
1307  */
1308 int
1309 vget(vp, flags, p)
1310         register struct vnode *vp;
1311         int flags;
1312         struct proc *p;
1313 {
1314         int error;
1315
1316         /*
1317          * If the vnode is in the process of being cleaned out for
1318          * another use, we wait for the cleaning to finish and then
1319          * return failure. Cleaning is determined by checking that
1320          * the VXLOCK flag is set.
1321          */
1322         if ((flags & LK_INTERLOCK) == 0) {
1323                 simple_lock(&vp->v_interlock);
1324         }
1325         if (vp->v_flag & VXLOCK) {
1326                 vp->v_flag |= VXWANT;
1327                 simple_unlock(&vp->v_interlock);
1328                 tsleep((caddr_t)vp, PINOD, "vget", 0);
1329                 return (ENOENT);
1330         }
1331
1332         vp->v_usecount++;
1333
1334         if (VSHOULDBUSY(vp))
1335                 vbusy(vp);
1336         if (flags & LK_TYPE_MASK) {
1337                 if ((error = vn_lock(vp, flags | LK_INTERLOCK, p)) != 0) {
1338                         /*
1339                          * must expand vrele here because we do not want
1340                          * to call VOP_INACTIVE if the reference count
1341                          * drops back to zero since it was never really
1342                          * active. We must remove it from the free list
1343                          * before sleeping so that multiple processes do
1344                          * not try to recycle it.
1345                          */
1346                         simple_lock(&vp->v_interlock);
1347                         vp->v_usecount--;
1348                         if (VSHOULDFREE(vp))
1349                                 vfree(vp);
1350                         simple_unlock(&vp->v_interlock);
1351                 }
1352                 return (error);
1353         }
1354         simple_unlock(&vp->v_interlock);
1355         return (0);
1356 }
1357
1358 void
1359 vref(struct vnode *vp)
1360 {
1361         simple_lock(&vp->v_interlock);
1362         vp->v_usecount++;
1363         simple_unlock(&vp->v_interlock);
1364 }
1365
1366 /*
1367  * Vnode put/release.
1368  * If count drops to zero, call inactive routine and return to freelist.
1369  */
1370 void
1371 vrele(vp)
1372         struct vnode *vp;
1373 {
1374         struct proc *p = curproc;       /* XXX */
1375
1376         KASSERT(vp != NULL, ("vrele: null vp"));
1377
1378         simple_lock(&vp->v_interlock);
1379
1380         if (vp->v_usecount > 1) {
1381
1382                 vp->v_usecount--;
1383                 simple_unlock(&vp->v_interlock);
1384
1385                 return;
1386         }
1387
1388         if (vp->v_usecount == 1) {
1389
1390                 vp->v_usecount--;
1391                 if (VSHOULDFREE(vp))
1392                         vfree(vp);
1393         /*
1394          * If we are doing a vput, the node is already locked, and we must
1395          * call VOP_INACTIVE with the node locked.  So, in the case of
1396          * vrele, we explicitly lock the vnode before calling VOP_INACTIVE.
1397          */
1398                 if (vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK, p) == 0) {
1399                         VOP_INACTIVE(vp, p);
1400                 }
1401
1402         } else {
1403 #ifdef DIAGNOSTIC
1404                 vprint("vrele: negative ref count", vp);
1405                 simple_unlock(&vp->v_interlock);
1406 #endif
1407                 panic("vrele: negative ref cnt");
1408         }
1409 }
1410
1411 void
1412 vput(vp)
1413         struct vnode *vp;
1414 {
1415         struct proc *p = curproc;       /* XXX */
1416
1417         KASSERT(vp != NULL, ("vput: null vp"));
1418
1419         simple_lock(&vp->v_interlock);
1420
1421         if (vp->v_usecount > 1) {
1422
1423                 vp->v_usecount--;
1424                 VOP_UNLOCK(vp, LK_INTERLOCK, p);
1425                 return;
1426
1427         }
1428
1429         if (vp->v_usecount == 1) {
1430
1431                 vp->v_usecount--;
1432                 if (VSHOULDFREE(vp))
1433                         vfree(vp);
1434         /*
1435          * If we are doing a vput, the node is already locked, and we must
1436          * call VOP_INACTIVE with the node locked.  So, in the case of
1437          * vrele, we explicitly lock the vnode before calling VOP_INACTIVE.
1438          */
1439                 simple_unlock(&vp->v_interlock);
1440                 VOP_INACTIVE(vp, p);
1441
1442         } else {
1443 #ifdef DIAGNOSTIC
1444                 vprint("vput: negative ref count", vp);
1445 #endif
1446                 panic("vput: negative ref cnt");
1447         }
1448 }
1449
1450 /*
1451  * Somebody doesn't want the vnode recycled.
1452  */
1453 void
1454 vhold(vp)
1455         register struct vnode *vp;
1456 {
1457         int s;
1458
1459         s = splbio();
1460         vp->v_holdcnt++;
1461         if (VSHOULDBUSY(vp))
1462                 vbusy(vp);
1463         splx(s);
1464 }
1465
1466 /*
1467  * One less who cares about this vnode.
1468  */
1469 void
1470 vdrop(vp)
1471         register struct vnode *vp;
1472 {
1473         int s;
1474
1475         s = splbio();
1476         if (vp->v_holdcnt <= 0)
1477                 panic("vdrop: holdcnt");
1478         vp->v_holdcnt--;
1479         if (VSHOULDFREE(vp))
1480                 vfree(vp);
1481         splx(s);
1482 }
1483
1484 /*
1485  * Remove any vnodes in the vnode table belonging to mount point mp.
1486  *
1487  * If MNT_NOFORCE is specified, there should not be any active ones,
1488  * return error if any are found (nb: this is a user error, not a
1489  * system error). If MNT_FORCE is specified, detach any active vnodes
1490  * that are found.
1491  */
1492 #ifdef DIAGNOSTIC
1493 static int busyprt = 0;         /* print out busy vnodes */
1494 SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, "");
1495 #endif
1496
1497 int
1498 vflush(mp, skipvp, flags)
1499         struct mount *mp;
1500         struct vnode *skipvp;
1501         int flags;
1502 {
1503         struct proc *p = curproc;       /* XXX */
1504         struct vnode *vp, *nvp;
1505         int busy = 0;
1506
1507         simple_lock(&mntvnode_slock);
1508 loop:
1509         for (vp = mp->mnt_vnodelist.lh_first; vp; vp = nvp) {
1510                 /*
1511                  * Make sure this vnode wasn't reclaimed in getnewvnode().
1512                  * Start over if it has (it won't be on the list anymore).
1513                  */
1514                 if (vp->v_mount != mp)
1515                         goto loop;
1516                 nvp = vp->v_mntvnodes.le_next;
1517                 /*
1518                  * Skip over a selected vnode.
1519                  */
1520                 if (vp == skipvp)
1521                         continue;
1522
1523                 simple_lock(&vp->v_interlock);
1524                 /*
1525                  * Skip over a vnodes marked VSYSTEM.
1526                  */
1527                 if ((flags & SKIPSYSTEM) && (vp->v_flag & VSYSTEM)) {
1528                         simple_unlock(&vp->v_interlock);
1529                         continue;
1530                 }
1531                 /*
1532                  * If WRITECLOSE is set, only flush out regular file vnodes
1533                  * open for writing.
1534                  */
1535                 if ((flags & WRITECLOSE) &&
1536                     (vp->v_writecount == 0 || vp->v_type != VREG)) {
1537                         simple_unlock(&vp->v_interlock);
1538                         continue;
1539                 }
1540
1541                 /*
1542                  * With v_usecount == 0, all we need to do is clear out the
1543                  * vnode data structures and we are done.
1544                  */
1545                 if (vp->v_usecount == 0) {
1546                         simple_unlock(&mntvnode_slock);
1547                         vgonel(vp, p);
1548                         simple_lock(&mntvnode_slock);
1549                         continue;
1550                 }
1551
1552                 /*
1553                  * If FORCECLOSE is set, forcibly close the vnode. For block
1554                  * or character devices, revert to an anonymous device. For
1555                  * all other files, just kill them.
1556                  */
1557                 if (flags & FORCECLOSE) {
1558                         simple_unlock(&mntvnode_slock);
1559                         if (vp->v_type != VBLK && vp->v_type != VCHR) {
1560                                 vgonel(vp, p);
1561                         } else {
1562                                 vclean(vp, 0, p);
1563                                 vp->v_op = spec_vnodeop_p;
1564                                 insmntque(vp, (struct mount *) 0);
1565                         }
1566                         simple_lock(&mntvnode_slock);
1567                         continue;
1568                 }
1569 #ifdef DIAGNOSTIC
1570                 if (busyprt)
1571                         vprint("vflush: busy vnode", vp);
1572 #endif
1573                 simple_unlock(&vp->v_interlock);
1574                 busy++;
1575         }
1576         simple_unlock(&mntvnode_slock);
1577         if (busy)
1578                 return (EBUSY);
1579         return (0);
1580 }
1581
1582 /*
1583  * Disassociate the underlying file system from a vnode.
1584  */
1585 static void
1586 vclean(vp, flags, p)
1587         struct vnode *vp;
1588         int flags;
1589         struct proc *p;
1590 {
1591         int active;
1592         vm_object_t obj;
1593
1594         /*
1595          * Check to see if the vnode is in use. If so we have to reference it
1596          * before we clean it out so that its count cannot fall to zero and
1597          * generate a race against ourselves to recycle it.
1598          */
1599         if ((active = vp->v_usecount))
1600                 vp->v_usecount++;
1601
1602         /*
1603          * Prevent the vnode from being recycled or brought into use while we
1604          * clean it out.
1605          */
1606         if (vp->v_flag & VXLOCK)
1607                 panic("vclean: deadlock");
1608         vp->v_flag |= VXLOCK;
1609         /*
1610          * Even if the count is zero, the VOP_INACTIVE routine may still
1611          * have the object locked while it cleans it out. The VOP_LOCK
1612          * ensures that the VOP_INACTIVE routine is done with its work.
1613          * For active vnodes, it ensures that no other activity can
1614          * occur while the underlying object is being cleaned out.
1615          */
1616         VOP_LOCK(vp, LK_DRAIN | LK_INTERLOCK, p);
1617
1618         /*
1619          * Clean out any buffers associated with the vnode.
1620          */
1621         vinvalbuf(vp, V_SAVE, NOCRED, p, 0, 0);
1622         if ((obj = vp->v_object) != NULL) {
1623                 if (obj->ref_count == 0) {
1624                         /*
1625                          * This is a normal way of shutting down the object/vnode
1626                          * association.
1627                          */
1628                         vm_object_terminate(obj);
1629                 } else {
1630                         /*
1631                          * Woe to the process that tries to page now :-).
1632                          */
1633                         vm_pager_deallocate(obj);
1634                 }
1635         }
1636
1637         /*
1638          * If purging an active vnode, it must be closed and
1639          * deactivated before being reclaimed. Note that the
1640          * VOP_INACTIVE will unlock the vnode.
1641          */
1642         if (active) {
1643                 if (flags & DOCLOSE)
1644                         VOP_CLOSE(vp, FNONBLOCK, NOCRED, p);
1645                 VOP_INACTIVE(vp, p);
1646         } else {
1647                 /*
1648                  * Any other processes trying to obtain this lock must first
1649                  * wait for VXLOCK to clear, then call the new lock operation.
1650                  */
1651                 VOP_UNLOCK(vp, 0, p);
1652         }
1653         /*
1654          * Reclaim the vnode.
1655          */
1656         if (VOP_RECLAIM(vp, p))
1657                 panic("vclean: cannot reclaim");
1658
1659         if (active)
1660                 vrele(vp);
1661
1662         cache_purge(vp);
1663         if (vp->v_vnlock) {
1664 #if 0 /* This is the only place we have LK_DRAINED in the entire kernel ??? */
1665 #ifdef DIAGNOSTIC
1666                 if ((vp->v_vnlock->lk_flags & LK_DRAINED) == 0)
1667                         vprint("vclean: lock not drained", vp);
1668 #endif
1669 #endif
1670                 FREE(vp->v_vnlock, M_VNODE);
1671                 vp->v_vnlock = NULL;
1672         }
1673
1674         if (VSHOULDFREE(vp))
1675                 vfree(vp);
1676
1677         /*
1678          * Done with purge, notify sleepers of the grim news.
1679          */
1680         vp->v_op = dead_vnodeop_p;
1681         vn_pollgone(vp);
1682         vp->v_tag = VT_NON;
1683         vp->v_flag &= ~VXLOCK;
1684         if (vp->v_flag & VXWANT) {
1685                 vp->v_flag &= ~VXWANT;
1686                 wakeup((caddr_t) vp);
1687         }
1688 }
1689
1690 /*
1691  * Eliminate all activity associated with the requested vnode
1692  * and with all vnodes aliased to the requested vnode.
1693  */
1694 int
1695 vop_revoke(ap)
1696         struct vop_revoke_args /* {
1697                 struct vnode *a_vp;
1698                 int a_flags;
1699         } */ *ap;
1700 {
1701         struct vnode *vp, *vq;
1702         struct proc *p = curproc;       /* XXX */
1703
1704         KASSERT((ap->a_flags & REVOKEALL) != 0, ("vop_revoke"));
1705
1706         vp = ap->a_vp;
1707         simple_lock(&vp->v_interlock);
1708
1709         if (vp->v_flag & VALIASED) {
1710                 /*
1711                  * If a vgone (or vclean) is already in progress,
1712                  * wait until it is done and return.
1713                  */
1714                 if (vp->v_flag & VXLOCK) {
1715                         vp->v_flag |= VXWANT;
1716                         simple_unlock(&vp->v_interlock);
1717                         tsleep((caddr_t)vp, PINOD, "vop_revokeall", 0);
1718                         return (0);
1719                 }
1720                 /*
1721                  * Ensure that vp will not be vgone'd while we
1722                  * are eliminating its aliases.
1723                  */
1724                 vp->v_flag |= VXLOCK;
1725                 simple_unlock(&vp->v_interlock);
1726                 while (vp->v_flag & VALIASED) {
1727                         simple_lock(&spechash_slock);
1728                         for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
1729                                 if (vq->v_rdev != vp->v_rdev ||
1730                                     vq->v_type != vp->v_type || vp == vq)
1731                                         continue;
1732                                 simple_unlock(&spechash_slock);
1733                                 vgone(vq);
1734                                 break;
1735                         }
1736                         if (vq == NULLVP) {
1737                                 simple_unlock(&spechash_slock);
1738                         }
1739                 }
1740                 /*
1741                  * Remove the lock so that vgone below will
1742                  * really eliminate the vnode after which time
1743                  * vgone will awaken any sleepers.
1744                  */
1745                 simple_lock(&vp->v_interlock);
1746                 vp->v_flag &= ~VXLOCK;
1747                 if (vp->v_flag & VXWANT) {
1748                         vp->v_flag &= ~VXWANT;
1749                         wakeup(vp);
1750                 }
1751         }
1752         vgonel(vp, p);
1753         return (0);
1754 }
1755
1756 /*
1757  * Recycle an unused vnode to the front of the free list.
1758  * Release the passed interlock if the vnode will be recycled.
1759  */
1760 int
1761 vrecycle(vp, inter_lkp, p)
1762         struct vnode *vp;
1763         struct simplelock *inter_lkp;
1764         struct proc *p;
1765 {
1766
1767         simple_lock(&vp->v_interlock);
1768         if (vp->v_usecount == 0) {
1769                 if (inter_lkp) {
1770                         simple_unlock(inter_lkp);
1771                 }
1772                 vgonel(vp, p);
1773                 return (1);
1774         }
1775         simple_unlock(&vp->v_interlock);
1776         return (0);
1777 }
1778
1779 /*
1780  * Eliminate all activity associated with a vnode
1781  * in preparation for reuse.
1782  */
1783 void
1784 vgone(vp)
1785         register struct vnode *vp;
1786 {
1787         struct proc *p = curproc;       /* XXX */
1788
1789         simple_lock(&vp->v_interlock);
1790         vgonel(vp, p);
1791 }
1792
1793 /*
1794  * vgone, with the vp interlock held.
1795  */
1796 static void
1797 vgonel(vp, p)
1798         struct vnode *vp;
1799         struct proc *p;
1800 {
1801         int s;
1802         struct vnode *vq;
1803         struct vnode *vx;
1804
1805         /*
1806          * If a vgone (or vclean) is already in progress,
1807          * wait until it is done and return.
1808          */
1809         if (vp->v_flag & VXLOCK) {
1810                 vp->v_flag |= VXWANT;
1811                 simple_unlock(&vp->v_interlock);
1812                 tsleep((caddr_t)vp, PINOD, "vgone", 0);
1813                 return;
1814         }
1815
1816         /*
1817          * Clean out the filesystem specific data.
1818          */
1819         vclean(vp, DOCLOSE, p);
1820         simple_lock(&vp->v_interlock);
1821
1822         /*
1823          * Delete from old mount point vnode list, if on one.
1824          */
1825         if (vp->v_mount != NULL)
1826                 insmntque(vp, (struct mount *)0);
1827         /*
1828          * If special device, remove it from special device alias list
1829          * if it is on one.
1830          */
1831         if ((vp->v_type == VBLK || vp->v_type == VCHR) && vp->v_specinfo != 0) {
1832                 simple_lock(&spechash_slock);
1833                 if (*vp->v_hashchain == vp) {
1834                         *vp->v_hashchain = vp->v_specnext;
1835                 } else {
1836                         for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
1837                                 if (vq->v_specnext != vp)
1838                                         continue;
1839                                 vq->v_specnext = vp->v_specnext;
1840                                 break;
1841                         }
1842                         if (vq == NULL)
1843                                 panic("missing bdev");
1844                 }
1845                 if (vp->v_flag & VALIASED) {
1846                         vx = NULL;
1847                         for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
1848                                 if (vq->v_rdev != vp->v_rdev ||
1849                                     vq->v_type != vp->v_type)
1850                                         continue;
1851                                 if (vx)
1852                                         break;
1853                                 vx = vq;
1854                         }
1855                         if (vx == NULL)
1856                                 panic("missing alias");
1857                         if (vq == NULL)
1858                                 vx->v_flag &= ~VALIASED;
1859                         vp->v_flag &= ~VALIASED;
1860                 }
1861                 simple_unlock(&spechash_slock);
1862                 FREE(vp->v_specinfo, M_VNODE);
1863                 vp->v_specinfo = NULL;
1864         }
1865
1866         /*
1867          * If it is on the freelist and not already at the head,
1868          * move it to the head of the list. The test of the back
1869          * pointer and the reference count of zero is because
1870          * it will be removed from the free list by getnewvnode,
1871          * but will not have its reference count incremented until
1872          * after calling vgone. If the reference count were
1873          * incremented first, vgone would (incorrectly) try to
1874          * close the previous instance of the underlying object.
1875          */
1876         if (vp->v_usecount == 0 && !(vp->v_flag & VDOOMED)) {
1877                 s = splbio();
1878                 simple_lock(&vnode_free_list_slock);
1879                 if (vp->v_flag & VFREE) {
1880                         TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
1881                 } else if (vp->v_flag & VTBFREE) {
1882                         TAILQ_REMOVE(&vnode_tobefree_list, vp, v_freelist);
1883                         vp->v_flag &= ~VTBFREE;
1884                         freevnodes++;
1885                 } else
1886                         freevnodes++;
1887                 vp->v_flag |= VFREE;
1888                 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
1889                 simple_unlock(&vnode_free_list_slock);
1890                 splx(s);
1891         }
1892
1893         vp->v_type = VBAD;
1894         simple_unlock(&vp->v_interlock);
1895 }
1896
1897 /*
1898  * Lookup a vnode by device number.
1899  */
1900 int
1901 vfinddev(dev, type, vpp)
1902         dev_t dev;
1903         enum vtype type;
1904         struct vnode **vpp;
1905 {
1906         register struct vnode *vp;
1907         int rc = 0;
1908
1909         simple_lock(&spechash_slock);
1910         for (vp = speclisth[SPECHASH(dev)]; vp; vp = vp->v_specnext) {
1911                 if (dev != vp->v_rdev || type != vp->v_type)
1912                         continue;
1913                 *vpp = vp;
1914                 rc = 1;
1915                 break;
1916         }
1917         simple_unlock(&spechash_slock);
1918         return (rc);
1919 }
1920
1921 /*
1922  * Calculate the total number of references to a special device.
1923  */
1924 int
1925 vcount(vp)
1926         register struct vnode *vp;
1927 {
1928         struct vnode *vq, *vnext;
1929         int count;
1930
1931 loop:
1932         if ((vp->v_flag & VALIASED) == 0)
1933                 return (vp->v_usecount);
1934         simple_lock(&spechash_slock);
1935         for (count = 0, vq = *vp->v_hashchain; vq; vq = vnext) {
1936                 vnext = vq->v_specnext;
1937                 if (vq->v_rdev != vp->v_rdev || vq->v_type != vp->v_type)
1938                         continue;
1939                 /*
1940                  * Alias, but not in use, so flush it out.
1941                  */
1942                 if (vq->v_usecount == 0 && vq != vp) {
1943                         simple_unlock(&spechash_slock);
1944                         vgone(vq);
1945                         goto loop;
1946                 }
1947                 count += vq->v_usecount;
1948         }
1949         simple_unlock(&spechash_slock);
1950         return (count);
1951 }
1952 /*
1953  * Print out a description of a vnode.
1954  */
1955 static char *typename[] =
1956 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD"};
1957
1958 void
1959 vprint(label, vp)
1960         char *label;
1961         register struct vnode *vp;
1962 {
1963         char buf[96];
1964
1965         if (label != NULL)
1966                 printf("%s: %p: ", label, (void *)vp);
1967         else
1968                 printf("%p: ", (void *)vp);
1969         printf("type %s, usecount %d, writecount %d, refcount %d,",
1970             typename[vp->v_type], vp->v_usecount, vp->v_writecount,
1971             vp->v_holdcnt);
1972         buf[0] = '\0';
1973         if (vp->v_flag & VROOT)
1974                 strcat(buf, "|VROOT");
1975         if (vp->v_flag & VTEXT)
1976                 strcat(buf, "|VTEXT");
1977         if (vp->v_flag & VSYSTEM)
1978                 strcat(buf, "|VSYSTEM");
1979         if (vp->v_flag & VXLOCK)
1980                 strcat(buf, "|VXLOCK");
1981         if (vp->v_flag & VXWANT)
1982                 strcat(buf, "|VXWANT");
1983         if (vp->v_flag & VBWAIT)
1984                 strcat(buf, "|VBWAIT");
1985         if (vp->v_flag & VALIASED)
1986                 strcat(buf, "|VALIASED");
1987         if (vp->v_flag & VDOOMED)
1988                 strcat(buf, "|VDOOMED");
1989         if (vp->v_flag & VFREE)
1990                 strcat(buf, "|VFREE");
1991         if (vp->v_flag & VOBJBUF)
1992                 strcat(buf, "|VOBJBUF");
1993         if (buf[0] != '\0')
1994                 printf(" flags (%s)", &buf[1]);
1995         if (vp->v_data == NULL) {
1996                 printf("\n");
1997         } else {
1998                 printf("\n\t");
1999                 VOP_PRINT(vp);
2000         }
2001 }
2002
2003 #ifdef DDB
2004 #include <ddb/ddb.h>
2005 /*
2006  * List all of the locked vnodes in the system.
2007  * Called when debugging the kernel.
2008  */
2009 DB_SHOW_COMMAND(lockedvnodes, lockedvnodes)
2010 {
2011         struct proc *p = curproc;       /* XXX */
2012         struct mount *mp, *nmp;
2013         struct vnode *vp;
2014
2015         printf("Locked vnodes\n");
2016         simple_lock(&mountlist_slock);
2017         for (mp = mountlist.cqh_first; mp != (void *)&mountlist; mp = nmp) {
2018                 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock, p)) {
2019                         nmp = mp->mnt_list.cqe_next;
2020                         continue;
2021                 }
2022                 for (vp = mp->mnt_vnodelist.lh_first;
2023                      vp != NULL;
2024                      vp = vp->v_mntvnodes.le_next) {
2025                         if (VOP_ISLOCKED(vp))
2026                                 vprint((char *)0, vp);
2027                 }
2028                 simple_lock(&mountlist_slock);
2029                 nmp = mp->mnt_list.cqe_next;
2030                 vfs_unbusy(mp, p);
2031         }
2032         simple_unlock(&mountlist_slock);
2033 }
2034 #endif
2035
2036 /*
2037  * Top level filesystem related information gathering.
2038  */
2039 static int      sysctl_ovfs_conf __P(SYSCTL_HANDLER_ARGS);
2040
2041 static int
2042 vfs_sysctl SYSCTL_HANDLER_ARGS
2043 {
2044         int *name = (int *)arg1 - 1;    /* XXX */
2045         u_int namelen = arg2 + 1;       /* XXX */
2046         struct vfsconf *vfsp;
2047
2048 #if 1 || defined(COMPAT_PRELITE2)
2049         /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
2050         if (namelen == 1)
2051                 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
2052 #endif
2053
2054 #ifdef notyet
2055         /* all sysctl names at this level are at least name and field */
2056         if (namelen < 2)
2057                 return (ENOTDIR);               /* overloaded */
2058         if (name[0] != VFS_GENERIC) {
2059                 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
2060                         if (vfsp->vfc_typenum == name[0])
2061                                 break;
2062                 if (vfsp == NULL)
2063                         return (EOPNOTSUPP);
2064                 return ((*vfsp->vfc_vfsops->vfs_sysctl)(&name[1], namelen - 1,
2065                     oldp, oldlenp, newp, newlen, p));
2066         }
2067 #endif
2068         switch (name[1]) {
2069         case VFS_MAXTYPENUM:
2070                 if (namelen != 2)
2071                         return (ENOTDIR);
2072                 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int)));
2073         case VFS_CONF:
2074                 if (namelen != 3)
2075                         return (ENOTDIR);       /* overloaded */
2076                 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
2077                         if (vfsp->vfc_typenum == name[2])
2078                                 break;
2079                 if (vfsp == NULL)
2080                         return (EOPNOTSUPP);
2081                 return (SYSCTL_OUT(req, vfsp, sizeof *vfsp));
2082         }
2083         return (EOPNOTSUPP);
2084 }
2085
2086 SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD, vfs_sysctl,
2087         "Generic filesystem");
2088
2089 #if 1 || defined(COMPAT_PRELITE2)
2090
2091 static int
2092 sysctl_ovfs_conf SYSCTL_HANDLER_ARGS
2093 {
2094         int error;
2095         struct vfsconf *vfsp;
2096         struct ovfsconf ovfs;
2097
2098         for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
2099                 ovfs.vfc_vfsops = vfsp->vfc_vfsops;     /* XXX used as flag */
2100                 strcpy(ovfs.vfc_name, vfsp->vfc_name);
2101                 ovfs.vfc_index = vfsp->vfc_typenum;
2102                 ovfs.vfc_refcount = vfsp->vfc_refcount;
2103                 ovfs.vfc_flags = vfsp->vfc_flags;
2104                 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
2105                 if (error)
2106                         return error;
2107         }
2108         return 0;
2109 }
2110
2111 #endif /* 1 || COMPAT_PRELITE2 */
2112
2113 #if 0
2114 #define KINFO_VNODESLOP 10
2115 /*
2116  * Dump vnode list (via sysctl).
2117  * Copyout address of vnode followed by vnode.
2118  */
2119 /* ARGSUSED */
2120 static int
2121 sysctl_vnode SYSCTL_HANDLER_ARGS
2122 {
2123         struct proc *p = curproc;       /* XXX */
2124         struct mount *mp, *nmp;
2125         struct vnode *nvp, *vp;
2126         int error;
2127
2128 #define VPTRSZ  sizeof (struct vnode *)
2129 #define VNODESZ sizeof (struct vnode)
2130
2131         req->lock = 0;
2132         if (!req->oldptr) /* Make an estimate */
2133                 return (SYSCTL_OUT(req, 0,
2134                         (numvnodes + KINFO_VNODESLOP) * (VPTRSZ + VNODESZ)));
2135
2136         simple_lock(&mountlist_slock);
2137         for (mp = mountlist.cqh_first; mp != (void *)&mountlist; mp = nmp) {
2138                 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock, p)) {
2139                         nmp = mp->mnt_list.cqe_next;
2140                         continue;
2141                 }
2142 again:
2143                 simple_lock(&mntvnode_slock);
2144                 for (vp = mp->mnt_vnodelist.lh_first;
2145                      vp != NULL;
2146                      vp = nvp) {
2147                         /*
2148                          * Check that the vp is still associated with
2149                          * this filesystem.  RACE: could have been
2150                          * recycled onto the same filesystem.
2151                          */
2152                         if (vp->v_mount != mp) {
2153                                 simple_unlock(&mntvnode_slock);
2154                                 goto again;
2155                         }
2156                         nvp = vp->v_mntvnodes.le_next;
2157                         simple_unlock(&mntvnode_slock);
2158                         if ((error = SYSCTL_OUT(req, &vp, VPTRSZ)) ||
2159                             (error = SYSCTL_OUT(req, vp, VNODESZ)))
2160                                 return (error);
2161                         simple_lock(&mntvnode_slock);
2162                 }
2163                 simple_unlock(&mntvnode_slock);
2164                 simple_lock(&mountlist_slock);
2165                 nmp = mp->mnt_list.cqe_next;
2166                 vfs_unbusy(mp, p);
2167         }
2168         simple_unlock(&mountlist_slock);
2169
2170         return (0);
2171 }
2172 #endif
2173
2174 /*
2175  * XXX
2176  * Exporting the vnode list on large systems causes them to crash.
2177  * Exporting the vnode list on medium systems causes sysctl to coredump.
2178  */
2179 #if 0
2180 SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE|CTLFLAG_RD,
2181         0, 0, sysctl_vnode, "S,vnode", "");
2182 #endif
2183
2184 /*
2185  * Check to see if a filesystem is mounted on a block device.
2186  */
2187 int
2188 vfs_mountedon(vp)
2189         struct vnode *vp;
2190 {
2191         struct vnode *vq;
2192         int error = 0;
2193
2194         if (vp->v_specmountpoint != NULL)
2195                 return (EBUSY);
2196         if (vp->v_flag & VALIASED) {
2197                 simple_lock(&spechash_slock);
2198                 for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
2199                         if (vq->v_rdev != vp->v_rdev ||
2200                             vq->v_type != vp->v_type)
2201                                 continue;
2202                         if (vq->v_specmountpoint != NULL) {
2203                                 error = EBUSY;
2204                                 break;
2205                         }
2206                 }
2207                 simple_unlock(&spechash_slock);
2208         }
2209         return (error);
2210 }
2211
2212 /*
2213  * Unmount all filesystems. The list is traversed in reverse order
2214  * of mounting to avoid dependencies.
2215  */
2216 void
2217 vfs_unmountall()
2218 {
2219         struct mount *mp, *nmp;
2220         struct proc *p;
2221         int error;
2222
2223         if (curproc != NULL)
2224                 p = curproc;
2225         else
2226                 p = initproc;   /* XXX XXX should this be proc0? */
2227         /*
2228          * Since this only runs when rebooting, it is not interlocked.
2229          */
2230         for (mp = mountlist.cqh_last; mp != (void *)&mountlist; mp = nmp) {
2231                 nmp = mp->mnt_list.cqe_prev;
2232                 error = dounmount(mp, MNT_FORCE, p);
2233                 if (error) {
2234                         printf("unmount of %s failed (",
2235                             mp->mnt_stat.f_mntonname);
2236                         if (error == EBUSY)
2237                                 printf("BUSY)\n");
2238                         else
2239                                 printf("%d)\n", error);
2240                 }
2241         }
2242 }
2243
2244 /*
2245  * Build hash lists of net addresses and hang them off the mount point.
2246  * Called by ufs_mount() to set up the lists of export addresses.
2247  */
2248 static int
2249 vfs_hang_addrlist(mp, nep, argp)
2250         struct mount *mp;
2251         struct netexport *nep;
2252         struct export_args *argp;
2253 {
2254         register struct netcred *np;
2255         register struct radix_node_head *rnh;
2256         register int i;
2257         struct radix_node *rn;
2258         struct sockaddr *saddr, *smask = 0;
2259         struct domain *dom;
2260         int error;
2261
2262         if (argp->ex_addrlen == 0) {
2263                 if (mp->mnt_flag & MNT_DEFEXPORTED)
2264                         return (EPERM);
2265                 np = &nep->ne_defexported;
2266                 np->netc_exflags = argp->ex_flags;
2267                 np->netc_anon = argp->ex_anon;
2268                 np->netc_anon.cr_ref = 1;
2269                 mp->mnt_flag |= MNT_DEFEXPORTED;
2270                 return (0);
2271         }
2272         i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
2273         np = (struct netcred *) malloc(i, M_NETADDR, M_WAITOK);
2274         bzero((caddr_t) np, i);
2275         saddr = (struct sockaddr *) (np + 1);
2276         if ((error = copyin(argp->ex_addr, (caddr_t) saddr, argp->ex_addrlen)))
2277                 goto out;
2278         if (saddr->sa_len > argp->ex_addrlen)
2279                 saddr->sa_len = argp->ex_addrlen;
2280         if (argp->ex_masklen) {
2281                 smask = (struct sockaddr *) ((caddr_t) saddr + argp->ex_addrlen);
2282                 error = copyin(argp->ex_mask, (caddr_t) smask, argp->ex_masklen);
2283                 if (error)
2284                         goto out;
2285                 if (smask->sa_len > argp->ex_masklen)
2286                         smask->sa_len = argp->ex_masklen;
2287         }
2288         i = saddr->sa_family;
2289         if ((rnh = nep->ne_rtable[i]) == 0) {
2290                 /*
2291                  * Seems silly to initialize every AF when most are not used,
2292                  * do so on demand here
2293                  */
2294                 for (dom = domains; dom; dom = dom->dom_next)
2295                         if (dom->dom_family == i && dom->dom_rtattach) {
2296                                 dom->dom_rtattach((void **) &nep->ne_rtable[i],
2297                                     dom->dom_rtoffset);
2298                                 break;
2299                         }
2300                 if ((rnh = nep->ne_rtable[i]) == 0) {
2301                         error = ENOBUFS;
2302                         goto out;
2303                 }
2304         }
2305         rn = (*rnh->rnh_addaddr) ((caddr_t) saddr, (caddr_t) smask, rnh,
2306             np->netc_rnodes);
2307         if (rn == 0 || np != (struct netcred *) rn) {   /* already exists */
2308                 error = EPERM;
2309                 goto out;
2310         }
2311         np->netc_exflags = argp->ex_flags;
2312         np->netc_anon = argp->ex_anon;
2313         np->netc_anon.cr_ref = 1;
2314         return (0);
2315 out:
2316         free(np, M_NETADDR);
2317         return (error);
2318 }
2319
2320 /* ARGSUSED */
2321 static int
2322 vfs_free_netcred(rn, w)
2323         struct radix_node *rn;
2324         void *w;
2325 {
2326         register struct radix_node_head *rnh = (struct radix_node_head *) w;
2327
2328         (*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, rnh);
2329         free((caddr_t) rn, M_NETADDR);
2330         return (0);
2331 }
2332
2333 /*
2334  * Free the net address hash lists that are hanging off the mount points.
2335  */
2336 static void
2337 vfs_free_addrlist(nep)
2338         struct netexport *nep;
2339 {
2340         register int i;
2341         register struct radix_node_head *rnh;
2342
2343         for (i = 0; i <= AF_MAX; i++)
2344                 if ((rnh = nep->ne_rtable[i])) {
2345                         (*rnh->rnh_walktree) (rnh, vfs_free_netcred,
2346                             (caddr_t) rnh);
2347                         free((caddr_t) rnh, M_RTABLE);
2348                         nep->ne_rtable[i] = 0;
2349                 }
2350 }
2351
2352 int
2353 vfs_export(mp, nep, argp)
2354         struct mount *mp;
2355         struct netexport *nep;
2356         struct export_args *argp;
2357 {
2358         int error;
2359
2360         if (argp->ex_flags & MNT_DELEXPORT) {
2361                 if (mp->mnt_flag & MNT_EXPUBLIC) {
2362                         vfs_setpublicfs(NULL, NULL, NULL);
2363                         mp->mnt_flag &= ~MNT_EXPUBLIC;
2364                 }
2365                 vfs_free_addrlist(nep);
2366                 mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
2367         }
2368         if (argp->ex_flags & MNT_EXPORTED) {
2369                 if (argp->ex_flags & MNT_EXPUBLIC) {
2370                         if ((error = vfs_setpublicfs(mp, nep, argp)) != 0)
2371                                 return (error);
2372                         mp->mnt_flag |= MNT_EXPUBLIC;
2373                 }
2374                 if ((error = vfs_hang_addrlist(mp, nep, argp)))
2375                         return (error);
2376                 mp->mnt_flag |= MNT_EXPORTED;
2377         }
2378         return (0);
2379 }
2380
2381
2382 /*
2383  * Set the publicly exported filesystem (WebNFS). Currently, only
2384  * one public filesystem is possible in the spec (RFC 2054 and 2055)
2385  */
2386 int
2387 vfs_setpublicfs(mp, nep, argp)
2388         struct mount *mp;
2389         struct netexport *nep;
2390         struct export_args *argp;
2391 {
2392         int error;
2393         struct vnode *rvp;
2394         char *cp;
2395
2396         /*
2397          * mp == NULL -> invalidate the current info, the FS is
2398          * no longer exported. May be called from either vfs_export
2399          * or unmount, so check if it hasn't already been done.
2400          */
2401         if (mp == NULL) {
2402                 if (nfs_pub.np_valid) {
2403                         nfs_pub.np_valid = 0;
2404                         if (nfs_pub.np_index != NULL) {
2405                                 FREE(nfs_pub.np_index, M_TEMP);
2406                                 nfs_pub.np_index = NULL;
2407                         }
2408                 }
2409                 return (0);
2410         }
2411
2412         /*
2413          * Only one allowed at a time.
2414          */
2415         if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
2416                 return (EBUSY);
2417
2418         /*
2419          * Get real filehandle for root of exported FS.
2420          */
2421         bzero((caddr_t)&nfs_pub.np_handle, sizeof(nfs_pub.np_handle));
2422         nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid;
2423
2424         if ((error = VFS_ROOT(mp, &rvp)))
2425                 return (error);
2426
2427         if ((error = VFS_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid)))
2428                 return (error);
2429
2430         vput(rvp);
2431
2432         /*
2433          * If an indexfile was specified, pull it in.
2434          */
2435         if (argp->ex_indexfile != NULL) {
2436                 MALLOC(nfs_pub.np_index, char *, MAXNAMLEN + 1, M_TEMP,
2437                     M_WAITOK);
2438                 error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
2439                     MAXNAMLEN, (size_t *)0);
2440                 if (!error) {
2441                         /*
2442                          * Check for illegal filenames.
2443                          */
2444                         for (cp = nfs_pub.np_index; *cp; cp++) {
2445                                 if (*cp == '/') {
2446                                         error = EINVAL;
2447                                         break;
2448                                 }
2449                         }
2450                 }
2451                 if (error) {
2452                         FREE(nfs_pub.np_index, M_TEMP);
2453                         return (error);
2454                 }
2455         }
2456
2457         nfs_pub.np_mount = mp;
2458         nfs_pub.np_valid = 1;
2459         return (0);
2460 }
2461
2462 struct netcred *
2463 vfs_export_lookup(mp, nep, nam)
2464         register struct mount *mp;
2465         struct netexport *nep;
2466         struct sockaddr *nam;
2467 {
2468         register struct netcred *np;
2469         register struct radix_node_head *rnh;
2470         struct sockaddr *saddr;
2471
2472         np = NULL;
2473         if (mp->mnt_flag & MNT_EXPORTED) {
2474                 /*
2475                  * Lookup in the export list first.
2476                  */
2477                 if (nam != NULL) {
2478                         saddr = nam;
2479                         rnh = nep->ne_rtable[saddr->sa_family];
2480                         if (rnh != NULL) {
2481                                 np = (struct netcred *)
2482                                         (*rnh->rnh_matchaddr)((caddr_t)saddr,
2483                                                               rnh);
2484                                 if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
2485                                         np = NULL;
2486                         }
2487                 }
2488                 /*
2489                  * If no address match, use the default if it exists.
2490                  */
2491                 if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
2492                         np = &nep->ne_defexported;
2493         }
2494         return (np);
2495 }
2496
2497 /*
2498  * perform msync on all vnodes under a mount point
2499  * the mount point must be locked.
2500  */
2501 void
2502 vfs_msync(struct mount *mp, int flags) {
2503         struct vnode *vp, *nvp;
2504         struct vm_object *obj;
2505         int anyio, tries;
2506
2507         tries = 5;
2508 loop:
2509         anyio = 0;
2510         for (vp = mp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
2511
2512                 nvp = vp->v_mntvnodes.le_next;
2513
2514                 if (vp->v_mount != mp) {
2515                         goto loop;
2516                 }
2517
2518                 if (vp->v_flag & VXLOCK)        /* XXX: what if MNT_WAIT? */
2519                         continue;
2520
2521                 if (flags != MNT_WAIT) {
2522                         obj = vp->v_object;
2523                         if (obj == NULL || (obj->flags & OBJ_MIGHTBEDIRTY) == 0)
2524                                 continue;
2525                         if (VOP_ISLOCKED(vp))
2526                                 continue;
2527                 }
2528
2529                 simple_lock(&vp->v_interlock);
2530                 if (vp->v_object &&
2531                    (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
2532                         if (!vget(vp,
2533                                 LK_INTERLOCK | LK_EXCLUSIVE | LK_RETRY | LK_NOOBJ, curproc)) {
2534                                 if (vp->v_object) {
2535                                         vm_object_page_clean(vp->v_object, 0, 0, flags == MNT_WAIT ? OBJPC_SYNC : 0);
2536                                         anyio = 1;
2537                                 }
2538                                 vput(vp);
2539                         }
2540                 } else {
2541                         simple_unlock(&vp->v_interlock);
2542                 }
2543         }
2544         if (anyio && (--tries > 0))
2545                 goto loop;
2546 }
2547
2548 /*
2549  * Create the VM object needed for VMIO and mmap support.  This
2550  * is done for all VREG files in the system.  Some filesystems might
2551  * afford the additional metadata buffering capability of the
2552  * VMIO code by making the device node be VMIO mode also.
2553  *
2554  * vp must be locked when vfs_object_create is called.
2555  */
2556 int
2557 vfs_object_create(vp, p, cred)
2558         struct vnode *vp;
2559         struct proc *p;
2560         struct ucred *cred;
2561 {
2562         struct vattr vat;
2563         vm_object_t object;
2564         int error = 0;
2565
2566         if ((vp->v_type != VREG) && (vp->v_type != VBLK))
2567                 return 0;
2568
2569 retry:
2570         if ((object = vp->v_object) == NULL) {
2571                 if (vp->v_type == VREG) {
2572                         if ((error = VOP_GETATTR(vp, &vat, cred, p)) != 0)
2573                                 goto retn;
2574                         object = vnode_pager_alloc(vp, vat.va_size, 0, 0);
2575                 } else if (major(vp->v_rdev) < nblkdev &&
2576                     bdevsw[major(vp->v_rdev)] != NULL) {
2577                         /*
2578                          * This simply allocates the biggest object possible
2579                          * for a VBLK vnode.  This should be fixed, but doesn't
2580                          * cause any problems (yet).
2581                          */
2582                         object = vnode_pager_alloc(vp, IDX_TO_OFF(INT_MAX), 0, 0);
2583                 } else {
2584                         goto retn;
2585                 }
2586                 /*
2587                  * Dereference the reference we just created.  This assumes
2588                  * that the object is associated with the vp.
2589                  */
2590                 object->ref_count--;
2591                 vp->v_usecount--;
2592         } else {
2593                 if (object->flags & OBJ_DEAD) {
2594                         VOP_UNLOCK(vp, 0, p);
2595                         tsleep(object, PVM, "vodead", 0);
2596                         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
2597                         goto retry;
2598                 }
2599         }
2600
2601         KASSERT(vp->v_object != NULL, ("vfs_object_create: NULL object"));
2602         vp->v_flag |= VOBJBUF;
2603
2604 retn:
2605         return error;
2606 }
2607
2608 static void
2609 vfree(vp)
2610         struct vnode *vp;
2611 {
2612         int s;
2613
2614         s = splbio();
2615         simple_lock(&vnode_free_list_slock);
2616         if (vp->v_flag & VTBFREE) {
2617                 TAILQ_REMOVE(&vnode_tobefree_list, vp, v_freelist);
2618                 vp->v_flag &= ~VTBFREE;
2619         }
2620         if (vp->v_flag & VAGE) {
2621                 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
2622         } else {
2623                 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
2624         }
2625         freevnodes++;
2626         simple_unlock(&vnode_free_list_slock);
2627         vp->v_flag &= ~VAGE;
2628         vp->v_flag |= VFREE;
2629         splx(s);
2630 }
2631
2632 void
2633 vbusy(vp)
2634         struct vnode *vp;
2635 {
2636         int s;
2637
2638         s = splbio();
2639         simple_lock(&vnode_free_list_slock);
2640         if (vp->v_flag & VTBFREE) {
2641                 TAILQ_REMOVE(&vnode_tobefree_list, vp, v_freelist);
2642                 vp->v_flag &= ~VTBFREE;
2643         } else {
2644                 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
2645                 freevnodes--;
2646         }
2647         simple_unlock(&vnode_free_list_slock);
2648         vp->v_flag &= ~(VFREE|VAGE);
2649         splx(s);
2650 }
2651
2652 /*
2653  * Record a process's interest in events which might happen to
2654  * a vnode.  Because poll uses the historic select-style interface
2655  * internally, this routine serves as both the ``check for any
2656  * pending events'' and the ``record my interest in future events''
2657  * functions.  (These are done together, while the lock is held,
2658  * to avoid race conditions.)
2659  */
2660 int
2661 vn_pollrecord(vp, p, events)
2662         struct vnode *vp;
2663         struct proc *p;
2664         short events;
2665 {
2666         simple_lock(&vp->v_pollinfo.vpi_lock);
2667         if (vp->v_pollinfo.vpi_revents & events) {
2668                 /*
2669                  * This leaves events we are not interested
2670                  * in available for the other process which
2671                  * which presumably had requested them
2672                  * (otherwise they would never have been
2673                  * recorded).
2674                  */
2675                 events &= vp->v_pollinfo.vpi_revents;
2676                 vp->v_pollinfo.vpi_revents &= ~events;
2677
2678                 simple_unlock(&vp->v_pollinfo.vpi_lock);
2679                 return events;
2680         }
2681         vp->v_pollinfo.vpi_events |= events;
2682         selrecord(p, &vp->v_pollinfo.vpi_selinfo);
2683         simple_unlock(&vp->v_pollinfo.vpi_lock);
2684         return 0;
2685 }
2686
2687 /*
2688  * Note the occurrence of an event.  If the VN_POLLEVENT macro is used,
2689  * it is possible for us to miss an event due to race conditions, but
2690  * that condition is expected to be rare, so for the moment it is the
2691  * preferred interface.
2692  */
2693 void
2694 vn_pollevent(vp, events)
2695         struct vnode *vp;
2696         short events;
2697 {
2698         simple_lock(&vp->v_pollinfo.vpi_lock);
2699         if (vp->v_pollinfo.vpi_events & events) {
2700                 /*
2701                  * We clear vpi_events so that we don't
2702                  * call selwakeup() twice if two events are
2703                  * posted before the polling process(es) is
2704                  * awakened.  This also ensures that we take at
2705                  * most one selwakeup() if the polling process
2706                  * is no longer interested.  However, it does
2707                  * mean that only one event can be noticed at
2708                  * a time.  (Perhaps we should only clear those
2709                  * event bits which we note?) XXX
2710                  */
2711                 vp->v_pollinfo.vpi_events = 0;  /* &= ~events ??? */
2712                 vp->v_pollinfo.vpi_revents |= events;
2713                 selwakeup(&vp->v_pollinfo.vpi_selinfo);
2714         }
2715         simple_unlock(&vp->v_pollinfo.vpi_lock);
2716 }
2717
2718 /*
2719  * Wake up anyone polling on vp because it is being revoked.
2720  * This depends on dead_poll() returning POLLHUP for correct
2721  * behavior.
2722  */
2723 void
2724 vn_pollgone(vp)
2725         struct vnode *vp;
2726 {
2727         simple_lock(&vp->v_pollinfo.vpi_lock);
2728         if (vp->v_pollinfo.vpi_events) {
2729                 vp->v_pollinfo.vpi_events = 0;
2730                 selwakeup(&vp->v_pollinfo.vpi_selinfo);
2731         }
2732         simple_unlock(&vp->v_pollinfo.vpi_lock);
2733 }
2734
2735
2736
2737 /*
2738  * Routine to create and manage a filesystem syncer vnode.
2739  */
2740 #define sync_close ((int (*) __P((struct  vop_close_args *)))nullop)
2741 static int      sync_fsync __P((struct  vop_fsync_args *));
2742 static int      sync_inactive __P((struct  vop_inactive_args *));
2743 static int      sync_reclaim  __P((struct  vop_reclaim_args *));
2744 #define sync_lock ((int (*) __P((struct  vop_lock_args *)))vop_nolock)
2745 #define sync_unlock ((int (*) __P((struct  vop_unlock_args *)))vop_nounlock)
2746 static int      sync_print __P((struct vop_print_args *));
2747 #define sync_islocked ((int(*) __P((struct vop_islocked_args *)))vop_noislocked)
2748
2749 static vop_t **sync_vnodeop_p;
2750 static struct vnodeopv_entry_desc sync_vnodeop_entries[] = {
2751         { &vop_default_desc,    (vop_t *) vop_eopnotsupp },
2752         { &vop_close_desc,      (vop_t *) sync_close },         /* close */
2753         { &vop_fsync_desc,      (vop_t *) sync_fsync },         /* fsync */
2754         { &vop_inactive_desc,   (vop_t *) sync_inactive },      /* inactive */
2755         { &vop_reclaim_desc,    (vop_t *) sync_reclaim },       /* reclaim */
2756         { &vop_lock_desc,       (vop_t *) sync_lock },          /* lock */
2757         { &vop_unlock_desc,     (vop_t *) sync_unlock },        /* unlock */
2758         { &vop_print_desc,      (vop_t *) sync_print },         /* print */
2759         { &vop_islocked_desc,   (vop_t *) sync_islocked },      /* islocked */
2760         { NULL, NULL }
2761 };
2762 static struct vnodeopv_desc sync_vnodeop_opv_desc =
2763         { &sync_vnodeop_p, sync_vnodeop_entries };
2764
2765 VNODEOP_SET(sync_vnodeop_opv_desc);
2766
2767 /*
2768  * Create a new filesystem syncer vnode for the specified mount point.
2769  */
2770 int
2771 vfs_allocate_syncvnode(mp)
2772         struct mount *mp;
2773 {
2774         struct vnode *vp;
2775         static long start, incr, next;
2776         int error;
2777
2778         /* Allocate a new vnode */
2779         if ((error = getnewvnode(VT_VFS, mp, sync_vnodeop_p, &vp)) != 0) {
2780                 mp->mnt_syncer = NULL;
2781                 return (error);
2782         }
2783         vp->v_type = VNON;
2784         /*
2785          * Place the vnode onto the syncer worklist. We attempt to
2786          * scatter them about on the list so that they will go off
2787          * at evenly distributed times even if all the filesystems
2788          * are mounted at once.
2789          */
2790         next += incr;
2791         if (next == 0 || next > syncer_maxdelay) {
2792                 start /= 2;
2793                 incr /= 2;
2794                 if (start == 0) {
2795                         start = syncer_maxdelay / 2;
2796                         incr = syncer_maxdelay;
2797                 }
2798                 next = start;
2799         }
2800         vn_syncer_add_to_worklist(vp, syncdelay > 0 ? next % syncdelay : 0);
2801         mp->mnt_syncer = vp;
2802         return (0);
2803 }
2804
2805 /*
2806  * Do a lazy sync of the filesystem.
2807  */
2808 static int
2809 sync_fsync(ap)
2810         struct vop_fsync_args /* {
2811                 struct vnode *a_vp;
2812                 struct ucred *a_cred;
2813                 int a_waitfor;
2814                 struct proc *a_p;
2815         } */ *ap;
2816 {
2817         struct vnode *syncvp = ap->a_vp;
2818         struct mount *mp = syncvp->v_mount;
2819         struct proc *p = ap->a_p;
2820         int asyncflag;
2821
2822         /*
2823          * We only need to do something if this is a lazy evaluation.
2824          */
2825         if (ap->a_waitfor != MNT_LAZY)
2826                 return (0);
2827
2828         /*
2829          * Move ourselves to the back of the sync list.
2830          */
2831         vn_syncer_add_to_worklist(syncvp, syncdelay);
2832
2833         /*
2834          * Walk the list of vnodes pushing all that are dirty and
2835          * not already on the sync list.
2836          */
2837         simple_lock(&mountlist_slock);
2838         if (vfs_busy(mp, LK_EXCLUSIVE | LK_NOWAIT, &mountlist_slock, p) != 0) {
2839                 simple_unlock(&mountlist_slock);
2840                 return (0);
2841         }
2842         asyncflag = mp->mnt_flag & MNT_ASYNC;
2843         mp->mnt_flag &= ~MNT_ASYNC;
2844         vfs_msync(mp, MNT_NOWAIT);
2845         VFS_SYNC(mp, MNT_LAZY, ap->a_cred, p);
2846         if (asyncflag)
2847                 mp->mnt_flag |= MNT_ASYNC;
2848         vfs_unbusy(mp, p);
2849         return (0);
2850 }
2851
2852 /*
2853  * The syncer vnode is no referenced.
2854  */
2855 static int
2856 sync_inactive(ap)
2857         struct vop_inactive_args /* {
2858                 struct vnode *a_vp;
2859                 struct proc *a_p;
2860         } */ *ap;
2861 {
2862
2863         vgone(ap->a_vp);
2864         return (0);
2865 }
2866
2867 /*
2868  * The syncer vnode is no longer needed and is being decommissioned.
2869  *
2870  * Modifications to the worklist must be protected at splbio().
2871  */
2872 static int
2873 sync_reclaim(ap)
2874         struct vop_reclaim_args /* {
2875                 struct vnode *a_vp;
2876         } */ *ap;
2877 {
2878         struct vnode *vp = ap->a_vp;
2879         int s;
2880
2881         s = splbio();
2882         vp->v_mount->mnt_syncer = NULL;
2883         if (vp->v_flag & VONWORKLST) {
2884                 LIST_REMOVE(vp, v_synclist);
2885                 vp->v_flag &= ~VONWORKLST;
2886         }
2887         splx(s);
2888
2889         return (0);
2890 }
2891
2892 /*
2893  * Print out a syncer vnode.
2894  */
2895 static int
2896 sync_print(ap)
2897         struct vop_print_args /* {
2898                 struct vnode *a_vp;
2899         } */ *ap;
2900 {
2901         struct vnode *vp = ap->a_vp;
2902
2903         printf("syncer vnode");
2904         if (vp->v_vnlock != NULL)
2905                 lockmgr_printinfo(vp->v_vnlock);
2906         printf("\n");
2907         return (0);
2908 }