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.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
38 * @(#)vfs_subr.c 8.31 (Berkeley) 5/26/95
43 * External virtual filesystem routines
47 #include <sys/param.h>
48 #include <sys/systm.h>
52 #include <sys/dirent.h>
53 #include <sys/domain.h>
54 #include <sys/eventhandler.h>
55 #include <sys/fcntl.h>
56 #include <sys/kernel.h>
57 #include <sys/kthread.h>
58 #include <sys/malloc.h>
59 #include <sys/mount.h>
60 #include <sys/namei.h>
62 #include <sys/reboot.h>
63 #include <sys/socket.h>
65 #include <sys/sysctl.h>
66 #include <sys/vmmeter.h>
67 #include <sys/vnode.h>
69 #include <machine/limits.h>
72 #include <vm/vm_object.h>
73 #include <vm/vm_extern.h>
75 #include <vm/vm_map.h>
76 #include <vm/vm_page.h>
77 #include <vm/vm_pager.h>
78 #include <vm/vnode_pager.h>
79 #include <vm/vm_zone.h>
81 static MALLOC_DEFINE(M_NETADDR, "Export Host", "Export host address structure");
83 static void insmntque __P((struct vnode *vp, struct mount *mp));
84 static void vclean __P((struct vnode *vp, int flags, struct proc *p));
85 static void vfree __P((struct vnode *));
86 static unsigned long numvnodes;
87 SYSCTL_INT(_debug, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0, "");
89 enum vtype iftovt_tab[16] = {
90 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
91 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
94 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
95 S_IFSOCK, S_IFIFO, S_IFMT,
98 static TAILQ_HEAD(freelst, struct vnode) vnode_free_list; /* vnode free list */
99 struct tobefreelist vnode_tobefree_list; /* vnode free list */
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, "");
106 static int reassignbufcalls;
107 SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW, &reassignbufcalls, 0, "");
108 static int reassignbufloops;
109 SYSCTL_INT(_vfs, OID_AUTO, reassignbufloops, CTLFLAG_RW, &reassignbufloops, 0, "");
110 static int reassignbufsortgood;
111 SYSCTL_INT(_vfs, OID_AUTO, reassignbufsortgood, CTLFLAG_RW, &reassignbufsortgood, 0, "");
112 static int reassignbufsortbad;
113 SYSCTL_INT(_vfs, OID_AUTO, reassignbufsortbad, CTLFLAG_RW, &reassignbufsortbad, 0, "");
114 static int reassignbufmethod = 1;
115 SYSCTL_INT(_vfs, OID_AUTO, reassignbufmethod, CTLFLAG_RW, &reassignbufmethod, 0, "");
117 #ifdef ENABLE_VFS_IOOPT
119 SYSCTL_INT(_vfs, OID_AUTO, ioopt, CTLFLAG_RW, &vfs_ioopt, 0, "");
122 struct mntlist mountlist = TAILQ_HEAD_INITIALIZER(mountlist); /* mounted fs */
123 struct simplelock mountlist_slock;
124 struct simplelock mntvnode_slock;
125 int nfs_mount_type = -1;
126 #ifndef NULL_SIMPLELOCKS
127 static struct simplelock mntid_slock;
128 static struct simplelock vnode_free_list_slock;
129 static struct simplelock spechash_slock;
131 struct nfs_public nfs_pub; /* publicly exported FS */
132 static vm_zone_t vnode_zone;
135 * The workitem queue.
137 #define SYNCER_MAXDELAY 32
138 static int syncer_maxdelay = SYNCER_MAXDELAY; /* maximum delay time */
139 time_t syncdelay = 30; /* max time to delay syncing data */
140 time_t filedelay = 30; /* time to delay syncing files */
141 SYSCTL_INT(_kern, OID_AUTO, filedelay, CTLFLAG_RW, &filedelay, 0, "");
142 time_t dirdelay = 29; /* time to delay syncing directories */
143 SYSCTL_INT(_kern, OID_AUTO, dirdelay, CTLFLAG_RW, &dirdelay, 0, "");
144 time_t metadelay = 28; /* time to delay syncing metadata */
145 SYSCTL_INT(_kern, OID_AUTO, metadelay, CTLFLAG_RW, &metadelay, 0, "");
146 static int rushjob; /* number of slots to run ASAP */
147 static int stat_rush_requests; /* number of times I/O speeded up */
148 SYSCTL_INT(_debug, OID_AUTO, rush_requests, CTLFLAG_RW, &stat_rush_requests, 0, "");
150 static int syncer_delayno = 0;
151 static long syncer_mask;
152 LIST_HEAD(synclist, struct vnode);
153 static struct synclist *syncer_workitem_pending;
156 SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RW,
157 &desiredvnodes, 0, "Maximum number of vnodes");
159 static void vfs_free_addrlist __P((struct netexport *nep));
160 static int vfs_free_netcred __P((struct radix_node *rn, void *w));
161 static int vfs_hang_addrlist __P((struct mount *mp, struct netexport *nep,
162 struct export_args *argp));
165 * Initialize the vnode management data structures.
171 desiredvnodes = maxproc + cnt.v_page_count / 4;
172 simple_lock_init(&mntvnode_slock);
173 simple_lock_init(&mntid_slock);
174 simple_lock_init(&spechash_slock);
175 TAILQ_INIT(&vnode_free_list);
176 TAILQ_INIT(&vnode_tobefree_list);
177 simple_lock_init(&vnode_free_list_slock);
178 vnode_zone = zinit("VNODE", sizeof (struct vnode), 0, 0, 5);
180 * Initialize the filesystem syncer.
182 syncer_workitem_pending = hashinit(syncer_maxdelay, M_VNODE,
184 syncer_maxdelay = syncer_mask + 1;
188 * Mark a mount point as busy. Used to synchronize access and to delay
189 * unmounting. Interlock is not released on failure.
192 vfs_busy(mp, flags, interlkp, p)
195 struct simplelock *interlkp;
200 if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
201 if (flags & LK_NOWAIT)
203 mp->mnt_kern_flag |= MNTK_MWAIT;
205 simple_unlock(interlkp);
208 * Since all busy locks are shared except the exclusive
209 * lock granted when unmounting, the only place that a
210 * wakeup needs to be done is at the release of the
211 * exclusive lock at the end of dounmount.
213 tsleep((caddr_t)mp, PVFS, "vfs_busy", 0);
215 simple_lock(interlkp);
219 lkflags = LK_SHARED | LK_NOPAUSE;
221 lkflags |= LK_INTERLOCK;
222 if (lockmgr(&mp->mnt_lock, lkflags, interlkp, p))
223 panic("vfs_busy: unexpected lock failure");
228 * Free a busy filesystem.
236 lockmgr(&mp->mnt_lock, LK_RELEASE, NULL, p);
240 * Lookup a filesystem type, and if found allocate and initialize
241 * a mount structure for it.
243 * Devname is usually updated by mount(8) after booting.
246 vfs_rootmountalloc(fstypename, devname, mpp)
251 struct proc *p = curproc; /* XXX */
252 struct vfsconf *vfsp;
255 if (fstypename == NULL)
257 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
258 if (!strcmp(vfsp->vfc_name, fstypename))
262 mp = malloc((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK);
263 bzero((char *)mp, (u_long)sizeof(struct mount));
264 lockinit(&mp->mnt_lock, PVFS, "vfslock", 0, LK_NOPAUSE);
265 (void)vfs_busy(mp, LK_NOWAIT, 0, p);
266 LIST_INIT(&mp->mnt_vnodelist);
268 mp->mnt_op = vfsp->vfc_vfsops;
269 mp->mnt_flag = MNT_RDONLY;
270 mp->mnt_vnodecovered = NULLVP;
271 vfsp->vfc_refcount++;
272 mp->mnt_iosize_max = DFLTPHYS;
273 mp->mnt_stat.f_type = vfsp->vfc_typenum;
274 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
275 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
276 mp->mnt_stat.f_mntonname[0] = '/';
277 mp->mnt_stat.f_mntonname[1] = 0;
278 (void) copystr(devname, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, 0);
284 * Find an appropriate filesystem to use for the root. If a filesystem
285 * has not been preselected, walk through the list of known filesystems
286 * trying those that have mountroot routines, and try them until one
287 * works or we have tried them all.
289 #ifdef notdef /* XXX JH */
291 lite2_vfs_mountroot()
293 struct vfsconf *vfsp;
294 extern int (*lite2_mountroot) __P((void));
297 if (lite2_mountroot != NULL)
298 return ((*lite2_mountroot)());
299 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
300 if (vfsp->vfc_mountroot == NULL)
302 if ((error = (*vfsp->vfc_mountroot)()) == 0)
304 printf("%s_mountroot failed: %d\n", vfsp->vfc_name, error);
311 * Lookup a mount point by filesystem identifier.
317 register struct mount *mp;
319 simple_lock(&mountlist_slock);
320 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
321 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
322 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
323 simple_unlock(&mountlist_slock);
327 simple_unlock(&mountlist_slock);
328 return ((struct mount *) 0);
332 * Get a new unique fsid. Try to make its val[0] unique, since this value
333 * will be used to create fake device numbers for stat(). Also try (but
334 * not so hard) make its val[0] unique mod 2^16, since some emulators only
335 * support 16-bit device numbers. We end up with unique val[0]'s for the
336 * first 2^16 calls and unique val[0]'s mod 2^16 for the first 2^8 calls.
338 * Keep in mind that several mounts may be running in parallel. Starting
339 * the search one past where the previous search terminated is both a
340 * micro-optimization and a defense against returning the same fsid to
347 static u_int16_t mntid_base;
351 simple_lock(&mntid_slock);
352 mtype = mp->mnt_vfc->vfc_typenum;
353 tfsid.val[1] = mtype;
354 mtype = (mtype & 0xFF) << 16;
356 tfsid.val[0] = makeudev(255, mtype | mntid_base++);
357 if (vfs_getvfs(&tfsid) == NULL)
360 mp->mnt_stat.f_fsid.val[0] = tfsid.val[0];
361 mp->mnt_stat.f_fsid.val[1] = tfsid.val[1];
362 simple_unlock(&mntid_slock);
366 * Knob to control the precision of file timestamps:
368 * 0 = seconds only; nanoseconds zeroed.
369 * 1 = seconds and nanoseconds, accurate within 1/HZ.
370 * 2 = seconds and nanoseconds, truncated to microseconds.
371 * >=3 = seconds and nanoseconds, maximum precision.
373 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC };
375 static int timestamp_precision = TSP_SEC;
376 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW,
377 ×tamp_precision, 0, "");
380 * Get a current timestamp.
384 struct timespec *tsp;
388 switch (timestamp_precision) {
390 tsp->tv_sec = time_second;
398 TIMEVAL_TO_TIMESPEC(&tv, tsp);
408 * Set vnode attributes to VNOVAL
412 register struct vattr *vap;
416 vap->va_size = VNOVAL;
417 vap->va_bytes = VNOVAL;
418 vap->va_mode = VNOVAL;
419 vap->va_nlink = VNOVAL;
420 vap->va_uid = VNOVAL;
421 vap->va_gid = VNOVAL;
422 vap->va_fsid = VNOVAL;
423 vap->va_fileid = VNOVAL;
424 vap->va_blocksize = VNOVAL;
425 vap->va_rdev = VNOVAL;
426 vap->va_atime.tv_sec = VNOVAL;
427 vap->va_atime.tv_nsec = VNOVAL;
428 vap->va_mtime.tv_sec = VNOVAL;
429 vap->va_mtime.tv_nsec = VNOVAL;
430 vap->va_ctime.tv_sec = VNOVAL;
431 vap->va_ctime.tv_nsec = VNOVAL;
432 vap->va_flags = VNOVAL;
433 vap->va_gen = VNOVAL;
438 * Routines having to do with the management of the vnode table.
440 extern vop_t **dead_vnodeop_p;
443 * Return the next vnode from the free list.
446 getnewvnode(tag, mp, vops, vpp)
453 struct proc *p = curproc; /* XXX */
454 struct vnode *vp, *tvp, *nvp;
456 TAILQ_HEAD(freelst, struct vnode) vnode_tmp_list;
459 * We take the least recently used vnode from the freelist
460 * if we can get it and it has no cached pages, and no
461 * namecache entries are relative to it.
462 * Otherwise we allocate a new vnode
466 simple_lock(&vnode_free_list_slock);
467 TAILQ_INIT(&vnode_tmp_list);
469 for (vp = TAILQ_FIRST(&vnode_tobefree_list); vp; vp = nvp) {
470 nvp = TAILQ_NEXT(vp, v_freelist);
471 TAILQ_REMOVE(&vnode_tobefree_list, vp, v_freelist);
472 if (vp->v_flag & VAGE) {
473 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
475 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
477 vp->v_flag &= ~(VTBFREE|VAGE);
480 panic("tobe free vnode isn't");
484 if (wantfreevnodes && freevnodes < wantfreevnodes) {
486 } else if (!wantfreevnodes && freevnodes <= desiredvnodes) {
488 * XXX: this is only here to be backwards compatible
492 for (vp = TAILQ_FIRST(&vnode_free_list); vp; vp = nvp) {
493 nvp = TAILQ_NEXT(vp, v_freelist);
494 if (!simple_lock_try(&vp->v_interlock))
497 panic("free vnode isn't");
499 object = vp->v_object;
500 if (object && (object->resident_page_count || object->ref_count)) {
501 printf("object inconsistant state: RPC: %d, RC: %d\n",
502 object->resident_page_count, object->ref_count);
503 /* Don't recycle if it's caching some pages */
504 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
505 TAILQ_INSERT_TAIL(&vnode_tmp_list, vp, v_freelist);
507 } else if (LIST_FIRST(&vp->v_cache_src)) {
508 /* Don't recycle if active in the namecache */
509 simple_unlock(&vp->v_interlock);
517 for (tvp = TAILQ_FIRST(&vnode_tmp_list); tvp; tvp = nvp) {
518 nvp = TAILQ_NEXT(tvp, v_freelist);
519 TAILQ_REMOVE(&vnode_tmp_list, tvp, v_freelist);
520 TAILQ_INSERT_TAIL(&vnode_free_list, tvp, v_freelist);
521 simple_unlock(&tvp->v_interlock);
525 vp->v_flag |= VDOOMED;
526 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
528 simple_unlock(&vnode_free_list_slock);
531 if (vp->v_type != VBAD) {
534 simple_unlock(&vp->v_interlock);
542 panic("cleaned vnode isn't");
545 panic("Clean vnode has pending I/O's");
555 vp->v_writecount = 0; /* XXX */
557 simple_unlock(&vnode_free_list_slock);
558 vp = (struct vnode *) zalloc(vnode_zone);
559 bzero((char *) vp, sizeof *vp);
560 simple_lock_init(&vp->v_interlock);
563 LIST_INIT(&vp->v_cache_src);
564 TAILQ_INIT(&vp->v_cache_dst);
568 TAILQ_INIT(&vp->v_cleanblkhd);
569 TAILQ_INIT(&vp->v_dirtyblkhd);
579 vfs_object_create(vp, p, p->p_ucred);
584 * Move a vnode from one mount queue to another.
588 register struct vnode *vp;
589 register struct mount *mp;
592 simple_lock(&mntvnode_slock);
594 * Delete from old mount point vnode list, if on one.
596 if (vp->v_mount != NULL)
597 LIST_REMOVE(vp, v_mntvnodes);
599 * Insert into list of vnodes for the new mount point, if available.
601 if ((vp->v_mount = mp) == NULL) {
602 simple_unlock(&mntvnode_slock);
605 LIST_INSERT_HEAD(&mp->mnt_vnodelist, vp, v_mntvnodes);
606 simple_unlock(&mntvnode_slock);
610 * Update outstanding I/O count and do wakeup if requested.
614 register struct buf *bp;
616 register struct vnode *vp;
618 bp->b_flags &= ~B_WRITEINPROG;
619 if ((vp = bp->b_vp)) {
621 if (vp->v_numoutput < 0)
622 panic("vwakeup: neg numoutput");
623 if ((vp->v_numoutput == 0) && (vp->v_flag & VBWAIT)) {
624 vp->v_flag &= ~VBWAIT;
625 wakeup((caddr_t) &vp->v_numoutput);
631 * Flush out and invalidate all buffers associated with a vnode.
632 * Called with the underlying object locked.
635 vinvalbuf(vp, flags, cred, p, slpflag, slptimeo)
636 register struct vnode *vp;
640 int slpflag, slptimeo;
642 register struct buf *bp;
643 struct buf *nbp, *blist;
647 if (flags & V_SAVE) {
649 while (vp->v_numoutput) {
650 vp->v_flag |= VBWAIT;
651 error = tsleep((caddr_t)&vp->v_numoutput,
652 slpflag | (PRIBIO + 1), "vinvlbuf", slptimeo);
658 if (!TAILQ_EMPTY(&vp->v_dirtyblkhd)) {
660 if ((error = VOP_FSYNC(vp, cred, MNT_WAIT, p)) != 0)
663 if (vp->v_numoutput > 0 ||
664 !TAILQ_EMPTY(&vp->v_dirtyblkhd))
665 panic("vinvalbuf: dirty bufs");
671 blist = TAILQ_FIRST(&vp->v_cleanblkhd);
673 blist = TAILQ_FIRST(&vp->v_dirtyblkhd);
677 for (bp = blist; bp; bp = nbp) {
678 nbp = TAILQ_NEXT(bp, b_vnbufs);
679 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
680 error = BUF_TIMELOCK(bp,
681 LK_EXCLUSIVE | LK_SLEEPFAIL,
682 "vinvalbuf", slpflag, slptimeo);
689 * XXX Since there are no node locks for NFS, I
690 * believe there is a slight chance that a delayed
691 * write will occur while sleeping just above, so
692 * check for it. Note that vfs_bio_awrite expects
693 * buffers to reside on a queue, while VOP_BWRITE and
696 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
699 if (bp->b_vp == vp) {
700 if (bp->b_flags & B_CLUSTEROK) {
705 bp->b_flags |= B_ASYNC;
710 (void) BUF_WRITE(bp);
715 bp->b_flags |= (B_INVAL | B_NOCACHE | B_RELBUF);
716 bp->b_flags &= ~B_ASYNC;
721 while (vp->v_numoutput > 0) {
722 vp->v_flag |= VBWAIT;
723 tsleep(&vp->v_numoutput, PVM, "vnvlbv", 0);
729 * Destroy the copy in the VM cache, too.
731 simple_lock(&vp->v_interlock);
732 object = vp->v_object;
733 if (object != NULL) {
734 vm_object_page_remove(object, 0, 0,
735 (flags & V_SAVE) ? TRUE : FALSE);
737 simple_unlock(&vp->v_interlock);
739 if (!TAILQ_EMPTY(&vp->v_dirtyblkhd) || !TAILQ_EMPTY(&vp->v_cleanblkhd))
740 panic("vinvalbuf: flush failed");
745 * Truncate a file's buffer and pages to a specified length. This
746 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
750 vtruncbuf(vp, cred, p, length, blksize)
751 register struct vnode *vp;
757 register struct buf *bp;
763 * Round up to the *next* lbn.
765 trunclbn = (length + blksize - 1) / blksize;
772 for (bp = TAILQ_FIRST(&vp->v_cleanblkhd); bp; bp = nbp) {
773 nbp = TAILQ_NEXT(bp, b_vnbufs);
774 if (bp->b_lblkno >= trunclbn) {
775 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
776 BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL);
780 bp->b_flags |= (B_INVAL | B_RELBUF);
781 bp->b_flags &= ~B_ASYNC;
786 (((nbp->b_xflags & BX_VNCLEAN) == 0) ||
788 (nbp->b_flags & B_DELWRI))) {
794 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
795 nbp = TAILQ_NEXT(bp, b_vnbufs);
796 if (bp->b_lblkno >= trunclbn) {
797 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
798 BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL);
802 bp->b_flags |= (B_INVAL | B_RELBUF);
803 bp->b_flags &= ~B_ASYNC;
808 (((nbp->b_xflags & BX_VNDIRTY) == 0) ||
810 (nbp->b_flags & B_DELWRI) == 0)) {
819 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
820 nbp = TAILQ_NEXT(bp, b_vnbufs);
821 if ((bp->b_flags & B_DELWRI) && (bp->b_lblkno < 0)) {
822 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
823 BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL);
827 if (bp->b_vp == vp) {
828 bp->b_flags |= B_ASYNC;
830 bp->b_flags &= ~B_ASYNC;
840 while (vp->v_numoutput > 0) {
841 vp->v_flag |= VBWAIT;
842 tsleep(&vp->v_numoutput, PVM, "vbtrunc", 0);
847 vnode_pager_setsize(vp, length);
853 * Associate a buffer with a vnode.
857 register struct vnode *vp;
858 register struct buf *bp;
862 KASSERT(bp->b_vp == NULL, ("bgetvp: not free"));
866 bp->b_dev = vn_todev(vp);
868 * Insert onto list for new vnode.
871 bp->b_xflags |= BX_VNCLEAN;
872 bp->b_xflags &= ~BX_VNDIRTY;
873 TAILQ_INSERT_TAIL(&vp->v_cleanblkhd, bp, b_vnbufs);
878 * Disassociate a buffer from a vnode.
882 register struct buf *bp;
885 struct buflists *listheadp;
888 KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
891 * Delete from old vnode list, if on one.
895 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) {
896 if (bp->b_xflags & BX_VNDIRTY)
897 listheadp = &vp->v_dirtyblkhd;
899 listheadp = &vp->v_cleanblkhd;
900 TAILQ_REMOVE(listheadp, bp, b_vnbufs);
901 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN);
903 if ((vp->v_flag & VONWORKLST) && TAILQ_EMPTY(&vp->v_dirtyblkhd)) {
904 vp->v_flag &= ~VONWORKLST;
905 LIST_REMOVE(vp, v_synclist);
908 bp->b_vp = (struct vnode *) 0;
913 * The workitem queue.
915 * It is useful to delay writes of file data and filesystem metadata
916 * for tens of seconds so that quickly created and deleted files need
917 * not waste disk bandwidth being created and removed. To realize this,
918 * we append vnodes to a "workitem" queue. When running with a soft
919 * updates implementation, most pending metadata dependencies should
920 * not wait for more than a few seconds. Thus, mounted on block devices
921 * are delayed only about a half the time that file data is delayed.
922 * Similarly, directory updates are more critical, so are only delayed
923 * about a third the time that file data is delayed. Thus, there are
924 * SYNCER_MAXDELAY queues that are processed round-robin at a rate of
925 * one each second (driven off the filesystem syncer process). The
926 * syncer_delayno variable indicates the next queue that is to be processed.
927 * Items that need to be processed soon are placed in this queue:
929 * syncer_workitem_pending[syncer_delayno]
931 * A delay of fifteen seconds is done by placing the request fifteen
932 * entries later in the queue:
934 * syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask]
939 * Add an item to the syncer work queue.
942 vn_syncer_add_to_worklist(struct vnode *vp, int delay)
948 if (vp->v_flag & VONWORKLST) {
949 LIST_REMOVE(vp, v_synclist);
952 if (delay > syncer_maxdelay - 2)
953 delay = syncer_maxdelay - 2;
954 slot = (syncer_delayno + delay) & syncer_mask;
956 LIST_INSERT_HEAD(&syncer_workitem_pending[slot], vp, v_synclist);
957 vp->v_flag |= VONWORKLST;
961 struct proc *updateproc;
962 static void sched_sync __P((void));
963 static struct kproc_desc up_kp = {
968 SYSINIT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp)
971 * System filesystem synchronizer daemon.
976 struct synclist *slp;
980 struct proc *p = updateproc;
982 EVENTHANDLER_REGISTER(shutdown_pre_sync, shutdown_kproc, p,
986 kproc_suspend_loop(p);
988 starttime = time_second;
991 * Push files whose dirty time has expired. Be careful
992 * of interrupt race on slp queue.
995 slp = &syncer_workitem_pending[syncer_delayno];
997 if (syncer_delayno == syncer_maxdelay)
1001 while ((vp = LIST_FIRST(slp)) != NULL) {
1002 if (VOP_ISLOCKED(vp, NULL) == 0) {
1003 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
1004 (void) VOP_FSYNC(vp, p->p_ucred, MNT_LAZY, p);
1005 VOP_UNLOCK(vp, 0, p);
1008 if (LIST_FIRST(slp) == vp) {
1010 * Note: v_tag VT_VFS vps can remain on the
1011 * worklist too with no dirty blocks, but
1012 * since sync_fsync() moves it to a different
1015 if (TAILQ_EMPTY(&vp->v_dirtyblkhd) &&
1016 !vn_isdisk(vp, NULL))
1017 panic("sched_sync: fsync failed vp %p tag %d", vp, vp->v_tag);
1019 * Put us back on the worklist. The worklist
1020 * routine will remove us from our current
1021 * position and then add us back in at a later
1024 vn_syncer_add_to_worklist(vp, syncdelay);
1030 * Do soft update processing.
1033 (*bioops.io_sync)(NULL);
1036 * The variable rushjob allows the kernel to speed up the
1037 * processing of the filesystem syncer process. A rushjob
1038 * value of N tells the filesystem syncer to process the next
1039 * N seconds worth of work on its queue ASAP. Currently rushjob
1040 * is used by the soft update code to speed up the filesystem
1041 * syncer process when the incore state is getting so far
1042 * ahead of the disk that the kernel memory pool is being
1043 * threatened with exhaustion.
1050 * If it has taken us less than a second to process the
1051 * current work, then wait. Otherwise start right over
1052 * again. We can still lose time if any single round
1053 * takes more than two seconds, but it does not really
1054 * matter as we are just trying to generally pace the
1055 * filesystem activity.
1057 if (time_second == starttime)
1058 tsleep(&lbolt, PPAUSE, "syncer", 0);
1063 * Request the syncer daemon to speed up its work.
1064 * We never push it to speed up more than half of its
1065 * normal turn time, otherwise it could take over the cpu.
1073 if (updateproc->p_wchan == &lbolt)
1074 setrunnable(updateproc);
1076 if (rushjob < syncdelay / 2) {
1078 stat_rush_requests += 1;
1085 * Associate a p-buffer with a vnode.
1087 * Also sets B_PAGING flag to indicate that vnode is not fully associated
1088 * with the buffer. i.e. the bp has not been linked into the vnode or
1093 register struct vnode *vp;
1094 register struct buf *bp;
1097 KASSERT(bp->b_vp == NULL, ("pbgetvp: not free"));
1100 bp->b_flags |= B_PAGING;
1101 bp->b_dev = vn_todev(vp);
1105 * Disassociate a p-buffer from a vnode.
1109 register struct buf *bp;
1112 KASSERT(bp->b_vp != NULL, ("pbrelvp: NULL"));
1115 if (bp->b_vnbufs.tqe_next != NULL) {
1117 "relpbuf(): b_vp was probably reassignbuf()d %p %x",
1122 bp->b_vp = (struct vnode *) 0;
1123 bp->b_flags &= ~B_PAGING;
1127 pbreassignbuf(bp, newvp)
1129 struct vnode *newvp;
1131 if ((bp->b_flags & B_PAGING) == 0) {
1133 "pbreassignbuf() on non phys bp %p",
1141 * Reassign a buffer from one vnode to another.
1142 * Used to assign file specific control information
1143 * (indirect blocks) to the vnode to which they belong.
1146 reassignbuf(bp, newvp)
1147 register struct buf *bp;
1148 register struct vnode *newvp;
1150 struct buflists *listheadp;
1154 if (newvp == NULL) {
1155 printf("reassignbuf: NULL");
1161 * B_PAGING flagged buffers cannot be reassigned because their vp
1162 * is not fully linked in.
1164 if (bp->b_flags & B_PAGING)
1165 panic("cannot reassign paging buffer");
1169 * Delete from old vnode list, if on one.
1171 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) {
1172 if (bp->b_xflags & BX_VNDIRTY)
1173 listheadp = &bp->b_vp->v_dirtyblkhd;
1175 listheadp = &bp->b_vp->v_cleanblkhd;
1176 TAILQ_REMOVE(listheadp, bp, b_vnbufs);
1177 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN);
1178 if (bp->b_vp != newvp) {
1180 bp->b_vp = NULL; /* for clarification */
1184 * If dirty, put on list of dirty buffers; otherwise insert onto list
1187 if (bp->b_flags & B_DELWRI) {
1190 listheadp = &newvp->v_dirtyblkhd;
1191 if ((newvp->v_flag & VONWORKLST) == 0) {
1192 switch (newvp->v_type) {
1198 if (newvp->v_specmountpoint != NULL) {
1206 vn_syncer_add_to_worklist(newvp, delay);
1208 bp->b_xflags |= BX_VNDIRTY;
1209 tbp = TAILQ_FIRST(listheadp);
1211 bp->b_lblkno == 0 ||
1212 (bp->b_lblkno > 0 && tbp->b_lblkno < 0) ||
1213 (bp->b_lblkno > 0 && bp->b_lblkno < tbp->b_lblkno)) {
1214 TAILQ_INSERT_HEAD(listheadp, bp, b_vnbufs);
1215 ++reassignbufsortgood;
1216 } else if (bp->b_lblkno < 0) {
1217 TAILQ_INSERT_TAIL(listheadp, bp, b_vnbufs);
1218 ++reassignbufsortgood;
1219 } else if (reassignbufmethod == 1) {
1221 * New sorting algorithm, only handle sequential case,
1222 * otherwise append to end (but before metadata)
1224 if ((tbp = gbincore(newvp, bp->b_lblkno - 1)) != NULL &&
1225 (tbp->b_xflags & BX_VNDIRTY)) {
1227 * Found the best place to insert the buffer
1229 TAILQ_INSERT_AFTER(listheadp, tbp, bp, b_vnbufs);
1230 ++reassignbufsortgood;
1233 * Missed, append to end, but before meta-data.
1234 * We know that the head buffer in the list is
1235 * not meta-data due to prior conditionals.
1237 * Indirect effects: NFS second stage write
1238 * tends to wind up here, giving maximum
1239 * distance between the unstable write and the
1242 tbp = TAILQ_LAST(listheadp, buflists);
1243 while (tbp && tbp->b_lblkno < 0)
1244 tbp = TAILQ_PREV(tbp, buflists, b_vnbufs);
1245 TAILQ_INSERT_AFTER(listheadp, tbp, bp, b_vnbufs);
1246 ++reassignbufsortbad;
1250 * Old sorting algorithm, scan queue and insert
1253 while ((ttbp = TAILQ_NEXT(tbp, b_vnbufs)) &&
1254 (ttbp->b_lblkno < bp->b_lblkno)) {
1258 TAILQ_INSERT_AFTER(listheadp, tbp, bp, b_vnbufs);
1261 bp->b_xflags |= BX_VNCLEAN;
1262 TAILQ_INSERT_TAIL(&newvp->v_cleanblkhd, bp, b_vnbufs);
1263 if ((newvp->v_flag & VONWORKLST) &&
1264 TAILQ_EMPTY(&newvp->v_dirtyblkhd)) {
1265 newvp->v_flag &= ~VONWORKLST;
1266 LIST_REMOVE(newvp, v_synclist);
1269 if (bp->b_vp != newvp) {
1277 * Create a vnode for a block device.
1278 * Used for mounting the root file system.
1279 * XXX: This now changed to a VCHR due to the block/char merging.
1286 register struct vnode *vp;
1294 error = getnewvnode(VT_NON, (struct mount *)0, spec_vnodeop_p, &nvp);
1307 * Add vnode to the alias list hung off the dev_t.
1309 * The reason for this gunk is that multiple vnodes can reference
1310 * the same physical device, so checking vp->v_usecount to see
1311 * how many users there are is inadequate; the v_usecount for
1312 * the vnodes need to be accumulated. vcount() does that.
1315 addaliasu(nvp, nvp_rdev)
1320 if (nvp->v_type != VBLK && nvp->v_type != VCHR)
1321 panic("addaliasu on non-special vnode");
1322 addalias(nvp, udev2dev(nvp_rdev, nvp->v_type == VBLK ? 1 : 0));
1331 if (nvp->v_type != VBLK && nvp->v_type != VCHR)
1332 panic("addalias on non-special vnode");
1335 simple_lock(&spechash_slock);
1336 SLIST_INSERT_HEAD(&dev->si_hlist, nvp, v_specnext);
1337 simple_unlock(&spechash_slock);
1341 * Grab a particular vnode from the free list, increment its
1342 * reference count and lock it. The vnode lock bit is set if the
1343 * vnode is being eliminated in vgone. The process is awakened
1344 * when the transition is completed, and an error returned to
1345 * indicate that the vnode is no longer usable (possibly having
1346 * been changed to a new file system type).
1350 register struct vnode *vp;
1357 * If the vnode is in the process of being cleaned out for
1358 * another use, we wait for the cleaning to finish and then
1359 * return failure. Cleaning is determined by checking that
1360 * the VXLOCK flag is set.
1362 if ((flags & LK_INTERLOCK) == 0) {
1363 simple_lock(&vp->v_interlock);
1365 if (vp->v_flag & VXLOCK) {
1366 vp->v_flag |= VXWANT;
1367 simple_unlock(&vp->v_interlock);
1368 tsleep((caddr_t)vp, PINOD, "vget", 0);
1374 if (VSHOULDBUSY(vp))
1376 if (flags & LK_TYPE_MASK) {
1377 if ((error = vn_lock(vp, flags | LK_INTERLOCK, p)) != 0) {
1379 * must expand vrele here because we do not want
1380 * to call VOP_INACTIVE if the reference count
1381 * drops back to zero since it was never really
1382 * active. We must remove it from the free list
1383 * before sleeping so that multiple processes do
1384 * not try to recycle it.
1386 simple_lock(&vp->v_interlock);
1388 if (VSHOULDFREE(vp))
1390 simple_unlock(&vp->v_interlock);
1394 simple_unlock(&vp->v_interlock);
1399 vref(struct vnode *vp)
1401 simple_lock(&vp->v_interlock);
1403 simple_unlock(&vp->v_interlock);
1407 * Vnode put/release.
1408 * If count drops to zero, call inactive routine and return to freelist.
1414 struct proc *p = curproc; /* XXX */
1416 KASSERT(vp != NULL, ("vrele: null vp"));
1418 simple_lock(&vp->v_interlock);
1420 if (vp->v_usecount > 1) {
1423 simple_unlock(&vp->v_interlock);
1428 if (vp->v_usecount == 1) {
1431 if (VSHOULDFREE(vp))
1434 * If we are doing a vput, the node is already locked, and we must
1435 * call VOP_INACTIVE with the node locked. So, in the case of
1436 * vrele, we explicitly lock the vnode before calling VOP_INACTIVE.
1438 if (vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK, p) == 0) {
1439 VOP_INACTIVE(vp, p);
1444 vprint("vrele: negative ref count", vp);
1445 simple_unlock(&vp->v_interlock);
1447 panic("vrele: negative ref cnt");
1455 struct proc *p = curproc; /* XXX */
1457 KASSERT(vp != NULL, ("vput: null vp"));
1459 simple_lock(&vp->v_interlock);
1461 if (vp->v_usecount > 1) {
1464 VOP_UNLOCK(vp, LK_INTERLOCK, p);
1469 if (vp->v_usecount == 1) {
1472 if (VSHOULDFREE(vp))
1475 * If we are doing a vput, the node is already locked, and we must
1476 * call VOP_INACTIVE with the node locked. So, in the case of
1477 * vrele, we explicitly lock the vnode before calling VOP_INACTIVE.
1479 simple_unlock(&vp->v_interlock);
1480 VOP_INACTIVE(vp, p);
1484 vprint("vput: negative ref count", vp);
1486 panic("vput: negative ref cnt");
1491 * Somebody doesn't want the vnode recycled.
1495 register struct vnode *vp;
1501 if (VSHOULDBUSY(vp))
1507 * One less who cares about this vnode.
1511 register struct vnode *vp;
1516 if (vp->v_holdcnt <= 0)
1517 panic("vdrop: holdcnt");
1519 if (VSHOULDFREE(vp))
1525 * Remove any vnodes in the vnode table belonging to mount point mp.
1527 * If MNT_NOFORCE is specified, there should not be any active ones,
1528 * return error if any are found (nb: this is a user error, not a
1529 * system error). If MNT_FORCE is specified, detach any active vnodes
1533 static int busyprt = 0; /* print out busy vnodes */
1534 SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, "");
1538 vflush(mp, skipvp, flags)
1540 struct vnode *skipvp;
1543 struct proc *p = curproc; /* XXX */
1544 struct vnode *vp, *nvp;
1547 simple_lock(&mntvnode_slock);
1549 for (vp = LIST_FIRST(&mp->mnt_vnodelist); vp; vp = nvp) {
1551 * Make sure this vnode wasn't reclaimed in getnewvnode().
1552 * Start over if it has (it won't be on the list anymore).
1554 if (vp->v_mount != mp)
1556 nvp = LIST_NEXT(vp, v_mntvnodes);
1558 * Skip over a selected vnode.
1563 simple_lock(&vp->v_interlock);
1565 * Skip over a vnodes marked VSYSTEM.
1567 if ((flags & SKIPSYSTEM) && (vp->v_flag & VSYSTEM)) {
1568 simple_unlock(&vp->v_interlock);
1572 * If WRITECLOSE is set, only flush out regular file vnodes
1575 if ((flags & WRITECLOSE) &&
1576 (vp->v_writecount == 0 || vp->v_type != VREG)) {
1577 simple_unlock(&vp->v_interlock);
1582 * With v_usecount == 0, all we need to do is clear out the
1583 * vnode data structures and we are done.
1585 if (vp->v_usecount == 0) {
1586 simple_unlock(&mntvnode_slock);
1588 simple_lock(&mntvnode_slock);
1593 * If FORCECLOSE is set, forcibly close the vnode. For block
1594 * or character devices, revert to an anonymous device. For
1595 * all other files, just kill them.
1597 if (flags & FORCECLOSE) {
1598 simple_unlock(&mntvnode_slock);
1599 if (vp->v_type != VBLK && vp->v_type != VCHR) {
1603 vp->v_op = spec_vnodeop_p;
1604 insmntque(vp, (struct mount *) 0);
1606 simple_lock(&mntvnode_slock);
1611 vprint("vflush: busy vnode", vp);
1613 simple_unlock(&vp->v_interlock);
1616 simple_unlock(&mntvnode_slock);
1623 * Disassociate the underlying file system from a vnode.
1626 vclean(vp, flags, p)
1635 * Check to see if the vnode is in use. If so we have to reference it
1636 * before we clean it out so that its count cannot fall to zero and
1637 * generate a race against ourselves to recycle it.
1639 if ((active = vp->v_usecount))
1643 * Prevent the vnode from being recycled or brought into use while we
1646 if (vp->v_flag & VXLOCK)
1647 panic("vclean: deadlock");
1648 vp->v_flag |= VXLOCK;
1650 * Even if the count is zero, the VOP_INACTIVE routine may still
1651 * have the object locked while it cleans it out. The VOP_LOCK
1652 * ensures that the VOP_INACTIVE routine is done with its work.
1653 * For active vnodes, it ensures that no other activity can
1654 * occur while the underlying object is being cleaned out.
1656 VOP_LOCK(vp, LK_DRAIN | LK_INTERLOCK, p);
1659 * Clean out any buffers associated with the vnode.
1661 vinvalbuf(vp, V_SAVE, NOCRED, p, 0, 0);
1662 if ((obj = vp->v_object) != NULL) {
1663 if (obj->ref_count == 0) {
1665 * vclean() may be called twice. The first time removes the
1666 * primary reference to the object, the second time goes
1667 * one further and is a special-case to terminate the object.
1669 vm_object_terminate(obj);
1672 * Woe to the process that tries to page now :-).
1674 vm_pager_deallocate(obj);
1679 * If purging an active vnode, it must be closed and
1680 * deactivated before being reclaimed. Note that the
1681 * VOP_INACTIVE will unlock the vnode.
1684 if (flags & DOCLOSE)
1685 VOP_CLOSE(vp, FNONBLOCK, NOCRED, p);
1686 VOP_INACTIVE(vp, p);
1689 * Any other processes trying to obtain this lock must first
1690 * wait for VXLOCK to clear, then call the new lock operation.
1692 VOP_UNLOCK(vp, 0, p);
1695 * Reclaim the vnode.
1697 if (VOP_RECLAIM(vp, p))
1698 panic("vclean: cannot reclaim");
1702 * Inline copy of vrele() since VOP_INACTIVE
1703 * has already been called.
1705 simple_lock(&vp->v_interlock);
1706 if (--vp->v_usecount <= 0) {
1708 if (vp->v_usecount < 0 || vp->v_writecount != 0) {
1709 vprint("vclean: bad ref count", vp);
1710 panic("vclean: ref cnt");
1715 simple_unlock(&vp->v_interlock);
1720 FREE(vp->v_vnlock, M_VNODE);
1721 vp->v_vnlock = NULL;
1724 if (VSHOULDFREE(vp))
1728 * Done with purge, notify sleepers of the grim news.
1730 vp->v_op = dead_vnodeop_p;
1733 vp->v_flag &= ~VXLOCK;
1734 if (vp->v_flag & VXWANT) {
1735 vp->v_flag &= ~VXWANT;
1736 wakeup((caddr_t) vp);
1741 * Eliminate all activity associated with the requested vnode
1742 * and with all vnodes aliased to the requested vnode.
1746 struct vop_revoke_args /* {
1751 struct vnode *vp, *vq;
1754 KASSERT((ap->a_flags & REVOKEALL) != 0, ("vop_revoke"));
1758 * If a vgone (or vclean) is already in progress,
1759 * wait until it is done and return.
1761 if (vp->v_flag & VXLOCK) {
1762 vp->v_flag |= VXWANT;
1763 simple_unlock(&vp->v_interlock);
1764 tsleep((caddr_t)vp, PINOD, "vop_revokeall", 0);
1769 simple_lock(&spechash_slock);
1770 vq = SLIST_FIRST(&dev->si_hlist);
1771 simple_unlock(&spechash_slock);
1780 * Recycle an unused vnode to the front of the free list.
1781 * Release the passed interlock if the vnode will be recycled.
1784 vrecycle(vp, inter_lkp, p)
1786 struct simplelock *inter_lkp;
1790 simple_lock(&vp->v_interlock);
1791 if (vp->v_usecount == 0) {
1793 simple_unlock(inter_lkp);
1798 simple_unlock(&vp->v_interlock);
1803 * Eliminate all activity associated with a vnode
1804 * in preparation for reuse.
1808 register struct vnode *vp;
1810 struct proc *p = curproc; /* XXX */
1812 simple_lock(&vp->v_interlock);
1817 * vgone, with the vp interlock held.
1827 * If a vgone (or vclean) is already in progress,
1828 * wait until it is done and return.
1830 if (vp->v_flag & VXLOCK) {
1831 vp->v_flag |= VXWANT;
1832 simple_unlock(&vp->v_interlock);
1833 tsleep((caddr_t)vp, PINOD, "vgone", 0);
1838 * Clean out the filesystem specific data.
1840 vclean(vp, DOCLOSE, p);
1841 simple_lock(&vp->v_interlock);
1844 * Delete from old mount point vnode list, if on one.
1846 if (vp->v_mount != NULL)
1847 insmntque(vp, (struct mount *)0);
1849 * If special device, remove it from special device alias list
1852 if ((vp->v_type == VBLK || vp->v_type == VCHR) && vp->v_rdev != NULL) {
1853 simple_lock(&spechash_slock);
1854 SLIST_REMOVE(&vp->v_hashchain, vp, struct vnode, v_specnext);
1855 freedev(vp->v_rdev);
1856 simple_unlock(&spechash_slock);
1861 * If it is on the freelist and not already at the head,
1862 * move it to the head of the list. The test of the back
1863 * pointer and the reference count of zero is because
1864 * it will be removed from the free list by getnewvnode,
1865 * but will not have its reference count incremented until
1866 * after calling vgone. If the reference count were
1867 * incremented first, vgone would (incorrectly) try to
1868 * close the previous instance of the underlying object.
1870 if (vp->v_usecount == 0 && !(vp->v_flag & VDOOMED)) {
1872 simple_lock(&vnode_free_list_slock);
1873 if (vp->v_flag & VFREE) {
1874 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
1875 } else if (vp->v_flag & VTBFREE) {
1876 TAILQ_REMOVE(&vnode_tobefree_list, vp, v_freelist);
1877 vp->v_flag &= ~VTBFREE;
1881 vp->v_flag |= VFREE;
1882 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
1883 simple_unlock(&vnode_free_list_slock);
1888 simple_unlock(&vp->v_interlock);
1892 * Lookup a vnode by device number.
1895 vfinddev(dev, type, vpp)
1902 simple_lock(&spechash_slock);
1903 SLIST_FOREACH(vp, &dev->si_hlist, v_specnext) {
1904 if (type == vp->v_type) {
1906 simple_unlock(&spechash_slock);
1910 simple_unlock(&spechash_slock);
1915 * Calculate the total number of references to a special device.
1925 simple_lock(&spechash_slock);
1926 SLIST_FOREACH(vq, &vp->v_hashchain, v_specnext)
1927 count += vq->v_usecount;
1928 simple_unlock(&spechash_slock);
1933 * Same as above, but using the dev_t as argument
1942 vp = SLIST_FIRST(&dev->si_hlist);
1949 * Print out a description of a vnode.
1951 static char *typename[] =
1952 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD"};
1962 printf("%s: %p: ", label, (void *)vp);
1964 printf("%p: ", (void *)vp);
1965 printf("type %s, usecount %d, writecount %d, refcount %d,",
1966 typename[vp->v_type], vp->v_usecount, vp->v_writecount,
1969 if (vp->v_flag & VROOT)
1970 strcat(buf, "|VROOT");
1971 if (vp->v_flag & VTEXT)
1972 strcat(buf, "|VTEXT");
1973 if (vp->v_flag & VSYSTEM)
1974 strcat(buf, "|VSYSTEM");
1975 if (vp->v_flag & VXLOCK)
1976 strcat(buf, "|VXLOCK");
1977 if (vp->v_flag & VXWANT)
1978 strcat(buf, "|VXWANT");
1979 if (vp->v_flag & VBWAIT)
1980 strcat(buf, "|VBWAIT");
1981 if (vp->v_flag & VDOOMED)
1982 strcat(buf, "|VDOOMED");
1983 if (vp->v_flag & VFREE)
1984 strcat(buf, "|VFREE");
1985 if (vp->v_flag & VOBJBUF)
1986 strcat(buf, "|VOBJBUF");
1988 printf(" flags (%s)", &buf[1]);
1989 if (vp->v_data == NULL) {
1998 #include <ddb/ddb.h>
2000 * List all of the locked vnodes in the system.
2001 * Called when debugging the kernel.
2003 DB_SHOW_COMMAND(lockedvnodes, lockedvnodes)
2005 struct proc *p = curproc; /* XXX */
2006 struct mount *mp, *nmp;
2009 printf("Locked vnodes\n");
2010 simple_lock(&mountlist_slock);
2011 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
2012 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock, p)) {
2013 nmp = TAILQ_NEXT(mp, mnt_list);
2016 LIST_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) {
2017 if (VOP_ISLOCKED(vp, NULL))
2018 vprint((char *)0, vp);
2020 simple_lock(&mountlist_slock);
2021 nmp = TAILQ_NEXT(mp, mnt_list);
2024 simple_unlock(&mountlist_slock);
2029 * Top level filesystem related information gathering.
2031 static int sysctl_ovfs_conf __P(SYSCTL_HANDLER_ARGS);
2034 vfs_sysctl SYSCTL_HANDLER_ARGS
2036 int *name = (int *)arg1 - 1; /* XXX */
2037 u_int namelen = arg2 + 1; /* XXX */
2038 struct vfsconf *vfsp;
2040 #if 1 || defined(COMPAT_PRELITE2)
2041 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
2043 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
2047 /* all sysctl names at this level are at least name and field */
2049 return (ENOTDIR); /* overloaded */
2050 if (name[0] != VFS_GENERIC) {
2051 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
2052 if (vfsp->vfc_typenum == name[0])
2055 return (EOPNOTSUPP);
2056 return ((*vfsp->vfc_vfsops->vfs_sysctl)(&name[1], namelen - 1,
2057 oldp, oldlenp, newp, newlen, p));
2061 case VFS_MAXTYPENUM:
2064 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int)));
2067 return (ENOTDIR); /* overloaded */
2068 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
2069 if (vfsp->vfc_typenum == name[2])
2072 return (EOPNOTSUPP);
2073 return (SYSCTL_OUT(req, vfsp, sizeof *vfsp));
2075 return (EOPNOTSUPP);
2078 SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD, vfs_sysctl,
2079 "Generic filesystem");
2081 #if 1 || defined(COMPAT_PRELITE2)
2084 sysctl_ovfs_conf SYSCTL_HANDLER_ARGS
2087 struct vfsconf *vfsp;
2088 struct ovfsconf ovfs;
2090 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
2091 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
2092 strcpy(ovfs.vfc_name, vfsp->vfc_name);
2093 ovfs.vfc_index = vfsp->vfc_typenum;
2094 ovfs.vfc_refcount = vfsp->vfc_refcount;
2095 ovfs.vfc_flags = vfsp->vfc_flags;
2096 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
2103 #endif /* 1 || COMPAT_PRELITE2 */
2106 #define KINFO_VNODESLOP 10
2108 * Dump vnode list (via sysctl).
2109 * Copyout address of vnode followed by vnode.
2113 sysctl_vnode SYSCTL_HANDLER_ARGS
2115 struct proc *p = curproc; /* XXX */
2116 struct mount *mp, *nmp;
2117 struct vnode *nvp, *vp;
2120 #define VPTRSZ sizeof (struct vnode *)
2121 #define VNODESZ sizeof (struct vnode)
2124 if (!req->oldptr) /* Make an estimate */
2125 return (SYSCTL_OUT(req, 0,
2126 (numvnodes + KINFO_VNODESLOP) * (VPTRSZ + VNODESZ)));
2128 simple_lock(&mountlist_slock);
2129 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
2130 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock, p)) {
2131 nmp = TAILQ_NEXT(mp, mnt_list);
2135 simple_lock(&mntvnode_slock);
2136 for (vp = LIST_FIRST(&mp->mnt_vnodelist);
2140 * Check that the vp is still associated with
2141 * this filesystem. RACE: could have been
2142 * recycled onto the same filesystem.
2144 if (vp->v_mount != mp) {
2145 simple_unlock(&mntvnode_slock);
2148 nvp = LIST_NEXT(vp, v_mntvnodes);
2149 simple_unlock(&mntvnode_slock);
2150 if ((error = SYSCTL_OUT(req, &vp, VPTRSZ)) ||
2151 (error = SYSCTL_OUT(req, vp, VNODESZ)))
2153 simple_lock(&mntvnode_slock);
2155 simple_unlock(&mntvnode_slock);
2156 simple_lock(&mountlist_slock);
2157 nmp = TAILQ_NEXT(mp, mnt_list);
2160 simple_unlock(&mountlist_slock);
2168 * Exporting the vnode list on large systems causes them to crash.
2169 * Exporting the vnode list on medium systems causes sysctl to coredump.
2172 SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE|CTLFLAG_RD,
2173 0, 0, sysctl_vnode, "S,vnode", "");
2177 * Check to see if a filesystem is mounted on a block device.
2184 if (vp->v_specmountpoint != NULL)
2190 * Unmount all filesystems. The list is traversed in reverse order
2191 * of mounting to avoid dependencies.
2200 if (curproc != NULL)
2203 p = initproc; /* XXX XXX should this be proc0? */
2205 * Since this only runs when rebooting, it is not interlocked.
2207 while(!TAILQ_EMPTY(&mountlist)) {
2208 mp = TAILQ_LAST(&mountlist, mntlist);
2209 error = dounmount(mp, MNT_FORCE, p);
2211 TAILQ_REMOVE(&mountlist, mp, mnt_list);
2212 printf("unmount of %s failed (",
2213 mp->mnt_stat.f_mntonname);
2217 printf("%d)\n", error);
2219 /* The unmount has removed mp from the mountlist */
2225 * Build hash lists of net addresses and hang them off the mount point.
2226 * Called by ufs_mount() to set up the lists of export addresses.
2229 vfs_hang_addrlist(mp, nep, argp)
2231 struct netexport *nep;
2232 struct export_args *argp;
2234 register struct netcred *np;
2235 register struct radix_node_head *rnh;
2237 struct radix_node *rn;
2238 struct sockaddr *saddr, *smask = 0;
2242 if (argp->ex_addrlen == 0) {
2243 if (mp->mnt_flag & MNT_DEFEXPORTED)
2245 np = &nep->ne_defexported;
2246 np->netc_exflags = argp->ex_flags;
2247 np->netc_anon = argp->ex_anon;
2248 np->netc_anon.cr_ref = 1;
2249 mp->mnt_flag |= MNT_DEFEXPORTED;
2252 i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
2253 np = (struct netcred *) malloc(i, M_NETADDR, M_WAITOK);
2254 bzero((caddr_t) np, i);
2255 saddr = (struct sockaddr *) (np + 1);
2256 if ((error = copyin(argp->ex_addr, (caddr_t) saddr, argp->ex_addrlen)))
2258 if (saddr->sa_len > argp->ex_addrlen)
2259 saddr->sa_len = argp->ex_addrlen;
2260 if (argp->ex_masklen) {
2261 smask = (struct sockaddr *) ((caddr_t) saddr + argp->ex_addrlen);
2262 error = copyin(argp->ex_mask, (caddr_t) smask, argp->ex_masklen);
2265 if (smask->sa_len > argp->ex_masklen)
2266 smask->sa_len = argp->ex_masklen;
2268 i = saddr->sa_family;
2269 if ((rnh = nep->ne_rtable[i]) == 0) {
2271 * Seems silly to initialize every AF when most are not used,
2272 * do so on demand here
2274 for (dom = domains; dom; dom = dom->dom_next)
2275 if (dom->dom_family == i && dom->dom_rtattach) {
2276 dom->dom_rtattach((void **) &nep->ne_rtable[i],
2280 if ((rnh = nep->ne_rtable[i]) == 0) {
2285 rn = (*rnh->rnh_addaddr) ((caddr_t) saddr, (caddr_t) smask, rnh,
2287 if (rn == 0 || np != (struct netcred *) rn) { /* already exists */
2291 np->netc_exflags = argp->ex_flags;
2292 np->netc_anon = argp->ex_anon;
2293 np->netc_anon.cr_ref = 1;
2296 free(np, M_NETADDR);
2302 vfs_free_netcred(rn, w)
2303 struct radix_node *rn;
2306 register struct radix_node_head *rnh = (struct radix_node_head *) w;
2308 (*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, rnh);
2309 free((caddr_t) rn, M_NETADDR);
2314 * Free the net address hash lists that are hanging off the mount points.
2317 vfs_free_addrlist(nep)
2318 struct netexport *nep;
2321 register struct radix_node_head *rnh;
2323 for (i = 0; i <= AF_MAX; i++)
2324 if ((rnh = nep->ne_rtable[i])) {
2325 (*rnh->rnh_walktree) (rnh, vfs_free_netcred,
2327 free((caddr_t) rnh, M_RTABLE);
2328 nep->ne_rtable[i] = 0;
2333 vfs_export(mp, nep, argp)
2335 struct netexport *nep;
2336 struct export_args *argp;
2340 if (argp->ex_flags & MNT_DELEXPORT) {
2341 if (mp->mnt_flag & MNT_EXPUBLIC) {
2342 vfs_setpublicfs(NULL, NULL, NULL);
2343 mp->mnt_flag &= ~MNT_EXPUBLIC;
2345 vfs_free_addrlist(nep);
2346 mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
2348 if (argp->ex_flags & MNT_EXPORTED) {
2349 if (argp->ex_flags & MNT_EXPUBLIC) {
2350 if ((error = vfs_setpublicfs(mp, nep, argp)) != 0)
2352 mp->mnt_flag |= MNT_EXPUBLIC;
2354 if ((error = vfs_hang_addrlist(mp, nep, argp)))
2356 mp->mnt_flag |= MNT_EXPORTED;
2363 * Set the publicly exported filesystem (WebNFS). Currently, only
2364 * one public filesystem is possible in the spec (RFC 2054 and 2055)
2367 vfs_setpublicfs(mp, nep, argp)
2369 struct netexport *nep;
2370 struct export_args *argp;
2377 * mp == NULL -> invalidate the current info, the FS is
2378 * no longer exported. May be called from either vfs_export
2379 * or unmount, so check if it hasn't already been done.
2382 if (nfs_pub.np_valid) {
2383 nfs_pub.np_valid = 0;
2384 if (nfs_pub.np_index != NULL) {
2385 FREE(nfs_pub.np_index, M_TEMP);
2386 nfs_pub.np_index = NULL;
2393 * Only one allowed at a time.
2395 if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
2399 * Get real filehandle for root of exported FS.
2401 bzero((caddr_t)&nfs_pub.np_handle, sizeof(nfs_pub.np_handle));
2402 nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid;
2404 if ((error = VFS_ROOT(mp, &rvp)))
2407 if ((error = VFS_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid)))
2413 * If an indexfile was specified, pull it in.
2415 if (argp->ex_indexfile != NULL) {
2416 MALLOC(nfs_pub.np_index, char *, MAXNAMLEN + 1, M_TEMP,
2418 error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
2419 MAXNAMLEN, (size_t *)0);
2422 * Check for illegal filenames.
2424 for (cp = nfs_pub.np_index; *cp; cp++) {
2432 FREE(nfs_pub.np_index, M_TEMP);
2437 nfs_pub.np_mount = mp;
2438 nfs_pub.np_valid = 1;
2443 vfs_export_lookup(mp, nep, nam)
2444 register struct mount *mp;
2445 struct netexport *nep;
2446 struct sockaddr *nam;
2448 register struct netcred *np;
2449 register struct radix_node_head *rnh;
2450 struct sockaddr *saddr;
2453 if (mp->mnt_flag & MNT_EXPORTED) {
2455 * Lookup in the export list first.
2459 rnh = nep->ne_rtable[saddr->sa_family];
2461 np = (struct netcred *)
2462 (*rnh->rnh_matchaddr)((caddr_t)saddr,
2464 if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
2469 * If no address match, use the default if it exists.
2471 if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
2472 np = &nep->ne_defexported;
2478 * perform msync on all vnodes under a mount point
2479 * the mount point must be locked.
2482 vfs_msync(struct mount *mp, int flags) {
2483 struct vnode *vp, *nvp;
2484 struct vm_object *obj;
2490 for (vp = LIST_FIRST(&mp->mnt_vnodelist); vp != NULL; vp = nvp) {
2492 nvp = LIST_NEXT(vp, v_mntvnodes);
2494 if (vp->v_mount != mp) {
2498 if (vp->v_flag & VXLOCK) /* XXX: what if MNT_WAIT? */
2501 if (flags != MNT_WAIT) {
2503 if (obj == NULL || (obj->flags & OBJ_MIGHTBEDIRTY) == 0)
2505 if (VOP_ISLOCKED(vp, NULL))
2509 simple_lock(&vp->v_interlock);
2511 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
2513 LK_INTERLOCK | LK_EXCLUSIVE | LK_RETRY | LK_NOOBJ, curproc)) {
2515 vm_object_page_clean(vp->v_object, 0, 0, flags == MNT_WAIT ? OBJPC_SYNC : OBJPC_NOSYNC);
2521 simple_unlock(&vp->v_interlock);
2524 if (anyio && (--tries > 0))
2529 * Create the VM object needed for VMIO and mmap support. This
2530 * is done for all VREG files in the system. Some filesystems might
2531 * afford the additional metadata buffering capability of the
2532 * VMIO code by making the device node be VMIO mode also.
2534 * vp must be locked when vfs_object_create is called.
2537 vfs_object_create(vp, p, cred)
2546 if (!vn_isdisk(vp, NULL) && vn_canvmio(vp) == FALSE)
2550 if ((object = vp->v_object) == NULL) {
2551 if (vp->v_type == VREG || vp->v_type == VDIR) {
2552 if ((error = VOP_GETATTR(vp, &vat, cred, p)) != 0)
2554 object = vnode_pager_alloc(vp, vat.va_size, 0, 0);
2555 } else if (devsw(vp->v_rdev) != NULL) {
2557 * This simply allocates the biggest object possible
2558 * for a disk vnode. This should be fixed, but doesn't
2559 * cause any problems (yet).
2561 object = vnode_pager_alloc(vp, IDX_TO_OFF(INT_MAX), 0, 0);
2566 * Dereference the reference we just created. This assumes
2567 * that the object is associated with the vp.
2569 object->ref_count--;
2572 if (object->flags & OBJ_DEAD) {
2573 VOP_UNLOCK(vp, 0, p);
2574 tsleep(object, PVM, "vodead", 0);
2575 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
2580 KASSERT(vp->v_object != NULL, ("vfs_object_create: NULL object"));
2581 vp->v_flag |= VOBJBUF;
2594 simple_lock(&vnode_free_list_slock);
2595 if (vp->v_flag & VTBFREE) {
2596 TAILQ_REMOVE(&vnode_tobefree_list, vp, v_freelist);
2597 vp->v_flag &= ~VTBFREE;
2599 if (vp->v_flag & VAGE) {
2600 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
2602 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
2605 simple_unlock(&vnode_free_list_slock);
2606 vp->v_flag &= ~VAGE;
2607 vp->v_flag |= VFREE;
2618 simple_lock(&vnode_free_list_slock);
2619 if (vp->v_flag & VTBFREE) {
2620 TAILQ_REMOVE(&vnode_tobefree_list, vp, v_freelist);
2621 vp->v_flag &= ~VTBFREE;
2623 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
2626 simple_unlock(&vnode_free_list_slock);
2627 vp->v_flag &= ~(VFREE|VAGE);
2632 * Record a process's interest in events which might happen to
2633 * a vnode. Because poll uses the historic select-style interface
2634 * internally, this routine serves as both the ``check for any
2635 * pending events'' and the ``record my interest in future events''
2636 * functions. (These are done together, while the lock is held,
2637 * to avoid race conditions.)
2640 vn_pollrecord(vp, p, events)
2645 simple_lock(&vp->v_pollinfo.vpi_lock);
2646 if (vp->v_pollinfo.vpi_revents & events) {
2648 * This leaves events we are not interested
2649 * in available for the other process which
2650 * which presumably had requested them
2651 * (otherwise they would never have been
2654 events &= vp->v_pollinfo.vpi_revents;
2655 vp->v_pollinfo.vpi_revents &= ~events;
2657 simple_unlock(&vp->v_pollinfo.vpi_lock);
2660 vp->v_pollinfo.vpi_events |= events;
2661 selrecord(p, &vp->v_pollinfo.vpi_selinfo);
2662 simple_unlock(&vp->v_pollinfo.vpi_lock);
2667 * Note the occurrence of an event. If the VN_POLLEVENT macro is used,
2668 * it is possible for us to miss an event due to race conditions, but
2669 * that condition is expected to be rare, so for the moment it is the
2670 * preferred interface.
2673 vn_pollevent(vp, events)
2677 simple_lock(&vp->v_pollinfo.vpi_lock);
2678 if (vp->v_pollinfo.vpi_events & events) {
2680 * We clear vpi_events so that we don't
2681 * call selwakeup() twice if two events are
2682 * posted before the polling process(es) is
2683 * awakened. This also ensures that we take at
2684 * most one selwakeup() if the polling process
2685 * is no longer interested. However, it does
2686 * mean that only one event can be noticed at
2687 * a time. (Perhaps we should only clear those
2688 * event bits which we note?) XXX
2690 vp->v_pollinfo.vpi_events = 0; /* &= ~events ??? */
2691 vp->v_pollinfo.vpi_revents |= events;
2692 selwakeup(&vp->v_pollinfo.vpi_selinfo);
2694 simple_unlock(&vp->v_pollinfo.vpi_lock);
2698 * Wake up anyone polling on vp because it is being revoked.
2699 * This depends on dead_poll() returning POLLHUP for correct
2706 simple_lock(&vp->v_pollinfo.vpi_lock);
2707 if (vp->v_pollinfo.vpi_events) {
2708 vp->v_pollinfo.vpi_events = 0;
2709 selwakeup(&vp->v_pollinfo.vpi_selinfo);
2711 simple_unlock(&vp->v_pollinfo.vpi_lock);
2717 * Routine to create and manage a filesystem syncer vnode.
2719 #define sync_close ((int (*) __P((struct vop_close_args *)))nullop)
2720 static int sync_fsync __P((struct vop_fsync_args *));
2721 static int sync_inactive __P((struct vop_inactive_args *));
2722 static int sync_reclaim __P((struct vop_reclaim_args *));
2723 #define sync_lock ((int (*) __P((struct vop_lock_args *)))vop_nolock)
2724 #define sync_unlock ((int (*) __P((struct vop_unlock_args *)))vop_nounlock)
2725 static int sync_print __P((struct vop_print_args *));
2726 #define sync_islocked ((int(*) __P((struct vop_islocked_args *)))vop_noislocked)
2728 static vop_t **sync_vnodeop_p;
2729 static struct vnodeopv_entry_desc sync_vnodeop_entries[] = {
2730 { &vop_default_desc, (vop_t *) vop_eopnotsupp },
2731 { &vop_close_desc, (vop_t *) sync_close }, /* close */
2732 { &vop_fsync_desc, (vop_t *) sync_fsync }, /* fsync */
2733 { &vop_inactive_desc, (vop_t *) sync_inactive }, /* inactive */
2734 { &vop_reclaim_desc, (vop_t *) sync_reclaim }, /* reclaim */
2735 { &vop_lock_desc, (vop_t *) sync_lock }, /* lock */
2736 { &vop_unlock_desc, (vop_t *) sync_unlock }, /* unlock */
2737 { &vop_print_desc, (vop_t *) sync_print }, /* print */
2738 { &vop_islocked_desc, (vop_t *) sync_islocked }, /* islocked */
2741 static struct vnodeopv_desc sync_vnodeop_opv_desc =
2742 { &sync_vnodeop_p, sync_vnodeop_entries };
2744 VNODEOP_SET(sync_vnodeop_opv_desc);
2747 * Create a new filesystem syncer vnode for the specified mount point.
2750 vfs_allocate_syncvnode(mp)
2754 static long start, incr, next;
2757 /* Allocate a new vnode */
2758 if ((error = getnewvnode(VT_VFS, mp, sync_vnodeop_p, &vp)) != 0) {
2759 mp->mnt_syncer = NULL;
2764 * Place the vnode onto the syncer worklist. We attempt to
2765 * scatter them about on the list so that they will go off
2766 * at evenly distributed times even if all the filesystems
2767 * are mounted at once.
2770 if (next == 0 || next > syncer_maxdelay) {
2774 start = syncer_maxdelay / 2;
2775 incr = syncer_maxdelay;
2779 vn_syncer_add_to_worklist(vp, syncdelay > 0 ? next % syncdelay : 0);
2780 mp->mnt_syncer = vp;
2785 * Do a lazy sync of the filesystem.
2789 struct vop_fsync_args /* {
2791 struct ucred *a_cred;
2796 struct vnode *syncvp = ap->a_vp;
2797 struct mount *mp = syncvp->v_mount;
2798 struct proc *p = ap->a_p;
2802 * We only need to do something if this is a lazy evaluation.
2804 if (ap->a_waitfor != MNT_LAZY)
2808 * Move ourselves to the back of the sync list.
2810 vn_syncer_add_to_worklist(syncvp, syncdelay);
2813 * Walk the list of vnodes pushing all that are dirty and
2814 * not already on the sync list.
2816 simple_lock(&mountlist_slock);
2817 if (vfs_busy(mp, LK_EXCLUSIVE | LK_NOWAIT, &mountlist_slock, p) != 0) {
2818 simple_unlock(&mountlist_slock);
2821 asyncflag = mp->mnt_flag & MNT_ASYNC;
2822 mp->mnt_flag &= ~MNT_ASYNC;
2823 vfs_msync(mp, MNT_NOWAIT);
2824 VFS_SYNC(mp, MNT_LAZY, ap->a_cred, p);
2826 mp->mnt_flag |= MNT_ASYNC;
2832 * The syncer vnode is no referenced.
2836 struct vop_inactive_args /* {
2847 * The syncer vnode is no longer needed and is being decommissioned.
2849 * Modifications to the worklist must be protected at splbio().
2853 struct vop_reclaim_args /* {
2857 struct vnode *vp = ap->a_vp;
2861 vp->v_mount->mnt_syncer = NULL;
2862 if (vp->v_flag & VONWORKLST) {
2863 LIST_REMOVE(vp, v_synclist);
2864 vp->v_flag &= ~VONWORKLST;
2872 * Print out a syncer vnode.
2876 struct vop_print_args /* {
2880 struct vnode *vp = ap->a_vp;
2882 printf("syncer vnode");
2883 if (vp->v_vnlock != NULL)
2884 lockmgr_printinfo(vp->v_vnlock);
2890 * extract the dev_t from a VBLK or VCHR
2896 if (vp->v_type != VBLK && vp->v_type != VCHR)
2898 return (vp->v_rdev);
2902 * Check if vnode represents a disk device
2909 if (vp->v_type != VBLK && vp->v_type != VCHR) {
2914 if (vp->v_rdev == NULL) {
2919 if (!devsw(vp->v_rdev)) {
2924 if (!(devsw(vp->v_rdev)->d_flags & D_DISK)) {
2936 struct nameidata *ndp;
2939 if (!(flags & NDF_NO_FREE_PNBUF) &&
2940 (ndp->ni_cnd.cn_flags & HASBUF)) {
2941 zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
2942 ndp->ni_cnd.cn_flags &= ~HASBUF;
2944 if (!(flags & NDF_NO_DVP_UNLOCK) &&
2945 (ndp->ni_cnd.cn_flags & LOCKPARENT) &&
2946 ndp->ni_dvp != ndp->ni_vp)
2947 VOP_UNLOCK(ndp->ni_dvp, 0, ndp->ni_cnd.cn_proc);
2948 if (!(flags & NDF_NO_DVP_RELE) &&
2949 (ndp->ni_cnd.cn_flags & (LOCKPARENT|WANTPARENT))) {
2953 if (!(flags & NDF_NO_VP_UNLOCK) &&
2954 (ndp->ni_cnd.cn_flags & LOCKLEAF) && ndp->ni_vp)
2955 VOP_UNLOCK(ndp->ni_vp, 0, ndp->ni_cnd.cn_proc);
2956 if (!(flags & NDF_NO_VP_RELE) &&
2961 if (!(flags & NDF_NO_STARTDIR_RELE) &&
2962 (ndp->ni_cnd.cn_flags & SAVESTART)) {
2963 vrele(ndp->ni_startdir);
2964 ndp->ni_startdir = NULL;